3

I am trying to develop a Chrome Extension to access webcam and record a video, and to download it once stopped.

Extension fails with an error on invoking the following line of code:

navigator.mediaDevices.getUserMedia({ audio: false, video: true }).then(handleSuccess).catch(function (err) {alert(err)});".

Error is :

 NotAllowedError: Failed due to shutdown

I am using Chrome Version 80.0.3987.132 (Official Build) (64-bit) on Mac.

What am I doing wrong here? Can't we access device webcam via an extension? Any pointers?

Sample code below:

manifest.json

{
  "manifest_version": 2,
  "name": "Video Capture",
  "version": "0.1",
  "browser_action": {
    "default_icon": "logo.png",
    "default_popup": "popup.html"
   }
}

popup.html

<html>
  <head>
    <title>Video Capture</title>
    <script src="videoCapture.js"></script>
  </head>
  <body>
    <button id="start">Start</button>
    <button id="stop">Stop</button>
    <a id="download">Download</a>
  </body>
</html>

videoCapture.js

var shouldStop = false;
window.addEventListener('load', function showPopup() {
    alert("Extension started");
    startButton = document.getElementById('start');
    stopButton = document.getElementById('stop');
    downloadLink = document.getElementById('download');
    shouldStop = false;
    startButton.addEventListener('click', function() {
        if (!navigator.mediaDevices || !navigator.mediaDevices.enumerateDevices) {
            alert("This browser does not support the API yet");
        }
        alert("Media Devices Available..Starts recording.");

        var handleSuccess = function(stream) {
            alert("handling recording");
            const options = {mimeType: 'video/mp4'};
            const recordedChunks = [];
            const mediaRecorder = new MediaRecorder(stream);

            mediaRecorder.start(5000); //capture video for 5 seconds

            mediaRecorder.ondataavailable = function(e) {
                if (e.data.size > 0) {
                    recordedChunks.push(e.data);
                }
                if(shouldStop === true && stopped === false) {
                    mediaRecorder.stop();
                    stopped = true;
                    stream.getTracks().forEach(function(track) {
                        track.stop();
                    });
                }
            };

            mediaRecorder.onstop = function() {
                downloadLink.href = URL.createObjectURL(new Blob(recordedChunks));
                var currentTimestamp = Date.now();
                downloadLink.download = 'recording-'+currentTimestamp+'.mp4';
                alert("Click Download Link to download captured video");
            };
        };

        navigator.mediaDevices.getUserMedia({ audio: false, video: true })
            .then(handleSuccess).catch(function (err) {alert(err)});
    });

    stopButton.addEventListener('click', function() {
        shouldStop = true;
        alert("stopped");
    });    

});
Anoop
  • 53
  • 1
  • 5

1 Answers1

2

Linking to this question as a possible solution (getUserMedia needed to be run in the website tab NOT from the popup or the background script): https://stackoverflow.com/a/51009577/4136722

Wide Awake
  • 1,339
  • 1
  • 11
  • 18