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 pub video: String,
18 pub audio: String,
20 pub label: String,
23 pub video_bitrate: Option<u64>,
25 pub video_framerate: Option<u32>,
27 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
46pub struct MediaSource {
51 source: Mutex<broadcast::MediaSource>,
52 end: BroadcastEnd,
53 keyframe_requests: Arc<Notify>,
54 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 pub fn configure_video(&self, codec: &str) -> Result<(), MediaSourceError> {
85 self.with(|source| source.configure_video(codec))
86 }
87
88 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 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 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 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 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 pub async fn keyframe_request(&self) -> anyhow::Result<()> {
160 self.end.until(self.keyframe_requests.notified()).await
161 }
162
163 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
186pub struct Rendition {
188 rendition: MediaRendition,
189 end: BroadcastEnd,
190}
191
192impl Rendition {
193 pub fn configure_video(&mut self, codec: &str) -> Result<(), MediaSourceError> {
196 self.end.check()?;
197 self.rendition.configure_video(codec)
198 }
199
200 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}