Ubuntu下搭建Janus Server

Ubuntu下搭建Janus Server


目录

  1. Janus简介
  2. 下载和编译 Janus
  3. 配置和运行janus
  4. 视频通话联调测试

1. Janus简介

  1. Janus 是一个开源的,通过 C 语言实现了对 WebRTC 支持的 Gateway。
  2. Janus 自身实现得很简单,提供插件机制来支持不同的业务逻辑,配合官方自带插件就可以用来实现高效的 Media Server 服务。
  3. 本博客在 Ubuntu下搭建 Janus 服务器,实现 janus 官方 Demo 浏览器与 Android APP Demo(janus-gateway-android)之间的音视频通话。注意:浏览器打开音视频采集的话需要 HTTPS 加密访问。

2. 下载和编译 Janus

  1. 编译运行 Janus Server 需要依赖较多的一些第三方库,而这些依赖库在 Ubuntu 下主要通过 aptitude 进行安装,首先通过安装 aptitude:
sudo apt-get install aptitude
sudo apt-get update
  1. 执行以下命令更新镜像源
sudo apt-get update && sudo apt-get upgrade

1. 命令安装依赖

  1. Ubuntu 下通过 aptitude 批量安装依赖工具包
  2. 批量安装命令:
 sudo apt install build-essential
 
 
sudo aptitude install libmicrohttpd-dev libjansson-dev libnice-dev  libsofia-sip-ua-dev \
    libopus-dev libogg-dev libcurl4-openssl-dev pkg-config gengetopt \
    libtool automake
    
sudo aptitude install libglib2.3.4-dev  
    
sudo apt install cmake
sudo aptitude install libconfig-dev
sudo aptitude install libssl-dev
sudo aptitude install doxygen graphviz

# ffmpeg库 支持--enable-post-processing
sudo aptitude install libavcodec-dev libavformat-dev libswscale-dev libavutil-dev
  1. 附录:卸载命令
apt-get remove 会删除软件包而保留软件的配置文件
apt-get purge 会同时清除软件包和软件的配置文件
  1. 查找包命令
查找软件包
apt-cache search 软件包名
显示软件包的详细信息
apt-cache show 软件包名

2. 源码安装依赖

1. 安装 WebSocket

  1. janus 支持 WebSocket 是可选项,如果不安装,编译 janus 时,默认不支持 WebSocket 的链接请求,而 Android APP Demo 是通过 WebSocket 与 janus 进行通信的,因此如果需要 Android APP Demo 能与浏览器(HTTP)进行视频通话,所以就必须要在编译 janus 时支持 WebSocket。
  2. 依次执行以下命令,分别进行下载,编译,安装:
git clone https://gitee.com/embedded-lib/libwebsockets.git 
或 git clone https://github.com/warmcat/libwebsockets.git
cd libwebsockets
git branch -a 查看选择最新的稳定版本, 
git checkout v4.3.0 切换到较新稳定版本
mkdir build
cd build
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" ..
make && sudo make install
  1. 安装成功后,在编译 janus 时,janus 默认会增加对 WebSocket 的集成,或者通过增加编译参数 --enable-websockets 打开 WebSocket 开关

2. 安装 libsrtp

  1. Janus 需要至少 version 1.5 以上的 libsrtp,如果系统中已经安装了 libsrtp,则首先卸载后,手动安装新版本,这里我们安装 libsrtp 2.2,2. 依次执行以下命令:
git clone https://gitee.com/MediaNext/libsrtp.git
cd libsrtp
git checkout v2.3.0
./configure --prefix=/usr --enable-openssl
make shared_library && sudo make install

3. 安装libmicrohttpd

  1. libmicrohttpd支持–enable-rest
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
sudo make install

3. 编译 Janus

  1. 通过 Git 下载 Janus 源码,并编译安装:
git clone https://gitee.com/easyhao/janus-gateway.git
或git clone https://github.com/meetecho/janus-gateway.git
cd janus-gateway
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 
make
sudo make install
  1. make install的时候,将janus安装到 /opt/janus路径,插件的so库在/opt/janus/lib/janus/plugins

  2. configure 执行成功后,会输出 janus 所支持的 协议及插件,如下:

ompiler:                  gcc
libsrtp version:           2.x
SSL/crypto library:        OpenSSL
DTLS set-timeout:          not available
Mutex implementation:      GMutex (native futex on Linux)
DataChannels support:      yes
Recordings post-processor: yes
TURN REST API client:      yes
Doxygen documentation:     yes
Transports:
    REST (HTTP/HTTPS):     yes
    WebSockets:            yes
    RabbitMQ:              no
    MQTT:                  no
    Unix Sockets:          yes
    Nanomsg:               no
Plugins:
    Echo Test:             yes
    Streaming:             yes
    Video Call:            yes
    SIP Gateway:           yes
    NoSIP (RTP Bridge):    yes
    Audio Bridge:          yes
    Video Room:            yes
    Voice Mail:            yes
    Record&Play:           yes
    Text Room:             yes
    Lua Interpreter:       no
    Duktape Interpreter:   no
Event handlers:
    Sample event handler:  yes
    WebSocket ev. handler: yes
    RabbitMQ event handler:no
    MQTT event handler:    no
    Nanomsg event handler: no
    GELF event handler:    yes
External loggers:
    JSON file logger:      no
JavaScript modules:        no

3. 配置和运行janus

1. 配置nginx

  1. 安装nginx,主要用来提供web访问。

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

#下载nginx 1.15.8版本
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

#安装
sudo make install 

3. 修改nginx配置文件

  1. Nginx配置文件默认在 /usr/local/nginx/conf/nginx.conf
  2. 指向janus所在目录/opt/janus/share/janus/demos
    # HTTPS server
    #
    server {
        listen       443 ssl;
        server_name  localhost;
        # 配置相应的key
        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;
        # 指向janus demo所在目录
        location / {
            root   /opt/janus/share/janus/demos;
            index  index.html index.htm;
        }
    }

4. 启动nginx

sudo /usr/local/nginx/sbin/nginx  
  1. 然后通过 https://服务器公网地址(比如:https://8.141.75.xxx/)就可以访问到界面,但此时还不能正常通话。
    Ubuntu下搭建Janus Server_第1张图片
  2. 注意如果访问不通,需要设置安全组,添加80和443端口
    Ubuntu下搭建Janus Server_第2张图片
  3. 如果提示在Chrome中打开网页时出现以下问题:您的连接不是私密连接 攻击者可能会试图从 x.x.x.x 窃取您的信息(例如:密码、通讯内容或信用卡信息),直接在页面输入:thisisunsafe ,回车,页面即会自动刷新进入网页。

2. 安装和启动coturn

sudo apt-get install libssl-dev
sudo apt-get install libevent-dev
​
#git clone https://github.com/coturn/coturn 
#cd coturn
# 提供另一种安装方式turnserver是coturn的升级版本
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 && sudo make install
  1. 启动
sudo nohup turnserver -L 0.0.0.0 --min-port 50000 --max-port 60000  -a -u xxx:123456 -v -f -r nort.gov &
  1. 需要在安全组开放端口: TCP/UDP 3478 和 UDP 50000-60000

3. 配置janus的jcfg文件

  1. janus安装目录在/opt/janus
./bin ./etc ./include ./lib ./share
可执行文件 janus配置文件 janus头文件 janus库 存放脚本或者文档,web demo也在这里

1. 配置Video room

  1. 先配置video room,需要配置的文件为(目录/opt/janus/etc/janus),并开通8088,8089,8188,8989端口
  2. 要先把.sample后缀的文件拷贝成jcfg后缀
# 进到对应的目录
cd /opt/janus/etc/janus
# 拷贝文件
sudo cp janus.jcfg.sample janus.jcfg
sudo cp janus.transport.http.jcfg.sample janus.transport.http.jcfg
sudo cp janus.transport.websockets.jcfg.sample janus.transport.websockets.jcfg
sudo cp janus.plugin.videoroom.jcfg.sample janus.plugin.videoroom.jcfg
sudo cp janus.transport.pfunix.jcfg.sample janus.transport.pfunix.jcfg
sudo cp janus.plugin.streaming.jcfg.sample janus.plugin.streaming.jcfg
sudo cp janus.plugin.recordplay.jcfg.sample janus.plugin.recordplay.jcfg
sudo cp janus.plugin.voicemail.jcfg.sample janus.plugin.voicemail.jcfg
sudo cp janus.plugin.sip.jcfg.sample janus.plugin.sip.jcfg
sudo cp janus.plugin.nosip.jcfg.sample janus.plugin.nosip.jcfg
sudo cp janus.plugin.textroom.jcfg.sample  janus.plugin.textroom.jcfg
sudo cp janus.plugin.echotest.jcfg.sample janus.plugin.echotest.jcfg

2. 配置janus.jcfg

# 大概237行
stun_server = "8.141.75.xxx"
        stun_port = 3478
        nice_debug = false
# 大概267行 填写云服务器外网ip地址
nat_1_1_mapping = "8.141.75.xxx"
​
#大概274# credentials to authenticate...
        turn_server = "8.141.75.xxx"
        turn_port = 3478
        turn_type = "udp"
        turn_user = "ljw"
        turn_pwd = "123456"
  1. ​大约202行左右,设置udp端口范围。
    Ubuntu下搭建Janus Server_第3张图片

3. 配置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)
        threads = "unlimited"                   # unlimited=thread per connection, number=thread pool
        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
}
​
certificates: {
        cert_pem = "/home/ubuntu/cert/cert.pem" # 跟前面配置的cert.pem一样路径
        cert_key = "/home/ubuntu/cert/key.pem"
        #cert_pwd = "secretpassphrase"
        #ciphers = "PFS:-VERS-TLS1.0:-VERS-TLS1.1:-3DES-CBC:-ARCFOUR-128"
}

4. 配置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
}
​
certificates: {
        cert_pem = "/home/ubuntu/cert/cert.pem"
        cert_key = "/home/ubuntu/cert/key.pem"
        #cert_pwd = "secretpassphrase"
}

4. 修改网页默认支持的wss协议

  1. 修改 /opt/janus/share/janus/demos/videoroomtest.js文件,原来为(在45行处)
var server = null;
if(window.location.protocol === 'http:')
        server = "http://" + window.location.hostname + ":8088/janus";
else
        server = "https://" + window.location.hostname + ":8089/janus";
将默认的https协议改为wss
var server = "wss://" + window.location.hostname + ":8989";

Ubuntu下搭建Janus Server_第4张图片

5. 配置coturn地址-这里不是必须的

  1. 这里不是必须的
  2. 修改 /opt/janus/share/janus/demos/videoroomtest.js 文件,配置自己搭建的coturn地址,以下ip、用户名,密码根据实际修改。在约83行添加。
iceServers :[{
    urls: [
        "turn:8.141.75.xxx:3478?transport=udp",
        "turn:8.141.75.xxx:3478?transport=tcp"      
    ],
    username: "ljw",
    credential: "123456"
},
{
    urls: [
        "stun:8.141.75.xxx:3478"
    ]
}],

6. 云服务器端口开放

  1. 需要用到的端口有:TCP的8088,8089,8188,8989,3478,UDP的3478,50000~60000端口
    Ubuntu下搭建Janus Server_第5张图片

7. 运行 Janus

  1. WebSocket 的ws端口号为 8188 和 8989,记住这个端口号,在 Android APP Demo 中会使用到
  2. 启动 Janus:
/opt/janus/bin/janus --debug-level=5 --log-file=$HOME/janus-log
  1. 根据需要可以选择是否加上后面两个启动参数。

8. 测试web和web的通话

  1. 打开 https://8.141.75.xxx/videoroomtest.html
  2. 开两个同样的网页,然后点击start,输入名字则开始进行音视频通话测试。

Ubuntu下搭建Janus Server_第6张图片

4. 视频通话联调测试

  1. 我们使用 PC 下的 浏览器 与 Android APP Demo 进行联调,有空补上。

你可能感兴趣的:(WebRTC,ubuntu,音视频,服务器)