forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

invalidation.rs391 lines, 15.4 KiBHistoryRawDownload
1//! What an edit is allowed to tear down, and what it is not.
2//!
3//! Every setting a person can move while a timeline plays dirties *one* half of
4//! the pipeline at most: a grade, a fit policy and the project resolution are
5//! pictures, an equalizer is sound, and a fader is not even that -- it is a
6//! number the running mixer picks up between two blocks. The tests here are the
7//! measurement of that claim, and the thing they measure is the **audio tap**:
8//! it is the sound the device was handed last, and a restart clears it (the
9//! ring is flushed and nothing is fed again until a decoder has reopened, tens
10//! of milliseconds later). An empty tap right after an edit therefore *is* the
11//! hole in the sound the user reported hearing when they dragged a colour
12//! slider.
13//!
14//! Every test runs anywhere: with no PipeWire daemon there is no device, no tap
15//! and nothing to protect, so the audio assertions are made only when there is
16//! sound to make them about -- the clock assertions run either way.
17//!
18//! ```text
19//! cargo test -p engine --test invalidation -- --nocapture
20//! ```
21
22use std::path::{Path, PathBuf};
23use std::thread::sleep;
24use std::time::Duration;
25
26use engine::PlaybackSession;
27use engine::color::ColorParams;
28use engine::limiter::Limiter;
29use engine::project::Lane;
30
31fn asset(name: &str) -> PathBuf {
32 PathBuf::from(env!("CARGO_MANIFEST_DIR"))
33 .join("../../assets")
34 .join(name)
35}
36
37/// Silent, like every other suite that plays a real file: these tests are about
38/// whether the sound is *interrupted*, not about what it sounds like, and the
39/// gain is the last multiply before the device -- everything measured here is
40/// decoded, fed and counted exactly as it is when it is audible.
41fn open(path: &Path) -> PlaybackSession {
42 let session = PlaybackSession::open(path).expect("open");
43 session.set_gain(0.0);
44 session
45}
46
47/// How many samples the device has been handed lately. `None` with no audio
48/// device at all -- the machine's business, and nothing to assert about.
49fn heard(session: &PlaybackSession) -> Option<usize> {
50 session.audio_tap().map(|(tap, _)| tap.len())
51}
52
53/// Plays until the device has really been handed something, so a later empty
54/// tap means "this edit emptied it" rather than "it was never filled".
55fn play_until_heard(session: &mut PlaybackSession) -> Option<usize> {
56 session.play();
57 for _ in 0..200 {
58 session.tick();
59 while session.try_frame().is_some() {}
60 if heard(session).is_none_or(|n| n > 0) {
61 break;
62 }
63 sleep(Duration::from_millis(10));
64 }
65 let filled = heard(session);
66 if filled == Some(0) {
67 panic!("the device was never fed: nothing to measure");
68 }
69 filled
70}
71
72/// Waits until the picture has delivered a frame. The sound is heard first: the
73/// first picture trails it by tens to hundreds of milliseconds unloaded, and by
74/// more on a busy machine, so a window measured right after
75/// [`play_until_heard`] would be measuring the picture *starting*, not
76/// continuing. Polled against a generous deadline rather than a fixed wait.
77fn play_until_picture(session: &mut PlaybackSession) {
78 let deadline = std::time::Instant::now() + Duration::from_secs(20);
79 loop {
80 session.tick();
81 if session.try_frame().is_some() {
82 break;
83 }
84 assert!(
85 std::time::Instant::now() < deadline,
86 "no picture frame within 20 s of playback starting"
87 );
88 sleep(Duration::from_millis(10));
89 }
90}
91
92/// A grade dragged while the timeline plays. The slider commits per pointer
93/// sample (`set_color_live`), which used to reseek the whole session per
94/// sample: an audio flush, an audio re-open and a video decoder rebuild, ~40
95/// times across one drag of the bar. The sound must not notice.
96#[test]
97fn a_grade_during_playback_never_touches_the_sound() {
98 let mut session = open(&asset("test_av.mp4"));
99 let filled = play_until_heard(&mut session);
100 let before = session.now();
101
102 for step in 0..20 {
103 let grade = ColorParams {
104 saturation: 1.0 - step as f32 / 40.0,
105 ..Default::default()
106 };
107 assert!(
108 session.set_color_live(Lane::V1, 0, Some(grade)),
109 "the grade takes at step {step}"
110 );
111 // Right now, with no wait: a flush shows up here before anything can
112 // refill it.
113 if let Some(n) = heard(&session) {
114 assert!(
115 n > 0,
116 "step {step} of the drag emptied the device queue -- the sound was torn down by a colour change"
117 );
118 }
119 session.tick();
120 while session.try_frame().is_some() {}
121 }
122
123 assert_eq!(
124 heard(&session).is_some(),
125 filled.is_some(),
126 "the audio path disappeared across the drag"
127 );
128 let after = session.now();
129 assert!(
130 after >= before,
131 "the clock went backwards across the drag: {before:.3}s -> {after:.3}s"
132 );
133 assert!(
134 after - before < 0.5,
135 "20 grade steps cost {:.3}s of timeline",
136 after - before
137 );
138 eprintln!("20 live grades: clock {before:.3}s -> {after:.3}s, tap {filled:?} samples");
139}
140
141/// The project resolution, changed while the timeline plays -- the 4K film the
142/// report came from. Every clip is recomposed onto the new canvas, which is a
143/// video decoder rebuild and nothing else: no sample changes, so no sample may
144/// be discarded and the clock may not be re-anchored (that re-anchor, onto an
145/// empty ring, is what put the picture permanently ahead of the sound).
146#[test]
147fn a_resolution_change_during_playback_never_touches_the_sound() {
148 let mut session = open(&asset("test_av.mp4"));
149 let filled = play_until_heard(&mut session);
150 let native = session.native_resolution();
151 let before = session.now();
152
153 for (w, h) in [(1920, 1080), (640, 360), (3840, 2160)] {
154 assert!(session.set_resolution(w, h), "resize to {w}x{h}");
155 if let Some(n) = heard(&session) {
156 assert!(
157 n > 0,
158 "resizing to {w}x{h} emptied the device queue -- the sound was torn down by a picture setting"
159 );
160 }
161 let at = session.now();
162 assert!(
163 at >= before && at - before < 0.5,
164 "the clock jumped across the resize: {before:.3}s -> {at:.3}s"
165 );
166 assert_eq!(session.resolution(), (w, h));
167 session.tick();
168 while session.try_frame().is_some() {}
169 }
170 assert_eq!(
171 session.native_resolution(),
172 native,
173 "the media's own size is not the project's"
174 );
175 assert_eq!(
176 heard(&session).is_some(),
177 filled.is_some(),
178 "the audio path disappeared across the resizes"
179 );
180
181 // ...and it is still running afterwards, at real speed.
182 let mark = session.now();
183 for _ in 0..30 {
184 session.tick();
185 while session.try_frame().is_some() {}
186 sleep(Duration::from_millis(10));
187 }
188 let ran = session.now() - mark;
189 eprintln!("after three resizes the clock ran {ran:.3}s in 0.3s of real time");
190 assert!(ran > 0.05, "the clock stalled after the resize: {ran:.3}s");
191}
192
193/// A fader and the master ceiling, moved while the timeline plays. Both live at
194/// the mix and nowhere else, so a running mixer picks them up between blocks --
195/// nothing is flushed, nothing is reopened, and the picture does not blink.
196///
197/// The *first* move is allowed one restart: a single audio lane at unity with
198/// the limiter off is not mixed at all (the bit-exact path), so moving off it
199/// is what opens a mixer. Every move after that is live, which is what an arrow
200/// key held down on the mix card actually does.
201///
202/// The device queue must never shrink: a restart empties it, and that is the
203/// failure. It may keep filling while the timeline plays on, so each check is
204/// against the last reading, not against the reading taken when playback began.
205#[test]
206fn a_fader_during_playback_stops_restarting_the_sound() {
207 let mut session = open(&asset("test_av.mp4"));
208 let filled = play_until_heard(&mut session);
209 if filled.is_none() {
210 eprintln!("no audio device: nothing to mix");
211 return;
212 }
213
214 // The move that opens the mixer, and the refill after it.
215 assert!(session.set_lane_gain_db(Lane::A1, -3.0), "turn A1 down");
216 play_until_heard(&mut session);
217 let mut last = heard(&session);
218
219 for step in 1..=20 {
220 let db = -3.0 - step as f32 * 0.5;
221 assert!(session.set_lane_gain_db(Lane::A1, db), "fader to {db} dB");
222 let now = heard(&session);
223 assert!(
224 now >= last,
225 "nudge {step} emptied the device queue -- a fader restarted the stream: {last:?} -> {now:?}"
226 );
227 last = now;
228 }
229 // ...and the ceiling beside it, which is retuned rather than rebuilt.
230 for step in 0..8 {
231 let limiter = Limiter {
232 on: step % 2 == 0,
233 ..Limiter::default()
234 }
235 .with_ceiling(-1.0 - step as f32);
236 assert!(session.set_limiter(limiter), "ceiling {step}");
237 let now = heard(&session);
238 assert!(
239 now >= last,
240 "limiter step {step} emptied the device queue: {last:?} -> {now:?}"
241 );
242 last = now;
243 }
244 assert_eq!(session.lane_gain_db(Lane::A1), -13.0);
245 eprintln!("20 fader nudges and 8 limiter moves: not one restart");
246}
247
248/// The same rule on the timeline that has **no mixer at all**: one audio lane
249/// at unity with the limiter off is the bit-exact single-stream path, and a
250/// ceiling dragged while the limiter is *off* changes not one sample of it.
251/// That drag used to reopen the sound at every nudge -- there was no mixer to
252/// hand the number to, so the rebuild was the only way to apply a setting that
253/// did not apply to anything.
254///
255/// The device queue is the oracle, as above -- it must never shrink, though it
256/// may keep filling while the timeline plays on; the worker count is the second
257/// one, because a rebuild is a feeder thread per nudge whether the ear catches
258/// it or not.
259#[test]
260fn a_ceiling_dragged_with_the_limiter_off_rebuilds_nothing() {
261 let mut session = open(&asset("test_av.mp4"));
262 let filled = play_until_heard(&mut session);
263 let workers = session.live_workers();
264 let mut last = filled;
265
266 for step in 1..=20 {
267 // Off throughout: this is the setting nobody can hear yet. Every step a
268 // different ceiling, since setting the one already in force is refused
269 // and would measure nothing.
270 let limiter = Limiter {
271 on: false,
272 ..Limiter::default()
273 }
274 .with_ceiling(-1.0 - step as f32 * 0.25);
275 assert!(session.set_limiter(limiter), "ceiling {step}");
276 let now = heard(&session);
277 assert!(
278 now >= last,
279 "nudge {step} emptied the device queue -- a silent setting restarted the stream: {last:?} -> {now:?}"
280 );
281 last = now;
282 assert!(
283 session.live_workers() <= workers,
284 "nudge {step} started a worker: {} against {workers} before the drag",
285 session.live_workers()
286 );
287 }
288
289 // ...and turning it *on* is the one move that has to open a mixer, which is
290 // a rebuild and is allowed to be one.
291 assert!(session.set_limiter(Limiter::default().with_ceiling(-1.0)), "limiter on");
292 eprintln!("20 ceiling nudges with the limiter off: not one rebuild");
293}
294
295/// The other half of the rule, and the one that makes it a *classification*
296/// rather than a blanket "never rebuild": an equalizer really is a change to
297/// the samples, so it does restart the audio -- and must leave the picture
298/// alone while it does.
299#[test]
300fn an_equalizer_rebuilds_the_sound_and_only_the_sound() {
301 let mut session = open(&asset("test_av.mp4"));
302 play_until_heard(&mut session);
303 play_until_picture(&mut session);
304 let before = session.now();
305
306 let params = engine::eq::EqParams::default_layout();
307 let mut curve = params.clone();
308 curve.bands[0].gain_db = 6.0;
309 assert!(session.set_eq(Lane::A1, 0, Some(curve)), "equalize A1's clip");
310
311 let after = session.now();
312 assert!(
313 after >= before && after - before < 0.5,
314 "the equalizer moved the playhead: {before:.3}s -> {after:.3}s"
315 );
316 // The picture keeps coming: the video worker was never cancelled. The
317 // window is a minimum of 60 ticks, extended until a frame arrives, so the
318 // check is that the picture continues, not how fast a loaded machine decodes.
319 let deadline = std::time::Instant::now() + Duration::from_secs(20);
320 let mut frames = 0;
321 let mut ticks = 0;
322 while ticks < 60 || frames == 0 {
323 assert!(
324 std::time::Instant::now() < deadline,
325 "the picture stopped when the sound was rebuilt ({frames} frames in {ticks} ticks)"
326 );
327 session.tick();
328 while session.try_frame().is_some() {
329 frames += 1;
330 }
331 ticks += 1;
332 sleep(Duration::from_millis(10));
333 }
334 eprintln!("{frames} frames across an equalizer change, clock at {:.3}s", session.now());
335}
336
337/// The same classification asked at the one place it decides something a person
338/// can see: **after the timeline has played out**. A sound edit rebuilds the
339/// sound and starts no picture worker, so end of stream stands -- the last frame
340/// is still the last frame, the transport is still `Ended`, and the next press
341/// restarts from the top with the new curve on it. A revival here would spend
342/// that state on an edit with nothing new to show, leaving a play press to run
343/// the clock off the end instead of starting the film again.
344#[test]
345fn a_sound_edit_after_the_end_leaves_the_end_where_it_is() {
346 let mut session = open(&asset("test_av.mp4"));
347 // A breath short of the end: the tail is what this is about.
348 session.seek(4.8);
349 session.play();
350 let deadline = std::time::Instant::now() + Duration::from_secs(20);
351 while !session.is_eos() {
352 assert!(
353 std::time::Instant::now() < deadline,
354 "never reached the end of a 5 s file"
355 );
356 session.tick();
357 while session.try_frame().is_some() {}
358 }
359 // What the front-end does on the crossing (`Player::pump`): stopped, on the
360 // out point, and still ended.
361 session.halt_at_end();
362 let ended_at = session.now();
363 assert_eq!(ended_at, session.timeline_duration());
364
365 let mut curve = engine::eq::EqParams::default_layout();
366 curve.bands[0].gain_db = 6.0;
367 assert!(session.set_eq(Lane::A1, 0, Some(curve)), "equalize at the end");
368 assert!(
369 session.is_eos(),
370 "an equalizer past the end revived the session: nothing new to show"
371 );
372 assert!(!session.is_playing(), "...and it started the transport again");
373 assert!(
374 (session.now() - ended_at).abs() < 1e-9,
375 "the playhead left the out point: {:.6}s",
376 session.now()
377 );
378 // The parameters landed all the same -- that is what "the edit took" means.
379 assert!(session.eq_of(Lane::A1, 0).is_some(), "the curve did not apply");
380
381 // The fader's first move at the end goes down the same road (no mix is
382 // running to push it into, so it reseeks the audio too).
383 assert!(session.set_lane_gain_db(Lane::A1, -6.0), "fader at the end");
384 assert!(session.is_eos(), "a fader past the end revived the session");
385 assert_eq!(session.lane_gain_db(Lane::A1), -6.0);
386
387 // And the restart off that end is still there, which is what the state was
388 // being kept for.
389 session.seek(0.0);
390 assert!(!session.is_eos(), "a seek revives a played-out session");
391}