forked from Orphis/webrtc-sandbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera-controls.html
More file actions
264 lines (233 loc) · 7.73 KB
/
Copy pathcamera-controls.html
File metadata and controls
264 lines (233 loc) · 7.73 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<html>
<head>
<title>WebRTC Playground</title>
<style>
body {
font-family: monospace;
}
.encodingBox {
display: inline-block;
border: 1px solid black;
margin: 5px 5px 5px 5px;
padding: 5px 5px 5px 5px;
max-width: 90%;
}
video {
max-width: 600px;
}
</style>
</head>
<body>
<table>
<tr>
<td>
<video id="videoSource" autoplay muted></video>
</td>
</tr>
<tr>
<td>
<span id="videoSourceSize">0x0</span>
<span id="videoSourceFps">0</span>
fps
</td>
</tr>
</table>
<button id="start">Start!</button>
<button id="stop">Stop</button>
<select id="resolution">
<option value="QVGA">320x240 (QVGA)</option>
<option value="VGA">640x480 (VGA)</option>
<option value="HD">1280x720 (HD)</option>
<option value="FullHD">1920x1080 (FullHD)</option>
<option value="4K">4096x2160 (4K)</option>
<option value="8K">7680x4320 (8K)</option>
<option value="screen">Screen</option>
<option value="screen-FullHD">Screen (FullHD)</option>
<option value="screen-HD">Screen (HD)</option>
<option value="screen-conference">Screen (conference)</option>
<option value="screen-conference-FullHD">Screen (FullHD, conference)</option>
<option value="screen-conference-HD">Screen (HD, conference)</option>
</select>
|
<button id="enumerateDevices">Enumerate</button>
<select id="devices">
</select>
<br>
<br>
<div>Camera controls |
<button id="applyConstraints">Apply</button>
<div id="cameraControls"></div>
</div>
<button id="clearlog">Clear log</button>
<br>
<div id="div"></div>
<script>
const constraints = {
'QVGA': {
video: {
width: 320,
height: 240,
},
},
'VGA': {
video: {
width: 640,
height: 480,
},
},
'HD': {
video: {
width: 1280,
height: 720,
},
},
'FullHD': {
video: {
width: 1920,
height: 1080,
},
},
'4K': {
video: {
width: 4096,
height: 2160,
},
},
'8K': {
video: {
width: 7680,
height: 4320,
},
},
};
/*
const checkControls = (type) => {
if (videoSource.srcObject !== null) {
const capabilities = videoSource.srcObject.getVideoTracks()[0].getCapabilities();
log('Event ' + type + ' has brightness: ' + (capabilities['brightness'] !== undefined ? 'YES' : 'NO'));
}
};
videoSource.addEventListener('loadeddata', (e) => checkControls(e.type));
videoSource.addEventListener('loadedmetadata', (e) => checkControls(e.type));
videoSource.addEventListener('play', (e) => checkControls(e.type));
videoSource.addEventListener('timeupdate', (e) => checkControls(e.type));
*/
let stream;
let streamResolution;
const intervals = [];
const closeStream = () => {
for (const track of stream.getVideoTracks()) {
track.stop();
}
stream = null;
for (const interval of intervals) {
window.clearInterval(interval);
}
};
document.querySelector('#enumerateDevices').onclick = async () => {
const currentDevices = await navigator.mediaDevices.enumerateDevices();
devices.innerHTML = '';
for (const device of currentDevices) {
if (device.kind != 'videoinput') continue;
const option = document.createElement('option');
option.text = device.label;
option.value = device.deviceId;
devices.appendChild(option);
}
};
document.querySelector('#start').onclick = async () => {
try {
resolutionName = resolution.value;
if (stream && streamResolution && streamResolution !=
resolutionName) {
closeStream();
}
if (!stream) {
const currentConstraints = constraints[resolutionName];
if (devices.value !== '') {
currentConstraints.video.deviceId = devices.value;
}
stream = await navigator.mediaDevices.getUserMedia(constraints[resolutionName]);
streamResolution = resolutionName;
await setStream(videoSource, videoSourceSize, videoSourceFps, stream);
await populateCameraControls(stream.getVideoTracks()[0]);
}
} catch (e) {
log(e);
}
};
document.querySelector('#stop').onclick = () => {
closeStream();
};
document.querySelector('#applyConstraints').onclick = async () => {
const controls = document.querySelectorAll('#cameraControls input, #cameraControls select');
const currentSettings = videoSource.srcObject.getVideoTracks()[0].getSettings();
for (const control of controls) {
if (currentSettings[control.id] == undefined || currentSettings[control.id] == control.value) {
continue;
}
const newConstraints = {advanced: []};
const value = {};
value[control.id] = control.value;
newConstraints.advanced.push(value);
newConstraints[control.id] = control.value;
await videoSource.srcObject.getVideoTracks()[0].applyConstraints(newConstraints);
}
/* log(JSON.stringify(newConstraints, 0, 2));
await videoSource.srcObject.getVideoTracks()[0].applyConstraints(newConstraints);*/
log(JSON.stringify(videoSource.srcObject.getVideoTracks()[0].getSettings(), 0, 2));
};
document.querySelector('#clearlog').onclick = () => {
div.innerHTML = '';
};
const setStream = async (video, videoSize, videoFps, stream) => {
video.srcObject = stream;
video.onloadedmetadata = (e) => {
videoSize.textContent = video.videoWidth + 'x' + video.videoHeight;
};
video.onresize = (e) => {
videoSize.textContent = video.videoWidth + 'x' + video.videoHeight;
};
intervals.push(window.setInterval(((fpsBox, track) => {
fpsBox.textContent = track.getSettings().frameRate.toFixed(
1);
}).bind(null, videoFps, stream.getTracks()[0]), 1000));
};
const populateCameraControls = async (track) => {
await sleep(500);
const capabilities = track.getCapabilities();
const settings = track.getSettings();
const blacklist = ['aspectRatio', 'deviceId', 'facingMode', 'frameRate', 'groupId', 'height', 'resizeMode', 'width'];
for (const controlName in capabilities) {
if (blacklist.includes(controlName)) continue;
cameraControl = document.createElement('label');
cameraControl.appendChild(document.createTextNode(controlName + ': '));
const control = capabilities[controlName];
let controlWidget = null;
if (control instanceof Array) {
controlWidget = document.createElement('select');
for (const controlValue of control) {
const option = document.createElement('option');
option.value = controlValue;
option.text = controlValue;
controlWidget.appendChild(option);
}
} else {
controlWidget = document.createElement('input');
controlWidget.type = 'range';
controlWidget.min = control.min;
controlWidget.max = control.max;
controlWidget.step = control.step;
}
controlWidget.id = controlName;
controlWidget.value = settings[controlName];
cameraControl.appendChild(controlWidget);
cameraControls.appendChild(cameraControl);
cameraControls.appendChild(document.createElement('br'));
}
};
const log = (msg) => div.innerHTML += '<pre>' + msg + '</pre>';
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
</script>
</body>
</html>