feat: demuxer input enum
This commit is contained in:
parent
bd898981d5
commit
3e74159ade
@ -30,8 +30,23 @@ fn main() {
|
|||||||
std::process::exit(1);
|
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 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 {
|
unsafe {
|
||||||
let info = demuxer.probe_input().expect("demuxer failed");
|
let info = demuxer.probe_input().expect("demuxer failed");
|
||||||
println!("{}", info);
|
println!("{}", info);
|
||||||
@ -43,7 +58,7 @@ fn main() {
|
|||||||
break; // EOF
|
break; // EOF
|
||||||
}
|
}
|
||||||
if let Ok(frames) = decoder.decode_pkt(pkt, stream) {
|
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
|
// do nothing but decode entire stream
|
||||||
av_frame_free(&mut frame);
|
av_frame_free(&mut frame);
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,6 @@ impl Drop for CodecContext {
|
|||||||
|
|
||||||
pub struct Decoder {
|
pub struct Decoder {
|
||||||
codecs: HashMap<i32, CodecContext>,
|
codecs: HashMap<i32, CodecContext>,
|
||||||
pts: i64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Decoder {}
|
unsafe impl Send for Decoder {}
|
||||||
@ -40,7 +39,6 @@ impl Decoder {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
codecs: HashMap::new(),
|
codecs: HashMap::new(),
|
||||||
pts: 0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
77
src/demux.rs
77
src/demux.rs
@ -1,14 +1,13 @@
|
|||||||
use anyhow::Error;
|
use anyhow::Error;
|
||||||
use ffmpeg_sys_the_third::*;
|
use ffmpeg_sys_the_third::*;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::time::Instant;
|
|
||||||
use std::{ptr, slice};
|
use std::{ptr, slice};
|
||||||
|
|
||||||
use crate::get_ffmpeg_error_msg;
|
use crate::get_ffmpeg_error_msg;
|
||||||
use crate::return_ffmpeg_error;
|
use crate::return_ffmpeg_error;
|
||||||
use slimbox::{slimbox_unsize, SlimBox, SlimMut};
|
use slimbox::{slimbox_unsize, SlimBox, SlimMut};
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use std::io::{Read, Write};
|
use std::io::Read;
|
||||||
use std::mem::transmute;
|
use std::mem::transmute;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
@ -150,11 +149,14 @@ impl Display for StreamInfoChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum DemuxerInput {
|
||||||
|
Url(String),
|
||||||
|
Reader(Option<SlimBox<dyn Read + 'static>>),
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Demuxer {
|
pub struct Demuxer {
|
||||||
ctx: *mut AVFormatContext,
|
ctx: *mut AVFormatContext,
|
||||||
input: String,
|
input: DemuxerInput,
|
||||||
started: Instant,
|
|
||||||
buffer: Option<SlimBox<dyn Read + 'static>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Demuxer {
|
impl Demuxer {
|
||||||
@ -163,9 +165,7 @@ impl Demuxer {
|
|||||||
let ps = avformat_alloc_context();
|
let ps = avformat_alloc_context();
|
||||||
Self {
|
Self {
|
||||||
ctx: ps,
|
ctx: ps,
|
||||||
input: input.to_string(),
|
input: DemuxerInput::Url(input.to_string()),
|
||||||
started: Instant::now(),
|
|
||||||
buffer: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -177,40 +177,40 @@ impl Demuxer {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
ctx: ps,
|
ctx: ps,
|
||||||
input: String::new(),
|
input: DemuxerInput::Reader(Some(slimbox_unsize!(reader))),
|
||||||
started: Instant::now(),
|
|
||||||
buffer: Some(slimbox_unsize!(reader)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn open_input(&mut self) -> libc::c_int {
|
unsafe fn open_input(&mut self) -> libc::c_int {
|
||||||
if let Some(buffer) = self.buffer.take() {
|
match &mut self.input {
|
||||||
const BUFFER_SIZE: usize = 4096;
|
DemuxerInput::Url(input) => avformat_open_input(
|
||||||
let pb = avio_alloc_context(
|
&mut self.ctx,
|
||||||
av_mallocz(BUFFER_SIZE) as *mut libc::c_uchar,
|
format!("{}\0", input).as_ptr() as *const libc::c_char,
|
||||||
BUFFER_SIZE as libc::c_int,
|
ptr::null_mut(),
|
||||||
0,
|
ptr::null_mut(),
|
||||||
buffer.into_raw(),
|
),
|
||||||
Some(read_data),
|
DemuxerInput::Reader(input) => {
|
||||||
None,
|
let input = input.take().expect("input stream already taken");
|
||||||
None,
|
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;
|
(*self.ctx).pb = pb;
|
||||||
avformat_open_input(
|
avformat_open_input(
|
||||||
&mut self.ctx,
|
&mut self.ctx,
|
||||||
ptr::null_mut(),
|
ptr::null_mut(),
|
||||||
ptr::null_mut(),
|
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(),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -297,9 +297,8 @@ impl Demuxer {
|
|||||||
impl Drop for Demuxer {
|
impl Drop for Demuxer {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
if !(*(*self.ctx).pb).opaque.is_null() {
|
if let DemuxerInput::Reader(_) = self.input {
|
||||||
let ptr: SlimBox<dyn Read> = SlimBox::from_raw((*(*self.ctx).pb).opaque);
|
drop(SlimBox::<dyn Read>::from_raw((*(*self.ctx).pb).opaque));
|
||||||
drop(ptr)
|
|
||||||
}
|
}
|
||||||
avformat_free_context(self.ctx);
|
avformat_free_context(self.ctx);
|
||||||
self.ctx = ptr::null_mut();
|
self.ctx = ptr::null_mut();
|
||||||
|
@ -6,7 +6,7 @@ mod demux;
|
|||||||
mod resample;
|
mod resample;
|
||||||
mod scale;
|
mod scale;
|
||||||
|
|
||||||
#[macro_export(crate)]
|
#[macro_export]
|
||||||
macro_rules! return_ffmpeg_error {
|
macro_rules! return_ffmpeg_error {
|
||||||
($x:expr) => {
|
($x:expr) => {
|
||||||
if $x < 0 {
|
if $x < 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user