Skip to content

Commit 9f720ae

Browse files
committed
fix: Avoid audio streams filtering and use AdaptationSetCriteria instead
1 parent 4d99357 commit 9f720ae

File tree

4 files changed

+11
-118
lines changed

4 files changed

+11
-118
lines changed

lib/media/preload_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ shaka.media.PreloadManager = class extends shaka.util.FakeEventTarget {
661661
language: this.config_.preferredAudioLanguage,
662662
role: this.config_.preferredAudioRole,
663663
videoRole: this.config_.preferredVideoRole,
664-
channelCount: this.config_.preferredAudioChannelCount,
664+
channelCount: 0,
665665
hdrLevel: this.config_.preferredVideoHdrLevel,
666666
spatialAudio: this.config_.preferSpatialAudio,
667667
videoLayout: this.config_.preferredVideoLayout,

lib/player.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5665,6 +5665,14 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
56655665
config.language = audioTrack.language;
56665666
config.role = audioTrack.roles[0] || '';
56675667
config.spatialAudio = audioTrack.spatialAudio;
5668+
5669+
const active = this.streamingEngine_.getCurrentVariant();
5670+
const activeAudio = active && active.audio;
5671+
if (activeAudio) {
5672+
config.activeAudioCodec = activeAudio.codecs || '';
5673+
config.activeAudioChannelCount = activeAudio.channelsCount || 0;
5674+
}
5675+
56685676
this.currentAdaptationSetCriteria_.configure(config);
56695677
this.chooseVariantAndSwitch_(
56705678
/* clearBuffer= */ true, /* safeMargin= */ safeMargin,

lib/util/stream_utils.js

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -108,73 +108,15 @@ shaka.util.StreamUtils = class {
108108
// Now we have filtered variants and text tracks down by user preferences
109109
// for formats, codecs, and decoding attributes (smooth, efficient, etc).
110110

111-
const audioStreamsSet = new Set();
112111
const videoStreamsSet = new Set();
113112
for (const variant of variants) {
114-
if (variant.audio) {
115-
audioStreamsSet.add(variant.audio);
116-
}
117113
if (variant.video) {
118114
videoStreamsSet.add(variant.video);
119115
}
120116
}
121117

122-
// Audio streams sorted by bandwidth, lowest to highest.
123-
const audioStreams = Array.from(audioStreamsSet).sort((v1, v2) => {
124-
return v1.bandwidth - v2.bandwidth;
125-
});
126-
127-
// Maps that are used to choose codecs when there are no preferences in
128-
// effect above.
129-
const validAudioIds = [];
130-
const validAudioStreamsMap = new Map();
131-
132-
// A group ID composed of attributes like language, channels, role, etc to
133-
// find redundant audio streams that only differ in terms of things like
134-
// codec, format, and bitrate.
135-
const getAudioGroupId = (stream) => {
136-
const idParts = [
137-
stream.language,
138-
(stream.channelsCount || 0),
139-
(stream.audioSamplingRate || 0),
140-
stream.roles.join(','),
141-
stream.label,
142-
stream.groupId,
143-
stream.fastSwitching,
144-
];
145-
if (stream.dependencyStream) {
146-
idParts.push(stream.dependencyStream.baseOriginalId || '');
147-
}
148-
return idParts.join(';');
149-
};
150-
151-
// For the first audio stream in a group (lowest bandwidth by sorting
152-
// above), add it to the map. For subsequent audio streams in a group,
153-
// only add it to the map if the codec matches. This could make a bad
154-
// decision if not every audio codec is used on every quality level in the
155-
// audio ladder. In scenarios like this, use preferredAudioCodecs. If
156-
// preferredAudioCodecs is used, there should only be one codec left when
157-
// we get to this stage.
158-
for (const stream of audioStreams) {
159-
const groupId = getAudioGroupId(stream);
160-
const validAudioStreams = validAudioStreamsMap.get(groupId) || [];
161-
162-
if (!validAudioStreams.length) {
163-
validAudioStreams.push(stream);
164-
validAudioIds.push(stream.id);
165-
} else {
166-
const previousStream = validAudioStreams[validAudioStreams.length - 1];
167-
const previousCodec =
168-
MimeUtils.getNormalizedCodec(previousStream.codecs);
169-
const currentCodec =
170-
MimeUtils.getNormalizedCodec(stream.codecs);
171-
if (previousCodec == currentCodec) {
172-
validAudioStreams.push(stream);
173-
validAudioIds.push(stream.id);
174-
}
175-
}
176-
177-
validAudioStreamsMap.set(groupId, validAudioStreams);
118+
if (!videoStreamsSet.size) {
119+
return;
178120
}
179121

180122
// Keys based in MimeUtils.getNormalizedCodec. Lower is better
@@ -271,14 +213,7 @@ shaka.util.StreamUtils = class {
271213
// from a single video codec and a single audio codec **per stream group**
272214
// (resolution, etc).
273215
manifest.variants = manifest.variants.filter((variant) => {
274-
const audio = variant.audio;
275216
const video = variant.video;
276-
if (audio) {
277-
if (!validAudioIds.includes(audio.id)) {
278-
shaka.log.debug('Dropping Variant (better codec available)', variant);
279-
return false;
280-
}
281-
}
282217
if (video) {
283218
if (!validVideoIds.includes(video.id)) {
284219
shaka.log.debug('Dropping Variant (better codec available)', variant);

test/util/stream_utils_unit.js

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -898,56 +898,6 @@ describe('StreamUtils', () => {
898898
expect(manifest.variants.length).toBe(3);
899899
});
900900

901-
it('should filter variants by the best available bandwidth' +
902-
' for audio language', () => {
903-
// This test is flaky in some Tizen devices, due to codec restrictions.
904-
if (deviceDetected.getDeviceName() === 'Tizen') {
905-
pending('Skip flaky test in Tizen');
906-
}
907-
manifest = shaka.test.ManifestGenerator.generate((manifest) => {
908-
manifest.addVariant(0, (variant) => {
909-
variant.bandwidth = 4058558;
910-
variant.addAudio(1, (stream) => {
911-
stream.bandwidth = 100000;
912-
stream.language = 'en';
913-
stream.mime('audio/mp4', 'mp4a.40.2');
914-
});
915-
});
916-
manifest.addVariant(2, (variant) => {
917-
variant.bandwidth = 4781002;
918-
variant.addAudio(3, (stream) => {
919-
stream.bandwidth = 200000;
920-
stream.language = 'en';
921-
stream.mime('audio/mp4', 'flac');
922-
});
923-
});
924-
manifest.addVariant(4, (variant) => {
925-
variant.addAudio(5, (stream) => {
926-
stream.bandwidth = 100000;
927-
stream.language = 'es';
928-
stream.mime('audio/mp4', 'mp4a.40.2');
929-
});
930-
});
931-
manifest.addVariant(6, (variant) => {
932-
variant.addAudio(7, (stream) => {
933-
stream.bandwidth = 500000;
934-
stream.language = 'es';
935-
stream.mime('audio/mp4', 'flac');
936-
});
937-
});
938-
});
939-
940-
shaka.util.StreamUtils.chooseCodecsAndFilterManifest(manifest,
941-
/* preferredVideoCodecs= */[],
942-
/* preferredAudioCodecs= */[],
943-
/* preferredDecodingAttributes= */[],
944-
/* preferredTextFormats= */ []);
945-
946-
expect(manifest.variants.length).toBe(2);
947-
expect(manifest.variants.every((v) => v.audio.bandwidth == 100000))
948-
.toBeTruthy();
949-
});
950-
951901
it('should allow multiple codecs for codec switching', async () => {
952902
if (!await Util.isTypeSupported('video/webm; codecs="vp9"')) {
953903
pending('Codec VP9 is not supported by the platform.');

0 commit comments

Comments
 (0)