index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // wrt + socket + contrl
  2. const io = require("socket.io-client")
  3. const PWM = require('./lib/Pwm')
  4. const { PeerConnection, Video } = require('node-datachannel');
  5. const { createSocket } = require('dgram')
  6. const { spawn } = require('child_process')
  7. const url = 'ws://10.10.3.196:7896'
  8. class CarServer {
  9. constructor() {
  10. this.Peer = null
  11. this.track = null
  12. this.child = null
  13. this.udp = null
  14. this.socket = io(url, { auth: { roomID: "feiCar", name: 'car' } });
  15. this.socket.on('connect', this.connected.bind(this))
  16. this.socket.on('msg', this.onMsg.bind(this))
  17. this.socket.on('leaved', this.onLeved.bind(this))
  18. this.socket.on('joined', this.startVideo.bind(this))
  19. this.socket.on('disconnect',this.onLeved.bind(this))
  20. }
  21. // socket 连接
  22. connected() {
  23. console.log('connected');
  24. this.Peer = new PeerConnection("Peer1", { iceServers: [] });
  25. // 发送offer
  26. this.Peer.onGatheringStateChange(state => {
  27. console.log('GatheringState: ', state);
  28. if (state === 'complete') {
  29. const offer = this.Peer.localDescription();
  30. this.socket.emit("msg", offer);
  31. }
  32. })
  33. }
  34. // 用户信息
  35. onMsg(data) {
  36. try {
  37. if (data.type == 'answer') {
  38. this.Peer.setRemoteDescription(data.sdp, data.type);
  39. } else if (data.type === 'startRTC') {
  40. this.startVideo()
  41. } if (data.type === 'conctrl') {
  42. const state = this.Peer.state();
  43. if(state === 'connected') PWM.changPWM(data.conctrl);
  44. }
  45. } catch (error) {
  46. console.log(error);
  47. throw error
  48. }
  49. }
  50. // 用户离开
  51. onLeved() {
  52. if (this.child) this.child.kill()
  53. if (this.udp) this.udp.close()
  54. PWM.stop()
  55. this.child = null
  56. this.udp = null
  57. process.exit(1)
  58. }
  59. // 开始视频
  60. startVideo() {
  61. try {
  62. if (this.child) this.child.kill()
  63. if (this.udp) this.udp.close()
  64. // test video
  65. const video = new Video('video', 'SendOnly')
  66. video.addH264Codec(96)
  67. video.addSSRC(42, "video-send")
  68. this.track = this.Peer.addTrack(video)
  69. this.Peer.setLocalDescription()
  70. // UDP server
  71. const port = 7788
  72. this.udp = createSocket("udp4")
  73. this.udp.bind(port)
  74. // video push
  75. const args = [
  76. "libcamerasrc",
  77. "video/x-raw,width=960,height=720",
  78. "videoconvert",
  79. "queue",
  80. "x264enc tune=zerolatency bitrate=1000 key-int-max=30 speed-preset=1",
  81. "video/x-h264, profile=constrained-baseline",
  82. "rtph264pay pt=96 mtu=1200 ssrc=42",
  83. `udpsink host=127.0.0.1 port=${port}`,
  84. ].join(" ! ").split(" ")
  85. this.child = spawn("gst-launch-1.0", args)
  86. // listen UDP
  87. this.udp.on("message", (data) => {
  88. if (!this.track.isOpen()) return
  89. this.track.sendMessageBinary(data)
  90. });
  91. } catch (error) {
  92. console.log(error);
  93. throw error
  94. }
  95. }
  96. }
  97. new CarServer()