renderFrame method

void renderFrame(
  1. num epoch
)

Implementation

void renderFrame(num epoch) {
  try {
    //const int HAVE_NOTHING      = 0; // no information whether or not the video is ready
    //const int HAVE_METADATA     = 1; // metadata for the video is ready
    //const int HAVE_CURRENT_DATA = 2; // data for the current playback position is available, but not enough data to play next frame/millisecond
    //const int HAVE_FUTURE_DATA  = 3; // data for the current and at least the next frame is available
    const int HAVE_ENOUGH_DATA = 4; // enough data available to start playing

    if (video.readyState == HAVE_ENOUGH_DATA) {
      /**************************************************/
      /* scale and horizontally center the camera image */
      /**************************************************/
      var videoStreamSize = {
        'width': video.videoWidth,
        'height': video.videoHeight
      };
      var videoDisplaySize = {
        'width': video.scrollWidth,
        'height': video.scrollHeight
      };
      var videoRenderSize = calculateSize(videoStreamSize, videoDisplaySize);
      var xOffset =
          (videoDisplaySize['width']! - videoRenderSize['width']) / 2;

      widget.model.streamwidth = videoStreamSize["width"];
      widget.model.streamheight = videoStreamSize["height"];
      widget.model.displaywidth = videoDisplaySize["width"];
      widget.model.displayheight = videoDisplaySize["height"];
      widget.model.renderwidth = videoRenderSize["width"];
      widget.model.renderheight = videoRenderSize["height"];

      if (widget.model.scale)
      {
        canvas.width = S.toInt(videoRenderSize["width"]);
        canvas.height = S.toInt(videoRenderSize["height"]);
        canvas.context2D.drawImageScaled(video, xOffset, 0,
            videoRenderSize["width"], videoRenderSize["height"]);
      } else {
        canvas.width = videoStreamSize["width"];
        canvas.height = videoStreamSize["height"];
        canvas.context2D.drawImage(video, 0, 0);
      }

      lastFrame = epoch;
    }
  } catch(e) {
    // System.toast("Error in video");
  }

  // Request another frame
  if (abort != true) HTML.window.requestAnimationFrame(renderFrame);
}