Merge pull request #393 from derekdreery/recover_if_set_buffer_time_failed

Recover if hw_params_set_buffer_time failed.
This commit is contained in:
mitchmindtree 2020-04-17 16:39:15 +02:00 committed by GitHub
commit 624ba659da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 3 deletions

View File

@ -8,10 +8,10 @@ use crate::{
StreamConfig, StreamError, SupportedStreamConfig, SupportedStreamConfigRange, StreamConfig, StreamError, SupportedStreamConfig, SupportedStreamConfigRange,
SupportedStreamConfigsError, SupportedStreamConfigsError,
}; };
use std::cmp;
use std::sync::Arc; use std::sync::Arc;
use std::thread::{self, JoinHandle}; use std::thread::{self, JoinHandle};
use std::vec::IntoIter as VecIntoIter; use std::vec::IntoIter as VecIntoIter;
use std::{cmp, mem};
use traits::{DeviceTrait, HostTrait, StreamTrait}; use traits::{DeviceTrait, HostTrait, StreamTrait};
pub use self::enumerate::{default_input_device, default_output_device, Devices}; pub use self::enumerate::{default_input_device, default_output_device, Devices};
@ -775,7 +775,7 @@ fn set_hw_params_from_format<'a>(
config: &StreamConfig, config: &StreamConfig,
sample_format: SampleFormat, sample_format: SampleFormat,
) -> Result<alsa::pcm::HwParams<'a>, BackendSpecificError> { ) -> Result<alsa::pcm::HwParams<'a>, BackendSpecificError> {
let hw_params = alsa::pcm::HwParams::any(pcm_handle)?; let mut hw_params = alsa::pcm::HwParams::any(pcm_handle)?;
hw_params.set_access(alsa::pcm::Access::RWInterleaved)?; hw_params.set_access(alsa::pcm::Access::RWInterleaved)?;
let sample_format = if cfg!(target_endian = "big") { let sample_format = if cfg!(target_endian = "big") {
@ -797,7 +797,11 @@ fn set_hw_params_from_format<'a>(
hw_params.set_channels(config.channels as u32)?; hw_params.set_channels(config.channels as u32)?;
// If this isn't set manually a overlarge buffer may be used causing audio delay // If this isn't set manually a overlarge buffer may be used causing audio delay
hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest)?; let mut hw_params_copy = hw_params.clone();
if let Err(_) = hw_params.set_buffer_time_near(100_000, alsa::ValueOr::Nearest) {
// Swap out the params with errors for a snapshot taken before the error was introduced.
mem::swap(&mut hw_params_copy, &mut hw_params);
}
pcm_handle.hw_params(&hw_params)?; pcm_handle.hw_params(&hw_params)?;