question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidStateError on Screen Sharing from extension

See original GitHub issue

Salaam,

I have used the code of your Screen Sharing extension to upload my own extension. I successfully uploaded it and it can also be installed inline. After this, I used your following code to communicate to extension to get source id:

    // todo: need to check exact chrome browser because opera also uses chromium framework
    var isChrome = !!navigator.webkitGetUserMedia;

    // DetectRTC.js - https://github.com/muaz-khan/WebRTC-Experiment/tree/master/DetectRTC
    // Below code is taken from RTCMultiConnection-v1.8.js (http://www.rtcmulticonnection.org/changes-log/#v1.8)
    var DetectRTC = {};

    (function () {
        var screenCallback;

        DetectRTC.screen = {
            chromeMediaSource: 'screen',
            getSourceId: function (callback) {
                if (!callback) throw '"callback" parameter is mandatory.';
                screenCallback = callback;
                window.postMessage('get-sourceId', '*');
            },
            isChromeExtensionAvailable: function (callback) {
                if (!callback) return;

                if (DetectRTC.screen.chromeMediaSource == 'desktop') callback(true);

                // ask extension if it is available
                window.postMessage('are-you-there', '*');

                setTimeout(function () {
                    if (DetectRTC.screen.chromeMediaSource == 'screen') {
                        callback(false);
                    } else callback(true);
                }, 2000);
            },
            onMessageCallback: function (data) {
                console.log('chrome message', data);

                // "cancel" button is clicked
                if (data == 'PermissionDeniedError') {
                    DetectRTC.screen.chromeMediaSource = 'PermissionDeniedError';
                    if (screenCallback) return screenCallback('PermissionDeniedError');
                    else throw new Error('PermissionDeniedError');
                }

                // extension notified his presence
                if (data == 'kiboconnection-extension-loaded') {
                    DetectRTC.screen.chromeMediaSource = 'desktop';
                }

                // extension shared temp sourceId
                if (data.sourceId) {
                    DetectRTC.screen.sourceId = data.sourceId;
                    if (screenCallback) screenCallback(DetectRTC.screen.sourceId);
                }
            }
        };

        // check if desktop-capture extension installed.
        if (window.postMessage && isChrome) {
            DetectRTC.screen.isChromeExtensionAvailable(function (status) {
                $scope.extensionAvailable = !status;
            });
        }
    })();


    window.addEventListener('message', function (event) {
        if (event.origin != window.location.origin) {
            return;
        }

        //console.log('THIS IS THE EVENT')
        //console.log(event)

        DetectRTC.screen.onMessageCallback(event.data);
    });

    //-----------------//

    function captureUserMedia(onStreamApproved) {
        // this statement defines getUserMedia constraints
        // that will be used to capture content of screen
        var screen_constraints = {
            mandatory: {
                chromeMediaSource: DetectRTC.screen.chromeMediaSource,
                maxWidth: 1920,
                maxHeight: 1080,
                minAspectRatio: 1.77
            },
            optional: []
        };

        // this statement verifies chrome extension availability
        // if installed and available then it will invoke extension API
        // otherwise it will fallback to command-line based screen capturing API
        if (DetectRTC.screen.chromeMediaSource == 'desktop' && !DetectRTC.screen.sourceId) {
            DetectRTC.screen.getSourceId(function (error) {
                // if exception occurred or access denied
                if (error && error == 'PermissionDeniedError') {
                    alert('PermissionDeniedError: User denied to share content of his screen.');
                }

                captureUserMedia(onStreamApproved);
            });
            return;
        }

        console.log(DetectRTC.screen.chromeMediaSource)

        // this statement sets gets 'sourceId" and sets "chromeMediaSourceId"
        if (DetectRTC.screen.chromeMediaSource == 'desktop') {
            screen_constraints.mandatory.chromeMediaSourceId = DetectRTC.screen.sourceId;
        }

        // it is the session that we want to be captured
        // audio must be false
        var session = {
            audio: false,
            video: screen_constraints
        };

        // now invoking native getUserMedia API
        navigator.webkitGetUserMedia(session, onStreamApproved, OnStreamDenied);

    }

It successfully shares the screen for the first time. However, when I stop screen sharing using screenStream.stop() and then when I again try to share the screen, it gives the following error to me.

{"constraintName":"","message":"","name":"InvalidStateError"}

I then went to your demo to see if I am able reproduce the same error there too but there was no option to stop the screen sharing and starting again.

Have you ever come across this error? Can you help why does this occur? and how would one solve this?

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DasPartyEinhorncommented, Oct 20, 2017

I had the same problem. See here solution is to set sourceId to null after closing Screensharing because in the capture-screen.js this is stored globally and reused.

0reactions
imidimicommented, Apr 26, 2019
connection.addStream({ screen: true, oneway: true })

This is giving DOM Exception to me Untitled

Read more comments on GitHub >

github_iconTop Results From Across the Web

Desktop sharing Chrome extension throws ...
I'm writing a simple WebRTC Google Chrome extension for desktop sharing. I tried to use getusermedia , but every time the error callback ......
Read more >
3553 - "InvalidStateError" for chromeMediaSource 'desktop' in ...
The latest Canary build always returns "InvalidStateError" when requesting the desktop media stream. The option to enable screen sharing has ...
Read more >
MediaDevices.getDisplayMedia() - Web APIs | MDN
See Using the Screen Capture API for more details and an example. ... the user to dynamically switch the shared tab during screen-sharing....
Read more >
WebRTC Screen Sharing using RTCMultiConnection
i'm working on screen sharing extension. i used your code and make the changes as per my requirement. when i uploaded the xpi...
Read more >
Why Am I Having Error "Screen Sharing Extension Failed to ...
If you face the error "Screen Sharing Extension Failed to Install" during the call, Please follow steps below: 1. Please make...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found