2 pub use crate::frame::*;
3 pub use crate::io::byteio::*;
4 pub use crate::demuxers::{StreamManager, StreamIter};
5 pub use crate::options::*;
7 /// A list specifying general muxing errors.
8 #[derive(Debug,Clone,Copy,PartialEq)]
11 /// An invalid argument was provided to the muxer.
13 /// Trying to mux data without header being written.
15 /// Muxer encountered invalid input packet.
17 /// Input stream cannot be stored in this container format.
19 /// Data writing error.
21 /// Feature is not implemented.
23 /// Allocation failed.
25 /// Operation cannot succeed in principle (e.g. seeking in an output stream not supporting seeking).
29 /// A specialised `Result` type for muxing operations.
30 pub type MuxerResult<T> = Result<T, MuxerError>;
32 /// Muxer capabilities.
33 #[derive(Clone,Copy,Debug,PartialEq)]
34 pub enum MuxerCapabilities {
35 /// Muxer accepts single video stream with certain codec.
37 /// Codec name `"any"` means various codecs are supported.
38 SingleVideo(&'static str),
39 /// Muxer accepts single audio stream with certain codec.
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.
47 /// Muxer accepts only audio streams but can mux several video streams..
49 /// Muxer accepts variable amount of streams of any type.
53 impl From<ByteIOError> for MuxerError {
54 fn from(_: ByteIOError) -> Self { MuxerError::IOError }
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<()>;
69 /// Muxer structure with auxiliary data.
70 pub struct Muxer<'a> {
71 mux: Box<dyn MuxCore<'a> + 'a>,
72 streams: StreamManager,
76 /// Constructs a new `Muxer` instance.
77 fn new(mux: Box<dyn MuxCore<'a> + 'a>, strmgr: StreamManager) -> Self {
83 /// Returns a stream reference by its number.
84 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
85 self.streams.get_stream(idx)
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)
91 /// Reports the total number of streams.
92 pub fn get_num_streams(&self) -> usize {
93 self.streams.get_num_streams()
95 /// Returns an iterator over streams.
96 pub fn get_streams(&self) -> StreamIter {
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)
104 /// Flushes the current muxing state.
105 pub fn flush(&mut self) -> MuxerResult<()> {
108 /// Finishes muxing and writes necessary header and trailer information if needed.
109 pub fn end(mut self) -> MuxerResult<()> {
114 impl<'a> NAOptionHandler for Muxer<'a> {
115 fn get_supported_options(&self) -> &[NAOptionDefinition] {
116 self.mux.get_supported_options()
118 fn set_options(&mut self, options: &[NAOption]) {
119 self.mux.set_options(options);
121 fn query_option_value(&self, name: &str) -> Option<NAValue> {
122 self.mux.query_option_value(name)
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;
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))
143 /// List of registered muxers.
145 pub struct RegisteredMuxers {
146 muxes: Vec<&'static dyn MuxerCreator>,
149 impl RegisteredMuxers {
150 /// Constructs a new `RegisteredMuxers` instance.
151 pub fn new() -> Self {
152 Self { muxes: Vec::new() }
154 /// Registers a new muxer.
155 pub fn add_muxer(&mut self, mux: &'static dyn MuxerCreator) {
156 self.muxes.push(mux);
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 {
167 /// Provides an iterator over currently registered muxers.
168 pub fn iter(&self) -> std::slice::Iter<&dyn MuxerCreator> {