.net-core - WebRTC 和 Asp.Net Core

标签 .net-core signalr webrtc

我想将音频流从我的 Angular Web 应用程序录制到我的 Asp.net Core Api。

我认为,使用 SignalR 及其 websockets 是实现这一目标的好方法。

通过此 typescript 代码,我可以获得 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 时,我得到了 null。

当我阅读WebRTC的文档时,我看到了有关RTCPeerConnection的信息。 IceConnection ...我需要那个吗?

如何使用 SignalR 将音频从 WebClient 流式传输到 Asp.netCore API?文档? GitHub?

感谢您的帮助

最佳答案

我找到了访问麦克风流并将其传输到服务器的方法,代码如下:

  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 的代码,这不是这里的目的。

关于.net-core - WebRTC 和 Asp.Net Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50220281/

相关文章:

c# - 我们怎样才能返回一个与方法返回类型不同的对象呢?

javascript - 仅当在 Angular js 中手动重新加载页面时,signalr js 客户端方法才会调用

c# - 如何通过使用任务的 I/O 绑定(bind)工作正确利用粗粒度并发?

angular - 将 IFormFile 附加到邮件而不保存文件

c# - 如何在单例中使用作用域依赖注入(inject)

手机闪屏出现时 Signalr 最佳实践

c# - 3 层架构中的 SQL 依赖性和 SignalR

ios - 为什么有些库需要嵌入而有些不需要?

webrtc - WebRTC App 的带宽和质量控制

webrtc - 使用 Kurento 将 RTSP 流式传输到 WebRTC