forked from theonion/videojs-vast-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideojs.vast.js
197 lines (170 loc) · 6.04 KB
/
videojs.vast.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
(function(vjs, vast) {
"use strict";
var
extend = function(obj) {
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;
},
defaults = {
skip: 5,
},
vastPlugin = function(options) {
var player = this;
var settings = extend({}, defaults, options || {});
if (player.ads === undefined) {
console.log("VAST requires videojs-contrib-ads");
return;
}
// If we don't have a VAST url, just bail out.
if(settings.url === undefined) {
player.trigger('adtimeout');
return;
}
player.on('contentupdate', function(){
player.vast.getContent(settings.url);
});
player.on('readyforpreroll', function() {
player.vast.preroll();
});
player.vast.getContent = function(url) {
vast.client.get(url, function(response) {
if (response) {
for (var adIdx = 0; adIdx < response.ads.length; adIdx++) {
var ad = response.ads[adIdx];
for (var creaIdx = 0; creaIdx < ad.creatives.length; creaIdx++) {
var linearCreative = ad.creatives[creaIdx];
if (linearCreative.type !== "linear") continue;
if (linearCreative.mediaFiles.length) {
player.vast.sources = player.vast.createSourceObjects(linearCreative.mediaFiles);
if (!player.vast.sources.length) {
player.trigger('adtimeout');
return;
}
player.vastTracker = new vast.tracker(ad, linearCreative);
player.on('canplay', function() {this.vastTracker.load();});
player.on('timeupdate', function() {
if (isNaN(this.vastTracker.assetDuration)) {
this.vastTracker.assetDuration = this.duration();
}
this.vastTracker.setProgress(this.currentTime());
});
player.on('play', function() {this.vastTracker.setPaused(false);});
player.on('pause', function() {this.vastTracker.setPaused(true);});
break;
}
}
if (player.vastTracker) {
player.trigger("adsready");
break;
} else {
// Inform ad server we can't find suitable media file for this ad
vast.util.track(ad.errorURLTemplates, {ERRORCODE: 403});
}
}
}
if (!player.vastTracker) {
// No pre-roll, start video
player.trigger('adtimeout');
}
});
};
player.vast.preroll = function() {
player.ads.startLinearAdMode();
player.autoplay(true);
// play your linear ad content
var adSources = player.vast.sources;
player.src(adSources);
var clickthrough;
if (player.vastTracker.clickThroughURLTemplate) {
clickthrough = vast.util.resolveURLTemplates(
[player.vastTracker.clickThroughURLTemplate],
{
CACHEBUSTER: Math.round(Math.random() * 1.0e+10),
CONTENTPLAYHEAD: player.vastTracker.progressFormated()
}
)[0];
}
var blocker = document.createElement("a");
blocker.className = "vast-blocker";
blocker.href = clickthrough || "#";
blocker.target = "_blank";
blocker.onclick = function() {
var clicktrackers = player.vastTracker.clickTrackingURLTemplate;
if (clicktrackers) {
player.vastTracker.trackURLs(clicktrackers);
}
};
player.vast.blocker = blocker;
player.el().insertBefore(blocker, player.controlBar.el());
var skipButton = document.createElement("div");
skipButton.className = "vast-skip-button";
player.vast.skipButton = skipButton;
player.el().appendChild(skipButton);
player.on("timeupdate", player.vast.timeupdate);
skipButton.onclick = function(e) {
if((' ' + player.vast.skipButton.className + ' ').indexOf(' enabled ') >= 0) {
player.vast.tearDown();
}
if(Event.prototype.stopPropagation !== undefined) {
e.stopPropagation();
} else {
return false;
}
};
player.one("ended", player.vast.tearDown);
};
player.vast.tearDown = function() {
player.vast.skipButton.parentNode.removeChild(player.vast.skipButton);
player.vast.blocker.parentNode.removeChild(player.vast.blocker);
player.off('timeupdate', player.vast.timeupdate);
player.off('ended', player.vast.tearDown);
player.ads.endLinearAdMode();
};
player.vast.timeupdate = function(e) {
player.loadingSpinner.el().style.display = "none";
var timeLeft = Math.ceil(settings.skip - player.currentTime());
if(timeLeft > 0) {
player.vast.skipButton.innerHTML = "Skip in " + timeLeft + "...";
} else {
if((' ' + player.vast.skipButton.className + ' ').indexOf(' enabled ') === -1){
player.vast.skipButton.className += " enabled";
player.vast.skipButton.innerHTML = "Skip";
}
}
};
player.vast.createSourceObjects = function(media_files) {
var fileURLs = {};
var vidFormats = ['video/mp4', 'video/webm', 'video/ogv'];
// get a list of files with unique formats
for (var i = 0; i < media_files.length; i++) {
var file_url = media_files[i].fileURL;
var mime_type = media_files[i].mimeType;
if (vidFormats.indexOf(mime_type) >= 0) {
if(fileURLs[mime_type] === undefined) {
fileURLs[mime_type] = file_url;
}
}
}
var sources = [];
for (var j=0; j < vidFormats.length; j++) {
var format = vidFormats[j];
if (fileURLs[format] !== undefined) {
sources.push({
type: format,
src: fileURLs[format]
});
}
}
return sources;
};
};
vjs.plugin('vast', vastPlugin);
}(window.videojs, window.DMVAST));