Search code examples
javascriptfirefoxvideocanvasgetusermedia

Firefox: drawImage(video) fails with NS_ERROR_NOT_AVAILABLE: Component is not available


Trying to call drawImage with a video whose source is a webcam feed seems to fail in Firefox with an NS_ERROR_NOT_AVAILABLE: Component is not available.

I have tried to wait for every event the video tag fires: play, playing, canplay, loadeddata, loadedmetadata, and so on, and nothing works. This seems to be because these events are firing before the stream is properly loaded into the <video> element.

JSFiddle with error (You can view the error in the console)

A side effect is that the width and height of the video is also incorrect.


Solution

  • This is a bug in Firefox. The easiest fix is to simply keep trying until the error goes away, since no event fires at the correct time.

    See: http://jsfiddle.net/9aT63/25/

    Basically, you have to wrap the drawImage call in a try/catch block.

    function drawVideo() {
      try {
        $vidCanvasCtx.drawImage($vid, 0, 0, $vidCanvas.width, $vidCanvas.height);
        ...
      } catch (e) {
        if (e.name == "NS_ERROR_NOT_AVAILABLE") {
          // Wait a bit before trying again; you may wish to change the
          // length of this delay.
          setTimeout(drawVideo, 100);
        } else {
          throw e;
        }
      }
    }