feat: add audio thread in server being controlled

This commit is contained in:
Kingtous 2023-01-29 11:55:37 +08:00
parent 65ab43aa4a
commit 8e2d6945d0
2 changed files with 14 additions and 8 deletions

View File

@ -297,7 +297,7 @@ impl<T: InvokeUiSession> Remote<T> {
_ => {} _ => {}
} }
match rx_audio_data.try_recv() { match rx_audio_data.try_recv() {
Ok((instant, msg)) => match &msg.union { Ok((_instant, msg)) => match &msg.union {
Some(message::Union::AudioFrame(frame)) => { Some(message::Union::AudioFrame(frame)) => {
let mut msg = Message::new(); let mut msg = Message::new();
msg.set_audio_frame(frame.clone()); msg.set_audio_frame(frame.clone());
@ -311,7 +311,6 @@ impl<T: InvokeUiSession> Remote<T> {
log::debug!("send audio misc {:?}", misc.audio_format()); log::debug!("send audio misc {:?}", misc.audio_format());
} }
_ => {} _ => {}
None => {}
}, },
Err(err) => { Err(err) => {
if err == TryRecvError::Empty { if err == TryRecvError::Empty {

View File

@ -5,7 +5,7 @@ use crate::clipboard_file::*;
use crate::common::update_clipboard; use crate::common::update_clipboard;
#[cfg(windows)] #[cfg(windows)]
use crate::portable_service::client as portable_client; use crate::portable_service::client as portable_client;
use crate::video_service; use crate::{video_service, client::{MediaSender, start_audio_thread, LatencyController, MediaData}};
#[cfg(any(target_os = "android", target_os = "ios"))] #[cfg(any(target_os = "android", target_os = "ios"))]
use crate::{common::DEVICE_NAME, flutter::connection_manager::start_channel}; use crate::{common::DEVICE_NAME, flutter::connection_manager::start_channel};
use crate::{ipc, VERSION}; use crate::{ipc, VERSION};
@ -95,6 +95,7 @@ pub struct Connection {
disable_clipboard: bool, // by peer disable_clipboard: bool, // by peer
disable_audio: bool, // by peer disable_audio: bool, // by peer
enable_file_transfer: bool, // by peer enable_file_transfer: bool, // by peer
audio_sender: MediaSender, // audio by the remote peer/client
tx_input: std_mpsc::Sender<MessageInput>, // handle input messages tx_input: std_mpsc::Sender<MessageInput>, // handle input messages
video_ack_required: bool, video_ack_required: bool,
peer_info: (String, String), peer_info: (String, String),
@ -168,6 +169,9 @@ impl Connection {
let mut hbbs_rx = crate::hbbs_http::sync::signal_receiver(); let mut hbbs_rx = crate::hbbs_http::sync::signal_receiver();
let tx_cloned = tx.clone(); let tx_cloned = tx.clone();
// Start a audio thread to play the audio sent by peer.
let latency_controller = LatencyController::new();
let audio_sender = start_audio_thread(Some(latency_controller));
let mut conn = Self { let mut conn = Self {
inner: ConnInner { inner: ConnInner {
id, id,
@ -209,6 +213,7 @@ impl Connection {
#[cfg(windows)] #[cfg(windows)]
portable: Default::default(), portable: Default::default(),
from_switch: false, from_switch: false,
audio_sender,
}; };
#[cfg(not(any(target_os = "android", target_os = "ios")))] #[cfg(not(any(target_os = "android", target_os = "ios")))]
tokio::spawn(async move { tokio::spawn(async move {
@ -1534,8 +1539,9 @@ impl Connection {
_ => {} _ => {}
}, },
Some(misc::Union::AudioFormat(format)) => { Some(misc::Union::AudioFormat(format)) => {
// TODO: implement audio format handler if !self.disable_audio {
println!("recv audio format"); allow_err!(self.audio_sender.send(MediaData::AudioFormat(format)));
}
} }
#[cfg(feature = "flutter")] #[cfg(feature = "flutter")]
Some(misc::Union::SwitchSidesRequest(s)) => { Some(misc::Union::SwitchSidesRequest(s)) => {
@ -1554,9 +1560,10 @@ impl Connection {
} }
_ => {} _ => {}
}, },
Some(message::Union::AudioFrame(audio_frame)) => { Some(message::Union::AudioFrame(frame)) => {
// TODO: implement audio frame handler if !self.disable_audio {
println!("recv audio frame"); allow_err!(self.audio_sender.send(MediaData::AudioFrame(frame)));
}
} }
_ => {} _ => {}
} }