Skip to content

Commit 2c9415c

Browse files
alfredoyangkinetiknz
authored andcommitted
Dump structure from mp4parse_capi API
1 parent 7df7df7 commit 2c9415c

File tree

2 files changed

+115
-31
lines changed

2 files changed

+115
-31
lines changed

mp4parse_capi/examples/dump.rs

Lines changed: 109 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,119 @@
11
extern crate mp4parse;
2+
extern crate mp4parse_capi;
23

34
use std::env;
45
use std::fs::File;
5-
use std::io::{Seek, SeekFrom};
6+
use std::io::Read;
7+
use mp4parse_capi::*;
8+
9+
extern fn buf_read(buf: *mut u8, size: usize, userdata: *mut std::os::raw::c_void) -> isize {
10+
let input: &mut std::fs::File = unsafe { &mut *(userdata as *mut _) };
11+
let mut buf = unsafe { std::slice::from_raw_parts_mut(buf, size) };
12+
match input.read(&mut buf) {
13+
Ok(n) => n as isize,
14+
Err(_) => -1,
15+
}
16+
}
617

718
fn dump_file(filename: &str, verbose: bool) {
8-
let mut reader = match File::open(filename) {
9-
Ok(reader) => reader,
10-
_ => {
11-
println!("ERROR: invalid path '{}'", filename);
12-
return;
13-
}
19+
let mut file = File::open(filename).expect("Unknown file");
20+
let io = mp4parse_io {
21+
read: Some(buf_read),
22+
userdata: &mut file as *mut _ as *mut std::os::raw::c_void
1423
};
15-
let mut context = mp4parse::MediaContext::new();
16-
// Turn on debug output.
17-
if verbose {
18-
mp4parse::set_debug_mode(true);
19-
}
20-
// Read all boxes.
21-
match mp4parse::read_mp4(&mut reader, &mut context) {
22-
Ok(_) => {},
23-
Err(mp4parse::Error::Io(e)) => {
24-
println!("I/O ERROR: {:?} in '{}'", e, filename);
25-
panic!(e);
26-
},
27-
Err(e) => {
28-
let offset = reader.seek(SeekFrom::Current(0)).unwrap();
29-
println!("ERROR: {:?} in '{}' @ {}", e, filename, offset);
30-
},
31-
}
32-
if verbose {
33-
println!("-- result of parsing '{}' --\n{:?}", filename, context);
24+
25+
unsafe {
26+
let parser = mp4parse_new(&io);
27+
28+
if verbose {
29+
mp4parse_log(true);
30+
}
31+
32+
match mp4parse_read(parser) {
33+
mp4parse_status::OK => (),
34+
_ => {
35+
println!("-- fail to parse, '-v' for more info");
36+
return;
37+
},
38+
}
39+
40+
let mut frag_info = mp4parse_fragment_info { .. Default::default() };
41+
match mp4parse_get_fragment_info(parser, &mut frag_info) {
42+
mp4parse_status::OK => {
43+
println!("-- mp4parse_fragment_info {:?}", frag_info);
44+
},
45+
_ => {
46+
println!("-- mp4parse_fragment_info failed");
47+
return;
48+
}
49+
}
50+
51+
let mut counts: u32 = 0;
52+
match mp4parse_get_track_count(parser, &mut counts) {
53+
mp4parse_status::OK => (),
54+
_ => {
55+
println!("-- mp4parse_get_track_count failed");
56+
return;
57+
}
58+
}
59+
60+
for i in 0 .. counts {
61+
let mut track_info = mp4parse_track_info {
62+
track_type: mp4parse_track_type::AUDIO,
63+
codec: mp4parse_codec::UNKNOWN,
64+
track_id: 0,
65+
duration: 0,
66+
media_time: 0,
67+
};
68+
match mp4parse_get_track_info(parser, i, &mut track_info) {
69+
mp4parse_status::OK => {
70+
println!("-- mp4parse_get_track_info {:?}", track_info);
71+
},
72+
_ => {
73+
println!("-- mp4parse_get_track_info failed, track id: {}", i);
74+
return;
75+
}
76+
}
77+
78+
match track_info.track_type {
79+
mp4parse_track_type::AUDIO => {
80+
let mut audio_info = mp4parse_track_audio_info { .. Default::default() };
81+
match mp4parse_get_track_audio_info(parser, i, &mut audio_info) {
82+
mp4parse_status::OK => {
83+
println!("-- mp4parse_get_track_audio_info {:?}", audio_info);
84+
},
85+
_ => {
86+
println!("-- mp4parse_get_track_audio_info failed, track id: {}", i);
87+
return;
88+
}
89+
}
90+
},
91+
mp4parse_track_type::VIDEO => {
92+
let mut video_info = mp4parse_track_video_info { .. Default::default() };
93+
match mp4parse_get_track_video_info(parser, i, &mut video_info) {
94+
mp4parse_status::OK => {
95+
println!("-- mp4parse_get_track_video_info {:?}", video_info);
96+
},
97+
_ => {
98+
println!("-- mp4parse_get_track_video_info failed, track id: {}", i);
99+
return;
100+
}
101+
}
102+
},
103+
}
104+
105+
let mut indices = mp4parse_byte_data::default();
106+
match mp4parse_get_indice_table(parser, track_info.track_id, &mut indices) {
107+
mp4parse_status::OK => {
108+
println!("-- mp4parse_get_indice_table track_id {} indices {:?}", track_info.track_id, indices);
109+
},
110+
_ => {
111+
println!("-- mp4parse_get_indice_table failed, track_info.track_id: {}", track_info.track_id);
112+
return;
113+
}
114+
}
115+
}
116+
mp4parse_free(parser);
34117
}
35118
}
36119

mp4parse_capi/src/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Default for mp4parse_codec {
107107
}
108108

109109
#[repr(C)]
110-
#[derive(Default)]
110+
#[derive(Default, Debug)]
111111
pub struct mp4parse_track_info {
112112
pub track_type: mp4parse_track_type,
113113
pub codec: mp4parse_codec,
@@ -129,6 +129,7 @@ pub struct mp4parse_indice {
129129
}
130130

131131
#[repr(C)]
132+
#[derive(Debug)]
132133
pub struct mp4parse_byte_data {
133134
pub length: u32,
134135
// cheddar can't handle generic type, so it needs to be multiple data types here.
@@ -164,15 +165,15 @@ pub struct mp4parse_pssh_info {
164165
}
165166

166167
#[repr(C)]
167-
#[derive(Default)]
168+
#[derive(Default, Debug)]
168169
pub struct mp4parse_sinf_info {
169170
pub is_encrypted: u32,
170171
pub iv_size: u8,
171172
pub kid: mp4parse_byte_data,
172173
}
173174

174175
#[repr(C)]
175-
#[derive(Default)]
176+
#[derive(Default, Debug)]
176177
pub struct mp4parse_track_audio_info {
177178
pub channels: u16,
178179
pub bit_depth: u16,
@@ -184,7 +185,7 @@ pub struct mp4parse_track_audio_info {
184185
}
185186

186187
#[repr(C)]
187-
#[derive(Default)]
188+
#[derive(Default, Debug)]
188189
pub struct mp4parse_track_video_info {
189190
pub display_width: u32,
190191
pub display_height: u32,
@@ -196,7 +197,7 @@ pub struct mp4parse_track_video_info {
196197
}
197198

198199
#[repr(C)]
199-
#[derive(Default)]
200+
#[derive(Default, Debug)]
200201
pub struct mp4parse_fragment_info {
201202
pub fragment_duration: u64,
202203
// TODO:

0 commit comments

Comments
 (0)