OverconstrainedError when requesting speakers stream via getUserMedia

301 views
Skip to first unread message

Dewde

unread,
Dec 18, 2022, 6:12:53 AM12/18/22
to Chromium Extensions
Please consider the code below, in either the context of an extension or simple HTML context:

navigator.mediaDevices.getUserMedia({audio: true}).then(function() {
    navigator.mediaDevices.enumerateDevices().then((devices) => {
        devices.forEach(function(device1, k1) {
            if (device1.kind == 'audiooutput' && device1.deviceId == 'default') {
                const speakersGroupId = device1.groupId;
                devices.forEach(function(device2, k2) {
                    if (device2.groupId == speakersGroupId && ['default', 'communications'].includes(device2.deviceId) === false) {
                        const speakersId = device2.deviceId;
                        const constraints = {
                            audio: {
                                deviceId: {
                                    exact: speakersId
                                }
                            }
                        };
                        console.log('Requesting stream for deviceId '+speakersId);
                        navigator.mediaDevices.getUserMedia(constraints).then((stream) => { // **this always fails**
                            console.log(stream);
                        });
                    }
                });
            }
        });
    });
});


The code asks for permissions via the first getUserMedia, then enumerates all devices, picks the default audio output then tries to get a stream for that output.

But it will always throw the error: OverconstrainedError { constraint: "deviceId", message: "", name: "OverconstrainedError" } when getting the audio stream.

There is nothing I can do in Chrome 108 and 109 beta to get this to work.

I see a report here that it works, but not for me.

Please tell me that I'm doing something wrong, or if there's another way to get the speaker stream that doesn't involve chrome.tabCapture or chrome.desktopCapture.

Chrome MV3 extension ways are welcomed, besides HTML5.

Mouna Naravani

unread,
Dec 21, 2022, 4:33:52 AM12/21/22
to Chromium Extensions, Dewde
Hello,

I'm also facing same issue. Did you find any solution??

PhistucK

unread,
Dec 22, 2022, 10:48:50 AM12/22/22
to Dewde, Chromium Extensions
I thought navigator.mediaDevices.getDisplayMedia is the way to get the system audio?

PhistucK


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/e7b17065-692d-450c-afe6-b6434f060d97n%40chromium.org.

Dewde

unread,
Dec 26, 2022, 4:57:09 PM12/26/22
to Chromium Extensions, PhistucK, Chromium Extensions, Dewde
While possible to record sound with getDisplayMedia, it makes for a very poor user experience, asking for tab selection every time and then intrusively showing the blue rectangle with status bar and the Stop Sharing button. The alternative is to use tabCapture which is extremely buggy and undocumented, at least on MV3. You need to hack it using iframes or unwanted tabs to bring it to foreground, doesn't work in offscreen documents although they've been created especially for it, you need to have the extension invoked, you need to pass stream IDs around, and even when you get it to work, the tab sound gets muted, making it unusable in most use cases.
Reply all
Reply to author
Forward
0 new messages