系统环境
Ubuntu 16.04
在搭建之前建议关闭系统防火墙,参考:Ubuntu使用Ufw管理防火墙
apt-get install git unzip lrzsz nodejs npm automake autoconf libtool nodejs-legacy python-webtest golang –y
从http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html下载对应的版本到/usr/lib/jvm
目录,然后解压到当前目录:
tar zxf jdk-8u151-linux-x64.tar.gz
编辑/etc/source
文件,在文件尾添加如下内容(因为jdk版本不同,文件夹名称可能会有差别):
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
命令让设置生效。
cd ~
git clone https://github.com/webrtc/apprtc.git
cd apprtc
npm -g install grunt-cli
npm install
grunt build --force
使用ifconfig
命令查看本机IP信息:
我使用的阿里云的云服器,服务器在NAT之后,所以172.18.163.203
是本地IP,即内网IP;从云服务器的管理页面可以看到该服务器的外网IP,如120.79.164.111
。如果使用的是阿里云等服务器还需要在安全策略中设置将所有进/出端口都映射到172.18.163.203
。
下面以外网IP 120.79.164.111
为例,根据实际外网IP进行对应修改。
apprtc/out/app_engine/constants.py
设置ICE服务器的IP端口、信令服务器(collider)的IP端口,修改之后的constants.py文件完整内容如下:
# 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/out/app_engine/apprtc.py
apprtc/out/app_engine/index_template.html
设置TURN服务器信息,在javascript脚本中添加servers变量:
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变量;
peerConnectionConfig: { "rtcpMuxPolicy":"require","iceServers":servers,"bundlePolicy":"max-bundle"},iceServerRequestUrl: '{{ ice_server_url }}',
// peerConnectionConfig: {{ pc_config | safe }},
从https://download.csdn.net/download/china_jeffery/10375696下载google_appengine.tar
,并使用tar
命令解压:
cd ~
unzip google_appengine.tar
cd ~
./google_appengine/dev_appserver.py --host 172.18.163.203 ./apprtc/out/app_engine --skip_sdk_update_check
172.18.163.203
为内网IP。
webrtc信令服务可以使用的apprtc自带的collider,这个服务是使用go语言开发。collider已经位于apprtc/src
目录中,我们将其拷贝到~/collider/src
目录中:
cd ~
mkdir collider/src -p
cp apprtc/src/collider/ collider/src/ -r
编辑~/collider/src/collidermain/main.go
文件,将room-server修改为服务器的外网IP和端口8080:
var roomSrv = flag.String("room-server", "http://120.79.164.111:8080", "The origin of the room server")
在安装go之前最好先卸载掉系统上已经安装的go版本:
apt-get remove golang
apt-get remove golang*
apt-get autoremove
然后使用wget
命令下载go:
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
是环境变量生效:
export GOROOT=/root/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/root/collider/
使用go命令进行collider安装:
cd ~/collider
go get collidermain
go install collidermain
cd ~
./collider/bin/collidermain -port=8089 -tls=false
webrtc的turn服务是为提供NAT穿透或者中转使用的,因为webrtc客户端可能会在不同的路由器后面或者多层路由器后面,那么他们如何通讯呢?都需要穿透,穿透的服务就是turn服务,webrtc推荐使用的coturn(以前也叫turnserver),它支STUN和Relay。
sudo apt-get install libssl-dev
sudo apt-get install libevent-dev
cd ~
git clone https://github.com/coturn/coturn.git
cd coturn
configure && make && make install
./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