aboutsummaryrefslogtreecommitdiffstats
path: root/src/null.rs
blob: eece63b3650dd8e4080163710c6f5add3792df64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 get_capabilities(&self) -> u64 { 0 }
    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, 0);
        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 };

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{};