-
Notifications
You must be signed in to change notification settings - Fork 4
/
channel-two.js
1264 lines (961 loc) · 37.4 KB
/
channel-two.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
jQuery( function ( $ ) {
var options = {
showNowPlaying : true
};
/**
* How much logging?
* 0 = none
* 1 = Program starts/stops/ads
* 2 = Everything
* 3 = Everything and more
*/
var logLevel = 2;
/**
* The jQuery representation of the <video> tag.
*/
var tv = $( '#tv' );
var tvElement = tv.get(0);
var canvas = null;
var context = null;
if ( typeof OffscreenCanvas !== 'undefined' ) {
canvas = new OffscreenCanvas( 100, 100 );
context = canvas.getContext( '2d' );
}
/**
* The timer for the currently pending programming check.
*/
var nextProgrammingTimerId = null;
/**
* When watching for ad breaks, how many dark frames have we seen in a row?
*/
var consecutiveDarkFrames = 0;
/**
* What is the default minimum time (in seconds) between ad breaks?
*/
var minimumSecondsBetweenAdBreaks = 10 * 60;
/**
* When was the last ad break?
*/
var lastAdBreak = 0;
/**
* The timer for the currently pending commercial break check.
*/
var commercialBreakTimerId = null;
var programmingQueue = [];
var chyronTimerId = null;
/**
* Should captions be turned on regardless of the setting in the schedule? Press C to enable.
* Note that there's no way to turn captions off if they've been enabled in the schedule file.
*/
var captionOverride = false;
/**
* Webpages can't autoplay video or go full-screen without some interaction from the user,
* so require a button to be clicked to kick things off.
*/
$( '#start' ).on( 'click', function () {
if ( logLevel >= 2 ) console.log( "#start.click()" );
// Periodically re-fetch the programming schedule in case new content has been added (or changed, or removed, or...)
setInterval( refreshProgramming, 5 * 60 * 1000 );
if ( 'requestFullscreen' in document.documentElement ) {
document.documentElement.requestFullscreen();
} else if ( 'webkitRequestFullScreen' in document.documentElement ) {
document.documentElement.webkitRequestFullScreen();
} else if ( 'mozRequestFullScreen' in document.documentElement ) {
document.documentElement.mozRequestFullScreen();
}
if ( logLevel >= 2 ) console.log( "Looking for content that should be playing." );
// When the app is first loaded, see if there's a program we should be playing already, and
// if so, begin playback at the appropriate position.
let minutesAgo = 0;
let now = new Date();
timeLoop : while ( minutesAgo < ( 24 * 60 ) ) {
if ( logLevel >= 3 ) console.log( "Checking " + minutesAgo + " minutes ago" );
let adjustedTime = new Date( now.valueOf() - ( minutesAgo * 60 * 1000 ) );
for ( let cron in programming.schedule ) {
if ( cron.indexOf( '* ' ) === 0 ) {
if ( logLevel >= 3 ) console.log( "Skipping cron " + cron );
// Skip anything that is set as the backup for all the time. If there's nothing
// that's supposed to be playing now, it will be found later anyway.
continue;
}
if ( Cron.timeMatchesCron( adjustedTime, cron ) ) {
if ( logLevel >= 2 ) console.log( "Found matching cron: " + cron, programming.schedule[cron] );
// Confirm there's content long enough that it would still be playing.
let nextContent = getNextContentFromCron( cron );
if ( logLevel >= 2 ) console.log( nextContent + " would have played." );
// It doesn't matter if the file is long enough to reach the given duration.
// If it's not, it will end immediately and the 'ended' event will kick in.
programmingQueue.push( {
src: nextContent + '#t=' + ( ( minutesAgo * 60 ) + now.getSeconds() ),
cron : cron
} );
break timeLoop;
}
}
minutesAgo++;
}
queueNextProgramming();
// Remove this button, as it won't be needed again.
$( this ).remove();
} );
/**
* For debugging, when the screen is clicked, fast-forward the current program to its end.
*/
$( '#overlay' ).on( 'click', function () {
if ( logLevel >= 2 ) console.log( "#overlay.click()" );
if ( tvElement.duration ) {
tvElement.currentTime = tvElement.duration;
}
} );
/**
* When something goes wrong, just skip this video and move on.
*/
tv.on( 'error', function ( e ) {
console.log( e );
tv.removeAttr( 'src' );
tv.removeData( 'cron' );
tvElement.ended = true;
clearTimeout( commercialBreakTimerId );
queueNextProgramming();
} );
tv.on( 'ended', function () {
if ( logLevel >= 1 ) console.log( "TV has stopped playing." );
let currentCron = tv.data( 'cron' );
clearTimeout( commercialBreakTimerId );
if ( currentCron in programming.schedule && programming.schedule[ currentCron ].flags.ads ) {
let adBreakScheduled = adBreak();
if ( adBreakScheduled ) {
// adBreak() calls queueNextProgramming() already.
return;
}
}
// @todo If there are less than ~30 seconds until the next non-wildcard programming,
// just show the "We'll be right back graphic.
// Find what to play next.
queueNextProgramming();
} );
tvElement.addEventListener( 'loadeddata', function () {
// Video is loaded and can be played
if ( logLevel >= 2 ) console.log( "tvElement.loadeddata()" );
clearTimeout( commercialBreakTimerId );
// Start watching for ad breaks after 90 seconds.
commercialBreakTimerId = setTimeout( watchForCommercialBreak, 90 * 1000 );
let currentVideoSrc = tv.attr( 'src' ).split( '#t=' )[0];
if ( ! currentVideoSrc ) {
if ( logLevel >= 2 ) console.log( "Couldn't find video source." );
} else {
currentVideoSrc = decodePathParts( currentVideoSrc );
let duration = tvElement.duration;
if ( ! duration ) {
if ( logLevel >= 2 ) console.log( "Couldn't find duration." );
} else {
if ( logLevel >= 2 ) console.log( "The duration of " + currentVideoSrc + " is " + duration + " seconds" );
// It makes life easier knowing the duration before a video plays, so save the duration in case we didn't pre-calculate it.
let durationSaved = saveDuration( currentVideoSrc, duration );
if ( logLevel >= 2 ) console.log( "Saved duration of " + currentVideoSrc + "; return value was " + durationSaved );
}
}
if ( tv.attr( 'src' ).indexOf( '#t=' ) !== -1 ) {
let timestamp = parseFloat( tv.attr( 'src' ).split( '#t=' )[1] );
if ( timestamp < 0 ) {
if ( logLevel >= 2 ) console.log( "The timestamp is negative: " + timestamp );
// When given a negative timstamp, seek to that many seconds from the end.
let secondsFromEnd = timestamp * -1;
if ( tvElement.duration ) {
let seekToSeconds = tvElement.duration - secondsFromEnd;
if ( logLevel >= 2 ) console.log( "Seeking to " + seekToSeconds );
tvElement.currentTime = seekToSeconds;
} else {
if ( logLevel >= 2 ) console.log( "No duration available, so we can't seek." );
}
}
}
}, false );
/**
* Watch for moments during programming when the screen fades to black, and use that as an
* opportunity for a commercial break, if applicable.
*/
function watchForCommercialBreak() {
if ( logLevel >= 3 ) console.log( 'watchForCommercialBreak()' );
clearTimeout( commercialBreakTimerId );
if ( ! context ) {
if ( logLevel >= 2 ) console.log( "OffscreenCanvas is not available, so bailing from watchForCommercialBreak()" );
return;
}
// If the video isn't playing, don't loop
if ( tvElement.paused || tvElement.ended ) {
if ( logLevel >= 3 ) console.log( "paused or ended: " + tvElement.paused + "/" +tvElement.ended );
consecutiveDarkFrames = 0;
return false;
}
if ( tv.data( 'cron' ) === 'ads' ) {
// Don't do an ad break during ads. :)
consecutiveDarkFrames = 0;
return;
}
if ( tvElement.duration ) {
// Leave more time between ad breaks in longer programming.
if ( tvElement.duration < 60 * 30 ) {
minimumSecondsBetweenAdBreaks = 60 * 8;
} else if ( tvElement.duration < 60 * 60 ) {
minimumSecondsBetweenAdBreaks = 60 * 12;
} else {
minimumSecondsBetweenAdBreaks = 60 * 20;
}
if ( logLevel >= 3 ) console.log( "minimumSecondsBetweenAdBreaks: " + minimumSecondsBetweenAdBreaks );
}
// Draw the current frame of the video onto the hidden canvas
context.drawImage( tvElement, 0, 0, canvas.width, canvas.height );
// Pull the image data from the canvas
let frame_data = context.getImageData( 0, 0, canvas.width, canvas.height ).data;
// Get the length of the data, divide that by 4 to get the number of pixels
// then divide that by 4 again so we check the color of every 4th pixel
let frame_data_length = (frame_data.length / 4);
let pixel_count = 0;
let rgb_sums = [0, 0, 0];
for(let i = 0; i < frame_data_length; i += 4 ){
rgb_sums[0] += frame_data[i];
rgb_sums[1] += frame_data[i+1];
rgb_sums[2] += frame_data[i+2];
pixel_count++;
}
// Average the rgb sums to get the average color of the frame in rgb
rgb_sums[0] = Math.floor(rgb_sums[0]/pixel_count);
rgb_sums[1] = Math.floor(rgb_sums[1]/pixel_count);
rgb_sums[2] = Math.floor(rgb_sums[2]/pixel_count);
let averageSaturation = ( ( rgb_sums[0] + rgb_sums[1] + rgb_sums[2] ) / 3 );
if ( averageSaturation < 10 ) {
consecutiveDarkFrames++;
if ( consecutiveDarkFrames >= 8 ) {
let now = new Date();
// Possible ad break.
// @todo This seems to trigger right when something starts playing.
if ( logLevel >= 2 ) console.log( "Now would be a great time for an ad break!" );
consecutiveDarkFrames = 0;
if ( ( now - lastAdBreak ) < ( minimumSecondsBetweenAdBreaks * 1000 ) ) {
if ( logLevel >= 2 ) console.log( "But it's too soon since last ad break (" + ( ( new Date() ) - lastAdBreak ) / 1000 + ") seconds" );
commercialBreakTimerId = setTimeout( watchForCommercialBreak, 5 * 1000 );
} else {
let tookAdBreak = adBreak();
if ( ! tookAdBreak ) {
// If we didn't take an ad break, allow the commercial break check to keep going.
commercialBreakTimerId = setTimeout( watchForCommercialBreak, 5 * 1000 );
}
}
return;
}
} else {
consecutiveDarkFrames = 0;
}
commercialBreakTimerId = setTimeout( watchForCommercialBreak, 100 );
}
/**
* Potentially take a break for some ads.
*
* @return bool Whether a break was scheduled and taken.
*/
function adBreak() {
if ( logLevel >= 2 ) console.log( "adBreak()" );
let currentCron = tv.data( 'cron' );
if ( ! programming.schedule[ currentCron ].flags.ads ) {
// Ads are disabled right now.
return;
}
// Pick some ads.
programmingQueue = [];
// How much time is left in this program?
let secondsUntil = secondsUntilNextProgram();
if ( logLevel >= 2 ) console.log( "There are " + secondsUntil + " seconds until the next program." );
let secondsLeft = secondsLeftInCurrentProgram();
if ( logLevel >= 2 ) console.log( "There are " + secondsLeft + " seconds left in the current program." );
if (
secondsLeft < secondsUntil // A program might be longer than the slot it's scheduled for.
&& secondsUntil > 0 // The next program might be beginning right now.
&& secondsLeft !== false
&& ( secondsLeft > 90 || secondsLeft === 0 ) // And don't schedule anything in the last 90 seconds of a program, but right after is ok.
) {
let timeForAds = secondsUntil - secondsLeft;
let maxAds = 1000;
if ( secondsLeft > 0 ) {
if ( logLevel >= 2 ) console.log( "Scheduling a max of 5 ads." );
// In the middle of a program, play max 5 ads.
maxAds = 5;
} else {
let now = new Date();
// Don't schedule ads past the next half-hour mark.
timeForAds = Math.min(
timeForAds,
( ( 30 - ( now.getMinutes() % 30 ) ) * 60 ) - now.getSeconds()
);
if ( logLevel >= 2 ) console.log( "Scheduling a max of " + timeForAds + " seconds of ads, which brings us to the next half hour." );
}
if ( logLevel >= 2 ) console.log( "Allotting " + timeForAds + " seconds for ads." );
let adsThatMatch = [];
for ( let file in programming.ad_index ) {
if ( file.indexOf( programming.schedule[ currentCron ].flags.ads ) !== 0 ) {
continue;
}
adsThatMatch.push( file );
}
if ( logLevel >= 2 ) console.log( "adsThatMatch: ", adsThatMatch );
let totalAdTime = 0;
for ( let i = 0; i < maxAds; i++ ) {
let randomIndex = Math.floor( Math.random() * adsThatMatch.length );
if ( logLevel >= 2 ) console.log( "randomIndex and adsThatMatch.length: ", randomIndex, adsThatMatch.length );
let randomAd = adsThatMatch[ randomIndex ];
if ( logLevel >= 2 ) console.log( "randomIndex: ", randomIndex );
let adDuration = getDuration( randomAd );
if ( false === adDuration ) {
if ( logLevel >= 2 ) console.log( "No duration available for " + randomAd + "; assuming one minute." );
adDuration = 60;
}
if ( totalAdTime + adDuration > timeForAds ) {
if ( logLevel >= 2 ) console.log( "Finished scheduling " + totalAdTime + " seconds of ad time." );
break;
}
if ( adDuration > timeForAds ) {
if ( logLevel >= 2 ) console.log( "This ad (" + randomAd + ") is longer (" + adDuration + ") than the total time for ads (" + timeForAds + "), so skipping it and ending ad schedule." );
break;
}
totalAdTime += adDuration;
if ( logLevel >= 2 ) console.log( "Queueing ad " + randomAd + " (" + adDuration + " seconds)" );
programmingQueue.push( {
src: randomAd,
cron : 'ads',
secondsUntil : secondsUntil,
secondsLeft : secondsLeft,
timeForAds : timeForAds,
now : new Date()
} );
}
} else {
if ( logLevel >= 2 ) console.log( "Skipping ad scheduling due to secondsUntil/secondsLeft: " + secondsUntil + "/" + secondsLeft );
}
// If we scheduled any ads, pause the current show and add the rest of it to the queue.
if ( programmingQueue.length > 0 ) {
lastAdBreak = new Date();
// Add the rest of the current program to the queue.
programmingQueue.push( {
src : decodePathParts( tv.attr( 'src' ).split( '#t=' )[0] ) + '#t=' + tvElement.currentTime,
cron : currentCron
} );
if ( logLevel >= 2 ) console.log( "programmingQueue: ", programmingQueue );
tvElement.pause();
// Now start playing them.
queueNextProgramming();
return true;
} else {
return false;
}
}
function queueNextProgramming() {
if ( logLevel >= 2 ) console.log( "queueNextProgramming()" );
clearTimeout( nextProgrammingTimerId );
let now = new Date();
let secondsUntilNextMinute = ( 60 - now.getSeconds() );
if ( programmingQueue.length > 0 ) {
if ( logLevel >= 2 ) console.log( "Choosing from the programming queue:", programmingQueue );
let nextProgram = programmingQueue.shift();
let nextFileToPlay = nextProgram.src;
let cron = nextProgram.cron;
saveLastPlay( cron, nextFileToPlay );
tv.data( 'cron', cron );
play( nextFileToPlay );
// If this cron pattern matches every minute, this content should end if something else should be run.
if ( cron.indexOf( '* ' ) === 0 ) {
if ( logLevel >= 2 ) console.log( "Wildcard programming is playing; checking for more important content in " + secondsUntilNextMinute + " seconds." );
// We could skip setting this timer if the content duration is less than a minute, but
// I think it simplifies the logic if there's always a timer waiting during wildcard programming.
nextProgrammingTimerId = setTimeout( queueNextProgramming, ( secondsUntilNextMinute + 1 ) * 1000 );
}
return;
}
let cronForThisMinute = false;
for ( cron in programming.schedule ) {
if ( Cron.timeMatchesCron( now, cron ) ) {
if ( cron.indexOf( '* ' ) === 0 && tv.data( 'cron' ) && ! tvElement.ended ) {
if ( logLevel >= 2 ) console.log( "Found matching wildcard cron (" + cron + ") but skipping because something is playing." );
} else {
if ( logLevel >= 2 ) console.log( "Found matching cron: " + cron, programming.schedule[cron] );
cronForThisMinute = cron;
break;
}
} else {
if ( logLevel >= 2 ) console.log( cron + " doesn't match this minute (" + now + ")" );
}
}
let nextFileToPlay = false;
if ( cronForThisMinute ) {
nextFileToPlay = getNextContentFromCron( cron );
if ( tv.attr( 'src' ) && nextFileToPlay ) {
if ( tv.attr( 'src' ).indexOf( nextFileToPlay ) == 0 ) {
if ( logLevel >= 2 ) console.log( "Don't play the same file twice in a row in the same minute." );
nextFileToPlay = false;
}
}
}
if ( nextFileToPlay ) {
saveLastPlay( cron, nextFileToPlay );
tv.data( 'cron', cron );
play( nextFileToPlay );
// If this cron pattern matches every minute, this content should end if something else should be run.
if ( cron.indexOf( '* ' ) === 0 ) {
if ( logLevel >= 2 ) console.log( "Wildcard programming is playing; checking for more important content in " + secondsUntilNextMinute + " seconds." );
// We could skip setting this timer if the content duration is less than a minute, but
// I think it simplifies the logic if there's always a timer waiting during wildcard programming.
nextProgrammingTimerId = setTimeout( queueNextProgramming, ( secondsUntilNextMinute + 1 ) * 1000 );
}
} else {
if ( logLevel >= 2 ) console.log( "No cron found for this minute (" + now + "); trying again in " + secondsUntilNextMinute + " seconds." );
// The TV might be currently playing right now; if it is, it's wildcard programming, and we should let it keep going.
if ( tvElement.ended ) {
tv.removeData( 'cron' );
tv.removeAttr( 'src' );
tvElement.load();
}
if ( ! tv.data( 'cron' ) ) {
$( '#right-back' ).show();
}
nextProgrammingTimerId = setTimeout( queueNextProgramming, ( secondsUntilNextMinute + 1 ) * 1000 );
}
}
/**
* Play a given file in the video player.
*/
function play( path ) {
if ( logLevel >= 1 ) console.log( "play( " + path + " )" );
clearTimeout( chyronTimerId );
$( '#right-back' ).hide();
tv.removeAttr( 'src' );
tv.empty();
tv.attr( 'src', encodePathParts( path ) );
addCaptions( path );
setCaptionStatus();
tvElement.load();
// For non-ad content, show a "now playing: " chyron.
if ( options.showNowPlaying && tv.data( 'cron' ) != 'ads' ) {
let displayName = path;
// Get rid of the timestamp.
let displayNameParts = displayName.split( '#t=' );
if ( displayNameParts.length > 1 ) {
displayNameParts.pop();
}
displayName = displayNameParts.join( '#t=' );
// Get rid of the extension.
displayNameParts = displayName.split( '.' );
if ( displayNameParts.length > 1 ) {
displayNameParts.pop();
}
displayName = displayNameParts.join( '.' );
// Get the filename part.
displayNameParts = displayName.split( '/' );
displayName = displayNameParts.pop();
displayName = displayName.replace( /\(.*?\)/, '' );
displayName = displayName.trim();
$( '#chyron-banner' ).text( displayName );
$( '#overlay' ).addClass( 'chyroned' );
}
tvElement.play().then( function () {
chyronTimerId = setTimeout( function () {
$( '#overlay' ).animate( { opacity: 0 }, 1000, function () {
$( '#overlay' ).removeClass( 'chyroned' ).css( 'opacity', 1 );
} );
}, 5000 );
} ).catch( function ( e ) {
// Probably a file that can't be loaded or doesn't exist.
console.log( "tvElement.play() exception: ", e );
tv.removeAttr( 'src' );
tv.removeData( 'cron' );
tvElement.ended = true;
clearTimeout( commercialBreakTimerId );
queueNextProgramming();
} );
}
function addCaptions( path ) {
if ( logLevel >= 1 ) console.log( "addCaptions(" + path + ")" );
tv.empty();
let captions = getCaptions( path );
if ( captions ) {
let track = $( '<track/>' )
.attr( 'kind', 'captions' )
.attr( 'src', encodePathParts( captions ) )
.attr( 'default', 'default' );
tv.append( track );
if ( logLevel >= 1 ) console.log( "added captions" );
} else {
if ( logLevel >= 1 ) console.log( "There were no captions to add." );
}
}
function setCaptionStatus() {
let currentCron = tv.data( 'cron' );
if ( 'textTracks' in tvElement ) {
if ( logLevel >= 2 ) console.log( "There are " + tvElement.textTracks.length + " text tracks." );
for ( let i = 0; i < tvElement.textTracks.length; i++ ) {
if ( captionOverride || ( currentCron in programming.schedule && programming.schedule[ currentCron ].flags.captions ) ) {
if ( logLevel >= 2 ) console.log( "Enabling text track " + i );
tvElement.textTracks[i].mode = 'showing';
} else {
if ( logLevel >= 2 ) console.log( "Disabling text track " + i );
tvElement.textTracks[i].mode = 'hidden';
}
}
} else {
if ( logLevel >= 1 ) console.log( "This browser does not support textTracks." );
}
}
/**
* Given a cron pattern, find the next video that should be played.
* For a file or URL, it's the file or URL. For a directory, it's the video that comes alphabetically, or a random one if shuffle is enabled.
*
* @param string cron A cron pattern, like "0 10 * * *"
* @return string|bool Either a path to a video or false if none is available.
*/
function getNextContentFromCron( cron ) {
if ( logLevel >= 2 ) console.log( "getNextContentFromCron( " + cron + " )" );
let programmingDetails = programming.schedule[cron];
let potentialFiles = [];
for ( let i = 0; i < programming.schedule[cron].content.length; i++ ){
if ( 'url' in programming.schedule[cron].content[i] ) {
if ( potentialFiles.indexOf( programming.schedule[cron].content[i].url ) == -1 ) {
potentialFiles.push( programming.schedule[cron].content[i].url );
}
} else if ( 'file' in programming.schedule[cron].content[i] ) {
if ( potentialFiles.indexOf( programming.schedule[cron].content[i].file ) == -1 ) {
potentialFiles.push( programming.schedule[cron].content[i].file );
}
} else if ( 'dir' in programming.schedule[cron].content[i] ) {
for ( let file in programming.content_index ) {
if ( file.indexOf( programming.schedule[cron].content[i].dir ) !== 0 ) {
if ( file > programming.schedule[cron].content[i].dir ) {
// We've passed all of the relevant files, so we can quit.
break;
} else {
continue;
}
}
if ( potentialFiles.indexOf( file ) == -1 ) {
potentialFiles.push( file );
}
}
} else {
alert( "Unknown programming type: " + JSON.stringify( programming.schedule[cron].content[i] ) );
}
}
if ( logLevel >= 2 ) console.log( "Potential files for " + cron + ": ", potentialFiles );
if ( programming.schedule[cron].flags.shuffle ) {
if ( logLevel >= 2 ) console.log( "shuffle is enabled" );
// Pick a random one.
let randomIndex = Math.floor( Math.random() * potentialFiles.length );
return potentialFiles[ randomIndex ];
} else {
if ( logLevel >= 2 ) console.log( "shuffle is disabled" );
potentialFiles.sort();
let lastPlayedContent = getLastPlay( cron );
if ( logLevel >= 2 ) console.log( "The last played file for " + cron + " was " + lastPlayedContent );
let itsTheNextOne = false;
let firstContent = false;
for ( let i = 0; i < potentialFiles.length; i++ ) {
let file = potentialFiles[i];
if ( ! lastPlayedContent ) {
if ( logLevel >= 2 ) console.log( "No content has been played for this cron, returning " + file );
return file;
}
if ( ! firstContent ) {
// Just in case we need it.
firstContent = file;
}
if ( itsTheNextOne ) {
if ( logLevel >= 2 ) console.log( "Returning the next one: " + file );
return file;
} else if ( file == lastPlayedContent ) {
if ( logLevel >= 2 ) console.log( "Found " + lastPlayedContent + " in list, returning the next one." );
itsTheNextOne = true;
}
}
if ( logLevel >= 2 ) console.log( "Never found " + lastPlayedContent + " in the file list (or it was the last one); returning the first one: " + firstContent );
return firstContent;
}
if ( logLevel >= 2 ) console.log( "Found no next content from cron " + cron );
return false;
}
/**
* Given a cron pattern, find the previous video that would have played.
* For a file or URL, it's the file or URL. For a directory, it's the video that comes prior alphabetically, or a random one if shuffle is enabled.
*
* @param string cron A cron pattern, like "0 10 * * *"
* @return string|bool Either a path to a video or false if none is available.
*/
function getPreviousContentFromCron( cron ) {
if ( logLevel >= 2 ) console.log( "getPreviousContentFromCron( " + cron + " )" );
let programmingDetails = programming.schedule[cron];
let potentialFiles = [];
for ( let i = 0; i < programming.schedule[cron].content.length; i++ ){
if ( 'url' in programming.schedule[cron].content[i] ) {
if ( potentialFiles.indexOf( programming.schedule[cron].content[i].url ) == -1 ) {
potentialFiles.push( programming.schedule[cron].content[i].url );
}
} else if ( 'file' in programming.schedule[cron].content[i] ) {
if ( potentialFiles.indexOf( programming.schedule[cron].content[i].file ) == -1 ) {
potentialFiles.push( programming.schedule[cron].content[i].file );
}
} else if ( 'dir' in programming.schedule[cron].content[i] ) {
for ( let file in programming.content_index ) {
if ( file.indexOf( programming.schedule[cron].content[i].dir ) !== 0 ) {
if ( file > programming.schedule[cron].content[i].dir ) {
// We've passed all of the relevant files, so we can quit.
break;
} else {
continue;
}
}
if ( potentialFiles.indexOf( file ) == -1 ) {
potentialFiles.push( file );
}
}
} else {
alert( "Unknown programming type: " + JSON.stringify( programming.schedule[cron].content[i] ) );
}
}
if ( logLevel >= 2 ) console.log( "Potential files for " + cron + ": ", potentialFiles );
if ( programming.schedule[cron].flags.shuffle ) {
if ( logLevel >= 2 ) console.log( "shuffle is enabled" );
// Pick a random one.
let randomIndex = Math.floor( Math.random() * potentialFiles.length );
return potentialFiles[ randomIndex ];
} else {
if ( logLevel >= 2 ) console.log( "shuffle is disabled" );
potentialFiles.sort();
potentialFiles.reverse();
let lastPlayedContent = getLastPlay( cron );
if ( logLevel >= 2 ) console.log( "The last played file for " + cron + " was " + lastPlayedContent );
let itsTheNextOne = false;
let firstContent = false;
for ( let i = 0; i < potentialFiles.length; i++ ) {
let file = potentialFiles[i];
if ( ! lastPlayedContent ) {
if ( logLevel >= 2 ) console.log( "No content has been played for this cron, returning " + file );
return file;
}
if ( ! firstContent ) {
// Just in case we need it.
firstContent = file;
}
if ( itsTheNextOne ) {
if ( logLevel >= 2 ) console.log( "Returning the next one: " + file );
return file;
} else if ( file == lastPlayedContent ) {
if ( logLevel >= 2 ) console.log( "Found " + lastPlayedContent + " in list, returning the next one." );
itsTheNextOne = true;
}
}
if ( logLevel >= 2 ) console.log( "Never found " + lastPlayedContent + " in the file list (or it was the last one); returning the last one: " + firstContent );
return firstContent;
}
if ( logLevel >= 2 ) console.log( "Found no previous content from cron " + cron );
return false;
}
/**
* Get the stored play history -- a hash keyed by cron pattern containing the last video path that finished (or was interrupted).
*
* @return Object
*/
function getPlayHistory() {
if ( logLevel >= 3 ) console.log( "getPlayHistory()" );
if ( 'playHistory' in localStorage ) {
try {
return JSON.parse( localStorage.playHistory );
} catch ( error ) {
if ( logLevel >= 2 ) console.log( "playHistory was malformed (" + localStorage.playHistory + ")", error );
}
}
return {};
}
/**
* Save the playHistory
*/
function setPlayHistory( history ) {
if ( logLevel >= 3 ) console.log( "setPlayHistory()" );
let stringHistory = null;
try {
stringHistory = JSON.stringify( history );
} catch ( error ) {
if ( logLevel >= 2 ) console.log( "Couldn't stringify playHistory", error );
stringHistory = JSON.stringify( {} );
}
localStorage.setItem( 'playHistory', stringHistory );
}
function saveLastPlay( cron, file ) {
file = file.split( '#t=' )[0]; // Remove any timestamp.
let playHistory = getPlayHistory();
playHistory[cron] = file;
setPlayHistory( playHistory );
}
function getLastPlay( cron ) {
let playHistory = getPlayHistory();
let lastPlayedContent = false;
if ( cron in playHistory ) {
return playHistory[cron];
}
return false;
}
/**
* Find the duration, in seconds, of a given file.
*
* @return int|bool The duration, or false if not available.
*/
function getDuration( filePath ) {
if ( logLevel >= 2 ) console.log( "getDuration(" + filePath + ")" );
if ( filePath in programming.content_index ) {
if ( logLevel >= 2 ) console.log( "Found " + filePath + " in content_index" );
if ( 'duration' in programming.content_index[ filePath ] ) {
return programming.content_index[ filePath ].duration;
} else {
if ( logLevel >= 2 ) console.log( "But didn't find duration in programming.content_index[ filePath ]" );
}
}
if ( filePath in programming.ad_index ) {
if ( logLevel >= 2 ) console.log( "Found " + filePath + " in ad_index" );
if ( 'duration' in programming.ad_index[ filePath ] ) {
return programming.ad_index[ filePath ].duration;
} else {
if ( logLevel >= 2 ) console.log( "But didn't find duration in programming.ad_index[ filePath ]" );
}
}
if ( logLevel >= 2 ) console.log( "Could not find " + filePath + " with duration in content_index or ad_index" );
if ( 'durations' in localStorage ) {
let durations = JSON.parse( localStorage.durations );
if ( filePath in durations ) {
if ( logLevel >= 2 ) console.log( "Found duration in localStorage: " + durations[ filePath ] );
return durations[ filePath ];
}
}
return false;
}
/**
* If a caption file is available for a file, return its path.
*
* @return int|bool The duration, or false if not available.
*/
function getCaptions( filePath ) {
if ( logLevel >= 2 ) console.log( "getCaptions(" + filePath + ")" );
if ( filePath in programming.content_index ) {
if ( 'captions' in programming.content_index[ filePath ] ) {
return programming.content_index[ filePath ].captions;
}
} else if ( filePath in programming.ad_index ) {
if ( 'captions' in programming.ad_index[ filePath ] ) {
return programming.ad_index[ filePath ].captions;
}
}
return false;
}
/**
* Save the duration of a file that didn't have a stored duration in programming.js