Browse Source

更新 'media-send/peer-send/index.js'

Caner 3 years ago
parent
commit
2f6c6344b8
1 changed files with 74 additions and 28 deletions
  1. 74 28
      media-send/peer-send/index.js

+ 74 - 28
media-send/peer-send/index.js

@@ -1,8 +1,8 @@
 const io = require("socket.io-client")
-const { PeerConnection, Video } = require('node-datachannel');
-const { createSocket } = require('dgram')
+const { PeerConnection, Video,Audio } = require('node-datachannel');
 const { spawn } = require('child_process')
-const HOST = '' //Your signaling server address
+const UDP = require('./udp.js')
+const HOST = 'wss://car.caner.top' //Your signaling server address
 class CarServer {
     constructor() {
         this.Peer = null
@@ -63,36 +63,81 @@ class CarServer {
             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()
+            // // ------------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)
+            // });
+
 
-            // 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",
+            // ---------------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",
-                "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}`,
+                "omxh264enc",
+                "video/x-h264,profile=baseline",
+                "rtph264pay pt=97 mtu=1200 ssrc=43",
+                `udpsink host=127.0.0.1 port=${videoPort}`,
             ].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)
-            });
+            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)
@@ -101,3 +146,4 @@ class CarServer {
 
 }
 new CarServer()
+