reload-source-on-error.js 3.7 KB

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