Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 180 additions & 23 deletions frontend/src/features/call/components/CallParticipantsGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import type { RefObject } from 'react';

import type { RemoteParticipant } from '../hooks/useCallPeers';
Expand All @@ -18,6 +18,118 @@ export const CallParticipantsGrid = ({
containerRef,
isFullscreen,
}: CallParticipantsGridProps) => {
const blockedElementsRef = useRef(new Set<HTMLVideoElement>());
const [autoplayBlocked, setAutoplayBlocked] = useState(false);

const isAutoplayDeniedError = (error: unknown): error is DOMException =>
error instanceof DOMException && error.name === 'NotAllowedError';

const updateAutoplayBlockedState = useCallback(() => {
setAutoplayBlocked(blockedElementsRef.current.size > 0);
}, []);

const attemptToPlay = useCallback(
(element: HTMLVideoElement) => {
let playPromise: Promise<void> | undefined;

try {
playPromise = element.play();
} catch (error) {
if (isAutoplayDeniedError(error)) {
blockedElementsRef.current.add(element);
updateAutoplayBlockedState();
return;
}

blockedElementsRef.current.delete(element);
updateAutoplayBlockedState();
return;
}

if (!playPromise) {
blockedElementsRef.current.delete(element);
updateAutoplayBlockedState();
return;
}

playPromise
.then(() => {
blockedElementsRef.current.delete(element);
updateAutoplayBlockedState();
})
.catch((error) => {
if (isAutoplayDeniedError(error)) {
blockedElementsRef.current.add(element);
updateAutoplayBlockedState();
return;
}

blockedElementsRef.current.delete(element);
updateAutoplayBlockedState();
});
},
[updateAutoplayBlockedState],
);

const retryBlockedElements = useCallback(() => {
const blockedElements = Array.from(blockedElementsRef.current);

blockedElements.forEach((element) => {
if (!element.isConnected) {
blockedElementsRef.current.delete(element);
return;
}

attemptToPlay(element);
});

updateAutoplayBlockedState();
}, [attemptToPlay, updateAutoplayBlockedState]);

useEffect(() => {
if (!autoplayBlocked) {
return;
}

if (typeof document === 'undefined') {
return;
}

const target = containerRef.current ?? document;

const handleInteraction = () => {
retryBlockedElements();
};

target.addEventListener('click', handleInteraction);
target.addEventListener('touchend', handleInteraction);

retryBlockedElements();

return () => {
target.removeEventListener('click', handleInteraction);
target.removeEventListener('touchend', handleInteraction);
};
}, [autoplayBlocked, containerRef, retryBlockedElements]);

const clearBlockedElementById = useCallback(
(participantId: string) => {
let removed = false;

blockedElementsRef.current.forEach((element) => {
if (element.dataset?.autoplayParticipantId === participantId) {
blockedElementsRef.current.delete(element);
removed = true;
}
});

if (removed) {
updateAutoplayBlockedState();
}
},
[updateAutoplayBlockedState],
);

const attachStreamToElement = useCallback(
(element: HTMLVideoElement | null, stream: MediaStream | null | undefined) => {
if (!element) {
Expand All @@ -30,18 +142,45 @@ export const CallParticipantsGrid = ({
if (stream && currentStream !== stream) {
element.srcObject = stream;
streamAttached = true;
} else if (!stream) {
blockedElementsRef.current.delete(element);
updateAutoplayBlockedState();
}

if ((streamAttached || element.paused) && element.srcObject) {
element.play().catch(() => undefined);
attemptToPlay(element);
}
},
[attemptToPlay, updateAutoplayBlockedState],
);

const handleRemoteVideoElement = useCallback(
(
element: HTMLVideoElement | null,
participantId: string,
stream: MediaStream | null | undefined,
) => {
if (!element) {
clearBlockedElementById(participantId);
return;
}

element.dataset.autoplayParticipantId = participantId;
attachStreamToElement(element, stream);
},
[],
[attachStreamToElement, clearBlockedElementById],
);

const handleLocalVideoRef = useCallback(
(element: HTMLVideoElement | null) => {
const previousElement = localVideoRef.current;
if (!element && previousElement) {
blockedElementsRef.current.delete(previousElement);
updateAutoplayBlockedState();
}

localVideoRef.current = element;

if (!element) {
return;
}
Expand All @@ -50,23 +189,32 @@ export const CallParticipantsGrid = ({
element.muted = true;
attachStreamToElement(element, stream);
},
[attachStreamToElement, localStreamRef, localVideoRef],
[attachStreamToElement, localStreamRef, localVideoRef, updateAutoplayBlockedState],
);

if (isFullscreen && remoteParticipants.length > 0) {
const [primaryParticipant, ...secondaryParticipants] = remoteParticipants;

return (
<div ref={containerRef} className="video-stage video-stage--fullscreen">
{autoplayBlocked && (
<button
type="button"
className="video-grid__autoplay-warning"
onClick={retryBlockedElements}
>
Нажмите, чтобы включить звук участников
</button>
)}
<div className="video-stage__primary">
<video
autoPlay
playsInline
ref={(element) => {
attachStreamToElement(element, primaryParticipant.stream);
}}
className="video-stage__primary-video"
/>
<video
autoPlay
playsInline
ref={(element) => {
handleRemoteVideoElement(element, primaryParticipant.id, primaryParticipant.stream);
}}
className="video-stage__primary-video"
/>
<span className="video-grid__label">{primaryParticipant.id.slice(0, 6)}</span>
</div>

Expand All @@ -83,16 +231,16 @@ export const CallParticipantsGrid = ({

{secondaryParticipants.length > 0 && (
<div className="video-stage__thumbnails">
{secondaryParticipants.map((participant) => (
<div key={participant.id} className="video-stage__thumbnail">
<video
autoPlay
playsInline
ref={(element) => {
attachStreamToElement(element, participant.stream);
}}
className="video-stage__thumbnail-video"
/>
{secondaryParticipants.map((participant) => (
<div key={participant.id} className="video-stage__thumbnail">
<video
autoPlay
playsInline
ref={(element) => {
handleRemoteVideoElement(element, participant.id, participant.stream);
}}
className="video-stage__thumbnail-video"
/>
<span className="video-grid__label">{participant.id.slice(0, 6)}</span>
</div>
))}
Expand All @@ -104,6 +252,15 @@ export const CallParticipantsGrid = ({

return (
<div ref={containerRef} className="video-grid">
{autoplayBlocked && (
<button
type="button"
className="video-grid__autoplay-warning"
onClick={retryBlockedElements}
>
Нажмите, чтобы включить звук участников
</button>
)}
<div className="video-grid__item">
<video ref={handleLocalVideoRef} autoPlay playsInline muted className="video-grid__video" />
<span className="video-grid__label">Вы</span>
Expand All @@ -114,7 +271,7 @@ export const CallParticipantsGrid = ({
autoPlay
playsInline
ref={(element) => {
attachStreamToElement(element, participant.stream);
handleRemoteVideoElement(element, participant.id, participant.stream);
}}
/>
<span className="video-grid__label">{participant.id.slice(0, 6)}</span>
Expand Down
31 changes: 31 additions & 0 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ a:hover {
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
width: 100%;
position: relative;
}

.video-grid video {
Expand Down Expand Up @@ -128,6 +129,36 @@ a:hover {
font-size: 12px;
}

.video-grid__autoplay-warning {
position: absolute;
top: 16px;
left: 50%;
transform: translateX(-50%);
z-index: 5;
background: rgba(0, 0, 0, 0.75);
color: #fff;
padding: 10px 20px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-radius: 999px;
font-size: 14px;
line-height: 1.4;
cursor: pointer;
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(12px);
transition: background 0.2s ease, border-color 0.2s ease, transform 0.2s ease;
}

.video-grid__autoplay-warning:hover {
background: rgba(0, 0, 0, 0.85);
border-color: rgba(255, 255, 255, 0.35);
}

.video-grid__autoplay-warning:focus-visible {
outline: 2px solid #1677ff;
outline-offset: 2px;
transform: translateX(-50%) scale(1.02);
}

.video-stage {
position: relative;
width: 100%;
Expand Down