webRTC服务器搭建(基于Janus)与Demo运行

原文网址:https://blog.csdn.net/newchenxf/article/details/110451532
转载请注明出处^^

前言

2020年,直播带货不要太火,直播的方案基于啥?相信webRTC方案有一席之地。还有虽然没爆发,但各大公司在做的云游戏,也很多是基于webRTC。

webRTC服务器端最主要需要搭建2个东西,一个是STUN/TURN 服务器,一个是信令服务器。第一个好办,现成的coturn即可。甚至如果在内部局域网运行demo,还不需要这个。而信令服务器,可以自己裸搭建,也可以有封装的比较好的方案,比较出名的是Janus。

关于Janus搭建,网上已经有一些文章,但是我参考搭建,就是跑不起来,遇到了不少坑,所以基于最新的Janus代码(2020-12),重新说明一下搭建流程。

本文先介绍如何搭建Janus服务器(服务器使用Ubuntu 18),然后再介绍如何运行Demo,包括Android客户端和网页端。

1. 搭建Janus服务器

1.1 下载源码

到github官网先瞅瞅:
https://github.com/meetecho/janus-gateway

README 安装步骤说的挺清楚,但是不是每一步都必须。

1.2 安装Janus的依赖库

部分直接命令行安装,部分通过源码安装。下面分情况说明。

1.2.1 命令行安装

sudo aptitude install libmicrohttpd-dev libjansson-dev \
	libssl-dev libsrtp-dev libsofia-sip-ua-dev libglib2.0-dev \
	libopus-dev libogg-dev libcurl4-openssl-dev liblua5.3-dev \
	libconfig-dev pkg-config gengetopt libtool automake

请注意安装结果,如果有某一个安装失败,又是官网要求必须装的,则需要人肉解决一下。我就遇到坑,libcurl4没装成功,后面demo死活运行失败,最后发现是这个没装来着。

1.2.2 源码安装libnice

libnice,这是一个建立ice连接必须的库。
官网推荐的安装方法,依赖比较多,需要Python 3, Meson and Ninja

git clone https://gitlab.freedesktop.org/libnice/libnice
cd libnice
meson --prefix=/usr build && ninja -C build && sudo ninja -C build install

我是没安装成功,所以找了老版本,直接make,很快啊
先打开这个地址:https://launchpad.net/ubuntu/+source/libnice/0.1.16-1
下载libnice_0.1.16.orig.tar.gz
然后解压,然后执行:

  ./configure && make && sudo make install

1.2.3 源码安装libwebsocket

WebSocket不是必须的,但是最好装一下,因为websocket也很常用,Demo也会用。

git clone https://libwebsockets.org/repo/libwebsockets
cd libwebsockets
# If you want the stable version of libwebsockets, uncomment the next line
# git checkout v3.2-stable
mkdir build
cd build
# See https://github.com/meetecho/janus-gateway/issues/732 re: LWS_MAX_SMP
cmake -DLWS_MAX_SMP=1 -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" ..
make && sudo make install

如果安装失败,直接去libwebsockets github官网找一下原因。安装成功后,在编译 janus 时,janus 默认会增加对 WebSocket 的集成,或者通过增加编译参数 –enable-websockets 打开 WebSocket 开关。

1.2.4 源码安装libsrtp

这一个不是绝对必须的,因为前面命令行也安装了libsrtp-dev。事实上,运行起demo是没有问题的,如果你只跑demo,可以跳过。只不过,Janus官网建议,如果你的libsrtp版本低于1.5,建议升级到最新的,比如2.2,因为之前的版本可能有bug,会引入问题。所以如果是正儿八经运行,那就装最新的吧。

那么,怎么看你装的是哪个版本?简单,执行:

sudo apt-get install libsrtp-dev

会提示已经按照过。我这确实是老版本。

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libsrtp0-dev' instead of 'libsrtp-dev'
libsrtp0-dev is already the newest version (1.4.5~20130609~dfsg-2ubuntu1).

装新版本,官网也给出命令了:

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 && sudo make install

更新到2.2后,不需要pkg-config来修改版本,因为这个库名字和老版本不一样,叫做libsrtp2,不会冲突。如果有libsrtp2,Janus会自动选这个库。

1.2.5 源码安装usrsctp

这个也是非必须,如果要起用Data Channel才需要,这个干麽用的?简单说,就是推拉流运行起来后,如果需要文本互相发送,会用到。比如你给主播发个消息啥的。
要安装的话,命令如下。装好以后,编译janus会默认启用data channel。

git clone https://github.com/sctplab/usrsctp
cd usrsctp
./bootstrap
./configure --prefix=/usr --disable-programs --disable-inet --disable-inet6
make && sudo make install

1.3 编译Janus

好了,依赖都准备好了,可以编译janus了。

git clone https://github.com/meetecho/janus-gateway.git
sh autogen.sh
./configure --prefix=/opt/janus --enable-websockets
make
sudo make install

如果还想生成帮助文档,可以加一个–enable-docs,编译前,还需要装一下doxygen等辅助工具。

aptitude install doxygen graphviz

执行完configure,会打印janus的配置信息,我这打印如下:

Compiler:                  gcc
libsrtp version:           2.x
SSL/crypto library:        OpenSSL
DTLS set-timeout:          not available
Mutex implementation:      GMutex (native futex on Linux)
DataChannels support:      no
Recordings post-processor: no
TURN REST API client:      yes
Doxygen documentation:     no
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

可以看到,没有配置Data Channel,这不影响Demo运行起来,但如果你需要消息通道,需要根据官网再安装一下依赖。

1.4 运行Janus

Janus运行需要参数配置,安装目录已经有配置的sample。
目录在:/opt/janus/etc/janus/
进入以后,发现有很多jcfg.sample文件,比如janus.jcfg.sample, janus.transport.websockets.jcfg.sample。把这些都复制成可被读取的文件。没错,是全部复制,有10来个,不要怕。
例子

sudo cp -rf janus.jcfg.sample janus.jcfg

全部复制好了,接下来要稍微改一下东西。

Janus默认的配置,是没有enable SSL的,意味着,https和wss不支持。而有些浏览器,要拉起摄像头,必须是加密的。所以,如果你Demo运行不起来,可以服务端配置一下SSL。
具体说明如下。

1.4.1 生成ssl的证书

cd ~
mkdir ssl
cd ssl
# Gen ssl certs:
openssl req -new -newkey rsa:4096 -nodes -keyout key.pem -out cert.csr
openssl x509 -req -sha256 -days 365 -in cert.csr -signkey key.pem -out cert.pem
chmod 600 cert.csr
chmod 600 cert.pem
chmod 600 key.pem

当然了,我们这个是私人证书,Demo没问题,如果是公司性质,则需要弄个正式的证书来。如果对SSL证书不太了解,可以看这个文章:https://www.runoob.com/w3cnote/http-vs-https.html

1.4.2 配置文件修改ssl信息

打开/opt/janus/etc/janus/janus.jcfg,certificates修改key路径

certificates: {
        cert_pem = "/home/chenxiaofeng/ssl/cert.pem"
        cert_key = "/home/chenxiaofeng/ssl/key.pem"
        cert_pwd = "123456"
        #dtls_accept_selfsigned = false
        #dtls_ciphers = "your-desired-openssl-ciphers"
        #rsa_private_key = false
}

再打开/opt/janus/etc/janus/janus.transport.http.jcfg, 修改generals和certificates,启用https

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
      
        https = true                                    # Whether to enable HTTPS (default=false)
        secure_port = 8089                              # Web server HTTPS port, if enabled

}


certificates: {
        cert_pem = "/home/chenxiaofeng/ssl/cert.pem"
        cert_key = "/home/chenxiaofeng/ssl/key.pem"
        cert_pwd = "123456"
        #ciphers = "PFS:-VERS-TLS1.0:-VERS-TLS1.1:-3DES-CBC:-ARCFOUR-128"
}

再打开/opt/janus/etc/janus/janus.transport.websockets.jcfg, 修改generals和certificates,启用wss。

general: {

        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
}

certificates: {
        cert_pem = "/home/chenxiaofeng/ssl/cert.pem"
        cert_key = "/home/chenxiaofeng/ssl/key.pem"
        cert_pwd = "123456"
        #ciphers = "PFS:-VERS-TLS1.0:-VERS-TLS1.1:-3DES-CBC:-ARCFOUR-128"
}

1.4.3 运行起来

执行命令:

/opt/janus/bin/janus --debug-level=7

启动后端口就会打印一些日志信息,请关注。

Janus commit: 8491eb860bf7fdcee94b5fdec9e9e430fbe2421c
Compiled on:  2020? 11? 26? ??? 18:19:39 CST

Logger plugins folder: /opt/janus/lib/janus/loggers
[WARN] 	Couldn't access logger plugins folder...
---------------------------------------------------
  Starting Meetecho Janus (WebRTC Server) v0.10.8
---------------------------------------------------

[janus.jcfg]
   ....

[WARN] Janus is deployed on a private address (192.168.1.107) but you didn't specify any STUN server! Expect trouble if this is supposed to work over the internet and not just in a LAN...
Using certificates:
	/home/chenxiaofeng/ssl/cert.pem
	/home/chenxiaofeng/ssl/key.pem
Crypto: OpenSSL pre-1.1.0
[WARN] The libsrtp installation does not support AES-GCM profiles
Fingerprint of our certificate: ED:9C:80:16:2B:E6:C6:4E:43:B3:A5:8A:60:C3:0D:9B:44:B7:35:23:42:6C:03:6F:05:20:EE:1D:85:40:17:76
[WARN] Data Channels support not compiled
[WARN] Event handlers support disabled
Plugins folder: /opt/janus/lib/janus/plugins
Sessions watchdog started
Joining Janus requests handler thread
Loading plugin 'libjanus_sip.so'...
JANUS SIP plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.sip.jcfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.sip.jcfg'... error 2 (No such file or directory)
[WARN] Couldn't find .jcfg configuration file (janus.plugin.sip), trying .cfg
Configuration file: /opt/janus/etc/janus/janus.plugin.sip.cfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.sip.cfg'... error 2 (No such file or directory)
Local IP set to 192.168.1.107
JANUS SIP plugin initialized!
	Version: 8 (0.0.8)
	   [janus.plugin.sip] JANUS SIP plugin
	   This is a simple SIP plugin for Janus, allowing WebRTC peers to register at a SIP server and call SIP user agents through a Janus instance.
	   Plugin API version: 15
Loading plugin 'libjanus_audiobridge.so'...
Joining SIP handler thread
JANUS AudioBridge plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.audiobridge.jcfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.audiobridge.jcfg'... error 2 (No such file or directory)
[WARN] Couldn't find .jcfg configuration file (janus.plugin.audiobridge), trying .cfg
Configuration file: /opt/janus/etc/janus/janus.plugin.audiobridge.cfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.audiobridge.cfg'... error 2 (No such file or directory)
JANUS AudioBridge plugin initialized!
	Version: 11 (0.0.11)
	   [janus.plugin.audiobridge] JANUS AudioBridge plugin
	   This is a plugin implementing an audio conference bridge for Janus, mixing Opus streams.
	   Plugin API version: 15
Loading plugin 'libjanus_videocall.so'...
Joining AudioBridge handler thread
JANUS VideoCall plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.videocall.jcfg
[janus.plugin.videocall.jcfg]
    general: {
    }
JANUS VideoCall plugin initialized!
	Version: 6 (0.0.6)
	   [janus.plugin.videocall] JANUS VideoCall plugin
	   This is a simple video call plugin for Janus, allowing two WebRTC peers to call each other through a server.
	   Plugin API version: 15
Loading plugin 'libjanus_streaming.so'...
Joining VideoCall handler thread
JANUS Streaming plugin created!
[WARN] libcurl not available, Streaming plugin will not have RTSP support
Configuration file: /opt/janus/etc/janus/janus.plugin.streaming.jcfg
[janus.plugin.streaming.jcfg]
   ....
Adding Streaming mountpoint 'rtp-sample'
Audio enabled, Video enabled, Data NOT enabled
Audio enabled, Video enabled, Data NOT enabled
Adding Streaming mountpoint 'file-live-sample'
Starting streaming relay thread
Adding Streaming mountpoint 'file-ondemand-sample'
  ::: [1][rtp-sample] Opus/VP8 live stream coming from external source (live, RTP source, public, pin: no pin)
  ::: [2][file-live-sample] a-law file source (radio broadcast) (live, file source, public, pin: no pin)
  ::: [3][file-ondemand-sample] mu-law file source (music) (on demand, file source, public, pin: no pin)
Filesource (live) thread starting...
[file-live-sample] Opening file source /opt/janus/share/janus/streams/radio.alaw...
[file-live-sample] Streaming audio file: /opt/janus/share/janus/streams/radio.alaw
JANUS Streaming plugin initialized!
	Version: 8 (0.0.8)
	   [janus.plugin.streaming] JANUS Streaming plugin
	   This is a streaming plugin for Janus, allowing WebRTC peers to watch/listen to pre-recorded files or media generated by an external source.
	   Plugin API version: 15
Loading plugin 'libjanus_echotest.so'...
Joining Streaming handler thread
JANUS EchoTest plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.echotest.jcfg
[janus.plugin.echotest.jcfg]
    general: {
    }
JANUS EchoTest plugin initialized!
	Version: 7 (0.0.7)
	   [janus.plugin.echotest] JANUS EchoTest plugin
	   This is a trivial EchoTest plugin for Janus, just used to showcase the plugin interface.
	   Plugin API version: 15
Loading plugin 'libjanus_nosip.so'...
Joining EchoTest handler thread
JANUS NoSIP plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.nosip.jcfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.nosip.jcfg'... error 2 (No such file or directory)
[WARN] Couldn't find .jcfg configuration file (janus.plugin.nosip), trying .cfg
Configuration file: /opt/janus/etc/janus/janus.plugin.nosip.cfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.nosip.cfg'... error 2 (No such file or directory)
Local IP set to 192.168.1.107
....
Adding VideoRoom room 'room-1234'
Created VideoRoom: 1234 (Demo Room, public, opus/vp8 codecs, secret: adminpwd, pin: no pin, pvtid: optional)
Adding VideoRoom room 'room-5678'
Created VideoRoom: 5678 (VP9-SVC Demo Room, public, opus/vp9 codecs, secret: adminpwd, pin: no pin, pvtid: optional)
  ::: [5678][VP9-SVC Demo Room] 512000, max 6 publishers, FIR frequency of 10 seconds, opus audio codec(s), vp9 video codec(s)
  ::: [1234][Demo Room] 128000, max 6 publishers, FIR frequency of 10 seconds, opus audio codec(s), vp8 video codec(s)
JANUS VideoRoom plugin initialized!
	Version: 9 (0.0.9)
	   [janus.plugin.videoroom] JANUS VideoRoom plugin
	   This is a plugin implementing a videoconferencing SFU (Selective Forwarding Unit) for Janus, that is an audio/video router.
	   Plugin API version: 15
Loading plugin 'libjanus_textroom.so'...
Joining VideoRoom handler thread
Joining RTCP thread for RTP forwarders...
JANUS TextRoom plugin created!
[WARN] Data channels support not compiled, disabling TextRoom plugin
[WARN] The 'janus.plugin.textroom' plugin could not be initialized
Loading plugin 'libjanus_recordplay.so'...
JANUS Record&Play plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.recordplay.jcfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.recordplay.jcfg'... error 2 (No such file or directory)
[WARN] Couldn't find .jcfg configuration file (janus.plugin.recordplay), trying .cfg
Configuration file: /opt/janus/etc/janus/janus.plugin.recordplay.cfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.recordplay.cfg'... error 2 (No such file or directory)
[FATAL] [plugins/janus_recordplay.c:janus_recordplay_init:749] No recordings path specified, giving up...
[WARN] The 'janus.plugin.recordplay' plugin could not be initialized
Loading plugin 'libjanus_voicemail.so'...
JANUS VoiceMail plugin created!
Configuration file: /opt/janus/etc/janus/janus.plugin.voicemail.jcfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.voicemail.jcfg'... error 2 (No such file or directory)
[WARN] Couldn't find .jcfg configuration file (janus.plugin.voicemail), trying .cfg
Configuration file: /opt/janus/etc/janus/janus.plugin.voicemail.cfg
[ERR] [config.c:janus_config_parse:191]   -- Error reading configuration file 'janus.plugin.voicemail.cfg'... error 2 (No such file or directory)
....
libwebsockets logging: 0
WebSockets server started (port 8188)...
Using certificates:
	/home/chenxiaofeng/ssl/cert.pem
	/home/chenxiaofeng/ssl/key.pem
Secure WebSockets server started (port 8989)...
[WARN] Admin WebSockets server disabled
[WARN] Secure Admin WebSockets server disabled
JANUS WebSockets transport plugin initialized!
	Version: 1 (0.0.1)
	   [janus.transport.websockets] JANUS WebSockets transport plugin
	   This transport plugin adds WebSockets support to the Janus API via libwebsockets.
	   Plugin API version: 8
	   Janus API: enabled
	   Admin API: disabled
Loading transport plugin 'libjanus_pfunix.so'...
WebSockets thread started
JANUS Unix Sockets transport plugin created!
Configuration file: /opt/janus/etc/janus/janus.transport.pfunix.jcfg
[WARN] Unix Sockets server disabled (Janus API)
[WARN] Unix Sockets server disabled (Admin API)
[WARN] No Unix Sockets server started, giving up...
[WARN] The 'janus.transport.pfunix' plugin could not be initialized

启动日志有一些点需要关注:

(a) STUN server警告

[WARN] Janus is deployed on a private address (192.168.1.107) but you didn't specify any STUN server! Expect trouble if this is supposed to work over the internet and not just in a LAN...

上面的警告,对于我们运行Demo是没问题的。因为Demo一般运行在局域网,没有P2P打通的需要。但如果你是正式使用。则需要解决这个WARNING。
具体而言,就是安装STUN server,然后打开
/opt/janus/etc/janus/janus.jcfg
修改以下内容

nat: {
        #stun_server = "stun.voip.eutelia.it"
        #stun_port = 3478
..
        #turn_server = "myturnserver.com"
        #turn_port = 3478
        #turn_type = "udp"
        #turn_user = "myuser"
        #turn_pwd = "mypassword"

安装STUN server其实就是安装coturn。很简单

git clone https://github.com/coturn/coturn
cd coturn
./configure 
make 
sudo make install

安装coturn服务器不一定和janus一起,也可以一起。总之,装好后把信息写到janus的配置文件中。
有关coturn的介绍和安装,还可以见:https://medium.com/av-transcode/what-is-webrtc-and-how-to-setup-stun-turn-server-for-webrtc-communication-63314728b9d0

(b)Janus 的视频会议插件运行成功

JANUS VideoRoom plugin initialized!

如果没上面那句话,可能代表插件安装有问题,需要解决。

(c) Janus的websocket启动成功

WebSockets thread started

代表websocket启动成功。

(d)Janus的http启动成功

HTTP webserver started (port 8088, /janus path listener)...

1.4.4 检查运行端口

可以通过端口查看是否运行正常。

lsof -i | grep janus
janus   32306 chenxiaofeng    5u  IPv6  887081      0t0  UDP *:rfe 
janus   32306 chenxiaofeng    6u  IPv6  887082      0t0  UDP *:5004 
janus   32306 chenxiaofeng   13u  IPv6  884414      0t0  TCP *:omniorb (LISTEN)
janus   32306 chenxiaofeng   16u  IPv6  884415      0t0  TCP *:8089 (LISTEN)
janus   32306 chenxiaofeng   21u  IPv4  884417      0t0  TCP *:8188 (LISTEN)
janus   32306 chenxiaofeng   24u  IPv4  884418      0t0  TCP *:8989 (LISTEN)

8089是https, 8188是ws, 8989是wss(websocket secure)

2. 运行网页端Demo

网页端的demo,在janus下载的源码就有了。在源码根目录的html目录下。

现在就是要把这个html发布出来,从而其外部的浏览器,能够访问。

用什么发布呢,这里介绍http-server。

sudo apt-get install nodejs
sudo npm -g install http-server

安装好以后,进入janus源码的html目录。然后执行http-server。

> http-server 
Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://192.168.1.107:8080
Hit CTRL-C to stop the server

此时,就把demo发布出来了,根据提示,打开http://192.168.1.107:8080。是一个网页。

有2个经典的测试可以看。一个是Echo Test,这个是把电脑的视频推出去,同时拉回来。
webRTC服务器搭建(基于Janus)与Demo运行_第1张图片

还有一个是Video Room,就是视频会议。
webRTC服务器搭建(基于Janus)与Demo运行_第2张图片
点击start
webRTC服务器搭建(基于Janus)与Demo运行_第3张图片
随便写一个名称,然后加入,默认就进了1234的房间。如果你再打开一个网页,再写个名字加入,就有2个人了。demo最多支持6个人。
webRTC服务器搭建(基于Janus)与Demo运行_第4张图片

注意,如果要用websocket的ws或者wss,需要修改html/echotest.js或者html/videoroomtest.js文件,把server字段改一下,例如:

var server = "ws://" + window.location.hostname + ":8188";

另外,高能预警 web的demo,默认都是走http的,这在有些环境下的浏览器,是无法拉起摄像头的(权限问题),如果Demo运行失败,可以尝试https!!!我这里就遇到了,http死活不行,https稳得一匹!

当然了,我们用http-server发布,默认是不支持https的。如果要发布https,则启动http-server的命令,需要改一下,加上SSL的参数(想了解更多http-server如何配置SSL,见https://www.npmjs.com/package/http-server):

> http-server --ssl --cert /home/chenxiaofeng/ssl/cert.pem --key /home/chenxiaofeng/ssl/key.pem
Starting up http-server, serving ./ through https
Available on:
  https://127.0.0.1:8080
  https://192.168.1.107:8080

启动后,浏览器打开,会有警告,原因是我们ssl的key是自己随便生成的,没啥问题。点击最下面的链接即可。
webRTC服务器搭建(基于Janus)与Demo运行_第5张图片

打开以后,就可以玩Demo了!

3. 运行Android端Demo

除了网页端demo,android也可以做一样的事情。
下载janus的android demo源码。
https://github.com/meetecho/janus-mobile-sdk

尴尬的是,android的demo怎么编译,介绍的比较少。好在我摸索出来了。
首先,到源码目录,执行make。
make其实就是下载一些关键的第三方库,比如boringssl,编译demo要用。具体可以看根目录的Makefile。
make前,最好手动删除third_party整个目录,我这发现当前的最新代码(2020-12-07),默认有third_party/boringssl空目录,这会导致以为有了这个模块,不会下载。

另外,make过程中,还会提示没有装go,无法继续下载。咋办,那就装一个。

sudo apt-get install golang

接下来。AndroidStudio打开,引入工程即可。
目录是janus-mobile-sdk/examples/android

理论上,可以编译成功。

运行例子如下
webRTC服务器搭建(基于Janus)与Demo运行_第6张图片
输入框写地址,一个是IP,一个是端口。8088就是Janus的http端口。

点击Echo Test,然后点击START, CALL,即可运行成功。
webRTC服务器搭建(基于Janus)与Demo运行_第7张图片

除了官方的Demo,也有人自己纯通过websocket+rtc实现了android客户端。
这里我找到一个可运行的是:
https://github.com/lesliebeijing/JanusAndroidDemo

不过跑起来会崩溃,一方面是权限的代码要改一下,一方面是,websocket链接会失败。报错是:

CLEARTEXT communication to 192.168.1.107 not permitted by network security policy

原因,见:https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted

解决办法:
AndroidManifest加一个
webRTC服务器搭建(基于Janus)与Demo运行_第8张图片

参考

https://zhuanlan.zhihu.com/p/201113990
https://medium.com/agileops/install-janus-webrtc-ubuntu-server-18-04-4-b7e855de772e
https://www.qnrtc.com/posts/1195716763.html

你可能感兴趣的:(webRTC)