Skip to content

Commit 8930fda

Browse files
author
Chad Hart
committed
updated to getUserMedia promises
1 parent 3202e7e commit 8930fda

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea
1+
.idea
2+
.gitignore

js/resolutionScan.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var camVideo = $('#camera')[0], //where we will put & test our video output
88
deviceList = $('#devices')[0], //device list dropdown
99
devices, //getSources object to hold various camera options
10+
localStream,
1011
selectedCamera = [], //used to hold a camera's ID and other parameters
1112
tests, //holder for our test results
1213
r = 0, //used for iterating through the array
@@ -155,41 +156,59 @@ function gum(candidate, camId) {
155156
console.log("trying " + candidate.label);
156157

157158
//Kill any running streams;
158-
if (!!window.stream){
159-
camVideo.src = null;
160-
window.stream.stop();
159+
if (localStream) {
160+
localStream.getTracks().forEach(function (track) {
161+
track.stop();
162+
});
163+
var videoTracks = localStream.getVideoTracks();
164+
for (var i = 0; i !== videoTracks.length; ++i) {
165+
videoTracks[i].stop();
166+
}
161167
}
162168

163169
//create constraints object
164170
var constraints = {
165-
audio: false,
166-
video: {
167-
mandatory: {
168-
sourceId: camId,
169-
minWidth: candidate.width,
170-
minHeight: candidate.height,
171-
maxWidth: candidate.width,
172-
maxHeight: candidate.height
171+
audio: false,
172+
video: {
173+
mandatory: {
174+
sourceId: camId,
175+
minWidth: candidate.width,
176+
minHeight: candidate.height,
177+
maxWidth: candidate.width,
178+
maxHeight: candidate.height
173179

174-
}
175180
}
176-
};
181+
}
182+
};
183+
184+
navigator.mediaDevices.getUserMedia(constraints)
185+
.then(gotStream)
186+
.catch(function (error) {
187+
console.log('getUserMedia error!', error);
188+
189+
if (scanning) {
190+
//console.log("Stream dimensions for " + candidates[r].label + ": " + camVideo.videoWidth + "x" + camVideo.videoHeight);
191+
captureResults("fail: " + error.name);
192+
}
193+
});
177194

178-
getUserMedia(constraints, onStream, onFail); //getUserMedia call
179195

180-
function onStream(stream) {
196+
//getUserMedia(constraints, onStream, onFail); //getUserMedia call
197+
198+
function gotStream(stream) {
181199

182200
//change the video dimensions
183201
console.log("Display size for " + candidate.label + ": " + candidate.width + "x" + candidate.height);
184202
camVideo.width = candidate.width;
185203
camVideo.height = candidate.height;
186204

187-
window.stream = stream; // stream available to console
205+
localstream = stream; // stream available to console
188206
camVideo.src = window.URL.createObjectURL(stream);
189207
camVideo.play();
190208

191209
}
192-
210+
}
211+
/*
193212
function onFail(error) {
194213
console.log('Video error!', error);
195214
@@ -198,7 +217,7 @@ function gum(candidate, camId) {
198217
captureResults("fail: " + error.name);
199218
}
200219
}
201-
}
220+
}*/
202221

203222

204223
//Attach to play event

0 commit comments

Comments
 (0)