Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions res/css/_components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
@import "./views/avatars/_WidgetAvatar.scss";
@import "./views/beta/_BetaCard.scss";
@import "./views/context_menus/_CallContextMenu.scss";
@import "./views/context_menus/_DeviceContextMenu.scss";
@import "./views/context_menus/_IconizedContextMenu.scss";
@import "./views/context_menus/_MessageContextMenu.scss";
@import "./views/dialogs/_AddExistingToSpaceDialog.scss";
Expand Down
27 changes: 27 additions & 0 deletions res/css/views/context_menus/_DeviceContextMenu.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright 2021 Šimon Brandner <[email protected]>

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.
*/

.mx_DeviceContextMenu {
max-width: 252px;

.mx_DeviceContextMenu_device_icon {
display: none;
}

.mx_IconizedContextMenu_label {
padding-left: 0 !important;
}
}
5 changes: 5 additions & 0 deletions res/css/views/context_menus/_IconizedContextMenu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ limitations under the License.
padding-right: 20px;
}

.mx_IconizedContextMenu_optionList_label {
font-size: $font-15px;
font-weight: $font-semi-bold;
}

// the notFirst class is for cases where the optionList might be under a header of sorts.
&:nth-child(n + 2), .mx_IconizedContextMenu_optionList_notFirst {
// This is a bit of a hack when we could just use a simple border-top property,
Expand Down
23 changes: 23 additions & 0 deletions res/css/views/voip/CallView/_CallViewButtons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ limitations under the License.
justify-content: center;
align-items: center;

position: relative;

box-shadow: 0px 4px 4px 0px #00000026; // Same on both themes

&::before {
content: '';
display: inline-block;
Expand All @@ -60,6 +64,25 @@ limitations under the License.
width: 24px;
}

&.mx_CallViewButtons_dropdownButton {
width: 16px;
height: 16px;

position: absolute;
right: 0;
bottom: 0;

&::before {
width: 14px;
height: 14px;
mask-image: url('$(res)/img/element-icons/message/chevron-up.svg');
}

&.mx_CallViewButtons_dropdownButton_collapsed::before {
transform: rotate(180deg);
}
}

// State buttons
&.mx_CallViewButtons_button_on {
background-color: $call-view-button-on-background;
Expand Down
33 changes: 23 additions & 10 deletions src/MediaDeviceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export default class MediaDeviceHandler extends EventEmitter {
/**
* Retrieves devices from the SettingsStore and tells the js-sdk to use them
*/
public static loadDevices(): void {
public static async loadDevices(): Promise<void> {
const audioDeviceId = SettingsStore.getValue("webrtc_audioinput");
const videoDeviceId = SettingsStore.getValue("webrtc_videoinput");

MatrixClientPeg.get().getMediaHandler().setAudioInput(audioDeviceId);
MatrixClientPeg.get().getMediaHandler().setVideoInput(videoDeviceId);
await MatrixClientPeg.get().getMediaHandler().setAudioInput(audioDeviceId);
await MatrixClientPeg.get().getMediaHandler().setVideoInput(videoDeviceId);
}

public setAudioOutput(deviceId: string): void {
Expand All @@ -90,26 +90,26 @@ export default class MediaDeviceHandler extends EventEmitter {
* need to be ended and started again for this change to take effect
* @param {string} deviceId
*/
public setAudioInput(deviceId: string): void {
public async setAudioInput(deviceId: string): Promise<void> {
SettingsStore.setValue("webrtc_audioinput", null, SettingLevel.DEVICE, deviceId);
MatrixClientPeg.get().getMediaHandler().setAudioInput(deviceId);
return MatrixClientPeg.get().getMediaHandler().setAudioInput(deviceId);
}

/**
* This will not change the device that a potential call uses. The call will
* need to be ended and started again for this change to take effect
* @param {string} deviceId
*/
public setVideoInput(deviceId: string): void {
public async setVideoInput(deviceId: string): Promise<void> {
SettingsStore.setValue("webrtc_videoinput", null, SettingLevel.DEVICE, deviceId);
MatrixClientPeg.get().getMediaHandler().setVideoInput(deviceId);
return MatrixClientPeg.get().getMediaHandler().setVideoInput(deviceId);
}

public setDevice(deviceId: string, kind: MediaDeviceKindEnum): void {
public async setDevice(deviceId: string, kind: MediaDeviceKindEnum): Promise<void> {
switch (kind) {
case MediaDeviceKindEnum.AudioOutput: this.setAudioOutput(deviceId); break;
case MediaDeviceKindEnum.AudioInput: this.setAudioInput(deviceId); break;
case MediaDeviceKindEnum.VideoInput: this.setVideoInput(deviceId); break;
case MediaDeviceKindEnum.AudioInput: await this.setAudioInput(deviceId); break;
case MediaDeviceKindEnum.VideoInput: await this.setVideoInput(deviceId); break;
}
}

Expand All @@ -124,4 +124,17 @@ export default class MediaDeviceHandler extends EventEmitter {
public static getVideoInput(): string {
return SettingsStore.getValueAt(SettingLevel.DEVICE, "webrtc_videoinput");
}

/**
* Returns the current set deviceId for a device kind
* @param {MediaDeviceKindEnum} kind of the device that will be returned
* @returns {string} the deviceId
*/
public static getDevice(kind: MediaDeviceKindEnum): string {
switch (kind) {
case MediaDeviceKindEnum.AudioOutput: return this.getAudioOutput();
case MediaDeviceKindEnum.AudioInput: return this.getAudioInput();
case MediaDeviceKindEnum.VideoInput: return this.getVideoInput();
}
}
}
89 changes: 89 additions & 0 deletions src/components/views/context_menus/DeviceContextMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2021 Šimon Brandner <[email protected]>

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"),
germain-gg marked this conversation as resolved.
Show resolved Hide resolved
};

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;
10 changes: 9 additions & 1 deletion src/components/views/context_menus/IconizedContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface IProps extends IContextMenuProps {
interface IOptionListProps {
first?: boolean;
red?: boolean;
label?: string;
className?: string;
}

Expand Down Expand Up @@ -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>;
};
Expand Down
3 changes: 3 additions & 0 deletions src/components/views/elements/AccessibleTooltipButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -52,13 +53,15 @@ 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,
});
};

private hideTooltip = (ev: SyntheticEvent) => {
if (this.props.onHover) this.props.onHover(false);
this.setState({
hover: false,
});
Expand Down
Loading