共计 7634 个字符,预计需要花费 20 分钟才能阅读完成。
一、环境安装
-
安装 java
yum -y install java-1.8.0-openjdk*
-
安装 node.js
curl -sL https://rpm.nodesource.com/setup_10.x | bash - yum install -y nodejs # 确认是否成功 node --version npm --version # 构建 apprtc 用到 npm -g install grunt-cli grunt --version
-
安装 python 和 python-webtest
# 安装 epel 扩展源 yum -y install epel-release yum install python python-webtest python-pip pip install --upgrade pip
-
安装 google_appengine
mkdir /root/webrtc cd /root/webrtc wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.40.zip # 安装 unzip 解压工具 yum -y install unzip unzip google_appengine_1.9.40.zip
配置环境变量,在 /etc/profile 文件最后增加下列代码:
export GOOGLE_ENGINE_HOME=/root/webrtc/google_appengine export PATH=$PATH:$GOOGLE_ENGINE_HOME
使环境变量生效
source /etc/profile
-
安装 go
yum -y install go #安装 Golang
创建 go 工作目录
mkdir -p /root/webrtc/goWorkspace/src
在 /etc/profile 文件增加
export GOPATH=/root/webrtc/goWorkspace
保存退出后执行:
source /etc/profile
-
安装 libevent (给穿透服务器用的)
# 当前目录 /root/webrtc/ # https://github.com/coturn/coturn/wiki/CoturnConfig cd /root/webrtc/ wget 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 install
-
安装 apprtc
# 当前目录 /root/webrtc/ cd /root/webrtc/ git clone https://github.com/webrtc/apprtc.git # 将 collider 的源码软连接到 go 的工作目录下 (ln -s 软链接,快捷方式) 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
编译安装 collidermain(需要翻墙)
go get collidermain go install collidermain
-
安装 coturn
# 当前目录 /root/webrtc/ # https://github.com/coturn/coturn/wiki/Downloads cd /root/webrtc/ wget 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 yum groupinstall "Development Tools" yum -y install openssl-devel ./configure make install
二、参数配置以及运行服务
目前服务全部安装在同一台服务器上面,内网 ip: 192.168.3.51,如果有外网 ip, 可自行更改配置。
如果要开发 android 端,服务器最好能配上域名和一个与之对应认证的 SSL 证书。
-
coturn Nat 穿透服务器
# 启动 (内网 ip) # 账号 tina 密码:12345 这一步随便给,但是后面配置 apprtc 时需要用到 # 命令后加 & , 执行起来后按 ctr+c, 不会停止 nohup turnserver -L 192.168.3.51 -a -u shepherd:123456 -v -f -r nort.gov > /root/webrtc/turnserver.log 2>&1 &
检查端口确定服务是否正常
netstat -ntulp |grep turnserver
输出 3478 与 5766 端口
tcp 0 0 192.168.3.51:3478 0.0.0.0:* LISTEN 12412/turnserver tcp 0 0 127.0.0.1:5766 0.0.0.0:* LISTEN 12412/turnserver udp 0 0 192.168.3.51:3478 0.0.0.0:* 12412/turnserver udp 0 0 192.168.3.51:3478 0.0.0.0:* 12412/turnserver udp 0 0 192.168.3.51:3478 0.0.0.0:* 12412/turnserver udp 0 0 192.168.3.51:3478 0.0.0.0:* 12412/turnserver
-
collider 信令服务(拥有外网地址)
配置防火墙,允许访问 8089 端口(tcp,用于客户端和 collider 建立 websocket 信令通信)
# 创建自签名的数字证书 #如果没有 openssl, 需要安装 mkdir -p /cert cd /cert # CA 私钥 openssl genrsa -out key.pem 2048 # 自签名证书 openssl req -new -x509 -key key.pem -out cert.pem -days 1095 nohup $GOPATH/bin/collidermain -port=8089 -tls=true -room-server="http://192.168.3.51:8080" > /root/webrtc/collidermain.log 2>&1 &
检查端口
netstat -ntulp | grep collider
输出 8089 端口
tcp6 0 0 :::8089 :::* LISTEN 12827/collidermain
-
apprtc 房间服务器(8080 端口为房间服务,8000 端口为管理员服务)
3.1 修改配置文件(主要是配置 apprtc 对应的 conturn 和 collider 相关参数)
vim /root/webrtc/apprtc/src/app_engine/constants.py
#ICE_SERVER_OVERRIDE = None # Enable by uncomment below and comment out above, then specify turn and stun ICE_SERVER_OVERRIDE = [ { "urls": [ "turn:192.168.3.51:3478?transport=udp", "turn:192.168.3.51:3478?transport=tcp" ], "username": "shepherd", "credential": "123456" } ] ICE_SERVER_BASE_URL = 'https://192.168.3.51' 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 = '192.168.3.51:8089' WSS_INSTANCE_NAME_KEY = 'vm_name' WSS_INSTANCE_ZONE_KEY = 'zone' WSS_INSTANCES = [{ WSS_INSTANCE_HOST_KEY: '192.168.3.51:8089', WSS_INSTANCE_NAME_KEY: 'wsserver-std', WSS_INSTANCE_ZONE_KEY: 'us-central1-a' }]
3.2 执行编译
cd /root/webrtc/apprtc # 一定要加 sudo,否则会出现错误 1 sudo npm install # 安装 python 依赖 pip install -r requirements.txt # 由于 python 版本问题 grunt -v build
3.3 启动 apprtc 服务:
# 内网 ip nohup dev_appserver.py --host=192.168.3.51 /root/webrtc/apprtc/out/app_engine --skip_sdk_update_check > /root/webrtc/apprtc.log 2>&1 &
错误 1: closurecompiler 依赖报错
> closurecompiler@1.6.1 install /root/webrtc/apprtc/node_modules/closurecompiler > npm run-script configure > closurecompiler@1.6.1 configure /root/webrtc/apprtc/node_modules/closurecompiler > node scripts/configure.js Configuring ClosureCompiler.js 1.6.1 ... Downloading https://dl.google.com/closure-compiler/compiler-latest.tar.gz ... events.js:174 throw er; // Unhandled 'error' event ^ Error: EACCES: permission denied, open '/root/webrtc/apprtc/node_modules/closurecompiler/compiler/compiler.tar.gz' Emitted 'error' event at: at lazyFs.open (internal/fs/streams.js:272:12) at FSReqWrap.args [as oncomplete] (fs.js:140:20) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! closurecompiler@1.6.1 configure: `node scripts/configure.js` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the closurecompiler@1.6.1 configure script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm WARN Local package.json exists, but node_modules missing, did you mean to install? npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"}) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! closurecompiler@1.6.1 install: `npm run-script configure` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the closurecompiler@1.6.1 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2019-05-22T07_11_46_138Z-debug.log
错误 2 : requests 模块不存在
Running "shell:buildAppEnginePackage" (shell) task Traceback (most recent call last): File "./build/build_app_engine_package.py", line 12, in <module> import requests 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.
解决办法:安装这个依赖
pip install requests
错误 3:Warning: Error: Unable to access jarfile undefined
可能原因: 1. JDK 没有安装,或者没有 JAVA_HOME 环境变量 2. grunt 的版本要至少 1.3.2 以上
错误 4:iltorb@1.3.1 安装错误
> iltorb@1.3.1 install /root/webrtc/apprtc/node_modules/iltorb > node-pre-gyp install --fallback-to-build node-pre-gyp ERR! Tried to download(404): https://node-iltorb.s3.amazonaws.com/iltorb/v1.3.1/node-v64-linux-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for iltorb@1.3.1 and node@10.15.3 (node-v64 ABI) (falling back to source compile with node-gyp) node-pre-gyp ERR! Tried to download(undefined): https://node-iltorb.s3.amazonaws.com/iltorb/v1.3.1/node-v64-linux-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for iltorb@1.3.1 and node@10.15.3 (node-v64 ABI) (falling back to source compile with node-gyp)
解决办法:单独拿出来安装(可能是网络原因)
npm install iltorb # 再执行 sudo npm install
-
Nginx
反向代理 apprtc,使之支持 https 访问,如果 http 直接访问 apprtc,则客户端无法启动视频音频采集(必须得用 https 访问)
# 添加 yum 源 rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm yum install nginx
配置 nginx.conf
events {worker_connections 1024;} http{ upstream roomserver {server 192.168.3.51:8080;} server { listen 80; server_name 192.168.3.51; 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 192.168.3.51; 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; } } }
启动:
/usr/local/nginx/sbin/nginx
三、其他错误
错误 1: 浏览器通话跨域问题
浏览器启动视频时会出现这个错误提示
浏览器通话跨域问题 :pushState
Messages:Failed to start signaling: Failed to execute ‘pushState’ on ‘History’
解决办法:
# 可以在编译前的代码
# src/web_app/js/appcontroller.js
# 或者在编译后的代码上面
vim /root/webrtc/apprtc/out/app_engine/js/apprtc.debug.js
# 搜索 pushState 增加:
roomLink=roomLink.substring("http","https");
参考文章:
Ubuntu 下安装 WebRTC