forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

mux.rs90 lines, 3.0 KiBHistoryRawDownload
1//! The muxing half — the only half the replica calls.
2
3use ec_core::Muxer as _;
4use ec_ogg::granule_side_data;
5use oxideav_core::{Error, Muxer, Packet, Result, StreamInfo};
6
7use crate::WriteSeek;
8
9/// Lace header packets into the `extradata` form Vorbis setup data travels in:
10/// [`None`] when they cannot be described that way.
11pub fn xiph_lace(packets: &[&[u8]]) -> Option<Vec<u8>> {
12 ec_ogg::xiph_lace(packets)
13}
14
15/// Open a writer over `output` with `streams` already declared.
16pub 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/// The concrete muxer, so callers reach [`OggMuxer::set_page_target_bytes`]
25/// without a downcast.
26pub struct OggMuxer {
27 inner: ec_ogg::OggMuxer<Box<dyn WriteSeek>>,
28}
29
30impl OggMuxer {
31 /// Aim for `bytes` of body per page; [`None`] restores the default.
32 pub fn set_page_target_bytes(&mut self, bytes: Option<usize>) {
33 self.inner.set_page_target_bytes(bytes);
34 }
35}
36
37impl 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 /// The caller's `pts` *is* the granule position — where this packet ends —
47 /// which is the convention the incumbent muxer wrote onto its pages. It is
48 /// moved into the side data `ec_ogg` reads positions from, and the packet
49 /// keeps no start timestamp, because Ogg does not carry one.
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/// The replica's stream description, in family terms.
66fn 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(&params.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}