Add ability to change audio and video devices during a call (#7173)
This commit is contained in:
parent
ce3bc9dc07
commit
3c36a7f704
12 changed files with 247 additions and 24 deletions
89
src/components/views/context_menus/DeviceContextMenu.tsx
Normal file
89
src/components/views/context_menus/DeviceContextMenu.tsx
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
Copyright 2021 Šimon Brandner <simon.bra.ag@gmail.com>
|
||||
|
||||
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 React, { useEffect, useState } from "react";
|
||||
|
||||
import MediaDeviceHandler, { MediaDeviceKindEnum } from "../../../MediaDeviceHandler";
|
||||
import IconizedContextMenu, { IconizedContextMenuOptionList, IconizedContextMenuRadio } from "./IconizedContextMenu";
|
||||
import { IProps as IContextMenuProps } from "../../structures/ContextMenu";
|
||||
import { _t, _td } from "../../../languageHandler";
|
||||
|
||||
const SECTION_NAMES: Record<MediaDeviceKindEnum, string> = {
|
||||
[MediaDeviceKindEnum.AudioInput]: _td("Input devices"),
|
||||
[MediaDeviceKindEnum.AudioOutput]: _td("Output devices"),
|
||||
[MediaDeviceKindEnum.VideoInput]: _td("Cameras"),
|
||||
};
|
||||
|
||||
interface IDeviceContextMenuDeviceProps {
|
||||
label: string;
|
||||
selected: boolean;
|
||||
onClick: () => void;
|
||||
}
|
||||
|
||||
const DeviceContextMenuDevice: React.FC<IDeviceContextMenuDeviceProps> = ({ label, selected, onClick }) => {
|
||||
return <IconizedContextMenuRadio
|
||||
iconClassName="mx_DeviceContextMenu_device_icon"
|
||||
label={label}
|
||||
active={selected}
|
||||
onClick={onClick}
|
||||
/>;
|
||||
};
|
||||
|
||||
interface IDeviceContextMenuSectionProps {
|
||||
deviceKind: MediaDeviceKindEnum;
|
||||
}
|
||||
|
||||
const DeviceContextMenuSection: React.FC<IDeviceContextMenuSectionProps> = ({ deviceKind }) => {
|
||||
const [devices, setDevices] = useState<MediaDeviceInfo[]>([]);
|
||||
const [selectedDevice, setSelectedDevice] = useState(MediaDeviceHandler.getDevice(deviceKind));
|
||||
|
||||
useEffect(() => {
|
||||
const getDevices = async () => {
|
||||
return setDevices((await MediaDeviceHandler.getDevices())[deviceKind]);
|
||||
};
|
||||
getDevices();
|
||||
}, [deviceKind]);
|
||||
|
||||
const onDeviceClick = (deviceId: string): void => {
|
||||
MediaDeviceHandler.instance.setDevice(deviceId, deviceKind);
|
||||
setSelectedDevice(deviceId);
|
||||
};
|
||||
|
||||
return <IconizedContextMenuOptionList label={_t(SECTION_NAMES[deviceKind])}>
|
||||
{ devices.map(({ label, deviceId }) => {
|
||||
return <DeviceContextMenuDevice
|
||||
key={deviceId}
|
||||
label={label}
|
||||
selected={selectedDevice === deviceId}
|
||||
onClick={() => onDeviceClick(deviceId)}
|
||||
/>;
|
||||
}) }
|
||||
</IconizedContextMenuOptionList>;
|
||||
};
|
||||
|
||||
interface IProps extends IContextMenuProps {
|
||||
deviceKinds: MediaDeviceKind[];
|
||||
}
|
||||
|
||||
const DeviceContextMenu: React.FC<IProps> = ({ deviceKinds, ...props }) => {
|
||||
return <IconizedContextMenu compact className="mx_DeviceContextMenu" {...props}>
|
||||
{ deviceKinds.map((kind) => {
|
||||
return <DeviceContextMenuSection key={kind} deviceKind={kind as MediaDeviceKindEnum} />;
|
||||
}) }
|
||||
</IconizedContextMenu>;
|
||||
};
|
||||
|
||||
export default DeviceContextMenu;
|
|
@ -33,6 +33,7 @@ interface IProps extends IContextMenuProps {
|
|||
interface IOptionListProps {
|
||||
first?: boolean;
|
||||
red?: boolean;
|
||||
label?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
|
@ -126,13 +127,20 @@ export const IconizedContextMenuOption: React.FC<IOptionProps> = ({
|
|||
</MenuItem>;
|
||||
};
|
||||
|
||||
export const IconizedContextMenuOptionList: React.FC<IOptionListProps> = ({ first, red, className, children }) => {
|
||||
export const IconizedContextMenuOptionList: React.FC<IOptionListProps> = ({
|
||||
first,
|
||||
red,
|
||||
className,
|
||||
label,
|
||||
children,
|
||||
}) => {
|
||||
const classes = classNames("mx_IconizedContextMenu_optionList", className, {
|
||||
mx_IconizedContextMenu_optionList_notFirst: !first,
|
||||
mx_IconizedContextMenu_optionList_red: red,
|
||||
});
|
||||
|
||||
return <div className={classes}>
|
||||
{ label && <div><span className="mx_IconizedContextMenu_optionList_label">{ label }</span></div> }
|
||||
{ children }
|
||||
</div>;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue