|
| 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 | +} |
0 commit comments