caption-stream.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /**
  2. * mux.js
  3. *
  4. * Copyright (c) 2015 Brightcove
  5. * All rights reserved.
  6. *
  7. * Reads in-band caption information from a video elementary
  8. * stream. Captions must follow the CEA-708 standard for injection
  9. * into an MPEG-2 transport streams.
  10. * @see https://en.wikipedia.org/wiki/CEA-708
  11. * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
  12. */
  13. 'use strict';
  14. // -----------------
  15. // Link To Transport
  16. // -----------------
  17. // Supplemental enhancement information (SEI) NAL units have a
  18. // payload type field to indicate how they are to be
  19. // interpreted. CEAS-708 caption content is always transmitted with
  20. // payload type 0x04.
  21. var USER_DATA_REGISTERED_ITU_T_T35 = 4,
  22. RBSP_TRAILING_BITS = 128,
  23. Stream = require('../utils/stream');
  24. /**
  25. * Parse a supplemental enhancement information (SEI) NAL unit.
  26. * Stops parsing once a message of type ITU T T35 has been found.
  27. *
  28. * @param bytes {Uint8Array} the bytes of a SEI NAL unit
  29. * @return {object} the parsed SEI payload
  30. * @see Rec. ITU-T H.264, 7.3.2.3.1
  31. */
  32. var parseSei = function(bytes) {
  33. var
  34. i = 0,
  35. result = {
  36. payloadType: -1,
  37. payloadSize: 0
  38. },
  39. payloadType = 0,
  40. payloadSize = 0;
  41. // go through the sei_rbsp parsing each each individual sei_message
  42. while (i < bytes.byteLength) {
  43. // stop once we have hit the end of the sei_rbsp
  44. if (bytes[i] === RBSP_TRAILING_BITS) {
  45. break;
  46. }
  47. // Parse payload type
  48. while (bytes[i] === 0xFF) {
  49. payloadType += 255;
  50. i++;
  51. }
  52. payloadType += bytes[i++];
  53. // Parse payload size
  54. while (bytes[i] === 0xFF) {
  55. payloadSize += 255;
  56. i++;
  57. }
  58. payloadSize += bytes[i++];
  59. // this sei_message is a 608/708 caption so save it and break
  60. // there can only ever be one caption message in a frame's sei
  61. if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
  62. result.payloadType = payloadType;
  63. result.payloadSize = payloadSize;
  64. result.payload = bytes.subarray(i, i + payloadSize);
  65. break;
  66. }
  67. // skip the payload and parse the next message
  68. i += payloadSize;
  69. payloadType = 0;
  70. payloadSize = 0;
  71. }
  72. return result;
  73. };
  74. // see ANSI/SCTE 128-1 (2013), section 8.1
  75. var parseUserData = function(sei) {
  76. // itu_t_t35_contry_code must be 181 (United States) for
  77. // captions
  78. if (sei.payload[0] !== 181) {
  79. return null;
  80. }
  81. // itu_t_t35_provider_code should be 49 (ATSC) for captions
  82. if (((sei.payload[1] << 8) | sei.payload[2]) !== 49) {
  83. return null;
  84. }
  85. // the user_identifier should be "GA94" to indicate ATSC1 data
  86. if (String.fromCharCode(sei.payload[3],
  87. sei.payload[4],
  88. sei.payload[5],
  89. sei.payload[6]) !== 'GA94') {
  90. return null;
  91. }
  92. // finally, user_data_type_code should be 0x03 for caption data
  93. if (sei.payload[7] !== 0x03) {
  94. return null;
  95. }
  96. // return the user_data_type_structure and strip the trailing
  97. // marker bits
  98. return sei.payload.subarray(8, sei.payload.length - 1);
  99. };
  100. // see CEA-708-D, section 4.4
  101. var parseCaptionPackets = function(pts, userData) {
  102. var results = [], i, count, offset, data;
  103. // if this is just filler, return immediately
  104. if (!(userData[0] & 0x40)) {
  105. return results;
  106. }
  107. // parse out the cc_data_1 and cc_data_2 fields
  108. count = userData[0] & 0x1f;
  109. for (i = 0; i < count; i++) {
  110. offset = i * 3;
  111. data = {
  112. type: userData[offset + 2] & 0x03,
  113. pts: pts
  114. };
  115. // capture cc data when cc_valid is 1
  116. if (userData[offset + 2] & 0x04) {
  117. data.ccData = (userData[offset + 3] << 8) | userData[offset + 4];
  118. results.push(data);
  119. }
  120. }
  121. return results;
  122. };
  123. var CaptionStream = function() {
  124. CaptionStream.prototype.init.call(this);
  125. this.captionPackets_ = [];
  126. this.ccStreams_ = [
  127. new Cea608Stream(0, 0), // eslint-disable-line no-use-before-define
  128. new Cea608Stream(0, 1), // eslint-disable-line no-use-before-define
  129. new Cea608Stream(1, 0), // eslint-disable-line no-use-before-define
  130. new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
  131. ];
  132. this.reset();
  133. // forward data and done events from CCs to this CaptionStream
  134. this.ccStreams_.forEach(function(cc) {
  135. cc.on('data', this.trigger.bind(this, 'data'));
  136. cc.on('done', this.trigger.bind(this, 'done'));
  137. }, this);
  138. };
  139. CaptionStream.prototype = new Stream();
  140. CaptionStream.prototype.push = function(event) {
  141. var sei, userData;
  142. // only examine SEI NALs
  143. if (event.nalUnitType !== 'sei_rbsp') {
  144. return;
  145. }
  146. // parse the sei
  147. sei = parseSei(event.escapedRBSP);
  148. // ignore everything but user_data_registered_itu_t_t35
  149. if (sei.payloadType !== USER_DATA_REGISTERED_ITU_T_T35) {
  150. return;
  151. }
  152. // parse out the user data payload
  153. userData = parseUserData(sei);
  154. // ignore unrecognized userData
  155. if (!userData) {
  156. return;
  157. }
  158. // Sometimes, the same segment # will be downloaded twice. To stop the
  159. // caption data from being processed twice, we track the latest dts we've
  160. // received and ignore everything with a dts before that. However, since
  161. // data for a specific dts can be split across 2 packets on either side of
  162. // a segment boundary, we need to make sure we *don't* ignore the second
  163. // dts packet we receive that has dts === this.latestDts_. And thus, the
  164. // ignoreNextEqualDts_ flag was born.
  165. if (event.dts < this.latestDts_) {
  166. // We've started getting older data, so set the flag.
  167. this.ignoreNextEqualDts_ = true;
  168. return;
  169. } else if ((event.dts === this.latestDts_) && (this.ignoreNextEqualDts_)) {
  170. // We've received the last duplicate packet, time to start processing again
  171. this.ignoreNextEqualDts_ = false;
  172. return;
  173. }
  174. // parse out CC data packets and save them for later
  175. this.captionPackets_ = this.captionPackets_.concat(parseCaptionPackets(event.pts, userData));
  176. this.latestDts_ = event.dts;
  177. };
  178. CaptionStream.prototype.flush = function() {
  179. // make sure we actually parsed captions before proceeding
  180. if (!this.captionPackets_.length) {
  181. this.ccStreams_.forEach(function(cc) {
  182. cc.flush();
  183. }, this);
  184. return;
  185. }
  186. // In Chrome, the Array#sort function is not stable so add a
  187. // presortIndex that we can use to ensure we get a stable-sort
  188. this.captionPackets_.forEach(function(elem, idx) {
  189. elem.presortIndex = idx;
  190. });
  191. // sort caption byte-pairs based on their PTS values
  192. this.captionPackets_.sort(function(a, b) {
  193. if (a.pts === b.pts) {
  194. return a.presortIndex - b.presortIndex;
  195. }
  196. return a.pts - b.pts;
  197. });
  198. this.captionPackets_.forEach(function(packet) {
  199. if (packet.type < 2) {
  200. // Dispatch packet to the right Cea608Stream
  201. this.dispatchCea608Packet(packet);
  202. }
  203. // this is where an 'else' would go for a dispatching packets
  204. // to a theoretical Cea708Stream that handles SERVICEn data
  205. }, this);
  206. this.captionPackets_.length = 0;
  207. this.ccStreams_.forEach(function(cc) {
  208. cc.flush();
  209. }, this);
  210. return;
  211. };
  212. CaptionStream.prototype.reset = function() {
  213. this.latestDts_ = null;
  214. this.ignoreNextEqualDts_ = false;
  215. this.activeCea608Channel_ = [null, null];
  216. this.ccStreams_.forEach(function(ccStream) {
  217. ccStream.reset();
  218. });
  219. };
  220. CaptionStream.prototype.dispatchCea608Packet = function(packet) {
  221. // NOTE: packet.type is the CEA608 field
  222. if (this.setsChannel1Active(packet)) {
  223. this.activeCea608Channel_[packet.type] = 0;
  224. } else if (this.setsChannel2Active(packet)) {
  225. this.activeCea608Channel_[packet.type] = 1;
  226. }
  227. if (this.activeCea608Channel_[packet.type] === null) {
  228. // If we haven't received anything to set the active channel, discard the
  229. // data; we don't want jumbled captions
  230. return;
  231. }
  232. this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
  233. };
  234. CaptionStream.prototype.setsChannel1Active = function(packet) {
  235. return ((packet.ccData & 0x7800) === 0x1000);
  236. };
  237. CaptionStream.prototype.setsChannel2Active = function(packet) {
  238. return ((packet.ccData & 0x7800) === 0x1800);
  239. };
  240. // ----------------------
  241. // Session to Application
  242. // ----------------------
  243. var CHARACTER_TRANSLATION = {
  244. 0x2a: 0xe1, // á
  245. 0x5c: 0xe9, // é
  246. 0x5e: 0xed, // í
  247. 0x5f: 0xf3, // ó
  248. 0x60: 0xfa, // ú
  249. 0x7b: 0xe7, // ç
  250. 0x7c: 0xf7, // ÷
  251. 0x7d: 0xd1, // Ñ
  252. 0x7e: 0xf1, // ñ
  253. 0x7f: 0x2588, // █
  254. 0x0130: 0xae, // ®
  255. 0x0131: 0xb0, // °
  256. 0x0132: 0xbd, // ½
  257. 0x0133: 0xbf, // ¿
  258. 0x0134: 0x2122, // ™
  259. 0x0135: 0xa2, // ¢
  260. 0x0136: 0xa3, // £
  261. 0x0137: 0x266a, // ♪
  262. 0x0138: 0xe0, // à
  263. 0x0139: 0xa0, //
  264. 0x013a: 0xe8, // è
  265. 0x013b: 0xe2, // â
  266. 0x013c: 0xea, // ê
  267. 0x013d: 0xee, // î
  268. 0x013e: 0xf4, // ô
  269. 0x013f: 0xfb, // û
  270. 0x0220: 0xc1, // Á
  271. 0x0221: 0xc9, // É
  272. 0x0222: 0xd3, // Ó
  273. 0x0223: 0xda, // Ú
  274. 0x0224: 0xdc, // Ü
  275. 0x0225: 0xfc, // ü
  276. 0x0226: 0x2018, // ‘
  277. 0x0227: 0xa1, // ¡
  278. 0x0228: 0x2a, // *
  279. 0x0229: 0x27, // '
  280. 0x022a: 0x2014, // —
  281. 0x022b: 0xa9, // ©
  282. 0x022c: 0x2120, // ℠
  283. 0x022d: 0x2022, // •
  284. 0x022e: 0x201c, // “
  285. 0x022f: 0x201d, // ”
  286. 0x0230: 0xc0, // À
  287. 0x0231: 0xc2, // Â
  288. 0x0232: 0xc7, // Ç
  289. 0x0233: 0xc8, // È
  290. 0x0234: 0xca, // Ê
  291. 0x0235: 0xcb, // Ë
  292. 0x0236: 0xeb, // ë
  293. 0x0237: 0xce, // Î
  294. 0x0238: 0xcf, // Ï
  295. 0x0239: 0xef, // ï
  296. 0x023a: 0xd4, // Ô
  297. 0x023b: 0xd9, // Ù
  298. 0x023c: 0xf9, // ù
  299. 0x023d: 0xdb, // Û
  300. 0x023e: 0xab, // «
  301. 0x023f: 0xbb, // »
  302. 0x0320: 0xc3, // Ã
  303. 0x0321: 0xe3, // ã
  304. 0x0322: 0xcd, // Í
  305. 0x0323: 0xcc, // Ì
  306. 0x0324: 0xec, // ì
  307. 0x0325: 0xd2, // Ò
  308. 0x0326: 0xf2, // ò
  309. 0x0327: 0xd5, // Õ
  310. 0x0328: 0xf5, // õ
  311. 0x0329: 0x7b, // {
  312. 0x032a: 0x7d, // }
  313. 0x032b: 0x5c, // \
  314. 0x032c: 0x5e, // ^
  315. 0x032d: 0x5f, // _
  316. 0x032e: 0x7c, // |
  317. 0x032f: 0x7e, // ~
  318. 0x0330: 0xc4, // Ä
  319. 0x0331: 0xe4, // ä
  320. 0x0332: 0xd6, // Ö
  321. 0x0333: 0xf6, // ö
  322. 0x0334: 0xdf, // ß
  323. 0x0335: 0xa5, // ¥
  324. 0x0336: 0xa4, // ¤
  325. 0x0337: 0x2502, // │
  326. 0x0338: 0xc5, // Å
  327. 0x0339: 0xe5, // å
  328. 0x033a: 0xd8, // Ø
  329. 0x033b: 0xf8, // ø
  330. 0x033c: 0x250c, // ┌
  331. 0x033d: 0x2510, // ┐
  332. 0x033e: 0x2514, // └
  333. 0x033f: 0x2518 // ┘
  334. };
  335. var getCharFromCode = function(code) {
  336. if (code === null) {
  337. return '';
  338. }
  339. code = CHARACTER_TRANSLATION[code] || code;
  340. return String.fromCharCode(code);
  341. };
  342. // the index of the last row in a CEA-608 display buffer
  343. var BOTTOM_ROW = 14;
  344. // This array is used for mapping PACs -> row #, since there's no way of
  345. // getting it through bit logic.
  346. var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620,
  347. 0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420];
  348. // CEA-608 captions are rendered onto a 34x15 matrix of character
  349. // cells. The "bottom" row is the last element in the outer array.
  350. var createDisplayBuffer = function() {
  351. var result = [], i = BOTTOM_ROW + 1;
  352. while (i--) {
  353. result.push('');
  354. }
  355. return result;
  356. };
  357. var Cea608Stream = function(field, dataChannel) {
  358. Cea608Stream.prototype.init.call(this);
  359. this.field_ = field || 0;
  360. this.dataChannel_ = dataChannel || 0;
  361. this.name_ = 'CC' + (((this.field_ << 1) | this.dataChannel_) + 1);
  362. this.setConstants();
  363. this.reset();
  364. this.push = function(packet) {
  365. var data, swap, char0, char1, text;
  366. // remove the parity bits
  367. data = packet.ccData & 0x7f7f;
  368. // ignore duplicate control codes; the spec demands they're sent twice
  369. if (data === this.lastControlCode_) {
  370. this.lastControlCode_ = null;
  371. return;
  372. }
  373. // Store control codes
  374. if ((data & 0xf000) === 0x1000) {
  375. this.lastControlCode_ = data;
  376. } else if (data !== this.PADDING_) {
  377. this.lastControlCode_ = null;
  378. }
  379. char0 = data >>> 8;
  380. char1 = data & 0xff;
  381. if (data === this.PADDING_) {
  382. return;
  383. } else if (data === this.RESUME_CAPTION_LOADING_) {
  384. this.mode_ = 'popOn';
  385. } else if (data === this.END_OF_CAPTION_) {
  386. this.clearFormatting(packet.pts);
  387. // if a caption was being displayed, it's gone now
  388. this.flushDisplayed(packet.pts);
  389. // flip memory
  390. swap = this.displayed_;
  391. this.displayed_ = this.nonDisplayed_;
  392. this.nonDisplayed_ = swap;
  393. // start measuring the time to display the caption
  394. this.startPts_ = packet.pts;
  395. } else if (data === this.ROLL_UP_2_ROWS_) {
  396. this.topRow_ = BOTTOM_ROW - 1;
  397. this.mode_ = 'rollUp';
  398. } else if (data === this.ROLL_UP_3_ROWS_) {
  399. this.topRow_ = BOTTOM_ROW - 2;
  400. this.mode_ = 'rollUp';
  401. } else if (data === this.ROLL_UP_4_ROWS_) {
  402. this.topRow_ = BOTTOM_ROW - 3;
  403. this.mode_ = 'rollUp';
  404. } else if (data === this.CARRIAGE_RETURN_) {
  405. this.clearFormatting(packet.pts);
  406. this.flushDisplayed(packet.pts);
  407. this.shiftRowsUp_();
  408. this.startPts_ = packet.pts;
  409. } else if (data === this.BACKSPACE_) {
  410. if (this.mode_ === 'popOn') {
  411. this.nonDisplayed_[BOTTOM_ROW] = this.nonDisplayed_[BOTTOM_ROW].slice(0, -1);
  412. } else {
  413. this.displayed_[BOTTOM_ROW] = this.displayed_[BOTTOM_ROW].slice(0, -1);
  414. }
  415. } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
  416. this.flushDisplayed(packet.pts);
  417. this.displayed_ = createDisplayBuffer();
  418. } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
  419. this.nonDisplayed_ = createDisplayBuffer();
  420. } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
  421. this.mode_ = 'paintOn';
  422. // Append special characters to caption text
  423. } else if (this.isSpecialCharacter(char0, char1)) {
  424. // Bitmask char0 so that we can apply character transformations
  425. // regardless of field and data channel.
  426. // Then byte-shift to the left and OR with char1 so we can pass the
  427. // entire character code to `getCharFromCode`.
  428. char0 = (char0 & 0x03) << 8;
  429. text = getCharFromCode(char0 | char1);
  430. this[this.mode_](packet.pts, text);
  431. this.column_++;
  432. // Append extended characters to caption text
  433. } else if (this.isExtCharacter(char0, char1)) {
  434. // Extended characters always follow their "non-extended" equivalents.
  435. // IE if a "è" is desired, you'll always receive "eè"; non-compliant
  436. // decoders are supposed to drop the "è", while compliant decoders
  437. // backspace the "e" and insert "è".
  438. // Delete the previous character
  439. if (this.mode_ === 'popOn') {
  440. this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
  441. } else {
  442. this.displayed_[BOTTOM_ROW] = this.displayed_[BOTTOM_ROW].slice(0, -1);
  443. }
  444. // Bitmask char0 so that we can apply character transformations
  445. // regardless of field and data channel.
  446. // Then byte-shift to the left and OR with char1 so we can pass the
  447. // entire character code to `getCharFromCode`.
  448. char0 = (char0 & 0x03) << 8;
  449. text = getCharFromCode(char0 | char1);
  450. this[this.mode_](packet.pts, text);
  451. this.column_++;
  452. // Process mid-row codes
  453. } else if (this.isMidRowCode(char0, char1)) {
  454. // Attributes are not additive, so clear all formatting
  455. this.clearFormatting(packet.pts);
  456. // According to the standard, mid-row codes
  457. // should be replaced with spaces, so add one now
  458. this[this.mode_](packet.pts, ' ');
  459. this.column_++;
  460. if ((char1 & 0xe) === 0xe) {
  461. this.addFormatting(packet.pts, ['i']);
  462. }
  463. if ((char1 & 0x1) === 0x1) {
  464. this.addFormatting(packet.pts, ['u']);
  465. }
  466. // Detect offset control codes and adjust cursor
  467. } else if (this.isOffsetControlCode(char0, char1)) {
  468. // Cursor position is set by indent PAC (see below) in 4-column
  469. // increments, with an additional offset code of 1-3 to reach any
  470. // of the 32 columns specified by CEA-608. So all we need to do
  471. // here is increment the column cursor by the given offset.
  472. this.column_ += (char1 & 0x03);
  473. // Detect PACs (Preamble Address Codes)
  474. } else if (this.isPAC(char0, char1)) {
  475. // There's no logic for PAC -> row mapping, so we have to just
  476. // find the row code in an array and use its index :(
  477. var row = ROWS.indexOf(data & 0x1f20);
  478. if (row !== this.row_) {
  479. // formatting is only persistent for current row
  480. this.clearFormatting(packet.pts);
  481. this.row_ = row;
  482. }
  483. // All PACs can apply underline, so detect and apply
  484. // (All odd-numbered second bytes set underline)
  485. if ((char1 & 0x1) && (this.formatting_.indexOf('u') === -1)) {
  486. this.addFormatting(packet.pts, ['u']);
  487. }
  488. if ((data & 0x10) === 0x10) {
  489. // We've got an indent level code. Each successive even number
  490. // increments the column cursor by 4, so we can get the desired
  491. // column position by bit-shifting to the right (to get n/2)
  492. // and multiplying by 4.
  493. this.column_ = ((data & 0xe) >> 1) * 4;
  494. }
  495. if (this.isColorPAC(char1)) {
  496. // it's a color code, though we only support white, which
  497. // can be either normal or italicized. white italics can be
  498. // either 0x4e or 0x6e depending on the row, so we just
  499. // bitwise-and with 0xe to see if italics should be turned on
  500. if ((char1 & 0xe) === 0xe) {
  501. this.addFormatting(packet.pts, ['i']);
  502. }
  503. }
  504. // We have a normal character in char0, and possibly one in char1
  505. } else if (this.isNormalChar(char0)) {
  506. if (char1 === 0x00) {
  507. char1 = null;
  508. }
  509. text = getCharFromCode(char0);
  510. text += getCharFromCode(char1);
  511. this[this.mode_](packet.pts, text);
  512. this.column_ += text.length;
  513. } // finish data processing
  514. };
  515. };
  516. Cea608Stream.prototype = new Stream();
  517. // Trigger a cue point that captures the current state of the
  518. // display buffer
  519. Cea608Stream.prototype.flushDisplayed = function(pts) {
  520. var content = this.displayed_
  521. // remove spaces from the start and end of the string
  522. .map(function(row) {
  523. return row.trim();
  524. })
  525. // combine all text rows to display in one cue
  526. .join('\n')
  527. // and remove blank rows from the start and end, but not the middle
  528. .replace(/^\n+|\n+$/g, '');
  529. if (content.length) {
  530. this.trigger('data', {
  531. startPts: this.startPts_,
  532. endPts: pts,
  533. text: content,
  534. stream: this.name_
  535. });
  536. }
  537. };
  538. /**
  539. * Zero out the data, used for startup and on seek
  540. */
  541. Cea608Stream.prototype.reset = function() {
  542. this.mode_ = 'popOn';
  543. // When in roll-up mode, the index of the last row that will
  544. // actually display captions. If a caption is shifted to a row
  545. // with a lower index than this, it is cleared from the display
  546. // buffer
  547. this.topRow_ = 0;
  548. this.startPts_ = 0;
  549. this.displayed_ = createDisplayBuffer();
  550. this.nonDisplayed_ = createDisplayBuffer();
  551. this.lastControlCode_ = null;
  552. // Track row and column for proper line-breaking and spacing
  553. this.column_ = 0;
  554. this.row_ = BOTTOM_ROW;
  555. // This variable holds currently-applied formatting
  556. this.formatting_ = [];
  557. };
  558. /**
  559. * Sets up control code and related constants for this instance
  560. */
  561. Cea608Stream.prototype.setConstants = function() {
  562. // The following attributes have these uses:
  563. // ext_ : char0 for mid-row codes, and the base for extended
  564. // chars (ext_+0, ext_+1, and ext_+2 are char0s for
  565. // extended codes)
  566. // control_: char0 for control codes, except byte-shifted to the
  567. // left so that we can do this.control_ | CONTROL_CODE
  568. // offset_: char0 for tab offset codes
  569. //
  570. // It's also worth noting that control codes, and _only_ control codes,
  571. // differ between field 1 and field2. Field 2 control codes are always
  572. // their field 1 value plus 1. That's why there's the "| field" on the
  573. // control value.
  574. if (this.dataChannel_ === 0) {
  575. this.BASE_ = 0x10;
  576. this.EXT_ = 0x11;
  577. this.CONTROL_ = (0x14 | this.field_) << 8;
  578. this.OFFSET_ = 0x17;
  579. } else if (this.dataChannel_ === 1) {
  580. this.BASE_ = 0x18;
  581. this.EXT_ = 0x19;
  582. this.CONTROL_ = (0x1c | this.field_) << 8;
  583. this.OFFSET_ = 0x1f;
  584. }
  585. // Constants for the LSByte command codes recognized by Cea608Stream. This
  586. // list is not exhaustive. For a more comprehensive listing and semantics see
  587. // http://www.gpo.gov/fdsys/pkg/CFR-2010-title47-vol1/pdf/CFR-2010-title47-vol1-sec15-119.pdf
  588. // Padding
  589. this.PADDING_ = 0x0000;
  590. // Pop-on Mode
  591. this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
  592. this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f;
  593. // Roll-up Mode
  594. this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
  595. this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
  596. this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
  597. this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d;
  598. // paint-on mode (not supported)
  599. this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29;
  600. // Erasure
  601. this.BACKSPACE_ = this.CONTROL_ | 0x21;
  602. this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
  603. this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
  604. };
  605. /**
  606. * Detects if the 2-byte packet data is a special character
  607. *
  608. * Special characters have a second byte in the range 0x30 to 0x3f,
  609. * with the first byte being 0x11 (for data channel 1) or 0x19 (for
  610. * data channel 2).
  611. *
  612. * @param {Integer} char0 The first byte
  613. * @param {Integer} char1 The second byte
  614. * @return {Boolean} Whether the 2 bytes are an special character
  615. */
  616. Cea608Stream.prototype.isSpecialCharacter = function(char0, char1) {
  617. return (char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f);
  618. };
  619. /**
  620. * Detects if the 2-byte packet data is an extended character
  621. *
  622. * Extended characters have a second byte in the range 0x20 to 0x3f,
  623. * with the first byte being 0x12 or 0x13 (for data channel 1) or
  624. * 0x1a or 0x1b (for data channel 2).
  625. *
  626. * @param {Integer} char0 The first byte
  627. * @param {Integer} char1 The second byte
  628. * @return {Boolean} Whether the 2 bytes are an extended character
  629. */
  630. Cea608Stream.prototype.isExtCharacter = function(char0, char1) {
  631. return ((char0 === (this.EXT_ + 1) || char0 === (this.EXT_ + 2)) &&
  632. (char1 >= 0x20 && char1 <= 0x3f));
  633. };
  634. /**
  635. * Detects if the 2-byte packet is a mid-row code
  636. *
  637. * Mid-row codes have a second byte in the range 0x20 to 0x2f, with
  638. * the first byte being 0x11 (for data channel 1) or 0x19 (for data
  639. * channel 2).
  640. *
  641. * @param {Integer} char0 The first byte
  642. * @param {Integer} char1 The second byte
  643. * @return {Boolean} Whether the 2 bytes are a mid-row code
  644. */
  645. Cea608Stream.prototype.isMidRowCode = function(char0, char1) {
  646. return (char0 === this.EXT_ && (char1 >= 0x20 && char1 <= 0x2f));
  647. };
  648. /**
  649. * Detects if the 2-byte packet is an offset control code
  650. *
  651. * Offset control codes have a second byte in the range 0x21 to 0x23,
  652. * with the first byte being 0x17 (for data channel 1) or 0x1f (for
  653. * data channel 2).
  654. *
  655. * @param {Integer} char0 The first byte
  656. * @param {Integer} char1 The second byte
  657. * @return {Boolean} Whether the 2 bytes are an offset control code
  658. */
  659. Cea608Stream.prototype.isOffsetControlCode = function(char0, char1) {
  660. return (char0 === this.OFFSET_ && (char1 >= 0x21 && char1 <= 0x23));
  661. };
  662. /**
  663. * Detects if the 2-byte packet is a Preamble Address Code
  664. *
  665. * PACs have a first byte in the range 0x10 to 0x17 (for data channel 1)
  666. * or 0x18 to 0x1f (for data channel 2), with the second byte in the
  667. * range 0x40 to 0x7f.
  668. *
  669. * @param {Integer} char0 The first byte
  670. * @param {Integer} char1 The second byte
  671. * @return {Boolean} Whether the 2 bytes are a PAC
  672. */
  673. Cea608Stream.prototype.isPAC = function(char0, char1) {
  674. return (char0 >= this.BASE_ && char0 < (this.BASE_ + 8) &&
  675. (char1 >= 0x40 && char1 <= 0x7f));
  676. };
  677. /**
  678. * Detects if a packet's second byte is in the range of a PAC color code
  679. *
  680. * PAC color codes have the second byte be in the range 0x40 to 0x4f, or
  681. * 0x60 to 0x6f.
  682. *
  683. * @param {Integer} char1 The second byte
  684. * @return {Boolean} Whether the byte is a color PAC
  685. */
  686. Cea608Stream.prototype.isColorPAC = function(char1) {
  687. return ((char1 >= 0x40 && char1 <= 0x4f) || (char1 >= 0x60 && char1 <= 0x7f));
  688. };
  689. /**
  690. * Detects if a single byte is in the range of a normal character
  691. *
  692. * Normal text bytes are in the range 0x20 to 0x7f.
  693. *
  694. * @param {Integer} char The byte
  695. * @return {Boolean} Whether the byte is a normal character
  696. */
  697. Cea608Stream.prototype.isNormalChar = function(char) {
  698. return (char >= 0x20 && char <= 0x7f);
  699. };
  700. // Adds the opening HTML tag for the passed character to the caption text,
  701. // and keeps track of it for later closing
  702. Cea608Stream.prototype.addFormatting = function(pts, format) {
  703. this.formatting_ = this.formatting_.concat(format);
  704. var text = format.reduce(function(text, format) {
  705. return text + '<' + format + '>';
  706. }, '');
  707. this[this.mode_](pts, text);
  708. };
  709. // Adds HTML closing tags for current formatting to caption text and
  710. // clears remembered formatting
  711. Cea608Stream.prototype.clearFormatting = function(pts) {
  712. if (!this.formatting_.length) {
  713. return;
  714. }
  715. var text = this.formatting_.reverse().reduce(function(text, format) {
  716. return text + '</' + format + '>';
  717. }, '');
  718. this.formatting_ = [];
  719. this[this.mode_](pts, text);
  720. };
  721. // Mode Implementations
  722. Cea608Stream.prototype.popOn = function(pts, text) {
  723. var baseRow = this.nonDisplayed_[this.row_];
  724. // buffer characters
  725. baseRow += text;
  726. this.nonDisplayed_[this.row_] = baseRow;
  727. };
  728. Cea608Stream.prototype.rollUp = function(pts, text) {
  729. var baseRow = this.displayed_[BOTTOM_ROW];
  730. baseRow += text;
  731. this.displayed_[BOTTOM_ROW] = baseRow;
  732. };
  733. Cea608Stream.prototype.shiftRowsUp_ = function() {
  734. var i;
  735. // clear out inactive rows
  736. for (i = 0; i < this.topRow_; i++) {
  737. this.displayed_[i] = '';
  738. }
  739. // shift displayed rows up
  740. for (i = this.topRow_; i < BOTTOM_ROW; i++) {
  741. this.displayed_[i] = this.displayed_[i + 1];
  742. }
  743. // clear out the bottom row
  744. this.displayed_[BOTTOM_ROW] = '';
  745. };
  746. // paintOn mode is not implemented
  747. Cea608Stream.prototype.paintOn = function() {};
  748. // exports
  749. module.exports = {
  750. CaptionStream: CaptionStream,
  751. Cea608Stream: Cea608Stream
  752. };