Skip to content

v4.0.0

Latest

Choose a tag to compare

@mrlika mrlika released this 19 Aug 11:30
· 2 commits to main since this release
9ad4979

Summary of Major Updates

  1. Core-Owned Stream Identity (breaking): Player integrations now pass raw manifest properties (bitrate, codecs, width, height, ...) to the core, which computes each stream's full identity — identityHash (replaces Stream.index), swarmId, streamSwarmId, infoHash — once at registration and freezes it on the Stream object. Fully wire-compatible with v3: the default derivation is bit-identical (enforced by golden-vector tests), so default-config v4 peers keep sharing swarms with v3 peers.
  2. Predictable Swarm Infohashes: The new StreamConfig.streamSwarmIdBuilder callback and the Node-safe p2p-media-loader-core/server subpath let a server reproduce the exact infohash every client announces — e.g., to allowlist swarms on a private tracker.
  3. Robust, Observable Stream Registration: Core.addStreamIfNoneExists never throws; registration failures (including streamSwarmIdBuilder collisions) are reported via the new onStreamRegistrationError event. The swarmId-only configuration (no manifest URL) now works end to end.
  4. Public API Sweep — No Internals Exposed: Every core output is a detached snapshot. StreamWithSegments/SegmentWithStream are internal; new Core.getStreams() and Core.getStreamSegmentRuntimeIds() cover enumeration and manifest diffing.
  5. Consistent Event Payloads: Segment, peer, and tracker event payloads compose from shared base types; every peer event now reports the trackerUrl the peer was discovered from; the deprecated SegmentLoadDetails.segmentUrl is removed.
  6. Compatibility & Housekeeping: hls.js updated to ^1.7.0 (runtime-compatible down to hls.js 1.3, verified by source inspection); wss://tracker.webtorrent.dev added to default trackers; a latent legacy-WebView crash in manifest URL handling fixed.

Migrating from v3? See the migration guide.


Detailed Changes

1. Core-Owned Stream Identity

  • Registration API: Core.addStreamIfNoneExists takes { runtimeId, type, properties } (StreamRegistration); the identity fields are computed by the core:
    // v3
    core.addStreamIfNoneExists({
      runtimeId: url,
      type: "main",
      index: generateStreamShortId({ bitrate, codecs, width, height }),
    });
    
    // v4
    core.addStreamIfNoneExists({
      runtimeId: url,
      type: "main",
      properties: { bitrate, codecs, width, height },
    });
  • Stream type: Stream.index is replaced by Stream.identityHash; streams also carry properties (frozen at registration), swarmId, streamSwarmId, and infoHash — all computed once and immutable.
  • Cross-player parity: dedicated stream-properties extractor modules in the hls.js and Shaka integrations, pinned by parity test suites asserting both players derive the same identityHash for equivalent manifest metadata.
  • Wire compatibility: golden-vector tests freeze the v3 outputs of generateStreamShortId/getStreamSwarmId/getStreamHash and assert the new computeStreamIdentityHash/computeStreamSwarmId/computeInfoHash produce the same literals. No infohash changes unless a custom streamSwarmIdBuilder is configured.

2. Predictable Swarm Infohashes (Server-Side)

  • StreamConfig.streamSwarmIdBuilder: builds a custom stream swarm ID per stream from a rich context (swarmId, streamType, raw properties, identityHash, defaultStreamSwarmId, peerProtocolVersion). The returned string is hashed into the announced infohash, so it fully defines which swarm the stream's peers join. Validated at registration: must be a non-empty string, and two different stream identities may never collide onto one ID (the guard compares the full (swarmId, type, identityHash) tuple).
  • p2p-media-loader-core/server subpath (Node.js 16+, ESM — CommonJS projects use dynamic import()): exports computeStreamIdentityHash, computeStreamSwarmId, buildStreamSwarmId, computeInfoHash, and PEER_PROTOCOL_VERSION so a server can reproduce the exact client-side derivation — with or without a custom builder. See "Predicting swarm infohashes on a server" in the API documentation.
  • swarmId and streamSwarmIdBuilder are static-only: identity is derived once at registration, so dynamic configuration updates strip them instead of silently desynchronizing announced swarms.

3. Robust, Observable Stream Registration

  • addStreamIfNoneExists never throws: a stream that fails to register (unresolvable swarm ID, invalid or colliding builder output) stays unknown to the core — its segments play through the player's default loader without P2P, other streams are unaffected — and the failure is dispatched via the new onStreamRegistrationError event. A misconfigured streamSwarmIdBuilder is now observable in production instead of only in debug logs, and custom integrations can no longer crash their player's manifest parsing on a registration error.
  • swarmId-only configuration: vestigial guards requiring setManifestResponseUrl() after registration were removed from loadSegment/isSegmentLoadable, so new Core({ swarmId }) works end to end as documented.
  • Manifest URL handling (hls.js): the engine now uses hls.js's own resolved MANIFEST_LOADED URL (post-redirect for any loader honoring the response.url contract; verified identical in hls.js 1.3.5, 1.6.16, and 1.7.0). This also fixes a latent crash on legacy WebViews without xhr.responseURL.

4. Public API Sweep

  • New Core.getStreams(): lists all registered streams with their computed identities — e.g., every announced infohash — at any time, not just at registration.
  • New Core.getStreamSegmentRuntimeIds(streamRuntimeId): a snapshot ReadonlySet of the segment runtime IDs registered for a stream, in registration order — the manifest-diffing workflow that previously required reading the segments map.
  • Detached snapshots everywhere: getStream()/getStreams() results and the onStreamAdded payload are snapshots without the internal segments registry; stream properties are frozen. No core output shares live internal state, so consumers can neither corrupt the registry nor retain the per-stream segment graph in memory.
  • StreamWithSegments and SegmentWithStream are internal types now — Core.getStream() returns the plain stream.
  • TypedError is documented public API: the base class of all library errors and warnings; err instanceof TypedError catches any of them, with the machine-readable type discriminator.

5. Consistent Event Payloads

  • Shared base types: segment events extend SegmentEventDetails, peer events extend PeerDetails, tracker events extend the new TrackerEventDetails — adding a correlation field to an event family is now a one-line change.
  • PeerDetails.trackerUrl: onPeerConnect, onPeerClose, onPeerError, onPeerWarning, and onPeerConnectError all report which tracker the peer was discovered from.
  • New events: onStreamAdded (fired once per registered stream with its computed identity, including infoHash) and onStreamRegistrationError (see above).
  • Removed: the deprecated SegmentLoadDetails.segmentUrl — use segment.url.
  • ByteRange.start/end are readonly; SegmentResponse.data documents that it may alias the P2P-upload cache buffer (copy with data.slice(0) before transferring to a worker).

6. Compatibility & Housekeeping

  • hls.js ^1.7.0: adapted to the newly typed NullableNetworkDetails loader callbacks (null instead of undefined/{} for P2P-served fragments — hls.js's own convention). Verified runtime-compatible with hls.js 1.3.x and 1.6.x by source inspection.
  • Default trackers: added wss://tracker.webtorrent.dev.
  • Renamed: PartialShakaEngineConfig → PartialShakaP2PEngineConfig (Shaka package, matches sibling type names); the SegmentStorage interface's streamId parameter is renamed to streamSwarmId (the passed value is unchanged — custom storage implementations keep working).
  • Docs: new migration guide (linked from the README), a "Predicting swarm infohashes on a server" section in the API documentation, and API docs for v4.0.

Breaking Changes

v3 v4
Stream.index Stream.identityHash
generateStreamShortId(props) computeStreamIdentityHash(properties)
GenerateStreamShortIdProps StreamProperties
StreamWithSegments, SegmentWithStream removed — internal; use Core.getStreamSegmentRuntimeIds()
SegmentLoadDetails.segmentUrl (deprecated) removed — use segment.url
PartialShakaEngineConfig (shaka package) PartialShakaP2PEngineConfig
addStreamIfNoneExists throws on failure reports via onStreamRegistrationError, never throws
swarmId changeable via dynamic config static-only (stripped from dynamic updates)

Despite the API changes, the wire format is unchanged: v4 clients with default configuration join the same swarms as v3 clients.

Full Changelog: v3.0.1...v4.0.0