Skip to content

Commit e70779a

Browse files
authored
Refactor crate::format (shssoichiro#73)
1 parent 03db7c3 commit e70779a

File tree

7 files changed

+86
-295
lines changed

7 files changed

+86
-295
lines changed

src/device/input.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use std::ptr;
22

33
use crate::ffi::*;
44
use crate::format;
5-
use crate::Format;
65

76
pub struct AudioIter(*mut AVInputFormat);
87

98
impl Iterator for AudioIter {
10-
type Item = Format;
9+
type Item = format::Input;
1110

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

21-
Some(Format::Input(format::Input::wrap(ptr)))
20+
Some(format::Input::wrap(ptr))
2221
}
2322
}
2423
}
@@ -31,7 +30,7 @@ pub fn audio() -> AudioIter {
3130
pub struct VideoIter(*mut AVInputFormat);
3231

3332
impl Iterator for VideoIter {
34-
type Item = Format;
33+
type Item = format::Input;
3534

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

45-
Some(Format::Input(format::Input::wrap(ptr)))
44+
Some(format::Input::wrap(ptr))
4645
}
4746
}
4847
}

src/device/output.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ use std::ptr;
22

33
use crate::ffi::*;
44
use crate::format;
5-
use crate::Format;
65

76
pub struct AudioIter(*mut AVOutputFormat);
87

98
impl Iterator for AudioIter {
10-
type Item = Format;
9+
type Item = format::Output;
1110

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

21-
Some(Format::Output(format::Output::wrap(ptr)))
20+
Some(format::Output::wrap(ptr))
2221
}
2322
}
2423
}
@@ -31,7 +30,7 @@ pub fn audio() -> AudioIter {
3130
pub struct VideoIter(*mut AVOutputFormat);
3231

3332
impl Iterator for VideoIter {
34-
type Item = Format;
33+
type Item = format::Output;
3534

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

45-
Some(Format::Output(format::Output::wrap(ptr)))
44+
Some(format::Output::wrap(ptr))
4645
}
4746
}
4847
}

src/format/format/iter.rs

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,92 @@
1-
use std::ptr;
1+
use std::ptr::null_mut;
22

3-
use super::{Format, Input, Output};
43
use crate::ffi::*;
4+
use crate::format::format::{Input, Output};
5+
use libc::c_void;
56

6-
pub struct Iter {
7-
input: *mut AVInputFormat,
8-
output: *mut AVOutputFormat,
9-
step: Step,
7+
pub struct DemuxerIter {
8+
ptr: *mut c_void,
109
}
1110

12-
enum Step {
13-
Input,
14-
Output,
15-
Done,
16-
}
17-
18-
impl Iter {
11+
impl DemuxerIter {
1912
pub fn new() -> Self {
20-
Iter {
21-
input: ptr::null_mut(),
22-
output: ptr::null_mut(),
23-
step: Step::Input,
24-
}
13+
Self { ptr: null_mut() }
2514
}
2615
}
2716

28-
impl Default for Iter {
17+
impl Default for DemuxerIter {
2918
fn default() -> Self {
3019
Self::new()
3120
}
3221
}
3322

34-
impl Iterator for Iter {
35-
type Item = Format;
23+
impl Iterator for DemuxerIter {
24+
type Item = Input;
3625

37-
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
26+
fn next(&mut self) -> Option<Self::Item> {
3827
unsafe {
39-
match self.step {
40-
Step::Input => {
41-
let ptr = av_iformat_next(self.input);
28+
let next = av_demuxer_iterate(&mut self.ptr);
29+
if next.is_null() {
30+
None
31+
} else {
32+
Some(Input::wrap(next as _))
33+
}
34+
}
35+
}
36+
}
4237

43-
if ptr.is_null() && !self.input.is_null() {
44-
self.step = Step::Output;
38+
pub struct MuxerIter {
39+
ptr: *mut c_void,
40+
}
4541

46-
self.next()
47-
} else {
48-
self.input = ptr;
42+
impl MuxerIter {
43+
pub fn new() -> Self {
44+
Self { ptr: null_mut() }
45+
}
46+
}
4947

50-
Some(Format::Input(Input::wrap(ptr)))
51-
}
52-
}
48+
impl Default for MuxerIter {
49+
fn default() -> Self {
50+
Self::new()
51+
}
52+
}
5353

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

57-
if ptr.is_null() && !self.output.is_null() {
58-
self.step = Step::Done;
57+
fn next(&mut self) -> Option<Self::Item> {
58+
unsafe {
59+
let next = av_muxer_iterate(&mut self.ptr);
60+
if next.is_null() {
61+
None
62+
} else {
63+
Some(Output::wrap(next as _))
64+
}
65+
}
66+
}
67+
}
5968

60-
self.next()
61-
} else {
62-
self.output = ptr;
69+
#[cfg(test)]
70+
mod test {
71+
use super::*;
6372

64-
Some(Format::Output(Output::wrap(ptr)))
65-
}
66-
}
73+
#[test]
74+
fn muxer_iter() {
75+
for f in MuxerIter::new() {
76+
println!("{}:", f.name());
77+
println!("\t{}", f.description());
78+
println!("\t{:?}", f.extensions());
79+
println!("\t{:?}", f.mime_types());
80+
}
81+
}
6782

68-
Step::Done => None,
69-
}
83+
#[test]
84+
fn demuxer_iter() {
85+
for f in DemuxerIter::new() {
86+
println!("{}:", f.name());
87+
println!("\t{}", f.description());
88+
println!("\t{:?}", f.extensions());
89+
println!("\t{:?}", f.mime_types());
7090
}
7191
}
7292
}

src/format/format/mod.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,10 @@ pub use self::input::Input;
77
mod output;
88
pub use self::output::Output;
99

10-
#[cfg(not(feature = "ffmpeg_5_0"))]
11-
mod iter;
12-
#[cfg(not(feature = "ffmpeg_5_0"))]
13-
pub use self::iter::Iter;
14-
1510
#[cfg(feature = "ffmpeg_4_0")]
16-
mod new_iter;
11+
mod iter;
1712
#[cfg(feature = "ffmpeg_4_0")]
18-
pub use self::new_iter::{DemuxerIter, MuxerIter};
19-
20-
pub enum Format {
21-
Input(Input),
22-
Output(Output),
23-
}
24-
25-
impl Format {
26-
pub fn name(&self) -> &str {
27-
match *self {
28-
Format::Input(ref f) => f.name(),
29-
Format::Output(ref f) => f.name(),
30-
}
31-
}
32-
33-
pub fn description(&self) -> &str {
34-
match *self {
35-
Format::Input(ref f) => f.description(),
36-
Format::Output(ref f) => f.description(),
37-
}
38-
}
39-
40-
pub fn extensions(&self) -> Vec<&str> {
41-
match *self {
42-
Format::Input(ref f) => f.extensions(),
43-
Format::Output(ref f) => f.extensions(),
44-
}
45-
}
46-
47-
pub fn mime_types(&self) -> Vec<&str> {
48-
match *self {
49-
Format::Input(ref f) => f.mime_types(),
50-
Format::Output(ref f) => f.mime_types(),
51-
}
52-
}
53-
}
54-
55-
#[cfg(not(feature = "ffmpeg_5_0"))]
56-
pub fn list() -> Iter {
57-
Iter::new()
58-
}
13+
pub use self::iter::{DemuxerIter, MuxerIter};
5914

6015
#[cfg(feature = "ffmpeg_4_0")]
6116
pub fn list_demuxers() -> DemuxerIter {

src/format/format/new_iter.rs

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)