media-pp 0.1.6

A small, GStreamer-flavored media pipeline library built on FFmpeg.
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
use std::{
    sync::{Arc, Mutex},
    time::Duration,
};

use thiserror::Error as ThisError;

use crate::clock::Clock;

/// Which source currently defines the pipeline's media position.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlaybackMaster {
    /// No timestamped stream has established a position yet.
    Unavailable,
    /// Media position advances from the pipeline's pause-aware wall clock.
    Wall,
    /// An audio renderer owns the clock but has not started the endpoint yet.
    AudioPriming,
    /// An audio endpoint's played-sample position is the master clock.
    Audio,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ThisError)]
pub enum PlaybackClockError {
    #[error("this pipeline already has an audio playback-clock master")]
    AudioMasterAlreadyRegistered,

    #[error("the audio playback-clock registration is stale")]
    StaleAudioMaster,
}

/// Pipeline-wide media clock shared by audio output and video scheduling.
///
/// [`Clock`] remains the pipeline's monotonic control/pause clock. This
/// type adds the media-timeline position and can hand that position from a
/// wall-clock fallback to one audio renderer without letting the position
/// jump backwards. It deliberately contains no WASAPI types: an audio
/// backend publishes device-position snapshots through its private
/// registration, while video scheduling only reads the resulting position.
pub struct PlaybackClock {
    wall_clock: Arc<Clock>,
    state: Mutex<State>,
}

#[derive(Clone, Copy)]
enum State {
    Unavailable {
        next_registration: u64,
    },
    Wall {
        anchor_ns: i64,
        anchor_elapsed: Duration,
        next_registration: u64,
    },
    AudioPriming {
        registration: u64,
        held_ns: Option<i64>,
        next_registration: u64,
    },
    // Only an audio renderer moves the clock into these two, and the only one
    // in this crate is behind `wasapi-renderer`. They are dead in a build
    // without it, but they are the timeline contract `PlaybackClock` exists to
    // provide — gating them on a backend feature would invert that. See
    // `AudioMasterRegistration`.
    #[allow(dead_code)]
    Audio {
        registration: u64,
        position_ns: i64,
        sampled_elapsed: Duration,
        submitted_until_ns: i64,
        running: bool,
        next_registration: u64,
    },
    #[allow(dead_code)]
    AudioFallback {
        registration: u64,
        anchor_ns: i64,
        anchor_elapsed: Duration,
        next_registration: u64,
    },
}

impl PlaybackClock {
    pub(crate) fn new(wall_clock: Arc<Clock>) -> Self {
        Self {
            wall_clock,
            state: Mutex::new(State::Unavailable {
                next_registration: 1,
            }),
        }
    }

    pub fn master(&self) -> PlaybackMaster {
        match *self.state.lock().unwrap() {
            State::Unavailable { .. } => PlaybackMaster::Unavailable,
            State::Wall { .. } | State::AudioFallback { .. } => PlaybackMaster::Wall,
            State::AudioPriming { .. } => PlaybackMaster::AudioPriming,
            State::Audio { .. } => PlaybackMaster::Audio,
        }
    }

    #[cfg(test)]
    pub(crate) fn position_ns(&self) -> Option<i64> {
        let state = self.state.lock().unwrap();
        position_at(*state, self.wall_clock.elapsed())
    }

    pub(crate) fn interrupt_epoch(&self) -> u64 {
        self.wall_clock.interrupt_epoch()
    }

    /// Establishes a wall-clock media origin if no stream owns one yet.
    /// Returns the current position after doing so.
    #[cfg(test)]
    pub(crate) fn ensure_wall_origin(&self, media_ns: i64) -> Option<i64> {
        let mut state = self.state.lock().unwrap();
        if let State::Unavailable { next_registration } = *state {
            self.wall_clock.start();
            let elapsed = self.wall_clock.elapsed();
            *state = State::Wall {
                anchor_ns: media_ns,
                anchor_elapsed: elapsed,
                next_registration,
            };
        }
        position_at(*state, self.wall_clock.elapsed())
    }

    pub(crate) fn video_snapshot(&self, media_ns: i64) -> (PlaybackMaster, Option<i64>) {
        let mut state = self.state.lock().unwrap();
        if let State::Unavailable { next_registration } = *state {
            self.wall_clock.start();
            let elapsed = self.wall_clock.elapsed();
            *state = State::Wall {
                anchor_ns: media_ns,
                anchor_elapsed: elapsed,
                next_registration,
            };
        }
        let elapsed = self.wall_clock.elapsed();
        let master = match *state {
            State::Unavailable { .. } => PlaybackMaster::Unavailable,
            State::Wall { .. } | State::AudioFallback { .. } => PlaybackMaster::Wall,
            State::AudioPriming { .. } => PlaybackMaster::AudioPriming,
            State::Audio { .. } => PlaybackMaster::Audio,
        };
        (master, position_at(*state, elapsed))
    }

    /// Claims the timeline for one audio renderer. Unused in a build without
    /// an audio renderer (see `AudioMasterRegistration`), hence the `allow`.
    #[allow(dead_code)]
    pub(crate) fn register_audio_master(
        self: &Arc<Self>,
    ) -> Result<AudioMasterRegistration, PlaybackClockError> {
        let mut state = self.state.lock().unwrap();
        let elapsed = self.wall_clock.elapsed();
        let (held_ns, registration, next_registration) = match *state {
            State::Unavailable { next_registration } => {
                (None, next_registration, next_registration.wrapping_add(1))
            }
            State::Wall {
                next_registration, ..
            } => (
                position_at(*state, elapsed),
                next_registration,
                next_registration.wrapping_add(1),
            ),
            State::AudioPriming { .. } | State::Audio { .. } | State::AudioFallback { .. } => {
                return Err(PlaybackClockError::AudioMasterAlreadyRegistered);
            }
        };
        *state = State::AudioPriming {
            registration,
            held_ns,
            next_registration,
        };
        Ok(AudioMasterRegistration {
            clock: self.clone(),
            registration,
        })
    }

    /// Resets media state for a seek while retaining the current audio
    /// renderer's ownership. The next timestamp/device sample establishes
    /// the post-seek position.
    pub(crate) fn reset_for_seek(&self) {
        let mut state = self.state.lock().unwrap();
        *state = match *state {
            State::Unavailable { next_registration }
            | State::Wall {
                next_registration, ..
            } => State::Unavailable { next_registration },
            State::AudioPriming {
                registration,
                next_registration,
                ..
            }
            | State::Audio {
                registration,
                next_registration,
                ..
            }
            | State::AudioFallback {
                registration,
                next_registration,
                ..
            } => State::AudioPriming {
                registration,
                held_ns: None,
                next_registration,
            },
        };
    }

    #[allow(dead_code)]
    fn release_audio_master(&self, registration: u64) {
        let mut state = self.state.lock().unwrap();
        let elapsed = self.wall_clock.elapsed();
        let (matches, next_registration) = match *state {
            State::AudioPriming {
                registration: current,
                next_registration,
                ..
            }
            | State::Audio {
                registration: current,
                next_registration,
                ..
            }
            | State::AudioFallback {
                registration: current,
                next_registration,
                ..
            } => (current == registration, next_registration),
            State::Unavailable { .. } | State::Wall { .. } => return,
        };
        if !matches {
            return;
        }
        *state = match position_at(*state, elapsed) {
            Some(anchor_ns) => State::Wall {
                anchor_ns,
                anchor_elapsed: elapsed,
                next_registration,
            },
            None => State::Unavailable { next_registration },
        };
    }
}

/// Exclusive, generation-checked writer owned by one audio renderer.
/// Dropping it hands the last known position back to the wall clock.
///
/// The only audio renderer in this crate is `WasapiRenderer`, behind the
/// `wasapi-renderer` feature, so a build without it constructs this nowhere and
/// every method below is dead. That is why the `allow`s here are deliberate
/// rather than a `cfg(feature = "wasapi-renderer")` gate: `PlaybackClock` is
/// the backend-independent timeline every renderer binds to, and teaching it
/// about one backend's Cargo feature would invert that relationship. The
/// crate's own tests exercise this path, so it is covered even when no shipped
/// element uses it.
#[allow(dead_code)]
pub(crate) struct AudioMasterRegistration {
    clock: Arc<PlaybackClock>,
    registration: u64,
}

#[allow(dead_code)]
impl AudioMasterRegistration {
    pub(crate) fn priming_target_ns(&self) -> Result<Option<i64>, PlaybackClockError> {
        match *self.clock.state.lock().unwrap() {
            State::AudioPriming {
                registration,
                held_ns,
                ..
            } if registration == self.registration => Ok(held_ns),
            State::Audio { registration, .. } if registration == self.registration => Ok(None),
            State::AudioFallback { registration, .. } if registration == self.registration => {
                Ok(None)
            }
            _ => Err(PlaybackClockError::StaleAudioMaster),
        }
    }

    pub(crate) fn publish(
        &self,
        position_ns: i64,
        submitted_until_ns: i64,
        running: bool,
    ) -> Result<(), PlaybackClockError> {
        let mut state = self.clock.state.lock().unwrap();
        self.clock.wall_clock.start();
        let elapsed = self.clock.wall_clock.elapsed();
        let (held_ns, next_registration) = match *state {
            State::AudioPriming {
                registration,
                held_ns,
                next_registration,
            } if registration == self.registration => (held_ns, next_registration),
            State::Audio {
                registration,
                next_registration,
                ..
            } if registration == self.registration => (None, next_registration),
            State::AudioFallback {
                registration,
                next_registration,
                ..
            } if registration == self.registration => (None, next_registration),
            _ => return Err(PlaybackClockError::StaleAudioMaster),
        };

        // A master handoff must never make video scheduling move backwards.
        let position_ns = held_ns.map_or(position_ns, |held| position_ns.max(held));
        let submitted_until_ns = submitted_until_ns.max(position_ns);
        *state = State::Audio {
            registration: self.registration,
            position_ns,
            sampled_elapsed: elapsed,
            submitted_until_ns,
            running,
            next_registration,
        };
        Ok(())
    }

    /// Audio ended before another stream: continue from its final played
    /// position using the wall clock while retaining this registration so
    /// a second renderer cannot race the still-attached one.
    pub(crate) fn finish(&self, position_ns: i64) -> Result<(), PlaybackClockError> {
        let mut state = self.clock.state.lock().unwrap();
        let elapsed = self.clock.wall_clock.elapsed();
        let next_registration = match *state {
            State::AudioPriming {
                registration,
                next_registration,
                ..
            }
            | State::Audio {
                registration,
                next_registration,
                ..
            } if registration == self.registration => next_registration,
            _ => return Err(PlaybackClockError::StaleAudioMaster),
        };
        *state = State::AudioFallback {
            registration: self.registration,
            anchor_ns: position_ns,
            anchor_elapsed: elapsed,
            next_registration,
        };
        Ok(())
    }

    pub(crate) fn reset_for_seek(&self) -> Result<(), PlaybackClockError> {
        let mut state = self.clock.state.lock().unwrap();
        let next_registration = match *state {
            State::AudioPriming {
                registration,
                next_registration,
                ..
            }
            | State::Audio {
                registration,
                next_registration,
                ..
            }
            | State::AudioFallback {
                registration,
                next_registration,
                ..
            } if registration == self.registration => next_registration,
            _ => return Err(PlaybackClockError::StaleAudioMaster),
        };
        *state = State::AudioPriming {
            registration: self.registration,
            held_ns: None,
            next_registration,
        };
        Ok(())
    }
}

impl Drop for AudioMasterRegistration {
    fn drop(&mut self) {
        self.clock.release_audio_master(self.registration);
    }
}

fn position_at(state: State, elapsed: Duration) -> Option<i64> {
    match state {
        State::Unavailable { .. } => None,
        State::Wall {
            anchor_ns,
            anchor_elapsed,
            ..
        } => Some(add_duration(
            anchor_ns,
            elapsed.saturating_sub(anchor_elapsed),
        )),
        State::AudioPriming { held_ns, .. } => held_ns,
        State::Audio {
            position_ns,
            sampled_elapsed,
            submitted_until_ns,
            running,
            ..
        } => {
            let projected = if running {
                add_duration(position_ns, elapsed.saturating_sub(sampled_elapsed))
            } else {
                position_ns
            };
            Some(projected.min(submitted_until_ns))
        }
        State::AudioFallback {
            anchor_ns,
            anchor_elapsed,
            ..
        } => Some(add_duration(
            anchor_ns,
            elapsed.saturating_sub(anchor_elapsed),
        )),
    }
}

fn add_duration(value_ns: i64, duration: Duration) -> i64 {
    let delta = duration.as_nanos().min(i64::MAX as u128) as i64;
    value_ns.saturating_add(delta)
}

#[cfg(test)]
mod tests {
    use std::{thread, time::Duration};

    use super::*;

    #[test]
    fn wall_origin_advances_and_freezes_with_pipeline_clock() {
        let wall = Arc::new(Clock::new());
        let playback = PlaybackClock::new(wall.clone());
        assert!(playback.ensure_wall_origin(1_000).unwrap() >= 1_000);
        thread::sleep(Duration::from_millis(20));
        assert!(playback.position_ns().unwrap() >= 10_000_000);

        wall.pause();
        let paused = playback.position_ns().unwrap();
        thread::sleep(Duration::from_millis(20));
        assert_eq!(playback.position_ns(), Some(paused));
    }

    #[test]
    fn audio_handoff_never_moves_backwards_and_release_continues_on_wall() {
        let wall = Arc::new(Clock::new());
        let playback = Arc::new(PlaybackClock::new(wall));
        playback.ensure_wall_origin(50_000_000);
        let audio = playback.register_audio_master().unwrap();
        let held = audio.priming_target_ns().unwrap().unwrap();

        audio
            .publish(held - 10_000_000, held + 100_000_000, true)
            .unwrap();
        assert!(playback.position_ns().unwrap() >= held);
        drop(audio);
        let released = playback.position_ns().unwrap();
        thread::sleep(Duration::from_millis(10));
        assert!(playback.position_ns().unwrap() >= released);
        assert_eq!(playback.master(), PlaybackMaster::Wall);
    }

    #[test]
    fn only_one_audio_master_can_publish_and_seek_retains_its_generation() {
        let wall = Arc::new(Clock::new());
        let playback = Arc::new(PlaybackClock::new(wall));
        let audio = playback.register_audio_master().unwrap();
        assert!(matches!(
            playback.register_audio_master(),
            Err(PlaybackClockError::AudioMasterAlreadyRegistered)
        ));

        playback.reset_for_seek();
        audio.publish(2_000, 3_000, true).unwrap();
        assert_eq!(playback.master(), PlaybackMaster::Audio);
    }

    #[test]
    fn audio_projection_is_capped_at_submitted_media() {
        let wall = Arc::new(Clock::new());
        let playback = Arc::new(PlaybackClock::new(wall));
        let audio = playback.register_audio_master().unwrap();
        audio.publish(10, 1_000_000, true).unwrap();
        thread::sleep(Duration::from_millis(5));
        assert_eq!(playback.position_ns(), Some(1_000_000));
    }

    #[test]
    fn finished_audio_continues_on_wall_and_can_reset_for_seek() {
        let wall = Arc::new(Clock::new());
        let playback = Arc::new(PlaybackClock::new(wall));
        let audio = playback.register_audio_master().unwrap();
        audio.publish(1_000, 2_000, false).unwrap();
        audio.finish(2_000).unwrap();
        assert_eq!(playback.master(), PlaybackMaster::Wall);
        thread::sleep(Duration::from_millis(5));
        assert!(playback.position_ns().unwrap() > 2_000);

        audio.reset_for_seek().unwrap();
        assert_eq!(playback.master(), PlaybackMaster::AudioPriming);
        assert_eq!(playback.position_ns(), None);
    }
}