/
cea708_service_unit.js
674 lines (556 loc) · 21.4 KB
/
cea708_service_unit.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('Cea708Service', () => {
const CeaUtils = shaka.test.CeaUtils;
/** @type {!shaka.cea.Cea708Service} */
let service;
/**
* Hide window (2 bytes), with a bitmap provided to indicate all windows.
* @type {!Array<number>}
*/
const hideWindow = [0x8a, 0xff];
/**
* Define window (7 bytes), defines window #0 to be a visible window
* with 32 rows and 32 columns. (We specify 31 for each since decoder adds 1).
* @type {!Array<number>}
*/
const defineWindow = [
0x98, 0x38, 0x00, 0x00, 0x1f, 0x1f, 0x00,
];
/** @type {number} */
const startTime = 1;
/** @type {number} */
const endTime = 2;
/**
* We arbitrarily pick service 1 for all of these tests.
* @type {number}
*/
const serviceNumber = 1;
/** @type {string} */
const stream = `svc${serviceNumber}`;
/**
* Takes in a array of bytes and a presentation timestamp (in seconds),
* and converts it into a CEA-708 DTVCC Packet.
* @param {!Array<number>} bytes
* @param {number} pts
*/
const createCea708PacketFromBytes = (bytes, pts) => {
const cea708Bytes = bytes.map((code, i) => {
return {
pts,
type: shaka.cea.DtvccPacketBuilder.DTVCC_PACKET_DATA,
value: code,
order: i,
};
});
return new shaka.cea.DtvccPacket(cea708Bytes);
};
/**
* Takes in a CEA-708 service and array of 708 packets with control codes,
* and returns all the captions inside of them, using the service to decode.
* @param {!shaka.cea.Cea708Service} service
* @param {...!shaka.cea.DtvccPacket} packets
*/
const getCaptionsFromPackets = (service, ...packets) => {
const captions = [];
for (const packet of packets) {
while (packet.hasMoreData()) {
const caption = service.handleCea708ControlCode(packet);
if (caption) {
captions.push(caption);
}
}
}
return captions;
};
beforeEach(() => {
service = new shaka.cea.Cea708Service(serviceNumber);
});
it('decodes regular unstyled caption text', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('setPenLocation sets the pen location correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// SetPenLocation command to move the pen to (2, 0)
0x92, 0x02, 0x00,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// After decoding, the buffer should look like this (omitting null cells).
// [0]: test
// [1]:
// [2]: test
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('setPenAttributes sets underline and italics correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// setPenAttributes. First byte is a "don't care", since this
// decoder ignores it. First 2 bits of second byte are italics
// and underline toggles. Turn on italics + underline.
0x90, 0x00, 0xc0,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// setPenAttributes. Turn off italics + underline.
0x90, 0x00, 0x00,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// Three nested cues, where the middle one should be underlined+italicized.
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
CeaUtils.createStyledCue(
startTime, endTime, text,
/* underline= */ true, /* italics= */ true,
/* textColor= */ shaka.cea.CeaUtils.DEFAULT_TXT_COLOR,
/* backgroundColor= */ shaka.cea.CeaUtils.DEFAULT_BG_COLOR),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('setPenColor sets foreground and background color correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// setPenColor (4 bytes). Last 6 bits of byte 2 are R,G,B for foreground.
// Last 6 bits of byte 3 are R,G,B for background. This decoder ignores
// byte 4 which is edge color, so it's a "don't care".
0x91, 0x30, 0x33, 0x00, // Red foreground, magenta background.
// Series of G0 control codes that add text.
0x63, 0x6f, 0x6c, 0x6f, 0x72, // c, o, l, o, r
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// Two nested cues, the second one should have colors.
const text1 = 'test';
const text2 = 'color';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text1),
CeaUtils.createStyledCue(
startTime, endTime, text2,
/* underline= */ false, /* italics= */ false,
/* textColor= */ 'red', /* backgroundColor= */ 'magenta'),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles special characters from the G0, G1, G2, and G3 groups', () => {
const controlCodes = [
...defineWindow,
// Series of G0 text control code
0x7f, // A musical note, the only exception the G0 table has to ASCII.
// setPenLocation (1, 0) to go to next row.
0x92, 0x01, 0x00,
// Series of G1 control codes that add text.
0xa9, 0xb6, 0xf7, // ©, ¶, ÷
// setPenLocation (2, 0) to go to next row.
0x92, 0x02, 0x00,
// Series of G2 control codes that add text.
0x1079, 0x107b, 0x1039, // ⅞, ┐, ™
// setPenLocation (3, 0) to go to next row.
0x92, 0x03, 0x00,
// G3 control code.
0x10a0, // As of CEA-708-E, there is only 1 char in G3, on 0xa0.
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
const text1 = '♪';
const text2 = '©¶÷';
const text3 = '⅞┐™';
const text4 = '[CC]';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text1),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text2),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text3),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text4),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('adds an underline for unsupported chars from the G2/G3 groups', () => {
const controlCodes = [
...defineWindow,
// Series of G2 control codes that add text.
0x1036, 0x103c, 0x1070, // unsupported, œ, unsupported
// setPenLocation (1, 0) to go to next row.
0x92, 0x01, 0x00,
// Series of G3 control codes that add text.
0x10a0, 0x10a1, 0x10db, // [CC], unsupported, unsupported
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// Some of the characters are unsupported as of CEA-708-E, so they should
// be replaced by an underline.
const text1 = '_œ_';
const text2 = '[CC]__';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text1),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text2),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles the reset command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
const resetControlCode = [0x8f];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(resetControlCode, endTime);
// The text in the current window should have been emitted, and then clear
// should have been called.
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
spyOn(service, 'clear').and.callThrough();
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
expect(service.clear).toHaveBeenCalledTimes(1);
});
it('handles the setWindowAttributes command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// Currently, setWindowAttributes is only used to justify text,
// as specified by the last 2 bits of the fourth byte. The
// other bytes after the first byte are "don't care".
0x97, 0x00, 0x00, 0x01, 0x00, // Justify right
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// Right-justified text is expected.
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.textAlign = shaka.text.Cue.textAlign.RIGHT;
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles the carriage return command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, // t, e,
// Carriage return.
0x0d,
// Series of G0 control codes that add text.
0x73, 0x74, // s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
const text1 = 'te';
const text2 = 'st';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text1),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text2),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles the horizontal carriage return command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, // t, e,
// setPenLocation (1, 0) to go to next row.
0x92, 0x01, 0x00,
// Series of G0 control codes that add text.
0x6d, 0x70, // m, p
// Horizontal Carriage return.
0x0e,
// Series of G0 control codes that add text.
0x73, 0x74, // s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// HCR wipes the row and moves the pen to the row start.
const text1 = 'te';
const text2 = 'st';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text1),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text2),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles the ASCII backspace command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
// Backspace.
0x08,
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// Backspace should have erased the last 't' in 'test'.
const text = 'tes';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles the ASCII form-feed command correctly', () => {
const controlCodes = [
...defineWindow,
// Series of G0 control codes that add text.
0x61, 0x62, // a, b,
// setPenLocation (1, 0) to go to next row.
0x92, 0x01, 0x00,
// Series of G0 control codes that add text.
0x62, 0x61, // b, a
// Form-feed.
0x0c,
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
const packet1 = createCea708PacketFromBytes(controlCodes, startTime);
const packet2 = createCea708PacketFromBytes(hideWindow, endTime);
// The form feed control code would have wiped the entire window
// including new lines, and the text after is just 'test'.
const text = 'test';
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2);
expect(captions).toEqual(expectedCaptions);
});
it('handles C2 and C3 no-op control codes correctly', () => {
// As of CEA-708, the C2 and C3 control code group has no operations.
// However, the bytes are reserved for future modifications to the spec,
// and so the correct # of bytes should be skipped if they are seen.
const packets = [
// C2 control code data.
[0x1008, 0x00], // C2 Packet 1.
[0x1010, 0x00, 0x00], // C2 Packet 2.
[0x1018, 0x00, 0x00, 0x00], // C2 Packet 3.
// C3 control code data.
[0x1080, 0x00, 0x00, 0x00, 0x00], // C3 packet 1.
[0x1088, 0x00, 0x00, 0x00, 0x00, 0x00], // C3 packet 2.
];
const expectedSkips = [1, 2, 3, 4, 5]; // As per the CEA-708-E spec.
for (let i = 0; i < packets.length; i++) {
const packet = createCea708PacketFromBytes(packets[i], /* pts= */ 1);
spyOn(packet, 'skip');
getCaptionsFromPackets(service, packet);
expect(packet.skip).toHaveBeenCalledWith(expectedSkips[i]);
}
});
describe('handles commands that change the display of windows', () => {
const time1 = 1;
const time2 = 2;
const time3 = 4;
const time4 = 5;
const textControlCodes = [
// Series of G0 control codes that add text.
0x74, 0x65, 0x73, 0x74, // t, e, s, t
];
// These commands affect ALL windows, per the 0xff bitmap.
const toggleWindow = [0x8b, 0xff];
const displayWindow = [0x89, 0xff];
const deleteWindow = [0x8c, 0xff];
const clearWindow = [0x88, 0xff];
it('handles display, toggle, and delete commands on windows', () => {
// Define a visible window, add some text, and toggle it off,
// which should force the window to emit the caption, 'test'.
const packet1 = createCea708PacketFromBytes(defineWindow, time1);
const packet2 = createCea708PacketFromBytes(textControlCodes, time1);
const packet3 = createCea708PacketFromBytes(toggleWindow, time2);
// Window is now hidden. Turn it back on at time 3, and append
// more text to it.
const packet4 = createCea708PacketFromBytes(displayWindow, time3);
const packet5 = createCea708PacketFromBytes(textControlCodes, time3);
// Window is now being displayed. Delete all the windows.
// This should force the displayed window to emit the caption, 'testtest'.
const packet6 = createCea708PacketFromBytes(deleteWindow, time4);
const text1 = 'test';
const text2 = 'testtest';
const topLevelCue1 = new shaka.text.Cue(
/* startTime= */ time1, /* endTime= */ time2, '');
topLevelCue1.nestedCues = [
CeaUtils.createDefaultCue(
/* startTime= */ time1, /* endTime= */ time2, /* payload= */ text1),
];
const topLevelCue2 = new shaka.text.Cue(
/* startTime= */ time3, /* endTime= */ time4, '');
topLevelCue2.nestedCues = [
CeaUtils.createDefaultCue(
/* startTime= */ time3, /* endTime= */ time4, /* payload= */ text2),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue1,
},
{
stream,
cue: topLevelCue2,
},
];
const captions = getCaptionsFromPackets(
service, packet1, packet2, packet3, packet4, packet5, packet6);
expect(captions).toEqual(expectedCaptions);
});
it('handles the clear command on a window', () => {
// Define a visible window, add text to it, and then clear it.
// This should emit a caption, since a visible window is being cleared.
const packet1 = createCea708PacketFromBytes(defineWindow, time1);
const packet2 = createCea708PacketFromBytes(textControlCodes, time1);
const packet3 = createCea708PacketFromBytes(clearWindow, time2);
// Display the window again, and then hide it. Although a visible window
// that turns off usually emits, this should NOT emit a caption, since
// the window contains nothing in it after the clear.
const packet4 = createCea708PacketFromBytes(displayWindow, time3);
const packet5 = createCea708PacketFromBytes(textControlCodes, time3);
const packet6 = createCea708PacketFromBytes(hideWindow, time1);
// Only one cue should have been emitted as per the explanation above.
const text = 'test';
const topLevelCue = new shaka.text.Cue(
/* startTime= */ time1, /* endTime= */ time2, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(
/* startTime= */ time1, /* endTime= */ time2, /* payload= */ text),
];
const expectedCaptions = [
{
stream,
cue: topLevelCue,
},
];
const captions = getCaptionsFromPackets(service, packet1, packet2,
packet3, packet4, packet5, packet6);
expect(captions).toEqual(expectedCaptions);
});
});
});