1use crate::{Node, Property, Value};
4use std::{
5 cell::{Cell, RefCell},
6 collections::BTreeMap,
7 path::{Path, PathBuf},
8 rc::Rc,
9 sync::Arc,
10 time::{Duration, Instant},
11};
12
13#[derive(Clone)]
15pub struct MediaFrame(Arc<([u32; 2], Vec<u8>)>);
16
17impl MediaFrame {
18 pub fn new(size: [u32; 2], rgba: Vec<u8>) -> Option<Self> {
19 let [width, height] = size;
20 let bytes = (width as usize)
21 .checked_mul(height as usize)?
22 .checked_mul(4)?;
23 (width > 0 && height > 0 && bytes == rgba.len()).then(|| Self(Arc::new((size, rgba))))
24 }
25 pub fn size(&self) -> [u32; 2] {
26 self.0.0
27 }
28 pub fn rgba(&self) -> &[u8] {
29 &self.0.1
30 }
31}
32
33impl PartialEq for MediaFrame {
34 fn eq(&self, other: &Self) -> bool {
35 Arc::ptr_eq(&self.0, &other.0)
36 }
37}
38
39impl std::fmt::Debug for MediaFrame {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 f.debug_struct("MediaFrame")
42 .field("size", &self.size())
43 .finish_non_exhaustive()
44 }
45}
46
47#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
48pub enum MediaStatus {
49 #[default]
50 Empty,
51 Loading,
52 Seeking,
53 Buffering,
54 Paused,
55 Playing,
56 Stopped,
57 Ended,
58 Failed,
59}
60impl MediaStatus {
61 pub fn as_str(self) -> &'static str {
62 match self {
63 Self::Empty => "empty",
64 Self::Loading => "loading",
65 Self::Seeking => "seeking",
66 Self::Buffering => "buffering",
67 Self::Paused => "paused",
68 Self::Playing => "playing",
69 Self::Stopped => "stopped",
70 Self::Ended => "ended",
71 Self::Failed => "failed",
72 }
73 }
74}
75
76#[derive(Clone, Debug, PartialEq)]
77pub struct MediaOptions {
78 pub playback_rate: f64,
79 pub network_bandwidth: u64,
80 pub loops: i64,
81 pub audio_track: i64,
82 pub subtitle_track: i64,
83 pub subtitle_file: Option<PathBuf>,
84}
85
86impl Default for MediaOptions {
87 fn default() -> Self {
88 Self {
89 playback_rate: 1.0,
90 network_bandwidth: 0,
91 loops: 1,
92 audio_track: -1,
93 subtitle_track: -1,
94 subtitle_file: None,
95 }
96 }
97}
98
99#[derive(Clone, Debug, Default, PartialEq)]
100pub struct MediaState {
101 pub frame: Option<MediaFrame>,
102 pub position: f64,
103 pub duration: f64,
104 pub playing: bool,
105 pub ready: bool,
106 pub ended: bool,
107 pub error: String,
108 pub status: MediaStatus,
109 pub seekable: bool,
110 pub live: bool,
111 pub seek_start: f64,
112 pub seek_end: f64,
113 pub at_live_edge: bool,
114 pub has_audio: bool,
115 pub has_video: bool,
116 pub audio_tracks: i64,
117 pub subtitle_tracks: i64,
118 pub title: String,
119 pub subtitle: String,
120 pub audio_error: String,
121 pub subtitle_error: String,
122 pub buffer_progress: f64,
123 pub bitrate: i64,
124}
125
126#[derive(Clone, Copy, Debug, PartialEq)]
127pub enum MediaCommand {
128 Play,
129 Pause,
130 Stop,
131 Seek(f64),
132 GoLive,
133}
134
135pub trait MediaSession {
136 fn command(&mut self, command: MediaCommand);
137 fn set_volume(&mut self, volume: f64);
138 fn configure(&mut self, _options: &MediaOptions) {}
139 fn sample(&mut self, now: Instant) -> MediaState;
140 fn active(&self) -> bool;
141
142 fn poll_interval(&self) -> Duration {
143 Duration::from_millis(15)
144 }
145}
146
147type Factory = fn(&Path, bool, f64) -> Result<Box<dyn MediaSession>, String>;
148thread_local! {
149 static FACTORY: Cell<Option<Factory>> = const { Cell::new(None) };
150}
151
152pub fn register_media_backend(factory: Factory) {
153 FACTORY.set(Some(factory));
154}
155
156#[doc(hidden)]
158pub fn open_media_session(
159 path: &Path,
160 autoplay: bool,
161 volume: f64,
162) -> Result<Box<dyn MediaSession>, String> {
163 FACTORY
164 .get()
165 .ok_or_else(|| "Media playback requires a media-enabled host".to_owned())?(
166 path, autoplay, volume,
167 )
168}
169
170#[doc(hidden)]
171#[derive(Default)]
172pub struct MediaSlot {
173 state: Property<MediaState>,
174 commands: RefCell<Vec<MediaCommand>>,
175}
176
177impl MediaSlot {
178 pub fn position(&self) -> f64 {
179 self.state.get(MediaState::default).position
180 }
181 pub fn duration(&self) -> f64 {
182 self.state.get(MediaState::default).duration
183 }
184 pub fn playing(&self) -> bool {
185 self.state.get(MediaState::default).playing
186 }
187 pub fn ready(&self) -> bool {
188 self.state.get(MediaState::default).ready
189 }
190 pub fn ended(&self) -> bool {
191 self.state.get(MediaState::default).ended
192 }
193 pub fn error(&self) -> String {
194 self.state.get(MediaState::default).error
195 }
196
197 pub fn status(&self) -> String {
198 self.state.get(MediaState::default).status.as_str().into()
199 }
200 pub fn seekable(&self) -> bool {
201 self.state.get(MediaState::default).seekable
202 }
203 pub fn live(&self) -> bool {
204 self.state.get(MediaState::default).live
205 }
206 pub fn seek_start(&self) -> f64 {
207 self.state.get(MediaState::default).seek_start
208 }
209 pub fn seek_end(&self) -> f64 {
210 self.state.get(MediaState::default).seek_end
211 }
212 pub fn at_live_edge(&self) -> bool {
213 self.state.get(MediaState::default).at_live_edge
214 }
215 pub fn has_audio(&self) -> bool {
216 self.state.get(MediaState::default).has_audio
217 }
218 pub fn has_video(&self) -> bool {
219 self.state.get(MediaState::default).has_video
220 }
221 pub fn audio_tracks(&self) -> i64 {
222 self.state.get(MediaState::default).audio_tracks
223 }
224 pub fn subtitle_tracks(&self) -> i64 {
225 self.state.get(MediaState::default).subtitle_tracks
226 }
227 pub fn media_title(&self) -> String {
228 self.state.get(MediaState::default).title
229 }
230 pub fn subtitle(&self) -> String {
231 self.state.get(MediaState::default).subtitle
232 }
233 pub fn audio_error(&self) -> String {
234 self.state.get(MediaState::default).audio_error
235 }
236
237 pub fn subtitle_error(&self) -> String {
238 self.state.get(MediaState::default).subtitle_error
239 }
240 pub fn buffer_progress(&self) -> f64 {
241 self.state.get(MediaState::default).buffer_progress
242 }
243
244 pub fn bitrate(&self) -> i64 {
245 self.state.get(MediaState::default).bitrate
246 }
247
248 pub fn read(&self, name: &str) -> Option<Value> {
249 Some(match name {
250 "position" => self.position().into(),
251 "duration" => self.duration().into(),
252 "playing" => self.playing().into(),
253 "ready" => self.ready().into(),
254 "ended" => self.ended().into(),
255 "error" => self.error().into(),
256 "status" => self.status().into(),
257 "seekable" => self.seekable().into(),
258 "live" => self.live().into(),
259 "seek-start" => self.seek_start().into(),
260 "seek-end" => self.seek_end().into(),
261 "at-live-edge" => self.at_live_edge().into(),
262 "has-audio" => self.has_audio().into(),
263 "has-video" => self.has_video().into(),
264 "audio-tracks" => self.audio_tracks().into(),
265 "subtitle-tracks" => self.subtitle_tracks().into(),
266 "media-title" => self.media_title().into(),
267 "subtitle" => self.subtitle().into(),
268 "audio-error" => self.audio_error().into(),
269 "subtitle-error" => self.subtitle_error().into(),
270 "buffer-progress" => self.buffer_progress().into(),
271 "bitrate" => self.bitrate().into(),
272 _ => return None,
273 })
274 }
275
276 pub fn command(&self, command: MediaCommand) {
277 self.commands.borrow_mut().push(command);
278 }
279
280 pub fn populate(self: &Rc<Self>, node: &mut Node, file: &str) {
281 let state = self.state.get(MediaState::default);
282 node.media_frame = state.frame;
283 let source = node.text("source").unwrap_or_default();
284 let path = if source.starts_with("https://") || source.starts_with("http://") {
285 Some(PathBuf::from(source))
286 } else {
287 local_source(file, source)
288 };
289 let rate = node.number("playback-rate").unwrap_or(1.0);
290 let volume = node.number("volume").unwrap_or(1.0);
291 node.media = Some(MediaRegistration {
292 slot: self.clone(),
293 path,
294 autoplay: node.boolean("autoplay", false),
295 options: MediaOptions {
296 playback_rate: if rate.is_finite() {
297 rate.clamp(0.25, 4.0)
298 } else {
299 1.0
300 },
301 network_bandwidth: node
302 .int("network-bandwidth")
303 .unwrap_or(0)
304 .clamp(0, 1_000_000_000) as u64,
305 loops: node.int("loops").unwrap_or(1).max(-1),
306 audio_track: node.int("audio-track").unwrap_or(-1),
307 subtitle_track: node.int("subtitle-track").unwrap_or(-1),
308 subtitle_file: node
309 .text("subtitle-file")
310 .and_then(|source| local_source(file, source)),
311 },
312 volume: if node.boolean("muted", false) {
313 0.0
314 } else if volume.is_finite() {
315 volume.clamp(0.0, 1.0)
316 } else {
317 0.0
318 },
319 });
320 }
321}
322
323fn local_source(file: &str, source: &str) -> Option<PathBuf> {
324 if source.is_empty() {
325 return None;
326 }
327 let path = Path::new(source);
328 Some(Path::new(file).parent().unwrap_or(Path::new("")).join(path))
329}
330
331#[derive(Clone)]
332#[doc(hidden)]
333pub struct MediaRegistration {
334 slot: Rc<MediaSlot>,
335 path: Option<PathBuf>,
336 autoplay: bool,
337 volume: f64,
338 options: MediaOptions,
339}
340
341impl MediaRegistration {
342 pub fn key(&self) -> usize {
343 Rc::as_ptr(&self.slot) as usize
344 }
345}
346
347impl PartialEq for MediaRegistration {
348 fn eq(&self, other: &Self) -> bool {
349 Rc::ptr_eq(&self.slot, &other.slot)
350 && self.path == other.path
351 && self.autoplay == other.autoplay
352 && self.volume == other.volume
353 && self.options == other.options
354 }
355}
356
357impl std::fmt::Debug for MediaRegistration {
358 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
359 f.debug_struct("MediaRegistration")
360 .field("path", &self.path)
361 .finish_non_exhaustive()
362 }
363}
364
365struct Entry {
366 registration: MediaRegistration,
367 session: Option<Box<dyn MediaSession>>,
368}
369
370#[doc(hidden)]
371#[derive(Default)]
372pub struct MediaQueue {
373 entries: BTreeMap<usize, Entry>,
374 changed: bool,
375}
376
377impl MediaQueue {
378 pub fn sync<'a>(
379 &mut self,
380 registrations: impl IntoIterator<Item = &'a MediaRegistration>,
381 ) -> bool {
382 let mut changed = false;
383 let mut old = std::mem::take(&mut self.entries);
384 for registration in registrations {
385 let key = Rc::as_ptr(®istration.slot) as usize;
386 let previous = old.remove(&key);
387 let mut entry = match previous {
388 Some(entry) if entry.registration.path == registration.path => entry,
389 _ => {
390 let mut state = MediaState::default();
391 let session = registration.path.as_ref().and_then(|path| {
392 let result =
393 open_media_session(path, registration.autoplay, registration.volume);
394 match result {
395 Ok(mut session) => {
396 state.status = MediaStatus::Loading;
397 session.configure(®istration.options);
398 Some(session)
399 }
400 Err(error) => {
401 state.error = error;
402 state.status = MediaStatus::Failed;
403 None
404 }
405 }
406 });
407 changed |= publish(®istration.slot, state);
408 Entry {
409 registration: registration.clone(),
410 session,
411 }
412 }
413 };
414 if entry.registration.volume != registration.volume
415 && let Some(session) = &mut entry.session
416 {
417 session.set_volume(registration.volume);
418 }
419 if entry.registration.options != registration.options
420 && let Some(session) = &mut entry.session
421 {
422 session.configure(®istration.options);
423 }
424 entry.registration = registration.clone();
425 self.entries.insert(key, entry);
426 }
427 for (_, entry) in old {
428 entry.registration.slot.commands.take();
429 changed |= publish(&entry.registration.slot, MediaState::default());
430 }
431 self.changed |= changed;
432 changed
433 }
434
435 pub fn advance(&mut self, now: Instant) -> bool {
436 let mut changed = std::mem::take(&mut self.changed);
437 for entry in self.entries.values_mut() {
438 let commands = entry.registration.slot.commands.take();
439 if let Some(session) = &mut entry.session {
440 for command in commands {
441 session.command(command);
442 }
443 changed |= publish(&entry.registration.slot, session.sample(now));
444 }
445 }
446 changed
447 }
448
449 pub fn deadline(&self, now: Instant) -> Option<Instant> {
450 if self.changed {
451 return Some(now);
452 }
453 self.entries
454 .values()
455 .filter_map(|entry| {
456 if !entry.registration.slot.commands.borrow().is_empty() {
457 Some(now)
458 } else {
459 entry
460 .session
461 .as_ref()
462 .filter(|session| session.active())
463 .map(|session| now + session.poll_interval())
464 }
465 })
466 .min()
467 }
468}
469
470fn publish(slot: &MediaSlot, state: MediaState) -> bool {
471 if slot.state.get(MediaState::default) == state {
472 return false;
473 }
474 slot.state.set(state);
475 true
476}