Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor crate::format #73

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading