From d6aab6befaf72eb87e431f5b27cb6c6bc368b56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Velad=20Galv=C3=A1n?= Date: Mon, 13 Nov 2023 09:10:12 +0100 Subject: [PATCH] fix: Fix missing audio streams (#5869) Fixes https://github.com/shaka-project/shaka-player/issues/5858 --- lib/util/stream_utils.js | 29 ++++++++++++++++++----------- test/util/stream_utils_unit.js | 8 ++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/util/stream_utils.js b/lib/util/stream_utils.js index e61ae0266c..f67019d5ee 100644 --- a/lib/util/stream_utils.js +++ b/lib/util/stream_utils.js @@ -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) => { @@ -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) => { diff --git a/test/util/stream_utils_unit.js b/test/util/stream_utils_unit.js index 2e52557cb9..12f04704f4 100644 --- a/test/util/stream_utils_unit.js +++ b/test/util/stream_utils_unit.js @@ -928,12 +928,17 @@ 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) => { @@ -941,18 +946,21 @@ describe('StreamUtils', () => { 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'); }); }); });