/
cea608_memory_unit.js
299 lines (252 loc) · 8.56 KB
/
cea608_memory_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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('Cea608Memory', () => {
const CeaUtils = shaka.test.CeaUtils;
const CharSet = shaka.cea.Cea608Memory.CharSet;
/** @type {!shaka.cea.Cea608Memory} */
let memory;
/** @type {string} */
const stream = 'CC1';
beforeEach(() => {
// Create a CC1 Memory: F1 + C1 -> CC1
memory = new shaka.cea.Cea608Memory(/* fieldNum= */ 0, /* channelNum= */ 0);
});
it('adds and emits a series of basic characters from the buffer', () => {
const text = 'test word';
const startTime = 1;
const endTime = 2;
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
const caption = memory.forceEmit(startTime, endTime);
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, text),
];
const expectedCaption = {
stream,
cue: topLevelCue,
};
expect(caption).toEqual(expectedCaption);
});
it('adds and emits a series of special characters from the buffer', () => {
const startTime = 1;
const endTime = 2;
const expectedText = '½¿èôÇ©ë»ö{ß│';
const charGroups = [
{
set: CharSet.SPECIAL_NORTH_AMERICAN,
chars: [0x32, 0x33, 0x3a, 0x3e], // ½, ¿, è, ô
},
{
set: CharSet.SPANISH_FRENCH,
chars: [0x32, 0x2b, 0x36, 0x3f], // Ç, ©, ë, »
},
{
set: CharSet.PORTUGUESE_GERMAN,
chars: [0x33, 0x29, 0x34, 0x37], // ö, {, ß, ¦
},
];
for (const group of charGroups) {
for (const c of group.chars) {
if (group.set === CharSet.SPANISH_FRENCH ||
group.set === CharSet.PORTUGUESE_GERMAN) {
// As per the CEA-608 spec, a char received from these extended sets
// does a backspace over a preceding char. Thus, the spec mandates any
// extended char to be preceded by a basic char, which serves as a
// fallback for systems that can't decode the extended char.
memory.addChar(
CharSet.BASIC_NORTH_AMERICAN, 0x20);
}
memory.addChar(group.set, c);
}
}
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, expectedText),
];
const expectedCaption= {
stream,
cue: topLevelCue,
};
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
it('assigns styling appropriately to caption', () => {
const startTime = 1;
const endTime = 2;
const expectedText = 'test';
memory.setUnderline(true);
memory.setItalics(true);
memory.setTextColor('red');
for (const c of expectedText) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
memory.setUnderline(false);
memory.setItalics(false);
for (const c of expectedText) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createStyledCue(startTime, endTime,
expectedText, /* underline= */ true,
/* italics= */ true, /* textColor= */ 'red',
/* backgroundColor= */ shaka.cea.CeaUtils.DEFAULT_BG_COLOR),
CeaUtils.createStyledCue(startTime, endTime,
expectedText, /* underline= */ false,
/* italics= */ false, /* textColor= */ 'red',
/* backgroundColor= */ shaka.cea.CeaUtils.DEFAULT_BG_COLOR),
];
const expectedCaption = {
stream,
cue: topLevelCue,
};
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
it('trims leading and trailing newlines', () => {
const startTime = 1;
const endTime = 2;
const text = 'test';
memory.setRow(memory.getRow()+1);
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
memory.setRow(memory.getRow()+1);
memory.setRow(memory.getRow()+1);
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
memory.setRow(memory.getRow()+1);
memory.setRow(memory.getRow()+1);
// At this point, the memory looks like this:
// [1]:
// [2]: test
// [3]:
// [4]: test
// ...
// So we expect that test\n\ntest is emitted
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, text),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, text),
];
const expectedCaption = {
stream,
cue: topLevelCue,
};
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
it('does not emit caption when all rows are empty', () => {
const startTime = 1;
const endTime = 2;
memory.setRow(memory.getRow()+1);
memory.setRow(memory.getRow()+1);
memory.setRow(memory.getRow()+1);
memory.forceEmit(startTime, endTime);
// Nothing was added to the buffer, so nothing should be emitted.
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toBe(null);
});
it('erases a character from the buffer', () => {
const startTime = 1;
const endTime = 2;
const text = 'testt';
const expectedText = 'test';
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
}
memory.eraseChar(); // Erase the last 't' from 'testt'
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, expectedText),
];
const expectedCaption = {
stream,
cue: topLevelCue,
};
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
describe('eraseBuffer', () => {
it('erases the entire buffer', () => {
const startTime = 1;
const endTime = 2;
const text = '0123456789abcde';
// Add characters to the memory buffer.
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
memory.setRow(memory.getRow() + 1); // increment row
}
// Erase the entire memory buffer.
memory.eraseBuffer();
// Force out the memory buffer.
const caption = memory.forceEmit(startTime, endTime);
// Expect the forced out memory to be blank. We just cleared it.
expect(caption).toBe(null);
});
});
describe('moveRows', () => {
it('moves a set number of rows to a new position in the buffer', () => {
const startTime = 1;
const endTime = 2;
const text = 'test';
// Add the text to the buffer, each character on separate rows.
// At this point, the memory looks like:
// [1]: t
// [2]: e
// [3]: s
// [4]: t
for (const c of text) {
memory.addChar(CharSet.BASIC_NORTH_AMERICAN,
c.charCodeAt(0));
memory.setRow(memory.getRow() + 1); // increment row
}
// Move first 2 rows down to 5th row, and then clear their old positions.
// After these operations, the memory looks like:
// [1]:
// [2]:
// [3]: s
// [4]: t
// [5]: t
// [6]: e
const srcRowIdx = 1;
const dstRowIdx = 5;
const rowsToMove = 2;
memory.moveRows(dstRowIdx, srcRowIdx, rowsToMove);
memory.resetRows(srcRowIdx, rowsToMove - 1);
// Expected text is 's\nt\nt\ne'
const topLevelCue = new shaka.text.Cue(startTime, endTime, '');
topLevelCue.nestedCues = [
CeaUtils.createDefaultCue(startTime, endTime, 's'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 't'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 't'),
CeaUtils.createLineBreakCue(startTime, endTime),
CeaUtils.createDefaultCue(startTime, endTime, 'e'),
];
const expectedCaption = {
stream,
cue: topLevelCue,
};
// Force out the new memory.
const caption = memory.forceEmit(startTime, endTime);
expect(caption).toEqual(expectedCaption);
});
});
});