feat: improve peer video display

This commit is contained in:
Jeremy Kahn 2022-11-07 09:47:32 -06:00
parent 29aef6514b
commit b27a54eac4
2 changed files with 36 additions and 8 deletions

View File

@ -5,11 +5,24 @@ import { PeerNameDisplay } from 'components/PeerNameDisplay'
interface PeerVideoProps { interface PeerVideoProps {
isSelf?: boolean isSelf?: boolean
numberOfPeers: number
userId: string userId: string
videoStream: MediaStream videoStream: MediaStream
} }
export const PeerVideo = ({ isSelf, userId, videoStream }: PeerVideoProps) => { // Adapted from https://www.geeksforgeeks.org/find-the-next-perfect-square-greater-than-a-given-number/
const nextPerfectSquare = (base: number) => {
const nextInteger = Math.floor(Math.sqrt(base)) + 1
return nextInteger * nextInteger
}
export const PeerVideo = ({
isSelf,
numberOfPeers,
userId,
videoStream,
}: PeerVideoProps) => {
const videoRef = useRef<HTMLVideoElement>(null) const videoRef = useRef<HTMLVideoElement>(null)
useEffect(() => { useEffect(() => {
@ -20,15 +33,20 @@ export const PeerVideo = ({ isSelf, userId, videoStream }: PeerVideoProps) => {
video.srcObject = videoStream video.srcObject = videoStream
}, [videoRef, videoStream]) }, [videoRef, videoStream])
const sizePercent = 100 / Math.sqrt(nextPerfectSquare(numberOfPeers - 1))
return ( return (
<Paper <Paper
sx={{ sx={{
py: 2,
margin: '0.5em',
flexShrink: 1,
overflow: 'auto',
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: 'column',
flexShrink: 1,
justifyContent: 'center',
margin: '0.5em',
overflow: 'auto',
py: 2,
width: `calc(${sizePercent}% - 1em)`,
height: `calc(${sizePercent}% - 1em)`,
}} }}
elevation={10} elevation={10}
> >

View File

@ -35,27 +35,37 @@ export const RoomVideoDisplay = ({ userId }: RoomVideoDisplayProps) => {
[] []
) )
const numberOfPeers = (selfVideoStream ? 1 : 0) + peersWithVideo.length
return ( return (
<Paper <Paper
className="RoomVideoDisplay" className="RoomVideoDisplay"
elevation={3} elevation={3}
square square
sx={{ sx={{
alignItems: 'stretch', alignContent: 'center',
alignItems: 'center',
display: 'flex', display: 'flex',
flexDirection: 'column', flexDirection: numberOfPeers === 1 ? 'column' : 'row',
flexGrow: 1, flexGrow: 1,
flexWrap: 'wrap',
justifyContent: 'center', justifyContent: 'center',
overflow: 'auto', overflow: 'auto',
width: '75%', width: '75%',
}} }}
> >
{selfVideoStream && ( {selfVideoStream && (
<PeerVideo isSelf userId={userId} videoStream={selfVideoStream} /> <PeerVideo
isSelf
numberOfPeers={numberOfPeers}
userId={userId}
videoStream={selfVideoStream}
/>
)} )}
{peersWithVideo.map(peerWithVideo => ( {peersWithVideo.map(peerWithVideo => (
<PeerVideo <PeerVideo
key={peerWithVideo.peer.peerId} key={peerWithVideo.peer.peerId}
numberOfPeers={numberOfPeers}
userId={peerWithVideo.peer.userId} userId={peerWithVideo.peer.userId}
videoStream={peerWithVideo.videoStream} videoStream={peerWithVideo.videoStream}
/> />