add null encoder and muxer
authorKostya Shishkov <kostya.shishkov@gmail.com>
Fri, 12 Jun 2020 13:06:04 +0000 (15:06 +0200)
committerKostya Shishkov <kostya.shishkov@gmail.com>
Fri, 12 Jun 2020 13:06:04 +0000 (15:06 +0200)
src/main.rs
src/null.rs [new file with mode: 0644]

index 9e2bd88efd4830abbe1ca487a6272bc2de2f4605..55630fcdcc4161c96867f5210a70037da98c02ad 100644 (file)
@@ -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 (file)
index 0000000..eceef40
--- /dev/null
@@ -0,0 +1,102 @@
+use nihav_core::codecs::*;
+use nihav_core::muxers::*;
+use std::marker::PhantomData;
+
+#[derive(Default)]
+struct NullEncoder {
+    stream: Option<NAStreamRef>,
+    pkt:    Option<NAPacket>,
+}
+
+impl NullEncoder {
+    fn new() -> Self { Self::default() }
+}
+
+impl NAEncoder for NullEncoder {
+    fn negotiate_format(&self, encinfo: &EncodeParameters) -> EncoderResult<EncodeParameters> {
+        Ok(*encinfo)
+    }
+    fn init(&mut self, stream_id: u32, encinfo: EncodeParameters) -> EncoderResult<NAStreamRef> {
+        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<Option<NAPacket>> {
+        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<NAValue> { None }
+}
+
+fn get_encoder() -> Box<dyn NAEncoder + Send> {
+    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<NAValue> { None }
+}
+
+pub struct NullMuxerCreator {}
+
+impl MuxerCreator for NullMuxerCreator {
+    fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box<dyn MuxCore<'a> + '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{};