opt: remove latency detector on single audio

This commit is contained in:
Kingtous 2023-01-29 14:10:06 +08:00
parent 8e2d6945d0
commit 45a6fc3618
3 changed files with 19 additions and 1 deletions

View File

@ -714,6 +714,7 @@ impl AudioHandler {
.check_audio(frame.timestamp) .check_audio(frame.timestamp)
.not() .not()
{ {
log::debug!("audio frame {} is ignored", frame.timestamp);
return; return;
} }
} }
@ -724,6 +725,7 @@ impl AudioHandler {
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
if self.simple.is_none() { if self.simple.is_none() {
log::debug!("PulseAudio simple binding does not exists");
return; return;
} }
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
@ -768,6 +770,7 @@ impl AudioHandler {
unsafe { std::slice::from_raw_parts::<u8>(buffer.as_ptr() as _, n * 4) }; unsafe { std::slice::from_raw_parts::<u8>(buffer.as_ptr() as _, n * 4) };
self.simple.as_mut().map(|x| x.write(data_u8)); self.simple.as_mut().map(|x| x.write(data_u8));
} }
log::debug!("write Audio frame {} to system.", frame.timestamp);
} }
}); });
} }
@ -1589,9 +1592,11 @@ pub fn start_audio_thread(
if let Ok(data) = audio_receiver.recv() { if let Ok(data) = audio_receiver.recv() {
match data { match data {
MediaData::AudioFrame(af) => { MediaData::AudioFrame(af) => {
log::debug!("recved audio frame={}", af.timestamp);
audio_handler.handle_frame(af); audio_handler.handle_frame(af);
} }
MediaData::AudioFormat(f) => { MediaData::AudioFormat(f) => {
log::debug!("recved audio format, sample rate={}", f.sample_rate);
audio_handler.handle_format(f); audio_handler.handle_format(f);
} }
_ => {} _ => {}

View File

@ -18,6 +18,7 @@ pub struct LatencyController {
last_video_remote_ts: i64, // generated on remote device last_video_remote_ts: i64, // generated on remote device
update_time: Instant, update_time: Instant,
allow_audio: bool, allow_audio: bool,
enabled: bool
} }
impl Default for LatencyController { impl Default for LatencyController {
@ -26,6 +27,7 @@ impl Default for LatencyController {
last_video_remote_ts: Default::default(), last_video_remote_ts: Default::default(),
update_time: Instant::now(), update_time: Instant::now(),
allow_audio: Default::default(), allow_audio: Default::default(),
enabled: true
} }
} }
} }
@ -36,6 +38,11 @@ impl LatencyController {
Arc::new(Mutex::new(LatencyController::default())) Arc::new(Mutex::new(LatencyController::default()))
} }
/// Set whether this [LatencyController] should be enabled.
pub fn set_enabled(&mut self, enable: bool) {
self.enabled = enable;
}
/// Update the latency controller with the latest video timestamp. /// Update the latency controller with the latest video timestamp.
pub fn update_video(&mut self, timestamp: i64) { pub fn update_video(&mut self, timestamp: i64) {
self.last_video_remote_ts = timestamp; self.last_video_remote_ts = timestamp;
@ -44,6 +51,10 @@ impl LatencyController {
/// Check if the audio should be played based on the current latency. /// Check if the audio should be played based on the current latency.
pub fn check_audio(&mut self, timestamp: i64) -> bool { pub fn check_audio(&mut self, timestamp: i64) -> bool {
if !self.enabled {
self.allow_audio = true;
return self.allow_audio;
}
// Compute audio latency. // Compute audio latency.
let expected = self.update_time.elapsed().as_millis() as i64 + self.last_video_remote_ts; let expected = self.update_time.elapsed().as_millis() as i64 + self.last_video_remote_ts;
let latency = expected - timestamp; let latency = expected - timestamp;

View File

@ -171,6 +171,8 @@ impl Connection {
let tx_cloned = tx.clone(); let tx_cloned = tx.clone();
// Start a audio thread to play the audio sent by peer. // Start a audio thread to play the audio sent by peer.
let latency_controller = LatencyController::new(); let latency_controller = LatencyController::new();
// No video frame will be sent here, so we need to disable latency controller, or audio check may fail.
latency_controller.lock().unwrap().set_enabled(false);
let audio_sender = start_audio_thread(Some(latency_controller)); let audio_sender = start_audio_thread(Some(latency_controller));
let mut conn = Self { let mut conn = Self {
inner: ConnInner { inner: ConnInner {
@ -1561,7 +1563,7 @@ impl Connection {
_ => {} _ => {}
}, },
Some(message::Union::AudioFrame(frame)) => { Some(message::Union::AudioFrame(frame)) => {
if !self.disable_audio { if !self.disable_audio {
allow_err!(self.audio_sender.send(MediaData::AudioFrame(frame))); allow_err!(self.audio_sender.send(MediaData::AudioFrame(frame)));
} }
} }