Initializes the Media Foundation and COM subsystem This function performs one-time initialization of the Windows Media Foundation and COM runtime. It uses reference counting to ensure proper lifecycle management. Multiple calls are safe; only the first call performs actual initialization. Uses compare_exchange for efficient lock-free initialization.
()
| 46 | /// |
| 47 | /// Uses compare_exchange for efficient lock-free initialization. |
| 48 | pub fn initialize_mf() -> Result<()> { |
| 49 | if INITIALIZED |
| 50 | .compare_exchange(false, true, Ordering::SeqCst, Ordering::Relaxed) |
| 51 | .is_ok() |
| 52 | { |
| 53 | // We won the race to initialize |
| 54 | unsafe { |
| 55 | if let Err(e) = CoInitializeEx(None, COINIT_APARTMENTTHREADED).ok() { |
| 56 | INITIALIZED.store(false, Ordering::Release); |
| 57 | return Err(CameraError::Io(std::io::Error::other(e.to_string()))); |
| 58 | } |
| 59 | |
| 60 | if let Err(e) = MFStartup(MF_API_VERSION, MFSTARTUP_NOSOCKET) { |
| 61 | // Clean up COM if MF startup fails |
| 62 | CoUninitialize(); |
| 63 | INITIALIZED.store(false, Ordering::Release); |
| 64 | return Err(CameraError::Io(std::io::Error::other(e.to_string()))); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | // Increment reference count (use Release for synchronization with shutdown) |
| 69 | CAMERA_REFCNT.fetch_add(1, Ordering::Release); |
| 70 | Ok(()) |
| 71 | } |
| 72 | |
| 73 | /// Shuts down the Media Foundation subsystem |
| 74 | /// |
no outgoing calls
no test coverage detected