BlueGit

ict rusty-teams

fix(calls): "Default device" means the Windows default device

- WebRtcVoiceEngine::Init picked the communications default at every
  call start (here a monitor speaker); the user switched to the Windows
  default device by hand every call. AdmProxy now maps the
  communications default to the default device.
- Picking "Default device" in a call resolved to ADM index 0. It now
  resolves the default endpoint id (IMMDeviceEnumerator, eConsole) and
  matches it against the ADM device guid.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-14 · 6 weeks ago

Hash
cb5e569cdd5de9e0bb3fcbca5a9a1e7ef3035684
Parent
81cf74f4
4 files changed +81 −13
crates/calling/src/devices.rs +68 −11
@@ -80,11 +80,52 @@ pub fn shared_factory() -> PeerConnectionFactory {
80 80 FACTORY.get_or_init(PeerConnectionFactory::default).clone()
81 81 }
82 82
83 pub fn resolve_index(devices: &[AudioDevice], choice: &DeviceChoice) -> Option<u16> {
83 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
84 pub enum Flow {
85 Capture,
86 Render,
87 }
88
89 /// Endpoint id of the Windows default device (console role), which the ADM reports as the device guid.
90 #[cfg(windows)]
91 pub fn system_default_id(flow: Flow) -> Option<String> {
92 use windows::Win32::System::Com::{COINIT_MULTITHREADED, CoInitializeEx, CoUninitialize};
93 let initialized = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }.is_ok();
94 let id = default_endpoint_id(flow);
95 if initialized {
96 unsafe { CoUninitialize() };
97 }
98 id
99 }
100
101 #[cfg(windows)]
102 fn default_endpoint_id(flow: Flow) -> Option<String> {
103 use windows::Win32::Media::Audio::{IMMDeviceEnumerator, MMDeviceEnumerator, eCapture, eConsole, eRender};
104 use windows::Win32::System::Com::{CLSCTX_ALL, CoCreateInstance, CoTaskMemFree};
105 let data_flow = match flow {
106 Flow::Capture => eCapture,
107 Flow::Render => eRender,
108 };
109 unsafe {
110 let enumerator: IMMDeviceEnumerator = CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).ok()?;
111 let device = enumerator.GetDefaultAudioEndpoint(data_flow, eConsole).ok()?;
112 let id = device.GetId().ok()?;
113 let text = id.to_string().ok();
114 CoTaskMemFree(Some(id.0 as *const _));
115 text
116 }
117 }
118
119 #[cfg(not(windows))]
120 pub fn system_default_id(_flow: Flow) -> Option<String> {
121 None
122 }
123
124 pub fn resolve_index(devices: &[AudioDevice], choice: &DeviceChoice, system_default: Option<&str>) -> Option<u16> {
84 125 match choice {
85 DeviceChoice::SystemDefault => devices
86 .iter()
87 .find(|device| device.is_system_alias())
126 DeviceChoice::SystemDefault => system_default
127 .and_then(|id| devices.iter().find(|device| device.guid.eq_ignore_ascii_case(id)))
128 .or_else(|| devices.iter().find(|device| device.is_system_alias()))
88 129 .or(devices.first())
89 130 .map(|device| device.index),
90 131 DeviceChoice::Device(key) => devices
@@ -121,8 +162,12 @@ pub fn list_devices(factory: &PeerConnectionFactory) -> DeviceLists {
121 162 lists
122 163 }
123 164
165 fn default_for(choice: &DeviceChoice, flow: Flow) -> Option<String> {
166 (*choice == DeviceChoice::SystemDefault).then(|| system_default_id(flow)).flatten()
167 }
168
124 169 pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
125 let Some(index) = resolve_index(&enumerate(factory).inputs, choice) else {
170 let Some(index) = resolve_index(&enumerate(factory).inputs, choice, default_for(choice, Flow::Capture).as_deref()) else {
126 171 return false;
127 172 };
128 173 factory.stop_recording();
@@ -133,7 +178,7 @@ pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> b
133 178 }
134 179
135 180 pub fn select_output(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
136 let Some(index) = resolve_index(&enumerate(factory).outputs, choice) else {
181 let Some(index) = resolve_index(&enumerate(factory).outputs, choice, default_for(choice, Flow::Render).as_deref()) else {
137 182 return false;
138 183 };
139 184 factory.stop_playout();
@@ -189,17 +234,29 @@ mod tests {
189 234 let devices = [device(0, "default: Speakers", ""), device(1, "Speakers", ""), device(2, "Headset", "")];
190 235 let entries = menu_entries(&devices);
191 236 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);
237 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Headset".into()), None), Some(2));
238 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Gone".into()), None), None);
194 239 }
195 240
196 241 #[test]
197 242 fn the_system_default_resolves_to_the_alias_or_the_first_device() {
198 243 let with_alias = [device(0, "USB", "u"), device(1, "default: USB", "")];
199 assert_eq!(resolve_index(&with_alias, &DeviceChoice::SystemDefault), Some(1));
244 assert_eq!(resolve_index(&with_alias, &DeviceChoice::SystemDefault, None), Some(1));
200 245 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);
246 assert_eq!(resolve_index(&without_alias, &DeviceChoice::SystemDefault, None), Some(0));
247 assert_eq!(resolve_index(&[], &DeviceChoice::SystemDefault, None), None);
248 }
249
250 #[test]
251 fn the_system_default_follows_the_platform_default_endpoint() {
252 let devices = [
253 device(0, "Speakers (Realtek)", "{0.0.0.00000000}.{aaaa}"),
254 device(1, "Headset Earphone", "{0.0.0.00000000}.{BBBB}"),
255 ];
256 let headset = Some("{0.0.0.00000000}.{bbbb}");
257 assert_eq!(resolve_index(&devices, &DeviceChoice::SystemDefault, headset), Some(1));
258 assert_eq!(resolve_index(&devices, &DeviceChoice::SystemDefault, Some("{0.0.0.00000000}.{gone}")), Some(0));
259 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("{0.0.0.00000000}.{aaaa}".into()), headset), Some(0));
203 260 }
204 261
205 262 #[test]
@@ -80,11 +80,52 @@ pub fn shared_factory() -> PeerConnectionFactory {
80 FACTORY.get_or_init(PeerConnectionFactory::default).clone()
80 FACTORY.get_or_init(PeerConnectionFactory::default).clone()
81}
81}
82
82
83pub fn resolve_index(devices: &[AudioDevice], choice: &DeviceChoice) -> Option<u16> {
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84pub enum Flow {
85 Capture,
86 Render,
87}
88
89/// Endpoint id of the Windows default device (console role), which the ADM reports as the device guid.
90#[cfg(windows)]
91pub fn system_default_id(flow: Flow) -> Option<String> {
92 use windows::Win32::System::Com::{COINIT_MULTITHREADED, CoInitializeEx, CoUninitialize};
93 let initialized = unsafe { CoInitializeEx(None, COINIT_MULTITHREADED) }.is_ok();
94 let id = default_endpoint_id(flow);
95 if initialized {
96 unsafe { CoUninitialize() };
97 }
98 id
99}
100
101#[cfg(windows)]
102fn default_endpoint_id(flow: Flow) -> Option<String> {
103 use windows::Win32::Media::Audio::{IMMDeviceEnumerator, MMDeviceEnumerator, eCapture, eConsole, eRender};
104 use windows::Win32::System::Com::{CLSCTX_ALL, CoCreateInstance, CoTaskMemFree};
105 let data_flow = match flow {
106 Flow::Capture => eCapture,
107 Flow::Render => eRender,
108 };
109 unsafe {
110 let enumerator: IMMDeviceEnumerator = CoCreateInstance(&MMDeviceEnumerator, None, CLSCTX_ALL).ok()?;
111 let device = enumerator.GetDefaultAudioEndpoint(data_flow, eConsole).ok()?;
112 let id = device.GetId().ok()?;
113 let text = id.to_string().ok();
114 CoTaskMemFree(Some(id.0 as *const _));
115 text
116 }
117}
118
119#[cfg(not(windows))]
120pub fn system_default_id(_flow: Flow) -> Option<String> {
121 None
122}
123
124pub fn resolve_index(devices: &[AudioDevice], choice: &DeviceChoice, system_default: Option<&str>) -> Option<u16> {
84 match choice {
125 match choice {
85 DeviceChoice::SystemDefault => devices
126 DeviceChoice::SystemDefault => system_default
86 .iter()
127 .and_then(|id| devices.iter().find(|device| device.guid.eq_ignore_ascii_case(id)))
87 .find(|device| device.is_system_alias())
128 .or_else(|| devices.iter().find(|device| device.is_system_alias()))
88 .or(devices.first())
129 .or(devices.first())
89 .map(|device| device.index),
130 .map(|device| device.index),
90 DeviceChoice::Device(key) => devices
131 DeviceChoice::Device(key) => devices
@@ -121,8 +162,12 @@ pub fn list_devices(factory: &PeerConnectionFactory) -> DeviceLists {
121 lists
162 lists
122}
163}
123
164
165fn default_for(choice: &DeviceChoice, flow: Flow) -> Option<String> {
166 (*choice == DeviceChoice::SystemDefault).then(|| system_default_id(flow)).flatten()
167}
168
124pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
169pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
125 let Some(index) = resolve_index(&enumerate(factory).inputs, choice) else {
170 let Some(index) = resolve_index(&enumerate(factory).inputs, choice, default_for(choice, Flow::Capture).as_deref()) else {
126 return false;
171 return false;
127 };
172 };
128 factory.stop_recording();
173 factory.stop_recording();
@@ -133,7 +178,7 @@ pub fn select_input(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> b
133}
178}
134
179
135pub fn select_output(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
180pub fn select_output(factory: &PeerConnectionFactory, choice: &DeviceChoice) -> bool {
136 let Some(index) = resolve_index(&enumerate(factory).outputs, choice) else {
181 let Some(index) = resolve_index(&enumerate(factory).outputs, choice, default_for(choice, Flow::Render).as_deref()) else {
137 return false;
182 return false;
138 };
183 };
139 factory.stop_playout();
184 factory.stop_playout();
@@ -189,17 +234,29 @@ mod tests {
189 let devices = [device(0, "default: Speakers", ""), device(1, "Speakers", ""), device(2, "Headset", "")];
234 let devices = [device(0, "default: Speakers", ""), device(1, "Speakers", ""), device(2, "Headset", "")];
190 let entries = menu_entries(&devices);
235 let entries = menu_entries(&devices);
191 assert_eq!(entries[1].choice, DeviceChoice::Device("Speakers".into()));
236 assert_eq!(entries[1].choice, DeviceChoice::Device("Speakers".into()));
192 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Headset".into())), Some(2));
237 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Headset".into()), None), Some(2));
193 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Gone".into())), None);
238 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("Gone".into()), None), None);
194 }
239 }
195
240
196 #[test]
241 #[test]
197 fn the_system_default_resolves_to_the_alias_or_the_first_device() {
242 fn the_system_default_resolves_to_the_alias_or_the_first_device() {
198 let with_alias = [device(0, "USB", "u"), device(1, "default: USB", "")];
243 let with_alias = [device(0, "USB", "u"), device(1, "default: USB", "")];
199 assert_eq!(resolve_index(&with_alias, &DeviceChoice::SystemDefault), Some(1));
244 assert_eq!(resolve_index(&with_alias, &DeviceChoice::SystemDefault, None), Some(1));
200 let without_alias = [device(0, "Speakers", "s"), device(1, "Headset", "h")];
245 let without_alias = [device(0, "Speakers", "s"), device(1, "Headset", "h")];
201 assert_eq!(resolve_index(&without_alias, &DeviceChoice::SystemDefault), Some(0));
246 assert_eq!(resolve_index(&without_alias, &DeviceChoice::SystemDefault, None), Some(0));
202 assert_eq!(resolve_index(&[], &DeviceChoice::SystemDefault), None);
247 assert_eq!(resolve_index(&[], &DeviceChoice::SystemDefault, None), None);
248 }
249
250 #[test]
251 fn the_system_default_follows_the_platform_default_endpoint() {
252 let devices = [
253 device(0, "Speakers (Realtek)", "{0.0.0.00000000}.{aaaa}"),
254 device(1, "Headset Earphone", "{0.0.0.00000000}.{BBBB}"),
255 ];
256 let headset = Some("{0.0.0.00000000}.{bbbb}");
257 assert_eq!(resolve_index(&devices, &DeviceChoice::SystemDefault, headset), Some(1));
258 assert_eq!(resolve_index(&devices, &DeviceChoice::SystemDefault, Some("{0.0.0.00000000}.{gone}")), Some(0));
259 assert_eq!(resolve_index(&devices, &DeviceChoice::Device("{0.0.0.00000000}.{aaaa}".into()), headset), Some(0));
203 }
260 }
204
261
205 #[test]
262 #[test]
vendor/webrtc-sys/PATCH.md +1 −0
@@ -8,3 +8,4 @@ Upstream: crates.io `webrtc-sys 0.3.48` (livekit/rust-sdks). Wired in via `[patc
8 8 |---|---|---|
9 9 | `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `Init()` re-inits terminated sub ADMs; `EnsurePlatformAdmCreated()` re-inits a terminated platform ADM; `InitPlatformAdm()` replays transport, devices, observer, stereo; `Terminate()` stops synthetic playout and clears `playing_` / `recording_` | `WebRtcVoiceEngine::Terminate()` terminates the ADM after every call, `Init()` was a no-op: from the second call on 0 devices, no mic, no sound |
10 10 | `src/adm_proxy.cpp` | `BuiltInAECIsAvailable()` returns false on Windows | the voice capture DMO fails `StartRecording` ("Playout must be started before recording") when no remote audio plays yet - mic silent for the whole call (seen in the app log 2026-10-13) |
11 | `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `SetPlayoutDevice` / `SetRecordingDevice(WindowsDeviceType)` turn `kDefaultCommunicationDevice` into `kDefaultDevice` | `WebRtcVoiceEngine::Init` selects the communications default at every call start; the app's "Default device" means the Windows default device (seen 2026-10-14: speaker came up on the communications default, user picked the default device by hand every call) |
@@ -8,3 +8,4 @@ Upstream: crates.io `webrtc-sys 0.3.48` (livekit/rust-sdks). Wired in via `[patc
8|---|---|---|
8|---|---|---|
9| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `Init()` re-inits terminated sub ADMs; `EnsurePlatformAdmCreated()` re-inits a terminated platform ADM; `InitPlatformAdm()` replays transport, devices, observer, stereo; `Terminate()` stops synthetic playout and clears `playing_` / `recording_` | `WebRtcVoiceEngine::Terminate()` terminates the ADM after every call, `Init()` was a no-op: from the second call on 0 devices, no mic, no sound |
9| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `Init()` re-inits terminated sub ADMs; `EnsurePlatformAdmCreated()` re-inits a terminated platform ADM; `InitPlatformAdm()` replays transport, devices, observer, stereo; `Terminate()` stops synthetic playout and clears `playing_` / `recording_` | `WebRtcVoiceEngine::Terminate()` terminates the ADM after every call, `Init()` was a no-op: from the second call on 0 devices, no mic, no sound |
10| `src/adm_proxy.cpp` | `BuiltInAECIsAvailable()` returns false on Windows | the voice capture DMO fails `StartRecording` ("Playout must be started before recording") when no remote audio plays yet - mic silent for the whole call (seen in the app log 2026-10-13) |
10| `src/adm_proxy.cpp` | `BuiltInAECIsAvailable()` returns false on Windows | the voice capture DMO fails `StartRecording` ("Playout must be started before recording") when no remote audio plays yet - mic silent for the whole call (seen in the app log 2026-10-13) |
11| `src/adm_proxy.cpp`, `include/livekit/adm_proxy.h` | `SetPlayoutDevice` / `SetRecordingDevice(WindowsDeviceType)` turn `kDefaultCommunicationDevice` into `kDefaultDevice` | `WebRtcVoiceEngine::Init` selects the communications default at every call start; the app's "Default device" means the Windows default device (seen 2026-10-14: speaker came up on the communications default, user picked the default device by hand every call) |
vendor/webrtc-sys/include/livekit/adm_proxy.h +1 −0
@@ -316,6 +316,7 @@ class AdmProxy : public webrtc::AudioDeviceModule {
316 316 // SetPlayoutDevice/SetRecordingDevice overloads, so an explicit index 0
317 317 // and a Windows default device type both survive the replay.
318 318 using DeviceSelection = std::variant<uint16_t, WindowsDeviceType>;
319 static WindowsDeviceType SystemDefaultFor(WindowsDeviceType requested);
319 320 std::optional<DeviceSelection> selected_playout_device_
320 321 RTC_GUARDED_BY(worker_thread_);
321 322 std::optional<DeviceSelection> selected_recording_device_
@@ -316,6 +316,7 @@ class AdmProxy : public webrtc::AudioDeviceModule {
316 // SetPlayoutDevice/SetRecordingDevice overloads, so an explicit index 0
316 // SetPlayoutDevice/SetRecordingDevice overloads, so an explicit index 0
317 // and a Windows default device type both survive the replay.
317 // and a Windows default device type both survive the replay.
318 using DeviceSelection = std::variant<uint16_t, WindowsDeviceType>;
318 using DeviceSelection = std::variant<uint16_t, WindowsDeviceType>;
319 static WindowsDeviceType SystemDefaultFor(WindowsDeviceType requested);
319 std::optional<DeviceSelection> selected_playout_device_
320 std::optional<DeviceSelection> selected_playout_device_
320 RTC_GUARDED_BY(worker_thread_);
321 RTC_GUARDED_BY(worker_thread_);
321 std::optional<DeviceSelection> selected_recording_device_
322 std::optional<DeviceSelection> selected_recording_device_
vendor/webrtc-sys/src/adm_proxy.cpp +11 −2
@@ -431,6 +431,13 @@ int32_t AdmProxy::RecordingDeviceName(uint16_t index,
431 431 });
432 432 }
433 433
434 // WebRtcVoiceEngine::Init asks for the communications default at every call start; the app's default is the Windows default device.
435 AdmProxy::WindowsDeviceType AdmProxy::SystemDefaultFor(WindowsDeviceType requested) {
436 return requested == WindowsDeviceType::kDefaultCommunicationDevice
437 ? WindowsDeviceType::kDefaultDevice
438 : requested;
439 }
440
434 441 int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
435 442 return RunOnWorker([this, index] {
436 443 RTC_DCHECK_RUN_ON(worker_thread_);
@@ -442,7 +449,8 @@ int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
442 449 });
443 450 }
444 451
445 int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType device) {
452 int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType requested) {
453 const WindowsDeviceType device = SystemDefaultFor(requested);
446 454 return RunOnWorker([this, device] {
447 455 RTC_DCHECK_RUN_ON(worker_thread_);
448 456 selected_playout_device_ = DeviceSelection(device);
@@ -464,7 +472,8 @@ int32_t AdmProxy::SetRecordingDevice(uint16_t index) {
464 472 });
465 473 }
466 474
467 int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType device) {
475 int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType requested) {
476 const WindowsDeviceType device = SystemDefaultFor(requested);
468 477 return RunOnWorker([this, device] {
469 478 RTC_DCHECK_RUN_ON(worker_thread_);
470 479 selected_recording_device_ = DeviceSelection(device);
@@ -431,6 +431,13 @@ int32_t AdmProxy::RecordingDeviceName(uint16_t index,
431 });
431 });
432}
432}
433
433
434// WebRtcVoiceEngine::Init asks for the communications default at every call start; the app's default is the Windows default device.
435AdmProxy::WindowsDeviceType AdmProxy::SystemDefaultFor(WindowsDeviceType requested) {
436 return requested == WindowsDeviceType::kDefaultCommunicationDevice
437 ? WindowsDeviceType::kDefaultDevice
438 : requested;
439}
440
434int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
441int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
435 return RunOnWorker([this, index] {
442 return RunOnWorker([this, index] {
436 RTC_DCHECK_RUN_ON(worker_thread_);
443 RTC_DCHECK_RUN_ON(worker_thread_);
@@ -442,7 +449,8 @@ int32_t AdmProxy::SetPlayoutDevice(uint16_t index) {
442 });
449 });
443}
450}
444
451
445int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType device) {
452int32_t AdmProxy::SetPlayoutDevice(WindowsDeviceType requested) {
453 const WindowsDeviceType device = SystemDefaultFor(requested);
446 return RunOnWorker([this, device] {
454 return RunOnWorker([this, device] {
447 RTC_DCHECK_RUN_ON(worker_thread_);
455 RTC_DCHECK_RUN_ON(worker_thread_);
448 selected_playout_device_ = DeviceSelection(device);
456 selected_playout_device_ = DeviceSelection(device);
@@ -464,7 +472,8 @@ int32_t AdmProxy::SetRecordingDevice(uint16_t index) {
464 });
472 });
465}
473}
466
474
467int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType device) {
475int32_t AdmProxy::SetRecordingDevice(WindowsDeviceType requested) {
476 const WindowsDeviceType device = SystemDefaultFor(requested);
468 return RunOnWorker([this, device] {
477 return RunOnWorker([this, device] {
469 RTC_DCHECK_RUN_ON(worker_thread_);
478 RTC_DCHECK_RUN_ON(worker_thread_);
470 selected_recording_device_ = DeviceSelection(device);
479 selected_recording_device_ = DeviceSelection(device);