123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /**
- * 将数据打包成ntv接口返回格式
- * @param {json} msg
- */
- function ntvMsg(msg){
- if("result" in msg){
- msg.code = msg.result?0:1;
- delete msg.result;
- }
- if("message" in msg){
- msg.err_desc = msg.message;
- delete msg.message;
- }
-
- return msg;
- }
-
- //获取正在直播的ssrc列表
- function getSsrcs(req, res) {
- let result = { code: 0, err_desc: 'OK' };
- var arr = [];
- this.ssrcs.forEach(function (obj, ssrc) {
- obj.ssrc = ssrc;
- arr.push(obj);
- });
- result.code = 0;
- result.data = {};
- result.data.count=arr.length;
- result.data.items = arr;
- res.json(result);
- }
-
- //获取所有SIP会话
- function getSessions(req, res, next) {
- let result = { code: 0, err_desc: 'OK' ,data:{}};
- let items = [];
- let index = 0;
- this.sessions.forEach(function (session, id) {
- if (session.TAG === 'sip')
- items[index++] = { id:session.id,host: session.via.host, port: session.via.port, info: session.deviceinfo, status: session.devicestatus, catalog: session.catalog, playmode:session.playmode };
- });
- result.data.count=index;
- result.data.items=items;
- res.json(result);
- }
-
- //获取指定设备ID的目录数据
- //改造成返回某个设备的信息
- function getCatalog(req, res) {
- let result = { code: 1, err_desc: 'OK' };
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
- result.code = 0;
- result.data = {};
- result.data.count=1;
- result.data.items = [{ id:session.id, host: session.via.host, port: session.via.port, info: session.deviceinfo, status: session.devicestatus, catalog: session.catalog, playmode:session.playmode }];
- }
- else {
- result.err_desc = '设备不在线或没有注册!';
- }
- res.json(result);
- }
- //原有版本
- /*function getCatalog(req, res) {
- let result = { result: false, message: 'OK' };
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
-
- result.result = true;
- result.data = session.catalog;
- }
- else {
- result.message = 'device not online.';
- }
- res.json(result);
- }*/
-
- //预览请求
- async function realplay(req, res) {
-
- let result = { result: true, message: 'OK' };
-
- if (this.sessions.has(req.params.device)) {
-
- let session = this.sessions.get(req.params.device);
-
- //判断当前设备通道里是否存在通道编码
- let channelId = req.params.channel;
-
- let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
-
- if (channel) {
- switch (req.params.action) {
- case 'start':
- {
- result = await session.sendRealPlayMessage(channelId, req.params.host, req.params.port, req.params.mode);
- }
- break;
- case 'stop':
- {
- result = await session.sendStopRealPlayMessage(channelId, req.params.host, req.params.port);
- }
- break;
- default:
- {
- result.result = false;
- result.message = 'action error.';
- }
- break;
- }
- }
- else {
- result.result = false;
- result.message = 'device not found.';
- }
- }
- else {
- result.result = false;
- result.message = 'device not online.';
- }
- res.json(ntvMsg(result));
- }
-
- //回看请求
- async function playback(req, res) {
- let result = { result: true, message: 'OK' };
-
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
-
- //判断当前设备通道里是否存在通道编码
- let channelId = req.params.channel;
-
- let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
-
- if (channel) {
- switch (req.params.action) {
- case 'start':
- {
- result = await session.sendPlaybackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port, req.params.mode);
- }
- break;
- case 'stop':
- {
- result = await session.sendStopPlayBackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port);
- }
- break;
- default:
- {
- result.result = false;
- result.message = 'action error.';
- }
- break;
- }
- }
- else {
- result.result = false;
- result.message = 'device not found.';
- }
- }
- else {
- result.result = false;
- result.message = 'device not online';
- }
- res.json(ntvMsg(result));
- }
-
- //回看播放控制
- async function playControl(req, res) {
- let result = {};
-
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
-
- //判断当前设备通道里是否存在通道编码
- let channelId = req.params.channel;
-
- let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
-
- if (channel) {
- result = await session.sendPlayControlMessage(req.params.channel, req.params.begin, req.params.end, req.params.cmd, req.params.value);
- }
- else {
- result.result = false;
- result.message = 'device not found.';
- }
- }
- else {
- result.result = false;
- result.message = 'device not online.';
- }
- res.json(ntvMsg(result));
- }
-
- //云台控制
- function ptzControl(req, res) {
- let result = {};
-
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
-
- //判断当前设备通道里是否存在通道编码
- let channelId = req.params.channel;
- let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
-
- if (channel) {
- session.ControlPTZ(req.params.channel, req.params.value);
-
- result.result = true;
- result.message = 'OK';
- }
- else {
- result.result = false;
- result.message = 'device not found.';
- }
- }
- else {
- result.result = false;
- result.message = 'device not online.';
- }
- res.json(ntvMsg(result));
- }
-
- //录像文件查询
- async function recordQuery(req, res) {
- let result = {};
-
- if (this.sessions.has(req.params.device)) {
- let session = this.sessions.get(req.params.device);
-
- //判断当前设备通道里是否存在通道编码
- let channelId = req.params.channel;
- let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
-
- if (channel) {
- if (req.params.begin < req.params.end) {
-
- //unix时间转换
- var beginTime = new Date(req.params.begin * 1000).toJSON();
- var endTime = new Date(req.params.end * 1000).toJSON();
-
- result.data = await session.getRecordInfos(req.params.channel, beginTime, endTime);
-
- result.result = true;
- result.message = 'OK';
- }
- else {
- result.result = false;
- result.message = "beginTime 必须小于 endTime.";
- }
- }
- else {
- result.result = false;
- result.message = 'device not found.';
- }
- }
- else {
- result.result = false;
- result.message = 'device not online.';
- }
- res.json(ntvMsg(result));
- }
-
- //关闭流
- function closeStream(req, res) {
- let body = req.body;
-
- let result = { code: 0, msg: 'success' };
-
- if (body.stream) {
- //16位进制转10进制
- let ssrc = parseInt(body.stream, 16);
- //要补位
-
- ssrc = _prefixInteger(ssrc, 10);
-
- let selectSession = null;
- let selectDialog = null;
-
- for (session in this.sessions.values()) {
- let dialogs = sessson.dialogs;
- for (var key in dialogs) {
- let dialog = dialogs[key];
- if (dialog.ssrc && dialog.ssrc === ssrc) {
- selectSession = session;
- selectDialog = dialog;
- return;
- }
- }
- }
-
- if (selectDialog != null && selectSession != null) {
- if (selectDialog.play) {
- switch (selectDialog.play) {
- case 'realplay':
- {
- selectSession.StopRealPlay(selectDialog.channelId, selectDialog.host, selectDialog.port);
- }
- break;
- case 'playback':
- {
- selectSession.StopPlayBack(selectDialog.channelId, selectDialog.begin, selectDialog.end, selectDialog.host, selectDialog.port)
- }
- break;
- }
- }
- }
- }
-
- res.json(ntvMsg(result));
- }
-
- function _prefixInteger(num, m) {
- return (Array(m).join(0) + num).slice(-m);
- }
-
- module.exports = { getSsrcs: getSsrcs, getCatalog: getCatalog, realplay: realplay, getSessions: getSessions, playback: playback, ptzControl: ptzControl, playControl: playControl, recordQuery: recordQuery, closeStream: closeStream }
|