WebRTC series

  1. WebRTC source research (1) WebRTC architecture

  2. WebRTC source code research (2) WebRTC source directory structure

  3. WebRTC source research (3) WebRTC operation mechanism

4.WebRTC source research (4) Web server working principle and common protocol basis

  1. WebRTC source code research (5) Nodejs build environment

  2. WebRTC source research (6) Create a simple HTTP service

  3. WebRTC source code Research (7)

  4. [WebRTC source code Research (8)

  5. [WebRTC source code research (9)

  6. [WebRTC source code Research (10)

  7. WebRTC source research (11) access to audio and video equipment

  8. WebRTC source research (12) video constraints

  9. WebRTC source code study (13) audio constraints

  10. WebRTC source research (14) video effects

  11. WebRTC source study (15) get the figure from the video

  12. WebRTC source code study (16) only audio data acquisition

  13. WebRTC source research (17) MediaStreamAPI get video constraints

  14. WebRTC source research (18) WebRTC recording video principle

  15. WebRTC source research (19) WebRTC record acquisition plane data

  16. WebRTC source code research (20) WebRTC signaling server principle

  17. WebRTC source code research (21) WebRTC signaling server implementation

WebRTC source code research (20) WebRTC signaling server principle

1. Signaling server Overview

2. Functions of the signaling server

Without a signaling server, there is no way to communicate between weBRTCs. The transmission media data has two pieces of information that must be exchanged through the signaling server.

The following figure shows the roles of the signaling server:If we don’t have a signaling server, weBRTCs can’t communicate with each other. Let’s take a look at the figure above, which shows the role of the signaling server in the whole call process.

We want to realize P2P direct communication, need to exchange several information: media information, network information, specific business.

  • I. Media information

In terms of SDP, what is a codec? Does it support audio and video? What is the encoding method? Such information is described through THE SDP protocol and transmitted through the signaling server

  • Second, network information

Two WebRTC clients will choose P2P connection as far as possible, so how to discover each other before connection? That’s through the signaling server. First of all, all your network related information to the signaling server, the server for you to exchange to the peer end, the peer end to get your information, if in the same LAN, directly through P2P transmission; If not, first of all through the P2P, to see if it can get through, through the transmission, not through the transfer.

  • 3. Specific business

There is also a signaling server for transmission, such as join the room, leave the room, silence and other functions

So let’s analyze the things that the signaling server needs to handle in detail:

  1. First of all, let’s look at the following blue initiator and receiver. If the sender and receiver want to transmit media data, they have two messages that must be exchanged by the signaling server before they can communicate. These two messages. The first is media information, which is expressed through SDP. Can be simple to understand, we both sides to communicate, what is your codec device, such as I can now for video transmission, I am the H264 encoding, then the other side also want to tell me your codec can support H264 codes, we give you preach the past H264 codes, for example, you said only solution H265, it can not be communication between both sides. All this information must be delivered.
  2. In addition whether you support video support audio, what is your coding way, the information is through the SDP to describe it, through signaling server, first of all, the client would like to the SDP information sent to the server, the server to transfer, transfer to the other, you already know why to transit through signaling server, Because at this time there is no connection between each other, each other do not know the existence of the other side. So that’s the first message to send,
  3. The second information to be transmitted is the network information, we know that WebRTC ultimately through P2P transmission, so before they connect, how do they find each other? Also through the signaling server, first you need to send all the network related information to the server, then the server will help you exchange to the peer end, the peer end got your information, just know that we are in the same LAN, then directly through P2P transmission. If they are not in the same network, THEN I need to go through P2P to see if they can get through. There are several types of P2P. For symmetric, they cannot get through, but for asymmetric, they can try to get through. If you can not get through, but also through the server for transfer. So the signaling server basically transmits media related information for exchange, and the second is network related information for exchange,
  4. And the third is your specific business, join the room, leave the room, silence, forbid the other party to speak, or give the other party permission to speak and so on there are many signaling, you can set according to your business model.

This is the role of the signaling server in the whole WebRTC in the whole WebRTC communication, so this is a necessary signaling server in WebRTC.

3. Realization of signaling service

  • Why signaling service is not provided in WebRTC:
  1. WebRTC does not include the signaling service in the whole specification, but more standardizes all the processes on the client side. Then why not include the signaling server in the specification? The business model of each company is different, and it is difficult to unify the signaling of each company into a set of specifications. So let them define it for themselves instead.
  2. Only the exchange of information that I need can be realized. You can define other businesses by yourself, which is more flexible and easy for companies to accept.
  3. This is actually good for the promotion of the whole WebRTC.

4. Socket. IO

4.1 socket. IO advantage

There are many ways to implement a signaling server. We can use the primitive socket mechanism for communication, or we can use a third party open source library. We use socket. IO, mainly because it has several advantages:

  • Socket. IO is a WebSocket superset
  1. socket.ioIt comes with itWebSocketFunction. We all know that there are two protocols for the whole audio and video transmission,TCPandUDPIn the underlying protocolUDPIt is used for streaming media transmission, such as audio, video, and textUDPTo transmit,UDPThe problem is that it is unreliable transmission, which means I can lose packets, which is no problem for audio and video. When I have a bad Internet connection and I lose audio data, it freezes up at most, and it continues to work without affecting our normal calls too much.
  2. If it is different for signaling, signaling is generally usedTCPIt’s called a reliable connection, and my package has to be guaranteed to arrive, otherwise I’m disconnected because the network can’t support my entire business. So it either has to, and we’re done playing. In these two cases, our signaling must arrive during transmission.
  3. If our media information and network information can’t be exchanged, it certainly won’t work, so for signaling we generally useTCP.websocketThat’s what the bottom layer usesTCPSocket. IO also uses TCP.
  • Socket. IO has the concept of a room
  1. socket.ioThere’s the idea of a room,socket.ioWhen two or more individuals to communicate, first of all, I want to enter a room, this is a very natural logic, we are going to a class or a meeting, first you need to gather together and have the concept of a virtual logic, is everyone want to be in a room, everyone in this room you can communicate with each other.
  2. We can look atwebrtcIn the official example, there are actually three types of servers, namely room server, signaling server and streaming media transfer server, which are used heresocket.ioWe don’t have to write a separate room server here, which means that the room server and the signaling server are on the same server.
  • Socket. IO cross-platform, cross-terminal, cross-language

Socket. IO cross-platform, cross-terminal, cross-language, so that it is convenient for us to achieve signaling on each end of each end, to connect with our server, so it is particularly convenient, this is the use of socket to achieve socket. IO to achieve signaling server root cause.

4.2 Working Principle of socket. IO

As you can see above, socket. IO works quite simply,socket.ioOther it is ajsLibrary, which is based onnode.jsServerThis has to do with our technical options, which we chose earlierwebFor the server, yesnodeJSAnd, in thenodeJSWe’re adding this to the serversocket.ioSo we’re done on the server side, very simple.

But if we were to write a separate set, it would take a long, long time. On our existing nodeJS Web server, we just added socket. IO, and with a few simple lines of code changes, we implemented a server-side.

It doesn’t actually work with a server, so there’s a client.

IO is a lib client library that connects to NodeJS’s socket. IO service.

After the connection is established, we can create a room or join a room. Then users in the whole room can communicate with each other. The logic of this piece is very simple.

In addition, socket. IO can scale horizontally, cascade many socket. IO services, and string them together so that when I send a message, it can be transmitted on each socket. IO. Have two users, for example, the node, in Beijing the first user has a user is in Shanghai this point, it doesn’t matter in its upper is it server can string together, so users in Beijing first send your information to the server in Beijing, Beijing server through the network of nodes and then spread to Shanghai, The nodes in Shanghai eventually find the users in Shanghai and send them out, so that they can communicate with each other.

4.3 APIS provided by socket. IO

  • Send a message to this link:socket.emit() :

This means that the client sends a message to the server, and the server receives the message and gives a callback. So it’s a return, let’s say I want to join the room, and when I join it it gives me a return that I joined. It’s just for me personally. After I receive the success of joining, I can do the following logic processing. This creates an asynchronous operation.

  • Send a message to everyone in a room:io.in(room).emit():
  1. It was like a broadcast, and everyone included me, and I wanted to know that I was sending this message. In the case of joining a room, he’s actually sending a message to everyone in the room that the user has joined the room. Of course, some businesses do not need it, it just sends you back “you have joined successfully”.
  2. There is each end to maintain a user list, that who came and who went out, I have to be clear, this time everyone will receive this message, for the sender, I received this message, you can do the logic processing behind, do audio and video collection.
  3. For other users, when I receive a message that a user has joined, I update a list of my users and add that user to my list of users, which is to send a message to everyone in a room.
  4. ioIt represents the entire node that we deploysocketAll the people in the node are included,roomIt represents a specific room, which means everyone in the room has to send.
  • Send a message to everyone in a room except this connection : socket.broadcast.emit() :
  1. This is to send a message to everyone except myself. In an IO node, there may be many rooms, like 10 or 20 rooms, so this broadcast is broadcast, and I’m going to send a message to everyone except me (the whole site). For example, if I send a notification to cut off all users in the node, that is, to cut off all users to connect to other nodes, then you can send a message to everyone as the super administrator.

4.3.1 Sending the action command

  • Send the action command:
S: socket.emit('action');
C: socket.on('action'.function(){... });Copy the code

When S(server) sends an action message, that is, an action, C(client) listens for the action, using socket.on, which processes the action with a function followed by a specific logic.

  • Send an action command with data:
S: socket.emit('action', data);
C: socket.on('action'.function(data){... });Copy the code
  • An action is sent with two pieces of data:
S: socket.emit(action, arg1, arg1);
C: socket.on('action'.function(arg1,arg2){... });Copy the code
  • Sends an action command that includes the callback function in the emit method:
S: socket.emit('action',data,function(arg1,arg2){});
C: socket.on('action'.function(data,fn){fn('a'.'b'); });Copy the code