scratch-vm/src/io/video.js

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-04-03 11:44:44 -04:00
class Video {
constructor (runtime) {
this.runtime = runtime;
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 () {
if (this.provider) return this.provider.enableVideo();
return null;
2018-04-03 11:44:44 -04:00
}
/**
* Disable video stream (turn video off)
* @return {void}
*/
disableVideo () {
if (this.provider) return this.provider.disableVideo();
return null;
2018-04-05 14:42:35 -04:00
}
/**
* Return frame data from the video feed in a specified dimensions, format, and mirroring.
*
* @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.
*
* @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,
format = Video.FORMAT_IMAGE_DATA,
cacheTimeout = this._frameCacheTimeout
}) {
if (this.provider) return this.provider.getFrame({dimensions, mirror, format, cacheTimeout});
if (!this.videoReady) {
return null;
}
return null;
}
/**
* Set the preview ghost effect
* @param {number} ghost from 0 (visible) to 100 (invisible) - ghost effect
* @return {void}
*/
setPreviewGhost (ghost) {
if (this.provider) return this.provider.setPreviewGhost(ghost);
return null;
}
get videoReady () {
if (this.provider) return this.provider.videoReady();
return false;
2018-04-05 17:15:20 -04:00
}
2018-04-03 11:44:44 -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
*/
/**
* Set a video provider for this device.
* @param {VideoProvider} provider - Video provider to use
2018-04-03 11:44:44 -04:00
*/
setProvider (provider) {
this.provider = provider;
2018-04-03 11:44:44 -04:00
}
}
module.exports = Video;