-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathmediaHelper.js
41 lines (38 loc) · 1.44 KB
/
mediaHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class MediaHelper {
// Constants for different media request types
static MediaRequestType = {
VIDEO_ONLY: 1,
AUDIO_AND_VIDEO: 2,
AUDIO_ONLY: 3,
};
// Async function to request camera access.
// idealWidthPx and idealHeightPx is only used if VIDEO
// or AUDIO_AND_VIDEO is requested.
// Returns a MediaStream upon success. Otherwise, null.
static async requestCamera(requestType = this.MediaRequestType.AUDIO_AND_VIDEO, idealWidthPx = 640, idealHeightPx = 480) {
const resolution = {
width: {ideal: idealWidthPx},
height: {ideal: idealHeightPx},
};
let constraints;
switch (requestType) {
case this.MediaRequestType.VIDEO_ONLY:
constraints = {video: resolution};
break;
case this.MediaRequestType.AUDIO_AND_VIDEO:
constraints = {video: resolution, audio: true, frameRate: { min: 10, max: 10 }};
break;
case this.MediaRequestType.AUDIO_ONLY:
constraints = {audio: true};
break;
default:
throw `requestCamera(): Unhandled case: ${requestType}!`;
}
try {
return await navigator.mediaDevices.getUserMedia(constraints);
} catch (e) {
console.error(`Could not find ${Object.keys(constraints)} input device.`, e);
return null;
}
}
}