2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
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-10-08 01:25:33 +00:00
|
|
|
import { CallingButton, CallingButtonType } from './CallingButton';
|
2020-11-04 19:56:03 +00:00
|
|
|
import { CallBackgroundBlur } from './CallBackgroundBlur';
|
2020-06-04 18:16:19 +00:00
|
|
|
import { CallState } from '../types/Calling';
|
2020-11-04 19:56:03 +00:00
|
|
|
import { ColorType } from '../types/Colors';
|
2020-06-04 18:16:19 +00:00
|
|
|
import { LocalizerType } from '../types/Util';
|
|
|
|
|
|
|
|
export type PropsType = {
|
2020-11-06 17:36:37 +00:00
|
|
|
conversation: {
|
|
|
|
id: string;
|
|
|
|
avatarPath?: string;
|
|
|
|
color?: ColorType;
|
|
|
|
title: string;
|
|
|
|
name?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
profileName?: string;
|
|
|
|
};
|
|
|
|
callState: CallState;
|
2020-06-04 18:16:19 +00:00
|
|
|
hangUp: (_: HangUpType) => void;
|
|
|
|
hasLocalAudio: boolean;
|
|
|
|
hasLocalVideo: boolean;
|
|
|
|
hasRemoteVideo: boolean;
|
|
|
|
i18n: LocalizerType;
|
2020-11-06 17:36:37 +00:00
|
|
|
joinedAt?: number;
|
2020-11-04 19:56:03 +00:00
|
|
|
me: {
|
|
|
|
avatarPath?: string;
|
|
|
|
color?: ColorType;
|
|
|
|
name?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
profileName?: string;
|
|
|
|
title: string;
|
|
|
|
};
|
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-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
|
|
|
};
|
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
export const CallScreen: React.FC<PropsType> = ({
|
|
|
|
callState,
|
2020-11-06 17:36:37 +00:00
|
|
|
conversation,
|
2020-10-26 22:13:42 +00:00
|
|
|
hangUp,
|
|
|
|
hasLocalAudio,
|
|
|
|
hasLocalVideo,
|
|
|
|
hasRemoteVideo,
|
|
|
|
i18n,
|
2020-11-06 17:36:37 +00:00
|
|
|
joinedAt,
|
2020-11-04 19:56:03 +00:00
|
|
|
me,
|
2020-10-26 22:13:42 +00:00
|
|
|
setLocalAudio,
|
|
|
|
setLocalVideo,
|
|
|
|
setLocalPreview,
|
|
|
|
setRendererCanvas,
|
|
|
|
togglePip,
|
|
|
|
toggleSettings,
|
|
|
|
}) => {
|
|
|
|
const toggleAudio = useCallback(() => {
|
|
|
|
setLocalAudio({
|
|
|
|
enabled: !hasLocalAudio,
|
|
|
|
});
|
2020-11-06 17:36:37 +00:00
|
|
|
}, [setLocalAudio, hasLocalAudio]);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
const toggleVideo = useCallback(() => {
|
|
|
|
setLocalVideo({
|
|
|
|
enabled: !hasLocalVideo,
|
|
|
|
});
|
2020-11-06 17:36:37 +00:00
|
|
|
}, [setLocalVideo, hasLocalVideo]);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
const [acceptedDuration, setAcceptedDuration] = useState<number | null>(null);
|
|
|
|
const [showControls, setShowControls] = useState(true);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
const localVideoRef = useRef<HTMLVideoElement | null>(null);
|
|
|
|
const remoteVideoRef = useRef<HTMLCanvasElement | null>(null);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setLocalPreview({ element: localVideoRef });
|
|
|
|
setRendererCanvas({ element: remoteVideoRef });
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
return () => {
|
|
|
|
setLocalPreview({ element: undefined });
|
|
|
|
setRendererCanvas({ element: undefined });
|
|
|
|
};
|
|
|
|
}, [setLocalPreview, setRendererCanvas]);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
useEffect(() => {
|
2020-11-06 17:36:37 +00:00
|
|
|
if (!joinedAt) {
|
2020-10-26 22:13:42 +00:00
|
|
|
return noop;
|
2020-06-04 18:16:19 +00:00
|
|
|
}
|
2020-10-26 22:13:42 +00:00
|
|
|
// It's really jumpy with a value of 500ms.
|
|
|
|
const interval = setInterval(() => {
|
2020-11-06 17:36:37 +00:00
|
|
|
setAcceptedDuration(Date.now() - joinedAt);
|
2020-10-26 22:13:42 +00:00
|
|
|
}, 100);
|
|
|
|
return clearInterval.bind(null, interval);
|
2020-11-06 17:36:37 +00:00
|
|
|
}, [joinedAt]);
|
2020-10-26 22:13:42 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2020-09-12 00:46:52 +00:00
|
|
|
if (!showControls) {
|
2020-10-26 22:13:42 +00:00
|
|
|
return noop;
|
2020-06-04 18:16:19 +00:00
|
|
|
}
|
2020-10-26 22:13:42 +00:00
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setShowControls(false);
|
2020-06-04 18:16:19 +00:00
|
|
|
}, 5000);
|
2020-10-26 22:13:42 +00:00
|
|
|
return clearInterval.bind(null, timer);
|
|
|
|
}, [showControls]);
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('keydown', handleKeyDown);
|
|
|
|
};
|
|
|
|
}, [toggleAudio, toggleVideo]);
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
const isAudioOnly = !hasLocalVideo && !hasRemoteVideo;
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
const controlsFadeClass = classNames({
|
|
|
|
'module-ongoing-call__controls--fadeIn':
|
|
|
|
(showControls || isAudioOnly) && callState !== CallState.Accepted,
|
|
|
|
'module-ongoing-call__controls--fadeOut':
|
|
|
|
!showControls && !isAudioOnly && callState === CallState.Accepted,
|
|
|
|
});
|
|
|
|
|
|
|
|
const videoButtonType = hasLocalVideo
|
|
|
|
? CallingButtonType.VIDEO_ON
|
|
|
|
: CallingButtonType.VIDEO_OFF;
|
|
|
|
const audioButtonType = hasLocalAudio
|
|
|
|
? CallingButtonType.AUDIO_ON
|
|
|
|
: CallingButtonType.AUDIO_OFF;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="module-calling__container"
|
|
|
|
onMouseMove={() => {
|
|
|
|
setShowControls(true);
|
|
|
|
}}
|
|
|
|
role="group"
|
|
|
|
>
|
2020-06-04 18:16:19 +00:00
|
|
|
<div
|
2020-10-26 22:13:42 +00:00
|
|
|
className={classNames(
|
|
|
|
'module-calling__header',
|
|
|
|
'module-ongoing-call__header',
|
|
|
|
controlsFadeClass
|
|
|
|
)}
|
2020-06-04 18:16:19 +00:00
|
|
|
>
|
2020-10-26 22:13:42 +00:00
|
|
|
<div className="module-calling__header--header-name">
|
2020-11-06 17:36:37 +00:00
|
|
|
{conversation.title}
|
2020-06-04 18:16:19 +00:00
|
|
|
</div>
|
2020-10-26 22:13:42 +00:00
|
|
|
{renderHeaderMessage(i18n, callState, acceptedDuration)}
|
|
|
|
<div className="module-calling-tools">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
aria-label={i18n('callingDeviceSelection__settings')}
|
|
|
|
className="module-calling-tools__button module-calling-button__settings"
|
|
|
|
onClick={toggleSettings}
|
2020-06-04 18:16:19 +00:00
|
|
|
/>
|
2020-10-26 22:13:42 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
aria-label={i18n('calling__pip')}
|
|
|
|
className="module-calling-tools__button module-calling-button__pip"
|
|
|
|
onClick={togglePip}
|
2020-06-04 18:16:19 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-10-26 22:13:42 +00:00
|
|
|
{hasRemoteVideo ? (
|
|
|
|
<canvas
|
|
|
|
className="module-ongoing-call__remote-video-enabled"
|
|
|
|
ref={remoteVideoRef}
|
|
|
|
/>
|
|
|
|
) : (
|
2020-11-06 17:36:37 +00:00
|
|
|
renderAvatar(i18n, conversation)
|
2020-10-26 22:13:42 +00:00
|
|
|
)}
|
2020-11-04 19:56:03 +00:00
|
|
|
<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}
|
|
|
|
tooltipDistance={24}
|
|
|
|
/>
|
|
|
|
<CallingButton
|
|
|
|
buttonType={audioButtonType}
|
|
|
|
i18n={i18n}
|
|
|
|
onClick={toggleAudio}
|
|
|
|
tooltipDistance={24}
|
|
|
|
/>
|
|
|
|
<CallingButton
|
|
|
|
buttonType={CallingButtonType.HANG_UP}
|
|
|
|
i18n={i18n}
|
|
|
|
onClick={() => {
|
2020-11-06 17:36:37 +00:00
|
|
|
hangUp({ conversationId: conversation.id });
|
2020-11-04 19:56:03 +00:00
|
|
|
}}
|
|
|
|
tooltipDistance={24}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="module-ongoing-call__footer__local-preview">
|
|
|
|
{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>
|
2020-10-26 22:13:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
function renderAvatar(
|
|
|
|
i18n: LocalizerType,
|
2020-11-06 17:36:37 +00:00
|
|
|
{
|
2020-10-26 22:13:42 +00:00
|
|
|
avatarPath,
|
|
|
|
color,
|
|
|
|
name,
|
|
|
|
phoneNumber,
|
|
|
|
profileName,
|
|
|
|
title,
|
2020-11-06 17:36:37 +00:00
|
|
|
}: {
|
|
|
|
avatarPath?: string;
|
|
|
|
color?: ColorType;
|
|
|
|
title: string;
|
|
|
|
name?: string;
|
|
|
|
phoneNumber?: string;
|
|
|
|
profileName?: string;
|
|
|
|
}
|
|
|
|
): JSX.Element {
|
2020-10-26 22:13:42 +00:00
|
|
|
return (
|
|
|
|
<div className="module-ongoing-call__remote-video-disabled">
|
|
|
|
<Avatar
|
|
|
|
avatarPath={avatarPath}
|
|
|
|
color={color || 'ultramarine'}
|
|
|
|
noteToSelf={false}
|
|
|
|
conversationType="direct"
|
|
|
|
i18n={i18n}
|
|
|
|
name={name}
|
|
|
|
phoneNumber={phoneNumber}
|
|
|
|
profileName={profileName}
|
|
|
|
title={title}
|
|
|
|
size={112}
|
2020-06-04 18:16:19 +00:00
|
|
|
/>
|
2020-10-26 22:13:42 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +00:00
|
|
|
function renderHeaderMessage(
|
|
|
|
i18n: LocalizerType,
|
|
|
|
callState: CallState,
|
|
|
|
acceptedDuration: null | number
|
|
|
|
): JSX.Element | null {
|
|
|
|
let message = null;
|
|
|
|
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-10-26 22:13:42 +00:00
|
|
|
if (!message) {
|
|
|
|
return null;
|
2020-06-04 18:16:19 +00:00
|
|
|
}
|
2020-10-26 22:13:42 +00:00
|
|
|
return <div className="module-ongoing-call__header-message">{message}</div>;
|
|
|
|
}
|
2020-06-04 18:16:19 +00:00
|
|
|
|
2020-10-26 22:13:42 +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
|
|
|
}
|
2020-10-26 22:13:42 +00:00
|
|
|
return `${mins}:${secs}`;
|
2020-06-04 18:16:19 +00:00
|
|
|
}
|