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
84 changes: 84 additions & 0 deletions src/components/ui/production-line/data-channel-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
parseDataChannelMessage,
isRemoteMute,
isForceDisconnect,
getMuteStateBroadcast,
type EndpointMessage,
} from "./data-channel-parser.ts";

Expand Down Expand Up @@ -73,6 +74,36 @@ describe("parseDataChannelMessage", () => {
}
});

it("parses a global mute-state broadcast (#678)", () => {
const data = JSON.stringify({
type: "EndpointMessage",
payload: { muteState: "mute", participantId: "ep-9" },
to: "",
from: "ep-admin",
});

const result = parseDataChannelMessage(data);

expect(result.type).toBe("EndpointMessage");
if (result.type === "EndpointMessage") {
expect(result.payload.muteState).toBe("mute");
expect(result.payload.participantId).toBe("ep-9");
}
});

it("returns unknown when muteState is present but participantId is missing", () => {
const data = JSON.stringify({
type: "EndpointMessage",
payload: { muteState: "mute" },
to: "",
from: "ep-admin",
});

const result = parseDataChannelMessage(data);

expect(result.type).toBe("unknown");
});

it("returns unknown for invalid JSON", () => {
const result = parseDataChannelMessage("not json at all");

Expand Down Expand Up @@ -149,6 +180,59 @@ describe("isRemoteMute", () => {
});
});

describe("getMuteStateBroadcast", () => {
it("returns participantId and muted=true for a mute broadcast", () => {
const msg: EndpointMessage = {
type: "EndpointMessage",
payload: { muteState: "mute", participantId: "ep-9" },
to: "",
from: "ep-admin",
};

expect(getMuteStateBroadcast(msg)).toEqual({
participantId: "ep-9",
muted: true,
});
});

it("returns muted=false for an unmute broadcast", () => {
const msg: EndpointMessage = {
type: "EndpointMessage",
payload: { muteState: "unmute", participantId: "ep-9" },
to: "",
from: "ep-admin",
};

expect(getMuteStateBroadcast(msg)).toEqual({
participantId: "ep-9",
muted: false,
});
});

it("returns null for a targeted self-mute directive (no broadcast fields)", () => {
const msg: EndpointMessage = {
type: "EndpointMessage",
payload: { muteParticipant: "mute" },
to: "ep-1",
from: "ep-2",
};

expect(getMuteStateBroadcast(msg)).toBeNull();
});

it("does not report a mute-state broadcast as a remote self-mute or kick", () => {
const msg: EndpointMessage = {
type: "EndpointMessage",
payload: { muteState: "mute", participantId: "ep-9" },
to: "",
from: "ep-admin",
};

expect(isRemoteMute(msg)).toBe(false);
expect(isForceDisconnect(msg)).toBe(false);
});
});

describe("isForceDisconnect", () => {
it("returns true when forceDisconnect is disconnect and to !== from", () => {
const msg: EndpointMessage = {
Expand Down
59 changes: 57 additions & 2 deletions src/components/ui/production-line/data-channel-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,30 @@ export type DominantSpeakerMessage = {

export type EndpointMessage = {
type: "EndpointMessage";
payload: { muteParticipant?: string; forceDisconnect?: string };
payload: {
muteParticipant?: string;
forceDisconnect?: string;
// Advisory global-mute state broadcast (#678). `muteState` is "mute" |
// "unmute" and `participantId` is the endpointId the state refers to. This
// is broadcast to ALL endpoints so every client can reflect the mute state
// in its participant list; it is distinct from `muteParticipant`, which is
// a targeted directive telling a single endpoint to mute its own input.
muteState?: string;
participantId?: string;
};
to: string;
from: string;
};

/**
* Result of parsing a global-mute-state broadcast (#678): which participant the
* state refers to (by endpointId) and whether they are now muted.
*/
export type MuteStateBroadcast = {
participantId: string;
muted: boolean;
};

export type ParsedDataChannelMessage =
| DominantSpeakerMessage
| EndpointMessage
Expand Down Expand Up @@ -55,7 +74,11 @@ export const parseDataChannelMessage = (
(("muteParticipant" in message.payload &&
typeof message.payload.muteParticipant === "string") ||
("forceDisconnect" in message.payload &&
typeof message.payload.forceDisconnect === "string"))
typeof message.payload.forceDisconnect === "string") ||
("muteState" in message.payload &&
typeof message.payload.muteState === "string" &&
"participantId" in message.payload &&
typeof message.payload.participantId === "string"))
) {
return message as EndpointMessage;
}
Expand All @@ -76,3 +99,35 @@ export const isRemoteMute = (msg: EndpointMessage): boolean =>
*/
export const isForceDisconnect = (msg: EndpointMessage): boolean =>
msg.payload.forceDisconnect === "disconnect" && msg.to !== msg.from;

/**
* Extract an advisory global-mute-state broadcast (#678) from an
* EndpointMessage, or `null` if this message is not a mute-state broadcast.
*
* This is the receive side of option (a) from issue #678: an admin broadcasts
* the mute state of a participant to every endpoint so each client can show the
* correct mute indication in its participant list. It carries NO enforcement —
* the model is intentionally advisory, matching the already-merged
* `muteParticipant` directive, which only asks the target to mute itself. A
* muted participant can still unmute locally; nothing here prevents that.
*
* Deferred follow-up (option (b), out of scope for #678): an
* authoritative/server-enforced mute that persists per-participant admin-mute
* state in the backend and prevents self-unmute. That requires a new product
* decision and is intentionally NOT implemented here.
*/
export const getMuteStateBroadcast = (
msg: EndpointMessage
): MuteStateBroadcast | null => {
if (
typeof msg.payload.muteState === "string" &&
typeof msg.payload.participantId === "string"
) {
return {
participantId: msg.payload.participantId,
muted: msg.payload.muteState === "mute",
};
}

return null;
};
26 changes: 26 additions & 0 deletions src/components/ui/production-line/production-line.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const ProductionLine = ({
dataChannel,
isRemotelyMuted,
isRemotelyDisconnected,
participantMuteState,
} = callState;
const { isActiveParticipant } = useActiveParticipant(
audioLevelAboveThreshold
Expand Down Expand Up @@ -634,6 +635,8 @@ export const ProductionLine = ({
});

const muteParticipant = () => {
// Targeted advisory directive (already merged behaviour): asks the single
// target endpoint to mute its own input.
const msg = JSON.stringify({
type: "EndpointMessage",
to: userId,
Expand All @@ -642,8 +645,30 @@ export const ProductionLine = ({
},
});

// Global mute-state broadcast (#678): tell every endpoint that this
// participant is now muted so each client reflects it in its participant
// list. An empty `to` broadcasts to all other endpoints on the SMB
// conference. Advisory only — not server-enforced (authoritative mute is
// deferred follow-up option (b), which would persist state in the backend
// and prevent self-unmute).
const broadcast = JSON.stringify({
type: "EndpointMessage",
to: "",
payload: {
muteState: "mute",
participantId: userId,
},
});

if (dataChannel && dataChannel.readyState === "open") {
dataChannel.send(msg);
dataChannel.send(broadcast);
// The broadcast reaches every OTHER endpoint; update the acting admin's
// own participant list locally so it reflects the change immediately.
dispatch({
type: "UPDATE_PARTICIPANT_MUTE_STATE",
payload: { callId: id, participantId: userId, muted: true },
});
setMuteError(false);
setConfirmModalOpen(false);
} else {
Expand Down Expand Up @@ -1066,6 +1091,7 @@ export const ProductionLine = ({
<UserList
sessionId={sessionId}
participants={line.participants}
participantMuteState={participantMuteState}
dominantSpeaker={dominantSpeaker}
audioLevelAboveThreshold={audioLevelAboveThreshold}
programOutputLine={line.programOutputLine}
Expand Down
32 changes: 32 additions & 0 deletions src/components/ui/production-line/use-rtc-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,38 @@ describe("useRtcConnection data channel message handling", () => {
});
});

it("dispatches UPDATE_PARTICIPANT_MUTE_STATE on a mute-state broadcast (#678)", () => {
renderHook(() => useRtcConnection(connectedOptions), { wrapper });

capturedMessageHandler?.({
data: JSON.stringify({
type: "EndpointMessage",
payload: { muteState: "mute", participantId: "ep-9" },
to: "",
from: "ep-admin",
}),
} as MessageEvent);

expect(mockDispatch).toHaveBeenCalledWith({
type: "UPDATE_PARTICIPANT_MUTE_STATE",
payload: {
callId: "test-call-id",
participantId: "ep-9",
muted: true,
},
});
// A broadcast must NOT flip the receiving client's own self-mute state.
expect(mockDispatch).not.toHaveBeenCalledWith(
expect.objectContaining({
payload: expect.objectContaining({
updates: expect.objectContaining({
isRemotelyMuted: expect.anything(),
}),
}),
})
);
});

it("dispatches isRemotelyDisconnected on a forceDisconnect EndpointMessage", () => {
renderHook(() => useRtcConnection(connectedOptions), { wrapper });

Expand Down
18 changes: 17 additions & 1 deletion src/components/ui/production-line/use-rtc-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
parseDataChannelMessage,
isRemoteMute,
isForceDisconnect,
getMuteStateBroadcast,
} from "./data-channel-parser.ts";
import { waitForIceGathering } from "./ice-gathering.ts";
import { startRtcStatInterval } from "./rtc-stat-interval.ts";
Expand Down Expand Up @@ -223,7 +224,22 @@ const establishConnection = ({
},
});
} else if (message.type === "EndpointMessage") {
if (isForceDisconnect(message)) {
const muteBroadcast = getMuteStateBroadcast(message);
if (muteBroadcast) {
// Advisory global-mute state broadcast (#678): an admin muted/unmuted
// this participant and broadcast the new state to all endpoints. Record
// it per-endpointId so this client's participant list reflects the mute
// state of every participant, not just itself. Advisory only — this is
// not server-enforced (authoritative mute is deferred option (b)).
dispatch({
type: "UPDATE_PARTICIPANT_MUTE_STATE",
payload: {
callId,
participantId: muteBroadcast.participantId,
muted: muteBroadcast.muted,
},
});
} else if (isForceDisconnect(message)) {
// A client with admin rights kicked this participant. Flag the call so
// production-line can tear down the RTCPeerConnection, leave the line
// and surface a "You were removed from the call" notification.
Expand Down
30 changes: 29 additions & 1 deletion src/components/ui/production-line/user-list.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from "@emotion/styled";
import { UserIcon, WhipIcon } from "../../../assets/icons/icon.tsx";
import { MicMuted, UserIcon, WhipIcon } from "../../../assets/icons/icon.tsx";
import { TParticipant } from "./types.ts";
import {
getStatusClass,
Expand Down Expand Up @@ -39,8 +39,26 @@ const IsTalkingIndicator = styled.div<TIsTalkingIndicator>`
: ""}
`;

// Advisory global-mute indicator (#678): shown next to a participant whose
// endpointId is flagged muted in the call's participantMuteState map. The mark
// is fed by mute-state broadcasts on the data channel and therefore reflects
// the same state on every client. Advisory only — it mirrors what an admin
// asked for and is not server-enforced.
const MutedIndicator = styled.div`
display: flex;
align-items: center;
margin-right: 1rem;
flex-shrink: 0;

svg {
width: 2rem;
fill: #f96c6c;
}
`;

type TUserListOptions = {
participants: TParticipant[];
participantMuteState?: Record<string, boolean>;
sessionId: string | null;
dominantSpeaker: string | null;
audioLevelAboveThreshold: boolean;
Expand All @@ -60,6 +78,7 @@ type TUserListOptions = {

export const UserList = ({
participants,
participantMuteState,
sessionId,
dominantSpeaker,
audioLevelAboveThreshold,
Expand Down Expand Up @@ -91,6 +110,7 @@ export const UserList = ({
{participants.map((p) => {
const isYou = p.sessionId === sessionId;
const truncatedUsername = truncateParticipantName(p.name);
const isMuted = participantMuteState?.[p.endpointId] ?? false;
return (
<UserWrapper key={p.sessionId} isYou={isYou}>
<User title={p.name}>
Expand All @@ -111,6 +131,14 @@ export const UserList = ({
{truncatedUsername} {p.isActive ? "" : "(inactive)"}
</UserName>
</User>
{isMuted && (
<MutedIndicator
title={`${p.name} is muted`}
aria-label={`${p.name} is muted`}
>
<MicMuted />
</MutedIndicator>
)}
<UserActionSlots
participant={p}
isYou={isYou}
Expand Down
Loading
Loading