diff --git a/crates/engine/tests/ao_output.rs b/crates/engine/tests/ao_output.rs index 8338fac..5c002e9 100644 --- a/crates/engine/tests/ao_output.rs +++ b/crates/engine/tests/ao_output.rs @@ -28,6 +28,23 @@ fn tone() -> Vec { samples } +/// Opens the device the way this suite wants it: connected for real, and +/// silent. Everything these tests assert -- that the clock runs, at the right +/// rate, and stops when the stream is paused -- is measured from the device's +/// own position, which the gain does not touch: the frames are still consumed, +/// only the last multiply before the speaker is zero. +/// +/// ponytail: it does mean the suite is deaf, so a plugin that shipped with the +/// gain stuck at zero would pass it. Nothing here could ever hear that -- the +/// checks that can are `scale`'s unit test in engine-audio (unity leaves the +/// bytes alone) and a person with the app open. The upgrade path is recording +/// a null sink and asserting on the samples that come back. +fn open_silent() -> AoSession { + let ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + assert!(ao.set_volume(0.0), "mute rejected"); + ao +} + /// Waits up to `limit` for the clock to report a played position past zero. fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { let start = Instant::now(); @@ -46,7 +63,7 @@ fn wait_for_playback(ao: &AoSession, limit: Duration) -> i64 { #[ignore = "needs libengine_audio.so and a PipeWire daemon"] fn plays_and_tracks_position() { assert!(AoSession::probe(), "plugin not loadable"); - let mut ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + let mut ao = open_silent(); // The ring holds a second, so two halves go in whole -- enough real audio // to cover the measurement below without starving. @@ -96,14 +113,13 @@ fn plays_and_tracks_position() { #[ignore = "needs libengine_audio.so and a PipeWire daemon"] fn muting_silences_without_stopping_the_clock() { assert!(AoSession::probe(), "plugin not loadable"); - let mut ao = AoSession::open(RATE, CHANNELS).expect("no PipeWire playback available"); + let mut ao = open_silent(); let samples = tone(); assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); let first = wait_for_playback(&ao, Duration::from_secs(2)); - // Mute, then keep it fed: the position must move exactly as it would have. - assert!(ao.set_volume(0.0), "mute rejected"); + // Muted and fed: the position must move exactly as it would have. assert_eq!(ao.write(&samples), Some(samples.len()), "short write"); std::thread::sleep(Duration::from_millis(300)); let muted = ao.position().expect("position went unknown"); @@ -115,8 +131,15 @@ fn muting_silences_without_stopping_the_clock() { "the clock stalled while muted: {advanced} samples in 300 ms" ); - // Every level in range is taken, and the two ends are in range. - for gain in [0.0, 0.5, 1.0] { + // Every level in range is taken, and the two ends are in range. Done on an + // emptied ring and left back at zero, so that the audible gains this walks + // through have nothing but the underrun fill to be applied to: whoever runs + // the suite should not have to hear it prove that 1.0 is a legal number. + assert!(ao.flush(), "flush rejected"); + // A flush lands on the next callback and the ring refuses writes until it + // does, so give it a couple of quanta before asking for anything else. + std::thread::sleep(Duration::from_millis(50)); + for gain in [0.5, 1.0, 0.0] { assert!(ao.set_volume(gain), "gain {gain} rejected"); } // Out of range is refused at the ABI rather than multiplied into the diff --git a/crates/engine/tests/playback.rs b/crates/engine/tests/playback.rs index 154dbdb..546c776 100644 --- a/crates/engine/tests/playback.rs +++ b/crates/engine/tests/playback.rs @@ -20,6 +20,22 @@ fn asset(name: &str) -> PathBuf { .join(name) } +/// Opens a session the way every test here wants one: silent. The suite plays +/// real files through the real device, so without this a `cargo test` is two +/// minutes of noise out of the speakers of whoever ran it. +/// +/// Muting is the gain knob, not a pause and not a fake device: the stream is +/// still connected, the daemon still schedules it, the samples are still +/// decoded, written, and handed over -- only the last multiply before the +/// device is zero. Every clock, `fed` and `played_out` assertion below is +/// therefore measuring exactly what it measured when this was audible. +fn open(path: impl AsRef) -> PlaybackSession { + let session = PlaybackSession::open(path).expect("open"); + // False for the silent fixtures, which have no device to quieten. + session.set_gain(0.0); + session +} + /// One render tick of a front-end: advance the clock, take whatever frames are /// due. Returns the highest frame index seen so far. fn pump(session: &mut PlaybackSession, last_index: &mut Option) { @@ -53,7 +69,7 @@ fn run_for(session: &mut PlaybackSession, last_index: &mut Option, duration fn plays_at_real_speed(name: &str) { let path = asset(name); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); assert_eq!(session.now(), 0.0, "a fresh session starts at zero"); assert!(!session.is_playing(), "a fresh session starts paused"); let mut last_index = None; @@ -161,7 +177,7 @@ fn threads() -> usize { /// Wall-clock seek: no audio track, so this runs anywhere. #[test] fn seek_repositions_video_and_clock() { - let mut session = PlaybackSession::open(asset("test_baseline.mp4")).expect("open"); + let mut session = open(asset("test_baseline.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -195,7 +211,7 @@ fn seek_repositions_video_and_clock() { /// device is never fed another sample. #[test] fn seek_past_the_end_clamps() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let meta = *session.meta(); let duration = f64::from(meta.frame_count) / meta.frame_rate; @@ -224,7 +240,7 @@ fn seek_past_the_end_clamps() { /// abandoned workers behind, and the last one still wins. #[test] fn rapid_seeks_settle_on_the_last_one() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -254,7 +270,7 @@ fn rapid_seeks_settle_on_the_last_one() { #[test] #[ignore = "needs a running PipeWire daemon"] fn seek_keeps_the_audio_clock() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let meta = *session.meta(); let mut last_index = None; @@ -309,7 +325,7 @@ fn seek_keeps_the_audio_clock() { #[test] fn edits_traverse_cuts() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let whole = f64::from(total) / fps; assert!((session.timeline_duration() - whole).abs() < 1e-9); @@ -407,7 +423,7 @@ fn edits_traverse_cuts() { #[test] fn paste_duplicates_a_clip_at_the_playhead() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let whole = f64::from(total) / fps; @@ -481,7 +497,7 @@ fn paste_duplicates_a_clip_at_the_playhead() { #[test] fn paste_while_playing_does_not_stall() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let (fps, total) = (session.meta().frame_rate, session.meta().frame_count); let mut last_index = None; @@ -519,7 +535,7 @@ fn paste_while_playing_does_not_stall() { #[test] fn paste_at_eos_revives_the_session() { let path = asset("test_baseline.mp4"); - let mut session = PlaybackSession::open(&path).expect("open"); + let mut session = open(&path); let total = session.meta().frame_count; assert!(session.cut_at(2.0), "cut at 2 s"); @@ -555,7 +571,7 @@ fn paste_at_eos_revives_the_session() { #[test] #[ignore = "needs a running PipeWire daemon"] fn edits_keep_the_audio_clock() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let fps = session.meta().frame_rate; let mut last_index = None; @@ -622,7 +638,7 @@ fn frame_count(path: &Path) -> u32 { #[test] fn import_appends_a_second_source_and_plays_across_the_join() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); - let mut session = PlaybackSession::open(&av).expect("open"); + let mut session = open(&av); let (fps, first) = (session.meta().frame_rate, session.meta().frame_count); let second = frame_count(&av2); assert_eq!((first, second), (150, 120), "fixtures changed"); @@ -708,7 +724,7 @@ fn import_appends_a_second_source_and_plays_across_the_join() { /// changes nothing. #[test] fn import_refuses_what_does_not_match() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let err = session .import(&asset("test_mismatch.mp4")) @@ -731,7 +747,7 @@ fn import_refuses_what_does_not_match() { assert!((session.timeline_duration() - 5.0).abs() < 1e-9); // The mirror: audio into a silent timeline is refused just as loudly. - let mut silent = PlaybackSession::open(asset("test_baseline.mp4")).expect("open"); + let mut silent = open(asset("test_baseline.mp4")); let err = silent .import(&asset("test_av.mp4")) .expect_err("audio into a silent timeline") @@ -743,7 +759,7 @@ fn import_refuses_what_does_not_match() { /// out once and reused, which is what keeps clipboard clips valid forever. #[test] fn importing_one_file_twice_reuses_its_source() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let av2 = asset("test_av2.mp4"); session.import(&av2).expect("first import"); session.import(&av2).expect("second import"); @@ -768,7 +784,7 @@ fn importing_one_file_twice_reuses_its_source() { #[test] fn import_at_eos_resumes_into_the_new_clip() { let av2 = asset("test_av2.mp4"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; session.play(); @@ -797,7 +813,7 @@ fn a_vanished_source_is_skipped() { let scratch = std::env::temp_dir().join(format!("video_editor_vanish_{}.mp4", std::process::id())); std::fs::copy(asset("test_av2.mp4"), &scratch).expect("copy the fixture"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); let first = session.meta().frame_count; session.import(&scratch).expect("import the copy"); std::fs::remove_file(&scratch).expect("unlink"); @@ -816,7 +832,7 @@ fn a_vanished_source_is_skipped() { #[test] fn import_during_play_does_not_stall() { let (av, av2) = (asset("test_av.mp4"), asset("test_av2.mp4")); - let mut session = PlaybackSession::open(&av).expect("open"); + let mut session = open(&av); let first = session.meta().frame_count; let mut last_index = None; @@ -846,7 +862,7 @@ fn import_during_play_does_not_stall() { #[test] fn undo_after_import_is_one_step() { let av2 = asset("test_av2.mp4"); - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); session.import(&av2).expect("import"); assert_eq!(session.clip_spans().len(), 2); assert!((session.timeline_duration() - 9.0).abs() < 1e-9); @@ -882,7 +898,7 @@ fn undo_after_import_is_one_step() { #[test] #[ignore = "needs a running PipeWire daemon"] fn audio_runs_across_a_source_join() { - let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open"); + let mut session = open(asset("test_av.mp4")); session.import(&asset("test_av2.mp4")).expect("import"); let first = session.meta().frame_count;