thataustin
thataustin

Reputation: 2045

Mock navigator.mediaDevices.enumerateDevices

I want to be on a WebRTC call and hear the sound even though I don't have a microphone attached to the device. The web app I'm using to join the VOIP call detects that I don't have a microphone and turns off my audio because it assumes that I want to call in.

I noticed that the web app is using navigator.mediaDevices.enumerateDevices to determine whether or not I have the proper devices installed to be on the call. Is there any way I can mock that to return valid MediaDeviceInfo objects to trick the site into thinking that I have a valid microphone so I can stay on the call?

Edit pertaining to duplicate answer

I don't think I can just override navigator.enumerateDevices = function(){...}

The return value is a list of MediaDeviceInfo objects, which you can't create with the MediaDeviceInfo constructor. But I'd love to know if anyone knows of a way to create some sort of response array that would satiate any consumer of that resolved promise on the page.

Upvotes: 1

Views: 3309

Answers (2)

Nagendran
Nagendran

Reputation: 297

navigator.mediaDevices.enumerateDevices returns promise. Try the below snippet. You can add n number of devices.

var device1 = {
    deviceId: "default",
    kind: "audiooutput",
    label: "",
    groupId: "default"
}
device1.__proto__ = MediaDeviceInfo.prototype;
navigator.mediaDevices.enumerateDevices = function() { 
    return new Promise((res, rej)=>{res([device1])})
}

Upvotes: 4

SimfikDuke
SimfikDuke

Reputation: 1148

You can try something like this:

var device1 = {
    deviceId: "default",
    kind: "audiooutput",
    label: "",
    groupId: "default"
}
device1.__proto__ = MediaDeviceInfo.prototype; 
navigator.mediaDevices.enumerateDevices = function() { 
    return [device1]
}

Upvotes: 1

Related Questions