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