forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

invalidation.rs352 lines, 13.7 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/// A grade dragged while the timeline plays. The slider commits per pointer
73/// sample (`set_color_live`), which used to reseek the whole session per
74/// sample: an audio flush, an audio re-open and a video decoder rebuild, ~40
75/// times across one drag of the bar. The sound must not notice.
76#[test]
77fn a_grade_during_playback_never_touches_the_sound() {
78 let mut session = open(&asset("test_av.mp4"));
79 let filled = play_until_heard(&mut session);
80 let before = session.now();
81
82 for step in 0..20 {
83 let grade = ColorParams {
84 saturation: 1.0 - step as f32 / 40.0,
85 ..Default::default()
86 };
87 assert!(
88 session.set_color_live(Lane::V1, 0, Some(grade)),
89 "the grade takes at step {step}"
90 );
91 // Right now, with no wait: a flush shows up here before anything can
92 // refill it.
93 if let Some(n) = heard(&session) {
94 assert!(
95 n > 0,
96 "step {step} of the drag emptied the device queue -- the sound was torn down by a colour change"
97 );
98 }
99 session.tick();
100 while session.try_frame().is_some() {}
101 }
102
103 assert_eq!(
104 heard(&session).is_some(),
105 filled.is_some(),
106 "the audio path disappeared across the drag"
107 );
108 let after = session.now();
109 assert!(
110 after >= before,
111 "the clock went backwards across the drag: {before:.3}s -> {after:.3}s"
112 );
113 assert!(
114 after - before < 0.5,
115 "20 grade steps cost {:.3}s of timeline",
116 after - before
117 );
118 eprintln!("20 live grades: clock {before:.3}s -> {after:.3}s, tap {filled:?} samples");
119}
120
121/// The project resolution, changed while the timeline plays -- the 4K film the
122/// report came from. Every clip is recomposed onto the new canvas, which is a
123/// video decoder rebuild and nothing else: no sample changes, so no sample may
124/// be discarded and the clock may not be re-anchored (that re-anchor, onto an
125/// empty ring, is what put the picture permanently ahead of the sound).
126#[test]
127fn a_resolution_change_during_playback_never_touches_the_sound() {
128 let mut session = open(&asset("test_av.mp4"));
129 let filled = play_until_heard(&mut session);
130 let native = session.native_resolution();
131 let before = session.now();
132
133 for (w, h) in [(1920, 1080), (640, 360), (3840, 2160)] {
134 assert!(session.set_resolution(w, h), "resize to {w}x{h}");
135 if let Some(n) = heard(&session) {
136 assert!(
137 n > 0,
138 "resizing to {w}x{h} emptied the device queue -- the sound was torn down by a picture setting"
139 );
140 }
141 let at = session.now();
142 assert!(
143 at >= before && at - before < 0.5,
144 "the clock jumped across the resize: {before:.3}s -> {at:.3}s"
145 );
146 assert_eq!(session.resolution(), (w, h));
147 session.tick();
148 while session.try_frame().is_some() {}
149 }
150 assert_eq!(
151 session.native_resolution(),
152 native,
153 "the media's own size is not the project's"
154 );
155 assert_eq!(
156 heard(&session).is_some(),
157 filled.is_some(),
158 "the audio path disappeared across the resizes"
159 );
160
161 // ...and it is still running afterwards, at real speed.
162 let mark = session.now();
163 for _ in 0..30 {
164 session.tick();
165 while session.try_frame().is_some() {}
166 sleep(Duration::from_millis(10));
167 }
168 let ran = session.now() - mark;
169 eprintln!("after three resizes the clock ran {ran:.3}s in 0.3s of real time");
170 assert!(ran > 0.05, "the clock stalled after the resize: {ran:.3}s");
171}
172
173/// A fader and the master ceiling, moved while the timeline plays. Both live at
174/// the mix and nowhere else, so a running mixer picks them up between blocks --
175/// nothing is flushed, nothing is reopened, and the picture does not blink.
176///
177/// The *first* move is allowed one restart: a single audio lane at unity with
178/// the limiter off is not mixed at all (the bit-exact path), so moving off it
179/// is what opens a mixer. Every move after that is live, which is what an arrow
180/// key held down on the mix card actually does.
181#[test]
182fn a_fader_during_playback_stops_restarting_the_sound() {
183 let mut session = open(&asset("test_av.mp4"));
184 let filled = play_until_heard(&mut session);
185 if filled.is_none() {
186 eprintln!("no audio device: nothing to mix");
187 return;
188 }
189
190 // The move that opens the mixer, and the refill after it.
191 assert!(session.set_lane_gain_db(Lane::A1, -3.0), "turn A1 down");
192 play_until_heard(&mut session);
193
194 for step in 1..=20 {
195 let db = -3.0 - step as f32 * 0.5;
196 assert!(session.set_lane_gain_db(Lane::A1, db), "fader to {db} dB");
197 assert_eq!(
198 heard(&session),
199 filled,
200 "nudge {step} emptied the device queue -- a fader restarted the stream"
201 );
202 }
203 // ...and the ceiling beside it, which is retuned rather than rebuilt.
204 for step in 0..8 {
205 let limiter = Limiter {
206 on: step % 2 == 0,
207 ..Limiter::default()
208 }
209 .with_ceiling(-1.0 - step as f32);
210 assert!(session.set_limiter(limiter), "ceiling {step}");
211 assert_eq!(
212 heard(&session),
213 filled,
214 "limiter step {step} emptied the device queue"
215 );
216 }
217 assert_eq!(session.lane_gain_db(Lane::A1), -13.0);
218 eprintln!("20 fader nudges and 8 limiter moves: not one restart");
219}
220
221/// The same rule on the timeline that has **no mixer at all**: one audio lane
222/// at unity with the limiter off is the bit-exact single-stream path, and a
223/// ceiling dragged while the limiter is *off* changes not one sample of it.
224/// That drag used to reopen the sound at every nudge -- there was no mixer to
225/// hand the number to, so the rebuild was the only way to apply a setting that
226/// did not apply to anything.
227///
228/// The device queue is the oracle, as above; the worker count is the second
229/// one, because a rebuild is a feeder thread per nudge whether the ear catches
230/// it or not.
231#[test]
232fn a_ceiling_dragged_with_the_limiter_off_rebuilds_nothing() {
233 let mut session = open(&asset("test_av.mp4"));
234 let filled = play_until_heard(&mut session);
235 let workers = session.live_workers();
236
237 for step in 1..=20 {
238 // Off throughout: this is the setting nobody can hear yet. Every step a
239 // different ceiling, since setting the one already in force is refused
240 // and would measure nothing.
241 let limiter = Limiter {
242 on: false,
243 ..Limiter::default()
244 }
245 .with_ceiling(-1.0 - step as f32 * 0.25);
246 assert!(session.set_limiter(limiter), "ceiling {step}");
247 assert_eq!(
248 heard(&session),
249 filled,
250 "nudge {step} emptied the device queue -- a silent setting restarted the stream"
251 );
252 assert!(
253 session.live_workers() <= workers,
254 "nudge {step} started a worker: {} against {workers} before the drag",
255 session.live_workers()
256 );
257 }
258
259 // ...and turning it *on* is the one move that has to open a mixer, which is
260 // a rebuild and is allowed to be one.
261 assert!(session.set_limiter(Limiter::default().with_ceiling(-1.0)), "limiter on");
262 eprintln!("20 ceiling nudges with the limiter off: not one rebuild");
263}
264
265/// The other half of the rule, and the one that makes it a *classification*
266/// rather than a blanket "never rebuild": an equalizer really is a change to
267/// the samples, so it does restart the audio -- and must leave the picture
268/// alone while it does.
269#[test]
270fn an_equalizer_rebuilds_the_sound_and_only_the_sound() {
271 let mut session = open(&asset("test_av.mp4"));
272 play_until_heard(&mut session);
273 let before = session.now();
274
275 let params = engine::eq::EqParams::default_layout();
276 let mut curve = params.clone();
277 curve.bands[0].gain_db = 6.0;
278 assert!(session.set_eq(Lane::A1, 0, Some(curve)), "equalize A1's clip");
279
280 let after = session.now();
281 assert!(
282 after >= before && after - before < 0.5,
283 "the equalizer moved the playhead: {before:.3}s -> {after:.3}s"
284 );
285 // The picture keeps coming: the video worker was never cancelled.
286 let mut frames = 0;
287 for _ in 0..60 {
288 session.tick();
289 while session.try_frame().is_some() {
290 frames += 1;
291 }
292 sleep(Duration::from_millis(10));
293 }
294 eprintln!("{frames} frames across an equalizer change, clock at {:.3}s", session.now());
295 assert!(frames > 0, "the picture stopped when the sound was rebuilt");
296}
297
298/// The same classification asked at the one place it decides something a person
299/// can see: **after the timeline has played out**. A sound edit rebuilds the
300/// sound and starts no picture worker, so end of stream stands -- the last frame
301/// is still the last frame, the transport is still `Ended`, and the next press
302/// restarts from the top with the new curve on it. A revival here would spend
303/// that state on an edit with nothing new to show, leaving a play press to run
304/// the clock off the end instead of starting the film again.
305#[test]
306fn a_sound_edit_after_the_end_leaves_the_end_where_it_is() {
307 let mut session = open(&asset("test_av.mp4"));
308 // A breath short of the end: the tail is what this is about.
309 session.seek(4.8);
310 session.play();
311 let deadline = std::time::Instant::now() + Duration::from_secs(20);
312 while !session.is_eos() {
313 assert!(
314 std::time::Instant::now() < deadline,
315 "never reached the end of a 5 s file"
316 );
317 session.tick();
318 while session.try_frame().is_some() {}
319 }
320 // What the front-end does on the crossing (`Player::pump`): stopped, on the
321 // out point, and still ended.
322 session.halt_at_end();
323 let ended_at = session.now();
324 assert_eq!(ended_at, session.timeline_duration());
325
326 let mut curve = engine::eq::EqParams::default_layout();
327 curve.bands[0].gain_db = 6.0;
328 assert!(session.set_eq(Lane::A1, 0, Some(curve)), "equalize at the end");
329 assert!(
330 session.is_eos(),
331 "an equalizer past the end revived the session: nothing new to show"
332 );
333 assert!(!session.is_playing(), "...and it started the transport again");
334 assert!(
335 (session.now() - ended_at).abs() < 1e-9,
336 "the playhead left the out point: {:.6}s",
337 session.now()
338 );
339 // The parameters landed all the same -- that is what "the edit took" means.
340 assert!(session.eq_of(Lane::A1, 0).is_some(), "the curve did not apply");
341
342 // The fader's first move at the end goes down the same road (no mix is
343 // running to push it into, so it reseeks the audio too).
344 assert!(session.set_lane_gain_db(Lane::A1, -6.0), "fader at the end");
345 assert!(session.is_eos(), "a fader past the end revived the session");
346 assert_eq!(session.lane_gain_db(Lane::A1), -6.0);
347
348 // And the restart off that end is still there, which is what the state was
349 // being kept for.
350 session.seek(0.0);
351 assert!(!session.is_eos(), "a seek revives a played-out session");
352}