Skip to content

Commit a322cd6

Browse files
committed
docs: fix collision-prone streamSwarmIdBuilder example
The documented example keyed the stream swarm ID on resolution only (`height ?? 0`). ABR ladders routinely carry several bitrates at the same resolution, which share a height but have distinct identityHash — so the second registration hit the collision guard and threw inside the player's manifest handler, killing P2P for a config copied verbatim from the docs. Add bitrate to the example and spell out the uniqueness precondition, with identityHash (reproducible server-side via computeStreamIdentityHash) as the bulletproof fallback.
1 parent 420d798 commit a322cd6

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

api_documentation.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,13 @@ For full control, configure a custom `streamSwarmIdBuilder` on the client and ap
661661
const config = {
662662
swarmId: videoUuid,
663663
streamSwarmIdBuilder: ({ swarmId, streamType, properties }) =>
664-
`my-app-${swarmId}-${streamType}-${properties.height ?? 0}`,
664+
`my-app-${swarmId}-${streamType}-${properties.height ?? 0}-${properties.bitrate ?? 0}`,
665665
};
666666
667667
// Server (per quality, at transcode time)
668-
const infoHash = computeInfoHash(`my-app-${videoUuid}-main-${height}`);
668+
const infoHash = computeInfoHash(
669+
`my-app-${videoUuid}-main-${height}-${bitrate}`,
670+
);
669671
```
670672
671-
The stream swarm ID must be deterministic and identical across all peers of a swarm, and distinct streams must produce distinct IDs. It cannot be changed at runtime. Clients can also observe each registered stream's computed identity through the `onStreamAdded` core event.
673+
The stream swarm ID must be deterministic and identical across all peers of a swarm, and **every distinct stream must map to a distinct ID**. Include enough properties to guarantee that: resolution alone collides on ladders with several bitrates at the same resolution, so the example above adds `bitrate`. If your ladder can have several renditions sharing those fields (e.g. different codecs at the same resolution and bitrate), add the distinguishing property too, or incorporate the stream's `identityHash` (reproduce it server-side with `computeStreamIdentityHash(properties)`). Registering two different streams with the same ID throws. The builder cannot be changed at runtime. Clients can also observe each registered stream's computed identity through the `onStreamAdded` core event.

packages/p2p-media-loader-core/src/types.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,13 @@ export type StreamConfig = {
486486
* on a private tracker.
487487
*
488488
* The function must be deterministic and identical across all peers of the
489-
* swarm: peers whose streams resolve to the same infohash exchange segments
490-
* with each other, so distinct streams must produce distinct IDs.
489+
* swarm, and every distinct stream must map to a distinct ID: peers whose
490+
* streams resolve to the same infohash exchange segments with each other, and
491+
* registering two different streams with the same ID throws. Include enough
492+
* properties to guarantee uniqueness — resolution alone collides on ladders
493+
* with several bitrates at the same resolution. If in doubt, incorporate the
494+
* `identityHash` from the context (reproduce it server-side with
495+
* `computeStreamIdentityHash`).
491496
*
492497
* Called once per stream at registration. This property cannot be changed
493498
* at runtime.
@@ -503,8 +508,9 @@ export type StreamConfig = {
503508
*
504509
* @example
505510
* ```typescript
511+
* // Include bitrate: resolution alone collides on multi-bitrate ladders.
506512
* streamSwarmIdBuilder: ({ swarmId, streamType, properties }) =>
507-
* `my-app-${swarmId}-${streamType}-${properties.height ?? 0}`,
513+
* `my-app-${swarmId}-${streamType}-${properties.height ?? 0}-${properties.bitrate ?? 0}`,
508514
* ```
509515
*/
510516
streamSwarmIdBuilder?: (

0 commit comments

Comments
 (0)