fix(wasapi): use a thread handle when setting audio priority
This commit is contained in:
parent
6b374bcaed
commit
c2657388dc
2 changed files with 76 additions and 11 deletions
|
|
@ -9,7 +9,6 @@ use std::ptr;
|
||||||
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
|
use std::sync::mpsc::{channel, Receiver, SendError, Sender};
|
||||||
use std::thread::{self, JoinHandle};
|
use std::thread::{self, JoinHandle};
|
||||||
use windows::Win32::Foundation;
|
use windows::Win32::Foundation;
|
||||||
use windows::Win32::Foundation::HANDLE;
|
|
||||||
use windows::Win32::Foundation::WAIT_OBJECT_0;
|
use windows::Win32::Foundation::WAIT_OBJECT_0;
|
||||||
use windows::Win32::Media::Audio;
|
use windows::Win32::Media::Audio;
|
||||||
use windows::Win32::System::SystemServices;
|
use windows::Win32::System::SystemServices;
|
||||||
|
|
@ -18,8 +17,6 @@ use windows::Win32::System::Threading;
|
||||||
pub struct Stream {
|
pub struct Stream {
|
||||||
/// The high-priority audio processing thread calling callbacks.
|
/// The high-priority audio processing thread calling callbacks.
|
||||||
/// Option used for moving out in destructor.
|
/// Option used for moving out in destructor.
|
||||||
///
|
|
||||||
/// TODO: Actually set the thread priority.
|
|
||||||
thread: Option<JoinHandle<()>>,
|
thread: Option<JoinHandle<()>>,
|
||||||
|
|
||||||
// Commands processed by the `run()` method that is currently running.
|
// Commands processed by the `run()` method that is currently running.
|
||||||
|
|
@ -269,7 +266,9 @@ fn run_input(
|
||||||
data_callback: &mut dyn FnMut(&Data, &InputCallbackInfo),
|
data_callback: &mut dyn FnMut(&Data, &InputCallbackInfo),
|
||||||
error_callback: &mut dyn FnMut(StreamError),
|
error_callback: &mut dyn FnMut(StreamError),
|
||||||
) {
|
) {
|
||||||
boost_current_thread_priority();
|
if let Err(err) = boost_current_thread_priority() {
|
||||||
|
error_callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
|
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
|
||||||
|
|
@ -298,7 +297,9 @@ fn run_output(
|
||||||
data_callback: &mut dyn FnMut(&mut Data, &OutputCallbackInfo),
|
data_callback: &mut dyn FnMut(&mut Data, &OutputCallbackInfo),
|
||||||
error_callback: &mut dyn FnMut(StreamError),
|
error_callback: &mut dyn FnMut(StreamError),
|
||||||
) {
|
) {
|
||||||
boost_current_thread_priority();
|
if let Err(err) = boost_current_thread_priority() {
|
||||||
|
error_callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
|
match process_commands_and_await_signal(&mut run_ctxt, error_callback) {
|
||||||
|
|
@ -322,17 +323,21 @@ fn run_output(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn boost_current_thread_priority() {
|
// Priority boosting remains best-effort; callers report failure without stopping the stream.
|
||||||
|
fn boost_current_thread_priority() -> Result<(), StreamError> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let thread_id = Threading::GetCurrentThreadId();
|
Threading::SetThreadPriority(
|
||||||
|
Threading::GetCurrentThread(),
|
||||||
let _ = Threading::SetThreadPriority(
|
|
||||||
HANDLE(thread_id as isize),
|
|
||||||
Threading::THREAD_PRIORITY_TIME_CRITICAL,
|
Threading::THREAD_PRIORITY_TIME_CRITICAL,
|
||||||
);
|
)
|
||||||
}
|
}
|
||||||
|
.map_err(|err| super::windows_err_to_cpal_err_message(err, "SetThreadPriority failed: "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "stream_priority_tests.rs"]
|
||||||
|
mod priority_tests;
|
||||||
|
|
||||||
enum ControlFlow {
|
enum ControlFlow {
|
||||||
Break,
|
Break,
|
||||||
Continue,
|
Continue,
|
||||||
|
|
|
||||||
60
src/host/wasapi/stream_priority_tests.rs
Normal file
60
src/host/wasapi/stream_priority_tests.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
use super::boost_current_thread_priority;
|
||||||
|
use windows::Win32::System::Threading;
|
||||||
|
|
||||||
|
const THREAD_PRIORITY_ERROR_RETURN: i32 = i32::MAX;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boost_changes_the_calling_thread_priority() {
|
||||||
|
assert_boost_from(Threading::THREAD_PRIORITY_NORMAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boost_works_from_below_normal_priority() {
|
||||||
|
assert_boost_from(Threading::THREAD_PRIORITY_BELOW_NORMAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boost_works_from_above_normal_priority() {
|
||||||
|
assert_boost_from(Threading::THREAD_PRIORITY_ABOVE_NORMAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn boost_preserves_time_critical_priority() {
|
||||||
|
assert_boost_from(Threading::THREAD_PRIORITY_TIME_CRITICAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn assert_boost_from(initial: Threading::THREAD_PRIORITY) {
|
||||||
|
let parent = unsafe { Threading::GetCurrentThread() };
|
||||||
|
let parent_priority = unsafe { Threading::GetThreadPriority(parent) };
|
||||||
|
assert_ne!(parent_priority, THREAD_PRIORITY_ERROR_RETURN);
|
||||||
|
std::thread::spawn(move || unsafe {
|
||||||
|
let thread = Threading::GetCurrentThread();
|
||||||
|
let original = Threading::GetThreadPriority(thread);
|
||||||
|
assert_ne!(original, THREAD_PRIORITY_ERROR_RETURN);
|
||||||
|
Threading::SetThreadPriority(thread, initial)
|
||||||
|
.expect("could not establish initial priority");
|
||||||
|
|
||||||
|
let result = boost_current_thread_priority();
|
||||||
|
let observed = Threading::GetThreadPriority(thread);
|
||||||
|
let repeated = boost_current_thread_priority();
|
||||||
|
let observed_again = Threading::GetThreadPriority(thread);
|
||||||
|
let restored = Threading::SetThreadPriority(thread, Threading::THREAD_PRIORITY(original));
|
||||||
|
|
||||||
|
restored.expect("could not restore the original priority");
|
||||||
|
result.expect("priority boost failed");
|
||||||
|
repeated.expect("repeated priority boost failed");
|
||||||
|
assert_eq!(
|
||||||
|
observed,
|
||||||
|
Threading::THREAD_PRIORITY_TIME_CRITICAL.0,
|
||||||
|
"the production priority helper did not boost its calling thread"
|
||||||
|
);
|
||||||
|
assert_eq!(observed_again, observed);
|
||||||
|
assert_eq!(Threading::GetThreadPriority(thread), original);
|
||||||
|
})
|
||||||
|
.join()
|
||||||
|
.expect("priority regression thread failed");
|
||||||
|
assert_eq!(
|
||||||
|
unsafe { Threading::GetThreadPriority(parent) },
|
||||||
|
parent_priority
|
||||||
|
);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue