Ei kuvausta
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.

session.js 9.5KB

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