Files
element-call/src/VideoGrid.jsx

756 lines
18 KiB
React
Raw Normal View History

import React, { useCallback, useEffect, useRef, useState } from "react";
2021-08-17 16:57:25 -07:00
import { useDrag } from "react-use-gesture";
import { useSprings, animated } from "@react-spring/web";
2021-08-19 12:11:12 -07:00
import classNames from "classnames";
import styles from "./VideoGrid.module.css";
2021-08-17 16:57:25 -07:00
import useMeasure from "react-use-measure";
import moveArrItem from "lodash-move";
2021-08-19 12:11:12 -07:00
import { ReactComponent as MicIcon } from "./icons/Mic.svg";
2021-08-23 15:44:08 -07:00
import { ReactComponent as MuteMicIcon } from "./icons/MuteMic.svg";
import { ReactComponent as DisableVideoIcon } from "./icons/DisableVideo.svg";
2021-08-17 16:57:25 -07:00
2021-08-17 17:25:01 -07:00
function useIsMounted() {
const isMountedRef = useRef(false);
useEffect(() => {
isMountedRef.current = true;
return () => {
isMountedRef.current = false;
};
}, []);
return isMountedRef;
}
2021-08-17 16:57:25 -07:00
function isInside([x, y], targetTile) {
const left = targetTile.x;
const top = targetTile.y;
const bottom = targetTile.y + targetTile.height;
const right = targetTile.x + targetTile.width;
if (x < left || x > right || y < top || y > bottom) {
return false;
}
return true;
}
2021-08-31 11:36:59 -07:00
function getTilePositions(
tileCount,
presenterTileCount,
gridWidth,
gridHeight
) {
if (tileCount === 0) {
return [];
}
2021-08-17 16:57:25 -07:00
2021-08-17 17:32:05 -07:00
if (tileCount > 12) {
console.warn("Over 12 tiles is not currently supported");
}
2021-08-31 11:36:59 -07:00
const gap = 8;
2021-08-31 11:36:59 -07:00
const { layoutDirection, participantGridRatio } = getGridLayout(
tileCount,
presenterTileCount,
gridWidth,
gridHeight
);
2021-08-31 11:36:59 -07:00
let participantGridWidth, participantGridHeight;
2021-08-31 11:36:59 -07:00
if (layoutDirection === "vertical") {
participantGridWidth = gridWidth;
participantGridHeight = Math.round(gridHeight * participantGridRatio);
} else {
participantGridWidth = Math.round(gridWidth * participantGridRatio);
participantGridHeight = gridHeight;
}
2021-08-31 15:39:31 -07:00
const participantTileCount = tileCount - presenterTileCount;
2021-08-31 11:36:59 -07:00
const participantGridPositions = getSubGridPositions(
2021-08-31 15:39:31 -07:00
participantTileCount,
2021-08-31 11:36:59 -07:00
participantGridWidth,
participantGridHeight,
gap
);
const participantGridBounds = getSubGridBoundingBox(participantGridPositions);
2021-08-31 11:36:59 -07:00
let presenterGridWidth, presenterGridHeight;
2021-08-31 14:07:47 -07:00
if (presenterTileCount === 0) {
presenterGridWidth = 0;
presenterGridHeight = 0;
} else if (layoutDirection === "vertical") {
2021-08-31 11:36:59 -07:00
presenterGridWidth = gridWidth;
2021-08-31 15:39:31 -07:00
presenterGridHeight =
gridHeight -
(participantGridBounds.height + (participantTileCount ? gap * 2 : 0));
2021-08-31 11:36:59 -07:00
} else {
2021-08-31 15:39:31 -07:00
presenterGridWidth =
gridWidth -
(participantGridBounds.width + (participantTileCount ? gap * 2 : 0));
2021-08-31 11:36:59 -07:00
presenterGridHeight = gridHeight;
}
2021-08-31 11:36:59 -07:00
const presenterGridPositions = getSubGridPositions(
presenterTileCount,
presenterGridWidth,
presenterGridHeight,
gap
);
2021-08-31 13:54:18 -07:00
const tilePositions = [
...presenterGridPositions,
...participantGridPositions,
];
centerTiles(
presenterGridPositions,
presenterGridWidth,
presenterGridHeight,
0,
0
);
2021-08-31 11:36:59 -07:00
if (layoutDirection === "vertical") {
2021-08-31 13:54:18 -07:00
centerTiles(
2021-08-31 11:36:59 -07:00
participantGridPositions,
2021-08-31 13:54:18 -07:00
gridWidth,
gridHeight - presenterGridHeight,
2021-08-31 11:36:59 -07:00
0,
2021-08-31 13:54:18 -07:00
presenterGridHeight
);
} else {
2021-08-31 13:54:18 -07:00
centerTiles(
2021-08-31 11:36:59 -07:00
participantGridPositions,
2021-08-31 13:54:18 -07:00
gridWidth - presenterGridWidth,
gridHeight,
presenterGridWidth,
2021-08-31 11:36:59 -07:00
0
);
}
return tilePositions;
}
function getSubGridBoundingBox(positions) {
let left = 0,
right = 0,
top = 0,
bottom = 0;
for (let i = 0; i < positions.length; i++) {
const { x, y, width, height } = positions[i];
if (i === 0) {
left = x;
right = x + width;
top = y;
bottom = y + height;
} else {
if (x < left) {
left = x;
}
2021-08-31 11:36:59 -07:00
if (y < top) {
top = y;
}
2021-08-31 11:36:59 -07:00
if (x + width > right) {
right = x + width;
2021-08-17 16:57:25 -07:00
}
2021-08-31 11:36:59 -07:00
if (y + height > bottom) {
bottom = y + height;
}
2021-08-17 16:57:25 -07:00
}
2021-08-31 11:36:59 -07:00
}
2021-08-17 16:57:25 -07:00
2021-08-31 11:36:59 -07:00
return {
left,
right,
top,
bottom,
width: right - left,
height: bottom - top,
};
}
function getGridLayout(tileCount, presenterTileCount, gridWidth, gridHeight) {
let layoutDirection = "horizontal";
let participantGridRatio = 1;
if (presenterTileCount === 0) {
return { participantGridRatio, layoutDirection };
}
2021-08-31 11:36:59 -07:00
const gridAspectRatio = gridWidth / gridHeight;
if (gridAspectRatio < 1) {
layoutDirection = "vertical";
participantGridRatio = 1 / 3;
} else {
layoutDirection = "horizontal";
participantGridRatio = 1 / 3;
}
return { participantGridRatio, layoutDirection };
}
2021-08-17 16:57:25 -07:00
2021-08-31 13:54:18 -07:00
function centerTiles(positions, gridWidth, gridHeight, offsetLeft, offsetTop) {
2021-08-31 11:36:59 -07:00
const bounds = getSubGridBoundingBox(positions);
2021-08-31 13:54:18 -07:00
const leftOffset = Math.round((gridWidth - bounds.width) / 2) + offsetLeft;
const topOffset = Math.round((gridHeight - bounds.height) / 2) + offsetTop;
2021-08-31 11:36:59 -07:00
applyTileOffsets(positions, leftOffset, topOffset);
return positions;
}
function applyTileOffsets(positions, leftOffset, topOffset) {
for (const position of positions) {
position.x += leftOffset;
position.y += topOffset;
}
return positions;
}
function getSubGridLayout(tileCount, gridWidth, gridHeight) {
const gridAspectRatio = gridWidth / gridHeight;
let columnCount, rowCount;
let tileAspectRatio = 16 / 9;
if (gridAspectRatio < 3 / 4) {
// Phone
if (tileCount === 1) {
columnCount = 1;
rowCount = 1;
tileAspectRatio = 0;
} else if (tileCount <= 4) {
columnCount = 1;
rowCount = tileCount;
} else if (tileCount <= 12) {
columnCount = 2;
rowCount = Math.ceil(tileCount / columnCount);
tileAspectRatio = 0;
} else {
// Unsupported
columnCount = 3;
rowCount = Math.ceil(tileCount / columnCount);
tileAspectRatio = 1;
}
} else if (gridAspectRatio < 1) {
// Tablet
if (tileCount === 1) {
columnCount = 1;
rowCount = 1;
tileAspectRatio = 0;
} else if (tileCount <= 4) {
columnCount = 1;
rowCount = tileCount;
} else if (tileCount <= 12) {
columnCount = 2;
rowCount = Math.ceil(tileCount / columnCount);
} else {
// Unsupported
columnCount = 3;
rowCount = Math.ceil(tileCount / columnCount);
tileAspectRatio = 1;
}
} else if (gridAspectRatio < 17 / 9) {
// Computer
if (tileCount === 1) {
columnCount = 1;
rowCount = 1;
} else if (tileCount === 2) {
columnCount = 2;
rowCount = 1;
} else if (tileCount <= 4) {
columnCount = 2;
rowCount = 2;
} else if (tileCount <= 6) {
columnCount = 3;
rowCount = 2;
} else if (tileCount <= 8) {
columnCount = 4;
rowCount = 2;
tileAspectRatio = 1;
} else if (tileCount <= 12) {
columnCount = 4;
rowCount = 3;
tileAspectRatio = 1;
} else {
// Unsupported
columnCount = 4;
rowCount = 4;
}
} else if (gridAspectRatio <= 32 / 9) {
// Ultrawide
if (tileCount === 1) {
columnCount = 1;
rowCount = 1;
} else if (tileCount === 2) {
columnCount = 2;
rowCount = 1;
} else if (tileCount <= 4) {
columnCount = 2;
rowCount = 2;
} else if (tileCount <= 6) {
columnCount = 3;
rowCount = 2;
} else if (tileCount <= 8) {
columnCount = 4;
rowCount = 2;
} else if (tileCount <= 12) {
columnCount = 4;
rowCount = 3;
} else {
// Unsupported
columnCount = 4;
rowCount = 4;
}
} else {
// Super Ultrawide
if (tileCount <= 6) {
columnCount = tileCount;
rowCount = 1;
} else {
columnCount = Math.ceil(tileCount / 2);
rowCount = 2;
}
}
return { columnCount, rowCount, tileAspectRatio };
}
function getSubGridPositions(tileCount, gridWidth, gridHeight, gap) {
if (tileCount === 0) {
return [];
}
2021-08-17 16:57:25 -07:00
2021-08-31 11:36:59 -07:00
const { columnCount, rowCount, tileAspectRatio } = getSubGridLayout(
tileCount,
gridWidth,
gridHeight
);
const newTilePositions = [];
2021-08-17 16:57:25 -07:00
const boxWidth = Math.round(
(gridWidth - gap * (columnCount + 1)) / columnCount
);
const boxHeight = Math.round((gridHeight - gap * (rowCount + 1)) / rowCount);
let tileWidth, tileHeight;
if (tileAspectRatio) {
const boxAspectRatio = boxWidth / boxHeight;
if (boxAspectRatio > tileAspectRatio) {
tileWidth = boxHeight * tileAspectRatio;
tileHeight = boxHeight;
2021-08-23 23:40:26 -07:00
} else {
tileWidth = boxWidth;
tileHeight = boxWidth / tileAspectRatio;
2021-08-17 16:57:25 -07:00
}
} else {
tileWidth = boxWidth;
tileHeight = boxHeight;
}
2021-08-17 16:57:25 -07:00
for (let i = 0; i < tileCount; i++) {
const verticalIndex = Math.floor(i / columnCount);
2021-08-31 11:36:59 -07:00
const top = verticalIndex * gap + verticalIndex * tileHeight;
2021-08-17 16:57:25 -07:00
let rowItemCount;
2021-08-17 16:57:25 -07:00
if (verticalIndex + 1 === rowCount && tileCount % columnCount !== 0) {
rowItemCount = tileCount % columnCount;
} else {
rowItemCount = columnCount;
}
const horizontalIndex = i % columnCount;
let centeringPadding = 0;
if (rowItemCount < columnCount) {
2021-08-31 11:36:59 -07:00
const subgridWidth = tileWidth * columnCount + (gap * columnCount - 1);
centeringPadding = Math.round(
2021-08-31 11:36:59 -07:00
(subgridWidth - (tileWidth * rowItemCount + (gap * rowItemCount - 1))) /
2
);
2021-08-17 16:57:25 -07:00
}
const left =
2021-08-31 11:36:59 -07:00
centeringPadding + gap * horizontalIndex + tileWidth * horizontalIndex;
newTilePositions.push({
width: tileWidth,
height: tileHeight,
x: left,
y: top,
});
2021-08-17 16:57:25 -07:00
}
return newTilePositions;
}
2021-09-01 16:42:01 -07:00
export function VideoGrid({ participants, layout }) {
2021-08-17 16:57:25 -07:00
const [{ tiles, tilePositions }, setTileState] = useState({
tiles: [],
tilePositions: [],
});
const draggingTileRef = useRef(null);
2021-08-31 14:31:31 -07:00
const lastTappedRef = useRef({});
2021-09-01 16:42:01 -07:00
const lastLayoutRef = useRef(layout);
2021-08-17 17:25:01 -07:00
const isMounted = useIsMounted();
2021-08-17 16:57:25 -07:00
const [gridRef, gridBounds] = useMeasure();
useEffect(() => {
setTileState(({ tiles }) => {
const newTiles = [];
const removedTileKeys = [];
for (const tile of tiles) {
2021-09-01 16:42:01 -07:00
let participant = participants.find(
2021-08-17 16:57:25 -07:00
(participant) => participant.userId === tile.key
);
2021-09-01 16:42:01 -07:00
let remove = false;
if (!participant) {
remove = true;
participant = tile.participant;
removedTileKeys.push(tile.key);
2021-08-26 12:34:00 -07:00
}
2021-09-01 16:42:01 -07:00
let presenter;
if (layout === "spotlight") {
presenter = participant.activeSpeaker;
2021-08-17 16:57:25 -07:00
} else {
2021-09-01 16:42:01 -07:00
presenter = layout === lastLayoutRef.current ? tile.presenter : false;
2021-08-17 16:57:25 -07:00
}
2021-09-01 16:42:01 -07:00
newTiles.push({
key: participant.userId,
participant,
remove,
presenter,
});
2021-08-17 16:57:25 -07:00
}
for (const participant of participants) {
if (newTiles.some(({ key }) => participant.userId === key)) {
continue;
}
// Added tiles
newTiles.push({
key: participant.userId,
participant,
remove: false,
2021-09-01 16:42:01 -07:00
presenter: layout === "spotlight" && participant.activeSpeaker,
2021-08-17 16:57:25 -07:00
});
2021-08-18 16:27:29 -07:00
}
2021-08-26 12:34:00 -07:00
newTiles.sort((a, b) => (b.presenter ? 1 : 0) - (a.presenter ? 1 : 0));
2021-08-17 16:57:25 -07:00
if (removedTileKeys.length > 0) {
setTimeout(() => {
2021-08-17 17:25:01 -07:00
if (!isMounted.current) {
return;
}
2021-08-17 16:57:25 -07:00
setTileState(({ tiles }) => {
const newTiles = tiles.filter(
(tile) => !removedTileKeys.includes(tile.key)
);
2021-08-18 16:27:29 -07:00
2021-09-01 16:42:01 -07:00
const presenterTileCount = newTiles.reduce(
(count, tile) => count + (tile.presenter ? 1 : 0),
0
);
2021-08-17 16:57:25 -07:00
return {
tiles: newTiles,
tilePositions: getTilePositions(
newTiles.length,
2021-08-31 11:36:59 -07:00
presenterTileCount,
gridBounds.width,
gridBounds.height
),
2021-08-17 16:57:25 -07:00
};
});
}, 250);
}
2021-09-01 16:42:01 -07:00
const presenterTileCount = newTiles.reduce(
(count, tile) => count + (tile.presenter ? 1 : 0),
0
);
2021-08-17 16:57:25 -07:00
return {
tiles: newTiles,
tilePositions: getTilePositions(
newTiles.length,
2021-08-31 11:36:59 -07:00
presenterTileCount,
gridBounds.width,
gridBounds.height
),
2021-08-17 16:57:25 -07:00
};
});
2021-09-01 16:42:01 -07:00
}, [participants, gridBounds, layout]);
2021-08-17 16:57:25 -07:00
const animate = useCallback(
(tiles) => (tileIndex) => {
2021-08-17 16:57:25 -07:00
const tile = tiles[tileIndex];
const tilePosition = tilePositions[tileIndex];
2021-08-17 16:57:25 -07:00
const draggingTile = draggingTileRef.current;
const dragging = draggingTile && tile.key === draggingTile.key;
const remove = tile.remove;
if (dragging) {
return {
width: tilePosition.width,
height: tilePosition.height,
x: draggingTile.offsetX + draggingTile.x,
y: draggingTile.offsetY + draggingTile.y,
scale: 1.1,
opacity: 1,
zIndex: 1,
shadow: 15,
immediate: (key) => key === "zIndex" || key === "x" || key === "y",
from: {
scale: 0,
opacity: 0,
},
reset: false,
};
} else {
return {
...tilePosition,
scale: remove ? 0 : 1,
opacity: remove ? 0 : 1,
zIndex: 0,
shadow: 1,
from: {
scale: 0,
opacity: 0,
},
reset: false,
immediate: (key) => key === "zIndex",
2021-08-17 16:57:25 -07:00
};
}
},
[tiles, tilePositions]
);
const [springs, api] = useSprings(tiles.length, animate(tiles), [
2021-08-17 16:57:25 -07:00
tilePositions,
tiles,
]);
2021-08-31 14:31:31 -07:00
const onTap = useCallback(
(tileKey) => {
const lastTapped = lastTappedRef.current[tileKey];
2021-08-17 16:57:25 -07:00
2021-08-31 14:34:18 -07:00
if (!lastTapped || Date.now() - lastTapped > 500) {
2021-08-31 14:31:31 -07:00
lastTappedRef.current[tileKey] = Date.now();
return;
2021-08-17 16:57:25 -07:00
}
2021-08-31 14:31:31 -07:00
lastTappedRef.current[tileKey] = 0;
2021-08-26 12:34:00 -07:00
2021-08-31 14:31:31 -07:00
const tile = tiles.find((tile) => tile.key === tileKey);
2021-08-26 12:34:00 -07:00
2021-08-31 14:31:31 -07:00
if (!tile) {
return;
2021-08-17 16:57:25 -07:00
}
2021-08-31 14:31:31 -07:00
const participant = tile.participant;
2021-08-17 16:57:25 -07:00
2021-08-26 12:34:00 -07:00
setTileState((state) => {
let presenterTileCount = 0;
const newTiles = state.tiles.map((tile) => {
let newTile = tile;
if (tile.participant === participant) {
newTile = { ...tile, presenter: !tile.presenter };
}
if (newTile.presenter) {
presenterTileCount++;
}
return newTile;
});
newTiles.sort((a, b) => (b.presenter ? 1 : 0) - (a.presenter ? 1 : 0));
presenterTileCount;
return {
...state,
tiles: newTiles,
tilePositions: getTilePositions(
newTiles.length,
2021-08-31 11:36:59 -07:00
presenterTileCount,
gridBounds.width,
gridBounds.height
2021-08-26 12:34:00 -07:00
),
};
});
},
2021-08-31 14:31:31 -07:00
[tiles, gridBounds]
);
const bind = useDrag(
2021-08-31 14:44:21 -07:00
({ args: [key], active, xy, movement, tap, event }) => {
event.preventDefault();
2021-08-31 14:31:31 -07:00
if (tap) {
onTap(key);
return;
}
const dragTileIndex = tiles.findIndex((tile) => tile.key === key);
const dragTile = tiles[dragTileIndex];
const dragTilePosition = tilePositions[dragTileIndex];
let newTiles = tiles;
const cursorPosition = [xy[0] - gridBounds.left, xy[1] - gridBounds.top];
for (
let hoverTileIndex = 0;
hoverTileIndex < tiles.length;
hoverTileIndex++
) {
const hoverTile = tiles[hoverTileIndex];
const hoverTilePosition = tilePositions[hoverTileIndex];
if (hoverTile.key === key) {
continue;
}
if (isInside(cursorPosition, hoverTilePosition)) {
newTiles = moveArrItem(tiles, dragTileIndex, hoverTileIndex);
newTiles = newTiles.map((tile) => {
if (tile === hoverTile) {
return { ...tile, presenter: dragTile.presenter };
} else if (tile === dragTile) {
return { ...tile, presenter: hoverTile.presenter };
} else {
return tile;
}
});
newTiles.sort(
(a, b) => (b.presenter ? 1 : 0) - (a.presenter ? 1 : 0)
);
setTileState((state) => ({ ...state, tiles: newTiles }));
break;
}
}
if (active) {
if (!draggingTileRef.current) {
draggingTileRef.current = {
key: dragTile.key,
offsetX: dragTilePosition.x,
offsetY: dragTilePosition.y,
};
}
draggingTileRef.current.x = movement[0];
draggingTileRef.current.y = movement[1];
} else {
draggingTileRef.current = null;
}
api.start(animate(newTiles));
},
{ filterTaps: true }
2021-08-26 12:34:00 -07:00
);
2021-08-17 16:57:25 -07:00
return (
<div className={styles.grid} ref={gridRef}>
{springs.map(({ shadow, ...style }, i) => {
const tile = tiles[i];
2021-08-17 16:57:25 -07:00
return (
<ParticipantTile
{...bind(tile.key)}
key={tile.key}
style={{
boxShadow: shadow.to(
(s) => `rgba(0, 0, 0, 0.5) 0px ${s}px ${2 * s}px 0px`
),
...style,
}}
{...tile}
/>
);
})}
</div>
);
}
2021-08-31 14:31:31 -07:00
function ParticipantTile({ style, participant, remove, presenter, ...rest }) {
2021-08-17 16:57:25 -07:00
const videoRef = useRef();
useEffect(() => {
if (participant.stream) {
2021-08-18 18:34:17 -07:00
if (participant.local) {
videoRef.current.muted = true;
}
2021-08-17 16:57:25 -07:00
videoRef.current.srcObject = participant.stream;
videoRef.current.play();
} else {
videoRef.current.srcObject = null;
}
}, [participant.stream]);
2021-08-19 12:11:12 -07:00
// Firefox doesn't respect the disablePictureInPicture attribute
// https://bugzilla.mozilla.org/show_bug.cgi?id=1611831
2021-08-17 16:57:25 -07:00
return (
<animated.div className={styles.participantTile} style={style} {...rest}>
2021-08-19 12:11:12 -07:00
<div
className={classNames(styles.participantName, {
[styles.speaking]: participant.speaking,
})}
>
2021-08-23 15:44:08 -07:00
{participant.speaking ? (
<MicIcon />
) : participant.audioMuted ? (
<MuteMicIcon className={styles.muteMicIcon} />
) : null}
2021-08-19 12:11:12 -07:00
<span>{participant.userId}</span>
</div>
2021-08-23 15:44:08 -07:00
{participant.videoMuted && (
<DisableVideoIcon
className={styles.videoMuted}
width={48}
height={48}
/>
)}
2021-08-19 12:11:12 -07:00
<video ref={videoRef} playsInline disablePictureInPicture />
2021-08-17 16:57:25 -07:00
</animated.div>
);
}