Skip to content
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ default = ["realtime-dbus"]
# Promotes audio callback threads to real-time or high-priority scheduling for lower latency
# Requires: (Linux/BSD) `rtprio` granted in `limits.conf` (e.g. `@audio - rtprio 95`)
# Platform: Linux, DragonFly BSD, FreeBSD, NetBSD, Windows, Android
realtime = ["dep:audio_thread_priority"]
realtime = ["dep:audio_thread_priority", "dep:alsa-sys"]

# D-Bus/rtkit support on top of `realtime` for RT scheduling on Linux/BSD desktop systems
# Requires: D-Bus development libraries (libdbus-1-dev or equivalent)
Expand Down Expand Up @@ -115,6 +115,7 @@ jack = { version = "0.13", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.11"
alsa-sys = { version = "0.4", optional = true }
libc = "0.2"
audio_thread_priority = { version = "0.35", optional = true, default-features = false }
jack = { version = "0.13", optional = true }
Expand Down
44 changes: 44 additions & 0 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! Default backend on Linux and BSD systems.

extern crate alsa;
#[cfg(feature = "realtime")]
extern crate alsa_sys;
extern crate libc;

use std::{
Expand Down Expand Up @@ -988,6 +990,48 @@ fn output_stream_worker(
fn boost_current_thread_priority(
stream: &StreamInner,
) -> Result<audio_thread_priority::RtPriorityHandle, Error> {
use alsa_sys::*;
// SAFETY: `alsa::pcm::PCM` is `pub struct PCM(*mut snd_pcm_t, Cell<bool>)`. The crate
// does not expose a public `as_ptr()`, but we can cast and read from it.
// TODO: replace with `stream.handle.as_ptr()` once alsa-rs exposes it publicly.
let raw = unsafe {
(&stream.handle as *const alsa::pcm::PCM)
.cast::<*mut snd_pcm_t>()
.read()
};
Comment thread
roderickvd marked this conversation as resolved.
Comment thread
roderickvd marked this conversation as resolved.
let pcm_type = unsafe { snd_pcm_type(raw) };

// Only promote to RT for kernel-backed and pure-computation plugins. Others can exhaust
// RLIMIT_RTTIME when they block or coordinate with non-RT servers and trigger SIGXCPU
// on an RT thread.
if !matches!(
pcm_type,
SND_PCM_TYPE_HW
| SND_PCM_TYPE_HOOKS
| SND_PCM_TYPE_NULL
| SND_PCM_TYPE_COPY
| SND_PCM_TYPE_LINEAR
| SND_PCM_TYPE_ALAW
| SND_PCM_TYPE_MULAW
| SND_PCM_TYPE_ADPCM
| SND_PCM_TYPE_RATE
| SND_PCM_TYPE_ROUTE
| SND_PCM_TYPE_PLUG
| SND_PCM_TYPE_LINEAR_FLOAT
| SND_PCM_TYPE_IEC958
| SND_PCM_TYPE_SOFTVOL
) {
let type_name = unsafe {
std::ffi::CStr::from_ptr(snd_pcm_type_name(pcm_type))
.to_str()
.unwrap_or("unknown")
};
return Err(Error::with_message(
ErrorKind::RealtimeDenied,
format!("PCM type '{type_name}' is not eligible for real-time promotion"),
));
}

let period_frames = u32::try_from(stream.period_size).unwrap_or(0);
audio_thread_priority::promote_current_thread_to_real_time(period_frames, stream.sample_rate)
.map_err(Error::from)
Expand Down
Loading