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: conditionally add device permissions #86

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 50 additions & 0 deletions src/device/device-management.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
createDisplayStream,
createDisplayStreamWithAudio,
createMicrophoneStream,
getDevices,
} from './device-management';

jest.mock('../mocks/media-stream-stub');
Expand Down Expand Up @@ -254,4 +255,53 @@ describe('Device Management', () => {
expect(localDisplayStream.contentHint).toBe('motion');
});
});

describe('getDevices', () => {
edvujic marked this conversation as resolved.
Show resolved Hide resolved
it('should call ensureDevicePermissions with both audio and video input kinds when no deviceKind is provided', async () => {
expect.hasAssertions();
const mockDevices = [
{ kind: 'audioinput', deviceId: 'audio1' },
{ kind: 'videoinput', deviceId: 'video1' },
] as MediaDeviceInfo[];

jest.spyOn(media, 'ensureDevicePermissions').mockResolvedValue(mockDevices);
jest.spyOn(media, 'enumerateDevices').mockResolvedValue(mockDevices);

const devices = await getDevices();
expect(media.ensureDevicePermissions).toHaveBeenCalledWith(
[media.DeviceKind.AudioInput, media.DeviceKind.VideoInput],
media.enumerateDevices
);
expect(devices).toStrictEqual(mockDevices);
});

it('should call ensureDevicePermissions with the provided deviceKind', async () => {
expect.hasAssertions();
const mockDevices = [{ kind: 'audioinput', deviceId: 'audio1' }] as MediaDeviceInfo[];

jest.spyOn(media, 'ensureDevicePermissions').mockResolvedValue(mockDevices);
jest.spyOn(media, 'enumerateDevices').mockResolvedValue(mockDevices);

const devices = await getDevices(media.DeviceKind.AudioInput);
expect(media.ensureDevicePermissions).toHaveBeenCalledWith(
[media.DeviceKind.AudioInput],
media.enumerateDevices
);
expect(devices).toStrictEqual(mockDevices);
});

it('should filter devices based on the provided deviceKind', async () => {
expect.hasAssertions();
const mockDevices = [
{ kind: 'audioinput', deviceId: 'audio1' },
{ kind: 'videoinput', deviceId: 'video1' },
] as MediaDeviceInfo[];

jest.spyOn(media, 'ensureDevicePermissions').mockResolvedValue(mockDevices);
jest.spyOn(media, 'enumerateDevices').mockResolvedValue(mockDevices);

const devices = await getDevices(media.DeviceKind.AudioInput);
expect(devices).toStrictEqual([{ kind: 'audioinput', deviceId: 'audio1' }]);
});
});
});
14 changes: 10 additions & 4 deletions src/device/device-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,17 @@ export async function createDisplayStreamWithAudio<
*/
export async function getDevices(deviceKind?: media.DeviceKind): Promise<MediaDeviceInfo[]> {
let devices: MediaDeviceInfo[];
const deviceKinds: media.DeviceKind[] = [];

// If deviceKind is provided, add it to the array. Otherwise, add both audio and video input kinds.
if (deviceKind !== undefined) {
deviceKinds.push(deviceKind);
} else {
deviceKinds.push(media.DeviceKind.AudioInput, media.DeviceKind.VideoInput);
}
edvujic marked this conversation as resolved.
Show resolved Hide resolved

try {
devices = await media.ensureDevicePermissions(
[media.DeviceKind.AudioInput, media.DeviceKind.VideoInput],
media.enumerateDevices
);
devices = await media.ensureDevicePermissions(deviceKinds, media.enumerateDevices);
} catch (error) {
throw new WebrtcCoreError(
WebrtcCoreErrorType.DEVICE_PERMISSION_DENIED,
Expand Down
Loading