diff --git a/examples/main.rs b/examples/main.rs index 9fe77cc..c8f45a4 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -30,8 +30,23 @@ fn main() { std::process::exit(1); }; + let cd = read_as_custom_io(path.clone()); + scan_input(cd); + + let fd = read_as_file(path.clone()); + scan_input(fd); +} + +fn read_as_custom_io(path: PathBuf) -> Demuxer { let file = File::open(path).unwrap(); - let mut demuxer = Demuxer::new_custom_io(DropTest { inner: file }); + Demuxer::new_custom_io(DropTest { inner: file }) +} + +fn read_as_file(path_buf: PathBuf) -> Demuxer { + Demuxer::new(path_buf.to_str().unwrap()) +} + +fn scan_input(mut demuxer: Demuxer) { unsafe { let info = demuxer.probe_input().expect("demuxer failed"); println!("{}", info); @@ -43,7 +58,7 @@ fn main() { break; // EOF } if let Ok(frames) = decoder.decode_pkt(pkt, stream) { - for (mut frame, stream) in frames { + for (mut frame, _stream) in frames { // do nothing but decode entire stream av_frame_free(&mut frame); } diff --git a/src/decode.rs b/src/decode.rs index e652fd7..3cc22ce 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -29,7 +29,6 @@ impl Drop for CodecContext { pub struct Decoder { codecs: HashMap, - pts: i64, } unsafe impl Send for Decoder {} @@ -40,7 +39,6 @@ impl Decoder { pub fn new() -> Self { Self { codecs: HashMap::new(), - pts: 0, } } diff --git a/src/demux.rs b/src/demux.rs index dbeb02e..c2731c8 100644 --- a/src/demux.rs +++ b/src/demux.rs @@ -1,14 +1,13 @@ use anyhow::Error; use ffmpeg_sys_the_third::*; use std::ffi::CStr; -use std::time::Instant; use std::{ptr, slice}; use crate::get_ffmpeg_error_msg; use crate::return_ffmpeg_error; use slimbox::{slimbox_unsize, SlimBox, SlimMut}; use std::fmt::{Display, Formatter}; -use std::io::{Read, Write}; +use std::io::Read; use std::mem::transmute; #[no_mangle] @@ -150,11 +149,14 @@ impl Display for StreamInfoChannel { } } +pub enum DemuxerInput { + Url(String), + Reader(Option>), +} + pub struct Demuxer { ctx: *mut AVFormatContext, - input: String, - started: Instant, - buffer: Option>, + input: DemuxerInput, } impl Demuxer { @@ -163,9 +165,7 @@ impl Demuxer { let ps = avformat_alloc_context(); Self { ctx: ps, - input: input.to_string(), - started: Instant::now(), - buffer: None, + input: DemuxerInput::Url(input.to_string()), } } } @@ -177,40 +177,40 @@ impl Demuxer { Self { ctx: ps, - input: String::new(), - started: Instant::now(), - buffer: Some(slimbox_unsize!(reader)), + input: DemuxerInput::Reader(Some(slimbox_unsize!(reader))), } } } unsafe fn open_input(&mut self) -> libc::c_int { - if let Some(buffer) = self.buffer.take() { - const BUFFER_SIZE: usize = 4096; - let pb = avio_alloc_context( - av_mallocz(BUFFER_SIZE) as *mut libc::c_uchar, - BUFFER_SIZE as libc::c_int, - 0, - buffer.into_raw(), - Some(read_data), - None, - None, - ); + match &mut self.input { + DemuxerInput::Url(input) => avformat_open_input( + &mut self.ctx, + format!("{}\0", input).as_ptr() as *const libc::c_char, + ptr::null_mut(), + ptr::null_mut(), + ), + DemuxerInput::Reader(input) => { + let input = input.take().expect("input stream already taken"); + const BUFFER_SIZE: usize = 4096; + let pb = avio_alloc_context( + av_mallocz(BUFFER_SIZE) as *mut libc::c_uchar, + BUFFER_SIZE as libc::c_int, + 0, + input.into_raw(), + Some(read_data), + None, + None, + ); - (*self.ctx).pb = pb; - avformat_open_input( - &mut self.ctx, - ptr::null_mut(), - ptr::null_mut(), - ptr::null_mut(), - ) - } else { - avformat_open_input( - &mut self.ctx, - format!("{}\0", self.input).as_ptr() as *const libc::c_char, - ptr::null_mut(), - ptr::null_mut(), - ) + (*self.ctx).pb = pb; + avformat_open_input( + &mut self.ctx, + ptr::null_mut(), + ptr::null_mut(), + ptr::null_mut(), + ) + } } } @@ -297,9 +297,8 @@ impl Demuxer { impl Drop for Demuxer { fn drop(&mut self) { unsafe { - if !(*(*self.ctx).pb).opaque.is_null() { - let ptr: SlimBox = SlimBox::from_raw((*(*self.ctx).pb).opaque); - drop(ptr) + if let DemuxerInput::Reader(_) = self.input { + drop(SlimBox::::from_raw((*(*self.ctx).pb).opaque)); } avformat_free_context(self.ctx); self.ctx = ptr::null_mut(); diff --git a/src/lib.rs b/src/lib.rs index 0f717a3..41cb5a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,7 @@ mod demux; mod resample; mod scale; -#[macro_export(crate)] +#[macro_export] macro_rules! return_ffmpeg_error { ($x:expr) => { if $x < 0 {