diff --git a/sound/src/lib.rs b/sound/src/lib.rs index 491bc41..1d4cf15 100644 --- a/sound/src/lib.rs +++ b/sound/src/lib.rs @@ -2,7 +2,7 @@ #![forbid(unsafe_code)] -use std::collections::BTreeMap; +use std::collections::{BTreeMap, VecDeque}; use fader_core::{AdapterCommand, AdapterEvent, BackendEpoch, ChannelGains, Mute, StreamHandle}; @@ -37,6 +37,86 @@ pub struct FakeAdapter { during_snapshot: Vec, } +/// Result of enqueueing work into the bounded adapter lane. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum Enqueue { + Queued, + /// An unsent volume operation was replaced by a newer one. + Coalesced { + superseded: fader_core::OperationId, + }, + /// An event could not be retained; callers must expose a fault and resync. + Overflow, +} + +/// Bounded ordered dispatch with volume-only coalescing. +#[derive(Debug)] +pub struct AdapterQueue { + capacity: usize, + commands: VecDeque, + faulted: bool, +} + +impl AdapterQueue { + #[must_use] + pub fn new(capacity: usize) -> Self { + Self { + capacity, + commands: VecDeque::with_capacity(capacity), + faulted: false, + } + } + + pub fn push(&mut self, command: AdapterCommand) -> Enqueue { + if let AdapterCommand::SetVolume { + operation: _, + stream, + .. + } = &command + && let Some(index) = self.commands.iter().position(|queued| { + matches!(queued, AdapterCommand::SetVolume { stream: queued_stream, .. } if queued_stream == stream) + }) + && let Some(AdapterCommand::SetVolume { + operation: superseded, + .. + }) = self.commands.remove(index) + { + self.commands.insert(index, command); + return Enqueue::Coalesced { superseded }; + } + + if self.commands.len() >= self.capacity { + self.faulted = true; + return Enqueue::Overflow; + } + self.commands.push_back(command); + Enqueue::Queued + } + + pub fn pop(&mut self) -> Option { + self.commands.pop_front() + } + + #[must_use] + pub fn len(&self) -> usize { + self.commands.len() + } + + #[must_use] + pub fn is_empty(&self) -> bool { + self.commands.is_empty() + } + + #[must_use] + pub const fn faulted(&self) -> bool { + self.faulted + } + + pub fn clear_fault(&mut self) { + self.faulted = false; + } +} + impl FakeAdapter { #[must_use] pub fn new(epoch: BackendEpoch) -> Self { diff --git a/sound/tests/ordering.rs b/sound/tests/ordering.rs new file mode 100644 index 0000000..694c770 --- /dev/null +++ b/sound/tests/ordering.rs @@ -0,0 +1,72 @@ +use fader_core::{ + AdapterCommand, BackendEpoch, ChannelGains, Gain, Mute, OperationId, StreamHandle, +}; +use volume_lib::{AdapterQueue, Enqueue}; + +fn stream(id: u64) -> StreamHandle { + StreamHandle::new(BackendEpoch::new(1), id, 1) +} +fn volume(operation: u64, stream: StreamHandle, value: f64) -> AdapterCommand { + AdapterCommand::SetVolume { + operation: OperationId::new(operation), + stream, + channels: ChannelGains::new(vec![Gain::new(value).unwrap()]).unwrap(), + } +} +fn mute(operation: u64, muted: Mute) -> AdapterCommand { + AdapterCommand::SetMute { + operation: OperationId::new(operation), + stream: stream(1), + muted, + } +} + +#[test] +fn volume_burst_stays_bounded_and_ends_at_latest_value() { + let mut queue = AdapterQueue::new(2); + assert_eq!(queue.push(volume(1, stream(1), 0.1)), Enqueue::Queued); + for operation in 2_u32..=100 { + assert!(matches!( + queue.push(volume( + u64::from(operation), + stream(1), + f64::from(operation) / 100.0 + )), + Enqueue::Coalesced { .. } + )); + } + assert_eq!(queue.len(), 1); + let AdapterCommand::SetVolume { + operation, + channels, + .. + } = queue.pop().unwrap() + else { + panic!("volume command"); + }; + assert_eq!(operation, OperationId::new(100)); + assert_eq!(channels.maximum(), Gain::UNITY); +} + +#[test] +fn mute_edges_are_ordered_and_never_silently_coalesced() { + let mut queue = AdapterQueue::new(2); + assert_eq!(queue.push(mute(1, Mute::Muted)), Enqueue::Queued); + assert_eq!(queue.push(mute(2, Mute::Unmuted)), Enqueue::Queued); + assert_eq!(queue.push(mute(3, Mute::Muted)), Enqueue::Overflow); + assert!(queue.faulted()); + assert!(matches!( + queue.pop(), + Some(AdapterCommand::SetMute { + muted: Mute::Muted, + .. + }) + )); + assert!(matches!( + queue.pop(), + Some(AdapterCommand::SetMute { + muted: Mute::Unmuted, + .. + }) + )); +}