index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. const url = process.argv.splice(2);
  9. class CarServer {
  10. constructor() {
  11. this.Peer = null
  12. this.track = null
  13. this.child = null
  14. this.udp = null
  15. this.socket = io(url, { auth: { roomID: "feiCar", name: 'car' } });
  16. this.socket.on('connect', this.connected.bind(this))
  17. this.socket.on('msg', this.onMsg.bind(this))
  18. this.socket.on('leaved', this.onLeved.bind(this))
  19. this.socket.on('joined', this.startVideo.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. PWM.changPWM(data.conctrl)
  43. }
  44. } catch (error) {
  45. console.log('msg error:', error);
  46. }
  47. }
  48. // 用户离开
  49. onLeved() {
  50. if (this.child) this.child.kill()
  51. if (this.udp) this.udp.close()
  52. this.child = null
  53. this.udp = null
  54. process.exit(1)
  55. }
  56. // 开始视频
  57. startVideo() {
  58. try {
  59. if (this.child) this.child.kill()
  60. if (this.udp) this.udp.close()
  61. // test video
  62. const video = new Video('video', 'SendOnly')
  63. video.addH264Codec(96)
  64. video.addSSRC(42, "video-send")
  65. this.track = this.Peer.addTrack(video)
  66. this.Peer.setLocalDescription()
  67. // UDP server
  68. const port = 7788
  69. this.udp = createSocket("udp4")
  70. this.udp.bind(port)
  71. // video push
  72. const args = [
  73. "libcamerasrc",
  74. "video/x-raw,width=640,height=480",
  75. "videoconvert",
  76. "queue",
  77. "x264enc tune=zerolatency bitrate=1000 key-int-max=30",
  78. "video/x-h264, profile=constrained-baseline",
  79. "rtph264pay pt=96 mtu=1200 ssrc=42",
  80. `udpsink host=127.0.0.1 port=${port}`,
  81. ].join(" ! ").split(" ")
  82. this.child = spawn("gst-launch-1.0", args)
  83. // listen UDP
  84. this.udp.on("message", (data) => {
  85. if (!this.track.isOpen()) return
  86. this.track.sendMessageBinary(data)
  87. });
  88. } catch (error) {
  89. console.log('startvideo:', error)
  90. }
  91. }
  92. }
  93. new CarServer(url[0])