From b62c72df16f9032c3bccaa024e5aab345dba2df2 Mon Sep 17 00:00:00 2001 From: Jhonatan Gomes Date: Tue, 2 Jul 2024 19:33:43 -0300 Subject: [PATCH] Fix parsing of multiple codecs when converting from AVC1 to AVCOTI (cherry picked from commit 9c4eba92f7c28d503c6b271560a17f1a08fa5270) --- src/utils/codecs.ts | 22 +++++++++++++--------- tests/index.js | 1 + tests/unit/utils/codecs.ts | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 tests/unit/utils/codecs.ts diff --git a/src/utils/codecs.ts b/src/utils/codecs.ts index eb42ff9f975..ef25dd39b18 100644 --- a/src/utils/codecs.ts +++ b/src/utils/codecs.ts @@ -198,14 +198,18 @@ export function pickMostCompleteCodecName( export function convertAVC1ToAVCOTI(codec: string) { // Convert avc1 codec string from RFC-4281 to RFC-6381 for MediaSource.isTypeSupported - const avcdata = codec.split('.'); - if (avcdata.length > 2) { - let result = avcdata.shift() + '.'; - result += parseInt(avcdata.shift() as string).toString(16); - result += ('000' + parseInt(avcdata.shift() as string).toString(16)).slice( - -4, - ); - return result; + // Examples: avc1.66.30 to avc1.42001e and avc1.77.30,avc1.66.30 to avc1.4d001e,avc1.42001e. + const codecs = codec.split(','); + for (let i = 0; i < codecs.length; i++) { + const avcdata = codecs[i].split('.'); + if (avcdata.length > 2) { + let result = avcdata.shift() + '.'; + result += parseInt(avcdata.shift() as string).toString(16); + result += ( + '000' + parseInt(avcdata.shift() as string).toString(16) + ).slice(-4); + codecs[i] = result; + } } - return codec; + return codecs.join(','); } diff --git a/tests/index.js b/tests/index.js index 17e3f85fd80..ccb3c6da1ac 100644 --- a/tests/index.js +++ b/tests/index.js @@ -38,6 +38,7 @@ import './unit/loader/playlist-loader'; import './unit/utils/attr-list'; import './unit/utils/binary-search'; import './unit/utils/buffer-helper'; +import './unit/utils/codecs'; import './unit/utils/error-helper'; import './unit/utils/discontinuities'; import './unit/utils/exp-golomb'; diff --git a/tests/unit/utils/codecs.ts b/tests/unit/utils/codecs.ts new file mode 100644 index 00000000000..f0bedf515bc --- /dev/null +++ b/tests/unit/utils/codecs.ts @@ -0,0 +1,24 @@ +import { expect } from 'chai'; +import { convertAVC1ToAVCOTI } from '../../../src/utils/codecs'; + +describe('codecs', function () { + it('convert codec string from AVC1 to AVCOTI', function () { + expect(convertAVC1ToAVCOTI('avc1.66.30')).to.equal('avc1.42001e'); + }); + + it('convert list of codecs string from AVC1 to AVCOTI', function () { + expect(convertAVC1ToAVCOTI('avc1.77.30,avc1.66.30')).to.equal( + 'avc1.4d001e,avc1.42001e', + ); + }); + + it('does not convert string if it is already converted', function () { + expect(convertAVC1ToAVCOTI('avc1.64001E')).to.equal('avc1.64001E'); + }); + + it('does not convert list of codecs string if it is already converted', function () { + expect(convertAVC1ToAVCOTI('avc1.64001E,avc1.64001f')).to.equal( + 'avc1.64001E,avc1.64001f', + ); + }); +});