@@ -10,6 +10,7 @@ import DestinationNode from "./DestinationNode/destinationnode.js";
10
10
import EffectNode from "./ProcessingNodes/effectnode.js" ;
11
11
import TransitionNode from "./ProcessingNodes/transitionnode.js" ;
12
12
import RenderGraph from "./rendergraph.js" ;
13
+ import VideoElementCache from "./videoelementcache.js" ;
13
14
import { createSigmaGraphDataFromRenderGraph , visualiseVideoContextTimeline , visualiseVideoContextGraph , createControlFormForNode , UpdateablesManager , exportToJSON } from "./utils.js" ;
14
15
import DEFINITIONS from "./Definitions/definitions.js" ;
15
16
@@ -29,7 +30,7 @@ export default class VideoContext{
29
30
* ctx.play();
30
31
*
31
32
*/
32
- constructor ( canvas , initErrorCallback , options = { "preserveDrawingBuffer" :true , "manualUpdate" :false , "endOnLastSourceEnd" :true , useVideoElementCache :false , videoElementCacheSize :5 , webglContextAttributes : { preserveDrawingBuffer : true , alpha : false } } ) {
33
+ constructor ( canvas , initErrorCallback , options = { "preserveDrawingBuffer" :true , "manualUpdate" :false , "endOnLastSourceEnd" :true , useVideoElementCache :true , videoElementCacheSize :6 , webglContextAttributes : { preserveDrawingBuffer : true , alpha : false } } ) {
33
34
this . _canvas = canvas ;
34
35
let manualUpdate = false ;
35
36
this . endOnLastSourceEnd = true ;
@@ -53,19 +54,11 @@ export default class VideoContext{
53
54
}
54
55
55
56
// Initialise the video element cache
57
+ if ( ! options . useVideoElementCache ) options . useVideoElementCache = true ;
56
58
this . _useVideoElementCache = options . useVideoElementCache ;
57
59
if ( this . _useVideoElementCache ) {
58
- this . _videoElementCache = [ ] ;
59
- if ( options . videoElementCacheSize === undefined ) options . videoElementCacheSize = 5 ;
60
- console . log ( "USING VIDEO CACHE" ) ;
61
- console . log ( options . videoElementCacheSize ) ;
62
- for ( var i = 0 ; i < options . videoElementCacheSize ; i ++ ) {
63
- let videoElement = document . createElement ( "video" ) ;
64
- videoElement . setAttribute ( "crossorigin" , "anonymous" ) ;
65
- videoElement . setAttribute ( "webkit-playsinline" , "" ) ;
66
- videoElement . src = "" ;
67
- this . _videoElementCache . push ( videoElement ) ;
68
- }
60
+ if ( ! options . videoElementCacheSize ) options . videoElementCacheSize = 5 ;
61
+ this . _videoElementCache = new VideoElementCache ( options . videoElementCacheSize ) ;
69
62
}
70
63
71
64
@@ -351,22 +344,9 @@ export default class VideoContext{
351
344
*/
352
345
play ( ) {
353
346
console . debug ( "VideoContext - playing" ) ;
354
-
355
- //Mark the elements in the video element cache as being able to be controlled programattically.
356
- if ( this . _videoElementCache ) {
357
- for ( var i = 0 ; i < this . _videoElementCache . length ; i ++ ) {
358
- let videoElement = this . _videoElementCache [ i ] ;
359
- videoElement . play ( ) . then ( ( ) => {
360
- try {
361
- videoElement . pause ( ) ;
362
- } catch ( e ) {
363
- //catch the inevitable DOM exception.
364
- }
365
- } ) ;
366
- }
367
- }
368
-
369
-
347
+ //Initialise the video elemnt cache
348
+ if ( this . _videoElementCache ) this . _videoElementCache . init ( ) ;
349
+ // set the state.
370
350
this . _state = VideoContext . STATE . PLAYING ;
371
351
return true ;
372
352
}
@@ -391,34 +371,6 @@ export default class VideoContext{
391
371
}
392
372
393
373
394
- _getVideoElementCacheRemaining ( ) {
395
- let cacheCount = 0 ;
396
- if ( this . _useVideoElementCache ) {
397
- for ( var i = 0 ; i < this . _videoElementCache . length ; i ++ ) {
398
- let videoElement = this . _videoElementCache [ i ] ;
399
- // For some reason an uninitialised videoElement has its sr attribute set to the windows href. Hence the below check.
400
- if ( videoElement . src === "" || videoElement . src === undefined || videoElement . src === window . location . href ) cacheCount += 1 ;
401
- }
402
- }
403
- return cacheCount ;
404
- }
405
-
406
- _getCachedVideoElement ( ) {
407
- if ( this . _useVideoElementCache ) {
408
- for ( var i = 0 ; i < this . _videoElementCache . length ; i ++ ) {
409
- let videoElement = this . _videoElementCache [ i ] ;
410
- // For some reason an uninitialised videoElement has its sr attribute set to the windows href. Hence the below check.
411
- if ( videoElement . src === "" || videoElement . src === undefined || videoElement . src === window . location . href ) return videoElement ;
412
- }
413
- } else {
414
- console . error ( "Video element cache not enabled, pass {useVideoElementCache:true} into the VideoContext constructor." ) ;
415
- return ;
416
- }
417
- console . error ( "No available video element in the cache" ) ;
418
- return ;
419
- }
420
-
421
-
422
374
/**
423
375
* Create a new node representing a video source
424
376
*
@@ -436,14 +388,7 @@ export default class VideoContext{
436
388
* var videoNode = ctx.video(videoElement);
437
389
*/
438
390
video ( src , sourceOffset = 0 , preloadTime = 4 , videoElementAttributes = { } ) {
439
- let videoNode ;
440
- if ( this . _useVideoElementCache && typeof src === "string" ) {
441
- let videoElement = this . _getCachedVideoElement ( ) ;
442
- videoElement . src = src ;
443
- videoNode = new VideoNode ( videoElement , this . _gl , this . _renderGraph , this . _currentTime , this . _playbackRate , sourceOffset , preloadTime , this . _useVideoElementCache , videoElementAttributes ) ;
444
- } else {
445
- videoNode = new VideoNode ( src , this . _gl , this . _renderGraph , this . _currentTime , this . _playbackRate , sourceOffset , preloadTime , this . _useVideoElementCache , videoElementAttributes ) ;
446
- }
391
+ let videoNode = new VideoNode ( src , this . _gl , this . _renderGraph , this . _currentTime , this . _playbackRate , sourceOffset , preloadTime , this . _videoElementCache , videoElementAttributes ) ;
447
392
this . _sourceNodes . push ( videoNode ) ;
448
393
return videoNode ;
449
394
}
0 commit comments