From 4654ee13786532e35125f61d2d5025c4c298113a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Mon, 13 Nov 2017 15:20:00 +0100 Subject: [PATCH] webcam,dashboard: Add `isSupported()` API for providers Providers can signal whether they support the current environment using an optional `isSupported()` method. Providers that always work (eg. Google Drive) do not need to specify this method. The check for the Webcam currently only checks if the `getUserMedia` method exists at all. We could maybe do some more checks, like, if the mode is `video-only` or `video-audio` but MediaRecorder is not supported. --- src/plugins/Dashboard/index.js | 11 ++++++++++- src/plugins/Webcam/index.js | 33 +++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/plugins/Dashboard/index.js b/src/plugins/Dashboard/index.js index bbd94c524..672eceb70 100644 --- a/src/plugins/Dashboard/index.js +++ b/src/plugins/Dashboard/index.js @@ -311,8 +311,17 @@ module.exports = class DashboardUI extends Plugin { }) } + const isSupported = (target) => { + const plugin = this.core.getPlugin(target.id) + // If the plugin does not provide a `supported` check, assume the plugin works everywhere. + if (typeof plugin.isSupported !== 'function') { + return true + } + return plugin.isSupported() + } + const acquirers = pluginState.targets - .filter(target => target.type === 'acquirer') + .filter(target => target.type === 'acquirer' && isSupported(target)) .map(attachRenderFunctionToTarget) const progressindicators = pluginState.targets diff --git a/src/plugins/Webcam/index.js b/src/plugins/Webcam/index.js index 5a540db08..9f9f7f5e9 100644 --- a/src/plugins/Webcam/index.js +++ b/src/plugins/Webcam/index.js @@ -16,7 +16,7 @@ function getMediaDevices () { return navigator.mediaDevices } - let getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia + const getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia if (!getUserMedia) { return null } @@ -94,25 +94,34 @@ module.exports = class Webcam extends Plugin { } } - start () { - if (!this.mediaDevices) { - return Promise.reject(new Error('Webcam access not supported')) - } - - this.webcamActive = true + isSupported () { + return !!this.mediaDevices + } + getConstraints () { const acceptsAudio = this.opts.modes.indexOf('video-audio') !== -1 || this.opts.modes.indexOf('audio-only') !== -1 const acceptsVideo = this.opts.modes.indexOf('video-audio') !== -1 || this.opts.modes.indexOf('video-only') !== -1 || this.opts.modes.indexOf('picture') !== -1 + return { + audio: acceptsAudio, + video: acceptsVideo + } + } + + start () { + if (!this.isSupported()) { + return Promise.reject(new Error('Webcam access not supported')) + } + + this.webcamActive = true + + const constraints = this.getConstraints() + // ask user for access to their camera - return this.mediaDevices - .getUserMedia({ - audio: acceptsAudio, - video: acceptsVideo - }) + return this.mediaDevices.getUserMedia(constraints) .then((stream) => { this.stream = stream this.streamSrc = URL.createObjectURL(this.stream)