Fork me on GitHub

WebRTC服务器——Licode 环境搭建

WebRTC服务器——Licode 环境搭建

系统配置

阿里云服务器

Ubuntu 14.04.5 LTS

Docker 环境搭建

在一台空的机器上搭建docker环境,先要安装docker,执行下面的命令即可:

apt-get update
apt-get install docker.io

执行下面的shell脚本:

#!/bin/sh

MIN_PORT=30000;
MAX_PORT=30050;
docker run --name licode -p  3000:3000 -p $MIN_PORT-$MAX_PORT:$MIN_PORT-$MAX_PORT/udp -p 3001:3001  -p 8080:8080 -e "MIN_PORT=$MIN_PORT" -e "MAX_PORT=$MAX_PORT" -e "PUBLIC_IP=your_ip_address" lynckia/licode

没错任何错误的话,licode服务会自动运行起来,查看port:

root@test:/git/docker# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      865/sshd        
tcp6       0      0 :::8080                 :::*                    LISTEN      4732/docker-proxy
tcp6       0      0 :::3000                 :::*                    LISTEN      4496/docker-proxy
tcp6       0      0 :::3001                 :::*                    LISTEN      4856/docker-proxy

有几点注意

  1. 需要把相关端口在阿里云服务器上打开,这里用到的端口有:
  • TCP 端口:8080 3000 3001
  • UDP 端口:30000/30050 UDP端口需要按照官方文档写,我自己开始使用了另外一个范围的端口,死活跑不起来。
  1. 不要用国外的服务器,这点当初没有注意到,开始在一台香港的服务器上搭建,一直报一个TURN服务器的问题,后来在国内的机器上的试了一下才通过,应该是TURN服务器穿透的时候有问题。

最后可以在Chrome或者火狐浏览器中输入地址:

http://your_ip_address:3001

作者在一台Mac和一台andorid上测试的,看一下效果,就不露脸了~~

编译环境搭建

先按照官方文档的步骤操作:

克隆代码

git clone https://github.com/lynckia/licode.git
cd licode

安装依赖

安装Licode依赖的组件:

./scripts/installUbuntuDeps.sh

安装Licode

安装LIcode的各个组件:

./scripts/installNuve.sh
./scripts/installErizo.sh  

安装实例

./scripts/installBasicExample.sh

配置环境

因为是在阿里云服务器上搭建环境,有些关于服务器地址的配置需要修改,在licode_config.js中把下面的字段改成阿里云服务器的外网IP或者域名:

config.erizoController.publicIP = 'your_ip_address'; 
config.erizoController.hostname = 'your_ip_address';
config.erizoAgent.publicIP = 'your_ip_address'; //default value: ''

改一下网卡的名字,一般都是eth0:

config.erizoAgent.networkinterface = 'eth0'; //default value: '':w

运行Licode

启动Licode服务:

./scripts/initLicode.sh

启动Licode实例:

./scripts/initBasicExample.sh

关于配置的一些问题

在浏览器里面输入下面的URL之后:

http://your_ip_address:3001

浏览器会报下面的错误:

ICE failed, add a TURN server and see about:webrtc for more details

看一下licode的配置文件licode_config.js,配置ICE server的地方:

 config.erizoController.iceServers = [{'url': 'stun:stun.l.google.com:19302'}]; // default value: [{'url': 'stun:stun.l.google.com:19302'}]

只有一个STUN服务器链接,看来是需要自己添加一个TURN服务器,作者自己在本地搭建了一个coturn服务器,具体流程可以参考此链接,作者采用的是编译安装。

配置coturn服务

在网络上搜索了很多资料来配置coturn,很多都没有成功,下面是作者自己机器上的配置:

#如果多网卡,记得此处设置为和你所用监听的IP相对应的eth 
listening-device=eth0
listening-ip=your_local_address
external-ip=your_external_address
listening-port=3478
relay-device=eth0
min-port=30000
max-port=30050
Verbose
fingerprint
#webrtc需要使用此选项 
lt-cred-mech
#use-auth-secret
#static-auth-secret=123456
#之前turnadmin中-r参数的值,此处要对应 
realm=demo
stale-nonce
#可以添加用户名和密码 
user=demo:123456

#测试期间可以使用example/etc中的pem,自己计算的话需要用到openssl,方法为: #sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/    turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes 

#填写pem目录即可,如 
#cert=/root/cert/_cert.pem
cert=/root/cert/mycert.pem
#pkey=/root/cert/_pkey.pem
pkey=/root/cert/mycert.key
no-loopback-peers
no-multicast-peers
mobility
no-cli

关于如何添加用户名和密码,如何配置证书需要自行解决。

启动coturn服务

执行下面的命令:

turnserver -c path/turnserver.conf

碰到下面一堆log不要惊慌,这不是错误的log,只是在监听事件:

1112: timer_event_handler: timeout 0xc449b0: timer_handler
1113: timer_event_handler: timeout 0xc461b0: rtcp_map_timeout_handler
1113: timer_event_handler: timeout 0x7f2acc001c90: timer_handler
1113: timer_event_handler: timeout 0x7f2acc002ea0: timer_timeout_handler
1113: timer_event_handler: timeout 0x7f2ac4001c90: timer_handler
1113: timer_event_handler: timeout 0x7f2ac4002ea0: timer_timeout_handler
1113: timer_event_handler: timeout 0x7f2ac8001d30: timer_handler

修改Licode配置文件

添加了turn 选项:

config.erizoController.iceServers = [{'url': 'stun:stun.l.google.com:19302'},{'url':'turn:your_external_address:3478','username':'demo','credential':'123456'}]; // 

最后重启licode服务和实例,在地址栏在此输入地址,如果请求TURN穿透,coturn服务会有类似下面的log:

86: read_client_connection:4436:start
86: read_client_connection: data.buffer=0x7f26040033ec, data.len=100
86: session 000000000000000005: peer 10.0.42.1 lifetime updated: 300
86: session 000000000000000005: realm <demo> user <demo>: incoming packet CREATE_PERMISSION processed, success
86: write_client_connection:4220:start
86: write_client_connection: prepare to write to s 0x7f2604028170
86: write_client_connection:4243:end
86: read_client_connection:4542:end
86: udp_server_input_handler:666:end
86: udp_server_input_handler:628:start
86: read_client_connection:4436:start
86: read_client_connection: data.buffer=0x7f26040033ec, data.len=140
86: handle_turn_send:2916:start
86: handle_turn_send:3011:end
86: session 000000000000000005: realm <demo> user <demo>: incoming packet SEND processed, success
86: udp_server_input_handler:666:end
86: udp_server_input_handler:628:start
86: read_client_connection:4436:start
86: read_client_connection: data.buffer=0x7f260401816c, data.len=140

浏览器控制台上有时会有下面的log:

  ICE failed, your TURN server appears to be broken, see about:webrtc for more details

这是你的turnserver没有配置好,如果连接成功,最终浏览器控制台会有下面的链接成功的log:

DEBUG:  Event: ice-state-change erizo.js:28:39
DEBUG:  Event: icestatechanged erizo.js:28:39
INFO:  341271403301651300 - iceConnectionState: connected erizo.js:28:39
DEBUG:  Event: onAddStream erizo.js:28:39
DEBUG:  Event: stream-added erizo.js:28:39
DEBUG:  Event: signaling_message_erizo

相关文章:
如何构建大规模的端到端加密的群组视频通话

posted @ 2019-01-05 22:40  HarlanC  阅读(11040)  评论(5编辑  收藏  举报