搜索
 找回密码
 立即注册

webrtc 快速搭建 视频通话 视频会议 (亲测半个小时搭建成功)

音视频开发进阶 2022-11-16 22:11:37 90

1. 前置条件

  • 首先你需要有一台linux服务器,windows的也可以,请自行搞定
  • 一些 简单工具应该先装好 如:git、make、gcc之类的

2. 安装node和npm

下载官网最新nodejs:https://nodejs.org/en/download/

<pre class="public-DraftStyleDefault-pre" data-offset-key="cbl31-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="cbl31-0-0"><div data-offset-key="cbl31-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="cbl31-0-0"><span data-text="true">wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-x64.tar.xz</span></span></div></pre></pre>

安装

<pre class="public-DraftStyleDefault-pre" data-offset-key="9433v-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="9433v-0-0"><div data-offset-key="9433v-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="9433v-0-0"><span data-text="true"># 解压</span></span><span data-offset-key="9433v-0-1"><span data-text="true"> tar -xvf node-v10.16.0-linux-x64.tar.xz </span></span><span data-offset-key="9433v-0-2"><span data-text="true"># 改名</span></span><span data-offset-key="9433v-0-3"><span data-text="true"> mv node-v10.16.0-linux-x64 nodejs </span></span><span data-offset-key="9433v-0-4"><span data-text="true"># 进入目录</span></span><span data-offset-key="9433v-0-5"><span data-text="true"> cd nodejs/

</span></span><span data-offset-key="9433v-0-6"><span data-text="true"># 确认一下nodejs下bin目录是否有node和npm文件,如果有就可以执行软连接</span></span><span data-offset-key="9433v-0-7"><span data-text="true"> sudo ln -s /home/dds/webrtc/nodejs/bin/npm /usr/local/bin/ sudo ln -s /home/dds/webrtc/nodejs/bin/node /usr/local/bin/

看清楚,上面软链接的路径是你自己创建的路径,我的路径是/home/dds/webrtc/nodejs

</span></span><span data-offset-key="9433v-0-8"><span data-text="true">#查看是否安装</span></span><span data-offset-key="9433v-0-9"><span data-text="true"> node -v npm -v

</span></span><span data-offset-key="9433v-0-10"><span data-text="true"># 注意,ubuntu 有的是需要sudo,如果不想sudo,可以软链接到当前用户目录</span></span><span data-offset-key="9433v-0-11"><span data-text="true"> sudo ln -s /home/dds/webrtc/nodejs/bin/node /usr/bin/</span></span></div></pre></pre>

3. coturn穿透和转发服务器

3.1 ubuntu安装

ubuntu的话直接用apt安装就行了

<pre class="public-DraftStyleDefault-pre" data-offset-key="agj7k-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="agj7k-0-0"><div data-offset-key="agj7k-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="agj7k-0-0"><span data-text="true">sudo apt install coturn

</span></span></div></pre></pre>

3.2 centos安装

centos或者其他的系统根据下面的方式进行安装

安装依赖

<pre class="public-DraftStyleDefault-pre" data-offset-key="5mn7s-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="5mn7s-0-0"><div data-offset-key="5mn7s-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="5mn7s-0-0"><span data-text="true">Ubuntu, Debian, Mint:
$ sudo apt-get install libssl-dev(必须) $ sudo apt-get install libsqlite3 (or sqlite3) $ sudo apt-get install libsqlite3-dev (or sqlite3-dev) $ sudo apt-get install libevent-dev(必须) $ sudo apt-get install libpq-dev $ sudo apt-get install mysql-client $ sudo apt-get install libmysqlclient-dev $ sudo apt-get install libhiredis-dev

Fedora: $ sudo yum install openssl-devel $ sudo yum install sqlite $ sudo yum install sqlite-devel $ sudo yum install libevent $ sudo yum install libevent-devel $ sudo yum install postgresql-devel $ sudo yum install postgresql-server $ sudo yum install mysql-devel $ sudo yum install mysql-server $ sudo yum install hiredis $ sudo yum install hiredis-devel </span></span></div></pre></pre>

编译安装coturn

<pre class="public-DraftStyleDefault-pre" data-offset-key="5j5gv-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="5j5gv-0-0"><div data-offset-key="5j5gv-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="5j5gv-0-0"><span data-text="true">git clone https://github.com/coturn/coturn cd coturn ./configure make sudo make install

</span></span></div></pre></pre>

3.3 配置相关

查看是否安装成功

<pre class="public-DraftStyleDefault-pre" data-offset-key="1not3-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="1not3-0-0"><div data-offset-key="1not3-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="1not3-0-0"><span data-text="true">## 如果能够找到就说明已经安装成功</span></span><span data-offset-key="1not3-0-1"><span data-text="true"> which turnserver

</span></span></div></pre></pre>

根据自己的安装目录,配置文件/usr/local/etc/turnserver.conf 或者/etc/turnserver.conf

我的目录是 /usr/local/etc/turnserver.conf 配置 如下

<pre class="public-DraftStyleDefault-pre" data-offset-key="7kdpv-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="7kdpv-0-0"><div data-offset-key="7kdpv-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="7kdpv-0-0"><span data-text="true">verbose fingerprint lt-cred-mech realm=test user=ddssingsong:123456 stale-nonce no-loopback-peers no-multicast-peers mobility no-cli

</span></span></div></pre></pre>

或者下面这个配置,只配置stun(stun-only)

<pre class="public-DraftStyleDefault-pre" data-offset-key="dleho-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="dleho-0-0"><div data-offset-key="dleho-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="dleho-0-0"><span data-text="true">listening-ip=本地ip listening-port=3478 </span></span><span data-offset-key="dleho-0-1"><span data-text="true">#relay-ip=0.0.0.0</span></span><span data-offset-key="dleho-0-2"><span data-text="true"> external-ip=外网ip min-port=59000 max-port=65000 Verbose fingerprint no-stdout-log syslog user=ddssingsong:123456 no-tcp no-tls no-tcp-relay stun-only </span></span><span data-offset-key="dleho-0-3"><span data-text="true"># 下面是配置证书,不懂就问后端人员怎么用openssl生成这个</span></span><span data-offset-key="dleho-0-4"><span data-text="true"> cert=pem/turn_server_cert.pem pkey=pem/turn_server_pkey.pem </span></span><span data-offset-key="dleho-0-5"><span data-text="true">#secure-stun</span></span></div></pre></pre>

更详细的配置请看

https://github.com/ddssingsong/webrtc_server/blob/master/coturn/turnserver.conf

3.4 启动相关

<pre class="public-DraftStyleDefault-pre" data-offset-key="a97cd-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="a97cd-0-0"><div data-offset-key="a97cd-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="a97cd-0-0"><span data-text="true"># 如果按照上面的配置直接运行 turnserver

如果没有配置上述配置文件,可采用其他运行方法

/usr/local/bin/turnserver --syslog -a -f --min-port=32355 --max-port=65535 --user=dds:123456 -r dds --cert=turn_server_cert.pem --pkey=turn_server_pkey.pem --log-file=stdout -v

--syslog 使用系统日志 -a 长期验证机制 -f 使用指纹 --min-port 起始用的最小端口 --max-port 最大端口号 --user=dds:123456 turn用户名和密码 -r realm组别 --cert PEM格式的证书 --pkey PEM格式的私钥文件 -l, --log-file,<filename> 指定日志文件 -v verbose

请根据需要选择</span></span></div></pre></pre>

测试地址,请分别测试stun和turn,relay代表turn

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

编辑切换为居中

添加图片注释,不超过 140 字(可选)

4. 安装webrtc服务器和浏览器端

下载 代码

<pre class="public-DraftStyleDefault-pre" data-offset-key="6tg43-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="6tg43-0-0"><div data-offset-key="6tg43-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="6tg43-0-0"><span data-text="true"># 代码检出来</span></span><span data-offset-key="6tg43-0-1"><span data-text="true"> git clone https://github.com/ddssingsong/webrtc_server_node.git
cd webrtc_server</span></span></div></pre></pre>

修改/public/dist/js/SkyRTC-client.js,设置穿透服务器

<pre class="public-DraftStyleDefault-pre" data-offset-key="9gbla-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="9gbla-0-0"><div data-offset-key="9gbla-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="9gbla-0-0"><span data-text="true"> var iceServer = { "iceServers": [ { "url": "stun:stun.l.google.com:19302" }, { "url": "stun:118.25.25.147:3478" }, { "url": "turn:118.25.25.147:3478", "username":"ddssingsong", "credential":"123456" } ] };

</span></span></div></pre></pre>

修改/public/dist/js/conn.js

<pre class="public-DraftStyleDefault-pre" data-offset-key="e7bei-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="e7bei-0-0"><div data-offset-key="e7bei-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="e7bei-0-0"><span data-text="true">## 最后一行

如果没有配wss代理

rtc.connect("ws:" + window.location.href.substring(window.location.protocol.length).split('#')[0], window.location.hash.slice(1));

如果配了nginx wss代理 rtc.connect("wss:" + window.location.href.substring(window.location.protocol.length).split('#')[0]+"/wss", window.location.hash.slice(1));

后面的那个“/wss”是根据自己配的代理路径</span></span></div></pre></pre>

运行

<pre class="public-DraftStyleDefault-pre" data-offset-key="4rpc0-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="4rpc0-0-0"><div data-offset-key="4rpc0-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="4rpc0-0-0"><span data-text="true"># cd到项目路径</span></span><span data-offset-key="4rpc0-0-1"><span data-text="true">

</span></span><span data-offset-key="4rpc0-0-2"><span data-text="true"># 安装依赖</span></span><span data-offset-key="4rpc0-0-3"><span data-text="true"> npm install

</span></span><span data-offset-key="4rpc0-0-4"><span data-text="true"># 运行</span></span><span data-offset-key="4rpc0-0-5"><span data-text="true"> node server.js

</span></span></div></pre></pre>

其实到了这一步就可以测试客户端了,往下看获取线上部署详情

客户端测试可以不使用nginx配置代理,只需要使用ws即可

5. 安装nginx

如果是ubuntu的话还是可以使用apt安装

<pre class="public-DraftStyleDefault-pre" data-offset-key="eb4kf-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="eb4kf-0-0"><div data-offset-key="eb4kf-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="eb4kf-0-0"><span data-text="true">sudo apt-get install nginx</span></span></div></pre></pre>

centos按照下面的方式进行

安装依赖

<pre class="public-DraftStyleDefault-pre" data-offset-key="frr74-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="frr74-0-0"><div data-offset-key="frr74-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="frr74-0-0"><span data-text="true">yum install -y gcc gcc-c++ autoconf automake make zlib zlib-devel openssl openssl-devel pcre pcre-devel</span></span></div></pre></pre>

编译安装nginx

<pre class="public-DraftStyleDefault-pre" data-offset-key="5hn6c-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="5hn6c-0-0"><div data-offset-key="5hn6c-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="5hn6c-0-0"><span data-text="true">wget -C http://nginx.org/download/nginx-1.12.0.tar.gz tar xvf nginx-1.12.0.tar.gz cd nginx-1.12.0

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

make

sudo make install </span></span></div></pre></pre>

生成证书,这个只是简单的生成,请慎重对待

<pre class="public-DraftStyleDefault-pre" data-offset-key="3iqob-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="3iqob-0-0"><div data-offset-key="3iqob-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="3iqob-0-0"><span data-text="true"># 移动到目录,下面会用到 cd / sudo mkdir cert ce cert

生成服务器证书key

sudo openssl genrsa -out cert.pem 1024

生成证书请求,需要你输入信息,一路回车就行,不要输入内容

sudo openssl req -new -key cert.pem -out cert.csr

生成crt证书

sudo openssl x509 -req -days 3650 -in cert.csr -signkey cert.pem -out cert.crt</span></span></div></pre></pre>

修改 配置文件/usr/local/nginx/conf/nginx.conf或者/etc/nginx/nginx.conf,没有的话自己找一下

将下面的内容帖进去就行了

<pre class="public-DraftStyleDefault-pre" data-offset-key="3kovr-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="3kovr-0-0"><div data-offset-key="3kovr-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="3kovr-0-0"><span data-text="true">user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf;

events { worker_connections 768;

multi_accept on;

}

http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048;

include /etc/nginx/mime.types;
default_type application/octet-stream;


ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

 #代理https
upstream web {
        server 0.0.0.0:3000;    
    }
#代理websocket
upstream websocket {
        server 0.0.0.0:3000;   
    }

server { 
    listen       443; 
    server_name  localhost;
    ssl          on;

    ssl_certificate     /cert/cert.crt;#配置证书
    ssl_certificate_key  /cert/cert.key;#配置密钥

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  50m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv2 SSLv3;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;


#wss 反向代理  
location /wss {
    proxy_pass http://websocket/; # 代理到上面的地址去
    proxy_read_timeout 300s;
    proxy_set_header Host $host;
    proxy_set_header X-Real_IP $remote_addr;
    proxy_set_header X-Forwarded-for $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'Upgrade';

}

https 反向代理

location / {
    proxy_pass         http://web/;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

} } }

</span></span></div></pre></pre>

开启nginx

<pre class="public-DraftStyleDefault-pre" data-offset-key="3eh03-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="3eh03-0-0"><div data-offset-key="3eh03-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="3eh03-0-0"><span data-text="true">#查看是否开启 ps -ef|grep nginx

改变配置文件重启nginx

sudo nginx -s reload</span></span></div></pre></pre>

测试浏览器

<pre class="public-DraftStyleDefault-pre" data-offset-key="c3mp-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="c3mp-0-0"><div data-offset-key="c3mp-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="c3mp-0-0"><span data-text="true">#访问</span></span><span data-offset-key="c3mp-0-1"><span data-text="true">

https://serverIp</span></span><span data-offset-key="c3mp-0-2"><span data-text="true">#roomName</span></span><span data-offset-key="c3mp-0-3"><span data-text="true">

如: 外网:https://192.168.1.123/</span></span><span data-offset-key="c3mp-0-4"><span data-text="true">#123</span></span><span data-offset-key="c3mp-0-5"><span data-text="true"> 内网:http:192.168.1.123:3000</span></span><span data-offset-key="c3mp-0-6"><span data-text="true">#123</span></span><span data-offset-key="c3mp-0-7"><span data-text="true">

</span></span><span data-offset-key="c3mp-0-8"><span data-text="true"># 查看效果,其中roomName为进入的房间名,不同房间的用户无法互相通话</span></span></div></pre></pre>

测试客户端

将这个项目下下来使用 android studio 编译并安装

<pre class="public-DraftStyleDefault-pre" data-offset-key="8uvdc-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="8uvdc-0-0"><div data-offset-key="8uvdc-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="8uvdc-0-0"><span data-text="true">## 看清楚分支,项目一直在开发中,所以请使用固定分支测试,一般使用branch_nodejs分支测试,master和dev是最新代码</span></span><span data-offset-key="8uvdc-0-1"><span data-text="true"> https://github.com/ddssingsong/webrtc_android</span></span></div></pre></pre>

修改WebrtcUtil.java,要去掉界面上的地址哦

<pre class="public-DraftStyleDefault-pre" data-offset-key="7h0vk-0-0"><pre class="Editable-styled" data-block="true" data-editor="41hd1" data-offset-key="7h0vk-0-0"><div data-offset-key="7h0vk-0-0" class="public-DraftStyleDefault-block public-DraftStyleDefault-ltr"><span data-offset-key="7h0vk-0-0"><span data-text="true"> </span></span><span data-offset-key="7h0vk-0-1"><span data-text="true">// turn and stun</span></span><span data-offset-key="7h0vk-0-2"><span data-text="true"> </span></span><span data-offset-key="7h0vk-0-3"><span data-text="true">// 外网测试才需要</span></span><span data-offset-key="7h0vk-0-4"><span data-text="true"> private static MyIceServer[] iceServers = { new MyIceServer("stun:stun.l.google.com:19302"), new MyIceServer("118.25.25.147:3478?transport=udp"), new MyIceServer("118.25.25.147:3478?transport=udp", "ddssingsong", "123456"), new MyIceServer("118.25.25.147:3478?transport=tcp", "ddssingsong", "123456"),

};

</span></span><span data-offset-key="7h0vk-0-5"><span data-text="true">// 外网测试</span></span><span data-offset-key="7h0vk-0-6"><span data-text="true">
private static String WSS = "wss://47.254.34.146/wss";

</span></span><span data-offset-key="7h0vk-0-7"><span data-text="true">//本地内网信令地址</span></span><span data-offset-key="7h0vk-0-8"><span data-text="true">
private static String WSS = "ws://192.168.1.122:3000";

</span></span></div></pre></pre>

好了,基本完成

编辑

添加图片注释,不超过 140 字(可选)

https://github.com/ddssingsong

原文链接:https://zego.csdn.net/63494a57d3efff3090b589aa.html?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Eactivity-5-86225524-blog-77193883.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Eactivity-5-86225524-blog-77193883.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=10

使用道具 举报

随机推荐

0 回复

游客
返回顶部