Browse Source

移除音频
Signed-off-by: caner <5658514@qq.com>

caner 3 years ago
parent
commit
02a126600d
3 changed files with 5 additions and 86 deletions
  1. 3 13
      index.js
  2. 2 2
      lib/Pwm.js
  3. 0 71
      lib/audioSource.js

+ 3 - 13
index.js

@@ -2,7 +2,6 @@
 // wrt + socket + contrl
 const io = require("socket.io-client")
 const RTCVideoSource = require('./lib/videoSource')
-const RTCAudioSource = require('./lib/audioSource')
 //const PWM = require('./lib/Pwm')
 const { RTCPeerConnection, MediaStream } = require("wrtc")
 const HOST = 'ws://10.10.3.196:7896'
@@ -10,8 +9,7 @@ class CarServer {
     constructor(HOST) {
         this.webRTC = null
         this.mediaStream = null
-        this.videoSource = null
-        this.audioSource = null        
+        this.videoSource = null     
         this.socket = io(HOST, {
             auth: {
                 roomID: "feiCar",
@@ -48,18 +46,12 @@ class CarServer {
         // 初始化媒体源
         this.mediaStream = new MediaStream()
         this.videoSource = new RTCVideoSource()
-        this.audioSource = new RTCAudioSource()
         // 加入媒体源
         const videoTrack = this.videoSource.createTrack()
         this.mediaStream.addTrack(videoTrack)
         this.webRTC.addTrack(videoTrack, this.mediaStream)
-        // 加入音频源
-        const audioTrack = this.audioSource.createTrack()
-        this.mediaStream.addTrack(audioTrack)
-        this.webRTC.addTrack(audioTrack, this.mediaStream)
-        // 加入媒体
+        // 开始媒体
         this.videoSource.start()
-        this.audioSource.start()
         // 监听ice
         this.webRTC.onicecandidate = event => {
             if (event.candidate) {
@@ -108,12 +100,10 @@ class CarServer {
 
     destroyed() {
         if (this.webRTC) this.webRTC.close()
-        if (this.audioSource) this.audioSource.stop()
         if (this.videoSource) this.videoSource.stop()
         this.webRTC = null
         this.mediaStream = null
-        this.videoSource = null
-        this.audioSource = null       
+        this.videoSource = null     
         process.exit(1) 
     }    
 }

+ 2 - 2
lib/Pwm.js

@@ -21,8 +21,8 @@ class ContrlService {
         // prio init
         RPIO.init({ mapping: "gpio" });
         // 继电器解锁
-        RPIO.open(22, RPIO.OUTPUT, RPIO.LOW);
-        RPIO.open(23, RPIO.OUTPUT, RPIO.LOW);
+        RPIO.open(22, RPIO.OUTPUT, RPIO.HIGH);
+        RPIO.open(23, RPIO.OUTPUT, RPIO.HIGH);
         // 开灯
         RPIO.open(22, RPIO.OUTPUT, RPIO.HIGH);
         // init PWM

+ 0 - 71
lib/audioSource.js

@@ -1,71 +0,0 @@
-const { RTCAudioSource } = require('wrtc').nonstandard;
-const ffmpeg = require('fluent-ffmpeg')
-
-class AudioSourceService extends RTCAudioSource {
-    constructor() {
-        super();
-        this.command = null // 存储音频源
-        this.cache = Buffer.alloc(0)
-    }
-
-    // 创建通道
-    createTrack() {
-        const track = super.createTrack()
-        return track
-    }
-
-
-    // 获取音频源
-    start() {
-        if (this.command !== null) this.stop()
-        this.command = ffmpeg("plughw:1,0")
-            .inputFormat("alsa")
-            .audioChannels(1)
-            .audioFrequency(48000)
-            .audioCodec("pcm_s16le")
-            .outputFormat("s16le")
-            .on("start", () => {
-                console.log("Audio start !");
-            })
-            .on("error", function (err) {
-                console.log('Audio processing an error occurred: ' + err.message);
-                // 退出进程
-                process.exit(1)
-            })
-        this.ffstream = this.command.pipe();
-        this.ffstream.on("data", (buffer) => {
-            // console.log("Audio buffer length", buffer.length);
-            this.cache = Buffer.concat([this.cache, buffer]);
-        });
-
-        const processData = () => {
-            while (this.cache.length > 960) {
-                const buffer = this.cache.slice(0, 960);
-                this.cache = this.cache.slice(960);
-                const samples = new Int16Array(new Uint8Array(buffer).buffer);
-                this.onData({
-                    bitsPerSample: 16,
-                    sampleRate: 48000,
-                    channelCount: 1,
-                    numberOfFrames: samples.length,
-                    type: "data",
-                    samples,
-                });
-            }
-            if (this.command !== null) {
-                setTimeout(() => processData(), 10);
-            }
-        };
-        processData();
-    }
-
-    // 停止获取
-    stop() {
-        console.log("audio source stop");
-        if (this.command != null) {
-            this.command.kill("SIGHUP")
-            this.command = null
-        }
-    }
-}
-module.exports = AudioSourceService