Skip to content

Add method to query latency from Stream #964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/host/coreaudio/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub use self::enumerate::{
SupportedOutputConfigs,
};

use coreaudio::sys::{
kAudioDevicePropertyLatency, kAudioDevicePropertySafetyOffset, kAudioStreamPropertyLatency,
};
use property_listener::AudioObjectPropertyListener;

pub mod enumerate;
Expand Down Expand Up @@ -956,6 +959,55 @@ impl StreamTrait for Stream {

stream.pause()
}

fn latency(&self) -> Option<u32> {
let stream = self.inner.lock().unwrap();
let audio_unit = &stream.audio_unit;

// device presentation latency
let device_latency: u32 = match audio_unit.get_property(
kAudioDevicePropertyLatency,
Scope::Global,
Element::Output,
) {
Ok(device_latency) => device_latency,
Err(_) => return None,
};

// stream presentation latency
let stream_latency: u32 = match audio_unit.get_property(
kAudioStreamPropertyLatency,
Scope::Global,
Element::Output,
) {
Ok(stream_latency) => stream_latency,
Err(_) => return None,
};

// device safety offset
let safety_offset: u32 = match audio_unit.get_property(
kAudioDevicePropertySafetyOffset,
Scope::Global,
Element::Output,
) {
Ok(safety_offset) => safety_offset,
Err(_) => return None,
};

// IO buffer frame size
let buffer_size: u32 = match audio_unit.get_property(
kAudioDevicePropertyBufferFrameSize,
Scope::Global,
Element::Output,
) {
Ok(buffer_size) => buffer_size,
Err(_) => return None,
};

let latency = device_latency + stream_latency + safety_offset + buffer_size;

Some(latency)
}
}

fn get_io_buffer_frame_size_range(
Expand Down
11 changes: 11 additions & 0 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,17 @@ macro_rules! impl_platform_host {
)*
}
}

fn latency(&self) -> Option<u32> {
match self.0 {
$(
$(#[cfg($feat)])?
StreamInner::$HostVariant(ref s) => {
s.latency()
}
)*
}
}
}

impl From<DeviceInner> for Device {
Expand Down
8 changes: 8 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,12 @@ pub trait StreamTrait {
/// Note: Not all devices support suspending the stream at the hardware level. This method may
/// fail in these cases.
fn pause(&self) -> Result<(), PauseStreamError>;

/// The current latency of the stream in samples (if available).
///
/// Note: This is not implemented on all platforms and not all backends return reliable
/// information.
fn latency(&self) -> Option<u32> {
None
}
}
Loading