2 pub use crate::frame::*;
3 pub use crate::io::byteio::*;
4 pub use crate::demuxers::{StreamManager, StreamIter};
6 /// A list specifying general muxing errors.
7 #[derive(Debug,Clone,Copy,PartialEq)]
10 /// An invalid argument was provided to the muxer.
12 /// Trying to mux data without header being written.
14 /// Muxer encountered invalid input packet.
16 /// Input stream cannot be stored in this container format.
18 /// Data writing error.
20 /// Feature is not implemented.
22 /// Allocation failed.
24 /// Operation cannot succeed in principle (e.g. seeking in an output stream not supporting seeking).
28 /// A specialised `Result` type for muxing operations.
29 pub type MuxerResult<T> = Result<T, MuxerError>;
31 /// Muxer capabilities.
32 #[derive(Clone,Copy,Debug,PartialEq)]
33 pub enum MuxerCapabilities {
34 /// Muxer accepts single video stream with certain codec.
36 /// Codec name `"any"` means various codecs are supported.
37 SingleVideo(&'static str),
38 /// Muxer accepts single audio stream with certain codec.
40 /// Codec name `"any"` means various codecs are supported.
41 SingleAudio(&'static str),
42 /// Muxer accepts single video stream and single audio stream with defined codecs.
43 SingleVideoAndAudio(&'static str, &'static str),
44 /// Muxer accepts only video streams but can mux several video streams.
46 /// Muxer accepts only audio streams but can mux several video streams..
48 /// Muxer accepts variable amount of streams of any type.
52 impl From<ByteIOError> for MuxerError {
53 fn from(_: ByteIOError) -> Self { MuxerError::IOError }
56 /// A trait for muxing operations.
57 pub trait MuxCore<'a> {
58 /// Prepares everything for packet muxing.
59 fn create(&mut self, strmgr: &StreamManager) -> MuxerResult<()>;
60 /// Queues a packet for muxing.
61 fn mux_frame(&mut self, strmgr: &StreamManager, pkt: NAPacket) -> MuxerResult<()>;
62 /// Flushes the current muxing state.
63 fn flush(&mut self) -> MuxerResult<()>;
64 /// Finishes muxing and writes necessary header and trailer information if needed.
65 fn end(&mut self) -> MuxerResult<()>;
68 /// Muxer structure with auxiliary data.
69 pub struct Muxer<'a> {
70 mux: Box<dyn MuxCore<'a> + 'a>,
71 streams: StreamManager,
75 /// Constructs a new `Muxer` instance.
76 fn new(mux: Box<dyn MuxCore<'a> + 'a>, str: StreamManager) -> Self {
82 /// Returns a stream reference by its number.
83 pub fn get_stream(&self, idx: usize) -> Option<NAStreamRef> {
84 self.streams.get_stream(idx)
86 /// Returns a stream reference by its ID.
87 pub fn get_stream_by_id(&self, id: u32) -> Option<NAStreamRef> {
88 self.streams.get_stream_by_id(id)
90 /// Reports the total number of streams.
91 pub fn get_num_streams(&self) -> usize {
92 self.streams.get_num_streams()
94 /// Returns an iterator over streams.
95 pub fn get_streams(&self) -> StreamIter {
99 /// Demuxes a new packet from the container.
100 pub fn mux_frame(&mut self, pkt: NAPacket) -> MuxerResult<()> {
101 self.mux.mux_frame(&self.streams, pkt)
103 /// Returns internal seek index.
104 pub fn flush(&mut self) -> MuxerResult<()> {
107 /// Finishes muxing and writes necessary header and trailer information if needed.
108 pub fn end(mut self) -> MuxerResult<()> {
113 /// The trait for creating muxers.
114 pub trait MuxerCreator {
115 /// Creates new muxer instance that will use `ByteWriter` for output.
116 fn new_muxer<'a>(&self, bw: &'a mut ByteWriter<'a>) -> Box<dyn MuxCore<'a> + 'a>;
117 /// Returns the name of current muxer creator (equal to the container name it can create).
118 fn get_name(&self) -> &'static str;
119 /// Returns muxer capabilities for the current muxer.
120 fn get_capabilities(&self) -> MuxerCapabilities;
123 /// Creates muxer for a provided bytestream writer.
124 pub fn create_muxer<'a>(mxcr: &MuxerCreator, str: StreamManager, bw: &'a mut ByteWriter<'a>) -> MuxerResult<Muxer<'a>> {
125 let mut mux = mxcr.new_muxer(bw);
127 Ok(Muxer::new(mux, str))
130 /// List of registered muxers.
132 pub struct RegisteredMuxers {
133 muxes: Vec<&'static MuxerCreator>,
136 impl RegisteredMuxers {
137 /// Constructs a new `RegisteredMuxers` instance.
138 pub fn new() -> Self {
139 Self { muxes: Vec::new() }
141 /// Registers a new muxer.
142 pub fn add_muxer(&mut self, mux: &'static MuxerCreator) {
143 self.muxes.push(mux);
145 /// Searches for a muxer that supports requested container format.
146 pub fn find_muxer(&self, name: &str) -> Option<&MuxerCreator> {
147 for &mux in self.muxes.iter() {
148 if mux.get_name() == name {