Add the ability to get backend latency

This commit is contained in:
JasonLG1979 2023-06-21 19:17:23 -05:00
parent 0cefd0ea66
commit 35b94bdc94
3 changed files with 31 additions and 0 deletions

View file

@ -456,6 +456,23 @@ impl Sink for AlsaSink {
Ok(())
}
fn get_latency_pcm(&mut self) -> u64 {
let buffer_len = self.period_buffer.len();
self.pcm
.as_mut()
.and_then(|pcm| {
pcm.status().ok().map(|status| {
let delay_frames = status.get_delay();
let frames_in_buffer = pcm.bytes_to_frames(buffer_len as isize);
(delay_frames + frames_in_buffer) as u64
})
})
.unwrap_or(0)
}
sink_as_bytes!();
}

View file

@ -30,6 +30,9 @@ pub trait Sink {
fn stop(&mut self) -> SinkResult<()> {
Ok(())
}
fn get_latency_pcm(&mut self) -> u64 {
0
}
fn write(&mut self, packet: AudioPacket, converter: &mut Converter) -> SinkResult<()>;
}

View file

@ -131,6 +131,17 @@ impl Sink for PulseAudioSink {
Ok(())
}
fn get_latency_pcm(&mut self) -> u64 {
self.sink
.as_mut()
.and_then(|sink| {
sink.get_latency()
.ok()
.map(|micro_sec| (micro_sec.as_secs_f64() * SAMPLE_RATE as f64).round() as u64)
})
.unwrap_or(0)
}
sink_as_bytes!();
}