2022-02-08 18:30:33 +00:00
|
|
|
// Copyright 2020-2022 Signal Messenger, LLC
|
2020-11-13 19:57:55 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { CSSProperties } from 'react';
|
2020-11-17 19:49:48 +00:00
|
|
|
import React, {
|
|
|
|
useState,
|
|
|
|
useRef,
|
|
|
|
useMemo,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
} from 'react';
|
2020-11-16 19:57:58 +00:00
|
|
|
import classNames from 'classnames';
|
2020-11-13 19:57:55 +00:00
|
|
|
import { noop } from 'lodash';
|
2021-09-28 16:37:03 +00:00
|
|
|
import type { VideoFrameSource } from 'ringrtc';
|
|
|
|
import type { GroupCallRemoteParticipantType } from '../types/Calling';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { AvatarColors } from '../types/Colors';
|
2020-11-13 19:57:55 +00:00
|
|
|
import { CallBackgroundBlur } from './CallBackgroundBlur';
|
2022-02-08 18:30:33 +00:00
|
|
|
import { CallingAudioIndicator } from './CallingAudioIndicator';
|
2020-11-19 18:13:36 +00:00
|
|
|
import { Avatar, AvatarSize } from './Avatar';
|
2021-04-27 19:29:59 +00:00
|
|
|
import { ConfirmationDialog } from './ConfirmationDialog';
|
2020-12-02 01:30:25 +00:00
|
|
|
import { Intl } from './Intl';
|
2020-11-19 23:31:04 +00:00
|
|
|
import { ContactName } from './conversation/ContactName';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useIntersectionObserver } from '../hooks/useIntersectionObserver';
|
2022-05-23 17:16:13 +00:00
|
|
|
import { MAX_FRAME_HEIGHT, MAX_FRAME_WIDTH } from '../calling/constants';
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2021-06-25 17:23:15 +00:00
|
|
|
const MAX_TIME_TO_SHOW_STALE_VIDEO_FRAMES = 5000;
|
2022-01-20 18:43:58 +00:00
|
|
|
const MAX_TIME_TO_SHOW_STALE_SCREENSHARE_FRAMES = 60000;
|
2021-06-25 17:23:15 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type BasePropsType = {
|
2021-12-11 00:21:28 +00:00
|
|
|
getFrameBuffer: () => Buffer;
|
2020-11-13 19:57:55 +00:00
|
|
|
getGroupCallVideoFrameSource: (demuxId: number) => VideoFrameSource;
|
2020-11-19 18:13:36 +00:00
|
|
|
i18n: LocalizerType;
|
2021-12-06 23:06:13 +00:00
|
|
|
onVisibilityChanged?: (demuxId: number, isVisible: boolean) => unknown;
|
2020-11-19 18:13:36 +00:00
|
|
|
remoteParticipant: GroupCallRemoteParticipantType;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type InPipPropsType = {
|
2020-11-17 19:49:48 +00:00
|
|
|
isInPip: true;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type InOverflowAreaPropsType = {
|
2020-11-17 19:49:48 +00:00
|
|
|
height: number;
|
2020-11-19 18:13:36 +00:00
|
|
|
isInPip?: false;
|
2022-05-19 03:28:51 +00:00
|
|
|
audioLevel: number;
|
2021-01-08 18:58:28 +00:00
|
|
|
width: number;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2021-01-08 18:58:28 +00:00
|
|
|
|
2021-01-14 18:07:05 +00:00
|
|
|
type InGridPropsType = InOverflowAreaPropsType & {
|
2020-11-13 19:57:55 +00:00
|
|
|
left: number;
|
|
|
|
top: number;
|
2021-01-14 18:07:05 +00:00
|
|
|
};
|
2020-11-13 19:57:55 +00:00
|
|
|
|
2021-01-08 18:58:28 +00:00
|
|
|
export type PropsType = BasePropsType &
|
|
|
|
(InPipPropsType | InOverflowAreaPropsType | InGridPropsType);
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
export const GroupCallRemoteParticipant: React.FC<PropsType> = React.memo(
|
|
|
|
props => {
|
2021-12-06 23:06:13 +00:00
|
|
|
const {
|
|
|
|
getFrameBuffer,
|
|
|
|
getGroupCallVideoFrameSource,
|
|
|
|
i18n,
|
|
|
|
onVisibilityChanged,
|
|
|
|
} = props;
|
2020-11-19 18:13:36 +00:00
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
const {
|
2021-05-07 22:21:10 +00:00
|
|
|
acceptedMessageRequest,
|
2020-11-19 18:13:36 +00:00
|
|
|
avatarPath,
|
|
|
|
color,
|
2020-11-18 19:07:25 +00:00
|
|
|
demuxId,
|
|
|
|
hasRemoteAudio,
|
|
|
|
hasRemoteVideo,
|
2020-12-02 01:30:25 +00:00
|
|
|
isBlocked,
|
2021-05-07 22:21:10 +00:00
|
|
|
isMe,
|
2020-12-02 01:30:25 +00:00
|
|
|
profileName,
|
2021-05-07 22:21:10 +00:00
|
|
|
sharedGroupNames,
|
2022-01-20 18:43:58 +00:00
|
|
|
sharingScreen,
|
2020-12-02 01:30:25 +00:00
|
|
|
title,
|
2021-01-08 18:58:28 +00:00
|
|
|
videoAspectRatio,
|
2020-11-19 18:13:36 +00:00
|
|
|
} = props.remoteParticipant;
|
2020-11-18 19:07:25 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const [hasReceivedVideoRecently, setHasReceivedVideoRecently] =
|
|
|
|
useState(false);
|
2021-01-08 18:58:28 +00:00
|
|
|
const [isWide, setIsWide] = useState<boolean>(
|
|
|
|
videoAspectRatio ? videoAspectRatio >= 1 : true
|
|
|
|
);
|
2020-12-02 01:30:25 +00:00
|
|
|
const [showBlockInfo, setShowBlockInfo] = useState(false);
|
2020-11-18 19:07:25 +00:00
|
|
|
|
2021-06-25 17:23:15 +00:00
|
|
|
// We have some state (`hasReceivedVideoRecently`) and this ref. We can't have a
|
|
|
|
// single state value like `lastReceivedVideoAt` because (1) it won't automatically
|
|
|
|
// trigger a re-render after the video has become stale (2) it would cause a full
|
|
|
|
// re-render of the component for every frame, which is way too often.
|
|
|
|
//
|
|
|
|
// Alternatively, we could create a timeout that's reset every time we get a video
|
|
|
|
// frame (perhaps using a debounce function), but that becomes harder to clean up
|
|
|
|
// when the component unmounts.
|
|
|
|
const lastReceivedVideoAt = useRef(-Infinity);
|
2020-11-18 19:07:25 +00:00
|
|
|
const remoteVideoRef = useRef<HTMLCanvasElement | null>(null);
|
|
|
|
const canvasContextRef = useRef<CanvasRenderingContext2D | null>(null);
|
2021-12-11 00:21:28 +00:00
|
|
|
const imageDataRef = useRef<ImageData | null>(null);
|
2020-11-13 19:57:55 +00:00
|
|
|
|
2021-11-11 22:43:05 +00:00
|
|
|
const [intersectionRef, intersectionObserverEntry] =
|
|
|
|
useIntersectionObserver();
|
2021-01-08 18:58:28 +00:00
|
|
|
const isVisible = intersectionObserverEntry
|
|
|
|
? intersectionObserverEntry.isIntersecting
|
|
|
|
: true;
|
|
|
|
|
2021-12-06 23:06:13 +00:00
|
|
|
useEffect(() => {
|
|
|
|
onVisibilityChanged?.(demuxId, isVisible);
|
|
|
|
}, [demuxId, isVisible, onVisibilityChanged]);
|
|
|
|
|
2021-06-25 17:23:15 +00:00
|
|
|
const wantsToShowVideo = hasRemoteVideo && !isBlocked && isVisible;
|
|
|
|
const hasVideoToShow = wantsToShowVideo && hasReceivedVideoRecently;
|
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
const videoFrameSource = useMemo(
|
|
|
|
() => getGroupCallVideoFrameSource(demuxId),
|
|
|
|
[getGroupCallVideoFrameSource, demuxId]
|
2020-11-17 19:49:48 +00:00
|
|
|
);
|
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
const renderVideoFrame = useCallback(() => {
|
2022-01-20 18:43:58 +00:00
|
|
|
const frameAge = Date.now() - lastReceivedVideoAt.current;
|
|
|
|
const maxFrameAge = sharingScreen
|
|
|
|
? MAX_TIME_TO_SHOW_STALE_SCREENSHARE_FRAMES
|
|
|
|
: MAX_TIME_TO_SHOW_STALE_VIDEO_FRAMES;
|
|
|
|
if (frameAge > maxFrameAge) {
|
2021-06-25 17:23:15 +00:00
|
|
|
setHasReceivedVideoRecently(false);
|
|
|
|
}
|
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
const canvasEl = remoteVideoRef.current;
|
|
|
|
if (!canvasEl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const canvasContext = canvasContextRef.current;
|
|
|
|
if (!canvasContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-08 17:32:49 +00:00
|
|
|
// This frame buffer is shared by all participants, so it may contain pixel data
|
|
|
|
// for other participants, or pixel data from a previous frame. That's why we
|
|
|
|
// return early and use the `frameWidth` and `frameHeight`.
|
|
|
|
const frameBuffer = getFrameBuffer();
|
2021-12-11 00:21:28 +00:00
|
|
|
const frameDimensions = videoFrameSource.receiveVideoFrame(frameBuffer);
|
2020-11-18 19:07:25 +00:00
|
|
|
if (!frameDimensions) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
const [frameWidth, frameHeight] = frameDimensions;
|
2021-01-08 17:32:49 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
frameWidth < 2 ||
|
|
|
|
frameHeight < 2 ||
|
2022-05-23 17:16:13 +00:00
|
|
|
frameWidth > MAX_FRAME_WIDTH ||
|
|
|
|
frameHeight > MAX_FRAME_HEIGHT
|
2021-01-08 17:32:49 +00:00
|
|
|
) {
|
2020-11-18 19:07:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
canvasEl.width = frameWidth;
|
|
|
|
canvasEl.height = frameHeight;
|
|
|
|
|
2021-12-11 00:21:28 +00:00
|
|
|
let imageData = imageDataRef.current;
|
|
|
|
if (
|
|
|
|
imageData?.width !== frameWidth ||
|
|
|
|
imageData?.height !== frameHeight
|
|
|
|
) {
|
|
|
|
imageData = new ImageData(frameWidth, frameHeight);
|
|
|
|
imageDataRef.current = imageData;
|
|
|
|
}
|
|
|
|
imageData.data.set(frameBuffer.subarray(0, frameWidth * frameHeight * 4));
|
|
|
|
canvasContext.putImageData(imageData, 0, 0);
|
2020-11-18 19:07:25 +00:00
|
|
|
|
2021-06-25 17:23:15 +00:00
|
|
|
lastReceivedVideoAt.current = Date.now();
|
|
|
|
|
|
|
|
setHasReceivedVideoRecently(true);
|
2020-11-18 19:07:25 +00:00
|
|
|
setIsWide(frameWidth > frameHeight);
|
2022-01-20 18:43:58 +00:00
|
|
|
}, [getFrameBuffer, videoFrameSource, sharingScreen]);
|
2020-11-18 19:07:25 +00:00
|
|
|
|
2021-06-25 17:23:15 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!hasRemoteVideo) {
|
|
|
|
setHasReceivedVideoRecently(false);
|
|
|
|
}
|
|
|
|
}, [hasRemoteVideo]);
|
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
useEffect(() => {
|
2021-01-08 18:58:28 +00:00
|
|
|
if (!hasRemoteVideo || !isVisible) {
|
2020-11-18 19:07:25 +00:00
|
|
|
return noop;
|
|
|
|
}
|
|
|
|
|
|
|
|
let rafId = requestAnimationFrame(tick);
|
|
|
|
|
|
|
|
function tick() {
|
|
|
|
renderVideoFrame();
|
|
|
|
rafId = requestAnimationFrame(tick);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
cancelAnimationFrame(rafId);
|
|
|
|
};
|
2021-01-08 18:58:28 +00:00
|
|
|
}, [hasRemoteVideo, isVisible, renderVideoFrame, videoFrameSource]);
|
2020-11-18 19:07:25 +00:00
|
|
|
|
|
|
|
let canvasStyles: CSSProperties;
|
|
|
|
let containerStyles: CSSProperties;
|
2020-11-17 19:49:48 +00:00
|
|
|
|
|
|
|
// If our `width` and `height` props don't match the canvas's aspect ratio, we want to
|
|
|
|
// fill the container. This can happen when RingRTC gives us an inaccurate
|
|
|
|
// `videoAspectRatio`, or if the container is an unexpected size.
|
2020-11-18 19:07:25 +00:00
|
|
|
if (isWide) {
|
|
|
|
canvasStyles = { width: '100%' };
|
2020-11-13 19:57:55 +00:00
|
|
|
} else {
|
2020-11-18 19:07:25 +00:00
|
|
|
canvasStyles = { height: '100%' };
|
2020-11-17 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:13:36 +00:00
|
|
|
let avatarSize: number;
|
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
if (props.isInPip) {
|
|
|
|
containerStyles = canvasStyles;
|
2020-11-19 18:13:36 +00:00
|
|
|
avatarSize = AvatarSize.FIFTY_TWO;
|
2020-11-18 19:07:25 +00:00
|
|
|
} else {
|
2021-01-08 18:58:28 +00:00
|
|
|
const { width, height } = props;
|
2020-11-19 18:13:36 +00:00
|
|
|
const shorterDimension = Math.min(width, height);
|
|
|
|
|
|
|
|
if (shorterDimension >= 240) {
|
|
|
|
avatarSize = AvatarSize.ONE_HUNDRED_TWELVE;
|
|
|
|
} else if (shorterDimension >= 180) {
|
|
|
|
avatarSize = AvatarSize.EIGHTY;
|
|
|
|
} else {
|
|
|
|
avatarSize = AvatarSize.FIFTY_TWO;
|
|
|
|
}
|
2020-11-18 19:07:25 +00:00
|
|
|
|
|
|
|
containerStyles = {
|
|
|
|
height,
|
|
|
|
width,
|
|
|
|
};
|
2021-01-08 18:58:28 +00:00
|
|
|
|
|
|
|
if ('top' in props) {
|
|
|
|
containerStyles.position = 'absolute';
|
|
|
|
containerStyles.top = props.top;
|
|
|
|
containerStyles.left = props.left;
|
|
|
|
}
|
2020-11-13 19:57:55 +00:00
|
|
|
}
|
2020-11-17 19:49:48 +00:00
|
|
|
|
2020-11-18 19:07:25 +00:00
|
|
|
return (
|
2021-01-06 17:22:48 +00:00
|
|
|
<>
|
|
|
|
{showBlockInfo && (
|
2021-04-27 19:29:59 +00:00
|
|
|
<ConfirmationDialog
|
2022-09-27 20:24:21 +00:00
|
|
|
dialogName="GroupCallRemoteParticipant.blockInfo"
|
2021-04-27 19:29:59 +00:00
|
|
|
cancelText={i18n('ok')}
|
2021-01-06 17:22:48 +00:00
|
|
|
i18n={i18n}
|
|
|
|
onClose={() => {
|
|
|
|
setShowBlockInfo(false);
|
|
|
|
}}
|
|
|
|
title={
|
|
|
|
<div className="module-ongoing-call__group-call-remote-participant__blocked--modal-title">
|
|
|
|
<Intl
|
|
|
|
i18n={i18n}
|
|
|
|
id="calling__you-have-blocked"
|
2021-09-16 16:15:43 +00:00
|
|
|
components={[<ContactName key="name" title={title} />]}
|
2021-01-06 17:22:48 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
}
|
2020-11-19 23:31:04 +00:00
|
|
|
>
|
2021-01-06 17:22:48 +00:00
|
|
|
{i18n('calling__block-info')}
|
2021-04-27 19:29:59 +00:00
|
|
|
</ConfirmationDialog>
|
2020-11-19 23:31:04 +00:00
|
|
|
)}
|
2021-01-06 17:22:48 +00:00
|
|
|
|
|
|
|
<div
|
|
|
|
className="module-ongoing-call__group-call-remote-participant"
|
2021-01-08 18:58:28 +00:00
|
|
|
ref={intersectionRef}
|
2021-01-06 17:22:48 +00:00
|
|
|
style={containerStyles}
|
|
|
|
>
|
2021-12-08 15:22:37 +00:00
|
|
|
{!props.isInPip && (
|
2021-01-06 17:22:48 +00:00
|
|
|
<div
|
|
|
|
className={classNames(
|
2022-05-09 23:51:57 +00:00
|
|
|
'module-ongoing-call__group-call-remote-participant__info'
|
2021-01-06 17:22:48 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<ContactName
|
2021-12-08 15:22:37 +00:00
|
|
|
module="module-ongoing-call__group-call-remote-participant__info__contact-name"
|
2020-12-02 01:30:25 +00:00
|
|
|
title={title}
|
|
|
|
/>
|
2022-02-08 18:30:33 +00:00
|
|
|
<CallingAudioIndicator
|
2022-02-25 15:24:05 +00:00
|
|
|
hasAudio={hasRemoteAudio}
|
2022-05-19 03:28:51 +00:00
|
|
|
audioLevel={props.audioLevel}
|
2022-02-08 18:30:33 +00:00
|
|
|
/>
|
2021-01-06 17:22:48 +00:00
|
|
|
</div>
|
|
|
|
)}
|
2021-06-25 17:23:15 +00:00
|
|
|
{wantsToShowVideo && (
|
2021-01-06 17:22:48 +00:00
|
|
|
<canvas
|
|
|
|
className="module-ongoing-call__group-call-remote-participant__remote-video"
|
2021-06-25 17:23:15 +00:00
|
|
|
style={{
|
|
|
|
...canvasStyles,
|
|
|
|
// If we want to show video but don't have any yet, we still render the
|
|
|
|
// canvas invisibly. This lets us render frame data immediately without
|
|
|
|
// having to juggle anything.
|
|
|
|
...(hasVideoToShow ? {} : { display: 'none' }),
|
|
|
|
}}
|
2021-01-06 17:22:48 +00:00
|
|
|
ref={canvasEl => {
|
|
|
|
remoteVideoRef.current = canvasEl;
|
|
|
|
if (canvasEl) {
|
|
|
|
canvasContextRef.current = canvasEl.getContext('2d', {
|
|
|
|
alpha: false,
|
|
|
|
desynchronized: true,
|
|
|
|
storage: 'discardable',
|
|
|
|
} as CanvasRenderingContext2DSettings);
|
|
|
|
} else {
|
|
|
|
canvasContextRef.current = null;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2021-06-25 17:23:15 +00:00
|
|
|
)}
|
|
|
|
{!hasVideoToShow && (
|
2021-01-06 17:22:48 +00:00
|
|
|
<CallBackgroundBlur avatarPath={avatarPath} color={color}>
|
|
|
|
{isBlocked ? (
|
|
|
|
<>
|
|
|
|
<i className="module-ongoing-call__group-call-remote-participant__blocked" />
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="module-ongoing-call__group-call-remote-participant__blocked--info"
|
|
|
|
onClick={() => {
|
|
|
|
setShowBlockInfo(true);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{i18n('moreInfo')}
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<Avatar
|
2021-05-07 22:21:10 +00:00
|
|
|
acceptedMessageRequest={acceptedMessageRequest}
|
2021-01-06 17:22:48 +00:00
|
|
|
avatarPath={avatarPath}
|
2021-12-01 17:24:00 +00:00
|
|
|
badge={undefined}
|
2021-08-06 00:17:05 +00:00
|
|
|
color={color || AvatarColors[0]}
|
2021-01-06 17:22:48 +00:00
|
|
|
noteToSelf={false}
|
|
|
|
conversationType="direct"
|
|
|
|
i18n={i18n}
|
2021-05-07 22:21:10 +00:00
|
|
|
isMe={isMe}
|
2021-01-06 17:22:48 +00:00
|
|
|
profileName={profileName}
|
|
|
|
title={title}
|
2021-05-07 22:21:10 +00:00
|
|
|
sharedGroupNames={sharedGroupNames}
|
2021-01-06 17:22:48 +00:00
|
|
|
size={avatarSize}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</CallBackgroundBlur>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
2020-11-18 19:07:25 +00:00
|
|
|
);
|
2020-11-13 19:57:55 +00:00
|
|
|
}
|
2020-11-18 19:07:25 +00:00
|
|
|
);
|