Selecting camera based on facingMode; Relates to #128

pull/131/head
Christoph Oberhofer 9 years ago
parent 2c45929800
commit 473f4366a5

@ -97,9 +97,9 @@ $(function() {
constraints: function(value){ constraints: function(value){
var values = value.split('x'); var values = value.split('x');
return { return {
width: parseInt(values[0]), width: {min: parseInt(values[0])},
height: parseInt(values[1]) height: {min: parseInt(values[1])}
} };
} }
}, },
numOfWorkers: function(value) { numOfWorkers: function(value) {
@ -128,9 +128,10 @@ $(function() {
inputStream: { inputStream: {
type : "LiveStream", type : "LiveStream",
constraints: { constraints: {
width: 640, width: {min: 640},
height: 480, height: {min: 480},
facingMode: "environment" facingMode: "environment",
aspectRatio: {min: 1, max: 2}
} }
}, },
locator: { locator: {

@ -1,4 +1,9 @@
import {merge, pick} from 'lodash'; import {omit, pick} from 'lodash';
const facingMatching = {
"user": /front/i,
"environment": /back/i
};
var streamRef; var streamRef;
@ -68,16 +73,50 @@ function deprecatedConstraints(videoConstraints) {
return normalized; return normalized;
} }
function pickDevice(constraints) {
const desiredFacing = constraints.video.facingMode,
facingMatch = facingMatching[desiredFacing];
if (!facingMatch) {
return Promise.resolve(constraints);
}
return navigator.mediaDevices.enumerateDevices()
.then(devices => {
const selectedDeviceId = devices
.filter(device => device.kind === 'videoinput' && facingMatch.test(device.label))
.map(facingDevice => facingDevice.deviceId)[0];
if (selectedDeviceId) {
constraints = {
...constraints,
video: {
...omit(constraints.video, ["facingMode"]),
deviceId: selectedDeviceId,
}
};
}
return Promise.resolve(constraints);
});
}
function pickConstraints(videoConstraints) { function pickConstraints(videoConstraints) {
return { const normalizedConstraints = {
audio: false, audio: false,
video: deprecatedConstraints(videoConstraints) video: deprecatedConstraints(videoConstraints)
}; };
if (!normalizedConstraints.video.deviceId) {
if (typeof normalizedConstraints.video.facingMode === 'string'
&& normalizedConstraints.video.facingMode.length > 0) {
return pickDevice(normalizedConstraints);
}
}
return Promise.resolve(normalizedConstraints);
} }
export default { export default {
request: function(video, videoConstraints) { request: function(video, videoConstraints) {
return initCamera(video, pickConstraints(videoConstraints)); return pickConstraints(videoConstraints)
.then(initCamera.bind(null, video));
}, },
release: function() { release: function() {
var tracks = streamRef && streamRef.getVideoTracks(); var tracks = streamRef && streamRef.getVideoTracks();

Loading…
Cancel
Save