-
Notifications
You must be signed in to change notification settings - Fork 211
/
caption-stream.test.js
3380 lines (3035 loc) · 110 KB
/
caption-stream.test.js
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
var segments = require('data-files!segments');
var
window = require('global/window'),
captionStream,
m2ts = require('../lib/m2ts'),
mp4 = require('../lib/mp4'),
QUnit = require('qunit'),
seiNalUnitGenerator = require('./utils/sei-nal-unit-generator'),
makeSeiFromCaptionPacket = seiNalUnitGenerator.makeSeiFromCaptionPacket,
makeSeiFromMultipleCaptionPackets = seiNalUnitGenerator.makeSeiFromMultipleCaptionPackets,
characters = seiNalUnitGenerator.characters,
packetHeader708 = seiNalUnitGenerator.packetHeader708,
displayWindows708 = seiNalUnitGenerator.displayWindows708,
cc708PinkUnderscore = require('./utils/cc708-pink-underscore'),
cc708Korean = require('./utils/cc708-korean'),
sintelCaptions = segments['sintel-captions.ts'](),
mixed608708Captions = require('./utils/mixed-608-708-captions.js'),
multiChannel608Captions = segments['multi-channel-608-captions.ts']();
QUnit.module('Caption Stream', {
beforeEach: function() {
captionStream = new m2ts.CaptionStream();
}
});
QUnit.test('parses SEIs messages larger than 255 bytes', function(assert) {
var packets = [], data;
captionStream.ccStreams_[0].push = function(packet) {
packets.push(packet);
};
// set data channel 1 active for field 1
captionStream.activeCea608Channel_[0] = 0;
data = new Uint8Array(268);
data[0] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[1] = 0xff; // payload_size
data[2] = 0x0d; // payload_size
data[3] = 181; // itu_t_t35_country_code
data[4] = 0x00;
data[5] = 0x31; // itu_t_t35_provider_code
data[6] = 0x47;
data[7] = 0x41;
data[8] = 0x39;
data[9] = 0x34; // user_identifier, "GA94"
data[10] = 0x03; // user_data_type_code, 0x03 is cc_data
data[11] = 0xc1; // process_cc_data, cc_count
data[12] = 0xff; // reserved
data[13] = 0xfc; // cc_valid, cc_type (608, field 1)
data[14] = 0xff; // cc_data_1 with parity bit set
data[15] = 0x0e; // cc_data_2 without parity bit set
data[16] = 0xff; // marker_bits
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: data
});
captionStream.flush();
assert.equal(packets.length, 1, 'parsed a caption');
});
QUnit.test('parses SEIs containing multiple messages', function(assert) {
var packets = [], data;
captionStream.ccStreams_[0].push = function(packet) {
packets.push(packet);
};
// set data channel 1 active for field 1
captionStream.activeCea608Channel_[0] = 0;
data = new Uint8Array(22);
data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
data[1] = 0x04; // payload_size
data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[7] = 0x0d; // payload_size
data[8] = 181; // itu_t_t35_country_code
data[9] = 0x00;
data[10] = 0x31; // itu_t_t35_provider_code
data[11] = 0x47;
data[12] = 0x41;
data[13] = 0x39;
data[14] = 0x34; // user_identifier, "GA94"
data[15] = 0x03; // user_data_type_code, 0x03 is cc_data
data[16] = 0xc1; // process_cc_data, cc_count
data[17] = 0xff; // reserved
data[18] = 0xfc; // cc_valid, cc_type (608, field 1)
data[19] = 0xff; // cc_data_1 with parity bit set
data[20] = 0x0e; // cc_data_2 without parity bit set
data[21] = 0xff; // marker_bits
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: data
});
captionStream.flush();
assert.equal(packets.length, 1, 'parsed a caption');
});
QUnit.test('parses SEIs containing multiple messages of type user_data_registered_itu_t_t35', function(assert) {
var packets = [], data;
captionStream.ccStreams_[0].push = function(packet) {
packets.push(packet);
};
// set data channel 1 active for field 1
captionStream.activeCea608Channel_[0] = 0;
data = new Uint8Array(33);
data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
data[1] = 0x04; // payload_size
// https://www.etsi.org/deliver/etsi_ts/101100_101199/101154/01.11.01_60/ts_101154v011101p.pdf#page=117
data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[7] = 0x09; // payload_size
data[8] = 181; // itu_t_t35_country_code
data[9] = 0x00;
data[10] = 0x31; // itu_t_t35_provider_code
data[11] = 0x44;
data[12] = 0x54;
data[13] = 0x47;
data[14] = 0x31; // user_identifier, "DTG1"
data[15] = 0x11; // zero_bit (b7), active_format_flag (b6), reserved (b5-b0)
data[16] = 0xF0; // reserved (b7-b4), active_format (b3-b0)
data[17] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[18] = 0x0d; // payload_size
data[19] = 181; // itu_t_t35_country_code
data[20] = 0x00;
data[21] = 0x31; // itu_t_t35_provider_code
data[22] = 0x47;
data[23] = 0x41;
data[24] = 0x39;
data[25] = 0x34; // user_identifier, "GA94"
data[26] = 0x03; // user_data_type_code, 0x03 is cc_data
data[27] = 0xc1; // process_cc_data, cc_count
data[28] = 0xff; // reserved
data[29] = 0xfc; // cc_valid, cc_type (608, field 1)
data[30] = 0xff; // cc_data_1 with parity bit set
data[31] = 0x0e; // cc_data_2 without parity bit set
data[32] = 0xff; // marker_bits
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: data
});
captionStream.flush();
assert.equal(packets.length, 1, 'ignored DTG1 payload, parsed a GA94 caption');
});
QUnit.test('does not throw error if only invalid payloads', function(assert) {
var packets = [], data;
captionStream.ccStreams_[0].push = function(packet) {
packets.push(packet);
};
// set data channel 1 active for field 1
captionStream.activeCea608Channel_[0] = 0;
data = new Uint8Array(33);
data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
data[1] = 0x04; // payload_size
// https://www.etsi.org/deliver/etsi_ts/101100_101199/101154/01.11.01_60/ts_101154v011101p.pdf#page=117
data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[7] = 0x09; // payload_size
data[8] = 181; // itu_t_t35_country_code
data[9] = 0x00;
data[10] = 0x31; // itu_t_t35_provider_code
data[11] = 0x44;
data[12] = 0x54;
data[13] = 0x47;
data[14] = 0x31; // user_identifier, "DTG1"
data[15] = 0x11; // zero_bit (b7), active_format_flag (b6), reserved (b5-b0)
data[16] = 0xF0; // reserved (b7-b4), active_format (b3-b0)
data[17] = 0x04; // payload_type === user_data_registered_itu_t_t35
data[18] = 0x0d; // payload_size
data[19] = 181; // itu_t_t35_country_code
data[20] = 0x00;
data[21] = 0x31; // itu_t_t35_provider_code
data[22] = 0x47;
data[23] = 0x41;
data[24] = 0x39;
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: data
});
captionStream.flush();
assert.equal(packets.length, 0, 'ignored DTG1 payload');
});
QUnit.test('ignores SEIs that do not have type user_data_registered_itu_t_t35', function(assert) {
var captions = [];
captionStream.on('data', function(caption) {
captions.push(caption);
});
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: new Uint8Array([
0x05 // payload_type !== user_data_registered_itu_t_t35
])
});
assert.equal(captions.length, 0, 'ignored the unknown payload type');
});
QUnit.test('parses a minimal example of caption data', function(assert) {
var packets = [];
captionStream.ccStreams_[0].push = function(packet) {
packets.push(packet);
};
// set data channel 1 active for field 1
captionStream.activeCea608Channel_[0] = 0;
captionStream.push({
nalUnitType: 'sei_rbsp',
escapedRBSP: new Uint8Array([
0x04, // payload_type === user_data_registered_itu_t_t35
0x0d, // payload_size
181, // itu_t_t35_country_code
0x00, 0x31, // itu_t_t35_provider_code
0x47, 0x41, 0x39, 0x34, // user_identifier, "GA94"
0x03, // user_data_type_code, 0x03 is cc_data
// 110 00001
0xc1, // process_cc_data, cc_count
0xff, // reserved
// 1111 1100
0xfc, // cc_valid, cc_type (608, field 1)
0xff, // cc_data_1 with parity bit set
0x0e, // cc_data_2 without parity bit set
0xff // marker_bits
])
});
captionStream.flush();
assert.equal(packets.length, 1, 'parsed a caption packet');
});
QUnit.test('can be parsed from a segment', function(assert) {
var transmuxer = new mp4.Transmuxer(),
captions = [];
// Setting the BMDT to ensure that captions and id3 tags are not
// time-shifted by this value when they are output and instead are
// zero-based
transmuxer.setBaseMediaDecodeTime(100000);
transmuxer.on('data', function(data) {
if (data.captions) {
captions = captions.concat(data.captions);
}
});
transmuxer.push(sintelCaptions);
transmuxer.flush();
assert.equal(captions.length, 2, 'parsed two captions');
assert.equal(captions[0].content[0].text.indexOf('ASUKA'), 0, 'parsed the start of the first caption');
assert.ok(captions[0].content[0].text.indexOf('Japanese') > 0, 'parsed the end of the first caption');
assert.equal(captions[0].startTime, 1, 'parsed the start time');
assert.equal(captions[0].endTime, 4, 'parsed the end time');
});
QUnit.test('dispatches caption track information', function(assert) {
var transmuxer = new mp4.Transmuxer(),
captions = [],
captionStreams = {};
// Setting the BMDT to ensure that captions and id3 tags are not
// time-shifted by this value when they are output and instead are
// zero-based
transmuxer.setBaseMediaDecodeTime(100000);
transmuxer.on('data', function(data) {
if (data.captions) {
captions = captions.concat(data.captions);
for (var trackId in data.captionStreams) {
captionStreams[trackId] = true;
}
}
});
transmuxer.push(multiChannel608Captions);
transmuxer.flush();
assert.deepEqual(captionStreams, {CC1: true, CC3: true}, 'found captions in CC1 and CC3');
assert.equal(captions.length, 4, 'parsed eight captions');
assert.equal(captions[0].content[0].text, 'être une période de questions', 'parsed the text of the first caption in CC3');
assert.equal(captions[1].content[0].text, 'PERIOD, FOLKS.', 'parsed the text of the first caption in CC1');
});
QUnit.test('sorting is fun', function(assert) {
var packets, captions, seiNals;
packets = [
// Send another command so that the second EOC isn't ignored
{ pts: 10 * 1000, ccData: 0x1420, type: 0 },
// RCL, resume caption loading
{ pts: 1000, ccData: 0x1420, type: 0 },
// 'test string #1'
{ pts: 1000, ccData: characters('te'), type: 0 },
{ pts: 1000, ccData: characters('st'), type: 0 },
{ pts: 1000, ccData: characters(' s'), type: 0 },
// 'test string #2'
{ pts: 10 * 1000, ccData: characters('te'), type: 0 },
{ pts: 10 * 1000, ccData: characters('st'), type: 0 },
{ pts: 10 * 1000, ccData: characters(' s'), type: 0 },
// 'test string #1' continued
{ pts: 1000, ccData: characters('tr'), type: 0 },
{ pts: 1000, ccData: characters('in'), type: 0 },
{ pts: 1000, ccData: characters('g '), type: 0 },
{ pts: 1000, ccData: characters('#1'), type: 0 },
// 'test string #2' continued
{ pts: 10 * 1000, ccData: characters('tr'), type: 0 },
{ pts: 10 * 1000, ccData: characters('in'), type: 0 },
{ pts: 10 * 1000, ccData: characters('g '), type: 0 },
{ pts: 10 * 1000, ccData: characters('#2'), type: 0 },
// EOC, End of Caption. End display
{ pts: 10 * 1000, ccData: 0x142f, type: 0 },
// EOC, End of Caption. Finished transmitting, begin display
{ pts: 1000, ccData: 0x142f, type: 0 },
// Send another command so that the second EOC isn't ignored
{ pts: 20 * 1000, ccData: 0x1420, type: 0 },
// EOC, End of Caption. End display
{ pts: 20 * 1000, ccData: 0x142f, type: 0 }
];
captions = [];
seiNals = packets.map(makeSeiFromCaptionPacket);
captionStream.on('data', function(caption) {
captions.push(caption);
});
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 2, 'detected two captions');
assert.equal(captions[0].content[0].text, 'test string #1', 'parsed caption 1');
assert.equal(captions[1].content[0].text, 'test string #2', 'parsed caption 2');
});
QUnit.test('drops duplicate segments', function(assert) {
var packets, captions, seiNals;
packets = [
{
pts: 1000, dts: 1000, captions: [
{ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
{ccData: 0x1420, type: 0 }, // RCL, duplicate as per spec
{ccData: characters('te'), type: 0 },
{ccData: characters('st'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters(' s'), type: 0 },
{ccData: characters('tr'), type: 0 },
{ccData: characters('in'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('g '), type: 0 },
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters(' s'), type: 0 },
{ccData: characters('tr'), type: 0 },
{ccData: characters('in'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('g '), type: 0 },
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
},
{
pts: 4000, dts: 4000, captions: [
{ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
{ccData: 0x142f, type: 0 }, // EOC, duplicate as per spec
{ccData: 0x142f, type: 0 }, // EOC, mark display end and flush
{ccData: 0x142f, type: 0 } // EOC, duplicate as per spec
]
}
];
captions = [];
seiNals = packets.map(makeSeiFromMultipleCaptionPackets);
captionStream.on('data', function(caption) {
captions.push(caption);
});
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 1, 'detected one caption');
assert.equal(captions[0].content[0].text, 'test string data', 'parsed caption properly');
});
QUnit.test('drops duplicate segments with multi-segment DTS values', function(assert) {
var packets, captions, seiNals;
packets = [
{
pts: 1000, dts: 1000, captions: [
{ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
{ccData: 0x1420, type: 0 }, // RCL, duplicate as per spec
{ccData: characters('te'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters('st'), type: 0 },
{ccData: characters(' s'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters('tr'), type: 0 },
{ccData: characters('in'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('g '), type: 0 },
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters(' s'), type: 0 },
{ccData: characters('tr'), type: 0 },
{ccData: characters('in'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('g '), type: 0 },
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters(' s'), type: 0 },
{ccData: characters('tu'), type: 0 },
{ccData: characters('ff'), type: 0 }
]
},
{
pts: 4000, dts: 4000, captions: [
{ccData: 0x142f, type: 0 }, // EOC (end of caption)
// EOC not duplicated for robustness testing
{ccData: 0x1420, type: 0 } // RCL (resume caption loading)
]
},
{
pts: 5000, dts: 5000, captions: [
{ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
{ccData: characters(' a'), type: 0 },
{ccData: characters('nd'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters(' e'), type: 0 },
{ccData: characters('ve'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('n '), type: 0 },
{ccData: characters('mo'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('re'), type: 0 },
{ccData: characters(' t'), type: 0 }
]
},
{
pts: 5000, dts: 5000, captions: [
{ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
{ccData: characters(' a'), type: 0 },
{ccData: characters('nd'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters(' e'), type: 0 },
{ccData: characters('ve'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('n '), type: 0 },
{ccData: characters('mo'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('re'), type: 0 },
{ccData: characters(' t'), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('ex'), type: 0 },
{ccData: characters('t '), type: 0 }
]
},
{
pts: 6000, dts: 6000, captions: [
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
},
{
pts: 7000, dts: 7000, captions: [
{ccData: characters(' h'), type: 0 },
{ccData: characters('er'), type: 0 }
]
},
{
pts: 8000, dts: 8000, captions: [
{ccData: characters('e!'), type: 0 },
{ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
{ccData: 0x142f, type: 0 }, // EOC, duplicated as per spec
{ccData: 0x142f, type: 0 } // EOC, mark display end and flush
// EOC not duplicated for robustness testing
]
}
];
captions = [];
seiNals = packets.map(makeSeiFromMultipleCaptionPackets);
captionStream.on('data', function(caption) {
captions.push(caption);
});
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 2, 'detected two captions');
assert.equal(captions[0].content[0].text, 'test string data stuff', 'parsed caption properly');
assert.equal(captions[1].content[0].text, 'and even more text data here!', 'parsed caption properly');
});
QUnit.test('doesn\'t ignore older segments if reset', function(assert) {
var firstPackets, secondPackets, captions, seiNals1, seiNals2;
firstPackets = [
{
pts: 11000, dts: 11000, captions: [
{ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
{ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
{ccData: characters('te'), type: 0 }
]
},
{
pts: 12000, dts: 12000, captions: [
{ccData: characters('st'), type: 0 },
{ccData: characters(' s'), type: 0 }
]
},
{
pts: 12000, dts: 12000, captions: [
{ccData: characters('tr'), type: 0 },
{ccData: characters('in'), type: 0 }
]
},
{
pts: 13000, dts: 13000, captions: [
{ccData: characters('g '), type: 0 },
{ccData: characters('da'), type: 0 },
{ccData: characters('ta'), type: 0 }
]
}
];
secondPackets = [
{
pts: 1000, dts: 1000, captions: [
{ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
{ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
{ccData: characters('af'), type: 0 }
]
},
{
pts: 2000, dts: 2000, captions: [
{ccData: characters('te'), type: 0 },
{ccData: characters('r '), type: 0 },
{ccData: characters('re'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('se'), type: 0 },
{ccData: characters('t '), type: 0 },
{ccData: characters('da'), type: 0 }
]
},
{
pts: 3000, dts: 3000, captions: [
{ccData: characters('ta'), type: 0 },
{ccData: characters('!!'), type: 0 }
]
},
{
pts: 4000, dts: 4000, captions: [
{ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
{ccData: 0x142f, type: 0 }, // EOC, duplicated as per spec
{ccData: 0x142f, type: 0 } // EOC, mark display end and flush
// EOC not duplicated for robustness testing
]
}
];
captions = [];
seiNals1 = firstPackets.map(makeSeiFromMultipleCaptionPackets);
seiNals2 = secondPackets.map(makeSeiFromMultipleCaptionPackets);
captionStream.on('data', function(caption) {
captions.push(caption);
});
seiNals1.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captionStream.latestDts_, 13000, 'DTS is tracked correctly');
captionStream.reset();
assert.equal(captionStream.latestDts_, null, 'DTS tracking was reset');
seiNals2.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captionStream.latestDts_, 4000, 'DTS is tracked correctly');
assert.equal(captions.length, 1, 'detected one caption');
assert.equal(captions[0].content[0].text, 'after reset data!!', 'parsed caption properly');
});
QUnit.test('extracts all theoretical caption channels', function(assert) {
var captions = [];
captionStream.ccStreams_.forEach(function(cc) {
cc.on('data', function(caption) {
captions.push(caption);
});
});
// RU2 = roll-up, 2 rows
// CR = carriage return
var packets = [
{ pts: 1000, type: 0, ccData: 0x1425 }, // RU2 (sets CC1)
{ pts: 2000, type: 0, ccData: characters('1a') }, // CC1
{ pts: 3000, type: 0, ccData: 0x1c25 }, // RU2 (sets CC2)
{ pts: 4000, type: 1, ccData: 0x1525 }, // RU2 (sets CC3)
{ pts: 5000, type: 1, ccData: characters('3a') }, // CC3
// this next one tests if active channel is tracked per-field
// instead of globally
{ pts: 6000, type: 0, ccData: characters('2a') }, // CC2
{ pts: 7000, type: 1, ccData: 0x1d25 }, // RU2 (sets CC4)
{ pts: 8000, type: 1, ccData: characters('4a') }, // CC4
{ pts: 9000, type: 1, ccData: characters('4b') }, // CC4
{ pts: 10000, type: 0, ccData: 0x142d }, // CR (sets + flushes CC1)
{ pts: 11000, type: 0, ccData: 0x1c2d }, // CR (sets + flushes CC2)
{ pts: 12000, type: 0, ccData: 0x1425 }, // RU2 (sets CC1)
{ pts: 13000, type: 0, ccData: characters('1b') }, // CC1
{ pts: 14000, type: 0, ccData: characters('1c') }, // CC1
{ pts: 15000, type: 0, ccData: 0x142d }, // CR (sets + flushes CC1)
{ pts: 16000, type: 1, ccData: 0x152d }, // CR (sets + flushes CC3)
{ pts: 17000, type: 1, ccData: 0x1d2d }, // CR (sets + flushes CC4)
{ pts: 18000, type: 0, ccData: 0x1c25 }, // RU2 (sets CC2)
{ pts: 19000, type: 0, ccData: characters('2b') }, // CC2
{ pts: 20000, type: 0, ccData: 0x1c2d } // CR (sets + flushes CC2)
];
var seiNals = packets.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 6, 'got all captions');
assert.equal(captions[0].content[0].text, '1a', 'cc1 first row');
assert.equal(captions[1].content[0].text, '2a', 'cc2 first row');
assert.equal(captions[2].content[0].text, '1a', 'cc1 first row');
assert.equal(captions[2].content[1].text, '1b1c', 'cc1 second row');
assert.equal(captions[3].content[0].text, '3a', 'cc3 first row');
assert.equal(captions[4].content[0].text, '4a4b', 'cc4 first row');
assert.equal(captions[5].content[0].text, '2a', 'cc2 first row');
assert.equal(captions[5].content[1].text, '2b', 'cc2 second row');
});
QUnit.test('drops data until first command that sets activeChannel for a field', function(assert) {
var captions = [];
captionStream.ccStreams_.forEach(function(cc) {
cc.on('data', function(caption) {
captions.push(caption);
});
});
var packets = [
// test that packets in same field and same data channel are dropped
// before a control code that sets the data channel
{ pts: 0 * 1000, ccData: characters('no'), type: 0 },
{ pts: 0 * 1000, ccData: characters('t '), type: 0 },
{ pts: 0 * 1000, ccData: characters('th'), type: 0 },
{ pts: 0 * 1000, ccData: characters('is'), type: 0 },
// EOC (end of caption), sets CC1
{ pts: 1 * 1000, ccData: 0x142f, type: 0 },
// RCL (resume caption loading)
{ pts: 1 * 1000, ccData: 0x1420, type: 0 },
// EOC, if data wasn't dropped this would dispatch a caption
{ pts: 2 * 1000, ccData: 0x142f, type: 0 },
// RCL
{ pts: 3 * 1000, ccData: 0x1420, type: 0 },
{ pts: 4 * 1000, ccData: characters('fi'), type: 0 },
{ pts: 4 * 1000, ccData: characters('el'), type: 0 },
{ pts: 4 * 1000, ccData: characters('d0'), type: 0 },
// EOC, mark display start
{ pts: 5 * 1000, ccData: 0x142f, type: 0 },
// EOC, duplicated as per spec
{ pts: 5 * 1000, ccData: 0x142f, type: 0 },
// EOC, mark display end and flush
{ pts: 6 * 1000, ccData: 0x142f, type: 0 },
// EOC not duplicated cuz not necessary
// now switch to field 1 and test that packets in the same field
// but DIFFERENT data channel are dropped
{ pts: 7 * 1000, ccData: characters('or'), type: 1 },
{ pts: 7 * 1000, ccData: characters(' t'), type: 1 },
{ pts: 7 * 1000, ccData: characters('hi'), type: 1 },
{ pts: 7 * 1000, ccData: characters('s.'), type: 1 },
// EOC (end of caption, sets CC4)
{ pts: 8 * 1000, ccData: 0x1d2f, type: 1 },
// RCL (resume caption loading)
{ pts: 8 * 1000, ccData: 0x1d20, type: 1 },
// EOC, if data wasn't dropped this would dispatch a caption
{ pts: 9 * 1000, ccData: 0x1d2f, type: 1 },
// RCL
{ pts: 10 * 1000, ccData: 0x1d20, type: 1 },
{ pts: 11 * 1000, ccData: characters('fi'), type: 1 },
{ pts: 11 * 1000, ccData: characters('el'), type: 1 },
{ pts: 11 * 1000, ccData: characters('d1'), type: 1 },
// EOC, mark display start
{ pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
// EOC, duplicated as per spec
{ pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
// EOC, mark display end and flush
{ pts: 13 * 1000, ccData: 0x1d2f, type: 1 }
// EOC not duplicated cuz not necessary
];
var seiNals = packets.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 2, 'received 2 captions');
assert.equal(captions[0].content[0].text, 'field0', 'received only confirmed field0 data');
assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
assert.equal(captions[1].content[0].text, 'field1', 'received only confirmed field1 data');
assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
});
QUnit.test('clears buffer and drops data until first command that sets activeChannel after reset', function(assert) {
var firstPackets, secondPackets, captions, seiNals1, seiNals2;
captions = [];
firstPackets = [
// RCL (resume caption loading), CC1
{ pts: 1 * 1000, ccData: 0x1420, type: 0 },
{ pts: 2 * 1000, ccData: characters('fi'), type: 0 },
{ pts: 2 * 1000, ccData: characters('el'), type: 0 },
{ pts: 2 * 1000, ccData: characters('d0'), type: 0 },
// EOC (end of caption), swap text to displayed memory
{ pts: 3 * 1000, ccData: 0x142f, type: 0 },
{ pts: 4 * 1000, ccData: characters('fi'), type: 0 },
{ pts: 4 * 1000, ccData: characters('el'), type: 0 },
{ pts: 4 * 1000, ccData: characters('d0'), type: 0 },
// RCL (resume caption loading), CC4
{ pts: 5 * 1000, ccData: 0x1d20, type: 1 },
{ pts: 6 * 1000, ccData: characters('fi'), type: 1 },
{ pts: 6 * 1000, ccData: characters('el'), type: 1 },
{ pts: 6 * 1000, ccData: characters('d1'), type: 1 },
// EOC (end of caption), swap text to displayed memory
{ pts: 7 * 1000, ccData: 0x1d2f, type: 1 },
{ pts: 8 * 1000, ccData: characters('fi'), type: 1 },
{ pts: 8 * 1000, ccData: characters('el'), type: 1 },
{ pts: 8 * 1000, ccData: characters('d1'), type: 1 }
];
secondPackets = [
// following packets are dropped
{ pts: 9 * 1000, ccData: characters('no'), type: 0 },
{ pts: 9 * 1000, ccData: characters('t '), type: 0 },
{ pts: 9 * 1000, ccData: characters('th'), type: 0 },
{ pts: 9 * 1000, ccData: characters('is'), type: 0 },
{ pts: 10 * 1000, ccData: characters('or'), type: 1 },
{ pts: 10 * 1000, ccData: characters(' t'), type: 1 },
{ pts: 10 * 1000, ccData: characters('hi'), type: 1 },
{ pts: 10 * 1000, ccData: characters('s.'), type: 1 },
// EOC (end of caption), sets CC1
{ pts: 11 * 1000, ccData: 0x142f, type: 0 },
// RCL (resume caption loading), CC1
{ pts: 11 * 1000, ccData: 0x1420, type: 0 },
// EOC, sets CC4
{ pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
// RCL, CC4
{ pts: 12 * 1000, ccData: 0x1d20, type: 1 },
// EOC, CC1, would dispatch caption if packets weren't ignored
{ pts: 13 * 1000, ccData: 0x142f, type: 0 },
// RCL, CC1
{ pts: 13 * 1000, ccData: 0x1420, type: 0 },
// EOC, CC4, would dispatch caption if packets weren't ignored
{ pts: 14 * 1000, ccData: 0x1d2f, type: 1 },
// RCL, CC4
{ pts: 14 * 1000, ccData: 0x1d20, type: 1 },
{ pts: 18 * 1000, ccData: characters('bu'), type: 0 },
{ pts: 18 * 1000, ccData: characters('t '), type: 0 },
{ pts: 18 * 1000, ccData: characters('th'), type: 0 },
{ pts: 18 * 1000, ccData: characters('is'), type: 0 },
{ pts: 19 * 1000, ccData: characters('an'), type: 1 },
{ pts: 19 * 1000, ccData: characters('d '), type: 1 },
{ pts: 19 * 1000, ccData: characters('th'), type: 1 },
{ pts: 19 * 1000, ccData: characters('is'), type: 1 },
// EOC (end of caption), CC1, mark caption 1 start
{ pts: 20 * 1000, ccData: 0x142f, type: 0 },
// EOC, CC1, duplicated as per spec
{ pts: 20 * 1000, ccData: 0x142f, type: 0 },
// EOC, CC1, mark caption 1 end and dispatch
{ pts: 21 * 1000, ccData: 0x142f, type: 0 },
// No duplicate EOC cuz not necessary
// EOC, CC4, mark caption 2 start
{ pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
// EOC, CC4, duplicated as per spec
{ pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
// EOC, CC4, mark caption 2 end and dispatch
{ pts: 23 * 1000, ccData: 0x1d2f, type: 1 }
// No duplicate EOC cuz not necessary
];
seiNals1 = firstPackets.map(makeSeiFromCaptionPacket);
seiNals2 = secondPackets.map(makeSeiFromCaptionPacket);
captionStream.on('data', function(caption) {
captions.push(caption);
});
seiNals1.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14].text, 'field0',
'there is data in non-displayed memory for field 0 before reset');
assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14].text, 'field1',
'there is data in non-displayed memory for field 1 before reset');
assert.equal(captionStream.ccStreams_[0].displayed_[14].text, 'field0',
'there is data in displayed memory for field 0 before reset');
assert.equal(captionStream.ccStreams_[3].displayed_[14].text, 'field1',
'there is data in displayed memory for field 1 before reset');
captionStream.reset();
assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14].text, '',
'there is no data in non-displayed memory for field 0 after reset');
assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14].text, '',
'there is no data in non-displayed memory for field 1 after reset');
assert.equal(captionStream.ccStreams_[0].displayed_[14].text, '',
'there is no data in displayed memory for field 0 after reset');
assert.equal(captionStream.ccStreams_[3].displayed_[14].text, '',
'there is no data in displayed memory for field 1 after reset');
seiNals2.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 2, 'detected two captions');
assert.equal(captions[0].content[0].text, 'but this', 'parsed caption properly');
assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
assert.equal(captions[1].content[0].text, 'and this', 'parsed caption properly');
assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
});
QUnit.test("don't mess up 608 captions when 708 are present", function(assert) {
var captions = [];
captionStream.ccStreams_.forEach(function(cc) {
cc.on('data', function(caption) {
captions.push(caption);
});
});
var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captions.length, 3, 'parsed three captions');
// first caption stream
assert.equal(captions[0].content[0].text, 'BUT IT\'S NOT SUFFERING', 'first stream: parsed first content text correctly');
assert.equal(captions[0].content[1].text, 'RIGHW.', 'first stream: parsed second content text correctly');
// there is also bad data in the captions, but the null ascii character is removed
// second caption stream
assert.equal(captions[1].content[0].text, 'IT\'S NOT A THREAT TO ANYBODY.', 'second stream: parsed content text correctly');
// third stream
assert.equal(captions[2].content[0].text, 'WE TRY NOT TO PUT AN ANIMAL DOWN', 'third stream: parsed first content text correctly');
assert.equal(captions[2].content[1].text, 'IF WE DON\'T HAVE TO.', 'third stream: parsed second content text correctly');
});
QUnit.test("both 608 and 708 captions are available by default", function(assert) {
var cc608 = [];
var cc708 = [];
captionStream.on('data', function(caption) {
if (caption.stream === 'CC1') {
cc608.push(caption);
} else {
cc708.push(caption);
}
});
var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(cc608.length, 3, 'parsed three 608 cues');
assert.equal(cc708.length, 3, 'parsed three 708 cues');
});
QUnit.test("708 parsing can be turned off", function(assert) {
captionStream.reset();
captionStream = new m2ts.CaptionStream({
parse708captions: false
});
var cc608 = [];
var cc708 = [];
captionStream.on('data', function(caption) {
if (caption.stream === 'CC1') {
cc608.push(caption);
} else {
cc708.push(caption);
}
});
var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
seiNals.forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(cc608.length, 3, 'parsed three 608 cues');
assert.equal(cc708.length, 0, 'did not parse any 708 cues');
});
QUnit.test('ignores XDS and Text packets', function(assert) {
var captions = [];
captionStream.on('data', function(caption) {
captions.push(caption);
});
[
// RCL, resume caption loading, CC3
{ pts: 1000, ccData: 0x1520, type: 1 },
{ pts: 2000, ccData: characters('hi'), type: 1 },
// EOC, End of Caption
{ pts: 3000, ccData: 0x152f, type: 1 },
// Send another command so that the second EOC isn't ignored
{ pts: 3000, ccData: 0x152f, type: 1 },
// EOC, End of Caption
{ pts: 4000, ccData: 0x152f, type: 1 },
// ENM, Erase Non-Displayed Memory
{ pts: 4000, ccData: 0x152e, type: 1 },
// RCL, resume caption loading, CC1
{ pts: 5000, ccData: 0x1420, type: 0 }
].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captionStream.activeCea608Channel_[0], 0, 'field 1: CC1 is active');
assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
[
// TR, text restart, CC1
{ pts: 5000, ccData: 0x142a, type: 0 },
{ pts: 6000, ccData: characters('tx'), type: 0 }
].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
captionStream.flush();
assert.equal(captionStream.activeCea608Channel_[0], null, 'field 1: disabled');
assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
[
// EOC, End of Caption
{ pts: 7000, ccData: 0x142f, type: 0 },
// Send another command so that the second EOC isn't ignored
{ pts: 7000, ccData: 0x142f, type: 0 },
// EOC, End of Caption
{ pts: 8000, ccData: 0x142f, type: 0 },
// RCL, resume caption loading, CC3
{ pts: 9000, ccData: 0x1520, type: 1 },
// XDS start, "current" class, program identification number type
{ pts: 10000, ccData: 0x0101, type: 1 },