18

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.

Kkulikovskis
  • 2,028
  • 16
  • 28
  • did u find the issue / workaround? – asafrob Apr 13 '20 at 12:33
  • No, unfortunately, I didn't. I have not looked into the issue more since a few days after I wrote this problem. Since our project priorities shifted this problem was left unsolved. But if you find any solution please post it here as it seems from the upvotes this is not uncommon – Kkulikovskis Apr 14 '20 at 13:37

3 Answers3

0

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!

Ivan Sanchez
  • 546
  • 5
  • 17
  • Could you please point to the solution comment? Because my issue is not directly related to what that thread is about. At least I can't find a connection. i have no problem with the stream itself or playing video. Problem is with enforcing constraints – Kkulikovskis Feb 05 '19 at 17:27
  • I saw the comment of tetreault: @alienpavlov here's my full JS demo if you need it. Know I see that it doesn't appears to be the solution at all. Sorry... And searching on the guide, I see that safari is not compatible for this method getConstrains() [Link here](https://developer.mozilla.org/es/docs/Web/API/MediaStreamTrack/getConstraints) – Ivan Sanchez Feb 05 '19 at 18:57
  • 1
    it says that the compatibility is unknown. But that is not the issue - the issue is that the constraints themselves are not working – Kkulikovskis Feb 08 '19 at 09:11
  • 1
    Still an issue in 2020. – Chemdream May 14 '20 at 03:01
0
   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.

Rowf Abd
  • 734
  • 10
  • 21
  • `getSupportedConstraints` shows what constraints are supported - and `facingMode` is also returned when I call this function. The problem is that even though it is supported, it is completely ignored when set. – Kkulikovskis Feb 09 '19 at 12:21
0

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.

asafrob
  • 1,838
  • 13
  • 16
  • This is accurate in my experiments lately, too. I don't get the impression many people are using this tech and Apple isn't paying a lot of attention to it either. – johnny Apr 20 '20 at 23:06