70 lines
2.4 KiB
Rust
70 lines
2.4 KiB
Rust
//! Mute intent planning independent of gain and native audio state.
|
|
use crate::{Mute, StreamHandle, ValueState};
|
|
use std::collections::BTreeMap;
|
|
|
|
/// Explicit mute actions or a toggle resolved at command acceptance time.
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum MuteIntent {
|
|
/// Apply one value to every resolved stream.
|
|
Set(Mute),
|
|
/// Mute if any member is unmuted; otherwise unmute.
|
|
Toggle,
|
|
}
|
|
|
|
/// Aggregates observed mute values, preserving mixed and unavailable states.
|
|
pub fn mute_view(values: impl IntoIterator<Item = Mute>) -> ValueState<Mute> {
|
|
let mut values = values.into_iter();
|
|
let Some(first) = values.next() else {
|
|
return ValueState::Unavailable;
|
|
};
|
|
if values.any(|value| value != first) {
|
|
ValueState::Mixed
|
|
} else {
|
|
ValueState::Uniform(first)
|
|
}
|
|
}
|
|
|
|
/// Serial command intent shared by overlapping targets.
|
|
///
|
|
/// Pending intent affects subsequent toggles, never the observed mute view.
|
|
/// The state owner clears a stream when its latest operation settles or is
|
|
/// cancelled, and on removal/disconnect. Older operation completions must not
|
|
/// clear a newer intent; operation correlation belongs to the server reducer.
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct MutePlanner {
|
|
pending: BTreeMap<StreamHandle, Mute>,
|
|
}
|
|
|
|
impl MutePlanner {
|
|
/// Resolves an action into one explicit mute command per stream.
|
|
/// No channel gain is accepted or modified by this API.
|
|
pub fn plan(
|
|
&mut self,
|
|
observed: &BTreeMap<StreamHandle, Mute>,
|
|
intent: MuteIntent,
|
|
) -> BTreeMap<StreamHandle, Mute> {
|
|
let value = match intent {
|
|
MuteIntent::Set(value) => value,
|
|
MuteIntent::Toggle => {
|
|
let any_unmuted = observed.iter().any(|(handle, value)| {
|
|
self.pending.get(handle).unwrap_or(value) == &Mute::Unmuted
|
|
});
|
|
if any_unmuted {
|
|
Mute::Muted
|
|
} else {
|
|
Mute::Unmuted
|
|
}
|
|
}
|
|
};
|
|
let commands: BTreeMap<_, _> = observed.keys().map(|handle| (*handle, value)).collect();
|
|
self.pending
|
|
.extend(commands.iter().map(|(handle, value)| (*handle, *value)));
|
|
commands
|
|
}
|
|
|
|
/// Releases intent only after the owner has checked operation freshness.
|
|
pub fn clear(&mut self, handle: StreamHandle) {
|
|
self.pending.remove(&handle);
|
|
}
|
|
}
|