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
4 changes: 4 additions & 0 deletions .github/workflows/cpal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
run: sudo apt-get install libasound2-dev
- name: Install libjack
run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0
- name: Install dbus
run: sudo apt-get install libdbus-1-dev
- name: Install stable
uses: dtolnay/rust-toolchain@stable
with:
Expand Down Expand Up @@ -67,6 +69,8 @@ jobs:
run: sudo apt-get install libasound2-dev
- name: Install libjack
run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0
- name: Install dbus
run: sudo apt-get install libdbus-1-dev
- name: Install stable
uses: dtolnay/rust-toolchain@stable
- name: Run without features
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ windows = { version = "0.54.0", features = [
"Win32_Media_Multimedia",
"Win32_UI_Shell_PropertiesSystem"
]}
audio_thread_priority = { version = "0.33.0", optional = true }
asio-sys = { version = "0.2", path = "asio-sys", optional = true }
num-traits = { version = "0.2.6", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.9"
libc = "0.2"
audio_thread_priority = { version = "0.33.0", optional = true }
jack = { version = "0.13.0", optional = true }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
Expand Down
23 changes: 23 additions & 0 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ fn input_stream_worker(
error_callback: &mut (dyn FnMut(StreamError) + Send + 'static),
timeout: Option<Duration>,
) {
boost_current_thread_priority(stream.conf.buffer_size, stream.conf.sample_rate);

let mut ctxt = StreamWorkerContext::new(&timeout);
loop {
let flow =
Expand Down Expand Up @@ -640,6 +642,8 @@ fn output_stream_worker(
error_callback: &mut (dyn FnMut(StreamError) + Send + 'static),
timeout: Option<Duration>,
) {
boost_current_thread_priority(stream.conf.buffer_size, stream.conf.sample_rate);

let mut ctxt = StreamWorkerContext::new(&timeout);
loop {
let flow =
Expand Down Expand Up @@ -684,6 +688,25 @@ fn output_stream_worker(
}
}

#[cfg(feature = "audio_thread_priority")]
fn boost_current_thread_priority(buffer_size: BufferSize, sample_rate: SampleRate) {
use audio_thread_priority::promote_current_thread_to_real_time;

let buffer_size = if let BufferSize::Fixed(buffer_size) = buffer_size {
buffer_size
} else {
// if the buffer size isn't fixed, let audio_thread_priority choose a sensible default value
0
};

if let Err(err) = promote_current_thread_to_real_time(buffer_size, sample_rate.0) {
eprintln!("Failed to promote audio thread to real-time priority: {err}");
}
}

#[cfg(not(feature = "audio_thread_priority"))]
fn boost_current_thread_priority(_: BufferSize, _: SampleRate) {}

enum PollDescriptorsFlow {
Continue,
Return,
Expand Down
36 changes: 30 additions & 6 deletions src/host/wasapi/stream.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use super::windows_err_to_cpal_err;
use crate::traits::StreamTrait;
use crate::{
BackendSpecificError, Data, InputCallbackInfo, OutputCallbackInfo, PauseStreamError,
PlayStreamError, SampleFormat, StreamError,
BackendSpecificError, BufferSize, Data, InputCallbackInfo, OutputCallbackInfo,
PauseStreamError, PlayStreamError, SampleFormat, StreamError,
};
use std::mem;
use std::ptr;
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
use std::thread::{self, JoinHandle};
use windows::Win32::Foundation;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Foundation::WAIT_OBJECT_0;
use windows::Win32::Media::Audio;
use windows::Win32::System::SystemServices;
Expand Down Expand Up @@ -269,7 +268,10 @@ fn run_input(
data_callback: &mut dyn FnMut(&Data, &InputCallbackInfo),
error_callback: &mut dyn FnMut(StreamError),
) {
boost_current_thread_priority();
boost_current_thread_priority(
run_ctxt.stream.config.buffer_size,
run_ctxt.stream.config.sample_rate,
);

loop {
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
Expand Down Expand Up @@ -298,7 +300,10 @@ fn run_output(
data_callback: &mut dyn FnMut(&mut Data, &OutputCallbackInfo),
error_callback: &mut dyn FnMut(StreamError),
) {
boost_current_thread_priority();
boost_current_thread_priority(
run_ctxt.stream.config.buffer_size,
run_ctxt.stream.config.sample_rate,
);

loop {
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
Expand All @@ -322,7 +327,26 @@ fn run_output(
}
}

fn boost_current_thread_priority() {
#[cfg(feature = "audio_thread_priority")]
fn boost_current_thread_priority(buffer_size: BufferSize, sample_rate: crate::SampleRate) {
use audio_thread_priority::promote_current_thread_to_real_time;

let buffer_size = if let BufferSize::Fixed(buffer_size) = buffer_size {
buffer_size
} else {
// if the buffer size isn't fixed, let audio_thread_priority choose a sensible default value
0
};

if let Err(err) = promote_current_thread_to_real_time(buffer_size, sample_rate.0) {
eprintln!("Failed to promote audio thread to real-time priority: {err}");
}
}

#[cfg(not(feature = "audio_thread_priority"))]
fn boost_current_thread_priority(_: BufferSize, _: crate::SampleRate) {
use windows::Win32::Foundation::HANDLE;

unsafe {
let thread_id = Threading::GetCurrentThreadId();

Expand Down