Code cleanup part 2: Huge ass refactor edition
This commit is contained in:
116
src/sm_session_notifier.rs
Normal file
116
src/sm_session_notifier.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use std::{
|
||||
error::Error,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
device_notification_client::DeviceNotificationClient, session_notification::SessionNotification,
|
||||
};
|
||||
use widestring::U16CStr;
|
||||
use windows::{
|
||||
core::{Interface, PCWSTR},
|
||||
Win32::{
|
||||
Devices::FunctionDiscovery::PKEY_Device_FriendlyName,
|
||||
Media::Audio::{
|
||||
eRender, IAudioSessionControl2, IAudioSessionManager2, IAudioSessionNotification,
|
||||
IMMDeviceEnumerator, IMMNotificationClient, MMDeviceEnumerator, DEVICE_STATE_ACTIVE,
|
||||
},
|
||||
System::Com::{CoCreateInstance, CLSCTX_ALL, STGM_READ},
|
||||
},
|
||||
};
|
||||
pub(crate) struct SMSessionNotifier {
|
||||
device_enumerator: IMMDeviceEnumerator,
|
||||
device_notification_client: IMMNotificationClient,
|
||||
session_notification: IAudioSessionNotification,
|
||||
session_managers: Vec<IAudioSessionManager2>,
|
||||
notification_function: Box<dyn Fn(IAudioSessionControl2)>,
|
||||
}
|
||||
|
||||
impl SMSessionNotifier {
|
||||
pub(crate) fn new(
|
||||
callback: Box<dyn Fn(IAudioSessionControl2)>,
|
||||
) -> Arc<Mutex<Option<SMSessionNotifier>>> {
|
||||
let session_notifier_arc: Arc<Mutex<Option<SMSessionNotifier>>> =
|
||||
Arc::new(Mutex::new(None));
|
||||
let sn2 = session_notifier_arc.clone();
|
||||
let sn3 = session_notifier_arc.clone();
|
||||
let notifier = SMSessionNotifier {
|
||||
session_managers: Vec::new(),
|
||||
device_enumerator: unsafe {
|
||||
CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).unwrap()
|
||||
},
|
||||
device_notification_client: IMMNotificationClient::from(DeviceNotificationClient {
|
||||
callback: Box::new(move |id| {
|
||||
match sn2.lock().unwrap().as_mut().unwrap().add_device_by_id(id) {
|
||||
Ok(_) => {},
|
||||
Err(e) => {
|
||||
println!("Failed to add new device: {:?}", e);
|
||||
},
|
||||
}
|
||||
}),
|
||||
}),
|
||||
session_notification: IAudioSessionNotification::from(SessionNotification {
|
||||
callback: Box::new(move |session| {
|
||||
(sn3.lock().unwrap().as_mut().unwrap().notification_function)(session)
|
||||
}),
|
||||
}),
|
||||
notification_function: callback,
|
||||
};
|
||||
*session_notifier_arc.lock().unwrap() = Some(notifier);
|
||||
return session_notifier_arc.clone();
|
||||
}
|
||||
|
||||
pub(crate) fn boot_devices(self: &mut SMSessionNotifier) -> Result<(), Box<dyn Error>> {
|
||||
unsafe {
|
||||
self.device_enumerator
|
||||
.RegisterEndpointNotificationCallback(&self.device_notification_client)?;
|
||||
let device_collection = self
|
||||
.device_enumerator
|
||||
.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE)?;
|
||||
for device in (0..device_collection.GetCount()?).map(|x| device_collection.Item(x)) {
|
||||
let mmdevice = device?;
|
||||
self.add_device(mmdevice)?;
|
||||
}
|
||||
println!("All devices initialized.");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
fn add_device_by_id(self: &mut SMSessionNotifier, id: &PCWSTR) -> Result<(), Box<dyn Error>> {
|
||||
unsafe {
|
||||
let device_enumerator: IMMDeviceEnumerator =
|
||||
CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL)?;
|
||||
let device = device_enumerator.GetDevice(id)?;
|
||||
self.add_device(device)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn add_device(
|
||||
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_count = session_enumerator.GetCount()?;
|
||||
let device_sessions = (0..session_count)
|
||||
.map(|idx| {
|
||||
session_enumerator
|
||||
.GetSession(idx)
|
||||
.and_then(|session| session.cast())
|
||||
})
|
||||
.collect::<Result<Vec<IAudioSessionControl2>, _>>()?;
|
||||
for session in device_sessions {
|
||||
(self.notification_function)(session);
|
||||
}
|
||||
let prop_store = device.OpenPropertyStore(STGM_READ)?;
|
||||
// this definition isn't in windows-rs yet apparently :(
|
||||
let prop_var = prop_store.GetValue(&PKEY_Device_FriendlyName)?;
|
||||
let str = U16CStr::from_ptr_str(prop_var.Anonymous.Anonymous.Anonymous.pwszVal.0)
|
||||
.to_string_lossy();
|
||||
println!("Device Added: {} Existing Sessions: {}", str, session_count);
|
||||
self.session_managers.push(sm);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user