Skip to content

Commit

Permalink
Refactor crate::format (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezyLemon authored Oct 15, 2024
1 parent 03db7c3 commit e70779a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 295 deletions.
9 changes: 4 additions & 5 deletions src/device/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::ptr;

use crate::ffi::*;
use crate::format;
use crate::Format;

pub struct AudioIter(*mut AVInputFormat);

impl Iterator for AudioIter {
type Item = Format;
type Item = format::Input;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
Expand All @@ -18,7 +17,7 @@ impl Iterator for AudioIter {
} else {
self.0 = ptr;

Some(Format::Input(format::Input::wrap(ptr)))
Some(format::Input::wrap(ptr))
}
}
}
Expand All @@ -31,7 +30,7 @@ pub fn audio() -> AudioIter {
pub struct VideoIter(*mut AVInputFormat);

impl Iterator for VideoIter {
type Item = Format;
type Item = format::Input;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
Expand All @@ -42,7 +41,7 @@ impl Iterator for VideoIter {
} else {
self.0 = ptr;

Some(Format::Input(format::Input::wrap(ptr)))
Some(format::Input::wrap(ptr))
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/device/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use std::ptr;

use crate::ffi::*;
use crate::format;
use crate::Format;

pub struct AudioIter(*mut AVOutputFormat);

impl Iterator for AudioIter {
type Item = Format;
type Item = format::Output;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
Expand All @@ -18,7 +17,7 @@ impl Iterator for AudioIter {
} else {
self.0 = ptr as *mut AVOutputFormat;

Some(Format::Output(format::Output::wrap(ptr)))
Some(format::Output::wrap(ptr))
}
}
}
Expand All @@ -31,7 +30,7 @@ pub fn audio() -> AudioIter {
pub struct VideoIter(*mut AVOutputFormat);

impl Iterator for VideoIter {
type Item = Format;
type Item = format::Output;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
Expand All @@ -42,7 +41,7 @@ impl Iterator for VideoIter {
} else {
self.0 = ptr as *mut AVOutputFormat;

Some(Format::Output(format::Output::wrap(ptr)))
Some(format::Output::wrap(ptr))
}
}
}
Expand Down
110 changes: 65 additions & 45 deletions src/format/format/iter.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,92 @@
use std::ptr;
use std::ptr::null_mut;

use super::{Format, Input, Output};
use crate::ffi::*;
use crate::format::format::{Input, Output};
use libc::c_void;

pub struct Iter {
input: *mut AVInputFormat,
output: *mut AVOutputFormat,
step: Step,
pub struct DemuxerIter {
ptr: *mut c_void,
}

enum Step {
Input,
Output,
Done,
}

impl Iter {
impl DemuxerIter {
pub fn new() -> Self {
Iter {
input: ptr::null_mut(),
output: ptr::null_mut(),
step: Step::Input,
}
Self { ptr: null_mut() }
}
}

impl Default for Iter {
impl Default for DemuxerIter {
fn default() -> Self {
Self::new()
}
}

impl Iterator for Iter {
type Item = Format;
impl Iterator for DemuxerIter {
type Item = Input;

fn next(&mut self) -> Option<<Self as Iterator>::Item> {
fn next(&mut self) -> Option<Self::Item> {
unsafe {
match self.step {
Step::Input => {
let ptr = av_iformat_next(self.input);
let next = av_demuxer_iterate(&mut self.ptr);
if next.is_null() {
None
} else {
Some(Input::wrap(next as _))
}
}
}
}

if ptr.is_null() && !self.input.is_null() {
self.step = Step::Output;
pub struct MuxerIter {
ptr: *mut c_void,
}

self.next()
} else {
self.input = ptr;
impl MuxerIter {
pub fn new() -> Self {
Self { ptr: null_mut() }
}
}

Some(Format::Input(Input::wrap(ptr)))
}
}
impl Default for MuxerIter {
fn default() -> Self {
Self::new()
}
}

Step::Output => {
let ptr = av_oformat_next(self.output);
impl Iterator for MuxerIter {
type Item = Output;

if ptr.is_null() && !self.output.is_null() {
self.step = Step::Done;
fn next(&mut self) -> Option<Self::Item> {
unsafe {
let next = av_muxer_iterate(&mut self.ptr);
if next.is_null() {
None
} else {
Some(Output::wrap(next as _))
}
}
}
}

self.next()
} else {
self.output = ptr;
#[cfg(test)]
mod test {
use super::*;

Some(Format::Output(Output::wrap(ptr)))
}
}
#[test]
fn muxer_iter() {
for f in MuxerIter::new() {
println!("{}:", f.name());
println!("\t{}", f.description());
println!("\t{:?}", f.extensions());
println!("\t{:?}", f.mime_types());
}
}

Step::Done => None,
}
#[test]
fn demuxer_iter() {
for f in DemuxerIter::new() {
println!("{}:", f.name());
println!("\t{}", f.description());
println!("\t{:?}", f.extensions());
println!("\t{:?}", f.mime_types());
}
}
}
49 changes: 2 additions & 47 deletions src/format/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,10 @@ pub use self::input::Input;
mod output;
pub use self::output::Output;

#[cfg(not(feature = "ffmpeg_5_0"))]
mod iter;
#[cfg(not(feature = "ffmpeg_5_0"))]
pub use self::iter::Iter;

#[cfg(feature = "ffmpeg_4_0")]
mod new_iter;
mod iter;
#[cfg(feature = "ffmpeg_4_0")]
pub use self::new_iter::{DemuxerIter, MuxerIter};

pub enum Format {
Input(Input),
Output(Output),
}

impl Format {
pub fn name(&self) -> &str {
match *self {
Format::Input(ref f) => f.name(),
Format::Output(ref f) => f.name(),
}
}

pub fn description(&self) -> &str {
match *self {
Format::Input(ref f) => f.description(),
Format::Output(ref f) => f.description(),
}
}

pub fn extensions(&self) -> Vec<&str> {
match *self {
Format::Input(ref f) => f.extensions(),
Format::Output(ref f) => f.extensions(),
}
}

pub fn mime_types(&self) -> Vec<&str> {
match *self {
Format::Input(ref f) => f.mime_types(),
Format::Output(ref f) => f.mime_types(),
}
}
}

#[cfg(not(feature = "ffmpeg_5_0"))]
pub fn list() -> Iter {
Iter::new()
}
pub use self::iter::{DemuxerIter, MuxerIter};

#[cfg(feature = "ffmpeg_4_0")]
pub fn list_demuxers() -> DemuxerIter {
Expand Down
93 changes: 0 additions & 93 deletions src/format/format/new_iter.rs

This file was deleted.

Loading

0 comments on commit e70779a

Please sign in to comment.