caption-stream.test.js 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096
  1. 'use strict';
  2. var segments = require('data-files!segments');
  3. var
  4. window = require('global/window'),
  5. captionStream,
  6. m2ts = require('../lib/m2ts'),
  7. mp4 = require('../lib/mp4'),
  8. QUnit = require('qunit'),
  9. seiNalUnitGenerator = require('./utils/sei-nal-unit-generator'),
  10. makeSeiFromCaptionPacket = seiNalUnitGenerator.makeSeiFromCaptionPacket,
  11. makeSeiFromMultipleCaptionPackets = seiNalUnitGenerator.makeSeiFromMultipleCaptionPackets,
  12. characters = seiNalUnitGenerator.characters,
  13. packetHeader708 = seiNalUnitGenerator.packetHeader708,
  14. displayWindows708 = seiNalUnitGenerator.displayWindows708,
  15. cc708PinkUnderscore = require('./utils/cc708-pink-underscore'),
  16. cc708Korean = require('./utils/cc708-korean'),
  17. sintelCaptions = segments['sintel-captions.ts'](),
  18. mixed608708Captions = require('./utils/mixed-608-708-captions.js'),
  19. multiChannel608Captions = segments['multi-channel-608-captions.ts']();
  20. QUnit.module('Caption Stream', {
  21. beforeEach: function() {
  22. captionStream = new m2ts.CaptionStream();
  23. }
  24. });
  25. QUnit.test('parses SEIs messages larger than 255 bytes', function(assert) {
  26. var packets = [], data;
  27. captionStream.ccStreams_[0].push = function(packet) {
  28. packets.push(packet);
  29. };
  30. // set data channel 1 active for field 1
  31. captionStream.activeCea608Channel_[0] = 0;
  32. data = new Uint8Array(268);
  33. data[0] = 0x04; // payload_type === user_data_registered_itu_t_t35
  34. data[1] = 0xff; // payload_size
  35. data[2] = 0x0d; // payload_size
  36. data[3] = 181; // itu_t_t35_country_code
  37. data[4] = 0x00;
  38. data[5] = 0x31; // itu_t_t35_provider_code
  39. data[6] = 0x47;
  40. data[7] = 0x41;
  41. data[8] = 0x39;
  42. data[9] = 0x34; // user_identifier, "GA94"
  43. data[10] = 0x03; // user_data_type_code, 0x03 is cc_data
  44. data[11] = 0xc1; // process_cc_data, cc_count
  45. data[12] = 0xff; // reserved
  46. data[13] = 0xfc; // cc_valid, cc_type (608, field 1)
  47. data[14] = 0xff; // cc_data_1 with parity bit set
  48. data[15] = 0x0e; // cc_data_2 without parity bit set
  49. data[16] = 0xff; // marker_bits
  50. captionStream.push({
  51. nalUnitType: 'sei_rbsp',
  52. escapedRBSP: data
  53. });
  54. captionStream.flush();
  55. assert.equal(packets.length, 1, 'parsed a caption');
  56. });
  57. QUnit.test('parses SEIs containing multiple messages', function(assert) {
  58. var packets = [], data;
  59. captionStream.ccStreams_[0].push = function(packet) {
  60. packets.push(packet);
  61. };
  62. // set data channel 1 active for field 1
  63. captionStream.activeCea608Channel_[0] = 0;
  64. data = new Uint8Array(22);
  65. data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
  66. data[1] = 0x04; // payload_size
  67. data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
  68. data[7] = 0x0d; // payload_size
  69. data[8] = 181; // itu_t_t35_country_code
  70. data[9] = 0x00;
  71. data[10] = 0x31; // itu_t_t35_provider_code
  72. data[11] = 0x47;
  73. data[12] = 0x41;
  74. data[13] = 0x39;
  75. data[14] = 0x34; // user_identifier, "GA94"
  76. data[15] = 0x03; // user_data_type_code, 0x03 is cc_data
  77. data[16] = 0xc1; // process_cc_data, cc_count
  78. data[17] = 0xff; // reserved
  79. data[18] = 0xfc; // cc_valid, cc_type (608, field 1)
  80. data[19] = 0xff; // cc_data_1 with parity bit set
  81. data[20] = 0x0e; // cc_data_2 without parity bit set
  82. data[21] = 0xff; // marker_bits
  83. captionStream.push({
  84. nalUnitType: 'sei_rbsp',
  85. escapedRBSP: data
  86. });
  87. captionStream.flush();
  88. assert.equal(packets.length, 1, 'parsed a caption');
  89. });
  90. QUnit.test('parses SEIs containing multiple messages of type user_data_registered_itu_t_t35', function(assert) {
  91. var packets = [], data;
  92. captionStream.ccStreams_[0].push = function(packet) {
  93. packets.push(packet);
  94. };
  95. // set data channel 1 active for field 1
  96. captionStream.activeCea608Channel_[0] = 0;
  97. data = new Uint8Array(33);
  98. data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
  99. data[1] = 0x04; // payload_size
  100. // https://www.etsi.org/deliver/etsi_ts/101100_101199/101154/01.11.01_60/ts_101154v011101p.pdf#page=117
  101. data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
  102. data[7] = 0x09; // payload_size
  103. data[8] = 181; // itu_t_t35_country_code
  104. data[9] = 0x00;
  105. data[10] = 0x31; // itu_t_t35_provider_code
  106. data[11] = 0x44;
  107. data[12] = 0x54;
  108. data[13] = 0x47;
  109. data[14] = 0x31; // user_identifier, "DTG1"
  110. data[15] = 0x11; // zero_bit (b7), active_format_flag (b6), reserved (b5-b0)
  111. data[16] = 0xF0; // reserved (b7-b4), active_format (b3-b0)
  112. data[17] = 0x04; // payload_type === user_data_registered_itu_t_t35
  113. data[18] = 0x0d; // payload_size
  114. data[19] = 181; // itu_t_t35_country_code
  115. data[20] = 0x00;
  116. data[21] = 0x31; // itu_t_t35_provider_code
  117. data[22] = 0x47;
  118. data[23] = 0x41;
  119. data[24] = 0x39;
  120. data[25] = 0x34; // user_identifier, "GA94"
  121. data[26] = 0x03; // user_data_type_code, 0x03 is cc_data
  122. data[27] = 0xc1; // process_cc_data, cc_count
  123. data[28] = 0xff; // reserved
  124. data[29] = 0xfc; // cc_valid, cc_type (608, field 1)
  125. data[30] = 0xff; // cc_data_1 with parity bit set
  126. data[31] = 0x0e; // cc_data_2 without parity bit set
  127. data[32] = 0xff; // marker_bits
  128. captionStream.push({
  129. nalUnitType: 'sei_rbsp',
  130. escapedRBSP: data
  131. });
  132. captionStream.flush();
  133. assert.equal(packets.length, 1, 'ignored DTG1 payload, parsed a GA94 caption');
  134. });
  135. QUnit.test('does not throw error if only invalid payloads', function(assert) {
  136. var packets = [], data;
  137. captionStream.ccStreams_[0].push = function(packet) {
  138. packets.push(packet);
  139. };
  140. // set data channel 1 active for field 1
  141. captionStream.activeCea608Channel_[0] = 0;
  142. data = new Uint8Array(33);
  143. data[0] = 0x01; // payload_type !== user_data_registered_itu_t_t35
  144. data[1] = 0x04; // payload_size
  145. // https://www.etsi.org/deliver/etsi_ts/101100_101199/101154/01.11.01_60/ts_101154v011101p.pdf#page=117
  146. data[6] = 0x04; // payload_type === user_data_registered_itu_t_t35
  147. data[7] = 0x09; // payload_size
  148. data[8] = 181; // itu_t_t35_country_code
  149. data[9] = 0x00;
  150. data[10] = 0x31; // itu_t_t35_provider_code
  151. data[11] = 0x44;
  152. data[12] = 0x54;
  153. data[13] = 0x47;
  154. data[14] = 0x31; // user_identifier, "DTG1"
  155. data[15] = 0x11; // zero_bit (b7), active_format_flag (b6), reserved (b5-b0)
  156. data[16] = 0xF0; // reserved (b7-b4), active_format (b3-b0)
  157. data[17] = 0x04; // payload_type === user_data_registered_itu_t_t35
  158. data[18] = 0x0d; // payload_size
  159. data[19] = 181; // itu_t_t35_country_code
  160. data[20] = 0x00;
  161. data[21] = 0x31; // itu_t_t35_provider_code
  162. data[22] = 0x47;
  163. data[23] = 0x41;
  164. data[24] = 0x39;
  165. captionStream.push({
  166. nalUnitType: 'sei_rbsp',
  167. escapedRBSP: data
  168. });
  169. captionStream.flush();
  170. assert.equal(packets.length, 0, 'ignored DTG1 payload');
  171. });
  172. QUnit.test('ignores SEIs that do not have type user_data_registered_itu_t_t35', function(assert) {
  173. var captions = [];
  174. captionStream.on('data', function(caption) {
  175. captions.push(caption);
  176. });
  177. captionStream.push({
  178. nalUnitType: 'sei_rbsp',
  179. escapedRBSP: new Uint8Array([
  180. 0x05 // payload_type !== user_data_registered_itu_t_t35
  181. ])
  182. });
  183. assert.equal(captions.length, 0, 'ignored the unknown payload type');
  184. });
  185. QUnit.test('parses a minimal example of caption data', function(assert) {
  186. var packets = [];
  187. captionStream.ccStreams_[0].push = function(packet) {
  188. packets.push(packet);
  189. };
  190. // set data channel 1 active for field 1
  191. captionStream.activeCea608Channel_[0] = 0;
  192. captionStream.push({
  193. nalUnitType: 'sei_rbsp',
  194. escapedRBSP: new Uint8Array([
  195. 0x04, // payload_type === user_data_registered_itu_t_t35
  196. 0x0d, // payload_size
  197. 181, // itu_t_t35_country_code
  198. 0x00, 0x31, // itu_t_t35_provider_code
  199. 0x47, 0x41, 0x39, 0x34, // user_identifier, "GA94"
  200. 0x03, // user_data_type_code, 0x03 is cc_data
  201. // 110 00001
  202. 0xc1, // process_cc_data, cc_count
  203. 0xff, // reserved
  204. // 1111 1100
  205. 0xfc, // cc_valid, cc_type (608, field 1)
  206. 0xff, // cc_data_1 with parity bit set
  207. 0x0e, // cc_data_2 without parity bit set
  208. 0xff // marker_bits
  209. ])
  210. });
  211. captionStream.flush();
  212. assert.equal(packets.length, 1, 'parsed a caption packet');
  213. });
  214. QUnit.test('can be parsed from a segment', function(assert) {
  215. var transmuxer = new mp4.Transmuxer(),
  216. captions = [];
  217. // Setting the BMDT to ensure that captions and id3 tags are not
  218. // time-shifted by this value when they are output and instead are
  219. // zero-based
  220. transmuxer.setBaseMediaDecodeTime(100000);
  221. transmuxer.on('data', function(data) {
  222. if (data.captions) {
  223. captions = captions.concat(data.captions);
  224. }
  225. });
  226. transmuxer.push(sintelCaptions);
  227. transmuxer.flush();
  228. assert.equal(captions.length, 2, 'parsed two captions');
  229. assert.equal(captions[0].text.indexOf('ASUKA'), 0, 'parsed the start of the first caption');
  230. assert.ok(captions[0].text.indexOf('Japanese') > 0, 'parsed the end of the first caption');
  231. assert.equal(captions[0].startTime, 1, 'parsed the start time');
  232. assert.equal(captions[0].endTime, 4, 'parsed the end time');
  233. });
  234. QUnit.test('dispatches caption track information', function(assert) {
  235. var transmuxer = new mp4.Transmuxer(),
  236. captions = [],
  237. captionStreams = {};
  238. // Setting the BMDT to ensure that captions and id3 tags are not
  239. // time-shifted by this value when they are output and instead are
  240. // zero-based
  241. transmuxer.setBaseMediaDecodeTime(100000);
  242. transmuxer.on('data', function(data) {
  243. if (data.captions) {
  244. captions = captions.concat(data.captions);
  245. for (var trackId in data.captionStreams) {
  246. captionStreams[trackId] = true;
  247. }
  248. }
  249. });
  250. transmuxer.push(multiChannel608Captions);
  251. transmuxer.flush();
  252. assert.deepEqual(captionStreams, {CC1: true, CC3: true}, 'found captions in CC1 and CC3');
  253. assert.equal(captions.length, 4, 'parsed eight captions');
  254. assert.equal(captions[0].text, 'être une période de questions', 'parsed the text of the first caption in CC3');
  255. assert.equal(captions[1].text, 'PERIOD, FOLKS.', 'parsed the text of the first caption in CC1');
  256. });
  257. QUnit.test('sorting is fun', function(assert) {
  258. var packets, captions, seiNals;
  259. packets = [
  260. // Send another command so that the second EOC isn't ignored
  261. { pts: 10 * 1000, ccData: 0x1420, type: 0 },
  262. // RCL, resume caption loading
  263. { pts: 1000, ccData: 0x1420, type: 0 },
  264. // 'test string #1'
  265. { pts: 1000, ccData: characters('te'), type: 0 },
  266. { pts: 1000, ccData: characters('st'), type: 0 },
  267. { pts: 1000, ccData: characters(' s'), type: 0 },
  268. // 'test string #2'
  269. { pts: 10 * 1000, ccData: characters('te'), type: 0 },
  270. { pts: 10 * 1000, ccData: characters('st'), type: 0 },
  271. { pts: 10 * 1000, ccData: characters(' s'), type: 0 },
  272. // 'test string #1' continued
  273. { pts: 1000, ccData: characters('tr'), type: 0 },
  274. { pts: 1000, ccData: characters('in'), type: 0 },
  275. { pts: 1000, ccData: characters('g '), type: 0 },
  276. { pts: 1000, ccData: characters('#1'), type: 0 },
  277. // 'test string #2' continued
  278. { pts: 10 * 1000, ccData: characters('tr'), type: 0 },
  279. { pts: 10 * 1000, ccData: characters('in'), type: 0 },
  280. { pts: 10 * 1000, ccData: characters('g '), type: 0 },
  281. { pts: 10 * 1000, ccData: characters('#2'), type: 0 },
  282. // EOC, End of Caption. End display
  283. { pts: 10 * 1000, ccData: 0x142f, type: 0 },
  284. // EOC, End of Caption. Finished transmitting, begin display
  285. { pts: 1000, ccData: 0x142f, type: 0 },
  286. // Send another command so that the second EOC isn't ignored
  287. { pts: 20 * 1000, ccData: 0x1420, type: 0 },
  288. // EOC, End of Caption. End display
  289. { pts: 20 * 1000, ccData: 0x142f, type: 0 }
  290. ];
  291. captions = [];
  292. seiNals = packets.map(makeSeiFromCaptionPacket);
  293. captionStream.on('data', function(caption) {
  294. captions.push(caption);
  295. });
  296. seiNals.forEach(captionStream.push, captionStream);
  297. captionStream.flush();
  298. assert.equal(captions.length, 2, 'detected two captions');
  299. assert.equal(captions[0].text, 'test string #1', 'parsed caption 1');
  300. assert.equal(captions[1].text, 'test string #2', 'parsed caption 2');
  301. });
  302. QUnit.test('drops duplicate segments', function(assert) {
  303. var packets, captions, seiNals;
  304. packets = [
  305. {
  306. pts: 1000, dts: 1000, captions: [
  307. {ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
  308. {ccData: 0x1420, type: 0 }, // RCL, duplicate as per spec
  309. {ccData: characters('te'), type: 0 },
  310. {ccData: characters('st'), type: 0 }
  311. ]
  312. },
  313. {
  314. pts: 2000, dts: 2000, captions: [
  315. {ccData: characters(' s'), type: 0 },
  316. {ccData: characters('tr'), type: 0 },
  317. {ccData: characters('in'), type: 0 }
  318. ]
  319. },
  320. {
  321. pts: 3000, dts: 3000, captions: [
  322. {ccData: characters('g '), type: 0 },
  323. {ccData: characters('da'), type: 0 },
  324. {ccData: characters('ta'), type: 0 }
  325. ]
  326. },
  327. {
  328. pts: 2000, dts: 2000, captions: [
  329. {ccData: characters(' s'), type: 0 },
  330. {ccData: characters('tr'), type: 0 },
  331. {ccData: characters('in'), type: 0 }
  332. ]
  333. },
  334. {
  335. pts: 3000, dts: 3000, captions: [
  336. {ccData: characters('g '), type: 0 },
  337. {ccData: characters('da'), type: 0 },
  338. {ccData: characters('ta'), type: 0 }
  339. ]
  340. },
  341. {
  342. pts: 4000, dts: 4000, captions: [
  343. {ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
  344. {ccData: 0x142f, type: 0 }, // EOC, duplicate as per spec
  345. {ccData: 0x142f, type: 0 }, // EOC, mark display end and flush
  346. {ccData: 0x142f, type: 0 } // EOC, duplicate as per spec
  347. ]
  348. }
  349. ];
  350. captions = [];
  351. seiNals = packets.map(makeSeiFromMultipleCaptionPackets);
  352. captionStream.on('data', function(caption) {
  353. captions.push(caption);
  354. });
  355. seiNals.forEach(captionStream.push, captionStream);
  356. captionStream.flush();
  357. assert.equal(captions.length, 1, 'detected one caption');
  358. assert.equal(captions[0].text, 'test string data', 'parsed caption properly');
  359. });
  360. QUnit.test('drops duplicate segments with multi-segment DTS values', function(assert) {
  361. var packets, captions, seiNals;
  362. packets = [
  363. {
  364. pts: 1000, dts: 1000, captions: [
  365. {ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
  366. {ccData: 0x1420, type: 0 }, // RCL, duplicate as per spec
  367. {ccData: characters('te'), type: 0 }
  368. ]
  369. },
  370. {
  371. pts: 2000, dts: 2000, captions: [
  372. {ccData: characters('st'), type: 0 },
  373. {ccData: characters(' s'), type: 0 }
  374. ]
  375. },
  376. {
  377. pts: 2000, dts: 2000, captions: [
  378. {ccData: characters('tr'), type: 0 },
  379. {ccData: characters('in'), type: 0 }
  380. ]
  381. },
  382. {
  383. pts: 3000, dts: 3000, captions: [
  384. {ccData: characters('g '), type: 0 },
  385. {ccData: characters('da'), type: 0 },
  386. {ccData: characters('ta'), type: 0 }
  387. ]
  388. },
  389. {
  390. pts: 2000, dts: 2000, captions: [
  391. {ccData: characters(' s'), type: 0 },
  392. {ccData: characters('tr'), type: 0 },
  393. {ccData: characters('in'), type: 0 }
  394. ]
  395. },
  396. {
  397. pts: 3000, dts: 3000, captions: [
  398. {ccData: characters('g '), type: 0 },
  399. {ccData: characters('da'), type: 0 },
  400. {ccData: characters('ta'), type: 0 }
  401. ]
  402. },
  403. {
  404. pts: 3000, dts: 3000, captions: [
  405. {ccData: characters(' s'), type: 0 },
  406. {ccData: characters('tu'), type: 0 },
  407. {ccData: characters('ff'), type: 0 }
  408. ]
  409. },
  410. {
  411. pts: 4000, dts: 4000, captions: [
  412. {ccData: 0x142f, type: 0 }, // EOC (end of caption)
  413. // EOC not duplicated for robustness testing
  414. {ccData: 0x1420, type: 0 } // RCL (resume caption loading)
  415. ]
  416. },
  417. {
  418. pts: 5000, dts: 5000, captions: [
  419. {ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
  420. {ccData: characters(' a'), type: 0 },
  421. {ccData: characters('nd'), type: 0 }
  422. ]
  423. },
  424. {
  425. pts: 6000, dts: 6000, captions: [
  426. {ccData: characters(' e'), type: 0 },
  427. {ccData: characters('ve'), type: 0 }
  428. ]
  429. },
  430. {
  431. pts: 6000, dts: 6000, captions: [
  432. {ccData: characters('n '), type: 0 },
  433. {ccData: characters('mo'), type: 0 }
  434. ]
  435. },
  436. {
  437. pts: 6000, dts: 6000, captions: [
  438. {ccData: characters('re'), type: 0 },
  439. {ccData: characters(' t'), type: 0 }
  440. ]
  441. },
  442. {
  443. pts: 5000, dts: 5000, captions: [
  444. {ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
  445. {ccData: characters(' a'), type: 0 },
  446. {ccData: characters('nd'), type: 0 }
  447. ]
  448. },
  449. {
  450. pts: 6000, dts: 6000, captions: [
  451. {ccData: characters(' e'), type: 0 },
  452. {ccData: characters('ve'), type: 0 }
  453. ]
  454. },
  455. {
  456. pts: 6000, dts: 6000, captions: [
  457. {ccData: characters('n '), type: 0 },
  458. {ccData: characters('mo'), type: 0 }
  459. ]
  460. },
  461. {
  462. pts: 6000, dts: 6000, captions: [
  463. {ccData: characters('re'), type: 0 },
  464. {ccData: characters(' t'), type: 0 }
  465. ]
  466. },
  467. {
  468. pts: 6000, dts: 6000, captions: [
  469. {ccData: characters('ex'), type: 0 },
  470. {ccData: characters('t '), type: 0 }
  471. ]
  472. },
  473. {
  474. pts: 6000, dts: 6000, captions: [
  475. {ccData: characters('da'), type: 0 },
  476. {ccData: characters('ta'), type: 0 }
  477. ]
  478. },
  479. {
  480. pts: 7000, dts: 7000, captions: [
  481. {ccData: characters(' h'), type: 0 },
  482. {ccData: characters('er'), type: 0 }
  483. ]
  484. },
  485. {
  486. pts: 8000, dts: 8000, captions: [
  487. {ccData: characters('e!'), type: 0 },
  488. {ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
  489. {ccData: 0x142f, type: 0 }, // EOC, duplicated as per spec
  490. {ccData: 0x142f, type: 0 } // EOC, mark display end and flush
  491. // EOC not duplicated for robustness testing
  492. ]
  493. }
  494. ];
  495. captions = [];
  496. seiNals = packets.map(makeSeiFromMultipleCaptionPackets);
  497. captionStream.on('data', function(caption) {
  498. captions.push(caption);
  499. });
  500. seiNals.forEach(captionStream.push, captionStream);
  501. captionStream.flush();
  502. assert.equal(captions.length, 2, 'detected two captions');
  503. assert.equal(captions[0].text, 'test string data stuff', 'parsed caption properly');
  504. assert.equal(captions[1].text, 'and even more text data here!', 'parsed caption properly');
  505. });
  506. QUnit.test('doesn\'t ignore older segments if reset', function(assert) {
  507. var firstPackets, secondPackets, captions, seiNals1, seiNals2;
  508. firstPackets = [
  509. {
  510. pts: 11000, dts: 11000, captions: [
  511. {ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
  512. {ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
  513. {ccData: characters('te'), type: 0 }
  514. ]
  515. },
  516. {
  517. pts: 12000, dts: 12000, captions: [
  518. {ccData: characters('st'), type: 0 },
  519. {ccData: characters(' s'), type: 0 }
  520. ]
  521. },
  522. {
  523. pts: 12000, dts: 12000, captions: [
  524. {ccData: characters('tr'), type: 0 },
  525. {ccData: characters('in'), type: 0 }
  526. ]
  527. },
  528. {
  529. pts: 13000, dts: 13000, captions: [
  530. {ccData: characters('g '), type: 0 },
  531. {ccData: characters('da'), type: 0 },
  532. {ccData: characters('ta'), type: 0 }
  533. ]
  534. }
  535. ];
  536. secondPackets = [
  537. {
  538. pts: 1000, dts: 1000, captions: [
  539. {ccData: 0x1420, type: 0 }, // RCL (resume caption loading)
  540. {ccData: 0x1420, type: 0 }, // RCL, duplicated as per spec
  541. {ccData: characters('af'), type: 0 }
  542. ]
  543. },
  544. {
  545. pts: 2000, dts: 2000, captions: [
  546. {ccData: characters('te'), type: 0 },
  547. {ccData: characters('r '), type: 0 },
  548. {ccData: characters('re'), type: 0 }
  549. ]
  550. },
  551. {
  552. pts: 3000, dts: 3000, captions: [
  553. {ccData: characters('se'), type: 0 },
  554. {ccData: characters('t '), type: 0 },
  555. {ccData: characters('da'), type: 0 }
  556. ]
  557. },
  558. {
  559. pts: 3000, dts: 3000, captions: [
  560. {ccData: characters('ta'), type: 0 },
  561. {ccData: characters('!!'), type: 0 }
  562. ]
  563. },
  564. {
  565. pts: 4000, dts: 4000, captions: [
  566. {ccData: 0x142f, type: 0 }, // EOC (end of caption), mark display start
  567. {ccData: 0x142f, type: 0 }, // EOC, duplicated as per spec
  568. {ccData: 0x142f, type: 0 } // EOC, mark display end and flush
  569. // EOC not duplicated for robustness testing
  570. ]
  571. }
  572. ];
  573. captions = [];
  574. seiNals1 = firstPackets.map(makeSeiFromMultipleCaptionPackets);
  575. seiNals2 = secondPackets.map(makeSeiFromMultipleCaptionPackets);
  576. captionStream.on('data', function(caption) {
  577. captions.push(caption);
  578. });
  579. seiNals1.forEach(captionStream.push, captionStream);
  580. captionStream.flush();
  581. assert.equal(captionStream.latestDts_, 13000, 'DTS is tracked correctly');
  582. captionStream.reset();
  583. assert.equal(captionStream.latestDts_, null, 'DTS tracking was reset');
  584. seiNals2.forEach(captionStream.push, captionStream);
  585. captionStream.flush();
  586. assert.equal(captionStream.latestDts_, 4000, 'DTS is tracked correctly');
  587. assert.equal(captions.length, 1, 'detected one caption');
  588. assert.equal(captions[0].text, 'after reset data!!', 'parsed caption properly');
  589. });
  590. QUnit.test('extracts all theoretical caption channels', function(assert) {
  591. var captions = [];
  592. captionStream.ccStreams_.forEach(function(cc) {
  593. cc.on('data', function(caption) {
  594. captions.push(caption);
  595. });
  596. });
  597. // RU2 = roll-up, 2 rows
  598. // CR = carriage return
  599. var packets = [
  600. { pts: 1000, type: 0, ccData: 0x1425 }, // RU2 (sets CC1)
  601. { pts: 2000, type: 0, ccData: characters('1a') }, // CC1
  602. { pts: 3000, type: 0, ccData: 0x1c25 }, // RU2 (sets CC2)
  603. { pts: 4000, type: 1, ccData: 0x1525 }, // RU2 (sets CC3)
  604. { pts: 5000, type: 1, ccData: characters('3a') }, // CC3
  605. // this next one tests if active channel is tracked per-field
  606. // instead of globally
  607. { pts: 6000, type: 0, ccData: characters('2a') }, // CC2
  608. { pts: 7000, type: 1, ccData: 0x1d25 }, // RU2 (sets CC4)
  609. { pts: 8000, type: 1, ccData: characters('4a') }, // CC4
  610. { pts: 9000, type: 1, ccData: characters('4b') }, // CC4
  611. { pts: 10000, type: 0, ccData: 0x142d }, // CR (sets + flushes CC1)
  612. { pts: 11000, type: 0, ccData: 0x1c2d }, // CR (sets + flushes CC2)
  613. { pts: 12000, type: 0, ccData: 0x1425 }, // RU2 (sets CC1)
  614. { pts: 13000, type: 0, ccData: characters('1b') }, // CC1
  615. { pts: 14000, type: 0, ccData: characters('1c') }, // CC1
  616. { pts: 15000, type: 0, ccData: 0x142d }, // CR (sets + flushes CC1)
  617. { pts: 16000, type: 1, ccData: 0x152d }, // CR (sets + flushes CC3)
  618. { pts: 17000, type: 1, ccData: 0x1d2d }, // CR (sets + flushes CC4)
  619. { pts: 18000, type: 0, ccData: 0x1c25 }, // RU2 (sets CC2)
  620. { pts: 19000, type: 0, ccData: characters('2b') }, // CC2
  621. { pts: 20000, type: 0, ccData: 0x1c2d } // CR (sets + flushes CC2)
  622. ];
  623. var seiNals = packets.map(makeSeiFromCaptionPacket);
  624. seiNals.forEach(captionStream.push, captionStream);
  625. captionStream.flush();
  626. assert.equal(captions.length, 6, 'got all captions');
  627. assert.equal(captions[0].text, '1a', 'cc1 first row');
  628. assert.equal(captions[1].text, '2a', 'cc2 first row');
  629. assert.equal(captions[2].text, '1a\n1b1c', 'cc1 first and second row');
  630. assert.equal(captions[3].text, '3a', 'cc3 first row');
  631. assert.equal(captions[4].text, '4a4b', 'cc4 first row');
  632. assert.equal(captions[5].text, '2a\n2b', 'cc2 first and second row');
  633. });
  634. QUnit.test('drops data until first command that sets activeChannel for a field', function(assert) {
  635. var captions = [];
  636. captionStream.ccStreams_.forEach(function(cc) {
  637. cc.on('data', function(caption) {
  638. captions.push(caption);
  639. });
  640. });
  641. var packets = [
  642. // test that packets in same field and same data channel are dropped
  643. // before a control code that sets the data channel
  644. { pts: 0 * 1000, ccData: characters('no'), type: 0 },
  645. { pts: 0 * 1000, ccData: characters('t '), type: 0 },
  646. { pts: 0 * 1000, ccData: characters('th'), type: 0 },
  647. { pts: 0 * 1000, ccData: characters('is'), type: 0 },
  648. // EOC (end of caption), sets CC1
  649. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  650. // RCL (resume caption loading)
  651. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  652. // EOC, if data wasn't dropped this would dispatch a caption
  653. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  654. // RCL
  655. { pts: 3 * 1000, ccData: 0x1420, type: 0 },
  656. { pts: 4 * 1000, ccData: characters('fi'), type: 0 },
  657. { pts: 4 * 1000, ccData: characters('el'), type: 0 },
  658. { pts: 4 * 1000, ccData: characters('d0'), type: 0 },
  659. // EOC, mark display start
  660. { pts: 5 * 1000, ccData: 0x142f, type: 0 },
  661. // EOC, duplicated as per spec
  662. { pts: 5 * 1000, ccData: 0x142f, type: 0 },
  663. // EOC, mark display end and flush
  664. { pts: 6 * 1000, ccData: 0x142f, type: 0 },
  665. // EOC not duplicated cuz not necessary
  666. // now switch to field 1 and test that packets in the same field
  667. // but DIFFERENT data channel are dropped
  668. { pts: 7 * 1000, ccData: characters('or'), type: 1 },
  669. { pts: 7 * 1000, ccData: characters(' t'), type: 1 },
  670. { pts: 7 * 1000, ccData: characters('hi'), type: 1 },
  671. { pts: 7 * 1000, ccData: characters('s.'), type: 1 },
  672. // EOC (end of caption, sets CC4)
  673. { pts: 8 * 1000, ccData: 0x1d2f, type: 1 },
  674. // RCL (resume caption loading)
  675. { pts: 8 * 1000, ccData: 0x1d20, type: 1 },
  676. // EOC, if data wasn't dropped this would dispatch a caption
  677. { pts: 9 * 1000, ccData: 0x1d2f, type: 1 },
  678. // RCL
  679. { pts: 10 * 1000, ccData: 0x1d20, type: 1 },
  680. { pts: 11 * 1000, ccData: characters('fi'), type: 1 },
  681. { pts: 11 * 1000, ccData: characters('el'), type: 1 },
  682. { pts: 11 * 1000, ccData: characters('d1'), type: 1 },
  683. // EOC, mark display start
  684. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  685. // EOC, duplicated as per spec
  686. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  687. // EOC, mark display end and flush
  688. { pts: 13 * 1000, ccData: 0x1d2f, type: 1 }
  689. // EOC not duplicated cuz not necessary
  690. ];
  691. var seiNals = packets.map(makeSeiFromCaptionPacket);
  692. seiNals.forEach(captionStream.push, captionStream);
  693. captionStream.flush();
  694. assert.equal(captions.length, 2, 'received 2 captions');
  695. assert.equal(captions[0].text, 'field0', 'received only confirmed field0 data');
  696. assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
  697. assert.equal(captions[1].text, 'field1', 'received only confirmed field1 data');
  698. assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
  699. });
  700. QUnit.test('clears buffer and drops data until first command that sets activeChannel after reset', function(assert) {
  701. var firstPackets, secondPackets, captions, seiNals1, seiNals2;
  702. captions = [];
  703. firstPackets = [
  704. // RCL (resume caption loading), CC1
  705. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  706. { pts: 2 * 1000, ccData: characters('fi'), type: 0 },
  707. { pts: 2 * 1000, ccData: characters('el'), type: 0 },
  708. { pts: 2 * 1000, ccData: characters('d0'), type: 0 },
  709. // EOC (end of caption), swap text to displayed memory
  710. { pts: 3 * 1000, ccData: 0x142f, type: 0 },
  711. { pts: 4 * 1000, ccData: characters('fi'), type: 0 },
  712. { pts: 4 * 1000, ccData: characters('el'), type: 0 },
  713. { pts: 4 * 1000, ccData: characters('d0'), type: 0 },
  714. // RCL (resume caption loading), CC4
  715. { pts: 5 * 1000, ccData: 0x1d20, type: 1 },
  716. { pts: 6 * 1000, ccData: characters('fi'), type: 1 },
  717. { pts: 6 * 1000, ccData: characters('el'), type: 1 },
  718. { pts: 6 * 1000, ccData: characters('d1'), type: 1 },
  719. // EOC (end of caption), swap text to displayed memory
  720. { pts: 7 * 1000, ccData: 0x1d2f, type: 1 },
  721. { pts: 8 * 1000, ccData: characters('fi'), type: 1 },
  722. { pts: 8 * 1000, ccData: characters('el'), type: 1 },
  723. { pts: 8 * 1000, ccData: characters('d1'), type: 1 }
  724. ];
  725. secondPackets = [
  726. // following packets are dropped
  727. { pts: 9 * 1000, ccData: characters('no'), type: 0 },
  728. { pts: 9 * 1000, ccData: characters('t '), type: 0 },
  729. { pts: 9 * 1000, ccData: characters('th'), type: 0 },
  730. { pts: 9 * 1000, ccData: characters('is'), type: 0 },
  731. { pts: 10 * 1000, ccData: characters('or'), type: 1 },
  732. { pts: 10 * 1000, ccData: characters(' t'), type: 1 },
  733. { pts: 10 * 1000, ccData: characters('hi'), type: 1 },
  734. { pts: 10 * 1000, ccData: characters('s.'), type: 1 },
  735. // EOC (end of caption), sets CC1
  736. { pts: 11 * 1000, ccData: 0x142f, type: 0 },
  737. // RCL (resume caption loading), CC1
  738. { pts: 11 * 1000, ccData: 0x1420, type: 0 },
  739. // EOC, sets CC4
  740. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  741. // RCL, CC4
  742. { pts: 12 * 1000, ccData: 0x1d20, type: 1 },
  743. // EOC, CC1, would dispatch caption if packets weren't ignored
  744. { pts: 13 * 1000, ccData: 0x142f, type: 0 },
  745. // RCL, CC1
  746. { pts: 13 * 1000, ccData: 0x1420, type: 0 },
  747. // EOC, CC4, would dispatch caption if packets weren't ignored
  748. { pts: 14 * 1000, ccData: 0x1d2f, type: 1 },
  749. // RCL, CC4
  750. { pts: 14 * 1000, ccData: 0x1d20, type: 1 },
  751. { pts: 18 * 1000, ccData: characters('bu'), type: 0 },
  752. { pts: 18 * 1000, ccData: characters('t '), type: 0 },
  753. { pts: 18 * 1000, ccData: characters('th'), type: 0 },
  754. { pts: 18 * 1000, ccData: characters('is'), type: 0 },
  755. { pts: 19 * 1000, ccData: characters('an'), type: 1 },
  756. { pts: 19 * 1000, ccData: characters('d '), type: 1 },
  757. { pts: 19 * 1000, ccData: characters('th'), type: 1 },
  758. { pts: 19 * 1000, ccData: characters('is'), type: 1 },
  759. // EOC (end of caption), CC1, mark caption 1 start
  760. { pts: 20 * 1000, ccData: 0x142f, type: 0 },
  761. // EOC, CC1, duplicated as per spec
  762. { pts: 20 * 1000, ccData: 0x142f, type: 0 },
  763. // EOC, CC1, mark caption 1 end and dispatch
  764. { pts: 21 * 1000, ccData: 0x142f, type: 0 },
  765. // No duplicate EOC cuz not necessary
  766. // EOC, CC4, mark caption 2 start
  767. { pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
  768. // EOC, CC4, duplicated as per spec
  769. { pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
  770. // EOC, CC4, mark caption 2 end and dispatch
  771. { pts: 23 * 1000, ccData: 0x1d2f, type: 1 }
  772. // No duplicate EOC cuz not necessary
  773. ];
  774. seiNals1 = firstPackets.map(makeSeiFromCaptionPacket);
  775. seiNals2 = secondPackets.map(makeSeiFromCaptionPacket);
  776. captionStream.on('data', function(caption) {
  777. captions.push(caption);
  778. });
  779. seiNals1.forEach(captionStream.push, captionStream);
  780. captionStream.flush();
  781. assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14], 'field0',
  782. 'there is data in non-displayed memory for field 0 before reset');
  783. assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14], 'field1',
  784. 'there is data in non-displayed memory for field 1 before reset');
  785. assert.equal(captionStream.ccStreams_[0].displayed_[14], 'field0',
  786. 'there is data in displayed memory for field 0 before reset');
  787. assert.equal(captionStream.ccStreams_[3].displayed_[14], 'field1',
  788. 'there is data in displayed memory for field 1 before reset');
  789. captionStream.reset();
  790. assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14], '',
  791. 'there is no data in non-displayed memory for field 0 after reset');
  792. assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14], '',
  793. 'there is no data in non-displayed memory for field 1 after reset');
  794. assert.equal(captionStream.ccStreams_[0].displayed_[14], '',
  795. 'there is no data in displayed memory for field 0 after reset');
  796. assert.equal(captionStream.ccStreams_[3].displayed_[14], '',
  797. 'there is no data in displayed memory for field 1 after reset');
  798. seiNals2.forEach(captionStream.push, captionStream);
  799. captionStream.flush();
  800. assert.equal(captions.length, 2, 'detected two captions');
  801. assert.equal(captions[0].text, 'but this', 'parsed caption properly');
  802. assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
  803. assert.equal(captions[1].text, 'and this', 'parsed caption properly');
  804. assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
  805. });
  806. QUnit.test("don't mess up 608 captions when 708 are present", function(assert) {
  807. var captions = [];
  808. captionStream.ccStreams_.forEach(function(cc) {
  809. cc.on('data', function(caption) {
  810. captions.push(caption);
  811. });
  812. });
  813. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  814. seiNals.forEach(captionStream.push, captionStream);
  815. captionStream.flush();
  816. assert.equal(captions.length, 3, 'parsed three captions');
  817. assert.equal(captions[0].text, 'BUT IT\'S NOT SUFFERING\nRIGHW.', 'parsed first caption correctly');
  818. // there is also bad data in the captions, but the null ascii character is removed
  819. assert.equal(captions[1].text, 'IT\'S NOT A THREAT TO ANYBODY.', 'parsed second caption correctly');
  820. assert.equal(captions[2].text, 'WE TRY NOT TO PUT AN ANIMAL DOWN\nIF WE DON\'T HAVE TO.', 'parsed third caption correctly');
  821. });
  822. QUnit.test("both 608 and 708 captions are available by default", function(assert) {
  823. var cc608 = [];
  824. var cc708 = [];
  825. captionStream.on('data', function(caption) {
  826. if (caption.stream === 'CC1') {
  827. cc608.push(caption);
  828. } else {
  829. cc708.push(caption);
  830. }
  831. });
  832. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  833. seiNals.forEach(captionStream.push, captionStream);
  834. captionStream.flush();
  835. assert.equal(cc608.length, 3, 'parsed three 608 cues');
  836. assert.equal(cc708.length, 3, 'parsed three 708 cues');
  837. });
  838. QUnit.test("708 parsing can be turned off", function(assert) {
  839. captionStream.reset();
  840. captionStream = new m2ts.CaptionStream({
  841. parse708captions: false
  842. });
  843. var cc608 = [];
  844. var cc708 = [];
  845. captionStream.on('data', function(caption) {
  846. if (caption.stream === 'CC1') {
  847. cc608.push(caption);
  848. } else {
  849. cc708.push(caption);
  850. }
  851. });
  852. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  853. seiNals.forEach(captionStream.push, captionStream);
  854. captionStream.flush();
  855. assert.equal(cc608.length, 3, 'parsed three 608 cues');
  856. assert.equal(cc708.length, 0, 'did not parse any 708 cues');
  857. });
  858. QUnit.test('ignores XDS and Text packets', function(assert) {
  859. var captions = [];
  860. captionStream.on('data', function(caption) {
  861. captions.push(caption);
  862. });
  863. [
  864. // RCL, resume caption loading, CC3
  865. { pts: 1000, ccData: 0x1520, type: 1 },
  866. { pts: 2000, ccData: characters('hi'), type: 1 },
  867. // EOC, End of Caption
  868. { pts: 3000, ccData: 0x152f, type: 1 },
  869. // Send another command so that the second EOC isn't ignored
  870. { pts: 3000, ccData: 0x152f, type: 1 },
  871. // EOC, End of Caption
  872. { pts: 4000, ccData: 0x152f, type: 1 },
  873. // ENM, Erase Non-Displayed Memory
  874. { pts: 4000, ccData: 0x152e, type: 1 },
  875. // RCL, resume caption loading, CC1
  876. { pts: 5000, ccData: 0x1420, type: 0 }
  877. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  878. captionStream.flush();
  879. assert.equal(captionStream.activeCea608Channel_[0], 0, 'field 1: CC1 is active');
  880. assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
  881. [
  882. // TR, text restart, CC1
  883. { pts: 5000, ccData: 0x142a, type: 0 },
  884. { pts: 6000, ccData: characters('tx'), type: 0 }
  885. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  886. captionStream.flush();
  887. assert.equal(captionStream.activeCea608Channel_[0], null, 'field 1: disabled');
  888. assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
  889. [
  890. // EOC, End of Caption
  891. { pts: 7000, ccData: 0x142f, type: 0 },
  892. // Send another command so that the second EOC isn't ignored
  893. { pts: 7000, ccData: 0x142f, type: 0 },
  894. // EOC, End of Caption
  895. { pts: 8000, ccData: 0x142f, type: 0 },
  896. // RCL, resume caption loading, CC3
  897. { pts: 9000, ccData: 0x1520, type: 1 },
  898. // XDS start, "current" class, program identification number type
  899. { pts: 10000, ccData: 0x0101, type: 1 },
  900. { pts: 11000, ccData: characters('oh'), type: 1 },
  901. // XDS end
  902. { pts: 12000, ccData: 0x0f00, type: 1 }
  903. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  904. captionStream.flush();
  905. assert.equal(captionStream.activeCea608Channel_[0], 0, 'field 1: CC1 is active');
  906. assert.equal(captionStream.activeCea608Channel_[1], null, 'field 2: disabled');
  907. [
  908. // EOC, End of Caption
  909. { pts: 13000, ccData: 0x152f, type: 1 },
  910. // Send another command so that the second EOC isn't ignored
  911. { pts: 13000, ccData: 0x152f, type: 1 }
  912. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  913. captionStream.flush();
  914. assert.equal(captions.length, 1, 'only parsed real caption');
  915. assert.equal(captions[0].text, 'hi', 'caption is correct');
  916. });
  917. // Full character translation tests are below for Cea608Stream, they just only
  918. // test support for CC1. See those tests and the source code for more about the
  919. // mechanics of special and extended characters.
  920. QUnit.test('special and extended character codes work regardless of field and data channel', function(assert) {
  921. var packets, seiNals, captions = [];
  922. packets = [
  923. // RU2 (roll-up, 2 rows), CC2
  924. { ccData: 0x1c25, type: 0 },
  925. // ®
  926. { ccData: 0x1930, type: 0 },
  927. // CR (carriage return), CC2, flush caption
  928. { ccData: 0x1c2d, type: 0 },
  929. // RU2, CC3
  930. { ccData: 0x1525, type: 1 },
  931. // "
  932. { ccData: 0x2200, type: 1 },
  933. // «
  934. { ccData: 0x123e, type: 1 },
  935. // CR, CC3, flush caption
  936. { ccData: 0x152d, type: 1 },
  937. // RU2, CC4
  938. { ccData: 0x1d25, type: 1 },
  939. // "
  940. { ccData: 0x2200, type: 1 },
  941. // »
  942. { ccData: 0x1a3f, type: 1 },
  943. // CR, CC4, flush caption
  944. { ccData: 0x1d2d, type: 1 }
  945. ];
  946. captionStream.on('data', function(caption) {
  947. captions.push(caption);
  948. });
  949. seiNals = packets.map(makeSeiFromCaptionPacket);
  950. seiNals.forEach(captionStream.push, captionStream);
  951. captionStream.flush();
  952. assert.deepEqual(captions[0].text, String.fromCharCode(0xae), 'CC2 special character correct');
  953. assert.deepEqual(captions[1].text, String.fromCharCode(0xab), 'CC3 extended character correct');
  954. assert.deepEqual(captions[2].text, String.fromCharCode(0xbb), 'CC4 extended character correct');
  955. });
  956. QUnit.test('number of roll up rows takes precedence over base row command', function(assert) {
  957. var captions = [];
  958. var packets = [
  959. // RU2 (roll-up, 2 rows), CC1
  960. { type: 0, ccData: 0x1425 },
  961. // RU2, CC1
  962. { type: 0, ccData: 0x1425 },
  963. // PAC: row 1 (sets base row to row 1)
  964. { type: 0, ccData: 0x1170 },
  965. // PAC: row 1
  966. { type: 0, ccData: 0x1170 },
  967. // -
  968. { type: 0, ccData: 0x2d00 },
  969. // CR
  970. { type: 0, ccData: 0x14ad },
  971. // CR
  972. { type: 0, ccData: 0x14ad },
  973. // RU3 (roll-up, 3 rows), CC1
  974. { type: 0, ccData: 0x1426 },
  975. // RU3, CC1
  976. { type: 0, ccData: 0x1426 },
  977. // PAC, row 11
  978. { type: 0, ccData: 0x13d0 },
  979. // PAC, row 11
  980. { type: 0, ccData: 0x13d0 },
  981. // so
  982. { type: 0, ccData: 0x736f },
  983. // CR
  984. { type: 0, ccData: 0x14ad },
  985. // CR
  986. { type: 0, ccData: 0x14ad }
  987. ];
  988. var seis;
  989. captionStream.on('data', function(caption) {
  990. captions.push(caption);
  991. });
  992. seis = packets.map(makeSeiFromCaptionPacket);
  993. seis.forEach(captionStream.push, captionStream);
  994. captionStream.flush();
  995. assert.deepEqual(captions[0].text, '-', 'RU2 caption is correct');
  996. assert.deepEqual(captions[1].text, '-\nso', 'RU3 caption is correct');
  997. packets = [
  998. // switching from row 11 to 0
  999. // PAC: row 0 (sets base row to row 0)
  1000. { type: 0, ccData: 0x1140 },
  1001. // PAC: row 0
  1002. { type: 0, ccData: 0x1140 },
  1003. // CR
  1004. { type: 0, ccData: 0x14ad },
  1005. // CR
  1006. { type: 0, ccData: 0x14ad }
  1007. ];
  1008. seis = packets.map(makeSeiFromCaptionPacket);
  1009. seis.forEach(captionStream.push, captionStream);
  1010. captionStream.flush();
  1011. assert.deepEqual(captions[2].text, '-\nso', 'RU3 caption is correct');
  1012. });
  1013. var cea608Stream;
  1014. QUnit.module('CEA 608 Stream', {
  1015. beforeEach: function() {
  1016. cea608Stream = new m2ts.Cea608Stream();
  1017. }
  1018. });
  1019. QUnit.skip('filters null data', function(assert) {
  1020. assert.ok(false, 'not implemented');
  1021. });
  1022. QUnit.skip('removes parity bits', function(assert) {
  1023. assert.ok(false, 'not implemented');
  1024. });
  1025. QUnit.test('converts non-ASCII character codes to ASCII', function(assert) {
  1026. var packets, captions;
  1027. packets = [
  1028. // RCL, resume caption loading
  1029. { ccData: 0x1420, type: 0 },
  1030. // ASCII exceptions
  1031. { ccData: 0x2a5c, type: 0 },
  1032. { ccData: 0x5e5f, type: 0 },
  1033. { ccData: 0x607b, type: 0 },
  1034. { ccData: 0x7c7d, type: 0 },
  1035. { ccData: 0x7e7f, type: 0 },
  1036. // EOC, End of Caption
  1037. { pts: 1000, ccData: 0x142f, type: 0 },
  1038. // Send another command so that the second EOC isn't ignored
  1039. { ccData: 0x1420, type: 0 },
  1040. // EOC, End of Caption, clear the display
  1041. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1042. ];
  1043. captions = [];
  1044. cea608Stream.on('data', function(caption) {
  1045. captions.push(caption);
  1046. });
  1047. packets.forEach(cea608Stream.push, cea608Stream);
  1048. assert.equal(captions[0].text,
  1049. String.fromCharCode(0xe1, 0xe9, 0xed, 0xf3, 0xfa, 0xe7, 0xf7, 0xd1, 0xf1, 0x2588),
  1050. 'translated non-standard characters');
  1051. });
  1052. QUnit.test('properly handles special character codes', function(assert) {
  1053. var packets, captions;
  1054. packets = [
  1055. // RCL, resume caption loading
  1056. { ccData: 0x1420, type: 0 },
  1057. // Special characters as defined by CEA-608
  1058. // see the CHARACTER_TRANSLATION hash in lib/m2ts/caption-stream for the
  1059. // mapping table
  1060. { ccData: 0x1130, type: 0 },
  1061. { ccData: 0x1131, type: 0 },
  1062. { ccData: 0x1132, type: 0 },
  1063. { ccData: 0x1133, type: 0 },
  1064. { ccData: 0x1134, type: 0 },
  1065. { ccData: 0x1135, type: 0 },
  1066. { ccData: 0x1136, type: 0 },
  1067. { ccData: 0x1137, type: 0 },
  1068. { ccData: 0x1138, type: 0 },
  1069. { ccData: 0x1139, type: 0 },
  1070. { ccData: 0x113a, type: 0 },
  1071. { ccData: 0x113b, type: 0 },
  1072. { ccData: 0x113c, type: 0 },
  1073. { ccData: 0x113d, type: 0 },
  1074. { ccData: 0x113e, type: 0 },
  1075. { ccData: 0x113f, type: 0 },
  1076. // EOC, End of Caption
  1077. { pts: 1000, ccData: 0x142f, type: 0 },
  1078. // Send another command so that the second EOC isn't ignored
  1079. { ccData: 0x1420, type: 0 },
  1080. // EOC, End of Caption, CC1, clear the display
  1081. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1082. ];
  1083. captions = [];
  1084. cea608Stream.on('data', function(caption) {
  1085. captions.push(caption);
  1086. });
  1087. packets.forEach(cea608Stream.push, cea608Stream);
  1088. assert.equal(captions[0].text,
  1089. String.fromCharCode(0xae, 0xb0, 0xbd, 0xbf, 0x2122, 0xa2, 0xa3, 0x266a,
  1090. 0xe0, 0xa0, 0xe8, 0xe2, 0xea, 0xee, 0xf4, 0xfb),
  1091. 'translated special characters');
  1092. });
  1093. QUnit.test('properly handles extended character codes', function(assert) {
  1094. var packets, captions;
  1095. packets = [
  1096. // RCL, resume caption loading
  1097. { ccData: 0x1420, type: 0 },
  1098. // Extended characters are defined in CEA-608 as a standard character,
  1099. // which is followed by an extended character, and the standard character
  1100. // gets deleted.
  1101. // see the CHARACTER_TRANSLATION hash in lib/m2ts/caption-stream for the
  1102. // mapping table
  1103. { ccData: 0x2200, type: 0 },
  1104. { ccData: 0x123e, type: 0 },
  1105. { ccData: 0x4c41, type: 0 },
  1106. { ccData: 0x1230, type: 0 },
  1107. { ccData: 0x2d4c, type: 0 },
  1108. { ccData: 0x4100, type: 0 },
  1109. { ccData: 0x1338, type: 0 },
  1110. { ccData: 0x204c, type: 0 },
  1111. { ccData: 0x417d, type: 0 },
  1112. { ccData: 0x4400, type: 0 },
  1113. { ccData: 0x1137, type: 0 },
  1114. { ccData: 0x2200, type: 0 },
  1115. { ccData: 0x123f, type: 0 },
  1116. // EOC, End of Caption
  1117. { pts: 1000, ccData: 0x142f, type: 0 },
  1118. // Send another command so that the second EOC isn't ignored
  1119. { ccData: 0x1420, type: 0 },
  1120. // EOC, End of Caption, clear the display
  1121. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1122. ];
  1123. captions = [];
  1124. cea608Stream.on('data', function(caption) {
  1125. captions.push(caption);
  1126. });
  1127. packets.forEach(cea608Stream.push, cea608Stream);
  1128. assert.equal(captions[0].text, '«LÀ-LÅ LAÑD♪»',
  1129. 'translated special characters');
  1130. });
  1131. QUnit.test('pop-on mode', function(assert) {
  1132. var packets, captions;
  1133. packets = [
  1134. // RCL, resume caption loading
  1135. { ccData: 0x1420, type: 0 },
  1136. // 'hi'
  1137. { ccData: characters('hi'), type: 0 },
  1138. // EOC, End of Caption. Finished transmitting, begin display
  1139. { pts: 1000, ccData: 0x142f, type: 0 },
  1140. // Send another command so that the second EOC isn't ignored
  1141. { ccData: 0x1420, type: 0 },
  1142. // EOC, End of Caption. End display
  1143. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1144. ];
  1145. captions = [];
  1146. cea608Stream.on('data', function(caption) {
  1147. captions.push(caption);
  1148. });
  1149. packets.forEach(cea608Stream.push, cea608Stream);
  1150. assert.equal(captions.length, 1, 'detected a caption');
  1151. assert.deepEqual(captions[0], {
  1152. startPts: 1000,
  1153. endPts: 10 * 1000,
  1154. text: 'hi',
  1155. stream: 'CC1'
  1156. }, 'parsed the caption');
  1157. });
  1158. QUnit.test('ignores null characters', function(assert) {
  1159. var packets, captions;
  1160. packets = [
  1161. // RCL, resume caption loading
  1162. { ccData: 0x1420, type: 0 },
  1163. // 'mu'
  1164. { ccData: characters('mu'), type: 0 },
  1165. // null characters
  1166. { ccData: 0x0000, type: 0 },
  1167. // ' x'
  1168. { ccData: characters(' x'), type: 0 },
  1169. // EOC, End of Caption. Finished transmitting, begin display
  1170. { pts: 1000, ccData: 0x142f, type: 0 },
  1171. // Send another command so that the second EOC isn't ignored
  1172. { ccData: 0x1420, type: 0 },
  1173. // EOC, End of Caption. End display
  1174. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1175. ];
  1176. captions = [];
  1177. cea608Stream.on('data', function(caption) {
  1178. captions.push(caption);
  1179. });
  1180. packets.forEach(cea608Stream.push, cea608Stream);
  1181. assert.equal(captions.length, 1, 'detected a caption');
  1182. assert.deepEqual(captions[0], {
  1183. startPts: 1000,
  1184. endPts: 10 * 1000,
  1185. text: 'mu x',
  1186. stream: 'CC1'
  1187. }, 'ignored null characters');
  1188. });
  1189. QUnit.test('recognizes the Erase Displayed Memory command', function(assert) {
  1190. var packets, captions;
  1191. packets = [
  1192. // RCL, resume caption loading
  1193. { ccData: 0x1420, type: 0 },
  1194. // '01'
  1195. { ccData: characters('01'), type: 0 },
  1196. // EOC, End of Caption. Finished transmitting, display '01'
  1197. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1198. // EDM, Erase Displayed Memory
  1199. { pts: 1.5 * 1000, ccData: 0x142c, type: 0 },
  1200. // '23'
  1201. { ccData: characters('23'), type: 0 },
  1202. // EOC, End of Caption. Display '23'
  1203. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  1204. // '34'
  1205. { ccData: characters('34'), type: 0 },
  1206. // EOC, End of Caption. Display '34'
  1207. { pts: 3 * 1000, ccData: 0x142f, type: 0 },
  1208. // Send another command so that the second EOC isn't ignored
  1209. { ccData: 0x1420, type: 0},
  1210. // EOC, End of Caption
  1211. { pts: 4 * 1000, ccData: 0x142f, type: 0 }
  1212. ];
  1213. captions = [];
  1214. cea608Stream.on('data', function(caption) {
  1215. captions.push(caption);
  1216. });
  1217. packets.forEach(cea608Stream.push, cea608Stream);
  1218. assert.equal(captions.length, 3, 'detected three captions');
  1219. assert.deepEqual(captions[0], {
  1220. startPts: 1 * 1000,
  1221. endPts: 1.5 * 1000,
  1222. text: '01',
  1223. stream: 'CC1'
  1224. }, 'parsed the first caption');
  1225. assert.deepEqual(captions[1], {
  1226. startPts: 2 * 1000,
  1227. endPts: 3 * 1000,
  1228. text: '23',
  1229. stream: 'CC1'
  1230. }, 'parsed the second caption');
  1231. assert.deepEqual(captions[2], {
  1232. startPts: 3 * 1000,
  1233. endPts: 4 * 1000,
  1234. text: '34',
  1235. stream: 'CC1'
  1236. }, 'parsed the third caption');
  1237. });
  1238. QUnit.test('backspaces are applied to non-displayed memory for pop-on mode', function(assert) {
  1239. var captions = [], packets;
  1240. cea608Stream.on('data', function(caption) {
  1241. captions.push(caption);
  1242. });
  1243. packets = [
  1244. // RCL, resume caption loading
  1245. { ccData: 0x1420, type: 0 },
  1246. // '01'
  1247. { ccData: characters('01'), type: 0 },
  1248. // backspace
  1249. { ccData: 0x1421, type: 0 },
  1250. { ccData: characters('23'), type: 0 },
  1251. // PAC: row 13, no indent
  1252. { pts: 1 * 1000, ccData: 0x1370, type: 0 },
  1253. { pts: 1 * 1000, ccData: characters('32'), type: 0 },
  1254. // backspace
  1255. { pts: 2 * 1000, ccData: 0x1421, type: 0 },
  1256. { pts: 3 * 1000, ccData: characters('10'), type: 0 },
  1257. // EOC, End of Caption
  1258. { pts: 4 * 1000, ccData: 0x142f, type: 0 },
  1259. // Send another command so that the second EOC isn't ignored
  1260. { ccData: 0x1420, type: 0 },
  1261. // EOC, End of Caption, flush caption
  1262. { pts: 5 * 1000, ccData: 0x142f, type: 0 }
  1263. ];
  1264. packets.forEach(cea608Stream.push, cea608Stream);
  1265. assert.equal(captions.length, 1, 'detected a caption');
  1266. assert.equal(captions[0].text, '310\n\n023', 'applied the backspaces');
  1267. });
  1268. QUnit.test('backspaces on cleared memory are no-ops', function(assert) {
  1269. var captions = [], packets;
  1270. cea608Stream.on('data', function(caption) {
  1271. captions.push(caption);
  1272. });
  1273. packets = [
  1274. // RCL, resume caption loading
  1275. { ccData: 0x1420 },
  1276. // backspace
  1277. { ccData: 0x1421 },
  1278. // EOC, End of Caption. Finished transmitting, display '01'
  1279. { pts: 1 * 1000, ccData: 0x142f }
  1280. ];
  1281. packets.forEach(cea608Stream.push, cea608Stream);
  1282. assert.equal(captions.length, 0, 'no captions detected');
  1283. });
  1284. QUnit.test('recognizes the Erase Non-Displayed Memory command', function(assert) {
  1285. var packets, captions;
  1286. packets = [
  1287. // RCL, resume caption loading
  1288. { ccData: 0x1420, type: 0 },
  1289. // '01'
  1290. { ccData: characters('01'), type: 0 },
  1291. // ENM, Erase Non-Displayed Memory
  1292. { ccData: 0x142e, type: 0 },
  1293. { ccData: characters('23'), type: 0 },
  1294. // EOC, End of Caption. Finished transmitting, display '23'
  1295. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1296. // Send another command so that the second EOC isn't ignored
  1297. { ccData: 0x1420, type: 0 },
  1298. // EOC, End of Caption
  1299. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1300. ];
  1301. captions = [];
  1302. packets.forEach(cea608Stream.push, cea608Stream);
  1303. cea608Stream.on('data', function(caption) {
  1304. captions.push(caption);
  1305. });
  1306. packets.forEach(cea608Stream.push, cea608Stream);
  1307. assert.equal(captions.length, 1, 'detected one caption');
  1308. assert.deepEqual(captions[0], {
  1309. startPts: 1 * 1000,
  1310. endPts: 2 * 1000,
  1311. text: '23',
  1312. stream: 'CC1'
  1313. }, 'cleared the non-displayed memory');
  1314. });
  1315. QUnit.test('ignores unrecognized commands', function(assert) {
  1316. var packets, captions;
  1317. packets = [
  1318. // RCL, resume caption loading
  1319. { ccData: 0x1420, type: 0 },
  1320. // a row-9 magenta command, which is not supported
  1321. { ccData: 0x1f4c, type: 0 },
  1322. // '01'
  1323. { ccData: characters('01'), type: 0 },
  1324. // EOC, End of Caption
  1325. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1326. // Send another command so that the second EOC isn't ignored
  1327. { ccData: 0x1420, type: 0 },
  1328. // EOC, End of Caption
  1329. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1330. ];
  1331. captions = [];
  1332. cea608Stream.on('data', function(caption) {
  1333. captions.push(caption);
  1334. });
  1335. packets.forEach(cea608Stream.push, cea608Stream);
  1336. assert.equal(captions[0].text, '01', 'skipped the unrecognized commands');
  1337. });
  1338. QUnit.skip('applies preamble address codes', function(assert) {
  1339. assert.ok(false, 'not implemented');
  1340. });
  1341. QUnit.skip('applies mid-row colors', function(assert) {
  1342. assert.ok(false, 'not implemented');
  1343. });
  1344. QUnit.test('applies mid-row underline', function(assert) {
  1345. var captions = [];
  1346. cea608Stream.on('data', function(caption) {
  1347. captions.push(caption);
  1348. });
  1349. var packets = [
  1350. // RU2 (roll-up, 2 rows)
  1351. { ccData: 0x1425, type: 0 },
  1352. { ccData: characters('no'), type: 0 },
  1353. // mid-row, white underline
  1354. { ccData: 0x1121, type: 0 },
  1355. { ccData: characters('ye'), type: 0 },
  1356. { ccData: characters('s.'), type: 0 },
  1357. // CR (carriage return), dispatches caption
  1358. { ccData: 0x142d, type: 0 }
  1359. ];
  1360. packets.forEach(cea608Stream.push, cea608Stream);
  1361. assert.equal(captions[0].text, 'no <u>yes.</u>', 'properly closed by CR');
  1362. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1363. });
  1364. QUnit.test('applies mid-row italics', function(assert) {
  1365. var captions = [];
  1366. cea608Stream.on('data', function(caption) {
  1367. captions.push(caption);
  1368. });
  1369. var packets = [
  1370. // RU2 (roll-up, 2 rows)
  1371. { ccData: 0x1425, type: 0 },
  1372. { ccData: characters('no'), type: 0 },
  1373. // mid-row, italics
  1374. { ccData: 0x112e, type: 0 },
  1375. { ccData: characters('ye'), type: 0 },
  1376. { ccData: characters('s.'), type: 0 },
  1377. // CR (carriage return), dispatches caption
  1378. { ccData: 0x142d, type: 0 }
  1379. ];
  1380. packets.forEach(cea608Stream.push, cea608Stream);
  1381. assert.equal(captions[0].text, 'no <i>yes.</i>', 'properly closed by CR');
  1382. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1383. });
  1384. QUnit.test('applies mid-row italics underline', function(assert) {
  1385. var captions = [];
  1386. cea608Stream.on('data', function(caption) {
  1387. captions.push(caption);
  1388. });
  1389. var packets = [
  1390. // RU2 (roll-up, 2 rows)
  1391. { ccData: 0x1425, type: 0 },
  1392. { ccData: characters('no'), type: 0 },
  1393. // mid-row, italics underline
  1394. { ccData: 0x112f, type: 0 },
  1395. { ccData: characters('ye'), type: 0 },
  1396. { ccData: characters('s.'), type: 0 },
  1397. // CR (carriage return), dispatches caption
  1398. { ccData: 0x142d, type: 0 }
  1399. ];
  1400. packets.forEach(cea608Stream.push, cea608Stream);
  1401. assert.equal(captions[0].text, 'no <i><u>yes.</u></i>', 'properly closed by CR');
  1402. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1403. });
  1404. // NOTE: With the exception of white italics PACs (the following two test
  1405. // cases), PACs only have their underline attribute extracted and used
  1406. QUnit.test('applies PAC underline', function(assert) {
  1407. var captions = [];
  1408. cea608Stream.on('data', function(caption) {
  1409. captions.push(caption);
  1410. });
  1411. var packets = [
  1412. // RU2 (roll-up, 2 rows)
  1413. { ccData: 0x1425, type: 0 },
  1414. // PAC: row 15, white underline
  1415. { ccData: 0x1461, type: 0 },
  1416. { ccData: characters('ye'), type: 0 },
  1417. { ccData: characters('s.'), type: 0 },
  1418. // CR (carriage return), dispatches caption
  1419. { ccData: 0x142d, type: 0 }
  1420. ];
  1421. packets.forEach(cea608Stream.push, cea608Stream);
  1422. assert.equal(captions[0].text, '<u>yes.</u>', 'properly closed by CR');
  1423. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1424. });
  1425. QUnit.test('applies PAC white italics', function(assert) {
  1426. var captions = [];
  1427. cea608Stream.on('data', function(caption) {
  1428. captions.push(caption);
  1429. });
  1430. var packets = [
  1431. // RU2 (roll-up, 2 rows)
  1432. { ccData: 0x1425, type: 0 },
  1433. // PAC: row 15, white italics
  1434. { ccData: 0x146e, type: 0 },
  1435. { ccData: characters('ye'), type: 0 },
  1436. { ccData: characters('s.'), type: 0 },
  1437. // CR (carriage return), dispatches caption
  1438. { ccData: 0x142d, type: 0 }
  1439. ];
  1440. packets.forEach(cea608Stream.push, cea608Stream);
  1441. assert.equal(captions[0].text, '<i>yes.</i>', 'properly closed by CR');
  1442. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1443. });
  1444. QUnit.test('applies PAC white italics underline', function(assert) {
  1445. var captions = [];
  1446. cea608Stream.on('data', function(caption) {
  1447. captions.push(caption);
  1448. });
  1449. var packets = [
  1450. // RU2 (roll-up, 2 rows)
  1451. { ccData: 0x1425, type: 0 },
  1452. // PAC: row 15, white italics underline
  1453. { ccData: 0x146f, type: 0 },
  1454. { ccData: characters('ye'), type: 0 },
  1455. { ccData: characters('s.'), type: 0 },
  1456. // CR (carriage return), dispatches caption
  1457. { ccData: 0x142d, type: 0 }
  1458. ];
  1459. packets.forEach(cea608Stream.push, cea608Stream);
  1460. assert.equal(captions[0].text, '<u><i>yes.</i></u>', 'properly closed by CR');
  1461. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1462. });
  1463. QUnit.test('closes formatting at PAC row change', function(assert) {
  1464. var captions = [];
  1465. cea608Stream.on('data', function(caption) {
  1466. captions.push(caption);
  1467. });
  1468. var packets = [
  1469. // RCL, resume caption loading
  1470. { ccData: 0x1420, type: 0 },
  1471. // PAC: row 14, white italics underlime
  1472. { ccData: 0x144f, type: 0 },
  1473. { ccData: characters('ye'), type: 0 },
  1474. { ccData: characters('s.'), type: 0 },
  1475. // PAC: row 15, indent 0
  1476. { ccData: 0x1470, type: 0 },
  1477. { ccData: characters('no'), type: 0 },
  1478. // EOC, End of Caption
  1479. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1480. // RCL, resume caption loading
  1481. { ccData: 0x1420, type: 0 },
  1482. // EOC, End of Caption
  1483. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1484. ];
  1485. packets.forEach(cea608Stream.push, cea608Stream);
  1486. assert.equal(captions[0].text, '<u><i>yes.</i></u>\nno', 'properly closed by PAC row change');
  1487. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1488. });
  1489. QUnit.test('closes formatting at EOC', function(assert) {
  1490. var captions = [];
  1491. cea608Stream.on('data', function(caption) {
  1492. captions.push(caption);
  1493. });
  1494. var packets = [
  1495. // RCL, resume caption loading
  1496. { ccData: 0x1420, type: 0 },
  1497. // PAC: row 15, white italics underline
  1498. { ccData: 0x146f, type: 0 },
  1499. { ccData: characters('ye'), type: 0 },
  1500. { ccData: characters('s.'), type: 0 },
  1501. // EOC, End of Caption
  1502. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1503. // Send another command so that the second EOC isn't ignored
  1504. { ccData: 0x1420, type: 0 },
  1505. // EOC, End of Caption
  1506. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1507. ];
  1508. packets.forEach(cea608Stream.push, cea608Stream);
  1509. assert.equal(captions[0].text, '<u><i>yes.</i></u>', 'properly closed by EOC');
  1510. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1511. });
  1512. QUnit.test('closes formatting at negating mid-row code', function(assert) {
  1513. var captions = [];
  1514. cea608Stream.on('data', function(caption) {
  1515. captions.push(caption);
  1516. });
  1517. var packets = [
  1518. // RU2 (roll-up, 2 rows)
  1519. { ccData: 0x1425, type: 0 },
  1520. { ccData: characters('no'), type: 0 },
  1521. // mid-row: italics underline
  1522. { ccData: 0x112f, type: 0 },
  1523. { ccData: characters('ye'), type: 0 },
  1524. { ccData: characters('s.'), type: 0 },
  1525. // mid-row: white
  1526. { ccData: 0x1120, type: 0 },
  1527. { ccData: characters('no'), type: 0 }
  1528. ];
  1529. packets.forEach(cea608Stream.push, cea608Stream);
  1530. cea608Stream.flushDisplayed();
  1531. assert.equal(captions[0].text, 'no <i><u>yes.</u></i> no', 'properly closed by negating mid-row code');
  1532. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1533. });
  1534. QUnit.test('roll-up display mode', function(assert) {
  1535. var captions = [];
  1536. cea608Stream.on('data', function(caption) {
  1537. captions.push(caption);
  1538. });
  1539. [ // RU2, roll-up captions 2 rows
  1540. { ccData: 0x1425, type: 0},
  1541. // '01'
  1542. {
  1543. pts: 1 * 1000,
  1544. ccData: characters('01'),
  1545. type: 0
  1546. },
  1547. // CR, carriage return
  1548. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1549. ].forEach(cea608Stream.push, cea608Stream);
  1550. assert.equal(captions.length, 1, 'detected one caption');
  1551. assert.deepEqual(captions[0], {
  1552. startPts: 0 * 1000,
  1553. endPts: 3 * 1000,
  1554. text: '01',
  1555. stream: 'CC1'
  1556. }, 'parsed the caption');
  1557. captions = [];
  1558. [ // RU4, roll-up captions 4 rows
  1559. { ccdata: 0x1427, type: 0 },
  1560. // '23'
  1561. {
  1562. pts: 4 * 1000,
  1563. ccData: characters('23'),
  1564. type: 0,
  1565. stream: 'CC1'
  1566. },
  1567. // CR
  1568. { pts: 5 * 1000, ccData: 0x142d, type: 0 }
  1569. ].forEach(cea608Stream.push, cea608Stream);
  1570. assert.equal(captions.length, 1, 'detected another caption');
  1571. assert.deepEqual(captions[0], {
  1572. startPts: 3 * 1000,
  1573. endPts: 5 * 1000,
  1574. text: '01\n23',
  1575. stream: 'CC1'
  1576. }, 'parsed the new caption and kept the caption up after the new caption');
  1577. });
  1578. QUnit.test('roll-up displays multiple rows simultaneously', function(assert) {
  1579. var captions = [];
  1580. cea608Stream.on('data', function(caption) {
  1581. captions.push(caption);
  1582. });
  1583. [ // RU2, roll-up captions 2 rows
  1584. { ccData: 0x1425, type: 0 },
  1585. // '01'
  1586. {
  1587. pts: 0 * 1000,
  1588. ccData: characters('01'),
  1589. type: 0,
  1590. stream: 'CC1'
  1591. },
  1592. // CR, carriage return
  1593. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1594. ].forEach(cea608Stream.push, cea608Stream);
  1595. assert.equal(captions.length, 1, 'detected a caption');
  1596. assert.deepEqual(captions[0], {
  1597. startPts: 0 * 1000,
  1598. endPts: 1 * 1000,
  1599. text: '01',
  1600. stream: 'CC1'
  1601. }, 'created a caption for the first period');
  1602. captions = [];
  1603. [ // '23'
  1604. {
  1605. pts: 2 * 1000,
  1606. ccData: characters('23'),
  1607. type: 0,
  1608. stream: 'CC1'
  1609. },
  1610. // CR, carriage return
  1611. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1612. ].forEach(cea608Stream.push, cea608Stream);
  1613. assert.equal(captions.length, 1, 'detected another caption');
  1614. assert.deepEqual(captions[0], {
  1615. startPts: 1 * 1000,
  1616. endPts: 3 * 1000,
  1617. text: '01\n23',
  1618. stream: 'CC1'
  1619. }, 'created the top and bottom rows after the shift up');
  1620. captions = [];
  1621. [ // '45'
  1622. {
  1623. pts: 4 * 1000,
  1624. ccData: characters('45'),
  1625. type: 0,
  1626. stream: 'CC1'
  1627. },
  1628. // CR, carriage return
  1629. { pts: 5 * 1000, ccData: 0x142d, type: 0 }
  1630. ].forEach(cea608Stream.push, cea608Stream);
  1631. assert.equal(captions.length, 1, 'detected third caption');
  1632. assert.deepEqual(captions[0], {
  1633. startPts: 3 * 1000,
  1634. endPts: 5 * 1000,
  1635. text: '23\n45',
  1636. stream: 'CC1'
  1637. }, 'created the top and bottom rows after the shift up');
  1638. });
  1639. QUnit.test('the roll-up count can be changed on-the-fly', function(assert) {
  1640. var captions = [];
  1641. cea608Stream.on('data', function(caption) {
  1642. captions.push(caption);
  1643. });
  1644. [ // RU2, roll-up captions 2 rows
  1645. { ccData: 0x1425, type: 0 },
  1646. // '01'
  1647. {
  1648. pts: 0 * 1000,
  1649. ccData: characters('01'),
  1650. type: 0,
  1651. stream: 'CC1'
  1652. },
  1653. // CR, carriage return
  1654. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1655. ].forEach(cea608Stream.push, cea608Stream);
  1656. captions = [];
  1657. [ // RU3, roll-up captions 3 rows
  1658. { ccData: 0x1426, type: 0 },
  1659. // CR, carriage return
  1660. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  1661. ].forEach(cea608Stream.push, cea608Stream);
  1662. assert.equal(captions.length, 1, 'still displaying a caption');
  1663. captions = [];
  1664. [ // RU4, roll-up captions 4 rows
  1665. { ccData: 0x1427, type: 0 },
  1666. // CR, carriage return
  1667. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1668. ].forEach(cea608Stream.push, cea608Stream);
  1669. assert.equal(captions.length, 1, 'still displaying a caption');
  1670. captions = [];
  1671. // RU3, roll-up captions 3 rows
  1672. cea608Stream.push({ ccdata: 0x1426, type: 0 });
  1673. assert.equal(captions.length, 0, 'cleared the caption');
  1674. });
  1675. QUnit.test('switching to roll-up from pop-on wipes memories and flushes captions', function(assert) {
  1676. var captions = [];
  1677. cea608Stream.on('data', function(caption) {
  1678. captions.push(caption);
  1679. });
  1680. [
  1681. // RCL (resume caption loading)
  1682. { pts: 0 * 1000, ccData: 0x1420, type: 0 },
  1683. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1684. // EOC (end of caption), mark 1st caption start
  1685. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1686. // RCL, resume caption loading
  1687. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  1688. { pts: 2 * 1000, ccData: characters('oh'), type: 0 },
  1689. // EOC, mark 2nd caption start and flush 1st caption
  1690. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  1691. // RU2 (roll-up, 2 rows), flush 2nd caption
  1692. { pts: 3 * 1000, ccData: 0x1425, type: 0 }
  1693. ].forEach(cea608Stream.push, cea608Stream);
  1694. var displayed = cea608Stream.displayed_.reduce(function(acc, val) {
  1695. acc += val;
  1696. return acc;
  1697. });
  1698. var nonDisplayed = cea608Stream.nonDisplayed_.reduce(function(acc, val) {
  1699. acc += val;
  1700. return acc;
  1701. });
  1702. assert.equal(captions.length, 2, 'both captions flushed');
  1703. assert.equal(displayed, '', 'displayed memory is wiped');
  1704. assert.equal(nonDisplayed, '', 'non-displayed memory is wiped');
  1705. assert.deepEqual(captions[0], {
  1706. startPts: 1000,
  1707. endPts: 2000,
  1708. text: 'hi',
  1709. stream: 'CC1'
  1710. }, 'first caption correct');
  1711. assert.deepEqual(captions[1], {
  1712. startPts: 2000,
  1713. endPts: 3000,
  1714. text: 'oh',
  1715. stream: 'CC1'
  1716. }, 'second caption correct');
  1717. });
  1718. QUnit.test('switching to roll-up from paint-on wipes memories and flushes captions', function(assert) {
  1719. var captions = [];
  1720. cea608Stream.on('data', function(caption) {
  1721. captions.push(caption);
  1722. });
  1723. [
  1724. // RDC (resume direct captioning)
  1725. { pts: 0 * 1000, ccData: 0x1429, type: 0 },
  1726. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1727. // RU2 (roll-up, 2 rows), flush displayed caption
  1728. { pts: 1 * 1000, ccData: 0x1425, type: 0 }
  1729. ].forEach(cea608Stream.push, cea608Stream);
  1730. var displayed = cea608Stream.displayed_.reduce(function(acc, val) {
  1731. acc += val;
  1732. return acc;
  1733. });
  1734. var nonDisplayed = cea608Stream.nonDisplayed_.reduce(function(acc, val) {
  1735. acc += val;
  1736. return acc;
  1737. });
  1738. assert.equal(captions.length, 1, 'flushed caption');
  1739. assert.equal(displayed, '', 'displayed memory is wiped');
  1740. assert.equal(nonDisplayed, '', 'non-displayed memory is wiped');
  1741. assert.deepEqual(captions[0], {
  1742. startPts: 0,
  1743. endPts: 1000,
  1744. text: 'hi',
  1745. stream: 'CC1'
  1746. }, 'caption correct');
  1747. });
  1748. // NOTE: This should change to not wiping the display when caption
  1749. // positioning is properly implemented
  1750. QUnit.test('switching to paint-on from pop-on flushes display', function(assert) {
  1751. var captions = [];
  1752. cea608Stream.on('data', function(caption) {
  1753. captions.push(caption);
  1754. });
  1755. [
  1756. // RCL (resume caption loading)
  1757. { pts: 0 * 1000, ccData: 0x1420, type: 0 },
  1758. // PAC: row 14, indent 0
  1759. { pts: 0 * 1000, ccData: 0x1450, type: 0 },
  1760. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1761. // EOC (end of caption), mark caption start
  1762. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1763. // RCL
  1764. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  1765. // RDC (resume direct captioning), flush caption
  1766. { pts: 2 * 1000, ccData: 0x1429, type: 0 },
  1767. // PAC: row 14, indent 0
  1768. { pts: 2 * 1000, ccData: 0x1450, type: 0 },
  1769. // TO1 (tab offset 1 column)
  1770. { pts: 2 * 1000, ccData: 0x1721, type: 0 },
  1771. { pts: 3 * 1000, ccData: characters('io'), type: 0 },
  1772. // EDM (erase displayed memory), flush paint-on caption
  1773. { pts: 4 * 1000, ccData: 0x142c, type: 0 }
  1774. ].forEach(cea608Stream.push, cea608Stream);
  1775. assert.equal(captions.length, 2, 'detected 2 captions');
  1776. assert.equal(captions[0].text, 'hi', 'pop-on caption received');
  1777. assert.equal(captions[0].startPts, 1000, 'proper start pts');
  1778. assert.equal(captions[0].endPts, 2000, 'proper end pts');
  1779. assert.equal(captions[1].text, 'io', 'paint-on caption received');
  1780. assert.equal(captions[1].startPts, 2000, 'proper start pts');
  1781. assert.equal(captions[1].endPts, 4000, 'proper end pts');
  1782. });
  1783. QUnit.test('backspaces are reflected in the generated captions', function(assert) {
  1784. var captions = [];
  1785. cea608Stream.on('data', function(caption) {
  1786. captions.push(caption);
  1787. });
  1788. [ // RU2, roll-up captions 2 rows
  1789. { ccData: 0x1425, type: 0 },
  1790. // '01'
  1791. {
  1792. pts: 0 * 1000,
  1793. ccData: characters('01'),
  1794. type: 0
  1795. },
  1796. // backspace
  1797. { ccData: 0x1421, type: 0 },
  1798. {
  1799. pts: 1 * 1000,
  1800. ccData: characters('23'),
  1801. type: 0
  1802. },
  1803. // CR, carriage return
  1804. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1805. ].forEach(cea608Stream.push, cea608Stream);
  1806. assert.equal(captions.length, 1, 'detected a caption');
  1807. assert.equal(captions[0].text, '023', 'applied the backspace');
  1808. });
  1809. QUnit.test('backspaces can remove a caption entirely', function(assert) {
  1810. var captions = [];
  1811. cea608Stream.on('data', function(caption) {
  1812. captions.push(caption);
  1813. });
  1814. [ // RU2, roll-up captions 2 rows
  1815. { ccData: 0x1425, type: 0 },
  1816. // '01'
  1817. {
  1818. pts: 0 * 1000,
  1819. ccData: characters('01'),
  1820. type: 0
  1821. },
  1822. // backspace
  1823. { ccData: 0x1421, type: 0 },
  1824. // Send another command so that the backspace isn't
  1825. // ignored as a duplicate command
  1826. { ccData: 0x1425, type: 0 },
  1827. // backspace
  1828. { ccData: 0x1421, type: 0 },
  1829. // CR, carriage return
  1830. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1831. ].forEach(cea608Stream.push, cea608Stream);
  1832. assert.equal(captions.length, 0, 'no caption emitted');
  1833. });
  1834. QUnit.test('a second identical control code immediately following the first is ignored', function(assert) {
  1835. var captions = [];
  1836. cea608Stream.on('data', function(caption) {
  1837. captions.push(caption);
  1838. });
  1839. [ // RU2, roll-up captions 2 rows
  1840. { ccData: 0x1425, type: 0 },
  1841. // '01'
  1842. {
  1843. pts: 0 * 1000,
  1844. ccData: characters('01'),
  1845. type: 0
  1846. },
  1847. // '02'
  1848. {
  1849. pts: 1 * 1000,
  1850. ccData: characters('02'),
  1851. type: 0
  1852. },
  1853. // backspace
  1854. { ccData: 0x1421, type: 0 },
  1855. // backspace
  1856. { ccData: 0x1421, type: 0 }, // duplicate is ignored
  1857. // backspace
  1858. { ccData: 0x1421, type: 0 },
  1859. // CR, carriage return
  1860. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  1861. ].forEach(cea608Stream.push, cea608Stream);
  1862. assert.equal(captions.length, 1, 'caption emitted');
  1863. assert.equal(captions[0].text, '01', 'only two backspaces processed');
  1864. });
  1865. QUnit.test('a second identical control code separated by only padding from the first is ignored', function(assert) {
  1866. var captions = [];
  1867. cea608Stream.on('data', function(caption) {
  1868. captions.push(caption);
  1869. });
  1870. [ // RU2, roll-up captions 2 rows
  1871. { ccData: 0x1425, type: 0 },
  1872. // '01'
  1873. {
  1874. pts: 0 * 1000,
  1875. ccData: characters('01'),
  1876. type: 0
  1877. },
  1878. // '02'
  1879. {
  1880. pts: 1 * 1000,
  1881. ccData: characters('02'),
  1882. type: 0
  1883. },
  1884. // backspace
  1885. { ccData: 0x1421, type: 0 },
  1886. // padding
  1887. { ccData: 0x0000, type: 0 },
  1888. { ccData: 0x0000, type: 0 },
  1889. { ccData: 0x0000, type: 0 },
  1890. // backspace
  1891. { pts: 2 * 1000, ccData: 0x1421, type: 0 }, // duplicate is ignored
  1892. // CR, carriage return
  1893. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1894. ].forEach(cea608Stream.push, cea608Stream);
  1895. assert.equal(captions.length, 1, 'caption emitted');
  1896. assert.equal(captions[0].text, '010', 'only one backspace processed');
  1897. });
  1898. QUnit.test('preamble address codes on same row are NOT converted into spaces', function(assert) {
  1899. var captions = [];
  1900. cea608Stream.on('data', function(caption) {
  1901. captions.push(caption);
  1902. });
  1903. [ // RU2, roll-up captions 2 rows
  1904. { ccData: 0x1425, type: 0 },
  1905. // '01'
  1906. {
  1907. pts: 0 * 1000,
  1908. ccData: characters('01'),
  1909. type: 0
  1910. },
  1911. // PAC: row 15, indent 0
  1912. { ccData: 0x1470, type: 0 },
  1913. // '02'
  1914. {
  1915. pts: 1 * 1000,
  1916. ccData: characters('02'),
  1917. type: 0
  1918. },
  1919. // CR, carriage return
  1920. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  1921. ].forEach(cea608Stream.push, cea608Stream);
  1922. assert.equal(captions.length, 1, 'caption emitted');
  1923. assert.equal(captions[0].text, '0102', 'PACs were NOT converted to space');
  1924. });
  1925. QUnit.test('preserves newlines from PACs in pop-on mode', function(assert) {
  1926. var captions = [];
  1927. cea608Stream.on('data', function(caption) {
  1928. captions.push(caption);
  1929. });
  1930. [
  1931. // RCL, resume caption loading
  1932. { ccData: 0x1420, type: 0 },
  1933. // ENM, erase non-displayed memory
  1934. { ccData: 0x142e, type: 0 },
  1935. // PAC: row 12, indent 0
  1936. { ccData: 0x1350, type: 0 },
  1937. // text: TEST
  1938. { ccData: 0x5445, type: 0 },
  1939. { ccData: 0x5354, type: 0 },
  1940. // PAC: row 14, indent 0
  1941. { ccData: 0x1450, type: 0 },
  1942. // text: STRING
  1943. { ccData: 0x5354, type: 0 },
  1944. { ccData: 0x5249, type: 0 },
  1945. { ccData: 0x4e47, type: 0 },
  1946. // PAC: row 15, indent 0
  1947. { ccData: 0x1470, type: 0 },
  1948. // text: DATA
  1949. { ccData: 0x4441, type: 0 },
  1950. { ccData: 0x5441, type: 0 },
  1951. // EOC, end of caption
  1952. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1953. // EOC, duplicated as per spec
  1954. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1955. // EOC, dispatch caption
  1956. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1957. ].forEach(cea608Stream.push, cea608Stream);
  1958. assert.equal(captions.length, 1, 'caption emitted');
  1959. assert.equal(captions[0].text, 'TEST\n\nSTRING\nDATA', 'Position PACs were converted to newlines');
  1960. });
  1961. QUnit.test('extracts real-world cc1 and cc3 channels', function(assert) {
  1962. var cea608Stream1 = cea608Stream;
  1963. var cea608Stream3 = new m2ts.Cea608Stream(1, 0);
  1964. var captions = [];
  1965. cea608Stream1.on('data', function(caption) {
  1966. captions.push(caption);
  1967. });
  1968. cea608Stream3.on('data', function(caption) {
  1969. captions.push(caption);
  1970. });
  1971. var packets = [
  1972. { pts: 425316, type: 0, ccData: 5158 }, // RU3
  1973. { pts: 431322, type: 0, ccData: 5165 }, // CR
  1974. { pts: 440331, type: 0, ccData: 4944 }, // position 11,0
  1975. { pts: 443334, type: 0, ccData: 20549 }, // PE
  1976. { pts: 449340, type: 0, ccData: 21065 }, // RI
  1977. { pts: 449340, type: 0, ccData: 0 }, // padding
  1978. { pts: 452343, type: 0, ccData: 20292 }, // OD
  1979. { pts: 458349, type: 0, ccData: 11264 }, // ,
  1980. { pts: 458349, type: 0, ccData: 0 }, // padding
  1981. { pts: 461352, type: 0, ccData: 0 }, // padding
  1982. { pts: 467358, type: 0, ccData: 8192 }, // (space)
  1983. { pts: 467358, type: 0, ccData: 17920 }, // F
  1984. { pts: 470361, type: 0, ccData: 0 }, // padding
  1985. { pts: 476367, type: 0, ccData: 0 }, // padding
  1986. { pts: 476367, type: 0, ccData: 20300 }, // OL
  1987. { pts: 479370, type: 0, ccData: 19283 }, // KS
  1988. { pts: 485376, type: 0, ccData: 0 }, // padding
  1989. { pts: 485376, type: 0, ccData: 11776 }, // .
  1990. { pts: 674565, type: 0, ccData: 5158 }, // RU3
  1991. { pts: 677568, type: 0, ccData: 5165 }, // CR
  1992. { pts: 371262, type: 1, ccData: 5414 }, // RU3
  1993. { pts: 377268, type: 1, ccData: 0 }, // padding
  1994. { pts: 377268, type: 1, ccData: 4944 }, // position 11,0
  1995. { pts: 380271, type: 1, ccData: 0 }, // padding
  1996. { pts: 386277, type: 1, ccData: 4412 }, // ê
  1997. { pts: 386277, type: 1, ccData: 0 }, // padding
  1998. { pts: 389280, type: 1, ccData: 29810 }, // tr
  1999. { pts: 395286, type: 1, ccData: 25888 }, // e(space)
  2000. { pts: 395286, type: 1, ccData: 30062 }, // un
  2001. { pts: 398289, type: 1, ccData: 25888 }, // e(space)
  2002. { pts: 404295, type: 1, ccData: 28764 }, // pé
  2003. { pts: 404295, type: 1, ccData: 29289 }, // ri
  2004. { pts: 407298, type: 1, ccData: 28516 }, // od
  2005. { pts: 413304, type: 1, ccData: 25856 }, // e
  2006. { pts: 413304, type: 1, ccData: 0 }, // padding
  2007. { pts: 443334, type: 1, ccData: 8292 }, // (space)d
  2008. { pts: 449340, type: 1, ccData: 25888 }, // e(space)
  2009. { pts: 449340, type: 1, ccData: 29045 }, // qu
  2010. { pts: 452343, type: 1, ccData: 25971 }, // es
  2011. { pts: 458349, type: 1, ccData: 29801 }, // ti
  2012. { pts: 458349, type: 1, ccData: 28526 }, // on
  2013. { pts: 461352, type: 1, ccData: 29440 }, // s
  2014. { pts: 467358, type: 1, ccData: 5421 }, // CR
  2015. { pts: 467358, type: 1, ccData: 0 }, // padding
  2016. { pts: 470361, type: 1, ccData: 5414 }, // RU3
  2017. { pts: 476367, type: 1, ccData: 0 } // padding
  2018. ];
  2019. packets.forEach(function(packet) {
  2020. cea608Stream1.push(packet);
  2021. cea608Stream3.push(packet);
  2022. });
  2023. var cc1 = {stream: 'CC1', text: 'PERIOD, FOLKS.'};
  2024. var cc3 = {stream: 'CC3', text: 'être une période de questions'};
  2025. assert.equal(captions.length, 2, 'caption emitted');
  2026. assert.equal(captions[0].stream, cc1.stream, 'cc1 stream detected');
  2027. assert.equal(captions[0].text, cc1.text, 'cc1 stream extracted successfully');
  2028. assert.equal(captions[1].stream, cc3.stream, 'cc3 stream detected');
  2029. assert.equal(captions[1].text, cc3.text, 'cc3 stream extracted successfully');
  2030. });
  2031. QUnit.test('backspaces stop at the beginning of the line', function(assert) {
  2032. var captions = [];
  2033. cea608Stream.on('data', function(caption) {
  2034. captions.push(caption);
  2035. });
  2036. [ // RU2, roll-up captions 2 rows
  2037. { ccData: 0x1425, type: 0 },
  2038. // '01'
  2039. {
  2040. pts: 0 * 1000,
  2041. ccData: characters('01'),
  2042. type: 0
  2043. },
  2044. // backspace
  2045. { ccData: 0x1421, type: 0 },
  2046. // Send another command so that the backspace isn't
  2047. // ignored as a duplicate command
  2048. { ccData: 0x1425, type: 0 },
  2049. // backspace
  2050. { ccData: 0x1421, type: 0 },
  2051. // Send another command so that the backspace isn't
  2052. // ignored as a duplicate command
  2053. { ccData: 0x1425, type: 0 },
  2054. // backspace
  2055. { ccData: 0x1421, type: 0 },
  2056. // CR, carriage return
  2057. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  2058. ].forEach(cea608Stream.push, cea608Stream);
  2059. assert.equal(captions.length, 0, 'no caption emitted');
  2060. });
  2061. QUnit.test('reset works', function(assert) {
  2062. var captions = [];
  2063. cea608Stream.on('data', function(caption) {
  2064. captions.push(caption);
  2065. });
  2066. [ // RU2, roll-up captions 2 rows
  2067. { pts: 0, ccData: 0x1425, type: 0 },
  2068. // mid-row: white underline
  2069. { pts: 0, ccData: 0x1121, type: 0 },
  2070. { pts: 0, ccData: characters('01'), type: 0 }
  2071. ].forEach(cea608Stream.push, cea608Stream);
  2072. var buffer = cea608Stream.displayed_.map(function(row) {
  2073. return row.trim();
  2074. }).join('\n')
  2075. .replace(/^\n+|\n+$/g, '');
  2076. assert.equal(buffer, '<u>01', 'buffer is as expected');
  2077. cea608Stream.reset();
  2078. buffer = cea608Stream.displayed_
  2079. .map(function(row) {
  2080. return row.trim();
  2081. })
  2082. .join('\n')
  2083. .replace(/^\n+|\n+$/g, '');
  2084. assert.equal(buffer, '', 'displayed buffer reset successfully');
  2085. assert.equal(cea608Stream.lastControlCode_, null, 'last control code reset successfully');
  2086. assert.deepEqual(cea608Stream.formatting_, [], 'formatting was reset');
  2087. });
  2088. QUnit.test('paint-on mode', function(assert) {
  2089. var packets, captions;
  2090. packets = [
  2091. // RDC, resume direct captioning, begin display
  2092. { pts: 1000, ccData: 0x1429, type: 0 },
  2093. { pts: 2000, ccData: characters('hi'), type: 0 },
  2094. // EDM, erase displayed memory. Finish display, flush caption
  2095. { pts: 3000, ccData: 0x142c, type: 0 }
  2096. ];
  2097. captions = [];
  2098. cea608Stream.on('data', function(caption) {
  2099. captions.push(caption);
  2100. });
  2101. packets.forEach(cea608Stream.push, cea608Stream);
  2102. assert.equal(captions.length, 1, 'detected a caption');
  2103. assert.deepEqual(captions[0], {
  2104. startPts: 1000,
  2105. endPts: 3000,
  2106. text: 'hi',
  2107. stream: 'CC1'
  2108. }, 'parsed the caption');
  2109. });
  2110. QUnit.test('preserves newlines from PACs in paint-on mode', function(assert) {
  2111. var captions = [];
  2112. cea608Stream.on('data', function(caption) {
  2113. captions.push(caption);
  2114. });
  2115. [
  2116. // RDC, resume direct captioning
  2117. { pts: 1000, ccData: 0x1429, type: 0 },
  2118. // PAC: row 12, indent 0
  2119. { pts: 1000, ccData: 0x1350, type: 0 },
  2120. // text: TEST
  2121. { pts: 2000, ccData: 0x5445, type: 0 },
  2122. { pts: 2000, ccData: 0x5354, type: 0 },
  2123. // PAC: row 14, indent 0
  2124. { pts: 3000, ccData: 0x1450, type: 0 },
  2125. // text: STRING
  2126. { pts: 3000, ccData: 0x5354, type: 0 },
  2127. { pts: 4000, ccData: 0x5249, type: 0 },
  2128. { pts: 4000, ccData: 0x4e47, type: 0 },
  2129. // PAC: row 15, indent 0
  2130. { pts: 5000, ccData: 0x1470, type: 0 },
  2131. // text: DATA
  2132. { pts: 5000, ccData: 0x4441, type: 0 },
  2133. { pts: 6000, ccData: 0x5441, type: 0 },
  2134. // EDM, erase displayed memory. Finish display, flush caption
  2135. { pts: 6000, ccData: 0x142c, type: 0 }
  2136. ].forEach(cea608Stream.push, cea608Stream);
  2137. assert.equal(captions.length, 1, 'caption emitted');
  2138. assert.equal(captions[0].text, 'TEST\n\nSTRING\nDATA', 'Position PACs were converted to newlines');
  2139. });
  2140. QUnit.test('backspaces are reflected in the generated captions (paint-on)', function(assert) {
  2141. var captions = [];
  2142. cea608Stream.on('data', function(caption) {
  2143. captions.push(caption);
  2144. });
  2145. [ // RDC, resume direct captioning
  2146. { ccData: 0x1429, type: 0 },
  2147. // '01', default row 15
  2148. { pts: 0 * 1000, ccData: characters('01'), type: 0 },
  2149. // backspace
  2150. { pts: 0 * 1000, ccData: 0x1421, type: 0 },
  2151. { pts: 1 * 1000, ccData: characters('23'), type: 0 },
  2152. // PAC: row 13, indent 0
  2153. { pts: 2 * 1000, ccData: 0x1370, type: 0 },
  2154. { pts: 2 * 1000, ccData: characters('32'), type: 0 },
  2155. // backspace
  2156. { pts: 3 * 1000, ccData: 0x1421, type: 0 },
  2157. { pts: 4 * 1000, ccData: characters('10'), type: 0 },
  2158. // EDM, erase displayed memory, flush caption
  2159. { pts: 5 * 1000, ccData: 0x142c, type: 0 }
  2160. ].forEach(cea608Stream.push, cea608Stream);
  2161. assert.equal(captions.length, 1, 'detected a caption');
  2162. assert.equal(captions[0].text, '310\n\n023', 'applied the backspaces');
  2163. });
  2164. QUnit.test('mix of all modes (extract from CNN)', function(assert) {
  2165. var captions = [];
  2166. cea608Stream.on('data', function(caption) {
  2167. captions.push(caption);
  2168. });
  2169. [
  2170. // RU2 (roll-up, 2 rows)
  2171. { pts: 6675, ccData: 0x1425, type: 0 },
  2172. // CR (carriange return), flush nothing
  2173. { pts: 6675, ccData: 0x142d, type: 0 },
  2174. // PAC: row 2, indent 0
  2175. { pts: 6675, ccData: 0x1170, type: 0 },
  2176. // text: YEAR.
  2177. { pts: 6676, ccData: 0x5945, type: 0 },
  2178. { pts: 6676, ccData: 0x4152, type: 0 },
  2179. { pts: 6676, ccData: 0x2e00, type: 0 },
  2180. // RU2 (roll-up, 2 rows)
  2181. { pts: 6677, ccData: 0x1425, type: 0 },
  2182. // CR (carriange return), flush 1 row
  2183. { pts: 6677, ccData: 0x142d, type: 0 },
  2184. // PAC: row 2, indent 0
  2185. { pts: 6677, ccData: 0x1170, type: 0 },
  2186. // text: GO TO CNNHEROS.COM.
  2187. { pts: 6677, ccData: 0x474f, type: 0 },
  2188. { pts: 6678, ccData: 0x2054, type: 0 },
  2189. { pts: 6678, ccData: 0x4f00, type: 0 },
  2190. { pts: 6678, ccData: 0x2043, type: 0 },
  2191. { pts: 6679, ccData: 0x4e4e, type: 0 },
  2192. { pts: 6679, ccData: 0x4845, type: 0 },
  2193. { pts: 6679, ccData: 0x524f, type: 0 },
  2194. { pts: 6680, ccData: 0x532e, type: 0 },
  2195. { pts: 6680, ccData: 0x434f, type: 0 },
  2196. { pts: 6680, ccData: 0x4d2e, type: 0 },
  2197. // EDM (erase displayed memory), flush 2 displayed roll-up rows
  2198. { pts: 6697, ccData: 0x142c, type: 0 },
  2199. // RDC (resume direct captioning), wipes memories, flushes nothing
  2200. { pts: 6749, ccData: 0x1429, type: 0 },
  2201. // PAC: row 1, indent 0
  2202. { pts: 6750, ccData: 0x1150, type: 0 },
  2203. // text: Did your Senator or Congressman
  2204. { pts: 6750, ccData: 0x4469, type: 0 },
  2205. { pts: 6750, ccData: 0x6420, type: 0 },
  2206. { pts: 6750, ccData: 0x796f, type: 0 },
  2207. { pts: 6751, ccData: 0x7572, type: 0 },
  2208. { pts: 6751, ccData: 0x2053, type: 0 },
  2209. { pts: 6751, ccData: 0x656e, type: 0 },
  2210. { pts: 6752, ccData: 0x6174, type: 0 },
  2211. { pts: 6752, ccData: 0x6f72, type: 0 },
  2212. { pts: 6752, ccData: 0x206f, type: 0 },
  2213. { pts: 6753, ccData: 0x7220, type: 0 },
  2214. { pts: 6753, ccData: 0x436f, type: 0 },
  2215. { pts: 6753, ccData: 0x6e67, type: 0 },
  2216. { pts: 6753, ccData: 0x7265, type: 0 },
  2217. { pts: 6754, ccData: 0x7373, type: 0 },
  2218. { pts: 6754, ccData: 0x6d61, type: 0 },
  2219. { pts: 6754, ccData: 0x6e00, type: 0 },
  2220. // PAC: row 2, indent 0
  2221. { pts: 6755, ccData: 0x1170, type: 0 },
  2222. // TO2 (tab offset 2 columns)
  2223. { pts: 6755, ccData: 0x1722, type: 0 },
  2224. // text: get elected by talking tough
  2225. { pts: 6755, ccData: 0x6765, type: 0 },
  2226. { pts: 6756, ccData: 0x7420, type: 0 },
  2227. { pts: 6756, ccData: 0x656c, type: 0 },
  2228. { pts: 6756, ccData: 0x6563, type: 0 },
  2229. { pts: 6756, ccData: 0x7465, type: 0 },
  2230. { pts: 6757, ccData: 0x6420, type: 0 },
  2231. { pts: 6757, ccData: 0x6279, type: 0 },
  2232. { pts: 6757, ccData: 0x2074, type: 0 },
  2233. { pts: 6758, ccData: 0x616c, type: 0 },
  2234. { pts: 6758, ccData: 0x6b69, type: 0 },
  2235. { pts: 6758, ccData: 0x6e67, type: 0 },
  2236. { pts: 6759, ccData: 0x2074, type: 0 },
  2237. { pts: 6759, ccData: 0x6f75, type: 0 },
  2238. { pts: 6759, ccData: 0x6768, type: 0 },
  2239. // RCL (resume caption loading)
  2240. { pts: 6759, ccData: 0x1420, type: 0 },
  2241. // PAC: row 1, indent 4
  2242. { pts: 6760, ccData: 0x1152, type: 0 },
  2243. // TO1 (tab offset 1 column)
  2244. { pts: 6760, ccData: 0x1721, type: 0 },
  2245. // text: on the national debt?
  2246. { pts: 6760, ccData: 0x6f6e, type: 0 },
  2247. { pts: 6761, ccData: 0x2074, type: 0 },
  2248. { pts: 6761, ccData: 0x6865, type: 0 },
  2249. { pts: 6761, ccData: 0x206e, type: 0 },
  2250. { pts: 6762, ccData: 0x6174, type: 0 },
  2251. { pts: 6762, ccData: 0x696f, type: 0 },
  2252. { pts: 6762, ccData: 0x6e61, type: 0 },
  2253. { pts: 6762, ccData: 0x6c20, type: 0 },
  2254. { pts: 6763, ccData: 0x6465, type: 0 },
  2255. { pts: 6763, ccData: 0x6274, type: 0 },
  2256. { pts: 6763, ccData: 0x3f00, type: 0 },
  2257. // RCL (resume caption loading)
  2258. { pts: 6781, ccData: 0x1420, type: 0 },
  2259. // EDM (erase displayed memory), flush paint-on caption
  2260. { pts: 6781, ccData: 0x142c, type: 0 },
  2261. // EOC (end of caption), mark pop-on caption 1 start
  2262. { pts: 6782, ccData: 0x142f, type: 0 },
  2263. // RCL (resume caption loading)
  2264. { pts: 6782, ccData: 0x1420, type: 0 },
  2265. // PAC: row 1, indent 4
  2266. { pts: 6782, ccData: 0x1152, type: 0 },
  2267. // TO2 (tab offset 2 columns)
  2268. { pts: 6783, ccData: 0x1722, type: 0 },
  2269. // text: Will they stay true
  2270. { pts: 6783, ccData: 0x5769, type: 0 },
  2271. { pts: 6783, ccData: 0x6c6c, type: 0 },
  2272. { pts: 6783, ccData: 0x2074, type: 0 },
  2273. { pts: 6784, ccData: 0x6865, type: 0 },
  2274. { pts: 6784, ccData: 0x7920, type: 0 },
  2275. { pts: 6784, ccData: 0x7374, type: 0 },
  2276. { pts: 6785, ccData: 0x6179, type: 0 },
  2277. { pts: 6785, ccData: 0x2074, type: 0 },
  2278. { pts: 6785, ccData: 0x7275, type: 0 },
  2279. { pts: 6786, ccData: 0x6500, type: 0 },
  2280. // PAC: row 2, indent 8
  2281. { pts: 6786, ccData: 0x1174, type: 0 },
  2282. // text: to their words?
  2283. { pts: 6786, ccData: 0x746f, type: 0 },
  2284. { pts: 6786, ccData: 0x2074, type: 0 },
  2285. { pts: 6787, ccData: 0x6865, type: 0 },
  2286. { pts: 6787, ccData: 0x6972, type: 0 },
  2287. { pts: 6787, ccData: 0x2077, type: 0 },
  2288. { pts: 6788, ccData: 0x6f72, type: 0 },
  2289. { pts: 6788, ccData: 0x6473, type: 0 },
  2290. { pts: 6788, ccData: 0x3f00, type: 0 },
  2291. // RCL (resume caption loading)
  2292. { pts: 6797, ccData: 0x1420, type: 0 },
  2293. // EDM (erase displayed memory), mark pop-on caption 1 end and flush
  2294. { pts: 6797, ccData: 0x142c, type: 0 },
  2295. // EOC (end of caption), mark pop-on caption 2 start, flush nothing
  2296. { pts: 6798, ccData: 0x142f, type: 0 },
  2297. // RCL
  2298. { pts: 6799, ccData: 0x1420, type: 0 },
  2299. // EOC, mark pop-on caption 2 end and flush
  2300. { pts: 6838, ccData: 0x142f, type: 0 },
  2301. // RU2 (roll-up, 2 rows), wipes memories
  2302. { pts: 6841, ccData: 0x1425, type: 0 },
  2303. // CR (carriage return), flush nothing
  2304. { pts: 6841, ccData: 0x142d, type: 0 },
  2305. // PAC: row 2, indent 0
  2306. { pts: 6841, ccData: 0x1170, type: 0 },
  2307. // text: NO MORE SPECULATION, NO MORE
  2308. { pts: 6841, ccData: 0x3e3e, type: 0 },
  2309. { pts: 6841, ccData: 0x3e00, type: 0 },
  2310. { pts: 6842, ccData: 0x204e, type: 0 },
  2311. { pts: 6842, ccData: 0x4f00, type: 0 },
  2312. { pts: 6842, ccData: 0x204d, type: 0 },
  2313. { pts: 6842, ccData: 0x4f52, type: 0 },
  2314. { pts: 6842, ccData: 0x4500, type: 0 },
  2315. { pts: 6842, ccData: 0x2000, type: 0 },
  2316. { pts: 6842, ccData: 0x5350, type: 0 },
  2317. { pts: 6843, ccData: 0x4543, type: 0 },
  2318. { pts: 6843, ccData: 0x554c, type: 0 },
  2319. { pts: 6843, ccData: 0x4154, type: 0 },
  2320. { pts: 6843, ccData: 0x494f, type: 0 },
  2321. { pts: 6843, ccData: 0x4e2c, type: 0 },
  2322. { pts: 6843, ccData: 0x204e, type: 0 },
  2323. { pts: 6843, ccData: 0x4f00, type: 0 },
  2324. { pts: 6843, ccData: 0x204d, type: 0 },
  2325. { pts: 6844, ccData: 0x4f52, type: 0 },
  2326. { pts: 6844, ccData: 0x4500, type: 0 },
  2327. // RU2 (roll-up, two rows)
  2328. { pts: 6844, ccData: 0x1425, type: 0 },
  2329. // CR (carriage return), flush 1 roll-up row
  2330. { pts: 6844, ccData: 0x142d, type: 0 },
  2331. // PAC: row 2, indent 0
  2332. { pts: 6844, ccData: 0x1170, type: 0 },
  2333. // text: RUMORS OR GUESSING GAMES.
  2334. { pts: 6844, ccData: 0x5255, type: 0 },
  2335. { pts: 6844, ccData: 0x4d4f, type: 0 },
  2336. { pts: 6844, ccData: 0x5253, type: 0 },
  2337. { pts: 6844, ccData: 0x204f, type: 0 },
  2338. { pts: 6845, ccData: 0x5200, type: 0 },
  2339. { pts: 6845, ccData: 0x2047, type: 0 },
  2340. { pts: 6845, ccData: 0x5545, type: 0 },
  2341. { pts: 6845, ccData: 0x5353, type: 0 },
  2342. { pts: 6845, ccData: 0x494e, type: 0 },
  2343. { pts: 6845, ccData: 0x4700, type: 0 },
  2344. { pts: 6845, ccData: 0x2047, type: 0 },
  2345. { pts: 6845, ccData: 0x414d, type: 0 },
  2346. { pts: 6845, ccData: 0x4553, type: 0 },
  2347. { pts: 6845, ccData: 0x2e00, type: 0 },
  2348. // RU2 (roll-up, 2 rows)
  2349. { pts: 6846, ccData: 0x1425, type: 0 },
  2350. // CR (carriage return), flush 2 roll-up rows
  2351. { pts: 6846, ccData: 0x142d, type: 0 }
  2352. ].forEach(cea608Stream.push, cea608Stream);
  2353. assert.equal(captions.length, 7, 'detected 7 captions of varying types');
  2354. assert.deepEqual(captions[0], {
  2355. startPts: 6675,
  2356. endPts: 6677,
  2357. text: 'YEAR.',
  2358. stream: 'CC1'
  2359. }, 'parsed the 1st roll-up caption');
  2360. assert.deepEqual(captions[1], {
  2361. startPts: 6677,
  2362. endPts: 6697,
  2363. text: 'YEAR.\nGO TO CNNHEROS.COM.',
  2364. stream: 'CC1'
  2365. }, 'parsed the 2nd roll-up caption');
  2366. assert.deepEqual(captions[2], {
  2367. startPts: 6749,
  2368. endPts: 6781,
  2369. text: 'Did your Senator or Congressman\nget elected by talking tough',
  2370. stream: 'CC1'
  2371. }, 'parsed the paint-on caption');
  2372. assert.deepEqual(captions[3], {
  2373. startPts: 6782,
  2374. endPts: 6797,
  2375. text: 'on the national debt?',
  2376. stream: 'CC1'
  2377. }, 'parsed the 1st pop-on caption');
  2378. assert.deepEqual(captions[4], {
  2379. startPts: 6798,
  2380. endPts: 6838,
  2381. text: 'Will they stay true\nto their words?',
  2382. stream: 'CC1'
  2383. }, 'parsed the 2nd pop-on caption');
  2384. assert.deepEqual(captions[5], {
  2385. startPts: 6841,
  2386. endPts: 6844,
  2387. text: '>>> NO MORE SPECULATION, NO MORE',
  2388. stream: 'CC1'
  2389. }, 'parsed the 3rd roll-up caption');
  2390. assert.deepEqual(captions[6], {
  2391. startPts: 6844,
  2392. endPts: 6846,
  2393. text: '>>> NO MORE SPECULATION, NO MORE\nRUMORS OR GUESSING GAMES.',
  2394. stream: 'CC1'
  2395. }, 'parsed the 4th roll-up caption');
  2396. });
  2397. QUnit.test('Cea608Stream will trigger log on malformed captions', function(assert) {
  2398. var result;
  2399. var logs = [];
  2400. cea608Stream.on('log', function(log) {
  2401. logs.push(log);
  2402. })
  2403. // this will force an exception to happen in flushDisplayed
  2404. cea608Stream.displayed_[0] = undefined;
  2405. try {
  2406. cea608Stream.flushDisplayed();
  2407. result = true;
  2408. } catch (e) {
  2409. result = false;
  2410. }
  2411. assert.ok(
  2412. result,
  2413. 'the function does not throw an exception'
  2414. );
  2415. assert.deepEqual(
  2416. logs,
  2417. [
  2418. {level: 'warn', message: 'Skipping a malformed 608 caption at index 0.'}
  2419. ],
  2420. 'logs were triggered'
  2421. );
  2422. });
  2423. var cea708Stream;
  2424. QUnit.module('CEA 708 Stream', {
  2425. beforeEach: function() {
  2426. cea708Stream = new m2ts.Cea708Stream();
  2427. }
  2428. });
  2429. QUnit.test('Filters encoding values out of captionServices option block', function(assert) {
  2430. var expectedServiceEncodings = {
  2431. SERVICE1: 'euc-kr',
  2432. SERVICE2: 'utf-8',
  2433. };
  2434. cea708Stream = new m2ts.Cea708Stream({
  2435. captionServices: {
  2436. SERVICE1: {
  2437. language: 'kr',
  2438. label: 'Korean',
  2439. encoding: 'euc-kr'
  2440. },
  2441. SERVICE2: {
  2442. language: 'en',
  2443. label: 'English',
  2444. encoding: 'utf-8'
  2445. }
  2446. }
  2447. });
  2448. assert.deepEqual(cea708Stream.serviceEncodings, expectedServiceEncodings, 'filtered encodings correctly');
  2449. });
  2450. QUnit.test('parses 708 captions', function(assert) {
  2451. var captions = [];
  2452. cea708Stream.on('data', function(caption) {
  2453. captions.push(caption);
  2454. });
  2455. cc708PinkUnderscore.forEach(cea708Stream.push, cea708Stream);
  2456. assert.equal(captions.length, 235, 'parsed 235 captions');
  2457. assert.deepEqual(captions[0], {
  2458. startPts: 6723335478,
  2459. endPts: 6723626769,
  2460. text: '\"Pinkalicious_and_Peterrific\"\nis_made_possible_in_part_by:',
  2461. stream: 'cc708_1'
  2462. }, 'parsed first caption correctly');
  2463. assert.deepEqual(captions[1], {
  2464. startPts: 6723740883,
  2465. endPts: 6723945087,
  2466. text: 'GIRL:\nRead_me_the_tale\nof_a_faraway_land.',
  2467. stream: 'cc708_1'
  2468. }, 'parsed second caption correctly');
  2469. assert.deepEqual(captions[2], {
  2470. startPts: 6723948090,
  2471. endPts: 6724200342,
  2472. text: 'Tell_me_of_planets\nwith_oceans_of_sand.',
  2473. stream: 'cc708_1'
  2474. }, 'parsed third caption correctly');
  2475. assert.deepEqual(captions[33], {
  2476. startPts: 6732617751,
  2477. endPts: 6732876009,
  2478. text: '♪_It\'s_a_Pinkalicious_feeling_♪',
  2479. stream: 'cc708_1'
  2480. }, 'parsed caption 33 correctly with music note');
  2481. assert.deepEqual(captions[38], {
  2482. startPts: 6734218350,
  2483. endPts: 6734425557,
  2484. text: 'PINKALICIOUS:\n\"Dream_Salon.\"',
  2485. stream: 'cc708_1'
  2486. }, 'parsed caption 38 correctly');
  2487. assert.deepEqual(captions[234], {
  2488. startPts: 6778809897,
  2489. endPts: 6779104191,
  2490. text: 'I_guess_I\'ll_just_have\nto_duck_a_little_bit.',
  2491. stream: 'cc708_1'
  2492. }, 'parsed caption 234 correctly');
  2493. });
  2494. QUnit.test('Decodes multibyte characters if valid encoding option is provided and TextDecoder is supported', function(assert) {
  2495. var captions = [];
  2496. cea708Stream = new m2ts.Cea708Stream({
  2497. captionServices: {
  2498. SERVICE1: {
  2499. encoding: 'euc-kr'
  2500. }
  2501. }
  2502. });
  2503. cea708Stream.on('data', function(caption) {
  2504. captions.push(caption);
  2505. });
  2506. cc708Korean.forEach(cea708Stream.push, cea708Stream);
  2507. cea708Stream.flushDisplayed(4721138662, cea708Stream.services[1]);
  2508. assert.equal(captions.length, 1, 'parsed single caption correctly');
  2509. if (window.TextDecoder) {
  2510. assert.ok(cea708Stream.services[1].textDecoder_, 'TextDecoder created when supported');
  2511. assert.equal(
  2512. captions[0].text,
  2513. '니가 ',
  2514. 'parsed multibyte characters correctly'
  2515. );
  2516. } else {
  2517. assert.notOk(cea708Stream.services[1].textDecoder_, 'TextDecoder not created when unsupported');
  2518. }
  2519. });
  2520. QUnit.test('Creates TextDecoder only if valid encoding value is provided', function(assert) {
  2521. var secondCea708Stream;
  2522. cea708Stream = new m2ts.Cea708Stream({
  2523. captionServices: {
  2524. SERVICE1: {
  2525. encoding: 'euc-kr'
  2526. }
  2527. }
  2528. });
  2529. cc708Korean.forEach(cea708Stream.push, cea708Stream);
  2530. cea708Stream.flushDisplayed(4721138662, cea708Stream.services[1]);
  2531. if (window.TextDecoder) {
  2532. assert.ok(cea708Stream.services[1].textDecoder_, 'TextDecoder created successfully when encoding is valid');
  2533. }
  2534. secondCea708Stream = new m2ts.Cea708Stream({
  2535. captionServices: {
  2536. SERVICE1: {
  2537. encoding: 'invalid'
  2538. }
  2539. }
  2540. });
  2541. cc708Korean.forEach(secondCea708Stream.push, secondCea708Stream);
  2542. secondCea708Stream.flushDisplayed(4721138662, secondCea708Stream.services[1]);
  2543. assert.notOk(secondCea708Stream.services[1].textDecoder_, 'TextDecoder not created when encoding is invalid');
  2544. });
  2545. QUnit.test('reset command', function(assert) {
  2546. var captions = [];
  2547. cea708Stream.on('data', function(caption) {
  2548. captions.push(caption);
  2549. });
  2550. [
  2551. { type: 3, pts: 153315036, ccData: 0x8322 },
  2552. { type: 2, pts: 153315036, ccData: 0x4820 },
  2553. { type: 2, pts: 153315036, ccData: 0x0000 },
  2554. { type: 3, pts: 153318039, ccData: 0xc322 },
  2555. { type: 2, pts: 153318039, ccData: 0x4953 },
  2556. { type: 2, pts: 153318039, ccData: 0x0000 },
  2557. { type: 3, pts: 153387108, ccData: 0x0628 },
  2558. { type: 2, pts: 153387108, ccData: 0x0d90 },
  2559. { type: 2, pts: 153387108, ccData: 0x0503 },
  2560. { type: 2, pts: 153387108, ccData: 0x912a },
  2561. { type: 2, pts: 153387108, ccData: 0x002a },
  2562. { type: 2, pts: 153387108, ccData: 0x0000 },
  2563. { type: 3, pts: 153405126, ccData: 0x4da2 },
  2564. { type: 2, pts: 153405126, ccData: 0x8c0f },
  2565. { type: 2, pts: 153405126, ccData: 0x628c },
  2566. { type: 2, pts: 153405126, ccData: 0x0f31 },
  2567. { type: 2, pts: 153405126, ccData: 0x983b },
  2568. { type: 2, pts: 153405126, ccData: 0x912a },
  2569. { type: 2, pts: 153405126, ccData: 0x8f00 },
  2570. { type: 2, pts: 153405126, ccData: 0x611f },
  2571. { type: 2, pts: 153405126, ccData: 0x002a },
  2572. { type: 2, pts: 153405126, ccData: 0x1090 },
  2573. { type: 2, pts: 153405126, ccData: 0x0503 },
  2574. { type: 3, pts: 153408129, ccData: 0x8a31 },
  2575. { type: 2, pts: 153408129, ccData: 0x9201 },
  2576. { type: 2, pts: 153408129, ccData: 0x983b },
  2577. // RST (Reset command)
  2578. { type: 2, pts: 153408129, ccData: 0x8f00 },
  2579. { type: 2, pts: 153408129, ccData: 0x0000 },
  2580. { type: 2, pts: 153408129, ccData: 0x611f },
  2581. { type: 2, pts: 153408129, ccData: 0x1090 },
  2582. { type: 2, pts: 153408129, ccData: 0x0000 },
  2583. { type: 2, pts: 153408129, ccData: 0x0503 },
  2584. { type: 2, pts: 153408129, ccData: 0x912a },
  2585. { type: 2, pts: 153408129, ccData: 0x0000 },
  2586. { type: 2, pts: 153408129, ccData: 0x9201 },
  2587. { type: 3, pts: 153414135, ccData: 0xc322 },
  2588. { type: 2, pts: 153414135, ccData: 0x434f },
  2589. { type: 2, pts: 153414135, ccData: 0x0000 }
  2590. ].forEach(cea708Stream.push, cea708Stream);
  2591. assert.equal(captions.length, 1, 'parsed 1 caption');
  2592. assert.deepEqual(captions[0], {
  2593. startPts: 153315036,
  2594. endPts: 153408129,
  2595. text: '*\n;',
  2596. stream: 'cc708_1'
  2597. }, 'parsed the caption correctly');
  2598. });
  2599. QUnit.test('windowing', function(assert) {
  2600. var captions = [];
  2601. cea708Stream.on('data', function(caption) {
  2602. captions.push(caption);
  2603. });
  2604. [
  2605. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2606. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2607. { type: 2, pts: 1000, ccData: characters('w0') },
  2608. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2609. { type: 2, pts: 1000, ccData: 0x8100 }, // CW1
  2610. { type: 2, pts: 1000, ccData: characters('w1') },
  2611. { type: 3, pts: 1000, ccData: packetHeader708(2, 3, 1, 4) },
  2612. { type: 2, pts: 1000, ccData: 0x8200 }, // CW2
  2613. { type: 2, pts: 1000, ccData: characters('w2') },
  2614. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2615. { type: 2, pts: 1000, ccData: 0x8300 }, // CW3
  2616. { type: 2, pts: 1000, ccData: characters('w3') },
  2617. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2618. { type: 2, pts: 1000, ccData: 0x8400 }, // CW4
  2619. { type: 2, pts: 1000, ccData: characters('w4') },
  2620. { type: 3, pts: 1000, ccData: packetHeader708(2, 3, 1, 4) },
  2621. { type: 2, pts: 1000, ccData: 0x8500 }, // CW5
  2622. { type: 2, pts: 1000, ccData: characters('w5') },
  2623. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2624. { type: 2, pts: 1000, ccData: 0x8600 }, // CW6
  2625. { type: 2, pts: 1000, ccData: characters('w6') },
  2626. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2627. { type: 2, pts: 1000, ccData: 0x8700 }, // CW7
  2628. { type: 2, pts: 1000, ccData: characters('w7') },
  2629. { type: 3, pts: 2000, ccData: packetHeader708(2, 3, 1, 4) },
  2630. { type: 2, pts: 2000, ccData: 0x8aff }, // HDW (Hide all)
  2631. { type: 2, pts: 2000, ccData: displayWindows708([0]) },
  2632. { type: 3, pts: 3000, ccData: packetHeader708(0, 3, 1, 4) },
  2633. { type: 2, pts: 3000, ccData: 0x8aff }, // HDW (Hide all)
  2634. { type: 2, pts: 3000, ccData: displayWindows708([1]) },
  2635. { type: 3, pts: 4000, ccData: packetHeader708(1, 3, 1, 4) },
  2636. { type: 2, pts: 4000, ccData: 0x8aff }, // HDW (Hide all)
  2637. { type: 2, pts: 4000, ccData: displayWindows708([2, 3]) },
  2638. { type: 3, pts: 5000, ccData: packetHeader708(2, 3, 1, 4) },
  2639. { type: 2, pts: 5000, ccData: 0x8aff }, // HDW (Hide all)
  2640. { type: 2, pts: 5000, ccData: displayWindows708([3, 4]) },
  2641. { type: 3, pts: 6000, ccData: packetHeader708(0, 3, 1, 4) },
  2642. { type: 2, pts: 6000, ccData: 0x8aff }, // HDW (Hide all)
  2643. { type: 2, pts: 6000, ccData: displayWindows708([5, 6, 7]) },
  2644. { type: 3, pts: 7000, ccData: packetHeader708(1, 2, 1, 2) },
  2645. { type: 2, pts: 7000, ccData: 0x8aff }, // HDW (Hide all)
  2646. // Indicate end of last packet
  2647. { type: 3, pts: 8000, ccData: packetHeader708(2, 1, 1, 0) }
  2648. ].forEach(cea708Stream.push, cea708Stream);
  2649. assert.equal(captions.length, 5, 'parsed 5 captions');
  2650. assert.deepEqual(captions[0], {
  2651. startPts: 2000,
  2652. endPts: 3000,
  2653. text: 'w0',
  2654. stream: 'cc708_1'
  2655. }, 'parsed caption 0 correctly');
  2656. assert.deepEqual(captions[1], {
  2657. startPts: 3000,
  2658. endPts: 4000,
  2659. text: 'w1',
  2660. stream: 'cc708_1'
  2661. }, 'parsed caption 1 correctly');
  2662. assert.deepEqual(captions[2], {
  2663. startPts: 4000,
  2664. endPts: 5000,
  2665. text: 'w2\n\nw3',
  2666. stream: 'cc708_1'
  2667. }, 'parsed caption 2 correctly');
  2668. assert.deepEqual(captions[3], {
  2669. startPts: 5000,
  2670. endPts: 6000,
  2671. text: 'w3\n\nw4',
  2672. stream: 'cc708_1'
  2673. }, 'parsed caption 3 correctly');
  2674. assert.deepEqual(captions[4], {
  2675. startPts: 6000,
  2676. endPts: 7000,
  2677. text: 'w5\n\nw6\n\nw7',
  2678. stream: 'cc708_1'
  2679. }, 'parsed caption 4 correctly');
  2680. });
  2681. QUnit.test('backspace', function(assert) {
  2682. var captions = [];
  2683. cea708Stream.on('data', function(caption) {
  2684. captions.push(caption);
  2685. });
  2686. [
  2687. { type: 3, pts: 1000, ccData: packetHeader708(0, 7, 1, 12) },
  2688. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2689. { type: 2, pts: 1000, ccData: characters('ty') },
  2690. { type: 2, pts: 1000, ccData: characters('op') },
  2691. { type: 2, pts: 1000, ccData: 0x0808 }, // BS BS: Backspace twice
  2692. { type: 2, pts: 1000, ccData: characters('po') },
  2693. { type: 2, pts: 1000, ccData: displayWindows708([0]) },
  2694. { type: 3, pts: 2000, ccData: packetHeader708(1, 2, 1, 2) },
  2695. { type: 2, pts: 2000, ccData: 0x8aff },
  2696. // Indicate end of last packet
  2697. { type: 3, pts: 3000, ccData: packetHeader708(2, 1, 1, 0) }
  2698. ].forEach(cea708Stream.push, cea708Stream);
  2699. assert.equal(captions.length, 1, 'parsed 1 caption');
  2700. assert.equal(captions[0].text, 'typo', 'parsed caption with backspaces correctly');
  2701. });
  2702. QUnit.test('extended character set', function(assert) {
  2703. var captions = [];
  2704. cea708Stream.on('data', function(caption) {
  2705. captions.push(caption);
  2706. });
  2707. [
  2708. { type: 3, pts: 1000, ccData: packetHeader708(0, 7, 1, 12) },
  2709. { type: 2, pts: 1000, ccData: displayWindows708([0]) },
  2710. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2711. { type: 2, pts: 1000, ccData: 0x103f }, // Ÿ
  2712. { type: 2, pts: 1000, ccData: 0x1035 }, // •
  2713. { type: 2, pts: 1000, ccData: 0x103f }, // Ÿ
  2714. { type: 2, pts: 1000, ccData: 0x0020 },
  2715. { type: 3, pts: 1000, ccData: packetHeader708(1, 7, 1, 12) },
  2716. { type: 2, pts: 1000, ccData: 0x103c }, // œ
  2717. { type: 2, pts: 1000, ccData: 0x102a }, // Š
  2718. { type: 2, pts: 1000, ccData: 0x1025 }, // …
  2719. { type: 2, pts: 1000, ccData: 0x102a }, // Š
  2720. { type: 2, pts: 1000, ccData: 0x103c }, // œ
  2721. { type: 2, pts: 1000, ccData: 0x0020 },
  2722. { type: 3, pts: 1000, ccData: packetHeader708(2, 5, 1, 8) },
  2723. { type: 2, pts: 1000, ccData: 0x1033 }, // “
  2724. { type: 2, pts: 1000, ccData: 0x103d }, // ℠
  2725. { type: 2, pts: 1000, ccData: 0x1034 }, // ”
  2726. { type: 2, pts: 1000, ccData: 0x1039 }, // ™
  2727. { type: 3, pts: 2000, ccData: packetHeader708(0, 2, 1, 2) },
  2728. { type: 2, pts: 2000, ccData: 0x8aff },
  2729. // Indicate end of last packet
  2730. { type: 3, pts: 3000, ccData: packetHeader708(1, 1, 1, 0) }
  2731. ].forEach(cea708Stream.push, cea708Stream);
  2732. assert.equal(captions.length, 1, 'parsed 1 caption');
  2733. assert.equal(captions[0].text, 'Ÿ•Ÿ œŠ…Šœ “℠”™', 'parsed extended characters correctly');
  2734. });
  2735. QUnit.test('roll up', function(assert) {
  2736. var captions = [];
  2737. cea708Stream.on('data', function(caption) {
  2738. captions.push(caption);
  2739. });
  2740. [
  2741. // Define window with two virtual rows (rowCount = 1)
  2742. { type: 3, pts: 1000, ccData: packetHeader708(0, 4, 1, 6) },
  2743. { type: 2, pts: 1000, ccData: 0x983b },
  2744. { type: 2, pts: 1000, ccData: 0x8f00 },
  2745. { type: 2, pts: 1000, ccData: 0x611f },
  2746. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2747. { type: 2, pts: 1000, ccData: characters('L1') },
  2748. { type: 2, pts: 1000, ccData: 0x0d00 }, // CR
  2749. { type: 3, pts: 2000, ccData: packetHeader708(2, 3, 1, 4) },
  2750. { type: 2, pts: 2000, ccData: characters('L2') },
  2751. { type: 2, pts: 2000, ccData: 0x0d00 }, // CR
  2752. { type: 3, pts: 3000, ccData: packetHeader708(0, 3, 1, 4) },
  2753. { type: 2, pts: 3000, ccData: characters('L3') },
  2754. { type: 2, pts: 3000, ccData: 0x0d00 }, // CR
  2755. { type: 3, pts: 4000, ccData: packetHeader708(1, 3, 1, 4) },
  2756. { type: 2, pts: 4000, ccData: characters('L4') },
  2757. { type: 2, pts: 4000, ccData: 0x0d00 }, // CR
  2758. { type: 3, pts: 5000, ccData: packetHeader708(2, 2, 1, 2) },
  2759. { type: 2, pts: 5000, ccData: 0x8aff },
  2760. // Indicate end of last packet
  2761. { type: 3, pts: 6000, ccData: packetHeader708(0, 1, 1, 0) }
  2762. ].forEach(cea708Stream.push, cea708Stream);
  2763. assert.equal(captions.length, 3, 'parsed 3 captions');
  2764. assert.equal(captions[0].text, 'L1\nL2', 'parsed caption 1 correctly');
  2765. assert.equal(captions[1].text, 'L2\nL3', 'parsed caption 2 correctly');
  2766. assert.equal(captions[2].text, 'L3\nL4', 'parsed caption 3 correctly');
  2767. });