2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-12-08 22:28:44 +00:00
|
|
|
import React, { ReactNode } from 'react';
|
2020-12-12 00:44:07 +00:00
|
|
|
import Measure from 'react-measure';
|
|
|
|
import { debounce } from 'lodash';
|
2020-10-08 01:25:33 +00:00
|
|
|
import {
|
|
|
|
SetLocalAudioType,
|
|
|
|
SetLocalPreviewType,
|
|
|
|
SetLocalVideoType,
|
|
|
|
} from '../state/ducks/calling';
|
2020-11-19 18:11:35 +00:00
|
|
|
import { CallingButton, CallingButtonType } from './CallingButton';
|
|
|
|
import { TooltipPlacement } from './Tooltip';
|
2020-10-08 01:25:33 +00:00
|
|
|
import { CallBackgroundBlur } from './CallBackgroundBlur';
|
2020-11-17 15:07:53 +00:00
|
|
|
import { CallingHeader } from './CallingHeader';
|
|
|
|
import { Spinner } from './Spinner';
|
2020-11-06 17:36:37 +00:00
|
|
|
import { ColorType } from '../types/Colors';
|
2020-11-17 15:07:53 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
2020-12-08 19:37:04 +00:00
|
|
|
import { ConversationType } from '../state/ducks/conversations';
|
2020-12-12 00:44:07 +00:00
|
|
|
import {
|
|
|
|
REQUESTED_VIDEO_WIDTH,
|
|
|
|
REQUESTED_VIDEO_HEIGHT,
|
|
|
|
} from '../calling/constants';
|
|
|
|
|
|
|
|
// We request dimensions but may not get them depending on the user's webcam. This is our
|
|
|
|
// fallback while we don't know.
|
|
|
|
const VIDEO_ASPECT_RATIO_FALLBACK =
|
|
|
|
REQUESTED_VIDEO_WIDTH / REQUESTED_VIDEO_HEIGHT;
|
2020-10-08 01:25:33 +00:00
|
|
|
|
|
|
|
export type PropsType = {
|
2020-10-14 16:30:50 +00:00
|
|
|
availableCameras: Array<MediaDeviceInfo>;
|
2020-11-06 17:36:37 +00:00
|
|
|
conversation: {
|
|
|
|
title: string;
|
|
|
|
};
|
2020-10-08 01:25:33 +00:00
|
|
|
hasLocalAudio: boolean;
|
|
|
|
hasLocalVideo: boolean;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
isGroupCall: boolean;
|
2020-11-20 20:14:07 +00:00
|
|
|
isCallFull?: boolean;
|
2020-11-09 18:56:20 +00:00
|
|
|
me: {
|
|
|
|
avatarPath?: string;
|
|
|
|
color?: ColorType;
|
2020-12-08 19:37:04 +00:00
|
|
|
uuid: string;
|
2020-11-09 18:56:20 +00:00
|
|
|
};
|
2020-10-08 01:25:33 +00:00
|
|
|
onCallCanceled: () => void;
|
|
|
|
onJoinCall: () => void;
|
2020-12-08 19:37:04 +00:00
|
|
|
peekedParticipants: Array<ConversationType>;
|
2020-10-08 01:25:33 +00:00
|
|
|
setLocalAudio: (_: SetLocalAudioType) => void;
|
|
|
|
setLocalVideo: (_: SetLocalVideoType) => void;
|
|
|
|
setLocalPreview: (_: SetLocalPreviewType) => void;
|
2020-11-20 19:39:50 +00:00
|
|
|
showParticipantsList: boolean;
|
2020-10-08 01:25:33 +00:00
|
|
|
toggleParticipants: () => void;
|
|
|
|
toggleSettings: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const CallingLobby = ({
|
2020-10-14 16:30:50 +00:00
|
|
|
availableCameras,
|
2020-11-06 17:36:37 +00:00
|
|
|
conversation,
|
2020-10-08 01:25:33 +00:00
|
|
|
hasLocalAudio,
|
|
|
|
hasLocalVideo,
|
|
|
|
i18n,
|
|
|
|
isGroupCall = false,
|
2020-11-20 20:14:07 +00:00
|
|
|
isCallFull = false,
|
2020-11-09 18:56:20 +00:00
|
|
|
me,
|
2020-10-08 01:25:33 +00:00
|
|
|
onCallCanceled,
|
|
|
|
onJoinCall,
|
2020-12-02 18:14:03 +00:00
|
|
|
peekedParticipants,
|
2020-10-08 01:25:33 +00:00
|
|
|
setLocalAudio,
|
|
|
|
setLocalPreview,
|
|
|
|
setLocalVideo,
|
2020-11-20 19:39:50 +00:00
|
|
|
showParticipantsList,
|
2020-10-08 01:25:33 +00:00
|
|
|
toggleParticipants,
|
|
|
|
toggleSettings,
|
|
|
|
}: PropsType): JSX.Element => {
|
2020-12-12 00:44:07 +00:00
|
|
|
const [
|
|
|
|
localPreviewContainerWidth,
|
|
|
|
setLocalPreviewContainerWidth,
|
|
|
|
] = React.useState<null | number>(null);
|
|
|
|
const [
|
|
|
|
localPreviewContainerHeight,
|
|
|
|
setLocalPreviewContainerHeight,
|
|
|
|
] = React.useState<null | number>(null);
|
|
|
|
const [localVideoAspectRatio, setLocalVideoAspectRatio] = React.useState(
|
|
|
|
VIDEO_ASPECT_RATIO_FALLBACK
|
|
|
|
);
|
|
|
|
const localVideoRef = React.useRef<null | HTMLVideoElement>(null);
|
2020-10-08 01:25:33 +00:00
|
|
|
|
|
|
|
const toggleAudio = React.useCallback((): void => {
|
|
|
|
setLocalAudio({ enabled: !hasLocalAudio });
|
2020-11-06 17:36:37 +00:00
|
|
|
}, [hasLocalAudio, setLocalAudio]);
|
2020-10-08 01:25:33 +00:00
|
|
|
|
|
|
|
const toggleVideo = React.useCallback((): void => {
|
|
|
|
setLocalVideo({ enabled: !hasLocalVideo });
|
2020-11-06 17:36:37 +00:00
|
|
|
}, [hasLocalVideo, setLocalVideo]);
|
2020-10-08 01:25:33 +00:00
|
|
|
|
2020-12-12 00:44:07 +00:00
|
|
|
const hasEverMeasured =
|
|
|
|
localPreviewContainerWidth !== null && localPreviewContainerHeight !== null;
|
|
|
|
const setLocalPreviewContainerDimensions = React.useMemo(() => {
|
|
|
|
const set = (bounds: Readonly<{ width: number; height: number }>) => {
|
|
|
|
setLocalPreviewContainerWidth(bounds.width);
|
|
|
|
setLocalPreviewContainerHeight(bounds.height);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hasEverMeasured) {
|
|
|
|
return debounce(set, 100, { maxWait: 3000 });
|
|
|
|
}
|
|
|
|
return set;
|
|
|
|
}, [
|
|
|
|
hasEverMeasured,
|
|
|
|
setLocalPreviewContainerWidth,
|
|
|
|
setLocalPreviewContainerHeight,
|
|
|
|
]);
|
|
|
|
|
2020-10-08 01:25:33 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
setLocalPreview({ element: localVideoRef });
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
setLocalPreview({ element: undefined });
|
|
|
|
};
|
|
|
|
}, [setLocalPreview]);
|
|
|
|
|
2020-12-12 00:44:07 +00:00
|
|
|
// This isn't perfect because it doesn't react to changes in the webcam's aspect ratio.
|
|
|
|
// For example, if you changed from Webcam A to Webcam B and Webcam B had a different
|
|
|
|
// aspect ratio, we wouldn't update.
|
|
|
|
//
|
|
|
|
// Unfortunately, RingRTC (1) doesn't update these dimensions with the "real" camera
|
|
|
|
// dimensions (2) doesn't give us any hooks or callbacks. For now, this works okay.
|
|
|
|
// We have `object-fit: contain` in the CSS in case we're wrong; not ideal, but
|
|
|
|
// usable.
|
|
|
|
React.useEffect(() => {
|
|
|
|
const videoEl = localVideoRef.current;
|
|
|
|
if (hasLocalVideo && videoEl && videoEl.width && videoEl.height) {
|
|
|
|
setLocalVideoAspectRatio(videoEl.width / videoEl.height);
|
|
|
|
}
|
|
|
|
}, [hasLocalVideo, setLocalVideoAspectRatio]);
|
|
|
|
|
2020-10-08 01:25:33 +00:00
|
|
|
React.useEffect(() => {
|
|
|
|
function handleKeyDown(event: KeyboardEvent): void {
|
|
|
|
let eventHandled = false;
|
|
|
|
|
|
|
|
if (event.shiftKey && (event.key === 'V' || event.key === 'v')) {
|
|
|
|
toggleVideo();
|
|
|
|
eventHandled = true;
|
|
|
|
} else if (event.shiftKey && (event.key === 'M' || event.key === 'm')) {
|
|
|
|
toggleAudio();
|
|
|
|
eventHandled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eventHandled) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handleKeyDown);
|
|
|
|
};
|
|
|
|
}, [toggleVideo, toggleAudio]);
|
|
|
|
|
2020-11-17 15:07:53 +00:00
|
|
|
const [isCallConnecting, setIsCallConnecting] = React.useState(false);
|
|
|
|
|
2020-10-14 16:30:50 +00:00
|
|
|
// eslint-disable-next-line no-nested-ternary
|
2020-10-08 01:25:33 +00:00
|
|
|
const videoButtonType = hasLocalVideo
|
|
|
|
? CallingButtonType.VIDEO_ON
|
2020-10-14 16:30:50 +00:00
|
|
|
: availableCameras.length === 0
|
|
|
|
? CallingButtonType.VIDEO_DISABLED
|
2020-10-08 01:25:33 +00:00
|
|
|
: CallingButtonType.VIDEO_OFF;
|
|
|
|
const audioButtonType = hasLocalAudio
|
|
|
|
? CallingButtonType.AUDIO_ON
|
|
|
|
: CallingButtonType.AUDIO_OFF;
|
|
|
|
|
2020-12-02 18:14:03 +00:00
|
|
|
// It should be rare to see yourself in this list, but it's possible if (1) you rejoin
|
|
|
|
// quickly, causing the server to return stale state (2) you have joined on another
|
|
|
|
// device.
|
|
|
|
const participantNames = peekedParticipants.map(participant =>
|
2020-12-08 19:37:04 +00:00
|
|
|
participant.uuid === me.uuid
|
2020-12-02 18:14:03 +00:00
|
|
|
? i18n('you')
|
|
|
|
: participant.firstName || participant.title
|
|
|
|
);
|
2020-12-08 23:35:21 +00:00
|
|
|
const hasYou = peekedParticipants.some(
|
|
|
|
participant => participant.uuid === me.uuid
|
|
|
|
);
|
2020-12-02 18:14:03 +00:00
|
|
|
|
2020-12-08 22:28:44 +00:00
|
|
|
const canJoin = !isCallFull && !isCallConnecting;
|
|
|
|
|
|
|
|
let joinButtonChildren: ReactNode;
|
2020-11-20 20:14:07 +00:00
|
|
|
if (isCallFull) {
|
2020-12-08 22:28:44 +00:00
|
|
|
joinButtonChildren = i18n('calling__call-is-full');
|
2020-11-20 20:14:07 +00:00
|
|
|
} else if (isCallConnecting) {
|
2020-12-08 22:28:44 +00:00
|
|
|
joinButtonChildren = <Spinner svgSize="small" />;
|
|
|
|
} else if (peekedParticipants.length) {
|
|
|
|
joinButtonChildren = i18n('calling__join');
|
2020-11-20 20:14:07 +00:00
|
|
|
} else {
|
2020-12-08 22:28:44 +00:00
|
|
|
joinButtonChildren = i18n('calling__start');
|
2020-11-20 20:14:07 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 00:44:07 +00:00
|
|
|
let localPreviewStyles: React.CSSProperties;
|
|
|
|
// It'd be nice to use `hasEverMeasured` here, too, but TypeScript isn't smart enough
|
|
|
|
// to understand the logic here.
|
|
|
|
if (
|
|
|
|
localPreviewContainerWidth !== null &&
|
|
|
|
localPreviewContainerHeight !== null
|
|
|
|
) {
|
|
|
|
const containerAspectRatio =
|
|
|
|
localPreviewContainerWidth / localPreviewContainerHeight;
|
|
|
|
localPreviewStyles =
|
|
|
|
containerAspectRatio < localVideoAspectRatio
|
|
|
|
? {
|
|
|
|
width: '100%',
|
|
|
|
height: Math.floor(
|
|
|
|
localPreviewContainerWidth / localVideoAspectRatio
|
|
|
|
),
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
width: Math.floor(
|
|
|
|
localPreviewContainerHeight * localVideoAspectRatio
|
|
|
|
),
|
|
|
|
height: '100%',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
localPreviewStyles = { display: 'none' };
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:25:33 +00:00
|
|
|
return (
|
|
|
|
<div className="module-calling__container">
|
2020-11-17 15:07:53 +00:00
|
|
|
<CallingHeader
|
2020-11-23 21:37:39 +00:00
|
|
|
title={conversation.title}
|
2020-11-17 15:07:53 +00:00
|
|
|
i18n={i18n}
|
|
|
|
isGroupCall={isGroupCall}
|
2020-12-02 18:14:03 +00:00
|
|
|
participantCount={peekedParticipants.length}
|
2020-11-20 19:39:50 +00:00
|
|
|
showParticipantsList={showParticipantsList}
|
2020-11-17 15:07:53 +00:00
|
|
|
toggleParticipants={toggleParticipants}
|
|
|
|
toggleSettings={toggleSettings}
|
|
|
|
/>
|
|
|
|
|
2020-12-12 00:44:07 +00:00
|
|
|
<Measure
|
|
|
|
bounds
|
|
|
|
onResize={({ bounds }) => {
|
|
|
|
if (!bounds) {
|
|
|
|
window.log.error('We should be measuring bounds');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setLocalPreviewContainerDimensions(bounds);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{({ measureRef }) => (
|
|
|
|
<div
|
|
|
|
ref={measureRef}
|
|
|
|
className="module-calling-lobby__local-preview-container"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="module-calling-lobby__local-preview"
|
|
|
|
style={localPreviewStyles}
|
|
|
|
>
|
|
|
|
{hasLocalVideo && availableCameras.length > 0 ? (
|
|
|
|
<video
|
|
|
|
className="module-calling-lobby__local-preview__video-on"
|
|
|
|
ref={localVideoRef}
|
|
|
|
autoPlay
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<CallBackgroundBlur avatarPath={me.avatarPath} color={me.color}>
|
|
|
|
<div className="module-calling-lobby__local-preview__video-off__icon" />
|
|
|
|
<span className="module-calling-lobby__local-preview__video-off__text">
|
|
|
|
{i18n('calling__your-video-is-off')}
|
|
|
|
</span>
|
|
|
|
</CallBackgroundBlur>
|
|
|
|
)}
|
2020-10-08 01:25:33 +00:00
|
|
|
|
2020-12-12 00:44:07 +00:00
|
|
|
<div className="module-calling__buttons">
|
|
|
|
<CallingButton
|
|
|
|
buttonType={videoButtonType}
|
|
|
|
i18n={i18n}
|
|
|
|
onClick={toggleVideo}
|
|
|
|
tooltipDirection={TooltipPlacement.Top}
|
|
|
|
/>
|
|
|
|
<CallingButton
|
|
|
|
buttonType={audioButtonType}
|
|
|
|
i18n={i18n}
|
|
|
|
onClick={toggleAudio}
|
|
|
|
tooltipDirection={TooltipPlacement.Top}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Measure>
|
2020-10-08 01:25:33 +00:00
|
|
|
|
2020-11-17 15:07:53 +00:00
|
|
|
{isGroupCall ? (
|
|
|
|
<div className="module-calling-lobby__info">
|
|
|
|
{participantNames.length === 0 &&
|
|
|
|
i18n('calling__lobby-summary--zero')}
|
|
|
|
{participantNames.length === 1 &&
|
2020-12-08 23:35:21 +00:00
|
|
|
hasYou &&
|
|
|
|
i18n('calling__lobby-summary--self')}
|
|
|
|
{participantNames.length === 1 &&
|
|
|
|
!hasYou &&
|
2020-11-17 15:07:53 +00:00
|
|
|
i18n('calling__lobby-summary--single', participantNames)}
|
|
|
|
{participantNames.length === 2 &&
|
|
|
|
i18n('calling__lobby-summary--double', {
|
|
|
|
first: participantNames[0],
|
|
|
|
second: participantNames[1],
|
|
|
|
})}
|
|
|
|
{participantNames.length === 3 &&
|
|
|
|
i18n('calling__lobby-summary--triple', {
|
|
|
|
first: participantNames[0],
|
|
|
|
second: participantNames[1],
|
|
|
|
third: participantNames[2],
|
|
|
|
})}
|
|
|
|
{participantNames.length > 3 &&
|
|
|
|
i18n('calling__lobby-summary--many', {
|
|
|
|
first: participantNames[0],
|
|
|
|
second: participantNames[1],
|
|
|
|
others: String(participantNames.length - 2),
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
|
2020-10-08 01:25:33 +00:00
|
|
|
<div className="module-calling-lobby__actions">
|
|
|
|
<button
|
|
|
|
className="module-button__gray module-calling-lobby__button"
|
|
|
|
onClick={onCallCanceled}
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
{i18n('cancel')}
|
|
|
|
</button>
|
2020-12-08 22:28:44 +00:00
|
|
|
<button
|
|
|
|
className="module-button__green module-calling-lobby__button"
|
|
|
|
disabled={!canJoin}
|
|
|
|
onClick={
|
|
|
|
canJoin
|
|
|
|
? () => {
|
|
|
|
setIsCallConnecting(true);
|
|
|
|
onJoinCall();
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
tabIndex={0}
|
|
|
|
type="button"
|
|
|
|
>
|
|
|
|
{joinButtonChildren}
|
|
|
|
</button>
|
2020-10-08 01:25:33 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|