From 119ada803e1e9953a06a4eb984527231b7e2d4d4 Mon Sep 17 00:00:00 2001 From: Jacob Hughes Date: Mon, 31 Aug 2020 13:35:06 -0400 Subject: [PATCH 1/4] 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/4] 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. From b5b6014edf0da61f72840adefe8cadd2aac64479 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Thu, 10 Sep 2020 15:50:35 +0200 Subject: [PATCH 3/4] wasm backends: Fix default_output_config behaviour --- src/host/emscripten/mod.rs | 2 +- src/host/webaudio/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host/emscripten/mod.rs b/src/host/emscripten/mod.rs index dbf5078..07505aa 100644 --- a/src/host/emscripten/mod.rs +++ b/src/host/emscripten/mod.rs @@ -107,7 +107,7 @@ impl Device { configs.sort_by(|a, b| a.cmp_default_heuristics(b)); let config = configs .into_iter() - .next() + .last() .expect(EXPECT) .with_sample_rate(DEFAULT_SAMPLE_RATE); Ok(config) diff --git a/src/host/webaudio/mod.rs b/src/host/webaudio/mod.rs index f58f18c..6db820a 100644 --- a/src/host/webaudio/mod.rs +++ b/src/host/webaudio/mod.rs @@ -125,7 +125,7 @@ impl Device { configs.sort_by(|a, b| a.cmp_default_heuristics(b)); let config = configs .into_iter() - .next() + .last() .expect(EXPECT) .with_sample_rate(DEFAULT_SAMPLE_RATE); Ok(config) From 0b300afefe17f60d3b4efb4e896733566ef4f42a Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Fri, 11 Sep 2020 11:34:23 +0200 Subject: [PATCH 4/4] wasm backends: Avoid a heap allocation in default_output_config() --- src/host/emscripten/mod.rs | 10 +++++----- src/host/webaudio/mod.rs | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/host/emscripten/mod.rs b/src/host/emscripten/mod.rs index 07505aa..4c51f3d 100644 --- a/src/host/emscripten/mod.rs +++ b/src/host/emscripten/mod.rs @@ -103,13 +103,13 @@ impl Device { fn default_output_config(&self) -> Result { const EXPECT: &str = "expected at least one valid webaudio stream config"; - let mut configs: Vec<_> = self.supported_output_configs().expect(EXPECT).collect(); - configs.sort_by(|a, b| a.cmp_default_heuristics(b)); - let config = configs - .into_iter() - .last() + let config = self + .supported_output_configs() .expect(EXPECT) + .max_by(|a, b| a.cmp_default_heuristics(b)) + .unwrap() .with_sample_rate(DEFAULT_SAMPLE_RATE); + Ok(config) } } diff --git a/src/host/webaudio/mod.rs b/src/host/webaudio/mod.rs index 6db820a..fc138ef 100644 --- a/src/host/webaudio/mod.rs +++ b/src/host/webaudio/mod.rs @@ -121,13 +121,13 @@ impl Device { #[inline] fn default_output_config(&self) -> Result { const EXPECT: &str = "expected at least one valid webaudio stream config"; - let mut configs: Vec<_> = self.supported_output_configs().expect(EXPECT).collect(); - configs.sort_by(|a, b| a.cmp_default_heuristics(b)); - let config = configs - .into_iter() - .last() + let config = self + .supported_output_configs() .expect(EXPECT) + .max_by(|a, b| a.cmp_default_heuristics(b)) + .unwrap() .with_sample_rate(DEFAULT_SAMPLE_RATE); + Ok(config) } }