33b4f3ac by David LaPalomento

Merge pull request #243 from videojs/resolution-plus-one

Resolution best variant plus one.
2 parents 4624510d 8ad068fe
......@@ -424,7 +424,9 @@ videojs.Hls.prototype.selectPlaylist = function () {
bandwidthPlaylists = [],
i = sortedPlaylists.length,
variant,
oldvariant,
bandwidthBestVariant,
resolutionPlusOne,
resolutionBestVariant;
sortedPlaylists.sort(videojs.Hls.comparePlaylistBandwidth);
......@@ -460,6 +462,7 @@ videojs.Hls.prototype.selectPlaylist = function () {
// iterate through the bandwidth-filtered playlists and find
// best rendition by player dimension
while (i--) {
oldvariant = variant;
variant = bandwidthPlaylists[i];
// ignore playlists without resolution information
......@@ -470,18 +473,29 @@ videojs.Hls.prototype.selectPlaylist = function () {
continue;
}
// since the playlists are sorted, the first variant that has
// dimensions less than or equal to the player size is the
// best
if (variant.attributes.RESOLUTION.width <= player.width() &&
variant.attributes.RESOLUTION.height <= player.height()) {
// dimensions less than or equal to the player size is the best
if (variant.attributes.RESOLUTION.width === player.width() &&
variant.attributes.RESOLUTION.height === player.height()) {
// if we have the exact resolution as the player use it
resolutionPlusOne = null;
resolutionBestVariant = variant;
break;
} else if (variant.attributes.RESOLUTION.width < player.width() &&
variant.attributes.RESOLUTION.height < player.height()) {
// if we don't have an exact match, see if we have a good higher quality variant to use
if (oldvariant.attributes && oldvariant.attributes.RESOLUTION &&
oldvariant.attributes.RESOLUTION.width && oldvariant.attributes.RESOLUTION.height) {
resolutionPlusOne = oldvariant;
}
resolutionBestVariant = variant;
break;
}
}
// fallback chain of variants
return resolutionBestVariant || bandwidthBestVariant || sortedPlaylists[0];
return resolutionPlusOne || resolutionBestVariant || bandwidthBestVariant || sortedPlaylists[0];
};
/**
......
......@@ -831,8 +831,8 @@ test('selects the correct rendition by player dimensions', function() {
playlist = player.hls.selectPlaylist();
deepEqual(playlist.attributes.RESOLUTION, {width:396,height:224},'should return the correct resolution by player dimensions');
equal(playlist.attributes.BANDWIDTH, 440000, 'should have the expected bandwidth in case of multiple');
deepEqual(playlist.attributes.RESOLUTION, {width:960,height:540},'should return the correct resolution by player dimensions');
equal(playlist.attributes.BANDWIDTH, 1928000, 'should have the expected bandwidth in case of multiple');
player.width(1920);
player.height(1080);
......@@ -843,6 +843,13 @@ test('selects the correct rendition by player dimensions', function() {
deepEqual(playlist.attributes.RESOLUTION, {width:960,height:540},'should return the correct resolution by player dimensions');
equal(playlist.attributes.BANDWIDTH, 1928000, 'should have the expected bandwidth in case of multiple');
player.width(396);
player.height(224);
playlist = player.hls.selectPlaylist();
deepEqual(playlist.attributes.RESOLUTION, {width:396,height:224},'should return the correct resolution by player dimensions, if exact match');
equal(playlist.attributes.BANDWIDTH, 440000, 'should have the expected bandwidth in case of multiple, if exact match');
});
......