No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vag.js 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * 将数据打包成ntv接口返回格式
  3. * @param {json} msg
  4. */
  5. function ntvMsg(msg){
  6. if("result" in msg){
  7. msg.code = msg.result?0:1;
  8. delete msg.result;
  9. }
  10. if("message" in msg){
  11. msg.err_desc = msg.message;
  12. delete msg.message;
  13. }
  14. return msg;
  15. }
  16. //获取正在直播的ssrc列表
  17. function getSsrcs(req, res) {
  18. let result = { code: 0, err_desc: 'OK' };
  19. var arr = [];
  20. this.ssrcs.forEach(function (obj, ssrc) {
  21. obj.ssrc = ssrc;
  22. arr.push(obj);
  23. });
  24. result.code = 0;
  25. result.data = {};
  26. result.data.count=arr.length;
  27. result.data.items = arr;
  28. res.json(result);
  29. }
  30. //获取所有SIP会话
  31. function getSessions(req, res, next) {
  32. let result = { code: 0, err_desc: 'OK' ,data:{}};
  33. let items = [];
  34. let index = 0;
  35. this.sessions.forEach(function (session, id) {
  36. if (session.TAG === 'sip')
  37. 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 };
  38. });
  39. result.data.count=index;
  40. result.data.items=items;
  41. res.json(result);
  42. }
  43. //获取指定设备ID的目录数据
  44. //改造成返回某个设备的信息
  45. function getCatalog(req, res) {
  46. let result = { code: 1, err_desc: 'OK' };
  47. if (this.sessions.has(req.params.device)) {
  48. let session = this.sessions.get(req.params.device);
  49. result.code = 0;
  50. result.data = {};
  51. result.data.count=1;
  52. 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 }];
  53. }
  54. else {
  55. result.err_desc = '设备不在线或没有注册!';
  56. }
  57. res.json(result);
  58. }
  59. //原有版本
  60. /*function getCatalog(req, res) {
  61. let result = { result: false, message: 'OK' };
  62. if (this.sessions.has(req.params.device)) {
  63. let session = this.sessions.get(req.params.device);
  64. result.result = true;
  65. result.data = session.catalog;
  66. }
  67. else {
  68. result.message = 'device not online.';
  69. }
  70. res.json(result);
  71. }*/
  72. //预览请求
  73. async function realplay(req, res) {
  74. let result = { result: true, message: 'OK' };
  75. if (this.sessions.has(req.params.device)) {
  76. let session = this.sessions.get(req.params.device);
  77. //判断当前设备通道里是否存在通道编码
  78. let channelId = req.params.channel;
  79. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  80. if (channel) {
  81. switch (req.params.action) {
  82. case 'start':
  83. {
  84. result = await session.sendRealPlayMessage(channelId, req.params.host, req.params.port, req.params.mode);
  85. }
  86. break;
  87. case 'stop':
  88. {
  89. result = await session.sendStopRealPlayMessage(channelId, req.params.host, req.params.port);
  90. }
  91. break;
  92. default:
  93. {
  94. result.result = false;
  95. result.message = 'action error.';
  96. }
  97. break;
  98. }
  99. }
  100. else {
  101. result.result = false;
  102. result.message = 'device not found.';
  103. }
  104. }
  105. else {
  106. result.result = false;
  107. result.message = 'device not online.';
  108. }
  109. res.json(ntvMsg(result));
  110. }
  111. //回看请求
  112. async function playback(req, res) {
  113. let result = { result: true, message: 'OK' };
  114. if (this.sessions.has(req.params.device)) {
  115. let session = this.sessions.get(req.params.device);
  116. //判断当前设备通道里是否存在通道编码
  117. let channelId = req.params.channel;
  118. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  119. if (channel) {
  120. switch (req.params.action) {
  121. case 'start':
  122. {
  123. result = await session.sendPlaybackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port, req.params.mode);
  124. }
  125. break;
  126. case 'stop':
  127. {
  128. result = await session.sendStopPlayBackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port);
  129. }
  130. break;
  131. default:
  132. {
  133. result.result = false;
  134. result.message = 'action error.';
  135. }
  136. break;
  137. }
  138. }
  139. else {
  140. result.result = false;
  141. result.message = 'device not found.';
  142. }
  143. }
  144. else {
  145. result.result = false;
  146. result.message = 'device not online';
  147. }
  148. res.json(ntvMsg(result));
  149. }
  150. //回看播放控制
  151. async function playControl(req, res) {
  152. let result = {};
  153. if (this.sessions.has(req.params.device)) {
  154. let session = this.sessions.get(req.params.device);
  155. //判断当前设备通道里是否存在通道编码
  156. let channelId = req.params.channel;
  157. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  158. if (channel) {
  159. result = await session.sendPlayControlMessage(req.params.channel, req.params.begin, req.params.end, req.params.cmd, req.params.value);
  160. }
  161. else {
  162. result.result = false;
  163. result.message = 'device not found.';
  164. }
  165. }
  166. else {
  167. result.result = false;
  168. result.message = 'device not online.';
  169. }
  170. res.json(ntvMsg(result));
  171. }
  172. //云台控制
  173. function ptzControl(req, res) {
  174. let result = {};
  175. if (this.sessions.has(req.params.device)) {
  176. let session = this.sessions.get(req.params.device);
  177. //判断当前设备通道里是否存在通道编码
  178. let channelId = req.params.channel;
  179. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  180. if (channel) {
  181. session.ControlPTZ(req.params.channel, req.params.value);
  182. result.result = true;
  183. result.message = 'OK';
  184. }
  185. else {
  186. result.result = false;
  187. result.message = 'device not found.';
  188. }
  189. }
  190. else {
  191. result.result = false;
  192. result.message = 'device not online.';
  193. }
  194. res.json(ntvMsg(result));
  195. }
  196. //录像文件查询
  197. async function recordQuery(req, res) {
  198. let result = {};
  199. if (this.sessions.has(req.params.device)) {
  200. let session = this.sessions.get(req.params.device);
  201. //判断当前设备通道里是否存在通道编码
  202. let channelId = req.params.channel;
  203. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  204. if (channel) {
  205. if (req.params.begin < req.params.end) {
  206. //unix时间转换
  207. var beginTime = new Date(req.params.begin * 1000).toJSON();
  208. var endTime = new Date(req.params.end * 1000).toJSON();
  209. result.data = await session.getRecordInfos(req.params.channel, beginTime, endTime);
  210. result.result = true;
  211. result.message = 'OK';
  212. }
  213. else {
  214. result.result = false;
  215. result.message = "beginTime 必须小于 endTime.";
  216. }
  217. }
  218. else {
  219. result.result = false;
  220. result.message = 'device not found.';
  221. }
  222. }
  223. else {
  224. result.result = false;
  225. result.message = 'device not online.';
  226. }
  227. res.json(ntvMsg(result));
  228. }
  229. //关闭流
  230. function closeStream(req, res) {
  231. let body = req.body;
  232. let result = { code: 0, msg: 'success' };
  233. if (body.stream) {
  234. //16位进制转10进制
  235. let ssrc = parseInt(body.stream, 16);
  236. //要补位
  237. ssrc = _prefixInteger(ssrc, 10);
  238. let selectSession = null;
  239. let selectDialog = null;
  240. for (session in this.sessions.values()) {
  241. let dialogs = sessson.dialogs;
  242. for (var key in dialogs) {
  243. let dialog = dialogs[key];
  244. if (dialog.ssrc && dialog.ssrc === ssrc) {
  245. selectSession = session;
  246. selectDialog = dialog;
  247. return;
  248. }
  249. }
  250. }
  251. if (selectDialog != null && selectSession != null) {
  252. if (selectDialog.play) {
  253. switch (selectDialog.play) {
  254. case 'realplay':
  255. {
  256. selectSession.StopRealPlay(selectDialog.channelId, selectDialog.host, selectDialog.port);
  257. }
  258. break;
  259. case 'playback':
  260. {
  261. selectSession.StopPlayBack(selectDialog.channelId, selectDialog.begin, selectDialog.end, selectDialog.host, selectDialog.port)
  262. }
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. res.json(ntvMsg(result));
  269. }
  270. function _prefixInteger(num, m) {
  271. return (Array(m).join(0) + num).slice(-m);
  272. }
  273. module.exports = { getSsrcs: getSsrcs, getCatalog: getCatalog, realplay: realplay, getSessions: getSessions, playback: playback, ptzControl: ptzControl, playControl: playControl, recordQuery: recordQuery, closeStream: closeStream }