Skip to main content

moq_rtc/codec/
opus.rs

1//! Opus bridge.
2//!
3//! str0m hands us one Opus packet per frame, which is exactly the
4//! raw shape that [`moq_mux::codec::opus::Import`] consumes.
5
6use crate::{Result, codec};
7
8pub struct Bridge {
9	import: moq_mux::codec::opus::Import,
10}
11
12impl Bridge {
13	pub fn new(
14		mut broadcast: moq_net::broadcast::Producer,
15		catalog: moq_mux::catalog::Producer,
16		sample_rate: u32,
17		channel_count: u32,
18	) -> Result<Self> {
19		let config = moq_mux::codec::opus::Config::new(sample_rate, channel_count);
20		let track = broadcast.unique_track(".opus", catalog.track_info())?;
21		let import = moq_mux::codec::opus::Import::new(track, catalog.reserve(), config.into())?;
22		Ok(Self { import })
23	}
24}
25
26impl codec::Bridge for Bridge {
27	fn push(&mut self, frame: codec::Frame) -> Result<()> {
28		let pts = moq_net::Timestamp::from_micros(frame.timestamp_us)
29			.map_err(|err| crate::Error::Other(anyhow::anyhow!("invalid timestamp: {err}")))?;
30		self.import.decode(&frame.payload, Some(pts))?;
31		// The importer accumulates; cut each packet into its own group (one QUIC stream) so the
32		// relay forwards it without waiting for the next.
33		self.import.cut(None)?;
34		Ok(())
35	}
36
37	fn abort(self: Box<Self>, err: moq_net::Error) {
38		self.import.abort(err);
39	}
40}