Merge pull request #419 from fy2462/fix_audio_switch_on_linux

Fix audio switch on linux
This commit is contained in:
RustDesk 2022-02-28 12:08:44 +08:00 committed by GitHub
commit 9cc3cf92ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

View File

@ -515,6 +515,10 @@ pub fn set_option(key: &str, value: &str) {
set_options(options).ok();
}
pub fn restart_autdio_service() {
crate::audio_service::restart();
}
#[tokio::main(flavor = "current_thread")]
pub async fn set_options(value: HashMap<String, String>) -> ResultType<()> {
let mut c = connect(1000, "").await?;

View File

@ -14,9 +14,11 @@
use super::*;
use magnum_opus::{Application::*, Channels::*, Encoder};
use std::sync::atomic::{AtomicBool, Ordering};
pub const NAME: &'static str = "audio";
pub const AUDIO_DATA_SIZE_U8: usize = 960 * 4; // 10ms in 48000 stereo
static RESTARTING: AtomicBool = AtomicBool::new(false);
#[cfg(not(target_os = "linux"))]
pub fn new() -> GenericService {
@ -32,12 +34,21 @@ pub fn new() -> GenericService {
sp
}
pub fn restart() {
log::info!("restart the audio service, freezing now...");
if RESTARTING.load(Ordering::SeqCst) {
return;
}
RESTARTING.store(true, Ordering::SeqCst);
}
#[cfg(target_os = "linux")]
mod pa_impl {
use super::*;
#[tokio::main(flavor = "current_thread")]
pub async fn run(sp: GenericService) -> ResultType<()> {
hbb_common::sleep(0.1).await; // one moment to wait for _pa ipc
RESTARTING.store(false, Ordering::SeqCst);
let mut stream = crate::ipc::connect(1000, "_pa").await?;
unsafe {
AUDIO_ZERO_COUNT = 0;
@ -52,7 +63,7 @@ mod pa_impl {
.await
);
let zero_audio_frame: Vec<f32> = vec![0.; AUDIO_DATA_SIZE_U8 / 4];
while sp.ok() {
while sp.ok() && !RESTARTING.load(Ordering::SeqCst) {
sp.snapshot(|sps| {
sps.send(create_format_msg(crate::platform::linux::PA_SAMPLE_RATE, 2));
Ok(())
@ -61,7 +72,7 @@ mod pa_impl {
if data.len() == 0 {
send_f32(&zero_audio_frame, &mut encoder, &sp);
continue;
}
}
if data.len() != AUDIO_DATA_SIZE_U8 {
continue;
}

View File

@ -364,6 +364,10 @@ impl UI {
}
}
fn refresh_audio_input(&self) {
ipc::restart_autdio_service();
}
fn install_path(&mut self) -> String {
#[cfg(windows)]
return crate::platform::windows::get_install_info().1;
@ -701,6 +705,7 @@ impl sciter::EventHandler for UI {
fn create_shortcut(String);
fn discover();
fn get_lan_peers();
fn refresh_audio_input();
}
}

View File

@ -131,6 +131,7 @@ class AudioInputs: Reactor.Component {
if (v == this.get_value()) return;
if (v == this.get_default()) v = "";
handler.set_option("audio-input", v);
handler.refresh_audio_input();
this.toggleMenuState();
}
}