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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. const Net = require('net');
  2. const Logger = require('../core/logger');
  3. const NodeRtpSession = require('./session');
  4. const context = require('../core/ctx');
  5. const NodeRtmpClient = require('../core/rtmp_client');
  6. const RtpSession = require("rtp-rtcp").RtpSession;
  7. //GB28181 媒体服务器
  8. class NodeGB28181StreamServer {
  9. constructor(config) {
  10. this.listen = config.GB28181.streamServer.listen || 9200;
  11. this.host = config.GB28181.streamServer.host || '0.0.0.0';
  12. //开启端口复用
  13. if (config.GB28181.streamServer.invite_port_fixed) {
  14. //RTP-RTCP UDP-Server
  15. this.udpServer = new RtpSession(this.listen);
  16. this.udpServer.createRtcpServer();
  17. //TCP-Server
  18. this.tcpServer = Net.createServer((socket) => {
  19. let session = new NodeRtpSession(config, socket);
  20. session.run();
  21. });
  22. }
  23. //主动取流客户端(TCP/主动模式)
  24. this.tcpClients = {};
  25. //默认的RTMP服务器基地址
  26. this.rtmpServer = config.GB28181.streamServer.rtmpServer || 'rtmp://127.0.0.1/live';
  27. }
  28. run() {
  29. if (this.udpServer) {
  30. //TCP
  31. this.tcpServer.listen(this.listen, () => {
  32. Logger.log(`Node Media GB28181-Stream/TCP Server started on port: ${this.listen}`);
  33. });
  34. this.tcpServer.on('error', (e) => {
  35. Logger.error(`Node Media GB28181-Stream/TCP Server ${e}`);
  36. });
  37. this.tcpServer.on('close', () => {
  38. Logger.log('Node Media GB28181-Stream/TCP Server Close.');
  39. });
  40. }
  41. if (this.udpServer) {
  42. //UDP
  43. this.udpServer.on("listening", () => {
  44. Logger.log(`Node Media GB28181-Stream/UDP Server started on port: ${this.listen}`);
  45. });
  46. this.udpServer.on("message", (msg, info) => {
  47. NodeRtpSession.parseRTPacket(msg);
  48. });
  49. }
  50. //SDP收到
  51. context.nodeEvent.on('sdpReceived', this.sdpReceived.bind(this));
  52. //RTP己处理好
  53. context.nodeEvent.on('rtpReadyed', this.rtpReceived.bind(this));
  54. //停止播放,关闭推流客户端
  55. context.nodeEvent.on('stopPlayed', (ssrc) => {
  56. if (context.publishers.has(ssrc)) {
  57. let rtmpClient = context.publishers.get(ssrc);
  58. rtmpClient.stop();
  59. context.publishers.delete(ssrc);
  60. }
  61. });
  62. }
  63. //接收到 INVITE SDP 描述
  64. sdpReceived(sdp) {
  65. //判断流发送者SDP描述,如果是 TCP主动模式 则创建主动取流客户端
  66. if (sdp.media.length > 0 && sdp.media[0].ssrc) {
  67. let ssrc = sdp.media[0].ssrc;
  68. let host = sdp.connection.ip || sdp.origin.address;
  69. let version = sdp.connection.version || 4; //IPV4 or IPV6
  70. let port = sdp.media[0].port;
  71. let protocol = sdp.media[0].protocol;
  72. let mode = 0;
  73. switch (protocol) {
  74. //UDP
  75. case 'RTP/AVP':
  76. {
  77. mode = 0;
  78. }
  79. break;
  80. //TCP
  81. case 'TCP/RTP/AVP':
  82. {
  83. let setup = sdp.media[0].setup;
  84. switch (setup) {
  85. //背动模式,需要创建TCP-Client 去取流
  86. case 'passive':
  87. {
  88. mode = 2;
  89. this.createTCPClient(ssrc, host, port);
  90. }
  91. break;
  92. //主动模式
  93. case 'active':
  94. {
  95. mode = 1;
  96. }
  97. break;
  98. }
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. //创建TCP主动取流客户端
  105. createTCPClient(ssrc, host, port) {
  106. if (!this.tcpClients[ssrc]) {
  107. let tcpClient = new Net.Socket();
  108. this.tcpClients[ssrc] = tcpClient;
  109. tcpClient._cache = Buffer.alloc(0);
  110. tcpClient.connect(port, host, () => { Logger.log("[GB28181_TCP_Active] 连接成功,等待接收 RTP 数据包...") });
  111. tcpClient.on('data', (data) => {
  112. tcpClient._cache = Buffer.concat([tcpClient._cache, data]);
  113. while (tcpClient._cache.length > 1 && tcpClient._cache.length >= (tcpClient._cache.readUInt16BE(0) + 2)) {
  114. let rtplength = tcpClient._cache.readUInt16BE(0);
  115. let rtpData = tcpClient._cache.slice(2, rtplength + 2);
  116. NodeRtpSession.parseRTPacket(rtpData);
  117. tcpClient._cache = tcpClient._cache.slice(rtplength + 2);
  118. }
  119. });
  120. //连接关闭
  121. tcpClient.on('error', (err) => {
  122. Logger.log("[GB28181_TCP_Active] 连接关闭...") ;
  123. tcpClient.destroy();
  124. delete this.tcpClients[ssrc];
  125. });
  126. }
  127. }
  128. //TCPServer/UDPServer 接收到nalus
  129. rtpReceived(ssrc, timestamp, packet) {
  130. if (!context.publishers.has(ssrc)) {
  131. var rtmpClient = new NodeRtmpClient(`${this.rtmpServer}/${ssrc}`);
  132. rtmpClient.startPush();
  133. //RTMP 发布流状态
  134. rtmpClient.on('status', (info) => {
  135. if (info.code === 'NetStream.Publish.Start')
  136. rtmpClient.isPublishStart = true;
  137. });
  138. //连接关闭
  139. rtmpClient.on('close', () => {
  140. context.nodeEvent.emit('rtmpClientClose', ssrc);
  141. });
  142. context.publishers.set(ssrc, rtmpClient);
  143. }
  144. let rtmpClinet = context.publishers.get(ssrc);
  145. //记录收包时间,长时间未收包关闭会话
  146. rtmpClinet._lastReceiveTime = new Date();
  147. //发送视频第一包
  148. if (!rtmpClinet.sendfirstVideoPacket && rtmpClinet.isPublishStart) {
  149. let streaminfo = rtmpClinet._streaminfo;
  150. switch (streaminfo.video) {
  151. case 0x24:
  152. {
  153. let vps = rtmpClinet._vps;
  154. let sps = rtmpClinet._sps;
  155. let pps = rtmpClinet._pps;
  156. if (vps && sps && pps) {
  157. let _packet = Buffer.concat([Buffer.from([0x1C, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x5A, 0xF0, 0x01, 0xFC, 0xFD, 0xF8, 0xF8, 0x00, 0x00, 0x0F, 0x03, 0x20, 0x00, 0x01, vps.length >> 8 & 0xff, vps.length & 0xff]), vps, Buffer.from([0x21, 0x00, 0x01, sps.length >> 8 & 0xff, sps.length & 0xff]), sps, Buffer.from([0x22, 0x00, 0x01, pps.length >> 8 & 0xff, pps.length & 0xff]), pps]);
  158. rtmpClinet.pushVideo(_packet, 0);
  159. rtmpClinet.deltaVideo = 0;
  160. rtmpClinet.sendfirstVideoPacket = true;
  161. }
  162. }
  163. break;
  164. case 0x1b:
  165. {
  166. let sps = rtmpClinet._sps;
  167. let pps = rtmpClinet._pps;
  168. if (sps && pps) {
  169. let _packet = Buffer.concat([Buffer.from([0x17, 0x00, 0x00, 0x00, 0x00, 0x01, sps.readUInt8(1), sps.readUInt8(2), sps.readUInt8(3), 0xff, 0xe1, sps.length >> 8 & 0xff, sps.length & 0xff]), sps, Buffer.from([0x01, pps.length >> 8 & 0xff, pps.length & 0xff]), pps]);
  170. rtmpClinet.pushVideo(_packet, 0);
  171. rtmpClinet.deltaVideo = 0;
  172. rtmpClinet.sendfirstVideoPacket = true;
  173. }
  174. }
  175. break;
  176. }
  177. }
  178. //发送音频第一包
  179. if (!rtmpClinet.sendfirstAudioPacket && rtmpClinet.isPublishStart) {
  180. let streaminfo = rtmpClinet._streaminfo;
  181. switch (streaminfo.audio) {
  182. //AAC
  183. case 0x0f:
  184. {
  185. //ToDo 需要判断音频信息,采样率,采样深度,码率
  186. }
  187. break;
  188. //G711a
  189. case 0x90:
  190. {
  191. var _packet = Buffer.from([0x70]);
  192. rtmpClinet.pushAudio(_packet, 0);
  193. rtmpClinet.deltaAudio = 0;
  194. rtmpClinet.sendfirstAudioPacket = true;
  195. }
  196. break;
  197. //G711u
  198. case 0x91:
  199. {
  200. var _packet = Buffer.from([0x80]);
  201. rtmpClinet.pushAudio(_packet, 0);
  202. rtmpClinet.deltaAudio = 0;
  203. rtmpClinet.sendfirstAudioPacket = true;
  204. }
  205. break;
  206. }
  207. }
  208. //判断packet.streaminfo H264/H265
  209. if (!rtmpClinet._streaminfo && packet.streaminfo)
  210. rtmpClinet._streaminfo = packet.streaminfo;
  211. if (!rtmpClinet._streaminfo.video && packet.streaminfo.video)
  212. rtmpClinet._streaminfo.video = packet.streaminfo.video;
  213. if (!rtmpClinet._streaminfo.audio && packet.streaminfo.audio)
  214. rtmpClinet._streaminfo.audio = packet.streaminfo.audio;
  215. //发送视频
  216. packet.video.forEach(nalu => {
  217. switch (rtmpClinet._streaminfo.video) {
  218. //H265
  219. case 0x24:
  220. {
  221. let naluType = (nalu.readUInt8(0) & 0x7E) >> 1;
  222. switch (naluType) {
  223. case 19:
  224. rtmpClinet._keyframe = nalu;
  225. break;
  226. case 32:
  227. if (!rtmpClinet._vps)
  228. rtmpClinet._vps = nalu;
  229. break;
  230. case 33:
  231. if (!rtmpClinet._sps)
  232. rtmpClinet._sps = nalu;
  233. break;
  234. case 34:
  235. if (!rtmpClinet._pps)
  236. rtmpClinet._pps = nalu;
  237. break;
  238. }
  239. //flv封装
  240. if (naluType !== 32 && naluType !== 33 && naluType !== 34) {
  241. let packet = Buffer.concat([Buffer.from([naluType == 19 ? 0x1C : 0x2C, 0x01, 0x00, 0x00, 0x00, (nalu.length >> 24 & 0xff), (nalu.length >> 16 & 0xff), (nalu.length >> 8 & 0xff), (nalu.length & 0xff)]), nalu]);
  242. rtmpClinet.deltaVideo += timestamp / 90;
  243. if (rtmpClinet.isPublishStart && rtmpClinet.sendfirstVideoPacket)
  244. rtmpClinet.pushVideo(packet, rtmpClinet.deltaVideo);
  245. }
  246. }
  247. break;
  248. //H264
  249. case 0x1b:
  250. {
  251. let naluType = nalu.readUInt8(0) & 0x1F;
  252. switch (naluType) {
  253. case 5:
  254. rtmpClinet._keyframe = nalu;
  255. break;
  256. case 7:
  257. if (!rtmpClinet._sps)
  258. rtmpClinet._sps = nalu;
  259. break;
  260. case 8:
  261. if (!rtmpClinet._pps)
  262. rtmpClinet._pps = nalu;
  263. break;
  264. }
  265. //flv封装
  266. if (naluType !== 7 && naluType !== 8) {
  267. let packet = Buffer.concat([Buffer.from([naluType == 5 ? 0x17 : 0x27, 0x01, 0x00, 0x00, 0x00, (nalu.length >> 24 & 0xff), (nalu.length >> 16 & 0xff), (nalu.length >> 8 & 0xff), (nalu.length & 0xff)]), nalu]);
  268. rtmpClinet.deltaVideo += timestamp / 90;
  269. if (rtmpClinet.isPublishStart && rtmpClinet.sendfirstVideoPacket)
  270. rtmpClinet.pushVideo(packet, rtmpClinet.deltaVideo);
  271. }
  272. }
  273. break;
  274. //SVAC
  275. case 0x80:
  276. break;
  277. }
  278. });
  279. //发送音频
  280. if (packet.audio.length > 0) {
  281. if (rtmpClinet.isPublishStart && rtmpClinet.sendfirstAudioPacket) {
  282. switch (rtmpClinet._streaminfo.audio) {
  283. //G711a
  284. case 0x90:
  285. {
  286. rtmpClinet.deltaAudio += (packet.audio.length / 8000) * 1000;
  287. rtmpClinet.pushAudio(Buffer.concat([Buffer.from([0x70]), packet.audio]), rtmpClinet.deltaAudio);
  288. }
  289. break;
  290. //G711u
  291. case 0x91:
  292. {
  293. rtmpClinet.deltaAudio += (packet.audio.length / 8000) * 1000;
  294. rtmpClinet.pushAudio(Buffer.concat([Buffer.from([0x80]), packet.audio]), rtmpClinet.deltaAudio);
  295. }
  296. break;
  297. }
  298. }
  299. }
  300. }
  301. stop() {
  302. if (this.tcpServer)
  303. this.tcpServer.close();
  304. if (this.udpServer)
  305. this.udpServer.close();
  306. context.sessions.forEach((session, id) => {
  307. if (session instanceof NodeRtpSession) {
  308. session.socket.destroy();
  309. context.sessions.delete(id);
  310. }
  311. });
  312. }
  313. }
  314. module.exports = NodeGB28181StreamServer