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

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