caption-stream.test.js 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  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].content[0].text.indexOf('ASUKA'), 0, 'parsed the start of the first caption');
  230. assert.ok(captions[0].content[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].content[0].text, 'être une période de questions', 'parsed the text of the first caption in CC3');
  255. assert.equal(captions[1].content[0].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].content[0].text, 'test string #1', 'parsed caption 1');
  300. assert.equal(captions[1].content[0].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].content[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].content[0].text, 'test string data stuff', 'parsed caption properly');
  504. assert.equal(captions[1].content[0].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].content[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].content[0].text, '1a', 'cc1 first row');
  628. assert.equal(captions[1].content[0].text, '2a', 'cc2 first row');
  629. assert.equal(captions[2].content[0].text, '1a', 'cc1 first row');
  630. assert.equal(captions[2].content[1].text, '1b1c', 'cc1 second row');
  631. assert.equal(captions[3].content[0].text, '3a', 'cc3 first row');
  632. assert.equal(captions[4].content[0].text, '4a4b', 'cc4 first row');
  633. assert.equal(captions[5].content[0].text, '2a', 'cc2 first row');
  634. assert.equal(captions[5].content[1].text, '2b', 'cc2 second row');
  635. });
  636. QUnit.test('drops data until first command that sets activeChannel for a field', function(assert) {
  637. var captions = [];
  638. captionStream.ccStreams_.forEach(function(cc) {
  639. cc.on('data', function(caption) {
  640. captions.push(caption);
  641. });
  642. });
  643. var packets = [
  644. // test that packets in same field and same data channel are dropped
  645. // before a control code that sets the data channel
  646. { pts: 0 * 1000, ccData: characters('no'), type: 0 },
  647. { pts: 0 * 1000, ccData: characters('t '), type: 0 },
  648. { pts: 0 * 1000, ccData: characters('th'), type: 0 },
  649. { pts: 0 * 1000, ccData: characters('is'), type: 0 },
  650. // EOC (end of caption), sets CC1
  651. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  652. // RCL (resume caption loading)
  653. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  654. // EOC, if data wasn't dropped this would dispatch a caption
  655. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  656. // RCL
  657. { pts: 3 * 1000, ccData: 0x1420, type: 0 },
  658. { pts: 4 * 1000, ccData: characters('fi'), type: 0 },
  659. { pts: 4 * 1000, ccData: characters('el'), type: 0 },
  660. { pts: 4 * 1000, ccData: characters('d0'), type: 0 },
  661. // EOC, mark display start
  662. { pts: 5 * 1000, ccData: 0x142f, type: 0 },
  663. // EOC, duplicated as per spec
  664. { pts: 5 * 1000, ccData: 0x142f, type: 0 },
  665. // EOC, mark display end and flush
  666. { pts: 6 * 1000, ccData: 0x142f, type: 0 },
  667. // EOC not duplicated cuz not necessary
  668. // now switch to field 1 and test that packets in the same field
  669. // but DIFFERENT data channel are dropped
  670. { pts: 7 * 1000, ccData: characters('or'), type: 1 },
  671. { pts: 7 * 1000, ccData: characters(' t'), type: 1 },
  672. { pts: 7 * 1000, ccData: characters('hi'), type: 1 },
  673. { pts: 7 * 1000, ccData: characters('s.'), type: 1 },
  674. // EOC (end of caption, sets CC4)
  675. { pts: 8 * 1000, ccData: 0x1d2f, type: 1 },
  676. // RCL (resume caption loading)
  677. { pts: 8 * 1000, ccData: 0x1d20, type: 1 },
  678. // EOC, if data wasn't dropped this would dispatch a caption
  679. { pts: 9 * 1000, ccData: 0x1d2f, type: 1 },
  680. // RCL
  681. { pts: 10 * 1000, ccData: 0x1d20, type: 1 },
  682. { pts: 11 * 1000, ccData: characters('fi'), type: 1 },
  683. { pts: 11 * 1000, ccData: characters('el'), type: 1 },
  684. { pts: 11 * 1000, ccData: characters('d1'), type: 1 },
  685. // EOC, mark display start
  686. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  687. // EOC, duplicated as per spec
  688. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  689. // EOC, mark display end and flush
  690. { pts: 13 * 1000, ccData: 0x1d2f, type: 1 }
  691. // EOC not duplicated cuz not necessary
  692. ];
  693. var seiNals = packets.map(makeSeiFromCaptionPacket);
  694. seiNals.forEach(captionStream.push, captionStream);
  695. captionStream.flush();
  696. assert.equal(captions.length, 2, 'received 2 captions');
  697. assert.equal(captions[0].content[0].text, 'field0', 'received only confirmed field0 data');
  698. assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
  699. assert.equal(captions[1].content[0].text, 'field1', 'received only confirmed field1 data');
  700. assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
  701. });
  702. QUnit.test('clears buffer and drops data until first command that sets activeChannel after reset', function(assert) {
  703. var firstPackets, secondPackets, captions, seiNals1, seiNals2;
  704. captions = [];
  705. firstPackets = [
  706. // RCL (resume caption loading), CC1
  707. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  708. { pts: 2 * 1000, ccData: characters('fi'), type: 0 },
  709. { pts: 2 * 1000, ccData: characters('el'), type: 0 },
  710. { pts: 2 * 1000, ccData: characters('d0'), type: 0 },
  711. // EOC (end of caption), swap text to displayed memory
  712. { pts: 3 * 1000, ccData: 0x142f, type: 0 },
  713. { pts: 4 * 1000, ccData: characters('fi'), type: 0 },
  714. { pts: 4 * 1000, ccData: characters('el'), type: 0 },
  715. { pts: 4 * 1000, ccData: characters('d0'), type: 0 },
  716. // RCL (resume caption loading), CC4
  717. { pts: 5 * 1000, ccData: 0x1d20, type: 1 },
  718. { pts: 6 * 1000, ccData: characters('fi'), type: 1 },
  719. { pts: 6 * 1000, ccData: characters('el'), type: 1 },
  720. { pts: 6 * 1000, ccData: characters('d1'), type: 1 },
  721. // EOC (end of caption), swap text to displayed memory
  722. { pts: 7 * 1000, ccData: 0x1d2f, type: 1 },
  723. { pts: 8 * 1000, ccData: characters('fi'), type: 1 },
  724. { pts: 8 * 1000, ccData: characters('el'), type: 1 },
  725. { pts: 8 * 1000, ccData: characters('d1'), type: 1 }
  726. ];
  727. secondPackets = [
  728. // following packets are dropped
  729. { pts: 9 * 1000, ccData: characters('no'), type: 0 },
  730. { pts: 9 * 1000, ccData: characters('t '), type: 0 },
  731. { pts: 9 * 1000, ccData: characters('th'), type: 0 },
  732. { pts: 9 * 1000, ccData: characters('is'), type: 0 },
  733. { pts: 10 * 1000, ccData: characters('or'), type: 1 },
  734. { pts: 10 * 1000, ccData: characters(' t'), type: 1 },
  735. { pts: 10 * 1000, ccData: characters('hi'), type: 1 },
  736. { pts: 10 * 1000, ccData: characters('s.'), type: 1 },
  737. // EOC (end of caption), sets CC1
  738. { pts: 11 * 1000, ccData: 0x142f, type: 0 },
  739. // RCL (resume caption loading), CC1
  740. { pts: 11 * 1000, ccData: 0x1420, type: 0 },
  741. // EOC, sets CC4
  742. { pts: 12 * 1000, ccData: 0x1d2f, type: 1 },
  743. // RCL, CC4
  744. { pts: 12 * 1000, ccData: 0x1d20, type: 1 },
  745. // EOC, CC1, would dispatch caption if packets weren't ignored
  746. { pts: 13 * 1000, ccData: 0x142f, type: 0 },
  747. // RCL, CC1
  748. { pts: 13 * 1000, ccData: 0x1420, type: 0 },
  749. // EOC, CC4, would dispatch caption if packets weren't ignored
  750. { pts: 14 * 1000, ccData: 0x1d2f, type: 1 },
  751. // RCL, CC4
  752. { pts: 14 * 1000, ccData: 0x1d20, type: 1 },
  753. { pts: 18 * 1000, ccData: characters('bu'), type: 0 },
  754. { pts: 18 * 1000, ccData: characters('t '), type: 0 },
  755. { pts: 18 * 1000, ccData: characters('th'), type: 0 },
  756. { pts: 18 * 1000, ccData: characters('is'), type: 0 },
  757. { pts: 19 * 1000, ccData: characters('an'), type: 1 },
  758. { pts: 19 * 1000, ccData: characters('d '), type: 1 },
  759. { pts: 19 * 1000, ccData: characters('th'), type: 1 },
  760. { pts: 19 * 1000, ccData: characters('is'), type: 1 },
  761. // EOC (end of caption), CC1, mark caption 1 start
  762. { pts: 20 * 1000, ccData: 0x142f, type: 0 },
  763. // EOC, CC1, duplicated as per spec
  764. { pts: 20 * 1000, ccData: 0x142f, type: 0 },
  765. // EOC, CC1, mark caption 1 end and dispatch
  766. { pts: 21 * 1000, ccData: 0x142f, type: 0 },
  767. // No duplicate EOC cuz not necessary
  768. // EOC, CC4, mark caption 2 start
  769. { pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
  770. // EOC, CC4, duplicated as per spec
  771. { pts: 22 * 1000, ccData: 0x1d2f, type: 1 },
  772. // EOC, CC4, mark caption 2 end and dispatch
  773. { pts: 23 * 1000, ccData: 0x1d2f, type: 1 }
  774. // No duplicate EOC cuz not necessary
  775. ];
  776. seiNals1 = firstPackets.map(makeSeiFromCaptionPacket);
  777. seiNals2 = secondPackets.map(makeSeiFromCaptionPacket);
  778. captionStream.on('data', function(caption) {
  779. captions.push(caption);
  780. });
  781. seiNals1.forEach(captionStream.push, captionStream);
  782. captionStream.flush();
  783. assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14].text, 'field0',
  784. 'there is data in non-displayed memory for field 0 before reset');
  785. assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14].text, 'field1',
  786. 'there is data in non-displayed memory for field 1 before reset');
  787. assert.equal(captionStream.ccStreams_[0].displayed_[14].text, 'field0',
  788. 'there is data in displayed memory for field 0 before reset');
  789. assert.equal(captionStream.ccStreams_[3].displayed_[14].text, 'field1',
  790. 'there is data in displayed memory for field 1 before reset');
  791. captionStream.reset();
  792. assert.equal(captionStream.ccStreams_[0].nonDisplayed_[14].text, '',
  793. 'there is no data in non-displayed memory for field 0 after reset');
  794. assert.equal(captionStream.ccStreams_[3].nonDisplayed_[14].text, '',
  795. 'there is no data in non-displayed memory for field 1 after reset');
  796. assert.equal(captionStream.ccStreams_[0].displayed_[14].text, '',
  797. 'there is no data in displayed memory for field 0 after reset');
  798. assert.equal(captionStream.ccStreams_[3].displayed_[14].text, '',
  799. 'there is no data in displayed memory for field 1 after reset');
  800. seiNals2.forEach(captionStream.push, captionStream);
  801. captionStream.flush();
  802. assert.equal(captions.length, 2, 'detected two captions');
  803. assert.equal(captions[0].content[0].text, 'but this', 'parsed caption properly');
  804. assert.equal(captions[0].stream, 'CC1', 'caption went to right channel');
  805. assert.equal(captions[1].content[0].text, 'and this', 'parsed caption properly');
  806. assert.equal(captions[1].stream, 'CC4', 'caption went to right channel');
  807. });
  808. QUnit.test("don't mess up 608 captions when 708 are present", function(assert) {
  809. var captions = [];
  810. captionStream.ccStreams_.forEach(function(cc) {
  811. cc.on('data', function(caption) {
  812. captions.push(caption);
  813. });
  814. });
  815. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  816. seiNals.forEach(captionStream.push, captionStream);
  817. captionStream.flush();
  818. assert.equal(captions.length, 3, 'parsed three captions');
  819. // first caption stream
  820. assert.equal(captions[0].content[0].text, 'BUT IT\'S NOT SUFFERING', 'first stream: parsed first content text correctly');
  821. assert.equal(captions[0].content[1].text, 'RIGHW.', 'first stream: parsed second content text correctly');
  822. // there is also bad data in the captions, but the null ascii character is removed
  823. // second caption stream
  824. assert.equal(captions[1].content[0].text, 'IT\'S NOT A THREAT TO ANYBODY.', 'second stream: parsed content text correctly');
  825. // third stream
  826. assert.equal(captions[2].content[0].text, 'WE TRY NOT TO PUT AN ANIMAL DOWN', 'third stream: parsed first content text correctly');
  827. assert.equal(captions[2].content[1].text, 'IF WE DON\'T HAVE TO.', 'third stream: parsed second content text correctly');
  828. });
  829. QUnit.test("both 608 and 708 captions are available by default", function(assert) {
  830. var cc608 = [];
  831. var cc708 = [];
  832. captionStream.on('data', function(caption) {
  833. if (caption.stream === 'CC1') {
  834. cc608.push(caption);
  835. } else {
  836. cc708.push(caption);
  837. }
  838. });
  839. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  840. seiNals.forEach(captionStream.push, captionStream);
  841. captionStream.flush();
  842. assert.equal(cc608.length, 3, 'parsed three 608 cues');
  843. assert.equal(cc708.length, 3, 'parsed three 708 cues');
  844. });
  845. QUnit.test("708 parsing can be turned off", function(assert) {
  846. captionStream.reset();
  847. captionStream = new m2ts.CaptionStream({
  848. parse708captions: false
  849. });
  850. var cc608 = [];
  851. var cc708 = [];
  852. captionStream.on('data', function(caption) {
  853. if (caption.stream === 'CC1') {
  854. cc608.push(caption);
  855. } else {
  856. cc708.push(caption);
  857. }
  858. });
  859. var seiNals = mixed608708Captions.map(makeSeiFromCaptionPacket);
  860. seiNals.forEach(captionStream.push, captionStream);
  861. captionStream.flush();
  862. assert.equal(cc608.length, 3, 'parsed three 608 cues');
  863. assert.equal(cc708.length, 0, 'did not parse any 708 cues');
  864. });
  865. QUnit.test('ignores XDS and Text packets', function(assert) {
  866. var captions = [];
  867. captionStream.on('data', function(caption) {
  868. captions.push(caption);
  869. });
  870. [
  871. // RCL, resume caption loading, CC3
  872. { pts: 1000, ccData: 0x1520, type: 1 },
  873. { pts: 2000, ccData: characters('hi'), type: 1 },
  874. // EOC, End of Caption
  875. { pts: 3000, ccData: 0x152f, type: 1 },
  876. // Send another command so that the second EOC isn't ignored
  877. { pts: 3000, ccData: 0x152f, type: 1 },
  878. // EOC, End of Caption
  879. { pts: 4000, ccData: 0x152f, type: 1 },
  880. // ENM, Erase Non-Displayed Memory
  881. { pts: 4000, ccData: 0x152e, type: 1 },
  882. // RCL, resume caption loading, CC1
  883. { pts: 5000, ccData: 0x1420, type: 0 }
  884. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  885. captionStream.flush();
  886. assert.equal(captionStream.activeCea608Channel_[0], 0, 'field 1: CC1 is active');
  887. assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
  888. [
  889. // TR, text restart, CC1
  890. { pts: 5000, ccData: 0x142a, type: 0 },
  891. { pts: 6000, ccData: characters('tx'), type: 0 }
  892. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  893. captionStream.flush();
  894. assert.equal(captionStream.activeCea608Channel_[0], null, 'field 1: disabled');
  895. assert.equal(captionStream.activeCea608Channel_[1], 0, 'field 2: CC3 is active');
  896. [
  897. // EOC, End of Caption
  898. { pts: 7000, ccData: 0x142f, type: 0 },
  899. // Send another command so that the second EOC isn't ignored
  900. { pts: 7000, ccData: 0x142f, type: 0 },
  901. // EOC, End of Caption
  902. { pts: 8000, ccData: 0x142f, type: 0 },
  903. // RCL, resume caption loading, CC3
  904. { pts: 9000, ccData: 0x1520, type: 1 },
  905. // XDS start, "current" class, program identification number type
  906. { pts: 10000, ccData: 0x0101, type: 1 },
  907. { pts: 11000, ccData: characters('oh'), type: 1 },
  908. // XDS end
  909. { pts: 12000, ccData: 0x0f00, type: 1 }
  910. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  911. captionStream.flush();
  912. assert.equal(captionStream.activeCea608Channel_[0], 0, 'field 1: CC1 is active');
  913. assert.equal(captionStream.activeCea608Channel_[1], null, 'field 2: disabled');
  914. [
  915. // EOC, End of Caption
  916. { pts: 13000, ccData: 0x152f, type: 1 },
  917. // Send another command so that the second EOC isn't ignored
  918. { pts: 13000, ccData: 0x152f, type: 1 }
  919. ].map(makeSeiFromCaptionPacket).forEach(captionStream.push, captionStream);
  920. captionStream.flush();
  921. assert.equal(captions.length, 1, 'only parsed real caption');
  922. assert.equal(captions[0].content[0].text, 'hi', 'caption is correct');
  923. });
  924. // Full character translation tests are below for Cea608Stream, they just only
  925. // test support for CC1. See those tests and the source code for more about the
  926. // mechanics of special and extended characters.
  927. QUnit.test('special and extended character codes work regardless of field and data channel', function(assert) {
  928. var packets, seiNals, captions = [];
  929. packets = [
  930. // RU2 (roll-up, 2 rows), CC2
  931. { ccData: 0x1c25, type: 0 },
  932. // ®
  933. { ccData: 0x1930, type: 0 },
  934. // CR (carriage return), CC2, flush caption
  935. { ccData: 0x1c2d, type: 0 },
  936. // RU2, CC3
  937. { ccData: 0x1525, type: 1 },
  938. // "
  939. { ccData: 0x2200, type: 1 },
  940. // «
  941. { ccData: 0x123e, type: 1 },
  942. // CR, CC3, flush caption
  943. { ccData: 0x152d, type: 1 },
  944. // RU2, CC4
  945. { ccData: 0x1d25, type: 1 },
  946. // "
  947. { ccData: 0x2200, type: 1 },
  948. // »
  949. { ccData: 0x1a3f, type: 1 },
  950. // CR, CC4, flush caption
  951. { ccData: 0x1d2d, type: 1 }
  952. ];
  953. captionStream.on('data', function(caption) {
  954. captions.push(caption);
  955. });
  956. seiNals = packets.map(makeSeiFromCaptionPacket);
  957. seiNals.forEach(captionStream.push, captionStream);
  958. captionStream.flush();
  959. assert.deepEqual(captions[0].content[0].text, String.fromCharCode(0xae), 'CC2 special character correct');
  960. assert.deepEqual(captions[1].content[0].text, String.fromCharCode(0xab), 'CC3 extended character correct');
  961. assert.deepEqual(captions[2].content[0].text, String.fromCharCode(0xbb), 'CC4 extended character correct');
  962. });
  963. QUnit.test('number of roll up rows takes precedence over base row command', function(assert) {
  964. var captions = [];
  965. var packets = [
  966. // RU2 (roll-up, 2 rows), CC1
  967. { type: 0, ccData: 0x1425 },
  968. // RU2, CC1
  969. { type: 0, ccData: 0x1425 },
  970. // PAC: row 1 (sets base row to row 1)
  971. { type: 0, ccData: 0x1170 },
  972. // PAC: row 1
  973. { type: 0, ccData: 0x1170 },
  974. // -
  975. { type: 0, ccData: 0x2d00 },
  976. // CR
  977. { type: 0, ccData: 0x14ad },
  978. // CR
  979. { type: 0, ccData: 0x14ad },
  980. // RU3 (roll-up, 3 rows), CC1
  981. { type: 0, ccData: 0x1426 },
  982. // RU3, CC1
  983. { type: 0, ccData: 0x1426 },
  984. // PAC, row 11
  985. { type: 0, ccData: 0x13d0 },
  986. // PAC, row 11
  987. { type: 0, ccData: 0x13d0 },
  988. // so
  989. { type: 0, ccData: 0x736f },
  990. // CR
  991. { type: 0, ccData: 0x14ad },
  992. // CR
  993. { type: 0, ccData: 0x14ad }
  994. ];
  995. var seis;
  996. captionStream.on('data', function(caption) {
  997. captions.push(caption);
  998. });
  999. seis = packets.map(makeSeiFromCaptionPacket);
  1000. seis.forEach(captionStream.push, captionStream);
  1001. captionStream.flush();
  1002. assert.deepEqual(captions[0].content[0].text, '-', 'RU2 caption is correct');
  1003. assert.deepEqual(captions[1].content[0].text, '-', 'first RU3 caption is correct');
  1004. assert.deepEqual(captions[1].content[1].text, 'so', 'second RU3 caption is correct');
  1005. packets = [
  1006. // switching from row 11 to 0
  1007. // PAC: row 0 (sets base row to row 0)
  1008. { type: 0, ccData: 0x1140 },
  1009. // PAC: row 0
  1010. { type: 0, ccData: 0x1140 },
  1011. // CR
  1012. { type: 0, ccData: 0x14ad },
  1013. // CR
  1014. { type: 0, ccData: 0x14ad }
  1015. ];
  1016. seis = packets.map(makeSeiFromCaptionPacket);
  1017. seis.forEach(captionStream.push, captionStream);
  1018. captionStream.flush();
  1019. assert.deepEqual(captions[2].content[0].text, '-', 'first RU3 caption is correct');
  1020. assert.deepEqual(captions[2].content[1].text, 'so', 'second RU3 caption is correct');
  1021. });
  1022. var cea608Stream;
  1023. QUnit.module('CEA 608 Stream', {
  1024. beforeEach: function() {
  1025. cea608Stream = new m2ts.Cea608Stream();
  1026. }
  1027. });
  1028. QUnit.skip('filters null data', function(assert) {
  1029. assert.ok(false, 'not implemented');
  1030. });
  1031. QUnit.skip('removes parity bits', function(assert) {
  1032. assert.ok(false, 'not implemented');
  1033. });
  1034. QUnit.test('converts non-ASCII character codes to ASCII', function(assert) {
  1035. var packets, captions;
  1036. packets = [
  1037. // RCL, resume caption loading
  1038. { ccData: 0x1420, type: 0 },
  1039. // ASCII exceptions
  1040. { ccData: 0x2a5c, type: 0 },
  1041. { ccData: 0x5e5f, type: 0 },
  1042. { ccData: 0x607b, type: 0 },
  1043. { ccData: 0x7c7d, type: 0 },
  1044. { ccData: 0x7e7f, type: 0 },
  1045. // EOC, End of Caption
  1046. { pts: 1000, ccData: 0x142f, type: 0 },
  1047. // Send another command so that the second EOC isn't ignored
  1048. { ccData: 0x1420, type: 0 },
  1049. // EOC, End of Caption, clear the display
  1050. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1051. ];
  1052. captions = [];
  1053. cea608Stream.on('data', function(caption) {
  1054. captions.push(caption);
  1055. });
  1056. packets.forEach(cea608Stream.push, cea608Stream);
  1057. assert.equal(captions[0].content[0].text,
  1058. String.fromCharCode(0xe1, 0xe9, 0xed, 0xf3, 0xfa, 0xe7, 0xf7, 0xd1, 0xf1, 0x2588),
  1059. 'translated non-standard characters');
  1060. });
  1061. QUnit.test('properly handles special character codes', function(assert) {
  1062. var packets, captions;
  1063. packets = [
  1064. // RCL, resume caption loading
  1065. { ccData: 0x1420, type: 0 },
  1066. // Special characters as defined by CEA-608
  1067. // see the CHARACTER_TRANSLATION hash in lib/m2ts/caption-stream for the
  1068. // mapping table
  1069. { ccData: 0x1130, type: 0 },
  1070. { ccData: 0x1131, type: 0 },
  1071. { ccData: 0x1132, type: 0 },
  1072. { ccData: 0x1133, type: 0 },
  1073. { ccData: 0x1134, type: 0 },
  1074. { ccData: 0x1135, type: 0 },
  1075. { ccData: 0x1136, type: 0 },
  1076. { ccData: 0x1137, type: 0 },
  1077. { ccData: 0x1138, type: 0 },
  1078. { ccData: 0x1139, type: 0 },
  1079. { ccData: 0x113a, type: 0 },
  1080. { ccData: 0x113b, type: 0 },
  1081. { ccData: 0x113c, type: 0 },
  1082. { ccData: 0x113d, type: 0 },
  1083. { ccData: 0x113e, type: 0 },
  1084. { ccData: 0x113f, type: 0 },
  1085. // EOC, End of Caption
  1086. { pts: 1000, ccData: 0x142f, type: 0 },
  1087. // Send another command so that the second EOC isn't ignored
  1088. { ccData: 0x1420, type: 0 },
  1089. // EOC, End of Caption, CC1, clear the display
  1090. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1091. ];
  1092. captions = [];
  1093. cea608Stream.on('data', function(caption) {
  1094. captions.push(caption);
  1095. });
  1096. packets.forEach(cea608Stream.push, cea608Stream);
  1097. assert.equal(captions[0].content[0].text,
  1098. String.fromCharCode(0xae, 0xb0, 0xbd, 0xbf, 0x2122, 0xa2, 0xa3, 0x266a,
  1099. 0xe0, 0xa0, 0xe8, 0xe2, 0xea, 0xee, 0xf4, 0xfb),
  1100. 'translated special characters');
  1101. });
  1102. QUnit.test('properly handles extended character codes', function(assert) {
  1103. var packets, captions;
  1104. packets = [
  1105. // RCL, resume caption loading
  1106. { ccData: 0x1420, type: 0 },
  1107. // Extended characters are defined in CEA-608 as a standard character,
  1108. // which is followed by an extended character, and the standard character
  1109. // gets deleted.
  1110. // see the CHARACTER_TRANSLATION hash in lib/m2ts/caption-stream for the
  1111. // mapping table
  1112. { ccData: 0x2200, type: 0 },
  1113. { ccData: 0x123e, type: 0 },
  1114. { ccData: 0x4c41, type: 0 },
  1115. { ccData: 0x1230, type: 0 },
  1116. { ccData: 0x2d4c, type: 0 },
  1117. { ccData: 0x4100, type: 0 },
  1118. { ccData: 0x1338, type: 0 },
  1119. { ccData: 0x204c, type: 0 },
  1120. { ccData: 0x417d, type: 0 },
  1121. { ccData: 0x4400, type: 0 },
  1122. { ccData: 0x1137, type: 0 },
  1123. { ccData: 0x2200, type: 0 },
  1124. { ccData: 0x123f, type: 0 },
  1125. // EOC, End of Caption
  1126. { pts: 1000, ccData: 0x142f, type: 0 },
  1127. // Send another command so that the second EOC isn't ignored
  1128. { ccData: 0x1420, type: 0 },
  1129. // EOC, End of Caption, clear the display
  1130. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1131. ];
  1132. captions = [];
  1133. cea608Stream.on('data', function(caption) {
  1134. captions.push(caption);
  1135. });
  1136. packets.forEach(cea608Stream.push, cea608Stream);
  1137. assert.equal(captions[0].content[0].text, '«LÀ-LÅ LAÑD♪»',
  1138. 'translated special characters');
  1139. });
  1140. QUnit.test('pop-on mode', function(assert) {
  1141. var packets, captions;
  1142. packets = [
  1143. // RCL, resume caption loading
  1144. { ccData: 0x1420, type: 0 },
  1145. // 'hi'
  1146. { ccData: characters('hi'), type: 0 },
  1147. // EOC, End of Caption. Finished transmitting, begin display
  1148. { pts: 1000, ccData: 0x142f, type: 0 },
  1149. // Send another command so that the second EOC isn't ignored
  1150. { ccData: 0x1420, type: 0 },
  1151. // EOC, End of Caption. End display
  1152. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1153. ];
  1154. captions = [];
  1155. cea608Stream.on('data', function(caption) {
  1156. captions.push(caption);
  1157. });
  1158. packets.forEach(cea608Stream.push, cea608Stream);
  1159. assert.equal(captions.length, 1, 'detected a caption');
  1160. assert.deepEqual(captions[0], {
  1161. startPts: 1000,
  1162. endPts: 10 * 1000,
  1163. content: [{
  1164. line: 15,
  1165. position: 10,
  1166. text: 'hi'
  1167. }],
  1168. stream: 'CC1'
  1169. }, 'parsed the caption');
  1170. });
  1171. QUnit.test('ignores null characters', function(assert) {
  1172. var packets, captions;
  1173. packets = [
  1174. // RCL, resume caption loading
  1175. { ccData: 0x1420, type: 0 },
  1176. // 'mu'
  1177. { ccData: characters('mu'), type: 0 },
  1178. // null characters
  1179. { ccData: 0x0000, type: 0 },
  1180. // ' x'
  1181. { ccData: characters(' x'), type: 0 },
  1182. // EOC, End of Caption. Finished transmitting, begin display
  1183. { pts: 1000, ccData: 0x142f, type: 0 },
  1184. // Send another command so that the second EOC isn't ignored
  1185. { ccData: 0x1420, type: 0 },
  1186. // EOC, End of Caption. End display
  1187. { pts: 10 * 1000, ccData: 0x142f, type: 0 }
  1188. ];
  1189. captions = [];
  1190. cea608Stream.on('data', function(caption) {
  1191. captions.push(caption);
  1192. });
  1193. packets.forEach(cea608Stream.push, cea608Stream);
  1194. assert.equal(captions.length, 1, 'detected a caption');
  1195. assert.deepEqual(captions[0], {
  1196. startPts: 1000,
  1197. endPts: 10 * 1000,
  1198. content: [{
  1199. line: 15,
  1200. position: 10,
  1201. text: 'mu x'
  1202. }],
  1203. stream: 'CC1'
  1204. }, 'ignored null characters');
  1205. });
  1206. QUnit.test('recognizes the Erase Displayed Memory command', function(assert) {
  1207. var packets, captions;
  1208. packets = [
  1209. // RCL, resume caption loading
  1210. { ccData: 0x1420, type: 0 },
  1211. // '01'
  1212. { ccData: characters('01'), type: 0 },
  1213. // EOC, End of Caption. Finished transmitting, display '01'
  1214. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1215. // EDM, Erase Displayed Memory
  1216. { pts: 1.5 * 1000, ccData: 0x142c, type: 0 },
  1217. // '23'
  1218. { ccData: characters('23'), type: 0 },
  1219. // EOC, End of Caption. Display '23'
  1220. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  1221. // '34'
  1222. { ccData: characters('34'), type: 0 },
  1223. // EOC, End of Caption. Display '34'
  1224. { pts: 3 * 1000, ccData: 0x142f, type: 0 },
  1225. // Send another command so that the second EOC isn't ignored
  1226. { ccData: 0x1420, type: 0},
  1227. // EOC, End of Caption
  1228. { pts: 4 * 1000, ccData: 0x142f, type: 0 }
  1229. ];
  1230. captions = [];
  1231. cea608Stream.on('data', function(caption) {
  1232. captions.push(caption);
  1233. });
  1234. packets.forEach(cea608Stream.push, cea608Stream);
  1235. assert.equal(captions.length, 3, 'detected three captions');
  1236. assert.deepEqual(captions[0], {
  1237. startPts: 1 * 1000,
  1238. endPts: 1.5 * 1000,
  1239. content: [{
  1240. line: 15,
  1241. position: 10,
  1242. text: '01'
  1243. }],
  1244. stream: 'CC1'
  1245. }, 'parsed the first caption');
  1246. assert.deepEqual(captions[1], {
  1247. startPts: 2 * 1000,
  1248. endPts: 3 * 1000,
  1249. content: [{
  1250. line: 15,
  1251. position: 10,
  1252. text: '23'
  1253. }],
  1254. stream: 'CC1'
  1255. }, 'parsed the second caption');
  1256. assert.deepEqual(captions[2], {
  1257. startPts: 3 * 1000,
  1258. endPts: 4 * 1000,
  1259. content: [{
  1260. line: 15,
  1261. position: 10,
  1262. text: '34'
  1263. }],
  1264. stream: 'CC1'
  1265. }, 'parsed the third caption');
  1266. });
  1267. QUnit.test('correct content text is added to non-displayed memory for pop-on mode', function(assert) {
  1268. var captions = [], packets;
  1269. cea608Stream.on('data', function(caption) {
  1270. captions.push(caption);
  1271. });
  1272. packets = [
  1273. // RCL, resume caption loading
  1274. { ccData: 0x1420, type: 0 },
  1275. // '01'
  1276. { ccData: characters('01'), type: 0 },
  1277. // backspace
  1278. { ccData: 0x1421, type: 0 },
  1279. { ccData: characters('23'), type: 0 },
  1280. // PAC: row 13, no indent
  1281. { pts: 1 * 1000, ccData: 0x1370, type: 0 },
  1282. { pts: 1 * 1000, ccData: characters('32'), type: 0 },
  1283. // backspace
  1284. { pts: 2 * 1000, ccData: 0x1421, type: 0 },
  1285. { pts: 3 * 1000, ccData: characters('10'), type: 0 },
  1286. // EOC, End of Caption
  1287. { pts: 4 * 1000, ccData: 0x142f, type: 0 },
  1288. // Send another command so that the second EOC isn't ignored
  1289. { ccData: 0x1420, type: 0 },
  1290. // EOC, End of Caption, flush caption
  1291. { pts: 5 * 1000, ccData: 0x142f, type: 0 }
  1292. ];
  1293. packets.forEach(cea608Stream.push, cea608Stream);
  1294. assert.equal(captions.length, 1, 'detected a caption');
  1295. assert.equal(captions[0].content[0].text, '310', 'first content text');
  1296. assert.equal(captions[0].content[1].text, '023', 'second content text');
  1297. });
  1298. QUnit.test('backspaces on cleared memory are no-ops', function(assert) {
  1299. var captions = [], packets;
  1300. cea608Stream.on('data', function(caption) {
  1301. captions.push(caption);
  1302. });
  1303. packets = [
  1304. // RCL, resume caption loading
  1305. { ccData: 0x1420 },
  1306. // backspace
  1307. { ccData: 0x1421 },
  1308. // EOC, End of Caption. Finished transmitting, display '01'
  1309. { pts: 1 * 1000, ccData: 0x142f }
  1310. ];
  1311. packets.forEach(cea608Stream.push, cea608Stream);
  1312. assert.equal(captions.length, 0, 'no captions detected');
  1313. });
  1314. QUnit.test('recognizes the Erase Non-Displayed Memory command', function(assert) {
  1315. var packets, captions;
  1316. packets = [
  1317. // RCL, resume caption loading
  1318. { ccData: 0x1420, type: 0 },
  1319. // '01'
  1320. { ccData: characters('01'), type: 0 },
  1321. // ENM, Erase Non-Displayed Memory
  1322. { ccData: 0x142e, type: 0 },
  1323. { ccData: characters('23'), type: 0 },
  1324. // EOC, End of Caption. Finished transmitting, display '23'
  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. packets.forEach(cea608Stream.push, cea608Stream);
  1333. cea608Stream.on('data', function(caption) {
  1334. captions.push(caption);
  1335. });
  1336. packets.forEach(cea608Stream.push, cea608Stream);
  1337. assert.equal(captions.length, 1, 'detected one caption');
  1338. assert.deepEqual(captions[0], {
  1339. startPts: 1 * 1000,
  1340. endPts: 2 * 1000,
  1341. content: [{
  1342. line: 15,
  1343. position: 10,
  1344. text: '23'
  1345. }],
  1346. stream: 'CC1'
  1347. }, 'cleared the non-displayed memory');
  1348. });
  1349. QUnit.test('ignores unrecognized commands', function(assert) {
  1350. var packets, captions;
  1351. packets = [
  1352. // RCL, resume caption loading
  1353. { ccData: 0x1420, type: 0 },
  1354. // a row-9 magenta command, which is not supported
  1355. { ccData: 0x1f4c, type: 0 },
  1356. // '01'
  1357. { ccData: characters('01'), type: 0 },
  1358. // EOC, End of Caption
  1359. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1360. // Send another command so that the second EOC isn't ignored
  1361. { ccData: 0x1420, type: 0 },
  1362. // EOC, End of Caption
  1363. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1364. ];
  1365. captions = [];
  1366. cea608Stream.on('data', function(caption) {
  1367. captions.push(caption);
  1368. });
  1369. packets.forEach(cea608Stream.push, cea608Stream);
  1370. assert.equal(captions[0].content[0].text, '01', 'skipped the unrecognized commands');
  1371. });
  1372. QUnit.skip('applies preamble address codes', function(assert) {
  1373. assert.ok(false, 'not implemented');
  1374. });
  1375. QUnit.skip('applies mid-row colors', function(assert) {
  1376. assert.ok(false, 'not implemented');
  1377. });
  1378. QUnit.test('applies mid-row underline', function(assert) {
  1379. var captions = [];
  1380. cea608Stream.on('data', function(caption) {
  1381. captions.push(caption);
  1382. });
  1383. var packets = [
  1384. // RU2 (roll-up, 2 rows)
  1385. { ccData: 0x1425, type: 0 },
  1386. { ccData: characters('no'), type: 0 },
  1387. // mid-row, white underline
  1388. { ccData: 0x1121, type: 0 },
  1389. { ccData: characters('ye'), type: 0 },
  1390. { ccData: characters('s.'), type: 0 },
  1391. // CR (carriage return), dispatches caption
  1392. { ccData: 0x142d, type: 0 }
  1393. ];
  1394. packets.forEach(cea608Stream.push, cea608Stream);
  1395. assert.equal(captions[0].content[0].text, 'no <u>yes.</u>', 'properly closed by CR');
  1396. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1397. });
  1398. QUnit.test('applies mid-row italics', function(assert) {
  1399. var captions = [];
  1400. cea608Stream.on('data', function(caption) {
  1401. captions.push(caption);
  1402. });
  1403. var packets = [
  1404. // RU2 (roll-up, 2 rows)
  1405. { ccData: 0x1425, type: 0 },
  1406. { ccData: characters('no'), type: 0 },
  1407. // mid-row, italics
  1408. { ccData: 0x112e, type: 0 },
  1409. { ccData: characters('ye'), type: 0 },
  1410. { ccData: characters('s.'), type: 0 },
  1411. // CR (carriage return), dispatches caption
  1412. { ccData: 0x142d, type: 0 }
  1413. ];
  1414. packets.forEach(cea608Stream.push, cea608Stream);
  1415. assert.equal(captions[0].content[0].text, 'no <i>yes.</i>', 'properly closed by CR');
  1416. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1417. });
  1418. QUnit.test('applies mid-row italics underline', function(assert) {
  1419. var captions = [];
  1420. cea608Stream.on('data', function(caption) {
  1421. captions.push(caption);
  1422. });
  1423. var packets = [
  1424. // RU2 (roll-up, 2 rows)
  1425. { ccData: 0x1425, type: 0 },
  1426. { ccData: characters('no'), type: 0 },
  1427. // mid-row, italics underline
  1428. { ccData: 0x112f, type: 0 },
  1429. { ccData: characters('ye'), type: 0 },
  1430. { ccData: characters('s.'), type: 0 },
  1431. // CR (carriage return), dispatches caption
  1432. { ccData: 0x142d, type: 0 }
  1433. ];
  1434. packets.forEach(cea608Stream.push, cea608Stream);
  1435. assert.equal(captions[0].content[0].text, 'no <i><u>yes.</u></i>', 'properly closed by CR');
  1436. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1437. });
  1438. // NOTE: With the exception of white italics PACs (the following two test
  1439. // cases), PACs only have their underline attribute extracted and used
  1440. QUnit.test('applies PAC underline', function(assert) {
  1441. var captions = [];
  1442. cea608Stream.on('data', function(caption) {
  1443. captions.push(caption);
  1444. });
  1445. var packets = [
  1446. // RU2 (roll-up, 2 rows)
  1447. { ccData: 0x1425, type: 0 },
  1448. // PAC: row 15, white underline
  1449. { ccData: 0x1461, type: 0 },
  1450. { ccData: characters('ye'), type: 0 },
  1451. { ccData: characters('s.'), type: 0 },
  1452. // CR (carriage return), dispatches caption
  1453. { ccData: 0x142d, type: 0 }
  1454. ];
  1455. packets.forEach(cea608Stream.push, cea608Stream);
  1456. assert.equal(captions[0].content[0].text, '<u>yes.</u>', 'properly closed by CR');
  1457. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1458. });
  1459. QUnit.test('applies PAC white italics', function(assert) {
  1460. var captions = [];
  1461. cea608Stream.on('data', function(caption) {
  1462. captions.push(caption);
  1463. });
  1464. var packets = [
  1465. // RU2 (roll-up, 2 rows)
  1466. { ccData: 0x1425, type: 0 },
  1467. // PAC: row 15, white italics
  1468. { ccData: 0x146e, type: 0 },
  1469. { ccData: characters('ye'), type: 0 },
  1470. { ccData: characters('s.'), type: 0 },
  1471. // CR (carriage return), dispatches caption
  1472. { ccData: 0x142d, type: 0 }
  1473. ];
  1474. packets.forEach(cea608Stream.push, cea608Stream);
  1475. assert.equal(captions[0].content[0].text, '<i>yes.</i>', 'properly closed by CR');
  1476. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1477. });
  1478. QUnit.test('applies PAC white italics underline', function(assert) {
  1479. var captions = [];
  1480. cea608Stream.on('data', function(caption) {
  1481. captions.push(caption);
  1482. });
  1483. var packets = [
  1484. // RU2 (roll-up, 2 rows)
  1485. { ccData: 0x1425, type: 0 },
  1486. // PAC: row 15, white italics underline
  1487. { ccData: 0x146f, type: 0 },
  1488. { ccData: characters('ye'), type: 0 },
  1489. { ccData: characters('s.'), type: 0 },
  1490. // CR (carriage return), dispatches caption
  1491. { ccData: 0x142d, type: 0 }
  1492. ];
  1493. packets.forEach(cea608Stream.push, cea608Stream);
  1494. assert.equal(captions[0].content[0].text, '<u><i>yes.</i></u>', 'properly closed by CR');
  1495. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1496. });
  1497. QUnit.test('includes all caption text at PAC row change', function(assert) {
  1498. var captions = [];
  1499. cea608Stream.on('data', function(caption) {
  1500. captions.push(caption);
  1501. });
  1502. var packets = [
  1503. // RCL, resume caption loading
  1504. { ccData: 0x1420, type: 0 },
  1505. // PAC: row 14, white italics underlime
  1506. { ccData: 0x144f, type: 0 },
  1507. { ccData: characters('ye'), type: 0 },
  1508. { ccData: characters('s.'), type: 0 },
  1509. // PAC: row 15, indent 0
  1510. { ccData: 0x1470, type: 0 },
  1511. { ccData: characters('no'), type: 0 },
  1512. // EOC, End of Caption
  1513. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1514. // RCL, resume caption loading
  1515. { ccData: 0x1420, type: 0 },
  1516. // EOC, End of Caption
  1517. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1518. ];
  1519. packets.forEach(cea608Stream.push, cea608Stream);
  1520. assert.equal(captions[0].content[0].text, '<u><i>yes.</i></u>', 'first content text');
  1521. assert.equal(captions[0].content[1].text, 'no', 'second content text');
  1522. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1523. });
  1524. QUnit.test('closes formatting at EOC', function(assert) {
  1525. var captions = [];
  1526. cea608Stream.on('data', function(caption) {
  1527. captions.push(caption);
  1528. });
  1529. var packets = [
  1530. // RCL, resume caption loading
  1531. { ccData: 0x1420, type: 0 },
  1532. // PAC: row 15, white italics underline
  1533. { ccData: 0x146f, type: 0 },
  1534. { ccData: characters('ye'), type: 0 },
  1535. { ccData: characters('s.'), type: 0 },
  1536. // EOC, End of Caption
  1537. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1538. // Send another command so that the second EOC isn't ignored
  1539. { ccData: 0x1420, type: 0 },
  1540. // EOC, End of Caption
  1541. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  1542. ];
  1543. packets.forEach(cea608Stream.push, cea608Stream);
  1544. assert.equal(captions[0].content[0].text, '<u><i>yes.</i></u>', 'properly closed by EOC');
  1545. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1546. });
  1547. QUnit.test('closes formatting at negating mid-row code', function(assert) {
  1548. var captions = [];
  1549. cea608Stream.on('data', function(caption) {
  1550. captions.push(caption);
  1551. });
  1552. var packets = [
  1553. // RU2 (roll-up, 2 rows)
  1554. { ccData: 0x1425, type: 0 },
  1555. { ccData: characters('no'), type: 0 },
  1556. // mid-row: italics underline
  1557. { ccData: 0x112f, type: 0 },
  1558. { ccData: characters('ye'), type: 0 },
  1559. { ccData: characters('s.'), type: 0 },
  1560. // mid-row: white
  1561. { ccData: 0x1120, type: 0 },
  1562. { ccData: characters('no'), type: 0 }
  1563. ];
  1564. packets.forEach(cea608Stream.push, cea608Stream);
  1565. cea608Stream.flushDisplayed();
  1566. assert.equal(captions[0].content[0].text, 'no <i><u>yes.</u></i> no', 'properly closed by negating mid-row code');
  1567. assert.deepEqual(cea608Stream.formatting_, [], 'formatting is empty');
  1568. });
  1569. QUnit.test('roll-up display mode', function(assert) {
  1570. var captions = [];
  1571. cea608Stream.on('data', function(caption) {
  1572. captions.push(caption);
  1573. });
  1574. [ // RU2, roll-up captions 2 rows
  1575. { ccData: 0x1425, type: 0},
  1576. // '01'
  1577. {
  1578. pts: 1 * 1000,
  1579. ccData: characters('01'),
  1580. type: 0
  1581. },
  1582. // CR, carriage return
  1583. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1584. ].forEach(cea608Stream.push, cea608Stream);
  1585. assert.equal(captions.length, 1, 'detected one caption');
  1586. assert.deepEqual(captions[0], {
  1587. startPts: 0 * 1000,
  1588. endPts: 3 * 1000,
  1589. content: [{
  1590. line: 15,
  1591. position: 10,
  1592. text: '01'
  1593. }],
  1594. stream: 'CC1'
  1595. }, 'parsed the caption');
  1596. captions = [];
  1597. [ // RU4, roll-up captions 4 rows
  1598. { ccdata: 0x1427, type: 0 },
  1599. // '23'
  1600. {
  1601. pts: 4 * 1000,
  1602. ccData: characters('23'),
  1603. type: 0,
  1604. stream: 'CC1'
  1605. },
  1606. // CR
  1607. { pts: 5 * 1000, ccData: 0x142d, type: 0 }
  1608. ].forEach(cea608Stream.push, cea608Stream);
  1609. assert.equal(captions.length, 1, 'detected another caption');
  1610. assert.deepEqual(captions[0], {
  1611. startPts: 3 * 1000,
  1612. endPts: 5 * 1000,
  1613. content: [
  1614. {
  1615. line: 14,
  1616. position: 10,
  1617. text: '01'
  1618. },
  1619. {
  1620. line: 15,
  1621. position: 10,
  1622. text: '23'
  1623. }
  1624. ],
  1625. stream: 'CC1'
  1626. }, 'parsed the new caption and kept the caption up after the new caption');
  1627. });
  1628. QUnit.test('roll-up displays multiple rows simultaneously', function(assert) {
  1629. var captions = [];
  1630. cea608Stream.on('data', function(caption) {
  1631. captions.push(caption);
  1632. });
  1633. [ // RU2, roll-up captions 2 rows
  1634. { ccData: 0x1425, type: 0 },
  1635. // '01'
  1636. {
  1637. pts: 0 * 1000,
  1638. ccData: characters('01'),
  1639. type: 0,
  1640. stream: 'CC1'
  1641. },
  1642. // CR, carriage return
  1643. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1644. ].forEach(cea608Stream.push, cea608Stream);
  1645. assert.equal(captions.length, 1, 'detected a caption');
  1646. assert.deepEqual(captions[0], {
  1647. startPts: 0 * 1000,
  1648. endPts: 1 * 1000,
  1649. content: [{
  1650. line: 15,
  1651. position: 10,
  1652. text: '01'
  1653. }],
  1654. stream: 'CC1'
  1655. }, 'created a caption for the first period');
  1656. captions = [];
  1657. [ // '23'
  1658. {
  1659. pts: 2 * 1000,
  1660. ccData: characters('23'),
  1661. type: 0,
  1662. stream: 'CC1'
  1663. },
  1664. // CR, carriage return
  1665. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1666. ].forEach(cea608Stream.push, cea608Stream);
  1667. assert.equal(captions.length, 1, 'detected another caption');
  1668. assert.deepEqual(captions[0], {
  1669. startPts: 1 * 1000,
  1670. endPts: 3 * 1000,
  1671. content: [
  1672. {
  1673. line: 14,
  1674. position: 10,
  1675. text: '01'
  1676. },
  1677. {
  1678. line: 15,
  1679. position: 10,
  1680. text: '23'
  1681. }
  1682. ],
  1683. stream: 'CC1'
  1684. }, 'created the top and bottom rows after the shift up');
  1685. captions = [];
  1686. [ // '45'
  1687. {
  1688. pts: 4 * 1000,
  1689. ccData: characters('45'),
  1690. type: 0,
  1691. stream: 'CC1'
  1692. },
  1693. // CR, carriage return
  1694. { pts: 5 * 1000, ccData: 0x142d, type: 0 }
  1695. ].forEach(cea608Stream.push, cea608Stream);
  1696. assert.equal(captions.length, 1, 'detected third caption');
  1697. assert.deepEqual(captions[0], {
  1698. startPts: 3 * 1000,
  1699. endPts: 5 * 1000,
  1700. "content": [
  1701. {
  1702. line: 14,
  1703. position: 10,
  1704. text: '23'
  1705. },
  1706. {
  1707. line: 15,
  1708. position: 10,
  1709. text: '45'
  1710. }
  1711. ],
  1712. stream: 'CC1'
  1713. }, 'created the top and bottom rows after the shift up');
  1714. });
  1715. QUnit.test('the roll-up count can be changed on-the-fly', function(assert) {
  1716. var captions = [];
  1717. cea608Stream.on('data', function(caption) {
  1718. captions.push(caption);
  1719. });
  1720. [ // RU2, roll-up captions 2 rows
  1721. { ccData: 0x1425, type: 0 },
  1722. // '01'
  1723. {
  1724. pts: 0 * 1000,
  1725. ccData: characters('01'),
  1726. type: 0,
  1727. stream: 'CC1'
  1728. },
  1729. // CR, carriage return
  1730. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1731. ].forEach(cea608Stream.push, cea608Stream);
  1732. captions = [];
  1733. [ // RU3, roll-up captions 3 rows
  1734. { ccData: 0x1426, type: 0 },
  1735. // CR, carriage return
  1736. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  1737. ].forEach(cea608Stream.push, cea608Stream);
  1738. assert.equal(captions.length, 1, 'still displaying a caption');
  1739. captions = [];
  1740. [ // RU4, roll-up captions 4 rows
  1741. { ccData: 0x1427, type: 0 },
  1742. // CR, carriage return
  1743. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1744. ].forEach(cea608Stream.push, cea608Stream);
  1745. assert.equal(captions.length, 1, 'still displaying a caption');
  1746. captions = [];
  1747. // RU3, roll-up captions 3 rows
  1748. cea608Stream.push({ ccdata: 0x1426, type: 0 });
  1749. assert.equal(captions.length, 0, 'cleared the caption');
  1750. });
  1751. QUnit.test('switching to roll-up from pop-on wipes memories and flushes captions', function(assert) {
  1752. var captions = [];
  1753. cea608Stream.on('data', function(caption) {
  1754. captions.push(caption);
  1755. });
  1756. [
  1757. // RCL (resume caption loading)
  1758. { pts: 0 * 1000, ccData: 0x1420, type: 0 },
  1759. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1760. // EOC (end of caption), mark 1st caption start
  1761. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1762. // RCL, resume caption loading
  1763. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  1764. { pts: 2 * 1000, ccData: characters('oh'), type: 0 },
  1765. // EOC, mark 2nd caption start and flush 1st caption
  1766. { pts: 2 * 1000, ccData: 0x142f, type: 0 },
  1767. // RU2 (roll-up, 2 rows), flush 2nd caption
  1768. { pts: 3 * 1000, ccData: 0x1425, type: 0 }
  1769. ].forEach(cea608Stream.push, cea608Stream);
  1770. var displayed = cea608Stream.displayed_.reduce(function(acc, val) {
  1771. acc += val.text;
  1772. return acc;
  1773. }, '');
  1774. var nonDisplayed = cea608Stream.nonDisplayed_.reduce(function(acc, val) {
  1775. acc += val.text;
  1776. return acc;
  1777. }, '');
  1778. assert.equal(captions.length, 2, 'both captions flushed');
  1779. assert.equal(displayed, '', 'displayed memory is wiped');
  1780. assert.equal(nonDisplayed, '', 'non-displayed memory is wiped');
  1781. assert.deepEqual(captions[0], {
  1782. startPts: 1000,
  1783. endPts: 2000,
  1784. content: [{
  1785. line: 15,
  1786. position: 10,
  1787. text: 'hi',
  1788. }],
  1789. stream: 'CC1'
  1790. }, 'first caption correct');
  1791. assert.deepEqual(captions[1], {
  1792. startPts: 2000,
  1793. endPts: 3000,
  1794. content: [{
  1795. line: 15,
  1796. position: 10,
  1797. text: 'oh',
  1798. }],
  1799. stream: 'CC1'
  1800. }, 'second caption correct');
  1801. });
  1802. QUnit.test('switching to roll-up from paint-on wipes memories and flushes captions', function(assert) {
  1803. var captions = [];
  1804. cea608Stream.on('data', function(caption) {
  1805. captions.push(caption);
  1806. });
  1807. [
  1808. // RDC (resume direct captioning)
  1809. { pts: 0 * 1000, ccData: 0x1429, type: 0 },
  1810. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1811. // RU2 (roll-up, 2 rows), flush displayed caption
  1812. { pts: 1 * 1000, ccData: 0x1425, type: 0 }
  1813. ].forEach(cea608Stream.push, cea608Stream);
  1814. var displayed = cea608Stream.displayed_.reduce(function(acc, val) {
  1815. acc += val.text;
  1816. return acc;
  1817. }, '');
  1818. var nonDisplayed = cea608Stream.nonDisplayed_.reduce(function(acc, val) {
  1819. acc += val.text;
  1820. return acc;
  1821. }, '');
  1822. assert.equal(captions.length, 1, 'flushed caption');
  1823. assert.equal(displayed, '', 'displayed memory is wiped');
  1824. assert.equal(nonDisplayed, '', 'non-displayed memory is wiped');
  1825. assert.deepEqual(captions[0], {
  1826. startPts: 0,
  1827. endPts: 1000,
  1828. content: [{
  1829. line: 15,
  1830. position: 10,
  1831. text: 'hi',
  1832. }],
  1833. stream: 'CC1'
  1834. }, 'caption correct');
  1835. });
  1836. // NOTE: This should change to not wiping the display when caption
  1837. // positioning is properly implemented
  1838. QUnit.test('switching to paint-on from pop-on flushes display', function(assert) {
  1839. var captions = [];
  1840. cea608Stream.on('data', function(caption) {
  1841. captions.push(caption);
  1842. });
  1843. [
  1844. // RCL (resume caption loading)
  1845. { pts: 0 * 1000, ccData: 0x1420, type: 0 },
  1846. // PAC: row 14, indent 0
  1847. { pts: 0 * 1000, ccData: 0x1450, type: 0 },
  1848. { pts: 0 * 1000, ccData: characters('hi'), type: 0 },
  1849. // EOC (end of caption), mark caption start
  1850. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  1851. // RCL
  1852. { pts: 1 * 1000, ccData: 0x1420, type: 0 },
  1853. // RDC (resume direct captioning), flush caption
  1854. { pts: 2 * 1000, ccData: 0x1429, type: 0 },
  1855. // PAC: row 14, indent 0
  1856. { pts: 2 * 1000, ccData: 0x1450, type: 0 },
  1857. // TO1 (tab offset 1 column)
  1858. { pts: 2 * 1000, ccData: 0x1721, type: 0 },
  1859. { pts: 3 * 1000, ccData: characters('io'), type: 0 },
  1860. // EDM (erase displayed memory), flush paint-on caption
  1861. { pts: 4 * 1000, ccData: 0x142c, type: 0 }
  1862. ].forEach(cea608Stream.push, cea608Stream);
  1863. assert.equal(captions.length, 2, 'detected 2 captions');
  1864. assert.equal(captions[0].content[0].text, 'hi', 'pop-on caption received');
  1865. assert.equal(captions[0].startPts, 1000, 'proper start pts');
  1866. assert.equal(captions[0].endPts, 2000, 'proper end pts');
  1867. assert.equal(captions[1].content[0].text, 'io', 'paint-on caption received');
  1868. assert.equal(captions[1].startPts, 2000, 'proper start pts');
  1869. assert.equal(captions[1].endPts, 4000, 'proper end pts');
  1870. });
  1871. QUnit.test('backspaces are reflected in the generated captions', function(assert) {
  1872. var captions = [];
  1873. cea608Stream.on('data', function(caption) {
  1874. captions.push(caption);
  1875. });
  1876. [ // RU2, roll-up captions 2 rows
  1877. { ccData: 0x1425, type: 0 },
  1878. // '01'
  1879. {
  1880. pts: 0 * 1000,
  1881. ccData: characters('01'),
  1882. type: 0
  1883. },
  1884. // backspace
  1885. { ccData: 0x1421, type: 0 },
  1886. {
  1887. pts: 1 * 1000,
  1888. ccData: characters('23'),
  1889. type: 0
  1890. },
  1891. // CR, carriage return
  1892. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1893. ].forEach(cea608Stream.push, cea608Stream);
  1894. assert.equal(captions.length, 1, 'detected a caption');
  1895. assert.equal(captions[0].content[0].text, '023', 'applied the backspace');
  1896. });
  1897. QUnit.test('backspaces can remove a caption entirely', function(assert) {
  1898. var captions = [];
  1899. cea608Stream.on('data', function(caption) {
  1900. captions.push(caption);
  1901. });
  1902. [ // RU2, roll-up captions 2 rows
  1903. { ccData: 0x1425, type: 0 },
  1904. // '01'
  1905. {
  1906. pts: 0 * 1000,
  1907. ccData: characters('01'),
  1908. type: 0
  1909. },
  1910. // backspace
  1911. { ccData: 0x1421, type: 0 },
  1912. // Send another command so that the backspace isn't
  1913. // ignored as a duplicate command
  1914. { ccData: 0x1425, type: 0 },
  1915. // backspace
  1916. { ccData: 0x1421, type: 0 },
  1917. // CR, carriage return
  1918. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  1919. ].forEach(cea608Stream.push, cea608Stream);
  1920. assert.equal(captions.length, 0, 'no caption emitted');
  1921. });
  1922. QUnit.test('a second identical control code immediately following the first is ignored', function(assert) {
  1923. var captions = [];
  1924. cea608Stream.on('data', function(caption) {
  1925. captions.push(caption);
  1926. });
  1927. [ // RU2, roll-up captions 2 rows
  1928. { ccData: 0x1425, type: 0 },
  1929. // '01'
  1930. {
  1931. pts: 0 * 1000,
  1932. ccData: characters('01'),
  1933. type: 0
  1934. },
  1935. // '02'
  1936. {
  1937. pts: 1 * 1000,
  1938. ccData: characters('02'),
  1939. type: 0
  1940. },
  1941. // backspace
  1942. { ccData: 0x1421, type: 0 },
  1943. // backspace
  1944. { ccData: 0x1421, type: 0 }, // duplicate is ignored
  1945. // backspace
  1946. { ccData: 0x1421, type: 0 },
  1947. // CR, carriage return
  1948. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  1949. ].forEach(cea608Stream.push, cea608Stream);
  1950. assert.equal(captions.length, 1, 'caption emitted');
  1951. assert.equal(captions[0].content[0].text, '01', 'only two backspaces processed');
  1952. });
  1953. QUnit.test('a second identical control code separated by only padding from the first is ignored', function(assert) {
  1954. var captions = [];
  1955. cea608Stream.on('data', function(caption) {
  1956. captions.push(caption);
  1957. });
  1958. [ // RU2, roll-up captions 2 rows
  1959. { ccData: 0x1425, type: 0 },
  1960. // '01'
  1961. {
  1962. pts: 0 * 1000,
  1963. ccData: characters('01'),
  1964. type: 0
  1965. },
  1966. // '02'
  1967. {
  1968. pts: 1 * 1000,
  1969. ccData: characters('02'),
  1970. type: 0
  1971. },
  1972. // backspace
  1973. { ccData: 0x1421, type: 0 },
  1974. // padding
  1975. { ccData: 0x0000, type: 0 },
  1976. { ccData: 0x0000, type: 0 },
  1977. { ccData: 0x0000, type: 0 },
  1978. // backspace
  1979. { pts: 2 * 1000, ccData: 0x1421, type: 0 }, // duplicate is ignored
  1980. // CR, carriage return
  1981. { pts: 3 * 1000, ccData: 0x142d, type: 0 }
  1982. ].forEach(cea608Stream.push, cea608Stream);
  1983. assert.equal(captions.length, 1, 'caption emitted');
  1984. assert.equal(captions[0].content[0].text, '010', 'only one backspace processed');
  1985. });
  1986. QUnit.test('preamble address codes on same row are NOT converted into spaces', function(assert) {
  1987. var captions = [];
  1988. cea608Stream.on('data', function(caption) {
  1989. captions.push(caption);
  1990. });
  1991. [ // RU2, roll-up captions 2 rows
  1992. { ccData: 0x1425, type: 0 },
  1993. // '01'
  1994. {
  1995. pts: 0 * 1000,
  1996. ccData: characters('01'),
  1997. type: 0
  1998. },
  1999. // PAC: row 15, indent 0
  2000. { ccData: 0x1470, type: 0 },
  2001. // '02'
  2002. {
  2003. pts: 1 * 1000,
  2004. ccData: characters('02'),
  2005. type: 0
  2006. },
  2007. // CR, carriage return
  2008. { pts: 2 * 1000, ccData: 0x142d, type: 0 }
  2009. ].forEach(cea608Stream.push, cea608Stream);
  2010. assert.equal(captions.length, 1, 'caption emitted');
  2011. assert.equal(captions[0].content[0].text, '0102', 'PACs were NOT converted to space');
  2012. });
  2013. QUnit.test('generates correct content with PACs in pop-on mode', function(assert) {
  2014. var captions = [];
  2015. cea608Stream.on('data', function(caption) {
  2016. captions.push(caption);
  2017. });
  2018. [
  2019. // RCL, resume caption loading
  2020. { ccData: 0x1420, type: 0 },
  2021. // ENM, erase non-displayed memory
  2022. { ccData: 0x142e, type: 0 },
  2023. // PAC: row 12, indent 0
  2024. { ccData: 0x1350, type: 0 },
  2025. // text: TEST
  2026. { ccData: 0x5445, type: 0 },
  2027. { ccData: 0x5354, type: 0 },
  2028. // PAC: row 14, indent 0
  2029. { ccData: 0x1450, type: 0 },
  2030. // text: STRING
  2031. { ccData: 0x5354, type: 0 },
  2032. { ccData: 0x5249, type: 0 },
  2033. { ccData: 0x4e47, type: 0 },
  2034. // PAC: row 15, indent 0
  2035. { ccData: 0x1470, type: 0 },
  2036. // text: DATA
  2037. { ccData: 0x4441, type: 0 },
  2038. { ccData: 0x5441, type: 0 },
  2039. // EOC, end of caption
  2040. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  2041. // EOC, duplicated as per spec
  2042. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  2043. // EOC, dispatch caption
  2044. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  2045. ].forEach(cea608Stream.push, cea608Stream);
  2046. assert.equal(captions.length, 1, 'caption emitted');
  2047. assert.equal(captions[0].content[0].text, 'TEST', 'first content text');
  2048. assert.equal(captions[0].content[1].text, 'STRING', 'second content text');
  2049. assert.equal(captions[0].content[2].text, 'DATA', 'third content text');
  2050. });
  2051. QUnit.test('extracts real-world cc1 and cc3 channels', function(assert) {
  2052. var cea608Stream1 = cea608Stream;
  2053. var cea608Stream3 = new m2ts.Cea608Stream(1, 0);
  2054. var captions = [];
  2055. cea608Stream1.on('data', function(caption) {
  2056. captions.push(caption);
  2057. });
  2058. cea608Stream3.on('data', function(caption) {
  2059. captions.push(caption);
  2060. });
  2061. var packets = [
  2062. { pts: 425316, type: 0, ccData: 5158 }, // RU3
  2063. { pts: 431322, type: 0, ccData: 5165 }, // CR
  2064. { pts: 440331, type: 0, ccData: 4944 }, // position 11,0
  2065. { pts: 443334, type: 0, ccData: 20549 }, // PE
  2066. { pts: 449340, type: 0, ccData: 21065 }, // RI
  2067. { pts: 449340, type: 0, ccData: 0 }, // padding
  2068. { pts: 452343, type: 0, ccData: 20292 }, // OD
  2069. { pts: 458349, type: 0, ccData: 11264 }, // ,
  2070. { pts: 458349, type: 0, ccData: 0 }, // padding
  2071. { pts: 461352, type: 0, ccData: 0 }, // padding
  2072. { pts: 467358, type: 0, ccData: 8192 }, // (space)
  2073. { pts: 467358, type: 0, ccData: 17920 }, // F
  2074. { pts: 470361, type: 0, ccData: 0 }, // padding
  2075. { pts: 476367, type: 0, ccData: 0 }, // padding
  2076. { pts: 476367, type: 0, ccData: 20300 }, // OL
  2077. { pts: 479370, type: 0, ccData: 19283 }, // KS
  2078. { pts: 485376, type: 0, ccData: 0 }, // padding
  2079. { pts: 485376, type: 0, ccData: 11776 }, // .
  2080. { pts: 674565, type: 0, ccData: 5158 }, // RU3
  2081. { pts: 677568, type: 0, ccData: 5165 }, // CR
  2082. { pts: 371262, type: 1, ccData: 5414 }, // RU3
  2083. { pts: 377268, type: 1, ccData: 0 }, // padding
  2084. { pts: 377268, type: 1, ccData: 4944 }, // position 11,0
  2085. { pts: 380271, type: 1, ccData: 0 }, // padding
  2086. { pts: 386277, type: 1, ccData: 4412 }, // ê
  2087. { pts: 386277, type: 1, ccData: 0 }, // padding
  2088. { pts: 389280, type: 1, ccData: 29810 }, // tr
  2089. { pts: 395286, type: 1, ccData: 25888 }, // e(space)
  2090. { pts: 395286, type: 1, ccData: 30062 }, // un
  2091. { pts: 398289, type: 1, ccData: 25888 }, // e(space)
  2092. { pts: 404295, type: 1, ccData: 28764 }, // pé
  2093. { pts: 404295, type: 1, ccData: 29289 }, // ri
  2094. { pts: 407298, type: 1, ccData: 28516 }, // od
  2095. { pts: 413304, type: 1, ccData: 25856 }, // e
  2096. { pts: 413304, type: 1, ccData: 0 }, // padding
  2097. { pts: 443334, type: 1, ccData: 8292 }, // (space)d
  2098. { pts: 449340, type: 1, ccData: 25888 }, // e(space)
  2099. { pts: 449340, type: 1, ccData: 29045 }, // qu
  2100. { pts: 452343, type: 1, ccData: 25971 }, // es
  2101. { pts: 458349, type: 1, ccData: 29801 }, // ti
  2102. { pts: 458349, type: 1, ccData: 28526 }, // on
  2103. { pts: 461352, type: 1, ccData: 29440 }, // s
  2104. { pts: 467358, type: 1, ccData: 5421 }, // CR
  2105. { pts: 467358, type: 1, ccData: 0 }, // padding
  2106. { pts: 470361, type: 1, ccData: 5414 }, // RU3
  2107. { pts: 476367, type: 1, ccData: 0 } // padding
  2108. ];
  2109. packets.forEach(function(packet) {
  2110. cea608Stream1.push(packet);
  2111. cea608Stream3.push(packet);
  2112. });
  2113. var cc1 = {stream: 'CC1', content: [{ text: 'PERIOD, FOLKS.'}] };
  2114. var cc3 = {stream: 'CC3', content: [{ text: 'être une période de questions' }] };
  2115. assert.equal(captions.length, 2, 'caption emitted');
  2116. assert.equal(captions[0].stream, cc1.stream, 'cc1 stream detected');
  2117. assert.equal(captions[0].content[0].text, cc1.content[0].text, 'cc1 stream extracted successfully');
  2118. assert.equal(captions[1].stream, cc3.stream, 'cc3 stream detected');
  2119. assert.equal(captions[1].content[0].text, cc3.content[0].text, 'cc3 stream extracted successfully');
  2120. });
  2121. QUnit.test('backspaces stop at the beginning of the line', function(assert) {
  2122. var captions = [];
  2123. cea608Stream.on('data', function(caption) {
  2124. captions.push(caption);
  2125. });
  2126. [ // RU2, roll-up captions 2 rows
  2127. { ccData: 0x1425, type: 0 },
  2128. // '01'
  2129. {
  2130. pts: 0 * 1000,
  2131. ccData: characters('01'),
  2132. type: 0
  2133. },
  2134. // backspace
  2135. { ccData: 0x1421, type: 0 },
  2136. // Send another command so that the backspace isn't
  2137. // ignored as a duplicate command
  2138. { ccData: 0x1425, type: 0 },
  2139. // backspace
  2140. { ccData: 0x1421, type: 0 },
  2141. // Send another command so that the backspace isn't
  2142. // ignored as a duplicate command
  2143. { ccData: 0x1425, type: 0 },
  2144. // backspace
  2145. { ccData: 0x1421, type: 0 },
  2146. // CR, carriage return
  2147. { pts: 1 * 1000, ccData: 0x142d, type: 0 }
  2148. ].forEach(cea608Stream.push, cea608Stream);
  2149. assert.equal(captions.length, 0, 'no caption emitted');
  2150. });
  2151. QUnit.test('reset works', function(assert) {
  2152. var captions = [];
  2153. cea608Stream.on('data', function(caption) {
  2154. captions.push(caption);
  2155. });
  2156. [ // RU2, roll-up captions 2 rows
  2157. { pts: 0, ccData: 0x1425, type: 0 },
  2158. // mid-row: white underline
  2159. { pts: 0, ccData: 0x1121, type: 0 },
  2160. { pts: 0, ccData: characters('01'), type: 0 }
  2161. ].forEach(cea608Stream.push, cea608Stream);
  2162. var buffer = cea608Stream.displayed_.map(function(row) {
  2163. return row.text.trim();
  2164. }).join('\n')
  2165. .replace(/^\n+|\n+$/g, '');
  2166. assert.equal(buffer, '<u>01', 'buffer is as expected');
  2167. cea608Stream.reset();
  2168. buffer = cea608Stream.displayed_
  2169. .map(function(row) {
  2170. return row.text.trim();
  2171. })
  2172. .join('\n')
  2173. .replace(/^\n+|\n+$/g, '');
  2174. assert.equal(buffer, '', 'displayed buffer reset successfully');
  2175. assert.equal(cea608Stream.lastControlCode_, null, 'last control code reset successfully');
  2176. assert.deepEqual(cea608Stream.formatting_, [], 'formatting was reset');
  2177. });
  2178. QUnit.test('paint-on mode', function(assert) {
  2179. var packets, captions;
  2180. packets = [
  2181. // RDC, resume direct captioning, begin display
  2182. { pts: 1000, ccData: 0x1429, type: 0 },
  2183. { pts: 2000, ccData: characters('hi'), type: 0 },
  2184. // EDM, erase displayed memory. Finish display, flush caption
  2185. { pts: 3000, ccData: 0x142c, type: 0 }
  2186. ];
  2187. captions = [];
  2188. cea608Stream.on('data', function(caption) {
  2189. captions.push(caption);
  2190. });
  2191. packets.forEach(cea608Stream.push, cea608Stream);
  2192. assert.equal(captions.length, 1, 'detected a caption');
  2193. assert.deepEqual(captions[0], {
  2194. startPts: 1000,
  2195. endPts: 3000,
  2196. content: [{
  2197. line: 15,
  2198. position: 10,
  2199. text: 'hi',
  2200. }],
  2201. stream: 'CC1'
  2202. }, 'parsed the caption');
  2203. });
  2204. QUnit.test('generates correct text from PACs in paint-on mode', function(assert) {
  2205. var captions = [];
  2206. cea608Stream.on('data', function(caption) {
  2207. captions.push(caption);
  2208. });
  2209. [
  2210. // RDC, resume direct captioning
  2211. { pts: 1000, ccData: 0x1429, type: 0 },
  2212. // PAC: row 12, indent 0
  2213. { pts: 1000, ccData: 0x1350, type: 0 },
  2214. // text: TEST
  2215. { pts: 2000, ccData: 0x5445, type: 0 },
  2216. { pts: 2000, ccData: 0x5354, type: 0 },
  2217. // PAC: row 14, indent 0
  2218. { pts: 3000, ccData: 0x1450, type: 0 },
  2219. // text: STRING
  2220. { pts: 3000, ccData: 0x5354, type: 0 },
  2221. { pts: 4000, ccData: 0x5249, type: 0 },
  2222. { pts: 4000, ccData: 0x4e47, type: 0 },
  2223. // PAC: row 15, indent 0
  2224. { pts: 5000, ccData: 0x1470, type: 0 },
  2225. // text: DATA
  2226. { pts: 5000, ccData: 0x4441, type: 0 },
  2227. { pts: 6000, ccData: 0x5441, type: 0 },
  2228. // EDM, erase displayed memory. Finish display, flush caption
  2229. { pts: 6000, ccData: 0x142c, type: 0 }
  2230. ].forEach(cea608Stream.push, cea608Stream);
  2231. assert.equal(captions.length, 1, 'caption emitted');
  2232. assert.equal(captions[0].content[0].text, 'TEST', 'first content text');
  2233. assert.equal(captions[0].content[1].text, 'STRING', 'second content text');
  2234. assert.equal(captions[0].content[2].text, 'DATA', 'third content text');
  2235. });
  2236. QUnit.test('multiple caption texts are generated (paint-on)', function(assert) {
  2237. var captions = [];
  2238. cea608Stream.on('data', function(caption) {
  2239. captions.push(caption);
  2240. });
  2241. [ // RDC, resume direct captioning
  2242. { ccData: 0x1429, type: 0 },
  2243. // '01', default row 15
  2244. { pts: 0 * 1000, ccData: characters('01'), type: 0 },
  2245. // backspace
  2246. { pts: 0 * 1000, ccData: 0x1421, type: 0 },
  2247. { pts: 1 * 1000, ccData: characters('23'), type: 0 },
  2248. // PAC: row 13, indent 0
  2249. { pts: 2 * 1000, ccData: 0x1370, type: 0 },
  2250. { pts: 2 * 1000, ccData: characters('32'), type: 0 },
  2251. // backspace
  2252. { pts: 3 * 1000, ccData: 0x1421, type: 0 },
  2253. { pts: 4 * 1000, ccData: characters('10'), type: 0 },
  2254. // EDM, erase displayed memory, flush caption
  2255. { pts: 5 * 1000, ccData: 0x142c, type: 0 }
  2256. ].forEach(cea608Stream.push, cea608Stream);
  2257. assert.equal(captions.length, 1, 'detected a caption');
  2258. assert.equal(captions[0].content[0].text, '310', 'first caption text');
  2259. assert.equal(captions[0].content[1].text, '023', 'second caption text');
  2260. });
  2261. QUnit.test('PAC indent code increases the position', function(assert) {
  2262. var captions = [];
  2263. cea608Stream.on('data', function(caption) {
  2264. captions.push(caption);
  2265. });
  2266. var packets = [
  2267. // RCL, resume caption loading
  2268. { ccData: 0x1420, type: 0 },
  2269. // PAC indent code representing 4 indentations.
  2270. { ccData: 5240, type: 0 },
  2271. { ccData: characters('te'), type: 0 },
  2272. { ccData: characters('st'), type: 0 },
  2273. // EOC, End of Caption
  2274. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  2275. // RCL, resume caption loading
  2276. { ccData: 0x1420, type: 0 },
  2277. // EOC, End of Caption
  2278. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  2279. ];
  2280. packets.forEach(cea608Stream.push, cea608Stream);
  2281. assert.equal(captions[0].content[0].text, 'test', 'content text');
  2282. assert.equal(captions[0].content[0].line, 15, 'positions the caption to the bottom of the screen');
  2283. assert.equal(captions[0].content[0].position, 50, 'positions the caption to the right of the screen');
  2284. });
  2285. QUnit.test('PAC offset code increases the position', function(assert) {
  2286. var captions = [];
  2287. cea608Stream.on('data', function(caption) {
  2288. captions.push(caption);
  2289. });
  2290. var packets = [
  2291. // RCL, resume caption loading
  2292. { ccData: 0x1420, type: 0 },
  2293. // PAC: row 1, indent 0
  2294. { pts: 6750, ccData: 0x1150, type: 0 },
  2295. // TO2 (tab offset 2 columns)
  2296. { pts: 6755, ccData: 0x1722, type: 0 },
  2297. { ccData: characters('te'), type: 0 },
  2298. { ccData: characters('st'), type: 0 },
  2299. // EOC, End of Caption
  2300. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  2301. // RCL, resume caption loading
  2302. { ccData: 0x1420, type: 0 },
  2303. // EOC, End of Caption
  2304. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  2305. ];
  2306. packets.forEach(cea608Stream.push, cea608Stream);
  2307. assert.equal(captions[0].content[0].text, 'test', 'content text');
  2308. assert.equal(captions[0].content[0].line, 1, 'positions the caption to the bottom of the screen');
  2309. // Two tab offset columns adds 5 to the position (2 * 2.5)
  2310. assert.equal(captions[0].content[0].position, 15, 'positions the caption to the right');
  2311. });
  2312. QUnit.test('PAC row command ensures we have the correct line property for captions', function(assert) {
  2313. var captions = [];
  2314. cea608Stream.on('data', function(caption) {
  2315. captions.push(caption);
  2316. });
  2317. var packets = [
  2318. // RU2 (roll-up, 2 rows)
  2319. { pts: 6675, ccData: 0x1425, type: 0 },
  2320. // CR (carriange return), flush nothing
  2321. { pts: 6675, ccData: 0x142d, type: 0 },
  2322. // PAC: row 2, indent 0
  2323. // This should ensure the captions are at the top of the screen.
  2324. { pts: 6675, ccData: 0x1170, type: 0 },
  2325. // text: YEAR.
  2326. { pts: 6676, ccData: 0x5945, type: 0 },
  2327. { pts: 6676, ccData: 0x4152, type: 0 },
  2328. { pts: 6676, ccData: 0x2e00, type: 0 },
  2329. // RU2 (roll-up, 2 rows)
  2330. { pts: 6677, ccData: 0x1425, type: 0 },
  2331. // CR (carriange return), flush 1 row
  2332. { pts: 6677, ccData: 0x142d, type: 0 },
  2333. // EDM (erase displayed memory), flush 2 displayed roll-up rows
  2334. { pts: 6697, ccData: 0x142c, type: 0 },
  2335. // RDC (resume direct captioning), wipes memories, flushes nothing
  2336. { pts: 6749, ccData: 0x1429, type: 0 },
  2337. // PAC: row 1, indent 0
  2338. { pts: 6750, ccData: 0x1150, type: 0 },
  2339. // EOC, End of Caption
  2340. { pts: 1 * 1000, ccData: 0x142f, type: 0 },
  2341. // RCL, resume caption loading
  2342. { ccData: 0x1420, type: 0 },
  2343. // EOC, End of Caption
  2344. { pts: 2 * 1000, ccData: 0x142f, type: 0 }
  2345. ];
  2346. // First caption stream is at the second most bottom row.
  2347. packets.forEach(cea608Stream.push, cea608Stream);
  2348. assert.equal(captions[0].content[0].text, 'YEAR.', 'content text');
  2349. assert.equal(captions[0].content[0].line, 2, 'positions the caption in the second most bottom row');
  2350. assert.equal(captions[0].content[0].position, 10, 'position of the caption');
  2351. // Second caption stream is at the most bottom row.
  2352. assert.equal(captions[1].content[0].text, 'YEAR.', 'content text');
  2353. assert.equal(captions[1].content[0].line, 1, 'positions the caption in the most bottom row');
  2354. assert.equal(captions[1].content[0].position, 10, 'position of the caption');
  2355. });
  2356. QUnit.test('mix of all modes (extract from CNN)', function(assert) {
  2357. var captions = [];
  2358. cea608Stream.on('data', function(caption) {
  2359. captions.push(caption);
  2360. });
  2361. [
  2362. // RU2 (roll-up, 2 rows)
  2363. { pts: 6675, ccData: 0x1425, type: 0 },
  2364. // CR (carriange return), flush nothing
  2365. { pts: 6675, ccData: 0x142d, type: 0 },
  2366. // PAC: row 2, indent 0
  2367. { pts: 6675, ccData: 0x1170, type: 0 },
  2368. // text: YEAR.
  2369. { pts: 6676, ccData: 0x5945, type: 0 },
  2370. { pts: 6676, ccData: 0x4152, type: 0 },
  2371. { pts: 6676, ccData: 0x2e00, type: 0 },
  2372. // RU2 (roll-up, 2 rows)
  2373. { pts: 6677, ccData: 0x1425, type: 0 },
  2374. // CR (carriange return), flush 1 row
  2375. { pts: 6677, ccData: 0x142d, type: 0 },
  2376. // PAC: row 2, indent 0
  2377. { pts: 6677, ccData: 0x1170, type: 0 },
  2378. // text: GO TO CNNHEROS.COM.
  2379. { pts: 6677, ccData: 0x474f, type: 0 },
  2380. { pts: 6678, ccData: 0x2054, type: 0 },
  2381. { pts: 6678, ccData: 0x4f00, type: 0 },
  2382. { pts: 6678, ccData: 0x2043, type: 0 },
  2383. { pts: 6679, ccData: 0x4e4e, type: 0 },
  2384. { pts: 6679, ccData: 0x4845, type: 0 },
  2385. { pts: 6679, ccData: 0x524f, type: 0 },
  2386. { pts: 6680, ccData: 0x532e, type: 0 },
  2387. { pts: 6680, ccData: 0x434f, type: 0 },
  2388. { pts: 6680, ccData: 0x4d2e, type: 0 },
  2389. // EDM (erase displayed memory), flush 2 displayed roll-up rows
  2390. { pts: 6697, ccData: 0x142c, type: 0 },
  2391. // RDC (resume direct captioning), wipes memories, flushes nothing
  2392. { pts: 6749, ccData: 0x1429, type: 0 },
  2393. // PAC: row 1, indent 0
  2394. { pts: 6750, ccData: 0x1150, type: 0 },
  2395. // text: Did your Senator or Congressman
  2396. { pts: 6750, ccData: 0x4469, type: 0 },
  2397. { pts: 6750, ccData: 0x6420, type: 0 },
  2398. { pts: 6750, ccData: 0x796f, type: 0 },
  2399. { pts: 6751, ccData: 0x7572, type: 0 },
  2400. { pts: 6751, ccData: 0x2053, type: 0 },
  2401. { pts: 6751, ccData: 0x656e, type: 0 },
  2402. { pts: 6752, ccData: 0x6174, type: 0 },
  2403. { pts: 6752, ccData: 0x6f72, type: 0 },
  2404. { pts: 6752, ccData: 0x206f, type: 0 },
  2405. { pts: 6753, ccData: 0x7220, type: 0 },
  2406. { pts: 6753, ccData: 0x436f, type: 0 },
  2407. { pts: 6753, ccData: 0x6e67, type: 0 },
  2408. { pts: 6753, ccData: 0x7265, type: 0 },
  2409. { pts: 6754, ccData: 0x7373, type: 0 },
  2410. { pts: 6754, ccData: 0x6d61, type: 0 },
  2411. { pts: 6754, ccData: 0x6e00, type: 0 },
  2412. // PAC: row 2, indent 0
  2413. { pts: 6755, ccData: 0x1170, type: 0 },
  2414. // TO2 (tab offset 2 columns)
  2415. { pts: 6755, ccData: 0x1722, type: 0 },
  2416. // text: get elected by talking tough
  2417. { pts: 6755, ccData: 0x6765, type: 0 },
  2418. { pts: 6756, ccData: 0x7420, type: 0 },
  2419. { pts: 6756, ccData: 0x656c, type: 0 },
  2420. { pts: 6756, ccData: 0x6563, type: 0 },
  2421. { pts: 6756, ccData: 0x7465, type: 0 },
  2422. { pts: 6757, ccData: 0x6420, type: 0 },
  2423. { pts: 6757, ccData: 0x6279, type: 0 },
  2424. { pts: 6757, ccData: 0x2074, type: 0 },
  2425. { pts: 6758, ccData: 0x616c, type: 0 },
  2426. { pts: 6758, ccData: 0x6b69, type: 0 },
  2427. { pts: 6758, ccData: 0x6e67, type: 0 },
  2428. { pts: 6759, ccData: 0x2074, type: 0 },
  2429. { pts: 6759, ccData: 0x6f75, type: 0 },
  2430. { pts: 6759, ccData: 0x6768, type: 0 },
  2431. // RCL (resume caption loading)
  2432. { pts: 6759, ccData: 0x1420, type: 0 },
  2433. // PAC: row 1, indent 4
  2434. { pts: 6760, ccData: 0x1152, type: 0 },
  2435. // TO1 (tab offset 1 column)
  2436. { pts: 6760, ccData: 0x1721, type: 0 },
  2437. // text: on the national debt?
  2438. { pts: 6760, ccData: 0x6f6e, type: 0 },
  2439. { pts: 6761, ccData: 0x2074, type: 0 },
  2440. { pts: 6761, ccData: 0x6865, type: 0 },
  2441. { pts: 6761, ccData: 0x206e, type: 0 },
  2442. { pts: 6762, ccData: 0x6174, type: 0 },
  2443. { pts: 6762, ccData: 0x696f, type: 0 },
  2444. { pts: 6762, ccData: 0x6e61, type: 0 },
  2445. { pts: 6762, ccData: 0x6c20, type: 0 },
  2446. { pts: 6763, ccData: 0x6465, type: 0 },
  2447. { pts: 6763, ccData: 0x6274, type: 0 },
  2448. { pts: 6763, ccData: 0x3f00, type: 0 },
  2449. // RCL (resume caption loading)
  2450. { pts: 6781, ccData: 0x1420, type: 0 },
  2451. // EDM (erase displayed memory), flush paint-on caption
  2452. { pts: 6781, ccData: 0x142c, type: 0 },
  2453. // EOC (end of caption), mark pop-on caption 1 start
  2454. { pts: 6782, ccData: 0x142f, type: 0 },
  2455. // RCL (resume caption loading)
  2456. { pts: 6782, ccData: 0x1420, type: 0 },
  2457. // PAC: row 1, indent 4
  2458. { pts: 6782, ccData: 0x1152, type: 0 },
  2459. // TO2 (tab offset 2 columns)
  2460. { pts: 6783, ccData: 0x1722, type: 0 },
  2461. // text: Will they stay true
  2462. { pts: 6783, ccData: 0x5769, type: 0 },
  2463. { pts: 6783, ccData: 0x6c6c, type: 0 },
  2464. { pts: 6783, ccData: 0x2074, type: 0 },
  2465. { pts: 6784, ccData: 0x6865, type: 0 },
  2466. { pts: 6784, ccData: 0x7920, type: 0 },
  2467. { pts: 6784, ccData: 0x7374, type: 0 },
  2468. { pts: 6785, ccData: 0x6179, type: 0 },
  2469. { pts: 6785, ccData: 0x2074, type: 0 },
  2470. { pts: 6785, ccData: 0x7275, type: 0 },
  2471. { pts: 6786, ccData: 0x6500, type: 0 },
  2472. // PAC: row 2, indent 8
  2473. { pts: 6786, ccData: 0x1174, type: 0 },
  2474. // text: to their words?
  2475. { pts: 6786, ccData: 0x746f, type: 0 },
  2476. { pts: 6786, ccData: 0x2074, type: 0 },
  2477. { pts: 6787, ccData: 0x6865, type: 0 },
  2478. { pts: 6787, ccData: 0x6972, type: 0 },
  2479. { pts: 6787, ccData: 0x2077, type: 0 },
  2480. { pts: 6788, ccData: 0x6f72, type: 0 },
  2481. { pts: 6788, ccData: 0x6473, type: 0 },
  2482. { pts: 6788, ccData: 0x3f00, type: 0 },
  2483. // RCL (resume caption loading)
  2484. { pts: 6797, ccData: 0x1420, type: 0 },
  2485. // EDM (erase displayed memory), mark pop-on caption 1 end and flush
  2486. { pts: 6797, ccData: 0x142c, type: 0 },
  2487. // EOC (end of caption), mark pop-on caption 2 start, flush nothing
  2488. { pts: 6798, ccData: 0x142f, type: 0 },
  2489. // RCL
  2490. { pts: 6799, ccData: 0x1420, type: 0 },
  2491. // EOC, mark pop-on caption 2 end and flush
  2492. { pts: 6838, ccData: 0x142f, type: 0 },
  2493. // RU2 (roll-up, 2 rows), wipes memories
  2494. { pts: 6841, ccData: 0x1425, type: 0 },
  2495. // CR (carriage return), flush nothing
  2496. { pts: 6841, ccData: 0x142d, type: 0 },
  2497. // PAC: row 2, indent 0
  2498. { pts: 6841, ccData: 0x1170, type: 0 },
  2499. // text: NO MORE SPECULATION, NO MORE
  2500. { pts: 6841, ccData: 0x3e3e, type: 0 },
  2501. { pts: 6841, ccData: 0x3e00, type: 0 },
  2502. { pts: 6842, ccData: 0x204e, type: 0 },
  2503. { pts: 6842, ccData: 0x4f00, type: 0 },
  2504. { pts: 6842, ccData: 0x204d, type: 0 },
  2505. { pts: 6842, ccData: 0x4f52, type: 0 },
  2506. { pts: 6842, ccData: 0x4500, type: 0 },
  2507. { pts: 6842, ccData: 0x2000, type: 0 },
  2508. { pts: 6842, ccData: 0x5350, type: 0 },
  2509. { pts: 6843, ccData: 0x4543, type: 0 },
  2510. { pts: 6843, ccData: 0x554c, type: 0 },
  2511. { pts: 6843, ccData: 0x4154, type: 0 },
  2512. { pts: 6843, ccData: 0x494f, type: 0 },
  2513. { pts: 6843, ccData: 0x4e2c, type: 0 },
  2514. { pts: 6843, ccData: 0x204e, type: 0 },
  2515. { pts: 6843, ccData: 0x4f00, type: 0 },
  2516. { pts: 6843, ccData: 0x204d, type: 0 },
  2517. { pts: 6844, ccData: 0x4f52, type: 0 },
  2518. { pts: 6844, ccData: 0x4500, type: 0 },
  2519. // RU2 (roll-up, two rows)
  2520. { pts: 6844, ccData: 0x1425, type: 0 },
  2521. // CR (carriage return), flush 1 roll-up row
  2522. { pts: 6844, ccData: 0x142d, type: 0 },
  2523. // PAC: row 2, indent 0
  2524. { pts: 6844, ccData: 0x1170, type: 0 },
  2525. // text: RUMORS OR GUESSING GAMES.
  2526. { pts: 6844, ccData: 0x5255, type: 0 },
  2527. { pts: 6844, ccData: 0x4d4f, type: 0 },
  2528. { pts: 6844, ccData: 0x5253, type: 0 },
  2529. { pts: 6844, ccData: 0x204f, type: 0 },
  2530. { pts: 6845, ccData: 0x5200, type: 0 },
  2531. { pts: 6845, ccData: 0x2047, type: 0 },
  2532. { pts: 6845, ccData: 0x5545, type: 0 },
  2533. { pts: 6845, ccData: 0x5353, type: 0 },
  2534. { pts: 6845, ccData: 0x494e, type: 0 },
  2535. { pts: 6845, ccData: 0x4700, type: 0 },
  2536. { pts: 6845, ccData: 0x2047, type: 0 },
  2537. { pts: 6845, ccData: 0x414d, type: 0 },
  2538. { pts: 6845, ccData: 0x4553, type: 0 },
  2539. { pts: 6845, ccData: 0x2e00, type: 0 },
  2540. // RU2 (roll-up, 2 rows)
  2541. { pts: 6846, ccData: 0x1425, type: 0 },
  2542. // CR (carriage return), flush 2 roll-up rows
  2543. { pts: 6846, ccData: 0x142d, type: 0 }
  2544. ].forEach(cea608Stream.push, cea608Stream);
  2545. assert.equal(captions.length, 7, 'detected 7 captions of varying types');
  2546. assert.deepEqual(captions[0], {
  2547. content: [{
  2548. line: 2,
  2549. position: 10,
  2550. text: 'YEAR.',
  2551. }],
  2552. startPts: 6675,
  2553. endPts: 6677,
  2554. stream: 'CC1'
  2555. }, 'parsed the 1st roll-up caption');
  2556. assert.deepEqual(captions[1], {
  2557. content: [
  2558. {
  2559. line: 1,
  2560. position: 10,
  2561. text: 'YEAR.',
  2562. },
  2563. {
  2564. line: 2,
  2565. position: 10,
  2566. text: 'GO TO CNNHEROS.COM.',
  2567. }
  2568. ],
  2569. startPts: 6677,
  2570. endPts: 6697,
  2571. stream: 'CC1'
  2572. }, 'parsed the 2nd roll-up caption');
  2573. assert.deepEqual(captions[2], {
  2574. content: [
  2575. {
  2576. line: 1,
  2577. position: 10,
  2578. text: 'Did your Senator or Congressman',
  2579. },
  2580. {
  2581. line: 2,
  2582. position: 10,
  2583. text: 'get elected by talking tough',
  2584. }
  2585. ],
  2586. startPts: 6749,
  2587. endPts: 6781,
  2588. stream: 'CC1'
  2589. }, 'parsed the paint-on caption');
  2590. assert.deepEqual(captions[3], {
  2591. content: [{
  2592. line: 1,
  2593. position: 22.5,
  2594. text: 'on the national debt?',
  2595. }],
  2596. startPts: 6782,
  2597. endPts: 6797,
  2598. stream: 'CC1'
  2599. }, 'parsed the 1st pop-on caption');
  2600. assert.deepEqual(captions[4], {
  2601. content: [
  2602. {
  2603. line: 1,
  2604. position: 25,
  2605. text: 'Will they stay true',
  2606. },
  2607. {
  2608. line: 2,
  2609. position: 30,
  2610. text: 'to their words?',
  2611. }
  2612. ],
  2613. startPts: 6798,
  2614. endPts: 6838,
  2615. stream: 'CC1'
  2616. }, 'parsed the 2nd pop-on caption');
  2617. assert.deepEqual(captions[5], {
  2618. content: [{
  2619. line: 2,
  2620. position: 10,
  2621. text: '>>> NO MORE SPECULATION, NO MORE',
  2622. }],
  2623. startPts: 6841,
  2624. endPts: 6844,
  2625. stream: 'CC1'
  2626. }, 'parsed the 3rd roll-up caption');
  2627. assert.deepEqual(captions[6], {
  2628. content: [
  2629. {
  2630. line: 1,
  2631. position: 10,
  2632. text: '>>> NO MORE SPECULATION, NO MORE',
  2633. },
  2634. {
  2635. line: 2,
  2636. position: 10,
  2637. text: 'RUMORS OR GUESSING GAMES.',
  2638. }
  2639. ],
  2640. startPts: 6844,
  2641. endPts: 6846,
  2642. stream: 'CC1'
  2643. }, 'parsed the 4th roll-up caption');
  2644. });
  2645. QUnit.test('Cea608Stream will trigger log on malformed captions', function(assert) {
  2646. var result;
  2647. var logs = [];
  2648. cea608Stream.on('log', function(log) {
  2649. logs.push(log);
  2650. })
  2651. // this will force an exception to happen in flushDisplayed
  2652. cea608Stream.displayed_[0] = undefined;
  2653. try {
  2654. cea608Stream.flushDisplayed();
  2655. result = true;
  2656. } catch (e) {
  2657. result = false;
  2658. }
  2659. assert.ok(
  2660. result,
  2661. 'the function does not throw an exception'
  2662. );
  2663. assert.deepEqual(
  2664. logs,
  2665. [
  2666. {level: 'warn', message: 'Skipping a malformed 608 caption at index 0.'}
  2667. ],
  2668. 'logs were triggered'
  2669. );
  2670. });
  2671. var cea708Stream;
  2672. QUnit.module('CEA 708 Stream', {
  2673. beforeEach: function() {
  2674. cea708Stream = new m2ts.Cea708Stream();
  2675. }
  2676. });
  2677. QUnit.test('Filters encoding values out of captionServices option block', function(assert) {
  2678. var expectedServiceEncodings = {
  2679. SERVICE1: 'euc-kr',
  2680. SERVICE2: 'utf-8',
  2681. };
  2682. cea708Stream = new m2ts.Cea708Stream({
  2683. captionServices: {
  2684. SERVICE1: {
  2685. language: 'kr',
  2686. label: 'Korean',
  2687. encoding: 'euc-kr'
  2688. },
  2689. SERVICE2: {
  2690. language: 'en',
  2691. label: 'English',
  2692. encoding: 'utf-8'
  2693. }
  2694. }
  2695. });
  2696. assert.deepEqual(cea708Stream.serviceEncodings, expectedServiceEncodings, 'filtered encodings correctly');
  2697. });
  2698. QUnit.test('parses 708 captions', function(assert) {
  2699. var captions = [];
  2700. cea708Stream.on('data', function(caption) {
  2701. captions.push(caption);
  2702. });
  2703. cc708PinkUnderscore.forEach(cea708Stream.push, cea708Stream);
  2704. assert.equal(captions.length, 235, 'parsed 235 captions');
  2705. assert.deepEqual(captions[0], {
  2706. startPts: 6723335478,
  2707. endPts: 6723626769,
  2708. text: '\"Pinkalicious_and_Peterrific\"\nis_made_possible_in_part_by:',
  2709. stream: 'cc708_1'
  2710. }, 'parsed first caption correctly');
  2711. assert.deepEqual(captions[1], {
  2712. startPts: 6723740883,
  2713. endPts: 6723945087,
  2714. text: 'GIRL:\nRead_me_the_tale\nof_a_faraway_land.',
  2715. stream: 'cc708_1'
  2716. }, 'parsed second caption correctly');
  2717. assert.deepEqual(captions[2], {
  2718. startPts: 6723948090,
  2719. endPts: 6724200342,
  2720. text: 'Tell_me_of_planets\nwith_oceans_of_sand.',
  2721. stream: 'cc708_1'
  2722. }, 'parsed third caption correctly');
  2723. assert.deepEqual(captions[33], {
  2724. startPts: 6732617751,
  2725. endPts: 6732876009,
  2726. text: '♪_It\'s_a_Pinkalicious_feeling_♪',
  2727. stream: 'cc708_1'
  2728. }, 'parsed caption 33 correctly with music note');
  2729. assert.deepEqual(captions[38], {
  2730. startPts: 6734218350,
  2731. endPts: 6734425557,
  2732. text: 'PINKALICIOUS:\n\"Dream_Salon.\"',
  2733. stream: 'cc708_1'
  2734. }, 'parsed caption 38 correctly');
  2735. assert.deepEqual(captions[234], {
  2736. startPts: 6778809897,
  2737. endPts: 6779104191,
  2738. text: 'I_guess_I\'ll_just_have\nto_duck_a_little_bit.',
  2739. stream: 'cc708_1'
  2740. }, 'parsed caption 234 correctly');
  2741. });
  2742. QUnit.test('Decodes multibyte characters if valid encoding option is provided and TextDecoder is supported', function(assert) {
  2743. var captions = [];
  2744. cea708Stream = new m2ts.Cea708Stream({
  2745. captionServices: {
  2746. SERVICE1: {
  2747. encoding: 'euc-kr'
  2748. }
  2749. }
  2750. });
  2751. cea708Stream.on('data', function(caption) {
  2752. captions.push(caption);
  2753. });
  2754. cc708Korean.forEach(cea708Stream.push, cea708Stream);
  2755. cea708Stream.flushDisplayed(4721138662, cea708Stream.services[1]);
  2756. assert.equal(captions.length, 1, 'parsed single caption correctly');
  2757. if (window.TextDecoder) {
  2758. assert.ok(cea708Stream.services[1].textDecoder_, 'TextDecoder created when supported');
  2759. assert.equal(
  2760. captions[0].text,
  2761. '니가 ',
  2762. 'parsed multibyte characters correctly'
  2763. );
  2764. } else {
  2765. assert.notOk(cea708Stream.services[1].textDecoder_, 'TextDecoder not created when unsupported');
  2766. }
  2767. });
  2768. QUnit.test('Decodes multi-byte characters as unicode if no valid encoding option is provided', function(assert) {
  2769. var captions = [];
  2770. cea708Stream = new m2ts.Cea708Stream({
  2771. captionServices: {
  2772. SERVICE1: {}
  2773. }
  2774. });
  2775. cea708Stream.on('data', function(caption) {
  2776. captions.push(caption);
  2777. });
  2778. cc708Korean.forEach(cea708Stream.push, cea708Stream);
  2779. cea708Stream.flushDisplayed(4721138662, cea708Stream.services[1]);
  2780. assert.equal(captions.length, 1, 'parsed single caption correctly');
  2781. assert.notOk(cea708Stream.services[1].textDecoder_, 'TextDecoder was not created');
  2782. assert.equal(
  2783. captions[0].text,
  2784. '듏낡 ',
  2785. 'parsed multibyte characters correctly'
  2786. );
  2787. });
  2788. QUnit.test('Creates TextDecoder only if valid encoding value is provided', function(assert) {
  2789. var secondCea708Stream;
  2790. cea708Stream = new m2ts.Cea708Stream({
  2791. captionServices: {
  2792. SERVICE1: {
  2793. encoding: 'euc-kr'
  2794. }
  2795. }
  2796. });
  2797. cc708Korean.forEach(cea708Stream.push, cea708Stream);
  2798. cea708Stream.flushDisplayed(4721138662, cea708Stream.services[1]);
  2799. if (window.TextDecoder) {
  2800. assert.ok(cea708Stream.services[1].textDecoder_, 'TextDecoder created successfully when encoding is valid');
  2801. }
  2802. secondCea708Stream = new m2ts.Cea708Stream({
  2803. captionServices: {
  2804. SERVICE1: {
  2805. encoding: 'invalid'
  2806. }
  2807. }
  2808. });
  2809. cc708Korean.forEach(secondCea708Stream.push, secondCea708Stream);
  2810. secondCea708Stream.flushDisplayed(4721138662, secondCea708Stream.services[1]);
  2811. assert.notOk(secondCea708Stream.services[1].textDecoder_, 'TextDecoder not created when encoding is invalid');
  2812. });
  2813. QUnit.test('reset command', function(assert) {
  2814. var captions = [];
  2815. cea708Stream.on('data', function(caption) {
  2816. captions.push(caption);
  2817. });
  2818. [
  2819. { type: 3, pts: 153315036, ccData: 0x8322 },
  2820. { type: 2, pts: 153315036, ccData: 0x4820 },
  2821. { type: 2, pts: 153315036, ccData: 0x0000 },
  2822. { type: 3, pts: 153318039, ccData: 0xc322 },
  2823. { type: 2, pts: 153318039, ccData: 0x4953 },
  2824. { type: 2, pts: 153318039, ccData: 0x0000 },
  2825. { type: 3, pts: 153387108, ccData: 0x0628 },
  2826. { type: 2, pts: 153387108, ccData: 0x0d90 },
  2827. { type: 2, pts: 153387108, ccData: 0x0503 },
  2828. { type: 2, pts: 153387108, ccData: 0x912a },
  2829. { type: 2, pts: 153387108, ccData: 0x002a },
  2830. { type: 2, pts: 153387108, ccData: 0x0000 },
  2831. { type: 3, pts: 153405126, ccData: 0x4da2 },
  2832. { type: 2, pts: 153405126, ccData: 0x8c0f },
  2833. { type: 2, pts: 153405126, ccData: 0x628c },
  2834. { type: 2, pts: 153405126, ccData: 0x0f31 },
  2835. { type: 2, pts: 153405126, ccData: 0x983b },
  2836. { type: 2, pts: 153405126, ccData: 0x912a },
  2837. { type: 2, pts: 153405126, ccData: 0x8f00 },
  2838. { type: 2, pts: 153405126, ccData: 0x611f },
  2839. { type: 2, pts: 153405126, ccData: 0x002a },
  2840. { type: 2, pts: 153405126, ccData: 0x1090 },
  2841. { type: 2, pts: 153405126, ccData: 0x0503 },
  2842. { type: 3, pts: 153408129, ccData: 0x8a31 },
  2843. { type: 2, pts: 153408129, ccData: 0x9201 },
  2844. { type: 2, pts: 153408129, ccData: 0x983b },
  2845. // RST (Reset command)
  2846. { type: 2, pts: 153408129, ccData: 0x8f00 },
  2847. { type: 2, pts: 153408129, ccData: 0x0000 },
  2848. { type: 2, pts: 153408129, ccData: 0x611f },
  2849. { type: 2, pts: 153408129, ccData: 0x1090 },
  2850. { type: 2, pts: 153408129, ccData: 0x0000 },
  2851. { type: 2, pts: 153408129, ccData: 0x0503 },
  2852. { type: 2, pts: 153408129, ccData: 0x912a },
  2853. { type: 2, pts: 153408129, ccData: 0x0000 },
  2854. { type: 2, pts: 153408129, ccData: 0x9201 },
  2855. { type: 3, pts: 153414135, ccData: 0xc322 },
  2856. { type: 2, pts: 153414135, ccData: 0x434f },
  2857. { type: 2, pts: 153414135, ccData: 0x0000 }
  2858. ].forEach(cea708Stream.push, cea708Stream);
  2859. assert.equal(captions.length, 1, 'parsed 1 caption');
  2860. assert.deepEqual(captions[0], {
  2861. startPts: 153315036,
  2862. endPts: 153408129,
  2863. text: '*\n;',
  2864. stream: 'cc708_1'
  2865. }, 'parsed the caption correctly');
  2866. });
  2867. QUnit.test('windowing', function(assert) {
  2868. var captions = [];
  2869. cea708Stream.on('data', function(caption) {
  2870. captions.push(caption);
  2871. });
  2872. [
  2873. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2874. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2875. { type: 2, pts: 1000, ccData: characters('w0') },
  2876. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2877. { type: 2, pts: 1000, ccData: 0x8100 }, // CW1
  2878. { type: 2, pts: 1000, ccData: characters('w1') },
  2879. { type: 3, pts: 1000, ccData: packetHeader708(2, 3, 1, 4) },
  2880. { type: 2, pts: 1000, ccData: 0x8200 }, // CW2
  2881. { type: 2, pts: 1000, ccData: characters('w2') },
  2882. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2883. { type: 2, pts: 1000, ccData: 0x8300 }, // CW3
  2884. { type: 2, pts: 1000, ccData: characters('w3') },
  2885. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2886. { type: 2, pts: 1000, ccData: 0x8400 }, // CW4
  2887. { type: 2, pts: 1000, ccData: characters('w4') },
  2888. { type: 3, pts: 1000, ccData: packetHeader708(2, 3, 1, 4) },
  2889. { type: 2, pts: 1000, ccData: 0x8500 }, // CW5
  2890. { type: 2, pts: 1000, ccData: characters('w5') },
  2891. { type: 3, pts: 1000, ccData: packetHeader708(0, 3, 1, 4) },
  2892. { type: 2, pts: 1000, ccData: 0x8600 }, // CW6
  2893. { type: 2, pts: 1000, ccData: characters('w6') },
  2894. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  2895. { type: 2, pts: 1000, ccData: 0x8700 }, // CW7
  2896. { type: 2, pts: 1000, ccData: characters('w7') },
  2897. { type: 3, pts: 2000, ccData: packetHeader708(2, 3, 1, 4) },
  2898. { type: 2, pts: 2000, ccData: 0x8aff }, // HDW (Hide all)
  2899. { type: 2, pts: 2000, ccData: displayWindows708([0]) },
  2900. { type: 3, pts: 3000, ccData: packetHeader708(0, 3, 1, 4) },
  2901. { type: 2, pts: 3000, ccData: 0x8aff }, // HDW (Hide all)
  2902. { type: 2, pts: 3000, ccData: displayWindows708([1]) },
  2903. { type: 3, pts: 4000, ccData: packetHeader708(1, 3, 1, 4) },
  2904. { type: 2, pts: 4000, ccData: 0x8aff }, // HDW (Hide all)
  2905. { type: 2, pts: 4000, ccData: displayWindows708([2, 3]) },
  2906. { type: 3, pts: 5000, ccData: packetHeader708(2, 3, 1, 4) },
  2907. { type: 2, pts: 5000, ccData: 0x8aff }, // HDW (Hide all)
  2908. { type: 2, pts: 5000, ccData: displayWindows708([3, 4]) },
  2909. { type: 3, pts: 6000, ccData: packetHeader708(0, 3, 1, 4) },
  2910. { type: 2, pts: 6000, ccData: 0x8aff }, // HDW (Hide all)
  2911. { type: 2, pts: 6000, ccData: displayWindows708([5, 6, 7]) },
  2912. { type: 3, pts: 7000, ccData: packetHeader708(1, 2, 1, 2) },
  2913. { type: 2, pts: 7000, ccData: 0x8aff }, // HDW (Hide all)
  2914. // Indicate end of last packet
  2915. { type: 3, pts: 8000, ccData: packetHeader708(2, 1, 1, 0) }
  2916. ].forEach(cea708Stream.push, cea708Stream);
  2917. assert.equal(captions.length, 5, 'parsed 5 captions');
  2918. assert.deepEqual(captions[0], {
  2919. startPts: 2000,
  2920. endPts: 3000,
  2921. text: 'w0',
  2922. stream: 'cc708_1'
  2923. }, 'parsed caption 0 correctly');
  2924. assert.deepEqual(captions[1], {
  2925. startPts: 3000,
  2926. endPts: 4000,
  2927. text: 'w1',
  2928. stream: 'cc708_1'
  2929. }, 'parsed caption 1 correctly');
  2930. assert.deepEqual(captions[2], {
  2931. startPts: 4000,
  2932. endPts: 5000,
  2933. text: 'w2\n\nw3',
  2934. stream: 'cc708_1'
  2935. }, 'parsed caption 2 correctly');
  2936. assert.deepEqual(captions[3], {
  2937. startPts: 5000,
  2938. endPts: 6000,
  2939. text: 'w3\n\nw4',
  2940. stream: 'cc708_1'
  2941. }, 'parsed caption 3 correctly');
  2942. assert.deepEqual(captions[4], {
  2943. startPts: 6000,
  2944. endPts: 7000,
  2945. text: 'w5\n\nw6\n\nw7',
  2946. stream: 'cc708_1'
  2947. }, 'parsed caption 4 correctly');
  2948. });
  2949. QUnit.test('backspace', function(assert) {
  2950. var captions = [];
  2951. cea708Stream.on('data', function(caption) {
  2952. captions.push(caption);
  2953. });
  2954. [
  2955. { type: 3, pts: 1000, ccData: packetHeader708(0, 7, 1, 12) },
  2956. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2957. { type: 2, pts: 1000, ccData: characters('ty') },
  2958. { type: 2, pts: 1000, ccData: characters('op') },
  2959. { type: 2, pts: 1000, ccData: 0x0808 }, // BS BS: Backspace twice
  2960. { type: 2, pts: 1000, ccData: characters('po') },
  2961. { type: 2, pts: 1000, ccData: displayWindows708([0]) },
  2962. { type: 3, pts: 2000, ccData: packetHeader708(1, 2, 1, 2) },
  2963. { type: 2, pts: 2000, ccData: 0x8aff },
  2964. // Indicate end of last packet
  2965. { type: 3, pts: 3000, ccData: packetHeader708(2, 1, 1, 0) }
  2966. ].forEach(cea708Stream.push, cea708Stream);
  2967. assert.equal(captions.length, 1, 'parsed 1 caption');
  2968. assert.equal(captions[0].text, 'typo', 'parsed caption with backspaces correctly');
  2969. });
  2970. QUnit.test('extended character set', function(assert) {
  2971. var captions = [];
  2972. cea708Stream.on('data', function(caption) {
  2973. captions.push(caption);
  2974. });
  2975. [
  2976. { type: 3, pts: 1000, ccData: packetHeader708(0, 7, 1, 12) },
  2977. { type: 2, pts: 1000, ccData: displayWindows708([0]) },
  2978. { type: 2, pts: 1000, ccData: 0x8000 }, // CW0
  2979. { type: 2, pts: 1000, ccData: 0x103f }, // Ÿ
  2980. { type: 2, pts: 1000, ccData: 0x1035 }, // •
  2981. { type: 2, pts: 1000, ccData: 0x103f }, // Ÿ
  2982. { type: 2, pts: 1000, ccData: 0x0020 },
  2983. { type: 3, pts: 1000, ccData: packetHeader708(1, 7, 1, 12) },
  2984. { type: 2, pts: 1000, ccData: 0x103c }, // œ
  2985. { type: 2, pts: 1000, ccData: 0x102a }, // Š
  2986. { type: 2, pts: 1000, ccData: 0x1025 }, // …
  2987. { type: 2, pts: 1000, ccData: 0x102a }, // Š
  2988. { type: 2, pts: 1000, ccData: 0x103c }, // œ
  2989. { type: 2, pts: 1000, ccData: 0x0020 },
  2990. { type: 3, pts: 1000, ccData: packetHeader708(2, 5, 1, 8) },
  2991. { type: 2, pts: 1000, ccData: 0x1033 }, // “
  2992. { type: 2, pts: 1000, ccData: 0x103d }, // ℠
  2993. { type: 2, pts: 1000, ccData: 0x1034 }, // ”
  2994. { type: 2, pts: 1000, ccData: 0x1039 }, // ™
  2995. { type: 3, pts: 2000, ccData: packetHeader708(0, 2, 1, 2) },
  2996. { type: 2, pts: 2000, ccData: 0x8aff },
  2997. // Indicate end of last packet
  2998. { type: 3, pts: 3000, ccData: packetHeader708(1, 1, 1, 0) }
  2999. ].forEach(cea708Stream.push, cea708Stream);
  3000. assert.equal(captions.length, 1, 'parsed 1 caption');
  3001. assert.equal(captions[0].text, 'Ÿ•Ÿ œŠ…Šœ “℠”™', 'parsed extended characters correctly');
  3002. });
  3003. QUnit.test('roll up', function(assert) {
  3004. var captions = [];
  3005. cea708Stream.on('data', function(caption) {
  3006. captions.push(caption);
  3007. });
  3008. [
  3009. // Define window with two virtual rows (rowCount = 1)
  3010. { type: 3, pts: 1000, ccData: packetHeader708(0, 4, 1, 6) },
  3011. { type: 2, pts: 1000, ccData: 0x983b },
  3012. { type: 2, pts: 1000, ccData: 0x8f00 },
  3013. { type: 2, pts: 1000, ccData: 0x611f },
  3014. { type: 3, pts: 1000, ccData: packetHeader708(1, 3, 1, 4) },
  3015. { type: 2, pts: 1000, ccData: characters('L1') },
  3016. { type: 2, pts: 1000, ccData: 0x0d00 }, // CR
  3017. { type: 3, pts: 2000, ccData: packetHeader708(2, 3, 1, 4) },
  3018. { type: 2, pts: 2000, ccData: characters('L2') },
  3019. { type: 2, pts: 2000, ccData: 0x0d00 }, // CR
  3020. { type: 3, pts: 3000, ccData: packetHeader708(0, 3, 1, 4) },
  3021. { type: 2, pts: 3000, ccData: characters('L3') },
  3022. { type: 2, pts: 3000, ccData: 0x0d00 }, // CR
  3023. { type: 3, pts: 4000, ccData: packetHeader708(1, 3, 1, 4) },
  3024. { type: 2, pts: 4000, ccData: characters('L4') },
  3025. { type: 2, pts: 4000, ccData: 0x0d00 }, // CR
  3026. { type: 3, pts: 5000, ccData: packetHeader708(2, 2, 1, 2) },
  3027. { type: 2, pts: 5000, ccData: 0x8aff },
  3028. // Indicate end of last packet
  3029. { type: 3, pts: 6000, ccData: packetHeader708(0, 1, 1, 0) }
  3030. ].forEach(cea708Stream.push, cea708Stream);
  3031. assert.equal(captions.length, 3, 'parsed 3 captions');
  3032. assert.equal(captions[0].text, 'L1\nL2', 'parsed caption 1 correctly');
  3033. assert.equal(captions[1].text, 'L2\nL3', 'parsed caption 2 correctly');
  3034. assert.equal(captions[2].text, 'L3\nL4', 'parsed caption 3 correctly');
  3035. });