Skip to content

Commit 38c5081

Browse files
committed
fix(player): Fix resolution changes with lang change.
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 #3262 Closes #3288 Change-Id: Ie4e529ad4ab942d74ae46318c605b45b8a07123b
1 parent 06982de commit 38c5081

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/player.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3541,6 +3541,38 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
35413541
new shaka.media.PreferenceBasedCriteria(language, role || '',
35423542
/* channelCount= */ 0, /* label= */ '');
35433543

3544+
if (!this.config_.abr.enabled) {
3545+
const diff = (a, b) => {
3546+
if (!a.video && !b.video) {
3547+
return 0;
3548+
} else if (!a.video || !b.video) {
3549+
return Infinity;
3550+
} else {
3551+
return Math.abs((a.video.height || 0) - (b.video.height || 0)) +
3552+
Math.abs((a.video.width || 0) - (b.video.width || 0));
3553+
}
3554+
};
3555+
// Find the variant whose size is closest to the active variant. This
3556+
// ensures we stay at about the same resolution when just changing the
3557+
// language/role.
3558+
const active = this.streamingEngine_.getCurrentVariant();
3559+
const set =
3560+
this.currentAdaptationSetCriteria_.create(this.manifest_.variants);
3561+
let bestVariant = null;
3562+
for (const curVariant of set.values()) {
3563+
if (!bestVariant ||
3564+
diff(bestVariant, active) > diff(curVariant, active)) {
3565+
bestVariant = curVariant;
3566+
}
3567+
}
3568+
if (bestVariant) {
3569+
const track = shaka.util.StreamUtils.variantToTrack(bestVariant);
3570+
this.selectVariantTrack(track, /* clearBuffer= */ true);
3571+
return;
3572+
}
3573+
}
3574+
3575+
// If we haven't switched yet, just use ABR to find a new track.
35443576
this.chooseVariantAndSwitch_();
35453577
} else if (this.video_ && this.video_.audioTracks) {
35463578
const audioTracks = Array.from(this.video_.audioTracks);

test/player_unit.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,22 @@ describe('Player', () => {
17271727
expect(getActiveVariantTrack().audioRoles).toEqual([]);
17281728
});
17291729

1730+
// https://github.com/google/shaka-player/issues/3262
1731+
it('selectAudioLanguage() doesn\'t change resolution', () => {
1732+
player.configure('abr.enabled', false);
1733+
abrManager.chooseIndex = 1;
1734+
const lowResEn =
1735+
variantTracks.filter((t) => t.language == 'en' && t.height == 200)[0];
1736+
player.selectVariantTrack(lowResEn);
1737+
1738+
// Switching to 'es' should keep the low-res stream and not choose the
1739+
// high-res version.
1740+
player.selectAudioLanguage('es');
1741+
const lowResEs =
1742+
variantTracks.filter((t) => t.language == 'es' && t.height == 200)[0];
1743+
expect(getActiveVariantTrack().id).toBe(lowResEs.id);
1744+
});
1745+
17301746
it('selectTextLanguage() does not change selected variant track', () => {
17311747
// This came up in a custom application that allows to select
17321748
// from all tracks regardless of selected language.

0 commit comments

Comments
 (0)