Web real-time Communication is an API that supports Web browsers to conduct real-time voice or video conversations. A room server, signaling server and Intranet penetration server are required.

Room server

Open source implementation: github.com/webrtc/appr…

The room server is used to create and manage the maintenance of the state of the call session, whether it is a call or a multi-party call, joining and leaving the room, etc

Signaling server

Apprtc.

Signaling is the process of communication coordination. In order to establish a webRTC communication process, clients need to exchange the following information

1. Session control information, which is used to start and end a call, including start and end video commands. 2. Messages used to notify each other when errors occur 3. Metadata, such as the decoding mode and bandwidth of each audio and video. 4. Network data, including the public IP address, port number, internal IP address, and port number of the peer party.Copy the code

Intranet Penetration Server

Github.com/coturn/cotu…

Metadata is forwarded to another client via a signaling server, but for streaming data, a point-to-point connection is first attempted once a session is established. Each client has a unique address that it can use to communicate and exchange data with other clients.

Generally, the Internet is connected behind a firewall or a home router configured with a private subnet. As a result, the IP address of our computers is not a WAN IP address, so we cannot communicate with each other directly. Allowing two computers on a private network to communicate is a technique commonly known as NAT penetration. WebRTC can use the ICE framework to overcome real-world complex networks.

STUN (Simple Traversal of UDP Through NAT) is a complete NAT Traversal solution, that is, Simple UDP Traversal of NAT.

Traversal (TURN) is Traversal Using Relay NAT in the same way as STUN, but TURN is Traversal by forwarding.

ICE (Interactive Connectivity Establishment) is a comprehensive NAT traversal solution that integrates the above two protocols. First, it tries to establish a connection with the host address obtained by the device system or network card. If this fails (the device behind the NATs will) ICE gets an external address from the STUN server, and if this fails, it uses the TURN relay server to communicate.

Open ports in Ali Cloud background (rules for joining the team) Ports: 3478, 8080, 8089, 80, 443

The directory is /root/webrtc

Linux background run command: nohup followed by &

Build AppRTC

Install the required tools (in addition to apt can also download the installation package or source code compiled installation) :

1. Install JDK

#To demonstrate that the initial environment has been reinstalled, run the following command after reinstallation:
apt-get update 

apt-get install openjdk-8-jdk 

java -version
#java version "1.8.0 comes with _181"
#Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
#Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Copy the code

2. Install Node.js

apt-get install nodejs-legacy 
apt-get install npm 
node --version
#v4.2.6
npm --version
#3.5.2

npm -g install grunt-cli
grunt --version
#Grunt - cli v1.3.2
Copy the code

Install Python and python-webtest (python2.7)

apt-get install python 
apt-get install python-webtest

python -V
#Python 2.7.12
Copy the code

4. Install Google_appEngine

Unzip wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.40.zip Google_appengine_1. 9.40. Zip
#To configure environment variables, add a line at the end of /etc/profile:
export PATH=$PATH:/root/webrtc/google_appengine
source /etc/profile
Copy the code

5. Install go (used to install APPRTC)

apt install golang-go

go version 
#Go version go1.6.2 Linux/amd64
Copy the code
#Create the GO working directory
mkdir -p /root/webrtc/goWorkspace/src
#To configure environment variables, add a line at the end of /etc/profile:
export GOPATH=/root/webrtc/goWorkspace
source /etc/profile
Copy the code

6, Install libevent (for penetration server)

#Current directory :root/webrtc/
#https://github.com/coturn/coturn/wiki/CoturnConfigWget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz tar XVF libevent - 2.0.21 - stable. Tar. Gz CD libevent-2.0.21-stable./configure make installCopy the code

7. Install APPRTC (2018/11/10)

#Current directory :root/webrtc/
git clone https://github.com/webrtc/apprtc.git
#Soft connect collider's source code to go's working directory (ln)-sSoft links, shortcuts)
ln -s /root/webrtc/apprtc/src/collider/collider $GOPATH/src
ln -s /root/webrtc/apprtc/src/collider/collidermain $GOPATH/src
ln -s /root/webrtc/apprtc/src/collider/collidertest $GOPATH/src
#Compile collidermain
go get collidermain
go install collidermain

#Go get collidermain: collidermain
#Error: package golang.org/x/net/websocket: unrecognized import path"golang.org/x/net/websocket"
#Perform:
#mkdir -p $GOPATH/src/golang.org/x/
#cd $GOPATH/src/golang.org/x/
#git clone https://github.com/golang/net.git net 
#go install net


Checking connectivity... done.
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc# ln -s /root/webrtc/apprtc/src/collider/collider $GOPATH/src
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc# ln -s /root/webrtc/apprtc/src/collider/collidermain $GOPATH/src
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc# ln -s /root/webrtc/apprtc/src/collider/collidertest $GOPATH/src
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc# ls $GOPATH/src
collider  collidermain  collidertest
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc#

collider  collidermain  collidertest
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc# go get collidermain
package golang.org/x/net/websocket: unrecognized import path "golang.org/x/net/websocket" (https fetch: Get https://golang.org/x/net/websocket?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc#

root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc/goWorkspace/src/golang.org/x# go install net
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc/goWorkspace/src/golang.org/x# go get collidermain
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc/goWorkspace/src/golang.org/x# go install collidermain
root@iZ2ze72sd9zx4b6hr6zz1iZ:~/webrtc/goWorkspace/src/golang.org/x#
Copy the code

8. Install coturn

#Directory: root/webrtc /
#https://github.com/coturn/coturn/wiki/DownloadsWget http://coturn.net/turnserver/v4.5.0.7/turnserver-4.5.0.7.tar.gz tar XVFZ turnserver - 4.5.0.7. Tar. Gz CD Turnserver - 4.5.0.7. / configure make installCopy the code

Configuration and Operation:

1. Coturn Nat traversal server

Configure a firewall to allow access to port 3478 (including TCP and UDP, which is used for NAT penetration)

#172.17.250.108: internal NETWORK IP address (Internal and external network IP address can be seen in the background of Ali Cloud)Nohup turnServer -L 172.17.250.108 -a -u Tina :12345 -V -f -r nort.gov &#Account Tina Password :12345 This step is optional, but the apPRTC configuration will be used later
#Command followed by ampersand, after the execution of CTR + C, will not stop
Copy the code
#Open a new window for executionNetstat ntulp | grep turnserver # or lsof - I: 3478#The output is roughly this successfulTCP 0 0 127.0.0.1:5766 0.0.0.0:* LISTEN 16848/turnserver TCP 0 0 172.31.247.136:3478 0.0.0.0:* LISTEN 16848/turnserver TCP 0 0 172.31.247.136:3478 0.0.0.0:* LISTEN 16848/turnserver UDP 0 0 172.31.247.136:3478 0.0.0.0:* 16848/turnserver UDP 0 0 172.31.247.136:3478 0.0.0.0:* 16848/ turnServerCopy the code

2. Collider Signaling server

Configure a firewall to allow access to port 8089 (TCP)

#Create a self-signed digital certificate
#If OpenSSL is not available, you need to install it
mkdir -p /cert
cd /cert
#The CA private key
openssl genrsa -out key.pem 2048 
#Self-signed certificateopenssl req -new -x509 -key key.pem -out cert.pem -days 1095 nohup $GOPATH/bin/collidermain -port=8089 -tls=true - room - server = "https://47.75.90.219:8080" & nohup $GOPATH/bin/collidermain - port = 8089 - TLS = true - room - server = "https://39.107.122.235:8080" &Copy the code
#Also check whether it is successful
netstat -ntulp | grep collider
tcp6       0      0 :::8089                 :::*                    LISTEN      16864/collidermain
Copy the code

3. Apprtc room server

Configure a firewall to allow access to port 8080 (TCP, which is used for Web access)

Modify the conturn and Collider for apPRTC.

 vim /root/webrtc/apprtc/src/app_engine/constants.py
 #47. * * *. 219 IP networks outside
Copy the code

#compile
cd /root/webrtc/apprtc
npm install
grunt build
#If: No module named requests
Copy the code

Error: The Requests module does not exist

ImportError: No module named requests
Warning: Command failed: python ./build/build_app_engine_package.py src out/app_engine
Traceback (most recent call last):
  File "./build/build_app_engine_package.py", line 12, in <module>
    import requests
ImportError: No module named requests
 Use --force to continue.

Aborted due to warnings.
Copy the code
Install the PIP

Download the setup-Python tool

#One line of command is too long. The $begins with a line of command
$cd /root/webrtc
$Wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg - no - check - certificate
$Chmod + x setuptools 0.6 c11 - py2.7. An egg
$. / setuptools 0.6 c11 - py2.7. An egg
$wget https://pypi.python.org/packages/source/ p/PIP/PIP - 1.5.4. Tar. Gz
$The tar xf - PIP - 1.5.4. Tar. Gz
$python setup.py install
$pip install requests
#Install before compiling:
#cd /root/webrtc/apprtc
#grunt build
Copy the code

Activation:

#***.136: Indicates the Intranet IP addressNohup dev_appserver. Py -- -- host = 172.17.250.108 / root/webrtc/apprtc/out/app_engine - skip_sdk_update_check &#Prompt for update: n

Copy the code
#checkRoot @ iZ2ze72sd9zx4b6hr6zz1iZ: ~ # netstat ntulp | grep TCP 8080 0 0 172.17.250.108:8080 0.0.0.0: * LISTEN 7628 / python#Output the followingroot@iZ2ze72sd9zx4b6hr6zz1iZ:~# lsof -i:8080 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME python 7628 root 8u IPv4  932360 0t0 TCP iZ2ze72sd9zx4b6hr6zz1iZ:http-alt (LISTEN)Copy the code

4, nginx

Reverse proxy apPRTC to support HTTPS access. If HTTP accesses APPRTC directly, the client cannot start video and audio collection (HTTPS access must be used).

Wget HTTP: / / http://nginx.org/download/nginx-1.15.6.tar.gz
#Run PCRE:apt-get install libpcre3-dev in the nginx directory
./configure --with-http_ssl_module
make install
#The default installation is /usr/local/nginx (can also use prefix)
#Configure nginx. Conf
vim /usr/local/nginx/conf/nginx.conf
#The content is as follows (note that you need to change your own public IP address)

Copy the code
#39.***.235 is my server address. } http{ upstream roomserver { server 39.***.235:8080; } server { listen 80; server_name 39.***.235; return 301 https://$server_name$request_uri; } server { root /usr/share/nginx/html; index index.php index.html index.htm; listen 443 ssl; ssl_certificate /cert/cert.pem; ssl_certificate_key /cert/key.pem; server_name 39.***.235; location / { proxy_pass http://roomserver$request_uri; proxy_set_header Host $host; } location ~ .php$ { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }}}Copy the code

Activation:

/usr/local/nginx/sbin/nginx

Browser call cross-domain problem :pushState

Messages:Failed to start signaling: Failed to execute ‘pushState’ on ‘History’

vim /root/webrtc/apprtc/out/app_engine/js/apprtc.debug.js
#Search pushState added:
roomLink=roomLink.substring("http","https");
Copy the code

Four services need to be started each time a server is restarted: