forked from webrtcHacks/WebRTC-Camera-Resolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolutionScan.js
470 lines (393 loc) · 13.8 KB
/
resolutionScan.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/**
* Main js file for WebRTC-Camera-Resolution finder
* Created by chad on 7/19/2014.
* Modified January 1, 2016
*/
'use strict';
//Global variables
let video = $('#video')[0], //where we will put & test our video output
deviceList = $('#devices')[0], //device list dropdown
devices = [], //getSources object to hold various camera options
selectedCamera = [], //used to hold a camera's ID and other parameters
tests, //holder for our test results
r = 0, //used for iterating through the array
camNum = 0, //used for iterating through number of camera
scanning = false; //variable to show if we are in the middle of a scan
function gotDevices(deviceInfos) {
$('#selectArea').show();
let camcount = 1; //used for labeling if the device label is not enumerated
for (let i = 0; i !== deviceInfos.length; ++i) {
let deviceInfo = deviceInfos[i];
let option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || 'camera ' + camcount;
devices.push(option);
deviceList.add(option);
camcount++;
}
}
}
function errorCallback(error) {
console.log('navigator.getUserMedia error: ', error);
}
//find & list camera devices on load
$(document).ready(() => {
console.log("adapter.js says this is " + adapter.browserDetails.browser + " " + adapter.browserDetails.version);
if (!navigator.getUserMedia) {
alert('You need a browser that supports WebRTC');
$("div").hide();
return;
}
//Call gUM early to force user gesture and allow device enumeration
navigator.mediaDevices.getUserMedia({audio: false, video: true})
.then((mediaStream) => {
window.stream = mediaStream; // make globally available
video.srcObject = mediaStream;
//Now enumerate devices
navigator.mediaDevices.enumerateDevices()
.then(gotDevices)
.catch(errorCallback);
})
.catch((error) => {
console.error('getUserMedia error!', error);
});
//Localhost unsecure http connections are allowed
if (document.location.hostname !== "localhost") {
//check if the user is using http vs. https & redirect to https if needed
if (document.location.protocol !== "https:") {
$(document).html("This doesn't work well on http. Redirecting to https");
console.log("redirecting to https");
document.location.href = "https:" + document.location.href.substring(document.location.protocol.length);
}
}
//Show text of what res's are used on QuickScan
let quickText = "Sizes:";
for (let q = 0; q < quickScan.length; q++) {
quickText += " " + quickScan[q].label
}
$('#quickLabel').text(quickText);
});
//Start scan by controlling the quick and full scan buttons
$('button').click(function(){
//setup for a quick scan using the hand-built quickScan object
if (this.innerHTML === "Quick Scan") {
console.log("Quick scan");
tests = quickScan;
}
//setup for a full scan and build scan object based on inputs
else if (this.innerHTML === "Full Scan") {
let highRes = $('#hiRes').val();
let lowRes = $('#loRes').val();
console.log("Full scan from " + lowRes + " to " + highRes);
tests = createAllResolutions(parseInt(lowRes), parseInt(highRes));
}
else {
return
}
scanning = true;
$('button').prop("disabled", true);
$('table').show();
$('#jump').show();
//if there is device enumeration
if (devices) {
//run through the deviceList to see what is selected
for (let deviceCount = 0, d = 0; d < deviceList.length; d++) {
if (deviceList[d].selected) {
//if it is selected, check the label against the getSources array to select the proper ID
for (let z = 0; z < devices.length; z++) {
if (devices[z].value === deviceList[d].value) {
//just pass along the id and label
let camera = {};
camera.id = devices[z].value;
camera.label = devices[z].text;
selectedCamera[deviceCount] = camera;
console.log(selectedCamera[deviceCount].label + "[" + selectedCamera[deviceCount].id + "] selected");
deviceCount++;
}
}
}
}
//Make sure there is at least 1 camera selected before starting
if (selectedCamera[0]) {
gum(tests[r], selectedCamera[0]);
}
else {
console.log("No camera selected. Defaulting to " + deviceList[0].text);
//$('button').prop("disabled",false);
selectedCamera[0] = {id: deviceList[0].value, label: deviceList[0].text};
gum(tests[r], selectedCamera[0]);
}
}
//if no device enumeration don't pass a Camera ID
else {
selectedCamera[0] = {label: "Unknown"};
gum(tests[r]);
}
});
//calls getUserMedia for a given camera and constraints
function gum(candidate, device) {
console.log("trying " + candidate.label + " on " + device.label);
//Kill any running streams;
if (stream) {
stream.getTracks().forEach((track) => {
track.stop();
});
}
//create constraints object
let constraints = {
audio: false,
video: {
deviceId: device.id ? {exact: device.id} : undefined,
width: {exact: candidate.width}, //new syntax
height: {exact: candidate.height} //new syntax
}
};
setTimeout(() => {
navigator.mediaDevices.getUserMedia(constraints)
.then(gotStream)
.catch((error) => {
console.log('getUserMedia error!', error);
if (scanning) {
captureResults("fail: " + error.name);
}
});
}, (stream ? 200 : 0)); //official examples had this at 200
function gotStream(mediaStream) {
//change the video dimensions
console.log("Display size for " + candidate.label + ": " + candidate.width + "x" + candidate.height);
video.width = candidate.width;
video.height = candidate.height;
window.stream = mediaStream; // make globally available
video.srcObject = mediaStream;
}
}
function displayVideoDimensions() {
//This should only happen during setup
if (tests === undefined)
return;
//Wait for dimensions if they don't show right away
if (!video.videoWidth) {
setTimeout(displayVideoDimensions, 500); //was 500
}
if (video.videoWidth * video.videoHeight > 0) {
if (tests[r].width + "x" + tests[r].height !== video.videoWidth + "x" + video.videoHeight) {
captureResults("fail: mismatch");
}
else {
captureResults("pass");
}
}
}
video.onloadedmetadata = displayVideoDimensions;
//Save results to the candidate so
function captureResults(status) {
console.log("Stream dimensions for " + tests[r].label + ": " + video.videoWidth + "x" + video.videoHeight);
if (!scanning) //exit if scan is not active
return;
tests[r].status = status;
tests[r].streamWidth = video.videoWidth;
tests[r].streamHeight = video.videoHeight;
let row = $('table#results')[0].insertRow(-1);
let browserVer = row.insertCell(0);
let deviceName = row.insertCell(1);
let label = row.insertCell(2);
let ratio = row.insertCell(3);
let ask = row.insertCell(4);
let actual = row.insertCell(5);
let statusCell = row.insertCell(6);
let deviceIndex = row.insertCell(7);
let resIndex = row.insertCell(8);
//don't show these
deviceIndex.style.display = "none";
resIndex.style.display = "none";
deviceIndex.class = "hidden";
resIndex.class = "hidden";
browserVer.innerHTML = adapter.browserDetails.browser + " " + adapter.browserDetails.version;
deviceName.innerHTML = selectedCamera[camNum].label;
label.innerHTML = tests[r].label;
ratio.innerHTML = tests[r].ratio;
ask.innerHTML = tests[r].width + "x" + tests[r].height;
actual.innerHTML = tests[r].streamWidth + "x" + tests[r].streamHeight;
statusCell.innerHTML = tests[r].status;
deviceIndex.innerHTML = camNum; //used for debugging
resIndex.innerHTML = r; //used for debugging
r++;
//go to the next tests
if (r < tests.length) {
gum(tests[r], selectedCamera[camNum]);
}
else if (camNum < selectedCamera.length - 1) { //move on to the next camera
camNum++;
r = 0;
gum(tests[r], selectedCamera[camNum])
}
else { //finish up
video.removeEventListener("onloadedmetadata", displayVideoDimensions); //turn off the event handler
$('button').off("click"); //turn the generic button handler off
scanning = false;
$(".pfin").show();
$('#csvOut').click(function() {
exportTableToCSV.apply(this, [$('#results'), 'gumResTestExport.csv']);
});
//allow to click on a row to test (only works with device Enumeration
if (devices) {
clickRows();
}
}
}
//allow clicking on a row to see the camera capture
//To do: figure out why this doesn't work in Firefox
function clickRows() {
$('tr').click(function() {
r = $(this).find("td").eq(8).html();
//lookup the device id based on the row label
for (let z = 0; z < selectedCamera.length; z++) {
if (selectedCamera[z].label === $(this).find("td").eq(1).html()) {
var thisCam = selectedCamera[z]; //devices[z].value;
console.log(this)
}
}
console.log("table click! clicked on " + thisCam + ":" + tests[r].label);
gum(tests[r], thisCam);
})
}
//Variables to use in the quick scan
const quickScan = [
{
"label": "4K(UHD)",
"width": 3840,
"height": 2160,
"ratio": "16:9"
},
{
"label": "1080p(FHD)",
"width": 1920,
"height": 1080,
"ratio": "16:9"
},
{
"label": "UXGA",
"width": 1600,
"height": 1200,
"ratio": "4:3"
},
{
"label": "720p(HD)",
"width": 1280,
"height": 720,
"ratio": "16:9"
},
{
"label": "SVGA",
"width": 800,
"height": 600,
"ratio": "4:3"
},
{
"label": "VGA",
"width": 640,
"height": 480,
"ratio": "4:3"
},
{
"label": "360p(nHD)",
"width": 640,
"height": 360,
"ratio": "16:9"
},
{
"label": "CIF",
"width": 352,
"height": 288,
"ratio": "4:3"
},
{
"label": "QVGA",
"width": 320,
"height": 240,
"ratio": "4:3"
},
{
"label": "QCIF",
"width": 176,
"height": 144,
"ratio": "4:3"
},
{
"label": "QQVGA",
"width": 160,
"height": 120,
"ratio": "4:3"
}
];
//creates an object with all HD & SD video ratios between two heights
function createAllResolutions(minHeight, maxHeight) {
const ratioHD = 16 / 9;
const ratioSD = 4 / 3;
let resolutions = [],
res;
for (let y = maxHeight; y >= minHeight; y--) {
//HD
res = {
"label": (y * ratioHD).toFixed() + "x" + y,
"width": parseInt((y * ratioHD).toFixed()), //this was returning a string
"height": y,
"ratio": "16:9"
};
resolutions.push(res);
//SD
res = {
"label": (y * ratioSD).toFixed() + "x" + y,
"width": parseInt((y * ratioSD).toFixed()),
"height": y,
"ratio": "4:3"
};
resolutions.push(res);
//square
//noinspection JSSuspiciousNameCombination
res = {
"label": y + "x" + y,
"width": y,
"height": y,
"ratio": "1:1"
};
resolutions.push(res);
}
console.log("resolutions length: " + resolutions.length);
return resolutions;
}
/*
Export results table to a CSV file in new window for download
source: http://jsfiddle.net/terryyounghk/KPEGU/
*/
function exportTableToCSV($table, filename) {
let $rows = $table.find('tr:has(th), tr:has(td)'),
// Temporary delimiter characters unlikely to be typed by keyboard
// This is to avoid accidentally splitting the actual contents
tmpColDelim = String.fromCharCode(11), // vertical tab character
tmpRowDelim = String.fromCharCode(0), // null character
// actual delimiter characters for CSV format
colDelim = '","',
rowDelim = '"\r\n"',
// Grab text from table into CSV formatted string
csv = '"' + $rows.map((i, row) => {
let $row = $(row),
$cols = $row.find('th, td');
return $cols.map((j, col) => {
let $col = $(col),
text = $col.text();
return text.replace(/"/g, '""'); // escape double quotes
}).get().join(tmpColDelim);
}).get().join(tmpRowDelim)
.split(tmpRowDelim).join(rowDelim)
.split(tmpColDelim).join(colDelim) + '"',
// Data URI
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
$(this)
.attr({
'download': filename,
'href': csvData,
'target': '_blank'
});
}