Skip to content

Commit

Permalink
Never adapt across roles or languages
Browse files Browse the repository at this point in the history
This adds additional logic and tests to filtering in StreamUtils.

Closes #918
Closes #947
Closes #949

Change-Id: I4cce0e0dd5d247e1eaadf7401116cdafb72c7259
  • Loading branch information
joeyparrish committed Aug 4, 2017
1 parent 6b6e740 commit 72c07b3
Show file tree
Hide file tree
Showing 3 changed files with 468 additions and 88 deletions.
26 changes: 12 additions & 14 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,8 +655,8 @@ shaka.Player.prototype.load = function(manifestUri, opt_startTime,
// active one, update abr manager with filtered variants for the current
// period.
var currentPeriod = this.streamingEngine_.getCurrentPeriod();
var variants = shaka.util.StreamUtils.filterVariantsByRoleAndLanguage(
currentPeriod, this.currentAudioLanguage_);
var variants = shaka.util.StreamUtils.filterVariantsByLanguageAndRole(
currentPeriod, this.currentAudioLanguage_, this.currentVariantRole_);
this.abrManager_.setVariants(variants);

var hasPrimary = currentPeriod.variants.some(function(variant) {
Expand Down Expand Up @@ -2396,12 +2396,10 @@ shaka.Player.prototype.chooseVariant_ = function(variants) {
shaka.Player.prototype.chooseStreamsAndSwitch_ = function(period) {
goog.asserts.assert(this.config_, 'Must not be destroyed');

var variants = shaka.util.StreamUtils.filterVariantsByRoleAndLanguage(
period, this.currentAudioLanguage_, /* opt_languageMatches */ undefined,
this.currentVariantRole_);
var textStreams = shaka.util.StreamUtils.filterTextStreamsByRoleAndLanguage(
period, this.currentTextLanguage_, /* opt_languageMatches */ undefined,
this.currentTextRole_);
var variants = shaka.util.StreamUtils.filterVariantsByLanguageAndRole(
period, this.currentAudioLanguage_, this.currentVariantRole_);
var textStreams = shaka.util.StreamUtils.filterTextStreamsByLanguageAndRole(
period, this.currentTextLanguage_, this.currentTextRole_);

// Because we're running this after a config change (manual language change),
// a new text stream, or a key status event, and because switching to an
Expand Down Expand Up @@ -2453,13 +2451,13 @@ shaka.Player.prototype.onChooseStreams_ = function(period) {
languageMatches[ContentType.AUDIO] = false;
languageMatches[ContentType.TEXT] = false;

var variants = StreamUtils.filterVariantsByRoleAndLanguage(
period, this.currentAudioLanguage_, languageMatches,
this.currentVariantRole_);
var variants = StreamUtils.filterVariantsByLanguageAndRole(
period, this.currentAudioLanguage_, this.currentVariantRole_,
languageMatches);

var textStreams = StreamUtils.filterTextStreamsByRoleAndLanguage(
period, this.currentTextLanguage_, languageMatches,
this.currentTextRole_);
var textStreams = StreamUtils.filterTextStreamsByLanguageAndRole(
period, this.currentTextLanguage_, this.currentTextRole_,
languageMatches);

shaka.log.v2('onChooseStreams_, variants and text streams: ',
variants, textStreams);
Expand Down
169 changes: 118 additions & 51 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ goog.require('shaka.media.DrmEngine');
goog.require('shaka.media.MediaSourceEngine');
goog.require('shaka.text.TextEngine');
goog.require('shaka.util.ArrayUtils');
goog.require('shaka.util.Functional');
goog.require('shaka.util.LanguageUtils');
goog.require('shaka.util.ManifestParserUtils');

Expand Down Expand Up @@ -373,31 +374,38 @@ shaka.util.StreamUtils.getPlayableVariants = function(variants) {
*
* @param {shakaExtern.Period} period
* @param {string} preferredLanguage
* @param {string} preferredRole
* @param {!Object=} opt_languageMatches
* @param {string=} opt_role
* @return {!Array.<!shakaExtern.Variant>}
*/
shaka.util.StreamUtils.filterVariantsByRoleAndLanguage = function(
period, preferredLanguage, opt_languageMatches, opt_role) {
shaka.util.StreamUtils.filterVariantsByLanguageAndRole = function(
period, preferredLanguage, preferredRole, opt_languageMatches) {
var LanguageUtils = shaka.util.LanguageUtils;
var ContentType = shaka.util.ManifestParserUtils.ContentType;
var variants = shaka.util.StreamUtils.getPlayableVariants(period.variants);

// Initially choose the first language in the list.
// Start with the set of primary variants.
/** @type {!Array.<!shakaExtern.Variant>} */
var chosen = variants.filter(function(variant) {
return variant.language == variants[0].language;
return variant.primary;
});

// Prefer primary variants.
var primaryVariants = variants.filter(function(variant) {
return variant.primary;
// If there were no primary variants, go back to all variants.
if (!chosen.length) {
chosen = variants;
}

// Now reduce the set to one language. This covers both arbitrary language
// choice and the reduction of the "primary" variant set to one language.
var firstLanguage = chosen.length ? chosen[0].language : '';
chosen = chosen.filter(function(variant) {
return variant.language == firstLanguage;
});
if (primaryVariants.length) chosen = primaryVariants;

// Choose based on language preference. Favor exact matches, then
// base matches, finally different subtags. Execute in reverse order so
// the later steps override the previous ones.
// Now search for matches based on language preference. If any language match
// is found, it overrides the selection above. Favor exact matches, then base
// matches, finally different subtags. Execute in reverse order so the later
// steps override the previous ones.
if (preferredLanguage) {
var pref = LanguageUtils.normalize(preferredLanguage);
[LanguageUtils.MatchType.OTHER_SUB_LANGUAGE_OKAY,
Expand All @@ -423,22 +431,30 @@ shaka.util.StreamUtils.filterVariantsByRoleAndLanguage = function(
}); // forEach(matchType)
} // if (preferredLanguage)

// Choose based on role preference. If there's no exact match, return
// what was chosen based on the language preference.
var role = opt_role || '';
if (role) {
var chosenWithRoles = chosen.filter(function(variant) {
return (variant.audio && (variant.audio.roles.indexOf(role) > - 1)) ||
(variant.video && (variant.video.roles.indexOf(role) > - 1));
});
if (chosenWithRoles.length) return chosenWithRoles;
else {
shaka.log.warning(
'No exact match for the role is found. Returning the selection ' +
'based on language preference.');
// Now refine the choice based on role preference.
if (preferredRole) {
var roleMatches = shaka.util.StreamUtils.filterVariantsByRole_(
chosen, preferredRole);
if (roleMatches.length) {
return roleMatches;
} else {
shaka.log.warning('No exact match for the variant role could be found.');
}
}
return chosen;

// Either there was no role preference, or it could not be satisfied.
// Choose an arbitrary role, if there are any, and filter out any other roles.
// This ensures we never adapt between roles.
var allRoles = chosen.map(function(variant) {
var audioRoles = variant.audio ? variant.audio.roles : [];
var videoRoles = variant.video ? variant.video.roles : [];
return audioRoles.concat(videoRoles);
}).reduce(shaka.util.Functional.collapseArrays, []);

if (!allRoles.length) {
return chosen;
}
return shaka.util.StreamUtils.filterVariantsByRole_(chosen, allRoles[0]);
};


Expand All @@ -447,29 +463,38 @@ shaka.util.StreamUtils.filterVariantsByRoleAndLanguage = function(
*
* @param {shakaExtern.Period} period
* @param {string} preferredLanguage
* @param {string} preferredRole
* @param {!Object=} opt_languageMatches
* @param {string=} opt_role
* @return {!Array.<!shakaExtern.Stream>}
*/
shaka.util.StreamUtils.filterTextStreamsByRoleAndLanguage = function(
period, preferredLanguage, opt_languageMatches, opt_role) {
shaka.util.StreamUtils.filterTextStreamsByLanguageAndRole = function(
period, preferredLanguage, preferredRole, opt_languageMatches) {
var LanguageUtils = shaka.util.LanguageUtils;
var ContentType = shaka.util.ManifestParserUtils.ContentType;
var streams = period.textStreams;

// Choose all the streams.
// Start with the set of primary streams.
/** @type {!Array.<!shakaExtern.Stream>} */
var chosen = streams;

// Prefer primary text streams.
var primaryStreams = streams.filter(function(stream) {
var chosen = streams.filter(function(stream) {
return stream.primary;
});
if (primaryStreams.length) chosen = primaryStreams;

// Override based on language preference. Favor exact matches, then
// base matches, finally different subtags. Execute in reverse order so
// the later steps override the previous ones.
// If there were no primary streams, go back to all streams.
if (!chosen.length) {
chosen = streams;
}

// Now reduce the set to one language. This covers both arbitrary language
// choice and the reduction of the "primary" stream set to one language.
var firstLanguage = chosen.length ? chosen[0].language : '';
chosen = chosen.filter(function(stream) {
return stream.language == firstLanguage;
});

// Now search for matches based on language preference. If any language match
// is found, it overrides the selection above. Favor exact matches, then base
// matches, finally different subtags. Execute in reverse order so the later
// steps override the previous ones.
if (preferredLanguage) {
var pref = LanguageUtils.normalize(preferredLanguage);
[LanguageUtils.MatchType.OTHER_SUB_LANGUAGE_OKAY,
Expand All @@ -492,21 +517,63 @@ shaka.util.StreamUtils.filterTextStreamsByRoleAndLanguage = function(
}); // forEach(stream)
}); // forEach(matchType)
} // if (preferredLanguage)
// Choose based on role preference. If there's no exact match, return
// what was chosen based on the language preference.
var role = opt_role || '';
if (role) {
var chosenWithRoles = chosen.filter(function(stream) {
return (stream && (stream.roles.indexOf(role) > - 1));
});
if (chosenWithRoles.length) return chosenWithRoles;
else {
shaka.log.warning(
'No exact match for the role is found. Returning the selection ' +
'based on language preference.');

// Now refine the choice based on role preference.
if (preferredRole) {
var roleMatches = shaka.util.StreamUtils.filterTextStreamsByRole_(
chosen, preferredRole);
if (roleMatches.length) {
return roleMatches;
} else {
shaka.log.warning('No exact match for the text role could be found.');
}
}
return chosen;

// Either there was no role preference, or it could not be satisfied.
// Choose an arbitrary role, if there are any, and filter out any other roles.
// This ensures we never adapt between roles.

var allRoles = chosen.map(function(stream) {
return stream.roles;
}).reduce(shaka.util.Functional.collapseArrays, []);

if (!allRoles.length) {
return chosen;
}
return shaka.util.StreamUtils.filterTextStreamsByRole_(chosen, allRoles[0]);
};


/**
* Filter Variants by role.
*
* @param {!Array.<shakaExtern.Variant>} variants
* @param {string} preferredRole
* @return {!Array.<shakaExtern.Variant>}
* @private
*/
shaka.util.StreamUtils.filterVariantsByRole_ =
function(variants, preferredRole) {
return variants.filter(function(variant) {
return (variant.audio && variant.audio.roles.indexOf(preferredRole) >= 0) ||
(variant.video && variant.video.roles.indexOf(preferredRole) >= 0);
});
};


/**
* Filter text Streams by role.
*
* @param {!Array.<shakaExtern.Stream>} textStreams
* @param {string} preferredRole
* @return {!Array.<shakaExtern.Stream>}
* @private
*/
shaka.util.StreamUtils.filterTextStreamsByRole_ =
function(textStreams, preferredRole) {
return textStreams.filter(function(stream) {
return stream.roles.indexOf(preferredRole) >= 0;
});
};


Expand Down
Loading

0 comments on commit 72c07b3

Please sign in to comment.