-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathplugin.js
355 lines (298 loc) · 9.81 KB
/
plugin.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
import videojs from 'video.js';
import document from 'global/document';
import {version as VERSION} from '../package.json';
const FlashObj = videojs.getComponent('Flash');
const defaultDismiss = !videojs.browser.IS_IPHONE;
// Video.js 5/6 cross-compatibility.
const registerPlugin = videojs.registerPlugin || videojs.plugin;
// Default options for the plugin.
const defaults = {
header: '',
code: '',
message: '',
timeout: 45 * 1000,
dismiss: defaultDismiss,
progressDisabled: false,
errors: {
'1': {
type: 'MEDIA_ERR_ABORTED',
headline: 'The video download was cancelled'
},
'2': {
type: 'MEDIA_ERR_NETWORK',
headline: 'The video connection was lost, please confirm you are ' +
'connected to the internet'
},
'3': {
type: 'MEDIA_ERR_DECODE',
headline: 'The video is bad or in a format that cannot be played on your browser'
},
'4': {
type: 'MEDIA_ERR_SRC_NOT_SUPPORTED',
headline: 'This video is either unavailable or not supported in this browser'
},
'5': {
type: 'MEDIA_ERR_ENCRYPTED',
headline: 'The video you are trying to watch is encrypted and we do not know how ' +
'to decrypt it'
},
'unknown': {
type: 'MEDIA_ERR_UNKNOWN',
headline: 'An unanticipated problem was encountered, check back soon and try again'
},
'-1': {
type: 'PLAYER_ERR_NO_SRC',
headline: 'No video has been loaded'
},
'-2': {
type: 'PLAYER_ERR_TIMEOUT',
headline: 'Could not download the video'
},
'PLAYER_ERR_DOMAIN_RESTRICTED': {
headline: 'This video is restricted from playing on your current domain'
},
'PLAYER_ERR_IP_RESTRICTED': {
headline: 'This video is restricted at your current IP address'
},
'PLAYER_ERR_GEO_RESTRICTED': {
headline: 'This video is restricted from playing in your current geographic region'
},
'FLASHLS_ERR_CROSS_DOMAIN': {
headline: 'The video could not be loaded: crossdomain access denied.'
}
}
};
const initPlugin = function(player, options) {
let monitor;
let waiting;
let isStalling;
const listeners = [];
const updateErrors = function(updates) {
options.errors = videojs.mergeOptions(options.errors, updates);
// Create `code`s from errors which don't have them (based on their keys).
Object.keys(options.errors).forEach(k => {
const err = options.errors[k];
if (!err.type) {
err.type = k;
}
});
};
// Make sure we flesh out initially-provided errors.
updateErrors();
// clears the previous monitor timeout and sets up a new one
const resetMonitor = function() {
// at this point the player has recovered
player.clearTimeout(waiting);
if (isStalling) {
isStalling = false;
player.removeClass('vjs-waiting');
}
// start the loading spinner if player has stalled
waiting = player.setTimeout(function() {
// player already has an error
// or is not playing under normal conditions
if (player.error() || player.paused() || player.ended()) {
return;
}
isStalling = true;
player.addClass('vjs-waiting');
}, 1000);
player.clearTimeout(monitor);
monitor = player.setTimeout(function() {
// player already has an error
// or is not playing under normal conditions
if (player.error() || player.paused() || player.ended()) {
return;
}
player.error({
code: -2,
type: 'PLAYER_ERR_TIMEOUT'
});
}, options.timeout);
// clear out any existing player timeout
// playback has recovered
if (player.error() && player.error().code === -2) {
player.error(null);
}
};
// clear any previously registered listeners
const cleanup = function() {
let listener;
while (listeners.length) {
listener = listeners.shift();
player.off(listener[0], listener[1]);
}
player.clearTimeout(monitor);
player.clearTimeout(waiting);
};
// creates and tracks a player listener if the player looks alive
const healthcheck = function(type, fn) {
const check = function() {
// if there's an error do not reset the monitor and
// clear the error unless time is progressing
if (!player.error()) {
// error if using Flash and its API is unavailable
const tech = player.$('.vjs-tech');
if (tech &&
tech.type === 'application/x-shockwave-flash' &&
!tech.vjs_getProperty) {
player.error({
code: -2,
type: 'PLAYER_ERR_TIMEOUT'
});
return;
}
// playback isn't expected if the player is paused
if (player.paused()) {
return resetMonitor();
}
// playback isn't expected once the video has ended
if (player.ended()) {
return resetMonitor();
}
}
fn.call(this);
};
player.on(type, check);
listeners.push([type, check]);
};
const onPlayStartMonitor = function() {
let lastTime = 0;
cleanup();
// if no playback is detected for long enough, trigger a timeout error
resetMonitor();
healthcheck(['timeupdate', 'adtimeupdate'], function() {
const currentTime = player.currentTime();
// playback is operating normally or has recovered
if (currentTime !== lastTime) {
lastTime = currentTime;
resetMonitor();
}
});
if (!options.progressDisabled) {
healthcheck('progress', resetMonitor);
}
};
const onPlayNoSource = function() {
if (!player.currentSrc()) {
player.error({
code: -1,
type: 'PLAYER_ERR_NO_SRC'
});
}
};
const onErrorHandler = function() {
let details = '';
let error = player.error();
const content = document.createElement('div');
let dialogContent = '';
// In the rare case when `error()` does not return an error object,
// defensively escape the handler function.
if (!error) {
return;
}
error = videojs.mergeOptions(error, options.errors[error.code || error.type || 0]);
if (error.message) {
details = `<div class="vjs-errors-details">${player.localize('Technical details')}
: <div class="vjs-errors-message">${player.localize(error.message)}</div>
</div>`;
}
if (error.code === 4 && FlashObj && !FlashObj.isSupported()) {
const flashMessage = player.localize(
'If you are using an older browser please try upgrading or installing Flash.'
);
details += `<span class="vjs-errors-flashmessage">${flashMessage}</span>`;
}
const display = player.getChild('errorDisplay');
content.className = 'vjs-errors-dialog';
content.id = 'vjs-errors-dialog';
dialogContent =
`<div class="vjs-errors-content-container">
<h2 class="vjs-errors-headline">${this.localize(error.headline)}</h2>
<div><b>${this.localize('Error Code')}</b>: ${(error.type || error.code)}</div>
${details}
</div>`;
const closeable = display.closeable(!('dismiss' in error) || error.dismiss);
// We should get a close button
if (closeable) {
dialogContent +=
`<div class="vjs-errors-ok-button-container">
<button class="vjs-errors-ok-button">${this.localize('OK')}</button>
</div>`;
content.innerHTML = dialogContent;
display.fillWith(content);
// Get the close button inside the error display
display.contentEl().firstChild.appendChild(display.getChild('closeButton').el());
const okButton = display.el().querySelector('.vjs-errors-ok-button');
player.on(okButton, 'click', function() {
display.close();
});
} else {
content.innerHTML = dialogContent;
display.fillWith(content);
}
if (player.currentWidth() <= 600 || player.currentHeight() <= 250) {
display.addClass('vjs-xs');
}
display.one('modalclose', () => player.error(null));
};
const onDisposeHandler = function() {
cleanup();
player.removeClass('vjs-errors');
player.off('play', onPlayStartMonitor);
player.off('play', onPlayNoSource);
player.off('dispose', onDisposeHandler);
player.off(['aderror', 'error'], onErrorHandler);
};
const reInitPlugin = function(newOptions) {
onDisposeHandler();
initPlugin(player, videojs.mergeOptions(defaults, newOptions));
};
reInitPlugin.extend = (errors) => updateErrors(errors);
reInitPlugin.getAll = () => videojs.mergeOptions(options.errors);
// Get / set timeout value. Restart monitor if changed.
reInitPlugin.timeout = function(timeout) {
if (typeof timeout === 'undefined') {
return options.timeout;
}
if (timeout !== options.timeout) {
options.timeout = timeout;
if (!player.paused()) {
onPlayStartMonitor();
}
}
};
reInitPlugin.disableProgress = function(disabled) {
options.progressDisabled = disabled;
onPlayStartMonitor();
};
player.on('play', onPlayStartMonitor);
player.on('play', onPlayNoSource);
player.on('dispose', onDisposeHandler);
player.on(['aderror', 'error'], onErrorHandler);
player.ready(() => {
player.addClass('vjs-errors');
});
// if the plugin is re-initialised during playback, start the timeout handler.
if (!player.paused()) {
onPlayStartMonitor();
}
// Include the version number.
reInitPlugin.VERSION = VERSION;
player.errors = reInitPlugin;
};
const errors = function(options) {
initPlugin(this, videojs.mergeOptions(defaults, options));
};
['extend', 'getAll', 'disableProgress'].forEach(k => {
errors[k] = function() {
videojs.log.warn(
`The errors.${k}() method is not available until the plugin has been initialized!`
);
};
});
// Include the version number.
errors.VERSION = VERSION;
// Register the plugin with video.js.
registerPlugin('errors', errors);
export default errors;