Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion .github/actions/determine-msrv/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ inputs:
description: The MSRV for Pipewire backend (optional, Linux only)
required: false
default: ''
wasapi-winrt-msrv:
description: The MSRV for the wasapi-winrt feature (optional, Windows only)
required: false
default: ''

outputs:
all-features:
Expand All @@ -33,6 +37,7 @@ runs:
JACK_MSRV="${{ inputs.jack-msrv }}"
PULSEAUDIO_MSRV="${{ inputs.pulseaudio-msrv }}"
PIPEWIRE_MSRV="${{ inputs.pipewire-msrv }}"
WASAPI_WINRT_MSRV="${{ inputs.wasapi-winrt-msrv }}"
# Use sort -V to find the maximum version
VERSIONS="$PLATFORM_MSRV $JACK_MSRV"
if [ -n "$PULSEAUDIO_MSRV" ]; then
Expand All @@ -41,6 +46,9 @@ runs:
if [ -n "$PIPEWIRE_MSRV" ]; then
VERSIONS="$VERSIONS $PIPEWIRE_MSRV"
fi
if [ -n "$WASAPI_WINRT_MSRV" ]; then
VERSIONS="$VERSIONS $WASAPI_WINRT_MSRV"
fi
MAX_MSRV=$(printf '%s\n' $VERSIONS | sort -V | tail -n1)
echo "all-features=$MAX_MSRV" >> $GITHUB_OUTPUT
echo "Platform MSRV: $PLATFORM_MSRV, JACK MSRV: $JACK_MSRV, PulseAudio MSRV: $PULSEAUDIO_MSRV, PIPEWIRE_MSRV: $PIPEWIRE_MSRV, Using for --all-features: $MAX_MSRV"
echo "Platform MSRV: $PLATFORM_MSRV, JACK MSRV: $JACK_MSRV, PulseAudio MSRV: $PULSEAUDIO_MSRV, PIPEWIRE_MSRV: $PIPEWIRE_MSRV, WASAPI-winrt MSRV: $WASAPI_WINRT_MSRV, Using for --all-features: $MAX_MSRV"
2 changes: 2 additions & 0 deletions .github/workflows/platforms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ env:
MSRV_PULSEAUDIO: "1.88"
MSRV_WASM: "1.85"
MSRV_WINDOWS: "1.85"
MSRV_WASAPI_WINRT: "1.95"

PACKAGES_LINUX: libasound2-dev libdbus-1-dev libjack-jackd2-dev libpipewire-0.3-dev

Expand Down Expand Up @@ -245,6 +246,7 @@ jobs:
with:
platform-msrv: ${{ env.MSRV_WINDOWS }}
jack-msrv: ${{ env.MSRV_JACK }}
wasapi-winrt-msrv: ${{ env.MSRV_WASAPI_WINRT }}

- name: Install Rust MSRV (${{ env.MSRV_WINDOWS }})
uses: dtolnay/rust-toolchain@master
Expand Down
14 changes: 14 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pulseaudio = [
"dep:portable-atomic",
]

# New WASAPI backend using WinRT
# Platform: Windows
wasapi-winrt = ["dep:tap", "dep:parking_lot", "dep:wasapi", "dep:itertools"]

# WebAssembly backend using wasm-bindgen
# Enables the Web Audio API backend for browser-based audio
# Required for any WebAssembly audio support
Expand All @@ -104,6 +108,7 @@ wasm-bindgen = [

[dependencies]
dasp_sample = "0.11"
windows-collections = "0.3.2"

[dev-dependencies]
anyhow = "1.0"
Expand All @@ -128,10 +133,19 @@ windows = { version = "0.62", features = [
"Win32_System_Variant",
"Win32_Media_Multimedia",
"Win32_UI_Shell_PropertiesSystem",
"Devices_Enumeration",
"Media_Devices",
"Media_Audio",
"Media_Render",
"Media_MediaProperties"
] }
wasapi = { version = "0.27.0", optional = true }
itertools = { version = "0.15.0", optional = true }
audio_thread_priority = { version = "0.36", optional = true, default-features = false }
asio-sys = { version = "0.5.0", path = "asio-sys", optional = true }
num-traits = { version = "0.2", optional = true }
tap = { version = "1.0.1", optional = true }
parking_lot = { version = "0.13.0", optional = true }
jack = { version = "0.13.5", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The minimum Rust version (MSRV) and minimum operating system / runtime version b
| PipeWire | Linux, BSD | 1.85 | PipeWire 0.3.53 |
| PulseAudio | Linux, BSD | 1.88 | — |
| WASAPI / ASIO | Windows | 1.85 | Windows 10 |
| New WASAPI (`-F wasapi-winrt`) | Windows | 1.95 | Windows 10 |
| WASM (`wasm32-unknown-unknown`) | WebAssembly | 1.85 | — |
| WASM (`wasm32-unknown-emscripten`) | WebAssembly | 1.85 | Emscripten 6.0.3, wasm-bindgen 0.2.127 |
| WASM (`audioworklet`) | WebAssembly | nightly | — |
Expand All @@ -70,6 +71,7 @@ The `audioworklet` backend additionally requires `-Zbuild-std` with atomics supp
| `pulseaudio` | Linux, BSD | PulseAudio sound server backend. Requires `libpulse-dev` (Debian/Ubuntu) or `pulseaudio-libs-devel` (Fedora). |
| `realtime` | Android, Linux, Windows | Raises the audio callback thread to real-time or high-priority scheduling for lower latency. On Linux, requires `CAP_SYS_NICE`, root, or an `rtprio` limit granted via `limits.conf` or systemd, unless `realtime-dbus` is also enabled. |
| `realtime-dbus` | Linux | Uses `rtkit` via D-Bus for RT scheduling on Linux desktop systems. Implies `realtime` on all platforms. Requires `libdbus-1-dev` on Linux. |
| `wasapi-winrt` | Windows | Rewrite of the WASAPI host, intended to replace the current one soon. |
| `wasm-bindgen` | WebAssembly (`wasm32-unknown-emscripten`, `wasm32-unknown-unknown`) | Web Audio API backend for browser-based audio; required for any WebAssembly audio support. See the `webaudio` example. |

See the [beep example](examples/beep.rs) for selecting the backend at runtime.
Expand Down
39 changes: 39 additions & 0 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub(crate) mod pulseaudio;
#[cfg(windows)]
pub(crate) mod wasapi;

#[rustfmt::skip]
#[cfg(all(windows, feature = "wasapi-winrt"))]
pub(crate) mod wasapi_winrt;

#[cfg(all(
target_arch = "wasm32",
any(target_os = "emscripten", target_os = "unknown"),
Expand Down Expand Up @@ -452,3 +456,38 @@ pub(crate) fn stop_tracks(media_stream: &web_sys::MediaStream) {
}
}
}

/// Holds the MMCSS registration made by `audio_thread_priority` and reverts it on drop.
#[cfg(all(windows, feature = "realtime"))]
#[derive(Default)]
struct RtPriority(Option<audio_thread_priority::RtPriorityHandle>);

#[cfg(all(windows, feature = "realtime"))]
impl Drop for RtPriority {
fn drop(&mut self) {
if let Some(handle) = self.0.take() {
// Nothing useful to do if reverting fails while the thread is exiting.
let _ = audio_thread_priority::demote_current_thread_from_real_time(handle);
}
}
}

/// Attempts to elevate the current thread to real-time or high-priority scheduling.
#[cfg(all(windows, feature = "realtime"))]
fn boost_current_thread_priority(
period_frames: crate::FrameCount,
sample_rate: crate::SampleRate,
) -> Result<RtPriority, crate::Error> {
use crate::ResultExt;
use windows::Win32::System::Threading;

match audio_thread_priority::promote_current_thread_to_real_time(period_frames, sample_rate) {
Ok(handle) => Ok(RtPriority(Some(handle))),
Err(_) => unsafe {
let thread_handle = Threading::GetCurrentThread();
Threading::SetThreadPriority(thread_handle, Threading::THREAD_PRIORITY_TIME_CRITICAL)
.map(|()| RtPriority::default())
.context("Failed to promote audio thread to real-time priority")
},
}
}
35 changes: 3 additions & 32 deletions src/host/wasapi/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use crate::{
traits::StreamTrait,
};

#[cfg(feature = "realtime")]
use crate::host::boost_current_thread_priority;

/// Returns the current default audio endpoint for `flow`, or `None` if none exists.
///
/// Used by `OnDeviceStateChanged` and `OnDeviceRemoved` to cover the edge case where the
Expand Down Expand Up @@ -797,38 +800,6 @@ fn run_output(
}
}

/// Holds the MMCSS registration made by `audio_thread_priority` and reverts it on drop.
#[cfg(feature = "realtime")]
#[derive(Default)]
struct RtPriority(Option<audio_thread_priority::RtPriorityHandle>);

#[cfg(feature = "realtime")]
impl Drop for RtPriority {
fn drop(&mut self) {
if let Some(handle) = self.0.take() {
// Nothing useful to do if reverting fails while the thread is exiting.
let _ = audio_thread_priority::demote_current_thread_from_real_time(handle);
}
}
}

/// Attempts to elevate the current thread to real-time or high-priority scheduling.
#[cfg(feature = "realtime")]
fn boost_current_thread_priority(
period_frames: FrameCount,
sample_rate: SampleRate,
) -> Result<RtPriority, Error> {
match audio_thread_priority::promote_current_thread_to_real_time(period_frames, sample_rate) {
Ok(handle) => Ok(RtPriority(Some(handle))),
Err(_) => unsafe {
let thread_handle = Threading::GetCurrentThread();
Threading::SetThreadPriority(thread_handle, Threading::THREAD_PRIORITY_TIME_CRITICAL)
.map(|()| RtPriority::default())
.context("Failed to promote audio thread to real-time priority")
},
}
}

fn default_device_change_error(flow: Option<Audio::EDataFlow>) -> Error {
match flow.and_then(get_current_default) {
None => ErrorKind::DeviceNotAvailable.into(),
Expand Down
Loading
Loading