Skip to content

Commit

Permalink
fix: Fix missing audio streams (#5869)
Browse files Browse the repository at this point in the history
Fixes #5858
  • Loading branch information
avelad authored Nov 13, 2023
1 parent 42f491f commit d6aab6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/util/stream_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,34 @@ shaka.util.StreamUtils = class {
const audioStreams = Array.from(audioStreamsSet).sort((v1, v2) => {
return v1.bandwidth - v2.bandwidth;
});
const audioStreamsMap = new Map();
const validAudioIds = [];
const validAudioStreamsMap = new Map();
const getAudioId = (stream) => {
return stream.language + (stream.channelsCount || 0) +
(stream.audioSamplingRate || 0) + stream.roles.join(',') +
stream.label + stream.groupId + stream.fastSwitching;
};
for (const stream of audioStreams) {
const id = getAudioId(stream);
if (!audioStreamsMap.has(id)) {
audioStreamsMap.set(id, stream);
const groupId = getAudioId(stream);
const validAudioStreams = validAudioStreamsMap.get(groupId) || [];
if (!validAudioStreams.length) {
validAudioStreams.push(stream);
validAudioIds.push(stream.id);
} else {
const includedStream = audioStreamsMap.get(id);
if (includedStream.bandwidth > stream.bandwidth) {
audioStreamsMap.set(id, stream);
const previousStream = validAudioStreams[validAudioStreams.length - 1];
const previousCodec =
MimeUtils.getNormalizedCodec(previousStream.codecs);
const currentCodec =
MimeUtils.getNormalizedCodec(stream.codecs);
if (previousCodec == currentCodec) {
if (stream.bandwidth > previousStream.bandwidth) {
validAudioStreams.push(stream);
validAudioIds.push(stream.id);
}
}
}
validAudioStreamsMap.set(groupId, validAudioStreams);
}
const validAudioStreams = [...audioStreamsMap.values()];

const videoStreams = Array.from(videoStreamsSet)
.sort((v1, v2) => {
Expand Down Expand Up @@ -161,9 +171,6 @@ shaka.util.StreamUtils = class {
validVideoStreamsMap.set(groupId, validVideoStreams);
}

const validAudioIds =
validAudioStreams.map((audioStream) => audioStream.id);

// Filter out any variants that don't match, forcing AbrManager to choose
// from a single video codec and a single audio codec possible.
manifest.variants = manifest.variants.filter((variant) => {
Expand Down
8 changes: 8 additions & 0 deletions test/util/stream_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,31 +928,39 @@ describe('StreamUtils', () => {

it('should filter variants by the best available bandwidth' +
' for audio language', () => {
// This test is flaky in some Tizen devices, due to codec restrictions.
if (shaka.util.Platform.isTizen()) {
pending('Skip flaky test in Tizen');
}
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
manifest.addVariant(0, (variant) => {
variant.bandwidth = 4058558;
variant.addAudio(1, (stream) => {
stream.bandwidth = 100000;
stream.language = 'en';
stream.mime('audio/mp4', 'mp4a.40.2');
});
});
manifest.addVariant(2, (variant) => {
variant.bandwidth = 4781002;
variant.addAudio(3, (stream) => {
stream.bandwidth = 200000;
stream.language = 'en';
stream.mime('audio/mp4', 'flac');
});
});
manifest.addVariant(4, (variant) => {
variant.addAudio(5, (stream) => {
stream.bandwidth = 100000;
stream.language = 'es';
stream.mime('audio/mp4', 'mp4a.40.2');
});
});
manifest.addVariant(6, (variant) => {
variant.addAudio(7, (stream) => {
stream.bandwidth = 500000;
stream.language = 'es';
stream.mime('audio/mp4', 'flac');
});
});
});
Expand Down

0 comments on commit d6aab6b

Please sign in to comment.