Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stream video? #1089

Closed
rodolfoap opened this issue Apr 29, 2024 · 3 comments
Closed

How to stream video? #1089

rodolfoap opened this issue Apr 29, 2024 · 3 comments

Comments

@rodolfoap
Copy link

rodolfoap commented Apr 29, 2024

Using the webcam example, I can stream a single RTSP video from a server, to multiple clients in browsers:

async def offer(request):
    ...
    peer_connection = RTCPeerConnection()
    ...
    player = MediaPlayer("rtsp://myserver:8554/live.sdp")  <- ONE PLAYER FOR EACH PEER
    video = MediaRelay().subscribe(player.video)
    peer_connection.addTrack(video)
    ...

However, this has an issue: since my video has 1920x1080, it consumes a lot of memory (two connections: 440Mb RAM). This is clear, since each MediaPlayer makes an additional connection for each peer.

An obvious option would be to use a single MediaPlayer for all peers:

player = MediaPlayer("rtsp://myserver:8554/live.sdp") # <- PLAYER IS UNIQUE

async def offer(request):
    ...
    peer_connection = RTCPeerConnection()
    ...
    video = MediaRelay().subscribe(player.video)
    peer_connection.addTrack(video)
    ...

This seems fine (three connections: 300Mb). However, there is a big problem here: the application interleaves frames to browser clients (if there are two opened browsers, client 1 gets frames 1,3,5,7... and client 2 gets frames 2,4,6,8...)!

Using a single player, how can I stream all frames to all clients? Can someone please provide an example (or tell me how to override recv() so that each frame is delivered to all browsers)?

Thanks and keep it up!

@ZoroLH
Copy link

ZoroLH commented Apr 30, 2024

Same problem, and memory usage keep increasing when open multiple browser pages.

@ZoroLH
Copy link

ZoroLH commented May 2, 2024

Fixed by resize frame size from 19201080 to 640480. Looks like this is not MediaRelay's issue, but cpu's issue. Because is not running on GPU on my macbook for rtsp h.264 video. When I rendering multiple 1920*1080 video, my macbook is hot, cpu usage is closed to 100%. When I compress the video, all clients are rendering without loosing frame.
BTW, relay=MediaRelay(). should be global.

@rodolfoap
Copy link
Author

rodolfoap commented May 7, 2024

BTW, relay=MediaRelay(). should be global.

Great! that was the solution. Resolution had no impact in my case.

player = MediaPlayer("rtsp://myserver:8554/live.sdp") # <- PLAYER IS UNIQUE
relay = MediaRelay()
async def offer(request):
    ...
    peer_connection = RTCPeerConnection()
    ...
    video = relay.subscribe(player.video)
    peer_connection.addTrack(video)
    ...

Just FYI, if you want to see the frame interleaving, create a video with numbered frames. In two browsers, you will clearly see one playing only odds and the other, evens.

But now, I have a second problem: the same MEMORY LEAK you've observed:

Same problem, and memory usage keep increasing when open multiple browser pages.

Will open a new issue for that, this precise problem is solved. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants