| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- const io = require("socket.io-client")
- const { PeerConnection, Video,Audio } = require('node-datachannel');
- const { spawn } = require('child_process')
- const UDP = require('./udp.js')
- const HOST = 'wss://car.caner.top' //Your signaling server address
- class CarServer {
- constructor() {
- this.Peer = null
- this.track = null
- this.child = null
- this.udp = null
- if (!HOST) console.error('Enter the signaling server address');
- this.socket = io(HOST, {
- auth: {
- roomID: 'test',
- name: '456'
- }
- });
- 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 conect
- connected() {
- console.log('connected');
- this.Peer = new PeerConnection("Peer1", { iceServers: [] });
- // send offer
- this.Peer.onGatheringStateChange(state => {
- console.log('GatheringState: ', state);
- if (state === 'complete') {
- const offer = this.Peer.localDescription();
- this.socket.emit("msg", offer);
- }
- })
- }
- // listen data
- onMsg(data) {
- try {
- if (data.type == 'answer') {
- this.Peer.setRemoteDescription(data.sdp, data.type);
- } else if (data.type === 'startRTC') {
- this.startVideo()
- }
- } catch (error) {
- console.log('msg error:', error);
- }
- }
- // listen leave
- onLeved() {
- if (this.child) this.child.kill()
- if (this.udp) this.udp.close()
- this.child = null
- this.udp = null
- process.exit(1)
- }
- // start video
- startVideo() {
- try {
- if (this.child) this.child.kill()
- if (this.udp) this.udp.close()
- // // ------------bullseys 64位系统----------
- // 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=320,height=240",
- // "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)
- // });
- // ---------------buster 32位系统------------------
- const video = new Video('video', 'SendOnly')
- video.addH264Codec(97)
- video.addSSRC(43, "video-send")
- const videoTrack = this.Peer.addTrack(video)
- const videoPort = 47788
- const videoArgs = [
- "v4l2src device=/dev/video0",
- "video/x-raw,width=960,height=720",
- "videoconvert",
- "queue",
- "omxh264enc",
- "video/x-h264,profile=baseline",
- "rtph264pay pt=97 mtu=1200 ssrc=43",
- `udpsink host=127.0.0.1 port=${videoPort}`,
- ].join(" ! ").split(" ")
- spawn("gst-launch-1.0", videoArgs)
- new UDP(videoPort, data => {
- if (!videoTrack.isOpen()) return
- videoTrack.sendMessageBinary(data)
- })
- // -----------audio--------------
- const audio = new Audio('audio', 'SendOnly')
- audio.addOpusCodec(96)
- audio.addSSRC(42, "audio-send")
- const audioTrack = this.Peer.addTrack(audio)
- const audioPort = 47789
- const audioArgs = [
- 'alsasrc device=plughw:1,0',
- "audio/x-raw,rate=8000,channels=1",
- "audioconvert",
- "queue",
- "opusenc",
- "rtpopuspay",
- `udpsink host=127.0.0.1 port=${audioPort}`,
- ].join(" ! ").split(" ")
- spawn("gst-launch-1.0", audioArgs)
- new UDP(audioPort, data => {
- if (!audioTrack.isOpen()) return
- audioTrack.sendMessageBinary(data)
- })
- this.Peer.setLocalDescription()
- } catch (error) {
- console.log('startvideo:', error)
- }
- }
- }
- new CarServer()
|