xrack-runtime 0.8.0

Runtime types, properties, models, and controls for generated XRack UI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
//! Host-independent media state shared by compiled and interpreted components.

use crate::{Node, Property, Value};
use std::{
    cell::{Cell, RefCell},
    collections::BTreeMap,
    path::{Path, PathBuf},
    rc::Rc,
    sync::Arc,
    time::{Duration, Instant},
};

/// One tightly packed, straight-alpha video image shared by scene snapshots.
#[derive(Clone)]
pub struct MediaFrame(Arc<([u32; 2], Vec<u8>)>);

impl MediaFrame {
    pub fn new(size: [u32; 2], rgba: Vec<u8>) -> Option<Self> {
        let [width, height] = size;
        let bytes = (width as usize)
            .checked_mul(height as usize)?
            .checked_mul(4)?;
        (width > 0 && height > 0 && bytes == rgba.len()).then(|| Self(Arc::new((size, rgba))))
    }
    pub fn size(&self) -> [u32; 2] {
        self.0.0
    }
    pub fn rgba(&self) -> &[u8] {
        &self.0.1
    }
}

impl PartialEq for MediaFrame {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl std::fmt::Debug for MediaFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MediaFrame")
            .field("size", &self.size())
            .finish_non_exhaustive()
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum MediaStatus {
    #[default]
    Empty,
    Loading,
    Ready,
    Seeking,
    Buffering,
    Paused,
    Playing,
    Stopped,
    Ended,
    Failed,
}
impl MediaStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Empty => "empty",
            Self::Loading => "loading",
            Self::Ready => "ready",
            Self::Seeking => "seeking",
            Self::Buffering => "buffering",
            Self::Paused => "paused",
            Self::Playing => "playing",
            Self::Stopped => "stopped",
            Self::Ended => "ended",
            Self::Failed => "failed",
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct MediaOptions {
    pub playback_rate: f64,
    pub network_bandwidth: u64,
    pub loops: i64,
    pub audio_track: i64,
    pub subtitle_track: i64,
    pub subtitle_file: Option<PathBuf>,
}

impl Default for MediaOptions {
    fn default() -> Self {
        Self {
            playback_rate: 1.0,
            network_bandwidth: 0,
            loops: 1,
            audio_track: -1,
            subtitle_track: -1,
            subtitle_file: None,
        }
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct MediaState {
    pub frame: Option<MediaFrame>,
    pub position: f64,
    pub duration: f64,
    pub playing: bool,
    pub ready: bool,
    pub ended: bool,
    pub error: String,
    pub status: MediaStatus,
    pub seekable: bool,
    pub live: bool,
    pub seek_start: f64,
    pub seek_end: f64,
    pub at_live_edge: bool,
    pub has_audio: bool,
    pub has_video: bool,
    pub audio_tracks: i64,
    pub subtitle_tracks: i64,
    pub title: String,
    pub subtitle: String,
    pub audio_error: String,
    pub subtitle_error: String,
    pub buffer_progress: f64,
    pub bitrate: i64,
    pub loops_remaining: i64,
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MediaCommand {
    Play,
    Pause,
    Stop,
    Seek(f64),
    GoLive,
}

pub trait MediaSession {
    fn command(&mut self, command: MediaCommand);
    fn set_volume(&mut self, volume: f64);
    fn configure(&mut self, _options: &MediaOptions) {}
    fn sample(&mut self, now: Instant) -> MediaState;
    fn active(&self) -> bool;

    fn poll_interval(&self) -> Duration {
        Duration::from_millis(15)
    }
}

type Factory = fn(&Path, bool, f64) -> Result<Box<dyn MediaSession>, String>;
type SoundFactory = fn(&Path, f64) -> Result<Box<dyn MediaSession>, String>;
thread_local! {
    static FACTORY: Cell<Option<Factory>> = const { Cell::new(None) };
    static SOUND_FACTORY: Cell<Option<SoundFactory>> = const { Cell::new(None) };
}

pub fn register_media_backend(factory: Factory) {
    FACTORY.set(Some(factory));
}

/// Register the preloaded sound effect backend for this UI thread.
#[doc(hidden)]
pub fn register_sound_backend(factory: SoundFactory) {
    SOUND_FACTORY.set(Some(factory));
}

/// Open a media session using the backend installed for this UI thread.
#[doc(hidden)]
pub fn open_media_session(
    path: &Path,
    autoplay: bool,
    volume: f64,
) -> Result<Box<dyn MediaSession>, String> {
    FACTORY
        .get()
        .ok_or_else(|| "Media playback requires a media-enabled host".to_owned())?(
        path, autoplay, volume,
    )
}

#[doc(hidden)]
#[derive(Default)]
pub struct MediaSlot {
    state: Property<MediaState>,
    commands: RefCell<Vec<MediaCommand>>,
}

impl MediaSlot {
    pub fn position(&self) -> f64 {
        self.state.get(MediaState::default).position
    }
    pub fn duration(&self) -> f64 {
        self.state.get(MediaState::default).duration
    }
    pub fn playing(&self) -> bool {
        self.state.get(MediaState::default).playing
    }
    pub fn ready(&self) -> bool {
        self.state.get(MediaState::default).ready
    }
    pub fn ended(&self) -> bool {
        self.state.get(MediaState::default).ended
    }
    pub fn error(&self) -> String {
        self.state.get(MediaState::default).error
    }

    pub fn status(&self) -> String {
        self.state.get(MediaState::default).status.as_str().into()
    }
    pub fn seekable(&self) -> bool {
        self.state.get(MediaState::default).seekable
    }
    pub fn live(&self) -> bool {
        self.state.get(MediaState::default).live
    }
    pub fn seek_start(&self) -> f64 {
        self.state.get(MediaState::default).seek_start
    }
    pub fn seek_end(&self) -> f64 {
        self.state.get(MediaState::default).seek_end
    }
    pub fn at_live_edge(&self) -> bool {
        self.state.get(MediaState::default).at_live_edge
    }
    pub fn has_audio(&self) -> bool {
        self.state.get(MediaState::default).has_audio
    }
    pub fn has_video(&self) -> bool {
        self.state.get(MediaState::default).has_video
    }
    pub fn audio_tracks(&self) -> i64 {
        self.state.get(MediaState::default).audio_tracks
    }
    pub fn subtitle_tracks(&self) -> i64 {
        self.state.get(MediaState::default).subtitle_tracks
    }
    pub fn media_title(&self) -> String {
        self.state.get(MediaState::default).title
    }
    pub fn subtitle(&self) -> String {
        self.state.get(MediaState::default).subtitle
    }
    pub fn audio_error(&self) -> String {
        self.state.get(MediaState::default).audio_error
    }

    pub fn subtitle_error(&self) -> String {
        self.state.get(MediaState::default).subtitle_error
    }
    pub fn buffer_progress(&self) -> f64 {
        self.state.get(MediaState::default).buffer_progress
    }

    pub fn bitrate(&self) -> i64 {
        self.state.get(MediaState::default).bitrate
    }

    pub fn loops_remaining(&self) -> i64 {
        self.state.get(MediaState::default).loops_remaining
    }

    pub fn read(&self, name: &str) -> Option<Value> {
        Some(match name {
            "position" => self.position().into(),
            "duration" => self.duration().into(),
            "playing" => self.playing().into(),
            "ready" => self.ready().into(),
            "ended" => self.ended().into(),
            "error" => self.error().into(),
            "status" => self.status().into(),
            "seekable" => self.seekable().into(),
            "live" => self.live().into(),
            "seek-start" => self.seek_start().into(),
            "seek-end" => self.seek_end().into(),
            "at-live-edge" => self.at_live_edge().into(),
            "has-audio" => self.has_audio().into(),
            "has-video" => self.has_video().into(),
            "audio-tracks" => self.audio_tracks().into(),
            "subtitle-tracks" => self.subtitle_tracks().into(),
            "media-title" => self.media_title().into(),
            "subtitle" => self.subtitle().into(),
            "audio-error" => self.audio_error().into(),
            "subtitle-error" => self.subtitle_error().into(),
            "buffer-progress" => self.buffer_progress().into(),
            "bitrate" => self.bitrate().into(),
            "loops-remaining" => self.loops_remaining().into(),
            _ => return None,
        })
    }

    pub fn command(&self, command: MediaCommand) {
        self.commands.borrow_mut().push(command);
    }

    pub fn populate(self: &Rc<Self>, node: &mut Node, file: &str) {
        let state = self.state.get(MediaState::default);
        node.media_frame = state.frame;
        let source = node.text("source").unwrap_or_default();
        let path = if source.starts_with("https://") || source.starts_with("http://") {
            Some(PathBuf::from(source))
        } else {
            local_source(file, source)
        };
        let rate = node.number("playback-rate").unwrap_or(1.0);
        let volume = node.number("volume").unwrap_or(1.0);
        node.media = Some(MediaRegistration {
            slot: self.clone(),
            path,
            sound_effect: false,
            autoplay: node.boolean("autoplay", false),
            options: MediaOptions {
                playback_rate: if rate.is_finite() {
                    rate.clamp(0.25, 4.0)
                } else {
                    1.0
                },
                network_bandwidth: node
                    .int("network-bandwidth")
                    .unwrap_or(0)
                    .clamp(0, 1_000_000_000) as u64,
                loops: node.int("loops").unwrap_or(1).max(-1),
                audio_track: node.int("audio-track").unwrap_or(-1),
                subtitle_track: node.int("subtitle-track").unwrap_or(-1),
                subtitle_file: node
                    .text("subtitle-file")
                    .and_then(|source| local_source(file, source)),
            },
            volume: effective_volume(volume, node.boolean("muted", false)),
        });
    }

    pub fn sound_effect(
        self: &Rc<Self>,
        file: &str,
        source: &str,
        volume: f64,
        muted: bool,
    ) -> MediaRegistration {
        MediaRegistration {
            slot: self.clone(),
            path: local_source(file, source),
            sound_effect: true,
            autoplay: false,
            volume: effective_volume(volume, muted),
            options: MediaOptions::default(),
        }
    }
}

fn effective_volume(volume: f64, muted: bool) -> f64 {
    if muted || !volume.is_finite() {
        0.0
    } else {
        volume.clamp(0.0, 1.0)
    }
}

fn local_source(file: &str, source: &str) -> Option<PathBuf> {
    if source.is_empty() {
        return None;
    }
    let path = Path::new(source);
    Some(Path::new(file).parent().unwrap_or(Path::new("")).join(path))
}

#[derive(Clone)]
#[doc(hidden)]
pub struct MediaRegistration {
    slot: Rc<MediaSlot>,
    path: Option<PathBuf>,
    sound_effect: bool,
    autoplay: bool,
    volume: f64,
    options: MediaOptions,
}

impl MediaRegistration {
    pub fn with_sound_loops(mut self, loops: i64) -> Self {
        self.options.loops = if loops == -1 { -1 } else { loops.max(1) };
        self
    }

    fn open(&self, path: &Path) -> Result<Box<dyn MediaSession>, String> {
        if self.sound_effect {
            let open = SOUND_FACTORY
                .get()
                .ok_or_else(|| "SoundEffect requires a media-enabled host".to_owned())?;
            open(path, self.volume)
        } else {
            open_media_session(path, self.autoplay, self.volume)
        }
    }

    pub fn key(&self) -> usize {
        Rc::as_ptr(&self.slot) as usize
    }
}

impl PartialEq for MediaRegistration {
    fn eq(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.slot, &other.slot)
            && self.path == other.path
            && self.sound_effect == other.sound_effect
            && self.autoplay == other.autoplay
            && self.volume == other.volume
            && self.options == other.options
    }
}

impl std::fmt::Debug for MediaRegistration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MediaRegistration")
            .field("path", &self.path)
            .finish_non_exhaustive()
    }
}

struct Entry {
    registration: MediaRegistration,
    session: Option<Box<dyn MediaSession>>,
}

#[doc(hidden)]
#[derive(Default)]
pub struct MediaQueue {
    entries: BTreeMap<usize, Entry>,
    changed: bool,
}

impl MediaQueue {
    pub fn sync<'a>(
        &mut self,
        registrations: impl IntoIterator<Item = &'a MediaRegistration>,
    ) -> bool {
        let mut changed = false;
        let mut old = std::mem::take(&mut self.entries);
        for registration in registrations {
            let key = Rc::as_ptr(&registration.slot) as usize;
            let previous = old.remove(&key);
            let mut entry = match previous {
                Some(entry)
                    if entry.registration.path == registration.path
                        && entry.registration.sound_effect == registration.sound_effect =>
                {
                    entry
                }
                _ => {
                    // Keep commands from the current UI turn for the newly bound source.
                    // A play already delivered to the previous session ends with that session.
                    let mut state = MediaState::default();
                    let session =
                        registration
                            .path
                            .as_ref()
                            .and_then(|path| match registration.open(path) {
                                Ok(mut session) => {
                                    state.status = MediaStatus::Loading;
                                    session.configure(&registration.options);
                                    Some(session)
                                }
                                Err(error) => {
                                    state.error = error;
                                    state.status = MediaStatus::Failed;
                                    None
                                }
                            });
                    changed |= publish(&registration.slot, state);
                    Entry {
                        registration: registration.clone(),
                        session,
                    }
                }
            };
            if entry.registration.volume != registration.volume
                && let Some(session) = &mut entry.session
            {
                session.set_volume(registration.volume);
            }
            if entry.registration.options != registration.options
                && let Some(session) = &mut entry.session
            {
                session.configure(&registration.options);
            }
            entry.registration = registration.clone();
            self.entries.insert(key, entry);
        }
        for (_, entry) in old {
            entry.registration.slot.commands.take();
            changed |= publish(&entry.registration.slot, MediaState::default());
        }
        self.changed |= changed;
        changed
    }

    pub fn advance(&mut self, now: Instant) -> bool {
        let mut changed = std::mem::take(&mut self.changed);
        for entry in self.entries.values_mut() {
            let commands = entry.registration.slot.commands.take();
            if let Some(session) = &mut entry.session {
                for command in commands {
                    session.command(command);
                }
                changed |= publish(&entry.registration.slot, session.sample(now));
            }
        }
        changed
    }

    pub fn deadline(&self, now: Instant) -> Option<Instant> {
        if self.changed {
            return Some(now);
        }
        self.entries
            .values()
            .filter_map(|entry| {
                if !entry.registration.slot.commands.borrow().is_empty() {
                    Some(now)
                } else {
                    entry
                        .session
                        .as_ref()
                        .filter(|session| session.active())
                        .map(|session| now + session.poll_interval())
                }
            })
            .min()
    }
}

fn publish(slot: &MediaSlot, state: MediaState) -> bool {
    if slot.state.get(MediaState::default) == state {
        return false;
    }
    slot.state.set(state);
    true
}