1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
use windows_sys::s;
use windows_sys::Win32::Foundation::GetLastError;
use windows_sys::Win32::Foundation::FALSE;
use windows_sys::Win32::Foundation::HANDLE;
use windows_sys::Win32::System::Threading::{
AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsA,
};
use crate::AudioThreadPriorityError;
use log::info;
#[derive(Debug)]
pub struct RtPriorityHandleInternal {
mmcss_task_index: u32,
task_handle: HANDLE,
}
impl RtPriorityHandleInternal {
pub fn new(mmcss_task_index: u32, task_handle: HANDLE) -> RtPriorityHandleInternal {
RtPriorityHandleInternal {
mmcss_task_index,
task_handle,
}
}
}
pub fn demote_current_thread_from_real_time_internal(
rt_priority_handle: RtPriorityHandleInternal,
) -> Result<(), AudioThreadPriorityError> {
let rv = unsafe { AvRevertMmThreadCharacteristics(rt_priority_handle.task_handle) };
if rv == FALSE {
return Err(AudioThreadPriorityError::new(&format!(
"Unable to restore the thread priority ({:?})",
unsafe { GetLastError() }
)));
}
info!(
"task {} priority restored.",
rt_priority_handle.mmcss_task_index
);
Ok(())
}
pub fn promote_current_thread_to_real_time_internal(
_audio_buffer_frames: u32,
_audio_samplerate_hz: u32,
) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> {
let mut task_index = 0u32;
let handle = unsafe { AvSetMmThreadCharacteristicsA(s!("Audio"), &mut task_index) };
let handle = RtPriorityHandleInternal::new(task_index, handle);
if handle.task_handle == 0 {
return Err(AudioThreadPriorityError::new(&format!(
"Unable to restore the thread priority ({:?})",
unsafe { GetLastError() }
)));
}
info!(
"task {} bumped to real time priority.",
handle.mmcss_task_index
);
Ok(handle)
}
|