Search Unity

Webcam Crash

Discussion in 'Windows' started by addscion, Jun 25, 2021.

  1. addscion

    addscion

    Joined:
    Jan 12, 2018
    Posts:
    13
    https://pastebin.com/YHKMWpCW

    I do not know if this crash is due to my laptop's age and webcam or an error in the code. Is this code sound?

    The webcam activates then the picture freezes seconds after.

    Two errors in log read:
    [] Device 0 not found = you have 0 devices available
    [] Error capturing camera feed. Maybe the camera has been disconnected?
     
  2. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    408
    Your code looks alright, but even if there is something wrong Unity shouldn't crash.
    Will you please file a bug for this issue?

    Webcams (on Windows) can be difficult to work with because of bad drivers, which might be the case here. My guess is we're failing to connect to the webcam (for some reason) which hits a bug somewhere in the error handling code paths.

    Unfortunately, this could be specific to your device and not something we can (easy) repro, but it's still worth filing a bug so we can at least investigate.
     
  3. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    I'm having a similar problem. Webcam video is fine until I maximize the Game window while in play mode. Then it freezes. Sometimes I get the same two error messages you are getting. If I de-maximize the Game window, the problem goes away. Also, with the Game window maximized, if I turn on the stats display, or check the Free Aspect/VSync (Game view only) box, the problem also goes away. Using 2020.3.17f1 under Windows 10 on an HP Omen 15. Problem is always reproducible on that computer, but does not appear at all on my somewhat older and slower Acer Predator.

    Problem does not appear if I maximize the Scene window. Nor does it appear if I undock the Game window and use the titlebar button to blow it up to full-screen.

    My scene is the default scene with a quad in it, and this code on the quad:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class VideoMin : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         if (WebCamTexture.devices.Length == 0)
    8.         {
    9.             Debug.Log("Can't find a camera.");
    10.         }
    11.         else
    12.         {
    13.             WebCamTexture camTex = new WebCamTexture(WebCamTexture.devices[0].name);
    14.             camTex.Play();
    15.  
    16.             GetComponent<Renderer>().material.mainTexture = camTex;
    17.         }
    18.     }
    19. }
     
  4. timke

    timke

    Joined:
    Nov 30, 2017
    Posts:
    408
    @Stevens-R-Miller

    That is a really strange bug and it makes me think it's actually a problem with frame timing or rendering, instead of an issue with the WebCam device itself. Maximizing the Game View shouldn't impact the video stream but it could alter the video frame renderer.

    Will you please file a bug for this issue?
    Thanks!
     
    Stevens-R-Miller likes this.
  5. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Done.

    Adding this line has, so far, consistently prevented the problem from returning:

    Application.targetFrameRate = 100;
     
  6. Seth-Robinson

    Seth-Robinson

    Joined:
    Aug 24, 2013
    Posts:
    11
    Just ran into this with the 2022.1.23f1 editor, the webcam (Logitech Brio) sputters off/on and it gets jerky, lagging up to 5 seconds at a time. Release build worked fine.

    Doing Stevens-R-Miller's trick by setting the max FPS fixed the editor issues. (Previously I was setting it to 0 for no limit)
     
    Stevens-R-Miller likes this.