From 935215063aed6eb588d0041b677eab6af53f58cd Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Fri, 12 Jun 2020 15:06:04 +0200 Subject: [PATCH] add null encoder and muxer --- src/main.rs | 5 +++ src/null.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/null.rs diff --git a/src/main.rs b/src/main.rs index 9e2bd88..55630fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,9 @@ use nihav_registry::detect; use nihav_allstuff::*; use std::env; +mod null; +use null::*; + fn print_options(name: &str, options: &[NAOptionDefinition]) { if options.is_empty() { println!("No custom options."); @@ -558,8 +561,10 @@ fn main() { let mut mux_reg = RegisteredMuxers::new(); nihav_register_all_muxers(&mut mux_reg); + mux_reg.add_muxer(NULL_MUXER); let mut enc_reg = RegisteredEncoders::new(); nihav_register_all_encoders(&mut enc_reg); + enc_reg.add_encoder(NULL_ENCODER); let mut transcoder = Transcoder::new(); diff --git a/src/null.rs b/src/null.rs new file mode 100644 index 0000000..eceef40 --- /dev/null +++ b/src/null.rs @@ -0,0 +1,102 @@ +use nihav_core::codecs::*; +use nihav_core::muxers::*; +use std::marker::PhantomData; + +#[derive(Default)] +struct NullEncoder { + stream: Option, + pkt: Option, +} + +impl NullEncoder { + fn new() -> Self { Self::default() } +} + +impl NAEncoder for NullEncoder { + fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult { + Ok(*encinfo) + } + fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult { + let stype = match encinfo.format { + NACodecTypeInfo::Audio(_) => StreamType::Audio, + NACodecTypeInfo::Video(_) => StreamType::Video, + NACodecTypeInfo::None => StreamType::Data, + }; + let info = NACodecInfo::new("null", encinfo.format, None); + let mut stream = NAStream::new(stype, stream_id, info, encinfo.tb_num, encinfo.tb_den); + stream.set_num(stream_id as usize); + let stream = stream.into_ref(); + self.stream = Some(stream.clone()); + + Ok(stream) + } + fn encode(&mut self, frm: &NAFrame) -> EncoderResult<()> { + self.pkt = Some(NAPacket::new(self.stream.clone().unwrap(), frm.ts, true, Vec::new())); + Ok(()) + } + fn get_packet(&mut self) -> EncoderResult> { + let mut npkt = None; + std::mem::swap(&mut self.pkt, &mut npkt); + Ok(npkt) + } + fn flush(&mut self) -> EncoderResult<()> { + Ok(()) + } +} + +impl NAOptionHandler for NullEncoder { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + +fn get_encoder() -> Box { + Box::new(NullEncoder::new()) +} + +pub const NULL_ENCODER: EncoderInfo = EncoderInfo { name: "null", get_encoder: get_encoder }; + +struct NullMuxer<'a> { + bw: PhantomData<&'a mut ByteWriter<'a>>, +} + +impl<'a> NullMuxer<'a> { + fn new(_bw: &'a mut ByteWriter<'a>) -> Self { + Self { + bw: PhantomData::default(), + } + } +} + +impl<'a> MuxCore<'a> for NullMuxer<'a> { + fn create(&mut self, _strmgr: &StreamManager) -> MuxerResult<()> { + Ok(()) + } + fn mux_frame(&mut self, _strmgr: &StreamManager, _pkt: NAPacket) -> MuxerResult<()> { + Ok(()) + } + fn flush(&mut self) -> MuxerResult<()> { + Ok(()) + } + fn end(&mut self) -> MuxerResult<()> { + Ok(()) + } +} + +impl<'a> NAOptionHandler for NullMuxer<'a> { + fn get_supported_options(&self) -> &[NAOptionDefinition] { &[] } + fn set_options(&mut self, _options: &[NAOption]) { } + fn query_option_value(&self, _name: &str) -> Option { None } +} + +pub struct NullMuxerCreator {} + +impl MuxerCreator for NullMuxerCreator { + fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box + 'a> { + Box::new(NullMuxer::new(bw)) + } + fn get_name(&self) -> &'static str { "null" } + fn get_capabilities(&self) -> MuxerCapabilities { MuxerCapabilities::Universal } +} + +pub const NULL_MUXER: &dyn MuxerCreator = &NullMuxerCreator{}; -- 2.30.2