node_ipc_session.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // Created by Mingliang Chen on 18/6/19.
  3. // illuspas[a]gmail.com
  4. // Copyright (c) 2018 Nodemedia. All rights reserved.
  5. //
  6. const Logger = require('./node_core_logger');
  7. const NodeRtmpClient = require('./node_rtmp_client');
  8. class NodeIpcSession {
  9. constructor(streamPath, pullPort, pushPort) {
  10. this.streamPath = streamPath;
  11. this.app = streamPath.split('/')[1];
  12. this.stream = streamPath.split('/')[2];
  13. this.pullPort = pullPort;
  14. this.pullRtmp = new NodeRtmpClient(`rtmp://127.0.0.1:${pullPort}${streamPath}`);
  15. this.pushPort = pushPort;
  16. this.pushRtmp = new NodeRtmpClient(`rtmp://127.0.0.1:${pushPort}${streamPath}`);
  17. this.isStart = false;
  18. Logger.debug(`[rtmp ipc] Create new ipc stream ${streamPath} from port=${pullPort} to port=${pushPort}`);
  19. }
  20. run() {
  21. this.isStart = true;
  22. this.pushRtmp.on('status', (info) => {
  23. if (info.code === 'NetStream.Publish.Start') {
  24. this.pullRtmp.startPull();
  25. }
  26. });
  27. this.pullRtmp.on('audio', (audioData, timestamp) => {
  28. this.pushRtmp.pushAudio(audioData, timestamp);
  29. });
  30. this.pullRtmp.on('video', (videoData, timestamp) => {
  31. this.pushRtmp.pushVideo(videoData, timestamp);
  32. });
  33. this.pullRtmp.on('script', (scriptData, timestamp) => {
  34. this.pushRtmp.pushScript(scriptData, timestamp);
  35. });
  36. this.pushRtmp.startPush();
  37. }
  38. stop() {
  39. this.isStart = false;
  40. this.pullRtmp.stop();
  41. this.pushRtmp.stop();
  42. Logger.debug(`[rtmp ipc] Stop ipc stream ${this.streamPath}`);
  43. }
  44. }
  45. module.exports = NodeIpcSession