Signal-Desktop/ts/components/CallScreen.tsx

565 lines
17 KiB
TypeScript
Raw Normal View History

// Copyright 2020-2022 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReactNode } from 'react';
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 type { VideoFrameSource } from 'ringrtc';
import type {
ActiveCallStateType,
2020-06-04 18:16:19 +00:00
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';
import { CallingPreCallInfo, RingMode } from './CallingPreCallInfo';
2020-10-08 01:25:33 +00:00
import { CallingButton, CallingButtonType } from './CallingButton';
import { CallBackgroundBlur } from './CallBackgroundBlur';
import type {
2020-12-02 18:14:03 +00:00
ActiveCallType,
GroupCallVideoRequest,
PresentedSource,
} from '../types/Calling';
import {
2020-11-13 19:57:55 +00:00
CallMode,
CallViewMode,
2020-11-13 19:57:55 +00:00
CallState,
GroupCallConnectionState,
GroupCallJoinState,
2020-11-13 19:57:55 +00:00
} from '../types/Calling';
import { AvatarColors } from '../types/Colors';
import type { ConversationType } from '../state/ducks/conversations';
import { CallingToastManager } from './CallingToastManager';
2020-11-13 19:57:55 +00:00
import { DirectCallRemoteParticipant } from './DirectCallRemoteParticipant';
import { GroupCallRemoteParticipants } from './GroupCallRemoteParticipants';
import type { LocalizerType } from '../types/Util';
2021-05-28 16:15:17 +00:00
import { NeedsScreenRecordingPermissionsModal } from './NeedsScreenRecordingPermissionsModal';
import { missingCaseError } from '../util/missingCaseError';
2021-09-29 21:20:52 +00:00
import * as KeyboardLayout from '../services/keyboardLayout';
import { useActivateSpeakerViewOnPresenting } from '../hooks/useActivateSpeakerViewOnPresenting';
import { CallingAudioIndicator } from './CallingAudioIndicator';
2022-05-10 18:14:08 +00:00
import {
useActiveCallShortcuts,
useKeyboardShortcuts,
} from '../hooks/useKeyboardShortcuts';
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;
getPresentingSources: () => void;
groupMembers?: Array<Pick<ConversationType, 'id' | 'firstName' | 'title'>>;
hangUpActiveCall: () => void;
2020-06-04 18:16:19 +00:00
i18n: LocalizerType;
joinedAt?: number;
me: ConversationType;
openSystemPreferencesAction: () => unknown;
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;
setPresenting: (_?: PresentedSource) => void;
2020-08-27 00:03:42 +00:00
setRendererCanvas: (_: SetRendererCanvasType) => void;
2020-11-17 15:07:53 +00:00
stickyControls: boolean;
switchToPresentationView: () => void;
switchFromPresentationView: () => void;
2020-11-17 15:07:53 +00:00
toggleParticipants: () => void;
2020-10-01 00:43:05 +00:00
togglePip: () => void;
toggleScreenRecordingPermissionsDialog: () => unknown;
2020-08-27 00:03:42 +00:00
toggleSettings: () => void;
2021-01-08 22:57:54 +00:00
toggleSpeakerView: () => void;
2020-06-04 18:16:19 +00:00
};
2021-09-18 00:20:29 +00:00
type DirectCallHeaderMessagePropsType = {
i18n: LocalizerType;
callState: CallState;
joinedAt?: number;
};
export const isInSpeakerView = (
call: Pick<ActiveCallStateType, 'viewMode'> | undefined
): boolean => {
return Boolean(
call?.viewMode === CallViewMode.Presentation ||
call?.viewMode === CallViewMode.Speaker
);
};
2021-09-18 00:20:29 +00:00
function DirectCallHeaderMessage({
callState,
i18n,
joinedAt,
}: DirectCallHeaderMessagePropsType): JSX.Element | null {
const [acceptedDuration, setAcceptedDuration] = useState<
number | undefined
>();
useEffect(() => {
if (!joinedAt) {
return noop;
}
// It's really jumpy with a value of 500ms.
const interval = setInterval(() => {
setAcceptedDuration(Date.now() - joinedAt);
}, 100);
return clearInterval.bind(null, interval);
}, [joinedAt]);
if (callState === CallState.Reconnecting) {
return <>{i18n('callReconnecting')}</>;
}
if (callState === CallState.Accepted && acceptedDuration) {
return <>{i18n('callDuration', [renderDuration(acceptedDuration)])}</>;
}
return null;
}
export const CallScreen: React.FC<PropsType> = ({
activeCall,
2020-11-13 19:57:55 +00:00
getGroupCallVideoFrameSource,
getPresentingSources,
groupMembers,
hangUpActiveCall,
i18n,
joinedAt,
me,
openSystemPreferencesAction,
setGroupCallVideoRequest,
setLocalAudio,
setLocalVideo,
setLocalPreview,
setPresenting,
setRendererCanvas,
2020-11-17 15:07:53 +00:00
stickyControls,
switchToPresentationView,
switchFromPresentationView,
2020-11-17 15:07:53 +00:00
toggleParticipants,
togglePip,
toggleScreenRecordingPermissionsDialog,
toggleSettings,
2021-01-08 22:57:54 +00:00
toggleSpeakerView,
}) => {
2020-12-02 18:14:03 +00:00
const {
conversation,
hasLocalAudio,
hasLocalVideo,
2022-05-19 03:28:51 +00:00
localAudioLevel,
presentingSource,
remoteParticipants,
showNeedsScreenRecordingPermissionsWarning,
2020-12-02 18:14:03 +00:00
showParticipantsList,
} = activeCall;
useActivateSpeakerViewOnPresenting({
remoteParticipants,
switchToPresentationView,
switchFromPresentationView,
});
2022-05-10 18:14:08 +00:00
const activeCallShortcuts = useActiveCallShortcuts(hangUpActiveCall);
useKeyboardShortcuts(activeCallShortcuts);
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 togglePresenting = useCallback(() => {
if (presentingSource) {
setPresenting();
} else {
getPresentingSources();
}
}, [getPresentingSources, presentingSource, setPresenting]);
2021-08-24 18:38:03 +00:00
const [controlsHover, setControlsHover] = useState(false);
const onControlsMouseEnter = useCallback(() => {
setControlsHover(true);
}, [setControlsHover]);
const onControlsMouseLeave = useCallback(() => {
setControlsHover(false);
}, [setControlsHover]);
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(() => {
2021-08-24 18:38:03 +00:00
if (!showControls || stickyControls || controlsHover) {
return noop;
2020-06-04 18:16:19 +00:00
}
const timer = setTimeout(() => {
setShowControls(false);
2020-06-04 18:16:19 +00:00
}, 5000);
2021-09-18 00:20:29 +00:00
return clearTimeout.bind(null, timer);
2021-08-24 18:38:03 +00:00
}, [showControls, stickyControls, controlsHover]);
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent): void => {
let eventHandled = false;
2021-09-29 21:20:52 +00:00
const key = KeyboardLayout.lookup(event);
if (event.shiftKey && (key === 'V' || key === 'v')) {
toggleVideo();
eventHandled = true;
2021-09-29 21:20:52 +00:00
} else if (event.shiftKey && (key === 'M' || 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
const currentPresenter = remoteParticipants.find(
participant => participant.presenting
);
const hasRemoteVideo = remoteParticipants.some(
2020-12-02 18:14:03 +00:00
remoteParticipant => remoteParticipant.hasRemoteVideo
);
2021-09-10 17:24:05 +00:00
const isSendingVideo = hasLocalVideo || presentingSource;
let isRinging: boolean;
let hasCallStarted: boolean;
2021-09-18 00:20:29 +00:00
let headerMessage: ReactNode | undefined;
2020-11-23 21:37:39 +00:00
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;
let remoteParticipantsElement: ReactNode;
2020-06-04 18:16:19 +00:00
2020-12-02 18:14:03 +00:00
switch (activeCall.callMode) {
case CallMode.Direct: {
isRinging =
activeCall.callState === CallState.Prering ||
activeCall.callState === CallState.Ringing;
hasCallStarted = !isRinging;
2021-09-18 00:20:29 +00:00
headerMessage = (
<DirectCallHeaderMessage
i18n={i18n}
callState={activeCall.callState || CallState.Prering}
joinedAt={joinedAt}
/>
2020-11-23 21:37:39 +00:00
);
headerTitle = isRinging ? undefined : 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;
remoteParticipantsElement = hasCallStarted ? (
2020-11-13 19:57:55 +00:00
<DirectCallRemoteParticipant
conversation={conversation}
hasRemoteVideo={hasRemoteVideo}
i18n={i18n}
setRendererCanvas={setRendererCanvas}
/>
) : (
<div className="module-ongoing-call__direct-call-ringing-spacer" />
2020-11-13 19:57:55 +00:00
);
break;
}
2020-11-13 19:57:55 +00:00
case CallMode.Group:
isRinging =
activeCall.outgoingRing && !activeCall.remoteParticipants.length;
hasCallStarted = activeCall.joinState !== GroupCallJoinState.NotJoined;
2020-12-02 18:14:03 +00:00
participantCount = activeCall.remoteParticipants.length + 1;
if (isRinging) {
headerTitle = undefined;
} else if (currentPresenter) {
headerTitle = i18n('calling__presenting--person-ongoing', [
currentPresenter.title,
]);
} else if (!activeCall.remoteParticipants.length) {
headerTitle = 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}
isInSpeakerView={isInSpeakerView(activeCall)}
2020-12-02 18:14:03 +00:00
remoteParticipants={activeCall.remoteParticipants}
setGroupCallVideoRequest={setGroupCallVideoRequest}
2022-05-19 03:28:51 +00:00
remoteAudioLevels={activeCall.remoteAudioLevels}
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
}
2021-09-10 17:24:05 +00:00
let lonelyInGroupNode: ReactNode;
let localPreviewNode: ReactNode;
if (
activeCall.callMode === CallMode.Group &&
2021-09-10 17:24:05 +00:00
!activeCall.remoteParticipants.length
) {
lonelyInGroupNode = (
<div
className={classNames(
'module-ongoing-call__local-preview-fullsize',
presentingSource &&
'module-ongoing-call__local-preview-fullsize--presenting'
)}
>
{isSendingVideo ? (
<video ref={localVideoRef} autoPlay />
) : (
<CallBackgroundBlur avatarPath={me.avatarPath} color={me.color}>
<Avatar
acceptedMessageRequest
avatarPath={me.avatarPath}
badge={undefined}
2021-09-10 17:24:05 +00:00
color={me.color || AvatarColors[0]}
noteToSelf={false}
conversationType="direct"
i18n={i18n}
isMe
name={me.name}
phoneNumber={me.phoneNumber}
profileName={me.profileName}
title={me.title}
// `sharedGroupNames` makes no sense for yourself, but `<Avatar>` needs it
// to determine blurring.
sharedGroupNames={[]}
size={80}
/>
<div className="module-calling__camera-is-off">
{i18n('calling__your-video-is-off')}
</div>
</CallBackgroundBlur>
)}
</div>
);
} else {
localPreviewNode = isSendingVideo ? (
<video
className={classNames(
'module-ongoing-call__footer__local-preview__video',
presentingSource &&
'module-ongoing-call__footer__local-preview__video--presenting'
)}
ref={localVideoRef}
autoPlay
/>
) : (
<CallBackgroundBlur avatarPath={me.avatarPath} color={me.color}>
<Avatar
acceptedMessageRequest
avatarPath={me.avatarPath}
badge={undefined}
2021-09-10 17:24:05 +00:00
color={me.color || AvatarColors[0]}
noteToSelf={false}
conversationType="direct"
i18n={i18n}
isMe
name={me.name}
phoneNumber={me.phoneNumber}
profileName={me.profileName}
title={me.title}
// See comment above about `sharedGroupNames`.
sharedGroupNames={[]}
size={80}
/>
</CallBackgroundBlur>
);
}
let videoButtonType: CallingButtonType;
if (presentingSource) {
videoButtonType = CallingButtonType.VIDEO_DISABLED;
} else if (hasLocalVideo) {
videoButtonType = CallingButtonType.VIDEO_ON;
} else {
videoButtonType = 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,
});
const isGroupCall = activeCall.callMode === CallMode.Group;
let presentingButtonType: CallingButtonType;
if (presentingSource) {
presentingButtonType = CallingButtonType.PRESENTING_ON;
} else if (currentPresenter) {
presentingButtonType = CallingButtonType.PRESENTING_DISABLED;
} else {
presentingButtonType = CallingButtonType.PRESENTING_OFF;
}
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
)}`,
`module-ongoing-call__container--${
hasCallStarted ? 'call-started' : 'call-not-started'
}`
2020-11-13 19:57:55 +00:00
)}
2021-09-18 00:20:29 +00:00
onFocus={() => {
setShowControls(true);
}}
onMouseMove={() => {
setShowControls(true);
}}
role="group"
>
{showNeedsScreenRecordingPermissionsWarning ? (
<NeedsScreenRecordingPermissionsModal
toggleScreenRecordingPermissionsDialog={
toggleScreenRecordingPermissionsDialog
}
i18n={i18n}
openSystemPreferencesAction={openSystemPreferencesAction}
/>
) : null}
<CallingToastManager activeCall={activeCall} i18n={i18n} />
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
i18n={i18n}
isInSpeakerView={isInSpeakerView(activeCall)}
isGroupCall={isGroupCall}
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}
2021-01-08 22:57:54 +00:00
toggleSpeakerView={toggleSpeakerView}
2020-11-17 15:07:53 +00:00
/>
2020-06-04 18:16:19 +00:00
</div>
{isRinging && (
<CallingPreCallInfo
conversation={conversation}
groupMembers={groupMembers}
i18n={i18n}
me={me}
ringMode={RingMode.IsRinging}
/>
)}
2020-11-17 15:07:53 +00:00
{remoteParticipantsElement}
2021-09-10 17:24:05 +00:00
{lonelyInGroupNode}
<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
)}
>
2021-08-19 01:04:38 +00:00
<CallingButton
buttonType={presentingButtonType}
i18n={i18n}
2021-09-18 00:20:29 +00:00
onMouseEnter={onControlsMouseEnter}
onMouseLeave={onControlsMouseLeave}
2021-08-19 01:04:38 +00:00
onClick={togglePresenting}
/>
<CallingButton
buttonType={videoButtonType}
i18n={i18n}
2021-09-18 00:20:29 +00:00
onMouseEnter={onControlsMouseEnter}
onMouseLeave={onControlsMouseLeave}
onClick={toggleVideo}
/>
<CallingButton
buttonType={audioButtonType}
i18n={i18n}
2021-09-18 00:20:29 +00:00
onMouseEnter={onControlsMouseEnter}
onMouseLeave={onControlsMouseLeave}
onClick={toggleAudio}
/>
<CallingButton
buttonType={CallingButtonType.HANG_UP}
i18n={i18n}
2021-09-18 00:20:29 +00:00
onMouseEnter={onControlsMouseEnter}
onMouseLeave={onControlsMouseLeave}
onClick={hangUpActiveCall}
/>
</div>
<div className="module-ongoing-call__footer__local-preview">
2021-09-10 17:24:05 +00:00
{localPreviewNode}
<CallingAudioIndicator
hasAudio={hasLocalAudio}
2022-05-19 03:28:51 +00:00
audioLevel={localAudioLevel}
/>
</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 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
}