Skip to content

Commit

Permalink
Fix parsing of multiple codecs when converting from AVC1 to AVCOTI
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonatangcavalcanti authored and robwalch committed Jul 3, 2024
1 parent 841e6a8 commit 9c4eba9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/utils/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,20 @@ 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(',');
}

export interface TypeSupported {
Expand Down
1 change: 1 addition & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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';
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/utils/codecs.ts
Original file line number Diff line number Diff line change
@@ -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',
);
});
});

0 comments on commit 9c4eba9

Please sign in to comment.