index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const io = require("socket.io-client")
  2. const { PeerConnection, Video } = require('node-datachannel');
  3. const { createSocket } = require('dgram')
  4. const { spawn } = require('child_process')
  5. const HOST = '' //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. // test video
  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. } catch (error) {
  90. console.log('startvideo:', error)
  91. }
  92. }
  93. }
  94. new CarServer()