Guys i have been trying to get access to both front and rear camera for chrome using webrtc and its perfectly working, i'm able to switch between both front and rear cameras. However, by default chrome picks up the front camera, is there any way i can make chrome pick up the rear camera when the browser opens up?
var videoSelect = document.querySelector('select#videoSource');
var selectors = [videoSelect];
function gotDevices(deviceInfos) {
// Handles being called several times to update labels. Preserve values.
var values = selectors.map(function(select) {
return select.value;
});
selectors.forEach(function(select) {
while (select.firstChild) {
select.removeChild(select.firstChild);
}
});
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
if (deviceInfo.kind === 'videoinput') {
option.text = deviceInfo.label || 'camera ' + (videoSelect.length + 1);
videoSelect.appendChild(option);
} else {
console.log('Some other kind of source/device: ', deviceInfo);
}
}
selectors.forEach(function(select, selectorIndex) {
if (Array.prototype.slice.call(select.childNodes).some(function(n) {
return n.value === values[selectorIndex];
})) {
select.value = values[selectorIndex];
}
});
}
navigator.mediaDevices.enumerateDevices().then(gotDevices).catch(handleError);
I tried accessing the array in descending order to get the back camera first, but did not help
for (var i = deviceInfos.length - 1; i >= 0; i--) {
var deviceInfo = deviceInfos[i];
var option = document.createElement('option');
option.value = deviceInfo.deviceId;
....... //code follows
Update: I also tried adding these lines into the above code but again the same response, opens up the front cam by default
for (var i = 0; i !== deviceInfos.length; ++i) {
var deviceInfo = deviceInfos[i];
if (deviceInfo.kind === 'videoinput' && deviceInfo.facing == "environment") {
videoSourceId = deviceInfo.id;
} else {
console.log('Some other kind of source/device: ', deviceInfo);
}
}
//other code
var constraints = {
video: { optional: [{sourceId: videoSourceId}] }
};
navigator.mediaDevices.getUserMedia(constraints).
then(gotStream).then(gotDevices).catch(handleError);
}
PS: I tried using facingMode: "environment" in the other code, but is there any scope for that here ?