From 119ada803e1e9953a06a4eb984527231b7e2d4d4 Mon Sep 17 00:00:00 2001 From: Jacob Hughes Date: Mon, 31 Aug 2020 13:35:06 -0400 Subject: [PATCH] Handle ALSA overruns/underruns by resetting the PCM device --- src/host/alsa/mod.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/host/alsa/mod.rs b/src/host/alsa/mod.rs index f9344b8..3af07da 100644 --- a/src/host/alsa/mod.rs +++ b/src/host/alsa/mod.rs @@ -505,6 +505,10 @@ fn input_stream_worker( match flow { PollDescriptorsFlow::Continue => continue, + PollDescriptorsFlow::XRun => { + stream.channel.prepare().unwrap(); + continue; + } PollDescriptorsFlow::Return => return, PollDescriptorsFlow::Ready { status, @@ -546,6 +550,10 @@ fn output_stream_worker( match flow { PollDescriptorsFlow::Continue => continue, + PollDescriptorsFlow::XRun => { + stream.channel.prepare().unwrap(); + continue; + } PollDescriptorsFlow::Return => return, PollDescriptorsFlow::Ready { status, @@ -598,6 +606,7 @@ enum PollDescriptorsFlow { avail_frames: usize, delay_frames: usize, }, + XRun, } // This block is shared between both input and output stream worker functions. @@ -666,7 +675,11 @@ fn poll_descriptors_and_prepare_buffer( // Only go on if there is at least `stream.period_len` samples. if available_samples < stream.period_len { - return Ok(PollDescriptorsFlow::Continue); + return if status.get_state() == alsa::pcm::State::XRun { + Ok(PollDescriptorsFlow::XRun) + } else { + Ok(PollDescriptorsFlow::Continue) + }; } // Prepare the data buffer.