2018-04-03 11:44:44 -04:00
|
|
|
const log = require('../util/log');
|
|
|
|
|
|
|
|
class Video {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* Reference to the owning Runtime.
|
|
|
|
* @type{!Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
2018-04-03 18:09:05 -04:00
|
|
|
/**
|
|
|
|
* Default value for mirrored frames.
|
|
|
|
* @type boolean
|
|
|
|
*/
|
|
|
|
this.mirror = true;
|
|
|
|
|
2018-04-03 11:44:44 -04:00
|
|
|
/**
|
|
|
|
* Cache frames for this many ms.
|
|
|
|
* @type number
|
|
|
|
*/
|
|
|
|
this._frameCacheTimeout = 16;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DOM Video element
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._video = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Usermedia stream track
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._track = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores some canvas/frame data per resolution/mirror states
|
|
|
|
*/
|
|
|
|
this._workspace = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Id representing a Scratch Renderer skin the video is rendered to for
|
|
|
|
* previewing.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._skinId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The Scratch Renderer Skin object.
|
|
|
|
* @type {Skin}
|
|
|
|
*/
|
|
|
|
this._skin = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Id for a drawable using the video's skin that will render as a video
|
|
|
|
* preview.
|
|
|
|
* @type {Drawable}
|
|
|
|
*/
|
|
|
|
this._drawable = -1;
|
2018-04-05 15:46:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the last state of the video transparency ghost effect
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this._ghost = 0;
|
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-05 14:30:57 -04:00
|
|
|
this.enabled = true;
|
2018-04-05 16:48:42 -04:00
|
|
|
return this._setupVideo();
|
2018-04-03 11:44:44 -04:00
|
|
|
}
|
|
|
|
|
2018-04-03 16:03:48 -04:00
|
|
|
/**
|
|
|
|
* Disable video stream (turn video off)
|
|
|
|
*/
|
|
|
|
disableVideo () {
|
2018-04-05 14:30:57 -04:00
|
|
|
this.enabled = false;
|
2018-04-05 14:42:35 -04:00
|
|
|
// If we have begun a setup process, call _teardown after it completes
|
2018-04-05 14:30:57 -04:00
|
|
|
if (this._singleSetup) {
|
|
|
|
this._singleSetup
|
2018-04-05 14:42:35 -04:00
|
|
|
.then(this._teardown.bind(this))
|
2018-04-05 17:15:20 -04:00
|
|
|
.catch(err => this.onError(err));
|
2018-04-03 18:16:12 -04:00
|
|
|
}
|
2018-04-03 16:03:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 14:42:35 -04:00
|
|
|
/**
|
|
|
|
* async part of disableVideo
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_teardown () {
|
|
|
|
// we might be asked to re-enable before _teardown is called, just ignore it.
|
|
|
|
if (this.enabled === false) {
|
|
|
|
this._disablePreview();
|
|
|
|
this._singleSetup = null;
|
|
|
|
// by clearing refs to video and track, we should lose our hold over the camera
|
|
|
|
this._video = null;
|
|
|
|
if (this._track) {
|
|
|
|
this._track.stop();
|
|
|
|
}
|
|
|
|
this._track = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:03:48 -04:00
|
|
|
/**
|
|
|
|
* Return frame data from the video feed in a specified dimensions, format, and mirroring.
|
|
|
|
* @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
|
|
|
|
}) {
|
|
|
|
if (!this.videoReady) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const [width, height] = dimensions;
|
|
|
|
const workspace = this._getWorkspace({dimensions, mirror: Boolean(mirror)});
|
|
|
|
const {videoWidth, videoHeight} = this._video;
|
|
|
|
const {canvas, context, lastUpdate, cacheData} = workspace;
|
|
|
|
const now = Date.now();
|
|
|
|
|
|
|
|
// if the canvas hasn't been updated...
|
|
|
|
if (lastUpdate + cacheTimeout < now) {
|
|
|
|
|
|
|
|
if (mirror) {
|
|
|
|
context.scale(-1, 1);
|
|
|
|
context.translate(width * -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.drawImage(this._video,
|
|
|
|
// source x, y, width, height
|
|
|
|
0, 0, videoWidth, videoHeight,
|
|
|
|
// dest x, y, width, height
|
|
|
|
0, 0, width, height
|
|
|
|
);
|
|
|
|
|
|
|
|
context.resetTransform();
|
|
|
|
workspace.lastUpdate = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// each data type has it's own data cache, but the canvas is the same
|
|
|
|
if (!cacheData[format]) {
|
|
|
|
cacheData[format] = {lastUpdate: 0};
|
|
|
|
}
|
|
|
|
const formatCache = cacheData[format];
|
|
|
|
|
|
|
|
if (formatCache.lastUpdate + cacheTimeout < now) {
|
|
|
|
if (format === Video.FORMAT_IMAGE_DATA) {
|
|
|
|
formatCache.lastData = context.getImageData(0, 0, width, height);
|
|
|
|
} else if (format === Video.FORMAT_CANVAS) {
|
|
|
|
// this will never change
|
|
|
|
formatCache.lastUpdate = Infinity;
|
|
|
|
formatCache.lastData = canvas;
|
|
|
|
} else {
|
|
|
|
log.error(`video io error - unimplemented format ${format}`);
|
|
|
|
// cache the null result forever, don't log about it again..
|
|
|
|
formatCache.lastUpdate = Infinity;
|
|
|
|
formatCache.lastData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// rather than set to now, this data is as stale as it's canvas is
|
|
|
|
formatCache.lastUpdate = Math.max(workspace.lastUpdate, formatCache.lastUpdate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatCache.lastData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the preview ghost effect
|
|
|
|
* @param {number} ghost from 0 (visible) to 100 (invisible) - ghost effect
|
|
|
|
*/
|
|
|
|
setPreviewGhost (ghost) {
|
2018-04-05 15:45:56 -04:00
|
|
|
this._ghost = ghost;
|
2018-04-03 16:03:48 -04:00
|
|
|
if (this._drawable) {
|
2018-04-03 17:46:31 -04:00
|
|
|
this.runtime.renderer.updateDrawableProperties(this._drawable, {ghost});
|
2018-04-03 16:03:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-05 17:15:20 -04:00
|
|
|
/**
|
|
|
|
* Method called when an error happens. Default implementation is just to log error.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @param {Error} error An error object from getUserMedia or other source of error.
|
|
|
|
*/
|
|
|
|
onError (error) {
|
|
|
|
log.error('Unhandled video io device error', error);
|
|
|
|
}
|
2018-04-03 16:03:48 -04:00
|
|
|
|
2018-04-03 11:44:44 -04:00
|
|
|
/**
|
|
|
|
* Create a video stream.
|
|
|
|
* Should probably be moved to -render or somewhere similar later
|
|
|
|
* @private
|
|
|
|
* @return {Promise} When video has been received, rejected if video is not received
|
|
|
|
*/
|
|
|
|
_setupVideo () {
|
2018-04-05 14:30:57 -04:00
|
|
|
// We cache the result of this setup so that we can only ever have a single
|
|
|
|
// video/getUserMedia request happen at a time.
|
2018-04-03 15:09:25 -04:00
|
|
|
if (this._singleSetup) {
|
|
|
|
return this._singleSetup;
|
2018-04-03 15:07:50 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 16:48:42 -04:00
|
|
|
this._singleSetup = new Promise((resolve, reject) => {
|
2018-04-03 11:44:44 -04:00
|
|
|
navigator.getUserMedia({
|
|
|
|
audio: false,
|
|
|
|
video: {
|
|
|
|
width: {min: 480, ideal: 640},
|
|
|
|
height: {min: 360, ideal: 480}
|
|
|
|
}
|
2018-04-05 16:48:42 -04:00
|
|
|
}, resolve, reject);
|
|
|
|
})
|
|
|
|
.then(stream => {
|
|
|
|
this._video = document.createElement('video');
|
2018-04-03 11:44:44 -04:00
|
|
|
this._video.src = window.URL.createObjectURL(stream);
|
|
|
|
// Hint to the stream that it should load. A standard way to do this
|
|
|
|
// is add the video tag to the DOM. Since this extension wants to
|
|
|
|
// hide the video tag and instead render a sample of the stream into
|
|
|
|
// the webgl rendered Scratch canvas, another hint like this one is
|
|
|
|
// needed.
|
|
|
|
this._track = stream.getTracks()[0];
|
2018-04-05 16:48:42 -04:00
|
|
|
this._setupPreview();
|
|
|
|
return this;
|
|
|
|
})
|
2018-04-05 17:15:20 -04:00
|
|
|
.catch(error => {
|
2018-04-03 15:09:25 -04:00
|
|
|
this._singleSetup = null;
|
2018-04-05 17:15:20 -04:00
|
|
|
this.onError(error);
|
2018-04-03 11:44:44 -04:00
|
|
|
});
|
2018-04-03 15:07:50 -04:00
|
|
|
|
2018-04-03 15:09:25 -04:00
|
|
|
return this._singleSetup;
|
2018-04-03 15:07:50 -04:00
|
|
|
}
|
|
|
|
|
2018-04-03 11:44:44 -04:00
|
|
|
_disablePreview () {
|
2018-04-05 16:48:42 -04:00
|
|
|
if (this._skin) {
|
2018-04-03 11:44:44 -04:00
|
|
|
this._skin.clear();
|
2018-04-03 17:46:31 -04:00
|
|
|
this.runtime.renderer.updateDrawableProperties(this._drawable, {visible: false});
|
2018-04-03 11:44:44 -04:00
|
|
|
}
|
|
|
|
this._renderPreviewFrame = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupPreview () {
|
|
|
|
const {renderer} = this.runtime;
|
|
|
|
if (!renderer) return;
|
|
|
|
|
|
|
|
if (this._skinId === -1 && this._skin === null && this._drawable === -1) {
|
|
|
|
this._skinId = renderer.createPenSkin();
|
|
|
|
this._skin = renderer._allSkins[this._skinId];
|
|
|
|
this._drawable = renderer.createDrawable();
|
|
|
|
renderer.setDrawableOrder(
|
|
|
|
this._drawable,
|
|
|
|
Video.ORDER
|
|
|
|
);
|
|
|
|
renderer.updateDrawableProperties(this._drawable, {
|
|
|
|
skinId: this._skinId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we haven't already created and started a preview frame render loop, do so
|
|
|
|
if (!this._renderPreviewFrame) {
|
2018-04-03 17:46:31 -04:00
|
|
|
renderer.updateDrawableProperties(this._drawable, {
|
2018-04-05 15:45:56 -04:00
|
|
|
ghost: this._ghost,
|
2018-04-03 17:46:31 -04:00
|
|
|
visible: true
|
|
|
|
});
|
2018-04-03 16:01:10 -04:00
|
|
|
|
2018-04-03 11:44:44 -04:00
|
|
|
this._renderPreviewFrame = () => {
|
2018-04-05 13:19:03 -04:00
|
|
|
clearTimeout(this._renderPreviewTimeout);
|
2018-04-03 11:44:44 -04:00
|
|
|
if (!this._renderPreviewFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-05 13:19:03 -04:00
|
|
|
this._renderPreviewTimeout = setTimeout(this._renderPreviewFrame, this.runtime.currentStepTime);
|
2018-04-03 11:44:44 -04:00
|
|
|
|
|
|
|
const canvas = this.getFrame({format: Video.FORMAT_CANVAS});
|
|
|
|
|
|
|
|
if (!canvas) {
|
2018-04-05 14:30:57 -04:00
|
|
|
this._skin.clear();
|
2018-04-03 11:44:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const xOffset = Video.DIMENSIONS[0] / -2;
|
|
|
|
const yOffset = Video.DIMENSIONS[1] / 2;
|
|
|
|
this._skin.drawStamp(canvas, xOffset, yOffset);
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
this._renderPreviewFrame();
|
|
|
|
}
|
2018-04-03 15:43:57 -04:00
|
|
|
}
|
2018-04-03 11:44:44 -04:00
|
|
|
|
|
|
|
get videoReady () {
|
2018-04-05 14:30:57 -04:00
|
|
|
if (!this.enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-03 11:44:44 -04:00
|
|
|
if (!this._video) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!this._track) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const {videoWidth, videoHeight} = this._video;
|
|
|
|
if (typeof videoWidth !== 'number' || typeof videoHeight !== 'number') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (videoWidth === 0 || videoHeight === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get an internal workspace for canvas/context/caches
|
|
|
|
* this uses some document stuff to create a canvas and what not, probably needs abstraction
|
|
|
|
* into the renderer layer?
|
|
|
|
* @private
|
|
|
|
* @return {object} A workspace for canvas/data storage. Internal format not documented intentionally
|
|
|
|
*/
|
|
|
|
_getWorkspace ({dimensions, mirror}) {
|
|
|
|
let workspace = this._workspace.find(space => (
|
|
|
|
space.dimensions.join('-') === dimensions.join('-') &&
|
|
|
|
space.mirror === mirror
|
|
|
|
));
|
|
|
|
if (!workspace) {
|
|
|
|
workspace = {
|
|
|
|
dimensions,
|
|
|
|
mirror,
|
|
|
|
canvas: document.createElement('canvas'),
|
|
|
|
lastUpdate: 0,
|
|
|
|
cacheData: {}
|
|
|
|
};
|
|
|
|
workspace.canvas.width = dimensions[0];
|
|
|
|
workspace.canvas.height = dimensions[1];
|
|
|
|
workspace.context = workspace.canvas.getContext('2d');
|
|
|
|
this._workspace.push(workspace);
|
|
|
|
}
|
|
|
|
return workspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = Video;
|