Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question iOS Safari WebGL Webcamtexture can't access back camera

Discussion in 'Web' started by kimvasquez17, Feb 11, 2022.

  1. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    Hi,

    I'm trying to access the back camera of my iPhone but it keeps opening the front-facing camera. But when I tried it on my android device, I can access the back camera.

    Android Device: Xiaomi Mi9T
    Browser: Chrome

    iOS device: iPhone 13 Pro Max ios 15.2
    Browser: Safari and Chrome

    Edit: Btw my Unity version is 2021.2.11f1

    Another Edit: I look into the ios chrome console and had an error. Error: Applying iOS Safari specific workaround to video playback: https://bugs.webkit.org/show_bug.cgi?id=217578

    Code below
    Code (CSharp):
    1. for (int i = 0; i < WebCamTexture.devices.Length; i++)
    2.         {
    3.             if (!WebCamTexture.devices[i].isFrontFacing)
    4.             {
    5.                 cam = new WebCamTexture(WebCamTexture.devices[i].name, Screen.width, Screen.height);
    6.                 break;
    7.             }
    8.         }
    9.  
    10.         if (cam == null)
    11.         {
    12.             Debug.Log("This device does not have back camera");
    13.             return;
    14.         }
    15.  
    16.  
    17.         cam.Play();
    18.         background.texture = cam;
     
    Last edited: Feb 11, 2022
  2. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    That's awesome! but right now it's too pricey for me. I only need the back cam access fix, only that part. Thanks btw!
     
  3. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    I found out that only 1 webcam device input in iOS is detected and 2 in Android. I edited the WebCam.js so that I can log its devices. The attached images are the logs in iOS and Android.

    iOS:

    iOS Log.jpg

    Android:

    Android Log.png
     
  4. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    I posted the bug here
    CASE 1406834. Still in Fogbugz though
     
  5. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    431
    The issues with front/back webcam access is currently being worked on.
     
    Thaina, kimvasquez17 and Marks4 like this.
  6. kimvasquez17

    kimvasquez17

    Joined:
    Dec 19, 2017
    Posts:
    22
    Thank you!
     
  7. Raphael92

    Raphael92

    Joined:
    Dec 25, 2019
    Posts:
    5
    The issues solved?
     
  8. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    534
    My plugin does this with no issues.
     
  9. Raphael92

    Raphael92

    Joined:
    Dec 25, 2019
    Posts:
    5
    I solved it by editing WebCam.js (Path: Unity\2021.3.3f1\Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\lib)
    It works on iOS but doesn't work on desktop.

    Before editing
    Code (JavaScript):
    1. navigator.mediaDevices.getUserMedia({
    2.             audio: false,
    3.             video: videoInputDevices[deviceId].deviceId ? {
    4.                 deviceId: { exact: videoInputDevices[deviceId].deviceId }
    5.             } : true
    6.         }) ....
    After editing
    Code (JavaScript):
    1. navigator.mediaDevices.getUserMedia({
    2.             audio: false,
    3.             video: {facingMode: {exact: "environment"}}
    4.         }) ...
     
    Last edited: May 25, 2022
  10. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,160
    Are there any progress?
     
  11. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    270
    The issue here was that browsers must request permission from the user to access the webcams to be able to get any information about them, we were missing that in our implementation. I added it and it landed in:
    2021.3.6f1.397, 2022.1.8f1.3359 and 2022.2.0a15.2224
     
    Thaina likes this.
  12. Thaina

    Thaina

    Joined:
    Jul 13, 2012
    Posts:
    1,160
    I am using 2022.1.11f and this still not worked in ios with this code

    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         var env = WebCamTexture.devices.FirstOrDefault((device) => !device.isFrontFacing);
    5.         var texture = new WebCamTexture(env.name);
    6.         image.texture = texture;
    7.         texture.Play();
    8.     }
    9.  
    I should get any device that was backface from this code. But it only worked in android and it open front camera in webgl ios instead

    I still need to hack the webcam.js to configure facingMode by myself like above comment

    As far as I can inventigate it seem chrome ios (with safari under it?) is not return proper data with
    navigator.mediaDevices.enumerateDevices
    . The detail about each device is somehow missing. It seem it not even return all camera
     
  13. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    287
    Requires modification of WebCam.js but allows back camera access on iOS.

    This is an example of WebCam.js modification of 2021.3.6f1.
     

    Attached Files:

    n4mm4p1984 likes this.
  14. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    270
    kimvasquez17 and Thaina like this.
  15. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    287
    Unity currently sets isFrontFacing by checking if device.label contains 'front'.

    The problem with iOS Safari is that if her browser (mobile device) language is set to something other than English, the device.label will be retrieved in that language.

    So, if it is set to Japanese etc., it will always be false.

    ex: Japanese
     

    Attached Files:

    • shot.png
      shot.png
      File size:
      181.6 KB
      Views:
      242
    Last edited: Aug 22, 2022
    KamilCSPS and Thaina like this.
  16. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    270
    @gtk2k ohhhh..that's a fun discovery for me :| I'll have to bring it up with the team and see if/how we can deal with this!
     
    Thaina likes this.