Free some additional structures from winapi properly
This commit is contained in:
@@ -16,7 +16,9 @@ use windows::{
|
||||
eRender, IAudioSessionControl2, IAudioSessionManager2, IAudioSessionNotification,
|
||||
IMMDeviceEnumerator, IMMNotificationClient, MMDeviceEnumerator, DEVICE_STATE_ACTIVE,
|
||||
},
|
||||
System::Com::{CoCreateInstance, CLSCTX_ALL, STGM_READ},
|
||||
System::Com::{
|
||||
CoCreateInstance, StructuredStorage::PropVariantClear, CLSCTX_ALL, STGM_READ,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
@@ -78,9 +80,7 @@ impl SMSessionNotifier {
|
||||
}),
|
||||
}),
|
||||
session_notification: IAudioSessionNotification::from(SessionNotification {
|
||||
observer: Box::new(SessionToMessage {
|
||||
sender
|
||||
}),
|
||||
observer: Box::new(SessionToMessage { sender }),
|
||||
}),
|
||||
notification_function: callback,
|
||||
receiver,
|
||||
@@ -98,7 +98,6 @@ impl SMSessionNotifier {
|
||||
let mmdevice = device?;
|
||||
self.add_device(mmdevice)?;
|
||||
}
|
||||
println!("All devices initialized.");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -117,9 +116,9 @@ impl SMSessionNotifier {
|
||||
self: &mut SMSessionNotifier,
|
||||
device: windows::Win32::Media::Audio::IMMDevice,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let sm: IAudioSessionManager2 = device.Activate(CLSCTX_ALL, None)?;
|
||||
sm.RegisterSessionNotification(&self.session_notification)?;
|
||||
let session_enumerator = sm.GetSessionEnumerator()?;
|
||||
let session_manager: IAudioSessionManager2 = device.Activate(CLSCTX_ALL, None)?;
|
||||
session_manager.RegisterSessionNotification(&self.session_notification)?;
|
||||
let session_enumerator = session_manager.GetSessionEnumerator()?;
|
||||
let session_count = session_enumerator.GetCount()?;
|
||||
let device_sessions = (0..session_count)
|
||||
.map(|idx| {
|
||||
@@ -132,13 +131,15 @@ impl SMSessionNotifier {
|
||||
(self.notification_function)(session);
|
||||
}
|
||||
let prop_store = device.OpenPropertyStore(STGM_READ)?;
|
||||
let prop_var = prop_store.GetValue(&PKEY_Device_FriendlyName)?;
|
||||
let mut prop_var = prop_store.GetValue(&PKEY_Device_FriendlyName)?;
|
||||
println!(
|
||||
"Device Added: {} Existing Sessions: {}",
|
||||
prop_var.Anonymous.Anonymous.Anonymous.pwszVal.to_string()?,
|
||||
session_count
|
||||
);
|
||||
self.session_managers.push(sm);
|
||||
PropVariantClear(&mut prop_var)?;
|
||||
|
||||
self.session_managers.push(session_manager);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -161,6 +162,21 @@ impl SMSessionNotifier {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for SMSessionNotifier {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
self.session_managers
|
||||
.drain(..)
|
||||
.map(|x| x.UnregisterSessionNotification(&self.session_notification))
|
||||
.collect::<Result<(), _>>()
|
||||
.unwrap();
|
||||
self.device_enumerator
|
||||
.UnregisterEndpointNotificationCallback(&self.device_notification_client)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SMSessionNotifierThread {
|
||||
handle: Option<JoinHandle<()>>,
|
||||
sender: Sender<SMMessage>,
|
||||
|
||||
Reference in New Issue
Block a user