r/WebRTC • • Apr 24 '21

How to build a simple SFU server?

i know there are a lot of free and opensource SFU servers out there, but the main reason I want to build a simple one is just to understand how it works, this is just out of curiousity as an engineer :) I am wondering how this fit together with webrtc since by design, webrtc are peer to peer, does each peer/users establish a peer to peer connection with an SFU server? how do the server route connections of all users? etc.

is there any resource,books, courses, that describes the technical details how to do this?

i just want satisfy my curiousity as an engineer (thats my nature. i even built my own game engine haha), and i dont want to start with reading the open sourced project like janus as they are already convulated and have lots of source code already involvedd with a ton of functionalities

(maybe if i can find a simple SFU project to study will do)

22 Upvotes

7 comments sorted by

4

u/tiger2380 Apr 24 '21

I just built one this month: https://github.com/tiger2380/simple_sfu/ that is called "simple sfu".

The entire concept is to have everyone talks to a single server. Users send their media stream to that single server, the server stores their media stream into the memory. When a new user joins that session, instead of all existing users resend their media stream, the server grabs the existed media streams from memory and sends them to the new user.

And of course, it is much more complex than this. To write your own SFU, you have to use C++ or GO. Sorry, I don't have any recommended books, courses or other resources to help you out.

3

u/Sean-Der Apr 25 '21

If you are ok with media only just do SDP, ICE, DTLS and SRTP. I would recommend going with C/C++, Java, Go, Typescript, C# or Rust. All these languages have the libraries you need today.

Think of your SFU as just 'RTP packet fanout' for your MVP. It is way more complicated then this in a production application. But for your MVP this is enough. These are the high level programming tasks.

  • Create a 'PeerConnection' on your server

    • It consumes and distributes RTP packets.

    • Each 'room' will be an array of 'PeerConnections' in your server.

  • Implement the PeerConnection

    • Generate a self-signed DTLS certificate

    • Start a ICE-lite listener

    • Generate a Offer that contains those details and two media sections

    • Respond to STUN packets from remote ICE Agent

    • Do DTLS Handshake

    • Extract keying material that is used to encrypt/decrypt S(RTP) packets

  • Distribute your RTP packets

    • On each incoming packets decrypt

    • Then distribute to each 'PeerConnection'

    • You will encrypt via SRTP

    • Then send via ICE-lite socket

I glossed over lots of details. I think this is enough info to go learn. Check out the book https://webrtcforthecurious.com/ it is an Open Source book that teaches the protocols and might be helpful.

1

u/youPersonalSideKik Apr 12 '25

Just out of curiosity, is the added complexity in any way related to concepts regarding SFUs or is it just stuff you have to deal with when the code is concerned with a growing user base ?

1

u/Sean-Der Apr 14 '25

It is going to be SFU specific.

You will have to implement Congestion Control. How do you effectively distribute video for multiple users (and each feed has multiple quality levels.)

How do you effectively handle load?

How do you handle updating/restarts. If you have long running sessions.

It's an interesting topic. https://pion.ly/discord and I would love to chat about it!

1

u/youPersonalSideKik Apr 14 '25

I am literally in the process of integrating an SFU in my virtual browser application and I had been struggling with writing my own RTP packets as a learning experience, I will would love to have a chat regarding the same 👌🏾

1

u/chapelierfou Apr 24 '21

libdatachannel has a simple SFU server example in C++ you might want to look into:

https://github.com/paullouisageneau/libdatachannel/tree/master/examples