diff --git a/src/controller/buffer-controller.ts b/src/controller/buffer-controller.ts index 334ce1221c8..bcdb23e4767 100644 --- a/src/controller/buffer-controller.ts +++ b/src/controller/buffer-controller.ts @@ -30,7 +30,6 @@ import { LevelDetails } from '../loader/level-details'; const MediaSource = getMediaSource(); const VIDEO_CODEC_PROFILE_REPACE = /([ha]vc.)(?:\.[^.,]+)+/; -const AUDIO_CODEC_REGEXP = /flac|opus/gi; export default class BufferController implements ComponentAPI { // The level details used to determine duration, target-duration and live @@ -257,11 +256,8 @@ export default class BufferController implements ComponentAPI { ); if (currentCodec !== nextCodec) { let trackCodec = levelCodec || codec; - if (trackName.indexOf('audio') !== -1) { - trackCodec = trackCodec.replace( - AUDIO_CODEC_REGEXP, - getCodecCompatibleName - ); + if (trackName.slice(0, 5) === 'audio') { + trackCodec = getCodecCompatibleName(trackCodec); } const mimeType = `${container};codecs=${trackCodec}`; this.appendChangeType(trackName, mimeType); @@ -723,8 +719,8 @@ export default class BufferController implements ComponentAPI { // use levelCodec as first priority let codec = track.levelCodec || track.codec; if (codec) { - if (trackName.indexOf('audio') !== -1) { - codec = codec.replace(AUDIO_CODEC_REGEXP, getCodecCompatibleName); + if (trackName.slice(0, 5) === 'audio') { + codec = getCodecCompatibleName(codec); } } const mimeType = `${track.container};codecs=${codec}`; diff --git a/src/controller/level-controller.ts b/src/controller/level-controller.ts index d0b785c6581..0d513cf8c90 100644 --- a/src/controller/level-controller.ts +++ b/src/controller/level-controller.ts @@ -26,8 +26,6 @@ const chromeOrFirefox: boolean = /chrome|firefox/.test( navigator.userAgent.toLowerCase() ); -const AUDIO_CODEC_REGEXP = /flac|opus/gi; - export default class LevelController extends BasePlaylistController { private _levels: Level[] = []; private _firstLevel: number = -1; @@ -112,10 +110,7 @@ export default class LevelController extends BasePlaylistController { } if (levelParsed.audioCodec) { - levelParsed.audioCodec = levelParsed.audioCodec.replace( - AUDIO_CODEC_REGEXP, - getCodecCompatibleName - ); + levelParsed.audioCodec = getCodecCompatibleName(levelParsed.audioCodec); } const levelKey = `${levelParsed.bitrate}-${levelParsed.attrs.RESOLUTION}-${levelParsed.attrs.CODECS}`; diff --git a/src/utils/codecs.ts b/src/utils/codecs.ts index 91d84a41a6f..6cf67f60ce5 100644 --- a/src/utils/codecs.ts +++ b/src/utils/codecs.ts @@ -118,6 +118,9 @@ function getCodecCompatibleNameLower( return lowerCaseCodec; } +const AUDIO_CODEC_REGEXP = /flac|opus/i; export function getCodecCompatibleName(codec: string): string { - return getCodecCompatibleNameLower(codec.toLowerCase() as LowerCaseCodecType); + return codec.replace(AUDIO_CODEC_REGEXP, (m) => + getCodecCompatibleNameLower(m.toLowerCase() as LowerCaseCodecType) + ); }