@@ -8,22 +8,51 @@ Image and Video base class
88*/
99
1010class Video {
11+ /**
12+ * @property {HTMLVideoElement } [video]
13+ */
14+
15+ /**
16+ * @param {HTMLVideoElement | p5.Video | null | undefined } [video] - Can pass a video
17+ * into the constructor of the model in order to run the model on every frame of the video.
18+ * @param {number | null | undefined } [size] - The size expected by the underlying model.
19+ * NOT the size of the current video. The size will be used to resize the current video.
20+ */
1121 constructor ( video , size ) {
22+ /**
23+ * @type {HTMLVideoElement | null }
24+ */
1225 this . videoElt = null ;
26+ /**
27+ * @type {number | null | undefined }
28+ */
1329 this . size = size ;
30+ /**
31+ * @type {boolean }
32+ */
1433 this . videoReady = false ;
1534
16- if ( video instanceof HTMLVideoElement ) {
17- this . videoElt = video ;
18- } else if ( video !== null && typeof video === 'object' && video . elt instanceof HTMLVideoElement ) {
19- // Handle p5.js video element
20- this . videoElt = video . elt ;
35+ if ( typeof HTMLVideoElement !== 'undefined' ) {
36+ if ( video instanceof HTMLVideoElement ) {
37+ this . videoElt = video ;
38+ } else if ( video !== null && typeof video === 'object' && video . elt instanceof HTMLVideoElement ) {
39+ // Handle p5.js video element
40+ this . videoElt = video . elt ;
41+ }
2142 }
2243 }
2344
45+ /**
46+ * Copies the stream from the source video into a new video element.
47+ * The copied element is set to property `this.video` and is also returned by the function.
48+ * @returns {Promise<HTMLVideoElement> }
49+ */
2450 async loadVideo ( ) {
2551 let stream ;
26- return new Promise ( ( resolve ) => {
52+ return new Promise ( ( resolve , reject ) => {
53+ if ( ! this . videoElt ) {
54+ reject ( new Error ( 'No video was passed to the constructor.' ) ) ;
55+ }
2756 this . video = document . createElement ( 'video' ) ;
2857 const sUsrAg = navigator . userAgent ;
2958 if ( sUsrAg . indexOf ( 'Firefox' ) > - 1 ) {
@@ -32,14 +61,17 @@ class Video {
3261 stream = this . videoElt . captureStream ( ) ;
3362 }
3463 this . video . srcObject = stream ;
35- this . video . width = this . size ;
36- this . video . height = this . size ;
64+ if ( this . size ) {
65+ this . video . width = this . size ;
66+ this . video . height = this . size ;
67+ }
3768 this . video . autoplay = true ;
3869 this . video . playsinline = true ;
3970 this . video . muted = true ;
4071 const playPromise = this . video . play ( ) ;
4172 if ( playPromise !== undefined ) {
4273 playPromise . then ( ( ) => {
74+ this . videoReady = true ;
4375 resolve ( this . video ) ;
4476 } ) ;
4577 }
0 commit comments