summaryrefslogtreecommitdiff
path: root/rust/crates/playback/src/lib.rs
blob: 2e10e8183f524a356d2830a39f1426b0a8e17109 (plain) (blame)
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
use time::{FrameRate, MediaTime};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PlaybackEvent {
    Started,
    Paused,
    Seeked(MediaTime),
    Updated(MediaTime),
    ReachedEnd(MediaTime),
}

pub struct PlaybackClock {
    is_playing: bool,
    current_time: MediaTime,
    total_duration: MediaTime,
    frame_rate: Option<FrameRate>,
    anchor_media: MediaTime,
    anchor_wall_seconds: f64,
    volume: f64,
    previous_volume: f64,
    muted: bool,
    is_scrubbing: bool,
}

impl Default for PlaybackClock {
    fn default() -> Self {
        Self::new()
    }
}

impl PlaybackClock {
    pub fn new() -> Self {
        Self {
            is_playing: false,
            current_time: MediaTime::ZERO,
            total_duration: MediaTime::ZERO,
            frame_rate: None,
            anchor_media: MediaTime::ZERO,
            anchor_wall_seconds: 0.0,
            volume: 1.0,
            previous_volume: 1.0,
            muted: false,
            is_scrubbing: false,
        }
    }

    pub fn is_playing(&self) -> bool {
        self.is_playing
    }

    pub fn current_time(&self) -> MediaTime {
        self.current_time
    }

    pub fn total_duration(&self) -> MediaTime {
        self.total_duration
    }

    pub fn volume(&self) -> f64 {
        self.volume
    }

    pub fn is_muted(&self) -> bool {
        self.muted
    }

    pub fn is_scrubbing(&self) -> bool {
        self.is_scrubbing
    }

    pub fn set_total_duration(&mut self, duration: MediaTime) -> Vec<PlaybackEvent> {
        self.total_duration = duration;
        let clamped = self.clamp(self.current_time);
        let reached_end = self.is_playing && clamped >= self.total_duration;
        if clamped == self.current_time && !reached_end {
            return Vec::new();
        }
        self.current_time = clamped;
        let mut events = vec![PlaybackEvent::Seeked(clamped)];
        if reached_end {
            self.is_playing = false;
            events.push(PlaybackEvent::Paused);
        }
        events
    }

    pub fn set_frame_rate(&mut self, frame_rate: Option<FrameRate>) {
        self.frame_rate = frame_rate;
    }

    pub fn play(&mut self, wall_seconds: f64) -> Vec<PlaybackEvent> {
        if self.total_duration <= MediaTime::ZERO {
            return Vec::new();
        }
        let mut events = Vec::new();
        if self.current_time >= self.total_duration {
            self.current_time = MediaTime::ZERO;
            events.push(PlaybackEvent::Seeked(MediaTime::ZERO));
        }
        self.is_playing = true;
        self.anchor_media = self.current_time;
        self.anchor_wall_seconds = wall_seconds;
        events.push(PlaybackEvent::Started);
        events
    }

    pub fn pause(&mut self) -> Vec<PlaybackEvent> {
        if !self.is_playing {
            return Vec::new();
        }
        self.is_playing = false;
        vec![PlaybackEvent::Paused]
    }

    pub fn toggle(&mut self, wall_seconds: f64) -> Vec<PlaybackEvent> {
        if self.is_playing {
            self.pause()
        } else {
            self.play(wall_seconds)
        }
    }

    pub fn seek(&mut self, time: MediaTime, wall_seconds: f64) -> Vec<PlaybackEvent> {
        self.current_time = self.clamp(time);
        if self.is_playing {
            self.anchor_media = self.current_time;
            self.anchor_wall_seconds = wall_seconds;
        }
        vec![PlaybackEvent::Seeked(self.current_time)]
    }

    pub fn tick(&mut self, wall_seconds: f64) -> Vec<PlaybackEvent> {
        if !self.is_playing {
            return Vec::new();
        }
        let elapsed = wall_seconds - self.anchor_wall_seconds;
        let Some(elapsed_time) = MediaTime::from_seconds_f64(elapsed) else {
            return Vec::new();
        };
        let raw = MediaTime::from_ticks(
            self.anchor_media.as_ticks().saturating_add(elapsed_time.as_ticks()),
        );
        let rounded = self.round_to_frame(raw);

        if rounded >= self.total_duration {
            self.is_playing = false;
            self.current_time = self.total_duration;
            return vec![
                PlaybackEvent::Paused,
                PlaybackEvent::ReachedEnd(self.total_duration),
            ];
        }
        if rounded == self.current_time {
            return Vec::new();
        }
        self.current_time = rounded;
        vec![PlaybackEvent::Updated(rounded)]
    }

    pub fn set_volume(&mut self, volume: f64) {
        let clamped = volume.clamp(0.0, 1.0);
        self.volume = clamped;
        self.muted = clamped == 0.0;
        if clamped > 0.0 {
            self.previous_volume = clamped;
        }
    }

    pub fn mute(&mut self) {
        if self.volume > 0.0 {
            self.previous_volume = self.volume;
        }
        self.muted = true;
        self.volume = 0.0;
    }

    pub fn unmute(&mut self) {
        self.muted = false;
        self.volume = self.previous_volume;
    }

    pub fn toggle_mute(&mut self) {
        if self.muted {
            self.unmute();
        } else {
            self.mute();
        }
    }

    pub fn set_scrubbing(&mut self, is_scrubbing: bool) {
        self.is_scrubbing = is_scrubbing;
    }

    fn clamp(&self, time: MediaTime) -> MediaTime {
        time.clamp(MediaTime::ZERO, self.total_duration)
    }

    fn round_to_frame(&self, time: MediaTime) -> MediaTime {
        let Some(rate) = self.frame_rate else {
            return time;
        };
        let Some(frame) = time.to_frame_round(rate) else {
            return time;
        };
        MediaTime::from_frame(frame, rate).unwrap_or(time)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const FPS_30: FrameRate = FrameRate {
        numerator: 30,
        denominator: 1,
    };

    fn clock() -> PlaybackClock {
        let mut clock = PlaybackClock::new();
        clock.set_frame_rate(Some(FPS_30));
        clock.set_total_duration(MediaTime::from_seconds_f64(4.0).unwrap());
        clock
    }

    #[test]
    fn play_at_end_restarts_from_zero() {
        let mut clock = clock();
        clock.seek(MediaTime::from_seconds_f64(10.0).unwrap(), 0.0);
        let events = clock.play(0.0);
        assert!(events.contains(&PlaybackEvent::Seeked(MediaTime::ZERO)));
        assert!(events.contains(&PlaybackEvent::Started));
    }

    #[test]
    fn play_with_empty_timeline_does_nothing() {
        let mut clock = PlaybackClock::new();
        assert!(clock.play(0.0).is_empty());
        assert!(!clock.is_playing());
    }

    #[test]
    fn tick_rounds_to_frames() {
        let mut clock = clock();
        clock.play(0.0);
        let events = clock.tick(0.5);
        let expected = MediaTime::from_frame(15, FPS_30).unwrap();
        assert_eq!(events, vec![PlaybackEvent::Updated(expected)]);
        assert_eq!(clock.current_time(), expected);
    }

    #[test]
    fn tick_stops_at_end() {
        let mut clock = clock();
        clock.play(0.0);
        let events = clock.tick(10.0);
        assert!(events.contains(&PlaybackEvent::Paused));
        assert_eq!(clock.current_time(), clock.total_duration());
        assert!(!clock.is_playing());
    }

    #[test]
    fn tick_same_frame_emits_nothing() {
        let mut clock = clock();
        clock.play(0.0);
        assert!(clock.tick(0.001).is_empty());
    }

    #[test]
    fn seek_clamps_into_timeline() {
        let mut clock = clock();
        clock.seek(MediaTime::from_seconds_f64(99.0).unwrap(), 0.0);
        assert_eq!(clock.current_time(), clock.total_duration());
    }

    #[test]
    fn seek_while_playing_reanchors() {
        let mut clock = clock();
        clock.play(0.0);
        clock.seek(MediaTime::from_seconds_f64(1.0).unwrap(), 5.0);
        let events = clock.tick(6.0);
        let expected = MediaTime::from_frame(60, FPS_30).unwrap();
        assert_eq!(events, vec![PlaybackEvent::Updated(expected)]);
    }

    #[test]
    fn volume_mute_cycle_restores_previous() {
        let mut clock = clock();
        clock.set_volume(0.7);
        clock.mute();
        assert_eq!(clock.volume(), 0.0);
        assert!(clock.is_muted());
        clock.unmute();
        assert_eq!(clock.volume(), 0.7);
        assert!(!clock.is_muted());
    }

    #[test]
    fn zero_volume_counts_as_muted() {
        let mut clock = clock();
        clock.set_volume(0.5);
        clock.set_volume(0.0);
        assert!(clock.is_muted());
        clock.unmute();
        assert_eq!(clock.volume(), 0.5);
    }

    #[test]
    fn shrinking_duration_pauses_at_end() {
        let mut clock = clock();
        clock.play(0.0);
        clock.tick(2.0);
        let events = clock.set_total_duration(MediaTime::from_seconds_f64(1.0).unwrap());
        assert!(events.contains(&PlaybackEvent::Paused));
        assert_eq!(clock.current_time(), MediaTime::from_seconds_f64(1.0).unwrap());
    }
}