codecs.test.js
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import window from 'global/window';
import QUnit from 'qunit';
import {
mapLegacyAvcCodecs,
translateLegacyCodecs,
parseCodecs,
codecsFromDefault,
isVideoCodec,
isAudioCodec,
muxerSupportsCodec,
browserSupportsCodec,
getMimeForCodec
} from '../src/codecs';
const supportedMuxerCodecs = [
'mp4a',
'avc1'
];
const unsupportedMuxerCodecs = [
'hvc1',
'ac-3',
'ec-3',
'mp3'
];
QUnit.module('Legacy Codecs');
QUnit.test('maps legacy AVC codecs', function(assert) {
assert.equal(
mapLegacyAvcCodecs('avc1.deadbeef'),
'avc1.deadbeef',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('avc1.dead.beef, mp4a.something'),
'avc1.dead.beef, mp4a.something',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('avc1.dead.beef,mp4a.something'),
'avc1.dead.beef,mp4a.something',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('mp4a.something,avc1.dead.beef'),
'mp4a.something,avc1.dead.beef',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('mp4a.something, avc1.dead.beef'),
'mp4a.something, avc1.dead.beef',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('avc1.42001e'),
'avc1.42001e',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('avc1.4d0020,mp4a.40.2'),
'avc1.4d0020,mp4a.40.2',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('mp4a.40.2,avc1.4d0020'),
'mp4a.40.2,avc1.4d0020',
'does nothing for non legacy pattern'
);
assert.equal(
mapLegacyAvcCodecs('mp4a.40.40'),
'mp4a.40.40',
'does nothing for non video codecs'
);
assert.equal(
mapLegacyAvcCodecs('avc1.66.30'),
'avc1.42001e',
'translates legacy video codec alone'
);
assert.equal(
mapLegacyAvcCodecs('avc1.66.30, mp4a.40.2'),
'avc1.42001e, mp4a.40.2',
'translates legacy video codec when paired with audio'
);
assert.equal(
mapLegacyAvcCodecs('mp4a.40.2, avc1.66.30'),
'mp4a.40.2, avc1.42001e',
'translates video codec when specified second'
);
});
QUnit.test('translates legacy codecs', function(assert) {
assert.deepEqual(
translateLegacyCodecs(['avc1.66.30', 'avc1.66.30']),
['avc1.42001e', 'avc1.42001e'],
'translates legacy avc1.66.30 codec'
);
assert.deepEqual(
translateLegacyCodecs(['avc1.42C01E', 'avc1.42C01E']),
['avc1.42C01E', 'avc1.42C01E'],
'does not translate modern codecs'
);
assert.deepEqual(
translateLegacyCodecs(['avc1.42C01E', 'avc1.66.30']),
['avc1.42C01E', 'avc1.42001e'],
'only translates legacy codecs when mixed'
);
assert.deepEqual(
translateLegacyCodecs(['avc1.4d0020', 'avc1.100.41', 'avc1.77.41',
'avc1.77.32', 'avc1.77.31', 'avc1.77.30',
'avc1.66.30', 'avc1.66.21', 'avc1.42C01e']),
['avc1.4d0020', 'avc1.640029', 'avc1.4d0029',
'avc1.4d0020', 'avc1.4d001f', 'avc1.4d001e',
'avc1.42001e', 'avc1.420015', 'avc1.42C01e'],
'translates a whole bunch'
);
});
QUnit.module('parseCodecs');
QUnit.test('parses text only codec string', function(assert) {
assert.deepEqual(
parseCodecs('stpp.ttml.im1t'),
[{mediaType: 'text', type: 'stpp.ttml.im1t', details: ''}],
'parsed text only codec string'
);
});
QUnit.test('parses video only codec string', function(assert) {
assert.deepEqual(
parseCodecs('avc1.42001e'),
[{mediaType: 'video', type: 'avc1', details: '.42001e'}],
'parsed video only codec string'
);
});
QUnit.test('parses audio only codec string', function(assert) {
assert.deepEqual(
parseCodecs('mp4a.40.2'),
[{mediaType: 'audio', type: 'mp4a', details: '.40.2'}],
'parsed audio only codec string'
);
});
QUnit.test('parses video, audio, and text codec string', function(assert) {
assert.deepEqual(
parseCodecs('avc1.42001e, mp4a.40.2, stpp.ttml.im1t'),
[
{mediaType: 'video', type: 'avc1', details: '.42001e'},
{mediaType: 'audio', type: 'mp4a', details: '.40.2'},
{mediaType: 'text', type: 'stpp.ttml.im1t', details: ''}
],
'parsed video, audio, and text codec string'
);
});
QUnit.test('parses video, audio, and text codec with mixed case', function(assert) {
assert.deepEqual(
parseCodecs('AvC1.42001E, Mp4A.40.E, stpp.TTML.im1T'),
[
{mediaType: 'video', type: 'AvC1', details: '.42001E'},
{mediaType: 'audio', type: 'Mp4A', details: '.40.E'},
{mediaType: 'text', type: 'stpp.TTML.im1T', details: ''}
],
'parsed video, audio, and text codec string'
);
});
QUnit.test('parses two unknown codec', function(assert) {
assert.deepEqual(
parseCodecs('fake.codec, other-fake'),
[
{mediaType: 'unknown', type: 'fake.codec', details: ''},
{mediaType: 'unknown', type: 'other-fake', details: ''}
],
'parsed faked codecs as video/audio'
);
});
QUnit.test('parses an unknown codec with a known audio', function(assert) {
assert.deepEqual(
parseCodecs('fake.codec, mp4a.40.2'),
[
{mediaType: 'unknown', type: 'fake.codec', details: ''},
{mediaType: 'audio', type: 'mp4a', details: '.40.2'}
],
'parsed audio and unknwon'
);
});
QUnit.test('parses an unknown codec with a known video', function(assert) {
assert.deepEqual(
parseCodecs('avc1.42001e, other-fake'),
[
{mediaType: 'video', type: 'avc1', details: '.42001e'},
{mediaType: 'unknown', type: 'other-fake', details: ''}
],
'parsed video and unknown'
);
});
QUnit.test('parses an unknown codec with a known text', function(assert) {
assert.deepEqual(
parseCodecs('stpp.ttml.im1t, other-fake'),
[
{mediaType: 'text', type: 'stpp.ttml.im1t', details: ''},
{mediaType: 'unknown', type: 'other-fake', details: ''}
],
'parsed text and unknown'
);
});
QUnit.test('parses an unknown codec with a known audio/video/text', function(assert) {
assert.deepEqual(
parseCodecs('fake.codec, avc1.42001e, mp4a.40.2, stpp.ttml.im1t'),
[
{mediaType: 'unknown', type: 'fake.codec', details: ''},
{mediaType: 'video', type: 'avc1', details: '.42001e'},
{mediaType: 'audio', type: 'mp4a', details: '.40.2'},
{mediaType: 'text', type: 'stpp.ttml.im1t', details: ''}
],
'parsed video/audio/text and unknown codecs'
);
});
QUnit.module('codecsFromDefault');
QUnit.test('returns falsey when no audio group ID', function(assert) {
assert.notOk(
codecsFromDefault(
{ mediaGroups: { AUDIO: {} } },
'',
),
'returns falsey when no audio group ID'
);
});
QUnit.test('returns falsey when no matching audio group', function(assert) {
assert.notOk(
codecsFromDefault(
{
mediaGroups: {
AUDIO: {
au1: {
en: {
default: false,
playlists: [{
attributes: { CODECS: 'mp4a.40.2' }
}]
},
es: {
default: true,
playlists: [{
attributes: { CODECS: 'mp4a.40.5' }
}]
}
}
}
}
},
'au2'
),
'returned falsey when no matching audio group'
);
});
QUnit.test('returns falsey when no default for audio group', function(assert) {
assert.notOk(
codecsFromDefault(
{
mediaGroups: {
AUDIO: {
au1: {
en: {
default: false,
playlists: [{
attributes: { CODECS: 'mp4a.40.2' }
}]
},
es: {
default: false,
playlists: [{
attributes: { CODECS: 'mp4a.40.5' }
}]
}
}
}
}
},
'au1'
),
'returned falsey when no default for audio group'
);
});
QUnit.test('returns parsed audio codecs for default in audio group', function(assert) {
assert.deepEqual(
codecsFromDefault(
{
mediaGroups: {
AUDIO: {
au1: {
en: {
default: false,
playlists: [{
attributes: { CODECS: 'mp4a.40.2, mp4a.40.20' }
}]
},
es: {
default: true,
playlists: [{
attributes: { CODECS: 'mp4a.40.5, mp4a.40.7' }
}]
}
}
}
}
},
'au1'
),
[
{mediaType: 'audio', type: 'mp4a', details: '.40.5'},
{mediaType: 'audio', type: 'mp4a', details: '.40.7'}
],
'returned parsed codec audio profile'
);
});
QUnit.module('isVideoCodec');
QUnit.test('works as expected', function(assert) {
[
'av1',
'avc01',
'avc1',
'avc02',
'avc2',
'vp09',
'vp9',
'vp8',
'vp08',
'hvc1',
'hev1',
'theora',
'mp4v'
].forEach(function(codec) {
assert.ok(isVideoCodec(codec), `"${codec}" is seen as a video codec`);
assert.ok(isVideoCodec(` ${codec} `), `" ${codec} " is seen as video codec`);
assert.ok(isVideoCodec(codec.toUpperCase()), `"${codec.toUpperCase()}" is seen as video codec`);
});
['invalid', 'foo', 'mp4a', 'opus', 'vorbis'].forEach(function(codec) {
assert.notOk(isVideoCodec(codec), `${codec} is not a video codec`);
});
});
QUnit.module('isAudioCodec');
QUnit.test('works as expected', function(assert) {
[
'mp4a',
'flac',
'vorbis',
'opus',
'ac-3',
'ac-4',
'ec-3',
'alac',
'speex',
'aac',
'mp3'
].forEach(function(codec) {
assert.ok(isAudioCodec(codec), `"${codec}" is seen as an audio codec`);
assert.ok(isAudioCodec(` ${codec} `), `" ${codec} " is seen as an audio codec`);
assert.ok(isAudioCodec(codec.toUpperCase()), `"${codec.toUpperCase()}" is seen as an audio codec`);
});
['invalid', 'foo', 'bar', 'avc1', 'av1'].forEach(function(codec) {
assert.notOk(isAudioCodec(codec), `${codec} is not an audio codec`);
});
});
QUnit.module('muxerSupportsCodec');
QUnit.test('works as expected', function(assert) {
const validMuxerCodecs = [];
const invalidMuxerCodecs = [];
unsupportedMuxerCodecs.forEach(function(badCodec) {
invalidMuxerCodecs.push(badCodec);
supportedMuxerCodecs.forEach(function(goodCodec) {
invalidMuxerCodecs.push(`${goodCodec}, ${badCodec}`);
});
});
// generate all combinations of valid codecs
supportedMuxerCodecs.forEach(function(codec, i) {
validMuxerCodecs.push(codec);
supportedMuxerCodecs.forEach(function(_codec, z) {
if (z === i) {
return;
}
validMuxerCodecs.push(`${codec}, ${_codec}`);
validMuxerCodecs.push(`${codec},${_codec}`);
});
});
validMuxerCodecs.forEach(function(codec) {
assert.ok(muxerSupportsCodec(codec), `"${codec}" is supported`);
assert.ok(muxerSupportsCodec(` ${codec} `), `" ${codec} " is supported`);
assert.ok(muxerSupportsCodec(codec.toUpperCase()), `"${codec.toUpperCase()}" is supported`);
});
invalidMuxerCodecs.forEach(function(codec) {
assert.notOk(muxerSupportsCodec(codec), `${codec} not supported`);
});
});
QUnit.module('browserSupportsCodec', {
beforeEach() {
this.oldMediaSource = window.MediaSource;
},
afterEach() {
window.MediaSource = this.oldMediaSource;
}
});
QUnit.test('works as expected', function(assert) {
window.MediaSource = {isTypeSupported: () => true};
assert.ok(browserSupportsCodec('test'), 'isTypeSupported true, browser does support codec');
window.MediaSource = {isTypeSupported: () => false};
assert.notOk(browserSupportsCodec('test'), 'isTypeSupported false, browser does not support codec');
window.MediaSource = null;
assert.notOk(browserSupportsCodec('test'), 'no MediaSource, browser does not support codec');
window.MediaSource = {isTypeSupported: null};
assert.notOk(browserSupportsCodec('test'), 'no isTypeSupported, browser does not support codec');
});
QUnit.module('getMimeForCodec');
QUnit.test('works as expected', function(assert) {
// mp4
assert.equal(getMimeForCodec('vp9,mp4a'), 'video/mp4;codecs="vp9,mp4a"', 'mp4 video/audio works');
assert.equal(getMimeForCodec('vp9'), 'video/mp4;codecs="vp9"', 'mp4 video works');
assert.equal(getMimeForCodec('mp4a'), 'audio/mp4;codecs="mp4a"', 'mp4 audio works');
// webm
assert.equal(getMimeForCodec('vp8,opus'), 'video/webm;codecs="vp8,opus"', 'webm video/audio works');
assert.equal(getMimeForCodec('vp8'), 'video/webm;codecs="vp8"', 'webm video works');
assert.equal(getMimeForCodec('vorbis'), 'audio/webm;codecs="vorbis"', 'webm audio works');
// ogg
assert.equal(getMimeForCodec('theora,vorbis'), 'video/ogg;codecs="theora,vorbis"', 'ogg video/audio works');
assert.equal(getMimeForCodec('theora'), 'video/ogg;codecs="theora"', 'ogg video works');
// ogg will never be selected for audio only
// mixed
assert.equal(getMimeForCodec('opus'), 'audio/mp4;codecs="opus"', 'mp4 takes priority over everything');
assert.equal(getMimeForCodec('vorbis'), 'audio/webm;codecs="vorbis"', 'webm takes priority over ogg');
assert.equal(getMimeForCodec('foo'), 'video/mp4;codecs="foo"', 'mp4 is the default');
assert.notOk(getMimeForCodec(), 'invalid codec returns undefined');
assert.equal(getMimeForCodec('Mp4A.40.2,AvC1.42001E'), 'video/mp4;codecs="Mp4A.40.2,AvC1.42001E"', 'case is preserved');
assert.equal(getMimeForCodec('stpp.ttml.im1t'), 'application/mp4;codecs="stpp.ttml.im1t"', 'text is parsed');
});