UNPKG

3.76 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6
7function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
8
9var _videoJs = require('video.js');
10
11var _videoJs2 = _interopRequireDefault(_videoJs);
12
13var defaultOptions = {
14 errorInterval: 30,
15 getSource: function getSource(next) {
16 var tech = this.tech({ IWillNotUseThisInPlugins: true });
17 var sourceObj = tech.currentSource_;
18
19 return next(sourceObj);
20 }
21};
22
23/**
24 * Main entry point for the plugin
25 *
26 * @param {Player} player a reference to a videojs Player instance
27 * @param {Object} [options] an object with plugin options
28 * @private
29 */
30var initPlugin = function initPlugin(player, options) {
31 var lastCalled = 0;
32 var seekTo = 0;
33 var localOptions = _videoJs2['default'].mergeOptions(defaultOptions, options);
34
35 player.ready(function () {
36 player.trigger({ type: 'usage', name: 'hls-error-reload-initialized' });
37 });
38
39 /**
40 * Player modifications to perform that must wait until `loadedmetadata`
41 * has been triggered
42 *
43 * @private
44 */
45 var loadedMetadataHandler = function loadedMetadataHandler() {
46 if (seekTo) {
47 player.currentTime(seekTo);
48 }
49 };
50
51 /**
52 * Set the source on the player element, play, and seek if necessary
53 *
54 * @param {Object} sourceObj An object specifying the source url and mime-type to play
55 * @private
56 */
57 var setSource = function setSource(sourceObj) {
58 if (sourceObj === null || sourceObj === undefined) {
59 return;
60 }
61 seekTo = player.duration() !== Infinity && player.currentTime() || 0;
62
63 player.one('loadedmetadata', loadedMetadataHandler);
64
65 player.src(sourceObj);
66 player.trigger({ type: 'usage', name: 'hls-error-reload' });
67 player.play();
68 };
69
70 /**
71 * Attempt to get a source from either the built-in getSource function
72 * or a custom function provided via the options
73 *
74 * @private
75 */
76 var errorHandler = function errorHandler() {
77 // Do not attempt to reload the source if a source-reload occurred before
78 // 'errorInterval' time has elapsed since the last source-reload
79 if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
80 player.trigger({ type: 'usage', name: 'hls-error-reload-canceled' });
81 return;
82 }
83
84 if (!localOptions.getSource || typeof localOptions.getSource !== 'function') {
85 _videoJs2['default'].log.error('ERROR: reloadSourceOnError - The option getSource must be a function!');
86 return;
87 }
88 lastCalled = Date.now();
89
90 return localOptions.getSource.call(player, setSource);
91 };
92
93 /**
94 * Unbind any event handlers that were bound by the plugin
95 *
96 * @private
97 */
98 var cleanupEvents = function cleanupEvents() {
99 player.off('loadedmetadata', loadedMetadataHandler);
100 player.off('error', errorHandler);
101 player.off('dispose', cleanupEvents);
102 };
103
104 /**
105 * Cleanup before re-initializing the plugin
106 *
107 * @param {Object} [newOptions] an object with plugin options
108 * @private
109 */
110 var reinitPlugin = function reinitPlugin(newOptions) {
111 cleanupEvents();
112 initPlugin(player, newOptions);
113 };
114
115 player.on('error', errorHandler);
116 player.on('dispose', cleanupEvents);
117
118 // Overwrite the plugin function so that we can correctly cleanup before
119 // initializing the plugin
120 player.reloadSourceOnError = reinitPlugin;
121};
122
123/**
124 * Reload the source when an error is detected as long as there
125 * wasn't an error previously within the last 30 seconds
126 *
127 * @param {Object} [options] an object with plugin options
128 */
129var reloadSourceOnError = function reloadSourceOnError(options) {
130 initPlugin(this, options);
131};
132
133exports['default'] = reloadSourceOnError;
134module.exports = exports['default'];
\No newline at end of file