Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 934
vadirn Avatar asked Nov 17 '15 16:11

vadirn


People also ask

What does getUserMedia return?

It returns a Promise that resolves to a MediaStream object. If the user denies permission, or matching media is not available, then the promise is rejected with NotAllowedError or NotFoundError DOMException respectively.

Which browsers support getUserMedia?

getUserMedia one. Get User Media/Stream API is supported by Mozilla Firefox browser version 36 to 61.

What is getUserMedia?

getUserMedia() method prompts the user for permission to use up to one video input device (such as a camera or shared screen) and up to one audio input device (such as a microphone) as the source for a MediaStream .

Is getUserMedia a part of webRTC?

getUserMedia() is a part of the webRTC media capture API and is used to get access to the camera and the microphone connected to the user device (user computer, smartphone, etc.) from the browser.


1 Answers

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).

like image 80
jib Avatar answered Oct 01 '22 13:10

jib