1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 |
|
7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
8 |
|
9 | var _config = require('./config');
|
10 |
|
11 | var _config2 = _interopRequireDefault(_config);
|
12 |
|
13 | var _playlist = require('./playlist');
|
14 |
|
15 | var _playlist2 = _interopRequireDefault(_playlist);
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var safeGetComputedStyle = function safeGetComputedStyle(el, property) {
|
30 | var result = undefined;
|
31 |
|
32 | if (!el) {
|
33 | return '';
|
34 | }
|
35 |
|
36 | result = window.getComputedStyle(el);
|
37 | if (!result) {
|
38 | return '';
|
39 | }
|
40 |
|
41 | return result[property];
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | var stableSort = function stableSort(array, sortFn) {
|
52 | var newArray = array.slice();
|
53 |
|
54 | array.sort(function (left, right) {
|
55 | var cmp = sortFn(left, right);
|
56 |
|
57 | if (cmp === 0) {
|
58 | return newArray.indexOf(left) - newArray.indexOf(right);
|
59 | }
|
60 | return cmp;
|
61 | });
|
62 | };
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | var comparePlaylistBandwidth = function comparePlaylistBandwidth(left, right) {
|
75 | var leftBandwidth = undefined;
|
76 | var rightBandwidth = undefined;
|
77 |
|
78 | if (left.attributes && left.attributes.BANDWIDTH) {
|
79 | leftBandwidth = left.attributes.BANDWIDTH;
|
80 | }
|
81 | leftBandwidth = leftBandwidth || window.Number.MAX_VALUE;
|
82 | if (right.attributes && right.attributes.BANDWIDTH) {
|
83 | rightBandwidth = right.attributes.BANDWIDTH;
|
84 | }
|
85 | rightBandwidth = rightBandwidth || window.Number.MAX_VALUE;
|
86 |
|
87 | return leftBandwidth - rightBandwidth;
|
88 | };
|
89 |
|
90 | exports.comparePlaylistBandwidth = comparePlaylistBandwidth;
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | var comparePlaylistResolution = function comparePlaylistResolution(left, right) {
|
101 | var leftWidth = undefined;
|
102 | var rightWidth = undefined;
|
103 |
|
104 | if (left.attributes && left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
|
105 | leftWidth = left.attributes.RESOLUTION.width;
|
106 | }
|
107 |
|
108 | leftWidth = leftWidth || window.Number.MAX_VALUE;
|
109 |
|
110 | if (right.attributes && right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
|
111 | rightWidth = right.attributes.RESOLUTION.width;
|
112 | }
|
113 |
|
114 | rightWidth = rightWidth || window.Number.MAX_VALUE;
|
115 |
|
116 |
|
117 |
|
118 | if (leftWidth === rightWidth && left.attributes.BANDWIDTH && right.attributes.BANDWIDTH) {
|
119 | return left.attributes.BANDWIDTH - right.attributes.BANDWIDTH;
|
120 | }
|
121 | return leftWidth - rightWidth;
|
122 | };
|
123 |
|
124 | exports.comparePlaylistResolution = comparePlaylistResolution;
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | var simpleSelector = function simpleSelector(master, playerBandwidth, playerWidth, playerHeight) {
|
141 |
|
142 | var sortedPlaylistReps = master.playlists.map(function (playlist) {
|
143 | var width = undefined;
|
144 | var height = undefined;
|
145 | var bandwidth = undefined;
|
146 |
|
147 | if (playlist.attributes) {
|
148 | width = playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.width;
|
149 | height = playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height;
|
150 | bandwidth = playlist.attributes.BANDWIDTH;
|
151 | }
|
152 |
|
153 | bandwidth = bandwidth || window.Number.MAX_VALUE;
|
154 |
|
155 | return {
|
156 | bandwidth: bandwidth,
|
157 | width: width,
|
158 | height: height,
|
159 | playlist: playlist
|
160 | };
|
161 | });
|
162 |
|
163 | stableSort(sortedPlaylistReps, function (left, right) {
|
164 | return left.bandwidth - right.bandwidth;
|
165 | });
|
166 |
|
167 |
|
168 |
|
169 | sortedPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
170 | return _playlist2['default'].isEnabled(rep.playlist);
|
171 | });
|
172 |
|
173 |
|
174 |
|
175 | var bandwidthPlaylistReps = sortedPlaylistReps.filter(function (rep) {
|
176 | return rep.bandwidth * _config2['default'].BANDWIDTH_VARIANCE < playerBandwidth;
|
177 | });
|
178 |
|
179 | var highestRemainingBandwidthRep = bandwidthPlaylistReps[bandwidthPlaylistReps.length - 1];
|
180 |
|
181 |
|
182 |
|
183 | var bandwidthBestRep = bandwidthPlaylistReps.filter(function (rep) {
|
184 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
185 | })[0];
|
186 |
|
187 |
|
188 | var haveResolution = bandwidthPlaylistReps.filter(function (rep) {
|
189 | return rep.width && rep.height;
|
190 | });
|
191 |
|
192 |
|
193 | stableSort(haveResolution, function (left, right) {
|
194 | return left.width - right.width;
|
195 | });
|
196 |
|
197 |
|
198 | var resolutionBestRepList = haveResolution.filter(function (rep) {
|
199 | return rep.width === playerWidth && rep.height === playerHeight;
|
200 | });
|
201 |
|
202 | highestRemainingBandwidthRep = resolutionBestRepList[resolutionBestRepList.length - 1];
|
203 |
|
204 | var resolutionBestRep = resolutionBestRepList.filter(function (rep) {
|
205 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
206 | })[0];
|
207 |
|
208 | var resolutionPlusOneList = undefined;
|
209 | var resolutionPlusOneSmallest = undefined;
|
210 | var resolutionPlusOneRep = undefined;
|
211 |
|
212 |
|
213 |
|
214 | if (!resolutionBestRep) {
|
215 | resolutionPlusOneList = haveResolution.filter(function (rep) {
|
216 | return rep.width > playerWidth || rep.height > playerHeight;
|
217 | });
|
218 |
|
219 |
|
220 | resolutionPlusOneSmallest = resolutionPlusOneList.filter(function (rep) {
|
221 | return rep.width === resolutionPlusOneList[0].width && rep.height === resolutionPlusOneList[0].height;
|
222 | });
|
223 |
|
224 |
|
225 |
|
226 | highestRemainingBandwidthRep = resolutionPlusOneSmallest[resolutionPlusOneSmallest.length - 1];
|
227 | resolutionPlusOneRep = resolutionPlusOneSmallest.filter(function (rep) {
|
228 | return rep.bandwidth === highestRemainingBandwidthRep.bandwidth;
|
229 | })[0];
|
230 | }
|
231 |
|
232 |
|
233 | return (resolutionPlusOneRep || resolutionBestRep || bandwidthBestRep || sortedPlaylistReps[0]).playlist;
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | var lastBandwidthSelector = function lastBandwidthSelector() {
|
249 | return simpleSelector(this.playlists.master, this.systemBandwidth, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10), parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10));
|
250 | };
|
251 |
|
252 | exports.lastBandwidthSelector = lastBandwidthSelector;
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | var movingAverageBandwidthSelector = function movingAverageBandwidthSelector(decay) {
|
268 | var average = -1;
|
269 |
|
270 | if (decay < 0 || decay > 1) {
|
271 | throw new Error('Moving average bandwidth decay must be between 0 and 1.');
|
272 | }
|
273 |
|
274 | return function () {
|
275 | if (average < 0) {
|
276 | average = this.systemBandwidth;
|
277 | }
|
278 |
|
279 | average = decay * this.systemBandwidth + (1 - decay) * average;
|
280 | return simpleSelector(this.playlists.master, average, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10), parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10));
|
281 | };
|
282 | };
|
283 |
|
284 | exports.movingAverageBandwidthSelector = movingAverageBandwidthSelector;
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | var minRebufferMaxBandwidthSelector = function minRebufferMaxBandwidthSelector(settings) {
|
314 | var master = settings.master;
|
315 | var currentTime = settings.currentTime;
|
316 | var bandwidth = settings.bandwidth;
|
317 | var duration = settings.duration;
|
318 | var segmentDuration = settings.segmentDuration;
|
319 | var timeUntilRebuffer = settings.timeUntilRebuffer;
|
320 | var currentTimeline = settings.currentTimeline;
|
321 | var syncController = settings.syncController;
|
322 |
|
323 | var bandwidthPlaylists = master.playlists.filter(_playlist2['default'].hasAttribute.bind(null, 'BANDWIDTH'));
|
324 |
|
325 | var rebufferingEstimates = bandwidthPlaylists.map(function (playlist) {
|
326 | var syncPoint = syncController.getSyncPoint(playlist, duration, currentTimeline, currentTime);
|
327 |
|
328 |
|
329 | var numRequests = syncPoint ? 1 : 2;
|
330 | var requestTimeEstimate = _playlist2['default'].estimateSegmentRequestTime(segmentDuration, bandwidth, playlist);
|
331 | var rebufferingImpact = requestTimeEstimate * numRequests - timeUntilRebuffer;
|
332 |
|
333 | return {
|
334 | playlist: playlist,
|
335 | rebufferingImpact: rebufferingImpact
|
336 | };
|
337 | });
|
338 |
|
339 | var noRebufferingPlaylists = rebufferingEstimates.filter(function (estimate) {
|
340 | return estimate.rebufferingImpact <= 0;
|
341 | });
|
342 |
|
343 |
|
344 | stableSort(noRebufferingPlaylists, function (a, b) {
|
345 | return comparePlaylistBandwidth(b.playlist, a.playlist);
|
346 | });
|
347 |
|
348 | if (noRebufferingPlaylists.length) {
|
349 | return noRebufferingPlaylists[0];
|
350 | }
|
351 |
|
352 | stableSort(rebufferingEstimates, function (a, b) {
|
353 | return a.rebufferingImpact - b.rebufferingImpact;
|
354 | });
|
355 |
|
356 | return rebufferingEstimates[0] || null;
|
357 | };
|
358 | exports.minRebufferMaxBandwidthSelector = minRebufferMaxBandwidthSelector; |
\ | No newline at end of file |