preface

In the previous WebRTC introduction, we have introduced the compilation of WebRTC and successfully run WebRTC in Android Studio. For more details, please click on the

WebRTC Import Android Studio.

In the following learning process, we will further use WebRTC to achieve real-time communication dialogue on Android. In order to facilitate the subsequent development and testing, today we will set up WebRTC server environment.

Installation environment

My cloud server is Ubuntu 16.04.

Note that try to use the same version of the system with the author, otherwise it may be because of the installation of different versions of the environment and cause the build failure

Ports 3478, 8080, 8089, 80, and 443 need to be opened in the background of the cloud server

This server setup tutorial is mainly from the WebRTC open source project APPRTC: github.com/webrtc/appr…

1. Install JDK

#If it's a brand new system then apt needs to be updated
apt-get update 
apt-get upgrade
#Install JDK8
apt-get install openjdk-8-jdk 
#Check whether Java is successfully installed. If the version number is displayed, the installation is successful
java -version
Copy the code

Install Node.js

apt install nodejs-legacy

#Take a look at version V4.2.6
node --version

apt-get install npm 
#Take a look at version 3.5.2
npm --version

#When installing gnURT, bring the version number. Otherwise, an error message may be displayed indicating that the Node version is too earlyNPM - g install [email protected]#Check grunt-cli v1.3.2
grunt --version
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 GO

apt install golang-go

#Check the version go Version GO1.6.2 Linux/AMd64
go version 

#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
#Refresh the environment variables
source /etc/profile

Copy the code

When using the vi command line, we may experience the problem of arrow keys mispositioning, because ubuntu is pre-installed tiny version, so we will have this inconvenience when using, but after we install vim full version, the keyboard keys under vi is normal.

Tiny sudo apt-get remove vim-common tiny sudo apt-get install vim

5. Install google_AppEngine

#Current directory :root/webrtc/Wget HTTP: / / https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.40.zip#Installed unzipApt-get install unzip unzip 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

Install libevent

#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 make installCopy the code

7. Install apPRTC

#Current directory :root/webrtc/
#If you don't have Git, you need to install git
git clone git://github.com/webrtc/apprtc.git
#Soft link collider's source code to go's working directory
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
#To compile ColliderMain, you need to know how to surf the web
go get collidermain
go install collidermain
Copy the code

When executing go get Collidermain, this collidermain may not be downloaded due to well-known reasons.

# error: package golang.org/x/net/websocket: unrecognized import path "golang.org/x/net/websocket"Copy the code

In this case, just use Git alone to download the Net:

mkdir -p $GOPATH/src/golang.org/x/ cd $GOPATH/src/golang.org/x/ git clone git://github.com/golang/net.git net go install Net # Retrieve colliderMain go get collidermain go install ColliderMainCopy 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

Configure the running server

1. Coturn Nat penetrates the server

Note that you need to set the user name and password, which will be used when configuring apPRTC later.

#Start the Intranet IP, which starts with 172 and can be queried in the background of your cloud server. For example, the author's IP is 172.16.16.4, the user name is Flyer, and the password is 123456Nohup Turnserver-L 172.16.16.4-A-U Flyer: 123456-V-f-r nort.gov &
#Run the lsof -i:3478 command to check whether the startup is successful. If the startup is successful, the following information is displayed
#COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
#turnserve 14754 root 16u IPv4 112232 0t0 UDP instance-5i00bk4l:3478 
#turnserve 14754 root 17u IPv4 112233 0t0 UDP instance-5i00bk4l:3478 
#turnserve 14754 root 32u IPv4 112263 0t0 TCP instance-5i00bk4l:3478 (LISTEN)
#turnserve 14754 root 33u IPv4 112267 0t0 TCP instance-5i00bk4l:3478 (LISTEN)
Copy the code

2. Collider signaling server

Configure the firewall to allow access to port 8089 (TCP, used by the client and Collider to establish WebSocket signaling communication)

#Create a self-signed digital certificate. If openSSL is not available, 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:// public IP address of your server :8080" &
#Run the lsof -i:8089 command to check whether the startup is successful. If the startup is successful, information similar to the following is displayed
#COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
#colliderm 14817 root 3u IPv6 112969 0t0 TCP *:8089 (LISTEN)
Copy the code

3. Set up apPRTC room server

Mainly is to modify the configuration file/root/webrtc apprtc/SRC/app_engine/the py the relevant parameter can be:

Note that you need to use the same user name and password that you set when configuring coturn Nat to penetrate the server

First modify the traversal server configuration:

Before the change#ICE_SERVER_OVERRIDE = [
#   {
#     "urls": [
#       "turn:hostname/IpToTurnServer:19305? transport=udp".
#       "turn:hostname/IpToTurnServer:19305? transport=tcp"
#].
#     "username": "TurnServerUsername".
#     "credential": "TurnServerCredentials"
#   },
#   {
#     "urls": [
#       "stun:hostname/IpToStunServer:19302"
#     ]
#   }
# ]

#The modifiedICE_SERVER_OVERRIDE = [ { "urls": ["turn: public IP address of your server :3478? Transport = UDP ", "turn: public IP address of your server :3478? Transport = TCP "], "username": "Coturn Nat passwd ", "credential":" Coturn Nat passwd "}, {"urls": ["stun: your server's public IP address :3478"]}]Copy the code

Then modify the ICE configuration:

#Before modifying
ICE_SERVER_BASE_URL = 'https://appr.tc'
#The modifiedICE_SERVER_BASE_URL = 'https://public IP address of your server'Copy the code

Last modified WSS_INSTANCE_HOST_KEY:


#Before the change
WSS_INSTANCE_HOST_KEY = 'host_port_pair'
WSS_INSTANCE_NAME_KEY = 'vm_name'
WSS_INSTANCE_ZONE_KEY = 'zone'
WSS_INSTANCES = [{
    WSS_INSTANCE_HOST_KEY: 'apprtc-ws.webrtc.org:443',
    WSS_INSTANCE_NAME_KEY: 'wsserver-std',
    WSS_INSTANCE_ZONE_KEY: 'us-central1-a'
}, {
    WSS_INSTANCE_HOST_KEY: 'apprtc-ws-2.webrtc.org:443',
    WSS_INSTANCE_NAME_KEY: 'wsserver-std-2',
    WSS_INSTANCE_ZONE_KEY: 'us-central1-f'
}]

#The modifiedWSS_INSTANCE_HOST_KEY = 'Public IP address of your server :8089' WSS_INSTANCE_NAME_KEY = 'vm_name' WSS_INSTANCE_ZONE_KEY = 'zone' WSS_INSTANCES = [{WSS_INSTANCE_HOST_KEY: 'public IP address of your server :8089', WSS_INSTANCE_NAME_KEY: 'wsserver-std', WSS_INSTANCE_ZONE_KEY: 'us-central1-a' }]Copy the code

Save the modification.

Compile the apprtc

#compile
cd /root/webrtc/apprtc

#The following step may cause an error indicating that the node version is too early
#NPM install -g n stable NPM install -g n stable sudo n stable
npm install --no-fund
#This step may report error, the following solution
grunt build
Copy the code

When grunt build is performed, it is possible that the requests module does not exist.

Error message:

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 import requests ImportError: No module named requests Use –force to continue.

Aborted due to warnings.

## # # # PIP installationCD/root/webrtc wget HTTP: / / https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --no-check-certificate chmod +x setuptools-0.6c11-py2.7.egg./setuptools-0.6c11-py2.7.egg wget https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz tar xf - PIP - 1.5.4. Tar. Gz CD PIP - 1.5.4 python setup. Py install pip install requests#Compile after the installation is complete:
cd /root/webrtc/apprtc
#Re-execute the compilation
grunt build

Copy the code

Start the apprtc

#Note that the following Intranet IP addresses start with 172Nohup dev_appserver. Py -- -- host = server network IP/root/webrtc/apprtc/out/app_engine - skip_sdk_update_check &Copy the code

At this stage, the WebRTC server is basically completed. At this time, enter http://server public IP address :8080/ in your browser. If it can be successfully opened, congratulations on your environment is successfully built!!

4. Install nginx agent HTTPS

Why do you need to install Nginx? Chrome does not support opening audio/video devices in non-HTTPS mode. Also, the signaling server (Collider) in apPRTC does not support non-HTTPS services.

However, we used HTTP services in the previous configuration, and did not configure HTTPS directly. Therefore, we need to use nginx as a proxy to implement HTTPS services. Of course, if we use HTTPS directly in the previous configuration, this step is not necessary, but we need to have a third party certificate instead of using a private certificate.

Download and install nginx:

#Download nginxWGWT http://nginx.org/download/nginx-1.16.1.tar.gz#Unpack theThe tar XVF nginx - 1.16.1. Tar. Gz#Installing PCREApt-get install libpcre3-dev CD nginx-1.16.1./configure --with-http_ssl_module make install#The default installation is /usr/local/nginx
#Configure nginx. Conf
vim /usr/local/nginx/conf/nginx.conf
Copy the code

Modify the nginx configuration/usr/local/nginx/conf/nginx. Conf:

events { worker_connections 1024; } HTTP {upstream roomServer {server Your server's public IP address :8080; } server { listen 80; Server_name Public IP address of your server; 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 4 Public IP address of your server; 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

Nginx start:

/usr/local/nginx/sbin/nginx
Copy the code

At this time, enter the server IP again in the browser to open the APPRTC, after opening something like this:

Then click to join the room, allow related permissions to see their own images through the camera…

Poking fun at two sentences

I have to say that preparing a WebRTC development and test environment is a bit of a hole. Although it is not difficult to follow the steps, there is too much to build and it is very easy to make mistakes.

The author also tried for several days, looked up a lot of information to do a lot of attempts to build a successful. Finally, for the Internet people’s network can not be connected and WTF vented…

‘!!!!!!

Pay attention to me, progress together, life is more than coding!!