diff --git a/crates/voelin-core/src/media.rs b/crates/voelin-core/src/media.rs index dc2a395..8f1f10e 100644 --- a/crates/voelin-core/src/media.rs +++ b/crates/voelin-core/src/media.rs @@ -4180,7 +4180,8 @@ mod tests { }; let streamer = Streamer::start(&codecs, config).await.unwrap(); let wait = |what: &str, done: &dyn Fn(&StreamerStats) -> bool| { - let deadline = Instant::now() + Duration::from_secs(5); + // What is waited for is a state; the deadline only ends a hang. + let deadline = Instant::now() + Duration::from_secs(30); loop { let stats = streamer.stats(); if done(&stats) { @@ -4190,15 +4191,18 @@ mod tests { std::thread::sleep(Duration::from_millis(20)); } }; - // The SineSource tone is 0.05; the second at half gain. + // The SineSource tone is 0.05; the second at half gain. The sources are metered + // before the mixer has made anything of them: the mix and the state of the + // sources are waited for too, not taken to be there with the meters. let near = |level: f32, want: f32| (level - want).abs() < want * 0.1; - let stats = wait("both tones", &|s| { + let stats = wait("both tones, mixed", &|s| { s.audio_sources.len() == 2 && near(s.audio_sources[0].level.peak, 0.05) && near(s.audio_sources[1].level.peak, 0.025) + && s.audio_sources.iter().all(|s| s.state == SourceState::Playing) + && s.audio_level.peak > 0.05 + && s.audio_limiter == 1.0 }); - assert!(stats.audio_sources.iter().all(|s| s.state == SourceState::Playing)); - assert!(stats.audio_level.peak > 0.05 && stats.audio_limiter == 1.0, "{stats:?}"); let first = stats.audio_sources[0].id; // Mute the first (kept: same id), drop the second, add the @@ -4214,19 +4218,31 @@ mod tests { streamer.reconfigure(&codecs, update).unwrap(); assert_eq!(streamer.audio_sources().len(), 3); let publisher = tap::publisher_id(); - let speak = std::thread::spawn(move || { - // 20 ms of a 0.2 tone every 20 ms, for a second. - let chunk: Vec = (0..960).map(|i| 0.2 * (i as f32 * 0.1).sin()).collect(); - for _ in 0..50 { - tap::microphone().publish(publisher, &chunk); - std::thread::sleep(Duration::from_millis(20)); + let heard = Arc::new(AtomicBool::new(false)); + let speak = std::thread::spawn({ + let heard = heard.clone(); + move || { + // 20 ms of a 0.2 tone every 20 ms, until the test has heard it: the + // source takes what is published only from when it runs, which a + // busy machine delays by any time (the cap ends a thread whose test + // failed first). + let cap = Instant::now() + Duration::from_secs(120); + let chunk: Vec = (0..960).map(|i| 0.2 * (i as f32 * 0.1).sin()).collect(); + while !heard.load(Ordering::Relaxed) && Instant::now() < cap { + tap::microphone().publish(publisher, &chunk); + std::thread::sleep(Duration::from_millis(20)); + } } }); let stats = wait("microphone in, tone muted", &|s| { s.audio_sources.len() == 3 && s.audio_sources[1].level.peak > 0.15 && s.audio_level.peak > 0.15 + && s.audio_sources[2].error.as_deref().is_some_and(|e| e.contains("no window")) + // Muted, and still metered: the tone's own level is read again. + && near(s.audio_sources[0].level.peak, 0.05) }); + heard.store(true, Ordering::Relaxed); speak.join().unwrap(); assert_eq!(stats.audio_sources[0].id, first, "the kept tone keeps its source"); assert!(stats.audio_sources[0].spec.muted);