Skip to content

Commit

Permalink
fix(player): Fix resolution changes with lang change.
Browse files Browse the repository at this point in the history
Now, when changing audio languages, we'll try to stick with the same
resolution we were at before.  This only happens with ABR disabled
since ABR will switch resolutions anyway.

Fixes shaka-project#3262
Closes shaka-project#3288

Change-Id: Ie4e529ad4ab942d74ae46318c605b45b8a07123b
  • Loading branch information
TheModMaker committed Mar 30, 2021
1 parent 06982de commit 38c5081
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -3541,6 +3541,38 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
new shaka.media.PreferenceBasedCriteria(language, role || '',
/* channelCount= */ 0, /* label= */ '');

if (!this.config_.abr.enabled) {
const diff = (a, b) => {
if (!a.video && !b.video) {
return 0;
} else if (!a.video || !b.video) {
return Infinity;
} else {
return Math.abs((a.video.height || 0) - (b.video.height || 0)) +
Math.abs((a.video.width || 0) - (b.video.width || 0));
}
};
// Find the variant whose size is closest to the active variant. This
// ensures we stay at about the same resolution when just changing the
// language/role.
const active = this.streamingEngine_.getCurrentVariant();
const set =
this.currentAdaptationSetCriteria_.create(this.manifest_.variants);
let bestVariant = null;
for (const curVariant of set.values()) {
if (!bestVariant ||
diff(bestVariant, active) > diff(curVariant, active)) {
bestVariant = curVariant;
}
}
if (bestVariant) {
const track = shaka.util.StreamUtils.variantToTrack(bestVariant);
this.selectVariantTrack(track, /* clearBuffer= */ true);
return;
}
}

// If we haven't switched yet, just use ABR to find a new track.
this.chooseVariantAndSwitch_();
} else if (this.video_ && this.video_.audioTracks) {
const audioTracks = Array.from(this.video_.audioTracks);
Expand Down
16 changes: 16 additions & 0 deletions test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,22 @@ describe('Player', () => {
expect(getActiveVariantTrack().audioRoles).toEqual([]);
});

// https://github.com/google/shaka-player/issues/3262
it('selectAudioLanguage() doesn\'t change resolution', () => {
player.configure('abr.enabled', false);
abrManager.chooseIndex = 1;
const lowResEn =
variantTracks.filter((t) => t.language == 'en' && t.height == 200)[0];
player.selectVariantTrack(lowResEn);

// Switching to 'es' should keep the low-res stream and not choose the
// high-res version.
player.selectAudioLanguage('es');
const lowResEs =
variantTracks.filter((t) => t.language == 'es' && t.height == 200)[0];
expect(getActiveVariantTrack().id).toBe(lowResEs.id);
});

it('selectTextLanguage() does not change selected variant track', () => {
// This came up in a custom application that allows to select
// from all tracks regardless of selected language.
Expand Down

0 comments on commit 38c5081

Please sign in to comment.