add enabled state tracking, disable state waits for setup complete

This commit is contained in:
Corey Frang 2018-04-05 14:30:57 -04:00 committed by Michael "Z" Goddard
parent ce03e978dc
commit f3a956af2b
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -91,10 +91,7 @@ class Video {
* @return {Promise.<Video>} resolves a promise to this IO device when video is ready. * @return {Promise.<Video>} resolves a promise to this IO device when video is ready.
*/ */
enableVideo () { enableVideo () {
if (this.videoReady) { this.enabled = true;
return Promise.resolve(this);
}
return this._setupVideo().then(() => this); return this._setupVideo().then(() => this);
} }
@ -102,14 +99,25 @@ class Video {
* Disable video stream (turn video off) * Disable video stream (turn video off)
*/ */
disableVideo () { disableVideo () {
this._disablePreview(); this.enabled = false;
this._singleSetup = null; // If we have begun a setup process, wait for it to complete
// by clearing refs to video and track, we should lose our hold over the camera if (this._singleSetup) {
this._video = null; this._singleSetup
if (this._track) { .then(() => {
this._track.stop(); // we might be asked to re-enable before setup completes
if (!this.enabled) {
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;
}
})
.catch(() => {});
} }
this._track = null;
} }
/** /**
@ -195,23 +203,14 @@ class Video {
* @return {Promise} When video has been received, rejected if video is not received * @return {Promise} When video has been received, rejected if video is not received
*/ */
_setupVideo () { _setupVideo () {
// We cache the result of this setup so that we can only ever have a single
// video/getUserMedia request happen at a time.
if (this._singleSetup) { if (this._singleSetup) {
return this._singleSetup; return this._singleSetup;
} }
this._video = document.createElement('video'); this._video = document.createElement('video');
const video = new Promise((resolve, reject) => { const video = new Promise((resolve, reject) => {
// if we disabledVideo in the same frame, just make sure singleSetup is cleared, never resolve
if (!this._video) {
this._singleSetup = null;
return;
}
// in the event there was some second setup
if (this._track) {
return resolve(this._video);
}
navigator.getUserMedia({ navigator.getUserMedia({
audio: false, audio: false,
video: { video: {
@ -219,17 +218,6 @@ class Video {
height: {min: 360, ideal: 480} height: {min: 360, ideal: 480}
} }
}, stream => { }, stream => {
// if we disabled video in the meantime...
if (!this._video) {
stream.getTracks()[0].stop();
this._singleSetup = null;
return;
}
// if we somehow got here with two user medias, stop the old stream
if (this._track) {
this._track.stop();
}
this._video.src = window.URL.createObjectURL(stream); this._video.src = window.URL.createObjectURL(stream);
// Hint to the stream that it should load. A standard way to do this // 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 // is add the video tag to the DOM. Since this extension wants to
@ -291,6 +279,7 @@ class Video {
const canvas = this.getFrame({format: Video.FORMAT_CANVAS}); const canvas = this.getFrame({format: Video.FORMAT_CANVAS});
if (!canvas) { if (!canvas) {
this._skin.clear();
return; return;
} }
@ -305,6 +294,9 @@ class Video {
} }
get videoReady () { get videoReady () {
if (!this.enabled) {
return false;
}
if (!this._video) { if (!this._video) {
return false; return false;
} }