功能简介
NMS支持在服务端收到流开始播放,结束播放,开始推流,结束推流时回调一个web服务接口。
可作为后台管理程序进行自定义鉴权,统计推流播放信息包括ip,开始结束时间,url参数,使用流量等。
开启方法
打开config.ini文件,取消notify_url前的注释,填上接收回调的web api地址。
如:
notify_url = http://192.168.0.8:8008/notify
简单用例
我们使用nodejs创建一个简单的web api接口,并打印接收到的信息
notify.js:
var http = require("http");http.createServer(function(request, response) {let data = [];request.on("data", (chunk) => {data.push(chunk);});request.on("end", () => {console.log(JSON.parse(data)); // 'Buy the milk'response.writeHead(200);response.end();});}).listen(8008);// 终端打印如下信息console.log("Server running at port: 8008");
node notify.js
postPublish 开始推流
当我们使用ffmpeg命令工具向nms推送一个rtmp流,nodejs测试服务端收到如下信息
{id: '4uowyvdy4vpxqcko7ojbphs5qi3qms5e',ip: '[::1]:60116',app: 'live',name: 'stream',query: {},action: 'postPublish',protocol: 'rtmp',createtime: 1575452695215,endtime: 0,inbytes: 0,outbytes: 0}
postPlay 开始播放
当我们使用ffplay命令工具从nms播放一个rtmp流,nodejs测试服务端收到如下信息
{id: 'xs503tuy3e1fpr4g80j3xbak3z1zay6o',ip: '[::1]:60132',app: 'live',name: 'stream',query: {},action: 'postPlay',protocol: 'rtmp',createtime: 1575452825248,endtime: 0,inbytes: 0,outbytes: 0}
donePlay 结束播放
当我们停止ffplay播放,nodejs测试服务端收到如下信息
{id: 'xs503tuy3e1fpr4g80j3xbak3z1zay6o',ip: '[::1]:60132',app: 'live',name: 'stream',query: {},action: 'donePlay',protocol: 'rtmp',createtime: 1575452825248,endtime: 1575452879548,inbytes: 650,outbytes: 84781275}
donePublish 结束推流
当我们停止ffmpeg推流,nodejs测试服务端收到如下信息
{id: '4uowyvdy4vpxqcko7ojbphs5qi3qms5e',ip: '[::1]:60116',app: 'live',name: 'stream',query: {},action: 'donePublish',protocol: 'rtmp',createtime: 1575452695215,endtime: 1575453341281,inbytes: 510152763,outbytes: 0}
url参数
当我们使用ffplay播放时给url带上参数
ffplay "rtmp://localhost/live/stream?user=123&vip=0&money=666&time=50"
回调信息中query字段会格式化请求参数,可以对用户信息和行为进行判断记录等。
{id: 'nruxgp2rog0iljxchaw05p378u905vda',ip: '[::1]:60202',app: 'live',name: 'stream',query: { money: [ '666' ], time: [ '50' ], user: [ '123' ], vip: [ '0' ] },action: 'postPlay',protocol: 'rtmp',createtime: 1575453067766,endtime: 0,inbytes: 0,outbytes: 0}
回调终止流
当我们需要自定义判断条件,用以允许或禁止用户播放或推流,可以通过返回200或200以外的状态码来控制
比如上面的nodejs服务,对所有请求返回200状态码,那么所有流都允许播放或推流
response.writeHead(200);
当我们判断用户的各种条件不满足时,返回400状态码,客户端则会被关闭
response.writeHead(400);
原创文章,转载请注明: 转载自贝壳博客
本文链接地址: NMS v3系列教程之 五、事件通知