-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
125 lines (102 loc) · 3.71 KB
/
scripts.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const controlBar = document.querySelector('#WebcamViewer__controlBar');
const videoWrapper = document.querySelector('#WebcamViewer__videoWrapper');
const videoStreaming = document.querySelector('#WebcamViewer__videoStreaming');
const audioSourceSelect = document.querySelector('#WebcamViewer__audioSourceSelect');
const videoSourceSelect = document.querySelector('#WebcamViewer__videoSourceSelect');
const fillContentCheckbox = document.querySelector('#WebcamViewer__fillContentCheckbox');
audioSourceSelect.onchange = getStream;
videoSourceSelect.onchange = getStream;
getStream().then(getDevices).then(gotDevices);
function getDevices() {
return navigator.mediaDevices.enumerateDevices();
}
function gotDevices(deviceInfos) {
window.deviceInfos = deviceInfos; // make available to console
for (const deviceInfo of deviceInfos) {
const option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'audioinput') {
option.text = deviceInfo.label || `Microphone ${audioSourceSelect.length + 1}`;
audioSourceSelect.appendChild(option);
} else if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || `Camera ${videoSourceSelect.length + 1}`;
videoSourceSelect.appendChild(option);
}
}
}
function getStream() {
if (window.stream) {
window.stream.getTracks().forEach(track => {
track.stop();
});
}
const audioSource = audioSourceSelect.value;
const videoSource = videoSourceSelect.value;
const constraints = {
audio: {deviceId: audioSource ? {exact: audioSource} : undefined},
video: {
deviceId: videoSource ? {exact: videoSource} : undefined,
width: {
min: 1280,
ideal: 1920,
max: 2560,
},
height: {
min: 720,
ideal: 1080,
max: 1440,
}
}
};
return navigator.mediaDevices.getUserMedia(constraints).
then(gotStream).catch(error => console.error(error));
}
function gotStream(stream) {
window.stream = stream; // make stream available to console
audioSourceSelect.selectedIndex = [...audioSourceSelect.options].
findIndex(option => option.text === stream.getAudioTracks()[0].label);
videoSourceSelect.selectedIndex = [...videoSourceSelect.options].
findIndex(option => option.text === stream.getVideoTracks()[0].label);
videoStreaming.srcObject = stream;
}
function getFullscreenElement() {
return document.fullscreenElement //standard property
|| document.webkitFullscreenElement //safari/opera support
|| document.mozFullscreenElement //firefox support
|| document.msFullscreenElement; //ie/edge support
}
function toggleFullscreen() {
if(getFullscreenElement()) {
document.exitFullscreen();
}else {
document.documentElement.requestFullscreen().catch(console.log);
}
}
let idleTimer = null;
let isControlBarVisible = false;
function showControlBar(time) {
clearTimeout(idleTimer);
const controlBarVisibleClass = 'WebcamViewer__controlBar--isVisible'
if (isControlBarVisible) {
controlBar.classList.add(controlBarVisibleClass)
}
isControlBarVisible = false;
idleTimer = setTimeout(function() {
controlBar.classList.remove(controlBarVisibleClass)
isControlBarVisible = true;
}, time);
}
document.addEventListener('mousemove', () => showControlBar(2000))
showControlBar(0);
videoWrapper.addEventListener('dblclick', () => {
toggleFullscreen();
});
fillContentCheckbox.addEventListener('click', ({ target }) => {
const isChecked = target?.checked
const fillContentClass = 'WebcamViewer__videoStreaming--fillContent'
if (isChecked) {
videoStreaming.classList.add(fillContentClass)
} else {
videoStreaming.classList.remove(fillContentClass)
}
}, false);