Add ability to change audio and video devices during a call (#7173)

This commit is contained in:
Šimon Brandner 2022-05-04 16:41:56 +02:00 committed by GitHub
parent ce3bc9dc07
commit 3c36a7f704
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 247 additions and 24 deletions

View 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;

View file

@ -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>;
};

View file

@ -28,6 +28,7 @@ interface IProps extends React.ComponentProps<typeof AccessibleButton> {
forceHide?: boolean;
yOffset?: number;
alignment?: Alignment;
onHover?: (hovering: boolean) => void;
onHideTooltip?(ev: SyntheticEvent): void;
}
@ -52,6 +53,7 @@ export default class AccessibleTooltipButton extends React.PureComponent<IProps,
}
private showTooltip = () => {
if (this.props.onHover) this.props.onHover(true);
if (this.props.forceHide) return;
this.setState({
hover: true,
@ -59,6 +61,7 @@ export default class AccessibleTooltipButton extends React.PureComponent<IProps,
};
private hideTooltip = (ev: SyntheticEvent) => {
if (this.props.onHover) this.props.onHover(false);
this.setState({
hover: false,
});

View file

@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { createRef } from "react";
import React, { createRef, useState } from "react";
import classNames from "classnames";
import { MatrixCall } from "matrix-js-sdk/src/webrtc/call";
@ -26,10 +26,14 @@ import DialpadContextMenu from "../../context_menus/DialpadContextMenu";
import { Alignment } from "../../elements/Tooltip";
import {
alwaysAboveLeftOf,
alwaysAboveRightOf,
ChevronFace,
ContextMenuTooltipButton,
useContextMenu,
} from '../../../structures/ContextMenu';
import { _t } from "../../../../languageHandler";
import DeviceContextMenu from "../../context_menus/DeviceContextMenu";
import { MediaDeviceKindEnum } from "../../../../MediaDeviceHandler";
// Height of the header duplicated from CSS because we need to subtract it from our max
// height to get the max height of the video
@ -39,15 +43,22 @@ const TOOLTIP_Y_OFFSET = -24;
const CONTROLS_HIDE_DELAY = 2000;
interface IButtonProps {
interface IButtonProps extends Omit<React.ComponentProps<typeof AccessibleTooltipButton>, "title"> {
state: boolean;
className: string;
onLabel: string;
offLabel: string;
onClick: () => void;
onLabel?: string;
offLabel?: string;
onClick: (event: React.MouseEvent) => void;
}
const CallViewToggleButton: React.FC<IButtonProps> = ({ state: isOn, className, onLabel, offLabel, onClick }) => {
const CallViewToggleButton: React.FC<IButtonProps> = ({
children,
state: isOn,
className,
onLabel,
offLabel,
...props
}) => {
const classes = classNames("mx_CallViewButtons_button", className, {
mx_CallViewButtons_button_on: isOn,
mx_CallViewButtons_button_off: !isOn,
@ -56,11 +67,48 @@ const CallViewToggleButton: React.FC<IButtonProps> = ({ state: isOn, className,
return (
<AccessibleTooltipButton
className={classes}
onClick={onClick}
title={isOn ? onLabel : offLabel}
alignment={Alignment.Top}
yOffset={TOOLTIP_Y_OFFSET}
/>
{...props}
>
{ children }
</AccessibleTooltipButton>
);
};
interface IDropdownButtonProps extends IButtonProps {
deviceKinds: MediaDeviceKindEnum[];
}
const CallViewDropdownButton: React.FC<IDropdownButtonProps> = ({ state, deviceKinds, ...props }) => {
const [menuDisplayed, buttonRef, openMenu, closeMenu] = useContextMenu();
const [hoveringDropdown, setHoveringDropdown] = useState(false);
const classes = classNames("mx_CallViewButtons_button", "mx_CallViewButtons_dropdownButton", {
mx_CallViewButtons_dropdownButton_collapsed: !menuDisplayed,
});
const onClick = (event: React.MouseEvent): void => {
event.stopPropagation();
openMenu();
};
return (
<CallViewToggleButton inputRef={buttonRef} forceHide={menuDisplayed || hoveringDropdown} state={state} {...props}>
<CallViewToggleButton
className={classes}
onClick={onClick}
onHover={(hovering) => setHoveringDropdown(hovering)}
state={state}
/>
{ menuDisplayed && <DeviceContextMenu
{...alwaysAboveRightOf(buttonRef.current?.getBoundingClientRect())}
onFinished={closeMenu}
deviceKinds={deviceKinds}
/> }
</CallViewToggleButton>
);
};
@ -221,19 +269,21 @@ export default class CallViewButtons extends React.Component<IProps, IState> {
alignment={Alignment.Top}
yOffset={TOOLTIP_Y_OFFSET}
/> }
<CallViewToggleButton
<CallViewDropdownButton
state={!this.props.buttonsState.micMuted}
className="mx_CallViewButtons_button_mic"
onLabel={_t("Mute the microphone")}
offLabel={_t("Unmute the microphone")}
onClick={this.props.handlers.onMicMuteClick}
deviceKinds={[MediaDeviceKindEnum.AudioInput, MediaDeviceKindEnum.AudioOutput]}
/>
{ this.props.buttonsVisibility.vidMute && <CallViewToggleButton
{ this.props.buttonsVisibility.vidMute && <CallViewDropdownButton
state={!this.props.buttonsState.vidMuted}
className="mx_CallViewButtons_button_vid"
onLabel={_t("Stop the camera")}
offLabel={_t("Start the camera")}
onClick={this.props.handlers.onVidMuteClick}
deviceKinds={[MediaDeviceKindEnum.VideoInput]}
/> }
{ this.props.buttonsVisibility.screensharing && <CallViewToggleButton
state={this.props.buttonsState.screensharing}

View file

@ -161,6 +161,7 @@ export default class VideoFeed extends React.PureComponent<IProps, IState> {
audioMuted: this.props.feed.isAudioMuted(),
videoMuted: this.props.feed.isVideoMuted(),
});
this.playMedia();
};
private onMuteStateChanged = () => {