Skip to content
Merged
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
9 changes: 2 additions & 7 deletions src/host/audioworklet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
DeviceDirection, DeviceId, DuplexCallbackInfo, DuplexStreamConfig, Error, ErrorKind,
FrameCount, Sample, SampleFormat, SampleRate, StreamConfig, StreamInstant, StreamTimestamp,
SupportedBufferSize, SupportedStreamConfig, SupportedStreamConfigRange,
host::frames_to_duration,
host::{frames_to_duration, secs_to_nanos},
traits::{DeviceTrait, HostTrait, StreamTrait},
};

Expand Down Expand Up @@ -1369,10 +1369,5 @@ fn total_latency_nanos(ctx: &web_sys::AudioContext) -> u64 {
.unwrap_or(0.0)
};
// `baseLatency` is fixed for the context lifetime; `outputLatency` can change.
let secs = read("baseLatency") + read("outputLatency");
if secs.is_finite() && secs > 0.0 {
(secs * 1_000_000_000.0).round() as u64
} else {
0
}
secs_to_nanos(read("baseLatency") + read("outputLatency"))
}
25 changes: 23 additions & 2 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,16 @@ pub(crate) const fn frames_to_duration(
/// Converts a duration in seconds, as reported by the platform's audio session, to nanoseconds.
///
/// Returns 0 for a value that is not finite and positive.
#[cfg(all(target_vendor = "apple", not(target_os = "macos")))]
#[cfg(any(
all(target_vendor = "apple", not(target_os = "macos")),
all(
target_arch = "wasm32",
target_os = "unknown",
feature = "wasm-bindgen",
feature = "audioworklet",
target_feature = "atomics"
),
))]
#[inline]
pub(crate) fn secs_to_nanos(secs: f64) -> u64 {
if secs.is_finite() && secs > 0.0 {
Expand All @@ -294,7 +303,19 @@ pub(crate) fn secs_to_nanos(secs: f64) -> u64 {
/// `Some(Duration::ZERO)` returns immediately.
///
/// [`StreamTrait::stop`]: crate::traits::StreamTrait::stop
#[cfg(any(all(windows, feature = "asio"), target_vendor = "apple"))]
#[cfg(any(
all(windows, feature = "asio"),
target_vendor = "apple",
all(
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
),
any(feature = "jack", feature = "pulseaudio"),
),
))]
pub(crate) fn wait_for_drain(window: std::time::Duration, timeout: Option<std::time::Duration>) {
let wait = timeout.map_or(window, |t| window.min(t));
if !wait.is_zero() {
Expand Down
13 changes: 5 additions & 8 deletions src/host/pulseaudio/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use pulseaudio::{AsPlaybackSource, protocol};

use crate::{
CallbackInfo, Data, Error, ErrorKind, FrameCount, SampleFormat, StreamInstant, StreamTimestamp,
host::{ErrorCallbackArc, emit_error, latch::Latch},
host::{ErrorCallbackArc, emit_error, latch::Latch, wait_for_drain},
traits::StreamTrait,
};

Expand Down Expand Up @@ -152,13 +152,10 @@ impl StreamTrait for Stream {
} => {
// TODO: use PulseAudio's drain() when https://github.com/colinmarc/pulseaudio-rs/pull/9 is merged.
draining.store(true, Ordering::Relaxed);
if timeout != Some(Duration::ZERO) {
let buffered = Duration::from_micros(fill_usec.load(Ordering::Relaxed));
let wait = timeout.map_or(buffered, |t| buffered.min(t));
if !wait.is_zero() {
std::thread::sleep(wait);
}
}
wait_for_drain(
Duration::from_micros(fill_usec.load(Ordering::Relaxed)),
timeout,
);
block_on(stream.cork()).map_err(Error::from)?;
block_on(stream.flush()).map_err(Error::from)?;
handle.notify();
Expand Down
Loading