Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to pass constraints to getUserMedia. #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ let opts = {
- If `opts.video` element was specified, it will have the `inactive` CSS class.
- `callback`: `function ()`

### Instascan.Camera.getCameras()
### Instascan.Camera.getCameras(options)

- Passes `options` along to getUserMedia ([see available options](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Parameters)).
- Defaults to `{ video: { facingMode: 'environment' } }` which will prefer the rear-facing camera if available.
- Enumerate available video devices. Returns promise.
- `.then(function (cameras) { ... })`
- Called when cameras are available.
Expand All @@ -159,9 +161,7 @@ let opts = {

## Compatibility

Instascan works on non-iOS platforms in [any browser that supports the WebRTC/getUserMedia API](http://caniuse.com/#feat=stream), which currently includes Chome, Firefox, Opera, and Edge. IE and Safari are not supported.

Instascan does not work on iOS since Apple does not yet support WebRTC in WebKit *and* forces other browser vendors (Chrome, Firefox, Opera) to use their implementation of WebKit. [Apple is actively working on WebRTC support in WebKit](https://bugs.webkit.org/show_bug.cgi?id=124288).
Instascan works in [any browser that supports the WebRTC/getUserMedia API](http://caniuse.com/#feat=stream), which currently includes Chrome, Safari, Firefox, Opera, and Edge. IE is not supported.

## Performance

Expand Down
10 changes: 6 additions & 4 deletions src/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,20 @@ class Camera {
this._stream = null;
}

static async getCameras() {
await this._ensureAccess();
static async getCameras(options) {
let defaults = { video: { facingMode: 'environment' } };
let constraints = Object.assign({}, defaults, options);
await this._ensureAccess(constraints);

let devices = await navigator.mediaDevices.enumerateDevices();
return devices
.filter(d => d.kind === 'videoinput')
.map(d => new Camera(d.deviceId, cameraName(d.label)));
}

static async _ensureAccess() {
static async _ensureAccess(constraints) {
return await this._wrapErrors(async () => {
let access = await navigator.mediaDevices.getUserMedia({ video: true });
let access = await navigator.mediaDevices.getUserMedia(constraints);
for (let stream of access.getVideoTracks()) {
stream.stop();
}
Expand Down