| 1 | |
| 2 | |
| 3 | use ec_core::Muxer as _; |
| 4 | use ec_ogg::granule_side_data; |
| 5 | use oxideav_core::{Error, Muxer, Packet, Result, StreamInfo}; |
| 6 | |
| 7 | use crate::WriteSeek; |
| 8 | |
| 9 | |
| 10 | |
| 11 | pub fn xiph_lace(packets: &[&[u8]]) -> Option<Vec<u8>> { |
| 12 | ec_ogg::xiph_lace(packets) |
| 13 | } |
| 14 | |
| 15 | |
| 16 | pub fn open_concrete(output: Box<dyn WriteSeek>, streams: &[StreamInfo]) -> Result<OggMuxer> { |
| 17 | let mut inner = ec_ogg::OggMuxer::new(output); |
| 18 | for stream in streams { |
| 19 | inner.add_stream(to_ec_stream(stream)?)?; |
| 20 | } |
| 21 | Ok(OggMuxer { inner }) |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | pub struct OggMuxer { |
| 27 | inner: ec_ogg::OggMuxer<Box<dyn WriteSeek>>, |
| 28 | } |
| 29 | |
| 30 | impl OggMuxer { |
| 31 | |
| 32 | pub fn set_page_target_bytes(&mut self, bytes: Option<usize>) { |
| 33 | self.inner.set_page_target_bytes(bytes); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl Muxer for OggMuxer { |
| 38 | fn format_name(&self) -> &str { |
| 39 | "ogg" |
| 40 | } |
| 41 | |
| 42 | fn write_header(&mut self) -> Result<()> { |
| 43 | Ok(self.inner.write_headers()?) |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | fn write_packet(&mut self, packet: &Packet) -> Result<()> { |
| 51 | let mut out = packet.clone(); |
| 52 | if let Some(granule) = packet.pts { |
| 53 | out.side_data.push(granule_side_data(granule)); |
| 54 | } |
| 55 | out.pts = None; |
| 56 | out.dts = None; |
| 57 | Ok(self.inner.write_packet(&out)?) |
| 58 | } |
| 59 | |
| 60 | fn write_trailer(&mut self) -> Result<()> { |
| 61 | Ok(self.inner.finish()?) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | fn to_ec_stream(stream: &StreamInfo) -> Result<ec_core::StreamInfo> { |
| 67 | let params = &stream.params; |
| 68 | let codec = params.codec_id.to_ec().ok_or_else(|| { |
| 69 | Error::Unsupported(format!( |
| 70 | "Ogg mux: codec {} is not one this family carries", |
| 71 | params.codec_id |
| 72 | )) |
| 73 | })?; |
| 74 | let mut out = ec_core::CodecParameters::new(codec); |
| 75 | if let ec_core::MediaParameters::Audio(audio) = &mut out.media { |
| 76 | if let Some(rate) = params.sample_rate { |
| 77 | audio.sample_rate = rate; |
| 78 | } |
| 79 | if let Some(channels) = params.channels { |
| 80 | audio.layout = ec_core::ChannelLayout::from_count(usize::from(channels).max(1)); |
| 81 | } |
| 82 | } |
| 83 | if !params.extradata.is_empty() { |
| 84 | out.extradata = Some(ec_core::Buf::copy_from_slice(¶ms.extradata)); |
| 85 | } |
| 86 | let mut info = ec_core::StreamInfo::new(stream.index, stream.time_base, out); |
| 87 | info.duration = stream.duration; |
| 88 | info.start_time = stream.start_time; |
| 89 | Ok(info) |
| 90 | } |