1. 程式人生 > >阿里雲上搭建webRTC 伺服器——Licode

阿里雲上搭建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:


[email protected]:/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埠需要按照官方文件寫,我自己開始使用了另外一個範圍的埠,死活跑不起來。
  2. 不要用國外的伺服器,這點當初沒有注意到,開始在一臺香港的伺服器上搭建,一直報一個TURN伺服器的問題,後來在國內的機器上的試了一下才通過,應該是TURN伺服器穿透的時候有問題。

最後可以在Chrome或者火狐瀏覽器中輸入地址:


http://your_ip_address:3001

作者在一臺Mac和一臺andorid上測試的,看一下效果,就不露臉了~~
_2019_01_05_9_44_48

編譯環境搭建

先按照官方文件的步驟操作:

克隆程式碼

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