Add MediaDeviceKindEnum

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner 2021-07-09 13:08:39 +02:00
parent 866c1b76bd
commit dadfba9075
No known key found for this signature in database
GPG key ID: 9760693FDD98A790
3 changed files with 20 additions and 23 deletions

View file

@ -20,12 +20,15 @@ import { SettingLevel } from "./settings/SettingLevel";
import { setMatrixCallAudioInput, setMatrixCallVideoInput } from "matrix-js-sdk/src/matrix"; import { setMatrixCallAudioInput, setMatrixCallVideoInput } from "matrix-js-sdk/src/matrix";
import EventEmitter from 'events'; import EventEmitter from 'events';
interface IMediaDevices { // XXX: MediaDeviceKind is a union type, so we make our own enum
audioOutput: Array<MediaDeviceInfo>; export enum MediaDeviceKindEnum {
audioInput: Array<MediaDeviceInfo>; AudioOutput = "audiooutput",
videoInput: Array<MediaDeviceInfo>; AudioInput = "audioinput",
VideoInput = "videoinput",
} }
export type IMediaDevices = Record<MediaDeviceKindEnum, Array<MediaDeviceInfo>>;
export enum MediaDeviceHandlerEvent { export enum MediaDeviceHandlerEvent {
AudioOutputChanged = "audio_output_changed", AudioOutputChanged = "audio_output_changed",
} }
@ -51,20 +54,14 @@ export default class MediaDeviceHandler extends EventEmitter {
try { try {
const devices = await navigator.mediaDevices.enumerateDevices(); const devices = await navigator.mediaDevices.enumerateDevices();
const output = {
[MediaDeviceKindEnum.AudioOutput]: [],
[MediaDeviceKindEnum.AudioInput]: [],
[MediaDeviceKindEnum.VideoInput]: [],
};
const audioOutput = []; devices.forEach((device) => output[device.kind].push(device));
const audioInput = []; return output;
const videoInput = [];
devices.forEach((device) => {
switch (device.kind) {
case 'audiooutput': audioOutput.push(device); break;
case 'audioinput': audioInput.push(device); break;
case 'videoinput': videoInput.push(device); break;
}
});
return { audioOutput, audioInput, videoInput };
} catch (error) { } catch (error) {
console.warn('Unable to refresh WebRTC Devices: ', error); console.warn('Unable to refresh WebRTC Devices: ', error);
} }

View file

@ -33,7 +33,7 @@ import RecordingPlayback from "../audio_messages/RecordingPlayback";
import { MsgType } from "matrix-js-sdk/src/@types/event"; import { MsgType } from "matrix-js-sdk/src/@types/event";
import Modal from "../../../Modal"; import Modal from "../../../Modal";
import ErrorDialog from "../dialogs/ErrorDialog"; import ErrorDialog from "../dialogs/ErrorDialog";
import MediaDeviceHandler from "../../../MediaDeviceHandler"; import MediaDeviceHandler, { MediaDeviceKindEnum } from "../../../MediaDeviceHandler";
interface IProps { interface IProps {
room: Room; room: Room;
@ -135,7 +135,7 @@ export default class VoiceRecordComposerTile extends React.PureComponent<IProps,
// change between this and recording, but at least we will have tried. // change between this and recording, but at least we will have tried.
try { try {
const devices = await MediaDeviceHandler.getDevices(); const devices = await MediaDeviceHandler.getDevices();
if (!devices?.['audioInput']?.length) { if (!devices?.[MediaDeviceKindEnum.AudioInput]?.length) {
Modal.createTrackedDialog('No Microphone Error', '', ErrorDialog, { Modal.createTrackedDialog('No Microphone Error', '', ErrorDialog, {
title: _t("No microphone found"), title: _t("No microphone found"),
description: <> description: <>

View file

@ -18,7 +18,7 @@ limitations under the License.
import React from 'react'; import React from 'react';
import { _t } from "../../../../../languageHandler"; import { _t } from "../../../../../languageHandler";
import SdkConfig from "../../../../../SdkConfig"; import SdkConfig from "../../../../../SdkConfig";
import MediaDeviceHandler, { IMediaDevices } from "../../../../../MediaDeviceHandler"; import MediaDeviceHandler, { IMediaDevices, MediaDeviceKindEnum } from "../../../../../MediaDeviceHandler";
import Field from "../../../elements/Field"; import Field from "../../../elements/Field";
import AccessibleButton from "../../../elements/AccessibleButton"; import AccessibleButton from "../../../elements/AccessibleButton";
import { MatrixClientPeg } from "../../../../../MatrixClientPeg"; import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
@ -178,7 +178,7 @@ export default class VoiceUserSettingsTab extends React.Component<{}, IState> {
} }
}; };
const audioOutputs = this.state.mediaDevices.audioOutput.slice(0); const audioOutputs = this.state.mediaDevices[MediaDeviceKindEnum.AudioOutput].slice(0);
if (audioOutputs.length > 0) { if (audioOutputs.length > 0) {
const defaultDevice = getDefaultDevice(audioOutputs); const defaultDevice = getDefaultDevice(audioOutputs);
speakerDropdown = ( speakerDropdown = (
@ -190,7 +190,7 @@ export default class VoiceUserSettingsTab extends React.Component<{}, IState> {
); );
} }
const audioInputs = this.state.mediaDevices.audioInput.slice(0); const audioInputs = this.state.mediaDevices[MediaDeviceKindEnum.AudioInput].slice(0);
if (audioInputs.length > 0) { if (audioInputs.length > 0) {
const defaultDevice = getDefaultDevice(audioInputs); const defaultDevice = getDefaultDevice(audioInputs);
microphoneDropdown = ( microphoneDropdown = (
@ -202,7 +202,7 @@ export default class VoiceUserSettingsTab extends React.Component<{}, IState> {
); );
} }
const videoInputs = this.state.mediaDevices.videoInput.slice(0); const videoInputs = this.state.mediaDevices[MediaDeviceKindEnum.VideoInput].slice(0);
if (videoInputs.length > 0) { if (videoInputs.length > 0) {
const defaultDevice = getDefaultDevice(videoInputs); const defaultDevice = getDefaultDevice(videoInputs);
webcamDropdown = ( webcamDropdown = (