]> git.nihav.org Git - nihav.git/blob - nihav-core/src/muxers/mod.rs
y4m: if no colourspace is specified it should be YUV 4:2:0
[nihav.git] / nihav-core / src / muxers / mod.rs
1 //! Muxer definitions.
2 pub use crate::frame::*;
3 pub use crate::io::byteio::*;
4 pub use crate::demuxers::{StreamManager, StreamIter};
5 pub use crate::options::*;
6
7 /// A list specifying general muxing errors.
8 #[derive(Debug,Clone,Copy,PartialEq)]
9 #[allow(dead_code)]
10 pub 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.
30 pub type MuxerResult<T> = Result<T, MuxerError>;
31
32 /// Muxer capabilities.
33 #[derive(Clone,Copy,Debug,PartialEq)]
34 pub 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
53 impl From<ByteIOError> for MuxerError {
54 fn from(_: ByteIOError) -> Self { MuxerError::IOError }
55 }
56
57 /// A trait for muxing operations.
58 pub trait MuxCore<'a>: NAOptionHandler {
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.
70 pub struct Muxer<'a> {
71 mux: Box<dyn MuxCore<'a> + 'a>,
72 streams: StreamManager,
73 }
74
75 impl<'a> Muxer<'a> {
76 /// Constructs a new `Muxer` instance.
77 fn new(mux: Box<dyn MuxCore<'a> + 'a>, strmgr: StreamManager) -> Self {
78 Muxer {
79 mux,
80 streams: strmgr,
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 /// Queues a new packet for muxing.
101 pub fn mux_frame(&mut self, pkt: NAPacket) -> MuxerResult<()> {
102 self.mux.mux_frame(&self.streams, pkt)
103 }
104 /// Flushes the current muxing state.
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 impl<'a> NAOptionHandler for Muxer<'a> {
115 fn get_supported_options(&self) -> &[NAOptionDefinition] {
116 self.mux.get_supported_options()
117 }
118 fn set_options(&mut self, options: &[NAOption]) {
119 self.mux.set_options(options);
120 }
121 fn query_option_value(&self, name: &str) -> Option<NAValue> {
122 self.mux.query_option_value(name)
123 }
124 }
125
126 /// The trait for creating muxers.
127 pub trait MuxerCreator {
128 /// Creates new muxer instance that will use `ByteWriter` for output.
129 fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box<dyn MuxCore<'a> + 'a>;
130 /// Returns the name of current muxer creator (equal to the container name it can create).
131 fn get_name(&self) -> &'static str;
132 /// Returns muxer capabilities for the current muxer.
133 fn get_capabilities(&self) -> MuxerCapabilities;
134 }
135
136 /// Creates muxer for a provided bytestream writer.
137 pub fn create_muxer<'a>(mxcr: &dyn MuxerCreator, strmgr: StreamManager, bw: &'a mut ByteWriter<'a>) -> MuxerResult<Muxer<'a>> {
138 let mut mux = mxcr.new_muxer(bw);
139 mux.create(&strmgr)?;
140 Ok(Muxer::new(mux, strmgr))
141 }
142
143 /// List of registered muxers.
144 #[derive(Default)]
145 pub struct RegisteredMuxers {
146 muxes: Vec<&'static dyn MuxerCreator>,
147 }
148
149 impl RegisteredMuxers {
150 /// Constructs a new `RegisteredMuxers` instance.
151 pub fn new() -> Self {
152 Self { muxes: Vec::new() }
153 }
154 /// Registers a new muxer.
155 pub fn add_muxer(&mut self, mux: &'static dyn MuxerCreator) {
156 self.muxes.push(mux);
157 }
158 /// Searches for a muxer that supports requested container format.
159 pub fn find_muxer(&self, name: &str) -> Option<&dyn MuxerCreator> {
160 for &mux in self.muxes.iter() {
161 if mux.get_name() == name {
162 return Some(mux);
163 }
164 }
165 None
166 }
167 /// Provides an iterator over currently registered muxers.
168 pub fn iter(&self) -> std::slice::Iter<&dyn MuxerCreator> {
169 self.muxes.iter()
170 }
171 }