make muxers handle options
[nihav.git] / nihav-core / src / muxers / mod.rs
CommitLineData
a92964d5
KS
1//! Muxer definitions.
2pub use crate::frame::*;
3pub use crate::io::byteio::*;
4pub use crate::demuxers::{StreamManager, StreamIter};
dc80f48e 5pub use crate::options::*;
a92964d5
KS
6
7/// A list specifying general muxing errors.
8#[derive(Debug,Clone,Copy,PartialEq)]
9#[allow(dead_code)]
10pub enum MuxerError {
11 /// An invalid argument was provided to the muxer.
12 InvalidArgument,
13 /// Trying to mux data without header being written.
14 NotCreated,
15 /// Muxer encountered invalid input packet.
16 InvalidData,
17 /// Input stream cannot be stored in this container format.
18 UnsupportedFormat,
19 /// Data writing error.
20 IOError,
21 /// Feature is not implemented.
22 NotImplemented,
23 /// Allocation failed.
24 MemoryError,
25 /// Operation cannot succeed in principle (e.g. seeking in an output stream not supporting seeking).
26 NotPossible,
27}
28
29/// A specialised `Result` type for muxing operations.
30pub type MuxerResult<T> = Result<T, MuxerError>;
31
f0081142
KS
32/// Muxer capabilities.
33#[derive(Clone,Copy,Debug,PartialEq)]
34pub enum MuxerCapabilities {
35 /// Muxer accepts single video stream with certain codec.
36 ///
37 /// Codec name `"any"` means various codecs are supported.
38 SingleVideo(&'static str),
39 /// Muxer accepts single audio stream with certain codec.
40 ///
41 /// Codec name `"any"` means various codecs are supported.
42 SingleAudio(&'static str),
43 /// Muxer accepts single video stream and single audio stream with defined codecs.
44 SingleVideoAndAudio(&'static str, &'static str),
45 /// Muxer accepts only video streams but can mux several video streams.
46 OnlyVideo,
47 /// Muxer accepts only audio streams but can mux several video streams..
48 OnlyAudio,
49 /// Muxer accepts variable amount of streams of any type.
50 Universal,
51}
52
a92964d5
KS
53impl From<ByteIOError> for MuxerError {
54 fn from(_: ByteIOError) -> Self { MuxerError::IOError }
55}
56
57/// A trait for muxing operations.
dc80f48e 58pub trait MuxCore<'a>: NAOptionHandler {
a92964d5
KS
59 /// Prepares everything for packet muxing.
60 fn create(&mut self, strmgr: &StreamManager) -> MuxerResult<()>;
61 /// Queues a packet for muxing.
62 fn mux_frame(&mut self, strmgr: &StreamManager, pkt: NAPacket) -> MuxerResult<()>;
63 /// Flushes the current muxing state.
64 fn flush(&mut self) -> MuxerResult<()>;
65 /// Finishes muxing and writes necessary header and trailer information if needed.
66 fn end(&mut self) -> MuxerResult<()>;
67}
68
69/// Muxer structure with auxiliary data.
70pub struct Muxer<'a> {
71 mux: Box<dyn MuxCore<'a> + 'a>,
72 streams: StreamManager,
73}
74
75impl<'a> Muxer<'a> {
76 /// Constructs a new `Muxer` instance.
77 fn new(mux: Box<dyn MuxCore<'a> + 'a>, str: StreamManager) -> Self {
78 Muxer {
79 mux,
80 streams: str,
81 }
82 }
83 /// Returns a stream reference by its number.
84 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
85 self.streams.get_stream(idx)
86 }
87 /// Returns a stream reference by its ID.
88 pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> {
89 self.streams.get_stream_by_id(id)
90 }
91 /// Reports the total number of streams.
92 pub fn get_num_streams(&self) -> usize {
93 self.streams.get_num_streams()
94 }
95 /// Returns an iterator over streams.
96 pub fn get_streams(&self) -> StreamIter {
97 self.streams.iter()
98 }
99
100 /// Demuxes a new packet from the container.
101 pub fn mux_frame(&mut self, pkt: NAPacket) -> MuxerResult<()> {
102 self.mux.mux_frame(&self.streams, pkt)
103 }
104 /// Returns internal seek index.
105 pub fn flush(&mut self) -> MuxerResult<()> {
106 self.mux.flush()
107 }
108 /// Finishes muxing and writes necessary header and trailer information if needed.
109 pub fn end(mut self) -> MuxerResult<()> {
110 self.mux.end()
111 }
112}
113
114/// The trait for creating muxers.
115pub trait MuxerCreator {
116 /// Creates new muxer instance that will use `ByteWriter` for output.
117 fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box<dyn MuxCore<'a> + 'a>;
118 /// Returns the name of current muxer creator (equal to the container name it can create).
119 fn get_name(&self) -> &'static str;
f0081142
KS
120 /// Returns muxer capabilities for the current muxer.
121 fn get_capabilities(&self) -> MuxerCapabilities;
a92964d5
KS
122}
123
124/// Creates muxer for a provided bytestream writer.
125pub fn create_muxer<'a>(mxcr: &MuxerCreator, str: StreamManager, bw: &'a mut ByteWriter<'a>) -> MuxerResult<Muxer<'a>> {
126 let mut mux = mxcr.new_muxer(bw);
127 mux.create(&str)?;
128 Ok(Muxer::new(mux, str))
129}
130
131/// List of registered muxers.
132#[derive(Default)]
133pub struct RegisteredMuxers {
134 muxes: Vec<&'static MuxerCreator>,
135}
136
137impl RegisteredMuxers {
138 /// Constructs a new `RegisteredMuxers` instance.
139 pub fn new() -> Self {
140 Self { muxes: Vec::new() }
141 }
142 /// Registers a new muxer.
143 pub fn add_muxer(&mut self, mux: &'static MuxerCreator) {
144 self.muxes.push(mux);
145 }
146 /// Searches for a muxer that supports requested container format.
147 pub fn find_muxer(&self, name: &str) -> Option<&MuxerCreator> {
148 for &mux in self.muxes.iter() {
149 if mux.get_name() == name {
150 return Some(mux);
151 }
152 }
153 None
154 }
155}