kessel-audio 0.2.0

A real-time voice audio pipeline in pure Rust: devices, echo cancellation, noise suppression and gain control, Opus, jitter buffers with loss concealment, mixing and levels.
Documentation
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! The phone backend's state machine on a fake OS: deferred start, session
//! activate and deactivate, suspend and resume, route changes, faults and
//! retries. The state machine tests drive it directly; the engine tests run
//! it on the engine's real threads with a fake device that plays a clock.

use super::{check_selection, Ends, Faults, Opened, OsEffects, Phone, PhoneIo};
use crate::apm::{ApmSettings, ProcessingConfig};
use crate::config::{Backend, EngineConfig};
use crate::device::NullDevice;
use crate::engine::{lock, AudioEngine, Ctrl, PhoneCmd, Shared};
use crate::error::{AudioError, ErrorCode};
use crate::events::{DeviceChoice, DeviceSelection, Event};
use crate::frame::EncoderCfg;
use crate::pipeline::Pause;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};

/// `Backend::TestPhone`'s payload: the fake device and what the "OS" does.
pub(crate) struct FakeIo(pub(crate) Box<dyn PhoneIo>, pub(crate) OsEffects);

impl std::fmt::Debug for FakeIo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("FakeIo")
            .field(&self.1)
            .finish_non_exhaustive()
    }
}

struct State {
    rate: u32,
    mic: bool,
    fail: Option<ErrorCode>,
    /// Drive the rings from a thread at `rate`, in real time.
    clock: bool,
    log: Vec<String>,
    faults: Option<Faults>,
}

/// The scripted OS. Clones share it.
#[derive(Clone)]
pub(crate) struct FakeOs(Arc<Mutex<State>>);

impl FakeOs {
    pub(crate) fn new(rate: u32) -> FakeOs {
        FakeOs(Arc::new(Mutex::new(State {
            rate,
            mic: true,
            fail: None,
            clock: false,
            log: Vec::new(),
            faults: None,
        })))
    }
    fn clocked(self) -> FakeOs {
        lock(&self.0).clock = true;
        self
    }
    fn set_rate(&self, r: u32) {
        lock(&self.0).rate = r;
    }
    fn set_mic(&self, on: bool) {
        lock(&self.0).mic = on;
    }
    fn fail(&self, code: Option<ErrorCode>) {
        lock(&self.0).fail = code;
    }
    /// What happened since the last call.
    fn log(&self) -> Vec<String> {
        std::mem::take(&mut lock(&self.0).log)
    }
    /// The open stream's device thread reports that it died.
    fn raise_fault(&self) {
        lock(&self.0)
            .faults
            .as_ref()
            .expect("a stream was opened")
            .raise();
    }
    fn io(&self) -> Box<dyn PhoneIo> {
        Box::new(FakeDev(self.clone()))
    }
}

struct FakeDev(FakeOs);

/// The open fake stream; dropping it stops its clock and logs the close.
struct Stream {
    os: FakeOs,
    stop: Arc<AtomicBool>,
    thread: Option<JoinHandle<()>>,
    _ends: Option<(rtrb::Producer<f32>, rtrb::Consumer<f32>)>,
}

impl Drop for Stream {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Release);
        if let Some(t) = self.thread.take() {
            t.join().expect("fake clock thread");
        }
        lock(&self.os.0).log.push("close".into());
    }
}

impl PhoneIo for FakeDev {
    fn current_rate(&self) -> Option<u32> {
        Some(lock(&self.0 .0).rate)
    }

    fn open(&mut self, ends: Ends) -> Result<Opened, AudioError> {
        let mut st = lock(&self.0 .0);
        let rate = st.rate;
        if let Some(code) = st.fail {
            st.log.push(format!("open {rate} failed"));
            return Err(AudioError::new(code, "scripted failure"));
        }
        st.log.push(format!("open {rate}"));
        st.faults = Some(ends.faults.clone());
        let mic = st.mic;
        let clock = st.clock;
        drop(st);
        let stop = Arc::new(AtomicBool::new(false));
        let (thread, kept) = if clock {
            let Ends {
                mut capture,
                mut playback,
                shared,
                ..
            } = ends;
            let stop = stop.clone();
            let t = std::thread::spawn(move || {
                // A 300 Hz tone at `rate`, 10 ms at a time, in real time.
                let block = (rate / 100) as usize;
                let mut n = 0u64;
                let mut out = vec![0.0f32; block];
                let mut inb = vec![0.0f32; block];
                let mut next = Instant::now();
                while !stop.load(Ordering::Acquire) {
                    for v in inb.iter_mut() {
                        *v = 0.3
                            * (2.0 * std::f32::consts::PI * 300.0 * n as f32 / rate as f32).sin();
                        n += 1;
                    }
                    if mic {
                        crate::device::push_capture(&mut capture, &inb, 1, |v| v, &shared.io);
                    }
                    crate::device::pop_playback(&mut playback, &mut out, 1, |v| v, &shared.io);
                    next += Duration::from_millis(10);
                    if let Some(d) = next.checked_duration_since(Instant::now()) {
                        std::thread::sleep(d);
                    }
                }
            });
            (Some(t), None)
        } else {
            (None, Some((ends.capture, ends.playback)))
        };
        Ok(Opened {
            stream: Box::new(Stream {
                os: self.0.clone(),
                stop,
                thread,
                _ends: kept,
            }),
            capture_rate: mic.then_some(rate),
            playback_rate: rate,
            name: "Fake",
            warnings: if mic {
                Vec::new()
            } else {
                vec![AudioError::new(ErrorCode::NoPermission, "scripted refusal")]
            },
        })
    }
}

// ---- The state machine, driven directly ----

fn shared() -> Arc<Shared> {
    Shared::new(&EngineConfig::null(NullDevice::default()))
}

/// The control messages sent to the processing thread, as words.
fn ctrl(shared: &Shared) -> Vec<String> {
    let mut out = Vec::new();
    while let Some(c) = shared.ctrl.pop() {
        out.push(match c {
            Ctrl::CaptureIo(Some(_)) => "capture".into(),
            Ctrl::CaptureIo(None) => "capture off".into(),
            Ctrl::PlayIo(Some(_)) => "play".into(),
            Ctrl::PlayIo(None) => "play off".into(),
            Ctrl::Pause { why, on } => {
                assert_eq!(why, Pause::Device, "the backend only sends device pauses");
                (if on { "pause" } else { "unpause" }).into()
            }
            _ => panic!("unexpected control message"),
        });
    }
    out
}

fn errors(shared: &Shared) -> Vec<ErrorCode> {
    let mut out = Vec::new();
    while let Some(e) = shared.events.pop() {
        if let Event::Error(e) = e {
            out.push(e.code);
        }
    }
    out
}

fn in_use(shared: &Shared) -> (Option<String>, Option<String>) {
    let u = lock(&shared.in_use).clone();
    (u.input.map(|d| d.name), u.output.map(|d| d.name))
}

const OPENED: [&str; 3] = ["capture", "play", "unpause"];
const CLOSED: [&str; 3] = ["pause", "capture off", "play off"];

fn rig(session_active: bool) -> (Arc<Shared>, FakeOs, Phone) {
    let s = shared();
    let os = FakeOs::new(48_000);
    let p = Phone::start(&s, os.io(), session_active);
    (s, os, p)
}

#[test]
fn a_deferred_start_opens_only_when_the_session_is_active() {
    let (s, os, mut p) = rig(false);
    assert_eq!(
        os.log(),
        Vec::<String>::new(),
        "nothing opens before the session"
    );
    assert_eq!(ctrl(&s), ["pause"], "the capture path waits for a device");
    assert_eq!(in_use(&s), (None, None));
    p.tick(&s);
    assert_eq!(os.log(), Vec::<String>::new());

    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), ["open 48000"]);
    assert_eq!(ctrl(&s), OPENED);
    assert_eq!(
        in_use(&s),
        (
            Some("Fake input (48000 Hz)".into()),
            Some("Fake output (48000 Hz)".into())
        )
    );
    assert_eq!(errors(&s), []);
}

#[test]
fn an_immediate_start_opens_at_once() {
    let (s, os, _p) = rig(true);
    assert_eq!(os.log(), ["open 48000"]);
    let mut want = vec!["pause"];
    want.extend(OPENED);
    assert_eq!(ctrl(&s), want);
}

#[test]
fn deactivating_closes_and_activating_reopens() {
    let (s, os, mut p) = rig(true);
    os.log();
    ctrl(&s);
    p.command(&s, PhoneCmd::Session(false));
    assert_eq!(os.log(), ["close"]);
    assert_eq!(ctrl(&s), CLOSED);
    assert_eq!(in_use(&s), (None, None));
    p.command(&s, PhoneCmd::Session(false));
    assert_eq!(os.log(), Vec::<String>::new(), "deactivating twice is once");
    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), ["open 48000"]);
    assert_eq!(ctrl(&s), OPENED);
    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), Vec::<String>::new(), "activating twice is once");
}

#[test]
fn suspend_closes_and_resume_reopens_only_with_the_session_active() {
    let (s, os, mut p) = rig(true);
    os.log();
    ctrl(&s);
    p.command(&s, PhoneCmd::Suspend(true));
    assert_eq!(os.log(), ["close"]);
    assert_eq!(ctrl(&s), CLOSED);
    // A cellular call: CallKit also deactivates the session around it.
    p.command(&s, PhoneCmd::Session(false));
    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), Vec::<String>::new(), "still suspended");
    p.command(&s, PhoneCmd::Suspend(false));
    assert_eq!(os.log(), ["open 48000"]);
    assert_eq!(ctrl(&s), OPENED);

    // Resumed before the session comes back: opens with the session.
    p.command(&s, PhoneCmd::Suspend(true));
    p.command(&s, PhoneCmd::Session(false));
    p.command(&s, PhoneCmd::Suspend(false));
    assert_eq!(os.log(), ["close"]);
    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), ["open 48000"]);
}

#[test]
fn a_route_change_reopens_at_the_new_rate_mid_call() {
    let (s, os, mut p) = rig(true);
    os.log();
    ctrl(&s);
    // Same rate (headphones in): nothing to do.
    p.command(&s, PhoneCmd::RouteChanged);
    assert_eq!(os.log(), Vec::<String>::new());
    assert_eq!(ctrl(&s), Vec::<String>::new());
    // Bluetooth hands-free: 16 kHz.
    os.set_rate(16_000);
    p.command(&s, PhoneCmd::RouteChanged);
    assert_eq!(os.log(), ["close", "open 16000"]);
    let mut want = CLOSED.to_vec();
    want.extend(OPENED);
    assert_eq!(ctrl(&s), want);
    assert_eq!(
        in_use(&s),
        (
            Some("Fake input (16000 Hz)".into()),
            Some("Fake output (16000 Hz)".into())
        )
    );
    assert_eq!(errors(&s), [], "a route change is not an error");
}

#[test]
fn a_route_change_while_closed_opens_nothing() {
    let (s, os, mut p) = rig(false);
    ctrl(&s);
    os.set_rate(16_000);
    p.command(&s, PhoneCmd::RouteChanged);
    assert_eq!(os.log(), Vec::<String>::new());
    p.command(&s, PhoneCmd::Session(true));
    assert_eq!(os.log(), ["open 16000"]);
}

#[test]
fn a_fault_from_the_open_stream_reopens_and_a_stale_one_is_ignored() {
    let (s, os, mut p) = rig(true);
    os.log();
    ctrl(&s);
    let stale = lock(&os.0).faults.clone().expect("faults");
    os.raise_fault();
    p.tick(&s);
    assert_eq!(os.log(), ["close", "open 48000"]);
    assert_eq!(errors(&s), []);
    stale.raise();
    p.tick(&s);
    assert_eq!(
        os.log(),
        Vec::<String>::new(),
        "a fault from a closed stream"
    );
}

#[test]
fn a_failed_open_raises_once_and_is_retried() {
    let s = shared();
    let os = FakeOs::new(48_000);
    os.fail(Some(ErrorCode::DeviceBusy));
    let mut p = Phone::start(&s, os.io(), true);
    assert_eq!(os.log(), ["open 48000 failed"]);
    assert_eq!(errors(&s), [ErrorCode::DeviceBusy]);
    assert_eq!(ctrl(&s), ["pause"]);
    p.tick(&s);
    assert_eq!(os.log(), Vec::<String>::new(), "not before the retry time");

    std::thread::sleep(super::RETRY + Duration::from_millis(50));
    p.tick(&s);
    assert_eq!(os.log(), ["open 48000 failed"]);
    assert_eq!(errors(&s), [], "the same failure is raised once");

    // The app's next command tries at once.
    os.fail(None);
    p.command(&s, PhoneCmd::RouteChanged);
    assert_eq!(os.log(), ["open 48000"]);
    assert_eq!(ctrl(&s), OPENED);
    assert_eq!(errors(&s), []);
}

#[test]
fn a_device_rate_out_of_range_is_refused_and_closed() {
    let s = shared();
    let os = FakeOs::new(192_000);
    let _p = Phone::start(&s, os.io(), true);
    assert_eq!(os.log(), ["open 192000", "close"]);
    assert_eq!(errors(&s), [ErrorCode::UnsupportedConfig]);
    assert_eq!(ctrl(&s), ["pause"]);
}

#[test]
fn no_microphone_plays_and_keeps_the_capture_path_paused() {
    let s = shared();
    let os = FakeOs::new(48_000);
    os.set_mic(false);
    let _p = Phone::start(&s, os.io(), true);
    assert_eq!(os.log(), ["open 48000"]);
    assert_eq!(ctrl(&s), ["pause", "play"]);
    assert_eq!(errors(&s), [ErrorCode::NoPermission]);
    assert_eq!(in_use(&s), (None, Some("Fake output (48000 Hz)".into())));
}

#[test]
fn the_os_picks_the_route() {
    assert_eq!(check_selection(&DeviceSelection::default()), Ok(()));
    let sel = DeviceSelection {
        input: Some(DeviceChoice::Id(super::INPUT_ID.into())),
        output: Some(DeviceChoice::Default),
    };
    assert_eq!(check_selection(&sel), Ok(()));
    let sel = DeviceSelection {
        input: Some(DeviceChoice::Id("usb-mic".into())),
        output: None,
    };
    assert_eq!(
        check_selection(&sel).expect_err("not ours").code,
        ErrorCode::NoDevice
    );
}

#[test]
fn sonora_runs_only_what_the_os_does_not() {
    let all = ProcessingConfig::default().settings();
    let off = ProcessingConfig::off()
        .with_stage_fallback_noise_suppression(None)
        .settings();
    // The OS suppresses noise, so no stage fallback either.
    assert_eq!(OsEffects::default().remaining(all), off);
    let no_aec = OsEffects {
        echo_cancellation: false,
        ..OsEffects::default()
    };
    assert_eq!(
        no_aec.remaining(all),
        ApmSettings {
            echo_cancellation: true,
            noise_suppression: None,
            gain_control: false,
            high_pass: true,
            stage_fallback_ns: None,
        }
    );
    let none = OsEffects {
        echo_cancellation: false,
        noise_suppression: false,
        gain_control: false,
    };
    assert_eq!(none.remaining(all), all);
    // What the caller turned off stays off.
    assert_eq!(none.remaining(off), off);
}

#[test]
fn a_capture_stage_survives_the_os_effects() {
    struct Nop;
    impl crate::CaptureStage for Nop {
        fn process(&mut self, _: &mut [f32; 480]) {}
        fn latency_samples(&self) -> usize {
            0
        }
        fn reset(&mut self) {}
    }
    let cfg = EngineConfig {
        backend: Backend::TestPhone(FakeIo(FakeOs::new(48_000).io(), OsEffects::default())),
        processing: ProcessingConfig::default().with_capture_stage(Box::new(Nop)),
        ..EngineConfig::null(NullDevice::default())
    }
    .with_os_effects();
    // The OS suppresses noise, so there is no step-down floor either.
    assert_eq!(
        cfg.processing.settings(),
        ProcessingConfig::off()
            .with_stage_fallback_noise_suppression(None)
            .settings()
    );
    assert!(cfg.processing.capture_stage.is_some());
}

// ---- On the engine's threads ----

fn phone_engine(os: &FakeOs, deferred: bool) -> AudioEngine {
    let cfg = EngineConfig {
        backend: Backend::TestPhone(FakeIo(os.io(), OsEffects::default())),
        // DTX off: a frame every 20 ms from the tone.
        encoder: EncoderCfg {
            dtx: false,
            ..EncoderCfg::default()
        },
        processing_poll: Duration::from_micros(500),
        ..EngineConfig::null(NullDevice::default())
    };
    if deferred {
        AudioEngine::start_when_session_active(cfg).expect("start")
    } else {
        AudioEngine::start(cfg).expect("start")
    }
}

/// Wait up to `within` for `n` frames; returns their timestamps.
fn frames(e: &AudioEngine, n: usize, within: Duration) -> Vec<u32> {
    let rx = e.capture_frames();
    let deadline = Instant::now() + within;
    let mut ts = Vec::new();
    while ts.len() < n {
        let left = deadline.saturating_duration_since(Instant::now());
        assert!(
            !left.is_zero(),
            "only {} of {n} frames in {within:?}",
            ts.len()
        );
        if let Some(f) = rx.recv_timeout(left) {
            ts.push(f.ts48k);
        }
    }
    ts
}

fn drain(e: &AudioEngine) -> Vec<u32> {
    let rx = e.capture_frames();
    std::iter::from_fn(|| rx.try_recv().map(|f| f.ts48k)).collect()
}

fn wait_for(what: &str, within: Duration, mut f: impl FnMut() -> bool) {
    let deadline = Instant::now() + within;
    while !f() {
        assert!(Instant::now() < deadline, "timed out waiting for {what}");
        std::thread::sleep(Duration::from_millis(5));
    }
}

fn steps(ts: &[u32]) -> Vec<u32> {
    ts.windows(2).map(|w| w[1].wrapping_sub(w[0])).collect()
}

#[test]
fn engine_defers_until_the_session_is_active_and_survives_a_route_change() {
    let os = FakeOs::new(48_000).clocked();
    let e = phone_engine(&os, true);
    std::thread::sleep(Duration::from_millis(200));
    assert_eq!(drain(&e), Vec::<u32>::new(), "no frames before the session");
    assert_eq!(os.log(), Vec::<String>::new());

    e.session_activated();
    let first = frames(&e, 10, Duration::from_secs(2));
    assert_eq!(first[0], 0, "the first frame keeps initial_ts48k");
    assert!(steps(&first).iter().all(|&d| d == 960), "{first:?}");
    assert_eq!(os.log(), ["open 48000"]);

    // Bluetooth hands-free mid-call: the device reopens at 16 kHz and the
    // frames go on at 48 kHz.
    os.set_rate(16_000);
    e.route_changed();
    wait_for("the 16 kHz device", Duration::from_secs(2), || {
        e.devices_in_use()
            .input
            .is_some_and(|d| d.name.contains("16000"))
    });
    let mut after = drain(&e);
    after.extend(frames(&e, 25, Duration::from_secs(2)));
    let all: Vec<u32> = first.iter().chain(after.iter()).copied().collect();
    // Time only moves forward, in whole frames; the reopen is at most a
    // short gap.
    for d in steps(&all) {
        assert!(
            d >= 960 && d.is_multiple_of(960) && d <= 960 * 20,
            "step {d} in {all:?}"
        );
    }
    assert_eq!(os.log(), ["close", "open 16000"]);
    let errs: Vec<Event> = std::iter::from_fn(|| e.events().try_recv())
        .filter(|ev| matches!(ev, Event::Error(_)))
        .collect();
    assert_eq!(errs, [], "a route change is not an error");
}

#[test]
fn engine_suspend_stops_frames_and_resume_carries_the_gap() {
    let os = FakeOs::new(48_000).clocked();
    let e = phone_engine(&os, false);
    let before = frames(&e, 10, Duration::from_secs(2));
    e.suspend();
    wait_for("the device to close", Duration::from_secs(2), || {
        e.devices_in_use().input.is_none()
    });
    let suspended_at = Instant::now();
    drain(&e);
    std::thread::sleep(Duration::from_millis(500));
    assert_eq!(drain(&e), Vec::<u32>::new(), "no frames while suspended");
    e.resume();
    let after = frames(&e, 5, Duration::from_secs(2));
    let paused = suspended_at.elapsed();
    assert_eq!(os.log(), ["open 48000", "close", "open 48000"]);
    let last_before = *before.last().expect("frames");
    let jump = after[0].wrapping_sub(last_before);
    // At least the time we know it was suspended for, at most that plus a
    // second of scheduling slack.
    let lo = 500 * 48;
    let hi = (paused.as_millis() as u32 + 1000) * 48;
    assert!(
        jump >= lo && jump <= hi && jump.is_multiple_of(960),
        "jump {jump} (lo {lo}, hi {hi})"
    );
    assert!(steps(&after).iter().all(|&d| d == 960), "{after:?}");
}

#[test]
fn engine_routes_are_the_os_s_and_deferral_needs_a_phone_backend() {
    let os = FakeOs::new(48_000);
    let e = phone_engine(&os, false);
    e.set_device(DeviceSelection::default())
        .expect("the default route");
    let err = e
        .set_device(DeviceSelection {
            input: Some(DeviceChoice::Id("usb-mic".into())),
            output: None,
        })
        .expect_err("a device the OS would pick");
    assert_eq!(err.code, ErrorCode::NoDevice);
    let list = e.devices();
    assert_eq!(list.inputs.len(), 1);
    assert_eq!(list.outputs.len(), 1);

    let err = AudioEngine::start_when_session_active(EngineConfig::null(NullDevice::default()))
        .expect_err("the null device can't defer");
    assert_eq!(err.code, ErrorCode::InvalidConfig);
}

#[test]
fn engine_suspend_works_on_the_null_device_too() {
    let mut phase = 0.0f32;
    let dev = NullDevice {
        input: Some(Box::new(move |b: &mut [f32]| {
            for v in b.iter_mut() {
                *v = 0.3 * phase.sin();
                phase = (phase + 0.04) % std::f32::consts::TAU;
            }
        })),
        ..NullDevice::default()
    };
    let e = AudioEngine::start(EngineConfig {
        encoder: EncoderCfg {
            dtx: false,
            ..EncoderCfg::default()
        },
        processing: ProcessingConfig::off(),
        processing_poll: Duration::from_micros(500),
        ..EngineConfig::null(dev)
    })
    .expect("start");
    let before = frames(&e, 5, Duration::from_secs(2));
    e.suspend();
    std::thread::sleep(Duration::from_millis(100));
    let tail = drain(&e);
    std::thread::sleep(Duration::from_millis(300));
    assert_eq!(drain(&e), Vec::<u32>::new(), "no frames while suspended");
    e.resume();
    let after = frames(&e, 3, Duration::from_secs(2));
    let last = *tail.last().unwrap_or(before.last().expect("frames"));
    let jump = after[0].wrapping_sub(last);
    assert!(jump >= 300 * 48 && jump.is_multiple_of(960), "jump {jump}");
}