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
有幾點注意
- 需要把相關端口在阿里雲服務器上打開,這里用到的端口有:
- TCP 端口:8080 3000 3001
- UDP 端口:30000/30050 UDP端口需要按照官方文檔寫,我自己開始使用了另外一個范圍的端口,死活跑不起來。
- 不要用國外的服務器,這點當初沒有注意到,開始在一台香港的服務器上搭建,一直報一個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
最后放一個搭建好的licode環境鏈接:
webrtc實驗田