sew-call-client 0.9.6

Sew's native voice engine: LiveKit-protocol signalling and reconnect against Sew's media server, str0m transport with TURN over UDP, TCP and TLS (relay-only when asked), MLS call keys with the mandatory E2EE checks over the call credential and the call socket, the device store, and the kessel-audio bridge.
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
//! The seam to kessel-audio's capture hook: the trait the stage is handed over as, putting it
//! into the processing config, and reading the pipeline's bypass state.
//!
//! What kessel-audio does with the stage:
//! - it runs between echo cancellation and gain control, once per 10 ms block on the processing
//!   thread; `reset` runs there once before the first `process` (so the stage's first block
//!   allocates nothing), and again on a stream restart, after a capture gap of more than a
//!   second and before a retry;
//! - two calls in a row of 9 ms or more, or a 99th percentile over 6 ms across the last 100
//!   calls, is a strike (one late call alone never is): the stage is bypassed (the echo
//!   canceller's output goes on at the stage's latency) and retried after 10 s of capture; it is
//!   bypassed for good only after 3 strikes within 60 s of capture (older strikes decay); a panic
//!   or a non-finite sample is a fault, bypassed for good;
//! - while it is bypassed, kessel-audio's own noise suppression (Moderate, the default fallback
//!   kept here) stands in, cross-faded over one block;
//! - `EncodedFrame::ts48k` does not include the stage's latency: the audio in a frame is
//!   `latency_samples` (480, 10 ms) older than its stamp;
//! - for a grace window after a busy moment of the device (the stage starting, a capture
//!   stream starting or restarting, the phone's audio session activating, a route change, and
//!   each `TransientEvent` reported through `AudioEngine::transient_event`), slow calls are
//!   timed but do not count toward a strike; a fault is still final at once. The window is as
//!   long as the stage's own (`sew_noise::DEFAULT_GRACE`), so the pipeline and the stage hold
//!   their timing rules back for the same moment;
//! - the calls are timed on the processing thread's CPU time where the platform has it (the
//!   default clock, kept here), so time the device spends on other threads does not count
//!   against the stage; a call long enough on the wall clock (the default starvation cutoff)
//!   still counts as slow whatever its CPU time.

use kessel_audio::{
    AudioEngine, DspSchedConfig, DspSchedFallback, DspSchedMode, DspScheduling, DspTimeConstraint,
    ProcessingConfig, StageBypassReason, StageClock, TransientEvent,
};

use super::{
    CaptureDspSched, CaptureDspSchedFallback, CaptureDspSchedMode, CaptureDspScheduling,
    CaptureStageClock, CaptureTransient,
};

pub(crate) use kessel_audio::CaptureStage;

/// Whether this build's kessel-audio has the capture hook.
pub(crate) const HOOKED: bool = true;

/// Why the pipeline bypasses the stage now.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Bypass {
    /// Over its time budget.
    Budget,
    /// A panic or non-finite output.
    Fault,
}

/// The pipeline's grace window for the stage: the stage's own (`sew_noise::DEFAULT_GRACE`).
pub(crate) const STAGE_GRACE: std::time::Duration = sew_noise::DEFAULT_GRACE;

/// Hands `stage` to the pipeline, with kessel-audio's own noise suppression off (its fallback
/// for a bypassed stage stays at the default) and its grace window for the stage as long as
/// the stage's own ([`STAGE_GRACE`]). `true`: the stage is set.
pub(crate) fn install(processing: &mut ProcessingConfig, stage: Box<dyn CaptureStage>) -> bool {
    processing.noise_suppression = None;
    processing.capture_stage = Some(stage);
    processing.stage_grace = STAGE_GRACE;
    true
}

/// Whether this build's kessel-audio takes a scheduling request for its processing thread.
pub(crate) const DSP_SCHED: bool = true;

/// kessel-audio's mode for ours (`None`: `unsupported`, which is only ever reported).
fn sched_mode(m: CaptureDspSchedMode) -> Option<DspSchedMode> {
    match m {
        CaptureDspSchedMode::Workgroup => Some(DspSchedMode::Workgroup),
        CaptureDspSchedMode::TimeConstraintOnly => Some(DspSchedMode::TimeConstraintOnly),
        CaptureDspSchedMode::None => Some(DspSchedMode::None),
        CaptureDspSchedMode::OwnWorkgroup => Some(DspSchedMode::OwnWorkgroup),
        CaptureDspSchedMode::Unsupported => None,
    }
}

/// Ours for kessel-audio's mode (a mode it adds later is `None`).
fn capture_sched_mode(m: DspSchedMode) -> Option<CaptureDspSchedMode> {
    match m {
        DspSchedMode::Workgroup => Some(CaptureDspSchedMode::Workgroup),
        DspSchedMode::TimeConstraintOnly => Some(CaptureDspSchedMode::TimeConstraintOnly),
        DspSchedMode::None => Some(CaptureDspSchedMode::None),
        DspSchedMode::OwnWorkgroup => Some(CaptureDspSchedMode::OwnWorkgroup),
        DspSchedMode::Unsupported => Some(CaptureDspSchedMode::Unsupported),
        _ => None,
    }
}

/// Ours for kessel-audio's fallback reason (one it adds later is `Other`).
fn sched_fallback(f: DspSchedFallback) -> CaptureDspSchedFallback {
    match f {
        DspSchedFallback::TimeConstraintRefused => CaptureDspSchedFallback::TimeConstraintRefused,
        DspSchedFallback::OwnWorkgroupUnavailable => {
            CaptureDspSchedFallback::OwnWorkgroupUnavailable
        }
        DspSchedFallback::OwnWorkgroupJoinRefused => {
            CaptureDspSchedFallback::OwnWorkgroupJoinRefused
        }
        DspSchedFallback::IntervalRefused => CaptureDspSchedFallback::IntervalRefused,
        _ => CaptureDspSchedFallback::Other,
    }
}

fn micros(us: u32) -> std::time::Duration {
    std::time::Duration::from_micros(u64::from(us))
}

fn as_micros(d: std::time::Duration) -> u32 {
    u32::try_from(d.as_micros()).unwrap_or(u32::MAX)
}

/// kessel-audio's scheduling config for `sched` (`None`: its mode cannot be asked for).
fn sched_config(sched: CaptureDspSched) -> Option<DspSchedConfig> {
    Some(
        DspSchedConfig::new(sched_mode(sched.mode)?)
            .with_time_constraint(DspTimeConstraint::new(
                micros(sched.period_us),
                micros(sched.computation_us),
                micros(sched.constraint_us),
            ))
            .with_interval_deadline(micros(sched.interval_deadline_us)),
    )
}

/// Puts `sched` into the engine's config, for the processing thread to read as it starts.
/// `false`: its mode cannot be asked for, and `cfg` is unchanged.
pub(crate) fn apply_dsp_sched(
    cfg: &mut kessel_audio::EngineConfig,
    sched: CaptureDspSched,
) -> bool {
    let Some(config) = sched_config(sched) else {
        return false;
    };
    cfg.dsp_sched = config;
    true
}

/// kessel-audio's view of its processing thread's scheduling request.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct DspSchedState {
    /// The mode asked for, and the mode in effect.
    pub(crate) requested: CaptureDspSchedMode,
    pub(crate) effective: CaptureDspSchedMode,
    /// Why they differ.
    pub(crate) fallback: Option<CaptureDspSchedFallback>,
    /// The time-constraint figures asked for: period, computation, constraint, microseconds.
    pub(crate) time_constraint_us: Option<(u32, u32, u32)>,
    /// The work interval's deadline, microseconds.
    pub(crate) interval_deadline_us: Option<u32>,
    /// Work intervals finished.
    pub(crate) intervals: u64,
}

/// The engine's scheduling request and what came of it (`None`: a mode this version does not
/// know).
pub(crate) fn dsp_sched(engine: &AudioEngine) -> Option<DspSchedState> {
    let s = engine.stats().dsp_sched;
    Some(DspSchedState {
        requested: capture_sched_mode(s.mode_requested)?,
        effective: capture_sched_mode(s.mode_effective)?,
        fallback: s.fallback_reason.map(sched_fallback),
        time_constraint_us: s.time_constraint.map(|t| {
            (
                as_micros(t.period),
                as_micros(t.computation),
                as_micros(t.constraint),
            )
        }),
        interval_deadline_us: s.interval_deadline.map(as_micros),
        intervals: s.intervals,
    })
}

/// kessel-audio's name for a busy moment.
pub(crate) fn transient_event(t: CaptureTransient) -> TransientEvent {
    match t {
        CaptureTransient::CallStart => TransientEvent::CallStart,
        CaptureTransient::AudioSessionActivated => TransientEvent::AudioSessionActivated,
        CaptureTransient::RouteChange => TransientEvent::RouteChange,
        CaptureTransient::CameraStart => TransientEvent::CameraStart,
        CaptureTransient::AppSceneChange => TransientEvent::AppSceneChange,
        CaptureTransient::Other => TransientEvent::Other,
    }
}

/// Ours for kessel-audio's clock (a clock it adds later is `None`).
fn stage_clock(c: StageClock) -> Option<CaptureStageClock> {
    match c {
        StageClock::ThreadCpu => Some(CaptureStageClock::ThreadCpu),
        StageClock::Wall => Some(CaptureStageClock::Wall),
        _ => None,
    }
}

/// Ours for kessel-audio's processing thread scheduling (a mode it adds later is `None`).
fn dsp_scheduling(d: DspScheduling) -> Option<CaptureDspScheduling> {
    match d {
        DspScheduling::Workgroup => Some(CaptureDspScheduling::Workgroup),
        DspScheduling::TimeConstraint => Some(CaptureDspScheduling::TimeConstraint),
        DspScheduling::Qos => Some(CaptureDspScheduling::Qos),
        DspScheduling::None => Some(CaptureDspScheduling::None),
        DspScheduling::OwnWorkgroup => Some(CaptureDspScheduling::OwnWorkgroup),
        _ => None,
    }
}

/// Ours for kessel-audio's (a kind it adds later is `Other`).
fn capture_transient(t: TransientEvent) -> CaptureTransient {
    match t {
        TransientEvent::CallStart => CaptureTransient::CallStart,
        TransientEvent::AudioSessionActivated => CaptureTransient::AudioSessionActivated,
        TransientEvent::RouteChange => CaptureTransient::RouteChange,
        TransientEvent::CameraStart => CaptureTransient::CameraStart,
        TransientEvent::AppSceneChange => CaptureTransient::AppSceneChange,
        _ => CaptureTransient::Other,
    }
}

/// kessel-audio's view of the stage, for the call's log.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PipelineStage {
    /// Budget strikes and faults so far.
    pub(crate) strikes: u32,
    /// Why it was last bypassed (sticky: kept after a retry).
    pub(crate) bypassed: Option<Bypass>,
    /// The 99th percentile of the stage's last 100 calls, microseconds.
    pub(crate) p99_us: u32,
    /// A grace window is open now.
    pub(crate) grace: bool,
    /// Blocks processed inside grace windows so far.
    pub(crate) grace_blocks: u64,
    /// Of those, the calls over the budget that a window excused.
    pub(crate) grace_over_budget: u64,
    /// The last busy moment reported.
    pub(crate) last_transient: Option<CaptureTransient>,
    /// The clock timing the calls.
    pub(crate) clock: Option<CaptureStageClock>,
    /// The longest call on the wall clock, microseconds.
    pub(crate) max_wall_us: u32,
    /// Calls over the budget on the wall clock but not in CPU time.
    pub(crate) preempted_over_budget: u64,
    /// Calls past the starvation cutoff.
    pub(crate) starved: u64,
    /// How the OS schedules the processing thread the stage runs on.
    pub(crate) dsp_scheduling: Option<CaptureDspScheduling>,
}

/// kessel-audio's stage counters, when a stage is set.
pub(crate) fn pipeline_stage(engine: &AudioEngine) -> Option<PipelineStage> {
    let st = engine.stats().capture_stage?;
    Some(PipelineStage {
        strikes: st.strikes,
        bypassed: st.bypassed.map(bypass_of),
        p99_us: st.p99_block_us,
        grace: st.grace,
        grace_blocks: st.grace_blocks,
        grace_over_budget: st.grace_over_budget,
        last_transient: st.last_transient.map(capture_transient),
        clock: stage_clock(st.clock),
        max_wall_us: st.max_wall_us,
        preempted_over_budget: st.preempted_over_budget,
        starved: st.starved,
        dsp_scheduling: dsp_scheduling(st.dsp_scheduling),
    })
}

fn bypass_of(r: StageBypassReason) -> Bypass {
    match r {
        StageBypassReason::Budget => Bypass::Budget,
        _ => Bypass::Fault,
    }
}

/// Why the stage is bypassed now, if it is: kessel-audio's sticky reason while the stage's
/// output is not what goes on. A stage brought back by a retry is not bypassed (its reason
/// stays in kessel-audio's stats); one warming up after a retry still is.
pub(crate) fn bypass(engine: &AudioEngine) -> Option<Bypass> {
    let st = engine.stats().capture_stage?;
    if st.active {
        return None;
    }
    st.bypassed.map(bypass_of)
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Each busy moment maps to kessel-audio's own kind and back.
    #[test]
    fn transients_round_trip() {
        for t in [
            CaptureTransient::CallStart,
            CaptureTransient::AudioSessionActivated,
            CaptureTransient::RouteChange,
            CaptureTransient::CameraStart,
            CaptureTransient::AppSceneChange,
            CaptureTransient::Other,
        ] {
            assert_eq!(capture_transient(transient_event(t)), t);
        }
    }

    struct Pass;

    impl CaptureStage for Pass {
        fn process(&mut self, _block: &mut [f32; 480]) {}
        fn latency_samples(&self) -> usize {
            0
        }
        fn reset(&mut self) {}
    }

    /// The pipeline's grace window is the stage's own, and one the pipeline accepts.
    #[test]
    fn install_sets_the_stages_grace() {
        assert_eq!(STAGE_GRACE, sew_noise::DEFAULT_GRACE);
        assert!(STAGE_GRACE <= kessel_audio::MAX_STAGE_GRACE);
        assert!(STAGE_GRACE >= kessel_audio::DEFAULT_STAGE_GRACE);
        let mut p = ProcessingConfig::default();
        assert!(install(&mut p, Box::new(Pass)));
        assert_eq!(p.stage_grace, STAGE_GRACE);
        // The default clock and starvation cutoff are kept.
        let d = ProcessingConfig::default();
        assert_eq!(p.stage_clock, d.stage_clock);
        assert_eq!(p.stage_starvation_cutoff, d.stage_starvation_cutoff);
        assert_eq!(
            stage_clock(StageClock::default()),
            Some(if StageClock::ThreadCpu.is_supported() {
                CaptureStageClock::ThreadCpu
            } else {
                CaptureStageClock::Wall
            })
        );
        assert!(p.capture_stage.is_some());
        assert!(p.noise_suppression.is_none());
    }

    /// Each scheduling mode maps to kessel-audio's of the same name and back; `unsupported` is
    /// only reported; each fallback reason keeps its name; the default setting is
    /// kessel-audio's default, figure for figure.
    #[test]
    fn dsp_sched_maps_each_mode_and_the_defaults() {
        for (ours, k) in [
            (CaptureDspSchedMode::Workgroup, DspSchedMode::Workgroup),
            (
                CaptureDspSchedMode::TimeConstraintOnly,
                DspSchedMode::TimeConstraintOnly,
            ),
            (CaptureDspSchedMode::None, DspSchedMode::None),
            (
                CaptureDspSchedMode::OwnWorkgroup,
                DspSchedMode::OwnWorkgroup,
            ),
        ] {
            assert_eq!(sched_mode(ours), Some(k));
            assert_eq!(capture_sched_mode(k), Some(ours));
            assert_eq!(ours.as_str(), k.as_str());
        }
        assert_eq!(sched_mode(CaptureDspSchedMode::Unsupported), None);
        assert_eq!(
            capture_sched_mode(DspSchedMode::Unsupported),
            Some(CaptureDspSchedMode::Unsupported)
        );
        assert_eq!(
            CaptureDspSchedMode::Unsupported.as_str(),
            DspSchedMode::Unsupported.as_str()
        );
        for (k, ours, name) in [
            (
                DspSchedFallback::TimeConstraintRefused,
                CaptureDspSchedFallback::TimeConstraintRefused,
                "time_constraint_refused",
            ),
            (
                DspSchedFallback::OwnWorkgroupUnavailable,
                CaptureDspSchedFallback::OwnWorkgroupUnavailable,
                "own_workgroup_unavailable",
            ),
            (
                DspSchedFallback::OwnWorkgroupJoinRefused,
                CaptureDspSchedFallback::OwnWorkgroupJoinRefused,
                "own_workgroup_join_refused",
            ),
            (
                DspSchedFallback::IntervalRefused,
                CaptureDspSchedFallback::IntervalRefused,
                "interval_refused",
            ),
        ] {
            assert_eq!(sched_fallback(k), ours);
            assert_eq!(k.as_str(), name);
            assert_eq!(serde_json::to_value(ours).ok(), Some(name.into()));
        }
        assert_eq!(
            sched_config(CaptureDspSched::default()),
            Some(DspSchedConfig::default())
        );
        let own = CaptureDspSched::new(CaptureDspSchedMode::OwnWorkgroup)
            .with_time_constraint_us(10_000, 3_000, 5_000)
            .with_interval_deadline_us(4_000);
        let mut cfg = kessel_audio::EngineConfig::default();
        assert!(apply_dsp_sched(&mut cfg, own));
        assert_eq!(cfg.dsp_sched.mode, DspSchedMode::OwnWorkgroup);
        assert_eq!(cfg.dsp_sched.time_constraint.period, micros(10_000));
        assert_eq!(cfg.dsp_sched.time_constraint.computation, micros(3_000));
        assert_eq!(cfg.dsp_sched.time_constraint.constraint, micros(5_000));
        assert_eq!(cfg.dsp_sched.interval_deadline, micros(4_000));
        let before = cfg.dsp_sched;
        assert!(!apply_dsp_sched(
            &mut cfg,
            CaptureDspSched::new(CaptureDspSchedMode::Unsupported)
        ));
        assert_eq!(cfg.dsp_sched, before);
    }

    /// kessel-audio's processing thread scheduling maps one to one, its default to `none`.
    #[test]
    fn dsp_scheduling_maps_each_mode() {
        for (k, ours) in [
            (DspScheduling::Workgroup, CaptureDspScheduling::Workgroup),
            (
                DspScheduling::TimeConstraint,
                CaptureDspScheduling::TimeConstraint,
            ),
            (DspScheduling::Qos, CaptureDspScheduling::Qos),
            (DspScheduling::None, CaptureDspScheduling::None),
            (
                DspScheduling::OwnWorkgroup,
                CaptureDspScheduling::OwnWorkgroup,
            ),
        ] {
            assert_eq!(dsp_scheduling(k), Some(ours));
            assert_eq!(k.as_str(), ours.as_str());
            assert_eq!(
                serde_json::to_value(ours).ok(),
                Some(k.as_str().into()),
                "the JSON name is kessel-audio's name"
            );
        }
        assert_eq!(
            dsp_scheduling(DspScheduling::default()),
            Some(CaptureDspScheduling::None)
        );
    }
}