Skip to content

Commit

Permalink
fix: parse unknown codecs as audio or video (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Jul 16, 2020
1 parent 712c0c2 commit cd2c9bb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/codecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,44 @@ export const mapLegacyAvcCodecs = function(codecString) {
export const parseCodecs = function(codecString = '') {
const codecs = codecString.split(',');
const result = {};
const unknown = [];

codecs.forEach(function(codec) {
codec = codec.trim();
let codecType;

['video', 'audio'].forEach(function(name) {
const match = regexs[name].exec(codec.toLowerCase());

if (!match || match.length <= 1) {
return;
}
codecType = name;

// maintain codec case
const type = codec.substring(0, match[1].length);
const details = codec.replace(type, '');

result[name] = {type, details};
});

// codec type is not audio or video
// try to deremine its type after all the other codecs
if (!codecType) {
unknown.push(codec);
}
});

// TODO: Report that a codec could not be identified somehow
// Set unknown codecs to either the "missing" audio or video codec.
// If we have all unknown codecs the first one will always be
// video and the second one will always be audio.
unknown.forEach(function(codec) {
if (!result.video) {
result.video = {type: codec, details: ''};
} else if (!result.audio) {
result.audio = {type: codec, details: ''};
}
});

return result;
Expand Down
33 changes: 33 additions & 0 deletions test/codecs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,39 @@ QUnit.test('parses video and audio codec with mixed case', function(assert) {
);
});

QUnit.test('parses two unknown codec', function(assert) {
assert.deepEqual(
parseCodecs('fake.codec, other-fake'),
{
video: {type: 'fake.codec', details: ''},
audio: {type: 'other-fake', details: ''}
},
'parsed faked codecs as video/audio'
);
});

QUnit.test('parses on unknown video codec with a known audio', function(assert) {
assert.deepEqual(
parseCodecs('fake.codec, mp4a.40.2'),
{
video: {type: 'fake.codec', details: ''},
audio: {type: 'mp4a', details: '.40.2'}
},
'parsed faked video codec'
);
});

QUnit.test('parses on unknown audio codec with a known video', function(assert) {
assert.deepEqual(
parseCodecs('avc1.42001e, other-fake'),
{
video: {type: 'avc1', details: '.42001e'},
audio: {type: 'other-fake', details: ''}
},
'parsed faked audio codec'
);
});

QUnit.module('codecsFromDefault');

QUnit.test('returns falsey when no audio group ID', function(assert) {
Expand Down

0 comments on commit cd2c9bb

Please sign in to comment.