What constraints should I pass to getUserMedia() in order to get two video mediaStreamTracks?

不羁的心 提交于 2019-11-26 18:30:28

问题


I can get mediaDevices of 'videoinput' kind via navigator.mediaDevices.enumerateDevices() promise.

I can get mediaStream via navigator.mediaDevices.getUserMedia(constraints) promise.

What should constraints look like in order to have two video tracks in userMedia?


回答1:


You can get max one video track and one audio track each time you call getUserMedia(), but you can call it multiple times. This may ask the user more than once though, depending on https, browser, and what the user does.

Following the standard (which at this time requires using adapter.js in Chrome), to get a specific "videoinput" device, pass its deviceId into getUserMedia using the deviceId constraint:

navigator.mediaDevices.enumerateDevices()
.then(devices => {
  var camera = devices.find(device => device.kind == "videoinput");
  if (camera) {
    var constraints = { deviceId: { exact: camera.deviceId } };
    return navigator.mediaDevices.getUserMedia({ video: constraints });
  }
})
.then(stream => video.srcObject = stream)
.catch(e => console.error(e));

The exact keyword makes the constraint required, guaranteeing it'll return only the right one, or fail.

If you want two cameras, you'll have to call getUserMedia again with a different deviceId, and hope the OS you're on supports it (e.g. phones generally don't).



来源:https://stackoverflow.com/questions/33761770/what-constraints-should-i-pass-to-getusermedia-in-order-to-get-two-video-media

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!