Skip to content

Commit

Permalink
feat: add preferred video label (#5948)
Browse files Browse the repository at this point in the history
Closes #5947
  • Loading branch information
koenoe authored Nov 29, 2023
1 parent 419b1c3 commit 503327a
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 19 deletions.
1 change: 1 addition & 0 deletions demo/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ shakaDemo.Config = class {
this.addSection_('Language', docLink)
.addTextInput_('Preferred Audio Language', 'preferredAudioLanguage')
.addTextInput_('Preferred Audio Label', 'preferredAudioLabel')
.addTextInput_('Preferred Video Label', 'preferredVideoLabel')
.addTextInput_('Preferred Variant Role', 'preferredVariantRole')
.addTextInput_('Preferred Text Language', 'preferredTextLanguage')
.addTextInput_('Preferred Text Role', 'preferredTextRole')
Expand Down
1 change: 1 addition & 0 deletions docs/tutorials/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ player.getConfiguration();
playRangeStart: 0
preferredAudioLanguage: ""
preferredAudioLabel: ""
preferredVideoLabel: ""
preferredTextLanguage: ""
restrictions: Object
streaming: Object
Expand Down
3 changes: 3 additions & 0 deletions externs/shaka/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,7 @@ shaka.extern.OfflineConfiguration;
* preferredAudioChannelCount: number,
* preferredVideoHdrLevel: string,
* preferredVideoLayout: string,
* preferredVideoLabel: string,
* preferredDecodingAttributes: !Array.<string>,
* preferForcedSubs: boolean,
* restrictions: shaka.extern.Restrictions,
Expand Down Expand Up @@ -1641,6 +1642,8 @@ shaka.extern.OfflineConfiguration;
* Changing this during playback will not affect the current playback.
* @property {string} preferredAudioLabel
* The preferred label to use for audio tracks
* @property {string} preferredVideoLabel
* The preferred label to use for video tracks
* @property {string} preferredTextLanguage
* The preferred language to use for text tracks. If a matching text track
* is found, and the selected audio and text tracks have different languages,
Expand Down
57 changes: 47 additions & 10 deletions lib/media/adaptation_set_criteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ shaka.media.ExampleBasedCriteria = class {
// We can't know if role and label are really important, so we don't use
// role and label for this.
const role = '';
const label = '';
const audioLabel = '';
const videoLabel = '';
const hdrLevel = '';
const videoLayout = '';
const channelCount = example.audio && example.audio.channelsCount ?
Expand All @@ -67,7 +68,8 @@ shaka.media.ExampleBasedCriteria = class {

/** @private {!shaka.media.AdaptationSetCriteria} */
this.fallback_ = new shaka.media.PreferenceBasedCriteria(
example.language, role, channelCount, hdrLevel, videoLayout, label,
example.language, role, channelCount, hdrLevel, videoLayout,
audioLabel, videoLabel,
codecSwitchingStrategy, enableAudioGroups);
}

Expand Down Expand Up @@ -107,11 +109,13 @@ shaka.media.PreferenceBasedCriteria = class {
* @param {number} channelCount
* @param {string} hdrLevel
* @param {string} videoLayout
* @param {string=} label
* @param {string=} audioLabel
* @param {string=} videoLabel
* @param {shaka.config.CodecSwitchingStrategy=} codecSwitchingStrategy
* @param {boolean=} enableAudioGroups
*/
constructor(language, role, channelCount, hdrLevel, videoLayout, label = '',
constructor(language, role, channelCount, hdrLevel, videoLayout,
audioLabel = '', videoLabel = '',
codecSwitchingStrategy = shaka.config.CodecSwitchingStrategy.RELOAD,
enableAudioGroups = false) {
/** @private {string} */
Expand All @@ -125,7 +129,9 @@ shaka.media.PreferenceBasedCriteria = class {
/** @private {string} */
this.videoLayout_ = videoLayout;
/** @private {string} */
this.label_ = label;
this.audioLabel_ = audioLabel;
/** @private {string} */
this.videoLabel_ = videoLabel;
/** @private {shaka.config.CodecSwitchingStrategy} */
this.codecSwitchingStrategy_ = codecSwitchingStrategy;
/** @private {boolean} */
Expand Down Expand Up @@ -191,12 +197,23 @@ shaka.media.PreferenceBasedCriteria = class {
}
}

if (this.label_) {
const byLabel = Class.filterVariantsByLabel_(current, this.label_);
if (this.audioLabel_) {
const byLabel = Class.filterVariantsByAudioLabel_(
current, this.audioLabel_);
if (byLabel.length) {
current = byLabel;
} else {
shaka.log.warning('No exact match for audio label could be found.');
}
}

if (this.videoLabel_) {
const byLabel = Class.filterVariantsByVideoLabel_(
current, this.videoLabel_);
if (byLabel.length) {
current = byLabel;
} else {
shaka.log.warning('No exact match for variant label could be found.');
shaka.log.warning('No exact match for video label could be found.');
}
}

Expand Down Expand Up @@ -259,14 +276,14 @@ shaka.media.PreferenceBasedCriteria = class {
}

/**
* Filter Variants by label.
* Filter Variants by audio label.
*
* @param {!Array.<shaka.extern.Variant>} variants
* @param {string} preferredLabel
* @return {!Array.<shaka.extern.Variant>}
* @private
*/
static filterVariantsByLabel_(variants, preferredLabel) {
static filterVariantsByAudioLabel_(variants, preferredLabel) {
return variants.filter((variant) => {
if (!variant.audio || !variant.audio.label) {
return false;
Expand All @@ -278,6 +295,26 @@ shaka.media.PreferenceBasedCriteria = class {
});
}

/**
* Filter Variants by video label.
*
* @param {!Array.<shaka.extern.Variant>} variants
* @param {string} preferredLabel
* @return {!Array.<shaka.extern.Variant>}
* @private
*/
static filterVariantsByVideoLabel_(variants, preferredLabel) {
return variants.filter((variant) => {
if (!variant.video || !variant.video.label) {
return false;
}

const label1 = variant.video.label.toLowerCase();
const label2 = preferredLabel.toLowerCase();
return label1 == label2;
});
}

/**
* Filter Variants by channelCount.
*
Expand Down
6 changes: 5 additions & 1 deletion lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.config_.preferredVideoHdrLevel,
this.config_.preferredVideoLayout,
this.config_.preferredAudioLabel,
this.config_.preferredVideoLabel,
this.config_.mediaSource.codecSwitchingStrategy,
this.config_.manifest.dash.enableAudioGroups);

Expand Down Expand Up @@ -1858,6 +1859,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.config_.preferredVideoHdrLevel,
this.config_.preferredVideoLayout,
this.config_.preferredAudioLabel,
this.config_.preferredVideoLabel,
this.config_.mediaSource.codecSwitchingStrategy,
this.config_.manifest.dash.enableAudioGroups);

Expand Down Expand Up @@ -4063,7 +4065,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
channelsCount,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ '',
/* audioLabel= */ '',
/* videoLabel= */ '',
this.config_.mediaSource.codecSwitchingStrategy,
this.config_.manifest.dash.enableAudioGroups);

Expand Down Expand Up @@ -4188,6 +4191,7 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
/* hdrLevel= */ '',
/* videoLayout= */ '',
label,
/* videoLabel= */ '',
this.config_.mediaSource.codecSwitchingStrategy,
this.config_.manifest.dash.enableAudioGroups);

Expand Down
1 change: 1 addition & 0 deletions lib/util/player_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ shaka.util.PlayerConfiguration = class {
preferredAudioChannelCount: 2,
preferredVideoHdrLevel: 'AUTO',
preferredVideoLayout: '',
preferredVideoLabel: '',
preferredVideoCodecs: [],
preferredAudioCodecs: [],
preferForcedSubs: false,
Expand Down
54 changes: 46 additions & 8 deletions test/media/adaptation_set_criteria_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ describe('AdaptationSetCriteria', () => {
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ '',
/* audioLabel= */ '',
/* videoLabel= */ '',
shaka.config.CodecSwitchingStrategy.SMOOTH);
const set = builder.create(manifest.variants);

Expand All @@ -113,7 +114,7 @@ describe('AdaptationSetCriteria', () => {
}
});

it('should filter varaints when codec switching startegy'+
it('should filter varaints when codec switching strategy'+
'is not SMOOTH', () => {
const manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(1, (variant) => {
Expand Down Expand Up @@ -148,7 +149,8 @@ describe('AdaptationSetCriteria', () => {
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ '',
/* audioLabel= */ '',
/* videoLabel= */ '',
shaka.config.CodecSwitchingStrategy.RELOAD);
const set = builder.create(manifest.variants);

Expand Down Expand Up @@ -671,7 +673,7 @@ describe('AdaptationSetCriteria', () => {
]);
});

it('chooses variants with preferred label', () => {
it('chooses variants with preferred audio label', () => {
const manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(1, (variant) => {
variant.addAudio(10, (stream) => {
Expand All @@ -696,7 +698,7 @@ describe('AdaptationSetCriteria', () => {
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ 'preferredLabel');
/* audioLabel= */ 'preferredLabel');
const set = builder.create(manifest.variants);

checkSet(set, [
Expand All @@ -705,7 +707,7 @@ describe('AdaptationSetCriteria', () => {
]);
});

it('chooses variants with preferred label and language', () => {
it('chooses variants with preferred audio label and language', () => {
const manifest = shaka.test.ManifestGenerator.generate((manifest) => {
// Preferred language and label
manifest.addVariant(1, (variant) => {
Expand Down Expand Up @@ -743,7 +745,42 @@ describe('AdaptationSetCriteria', () => {
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ 'preferredLabel');
/* audioLabel= */ 'preferredLabel');
const set = builder.create(manifest.variants);

checkSet(set, [
manifest.variants[0],
manifest.variants[2],
]);
});

it('chooses variants with preferred video label', () => {
const manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(1, (variant) => {
variant.addVideo(10, (stream) => {
stream.label = 'preferredLabel';
});
});
manifest.addVariant(2, (variant) => {
variant.addVideo(20, (stream) => {
stream.label = 'otherLabel';
});
});
manifest.addVariant(3, (variant) => {
variant.addVideo(30, (stream) => {
stream.label = 'preferredLabel';
});
});
});

const builder = new shaka.media.PreferenceBasedCriteria(
/* language= */ '',
/* role= */ '',
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* audioLabel= */ '',
/* videoLabel= */ 'preferredLabel');
const set = builder.create(manifest.variants);

checkSet(set, [
Expand Down Expand Up @@ -783,7 +820,8 @@ describe('AdaptationSetCriteria', () => {
/* channelCount= */ 0,
/* hdrLevel= */ '',
/* videoLayout= */ '',
/* label= */ '',
/* audioLabel= */ '',
/* videoLabel= */ '',
shaka.config.CodecSwitchingStrategy.RELOAD,
/* enableAudioGroups= */ true);
const set = builder.create(manifest.variants);
Expand Down

0 comments on commit 503327a

Please sign in to comment.