use windows::{
core::Owned,
Win32::{
Media::MediaFoundation::{MFShutdown, MFStartup, MFSTARTUP_FULL, MF_VERSION},
System::Com::{CoIncrementMTAUsage, CO_MTA_USAGE_COOKIE},
},
};
use crate::VideoError;
pub(crate) struct MediaFoundationRuntime {
_mta: Owned<CO_MTA_USAGE_COOKIE>,
}
impl MediaFoundationRuntime {
pub(crate) fn new() -> Result<Self, VideoError> {
let cookie = unsafe { CoIncrementMTAUsage() }
.map_err(|error| platform_error("CoIncrementMTAUsage", error))?;
if let Err(error) = unsafe { MFStartup(MF_VERSION, MFSTARTUP_FULL) } {
drop(unsafe { Owned::new(cookie) });
return Err(platform_error("MFStartup", error));
}
Ok(Self {
_mta: unsafe { Owned::new(cookie) },
})
}
}
impl Drop for MediaFoundationRuntime {
fn drop(&mut self) {
let _ = unsafe { MFShutdown() };
}
}
pub(crate) fn platform_error(api: &'static str, error: windows::core::Error) -> VideoError {
VideoError::Platform {
api,
status: error.code().0,
}
}