-
Notifications
You must be signed in to change notification settings - Fork 0
/
videojs.ads.js
421 lines (368 loc) · 12.5 KB
/
videojs.ads.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
/**
* Basic Ad support plugin for video.js.
*
* Common code to support ad integrations.
*/
(function(window, document, vjs, undefined) {
"use strict";
var
/**
* Copies properties from one or more objects onto an original.
*/
extend = function(obj /*, arg1, arg2, ... */) {
var arg, i, k;
for (i=1; i<arguments.length; i++) {
arg = arguments[i];
for (k in arg) {
if (arg.hasOwnProperty(k)) {
obj[k] = arg[k];
}
}
}
return obj;
},
/**
* Add a handler for multiple listeners to an object that supports addEventListener() or on().
*
* @param {object} obj The object to which the handler will be assigned.
* @param {mixed} events A string, array of strings, or hash of string/callback pairs.
* @param {function} callback Invoked when specified events occur, if events param is not a hash.
*
* @return {object} obj The object passed in.
*/
on = function(obj, events, handler) {
var
type = Object.prototype.toString.call(events),
register = function(obj, event, handler) {
if (obj.addEventListener) {
obj.addEventListener(event, handler);
} else if (obj.on) {
obj.on(event, handler);
} else if (obj.attachEvent) {
obj.attachEvent('on' + event, handler);
} else {
throw new Error('object has no mechanism for adding event listeners');
}
},
i,
ii;
switch (type) {
case '[object String]':
register(obj, events, handler);
break;
case '[object Array]':
for (i = 0, ii = events.length; i<ii; i++) {
register(obj, events[i], handler);
}
break;
case '[object Object]':
for (i in events) {
if (events.hasOwnProperty(i)) {
register(obj, i, events[i]);
}
}
break;
default:
throw new Error('Unrecognized events parameter type: ' + type);
}
return obj;
},
/**
* Runs the callback at the next available opportunity.
* @see https://developer.mozilla.org/en-US/docs/Web/API/window.setImmediate
*/
setImmediate = function(callback) {
return (
window.setImmediate ||
window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.setTimeout
).call(window, callback, 0);
},
/**
* If ads are not playing, pauses the player at the next available
* opportunity. Has no effect if ads have started. This function is necessary
* because pausing a video element while processing a `play` event on iOS can
* cause the video element to continuously toggle between playing and paused
* states.
*
* @param {object} player The video player
*/
cancelContentPlay = function(player) {
setImmediate(function() {
if (!player.paused() && player.ads.state !== 'ad-playback') {
player.pause();
}
});
},
/**
* Returns an object that captures the portions of player state relevant to
* video playback. The result of this function can be passed to
* restorePlayerSnapshot with a player to return the player to the state it
* was in when this function was invoked.
* @param {object} player The videojs player object
*/
getPlayerSnapshot = function(player) {
var
tech = player.el().querySelector('.vjs-tech'),
snapshot = {
src: player.currentSrc(),
currentTime: player.currentTime(),
// on slow connections, player.paused() may be true when starting and
// stopping ads even though play has been requested. Hard-coding the
// playback state works for the purposes of ad playback but makes this
// an inaccurate snapshot.
play: true
};
if (tech) {
snapshot.nativePoster = tech.poster;
}
return snapshot;
},
removeClass = function(element, className) {
var
classes = element.className.split(/\s+/),
i = classes.length,
newClasses = [];
while (i--) {
if (classes[i] !== className) {
newClasses.push(classes[i]);
}
}
element.className = newClasses.join(' ');
},
/**
* Attempts to modify the specified player so that its state is equivalent to the state of the snapshot.
* @param {object} snapshot - the player state to apply
*/
restorePlayerSnapshot = function(player, snapshot) {
var
// the playback tech
tech = player.el().querySelector('.vjs-tech'),
// the number of remaining attempts to restore the snapshot
attempts = 20,
// finish restoring the playback state
resume = function() {
player.currentTime(snapshot.currentTime);
if (snapshot.play) {
player.play();
}
},
// determine if the video element has loaded enough of the snapshot source
// to be ready to apply the rest of the state
tryToResume = function() {
if (tech.seekable === undefined) {
// if the tech doesn't expose the seekable time ranges, try to
// resume playback immediately
resume();
return;
}
if (tech.seekable.length > 0) {
// if some period of the video is seekable, resume playback
resume();
return;
}
// delay a bit and then check again unless we're out of attempts
if (attempts--) {
setTimeout(tryToResume, 50);
}
};
if (snapshot.nativePoster) {
tech.poster = snapshot.nativePoster;
}
player.src(snapshot.src);
// safari requires a call to `load` to pick up a changed source
player.load();
player.one('loadedmetadata', tryToResume);
},
/**
* Remove the poster attribute from the video element tech, if present. When
* reusing a video element for multiple videos, the poster image will briefly
* reappear while the new source loads. Removing the attribute ahead of time
* prevents the poster from showing up between videos.
* @param {object} player The videojs player object
*/
removeNativePoster = function(player) {
var tech = player.el().querySelector('.vjs-tech');
if (tech) {
tech.poster = null;
}
},
// ---------------------------------------------------------------------------
// Ad Framework
// ---------------------------------------------------------------------------
// default framework settings
defaults = {
// maximum amount of time in ms to wait to receive `adsready` from the ad
// implementation after play has been requested. Ad implementations are
// expected to load any dynamic libraries and make any requests to determine
// ad policies for a video during this time.
timeout: 5000,
// maximum amount of time in ms to wait for the ad implementation to start
// linear ad mode after `readyforpreroll` has fired. This is in addition to
// the standard timeout.
prerollTimeout: 100
},
adFramework = function(options) {
var
player = this,
// merge options and defaults
settings = extend({}, defaults, options || {}),
fsmHandler;
// replace the ad initializer with the ad namespace
player.ads = {
state: 'content-set',
startLinearAdMode: function() {
player.trigger('adstart');
},
endLinearAdMode: function() {
player.trigger('adend');
}
};
fsmHandler = function(event) {
// Ad Playback State Machine
var
fsm = {
'content-set': {
events: {
'adsready': function() {
this.state = 'ads-ready';
},
'play': function() {
this.state = 'ads-ready?';
this.snapshot = getPlayerSnapshot(player);
cancelContentPlay(player);
// remove the poster so it doesn't flash between videos
removeNativePoster(player);
}
}
},
'ads-ready': {
events: {
'play': function() {
this.state = 'preroll?';
cancelContentPlay(player);
}
}
},
'preroll?': {
enter: function() {
// capture current player state snapshot (playing, currentTime, src)
this.snapshot = getPlayerSnapshot(player);
// remove the poster so it doesn't flash between videos
removeNativePoster(player);
// change class to show that we're waiting on ads
player.el().className += ' vjs-ad-loading';
// schedule an adtimeout event to fire if we waited too long
player.ads.timeout = window.setTimeout(function() {
player.trigger('adtimeout');
}, settings.prerollTimeout);
// signal to ad plugin that it's their opportunity to play a preroll
player.trigger('readyforpreroll');
},
leave: function() {
window.clearTimeout(player.ads.timeout);
removeClass(player.el(), 'vjs-ad-loading');
},
events: {
'play': function() {
cancelContentPlay(player);
},
'adstart': function() {
this.state = 'ad-playback';
player.el().className += ' vjs-ad-playing';
},
'adtimeout': function() {
this.state = 'content-playback';
player.play();
}
}
},
'ads-ready?': {
enter: function() {
player.el().className += ' vjs-ad-loading';
player.ads.timeout = window.setTimeout(function() {
player.trigger('adtimeout');
}, settings.timeout);
},
leave: function() {
window.clearTimeout(player.ads.timeout);
removeClass(player.el(), 'vjs-ad-loading');
},
events: {
'play': function() {
cancelContentPlay(player);
},
'adsready': function() {
this.state = 'preroll?';
},
'adtimeout': function() {
this.state = 'ad-timeout-playback';
}
}
},
'ad-timeout-playback': {
enter: function() {
restorePlayerSnapshot(player, this.snapshot);
},
events: {
'adsready': function() {
if (player.paused()) {
this.state = 'ads-ready';
} else {
this.state = 'preroll?';
}
}
}
},
'ad-playback': {
events: {
'adend': function() {
this.state = 'content-playback';
removeClass(player.el(), 'vjs-ad-playing');
restorePlayerSnapshot(player, this.snapshot);
}
}
},
'content-playback': {
events: {
'adstart': function() {
this.state = 'ad-playback';
this.snapshot = getPlayerSnapshot(player);
player.el().className += ' vjs-ad-playing';
// remove the poster so it doesn't flash between videos
removeNativePoster(player);
}
}
}
};
(function(state) {
var noop = function() {};
// process the current event with a noop default handler
(fsm[state].events[event.type] || noop).apply(player.ads);
// execute leave/enter callbacks if present
if (state !== player.ads.state) {
(fsm[state].leave || noop).apply(player.ads);
(fsm[player.ads.state].enter || noop).apply(player.ads);
}
})(player.ads.state);
};
// register for the events we're interested in
on(player, vjs.Html5.Events.concat([
// events emitted by ad plugin
'adtimeout',
// events emitted by third party ad implementors
'adsready',
'adstart', // startLinearAdMode()
'adend', // endLinearAdMode()
]), fsmHandler);
// kick off the fsm
if (!player.paused()) {
// simulate a play event if we're autoplaying
fsmHandler({type:'play'});
}
};
// register the ad plugin framework
vjs.plugin('ads', adFramework);
})(window, document, videojs);