From 193c89802e9dd7e663faefd44e168f39ac3053a1 Mon Sep 17 00:00:00 2001 From: gnattu Date: Fri, 26 Apr 2024 06:00:24 +0800 Subject: [PATCH] Fix MP3 audio playback capability checking with HLS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous check was too naive, assuming that most browsers that support playing MP3 directly in an mp4 file can support MP3 with HLS. However, this assumption is wrong. In fact, most browsers won’t play MP3 with HLS, with Safari being the only exception. Even on Safari, MP3 support with HLS is limited to the mpegts container, and it won’t play MP3 in the fmp4 container. --- src/scripts/browserDeviceProfile.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/scripts/browserDeviceProfile.js b/src/scripts/browserDeviceProfile.js index 33e07e4f0c..f3908d9999 100644 --- a/src/scripts/browserDeviceProfile.js +++ b/src/scripts/browserDeviceProfile.js @@ -121,6 +121,16 @@ function supportsAc3InHls(videoTestElement) { return false; } +function supportsMp3InHls(videoTestElement) { + + if (videoTestElement.canPlayType) { + return videoTestElement.canPlayType('application/x-mpegurl; codecs="avc1.42E01E, mp4a.40.34"').replace(/no/, '') + || videoTestElement.canPlayType('application/vnd.apple.mpegURL; codecs="avc1.42E01E, mp4a.40.34"').replace(/no/, ''); + } + + return false; +} + function canPlayAudioFormat(format) { let typeString; @@ -460,6 +470,7 @@ export default function (options) { } const canPlayAacVideoAudio = videoTestElement.canPlayType('video/mp4; codecs="avc1.640029, mp4a.40.2"').replace(/no/, ''); + const canPlayMp3VideoAudioInHls = supportsMp3InHls(videoTestElement); const canPlayAc3VideoAudio = supportsAc3(videoTestElement); const canPlayEac3VideoAudio = supportsEac3(videoTestElement); const canPlayAc3VideoAudioInHls = supportsAc3InHls(videoTestElement); @@ -474,12 +485,16 @@ export default function (options) { if (supportsMp3VideoAudio) { videoAudioCodecs.push('mp3'); + } - // PS4 fails to load HLS with mp3 audio - if (!browser.ps4) { - hlsInTsVideoAudioCodecs.push('mp3'); - } + // Safari is the only browser that supports mp3 with HLS, but only in mpegts container. + // The detect function will return false on Safari, which reflects the fmp4 support, so we have to hard-code it here. + if (browser.safari || canPlayMp3VideoAudioInHls) { + hlsInTsVideoAudioCodecs.push('mp3'); + } + // Most browsers won't support mp3 with HLS, so this is usually false, but just in case. + if (canPlayMp3VideoAudioInHls) { hlsInFmp4VideoAudioCodecs.push('mp3'); }