Skip to main content

moq_wasm/media/
media_source.rs

1use std::sync::{Arc, Mutex};
2
3use broadcast::{
4    Broadcast, DemandChanges, MediaRendition, MediaSourceConfig, MediaSourceError, TrackKind,
5};
6use bytes::Bytes;
7use mediapack::AudioLevel;
8use moqt::publish::TrackDemand;
9use tokio::sync::Notify;
10
11use super::broadcast_end::BroadcastEnd;
12
13#[derive(Debug, Clone)]
14pub struct MediaSourceOptions {
15    /// The video track, `video` by default; its media timeline is
16    /// `<video>_timeline`.
17    pub video: String,
18    /// The audio track, `audio` by default.
19    pub audio: String,
20    /// The catalog's label of the video, and of the audio with ` audio`
21    /// after it.
22    pub label: String,
23    /// The encoder's target in bits per second, for the catalog.
24    pub video_bitrate: Option<u64>,
25    /// The frames per second the encoder is capped at, for the catalog.
26    pub video_framerate: Option<u32>,
27    /// Has the relay ask for the tracks only while a subscriber takes them
28    /// (draft-18 §10.10), reported by `MediaSource::demand_change`; `true`
29    /// by default.
30    pub on_demand: bool,
31}
32
33impl MediaSourceOptions {
34    pub fn new(label: impl Into<String>) -> Self {
35        Self {
36            video: "video".into(),
37            audio: "audio".into(),
38            label: label.into(),
39            video_bitrate: None,
40            video_framerate: None,
41            on_demand: true,
42        }
43    }
44}
45
46/// A video track with its renditions and an audio track, fed with encoded
47/// frames and published as LOC with the MSF catalog and media timeline.
48/// Once its broadcast is closed or dropped, its pushes fail and its waits
49/// end with an error.
50pub struct MediaSource {
51    source: Mutex<broadcast::MediaSource>,
52    end: BroadcastEnd,
53    keyframe_requests: Arc<Notify>,
54    /// `None` for a source not published on demand.
55    demand_changes: Option<DemandChanges>,
56}
57
58impl MediaSource {
59    pub(super) fn new(
60        broadcast: &Broadcast,
61        options: MediaSourceOptions,
62        end: BroadcastEnd,
63    ) -> Self {
64        let config = MediaSourceConfig {
65            video: options.video,
66            audio: options.audio,
67            label: options.label,
68            video_bitrate: options.video_bitrate,
69            video_framerate: options.video_framerate,
70            cmaf: false,
71            on_demand: options.on_demand,
72        };
73        let source = broadcast::MediaSource::new(broadcast.clone(), config);
74        Self {
75            keyframe_requests: source.keyframe_requests(),
76            demand_changes: source.demand_changes(),
77            source: Mutex::new(source),
78            end,
79        }
80    }
81
82    /// `codec` is a WebCodecs codec string: `avc1.*` or `avc3.*` and
83    /// `hvc1.*` or `hev1.*` in Annex B, `vp8`, `vp09.*` or `av01.*`.
84    pub fn configure_video(&self, codec: &str) -> Result<(), MediaSourceError> {
85        self.with(|source| source.configure_video(codec))
86    }
87
88    /// One encoded frame, after `configure_video`. A keyframe opens a group
89    /// and sets the track's configuration (for H.264 and H.265, its
90    /// parameter sets). `timestamp_micros` must not run backwards.
91    pub fn push_video(
92        &self,
93        data: impl Into<Bytes>,
94        timestamp_micros: u64,
95    ) -> Result<(), MediaSourceError> {
96        self.with(|source| source.push_video(data.into(), timestamp_micros).map(drop))
97    }
98
99    /// `codec` is `opus`, or an AAC `mp4a.40.*` whose `description` is the
100    /// AudioSpecificConfig.
101    pub fn configure_audio(
102        &self,
103        codec: &str,
104        sample_rate: u32,
105        channels: u8,
106        description: Option<&[u8]>,
107    ) -> Result<(), MediaSourceError> {
108        self.with(|source| source.configure_audio(codec, sample_rate, channels, description))
109    }
110
111    /// One encoded audio frame, after `configure_audio`; `level` is written
112    /// as its LOC Audio Level property.
113    pub fn push_audio(
114        &self,
115        data: impl Into<Bytes>,
116        timestamp_micros: u64,
117        level: Option<AudioLevel>,
118    ) -> Result<(), MediaSourceError> {
119        self.with(|source| {
120            source
121                .push_audio(data.into(), timestamp_micros, level)
122                .map(drop)
123        })
124    }
125
126    /// Another encoding of the video in the track `<video>_<name>`: feed it
127    /// frames of the same pictures, with keyframes on the same pictures as
128    /// the video's. It joins the video's switching set
129    /// (draft-ietf-moq-cmsf-01 §3.2). `bitrate` (bits per second) and
130    /// `framerate` go into the catalog.
131    pub fn add_rendition(
132        &self,
133        name: &str,
134        label: impl Into<String>,
135        bitrate: Option<u64>,
136        framerate: Option<u32>,
137    ) -> Result<Rendition, MediaSourceError> {
138        let rendition =
139            self.with(|source| Ok(source.add_rendition(name, label.into(), bitrate, framerate)))?;
140        Ok(Rendition {
141            rendition,
142            end: self.end.clone(),
143        })
144    }
145
146    /// Ends the video (with its renditions) or the audio with PUBLISH_DONE
147    /// and sends the catalog without it. Stop pushing its media first;
148    /// configuring it again and pushing publishes it again. Fails when the
149    /// relay session has ended.
150    pub async fn remove_track(&self, kind: TrackKind) -> anyhow::Result<()> {
151        let removal = self.with(|source| Ok(source.remove_track(kind)))?;
152        removal.await
153    }
154
155    /// Resolves the next time a subscriber asks for a new group
156    /// (NEW_GROUP_REQUEST) or the broadcast needs one: encode the next frame
157    /// of the video and of every rendition as a keyframe then. It does not
158    /// end with the session: wait for `Session::closed` beside it.
159    pub async fn keyframe_request(&self) -> anyhow::Result<()> {
160        self.end.until(self.keyframe_requests.notified()).await
161    }
162
163    /// The next change of whether a subscriber takes one of the source's
164    /// tracks. A change replaces its track's unread one, so a late reader
165    /// gets each track's latest. Fails for a source added without `on_demand`.
166    pub async fn demand_change(&self) -> anyhow::Result<TrackDemand> {
167        let demand_changes = self
168            .demand_changes
169            .as_ref()
170            .ok_or_else(|| anyhow::anyhow!("the source was added without on_demand"))?;
171        self.end
172            .until(demand_changes.next())
173            .await?
174            .ok_or_else(|| anyhow::anyhow!("the media source stopped publishing"))
175    }
176
177    fn with<R>(
178        &self,
179        call: impl FnOnce(&mut broadcast::MediaSource) -> Result<R, MediaSourceError>,
180    ) -> Result<R, MediaSourceError> {
181        self.end.check()?;
182        call(&mut self.source.lock().unwrap())
183    }
184}
185
186/// Another encoding of a `MediaSource`'s video.
187pub struct Rendition {
188    rendition: MediaRendition,
189    end: BroadcastEnd,
190}
191
192impl Rendition {
193    /// As `MediaSource::configure_video`; it may differ from the video's
194    /// codec.
195    pub fn configure_video(&mut self, codec: &str) -> Result<(), MediaSourceError> {
196        self.end.check()?;
197        self.rendition.configure_video(codec)
198    }
199
200    /// An encoded frame of the same picture as a video frame, at that
201    /// frame's timestamp.
202    pub fn push_video(
203        &mut self,
204        data: impl Into<Bytes>,
205        timestamp_micros: u64,
206    ) -> Result<(), MediaSourceError> {
207        self.end.check()?;
208        self.rendition
209            .push_video(data.into(), timestamp_micros)
210            .map(drop)
211    }
212}