Merge pull request #654 from robintown/no-devices

Fix joining calls in matryoshka mode without audio or video inputs
This commit is contained in:
Robin 2022-10-23 13:33:15 -04:00 committed by GitHub
commit 4e9abfa3b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -48,24 +48,42 @@ export async function findDeviceByName(
* @return The available media devices * @return The available media devices
*/ */
export async function getDevices(): Promise<MediaDeviceInfo[]> { export async function getDevices(): Promise<MediaDeviceInfo[]> {
let stream: MediaStream; // First get the devices without their labels, to learn what kinds of streams
// we can request
let devices: MediaDeviceInfo[];
try { try {
stream = await navigator.mediaDevices.getUserMedia({ devices = await navigator.mediaDevices.enumerateDevices();
audio: true, } catch (error) {
video: true, logger.warn("Unable to refresh WebRTC devices", error);
}); devices = [];
}
let stream: MediaStream | null = null;
try {
if (devices.some((d) => d.kind === "audioinput")) {
// Holding just an audio stream will be enough to get us all device labels
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
} else if (devices.some((d) => d.kind === "videoinput")) {
// We have to resort to a video stream
stream = await navigator.mediaDevices.getUserMedia({ video: true });
}
} catch (e) { } catch (e) {
logger.info("Couldn't get media stream for enumerateDevices: failing"); logger.info("Couldn't get media stream for enumerateDevices: failing");
throw e; throw e;
} }
try { if (stream !== null) {
return await navigator.mediaDevices.enumerateDevices(); try {
} catch (error) { return await navigator.mediaDevices.enumerateDevices();
logger.warn("Unable to refresh WebRTC Devices: ", error); } catch (error) {
} finally { logger.warn("Unable to refresh WebRTC devices", error);
for (const track of stream.getTracks()) { } finally {
track.stop(); for (const track of stream.getTracks()) {
track.stop();
}
} }
} }
// If all else failed, continue without device labels
return devices;
} }