I’ve used the simple AIORTC webcam example to make an WebRTC server from an RTSP flow which can be queried from any browser:
async def offer(request):
...
peer_connection = RTCPeerConnection()
peer_connections.add(peer_connection)
...
# HERE! One player is created for each WebRTC OFFER
player = MediaPlayer("rtsp://myserver:8554/live.sdp")
video = MediaRelay().subscribe(player.video)
peer_connection.addTrack(video)
...
The problem here is that I need multiple clients to connect to the server. When multiple (browser) clients are connected, there’s a huge memory consumption.
An evident solution to consume less memory is to create a single player for all browser clients:
# HERE! A single player is created for all WebRTC OFFERs
player = MediaPlayer("rtsp://myserver:8554/live.sdp")
async def offer(request):
...
peer_connection = RTCPeerConnection()
peer_connections.add(peer_connection)
...
# Will use the unique player
video = MediaRelay().subscribe(player.video)
peer_connection.addTrack(video)
...
This solution is great from the memory pov (80Mb for the application + 100Mb for each client). However, there is a HUGE problem: clients receive interleaved frames!
I imagine that each client calls the .recv()
method, so if there are two clients, client 1 gets frames 1,3,5… and client 2 gets frames 2,4,6…
My knowledge of webrtc/video is very limited, is there any simple solution for this interleaving, or should I overload the MediaPlayer so to overload the PlayerStream.recv() method… (seems very complex!)?