Janus WebRTC server and SFU: a real time video calling app
This article was orignally published on the Metered blog: Janus WebRTC server and SFU: a real time video calling app
In this article we are going to create a video calling app with Janus WebRTC server and SFU
We are also going to learn about Metered.ca SFU and why it is a better option than Janus Server
What is Janus?
Janus is an Open source, general purpose WebRTC SFU server. It is a modular gateway that facilitates real time communication apps by handling the complex WebRTC protocols and provide a stable server for the purposes of media exhange
Key features of Janus SFU
Modular architecture: Janus has a plugin based system where each plugin has some specific functionality. This modularity allows for customization and extention
Protocol handling: Janus manages signaling and negotiation that is required for establishing webrtc connections.
Interoperability: Janus supports media codecs and is compatible with different clients and browsers thus Janus has broad compatibility
Scalability: Designed to handle multiple connections efficiently, Janus is scalable within a specific geographical zone. (that is it will work fine if all the users that are using Janus are within a small geographic zone)
Understanding SFU (Selective forwarding Unit)
A SFU or a Selective Forwarding Unit is a media server architecture that is used in WebRTC apps to efficiently route media streams between different media participants
The SFU receives incoming video streams from each participant and then selectively forwards the streams to other participants as requested.
That is each participant receives the streams that the participant request while all the participants push their streams to the SFU
Advantages of Using SFU for Routing Media Streams Efficiently
Scalability: SFU can handle a large number of connections because the SFU does not involve in computational intensive tasks such as encoding and decoding, instead just forwards the streams to the users
Low Latency: The SFU forwards the streams without processing thus leading to low latency. But if your users are all over the world you need a global distributed SFU service such as metered.ca cloud sou
Bandwidth Optimization
Simulcast Support: SFU can stream multiple streams of different qualities from the same participant. Clients can choose to stream different quality that best fits their devices and network capabilities for smooth video / audio streaming
Selective Subscription: Clients can subscribe to only the streams that they need, thus reducing unnecessary bandwidth usage.
Flexibility and Control: Participants have a greater flexibility over what they want to receive, for example if they want to receive just audio or receive video streams of select participants then they have the option to do that
Cost Efficiency: Since SFU require less processing as compared to MCU a smaller instance on the cloud can work for you
Preservation of the quality of media: SFU preserve media quality because they do not do any processing on the media streams just forwards the streams to requested participants.
Easier implementation of Advanced features
Dynamic Layouts Clients can customize their own layouts with the video streams that they are receiving. Rather than pre determined layouts that you get with video sdk and apis
Active Speaker Detection: SFU can forward streams of only the active speaker thus enhancing focus and reducing unnecessary data transmission.
Recording and streaming: SFU can forward the media stream to recording service or broadcast platforms with out affecting any communication between devices and users
Improved Reliability: With no processing of media on the server, there are fewer points of failure . SFU can also be clustered for redundancy, thus making sure that the services remain live even if one server encounters any issue.
Implementing Janus in your Application
Integrating Janus SFU in your app involves these steps. Here is step by step process to implement Janus in your application.
Step 1: Setting Up the Janus Server
System Preparation
Server Preparation: Ensure that you have a Ubuntu or Debian server running.
Install Dependencies: Janus requires libraries like
GLib
zlib
pkg-config
Jansson
libconfig
libnice (at least v0.1.16 suggested, v0.1.18 recommended)
OpenSSL (at least v1.0.1e)
libsrtp (at least v2.x suggested)
you can easily install these dependencies on ubuntu or Debian like
apt install libmicrohttpd-dev libjansson-dev \
libssl-dev libsofia-sip-ua-dev libglib2.0-dev \
libopus-dev libogg-dev libcurl4-openssl-dev liblua5.3-dev \
libconfig-dev pkg-config libtool automake
Download and Build Janus
- Clone the repo
git clone https://github.com/meetecho/janus-gateway.git
- Navigate to the directory
cd janus-gateway
- Generate config Scripts
sh autogen.sh
- Configure the build
./configure --prefix=/opt/janus
- Compile and install
make
sudo make install
sudo make configs
Configure Janus
Edit the config files
The file is located in
/opt/janus/etc/janus/
Important files include
janus.cfg
,janus.transport.websockets.cfg
andjanus.plugin.videoroom.cfg
Enable the needed plugins
Ensure that the video room plugin is enabled in order to use the SFU
You can also configure WebSockets transport if you want to use that for signalling in your application
Setup SSL Certificate
Generate SSL certificate
- You can generate a self signed certificate using lets encrypt if you want to use secure connections. This is an optional step if you want to use secure connections
Update configuration
- Point the configuration files to the SSL certificate
Start the Janus Server
- Run Janus
/opt/janus/bin/janus
Verify Operation
- Check the logs to see everything is running without any errors.
Step 2 Developing the Client Side Application
Set Up the Development Environment
Choose a front end framework
- You can choose between React, Angular or any other front end framework
Include WebRTC libraries
- You can also use WebRTC helper libraries like
adapter.js
for compatibility with different browsers
- You can also use WebRTC helper libraries like
Establish Signalling Server
Connect to Janus via Websockets
Implement the handshake in order to create a new session and then attach a video room plugin
Create or Join a video room
- Send a request to Janus server to create a new room or join an existing room on the server
Handle local media streams
- Show the local media streams to the user if you want to, we will also show the remote streams in the later section below
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
// Use the local stream
})
.catch(error => {
// Handle errors
});
Attach the local stream to the video element in the HTML to play the stream to the user
5. Publish to Janus
Use the RTCPeerConnection to create an SDP offer
Send the offer to Janus using the API
Receive the SDP answer from the Janus and set it as remote description
6. Subscribe to remote streams
Handle the
joined
andevent
messages to listen to notifications from other meeting participantsCreate the Peer connections for subscribers, that is for each remote participant you need to create a new RTCPeerConnection
Handling remote streams, get the remote streams and attach them to HTML video elements to play to video on the users web browser
Implement event listener and Handlers
- Monitor events like
oniceconnectionstatechange
andonicecandidate
and handle these events when they occur and implement the reconnect logic if needed
- Monitor events like
Exploring Metered.ca SFU as an Alternative to Janus
What is Metered Global Cloud SFU
Metered Global Cloud SFU is a cloud based Selective Forwarding Unit by Metered.ca
With Metered SFU developers can build any type of application including video, audio and data transmission
The Metered SFU runs using native WebRTC API and HTTP without the need for any proprietary SDKs.
Users are also automatically routed to the nearest SFU based on their geographical location
Multiple SFU are connected globally with each other using high speed interconnect, ensuring lowest possible latency
Key features of Metered Global SFU
Global Distribution:
- Users are connected to the closed SFU based on their geographical location thus have lowest latency and high performance
Platform Independence
- Eliminates the need for any proprietary SDK, thus enabling you develop across different platforms using the WebRTC own APIs
Flexible Publish-Subscribe Model:
- Capacity for unlimited participants and different session structures.
Scalability
- You can have small meetings as well as large scale live streaming sessions with thousands of users
Cost Efficiency:
Charges are based on rate of data transfer offering significant savings for high usage scenarios
Also there is pay per use billing available.
How Metered SFU works
Connection flow
Establishing a Connection
- Create a peer connection
const peerConnection = new RTCPeerConnection();
b. Send Local SDP offer
- Generate the SDP offer and then send it to the Metered.ca SFU using HTTP API request.
c. Receive SFU remote SDP
- Set the received SDP as the remote description on your
peerConnection
d. Connection Established
- Add the media track
peerConnection.addTrack(localMediaStream.getVideoTracks()[0], localMediaStream);
Publishing a Track
- Add Media Track
peerConnection.addTrack(localMediaStream.getVideoTracks()[0], localMediaStream);
b. Generate and Send SDP offer
Send the updated SDP offer to the SFU
Receive and set the SFU response to the SDP
Subscribing to a Track
Request the track subscription from the SFU
- Send the request to the SFU using the
trackid
andremoteTrackSessionId
of the desired stream.
Set the received SDP on your
peerConnection
Handle the incoming media and the
ontrack
event to show the incoming stream to the user
- Send the request to the SFU using the
Advantages of Metered SFU over Janus
Scalability
Media distribution
Clients send all their media streams to the SFU once, it does not matter how many participants are there
SFU then manages the distribution of streams to all the subscribers who request the specific streams
Large Scale streaming
- Metered SFU supports small meetings as well as large scale live streaming to thousands of users
Platform Independence
No Proprietary SDKs Required
Reduced Complexity
Flexible Publish Subscribe model
No Room Constraints: Metered SFU operates on sessions rather than pre-defined rooms.
Sessions can publish and subscribe to streams with no limit on the number of participants.
Dynamic and Scalable designs: Best for scalable designs such as live streaming.
Cost Efficiency
Metered SFU is cost efficient because we charge based on data transferred per GB rather than per minute billing
Only outbound data is charged and inbound data to SFU is free, this is specially cost effective in applications like surveillance where the streams are continuously uploaded to the SFU but are infrequently viewed
Global SFU Network:
The Metered SFU has global reach with SFUs in all 5 continents
The media streams are routed through high speed interconnect SFU links
This enables users from different parts of the world to experience low latency communication
Comparing Metered SFU with Janus
Deployment and Maintenance
Metered SFU:
Cloud based, managed service infrastructure
Offloads operational and maintenance responsibilities allowing developers to focus on application features
Janus
Requires self hosting, server setup and ongoing maintenance also it is not completely free as you have to pay for cloud servers
Requires managing updates, scaling and security patches
Scalability and Performance
Metered SFU
Automatically scales and can handle any number of participants
With SFUs in every geographical location, participants in every part of the world experience low latency and high performance
Janus
not scalable globally and depends on server resources that you have allocated
Requires complex setups like clustering for high load use cases
Cost Structure
Metered SFU
Billed on data transfer in GB which is cheaper and also pay as you go
More predictable and lower cost for applications that require high data usage
Janus
While open source there are operational costs associated with using Janus including server hosting and maintenance
Costs can increase significantly with scaling and high usage
Flexibility and Features
Metered SFU
Flexible and scalable publish and subscribe model
simplifies development with standard WebRTC APIs and supports unlimited participation
Janus
Offers plugin based architecture with features like video rooms streaming and others
Requires configuration and potential customization for specific use cases
Ease of Integration
Metered SFU
Simplifies the development process with standard WebRTC APIs
Reduces time to market with need to think about infrastructure setup, server requirements etc
API: TURN server management with powerful API. You can do things like Add/ Remove credentials via the API, Retrieve Per User / Credentials and User metrics via the API, Enable/ Disable credentials via the API, Retrive Usage data by date via the API.
Global Geo-Location targeting: Automatically directs traffic to the nearest servers, for lowest possible latency and highest quality performance. less than 50 ms latency anywhere around the world
Servers in all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York
Low Latency: less than 50 ms latency, anywhere across the world.
Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.
Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.
Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.
Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.
Enterprise Reliability: 99.999% Uptime with SLA.
Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability
5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan
Runs on port 80 and 443
Support TURNS + SSL to allow connections through deep packet inspection firewalls.
Supports both TCP and UDP
Free Unlimited STUN
Subscribe to my newsletter
Read articles from Ali Asgher directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by