1
0

Meida working

This commit is contained in:
James Ketr 2025-10-10 20:01:42 -07:00
parent 579632c293
commit a586f3b491
3 changed files with 123 additions and 113 deletions

View File

@ -163,7 +163,9 @@ const PlayerList: React.FC = () => {
> >
<MediaAgent {...{ session, peers, setPeers }} /> <MediaAgent {...{ session, peers, setPeers }} />
<List className="PlayerSelector"> <List className="PlayerSelector">
{players?.map((player) => ( {players?.map((player) => {
const peerObj = peers[player.session_id] || peers[player.name];
return (
<Box <Box
key={player.session_id} key={player.session_id}
sx={{ display: "flex", flexDirection: "column", alignItems: "center" }} sx={{ display: "flex", flexDirection: "column", alignItems: "center" }}
@ -190,17 +192,17 @@ const PlayerList: React.FC = () => {
</Box> </Box>
{player.name && !player.live && <div className="NoNetwork"></div>} {player.name && !player.live && <div className="NoNetwork"></div>}
</Box> </Box>
{player.name && player.live && peers[player.session_id] && (player.local || player.has_media !== false) ? ( {player.name && player.live && peerObj && (player.local || player.has_media !== false) ? (
<> <>
<MediaControl <MediaControl
sx={{ border: "3px solid blue" }} sx={{ border: "3px solid blue" }}
className="Medium" className="Medium"
key={player.session_id} key={player.session_id}
peer={peers[player.session_id]} peer={peerObj}
isSelf={player.local} isSelf={player.local}
sendJsonMessage={player.local ? sendJsonMessage : undefined} sendJsonMessage={player.local ? sendJsonMessage : undefined}
remoteAudioMuted={peers[player.session_id].muted} remoteAudioMuted={peerObj?.muted}
remoteVideoOff={peers[player.session_id].video_on === false} remoteVideoOff={peerObj?.video_on === false}
/> />
{/* If this is the local player and they haven't picked a color, show a picker */} {/* If this is the local player and they haven't picked a color, show a picker */}
@ -253,7 +255,8 @@ const PlayerList: React.FC = () => {
<video className="Video"></video> <video className="Video"></video>
)} )}
</Box> </Box>
))} );
})}
</List> </List>
</Paper> </Paper>
</Box> </Box>

View File

@ -1222,13 +1222,13 @@ const setPlayerName = (game: Game, session: Session, name: string): string | und
message = `${name} has rejoined the lobby.`; message = `${name} has rejoined the lobby.`;
} }
session.name = name; session.name = name;
if (session.ws && game.id in audio && session.id in audio[game.id]) { if (session.ws && game.id in audio && session.id in audio[game.id]!) {
webrtcPart(audio[game.id], session); webrtcPart(audio[game.id]!, session);
} }
} else { } else {
message = `${session.name} has changed their name to ${name}.`; message = `${session.name} hs changed their name to ${name}.`;
if (session.ws && game.id in audio) { if (session.ws && game.id in audio) {
webrtcPart(audio[game.id], session); webrtcPart(audio[game.id]!, session);
} }
} }
} }
@ -1245,7 +1245,7 @@ const setPlayerName = (game: Game, session: Session, name: string): string | und
} }
if (session.ws && session.hasAudio) { if (session.ws && session.hasAudio) {
webrtcJoin(audio[game.id], session); webrtcJoin(audio[game.id]!, session);
} }
console.log(`${info}: ${message}`); console.log(`${info}: ${message}`);
addChatMessage(game, null, message); addChatMessage(game, null, message);
@ -3822,7 +3822,7 @@ router.ws("/ws/:id", async (ws, req) => {
/* Cleanup any voice channels */ /* Cleanup any voice channels */
if (gameId in audio) { if (gameId in audio) {
try { try {
webrtcPart(audio[gameId], session); webrtcPart(audio[gameId]!, session);
} catch (e) { } catch (e) {
console.warn(`${short}: Error during part():`, e); console.warn(`${short}: Error during part():`, e);
} }
@ -3932,7 +3932,7 @@ router.ws("/ws/:id", async (ws, req) => {
// Clean up peer from audio registry before replacing WebSocket // Clean up peer from audio registry before replacing WebSocket
if (gameId in audio) { if (gameId in audio) {
try { try {
webrtcPart(audio[gameId], session); webrtcPart(audio[gameId]!, session);
console.log(`${short}: Cleaned up peer ${session.name} from audio registry during reconnection`); console.log(`${short}: Cleaned up peer ${session.name} from audio registry during reconnection`);
} catch (e) { } catch (e) {
console.warn(`${short}: Error cleaning up peer during reconnection:`, e); console.warn(`${short}: Error cleaning up peer during reconnection:`, e);
@ -3968,11 +3968,11 @@ router.ws("/ws/:id", async (ws, req) => {
// Accept either legacy `config`, newer `data`, or flat payloads where // Accept either legacy `config`, newer `data`, or flat payloads where
// the client sent fields at the top level (normalizeIncoming will // the client sent fields at the top level (normalizeIncoming will
// populate `data` with the parsed object in that case). // populate `data` with the parsed object in that case).
webrtcJoin(audio[gameId], session); webrtcJoin(audio[gameId]!, session);
break; break;
case "part": case "part":
webrtcPart(audio[gameId], session); webrtcPart(audio[gameId]!, session);
break; break;
case "relayICECandidate": case "relayICECandidate":

View File

@ -10,7 +10,13 @@
import { Session } from "./games/types"; import { Session } from "./games/types";
export const audio: Record<string, any> = {}; interface Peer {
ws: any;
name: string;
}
/* Map of session => peer_id => peer */
export const audio: Record<string, Record<string, Peer>> = {};
// Default send helper used when caller doesn't provide a safeSend implementation. // Default send helper used when caller doesn't provide a safeSend implementation.
const defaultSend = (targetOrSession: any, message: any): boolean => { const defaultSend = (targetOrSession: any, message: any): boolean => {
@ -29,7 +35,7 @@ const defaultSend = (targetOrSession: any, message: any): boolean => {
} }
}; };
export const join = (peers: any, session: any): void => { export const join = (peers: Record<string, Peer>, session: Session): void => {
const send = defaultSend; const send = defaultSend;
const ws = session.ws; const ws = session.ws;
@ -45,18 +51,19 @@ export const join = (peers: any, session: any): void => {
console.log(`${session.short}: <- join - ${session.name}`); console.log(`${session.short}: <- join - ${session.name}`);
const peer = peers[session.id];
// Use session.id as the canonical peer key // Use session.id as the canonical peer key
if (session.id in peers) { if (peer) {
console.log(`${session.short}:${session.id} - Already joined to Audio, updating WebSocket reference.`); console.log(`${session.short}:${session.id} - Already joined to Audio, updating WebSocket reference.`);
try { try {
const prev = peers[session.id] && peers[session.id].ws; const prev = peer.ws;
if (prev && prev._pingInterval) { if (prev && prev._pingInterval) {
clearInterval(prev._pingInterval); clearInterval(prev._pingInterval);
} }
} catch (e) { } catch (e) {
/* ignore */ /* ignore */
} }
peers[session.id].ws = ws; peer.ws = ws;
send(ws, { send(ws, {
type: "join_status", type: "join_status",
@ -72,7 +79,7 @@ export const join = (peers: any, session: any): void => {
type: "addPeer", type: "addPeer",
data: { data: {
peer_id: peerId, peer_id: peerId,
peer_name: peers[peerId].name || peerId, peer_name: peers[peerId]!.name,
should_create_offer: true, should_create_offer: true,
}, },
}); });
@ -82,7 +89,7 @@ export const join = (peers: any, session: any): void => {
for (const peerId in peers) { for (const peerId in peers) {
if (peerId === session.id) continue; if (peerId === session.id) continue;
send(peers[peerId].ws, { send(peers[peerId]!.ws, {
type: "addPeer", type: "addPeer",
data: { data: {
peer_id: session.id, peer_id: session.id,
@ -97,7 +104,7 @@ export const join = (peers: any, session: any): void => {
for (let peerId in peers) { for (let peerId in peers) {
// notify existing peers about the new client // notify existing peers about the new client
send(peers[peerId].ws, { send(peers[peerId]!.ws, {
type: "addPeer", type: "addPeer",
data: { data: {
peer_id: session.id, peer_id: session.id,
@ -111,7 +118,7 @@ export const join = (peers: any, session: any): void => {
type: "addPeer", type: "addPeer",
data: { data: {
peer_id: peerId, peer_id: peerId,
peer_name: peers[peerId].name || peerId, peer_name: peers[peerId]!.name || peerId,
should_create_offer: true, should_create_offer: true,
}, },
}); });
@ -130,7 +137,7 @@ export const join = (peers: any, session: any): void => {
}); });
}; };
export const part = (peers: any, session: any): void => { export const part = (peers: Record<string, Peer>, session: Session): void => {
const ws = session.ws; const ws = session.ws;
const send = defaultSend; const send = defaultSend;
@ -151,7 +158,7 @@ export const part = (peers: any, session: any): void => {
delete peers[session.id]; delete peers[session.id];
for (let peerId in peers) { for (let peerId in peers) {
send(peers[peerId].ws, { send(peers[peerId]!.ws, {
type: "removePeer", type: "removePeer",
data: { data: {
peer_id: session.id, peer_id: session.id,
@ -162,7 +169,7 @@ export const part = (peers: any, session: any): void => {
type: "removePeer", type: "removePeer",
data: { data: {
peer_id: peerId, peer_id: peerId,
peer_name: peers[peerId].name || peerId, peer_name: peers[peerId]!.name || peerId,
}, },
}); });
} }
@ -195,8 +202,8 @@ export const handleRelayICECandidate = (gameId: string, cfg: any, session: Sessi
}, },
}); });
if (peer_id in audio[gameId]) { if (peer_id in audio[gameId]!) {
const target = audio[gameId][peer_id] as any; const target = audio[gameId]![peer_id] as any;
if (!target || !target.ws) { if (!target || !target.ws) {
console.warn(`${session.id}:${gameId} relayICECandidate - target ${peer_id} has no ws`); console.warn(`${session.id}:${gameId} relayICECandidate - target ${peer_id} has no ws`);
} else if (!send(target.ws, message)) { } else if (!send(target.ws, message)) {
@ -236,8 +243,8 @@ export const handleRelaySessionDescription = (gameId: string, cfg: any, session:
session_description, session_description,
}, },
}); });
if (peer_id in audio[gameId]) { if (peer_id in audio[gameId]!) {
const target = audio[gameId][peer_id] as any; const target = audio[gameId]![peer_id] as any;
if (!target || !target.ws) { if (!target || !target.ws) {
console.warn(`${session.id}:${gameId} relaySessionDescription - target ${peer_id} has no ws`); console.warn(`${session.id}:${gameId} relaySessionDescription - target ${peer_id} has no ws`);
} else if (!send(target.ws, message)) { } else if (!send(target.ws, message)) {