jster
jster

Reputation: 31

Capture screen -- chrome.desktopCapture.chooseDesktopMedia fails -- PNacl extension

I'm trying to use the desktopCapture API in the following manner.

chrome.desktopCapture.chooseDesktopMedia(
            ["screen", "window"], onAccessApproved);

chrome.desktopCapture shows as undefined when I set a breakpoint and inspect it. Permissions in my manifest file are as follows:-

"permissions": ["desktopCapture", "notifications" ]

Common causes for failure of this API are listed here as

And I don't have those problems.

FYI, I am trying to develop a Chrome extension to capture the screen using PNacl, and have borrowed from the media_stream_video example downloaded from here. But I haven't even gotten to sending a message to the pexe side yet. I'm still stuck at chrome.desktopCapture.chooseDesktopMedia returning undefined.

Upvotes: 3

Views: 13926

Answers (1)

xdumaine
xdumaine

Reputation: 10329

You need to call chrome.desktopCapture.chooseDesktopMedia from the background script running in the context of the extension. This Sample shows a simple method to use the extension to get screen media.

Keep in mind that this is callback based, so you get access to the stream id from the callback.

This runs in the context of your page (see full example here):

    // check that the extension is installed
    if (sessionStorage.getScreenMediaJSExtensionId) {
        // send a message to your extension requesting media
        chrome.runtime.sendMessage(sessionStorage.getScreenMediaJSExtensionId,
            {type:'getScreen', id: 1}, null,
            function (data) {
                if (data.sourceId === '') { // user canceled
                    // handle error
                } else {
                    constraints.video.mandatory.chromeMediaSourceId = data.sourceId;
                    getUserMedia(constraints, callback);
                }
            }
        );
    }

And this run in the context of your extension (see full example here):

chrome.runtime.onMessageExternal.addListener(function (message, sender, callback) {
    switch(message.type) {
        case 'getScreen':
            var pending = chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
                                                               sender.tab, function (streamid) {
                // communicate this string to the app so it can call getUserMedia with it
                message.type = 'gotScreen';
                message.sourceId = streamid;
                callback(message);
                return false;
            });
            return true; // retain callback for chooseDesktopMedia result
    }
});

Upvotes: 1

Related Questions