Skip to content

Commit 3d13c05

Browse files
author
Chad Hart
committed
mediaDevices.enumerateDevices() updates
1 parent 8930fda commit 3d13c05

File tree

1 file changed

+57
-88
lines changed

1 file changed

+57
-88
lines changed

js/resolutionScan.js

Lines changed: 57 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,39 @@
66
//Global variables
77
var camVideo = $('#camera')[0], //where we will put & test our video output
88
deviceList = $('#devices')[0], //device list dropdown
9-
devices, //getSources object to hold various camera options
9+
devices = [], //getSources object to hold various camera options
1010
localStream,
1111
selectedCamera = [], //used to hold a camera's ID and other parameters
1212
tests, //holder for our test results
1313
r = 0, //used for iterating through the array
1414
camNum = 0, //used for iterating through number of camera
1515
scanning = false; //variable to show if we are in the middle of a scan
1616

17-
/*
18-
Get and list camera devices using MediaStreamTrack.getSources
19-
Inspiration 1: https://code.google.com/p/webrtc/source/browse/stable/samples/js/demos/html/device-switch.html
20-
Inspiration 2: https://raw.githubusercontent.com/muaz-khan/WebRTC-Experiment/master/demos/MediaStreamTrack.getSources.html
21-
*/
22-
function listVideoDevices(){
2317

24-
var videoDevices = [];
25-
if(!MediaStreamTrack.getSources) //note for the future: getSources changing to getMediaDevices
26-
{
27-
console.log("No media stream track enumeration");
28-
return;
29-
}
3018

31-
MediaStreamTrack.getSources(function (deviceOptions){
32-
if (deviceOptions){
33-
$('#selectArea').show();
34-
var camNum = 1; //count the number of cameras for id if device label is unavailable
35-
for (var x=0; x<deviceOptions.length; x++){
36-
if (deviceOptions[x].kind == 'video') {
37-
38-
//define our own Options so we can modify it if needed
39-
//needed for http & when label attribute is empty & we have to assign one
40-
var camOption = {
41-
label: deviceOptions[x].label,
42-
id: deviceOptions[x].id
43-
};
44-
45-
var listOption = document.createElement("option");
46-
47-
//label should already exists if previous approval on HTTPS site
48-
if (deviceOptions[x].label) {
49-
listOption.text = deviceOptions[x].label;
50-
}
51-
else {
52-
//Add a label if none exists (happens before first user capture approval)
53-
camOption.label = listOption.text = "Camera " + camNum;
54-
camNum++;
55-
}
56-
deviceList.add(listOption); //update the pull down list
57-
videoDevices.push(camOption); //only add video devices
58-
console.log("Camera found: " + JSON.stringify(deviceOptions[x]));
5919

60-
//selectedCamera = devices[0]; //set the default camera in case there is no user selection
61-
}
62-
}
20+
function gotDevices(deviceInfos) {
21+
$('#selectArea').show();
22+
for (var i = 0; i !== deviceInfos.length; ++i) {
23+
var deviceInfo = deviceInfos[i];
24+
var option = document.createElement('option');
25+
option.value = deviceInfo.deviceId;
26+
if (deviceInfo.kind === 'videoinput') {
27+
option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
28+
devices.push(option);
29+
deviceList.add(option);
6330
}
64-
else {
65-
console.log("No device sources found");
66-
}
67-
});
31+
}
32+
}
6833

69-
return videoDevices;
34+
function errorCallback(error) {
35+
console.log('navigator.getUserMedia error: ', error);
7036
}
7137

38+
navigator.mediaDevices.enumerateDevices()
39+
.then(gotDevices)
40+
.catch(errorCallback);
41+
7242
//find & list camera devices on load
7343
$(document).ready(function(){
7444

@@ -86,12 +56,12 @@ $(document).ready(function(){
8656
document.location.href = "https:" + document.location.href.substring(document.location.protocol.length);
8757
}
8858

89-
devices = listVideoDevices();
59+
//devices = listVideoDevices();
9060

9161
//Show text of what res's are used on QuickScan
9262
var quickText = "Sizes:";
93-
for(var l in quickScan){
94-
quickText += " " + quickScan[l].label
63+
for(var q=0; q < quickScan.length; q++){
64+
quickText += " " + quickScan[q].label
9565
}
9666
$('#quickLabel').text(quickText);
9767

@@ -107,8 +77,8 @@ $('button').click(function(){
10777
}
10878
//setup for a full scan and build scan object based on inputs
10979
else if (this.innerHTML == "Full Scan"){
110-
var highRes = $('#hiRes').val();
111-
var lowRes = $('#loRes').val();
80+
var highRes = $('#hiRes')[0].val();
81+
var lowRes = $('#loRes')[0].val();
11282
console.log("Full scan from " + lowRes + " to " + highRes);
11383
tests = createAllResolutions(parseInt(lowRes), parseInt(highRes) );
11484
}
@@ -120,13 +90,19 @@ $('button').click(function(){
12090

12191
//if there is device enumeration
12292
if (devices){
93+
12394
//run through the deviceList to see what is selected
12495
for (var deviceCount=0, d=0; d<deviceList.length; d++){
12596
if(deviceList[d].selected){
12697
//if it is selected, check the label against the getSources array to select the proper ID
127-
for(z=0; z<devices.length; z++) {
128-
if (devices[z].label == deviceList[d].value) {
129-
selectedCamera[deviceCount] = devices[z];
98+
for(var z=0; z<devices.length; z++) {
99+
if (devices[z].value == deviceList[d].value) {
100+
101+
//just pass along the id and label
102+
var camera = {};
103+
camera.id = devices[z].value;
104+
camera.label = devices[z].text;
105+
selectedCamera[deviceCount] = camera;
130106
console.log(selectedCamera[deviceCount].label + "[" + selectedCamera[deviceCount].id + "] selected");
131107
deviceCount++;
132108
}
@@ -136,10 +112,16 @@ $('button').click(function(){
136112

137113
//Make sure there is at least 1 camera selected before starting
138114
if (selectedCamera[0]) {
139-
gum(tests[r], selectedCamera[camNum].id);
115+
gum(tests[r], selectedCamera[0]);
140116
}
141117
else{
142-
alert("You must select a camera first");
118+
console.log("No camera selected. Defaulting to " + deviceList[0].text);
119+
//$('button').prop("disabled",false);
120+
121+
122+
selectedCamera[0] = {id: deviceList[0].value, label: deviceList[0].text};
123+
gum(tests[r], selectedCamera[0]);
124+
143125
}
144126
}
145127
//if no device enumeration don't pass a Camera ID
@@ -151,9 +133,8 @@ $('button').click(function(){
151133
});
152134

153135
//calls getUserMedia for a given camera and constraints
154-
function gum(candidate, camId) {
155-
156-
console.log("trying " + candidate.label);
136+
function gum(candidate, device) {
137+
console.log("trying " + candidate.label + " on " + device.label);
157138

158139
//Kill any running streams;
159140
if (localStream) {
@@ -171,7 +152,7 @@ function gum(candidate, camId) {
171152
audio: false,
172153
video: {
173154
mandatory: {
174-
sourceId: camId,
155+
sourceId: device.id,
175156
minWidth: candidate.width,
176157
minHeight: candidate.height,
177158
maxWidth: candidate.width,
@@ -192,9 +173,6 @@ function gum(candidate, camId) {
192173
}
193174
});
194175

195-
196-
//getUserMedia(constraints, onStream, onFail); //getUserMedia call
197-
198176
function gotStream(stream) {
199177

200178
//change the video dimensions
@@ -208,16 +186,6 @@ function gum(candidate, camId) {
208186

209187
}
210188
}
211-
/*
212-
function onFail(error) {
213-
console.log('Video error!', error);
214-
215-
if (scanning) {
216-
//console.log("Stream dimensions for " + candidates[r].label + ": " + camVideo.videoWidth + "x" + camVideo.videoHeight);
217-
captureResults("fail: " + error.name);
218-
}
219-
}
220-
}*/
221189

222190

223191
//Attach to play event
@@ -235,7 +203,7 @@ $('#camera').on("play", function(){
235203
captureResults("pass");
236204
}
237205
}
238-
//If not, wait another 50ms
206+
//If not, wait another 100ms
239207
}, 100);
240208
});
241209

@@ -275,12 +243,12 @@ function captureResults(status){
275243
r++;
276244
//go to the next tests
277245
if (r < tests.length){
278-
gum(tests[r], selectedCamera[camNum].id);
246+
gum(tests[r], selectedCamera[camNum]);
279247
}
280248
else if (camNum < selectedCamera.length -1){ //move on to the next camera
281249
camNum++;
282250
r=0;
283-
gum(tests[r], selectedCamera[camNum].id)
251+
gum(tests[r], selectedCamera[camNum])
284252
}
285253
else{ //finish up
286254
$('#camera').off("play"); //turn off the event handler
@@ -311,28 +279,29 @@ function clickRows(){
311279
r = $(this).find("td").eq(8).html();
312280

313281
//lookup the device id based on the row label
314-
for(z=0; z<devices.length; z++) {
315-
if(devices[z].label== $(this).find("td").eq(1).html()){
316-
var thisCamId = devices[z].id;
282+
for(z=0; z<selectedCamera.length; z++) {
283+
if(selectedCamera[z].label== $(this).find("td").eq(1).html()){
284+
var thisCam = selectedCamera[z]; //devices[z].value;
285+
console.log(this)
317286
}
318287
}
319288

320-
console.log("table click! clicked on " + tests[r].label );
321-
gum(tests[r], thisCamId);
289+
console.log("table click! clicked on " + thisCam + ":" + tests[r].label );
290+
gum(tests[r], thisCam);
322291
})
323292
}
324293

325294

326295
//Variables to use in the quick scan
327296
const quickScan = [
328297
{
329-
"label": "4K",
298+
"label": "4K(UHD)",
330299
"width" : 3840,
331300
"height": 2160,
332301
"ratio": "16:9"
333302
},
334303
{
335-
"label": "1080p",
304+
"label": "1080p(FHD)",
336305
"width": 1920,
337306
"height": 1080,
338307
"ratio": "16:9"
@@ -344,7 +313,7 @@ const quickScan = [
344313
"ratio": "4:3"
345314
},
346315
{
347-
"label": "720p",
316+
"label": "720p(HD)",
348317
"width": 1280,
349318
"height": 720,
350319
"ratio": "16:9"
@@ -362,7 +331,7 @@ const quickScan = [
362331
"ratio": "4:3"
363332
},
364333
{
365-
"label": "360p",
334+
"label": "360p(nHD)",
366335
"width": 640,
367336
"height": 360,
368337
"ratio": "16:9"

0 commit comments

Comments
 (0)