Skip to content

Commit eca598e

Browse files
authored
Merge pull request #609 from vector-im/dbkr/device_by_name
Use device labels rather than IDs in widget API
2 parents e8a875e + f808c56 commit eca598e

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

src/media-utils.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2022 Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { logger } from "matrix-js-sdk/src/logger";
18+
19+
/**
20+
* Finds a media device with label matching 'deviceName'
21+
* @param deviceName The label of the device to look for
22+
* @param devices The list of devices to search
23+
* @returns A matching media device or undefined if no matching device was found
24+
*/
25+
export async function findDeviceByName(
26+
deviceName: string,
27+
kind: MediaDeviceKind,
28+
devices: MediaDeviceInfo[]
29+
): Promise<string | undefined> {
30+
const deviceInfo = devices.find(
31+
(d) => d.kind === kind && d.label === deviceName
32+
);
33+
return deviceInfo?.deviceId;
34+
}
35+
36+
/**
37+
* Gets the available audio input/output and video input devices
38+
* from the browser: a wrapper around mediaDevices.enumerateDevices()
39+
* that requests a stream and holds it while calling enumerateDevices().
40+
* This is because some browsers (Firefox) only return device labels when
41+
* the app has an active user media stream. In Chrome, this will get a
42+
* stream from the default camera which can mean, for example, that the
43+
* light for the FaceTime camera turns on briefly even if you selected
44+
* another camera. Once the Permissions API
45+
* (https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API)
46+
* is ready for primetime, this should allow us to avoid this.
47+
*
48+
* @return The available media devices
49+
*/
50+
export async function getDevices(): Promise<MediaDeviceInfo[]> {
51+
let stream: MediaStream;
52+
try {
53+
stream = await navigator.mediaDevices.getUserMedia({
54+
audio: true,
55+
video: true,
56+
});
57+
} catch (e) {
58+
logger.info("Couldn't get media stream for enumerateDevices: failing");
59+
throw e;
60+
}
61+
62+
try {
63+
return await navigator.mediaDevices.enumerateDevices();
64+
} catch (error) {
65+
logger.warn("Unable to refresh WebRTC Devices: ", error);
66+
} finally {
67+
for (const track of stream.getTracks()) {
68+
track.stop();
69+
}
70+
}
71+
}

src/room/GroupCallView.tsx

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import React, { useCallback, useEffect, useState } from "react";
1818
import { useHistory } from "react-router-dom";
1919
import { GroupCall, GroupCallState } from "matrix-js-sdk/src/webrtc/groupCall";
2020
import { MatrixClient } from "matrix-js-sdk/src/client";
21+
import { logger } from "matrix-js-sdk/src/logger";
2122

2223
import type { IWidgetApiRequest } from "matrix-widget-api";
2324
import { widget, ElementWidgetActions, JoinCallData } from "../widget";
@@ -31,6 +32,7 @@ import { useRoomAvatar } from "./useRoomAvatar";
3132
import { useSentryGroupCallHandler } from "./useSentryGroupCallHandler";
3233
import { useLocationNavigation } from "../useLocationNavigation";
3334
import { useMediaHandler } from "../settings/useMediaHandler";
35+
import { findDeviceByName, getDevices } from "../media-utils";
3436

3537
declare global {
3638
interface Window {
@@ -94,10 +96,45 @@ export function GroupCallView({
9496
if (widget && preload) {
9597
// In preload mode, wait for a join action before entering
9698
const onJoin = async (ev: CustomEvent<IWidgetApiRequest>) => {
99+
// Get the available devices so we can match the selected device
100+
// to its ID. This involves getting a media stream (see docs on
101+
// the function) so we only do it once and re-use the result.
102+
const devices = await getDevices();
103+
97104
const { audioInput, videoInput } = ev.detail
98105
.data as unknown as JoinCallData;
99-
if (audioInput !== null) setAudioInput(audioInput);
100-
if (videoInput !== null) setVideoInput(videoInput);
106+
107+
if (audioInput !== null) {
108+
const deviceId = await findDeviceByName(
109+
audioInput,
110+
"audioinput",
111+
devices
112+
);
113+
if (!deviceId) {
114+
logger.warn("Unknown audio input: " + audioInput);
115+
} else {
116+
logger.debug(
117+
`Found audio input ID ${deviceId} for name ${audioInput}`
118+
);
119+
setAudioInput(deviceId);
120+
}
121+
}
122+
123+
if (videoInput !== null) {
124+
const deviceId = await findDeviceByName(
125+
videoInput,
126+
"videoinput",
127+
devices
128+
);
129+
if (!deviceId) {
130+
logger.warn("Unknown video input: " + videoInput);
131+
} else {
132+
logger.debug(
133+
`Found video input ID ${deviceId} for name ${videoInput}`
134+
);
135+
setVideoInput(deviceId);
136+
}
137+
}
101138
await Promise.all([
102139
groupCall.setMicrophoneMuted(audioInput === null),
103140
groupCall.setLocalVideoMuted(videoInput === null),

0 commit comments

Comments
 (0)