Skip to content

Commit 2013be6

Browse files
authored
Camera switching in a dropdown list (#247)
* Added camera switching in a dropdown * Added dropdown element for camera switching on old and new interface Added dropdown element on old interface
1 parent 4f29a95 commit 2013be6

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

dist/capture.dist.js

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,98 @@ $W.getUserMedia = function(options) {
265265
container.appendChild(video);
266266
video.autoplay = true;
267267
video.id = 'webcam-video'
268+
269+
var videoElement = document.getElementById('webcam-video');
270+
var videoSelect = document.querySelector('select#videoSource');
271+
var selectors = [videoSelect];
268272

269-
const successCallback = stream => {
273+
successCallback = stream => {
270274
$('#heightIndicator').show()
271275
$('#webcam-msg').hide()
272-
attachMediaStream(video, stream)
276+
277+
window.stream = stream;
278+
videoElement = attachMediaStream(videoElement, stream)
273279
if ($W.flipped == true) {
274280
$W.flipped = false; // <= turn it false because f_h() will toggle it. messy.
275281
$W.flip_horizontal();
276282
}
283+
return getVidDevices();
277284
};
278285

279286
const errorCallback = () => console.warn(error);
280287

281288
getUserMedia($W.defaultConstraints, successCallback, errorCallback);
289+
290+
gotVidDevices = (deviceInfos) => {
291+
let values = selectors.map(function(select) {
292+
return select.value;
293+
});
294+
295+
selectors.forEach(function(select) {
296+
while (select.firstChild) {
297+
select.removeChild(select.firstChild);
298+
}
299+
});
300+
for (let i = 0; i !== deviceInfos.length; ++i) {
301+
let deviceInfo = deviceInfos[i];
302+
let option = document.createElement('option');
303+
option.value = (deviceInfo.id || deviceInfo.deviceId);
304+
if (deviceInfo.kind === 'videoinput' || deviceInfo.kind === 'video') {
305+
console.log(deviceInfo.label);
306+
option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
307+
videoSelect.appendChild(option);
308+
}
309+
}
310+
311+
selectors.forEach(function(select, selectorIndex) {
312+
if (Array.prototype.slice.call(select.childNodes).some(function(n) {
313+
return n.value === values[selectorIndex];
314+
})) {
315+
select.value = values[selectorIndex];
316+
}
317+
});
318+
}
319+
320+
getVidDevices = () => {
321+
if (typeof Promise === 'undefined') {
322+
return MediaStreamTrack.getSources(gotVidDevices);
323+
} else {
324+
return navigator.mediaDevices.enumerateDevices()
325+
.then(gotVidDevices)
326+
.catch((error) => {
327+
console.error(error);
328+
});
329+
}
330+
}
331+
332+
getVidDevices();
333+
334+
start = () => {
335+
if (window.stream) {
336+
window.stream.getTracks().forEach(function(track) {
337+
track.stop(); //stopping the current video stream
338+
});
339+
}
340+
341+
var videoSource = videoSelect.value;
342+
var constraints = {
343+
video: {
344+
deviceId: videoSource ? {exact: videoSource} : undefined //Taking device ids as the video source
345+
}
346+
};
347+
348+
if (typeof Promise === 'undefined') {
349+
navigator.getUserMedia(constraints, successCallback, function(){});
350+
}
351+
else {
352+
navigator.mediaDevices.getUserMedia(constraints)
353+
.then(successCallback);
354+
}
355+
}
356+
357+
videoSelect.onchange = start; //repeating the process for source change
358+
359+
start();
282360
});
283361
};
284362

examples/capture/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@
174174
<a rel="tooltip" title="Flip video horizontally" class="btn btn-default btn-flip" onClick="$W.flip_horizontal()"><i class="fa fa-white fa-arrows-h"></i></a>
175175
<a rel="tooltip" title="Scale video" class="btn btn-default" onClick="$W.scale_h = parseFloat(prompt('Enter a horizontal scaling factor (default is 1):'))"><i class="fa fa-white fa-expand"></i></a>
176176
</div>
177+
178+
<div class="select" style="padding-top:5px;">
179+
<label for="videoSource">Camera source: </label><select id="videoSource"></select>
180+
</div>
177181

178182
<p><small><b>TOOLS</b></small></p>
179183
<div class="btn-group toolbar" style="margin-bottom:5px;">

examples/new-capture/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ <h1>
170170
<a rel="tooltip" title="" class="btn btn-default" onclick="$W.auto_detect_sample_row()" data-original-title="Auto select sample row"><i class="fa fa-white fa-arrows-v"></i> Auto-select Sample Row</a>
171171
<a rel="tooltip" title="Flip video horizontally" class="btn btn-default btn-flip" onClick="$W.flip_horizontal()"><i class="fa fa-white fa-arrows-h"></i> Flip image</a>
172172
<a rel="tooltip" title="Rotate video 90 &deg;" class="btn btn-default btn-rotate" onClick="$W.toggle_rotation()"><i class="fa fa-white fa-rotate-right"></i> Rotate</a>
173-
174173
</p>
174+
175+
<div class="select" style="padding-top:5px;">
176+
<label for="videoSource">Camera source: </label><select id="videoSource"></select>
177+
</div>
178+
175179
<p style="padding-top:5px;">
176180
Help <a href="http://publiclab.org/wiki/spectral-workbench-usage#Webcam+selection">selecting a camera</a>
177181
</p>

0 commit comments

Comments
 (0)