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>, str: 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 /// 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)
104 /// Returns internal seek index.
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 /// The trait for creating muxers.
115 pub 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;
120 /// Returns muxer capabilities for the current muxer.
121 fn get_capabilities(&self) -> MuxerCapabilities;
124 /// Creates muxer for a provided bytestream writer.
125 pub fn create_muxer<'a>(mxcr: &MuxerCreator, str: StreamManager, bw: &'a mut ByteWriter<'a>) -> MuxerResult<Muxer<'a>> {
126 let mut mux = mxcr.new_muxer(bw);
128 Ok(Muxer::new(mux, str))
131 /// List of registered muxers.
133 pub struct RegisteredMuxers {
134 muxes: Vec<&'static MuxerCreator>,
137 impl RegisteredMuxers {
138 /// Constructs a new `RegisteredMuxers` instance.
139 pub fn new() -> Self {
140 Self { muxes: Vec::new() }
142 /// Registers a new muxer.
143 pub fn add_muxer(&mut self, mux: &'static MuxerCreator) {
144 self.muxes.push(mux);
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 {