一、环境安装

  1. 安装 java

    yum -y install java-1.8.0-openjdk*
  2. 安装 node.js

    curl -sL https://rpm.nodesource.com/setup_10.x | bash -yum install -y nodejs# 确认是否成功node --versionnpm --version# 构建 apprtc 用到npm -g install grunt-cligrunt --version
  3. 安装 python 和 python-webtest

    # 安装epel扩展源yum -y install epel-releaseyum install python python-webtest python-pippip install --upgrade pip
  4. 安装 google_appengine

    mkdir /root/webrtccd /root/webrtcwget 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_appengineexport PATH=$PATH:$GOOGLE_ENGINE_HOME

    使环境变量生效

    source /etc/profile
  5. 安装 go

    yum -y install go    #安装 Golang

    创建 go 工作目录

    mkdir -p /root/webrtc/goWorkspace/src

    在 /etc/profile 文件增加

    export GOPATH=/root/webrtc/goWorkspace

    保存退出后执行:

    source /etc/profile
  6. 安装 libevent (给穿透服务器用的)

    # 当前目录 /root/webrtc/# https://github.com/coturn/coturn/wiki/CoturnConfigcd /root/webrtc/wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gztar xvf libevent-2.0.21-stable.tar.gzcd libevent-2.0.21-stable./configuremake install
  7. 安装 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/srcln -s /root/webrtc/apprtc/src/collider/collidermain $GOPATH/srcln -s /root/webrtc/apprtc/src/collider/collidertest $GOPATH/src

    编译安装 collidermain(需要翻墙)

    go get collidermaingo install collidermain
  8. 安装 coturn

    # 当前目录 /root/webrtc/# https://github.com/coturn/coturn/wiki/Downloadscd /root/webrtc/wget http://coturn.net/turnserver/v4.5.0.7/turnserver-4.5.0.7.tar.gztar xvfz turnserver-4.5.0.7.tar.gzcd turnserver-4.5.0.7yum groupinstall "Development Tools"yum -y install openssl-devel./configuremake install

二、参数配置以及运行服务

目前服务全部安装在同一台服务器上面,内网 ip: 192.168.3.51,如果有外网ip, 可自行更改配置。
如果要开发 android 端,服务器最好能配上域名和一个与之对应认证的SSL证书。
  1. 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/turnservertcp        0      0 127.0.0.1:5766          0.0.0.0:*               LISTEN      12412/turnserverudp        0      0 192.168.3.51:3478      0.0.0.0:*                           12412/turnserverudp        0      0 192.168.3.51:3478      0.0.0.0:*                           12412/turnserverudp        0      0 192.168.3.51:3478      0.0.0.0:*                           12412/turnserverudp        0      0 192.168.3.51:3478      0.0.0.0:*                           12412/turnserver
  2. collider 信令服务(拥有外网地址)

    配置防火墙,允许访问8089端口(tcp,用于客户端和collider建立websocket信令通信)

    #创建自签名的数字证书#如果没有openssl,需要安装mkdir -p /certcd /cert# CA私钥openssl genrsa -out key.pem 2048 # 自签名证书openssl req -new -x509 -key key.pem -out cert.pem -days 1095nohup $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
  3. 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 stunICE_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,否则会出现错误1sudo npm install# 安装 python 依赖pip install -r requirements.txt# 由于 python 版本问题grunt -v build

    3.3 启动 apprtc 服务:

    # 内网ipnohup 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.jsConfiguring 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 ELIFECYCLEnpm ERR! errno 1npm ERR! closurecompiler@1.6.1 configure: `node scripts/configure.js`npm ERR! Exit status 1npm 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 ELIFECYCLEnpm ERR! errno 1npm ERR! closurecompiler@1.6.1 install: `npm run-script configure`npm ERR! Exit status 1npm 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) taskTraceback (most recent call last):  File "./build/build_app_engine_package.py", line 12, in <module>    import requestsImportError: No module named requestsWarning: Command failed: python ./build/build_app_engine_package.py src out/app_engineTraceback (most recent call last):  File "./build/build_app_engine_package.py", line 12, in <module>    import requestsImportError: 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-buildnode-pre-gyp ERR! Tried to download(404): https://node-iltorb.s3.amazonaws.com/iltorb/v1.3.1/node-v64-linux-x64.tar.gznode-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.gznode-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
  4. 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.rpmyum 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