WebRTC и Asp.NetCore - PullRequest
       41

WebRTC и Asp.NetCore

0 голосов
/ 07 мая 2018

Я хотел бы записать аудиопоток из моего Angular Web App в мой Asp.net Core Api.

Я думаю, использование SignalR и его веб-сокетов - хороший способ сделать это.

С помощью этого машинописного кода я могу получить MediaStream:

import { HubConnection } from '@aspnet/signalr';

[...]

private stream: MediaStream;
private connection: webkitRTCPeerConnection;
@ViewChild('video') video;

[...]

navigator.mediaDevices.getUserMedia({ audio: true })
  .then(stream => {
    console.trace('Received local stream');
    this.video.srcObject = stream;
    this.stream = stream;

    var _hubConnection = new HubConnection('[MY_API_URL]/webrtc');
    this._hubConnection.send("SendStream", stream);
  })
  .catch(function (e) {
    console.error('getUserMedia() error: ' + e.message);
  });

И я обрабатываю поток в .NetCore API с помощью

  public class MyHub: Hub{
    public void SendStream(object o)
    {
    }
}

Но когда я бросил o в System.IO.Stream, я получил ноль.

Когда я прочитал документацию WebRTC, я увидел информацию о RTCPeerConnection. IceConnection ... Мне это нужно?

Как я могу транслировать аудио с WebClient на Asp.netCore API, используя SignalR? Документация? GitHub?

Спасибо за вашу помощь

1 Ответ

0 голосов
/ 09 мая 2018

Я нашел способ получить доступ к потоку микрофона и передать его на сервер, вот код:

  private audioCtx: AudioContext;
  private stream: MediaStream;

  convertFloat32ToInt16(buffer:Float32Array) {
    let l = buffer.length;
    let buf = new Int16Array(l);
    while (l--) {
      buf[l] = Math.min(1, buffer[l]) * 0x7FFF;
    }
    return buf.buffer;
  }

  startRecording() {
    navigator.mediaDevices.getUserMedia({ audio: true })
      .then(stream => {
        this.audioCtx = new AudioContext();
        this.audioCtx.createMediaStreamSource(stream);
        this.audioCtx.onstatechange = (state) => { console.log(state); }

        var scriptNode = this.audioCtx.createScriptProcessor(4096, 1, 1);
        scriptNode.onaudioprocess = (audioProcessingEvent) => {
          var buffer = [];
          // The input buffer is the song we loaded earlier
          var inputBuffer = audioProcessingEvent.inputBuffer;
          // Loop through the output channels (in this case there is only one)
          for (var channel = 0; channel < inputBuffer.numberOfChannels; channel++) {

            console.log("inputBuffer:" + audioProcessingEvent.inputBuffer.getChannelData(channel));
            var chunk = audioProcessingEvent.inputBuffer.getChannelData(channel);
            //because  endianness does matter
            this.MySignalRService.send("SendStream", this.convertFloat32ToInt16(chunk));
          }
        }
        var source = this.audioCtx.createMediaStreamSource(stream);
        source.connect(scriptNode);
        scriptNode.connect(this.audioCtx.destination);


        this.stream = stream;
      })
      .catch(function (e) {
        console.error('getUserMedia() error: ' + e.message);
      });
  }

  stopRecording() {
    try {
      let stream = this.stream;
      stream.getAudioTracks().forEach(track => track.stop());
      stream.getVideoTracks().forEach(track => track.stop());
      this.audioCtx.close();
    }
    catch (error) {
      console.error('stopRecording() error: ' + error);
    }
  }

Следующим шагом будет преобразование моего int32Array в файл wav.

источников, которые мне помогли:

Примечание: Я не добавил код о том, как настроить SignalR, здесь это не было целью.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...