UNPKG

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