5

I have a function which can let user select the camera and show the captured video on the page [like this]. My code works before Android Google Chrome version 52, but don't know why it is broken now.

First, I check which devices I can use.

navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
  devices.forEach(function(device) {
    console.log(device.kind + ": " + device.label +
                " id = " + device.deviceId);
  });
})
.catch(function(err) {
  console.log(err.name + ": " + err.message);
});

and it returns two cameras(one front, one back) as I expect.

videoinput: camera 1, facing front id = ef5f41259c805a533261c2d91c274fdbfa8a6c8d629231cb484845032f90e61a cam:19 videoinput: camera 0, facing back id = 81448a117b2569ba9af905d01384b32179b9d32fe6a3fbabddf03868f36e4750

Then I follow the sample code, and below is what exactly I use:

<video id="video" autoplay></video>
<script>
    var p = navigator.mediaDevices.getUserMedia({ video: {facingMode: "environment", width: 480, height: 800} });

    p.then(function(mediaStream) {
      var video = document.querySelector('video');
      video.src = window.URL.createObjectURL(mediaStream);
      video.onloadedmetadata = function(e) {
          // Do something with the video here.
      };
    });

    p.catch(function(err) { console.log(err.name); }); // always check for errors at the end.
</script>

Whether I set facingMode: "environment" or facingMode: {exact:"environment"}, it turns out using the front camera. Should I also report to Google?

Hui-Yu Lee
  • 909
  • 8
  • 20
  • 1
    See http://stackoverflow.com/questions/32086122/getusermedia-facingmode – jib Oct 13 '16 at 23:32
  • @jib, thanks. But should I use deviceId in contraints? since it's not written in the standard https://dev.w3.org/2011/webrtc/editor/archives/20140704/getusermedia.html#idl-def-MediaTrackConstraints and it only works with Chrome? – Hui-Yu Lee Oct 14 '16 at 06:30
  • `deviceId` ***is*** standard. See https://w3c.github.io/mediacapture-main/getusermedia.html#dom-mediatrackconstraintset-deviceid - and has been in Firefox since version 42. I believe Opera and Edge support it as well. Alternatively, use the [adapter.js](https://github.com/webrtc/adapter) polyfill I mention, which adds `facingMode` in Chrome. – jib Oct 14 '16 at 12:24
  • @jib unfortunately the facingMode "environment" doesn't work in Chrome v53 even if I include https://webrtc.github.io/adapter/adapter-latest.js, is it anything related to the notes [https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Browser_compatibility]: From version 47 to 52, the promise-based interface was only available through the adapter.js polyfill, or using the flag chrome://flags/#enable-experimental-web-platform-features. Starting with version 53, the promise-based interface is on by default, though that interface is still not available through navigator. – Hui-Yu Lee Oct 17 '16 at 02:24
  • 1
    Thanks for the heads-up. You're right, it [broke](https://github.com/webrtc/adapter/issues/370) in 53. – jib Oct 18 '16 at 00:18

0 Answers0