Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code clean-up: Video class. #1311

Merged
merged 1 commit into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/StyleTransfer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class StyleTransfer extends Video {
async load(model) {
if (this.videoElt) {
await this.loadVideo();
this.videoReady = true;
}
await this.loadCheckpoints(model);
return this;
Expand Down
48 changes: 40 additions & 8 deletions src/utils/Video.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,51 @@ Image and Video base class
*/

class Video {
/**
* @property {HTMLVideoElement} [video]
*/

/**
* @param {HTMLVideoElement | p5.Video | null | undefined} [video] - Can pass a video
* into the constructor of the model in order to run the model on every frame of the video.
* @param {number | null | undefined} [size] - The size expected by the underlying model.
* NOT the size of the current video. The size will be used to resize the current video.
*/
constructor(video, size) {
/**
* @type {HTMLVideoElement | null}
*/
this.videoElt = null;
/**
* @type {number | null | undefined}
*/
this.size = size;
/**
* @type {boolean}
*/
this.videoReady = false;

if (video instanceof HTMLVideoElement) {
this.videoElt = video;
} else if (video !== null && typeof video === 'object' && video.elt instanceof HTMLVideoElement) {
// Handle p5.js video element
this.videoElt = video.elt;
if (typeof HTMLVideoElement !== 'undefined') {
if (video instanceof HTMLVideoElement) {
this.videoElt = video;
} else if (video !== null && typeof video === 'object' && video.elt instanceof HTMLVideoElement) {
// Handle p5.js video element
this.videoElt = video.elt;
}
}
}

/**
* Copies the stream from the source video into a new video element.
* The copied element is set to property `this.video` and is also returned by the function.
* @returns {Promise<HTMLVideoElement>}
*/
async loadVideo() {
let stream;
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
if (!this.videoElt) {
reject(new Error('No video was passed to the constructor.'));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positive affirmation:

  • nice error catching up here! TY! 🙌

this.video = document.createElement('video');
const sUsrAg = navigator.userAgent;
if (sUsrAg.indexOf('Firefox') > -1) {
Expand All @@ -32,14 +61,17 @@ class Video {
stream = this.videoElt.captureStream();
}
this.video.srcObject = stream;
this.video.width = this.size;
this.video.height = this.size;
if (this.size) {
this.video.width = this.size;
this.video.height = this.size;
}
this.video.autoplay = true;
this.video.playsinline = true;
this.video.muted = true;
const playPromise = this.video.play();
if (playPromise !== undefined) {
playPromise.then(() => {
this.videoReady = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Positive affirmation:

  • nice one! This is def the right place to update the this.videoReady flag. 👍

resolve(this.video);
});
}
Expand Down