Blame view

node_modules/videojs-contrib-hls/src/reload-source-on-error.js 3.28 KB
2a09d1a4   liuqimichale   添加宜春 天水 宣化
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
  import videojs from 'video.js';
  
  const defaultOptions = {
    errorInterval: 30,
    getSource(next) {
      let tech = this.tech({ IWillNotUseThisInPlugins: true });
      let sourceObj = tech.currentSource_;
  
      return next(sourceObj);
    }
  };
  
  /**
   * Main entry point for the plugin
   *
   * @param {Player} player a reference to a videojs Player instance
   * @param {Object} [options] an object with plugin options
   * @private
   */
  const initPlugin = function(player, options) {
    let lastCalled = 0;
    let seekTo = 0;
    let localOptions = videojs.mergeOptions(defaultOptions, options);
  
    player.ready(() => {
      player.trigger({type: 'usage', name: 'hls-error-reload-initialized'});
    });
  
    /**
     * Player modifications to perform that must wait until `loadedmetadata`
     * has been triggered
     *
     * @private
     */
    const loadedMetadataHandler = function() {
      if (seekTo) {
        player.currentTime(seekTo);
      }
    };
  
    /**
     * Set the source on the player element, play, and seek if necessary
     *
     * @param {Object} sourceObj An object specifying the source url and mime-type to play
     * @private
     */
    const setSource = function(sourceObj) {
      if (sourceObj === null || sourceObj === undefined) {
        return;
      }
      seekTo = (player.duration() !== Infinity && player.currentTime()) || 0;
  
      player.one('loadedmetadata', loadedMetadataHandler);
  
      player.src(sourceObj);
      player.trigger({type: 'usage', name: 'hls-error-reload'});
      player.play();
    };
  
    /**
     * Attempt to get a source from either the built-in getSource function
     * or a custom function provided via the options
     *
     * @private
     */
    const errorHandler = function() {
      // Do not attempt to reload the source if a source-reload occurred before
      // 'errorInterval' time has elapsed since the last source-reload
      if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
        player.trigger({type: 'usage', name: 'hls-error-reload-canceled'});
        return;
      }
  
      if (!localOptions.getSource ||
          typeof localOptions.getSource !== 'function') {
        videojs.log.error(
          'ERROR: reloadSourceOnError - The option getSource must be a function!');
        return;
      }
      lastCalled = Date.now();
  
      return localOptions.getSource.call(player, setSource);
    };
  
    /**
     * Unbind any event handlers that were bound by the plugin
     *
     * @private
     */
    const cleanupEvents = function() {
      player.off('loadedmetadata', loadedMetadataHandler);
      player.off('error', errorHandler);
      player.off('dispose', cleanupEvents);
    };
  
    /**
     * Cleanup before re-initializing the plugin
     *
     * @param {Object} [newOptions] an object with plugin options
     * @private
     */
    const reinitPlugin = function(newOptions) {
      cleanupEvents();
      initPlugin(player, newOptions);
    };
  
    player.on('error', errorHandler);
    player.on('dispose', cleanupEvents);
  
    // Overwrite the plugin function so that we can correctly cleanup before
    // initializing the plugin
    player.reloadSourceOnError = reinitPlugin;
  };
  
  /**
   * Reload the source when an error is detected as long as there
   * wasn't an error previously within the last 30 seconds
   *
   * @param {Object} [options] an object with plugin options
   */
  const reloadSourceOnError = function(options) {
    initPlugin(this, options);
  };
  
  export default reloadSourceOnError;