webrtc环境搭建

本文使用apprtc搭建webrtc.

系统:ubuntu20.04

外网IP:xxx.xxx.xxx.xxx

内网IP: 192.168.0.96

1 环境搭建

webrtc的服务器包括:房间服务器(apprtc)、信令服务器(collider)、防火墙打洞服务器(coturn)、web/websocket代理服务器(nginx)。

1.1 coturn

安装

apt update 
apt install coturn

配置

> vi /etc/default/coturn
TURNSERVER_ENABLED=1
> vi /etc/turnserver.conf 
listening-port=3478
tls-listening-port=5349
listening-ip=192.168.0.96
relay-ip=192.168.0.96
external-ip=xxx.xxx.xxx.xxx
server-name=xxx.xxx.xxx.xxx
realm=xxx.xxx.xxx.xxx
lt-cred-mech
user=ouyang:123456
userdb=/var/lib/turn/turndb
cert=/usr/local/etc/turn_server_cert.pem
pkey=/usr/local/etc/turn_server_pkey.pem
no-stdout-log
no-stun
log-file=/var/tmp/turnserver.log
pidfile="/var/run/turnserver.pid"

重启

service coturn restart

测试

turnadmin -a -u ouyang -r xxx.xxx.xxx.xxx  -p 123456

turnutils_uclient -v -t -T -u ouyang -w 123456 xxx.xxx.xxx.xxx

1.2 apprtc

1.1.1 安装依赖环境

# 安装nodejs
sudo apt install nodejs

# 安装npm
sudo apt-get install npm

# 安装grunt
sudo npm -g install grunt-cli

# 安装python-pip模块
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py --insecure
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
tar -xf pip-9.0.1.tar.gz
cd pip-9.0.1
sudo python setup.py install
sudo ln -s /usr/local/python27/bin/pip /usr/bin/pip

# 安装requests
pip install certifi urllib3 idna chardet
pip install openssl-python
git clone git://github.com/kennethreitz/requests.git
cd requests/
sudo python setup.py install

# 安装GoogleAppEngine 
sudo pip install WebTest
wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.50.zip
unzip google_appengine_1.9.50.zip
vi /etc/profile
# 添加”export PATH=$PATH:/home/webrtc/google_appengine”
source /etc/profile

# 安装go
sudo apt install golang-go
# 检测go版本 
go version
# 创建go工作目录
mkdir -p ~/webrtc/goworkspace/src
# 配置环境变量:在/etc/profile文件最后增加一行:
vim /etc/profile
# 添加"export GOPATH=/home/lqf/webrtc/goworkspace"
source /etc/profile

1.1.2 apprtc安装配置

# 代码下载
git clone https://github.com/webrtc/apprtc
# 链接collider源码
ln -s ~/webrtc/apprtc/src/collider/collider $GOPATH/src
ln -s /home/ouyang/webrtc/apprtc/src/collider/collidermain $GOPATH/src
ln -s /home/ouyang/webrtc/apprtc/src/collider/collidertest $GOPATH/src
# 编译collidermain
mkdir -p $GOPATH/src/golang.org/x/
cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/net.git
go get collidermain
go install collidermain

# 配置证书文件
mkdir /cert
cd /cert
mv xxx.cert cert.pem
mv xxx.key key.pem

# 运行collidermain
$GOPATH/bin/collidermain -port=8089 -tls=false -room-server="http://xxx.xxx.xxx.xxx:8080"


安装apprtc代码中的grunt依赖

cd apprtc
npm install
grunt build //编译

修改配置文件

主要是src/app_engine目录下的apprtc.py和constants.py文件。对于src/app_engine目录下的文件每次修改后需执行命令grunt build重新编译

修改文件:src/app_engine/constants.py

#ICE_SERVER_OVERRIDE = None

ICE_SERVER_OVERRIDE  = [
   {
     "urls": [
         "turn:xxx.xxx.xxx.xxx:3478?transport=udp",
         "turn:xxx.xxx.xxx.xxx:3478?transport=tcp"
     ],
     "username": "ouyang",
     "credential": "123456"
   },
   {
     "urls": [
       "stun:xxx.xxx.xxx.xxx:3478"
     ]
   }
 ]

WSS_INSTANCES = [{
    WSS_INSTANCE_HOST_KEY: 'xxx.xxx.xxx.xxx:8089',
    WSS_INSTANCE_NAME_KEY: 'ouyang',
    WSS_INSTANCE_ZONE_KEY: '123456'
}]

修改文件:src/app_engine/apprtc.py

# iceServers will be filled in by the TURN HTTP request.
def make_pc_config(ice_transports, ice_server_override):
  config = {
  #'iceServers': [],
  'iceServers': [{"urls":"stun:xxx.xxx.xxx.xxx"},{"urls":"turn:ouyang@xxx.xxx.xxx.xxx","credential":"123456"}],
  'bundlePolicy': 'max-bundle',
  'rtcpMuxPolicy': 'require'
  };
  if ice_server_override:
    config['iceServers'] = ice_server_override
  if ice_transports:
    config['iceTransports'] = ice_transports
  return config

编译

grunt build

运行房间服务器

dev_appserver.py --host=0.0.0.0 ./out/app_engine

在浏览器中访问房间服务器

http://10.8.6.196:8080

1.3 nginx

1.3.1 安装

sudo apt update
# 安装依赖库
sudo apt install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl

# 下载nginx
wget http://nginx.org/download/nginx-1.15.8.tar.gz
tar xvzf nginx-1.15.8.tar.gz
cd nginx-1.15.8
# 配置
./configure --with-http_ssl_module 
# 编译
make
# 安装
sudo make install

注:

默认安装目录: /usr/local/nginx

启动:sudo /usr/local/nginx/sbin/nginx

停止:sudo /usr/local/nginx/sbin/nginx -s stop

1.3.2 配置web代理

vi /usr/local/nginx/conf/conf.d/apprtc-https-proxy.conf

upstream roomserver {
   server xxx.xxx.xxx.xxx:8080;
}
server {
    listen 443 ssl;
    ssl_certificate /root/cert/cert.pem;
    ssl_certificate_key /root/cert/key.pem;
    charset utf-8;
    # ip地址或者域名
    server_name xxx.xxx.xxx.xxx;
    location / {
        # 转向代理的地址
        proxy_pass http://roomserver$request_uri;
        proxy_set_header Host $host;
    }
}

1.3.3 配置websocket代理

vi /usr/local/nginx/conf/conf.d/apprtc-websocket-proxy.conf

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server xxx.xxx.xxx.xxx:8089;
}

server {
    listen 8088 ssl;
    ssl_certificate /root/cert/cert.pem;
    ssl_certificate_key /root/cert/key.pem;

    server_name xxx.xxx.xxx.xxx;
    location /ws {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_connect_timeout 4s; 
        proxy_read_timeout 6000s; 
        proxy_send_timeout 6000s;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

1.3.4 运行

加入配置文件

vi /usr/local/nginx/conf/nginx.conf

include /usr/local/nginx/conf/conf.d/*.conf;

运行

# 停止
sudo /usr/local/nginx/sbin/nginx -s stop
# 启动
sudo /usr/local/nginx/sbin/nginx

1.4 bug

# 问题1

Messages:Failed to start signaling: Failed to execute 'pushState' on 'History'

解决:

vi apprtc/src/web_app/js/appcontroller.js

AppController.prototype.displaySharingInfo_ = function(roomId, roomLink) {
 roomLink=roomLink.replace("http","https");
 this.roomLinkHref_.href = roomLink;
 this.roomLinkHref_.text = roomLink;
 this.roomLink_ = roomLink;
 this.pushCallNavigation_(roomId, roomLink);
 this.activate_(this.sharingDiv_);
};

# 问题2

Firefox 无法建立到 wss://xxx.xxx.xxx.xxx:8088/ws 服务器的连接。

解决

在浏览器上添加例外:

xxx.xxx.xxx.xxx:8089
xxx.xxx.xxx.xxx:443

2 运行

# 重启coturn
service coturn restart

# 启动信令服务器
$GOPATH/bin/collidermain -port=8089 -tls=false -room-server="http://xxx.xxx.xxx.xxx:8080" &

# 启动房间服务器
dev_appserver.py --host=0.0.0.0 ./out/app_engine &

# 重启nginx
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx

3 测试

打开浏览器

https://xxx.xxx.xxx.xxx

参考:
https://zhuanlan.zhihu.com/p/80936998
https://blog.csdn.net/ts_dchs/article/details/97402024
https://blog.csdn.net/lingshengxueyuan/article/details/100519054
https://github.com/webrtc/apprtc
https://github.com/webrtc/apprtc/blob/master/src/collider/README.md

上一篇:企业级无延时直播来了,用了就回不去了


下一篇:【网络通信 -- WebRTC】WebRTC 基础知识 -- 基础知识总结