系统环境 Ubuntu 16.04,在搭建之前建议关闭系统防火墙。

一. 房间服务器搭建

1.1 安装依赖程序

1
apt-get install git unzip  lrzsz  nodejs npm automake autoconf libtool nodejs-legacy python-webtest golang –y

1.2 安装jdk8

1.2.1 下载jdk8

此处下载对应的版本到/usr/lib/jvm目录,然后解压到当前目录:

1
tar zxf jdk-8u151-linux-x64.tar.gz

1.2.2 设置环境变量

编辑/etc/source文件,在文件尾添加如下内容(因为jdk版本不同,文件夹名称可能会有差别):

1
2
3
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_151
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

执行source /etc/profile命令让设置生效。

1.3 apprtc获取和安装

1.3.1 获取apprtc源码

1
2
cd ~
git clone https://github.com/webrtc/apprtc.git

1.3.2 安装apprtc

1
2
3
4
cd apprtc
npm -g install grunt-cli
npm install
grunt build --force

1.3.3 修改源码

使用ifconfig命令查看本机IP信息:

我使用的阿里云的云服器,服务器在NAT之后,所以172.18.163.203是本地IP,即内网IP;从云服务器的管理页面可以看到该服务器的外网IP,如120.79.164.111。如果使用的是阿里云等服务器还需要在安全策略中设置将所有进/出端口都映射到172.18.163.203

下面以外网IP 120.79.164.111为例,根据实际外网IP进行对应修改。

修改constants.py

constants.py路径:apprtc/out/app_engine/constants.py

设置ICE服务器的IP端口、信令服务器(collider)的IP端口,修改之后的constants.py文件完整内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2015 Google Inc. All Rights Reserved.

"""AppRTC Constants.

This module contains the constants used in AppRTC Python modules.
"""
import os

# Deprecated domains which we should to redirect to REDIRECT_URL.
REDIRECT_DOMAINS = [
'apprtc.appspot.com', 'apprtc.webrtc.org', 'www.appr.tc'
]
# URL which we should redirect to if matching in REDIRECT_DOMAINS.
REDIRECT_URL = 'https://appr.tc'

ROOM_MEMCACHE_EXPIRATION_SEC = 60 * 60 * 24
MEMCACHE_RETRY_LIMIT = 100

LOOPBACK_CLIENT_ID = 'LOOPBACK_CLIENT_ID'

# Turn/Stun server override. This allows AppRTC to connect to turn servers
# directly rather than retrieving them from an ICE server provider.
ICE_SERVER_OVERRIDE = None
# Enable by uncomment below and comment out above, then specify turn and stun
# 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"
# ]
# }
# ]

ICE_SERVER_BASE_URL = 'http://120.79.164.111:3033'
ICE_SERVER_URL_TEMPLATE = '%s/v1alpha/iceconfig?key=%s'
ICE_SERVER_API_KEY = os.environ.get('ICE_SERVER_API_KEY')

# Dictionary keys in the collider instance info constant.
WSS_INSTANCE_HOST_KEY = 'host_port_pair'
WSS_INSTANCE_NAME_KEY = 'vm_name'
WSS_INSTANCE_ZONE_KEY = 'zone'
WSS_INSTANCES = [{
WSS_INSTANCE_HOST_KEY: '120.79.164.111:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std',
WSS_INSTANCE_ZONE_KEY: 'us-central1-a'
}, {
WSS_INSTANCE_HOST_KEY: '120.79.164.111:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std-2',
WSS_INSTANCE_ZONE_KEY: 'us-central1-f'
}]

WSS_HOST_PORT_PAIRS = [ins[WSS_INSTANCE_HOST_KEY] for ins in WSS_INSTANCES]

# memcache key for the active collider host.
WSS_HOST_ACTIVE_HOST_KEY = 'wss_host_active_host'

# Dictionary keys in the collider probing result.
WSS_HOST_IS_UP_KEY = 'is_up'
WSS_HOST_STATUS_CODE_KEY = 'status_code'
WSS_HOST_ERROR_MESSAGE_KEY = 'error_message'

RESPONSE_ERROR = 'ERROR'
RESPONSE_ROOM_FULL = 'FULL'
RESPONSE_UNKNOWN_ROOM = 'UNKNOWN_ROOM'
RESPONSE_UNKNOWN_CLIENT = 'UNKNOWN_CLIENT'
RESPONSE_DUPLICATE_CLIENT = 'DUPLICATE_CLIENT'
RESPONSE_SUCCESS = 'SUCCESS'
RESPONSE_INVALID_REQUEST = 'INVALID_REQUEST'

IS_DEV_SERVER = os.environ.get('APPLICATION_ID', '').startswith('dev')

BIGQUERY_URL = 'https://www.googleapis.com/auth/bigquery'

# Dataset used in production.
BIGQUERY_DATASET_PROD = 'prod'

# Dataset used when running locally.
BIGQUERY_DATASET_LOCAL = 'dev'

# BigQuery table within the dataset.
BIGQUERY_TABLE = 'analytics'

修改apprtc.py

apprtc.py路径:apprtc/out/app_engine/apprtc.py



修改为

(大约位于146行左右)

修改index_template.html

index_template.html路径:apprtc/out/app_engine/index_template.html

设置TURN服务器信息,在javascript脚本中添加servers变量:

1
2
3
4
5
6
7
8
var servers=[{
credential:"helloword",
username:"helloword",
urls:[
"turn:120.79.164.111:3478?transport=udp",
"turn:120.79.164.111:3478?transport=tcp"
]
}];

并将peerConnectionConfig: {{ pc_config | safe }}修改为使用上面声明的servers变量;

1
2
peerConnectionConfig: { "rtcpMuxPolicy":"require","iceServers":servers,"bundlePolicy":"max-bundle"},iceServerRequestUrl: '{{ ice_server_url }}',
// peerConnectionConfig: {{ pc_config | safe }},

##1.4 获取google_appengine
https://download.csdn.net/download/china_jeffery/10375696下载google_appengine.tar,并使用tar命令解压:

1
2
cd ~
unzip google_appengine.tar

1.5 启动房间服务器

1
2
cd ~
./google_appengine/dev_appserver.py --host 172.18.163.203 ./apprtc/out/app_engine --skip_sdk_update_check

172.18.163.203为内网IP。

二. 信令服务器搭建

2.1 collider源码

Webrtc信令服务可以使用的apprtc自带的collider,这个服务是使用go语言开发。collider已经位于apprtc/src目录中,我们将其拷贝到~/collider/src目录中:

1
2
3
cd ~
mkdir collider/src -p
cp apprtc/src/collider/ collider/src/ -r

编辑~/collider/src/collidermain/main.go文件,将room-server修改为服务器的外网IP和端口8080:

1
var roomSrv = flag.String("room-server", "http://120.79.164.111:8080", "The origin of the room server")

2.2 安装go

在安装go之前最好先卸载掉系统上已经安装的go版本:

1
2
3
apt-get remove golang
apt-get remove golang*
apt-get autoremove

然后使用wget命令下载go:

1
wget https://storage.googleapis.com/golang/go1.5.1.linux-amd64.tar.gz

或者从https://download.csdn.net/download/china_jeffery/10375936下载。然后解压tar zxf go1.5.1.linux-amd64.tar.gz

添加如下环境变量到/etc/source文件尾,并使用source /etc/source是环境变量生效:

1
2
3
export GOROOT=/root/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/root/collider/

2.3 collider安装

使用go命令进行collider安装:

1
2
3
cd ~/collider
go get collidermain
go install collidermain

2.4 启动信令服务器

1
2
cd ~
./collider/bin/collidermain -port=8089 -tls=false

三. TURN服务器搭建

webrtc的turn服务是为提供NAT穿透或者中转使用的,因为webrtc客户端可能会在不同的路由器后面或者多层路由器后面,那么他们如何通讯呢?都需要穿透,穿透的服务就是turn服务,webrtc推荐使用的coturn(以前也叫turnserver),它支STUN和Relay。

3.1 安装依赖

1
2
sudo apt-get install libssl-dev
sudo apt-get install libevent-dev

3.2 下载并编译源码

1
2
3
4
cd ~
git clone https://github.com/coturn/coturn.git
cd coturn
configure && make && make install

3.3 启动TURN服务

1
./coturn/bin/turnserver --syslog -a -X 120.79.164.111 -E 172.18.163.203 -f --min-port=32355 --max-port=65535 --user=helloword:helloword -r helloword --cert=turn_server_cert.pem --pkey=turn_server_pkey.pem --log-file=stdout -v

120.79.164.111为外网IP,172.18.163.203为本地IP。
turnserver的参数见:

https://github.com/coturn/coturn/wiki/turnserver

本文最初发布在我的CSDN博客:《WebRTC – 官方apprtc示例搭建》