From 119ada803e1e9953a06a4eb984527231b7e2d4d4 Mon Sep 17 00:00:00 2001 From: Jacob Hughes Date: Mon, 31 Aug 2020 13:35:06 -0400 Subject: [PATCH 1/2] 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. From 6b914857819d4a0a314b5fa8f59d5c53d4713b52 Mon Sep 17 00:00:00 2001 From: Jacob Hughes Date: Tue, 1 Sep 2020 12:31:06 -0400 Subject: [PATCH 2/2] ALSA: Fix 100% CPU usage after underrun With my previous patch, after an underrun occurred sometimes polling the alsa FDs would always return POLLIN immediately, but avail_frames would be zero. Eventually avail_frames would become nonzero and audio output would work, but poll would still return immediately afterwards. Calling `stream.prepare()` when this happens seems to fix the issue after a few loops. --- src/host/alsa/mod.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/host/alsa/mod.rs b/src/host/alsa/mod.rs index 3af07da..7b8372e 100644 --- a/src/host/alsa/mod.rs +++ b/src/host/alsa/mod.rs @@ -504,9 +504,11 @@ fn input_stream_worker( .unwrap_or(PollDescriptorsFlow::Continue); match flow { - PollDescriptorsFlow::Continue => continue, + PollDescriptorsFlow::Continue => { + continue; + } PollDescriptorsFlow::XRun => { - stream.channel.prepare().unwrap(); + report_error(stream.channel.prepare(), error_callback); continue; } PollDescriptorsFlow::Return => return, @@ -549,9 +551,12 @@ fn output_stream_worker( .unwrap_or(PollDescriptorsFlow::Continue); match flow { - PollDescriptorsFlow::Continue => continue, + PollDescriptorsFlow::Continue => { + report_error(stream.channel.prepare(), error_callback); + continue; + } PollDescriptorsFlow::XRun => { - stream.channel.prepare().unwrap(); + report_error(stream.channel.prepare(), error_callback); continue; } PollDescriptorsFlow::Return => return, @@ -665,7 +670,12 @@ fn poll_descriptors_and_prepare_buffer( }; let status = stream.channel.status()?; - let avail_frames = status.get_avail() as usize; + let avail_frames = match stream.channel.avail() { + Err(err) if err.errno() == Some(nix::errno::Errno::EPIPE) => { + return Ok(PollDescriptorsFlow::XRun) + } + res => res, + }? as usize; let delay_frames = match status.get_delay() { // Buffer underrun. TODO: Notify the user. d if d < 0 => 0, @@ -675,11 +685,7 @@ 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 if status.get_state() == alsa::pcm::State::XRun { - Ok(PollDescriptorsFlow::XRun) - } else { - Ok(PollDescriptorsFlow::Continue) - }; + return Ok(PollDescriptorsFlow::Continue); } // Prepare the data buffer.