index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const io = require("socket.io-client")
  2. const { PeerConnection, Video,Audio } = require('node-datachannel');
  3. const { spawn } = require('child_process')
  4. const UDP = require('./udp.js')
  5. const HOST = 'wss://car.caner.top' //Your signaling server address
  6. class CarServer {
  7. constructor() {
  8. this.Peer = null
  9. this.track = null
  10. this.child = null
  11. this.udp = null
  12. if (!HOST) console.error('Enter the signaling server address');
  13. this.socket = io(HOST, {
  14. auth: {
  15. roomID: 'test',
  16. name: '456'
  17. }
  18. });
  19. this.socket.on('connect', this.connected.bind(this))
  20. this.socket.on('msg', this.onMsg.bind(this))
  21. this.socket.on('leaved', this.onLeved.bind(this))
  22. this.socket.on('joined', this.startVideo.bind(this))
  23. }
  24. // socket conect
  25. connected() {
  26. console.log('connected');
  27. this.Peer = new PeerConnection("Peer1", { iceServers: [] });
  28. // send offer
  29. this.Peer.onGatheringStateChange(state => {
  30. console.log('GatheringState: ', state);
  31. if (state === 'complete') {
  32. const offer = this.Peer.localDescription();
  33. this.socket.emit("msg", offer);
  34. }
  35. })
  36. }
  37. // listen data
  38. onMsg(data) {
  39. try {
  40. if (data.type == 'answer') {
  41. this.Peer.setRemoteDescription(data.sdp, data.type);
  42. } else if (data.type === 'startRTC') {
  43. this.startVideo()
  44. }
  45. } catch (error) {
  46. console.log('msg error:', error);
  47. }
  48. }
  49. // listen leave
  50. onLeved() {
  51. if (this.child) this.child.kill()
  52. if (this.udp) this.udp.close()
  53. this.child = null
  54. this.udp = null
  55. process.exit(1)
  56. }
  57. // start video
  58. startVideo() {
  59. try {
  60. if (this.child) this.child.kill()
  61. if (this.udp) this.udp.close()
  62. // // ------------bullseys 64位系统----------
  63. // const video = new Video('video', 'SendOnly')
  64. // video.addH264Codec(96)
  65. // video.addSSRC(42, "video-send")
  66. // this.track = this.Peer.addTrack(video)
  67. // this.Peer.setLocalDescription()
  68. // // UDP server
  69. // const port = 7788
  70. // this.udp = createSocket("udp4")
  71. // this.udp.bind(port)
  72. // // video push
  73. // const args = [
  74. // "libcamerasrc",
  75. // "video/x-raw,width=320,height=240",
  76. // "videoconvert",
  77. // "queue",
  78. // "x264enc tune=zerolatency bitrate=1000 key-int-max=30",
  79. // "video/x-h264, profile=constrained-baseline",
  80. // "rtph264pay pt=96 mtu=1200 ssrc=42",
  81. // `udpsink host=127.0.0.1 port=${port}`,
  82. // ].join(" ! ").split(" ")
  83. // this.child = spawn("gst-launch-1.0", args)
  84. // // listen UDP
  85. // this.udp.on("message", (data) => {
  86. // if (!this.track.isOpen()) return
  87. // this.track.sendMessageBinary(data)
  88. // });
  89. // ---------------buster 32位系统------------------
  90. const video = new Video('video', 'SendOnly')
  91. video.addH264Codec(97)
  92. video.addSSRC(43, "video-send")
  93. const videoTrack = this.Peer.addTrack(video)
  94. const videoPort = 47788
  95. const videoArgs = [
  96. "v4l2src device=/dev/video0",
  97. "video/x-raw,width=960,height=720",
  98. "videoconvert",
  99. "queue",
  100. "omxh264enc",
  101. "video/x-h264,profile=baseline",
  102. "rtph264pay pt=97 mtu=1200 ssrc=43",
  103. `udpsink host=127.0.0.1 port=${videoPort}`,
  104. ].join(" ! ").split(" ")
  105. spawn("gst-launch-1.0", videoArgs)
  106. new UDP(videoPort, data => {
  107. if (!videoTrack.isOpen()) return
  108. videoTrack.sendMessageBinary(data)
  109. })
  110. // -----------audio--------------
  111. const audio = new Audio('audio', 'SendOnly')
  112. audio.addOpusCodec(96)
  113. audio.addSSRC(42, "audio-send")
  114. const audioTrack = this.Peer.addTrack(audio)
  115. const audioPort = 47789
  116. const audioArgs = [
  117. 'alsasrc device=plughw:1,0',
  118. "audio/x-raw,rate=8000,channels=1",
  119. "audioconvert",
  120. "queue",
  121. "opusenc",
  122. "rtpopuspay",
  123. `udpsink host=127.0.0.1 port=${audioPort}`,
  124. ].join(" ! ").split(" ")
  125. spawn("gst-launch-1.0", audioArgs)
  126. new UDP(audioPort, data => {
  127. if (!audioTrack.isOpen()) return
  128. audioTrack.sendMessageBinary(data)
  129. })
  130. this.Peer.setLocalDescription()
  131. } catch (error) {
  132. console.log('startvideo:', error)
  133. }
  134. }
  135. }
  136. new CarServer()