1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
8 |
|
9 | var _videoJs = require('video.js');
|
10 |
|
11 | var _videoJs2 = _interopRequireDefault(_videoJs);
|
12 |
|
13 | var 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 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var 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 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | var loadedMetadataHandler = function loadedMetadataHandler() {
|
46 | if (seekTo) {
|
47 | player.currentTime(seekTo);
|
48 | }
|
49 | };
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
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 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | var errorHandler = function errorHandler() {
|
77 |
|
78 |
|
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 |
|
95 |
|
96 |
|
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 |
|
106 |
|
107 |
|
108 |
|
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 |
|
119 |
|
120 | player.reloadSourceOnError = reinitPlugin;
|
121 | };
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | var reloadSourceOnError = function reloadSourceOnError(options) {
|
130 | initPlugin(this, options);
|
131 | };
|
132 |
|
133 | exports['default'] = reloadSourceOnError;
|
134 | module.exports = exports['default']; |
\ | No newline at end of file |