天天看點

LiveGBS GB28181監控平台中語音對講 浏覽器采集音頻發送給攝像頭的前端實作

背景分析

近年來,國内視訊監控應用發展迅猛,系統接入規模不斷擴大,湧現了大量平台提供商,平台提供商的接入協定各不相同,終端制造商需要給每款終端維護提供各種不同平台的軟體版本,造成了極大的資源浪費。各地視訊大規模建設後,省級、國家級集中調閱,對重特大事件通過視訊掌握現場并進行指揮排程的需求逐漸湧現,然而不同平台間缺乏統一的互通協定。在這樣的産業背景下,基于終端标準化、平台互聯互通的需求,GB/T28181應運而生。

如下浏覽器采集音頻源碼摘自LiveGBS GB28181流媒體平台的前端web源碼:https://github.com/livegbs/GB28181-Server

前端頁面語音采集示例

function  talkStart(e) {
      if(this.recorder) {
        return;
      }
      var $target = $(e.currentTarget);
      LiveRecorder.get((rec, err) => {
        if(err) {
          alert(err);
          return
        }
        this.muted_bak = this.muted;
        this.$refs["player"].setMuted(true);
        $target.addClass("active");
        this.recorder = rec;
        this.recorder.start();
      }, {
        sampleBits: 16,
        sampleRate: 8000,
        pcmCallback: pcm => {
          if(this.bAudioSendError) return;
          var reader = new window.FileReader();
          reader.onloadend = () => {
            var base64 = reader.result;
            var base64 = base64.split(',')[1];
            this.bAudioSending = true;
            $.get("/api/v1/control/talk", {
              serial: this.serial,
              code: this.code,
              audio: base64,
            }).error(() => {
              if(!this.bAudioSendError) {
                this.bAudioSendError = true;
                setTimeout(() => {
                  this.bAudioSendError = false;
                }, 10000);
              }
            }).always(() => {
              this.bAudioSending = false;
            })
          }
          reader.readAsDataURL(pcm);
        }
      })
    }
    function talkStop() {
      if(this.recorder) {
        this.recorder.stop();
        this.recorder = null;
        $(this.$el).find(".fa-microphone.active, .ptz-talk.active").removeClass("active");
        this.$refs["player"].setMuted(this.muted_bak);
        return;
      }
    },
           

繼續閱讀