]>
Commit | Line | Data |
---|---|---|
a92964d5 KS |
1 | //! Muxer definitions. |
2 | pub use crate::frame::*; | |
3 | pub use crate::io::byteio::*; | |
4 | pub use crate::demuxers::{StreamManager, StreamIter}; | |
dc80f48e | 5 | pub use crate::options::*; |
a92964d5 KS |
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 | ||
f0081142 KS |
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 | ||
a92964d5 KS |
53 | impl From<ByteIOError> for MuxerError { |
54 | fn from(_: ByteIOError) -> Self { MuxerError::IOError } | |
55 | } | |
56 | ||
57 | /// A trait for muxing operations. | |
dc80f48e | 58 | pub 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. | |
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. | |
405cec9e | 77 | fn new(mux: Box<dyn MuxCore<'a> + 'a>, strmgr: StreamManager) -> Self { |
a92964d5 KS |
78 | Muxer { |
79 | mux, | |
405cec9e | 80 | streams: strmgr, |
a92964d5 KS |
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 | ||
12259bf7 | 100 | /// Queues a new packet for muxing. |
a92964d5 KS |
101 | pub fn mux_frame(&mut self, pkt: NAPacket) -> MuxerResult<()> { |
102 | self.mux.mux_frame(&self.streams, pkt) | |
103 | } | |
12259bf7 | 104 | /// Flushes the current muxing state. |
a92964d5 KS |
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 | ||
575959e8 KS |
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 | ||
a92964d5 KS |
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; | |
f0081142 KS |
132 | /// Returns muxer capabilities for the current muxer. |
133 | fn get_capabilities(&self) -> MuxerCapabilities; | |
a92964d5 KS |
134 | } |
135 | ||
136 | /// Creates muxer for a provided bytestream writer. | |
405cec9e | 137 | pub fn create_muxer<'a>(mxcr: &dyn MuxerCreator, strmgr: StreamManager, bw: &'a mut ByteWriter<'a>) -> MuxerResult<Muxer<'a>> { |
a92964d5 | 138 | let mut mux = mxcr.new_muxer(bw); |
405cec9e KS |
139 | mux.create(&strmgr)?; |
140 | Ok(Muxer::new(mux, strmgr)) | |
a92964d5 KS |
141 | } |
142 | ||
143 | /// List of registered muxers. | |
144 | #[derive(Default)] | |
145 | pub struct RegisteredMuxers { | |
ac818eac | 146 | muxes: Vec<&'static dyn MuxerCreator>, |
a92964d5 KS |
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. | |
ac818eac | 155 | pub fn add_muxer(&mut self, mux: &'static dyn MuxerCreator) { |
a92964d5 KS |
156 | self.muxes.push(mux); |
157 | } | |
158 | /// Searches for a muxer that supports requested container format. | |
ac818eac | 159 | pub fn find_muxer(&self, name: &str) -> Option<&dyn MuxerCreator> { |
e6aaad5c | 160 | self.muxes.iter().find(|&&mux| mux.get_name() == name).copied() |
a92964d5 | 161 | } |
cec53b88 | 162 | /// Provides an iterator over currently registered muxers. |
ac818eac | 163 | pub fn iter(&self) -> std::slice::Iter<&dyn MuxerCreator> { |
cec53b88 KS |
164 | self.muxes.iter() |
165 | } | |
a92964d5 | 166 | } |