2018-04-03 11:44:44 -04:00
|
|
|
class Video {
|
|
|
|
constructor (runtime) {
|
|
|
|
this.runtime = runtime;
|
2018-04-26 13:24:31 -04:00
|
|
|
this.provider = null;
|
2018-04-03 11:44:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static get FORMAT_IMAGE_DATA () {
|
|
|
|
return 'image-data';
|
|
|
|
}
|
|
|
|
|
|
|
|
static get FORMAT_CANVAS () {
|
|
|
|
return 'canvas';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dimensions the video stream is analyzed at after its rendered to the
|
|
|
|
* sample canvas.
|
|
|
|
* @type {Array.<number>}
|
|
|
|
*/
|
|
|
|
static get DIMENSIONS () {
|
|
|
|
return [480, 360];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order preview drawable is inserted at in the renderer.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
static get ORDER () {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request video be enabled. Sets up video, creates video skin and enables preview.
|
|
|
|
*
|
|
|
|
* ioDevices.video.requestVideo()
|
|
|
|
*
|
2018-04-04 14:20:13 -04:00
|
|
|
* @return {Promise.<Video>} resolves a promise to this IO device when video is ready.
|
2018-04-03 11:44:44 -04:00
|
|
|
*/
|
2018-04-04 14:20:13 -04:00
|
|
|
enableVideo () {
|
2018-04-26 13:24:31 -04:00
|
|
|
if (this.provider) return this.provider.enableVideo();
|
|
|
|
return null;
|
2018-04-03 11:44:44 -04:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:03:48 -04:00
|
|
|
/**
|
|
|
|
* Disable video stream (turn video off)
|
2018-04-26 13:24:31 -04:00
|
|
|
* @return {void}
|
2018-04-03 16:03:48 -04:00
|
|
|
*/
|
|
|
|
disableVideo () {
|
2018-04-26 13:24:31 -04:00
|
|
|
if (this.provider) return this.provider.disableVideo();
|
|
|
|
return null;
|
2018-04-05 14:42:35 -04:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:03:48 -04:00
|
|
|
/**
|
|
|
|
* Return frame data from the video feed in a specified dimensions, format, and mirroring.
|
2018-04-06 11:01:09 -04:00
|
|
|
*
|
|
|
|
* @param {object} frameInfo A descriptor of the frame you would like to receive.
|
|
|
|
* @param {Array.<number>} frameInfo.dimensions [width, height] array of numbers. Defaults to [480,360]
|
|
|
|
* @param {boolean} frameInfo.mirror If you specificly want a mirror/non-mirror frame, defaults to the global
|
|
|
|
* mirror state (ioDevices.video.mirror)
|
|
|
|
* @param {string} frameInfo.format Requested video format, available formats are 'image-data' and 'canvas'.
|
|
|
|
* @param {number} frameInfo.cacheTimeout Will reuse previous image data if the time since capture is less than
|
|
|
|
* the cacheTimeout. Defaults to 16ms.
|
|
|
|
*
|
2018-04-03 16:03:48 -04:00
|
|
|
* @return {ArrayBuffer|Canvas|string|null} Frame data in requested format, null when errors.
|
|
|
|
*/
|
|
|
|
getFrame ({
|
|
|
|
dimensions = Video.DIMENSIONS,
|
2018-04-03 18:09:05 -04:00
|
|
|
mirror = this.mirror,
|
2018-04-03 16:03:48 -04:00
|
|
|
format = Video.FORMAT_IMAGE_DATA,
|
|
|
|
cacheTimeout = this._frameCacheTimeout
|
|
|
|
}) {
|
2018-04-26 13:24:31 -04:00
|
|
|
if (this.provider) return this.provider.getFrame({dimensions, mirror, format, cacheTimeout});
|
2018-04-03 16:03:48 -04:00
|
|
|
if (!this.videoReady) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-26 13:24:31 -04:00
|
|
|
return null;
|
2018-04-03 16:03:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the preview ghost effect
|
|
|
|
* @param {number} ghost from 0 (visible) to 100 (invisible) - ghost effect
|
2018-04-26 13:24:31 -04:00
|
|
|
* @return {void}
|
2018-04-03 16:03:48 -04:00
|
|
|
*/
|
|
|
|
setPreviewGhost (ghost) {
|
2018-04-26 13:24:31 -04:00
|
|
|
if (this.provider) return this.provider.setPreviewGhost(ghost);
|
|
|
|
return null;
|
2018-04-03 16:03:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-26 13:24:31 -04:00
|
|
|
get videoReady () {
|
|
|
|
if (this.provider) return this.provider.videoReady();
|
|
|
|
return false;
|
2018-04-05 17:15:20 -04:00
|
|
|
}
|
2018-04-03 16:03:48 -04:00
|
|
|
|
2018-04-03 11:44:44 -04:00
|
|
|
/**
|
2018-04-26 13:24:31 -04:00
|
|
|
* @typedef VideoProvider
|
|
|
|
* @property {Function} enableVideo - Requests camera access from the user, and upon success,
|
|
|
|
* enables the video feed
|
|
|
|
* @property {Function} disableVideo - Turns off the video feed
|
|
|
|
* @property {Function} setGhostPreview - Controls the transparency of a visual layer
|
|
|
|
* over the video feed
|
|
|
|
* @property {Function} getFrame - Return frame data from the video feed in
|
|
|
|
* specified dimensions, format, and mirroring.
|
2018-04-03 11:44:44 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-04-26 13:24:31 -04:00
|
|
|
* Set a video provider for this device.
|
|
|
|
* @param {VideoProvider} provider - Video provider to use
|
2018-04-03 11:44:44 -04:00
|
|
|
*/
|
2018-04-26 13:24:31 -04:00
|
|
|
setProvider (provider) {
|
|
|
|
this.provider = provider;
|
2018-04-03 11:44:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Video;
|