signal-desktop/ts/components/CallScreen.tsx

359 lines
10 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { noop } from 'lodash';
2020-06-04 18:16:19 +00:00
import classNames from 'classnames';
import {
HangUpType,
SetLocalAudioType,
2020-08-27 00:03:42 +00:00
SetLocalPreviewType,
2020-06-04 18:16:19 +00:00
SetLocalVideoType,
2020-08-27 00:03:42 +00:00
SetRendererCanvasType,
2020-06-04 18:16:19 +00:00
} from '../state/ducks/calling';
import { Avatar } from './Avatar';
2020-11-17 15:07:53 +00:00
import { CallingHeader } from './CallingHeader';
2020-10-08 01:25:33 +00:00
import { CallingButton, CallingButtonType } from './CallingButton';
import { CallBackgroundBlur } from './CallBackgroundBlur';
2020-11-13 19:57:55 +00:00
import {
2020-12-02 18:14:03 +00:00
ActiveCallType,
2020-11-13 19:57:55 +00:00
CallMode,
CallState,
GroupCallConnectionState,
GroupCallVideoRequest,
2020-11-13 19:57:55 +00:00
VideoFrameSource,
} from '../types/Calling';
import { ColorType } from '../types/Colors';
2020-06-04 18:16:19 +00:00
import { LocalizerType } from '../types/Util';
2020-11-13 19:57:55 +00:00
import { missingCaseError } from '../util/missingCaseError';
import { DirectCallRemoteParticipant } from './DirectCallRemoteParticipant';
import { GroupCallRemoteParticipants } from './GroupCallRemoteParticipants';
import { GroupCallToastManager } from './GroupCallToastManager';
2020-06-04 18:16:19 +00:00
export type PropsType = {
activeCall: ActiveCallType;
2020-11-13 19:57:55 +00:00
getGroupCallVideoFrameSource: (demuxId: number) => VideoFrameSource;
2020-06-04 18:16:19 +00:00
hangUp: (_: HangUpType) => void;
i18n: LocalizerType;
joinedAt?: number;
me: {
avatarPath?: string;
color?: ColorType;
name?: string;
phoneNumber?: string;
profileName?: string;
title: string;
};
setGroupCallVideoRequest: (_: Array<GroupCallVideoRequest>) => void;
2020-06-04 18:16:19 +00:00
setLocalAudio: (_: SetLocalAudioType) => void;
setLocalVideo: (_: SetLocalVideoType) => void;
2020-08-27 00:03:42 +00:00
setLocalPreview: (_: SetLocalPreviewType) => void;
setRendererCanvas: (_: SetRendererCanvasType) => void;
2020-11-17 15:07:53 +00:00
stickyControls: boolean;
toggleParticipants: () => void;
2020-10-01 00:43:05 +00:00
togglePip: () => void;
2020-08-27 00:03:42 +00:00
toggleSettings: () => void;
2020-06-04 18:16:19 +00:00
};
export const CallScreen: React.FC<PropsType> = ({
activeCall,
2020-11-13 19:57:55 +00:00
getGroupCallVideoFrameSource,
hangUp,
i18n,
joinedAt,
me,
setGroupCallVideoRequest,
setLocalAudio,
setLocalVideo,
setLocalPreview,
setRendererCanvas,
2020-11-17 15:07:53 +00:00
stickyControls,
toggleParticipants,
togglePip,
toggleSettings,
}) => {
2020-12-02 18:14:03 +00:00
const {
conversation,
hasLocalAudio,
hasLocalVideo,
showParticipantsList,
} = activeCall;
const toggleAudio = useCallback(() => {
setLocalAudio({
enabled: !hasLocalAudio,
});
}, [setLocalAudio, hasLocalAudio]);
2020-06-04 18:16:19 +00:00
const toggleVideo = useCallback(() => {
setLocalVideo({
enabled: !hasLocalVideo,
});
}, [setLocalVideo, hasLocalVideo]);
2020-06-04 18:16:19 +00:00
const [acceptedDuration, setAcceptedDuration] = useState<number | null>(null);
const [showControls, setShowControls] = useState(true);
2020-06-04 18:16:19 +00:00
const localVideoRef = useRef<HTMLVideoElement | null>(null);
2020-06-04 18:16:19 +00:00
useEffect(() => {
setLocalPreview({ element: localVideoRef });
return () => {
setLocalPreview({ element: undefined });
};
}, [setLocalPreview, setRendererCanvas]);
2020-06-04 18:16:19 +00:00
useEffect(() => {
if (!joinedAt) {
return noop;
2020-06-04 18:16:19 +00:00
}
// It's really jumpy with a value of 500ms.
const interval = setInterval(() => {
setAcceptedDuration(Date.now() - joinedAt);
}, 100);
return clearInterval.bind(null, interval);
}, [joinedAt]);
useEffect(() => {
2020-11-17 15:07:53 +00:00
if (!showControls || stickyControls) {
return noop;
2020-06-04 18:16:19 +00:00
}
const timer = setTimeout(() => {
setShowControls(false);
2020-06-04 18:16:19 +00:00
}, 5000);
return clearInterval.bind(null, timer);
2020-11-17 15:07:53 +00:00
}, [showControls, stickyControls]);
useEffect(() => {
const 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();
setShowControls(true);
}
};
2020-06-04 18:16:19 +00:00
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [toggleAudio, toggleVideo]);
2020-06-04 18:16:19 +00:00
2020-12-02 18:14:03 +00:00
const hasRemoteVideo = activeCall.remoteParticipants.some(
remoteParticipant => remoteParticipant.hasRemoteVideo
);
2020-11-23 21:37:39 +00:00
let headerMessage: string | undefined;
let headerTitle: string | undefined;
2020-11-13 19:57:55 +00:00
let isConnected: boolean;
2020-11-23 21:37:39 +00:00
let participantCount: number;
2020-11-17 15:07:53 +00:00
let remoteParticipantsElement: JSX.Element;
2020-06-04 18:16:19 +00:00
2020-12-02 18:14:03 +00:00
switch (activeCall.callMode) {
2020-11-13 19:57:55 +00:00
case CallMode.Direct:
2020-11-23 21:37:39 +00:00
headerMessage = renderHeaderMessage(
i18n,
2020-12-02 18:14:03 +00:00
activeCall.callState || CallState.Prering,
2020-11-23 21:37:39 +00:00
acceptedDuration
);
headerTitle = conversation.title;
2020-12-02 18:14:03 +00:00
isConnected = activeCall.callState === CallState.Accepted;
2020-11-23 21:37:39 +00:00
participantCount = isConnected ? 2 : 0;
2020-11-17 15:07:53 +00:00
remoteParticipantsElement = (
2020-11-13 19:57:55 +00:00
<DirectCallRemoteParticipant
conversation={conversation}
hasRemoteVideo={hasRemoteVideo}
i18n={i18n}
setRendererCanvas={setRendererCanvas}
/>
);
break;
case CallMode.Group:
2020-12-02 18:14:03 +00:00
participantCount = activeCall.remoteParticipants.length + 1;
2020-11-23 21:37:39 +00:00
headerMessage = undefined;
2020-12-02 18:14:03 +00:00
headerTitle = activeCall.remoteParticipants.length
2020-11-23 21:37:39 +00:00
? undefined
: i18n('calling__in-this-call--zero');
2020-12-02 18:14:03 +00:00
isConnected =
activeCall.connectionState === GroupCallConnectionState.Connected;
2020-11-17 15:07:53 +00:00
remoteParticipantsElement = (
2020-11-13 19:57:55 +00:00
<GroupCallRemoteParticipants
getGroupCallVideoFrameSource={getGroupCallVideoFrameSource}
i18n={i18n}
2020-12-02 18:14:03 +00:00
remoteParticipants={activeCall.remoteParticipants}
setGroupCallVideoRequest={setGroupCallVideoRequest}
2020-11-13 19:57:55 +00:00
/>
);
break;
default:
2020-12-02 18:14:03 +00:00
throw missingCaseError(activeCall);
2020-11-13 19:57:55 +00:00
}
const videoButtonType = hasLocalVideo
? CallingButtonType.VIDEO_ON
: CallingButtonType.VIDEO_OFF;
const audioButtonType = hasLocalAudio
? CallingButtonType.AUDIO_ON
: CallingButtonType.AUDIO_OFF;
2020-11-13 19:57:55 +00:00
const isAudioOnly = !hasLocalVideo && !hasRemoteVideo;
const controlsFadeClass = classNames({
'module-ongoing-call__controls--fadeIn':
(showControls || isAudioOnly) && !isConnected,
'module-ongoing-call__controls--fadeOut':
!showControls && !isAudioOnly && isConnected,
});
return (
<div
2020-11-13 19:57:55 +00:00
className={classNames(
'module-calling__container',
`module-ongoing-call__container--${getCallModeClassSuffix(
2020-12-02 18:14:03 +00:00
activeCall.callMode
2020-11-13 19:57:55 +00:00
)}`
)}
onMouseMove={() => {
setShowControls(true);
}}
role="group"
>
2020-12-02 18:14:03 +00:00
{activeCall.callMode === CallMode.Group ? (
<GroupCallToastManager
2020-12-02 18:14:03 +00:00
connectionState={activeCall.connectionState}
i18n={i18n}
/>
) : null}
2020-06-04 18:16:19 +00:00
<div
2020-11-17 15:07:53 +00:00
className={classNames('module-ongoing-call__header', controlsFadeClass)}
2020-06-04 18:16:19 +00:00
>
2020-11-17 15:07:53 +00:00
<CallingHeader
canPip
i18n={i18n}
2020-12-02 18:14:03 +00:00
isGroupCall={activeCall.callMode === CallMode.Group}
2020-11-23 21:37:39 +00:00
message={headerMessage}
participantCount={participantCount}
2020-11-20 19:39:50 +00:00
showParticipantsList={showParticipantsList}
2020-11-23 21:37:39 +00:00
title={headerTitle}
2020-11-17 15:07:53 +00:00
toggleParticipants={toggleParticipants}
togglePip={togglePip}
toggleSettings={toggleSettings}
/>
2020-06-04 18:16:19 +00:00
</div>
2020-11-17 15:07:53 +00:00
{remoteParticipantsElement}
<div className="module-ongoing-call__footer">
{/* This layout-only element is not ideal.
See the comment in _modules.css for more. */}
<div className="module-ongoing-call__footer__local-preview-offset" />
<div
className={classNames(
'module-ongoing-call__footer__actions',
controlsFadeClass
)}
>
<CallingButton
buttonType={videoButtonType}
i18n={i18n}
onClick={toggleVideo}
/>
<CallingButton
buttonType={audioButtonType}
i18n={i18n}
onClick={toggleAudio}
/>
<CallingButton
buttonType={CallingButtonType.HANG_UP}
i18n={i18n}
onClick={() => {
hangUp({ conversationId: conversation.id });
}}
/>
</div>
<div
className={classNames('module-ongoing-call__footer__local-preview', {
'module-ongoing-call__footer__local-preview--audio-muted': !hasLocalAudio,
})}
>
{hasLocalVideo ? (
<video
className="module-ongoing-call__footer__local-preview__video"
ref={localVideoRef}
autoPlay
/>
) : (
<CallBackgroundBlur avatarPath={me.avatarPath} color={me.color}>
<Avatar
avatarPath={me.avatarPath}
color={me.color || 'ultramarine'}
noteToSelf={false}
conversationType="direct"
i18n={i18n}
name={me.name}
phoneNumber={me.phoneNumber}
profileName={me.profileName}
title={me.title}
size={80}
/>
</CallBackgroundBlur>
)}
</div>
2020-06-04 18:16:19 +00:00
</div>
</div>
);
};
2020-06-04 18:16:19 +00:00
2020-11-13 19:57:55 +00:00
function getCallModeClassSuffix(
callMode: CallMode.Direct | CallMode.Group
): string {
switch (callMode) {
case CallMode.Direct:
return 'direct';
case CallMode.Group:
return 'group';
default:
throw missingCaseError(callMode);
}
}
2020-06-04 18:16:19 +00:00
function renderHeaderMessage(
i18n: LocalizerType,
callState: CallState,
acceptedDuration: null | number
2020-11-23 21:37:39 +00:00
): string | undefined {
let message;
if (callState === CallState.Prering) {
message = i18n('outgoingCallPrering');
} else if (callState === CallState.Ringing) {
message = i18n('outgoingCallRinging');
} else if (callState === CallState.Reconnecting) {
message = i18n('callReconnecting');
} else if (callState === CallState.Accepted && acceptedDuration) {
message = i18n('callDuration', [renderDuration(acceptedDuration)]);
2020-06-04 18:16:19 +00:00
}
2020-11-23 21:37:39 +00:00
return message;
}
2020-06-04 18:16:19 +00:00
function renderDuration(ms: number): string {
const secs = Math.floor((ms / 1000) % 60)
.toString()
.padStart(2, '0');
const mins = Math.floor((ms / 60000) % 60)
.toString()
.padStart(2, '0');
const hours = Math.floor(ms / 3600000);
if (hours > 0) {
return `${hours}:${mins}:${secs}`;
2020-06-04 18:16:19 +00:00
}
return `${mins}:${secs}`;
2020-06-04 18:16:19 +00:00
}