forked from googleads/videojs-ima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
807 lines (672 loc) · 19.8 KB
/
controller.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
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* IMA SDK integration plugin for Video.js. For more information see
* https://www.github.com/googleads/videojs-ima
*/
import PlayerWrapper from './player-wrapper.js';
import AdUi from './ad-ui.js';
import SdkImpl from './sdk-impl.js';
/**
* The grand coordinator of the plugin. Facilitates communication between all
* other plugin classes.
*
* @param {Object} player Instance of the video.js player.
* @param {Object} options Options provided by the implementation.
* @constructor
* @struct
* @final
*/
const Controller = function(player, options) {
/**
* Stores user-provided settings.
* @type {Object}
*/
this.settings = {};
/**
* Content and ads ended listeners passed by the publisher to the plugin.
* These will be called when the plugin detects that content *and all
* ads* have completed. This differs from the contentEndedListeners in that
* contentEndedListeners will fire between content ending and a post-roll
* playing, whereas the contentAndAdsEndedListeners will fire after the
* post-roll completes.
*/
this.contentAndAdsEndedListeners = [];
/**
* Whether or not we are running on a mobile platform.
*/
this.isMobile = (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i) ||
navigator.userAgent.match(/Android/i));
/**
* Whether or not we are running on an iOS platform.
*/
this.isIos = (navigator.userAgent.match(/iPhone/i) ||
navigator.userAgent.match(/iPad/i));
this.initWithSettings(options);
/**
* Stores contrib-ads default settings.
*/
const contribAdsDefaults = {
debug: this.settings.debug,
timeout: this.settings.timeout,
prerollTimeout: this.settings.prerollTimeout,
};
const adsPluginSettings = this.extend(
{}, contribAdsDefaults, options.contribAdsSettings || {});
this.playerWrapper = new PlayerWrapper(player, adsPluginSettings, this);
this.adUi = new AdUi(this);
this.sdkImpl = new SdkImpl(this);
};
Controller.IMA_DEFAULTS = {
adLabel: 'Advertisement',
adLabelNofN: 'of',
debug: false,
disableAdControls: false,
prerollTimeout: 1000,
preventLateAdStart: false,
requestMode: 'onLoad',
showControlsForJSAds: true,
timeout: 5000,
};
/**
* Extends the settings to include user-provided settings.
*
* @param {Object} options Options to be used in initialization.
*/
Controller.prototype.initWithSettings = function(options) {
this.settings = this.extend({}, Controller.IMA_DEFAULTS, options || {});
this.warnAboutDeprecatedSettings();
// Default showing countdown timer to true.
this.showCountdown = true;
if (this.settings.showCountdown === false) {
this.showCountdown = false;
}
};
/**
* Logs console warnings when deprecated settings are used.
*/
Controller.prototype.warnAboutDeprecatedSettings = function() {
const deprecatedSettings = [
'adWillAutoplay',
'adsWillAutoplay',
'adWillPlayMuted',
'adsWillPlayMuted',
];
deprecatedSettings.forEach((setting) => {
if (this.settings[setting] !== undefined) {
console.warn(
'WARNING: videojs.ima setting ' + setting + ' is deprecated');
}
});
};
/**
* Return the settings object.
*
* @return {Object} The settings object.
*/
Controller.prototype.getSettings = function() {
return this.settings;
};
/**
* Return whether or not we're in a mobile environment.
*
* @return {boolean} True if running on mobile, false otherwise.
*/
Controller.prototype.getIsMobile = function() {
return this.isMobile;
};
/**
* Return whether or not we're in an iOS environment.
*
* @return {boolean} True if running on iOS, false otherwise.
*/
Controller.prototype.getIsIos = function() {
return this.isIos;
};
/**
* Inject the ad container div into the DOM.
*
* @param{HTMLElement} adContainerDiv The ad container div.
*/
Controller.prototype.injectAdContainerDiv = function(adContainerDiv) {
this.playerWrapper.injectAdContainerDiv(adContainerDiv);
};
/**
* @return {HTMLElement} The div for the ad container.
*/
Controller.prototype.getAdContainerDiv = function() {
return this.adUi.getAdContainerDiv();
};
/**
* @return {Object} The content player.
*/
Controller.prototype.getContentPlayer = function() {
return this.playerWrapper.getContentPlayer();
};
/**
* Returns the content playhead tracker.
*
* @return {Object} The content playhead tracker.
*/
Controller.prototype.getContentPlayheadTracker = function() {
return this.playerWrapper.getContentPlayheadTracker();
};
/**
* Requests ads.
*/
Controller.prototype.requestAds = function() {
this.sdkImpl.requestAds();
};
/**
* Add or modify a setting.
*
* @param {string} key Key to modify
* @param {Object} value Value to set at key.
*/
Controller.prototype.setSetting = function(key, value) {
this.settings[key] = value;
};
/**
* Called when there is an error loading ads.
*
* @param {Object} adErrorEvent The ad error event thrown by the IMA SDK.
*/
Controller.prototype.onErrorLoadingAds = function(adErrorEvent) {
this.adUi.onAdError();
this.playerWrapper.onAdError(adErrorEvent);
};
/**
* Called by the ad UI when the play/pause button is clicked.
*/
Controller.prototype.onAdPlayPauseClick = function() {
if (this.sdkImpl.isAdPlaying()) {
this.adUi.onAdsPaused();
this.sdkImpl.pauseAds();
} else {
this.adUi.onAdsPlaying();
this.sdkImpl.resumeAds();
}
};
/**
* Called by the ad UI when the mute button is clicked.
*
*/
Controller.prototype.onAdMuteClick = function() {
if (this.sdkImpl.isAdMuted()) {
this.playerWrapper.unmute();
this.adUi.unmute();
this.sdkImpl.unmute();
} else {
this.playerWrapper.mute();
this.adUi.mute();
this.sdkImpl.mute();
}
};
/**
* Set the volume of the player and ads. 0-1.
*
* @param {number} volume The new volume.
*/
Controller.prototype.setVolume = function(volume) {
this.playerWrapper.setVolume(volume);
this.sdkImpl.setVolume(volume);
};
/**
* @return {number} The volume of the content player.
*/
Controller.prototype.getPlayerVolume = function() {
return this.playerWrapper.getVolume();
};
/**
* Toggle fullscreen state.
*/
Controller.prototype.toggleFullscreen = function() {
this.playerWrapper.toggleFullscreen();
};
/**
* Relays ad errors to the player wrapper.
*
* @param {Object} adErrorEvent The ad error event thrown by the IMA SDK.
*/
Controller.prototype.onAdError = function(adErrorEvent) {
this.adUi.onAdError();
this.playerWrapper.onAdError(adErrorEvent);
};
/**
* Handles ad break starting.
*
* @param {Object} adEvent The event fired by the IMA SDK.
*/
Controller.prototype.onAdBreakStart = function(adEvent) {
this.playerWrapper.onAdBreakStart();
this.adUi.onAdBreakStart(adEvent);
};
/**
* Show the ad container.
*/
Controller.prototype.showAdContainer = function() {
this.adUi.showAdContainer();
};
/**
* Handles ad break ending.
*/
Controller.prototype.onAdBreakEnd = function() {
this.playerWrapper.onAdBreakEnd();
this.adUi.onAdBreakEnd();
};
/**
* Handles when all ads have finished playing.
*/
Controller.prototype.onAllAdsCompleted = function() {
this.adUi.onAllAdsCompleted();
this.playerWrapper.onAllAdsCompleted();
};
/**
* Handles the SDK firing an ad paused event.
*/
Controller.prototype.onAdsPaused = function() {
this.adUi.onAdsPaused();
};
/**
* Handles the SDK firing an ad resumed event.
*/
Controller.prototype.onAdsResumed = function() {
this.adUi.onAdsResumed();
};
/**
* Takes data from the sdk impl and passes it to the ad UI to update the UI.
*
* @param {number} currentTime Current time of the ad.
* @param {number} remainingTime Remaining time of the ad.
* @param {number} duration Duration of the ad.
* @param {number} adPosition Index of the ad in the pod.
* @param {number} totalAds Total number of ads in the pod.
*/
Controller.prototype.onAdPlayheadUpdated =
function(currentTime, remainingTime, duration, adPosition, totalAds) {
this.adUi.updateAdUi(
currentTime, remainingTime, duration, adPosition, totalAds);
};
/**
* Handles ad log messages.
* @param {google.ima.AdEvent} adEvent The AdEvent thrown by the IMA SDK.
*/
Controller.prototype.onAdLog = function(adEvent) {
this.playerWrapper.onAdLog(adEvent);
};
/**
* @return {Object} The current ad.
*/
Controller.prototype.getCurrentAd = function() {
return this.sdkImpl.getCurrentAd();
};
/**
* Play content.
*/
Controller.prototype.playContent = function() {
this.playerWrapper.play();
};
/**
* Handles when a linear ad starts.
*/
Controller.prototype.onLinearAdStart = function() {
this.adUi.onLinearAdStart();
this.playerWrapper.onAdStart();
};
/**
* Handles when a non-linear ad loads.
*/
Controller.prototype.onNonLinearAdLoad = function() {
this.adUi.onNonLinearAdLoad();
};
/**
* Handles when a non-linear ad starts.
*/
Controller.prototype.onNonLinearAdStart = function() {
this.adUi.onNonLinearAdLoad();
this.playerWrapper.onAdStart();
};
/**
* Get the player width.
*
* @return {number} The width of the player.
*/
Controller.prototype.getPlayerWidth = function() {
return this.playerWrapper.getPlayerWidth();
};
/**
* Get the player height.
*
* @return {number} The height of the player.
*/
Controller.prototype.getPlayerHeight = function() {
return this.playerWrapper.getPlayerHeight();
};
/**
* Tells the player wrapper that ads are ready.
*/
Controller.prototype.onAdsReady = function() {
this.playerWrapper.onAdsReady();
};
/**
* Called when the player wrapper detects that the player has been resized.
*
* @param {number} width The post-resize width of the player.
* @param {number} height The post-resize height of the player.
*/
Controller.prototype.onPlayerResize = function(width, height) {
this.sdkImpl.onPlayerResize(width, height);
};
/**
* Called by the player wrapper when content completes.
*/
Controller.prototype.onContentComplete = function() {
this.sdkImpl.onContentComplete();
};
/**
* Called by the player wrapper when it's time to play a post-roll but we don't
* have one to play.
*/
Controller.prototype.onNoPostroll = function() {
this.playerWrapper.onNoPostroll();
};
/**
* Called when content and all ads have completed.
*/
Controller.prototype.onContentAndAdsCompleted = function() {
for (let index in this.contentAndAdsEndedListeners) {
if (typeof this.contentAndAdsEndedListeners[index] === 'function') {
this.contentAndAdsEndedListeners[index]();
}
}
};
/**
* Called when the player is disposed.
*/
Controller.prototype.onPlayerDisposed = function() {
this.contentAndAdsEndedListeners = [];
this.sdkImpl.onPlayerDisposed();
};
/**
* Called when the player is ready to play a pre-roll.
*/
Controller.prototype.onPlayerReadyForPreroll = function() {
this.sdkImpl.onPlayerReadyForPreroll();
};
/**
* Called if the ad times out.
*/
Controller.prototype.onAdTimeout = function() {
this.sdkImpl.onAdTimeout();
};
/**
* Called when the player is ready.
*/
Controller.prototype.onPlayerReady = function() {
this.sdkImpl.onPlayerReady();
};
/**
* Called when the player enters fullscreen.
*/
Controller.prototype.onPlayerEnterFullscreen = function() {
this.adUi.onPlayerEnterFullscreen();
this.sdkImpl.onPlayerEnterFullscreen();
};
/**
* Called when the player exits fullscreen.
*/
Controller.prototype.onPlayerExitFullscreen = function() {
this.adUi.onPlayerExitFullscreen();
this.sdkImpl.onPlayerExitFullscreen();
};
/**
* Called when the player volume changes.
*
* @param {number} volume The new player volume.
*/
Controller.prototype.onPlayerVolumeChanged = function(volume) {
this.adUi.onPlayerVolumeChanged(volume);
this.sdkImpl.onPlayerVolumeChanged(volume);
};
/**
* Sets the content of the video player. You should use this method instead
* of setting the content src directly to ensure the proper ad tag is
* requested when the video content is loaded.
* @param {?string} contentSrc The URI for the content to be played. Leave
* blank to use the existing content.
* @param {?string} adTag The ad tag to be requested when the content loads.
* Leave blank to use the existing ad tag.
*/
Controller.prototype.setContentWithAdTag =
function(contentSrc, adTag) {
this.reset();
this.settings.adTagUrl = adTag ? adTag : this.settings.adTagUrl;
this.playerWrapper.changeSource(contentSrc);
};
/**
* Sets the content of the video player. You should use this method instead
* of setting the content src directly to ensure the proper ads response is
* used when the video content is loaded.
* @param {?string} contentSrc The URI for the content to be played. Leave
* blank to use the existing content.
* @param {?string} adsResponse The ads response to be requested when the
* content loads. Leave blank to use the existing ads response.
*/
Controller.prototype.setContentWithAdsResponse =
function(contentSrc, adsResponse) {
this.reset();
this.settings.adsResponse =
adsResponse ? adsResponse : this.settings.adsResponse;
this.playerWrapper.changeSource(contentSrc);
};
/**
* Sets the content of the video player. You should use this method instead
* of setting the content src directly to ensure the proper ads request is
* used when the video content is loaded.
* @param {?string} contentSrc The URI for the content to be played. Leave
* blank to use the existing content.
* @param {?Object} adsRequest The ads request to be requested when the
* content loads. Leave blank to use the existing ads request.
*/
Controller.prototype.setContentWithAdsRequest =
function(contentSrc, adsRequest) {
this.reset();
this.settings.adsRequest =
adsRequest ? adsRequest : this.settings.adsRequest;
this.playerWrapper.changeSource(contentSrc);
};
/**
* Resets the state of the plugin.
*/
Controller.prototype.reset = function() {
this.sdkImpl.reset();
this.playerWrapper.reset();
this.adUi.reset();
};
/**
* Listener JSDoc for ESLint. This listener can be passed to
* (add|remove)ContentEndedListener.
* @callback listener
*/
/**
* Adds a listener for the 'contentended' event of the video player. This should
* be used instead of setting an 'contentended' listener directly to ensure that
* the ima can do proper cleanup of the SDK before other event listeners are
* called.
* @param {listener} listener The listener to be called when content
* completes.
*/
Controller.prototype.addContentEndedListener = function(listener) {
this.playerWrapper.addContentEndedListener(listener);
};
/**
* Adds a listener that will be called when content and all ads have
* finished playing.
* @param {listener} listener The listener to be called when content and ads
* complete.
*/
Controller.prototype.addContentAndAdsEndedListener = function(listener) {
this.contentAndAdsEndedListeners.push(listener);
};
/**
* Sets the listener to be called to trigger manual ad break playback.
* @param {listener} listener The listener to be called to trigger manual ad
* break playback.
*/
Controller.prototype.setAdBreakReadyListener = function(listener) {
this.sdkImpl.setAdBreakReadyListener(listener);
};
/**
* Changes the flag to show or hide the ad countdown timer.
*
* @param {boolean} showCountdownIn Show or hide the countdown timer.
*/
Controller.prototype.setShowCountdown = function(showCountdownIn) {
this.adUi.setShowCountdown(showCountdownIn);
this.showCountdown = showCountdownIn;
this.adUi.countdownDiv.style.display = this.showCountdown ? 'block' : 'none';
};
/**
* Initializes the AdDisplayContainer. On mobile, this must be done as a
* result of user action.
*/
Controller.prototype.initializeAdDisplayContainer = function() {
this.sdkImpl.initializeAdDisplayContainer();
};
/**
* Called by publishers in manual ad break playback mode to start an ad
* break.
*/
Controller.prototype.playAdBreak = function() {
this.sdkImpl.playAdBreak();
};
/**
* Callback JSDoc for ESLint. This callback can be passed to addEventListener.
* @callback callback
*/
/**
* Ads an EventListener to the AdsManager. For a list of available events,
* see
* https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdEvent#.Type
* @param {google.ima.AdEvent.Type} event The AdEvent.Type for which to
* listen.
* @param {callback} callback The method to call when the event is fired.
*/
Controller.prototype.addEventListener = function(event, callback) {
this.sdkImpl.addEventListener(event, callback);
};
/**
* Returns the instance of the AdsManager.
* @return {google.ima.AdsManager} The AdsManager being used by the plugin.
*/
Controller.prototype.getAdsManager = function() {
return this.sdkImpl.getAdsManager();
};
/**
* Returns the instance of the player id.
* @return {string} The player id.
*/
Controller.prototype.getPlayerId = function() {
return this.playerWrapper.getPlayerId();
};
/**
* Changes the ad tag. You will need to call requestAds after this method
* for the new ads to be requested.
* @param {?string} adTag The ad tag to be requested the next time
* requestAds is called.
*/
Controller.prototype.changeAdTag = function(adTag) {
this.reset();
this.settings.adTagUrl = adTag;
};
/**
* Pauses the ad.
*/
Controller.prototype.pauseAd = function() {
this.adUi.onAdsPaused();
this.sdkImpl.pauseAds();
};
/**
* Resumes the ad.
*/
Controller.prototype.resumeAd = function() {
this.adUi.onAdsPlaying();
this.sdkImpl.resumeAds();
};
/**
* Toggles video/ad playback.
*/
Controller.prototype.togglePlayback = function() {
this.playerWrapper.togglePlayback();
};
/**
* @return {boolean} true if we expect that ads will autoplay. false otherwise.
*/
Controller.prototype.adsWillAutoplay = function() {
if (this.settings.adsWillAutoplay !== undefined) {
return this.settings.adsWillAutoplay;
} else if (this.settings.adWillAutoplay !== undefined) {
return this.settings.adWillAutoplay;
} else {
return !!this.playerWrapper.getPlayerOptions().autoplay;
}
};
/**
* @return {boolean} true if we expect that ads will autoplay. false otherwise.
*/
Controller.prototype.adsWillPlayMuted = function() {
if (this.settings.adsWillPlayMuted !== undefined) {
return this.settings.adsWillPlayMuted;
} else if (this.settings.adWillPlayMuted !== undefined) {
return this.settings.adWillPlayMuted;
} else if (this.playerWrapper.getPlayerOptions().muted !== undefined) {
return this.playerWrapper.getPlayerOptions().muted;
} else {
return this.playerWrapper.getVolume() == 0;
}
};
/**
* Triggers an event on the VJS player
* @param {string} name The event name.
* @param {Object} data The event data.
*/
Controller.prototype.triggerPlayerEvent = function(name, data) {
this.playerWrapper.triggerPlayerEvent(name, data);
};
/**
* Extends an object to include the contents of objects at parameters 2 onward.
*
* @param {Object} obj The object onto which the subsequent objects' parameters
* will be extended. This object will be modified.
* @param {...Object} var_args The objects whose properties are to be extended
* onto obj.
* @return {Object} The extended object.
*/
Controller.prototype.extend = function(obj, ...args) {
let arg;
let index;
let key;
for (index = 0; index < args.length; index++) {
arg = args[index];
for (key in arg) {
if (arg.hasOwnProperty(key)) {
obj[key] = arg[key];
}
}
}
return obj;
};
export default Controller;