getUserMedia constraints ignored on Safari 12

2020-08-10 07:21发布

问题:

For some reason Safari 12 (haven't tried on other versions) is completely ignoring getUserMedia constraints.

My code looks something like this

  navigator.mediaDevices.getUserMedia({
    video: {
      facingMode: 'environment',
      width: {
        min: 640,
        ideal: 1280
      }
    },
    audio: true
  })
 .then(userStream)//Safari on Mac ignores constraints and ends up executing here
 .catch(fallback)//Chrome on Mac raises and error and ends up executing here

On Chrome when I do stream.getVideoTracks()[0].getConstraints() I get exactly what is expected - constraints listed above.

When I do the same in Safari I get an empty object.

Problem arises when I try to force using a back camera (for mobile devices), then catch errors to fallback to front camera.

Chrome on Mac properly raises an error and falls back to front camera.

Safari on Mac does not raise any errors, when being forced to use back camera, resulting in wrong properties further in my app.

Have searched SO, getUserMedia and still can't find any solutions or even someone having the same problem.

Maybe there is a better way to determine if a device have front/back cameras available?

EDIT:

getSupportedConstraints() function correctly returns that facingMode is supported. Problem is that it is ignored when set. As for the getConstraints() function not being supported - that might be the case, but that is not the issue. The issue is enforcement of the supported constraints (facingMode in this case).

This problem can be easily replicated with the code provided, if you try to setting facingMode: 'environment' and try opening the same code from Chrome and from Safari.

回答1:

This issue on github for webrtc issue closed appears to be to have the solution for you.

I have struggled a lot with webrtc to make this work on different browsers.

Good luck!



回答2:

   MediaDevices.getSupportedConstraints()

getSupportedConstraints() would be the best option for you.

https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getSupportedConstraints#Browser_compatibility

So far it is working with Safari 11, it might be working with the Safari 12 also.



回答3:

I encountered a similar problem with mobile Safari. What I learned is that for now the exact keyword is ignored, so you should query the capabilities and and your own exact logic. also regarding frame rate this also does not work, but it does work for resolution.