-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.js
More file actions
57 lines (53 loc) · 1.72 KB
/
setup.js
File metadata and controls
57 lines (53 loc) · 1.72 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
localStream = null;
const detectDevices = (deviceInfos) => {
for (let i = 0; i !== deviceInfos.length; ++i) {
const deviceInfo = deviceInfos[i];
const element = document.createElement("option");
element.value = deviceInfo.deviceId;
if (deviceInfo.kind === "videoinput") {
element.innerText =
deviceInfo.label || `camera ${videoDevices.length + 1}`;
videoDevices.add(element);
} else if (deviceInfo.kind === "audioinput") {
element.innerText =
deviceInfo.label || `microphone ${audioDevices.length + 1}`;
audioDevices.add(element);
}
}
};
navigator.mediaDevices
.getUserMedia({ audio: true, video: true })
.then((stream) => {
window.localStream = stream;
return navigator.mediaDevices.enumerateDevices();
})
.then(detectDevices)
.then(() => {
localStream.getTracks().forEach((t) => t.stop());
localStream = null;
})
.catch((error) => console.log("Error detecting devices", error));
const startSelf = async () => {
if (videoCheck.checked && !videoDevices.value) {
videoDevices.selectedIndex = 0;
}
if (audioCheck.checked && !audioDevices.value) {
audioDevices.selectedIndex = 0;
}
const vDevId = videoDevices.value;
const aDevId = audioDevices.value;
const constraints = {};
if (videoCheck.checked) {
constraints.video = { deviceId: vDevId ? { exact: vDevId } : undefined };
}
if (audioCheck.checked) {
constraints.audio = { deviceId: aDevId ? { exact: aDevId } : undefined };
}
await navigator.mediaDevices
.getUserMedia(constraints)
.then((stream) => {
window.localStream = stream;
selfVideo.srcObject = window.localStream;
})
.catch((error) => console.log("Error start self", error));
};