Нема описа
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. const context = require('../core/ctx');
  2. const Logger = require('../core/logger');
  3. const RtpPacket = require("rtp-rtcp").RtpPacket;
  4. class NodeGB28181StreamServerSession {
  5. constructor(config, socket) {
  6. this.config = config;
  7. this.socket = socket;
  8. this.id = this.generateNewSessionID();
  9. this.ip = socket.remoteAddress;
  10. this.TAG = 'GB28181_TCP_Passive';
  11. context.sessions.set(this.id, this);
  12. }
  13. run() {
  14. this.socket.on('data', this.onSocketData.bind(this));
  15. this.socket.on('close', this.onSocketClose.bind(this));
  16. this.socket.on('error', this.onSocketError.bind(this));
  17. this.socket.on('timeout', this.onSocketTimeout.bind(this));
  18. this.isStarting = true;
  19. this.connectTime = new Date();
  20. Logger.log(`[${this.TAG} connect] id=${this.id} ip=${this.ip} `);
  21. this.cache = Buffer.alloc(0);
  22. if (!this.isStarting) {
  23. this.stop();
  24. return;
  25. }
  26. }
  27. stop() {
  28. if (this.isStarting) {
  29. this.isStarting = false;
  30. this.socket.end();
  31. Logger.log(`[${this.TAG} disconnect] id=${this.id}`);
  32. context.sessions.delete(this.id);
  33. }
  34. }
  35. onSocketClose() {
  36. this.stop();
  37. }
  38. onSocketError(e) {
  39. this.stop();
  40. }
  41. onSocketTimeout() {
  42. this.stop();
  43. }
  44. //接收TCP 包
  45. onSocketData(data) {
  46. //国标28181的tcp码流标准遵循的是RFC4571标准
  47. //RFC2326标准格式: $+长度+RTP头+数据
  48. //RFC4571标准格式: 长度+RTP头+数据
  49. this.cache = Buffer.concat([this.cache, data]);
  50. while (this.cache.length > 1 && this.cache.length >= (this.cache.readUInt16BE(0) + 2)) {
  51. let rtplength = this.cache.readUInt16BE(0);
  52. let rtpData = this.cache.slice(2, rtplength + 2);
  53. //ntv add 传输this
  54. NodeGB28181StreamServerSession.parseRTPacket(rtpData,this);
  55. this.cache = this.cache.slice(rtplength + 2);
  56. }
  57. //NodeGB28181StreamServerSession.parseTCPRTPacket(this.id, data);
  58. }
  59. //
  60. generateNewSessionID() {
  61. let sessionID = '';
  62. const possible = 'ABCDEFGHIJKLMNOPQRSTUVWKYZ0123456789';
  63. const numPossible = possible.length;
  64. do {
  65. for (let i = 0; i < 8; i++) {
  66. sessionID += possible.charAt((Math.random() * numPossible) | 0);
  67. }
  68. } while (context.sessions.has(sessionID))
  69. return sessionID;
  70. }
  71. //补位0
  72. static PrefixInteger(num, m) {
  73. return (Array(m).join(0) + num).slice(-m);
  74. }
  75. //处理UDP/RTP包
  76. static parseRTPacket(cache,that) {
  77. let rtpPacket = new RtpPacket(cache);
  78. let ssrc = rtpPacket.getSSRC();
  79. let seqNumber = rtpPacket.getSeqNumber();
  80. let playloadType = rtpPacket.getPayloadType();
  81. let timestamp = rtpPacket.getTimestamp();
  82. let playload = rtpPacket.getPayload();
  83. if (!this.rtpPackets)
  84. this.rtpPackets = new Map();
  85. if (!this.rtpPackets.has(ssrc))
  86. this.rtpPackets.set(ssrc, new Map());
  87. let session = this.rtpPackets.get(ssrc);
  88. //ntv-wangjian
  89. //Logger.log(`[${ssrc}] RTP Packet: timestamp:${timestamp} seqNumber:${seqNumber} length:${playload.length} `);
  90. switch (playloadType) {
  91. //PS封装
  92. case 96:
  93. {
  94. if (!session.has(timestamp)) {
  95. session.set(timestamp, playload);
  96. }
  97. else {
  98. session.set(timestamp, Buffer.concat([session.get(timestamp), playload]));
  99. }
  100. //等待下一帧 收到,处理上一帧
  101. if (session.size > 1) {
  102. let entries = session.entries();
  103. let first = entries.next().value;
  104. let second = entries.next().value;
  105. session.delete(first[0]);
  106. try {
  107. let packet = this.parseMpegPSPacket(first[1]);
  108. context.nodeEvent.emit('rtpReadyed', this.PrefixInteger(ssrc, 10), second[0] - first[0], packet);
  109. }
  110. catch (error) {
  111. Logger.log(`PS Packet Parse Fail! ${error}`);
  112. }
  113. }
  114. }
  115. break;
  116. }
  117. }
  118. //解析 PS 获取Nalus video/audio/streaminfo
  119. static parseMpegPSPacket(buf, offset) {
  120. let position = offset || 0;
  121. //PSM 编码信息
  122. let streaminfo = {};
  123. //PES-video-payload-nalus
  124. let naluscache = Buffer.alloc(0);
  125. //PES-audio-payload
  126. let audiocache = Buffer.alloc(0);
  127. //读取PES
  128. while (buf.length - 6 > position) {
  129. let Identifier = buf.readUInt32BE(position);
  130. position += 4;
  131. if (Identifier == 0x01ba) {
  132. //系统时钟基准(6)+PS复用速率(4)
  133. position += 9;
  134. //填充头长度
  135. let pack_stuffing_length = (buf.readUInt8(position) & 0x07);
  136. position += 1;
  137. position += pack_stuffing_length;
  138. if (position > buf.length)
  139. break;
  140. }
  141. //System Header 0xbb
  142. if (Identifier == 0x01bb) {
  143. //系统标题头长度
  144. let header_length = (buf.readUInt8(position) << 8 | buf.readUInt8(position + 1));
  145. position += 2;
  146. position += header_length;
  147. if (position > buf.length)
  148. break;
  149. }
  150. //PSM 0xbc 解包判断音/视频编码 类型
  151. if (Identifier == 0x01bc) {
  152. //PES-length
  153. let pes_packet_length = (buf.readUInt8(position) << 8 | buf.readUInt8(position + 1));
  154. position += 2;
  155. let program_stream_info_length = buf.readUInt16BE(position + 2);
  156. let elementary_stream_map_length = buf.readUInt16BE(position + 4);
  157. let start = 6 + program_stream_info_length;
  158. let end = 6 + program_stream_info_length + elementary_stream_map_length;
  159. while (start < end) {
  160. let stream_type = buf.readUInt8(position + start++);
  161. let elementary_stream_id = buf.readUInt8(position + start++);
  162. let elmentary_stream_info_length = buf.readUInt8(position + start++) << 8 | buf.readUInt8(position + start++);
  163. if (elementary_stream_id == 0xc0)
  164. streaminfo.audio = stream_type;
  165. if (elementary_stream_id == 0xe0)
  166. streaminfo.video = stream_type;
  167. start += elmentary_stream_info_length;
  168. }
  169. position += pes_packet_length;
  170. if (position > buf.length)
  171. break;
  172. }
  173. if (Identifier >= 0x01e0 && Identifier <= 0x01ef) {
  174. //PES-length
  175. let pes_packet_length = (buf.readUInt8(position) << 8 | buf.readUInt8(position + 1));
  176. position += 2;
  177. //PES packet header
  178. let pes_header_length = buf.readUInt8(position + 2) + 3;
  179. //视频数据
  180. let data = buf.slice(position + pes_header_length, position + pes_packet_length);
  181. naluscache = Buffer.concat([naluscache, data]);
  182. position += pes_packet_length;
  183. if (position > buf.length)
  184. break;
  185. }
  186. if (Identifier >= 0x01c0 && Identifier <= 0x01df) {
  187. //PES-length
  188. let pes_packet_length = (buf.readUInt8(position) << 8 | buf.readUInt8(position + 1));
  189. position += 2;
  190. //PES packet header
  191. let pes_header_length = buf.readUInt8(position + 2) + 3;
  192. //音频数据
  193. let data = buf.slice(position + pes_header_length, position + pes_packet_length);
  194. audiocache = Buffer.concat([audiocache, data]);
  195. position += pes_packet_length;
  196. if (position > buf.length)
  197. break;
  198. }
  199. }
  200. //读取完毕分析nalus
  201. position = 0;
  202. let indexs = [];
  203. //视频Nalues
  204. let nalus = [];
  205. while (naluscache.length - 4 > position) {
  206. if (naluscache.readUInt32BE(position) == 1) {
  207. indexs.push(position);
  208. position += 4;
  209. if (indexs.length > 1) {
  210. let nalu = naluscache.slice(indexs[indexs.length - 2] + 4, indexs[indexs.length - 1]);
  211. nalus.push(nalu);
  212. }
  213. }
  214. position++;
  215. }
  216. if (indexs.length > 0) {
  217. let nalu = naluscache.slice(indexs[indexs.length - 1] + 4);
  218. nalus.push(nalu);
  219. }
  220. return { video: nalus, audio: audiocache, streaminfo: streaminfo };
  221. }
  222. }
  223. module.exports = NodeGB28181StreamServerSession;