Preserve media presentation across route updates (#289)
This commit is contained in:
1 parent
b8963c65d7
commit
267e07340b
7 files changed
+81
-99
No files matched your search
@@ -286,8 +286,7 @@ function sameViewerQualityEvidenceIdentity(
|
||||
return (
|
||||
previous.viewerPeerId === next.viewerPeerId &&
|
||||
qualityEvidenceUpstreamMatches(previous, next.upstream) &&
|
||||
previous.guard.connectionId === next.guard.connectionId &&
|
||||
previous.guard.routeRevision === next.guard.routeRevision
|
||||
previous.guard.connectionId === next.guard.connectionId
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1417,7 +1417,6 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {
|
||||
message.revision !== activeRouteRevisionRef.current
|
||||
) {
|
||||
activeRouteRevisionRef.current = message.revision;
|
||||
clearAllViewerQualityEvidence();
|
||||
}
|
||||
showHostSfuQualityWarning(route, generation);
|
||||
}
|
||||
|
||||
@@ -1665,30 +1665,6 @@ export function ViewerPage({ roomId, viewerGrant }: ViewerPageProps) {
|
||||
);
|
||||
}, [frameProofEpoch, presentationState.connection, remoteMedia]);
|
||||
|
||||
useEffect(() => {
|
||||
if (presentation.overlay === "none") {
|
||||
return;
|
||||
}
|
||||
const video = videoRef.current as
|
||||
| (HTMLVideoElement & {
|
||||
webkitDisplayingFullscreen?: boolean;
|
||||
webkitExitFullscreen?: () => void;
|
||||
})
|
||||
| null;
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
if (document.fullscreenElement === video) {
|
||||
void document.exitFullscreen().catch(() => undefined);
|
||||
return;
|
||||
}
|
||||
if (video.webkitDisplayingFullscreen) {
|
||||
try {
|
||||
video.webkitExitFullscreen?.();
|
||||
} catch {}
|
||||
}
|
||||
}, [presentation.overlay]);
|
||||
|
||||
function retryConnection(): void {
|
||||
if (reconnectRoute === "sfu") {
|
||||
viewerSfuRouteRef.current?.reconnectActive();
|
||||
|
||||
@@ -112,6 +112,13 @@ export type ActiveViewerMediaEdge =
|
||||
upstream: { kind: "sfu" };
|
||||
};
|
||||
|
||||
export interface ActiveSfuViewerMediaState {
|
||||
viewerPeerId: string;
|
||||
viewerSessionId: string;
|
||||
revision: number;
|
||||
sfuPublicationGeneration: string;
|
||||
}
|
||||
|
||||
export interface SfuFallbackOptions {
|
||||
url: string;
|
||||
tokenIssuer: SfuTokenIssuer;
|
||||
@@ -131,14 +138,9 @@ export interface HybridMediaRouterOptions {
|
||||
setConnectionId: (roomId: string, viewerPeerId: string, connectionId: string) => void;
|
||||
deleteConnectionId: (roomId: string, viewerPeerId: string) => void;
|
||||
getShareGeneration: (roomId: string) => string | undefined;
|
||||
onActiveRouteChanged?: (roomId: string) => void;
|
||||
onViewerMediaState?: (
|
||||
onViewerMediaSnapshot?: (
|
||||
roomId: string,
|
||||
viewerPeerId: string,
|
||||
viewerSessionId: string,
|
||||
revision: number,
|
||||
sfuPublicationGeneration: string,
|
||||
ready: boolean,
|
||||
viewers: readonly ActiveSfuViewerMediaState[],
|
||||
) => void;
|
||||
now?: () => number;
|
||||
}
|
||||
@@ -689,7 +691,7 @@ export class HybridMediaRouter {
|
||||
...(edge.kind === "peer" ? { parentSessionId: edge.parentSessionId } : {}),
|
||||
}, this.now());
|
||||
if (invalidated && edge.usable && snapshot.paused) {
|
||||
this.options.onActiveRouteChanged?.(participant.roomId);
|
||||
this.publishViewerMediaState(participant.roomId, controller.snapshot());
|
||||
}
|
||||
this.requestPump(participant.roomId);
|
||||
}
|
||||
@@ -1349,7 +1351,14 @@ export class HybridMediaRouter {
|
||||
}
|
||||
}
|
||||
}
|
||||
this.options.onActiveRouteChanged?.(roomId);
|
||||
this.publishViewerMediaState(roomId, snapshot);
|
||||
}
|
||||
|
||||
private publishViewerMediaState(
|
||||
roomId: string,
|
||||
snapshot: RouteSnapshot<RouteResource>,
|
||||
): void {
|
||||
const viewers: ActiveSfuViewerMediaState[] = [];
|
||||
for (const viewer of this.options.roomStore.getConnectedViewers(roomId)) {
|
||||
const edge = snapshot.upstreamByViewer.get(viewer.peerId);
|
||||
if (
|
||||
@@ -1357,16 +1366,15 @@ export class HybridMediaRouter {
|
||||
edge.physicalActive &&
|
||||
this.pathIsPhysical(roomId, snapshot, viewer.peerId)
|
||||
) {
|
||||
this.options.onViewerMediaState?.(
|
||||
roomId,
|
||||
viewer.peerId,
|
||||
viewer.sessionId,
|
||||
snapshot.revision,
|
||||
edge.publicationGeneration,
|
||||
true,
|
||||
);
|
||||
viewers.push({
|
||||
viewerPeerId: viewer.peerId,
|
||||
viewerSessionId: viewer.sessionId,
|
||||
revision: snapshot.revision,
|
||||
sfuPublicationGeneration: edge.publicationGeneration,
|
||||
});
|
||||
}
|
||||
}
|
||||
this.options.onViewerMediaSnapshot?.(roomId, viewers);
|
||||
}
|
||||
|
||||
private pathIsPhysical(
|
||||
|
||||
+15
-50
@@ -34,6 +34,7 @@ import {
|
||||
} from "./room-store.js";
|
||||
import {
|
||||
HybridMediaRouter,
|
||||
type ActiveSfuViewerMediaState,
|
||||
type SfuFallbackOptions,
|
||||
} from "./hybrid-media-router.js";
|
||||
import { createIceConfig, type IceConfigOptions } from "./ice.js";
|
||||
@@ -179,26 +180,8 @@ export class SignalingServer {
|
||||
deleteConnectionId: (roomId, viewerPeerId) =>
|
||||
this.deleteViewerConnectionId(roomId, viewerPeerId),
|
||||
getShareGeneration: (roomId) => this.shareGenerationsByRoom.get(roomId),
|
||||
onActiveRouteChanged: (roomId) => {
|
||||
this.viewerMediaReadyByRoom.delete(roomId);
|
||||
this.sendViewerPresence(roomId);
|
||||
},
|
||||
onViewerMediaState: (
|
||||
roomId,
|
||||
viewerPeerId,
|
||||
viewerSessionId,
|
||||
revision,
|
||||
sfuPublicationGeneration,
|
||||
ready,
|
||||
) =>
|
||||
this.updateViewerMediaState(
|
||||
roomId,
|
||||
viewerPeerId,
|
||||
viewerSessionId,
|
||||
revision,
|
||||
sfuPublicationGeneration,
|
||||
ready,
|
||||
),
|
||||
onViewerMediaSnapshot: (roomId, viewers) =>
|
||||
this.replaceViewerMediaState(roomId, viewers),
|
||||
now: this.now,
|
||||
});
|
||||
}
|
||||
@@ -1852,40 +1835,22 @@ export class SignalingServer {
|
||||
recipients.forEach((socket) => this.send(socket, message));
|
||||
}
|
||||
|
||||
private updateViewerMediaState(
|
||||
private replaceViewerMediaState(
|
||||
roomId: string,
|
||||
viewerPeerId: string,
|
||||
viewerSessionId: string,
|
||||
revision: number,
|
||||
sfuPublicationGeneration: string,
|
||||
ready: boolean,
|
||||
viewers: readonly ActiveSfuViewerMediaState[],
|
||||
): void {
|
||||
let readyByViewer = this.viewerMediaReadyByRoom.get(roomId);
|
||||
if (ready) {
|
||||
readyByViewer ??= new Map<string, ViewerMediaReadyState>();
|
||||
readyByViewer.set(viewerPeerId, {
|
||||
sessionId: viewerSessionId,
|
||||
revision,
|
||||
sfuPublicationGeneration,
|
||||
});
|
||||
if (viewers.length > 0) {
|
||||
const readyByViewer = new Map<string, ViewerMediaReadyState>();
|
||||
for (const viewer of viewers) {
|
||||
readyByViewer.set(viewer.viewerPeerId, {
|
||||
sessionId: viewer.viewerSessionId,
|
||||
revision: viewer.revision,
|
||||
sfuPublicationGeneration: viewer.sfuPublicationGeneration,
|
||||
});
|
||||
}
|
||||
this.viewerMediaReadyByRoom.set(roomId, readyByViewer);
|
||||
} else {
|
||||
if (!readyByViewer) {
|
||||
return;
|
||||
}
|
||||
const current = readyByViewer.get(viewerPeerId);
|
||||
if (
|
||||
!current ||
|
||||
current.sessionId !== viewerSessionId ||
|
||||
current.revision !== revision ||
|
||||
current.sfuPublicationGeneration !== sfuPublicationGeneration
|
||||
) {
|
||||
return;
|
||||
}
|
||||
readyByViewer.delete(viewerPeerId);
|
||||
if (readyByViewer.size === 0) {
|
||||
this.viewerMediaReadyByRoom.delete(roomId);
|
||||
}
|
||||
this.viewerMediaReadyByRoom.delete(roomId);
|
||||
}
|
||||
this.sendViewerPresence(roomId);
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ function harness(
|
||||
})
|
||||
: undefined;
|
||||
const roomControl = withSfu ? new FakeSfuRoomControl() : undefined;
|
||||
const onActiveRouteChanged = vi.fn();
|
||||
const onViewerMediaSnapshot = vi.fn();
|
||||
let nextTokenIssueError: Error | null = null;
|
||||
const router = new HybridMediaRouter({
|
||||
roomStore: store,
|
||||
@@ -148,14 +148,14 @@ function harness(
|
||||
connections.delete(`${roomId}:${viewerPeerId}`);
|
||||
},
|
||||
getShareGeneration: () => SHARE_GENERATION,
|
||||
onActiveRouteChanged,
|
||||
onViewerMediaSnapshot,
|
||||
});
|
||||
return {
|
||||
store,
|
||||
sent,
|
||||
admission,
|
||||
roomControl,
|
||||
onActiveRouteChanged,
|
||||
onViewerMediaSnapshot,
|
||||
failNextTokenIssue() {
|
||||
nextTokenIssueError = new Error("token issue failed");
|
||||
},
|
||||
@@ -204,6 +204,41 @@ async function establishSfuRoom(
|
||||
}
|
||||
|
||||
describe("HybridMediaRouter v9 runtime", () => {
|
||||
it("reports active SFU viewers as one authoritative snapshot", async () => {
|
||||
const { store, sent, router, onViewerMediaSnapshot } = harness(1, true);
|
||||
try {
|
||||
const room = await store.createRoom();
|
||||
const { first, second } = await establishSfuRoom(
|
||||
store,
|
||||
sent,
|
||||
router,
|
||||
room,
|
||||
);
|
||||
|
||||
await vi.waitFor(() => {
|
||||
const [reportedRoomId, viewers] =
|
||||
onViewerMediaSnapshot.mock.calls.at(-1) ?? [];
|
||||
expect(reportedRoomId).toBe(room.roomId);
|
||||
expect(
|
||||
new Set(
|
||||
(viewers ?? []).map(
|
||||
(viewer: { viewerPeerId: string }) => viewer.viewerPeerId,
|
||||
),
|
||||
),
|
||||
).toEqual(new Set([first.peerId, second.peerId]));
|
||||
expect(
|
||||
new Set(
|
||||
(viewers ?? []).map(
|
||||
(viewer: { revision: number }) => viewer.revision,
|
||||
),
|
||||
).size,
|
||||
).toBe(1);
|
||||
});
|
||||
} finally {
|
||||
await router.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("wakes at the derived direct boundary and prepares SFU automatically", async () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(0);
|
||||
|
||||
@@ -497,7 +497,7 @@ describe("viewer quality evidence", () => {
|
||||
).toBe("video/H264");
|
||||
});
|
||||
|
||||
it("resets retained fields on identity or non-monotonic sequence changes", () => {
|
||||
it("retains fields across room revisions and resets changed media identities", () => {
|
||||
const first = presentViewerQualityEvidence(
|
||||
null,
|
||||
serverEvidence({ sequence: 9 }),
|
||||
@@ -523,7 +523,7 @@ describe("viewer quality evidence", () => {
|
||||
}),
|
||||
2_000,
|
||||
);
|
||||
expect(changedRoute.evidence.metrics.codec).toBeNull();
|
||||
expect(changedRoute.evidence.metrics.codec).toBe("video/H264");
|
||||
|
||||
const changedConnection = presentViewerQualityEvidence(
|
||||
first,
|
||||
|
||||
Reference in new issue
Block a user