Extract requestMediaPermissions (#9568)
This commit is contained in:
parent
f2f00f68ba
commit
fca6ff271c
11 changed files with 196 additions and 53 deletions
|
@ -16,19 +16,16 @@ limitations under the License.
|
|||
*/
|
||||
|
||||
import React from 'react';
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import { _t } from "../../../../../languageHandler";
|
||||
import SdkConfig from "../../../../../SdkConfig";
|
||||
import MediaDeviceHandler, { IMediaDevices, MediaDeviceKindEnum } from "../../../../../MediaDeviceHandler";
|
||||
import Field from "../../../elements/Field";
|
||||
import AccessibleButton from "../../../elements/AccessibleButton";
|
||||
import { MatrixClientPeg } from "../../../../../MatrixClientPeg";
|
||||
import Modal from "../../../../../Modal";
|
||||
import { SettingLevel } from "../../../../../settings/SettingLevel";
|
||||
import SettingsFlag from '../../../elements/SettingsFlag';
|
||||
import LabelledToggleSwitch from "../../../elements/LabelledToggleSwitch";
|
||||
import ErrorDialog from '../../../dialogs/ErrorDialog';
|
||||
import { requestMediaPermissions } from '../../../../../utils/media/requestMediaPermissions';
|
||||
|
||||
const getDefaultDevice = (devices: Array<Partial<MediaDeviceInfo>>) => {
|
||||
// Note we're looking for a device with deviceId 'default' but adding a device
|
||||
|
@ -90,37 +87,8 @@ export default class VoiceUserSettingsTab extends React.Component<{}, IState> {
|
|||
};
|
||||
|
||||
private requestMediaPermissions = async (): Promise<void> => {
|
||||
let constraints;
|
||||
let stream;
|
||||
let error;
|
||||
try {
|
||||
constraints = { video: true, audio: true };
|
||||
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
} catch (err) {
|
||||
// user likely doesn't have a webcam,
|
||||
// we should still allow to select a microphone
|
||||
if (err.name === "NotFoundError") {
|
||||
constraints = { audio: true };
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
} else {
|
||||
error = err;
|
||||
}
|
||||
}
|
||||
if (error) {
|
||||
logger.log("Failed to list userMedia devices", error);
|
||||
const brand = SdkConfig.get().brand;
|
||||
Modal.createDialog(ErrorDialog, {
|
||||
title: _t('No media permissions'),
|
||||
description: _t(
|
||||
'You may need to manually permit %(brand)s to access your microphone/webcam',
|
||||
{ brand },
|
||||
),
|
||||
});
|
||||
} else {
|
||||
const stream = await requestMediaPermissions();
|
||||
if (stream) {
|
||||
this.refreshMediaDevices(stream);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -754,6 +754,8 @@
|
|||
"Invite to %(spaceName)s": "Invite to %(spaceName)s",
|
||||
"Share your public space": "Share your public space",
|
||||
"Unknown App": "Unknown App",
|
||||
"No media permissions": "No media permissions",
|
||||
"You may need to manually permit %(brand)s to access your microphone/webcam": "You may need to manually permit %(brand)s to access your microphone/webcam",
|
||||
"This homeserver is not configured to display maps.": "This homeserver is not configured to display maps.",
|
||||
"This homeserver is not configured correctly to display maps, or the configured map server may be unreachable.": "This homeserver is not configured correctly to display maps, or the configured map server may be unreachable.",
|
||||
"Toggle attribution": "Toggle attribution",
|
||||
|
@ -1618,8 +1620,6 @@
|
|||
"Rooms outside of a space": "Rooms outside of a space",
|
||||
"Group all your rooms that aren't part of a space in one place.": "Group all your rooms that aren't part of a space in one place.",
|
||||
"Default Device": "Default Device",
|
||||
"No media permissions": "No media permissions",
|
||||
"You may need to manually permit %(brand)s to access your microphone/webcam": "You may need to manually permit %(brand)s to access your microphone/webcam",
|
||||
"Missing media permissions, click the button below to request.": "Missing media permissions, click the button below to request.",
|
||||
"Request media permissions": "Request media permissions",
|
||||
"Audio Output": "Audio Output",
|
||||
|
|
59
src/utils/media/requestMediaPermissions.tsx
Normal file
59
src/utils/media/requestMediaPermissions.tsx
Normal file
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
|
||||
import ErrorDialog from "../../components/views/dialogs/ErrorDialog";
|
||||
import { _t } from "../../languageHandler";
|
||||
import Modal from "../../Modal";
|
||||
import SdkConfig from "../../SdkConfig";
|
||||
|
||||
export const requestMediaPermissions = async (video = true): Promise<MediaStream | undefined> => {
|
||||
let stream: MediaStream | undefined;
|
||||
let error: any;
|
||||
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: true,
|
||||
video,
|
||||
});
|
||||
} catch (err: any) {
|
||||
// user likely doesn't have a webcam,
|
||||
// we should still allow to select a microphone
|
||||
if (video && err.name === "NotFoundError") {
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
} else {
|
||||
error = err;
|
||||
}
|
||||
}
|
||||
if (error) {
|
||||
logger.log("Failed to list userMedia devices", error);
|
||||
const brand = SdkConfig.get().brand;
|
||||
Modal.createDialog(ErrorDialog, {
|
||||
title: _t('No media permissions'),
|
||||
description: _t(
|
||||
'You may need to manually permit %(brand)s to access your microphone/webcam',
|
||||
{ brand },
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
return stream;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue