mux.js 359 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407
  1. /*! @name mux.js @version 6.3.0 @license Apache-2.0 */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('global/window')) :
  4. typeof define === 'function' && define.amd ? define(['global/window'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.muxjs = factory(global.window));
  6. }(this, (function (window) { 'use strict';
  7. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  8. var window__default = /*#__PURE__*/_interopDefaultLegacy(window);
  9. /**
  10. * mux.js
  11. *
  12. * Copyright (c) Brightcove
  13. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  14. *
  15. * A lightweight readable stream implemention that handles event dispatching.
  16. * Objects that inherit from streams should call init in their constructors.
  17. */
  18. var Stream = function Stream() {
  19. this.init = function () {
  20. var listeners = {};
  21. /**
  22. * Add a listener for a specified event type.
  23. * @param type {string} the event name
  24. * @param listener {function} the callback to be invoked when an event of
  25. * the specified type occurs
  26. */
  27. this.on = function (type, listener) {
  28. if (!listeners[type]) {
  29. listeners[type] = [];
  30. }
  31. listeners[type] = listeners[type].concat(listener);
  32. };
  33. /**
  34. * Remove a listener for a specified event type.
  35. * @param type {string} the event name
  36. * @param listener {function} a function previously registered for this
  37. * type of event through `on`
  38. */
  39. this.off = function (type, listener) {
  40. var index;
  41. if (!listeners[type]) {
  42. return false;
  43. }
  44. index = listeners[type].indexOf(listener);
  45. listeners[type] = listeners[type].slice();
  46. listeners[type].splice(index, 1);
  47. return index > -1;
  48. };
  49. /**
  50. * Trigger an event of the specified type on this stream. Any additional
  51. * arguments to this function are passed as parameters to event listeners.
  52. * @param type {string} the event name
  53. */
  54. this.trigger = function (type) {
  55. var callbacks, i, length, args;
  56. callbacks = listeners[type];
  57. if (!callbacks) {
  58. return;
  59. } // Slicing the arguments on every invocation of this method
  60. // can add a significant amount of overhead. Avoid the
  61. // intermediate object creation for the common case of a
  62. // single callback argument
  63. if (arguments.length === 2) {
  64. length = callbacks.length;
  65. for (i = 0; i < length; ++i) {
  66. callbacks[i].call(this, arguments[1]);
  67. }
  68. } else {
  69. args = [];
  70. i = arguments.length;
  71. for (i = 1; i < arguments.length; ++i) {
  72. args.push(arguments[i]);
  73. }
  74. length = callbacks.length;
  75. for (i = 0; i < length; ++i) {
  76. callbacks[i].apply(this, args);
  77. }
  78. }
  79. };
  80. /**
  81. * Destroys the stream and cleans up.
  82. */
  83. this.dispose = function () {
  84. listeners = {};
  85. };
  86. };
  87. };
  88. /**
  89. * Forwards all `data` events on this stream to the destination stream. The
  90. * destination stream should provide a method `push` to receive the data
  91. * events as they arrive.
  92. * @param destination {stream} the stream that will receive all `data` events
  93. * @param autoFlush {boolean} if false, we will not call `flush` on the destination
  94. * when the current stream emits a 'done' event
  95. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  96. */
  97. Stream.prototype.pipe = function (destination) {
  98. this.on('data', function (data) {
  99. destination.push(data);
  100. });
  101. this.on('done', function (flushSource) {
  102. destination.flush(flushSource);
  103. });
  104. this.on('partialdone', function (flushSource) {
  105. destination.partialFlush(flushSource);
  106. });
  107. this.on('endedtimeline', function (flushSource) {
  108. destination.endTimeline(flushSource);
  109. });
  110. this.on('reset', function (flushSource) {
  111. destination.reset(flushSource);
  112. });
  113. return destination;
  114. }; // Default stream functions that are expected to be overridden to perform
  115. // actual work. These are provided by the prototype as a sort of no-op
  116. // implementation so that we don't have to check for their existence in the
  117. // `pipe` function above.
  118. Stream.prototype.push = function (data) {
  119. this.trigger('data', data);
  120. };
  121. Stream.prototype.flush = function (flushSource) {
  122. this.trigger('done', flushSource);
  123. };
  124. Stream.prototype.partialFlush = function (flushSource) {
  125. this.trigger('partialdone', flushSource);
  126. };
  127. Stream.prototype.endTimeline = function (flushSource) {
  128. this.trigger('endedtimeline', flushSource);
  129. };
  130. Stream.prototype.reset = function (flushSource) {
  131. this.trigger('reset', flushSource);
  132. };
  133. var stream = Stream;
  134. /**
  135. * mux.js
  136. *
  137. * Copyright (c) Brightcove
  138. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  139. */
  140. var ONE_SECOND_IN_TS$5 = 90000,
  141. // 90kHz clock
  142. secondsToVideoTs,
  143. secondsToAudioTs,
  144. videoTsToSeconds,
  145. audioTsToSeconds,
  146. audioTsToVideoTs,
  147. videoTsToAudioTs,
  148. metadataTsToSeconds;
  149. secondsToVideoTs = function secondsToVideoTs(seconds) {
  150. return seconds * ONE_SECOND_IN_TS$5;
  151. };
  152. secondsToAudioTs = function secondsToAudioTs(seconds, sampleRate) {
  153. return seconds * sampleRate;
  154. };
  155. videoTsToSeconds = function videoTsToSeconds(timestamp) {
  156. return timestamp / ONE_SECOND_IN_TS$5;
  157. };
  158. audioTsToSeconds = function audioTsToSeconds(timestamp, sampleRate) {
  159. return timestamp / sampleRate;
  160. };
  161. audioTsToVideoTs = function audioTsToVideoTs(timestamp, sampleRate) {
  162. return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
  163. };
  164. videoTsToAudioTs = function videoTsToAudioTs(timestamp, sampleRate) {
  165. return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
  166. };
  167. /**
  168. * Adjust ID3 tag or caption timing information by the timeline pts values
  169. * (if keepOriginalTimestamps is false) and convert to seconds
  170. */
  171. metadataTsToSeconds = function metadataTsToSeconds(timestamp, timelineStartPts, keepOriginalTimestamps) {
  172. return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
  173. };
  174. var clock = {
  175. ONE_SECOND_IN_TS: ONE_SECOND_IN_TS$5,
  176. secondsToVideoTs: secondsToVideoTs,
  177. secondsToAudioTs: secondsToAudioTs,
  178. videoTsToSeconds: videoTsToSeconds,
  179. audioTsToSeconds: audioTsToSeconds,
  180. audioTsToVideoTs: audioTsToVideoTs,
  181. videoTsToAudioTs: videoTsToAudioTs,
  182. metadataTsToSeconds: metadataTsToSeconds
  183. };
  184. var ONE_SECOND_IN_TS$4 = clock.ONE_SECOND_IN_TS;
  185. var _AdtsStream;
  186. var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  187. /*
  188. * Accepts a ElementaryStream and emits data events with parsed
  189. * AAC Audio Frames of the individual packets. Input audio in ADTS
  190. * format is unpacked and re-emitted as AAC frames.
  191. *
  192. * @see http://wiki.multimedia.cx/index.php?title=ADTS
  193. * @see http://wiki.multimedia.cx/?title=Understanding_AAC
  194. */
  195. _AdtsStream = function AdtsStream(handlePartialSegments) {
  196. var buffer,
  197. frameNum = 0;
  198. _AdtsStream.prototype.init.call(this);
  199. this.skipWarn_ = function (start, end) {
  200. this.trigger('log', {
  201. level: 'warn',
  202. message: "adts skiping bytes " + start + " to " + end + " in frame " + frameNum + " outside syncword"
  203. });
  204. };
  205. this.push = function (packet) {
  206. var i = 0,
  207. frameLength,
  208. protectionSkipBytes,
  209. oldBuffer,
  210. sampleCount,
  211. adtsFrameDuration;
  212. if (!handlePartialSegments) {
  213. frameNum = 0;
  214. }
  215. if (packet.type !== 'audio') {
  216. // ignore non-audio data
  217. return;
  218. } // Prepend any data in the buffer to the input data so that we can parse
  219. // aac frames the cross a PES packet boundary
  220. if (buffer && buffer.length) {
  221. oldBuffer = buffer;
  222. buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
  223. buffer.set(oldBuffer);
  224. buffer.set(packet.data, oldBuffer.byteLength);
  225. } else {
  226. buffer = packet.data;
  227. } // unpack any ADTS frames which have been fully received
  228. // for details on the ADTS header, see http://wiki.multimedia.cx/index.php?title=ADTS
  229. var skip; // We use i + 7 here because we want to be able to parse the entire header.
  230. // If we don't have enough bytes to do that, then we definitely won't have a full frame.
  231. while (i + 7 < buffer.length) {
  232. // Look for the start of an ADTS header..
  233. if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
  234. if (typeof skip !== 'number') {
  235. skip = i;
  236. } // If a valid header was not found, jump one forward and attempt to
  237. // find a valid ADTS header starting at the next byte
  238. i++;
  239. continue;
  240. }
  241. if (typeof skip === 'number') {
  242. this.skipWarn_(skip, i);
  243. skip = null;
  244. } // The protection skip bit tells us if we have 2 bytes of CRC data at the
  245. // end of the ADTS header
  246. protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2; // Frame length is a 13 bit integer starting 16 bits from the
  247. // end of the sync sequence
  248. // NOTE: frame length includes the size of the header
  249. frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
  250. sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
  251. adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$4 / ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2]; // If we don't have enough data to actually finish this ADTS frame,
  252. // then we have to wait for more data
  253. if (buffer.byteLength - i < frameLength) {
  254. break;
  255. } // Otherwise, deliver the complete AAC frame
  256. this.trigger('data', {
  257. pts: packet.pts + frameNum * adtsFrameDuration,
  258. dts: packet.dts + frameNum * adtsFrameDuration,
  259. sampleCount: sampleCount,
  260. audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
  261. channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
  262. samplerate: ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2],
  263. samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
  264. // assume ISO/IEC 14496-12 AudioSampleEntry default of 16
  265. samplesize: 16,
  266. // data is the frame without it's header
  267. data: buffer.subarray(i + 7 + protectionSkipBytes, i + frameLength)
  268. });
  269. frameNum++;
  270. i += frameLength;
  271. }
  272. if (typeof skip === 'number') {
  273. this.skipWarn_(skip, i);
  274. skip = null;
  275. } // remove processed bytes from the buffer.
  276. buffer = buffer.subarray(i);
  277. };
  278. this.flush = function () {
  279. frameNum = 0;
  280. this.trigger('done');
  281. };
  282. this.reset = function () {
  283. buffer = void 0;
  284. this.trigger('reset');
  285. };
  286. this.endTimeline = function () {
  287. buffer = void 0;
  288. this.trigger('endedtimeline');
  289. };
  290. };
  291. _AdtsStream.prototype = new stream();
  292. var adts = _AdtsStream;
  293. /**
  294. * mux.js
  295. *
  296. * Copyright (c) Brightcove
  297. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  298. */
  299. var ExpGolomb;
  300. /**
  301. * Parser for exponential Golomb codes, a variable-bitwidth number encoding
  302. * scheme used by h264.
  303. */
  304. ExpGolomb = function ExpGolomb(workingData) {
  305. var // the number of bytes left to examine in workingData
  306. workingBytesAvailable = workingData.byteLength,
  307. // the current word being examined
  308. workingWord = 0,
  309. // :uint
  310. // the number of bits left to examine in the current word
  311. workingBitsAvailable = 0; // :uint;
  312. // ():uint
  313. this.length = function () {
  314. return 8 * workingBytesAvailable;
  315. }; // ():uint
  316. this.bitsAvailable = function () {
  317. return 8 * workingBytesAvailable + workingBitsAvailable;
  318. }; // ():void
  319. this.loadWord = function () {
  320. var position = workingData.byteLength - workingBytesAvailable,
  321. workingBytes = new Uint8Array(4),
  322. availableBytes = Math.min(4, workingBytesAvailable);
  323. if (availableBytes === 0) {
  324. throw new Error('no bytes available');
  325. }
  326. workingBytes.set(workingData.subarray(position, position + availableBytes));
  327. workingWord = new DataView(workingBytes.buffer).getUint32(0); // track the amount of workingData that has been processed
  328. workingBitsAvailable = availableBytes * 8;
  329. workingBytesAvailable -= availableBytes;
  330. }; // (count:int):void
  331. this.skipBits = function (count) {
  332. var skipBytes; // :int
  333. if (workingBitsAvailable > count) {
  334. workingWord <<= count;
  335. workingBitsAvailable -= count;
  336. } else {
  337. count -= workingBitsAvailable;
  338. skipBytes = Math.floor(count / 8);
  339. count -= skipBytes * 8;
  340. workingBytesAvailable -= skipBytes;
  341. this.loadWord();
  342. workingWord <<= count;
  343. workingBitsAvailable -= count;
  344. }
  345. }; // (size:int):uint
  346. this.readBits = function (size) {
  347. var bits = Math.min(workingBitsAvailable, size),
  348. // :uint
  349. valu = workingWord >>> 32 - bits; // :uint
  350. // if size > 31, handle error
  351. workingBitsAvailable -= bits;
  352. if (workingBitsAvailable > 0) {
  353. workingWord <<= bits;
  354. } else if (workingBytesAvailable > 0) {
  355. this.loadWord();
  356. }
  357. bits = size - bits;
  358. if (bits > 0) {
  359. return valu << bits | this.readBits(bits);
  360. }
  361. return valu;
  362. }; // ():uint
  363. this.skipLeadingZeros = function () {
  364. var leadingZeroCount; // :uint
  365. for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
  366. if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
  367. // the first bit of working word is 1
  368. workingWord <<= leadingZeroCount;
  369. workingBitsAvailable -= leadingZeroCount;
  370. return leadingZeroCount;
  371. }
  372. } // we exhausted workingWord and still have not found a 1
  373. this.loadWord();
  374. return leadingZeroCount + this.skipLeadingZeros();
  375. }; // ():void
  376. this.skipUnsignedExpGolomb = function () {
  377. this.skipBits(1 + this.skipLeadingZeros());
  378. }; // ():void
  379. this.skipExpGolomb = function () {
  380. this.skipBits(1 + this.skipLeadingZeros());
  381. }; // ():uint
  382. this.readUnsignedExpGolomb = function () {
  383. var clz = this.skipLeadingZeros(); // :uint
  384. return this.readBits(clz + 1) - 1;
  385. }; // ():int
  386. this.readExpGolomb = function () {
  387. var valu = this.readUnsignedExpGolomb(); // :int
  388. if (0x01 & valu) {
  389. // the number is odd if the low order bit is set
  390. return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
  391. }
  392. return -1 * (valu >>> 1); // divide by two then make it negative
  393. }; // Some convenience functions
  394. // :Boolean
  395. this.readBoolean = function () {
  396. return this.readBits(1) === 1;
  397. }; // ():int
  398. this.readUnsignedByte = function () {
  399. return this.readBits(8);
  400. };
  401. this.loadWord();
  402. };
  403. var expGolomb = ExpGolomb;
  404. var _H264Stream, _NalByteStream;
  405. var PROFILES_WITH_OPTIONAL_SPS_DATA;
  406. /**
  407. * Accepts a NAL unit byte stream and unpacks the embedded NAL units.
  408. */
  409. _NalByteStream = function NalByteStream() {
  410. var syncPoint = 0,
  411. i,
  412. buffer;
  413. _NalByteStream.prototype.init.call(this);
  414. /*
  415. * Scans a byte stream and triggers a data event with the NAL units found.
  416. * @param {Object} data Event received from H264Stream
  417. * @param {Uint8Array} data.data The h264 byte stream to be scanned
  418. *
  419. * @see H264Stream.push
  420. */
  421. this.push = function (data) {
  422. var swapBuffer;
  423. if (!buffer) {
  424. buffer = data.data;
  425. } else {
  426. swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
  427. swapBuffer.set(buffer);
  428. swapBuffer.set(data.data, buffer.byteLength);
  429. buffer = swapBuffer;
  430. }
  431. var len = buffer.byteLength; // Rec. ITU-T H.264, Annex B
  432. // scan for NAL unit boundaries
  433. // a match looks like this:
  434. // 0 0 1 .. NAL .. 0 0 1
  435. // ^ sync point ^ i
  436. // or this:
  437. // 0 0 1 .. NAL .. 0 0 0
  438. // ^ sync point ^ i
  439. // advance the sync point to a NAL start, if necessary
  440. for (; syncPoint < len - 3; syncPoint++) {
  441. if (buffer[syncPoint + 2] === 1) {
  442. // the sync point is properly aligned
  443. i = syncPoint + 5;
  444. break;
  445. }
  446. }
  447. while (i < len) {
  448. // look at the current byte to determine if we've hit the end of
  449. // a NAL unit boundary
  450. switch (buffer[i]) {
  451. case 0:
  452. // skip past non-sync sequences
  453. if (buffer[i - 1] !== 0) {
  454. i += 2;
  455. break;
  456. } else if (buffer[i - 2] !== 0) {
  457. i++;
  458. break;
  459. } // deliver the NAL unit if it isn't empty
  460. if (syncPoint + 3 !== i - 2) {
  461. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  462. } // drop trailing zeroes
  463. do {
  464. i++;
  465. } while (buffer[i] !== 1 && i < len);
  466. syncPoint = i - 2;
  467. i += 3;
  468. break;
  469. case 1:
  470. // skip past non-sync sequences
  471. if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
  472. i += 3;
  473. break;
  474. } // deliver the NAL unit
  475. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  476. syncPoint = i - 2;
  477. i += 3;
  478. break;
  479. default:
  480. // the current byte isn't a one or zero, so it cannot be part
  481. // of a sync sequence
  482. i += 3;
  483. break;
  484. }
  485. } // filter out the NAL units that were delivered
  486. buffer = buffer.subarray(syncPoint);
  487. i -= syncPoint;
  488. syncPoint = 0;
  489. };
  490. this.reset = function () {
  491. buffer = null;
  492. syncPoint = 0;
  493. this.trigger('reset');
  494. };
  495. this.flush = function () {
  496. // deliver the last buffered NAL unit
  497. if (buffer && buffer.byteLength > 3) {
  498. this.trigger('data', buffer.subarray(syncPoint + 3));
  499. } // reset the stream state
  500. buffer = null;
  501. syncPoint = 0;
  502. this.trigger('done');
  503. };
  504. this.endTimeline = function () {
  505. this.flush();
  506. this.trigger('endedtimeline');
  507. };
  508. };
  509. _NalByteStream.prototype = new stream(); // values of profile_idc that indicate additional fields are included in the SPS
  510. // see Recommendation ITU-T H.264 (4/2013),
  511. // 7.3.2.1.1 Sequence parameter set data syntax
  512. PROFILES_WITH_OPTIONAL_SPS_DATA = {
  513. 100: true,
  514. 110: true,
  515. 122: true,
  516. 244: true,
  517. 44: true,
  518. 83: true,
  519. 86: true,
  520. 118: true,
  521. 128: true,
  522. // TODO: the three profiles below don't
  523. // appear to have sps data in the specificiation anymore?
  524. 138: true,
  525. 139: true,
  526. 134: true
  527. };
  528. /**
  529. * Accepts input from a ElementaryStream and produces H.264 NAL unit data
  530. * events.
  531. */
  532. _H264Stream = function H264Stream() {
  533. var nalByteStream = new _NalByteStream(),
  534. self,
  535. trackId,
  536. currentPts,
  537. currentDts,
  538. discardEmulationPreventionBytes,
  539. readSequenceParameterSet,
  540. skipScalingList;
  541. _H264Stream.prototype.init.call(this);
  542. self = this;
  543. /*
  544. * Pushes a packet from a stream onto the NalByteStream
  545. *
  546. * @param {Object} packet - A packet received from a stream
  547. * @param {Uint8Array} packet.data - The raw bytes of the packet
  548. * @param {Number} packet.dts - Decode timestamp of the packet
  549. * @param {Number} packet.pts - Presentation timestamp of the packet
  550. * @param {Number} packet.trackId - The id of the h264 track this packet came from
  551. * @param {('video'|'audio')} packet.type - The type of packet
  552. *
  553. */
  554. this.push = function (packet) {
  555. if (packet.type !== 'video') {
  556. return;
  557. }
  558. trackId = packet.trackId;
  559. currentPts = packet.pts;
  560. currentDts = packet.dts;
  561. nalByteStream.push(packet);
  562. };
  563. /*
  564. * Identify NAL unit types and pass on the NALU, trackId, presentation and decode timestamps
  565. * for the NALUs to the next stream component.
  566. * Also, preprocess caption and sequence parameter NALUs.
  567. *
  568. * @param {Uint8Array} data - A NAL unit identified by `NalByteStream.push`
  569. * @see NalByteStream.push
  570. */
  571. nalByteStream.on('data', function (data) {
  572. var event = {
  573. trackId: trackId,
  574. pts: currentPts,
  575. dts: currentDts,
  576. data: data,
  577. nalUnitTypeCode: data[0] & 0x1f
  578. };
  579. switch (event.nalUnitTypeCode) {
  580. case 0x05:
  581. event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
  582. break;
  583. case 0x06:
  584. event.nalUnitType = 'sei_rbsp';
  585. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  586. break;
  587. case 0x07:
  588. event.nalUnitType = 'seq_parameter_set_rbsp';
  589. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  590. event.config = readSequenceParameterSet(event.escapedRBSP);
  591. break;
  592. case 0x08:
  593. event.nalUnitType = 'pic_parameter_set_rbsp';
  594. break;
  595. case 0x09:
  596. event.nalUnitType = 'access_unit_delimiter_rbsp';
  597. break;
  598. } // This triggers data on the H264Stream
  599. self.trigger('data', event);
  600. });
  601. nalByteStream.on('done', function () {
  602. self.trigger('done');
  603. });
  604. nalByteStream.on('partialdone', function () {
  605. self.trigger('partialdone');
  606. });
  607. nalByteStream.on('reset', function () {
  608. self.trigger('reset');
  609. });
  610. nalByteStream.on('endedtimeline', function () {
  611. self.trigger('endedtimeline');
  612. });
  613. this.flush = function () {
  614. nalByteStream.flush();
  615. };
  616. this.partialFlush = function () {
  617. nalByteStream.partialFlush();
  618. };
  619. this.reset = function () {
  620. nalByteStream.reset();
  621. };
  622. this.endTimeline = function () {
  623. nalByteStream.endTimeline();
  624. };
  625. /**
  626. * Advance the ExpGolomb decoder past a scaling list. The scaling
  627. * list is optionally transmitted as part of a sequence parameter
  628. * set and is not relevant to transmuxing.
  629. * @param count {number} the number of entries in this scaling list
  630. * @param expGolombDecoder {object} an ExpGolomb pointed to the
  631. * start of a scaling list
  632. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  633. */
  634. skipScalingList = function skipScalingList(count, expGolombDecoder) {
  635. var lastScale = 8,
  636. nextScale = 8,
  637. j,
  638. deltaScale;
  639. for (j = 0; j < count; j++) {
  640. if (nextScale !== 0) {
  641. deltaScale = expGolombDecoder.readExpGolomb();
  642. nextScale = (lastScale + deltaScale + 256) % 256;
  643. }
  644. lastScale = nextScale === 0 ? lastScale : nextScale;
  645. }
  646. };
  647. /**
  648. * Expunge any "Emulation Prevention" bytes from a "Raw Byte
  649. * Sequence Payload"
  650. * @param data {Uint8Array} the bytes of a RBSP from a NAL
  651. * unit
  652. * @return {Uint8Array} the RBSP without any Emulation
  653. * Prevention Bytes
  654. */
  655. discardEmulationPreventionBytes = function discardEmulationPreventionBytes(data) {
  656. var length = data.byteLength,
  657. emulationPreventionBytesPositions = [],
  658. i = 1,
  659. newLength,
  660. newData; // Find all `Emulation Prevention Bytes`
  661. while (i < length - 2) {
  662. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  663. emulationPreventionBytesPositions.push(i + 2);
  664. i += 2;
  665. } else {
  666. i++;
  667. }
  668. } // If no Emulation Prevention Bytes were found just return the original
  669. // array
  670. if (emulationPreventionBytesPositions.length === 0) {
  671. return data;
  672. } // Create a new array to hold the NAL unit data
  673. newLength = length - emulationPreventionBytesPositions.length;
  674. newData = new Uint8Array(newLength);
  675. var sourceIndex = 0;
  676. for (i = 0; i < newLength; sourceIndex++, i++) {
  677. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  678. // Skip this byte
  679. sourceIndex++; // Remove this position index
  680. emulationPreventionBytesPositions.shift();
  681. }
  682. newData[i] = data[sourceIndex];
  683. }
  684. return newData;
  685. };
  686. /**
  687. * Read a sequence parameter set and return some interesting video
  688. * properties. A sequence parameter set is the H264 metadata that
  689. * describes the properties of upcoming video frames.
  690. * @param data {Uint8Array} the bytes of a sequence parameter set
  691. * @return {object} an object with configuration parsed from the
  692. * sequence parameter set, including the dimensions of the
  693. * associated video frames.
  694. */
  695. readSequenceParameterSet = function readSequenceParameterSet(data) {
  696. var frameCropLeftOffset = 0,
  697. frameCropRightOffset = 0,
  698. frameCropTopOffset = 0,
  699. frameCropBottomOffset = 0,
  700. expGolombDecoder,
  701. profileIdc,
  702. levelIdc,
  703. profileCompatibility,
  704. chromaFormatIdc,
  705. picOrderCntType,
  706. numRefFramesInPicOrderCntCycle,
  707. picWidthInMbsMinus1,
  708. picHeightInMapUnitsMinus1,
  709. frameMbsOnlyFlag,
  710. scalingListCount,
  711. sarRatio = [1, 1],
  712. aspectRatioIdc,
  713. i;
  714. expGolombDecoder = new expGolomb(data);
  715. profileIdc = expGolombDecoder.readUnsignedByte(); // profile_idc
  716. profileCompatibility = expGolombDecoder.readUnsignedByte(); // constraint_set[0-5]_flag
  717. levelIdc = expGolombDecoder.readUnsignedByte(); // level_idc u(8)
  718. expGolombDecoder.skipUnsignedExpGolomb(); // seq_parameter_set_id
  719. // some profiles have more optional data we don't need
  720. if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
  721. chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
  722. if (chromaFormatIdc === 3) {
  723. expGolombDecoder.skipBits(1); // separate_colour_plane_flag
  724. }
  725. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_luma_minus8
  726. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_chroma_minus8
  727. expGolombDecoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
  728. if (expGolombDecoder.readBoolean()) {
  729. // seq_scaling_matrix_present_flag
  730. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  731. for (i = 0; i < scalingListCount; i++) {
  732. if (expGolombDecoder.readBoolean()) {
  733. // seq_scaling_list_present_flag[ i ]
  734. if (i < 6) {
  735. skipScalingList(16, expGolombDecoder);
  736. } else {
  737. skipScalingList(64, expGolombDecoder);
  738. }
  739. }
  740. }
  741. }
  742. }
  743. expGolombDecoder.skipUnsignedExpGolomb(); // log2_max_frame_num_minus4
  744. picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
  745. if (picOrderCntType === 0) {
  746. expGolombDecoder.readUnsignedExpGolomb(); // log2_max_pic_order_cnt_lsb_minus4
  747. } else if (picOrderCntType === 1) {
  748. expGolombDecoder.skipBits(1); // delta_pic_order_always_zero_flag
  749. expGolombDecoder.skipExpGolomb(); // offset_for_non_ref_pic
  750. expGolombDecoder.skipExpGolomb(); // offset_for_top_to_bottom_field
  751. numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
  752. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  753. expGolombDecoder.skipExpGolomb(); // offset_for_ref_frame[ i ]
  754. }
  755. }
  756. expGolombDecoder.skipUnsignedExpGolomb(); // max_num_ref_frames
  757. expGolombDecoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
  758. picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  759. picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  760. frameMbsOnlyFlag = expGolombDecoder.readBits(1);
  761. if (frameMbsOnlyFlag === 0) {
  762. expGolombDecoder.skipBits(1); // mb_adaptive_frame_field_flag
  763. }
  764. expGolombDecoder.skipBits(1); // direct_8x8_inference_flag
  765. if (expGolombDecoder.readBoolean()) {
  766. // frame_cropping_flag
  767. frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
  768. frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
  769. frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
  770. frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
  771. }
  772. if (expGolombDecoder.readBoolean()) {
  773. // vui_parameters_present_flag
  774. if (expGolombDecoder.readBoolean()) {
  775. // aspect_ratio_info_present_flag
  776. aspectRatioIdc = expGolombDecoder.readUnsignedByte();
  777. switch (aspectRatioIdc) {
  778. case 1:
  779. sarRatio = [1, 1];
  780. break;
  781. case 2:
  782. sarRatio = [12, 11];
  783. break;
  784. case 3:
  785. sarRatio = [10, 11];
  786. break;
  787. case 4:
  788. sarRatio = [16, 11];
  789. break;
  790. case 5:
  791. sarRatio = [40, 33];
  792. break;
  793. case 6:
  794. sarRatio = [24, 11];
  795. break;
  796. case 7:
  797. sarRatio = [20, 11];
  798. break;
  799. case 8:
  800. sarRatio = [32, 11];
  801. break;
  802. case 9:
  803. sarRatio = [80, 33];
  804. break;
  805. case 10:
  806. sarRatio = [18, 11];
  807. break;
  808. case 11:
  809. sarRatio = [15, 11];
  810. break;
  811. case 12:
  812. sarRatio = [64, 33];
  813. break;
  814. case 13:
  815. sarRatio = [160, 99];
  816. break;
  817. case 14:
  818. sarRatio = [4, 3];
  819. break;
  820. case 15:
  821. sarRatio = [3, 2];
  822. break;
  823. case 16:
  824. sarRatio = [2, 1];
  825. break;
  826. case 255:
  827. {
  828. sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
  829. break;
  830. }
  831. }
  832. if (sarRatio) {
  833. sarRatio[0] / sarRatio[1];
  834. }
  835. }
  836. }
  837. return {
  838. profileIdc: profileIdc,
  839. levelIdc: levelIdc,
  840. profileCompatibility: profileCompatibility,
  841. width: (picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2,
  842. height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
  843. // sar is sample aspect ratio
  844. sarRatio: sarRatio
  845. };
  846. };
  847. };
  848. _H264Stream.prototype = new stream();
  849. var h264 = {
  850. H264Stream: _H264Stream,
  851. NalByteStream: _NalByteStream
  852. };
  853. /**
  854. * mux.js
  855. *
  856. * Copyright (c) Brightcove
  857. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  858. */
  859. var codecs = {
  860. Adts: adts,
  861. h264: h264
  862. };
  863. var MAX_UINT32$1 = Math.pow(2, 32);
  864. var getUint64$5 = function getUint64(uint8) {
  865. var dv = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
  866. var value;
  867. if (dv.getBigUint64) {
  868. value = dv.getBigUint64(0);
  869. if (value < Number.MAX_SAFE_INTEGER) {
  870. return Number(value);
  871. }
  872. return value;
  873. }
  874. return dv.getUint32(0) * MAX_UINT32$1 + dv.getUint32(4);
  875. };
  876. var numbers = {
  877. getUint64: getUint64$5,
  878. MAX_UINT32: MAX_UINT32$1
  879. };
  880. var MAX_UINT32 = numbers.MAX_UINT32;
  881. var box, dinf, esds, ftyp, mdat, mfhd, minf, moof, moov, mvex, mvhd, trak, tkhd, mdia, mdhd, hdlr, sdtp, stbl, stsd, traf, trex, trun$1, types, MAJOR_BRAND, MINOR_VERSION, AVC1_BRAND, VIDEO_HDLR, AUDIO_HDLR, HDLR_TYPES, VMHD, SMHD, DREF, STCO, STSC, STSZ, STTS; // pre-calculate constants
  882. (function () {
  883. var i;
  884. types = {
  885. avc1: [],
  886. // codingname
  887. avcC: [],
  888. btrt: [],
  889. dinf: [],
  890. dref: [],
  891. esds: [],
  892. ftyp: [],
  893. hdlr: [],
  894. mdat: [],
  895. mdhd: [],
  896. mdia: [],
  897. mfhd: [],
  898. minf: [],
  899. moof: [],
  900. moov: [],
  901. mp4a: [],
  902. // codingname
  903. mvex: [],
  904. mvhd: [],
  905. pasp: [],
  906. sdtp: [],
  907. smhd: [],
  908. stbl: [],
  909. stco: [],
  910. stsc: [],
  911. stsd: [],
  912. stsz: [],
  913. stts: [],
  914. styp: [],
  915. tfdt: [],
  916. tfhd: [],
  917. traf: [],
  918. trak: [],
  919. trun: [],
  920. trex: [],
  921. tkhd: [],
  922. vmhd: []
  923. }; // In environments where Uint8Array is undefined (e.g., IE8), skip set up so that we
  924. // don't throw an error
  925. if (typeof Uint8Array === 'undefined') {
  926. return;
  927. }
  928. for (i in types) {
  929. if (types.hasOwnProperty(i)) {
  930. types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
  931. }
  932. }
  933. MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
  934. AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
  935. MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
  936. VIDEO_HDLR = new Uint8Array([0x00, // version 0
  937. 0x00, 0x00, 0x00, // flags
  938. 0x00, 0x00, 0x00, 0x00, // pre_defined
  939. 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
  940. 0x00, 0x00, 0x00, 0x00, // reserved
  941. 0x00, 0x00, 0x00, 0x00, // reserved
  942. 0x00, 0x00, 0x00, 0x00, // reserved
  943. 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
  944. ]);
  945. AUDIO_HDLR = new Uint8Array([0x00, // version 0
  946. 0x00, 0x00, 0x00, // flags
  947. 0x00, 0x00, 0x00, 0x00, // pre_defined
  948. 0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
  949. 0x00, 0x00, 0x00, 0x00, // reserved
  950. 0x00, 0x00, 0x00, 0x00, // reserved
  951. 0x00, 0x00, 0x00, 0x00, // reserved
  952. 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
  953. ]);
  954. HDLR_TYPES = {
  955. video: VIDEO_HDLR,
  956. audio: AUDIO_HDLR
  957. };
  958. DREF = new Uint8Array([0x00, // version 0
  959. 0x00, 0x00, 0x00, // flags
  960. 0x00, 0x00, 0x00, 0x01, // entry_count
  961. 0x00, 0x00, 0x00, 0x0c, // entry_size
  962. 0x75, 0x72, 0x6c, 0x20, // 'url' type
  963. 0x00, // version 0
  964. 0x00, 0x00, 0x01 // entry_flags
  965. ]);
  966. SMHD = new Uint8Array([0x00, // version
  967. 0x00, 0x00, 0x00, // flags
  968. 0x00, 0x00, // balance, 0 means centered
  969. 0x00, 0x00 // reserved
  970. ]);
  971. STCO = new Uint8Array([0x00, // version
  972. 0x00, 0x00, 0x00, // flags
  973. 0x00, 0x00, 0x00, 0x00 // entry_count
  974. ]);
  975. STSC = STCO;
  976. STSZ = new Uint8Array([0x00, // version
  977. 0x00, 0x00, 0x00, // flags
  978. 0x00, 0x00, 0x00, 0x00, // sample_size
  979. 0x00, 0x00, 0x00, 0x00 // sample_count
  980. ]);
  981. STTS = STCO;
  982. VMHD = new Uint8Array([0x00, // version
  983. 0x00, 0x00, 0x01, // flags
  984. 0x00, 0x00, // graphicsmode
  985. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
  986. ]);
  987. })();
  988. box = function box(type) {
  989. var payload = [],
  990. size = 0,
  991. i,
  992. result,
  993. view;
  994. for (i = 1; i < arguments.length; i++) {
  995. payload.push(arguments[i]);
  996. }
  997. i = payload.length; // calculate the total size we need to allocate
  998. while (i--) {
  999. size += payload[i].byteLength;
  1000. }
  1001. result = new Uint8Array(size + 8);
  1002. view = new DataView(result.buffer, result.byteOffset, result.byteLength);
  1003. view.setUint32(0, result.byteLength);
  1004. result.set(type, 4); // copy the payload into the result
  1005. for (i = 0, size = 8; i < payload.length; i++) {
  1006. result.set(payload[i], size);
  1007. size += payload[i].byteLength;
  1008. }
  1009. return result;
  1010. };
  1011. dinf = function dinf() {
  1012. return box(types.dinf, box(types.dref, DREF));
  1013. };
  1014. esds = function esds(track) {
  1015. return box(types.esds, new Uint8Array([0x00, // version
  1016. 0x00, 0x00, 0x00, // flags
  1017. // ES_Descriptor
  1018. 0x03, // tag, ES_DescrTag
  1019. 0x19, // length
  1020. 0x00, 0x00, // ES_ID
  1021. 0x00, // streamDependenceFlag, URL_flag, reserved, streamPriority
  1022. // DecoderConfigDescriptor
  1023. 0x04, // tag, DecoderConfigDescrTag
  1024. 0x11, // length
  1025. 0x40, // object type
  1026. 0x15, // streamType
  1027. 0x00, 0x06, 0x00, // bufferSizeDB
  1028. 0x00, 0x00, 0xda, 0xc0, // maxBitrate
  1029. 0x00, 0x00, 0xda, 0xc0, // avgBitrate
  1030. // DecoderSpecificInfo
  1031. 0x05, // tag, DecoderSpecificInfoTag
  1032. 0x02, // length
  1033. // ISO/IEC 14496-3, AudioSpecificConfig
  1034. // for samplingFrequencyIndex see ISO/IEC 13818-7:2006, 8.1.3.2.2, Table 35
  1035. track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02 // GASpecificConfig
  1036. ]));
  1037. };
  1038. ftyp = function ftyp() {
  1039. return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
  1040. };
  1041. hdlr = function hdlr(type) {
  1042. return box(types.hdlr, HDLR_TYPES[type]);
  1043. };
  1044. mdat = function mdat(data) {
  1045. return box(types.mdat, data);
  1046. };
  1047. mdhd = function mdhd(track) {
  1048. var result = new Uint8Array([0x00, // version 0
  1049. 0x00, 0x00, 0x00, // flags
  1050. 0x00, 0x00, 0x00, 0x02, // creation_time
  1051. 0x00, 0x00, 0x00, 0x03, // modification_time
  1052. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  1053. track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF, // duration
  1054. 0x55, 0xc4, // 'und' language (undetermined)
  1055. 0x00, 0x00]); // Use the sample rate from the track metadata, when it is
  1056. // defined. The sample rate can be parsed out of an ADTS header, for
  1057. // instance.
  1058. if (track.samplerate) {
  1059. result[12] = track.samplerate >>> 24 & 0xFF;
  1060. result[13] = track.samplerate >>> 16 & 0xFF;
  1061. result[14] = track.samplerate >>> 8 & 0xFF;
  1062. result[15] = track.samplerate & 0xFF;
  1063. }
  1064. return box(types.mdhd, result);
  1065. };
  1066. mdia = function mdia(track) {
  1067. return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
  1068. };
  1069. mfhd = function mfhd(sequenceNumber) {
  1070. return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // flags
  1071. (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF // sequence_number
  1072. ]));
  1073. };
  1074. minf = function minf(track) {
  1075. return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
  1076. };
  1077. moof = function moof(sequenceNumber, tracks) {
  1078. var trackFragments = [],
  1079. i = tracks.length; // build traf boxes for each track fragment
  1080. while (i--) {
  1081. trackFragments[i] = traf(tracks[i]);
  1082. }
  1083. return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
  1084. };
  1085. /**
  1086. * Returns a movie box.
  1087. * @param tracks {array} the tracks associated with this movie
  1088. * @see ISO/IEC 14496-12:2012(E), section 8.2.1
  1089. */
  1090. moov = function moov(tracks) {
  1091. var i = tracks.length,
  1092. boxes = [];
  1093. while (i--) {
  1094. boxes[i] = trak(tracks[i]);
  1095. }
  1096. return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
  1097. };
  1098. mvex = function mvex(tracks) {
  1099. var i = tracks.length,
  1100. boxes = [];
  1101. while (i--) {
  1102. boxes[i] = trex(tracks[i]);
  1103. }
  1104. return box.apply(null, [types.mvex].concat(boxes));
  1105. };
  1106. mvhd = function mvhd(duration) {
  1107. var bytes = new Uint8Array([0x00, // version 0
  1108. 0x00, 0x00, 0x00, // flags
  1109. 0x00, 0x00, 0x00, 0x01, // creation_time
  1110. 0x00, 0x00, 0x00, 0x02, // modification_time
  1111. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  1112. (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF, // duration
  1113. 0x00, 0x01, 0x00, 0x00, // 1.0 rate
  1114. 0x01, 0x00, // 1.0 volume
  1115. 0x00, 0x00, // reserved
  1116. 0x00, 0x00, 0x00, 0x00, // reserved
  1117. 0x00, 0x00, 0x00, 0x00, // reserved
  1118. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
  1119. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  1120. 0xff, 0xff, 0xff, 0xff // next_track_ID
  1121. ]);
  1122. return box(types.mvhd, bytes);
  1123. };
  1124. sdtp = function sdtp(track) {
  1125. var samples = track.samples || [],
  1126. bytes = new Uint8Array(4 + samples.length),
  1127. flags,
  1128. i; // leave the full box header (4 bytes) all zero
  1129. // write the sample table
  1130. for (i = 0; i < samples.length; i++) {
  1131. flags = samples[i].flags;
  1132. bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
  1133. }
  1134. return box(types.sdtp, bytes);
  1135. };
  1136. stbl = function stbl(track) {
  1137. return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
  1138. };
  1139. (function () {
  1140. var videoSample, audioSample;
  1141. stsd = function stsd(track) {
  1142. return box(types.stsd, new Uint8Array([0x00, // version 0
  1143. 0x00, 0x00, 0x00, // flags
  1144. 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
  1145. };
  1146. videoSample = function videoSample(track) {
  1147. var sps = track.sps || [],
  1148. pps = track.pps || [],
  1149. sequenceParameterSets = [],
  1150. pictureParameterSets = [],
  1151. i,
  1152. avc1Box; // assemble the SPSs
  1153. for (i = 0; i < sps.length; i++) {
  1154. sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
  1155. sequenceParameterSets.push(sps[i].byteLength & 0xFF); // sequenceParameterSetLength
  1156. sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i])); // SPS
  1157. } // assemble the PPSs
  1158. for (i = 0; i < pps.length; i++) {
  1159. pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
  1160. pictureParameterSets.push(pps[i].byteLength & 0xFF);
  1161. pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
  1162. }
  1163. avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  1164. 0x00, 0x01, // data_reference_index
  1165. 0x00, 0x00, // pre_defined
  1166. 0x00, 0x00, // reserved
  1167. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  1168. (track.width & 0xff00) >> 8, track.width & 0xff, // width
  1169. (track.height & 0xff00) >> 8, track.height & 0xff, // height
  1170. 0x00, 0x48, 0x00, 0x00, // horizresolution
  1171. 0x00, 0x48, 0x00, 0x00, // vertresolution
  1172. 0x00, 0x00, 0x00, 0x00, // reserved
  1173. 0x00, 0x01, // frame_count
  1174. 0x13, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6a, 0x73, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x69, 0x62, 0x2d, 0x68, 0x6c, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // compressorname
  1175. 0x00, 0x18, // depth = 24
  1176. 0x11, 0x11 // pre_defined = -1
  1177. ]), box(types.avcC, new Uint8Array([0x01, // configurationVersion
  1178. track.profileIdc, // AVCProfileIndication
  1179. track.profileCompatibility, // profile_compatibility
  1180. track.levelIdc, // AVCLevelIndication
  1181. 0xff // lengthSizeMinusOne, hard-coded to 4 bytes
  1182. ].concat([sps.length], // numOfSequenceParameterSets
  1183. sequenceParameterSets, // "SPS"
  1184. [pps.length], // numOfPictureParameterSets
  1185. pictureParameterSets // "PPS"
  1186. ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
  1187. 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
  1188. 0x00, 0x2d, 0xc6, 0xc0 // avgBitrate
  1189. ]))];
  1190. if (track.sarRatio) {
  1191. var hSpacing = track.sarRatio[0],
  1192. vSpacing = track.sarRatio[1];
  1193. avc1Box.push(box(types.pasp, new Uint8Array([(hSpacing & 0xFF000000) >> 24, (hSpacing & 0xFF0000) >> 16, (hSpacing & 0xFF00) >> 8, hSpacing & 0xFF, (vSpacing & 0xFF000000) >> 24, (vSpacing & 0xFF0000) >> 16, (vSpacing & 0xFF00) >> 8, vSpacing & 0xFF])));
  1194. }
  1195. return box.apply(null, avc1Box);
  1196. };
  1197. audioSample = function audioSample(track) {
  1198. return box(types.mp4a, new Uint8Array([// SampleEntry, ISO/IEC 14496-12
  1199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  1200. 0x00, 0x01, // data_reference_index
  1201. // AudioSampleEntry, ISO/IEC 14496-12
  1202. 0x00, 0x00, 0x00, 0x00, // reserved
  1203. 0x00, 0x00, 0x00, 0x00, // reserved
  1204. (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff, // channelcount
  1205. (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff, // samplesize
  1206. 0x00, 0x00, // pre_defined
  1207. 0x00, 0x00, // reserved
  1208. (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00 // samplerate, 16.16
  1209. // MP4AudioSampleEntry, ISO/IEC 14496-14
  1210. ]), esds(track));
  1211. };
  1212. })();
  1213. tkhd = function tkhd(track) {
  1214. var result = new Uint8Array([0x00, // version 0
  1215. 0x00, 0x00, 0x07, // flags
  1216. 0x00, 0x00, 0x00, 0x00, // creation_time
  1217. 0x00, 0x00, 0x00, 0x00, // modification_time
  1218. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  1219. 0x00, 0x00, 0x00, 0x00, // reserved
  1220. (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF, // duration
  1221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  1222. 0x00, 0x00, // layer
  1223. 0x00, 0x00, // alternate_group
  1224. 0x01, 0x00, // non-audio track volume
  1225. 0x00, 0x00, // reserved
  1226. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
  1227. (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00, // width
  1228. (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00 // height
  1229. ]);
  1230. return box(types.tkhd, result);
  1231. };
  1232. /**
  1233. * Generate a track fragment (traf) box. A traf box collects metadata
  1234. * about tracks in a movie fragment (moof) box.
  1235. */
  1236. traf = function traf(track) {
  1237. var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
  1238. trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00, // version 0
  1239. 0x00, 0x00, 0x3a, // flags
  1240. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  1241. 0x00, 0x00, 0x00, 0x01, // sample_description_index
  1242. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  1243. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  1244. 0x00, 0x00, 0x00, 0x00 // default_sample_flags
  1245. ]));
  1246. upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / MAX_UINT32);
  1247. lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % MAX_UINT32);
  1248. trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01, // version 1
  1249. 0x00, 0x00, 0x00, // flags
  1250. // baseMediaDecodeTime
  1251. upperWordBaseMediaDecodeTime >>> 24 & 0xFF, upperWordBaseMediaDecodeTime >>> 16 & 0xFF, upperWordBaseMediaDecodeTime >>> 8 & 0xFF, upperWordBaseMediaDecodeTime & 0xFF, lowerWordBaseMediaDecodeTime >>> 24 & 0xFF, lowerWordBaseMediaDecodeTime >>> 16 & 0xFF, lowerWordBaseMediaDecodeTime >>> 8 & 0xFF, lowerWordBaseMediaDecodeTime & 0xFF])); // the data offset specifies the number of bytes from the start of
  1252. // the containing moof to the first payload byte of the associated
  1253. // mdat
  1254. dataOffset = 32 + // tfhd
  1255. 20 + // tfdt
  1256. 8 + // traf header
  1257. 16 + // mfhd
  1258. 8 + // moof header
  1259. 8; // mdat header
  1260. // audio tracks require less metadata
  1261. if (track.type === 'audio') {
  1262. trackFragmentRun = trun$1(track, dataOffset);
  1263. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
  1264. } // video tracks should contain an independent and disposable samples
  1265. // box (sdtp)
  1266. // generate one and adjust offsets to match
  1267. sampleDependencyTable = sdtp(track);
  1268. trackFragmentRun = trun$1(track, sampleDependencyTable.length + dataOffset);
  1269. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
  1270. };
  1271. /**
  1272. * Generate a track box.
  1273. * @param track {object} a track definition
  1274. * @return {Uint8Array} the track box
  1275. */
  1276. trak = function trak(track) {
  1277. track.duration = track.duration || 0xffffffff;
  1278. return box(types.trak, tkhd(track), mdia(track));
  1279. };
  1280. trex = function trex(track) {
  1281. var result = new Uint8Array([0x00, // version 0
  1282. 0x00, 0x00, 0x00, // flags
  1283. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  1284. 0x00, 0x00, 0x00, 0x01, // default_sample_description_index
  1285. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  1286. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  1287. 0x00, 0x01, 0x00, 0x01 // default_sample_flags
  1288. ]); // the last two bytes of default_sample_flags is the sample
  1289. // degradation priority, a hint about the importance of this sample
  1290. // relative to others. Lower the degradation priority for all sample
  1291. // types other than video.
  1292. if (track.type !== 'video') {
  1293. result[result.length - 1] = 0x00;
  1294. }
  1295. return box(types.trex, result);
  1296. };
  1297. (function () {
  1298. var audioTrun, videoTrun, trunHeader; // This method assumes all samples are uniform. That is, if a
  1299. // duration is present for the first sample, it will be present for
  1300. // all subsequent samples.
  1301. // see ISO/IEC 14496-12:2012, Section 8.8.8.1
  1302. trunHeader = function trunHeader(samples, offset) {
  1303. var durationPresent = 0,
  1304. sizePresent = 0,
  1305. flagsPresent = 0,
  1306. compositionTimeOffset = 0; // trun flag constants
  1307. if (samples.length) {
  1308. if (samples[0].duration !== undefined) {
  1309. durationPresent = 0x1;
  1310. }
  1311. if (samples[0].size !== undefined) {
  1312. sizePresent = 0x2;
  1313. }
  1314. if (samples[0].flags !== undefined) {
  1315. flagsPresent = 0x4;
  1316. }
  1317. if (samples[0].compositionTimeOffset !== undefined) {
  1318. compositionTimeOffset = 0x8;
  1319. }
  1320. }
  1321. return [0x00, // version 0
  1322. 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01, // flags
  1323. (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF, // sample_count
  1324. (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF // data_offset
  1325. ];
  1326. };
  1327. videoTrun = function videoTrun(track, offset) {
  1328. var bytesOffest, bytes, header, samples, sample, i;
  1329. samples = track.samples || [];
  1330. offset += 8 + 12 + 16 * samples.length;
  1331. header = trunHeader(samples, offset);
  1332. bytes = new Uint8Array(header.length + samples.length * 16);
  1333. bytes.set(header);
  1334. bytesOffest = header.length;
  1335. for (i = 0; i < samples.length; i++) {
  1336. sample = samples[i];
  1337. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  1338. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  1339. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  1340. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  1341. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  1342. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  1343. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  1344. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  1345. bytes[bytesOffest++] = sample.flags.isLeading << 2 | sample.flags.dependsOn;
  1346. bytes[bytesOffest++] = sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample;
  1347. bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
  1348. bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F; // sample_flags
  1349. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
  1350. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
  1351. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
  1352. bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF; // sample_composition_time_offset
  1353. }
  1354. return box(types.trun, bytes);
  1355. };
  1356. audioTrun = function audioTrun(track, offset) {
  1357. var bytes, bytesOffest, header, samples, sample, i;
  1358. samples = track.samples || [];
  1359. offset += 8 + 12 + 8 * samples.length;
  1360. header = trunHeader(samples, offset);
  1361. bytes = new Uint8Array(header.length + samples.length * 8);
  1362. bytes.set(header);
  1363. bytesOffest = header.length;
  1364. for (i = 0; i < samples.length; i++) {
  1365. sample = samples[i];
  1366. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  1367. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  1368. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  1369. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  1370. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  1371. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  1372. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  1373. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  1374. }
  1375. return box(types.trun, bytes);
  1376. };
  1377. trun$1 = function trun(track, offset) {
  1378. if (track.type === 'audio') {
  1379. return audioTrun(track, offset);
  1380. }
  1381. return videoTrun(track, offset);
  1382. };
  1383. })();
  1384. var mp4Generator = {
  1385. ftyp: ftyp,
  1386. mdat: mdat,
  1387. moof: moof,
  1388. moov: moov,
  1389. initSegment: function initSegment(tracks) {
  1390. var fileType = ftyp(),
  1391. movie = moov(tracks),
  1392. result;
  1393. result = new Uint8Array(fileType.byteLength + movie.byteLength);
  1394. result.set(fileType);
  1395. result.set(movie, fileType.byteLength);
  1396. return result;
  1397. }
  1398. };
  1399. /**
  1400. * mux.js
  1401. *
  1402. * Copyright (c) Brightcove
  1403. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1404. */
  1405. var toUnsigned$3 = function toUnsigned(value) {
  1406. return value >>> 0;
  1407. };
  1408. var toHexString$1 = function toHexString(value) {
  1409. return ('00' + value.toString(16)).slice(-2);
  1410. };
  1411. var bin = {
  1412. toUnsigned: toUnsigned$3,
  1413. toHexString: toHexString$1
  1414. };
  1415. var parseType$2 = function parseType(buffer) {
  1416. var result = '';
  1417. result += String.fromCharCode(buffer[0]);
  1418. result += String.fromCharCode(buffer[1]);
  1419. result += String.fromCharCode(buffer[2]);
  1420. result += String.fromCharCode(buffer[3]);
  1421. return result;
  1422. };
  1423. var parseType_1 = parseType$2;
  1424. var toUnsigned$2 = bin.toUnsigned;
  1425. var findBox = function findBox(data, path) {
  1426. var results = [],
  1427. i,
  1428. size,
  1429. type,
  1430. end,
  1431. subresults;
  1432. if (!path.length) {
  1433. // short-circuit the search for empty paths
  1434. return null;
  1435. }
  1436. for (i = 0; i < data.byteLength;) {
  1437. size = toUnsigned$2(data[i] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3]);
  1438. type = parseType_1(data.subarray(i + 4, i + 8));
  1439. end = size > 1 ? i + size : data.byteLength;
  1440. if (type === path[0]) {
  1441. if (path.length === 1) {
  1442. // this is the end of the path and we've found the box we were
  1443. // looking for
  1444. results.push(data.subarray(i + 8, end));
  1445. } else {
  1446. // recursively search for the next box along the path
  1447. subresults = findBox(data.subarray(i + 8, end), path.slice(1));
  1448. if (subresults.length) {
  1449. results = results.concat(subresults);
  1450. }
  1451. }
  1452. }
  1453. i = end;
  1454. } // we've finished searching all of data
  1455. return results;
  1456. };
  1457. var findBox_1 = findBox;
  1458. /**
  1459. * Returns the first string in the data array ending with a null char '\0'
  1460. * @param {UInt8} data
  1461. * @returns the string with the null char
  1462. */
  1463. var uint8ToCString$1 = function uint8ToCString(data) {
  1464. var index = 0;
  1465. var curChar = String.fromCharCode(data[index]);
  1466. var retString = '';
  1467. while (curChar !== '\0') {
  1468. retString += curChar;
  1469. index++;
  1470. curChar = String.fromCharCode(data[index]);
  1471. } // Add nullChar
  1472. retString += curChar;
  1473. return retString;
  1474. };
  1475. var string = {
  1476. uint8ToCString: uint8ToCString$1
  1477. };
  1478. var uint8ToCString = string.uint8ToCString;
  1479. var getUint64$4 = numbers.getUint64;
  1480. /**
  1481. * Based on: ISO/IEC 23009 Section: 5.10.3.3
  1482. * References:
  1483. * https://dashif-documents.azurewebsites.net/Events/master/event.html#emsg-format
  1484. * https://aomediacodec.github.io/id3-emsg/
  1485. *
  1486. * Takes emsg box data as a uint8 array and returns a emsg box object
  1487. * @param {UInt8Array} boxData data from emsg box
  1488. * @returns A parsed emsg box object
  1489. */
  1490. var parseEmsgBox = function parseEmsgBox(boxData) {
  1491. // version + flags
  1492. var offset = 4;
  1493. var version = boxData[0];
  1494. var scheme_id_uri, value, timescale, presentation_time, presentation_time_delta, event_duration, id, message_data;
  1495. if (version === 0) {
  1496. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  1497. offset += scheme_id_uri.length;
  1498. value = uint8ToCString(boxData.subarray(offset));
  1499. offset += value.length;
  1500. var dv = new DataView(boxData.buffer);
  1501. timescale = dv.getUint32(offset);
  1502. offset += 4;
  1503. presentation_time_delta = dv.getUint32(offset);
  1504. offset += 4;
  1505. event_duration = dv.getUint32(offset);
  1506. offset += 4;
  1507. id = dv.getUint32(offset);
  1508. offset += 4;
  1509. } else if (version === 1) {
  1510. var dv = new DataView(boxData.buffer);
  1511. timescale = dv.getUint32(offset);
  1512. offset += 4;
  1513. presentation_time = getUint64$4(boxData.subarray(offset));
  1514. offset += 8;
  1515. event_duration = dv.getUint32(offset);
  1516. offset += 4;
  1517. id = dv.getUint32(offset);
  1518. offset += 4;
  1519. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  1520. offset += scheme_id_uri.length;
  1521. value = uint8ToCString(boxData.subarray(offset));
  1522. offset += value.length;
  1523. }
  1524. message_data = new Uint8Array(boxData.subarray(offset, boxData.byteLength));
  1525. var emsgBox = {
  1526. scheme_id_uri: scheme_id_uri,
  1527. value: value,
  1528. // if timescale is undefined or 0 set to 1
  1529. timescale: timescale ? timescale : 1,
  1530. presentation_time: presentation_time,
  1531. presentation_time_delta: presentation_time_delta,
  1532. event_duration: event_duration,
  1533. id: id,
  1534. message_data: message_data
  1535. };
  1536. return isValidEmsgBox(version, emsgBox) ? emsgBox : undefined;
  1537. };
  1538. /**
  1539. * Scales a presentation time or time delta with an offset with a provided timescale
  1540. * @param {number} presentationTime
  1541. * @param {number} timescale
  1542. * @param {number} timeDelta
  1543. * @param {number} offset
  1544. * @returns the scaled time as a number
  1545. */
  1546. var scaleTime = function scaleTime(presentationTime, timescale, timeDelta, offset) {
  1547. return presentationTime || presentationTime === 0 ? presentationTime / timescale : offset + timeDelta / timescale;
  1548. };
  1549. /**
  1550. * Checks the emsg box data for validity based on the version
  1551. * @param {number} version of the emsg box to validate
  1552. * @param {Object} emsg the emsg data to validate
  1553. * @returns if the box is valid as a boolean
  1554. */
  1555. var isValidEmsgBox = function isValidEmsgBox(version, emsg) {
  1556. var hasScheme = emsg.scheme_id_uri !== '\0';
  1557. var isValidV0Box = version === 0 && isDefined(emsg.presentation_time_delta) && hasScheme;
  1558. var isValidV1Box = version === 1 && isDefined(emsg.presentation_time) && hasScheme; // Only valid versions of emsg are 0 and 1
  1559. return !(version > 1) && isValidV0Box || isValidV1Box;
  1560. }; // Utility function to check if an object is defined
  1561. var isDefined = function isDefined(data) {
  1562. return data !== undefined || data !== null;
  1563. };
  1564. var emsg = {
  1565. parseEmsgBox: parseEmsgBox,
  1566. scaleTime: scaleTime
  1567. };
  1568. var tfhd = function tfhd(data) {
  1569. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  1570. result = {
  1571. version: data[0],
  1572. flags: new Uint8Array(data.subarray(1, 4)),
  1573. trackId: view.getUint32(4)
  1574. },
  1575. baseDataOffsetPresent = result.flags[2] & 0x01,
  1576. sampleDescriptionIndexPresent = result.flags[2] & 0x02,
  1577. defaultSampleDurationPresent = result.flags[2] & 0x08,
  1578. defaultSampleSizePresent = result.flags[2] & 0x10,
  1579. defaultSampleFlagsPresent = result.flags[2] & 0x20,
  1580. durationIsEmpty = result.flags[0] & 0x010000,
  1581. defaultBaseIsMoof = result.flags[0] & 0x020000,
  1582. i;
  1583. i = 8;
  1584. if (baseDataOffsetPresent) {
  1585. i += 4; // truncate top 4 bytes
  1586. // FIXME: should we read the full 64 bits?
  1587. result.baseDataOffset = view.getUint32(12);
  1588. i += 4;
  1589. }
  1590. if (sampleDescriptionIndexPresent) {
  1591. result.sampleDescriptionIndex = view.getUint32(i);
  1592. i += 4;
  1593. }
  1594. if (defaultSampleDurationPresent) {
  1595. result.defaultSampleDuration = view.getUint32(i);
  1596. i += 4;
  1597. }
  1598. if (defaultSampleSizePresent) {
  1599. result.defaultSampleSize = view.getUint32(i);
  1600. i += 4;
  1601. }
  1602. if (defaultSampleFlagsPresent) {
  1603. result.defaultSampleFlags = view.getUint32(i);
  1604. }
  1605. if (durationIsEmpty) {
  1606. result.durationIsEmpty = true;
  1607. }
  1608. if (!baseDataOffsetPresent && defaultBaseIsMoof) {
  1609. result.baseDataOffsetIsMoof = true;
  1610. }
  1611. return result;
  1612. };
  1613. var parseTfhd = tfhd;
  1614. var parseSampleFlags = function parseSampleFlags(flags) {
  1615. return {
  1616. isLeading: (flags[0] & 0x0c) >>> 2,
  1617. dependsOn: flags[0] & 0x03,
  1618. isDependedOn: (flags[1] & 0xc0) >>> 6,
  1619. hasRedundancy: (flags[1] & 0x30) >>> 4,
  1620. paddingValue: (flags[1] & 0x0e) >>> 1,
  1621. isNonSyncSample: flags[1] & 0x01,
  1622. degradationPriority: flags[2] << 8 | flags[3]
  1623. };
  1624. };
  1625. var parseSampleFlags_1 = parseSampleFlags;
  1626. var trun = function trun(data) {
  1627. var result = {
  1628. version: data[0],
  1629. flags: new Uint8Array(data.subarray(1, 4)),
  1630. samples: []
  1631. },
  1632. view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  1633. // Flag interpretation
  1634. dataOffsetPresent = result.flags[2] & 0x01,
  1635. // compare with 2nd byte of 0x1
  1636. firstSampleFlagsPresent = result.flags[2] & 0x04,
  1637. // compare with 2nd byte of 0x4
  1638. sampleDurationPresent = result.flags[1] & 0x01,
  1639. // compare with 2nd byte of 0x100
  1640. sampleSizePresent = result.flags[1] & 0x02,
  1641. // compare with 2nd byte of 0x200
  1642. sampleFlagsPresent = result.flags[1] & 0x04,
  1643. // compare with 2nd byte of 0x400
  1644. sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08,
  1645. // compare with 2nd byte of 0x800
  1646. sampleCount = view.getUint32(4),
  1647. offset = 8,
  1648. sample;
  1649. if (dataOffsetPresent) {
  1650. // 32 bit signed integer
  1651. result.dataOffset = view.getInt32(offset);
  1652. offset += 4;
  1653. } // Overrides the flags for the first sample only. The order of
  1654. // optional values will be: duration, size, compositionTimeOffset
  1655. if (firstSampleFlagsPresent && sampleCount) {
  1656. sample = {
  1657. flags: parseSampleFlags_1(data.subarray(offset, offset + 4))
  1658. };
  1659. offset += 4;
  1660. if (sampleDurationPresent) {
  1661. sample.duration = view.getUint32(offset);
  1662. offset += 4;
  1663. }
  1664. if (sampleSizePresent) {
  1665. sample.size = view.getUint32(offset);
  1666. offset += 4;
  1667. }
  1668. if (sampleCompositionTimeOffsetPresent) {
  1669. if (result.version === 1) {
  1670. sample.compositionTimeOffset = view.getInt32(offset);
  1671. } else {
  1672. sample.compositionTimeOffset = view.getUint32(offset);
  1673. }
  1674. offset += 4;
  1675. }
  1676. result.samples.push(sample);
  1677. sampleCount--;
  1678. }
  1679. while (sampleCount--) {
  1680. sample = {};
  1681. if (sampleDurationPresent) {
  1682. sample.duration = view.getUint32(offset);
  1683. offset += 4;
  1684. }
  1685. if (sampleSizePresent) {
  1686. sample.size = view.getUint32(offset);
  1687. offset += 4;
  1688. }
  1689. if (sampleFlagsPresent) {
  1690. sample.flags = parseSampleFlags_1(data.subarray(offset, offset + 4));
  1691. offset += 4;
  1692. }
  1693. if (sampleCompositionTimeOffsetPresent) {
  1694. if (result.version === 1) {
  1695. sample.compositionTimeOffset = view.getInt32(offset);
  1696. } else {
  1697. sample.compositionTimeOffset = view.getUint32(offset);
  1698. }
  1699. offset += 4;
  1700. }
  1701. result.samples.push(sample);
  1702. }
  1703. return result;
  1704. };
  1705. var parseTrun = trun;
  1706. var toUnsigned$1 = bin.toUnsigned;
  1707. var getUint64$3 = numbers.getUint64;
  1708. var tfdt = function tfdt(data) {
  1709. var result = {
  1710. version: data[0],
  1711. flags: new Uint8Array(data.subarray(1, 4))
  1712. };
  1713. if (result.version === 1) {
  1714. result.baseMediaDecodeTime = getUint64$3(data.subarray(4));
  1715. } else {
  1716. result.baseMediaDecodeTime = toUnsigned$1(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
  1717. }
  1718. return result;
  1719. };
  1720. var parseTfdt = tfdt;
  1721. // IE11 doesn't support indexOf for TypedArrays.
  1722. // Once IE11 support is dropped, this function should be removed.
  1723. var typedArrayIndexOf$1 = function typedArrayIndexOf(typedArray, element, fromIndex) {
  1724. if (!typedArray) {
  1725. return -1;
  1726. }
  1727. var currentIndex = fromIndex;
  1728. for (; currentIndex < typedArray.length; currentIndex++) {
  1729. if (typedArray[currentIndex] === element) {
  1730. return currentIndex;
  1731. }
  1732. }
  1733. return -1;
  1734. };
  1735. var typedArray = {
  1736. typedArrayIndexOf: typedArrayIndexOf$1
  1737. };
  1738. var typedArrayIndexOf = typedArray.typedArrayIndexOf,
  1739. // Frames that allow different types of text encoding contain a text
  1740. // encoding description byte [ID3v2.4.0 section 4.]
  1741. textEncodingDescriptionByte = {
  1742. Iso88591: 0x00,
  1743. // ISO-8859-1, terminated with \0.
  1744. Utf16: 0x01,
  1745. // UTF-16 encoded Unicode BOM, terminated with \0\0
  1746. Utf16be: 0x02,
  1747. // UTF-16BE encoded Unicode, without BOM, terminated with \0\0
  1748. Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0
  1749. },
  1750. // return a percent-encoded representation of the specified byte range
  1751. // @see http://en.wikipedia.org/wiki/Percent-encoding
  1752. percentEncode$1 = function percentEncode(bytes, start, end) {
  1753. var i,
  1754. result = '';
  1755. for (i = start; i < end; i++) {
  1756. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  1757. }
  1758. return result;
  1759. },
  1760. // return the string representation of the specified byte range,
  1761. // interpreted as UTf-8.
  1762. parseUtf8 = function parseUtf8(bytes, start, end) {
  1763. return decodeURIComponent(percentEncode$1(bytes, start, end));
  1764. },
  1765. // return the string representation of the specified byte range,
  1766. // interpreted as ISO-8859-1.
  1767. parseIso88591$1 = function parseIso88591(bytes, start, end) {
  1768. return unescape(percentEncode$1(bytes, start, end)); // jshint ignore:line
  1769. },
  1770. parseSyncSafeInteger$1 = function parseSyncSafeInteger(data) {
  1771. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  1772. },
  1773. frameParsers = {
  1774. 'APIC': function APIC(frame) {
  1775. var i = 1,
  1776. mimeTypeEndIndex,
  1777. descriptionEndIndex,
  1778. LINK_MIME_TYPE = '-->';
  1779. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  1780. // ignore frames with unrecognized character encodings
  1781. return;
  1782. } // parsing fields [ID3v2.4.0 section 4.14.]
  1783. mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
  1784. if (mimeTypeEndIndex < 0) {
  1785. // malformed frame
  1786. return;
  1787. } // parsing Mime type field (terminated with \0)
  1788. frame.mimeType = parseIso88591$1(frame.data, i, mimeTypeEndIndex);
  1789. i = mimeTypeEndIndex + 1; // parsing 1-byte Picture Type field
  1790. frame.pictureType = frame.data[i];
  1791. i++;
  1792. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
  1793. if (descriptionEndIndex < 0) {
  1794. // malformed frame
  1795. return;
  1796. } // parsing Description field (terminated with \0)
  1797. frame.description = parseUtf8(frame.data, i, descriptionEndIndex);
  1798. i = descriptionEndIndex + 1;
  1799. if (frame.mimeType === LINK_MIME_TYPE) {
  1800. // parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.])
  1801. frame.url = parseIso88591$1(frame.data, i, frame.data.length);
  1802. } else {
  1803. // parsing Picture Data field as binary data
  1804. frame.pictureData = frame.data.subarray(i, frame.data.length);
  1805. }
  1806. },
  1807. 'T*': function T(frame) {
  1808. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  1809. // ignore frames with unrecognized character encodings
  1810. return;
  1811. } // parse text field, do not include null terminator in the frame value
  1812. // frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.]
  1813. frame.value = parseUtf8(frame.data, 1, frame.data.length).replace(/\0*$/, ''); // text information frames supports multiple strings, stored as a terminator separated list [ID3v2.4.0 section 4.2.]
  1814. frame.values = frame.value.split('\0');
  1815. },
  1816. 'TXXX': function TXXX(frame) {
  1817. var descriptionEndIndex;
  1818. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  1819. // ignore frames with unrecognized character encodings
  1820. return;
  1821. }
  1822. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  1823. if (descriptionEndIndex === -1) {
  1824. return;
  1825. } // parse the text fields
  1826. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // do not include the null terminator in the tag value
  1827. // frames that allow different types of encoding contain terminated text
  1828. // [ID3v2.4.0 section 4.]
  1829. frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, '');
  1830. frame.data = frame.value;
  1831. },
  1832. 'W*': function W(frame) {
  1833. // parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  1834. // if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3]
  1835. frame.url = parseIso88591$1(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
  1836. },
  1837. 'WXXX': function WXXX(frame) {
  1838. var descriptionEndIndex;
  1839. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  1840. // ignore frames with unrecognized character encodings
  1841. return;
  1842. }
  1843. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  1844. if (descriptionEndIndex === -1) {
  1845. return;
  1846. } // parse the description and URL fields
  1847. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  1848. // if the value is followed by a string termination all the following information
  1849. // should be ignored [ID3v2.4.0 section 4.3]
  1850. frame.url = parseIso88591$1(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, '');
  1851. },
  1852. 'PRIV': function PRIV(frame) {
  1853. var i;
  1854. for (i = 0; i < frame.data.length; i++) {
  1855. if (frame.data[i] === 0) {
  1856. // parse the description and URL fields
  1857. frame.owner = parseIso88591$1(frame.data, 0, i);
  1858. break;
  1859. }
  1860. }
  1861. frame.privateData = frame.data.subarray(i + 1);
  1862. frame.data = frame.privateData;
  1863. }
  1864. };
  1865. var parseId3Frames$1 = function parseId3Frames(data) {
  1866. var frameSize,
  1867. frameHeader,
  1868. frameStart = 10,
  1869. tagSize = 0,
  1870. frames = []; // If we don't have enough data for a header, 10 bytes,
  1871. // or 'ID3' in the first 3 bytes this is not a valid ID3 tag.
  1872. if (data.length < 10 || data[0] !== 'I'.charCodeAt(0) || data[1] !== 'D'.charCodeAt(0) || data[2] !== '3'.charCodeAt(0)) {
  1873. return;
  1874. } // the frame size is transmitted as a 28-bit integer in the
  1875. // last four bytes of the ID3 header.
  1876. // The most significant bit of each byte is dropped and the
  1877. // results concatenated to recover the actual value.
  1878. tagSize = parseSyncSafeInteger$1(data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  1879. // convenient for our comparisons to include it
  1880. tagSize += 10; // check bit 6 of byte 5 for the extended header flag.
  1881. var hasExtendedHeader = data[5] & 0x40;
  1882. if (hasExtendedHeader) {
  1883. // advance the frame start past the extended header
  1884. frameStart += 4; // header size field
  1885. frameStart += parseSyncSafeInteger$1(data.subarray(10, 14));
  1886. tagSize -= parseSyncSafeInteger$1(data.subarray(16, 20)); // clip any padding off the end
  1887. } // parse one or more ID3 frames
  1888. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  1889. do {
  1890. // determine the number of bytes in this frame
  1891. frameSize = parseSyncSafeInteger$1(data.subarray(frameStart + 4, frameStart + 8));
  1892. if (frameSize < 1) {
  1893. break;
  1894. }
  1895. frameHeader = String.fromCharCode(data[frameStart], data[frameStart + 1], data[frameStart + 2], data[frameStart + 3]);
  1896. var frame = {
  1897. id: frameHeader,
  1898. data: data.subarray(frameStart + 10, frameStart + frameSize + 10)
  1899. };
  1900. frame.key = frame.id; // parse frame values
  1901. if (frameParsers[frame.id]) {
  1902. // use frame specific parser
  1903. frameParsers[frame.id](frame);
  1904. } else if (frame.id[0] === 'T') {
  1905. // use text frame generic parser
  1906. frameParsers['T*'](frame);
  1907. } else if (frame.id[0] === 'W') {
  1908. // use URL link frame generic parser
  1909. frameParsers['W*'](frame);
  1910. }
  1911. frames.push(frame);
  1912. frameStart += 10; // advance past the frame header
  1913. frameStart += frameSize; // advance past the frame body
  1914. } while (frameStart < tagSize);
  1915. return frames;
  1916. };
  1917. var parseId3 = {
  1918. parseId3Frames: parseId3Frames$1,
  1919. parseSyncSafeInteger: parseSyncSafeInteger$1,
  1920. frameParsers: frameParsers
  1921. };
  1922. var toUnsigned = bin.toUnsigned;
  1923. var toHexString = bin.toHexString;
  1924. var getUint64$2 = numbers.getUint64;
  1925. var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3;
  1926. var parseId3Frames = parseId3.parseId3Frames;
  1927. /**
  1928. * Parses an MP4 initialization segment and extracts the timescale
  1929. * values for any declared tracks. Timescale values indicate the
  1930. * number of clock ticks per second to assume for time-based values
  1931. * elsewhere in the MP4.
  1932. *
  1933. * To determine the start time of an MP4, you need two pieces of
  1934. * information: the timescale unit and the earliest base media decode
  1935. * time. Multiple timescales can be specified within an MP4 but the
  1936. * base media decode time is always expressed in the timescale from
  1937. * the media header box for the track:
  1938. * ```
  1939. * moov > trak > mdia > mdhd.timescale
  1940. * ```
  1941. * @param init {Uint8Array} the bytes of the init segment
  1942. * @return {object} a hash of track ids to timescale values or null if
  1943. * the init segment is malformed.
  1944. */
  1945. timescale = function timescale(init) {
  1946. var result = {},
  1947. traks = findBox_1(init, ['moov', 'trak']); // mdhd timescale
  1948. return traks.reduce(function (result, trak) {
  1949. var tkhd, version, index, id, mdhd;
  1950. tkhd = findBox_1(trak, ['tkhd'])[0];
  1951. if (!tkhd) {
  1952. return null;
  1953. }
  1954. version = tkhd[0];
  1955. index = version === 0 ? 12 : 20;
  1956. id = toUnsigned(tkhd[index] << 24 | tkhd[index + 1] << 16 | tkhd[index + 2] << 8 | tkhd[index + 3]);
  1957. mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
  1958. if (!mdhd) {
  1959. return null;
  1960. }
  1961. version = mdhd[0];
  1962. index = version === 0 ? 12 : 20;
  1963. result[id] = toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  1964. return result;
  1965. }, result);
  1966. };
  1967. /**
  1968. * Determine the base media decode start time, in seconds, for an MP4
  1969. * fragment. If multiple fragments are specified, the earliest time is
  1970. * returned.
  1971. *
  1972. * The base media decode time can be parsed from track fragment
  1973. * metadata:
  1974. * ```
  1975. * moof > traf > tfdt.baseMediaDecodeTime
  1976. * ```
  1977. * It requires the timescale value from the mdhd to interpret.
  1978. *
  1979. * @param timescale {object} a hash of track ids to timescale values.
  1980. * @return {number} the earliest base media decode start time for the
  1981. * fragment, in seconds
  1982. */
  1983. startTime = function startTime(timescale, fragment) {
  1984. var trafs; // we need info from two childrend of each track fragment box
  1985. trafs = findBox_1(fragment, ['moof', 'traf']); // determine the start times for each track
  1986. var lowestTime = trafs.reduce(function (acc, traf) {
  1987. var tfhd = findBox_1(traf, ['tfhd'])[0]; // get the track id from the tfhd
  1988. var id = toUnsigned(tfhd[4] << 24 | tfhd[5] << 16 | tfhd[6] << 8 | tfhd[7]); // assume a 90kHz clock if no timescale was specified
  1989. var scale = timescale[id] || 90e3; // get the base media decode time from the tfdt
  1990. var tfdt = findBox_1(traf, ['tfdt'])[0];
  1991. var dv = new DataView(tfdt.buffer, tfdt.byteOffset, tfdt.byteLength);
  1992. var baseTime; // version 1 is 64 bit
  1993. if (tfdt[0] === 1) {
  1994. baseTime = getUint64$2(tfdt.subarray(4, 12));
  1995. } else {
  1996. baseTime = dv.getUint32(4);
  1997. } // convert base time to seconds if it is a valid number.
  1998. var seconds;
  1999. if (typeof baseTime === 'bigint') {
  2000. seconds = baseTime / window__default['default'].BigInt(scale);
  2001. } else if (typeof baseTime === 'number' && !isNaN(baseTime)) {
  2002. seconds = baseTime / scale;
  2003. }
  2004. if (seconds < Number.MAX_SAFE_INTEGER) {
  2005. seconds = Number(seconds);
  2006. }
  2007. if (seconds < acc) {
  2008. acc = seconds;
  2009. }
  2010. return acc;
  2011. }, Infinity);
  2012. return typeof lowestTime === 'bigint' || isFinite(lowestTime) ? lowestTime : 0;
  2013. };
  2014. /**
  2015. * Determine the composition start, in seconds, for an MP4
  2016. * fragment.
  2017. *
  2018. * The composition start time of a fragment can be calculated using the base
  2019. * media decode time, composition time offset, and timescale, as follows:
  2020. *
  2021. * compositionStartTime = (baseMediaDecodeTime + compositionTimeOffset) / timescale
  2022. *
  2023. * All of the aforementioned information is contained within a media fragment's
  2024. * `traf` box, except for timescale info, which comes from the initialization
  2025. * segment, so a track id (also contained within a `traf`) is also necessary to
  2026. * associate it with a timescale
  2027. *
  2028. *
  2029. * @param timescales {object} - a hash of track ids to timescale values.
  2030. * @param fragment {Unit8Array} - the bytes of a media segment
  2031. * @return {number} the composition start time for the fragment, in seconds
  2032. **/
  2033. compositionStartTime = function compositionStartTime(timescales, fragment) {
  2034. var trafBoxes = findBox_1(fragment, ['moof', 'traf']);
  2035. var baseMediaDecodeTime = 0;
  2036. var compositionTimeOffset = 0;
  2037. var trackId;
  2038. if (trafBoxes && trafBoxes.length) {
  2039. // The spec states that track run samples contained within a `traf` box are contiguous, but
  2040. // it does not explicitly state whether the `traf` boxes themselves are contiguous.
  2041. // We will assume that they are, so we only need the first to calculate start time.
  2042. var tfhd = findBox_1(trafBoxes[0], ['tfhd'])[0];
  2043. var trun = findBox_1(trafBoxes[0], ['trun'])[0];
  2044. var tfdt = findBox_1(trafBoxes[0], ['tfdt'])[0];
  2045. if (tfhd) {
  2046. var parsedTfhd = parseTfhd(tfhd);
  2047. trackId = parsedTfhd.trackId;
  2048. }
  2049. if (tfdt) {
  2050. var parsedTfdt = parseTfdt(tfdt);
  2051. baseMediaDecodeTime = parsedTfdt.baseMediaDecodeTime;
  2052. }
  2053. if (trun) {
  2054. var parsedTrun = parseTrun(trun);
  2055. if (parsedTrun.samples && parsedTrun.samples.length) {
  2056. compositionTimeOffset = parsedTrun.samples[0].compositionTimeOffset || 0;
  2057. }
  2058. }
  2059. } // Get timescale for this specific track. Assume a 90kHz clock if no timescale was
  2060. // specified.
  2061. var timescale = timescales[trackId] || 90e3; // return the composition start time, in seconds
  2062. if (typeof baseMediaDecodeTime === 'bigint') {
  2063. compositionTimeOffset = window__default['default'].BigInt(compositionTimeOffset);
  2064. timescale = window__default['default'].BigInt(timescale);
  2065. }
  2066. var result = (baseMediaDecodeTime + compositionTimeOffset) / timescale;
  2067. if (typeof result === 'bigint' && result < Number.MAX_SAFE_INTEGER) {
  2068. result = Number(result);
  2069. }
  2070. return result;
  2071. };
  2072. /**
  2073. * Find the trackIds of the video tracks in this source.
  2074. * Found by parsing the Handler Reference and Track Header Boxes:
  2075. * moov > trak > mdia > hdlr
  2076. * moov > trak > tkhd
  2077. *
  2078. * @param {Uint8Array} init - The bytes of the init segment for this source
  2079. * @return {Number[]} A list of trackIds
  2080. *
  2081. * @see ISO-BMFF-12/2015, Section 8.4.3
  2082. **/
  2083. getVideoTrackIds = function getVideoTrackIds(init) {
  2084. var traks = findBox_1(init, ['moov', 'trak']);
  2085. var videoTrackIds = [];
  2086. traks.forEach(function (trak) {
  2087. var hdlrs = findBox_1(trak, ['mdia', 'hdlr']);
  2088. var tkhds = findBox_1(trak, ['tkhd']);
  2089. hdlrs.forEach(function (hdlr, index) {
  2090. var handlerType = parseType_1(hdlr.subarray(8, 12));
  2091. var tkhd = tkhds[index];
  2092. var view;
  2093. var version;
  2094. var trackId;
  2095. if (handlerType === 'vide') {
  2096. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  2097. version = view.getUint8(0);
  2098. trackId = version === 0 ? view.getUint32(12) : view.getUint32(20);
  2099. videoTrackIds.push(trackId);
  2100. }
  2101. });
  2102. });
  2103. return videoTrackIds;
  2104. };
  2105. getTimescaleFromMediaHeader = function getTimescaleFromMediaHeader(mdhd) {
  2106. // mdhd is a FullBox, meaning it will have its own version as the first byte
  2107. var version = mdhd[0];
  2108. var index = version === 0 ? 12 : 20;
  2109. return toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  2110. };
  2111. /**
  2112. * Get all the video, audio, and hint tracks from a non fragmented
  2113. * mp4 segment
  2114. */
  2115. getTracks = function getTracks(init) {
  2116. var traks = findBox_1(init, ['moov', 'trak']);
  2117. var tracks = [];
  2118. traks.forEach(function (trak) {
  2119. var track = {};
  2120. var tkhd = findBox_1(trak, ['tkhd'])[0];
  2121. var view, tkhdVersion; // id
  2122. if (tkhd) {
  2123. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  2124. tkhdVersion = view.getUint8(0);
  2125. track.id = tkhdVersion === 0 ? view.getUint32(12) : view.getUint32(20);
  2126. }
  2127. var hdlr = findBox_1(trak, ['mdia', 'hdlr'])[0]; // type
  2128. if (hdlr) {
  2129. var type = parseType_1(hdlr.subarray(8, 12));
  2130. if (type === 'vide') {
  2131. track.type = 'video';
  2132. } else if (type === 'soun') {
  2133. track.type = 'audio';
  2134. } else {
  2135. track.type = type;
  2136. }
  2137. } // codec
  2138. var stsd = findBox_1(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
  2139. if (stsd) {
  2140. var sampleDescriptions = stsd.subarray(8); // gives the codec type string
  2141. track.codec = parseType_1(sampleDescriptions.subarray(4, 8));
  2142. var codecBox = findBox_1(sampleDescriptions, [track.codec])[0];
  2143. var codecConfig, codecConfigType;
  2144. if (codecBox) {
  2145. // https://tools.ietf.org/html/rfc6381#section-3.3
  2146. if (/^[asm]vc[1-9]$/i.test(track.codec)) {
  2147. // we don't need anything but the "config" parameter of the
  2148. // avc1 codecBox
  2149. codecConfig = codecBox.subarray(78);
  2150. codecConfigType = parseType_1(codecConfig.subarray(4, 8));
  2151. if (codecConfigType === 'avcC' && codecConfig.length > 11) {
  2152. track.codec += '.'; // left padded with zeroes for single digit hex
  2153. // profile idc
  2154. track.codec += toHexString(codecConfig[9]); // the byte containing the constraint_set flags
  2155. track.codec += toHexString(codecConfig[10]); // level idc
  2156. track.codec += toHexString(codecConfig[11]);
  2157. } else {
  2158. // TODO: show a warning that we couldn't parse the codec
  2159. // and are using the default
  2160. track.codec = 'avc1.4d400d';
  2161. }
  2162. } else if (/^mp4[a,v]$/i.test(track.codec)) {
  2163. // we do not need anything but the streamDescriptor of the mp4a codecBox
  2164. codecConfig = codecBox.subarray(28);
  2165. codecConfigType = parseType_1(codecConfig.subarray(4, 8));
  2166. if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
  2167. track.codec += '.' + toHexString(codecConfig[19]); // this value is only a single digit
  2168. track.codec += '.' + toHexString(codecConfig[20] >>> 2 & 0x3f).replace(/^0/, '');
  2169. } else {
  2170. // TODO: show a warning that we couldn't parse the codec
  2171. // and are using the default
  2172. track.codec = 'mp4a.40.2';
  2173. }
  2174. } else {
  2175. // flac, opus, etc
  2176. track.codec = track.codec.toLowerCase();
  2177. }
  2178. }
  2179. }
  2180. var mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
  2181. if (mdhd) {
  2182. track.timescale = getTimescaleFromMediaHeader(mdhd);
  2183. }
  2184. tracks.push(track);
  2185. });
  2186. return tracks;
  2187. };
  2188. /**
  2189. * Returns an array of emsg ID3 data from the provided segmentData.
  2190. * An offset can also be provided as the Latest Arrival Time to calculate
  2191. * the Event Start Time of v0 EMSG boxes.
  2192. * See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing
  2193. *
  2194. * @param {Uint8Array} segmentData the segment byte array.
  2195. * @param {number} offset the segment start time or Latest Arrival Time,
  2196. * @return {Object[]} an array of ID3 parsed from EMSG boxes
  2197. */
  2198. getEmsgID3 = function getEmsgID3(segmentData, offset) {
  2199. if (offset === void 0) {
  2200. offset = 0;
  2201. }
  2202. var emsgBoxes = findBox_1(segmentData, ['emsg']);
  2203. return emsgBoxes.map(function (data) {
  2204. var parsedBox = emsg.parseEmsgBox(new Uint8Array(data));
  2205. var parsedId3Frames = parseId3Frames(parsedBox.message_data);
  2206. return {
  2207. cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset),
  2208. duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale),
  2209. frames: parsedId3Frames
  2210. };
  2211. });
  2212. };
  2213. var probe$2 = {
  2214. // export mp4 inspector's findBox and parseType for backwards compatibility
  2215. findBox: findBox_1,
  2216. parseType: parseType_1,
  2217. timescale: timescale,
  2218. startTime: startTime,
  2219. compositionStartTime: compositionStartTime,
  2220. videoTrackIds: getVideoTrackIds,
  2221. tracks: getTracks,
  2222. getTimescaleFromMediaHeader: getTimescaleFromMediaHeader,
  2223. getEmsgID3: getEmsgID3
  2224. };
  2225. /**
  2226. * mux.js
  2227. *
  2228. * Copyright (c) Brightcove
  2229. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2230. */
  2231. // Convert an array of nal units into an array of frames with each frame being
  2232. // composed of the nal units that make up that frame
  2233. // Also keep track of cummulative data about the frame from the nal units such
  2234. // as the frame duration, starting pts, etc.
  2235. var groupNalsIntoFrames = function groupNalsIntoFrames(nalUnits) {
  2236. var i,
  2237. currentNal,
  2238. currentFrame = [],
  2239. frames = []; // TODO added for LHLS, make sure this is OK
  2240. frames.byteLength = 0;
  2241. frames.nalCount = 0;
  2242. frames.duration = 0;
  2243. currentFrame.byteLength = 0;
  2244. for (i = 0; i < nalUnits.length; i++) {
  2245. currentNal = nalUnits[i]; // Split on 'aud'-type nal units
  2246. if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
  2247. // Since the very first nal unit is expected to be an AUD
  2248. // only push to the frames array when currentFrame is not empty
  2249. if (currentFrame.length) {
  2250. currentFrame.duration = currentNal.dts - currentFrame.dts; // TODO added for LHLS, make sure this is OK
  2251. frames.byteLength += currentFrame.byteLength;
  2252. frames.nalCount += currentFrame.length;
  2253. frames.duration += currentFrame.duration;
  2254. frames.push(currentFrame);
  2255. }
  2256. currentFrame = [currentNal];
  2257. currentFrame.byteLength = currentNal.data.byteLength;
  2258. currentFrame.pts = currentNal.pts;
  2259. currentFrame.dts = currentNal.dts;
  2260. } else {
  2261. // Specifically flag key frames for ease of use later
  2262. if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
  2263. currentFrame.keyFrame = true;
  2264. }
  2265. currentFrame.duration = currentNal.dts - currentFrame.dts;
  2266. currentFrame.byteLength += currentNal.data.byteLength;
  2267. currentFrame.push(currentNal);
  2268. }
  2269. } // For the last frame, use the duration of the previous frame if we
  2270. // have nothing better to go on
  2271. if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
  2272. currentFrame.duration = frames[frames.length - 1].duration;
  2273. } // Push the final frame
  2274. // TODO added for LHLS, make sure this is OK
  2275. frames.byteLength += currentFrame.byteLength;
  2276. frames.nalCount += currentFrame.length;
  2277. frames.duration += currentFrame.duration;
  2278. frames.push(currentFrame);
  2279. return frames;
  2280. }; // Convert an array of frames into an array of Gop with each Gop being composed
  2281. // of the frames that make up that Gop
  2282. // Also keep track of cummulative data about the Gop from the frames such as the
  2283. // Gop duration, starting pts, etc.
  2284. var groupFramesIntoGops = function groupFramesIntoGops(frames) {
  2285. var i,
  2286. currentFrame,
  2287. currentGop = [],
  2288. gops = []; // We must pre-set some of the values on the Gop since we
  2289. // keep running totals of these values
  2290. currentGop.byteLength = 0;
  2291. currentGop.nalCount = 0;
  2292. currentGop.duration = 0;
  2293. currentGop.pts = frames[0].pts;
  2294. currentGop.dts = frames[0].dts; // store some metadata about all the Gops
  2295. gops.byteLength = 0;
  2296. gops.nalCount = 0;
  2297. gops.duration = 0;
  2298. gops.pts = frames[0].pts;
  2299. gops.dts = frames[0].dts;
  2300. for (i = 0; i < frames.length; i++) {
  2301. currentFrame = frames[i];
  2302. if (currentFrame.keyFrame) {
  2303. // Since the very first frame is expected to be an keyframe
  2304. // only push to the gops array when currentGop is not empty
  2305. if (currentGop.length) {
  2306. gops.push(currentGop);
  2307. gops.byteLength += currentGop.byteLength;
  2308. gops.nalCount += currentGop.nalCount;
  2309. gops.duration += currentGop.duration;
  2310. }
  2311. currentGop = [currentFrame];
  2312. currentGop.nalCount = currentFrame.length;
  2313. currentGop.byteLength = currentFrame.byteLength;
  2314. currentGop.pts = currentFrame.pts;
  2315. currentGop.dts = currentFrame.dts;
  2316. currentGop.duration = currentFrame.duration;
  2317. } else {
  2318. currentGop.duration += currentFrame.duration;
  2319. currentGop.nalCount += currentFrame.length;
  2320. currentGop.byteLength += currentFrame.byteLength;
  2321. currentGop.push(currentFrame);
  2322. }
  2323. }
  2324. if (gops.length && currentGop.duration <= 0) {
  2325. currentGop.duration = gops[gops.length - 1].duration;
  2326. }
  2327. gops.byteLength += currentGop.byteLength;
  2328. gops.nalCount += currentGop.nalCount;
  2329. gops.duration += currentGop.duration; // push the final Gop
  2330. gops.push(currentGop);
  2331. return gops;
  2332. };
  2333. /*
  2334. * Search for the first keyframe in the GOPs and throw away all frames
  2335. * until that keyframe. Then extend the duration of the pulled keyframe
  2336. * and pull the PTS and DTS of the keyframe so that it covers the time
  2337. * range of the frames that were disposed.
  2338. *
  2339. * @param {Array} gops video GOPs
  2340. * @returns {Array} modified video GOPs
  2341. */
  2342. var extendFirstKeyFrame = function extendFirstKeyFrame(gops) {
  2343. var currentGop;
  2344. if (!gops[0][0].keyFrame && gops.length > 1) {
  2345. // Remove the first GOP
  2346. currentGop = gops.shift();
  2347. gops.byteLength -= currentGop.byteLength;
  2348. gops.nalCount -= currentGop.nalCount; // Extend the first frame of what is now the
  2349. // first gop to cover the time period of the
  2350. // frames we just removed
  2351. gops[0][0].dts = currentGop.dts;
  2352. gops[0][0].pts = currentGop.pts;
  2353. gops[0][0].duration += currentGop.duration;
  2354. }
  2355. return gops;
  2356. };
  2357. /**
  2358. * Default sample object
  2359. * see ISO/IEC 14496-12:2012, section 8.6.4.3
  2360. */
  2361. var createDefaultSample = function createDefaultSample() {
  2362. return {
  2363. size: 0,
  2364. flags: {
  2365. isLeading: 0,
  2366. dependsOn: 1,
  2367. isDependedOn: 0,
  2368. hasRedundancy: 0,
  2369. degradationPriority: 0,
  2370. isNonSyncSample: 1
  2371. }
  2372. };
  2373. };
  2374. /*
  2375. * Collates information from a video frame into an object for eventual
  2376. * entry into an MP4 sample table.
  2377. *
  2378. * @param {Object} frame the video frame
  2379. * @param {Number} dataOffset the byte offset to position the sample
  2380. * @return {Object} object containing sample table info for a frame
  2381. */
  2382. var sampleForFrame = function sampleForFrame(frame, dataOffset) {
  2383. var sample = createDefaultSample();
  2384. sample.dataOffset = dataOffset;
  2385. sample.compositionTimeOffset = frame.pts - frame.dts;
  2386. sample.duration = frame.duration;
  2387. sample.size = 4 * frame.length; // Space for nal unit size
  2388. sample.size += frame.byteLength;
  2389. if (frame.keyFrame) {
  2390. sample.flags.dependsOn = 2;
  2391. sample.flags.isNonSyncSample = 0;
  2392. }
  2393. return sample;
  2394. }; // generate the track's sample table from an array of gops
  2395. var generateSampleTable$1 = function generateSampleTable(gops, baseDataOffset) {
  2396. var h,
  2397. i,
  2398. sample,
  2399. currentGop,
  2400. currentFrame,
  2401. dataOffset = baseDataOffset || 0,
  2402. samples = [];
  2403. for (h = 0; h < gops.length; h++) {
  2404. currentGop = gops[h];
  2405. for (i = 0; i < currentGop.length; i++) {
  2406. currentFrame = currentGop[i];
  2407. sample = sampleForFrame(currentFrame, dataOffset);
  2408. dataOffset += sample.size;
  2409. samples.push(sample);
  2410. }
  2411. }
  2412. return samples;
  2413. }; // generate the track's raw mdat data from an array of gops
  2414. var concatenateNalData = function concatenateNalData(gops) {
  2415. var h,
  2416. i,
  2417. j,
  2418. currentGop,
  2419. currentFrame,
  2420. currentNal,
  2421. dataOffset = 0,
  2422. nalsByteLength = gops.byteLength,
  2423. numberOfNals = gops.nalCount,
  2424. totalByteLength = nalsByteLength + 4 * numberOfNals,
  2425. data = new Uint8Array(totalByteLength),
  2426. view = new DataView(data.buffer); // For each Gop..
  2427. for (h = 0; h < gops.length; h++) {
  2428. currentGop = gops[h]; // For each Frame..
  2429. for (i = 0; i < currentGop.length; i++) {
  2430. currentFrame = currentGop[i]; // For each NAL..
  2431. for (j = 0; j < currentFrame.length; j++) {
  2432. currentNal = currentFrame[j];
  2433. view.setUint32(dataOffset, currentNal.data.byteLength);
  2434. dataOffset += 4;
  2435. data.set(currentNal.data, dataOffset);
  2436. dataOffset += currentNal.data.byteLength;
  2437. }
  2438. }
  2439. }
  2440. return data;
  2441. }; // generate the track's sample table from a frame
  2442. var generateSampleTableForFrame = function generateSampleTableForFrame(frame, baseDataOffset) {
  2443. var sample,
  2444. dataOffset = baseDataOffset || 0,
  2445. samples = [];
  2446. sample = sampleForFrame(frame, dataOffset);
  2447. samples.push(sample);
  2448. return samples;
  2449. }; // generate the track's raw mdat data from a frame
  2450. var concatenateNalDataForFrame = function concatenateNalDataForFrame(frame) {
  2451. var i,
  2452. currentNal,
  2453. dataOffset = 0,
  2454. nalsByteLength = frame.byteLength,
  2455. numberOfNals = frame.length,
  2456. totalByteLength = nalsByteLength + 4 * numberOfNals,
  2457. data = new Uint8Array(totalByteLength),
  2458. view = new DataView(data.buffer); // For each NAL..
  2459. for (i = 0; i < frame.length; i++) {
  2460. currentNal = frame[i];
  2461. view.setUint32(dataOffset, currentNal.data.byteLength);
  2462. dataOffset += 4;
  2463. data.set(currentNal.data, dataOffset);
  2464. dataOffset += currentNal.data.byteLength;
  2465. }
  2466. return data;
  2467. };
  2468. var frameUtils = {
  2469. groupNalsIntoFrames: groupNalsIntoFrames,
  2470. groupFramesIntoGops: groupFramesIntoGops,
  2471. extendFirstKeyFrame: extendFirstKeyFrame,
  2472. generateSampleTable: generateSampleTable$1,
  2473. concatenateNalData: concatenateNalData,
  2474. generateSampleTableForFrame: generateSampleTableForFrame,
  2475. concatenateNalDataForFrame: concatenateNalDataForFrame
  2476. };
  2477. /**
  2478. * mux.js
  2479. *
  2480. * Copyright (c) Brightcove
  2481. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2482. */
  2483. var highPrefix = [33, 16, 5, 32, 164, 27];
  2484. var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
  2485. var zeroFill = function zeroFill(count) {
  2486. var a = [];
  2487. while (count--) {
  2488. a.push(0);
  2489. }
  2490. return a;
  2491. };
  2492. var makeTable = function makeTable(metaTable) {
  2493. return Object.keys(metaTable).reduce(function (obj, key) {
  2494. obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
  2495. return arr.concat(part);
  2496. }, []));
  2497. return obj;
  2498. }, {});
  2499. };
  2500. var silence;
  2501. var silence_1 = function silence_1() {
  2502. if (!silence) {
  2503. // Frames-of-silence to use for filling in missing AAC frames
  2504. var coneOfSilence = {
  2505. 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
  2506. 88200: [highPrefix, [231], zeroFill(170), [56]],
  2507. 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
  2508. 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
  2509. 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
  2510. 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
  2511. 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
  2512. 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
  2513. 12000: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 253, 128], zeroFill(259), [56]],
  2514. 11025: [lowPrefix, zeroFill(268), [3, 127, 248], zeroFill(268), [6, 255, 240], zeroFill(268), [13, 255, 224], zeroFill(268), [27, 255, 192], zeroFill(268), [55, 175, 128], zeroFill(108), [112]],
  2515. 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
  2516. };
  2517. silence = makeTable(coneOfSilence);
  2518. }
  2519. return silence;
  2520. };
  2521. /**
  2522. * mux.js
  2523. *
  2524. * Copyright (c) Brightcove
  2525. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2526. */
  2527. /**
  2528. * Sum the `byteLength` properties of the data in each AAC frame
  2529. */
  2530. var sumFrameByteLengths = function sumFrameByteLengths(array) {
  2531. var i,
  2532. currentObj,
  2533. sum = 0; // sum the byteLength's all each nal unit in the frame
  2534. for (i = 0; i < array.length; i++) {
  2535. currentObj = array[i];
  2536. sum += currentObj.data.byteLength;
  2537. }
  2538. return sum;
  2539. }; // Possibly pad (prefix) the audio track with silence if appending this track
  2540. // would lead to the introduction of a gap in the audio buffer
  2541. var prefixWithSilence = function prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
  2542. var baseMediaDecodeTimeTs,
  2543. frameDuration = 0,
  2544. audioGapDuration = 0,
  2545. audioFillFrameCount = 0,
  2546. audioFillDuration = 0,
  2547. silentFrame,
  2548. i,
  2549. firstFrame;
  2550. if (!frames.length) {
  2551. return;
  2552. }
  2553. baseMediaDecodeTimeTs = clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate); // determine frame clock duration based on sample rate, round up to avoid overfills
  2554. frameDuration = Math.ceil(clock.ONE_SECOND_IN_TS / (track.samplerate / 1024));
  2555. if (audioAppendStartTs && videoBaseMediaDecodeTime) {
  2556. // insert the shortest possible amount (audio gap or audio to video gap)
  2557. audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime); // number of full frames in the audio gap
  2558. audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
  2559. audioFillDuration = audioFillFrameCount * frameDuration;
  2560. } // don't attempt to fill gaps smaller than a single frame or larger
  2561. // than a half second
  2562. if (audioFillFrameCount < 1 || audioFillDuration > clock.ONE_SECOND_IN_TS / 2) {
  2563. return;
  2564. }
  2565. silentFrame = silence_1()[track.samplerate];
  2566. if (!silentFrame) {
  2567. // we don't have a silent frame pregenerated for the sample rate, so use a frame
  2568. // from the content instead
  2569. silentFrame = frames[0].data;
  2570. }
  2571. for (i = 0; i < audioFillFrameCount; i++) {
  2572. firstFrame = frames[0];
  2573. frames.splice(0, 0, {
  2574. data: silentFrame,
  2575. dts: firstFrame.dts - frameDuration,
  2576. pts: firstFrame.pts - frameDuration
  2577. });
  2578. }
  2579. track.baseMediaDecodeTime -= Math.floor(clock.videoTsToAudioTs(audioFillDuration, track.samplerate));
  2580. return audioFillDuration;
  2581. }; // If the audio segment extends before the earliest allowed dts
  2582. // value, remove AAC frames until starts at or after the earliest
  2583. // allowed DTS so that we don't end up with a negative baseMedia-
  2584. // DecodeTime for the audio track
  2585. var trimAdtsFramesByEarliestDts = function trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts) {
  2586. if (track.minSegmentDts >= earliestAllowedDts) {
  2587. return adtsFrames;
  2588. } // We will need to recalculate the earliest segment Dts
  2589. track.minSegmentDts = Infinity;
  2590. return adtsFrames.filter(function (currentFrame) {
  2591. // If this is an allowed frame, keep it and record it's Dts
  2592. if (currentFrame.dts >= earliestAllowedDts) {
  2593. track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
  2594. track.minSegmentPts = track.minSegmentDts;
  2595. return true;
  2596. } // Otherwise, discard it
  2597. return false;
  2598. });
  2599. }; // generate the track's raw mdat data from an array of frames
  2600. var generateSampleTable = function generateSampleTable(frames) {
  2601. var i,
  2602. currentFrame,
  2603. samples = [];
  2604. for (i = 0; i < frames.length; i++) {
  2605. currentFrame = frames[i];
  2606. samples.push({
  2607. size: currentFrame.data.byteLength,
  2608. duration: 1024 // For AAC audio, all samples contain 1024 samples
  2609. });
  2610. }
  2611. return samples;
  2612. }; // generate the track's sample table from an array of frames
  2613. var concatenateFrameData = function concatenateFrameData(frames) {
  2614. var i,
  2615. currentFrame,
  2616. dataOffset = 0,
  2617. data = new Uint8Array(sumFrameByteLengths(frames));
  2618. for (i = 0; i < frames.length; i++) {
  2619. currentFrame = frames[i];
  2620. data.set(currentFrame.data, dataOffset);
  2621. dataOffset += currentFrame.data.byteLength;
  2622. }
  2623. return data;
  2624. };
  2625. var audioFrameUtils = {
  2626. prefixWithSilence: prefixWithSilence,
  2627. trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
  2628. generateSampleTable: generateSampleTable,
  2629. concatenateFrameData: concatenateFrameData
  2630. };
  2631. /**
  2632. * mux.js
  2633. *
  2634. * Copyright (c) Brightcove
  2635. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2636. */
  2637. var ONE_SECOND_IN_TS$3 = clock.ONE_SECOND_IN_TS;
  2638. /**
  2639. * Store information about the start and end of the track and the
  2640. * duration for each frame/sample we process in order to calculate
  2641. * the baseMediaDecodeTime
  2642. */
  2643. var collectDtsInfo = function collectDtsInfo(track, data) {
  2644. if (typeof data.pts === 'number') {
  2645. if (track.timelineStartInfo.pts === undefined) {
  2646. track.timelineStartInfo.pts = data.pts;
  2647. }
  2648. if (track.minSegmentPts === undefined) {
  2649. track.minSegmentPts = data.pts;
  2650. } else {
  2651. track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
  2652. }
  2653. if (track.maxSegmentPts === undefined) {
  2654. track.maxSegmentPts = data.pts;
  2655. } else {
  2656. track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
  2657. }
  2658. }
  2659. if (typeof data.dts === 'number') {
  2660. if (track.timelineStartInfo.dts === undefined) {
  2661. track.timelineStartInfo.dts = data.dts;
  2662. }
  2663. if (track.minSegmentDts === undefined) {
  2664. track.minSegmentDts = data.dts;
  2665. } else {
  2666. track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
  2667. }
  2668. if (track.maxSegmentDts === undefined) {
  2669. track.maxSegmentDts = data.dts;
  2670. } else {
  2671. track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
  2672. }
  2673. }
  2674. };
  2675. /**
  2676. * Clear values used to calculate the baseMediaDecodeTime between
  2677. * tracks
  2678. */
  2679. var clearDtsInfo = function clearDtsInfo(track) {
  2680. delete track.minSegmentDts;
  2681. delete track.maxSegmentDts;
  2682. delete track.minSegmentPts;
  2683. delete track.maxSegmentPts;
  2684. };
  2685. /**
  2686. * Calculate the track's baseMediaDecodeTime based on the earliest
  2687. * DTS the transmuxer has ever seen and the minimum DTS for the
  2688. * current track
  2689. * @param track {object} track metadata configuration
  2690. * @param keepOriginalTimestamps {boolean} If true, keep the timestamps
  2691. * in the source; false to adjust the first segment to start at 0.
  2692. */
  2693. var calculateTrackBaseMediaDecodeTime = function calculateTrackBaseMediaDecodeTime(track, keepOriginalTimestamps) {
  2694. var baseMediaDecodeTime,
  2695. scale,
  2696. minSegmentDts = track.minSegmentDts; // Optionally adjust the time so the first segment starts at zero.
  2697. if (!keepOriginalTimestamps) {
  2698. minSegmentDts -= track.timelineStartInfo.dts;
  2699. } // track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
  2700. // we want the start of the first segment to be placed
  2701. baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime; // Add to that the distance this segment is from the very first
  2702. baseMediaDecodeTime += minSegmentDts; // baseMediaDecodeTime must not become negative
  2703. baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
  2704. if (track.type === 'audio') {
  2705. // Audio has a different clock equal to the sampling_rate so we need to
  2706. // scale the PTS values into the clock rate of the track
  2707. scale = track.samplerate / ONE_SECOND_IN_TS$3;
  2708. baseMediaDecodeTime *= scale;
  2709. baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
  2710. }
  2711. return baseMediaDecodeTime;
  2712. };
  2713. var trackDecodeInfo = {
  2714. clearDtsInfo: clearDtsInfo,
  2715. calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
  2716. collectDtsInfo: collectDtsInfo
  2717. };
  2718. /**
  2719. * mux.js
  2720. *
  2721. * Copyright (c) Brightcove
  2722. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2723. *
  2724. * Reads in-band caption information from a video elementary
  2725. * stream. Captions must follow the CEA-708 standard for injection
  2726. * into an MPEG-2 transport streams.
  2727. * @see https://en.wikipedia.org/wiki/CEA-708
  2728. * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
  2729. */
  2730. // payload type field to indicate how they are to be
  2731. // interpreted. CEAS-708 caption content is always transmitted with
  2732. // payload type 0x04.
  2733. var USER_DATA_REGISTERED_ITU_T_T35 = 4,
  2734. RBSP_TRAILING_BITS = 128;
  2735. /**
  2736. * Parse a supplemental enhancement information (SEI) NAL unit.
  2737. * Stops parsing once a message of type ITU T T35 has been found.
  2738. *
  2739. * @param bytes {Uint8Array} the bytes of a SEI NAL unit
  2740. * @return {object} the parsed SEI payload
  2741. * @see Rec. ITU-T H.264, 7.3.2.3.1
  2742. */
  2743. var parseSei = function parseSei(bytes) {
  2744. var i = 0,
  2745. result = {
  2746. payloadType: -1,
  2747. payloadSize: 0
  2748. },
  2749. payloadType = 0,
  2750. payloadSize = 0; // go through the sei_rbsp parsing each each individual sei_message
  2751. while (i < bytes.byteLength) {
  2752. // stop once we have hit the end of the sei_rbsp
  2753. if (bytes[i] === RBSP_TRAILING_BITS) {
  2754. break;
  2755. } // Parse payload type
  2756. while (bytes[i] === 0xFF) {
  2757. payloadType += 255;
  2758. i++;
  2759. }
  2760. payloadType += bytes[i++]; // Parse payload size
  2761. while (bytes[i] === 0xFF) {
  2762. payloadSize += 255;
  2763. i++;
  2764. }
  2765. payloadSize += bytes[i++]; // this sei_message is a 608/708 caption so save it and break
  2766. // there can only ever be one caption message in a frame's sei
  2767. if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
  2768. var userIdentifier = String.fromCharCode(bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6]);
  2769. if (userIdentifier === 'GA94') {
  2770. result.payloadType = payloadType;
  2771. result.payloadSize = payloadSize;
  2772. result.payload = bytes.subarray(i, i + payloadSize);
  2773. break;
  2774. } else {
  2775. result.payload = void 0;
  2776. }
  2777. } // skip the payload and parse the next message
  2778. i += payloadSize;
  2779. payloadType = 0;
  2780. payloadSize = 0;
  2781. }
  2782. return result;
  2783. }; // see ANSI/SCTE 128-1 (2013), section 8.1
  2784. var parseUserData = function parseUserData(sei) {
  2785. // itu_t_t35_contry_code must be 181 (United States) for
  2786. // captions
  2787. if (sei.payload[0] !== 181) {
  2788. return null;
  2789. } // itu_t_t35_provider_code should be 49 (ATSC) for captions
  2790. if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
  2791. return null;
  2792. } // the user_identifier should be "GA94" to indicate ATSC1 data
  2793. if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
  2794. return null;
  2795. } // finally, user_data_type_code should be 0x03 for caption data
  2796. if (sei.payload[7] !== 0x03) {
  2797. return null;
  2798. } // return the user_data_type_structure and strip the trailing
  2799. // marker bits
  2800. return sei.payload.subarray(8, sei.payload.length - 1);
  2801. }; // see CEA-708-D, section 4.4
  2802. var parseCaptionPackets = function parseCaptionPackets(pts, userData) {
  2803. var results = [],
  2804. i,
  2805. count,
  2806. offset,
  2807. data; // if this is just filler, return immediately
  2808. if (!(userData[0] & 0x40)) {
  2809. return results;
  2810. } // parse out the cc_data_1 and cc_data_2 fields
  2811. count = userData[0] & 0x1f;
  2812. for (i = 0; i < count; i++) {
  2813. offset = i * 3;
  2814. data = {
  2815. type: userData[offset + 2] & 0x03,
  2816. pts: pts
  2817. }; // capture cc data when cc_valid is 1
  2818. if (userData[offset + 2] & 0x04) {
  2819. data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
  2820. results.push(data);
  2821. }
  2822. }
  2823. return results;
  2824. };
  2825. var discardEmulationPreventionBytes$1 = function discardEmulationPreventionBytes(data) {
  2826. var length = data.byteLength,
  2827. emulationPreventionBytesPositions = [],
  2828. i = 1,
  2829. newLength,
  2830. newData; // Find all `Emulation Prevention Bytes`
  2831. while (i < length - 2) {
  2832. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  2833. emulationPreventionBytesPositions.push(i + 2);
  2834. i += 2;
  2835. } else {
  2836. i++;
  2837. }
  2838. } // If no Emulation Prevention Bytes were found just return the original
  2839. // array
  2840. if (emulationPreventionBytesPositions.length === 0) {
  2841. return data;
  2842. } // Create a new array to hold the NAL unit data
  2843. newLength = length - emulationPreventionBytesPositions.length;
  2844. newData = new Uint8Array(newLength);
  2845. var sourceIndex = 0;
  2846. for (i = 0; i < newLength; sourceIndex++, i++) {
  2847. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  2848. // Skip this byte
  2849. sourceIndex++; // Remove this position index
  2850. emulationPreventionBytesPositions.shift();
  2851. }
  2852. newData[i] = data[sourceIndex];
  2853. }
  2854. return newData;
  2855. }; // exports
  2856. var captionPacketParser = {
  2857. parseSei: parseSei,
  2858. parseUserData: parseUserData,
  2859. parseCaptionPackets: parseCaptionPackets,
  2860. discardEmulationPreventionBytes: discardEmulationPreventionBytes$1,
  2861. USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
  2862. };
  2863. // Link To Transport
  2864. // -----------------
  2865. var CaptionStream$1 = function CaptionStream(options) {
  2866. options = options || {};
  2867. CaptionStream.prototype.init.call(this); // parse708captions flag, default to true
  2868. this.parse708captions_ = typeof options.parse708captions === 'boolean' ? options.parse708captions : true;
  2869. this.captionPackets_ = [];
  2870. this.ccStreams_ = [new Cea608Stream(0, 0), // eslint-disable-line no-use-before-define
  2871. new Cea608Stream(0, 1), // eslint-disable-line no-use-before-define
  2872. new Cea608Stream(1, 0), // eslint-disable-line no-use-before-define
  2873. new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
  2874. ];
  2875. if (this.parse708captions_) {
  2876. this.cc708Stream_ = new Cea708Stream({
  2877. captionServices: options.captionServices
  2878. }); // eslint-disable-line no-use-before-define
  2879. }
  2880. this.reset(); // forward data and done events from CCs to this CaptionStream
  2881. this.ccStreams_.forEach(function (cc) {
  2882. cc.on('data', this.trigger.bind(this, 'data'));
  2883. cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
  2884. cc.on('done', this.trigger.bind(this, 'done'));
  2885. }, this);
  2886. if (this.parse708captions_) {
  2887. this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
  2888. this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
  2889. this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
  2890. }
  2891. };
  2892. CaptionStream$1.prototype = new stream();
  2893. CaptionStream$1.prototype.push = function (event) {
  2894. var sei, userData, newCaptionPackets; // only examine SEI NALs
  2895. if (event.nalUnitType !== 'sei_rbsp') {
  2896. return;
  2897. } // parse the sei
  2898. sei = captionPacketParser.parseSei(event.escapedRBSP); // no payload data, skip
  2899. if (!sei.payload) {
  2900. return;
  2901. } // ignore everything but user_data_registered_itu_t_t35
  2902. if (sei.payloadType !== captionPacketParser.USER_DATA_REGISTERED_ITU_T_T35) {
  2903. return;
  2904. } // parse out the user data payload
  2905. userData = captionPacketParser.parseUserData(sei); // ignore unrecognized userData
  2906. if (!userData) {
  2907. return;
  2908. } // Sometimes, the same segment # will be downloaded twice. To stop the
  2909. // caption data from being processed twice, we track the latest dts we've
  2910. // received and ignore everything with a dts before that. However, since
  2911. // data for a specific dts can be split across packets on either side of
  2912. // a segment boundary, we need to make sure we *don't* ignore the packets
  2913. // from the *next* segment that have dts === this.latestDts_. By constantly
  2914. // tracking the number of packets received with dts === this.latestDts_, we
  2915. // know how many should be ignored once we start receiving duplicates.
  2916. if (event.dts < this.latestDts_) {
  2917. // We've started getting older data, so set the flag.
  2918. this.ignoreNextEqualDts_ = true;
  2919. return;
  2920. } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
  2921. this.numSameDts_--;
  2922. if (!this.numSameDts_) {
  2923. // We've received the last duplicate packet, time to start processing again
  2924. this.ignoreNextEqualDts_ = false;
  2925. }
  2926. return;
  2927. } // parse out CC data packets and save them for later
  2928. newCaptionPackets = captionPacketParser.parseCaptionPackets(event.pts, userData);
  2929. this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
  2930. if (this.latestDts_ !== event.dts) {
  2931. this.numSameDts_ = 0;
  2932. }
  2933. this.numSameDts_++;
  2934. this.latestDts_ = event.dts;
  2935. };
  2936. CaptionStream$1.prototype.flushCCStreams = function (flushType) {
  2937. this.ccStreams_.forEach(function (cc) {
  2938. return flushType === 'flush' ? cc.flush() : cc.partialFlush();
  2939. }, this);
  2940. };
  2941. CaptionStream$1.prototype.flushStream = function (flushType) {
  2942. // make sure we actually parsed captions before proceeding
  2943. if (!this.captionPackets_.length) {
  2944. this.flushCCStreams(flushType);
  2945. return;
  2946. } // In Chrome, the Array#sort function is not stable so add a
  2947. // presortIndex that we can use to ensure we get a stable-sort
  2948. this.captionPackets_.forEach(function (elem, idx) {
  2949. elem.presortIndex = idx;
  2950. }); // sort caption byte-pairs based on their PTS values
  2951. this.captionPackets_.sort(function (a, b) {
  2952. if (a.pts === b.pts) {
  2953. return a.presortIndex - b.presortIndex;
  2954. }
  2955. return a.pts - b.pts;
  2956. });
  2957. this.captionPackets_.forEach(function (packet) {
  2958. if (packet.type < 2) {
  2959. // Dispatch packet to the right Cea608Stream
  2960. this.dispatchCea608Packet(packet);
  2961. } else {
  2962. // Dispatch packet to the Cea708Stream
  2963. this.dispatchCea708Packet(packet);
  2964. }
  2965. }, this);
  2966. this.captionPackets_.length = 0;
  2967. this.flushCCStreams(flushType);
  2968. };
  2969. CaptionStream$1.prototype.flush = function () {
  2970. return this.flushStream('flush');
  2971. }; // Only called if handling partial data
  2972. CaptionStream$1.prototype.partialFlush = function () {
  2973. return this.flushStream('partialFlush');
  2974. };
  2975. CaptionStream$1.prototype.reset = function () {
  2976. this.latestDts_ = null;
  2977. this.ignoreNextEqualDts_ = false;
  2978. this.numSameDts_ = 0;
  2979. this.activeCea608Channel_ = [null, null];
  2980. this.ccStreams_.forEach(function (ccStream) {
  2981. ccStream.reset();
  2982. });
  2983. }; // From the CEA-608 spec:
  2984. /*
  2985. * When XDS sub-packets are interleaved with other services, the end of each sub-packet shall be followed
  2986. * by a control pair to change to a different service. When any of the control codes from 0x10 to 0x1F is
  2987. * used to begin a control code pair, it indicates the return to captioning or Text data. The control code pair
  2988. * and subsequent data should then be processed according to the FCC rules. It may be necessary for the
  2989. * line 21 data encoder to automatically insert a control code pair (i.e. RCL, RU2, RU3, RU4, RDC, or RTD)
  2990. * to switch to captioning or Text.
  2991. */
  2992. // With that in mind, we ignore any data between an XDS control code and a
  2993. // subsequent closed-captioning control code.
  2994. CaptionStream$1.prototype.dispatchCea608Packet = function (packet) {
  2995. // NOTE: packet.type is the CEA608 field
  2996. if (this.setsTextOrXDSActive(packet)) {
  2997. this.activeCea608Channel_[packet.type] = null;
  2998. } else if (this.setsChannel1Active(packet)) {
  2999. this.activeCea608Channel_[packet.type] = 0;
  3000. } else if (this.setsChannel2Active(packet)) {
  3001. this.activeCea608Channel_[packet.type] = 1;
  3002. }
  3003. if (this.activeCea608Channel_[packet.type] === null) {
  3004. // If we haven't received anything to set the active channel, or the
  3005. // packets are Text/XDS data, discard the data; we don't want jumbled
  3006. // captions
  3007. return;
  3008. }
  3009. this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
  3010. };
  3011. CaptionStream$1.prototype.setsChannel1Active = function (packet) {
  3012. return (packet.ccData & 0x7800) === 0x1000;
  3013. };
  3014. CaptionStream$1.prototype.setsChannel2Active = function (packet) {
  3015. return (packet.ccData & 0x7800) === 0x1800;
  3016. };
  3017. CaptionStream$1.prototype.setsTextOrXDSActive = function (packet) {
  3018. return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
  3019. };
  3020. CaptionStream$1.prototype.dispatchCea708Packet = function (packet) {
  3021. if (this.parse708captions_) {
  3022. this.cc708Stream_.push(packet);
  3023. }
  3024. }; // ----------------------
  3025. // Session to Application
  3026. // ----------------------
  3027. // This hash maps special and extended character codes to their
  3028. // proper Unicode equivalent. The first one-byte key is just a
  3029. // non-standard character code. The two-byte keys that follow are
  3030. // the extended CEA708 character codes, along with the preceding
  3031. // 0x10 extended character byte to distinguish these codes from
  3032. // non-extended character codes. Every CEA708 character code that
  3033. // is not in this object maps directly to a standard unicode
  3034. // character code.
  3035. // The transparent space and non-breaking transparent space are
  3036. // technically not fully supported since there is no code to
  3037. // make them transparent, so they have normal non-transparent
  3038. // stand-ins.
  3039. // The special closed caption (CC) character isn't a standard
  3040. // unicode character, so a fairly similar unicode character was
  3041. // chosen in it's place.
  3042. var CHARACTER_TRANSLATION_708 = {
  3043. 0x7f: 0x266a,
  3044. // ♪
  3045. 0x1020: 0x20,
  3046. // Transparent Space
  3047. 0x1021: 0xa0,
  3048. // Nob-breaking Transparent Space
  3049. 0x1025: 0x2026,
  3050. // …
  3051. 0x102a: 0x0160,
  3052. // Š
  3053. 0x102c: 0x0152,
  3054. // Œ
  3055. 0x1030: 0x2588,
  3056. // █
  3057. 0x1031: 0x2018,
  3058. // ‘
  3059. 0x1032: 0x2019,
  3060. // ’
  3061. 0x1033: 0x201c,
  3062. // “
  3063. 0x1034: 0x201d,
  3064. // ”
  3065. 0x1035: 0x2022,
  3066. // •
  3067. 0x1039: 0x2122,
  3068. // ™
  3069. 0x103a: 0x0161,
  3070. // š
  3071. 0x103c: 0x0153,
  3072. // œ
  3073. 0x103d: 0x2120,
  3074. // ℠
  3075. 0x103f: 0x0178,
  3076. // Ÿ
  3077. 0x1076: 0x215b,
  3078. // ⅛
  3079. 0x1077: 0x215c,
  3080. // ⅜
  3081. 0x1078: 0x215d,
  3082. // ⅝
  3083. 0x1079: 0x215e,
  3084. // ⅞
  3085. 0x107a: 0x23d0,
  3086. // ⏐
  3087. 0x107b: 0x23a4,
  3088. // ⎤
  3089. 0x107c: 0x23a3,
  3090. // ⎣
  3091. 0x107d: 0x23af,
  3092. // ⎯
  3093. 0x107e: 0x23a6,
  3094. // ⎦
  3095. 0x107f: 0x23a1,
  3096. // ⎡
  3097. 0x10a0: 0x3138 // ㄸ (CC char)
  3098. };
  3099. var get708CharFromCode = function get708CharFromCode(code) {
  3100. var newCode = CHARACTER_TRANSLATION_708[code] || code;
  3101. if (code & 0x1000 && code === newCode) {
  3102. // Invalid extended code
  3103. return '';
  3104. }
  3105. return String.fromCharCode(newCode);
  3106. };
  3107. var within708TextBlock = function within708TextBlock(b) {
  3108. return 0x20 <= b && b <= 0x7f || 0xa0 <= b && b <= 0xff;
  3109. };
  3110. var Cea708Window = function Cea708Window(windowNum) {
  3111. this.windowNum = windowNum;
  3112. this.reset();
  3113. };
  3114. Cea708Window.prototype.reset = function () {
  3115. this.clearText();
  3116. this.pendingNewLine = false;
  3117. this.winAttr = {};
  3118. this.penAttr = {};
  3119. this.penLoc = {};
  3120. this.penColor = {}; // These default values are arbitrary,
  3121. // defineWindow will usually override them
  3122. this.visible = 0;
  3123. this.rowLock = 0;
  3124. this.columnLock = 0;
  3125. this.priority = 0;
  3126. this.relativePositioning = 0;
  3127. this.anchorVertical = 0;
  3128. this.anchorHorizontal = 0;
  3129. this.anchorPoint = 0;
  3130. this.rowCount = 1;
  3131. this.virtualRowCount = this.rowCount + 1;
  3132. this.columnCount = 41;
  3133. this.windowStyle = 0;
  3134. this.penStyle = 0;
  3135. };
  3136. Cea708Window.prototype.getText = function () {
  3137. return this.rows.join('\n');
  3138. };
  3139. Cea708Window.prototype.clearText = function () {
  3140. this.rows = [''];
  3141. this.rowIdx = 0;
  3142. };
  3143. Cea708Window.prototype.newLine = function (pts) {
  3144. if (this.rows.length >= this.virtualRowCount && typeof this.beforeRowOverflow === 'function') {
  3145. this.beforeRowOverflow(pts);
  3146. }
  3147. if (this.rows.length > 0) {
  3148. this.rows.push('');
  3149. this.rowIdx++;
  3150. } // Show all virtual rows since there's no visible scrolling
  3151. while (this.rows.length > this.virtualRowCount) {
  3152. this.rows.shift();
  3153. this.rowIdx--;
  3154. }
  3155. };
  3156. Cea708Window.prototype.isEmpty = function () {
  3157. if (this.rows.length === 0) {
  3158. return true;
  3159. } else if (this.rows.length === 1) {
  3160. return this.rows[0] === '';
  3161. }
  3162. return false;
  3163. };
  3164. Cea708Window.prototype.addText = function (text) {
  3165. this.rows[this.rowIdx] += text;
  3166. };
  3167. Cea708Window.prototype.backspace = function () {
  3168. if (!this.isEmpty()) {
  3169. var row = this.rows[this.rowIdx];
  3170. this.rows[this.rowIdx] = row.substr(0, row.length - 1);
  3171. }
  3172. };
  3173. var Cea708Service = function Cea708Service(serviceNum, encoding, stream) {
  3174. this.serviceNum = serviceNum;
  3175. this.text = '';
  3176. this.currentWindow = new Cea708Window(-1);
  3177. this.windows = [];
  3178. this.stream = stream; // Try to setup a TextDecoder if an `encoding` value was provided
  3179. if (typeof encoding === 'string') {
  3180. this.createTextDecoder(encoding);
  3181. }
  3182. };
  3183. /**
  3184. * Initialize service windows
  3185. * Must be run before service use
  3186. *
  3187. * @param {Integer} pts PTS value
  3188. * @param {Function} beforeRowOverflow Function to execute before row overflow of a window
  3189. */
  3190. Cea708Service.prototype.init = function (pts, beforeRowOverflow) {
  3191. this.startPts = pts;
  3192. for (var win = 0; win < 8; win++) {
  3193. this.windows[win] = new Cea708Window(win);
  3194. if (typeof beforeRowOverflow === 'function') {
  3195. this.windows[win].beforeRowOverflow = beforeRowOverflow;
  3196. }
  3197. }
  3198. };
  3199. /**
  3200. * Set current window of service to be affected by commands
  3201. *
  3202. * @param {Integer} windowNum Window number
  3203. */
  3204. Cea708Service.prototype.setCurrentWindow = function (windowNum) {
  3205. this.currentWindow = this.windows[windowNum];
  3206. };
  3207. /**
  3208. * Try to create a TextDecoder if it is natively supported
  3209. */
  3210. Cea708Service.prototype.createTextDecoder = function (encoding) {
  3211. if (typeof TextDecoder === 'undefined') {
  3212. this.stream.trigger('log', {
  3213. level: 'warn',
  3214. message: 'The `encoding` option is unsupported without TextDecoder support'
  3215. });
  3216. } else {
  3217. try {
  3218. this.textDecoder_ = new TextDecoder(encoding);
  3219. } catch (error) {
  3220. this.stream.trigger('log', {
  3221. level: 'warn',
  3222. message: 'TextDecoder could not be created with ' + encoding + ' encoding. ' + error
  3223. });
  3224. }
  3225. }
  3226. };
  3227. var Cea708Stream = function Cea708Stream(options) {
  3228. options = options || {};
  3229. Cea708Stream.prototype.init.call(this);
  3230. var self = this;
  3231. var captionServices = options.captionServices || {};
  3232. var captionServiceEncodings = {};
  3233. var serviceProps; // Get service encodings from captionServices option block
  3234. Object.keys(captionServices).forEach(function (serviceName) {
  3235. serviceProps = captionServices[serviceName];
  3236. if (/^SERVICE/.test(serviceName)) {
  3237. captionServiceEncodings[serviceName] = serviceProps.encoding;
  3238. }
  3239. });
  3240. this.serviceEncodings = captionServiceEncodings;
  3241. this.current708Packet = null;
  3242. this.services = {};
  3243. this.push = function (packet) {
  3244. if (packet.type === 3) {
  3245. // 708 packet start
  3246. self.new708Packet();
  3247. self.add708Bytes(packet);
  3248. } else {
  3249. if (self.current708Packet === null) {
  3250. // This should only happen at the start of a file if there's no packet start.
  3251. self.new708Packet();
  3252. }
  3253. self.add708Bytes(packet);
  3254. }
  3255. };
  3256. };
  3257. Cea708Stream.prototype = new stream();
  3258. /**
  3259. * Push current 708 packet, create new 708 packet.
  3260. */
  3261. Cea708Stream.prototype.new708Packet = function () {
  3262. if (this.current708Packet !== null) {
  3263. this.push708Packet();
  3264. }
  3265. this.current708Packet = {
  3266. data: [],
  3267. ptsVals: []
  3268. };
  3269. };
  3270. /**
  3271. * Add pts and both bytes from packet into current 708 packet.
  3272. */
  3273. Cea708Stream.prototype.add708Bytes = function (packet) {
  3274. var data = packet.ccData;
  3275. var byte0 = data >>> 8;
  3276. var byte1 = data & 0xff; // I would just keep a list of packets instead of bytes, but it isn't clear in the spec
  3277. // that service blocks will always line up with byte pairs.
  3278. this.current708Packet.ptsVals.push(packet.pts);
  3279. this.current708Packet.data.push(byte0);
  3280. this.current708Packet.data.push(byte1);
  3281. };
  3282. /**
  3283. * Parse completed 708 packet into service blocks and push each service block.
  3284. */
  3285. Cea708Stream.prototype.push708Packet = function () {
  3286. var packet708 = this.current708Packet;
  3287. var packetData = packet708.data;
  3288. var serviceNum = null;
  3289. var blockSize = null;
  3290. var i = 0;
  3291. var b = packetData[i++];
  3292. packet708.seq = b >> 6;
  3293. packet708.sizeCode = b & 0x3f; // 0b00111111;
  3294. for (; i < packetData.length; i++) {
  3295. b = packetData[i++];
  3296. serviceNum = b >> 5;
  3297. blockSize = b & 0x1f; // 0b00011111
  3298. if (serviceNum === 7 && blockSize > 0) {
  3299. // Extended service num
  3300. b = packetData[i++];
  3301. serviceNum = b;
  3302. }
  3303. this.pushServiceBlock(serviceNum, i, blockSize);
  3304. if (blockSize > 0) {
  3305. i += blockSize - 1;
  3306. }
  3307. }
  3308. };
  3309. /**
  3310. * Parse service block, execute commands, read text.
  3311. *
  3312. * Note: While many of these commands serve important purposes,
  3313. * many others just parse out the parameters or attributes, but
  3314. * nothing is done with them because this is not a full and complete
  3315. * implementation of the entire 708 spec.
  3316. *
  3317. * @param {Integer} serviceNum Service number
  3318. * @param {Integer} start Start index of the 708 packet data
  3319. * @param {Integer} size Block size
  3320. */
  3321. Cea708Stream.prototype.pushServiceBlock = function (serviceNum, start, size) {
  3322. var b;
  3323. var i = start;
  3324. var packetData = this.current708Packet.data;
  3325. var service = this.services[serviceNum];
  3326. if (!service) {
  3327. service = this.initService(serviceNum, i);
  3328. }
  3329. for (; i < start + size && i < packetData.length; i++) {
  3330. b = packetData[i];
  3331. if (within708TextBlock(b)) {
  3332. i = this.handleText(i, service);
  3333. } else if (b === 0x18) {
  3334. i = this.multiByteCharacter(i, service);
  3335. } else if (b === 0x10) {
  3336. i = this.extendedCommands(i, service);
  3337. } else if (0x80 <= b && b <= 0x87) {
  3338. i = this.setCurrentWindow(i, service);
  3339. } else if (0x98 <= b && b <= 0x9f) {
  3340. i = this.defineWindow(i, service);
  3341. } else if (b === 0x88) {
  3342. i = this.clearWindows(i, service);
  3343. } else if (b === 0x8c) {
  3344. i = this.deleteWindows(i, service);
  3345. } else if (b === 0x89) {
  3346. i = this.displayWindows(i, service);
  3347. } else if (b === 0x8a) {
  3348. i = this.hideWindows(i, service);
  3349. } else if (b === 0x8b) {
  3350. i = this.toggleWindows(i, service);
  3351. } else if (b === 0x97) {
  3352. i = this.setWindowAttributes(i, service);
  3353. } else if (b === 0x90) {
  3354. i = this.setPenAttributes(i, service);
  3355. } else if (b === 0x91) {
  3356. i = this.setPenColor(i, service);
  3357. } else if (b === 0x92) {
  3358. i = this.setPenLocation(i, service);
  3359. } else if (b === 0x8f) {
  3360. service = this.reset(i, service);
  3361. } else if (b === 0x08) {
  3362. // BS: Backspace
  3363. service.currentWindow.backspace();
  3364. } else if (b === 0x0c) {
  3365. // FF: Form feed
  3366. service.currentWindow.clearText();
  3367. } else if (b === 0x0d) {
  3368. // CR: Carriage return
  3369. service.currentWindow.pendingNewLine = true;
  3370. } else if (b === 0x0e) {
  3371. // HCR: Horizontal carriage return
  3372. service.currentWindow.clearText();
  3373. } else if (b === 0x8d) {
  3374. // DLY: Delay, nothing to do
  3375. i++;
  3376. } else ;
  3377. }
  3378. };
  3379. /**
  3380. * Execute an extended command
  3381. *
  3382. * @param {Integer} i Current index in the 708 packet
  3383. * @param {Service} service The service object to be affected
  3384. * @return {Integer} New index after parsing
  3385. */
  3386. Cea708Stream.prototype.extendedCommands = function (i, service) {
  3387. var packetData = this.current708Packet.data;
  3388. var b = packetData[++i];
  3389. if (within708TextBlock(b)) {
  3390. i = this.handleText(i, service, {
  3391. isExtended: true
  3392. });
  3393. }
  3394. return i;
  3395. };
  3396. /**
  3397. * Get PTS value of a given byte index
  3398. *
  3399. * @param {Integer} byteIndex Index of the byte
  3400. * @return {Integer} PTS
  3401. */
  3402. Cea708Stream.prototype.getPts = function (byteIndex) {
  3403. // There's 1 pts value per 2 bytes
  3404. return this.current708Packet.ptsVals[Math.floor(byteIndex / 2)];
  3405. };
  3406. /**
  3407. * Initializes a service
  3408. *
  3409. * @param {Integer} serviceNum Service number
  3410. * @return {Service} Initialized service object
  3411. */
  3412. Cea708Stream.prototype.initService = function (serviceNum, i) {
  3413. var serviceName = 'SERVICE' + serviceNum;
  3414. var self = this;
  3415. var serviceName;
  3416. var encoding;
  3417. if (serviceName in this.serviceEncodings) {
  3418. encoding = this.serviceEncodings[serviceName];
  3419. }
  3420. this.services[serviceNum] = new Cea708Service(serviceNum, encoding, self);
  3421. this.services[serviceNum].init(this.getPts(i), function (pts) {
  3422. self.flushDisplayed(pts, self.services[serviceNum]);
  3423. });
  3424. return this.services[serviceNum];
  3425. };
  3426. /**
  3427. * Execute text writing to current window
  3428. *
  3429. * @param {Integer} i Current index in the 708 packet
  3430. * @param {Service} service The service object to be affected
  3431. * @return {Integer} New index after parsing
  3432. */
  3433. Cea708Stream.prototype.handleText = function (i, service, options) {
  3434. var isExtended = options && options.isExtended;
  3435. var isMultiByte = options && options.isMultiByte;
  3436. var packetData = this.current708Packet.data;
  3437. var extended = isExtended ? 0x1000 : 0x0000;
  3438. var currentByte = packetData[i];
  3439. var nextByte = packetData[i + 1];
  3440. var win = service.currentWindow;
  3441. var char;
  3442. var charCodeArray; // Use the TextDecoder if one was created for this service
  3443. if (service.textDecoder_ && !isExtended) {
  3444. if (isMultiByte) {
  3445. charCodeArray = [currentByte, nextByte];
  3446. i++;
  3447. } else {
  3448. charCodeArray = [currentByte];
  3449. }
  3450. char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
  3451. } else {
  3452. char = get708CharFromCode(extended | currentByte);
  3453. }
  3454. if (win.pendingNewLine && !win.isEmpty()) {
  3455. win.newLine(this.getPts(i));
  3456. }
  3457. win.pendingNewLine = false;
  3458. win.addText(char);
  3459. return i;
  3460. };
  3461. /**
  3462. * Handle decoding of multibyte character
  3463. *
  3464. * @param {Integer} i Current index in the 708 packet
  3465. * @param {Service} service The service object to be affected
  3466. * @return {Integer} New index after parsing
  3467. */
  3468. Cea708Stream.prototype.multiByteCharacter = function (i, service) {
  3469. var packetData = this.current708Packet.data;
  3470. var firstByte = packetData[i + 1];
  3471. var secondByte = packetData[i + 2];
  3472. if (within708TextBlock(firstByte) && within708TextBlock(secondByte)) {
  3473. i = this.handleText(++i, service, {
  3474. isMultiByte: true
  3475. });
  3476. }
  3477. return i;
  3478. };
  3479. /**
  3480. * Parse and execute the CW# command.
  3481. *
  3482. * Set the current window.
  3483. *
  3484. * @param {Integer} i Current index in the 708 packet
  3485. * @param {Service} service The service object to be affected
  3486. * @return {Integer} New index after parsing
  3487. */
  3488. Cea708Stream.prototype.setCurrentWindow = function (i, service) {
  3489. var packetData = this.current708Packet.data;
  3490. var b = packetData[i];
  3491. var windowNum = b & 0x07;
  3492. service.setCurrentWindow(windowNum);
  3493. return i;
  3494. };
  3495. /**
  3496. * Parse and execute the DF# command.
  3497. *
  3498. * Define a window and set it as the current window.
  3499. *
  3500. * @param {Integer} i Current index in the 708 packet
  3501. * @param {Service} service The service object to be affected
  3502. * @return {Integer} New index after parsing
  3503. */
  3504. Cea708Stream.prototype.defineWindow = function (i, service) {
  3505. var packetData = this.current708Packet.data;
  3506. var b = packetData[i];
  3507. var windowNum = b & 0x07;
  3508. service.setCurrentWindow(windowNum);
  3509. var win = service.currentWindow;
  3510. b = packetData[++i];
  3511. win.visible = (b & 0x20) >> 5; // v
  3512. win.rowLock = (b & 0x10) >> 4; // rl
  3513. win.columnLock = (b & 0x08) >> 3; // cl
  3514. win.priority = b & 0x07; // p
  3515. b = packetData[++i];
  3516. win.relativePositioning = (b & 0x80) >> 7; // rp
  3517. win.anchorVertical = b & 0x7f; // av
  3518. b = packetData[++i];
  3519. win.anchorHorizontal = b; // ah
  3520. b = packetData[++i];
  3521. win.anchorPoint = (b & 0xf0) >> 4; // ap
  3522. win.rowCount = b & 0x0f; // rc
  3523. b = packetData[++i];
  3524. win.columnCount = b & 0x3f; // cc
  3525. b = packetData[++i];
  3526. win.windowStyle = (b & 0x38) >> 3; // ws
  3527. win.penStyle = b & 0x07; // ps
  3528. // The spec says there are (rowCount+1) "virtual rows"
  3529. win.virtualRowCount = win.rowCount + 1;
  3530. return i;
  3531. };
  3532. /**
  3533. * Parse and execute the SWA command.
  3534. *
  3535. * Set attributes of the current window.
  3536. *
  3537. * @param {Integer} i Current index in the 708 packet
  3538. * @param {Service} service The service object to be affected
  3539. * @return {Integer} New index after parsing
  3540. */
  3541. Cea708Stream.prototype.setWindowAttributes = function (i, service) {
  3542. var packetData = this.current708Packet.data;
  3543. var b = packetData[i];
  3544. var winAttr = service.currentWindow.winAttr;
  3545. b = packetData[++i];
  3546. winAttr.fillOpacity = (b & 0xc0) >> 6; // fo
  3547. winAttr.fillRed = (b & 0x30) >> 4; // fr
  3548. winAttr.fillGreen = (b & 0x0c) >> 2; // fg
  3549. winAttr.fillBlue = b & 0x03; // fb
  3550. b = packetData[++i];
  3551. winAttr.borderType = (b & 0xc0) >> 6; // bt
  3552. winAttr.borderRed = (b & 0x30) >> 4; // br
  3553. winAttr.borderGreen = (b & 0x0c) >> 2; // bg
  3554. winAttr.borderBlue = b & 0x03; // bb
  3555. b = packetData[++i];
  3556. winAttr.borderType += (b & 0x80) >> 5; // bt
  3557. winAttr.wordWrap = (b & 0x40) >> 6; // ww
  3558. winAttr.printDirection = (b & 0x30) >> 4; // pd
  3559. winAttr.scrollDirection = (b & 0x0c) >> 2; // sd
  3560. winAttr.justify = b & 0x03; // j
  3561. b = packetData[++i];
  3562. winAttr.effectSpeed = (b & 0xf0) >> 4; // es
  3563. winAttr.effectDirection = (b & 0x0c) >> 2; // ed
  3564. winAttr.displayEffect = b & 0x03; // de
  3565. return i;
  3566. };
  3567. /**
  3568. * Gather text from all displayed windows and push a caption to output.
  3569. *
  3570. * @param {Integer} i Current index in the 708 packet
  3571. * @param {Service} service The service object to be affected
  3572. */
  3573. Cea708Stream.prototype.flushDisplayed = function (pts, service) {
  3574. var displayedText = []; // TODO: Positioning not supported, displaying multiple windows will not necessarily
  3575. // display text in the correct order, but sample files so far have not shown any issue.
  3576. for (var winId = 0; winId < 8; winId++) {
  3577. if (service.windows[winId].visible && !service.windows[winId].isEmpty()) {
  3578. displayedText.push(service.windows[winId].getText());
  3579. }
  3580. }
  3581. service.endPts = pts;
  3582. service.text = displayedText.join('\n\n');
  3583. this.pushCaption(service);
  3584. service.startPts = pts;
  3585. };
  3586. /**
  3587. * Push a caption to output if the caption contains text.
  3588. *
  3589. * @param {Service} service The service object to be affected
  3590. */
  3591. Cea708Stream.prototype.pushCaption = function (service) {
  3592. if (service.text !== '') {
  3593. this.trigger('data', {
  3594. startPts: service.startPts,
  3595. endPts: service.endPts,
  3596. text: service.text,
  3597. stream: 'cc708_' + service.serviceNum
  3598. });
  3599. service.text = '';
  3600. service.startPts = service.endPts;
  3601. }
  3602. };
  3603. /**
  3604. * Parse and execute the DSW command.
  3605. *
  3606. * Set visible property of windows based on the parsed bitmask.
  3607. *
  3608. * @param {Integer} i Current index in the 708 packet
  3609. * @param {Service} service The service object to be affected
  3610. * @return {Integer} New index after parsing
  3611. */
  3612. Cea708Stream.prototype.displayWindows = function (i, service) {
  3613. var packetData = this.current708Packet.data;
  3614. var b = packetData[++i];
  3615. var pts = this.getPts(i);
  3616. this.flushDisplayed(pts, service);
  3617. for (var winId = 0; winId < 8; winId++) {
  3618. if (b & 0x01 << winId) {
  3619. service.windows[winId].visible = 1;
  3620. }
  3621. }
  3622. return i;
  3623. };
  3624. /**
  3625. * Parse and execute the HDW command.
  3626. *
  3627. * Set visible property of windows based on the parsed bitmask.
  3628. *
  3629. * @param {Integer} i Current index in the 708 packet
  3630. * @param {Service} service The service object to be affected
  3631. * @return {Integer} New index after parsing
  3632. */
  3633. Cea708Stream.prototype.hideWindows = function (i, service) {
  3634. var packetData = this.current708Packet.data;
  3635. var b = packetData[++i];
  3636. var pts = this.getPts(i);
  3637. this.flushDisplayed(pts, service);
  3638. for (var winId = 0; winId < 8; winId++) {
  3639. if (b & 0x01 << winId) {
  3640. service.windows[winId].visible = 0;
  3641. }
  3642. }
  3643. return i;
  3644. };
  3645. /**
  3646. * Parse and execute the TGW command.
  3647. *
  3648. * Set visible property of windows based on the parsed bitmask.
  3649. *
  3650. * @param {Integer} i Current index in the 708 packet
  3651. * @param {Service} service The service object to be affected
  3652. * @return {Integer} New index after parsing
  3653. */
  3654. Cea708Stream.prototype.toggleWindows = function (i, service) {
  3655. var packetData = this.current708Packet.data;
  3656. var b = packetData[++i];
  3657. var pts = this.getPts(i);
  3658. this.flushDisplayed(pts, service);
  3659. for (var winId = 0; winId < 8; winId++) {
  3660. if (b & 0x01 << winId) {
  3661. service.windows[winId].visible ^= 1;
  3662. }
  3663. }
  3664. return i;
  3665. };
  3666. /**
  3667. * Parse and execute the CLW command.
  3668. *
  3669. * Clear text of windows based on the parsed bitmask.
  3670. *
  3671. * @param {Integer} i Current index in the 708 packet
  3672. * @param {Service} service The service object to be affected
  3673. * @return {Integer} New index after parsing
  3674. */
  3675. Cea708Stream.prototype.clearWindows = function (i, service) {
  3676. var packetData = this.current708Packet.data;
  3677. var b = packetData[++i];
  3678. var pts = this.getPts(i);
  3679. this.flushDisplayed(pts, service);
  3680. for (var winId = 0; winId < 8; winId++) {
  3681. if (b & 0x01 << winId) {
  3682. service.windows[winId].clearText();
  3683. }
  3684. }
  3685. return i;
  3686. };
  3687. /**
  3688. * Parse and execute the DLW command.
  3689. *
  3690. * Re-initialize windows based on the parsed bitmask.
  3691. *
  3692. * @param {Integer} i Current index in the 708 packet
  3693. * @param {Service} service The service object to be affected
  3694. * @return {Integer} New index after parsing
  3695. */
  3696. Cea708Stream.prototype.deleteWindows = function (i, service) {
  3697. var packetData = this.current708Packet.data;
  3698. var b = packetData[++i];
  3699. var pts = this.getPts(i);
  3700. this.flushDisplayed(pts, service);
  3701. for (var winId = 0; winId < 8; winId++) {
  3702. if (b & 0x01 << winId) {
  3703. service.windows[winId].reset();
  3704. }
  3705. }
  3706. return i;
  3707. };
  3708. /**
  3709. * Parse and execute the SPA command.
  3710. *
  3711. * Set pen attributes of the current window.
  3712. *
  3713. * @param {Integer} i Current index in the 708 packet
  3714. * @param {Service} service The service object to be affected
  3715. * @return {Integer} New index after parsing
  3716. */
  3717. Cea708Stream.prototype.setPenAttributes = function (i, service) {
  3718. var packetData = this.current708Packet.data;
  3719. var b = packetData[i];
  3720. var penAttr = service.currentWindow.penAttr;
  3721. b = packetData[++i];
  3722. penAttr.textTag = (b & 0xf0) >> 4; // tt
  3723. penAttr.offset = (b & 0x0c) >> 2; // o
  3724. penAttr.penSize = b & 0x03; // s
  3725. b = packetData[++i];
  3726. penAttr.italics = (b & 0x80) >> 7; // i
  3727. penAttr.underline = (b & 0x40) >> 6; // u
  3728. penAttr.edgeType = (b & 0x38) >> 3; // et
  3729. penAttr.fontStyle = b & 0x07; // fs
  3730. return i;
  3731. };
  3732. /**
  3733. * Parse and execute the SPC command.
  3734. *
  3735. * Set pen color of the current window.
  3736. *
  3737. * @param {Integer} i Current index in the 708 packet
  3738. * @param {Service} service The service object to be affected
  3739. * @return {Integer} New index after parsing
  3740. */
  3741. Cea708Stream.prototype.setPenColor = function (i, service) {
  3742. var packetData = this.current708Packet.data;
  3743. var b = packetData[i];
  3744. var penColor = service.currentWindow.penColor;
  3745. b = packetData[++i];
  3746. penColor.fgOpacity = (b & 0xc0) >> 6; // fo
  3747. penColor.fgRed = (b & 0x30) >> 4; // fr
  3748. penColor.fgGreen = (b & 0x0c) >> 2; // fg
  3749. penColor.fgBlue = b & 0x03; // fb
  3750. b = packetData[++i];
  3751. penColor.bgOpacity = (b & 0xc0) >> 6; // bo
  3752. penColor.bgRed = (b & 0x30) >> 4; // br
  3753. penColor.bgGreen = (b & 0x0c) >> 2; // bg
  3754. penColor.bgBlue = b & 0x03; // bb
  3755. b = packetData[++i];
  3756. penColor.edgeRed = (b & 0x30) >> 4; // er
  3757. penColor.edgeGreen = (b & 0x0c) >> 2; // eg
  3758. penColor.edgeBlue = b & 0x03; // eb
  3759. return i;
  3760. };
  3761. /**
  3762. * Parse and execute the SPL command.
  3763. *
  3764. * Set pen location of the current window.
  3765. *
  3766. * @param {Integer} i Current index in the 708 packet
  3767. * @param {Service} service The service object to be affected
  3768. * @return {Integer} New index after parsing
  3769. */
  3770. Cea708Stream.prototype.setPenLocation = function (i, service) {
  3771. var packetData = this.current708Packet.data;
  3772. var b = packetData[i];
  3773. var penLoc = service.currentWindow.penLoc; // Positioning isn't really supported at the moment, so this essentially just inserts a linebreak
  3774. service.currentWindow.pendingNewLine = true;
  3775. b = packetData[++i];
  3776. penLoc.row = b & 0x0f; // r
  3777. b = packetData[++i];
  3778. penLoc.column = b & 0x3f; // c
  3779. return i;
  3780. };
  3781. /**
  3782. * Execute the RST command.
  3783. *
  3784. * Reset service to a clean slate. Re-initialize.
  3785. *
  3786. * @param {Integer} i Current index in the 708 packet
  3787. * @param {Service} service The service object to be affected
  3788. * @return {Service} Re-initialized service
  3789. */
  3790. Cea708Stream.prototype.reset = function (i, service) {
  3791. var pts = this.getPts(i);
  3792. this.flushDisplayed(pts, service);
  3793. return this.initService(service.serviceNum, i);
  3794. }; // This hash maps non-ASCII, special, and extended character codes to their
  3795. // proper Unicode equivalent. The first keys that are only a single byte
  3796. // are the non-standard ASCII characters, which simply map the CEA608 byte
  3797. // to the standard ASCII/Unicode. The two-byte keys that follow are the CEA608
  3798. // character codes, but have their MSB bitmasked with 0x03 so that a lookup
  3799. // can be performed regardless of the field and data channel on which the
  3800. // character code was received.
  3801. var CHARACTER_TRANSLATION = {
  3802. 0x2a: 0xe1,
  3803. // á
  3804. 0x5c: 0xe9,
  3805. // é
  3806. 0x5e: 0xed,
  3807. // í
  3808. 0x5f: 0xf3,
  3809. // ó
  3810. 0x60: 0xfa,
  3811. // ú
  3812. 0x7b: 0xe7,
  3813. // ç
  3814. 0x7c: 0xf7,
  3815. // ÷
  3816. 0x7d: 0xd1,
  3817. // Ñ
  3818. 0x7e: 0xf1,
  3819. // ñ
  3820. 0x7f: 0x2588,
  3821. // █
  3822. 0x0130: 0xae,
  3823. // ®
  3824. 0x0131: 0xb0,
  3825. // °
  3826. 0x0132: 0xbd,
  3827. // ½
  3828. 0x0133: 0xbf,
  3829. // ¿
  3830. 0x0134: 0x2122,
  3831. // ™
  3832. 0x0135: 0xa2,
  3833. // ¢
  3834. 0x0136: 0xa3,
  3835. // £
  3836. 0x0137: 0x266a,
  3837. // ♪
  3838. 0x0138: 0xe0,
  3839. // à
  3840. 0x0139: 0xa0,
  3841. //
  3842. 0x013a: 0xe8,
  3843. // è
  3844. 0x013b: 0xe2,
  3845. // â
  3846. 0x013c: 0xea,
  3847. // ê
  3848. 0x013d: 0xee,
  3849. // î
  3850. 0x013e: 0xf4,
  3851. // ô
  3852. 0x013f: 0xfb,
  3853. // û
  3854. 0x0220: 0xc1,
  3855. // Á
  3856. 0x0221: 0xc9,
  3857. // É
  3858. 0x0222: 0xd3,
  3859. // Ó
  3860. 0x0223: 0xda,
  3861. // Ú
  3862. 0x0224: 0xdc,
  3863. // Ü
  3864. 0x0225: 0xfc,
  3865. // ü
  3866. 0x0226: 0x2018,
  3867. // ‘
  3868. 0x0227: 0xa1,
  3869. // ¡
  3870. 0x0228: 0x2a,
  3871. // *
  3872. 0x0229: 0x27,
  3873. // '
  3874. 0x022a: 0x2014,
  3875. // —
  3876. 0x022b: 0xa9,
  3877. // ©
  3878. 0x022c: 0x2120,
  3879. // ℠
  3880. 0x022d: 0x2022,
  3881. // •
  3882. 0x022e: 0x201c,
  3883. // “
  3884. 0x022f: 0x201d,
  3885. // ”
  3886. 0x0230: 0xc0,
  3887. // À
  3888. 0x0231: 0xc2,
  3889. // Â
  3890. 0x0232: 0xc7,
  3891. // Ç
  3892. 0x0233: 0xc8,
  3893. // È
  3894. 0x0234: 0xca,
  3895. // Ê
  3896. 0x0235: 0xcb,
  3897. // Ë
  3898. 0x0236: 0xeb,
  3899. // ë
  3900. 0x0237: 0xce,
  3901. // Î
  3902. 0x0238: 0xcf,
  3903. // Ï
  3904. 0x0239: 0xef,
  3905. // ï
  3906. 0x023a: 0xd4,
  3907. // Ô
  3908. 0x023b: 0xd9,
  3909. // Ù
  3910. 0x023c: 0xf9,
  3911. // ù
  3912. 0x023d: 0xdb,
  3913. // Û
  3914. 0x023e: 0xab,
  3915. // «
  3916. 0x023f: 0xbb,
  3917. // »
  3918. 0x0320: 0xc3,
  3919. // Ã
  3920. 0x0321: 0xe3,
  3921. // ã
  3922. 0x0322: 0xcd,
  3923. // Í
  3924. 0x0323: 0xcc,
  3925. // Ì
  3926. 0x0324: 0xec,
  3927. // ì
  3928. 0x0325: 0xd2,
  3929. // Ò
  3930. 0x0326: 0xf2,
  3931. // ò
  3932. 0x0327: 0xd5,
  3933. // Õ
  3934. 0x0328: 0xf5,
  3935. // õ
  3936. 0x0329: 0x7b,
  3937. // {
  3938. 0x032a: 0x7d,
  3939. // }
  3940. 0x032b: 0x5c,
  3941. // \
  3942. 0x032c: 0x5e,
  3943. // ^
  3944. 0x032d: 0x5f,
  3945. // _
  3946. 0x032e: 0x7c,
  3947. // |
  3948. 0x032f: 0x7e,
  3949. // ~
  3950. 0x0330: 0xc4,
  3951. // Ä
  3952. 0x0331: 0xe4,
  3953. // ä
  3954. 0x0332: 0xd6,
  3955. // Ö
  3956. 0x0333: 0xf6,
  3957. // ö
  3958. 0x0334: 0xdf,
  3959. // ß
  3960. 0x0335: 0xa5,
  3961. // ¥
  3962. 0x0336: 0xa4,
  3963. // ¤
  3964. 0x0337: 0x2502,
  3965. // │
  3966. 0x0338: 0xc5,
  3967. // Å
  3968. 0x0339: 0xe5,
  3969. // å
  3970. 0x033a: 0xd8,
  3971. // Ø
  3972. 0x033b: 0xf8,
  3973. // ø
  3974. 0x033c: 0x250c,
  3975. // ┌
  3976. 0x033d: 0x2510,
  3977. // ┐
  3978. 0x033e: 0x2514,
  3979. // └
  3980. 0x033f: 0x2518 // ┘
  3981. };
  3982. var getCharFromCode = function getCharFromCode(code) {
  3983. if (code === null) {
  3984. return '';
  3985. }
  3986. code = CHARACTER_TRANSLATION[code] || code;
  3987. return String.fromCharCode(code);
  3988. }; // the index of the last row in a CEA-608 display buffer
  3989. var BOTTOM_ROW = 14; // This array is used for mapping PACs -> row #, since there's no way of
  3990. // getting it through bit logic.
  3991. var ROWS = [0x1100, 0x1120, 0x1200, 0x1220, 0x1500, 0x1520, 0x1600, 0x1620, 0x1700, 0x1720, 0x1000, 0x1300, 0x1320, 0x1400, 0x1420]; // CEA-608 captions are rendered onto a 34x15 matrix of character
  3992. // cells. The "bottom" row is the last element in the outer array.
  3993. var createDisplayBuffer = function createDisplayBuffer() {
  3994. var result = [],
  3995. i = BOTTOM_ROW + 1;
  3996. while (i--) {
  3997. result.push('');
  3998. }
  3999. return result;
  4000. };
  4001. var Cea608Stream = function Cea608Stream(field, dataChannel) {
  4002. Cea608Stream.prototype.init.call(this);
  4003. this.field_ = field || 0;
  4004. this.dataChannel_ = dataChannel || 0;
  4005. this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
  4006. this.setConstants();
  4007. this.reset();
  4008. this.push = function (packet) {
  4009. var data, swap, char0, char1, text; // remove the parity bits
  4010. data = packet.ccData & 0x7f7f; // ignore duplicate control codes; the spec demands they're sent twice
  4011. if (data === this.lastControlCode_) {
  4012. this.lastControlCode_ = null;
  4013. return;
  4014. } // Store control codes
  4015. if ((data & 0xf000) === 0x1000) {
  4016. this.lastControlCode_ = data;
  4017. } else if (data !== this.PADDING_) {
  4018. this.lastControlCode_ = null;
  4019. }
  4020. char0 = data >>> 8;
  4021. char1 = data & 0xff;
  4022. if (data === this.PADDING_) {
  4023. return;
  4024. } else if (data === this.RESUME_CAPTION_LOADING_) {
  4025. this.mode_ = 'popOn';
  4026. } else if (data === this.END_OF_CAPTION_) {
  4027. // If an EOC is received while in paint-on mode, the displayed caption
  4028. // text should be swapped to non-displayed memory as if it was a pop-on
  4029. // caption. Because of that, we should explicitly switch back to pop-on
  4030. // mode
  4031. this.mode_ = 'popOn';
  4032. this.clearFormatting(packet.pts); // if a caption was being displayed, it's gone now
  4033. this.flushDisplayed(packet.pts); // flip memory
  4034. swap = this.displayed_;
  4035. this.displayed_ = this.nonDisplayed_;
  4036. this.nonDisplayed_ = swap; // start measuring the time to display the caption
  4037. this.startPts_ = packet.pts;
  4038. } else if (data === this.ROLL_UP_2_ROWS_) {
  4039. this.rollUpRows_ = 2;
  4040. this.setRollUp(packet.pts);
  4041. } else if (data === this.ROLL_UP_3_ROWS_) {
  4042. this.rollUpRows_ = 3;
  4043. this.setRollUp(packet.pts);
  4044. } else if (data === this.ROLL_UP_4_ROWS_) {
  4045. this.rollUpRows_ = 4;
  4046. this.setRollUp(packet.pts);
  4047. } else if (data === this.CARRIAGE_RETURN_) {
  4048. this.clearFormatting(packet.pts);
  4049. this.flushDisplayed(packet.pts);
  4050. this.shiftRowsUp_();
  4051. this.startPts_ = packet.pts;
  4052. } else if (data === this.BACKSPACE_) {
  4053. if (this.mode_ === 'popOn') {
  4054. this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
  4055. } else {
  4056. this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
  4057. }
  4058. } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
  4059. this.flushDisplayed(packet.pts);
  4060. this.displayed_ = createDisplayBuffer();
  4061. } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
  4062. this.nonDisplayed_ = createDisplayBuffer();
  4063. } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
  4064. if (this.mode_ !== 'paintOn') {
  4065. // NOTE: This should be removed when proper caption positioning is
  4066. // implemented
  4067. this.flushDisplayed(packet.pts);
  4068. this.displayed_ = createDisplayBuffer();
  4069. }
  4070. this.mode_ = 'paintOn';
  4071. this.startPts_ = packet.pts; // Append special characters to caption text
  4072. } else if (this.isSpecialCharacter(char0, char1)) {
  4073. // Bitmask char0 so that we can apply character transformations
  4074. // regardless of field and data channel.
  4075. // Then byte-shift to the left and OR with char1 so we can pass the
  4076. // entire character code to `getCharFromCode`.
  4077. char0 = (char0 & 0x03) << 8;
  4078. text = getCharFromCode(char0 | char1);
  4079. this[this.mode_](packet.pts, text);
  4080. this.column_++; // Append extended characters to caption text
  4081. } else if (this.isExtCharacter(char0, char1)) {
  4082. // Extended characters always follow their "non-extended" equivalents.
  4083. // IE if a "è" is desired, you'll always receive "eè"; non-compliant
  4084. // decoders are supposed to drop the "è", while compliant decoders
  4085. // backspace the "e" and insert "è".
  4086. // Delete the previous character
  4087. if (this.mode_ === 'popOn') {
  4088. this.nonDisplayed_[this.row_] = this.nonDisplayed_[this.row_].slice(0, -1);
  4089. } else {
  4090. this.displayed_[this.row_] = this.displayed_[this.row_].slice(0, -1);
  4091. } // Bitmask char0 so that we can apply character transformations
  4092. // regardless of field and data channel.
  4093. // Then byte-shift to the left and OR with char1 so we can pass the
  4094. // entire character code to `getCharFromCode`.
  4095. char0 = (char0 & 0x03) << 8;
  4096. text = getCharFromCode(char0 | char1);
  4097. this[this.mode_](packet.pts, text);
  4098. this.column_++; // Process mid-row codes
  4099. } else if (this.isMidRowCode(char0, char1)) {
  4100. // Attributes are not additive, so clear all formatting
  4101. this.clearFormatting(packet.pts); // According to the standard, mid-row codes
  4102. // should be replaced with spaces, so add one now
  4103. this[this.mode_](packet.pts, ' ');
  4104. this.column_++;
  4105. if ((char1 & 0xe) === 0xe) {
  4106. this.addFormatting(packet.pts, ['i']);
  4107. }
  4108. if ((char1 & 0x1) === 0x1) {
  4109. this.addFormatting(packet.pts, ['u']);
  4110. } // Detect offset control codes and adjust cursor
  4111. } else if (this.isOffsetControlCode(char0, char1)) {
  4112. // Cursor position is set by indent PAC (see below) in 4-column
  4113. // increments, with an additional offset code of 1-3 to reach any
  4114. // of the 32 columns specified by CEA-608. So all we need to do
  4115. // here is increment the column cursor by the given offset.
  4116. this.column_ += char1 & 0x03; // Detect PACs (Preamble Address Codes)
  4117. } else if (this.isPAC(char0, char1)) {
  4118. // There's no logic for PAC -> row mapping, so we have to just
  4119. // find the row code in an array and use its index :(
  4120. var row = ROWS.indexOf(data & 0x1f20); // Configure the caption window if we're in roll-up mode
  4121. if (this.mode_ === 'rollUp') {
  4122. // This implies that the base row is incorrectly set.
  4123. // As per the recommendation in CEA-608(Base Row Implementation), defer to the number
  4124. // of roll-up rows set.
  4125. if (row - this.rollUpRows_ + 1 < 0) {
  4126. row = this.rollUpRows_ - 1;
  4127. }
  4128. this.setRollUp(packet.pts, row);
  4129. }
  4130. if (row !== this.row_) {
  4131. // formatting is only persistent for current row
  4132. this.clearFormatting(packet.pts);
  4133. this.row_ = row;
  4134. } // All PACs can apply underline, so detect and apply
  4135. // (All odd-numbered second bytes set underline)
  4136. if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
  4137. this.addFormatting(packet.pts, ['u']);
  4138. }
  4139. if ((data & 0x10) === 0x10) {
  4140. // We've got an indent level code. Each successive even number
  4141. // increments the column cursor by 4, so we can get the desired
  4142. // column position by bit-shifting to the right (to get n/2)
  4143. // and multiplying by 4.
  4144. this.column_ = ((data & 0xe) >> 1) * 4;
  4145. }
  4146. if (this.isColorPAC(char1)) {
  4147. // it's a color code, though we only support white, which
  4148. // can be either normal or italicized. white italics can be
  4149. // either 0x4e or 0x6e depending on the row, so we just
  4150. // bitwise-and with 0xe to see if italics should be turned on
  4151. if ((char1 & 0xe) === 0xe) {
  4152. this.addFormatting(packet.pts, ['i']);
  4153. }
  4154. } // We have a normal character in char0, and possibly one in char1
  4155. } else if (this.isNormalChar(char0)) {
  4156. if (char1 === 0x00) {
  4157. char1 = null;
  4158. }
  4159. text = getCharFromCode(char0);
  4160. text += getCharFromCode(char1);
  4161. this[this.mode_](packet.pts, text);
  4162. this.column_ += text.length;
  4163. } // finish data processing
  4164. };
  4165. };
  4166. Cea608Stream.prototype = new stream(); // Trigger a cue point that captures the current state of the
  4167. // display buffer
  4168. Cea608Stream.prototype.flushDisplayed = function (pts) {
  4169. var content = this.displayed_ // remove spaces from the start and end of the string
  4170. .map(function (row, index) {
  4171. try {
  4172. return row.trim();
  4173. } catch (e) {
  4174. // Ordinarily, this shouldn't happen. However, caption
  4175. // parsing errors should not throw exceptions and
  4176. // break playback.
  4177. this.trigger('log', {
  4178. level: 'warn',
  4179. message: 'Skipping a malformed 608 caption at index ' + index + '.'
  4180. });
  4181. return '';
  4182. }
  4183. }, this) // combine all text rows to display in one cue
  4184. .join('\n') // and remove blank rows from the start and end, but not the middle
  4185. .replace(/^\n+|\n+$/g, '');
  4186. if (content.length) {
  4187. this.trigger('data', {
  4188. startPts: this.startPts_,
  4189. endPts: pts,
  4190. text: content,
  4191. stream: this.name_
  4192. });
  4193. }
  4194. };
  4195. /**
  4196. * Zero out the data, used for startup and on seek
  4197. */
  4198. Cea608Stream.prototype.reset = function () {
  4199. this.mode_ = 'popOn'; // When in roll-up mode, the index of the last row that will
  4200. // actually display captions. If a caption is shifted to a row
  4201. // with a lower index than this, it is cleared from the display
  4202. // buffer
  4203. this.topRow_ = 0;
  4204. this.startPts_ = 0;
  4205. this.displayed_ = createDisplayBuffer();
  4206. this.nonDisplayed_ = createDisplayBuffer();
  4207. this.lastControlCode_ = null; // Track row and column for proper line-breaking and spacing
  4208. this.column_ = 0;
  4209. this.row_ = BOTTOM_ROW;
  4210. this.rollUpRows_ = 2; // This variable holds currently-applied formatting
  4211. this.formatting_ = [];
  4212. };
  4213. /**
  4214. * Sets up control code and related constants for this instance
  4215. */
  4216. Cea608Stream.prototype.setConstants = function () {
  4217. // The following attributes have these uses:
  4218. // ext_ : char0 for mid-row codes, and the base for extended
  4219. // chars (ext_+0, ext_+1, and ext_+2 are char0s for
  4220. // extended codes)
  4221. // control_: char0 for control codes, except byte-shifted to the
  4222. // left so that we can do this.control_ | CONTROL_CODE
  4223. // offset_: char0 for tab offset codes
  4224. //
  4225. // It's also worth noting that control codes, and _only_ control codes,
  4226. // differ between field 1 and field2. Field 2 control codes are always
  4227. // their field 1 value plus 1. That's why there's the "| field" on the
  4228. // control value.
  4229. if (this.dataChannel_ === 0) {
  4230. this.BASE_ = 0x10;
  4231. this.EXT_ = 0x11;
  4232. this.CONTROL_ = (0x14 | this.field_) << 8;
  4233. this.OFFSET_ = 0x17;
  4234. } else if (this.dataChannel_ === 1) {
  4235. this.BASE_ = 0x18;
  4236. this.EXT_ = 0x19;
  4237. this.CONTROL_ = (0x1c | this.field_) << 8;
  4238. this.OFFSET_ = 0x1f;
  4239. } // Constants for the LSByte command codes recognized by Cea608Stream. This
  4240. // list is not exhaustive. For a more comprehensive listing and semantics see
  4241. // http://www.gpo.gov/fdsys/pkg/CFR-2010-title47-vol1/pdf/CFR-2010-title47-vol1-sec15-119.pdf
  4242. // Padding
  4243. this.PADDING_ = 0x0000; // Pop-on Mode
  4244. this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
  4245. this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f; // Roll-up Mode
  4246. this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
  4247. this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
  4248. this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
  4249. this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d; // paint-on mode
  4250. this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29; // Erasure
  4251. this.BACKSPACE_ = this.CONTROL_ | 0x21;
  4252. this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
  4253. this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
  4254. };
  4255. /**
  4256. * Detects if the 2-byte packet data is a special character
  4257. *
  4258. * Special characters have a second byte in the range 0x30 to 0x3f,
  4259. * with the first byte being 0x11 (for data channel 1) or 0x19 (for
  4260. * data channel 2).
  4261. *
  4262. * @param {Integer} char0 The first byte
  4263. * @param {Integer} char1 The second byte
  4264. * @return {Boolean} Whether the 2 bytes are an special character
  4265. */
  4266. Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
  4267. return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
  4268. };
  4269. /**
  4270. * Detects if the 2-byte packet data is an extended character
  4271. *
  4272. * Extended characters have a second byte in the range 0x20 to 0x3f,
  4273. * with the first byte being 0x12 or 0x13 (for data channel 1) or
  4274. * 0x1a or 0x1b (for data channel 2).
  4275. *
  4276. * @param {Integer} char0 The first byte
  4277. * @param {Integer} char1 The second byte
  4278. * @return {Boolean} Whether the 2 bytes are an extended character
  4279. */
  4280. Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
  4281. return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
  4282. };
  4283. /**
  4284. * Detects if the 2-byte packet is a mid-row code
  4285. *
  4286. * Mid-row codes have a second byte in the range 0x20 to 0x2f, with
  4287. * the first byte being 0x11 (for data channel 1) or 0x19 (for data
  4288. * channel 2).
  4289. *
  4290. * @param {Integer} char0 The first byte
  4291. * @param {Integer} char1 The second byte
  4292. * @return {Boolean} Whether the 2 bytes are a mid-row code
  4293. */
  4294. Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
  4295. return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
  4296. };
  4297. /**
  4298. * Detects if the 2-byte packet is an offset control code
  4299. *
  4300. * Offset control codes have a second byte in the range 0x21 to 0x23,
  4301. * with the first byte being 0x17 (for data channel 1) or 0x1f (for
  4302. * data channel 2).
  4303. *
  4304. * @param {Integer} char0 The first byte
  4305. * @param {Integer} char1 The second byte
  4306. * @return {Boolean} Whether the 2 bytes are an offset control code
  4307. */
  4308. Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
  4309. return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
  4310. };
  4311. /**
  4312. * Detects if the 2-byte packet is a Preamble Address Code
  4313. *
  4314. * PACs have a first byte in the range 0x10 to 0x17 (for data channel 1)
  4315. * or 0x18 to 0x1f (for data channel 2), with the second byte in the
  4316. * range 0x40 to 0x7f.
  4317. *
  4318. * @param {Integer} char0 The first byte
  4319. * @param {Integer} char1 The second byte
  4320. * @return {Boolean} Whether the 2 bytes are a PAC
  4321. */
  4322. Cea608Stream.prototype.isPAC = function (char0, char1) {
  4323. return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
  4324. };
  4325. /**
  4326. * Detects if a packet's second byte is in the range of a PAC color code
  4327. *
  4328. * PAC color codes have the second byte be in the range 0x40 to 0x4f, or
  4329. * 0x60 to 0x6f.
  4330. *
  4331. * @param {Integer} char1 The second byte
  4332. * @return {Boolean} Whether the byte is a color PAC
  4333. */
  4334. Cea608Stream.prototype.isColorPAC = function (char1) {
  4335. return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
  4336. };
  4337. /**
  4338. * Detects if a single byte is in the range of a normal character
  4339. *
  4340. * Normal text bytes are in the range 0x20 to 0x7f.
  4341. *
  4342. * @param {Integer} char The byte
  4343. * @return {Boolean} Whether the byte is a normal character
  4344. */
  4345. Cea608Stream.prototype.isNormalChar = function (char) {
  4346. return char >= 0x20 && char <= 0x7f;
  4347. };
  4348. /**
  4349. * Configures roll-up
  4350. *
  4351. * @param {Integer} pts Current PTS
  4352. * @param {Integer} newBaseRow Used by PACs to slide the current window to
  4353. * a new position
  4354. */
  4355. Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
  4356. // Reset the base row to the bottom row when switching modes
  4357. if (this.mode_ !== 'rollUp') {
  4358. this.row_ = BOTTOM_ROW;
  4359. this.mode_ = 'rollUp'; // Spec says to wipe memories when switching to roll-up
  4360. this.flushDisplayed(pts);
  4361. this.nonDisplayed_ = createDisplayBuffer();
  4362. this.displayed_ = createDisplayBuffer();
  4363. }
  4364. if (newBaseRow !== undefined && newBaseRow !== this.row_) {
  4365. // move currently displayed captions (up or down) to the new base row
  4366. for (var i = 0; i < this.rollUpRows_; i++) {
  4367. this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
  4368. this.displayed_[this.row_ - i] = '';
  4369. }
  4370. }
  4371. if (newBaseRow === undefined) {
  4372. newBaseRow = this.row_;
  4373. }
  4374. this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
  4375. }; // Adds the opening HTML tag for the passed character to the caption text,
  4376. // and keeps track of it for later closing
  4377. Cea608Stream.prototype.addFormatting = function (pts, format) {
  4378. this.formatting_ = this.formatting_.concat(format);
  4379. var text = format.reduce(function (text, format) {
  4380. return text + '<' + format + '>';
  4381. }, '');
  4382. this[this.mode_](pts, text);
  4383. }; // Adds HTML closing tags for current formatting to caption text and
  4384. // clears remembered formatting
  4385. Cea608Stream.prototype.clearFormatting = function (pts) {
  4386. if (!this.formatting_.length) {
  4387. return;
  4388. }
  4389. var text = this.formatting_.reverse().reduce(function (text, format) {
  4390. return text + '</' + format + '>';
  4391. }, '');
  4392. this.formatting_ = [];
  4393. this[this.mode_](pts, text);
  4394. }; // Mode Implementations
  4395. Cea608Stream.prototype.popOn = function (pts, text) {
  4396. var baseRow = this.nonDisplayed_[this.row_]; // buffer characters
  4397. baseRow += text;
  4398. this.nonDisplayed_[this.row_] = baseRow;
  4399. };
  4400. Cea608Stream.prototype.rollUp = function (pts, text) {
  4401. var baseRow = this.displayed_[this.row_];
  4402. baseRow += text;
  4403. this.displayed_[this.row_] = baseRow;
  4404. };
  4405. Cea608Stream.prototype.shiftRowsUp_ = function () {
  4406. var i; // clear out inactive rows
  4407. for (i = 0; i < this.topRow_; i++) {
  4408. this.displayed_[i] = '';
  4409. }
  4410. for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
  4411. this.displayed_[i] = '';
  4412. } // shift displayed rows up
  4413. for (i = this.topRow_; i < this.row_; i++) {
  4414. this.displayed_[i] = this.displayed_[i + 1];
  4415. } // clear out the bottom row
  4416. this.displayed_[this.row_] = '';
  4417. };
  4418. Cea608Stream.prototype.paintOn = function (pts, text) {
  4419. var baseRow = this.displayed_[this.row_];
  4420. baseRow += text;
  4421. this.displayed_[this.row_] = baseRow;
  4422. }; // exports
  4423. var captionStream = {
  4424. CaptionStream: CaptionStream$1,
  4425. Cea608Stream: Cea608Stream,
  4426. Cea708Stream: Cea708Stream
  4427. };
  4428. /**
  4429. * mux.js
  4430. *
  4431. * Copyright (c) Brightcove
  4432. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4433. */
  4434. var streamTypes = {
  4435. H264_STREAM_TYPE: 0x1B,
  4436. ADTS_STREAM_TYPE: 0x0F,
  4437. METADATA_STREAM_TYPE: 0x15
  4438. };
  4439. var MAX_TS = 8589934592;
  4440. var RO_THRESH = 4294967296;
  4441. var TYPE_SHARED = 'shared';
  4442. var handleRollover$1 = function handleRollover(value, reference) {
  4443. var direction = 1;
  4444. if (value > reference) {
  4445. // If the current timestamp value is greater than our reference timestamp and we detect a
  4446. // timestamp rollover, this means the roll over is happening in the opposite direction.
  4447. // Example scenario: Enter a long stream/video just after a rollover occurred. The reference
  4448. // point will be set to a small number, e.g. 1. The user then seeks backwards over the
  4449. // rollover point. In loading this segment, the timestamp values will be very large,
  4450. // e.g. 2^33 - 1. Since this comes before the data we loaded previously, we want to adjust
  4451. // the time stamp to be `value - 2^33`.
  4452. direction = -1;
  4453. } // Note: A seek forwards or back that is greater than the RO_THRESH (2^32, ~13 hours) will
  4454. // cause an incorrect adjustment.
  4455. while (Math.abs(reference - value) > RO_THRESH) {
  4456. value += direction * MAX_TS;
  4457. }
  4458. return value;
  4459. };
  4460. var TimestampRolloverStream$1 = function TimestampRolloverStream(type) {
  4461. var lastDTS, referenceDTS;
  4462. TimestampRolloverStream.prototype.init.call(this); // The "shared" type is used in cases where a stream will contain muxed
  4463. // video and audio. We could use `undefined` here, but having a string
  4464. // makes debugging a little clearer.
  4465. this.type_ = type || TYPE_SHARED;
  4466. this.push = function (data) {
  4467. // Any "shared" rollover streams will accept _all_ data. Otherwise,
  4468. // streams will only accept data that matches their type.
  4469. if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
  4470. return;
  4471. }
  4472. if (referenceDTS === undefined) {
  4473. referenceDTS = data.dts;
  4474. }
  4475. data.dts = handleRollover$1(data.dts, referenceDTS);
  4476. data.pts = handleRollover$1(data.pts, referenceDTS);
  4477. lastDTS = data.dts;
  4478. this.trigger('data', data);
  4479. };
  4480. this.flush = function () {
  4481. referenceDTS = lastDTS;
  4482. this.trigger('done');
  4483. };
  4484. this.endTimeline = function () {
  4485. this.flush();
  4486. this.trigger('endedtimeline');
  4487. };
  4488. this.discontinuity = function () {
  4489. referenceDTS = void 0;
  4490. lastDTS = void 0;
  4491. };
  4492. this.reset = function () {
  4493. this.discontinuity();
  4494. this.trigger('reset');
  4495. };
  4496. };
  4497. TimestampRolloverStream$1.prototype = new stream();
  4498. var timestampRolloverStream = {
  4499. TimestampRolloverStream: TimestampRolloverStream$1,
  4500. handleRollover: handleRollover$1
  4501. };
  4502. var _MetadataStream;
  4503. _MetadataStream = function MetadataStream(options) {
  4504. var settings = {
  4505. // the bytes of the program-level descriptor field in MP2T
  4506. // see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
  4507. // program element descriptors"
  4508. descriptor: options && options.descriptor
  4509. },
  4510. // the total size in bytes of the ID3 tag being parsed
  4511. tagSize = 0,
  4512. // tag data that is not complete enough to be parsed
  4513. buffer = [],
  4514. // the total number of bytes currently in the buffer
  4515. bufferSize = 0,
  4516. i;
  4517. _MetadataStream.prototype.init.call(this); // calculate the text track in-band metadata track dispatch type
  4518. // https://html.spec.whatwg.org/multipage/embedded-content.html#steps-to-expose-a-media-resource-specific-text-track
  4519. this.dispatchType = streamTypes.METADATA_STREAM_TYPE.toString(16);
  4520. if (settings.descriptor) {
  4521. for (i = 0; i < settings.descriptor.length; i++) {
  4522. this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
  4523. }
  4524. }
  4525. this.push = function (chunk) {
  4526. var tag, frameStart, frameSize, frame, i, frameHeader;
  4527. if (chunk.type !== 'timed-metadata') {
  4528. return;
  4529. } // if data_alignment_indicator is set in the PES header,
  4530. // we must have the start of a new ID3 tag. Assume anything
  4531. // remaining in the buffer was malformed and throw it out
  4532. if (chunk.dataAlignmentIndicator) {
  4533. bufferSize = 0;
  4534. buffer.length = 0;
  4535. } // ignore events that don't look like ID3 data
  4536. if (buffer.length === 0 && (chunk.data.length < 10 || chunk.data[0] !== 'I'.charCodeAt(0) || chunk.data[1] !== 'D'.charCodeAt(0) || chunk.data[2] !== '3'.charCodeAt(0))) {
  4537. this.trigger('log', {
  4538. level: 'warn',
  4539. message: 'Skipping unrecognized metadata packet'
  4540. });
  4541. return;
  4542. } // add this chunk to the data we've collected so far
  4543. buffer.push(chunk);
  4544. bufferSize += chunk.data.byteLength; // grab the size of the entire frame from the ID3 header
  4545. if (buffer.length === 1) {
  4546. // the frame size is transmitted as a 28-bit integer in the
  4547. // last four bytes of the ID3 header.
  4548. // The most significant bit of each byte is dropped and the
  4549. // results concatenated to recover the actual value.
  4550. tagSize = parseId3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  4551. // convenient for our comparisons to include it
  4552. tagSize += 10;
  4553. } // if the entire frame has not arrived, wait for more data
  4554. if (bufferSize < tagSize) {
  4555. return;
  4556. } // collect the entire frame so it can be parsed
  4557. tag = {
  4558. data: new Uint8Array(tagSize),
  4559. frames: [],
  4560. pts: buffer[0].pts,
  4561. dts: buffer[0].dts
  4562. };
  4563. for (i = 0; i < tagSize;) {
  4564. tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
  4565. i += buffer[0].data.byteLength;
  4566. bufferSize -= buffer[0].data.byteLength;
  4567. buffer.shift();
  4568. } // find the start of the first frame and the end of the tag
  4569. frameStart = 10;
  4570. if (tag.data[5] & 0x40) {
  4571. // advance the frame start past the extended header
  4572. frameStart += 4; // header size field
  4573. frameStart += parseId3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end
  4574. tagSize -= parseId3.parseSyncSafeInteger(tag.data.subarray(16, 20));
  4575. } // parse one or more ID3 frames
  4576. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  4577. do {
  4578. // determine the number of bytes in this frame
  4579. frameSize = parseId3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
  4580. if (frameSize < 1) {
  4581. this.trigger('log', {
  4582. level: 'warn',
  4583. message: 'Malformed ID3 frame encountered. Skipping remaining metadata parsing.'
  4584. }); // If the frame is malformed, don't parse any further frames but allow previous valid parsed frames
  4585. // to be sent along.
  4586. break;
  4587. }
  4588. frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
  4589. frame = {
  4590. id: frameHeader,
  4591. data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
  4592. };
  4593. frame.key = frame.id; // parse frame values
  4594. if (parseId3.frameParsers[frame.id]) {
  4595. // use frame specific parser
  4596. parseId3.frameParsers[frame.id](frame);
  4597. } else if (frame.id[0] === 'T') {
  4598. // use text frame generic parser
  4599. parseId3.frameParsers['T*'](frame);
  4600. } else if (frame.id[0] === 'W') {
  4601. // use URL link frame generic parser
  4602. parseId3.frameParsers['W*'](frame);
  4603. } // handle the special PRIV frame used to indicate the start
  4604. // time for raw AAC data
  4605. if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
  4606. var d = frame.data,
  4607. size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  4608. size *= 4;
  4609. size += d[7] & 0x03;
  4610. frame.timeStamp = size; // in raw AAC, all subsequent data will be timestamped based
  4611. // on the value of this frame
  4612. // we couldn't have known the appropriate pts and dts before
  4613. // parsing this ID3 tag so set those values now
  4614. if (tag.pts === undefined && tag.dts === undefined) {
  4615. tag.pts = frame.timeStamp;
  4616. tag.dts = frame.timeStamp;
  4617. }
  4618. this.trigger('timestamp', frame);
  4619. }
  4620. tag.frames.push(frame);
  4621. frameStart += 10; // advance past the frame header
  4622. frameStart += frameSize; // advance past the frame body
  4623. } while (frameStart < tagSize);
  4624. this.trigger('data', tag);
  4625. };
  4626. };
  4627. _MetadataStream.prototype = new stream();
  4628. var metadataStream = _MetadataStream;
  4629. var TimestampRolloverStream = timestampRolloverStream.TimestampRolloverStream; // object types
  4630. var _TransportPacketStream, _TransportParseStream, _ElementaryStream; // constants
  4631. var MP2T_PACKET_LENGTH$1 = 188,
  4632. // bytes
  4633. SYNC_BYTE$1 = 0x47;
  4634. /**
  4635. * Splits an incoming stream of binary data into MPEG-2 Transport
  4636. * Stream packets.
  4637. */
  4638. _TransportPacketStream = function TransportPacketStream() {
  4639. var buffer = new Uint8Array(MP2T_PACKET_LENGTH$1),
  4640. bytesInBuffer = 0;
  4641. _TransportPacketStream.prototype.init.call(this); // Deliver new bytes to the stream.
  4642. /**
  4643. * Split a stream of data into M2TS packets
  4644. **/
  4645. this.push = function (bytes) {
  4646. var startIndex = 0,
  4647. endIndex = MP2T_PACKET_LENGTH$1,
  4648. everything; // If there are bytes remaining from the last segment, prepend them to the
  4649. // bytes that were pushed in
  4650. if (bytesInBuffer) {
  4651. everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
  4652. everything.set(buffer.subarray(0, bytesInBuffer));
  4653. everything.set(bytes, bytesInBuffer);
  4654. bytesInBuffer = 0;
  4655. } else {
  4656. everything = bytes;
  4657. } // While we have enough data for a packet
  4658. while (endIndex < everything.byteLength) {
  4659. // Look for a pair of start and end sync bytes in the data..
  4660. if (everything[startIndex] === SYNC_BYTE$1 && everything[endIndex] === SYNC_BYTE$1) {
  4661. // We found a packet so emit it and jump one whole packet forward in
  4662. // the stream
  4663. this.trigger('data', everything.subarray(startIndex, endIndex));
  4664. startIndex += MP2T_PACKET_LENGTH$1;
  4665. endIndex += MP2T_PACKET_LENGTH$1;
  4666. continue;
  4667. } // If we get here, we have somehow become de-synchronized and we need to step
  4668. // forward one byte at a time until we find a pair of sync bytes that denote
  4669. // a packet
  4670. startIndex++;
  4671. endIndex++;
  4672. } // If there was some data left over at the end of the segment that couldn't
  4673. // possibly be a whole packet, keep it because it might be the start of a packet
  4674. // that continues in the next segment
  4675. if (startIndex < everything.byteLength) {
  4676. buffer.set(everything.subarray(startIndex), 0);
  4677. bytesInBuffer = everything.byteLength - startIndex;
  4678. }
  4679. };
  4680. /**
  4681. * Passes identified M2TS packets to the TransportParseStream to be parsed
  4682. **/
  4683. this.flush = function () {
  4684. // If the buffer contains a whole packet when we are being flushed, emit it
  4685. // and empty the buffer. Otherwise hold onto the data because it may be
  4686. // important for decoding the next segment
  4687. if (bytesInBuffer === MP2T_PACKET_LENGTH$1 && buffer[0] === SYNC_BYTE$1) {
  4688. this.trigger('data', buffer);
  4689. bytesInBuffer = 0;
  4690. }
  4691. this.trigger('done');
  4692. };
  4693. this.endTimeline = function () {
  4694. this.flush();
  4695. this.trigger('endedtimeline');
  4696. };
  4697. this.reset = function () {
  4698. bytesInBuffer = 0;
  4699. this.trigger('reset');
  4700. };
  4701. };
  4702. _TransportPacketStream.prototype = new stream();
  4703. /**
  4704. * Accepts an MP2T TransportPacketStream and emits data events with parsed
  4705. * forms of the individual transport stream packets.
  4706. */
  4707. _TransportParseStream = function TransportParseStream() {
  4708. var parsePsi, parsePat, parsePmt, self;
  4709. _TransportParseStream.prototype.init.call(this);
  4710. self = this;
  4711. this.packetsWaitingForPmt = [];
  4712. this.programMapTable = undefined;
  4713. parsePsi = function parsePsi(payload, psi) {
  4714. var offset = 0; // PSI packets may be split into multiple sections and those
  4715. // sections may be split into multiple packets. If a PSI
  4716. // section starts in this packet, the payload_unit_start_indicator
  4717. // will be true and the first byte of the payload will indicate
  4718. // the offset from the current position to the start of the
  4719. // section.
  4720. if (psi.payloadUnitStartIndicator) {
  4721. offset += payload[offset] + 1;
  4722. }
  4723. if (psi.type === 'pat') {
  4724. parsePat(payload.subarray(offset), psi);
  4725. } else {
  4726. parsePmt(payload.subarray(offset), psi);
  4727. }
  4728. };
  4729. parsePat = function parsePat(payload, pat) {
  4730. pat.section_number = payload[7]; // eslint-disable-line camelcase
  4731. pat.last_section_number = payload[8]; // eslint-disable-line camelcase
  4732. // skip the PSI header and parse the first PMT entry
  4733. self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
  4734. pat.pmtPid = self.pmtPid;
  4735. };
  4736. /**
  4737. * Parse out the relevant fields of a Program Map Table (PMT).
  4738. * @param payload {Uint8Array} the PMT-specific portion of an MP2T
  4739. * packet. The first byte in this array should be the table_id
  4740. * field.
  4741. * @param pmt {object} the object that should be decorated with
  4742. * fields parsed from the PMT.
  4743. */
  4744. parsePmt = function parsePmt(payload, pmt) {
  4745. var sectionLength, tableEnd, programInfoLength, offset; // PMTs can be sent ahead of the time when they should actually
  4746. // take effect. We don't believe this should ever be the case
  4747. // for HLS but we'll ignore "forward" PMT declarations if we see
  4748. // them. Future PMT declarations have the current_next_indicator
  4749. // set to zero.
  4750. if (!(payload[5] & 0x01)) {
  4751. return;
  4752. } // overwrite any existing program map table
  4753. self.programMapTable = {
  4754. video: null,
  4755. audio: null,
  4756. 'timed-metadata': {}
  4757. }; // the mapping table ends at the end of the current section
  4758. sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
  4759. tableEnd = 3 + sectionLength - 4; // to determine where the table is, we have to figure out how
  4760. // long the program info descriptors are
  4761. programInfoLength = (payload[10] & 0x0f) << 8 | payload[11]; // advance the offset to the first entry in the mapping table
  4762. offset = 12 + programInfoLength;
  4763. while (offset < tableEnd) {
  4764. var streamType = payload[offset];
  4765. var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2]; // only map a single elementary_pid for audio and video stream types
  4766. // TODO: should this be done for metadata too? for now maintain behavior of
  4767. // multiple metadata streams
  4768. if (streamType === streamTypes.H264_STREAM_TYPE && self.programMapTable.video === null) {
  4769. self.programMapTable.video = pid;
  4770. } else if (streamType === streamTypes.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
  4771. self.programMapTable.audio = pid;
  4772. } else if (streamType === streamTypes.METADATA_STREAM_TYPE) {
  4773. // map pid to stream type for metadata streams
  4774. self.programMapTable['timed-metadata'][pid] = streamType;
  4775. } // move to the next table entry
  4776. // skip past the elementary stream descriptors, if present
  4777. offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
  4778. } // record the map on the packet as well
  4779. pmt.programMapTable = self.programMapTable;
  4780. };
  4781. /**
  4782. * Deliver a new MP2T packet to the next stream in the pipeline.
  4783. */
  4784. this.push = function (packet) {
  4785. var result = {},
  4786. offset = 4;
  4787. result.payloadUnitStartIndicator = !!(packet[1] & 0x40); // pid is a 13-bit field starting at the last bit of packet[1]
  4788. result.pid = packet[1] & 0x1f;
  4789. result.pid <<= 8;
  4790. result.pid |= packet[2]; // if an adaption field is present, its length is specified by the
  4791. // fifth byte of the TS packet header. The adaptation field is
  4792. // used to add stuffing to PES packets that don't fill a complete
  4793. // TS packet, and to specify some forms of timing and control data
  4794. // that we do not currently use.
  4795. if ((packet[3] & 0x30) >>> 4 > 0x01) {
  4796. offset += packet[offset] + 1;
  4797. } // parse the rest of the packet based on the type
  4798. if (result.pid === 0) {
  4799. result.type = 'pat';
  4800. parsePsi(packet.subarray(offset), result);
  4801. this.trigger('data', result);
  4802. } else if (result.pid === this.pmtPid) {
  4803. result.type = 'pmt';
  4804. parsePsi(packet.subarray(offset), result);
  4805. this.trigger('data', result); // if there are any packets waiting for a PMT to be found, process them now
  4806. while (this.packetsWaitingForPmt.length) {
  4807. this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
  4808. }
  4809. } else if (this.programMapTable === undefined) {
  4810. // When we have not seen a PMT yet, defer further processing of
  4811. // PES packets until one has been parsed
  4812. this.packetsWaitingForPmt.push([packet, offset, result]);
  4813. } else {
  4814. this.processPes_(packet, offset, result);
  4815. }
  4816. };
  4817. this.processPes_ = function (packet, offset, result) {
  4818. // set the appropriate stream type
  4819. if (result.pid === this.programMapTable.video) {
  4820. result.streamType = streamTypes.H264_STREAM_TYPE;
  4821. } else if (result.pid === this.programMapTable.audio) {
  4822. result.streamType = streamTypes.ADTS_STREAM_TYPE;
  4823. } else {
  4824. // if not video or audio, it is timed-metadata or unknown
  4825. // if unknown, streamType will be undefined
  4826. result.streamType = this.programMapTable['timed-metadata'][result.pid];
  4827. }
  4828. result.type = 'pes';
  4829. result.data = packet.subarray(offset);
  4830. this.trigger('data', result);
  4831. };
  4832. };
  4833. _TransportParseStream.prototype = new stream();
  4834. _TransportParseStream.STREAM_TYPES = {
  4835. h264: 0x1b,
  4836. adts: 0x0f
  4837. };
  4838. /**
  4839. * Reconsistutes program elementary stream (PES) packets from parsed
  4840. * transport stream packets. That is, if you pipe an
  4841. * mp2t.TransportParseStream into a mp2t.ElementaryStream, the output
  4842. * events will be events which capture the bytes for individual PES
  4843. * packets plus relevant metadata that has been extracted from the
  4844. * container.
  4845. */
  4846. _ElementaryStream = function ElementaryStream() {
  4847. var self = this,
  4848. segmentHadPmt = false,
  4849. // PES packet fragments
  4850. video = {
  4851. data: [],
  4852. size: 0
  4853. },
  4854. audio = {
  4855. data: [],
  4856. size: 0
  4857. },
  4858. timedMetadata = {
  4859. data: [],
  4860. size: 0
  4861. },
  4862. programMapTable,
  4863. parsePes = function parsePes(payload, pes) {
  4864. var ptsDtsFlags;
  4865. var startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]; // default to an empty array
  4866. pes.data = new Uint8Array(); // In certain live streams, the start of a TS fragment has ts packets
  4867. // that are frame data that is continuing from the previous fragment. This
  4868. // is to check that the pes data is the start of a new pes payload
  4869. if (startPrefix !== 1) {
  4870. return;
  4871. } // get the packet length, this will be 0 for video
  4872. pes.packetLength = 6 + (payload[4] << 8 | payload[5]); // find out if this packets starts a new keyframe
  4873. pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0; // PES packets may be annotated with a PTS value, or a PTS value
  4874. // and a DTS value. Determine what combination of values is
  4875. // available to work with.
  4876. ptsDtsFlags = payload[7]; // PTS and DTS are normally stored as a 33-bit number. Javascript
  4877. // performs all bitwise operations on 32-bit integers but javascript
  4878. // supports a much greater range (52-bits) of integer using standard
  4879. // mathematical operations.
  4880. // We construct a 31-bit value using bitwise operators over the 31
  4881. // most significant bits and then multiply by 4 (equal to a left-shift
  4882. // of 2) before we add the final 2 least significant bits of the
  4883. // timestamp (equal to an OR.)
  4884. if (ptsDtsFlags & 0xC0) {
  4885. // the PTS and DTS are not written out directly. For information
  4886. // on how they are encoded, see
  4887. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  4888. pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
  4889. pes.pts *= 4; // Left shift by 2
  4890. pes.pts += (payload[13] & 0x06) >>> 1; // OR by the two LSBs
  4891. pes.dts = pes.pts;
  4892. if (ptsDtsFlags & 0x40) {
  4893. pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
  4894. pes.dts *= 4; // Left shift by 2
  4895. pes.dts += (payload[18] & 0x06) >>> 1; // OR by the two LSBs
  4896. }
  4897. } // the data section starts immediately after the PES header.
  4898. // pes_header_data_length specifies the number of header bytes
  4899. // that follow the last byte of the field.
  4900. pes.data = payload.subarray(9 + payload[8]);
  4901. },
  4902. /**
  4903. * Pass completely parsed PES packets to the next stream in the pipeline
  4904. **/
  4905. flushStream = function flushStream(stream, type, forceFlush) {
  4906. var packetData = new Uint8Array(stream.size),
  4907. event = {
  4908. type: type
  4909. },
  4910. i = 0,
  4911. offset = 0,
  4912. packetFlushable = false,
  4913. fragment; // do nothing if there is not enough buffered data for a complete
  4914. // PES header
  4915. if (!stream.data.length || stream.size < 9) {
  4916. return;
  4917. }
  4918. event.trackId = stream.data[0].pid; // reassemble the packet
  4919. for (i = 0; i < stream.data.length; i++) {
  4920. fragment = stream.data[i];
  4921. packetData.set(fragment.data, offset);
  4922. offset += fragment.data.byteLength;
  4923. } // parse assembled packet's PES header
  4924. parsePes(packetData, event); // non-video PES packets MUST have a non-zero PES_packet_length
  4925. // check that there is enough stream data to fill the packet
  4926. packetFlushable = type === 'video' || event.packetLength <= stream.size; // flush pending packets if the conditions are right
  4927. if (forceFlush || packetFlushable) {
  4928. stream.size = 0;
  4929. stream.data.length = 0;
  4930. } // only emit packets that are complete. this is to avoid assembling
  4931. // incomplete PES packets due to poor segmentation
  4932. if (packetFlushable) {
  4933. self.trigger('data', event);
  4934. }
  4935. };
  4936. _ElementaryStream.prototype.init.call(this);
  4937. /**
  4938. * Identifies M2TS packet types and parses PES packets using metadata
  4939. * parsed from the PMT
  4940. **/
  4941. this.push = function (data) {
  4942. ({
  4943. pat: function pat() {// we have to wait for the PMT to arrive as well before we
  4944. // have any meaningful metadata
  4945. },
  4946. pes: function pes() {
  4947. var stream, streamType;
  4948. switch (data.streamType) {
  4949. case streamTypes.H264_STREAM_TYPE:
  4950. stream = video;
  4951. streamType = 'video';
  4952. break;
  4953. case streamTypes.ADTS_STREAM_TYPE:
  4954. stream = audio;
  4955. streamType = 'audio';
  4956. break;
  4957. case streamTypes.METADATA_STREAM_TYPE:
  4958. stream = timedMetadata;
  4959. streamType = 'timed-metadata';
  4960. break;
  4961. default:
  4962. // ignore unknown stream types
  4963. return;
  4964. } // if a new packet is starting, we can flush the completed
  4965. // packet
  4966. if (data.payloadUnitStartIndicator) {
  4967. flushStream(stream, streamType, true);
  4968. } // buffer this fragment until we are sure we've received the
  4969. // complete payload
  4970. stream.data.push(data);
  4971. stream.size += data.data.byteLength;
  4972. },
  4973. pmt: function pmt() {
  4974. var event = {
  4975. type: 'metadata',
  4976. tracks: []
  4977. };
  4978. programMapTable = data.programMapTable; // translate audio and video streams to tracks
  4979. if (programMapTable.video !== null) {
  4980. event.tracks.push({
  4981. timelineStartInfo: {
  4982. baseMediaDecodeTime: 0
  4983. },
  4984. id: +programMapTable.video,
  4985. codec: 'avc',
  4986. type: 'video'
  4987. });
  4988. }
  4989. if (programMapTable.audio !== null) {
  4990. event.tracks.push({
  4991. timelineStartInfo: {
  4992. baseMediaDecodeTime: 0
  4993. },
  4994. id: +programMapTable.audio,
  4995. codec: 'adts',
  4996. type: 'audio'
  4997. });
  4998. }
  4999. segmentHadPmt = true;
  5000. self.trigger('data', event);
  5001. }
  5002. })[data.type]();
  5003. };
  5004. this.reset = function () {
  5005. video.size = 0;
  5006. video.data.length = 0;
  5007. audio.size = 0;
  5008. audio.data.length = 0;
  5009. this.trigger('reset');
  5010. };
  5011. /**
  5012. * Flush any remaining input. Video PES packets may be of variable
  5013. * length. Normally, the start of a new video packet can trigger the
  5014. * finalization of the previous packet. That is not possible if no
  5015. * more video is forthcoming, however. In that case, some other
  5016. * mechanism (like the end of the file) has to be employed. When it is
  5017. * clear that no additional data is forthcoming, calling this method
  5018. * will flush the buffered packets.
  5019. */
  5020. this.flushStreams_ = function () {
  5021. // !!THIS ORDER IS IMPORTANT!!
  5022. // video first then audio
  5023. flushStream(video, 'video');
  5024. flushStream(audio, 'audio');
  5025. flushStream(timedMetadata, 'timed-metadata');
  5026. };
  5027. this.flush = function () {
  5028. // if on flush we haven't had a pmt emitted
  5029. // and we have a pmt to emit. emit the pmt
  5030. // so that we trigger a trackinfo downstream.
  5031. if (!segmentHadPmt && programMapTable) {
  5032. var pmt = {
  5033. type: 'metadata',
  5034. tracks: []
  5035. }; // translate audio and video streams to tracks
  5036. if (programMapTable.video !== null) {
  5037. pmt.tracks.push({
  5038. timelineStartInfo: {
  5039. baseMediaDecodeTime: 0
  5040. },
  5041. id: +programMapTable.video,
  5042. codec: 'avc',
  5043. type: 'video'
  5044. });
  5045. }
  5046. if (programMapTable.audio !== null) {
  5047. pmt.tracks.push({
  5048. timelineStartInfo: {
  5049. baseMediaDecodeTime: 0
  5050. },
  5051. id: +programMapTable.audio,
  5052. codec: 'adts',
  5053. type: 'audio'
  5054. });
  5055. }
  5056. self.trigger('data', pmt);
  5057. }
  5058. segmentHadPmt = false;
  5059. this.flushStreams_();
  5060. this.trigger('done');
  5061. };
  5062. };
  5063. _ElementaryStream.prototype = new stream();
  5064. var m2ts$1 = {
  5065. PAT_PID: 0x0000,
  5066. MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH$1,
  5067. TransportPacketStream: _TransportPacketStream,
  5068. TransportParseStream: _TransportParseStream,
  5069. ElementaryStream: _ElementaryStream,
  5070. TimestampRolloverStream: TimestampRolloverStream,
  5071. CaptionStream: captionStream.CaptionStream,
  5072. Cea608Stream: captionStream.Cea608Stream,
  5073. Cea708Stream: captionStream.Cea708Stream,
  5074. MetadataStream: metadataStream
  5075. };
  5076. for (var type in streamTypes) {
  5077. if (streamTypes.hasOwnProperty(type)) {
  5078. m2ts$1[type] = streamTypes[type];
  5079. }
  5080. }
  5081. var m2ts_1 = m2ts$1;
  5082. /**
  5083. * mux.js
  5084. *
  5085. * Copyright (c) Brightcove
  5086. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  5087. *
  5088. * Utilities to detect basic properties and metadata about Aac data.
  5089. */
  5090. var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  5091. var parseId3TagSize = function parseId3TagSize(header, byteIndex) {
  5092. var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
  5093. flags = header[byteIndex + 5],
  5094. footerPresent = (flags & 16) >> 4; // if we get a negative returnSize clamp it to 0
  5095. returnSize = returnSize >= 0 ? returnSize : 0;
  5096. if (footerPresent) {
  5097. return returnSize + 20;
  5098. }
  5099. return returnSize + 10;
  5100. };
  5101. var getId3Offset = function getId3Offset(data, offset) {
  5102. if (data.length - offset < 10 || data[offset] !== 'I'.charCodeAt(0) || data[offset + 1] !== 'D'.charCodeAt(0) || data[offset + 2] !== '3'.charCodeAt(0)) {
  5103. return offset;
  5104. }
  5105. offset += parseId3TagSize(data, offset);
  5106. return getId3Offset(data, offset);
  5107. }; // TODO: use vhs-utils
  5108. var isLikelyAacData$2 = function isLikelyAacData(data) {
  5109. var offset = getId3Offset(data, 0);
  5110. return data.length >= offset + 2 && (data[offset] & 0xFF) === 0xFF && (data[offset + 1] & 0xF0) === 0xF0 && // verify that the 2 layer bits are 0, aka this
  5111. // is not mp3 data but aac data.
  5112. (data[offset + 1] & 0x16) === 0x10;
  5113. };
  5114. var parseSyncSafeInteger = function parseSyncSafeInteger(data) {
  5115. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  5116. }; // return a percent-encoded representation of the specified byte range
  5117. // @see http://en.wikipedia.org/wiki/Percent-encoding
  5118. var percentEncode = function percentEncode(bytes, start, end) {
  5119. var i,
  5120. result = '';
  5121. for (i = start; i < end; i++) {
  5122. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  5123. }
  5124. return result;
  5125. }; // return the string representation of the specified byte range,
  5126. // interpreted as ISO-8859-1.
  5127. var parseIso88591 = function parseIso88591(bytes, start, end) {
  5128. return unescape(percentEncode(bytes, start, end)); // jshint ignore:line
  5129. };
  5130. var parseAdtsSize = function parseAdtsSize(header, byteIndex) {
  5131. var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
  5132. middle = header[byteIndex + 4] << 3,
  5133. highTwo = header[byteIndex + 3] & 0x3 << 11;
  5134. return highTwo | middle | lowThree;
  5135. };
  5136. var parseType$1 = function parseType(header, byteIndex) {
  5137. if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
  5138. return 'timed-metadata';
  5139. } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
  5140. return 'audio';
  5141. }
  5142. return null;
  5143. };
  5144. var parseSampleRate = function parseSampleRate(packet) {
  5145. var i = 0;
  5146. while (i + 5 < packet.length) {
  5147. if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
  5148. // If a valid header was not found, jump one forward and attempt to
  5149. // find a valid ADTS header starting at the next byte
  5150. i++;
  5151. continue;
  5152. }
  5153. return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
  5154. }
  5155. return null;
  5156. };
  5157. var parseAacTimestamp = function parseAacTimestamp(packet) {
  5158. var frameStart, frameSize, frame, frameHeader; // find the start of the first frame and the end of the tag
  5159. frameStart = 10;
  5160. if (packet[5] & 0x40) {
  5161. // advance the frame start past the extended header
  5162. frameStart += 4; // header size field
  5163. frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
  5164. } // parse one or more ID3 frames
  5165. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  5166. do {
  5167. // determine the number of bytes in this frame
  5168. frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
  5169. if (frameSize < 1) {
  5170. return null;
  5171. }
  5172. frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
  5173. if (frameHeader === 'PRIV') {
  5174. frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
  5175. for (var i = 0; i < frame.byteLength; i++) {
  5176. if (frame[i] === 0) {
  5177. var owner = parseIso88591(frame, 0, i);
  5178. if (owner === 'com.apple.streaming.transportStreamTimestamp') {
  5179. var d = frame.subarray(i + 1);
  5180. var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  5181. size *= 4;
  5182. size += d[7] & 0x03;
  5183. return size;
  5184. }
  5185. break;
  5186. }
  5187. }
  5188. }
  5189. frameStart += 10; // advance past the frame header
  5190. frameStart += frameSize; // advance past the frame body
  5191. } while (frameStart < packet.byteLength);
  5192. return null;
  5193. };
  5194. var utils = {
  5195. isLikelyAacData: isLikelyAacData$2,
  5196. parseId3TagSize: parseId3TagSize,
  5197. parseAdtsSize: parseAdtsSize,
  5198. parseType: parseType$1,
  5199. parseSampleRate: parseSampleRate,
  5200. parseAacTimestamp: parseAacTimestamp
  5201. };
  5202. var _AacStream;
  5203. /**
  5204. * Splits an incoming stream of binary data into ADTS and ID3 Frames.
  5205. */
  5206. _AacStream = function AacStream() {
  5207. var everything = new Uint8Array(),
  5208. timeStamp = 0;
  5209. _AacStream.prototype.init.call(this);
  5210. this.setTimestamp = function (timestamp) {
  5211. timeStamp = timestamp;
  5212. };
  5213. this.push = function (bytes) {
  5214. var frameSize = 0,
  5215. byteIndex = 0,
  5216. bytesLeft,
  5217. chunk,
  5218. packet,
  5219. tempLength; // If there are bytes remaining from the last segment, prepend them to the
  5220. // bytes that were pushed in
  5221. if (everything.length) {
  5222. tempLength = everything.length;
  5223. everything = new Uint8Array(bytes.byteLength + tempLength);
  5224. everything.set(everything.subarray(0, tempLength));
  5225. everything.set(bytes, tempLength);
  5226. } else {
  5227. everything = bytes;
  5228. }
  5229. while (everything.length - byteIndex >= 3) {
  5230. if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
  5231. // Exit early because we don't have enough to parse
  5232. // the ID3 tag header
  5233. if (everything.length - byteIndex < 10) {
  5234. break;
  5235. } // check framesize
  5236. frameSize = utils.parseId3TagSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  5237. // to emit a full packet
  5238. // Add to byteIndex to support multiple ID3 tags in sequence
  5239. if (byteIndex + frameSize > everything.length) {
  5240. break;
  5241. }
  5242. chunk = {
  5243. type: 'timed-metadata',
  5244. data: everything.subarray(byteIndex, byteIndex + frameSize)
  5245. };
  5246. this.trigger('data', chunk);
  5247. byteIndex += frameSize;
  5248. continue;
  5249. } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
  5250. // Exit early because we don't have enough to parse
  5251. // the ADTS frame header
  5252. if (everything.length - byteIndex < 7) {
  5253. break;
  5254. }
  5255. frameSize = utils.parseAdtsSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  5256. // to emit a full packet
  5257. if (byteIndex + frameSize > everything.length) {
  5258. break;
  5259. }
  5260. packet = {
  5261. type: 'audio',
  5262. data: everything.subarray(byteIndex, byteIndex + frameSize),
  5263. pts: timeStamp,
  5264. dts: timeStamp
  5265. };
  5266. this.trigger('data', packet);
  5267. byteIndex += frameSize;
  5268. continue;
  5269. }
  5270. byteIndex++;
  5271. }
  5272. bytesLeft = everything.length - byteIndex;
  5273. if (bytesLeft > 0) {
  5274. everything = everything.subarray(byteIndex);
  5275. } else {
  5276. everything = new Uint8Array();
  5277. }
  5278. };
  5279. this.reset = function () {
  5280. everything = new Uint8Array();
  5281. this.trigger('reset');
  5282. };
  5283. this.endTimeline = function () {
  5284. everything = new Uint8Array();
  5285. this.trigger('endedtimeline');
  5286. };
  5287. };
  5288. _AacStream.prototype = new stream();
  5289. var aac = _AacStream;
  5290. // constants
  5291. var AUDIO_PROPERTIES = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
  5292. var audioProperties = AUDIO_PROPERTIES;
  5293. var VIDEO_PROPERTIES = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
  5294. var videoProperties = VIDEO_PROPERTIES;
  5295. var H264Stream$1 = h264.H264Stream;
  5296. var isLikelyAacData$1 = utils.isLikelyAacData;
  5297. var ONE_SECOND_IN_TS$2 = clock.ONE_SECOND_IN_TS; // object types
  5298. var _VideoSegmentStream$1, _AudioSegmentStream$1, _Transmuxer$1, _CoalesceStream;
  5299. var retriggerForStream = function retriggerForStream(key, event) {
  5300. event.stream = key;
  5301. this.trigger('log', event);
  5302. };
  5303. var addPipelineLogRetriggers = function addPipelineLogRetriggers(transmuxer, pipeline) {
  5304. var keys = Object.keys(pipeline);
  5305. for (var i = 0; i < keys.length; i++) {
  5306. var key = keys[i]; // skip non-stream keys and headOfPipeline
  5307. // which is just a duplicate
  5308. if (key === 'headOfPipeline' || !pipeline[key].on) {
  5309. continue;
  5310. }
  5311. pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
  5312. }
  5313. };
  5314. /**
  5315. * Compare two arrays (even typed) for same-ness
  5316. */
  5317. var arrayEquals = function arrayEquals(a, b) {
  5318. var i;
  5319. if (a.length !== b.length) {
  5320. return false;
  5321. } // compare the value of each element in the array
  5322. for (i = 0; i < a.length; i++) {
  5323. if (a[i] !== b[i]) {
  5324. return false;
  5325. }
  5326. }
  5327. return true;
  5328. };
  5329. var generateSegmentTimingInfo = function generateSegmentTimingInfo(baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
  5330. var ptsOffsetFromDts = startPts - startDts,
  5331. decodeDuration = endDts - startDts,
  5332. presentationDuration = endPts - startPts; // The PTS and DTS values are based on the actual stream times from the segment,
  5333. // however, the player time values will reflect a start from the baseMediaDecodeTime.
  5334. // In order to provide relevant values for the player times, base timing info on the
  5335. // baseMediaDecodeTime and the DTS and PTS durations of the segment.
  5336. return {
  5337. start: {
  5338. dts: baseMediaDecodeTime,
  5339. pts: baseMediaDecodeTime + ptsOffsetFromDts
  5340. },
  5341. end: {
  5342. dts: baseMediaDecodeTime + decodeDuration,
  5343. pts: baseMediaDecodeTime + presentationDuration
  5344. },
  5345. prependedContentDuration: prependedContentDuration,
  5346. baseMediaDecodeTime: baseMediaDecodeTime
  5347. };
  5348. };
  5349. /**
  5350. * Constructs a single-track, ISO BMFF media segment from AAC data
  5351. * events. The output of this stream can be fed to a SourceBuffer
  5352. * configured with a suitable initialization segment.
  5353. * @param track {object} track metadata configuration
  5354. * @param options {object} transmuxer options object
  5355. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5356. * in the source; false to adjust the first segment to start at 0.
  5357. */
  5358. _AudioSegmentStream$1 = function AudioSegmentStream(track, options) {
  5359. var adtsFrames = [],
  5360. sequenceNumber,
  5361. earliestAllowedDts = 0,
  5362. audioAppendStartTs = 0,
  5363. videoBaseMediaDecodeTime = Infinity;
  5364. options = options || {};
  5365. sequenceNumber = options.firstSequenceNumber || 0;
  5366. _AudioSegmentStream$1.prototype.init.call(this);
  5367. this.push = function (data) {
  5368. trackDecodeInfo.collectDtsInfo(track, data);
  5369. if (track) {
  5370. audioProperties.forEach(function (prop) {
  5371. track[prop] = data[prop];
  5372. });
  5373. } // buffer audio data until end() is called
  5374. adtsFrames.push(data);
  5375. };
  5376. this.setEarliestDts = function (earliestDts) {
  5377. earliestAllowedDts = earliestDts;
  5378. };
  5379. this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  5380. videoBaseMediaDecodeTime = baseMediaDecodeTime;
  5381. };
  5382. this.setAudioAppendStart = function (timestamp) {
  5383. audioAppendStartTs = timestamp;
  5384. };
  5385. this.flush = function () {
  5386. var frames, moof, mdat, boxes, frameDuration, segmentDuration, videoClockCyclesOfSilencePrefixed; // return early if no audio data has been observed
  5387. if (adtsFrames.length === 0) {
  5388. this.trigger('done', 'AudioSegmentStream');
  5389. return;
  5390. }
  5391. frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
  5392. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps); // amount of audio filled but the value is in video clock rather than audio clock
  5393. videoClockCyclesOfSilencePrefixed = audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime); // we have to build the index from byte locations to
  5394. // samples (that is, adts frames) in the audio data
  5395. track.samples = audioFrameUtils.generateSampleTable(frames); // concatenate the audio data to constuct the mdat
  5396. mdat = mp4Generator.mdat(audioFrameUtils.concatenateFrameData(frames));
  5397. adtsFrames = [];
  5398. moof = mp4Generator.moof(sequenceNumber, [track]);
  5399. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // bump the sequence number for next time
  5400. sequenceNumber++;
  5401. boxes.set(moof);
  5402. boxes.set(mdat, moof.byteLength);
  5403. trackDecodeInfo.clearDtsInfo(track);
  5404. frameDuration = Math.ceil(ONE_SECOND_IN_TS$2 * 1024 / track.samplerate); // TODO this check was added to maintain backwards compatibility (particularly with
  5405. // tests) on adding the timingInfo event. However, it seems unlikely that there's a
  5406. // valid use-case where an init segment/data should be triggered without associated
  5407. // frames. Leaving for now, but should be looked into.
  5408. if (frames.length) {
  5409. segmentDuration = frames.length * frameDuration;
  5410. this.trigger('segmentTimingInfo', generateSegmentTimingInfo( // The audio track's baseMediaDecodeTime is in audio clock cycles, but the
  5411. // frame info is in video clock cycles. Convert to match expectation of
  5412. // listeners (that all timestamps will be based on video clock cycles).
  5413. clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate), // frame times are already in video clock, as is segment duration
  5414. frames[0].dts, frames[0].pts, frames[0].dts + segmentDuration, frames[0].pts + segmentDuration, videoClockCyclesOfSilencePrefixed || 0));
  5415. this.trigger('timingInfo', {
  5416. start: frames[0].pts,
  5417. end: frames[0].pts + segmentDuration
  5418. });
  5419. }
  5420. this.trigger('data', {
  5421. track: track,
  5422. boxes: boxes
  5423. });
  5424. this.trigger('done', 'AudioSegmentStream');
  5425. };
  5426. this.reset = function () {
  5427. trackDecodeInfo.clearDtsInfo(track);
  5428. adtsFrames = [];
  5429. this.trigger('reset');
  5430. };
  5431. };
  5432. _AudioSegmentStream$1.prototype = new stream();
  5433. /**
  5434. * Constructs a single-track, ISO BMFF media segment from H264 data
  5435. * events. The output of this stream can be fed to a SourceBuffer
  5436. * configured with a suitable initialization segment.
  5437. * @param track {object} track metadata configuration
  5438. * @param options {object} transmuxer options object
  5439. * @param options.alignGopsAtEnd {boolean} If true, start from the end of the
  5440. * gopsToAlignWith list when attempting to align gop pts
  5441. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5442. * in the source; false to adjust the first segment to start at 0.
  5443. */
  5444. _VideoSegmentStream$1 = function VideoSegmentStream(track, options) {
  5445. var sequenceNumber,
  5446. nalUnits = [],
  5447. gopsToAlignWith = [],
  5448. config,
  5449. pps;
  5450. options = options || {};
  5451. sequenceNumber = options.firstSequenceNumber || 0;
  5452. _VideoSegmentStream$1.prototype.init.call(this);
  5453. delete track.minPTS;
  5454. this.gopCache_ = [];
  5455. /**
  5456. * Constructs a ISO BMFF segment given H264 nalUnits
  5457. * @param {Object} nalUnit A data event representing a nalUnit
  5458. * @param {String} nalUnit.nalUnitType
  5459. * @param {Object} nalUnit.config Properties for a mp4 track
  5460. * @param {Uint8Array} nalUnit.data The nalUnit bytes
  5461. * @see lib/codecs/h264.js
  5462. **/
  5463. this.push = function (nalUnit) {
  5464. trackDecodeInfo.collectDtsInfo(track, nalUnit); // record the track config
  5465. if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
  5466. config = nalUnit.config;
  5467. track.sps = [nalUnit.data];
  5468. videoProperties.forEach(function (prop) {
  5469. track[prop] = config[prop];
  5470. }, this);
  5471. }
  5472. if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
  5473. pps = nalUnit.data;
  5474. track.pps = [nalUnit.data];
  5475. } // buffer video until flush() is called
  5476. nalUnits.push(nalUnit);
  5477. };
  5478. /**
  5479. * Pass constructed ISO BMFF track and boxes on to the
  5480. * next stream in the pipeline
  5481. **/
  5482. this.flush = function () {
  5483. var frames,
  5484. gopForFusion,
  5485. gops,
  5486. moof,
  5487. mdat,
  5488. boxes,
  5489. prependedContentDuration = 0,
  5490. firstGop,
  5491. lastGop; // Throw away nalUnits at the start of the byte stream until
  5492. // we find the first AUD
  5493. while (nalUnits.length) {
  5494. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  5495. break;
  5496. }
  5497. nalUnits.shift();
  5498. } // Return early if no video data has been observed
  5499. if (nalUnits.length === 0) {
  5500. this.resetStream_();
  5501. this.trigger('done', 'VideoSegmentStream');
  5502. return;
  5503. } // Organize the raw nal-units into arrays that represent
  5504. // higher-level constructs such as frames and gops
  5505. // (group-of-pictures)
  5506. frames = frameUtils.groupNalsIntoFrames(nalUnits);
  5507. gops = frameUtils.groupFramesIntoGops(frames); // If the first frame of this fragment is not a keyframe we have
  5508. // a problem since MSE (on Chrome) requires a leading keyframe.
  5509. //
  5510. // We have two approaches to repairing this situation:
  5511. // 1) GOP-FUSION:
  5512. // This is where we keep track of the GOPS (group-of-pictures)
  5513. // from previous fragments and attempt to find one that we can
  5514. // prepend to the current fragment in order to create a valid
  5515. // fragment.
  5516. // 2) KEYFRAME-PULLING:
  5517. // Here we search for the first keyframe in the fragment and
  5518. // throw away all the frames between the start of the fragment
  5519. // and that keyframe. We then extend the duration and pull the
  5520. // PTS of the keyframe forward so that it covers the time range
  5521. // of the frames that were disposed of.
  5522. //
  5523. // #1 is far prefereable over #2 which can cause "stuttering" but
  5524. // requires more things to be just right.
  5525. if (!gops[0][0].keyFrame) {
  5526. // Search for a gop for fusion from our gopCache
  5527. gopForFusion = this.getGopForFusion_(nalUnits[0], track);
  5528. if (gopForFusion) {
  5529. // in order to provide more accurate timing information about the segment, save
  5530. // the number of seconds prepended to the original segment due to GOP fusion
  5531. prependedContentDuration = gopForFusion.duration;
  5532. gops.unshift(gopForFusion); // Adjust Gops' metadata to account for the inclusion of the
  5533. // new gop at the beginning
  5534. gops.byteLength += gopForFusion.byteLength;
  5535. gops.nalCount += gopForFusion.nalCount;
  5536. gops.pts = gopForFusion.pts;
  5537. gops.dts = gopForFusion.dts;
  5538. gops.duration += gopForFusion.duration;
  5539. } else {
  5540. // If we didn't find a candidate gop fall back to keyframe-pulling
  5541. gops = frameUtils.extendFirstKeyFrame(gops);
  5542. }
  5543. } // Trim gops to align with gopsToAlignWith
  5544. if (gopsToAlignWith.length) {
  5545. var alignedGops;
  5546. if (options.alignGopsAtEnd) {
  5547. alignedGops = this.alignGopsAtEnd_(gops);
  5548. } else {
  5549. alignedGops = this.alignGopsAtStart_(gops);
  5550. }
  5551. if (!alignedGops) {
  5552. // save all the nals in the last GOP into the gop cache
  5553. this.gopCache_.unshift({
  5554. gop: gops.pop(),
  5555. pps: track.pps,
  5556. sps: track.sps
  5557. }); // Keep a maximum of 6 GOPs in the cache
  5558. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  5559. nalUnits = []; // return early no gops can be aligned with desired gopsToAlignWith
  5560. this.resetStream_();
  5561. this.trigger('done', 'VideoSegmentStream');
  5562. return;
  5563. } // Some gops were trimmed. clear dts info so minSegmentDts and pts are correct
  5564. // when recalculated before sending off to CoalesceStream
  5565. trackDecodeInfo.clearDtsInfo(track);
  5566. gops = alignedGops;
  5567. }
  5568. trackDecodeInfo.collectDtsInfo(track, gops); // First, we have to build the index from byte locations to
  5569. // samples (that is, frames) in the video data
  5570. track.samples = frameUtils.generateSampleTable(gops); // Concatenate the video data and construct the mdat
  5571. mdat = mp4Generator.mdat(frameUtils.concatenateNalData(gops));
  5572. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
  5573. this.trigger('processedGopsInfo', gops.map(function (gop) {
  5574. return {
  5575. pts: gop.pts,
  5576. dts: gop.dts,
  5577. byteLength: gop.byteLength
  5578. };
  5579. }));
  5580. firstGop = gops[0];
  5581. lastGop = gops[gops.length - 1];
  5582. this.trigger('segmentTimingInfo', generateSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
  5583. this.trigger('timingInfo', {
  5584. start: gops[0].pts,
  5585. end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
  5586. }); // save all the nals in the last GOP into the gop cache
  5587. this.gopCache_.unshift({
  5588. gop: gops.pop(),
  5589. pps: track.pps,
  5590. sps: track.sps
  5591. }); // Keep a maximum of 6 GOPs in the cache
  5592. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  5593. nalUnits = [];
  5594. this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
  5595. this.trigger('timelineStartInfo', track.timelineStartInfo);
  5596. moof = mp4Generator.moof(sequenceNumber, [track]); // it would be great to allocate this array up front instead of
  5597. // throwing away hundreds of media segment fragments
  5598. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // Bump the sequence number for next time
  5599. sequenceNumber++;
  5600. boxes.set(moof);
  5601. boxes.set(mdat, moof.byteLength);
  5602. this.trigger('data', {
  5603. track: track,
  5604. boxes: boxes
  5605. });
  5606. this.resetStream_(); // Continue with the flush process now
  5607. this.trigger('done', 'VideoSegmentStream');
  5608. };
  5609. this.reset = function () {
  5610. this.resetStream_();
  5611. nalUnits = [];
  5612. this.gopCache_.length = 0;
  5613. gopsToAlignWith.length = 0;
  5614. this.trigger('reset');
  5615. };
  5616. this.resetStream_ = function () {
  5617. trackDecodeInfo.clearDtsInfo(track); // reset config and pps because they may differ across segments
  5618. // for instance, when we are rendition switching
  5619. config = undefined;
  5620. pps = undefined;
  5621. }; // Search for a candidate Gop for gop-fusion from the gop cache and
  5622. // return it or return null if no good candidate was found
  5623. this.getGopForFusion_ = function (nalUnit) {
  5624. var halfSecond = 45000,
  5625. // Half-a-second in a 90khz clock
  5626. allowableOverlap = 10000,
  5627. // About 3 frames @ 30fps
  5628. nearestDistance = Infinity,
  5629. dtsDistance,
  5630. nearestGopObj,
  5631. currentGop,
  5632. currentGopObj,
  5633. i; // Search for the GOP nearest to the beginning of this nal unit
  5634. for (i = 0; i < this.gopCache_.length; i++) {
  5635. currentGopObj = this.gopCache_[i];
  5636. currentGop = currentGopObj.gop; // Reject Gops with different SPS or PPS
  5637. if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
  5638. continue;
  5639. } // Reject Gops that would require a negative baseMediaDecodeTime
  5640. if (currentGop.dts < track.timelineStartInfo.dts) {
  5641. continue;
  5642. } // The distance between the end of the gop and the start of the nalUnit
  5643. dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration; // Only consider GOPS that start before the nal unit and end within
  5644. // a half-second of the nal unit
  5645. if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
  5646. // Always use the closest GOP we found if there is more than
  5647. // one candidate
  5648. if (!nearestGopObj || nearestDistance > dtsDistance) {
  5649. nearestGopObj = currentGopObj;
  5650. nearestDistance = dtsDistance;
  5651. }
  5652. }
  5653. }
  5654. if (nearestGopObj) {
  5655. return nearestGopObj.gop;
  5656. }
  5657. return null;
  5658. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  5659. // of gopsToAlignWith starting from the START of the list
  5660. this.alignGopsAtStart_ = function (gops) {
  5661. var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
  5662. byteLength = gops.byteLength;
  5663. nalCount = gops.nalCount;
  5664. duration = gops.duration;
  5665. alignIndex = gopIndex = 0;
  5666. while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
  5667. align = gopsToAlignWith[alignIndex];
  5668. gop = gops[gopIndex];
  5669. if (align.pts === gop.pts) {
  5670. break;
  5671. }
  5672. if (gop.pts > align.pts) {
  5673. // this current gop starts after the current gop we want to align on, so increment
  5674. // align index
  5675. alignIndex++;
  5676. continue;
  5677. } // current gop starts before the current gop we want to align on. so increment gop
  5678. // index
  5679. gopIndex++;
  5680. byteLength -= gop.byteLength;
  5681. nalCount -= gop.nalCount;
  5682. duration -= gop.duration;
  5683. }
  5684. if (gopIndex === 0) {
  5685. // no gops to trim
  5686. return gops;
  5687. }
  5688. if (gopIndex === gops.length) {
  5689. // all gops trimmed, skip appending all gops
  5690. return null;
  5691. }
  5692. alignedGops = gops.slice(gopIndex);
  5693. alignedGops.byteLength = byteLength;
  5694. alignedGops.duration = duration;
  5695. alignedGops.nalCount = nalCount;
  5696. alignedGops.pts = alignedGops[0].pts;
  5697. alignedGops.dts = alignedGops[0].dts;
  5698. return alignedGops;
  5699. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  5700. // of gopsToAlignWith starting from the END of the list
  5701. this.alignGopsAtEnd_ = function (gops) {
  5702. var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
  5703. alignIndex = gopsToAlignWith.length - 1;
  5704. gopIndex = gops.length - 1;
  5705. alignEndIndex = null;
  5706. matchFound = false;
  5707. while (alignIndex >= 0 && gopIndex >= 0) {
  5708. align = gopsToAlignWith[alignIndex];
  5709. gop = gops[gopIndex];
  5710. if (align.pts === gop.pts) {
  5711. matchFound = true;
  5712. break;
  5713. }
  5714. if (align.pts > gop.pts) {
  5715. alignIndex--;
  5716. continue;
  5717. }
  5718. if (alignIndex === gopsToAlignWith.length - 1) {
  5719. // gop.pts is greater than the last alignment candidate. If no match is found
  5720. // by the end of this loop, we still want to append gops that come after this
  5721. // point
  5722. alignEndIndex = gopIndex;
  5723. }
  5724. gopIndex--;
  5725. }
  5726. if (!matchFound && alignEndIndex === null) {
  5727. return null;
  5728. }
  5729. var trimIndex;
  5730. if (matchFound) {
  5731. trimIndex = gopIndex;
  5732. } else {
  5733. trimIndex = alignEndIndex;
  5734. }
  5735. if (trimIndex === 0) {
  5736. return gops;
  5737. }
  5738. var alignedGops = gops.slice(trimIndex);
  5739. var metadata = alignedGops.reduce(function (total, gop) {
  5740. total.byteLength += gop.byteLength;
  5741. total.duration += gop.duration;
  5742. total.nalCount += gop.nalCount;
  5743. return total;
  5744. }, {
  5745. byteLength: 0,
  5746. duration: 0,
  5747. nalCount: 0
  5748. });
  5749. alignedGops.byteLength = metadata.byteLength;
  5750. alignedGops.duration = metadata.duration;
  5751. alignedGops.nalCount = metadata.nalCount;
  5752. alignedGops.pts = alignedGops[0].pts;
  5753. alignedGops.dts = alignedGops[0].dts;
  5754. return alignedGops;
  5755. };
  5756. this.alignGopsWith = function (newGopsToAlignWith) {
  5757. gopsToAlignWith = newGopsToAlignWith;
  5758. };
  5759. };
  5760. _VideoSegmentStream$1.prototype = new stream();
  5761. /**
  5762. * A Stream that can combine multiple streams (ie. audio & video)
  5763. * into a single output segment for MSE. Also supports audio-only
  5764. * and video-only streams.
  5765. * @param options {object} transmuxer options object
  5766. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5767. * in the source; false to adjust the first segment to start at media timeline start.
  5768. */
  5769. _CoalesceStream = function CoalesceStream(options, metadataStream) {
  5770. // Number of Tracks per output segment
  5771. // If greater than 1, we combine multiple
  5772. // tracks into a single segment
  5773. this.numberOfTracks = 0;
  5774. this.metadataStream = metadataStream;
  5775. options = options || {};
  5776. if (typeof options.remux !== 'undefined') {
  5777. this.remuxTracks = !!options.remux;
  5778. } else {
  5779. this.remuxTracks = true;
  5780. }
  5781. if (typeof options.keepOriginalTimestamps === 'boolean') {
  5782. this.keepOriginalTimestamps = options.keepOriginalTimestamps;
  5783. } else {
  5784. this.keepOriginalTimestamps = false;
  5785. }
  5786. this.pendingTracks = [];
  5787. this.videoTrack = null;
  5788. this.pendingBoxes = [];
  5789. this.pendingCaptions = [];
  5790. this.pendingMetadata = [];
  5791. this.pendingBytes = 0;
  5792. this.emittedTracks = 0;
  5793. _CoalesceStream.prototype.init.call(this); // Take output from multiple
  5794. this.push = function (output) {
  5795. // buffer incoming captions until the associated video segment
  5796. // finishes
  5797. if (output.text) {
  5798. return this.pendingCaptions.push(output);
  5799. } // buffer incoming id3 tags until the final flush
  5800. if (output.frames) {
  5801. return this.pendingMetadata.push(output);
  5802. } // Add this track to the list of pending tracks and store
  5803. // important information required for the construction of
  5804. // the final segment
  5805. this.pendingTracks.push(output.track);
  5806. this.pendingBytes += output.boxes.byteLength; // TODO: is there an issue for this against chrome?
  5807. // We unshift audio and push video because
  5808. // as of Chrome 75 when switching from
  5809. // one init segment to another if the video
  5810. // mdat does not appear after the audio mdat
  5811. // only audio will play for the duration of our transmux.
  5812. if (output.track.type === 'video') {
  5813. this.videoTrack = output.track;
  5814. this.pendingBoxes.push(output.boxes);
  5815. }
  5816. if (output.track.type === 'audio') {
  5817. this.audioTrack = output.track;
  5818. this.pendingBoxes.unshift(output.boxes);
  5819. }
  5820. };
  5821. };
  5822. _CoalesceStream.prototype = new stream();
  5823. _CoalesceStream.prototype.flush = function (flushSource) {
  5824. var offset = 0,
  5825. event = {
  5826. captions: [],
  5827. captionStreams: {},
  5828. metadata: [],
  5829. info: {}
  5830. },
  5831. caption,
  5832. id3,
  5833. initSegment,
  5834. timelineStartPts = 0,
  5835. i;
  5836. if (this.pendingTracks.length < this.numberOfTracks) {
  5837. if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
  5838. // Return because we haven't received a flush from a data-generating
  5839. // portion of the segment (meaning that we have only recieved meta-data
  5840. // or captions.)
  5841. return;
  5842. } else if (this.remuxTracks) {
  5843. // Return until we have enough tracks from the pipeline to remux (if we
  5844. // are remuxing audio and video into a single MP4)
  5845. return;
  5846. } else if (this.pendingTracks.length === 0) {
  5847. // In the case where we receive a flush without any data having been
  5848. // received we consider it an emitted track for the purposes of coalescing
  5849. // `done` events.
  5850. // We do this for the case where there is an audio and video track in the
  5851. // segment but no audio data. (seen in several playlists with alternate
  5852. // audio tracks and no audio present in the main TS segments.)
  5853. this.emittedTracks++;
  5854. if (this.emittedTracks >= this.numberOfTracks) {
  5855. this.trigger('done');
  5856. this.emittedTracks = 0;
  5857. }
  5858. return;
  5859. }
  5860. }
  5861. if (this.videoTrack) {
  5862. timelineStartPts = this.videoTrack.timelineStartInfo.pts;
  5863. videoProperties.forEach(function (prop) {
  5864. event.info[prop] = this.videoTrack[prop];
  5865. }, this);
  5866. } else if (this.audioTrack) {
  5867. timelineStartPts = this.audioTrack.timelineStartInfo.pts;
  5868. audioProperties.forEach(function (prop) {
  5869. event.info[prop] = this.audioTrack[prop];
  5870. }, this);
  5871. }
  5872. if (this.videoTrack || this.audioTrack) {
  5873. if (this.pendingTracks.length === 1) {
  5874. event.type = this.pendingTracks[0].type;
  5875. } else {
  5876. event.type = 'combined';
  5877. }
  5878. this.emittedTracks += this.pendingTracks.length;
  5879. initSegment = mp4Generator.initSegment(this.pendingTracks); // Create a new typed array to hold the init segment
  5880. event.initSegment = new Uint8Array(initSegment.byteLength); // Create an init segment containing a moov
  5881. // and track definitions
  5882. event.initSegment.set(initSegment); // Create a new typed array to hold the moof+mdats
  5883. event.data = new Uint8Array(this.pendingBytes); // Append each moof+mdat (one per track) together
  5884. for (i = 0; i < this.pendingBoxes.length; i++) {
  5885. event.data.set(this.pendingBoxes[i], offset);
  5886. offset += this.pendingBoxes[i].byteLength;
  5887. } // Translate caption PTS times into second offsets to match the
  5888. // video timeline for the segment, and add track info
  5889. for (i = 0; i < this.pendingCaptions.length; i++) {
  5890. caption = this.pendingCaptions[i];
  5891. caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
  5892. caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
  5893. event.captionStreams[caption.stream] = true;
  5894. event.captions.push(caption);
  5895. } // Translate ID3 frame PTS times into second offsets to match the
  5896. // video timeline for the segment
  5897. for (i = 0; i < this.pendingMetadata.length; i++) {
  5898. id3 = this.pendingMetadata[i];
  5899. id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
  5900. event.metadata.push(id3);
  5901. } // We add this to every single emitted segment even though we only need
  5902. // it for the first
  5903. event.metadata.dispatchType = this.metadataStream.dispatchType; // Reset stream state
  5904. this.pendingTracks.length = 0;
  5905. this.videoTrack = null;
  5906. this.pendingBoxes.length = 0;
  5907. this.pendingCaptions.length = 0;
  5908. this.pendingBytes = 0;
  5909. this.pendingMetadata.length = 0; // Emit the built segment
  5910. // We include captions and ID3 tags for backwards compatibility,
  5911. // ideally we should send only video and audio in the data event
  5912. this.trigger('data', event); // Emit each caption to the outside world
  5913. // Ideally, this would happen immediately on parsing captions,
  5914. // but we need to ensure that video data is sent back first
  5915. // so that caption timing can be adjusted to match video timing
  5916. for (i = 0; i < event.captions.length; i++) {
  5917. caption = event.captions[i];
  5918. this.trigger('caption', caption);
  5919. } // Emit each id3 tag to the outside world
  5920. // Ideally, this would happen immediately on parsing the tag,
  5921. // but we need to ensure that video data is sent back first
  5922. // so that ID3 frame timing can be adjusted to match video timing
  5923. for (i = 0; i < event.metadata.length; i++) {
  5924. id3 = event.metadata[i];
  5925. this.trigger('id3Frame', id3);
  5926. }
  5927. } // Only emit `done` if all tracks have been flushed and emitted
  5928. if (this.emittedTracks >= this.numberOfTracks) {
  5929. this.trigger('done');
  5930. this.emittedTracks = 0;
  5931. }
  5932. };
  5933. _CoalesceStream.prototype.setRemux = function (val) {
  5934. this.remuxTracks = val;
  5935. };
  5936. /**
  5937. * A Stream that expects MP2T binary data as input and produces
  5938. * corresponding media segments, suitable for use with Media Source
  5939. * Extension (MSE) implementations that support the ISO BMFF byte
  5940. * stream format, like Chrome.
  5941. */
  5942. _Transmuxer$1 = function Transmuxer(options) {
  5943. var self = this,
  5944. hasFlushed = true,
  5945. videoTrack,
  5946. audioTrack;
  5947. _Transmuxer$1.prototype.init.call(this);
  5948. options = options || {};
  5949. this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
  5950. this.transmuxPipeline_ = {};
  5951. this.setupAacPipeline = function () {
  5952. var pipeline = {};
  5953. this.transmuxPipeline_ = pipeline;
  5954. pipeline.type = 'aac';
  5955. pipeline.metadataStream = new m2ts_1.MetadataStream(); // set up the parsing pipeline
  5956. pipeline.aacStream = new aac();
  5957. pipeline.audioTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('audio');
  5958. pipeline.timedMetadataTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('timed-metadata');
  5959. pipeline.adtsStream = new adts();
  5960. pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
  5961. pipeline.headOfPipeline = pipeline.aacStream;
  5962. pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
  5963. pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
  5964. pipeline.metadataStream.on('timestamp', function (frame) {
  5965. pipeline.aacStream.setTimestamp(frame.timeStamp);
  5966. });
  5967. pipeline.aacStream.on('data', function (data) {
  5968. if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
  5969. return;
  5970. }
  5971. audioTrack = audioTrack || {
  5972. timelineStartInfo: {
  5973. baseMediaDecodeTime: self.baseMediaDecodeTime
  5974. },
  5975. codec: 'adts',
  5976. type: 'audio'
  5977. }; // hook up the audio segment stream to the first track with aac data
  5978. pipeline.coalesceStream.numberOfTracks++;
  5979. pipeline.audioSegmentStream = new _AudioSegmentStream$1(audioTrack, options);
  5980. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  5981. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo')); // Set up the final part of the audio pipeline
  5982. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream); // emit pmt info
  5983. self.trigger('trackinfo', {
  5984. hasAudio: !!audioTrack,
  5985. hasVideo: !!videoTrack
  5986. });
  5987. }); // Re-emit any data coming from the coalesce stream to the outside world
  5988. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data')); // Let the consumer know we have finished flushing the entire pipeline
  5989. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  5990. addPipelineLogRetriggers(this, pipeline);
  5991. };
  5992. this.setupTsPipeline = function () {
  5993. var pipeline = {};
  5994. this.transmuxPipeline_ = pipeline;
  5995. pipeline.type = 'ts';
  5996. pipeline.metadataStream = new m2ts_1.MetadataStream(); // set up the parsing pipeline
  5997. pipeline.packetStream = new m2ts_1.TransportPacketStream();
  5998. pipeline.parseStream = new m2ts_1.TransportParseStream();
  5999. pipeline.elementaryStream = new m2ts_1.ElementaryStream();
  6000. pipeline.timestampRolloverStream = new m2ts_1.TimestampRolloverStream();
  6001. pipeline.adtsStream = new adts();
  6002. pipeline.h264Stream = new H264Stream$1();
  6003. pipeline.captionStream = new m2ts_1.CaptionStream(options);
  6004. pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
  6005. pipeline.headOfPipeline = pipeline.packetStream; // disassemble MPEG2-TS packets into elementary streams
  6006. pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream); // !!THIS ORDER IS IMPORTANT!!
  6007. // demux the streams
  6008. pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
  6009. pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
  6010. pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream); // Hook up CEA-608/708 caption stream
  6011. pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
  6012. pipeline.elementaryStream.on('data', function (data) {
  6013. var i;
  6014. if (data.type === 'metadata') {
  6015. i = data.tracks.length; // scan the tracks listed in the metadata
  6016. while (i--) {
  6017. if (!videoTrack && data.tracks[i].type === 'video') {
  6018. videoTrack = data.tracks[i];
  6019. videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  6020. } else if (!audioTrack && data.tracks[i].type === 'audio') {
  6021. audioTrack = data.tracks[i];
  6022. audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  6023. }
  6024. } // hook up the video segment stream to the first track with h264 data
  6025. if (videoTrack && !pipeline.videoSegmentStream) {
  6026. pipeline.coalesceStream.numberOfTracks++;
  6027. pipeline.videoSegmentStream = new _VideoSegmentStream$1(videoTrack, options);
  6028. pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));
  6029. pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
  6030. // When video emits timelineStartInfo data after a flush, we forward that
  6031. // info to the AudioSegmentStream, if it exists, because video timeline
  6032. // data takes precedence. Do not do this if keepOriginalTimestamps is set,
  6033. // because this is a particularly subtle form of timestamp alteration.
  6034. if (audioTrack && !options.keepOriginalTimestamps) {
  6035. audioTrack.timelineStartInfo = timelineStartInfo; // On the first segment we trim AAC frames that exist before the
  6036. // very earliest DTS we have seen in video because Chrome will
  6037. // interpret any video track with a baseMediaDecodeTime that is
  6038. // non-zero as a gap.
  6039. pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
  6040. }
  6041. });
  6042. pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
  6043. pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
  6044. pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
  6045. if (audioTrack) {
  6046. pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
  6047. }
  6048. });
  6049. pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo')); // Set up the final part of the video pipeline
  6050. pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
  6051. }
  6052. if (audioTrack && !pipeline.audioSegmentStream) {
  6053. // hook up the audio segment stream to the first track with aac data
  6054. pipeline.coalesceStream.numberOfTracks++;
  6055. pipeline.audioSegmentStream = new _AudioSegmentStream$1(audioTrack, options);
  6056. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  6057. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
  6058. pipeline.audioSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'audioSegmentTimingInfo')); // Set up the final part of the audio pipeline
  6059. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
  6060. } // emit pmt info
  6061. self.trigger('trackinfo', {
  6062. hasAudio: !!audioTrack,
  6063. hasVideo: !!videoTrack
  6064. });
  6065. }
  6066. }); // Re-emit any data coming from the coalesce stream to the outside world
  6067. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
  6068. pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
  6069. id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
  6070. self.trigger('id3Frame', id3Frame);
  6071. });
  6072. pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption')); // Let the consumer know we have finished flushing the entire pipeline
  6073. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  6074. addPipelineLogRetriggers(this, pipeline);
  6075. }; // hook up the segment streams once track metadata is delivered
  6076. this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  6077. var pipeline = this.transmuxPipeline_;
  6078. if (!options.keepOriginalTimestamps) {
  6079. this.baseMediaDecodeTime = baseMediaDecodeTime;
  6080. }
  6081. if (audioTrack) {
  6082. audioTrack.timelineStartInfo.dts = undefined;
  6083. audioTrack.timelineStartInfo.pts = undefined;
  6084. trackDecodeInfo.clearDtsInfo(audioTrack);
  6085. if (pipeline.audioTimestampRolloverStream) {
  6086. pipeline.audioTimestampRolloverStream.discontinuity();
  6087. }
  6088. }
  6089. if (videoTrack) {
  6090. if (pipeline.videoSegmentStream) {
  6091. pipeline.videoSegmentStream.gopCache_ = [];
  6092. }
  6093. videoTrack.timelineStartInfo.dts = undefined;
  6094. videoTrack.timelineStartInfo.pts = undefined;
  6095. trackDecodeInfo.clearDtsInfo(videoTrack);
  6096. pipeline.captionStream.reset();
  6097. }
  6098. if (pipeline.timestampRolloverStream) {
  6099. pipeline.timestampRolloverStream.discontinuity();
  6100. }
  6101. };
  6102. this.setAudioAppendStart = function (timestamp) {
  6103. if (audioTrack) {
  6104. this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
  6105. }
  6106. };
  6107. this.setRemux = function (val) {
  6108. var pipeline = this.transmuxPipeline_;
  6109. options.remux = val;
  6110. if (pipeline && pipeline.coalesceStream) {
  6111. pipeline.coalesceStream.setRemux(val);
  6112. }
  6113. };
  6114. this.alignGopsWith = function (gopsToAlignWith) {
  6115. if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
  6116. this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
  6117. }
  6118. };
  6119. this.getLogTrigger_ = function (key) {
  6120. var self = this;
  6121. return function (event) {
  6122. event.stream = key;
  6123. self.trigger('log', event);
  6124. };
  6125. }; // feed incoming data to the front of the parsing pipeline
  6126. this.push = function (data) {
  6127. if (hasFlushed) {
  6128. var isAac = isLikelyAacData$1(data);
  6129. if (isAac && this.transmuxPipeline_.type !== 'aac') {
  6130. this.setupAacPipeline();
  6131. } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
  6132. this.setupTsPipeline();
  6133. }
  6134. hasFlushed = false;
  6135. }
  6136. this.transmuxPipeline_.headOfPipeline.push(data);
  6137. }; // flush any buffered data
  6138. this.flush = function () {
  6139. hasFlushed = true; // Start at the top of the pipeline and flush all pending work
  6140. this.transmuxPipeline_.headOfPipeline.flush();
  6141. };
  6142. this.endTimeline = function () {
  6143. this.transmuxPipeline_.headOfPipeline.endTimeline();
  6144. };
  6145. this.reset = function () {
  6146. if (this.transmuxPipeline_.headOfPipeline) {
  6147. this.transmuxPipeline_.headOfPipeline.reset();
  6148. }
  6149. }; // Caption data has to be reset when seeking outside buffered range
  6150. this.resetCaptions = function () {
  6151. if (this.transmuxPipeline_.captionStream) {
  6152. this.transmuxPipeline_.captionStream.reset();
  6153. }
  6154. };
  6155. };
  6156. _Transmuxer$1.prototype = new stream();
  6157. var transmuxer$2 = {
  6158. Transmuxer: _Transmuxer$1,
  6159. VideoSegmentStream: _VideoSegmentStream$1,
  6160. AudioSegmentStream: _AudioSegmentStream$1,
  6161. AUDIO_PROPERTIES: audioProperties,
  6162. VIDEO_PROPERTIES: videoProperties,
  6163. // exported for testing
  6164. generateSegmentTimingInfo: generateSegmentTimingInfo
  6165. };
  6166. var discardEmulationPreventionBytes = captionPacketParser.discardEmulationPreventionBytes;
  6167. var CaptionStream = captionStream.CaptionStream;
  6168. /**
  6169. * Maps an offset in the mdat to a sample based on the the size of the samples.
  6170. * Assumes that `parseSamples` has been called first.
  6171. *
  6172. * @param {Number} offset - The offset into the mdat
  6173. * @param {Object[]} samples - An array of samples, parsed using `parseSamples`
  6174. * @return {?Object} The matching sample, or null if no match was found.
  6175. *
  6176. * @see ISO-BMFF-12/2015, Section 8.8.8
  6177. **/
  6178. var mapToSample = function mapToSample(offset, samples) {
  6179. var approximateOffset = offset;
  6180. for (var i = 0; i < samples.length; i++) {
  6181. var sample = samples[i];
  6182. if (approximateOffset < sample.size) {
  6183. return sample;
  6184. }
  6185. approximateOffset -= sample.size;
  6186. }
  6187. return null;
  6188. };
  6189. /**
  6190. * Finds SEI nal units contained in a Media Data Box.
  6191. * Assumes that `parseSamples` has been called first.
  6192. *
  6193. * @param {Uint8Array} avcStream - The bytes of the mdat
  6194. * @param {Object[]} samples - The samples parsed out by `parseSamples`
  6195. * @param {Number} trackId - The trackId of this video track
  6196. * @return {Object[]} seiNals - the parsed SEI NALUs found.
  6197. * The contents of the seiNal should match what is expected by
  6198. * CaptionStream.push (nalUnitType, size, data, escapedRBSP, pts, dts)
  6199. *
  6200. * @see ISO-BMFF-12/2015, Section 8.1.1
  6201. * @see Rec. ITU-T H.264, 7.3.2.3.1
  6202. **/
  6203. var findSeiNals = function findSeiNals(avcStream, samples, trackId) {
  6204. var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
  6205. result = {
  6206. logs: [],
  6207. seiNals: []
  6208. },
  6209. seiNal,
  6210. i,
  6211. length,
  6212. lastMatchedSample;
  6213. for (i = 0; i + 4 < avcStream.length; i += length) {
  6214. length = avcView.getUint32(i);
  6215. i += 4; // Bail if this doesn't appear to be an H264 stream
  6216. if (length <= 0) {
  6217. continue;
  6218. }
  6219. switch (avcStream[i] & 0x1F) {
  6220. case 0x06:
  6221. var data = avcStream.subarray(i + 1, i + 1 + length);
  6222. var matchingSample = mapToSample(i, samples);
  6223. seiNal = {
  6224. nalUnitType: 'sei_rbsp',
  6225. size: length,
  6226. data: data,
  6227. escapedRBSP: discardEmulationPreventionBytes(data),
  6228. trackId: trackId
  6229. };
  6230. if (matchingSample) {
  6231. seiNal.pts = matchingSample.pts;
  6232. seiNal.dts = matchingSample.dts;
  6233. lastMatchedSample = matchingSample;
  6234. } else if (lastMatchedSample) {
  6235. // If a matching sample cannot be found, use the last
  6236. // sample's values as they should be as close as possible
  6237. seiNal.pts = lastMatchedSample.pts;
  6238. seiNal.dts = lastMatchedSample.dts;
  6239. } else {
  6240. result.logs.push({
  6241. level: 'warn',
  6242. message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
  6243. });
  6244. break;
  6245. }
  6246. result.seiNals.push(seiNal);
  6247. break;
  6248. }
  6249. }
  6250. return result;
  6251. };
  6252. /**
  6253. * Parses sample information out of Track Run Boxes and calculates
  6254. * the absolute presentation and decode timestamps of each sample.
  6255. *
  6256. * @param {Array<Uint8Array>} truns - The Trun Run boxes to be parsed
  6257. * @param {Number|BigInt} baseMediaDecodeTime - base media decode time from tfdt
  6258. @see ISO-BMFF-12/2015, Section 8.8.12
  6259. * @param {Object} tfhd - The parsed Track Fragment Header
  6260. * @see inspect.parseTfhd
  6261. * @return {Object[]} the parsed samples
  6262. *
  6263. * @see ISO-BMFF-12/2015, Section 8.8.8
  6264. **/
  6265. var parseSamples = function parseSamples(truns, baseMediaDecodeTime, tfhd) {
  6266. var currentDts = baseMediaDecodeTime;
  6267. var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
  6268. var defaultSampleSize = tfhd.defaultSampleSize || 0;
  6269. var trackId = tfhd.trackId;
  6270. var allSamples = [];
  6271. truns.forEach(function (trun) {
  6272. // Note: We currently do not parse the sample table as well
  6273. // as the trun. It's possible some sources will require this.
  6274. // moov > trak > mdia > minf > stbl
  6275. var trackRun = parseTrun(trun);
  6276. var samples = trackRun.samples;
  6277. samples.forEach(function (sample) {
  6278. if (sample.duration === undefined) {
  6279. sample.duration = defaultSampleDuration;
  6280. }
  6281. if (sample.size === undefined) {
  6282. sample.size = defaultSampleSize;
  6283. }
  6284. sample.trackId = trackId;
  6285. sample.dts = currentDts;
  6286. if (sample.compositionTimeOffset === undefined) {
  6287. sample.compositionTimeOffset = 0;
  6288. }
  6289. if (typeof currentDts === 'bigint') {
  6290. sample.pts = currentDts + window__default['default'].BigInt(sample.compositionTimeOffset);
  6291. currentDts += window__default['default'].BigInt(sample.duration);
  6292. } else {
  6293. sample.pts = currentDts + sample.compositionTimeOffset;
  6294. currentDts += sample.duration;
  6295. }
  6296. });
  6297. allSamples = allSamples.concat(samples);
  6298. });
  6299. return allSamples;
  6300. };
  6301. /**
  6302. * Parses out caption nals from an FMP4 segment's video tracks.
  6303. *
  6304. * @param {Uint8Array} segment - The bytes of a single segment
  6305. * @param {Number} videoTrackId - The trackId of a video track in the segment
  6306. * @return {Object.<Number, Object[]>} A mapping of video trackId to
  6307. * a list of seiNals found in that track
  6308. **/
  6309. var parseCaptionNals = function parseCaptionNals(segment, videoTrackId) {
  6310. // To get the samples
  6311. var trafs = findBox_1(segment, ['moof', 'traf']); // To get SEI NAL units
  6312. var mdats = findBox_1(segment, ['mdat']);
  6313. var captionNals = {};
  6314. var mdatTrafPairs = []; // Pair up each traf with a mdat as moofs and mdats are in pairs
  6315. mdats.forEach(function (mdat, index) {
  6316. var matchingTraf = trafs[index];
  6317. mdatTrafPairs.push({
  6318. mdat: mdat,
  6319. traf: matchingTraf
  6320. });
  6321. });
  6322. mdatTrafPairs.forEach(function (pair) {
  6323. var mdat = pair.mdat;
  6324. var traf = pair.traf;
  6325. var tfhd = findBox_1(traf, ['tfhd']); // Exactly 1 tfhd per traf
  6326. var headerInfo = parseTfhd(tfhd[0]);
  6327. var trackId = headerInfo.trackId;
  6328. var tfdt = findBox_1(traf, ['tfdt']); // Either 0 or 1 tfdt per traf
  6329. var baseMediaDecodeTime = tfdt.length > 0 ? parseTfdt(tfdt[0]).baseMediaDecodeTime : 0;
  6330. var truns = findBox_1(traf, ['trun']);
  6331. var samples;
  6332. var result; // Only parse video data for the chosen video track
  6333. if (videoTrackId === trackId && truns.length > 0) {
  6334. samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
  6335. result = findSeiNals(mdat, samples, trackId);
  6336. if (!captionNals[trackId]) {
  6337. captionNals[trackId] = {
  6338. seiNals: [],
  6339. logs: []
  6340. };
  6341. }
  6342. captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
  6343. captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
  6344. }
  6345. });
  6346. return captionNals;
  6347. };
  6348. /**
  6349. * Parses out inband captions from an MP4 container and returns
  6350. * caption objects that can be used by WebVTT and the TextTrack API.
  6351. * @see https://developer.mozilla.org/en-US/docs/Web/API/VTTCue
  6352. * @see https://developer.mozilla.org/en-US/docs/Web/API/TextTrack
  6353. * Assumes that `probe.getVideoTrackIds` and `probe.timescale` have been called first
  6354. *
  6355. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  6356. * @param {Number} trackId - The id of the video track to parse
  6357. * @param {Number} timescale - The timescale for the video track from the init segment
  6358. *
  6359. * @return {?Object[]} parsedCaptions - A list of captions or null if no video tracks
  6360. * @return {Number} parsedCaptions[].startTime - The time to show the caption in seconds
  6361. * @return {Number} parsedCaptions[].endTime - The time to stop showing the caption in seconds
  6362. * @return {String} parsedCaptions[].text - The visible content of the caption
  6363. **/
  6364. var parseEmbeddedCaptions = function parseEmbeddedCaptions(segment, trackId, timescale) {
  6365. var captionNals; // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  6366. if (trackId === null) {
  6367. return null;
  6368. }
  6369. captionNals = parseCaptionNals(segment, trackId);
  6370. var trackNals = captionNals[trackId] || {};
  6371. return {
  6372. seiNals: trackNals.seiNals,
  6373. logs: trackNals.logs,
  6374. timescale: timescale
  6375. };
  6376. };
  6377. /**
  6378. * Converts SEI NALUs into captions that can be used by video.js
  6379. **/
  6380. var CaptionParser = function CaptionParser() {
  6381. var isInitialized = false;
  6382. var captionStream; // Stores segments seen before trackId and timescale are set
  6383. var segmentCache; // Stores video track ID of the track being parsed
  6384. var trackId; // Stores the timescale of the track being parsed
  6385. var timescale; // Stores captions parsed so far
  6386. var parsedCaptions; // Stores whether we are receiving partial data or not
  6387. var parsingPartial;
  6388. /**
  6389. * A method to indicate whether a CaptionParser has been initalized
  6390. * @returns {Boolean}
  6391. **/
  6392. this.isInitialized = function () {
  6393. return isInitialized;
  6394. };
  6395. /**
  6396. * Initializes the underlying CaptionStream, SEI NAL parsing
  6397. * and management, and caption collection
  6398. **/
  6399. this.init = function (options) {
  6400. captionStream = new CaptionStream();
  6401. isInitialized = true;
  6402. parsingPartial = options ? options.isPartial : false; // Collect dispatched captions
  6403. captionStream.on('data', function (event) {
  6404. // Convert to seconds in the source's timescale
  6405. event.startTime = event.startPts / timescale;
  6406. event.endTime = event.endPts / timescale;
  6407. parsedCaptions.captions.push(event);
  6408. parsedCaptions.captionStreams[event.stream] = true;
  6409. });
  6410. captionStream.on('log', function (log) {
  6411. parsedCaptions.logs.push(log);
  6412. });
  6413. };
  6414. /**
  6415. * Determines if a new video track will be selected
  6416. * or if the timescale changed
  6417. * @return {Boolean}
  6418. **/
  6419. this.isNewInit = function (videoTrackIds, timescales) {
  6420. if (videoTrackIds && videoTrackIds.length === 0 || timescales && typeof timescales === 'object' && Object.keys(timescales).length === 0) {
  6421. return false;
  6422. }
  6423. return trackId !== videoTrackIds[0] || timescale !== timescales[trackId];
  6424. };
  6425. /**
  6426. * Parses out SEI captions and interacts with underlying
  6427. * CaptionStream to return dispatched captions
  6428. *
  6429. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  6430. * @param {Number[]} videoTrackIds - A list of video tracks found in the init segment
  6431. * @param {Object.<Number, Number>} timescales - The timescales found in the init segment
  6432. * @see parseEmbeddedCaptions
  6433. * @see m2ts/caption-stream.js
  6434. **/
  6435. this.parse = function (segment, videoTrackIds, timescales) {
  6436. var parsedData;
  6437. if (!this.isInitialized()) {
  6438. return null; // This is not likely to be a video segment
  6439. } else if (!videoTrackIds || !timescales) {
  6440. return null;
  6441. } else if (this.isNewInit(videoTrackIds, timescales)) {
  6442. // Use the first video track only as there is no
  6443. // mechanism to switch to other video tracks
  6444. trackId = videoTrackIds[0];
  6445. timescale = timescales[trackId]; // If an init segment has not been seen yet, hold onto segment
  6446. // data until we have one.
  6447. // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  6448. } else if (trackId === null || !timescale) {
  6449. segmentCache.push(segment);
  6450. return null;
  6451. } // Now that a timescale and trackId is set, parse cached segments
  6452. while (segmentCache.length > 0) {
  6453. var cachedSegment = segmentCache.shift();
  6454. this.parse(cachedSegment, videoTrackIds, timescales);
  6455. }
  6456. parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
  6457. if (parsedData && parsedData.logs) {
  6458. parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
  6459. }
  6460. if (parsedData === null || !parsedData.seiNals) {
  6461. if (parsedCaptions.logs.length) {
  6462. return {
  6463. logs: parsedCaptions.logs,
  6464. captions: [],
  6465. captionStreams: []
  6466. };
  6467. }
  6468. return null;
  6469. }
  6470. this.pushNals(parsedData.seiNals); // Force the parsed captions to be dispatched
  6471. this.flushStream();
  6472. return parsedCaptions;
  6473. };
  6474. /**
  6475. * Pushes SEI NALUs onto CaptionStream
  6476. * @param {Object[]} nals - A list of SEI nals parsed using `parseCaptionNals`
  6477. * Assumes that `parseCaptionNals` has been called first
  6478. * @see m2ts/caption-stream.js
  6479. **/
  6480. this.pushNals = function (nals) {
  6481. if (!this.isInitialized() || !nals || nals.length === 0) {
  6482. return null;
  6483. }
  6484. nals.forEach(function (nal) {
  6485. captionStream.push(nal);
  6486. });
  6487. };
  6488. /**
  6489. * Flushes underlying CaptionStream to dispatch processed, displayable captions
  6490. * @see m2ts/caption-stream.js
  6491. **/
  6492. this.flushStream = function () {
  6493. if (!this.isInitialized()) {
  6494. return null;
  6495. }
  6496. if (!parsingPartial) {
  6497. captionStream.flush();
  6498. } else {
  6499. captionStream.partialFlush();
  6500. }
  6501. };
  6502. /**
  6503. * Reset caption buckets for new data
  6504. **/
  6505. this.clearParsedCaptions = function () {
  6506. parsedCaptions.captions = [];
  6507. parsedCaptions.captionStreams = {};
  6508. parsedCaptions.logs = [];
  6509. };
  6510. /**
  6511. * Resets underlying CaptionStream
  6512. * @see m2ts/caption-stream.js
  6513. **/
  6514. this.resetCaptionStream = function () {
  6515. if (!this.isInitialized()) {
  6516. return null;
  6517. }
  6518. captionStream.reset();
  6519. };
  6520. /**
  6521. * Convenience method to clear all captions flushed from the
  6522. * CaptionStream and still being parsed
  6523. * @see m2ts/caption-stream.js
  6524. **/
  6525. this.clearAllCaptions = function () {
  6526. this.clearParsedCaptions();
  6527. this.resetCaptionStream();
  6528. };
  6529. /**
  6530. * Reset caption parser
  6531. **/
  6532. this.reset = function () {
  6533. segmentCache = [];
  6534. trackId = null;
  6535. timescale = null;
  6536. if (!parsedCaptions) {
  6537. parsedCaptions = {
  6538. captions: [],
  6539. // CC1, CC2, CC3, CC4
  6540. captionStreams: {},
  6541. logs: []
  6542. };
  6543. } else {
  6544. this.clearParsedCaptions();
  6545. }
  6546. this.resetCaptionStream();
  6547. };
  6548. this.reset();
  6549. };
  6550. var captionParser = CaptionParser;
  6551. /**
  6552. * mux.js
  6553. *
  6554. * Copyright (c) Brightcove
  6555. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6556. */
  6557. var mp4 = {
  6558. generator: mp4Generator,
  6559. probe: probe$2,
  6560. Transmuxer: transmuxer$2.Transmuxer,
  6561. AudioSegmentStream: transmuxer$2.AudioSegmentStream,
  6562. VideoSegmentStream: transmuxer$2.VideoSegmentStream,
  6563. CaptionParser: captionParser
  6564. };
  6565. /**
  6566. * mux.js
  6567. *
  6568. * Copyright (c) Brightcove
  6569. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6570. *
  6571. * An object that stores the bytes of an FLV tag and methods for
  6572. * querying and manipulating that data.
  6573. * @see http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf
  6574. */
  6575. var _FlvTag; // (type:uint, extraData:Boolean = false) extends ByteArray
  6576. _FlvTag = function FlvTag(type, extraData) {
  6577. var // Counter if this is a metadata tag, nal start marker if this is a video
  6578. // tag. unused if this is an audio tag
  6579. adHoc = 0,
  6580. // :uint
  6581. // The default size is 16kb but this is not enough to hold iframe
  6582. // data and the resizing algorithm costs a bit so we create a larger
  6583. // starting buffer for video tags
  6584. bufferStartSize = 16384,
  6585. // checks whether the FLV tag has enough capacity to accept the proposed
  6586. // write and re-allocates the internal buffers if necessary
  6587. prepareWrite = function prepareWrite(flv, count) {
  6588. var bytes,
  6589. minLength = flv.position + count;
  6590. if (minLength < flv.bytes.byteLength) {
  6591. // there's enough capacity so do nothing
  6592. return;
  6593. } // allocate a new buffer and copy over the data that will not be modified
  6594. bytes = new Uint8Array(minLength * 2);
  6595. bytes.set(flv.bytes.subarray(0, flv.position), 0);
  6596. flv.bytes = bytes;
  6597. flv.view = new DataView(flv.bytes.buffer);
  6598. },
  6599. // commonly used metadata properties
  6600. widthBytes = _FlvTag.widthBytes || new Uint8Array('width'.length),
  6601. heightBytes = _FlvTag.heightBytes || new Uint8Array('height'.length),
  6602. videocodecidBytes = _FlvTag.videocodecidBytes || new Uint8Array('videocodecid'.length),
  6603. i;
  6604. if (!_FlvTag.widthBytes) {
  6605. // calculating the bytes of common metadata names ahead of time makes the
  6606. // corresponding writes faster because we don't have to loop over the
  6607. // characters
  6608. // re-test with test/perf.html if you're planning on changing this
  6609. for (i = 0; i < 'width'.length; i++) {
  6610. widthBytes[i] = 'width'.charCodeAt(i);
  6611. }
  6612. for (i = 0; i < 'height'.length; i++) {
  6613. heightBytes[i] = 'height'.charCodeAt(i);
  6614. }
  6615. for (i = 0; i < 'videocodecid'.length; i++) {
  6616. videocodecidBytes[i] = 'videocodecid'.charCodeAt(i);
  6617. }
  6618. _FlvTag.widthBytes = widthBytes;
  6619. _FlvTag.heightBytes = heightBytes;
  6620. _FlvTag.videocodecidBytes = videocodecidBytes;
  6621. }
  6622. this.keyFrame = false; // :Boolean
  6623. switch (type) {
  6624. case _FlvTag.VIDEO_TAG:
  6625. this.length = 16; // Start the buffer at 256k
  6626. bufferStartSize *= 6;
  6627. break;
  6628. case _FlvTag.AUDIO_TAG:
  6629. this.length = 13;
  6630. this.keyFrame = true;
  6631. break;
  6632. case _FlvTag.METADATA_TAG:
  6633. this.length = 29;
  6634. this.keyFrame = true;
  6635. break;
  6636. default:
  6637. throw new Error('Unknown FLV tag type');
  6638. }
  6639. this.bytes = new Uint8Array(bufferStartSize);
  6640. this.view = new DataView(this.bytes.buffer);
  6641. this.bytes[0] = type;
  6642. this.position = this.length;
  6643. this.keyFrame = extraData; // Defaults to false
  6644. // presentation timestamp
  6645. this.pts = 0; // decoder timestamp
  6646. this.dts = 0; // ByteArray#writeBytes(bytes:ByteArray, offset:uint = 0, length:uint = 0)
  6647. this.writeBytes = function (bytes, offset, length) {
  6648. var start = offset || 0,
  6649. end;
  6650. length = length || bytes.byteLength;
  6651. end = start + length;
  6652. prepareWrite(this, length);
  6653. this.bytes.set(bytes.subarray(start, end), this.position);
  6654. this.position += length;
  6655. this.length = Math.max(this.length, this.position);
  6656. }; // ByteArray#writeByte(value:int):void
  6657. this.writeByte = function (byte) {
  6658. prepareWrite(this, 1);
  6659. this.bytes[this.position] = byte;
  6660. this.position++;
  6661. this.length = Math.max(this.length, this.position);
  6662. }; // ByteArray#writeShort(value:int):void
  6663. this.writeShort = function (short) {
  6664. prepareWrite(this, 2);
  6665. this.view.setUint16(this.position, short);
  6666. this.position += 2;
  6667. this.length = Math.max(this.length, this.position);
  6668. }; // Negative index into array
  6669. // (pos:uint):int
  6670. this.negIndex = function (pos) {
  6671. return this.bytes[this.length - pos];
  6672. }; // The functions below ONLY work when this[0] == VIDEO_TAG.
  6673. // We are not going to check for that because we dont want the overhead
  6674. // (nal:ByteArray = null):int
  6675. this.nalUnitSize = function () {
  6676. if (adHoc === 0) {
  6677. return 0;
  6678. }
  6679. return this.length - (adHoc + 4);
  6680. };
  6681. this.startNalUnit = function () {
  6682. // remember position and add 4 bytes
  6683. if (adHoc > 0) {
  6684. throw new Error('Attempted to create new NAL wihout closing the old one');
  6685. } // reserve 4 bytes for nal unit size
  6686. adHoc = this.length;
  6687. this.length += 4;
  6688. this.position = this.length;
  6689. }; // (nal:ByteArray = null):void
  6690. this.endNalUnit = function (nalContainer) {
  6691. var nalStart, // :uint
  6692. nalLength; // :uint
  6693. // Rewind to the marker and write the size
  6694. if (this.length === adHoc + 4) {
  6695. // we started a nal unit, but didnt write one, so roll back the 4 byte size value
  6696. this.length -= 4;
  6697. } else if (adHoc > 0) {
  6698. nalStart = adHoc + 4;
  6699. nalLength = this.length - nalStart;
  6700. this.position = adHoc;
  6701. this.view.setUint32(this.position, nalLength);
  6702. this.position = this.length;
  6703. if (nalContainer) {
  6704. // Add the tag to the NAL unit
  6705. nalContainer.push(this.bytes.subarray(nalStart, nalStart + nalLength));
  6706. }
  6707. }
  6708. adHoc = 0;
  6709. };
  6710. /**
  6711. * Write out a 64-bit floating point valued metadata property. This method is
  6712. * called frequently during a typical parse and needs to be fast.
  6713. */
  6714. // (key:String, val:Number):void
  6715. this.writeMetaDataDouble = function (key, val) {
  6716. var i;
  6717. prepareWrite(this, 2 + key.length + 9); // write size of property name
  6718. this.view.setUint16(this.position, key.length);
  6719. this.position += 2; // this next part looks terrible but it improves parser throughput by
  6720. // 10kB/s in my testing
  6721. // write property name
  6722. if (key === 'width') {
  6723. this.bytes.set(widthBytes, this.position);
  6724. this.position += 5;
  6725. } else if (key === 'height') {
  6726. this.bytes.set(heightBytes, this.position);
  6727. this.position += 6;
  6728. } else if (key === 'videocodecid') {
  6729. this.bytes.set(videocodecidBytes, this.position);
  6730. this.position += 12;
  6731. } else {
  6732. for (i = 0; i < key.length; i++) {
  6733. this.bytes[this.position] = key.charCodeAt(i);
  6734. this.position++;
  6735. }
  6736. } // skip null byte
  6737. this.position++; // write property value
  6738. this.view.setFloat64(this.position, val);
  6739. this.position += 8; // update flv tag length
  6740. this.length = Math.max(this.length, this.position);
  6741. ++adHoc;
  6742. }; // (key:String, val:Boolean):void
  6743. this.writeMetaDataBoolean = function (key, val) {
  6744. var i;
  6745. prepareWrite(this, 2);
  6746. this.view.setUint16(this.position, key.length);
  6747. this.position += 2;
  6748. for (i = 0; i < key.length; i++) {
  6749. // if key.charCodeAt(i) >= 255, handle error
  6750. prepareWrite(this, 1);
  6751. this.bytes[this.position] = key.charCodeAt(i);
  6752. this.position++;
  6753. }
  6754. prepareWrite(this, 2);
  6755. this.view.setUint8(this.position, 0x01);
  6756. this.position++;
  6757. this.view.setUint8(this.position, val ? 0x01 : 0x00);
  6758. this.position++;
  6759. this.length = Math.max(this.length, this.position);
  6760. ++adHoc;
  6761. }; // ():ByteArray
  6762. this.finalize = function () {
  6763. var dtsDelta, // :int
  6764. len; // :int
  6765. switch (this.bytes[0]) {
  6766. // Video Data
  6767. case _FlvTag.VIDEO_TAG:
  6768. // We only support AVC, 1 = key frame (for AVC, a seekable
  6769. // frame), 2 = inter frame (for AVC, a non-seekable frame)
  6770. this.bytes[11] = (this.keyFrame || extraData ? 0x10 : 0x20) | 0x07;
  6771. this.bytes[12] = extraData ? 0x00 : 0x01;
  6772. dtsDelta = this.pts - this.dts;
  6773. this.bytes[13] = (dtsDelta & 0x00FF0000) >>> 16;
  6774. this.bytes[14] = (dtsDelta & 0x0000FF00) >>> 8;
  6775. this.bytes[15] = (dtsDelta & 0x000000FF) >>> 0;
  6776. break;
  6777. case _FlvTag.AUDIO_TAG:
  6778. this.bytes[11] = 0xAF; // 44 kHz, 16-bit stereo
  6779. this.bytes[12] = extraData ? 0x00 : 0x01;
  6780. break;
  6781. case _FlvTag.METADATA_TAG:
  6782. this.position = 11;
  6783. this.view.setUint8(this.position, 0x02); // String type
  6784. this.position++;
  6785. this.view.setUint16(this.position, 0x0A); // 10 Bytes
  6786. this.position += 2; // set "onMetaData"
  6787. this.bytes.set([0x6f, 0x6e, 0x4d, 0x65, 0x74, 0x61, 0x44, 0x61, 0x74, 0x61], this.position);
  6788. this.position += 10;
  6789. this.bytes[this.position] = 0x08; // Array type
  6790. this.position++;
  6791. this.view.setUint32(this.position, adHoc);
  6792. this.position = this.length;
  6793. this.bytes.set([0, 0, 9], this.position);
  6794. this.position += 3; // End Data Tag
  6795. this.length = this.position;
  6796. break;
  6797. }
  6798. len = this.length - 11; // write the DataSize field
  6799. this.bytes[1] = (len & 0x00FF0000) >>> 16;
  6800. this.bytes[2] = (len & 0x0000FF00) >>> 8;
  6801. this.bytes[3] = (len & 0x000000FF) >>> 0; // write the Timestamp
  6802. this.bytes[4] = (this.dts & 0x00FF0000) >>> 16;
  6803. this.bytes[5] = (this.dts & 0x0000FF00) >>> 8;
  6804. this.bytes[6] = (this.dts & 0x000000FF) >>> 0;
  6805. this.bytes[7] = (this.dts & 0xFF000000) >>> 24; // write the StreamID
  6806. this.bytes[8] = 0;
  6807. this.bytes[9] = 0;
  6808. this.bytes[10] = 0; // Sometimes we're at the end of the view and have one slot to write a
  6809. // uint32, so, prepareWrite of count 4, since, view is uint8
  6810. prepareWrite(this, 4);
  6811. this.view.setUint32(this.length, this.length);
  6812. this.length += 4;
  6813. this.position += 4; // trim down the byte buffer to what is actually being used
  6814. this.bytes = this.bytes.subarray(0, this.length);
  6815. this.frameTime = _FlvTag.frameTime(this.bytes); // if bytes.bytelength isn't equal to this.length, handle error
  6816. return this;
  6817. };
  6818. };
  6819. _FlvTag.AUDIO_TAG = 0x08; // == 8, :uint
  6820. _FlvTag.VIDEO_TAG = 0x09; // == 9, :uint
  6821. _FlvTag.METADATA_TAG = 0x12; // == 18, :uint
  6822. // (tag:ByteArray):Boolean {
  6823. _FlvTag.isAudioFrame = function (tag) {
  6824. return _FlvTag.AUDIO_TAG === tag[0];
  6825. }; // (tag:ByteArray):Boolean {
  6826. _FlvTag.isVideoFrame = function (tag) {
  6827. return _FlvTag.VIDEO_TAG === tag[0];
  6828. }; // (tag:ByteArray):Boolean {
  6829. _FlvTag.isMetaData = function (tag) {
  6830. return _FlvTag.METADATA_TAG === tag[0];
  6831. }; // (tag:ByteArray):Boolean {
  6832. _FlvTag.isKeyFrame = function (tag) {
  6833. if (_FlvTag.isVideoFrame(tag)) {
  6834. return tag[11] === 0x17;
  6835. }
  6836. if (_FlvTag.isAudioFrame(tag)) {
  6837. return true;
  6838. }
  6839. if (_FlvTag.isMetaData(tag)) {
  6840. return true;
  6841. }
  6842. return false;
  6843. }; // (tag:ByteArray):uint {
  6844. _FlvTag.frameTime = function (tag) {
  6845. var pts = tag[4] << 16; // :uint
  6846. pts |= tag[5] << 8;
  6847. pts |= tag[6] << 0;
  6848. pts |= tag[7] << 24;
  6849. return pts;
  6850. };
  6851. var flvTag = _FlvTag;
  6852. /**
  6853. * The final stage of the transmuxer that emits the flv tags
  6854. * for audio, video, and metadata. Also tranlates in time and
  6855. * outputs caption data and id3 cues.
  6856. */
  6857. var CoalesceStream = function CoalesceStream(options) {
  6858. // Number of Tracks per output segment
  6859. // If greater than 1, we combine multiple
  6860. // tracks into a single segment
  6861. this.numberOfTracks = 0;
  6862. this.metadataStream = options.metadataStream;
  6863. this.videoTags = [];
  6864. this.audioTags = [];
  6865. this.videoTrack = null;
  6866. this.audioTrack = null;
  6867. this.pendingCaptions = [];
  6868. this.pendingMetadata = [];
  6869. this.pendingTracks = 0;
  6870. this.processedTracks = 0;
  6871. CoalesceStream.prototype.init.call(this); // Take output from multiple
  6872. this.push = function (output) {
  6873. // buffer incoming captions until the associated video segment
  6874. // finishes
  6875. if (output.text) {
  6876. return this.pendingCaptions.push(output);
  6877. } // buffer incoming id3 tags until the final flush
  6878. if (output.frames) {
  6879. return this.pendingMetadata.push(output);
  6880. }
  6881. if (output.track.type === 'video') {
  6882. this.videoTrack = output.track;
  6883. this.videoTags = output.tags;
  6884. this.pendingTracks++;
  6885. }
  6886. if (output.track.type === 'audio') {
  6887. this.audioTrack = output.track;
  6888. this.audioTags = output.tags;
  6889. this.pendingTracks++;
  6890. }
  6891. };
  6892. };
  6893. CoalesceStream.prototype = new stream();
  6894. CoalesceStream.prototype.flush = function (flushSource) {
  6895. var id3,
  6896. caption,
  6897. i,
  6898. timelineStartPts,
  6899. event = {
  6900. tags: {},
  6901. captions: [],
  6902. captionStreams: {},
  6903. metadata: []
  6904. };
  6905. if (this.pendingTracks < this.numberOfTracks) {
  6906. if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
  6907. // Return because we haven't received a flush from a data-generating
  6908. // portion of the segment (meaning that we have only recieved meta-data
  6909. // or captions.)
  6910. return;
  6911. } else if (this.pendingTracks === 0) {
  6912. // In the case where we receive a flush without any data having been
  6913. // received we consider it an emitted track for the purposes of coalescing
  6914. // `done` events.
  6915. // We do this for the case where there is an audio and video track in the
  6916. // segment but no audio data. (seen in several playlists with alternate
  6917. // audio tracks and no audio present in the main TS segments.)
  6918. this.processedTracks++;
  6919. if (this.processedTracks < this.numberOfTracks) {
  6920. return;
  6921. }
  6922. }
  6923. }
  6924. this.processedTracks += this.pendingTracks;
  6925. this.pendingTracks = 0;
  6926. if (this.processedTracks < this.numberOfTracks) {
  6927. return;
  6928. }
  6929. if (this.videoTrack) {
  6930. timelineStartPts = this.videoTrack.timelineStartInfo.pts;
  6931. } else if (this.audioTrack) {
  6932. timelineStartPts = this.audioTrack.timelineStartInfo.pts;
  6933. }
  6934. event.tags.videoTags = this.videoTags;
  6935. event.tags.audioTags = this.audioTags; // Translate caption PTS times into second offsets into the
  6936. // video timeline for the segment, and add track info
  6937. for (i = 0; i < this.pendingCaptions.length; i++) {
  6938. caption = this.pendingCaptions[i];
  6939. caption.startTime = caption.startPts - timelineStartPts;
  6940. caption.startTime /= 90e3;
  6941. caption.endTime = caption.endPts - timelineStartPts;
  6942. caption.endTime /= 90e3;
  6943. event.captionStreams[caption.stream] = true;
  6944. event.captions.push(caption);
  6945. } // Translate ID3 frame PTS times into second offsets into the
  6946. // video timeline for the segment
  6947. for (i = 0; i < this.pendingMetadata.length; i++) {
  6948. id3 = this.pendingMetadata[i];
  6949. id3.cueTime = id3.pts - timelineStartPts;
  6950. id3.cueTime /= 90e3;
  6951. event.metadata.push(id3);
  6952. } // We add this to every single emitted segment even though we only need
  6953. // it for the first
  6954. event.metadata.dispatchType = this.metadataStream.dispatchType; // Reset stream state
  6955. this.videoTrack = null;
  6956. this.audioTrack = null;
  6957. this.videoTags = [];
  6958. this.audioTags = [];
  6959. this.pendingCaptions.length = 0;
  6960. this.pendingMetadata.length = 0;
  6961. this.pendingTracks = 0;
  6962. this.processedTracks = 0; // Emit the final segment
  6963. this.trigger('data', event);
  6964. this.trigger('done');
  6965. };
  6966. var coalesceStream = CoalesceStream;
  6967. /**
  6968. * mux.js
  6969. *
  6970. * Copyright (c) Brightcove
  6971. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6972. */
  6973. var TagList = function TagList() {
  6974. var self = this;
  6975. this.list = [];
  6976. this.push = function (tag) {
  6977. this.list.push({
  6978. bytes: tag.bytes,
  6979. dts: tag.dts,
  6980. pts: tag.pts,
  6981. keyFrame: tag.keyFrame,
  6982. metaDataTag: tag.metaDataTag
  6983. });
  6984. };
  6985. Object.defineProperty(this, 'length', {
  6986. get: function get() {
  6987. return self.list.length;
  6988. }
  6989. });
  6990. };
  6991. var tagList = TagList;
  6992. var H264Stream = h264.H264Stream;
  6993. var _Transmuxer, _VideoSegmentStream, _AudioSegmentStream, collectTimelineInfo, metaDataTag, extraDataTag;
  6994. /**
  6995. * Store information about the start and end of the tracka and the
  6996. * duration for each frame/sample we process in order to calculate
  6997. * the baseMediaDecodeTime
  6998. */
  6999. collectTimelineInfo = function collectTimelineInfo(track, data) {
  7000. if (typeof data.pts === 'number') {
  7001. if (track.timelineStartInfo.pts === undefined) {
  7002. track.timelineStartInfo.pts = data.pts;
  7003. } else {
  7004. track.timelineStartInfo.pts = Math.min(track.timelineStartInfo.pts, data.pts);
  7005. }
  7006. }
  7007. if (typeof data.dts === 'number') {
  7008. if (track.timelineStartInfo.dts === undefined) {
  7009. track.timelineStartInfo.dts = data.dts;
  7010. } else {
  7011. track.timelineStartInfo.dts = Math.min(track.timelineStartInfo.dts, data.dts);
  7012. }
  7013. }
  7014. };
  7015. metaDataTag = function metaDataTag(track, pts) {
  7016. var tag = new flvTag(flvTag.METADATA_TAG); // :FlvTag
  7017. tag.dts = pts;
  7018. tag.pts = pts;
  7019. tag.writeMetaDataDouble('videocodecid', 7);
  7020. tag.writeMetaDataDouble('width', track.width);
  7021. tag.writeMetaDataDouble('height', track.height);
  7022. return tag;
  7023. };
  7024. extraDataTag = function extraDataTag(track, pts) {
  7025. var i,
  7026. tag = new flvTag(flvTag.VIDEO_TAG, true);
  7027. tag.dts = pts;
  7028. tag.pts = pts;
  7029. tag.writeByte(0x01); // version
  7030. tag.writeByte(track.profileIdc); // profile
  7031. tag.writeByte(track.profileCompatibility); // compatibility
  7032. tag.writeByte(track.levelIdc); // level
  7033. tag.writeByte(0xFC | 0x03); // reserved (6 bits), NULA length size - 1 (2 bits)
  7034. tag.writeByte(0xE0 | 0x01); // reserved (3 bits), num of SPS (5 bits)
  7035. tag.writeShort(track.sps[0].length); // data of SPS
  7036. tag.writeBytes(track.sps[0]); // SPS
  7037. tag.writeByte(track.pps.length); // num of PPS (will there ever be more that 1 PPS?)
  7038. for (i = 0; i < track.pps.length; ++i) {
  7039. tag.writeShort(track.pps[i].length); // 2 bytes for length of PPS
  7040. tag.writeBytes(track.pps[i]); // data of PPS
  7041. }
  7042. return tag;
  7043. };
  7044. /**
  7045. * Constructs a single-track, media segment from AAC data
  7046. * events. The output of this stream can be fed to flash.
  7047. */
  7048. _AudioSegmentStream = function AudioSegmentStream(track) {
  7049. var adtsFrames = [],
  7050. videoKeyFrames = [],
  7051. oldExtraData;
  7052. _AudioSegmentStream.prototype.init.call(this);
  7053. this.push = function (data) {
  7054. collectTimelineInfo(track, data);
  7055. if (track) {
  7056. track.audioobjecttype = data.audioobjecttype;
  7057. track.channelcount = data.channelcount;
  7058. track.samplerate = data.samplerate;
  7059. track.samplingfrequencyindex = data.samplingfrequencyindex;
  7060. track.samplesize = data.samplesize;
  7061. track.extraData = track.audioobjecttype << 11 | track.samplingfrequencyindex << 7 | track.channelcount << 3;
  7062. }
  7063. data.pts = Math.round(data.pts / 90);
  7064. data.dts = Math.round(data.dts / 90); // buffer audio data until end() is called
  7065. adtsFrames.push(data);
  7066. };
  7067. this.flush = function () {
  7068. var currentFrame,
  7069. adtsFrame,
  7070. lastMetaPts,
  7071. tags = new tagList(); // return early if no audio data has been observed
  7072. if (adtsFrames.length === 0) {
  7073. this.trigger('done', 'AudioSegmentStream');
  7074. return;
  7075. }
  7076. lastMetaPts = -Infinity;
  7077. while (adtsFrames.length) {
  7078. currentFrame = adtsFrames.shift(); // write out a metadata frame at every video key frame
  7079. if (videoKeyFrames.length && currentFrame.pts >= videoKeyFrames[0]) {
  7080. lastMetaPts = videoKeyFrames.shift();
  7081. this.writeMetaDataTags(tags, lastMetaPts);
  7082. } // also write out metadata tags every 1 second so that the decoder
  7083. // is re-initialized quickly after seeking into a different
  7084. // audio configuration.
  7085. if (track.extraData !== oldExtraData || currentFrame.pts - lastMetaPts >= 1000) {
  7086. this.writeMetaDataTags(tags, currentFrame.pts);
  7087. oldExtraData = track.extraData;
  7088. lastMetaPts = currentFrame.pts;
  7089. }
  7090. adtsFrame = new flvTag(flvTag.AUDIO_TAG);
  7091. adtsFrame.pts = currentFrame.pts;
  7092. adtsFrame.dts = currentFrame.dts;
  7093. adtsFrame.writeBytes(currentFrame.data);
  7094. tags.push(adtsFrame.finalize());
  7095. }
  7096. videoKeyFrames.length = 0;
  7097. oldExtraData = null;
  7098. this.trigger('data', {
  7099. track: track,
  7100. tags: tags.list
  7101. });
  7102. this.trigger('done', 'AudioSegmentStream');
  7103. };
  7104. this.writeMetaDataTags = function (tags, pts) {
  7105. var adtsFrame;
  7106. adtsFrame = new flvTag(flvTag.METADATA_TAG); // For audio, DTS is always the same as PTS. We want to set the DTS
  7107. // however so we can compare with video DTS to determine approximate
  7108. // packet order
  7109. adtsFrame.pts = pts;
  7110. adtsFrame.dts = pts; // AAC is always 10
  7111. adtsFrame.writeMetaDataDouble('audiocodecid', 10);
  7112. adtsFrame.writeMetaDataBoolean('stereo', track.channelcount === 2);
  7113. adtsFrame.writeMetaDataDouble('audiosamplerate', track.samplerate); // Is AAC always 16 bit?
  7114. adtsFrame.writeMetaDataDouble('audiosamplesize', 16);
  7115. tags.push(adtsFrame.finalize());
  7116. adtsFrame = new flvTag(flvTag.AUDIO_TAG, true); // For audio, DTS is always the same as PTS. We want to set the DTS
  7117. // however so we can compare with video DTS to determine approximate
  7118. // packet order
  7119. adtsFrame.pts = pts;
  7120. adtsFrame.dts = pts;
  7121. adtsFrame.view.setUint16(adtsFrame.position, track.extraData);
  7122. adtsFrame.position += 2;
  7123. adtsFrame.length = Math.max(adtsFrame.length, adtsFrame.position);
  7124. tags.push(adtsFrame.finalize());
  7125. };
  7126. this.onVideoKeyFrame = function (pts) {
  7127. videoKeyFrames.push(pts);
  7128. };
  7129. };
  7130. _AudioSegmentStream.prototype = new stream();
  7131. /**
  7132. * Store FlvTags for the h264 stream
  7133. * @param track {object} track metadata configuration
  7134. */
  7135. _VideoSegmentStream = function VideoSegmentStream(track) {
  7136. var nalUnits = [],
  7137. config,
  7138. h264Frame;
  7139. _VideoSegmentStream.prototype.init.call(this);
  7140. this.finishFrame = function (tags, frame) {
  7141. if (!frame) {
  7142. return;
  7143. } // Check if keyframe and the length of tags.
  7144. // This makes sure we write metadata on the first frame of a segment.
  7145. if (config && track && track.newMetadata && (frame.keyFrame || tags.length === 0)) {
  7146. // Push extra data on every IDR frame in case we did a stream change + seek
  7147. var metaTag = metaDataTag(config, frame.dts).finalize();
  7148. var extraTag = extraDataTag(track, frame.dts).finalize();
  7149. metaTag.metaDataTag = extraTag.metaDataTag = true;
  7150. tags.push(metaTag);
  7151. tags.push(extraTag);
  7152. track.newMetadata = false;
  7153. this.trigger('keyframe', frame.dts);
  7154. }
  7155. frame.endNalUnit();
  7156. tags.push(frame.finalize());
  7157. h264Frame = null;
  7158. };
  7159. this.push = function (data) {
  7160. collectTimelineInfo(track, data);
  7161. data.pts = Math.round(data.pts / 90);
  7162. data.dts = Math.round(data.dts / 90); // buffer video until flush() is called
  7163. nalUnits.push(data);
  7164. };
  7165. this.flush = function () {
  7166. var currentNal,
  7167. tags = new tagList(); // Throw away nalUnits at the start of the byte stream until we find
  7168. // the first AUD
  7169. while (nalUnits.length) {
  7170. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  7171. break;
  7172. }
  7173. nalUnits.shift();
  7174. } // return early if no video data has been observed
  7175. if (nalUnits.length === 0) {
  7176. this.trigger('done', 'VideoSegmentStream');
  7177. return;
  7178. }
  7179. while (nalUnits.length) {
  7180. currentNal = nalUnits.shift(); // record the track config
  7181. if (currentNal.nalUnitType === 'seq_parameter_set_rbsp') {
  7182. track.newMetadata = true;
  7183. config = currentNal.config;
  7184. track.width = config.width;
  7185. track.height = config.height;
  7186. track.sps = [currentNal.data];
  7187. track.profileIdc = config.profileIdc;
  7188. track.levelIdc = config.levelIdc;
  7189. track.profileCompatibility = config.profileCompatibility;
  7190. h264Frame.endNalUnit();
  7191. } else if (currentNal.nalUnitType === 'pic_parameter_set_rbsp') {
  7192. track.newMetadata = true;
  7193. track.pps = [currentNal.data];
  7194. h264Frame.endNalUnit();
  7195. } else if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
  7196. if (h264Frame) {
  7197. this.finishFrame(tags, h264Frame);
  7198. }
  7199. h264Frame = new flvTag(flvTag.VIDEO_TAG);
  7200. h264Frame.pts = currentNal.pts;
  7201. h264Frame.dts = currentNal.dts;
  7202. } else {
  7203. if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
  7204. // the current sample is a key frame
  7205. h264Frame.keyFrame = true;
  7206. }
  7207. h264Frame.endNalUnit();
  7208. }
  7209. h264Frame.startNalUnit();
  7210. h264Frame.writeBytes(currentNal.data);
  7211. }
  7212. if (h264Frame) {
  7213. this.finishFrame(tags, h264Frame);
  7214. }
  7215. this.trigger('data', {
  7216. track: track,
  7217. tags: tags.list
  7218. }); // Continue with the flush process now
  7219. this.trigger('done', 'VideoSegmentStream');
  7220. };
  7221. };
  7222. _VideoSegmentStream.prototype = new stream();
  7223. /**
  7224. * An object that incrementally transmuxes MPEG2 Trasport Stream
  7225. * chunks into an FLV.
  7226. */
  7227. _Transmuxer = function Transmuxer(options) {
  7228. var self = this,
  7229. packetStream,
  7230. parseStream,
  7231. elementaryStream,
  7232. videoTimestampRolloverStream,
  7233. audioTimestampRolloverStream,
  7234. timedMetadataTimestampRolloverStream,
  7235. adtsStream,
  7236. h264Stream,
  7237. videoSegmentStream,
  7238. audioSegmentStream,
  7239. captionStream,
  7240. coalesceStream$1;
  7241. _Transmuxer.prototype.init.call(this);
  7242. options = options || {}; // expose the metadata stream
  7243. this.metadataStream = new m2ts_1.MetadataStream();
  7244. options.metadataStream = this.metadataStream; // set up the parsing pipeline
  7245. packetStream = new m2ts_1.TransportPacketStream();
  7246. parseStream = new m2ts_1.TransportParseStream();
  7247. elementaryStream = new m2ts_1.ElementaryStream();
  7248. videoTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('video');
  7249. audioTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('audio');
  7250. timedMetadataTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('timed-metadata');
  7251. adtsStream = new adts();
  7252. h264Stream = new H264Stream();
  7253. coalesceStream$1 = new coalesceStream(options); // disassemble MPEG2-TS packets into elementary streams
  7254. packetStream.pipe(parseStream).pipe(elementaryStream); // !!THIS ORDER IS IMPORTANT!!
  7255. // demux the streams
  7256. elementaryStream.pipe(videoTimestampRolloverStream).pipe(h264Stream);
  7257. elementaryStream.pipe(audioTimestampRolloverStream).pipe(adtsStream);
  7258. elementaryStream.pipe(timedMetadataTimestampRolloverStream).pipe(this.metadataStream).pipe(coalesceStream$1); // if CEA-708 parsing is available, hook up a caption stream
  7259. captionStream = new m2ts_1.CaptionStream(options);
  7260. h264Stream.pipe(captionStream).pipe(coalesceStream$1); // hook up the segment streams once track metadata is delivered
  7261. elementaryStream.on('data', function (data) {
  7262. var i, videoTrack, audioTrack;
  7263. if (data.type === 'metadata') {
  7264. i = data.tracks.length; // scan the tracks listed in the metadata
  7265. while (i--) {
  7266. if (data.tracks[i].type === 'video') {
  7267. videoTrack = data.tracks[i];
  7268. } else if (data.tracks[i].type === 'audio') {
  7269. audioTrack = data.tracks[i];
  7270. }
  7271. } // hook up the video segment stream to the first track with h264 data
  7272. if (videoTrack && !videoSegmentStream) {
  7273. coalesceStream$1.numberOfTracks++;
  7274. videoSegmentStream = new _VideoSegmentStream(videoTrack); // Set up the final part of the video pipeline
  7275. h264Stream.pipe(videoSegmentStream).pipe(coalesceStream$1);
  7276. }
  7277. if (audioTrack && !audioSegmentStream) {
  7278. // hook up the audio segment stream to the first track with aac data
  7279. coalesceStream$1.numberOfTracks++;
  7280. audioSegmentStream = new _AudioSegmentStream(audioTrack); // Set up the final part of the audio pipeline
  7281. adtsStream.pipe(audioSegmentStream).pipe(coalesceStream$1);
  7282. if (videoSegmentStream) {
  7283. videoSegmentStream.on('keyframe', audioSegmentStream.onVideoKeyFrame);
  7284. }
  7285. }
  7286. }
  7287. }); // feed incoming data to the front of the parsing pipeline
  7288. this.push = function (data) {
  7289. packetStream.push(data);
  7290. }; // flush any buffered data
  7291. this.flush = function () {
  7292. // Start at the top of the pipeline and flush all pending work
  7293. packetStream.flush();
  7294. }; // Caption data has to be reset when seeking outside buffered range
  7295. this.resetCaptions = function () {
  7296. captionStream.reset();
  7297. }; // Re-emit any data coming from the coalesce stream to the outside world
  7298. coalesceStream$1.on('data', function (event) {
  7299. self.trigger('data', event);
  7300. }); // Let the consumer know we have finished flushing the entire pipeline
  7301. coalesceStream$1.on('done', function () {
  7302. self.trigger('done');
  7303. });
  7304. };
  7305. _Transmuxer.prototype = new stream(); // forward compatibility
  7306. var transmuxer$1 = _Transmuxer;
  7307. // http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf.
  7308. // Technically, this function returns the header and a metadata FLV tag
  7309. // if duration is greater than zero
  7310. // duration in seconds
  7311. // @return {object} the bytes of the FLV header as a Uint8Array
  7312. var getFlvHeader = function getFlvHeader(duration, audio, video) {
  7313. // :ByteArray {
  7314. var headBytes = new Uint8Array(3 + 1 + 1 + 4),
  7315. head = new DataView(headBytes.buffer),
  7316. metadata,
  7317. result,
  7318. metadataLength; // default arguments
  7319. duration = duration || 0;
  7320. audio = audio === undefined ? true : audio;
  7321. video = video === undefined ? true : video; // signature
  7322. head.setUint8(0, 0x46); // 'F'
  7323. head.setUint8(1, 0x4c); // 'L'
  7324. head.setUint8(2, 0x56); // 'V'
  7325. // version
  7326. head.setUint8(3, 0x01); // flags
  7327. head.setUint8(4, (audio ? 0x04 : 0x00) | (video ? 0x01 : 0x00)); // data offset, should be 9 for FLV v1
  7328. head.setUint32(5, headBytes.byteLength); // init the first FLV tag
  7329. if (duration <= 0) {
  7330. // no duration available so just write the first field of the first
  7331. // FLV tag
  7332. result = new Uint8Array(headBytes.byteLength + 4);
  7333. result.set(headBytes);
  7334. result.set([0, 0, 0, 0], headBytes.byteLength);
  7335. return result;
  7336. } // write out the duration metadata tag
  7337. metadata = new flvTag(flvTag.METADATA_TAG);
  7338. metadata.pts = metadata.dts = 0;
  7339. metadata.writeMetaDataDouble('duration', duration);
  7340. metadataLength = metadata.finalize().length;
  7341. result = new Uint8Array(headBytes.byteLength + metadataLength);
  7342. result.set(headBytes);
  7343. result.set(head.byteLength, metadataLength);
  7344. return result;
  7345. };
  7346. var flvHeader = getFlvHeader;
  7347. /**
  7348. * mux.js
  7349. *
  7350. * Copyright (c) Brightcove
  7351. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7352. */
  7353. var flv = {
  7354. tag: flvTag,
  7355. Transmuxer: transmuxer$1,
  7356. getFlvHeader: flvHeader
  7357. };
  7358. /**
  7359. * mux.js
  7360. *
  7361. * Copyright (c) Brightcove
  7362. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7363. */
  7364. var m2ts = m2ts_1;
  7365. var ONE_SECOND_IN_TS$1 = clock.ONE_SECOND_IN_TS;
  7366. /**
  7367. * Constructs a single-track, ISO BMFF media segment from AAC data
  7368. * events. The output of this stream can be fed to a SourceBuffer
  7369. * configured with a suitable initialization segment.
  7370. */
  7371. var AudioSegmentStream = function AudioSegmentStream(track, options) {
  7372. var adtsFrames = [],
  7373. sequenceNumber = 0,
  7374. earliestAllowedDts = 0,
  7375. audioAppendStartTs = 0,
  7376. videoBaseMediaDecodeTime = Infinity,
  7377. segmentStartPts = null,
  7378. segmentEndPts = null;
  7379. options = options || {};
  7380. AudioSegmentStream.prototype.init.call(this);
  7381. this.push = function (data) {
  7382. trackDecodeInfo.collectDtsInfo(track, data);
  7383. if (track) {
  7384. audioProperties.forEach(function (prop) {
  7385. track[prop] = data[prop];
  7386. });
  7387. } // buffer audio data until end() is called
  7388. adtsFrames.push(data);
  7389. };
  7390. this.setEarliestDts = function (earliestDts) {
  7391. earliestAllowedDts = earliestDts;
  7392. };
  7393. this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  7394. videoBaseMediaDecodeTime = baseMediaDecodeTime;
  7395. };
  7396. this.setAudioAppendStart = function (timestamp) {
  7397. audioAppendStartTs = timestamp;
  7398. };
  7399. this.processFrames_ = function () {
  7400. var frames, moof, mdat, boxes, timingInfo; // return early if no audio data has been observed
  7401. if (adtsFrames.length === 0) {
  7402. return;
  7403. }
  7404. frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
  7405. if (frames.length === 0) {
  7406. // return early if the frames are all after the earliest allowed DTS
  7407. // TODO should we clear the adtsFrames?
  7408. return;
  7409. }
  7410. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
  7411. audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime); // we have to build the index from byte locations to
  7412. // samples (that is, adts frames) in the audio data
  7413. track.samples = audioFrameUtils.generateSampleTable(frames); // concatenate the audio data to constuct the mdat
  7414. mdat = mp4Generator.mdat(audioFrameUtils.concatenateFrameData(frames));
  7415. adtsFrames = [];
  7416. moof = mp4Generator.moof(sequenceNumber, [track]); // bump the sequence number for next time
  7417. sequenceNumber++;
  7418. track.initSegment = mp4Generator.initSegment([track]); // it would be great to allocate this array up front instead of
  7419. // throwing away hundreds of media segment fragments
  7420. boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
  7421. boxes.set(moof);
  7422. boxes.set(mdat, moof.byteLength);
  7423. trackDecodeInfo.clearDtsInfo(track);
  7424. if (segmentStartPts === null) {
  7425. segmentEndPts = segmentStartPts = frames[0].pts;
  7426. }
  7427. segmentEndPts += frames.length * (ONE_SECOND_IN_TS$1 * 1024 / track.samplerate);
  7428. timingInfo = {
  7429. start: segmentStartPts
  7430. };
  7431. this.trigger('timingInfo', timingInfo);
  7432. this.trigger('data', {
  7433. track: track,
  7434. boxes: boxes
  7435. });
  7436. };
  7437. this.flush = function () {
  7438. this.processFrames_(); // trigger final timing info
  7439. this.trigger('timingInfo', {
  7440. start: segmentStartPts,
  7441. end: segmentEndPts
  7442. });
  7443. this.resetTiming_();
  7444. this.trigger('done', 'AudioSegmentStream');
  7445. };
  7446. this.partialFlush = function () {
  7447. this.processFrames_();
  7448. this.trigger('partialdone', 'AudioSegmentStream');
  7449. };
  7450. this.endTimeline = function () {
  7451. this.flush();
  7452. this.trigger('endedtimeline', 'AudioSegmentStream');
  7453. };
  7454. this.resetTiming_ = function () {
  7455. trackDecodeInfo.clearDtsInfo(track);
  7456. segmentStartPts = null;
  7457. segmentEndPts = null;
  7458. };
  7459. this.reset = function () {
  7460. this.resetTiming_();
  7461. adtsFrames = [];
  7462. this.trigger('reset');
  7463. };
  7464. };
  7465. AudioSegmentStream.prototype = new stream();
  7466. var audioSegmentStream = AudioSegmentStream;
  7467. var VideoSegmentStream = function VideoSegmentStream(track, options) {
  7468. var sequenceNumber = 0,
  7469. nalUnits = [],
  7470. frameCache = [],
  7471. // gopsToAlignWith = [],
  7472. config,
  7473. pps,
  7474. segmentStartPts = null,
  7475. segmentEndPts = null,
  7476. gops,
  7477. ensureNextFrameIsKeyFrame = true;
  7478. options = options || {};
  7479. VideoSegmentStream.prototype.init.call(this);
  7480. this.push = function (nalUnit) {
  7481. trackDecodeInfo.collectDtsInfo(track, nalUnit);
  7482. if (typeof track.timelineStartInfo.dts === 'undefined') {
  7483. track.timelineStartInfo.dts = nalUnit.dts;
  7484. } // record the track config
  7485. if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
  7486. config = nalUnit.config;
  7487. track.sps = [nalUnit.data];
  7488. videoProperties.forEach(function (prop) {
  7489. track[prop] = config[prop];
  7490. }, this);
  7491. }
  7492. if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
  7493. pps = nalUnit.data;
  7494. track.pps = [nalUnit.data];
  7495. } // buffer video until flush() is called
  7496. nalUnits.push(nalUnit);
  7497. };
  7498. this.processNals_ = function (cacheLastFrame) {
  7499. var i;
  7500. nalUnits = frameCache.concat(nalUnits); // Throw away nalUnits at the start of the byte stream until
  7501. // we find the first AUD
  7502. while (nalUnits.length) {
  7503. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  7504. break;
  7505. }
  7506. nalUnits.shift();
  7507. } // Return early if no video data has been observed
  7508. if (nalUnits.length === 0) {
  7509. return;
  7510. }
  7511. var frames = frameUtils.groupNalsIntoFrames(nalUnits);
  7512. if (!frames.length) {
  7513. return;
  7514. } // note that the frame cache may also protect us from cases where we haven't
  7515. // pushed data for the entire first or last frame yet
  7516. frameCache = frames[frames.length - 1];
  7517. if (cacheLastFrame) {
  7518. frames.pop();
  7519. frames.duration -= frameCache.duration;
  7520. frames.nalCount -= frameCache.length;
  7521. frames.byteLength -= frameCache.byteLength;
  7522. }
  7523. if (!frames.length) {
  7524. nalUnits = [];
  7525. return;
  7526. }
  7527. this.trigger('timelineStartInfo', track.timelineStartInfo);
  7528. if (ensureNextFrameIsKeyFrame) {
  7529. gops = frameUtils.groupFramesIntoGops(frames);
  7530. if (!gops[0][0].keyFrame) {
  7531. gops = frameUtils.extendFirstKeyFrame(gops);
  7532. if (!gops[0][0].keyFrame) {
  7533. // we haven't yet gotten a key frame, so reset nal units to wait for more nal
  7534. // units
  7535. nalUnits = [].concat.apply([], frames).concat(frameCache);
  7536. frameCache = [];
  7537. return;
  7538. }
  7539. frames = [].concat.apply([], gops);
  7540. frames.duration = gops.duration;
  7541. }
  7542. ensureNextFrameIsKeyFrame = false;
  7543. }
  7544. if (segmentStartPts === null) {
  7545. segmentStartPts = frames[0].pts;
  7546. segmentEndPts = segmentStartPts;
  7547. }
  7548. segmentEndPts += frames.duration;
  7549. this.trigger('timingInfo', {
  7550. start: segmentStartPts,
  7551. end: segmentEndPts
  7552. });
  7553. for (i = 0; i < frames.length; i++) {
  7554. var frame = frames[i];
  7555. track.samples = frameUtils.generateSampleTableForFrame(frame);
  7556. var mdat = mp4Generator.mdat(frameUtils.concatenateNalDataForFrame(frame));
  7557. trackDecodeInfo.clearDtsInfo(track);
  7558. trackDecodeInfo.collectDtsInfo(track, frame);
  7559. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
  7560. var moof = mp4Generator.moof(sequenceNumber, [track]);
  7561. sequenceNumber++;
  7562. track.initSegment = mp4Generator.initSegment([track]);
  7563. var boxes = new Uint8Array(moof.byteLength + mdat.byteLength);
  7564. boxes.set(moof);
  7565. boxes.set(mdat, moof.byteLength);
  7566. this.trigger('data', {
  7567. track: track,
  7568. boxes: boxes,
  7569. sequence: sequenceNumber,
  7570. videoFrameDts: frame.dts,
  7571. videoFramePts: frame.pts
  7572. });
  7573. }
  7574. nalUnits = [];
  7575. };
  7576. this.resetTimingAndConfig_ = function () {
  7577. config = undefined;
  7578. pps = undefined;
  7579. segmentStartPts = null;
  7580. segmentEndPts = null;
  7581. };
  7582. this.partialFlush = function () {
  7583. this.processNals_(true);
  7584. this.trigger('partialdone', 'VideoSegmentStream');
  7585. };
  7586. this.flush = function () {
  7587. this.processNals_(false); // reset config and pps because they may differ across segments
  7588. // for instance, when we are rendition switching
  7589. this.resetTimingAndConfig_();
  7590. this.trigger('done', 'VideoSegmentStream');
  7591. };
  7592. this.endTimeline = function () {
  7593. this.flush();
  7594. this.trigger('endedtimeline', 'VideoSegmentStream');
  7595. };
  7596. this.reset = function () {
  7597. this.resetTimingAndConfig_();
  7598. frameCache = [];
  7599. nalUnits = [];
  7600. ensureNextFrameIsKeyFrame = true;
  7601. this.trigger('reset');
  7602. };
  7603. };
  7604. VideoSegmentStream.prototype = new stream();
  7605. var videoSegmentStream = VideoSegmentStream;
  7606. var isLikelyAacData = utils.isLikelyAacData;
  7607. var createPipeline = function createPipeline(object) {
  7608. object.prototype = new stream();
  7609. object.prototype.init.call(object);
  7610. return object;
  7611. };
  7612. var tsPipeline = function tsPipeline(options) {
  7613. var pipeline = {
  7614. type: 'ts',
  7615. tracks: {
  7616. audio: null,
  7617. video: null
  7618. },
  7619. packet: new m2ts_1.TransportPacketStream(),
  7620. parse: new m2ts_1.TransportParseStream(),
  7621. elementary: new m2ts_1.ElementaryStream(),
  7622. timestampRollover: new m2ts_1.TimestampRolloverStream(),
  7623. adts: new codecs.Adts(),
  7624. h264: new codecs.h264.H264Stream(),
  7625. captionStream: new m2ts_1.CaptionStream(options),
  7626. metadataStream: new m2ts_1.MetadataStream()
  7627. };
  7628. pipeline.headOfPipeline = pipeline.packet; // Transport Stream
  7629. pipeline.packet.pipe(pipeline.parse).pipe(pipeline.elementary).pipe(pipeline.timestampRollover); // H264
  7630. pipeline.timestampRollover.pipe(pipeline.h264); // Hook up CEA-608/708 caption stream
  7631. pipeline.h264.pipe(pipeline.captionStream);
  7632. pipeline.timestampRollover.pipe(pipeline.metadataStream); // ADTS
  7633. pipeline.timestampRollover.pipe(pipeline.adts);
  7634. pipeline.elementary.on('data', function (data) {
  7635. if (data.type !== 'metadata') {
  7636. return;
  7637. }
  7638. for (var i = 0; i < data.tracks.length; i++) {
  7639. if (!pipeline.tracks[data.tracks[i].type]) {
  7640. pipeline.tracks[data.tracks[i].type] = data.tracks[i];
  7641. pipeline.tracks[data.tracks[i].type].timelineStartInfo.baseMediaDecodeTime = options.baseMediaDecodeTime;
  7642. }
  7643. }
  7644. if (pipeline.tracks.video && !pipeline.videoSegmentStream) {
  7645. pipeline.videoSegmentStream = new videoSegmentStream(pipeline.tracks.video, options);
  7646. pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
  7647. if (pipeline.tracks.audio && !options.keepOriginalTimestamps) {
  7648. pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - options.baseMediaDecodeTime);
  7649. }
  7650. });
  7651. pipeline.videoSegmentStream.on('timingInfo', pipeline.trigger.bind(pipeline, 'videoTimingInfo'));
  7652. pipeline.videoSegmentStream.on('data', function (data) {
  7653. pipeline.trigger('data', {
  7654. type: 'video',
  7655. data: data
  7656. });
  7657. });
  7658. pipeline.videoSegmentStream.on('done', pipeline.trigger.bind(pipeline, 'done'));
  7659. pipeline.videoSegmentStream.on('partialdone', pipeline.trigger.bind(pipeline, 'partialdone'));
  7660. pipeline.videoSegmentStream.on('endedtimeline', pipeline.trigger.bind(pipeline, 'endedtimeline'));
  7661. pipeline.h264.pipe(pipeline.videoSegmentStream);
  7662. }
  7663. if (pipeline.tracks.audio && !pipeline.audioSegmentStream) {
  7664. pipeline.audioSegmentStream = new audioSegmentStream(pipeline.tracks.audio, options);
  7665. pipeline.audioSegmentStream.on('data', function (data) {
  7666. pipeline.trigger('data', {
  7667. type: 'audio',
  7668. data: data
  7669. });
  7670. });
  7671. pipeline.audioSegmentStream.on('done', pipeline.trigger.bind(pipeline, 'done'));
  7672. pipeline.audioSegmentStream.on('partialdone', pipeline.trigger.bind(pipeline, 'partialdone'));
  7673. pipeline.audioSegmentStream.on('endedtimeline', pipeline.trigger.bind(pipeline, 'endedtimeline'));
  7674. pipeline.audioSegmentStream.on('timingInfo', pipeline.trigger.bind(pipeline, 'audioTimingInfo'));
  7675. pipeline.adts.pipe(pipeline.audioSegmentStream);
  7676. } // emit pmt info
  7677. pipeline.trigger('trackinfo', {
  7678. hasAudio: !!pipeline.tracks.audio,
  7679. hasVideo: !!pipeline.tracks.video
  7680. });
  7681. });
  7682. pipeline.captionStream.on('data', function (caption) {
  7683. var timelineStartPts;
  7684. if (pipeline.tracks.video) {
  7685. timelineStartPts = pipeline.tracks.video.timelineStartInfo.pts || 0;
  7686. } else {
  7687. // This will only happen if we encounter caption packets before
  7688. // video data in a segment. This is an unusual/unlikely scenario,
  7689. // so we assume the timeline starts at zero for now.
  7690. timelineStartPts = 0;
  7691. } // Translate caption PTS times into second offsets into the
  7692. // video timeline for the segment
  7693. caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, options.keepOriginalTimestamps);
  7694. caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, options.keepOriginalTimestamps);
  7695. pipeline.trigger('caption', caption);
  7696. });
  7697. pipeline = createPipeline(pipeline);
  7698. pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
  7699. return pipeline;
  7700. };
  7701. var aacPipeline = function aacPipeline(options) {
  7702. var pipeline = {
  7703. type: 'aac',
  7704. tracks: {
  7705. audio: null
  7706. },
  7707. metadataStream: new m2ts_1.MetadataStream(),
  7708. aacStream: new aac(),
  7709. audioRollover: new m2ts_1.TimestampRolloverStream('audio'),
  7710. timedMetadataRollover: new m2ts_1.TimestampRolloverStream('timed-metadata'),
  7711. adtsStream: new adts(true)
  7712. }; // set up the parsing pipeline
  7713. pipeline.headOfPipeline = pipeline.aacStream;
  7714. pipeline.aacStream.pipe(pipeline.audioRollover).pipe(pipeline.adtsStream);
  7715. pipeline.aacStream.pipe(pipeline.timedMetadataRollover).pipe(pipeline.metadataStream);
  7716. pipeline.metadataStream.on('timestamp', function (frame) {
  7717. pipeline.aacStream.setTimestamp(frame.timeStamp);
  7718. });
  7719. pipeline.aacStream.on('data', function (data) {
  7720. if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
  7721. return;
  7722. }
  7723. pipeline.tracks.audio = pipeline.tracks.audio || {
  7724. timelineStartInfo: {
  7725. baseMediaDecodeTime: options.baseMediaDecodeTime
  7726. },
  7727. codec: 'adts',
  7728. type: 'audio'
  7729. }; // hook up the audio segment stream to the first track with aac data
  7730. pipeline.audioSegmentStream = new audioSegmentStream(pipeline.tracks.audio, options);
  7731. pipeline.audioSegmentStream.on('data', function (data) {
  7732. pipeline.trigger('data', {
  7733. type: 'audio',
  7734. data: data
  7735. });
  7736. });
  7737. pipeline.audioSegmentStream.on('partialdone', pipeline.trigger.bind(pipeline, 'partialdone'));
  7738. pipeline.audioSegmentStream.on('done', pipeline.trigger.bind(pipeline, 'done'));
  7739. pipeline.audioSegmentStream.on('endedtimeline', pipeline.trigger.bind(pipeline, 'endedtimeline'));
  7740. pipeline.audioSegmentStream.on('timingInfo', pipeline.trigger.bind(pipeline, 'audioTimingInfo')); // Set up the final part of the audio pipeline
  7741. pipeline.adtsStream.pipe(pipeline.audioSegmentStream);
  7742. pipeline.trigger('trackinfo', {
  7743. hasAudio: !!pipeline.tracks.audio,
  7744. hasVideo: !!pipeline.tracks.video
  7745. });
  7746. }); // set the pipeline up as a stream before binding to get access to the trigger function
  7747. pipeline = createPipeline(pipeline);
  7748. pipeline.metadataStream.on('data', pipeline.trigger.bind(pipeline, 'id3Frame'));
  7749. return pipeline;
  7750. };
  7751. var setupPipelineListeners = function setupPipelineListeners(pipeline, transmuxer) {
  7752. pipeline.on('data', transmuxer.trigger.bind(transmuxer, 'data'));
  7753. pipeline.on('done', transmuxer.trigger.bind(transmuxer, 'done'));
  7754. pipeline.on('partialdone', transmuxer.trigger.bind(transmuxer, 'partialdone'));
  7755. pipeline.on('endedtimeline', transmuxer.trigger.bind(transmuxer, 'endedtimeline'));
  7756. pipeline.on('audioTimingInfo', transmuxer.trigger.bind(transmuxer, 'audioTimingInfo'));
  7757. pipeline.on('videoTimingInfo', transmuxer.trigger.bind(transmuxer, 'videoTimingInfo'));
  7758. pipeline.on('trackinfo', transmuxer.trigger.bind(transmuxer, 'trackinfo'));
  7759. pipeline.on('id3Frame', function (event) {
  7760. // add this to every single emitted segment even though it's only needed for the first
  7761. event.dispatchType = pipeline.metadataStream.dispatchType; // keep original time, can be adjusted if needed at a higher level
  7762. event.cueTime = clock.videoTsToSeconds(event.pts);
  7763. transmuxer.trigger('id3Frame', event);
  7764. });
  7765. pipeline.on('caption', function (event) {
  7766. transmuxer.trigger('caption', event);
  7767. });
  7768. };
  7769. var Transmuxer = function Transmuxer(options) {
  7770. var pipeline = null,
  7771. hasFlushed = true;
  7772. options = options || {};
  7773. Transmuxer.prototype.init.call(this);
  7774. options.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
  7775. this.push = function (bytes) {
  7776. if (hasFlushed) {
  7777. var isAac = isLikelyAacData(bytes);
  7778. if (isAac && (!pipeline || pipeline.type !== 'aac')) {
  7779. pipeline = aacPipeline(options);
  7780. setupPipelineListeners(pipeline, this);
  7781. } else if (!isAac && (!pipeline || pipeline.type !== 'ts')) {
  7782. pipeline = tsPipeline(options);
  7783. setupPipelineListeners(pipeline, this);
  7784. }
  7785. hasFlushed = false;
  7786. }
  7787. pipeline.headOfPipeline.push(bytes);
  7788. };
  7789. this.flush = function () {
  7790. if (!pipeline) {
  7791. return;
  7792. }
  7793. hasFlushed = true;
  7794. pipeline.headOfPipeline.flush();
  7795. };
  7796. this.partialFlush = function () {
  7797. if (!pipeline) {
  7798. return;
  7799. }
  7800. pipeline.headOfPipeline.partialFlush();
  7801. };
  7802. this.endTimeline = function () {
  7803. if (!pipeline) {
  7804. return;
  7805. }
  7806. pipeline.headOfPipeline.endTimeline();
  7807. };
  7808. this.reset = function () {
  7809. if (!pipeline) {
  7810. return;
  7811. }
  7812. pipeline.headOfPipeline.reset();
  7813. };
  7814. this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  7815. if (!options.keepOriginalTimestamps) {
  7816. options.baseMediaDecodeTime = baseMediaDecodeTime;
  7817. }
  7818. if (!pipeline) {
  7819. return;
  7820. }
  7821. if (pipeline.tracks.audio) {
  7822. pipeline.tracks.audio.timelineStartInfo.dts = undefined;
  7823. pipeline.tracks.audio.timelineStartInfo.pts = undefined;
  7824. trackDecodeInfo.clearDtsInfo(pipeline.tracks.audio);
  7825. if (pipeline.audioRollover) {
  7826. pipeline.audioRollover.discontinuity();
  7827. }
  7828. }
  7829. if (pipeline.tracks.video) {
  7830. if (pipeline.videoSegmentStream) {
  7831. pipeline.videoSegmentStream.gopCache_ = [];
  7832. }
  7833. pipeline.tracks.video.timelineStartInfo.dts = undefined;
  7834. pipeline.tracks.video.timelineStartInfo.pts = undefined;
  7835. trackDecodeInfo.clearDtsInfo(pipeline.tracks.video); // pipeline.captionStream.reset();
  7836. }
  7837. if (pipeline.timestampRollover) {
  7838. pipeline.timestampRollover.discontinuity();
  7839. }
  7840. };
  7841. this.setRemux = function (val) {
  7842. options.remux = val;
  7843. if (pipeline && pipeline.coalesceStream) {
  7844. pipeline.coalesceStream.setRemux(val);
  7845. }
  7846. };
  7847. this.setAudioAppendStart = function (audioAppendStart) {
  7848. if (!pipeline || !pipeline.tracks.audio || !pipeline.audioSegmentStream) {
  7849. return;
  7850. }
  7851. pipeline.audioSegmentStream.setAudioAppendStart(audioAppendStart);
  7852. }; // TODO GOP alignment support
  7853. // Support may be a bit trickier than with full segment appends, as GOPs may be split
  7854. // and processed in a more granular fashion
  7855. this.alignGopsWith = function (gopsToAlignWith) {
  7856. return;
  7857. };
  7858. };
  7859. Transmuxer.prototype = new stream();
  7860. var transmuxer = Transmuxer;
  7861. var partial = {
  7862. Transmuxer: transmuxer
  7863. };
  7864. var getUint64$1 = numbers.getUint64;
  7865. var parseSidx = function parseSidx(data) {
  7866. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  7867. result = {
  7868. version: data[0],
  7869. flags: new Uint8Array(data.subarray(1, 4)),
  7870. references: [],
  7871. referenceId: view.getUint32(4),
  7872. timescale: view.getUint32(8)
  7873. },
  7874. i = 12;
  7875. if (result.version === 0) {
  7876. result.earliestPresentationTime = view.getUint32(i);
  7877. result.firstOffset = view.getUint32(i + 4);
  7878. i += 8;
  7879. } else {
  7880. // read 64 bits
  7881. result.earliestPresentationTime = getUint64$1(data.subarray(i));
  7882. result.firstOffset = getUint64$1(data.subarray(i + 8));
  7883. i += 16;
  7884. }
  7885. i += 2; // reserved
  7886. var referenceCount = view.getUint16(i);
  7887. i += 2; // start of references
  7888. for (; referenceCount > 0; i += 12, referenceCount--) {
  7889. result.references.push({
  7890. referenceType: (data[i] & 0x80) >>> 7,
  7891. referencedSize: view.getUint32(i) & 0x7FFFFFFF,
  7892. subsegmentDuration: view.getUint32(i + 4),
  7893. startsWithSap: !!(data[i + 8] & 0x80),
  7894. sapType: (data[i + 8] & 0x70) >>> 4,
  7895. sapDeltaTime: view.getUint32(i + 8) & 0x0FFFFFFF
  7896. });
  7897. }
  7898. return result;
  7899. };
  7900. var parseSidx_1 = parseSidx;
  7901. var getUint64 = numbers.getUint64;
  7902. var inspectMp4,
  7903. _textifyMp,
  7904. parseMp4Date = function parseMp4Date(seconds) {
  7905. return new Date(seconds * 1000 - 2082844800000);
  7906. },
  7907. nalParse = function nalParse(avcStream) {
  7908. var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
  7909. result = [],
  7910. i,
  7911. length;
  7912. for (i = 0; i + 4 < avcStream.length; i += length) {
  7913. length = avcView.getUint32(i);
  7914. i += 4; // bail if this doesn't appear to be an H264 stream
  7915. if (length <= 0) {
  7916. result.push('<span style=\'color:red;\'>MALFORMED DATA</span>');
  7917. continue;
  7918. }
  7919. switch (avcStream[i] & 0x1F) {
  7920. case 0x01:
  7921. result.push('slice_layer_without_partitioning_rbsp');
  7922. break;
  7923. case 0x05:
  7924. result.push('slice_layer_without_partitioning_rbsp_idr');
  7925. break;
  7926. case 0x06:
  7927. result.push('sei_rbsp');
  7928. break;
  7929. case 0x07:
  7930. result.push('seq_parameter_set_rbsp');
  7931. break;
  7932. case 0x08:
  7933. result.push('pic_parameter_set_rbsp');
  7934. break;
  7935. case 0x09:
  7936. result.push('access_unit_delimiter_rbsp');
  7937. break;
  7938. default:
  7939. result.push('UNKNOWN NAL - ' + avcStream[i] & 0x1F);
  7940. break;
  7941. }
  7942. }
  7943. return result;
  7944. },
  7945. // registry of handlers for individual mp4 box types
  7946. parse = {
  7947. // codingname, not a first-class box type. stsd entries share the
  7948. // same format as real boxes so the parsing infrastructure can be
  7949. // shared
  7950. avc1: function avc1(data) {
  7951. var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  7952. return {
  7953. dataReferenceIndex: view.getUint16(6),
  7954. width: view.getUint16(24),
  7955. height: view.getUint16(26),
  7956. horizresolution: view.getUint16(28) + view.getUint16(30) / 16,
  7957. vertresolution: view.getUint16(32) + view.getUint16(34) / 16,
  7958. frameCount: view.getUint16(40),
  7959. depth: view.getUint16(74),
  7960. config: inspectMp4(data.subarray(78, data.byteLength))
  7961. };
  7962. },
  7963. avcC: function avcC(data) {
  7964. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  7965. result = {
  7966. configurationVersion: data[0],
  7967. avcProfileIndication: data[1],
  7968. profileCompatibility: data[2],
  7969. avcLevelIndication: data[3],
  7970. lengthSizeMinusOne: data[4] & 0x03,
  7971. sps: [],
  7972. pps: []
  7973. },
  7974. numOfSequenceParameterSets = data[5] & 0x1f,
  7975. numOfPictureParameterSets,
  7976. nalSize,
  7977. offset,
  7978. i; // iterate past any SPSs
  7979. offset = 6;
  7980. for (i = 0; i < numOfSequenceParameterSets; i++) {
  7981. nalSize = view.getUint16(offset);
  7982. offset += 2;
  7983. result.sps.push(new Uint8Array(data.subarray(offset, offset + nalSize)));
  7984. offset += nalSize;
  7985. } // iterate past any PPSs
  7986. numOfPictureParameterSets = data[offset];
  7987. offset++;
  7988. for (i = 0; i < numOfPictureParameterSets; i++) {
  7989. nalSize = view.getUint16(offset);
  7990. offset += 2;
  7991. result.pps.push(new Uint8Array(data.subarray(offset, offset + nalSize)));
  7992. offset += nalSize;
  7993. }
  7994. return result;
  7995. },
  7996. btrt: function btrt(data) {
  7997. var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  7998. return {
  7999. bufferSizeDB: view.getUint32(0),
  8000. maxBitrate: view.getUint32(4),
  8001. avgBitrate: view.getUint32(8)
  8002. };
  8003. },
  8004. edts: function edts(data) {
  8005. return {
  8006. boxes: inspectMp4(data)
  8007. };
  8008. },
  8009. elst: function elst(data) {
  8010. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8011. result = {
  8012. version: view.getUint8(0),
  8013. flags: new Uint8Array(data.subarray(1, 4)),
  8014. edits: []
  8015. },
  8016. entryCount = view.getUint32(4),
  8017. i;
  8018. for (i = 8; entryCount; entryCount--) {
  8019. if (result.version === 0) {
  8020. result.edits.push({
  8021. segmentDuration: view.getUint32(i),
  8022. mediaTime: view.getInt32(i + 4),
  8023. mediaRate: view.getUint16(i + 8) + view.getUint16(i + 10) / (256 * 256)
  8024. });
  8025. i += 12;
  8026. } else {
  8027. result.edits.push({
  8028. segmentDuration: getUint64(data.subarray(i)),
  8029. mediaTime: getUint64(data.subarray(i + 8)),
  8030. mediaRate: view.getUint16(i + 16) + view.getUint16(i + 18) / (256 * 256)
  8031. });
  8032. i += 20;
  8033. }
  8034. }
  8035. return result;
  8036. },
  8037. esds: function esds(data) {
  8038. return {
  8039. version: data[0],
  8040. flags: new Uint8Array(data.subarray(1, 4)),
  8041. esId: data[6] << 8 | data[7],
  8042. streamPriority: data[8] & 0x1f,
  8043. decoderConfig: {
  8044. objectProfileIndication: data[11],
  8045. streamType: data[12] >>> 2 & 0x3f,
  8046. bufferSize: data[13] << 16 | data[14] << 8 | data[15],
  8047. maxBitrate: data[16] << 24 | data[17] << 16 | data[18] << 8 | data[19],
  8048. avgBitrate: data[20] << 24 | data[21] << 16 | data[22] << 8 | data[23],
  8049. decoderConfigDescriptor: {
  8050. tag: data[24],
  8051. length: data[25],
  8052. audioObjectType: data[26] >>> 3 & 0x1f,
  8053. samplingFrequencyIndex: (data[26] & 0x07) << 1 | data[27] >>> 7 & 0x01,
  8054. channelConfiguration: data[27] >>> 3 & 0x0f
  8055. }
  8056. }
  8057. };
  8058. },
  8059. ftyp: function ftyp(data) {
  8060. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8061. result = {
  8062. majorBrand: parseType_1(data.subarray(0, 4)),
  8063. minorVersion: view.getUint32(4),
  8064. compatibleBrands: []
  8065. },
  8066. i = 8;
  8067. while (i < data.byteLength) {
  8068. result.compatibleBrands.push(parseType_1(data.subarray(i, i + 4)));
  8069. i += 4;
  8070. }
  8071. return result;
  8072. },
  8073. dinf: function dinf(data) {
  8074. return {
  8075. boxes: inspectMp4(data)
  8076. };
  8077. },
  8078. dref: function dref(data) {
  8079. return {
  8080. version: data[0],
  8081. flags: new Uint8Array(data.subarray(1, 4)),
  8082. dataReferences: inspectMp4(data.subarray(8))
  8083. };
  8084. },
  8085. hdlr: function hdlr(data) {
  8086. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8087. result = {
  8088. version: view.getUint8(0),
  8089. flags: new Uint8Array(data.subarray(1, 4)),
  8090. handlerType: parseType_1(data.subarray(8, 12)),
  8091. name: ''
  8092. },
  8093. i = 8; // parse out the name field
  8094. for (i = 24; i < data.byteLength; i++) {
  8095. if (data[i] === 0x00) {
  8096. // the name field is null-terminated
  8097. i++;
  8098. break;
  8099. }
  8100. result.name += String.fromCharCode(data[i]);
  8101. } // decode UTF-8 to javascript's internal representation
  8102. // see http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html
  8103. result.name = decodeURIComponent(escape(result.name));
  8104. return result;
  8105. },
  8106. mdat: function mdat(data) {
  8107. return {
  8108. byteLength: data.byteLength,
  8109. nals: nalParse(data)
  8110. };
  8111. },
  8112. mdhd: function mdhd(data) {
  8113. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8114. i = 4,
  8115. language,
  8116. result = {
  8117. version: view.getUint8(0),
  8118. flags: new Uint8Array(data.subarray(1, 4)),
  8119. language: ''
  8120. };
  8121. if (result.version === 1) {
  8122. i += 4;
  8123. result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8124. i += 8;
  8125. result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8126. i += 4;
  8127. result.timescale = view.getUint32(i);
  8128. i += 8;
  8129. result.duration = view.getUint32(i); // truncating top 4 bytes
  8130. } else {
  8131. result.creationTime = parseMp4Date(view.getUint32(i));
  8132. i += 4;
  8133. result.modificationTime = parseMp4Date(view.getUint32(i));
  8134. i += 4;
  8135. result.timescale = view.getUint32(i);
  8136. i += 4;
  8137. result.duration = view.getUint32(i);
  8138. }
  8139. i += 4; // language is stored as an ISO-639-2/T code in an array of three 5-bit fields
  8140. // each field is the packed difference between its ASCII value and 0x60
  8141. language = view.getUint16(i);
  8142. result.language += String.fromCharCode((language >> 10) + 0x60);
  8143. result.language += String.fromCharCode(((language & 0x03e0) >> 5) + 0x60);
  8144. result.language += String.fromCharCode((language & 0x1f) + 0x60);
  8145. return result;
  8146. },
  8147. mdia: function mdia(data) {
  8148. return {
  8149. boxes: inspectMp4(data)
  8150. };
  8151. },
  8152. mfhd: function mfhd(data) {
  8153. return {
  8154. version: data[0],
  8155. flags: new Uint8Array(data.subarray(1, 4)),
  8156. sequenceNumber: data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]
  8157. };
  8158. },
  8159. minf: function minf(data) {
  8160. return {
  8161. boxes: inspectMp4(data)
  8162. };
  8163. },
  8164. // codingname, not a first-class box type. stsd entries share the
  8165. // same format as real boxes so the parsing infrastructure can be
  8166. // shared
  8167. mp4a: function mp4a(data) {
  8168. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8169. result = {
  8170. // 6 bytes reserved
  8171. dataReferenceIndex: view.getUint16(6),
  8172. // 4 + 4 bytes reserved
  8173. channelcount: view.getUint16(16),
  8174. samplesize: view.getUint16(18),
  8175. // 2 bytes pre_defined
  8176. // 2 bytes reserved
  8177. samplerate: view.getUint16(24) + view.getUint16(26) / 65536
  8178. }; // if there are more bytes to process, assume this is an ISO/IEC
  8179. // 14496-14 MP4AudioSampleEntry and parse the ESDBox
  8180. if (data.byteLength > 28) {
  8181. result.streamDescriptor = inspectMp4(data.subarray(28))[0];
  8182. }
  8183. return result;
  8184. },
  8185. moof: function moof(data) {
  8186. return {
  8187. boxes: inspectMp4(data)
  8188. };
  8189. },
  8190. moov: function moov(data) {
  8191. return {
  8192. boxes: inspectMp4(data)
  8193. };
  8194. },
  8195. mvex: function mvex(data) {
  8196. return {
  8197. boxes: inspectMp4(data)
  8198. };
  8199. },
  8200. mvhd: function mvhd(data) {
  8201. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8202. i = 4,
  8203. result = {
  8204. version: view.getUint8(0),
  8205. flags: new Uint8Array(data.subarray(1, 4))
  8206. };
  8207. if (result.version === 1) {
  8208. i += 4;
  8209. result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8210. i += 8;
  8211. result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8212. i += 4;
  8213. result.timescale = view.getUint32(i);
  8214. i += 8;
  8215. result.duration = view.getUint32(i); // truncating top 4 bytes
  8216. } else {
  8217. result.creationTime = parseMp4Date(view.getUint32(i));
  8218. i += 4;
  8219. result.modificationTime = parseMp4Date(view.getUint32(i));
  8220. i += 4;
  8221. result.timescale = view.getUint32(i);
  8222. i += 4;
  8223. result.duration = view.getUint32(i);
  8224. }
  8225. i += 4; // convert fixed-point, base 16 back to a number
  8226. result.rate = view.getUint16(i) + view.getUint16(i + 2) / 16;
  8227. i += 4;
  8228. result.volume = view.getUint8(i) + view.getUint8(i + 1) / 8;
  8229. i += 2;
  8230. i += 2;
  8231. i += 2 * 4;
  8232. result.matrix = new Uint32Array(data.subarray(i, i + 9 * 4));
  8233. i += 9 * 4;
  8234. i += 6 * 4;
  8235. result.nextTrackId = view.getUint32(i);
  8236. return result;
  8237. },
  8238. pdin: function pdin(data) {
  8239. var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  8240. return {
  8241. version: view.getUint8(0),
  8242. flags: new Uint8Array(data.subarray(1, 4)),
  8243. rate: view.getUint32(4),
  8244. initialDelay: view.getUint32(8)
  8245. };
  8246. },
  8247. sdtp: function sdtp(data) {
  8248. var result = {
  8249. version: data[0],
  8250. flags: new Uint8Array(data.subarray(1, 4)),
  8251. samples: []
  8252. },
  8253. i;
  8254. for (i = 4; i < data.byteLength; i++) {
  8255. result.samples.push({
  8256. dependsOn: (data[i] & 0x30) >> 4,
  8257. isDependedOn: (data[i] & 0x0c) >> 2,
  8258. hasRedundancy: data[i] & 0x03
  8259. });
  8260. }
  8261. return result;
  8262. },
  8263. sidx: parseSidx_1,
  8264. smhd: function smhd(data) {
  8265. return {
  8266. version: data[0],
  8267. flags: new Uint8Array(data.subarray(1, 4)),
  8268. balance: data[4] + data[5] / 256
  8269. };
  8270. },
  8271. stbl: function stbl(data) {
  8272. return {
  8273. boxes: inspectMp4(data)
  8274. };
  8275. },
  8276. ctts: function ctts(data) {
  8277. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8278. result = {
  8279. version: view.getUint8(0),
  8280. flags: new Uint8Array(data.subarray(1, 4)),
  8281. compositionOffsets: []
  8282. },
  8283. entryCount = view.getUint32(4),
  8284. i;
  8285. for (i = 8; entryCount; i += 8, entryCount--) {
  8286. result.compositionOffsets.push({
  8287. sampleCount: view.getUint32(i),
  8288. sampleOffset: view[result.version === 0 ? 'getUint32' : 'getInt32'](i + 4)
  8289. });
  8290. }
  8291. return result;
  8292. },
  8293. stss: function stss(data) {
  8294. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8295. result = {
  8296. version: view.getUint8(0),
  8297. flags: new Uint8Array(data.subarray(1, 4)),
  8298. syncSamples: []
  8299. },
  8300. entryCount = view.getUint32(4),
  8301. i;
  8302. for (i = 8; entryCount; i += 4, entryCount--) {
  8303. result.syncSamples.push(view.getUint32(i));
  8304. }
  8305. return result;
  8306. },
  8307. stco: function stco(data) {
  8308. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8309. result = {
  8310. version: data[0],
  8311. flags: new Uint8Array(data.subarray(1, 4)),
  8312. chunkOffsets: []
  8313. },
  8314. entryCount = view.getUint32(4),
  8315. i;
  8316. for (i = 8; entryCount; i += 4, entryCount--) {
  8317. result.chunkOffsets.push(view.getUint32(i));
  8318. }
  8319. return result;
  8320. },
  8321. stsc: function stsc(data) {
  8322. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8323. entryCount = view.getUint32(4),
  8324. result = {
  8325. version: data[0],
  8326. flags: new Uint8Array(data.subarray(1, 4)),
  8327. sampleToChunks: []
  8328. },
  8329. i;
  8330. for (i = 8; entryCount; i += 12, entryCount--) {
  8331. result.sampleToChunks.push({
  8332. firstChunk: view.getUint32(i),
  8333. samplesPerChunk: view.getUint32(i + 4),
  8334. sampleDescriptionIndex: view.getUint32(i + 8)
  8335. });
  8336. }
  8337. return result;
  8338. },
  8339. stsd: function stsd(data) {
  8340. return {
  8341. version: data[0],
  8342. flags: new Uint8Array(data.subarray(1, 4)),
  8343. sampleDescriptions: inspectMp4(data.subarray(8))
  8344. };
  8345. },
  8346. stsz: function stsz(data) {
  8347. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8348. result = {
  8349. version: data[0],
  8350. flags: new Uint8Array(data.subarray(1, 4)),
  8351. sampleSize: view.getUint32(4),
  8352. entries: []
  8353. },
  8354. i;
  8355. for (i = 12; i < data.byteLength; i += 4) {
  8356. result.entries.push(view.getUint32(i));
  8357. }
  8358. return result;
  8359. },
  8360. stts: function stts(data) {
  8361. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8362. result = {
  8363. version: data[0],
  8364. flags: new Uint8Array(data.subarray(1, 4)),
  8365. timeToSamples: []
  8366. },
  8367. entryCount = view.getUint32(4),
  8368. i;
  8369. for (i = 8; entryCount; i += 8, entryCount--) {
  8370. result.timeToSamples.push({
  8371. sampleCount: view.getUint32(i),
  8372. sampleDelta: view.getUint32(i + 4)
  8373. });
  8374. }
  8375. return result;
  8376. },
  8377. styp: function styp(data) {
  8378. return parse.ftyp(data);
  8379. },
  8380. tfdt: parseTfdt,
  8381. tfhd: parseTfhd,
  8382. tkhd: function tkhd(data) {
  8383. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  8384. i = 4,
  8385. result = {
  8386. version: view.getUint8(0),
  8387. flags: new Uint8Array(data.subarray(1, 4))
  8388. };
  8389. if (result.version === 1) {
  8390. i += 4;
  8391. result.creationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8392. i += 8;
  8393. result.modificationTime = parseMp4Date(view.getUint32(i)); // truncating top 4 bytes
  8394. i += 4;
  8395. result.trackId = view.getUint32(i);
  8396. i += 4;
  8397. i += 8;
  8398. result.duration = view.getUint32(i); // truncating top 4 bytes
  8399. } else {
  8400. result.creationTime = parseMp4Date(view.getUint32(i));
  8401. i += 4;
  8402. result.modificationTime = parseMp4Date(view.getUint32(i));
  8403. i += 4;
  8404. result.trackId = view.getUint32(i);
  8405. i += 4;
  8406. i += 4;
  8407. result.duration = view.getUint32(i);
  8408. }
  8409. i += 4;
  8410. i += 2 * 4;
  8411. result.layer = view.getUint16(i);
  8412. i += 2;
  8413. result.alternateGroup = view.getUint16(i);
  8414. i += 2; // convert fixed-point, base 16 back to a number
  8415. result.volume = view.getUint8(i) + view.getUint8(i + 1) / 8;
  8416. i += 2;
  8417. i += 2;
  8418. result.matrix = new Uint32Array(data.subarray(i, i + 9 * 4));
  8419. i += 9 * 4;
  8420. result.width = view.getUint16(i) + view.getUint16(i + 2) / 65536;
  8421. i += 4;
  8422. result.height = view.getUint16(i) + view.getUint16(i + 2) / 65536;
  8423. return result;
  8424. },
  8425. traf: function traf(data) {
  8426. return {
  8427. boxes: inspectMp4(data)
  8428. };
  8429. },
  8430. trak: function trak(data) {
  8431. return {
  8432. boxes: inspectMp4(data)
  8433. };
  8434. },
  8435. trex: function trex(data) {
  8436. var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  8437. return {
  8438. version: data[0],
  8439. flags: new Uint8Array(data.subarray(1, 4)),
  8440. trackId: view.getUint32(4),
  8441. defaultSampleDescriptionIndex: view.getUint32(8),
  8442. defaultSampleDuration: view.getUint32(12),
  8443. defaultSampleSize: view.getUint32(16),
  8444. sampleDependsOn: data[20] & 0x03,
  8445. sampleIsDependedOn: (data[21] & 0xc0) >> 6,
  8446. sampleHasRedundancy: (data[21] & 0x30) >> 4,
  8447. samplePaddingValue: (data[21] & 0x0e) >> 1,
  8448. sampleIsDifferenceSample: !!(data[21] & 0x01),
  8449. sampleDegradationPriority: view.getUint16(22)
  8450. };
  8451. },
  8452. trun: parseTrun,
  8453. 'url ': function url(data) {
  8454. return {
  8455. version: data[0],
  8456. flags: new Uint8Array(data.subarray(1, 4))
  8457. };
  8458. },
  8459. vmhd: function vmhd(data) {
  8460. var view = new DataView(data.buffer, data.byteOffset, data.byteLength);
  8461. return {
  8462. version: data[0],
  8463. flags: new Uint8Array(data.subarray(1, 4)),
  8464. graphicsmode: view.getUint16(4),
  8465. opcolor: new Uint16Array([view.getUint16(6), view.getUint16(8), view.getUint16(10)])
  8466. };
  8467. }
  8468. };
  8469. /**
  8470. * Return a javascript array of box objects parsed from an ISO base
  8471. * media file.
  8472. * @param data {Uint8Array} the binary data of the media to be inspected
  8473. * @return {array} a javascript array of potentially nested box objects
  8474. */
  8475. inspectMp4 = function inspectMp4(data) {
  8476. var i = 0,
  8477. result = [],
  8478. view,
  8479. size,
  8480. type,
  8481. end,
  8482. box; // Convert data from Uint8Array to ArrayBuffer, to follow Dataview API
  8483. var ab = new ArrayBuffer(data.length);
  8484. var v = new Uint8Array(ab);
  8485. for (var z = 0; z < data.length; ++z) {
  8486. v[z] = data[z];
  8487. }
  8488. view = new DataView(ab);
  8489. while (i < data.byteLength) {
  8490. // parse box data
  8491. size = view.getUint32(i);
  8492. type = parseType_1(data.subarray(i + 4, i + 8));
  8493. end = size > 1 ? i + size : data.byteLength; // parse type-specific data
  8494. box = (parse[type] || function (data) {
  8495. return {
  8496. data: data
  8497. };
  8498. })(data.subarray(i + 8, end));
  8499. box.size = size;
  8500. box.type = type; // store this box and move to the next
  8501. result.push(box);
  8502. i = end;
  8503. }
  8504. return result;
  8505. };
  8506. /**
  8507. * Returns a textual representation of the javascript represtentation
  8508. * of an MP4 file. You can use it as an alternative to
  8509. * JSON.stringify() to compare inspected MP4s.
  8510. * @param inspectedMp4 {array} the parsed array of boxes in an MP4
  8511. * file
  8512. * @param depth {number} (optional) the number of ancestor boxes of
  8513. * the elements of inspectedMp4. Assumed to be zero if unspecified.
  8514. * @return {string} a text representation of the parsed MP4
  8515. */
  8516. _textifyMp = function textifyMp4(inspectedMp4, depth) {
  8517. var indent;
  8518. depth = depth || 0;
  8519. indent = new Array(depth * 2 + 1).join(' '); // iterate over all the boxes
  8520. return inspectedMp4.map(function (box, index) {
  8521. // list the box type first at the current indentation level
  8522. return indent + box.type + '\n' + // the type is already included and handle child boxes separately
  8523. Object.keys(box).filter(function (key) {
  8524. return key !== 'type' && key !== 'boxes'; // output all the box properties
  8525. }).map(function (key) {
  8526. var prefix = indent + ' ' + key + ': ',
  8527. value = box[key]; // print out raw bytes as hexademical
  8528. if (value instanceof Uint8Array || value instanceof Uint32Array) {
  8529. var bytes = Array.prototype.slice.call(new Uint8Array(value.buffer, value.byteOffset, value.byteLength)).map(function (byte) {
  8530. return ' ' + ('00' + byte.toString(16)).slice(-2);
  8531. }).join('').match(/.{1,24}/g);
  8532. if (!bytes) {
  8533. return prefix + '<>';
  8534. }
  8535. if (bytes.length === 1) {
  8536. return prefix + '<' + bytes.join('').slice(1) + '>';
  8537. }
  8538. return prefix + '<\n' + bytes.map(function (line) {
  8539. return indent + ' ' + line;
  8540. }).join('\n') + '\n' + indent + ' >';
  8541. } // stringify generic objects
  8542. return prefix + JSON.stringify(value, null, 2).split('\n').map(function (line, index) {
  8543. if (index === 0) {
  8544. return line;
  8545. }
  8546. return indent + ' ' + line;
  8547. }).join('\n');
  8548. }).join('\n') + ( // recursively textify the child boxes
  8549. box.boxes ? '\n' + _textifyMp(box.boxes, depth + 1) : '');
  8550. }).join('\n');
  8551. };
  8552. var mp4Inspector = {
  8553. inspect: inspectMp4,
  8554. textify: _textifyMp,
  8555. parseType: parseType_1,
  8556. findBox: findBox_1,
  8557. parseTraf: parse.traf,
  8558. parseTfdt: parse.tfdt,
  8559. parseHdlr: parse.hdlr,
  8560. parseTfhd: parse.tfhd,
  8561. parseTrun: parse.trun,
  8562. parseSidx: parse.sidx
  8563. };
  8564. /**
  8565. * mux.js
  8566. *
  8567. * Copyright (c) Brightcove
  8568. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  8569. */
  8570. var tagTypes = {
  8571. 0x08: 'audio',
  8572. 0x09: 'video',
  8573. 0x12: 'metadata'
  8574. },
  8575. hex = function hex(val) {
  8576. return '0x' + ('00' + val.toString(16)).slice(-2).toUpperCase();
  8577. },
  8578. hexStringList = function hexStringList(data) {
  8579. var arr = [],
  8580. i;
  8581. while (data.byteLength > 0) {
  8582. i = 0;
  8583. arr.push(hex(data[i++]));
  8584. data = data.subarray(i);
  8585. }
  8586. return arr.join(' ');
  8587. },
  8588. parseAVCTag = function parseAVCTag(tag, obj) {
  8589. var avcPacketTypes = ['AVC Sequence Header', 'AVC NALU', 'AVC End-of-Sequence'],
  8590. compositionTime = tag[1] & parseInt('01111111', 2) << 16 | tag[2] << 8 | tag[3];
  8591. obj = obj || {};
  8592. obj.avcPacketType = avcPacketTypes[tag[0]];
  8593. obj.CompositionTime = tag[1] & parseInt('10000000', 2) ? -compositionTime : compositionTime;
  8594. if (tag[0] === 1) {
  8595. obj.nalUnitTypeRaw = hexStringList(tag.subarray(4, 100));
  8596. } else {
  8597. obj.data = hexStringList(tag.subarray(4));
  8598. }
  8599. return obj;
  8600. },
  8601. parseVideoTag = function parseVideoTag(tag, obj) {
  8602. var frameTypes = ['Unknown', 'Keyframe (for AVC, a seekable frame)', 'Inter frame (for AVC, a nonseekable frame)', 'Disposable inter frame (H.263 only)', 'Generated keyframe (reserved for server use only)', 'Video info/command frame'],
  8603. codecID = tag[0] & parseInt('00001111', 2);
  8604. obj = obj || {};
  8605. obj.frameType = frameTypes[(tag[0] & parseInt('11110000', 2)) >>> 4];
  8606. obj.codecID = codecID;
  8607. if (codecID === 7) {
  8608. return parseAVCTag(tag.subarray(1), obj);
  8609. }
  8610. return obj;
  8611. },
  8612. parseAACTag = function parseAACTag(tag, obj) {
  8613. var packetTypes = ['AAC Sequence Header', 'AAC Raw'];
  8614. obj = obj || {};
  8615. obj.aacPacketType = packetTypes[tag[0]];
  8616. obj.data = hexStringList(tag.subarray(1));
  8617. return obj;
  8618. },
  8619. parseAudioTag = function parseAudioTag(tag, obj) {
  8620. var formatTable = ['Linear PCM, platform endian', 'ADPCM', 'MP3', 'Linear PCM, little endian', 'Nellymoser 16-kHz mono', 'Nellymoser 8-kHz mono', 'Nellymoser', 'G.711 A-law logarithmic PCM', 'G.711 mu-law logarithmic PCM', 'reserved', 'AAC', 'Speex', 'MP3 8-Khz', 'Device-specific sound'],
  8621. samplingRateTable = ['5.5-kHz', '11-kHz', '22-kHz', '44-kHz'],
  8622. soundFormat = (tag[0] & parseInt('11110000', 2)) >>> 4;
  8623. obj = obj || {};
  8624. obj.soundFormat = formatTable[soundFormat];
  8625. obj.soundRate = samplingRateTable[(tag[0] & parseInt('00001100', 2)) >>> 2];
  8626. obj.soundSize = (tag[0] & parseInt('00000010', 2)) >>> 1 ? '16-bit' : '8-bit';
  8627. obj.soundType = tag[0] & parseInt('00000001', 2) ? 'Stereo' : 'Mono';
  8628. if (soundFormat === 10) {
  8629. return parseAACTag(tag.subarray(1), obj);
  8630. }
  8631. return obj;
  8632. },
  8633. parseGenericTag = function parseGenericTag(tag) {
  8634. return {
  8635. tagType: tagTypes[tag[0]],
  8636. dataSize: tag[1] << 16 | tag[2] << 8 | tag[3],
  8637. timestamp: tag[7] << 24 | tag[4] << 16 | tag[5] << 8 | tag[6],
  8638. streamID: tag[8] << 16 | tag[9] << 8 | tag[10]
  8639. };
  8640. },
  8641. inspectFlvTag = function inspectFlvTag(tag) {
  8642. var header = parseGenericTag(tag);
  8643. switch (tag[0]) {
  8644. case 0x08:
  8645. parseAudioTag(tag.subarray(11), header);
  8646. break;
  8647. case 0x09:
  8648. parseVideoTag(tag.subarray(11), header);
  8649. break;
  8650. }
  8651. return header;
  8652. },
  8653. inspectFlv = function inspectFlv(bytes) {
  8654. var i = 9,
  8655. // header
  8656. dataSize,
  8657. parsedResults = [],
  8658. tag; // traverse the tags
  8659. i += 4; // skip previous tag size
  8660. while (i < bytes.byteLength) {
  8661. dataSize = bytes[i + 1] << 16;
  8662. dataSize |= bytes[i + 2] << 8;
  8663. dataSize |= bytes[i + 3];
  8664. dataSize += 11;
  8665. tag = bytes.subarray(i, i + dataSize);
  8666. parsedResults.push(inspectFlvTag(tag));
  8667. i += dataSize + 4;
  8668. }
  8669. return parsedResults;
  8670. },
  8671. textifyFlv = function textifyFlv(flvTagArray) {
  8672. return JSON.stringify(flvTagArray, null, 2);
  8673. };
  8674. var flvInspector = {
  8675. inspectTag: inspectFlvTag,
  8676. inspect: inspectFlv,
  8677. textify: textifyFlv
  8678. };
  8679. var parsePid = function parsePid(packet) {
  8680. var pid = packet[1] & 0x1f;
  8681. pid <<= 8;
  8682. pid |= packet[2];
  8683. return pid;
  8684. };
  8685. var parsePayloadUnitStartIndicator = function parsePayloadUnitStartIndicator(packet) {
  8686. return !!(packet[1] & 0x40);
  8687. };
  8688. var parseAdaptionField = function parseAdaptionField(packet) {
  8689. var offset = 0; // if an adaption field is present, its length is specified by the
  8690. // fifth byte of the TS packet header. The adaptation field is
  8691. // used to add stuffing to PES packets that don't fill a complete
  8692. // TS packet, and to specify some forms of timing and control data
  8693. // that we do not currently use.
  8694. if ((packet[3] & 0x30) >>> 4 > 0x01) {
  8695. offset += packet[4] + 1;
  8696. }
  8697. return offset;
  8698. };
  8699. var parseType = function parseType(packet, pmtPid) {
  8700. var pid = parsePid(packet);
  8701. if (pid === 0) {
  8702. return 'pat';
  8703. } else if (pid === pmtPid) {
  8704. return 'pmt';
  8705. } else if (pmtPid) {
  8706. return 'pes';
  8707. }
  8708. return null;
  8709. };
  8710. var parsePat = function parsePat(packet) {
  8711. var pusi = parsePayloadUnitStartIndicator(packet);
  8712. var offset = 4 + parseAdaptionField(packet);
  8713. if (pusi) {
  8714. offset += packet[offset] + 1;
  8715. }
  8716. return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
  8717. };
  8718. var parsePmt = function parsePmt(packet) {
  8719. var programMapTable = {};
  8720. var pusi = parsePayloadUnitStartIndicator(packet);
  8721. var payloadOffset = 4 + parseAdaptionField(packet);
  8722. if (pusi) {
  8723. payloadOffset += packet[payloadOffset] + 1;
  8724. } // PMTs can be sent ahead of the time when they should actually
  8725. // take effect. We don't believe this should ever be the case
  8726. // for HLS but we'll ignore "forward" PMT declarations if we see
  8727. // them. Future PMT declarations have the current_next_indicator
  8728. // set to zero.
  8729. if (!(packet[payloadOffset + 5] & 0x01)) {
  8730. return;
  8731. }
  8732. var sectionLength, tableEnd, programInfoLength; // the mapping table ends at the end of the current section
  8733. sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
  8734. tableEnd = 3 + sectionLength - 4; // to determine where the table is, we have to figure out how
  8735. // long the program info descriptors are
  8736. programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11]; // advance the offset to the first entry in the mapping table
  8737. var offset = 12 + programInfoLength;
  8738. while (offset < tableEnd) {
  8739. var i = payloadOffset + offset; // add an entry that maps the elementary_pid to the stream_type
  8740. programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i]; // move to the next table entry
  8741. // skip past the elementary stream descriptors, if present
  8742. offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
  8743. }
  8744. return programMapTable;
  8745. };
  8746. var parsePesType = function parsePesType(packet, programMapTable) {
  8747. var pid = parsePid(packet);
  8748. var type = programMapTable[pid];
  8749. switch (type) {
  8750. case streamTypes.H264_STREAM_TYPE:
  8751. return 'video';
  8752. case streamTypes.ADTS_STREAM_TYPE:
  8753. return 'audio';
  8754. case streamTypes.METADATA_STREAM_TYPE:
  8755. return 'timed-metadata';
  8756. default:
  8757. return null;
  8758. }
  8759. };
  8760. var parsePesTime = function parsePesTime(packet) {
  8761. var pusi = parsePayloadUnitStartIndicator(packet);
  8762. if (!pusi) {
  8763. return null;
  8764. }
  8765. var offset = 4 + parseAdaptionField(packet);
  8766. if (offset >= packet.byteLength) {
  8767. // From the H 222.0 MPEG-TS spec
  8768. // "For transport stream packets carrying PES packets, stuffing is needed when there
  8769. // is insufficient PES packet data to completely fill the transport stream packet
  8770. // payload bytes. Stuffing is accomplished by defining an adaptation field longer than
  8771. // the sum of the lengths of the data elements in it, so that the payload bytes
  8772. // remaining after the adaptation field exactly accommodates the available PES packet
  8773. // data."
  8774. //
  8775. // If the offset is >= the length of the packet, then the packet contains no data
  8776. // and instead is just adaption field stuffing bytes
  8777. return null;
  8778. }
  8779. var pes = null;
  8780. var ptsDtsFlags; // PES packets may be annotated with a PTS value, or a PTS value
  8781. // and a DTS value. Determine what combination of values is
  8782. // available to work with.
  8783. ptsDtsFlags = packet[offset + 7]; // PTS and DTS are normally stored as a 33-bit number. Javascript
  8784. // performs all bitwise operations on 32-bit integers but javascript
  8785. // supports a much greater range (52-bits) of integer using standard
  8786. // mathematical operations.
  8787. // We construct a 31-bit value using bitwise operators over the 31
  8788. // most significant bits and then multiply by 4 (equal to a left-shift
  8789. // of 2) before we add the final 2 least significant bits of the
  8790. // timestamp (equal to an OR.)
  8791. if (ptsDtsFlags & 0xC0) {
  8792. pes = {}; // the PTS and DTS are not written out directly. For information
  8793. // on how they are encoded, see
  8794. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  8795. pes.pts = (packet[offset + 9] & 0x0E) << 27 | (packet[offset + 10] & 0xFF) << 20 | (packet[offset + 11] & 0xFE) << 12 | (packet[offset + 12] & 0xFF) << 5 | (packet[offset + 13] & 0xFE) >>> 3;
  8796. pes.pts *= 4; // Left shift by 2
  8797. pes.pts += (packet[offset + 13] & 0x06) >>> 1; // OR by the two LSBs
  8798. pes.dts = pes.pts;
  8799. if (ptsDtsFlags & 0x40) {
  8800. pes.dts = (packet[offset + 14] & 0x0E) << 27 | (packet[offset + 15] & 0xFF) << 20 | (packet[offset + 16] & 0xFE) << 12 | (packet[offset + 17] & 0xFF) << 5 | (packet[offset + 18] & 0xFE) >>> 3;
  8801. pes.dts *= 4; // Left shift by 2
  8802. pes.dts += (packet[offset + 18] & 0x06) >>> 1; // OR by the two LSBs
  8803. }
  8804. }
  8805. return pes;
  8806. };
  8807. var parseNalUnitType = function parseNalUnitType(type) {
  8808. switch (type) {
  8809. case 0x05:
  8810. return 'slice_layer_without_partitioning_rbsp_idr';
  8811. case 0x06:
  8812. return 'sei_rbsp';
  8813. case 0x07:
  8814. return 'seq_parameter_set_rbsp';
  8815. case 0x08:
  8816. return 'pic_parameter_set_rbsp';
  8817. case 0x09:
  8818. return 'access_unit_delimiter_rbsp';
  8819. default:
  8820. return null;
  8821. }
  8822. };
  8823. var videoPacketContainsKeyFrame = function videoPacketContainsKeyFrame(packet) {
  8824. var offset = 4 + parseAdaptionField(packet);
  8825. var frameBuffer = packet.subarray(offset);
  8826. var frameI = 0;
  8827. var frameSyncPoint = 0;
  8828. var foundKeyFrame = false;
  8829. var nalType; // advance the sync point to a NAL start, if necessary
  8830. for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
  8831. if (frameBuffer[frameSyncPoint + 2] === 1) {
  8832. // the sync point is properly aligned
  8833. frameI = frameSyncPoint + 5;
  8834. break;
  8835. }
  8836. }
  8837. while (frameI < frameBuffer.byteLength) {
  8838. // look at the current byte to determine if we've hit the end of
  8839. // a NAL unit boundary
  8840. switch (frameBuffer[frameI]) {
  8841. case 0:
  8842. // skip past non-sync sequences
  8843. if (frameBuffer[frameI - 1] !== 0) {
  8844. frameI += 2;
  8845. break;
  8846. } else if (frameBuffer[frameI - 2] !== 0) {
  8847. frameI++;
  8848. break;
  8849. }
  8850. if (frameSyncPoint + 3 !== frameI - 2) {
  8851. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  8852. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  8853. foundKeyFrame = true;
  8854. }
  8855. } // drop trailing zeroes
  8856. do {
  8857. frameI++;
  8858. } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
  8859. frameSyncPoint = frameI - 2;
  8860. frameI += 3;
  8861. break;
  8862. case 1:
  8863. // skip past non-sync sequences
  8864. if (frameBuffer[frameI - 1] !== 0 || frameBuffer[frameI - 2] !== 0) {
  8865. frameI += 3;
  8866. break;
  8867. }
  8868. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  8869. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  8870. foundKeyFrame = true;
  8871. }
  8872. frameSyncPoint = frameI - 2;
  8873. frameI += 3;
  8874. break;
  8875. default:
  8876. // the current byte isn't a one or zero, so it cannot be part
  8877. // of a sync sequence
  8878. frameI += 3;
  8879. break;
  8880. }
  8881. }
  8882. frameBuffer = frameBuffer.subarray(frameSyncPoint);
  8883. frameI -= frameSyncPoint;
  8884. frameSyncPoint = 0; // parse the final nal
  8885. if (frameBuffer && frameBuffer.byteLength > 3) {
  8886. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  8887. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  8888. foundKeyFrame = true;
  8889. }
  8890. }
  8891. return foundKeyFrame;
  8892. };
  8893. var probe$1 = {
  8894. parseType: parseType,
  8895. parsePat: parsePat,
  8896. parsePmt: parsePmt,
  8897. parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
  8898. parsePesType: parsePesType,
  8899. parsePesTime: parsePesTime,
  8900. videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
  8901. };
  8902. var handleRollover = timestampRolloverStream.handleRollover;
  8903. var probe = {};
  8904. probe.ts = probe$1;
  8905. probe.aac = utils;
  8906. var ONE_SECOND_IN_TS = clock.ONE_SECOND_IN_TS;
  8907. var MP2T_PACKET_LENGTH = 188,
  8908. // bytes
  8909. SYNC_BYTE = 0x47;
  8910. /**
  8911. * walks through segment data looking for pat and pmt packets to parse out
  8912. * program map table information
  8913. */
  8914. var parsePsi_ = function parsePsi_(bytes, pmt) {
  8915. var startIndex = 0,
  8916. endIndex = MP2T_PACKET_LENGTH,
  8917. packet,
  8918. type;
  8919. while (endIndex < bytes.byteLength) {
  8920. // Look for a pair of start and end sync bytes in the data..
  8921. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  8922. // We found a packet
  8923. packet = bytes.subarray(startIndex, endIndex);
  8924. type = probe.ts.parseType(packet, pmt.pid);
  8925. switch (type) {
  8926. case 'pat':
  8927. pmt.pid = probe.ts.parsePat(packet);
  8928. break;
  8929. case 'pmt':
  8930. var table = probe.ts.parsePmt(packet);
  8931. pmt.table = pmt.table || {};
  8932. Object.keys(table).forEach(function (key) {
  8933. pmt.table[key] = table[key];
  8934. });
  8935. break;
  8936. }
  8937. startIndex += MP2T_PACKET_LENGTH;
  8938. endIndex += MP2T_PACKET_LENGTH;
  8939. continue;
  8940. } // If we get here, we have somehow become de-synchronized and we need to step
  8941. // forward one byte at a time until we find a pair of sync bytes that denote
  8942. // a packet
  8943. startIndex++;
  8944. endIndex++;
  8945. }
  8946. };
  8947. /**
  8948. * walks through the segment data from the start and end to get timing information
  8949. * for the first and last audio pes packets
  8950. */
  8951. var parseAudioPes_ = function parseAudioPes_(bytes, pmt, result) {
  8952. var startIndex = 0,
  8953. endIndex = MP2T_PACKET_LENGTH,
  8954. packet,
  8955. type,
  8956. pesType,
  8957. pusi,
  8958. parsed;
  8959. var endLoop = false; // Start walking from start of segment to get first audio packet
  8960. while (endIndex <= bytes.byteLength) {
  8961. // Look for a pair of start and end sync bytes in the data..
  8962. if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
  8963. // We found a packet
  8964. packet = bytes.subarray(startIndex, endIndex);
  8965. type = probe.ts.parseType(packet, pmt.pid);
  8966. switch (type) {
  8967. case 'pes':
  8968. pesType = probe.ts.parsePesType(packet, pmt.table);
  8969. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  8970. if (pesType === 'audio' && pusi) {
  8971. parsed = probe.ts.parsePesTime(packet);
  8972. if (parsed) {
  8973. parsed.type = 'audio';
  8974. result.audio.push(parsed);
  8975. endLoop = true;
  8976. }
  8977. }
  8978. break;
  8979. }
  8980. if (endLoop) {
  8981. break;
  8982. }
  8983. startIndex += MP2T_PACKET_LENGTH;
  8984. endIndex += MP2T_PACKET_LENGTH;
  8985. continue;
  8986. } // If we get here, we have somehow become de-synchronized and we need to step
  8987. // forward one byte at a time until we find a pair of sync bytes that denote
  8988. // a packet
  8989. startIndex++;
  8990. endIndex++;
  8991. } // Start walking from end of segment to get last audio packet
  8992. endIndex = bytes.byteLength;
  8993. startIndex = endIndex - MP2T_PACKET_LENGTH;
  8994. endLoop = false;
  8995. while (startIndex >= 0) {
  8996. // Look for a pair of start and end sync bytes in the data..
  8997. if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
  8998. // We found a packet
  8999. packet = bytes.subarray(startIndex, endIndex);
  9000. type = probe.ts.parseType(packet, pmt.pid);
  9001. switch (type) {
  9002. case 'pes':
  9003. pesType = probe.ts.parsePesType(packet, pmt.table);
  9004. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  9005. if (pesType === 'audio' && pusi) {
  9006. parsed = probe.ts.parsePesTime(packet);
  9007. if (parsed) {
  9008. parsed.type = 'audio';
  9009. result.audio.push(parsed);
  9010. endLoop = true;
  9011. }
  9012. }
  9013. break;
  9014. }
  9015. if (endLoop) {
  9016. break;
  9017. }
  9018. startIndex -= MP2T_PACKET_LENGTH;
  9019. endIndex -= MP2T_PACKET_LENGTH;
  9020. continue;
  9021. } // If we get here, we have somehow become de-synchronized and we need to step
  9022. // forward one byte at a time until we find a pair of sync bytes that denote
  9023. // a packet
  9024. startIndex--;
  9025. endIndex--;
  9026. }
  9027. };
  9028. /**
  9029. * walks through the segment data from the start and end to get timing information
  9030. * for the first and last video pes packets as well as timing information for the first
  9031. * key frame.
  9032. */
  9033. var parseVideoPes_ = function parseVideoPes_(bytes, pmt, result) {
  9034. var startIndex = 0,
  9035. endIndex = MP2T_PACKET_LENGTH,
  9036. packet,
  9037. type,
  9038. pesType,
  9039. pusi,
  9040. parsed,
  9041. frame,
  9042. i,
  9043. pes;
  9044. var endLoop = false;
  9045. var currentFrame = {
  9046. data: [],
  9047. size: 0
  9048. }; // Start walking from start of segment to get first video packet
  9049. while (endIndex < bytes.byteLength) {
  9050. // Look for a pair of start and end sync bytes in the data..
  9051. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  9052. // We found a packet
  9053. packet = bytes.subarray(startIndex, endIndex);
  9054. type = probe.ts.parseType(packet, pmt.pid);
  9055. switch (type) {
  9056. case 'pes':
  9057. pesType = probe.ts.parsePesType(packet, pmt.table);
  9058. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  9059. if (pesType === 'video') {
  9060. if (pusi && !endLoop) {
  9061. parsed = probe.ts.parsePesTime(packet);
  9062. if (parsed) {
  9063. parsed.type = 'video';
  9064. result.video.push(parsed);
  9065. endLoop = true;
  9066. }
  9067. }
  9068. if (!result.firstKeyFrame) {
  9069. if (pusi) {
  9070. if (currentFrame.size !== 0) {
  9071. frame = new Uint8Array(currentFrame.size);
  9072. i = 0;
  9073. while (currentFrame.data.length) {
  9074. pes = currentFrame.data.shift();
  9075. frame.set(pes, i);
  9076. i += pes.byteLength;
  9077. }
  9078. if (probe.ts.videoPacketContainsKeyFrame(frame)) {
  9079. var firstKeyFrame = probe.ts.parsePesTime(frame); // PTS/DTS may not be available. Simply *not* setting
  9080. // the keyframe seems to work fine with HLS playback
  9081. // and definitely preferable to a crash with TypeError...
  9082. if (firstKeyFrame) {
  9083. result.firstKeyFrame = firstKeyFrame;
  9084. result.firstKeyFrame.type = 'video';
  9085. } else {
  9086. // eslint-disable-next-line
  9087. console.warn('Failed to extract PTS/DTS from PES at first keyframe. ' + 'This could be an unusual TS segment, or else mux.js did not ' + 'parse your TS segment correctly. If you know your TS ' + 'segments do contain PTS/DTS on keyframes please file a bug ' + 'report! You can try ffprobe to double check for yourself.');
  9088. }
  9089. }
  9090. currentFrame.size = 0;
  9091. }
  9092. }
  9093. currentFrame.data.push(packet);
  9094. currentFrame.size += packet.byteLength;
  9095. }
  9096. }
  9097. break;
  9098. }
  9099. if (endLoop && result.firstKeyFrame) {
  9100. break;
  9101. }
  9102. startIndex += MP2T_PACKET_LENGTH;
  9103. endIndex += MP2T_PACKET_LENGTH;
  9104. continue;
  9105. } // If we get here, we have somehow become de-synchronized and we need to step
  9106. // forward one byte at a time until we find a pair of sync bytes that denote
  9107. // a packet
  9108. startIndex++;
  9109. endIndex++;
  9110. } // Start walking from end of segment to get last video packet
  9111. endIndex = bytes.byteLength;
  9112. startIndex = endIndex - MP2T_PACKET_LENGTH;
  9113. endLoop = false;
  9114. while (startIndex >= 0) {
  9115. // Look for a pair of start and end sync bytes in the data..
  9116. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  9117. // We found a packet
  9118. packet = bytes.subarray(startIndex, endIndex);
  9119. type = probe.ts.parseType(packet, pmt.pid);
  9120. switch (type) {
  9121. case 'pes':
  9122. pesType = probe.ts.parsePesType(packet, pmt.table);
  9123. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  9124. if (pesType === 'video' && pusi) {
  9125. parsed = probe.ts.parsePesTime(packet);
  9126. if (parsed) {
  9127. parsed.type = 'video';
  9128. result.video.push(parsed);
  9129. endLoop = true;
  9130. }
  9131. }
  9132. break;
  9133. }
  9134. if (endLoop) {
  9135. break;
  9136. }
  9137. startIndex -= MP2T_PACKET_LENGTH;
  9138. endIndex -= MP2T_PACKET_LENGTH;
  9139. continue;
  9140. } // If we get here, we have somehow become de-synchronized and we need to step
  9141. // forward one byte at a time until we find a pair of sync bytes that denote
  9142. // a packet
  9143. startIndex--;
  9144. endIndex--;
  9145. }
  9146. };
  9147. /**
  9148. * Adjusts the timestamp information for the segment to account for
  9149. * rollover and convert to seconds based on pes packet timescale (90khz clock)
  9150. */
  9151. var adjustTimestamp_ = function adjustTimestamp_(segmentInfo, baseTimestamp) {
  9152. if (segmentInfo.audio && segmentInfo.audio.length) {
  9153. var audioBaseTimestamp = baseTimestamp;
  9154. if (typeof audioBaseTimestamp === 'undefined' || isNaN(audioBaseTimestamp)) {
  9155. audioBaseTimestamp = segmentInfo.audio[0].dts;
  9156. }
  9157. segmentInfo.audio.forEach(function (info) {
  9158. info.dts = handleRollover(info.dts, audioBaseTimestamp);
  9159. info.pts = handleRollover(info.pts, audioBaseTimestamp); // time in seconds
  9160. info.dtsTime = info.dts / ONE_SECOND_IN_TS;
  9161. info.ptsTime = info.pts / ONE_SECOND_IN_TS;
  9162. });
  9163. }
  9164. if (segmentInfo.video && segmentInfo.video.length) {
  9165. var videoBaseTimestamp = baseTimestamp;
  9166. if (typeof videoBaseTimestamp === 'undefined' || isNaN(videoBaseTimestamp)) {
  9167. videoBaseTimestamp = segmentInfo.video[0].dts;
  9168. }
  9169. segmentInfo.video.forEach(function (info) {
  9170. info.dts = handleRollover(info.dts, videoBaseTimestamp);
  9171. info.pts = handleRollover(info.pts, videoBaseTimestamp); // time in seconds
  9172. info.dtsTime = info.dts / ONE_SECOND_IN_TS;
  9173. info.ptsTime = info.pts / ONE_SECOND_IN_TS;
  9174. });
  9175. if (segmentInfo.firstKeyFrame) {
  9176. var frame = segmentInfo.firstKeyFrame;
  9177. frame.dts = handleRollover(frame.dts, videoBaseTimestamp);
  9178. frame.pts = handleRollover(frame.pts, videoBaseTimestamp); // time in seconds
  9179. frame.dtsTime = frame.dts / ONE_SECOND_IN_TS;
  9180. frame.ptsTime = frame.pts / ONE_SECOND_IN_TS;
  9181. }
  9182. }
  9183. };
  9184. /**
  9185. * inspects the aac data stream for start and end time information
  9186. */
  9187. var inspectAac_ = function inspectAac_(bytes) {
  9188. var endLoop = false,
  9189. audioCount = 0,
  9190. sampleRate = null,
  9191. timestamp = null,
  9192. frameSize = 0,
  9193. byteIndex = 0,
  9194. packet;
  9195. while (bytes.length - byteIndex >= 3) {
  9196. var type = probe.aac.parseType(bytes, byteIndex);
  9197. switch (type) {
  9198. case 'timed-metadata':
  9199. // Exit early because we don't have enough to parse
  9200. // the ID3 tag header
  9201. if (bytes.length - byteIndex < 10) {
  9202. endLoop = true;
  9203. break;
  9204. }
  9205. frameSize = probe.aac.parseId3TagSize(bytes, byteIndex); // Exit early if we don't have enough in the buffer
  9206. // to emit a full packet
  9207. if (frameSize > bytes.length) {
  9208. endLoop = true;
  9209. break;
  9210. }
  9211. if (timestamp === null) {
  9212. packet = bytes.subarray(byteIndex, byteIndex + frameSize);
  9213. timestamp = probe.aac.parseAacTimestamp(packet);
  9214. }
  9215. byteIndex += frameSize;
  9216. break;
  9217. case 'audio':
  9218. // Exit early because we don't have enough to parse
  9219. // the ADTS frame header
  9220. if (bytes.length - byteIndex < 7) {
  9221. endLoop = true;
  9222. break;
  9223. }
  9224. frameSize = probe.aac.parseAdtsSize(bytes, byteIndex); // Exit early if we don't have enough in the buffer
  9225. // to emit a full packet
  9226. if (frameSize > bytes.length) {
  9227. endLoop = true;
  9228. break;
  9229. }
  9230. if (sampleRate === null) {
  9231. packet = bytes.subarray(byteIndex, byteIndex + frameSize);
  9232. sampleRate = probe.aac.parseSampleRate(packet);
  9233. }
  9234. audioCount++;
  9235. byteIndex += frameSize;
  9236. break;
  9237. default:
  9238. byteIndex++;
  9239. break;
  9240. }
  9241. if (endLoop) {
  9242. return null;
  9243. }
  9244. }
  9245. if (sampleRate === null || timestamp === null) {
  9246. return null;
  9247. }
  9248. var audioTimescale = ONE_SECOND_IN_TS / sampleRate;
  9249. var result = {
  9250. audio: [{
  9251. type: 'audio',
  9252. dts: timestamp,
  9253. pts: timestamp
  9254. }, {
  9255. type: 'audio',
  9256. dts: timestamp + audioCount * 1024 * audioTimescale,
  9257. pts: timestamp + audioCount * 1024 * audioTimescale
  9258. }]
  9259. };
  9260. return result;
  9261. };
  9262. /**
  9263. * inspects the transport stream segment data for start and end time information
  9264. * of the audio and video tracks (when present) as well as the first key frame's
  9265. * start time.
  9266. */
  9267. var inspectTs_ = function inspectTs_(bytes) {
  9268. var pmt = {
  9269. pid: null,
  9270. table: null
  9271. };
  9272. var result = {};
  9273. parsePsi_(bytes, pmt);
  9274. for (var pid in pmt.table) {
  9275. if (pmt.table.hasOwnProperty(pid)) {
  9276. var type = pmt.table[pid];
  9277. switch (type) {
  9278. case streamTypes.H264_STREAM_TYPE:
  9279. result.video = [];
  9280. parseVideoPes_(bytes, pmt, result);
  9281. if (result.video.length === 0) {
  9282. delete result.video;
  9283. }
  9284. break;
  9285. case streamTypes.ADTS_STREAM_TYPE:
  9286. result.audio = [];
  9287. parseAudioPes_(bytes, pmt, result);
  9288. if (result.audio.length === 0) {
  9289. delete result.audio;
  9290. }
  9291. break;
  9292. }
  9293. }
  9294. }
  9295. return result;
  9296. };
  9297. /**
  9298. * Inspects segment byte data and returns an object with start and end timing information
  9299. *
  9300. * @param {Uint8Array} bytes The segment byte data
  9301. * @param {Number} baseTimestamp Relative reference timestamp used when adjusting frame
  9302. * timestamps for rollover. This value must be in 90khz clock.
  9303. * @return {Object} Object containing start and end frame timing info of segment.
  9304. */
  9305. var inspect = function inspect(bytes, baseTimestamp) {
  9306. var isAacData = probe.aac.isLikelyAacData(bytes);
  9307. var result;
  9308. if (isAacData) {
  9309. result = inspectAac_(bytes);
  9310. } else {
  9311. result = inspectTs_(bytes);
  9312. }
  9313. if (!result || !result.audio && !result.video) {
  9314. return null;
  9315. }
  9316. adjustTimestamp_(result, baseTimestamp);
  9317. return result;
  9318. };
  9319. var tsInspector = {
  9320. inspect: inspect,
  9321. parseAudioPes_: parseAudioPes_
  9322. };
  9323. var muxjs = {
  9324. codecs: codecs,
  9325. mp4: mp4,
  9326. flv: flv,
  9327. mp2t: m2ts,
  9328. partial: partial
  9329. }; // include all the tools when the full library is required
  9330. muxjs.mp4.tools = mp4Inspector;
  9331. muxjs.flv.tools = flvInspector;
  9332. muxjs.mp2t.tools = tsInspector;
  9333. var lib = muxjs;
  9334. return lib;
  9335. })));