transmuxer_test.js
23.7 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
(function(window, videojs) {
'use strict';
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
Test methods:
module(name, {[setup][ ,teardown]})
test(name, callback)
expect(numberOfAssertions)
stop(increment)
start(decrement)
Test assertions:
ok(value, [message])
equal(actual, expected, [message])
notEqual(actual, expected, [message])
deepEqual(actual, expected, [message])
notDeepEqual(actual, expected, [message])
strictEqual(actual, expected, [message])
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/
var
PacketStream = videojs.mp2t.PacketStream,
packetStream,
ParseStream = videojs.mp2t.ParseStream,
parseStream,
ProgramStream = videojs.mp2t.ProgramStream,
programStream,
H264Stream = videojs.mp2t.H264Stream,
h264Stream,
Transmuxer = videojs.mp2t.Transmuxer,
transmuxer,
MP2T_PACKET_LENGTH = videojs.mp2t.MP2T_PACKET_LENGTH,
H264_STREAM_TYPE = videojs.mp2t.H264_STREAM_TYPE,
ADTS_STREAM_TYPE = videojs.mp2t.ADTS_STREAM_TYPE,
packetize,
PAT,
PMT,
standalonePes,
validateTrack,
videoPes;
module('MP2T Packet Stream', {
setup: function() {
packetStream = new PacketStream();
}
});
test('empty input does not error', function() {
packetStream.push(new Uint8Array([]));
ok(true, 'did not throw');
});
test('parses a generic packet', function() {
var datas = [];
packetStream.on('data', function(event) {
datas.push(event);
});
packetStream.push(new Uint8Array(188));
equal(1, datas.length, 'fired one event');
equal(datas[0].byteLength, 188, 'delivered the packet');
});
test('buffers partial packets', function() {
var datas = [];
packetStream.on('data', function(event) {
datas.push(event);
});
packetStream.push(new Uint8Array(187));
equal(0, datas.length, 'did not fire an event');
packetStream.push(new Uint8Array(189));
equal(2, datas.length, 'fired events');
equal(188, datas[0].byteLength, 'parsed the first packet');
equal(188, datas[1].byteLength, 'parsed the second packet');
});
test('parses multiple packets delivered at once', function() {
var datas = [];
packetStream.on('data', function(event) {
datas.push(event);
});
packetStream.push(new Uint8Array(188 * 3));
equal(3, datas.length, 'fired three events');
equal(188, datas[0].byteLength, 'parsed the first packet');
equal(188, datas[1].byteLength, 'parsed the second packet');
equal(188, datas[2].byteLength, 'parsed the third packet');
});
test('buffers extra after multiple packets', function() {
var datas = [];
packetStream.on('data', function(event) {
datas.push(event);
});
packetStream.push(new Uint8Array(188 * 2 + 10));
equal(2, datas.length, 'fired two events');
equal(188, datas[0].byteLength, 'parsed the first packet');
equal(188, datas[1].byteLength, 'parsed the second packet');
packetStream.push(new Uint8Array(178));
equal(3, datas.length, 'fired a final event');
equal(188, datas[2].length, 'parsed the finel packet');
});
module('MP2T ParseStream', {
setup: function() {
packetStream = new PacketStream();
parseStream = new ParseStream();
packetStream.pipe(parseStream);
}
});
test('emits an error on an invalid packet', function() {
var errors = [];
parseStream.on('error', function(error) {
errors.push(error);
});
parseStream.push(new Uint8Array(188));
equal(1, errors.length, 'emitted an error');
});
test('parses generic packet properties', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0001 tsc:01 afc:10 cc:11 padding: 00
0x40, 0x01, 0x6c
]));
ok(packet.payloadUnitStartIndicator, 'parsed payload_unit_start_indicator');
ok(packet.pid, 'parsed PID');
});
test('parses piped data events', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0001 tsc:01 afc:10 cc:11 padding: 00
0x40, 0x01, 0x6c
]));
ok(packet, 'parsed a packet');
});
test('parses a data packet with adaptation fields', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0000 tsc:01 afc:10 cc:11 afl:00 0000 00 stuffing:00 0000 00 pscp:00 0001 padding:0000
0x40, 0x00, 0x6c, 0x00, 0x00, 0x10
]));
strictEqual(packet.type, 'pat', 'parsed the packet type');
});
test('parses a PES packet', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
// setup a program map table
parseStream.programMapTable = {
0x0010: videojs.mp2t.H264_STREAM_TYPE
};
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0010 tsc:01 afc:01 cc:11 padding:00
0x40, 0x02, 0x5c
]));
strictEqual(packet.type, 'pes', 'parsed a PES packet');
});
test('parses packets with variable length adaptation fields and a payload', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
// setup a program map table
parseStream.programMapTable = {
0x0010: videojs.mp2t.H264_STREAM_TYPE
};
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0010 tsc:01 afc:11 cc:11 afl:00 0000 11 stuffing:00 0000 0000 00 pscp:00 0001
0x40, 0x02, 0x7c, 0x0c, 0x00, 0x01
]));
strictEqual(packet.type, 'pes', 'parsed a PES packet');
});
/*
Packet Header:
| sb | tei pusi tp pid:5 | pid | tsc afc cc |
with af:
| afl | ... | <data> |
without af:
| <data> |
PAT:
| pf? | ... |
| tid | ssi '0' r sl:4 | sl | tsi:8 |
| tsi | r vn cni | sn | lsn |
with program_number == '0':
| pn | pn | r np:5 | np |
otherwise:
| pn | pn | r pmp:5 | pmp |
*/
PAT = [
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0000 0000
0x40, 0x00,
// tsc:01 afc:01 cc:0000 pointer_field:0000 0000
0x50, 0x00,
// tid:0000 0000 ssi:0 0:0 r:00 sl:0000 0000 0000
0x00, 0x00, 0x00,
// tsi:0000 0000 0000 0000
0x00, 0x00,
// r:00 vn:00 000 cni:1 sn:0000 0000 lsn:0000 0000
0x01, 0x00, 0x00,
// pn:0000 0000 0000 0001
0x00, 0x01,
// r:000 pmp:0 0000 0010 0000
0x00, 0x10,
// crc32:0000 0000 0000 0000 0000 0000 0000 0000
0x00, 0x00, 0x00, 0x00
];
test('parses the program map table pid from the program association table (PAT)', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.push(new Uint8Array(PAT));
ok(packet, 'parsed a packet');
strictEqual(0x0010, parseStream.pmtPid, 'parsed PMT pid');
});
PMT = [
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0010 0000
0x40, 0x10,
// tsc:01 afc:01 cc:0000 pointer_field:0000 0000
0x50, 0x00,
// tid:0000 0010 ssi:0 0:0 r:00 sl:0000 0001 0111
0x02, 0x00, 0x17,
// pn:0000 0000 0000 0001
0x00, 0x01,
// r:00 vn:00 000 cni:1 sn:0000 0000 lsn:0000 0000
0x01, 0x00, 0x00,
// r:000 ppid:0 0011 1111 1111
0x03, 0xff,
// r:0000 pil:0000 0000 0000
0x00, 0x00,
// h264
// st:0001 1010 r:000 epid:0 0000 0001 0001
0x1b, 0x00, 0x11,
// r:0000 esil:0000 0000 0000
0x00, 0x00,
// adts
// st:0000 1111 r:000 epid:0 0000 0001 0010
0x0f, 0x00, 0x12,
// r:0000 esil:0000 0000 0000
0x00, 0x00,
// crc
0x00, 0x00, 0x00, 0x00
];
test('parse the elementary streams from a program map table', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.pmtPid = 0x0010;
parseStream.push(new Uint8Array(PMT.concat(0, 0, 0, 0, 0)));
ok(packet, 'parsed a packet');
ok(parseStream.programMapTable, 'parsed a program map');
strictEqual(0x1b, parseStream.programMapTable[0x11], 'associated h264 with pid 0x11');
strictEqual(0x0f, parseStream.programMapTable[0x12], 'associated adts with pid 0x12');
strictEqual(parseStream.programMapTable[0], undefined, 'ignored trailing stuffing bytes');
deepEqual(parseStream.programMapTable, packet.programMapTable, 'recorded the PMT');
});
test('parses an elementary stream packet with just a pts', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.programMapTable = {
0x11: 0x1b // pid 0x11 is h264 data
};
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0001 0001
0x40, 0x11,
// tsc:01 afc:01 cc:0000
0x50,
// pscp:0000 0000 0000 0000 0000 0001
0x00, 0x00, 0x01,
// sid:0000 0000 ppl:0000 0000 0000 1001
0x00, 0x00, 0x09,
// 10 psc:00 pp:0 dai:1 c:0 ooc:0
0x84,
// pdf:10 ef:1 erf:0 dtmf:0 acif:0 pcf:0 pef:0
0xc0,
// phdl:0000 0101 '0010' pts:000 mb:1 pts:0000 0000
0x05, 0x21, 0x00,
// pts:0000 000 mb:1 pts:0000 0000 pts:0000 000 mb:1
0x01, 0x00, 0x01,
// "data":0101
0x11
]));
ok(packet, 'parsed a packet');
equal('pes', packet.type, 'recognized a PES packet');
equal(0x1b, packet.streamType, 'tracked the stream_type');
equal(1, packet.data.byteLength, 'parsed a single data byte');
equal(0x11, packet.data[0], 'parsed the data');
equal(0, packet.pts, 'parsed the pts');
});
test('parses an elementary stream packet with a pts and dts', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
parseStream.programMapTable = {
0x11: 0x1b // pid 0x11 is h264 data
};
parseStream.push(new Uint8Array([
0x47, // sync byte
// tei:0 pusi:1 tp:0 pid:0 0000 0001 0001
0x40, 0x11,
// tsc:01 afc:01 cc:0000
0x50,
// pscp:0000 0000 0000 0000 0000 0001
0x00, 0x00, 0x01,
// sid:0000 0000 ppl:0000 0000 0000 1110
0x00, 0x00, 0x0e,
// 10 psc:00 pp:0 dai:1 c:0 ooc:0
0x84,
// pdf:11 ef:1 erf:0 dtmf:0 acif:0 pcf:0 pef:0
0xe0,
// phdl:0000 1010 '0011' pts:000 mb:1 pts:0000 0000
0x0a, 0x21, 0x00,
// pts:0000 000 mb:1 pts:0000 0000 pts:0000 100 mb:1
0x01, 0x00, 0x09,
// '0001' dts:000 mb:1 dts:0000 0000 dts:0000 000 mb:1
0x11, 0x00, 0x01,
// dts:0000 0000 dts:0000 010 mb:1
0x00, 0x05,
// "data":0101
0x11
]));
ok(packet, 'parsed a packet');
equal('pes', packet.type, 'recognized a PES packet');
equal(0x1b, packet.streamType, 'tracked the stream_type');
equal(1, packet.data.byteLength, 'parsed a single data byte');
equal(0x11, packet.data[0], 'parsed the data');
equal(4 / 90, packet.pts, 'parsed the pts');
equal(2 / 90, packet.dts, 'parsed the dts');
});
// helper function to create video PES packets
videoPes = function(data, first) {
var
adaptationFieldLength = 188 - data.length - (first ? 18 : 17),
result = [
// sync byte
0x47,
// tei:0 pusi:1 tp:0 pid:0 0000 0001 0001
0x40, 0x11,
// tsc:01 afc:11 cc:0000
0x70
].concat([
// afl
adaptationFieldLength & 0xff,
// di:0 rai:0 espi:0 pf:0 of:0 spf:0 tpdf:0 afef:0
0x00
]),
i;
i = adaptationFieldLength - 1;
while (i--) {
// stuffing_bytes
result.push(0xff);
}
result = result.concat([
// pscp:0000 0000 0000 0000 0000 0001
0x00, 0x00, 0x01,
// sid:0000 0000 ppl:0000 0000 0000 0101
0x00, 0x00, 0x05,
// 10 psc:00 pp:0 dai:1 c:0 ooc:0
0x84,
// pdf:00 ef:1 erf:0 dtmf:0 acif:0 pcf:0 pef:0
0x20,
// phdl:0000 0000
0x00
]);
if (first) {
result.push(0x00);
}
result = result.concat([
// NAL unit start code
0x00, 0x00, 0x01
].concat(data));
return result;
};
standalonePes = videoPes([0xaf, 0x01], true);
test('parses an elementary stream packet without a pts or dts', function() {
var packet;
parseStream.on('data', function(data) {
packet = data;
});
// pid 0x11 is h264 data
parseStream.programMapTable = {
0x11: H264_STREAM_TYPE
};
parseStream.push(new Uint8Array(standalonePes));
ok(packet, 'parsed a packet');
equal('pes', packet.type, 'recognized a PES packet');
equal(0x1b, packet.streamType, 'tracked the stream_type');
equal(2 + 4, packet.data.byteLength, 'parsed two data bytes');
equal(0xaf, packet.data[packet.data.length - 2], 'parsed the first data byte');
equal(0x01, packet.data[packet.data.length - 1], 'parsed the second data byte');
ok(!packet.pts, 'did not parse a pts');
ok(!packet.dts, 'did not parse a dts');
});
module('MP2T ProgramStream', {
setup: function() {
programStream = new ProgramStream();
}
});
packetize = function(data) {
var packet = new Uint8Array(MP2T_PACKET_LENGTH);
packet.set(data);
return packet;
};
test('parses metadata events from PSI packets', function() {
var
metadatas = [],
datas = 0,
sortById = function(left, right) {
return left.id - right.id;
};
programStream.on('data', function(data) {
if (data.type === 'metadata') {
metadatas.push(data);
}
datas++;
});
programStream.push({
type: 'pat'
});
programStream.push({
type: 'pmt',
programMapTable: {
1: 0x1b,
2: 0x0f
}
});
equal(1, datas, 'data fired');
equal(1, metadatas.length, 'metadata generated');
metadatas[0].tracks.sort(sortById);
deepEqual(metadatas[0].tracks, [{
id: 1,
codec: 'avc',
type: 'video'
}, {
id: 2,
codec: 'adts',
type: 'audio'
}], 'identified two tracks');
});
test('parses standalone program stream packets', function() {
var packets = [];
programStream.on('data', function(packet) {
packets.push(packet);
});
programStream.push({
type: 'pes',
data: new Uint8Array(19)
});
programStream.end();
equal(1, packets.length, 'built one packet');
equal('audio', packets[0].type, 'identified audio data');
equal(19, packets[0].data.byteLength, 'parsed the correct payload size');
});
test('aggregates program stream packets from the transport stream', function() {
var events = [];
programStream.on('data', function(event) {
events.push(event);
});
programStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array(7)
});
equal(0, events.length, 'buffers partial packets');
programStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array(13)
});
programStream.end();
equal(1, events.length, 'built one packet');
equal('video', events[0].type, 'identified video data');
equal(20, events[0].data.byteLength, 'concatenated transport packets');
});
test('buffers audio and video program streams individually', function() {
var events = [];
programStream.on('data', function(event) {
events.push(event);
});
programStream.push({
type: 'pes',
payloadUnitStartIndicator: true,
streamType: H264_STREAM_TYPE,
data: new Uint8Array(1)
});
programStream.push({
type: 'pes',
payloadUnitStartIndicator: true,
streamType: ADTS_STREAM_TYPE,
data: new Uint8Array(1)
});
equal(0, events.length, 'buffers partial packets');
programStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array(1)
});
programStream.push({
type: 'pes',
streamType: ADTS_STREAM_TYPE,
data: new Uint8Array(1)
});
programStream.end();
equal(2, events.length, 'parsed a complete packet');
equal('video', events[0].type, 'identified video data');
equal('audio', events[1].type, 'identified audio data');
});
test('flushes the buffered packets when a new one of that type is started', function() {
var packets = [];
programStream.on('data', function(packet) {
packets.push(packet);
});
programStream.push({
type: 'pes',
payloadUnitStartIndicator: true,
streamType: H264_STREAM_TYPE,
data: new Uint8Array(1)
});
programStream.push({
type: 'pes',
payloadUnitStartIndicator: true,
streamType: ADTS_STREAM_TYPE,
data: new Uint8Array(7)
});
programStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array(1)
});
equal(0, packets.length, 'buffers packets by type');
programStream.push({
type: 'pes',
payloadUnitStartIndicator: true,
streamType: H264_STREAM_TYPE,
data: new Uint8Array(1)
});
equal(1, packets.length, 'built one packet');
equal('video', packets[0].type, 'identified video data');
equal(2, packets[0].data.byteLength, 'concatenated packets');
programStream.end();
equal(3, packets.length, 'built tow more packets');
equal('video', packets[1].type, 'identified video data');
equal(1, packets[1].data.byteLength, 'parsed the video payload');
equal('audio', packets[2].type, 'identified audio data');
equal(7, packets[2].data.byteLength, 'parsed the audio payload');
});
module('H264 Stream', {
setup: function() {
h264Stream = new H264Stream();
}
});
test('unpacks nal units from simple byte stream framing', function() {
var data;
h264Stream.on('data', function(event) {
data = event;
});
// the simplest byte stream framing:
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x00, 0x00, 0x01,
0x09, 0x07,
0x00, 0x00, 0x01
])
});
ok(data, 'generated a data event');
equal(data.nalUnitType, 'access_unit_delimiter_rbsp', 'identified an access unit delimiter');
equal(data.data.length, 2, 'calculated nal unit length');
equal(data.data[1], 7, 'read a payload byte');
});
test('unpacks nal units from byte streams split across pushes', function() {
var data;
h264Stream.on('data', function(event) {
data = event;
});
// handles byte streams split across pushes
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x00, 0x00, 0x01,
0x09])
});
ok(!data, 'buffers NAL units across events');
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x07,
0x00, 0x00, 0x01
])
});
ok(data, 'generated a data event');
equal(data.nalUnitType, 'access_unit_delimiter_rbsp', 'identified an access unit delimiter');
equal(data.data.length, 2, 'calculated nal unit length');
equal(data.data[1], 7, 'read a payload byte');
});
test('unpacks nal units from byte streams with split sync points', function() {
var data;
h264Stream.on('data', function(event) {
data = event;
});
// handles sync points split across pushes
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x00, 0x00, 0x01,
0x09, 0x07,
0x00])
});
ok(!data, 'buffers NAL units across events');
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x01
])
});
ok(data, 'generated a data event');
equal(data.nalUnitType, 'access_unit_delimiter_rbsp', 'identified an access unit delimiter');
equal(data.data.length, 2, 'calculated nal unit length');
equal(data.data[1], 7, 'read a payload byte');
});
test('parses nal unit types', function() {
var data;
h264Stream.on('data', function(event) {
data = event;
});
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x00, 0x00, 0x01,
0x09
])
});
h264Stream.end();
ok(data, 'generated a data event');
equal(data.nalUnitType, 'access_unit_delimiter_rbsp', 'identified an access unit delimiter');
data = null;
h264Stream.push({
type: 'video',
data: new Uint8Array([
0x00, 0x00, 0x00, 0x01,
0x07,
0x27, 0x42, 0xe0, 0x0b,
0xa9, 0x18, 0x60, 0x9d,
0x80, 0x35, 0x06, 0x01,
0x06, 0xb6, 0xc2, 0xb5,
0xef, 0x7c, 0x04
])
});
h264Stream.end();
ok(data, 'generated a data event');
equal(data.nalUnitType, 'seq_parameter_set_rbsp', 'identified a sequence parameter set');
});
module('Transmuxer', {
setup: function() {
transmuxer = new Transmuxer();
}
});
test('generates an init segment', function() {
var segments = [];
transmuxer.on('data', function(segment) {
segments.push(segment);
});
transmuxer.push(packetize(PAT));
transmuxer.push(packetize(PMT));
transmuxer.push(packetize(videoPes([
0x07,
0x27, 0x42, 0xe0, 0x0b,
0xa9, 0x18, 0x60, 0x9d,
0x80, 0x53, 0x06, 0x01,
0x06, 0xb6, 0xc2, 0xb5,
0xef, 0x7c, 0x04
], true)));
transmuxer.end();
equal(segments.length, 2, 'has an init segment');
});
test('buffers video samples until ended', function() {
var samples = [], boxes;
transmuxer.on('data', function(data) {
samples.push(data);
});
transmuxer.push(packetize(PAT));
transmuxer.push(packetize(PMT));
// buffer a NAL
transmuxer.push(packetize(videoPes([0x09, 0x01], true)));
transmuxer.push(packetize(videoPes([0x00, 0x02])));
// add an access_unit_delimiter_rbsp
transmuxer.push(packetize(videoPes([0x09, 0x03])));
transmuxer.push(packetize(videoPes([0x00, 0x04])));
transmuxer.push(packetize(videoPes([0x00, 0x05])));
// flush everything
transmuxer.end();
equal(samples.length, 1, 'emitted one event');
boxes = videojs.inspectMp4(samples[0].data);
equal(boxes.length, 2, 'generated two boxes');
equal(boxes[0].type, 'moof', 'the first box is a moof');
equal(boxes[1].type, 'mdat', 'the second box is a mdat');
deepEqual(new Uint8Array(samples[0].data.subarray(samples[0].data.length - 10)),
new Uint8Array([
0x09, 0x01,
0x00, 0x02,
0x09, 0x03,
0x00, 0x04,
0x00, 0x05]),
'concatenated NALs into an mdat');
});
validateTrack = function(track, metadata) {
var mdia, handlerType;
equal(track.type, 'trak', 'wrote the track type');
equal(track.boxes.length, 2, 'wrote track children');
equal(track.boxes[0].type, 'tkhd', 'wrote the track header');
if (metadata) {
if (metadata.trackId) {
equal(track.boxes[0].trackId, metadata.trackId, 'wrote the track id');
}
if (metadata.width) {
equal(track.boxes[0].width, metadata.width, 'wrote the width');
}
if (metadata.height) {
equal(track.boxes[0].height, metadata.height, 'wrote the height');
}
}
mdia = track.boxes[1];
equal(mdia.type, 'mdia', 'wrote the media');
equal(mdia.boxes.length, 3, 'wrote the mdia children');
equal(mdia.boxes[0].type, 'mdhd', 'wrote the media header');
equal(mdia.boxes[0].language, 'und', 'the language is undefined');
equal(mdia.boxes[0].duration, 0xffffffff, 'the duration is at maximum');
equal(mdia.boxes[1].type, 'hdlr', 'wrote the media handler');
handlerType = mdia.boxes[1].handlerType;
equal(mdia.boxes[2].type, 'minf', 'wrote the media info');
};
test('parses an example mp2t file and generates media segments', function() {
var
segments = [],
sequenceNumber = window.Infinity,
i, boxes, mfhd, traf;
transmuxer.on('data', function(segment) {
segments.push(segment);
});
transmuxer.push(window.bcSegment);
transmuxer.end();
equal(segments.length, 2, 'generated two segments');
boxes = videojs.inspectMp4(segments[0].data);
equal(boxes.length, 2, 'init segments are composed of two boxes');
equal(boxes[0].type, 'ftyp', 'the first box is an ftyp');
equal(boxes[1].type, 'moov', 'the second box is a moov');
equal(boxes[1].boxes[0].type, 'mvhd', 'generated an mvhd');
validateTrack(boxes[1].boxes[1], {
trackId: 256,
width: 388,
height: 300
});
validateTrack(boxes[1].boxes[2], {
trackId: 257
});
equal(boxes[1].boxes[3].type, 'mvex', 'generated an mvex');
boxes = videojs.inspectMp4(segments[1].data);
ok(boxes.length > 0, 'media segments are not empty');
ok(boxes.length % 2 === 0, 'media segments are composed of pairs of boxes');
for (i = 0; i < boxes.length; i += 2) {
equal(boxes[i].type, 'moof', 'first box is a moof');
equal(boxes[i].boxes.length, 2, 'the moof has two children');
mfhd = boxes[i].boxes[0];
equal(mfhd.type, 'mfhd', 'mfhd is a child of the moof');
ok(mfhd.sequenceNumber < sequenceNumber, 'sequence numbers are increasing');
sequenceNumber = mfhd.sequenceNumber;
traf = boxes[i].boxes[1];
equal(traf.type, 'traf', 'traf is a child of the moof');
equal(boxes[i + 1].type, 'mdat', 'second box is an mdat');
}
});
})(window, window.videojs);