Skip to content

Scalable Viewer Distribution with PipeTransport #7

Description

@Harxhit

Goal

Implement scalable live-stream viewer distribution using MediaSoup PipeTransport and worker-aware router selection.

Current Architecture

  • Single Worker
  • Single Router
  • Broadcaster produces audio/video
  • Viewers consume directly from same Router

Target Architecture

Main Broadcaster
       │
   Router A
(Ingress Router)
       │
 ┌─────┼─────┐
 │     │     │
 ▼     ▼     ▼
Router B Router C Router D
  • Router A hosts broadcasters/co-hosts.
  • Viewer routers (B/C/D) host consumers only.
  • Producers are piped from Router A to viewer routers.
  • Viewers are assigned based on worker load.

Tasks

Worker Load Tracking

  • Track worker CPU usage.
  • Track router count per worker.
  • Track transport count per worker.
  • Track consumer count per worker.
  • Expose worker metrics for debugging.

Router Selection

  • Implement least-loaded worker selection.
  • Create viewer router on selected worker if needed.
  • Add router assignment strategy abstraction.

PipeTransport Infrastructure

  • Research router.pipeToRouter().
  • Understand PipeProducer lifecycle.
  • Understand PipeConsumer lifecycle.
  • Handle router close events.
  • Handle worker crash scenarios.

Live Session Registry

  • Store ingress router ID.
  • Store broadcaster producer IDs.
  • Store co-host producer IDs.
  • Track piped routers.
  • Prevent duplicate piping.

Example:

{
  ingressRouterId,
  producerIds,
  pipedRouters
}

Viewer Join Flow

  • Select least-loaded worker.
  • Resolve target router.
  • Pipe producers if router is not already piped.
  • Create consumer transport.
  • Create consumers from PipeProducers.

Producer Synchronization

  • When broadcaster starts video, pipe to all viewer routers.
  • When broadcaster starts audio, pipe to all viewer routers.
  • When co-host joins, pipe new producers.
  • When producer closes, cleanup PipeProducers.

Metrics & Logging

  • Log router creation.
  • Log pipe creation.
  • Log worker load.
  • Log viewer router assignment.
  • Log active viewers per router.
  • Log active PipeProducers.

Future Investigation

  • Multi-worker architecture.
  • Multi-router architecture.
  • Cross-server scaling.
  • Geo-distributed viewer routing.
  • Autoscaling based on worker load.

Docs to refer

https://mediasoup.org/documentation/v3/mediasoup/api/?utm_source=chatgpt.com#RouterOptions

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    futurePlanned for future

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions