rendition-mixin.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  6. var _playlistJs = require('./playlist.js');
  7. /**
  8. * Returns a function that acts as the Enable/disable playlist function.
  9. *
  10. * @param {PlaylistLoader} loader - The master playlist loader
  11. * @param {String} playlistUri - uri of the playlist
  12. * @param {Function} changePlaylistFn - A function to be called after a
  13. * playlist's enabled-state has been changed. Will NOT be called if a
  14. * playlist's enabled-state is unchanged
  15. * @param {Boolean=} enable - Value to set the playlist enabled-state to
  16. * or if undefined returns the current enabled-state for the playlist
  17. * @return {Function} Function for setting/getting enabled
  18. */
  19. var enableFunction = function enableFunction(loader, playlistUri, changePlaylistFn) {
  20. return function (enable) {
  21. var playlist = loader.master.playlists[playlistUri];
  22. var incompatible = (0, _playlistJs.isIncompatible)(playlist);
  23. var currentlyEnabled = (0, _playlistJs.isEnabled)(playlist);
  24. if (typeof enable === 'undefined') {
  25. return currentlyEnabled;
  26. }
  27. if (enable) {
  28. delete playlist.disabled;
  29. } else {
  30. playlist.disabled = true;
  31. }
  32. if (enable !== currentlyEnabled && !incompatible) {
  33. // Ensure the outside world knows about our changes
  34. changePlaylistFn();
  35. if (enable) {
  36. loader.trigger('renditionenabled');
  37. } else {
  38. loader.trigger('renditiondisabled');
  39. }
  40. }
  41. return enable;
  42. };
  43. };
  44. /**
  45. * The representation object encapsulates the publicly visible information
  46. * in a media playlist along with a setter/getter-type function (enabled)
  47. * for changing the enabled-state of a particular playlist entry
  48. *
  49. * @class Representation
  50. */
  51. var Representation = function Representation(hlsHandler, playlist, id) {
  52. _classCallCheck(this, Representation);
  53. // Get a reference to a bound version of fastQualityChange_
  54. var fastChangeFunction = hlsHandler.masterPlaylistController_.fastQualityChange_.bind(hlsHandler.masterPlaylistController_);
  55. // some playlist attributes are optional
  56. if (playlist.attributes.RESOLUTION) {
  57. var resolution = playlist.attributes.RESOLUTION;
  58. this.width = resolution.width;
  59. this.height = resolution.height;
  60. }
  61. this.bandwidth = playlist.attributes.BANDWIDTH;
  62. // The id is simply the ordinality of the media playlist
  63. // within the master playlist
  64. this.id = id;
  65. // Partially-apply the enableFunction to create a playlist-
  66. // specific variant
  67. this.enabled = enableFunction(hlsHandler.playlists, playlist.uri, fastChangeFunction);
  68. }
  69. /**
  70. * A mixin function that adds the `representations` api to an instance
  71. * of the HlsHandler class
  72. * @param {HlsHandler} hlsHandler - An instance of HlsHandler to add the
  73. * representation API into
  74. */
  75. ;
  76. var renditionSelectionMixin = function renditionSelectionMixin(hlsHandler) {
  77. var playlists = hlsHandler.playlists;
  78. // Add a single API-specific function to the HlsHandler instance
  79. hlsHandler.representations = function () {
  80. return playlists.master.playlists.filter(function (media) {
  81. return !(0, _playlistJs.isIncompatible)(media);
  82. }).map(function (e, i) {
  83. return new Representation(hlsHandler, e, e.uri);
  84. });
  85. };
  86. };
  87. exports['default'] = renditionSelectionMixin;
  88. module.exports = exports['default'];