| 1 | use std::sync::OnceLock; |
| 2 | |
| 3 | use libwebrtc::peer_connection_factory::PeerConnectionFactory; |
| 4 | use libwebrtc::peer_connection_factory::native::PeerConnectionFactoryExt; |
| 5 | |
| 6 | pub const DEFAULT_DEVICE_LABEL: &str = "Default device"; |
| 7 | const SYSTEM_ALIAS_PREFIXES: [&str; 3] = ["default", "communication", "sysdefault"]; |
| 8 | |
| 9 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 10 | pub struct AudioDevice { |
| 11 | pub index: u16, |
| 12 | pub guid: String, |
| 13 | pub name: String, |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 17 | pub enum DeviceChoice { |
| 18 | #[default] |
| 19 | SystemDefault, |
| 20 | Device(String), |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 24 | pub struct DeviceEntry { |
| 25 | pub choice: DeviceChoice, |
| 26 | pub label: String, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 30 | pub struct DeviceLists { |
| 31 | pub inputs: Vec<AudioDevice>, |
| 32 | pub outputs: Vec<AudioDevice>, |
| 33 | } |
| 34 | |
| 35 | impl AudioDevice { |
| 36 | pub fn key(&self) -> String { |
| 37 | if self.guid.is_empty() { |
| 38 | self.name.clone() |
| 39 | } else { |
| 40 | self.guid.clone() |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn is_system_alias(&self) -> bool { |
| 45 | let name = self.name.to_lowercase(); |
| 46 | let guid = self.guid.to_lowercase(); |
| 47 | SYSTEM_ALIAS_PREFIXES |
| 48 | .iter() |
| 49 | .any(|prefix| name.starts_with(prefix) || guid == *prefix) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | pub fn menu_entries(devices: &[AudioDevice]) -> Vec<DeviceEntry> { |
| 54 | let default_entry = DeviceEntry { |
| 55 | choice: DeviceChoice::SystemDefault, |
| 56 | label: DEFAULT_DEVICE_LABEL.to_owned(), |
| 57 | }; |
| 58 | let named = devices |
| 59 | .iter() |
| 60 | .filter(|device| !device.is_system_alias() && !device.name.trim().is_empty()) |
| 61 | .map(|device| DeviceEntry { |
| 62 | choice: DeviceChoice::Device(device.key()), |
| 63 | label: device.name.clone(), |
| 64 | }); |
| 65 | std::iter::once(default_entry).chain(named).collect() |
| 66 | } |
| 67 | |
| 68 | impl DeviceLists { |
| 69 | pub fn input_entries(&self) -> Vec<DeviceEntry> { |
| 70 | menu_entries(&self.inputs) |
| 71 | } |
| 72 | |
| 73 | pub fn output_entries(&self) -> Vec<DeviceEntry> { |
| 74 | menu_entries(&self.outputs) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | pub fn shared_factory() -> PeerConnectionFactory { |
| 79 | static FACTORY: OnceLock<PeerConnectionFactory> = OnceLock::new(); |
| 80 | FACTORY.get_or_init(PeerConnectionFactory::default).clone() |
| 81 | } |
| 82 | |
| 83 | pub fn resolve_index(devices: &[AudioDevice], choice: &DeviceChoice) -> Option<u16> { |
| 84 | match choice { |
| 85 | DeviceChoice::SystemDefault => devices |
| 86 | .iter() |
| 87 | .find(|device| device.is_system_alias()) |
| 88 | .or(devices.first()) |
| 89 | .map(|device| device.index), |
| 90 | DeviceChoice::Device(key) => devices |
| 91 | .iter() |
| 92 | .find(|device| device.key() == *key) |
| 93 | .map(|device| device.index), |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | fn enumerate(factory: &PeerConnectionFactory) -> DeviceLists { |
| 98 | let inputs = (0..factory.recording_devices().max(0) as u16) |
| 99 | .map(|index| AudioDevice { |
| 100 | index, |
| 101 | guid: factory.recording_device_guid(index), |
| 102 | name: factory.recording_device_name(index), |
| 103 | }) |
| 104 | .collect(); |
| 105 | let outputs = (0..factory.playout_devices().max(0) as u16) |
| 106 | .map(|index| AudioDevice { |
| 107 | index, |
| 108 | guid: factory.playout_device_guid(index), |
| 109 | name: factory.playout_device_name(index), |
| 110 | }) |
| 111 | .collect(); |
| 112 | DeviceLists { inputs, outputs } |
| 113 | } |
| 114 | |
| 115 | pub fn list_devices(factory: &PeerConnectionFactory) -> DeviceLists { |
| 116 | if !factory.acquire_platform_adm() { |
| 117 | return DeviceLists::default(); |
| 118 | } |
| 119 | let lists = enumerate(factory); |
| 120 | factory.release_platform_adm(); |
| 121 | lists |
| 122 | } |
| 123 | |
| 124 | pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool { |
| 125 | let Some(index) = resolve_index(&enumerate(factory).inputs, choice) else { |
| 126 | return false; |
| 127 | }; |
| 128 | factory.stop_recording(); |
| 129 | let selected = factory.set_recording_device(index); |
| 130 | factory.init_recording(); |
| 131 | factory.start_recording(); |
| 132 | selected |
| 133 | } |
| 134 | |
| 135 | pub fn select_output(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool { |
| 136 | let Some(index) = resolve_index(&enumerate(factory).outputs, choice) else { |
| 137 | return false; |
| 138 | }; |
| 139 | factory.stop_playout(); |
| 140 | let selected = factory.set_playout_device(index); |
| 141 | factory.init_playout(); |
| 142 | factory.start_playout(); |
| 143 | selected |
| 144 | } |
| 145 | |
| 146 | #[cfg(test)] |
| 147 | mod tests { |
| 148 | use super::*; |
| 149 | |
| 150 | fn device(index: u16, name: &str, guid: &str) -> AudioDevice { |
| 151 | AudioDevice { |
| 152 | index, |
| 153 | guid: guid.into(), |
| 154 | name: name.into(), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | #[test] |
| 159 | fn default_device_comes_first_and_aliases_are_dropped() { |
| 160 | let devices = [ |
| 161 | device(0, "Headset Microphone", "{b}"), |
| 162 | device(1, "Default - Headset Microphone", "{a}"), |
| 163 | device(2, "Communication - Headset Microphone", "{c}"), |
| 164 | device(3, "Webcam Microphone", "{w}"), |
| 165 | ]; |
| 166 | let entries = menu_entries(&devices); |
| 167 | let labels: Vec<&str> = entries.iter().map(|entry| entry.label.as_str()).collect(); |
| 168 | assert_eq!(labels, ["Default device", "Headset Microphone", "Webcam Microphone"]); |
| 169 | assert_eq!(entries[0].choice, DeviceChoice::SystemDefault); |
| 170 | assert_eq!(entries[1].choice, DeviceChoice::Device("{b}".into())); |
| 171 | } |
| 172 | |
| 173 | #[test] |
| 174 | fn no_devices_still_offers_the_default_entry() { |
| 175 | let entries = menu_entries(&[]); |
| 176 | assert_eq!(entries.len(), 1); |
| 177 | assert_eq!(entries[0].label, DEFAULT_DEVICE_LABEL); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn linux_default_names_are_aliases() { |
| 182 | let devices = [device(0, "default", "default"), device(1, "sysdefault:CARD=PCH", "x"), device(2, "USB Audio", "usb")]; |
| 183 | let labels: Vec<String> = menu_entries(&devices).into_iter().map(|entry| entry.label).collect(); |
| 184 | assert_eq!(labels, ["Default device", "USB Audio"]); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn devices_without_a_guid_are_keyed_by_name() { |
| 189 | let devices = [device(0, "default: Speakers", ""), device(1, "Speakers", ""), device(2, "Headset", "")]; |
| 190 | let entries = menu_entries(&devices); |
| 191 | assert_eq!(entries[1].choice, DeviceChoice::Device("Speakers".into())); |
| 192 | assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Headset".into())), Some(2)); |
| 193 | assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Gone".into())), None); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn the_system_default_resolves_to_the_alias_or_the_first_device() { |
| 198 | let with_alias = [device(0, "USB", "u"), device(1, "default: USB", "")]; |
| 199 | assert_eq!(resolve_index(&with_alias, &DeviceChoice::SystemDefault), Some(1)); |
| 200 | let without_alias = [device(0, "Speakers", "s"), device(1, "Headset", "h")]; |
| 201 | assert_eq!(resolve_index(&without_alias, &DeviceChoice::SystemDefault), Some(0)); |
| 202 | assert_eq!(resolve_index(&[], &DeviceChoice::SystemDefault), None); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn unnamed_devices_are_hidden() { |
| 207 | let entries = menu_entries(&[device(0, " ", "g")]); |
| 208 | assert_eq!(entries.len(), 1); |
| 209 | } |
| 210 | } |