安装环境为Centos7
#!/bin/sh
yum install -y epel-release && \
yum update -y && \
yum install -y deltarpm && \
yum install -y openssh-server sudo which file curl zip unzip wget && \
yum install -y libmicrohttpd-devel jansson-devel libnice-devel glib22-devel opus-devel libogg-devel pkgconfig gengetopt libtool autoconf automake make gcc gcc-c++ git cmake libconfig-devel openssl-devel
#upgrade libsrtp 1.5.4
#wget https://github.com/cisco/libsrtp/archive/v1.5.4.tar.gz
#tar xfv v1.5.4.tar.gz
#cd libsrtp-1.5.4
#./configure --prefix=/usr --enable-openssl
#make shared_library && sudo make install
#cd -
wget https://github.com/cisco/libsrtp/archive/v2.0.0.tar.gz
tar xfv v2.0.0.tar.gz
cd libsrtp-2.0.0
./configure --prefix=/usr --enable-openssl
make shared_library && sudo make install
cd -
#install sofia-sip for sip-gateway plugin
wget https://sourceforge.net/projects/sofia-sip/files/sofia-sip/1.12.11/sofia-sip-1.12.11.tar.gz
tar zxf sofia-sip-1.12.11.tar.gz && cd sofia-sip-1.12.11 && ./configure --prefix=/usr CFLAGS=-fno-aggressive-loop-optimizations && make && make install
cd -
#install usrsctp for Data channel support
git clone https://github.com/sctplab/usrsctp && cd usrsctp && \
./bootstrap && \
./configure --prefix=/usr && make && make install
cd -
#install libwebsocket for android or ios instead of http/https
git clone https://github.com/warmcat/libwebsockets && \
mkdir libwebsockets/build && cd libwebsockets/build && \
cmake -DMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" .. && \
make && make install
cd -
#Janus
#if cannot configure plugin sofia,Perhaps you should add the directory containing `sofia-sip-ua.pc' to the PKG_CONFIG_PATH environment variable,
#for example centos7 :export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig
#if cannot load libsofia-sip-ua.so.0 , try ldconfig -v
git clone https://github.com/meetecho/janus-gateway.git && \
cd janus-gateway &&\
sh autogen.sh && \
./configure --prefix=/opt/janus --disable-rabbitmq --disable-docs &&\
make && make install && make configs
上述脚本整理了sip(sofia)和 datachannel插件安装,消息通道额外支持了websocket
1.若要使用libsrtp1.5.4,需要在configure janus 时加上 --disable-libsrtp2
2.如果需要支持mqtt可以安装paho.mqtt.c后重新configure janus编译安装
git clone https://github.com/eclipse/paho.mqtt.c.git
cd paho.mqtt.c
make && sudo make install
其他插件可以参考janus-gateway
nohup /opt/janus/bin/janus >> /var/log/janus.log 2>&1 &
可以通过 如下命令查看janus已经起来
netstat -anp | grep janus
tcp 0 0 0.0.0.0:8188 0.0.0.0:* LISTEN 3946/janus
tcp6 0 0 :::18000 :::* LISTEN 3946/janus
tcp6 0 0 :::18001 :::* LISTEN 3946/janus
tcp6 0 0 172.19.129.176:18001 117.136.8.142:58763 ESTABLISHED 3946/janus
tcp6 0 0 172.19.129.176:18001 117.136.8.142:58762 ESTABLISHED 3946/janus
tcp6 0 0 172.19.129.176:18001 117.136.8.142:58761 ESTABLISHED 3946/janus
udp 0 0 0.0.0.0:5002 0.0.0.0:* 3946/janus
udp 0 0 0.0.0.0:5004 0.0.0.0:* 3946/janus
udp 0 0 0.0.0.0:47277 0.0.0.0:* 3946/janus
udp6 0 0 :::36906 :::* 3946/janus
raw 0 0 0.0.0.0:132 0.0.0.0:* 7 3946/janus
raw6 0 0 :::132 :::* 7 3946/janus
janus安装目录在/opt/janus
./bin | ./etc | ./include | ./lib | ./share |
---|---|---|---|---|
可执行文件 | janus配置文件 | janus头文件 | janus库 | 存放脚本或者文档,web demo也在这里 |
janus基于插件思想,通过实现基础架构,完成了与浏览器链接的建立过程。配置文件比较清晰,按不同插件配置
janus.cfg
可能需要配置下turn,该配置文件提供两种方式turn的配置
1.静态配置的turn server
[nat]
turn_server = 127.0.0.1
turn_port = 3478
turn_type = udp
turn_user = username
turn_pwd = password
2.配置TURN REST API动态获取turn 服务
turn_rest_api = http://yourbackend.com/path/to/api
turn_rest_api_key = anyapikeyyoumayhaveset
turn_rest_api_method = GET
其他相关配置需要的话可以参考对应注释使用
janus.transport.http.cfg
本文使用的消息通道是http,以http配置为例
[general]
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 = yes ; Whether to enable the plain HTTP interface
port = 18000 ; 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 = yes ; Whether to enable HTTPS (default=no)
secure_port = 18001 ; Web server HTTPS port, if enabled
在这里可以配置监听端口线程数等,支持http和https
janus.plugin.sip.cfg
以sip(sofia)插件为例子
[general]
; Specify which local IP address to use. If not set it will be automatically
; guessed from the system
;local_ip = 1.2.3.4
; Enable local keep-alives to keep the registration open. Keep-alives are
; sent in the form of OPTIONS requests, at the given interval inseconds.
; (0 to disable)
keepalive_interval = 120
; Indicate if the server is behind NAT. If so, the server will use STUN
; to guess its own public IP address and use it in the Contact header of
; outgoing requests
behind_nat = no
; User-Agent string to be used
; user_agent = Cool WebRTC Gateway
; Expiration time for registrations
register_ttl = 3600
; Range of ports to use for RTP/RTCP (default=10000-60000)
rtp_port_range = 40000-60000
可以配置rtp端口范围等,若服务部署在nat网络下可以配置behind_nat = yes,则服务端会通过turn进行网络穿透
janus的demo目录在/opt/janus/share/janus/demos
配置http服务
本文使用nginx配置,centos 下可以直接通过命令安装
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install -y nginx
启动
sudo systemctl start nginx.service
配置支持http和https访问
1 .若使用自签名证书
sudo mkdir /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
2 .配置nginx
vim /etc/nginx/conf.d/default.conf
server {
listen 80;
listen *:443 ssl;
server_name localhost;
location / {
root /opt/janus/share/janus/demos;
index index.html index.htm index.php;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
}
#执行下面命令使配置生效
nginx -s reload
访问demo
chrome中使用webrtc必须通过https访问,在浏览器中访问地址
https://yourip_or_domain
如果无法访问或者资源加载不完整,可检查防火墙或者代理,配置该ip和cdnjs.cloudflare.com不通过代理直接访问
以siptest demo(SIP Gateway (Sofia) )为例子
若点击start出现上述问题是因为自签证书问题导致,可访问https://yourip_or_domain:port会提示证书问题点击继续访问信任,再返回start siptest即可,port为janus的 https监听端口
正常开始后,如下配置点击register 即可开始sip呼叫
Extension
默认demo里面没有配置iceserver,如果服务端报错ice fail可以在客户端代码加上iceserver
vim siptest.js
janus = new Janus(
{
server: server,
iceServers: [{urls: "turn:turnip:port", username: "username", credential: "password"}],
success: function() {
demo中默认没有启用录音功能,但是每个插件都有提供录音接口,可在源码内查看对应REST API说明,在siptest中录音接口说明如下
{
"request" : "recording",
"action" : ""
"audio" : ,
"video" : ,
"peer_audio" : ,
"peer_video" : ,
"filename" : " "
}
开始录音
sipcall.send({"message": { "request": "recording", "action":"start","audio":true,"peer_audio":true,"filename":"/opt/janus/share/janus/recordings/" + $('#authuser').val() + "to" + $('#peer').val() + "_" + getNowFormatDate()}});
停止录音
sipcall.send({"message": { "request": "recording", "action":"stop"}});
其他插件如videocall videoroom类似可以录音
git clone https://github.com/coturn/coturn.git
cd coturn
./configure
make && make install
除了janus还可以选用medooze,最近在webrtchacks上有文章对janus mediasoup medooze等webrtc server 进行SFU负载测试(文章链接),medooze性能表现比较优秀,代码也比较易读,听webrtc中文群里大牛说medooze近期可能会有大的更新貌似,会有全端客户端开源,到时再学习 - -