Janus WebRTC server and SFU: a real time video calling app

Ali AsgherAli Asgher
11 min read

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

  1. 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

  2. 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

  3. Bandwidth Optimization

    1. 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

    2. Selective Subscription: Clients can subscribe to only the streams that they need, thus reducing unnecessary bandwidth usage.

  4. 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

  5. Cost Efficiency: Since SFU require less processing as compared to MCU a smaller instance on the cloud can work for you

  6. 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.

  7. Easier implementation of Advanced features

    1. 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

    2. Active Speaker Detection: SFU can forward streams of only the active speaker thus enhancing focus and reducing unnecessary data transmission.

    3. Recording and streaming: SFU can forward the media stream to recording service or broadcast platforms with out affecting any communication between devices and users

  8. 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

  1. System Preparation

    1. Server Preparation: Ensure that you have a Ubuntu or Debian server running.

    2. 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
  1. Configure Janus

    1. Edit the config files

      1. The file is located in /opt/janus/etc/janus/

      2. Important files include janus.cfg , janus.transport.websockets.cfg and janus.plugin.videoroom.cfg

    2. Enable the needed plugins

      1. Ensure that the video room plugin is enabled in order to use the SFU

      2. You can also configure WebSockets transport if you want to use that for signalling in your application

  2. Setup SSL Certificate

    1. Generate SSL certificate

      1. 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
    2. Update configuration

      1. Point the configuration files to the SSL certificate
  3. Start the Janus Server

    1. Run Janus
/opt/janus/bin/janus
  1. Verify Operation

    1. Check the logs to see everything is running without any errors.

Step 2 Developing the Client Side Application

  1. Set Up the Development Environment

    1. Choose a front end framework

      1. You can choose between React, Angular or any other front end framework
    2. Include WebRTC libraries

      1. You can also use WebRTC helper libraries like adapter.js for compatibility with different browsers
  2. Establish Signalling Server

    1. Connect to Janus via Websockets

    2. Implement the handshake in order to create a new session and then attach a video room plugin

  3. Create or Join a video room

    1. Send a request to Janus server to create a new room or join an existing room on the server
  4. Handle local media streams

    1. 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 and event messages to listen to notifications from other meeting participants

  • Create 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

  1. Implement event listener and Handlers

    1. Monitor events like oniceconnectionstatechange and onicecandidate and handle these events when they occur and implement the reconnect logic if needed

Exploring Metered.ca SFU as an Alternative to Janus

What is Metered Global Cloud SFU

  1. Metered Global Cloud SFU is a cloud based Selective Forwarding Unit by Metered.ca

  2. With Metered SFU developers can build any type of application including video, audio and data transmission

  3. The Metered SFU runs using native WebRTC API and HTTP without the need for any proprietary SDKs.

  4. Users are also automatically routed to the nearest SFU based on their geographical location

  5. 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

  1. Establishing a Connection

    1. 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);
  1. Publishing a Track

    1. 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

  1. Subscribing to a Track

      1. Request the track subscription from the SFU

        1. Send the request to the SFU using the trackid and remoteTrackSessionId of the desired stream.
        1. Set the received SDP on your peerConnection

        2. Handle the incoming media and the ontrack event to show the incoming stream to the user

Advantages of Metered SFU over Janus

  1. Scalability

    1. Media distribution

      1. Clients send all their media streams to the SFU once, it does not matter how many participants are there

      2. SFU then manages the distribution of streams to all the subscribers who request the specific streams

    2. Large Scale streaming

      1. Metered SFU supports small meetings as well as large scale live streaming to thousands of users
  2. Platform Independence

    1. No Proprietary SDKs Required

    2. Reduced Complexity

  3. Flexible Publish Subscribe model

    1. No Room Constraints: Metered SFU operates on sessions rather than pre-defined rooms.

    2. Sessions can publish and subscribe to streams with no limit on the number of participants.

    3. Dynamic and Scalable designs: Best for scalable designs such as live streaming.

  4. Cost Efficiency

    1. Metered SFU is cost efficient because we charge based on data transferred per GB rather than per minute billing

    2. 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

  5. Global SFU Network:

    1. The Metered SFU has global reach with SFUs in all 5 continents

    2. The media streams are routed through high speed interconnect SFU links

    3. This enables users from different parts of the world to experience low latency communication


Comparing Metered SFU with Janus

  1. Deployment and Maintenance

    1. Metered SFU:

      1. Cloud based, managed service infrastructure

      2. Offloads operational and maintenance responsibilities allowing developers to focus on application features

    2. Janus

      1. Requires self hosting, server setup and ongoing maintenance also it is not completely free as you have to pay for cloud servers

      2. Requires managing updates, scaling and security patches

  2. Scalability and Performance

    1. Metered SFU

      1. Automatically scales and can handle any number of participants

      2. With SFUs in every geographical location, participants in every part of the world experience low latency and high performance

    2. Janus

      1. not scalable globally and depends on server resources that you have allocated

      2. Requires complex setups like clustering for high load use cases

  3. Cost Structure

    1. Metered SFU

      1. Billed on data transfer in GB which is cheaper and also pay as you go

      2. More predictable and lower cost for applications that require high data usage

    2. Janus

      1. While open source there are operational costs associated with using Janus including server hosting and maintenance

      2. Costs can increase significantly with scaling and high usage

  4. Flexibility and Features

    1. Metered SFU

      1. Flexible and scalable publish and subscribe model

      2. simplifies development with standard WebRTC APIs and supports unlimited participation

    2. Janus

      1. Offers plugin based architecture with features like video rooms streaming and others

      2. Requires configuration and potential customization for specific use cases

  5. Ease of Integration

    1. Metered SFU

      1. Simplifies the development process with standard WebRTC APIs

      2. Reduces time to market with need to think about infrastructure setup, server requirements etc


Metered TURN servers

  1. 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.

  2. 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

  3. Servers in all the Regions of the world: Toronto, Miami, San Francisco, Amsterdam, London, Frankfurt, Bangalore, Singapore,Sydney, Seoul, Dallas, New York

  4. Low Latency: less than 50 ms latency, anywhere across the world.

  5. Cost-Effective: pay-as-you-go pricing with bandwidth and volume discounts available.

  6. Easy Administration: Get usage logs, emails when accounts reach threshold limits, billing records and email and phone support.

  7. Standards Compliant: Conforms to RFCs 5389, 5769, 5780, 5766, 6062, 6156, 5245, 5768, 6336, 6544, 5928 over UDP, TCP, TLS, and DTLS.

  8. Multi‑Tenancy: Create multiple credentials and separate the usage by customer, or different apps. Get Usage logs, billing records and threshold alerts.

  9. Enterprise Reliability: 99.999% Uptime with SLA.

  10. Enterprise Scale: With no limit on concurrent traffic or total traffic. Metered TURN Servers provide Enterprise Scalability

  11. 5 GB/mo Free: Get 5 GB every month free TURN server usage with the Free Plan

  12. Runs on port 80 and 443

  13. Support TURNS + SSL to allow connections through deep packet inspection firewalls.

  14. Supports both TCP and UDP

  15. Free Unlimited STUN

0
Subscribe to my newsletter

Read articles from Ali Asgher directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ali Asgher
Ali Asgher