From 180549496fb1eda43e38b3b9352c7f6f0418ec8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Philippe=20C=C3=B4t=C3=A9?= Date: Sat, 23 Sep 2017 14:56:43 -0400 Subject: [PATCH] Allow custom constraints to be passed to getUserMedia() There is currently no way to control the webcam media constraints. This PR changes that by allowing you to pass a `mediaConstraints` property in the `options` parameter of `tracking.track()`. This would be very useful if you want, for example, to use a low-res feed for faster color tracking or if you need to pick a specific webcam out of many. The suggested implementation is backwards-compatible. --- src/tracking.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/tracking.js b/src/tracking.js index b1229b9..08ce629 100644 --- a/src/tracking.js +++ b/src/tracking.js @@ -55,10 +55,17 @@ * @param {object} opt_options Optional configuration to the tracker. */ tracking.initUserMedia_ = function(element, opt_options) { - window.navigator.mediaDevices.getUserMedia({ + + var constraints = { video: true, audio: (opt_options && opt_options.audio) ? true : false, - }).then(function(stream) { + }; + + if (opt_options && opt_options.mediaConstraints) { + constraints = opt_options.mediaConstraints; + } + + window.navigator.mediaDevices.getUserMedia(constraints).then(function(stream) { element.srcObject = stream; }).catch(function(err) { throw Error('Cannot capture user camera.');