搭建Janus WebRTC视频推流服务

tech2025-12-12  4

搭建Janus WebRTC视频推流服务

1、环境准备

创建一台阿里云机器ubuntu16.04谷歌浏览器(https访问)开通安全组端口TCP:22、80、443、3478、7088、7188、7889、7989、8088、8089、8188、8989UDP:3478、30000-60000

2.编译运行Janus Server需要依赖较多的一些第三方库,安装aptitude

apt-get install aptitude

3.安装依赖

aptitude install libmicrohttpd-dev libjansson-dev libnice-dev \ libssl1.0.1-dev libsrtp-dev libsofia-sip-ua-dev libglib2.3.4-dev \ libopus-dev libogg-dev libcurl4-openssl-dev pkg-config gengetopt \ libtool automake apt install -y cmake libconfig-dev libssl-dev oxygen graphviz aptitude install libavcodec-dev libavformat-dev libswscale-dev libavutil-dev

4.安装 WebSocket

git clone https://github.com/warmcat/libwebsockets.git cd libwebsockets git branch -a 查看选择最新的稳定版本,目前的是remotes/origin/v3.2-stable git checkout v3.2-stable 切换到最新稳定版本 mkdir build cd build cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" .. make && make install

5.安装 libsrtp

wget https://github.com/cisco/libsrtp/archive/v2.2.0.tar.gz tar xfv v2.2.0.tar.gz cd libsrtp-2.2.0 ./configure --prefix=/usr --enable-openssl make shared_library && make install

6.安装 libusrsctp

git clone https://github.com/Kurento/libusrsctp.git cd libusrsctp ./bootstrap ./configure make && make install

7.安装 libmicrohttpd

wget https://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-0.9.71.tar.gz tar zxf libmicrohttpd-0.9.71.tar.gz cd libmicrohttpd-0.9.71/ ./configure make && make install

8.编译 Janus

git clone https://github.com/meetecho/janus-gateway.git git tag 查看当前的 tag,选择最新稳定的版本v0.10.4 git checkout v0.10.4 sh autogen.sh ./configure --prefix=/opt/janus --enable-websockets --enable-post-processing --enable-docs --enable-rest --enable-data-channels make && make install

9.通过配置nginx来实现https

(1)生成证书

mkdir -p ~/cert cd ~/cert # CA私钥 openssl genrsa -out key.pem 2048 # 自签名证书 openssl req -new -x509 -key key.pem -out cert.pem -days 1095

(2)安装nginx

wget http://nginx.org/download/nginx-1.15.8.tar.gz tar xvzf nginx-1.15.8.tar.gz cd nginx-1.15.8/ # 配置,一定要支持https ./configure --with-http_ssl_module #编译 make && make install

(3)修改nginx配置文件

vim /usr/local/nginx/conf/nginx.conf #配置如下: # HTTPS server # server { listen 80; listen 443 ssl; #修改的位置 server_name localhost; #新增的证书 #start ssl_certificate /root/cert/cert.pem; ssl_certificate_key /root/cert/key.pem; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; #end location / { root /opt/janus/share/janus/demos; #修改的位置 index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

(4)配置全局nginx命令

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

(5)启动nginx

nginx

(6)重启nginx

nginx -s reload

10.安装启动coturn

#安装 apt-get install -y libssl-dev libevent-dev wget http://coturn.net/turnserver/v4.5.0.7/turnserver-4.5.0.7.tar.gz tar xfz turnserver-4.5.0.7.tar.gz cd turnserver-4.5.0.7 ./configure make && make install #启动 nohup turnserver -L 0.0.0.0 --min-port 30000 --max-port 60000 -a -u zhangpeng:123456 -v -f -r nort.gov &

11.修改janus的配置文件

#要先把.sample后缀的文件拷贝成jcfg后缀 cd /opt/janus/etc/janus cp janus.jcfg.sample janus.jcfg cp janus.transport.http.jcfg.sample janus.transport.http.jcfg cp janus.transport.websockets.jcfg.sample janus.transport.websockets.jcfg cp janus.plugin.videoroom.jcfg.sample janus.plugin.videoroom.jcfg cp janus.transport.pfunix.jcfg.sample janus.transport.pfunix.jcfg cp janus.plugin.streaming.jcfg.sample janus.plugin.streaming.jcfg cp janus.plugin.recordplay.jcfg.sample janus.plugin.recordplay.jcfg cp janus.plugin.voicemail.jcfg.sample janus.plugin.voicemail.jcfg cp janus.plugin.sip.jcfg.sample janus.plugin.sip.jcfg cp janus.plugin.nosip.jcfg.sample janus.plugin.nosip.jcfg cp janus.plugin.textroom.jcfg.sample janus.plugin.textroom.jcfg cp janus.plugin.echotest.jcfg.sample janus.plugin.echotest.jcfg

12.修改janus.jcfg

vim /opt/janus/etc/janus/janus.jcfg #修改如下: nat: { stun_server = "外网ip" stun_port = 3478 nice_debug = false turn_server = "外网ip" turn_port = 3478 turn_type = "udp" turn_user = "zhangpeng" turn_pwd = "123456" ice_ignore_list = "vmnet" }

13.修改janus.transport.http.jcfg

vim /opt/janus/etc/janus/janus.transport.http.jcfg #修改如下:证书路径为上面生成证书的路径 general: { #events = true # Whether to notify event handlers about transport events (default=true) json = "indented" # Whether the JSON messages should be indented (default), # plain (no indentation) or compact (no indentation and no spaces) base_path = "/janus" # Base path to bind to in the web server (plain HTTP only) http = true # Whether to enable the plain HTTP interface port = 8088 # Web server HTTP port #interface = "eth0" # Whether we should bind this server to a specific interface only #ip = "192.168.0.1" # Whether we should bind this server to a specific IP address (v4 or v6) only https = true # Whether to enable HTTPS (default=false) secure_port = 8089 # Web server HTTPS port, if enabled #secure_interface = "eth0" # Whether we should bind this server to a specific interface only #secure_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address (v4 or v6) only #acl = "127.,192.168.0." # Only allow requests coming from this comma separated list of addresses } admin: { admin_base_path = "/admin" # Base path to bind to in the admin/monitor web server (plain HTTP only) admin_http = true # Whether to enable the plain HTTP interface admin_port = 7088 # Admin/monitor web server HTTP port #admin_interface = "eth0" # Whether we should bind this server to a specific interface only #admin_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address (v4 or v6) only admin_https = true # Whether to enable HTTPS (default=false) admin_secure_port = 7889 # Admin/monitor web server HTTPS port, if enabled #admin_secure_interface = "eth0" # Whether we should bind this server to a specific interface only #admin_secure_ip = "192.168.0.1 # Whether we should bind this server to a specific IP address (v4 or v6) only #admin_acl = "127.,192.168.0." # Only allow requests coming from this comma separated list of addresses } certificates: { cert_pem = "/root/cert/cert.pem" cert_key = "/root/cert/key.pem" #cert_pwd = "secretpassphrase" #ciphers = "PFS:-VERS-TLS1.0:-VERS-TLS1.1:-3DES-CBC:-ARCFOUR-128" }

14.修改janus.transport.websockets.jcfg

vim /opt/janus/etc/janus/janus.transport.websockets.jcfg #修改如下:证书路径为上面生成证书的路径 general: { #events = true # Whether to notify event handlers about transport events (default=true) json = "indented" # Whether the JSON messages should be indented (default), # plain (no indentation) or compact (no indentation and no spaces) #pingpong_trigger = 30 # After how many seconds of idle, a PING should be sent #pingpong_timeout = 10 # After how many seconds of not getting a PONG, a timeout should be detected ws = true # Whether to enable the WebSockets API ws_port = 8188 # WebSockets server port #ws_interface = "eth0" # Whether we should bind this server to a specific interface only #ws_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address only wss = true # Whether to enable secure WebSockets wss_port = 8989 # WebSockets server secure port, if enabled #wss_interface = "eth0" # Whether we should bind this server to a specific interface only #wss_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address only #ws_logging = "err,warn" # libwebsockets debugging level as a comma separated list of things # to debug, supported values: err, warn, notice, info, debug, parser, # header, ext, client, latency, user, count (plus 'none' and 'all') #ws_acl = "127.,192.168.0." # Only allow requests coming from this comma separated list of addresses } admin: { admin_ws = true # Whether to enable the Admin API WebSockets API admin_ws_port = 7188 # Admin API WebSockets server port, if enabled #admin_ws_interface = "eth0" # Whether we should bind this server to a specific interface only #admin_ws_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address only admin_wss = true # Whether to enable the Admin API secure WebSockets admin_wss_port = 7989 # Admin API WebSockets server secure port, if enabled #admin_wss_interface = "eth0" # Whether we should bind this server to a specific interface only #admin_wss_ip = "192.168.0.1" # Whether we should bind this server to a specific IP address only #admin_ws_acl = "127.,192.168.0." # Only allow requests coming from this comma separated list of addresses } certificates: { cert_pem = "/root/cert/cert.pem" cert_key = "/root/cert/key.pem" #cert_pwd = "secretpassphrase" #ciphers = "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256" }

15.修改demos里面的配置文件,路径为/opt/janus/share/janus/demos/,需要哪些模块就修改哪些,以vp9、videoroom为例:

vim /opt/janus/share/janus/demos/videoroomtest.js vim /opt/janus/share/janus/demos/vp9svctest.js #修改都如下面一下 #原始文件 var server = null; if(window.location.protocol === 'http:') server = "http://" + window.location.hostname + ":8088/janus"; else server = "https://" + window.location.hostname + ":8089/janus"; #修改之后 var server = null; if(window.location.protocol === 'ws:') server = "ws://" + window.location.hostname + ":8188"; else server = "wss://" + window.location.hostname + ":8989";

16.运行Janus

#启动命令 /opt/janus/bin/janus --debug-level=5 --log-file=$HOME/janus-log

17.测试web与web通信

地址:https://外网ip

点击安全连接,继续访问

进入页面之后,点击Demo,选择VP9-SVC Video Room 点击start–>输入房间号,点击Join the room

成功!!!!!!!!!!!!!

最新回复(0)