How to get callback on blocking webcam by user in Javascript getUserMedia?

Viewed 333

I starts webcam stream in my web page using navigator.mediaDevices.getUserMedia. I want to get some callback event when user explicitly blocking camera from the tab option ('Pause' on safari & 'Always block camera' option in chrome). I attached chrome screenshot just to clear the problem.

eChrome option to block camera when

When user allows permission first time and then blocking camera using above option then there is no event being triggered at my code. I tried searching for such events but did not find anything. I just wanted to show some message to user on blocking the camera. Following is my full code using to start webcam stream.

function startCamera() {
  if (streaming) return;
  navigator.mediaDevices.getUserMedia({video: true, audio: false})
    .then(function(s) {
    stream = s;
    video.srcObject = s;
    video.play();
    console.log("Start camera");
    isCameraCap = true;
  })
    .catch(function(err) {
     //executing when user disallow permission 
    console.log(`Stop camera at error ${err}`);
    console.log("An error occured! " + err);
    isCameraCap = true;
  });

  video.addEventListener("canplay", function(ev){
    if (!streaming) {
      videoWidth = video.videoWidth;
      videoHeight = video.videoHeight;
      video.setAttribute("width", videoWidth);
      video.setAttribute("height", videoHeight);
      canvasOutput.width = videoWidth;
      canvasOutput.height = videoHeight;
      streaming = true;
    }
    startVideoProcessing();
  }, false);
}

1 Answers
stream.getTracks().forEach(track => track.onended = () => console.log('stopped'))

You can listen to the 'ended' event once you have the stream. This will give you a signal that capturing has stopped, without giving you the exact reason (i.e. it could also fire when the user unplugs the webcam)

Related