6
6
//Global variables
7
7
var camVideo = $ ( '#camera' ) [ 0 ] , //where we will put & test our video output
8
8
deviceList = $ ( '#devices' ) [ 0 ] , //device list dropdown
9
- devices , //getSources object to hold various camera options
9
+ devices = [ ] , //getSources object to hold various camera options
10
10
localStream ,
11
11
selectedCamera = [ ] , //used to hold a camera's ID and other parameters
12
12
tests , //holder for our test results
13
13
r = 0 , //used for iterating through the array
14
14
camNum = 0 , //used for iterating through number of camera
15
15
scanning = false ; //variable to show if we are in the middle of a scan
16
16
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 ( ) {
23
17
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
- }
30
18
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 ] ) ) ;
59
19
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 ) ;
63
30
}
64
- else {
65
- console . log ( "No device sources found" ) ;
66
- }
67
- } ) ;
31
+ }
32
+ }
68
33
69
- return videoDevices ;
34
+ function errorCallback ( error ) {
35
+ console . log ( 'navigator.getUserMedia error: ' , error ) ;
70
36
}
71
37
38
+ navigator . mediaDevices . enumerateDevices ( )
39
+ . then ( gotDevices )
40
+ . catch ( errorCallback ) ;
41
+
72
42
//find & list camera devices on load
73
43
$ ( document ) . ready ( function ( ) {
74
44
@@ -86,12 +56,12 @@ $(document).ready(function(){
86
56
document . location . href = "https:" + document . location . href . substring ( document . location . protocol . length ) ;
87
57
}
88
58
89
- devices = listVideoDevices ( ) ;
59
+ // devices = listVideoDevices();
90
60
91
61
//Show text of what res's are used on QuickScan
92
62
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
95
65
}
96
66
$ ( '#quickLabel' ) . text ( quickText ) ;
97
67
@@ -107,8 +77,8 @@ $('button').click(function(){
107
77
}
108
78
//setup for a full scan and build scan object based on inputs
109
79
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 ( ) ;
112
82
console . log ( "Full scan from " + lowRes + " to " + highRes ) ;
113
83
tests = createAllResolutions ( parseInt ( lowRes ) , parseInt ( highRes ) ) ;
114
84
}
@@ -120,13 +90,19 @@ $('button').click(function(){
120
90
121
91
//if there is device enumeration
122
92
if ( devices ) {
93
+
123
94
//run through the deviceList to see what is selected
124
95
for ( var deviceCount = 0 , d = 0 ; d < deviceList . length ; d ++ ) {
125
96
if ( deviceList [ d ] . selected ) {
126
97
//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 ;
130
106
console . log ( selectedCamera [ deviceCount ] . label + "[" + selectedCamera [ deviceCount ] . id + "] selected" ) ;
131
107
deviceCount ++ ;
132
108
}
@@ -136,10 +112,16 @@ $('button').click(function(){
136
112
137
113
//Make sure there is at least 1 camera selected before starting
138
114
if ( selectedCamera [ 0 ] ) {
139
- gum ( tests [ r ] , selectedCamera [ camNum ] . id ) ;
115
+ gum ( tests [ r ] , selectedCamera [ 0 ] ) ;
140
116
}
141
117
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
+
143
125
}
144
126
}
145
127
//if no device enumeration don't pass a Camera ID
@@ -151,9 +133,8 @@ $('button').click(function(){
151
133
} ) ;
152
134
153
135
//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 ) ;
157
138
158
139
//Kill any running streams;
159
140
if ( localStream ) {
@@ -171,7 +152,7 @@ function gum(candidate, camId) {
171
152
audio : false ,
172
153
video : {
173
154
mandatory : {
174
- sourceId : camId ,
155
+ sourceId : device . id ,
175
156
minWidth : candidate . width ,
176
157
minHeight : candidate . height ,
177
158
maxWidth : candidate . width ,
@@ -192,9 +173,6 @@ function gum(candidate, camId) {
192
173
}
193
174
} ) ;
194
175
195
-
196
- //getUserMedia(constraints, onStream, onFail); //getUserMedia call
197
-
198
176
function gotStream ( stream ) {
199
177
200
178
//change the video dimensions
@@ -208,16 +186,6 @@ function gum(candidate, camId) {
208
186
209
187
}
210
188
}
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
- }*/
221
189
222
190
223
191
//Attach to play event
@@ -235,7 +203,7 @@ $('#camera').on("play", function(){
235
203
captureResults ( "pass" ) ;
236
204
}
237
205
}
238
- //If not, wait another 50ms
206
+ //If not, wait another 100ms
239
207
} , 100 ) ;
240
208
} ) ;
241
209
@@ -275,12 +243,12 @@ function captureResults(status){
275
243
r ++ ;
276
244
//go to the next tests
277
245
if ( r < tests . length ) {
278
- gum ( tests [ r ] , selectedCamera [ camNum ] . id ) ;
246
+ gum ( tests [ r ] , selectedCamera [ camNum ] ) ;
279
247
}
280
248
else if ( camNum < selectedCamera . length - 1 ) { //move on to the next camera
281
249
camNum ++ ;
282
250
r = 0 ;
283
- gum ( tests [ r ] , selectedCamera [ camNum ] . id )
251
+ gum ( tests [ r ] , selectedCamera [ camNum ] )
284
252
}
285
253
else { //finish up
286
254
$ ( '#camera' ) . off ( "play" ) ; //turn off the event handler
@@ -311,28 +279,29 @@ function clickRows(){
311
279
r = $ ( this ) . find ( "td" ) . eq ( 8 ) . html ( ) ;
312
280
313
281
//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 )
317
286
}
318
287
}
319
288
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 ) ;
322
291
} )
323
292
}
324
293
325
294
326
295
//Variables to use in the quick scan
327
296
const quickScan = [
328
297
{
329
- "label" : "4K" ,
298
+ "label" : "4K(UHD) " ,
330
299
"width" : 3840 ,
331
300
"height" : 2160 ,
332
301
"ratio" : "16:9"
333
302
} ,
334
303
{
335
- "label" : "1080p" ,
304
+ "label" : "1080p(FHD) " ,
336
305
"width" : 1920 ,
337
306
"height" : 1080 ,
338
307
"ratio" : "16:9"
@@ -344,7 +313,7 @@ const quickScan = [
344
313
"ratio" : "4:3"
345
314
} ,
346
315
{
347
- "label" : "720p" ,
316
+ "label" : "720p(HD) " ,
348
317
"width" : 1280 ,
349
318
"height" : 720 ,
350
319
"ratio" : "16:9"
@@ -362,7 +331,7 @@ const quickScan = [
362
331
"ratio" : "4:3"
363
332
} ,
364
333
{
365
- "label" : "360p" ,
334
+ "label" : "360p(nHD) " ,
366
335
"width" : 640 ,
367
336
"height" : 360 ,
368
337
"ratio" : "16:9"
0 commit comments