Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mediafoundation-utils): add MMCSS audio thread priority handle

Richie McIlroyd695873b53bb
diff --git a/crates/mediafoundation-utils/Cargo.toml b/crates/mediafoundation-utils/Cargo.toml
index b802602c5..a9b016878 100644
--- a/crates/mediafoundation-utils/Cargo.toml
+++ b/crates/mediafoundation-utils/Cargo.toml
@@ -12,6 +12,7 @@ windows = { workspace = true, features = [
"Win32_Media_MediaFoundation",
"Win32_Media_DirectShow",
"Win32_System_Com",
+ "Win32_System_Threading",
] }
[lints]
diff --git a/crates/mediafoundation-utils/src/lib.rs b/crates/mediafoundation-utils/src/lib.rs
index 3ead730fc..084f09e36 100644
--- a/crates/mediafoundation-utils/src/lib.rs
+++ b/crates/mediafoundation-utils/src/lib.rs
@@ -6,13 +6,16 @@ use std::{
};
use windows::{
Win32::{
+ Foundation::HANDLE,
Media::MediaFoundation::{IMFMediaBuffer, MFSTARTUP_FULL, MFStartup},
- System::WinRT::{RO_INIT_MULTITHREADED, RoInitialize},
+ System::{
+ Threading::{AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsW},
+ WinRT::{RO_INIT_MULTITHREADED, RoInitialize},
+ },
},
- core::Result,
+ core::{PCWSTR, Result},
};
-// This is the value for Win7+
pub const MF_VERSION: u32 = 131184;
pub fn thread_init() {
@@ -20,6 +23,30 @@ pub fn thread_init() {
let _ = unsafe { MFStartup(MF_VERSION, MFSTARTUP_FULL) };
}
+pub struct MmcssAudioHandle {
+ handle: HANDLE,
+}
+
+impl MmcssAudioHandle {
+ pub fn register_audio() -> Option<Self> {
+ let task_name: Vec<u16> = "Audio\0".encode_utf16().collect();
+ let mut task_index: u32 = 0;
+ match unsafe { AvSetMmThreadCharacteristicsW(PCWSTR(task_name.as_ptr()), &mut task_index) }
+ {
+ Ok(handle) if !handle.is_invalid() => Some(Self { handle }),
+ _ => None,
+ }
+ }
+}
+
+impl Drop for MmcssAudioHandle {
+ fn drop(&mut self) {
+ if !self.handle.is_invalid() {
+ let _ = unsafe { AvRevertMmThreadCharacteristics(self.handle) };
+ }
+ }
+}
+
pub trait IMFMediaBufferExt {
fn lock(&self) -> Result<IMFMediaBufferLock<'_>>;
fn lock_for_write(&self) -> Result<IMFMediaBufferLock<'_>>;