简体   繁体   English

将“navigator.mediaDevices.getUserMedia”替换为“canvas.captureStream”

[英]Replace "navigator.mediaDevices.getUserMedia" with "canvas.captureStream"

I'm using Red5pro html SDK to create a peer to peer connection between a server and a client, video and audio chat work like a charm with getUserMedia() .我正在使用 Red5pro html SDK 在服务器和客户端之间创建点对点连接,视频和音频聊天就像使用getUserMedia()的魅力一样。 The problem is, I want to stream from a DOM element(Canvas), not from user's camera, using captureStream() .问题是,我想使用captureStream()从 DOM 元素(画布)而不是从用户的相机进行流式传输。

Red5Pro has method called " OnGetUserMedia " with the following instructions: Red5Pro 具有名为“ OnGetUserMedia ”的方法,其说明如下:

The onGetUserMedia method - when defined on the configuration provide to a WebRTC-based Publisher - will override the internal call to getUserMedia in the Red5 Pro HTML SDK. onGetUserMedia 方法 - 在配置中定义时提供给基于 WebRTC 的发布者 - 将覆盖 Red5 Pro HTML SDK 中对 getUserMedia 的内部调用。

You can provide your own logic on how getUserMedia is invoked and a Media Stream attained by setting the onGetUserMedia attribute to a method that conforms to the following guidelines:您可以通过将 onGetUserMedia 属性设置为符合以下准则的方法来提供有关如何调用 getUserMedia 和获得媒体流的自己的逻辑:

No input arguments are provided to onGetUserMedia.没有向 onGetUserMedia 提供输入参数。 It is expected that a Promise object is returned.期望返回一个 Promise 对象。 A MediaStream object must be provided in the resolve of the Promise.必须在 Promise 的解析中提供 MediaStream 对象。

When I did a research and asked the Red5pro Support team, they said当我进行研究并询问 Red5pro 支持团队时,他们说

Inside of the promise returned, you can derive a MediaStream from captureStream.在返回的承诺中,您可以从 captureStream 派生一个 MediaStream。

Ref: Red5Pro Documentation参考: Red5Pro 文档

I just don't have a clue what to do, or what to change.我只是不知道该怎么做,或者改变什么。

here's a sample of the onGetUserMedia Method:这是onGetUserMedia方法的示例:

{
  host: "localhost",
  protocol: 8083,
  port: 8081,
  streamName: "mystream",
  iceServers: [{ urls: "stun:stun2.l.google.com:19302" }],
  onGetUserMedia: function () {
    return navigator.mediaDevices.getUserMedia({
      audio: true,
      video: {
        width: {
          max: 1920,
          ideal: 1280,
          min: 640,
        },
        width: {
          max: 1080,
          ideal: 720,
          min: 360,
        },
      },
    });
  },
};

Any Help?任何帮助?

What you have been asked to do is to set the onGetUserMedia property to a function that does return a Promise, itself resolving to a MediaStream.你被要求做的是将onGetUserMedia属性设置为一个返回 Promise 的函数,它本身解析为 MediaStream。

This is probably because they've built their API with the correct assumption that most MediaStreams their user will have is one coming from mediaDevices.getUserMedia method, and this method does return such a Promise that do resolve to a MediaStream.这可能是因为他们在构建 API 时正确假设了他们的用户将拥有的大多数 MediaStreams 来自mediaDevices.getUserMedia方法,并且此方法确实返回了这样一个 Promise 并解析为 MediaStream。

In order to comply with their code, you have to wrap your own MediaStream in such a Promise.为了遵守他们的代码,您必须将自己的 MediaStream 包装在这样的 Promise 中。
Indeed, HTMLCanvasElement.captureStream is synchronous and returns directly the MediaStream, without a Promise wrapper.事实上, HTMLCanvasElement.captureStream是同步的,直接返回 MediaStream,没有 Promise 包装器。

So to do this, you just need to create a function that will wrap your MediaStream in a Promise, and this can be done by the Promise.resolve method:因此,要做到这一点,您只需要创建一个将 MediaStream 包装在 Promise 中的函数,这可以通过Promise.resolve方法完成:

{
  [...]
  iceServers: [{urls: 'stun:stun2.l.google.com:19302'}],
  onGetUserMedia: () => Promise.resolve(canvas.captureStream(frameRate))
}

Ps:附:
() => syntax is ES6 Arrow function , widely supported by browsers that do support HTMLCanvasElement.captureStream . () =>语法是 ES6 箭头函数,被支持HTMLCanvasElement.captureStream 的浏览器广泛支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM