Determine which audio device is in use

307 views
Skip to first unread message

Sebastian J

unread,
Sep 15, 2020, 10:49:13 AM9/15/20
to discuss-webrtc
Hi, I suspect the answer is no since there is no such API in AudioDeviceModule, but in case I missed it, is there a way to determine which audio device will be opened by the ADM by default?

Also, is there a way to tell a peer connection to recreate the audio device if the device selected by the user changes?

Or do apps that support this just tear down the connections and reestablish them manually before anyone notices?

Sj

Pallab Gain

unread,
Sep 15, 2020, 2:45:40 PM9/15/20
to discuss-webrtc
I also think there is no API available to know which device(s) are currently in use. 

But, based on current browser API  ( should be possible with android, iOS with other platforms as well ) 
- We can know which is the currently selected device(s) using ( https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/enumerateDevices ). One possible solution might be is remember which is the currently selected device(s) and store the device(s) ID in your application before calling getUserMedia ( if you don't provide any device ID it will use the default device from the device enumeration list ). Then you can listen to device selection change from your SDK/app, and use replaceTrack ( https://developer.mozilla.org/en-US/docs/Web/API/RTCRtpSender/replaceTrack ) to replace the track. It is possible to handle the device change without tearing down the peer connection session.

  

Henrik Andreasson

unread,
Sep 17, 2020, 4:09:11 AM9/17/20
to discuss-webrtc
There are audio-device enumeration APIs for the native ADM for desktop platforms (Windows, Mac OSX, Linux) as well.

See e.g. these examples:


The code is rather old and is not part of the main WebRTC stack but it should work OK as is. 

--

---
You received this message because you are subscribed to the Google Groups "discuss-webrtc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to discuss-webrt...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/discuss-webrtc/041558bb-defe-43b2-92d3-e4b2661ed1een%40googlegroups.com.

Sebastian J

unread,
Sep 21, 2020, 12:36:12 PM9/21/20
to discuss-webrtc
Thank you Pallab, Henrik,

(I should have specified, I am using the native api!)

I have enumerated and allowed the user to select an appropriate audio device in my application. 

I also found the API Windows uses to return the default device, though how exactly Windows chooses the device is unclear. The docs (https://docs.microsoft.com/en-us/windows/win32/api/mmdeviceapi/nf-mmdeviceapi-immdeviceenumerator-getdefaultaudioendpoint) state that it is chosen based on role, but I've not seen how to set by role in the OS.

I have managed to change the device at runtime though. To do this I stop the ADM, change the device, and restart it. The recording must be restarted even when only changing playout, as the buffers must be in sync for echo cancellation. Playout and Recording must also be initialised, as the object is de-initialised on stop. That is, the sequence looks like:

            adm.StopPlayout();
            adm.StopRecording();
            adm.SetPlayoutDevice(device); // or recording...
            adm.InitPlayout();
            adm.InitRecording();
            adm.StartPlayout();
            adm.StartRecording();

The existing streams are piped to the new device so I don't interact with the transceivers or perform any signaling.

I am not sure if this is correct, as it is mainly from trial and error, but it works on multiple Windows PCs so far.

Sj

Henrik Andreasson

unread,
Sep 22, 2020, 3:31:55 AM9/22/20
to discuss-webrtc
In native WebRTC, you can use this enumerator to select by role (Default or Default Comm). Each device can be given this role if you have more than one device. Simply use the Sound settings dialog in your system settings and WebRTC will use the device matching the role you selected.

Sebastian J

unread,
Sep 22, 2020, 5:16:32 AM9/22/20
to discuss-webrtc
Thanks Henrik. I realised I have seen the option before. For anyone else, it's not exposed in the new Sound Settings panel but in the traditional Sound control panel applet (Google Images search for "windows default communication device" and you'll recognise it). No one I've spoken to remembered this option existed, and since our application is slightly different to traditional teleconferencing I have set it to use the default (non communications) device.

Sj

Reply all
Reply to author
Forward
0 new messages