Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。
refactor(route): unify route candidate deadline ownership (#369)
AAA4c081f4f8866
diff --git a/docs/adr/0005-automatic-hybrid-media-routing.md b/docs/adr/0005-automatic-hybrid-media-routing.mdindex 9a908d02..44394b13 100644--- a/docs/adr/0005-automatic-hybrid-media-routing.md+++ b/docs/adr/0005-automatic-hybrid-media-routing.md@@ -147,6 +147,10 @@ or only Peer candidate remains until the unchanged total operation deadline;exact transport progress also retains it until media proof or that deadline.After SFU commits, each one-candidate direct continuation therefore uses itsfull background operation while working media stays authoritative.+The pending Viewer transport has no second initial-connection or transient-+disconnect deadline. It reports an actual Browser terminal failure immediately;+only after commit does the active Viewer edge take ownership of its existing+reconnect timers.If the Host is full and no publication exists, one bounded SFU-bootstrap intentowns the original waiting demand and a finite cursor of eligible Host-directdiff --git a/docs/product/routing-transport.md b/docs/product/routing-transport.mdindex 895f2f90..d9303ed8 100644--- a/docs/product/routing-transport.md+++ b/docs/product/routing-transport.md@@ -83,6 +83,9 @@ Transport-connected progress also retains a candidate through that deadline.Consequently a working SFU route can carry media while its one-candidate directconvergence uses the full background operation, and peer-only acquisition doesnot abandon its only possible route at the foreground boundary.+The pending Viewer transport reports an actual Browser failure but installs no+shorter initial or disconnected-state deadline. Viewer-owned reconnect timing+begins only after that exact candidate commits as the active route.When a newly committed Host-root Viewer exposes unused downstream capacity whileanother Host root has at least two direct children, the same background operation maydiff --git a/src/client/pages/ViewerPage.tsx b/src/client/pages/ViewerPage.tsxindex dcab782b..c94da89f 100644--- a/src/client/pages/ViewerPage.tsx+++ b/src/client/pages/ViewerPage.tsx@@ -1050,10 +1050,12 @@ export function ViewerPage({ roomId, viewerGrant }: ViewerPageProps) {return false;}const previousPeer = peerRef.current;- probe.peer.stopDecodedFrameProof();+ const activatedPeer = probe.peer;+ activatedPeer.stopDecodedFrameProof();pendingPeer = null;prepareParent(null);- peerRef.current = probe.peer;+ peerRef.current = activatedPeer;+ activatedPeer.activatePreparedRoute();previousPeer?.dispose();applyMediaAssignment({@@ -1466,7 +1468,10 @@ export function ViewerPage({ roomId, viewerGrant }: ViewerPageProps) {: true;},},- currentRoutePolicy.natPrediction,+ {+ natPredictionEnabled: currentRoutePolicy.natPrediction,+ recoveryOwner: "route",+ },);probe.peer = peer;return peer;@@ -1557,7 +1562,7 @@ export function ViewerPage({ roomId, viewerGrant }: ViewerPageProps) {return true;},},- currentRoutePolicy.natPrediction,+ { natPredictionEnabled: currentRoutePolicy.natPrediction },);peerRef.current = peer;return peer;diff --git a/src/client/webrtc/viewer-peer.ts b/src/client/webrtc/viewer-peer.tsindex dc2ae5aa..ec66a545 100644--- a/src/client/webrtc/viewer-peer.ts+++ b/src/client/webrtc/viewer-peer.ts@@ -43,6 +43,11 @@ interface ViewerPeerEvents {) => boolean;}+interface ViewerPeerOptions {+ natPredictionEnabled?: boolean;+ recoveryOwner?: "viewer" | "route";+}+export class ViewerPeer {private connection: RTCPeerConnection | null = null;private connectionId: string | null = null;@@ -67,13 +72,17 @@ export class ViewerPeer {private localIceCandidates: NatPredictionCandidateEmitter | null = null;private snapshot: PeerSnapshot | null = null;private descriptionTail: Promise<void> = Promise.resolve();+ private readonly natPredictionEnabled: boolean;+ private recoveryOwner: "viewer" | "route";constructor(iceConfig: PeerIceConfig,private readonly events: ViewerPeerEvents,- private readonly natPredictionEnabled = false,+ options: ViewerPeerOptions = {},) {this.currentIceConfig = iceConfig;+ this.natPredictionEnabled = options.natPredictionEnabled ?? false;+ this.recoveryOwner = options.recoveryOwner ?? "viewer";}async acceptSignal(@@ -124,6 +133,7 @@ export class ViewerPeer {requestRecovery(rebuild = false): boolean {if (+ this.recoveryOwner !== "viewer" ||this.recoveryTimer !== null ||!this.connection ||!this.connectionId ||@@ -177,6 +187,16 @@ export class ViewerPeer {this.stopDecodedFrameObserver = null;}+ activatePreparedRoute(): void {+ if (this.recoveryOwner === "viewer") {+ return;+ }+ this.recoveryOwner = "viewer";+ if (this.connection) {+ this.handleConnectionState(this.connection.connectionState);+ }+ }+dispose(): void {if (this.disposed) {return;@@ -384,6 +404,13 @@ export class ViewerPeer {this.resetAutomaticRecovery();return;}+ if (this.recoveryOwner === "route") {+ this.clearDisconnectTimer();+ if (state === "failed") {+ this.reportRecoveryExhausted();+ }+ return;+ }if (state === "failed") {this.clearDisconnectTimer();this.attemptAutomaticRecovery();@@ -434,7 +461,10 @@ export class ViewerPeer {connection: RTCPeerConnection,connectionId: string,): void {- if (!this.isCurrentConnection(connection, connectionId)) {+ if (+ this.recoveryOwner !== "viewer" ||+ !this.isCurrentConnection(connection, connectionId)+ ) {return;}this.clearInitialConnectionTimer();diff --git a/tests/viewer-peer-generation.test.ts b/tests/viewer-peer-generation.test.tsindex 84ad98c8..031ff02a 100644--- a/tests/viewer-peer-generation.test.ts+++ b/tests/viewer-peer-generation.test.ts@@ -188,7 +188,7 @@ function createPeer(onStream: () => undefined,onUpdate: (snapshot) => snapshots.push(snapshot),},- natPrediction,+ { natPredictionEnabled: natPrediction },);}@@ -424,6 +424,72 @@ describe("ViewerPeer connection generations", () => {expect(timeoutDelays.get(2)).toBe(3_000);});+ it("leaves a pending route candidate under the route deadline", async () => {+ const restartRequests: string[] = [];+ const exhausted: string[] = [];+ const peer = new ViewerPeer(+ { iceServers: [] },+ {+ sendSignal: () => true,+ sendRestartRequest: (_peerId, connectionId) => {+ restartRequests.push(connectionId);+ return true;+ },+ onStream: () => undefined,+ onUpdate: () => undefined,+ onRecoveryExhausted: (_peerId, connectionId) => {+ exhausted.push(connectionId);+ return true;+ },+ },+ { recoveryOwner: "route" },+ );++ await peer.acceptSignal("host", offer("route-candidate"));+ const connection = FakePeerConnection.instances[0]!;+ connection.connectionState = "disconnected";+ connection.dispatchEvent(new Event("connectionstatechange"));++ expect(timeoutDelays).toEqual(new Map());+ expect(restartRequests).toEqual([]);+ expect(exhausted).toEqual([]);++ connection.connectionState = "failed";+ connection.dispatchEvent(new Event("connectionstatechange"));+ expect(restartRequests).toEqual([]);+ expect(exhausted).toEqual(["route-candidate"]);+ });++ it("transfers recovery ownership when a prepared route fails during commit", async () => {+ const restartRequests: Array<{+ connectionId: string;+ rebuild: boolean;+ }> = [];+ const peer = new ViewerPeer(+ { iceServers: [] },+ {+ sendSignal: () => true,+ sendRestartRequest: (_peerId, connectionId, rebuild) => {+ restartRequests.push({ connectionId, rebuild });+ return true;+ },+ onStream: () => undefined,+ onUpdate: () => undefined,+ },+ { recoveryOwner: "route" },+ );++ await peer.acceptSignal("host", offer("committed-candidate"));+ const connection = FakePeerConnection.instances[0]!;+ connection.connectionState = "failed";+ peer.activatePreparedRoute();++ expect(restartRequests).toEqual([+ { connectionId: "committed-candidate", rebuild: false },+ ]);+ expect([...timeoutDelays.values()]).toEqual([3_000]);+ });+it("cancels the initial deadline after connecting", async () => {const restartRequests: string[] = [];const peer = new ViewerPeer(