fix(M7.4): keep fader pickup tracking through Pulse echoes

Correlate subscription notifications that match confirmed Pulse writes with their originating operation. This prevents server-generated echoes from being treated as external edits and re-arming pickup during continuous physical motion, while differing observations still re-arm it.

Tests: full workspace fmt/check/test/doc/clippy/release gates; default/no-default backend matrix; isolated real Pulse/PipeWire stream fixture.
This commit is contained in:
Dandaedre 2026-10-15 03:48:30 +02:00
parent 9b3cc9c9a2
commit 3a75e24003
Signed by: Dandaedre
GPG Key ID: 440C6592E8A26D99
4 changed files with 190 additions and 10 deletions

View File

@ -163,6 +163,13 @@ Use a Tokio event task as the single domain owner. MIDI, IPC, persistence, and a
PulseAudio and native PipeWire event loops run on their own owner threads. In particular, PipeWire Rust objects are not generally Send/Sync; transfer commands and copied data, not handles. [PipeWire Rust threading documentation](https://pipewire.pages.freedesktop.org/pipewire-rs/pipewire/index.html)
Pulse subscription notifications caused by the server's own confirmed writes
retain their originating operation when their copied volume/mute state matches
the expected echo. They remain observations, but must not masquerade as
external edits and repeatedly re-arm a non-motorized fader during motion. A
different observed state clears the echo expectation and remains an external
change that re-arms pickup.
Use bounded `mpsc` for ordered work and `watch` for latest display snapshots. Keep button edges and lifecycle events separate from coalescible fader motion. A MIDI callback only parses/copies bounded input and tries to enqueue; on overflow set an explicit fault/resync flag rather than silently losing mute edges. A dropped button edge cannot be reconstructed from an audio snapshot: report the fault and require a new user press, never guess a toggle. Dedicate output workers to rate-limited MIDI feedback. [Tokio synchronization primitives](https://docs.rs/tokio/latest/tokio/sync/index.html)
The production audio dispatcher retains a bounded server-owned queue in front

View File

@ -282,6 +282,34 @@ fn external_volume_change_rearms_midi_pickup() {
assert_eq!(state.midi_intent(intent(0.8), 3).unwrap().len(), 1);
}
#[test]
fn correlated_write_echo_keeps_midi_pickup_tracking() {
let mut state = ready_state();
let surface = SurfaceId::new("desk-left").unwrap();
let fader = FaderId::new("media-fader").unwrap();
let intent = |value| MidiIntent::AbsoluteVolume {
surface: surface.clone(),
fader: fader.clone(),
gain: gain(value),
};
let first = state.midi_intent(intent(0.4), 1).unwrap();
let operation = match first.as_slice() {
[fader_core::AdapterCommand::SetVolume { operation, .. }] => *operation,
commands => panic!("expected one volume command, got {commands:?}"),
};
state
.worker_event(observed_for(
stream(),
[0.4, 0.2],
Mute::Unmuted,
Some(operation),
))
.unwrap();
assert_eq!(state.midi_intent(intent(0.8), 2).unwrap().len(), 1);
}
#[test]
fn controller_disconnect_forgets_the_tracked_physical_position() {
let mut state = ready_state();

View File

@ -175,6 +175,64 @@ struct OutputTracker {
current: Option<(u32, StreamHandle)>,
}
#[derive(Debug)]
struct ExpectedEcho {
channels: fader_core::ChannelGains,
muted: fader_core::Mute,
operation: fader_core::OperationId,
remaining: usize,
}
#[derive(Debug, Default)]
struct ExpectedEchoes(BTreeMap<StreamHandle, ExpectedEcho>);
impl ExpectedEchoes {
fn record(
&mut self,
stream: StreamHandle,
channels: fader_core::ChannelGains,
muted: fader_core::Mute,
operation: fader_core::OperationId,
) {
let remaining = self
.0
.get(&stream)
.map_or(1, |echo| echo.remaining.saturating_add(1));
self.0.insert(
stream,
ExpectedEcho {
channels,
muted,
operation,
remaining,
},
);
}
fn correlate(
&mut self,
stream: StreamHandle,
channels: &fader_core::ChannelGains,
muted: fader_core::Mute,
) -> Option<fader_core::OperationId> {
let echo = self.0.get_mut(&stream)?;
if echo.channels != *channels || echo.muted != muted {
self.0.remove(&stream);
return None;
}
let operation = echo.operation;
echo.remaining -= 1;
if echo.remaining == 0 {
self.0.remove(&stream);
}
Some(operation)
}
fn remove(&mut self, stream: StreamHandle) {
self.0.remove(&stream);
}
}
impl OutputTracker {
fn new(epoch: BackendEpoch) -> Self {
Self {
@ -379,6 +437,7 @@ fn run_connection<C: PulseConnection>(
let mut tracker = StreamTracker::new(epoch);
let mut output_tracker = OutputTracker::new(epoch);
let mut expected_echoes = ExpectedEchoes::default();
for stream in connection.streams()? {
let handle = tracker
.current_or_insert(stream.index)
@ -396,7 +455,13 @@ fn run_connection<C: PulseConnection>(
}
}
while let Some(notification) = connection.poll_notification(Duration::ZERO)? {
if !process_notification(notification, &mut tracker, &mut output_tracker, events)? {
if !process_notification(
notification,
&mut tracker,
&mut output_tracker,
&mut expected_echoes,
events,
)? {
return Ok(());
}
}
@ -412,6 +477,7 @@ fn run_connection<C: PulseConnection>(
connection,
&mut tracker,
&mut output_tracker,
&mut expected_echoes,
events,
) {
return Ok(());
@ -421,7 +487,13 @@ fn run_connection<C: PulseConnection>(
Err(TryRecvError::Empty) => {}
}
if let Some(notification) = connection.poll_notification(NOTIFICATION_POLL)?
&& !process_notification(notification, &mut tracker, &mut output_tracker, events)?
&& !process_notification(
notification,
&mut tracker,
&mut output_tracker,
&mut expected_echoes,
events,
)?
{
return Ok(());
}
@ -432,6 +504,7 @@ fn process_notification(
notification: PulseNotification,
tracker: &mut StreamTracker,
output_tracker: &mut OutputTracker,
expected_echoes: &mut ExpectedEchoes,
events: &EventLane,
) -> Result<bool, PulseError> {
match notification {
@ -448,12 +521,14 @@ fn process_notification(
let handle = tracker
.current_or_insert(stream.index)
.map_err(|message| PulseError::Volume(message.to_owned()))?;
Ok(emit_stream(events, handle, stream, None))
let operation = expected_echoes.correlate(handle, &stream.channels, stream.muted);
Ok(emit_stream(events, handle, stream, operation))
}
PulseNotification::Removed(index) => {
let Some(stream) = tracker.remove(index) else {
return Ok(true);
};
expected_echoes.remove(stream);
Ok(events.emit(WorkerEvent::Adapter(AdapterEvent::StreamRemoved { stream })))
}
PulseNotification::OutputChanged(output) => {
@ -466,12 +541,14 @@ fn process_notification(
let (_, handle) = output_tracker
.replace(output.index)
.map_err(|message| PulseError::Volume(message.to_owned()))?;
Ok(emit_output(events, handle, output, None))
let operation = expected_echoes.correlate(handle, &output.channels, output.muted);
Ok(emit_output(events, handle, output, operation))
}
PulseNotification::OutputRemoved(index) => {
let Some(stream) = output_tracker.remove(index) else {
return Ok(true);
};
expected_echoes.remove(stream);
Ok(events.emit(WorkerEvent::Adapter(AdapterEvent::StreamRemoved { stream })))
}
PulseNotification::DefaultOutputChanged(output) => {
@ -483,13 +560,15 @@ fn process_notification(
let (_, handle) = output_tracker
.replace(output.index)
.map_err(|message| PulseError::Volume(message.to_owned()))?;
return Ok(emit_output(events, handle, output.clone(), None));
let operation = expected_echoes.correlate(handle, &output.channels, output.muted);
return Ok(emit_output(events, handle, output.clone(), operation));
}
let removed = output_tracker.clear();
if let Some(stream) = removed
&& !events.emit(WorkerEvent::Adapter(AdapterEvent::StreamRemoved { stream }))
{
return Ok(false);
if let Some(stream) = removed {
expected_echoes.remove(stream);
if !events.emit(WorkerEvent::Adapter(AdapterEvent::StreamRemoved { stream })) {
return Ok(false);
}
}
let Some(output) = output else {
return Ok(true);
@ -507,6 +586,7 @@ fn process_command<C: PulseConnection>(
connection: &mut C,
tracker: &mut StreamTracker,
output_tracker: &mut OutputTracker,
expected_echoes: &mut ExpectedEchoes,
events: &EventLane,
) -> bool {
let (operation, handle) = match &command {
@ -534,6 +614,14 @@ fn process_command<C: PulseConnection>(
{
return false;
}
if !is_readback {
expected_echoes.record(
handle,
stream.channels.clone(),
stream.muted,
operation,
);
}
emit_stream(events, handle, stream, Some(operation))
}
Err(error) => emit_failure(events, operation, error.to_string()),
@ -558,6 +646,9 @@ fn process_command<C: PulseConnection>(
{
return false;
}
if !is_readback {
expected_echoes.record(handle, output.channels.clone(), output.muted, operation);
}
emit_output(events, handle, output, Some(operation))
}
Err(error) => emit_failure(events, operation, error.to_string()),
@ -999,6 +1090,57 @@ mod tests {
assert!(worker.shutdown());
}
#[test]
fn subscription_echoes_keep_the_originating_operation() {
let (connection, control, _) =
FakeConnection::new(vec![stream(3, &[0.2, 0.4], Mute::Unmuted)]);
let mut worker = PulseWorker::spawn_with(BackendEpoch::new(5), 32, move || Ok(connection));
let initial = initial_snapshot(&worker);
let handle = observed(&initial)[0].0;
let operation = OperationId::new(10);
let written = stream(3, &[0.3, 0.6], Mute::Unmuted);
worker
.try_submit(AdapterCommand::SetVolume {
operation,
stream: handle,
channels: written.channels.clone(),
})
.expect("queued volume write");
assert!(matches!(
receive(&worker),
WorkerEvent::Adapter(AdapterEvent::WriteAccepted { operation: actual })
if actual == operation
));
let _metadata = receive(&worker);
let _confirmed = receive(&worker);
control.notify(PulseNotification::Changed(written));
let _metadata = receive(&worker);
assert!(matches!(
receive(&worker),
WorkerEvent::Adapter(AdapterEvent::Observed {
operation: Some(actual),
..
}) if actual == operation
));
control.notify(PulseNotification::Changed(stream(
3,
&[0.45, 0.9],
Mute::Unmuted,
)));
let _metadata = receive(&worker);
assert!(matches!(
receive(&worker),
WorkerEvent::Adapter(AdapterEvent::Observed {
operation: None,
..
})
));
assert!(worker.shutdown());
}
#[test]
fn default_output_is_distinct_and_accepts_confirmed_writes() {
let (connection, control, _) = FakeConnection::new(Vec::new());

View File

@ -252,7 +252,10 @@ Dependencies: M3, M2; may proceed after the fake-adapter slice.
polls at 20 ms while pending and 250 ms while idle, and retains a bounded
128-edge mute FIFO. The live server uses the tested 256-command coalescing
queue ahead of a one-slot Pulse command lane, an independent 256-event
snapshot lane, and a batch event drain.
snapshot lane, and a batch event drain. Matching Pulse subscription echoes
retain the originating operation so confirmed writes do not repeatedly re-arm
pickup during continuous physical fader motion; genuinely different external
observations still re-arm it.
## M8 — Live TUI