Skip to content

Commit

Permalink
fix: Select first of identical audio streams (#3869)
Browse files Browse the repository at this point in the history
If a manifest lists 2 audio streams, select the first acceptable stream instead of the last one. For example below, previously the 2nd stream was selected, and now the first stream will be selected. Other players like roku, video.js and exoplayer select the first one.

```
#EXT-X-MEDIA:TYPE=AUDIO,URI="stream_1.m3u8",GROUP-ID="default-audio-group",NAME="128k",AUTOSELECT=YES,CHANNELS="2"
#EXT-X-MEDIA:TYPE=AUDIO,URI="stream_2.m3u8",GROUP-ID="default-audio-group",NAME="64k",CHANNELS="2"
(video streams snipped)
```
  • Loading branch information
wjywbs committed Feb 17, 2022
1 parent 49fbcb4 commit a6d8610
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 12 additions & 6 deletions lib/abr/simple_abr_manager.js
Expand Up @@ -9,7 +9,6 @@ goog.provide('shaka.abr.SimpleAbrManager');
goog.require('goog.asserts');
goog.require('shaka.abr.EwmaBandwidthEstimator');
goog.require('shaka.log');
goog.require('shaka.util.Iterables');
goog.require('shaka.util.StreamUtils');


Expand Down Expand Up @@ -145,23 +144,30 @@ shaka.abr.SimpleAbrManager = class {
// Start by assuming that we will use the first Stream.
let chosen = sortedVariants[0] || null;

const enumerate = (it) => shaka.util.Iterables.enumerate(it);
for (const {item, next} of enumerate(sortedVariants)) {
for (let i = 0; i < sortedVariants.length; i++) {
const item = sortedVariants[i];
const playbackRate =
!isNaN(this.playbackRate_) ? Math.abs(this.playbackRate_) : 1;
const itemBandwidth = playbackRate * item.bandwidth;
const minBandwidth =
itemBandwidth / this.config_.bandwidthDowngradeTarget;
const nextBandwidth =
playbackRate * (next || {bandwidth: Infinity}).bandwidth;
let next = {bandwidth: Infinity};
for (let j = i + 1; j < sortedVariants.length; j++) {
if (item.bandwidth != sortedVariants[j].bandwidth) {
next = sortedVariants[j];
break;
}
}
const nextBandwidth = playbackRate * next.bandwidth;
const maxBandwidth = nextBandwidth / this.config_.bandwidthUpgradeTarget;
shaka.log.v2('Bandwidth ranges:',
(itemBandwidth / 1e6).toFixed(3),
(minBandwidth / 1e6).toFixed(3),
(maxBandwidth / 1e6).toFixed(3));

if (currentBandwidth >= minBandwidth &&
currentBandwidth <= maxBandwidth) {
currentBandwidth <= maxBandwidth &&
chosen.bandwidth != item.bandwidth) {
chosen = item;
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/abr/simple_abr_manager_unit.js
Expand Up @@ -91,7 +91,7 @@ describe('SimpleAbrManager', () => {
config.defaultBandwidthEstimate = 3e6;
abrManager.configure(config);
const chosen = abrManager.chooseVariant();
expect(chosen.id).toBe(104);
expect(chosen.id).toBe(103);
});

it('can handle empty variants', () => {
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('SimpleAbrManager', () => {
abrManager.segmentDownloaded(1000, bytesPerSecond);

// Expect variants 4 to be chosen
const expectedVariant = variants[4];
const expectedVariant = variants[3];

expect(switchCallback).toHaveBeenCalledWith(expectedVariant);
});
Expand Down

0 comments on commit a6d8610

Please sign in to comment.