WebRTC and Install Janus on Ubuntu 18.04

Tram Ho

1. Overview of WebRTC

An open source project that allows browsers and mobile applications to communicate directly with each other in real time (peer to peer). Webrtc provides java script api to help build web applications that simply transmit data, video, and audio.

WebRTC is supported by major browsers like Chrome, Firefox, Opera and Microsoft Edge, as well as platforms like Android and iOS. WebRTC does not need to install any external plugin in the browser.

2. Steps to set up webrtc

WebRTC establish link like? That is an important question that I think many people will care about. So how many steps does webrtc make the connection between two PCs? Please answer in two steps as follows:

2.1. Find a PC location

To understand more easily how webrtc find the location of a PC, think that when you want to call someone, you have to dial the phone number of the other person and call the connection. The same is the opposite when someone wants to call you. In the case of a phone call, the phone number is a user identification identifier used by the telecom operator to determine the recipient’s location.

What about web application? It will differ from telecom in that each web browser to go to the internet is mostly through a router (switch, router) these devices are called Network Address Traversal (NAT) , so the PCs don’t The private IP address of another PC can be seen over the Internet because it is affected by NAT’s private IP.

So how do PCs know each other’s private IP? Let me think that when I want someone to call, I give them my phone number. To remedy this we use a protocol called the Interactive Connectivity Establishment (ICE).

ICE plays a role in finding the best way to connect the two machines together.

ICE provides us “ICE Candidate” containing public IP, port, other connection information. You understand roughly that ICE Candidate does not have route information from the Internet through the NATs to the PC.

ICE can connect directly or indirectly in case of no NAT or NAT.

  1. When there is NAT, ICE must rely on Session Traversal Utilities for NAT (STUN) and / or Traversal Using Relays around NAT (TURN).
  2. In the absence of NAT, ICE is very simple to connect two machines together because there are separate IPs of the machines available.

So what is the role of STUN and TURN?

STUN server

STUN is understood to allow a PC to find out its own IP address, that is, a PC-A needs to know its own IP address, then send requet to STUN. STUN will send a response to the IP of PC-A, at which time the IP address of PC-A can be found by other PCs to connect.

However, in case PC-A is blocked by firewall or for some reason STUN cannot provide its IP, ICE will rely on TURN to establish connection. When relying on TURN server, this time is no longer called peer to peer but client to host. Of course, webrtc still prioritizes to connect peer to peer rather than using to STUN, but TURN server is always ready for redundancy.

So what is TURN server? You continue to see below.

TURN server

TURN is a relay server and acts as an intermediary to transmit data, audio, and video when it is not possible to connect directly between two PCs.

The required TURN is available throughout even when the WebRTC connection has been established.

Note :

  1. TURN is the last solution when webrtc cannot establish a P2P connection.
  2. Data flows through the TURN server, it consumes a lot of bandwidth and communication does not use P2P in this case.

2.2. Notice of webrtc setup

Once the PCs have got ICE candidate, the next step is to send ICE Candidate to another peer PC for connection. How are candidates submitted for ICE? It will be grouped together of candidates ICE and Session Description into a single object.

Then it sends the object using Session Description Protocol (SDP). There are also cases where the ICE Candidate is not bundled and sent separately called Trickle ICE. (Trickle ICE is a new concept that I would not like to talk about in this sharing)

Coming here to raise a question, when there is no receiver information, how to send ICE candidate and session description?

It will use a Signalling Mechanism, a signaling mechanism such as name Suggestions, or exchange signals between computers that intend to connect.

Common signaling mechanism uses communication channels such as web socket, socket.io . That is how two PCs connect peer to peer together to establish webrtc connection, to be clear, I summarize it through the following example:

PC-A establishes WebRTC with PC-B need to perform the following actions:

  1. PC-A generates the ICE Candidate using the Interactive Connectivity Establishment (ICE). In most cases, it requires a (STUN) or (TURN).
  2. PC-A groups the ICE Candidate and Session Description into a single object. This object is stored as a Local Description in PC-A and transferred to PC-B through this partial signalling mechanism called Offer.
  3. PC-B receives the offer and stores it as a remote description for further use. PC-B generates its own ICE Candidate and Session Description, stores it as a local description and sends it to PC-A through the signalling mechanism. This section is called Answer.

(Note: As stated earlier, ICE candidates in steps 2 and 3 may also be submitted separately)

  1. PC-A receives the Answer from PC-B and stores it as Remote Description. Now both machines have each other’s connection information and can initiate communication via WebRTC.

3. Janus server

Janus is a WebRTC server developed by Meetecho. It provides the means of establishing WebRTC communication between browsers, between android, IOS and browser.

Communication using JSON. Any specific feature / application is provided by server side plugins, Browsers can then contact via Janus to take advantage of the functionality they provide.

Install Janus server on Ubuntu I installed janus on ubuntu 18.04, the steps are as follows:

Step1: Create the file dependencies.sh to install the necessary software and dependence. Open the file dependencies.sh and add the content:

open terminal run: bash dependencies.sh or zsh dependencies.sh

Step2: Install libnice

Step3: Install libsrtp2

Step4: Install usrsctp

Step5: Install libwebsockets

Step6: Install MQTT C

Step7: Install RabbitMQ

Step8: Install janus

At this point, you have completed the installation to start the janus server you go to:

If you see the log above is ok, the other red ERRORs I haven’t connected to the STUN server, haven’t pointed to the websocket yet. I will talk to the back.

Video installation: https://www.youtube.com/watch?v=fS8lj9csVuI

Next to open the web and run the Echo Test demo, how to do? You copy the html directory from the janus-gateway folder to: / var / www /

There are 2 ways to use web server apache2 or nginx to run web up. Here I use nginx install and add nginx:

After installing nginx, open the file

You will see root pointing to your html directory just copied, this is the default path, you can leave the html folder somewhere else and point to.

trembling nginx up

Access http: // localhost

Note: chorme version 47 and above only allow getUserMedia under https or localhost, so you want to access the ip link format like this: will need a little setup in your browser Open: chrome : // flags / then enable ip to access your computer

Where there is no NAT. Like my computer is connected to the internal wifi network with the mobile, the video calls directly to each other. On my laptop, register the name: duy

On my phone, I registered the name: napo

Now call from laptop to mobile

Where there is NAT

That is, the internet with routers will need a STUN server. Note: when connecting to the websocket server you will and this file:

That server is pointing to websocekt you can replace the socket eg: var server = “wss: // xxxxx / janus”;

Config janus points to the STUN server public, then you can go into this file:

Change the STUN server link in config NAT:

OK so you can call over the internet, between PC and mobile.

That’s all it is to build a webrtc using the janus server.

Good luck ! ^ _ ^

Share the news now

Source : Viblo