Important
This library still in very early development. The API might change in the future. We use it in production for inLive Hub project. We're a small team, so we can't guarantee that we can fix all issues. But we will try our best to fix it especially when it's related with our product. If you have any questions or find any issues, please write it to this repo Github issue. We will appreciate it.
This Golang package is an SFU library based on Pion WebRTC. It is designed to be used in the inLive Hub project, but it can also be used in other projects. The goal is to have a Golang SFU library that is portable but easy to extend.
This is a Go module, so you can install it by running this command in your app directory:
go get github.com/inlivedev/sfu
- Group call
- Screen sharing
- Perfect negotiation
- Data channel - note: only room channel currenty available
- Simulcast
- Scalable Video Coding (SVC)
- Voice activity detection
- Room statistic
This SFU package has four components:
- Client, a WebRTC client as a relay client in the SFU.
- SFU, the SFU server.
- Room, a signaling controller between SFU and clients.
- Room Manager, a controller to manage multiple rooms
The one that you will interact with will be the room and the room manager. A server will at least have a single room manager. Before being able to run a group video call, you need to create a room with the room manager, then use that room to add clients to it. You can use any protocol as your signaling controller.
SFU <---> Room <---> REST/WebSocket/gRPC <---> browser/app
|
|
\/
Clients
See the example folder to see how to write a group video call app with this SFU package. The following section will explain in detail how to use this package.
On the first connection to SFU, a client will do a WebRTC negotiation, exchanging Session Description Protocol(SDP) and ice candidates. The client will send an offer SDP to initiate the negotiation. The SFU will respond with an answer SDP. Then both will exchange the ice candidates.
The steps will be like this to initiate the negotiation with the client.
- Add a client first to the SFU by calling AddClient(clientID, sfu.DefaultClientOptions()) on the current room instance. The
clientID
must be a unique string. The client options can be customized, but the default should be enough. - Once added, now we can access the relay client from the SFU through
currentRoom.SFU.GetClient(clientID)
. The relay client is a WebRTC client on the SFU side that will be used to negotiate with the client. - To start the negotiation you can pass the offer SDP from the client to client.Negotiate(offer) method. The method will return an answer SDP that you need to pass back to the client. As part of the negotiation, you need to pass the ice candidates from the client to client.AddICECandidate(candidate) method.
- You also need to pass the ice candidates from the relay client to the client. You can get the ice candidates from the relay client by listening to the
client.OnICECandidate
event. The event will be triggered when the relay client has a new ice candidate. You only need to send this ice candidate to the client.
Once you complete the five steps above, the connection should be established. See the example of how the connection is established here
When a client wants to publish a track to the SFU, usually the flow is like this:
- Get the media tracks from either the camera or the screen sharing. You can use getUserMedia to get the media tracks.
- Add the media tracks to the WebRTC PeerConnection instance. You can use addTrack to add the media tracks to the PeerConnection instance.
- This will add the tracks to SFU but won't be published until the client sets the track source type either
media
orscreen
. - When the track is added to SFU, the client will trigger
client.OnTrackAdded([]*tracks)
. We need to use this event to set the track source type by callingclient.
SetTracksSourceType(map[trackTypes string]TrackType)` - When successful, this will trigger
client.OnTrackAvailable([]*tracks)
event on other clients. - Use this event to subscribe to the tracks by calling
client.SubscribeTracks(tracksReq []SubscribeTrackRequest)
method. When successful, it will triggerpeerConnection.OnTrack()
event that we can use to attach the track to the HTML video element. Or instead of manually subscribing to each track, you can also useclient.SubscribeAllTracks()
method to subscribe to all available tracks. You only need to call it once, and all available tracks will automatically added to the PeerConnection instance.
See the client_test.go for more details on how to publish and subscribe to tracks.
Usually, renegotiation will be needed when a new track is added or removed. The renegotiation can be initiated by the SFU if a new client joins the room and publishes a new track to the room. To broadcast the track to all clients, the SFU must renegotiate with each client to inform them about the new tracks.
To wait for the renegotiation process from SFU to make sure you receive a new track once published in a room, you can do this:
- When creating a new client after calling
currentRoom.addClient()
method, you can listen to theclient.OnRenegotiation
event. The event will be triggered when the SFU is trying to renegotiate with the client. - Once you receive an offer from the SFU, you can pass the offer SDP to the client and get the answer to pass back to the SFU.
- The event
client.OnRenegotiation
must return an answer SDP received from the client, and it suggested having a timeout when waiting for the answer from the client. If the timeout is reached, you can return an empty SDP to the SFU.
See the example of how the renegotiation is handled here
The renegotiation also can be initiated by the client if the client is adding a new track, for example, by doing a screen sharing. Because both sides can initiate the renegotiation, there is a possibility that both sides are trying to renegotiate at the same time.
To prevent this, we need to check with the SFU relay client if it is allowed to do renegotiation from the client side by checking the client.IsAllowNegotiation() method. If it returns true, then we can start the renegotiation by calling client.Negotiate(offer) method. The same method that we used for the first negotiation. The SFU will respond with an SDP answer to the client. Make sure after you call client.IsAllowNegotiation() method, you also call client.Negotiate(offer) method to make sure the in-renegotiation state is processed by the SFU. If you do not call the following method, the SFU will think that the client is doing a renegotiation and won't initiate the renegotiation.
If the client.IsAllowNegotiation()
is returned false, it means the SFU is currently trying to renegotiate with the client. So the client should wait until the SFU is done renegotiating with the client. In the browser, you can listen to the onnegotiationneeded
event to know when the SFU is done renegotiating with the client.
If a client wants to leave a room or disconnect from the SFU. You can close the PeerConnection instance on the client side. The SFU will detect that the client is closed and will remove the client from the room.
This library is using Glog for logging. You can set the log level by setting the -flag stderrthreshold=warning
when running an application from the command line. You can control it also from your app using the flag package. The default log level is info
. The available log levels are info
, warning
, error
, fatal
, and panic
. You can read more about Glog here
To set the log printing to stderr instead of a file, you can set the -flag logtostderr=true
flag variable to true
. The default is false
.
MIT License - see LICENSE for the full text