I'm starting off with WebRTC and this is largely a design question. Looking for the pros and cons of two competing approaches.
Context
I'm creating a media server that will stream video to multiple clients. Each client can request one or more video streams to view in a browser page, then close those and request others, and so on. A single client may have a persistent connection for hours or days at a time, toggling different streams on and off at will.
Question
How do I manage the peer connections with clients?
Considered Approaches
- Each (client, video stream) pair gets its own peer connection. So if a client is viewing 5 video streams, that webpage will have 5 peer connections. Each peer connection has a single track. This involves creating / destroying peer connections each time the client takes an action to view / close a video stream.
- Each (client) gets a single peer connection. Based on client actions, we either add tracks or remove tracks from the peer connection. The single connection lives on as long as the client is on our webpage.
On the surface, Option 2 seems less "wasteful" in that there's a single connection, but reading up on WebRTC signaling, the act of adding / removing tracks triggers a new round of offer / answer, so I'm not sure its actually more efficient. Moreover, it seems like it would be harder to manage and implement since its more stateful, e.g., to make sure tracks are properly cleaned-up when no longer used.
Thanks.