Properly use LiveKit screen sharing

This commit is contained in:
Daniel Abramov 2023-06-06 13:16:36 +02:00
parent 79018606b2
commit dc98960d8d
3 changed files with 70 additions and 71 deletions

View file

@ -72,6 +72,7 @@ import { RageshakeRequestModal } from "./RageshakeRequestModal";
import { MatrixInfo } from "./VideoPreview"; import { MatrixInfo } from "./VideoPreview";
import { useJoinRule } from "./useJoinRule"; import { useJoinRule } from "./useJoinRule";
import { ParticipantInfo } from "./useGroupCall"; import { ParticipantInfo } from "./useGroupCall";
import { TileContent } from "../video-grid/VideoTile";
const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {}); const canScreenshare = "getDisplayMedia" in (navigator.mediaDevices ?? {});
// There is currently a bug in Safari our our code with cloning and sending MediaStreams // There is currently a bug in Safari our our code with cloning and sending MediaStreams
@ -234,18 +235,6 @@ export function InCallView({
const reducedControls = boundsValid && bounds.width <= 400; const reducedControls = boundsValid && bounds.width <= 400;
const noControls = reducedControls && bounds.height <= 400; const noControls = reducedControls && bounds.height <= 400;
const items = useParticipantTiles(livekitRoom, participants, {
userId: client.getUserId()!,
deviceId: client.getDeviceId()!,
});
// The maximised participant: the focused (active) participant if the
// window is too small to show everyone.
const maximisedParticipant = useMemo(
() => (noControls ? items.find((item) => item.focused) ?? null : null),
[noControls, items]
);
const renderAvatar = useCallback( const renderAvatar = useCallback(
(roomMember: RoomMember, width: number, height: number) => { (roomMember: RoomMember, width: number, height: number) => {
const avatarUrl = roomMember.getMxcAvatarUrl(); const avatarUrl = roomMember.getMxcAvatarUrl();
@ -266,6 +255,8 @@ export function InCallView({
const prefersReducedMotion = usePrefersReducedMotion(); const prefersReducedMotion = usePrefersReducedMotion();
const items = useParticipantTiles(livekitRoom, participants);
const renderContent = (): JSX.Element => { const renderContent = (): JSX.Element => {
if (items.length === 0) { if (items.length === 0) {
return ( return (
@ -274,18 +265,6 @@ export function InCallView({
</div> </div>
); );
} }
if (maximisedParticipant) {
return (
<VideoTileContainer
height={bounds.height}
width={bounds.width}
key={maximisedParticipant.id}
item={maximisedParticipant.data}
getAvatar={renderAvatar}
maximised={Boolean(maximisedParticipant)}
/>
);
}
return ( return (
<VideoGrid <VideoGrid
@ -296,7 +275,6 @@ export function InCallView({
{(child) => ( {(child) => (
<VideoTileContainer <VideoTileContainer
getAvatar={renderAvatar} getAvatar={renderAvatar}
maximised={false}
item={child.data} item={child.data}
{...child} {...child}
/> />
@ -311,7 +289,7 @@ export function InCallView({
} = useRageshakeRequestModal(groupCall.room.roomId); } = useRageshakeRequestModal(groupCall.room.roomId);
const containerClasses = classNames(styles.inRoom, { const containerClasses = classNames(styles.inRoom, {
[styles.maximised]: maximisedParticipant, [styles.maximised]: undefined,
}); });
let footer: JSX.Element | null; let footer: JSX.Element | null;
@ -337,16 +315,14 @@ export function InCallView({
onPress={toggleScreenSharing} onPress={toggleScreenSharing}
/> />
)} )}
{!maximisedParticipant && ( <OverflowMenu
<OverflowMenu roomId={matrixInfo.roomId}
roomId={matrixInfo.roomId} mediaDevices={mediaDevices}
mediaDevices={mediaDevices} inCall
inCall showInvite={joinRule === JoinRule.Public}
showInvite={joinRule === JoinRule.Public} feedbackModalState={feedbackModalState}
feedbackModalState={feedbackModalState} feedbackModalProps={feedbackModalProps}
feedbackModalProps={feedbackModalProps} />
/>
)}
<HangupButton onPress={onLeave} /> <HangupButton onPress={onLeave} />
</div> </div>
); );
@ -354,7 +330,7 @@ export function InCallView({
return ( return (
<div className={containerClasses} ref={containerRef}> <div className={containerClasses} ref={containerRef}>
{!hideHeader && !maximisedParticipant && ( {!hideHeader && (
<Header> <Header>
<LeftNav> <LeftNav>
<RoomHeaderInfo <RoomHeaderInfo
@ -389,43 +365,54 @@ export function InCallView({
); );
} }
interface ParticipantID {
userId: string;
deviceId: string;
}
function useParticipantTiles( function useParticipantTiles(
livekitRoom: Room, livekitRoom: Room,
participants: Map<RoomMember, Map<string, ParticipantInfo>>, participants: Map<RoomMember, Map<string, ParticipantInfo>>
local: ParticipantID
): TileDescriptor<ItemData>[] { ): TileDescriptor<ItemData>[] {
const sfuParticipants = useParticipants({ const sfuParticipants = useParticipants({
room: livekitRoom, room: livekitRoom,
}); });
const [localUserId, localDeviceId] = [local.userId, local.deviceId];
const items = useMemo(() => { const items = useMemo(() => {
const tiles: TileDescriptor<ItemData>[] = []; const tiles: TileDescriptor<ItemData>[] = [];
for (const [member, participantMap] of participants) { for (const [member, participantMap] of participants) {
for (const [deviceId] of participantMap) { for (const [deviceId] of participantMap) {
const id = `${member.userId}:${deviceId}`; const id = `${member.userId}:${deviceId}`;
const sfuParticipant = sfuParticipants.find((p) => p.identity === id); const sfuParticipant = sfuParticipants.find((p) => p.identity === id);
const hasScreenShare = // Skip rendering participants that did not connect to the SFU.
sfuParticipant?.getTrack(Track.Source.ScreenShare) !== undefined; if (!sfuParticipant) {
continue;
}
const descriptor = { const userMediaTile = {
id, id,
focused: hasScreenShare && !sfuParticipant?.isLocal, focused: false,
local: member.userId == localUserId && deviceId == localDeviceId, local: sfuParticipant.isLocal,
data: { data: {
member, member,
sfuParticipant, sfuParticipant,
content: TileContent.UserMedia,
}, },
}; };
tiles.push(descriptor); // Add a tile for user media.
tiles.push(userMediaTile);
// If there is a screen sharing enabled for this participant, create a tile for it as well.
if (sfuParticipant.isScreenShareEnabled) {
const screenShareTile = {
...userMediaTile,
id: `${id}:screen-share`,
focused: true,
data: {
...userMediaTile.data,
content: TileContent.ScreenShare,
},
};
tiles.push(screenShareTile);
}
} }
} }
@ -434,7 +421,7 @@ function useParticipantTiles(
); );
return tiles; return tiles;
}, [localUserId, localDeviceId, participants, sfuParticipants]); }, [participants, sfuParticipants]);
return items; return items;
} }

View file

@ -29,20 +29,29 @@ import styles from "./VideoTile.module.css";
import { ReactComponent as MicMutedIcon } from "../icons/MicMuted.svg"; import { ReactComponent as MicMutedIcon } from "../icons/MicMuted.svg";
import { ReactComponent as VideoMutedIcon } from "../icons/VideoMuted.svg"; import { ReactComponent as VideoMutedIcon } from "../icons/VideoMuted.svg";
export enum TileContent {
UserMedia = "user-media",
ScreenShare = "screen-share",
}
interface Props { interface Props {
name: string;
avatar?: JSX.Element; avatar?: JSX.Element;
className?: string; className?: string;
name: string;
sfuParticipant: LocalParticipant | RemoteParticipant; sfuParticipant: LocalParticipant | RemoteParticipant;
content: TileContent;
} }
export const VideoTile = forwardRef<HTMLDivElement, Props>( export const VideoTile = forwardRef<HTMLDivElement, Props>(
({ name, avatar, className, sfuParticipant, ...rest }, ref) => { ({ name, avatar, className, sfuParticipant, content, ...rest }, ref) => {
const { t } = useTranslation(); const { t } = useTranslation();
const audioEl = React.useRef<HTMLAudioElement>(null); const audioEl = React.useRef<HTMLAudioElement>(null);
const { isMuted: microphoneMuted } = useMediaTrack( const { isMuted: microphoneMuted } = useMediaTrack(
Track.Source.Microphone, content === TileContent.UserMedia
? Track.Source.Microphone
: Track.Source.ScreenShareAudio,
sfuParticipant, sfuParticipant,
{ {
element: audioEl, element: audioEl,
@ -56,7 +65,6 @@ export const VideoTile = forwardRef<HTMLDivElement, Props>(
[styles.speaking]: sfuParticipant.isSpeaking, [styles.speaking]: sfuParticipant.isSpeaking,
[styles.muted]: microphoneMuted, [styles.muted]: microphoneMuted,
[styles.screenshare]: false, [styles.screenshare]: false,
[styles.maximised]: false,
})} })}
ref={ref} ref={ref}
{...rest} {...rest}
@ -80,7 +88,14 @@ export const VideoTile = forwardRef<HTMLDivElement, Props>(
<ConnectionQualityIndicator participant={sfuParticipant} /> <ConnectionQualityIndicator participant={sfuParticipant} />
</div> </div>
))} ))}
<VideoTrack participant={sfuParticipant} source={Track.Source.Camera} /> <VideoTrack
participant={sfuParticipant}
source={
content === TileContent.UserMedia
? Track.Source.Camera
: Track.Source.ScreenShare
}
/>
<audio ref={audioEl} /> <audio ref={audioEl} />
</animated.div> </animated.div>
); );

View file

@ -19,11 +19,12 @@ import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { LocalParticipant, RemoteParticipant } from "livekit-client"; import { LocalParticipant, RemoteParticipant } from "livekit-client";
import { useRoomMemberName } from "./useRoomMemberName"; import { useRoomMemberName } from "./useRoomMemberName";
import { VideoTile } from "./VideoTile"; import { TileContent, VideoTile } from "./VideoTile";
export interface ItemData { export interface ItemData {
member: RoomMember; member: RoomMember;
sfuParticipant?: LocalParticipant | RemoteParticipant; sfuParticipant: LocalParticipant | RemoteParticipant;
content: TileContent;
} }
interface Props { interface Props {
@ -35,7 +36,6 @@ interface Props {
width: number, width: number,
height: number height: number
) => JSX.Element; ) => JSX.Element;
maximised: boolean;
} }
export function VideoTileContainer({ export function VideoTileContainer({
@ -43,22 +43,19 @@ export function VideoTileContainer({
width, width,
height, height,
getAvatar, getAvatar,
maximised,
...rest ...rest
}: Props) { }: Props) {
const { rawDisplayName } = useRoomMemberName(item.member); const { rawDisplayName } = useRoomMemberName(item.member);
return ( return (
<> <>
{!item.sfuParticipant && null} <VideoTile
{item.sfuParticipant && ( sfuParticipant={item.sfuParticipant}
<VideoTile content={item.content}
sfuParticipant={item.sfuParticipant} name={rawDisplayName}
name={rawDisplayName} avatar={getAvatar && getAvatar(item.member, width, height)}
avatar={getAvatar && getAvatar(item.member, width, height)} {...rest}
{...rest} />
/>
)}
</> </>
); );
} }