Skip to content

Commit bb4eee4

Browse files
authored
Merge pull request #32 from phillipj/use-last-device
Allow custom device id chooser function to be provided as prop
2 parents 9b10f58 + 8d94b6f commit bb4eee4

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ Type: `string`, Optional
123123

124124
ClassName for the container element.
125125

126+
**chooseDeviceId**
127+
128+
Type: `function`, Optional, Arguments: (1) video devices matching `facingMode`, (2) all video devices
129+
130+
Called when choosing which device to use for scanning. By default chooses the first video device matching `facingMode`, if no devices match the first video device found is choosen.
131+
126132
## Dev
127133

128134
### Install dependencies

lib/getDeviceId.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
var _require = require('./errors'),
44
NoVideoInputDevicesError = _require.NoVideoInputDevicesError;
55

6+
function defaultDeviceIdChooser(filteredDevices, videoDevices) {
7+
return filteredDevices.length > 0 ? filteredDevices[0].deviceId
8+
// No device found with the pattern thus use another video device
9+
: videoDevices[0].deviceId;
10+
}
11+
612
module.exports = function getDeviceId(facingMode) {
13+
var chooseDeviceId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultDeviceIdChooser;
14+
715
// Get manual deviceId from available devices.
816
return new Promise(function (resolve, reject) {
917
var enumerateDevices = void 0;
@@ -35,12 +43,7 @@ module.exports = function getDeviceId(facingMode) {
3543
return pattern.test(label);
3644
});
3745

38-
if (filteredDevices.length > 0) {
39-
resolve(filteredDevices[0].deviceId);
40-
} else {
41-
// No device found with the pattern thus use another video device
42-
resolve(videoDevices[0].deviceId);
43-
}
46+
resolve(chooseDeviceId(filteredDevices, videoDevices));
4447
});
4548
});
4649
};

lib/index.js

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/getDeviceId.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
const { NoVideoInputDevicesError } = require('./errors')
22

3-
module.exports = function getDeviceId(facingMode) {
3+
function defaultDeviceIdChooser(filteredDevices, videoDevices) {
4+
return (filteredDevices.length > 0)
5+
? filteredDevices[0].deviceId
6+
// No device found with the pattern thus use another video device
7+
: videoDevices[0].deviceId
8+
}
9+
10+
module.exports = function getDeviceId(facingMode, chooseDeviceId = defaultDeviceIdChooser) {
411
// Get manual deviceId from available devices.
512
return new Promise((resolve, reject) => {
613
let enumerateDevices
@@ -32,12 +39,7 @@ module.exports = function getDeviceId(facingMode) {
3239
const filteredDevices = videoDevices.filter(({ label }) =>
3340
pattern.test(label))
3441

35-
if (filteredDevices.length > 0) {
36-
resolve(filteredDevices[0].deviceId)
37-
} else {
38-
// No device found with the pattern thus use another video device
39-
resolve(videoDevices[0].deviceId)
40-
}
42+
resolve(chooseDeviceId(filteredDevices, videoDevices))
4143
})
4244
})
4345
}

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = class Reader extends Component {
2727
maxImageSize: PropTypes.number,
2828
style: PropTypes.object,
2929
className: PropTypes.string,
30+
chooseDeviceId: PropTypes.func,
3031
};
3132
static defaultProps = { delay: 500, style: {}, maxImageSize: 1000 };
3233

@@ -117,9 +118,9 @@ module.exports = class Reader extends Component {
117118
}
118119
}
119120
initiate(props = this.props) {
120-
const { onError, facingMode } = props
121+
const { onError, facingMode, chooseDeviceId } = props
121122

122-
getDeviceId(facingMode)
123+
getDeviceId(facingMode, chooseDeviceId)
123124
.then(deviceId => {
124125
return navigator.mediaDevices.getUserMedia({
125126
video: {

0 commit comments

Comments
 (0)