-
Notifications
You must be signed in to change notification settings - Fork 285
/
dai-controller.js
296 lines (262 loc) · 7.55 KB
/
dai-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
/**
* Copyright 2021 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 SdkImpl from './sdk-impl.js';
/**
* The coordinator for the DAI portion 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 DaiController = function(player, options) {
/**
* If the stream is currently in an ad break.
* @type {boolean}
*/
this.inAdBreak = false;
/**
* Stores user-provided settings.
* @type {Object!}
*/
this.settings = {};
/**
* 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 =
Object.assign({}, contribAdsDefaults, options.contribAdsSettings || {});
this.playerWrapper = new PlayerWrapper(player, adsPluginSettings, this);
this.sdkImpl = new SdkImpl(this);
};
DaiController.IMA_DEFAULTS = {
adLabel: 'Advertisement',
adLabelNofN: 'of',
debug: false,
disableAdControls: false,
showControlsForJSAds: true,
};
/**
* Extends the settings to include user-provided settings.
*
* @param {Object!} options Options to be used in initialization.
*/
DaiController.prototype.initWithSettings = function(options) {
this.settings = Object.assign({}, DaiController.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.
*/
DaiController.prototype.warnAboutDeprecatedSettings = function() {
const deprecatedSettings = [
// Currently no DAI plugin settings are deprecated.
];
deprecatedSettings.forEach((setting) => {
if (this.settings[setting] !== undefined) {
console.warn(
'WARNING: videojs.imaDai setting ' + setting + ' is deprecated');
}
});
};
/**
* Return the settings object.
*
* @return {Object!} The settings object.
*/
DaiController.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.
*/
DaiController.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.
*/
DaiController.prototype.getIsIos = function() {
return this.isIos;
};
/**
* @return {Object!} The html5 player.
*/
DaiController.prototype.getStreamPlayer = function() {
return this.playerWrapper.getStreamPlayer();
};
/**
* @return {Object!} The video.js player.
*/
DaiController.prototype.getVjsPlayer = function() {
return this.playerWrapper.getVjsPlayer();
};
/**
* Requests the stream.
*/
DaiController.prototype.requestStream = function() {
this.sdkImpl.requestStream();
};
/**
* Add or modify a setting.
*
* @param {string} key Key to modify
* @param {Object!} value Value to set at key.
*/
DaiController.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.
*/
DaiController.prototype.onErrorLoadingAds = function(adErrorEvent) {
this.playerWrapper.onAdError(adErrorEvent);
};
/**
* Relays ad errors to the player wrapper.
*
* @param {Object!} adErrorEvent The ad error event thrown by the IMA SDK.
*/
DaiController.prototype.onAdError = function(adErrorEvent) {
this.playerWrapper.onAdError(adErrorEvent);
};
/**
* Signals player that an ad break has started.
*/
DaiController.prototype.onAdBreakStart = function() {
this.inAdBreak = true;
this.playerWrapper.onAdBreakStart();
};
/**
* Signals player that an ad break has ended.
*/
DaiController.prototype.onAdBreakEnd = function() {
this.inAdBreak = false;
this.playerWrapper.onAdBreakEnd();
};
/**
* Called when the player is disposed.
*/
DaiController.prototype.onPlayerDisposed = function() {
this.contentAndAdsEndedListeners = [];
this.sdkImpl.onPlayerDisposed();
};
/**
* Returns if the stream is currently in an ad break.
* @return {boolean} If the stream is currently in an ad break.
*/
DaiController.prototype.isInAdBreak = function() {
return this.inAdBreak;
};
/**
* Called on seek end to check for ad snapback.
* @param {number} currentTime the current time of the stream.
*/
DaiController.prototype.onSeekEnd = function(currentTime) {
this.sdkImpl.onSeekEnd(currentTime);
};
/**
* Called when the player is ready.
*/
DaiController.prototype.onPlayerReady = function() {
this.sdkImpl.onPlayerReady();
};
/**
* Resets the state of the plugin.
*/
DaiController.prototype.reset = function() {
this.sdkImpl.reset();
this.playerWrapper.reset();
};
/**
* Adds an EventListener to the StreamManager. For a list of available events,
* see
* https://developers.google.com/interactive-media-ads/docs/sdks/html5/dai/reference/js/StreamEvent
* @param {google.ima.StreamEvent.Type!} event The AdEvent.Type for which to
* listen.
* @param {callback!} callback The method to call when the event is fired.
*/
DaiController.prototype.addEventListener = function(event, callback) {
this.sdkImpl.addEventListener(event, callback);
};
/**
* Returns the instance of the StreamManager.
* @return {google.ima.StreamManager!} The StreamManager being used by the
* plugin.
*/
DaiController.prototype.getStreamManager = function() {
return this.sdkImpl.getStreamManager();
};
/**
* Returns the instance of the player id.
* @return {string} The player id.
*/
DaiController.prototype.getPlayerId = function() {
return this.playerWrapper.getPlayerId();
};
/**
* @return {boolean} true if we expect that the stream will autoplay. false
* otherwise.
*/
DaiController.prototype.streamWillAutoplay = function() {
if (this.settings.streamWillAutoplay !== undefined) {
return this.settings.streamWillAutoplay;
} else {
return !!this.playerWrapper.getPlayerOptions().autoplay;
}
};
/**
* Triggers an event on the VJS player
* @param {string} name The event name.
* @param {Object!} data The event data.
*/
DaiController.prototype.triggerPlayerEvent = function(name, data) {
this.playerWrapper.triggerPlayerEvent(name, data);
};
export default DaiController;