da03d91d by Tom Johnson

viewport rendition selection

1 parent bda942bc
...@@ -62,6 +62,34 @@ var ...@@ -62,6 +62,34 @@ var
62 }, 62 },
63 63
64 /** 64 /**
65 * A comparator function to sort two playlist object by resolution (width).
66 * @param left {object} a media playlist object
67 * @param right {object} a media playlist object
68 * @return {number} Greater than zero if the resolution.width attribute of
69 * left is greater than the corresponding attribute of right. Less
70 * than zero if the resolution.width of right is greater than left and
71 * exactly zero if the two are equal.
72 */
73 playlistResolution = function(left, right) {
74 var leftWidth, rightWidth;
75
76 if(left.attributes && left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
77 leftWidth = left.attributes.RESOLUTION.width;
78 }
79
80 leftWidth = leftWidth || window.Number.MAX_VALUE;
81
82 if(right.attributes && right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
83 rightWidth = right.attributes.RESOLUTION.width;
84 }
85
86 rightWidth = rightWidth || window.Number.MAX_VALUE;
87
88 return leftWidth - rightWidth;
89
90 },
91
92 /**
65 * TODO - Document this great feature. 93 * TODO - Document this great feature.
66 * 94 *
67 * @param playlist 95 * @param playlist
...@@ -206,6 +234,8 @@ var ...@@ -206,6 +234,8 @@ var
206 fillBuffer(currentTime * 1000); 234 fillBuffer(currentTime * 1000);
207 }); 235 });
208 236
237 player.hls.useViewportSelection = true;
238
209 /** 239 /**
210 * Chooses the appropriate media playlist based on the current 240 * Chooses the appropriate media playlist based on the current
211 * bandwidth estimate and the player size. 241 * bandwidth estimate and the player size.
...@@ -217,11 +247,13 @@ var ...@@ -217,11 +247,13 @@ var
217 bestVariant, 247 bestVariant,
218 effectiveBitrate, 248 effectiveBitrate,
219 sortedPlaylists = player.hls.master.playlists.slice(), 249 sortedPlaylists = player.hls.master.playlists.slice(),
250 mappedPlaylists = [],
220 i = sortedPlaylists.length, 251 i = sortedPlaylists.length,
221 variant; 252 variant;
222 253
223 sortedPlaylists.sort(playlistBandwidth); 254 sortedPlaylists.sort(playlistBandwidth);
224 255
256 // determine best variant by bandwidth
225 while (i--) { 257 while (i--) {
226 variant = sortedPlaylists[i]; 258 variant = sortedPlaylists[i];
227 259
...@@ -241,6 +273,52 @@ var ...@@ -241,6 +273,52 @@ var
241 } 273 }
242 } 274 }
243 275
276 // determine best variant by resolution
277 // we only want to run this if bandwidth routine above determined a best variant and override is true (default)
278 if (bestVariant && player.hls.useViewportSelection) {
279 // reset i
280 i = sortedPlaylists.length;
281
282 // map the playlists by resolution
283 // NOTE - this needs to be separate routine because eventually we want to set to index+1
284 while(i--) {
285 variant = sortedPlaylists[i];
286
287 // ignored playlists without resolution information
288 if (!variant.attributes || !variant.attributes.RESOLUTION || !variant.attributes.RESOLUTION.width || !variant.attributes.RESOLUTION.height) {
289 continue;
290 }
291
292 mappedPlaylists.push(variant);
293 }
294
295 // set index to the available mapped renditions
296 i = mappedPlaylists.length;
297
298 // sort by resolution [currently widths]
299 mappedPlaylists.sort(playlistResolution);
300
301 // iterate through the mapped playlists and assign a best variant based on rendition resolution
302 while (i--) {
303 variant = mappedPlaylists[i];
304
305 // override the bandwidth best variant with the best rendition variant
306 if (variant.attributes.RESOLUTION.width <= player.width() && variant.attributes.RESOLUTION.height <= player.height()) {
307
308 bestVariant = variant;
309
310 // TODO - select the variant one index higher than the best variant to account for dimension variance
311 /*
312 if( mappedPlaylists[i+1] != undefined )
313 {
314 bestVariant = mappedPlaylists[i+1];
315 */
316
317 break;
318 }
319 }
320 }
321
244 // if no acceptable variant was found, fall back on the lowest 322 // if no acceptable variant was found, fall back on the lowest
245 // bitrate playlist 323 // bitrate playlist
246 return bestVariant || sortedPlaylists[0]; 324 return bestVariant || sortedPlaylists[0];
......