How to find out reason for MediaStreamTrack.onended event

Viewed 247

I have a website that is used to take pictures, user has to take one picture with main camera and then second picture (selfie) with front camera. All those pictures are saved as blobs in db and can be viewed in a separate page.

Issue: sometimes one of the photos are plain black and it seems that mediaStreamTrack ends randomly which causes the image to arrive to DB as plain black. (this mostly happens with iPhones, but I have seen desktops with win10 have the same issue since I log userAgent and made a function that logs some events like 'camera permission requested', 'permission granted', 'stream ended').

Is there a way to obtain why onended event was fired?

function startVideo(facingMode = 'environment') {
  if (this.mediaStream && facingMode === 'user') {
    // stop previous stream to start a new one with different camera
    this.mediaStream.getVideoTracks[0].stop();
  }
  const videoEl = video.current;
  const canvasEl = canvas.current;

  navigator.mediaDevices
    .getUserMedia({
      video: {
        facingMode:
          facingMode === "user" ? { exact: facingMode } : facingMode,
        height: {
          min: 720,
          max: 720
        },
        width: {
          min: 720,
          max: 1280
        },
          advanced: [{ aspectRatio: 1 }]
      },
      audio: false
    })
    .then((stream) => {
      if (this.mediaStream !== stream) this.mediaStream = stream;
      videoEl.srcObject = this.mediaStream;
      videoEl.play();

      this.mediaStream.getVideoTracks()[0].onended = () => {
        console.log('stream ended unexpectedly');
        this.sendUserLog('stream ended');
      };
    })
    .catch((error) => {
      if (error.name === 'OverconstrainedError') {
        this.sendUserLog('camera quality too low')
      } else {
        console.log("An error occurred: " + error));
        this.sendUserLog('permission denied');
      }
    })
}

I also tried to log the onended event, but it only shows the source mediaStream properties and and type: 'ended', but I already know that since the event fired.

Also since most of these cases happen with mobiles, it seems implausible that camera was disconnected manually.

0 Answers
Related