Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(WEBRTC-746): recursive call to getDevices when the browser does not support audiooutput like Safari and Firefox. #223

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/js/src/Modules/Verto/tests/setup/browsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Object.defineProperty(navigator, 'mediaDevices', {
value: {
enumerateDevices: jest.fn().mockResolvedValue(ENUMERATED_MEDIA_DEVICES),
getSupportedConstraints: jest.fn().mockReturnValue(SUPPORTED_CONSTRAINTS),
getUserMedia: jest.fn((constraints) => {
getUserMedia: jest.fn(async (constraints) => {
const stream = new global.MediaStream();
const { audio = null, video = null } = constraints;
if (audio !== null) {
Expand Down
10 changes: 0 additions & 10 deletions packages/js/src/Modules/Verto/tests/webrtc/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,12 @@ describe('Helpers browser functions', () => {
},
];
it('should invoke getUserMedia to request camera permissions and return device list removing duplicates', async (done) => {
// @ts-ignore
navigator.mediaDevices.enumerateDevices.mockResolvedValueOnce(
DEVICES_CAMERA_NO_LABELS
);
const devices = await getDevices();
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledTimes(1);
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({
audio: true,
video: true,
});
expect(devices).toHaveLength(5);
expect(devices[0].label).toEqual(
'Default - External Microphone (Built-in)'
);
Expand Down Expand Up @@ -266,17 +261,12 @@ describe('Helpers browser functions', () => {
},
];
it('should invoke getUserMedia to request microphone permissions and return device list removing duplicates', async (done) => {
// @ts-ignore
navigator.mediaDevices.enumerateDevices.mockResolvedValueOnce(
DEVICES_MICROPHONE_NO_LABELS
);
const devices = await getDevices();
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledTimes(1);
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({
audio: true,
video: true,
});
expect(devices).toHaveLength(5);
expect(devices[0].label).toEqual(
'Default - External Microphone (Built-in)'
);
Expand Down
58 changes: 30 additions & 28 deletions packages/js/src/Modules/Verto/webrtc/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,39 @@ const getDevices = async (
kind: MediaDeviceKind | undefined = null,
fullList: boolean = false
): Promise<MediaDeviceInfo[]> => {
let devices = await WebRTC.enumerateDevices().catch((error) => {
console.error('enumerateDevices', error);
return [];
});
if (kind) {
devices = devices.filter((d: MediaDeviceInfo) => d.kind === kind);
}
const valid: boolean =
devices.length &&
devices.every((d: MediaDeviceInfo) => d.deviceId && d.label);
if (!valid) {
const stream = await WebRTC.getUserMedia(_constraintsByKind(kind));
WebRTC.stopStream(stream);
return getDevices(kind);
}
if (fullList === true) {
return devices;
}
const found = [];
devices = devices.filter(({ kind, groupId }: MediaDeviceInfo) => {
if (!groupId) {
return true;
let devices = [];
//get user device browser permission
const stream = await navigator.mediaDevices
.getUserMedia(_constraintsByKind(kind))
.catch((error) => {
console.error(error);
return null;
});

if (stream) {
devices = await navigator.mediaDevices.enumerateDevices();
if (kind) {
devices = devices.filter((d: MediaDeviceInfo) => d.kind === kind);
}
const key = `${kind}-${groupId}`;
if (!found.includes(key)) {
found.push(key);
return true;

if (fullList === true) {
return devices;
}
return false;
});

// Remove duplicate devices
const found = [];
devices = devices.filter(({ kind, groupId }: MediaDeviceInfo) => {
if (!groupId) {
return true;
}
const key = `${kind}-${groupId}`;
if (!found.includes(key)) {
found.push(key);
return true;
}
return false;
});
}
return devices;
};

Expand Down