chrome.tabCapture.capture returned stream is undefined

I've got some js code in a chrome background extension of the following :

function handleCapture(stream) {
    console.log('content captured');
    console.log("backround.js stream: ", stream);
    alert(stream);
    // localStream = stream; // used by RTCPeerConnection addStream();
    // initialize(); // start signalling and peer connection process
}

function captureCurrentTab() {
    console.log('reqeusted current tab');
    chrome.tabs.getSelected(null, function(tab) {
        console.log('got current tab');
        var selectedTabId = tab.id;
        chrome.tabCapture.capture({
            audio : false,
            video : true
        }, handleCapture);
    });
}

However, when this is ran, the "handleCapture" variable "stream" that is passed in is always undefined? Is this to be expected or is there something that I am missing here?

Also, I've confirmed that my manifest.json contains the capture permission and I am using chrome canary Version 31.0.1607.1 canary Aura.

Thanks, Mike


You should probably provide some constraints to make it work. See: http://developer.chrome.com/extensions/tabCapture.html#type-MediaStreamConstraint

The capture param you provided is a MediaTrackConstraint, see: http://dev.w3.org/2011/webrtc/editor/getusermedia.html#mediastreamconstraints

that is also a simple JS object, where you should set some mandatory options, see: http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-MediaTrackConstraints

So the following should help, if you set all the needed settings in mandatory object:

chrome.tabCapture.capture({
    audio : false,
    video : true,
    {
        mandatory: {
            width: { min: 640 },
            height: { min: 480 }
        }
    }
}, handleCapture);

I had this same issue when I was trying to drive a tabCapture purely from a background script, I found this on the tabCapture reference page:

Captures the visible area of the currently active tab. This method can only be used on the currently active page after the extension has been invoked , similar to the way that activeTab works. Note that Chrome internal pages cannot be captured.

My understanding is that this means you need to drive it from a browserAction for your extension, like so:

chrome.browserAction.onClicked.addListener(function(request) {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabCapture.capture({audio: true, video: true}, callback);
    });
});

That's what worked for me.

链接地址: http://www.djcxy.com/p/63720.html

上一篇: Chrome pushmessaging API向后兼容性

下一篇: chrome.tabCapture.capture返回的流未定义