Skip to content

Commit

Permalink
feat: conditionally add device permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
evujici committed Sep 19, 2024
1 parent 33cb00d commit ae1e499
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
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', () => {
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);
}

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

0 comments on commit ae1e499

Please sign in to comment.