所有服务搭建在同一台centos服务器上(192.168.5.8),效果图:
搭建AppRTC
0、创建目录
mkdir ~/webrtc
cd ~/webrtc
安装需要的各种工具(除了apt之外还可以下载安装包或者源码自己编译安装):
1、安装JDK
#为了演示初始环境 已重装系统, 重装后先执行:
yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
#查看安装路径
dirname $(readlink $(readlink $(which java)))
#上一步得到安装路径/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin
vim /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
保存退出文件,然后执行一下使设置立即生效
source /etc/profile
测试是否可以正常
[liaoqingfu@bmms ~]$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode
2、安装node.js
yum install git
yum install ant
yum install -y nodejs
yum install npm
npm -g install grunt-cli
# 测试版本信息
node --version
显示v6.14.3
npm --version
显示3.10.10
grunt --version
显示grunt-cli v1.3.2
3、安装Python和Python-webtest (python2.7)
yum install python
yum install python-webtest
python -V
#Python 2.7.5
4、安装google_appengine
wget https://storage.googleapis.com/appengine-sdks/featured/google_appengine_1.9.40.zip
unzip google_appengine_1.9.40.zip
#配置环境变量:在/etc/profile文件最后增加一行:
export PATH=$PATH:/root/webrtc/google_appengine
# 生效
source /etc/profile
5、安装go
yum install epel-release -y
yum install go -y
go version
#go version go1.11.4 linux/amd64
#创建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/CoturnConfig
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make install
7、安装apprtc
#当前目录:root/webrtc/
git clone https://github.com/webrtc/apprtc.git
#将collider的源码软连接到go的工作目录下
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
#go get collidermain: 被墙
#报错: package golang.org/x/net/websocket: unrecognized import path "golang.org/x/net/websocket"
#执行:
#mkdir -p $GOPATH/src/golang.org/x/
#cd $GOPATH/src/golang.org/x/
#git clone https://github.com/golang/net.git net
#go install net
8、安装coturn
如果没有openssl,需要安装 yum install openssl-devel
#目录:root/webrtc/
#https://github.com/coturn/coturn/wiki/Downloads
wget http://coturn.net/turnserver/v4.5.0.7/turnserver-4.5.0.7.tar.gz
tar xfz turnserver-4.5.0.7.tar.gz
cd turnserver-4.5.0.7
./configure
make install
./configure --enable-static
安装https://github.com/coturn/coturn.git的版本更新,支持http访问:
nohup turnserver -L 192.168.5.8 -a -u root:123456 -v -f -r nort.gov > /root/webrtc/turnserver.log 2>&1 &
配置与运行
1、coturn Nat穿透服务器
配置防火墙,允许访问3478端口(含tcp和udp,此端口用于nat穿透)
#启动 192.168.5.8:内网ip(腾讯云后台可以看到内外网ip)
nohup turnserver -L 192.168.5.8 -a -u root:123456 -v -f -r nort.gov &
#账号 lqf 密码:123456 这一步随便给,但是后面配置apprtc时需要用到
#命令后加 & ,执行起来后按 ctr+c,不会停止
#开启新窗口 执行
netstat -ntulp | grep turnserver #或者 lsof -i:3478
#输出大致这样的成功
tcp 0 0 127.0.0.1:5766 0.0.0.0:* LISTEN 16848/turnserver
tcp 0 0 172.16.0.3:3478 0.0.0.0:* LISTEN 16848/turnserver
tcp 0 0 172.16.0.3:3478 0.0.0.0:* LISTEN 16848/turnserver
udp 0 0 172.16.0.3:3478 0.0.0.0:* 16848/turnserver
udp 0 0 172.16.0.3:3478 0.0.0.0:* 16848/turnserver
2、collider 信令服务器
配置防火墙,允许访问8089端口(tcp,用于客户端和collider建立websocket信令通信)
#创建自签名的数字证书
#如果没有openssl,需要安装 yum install openssl-devel
mkdir -p /cert
cd /cert
# CA私钥
openssl genrsa -out key.pem 2048
# 自签名证书
openssl req -new -x509 -key key.pem -out cert.pem -days 1095
# 执行collider 信令服务器
nohup $GOPATH/bin/collidermain -port=8089 -tls=true -room-server="http://192.168.5.8:8090" &
#同样检查是否成功
netstat -ntulp | grep collider
tcp6 0 0 :::8089 :::* LISTEN 16864/collidermain
3、apprtc 房间服务器
配置防火墙,允许访问8080端口(tcp,此端口用于web访问)
配置文件修改(主要是配置apprtc对应的conturn和collider相关参数)
vim /root/webrtc/apprtc/src/app_engine/constants.py
外网ip (外网的话,参考开头的第三个博客,我这个里用的都是一个IP地址: 192.168.5.8)
一定要注意IP是内网还是外网IP
修改后(填的都是外网IP)
# 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 = [
{undefined
"urls": [
"turn:192.168.5.8:3478?transport=udp",
"turn:192.168.5.8:3478?transport=tcp"
],
"username": "root",
"credential": "123456"
},
{undefined
"urls": [
"stun:192.168.5.8:8090"
]
}
]
ICE_SERVER_BASE_URL = 'https:192.168.5.8:8088'
ICE_SERVER_URL_TEMPLATE = '%s/v1alpha/iceconfig?key=%s'
ICE_SERVER_API_KEY = os.environ.get('ICE_SERVER_API_KEY')
HEADER_MESSAGE = os.environ.get('HEADER_MESSAGE')
ICE_SERVER_URLS = [url for url in os.environ.get('ICE_SERVER_URLS', '').split(',') if url]
# Dictionary keys in the collider instance info constant.
WSS_INSTANCE_HOST_KEY = '192.168.5.8:8089'
WSS_INSTANCE_NAME_KEY = 'vm_name'
WSS_INSTANCE_ZONE_KEY = 'zone'
WSS_INSTANCES = [{undefined
WSS_INSTANCE_HOST_KEY: '192.168.5.8:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std',
WSS_INSTANCE_ZONE_KEY: 'us-central1-a'
}]
#编译
cd /root/webrtc/apprtc
npm install
grunt build
#如果出现 : No module named requests
错误: 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.
Aborted due to warnings.
安装pip
下载setup-python工具
# 有一行命令太长了,$开头的是一行命令
$cd /root/webrtc
$wget https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg --no-check-certificate
$chmod +x setuptools-0.6c11-py2.7.egg
$./setuptools-0.6c11-py2.7.egg
$wget https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz
$tar -xf pip-1.5.4.tar.gz
cd pip-1.5.4/
$python setup.py install
$pip install requests
#安装完成后再执行编译:
#cd /root/webrtc/apprtc
#grunt build
启动:
#172.16.0.3 : 内网ip
# 默认端口是8080, 可以自己指定端口
nohup /root/webrtc/google_appengine/dev_appserver.py --host=0.0.0.0 --port=8090 /root/webrtc/apprtc/out/app_engine --skip_sdk_update_check &
nohup /root/webrtc/google_appengine/dev_appserver.py --host=0.0.0.0 --port=8080 /root/webrtc/apprtc/out/app_engine --skip_sdk_update_check &
#提示更新选择: n
此时可以通过火狐浏览器访问测试:
http://192.168.5.8:8080/
#检查
netstat -ntulp | grep 8080
#输出下列内容
tcp 0 0 192.168.5.8:8080 0.0.0.0:* LISTEN 17032/python
4、nginx
反向代理apprtc,使之支持https访问,如果http直接访问apprtc,则客户端无法启动视频音频采集(必须得用https访问)
#在nginx目录执行 PCRE: yum install pcre pcre-devel -y
wget http://nginx.org/download/nginx-1.17.9.tar.gz
tar zxf nginx-1.17.9.tar.gz
cd nginx-1.17.9/
./configure --with-http_ssl_module
make install
#默认安装在/usr/local/nginx(也可以执行prefix)
#配置nginx.conf
vim /usr/local/nginx/conf/nginx.conf
#内容如下(注意修改自己的公网ip)
events {undefined
worker_connections 1024;
}
http{undefined
upstream roomserver {undefined
server192.168.5.8:8080;
}
server {undefined
listen 80;
server_name 192.168.5.8;
return 301 https://$server_name$request_uri;
}
server {undefined
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.5.8;
location / {undefined
proxy_pass http://roomserver$request_uri;
proxy_set_header Host $host;
}
location ~ .php$ {undefined
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
域名的反向代理
websocket代理
mkdir conf.d(在/usr/local/nginx/conf/ 目录下)
vim websocket_proxy.conf
map $http_upgrade $connection_upgrade {undefined
default upgrade;
'' close;
}
upstream websocket {undefined
server 192.168.5.8:8089;
}
server {undefined
listen 8088;
ssl on;
ssl_certificate /home/liaoqingfu/cert/server.pem;
ssl_certificate_key /home/liaoqingfu/cert/_.lqf.com.key;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
server_name easywebrtc.com;
access_log /var/log/nginx/test_websocket.access.log;
location / {undefined
proxy_pass https://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
启动:
/usr/local/nginx/sbin/nginx
浏览器通话跨域问题 :pushState
Messages:Failed to start signaling: Failed to execute 'pushState' on 'History'
vim /root/webrtc/apprtc/out/app_engine/js/apprtc.debug.js
#搜索 pushState 增加:
roomLink=roomLink.substring("http","https");
最终结果(大概4200行的修改)
AppController.prototype.pushCallNavigation_ = function(roomId, roomLink) {undefined
if (!isChromeApp()) {undefined
roomLink=roomLink.substring("http","https");
window.history.pushState({"roomId":roomId, "roomLink":roomLink}, roomId, roomLink);
}
};
大概446行加上
roomLink=roomLink.replace("http","https");
如果ice none
.我这里用的iceserver,好像最新的webrtc demo用的是iceserver,所谓的iceserver其实就是请求一个url网址,返回一组打洞服务器地址,我这里改成这个
ICE_SERVER_BASE_URL = 'http://192.168.5.8'
完整的url是http://192.168.5.8/v1alpha/iceconfig/?key=north,这里的url需要自己做一个网站配置一下,理论上,如果只是测试,写成一个静态的index.html返回给客户端应该也是可以的。
url返回的json数据是:
{"iceServers":[{"username":"1473662094:ling","credential":"HbWuzrR0z\/Ei0wBPvHISXpdGvc4=","ttl":86400,"urls":["turn:192.168.5.8:3478?transport=udp","turn:192.168.5.8:3478?transport=tcp","turn:192.168.5.8:3479?transport=udp","turn:192.168.5.8:3479?transport=tcp"]},{"username":"1473662094:ling","credential":"HbWuzrR0z\/Ei0wBPvHISXpdGvc4=","ttl":86400,"urls":["turn:192.168.5.8:3478?transport=udp","turn:192.168.5.8:3478?transport=tcp","turn:192.168.5.8:3479?transport=udp","turn:192.168.123.231:3479?transport=tcp"]}]}
注意这里的urls,链接里面是uris,这是不同的。最新webrtc demo是用的urls解析的。另外mac版本的demo解析这个json和android不同,有bug,需要自己修复下。
这个网址可以检测ICE服务器的工作状态:
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
参考:https://blog.csdn.net/wangxudongx/article/details/105443625
参考:1)https://segmentfault.com/a/1190000019381873
2)http://www.devyk.top/2020/06/04/webrtc-%E5%AD%A6%E4%B9%A0-%E4%B8%80-%E6%90%AD%E5%BB%BA-AppRTC-%E6%9C%8D%E5%8A%A1/
3)https://www.yuque.com/docs/share/a12e0599-42e3-41f3-88d8-4c372a091856?#