use serde::{Deserialize, Serialize};
#[cfg(feature = "noise-bench")]
pub mod bench;
#[cfg(feature = "audio-core")]
mod control;
#[cfg(feature = "audio-core")]
mod hook;
#[cfg(any(feature = "dfn3", feature = "noise-bench"))]
mod mem;
#[cfg(feature = "noise-probe")]
pub mod probe;
#[cfg(feature = "dfn3")]
pub use control::{DFN3_IDLE_UNLOAD, Dfn3LoadInfo};
#[cfg(any(feature = "dfn3", feature = "noise-bench"))]
pub(crate) use mem::rss_kib;
#[cfg(feature = "noise-bench")]
pub(crate) use mem::rss_peak_kib;
#[cfg(feature = "audio-core")]
pub(crate) use control::LiveAudio;
#[cfg(feature = "audio-core")]
pub use control::NoiseRemovalControl;
pub(crate) trait NoiseSource: Send + Sync {
fn noise_state(&self) -> Option<NoiseRemovalState>;
fn grace(&self) {}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum NoiseRemoval {
Off,
#[default]
Standard,
}
impl NoiseRemoval {
pub const ALL: [NoiseRemoval; 2] = [NoiseRemoval::Off, NoiseRemoval::Standard];
pub fn as_str(self) -> &'static str {
match self {
NoiseRemoval::Off => "off",
NoiseRemoval::Standard => "standard",
}
}
pub fn from_name(name: &str) -> Option<NoiseRemoval> {
NoiseRemoval::ALL.into_iter().find(|m| m.as_str() == name)
}
}
impl std::fmt::Display for NoiseRemoval {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum NoiseRemovalReason {
Budget,
Fault,
Unavailable,
SwitchFailed,
Loading,
}
impl NoiseRemovalReason {
pub fn as_str(self) -> &'static str {
match self {
NoiseRemovalReason::Budget => "budget",
NoiseRemovalReason::Fault => "fault",
NoiseRemovalReason::Unavailable => "unavailable",
NoiseRemovalReason::SwitchFailed => "switch_failed",
NoiseRemovalReason::Loading => "loading",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum PhoneAudioMode {
Os,
Raw,
}
impl PhoneAudioMode {
pub fn for_mode(mode: NoiseRemoval) -> PhoneAudioMode {
match mode {
NoiseRemoval::Off => PhoneAudioMode::Raw,
NoiseRemoval::Standard => PhoneAudioMode::Os,
}
}
pub fn as_str(self) -> &'static str {
match self {
PhoneAudioMode::Os => "os",
PhoneAudioMode::Raw => "raw",
}
}
}
impl std::fmt::Display for PhoneAudioMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum NoiseModel {
Dfn3,
Rnnoise,
}
impl NoiseModel {
pub fn as_str(self) -> &'static str {
match self {
NoiseModel::Dfn3 => "dfn3",
NoiseModel::Rnnoise => "rnnoise",
}
}
}
impl std::fmt::Display for NoiseModel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum NoiseStepDownReason {
SlowBlock,
SlowWindow,
Heavy,
Fault,
Forced,
Starved,
}
impl NoiseStepDownReason {
pub fn as_str(self) -> &'static str {
match self {
NoiseStepDownReason::SlowBlock => "slow_block",
NoiseStepDownReason::SlowWindow => "slow_window",
NoiseStepDownReason::Heavy => "heavy",
NoiseStepDownReason::Fault => "fault",
NoiseStepDownReason::Forced => "forced",
NoiseStepDownReason::Starved => "starved",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum NoiseDfn3State {
Idle,
Warming,
Running,
SteppedDown,
Retired,
}
impl NoiseDfn3State {
pub fn as_str(self) -> &'static str {
match self {
NoiseDfn3State::Idle => "idle",
NoiseDfn3State::Warming => "warming",
NoiseDfn3State::Running => "running",
NoiseDfn3State::SteppedDown => "stepped_down",
NoiseDfn3State::Retired => "retired",
}
}
}
impl std::fmt::Display for NoiseDfn3State {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl std::fmt::Display for NoiseStepDownReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CaptureTransient {
CallStart,
AudioSessionActivated,
RouteChange,
CameraStart,
AppSceneChange,
Other,
}
impl CaptureTransient {
pub fn as_str(self) -> &'static str {
match self {
CaptureTransient::CallStart => "call_start",
CaptureTransient::AudioSessionActivated => "audio_session_activated",
CaptureTransient::RouteChange => "route_change",
CaptureTransient::CameraStart => "camera_start",
CaptureTransient::AppSceneChange => "app_scene_change",
CaptureTransient::Other => "other",
}
}
}
impl std::fmt::Display for CaptureTransient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CaptureStageClock {
ThreadCpu,
Wall,
}
impl CaptureStageClock {
pub fn as_str(self) -> &'static str {
match self {
CaptureStageClock::ThreadCpu => "thread_cpu",
CaptureStageClock::Wall => "wall",
}
}
}
impl std::fmt::Display for CaptureStageClock {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CaptureDspSchedMode {
#[default]
Workgroup,
TimeConstraintOnly,
None,
OwnWorkgroup,
Unsupported,
}
impl CaptureDspSchedMode {
pub const fn as_str(self) -> &'static str {
match self {
CaptureDspSchedMode::Workgroup => "workgroup",
CaptureDspSchedMode::TimeConstraintOnly => "time_constraint_only",
CaptureDspSchedMode::None => "none",
CaptureDspSchedMode::OwnWorkgroup => "own_workgroup",
CaptureDspSchedMode::Unsupported => "unsupported",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CaptureDspSchedFallback {
TimeConstraintRefused,
OwnWorkgroupUnavailable,
OwnWorkgroupJoinRefused,
IntervalRefused,
Other,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum CaptureDspSchedError {
#[error("the mode `unsupported` is only reported, never asked for")]
ModeNotAskable,
#[error("the time constraint needs 0 < computation <= constraint <= period <= 1 s")]
TimeConstraint,
#[error("the interval deadline must be above 0 and at most the period")]
IntervalDeadline,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct CaptureDspSched {
pub mode: CaptureDspSchedMode,
pub period_us: u32,
pub computation_us: u32,
pub constraint_us: u32,
pub interval_deadline_us: u32,
}
impl Default for CaptureDspSched {
fn default() -> CaptureDspSched {
CaptureDspSched::new(CaptureDspSchedMode::Workgroup)
}
}
impl CaptureDspSched {
pub const fn new(mode: CaptureDspSchedMode) -> CaptureDspSched {
CaptureDspSched {
mode,
period_us: 10_000,
computation_us: 6_000,
constraint_us: 9_000,
interval_deadline_us: 3_000,
}
}
pub const fn with_time_constraint_us(
mut self,
period_us: u32,
computation_us: u32,
constraint_us: u32,
) -> CaptureDspSched {
self.period_us = period_us;
self.computation_us = computation_us;
self.constraint_us = constraint_us;
self
}
pub const fn with_interval_deadline_us(mut self, deadline_us: u32) -> CaptureDspSched {
self.interval_deadline_us = deadline_us;
self
}
pub const fn validate(&self) -> Result<(), CaptureDspSchedError> {
if matches!(self.mode, CaptureDspSchedMode::Unsupported) {
return Err(CaptureDspSchedError::ModeNotAskable);
}
if self.computation_us == 0
|| self.computation_us > self.constraint_us
|| self.constraint_us > self.period_us
|| self.period_us > 1_000_000
{
return Err(CaptureDspSchedError::TimeConstraint);
}
if self.interval_deadline_us == 0 || self.interval_deadline_us > self.period_us {
return Err(CaptureDspSchedError::IntervalDeadline);
}
Ok(())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CaptureDspScheduling {
Workgroup,
TimeConstraint,
Qos,
None,
OwnWorkgroup,
}
impl CaptureDspScheduling {
pub fn as_str(self) -> &'static str {
match self {
CaptureDspScheduling::Workgroup => "workgroup",
CaptureDspScheduling::TimeConstraint => "time_constraint",
CaptureDspScheduling::Qos => "qos",
CaptureDspScheduling::None => "none",
CaptureDspScheduling::OwnWorkgroup => "own_workgroup",
}
}
}
impl std::fmt::Display for CaptureDspScheduling {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub struct NoiseRemovalState {
pub mode: NoiseRemoval,
pub active: NoiseRemoval,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<NoiseRemovalReason>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<NoiseModel>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model_reason: Option<NoiseRemovalReason>,
#[serde(default, skip_serializing_if = "is_zero")]
pub step_downs: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub phone_mode: Option<PhoneAudioMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub phone_mode_reason: Option<NoiseRemovalReason>,
#[serde(default, skip_serializing_if = "is_zero")]
pub phone_mode_switches: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub phone_mode_gap_ms: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub step_down_reason: Option<NoiseStepDownReason>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub last_step_down_ms_ago: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slowest_block_us: Option<u64>,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub blocks_over_budget: u64,
#[serde(default, skip_serializing_if = "is_false")]
pub in_grace: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub dfn3_state: Option<NoiseDfn3State>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retry_in_ms: Option<u64>,
#[serde(default, skip_serializing_if = "is_zero")]
pub grace_step_downs: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slowest_block_cpu_us: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub p99_block_us: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub p99_block_cpu_us: Option<u64>,
#[serde(default, skip_serializing_if = "is_zero")]
pub capture_stage_strikes: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_bypassed: Option<NoiseRemovalReason>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_p99_us: Option<u32>,
#[serde(default, skip_serializing_if = "is_false")]
pub capture_stage_grace: bool,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub capture_stage_grace_blocks: u64,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub capture_stage_grace_over_budget: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_last_transient: Option<CaptureTransient>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_clock: Option<CaptureStageClock>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_max_wall_us: Option<u32>,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub capture_stage_preempted_over_budget: u64,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub capture_stage_starved: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_scheduling: Option<CaptureDspScheduling>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_sched_requested: Option<CaptureDspSchedMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_sched_effective: Option<CaptureDspSchedMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_sched_fallback: Option<CaptureDspSchedFallback>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_period_us: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_computation_us: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_constraint_us: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub capture_stage_dsp_interval_deadline_us: Option<u32>,
#[serde(default, skip_serializing_if = "is_zero_u64")]
pub capture_stage_dsp_intervals: u64,
}
fn is_zero(v: &u32) -> bool {
*v == 0
}
fn is_zero_u64(v: &u64) -> bool {
*v == 0
}
fn is_false(v: &bool) -> bool {
!*v
}
impl NoiseRemovalState {
pub fn new(
mode: NoiseRemoval,
active: NoiseRemoval,
reason: Option<NoiseRemovalReason>,
) -> NoiseRemovalState {
NoiseRemovalState {
mode,
active,
reason,
model: None,
model_reason: None,
step_downs: 0,
phone_mode: None,
phone_mode_reason: None,
phone_mode_switches: 0,
phone_mode_gap_ms: None,
step_down_reason: None,
last_step_down_ms_ago: None,
slowest_block_us: None,
blocks_over_budget: 0,
in_grace: false,
dfn3_state: None,
retry_in_ms: None,
grace_step_downs: 0,
slowest_block_cpu_us: None,
p99_block_us: None,
p99_block_cpu_us: None,
capture_stage_strikes: 0,
capture_stage_bypassed: None,
capture_stage_p99_us: None,
capture_stage_grace: false,
capture_stage_grace_blocks: 0,
capture_stage_grace_over_budget: 0,
capture_stage_last_transient: None,
capture_stage_clock: None,
capture_stage_max_wall_us: None,
capture_stage_preempted_over_budget: 0,
capture_stage_starved: 0,
capture_stage_dsp_scheduling: None,
capture_stage_dsp_sched_requested: None,
capture_stage_dsp_sched_effective: None,
capture_stage_dsp_sched_fallback: None,
capture_stage_dsp_period_us: None,
capture_stage_dsp_computation_us: None,
capture_stage_dsp_constraint_us: None,
capture_stage_dsp_interval_deadline_us: None,
capture_stage_dsp_intervals: 0,
}
}
#[cfg_attr(not(feature = "audio-core"), allow(dead_code))]
pub(crate) fn change_key(mut self) -> NoiseRemovalState {
self.last_step_down_ms_ago = None;
self.slowest_block_us = None;
self.blocks_over_budget = 0;
self.retry_in_ms = None;
self.slowest_block_cpu_us = None;
self.p99_block_us = None;
self.p99_block_cpu_us = None;
self.capture_stage_p99_us = None;
self.capture_stage_grace = false;
self.capture_stage_grace_blocks = 0;
self.capture_stage_grace_over_budget = 0;
self.capture_stage_last_transient = None;
self.capture_stage_clock = None;
self.capture_stage_max_wall_us = None;
self.capture_stage_preempted_over_budget = 0;
self.capture_stage_starved = 0;
self.capture_stage_dsp_scheduling = None;
self.capture_stage_dsp_sched_requested = None;
self.capture_stage_dsp_sched_effective = None;
self.capture_stage_dsp_sched_fallback = None;
self.capture_stage_dsp_period_us = None;
self.capture_stage_dsp_computation_us = None;
self.capture_stage_dsp_constraint_us = None;
self.capture_stage_dsp_interval_deadline_us = None;
self.capture_stage_dsp_intervals = 0;
self
}
pub fn with_phone_mode(
mut self,
phone_mode: Option<PhoneAudioMode>,
phone_mode_reason: Option<NoiseRemovalReason>,
switches: u32,
gap_ms: Option<u32>,
) -> NoiseRemovalState {
self.phone_mode = phone_mode;
self.phone_mode_reason = phone_mode_reason;
self.phone_mode_switches = switches;
self.phone_mode_gap_ms = gap_ms;
self
}
pub fn with_model(
mut self,
model: Option<NoiseModel>,
model_reason: Option<NoiseRemovalReason>,
step_downs: u32,
) -> NoiseRemovalState {
self.model = model;
self.model_reason = model_reason;
self.step_downs = step_downs;
self
}
pub fn running(mode: NoiseRemoval) -> NoiseRemovalState {
NoiseRemovalState::new(mode, mode, None)
}
pub fn stepped_down(mode: NoiseRemoval, reason: NoiseRemovalReason) -> NoiseRemovalState {
if mode == NoiseRemoval::Off {
NoiseRemovalState::running(NoiseRemoval::Off)
} else {
NoiseRemovalState::new(mode, NoiseRemoval::Off, Some(reason))
}
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn names_and_json() {
assert_eq!(NoiseRemoval::default(), NoiseRemoval::Standard);
for m in NoiseRemoval::ALL {
assert_eq!(NoiseRemoval::from_name(m.as_str()), Some(m));
assert_eq!(
serde_json::to_value(m).unwrap(),
serde_json::json!(m.as_str())
);
}
assert_eq!(NoiseRemoval::from_name("strong"), None);
assert_eq!(NoiseRemoval::from_name("Standard"), None);
let s = NoiseRemovalState::running(NoiseRemoval::Standard);
assert_eq!(
serde_json::to_value(s).unwrap(),
serde_json::json!({"mode": "standard", "active": "standard"})
);
let s = NoiseRemovalState::stepped_down(NoiseRemoval::Standard, NoiseRemovalReason::Budget);
assert_eq!(
serde_json::to_value(s).unwrap(),
serde_json::json!({"mode": "standard", "active": "off", "reason": "budget"})
);
for (r, n) in [
(NoiseRemovalReason::Budget, "budget"),
(NoiseRemovalReason::Fault, "fault"),
(NoiseRemovalReason::Unavailable, "unavailable"),
(NoiseRemovalReason::SwitchFailed, "switch_failed"),
(NoiseRemovalReason::Loading, "loading"),
] {
assert_eq!(r.as_str(), n);
assert_eq!(serde_json::to_value(r).unwrap(), serde_json::json!(n));
}
assert_eq!(
NoiseRemovalState::stepped_down(NoiseRemoval::Off, NoiseRemovalReason::Fault),
NoiseRemovalState::running(NoiseRemoval::Off)
);
let back: NoiseRemovalState =
serde_json::from_value(serde_json::json!({"mode": "off", "active": "off"})).unwrap();
assert_eq!(back, NoiseRemovalState::running(NoiseRemoval::Off));
}
#[test]
fn model_fields_json() {
let s = NoiseRemovalState::running(NoiseRemoval::Standard).with_model(
Some(NoiseModel::Dfn3),
None,
0,
);
assert_eq!(
serde_json::to_value(s).unwrap(),
serde_json::json!({"mode": "standard", "active": "standard", "model": "dfn3"})
);
let s = NoiseRemovalState::running(NoiseRemoval::Standard).with_model(
Some(NoiseModel::Rnnoise),
Some(NoiseRemovalReason::Budget),
2,
);
let v = serde_json::to_value(s).unwrap();
assert_eq!(
v,
serde_json::json!({"mode": "standard", "active": "standard", "model": "rnnoise",
"model_reason": "budget", "step_downs": 2})
);
let back: NoiseRemovalState = serde_json::from_value(v).unwrap();
assert_eq!(back, s);
for (m, n) in [(NoiseModel::Dfn3, "dfn3"), (NoiseModel::Rnnoise, "rnnoise")] {
assert_eq!(m.as_str(), n);
assert_eq!(serde_json::to_value(m).unwrap(), serde_json::json!(n));
}
}
#[test]
fn timing_fields_json() {
let base = NoiseRemovalState::running(NoiseRemoval::Standard);
assert_eq!(
serde_json::to_value(base).unwrap(),
serde_json::json!({"mode": "standard", "active": "standard"})
);
let mut s = base;
s.step_down_reason = Some(NoiseStepDownReason::SlowWindow);
s.last_step_down_ms_ago = Some(1_234);
s.slowest_block_us = Some(7_100);
s.blocks_over_budget = 3;
s.in_grace = true;
s.dfn3_state = Some(NoiseDfn3State::SteppedDown);
s.retry_in_ms = Some(4_200);
s.grace_step_downs = 1;
s.slowest_block_cpu_us = Some(1_300);
s.p99_block_us = Some(6_900);
s.p99_block_cpu_us = Some(900);
s.capture_stage_strikes = 1;
s.capture_stage_bypassed = Some(NoiseRemovalReason::Budget);
s.capture_stage_p99_us = Some(6_200);
s.capture_stage_grace = true;
s.capture_stage_grace_blocks = 150;
s.capture_stage_grace_over_budget = 4;
s.capture_stage_last_transient = Some(CaptureTransient::CameraStart);
s.capture_stage_clock = Some(CaptureStageClock::ThreadCpu);
s.capture_stage_max_wall_us = Some(21_000);
s.capture_stage_preempted_over_budget = 5;
s.capture_stage_starved = 1;
s.capture_stage_dsp_scheduling = Some(CaptureDspScheduling::TimeConstraint);
let v = serde_json::to_value(s).unwrap();
assert_eq!(
v,
serde_json::json!({"mode": "standard", "active": "standard",
"step_down_reason": "slow_window", "last_step_down_ms_ago": 1234,
"slowest_block_us": 7100, "blocks_over_budget": 3, "in_grace": true,
"dfn3_state": "stepped_down", "retry_in_ms": 4200, "grace_step_downs": 1,
"slowest_block_cpu_us": 1300, "p99_block_us": 6900, "p99_block_cpu_us": 900,
"capture_stage_strikes": 1, "capture_stage_bypassed": "budget",
"capture_stage_p99_us": 6200, "capture_stage_grace": true,
"capture_stage_grace_blocks": 150, "capture_stage_grace_over_budget": 4,
"capture_stage_last_transient": "camera_start",
"capture_stage_clock": "thread_cpu", "capture_stage_max_wall_us": 21000,
"capture_stage_preempted_over_budget": 5, "capture_stage_starved": 1,
"capture_stage_dsp_scheduling": "time_constraint"})
);
let back: NoiseRemovalState = serde_json::from_value(v).unwrap();
assert_eq!(back, s);
for (r, n) in [
(NoiseStepDownReason::SlowBlock, "slow_block"),
(NoiseStepDownReason::SlowWindow, "slow_window"),
(NoiseStepDownReason::Heavy, "heavy"),
(NoiseStepDownReason::Fault, "fault"),
(NoiseStepDownReason::Forced, "forced"),
(NoiseStepDownReason::Starved, "starved"),
] {
assert_eq!(r.as_str(), n);
assert_eq!(r.to_string(), n);
assert_eq!(serde_json::to_value(r).unwrap(), serde_json::json!(n));
}
for (d, n) in [
(NoiseDfn3State::Idle, "idle"),
(NoiseDfn3State::Warming, "warming"),
(NoiseDfn3State::Running, "running"),
(NoiseDfn3State::SteppedDown, "stepped_down"),
(NoiseDfn3State::Retired, "retired"),
] {
assert_eq!(d.as_str(), n);
assert_eq!(d.to_string(), n);
assert_eq!(serde_json::to_value(d).unwrap(), serde_json::json!(n));
}
let old: NoiseRemovalState = serde_json::from_value(serde_json::json!({
"mode": "standard", "active": "standard", "slowest_block_us": 7100}))
.unwrap();
assert_eq!(
(old.dfn3_state, old.retry_in_ms, old.grace_step_downs),
(None, None, 0)
);
let mut counters = base;
counters.last_step_down_ms_ago = Some(10);
counters.slowest_block_us = Some(1);
counters.blocks_over_budget = 9;
counters.capture_stage_p99_us = Some(5);
counters.retry_in_ms = Some(3_000);
counters.slowest_block_cpu_us = Some(2);
counters.p99_block_us = Some(3);
counters.p99_block_cpu_us = Some(4);
counters.capture_stage_grace = true;
counters.capture_stage_grace_blocks = 7;
counters.capture_stage_grace_over_budget = 2;
counters.capture_stage_last_transient = Some(CaptureTransient::AppSceneChange);
counters.capture_stage_clock = Some(CaptureStageClock::Wall);
counters.capture_stage_max_wall_us = Some(9);
counters.capture_stage_preempted_over_budget = 3;
counters.capture_stage_starved = 1;
counters.capture_stage_dsp_scheduling = Some(CaptureDspScheduling::Workgroup);
assert_eq!(counters.change_key(), base.change_key());
for (d, n) in [
(CaptureDspScheduling::Workgroup, "workgroup"),
(CaptureDspScheduling::TimeConstraint, "time_constraint"),
(CaptureDspScheduling::Qos, "qos"),
(CaptureDspScheduling::None, "none"),
] {
assert_eq!(d.as_str(), n);
assert_eq!(d.to_string(), n);
assert_eq!(serde_json::to_value(d).unwrap(), serde_json::json!(n));
let back: CaptureDspScheduling = serde_json::from_value(serde_json::json!(n)).unwrap();
assert_eq!(back, d);
}
for (c, n) in [
(CaptureStageClock::ThreadCpu, "thread_cpu"),
(CaptureStageClock::Wall, "wall"),
] {
assert_eq!(c.as_str(), n);
assert_eq!(c.to_string(), n);
assert_eq!(serde_json::to_value(c).unwrap(), serde_json::json!(n));
}
for (t, n) in [
(CaptureTransient::CallStart, "call_start"),
(
CaptureTransient::AudioSessionActivated,
"audio_session_activated",
),
(CaptureTransient::RouteChange, "route_change"),
(CaptureTransient::CameraStart, "camera_start"),
(CaptureTransient::AppSceneChange, "app_scene_change"),
(CaptureTransient::Other, "other"),
] {
assert_eq!(t.as_str(), n);
assert_eq!(t.to_string(), n);
assert_eq!(serde_json::to_value(t).unwrap(), serde_json::json!(n));
}
let mut state = base;
state.dfn3_state = Some(NoiseDfn3State::Warming);
assert_ne!(state.change_key(), base.change_key());
let mut tied = base;
tied.grace_step_downs = 1;
assert_ne!(tied.change_key(), base.change_key());
let mut grace = base;
grace.in_grace = true;
assert_ne!(grace.change_key(), base.change_key());
let mut reason = base;
reason.step_down_reason = Some(NoiseStepDownReason::Heavy);
assert_ne!(reason.change_key(), base.change_key());
let mut strikes = base;
strikes.capture_stage_strikes = 1;
assert_ne!(strikes.change_key(), base.change_key());
}
#[test]
fn phone_fields_json() {
let s = NoiseRemovalState::running(NoiseRemoval::Off).with_phone_mode(
Some(PhoneAudioMode::Raw),
None,
0,
None,
);
assert_eq!(
serde_json::to_value(s).unwrap(),
serde_json::json!({"mode": "off", "active": "off", "phone_mode": "raw"})
);
let s = NoiseRemovalState::running(NoiseRemoval::Off).with_phone_mode(
Some(PhoneAudioMode::Os),
Some(NoiseRemovalReason::SwitchFailed),
3,
Some(42),
);
let v = serde_json::to_value(s).unwrap();
assert_eq!(
v,
serde_json::json!({"mode": "off", "active": "off", "phone_mode": "os",
"phone_mode_reason": "switch_failed", "phone_mode_switches": 3,
"phone_mode_gap_ms": 42})
);
let back: NoiseRemovalState = serde_json::from_value(v).unwrap();
assert_eq!(back, s);
for (m, n) in [(PhoneAudioMode::Os, "os"), (PhoneAudioMode::Raw, "raw")] {
assert_eq!(m.as_str(), n);
assert_eq!(m.to_string(), n);
assert_eq!(serde_json::to_value(m).unwrap(), serde_json::json!(n));
}
assert_eq!(
PhoneAudioMode::for_mode(NoiseRemoval::Off),
PhoneAudioMode::Raw
);
assert_eq!(
PhoneAudioMode::for_mode(NoiseRemoval::Standard),
PhoneAudioMode::Os
);
}
}