| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- // wrt + socket + contrl
- const io = require("socket.io-client")
- const PWM = require('./lib/Pwm')
- const { PeerConnection, Video } = require('node-datachannel');
- const { createSocket } = require('dgram')
- const { spawn } = require('child_process')
- // const url = 'ws://10.10.3.196:7896'
- const url = process.argv.splice(2);
- class CarServer {
- constructor() {
- this.Peer = null
- this.track = null
- this.child = null
- this.udp = null
- this.socket = io(url, { auth: { roomID: "feiCar", name: 'car' } });
- this.socket.on('connect', this.connected.bind(this))
- this.socket.on('msg', this.onMsg.bind(this))
- this.socket.on('leaved', this.onLeved.bind(this))
- this.socket.on('joined', this.startVideo.bind(this))
- }
- // socket 连接
- connected() {
- console.log('connected');
- this.Peer = new PeerConnection("Peer1", { iceServers: [] });
- // 发送offer
- this.Peer.onGatheringStateChange(state => {
- console.log('GatheringState: ', state);
- if (state === 'complete') {
- const offer = this.Peer.localDescription();
- this.socket.emit("msg", offer);
- }
- })
- }
- // 用户信息
- onMsg(data) {
- try {
- if (data.type == 'answer') {
- this.Peer.setRemoteDescription(data.sdp, data.type);
- } else if (data.type === 'startRTC') {
- this.startVideo()
- } if (data.type === 'conctrl') {
- PWM.changPWM(data.conctrl)
- }
- } catch (error) {
- console.log('msg error:', error);
- }
- }
- // 用户离开
- onLeved() {
- if (this.child) this.child.kill()
- if (this.udp) this.udp.close()
- this.child = null
- this.udp = null
- process.exit(1)
- }
- // 开始视频
- startVideo() {
- try {
- if (this.child) this.child.kill()
- if (this.udp) this.udp.close()
- // test video
- const video = new Video('video', 'SendOnly')
- video.addH264Codec(96)
- video.addSSRC(42, "video-send")
- this.track = this.Peer.addTrack(video)
- this.Peer.setLocalDescription()
- // UDP server
- const port = 7788
- this.udp = createSocket("udp4")
- this.udp.bind(port)
- // video push
- const args = [
- "libcamerasrc",
- "video/x-raw,width=640,height=480",
- "videoconvert",
- "queue",
- "x264enc tune=zerolatency bitrate=1000 key-int-max=30",
- "video/x-h264, profile=constrained-baseline",
- "rtph264pay pt=96 mtu=1200 ssrc=42",
- `udpsink host=127.0.0.1 port=${port}`,
- ].join(" ! ").split(" ")
- this.child = spawn("gst-launch-1.0", args)
- // listen UDP
- this.udp.on("message", (data) => {
- if (!this.track.isOpen()) return
- this.track.sendMessageBinary(data)
- });
- } catch (error) {
- console.log('startvideo:', error)
- }
- }
- }
- new CarServer(url[0])
|