webrtc服務(wù)器搭建:(參考文獻(xiàn))
基于外網(wǎng):
http://www.reibang.com/p/5431a7066f47
http://linxunfeng.top/2018/06/07/AppRTC%E5%AE%9E%E6%88%98/
https://blog.csdn.net/china_jeffery/article/details/80092309
https://blog.csdn.net/gladsnow/article/details/77900578
http://www.reibang.com/p/7fd28c26a2cb
基于局域網(wǎng):
https://blog.csdn.net/gladsnow/article/details/77900333
https://blog.csdn.net/s569646547/article/details/50780682
https://blog.csdn.net/lamb7758/article/details/77045735
操作系統(tǒng):Ubuntu 16.04?
IP地址:局域網(wǎng) 192.168.6.54
1芦缰、webrtc_apprtc搭建:
sudo apt-get install npm
npm -g install grunt-cli
下載Google App Engine SDK for Python并解壓,編輯
sudo vim /etc/profile
export PATH="$PATH:/home/parallels/webrtc/google_appengine/"
source /etc/profile
wget https://nodejs.org/dist/v7.7.0/node-v7.7.0-linux-x64.tar.gztar xvf node-v7.7.0-linux-x64.tar.gz
sudo vim /etc/profile
export PATH=$PATH:$HOME/node-v5.9.0-sunos-x64/bin
source /etc/profile
git clone https://github.com/webrtc/apprtc
cd apprtc
npm install
grunt build
主要是src/app_engine目錄下的apprtc.py和constants.py文件。對(duì)于src/app_engine目錄下的文件每次修改后需執(zhí)行命令grunt build重新編譯掠廓,也可以直接編輯out/app_engine目錄下的apprtc.py和constants.py避免重新編譯道逗。
1.修改/root/apprtc/src/app_engine/constants.py:
# 這部分為 添加
TURN_BASE_URL = 'http://192.168.6.54:80'; #本機(jī)內(nèi)網(wǎng)地址192.168.6.54,此處的端口號(hào)與Nginx監(jiān)聽的端口號(hào)保持一致
? ? ? ? TURN_URL_TEMPLATE = '%s/turn.php?username=%s&key=%s'; #如果turn.php未實(shí)現(xiàn)奕翔,可使用默認(rèn)配置
? ? ? ? CEOD_KEY = 'yangjiangang'? #此處后面turn配置的用戶名保持一致
? ? ? ? ICE_SERVER_BASE_URL = 'http://192.168.6.54:80';#此處的端口號(hào)與Nginx監(jiān)聽的端口號(hào)保持一致
? ? ? ? ICE_SERVER_URL_TEMPLATE = '%s/iceconfig.php?key=%s'; #如果iceconfig.php未實(shí)現(xiàn),可用默認(rèn)配置,但是Android Apk會(huì)有問題
ICE_SERVER_API_KEY = os.environ.get('ICE_SERVER_API_KEY')
? ? ? ? WSS_INSTANCE_HOST_KEY = '192.168.6.54:8089'? #信令服務(wù)器端口號(hào)8089?
? ? ? ? WSS_INSTANCE_NAME_KEY = 'vm_name'
? ? ? ? WSS_INSTANCE_ZONE_KEY = 'zone'
? ? ? ? WSS_INSTANCES = [{
? ? ? ? WSS_INSTANCE_HOST_KEY: '192.168.6.54:8089',
? ? ? ? WSS_INSTANCE_NAME_KEY: 'wsserver-std',
? ? ? ? WSS_INSTANCE_ZONE_KEY: 'us-central1-a'?
? ? ? ? }, {?
? ? ? ? WSS_INSTANCE_HOST_KEY: '192.168.6.54:8089',
? ? ? ? WSS_INSTANCE_NAME_KEY: 'wsserver-std-2',
? ? ? ? WSS_INSTANCE_ZONE_KEY: 'us-central1-f'
? ? ? ? }]
2.修改/root/apprtc/src/app_engine/apprtc.py(若使用https,則不需修改此文件)
if wss_tls and wss_tls == 'false':
? ? ? wss_url = 'ws://' + wss_host_port_pair + '/ws'
? ? ? ? ? wss_post_url = 'http://' + wss_host_port_pair
? ? ? ? else:
? ? ? ? ? ? ? ? ? wss_url = 'ws://' + wss_host_port_pair + '/ws'
? ? ? ? ? wss_post_url = 'http://' + wss_host_port_pair
把原來的wss和https的scheme都改為ws和http绸贡,不要讓客戶端或者瀏覽器去使用SSL鏈接加袋。若有第三方根證書的簽名機(jī)構(gòu)頒發(fā)的證書可忽略猴抹。
修改完后重新執(zhí)行
grunt build
運(yùn)行房間服務(wù)器(room server):
在目錄/root/google_appengine目錄下找到dev_appserver.py腳本,執(zhí)行以下語(yǔ)句
./dev_appserver.py --host=192.168.6.54 /root/apprtc/out/app_engine
若想后臺(tái)運(yùn)行,則執(zhí)行?
nohup ./dev_appserver.py --host=192.168.6.54 /root/apprtc/out/app_engine &
在瀏覽器中訪問房間服務(wù)器
2锁荔、webrtc_collider搭建:
配置編譯環(huán)境
wget https://storage.googleapis.com/golang/go1.6.3.linux-amd64.tar.gztar xvf go1.6.3.linux-amd64.tar.gz
sudo vim /etc/profile
export GOROOT=/home/parallels/webrtc/go
export GOPATH=/home/parallels/webrtc/collider
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
source /etc/profile
拷貝collider源碼
mkdir -p collider/src
cp -rf apprtc/src/collider/* /collider/src
修改房間服務(wù)器的地址
sudo vim ./collider/src/collidermain/main.go
varroomSrv=flag.String("room-server","http://192.168.6.54:8080","The origin of the room server")
編譯信令服務(wù)器
進(jìn)入目錄?collider/src/
go get collidermain
go install collidermain
編譯成功后蟀给,在collider目錄下會(huì)生成bin?
運(yùn)行信令服務(wù)器
進(jìn)入/collider/bin/目錄,運(yùn)行信令服務(wù)器
./collidermain -port=8089 -tls=false
若想后臺(tái)運(yùn)行阳堕,則執(zhí)行
nohup ./collidermain -port=8089 -tls=false &
3跋理、webrtc_turn搭建:
sudo apt-get install libssl-devsudo apt-get install libevent-dev
git clone https://github.com/coturn/coturn.gitcd coturn
configure && make && make install
which turnserver查看是否成功
sudo cp /usr/local/etc/turnserver.conf.default /usr/local/etc/turnserver.conf
注意:/usr/local/etc/turnserver.conf才是配置文件!?
/usr/local/etc/turnserver.conf.default并不是配置文件恬总!
生成md5碼:turnadmin -k –u 用戶名 -r 地區(qū) -p 密碼?
turnadmin -k -u yangjiangang -r beijing -p 12345678
0x461c0b00c9fd030d2f3659806c8db557
一定要把md5碼記錄下來前普,下面需要用到的。
生成證書壹堰,該過程需要你填寫一些信息自己看著填吧拭卿!隨便寫
parallels@parallels-vm:~$sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes
Generating a 2048 bit RSA private key
..........+++
.......................................+++
writing new private key to '/etc/turn_server_pkey.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:beijing
Locality Name (eg, city) []:beijing
Organization Name (eg, company) [Internet Widgits Pty Ltd]:wenlu
Organizational Unit Name (eg, section) []:wenlu
Common Name (e.g. server FQDN or YOUR name) []:yangjiangang
Email Address []:18810626720@163.com
創(chuàng)建turnuserdb.conf文件?,在其中填入之前生成的用戶名和key (md5碼)?
sudo vi /etc/turnserdb.conf
yangjiangang:0x461c0b00c9fd030d2f3659806c8db557
sudo gedit /usr/local/etc/turnserver.conf
listening-device=eth0
relay-device=eth0
listening-ip=10.211.55.15
listening-port=3478
tls-listening-port=5349
relay-ip=10.211.55.15
external-ip=10.211.55.15
relay-threads=50
lt-cred-mech
static-auth-secret=yangjiangang
user=yangjiangang:0x461c0b00c9fd030d2f3659806c8db557
userdb=/etc/turnuserdb.conf
#max-bps=102400
pidfile="/var/run/turnserver.pid"
no-loopback-peers
no-multicast-peers
sha256
mobility
no-cli
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
stale-nonce
use-auth-secret
Verbose
fingerprint
sudo turnserver -L 10.211.55.15 -o -a -b /etc/turnserdb.conf -f -r yangjiangang
service coturn start
在瀏覽器上輸入http://10.211.55.15:3478
這樣穿透就搭建成功了!
4贱纠、配置nginx:
apt-get install nginx
apt-get install php
apt-get install php7.0-fpm
sudo vim /etc/nginx/sites-available/default
? ? ? ? upstream roomserver {
? ? ? ? ? ? server 192.168.6.54:8080;
? ? ? ? }
? ? ? ? server {
? ? ? ? ? ? #listen 80 default_server;
? ? ? ? ? ? #listen [::]:80 default_server;
? ? ? ? ? ? listen 80;
? ? ? ? ? ? # SSL configuration
? ? ? ? ? ? #
? ? ? ? ? ? # listen 443 ssl default_server;
? ? ? ? ? ? # listen [::]:443 ssl default_server;
? ? ? ? ? ? #
? ? ? ? ? ? # Note: You should disable gzip for SSL traffic.
? ? ? ? ? ? # See: https://bugs.debian.org/773332
? ? ? ? ? ? #
? ? ? ? ? ? # Read up on ssl_ciphers to ensure a secure configuration.
? ? ? ? ? ? # See: https://bugs.debian.org/765782
? ? ? ? ? ? #
? ? ? ? ? ? # Self signed certs generated by the ssl-cert package
? ? ? ? ? ? # Don't use them in a production server!
? ? ? ? ? ? #
? ? ? ? ? ? # include snippets/snakeoil.conf;
? ? ? ? ? ? root /var/www/html;
? ? ? ? ? ? # Add index.php to the list if you are using PHP
? ? ? ? ? ? index index.html index.htm index.nginx-debian.html index.php; #此處添加index.php
? ? ? ? ? ? server_name _;
? ? ? ? ? ? #? location / {
? ? ? ? ? ? ? ? # First attempt to serve request as file, then
? ? ? ? ? ? ? ? # as directory, then fall back to displaying a 404.
? ? ? ? ? ? ? ? #? try_files $uri $uri/ =404;
? ? ? ? ? ? #? }
? ? ? ? ? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? ? ? ? ? #
? ? ? ? ? ? location ~ \.php$ {
? ? ? ? ? ? ? ? include snippets/fastcgi-php.conf;
? ? ? ? ? ? ? ? # With php7.0-cgi alone:
? ? ? ? ? ? ? ? #? fastcgi_pass 127.0.0.1:9000;
? ? ? ? ? ? ? ? # With php7.0-fpm:
? ? ? ? ? ? ? ? fastcgi_pass unix:/run/php/php7.0-fpm.sock;
? ? ? ? ? ? }
? ? ? ? ? ? location / {
? ? ? ? ? ? ? ? proxy_pass http://roomserver$request_uri;
? ? ? ? ? ? ? ? proxy_set_header Host $host;
? ? ? ? ? ? }
? ? ? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? ? ? # concurs with nginx's one
? ? ? ? ? ? #
? ? ? ? ? ? #location ~ /\.ht {
? ? ? ? ? ? ? ? #? deny all;
? ? ? ? ? ? #}
? ? ? ? }
編寫turn.php文件和iceconfig.php文件峻厚,并把文件放到目錄/var/www/html/目錄下
turn.php文件內(nèi)容:
<?php?
? ? ? ? ? ? $request_username = $_GET["username"];?
? ? ? ? ? ? if(empty($request_username)) {?
? ? ? ? ? ? ? ? echo "username == null";?
? ? ? ? ? ? ? ? exit;?
? ? ? ? ? ? }?
? ? ? ? ? ? $request_key = $_GET["key"];?
? ? ? ? ? ? $time_to_live = 600;?
? ? ? ? ? ? $timestamp = time() + $time_to_live;//失效時(shí)間?
? ? ? ? ? ? $response_username = $timestamp.":".$_GET["username"];?
? ? ? ? ? ? $response_key = $request_key;?
? ? ? ? ? ? if(empty($response_key))?
? ? ? ? ? ? $response_key = "code_key"; //constants.py中CEOD_KEY?
? ? ? ? ? ? $response_password = getSignature($response_username, $response_key);?
? ? ? ? ? ? $jsonObj = new Response();?
? ? ? ? ? ? $jsonObj->username = $response_username;?
? ? ? ? ? ? $jsonObj->password = $response_password;?
? ? ? ? ? ? $jsonObj->ttl = 86400;
? ? ? ? ? ? //此處需配置自己的服務(wù)器
? ? ? ? ? ? $jsonObj->uris= array("stun:192.168.6.54:3478","turn:192.168.6.54:3478?transport=udp","turn:192.168.6.54:3478?transport=tcp");
? ? ? ? ? ? echo json_encode($jsonObj);?
? ? ? ? /**?
? ? ? ? * 使用HMAC-SHA1算法生成簽名值?
? ? ? ? *?
? ? ? ? * @param $str 源串?
? ? ? ? * @param $key 密鑰?
? ? ? ? *?
? ? ? ? * @return 簽名值?
? ? ? ? */
? ? ? ? function getSignature($str, $key) {
? ? ? ? $signature = "";
? ? ? ? if (function_exists('hash_hmac')) {
? ? ? ? $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
? ? ? ? } else {
? ? ? ? $blocksize = 64;
? ? ? ? $hashfunc = 'sha1';
? ? ? ? if (strlen($key) > $blocksize) {
? ? ? ? $key = pack('H*', $hashfunc($key));
? ? ? ? }
? ? ? ? $key = str_pad($key, $blocksize, chr(0x00));
? ? ? ? $ipad = str_repeat(chr(0x36), $blocksize);
? ? ? ? $opad = str_repeat(chr(0x5c), $blocksize);
? ? ? ? $hmac = pack(
? ? ? ? 'H*', $hashfunc(
? ? ? ? ($key ^ $opad) . pack(
? ? ? ? 'H*', $hashfunc(
? ? ? ? ($key ^ $ipad) . $str
? ? ? ? )
? ? ? ? )
? ? ? ? )
? ? ? ? );
? ? ? ? $signature = base64_encode($hmac);
? ? ? ? }
? ? ? ? ? ? return $signature;
? ? ? ? ? ? }
? ? ? ? ? ? class Response {?
? ? ? ? ? ? ? ? public $username = "";?
? ? ? ? ? ? ? ? public $password = "";?
? ? ? ? ? ? ? ? public $ttl = "";?
? ? ? ? ? ? ? ? public $uris = array("");?
? ? ? ? ? ? }?
? ? ? ? ?>
iceconfig.php文件內(nèi)容:
<?php?
? ? ? ? ? ? $request_username = "inesadt";? //配置成自己的turn服務(wù)器用戶名
? ? ? ? ? ? if(empty($request_username)) {?
? ? ? ? ? ? ? ? echo "username == null";?
? ? ? ? ? ? ? ? exit;?
? ? ? ? ? ? }?
? ? ? ? ? ? $request_key = "inesadt";? //配置成自己的turn服務(wù)器密碼
? ? ? ? ? ? $time_to_live = 600;?
? ? ? ? ? ? $timestamp = time() + $time_to_live;//失效時(shí)間?
? ? ? ? ? ? $response_username = $timestamp.":".$_GET["username"];?
? ? ? ? ? ? $response_key = $request_key;?
? ? ? ? ? ? if(empty($response_key))?
? ? ? ? ? ? $response_key = "CEOD_KEY";//constants.py中CEOD_KEY?
? ? ? ? ? ? $response_password = getSignature($response_username, $response_key);?
? ? ? ? ? ? $arrayObj = array();
? ? ? ? ? ? $arrayObj[0]['username'] = $response_username;
? ? ? ? ? ? $arrayObj[0]['credential'] = $response_password;
? ? ? ? ? ? //配置成自己的stun/turn服務(wù)器
? ? ? ? ? ? $arrayObj[0]['urls'][0] = "stun:192.168.6.54:3478";
? ? ? ? ? ? $arrayObj[0]['urls'][1] = "turn:192.168.6.54:3478?transport=tcp";
? ? ? ? ? ? $arrayObj[0]['uris'][0] = "stun:192.168.6.54:3478";
? ? ? ? ? ? $arrayObj[0]['uris'][1] = "turn:192.168.6.54:3478?transport=tcp";
? ? ? ? ? ? $jsonObj = new Response();?
? ? ? ? ? ? $jsonObj->lifetimeDuration = "300.000s";
? ? ? ? ? ? $jsonObj->iceServers = $arrayObj;
? ? ? ? ? ? echo json_encode($jsonObj);?
? ? ? ? ? ? /**?
? ? ? ? ? ? * 使用HMAC-SHA1算法生成簽名值?
? ? ? ? ? ? *?
? ? ? ? ? ? * @param $str 源串?
? ? ? ? ? ? * @param $key 密鑰?
? ? ? ? ? ? *?
? ? ? ? ? ? * @return 簽名值?
? ? ? ? ? ? */
? ? ? ? ? ? function getSignature($str, $key) {
? ? ? ? ? ? ? ? $signature = "";
? ? ? ? ? ? ? ? if (function_exists('hash_hmac')) {
? ? ? ? ? ? ? ? ? ? $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? $blocksize = 64;
? ? ? ? ? ? ? ? ? ? hashfunc = 'sha1';
? ? ? ? ? ? ? ? ? ? if (strlen($key) > $blocksize) {
? ? ? ? ? ? ? ? ? ? ? ? $key = pack('H*', $hashfunc($key));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? $key = str_pad($key, $blocksize, chr(0x00));
? ? ? ? ? ? ? ? ? ? $ipad = str_repeat(chr(0x36), $blocksize);
? ? ? ? ? ? ? ? ? ? $opad = str_repeat(chr(0x5c), $blocksize);
? ? ? ? ? ? ? ? ? ? $hmac = pack(? ?
? ? ? ? ? ? ? ? ? ? 'H*', $hashfunc(? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ($key ^ $opad) . pack(? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 'H*', $hashfunc(? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ($key ^ $ipad) . $str? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? )? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? )? ?
? ? ? ? ? ? ? ? ? ? ? ? )? ?
? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? ? ? $signature = base64_encode($hmac);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return $signature;
? ? ? ? ? }
? ? ? ? ? ? class Response {
? ? ? ? ? ? ? ? ? ? public $lifetimeDuration = "";
? ? ? ? ? ? ? ? ? ? public $iceServers = array("");
? ? ? ? ? ? }
? ? ? ? ?>
注:關(guān)于turn.php和iceconfig.php文件實(shí)現(xiàn)的必要性,如果是http局域網(wǎng)谆焊,即使不實(shí)現(xiàn)這兩個(gè)文件惠桃,在瀏覽器之間也可實(shí)現(xiàn)視頻通信,但是如果使用Google的Android apk作為客戶端,則可能會(huì)存在問題辜王。經(jīng)過測(cè)試劈狐,實(shí)現(xiàn)iceconfig.php即可,turn.php文件可不實(shí)現(xiàn)呐馆。
重啟Nginx服務(wù)器和php7.0-fpm
service nginx restart
service php7.0-fpm restart