UNPKG

3.6 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 * Enable/disable playlist function. It is intended to have the first two
13 * arguments partially-applied in order to create the final per-playlist
14 * function.
15 *
16 * @param {PlaylistLoader} playlist - The rendition or media-playlist
17 * @param {Function} changePlaylistFn - A function to be called after a
18 * playlist's enabled-state has been changed. Will NOT be called if a
19 * playlist's enabled-state is unchanged
20 * @param {Boolean=} enable - Value to set the playlist enabled-state to
21 * or if undefined returns the current enabled-state for the playlist
22 * @return {Boolean} The current enabled-state of the playlist
23 */
24var enableFunction = function enableFunction(loader, playlistUri, changePlaylistFn, enable) {
25 var playlist = loader.master.playlists[playlistUri];
26 var blacklisted = (0, _playlistJs.isBlacklisted)(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 && !blacklisted) {
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 * The representation object encapsulates the publicly visible information
53 * in a media playlist along with a setter/getter-type function (enabled)
54 * for changing the enabled-state of a particular playlist entry
55 *
56 * @class Representation
57 */
58
59var Representation = function Representation(hlsHandler, playlist, id) {
60 _classCallCheck(this, Representation);
61
62 // Get a reference to a bound version of fastQualityChange_
63 var fastChangeFunction = hlsHandler.masterPlaylistController_.fastQualityChange_.bind(hlsHandler.masterPlaylistController_);
64
65 // Carefully descend into the playlist's attributes since most
66 // properties are optional
67 if (playlist.attributes) {
68 var attributes = playlist.attributes;
69
70 if (attributes.RESOLUTION) {
71 var resolution = attributes.RESOLUTION;
72
73 this.width = resolution.width;
74 this.height = resolution.height;
75 }
76
77 this.bandwidth = attributes.BANDWIDTH;
78 }
79
80 // The id is simply the ordinality of the media playlist
81 // within the master playlist
82 this.id = id;
83
84 // Partially-apply the enableFunction to create a playlist-
85 // specific variant
86 this.enabled = enableFunction.bind(this, hlsHandler.playlists, playlist.uri, fastChangeFunction);
87}
88
89/**
90 * A mixin function that adds the `representations` api to an instance
91 * of the HlsHandler class
92 * @param {HlsHandler} hlsHandler - An instance of HlsHandler to add the
93 * representation API into
94 */
95;
96
97var renditionSelectionMixin = function renditionSelectionMixin(hlsHandler) {
98 var playlists = hlsHandler.playlists;
99
100 // Add a single API-specific function to the HlsHandler instance
101 hlsHandler.representations = function () {
102 return playlists.master.playlists.filter(function (media) {
103 return !(0, _playlistJs.isBlacklisted)(media);
104 }).map(function (e, i) {
105 return new Representation(hlsHandler, e, e.uri);
106 });
107 };
108};
109
110exports['default'] = renditionSelectionMixin;
111module.exports = exports['default'];
\No newline at end of file