mux-mp4.js 259 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101
  1. /*! @name mux.js @version 7.0.2 @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. var MAX_UINT32$1 = Math.pow(2, 32);
  10. var getUint64$3 = function getUint64(uint8) {
  11. var dv = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
  12. var value;
  13. if (dv.getBigUint64) {
  14. value = dv.getBigUint64(0);
  15. if (value < Number.MAX_SAFE_INTEGER) {
  16. return Number(value);
  17. }
  18. return value;
  19. }
  20. return dv.getUint32(0) * MAX_UINT32$1 + dv.getUint32(4);
  21. };
  22. var numbers = {
  23. getUint64: getUint64$3,
  24. MAX_UINT32: MAX_UINT32$1
  25. };
  26. var MAX_UINT32 = numbers.MAX_UINT32;
  27. 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
  28. (function () {
  29. var i;
  30. types = {
  31. avc1: [],
  32. // codingname
  33. avcC: [],
  34. btrt: [],
  35. dinf: [],
  36. dref: [],
  37. esds: [],
  38. ftyp: [],
  39. hdlr: [],
  40. mdat: [],
  41. mdhd: [],
  42. mdia: [],
  43. mfhd: [],
  44. minf: [],
  45. moof: [],
  46. moov: [],
  47. mp4a: [],
  48. // codingname
  49. mvex: [],
  50. mvhd: [],
  51. pasp: [],
  52. sdtp: [],
  53. smhd: [],
  54. stbl: [],
  55. stco: [],
  56. stsc: [],
  57. stsd: [],
  58. stsz: [],
  59. stts: [],
  60. styp: [],
  61. tfdt: [],
  62. tfhd: [],
  63. traf: [],
  64. trak: [],
  65. trun: [],
  66. trex: [],
  67. tkhd: [],
  68. vmhd: []
  69. }; // In environments where Uint8Array is undefined (e.g., IE8), skip set up so that we
  70. // don't throw an error
  71. if (typeof Uint8Array === 'undefined') {
  72. return;
  73. }
  74. for (i in types) {
  75. if (types.hasOwnProperty(i)) {
  76. types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
  77. }
  78. }
  79. MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
  80. AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
  81. MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
  82. VIDEO_HDLR = new Uint8Array([0x00, // version 0
  83. 0x00, 0x00, 0x00, // flags
  84. 0x00, 0x00, 0x00, 0x00, // pre_defined
  85. 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
  86. 0x00, 0x00, 0x00, 0x00, // reserved
  87. 0x00, 0x00, 0x00, 0x00, // reserved
  88. 0x00, 0x00, 0x00, 0x00, // reserved
  89. 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
  90. ]);
  91. AUDIO_HDLR = new Uint8Array([0x00, // version 0
  92. 0x00, 0x00, 0x00, // flags
  93. 0x00, 0x00, 0x00, 0x00, // pre_defined
  94. 0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
  95. 0x00, 0x00, 0x00, 0x00, // reserved
  96. 0x00, 0x00, 0x00, 0x00, // reserved
  97. 0x00, 0x00, 0x00, 0x00, // reserved
  98. 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
  99. ]);
  100. HDLR_TYPES = {
  101. video: VIDEO_HDLR,
  102. audio: AUDIO_HDLR
  103. };
  104. DREF = new Uint8Array([0x00, // version 0
  105. 0x00, 0x00, 0x00, // flags
  106. 0x00, 0x00, 0x00, 0x01, // entry_count
  107. 0x00, 0x00, 0x00, 0x0c, // entry_size
  108. 0x75, 0x72, 0x6c, 0x20, // 'url' type
  109. 0x00, // version 0
  110. 0x00, 0x00, 0x01 // entry_flags
  111. ]);
  112. SMHD = new Uint8Array([0x00, // version
  113. 0x00, 0x00, 0x00, // flags
  114. 0x00, 0x00, // balance, 0 means centered
  115. 0x00, 0x00 // reserved
  116. ]);
  117. STCO = new Uint8Array([0x00, // version
  118. 0x00, 0x00, 0x00, // flags
  119. 0x00, 0x00, 0x00, 0x00 // entry_count
  120. ]);
  121. STSC = STCO;
  122. STSZ = new Uint8Array([0x00, // version
  123. 0x00, 0x00, 0x00, // flags
  124. 0x00, 0x00, 0x00, 0x00, // sample_size
  125. 0x00, 0x00, 0x00, 0x00 // sample_count
  126. ]);
  127. STTS = STCO;
  128. VMHD = new Uint8Array([0x00, // version
  129. 0x00, 0x00, 0x01, // flags
  130. 0x00, 0x00, // graphicsmode
  131. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
  132. ]);
  133. })();
  134. box = function box(type) {
  135. var payload = [],
  136. size = 0,
  137. i,
  138. result,
  139. view;
  140. for (i = 1; i < arguments.length; i++) {
  141. payload.push(arguments[i]);
  142. }
  143. i = payload.length; // calculate the total size we need to allocate
  144. while (i--) {
  145. size += payload[i].byteLength;
  146. }
  147. result = new Uint8Array(size + 8);
  148. view = new DataView(result.buffer, result.byteOffset, result.byteLength);
  149. view.setUint32(0, result.byteLength);
  150. result.set(type, 4); // copy the payload into the result
  151. for (i = 0, size = 8; i < payload.length; i++) {
  152. result.set(payload[i], size);
  153. size += payload[i].byteLength;
  154. }
  155. return result;
  156. };
  157. dinf = function dinf() {
  158. return box(types.dinf, box(types.dref, DREF));
  159. };
  160. esds = function esds(track) {
  161. return box(types.esds, new Uint8Array([0x00, // version
  162. 0x00, 0x00, 0x00, // flags
  163. // ES_Descriptor
  164. 0x03, // tag, ES_DescrTag
  165. 0x19, // length
  166. 0x00, 0x00, // ES_ID
  167. 0x00, // streamDependenceFlag, URL_flag, reserved, streamPriority
  168. // DecoderConfigDescriptor
  169. 0x04, // tag, DecoderConfigDescrTag
  170. 0x11, // length
  171. 0x40, // object type
  172. 0x15, // streamType
  173. 0x00, 0x06, 0x00, // bufferSizeDB
  174. 0x00, 0x00, 0xda, 0xc0, // maxBitrate
  175. 0x00, 0x00, 0xda, 0xc0, // avgBitrate
  176. // DecoderSpecificInfo
  177. 0x05, // tag, DecoderSpecificInfoTag
  178. 0x02, // length
  179. // ISO/IEC 14496-3, AudioSpecificConfig
  180. // for samplingFrequencyIndex see ISO/IEC 13818-7:2006, 8.1.3.2.2, Table 35
  181. track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02 // GASpecificConfig
  182. ]));
  183. };
  184. ftyp = function ftyp() {
  185. return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
  186. };
  187. hdlr = function hdlr(type) {
  188. return box(types.hdlr, HDLR_TYPES[type]);
  189. };
  190. mdat = function mdat(data) {
  191. return box(types.mdat, data);
  192. };
  193. mdhd = function mdhd(track) {
  194. var result = new Uint8Array([0x00, // version 0
  195. 0x00, 0x00, 0x00, // flags
  196. 0x00, 0x00, 0x00, 0x02, // creation_time
  197. 0x00, 0x00, 0x00, 0x03, // modification_time
  198. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  199. track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF, // duration
  200. 0x55, 0xc4, // 'und' language (undetermined)
  201. 0x00, 0x00]); // Use the sample rate from the track metadata, when it is
  202. // defined. The sample rate can be parsed out of an ADTS header, for
  203. // instance.
  204. if (track.samplerate) {
  205. result[12] = track.samplerate >>> 24 & 0xFF;
  206. result[13] = track.samplerate >>> 16 & 0xFF;
  207. result[14] = track.samplerate >>> 8 & 0xFF;
  208. result[15] = track.samplerate & 0xFF;
  209. }
  210. return box(types.mdhd, result);
  211. };
  212. mdia = function mdia(track) {
  213. return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
  214. };
  215. mfhd = function mfhd(sequenceNumber) {
  216. return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // flags
  217. (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF // sequence_number
  218. ]));
  219. };
  220. minf = function minf(track) {
  221. return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
  222. };
  223. moof = function moof(sequenceNumber, tracks) {
  224. var trackFragments = [],
  225. i = tracks.length; // build traf boxes for each track fragment
  226. while (i--) {
  227. trackFragments[i] = traf(tracks[i]);
  228. }
  229. return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
  230. };
  231. /**
  232. * Returns a movie box.
  233. * @param tracks {array} the tracks associated with this movie
  234. * @see ISO/IEC 14496-12:2012(E), section 8.2.1
  235. */
  236. moov = function moov(tracks) {
  237. var i = tracks.length,
  238. boxes = [];
  239. while (i--) {
  240. boxes[i] = trak(tracks[i]);
  241. }
  242. return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
  243. };
  244. mvex = function mvex(tracks) {
  245. var i = tracks.length,
  246. boxes = [];
  247. while (i--) {
  248. boxes[i] = trex(tracks[i]);
  249. }
  250. return box.apply(null, [types.mvex].concat(boxes));
  251. };
  252. mvhd = function mvhd(duration) {
  253. var bytes = new Uint8Array([0x00, // version 0
  254. 0x00, 0x00, 0x00, // flags
  255. 0x00, 0x00, 0x00, 0x01, // creation_time
  256. 0x00, 0x00, 0x00, 0x02, // modification_time
  257. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  258. (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF, // duration
  259. 0x00, 0x01, 0x00, 0x00, // 1.0 rate
  260. 0x01, 0x00, // 1.0 volume
  261. 0x00, 0x00, // reserved
  262. 0x00, 0x00, 0x00, 0x00, // reserved
  263. 0x00, 0x00, 0x00, 0x00, // reserved
  264. 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
  265. 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
  266. 0xff, 0xff, 0xff, 0xff // next_track_ID
  267. ]);
  268. return box(types.mvhd, bytes);
  269. };
  270. sdtp = function sdtp(track) {
  271. var samples = track.samples || [],
  272. bytes = new Uint8Array(4 + samples.length),
  273. flags,
  274. i; // leave the full box header (4 bytes) all zero
  275. // write the sample table
  276. for (i = 0; i < samples.length; i++) {
  277. flags = samples[i].flags;
  278. bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
  279. }
  280. return box(types.sdtp, bytes);
  281. };
  282. stbl = function stbl(track) {
  283. return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
  284. };
  285. (function () {
  286. var videoSample, audioSample;
  287. stsd = function stsd(track) {
  288. return box(types.stsd, new Uint8Array([0x00, // version 0
  289. 0x00, 0x00, 0x00, // flags
  290. 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
  291. };
  292. videoSample = function videoSample(track) {
  293. var sps = track.sps || [],
  294. pps = track.pps || [],
  295. sequenceParameterSets = [],
  296. pictureParameterSets = [],
  297. i,
  298. avc1Box; // assemble the SPSs
  299. for (i = 0; i < sps.length; i++) {
  300. sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
  301. sequenceParameterSets.push(sps[i].byteLength & 0xFF); // sequenceParameterSetLength
  302. sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i])); // SPS
  303. } // assemble the PPSs
  304. for (i = 0; i < pps.length; i++) {
  305. pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
  306. pictureParameterSets.push(pps[i].byteLength & 0xFF);
  307. pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
  308. }
  309. avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  310. 0x00, 0x01, // data_reference_index
  311. 0x00, 0x00, // pre_defined
  312. 0x00, 0x00, // reserved
  313. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  314. (track.width & 0xff00) >> 8, track.width & 0xff, // width
  315. (track.height & 0xff00) >> 8, track.height & 0xff, // height
  316. 0x00, 0x48, 0x00, 0x00, // horizresolution
  317. 0x00, 0x48, 0x00, 0x00, // vertresolution
  318. 0x00, 0x00, 0x00, 0x00, // reserved
  319. 0x00, 0x01, // frame_count
  320. 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
  321. 0x00, 0x18, // depth = 24
  322. 0x11, 0x11 // pre_defined = -1
  323. ]), box(types.avcC, new Uint8Array([0x01, // configurationVersion
  324. track.profileIdc, // AVCProfileIndication
  325. track.profileCompatibility, // profile_compatibility
  326. track.levelIdc, // AVCLevelIndication
  327. 0xff // lengthSizeMinusOne, hard-coded to 4 bytes
  328. ].concat([sps.length], // numOfSequenceParameterSets
  329. sequenceParameterSets, // "SPS"
  330. [pps.length], // numOfPictureParameterSets
  331. pictureParameterSets // "PPS"
  332. ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
  333. 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
  334. 0x00, 0x2d, 0xc6, 0xc0 // avgBitrate
  335. ]))];
  336. if (track.sarRatio) {
  337. var hSpacing = track.sarRatio[0],
  338. vSpacing = track.sarRatio[1];
  339. 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])));
  340. }
  341. return box.apply(null, avc1Box);
  342. };
  343. audioSample = function audioSample(track) {
  344. return box(types.mp4a, new Uint8Array([// SampleEntry, ISO/IEC 14496-12
  345. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  346. 0x00, 0x01, // data_reference_index
  347. // AudioSampleEntry, ISO/IEC 14496-12
  348. 0x00, 0x00, 0x00, 0x00, // reserved
  349. 0x00, 0x00, 0x00, 0x00, // reserved
  350. (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff, // channelcount
  351. (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff, // samplesize
  352. 0x00, 0x00, // pre_defined
  353. 0x00, 0x00, // reserved
  354. (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00 // samplerate, 16.16
  355. // MP4AudioSampleEntry, ISO/IEC 14496-14
  356. ]), esds(track));
  357. };
  358. })();
  359. tkhd = function tkhd(track) {
  360. var result = new Uint8Array([0x00, // version 0
  361. 0x00, 0x00, 0x07, // flags
  362. 0x00, 0x00, 0x00, 0x00, // creation_time
  363. 0x00, 0x00, 0x00, 0x00, // modification_time
  364. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  365. 0x00, 0x00, 0x00, 0x00, // reserved
  366. (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF, // duration
  367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  368. 0x00, 0x00, // layer
  369. 0x00, 0x00, // alternate_group
  370. 0x01, 0x00, // non-audio track volume
  371. 0x00, 0x00, // reserved
  372. 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
  373. (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00, // width
  374. (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00 // height
  375. ]);
  376. return box(types.tkhd, result);
  377. };
  378. /**
  379. * Generate a track fragment (traf) box. A traf box collects metadata
  380. * about tracks in a movie fragment (moof) box.
  381. */
  382. traf = function traf(track) {
  383. var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
  384. trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00, // version 0
  385. 0x00, 0x00, 0x3a, // flags
  386. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  387. 0x00, 0x00, 0x00, 0x01, // sample_description_index
  388. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  389. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  390. 0x00, 0x00, 0x00, 0x00 // default_sample_flags
  391. ]));
  392. upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / MAX_UINT32);
  393. lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % MAX_UINT32);
  394. trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01, // version 1
  395. 0x00, 0x00, 0x00, // flags
  396. // baseMediaDecodeTime
  397. 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
  398. // the containing moof to the first payload byte of the associated
  399. // mdat
  400. dataOffset = 32 + // tfhd
  401. 20 + // tfdt
  402. 8 + // traf header
  403. 16 + // mfhd
  404. 8 + // moof header
  405. 8; // mdat header
  406. // audio tracks require less metadata
  407. if (track.type === 'audio') {
  408. trackFragmentRun = trun$1(track, dataOffset);
  409. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
  410. } // video tracks should contain an independent and disposable samples
  411. // box (sdtp)
  412. // generate one and adjust offsets to match
  413. sampleDependencyTable = sdtp(track);
  414. trackFragmentRun = trun$1(track, sampleDependencyTable.length + dataOffset);
  415. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
  416. };
  417. /**
  418. * Generate a track box.
  419. * @param track {object} a track definition
  420. * @return {Uint8Array} the track box
  421. */
  422. trak = function trak(track) {
  423. track.duration = track.duration || 0xffffffff;
  424. return box(types.trak, tkhd(track), mdia(track));
  425. };
  426. trex = function trex(track) {
  427. var result = new Uint8Array([0x00, // version 0
  428. 0x00, 0x00, 0x00, // flags
  429. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  430. 0x00, 0x00, 0x00, 0x01, // default_sample_description_index
  431. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  432. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  433. 0x00, 0x01, 0x00, 0x01 // default_sample_flags
  434. ]); // the last two bytes of default_sample_flags is the sample
  435. // degradation priority, a hint about the importance of this sample
  436. // relative to others. Lower the degradation priority for all sample
  437. // types other than video.
  438. if (track.type !== 'video') {
  439. result[result.length - 1] = 0x00;
  440. }
  441. return box(types.trex, result);
  442. };
  443. (function () {
  444. var audioTrun, videoTrun, trunHeader; // This method assumes all samples are uniform. That is, if a
  445. // duration is present for the first sample, it will be present for
  446. // all subsequent samples.
  447. // see ISO/IEC 14496-12:2012, Section 8.8.8.1
  448. trunHeader = function trunHeader(samples, offset) {
  449. var durationPresent = 0,
  450. sizePresent = 0,
  451. flagsPresent = 0,
  452. compositionTimeOffset = 0; // trun flag constants
  453. if (samples.length) {
  454. if (samples[0].duration !== undefined) {
  455. durationPresent = 0x1;
  456. }
  457. if (samples[0].size !== undefined) {
  458. sizePresent = 0x2;
  459. }
  460. if (samples[0].flags !== undefined) {
  461. flagsPresent = 0x4;
  462. }
  463. if (samples[0].compositionTimeOffset !== undefined) {
  464. compositionTimeOffset = 0x8;
  465. }
  466. }
  467. return [0x00, // version 0
  468. 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01, // flags
  469. (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF, // sample_count
  470. (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF // data_offset
  471. ];
  472. };
  473. videoTrun = function videoTrun(track, offset) {
  474. var bytesOffest, bytes, header, samples, sample, i;
  475. samples = track.samples || [];
  476. offset += 8 + 12 + 16 * samples.length;
  477. header = trunHeader(samples, offset);
  478. bytes = new Uint8Array(header.length + samples.length * 16);
  479. bytes.set(header);
  480. bytesOffest = header.length;
  481. for (i = 0; i < samples.length; i++) {
  482. sample = samples[i];
  483. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  484. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  485. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  486. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  487. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  488. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  489. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  490. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  491. bytes[bytesOffest++] = sample.flags.isLeading << 2 | sample.flags.dependsOn;
  492. bytes[bytesOffest++] = sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample;
  493. bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
  494. bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F; // sample_flags
  495. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
  496. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
  497. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
  498. bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF; // sample_composition_time_offset
  499. }
  500. return box(types.trun, bytes);
  501. };
  502. audioTrun = function audioTrun(track, offset) {
  503. var bytes, bytesOffest, header, samples, sample, i;
  504. samples = track.samples || [];
  505. offset += 8 + 12 + 8 * samples.length;
  506. header = trunHeader(samples, offset);
  507. bytes = new Uint8Array(header.length + samples.length * 8);
  508. bytes.set(header);
  509. bytesOffest = header.length;
  510. for (i = 0; i < samples.length; i++) {
  511. sample = samples[i];
  512. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  513. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  514. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  515. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  516. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  517. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  518. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  519. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  520. }
  521. return box(types.trun, bytes);
  522. };
  523. trun$1 = function trun(track, offset) {
  524. if (track.type === 'audio') {
  525. return audioTrun(track, offset);
  526. }
  527. return videoTrun(track, offset);
  528. };
  529. })();
  530. var mp4Generator = {
  531. ftyp: ftyp,
  532. mdat: mdat,
  533. moof: moof,
  534. moov: moov,
  535. initSegment: function initSegment(tracks) {
  536. var fileType = ftyp(),
  537. movie = moov(tracks),
  538. result;
  539. result = new Uint8Array(fileType.byteLength + movie.byteLength);
  540. result.set(fileType);
  541. result.set(movie, fileType.byteLength);
  542. return result;
  543. }
  544. };
  545. /**
  546. * mux.js
  547. *
  548. * Copyright (c) Brightcove
  549. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  550. */
  551. var toUnsigned$3 = function toUnsigned(value) {
  552. return value >>> 0;
  553. };
  554. var toHexString$1 = function toHexString(value) {
  555. return ('00' + value.toString(16)).slice(-2);
  556. };
  557. var bin = {
  558. toUnsigned: toUnsigned$3,
  559. toHexString: toHexString$1
  560. };
  561. var parseType$1 = function parseType(buffer) {
  562. var result = '';
  563. result += String.fromCharCode(buffer[0]);
  564. result += String.fromCharCode(buffer[1]);
  565. result += String.fromCharCode(buffer[2]);
  566. result += String.fromCharCode(buffer[3]);
  567. return result;
  568. };
  569. var parseType_1 = parseType$1;
  570. var toUnsigned$2 = bin.toUnsigned;
  571. var findBox = function findBox(data, path) {
  572. var results = [],
  573. i,
  574. size,
  575. type,
  576. end,
  577. subresults;
  578. if (!path.length) {
  579. // short-circuit the search for empty paths
  580. return null;
  581. }
  582. for (i = 0; i < data.byteLength;) {
  583. size = toUnsigned$2(data[i] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3]);
  584. type = parseType_1(data.subarray(i + 4, i + 8));
  585. end = size > 1 ? i + size : data.byteLength;
  586. if (type === path[0]) {
  587. if (path.length === 1) {
  588. // this is the end of the path and we've found the box we were
  589. // looking for
  590. results.push(data.subarray(i + 8, end));
  591. } else {
  592. // recursively search for the next box along the path
  593. subresults = findBox(data.subarray(i + 8, end), path.slice(1));
  594. if (subresults.length) {
  595. results = results.concat(subresults);
  596. }
  597. }
  598. }
  599. i = end;
  600. } // we've finished searching all of data
  601. return results;
  602. };
  603. var findBox_1 = findBox;
  604. /**
  605. * Returns the first string in the data array ending with a null char '\0'
  606. * @param {UInt8} data
  607. * @returns the string with the null char
  608. */
  609. var uint8ToCString$1 = function uint8ToCString(data) {
  610. var index = 0;
  611. var curChar = String.fromCharCode(data[index]);
  612. var retString = '';
  613. while (curChar !== '\0') {
  614. retString += curChar;
  615. index++;
  616. curChar = String.fromCharCode(data[index]);
  617. } // Add nullChar
  618. retString += curChar;
  619. return retString;
  620. };
  621. var string = {
  622. uint8ToCString: uint8ToCString$1
  623. };
  624. var uint8ToCString = string.uint8ToCString;
  625. var getUint64$2 = numbers.getUint64;
  626. /**
  627. * Based on: ISO/IEC 23009 Section: 5.10.3.3
  628. * References:
  629. * https://dashif-documents.azurewebsites.net/Events/master/event.html#emsg-format
  630. * https://aomediacodec.github.io/id3-emsg/
  631. *
  632. * Takes emsg box data as a uint8 array and returns a emsg box object
  633. * @param {UInt8Array} boxData data from emsg box
  634. * @returns A parsed emsg box object
  635. */
  636. var parseEmsgBox = function parseEmsgBox(boxData) {
  637. // version + flags
  638. var offset = 4;
  639. var version = boxData[0];
  640. var scheme_id_uri, value, timescale, presentation_time, presentation_time_delta, event_duration, id, message_data;
  641. if (version === 0) {
  642. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  643. offset += scheme_id_uri.length;
  644. value = uint8ToCString(boxData.subarray(offset));
  645. offset += value.length;
  646. var dv = new DataView(boxData.buffer);
  647. timescale = dv.getUint32(offset);
  648. offset += 4;
  649. presentation_time_delta = dv.getUint32(offset);
  650. offset += 4;
  651. event_duration = dv.getUint32(offset);
  652. offset += 4;
  653. id = dv.getUint32(offset);
  654. offset += 4;
  655. } else if (version === 1) {
  656. var dv = new DataView(boxData.buffer);
  657. timescale = dv.getUint32(offset);
  658. offset += 4;
  659. presentation_time = getUint64$2(boxData.subarray(offset));
  660. offset += 8;
  661. event_duration = dv.getUint32(offset);
  662. offset += 4;
  663. id = dv.getUint32(offset);
  664. offset += 4;
  665. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  666. offset += scheme_id_uri.length;
  667. value = uint8ToCString(boxData.subarray(offset));
  668. offset += value.length;
  669. }
  670. message_data = new Uint8Array(boxData.subarray(offset, boxData.byteLength));
  671. var emsgBox = {
  672. scheme_id_uri: scheme_id_uri,
  673. value: value,
  674. // if timescale is undefined or 0 set to 1
  675. timescale: timescale ? timescale : 1,
  676. presentation_time: presentation_time,
  677. presentation_time_delta: presentation_time_delta,
  678. event_duration: event_duration,
  679. id: id,
  680. message_data: message_data
  681. };
  682. return isValidEmsgBox(version, emsgBox) ? emsgBox : undefined;
  683. };
  684. /**
  685. * Scales a presentation time or time delta with an offset with a provided timescale
  686. * @param {number} presentationTime
  687. * @param {number} timescale
  688. * @param {number} timeDelta
  689. * @param {number} offset
  690. * @returns the scaled time as a number
  691. */
  692. var scaleTime = function scaleTime(presentationTime, timescale, timeDelta, offset) {
  693. return presentationTime || presentationTime === 0 ? presentationTime / timescale : offset + timeDelta / timescale;
  694. };
  695. /**
  696. * Checks the emsg box data for validity based on the version
  697. * @param {number} version of the emsg box to validate
  698. * @param {Object} emsg the emsg data to validate
  699. * @returns if the box is valid as a boolean
  700. */
  701. var isValidEmsgBox = function isValidEmsgBox(version, emsg) {
  702. var hasScheme = emsg.scheme_id_uri !== '\0';
  703. var isValidV0Box = version === 0 && isDefined(emsg.presentation_time_delta) && hasScheme;
  704. var isValidV1Box = version === 1 && isDefined(emsg.presentation_time) && hasScheme; // Only valid versions of emsg are 0 and 1
  705. return !(version > 1) && isValidV0Box || isValidV1Box;
  706. }; // Utility function to check if an object is defined
  707. var isDefined = function isDefined(data) {
  708. return data !== undefined || data !== null;
  709. };
  710. var emsg = {
  711. parseEmsgBox: parseEmsgBox,
  712. scaleTime: scaleTime
  713. };
  714. var tfhd = function tfhd(data) {
  715. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  716. result = {
  717. version: data[0],
  718. flags: new Uint8Array(data.subarray(1, 4)),
  719. trackId: view.getUint32(4)
  720. },
  721. baseDataOffsetPresent = result.flags[2] & 0x01,
  722. sampleDescriptionIndexPresent = result.flags[2] & 0x02,
  723. defaultSampleDurationPresent = result.flags[2] & 0x08,
  724. defaultSampleSizePresent = result.flags[2] & 0x10,
  725. defaultSampleFlagsPresent = result.flags[2] & 0x20,
  726. durationIsEmpty = result.flags[0] & 0x010000,
  727. defaultBaseIsMoof = result.flags[0] & 0x020000,
  728. i;
  729. i = 8;
  730. if (baseDataOffsetPresent) {
  731. i += 4; // truncate top 4 bytes
  732. // FIXME: should we read the full 64 bits?
  733. result.baseDataOffset = view.getUint32(12);
  734. i += 4;
  735. }
  736. if (sampleDescriptionIndexPresent) {
  737. result.sampleDescriptionIndex = view.getUint32(i);
  738. i += 4;
  739. }
  740. if (defaultSampleDurationPresent) {
  741. result.defaultSampleDuration = view.getUint32(i);
  742. i += 4;
  743. }
  744. if (defaultSampleSizePresent) {
  745. result.defaultSampleSize = view.getUint32(i);
  746. i += 4;
  747. }
  748. if (defaultSampleFlagsPresent) {
  749. result.defaultSampleFlags = view.getUint32(i);
  750. }
  751. if (durationIsEmpty) {
  752. result.durationIsEmpty = true;
  753. }
  754. if (!baseDataOffsetPresent && defaultBaseIsMoof) {
  755. result.baseDataOffsetIsMoof = true;
  756. }
  757. return result;
  758. };
  759. var parseTfhd = tfhd;
  760. var parseSampleFlags = function parseSampleFlags(flags) {
  761. return {
  762. isLeading: (flags[0] & 0x0c) >>> 2,
  763. dependsOn: flags[0] & 0x03,
  764. isDependedOn: (flags[1] & 0xc0) >>> 6,
  765. hasRedundancy: (flags[1] & 0x30) >>> 4,
  766. paddingValue: (flags[1] & 0x0e) >>> 1,
  767. isNonSyncSample: flags[1] & 0x01,
  768. degradationPriority: flags[2] << 8 | flags[3]
  769. };
  770. };
  771. var parseSampleFlags_1 = parseSampleFlags;
  772. var trun = function trun(data) {
  773. var result = {
  774. version: data[0],
  775. flags: new Uint8Array(data.subarray(1, 4)),
  776. samples: []
  777. },
  778. view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  779. // Flag interpretation
  780. dataOffsetPresent = result.flags[2] & 0x01,
  781. // compare with 2nd byte of 0x1
  782. firstSampleFlagsPresent = result.flags[2] & 0x04,
  783. // compare with 2nd byte of 0x4
  784. sampleDurationPresent = result.flags[1] & 0x01,
  785. // compare with 2nd byte of 0x100
  786. sampleSizePresent = result.flags[1] & 0x02,
  787. // compare with 2nd byte of 0x200
  788. sampleFlagsPresent = result.flags[1] & 0x04,
  789. // compare with 2nd byte of 0x400
  790. sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08,
  791. // compare with 2nd byte of 0x800
  792. sampleCount = view.getUint32(4),
  793. offset = 8,
  794. sample;
  795. if (dataOffsetPresent) {
  796. // 32 bit signed integer
  797. result.dataOffset = view.getInt32(offset);
  798. offset += 4;
  799. } // Overrides the flags for the first sample only. The order of
  800. // optional values will be: duration, size, compositionTimeOffset
  801. if (firstSampleFlagsPresent && sampleCount) {
  802. sample = {
  803. flags: parseSampleFlags_1(data.subarray(offset, offset + 4))
  804. };
  805. offset += 4;
  806. if (sampleDurationPresent) {
  807. sample.duration = view.getUint32(offset);
  808. offset += 4;
  809. }
  810. if (sampleSizePresent) {
  811. sample.size = view.getUint32(offset);
  812. offset += 4;
  813. }
  814. if (sampleCompositionTimeOffsetPresent) {
  815. if (result.version === 1) {
  816. sample.compositionTimeOffset = view.getInt32(offset);
  817. } else {
  818. sample.compositionTimeOffset = view.getUint32(offset);
  819. }
  820. offset += 4;
  821. }
  822. result.samples.push(sample);
  823. sampleCount--;
  824. }
  825. while (sampleCount--) {
  826. sample = {};
  827. if (sampleDurationPresent) {
  828. sample.duration = view.getUint32(offset);
  829. offset += 4;
  830. }
  831. if (sampleSizePresent) {
  832. sample.size = view.getUint32(offset);
  833. offset += 4;
  834. }
  835. if (sampleFlagsPresent) {
  836. sample.flags = parseSampleFlags_1(data.subarray(offset, offset + 4));
  837. offset += 4;
  838. }
  839. if (sampleCompositionTimeOffsetPresent) {
  840. if (result.version === 1) {
  841. sample.compositionTimeOffset = view.getInt32(offset);
  842. } else {
  843. sample.compositionTimeOffset = view.getUint32(offset);
  844. }
  845. offset += 4;
  846. }
  847. result.samples.push(sample);
  848. }
  849. return result;
  850. };
  851. var parseTrun = trun;
  852. var toUnsigned$1 = bin.toUnsigned;
  853. var getUint64$1 = numbers.getUint64;
  854. var tfdt = function tfdt(data) {
  855. var result = {
  856. version: data[0],
  857. flags: new Uint8Array(data.subarray(1, 4))
  858. };
  859. if (result.version === 1) {
  860. result.baseMediaDecodeTime = getUint64$1(data.subarray(4));
  861. } else {
  862. result.baseMediaDecodeTime = toUnsigned$1(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
  863. }
  864. return result;
  865. };
  866. var parseTfdt = tfdt;
  867. // IE11 doesn't support indexOf for TypedArrays.
  868. // Once IE11 support is dropped, this function should be removed.
  869. var typedArrayIndexOf$1 = function typedArrayIndexOf(typedArray, element, fromIndex) {
  870. if (!typedArray) {
  871. return -1;
  872. }
  873. var currentIndex = fromIndex;
  874. for (; currentIndex < typedArray.length; currentIndex++) {
  875. if (typedArray[currentIndex] === element) {
  876. return currentIndex;
  877. }
  878. }
  879. return -1;
  880. };
  881. var typedArray = {
  882. typedArrayIndexOf: typedArrayIndexOf$1
  883. };
  884. var typedArrayIndexOf = typedArray.typedArrayIndexOf,
  885. // Frames that allow different types of text encoding contain a text
  886. // encoding description byte [ID3v2.4.0 section 4.]
  887. textEncodingDescriptionByte = {
  888. Iso88591: 0x00,
  889. // ISO-8859-1, terminated with \0.
  890. Utf16: 0x01,
  891. // UTF-16 encoded Unicode BOM, terminated with \0\0
  892. Utf16be: 0x02,
  893. // UTF-16BE encoded Unicode, without BOM, terminated with \0\0
  894. Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0
  895. },
  896. // return a percent-encoded representation of the specified byte range
  897. // @see http://en.wikipedia.org/wiki/Percent-encoding
  898. percentEncode$1 = function percentEncode(bytes, start, end) {
  899. var i,
  900. result = '';
  901. for (i = start; i < end; i++) {
  902. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  903. }
  904. return result;
  905. },
  906. // return the string representation of the specified byte range,
  907. // interpreted as UTf-8.
  908. parseUtf8 = function parseUtf8(bytes, start, end) {
  909. return decodeURIComponent(percentEncode$1(bytes, start, end));
  910. },
  911. // return the string representation of the specified byte range,
  912. // interpreted as ISO-8859-1.
  913. parseIso88591$1 = function parseIso88591(bytes, start, end) {
  914. return unescape(percentEncode$1(bytes, start, end)); // jshint ignore:line
  915. },
  916. parseSyncSafeInteger$1 = function parseSyncSafeInteger(data) {
  917. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  918. },
  919. frameParsers = {
  920. 'APIC': function APIC(frame) {
  921. var i = 1,
  922. mimeTypeEndIndex,
  923. descriptionEndIndex,
  924. LINK_MIME_TYPE = '-->';
  925. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  926. // ignore frames with unrecognized character encodings
  927. return;
  928. } // parsing fields [ID3v2.4.0 section 4.14.]
  929. mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
  930. if (mimeTypeEndIndex < 0) {
  931. // malformed frame
  932. return;
  933. } // parsing Mime type field (terminated with \0)
  934. frame.mimeType = parseIso88591$1(frame.data, i, mimeTypeEndIndex);
  935. i = mimeTypeEndIndex + 1; // parsing 1-byte Picture Type field
  936. frame.pictureType = frame.data[i];
  937. i++;
  938. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
  939. if (descriptionEndIndex < 0) {
  940. // malformed frame
  941. return;
  942. } // parsing Description field (terminated with \0)
  943. frame.description = parseUtf8(frame.data, i, descriptionEndIndex);
  944. i = descriptionEndIndex + 1;
  945. if (frame.mimeType === LINK_MIME_TYPE) {
  946. // parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.])
  947. frame.url = parseIso88591$1(frame.data, i, frame.data.length);
  948. } else {
  949. // parsing Picture Data field as binary data
  950. frame.pictureData = frame.data.subarray(i, frame.data.length);
  951. }
  952. },
  953. 'T*': function T(frame) {
  954. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  955. // ignore frames with unrecognized character encodings
  956. return;
  957. } // parse text field, do not include null terminator in the frame value
  958. // frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.]
  959. 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.]
  960. frame.values = frame.value.split('\0');
  961. },
  962. 'TXXX': function TXXX(frame) {
  963. var descriptionEndIndex;
  964. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  965. // ignore frames with unrecognized character encodings
  966. return;
  967. }
  968. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  969. if (descriptionEndIndex === -1) {
  970. return;
  971. } // parse the text fields
  972. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // do not include the null terminator in the tag value
  973. // frames that allow different types of encoding contain terminated text
  974. // [ID3v2.4.0 section 4.]
  975. frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, '');
  976. frame.data = frame.value;
  977. },
  978. 'W*': function W(frame) {
  979. // parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  980. // if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3]
  981. frame.url = parseIso88591$1(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
  982. },
  983. 'WXXX': function WXXX(frame) {
  984. var descriptionEndIndex;
  985. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  986. // ignore frames with unrecognized character encodings
  987. return;
  988. }
  989. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  990. if (descriptionEndIndex === -1) {
  991. return;
  992. } // parse the description and URL fields
  993. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  994. // if the value is followed by a string termination all the following information
  995. // should be ignored [ID3v2.4.0 section 4.3]
  996. frame.url = parseIso88591$1(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, '');
  997. },
  998. 'PRIV': function PRIV(frame) {
  999. var i;
  1000. for (i = 0; i < frame.data.length; i++) {
  1001. if (frame.data[i] === 0) {
  1002. // parse the description and URL fields
  1003. frame.owner = parseIso88591$1(frame.data, 0, i);
  1004. break;
  1005. }
  1006. }
  1007. frame.privateData = frame.data.subarray(i + 1);
  1008. frame.data = frame.privateData;
  1009. }
  1010. };
  1011. var parseId3Frames$1 = function parseId3Frames(data) {
  1012. var frameSize,
  1013. frameHeader,
  1014. frameStart = 10,
  1015. tagSize = 0,
  1016. frames = []; // If we don't have enough data for a header, 10 bytes,
  1017. // or 'ID3' in the first 3 bytes this is not a valid ID3 tag.
  1018. if (data.length < 10 || data[0] !== 'I'.charCodeAt(0) || data[1] !== 'D'.charCodeAt(0) || data[2] !== '3'.charCodeAt(0)) {
  1019. return;
  1020. } // the frame size is transmitted as a 28-bit integer in the
  1021. // last four bytes of the ID3 header.
  1022. // The most significant bit of each byte is dropped and the
  1023. // results concatenated to recover the actual value.
  1024. tagSize = parseSyncSafeInteger$1(data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  1025. // convenient for our comparisons to include it
  1026. tagSize += 10; // check bit 6 of byte 5 for the extended header flag.
  1027. var hasExtendedHeader = data[5] & 0x40;
  1028. if (hasExtendedHeader) {
  1029. // advance the frame start past the extended header
  1030. frameStart += 4; // header size field
  1031. frameStart += parseSyncSafeInteger$1(data.subarray(10, 14));
  1032. tagSize -= parseSyncSafeInteger$1(data.subarray(16, 20)); // clip any padding off the end
  1033. } // parse one or more ID3 frames
  1034. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  1035. do {
  1036. // determine the number of bytes in this frame
  1037. frameSize = parseSyncSafeInteger$1(data.subarray(frameStart + 4, frameStart + 8));
  1038. if (frameSize < 1) {
  1039. break;
  1040. }
  1041. frameHeader = String.fromCharCode(data[frameStart], data[frameStart + 1], data[frameStart + 2], data[frameStart + 3]);
  1042. var frame = {
  1043. id: frameHeader,
  1044. data: data.subarray(frameStart + 10, frameStart + frameSize + 10)
  1045. };
  1046. frame.key = frame.id; // parse frame values
  1047. if (frameParsers[frame.id]) {
  1048. // use frame specific parser
  1049. frameParsers[frame.id](frame);
  1050. } else if (frame.id[0] === 'T') {
  1051. // use text frame generic parser
  1052. frameParsers['T*'](frame);
  1053. } else if (frame.id[0] === 'W') {
  1054. // use URL link frame generic parser
  1055. frameParsers['W*'](frame);
  1056. }
  1057. frames.push(frame);
  1058. frameStart += 10; // advance past the frame header
  1059. frameStart += frameSize; // advance past the frame body
  1060. } while (frameStart < tagSize);
  1061. return frames;
  1062. };
  1063. var parseId3 = {
  1064. parseId3Frames: parseId3Frames$1,
  1065. parseSyncSafeInteger: parseSyncSafeInteger$1,
  1066. frameParsers: frameParsers
  1067. };
  1068. var toUnsigned = bin.toUnsigned;
  1069. var toHexString = bin.toHexString;
  1070. var getUint64 = numbers.getUint64;
  1071. var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3;
  1072. var parseId3Frames = parseId3.parseId3Frames;
  1073. /**
  1074. * Parses an MP4 initialization segment and extracts the timescale
  1075. * values for any declared tracks. Timescale values indicate the
  1076. * number of clock ticks per second to assume for time-based values
  1077. * elsewhere in the MP4.
  1078. *
  1079. * To determine the start time of an MP4, you need two pieces of
  1080. * information: the timescale unit and the earliest base media decode
  1081. * time. Multiple timescales can be specified within an MP4 but the
  1082. * base media decode time is always expressed in the timescale from
  1083. * the media header box for the track:
  1084. * ```
  1085. * moov > trak > mdia > mdhd.timescale
  1086. * ```
  1087. * @param init {Uint8Array} the bytes of the init segment
  1088. * @return {object} a hash of track ids to timescale values or null if
  1089. * the init segment is malformed.
  1090. */
  1091. timescale = function timescale(init) {
  1092. var result = {},
  1093. traks = findBox_1(init, ['moov', 'trak']); // mdhd timescale
  1094. return traks.reduce(function (result, trak) {
  1095. var tkhd, version, index, id, mdhd;
  1096. tkhd = findBox_1(trak, ['tkhd'])[0];
  1097. if (!tkhd) {
  1098. return null;
  1099. }
  1100. version = tkhd[0];
  1101. index = version === 0 ? 12 : 20;
  1102. id = toUnsigned(tkhd[index] << 24 | tkhd[index + 1] << 16 | tkhd[index + 2] << 8 | tkhd[index + 3]);
  1103. mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
  1104. if (!mdhd) {
  1105. return null;
  1106. }
  1107. version = mdhd[0];
  1108. index = version === 0 ? 12 : 20;
  1109. result[id] = toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  1110. return result;
  1111. }, result);
  1112. };
  1113. /**
  1114. * Determine the base media decode start time, in seconds, for an MP4
  1115. * fragment. If multiple fragments are specified, the earliest time is
  1116. * returned.
  1117. *
  1118. * The base media decode time can be parsed from track fragment
  1119. * metadata:
  1120. * ```
  1121. * moof > traf > tfdt.baseMediaDecodeTime
  1122. * ```
  1123. * It requires the timescale value from the mdhd to interpret.
  1124. *
  1125. * @param timescale {object} a hash of track ids to timescale values.
  1126. * @return {number} the earliest base media decode start time for the
  1127. * fragment, in seconds
  1128. */
  1129. startTime = function startTime(timescale, fragment) {
  1130. var trafs; // we need info from two childrend of each track fragment box
  1131. trafs = findBox_1(fragment, ['moof', 'traf']); // determine the start times for each track
  1132. var lowestTime = trafs.reduce(function (acc, traf) {
  1133. var tfhd = findBox_1(traf, ['tfhd'])[0]; // get the track id from the tfhd
  1134. var id = toUnsigned(tfhd[4] << 24 | tfhd[5] << 16 | tfhd[6] << 8 | tfhd[7]); // assume a 90kHz clock if no timescale was specified
  1135. var scale = timescale[id] || 90e3; // get the base media decode time from the tfdt
  1136. var tfdt = findBox_1(traf, ['tfdt'])[0];
  1137. var dv = new DataView(tfdt.buffer, tfdt.byteOffset, tfdt.byteLength);
  1138. var baseTime; // version 1 is 64 bit
  1139. if (tfdt[0] === 1) {
  1140. baseTime = getUint64(tfdt.subarray(4, 12));
  1141. } else {
  1142. baseTime = dv.getUint32(4);
  1143. } // convert base time to seconds if it is a valid number.
  1144. var seconds;
  1145. if (typeof baseTime === 'bigint') {
  1146. seconds = baseTime / window__default['default'].BigInt(scale);
  1147. } else if (typeof baseTime === 'number' && !isNaN(baseTime)) {
  1148. seconds = baseTime / scale;
  1149. }
  1150. if (seconds < Number.MAX_SAFE_INTEGER) {
  1151. seconds = Number(seconds);
  1152. }
  1153. if (seconds < acc) {
  1154. acc = seconds;
  1155. }
  1156. return acc;
  1157. }, Infinity);
  1158. return typeof lowestTime === 'bigint' || isFinite(lowestTime) ? lowestTime : 0;
  1159. };
  1160. /**
  1161. * Determine the composition start, in seconds, for an MP4
  1162. * fragment.
  1163. *
  1164. * The composition start time of a fragment can be calculated using the base
  1165. * media decode time, composition time offset, and timescale, as follows:
  1166. *
  1167. * compositionStartTime = (baseMediaDecodeTime + compositionTimeOffset) / timescale
  1168. *
  1169. * All of the aforementioned information is contained within a media fragment's
  1170. * `traf` box, except for timescale info, which comes from the initialization
  1171. * segment, so a track id (also contained within a `traf`) is also necessary to
  1172. * associate it with a timescale
  1173. *
  1174. *
  1175. * @param timescales {object} - a hash of track ids to timescale values.
  1176. * @param fragment {Unit8Array} - the bytes of a media segment
  1177. * @return {number} the composition start time for the fragment, in seconds
  1178. **/
  1179. compositionStartTime = function compositionStartTime(timescales, fragment) {
  1180. var trafBoxes = findBox_1(fragment, ['moof', 'traf']);
  1181. var baseMediaDecodeTime = 0;
  1182. var compositionTimeOffset = 0;
  1183. var trackId;
  1184. if (trafBoxes && trafBoxes.length) {
  1185. // The spec states that track run samples contained within a `traf` box are contiguous, but
  1186. // it does not explicitly state whether the `traf` boxes themselves are contiguous.
  1187. // We will assume that they are, so we only need the first to calculate start time.
  1188. var tfhd = findBox_1(trafBoxes[0], ['tfhd'])[0];
  1189. var trun = findBox_1(trafBoxes[0], ['trun'])[0];
  1190. var tfdt = findBox_1(trafBoxes[0], ['tfdt'])[0];
  1191. if (tfhd) {
  1192. var parsedTfhd = parseTfhd(tfhd);
  1193. trackId = parsedTfhd.trackId;
  1194. }
  1195. if (tfdt) {
  1196. var parsedTfdt = parseTfdt(tfdt);
  1197. baseMediaDecodeTime = parsedTfdt.baseMediaDecodeTime;
  1198. }
  1199. if (trun) {
  1200. var parsedTrun = parseTrun(trun);
  1201. if (parsedTrun.samples && parsedTrun.samples.length) {
  1202. compositionTimeOffset = parsedTrun.samples[0].compositionTimeOffset || 0;
  1203. }
  1204. }
  1205. } // Get timescale for this specific track. Assume a 90kHz clock if no timescale was
  1206. // specified.
  1207. var timescale = timescales[trackId] || 90e3; // return the composition start time, in seconds
  1208. if (typeof baseMediaDecodeTime === 'bigint') {
  1209. compositionTimeOffset = window__default['default'].BigInt(compositionTimeOffset);
  1210. timescale = window__default['default'].BigInt(timescale);
  1211. }
  1212. var result = (baseMediaDecodeTime + compositionTimeOffset) / timescale;
  1213. if (typeof result === 'bigint' && result < Number.MAX_SAFE_INTEGER) {
  1214. result = Number(result);
  1215. }
  1216. return result;
  1217. };
  1218. /**
  1219. * Find the trackIds of the video tracks in this source.
  1220. * Found by parsing the Handler Reference and Track Header Boxes:
  1221. * moov > trak > mdia > hdlr
  1222. * moov > trak > tkhd
  1223. *
  1224. * @param {Uint8Array} init - The bytes of the init segment for this source
  1225. * @return {Number[]} A list of trackIds
  1226. *
  1227. * @see ISO-BMFF-12/2015, Section 8.4.3
  1228. **/
  1229. getVideoTrackIds = function getVideoTrackIds(init) {
  1230. var traks = findBox_1(init, ['moov', 'trak']);
  1231. var videoTrackIds = [];
  1232. traks.forEach(function (trak) {
  1233. var hdlrs = findBox_1(trak, ['mdia', 'hdlr']);
  1234. var tkhds = findBox_1(trak, ['tkhd']);
  1235. hdlrs.forEach(function (hdlr, index) {
  1236. var handlerType = parseType_1(hdlr.subarray(8, 12));
  1237. var tkhd = tkhds[index];
  1238. var view;
  1239. var version;
  1240. var trackId;
  1241. if (handlerType === 'vide') {
  1242. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  1243. version = view.getUint8(0);
  1244. trackId = version === 0 ? view.getUint32(12) : view.getUint32(20);
  1245. videoTrackIds.push(trackId);
  1246. }
  1247. });
  1248. });
  1249. return videoTrackIds;
  1250. };
  1251. getTimescaleFromMediaHeader = function getTimescaleFromMediaHeader(mdhd) {
  1252. // mdhd is a FullBox, meaning it will have its own version as the first byte
  1253. var version = mdhd[0];
  1254. var index = version === 0 ? 12 : 20;
  1255. return toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  1256. };
  1257. /**
  1258. * Get all the video, audio, and hint tracks from a non fragmented
  1259. * mp4 segment
  1260. */
  1261. getTracks = function getTracks(init) {
  1262. var traks = findBox_1(init, ['moov', 'trak']);
  1263. var tracks = [];
  1264. traks.forEach(function (trak) {
  1265. var track = {};
  1266. var tkhd = findBox_1(trak, ['tkhd'])[0];
  1267. var view, tkhdVersion; // id
  1268. if (tkhd) {
  1269. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  1270. tkhdVersion = view.getUint8(0);
  1271. track.id = tkhdVersion === 0 ? view.getUint32(12) : view.getUint32(20);
  1272. }
  1273. var hdlr = findBox_1(trak, ['mdia', 'hdlr'])[0]; // type
  1274. if (hdlr) {
  1275. var type = parseType_1(hdlr.subarray(8, 12));
  1276. if (type === 'vide') {
  1277. track.type = 'video';
  1278. } else if (type === 'soun') {
  1279. track.type = 'audio';
  1280. } else {
  1281. track.type = type;
  1282. }
  1283. } // codec
  1284. var stsd = findBox_1(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
  1285. if (stsd) {
  1286. var sampleDescriptions = stsd.subarray(8); // gives the codec type string
  1287. track.codec = parseType_1(sampleDescriptions.subarray(4, 8));
  1288. var codecBox = findBox_1(sampleDescriptions, [track.codec])[0];
  1289. var codecConfig, codecConfigType;
  1290. if (codecBox) {
  1291. // https://tools.ietf.org/html/rfc6381#section-3.3
  1292. if (/^[asm]vc[1-9]$/i.test(track.codec)) {
  1293. // we don't need anything but the "config" parameter of the
  1294. // avc1 codecBox
  1295. codecConfig = codecBox.subarray(78);
  1296. codecConfigType = parseType_1(codecConfig.subarray(4, 8));
  1297. if (codecConfigType === 'avcC' && codecConfig.length > 11) {
  1298. track.codec += '.'; // left padded with zeroes for single digit hex
  1299. // profile idc
  1300. track.codec += toHexString(codecConfig[9]); // the byte containing the constraint_set flags
  1301. track.codec += toHexString(codecConfig[10]); // level idc
  1302. track.codec += toHexString(codecConfig[11]);
  1303. } else {
  1304. // TODO: show a warning that we couldn't parse the codec
  1305. // and are using the default
  1306. track.codec = 'avc1.4d400d';
  1307. }
  1308. } else if (/^mp4[a,v]$/i.test(track.codec)) {
  1309. // we do not need anything but the streamDescriptor of the mp4a codecBox
  1310. codecConfig = codecBox.subarray(28);
  1311. codecConfigType = parseType_1(codecConfig.subarray(4, 8));
  1312. if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
  1313. track.codec += '.' + toHexString(codecConfig[19]); // this value is only a single digit
  1314. track.codec += '.' + toHexString(codecConfig[20] >>> 2 & 0x3f).replace(/^0/, '');
  1315. } else {
  1316. // TODO: show a warning that we couldn't parse the codec
  1317. // and are using the default
  1318. track.codec = 'mp4a.40.2';
  1319. }
  1320. } else {
  1321. // flac, opus, etc
  1322. track.codec = track.codec.toLowerCase();
  1323. }
  1324. }
  1325. }
  1326. var mdhd = findBox_1(trak, ['mdia', 'mdhd'])[0];
  1327. if (mdhd) {
  1328. track.timescale = getTimescaleFromMediaHeader(mdhd);
  1329. }
  1330. tracks.push(track);
  1331. });
  1332. return tracks;
  1333. };
  1334. /**
  1335. * Returns an array of emsg ID3 data from the provided segmentData.
  1336. * An offset can also be provided as the Latest Arrival Time to calculate
  1337. * the Event Start Time of v0 EMSG boxes.
  1338. * See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing
  1339. *
  1340. * @param {Uint8Array} segmentData the segment byte array.
  1341. * @param {number} offset the segment start time or Latest Arrival Time,
  1342. * @return {Object[]} an array of ID3 parsed from EMSG boxes
  1343. */
  1344. getEmsgID3 = function getEmsgID3(segmentData, offset) {
  1345. if (offset === void 0) {
  1346. offset = 0;
  1347. }
  1348. var emsgBoxes = findBox_1(segmentData, ['emsg']);
  1349. return emsgBoxes.map(function (data) {
  1350. var parsedBox = emsg.parseEmsgBox(new Uint8Array(data));
  1351. var parsedId3Frames = parseId3Frames(parsedBox.message_data);
  1352. return {
  1353. cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset),
  1354. duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale),
  1355. frames: parsedId3Frames
  1356. };
  1357. });
  1358. };
  1359. var probe = {
  1360. // export mp4 inspector's findBox and parseType for backwards compatibility
  1361. findBox: findBox_1,
  1362. parseType: parseType_1,
  1363. timescale: timescale,
  1364. startTime: startTime,
  1365. compositionStartTime: compositionStartTime,
  1366. videoTrackIds: getVideoTrackIds,
  1367. tracks: getTracks,
  1368. getTimescaleFromMediaHeader: getTimescaleFromMediaHeader,
  1369. getEmsgID3: getEmsgID3
  1370. };
  1371. /**
  1372. * mux.js
  1373. *
  1374. * Copyright (c) Brightcove
  1375. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1376. *
  1377. * A lightweight readable stream implemention that handles event dispatching.
  1378. * Objects that inherit from streams should call init in their constructors.
  1379. */
  1380. var Stream = function Stream() {
  1381. this.init = function () {
  1382. var listeners = {};
  1383. /**
  1384. * Add a listener for a specified event type.
  1385. * @param type {string} the event name
  1386. * @param listener {function} the callback to be invoked when an event of
  1387. * the specified type occurs
  1388. */
  1389. this.on = function (type, listener) {
  1390. if (!listeners[type]) {
  1391. listeners[type] = [];
  1392. }
  1393. listeners[type] = listeners[type].concat(listener);
  1394. };
  1395. /**
  1396. * Remove a listener for a specified event type.
  1397. * @param type {string} the event name
  1398. * @param listener {function} a function previously registered for this
  1399. * type of event through `on`
  1400. */
  1401. this.off = function (type, listener) {
  1402. var index;
  1403. if (!listeners[type]) {
  1404. return false;
  1405. }
  1406. index = listeners[type].indexOf(listener);
  1407. listeners[type] = listeners[type].slice();
  1408. listeners[type].splice(index, 1);
  1409. return index > -1;
  1410. };
  1411. /**
  1412. * Trigger an event of the specified type on this stream. Any additional
  1413. * arguments to this function are passed as parameters to event listeners.
  1414. * @param type {string} the event name
  1415. */
  1416. this.trigger = function (type) {
  1417. var callbacks, i, length, args;
  1418. callbacks = listeners[type];
  1419. if (!callbacks) {
  1420. return;
  1421. } // Slicing the arguments on every invocation of this method
  1422. // can add a significant amount of overhead. Avoid the
  1423. // intermediate object creation for the common case of a
  1424. // single callback argument
  1425. if (arguments.length === 2) {
  1426. length = callbacks.length;
  1427. for (i = 0; i < length; ++i) {
  1428. callbacks[i].call(this, arguments[1]);
  1429. }
  1430. } else {
  1431. args = [];
  1432. i = arguments.length;
  1433. for (i = 1; i < arguments.length; ++i) {
  1434. args.push(arguments[i]);
  1435. }
  1436. length = callbacks.length;
  1437. for (i = 0; i < length; ++i) {
  1438. callbacks[i].apply(this, args);
  1439. }
  1440. }
  1441. };
  1442. /**
  1443. * Destroys the stream and cleans up.
  1444. */
  1445. this.dispose = function () {
  1446. listeners = {};
  1447. };
  1448. };
  1449. };
  1450. /**
  1451. * Forwards all `data` events on this stream to the destination stream. The
  1452. * destination stream should provide a method `push` to receive the data
  1453. * events as they arrive.
  1454. * @param destination {stream} the stream that will receive all `data` events
  1455. * @param autoFlush {boolean} if false, we will not call `flush` on the destination
  1456. * when the current stream emits a 'done' event
  1457. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  1458. */
  1459. Stream.prototype.pipe = function (destination) {
  1460. this.on('data', function (data) {
  1461. destination.push(data);
  1462. });
  1463. this.on('done', function (flushSource) {
  1464. destination.flush(flushSource);
  1465. });
  1466. this.on('partialdone', function (flushSource) {
  1467. destination.partialFlush(flushSource);
  1468. });
  1469. this.on('endedtimeline', function (flushSource) {
  1470. destination.endTimeline(flushSource);
  1471. });
  1472. this.on('reset', function (flushSource) {
  1473. destination.reset(flushSource);
  1474. });
  1475. return destination;
  1476. }; // Default stream functions that are expected to be overridden to perform
  1477. // actual work. These are provided by the prototype as a sort of no-op
  1478. // implementation so that we don't have to check for their existence in the
  1479. // `pipe` function above.
  1480. Stream.prototype.push = function (data) {
  1481. this.trigger('data', data);
  1482. };
  1483. Stream.prototype.flush = function (flushSource) {
  1484. this.trigger('done', flushSource);
  1485. };
  1486. Stream.prototype.partialFlush = function (flushSource) {
  1487. this.trigger('partialdone', flushSource);
  1488. };
  1489. Stream.prototype.endTimeline = function (flushSource) {
  1490. this.trigger('endedtimeline', flushSource);
  1491. };
  1492. Stream.prototype.reset = function (flushSource) {
  1493. this.trigger('reset', flushSource);
  1494. };
  1495. var stream = Stream;
  1496. /**
  1497. * mux.js
  1498. *
  1499. * Copyright (c) Brightcove
  1500. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1501. */
  1502. // Convert an array of nal units into an array of frames with each frame being
  1503. // composed of the nal units that make up that frame
  1504. // Also keep track of cummulative data about the frame from the nal units such
  1505. // as the frame duration, starting pts, etc.
  1506. var groupNalsIntoFrames = function groupNalsIntoFrames(nalUnits) {
  1507. var i,
  1508. currentNal,
  1509. currentFrame = [],
  1510. frames = []; // TODO added for LHLS, make sure this is OK
  1511. frames.byteLength = 0;
  1512. frames.nalCount = 0;
  1513. frames.duration = 0;
  1514. currentFrame.byteLength = 0;
  1515. for (i = 0; i < nalUnits.length; i++) {
  1516. currentNal = nalUnits[i]; // Split on 'aud'-type nal units
  1517. if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
  1518. // Since the very first nal unit is expected to be an AUD
  1519. // only push to the frames array when currentFrame is not empty
  1520. if (currentFrame.length) {
  1521. currentFrame.duration = currentNal.dts - currentFrame.dts; // TODO added for LHLS, make sure this is OK
  1522. frames.byteLength += currentFrame.byteLength;
  1523. frames.nalCount += currentFrame.length;
  1524. frames.duration += currentFrame.duration;
  1525. frames.push(currentFrame);
  1526. }
  1527. currentFrame = [currentNal];
  1528. currentFrame.byteLength = currentNal.data.byteLength;
  1529. currentFrame.pts = currentNal.pts;
  1530. currentFrame.dts = currentNal.dts;
  1531. } else {
  1532. // Specifically flag key frames for ease of use later
  1533. if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
  1534. currentFrame.keyFrame = true;
  1535. }
  1536. currentFrame.duration = currentNal.dts - currentFrame.dts;
  1537. currentFrame.byteLength += currentNal.data.byteLength;
  1538. currentFrame.push(currentNal);
  1539. }
  1540. } // For the last frame, use the duration of the previous frame if we
  1541. // have nothing better to go on
  1542. if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
  1543. currentFrame.duration = frames[frames.length - 1].duration;
  1544. } // Push the final frame
  1545. // TODO added for LHLS, make sure this is OK
  1546. frames.byteLength += currentFrame.byteLength;
  1547. frames.nalCount += currentFrame.length;
  1548. frames.duration += currentFrame.duration;
  1549. frames.push(currentFrame);
  1550. return frames;
  1551. }; // Convert an array of frames into an array of Gop with each Gop being composed
  1552. // of the frames that make up that Gop
  1553. // Also keep track of cummulative data about the Gop from the frames such as the
  1554. // Gop duration, starting pts, etc.
  1555. var groupFramesIntoGops = function groupFramesIntoGops(frames) {
  1556. var i,
  1557. currentFrame,
  1558. currentGop = [],
  1559. gops = []; // We must pre-set some of the values on the Gop since we
  1560. // keep running totals of these values
  1561. currentGop.byteLength = 0;
  1562. currentGop.nalCount = 0;
  1563. currentGop.duration = 0;
  1564. currentGop.pts = frames[0].pts;
  1565. currentGop.dts = frames[0].dts; // store some metadata about all the Gops
  1566. gops.byteLength = 0;
  1567. gops.nalCount = 0;
  1568. gops.duration = 0;
  1569. gops.pts = frames[0].pts;
  1570. gops.dts = frames[0].dts;
  1571. for (i = 0; i < frames.length; i++) {
  1572. currentFrame = frames[i];
  1573. if (currentFrame.keyFrame) {
  1574. // Since the very first frame is expected to be an keyframe
  1575. // only push to the gops array when currentGop is not empty
  1576. if (currentGop.length) {
  1577. gops.push(currentGop);
  1578. gops.byteLength += currentGop.byteLength;
  1579. gops.nalCount += currentGop.nalCount;
  1580. gops.duration += currentGop.duration;
  1581. }
  1582. currentGop = [currentFrame];
  1583. currentGop.nalCount = currentFrame.length;
  1584. currentGop.byteLength = currentFrame.byteLength;
  1585. currentGop.pts = currentFrame.pts;
  1586. currentGop.dts = currentFrame.dts;
  1587. currentGop.duration = currentFrame.duration;
  1588. } else {
  1589. currentGop.duration += currentFrame.duration;
  1590. currentGop.nalCount += currentFrame.length;
  1591. currentGop.byteLength += currentFrame.byteLength;
  1592. currentGop.push(currentFrame);
  1593. }
  1594. }
  1595. if (gops.length && currentGop.duration <= 0) {
  1596. currentGop.duration = gops[gops.length - 1].duration;
  1597. }
  1598. gops.byteLength += currentGop.byteLength;
  1599. gops.nalCount += currentGop.nalCount;
  1600. gops.duration += currentGop.duration; // push the final Gop
  1601. gops.push(currentGop);
  1602. return gops;
  1603. };
  1604. /*
  1605. * Search for the first keyframe in the GOPs and throw away all frames
  1606. * until that keyframe. Then extend the duration of the pulled keyframe
  1607. * and pull the PTS and DTS of the keyframe so that it covers the time
  1608. * range of the frames that were disposed.
  1609. *
  1610. * @param {Array} gops video GOPs
  1611. * @returns {Array} modified video GOPs
  1612. */
  1613. var extendFirstKeyFrame = function extendFirstKeyFrame(gops) {
  1614. var currentGop;
  1615. if (!gops[0][0].keyFrame && gops.length > 1) {
  1616. // Remove the first GOP
  1617. currentGop = gops.shift();
  1618. gops.byteLength -= currentGop.byteLength;
  1619. gops.nalCount -= currentGop.nalCount; // Extend the first frame of what is now the
  1620. // first gop to cover the time period of the
  1621. // frames we just removed
  1622. gops[0][0].dts = currentGop.dts;
  1623. gops[0][0].pts = currentGop.pts;
  1624. gops[0][0].duration += currentGop.duration;
  1625. }
  1626. return gops;
  1627. };
  1628. /**
  1629. * Default sample object
  1630. * see ISO/IEC 14496-12:2012, section 8.6.4.3
  1631. */
  1632. var createDefaultSample = function createDefaultSample() {
  1633. return {
  1634. size: 0,
  1635. flags: {
  1636. isLeading: 0,
  1637. dependsOn: 1,
  1638. isDependedOn: 0,
  1639. hasRedundancy: 0,
  1640. degradationPriority: 0,
  1641. isNonSyncSample: 1
  1642. }
  1643. };
  1644. };
  1645. /*
  1646. * Collates information from a video frame into an object for eventual
  1647. * entry into an MP4 sample table.
  1648. *
  1649. * @param {Object} frame the video frame
  1650. * @param {Number} dataOffset the byte offset to position the sample
  1651. * @return {Object} object containing sample table info for a frame
  1652. */
  1653. var sampleForFrame = function sampleForFrame(frame, dataOffset) {
  1654. var sample = createDefaultSample();
  1655. sample.dataOffset = dataOffset;
  1656. sample.compositionTimeOffset = frame.pts - frame.dts;
  1657. sample.duration = frame.duration;
  1658. sample.size = 4 * frame.length; // Space for nal unit size
  1659. sample.size += frame.byteLength;
  1660. if (frame.keyFrame) {
  1661. sample.flags.dependsOn = 2;
  1662. sample.flags.isNonSyncSample = 0;
  1663. }
  1664. return sample;
  1665. }; // generate the track's sample table from an array of gops
  1666. var generateSampleTable$1 = function generateSampleTable(gops, baseDataOffset) {
  1667. var h,
  1668. i,
  1669. sample,
  1670. currentGop,
  1671. currentFrame,
  1672. dataOffset = baseDataOffset || 0,
  1673. samples = [];
  1674. for (h = 0; h < gops.length; h++) {
  1675. currentGop = gops[h];
  1676. for (i = 0; i < currentGop.length; i++) {
  1677. currentFrame = currentGop[i];
  1678. sample = sampleForFrame(currentFrame, dataOffset);
  1679. dataOffset += sample.size;
  1680. samples.push(sample);
  1681. }
  1682. }
  1683. return samples;
  1684. }; // generate the track's raw mdat data from an array of gops
  1685. var concatenateNalData = function concatenateNalData(gops) {
  1686. var h,
  1687. i,
  1688. j,
  1689. currentGop,
  1690. currentFrame,
  1691. currentNal,
  1692. dataOffset = 0,
  1693. nalsByteLength = gops.byteLength,
  1694. numberOfNals = gops.nalCount,
  1695. totalByteLength = nalsByteLength + 4 * numberOfNals,
  1696. data = new Uint8Array(totalByteLength),
  1697. view = new DataView(data.buffer); // For each Gop..
  1698. for (h = 0; h < gops.length; h++) {
  1699. currentGop = gops[h]; // For each Frame..
  1700. for (i = 0; i < currentGop.length; i++) {
  1701. currentFrame = currentGop[i]; // For each NAL..
  1702. for (j = 0; j < currentFrame.length; j++) {
  1703. currentNal = currentFrame[j];
  1704. view.setUint32(dataOffset, currentNal.data.byteLength);
  1705. dataOffset += 4;
  1706. data.set(currentNal.data, dataOffset);
  1707. dataOffset += currentNal.data.byteLength;
  1708. }
  1709. }
  1710. }
  1711. return data;
  1712. }; // generate the track's sample table from a frame
  1713. var generateSampleTableForFrame = function generateSampleTableForFrame(frame, baseDataOffset) {
  1714. var sample,
  1715. dataOffset = baseDataOffset || 0,
  1716. samples = [];
  1717. sample = sampleForFrame(frame, dataOffset);
  1718. samples.push(sample);
  1719. return samples;
  1720. }; // generate the track's raw mdat data from a frame
  1721. var concatenateNalDataForFrame = function concatenateNalDataForFrame(frame) {
  1722. var i,
  1723. currentNal,
  1724. dataOffset = 0,
  1725. nalsByteLength = frame.byteLength,
  1726. numberOfNals = frame.length,
  1727. totalByteLength = nalsByteLength + 4 * numberOfNals,
  1728. data = new Uint8Array(totalByteLength),
  1729. view = new DataView(data.buffer); // For each NAL..
  1730. for (i = 0; i < frame.length; i++) {
  1731. currentNal = frame[i];
  1732. view.setUint32(dataOffset, currentNal.data.byteLength);
  1733. dataOffset += 4;
  1734. data.set(currentNal.data, dataOffset);
  1735. dataOffset += currentNal.data.byteLength;
  1736. }
  1737. return data;
  1738. };
  1739. var frameUtils = {
  1740. groupNalsIntoFrames: groupNalsIntoFrames,
  1741. groupFramesIntoGops: groupFramesIntoGops,
  1742. extendFirstKeyFrame: extendFirstKeyFrame,
  1743. generateSampleTable: generateSampleTable$1,
  1744. concatenateNalData: concatenateNalData,
  1745. generateSampleTableForFrame: generateSampleTableForFrame,
  1746. concatenateNalDataForFrame: concatenateNalDataForFrame
  1747. };
  1748. /**
  1749. * mux.js
  1750. *
  1751. * Copyright (c) Brightcove
  1752. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1753. */
  1754. var highPrefix = [33, 16, 5, 32, 164, 27];
  1755. var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
  1756. var zeroFill = function zeroFill(count) {
  1757. var a = [];
  1758. while (count--) {
  1759. a.push(0);
  1760. }
  1761. return a;
  1762. };
  1763. var makeTable = function makeTable(metaTable) {
  1764. return Object.keys(metaTable).reduce(function (obj, key) {
  1765. obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
  1766. return arr.concat(part);
  1767. }, []));
  1768. return obj;
  1769. }, {});
  1770. };
  1771. var silence;
  1772. var silence_1 = function silence_1() {
  1773. if (!silence) {
  1774. // Frames-of-silence to use for filling in missing AAC frames
  1775. var coneOfSilence = {
  1776. 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
  1777. 88200: [highPrefix, [231], zeroFill(170), [56]],
  1778. 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
  1779. 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
  1780. 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
  1781. 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
  1782. 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
  1783. 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
  1784. 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]],
  1785. 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]],
  1786. 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
  1787. };
  1788. silence = makeTable(coneOfSilence);
  1789. }
  1790. return silence;
  1791. };
  1792. /**
  1793. * mux.js
  1794. *
  1795. * Copyright (c) Brightcove
  1796. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1797. */
  1798. var ONE_SECOND_IN_TS$3 = 90000,
  1799. // 90kHz clock
  1800. secondsToVideoTs,
  1801. secondsToAudioTs,
  1802. videoTsToSeconds,
  1803. audioTsToSeconds,
  1804. audioTsToVideoTs,
  1805. videoTsToAudioTs,
  1806. metadataTsToSeconds;
  1807. secondsToVideoTs = function secondsToVideoTs(seconds) {
  1808. return seconds * ONE_SECOND_IN_TS$3;
  1809. };
  1810. secondsToAudioTs = function secondsToAudioTs(seconds, sampleRate) {
  1811. return seconds * sampleRate;
  1812. };
  1813. videoTsToSeconds = function videoTsToSeconds(timestamp) {
  1814. return timestamp / ONE_SECOND_IN_TS$3;
  1815. };
  1816. audioTsToSeconds = function audioTsToSeconds(timestamp, sampleRate) {
  1817. return timestamp / sampleRate;
  1818. };
  1819. audioTsToVideoTs = function audioTsToVideoTs(timestamp, sampleRate) {
  1820. return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
  1821. };
  1822. videoTsToAudioTs = function videoTsToAudioTs(timestamp, sampleRate) {
  1823. return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
  1824. };
  1825. /**
  1826. * Adjust ID3 tag or caption timing information by the timeline pts values
  1827. * (if keepOriginalTimestamps is false) and convert to seconds
  1828. */
  1829. metadataTsToSeconds = function metadataTsToSeconds(timestamp, timelineStartPts, keepOriginalTimestamps) {
  1830. return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
  1831. };
  1832. var clock = {
  1833. ONE_SECOND_IN_TS: ONE_SECOND_IN_TS$3,
  1834. secondsToVideoTs: secondsToVideoTs,
  1835. secondsToAudioTs: secondsToAudioTs,
  1836. videoTsToSeconds: videoTsToSeconds,
  1837. audioTsToSeconds: audioTsToSeconds,
  1838. audioTsToVideoTs: audioTsToVideoTs,
  1839. videoTsToAudioTs: videoTsToAudioTs,
  1840. metadataTsToSeconds: metadataTsToSeconds
  1841. };
  1842. /**
  1843. * mux.js
  1844. *
  1845. * Copyright (c) Brightcove
  1846. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1847. */
  1848. /**
  1849. * Sum the `byteLength` properties of the data in each AAC frame
  1850. */
  1851. var sumFrameByteLengths = function sumFrameByteLengths(array) {
  1852. var i,
  1853. currentObj,
  1854. sum = 0; // sum the byteLength's all each nal unit in the frame
  1855. for (i = 0; i < array.length; i++) {
  1856. currentObj = array[i];
  1857. sum += currentObj.data.byteLength;
  1858. }
  1859. return sum;
  1860. }; // Possibly pad (prefix) the audio track with silence if appending this track
  1861. // would lead to the introduction of a gap in the audio buffer
  1862. var prefixWithSilence = function prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
  1863. var baseMediaDecodeTimeTs,
  1864. frameDuration = 0,
  1865. audioGapDuration = 0,
  1866. audioFillFrameCount = 0,
  1867. audioFillDuration = 0,
  1868. silentFrame,
  1869. i,
  1870. firstFrame;
  1871. if (!frames.length) {
  1872. return;
  1873. }
  1874. baseMediaDecodeTimeTs = clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate); // determine frame clock duration based on sample rate, round up to avoid overfills
  1875. frameDuration = Math.ceil(clock.ONE_SECOND_IN_TS / (track.samplerate / 1024));
  1876. if (audioAppendStartTs && videoBaseMediaDecodeTime) {
  1877. // insert the shortest possible amount (audio gap or audio to video gap)
  1878. audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime); // number of full frames in the audio gap
  1879. audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
  1880. audioFillDuration = audioFillFrameCount * frameDuration;
  1881. } // don't attempt to fill gaps smaller than a single frame or larger
  1882. // than a half second
  1883. if (audioFillFrameCount < 1 || audioFillDuration > clock.ONE_SECOND_IN_TS / 2) {
  1884. return;
  1885. }
  1886. silentFrame = silence_1()[track.samplerate];
  1887. if (!silentFrame) {
  1888. // we don't have a silent frame pregenerated for the sample rate, so use a frame
  1889. // from the content instead
  1890. silentFrame = frames[0].data;
  1891. }
  1892. for (i = 0; i < audioFillFrameCount; i++) {
  1893. firstFrame = frames[0];
  1894. frames.splice(0, 0, {
  1895. data: silentFrame,
  1896. dts: firstFrame.dts - frameDuration,
  1897. pts: firstFrame.pts - frameDuration
  1898. });
  1899. }
  1900. track.baseMediaDecodeTime -= Math.floor(clock.videoTsToAudioTs(audioFillDuration, track.samplerate));
  1901. return audioFillDuration;
  1902. }; // If the audio segment extends before the earliest allowed dts
  1903. // value, remove AAC frames until starts at or after the earliest
  1904. // allowed DTS so that we don't end up with a negative baseMedia-
  1905. // DecodeTime for the audio track
  1906. var trimAdtsFramesByEarliestDts = function trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts) {
  1907. if (track.minSegmentDts >= earliestAllowedDts) {
  1908. return adtsFrames;
  1909. } // We will need to recalculate the earliest segment Dts
  1910. track.minSegmentDts = Infinity;
  1911. return adtsFrames.filter(function (currentFrame) {
  1912. // If this is an allowed frame, keep it and record it's Dts
  1913. if (currentFrame.dts >= earliestAllowedDts) {
  1914. track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
  1915. track.minSegmentPts = track.minSegmentDts;
  1916. return true;
  1917. } // Otherwise, discard it
  1918. return false;
  1919. });
  1920. }; // generate the track's raw mdat data from an array of frames
  1921. var generateSampleTable = function generateSampleTable(frames) {
  1922. var i,
  1923. currentFrame,
  1924. samples = [];
  1925. for (i = 0; i < frames.length; i++) {
  1926. currentFrame = frames[i];
  1927. samples.push({
  1928. size: currentFrame.data.byteLength,
  1929. duration: 1024 // For AAC audio, all samples contain 1024 samples
  1930. });
  1931. }
  1932. return samples;
  1933. }; // generate the track's sample table from an array of frames
  1934. var concatenateFrameData = function concatenateFrameData(frames) {
  1935. var i,
  1936. currentFrame,
  1937. dataOffset = 0,
  1938. data = new Uint8Array(sumFrameByteLengths(frames));
  1939. for (i = 0; i < frames.length; i++) {
  1940. currentFrame = frames[i];
  1941. data.set(currentFrame.data, dataOffset);
  1942. dataOffset += currentFrame.data.byteLength;
  1943. }
  1944. return data;
  1945. };
  1946. var audioFrameUtils = {
  1947. prefixWithSilence: prefixWithSilence,
  1948. trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
  1949. generateSampleTable: generateSampleTable,
  1950. concatenateFrameData: concatenateFrameData
  1951. };
  1952. /**
  1953. * mux.js
  1954. *
  1955. * Copyright (c) Brightcove
  1956. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  1957. */
  1958. var ONE_SECOND_IN_TS$2 = clock.ONE_SECOND_IN_TS;
  1959. /**
  1960. * Store information about the start and end of the track and the
  1961. * duration for each frame/sample we process in order to calculate
  1962. * the baseMediaDecodeTime
  1963. */
  1964. var collectDtsInfo = function collectDtsInfo(track, data) {
  1965. if (typeof data.pts === 'number') {
  1966. if (track.timelineStartInfo.pts === undefined) {
  1967. track.timelineStartInfo.pts = data.pts;
  1968. }
  1969. if (track.minSegmentPts === undefined) {
  1970. track.minSegmentPts = data.pts;
  1971. } else {
  1972. track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
  1973. }
  1974. if (track.maxSegmentPts === undefined) {
  1975. track.maxSegmentPts = data.pts;
  1976. } else {
  1977. track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
  1978. }
  1979. }
  1980. if (typeof data.dts === 'number') {
  1981. if (track.timelineStartInfo.dts === undefined) {
  1982. track.timelineStartInfo.dts = data.dts;
  1983. }
  1984. if (track.minSegmentDts === undefined) {
  1985. track.minSegmentDts = data.dts;
  1986. } else {
  1987. track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
  1988. }
  1989. if (track.maxSegmentDts === undefined) {
  1990. track.maxSegmentDts = data.dts;
  1991. } else {
  1992. track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
  1993. }
  1994. }
  1995. };
  1996. /**
  1997. * Clear values used to calculate the baseMediaDecodeTime between
  1998. * tracks
  1999. */
  2000. var clearDtsInfo = function clearDtsInfo(track) {
  2001. delete track.minSegmentDts;
  2002. delete track.maxSegmentDts;
  2003. delete track.minSegmentPts;
  2004. delete track.maxSegmentPts;
  2005. };
  2006. /**
  2007. * Calculate the track's baseMediaDecodeTime based on the earliest
  2008. * DTS the transmuxer has ever seen and the minimum DTS for the
  2009. * current track
  2010. * @param track {object} track metadata configuration
  2011. * @param keepOriginalTimestamps {boolean} If true, keep the timestamps
  2012. * in the source; false to adjust the first segment to start at 0.
  2013. */
  2014. var calculateTrackBaseMediaDecodeTime = function calculateTrackBaseMediaDecodeTime(track, keepOriginalTimestamps) {
  2015. var baseMediaDecodeTime,
  2016. scale,
  2017. minSegmentDts = track.minSegmentDts; // Optionally adjust the time so the first segment starts at zero.
  2018. if (!keepOriginalTimestamps) {
  2019. minSegmentDts -= track.timelineStartInfo.dts;
  2020. } // track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
  2021. // we want the start of the first segment to be placed
  2022. baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime; // Add to that the distance this segment is from the very first
  2023. baseMediaDecodeTime += minSegmentDts; // baseMediaDecodeTime must not become negative
  2024. baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
  2025. if (track.type === 'audio') {
  2026. // Audio has a different clock equal to the sampling_rate so we need to
  2027. // scale the PTS values into the clock rate of the track
  2028. scale = track.samplerate / ONE_SECOND_IN_TS$2;
  2029. baseMediaDecodeTime *= scale;
  2030. baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
  2031. }
  2032. return baseMediaDecodeTime;
  2033. };
  2034. var trackDecodeInfo = {
  2035. clearDtsInfo: clearDtsInfo,
  2036. calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
  2037. collectDtsInfo: collectDtsInfo
  2038. };
  2039. /**
  2040. * mux.js
  2041. *
  2042. * Copyright (c) Brightcove
  2043. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  2044. *
  2045. * Reads in-band caption information from a video elementary
  2046. * stream. Captions must follow the CEA-708 standard for injection
  2047. * into an MPEG-2 transport streams.
  2048. * @see https://en.wikipedia.org/wiki/CEA-708
  2049. * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
  2050. */
  2051. // payload type field to indicate how they are to be
  2052. // interpreted. CEAS-708 caption content is always transmitted with
  2053. // payload type 0x04.
  2054. var USER_DATA_REGISTERED_ITU_T_T35 = 4,
  2055. RBSP_TRAILING_BITS = 128;
  2056. /**
  2057. * Parse a supplemental enhancement information (SEI) NAL unit.
  2058. * Stops parsing once a message of type ITU T T35 has been found.
  2059. *
  2060. * @param bytes {Uint8Array} the bytes of a SEI NAL unit
  2061. * @return {object} the parsed SEI payload
  2062. * @see Rec. ITU-T H.264, 7.3.2.3.1
  2063. */
  2064. var parseSei = function parseSei(bytes) {
  2065. var i = 0,
  2066. result = {
  2067. payloadType: -1,
  2068. payloadSize: 0
  2069. },
  2070. payloadType = 0,
  2071. payloadSize = 0; // go through the sei_rbsp parsing each each individual sei_message
  2072. while (i < bytes.byteLength) {
  2073. // stop once we have hit the end of the sei_rbsp
  2074. if (bytes[i] === RBSP_TRAILING_BITS) {
  2075. break;
  2076. } // Parse payload type
  2077. while (bytes[i] === 0xFF) {
  2078. payloadType += 255;
  2079. i++;
  2080. }
  2081. payloadType += bytes[i++]; // Parse payload size
  2082. while (bytes[i] === 0xFF) {
  2083. payloadSize += 255;
  2084. i++;
  2085. }
  2086. payloadSize += bytes[i++]; // this sei_message is a 608/708 caption so save it and break
  2087. // there can only ever be one caption message in a frame's sei
  2088. if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
  2089. var userIdentifier = String.fromCharCode(bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6]);
  2090. if (userIdentifier === 'GA94') {
  2091. result.payloadType = payloadType;
  2092. result.payloadSize = payloadSize;
  2093. result.payload = bytes.subarray(i, i + payloadSize);
  2094. break;
  2095. } else {
  2096. result.payload = void 0;
  2097. }
  2098. } // skip the payload and parse the next message
  2099. i += payloadSize;
  2100. payloadType = 0;
  2101. payloadSize = 0;
  2102. }
  2103. return result;
  2104. }; // see ANSI/SCTE 128-1 (2013), section 8.1
  2105. var parseUserData = function parseUserData(sei) {
  2106. // itu_t_t35_contry_code must be 181 (United States) for
  2107. // captions
  2108. if (sei.payload[0] !== 181) {
  2109. return null;
  2110. } // itu_t_t35_provider_code should be 49 (ATSC) for captions
  2111. if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
  2112. return null;
  2113. } // the user_identifier should be "GA94" to indicate ATSC1 data
  2114. if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
  2115. return null;
  2116. } // finally, user_data_type_code should be 0x03 for caption data
  2117. if (sei.payload[7] !== 0x03) {
  2118. return null;
  2119. } // return the user_data_type_structure and strip the trailing
  2120. // marker bits
  2121. return sei.payload.subarray(8, sei.payload.length - 1);
  2122. }; // see CEA-708-D, section 4.4
  2123. var parseCaptionPackets = function parseCaptionPackets(pts, userData) {
  2124. var results = [],
  2125. i,
  2126. count,
  2127. offset,
  2128. data; // if this is just filler, return immediately
  2129. if (!(userData[0] & 0x40)) {
  2130. return results;
  2131. } // parse out the cc_data_1 and cc_data_2 fields
  2132. count = userData[0] & 0x1f;
  2133. for (i = 0; i < count; i++) {
  2134. offset = i * 3;
  2135. data = {
  2136. type: userData[offset + 2] & 0x03,
  2137. pts: pts
  2138. }; // capture cc data when cc_valid is 1
  2139. if (userData[offset + 2] & 0x04) {
  2140. data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
  2141. results.push(data);
  2142. }
  2143. }
  2144. return results;
  2145. };
  2146. var discardEmulationPreventionBytes$1 = function discardEmulationPreventionBytes(data) {
  2147. var length = data.byteLength,
  2148. emulationPreventionBytesPositions = [],
  2149. i = 1,
  2150. newLength,
  2151. newData; // Find all `Emulation Prevention Bytes`
  2152. while (i < length - 2) {
  2153. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  2154. emulationPreventionBytesPositions.push(i + 2);
  2155. i += 2;
  2156. } else {
  2157. i++;
  2158. }
  2159. } // If no Emulation Prevention Bytes were found just return the original
  2160. // array
  2161. if (emulationPreventionBytesPositions.length === 0) {
  2162. return data;
  2163. } // Create a new array to hold the NAL unit data
  2164. newLength = length - emulationPreventionBytesPositions.length;
  2165. newData = new Uint8Array(newLength);
  2166. var sourceIndex = 0;
  2167. for (i = 0; i < newLength; sourceIndex++, i++) {
  2168. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  2169. // Skip this byte
  2170. sourceIndex++; // Remove this position index
  2171. emulationPreventionBytesPositions.shift();
  2172. }
  2173. newData[i] = data[sourceIndex];
  2174. }
  2175. return newData;
  2176. }; // exports
  2177. var captionPacketParser = {
  2178. parseSei: parseSei,
  2179. parseUserData: parseUserData,
  2180. parseCaptionPackets: parseCaptionPackets,
  2181. discardEmulationPreventionBytes: discardEmulationPreventionBytes$1,
  2182. USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
  2183. };
  2184. // Link To Transport
  2185. // -----------------
  2186. var CaptionStream$1 = function CaptionStream(options) {
  2187. options = options || {};
  2188. CaptionStream.prototype.init.call(this); // parse708captions flag, default to true
  2189. this.parse708captions_ = typeof options.parse708captions === 'boolean' ? options.parse708captions : true;
  2190. this.captionPackets_ = [];
  2191. this.ccStreams_ = [new Cea608Stream(0, 0), // eslint-disable-line no-use-before-define
  2192. new Cea608Stream(0, 1), // eslint-disable-line no-use-before-define
  2193. new Cea608Stream(1, 0), // eslint-disable-line no-use-before-define
  2194. new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
  2195. ];
  2196. if (this.parse708captions_) {
  2197. this.cc708Stream_ = new Cea708Stream({
  2198. captionServices: options.captionServices
  2199. }); // eslint-disable-line no-use-before-define
  2200. }
  2201. this.reset(); // forward data and done events from CCs to this CaptionStream
  2202. this.ccStreams_.forEach(function (cc) {
  2203. cc.on('data', this.trigger.bind(this, 'data'));
  2204. cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
  2205. cc.on('done', this.trigger.bind(this, 'done'));
  2206. }, this);
  2207. if (this.parse708captions_) {
  2208. this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
  2209. this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
  2210. this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
  2211. }
  2212. };
  2213. CaptionStream$1.prototype = new stream();
  2214. CaptionStream$1.prototype.push = function (event) {
  2215. var sei, userData, newCaptionPackets; // only examine SEI NALs
  2216. if (event.nalUnitType !== 'sei_rbsp') {
  2217. return;
  2218. } // parse the sei
  2219. sei = captionPacketParser.parseSei(event.escapedRBSP); // no payload data, skip
  2220. if (!sei.payload) {
  2221. return;
  2222. } // ignore everything but user_data_registered_itu_t_t35
  2223. if (sei.payloadType !== captionPacketParser.USER_DATA_REGISTERED_ITU_T_T35) {
  2224. return;
  2225. } // parse out the user data payload
  2226. userData = captionPacketParser.parseUserData(sei); // ignore unrecognized userData
  2227. if (!userData) {
  2228. return;
  2229. } // Sometimes, the same segment # will be downloaded twice. To stop the
  2230. // caption data from being processed twice, we track the latest dts we've
  2231. // received and ignore everything with a dts before that. However, since
  2232. // data for a specific dts can be split across packets on either side of
  2233. // a segment boundary, we need to make sure we *don't* ignore the packets
  2234. // from the *next* segment that have dts === this.latestDts_. By constantly
  2235. // tracking the number of packets received with dts === this.latestDts_, we
  2236. // know how many should be ignored once we start receiving duplicates.
  2237. if (event.dts < this.latestDts_) {
  2238. // We've started getting older data, so set the flag.
  2239. this.ignoreNextEqualDts_ = true;
  2240. return;
  2241. } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
  2242. this.numSameDts_--;
  2243. if (!this.numSameDts_) {
  2244. // We've received the last duplicate packet, time to start processing again
  2245. this.ignoreNextEqualDts_ = false;
  2246. }
  2247. return;
  2248. } // parse out CC data packets and save them for later
  2249. newCaptionPackets = captionPacketParser.parseCaptionPackets(event.pts, userData);
  2250. this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
  2251. if (this.latestDts_ !== event.dts) {
  2252. this.numSameDts_ = 0;
  2253. }
  2254. this.numSameDts_++;
  2255. this.latestDts_ = event.dts;
  2256. };
  2257. CaptionStream$1.prototype.flushCCStreams = function (flushType) {
  2258. this.ccStreams_.forEach(function (cc) {
  2259. return flushType === 'flush' ? cc.flush() : cc.partialFlush();
  2260. }, this);
  2261. };
  2262. CaptionStream$1.prototype.flushStream = function (flushType) {
  2263. // make sure we actually parsed captions before proceeding
  2264. if (!this.captionPackets_.length) {
  2265. this.flushCCStreams(flushType);
  2266. return;
  2267. } // In Chrome, the Array#sort function is not stable so add a
  2268. // presortIndex that we can use to ensure we get a stable-sort
  2269. this.captionPackets_.forEach(function (elem, idx) {
  2270. elem.presortIndex = idx;
  2271. }); // sort caption byte-pairs based on their PTS values
  2272. this.captionPackets_.sort(function (a, b) {
  2273. if (a.pts === b.pts) {
  2274. return a.presortIndex - b.presortIndex;
  2275. }
  2276. return a.pts - b.pts;
  2277. });
  2278. this.captionPackets_.forEach(function (packet) {
  2279. if (packet.type < 2) {
  2280. // Dispatch packet to the right Cea608Stream
  2281. this.dispatchCea608Packet(packet);
  2282. } else {
  2283. // Dispatch packet to the Cea708Stream
  2284. this.dispatchCea708Packet(packet);
  2285. }
  2286. }, this);
  2287. this.captionPackets_.length = 0;
  2288. this.flushCCStreams(flushType);
  2289. };
  2290. CaptionStream$1.prototype.flush = function () {
  2291. return this.flushStream('flush');
  2292. }; // Only called if handling partial data
  2293. CaptionStream$1.prototype.partialFlush = function () {
  2294. return this.flushStream('partialFlush');
  2295. };
  2296. CaptionStream$1.prototype.reset = function () {
  2297. this.latestDts_ = null;
  2298. this.ignoreNextEqualDts_ = false;
  2299. this.numSameDts_ = 0;
  2300. this.activeCea608Channel_ = [null, null];
  2301. this.ccStreams_.forEach(function (ccStream) {
  2302. ccStream.reset();
  2303. });
  2304. }; // From the CEA-608 spec:
  2305. /*
  2306. * When XDS sub-packets are interleaved with other services, the end of each sub-packet shall be followed
  2307. * by a control pair to change to a different service. When any of the control codes from 0x10 to 0x1F is
  2308. * used to begin a control code pair, it indicates the return to captioning or Text data. The control code pair
  2309. * and subsequent data should then be processed according to the FCC rules. It may be necessary for the
  2310. * line 21 data encoder to automatically insert a control code pair (i.e. RCL, RU2, RU3, RU4, RDC, or RTD)
  2311. * to switch to captioning or Text.
  2312. */
  2313. // With that in mind, we ignore any data between an XDS control code and a
  2314. // subsequent closed-captioning control code.
  2315. CaptionStream$1.prototype.dispatchCea608Packet = function (packet) {
  2316. // NOTE: packet.type is the CEA608 field
  2317. if (this.setsTextOrXDSActive(packet)) {
  2318. this.activeCea608Channel_[packet.type] = null;
  2319. } else if (this.setsChannel1Active(packet)) {
  2320. this.activeCea608Channel_[packet.type] = 0;
  2321. } else if (this.setsChannel2Active(packet)) {
  2322. this.activeCea608Channel_[packet.type] = 1;
  2323. }
  2324. if (this.activeCea608Channel_[packet.type] === null) {
  2325. // If we haven't received anything to set the active channel, or the
  2326. // packets are Text/XDS data, discard the data; we don't want jumbled
  2327. // captions
  2328. return;
  2329. }
  2330. this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
  2331. };
  2332. CaptionStream$1.prototype.setsChannel1Active = function (packet) {
  2333. return (packet.ccData & 0x7800) === 0x1000;
  2334. };
  2335. CaptionStream$1.prototype.setsChannel2Active = function (packet) {
  2336. return (packet.ccData & 0x7800) === 0x1800;
  2337. };
  2338. CaptionStream$1.prototype.setsTextOrXDSActive = function (packet) {
  2339. return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
  2340. };
  2341. CaptionStream$1.prototype.dispatchCea708Packet = function (packet) {
  2342. if (this.parse708captions_) {
  2343. this.cc708Stream_.push(packet);
  2344. }
  2345. }; // ----------------------
  2346. // Session to Application
  2347. // ----------------------
  2348. // This hash maps special and extended character codes to their
  2349. // proper Unicode equivalent. The first one-byte key is just a
  2350. // non-standard character code. The two-byte keys that follow are
  2351. // the extended CEA708 character codes, along with the preceding
  2352. // 0x10 extended character byte to distinguish these codes from
  2353. // non-extended character codes. Every CEA708 character code that
  2354. // is not in this object maps directly to a standard unicode
  2355. // character code.
  2356. // The transparent space and non-breaking transparent space are
  2357. // technically not fully supported since there is no code to
  2358. // make them transparent, so they have normal non-transparent
  2359. // stand-ins.
  2360. // The special closed caption (CC) character isn't a standard
  2361. // unicode character, so a fairly similar unicode character was
  2362. // chosen in it's place.
  2363. var CHARACTER_TRANSLATION_708 = {
  2364. 0x7f: 0x266a,
  2365. // ♪
  2366. 0x1020: 0x20,
  2367. // Transparent Space
  2368. 0x1021: 0xa0,
  2369. // Nob-breaking Transparent Space
  2370. 0x1025: 0x2026,
  2371. // …
  2372. 0x102a: 0x0160,
  2373. // Š
  2374. 0x102c: 0x0152,
  2375. // Œ
  2376. 0x1030: 0x2588,
  2377. // █
  2378. 0x1031: 0x2018,
  2379. // ‘
  2380. 0x1032: 0x2019,
  2381. // ’
  2382. 0x1033: 0x201c,
  2383. // “
  2384. 0x1034: 0x201d,
  2385. // ”
  2386. 0x1035: 0x2022,
  2387. // •
  2388. 0x1039: 0x2122,
  2389. // ™
  2390. 0x103a: 0x0161,
  2391. // š
  2392. 0x103c: 0x0153,
  2393. // œ
  2394. 0x103d: 0x2120,
  2395. // ℠
  2396. 0x103f: 0x0178,
  2397. // Ÿ
  2398. 0x1076: 0x215b,
  2399. // ⅛
  2400. 0x1077: 0x215c,
  2401. // ⅜
  2402. 0x1078: 0x215d,
  2403. // ⅝
  2404. 0x1079: 0x215e,
  2405. // ⅞
  2406. 0x107a: 0x23d0,
  2407. // ⏐
  2408. 0x107b: 0x23a4,
  2409. // ⎤
  2410. 0x107c: 0x23a3,
  2411. // ⎣
  2412. 0x107d: 0x23af,
  2413. // ⎯
  2414. 0x107e: 0x23a6,
  2415. // ⎦
  2416. 0x107f: 0x23a1,
  2417. // ⎡
  2418. 0x10a0: 0x3138 // ㄸ (CC char)
  2419. };
  2420. var get708CharFromCode = function get708CharFromCode(code) {
  2421. var newCode = CHARACTER_TRANSLATION_708[code] || code;
  2422. if (code & 0x1000 && code === newCode) {
  2423. // Invalid extended code
  2424. return '';
  2425. }
  2426. return String.fromCharCode(newCode);
  2427. };
  2428. var within708TextBlock = function within708TextBlock(b) {
  2429. return 0x20 <= b && b <= 0x7f || 0xa0 <= b && b <= 0xff;
  2430. };
  2431. var Cea708Window = function Cea708Window(windowNum) {
  2432. this.windowNum = windowNum;
  2433. this.reset();
  2434. };
  2435. Cea708Window.prototype.reset = function () {
  2436. this.clearText();
  2437. this.pendingNewLine = false;
  2438. this.winAttr = {};
  2439. this.penAttr = {};
  2440. this.penLoc = {};
  2441. this.penColor = {}; // These default values are arbitrary,
  2442. // defineWindow will usually override them
  2443. this.visible = 0;
  2444. this.rowLock = 0;
  2445. this.columnLock = 0;
  2446. this.priority = 0;
  2447. this.relativePositioning = 0;
  2448. this.anchorVertical = 0;
  2449. this.anchorHorizontal = 0;
  2450. this.anchorPoint = 0;
  2451. this.rowCount = 1;
  2452. this.virtualRowCount = this.rowCount + 1;
  2453. this.columnCount = 41;
  2454. this.windowStyle = 0;
  2455. this.penStyle = 0;
  2456. };
  2457. Cea708Window.prototype.getText = function () {
  2458. return this.rows.join('\n');
  2459. };
  2460. Cea708Window.prototype.clearText = function () {
  2461. this.rows = [''];
  2462. this.rowIdx = 0;
  2463. };
  2464. Cea708Window.prototype.newLine = function (pts) {
  2465. if (this.rows.length >= this.virtualRowCount && typeof this.beforeRowOverflow === 'function') {
  2466. this.beforeRowOverflow(pts);
  2467. }
  2468. if (this.rows.length > 0) {
  2469. this.rows.push('');
  2470. this.rowIdx++;
  2471. } // Show all virtual rows since there's no visible scrolling
  2472. while (this.rows.length > this.virtualRowCount) {
  2473. this.rows.shift();
  2474. this.rowIdx--;
  2475. }
  2476. };
  2477. Cea708Window.prototype.isEmpty = function () {
  2478. if (this.rows.length === 0) {
  2479. return true;
  2480. } else if (this.rows.length === 1) {
  2481. return this.rows[0] === '';
  2482. }
  2483. return false;
  2484. };
  2485. Cea708Window.prototype.addText = function (text) {
  2486. this.rows[this.rowIdx] += text;
  2487. };
  2488. Cea708Window.prototype.backspace = function () {
  2489. if (!this.isEmpty()) {
  2490. var row = this.rows[this.rowIdx];
  2491. this.rows[this.rowIdx] = row.substr(0, row.length - 1);
  2492. }
  2493. };
  2494. var Cea708Service = function Cea708Service(serviceNum, encoding, stream) {
  2495. this.serviceNum = serviceNum;
  2496. this.text = '';
  2497. this.currentWindow = new Cea708Window(-1);
  2498. this.windows = [];
  2499. this.stream = stream; // Try to setup a TextDecoder if an `encoding` value was provided
  2500. if (typeof encoding === 'string') {
  2501. this.createTextDecoder(encoding);
  2502. }
  2503. };
  2504. /**
  2505. * Initialize service windows
  2506. * Must be run before service use
  2507. *
  2508. * @param {Integer} pts PTS value
  2509. * @param {Function} beforeRowOverflow Function to execute before row overflow of a window
  2510. */
  2511. Cea708Service.prototype.init = function (pts, beforeRowOverflow) {
  2512. this.startPts = pts;
  2513. for (var win = 0; win < 8; win++) {
  2514. this.windows[win] = new Cea708Window(win);
  2515. if (typeof beforeRowOverflow === 'function') {
  2516. this.windows[win].beforeRowOverflow = beforeRowOverflow;
  2517. }
  2518. }
  2519. };
  2520. /**
  2521. * Set current window of service to be affected by commands
  2522. *
  2523. * @param {Integer} windowNum Window number
  2524. */
  2525. Cea708Service.prototype.setCurrentWindow = function (windowNum) {
  2526. this.currentWindow = this.windows[windowNum];
  2527. };
  2528. /**
  2529. * Try to create a TextDecoder if it is natively supported
  2530. */
  2531. Cea708Service.prototype.createTextDecoder = function (encoding) {
  2532. if (typeof TextDecoder === 'undefined') {
  2533. this.stream.trigger('log', {
  2534. level: 'warn',
  2535. message: 'The `encoding` option is unsupported without TextDecoder support'
  2536. });
  2537. } else {
  2538. try {
  2539. this.textDecoder_ = new TextDecoder(encoding);
  2540. } catch (error) {
  2541. this.stream.trigger('log', {
  2542. level: 'warn',
  2543. message: 'TextDecoder could not be created with ' + encoding + ' encoding. ' + error
  2544. });
  2545. }
  2546. }
  2547. };
  2548. var Cea708Stream = function Cea708Stream(options) {
  2549. options = options || {};
  2550. Cea708Stream.prototype.init.call(this);
  2551. var self = this;
  2552. var captionServices = options.captionServices || {};
  2553. var captionServiceEncodings = {};
  2554. var serviceProps; // Get service encodings from captionServices option block
  2555. Object.keys(captionServices).forEach(function (serviceName) {
  2556. serviceProps = captionServices[serviceName];
  2557. if (/^SERVICE/.test(serviceName)) {
  2558. captionServiceEncodings[serviceName] = serviceProps.encoding;
  2559. }
  2560. });
  2561. this.serviceEncodings = captionServiceEncodings;
  2562. this.current708Packet = null;
  2563. this.services = {};
  2564. this.push = function (packet) {
  2565. if (packet.type === 3) {
  2566. // 708 packet start
  2567. self.new708Packet();
  2568. self.add708Bytes(packet);
  2569. } else {
  2570. if (self.current708Packet === null) {
  2571. // This should only happen at the start of a file if there's no packet start.
  2572. self.new708Packet();
  2573. }
  2574. self.add708Bytes(packet);
  2575. }
  2576. };
  2577. };
  2578. Cea708Stream.prototype = new stream();
  2579. /**
  2580. * Push current 708 packet, create new 708 packet.
  2581. */
  2582. Cea708Stream.prototype.new708Packet = function () {
  2583. if (this.current708Packet !== null) {
  2584. this.push708Packet();
  2585. }
  2586. this.current708Packet = {
  2587. data: [],
  2588. ptsVals: []
  2589. };
  2590. };
  2591. /**
  2592. * Add pts and both bytes from packet into current 708 packet.
  2593. */
  2594. Cea708Stream.prototype.add708Bytes = function (packet) {
  2595. var data = packet.ccData;
  2596. var byte0 = data >>> 8;
  2597. var byte1 = data & 0xff; // I would just keep a list of packets instead of bytes, but it isn't clear in the spec
  2598. // that service blocks will always line up with byte pairs.
  2599. this.current708Packet.ptsVals.push(packet.pts);
  2600. this.current708Packet.data.push(byte0);
  2601. this.current708Packet.data.push(byte1);
  2602. };
  2603. /**
  2604. * Parse completed 708 packet into service blocks and push each service block.
  2605. */
  2606. Cea708Stream.prototype.push708Packet = function () {
  2607. var packet708 = this.current708Packet;
  2608. var packetData = packet708.data;
  2609. var serviceNum = null;
  2610. var blockSize = null;
  2611. var i = 0;
  2612. var b = packetData[i++];
  2613. packet708.seq = b >> 6;
  2614. packet708.sizeCode = b & 0x3f; // 0b00111111;
  2615. for (; i < packetData.length; i++) {
  2616. b = packetData[i++];
  2617. serviceNum = b >> 5;
  2618. blockSize = b & 0x1f; // 0b00011111
  2619. if (serviceNum === 7 && blockSize > 0) {
  2620. // Extended service num
  2621. b = packetData[i++];
  2622. serviceNum = b;
  2623. }
  2624. this.pushServiceBlock(serviceNum, i, blockSize);
  2625. if (blockSize > 0) {
  2626. i += blockSize - 1;
  2627. }
  2628. }
  2629. };
  2630. /**
  2631. * Parse service block, execute commands, read text.
  2632. *
  2633. * Note: While many of these commands serve important purposes,
  2634. * many others just parse out the parameters or attributes, but
  2635. * nothing is done with them because this is not a full and complete
  2636. * implementation of the entire 708 spec.
  2637. *
  2638. * @param {Integer} serviceNum Service number
  2639. * @param {Integer} start Start index of the 708 packet data
  2640. * @param {Integer} size Block size
  2641. */
  2642. Cea708Stream.prototype.pushServiceBlock = function (serviceNum, start, size) {
  2643. var b;
  2644. var i = start;
  2645. var packetData = this.current708Packet.data;
  2646. var service = this.services[serviceNum];
  2647. if (!service) {
  2648. service = this.initService(serviceNum, i);
  2649. }
  2650. for (; i < start + size && i < packetData.length; i++) {
  2651. b = packetData[i];
  2652. if (within708TextBlock(b)) {
  2653. i = this.handleText(i, service);
  2654. } else if (b === 0x18) {
  2655. i = this.multiByteCharacter(i, service);
  2656. } else if (b === 0x10) {
  2657. i = this.extendedCommands(i, service);
  2658. } else if (0x80 <= b && b <= 0x87) {
  2659. i = this.setCurrentWindow(i, service);
  2660. } else if (0x98 <= b && b <= 0x9f) {
  2661. i = this.defineWindow(i, service);
  2662. } else if (b === 0x88) {
  2663. i = this.clearWindows(i, service);
  2664. } else if (b === 0x8c) {
  2665. i = this.deleteWindows(i, service);
  2666. } else if (b === 0x89) {
  2667. i = this.displayWindows(i, service);
  2668. } else if (b === 0x8a) {
  2669. i = this.hideWindows(i, service);
  2670. } else if (b === 0x8b) {
  2671. i = this.toggleWindows(i, service);
  2672. } else if (b === 0x97) {
  2673. i = this.setWindowAttributes(i, service);
  2674. } else if (b === 0x90) {
  2675. i = this.setPenAttributes(i, service);
  2676. } else if (b === 0x91) {
  2677. i = this.setPenColor(i, service);
  2678. } else if (b === 0x92) {
  2679. i = this.setPenLocation(i, service);
  2680. } else if (b === 0x8f) {
  2681. service = this.reset(i, service);
  2682. } else if (b === 0x08) {
  2683. // BS: Backspace
  2684. service.currentWindow.backspace();
  2685. } else if (b === 0x0c) {
  2686. // FF: Form feed
  2687. service.currentWindow.clearText();
  2688. } else if (b === 0x0d) {
  2689. // CR: Carriage return
  2690. service.currentWindow.pendingNewLine = true;
  2691. } else if (b === 0x0e) {
  2692. // HCR: Horizontal carriage return
  2693. service.currentWindow.clearText();
  2694. } else if (b === 0x8d) {
  2695. // DLY: Delay, nothing to do
  2696. i++;
  2697. } else ;
  2698. }
  2699. };
  2700. /**
  2701. * Execute an extended command
  2702. *
  2703. * @param {Integer} i Current index in the 708 packet
  2704. * @param {Service} service The service object to be affected
  2705. * @return {Integer} New index after parsing
  2706. */
  2707. Cea708Stream.prototype.extendedCommands = function (i, service) {
  2708. var packetData = this.current708Packet.data;
  2709. var b = packetData[++i];
  2710. if (within708TextBlock(b)) {
  2711. i = this.handleText(i, service, {
  2712. isExtended: true
  2713. });
  2714. }
  2715. return i;
  2716. };
  2717. /**
  2718. * Get PTS value of a given byte index
  2719. *
  2720. * @param {Integer} byteIndex Index of the byte
  2721. * @return {Integer} PTS
  2722. */
  2723. Cea708Stream.prototype.getPts = function (byteIndex) {
  2724. // There's 1 pts value per 2 bytes
  2725. return this.current708Packet.ptsVals[Math.floor(byteIndex / 2)];
  2726. };
  2727. /**
  2728. * Initializes a service
  2729. *
  2730. * @param {Integer} serviceNum Service number
  2731. * @return {Service} Initialized service object
  2732. */
  2733. Cea708Stream.prototype.initService = function (serviceNum, i) {
  2734. var serviceName = 'SERVICE' + serviceNum;
  2735. var self = this;
  2736. var serviceName;
  2737. var encoding;
  2738. if (serviceName in this.serviceEncodings) {
  2739. encoding = this.serviceEncodings[serviceName];
  2740. }
  2741. this.services[serviceNum] = new Cea708Service(serviceNum, encoding, self);
  2742. this.services[serviceNum].init(this.getPts(i), function (pts) {
  2743. self.flushDisplayed(pts, self.services[serviceNum]);
  2744. });
  2745. return this.services[serviceNum];
  2746. };
  2747. /**
  2748. * Execute text writing to current window
  2749. *
  2750. * @param {Integer} i Current index in the 708 packet
  2751. * @param {Service} service The service object to be affected
  2752. * @return {Integer} New index after parsing
  2753. */
  2754. Cea708Stream.prototype.handleText = function (i, service, options) {
  2755. var isExtended = options && options.isExtended;
  2756. var isMultiByte = options && options.isMultiByte;
  2757. var packetData = this.current708Packet.data;
  2758. var extended = isExtended ? 0x1000 : 0x0000;
  2759. var currentByte = packetData[i];
  2760. var nextByte = packetData[i + 1];
  2761. var win = service.currentWindow;
  2762. var char;
  2763. var charCodeArray; // Converts an array of bytes to a unicode hex string.
  2764. function toHexString(byteArray) {
  2765. return byteArray.map(function (byte) {
  2766. return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  2767. }).join('');
  2768. }
  2769. if (isMultiByte) {
  2770. charCodeArray = [currentByte, nextByte];
  2771. i++;
  2772. } else {
  2773. charCodeArray = [currentByte];
  2774. } // Use the TextDecoder if one was created for this service
  2775. if (service.textDecoder_ && !isExtended) {
  2776. char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
  2777. } else {
  2778. // We assume any multi-byte char without a decoder is unicode.
  2779. if (isMultiByte) {
  2780. var unicode = toHexString(charCodeArray); // Takes a unicode hex string and creates a single character.
  2781. char = String.fromCharCode(parseInt(unicode, 16));
  2782. } else {
  2783. char = get708CharFromCode(extended | currentByte);
  2784. }
  2785. }
  2786. if (win.pendingNewLine && !win.isEmpty()) {
  2787. win.newLine(this.getPts(i));
  2788. }
  2789. win.pendingNewLine = false;
  2790. win.addText(char);
  2791. return i;
  2792. };
  2793. /**
  2794. * Handle decoding of multibyte character
  2795. *
  2796. * @param {Integer} i Current index in the 708 packet
  2797. * @param {Service} service The service object to be affected
  2798. * @return {Integer} New index after parsing
  2799. */
  2800. Cea708Stream.prototype.multiByteCharacter = function (i, service) {
  2801. var packetData = this.current708Packet.data;
  2802. var firstByte = packetData[i + 1];
  2803. var secondByte = packetData[i + 2];
  2804. if (within708TextBlock(firstByte) && within708TextBlock(secondByte)) {
  2805. i = this.handleText(++i, service, {
  2806. isMultiByte: true
  2807. });
  2808. }
  2809. return i;
  2810. };
  2811. /**
  2812. * Parse and execute the CW# command.
  2813. *
  2814. * Set the current window.
  2815. *
  2816. * @param {Integer} i Current index in the 708 packet
  2817. * @param {Service} service The service object to be affected
  2818. * @return {Integer} New index after parsing
  2819. */
  2820. Cea708Stream.prototype.setCurrentWindow = function (i, service) {
  2821. var packetData = this.current708Packet.data;
  2822. var b = packetData[i];
  2823. var windowNum = b & 0x07;
  2824. service.setCurrentWindow(windowNum);
  2825. return i;
  2826. };
  2827. /**
  2828. * Parse and execute the DF# command.
  2829. *
  2830. * Define a window and set it as the current window.
  2831. *
  2832. * @param {Integer} i Current index in the 708 packet
  2833. * @param {Service} service The service object to be affected
  2834. * @return {Integer} New index after parsing
  2835. */
  2836. Cea708Stream.prototype.defineWindow = function (i, service) {
  2837. var packetData = this.current708Packet.data;
  2838. var b = packetData[i];
  2839. var windowNum = b & 0x07;
  2840. service.setCurrentWindow(windowNum);
  2841. var win = service.currentWindow;
  2842. b = packetData[++i];
  2843. win.visible = (b & 0x20) >> 5; // v
  2844. win.rowLock = (b & 0x10) >> 4; // rl
  2845. win.columnLock = (b & 0x08) >> 3; // cl
  2846. win.priority = b & 0x07; // p
  2847. b = packetData[++i];
  2848. win.relativePositioning = (b & 0x80) >> 7; // rp
  2849. win.anchorVertical = b & 0x7f; // av
  2850. b = packetData[++i];
  2851. win.anchorHorizontal = b; // ah
  2852. b = packetData[++i];
  2853. win.anchorPoint = (b & 0xf0) >> 4; // ap
  2854. win.rowCount = b & 0x0f; // rc
  2855. b = packetData[++i];
  2856. win.columnCount = b & 0x3f; // cc
  2857. b = packetData[++i];
  2858. win.windowStyle = (b & 0x38) >> 3; // ws
  2859. win.penStyle = b & 0x07; // ps
  2860. // The spec says there are (rowCount+1) "virtual rows"
  2861. win.virtualRowCount = win.rowCount + 1;
  2862. return i;
  2863. };
  2864. /**
  2865. * Parse and execute the SWA command.
  2866. *
  2867. * Set attributes of the current window.
  2868. *
  2869. * @param {Integer} i Current index in the 708 packet
  2870. * @param {Service} service The service object to be affected
  2871. * @return {Integer} New index after parsing
  2872. */
  2873. Cea708Stream.prototype.setWindowAttributes = function (i, service) {
  2874. var packetData = this.current708Packet.data;
  2875. var b = packetData[i];
  2876. var winAttr = service.currentWindow.winAttr;
  2877. b = packetData[++i];
  2878. winAttr.fillOpacity = (b & 0xc0) >> 6; // fo
  2879. winAttr.fillRed = (b & 0x30) >> 4; // fr
  2880. winAttr.fillGreen = (b & 0x0c) >> 2; // fg
  2881. winAttr.fillBlue = b & 0x03; // fb
  2882. b = packetData[++i];
  2883. winAttr.borderType = (b & 0xc0) >> 6; // bt
  2884. winAttr.borderRed = (b & 0x30) >> 4; // br
  2885. winAttr.borderGreen = (b & 0x0c) >> 2; // bg
  2886. winAttr.borderBlue = b & 0x03; // bb
  2887. b = packetData[++i];
  2888. winAttr.borderType += (b & 0x80) >> 5; // bt
  2889. winAttr.wordWrap = (b & 0x40) >> 6; // ww
  2890. winAttr.printDirection = (b & 0x30) >> 4; // pd
  2891. winAttr.scrollDirection = (b & 0x0c) >> 2; // sd
  2892. winAttr.justify = b & 0x03; // j
  2893. b = packetData[++i];
  2894. winAttr.effectSpeed = (b & 0xf0) >> 4; // es
  2895. winAttr.effectDirection = (b & 0x0c) >> 2; // ed
  2896. winAttr.displayEffect = b & 0x03; // de
  2897. return i;
  2898. };
  2899. /**
  2900. * Gather text from all displayed windows and push a caption to output.
  2901. *
  2902. * @param {Integer} i Current index in the 708 packet
  2903. * @param {Service} service The service object to be affected
  2904. */
  2905. Cea708Stream.prototype.flushDisplayed = function (pts, service) {
  2906. var displayedText = []; // TODO: Positioning not supported, displaying multiple windows will not necessarily
  2907. // display text in the correct order, but sample files so far have not shown any issue.
  2908. for (var winId = 0; winId < 8; winId++) {
  2909. if (service.windows[winId].visible && !service.windows[winId].isEmpty()) {
  2910. displayedText.push(service.windows[winId].getText());
  2911. }
  2912. }
  2913. service.endPts = pts;
  2914. service.text = displayedText.join('\n\n');
  2915. this.pushCaption(service);
  2916. service.startPts = pts;
  2917. };
  2918. /**
  2919. * Push a caption to output if the caption contains text.
  2920. *
  2921. * @param {Service} service The service object to be affected
  2922. */
  2923. Cea708Stream.prototype.pushCaption = function (service) {
  2924. if (service.text !== '') {
  2925. this.trigger('data', {
  2926. startPts: service.startPts,
  2927. endPts: service.endPts,
  2928. text: service.text,
  2929. stream: 'cc708_' + service.serviceNum
  2930. });
  2931. service.text = '';
  2932. service.startPts = service.endPts;
  2933. }
  2934. };
  2935. /**
  2936. * Parse and execute the DSW command.
  2937. *
  2938. * Set visible property of windows based on the parsed bitmask.
  2939. *
  2940. * @param {Integer} i Current index in the 708 packet
  2941. * @param {Service} service The service object to be affected
  2942. * @return {Integer} New index after parsing
  2943. */
  2944. Cea708Stream.prototype.displayWindows = function (i, service) {
  2945. var packetData = this.current708Packet.data;
  2946. var b = packetData[++i];
  2947. var pts = this.getPts(i);
  2948. this.flushDisplayed(pts, service);
  2949. for (var winId = 0; winId < 8; winId++) {
  2950. if (b & 0x01 << winId) {
  2951. service.windows[winId].visible = 1;
  2952. }
  2953. }
  2954. return i;
  2955. };
  2956. /**
  2957. * Parse and execute the HDW command.
  2958. *
  2959. * Set visible property of windows based on the parsed bitmask.
  2960. *
  2961. * @param {Integer} i Current index in the 708 packet
  2962. * @param {Service} service The service object to be affected
  2963. * @return {Integer} New index after parsing
  2964. */
  2965. Cea708Stream.prototype.hideWindows = function (i, service) {
  2966. var packetData = this.current708Packet.data;
  2967. var b = packetData[++i];
  2968. var pts = this.getPts(i);
  2969. this.flushDisplayed(pts, service);
  2970. for (var winId = 0; winId < 8; winId++) {
  2971. if (b & 0x01 << winId) {
  2972. service.windows[winId].visible = 0;
  2973. }
  2974. }
  2975. return i;
  2976. };
  2977. /**
  2978. * Parse and execute the TGW command.
  2979. *
  2980. * Set visible property of windows based on the parsed bitmask.
  2981. *
  2982. * @param {Integer} i Current index in the 708 packet
  2983. * @param {Service} service The service object to be affected
  2984. * @return {Integer} New index after parsing
  2985. */
  2986. Cea708Stream.prototype.toggleWindows = function (i, service) {
  2987. var packetData = this.current708Packet.data;
  2988. var b = packetData[++i];
  2989. var pts = this.getPts(i);
  2990. this.flushDisplayed(pts, service);
  2991. for (var winId = 0; winId < 8; winId++) {
  2992. if (b & 0x01 << winId) {
  2993. service.windows[winId].visible ^= 1;
  2994. }
  2995. }
  2996. return i;
  2997. };
  2998. /**
  2999. * Parse and execute the CLW command.
  3000. *
  3001. * Clear text of windows based on the parsed bitmask.
  3002. *
  3003. * @param {Integer} i Current index in the 708 packet
  3004. * @param {Service} service The service object to be affected
  3005. * @return {Integer} New index after parsing
  3006. */
  3007. Cea708Stream.prototype.clearWindows = function (i, service) {
  3008. var packetData = this.current708Packet.data;
  3009. var b = packetData[++i];
  3010. var pts = this.getPts(i);
  3011. this.flushDisplayed(pts, service);
  3012. for (var winId = 0; winId < 8; winId++) {
  3013. if (b & 0x01 << winId) {
  3014. service.windows[winId].clearText();
  3015. }
  3016. }
  3017. return i;
  3018. };
  3019. /**
  3020. * Parse and execute the DLW command.
  3021. *
  3022. * Re-initialize windows based on the parsed bitmask.
  3023. *
  3024. * @param {Integer} i Current index in the 708 packet
  3025. * @param {Service} service The service object to be affected
  3026. * @return {Integer} New index after parsing
  3027. */
  3028. Cea708Stream.prototype.deleteWindows = function (i, service) {
  3029. var packetData = this.current708Packet.data;
  3030. var b = packetData[++i];
  3031. var pts = this.getPts(i);
  3032. this.flushDisplayed(pts, service);
  3033. for (var winId = 0; winId < 8; winId++) {
  3034. if (b & 0x01 << winId) {
  3035. service.windows[winId].reset();
  3036. }
  3037. }
  3038. return i;
  3039. };
  3040. /**
  3041. * Parse and execute the SPA command.
  3042. *
  3043. * Set pen attributes of the current window.
  3044. *
  3045. * @param {Integer} i Current index in the 708 packet
  3046. * @param {Service} service The service object to be affected
  3047. * @return {Integer} New index after parsing
  3048. */
  3049. Cea708Stream.prototype.setPenAttributes = function (i, service) {
  3050. var packetData = this.current708Packet.data;
  3051. var b = packetData[i];
  3052. var penAttr = service.currentWindow.penAttr;
  3053. b = packetData[++i];
  3054. penAttr.textTag = (b & 0xf0) >> 4; // tt
  3055. penAttr.offset = (b & 0x0c) >> 2; // o
  3056. penAttr.penSize = b & 0x03; // s
  3057. b = packetData[++i];
  3058. penAttr.italics = (b & 0x80) >> 7; // i
  3059. penAttr.underline = (b & 0x40) >> 6; // u
  3060. penAttr.edgeType = (b & 0x38) >> 3; // et
  3061. penAttr.fontStyle = b & 0x07; // fs
  3062. return i;
  3063. };
  3064. /**
  3065. * Parse and execute the SPC command.
  3066. *
  3067. * Set pen color of the current window.
  3068. *
  3069. * @param {Integer} i Current index in the 708 packet
  3070. * @param {Service} service The service object to be affected
  3071. * @return {Integer} New index after parsing
  3072. */
  3073. Cea708Stream.prototype.setPenColor = function (i, service) {
  3074. var packetData = this.current708Packet.data;
  3075. var b = packetData[i];
  3076. var penColor = service.currentWindow.penColor;
  3077. b = packetData[++i];
  3078. penColor.fgOpacity = (b & 0xc0) >> 6; // fo
  3079. penColor.fgRed = (b & 0x30) >> 4; // fr
  3080. penColor.fgGreen = (b & 0x0c) >> 2; // fg
  3081. penColor.fgBlue = b & 0x03; // fb
  3082. b = packetData[++i];
  3083. penColor.bgOpacity = (b & 0xc0) >> 6; // bo
  3084. penColor.bgRed = (b & 0x30) >> 4; // br
  3085. penColor.bgGreen = (b & 0x0c) >> 2; // bg
  3086. penColor.bgBlue = b & 0x03; // bb
  3087. b = packetData[++i];
  3088. penColor.edgeRed = (b & 0x30) >> 4; // er
  3089. penColor.edgeGreen = (b & 0x0c) >> 2; // eg
  3090. penColor.edgeBlue = b & 0x03; // eb
  3091. return i;
  3092. };
  3093. /**
  3094. * Parse and execute the SPL command.
  3095. *
  3096. * Set pen location of the current window.
  3097. *
  3098. * @param {Integer} i Current index in the 708 packet
  3099. * @param {Service} service The service object to be affected
  3100. * @return {Integer} New index after parsing
  3101. */
  3102. Cea708Stream.prototype.setPenLocation = function (i, service) {
  3103. var packetData = this.current708Packet.data;
  3104. var b = packetData[i];
  3105. var penLoc = service.currentWindow.penLoc; // Positioning isn't really supported at the moment, so this essentially just inserts a linebreak
  3106. service.currentWindow.pendingNewLine = true;
  3107. b = packetData[++i];
  3108. penLoc.row = b & 0x0f; // r
  3109. b = packetData[++i];
  3110. penLoc.column = b & 0x3f; // c
  3111. return i;
  3112. };
  3113. /**
  3114. * Execute the RST command.
  3115. *
  3116. * Reset service to a clean slate. Re-initialize.
  3117. *
  3118. * @param {Integer} i Current index in the 708 packet
  3119. * @param {Service} service The service object to be affected
  3120. * @return {Service} Re-initialized service
  3121. */
  3122. Cea708Stream.prototype.reset = function (i, service) {
  3123. var pts = this.getPts(i);
  3124. this.flushDisplayed(pts, service);
  3125. return this.initService(service.serviceNum, i);
  3126. }; // This hash maps non-ASCII, special, and extended character codes to their
  3127. // proper Unicode equivalent. The first keys that are only a single byte
  3128. // are the non-standard ASCII characters, which simply map the CEA608 byte
  3129. // to the standard ASCII/Unicode. The two-byte keys that follow are the CEA608
  3130. // character codes, but have their MSB bitmasked with 0x03 so that a lookup
  3131. // can be performed regardless of the field and data channel on which the
  3132. // character code was received.
  3133. var CHARACTER_TRANSLATION = {
  3134. 0x2a: 0xe1,
  3135. // á
  3136. 0x5c: 0xe9,
  3137. // é
  3138. 0x5e: 0xed,
  3139. // í
  3140. 0x5f: 0xf3,
  3141. // ó
  3142. 0x60: 0xfa,
  3143. // ú
  3144. 0x7b: 0xe7,
  3145. // ç
  3146. 0x7c: 0xf7,
  3147. // ÷
  3148. 0x7d: 0xd1,
  3149. // Ñ
  3150. 0x7e: 0xf1,
  3151. // ñ
  3152. 0x7f: 0x2588,
  3153. // █
  3154. 0x0130: 0xae,
  3155. // ®
  3156. 0x0131: 0xb0,
  3157. // °
  3158. 0x0132: 0xbd,
  3159. // ½
  3160. 0x0133: 0xbf,
  3161. // ¿
  3162. 0x0134: 0x2122,
  3163. // ™
  3164. 0x0135: 0xa2,
  3165. // ¢
  3166. 0x0136: 0xa3,
  3167. // £
  3168. 0x0137: 0x266a,
  3169. // ♪
  3170. 0x0138: 0xe0,
  3171. // à
  3172. 0x0139: 0xa0,
  3173. //
  3174. 0x013a: 0xe8,
  3175. // è
  3176. 0x013b: 0xe2,
  3177. // â
  3178. 0x013c: 0xea,
  3179. // ê
  3180. 0x013d: 0xee,
  3181. // î
  3182. 0x013e: 0xf4,
  3183. // ô
  3184. 0x013f: 0xfb,
  3185. // û
  3186. 0x0220: 0xc1,
  3187. // Á
  3188. 0x0221: 0xc9,
  3189. // É
  3190. 0x0222: 0xd3,
  3191. // Ó
  3192. 0x0223: 0xda,
  3193. // Ú
  3194. 0x0224: 0xdc,
  3195. // Ü
  3196. 0x0225: 0xfc,
  3197. // ü
  3198. 0x0226: 0x2018,
  3199. // ‘
  3200. 0x0227: 0xa1,
  3201. // ¡
  3202. 0x0228: 0x2a,
  3203. // *
  3204. 0x0229: 0x27,
  3205. // '
  3206. 0x022a: 0x2014,
  3207. // —
  3208. 0x022b: 0xa9,
  3209. // ©
  3210. 0x022c: 0x2120,
  3211. // ℠
  3212. 0x022d: 0x2022,
  3213. // •
  3214. 0x022e: 0x201c,
  3215. // “
  3216. 0x022f: 0x201d,
  3217. // ”
  3218. 0x0230: 0xc0,
  3219. // À
  3220. 0x0231: 0xc2,
  3221. // Â
  3222. 0x0232: 0xc7,
  3223. // Ç
  3224. 0x0233: 0xc8,
  3225. // È
  3226. 0x0234: 0xca,
  3227. // Ê
  3228. 0x0235: 0xcb,
  3229. // Ë
  3230. 0x0236: 0xeb,
  3231. // ë
  3232. 0x0237: 0xce,
  3233. // Î
  3234. 0x0238: 0xcf,
  3235. // Ï
  3236. 0x0239: 0xef,
  3237. // ï
  3238. 0x023a: 0xd4,
  3239. // Ô
  3240. 0x023b: 0xd9,
  3241. // Ù
  3242. 0x023c: 0xf9,
  3243. // ù
  3244. 0x023d: 0xdb,
  3245. // Û
  3246. 0x023e: 0xab,
  3247. // «
  3248. 0x023f: 0xbb,
  3249. // »
  3250. 0x0320: 0xc3,
  3251. // Ã
  3252. 0x0321: 0xe3,
  3253. // ã
  3254. 0x0322: 0xcd,
  3255. // Í
  3256. 0x0323: 0xcc,
  3257. // Ì
  3258. 0x0324: 0xec,
  3259. // ì
  3260. 0x0325: 0xd2,
  3261. // Ò
  3262. 0x0326: 0xf2,
  3263. // ò
  3264. 0x0327: 0xd5,
  3265. // Õ
  3266. 0x0328: 0xf5,
  3267. // õ
  3268. 0x0329: 0x7b,
  3269. // {
  3270. 0x032a: 0x7d,
  3271. // }
  3272. 0x032b: 0x5c,
  3273. // \
  3274. 0x032c: 0x5e,
  3275. // ^
  3276. 0x032d: 0x5f,
  3277. // _
  3278. 0x032e: 0x7c,
  3279. // |
  3280. 0x032f: 0x7e,
  3281. // ~
  3282. 0x0330: 0xc4,
  3283. // Ä
  3284. 0x0331: 0xe4,
  3285. // ä
  3286. 0x0332: 0xd6,
  3287. // Ö
  3288. 0x0333: 0xf6,
  3289. // ö
  3290. 0x0334: 0xdf,
  3291. // ß
  3292. 0x0335: 0xa5,
  3293. // ¥
  3294. 0x0336: 0xa4,
  3295. // ¤
  3296. 0x0337: 0x2502,
  3297. // │
  3298. 0x0338: 0xc5,
  3299. // Å
  3300. 0x0339: 0xe5,
  3301. // å
  3302. 0x033a: 0xd8,
  3303. // Ø
  3304. 0x033b: 0xf8,
  3305. // ø
  3306. 0x033c: 0x250c,
  3307. // ┌
  3308. 0x033d: 0x2510,
  3309. // ┐
  3310. 0x033e: 0x2514,
  3311. // └
  3312. 0x033f: 0x2518 // ┘
  3313. };
  3314. var getCharFromCode = function getCharFromCode(code) {
  3315. if (code === null) {
  3316. return '';
  3317. }
  3318. code = CHARACTER_TRANSLATION[code] || code;
  3319. return String.fromCharCode(code);
  3320. }; // the index of the last row in a CEA-608 display buffer
  3321. var BOTTOM_ROW = 14; // This array is used for mapping PACs -> row #, since there's no way of
  3322. // getting it through bit logic.
  3323. 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
  3324. // cells. The "bottom" row is the last element in the outer array.
  3325. // We keep track of positioning information as we go by storing the
  3326. // number of indentations and the tab offset in this buffer.
  3327. var createDisplayBuffer = function createDisplayBuffer() {
  3328. var result = [],
  3329. i = BOTTOM_ROW + 1;
  3330. while (i--) {
  3331. result.push({
  3332. text: '',
  3333. indent: 0,
  3334. offset: 0
  3335. });
  3336. }
  3337. return result;
  3338. };
  3339. var Cea608Stream = function Cea608Stream(field, dataChannel) {
  3340. Cea608Stream.prototype.init.call(this);
  3341. this.field_ = field || 0;
  3342. this.dataChannel_ = dataChannel || 0;
  3343. this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
  3344. this.setConstants();
  3345. this.reset();
  3346. this.push = function (packet) {
  3347. var data, swap, char0, char1, text; // remove the parity bits
  3348. data = packet.ccData & 0x7f7f; // ignore duplicate control codes; the spec demands they're sent twice
  3349. if (data === this.lastControlCode_) {
  3350. this.lastControlCode_ = null;
  3351. return;
  3352. } // Store control codes
  3353. if ((data & 0xf000) === 0x1000) {
  3354. this.lastControlCode_ = data;
  3355. } else if (data !== this.PADDING_) {
  3356. this.lastControlCode_ = null;
  3357. }
  3358. char0 = data >>> 8;
  3359. char1 = data & 0xff;
  3360. if (data === this.PADDING_) {
  3361. return;
  3362. } else if (data === this.RESUME_CAPTION_LOADING_) {
  3363. this.mode_ = 'popOn';
  3364. } else if (data === this.END_OF_CAPTION_) {
  3365. // If an EOC is received while in paint-on mode, the displayed caption
  3366. // text should be swapped to non-displayed memory as if it was a pop-on
  3367. // caption. Because of that, we should explicitly switch back to pop-on
  3368. // mode
  3369. this.mode_ = 'popOn';
  3370. this.clearFormatting(packet.pts); // if a caption was being displayed, it's gone now
  3371. this.flushDisplayed(packet.pts); // flip memory
  3372. swap = this.displayed_;
  3373. this.displayed_ = this.nonDisplayed_;
  3374. this.nonDisplayed_ = swap; // start measuring the time to display the caption
  3375. this.startPts_ = packet.pts;
  3376. } else if (data === this.ROLL_UP_2_ROWS_) {
  3377. this.rollUpRows_ = 2;
  3378. this.setRollUp(packet.pts);
  3379. } else if (data === this.ROLL_UP_3_ROWS_) {
  3380. this.rollUpRows_ = 3;
  3381. this.setRollUp(packet.pts);
  3382. } else if (data === this.ROLL_UP_4_ROWS_) {
  3383. this.rollUpRows_ = 4;
  3384. this.setRollUp(packet.pts);
  3385. } else if (data === this.CARRIAGE_RETURN_) {
  3386. this.clearFormatting(packet.pts);
  3387. this.flushDisplayed(packet.pts);
  3388. this.shiftRowsUp_();
  3389. this.startPts_ = packet.pts;
  3390. } else if (data === this.BACKSPACE_) {
  3391. if (this.mode_ === 'popOn') {
  3392. this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
  3393. } else {
  3394. this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
  3395. }
  3396. } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
  3397. this.flushDisplayed(packet.pts);
  3398. this.displayed_ = createDisplayBuffer();
  3399. } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
  3400. this.nonDisplayed_ = createDisplayBuffer();
  3401. } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
  3402. if (this.mode_ !== 'paintOn') {
  3403. // NOTE: This should be removed when proper caption positioning is
  3404. // implemented
  3405. this.flushDisplayed(packet.pts);
  3406. this.displayed_ = createDisplayBuffer();
  3407. }
  3408. this.mode_ = 'paintOn';
  3409. this.startPts_ = packet.pts; // Append special characters to caption text
  3410. } else if (this.isSpecialCharacter(char0, char1)) {
  3411. // Bitmask char0 so that we can apply character transformations
  3412. // regardless of field and data channel.
  3413. // Then byte-shift to the left and OR with char1 so we can pass the
  3414. // entire character code to `getCharFromCode`.
  3415. char0 = (char0 & 0x03) << 8;
  3416. text = getCharFromCode(char0 | char1);
  3417. this[this.mode_](packet.pts, text);
  3418. this.column_++; // Append extended characters to caption text
  3419. } else if (this.isExtCharacter(char0, char1)) {
  3420. // Extended characters always follow their "non-extended" equivalents.
  3421. // IE if a "è" is desired, you'll always receive "eè"; non-compliant
  3422. // decoders are supposed to drop the "è", while compliant decoders
  3423. // backspace the "e" and insert "è".
  3424. // Delete the previous character
  3425. if (this.mode_ === 'popOn') {
  3426. this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
  3427. } else {
  3428. this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
  3429. } // Bitmask char0 so that we can apply character transformations
  3430. // regardless of field and data channel.
  3431. // Then byte-shift to the left and OR with char1 so we can pass the
  3432. // entire character code to `getCharFromCode`.
  3433. char0 = (char0 & 0x03) << 8;
  3434. text = getCharFromCode(char0 | char1);
  3435. this[this.mode_](packet.pts, text);
  3436. this.column_++; // Process mid-row codes
  3437. } else if (this.isMidRowCode(char0, char1)) {
  3438. // Attributes are not additive, so clear all formatting
  3439. this.clearFormatting(packet.pts); // According to the standard, mid-row codes
  3440. // should be replaced with spaces, so add one now
  3441. this[this.mode_](packet.pts, ' ');
  3442. this.column_++;
  3443. if ((char1 & 0xe) === 0xe) {
  3444. this.addFormatting(packet.pts, ['i']);
  3445. }
  3446. if ((char1 & 0x1) === 0x1) {
  3447. this.addFormatting(packet.pts, ['u']);
  3448. } // Detect offset control codes and adjust cursor
  3449. } else if (this.isOffsetControlCode(char0, char1)) {
  3450. // Cursor position is set by indent PAC (see below) in 4-column
  3451. // increments, with an additional offset code of 1-3 to reach any
  3452. // of the 32 columns specified by CEA-608. So all we need to do
  3453. // here is increment the column cursor by the given offset.
  3454. var offset = char1 & 0x03; // For an offest value 1-3, set the offset for that caption
  3455. // in the non-displayed array.
  3456. this.nonDisplayed_[this.row_].offset = offset;
  3457. this.column_ += offset; // Detect PACs (Preamble Address Codes)
  3458. } else if (this.isPAC(char0, char1)) {
  3459. // There's no logic for PAC -> row mapping, so we have to just
  3460. // find the row code in an array and use its index :(
  3461. var row = ROWS.indexOf(data & 0x1f20); // Configure the caption window if we're in roll-up mode
  3462. if (this.mode_ === 'rollUp') {
  3463. // This implies that the base row is incorrectly set.
  3464. // As per the recommendation in CEA-608(Base Row Implementation), defer to the number
  3465. // of roll-up rows set.
  3466. if (row - this.rollUpRows_ + 1 < 0) {
  3467. row = this.rollUpRows_ - 1;
  3468. }
  3469. this.setRollUp(packet.pts, row);
  3470. }
  3471. if (row !== this.row_) {
  3472. // formatting is only persistent for current row
  3473. this.clearFormatting(packet.pts);
  3474. this.row_ = row;
  3475. } // All PACs can apply underline, so detect and apply
  3476. // (All odd-numbered second bytes set underline)
  3477. if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
  3478. this.addFormatting(packet.pts, ['u']);
  3479. }
  3480. if ((data & 0x10) === 0x10) {
  3481. // We've got an indent level code. Each successive even number
  3482. // increments the column cursor by 4, so we can get the desired
  3483. // column position by bit-shifting to the right (to get n/2)
  3484. // and multiplying by 4.
  3485. var indentations = (data & 0xe) >> 1;
  3486. this.column_ = indentations * 4; // add to the number of indentations for positioning
  3487. this.nonDisplayed_[this.row_].indent += indentations;
  3488. }
  3489. if (this.isColorPAC(char1)) {
  3490. // it's a color code, though we only support white, which
  3491. // can be either normal or italicized. white italics can be
  3492. // either 0x4e or 0x6e depending on the row, so we just
  3493. // bitwise-and with 0xe to see if italics should be turned on
  3494. if ((char1 & 0xe) === 0xe) {
  3495. this.addFormatting(packet.pts, ['i']);
  3496. }
  3497. } // We have a normal character in char0, and possibly one in char1
  3498. } else if (this.isNormalChar(char0)) {
  3499. if (char1 === 0x00) {
  3500. char1 = null;
  3501. }
  3502. text = getCharFromCode(char0);
  3503. text += getCharFromCode(char1);
  3504. this[this.mode_](packet.pts, text);
  3505. this.column_ += text.length;
  3506. } // finish data processing
  3507. };
  3508. };
  3509. Cea608Stream.prototype = new stream(); // Trigger a cue point that captures the current state of the
  3510. // display buffer
  3511. Cea608Stream.prototype.flushDisplayed = function (pts) {
  3512. var _this = this;
  3513. var logWarning = function logWarning(index) {
  3514. _this.trigger('log', {
  3515. level: 'warn',
  3516. message: 'Skipping a malformed 608 caption at index ' + index + '.'
  3517. });
  3518. };
  3519. var content = [];
  3520. this.displayed_.forEach(function (row, i) {
  3521. if (row && row.text && row.text.length) {
  3522. try {
  3523. // remove spaces from the start and end of the string
  3524. row.text = row.text.trim();
  3525. } catch (e) {
  3526. // Ordinarily, this shouldn't happen. However, caption
  3527. // parsing errors should not throw exceptions and
  3528. // break playback.
  3529. logWarning(i);
  3530. } // See the below link for more details on the following fields:
  3531. // https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/608toVTT.html#positioning-in-cea-608
  3532. if (row.text.length) {
  3533. content.push({
  3534. // The text to be displayed in the caption from this specific row, with whitespace removed.
  3535. text: row.text,
  3536. // Value between 1 and 15 representing the PAC row used to calculate line height.
  3537. line: i + 1,
  3538. // A number representing the indent position by percentage (CEA-608 PAC indent code).
  3539. // The value will be a number between 10 and 80. Offset is used to add an aditional
  3540. // value to the position if necessary.
  3541. position: 10 + Math.min(70, row.indent * 10) + row.offset * 2.5
  3542. });
  3543. }
  3544. } else if (row === undefined || row === null) {
  3545. logWarning(i);
  3546. }
  3547. });
  3548. if (content.length) {
  3549. this.trigger('data', {
  3550. startPts: this.startPts_,
  3551. endPts: pts,
  3552. content: content,
  3553. stream: this.name_
  3554. });
  3555. }
  3556. };
  3557. /**
  3558. * Zero out the data, used for startup and on seek
  3559. */
  3560. Cea608Stream.prototype.reset = function () {
  3561. this.mode_ = 'popOn'; // When in roll-up mode, the index of the last row that will
  3562. // actually display captions. If a caption is shifted to a row
  3563. // with a lower index than this, it is cleared from the display
  3564. // buffer
  3565. this.topRow_ = 0;
  3566. this.startPts_ = 0;
  3567. this.displayed_ = createDisplayBuffer();
  3568. this.nonDisplayed_ = createDisplayBuffer();
  3569. this.lastControlCode_ = null; // Track row and column for proper line-breaking and spacing
  3570. this.column_ = 0;
  3571. this.row_ = BOTTOM_ROW;
  3572. this.rollUpRows_ = 2; // This variable holds currently-applied formatting
  3573. this.formatting_ = [];
  3574. };
  3575. /**
  3576. * Sets up control code and related constants for this instance
  3577. */
  3578. Cea608Stream.prototype.setConstants = function () {
  3579. // The following attributes have these uses:
  3580. // ext_ : char0 for mid-row codes, and the base for extended
  3581. // chars (ext_+0, ext_+1, and ext_+2 are char0s for
  3582. // extended codes)
  3583. // control_: char0 for control codes, except byte-shifted to the
  3584. // left so that we can do this.control_ | CONTROL_CODE
  3585. // offset_: char0 for tab offset codes
  3586. //
  3587. // It's also worth noting that control codes, and _only_ control codes,
  3588. // differ between field 1 and field2. Field 2 control codes are always
  3589. // their field 1 value plus 1. That's why there's the "| field" on the
  3590. // control value.
  3591. if (this.dataChannel_ === 0) {
  3592. this.BASE_ = 0x10;
  3593. this.EXT_ = 0x11;
  3594. this.CONTROL_ = (0x14 | this.field_) << 8;
  3595. this.OFFSET_ = 0x17;
  3596. } else if (this.dataChannel_ === 1) {
  3597. this.BASE_ = 0x18;
  3598. this.EXT_ = 0x19;
  3599. this.CONTROL_ = (0x1c | this.field_) << 8;
  3600. this.OFFSET_ = 0x1f;
  3601. } // Constants for the LSByte command codes recognized by Cea608Stream. This
  3602. // list is not exhaustive. For a more comprehensive listing and semantics see
  3603. // http://www.gpo.gov/fdsys/pkg/CFR-2010-title47-vol1/pdf/CFR-2010-title47-vol1-sec15-119.pdf
  3604. // Padding
  3605. this.PADDING_ = 0x0000; // Pop-on Mode
  3606. this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
  3607. this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f; // Roll-up Mode
  3608. this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
  3609. this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
  3610. this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
  3611. this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d; // paint-on mode
  3612. this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29; // Erasure
  3613. this.BACKSPACE_ = this.CONTROL_ | 0x21;
  3614. this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
  3615. this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
  3616. };
  3617. /**
  3618. * Detects if the 2-byte packet data is a special character
  3619. *
  3620. * Special characters have a second byte in the range 0x30 to 0x3f,
  3621. * with the first byte being 0x11 (for data channel 1) or 0x19 (for
  3622. * data channel 2).
  3623. *
  3624. * @param {Integer} char0 The first byte
  3625. * @param {Integer} char1 The second byte
  3626. * @return {Boolean} Whether the 2 bytes are an special character
  3627. */
  3628. Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
  3629. return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
  3630. };
  3631. /**
  3632. * Detects if the 2-byte packet data is an extended character
  3633. *
  3634. * Extended characters have a second byte in the range 0x20 to 0x3f,
  3635. * with the first byte being 0x12 or 0x13 (for data channel 1) or
  3636. * 0x1a or 0x1b (for data channel 2).
  3637. *
  3638. * @param {Integer} char0 The first byte
  3639. * @param {Integer} char1 The second byte
  3640. * @return {Boolean} Whether the 2 bytes are an extended character
  3641. */
  3642. Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
  3643. return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
  3644. };
  3645. /**
  3646. * Detects if the 2-byte packet is a mid-row code
  3647. *
  3648. * Mid-row codes have a second byte in the range 0x20 to 0x2f, with
  3649. * the first byte being 0x11 (for data channel 1) or 0x19 (for data
  3650. * channel 2).
  3651. *
  3652. * @param {Integer} char0 The first byte
  3653. * @param {Integer} char1 The second byte
  3654. * @return {Boolean} Whether the 2 bytes are a mid-row code
  3655. */
  3656. Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
  3657. return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
  3658. };
  3659. /**
  3660. * Detects if the 2-byte packet is an offset control code
  3661. *
  3662. * Offset control codes have a second byte in the range 0x21 to 0x23,
  3663. * with the first byte being 0x17 (for data channel 1) or 0x1f (for
  3664. * data channel 2).
  3665. *
  3666. * @param {Integer} char0 The first byte
  3667. * @param {Integer} char1 The second byte
  3668. * @return {Boolean} Whether the 2 bytes are an offset control code
  3669. */
  3670. Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
  3671. return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
  3672. };
  3673. /**
  3674. * Detects if the 2-byte packet is a Preamble Address Code
  3675. *
  3676. * PACs have a first byte in the range 0x10 to 0x17 (for data channel 1)
  3677. * or 0x18 to 0x1f (for data channel 2), with the second byte in the
  3678. * range 0x40 to 0x7f.
  3679. *
  3680. * @param {Integer} char0 The first byte
  3681. * @param {Integer} char1 The second byte
  3682. * @return {Boolean} Whether the 2 bytes are a PAC
  3683. */
  3684. Cea608Stream.prototype.isPAC = function (char0, char1) {
  3685. return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
  3686. };
  3687. /**
  3688. * Detects if a packet's second byte is in the range of a PAC color code
  3689. *
  3690. * PAC color codes have the second byte be in the range 0x40 to 0x4f, or
  3691. * 0x60 to 0x6f.
  3692. *
  3693. * @param {Integer} char1 The second byte
  3694. * @return {Boolean} Whether the byte is a color PAC
  3695. */
  3696. Cea608Stream.prototype.isColorPAC = function (char1) {
  3697. return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
  3698. };
  3699. /**
  3700. * Detects if a single byte is in the range of a normal character
  3701. *
  3702. * Normal text bytes are in the range 0x20 to 0x7f.
  3703. *
  3704. * @param {Integer} char The byte
  3705. * @return {Boolean} Whether the byte is a normal character
  3706. */
  3707. Cea608Stream.prototype.isNormalChar = function (char) {
  3708. return char >= 0x20 && char <= 0x7f;
  3709. };
  3710. /**
  3711. * Configures roll-up
  3712. *
  3713. * @param {Integer} pts Current PTS
  3714. * @param {Integer} newBaseRow Used by PACs to slide the current window to
  3715. * a new position
  3716. */
  3717. Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
  3718. // Reset the base row to the bottom row when switching modes
  3719. if (this.mode_ !== 'rollUp') {
  3720. this.row_ = BOTTOM_ROW;
  3721. this.mode_ = 'rollUp'; // Spec says to wipe memories when switching to roll-up
  3722. this.flushDisplayed(pts);
  3723. this.nonDisplayed_ = createDisplayBuffer();
  3724. this.displayed_ = createDisplayBuffer();
  3725. }
  3726. if (newBaseRow !== undefined && newBaseRow !== this.row_) {
  3727. // move currently displayed captions (up or down) to the new base row
  3728. for (var i = 0; i < this.rollUpRows_; i++) {
  3729. this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
  3730. this.displayed_[this.row_ - i] = {
  3731. text: '',
  3732. indent: 0,
  3733. offset: 0
  3734. };
  3735. }
  3736. }
  3737. if (newBaseRow === undefined) {
  3738. newBaseRow = this.row_;
  3739. }
  3740. this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
  3741. }; // Adds the opening HTML tag for the passed character to the caption text,
  3742. // and keeps track of it for later closing
  3743. Cea608Stream.prototype.addFormatting = function (pts, format) {
  3744. this.formatting_ = this.formatting_.concat(format);
  3745. var text = format.reduce(function (text, format) {
  3746. return text + '<' + format + '>';
  3747. }, '');
  3748. this[this.mode_](pts, text);
  3749. }; // Adds HTML closing tags for current formatting to caption text and
  3750. // clears remembered formatting
  3751. Cea608Stream.prototype.clearFormatting = function (pts) {
  3752. if (!this.formatting_.length) {
  3753. return;
  3754. }
  3755. var text = this.formatting_.reverse().reduce(function (text, format) {
  3756. return text + '</' + format + '>';
  3757. }, '');
  3758. this.formatting_ = [];
  3759. this[this.mode_](pts, text);
  3760. }; // Mode Implementations
  3761. Cea608Stream.prototype.popOn = function (pts, text) {
  3762. var baseRow = this.nonDisplayed_[this.row_].text; // buffer characters
  3763. baseRow += text;
  3764. this.nonDisplayed_[this.row_].text = baseRow;
  3765. };
  3766. Cea608Stream.prototype.rollUp = function (pts, text) {
  3767. var baseRow = this.displayed_[this.row_].text;
  3768. baseRow += text;
  3769. this.displayed_[this.row_].text = baseRow;
  3770. };
  3771. Cea608Stream.prototype.shiftRowsUp_ = function () {
  3772. var i; // clear out inactive rows
  3773. for (i = 0; i < this.topRow_; i++) {
  3774. this.displayed_[i] = {
  3775. text: '',
  3776. indent: 0,
  3777. offset: 0
  3778. };
  3779. }
  3780. for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
  3781. this.displayed_[i] = {
  3782. text: '',
  3783. indent: 0,
  3784. offset: 0
  3785. };
  3786. } // shift displayed rows up
  3787. for (i = this.topRow_; i < this.row_; i++) {
  3788. this.displayed_[i] = this.displayed_[i + 1];
  3789. } // clear out the bottom row
  3790. this.displayed_[this.row_] = {
  3791. text: '',
  3792. indent: 0,
  3793. offset: 0
  3794. };
  3795. };
  3796. Cea608Stream.prototype.paintOn = function (pts, text) {
  3797. var baseRow = this.displayed_[this.row_].text;
  3798. baseRow += text;
  3799. this.displayed_[this.row_].text = baseRow;
  3800. }; // exports
  3801. var captionStream = {
  3802. CaptionStream: CaptionStream$1,
  3803. Cea608Stream: Cea608Stream,
  3804. Cea708Stream: Cea708Stream
  3805. };
  3806. /**
  3807. * mux.js
  3808. *
  3809. * Copyright (c) Brightcove
  3810. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  3811. */
  3812. var streamTypes = {
  3813. H264_STREAM_TYPE: 0x1B,
  3814. ADTS_STREAM_TYPE: 0x0F,
  3815. METADATA_STREAM_TYPE: 0x15
  3816. };
  3817. var MAX_TS = 8589934592;
  3818. var RO_THRESH = 4294967296;
  3819. var TYPE_SHARED = 'shared';
  3820. var handleRollover = function handleRollover(value, reference) {
  3821. var direction = 1;
  3822. if (value > reference) {
  3823. // If the current timestamp value is greater than our reference timestamp and we detect a
  3824. // timestamp rollover, this means the roll over is happening in the opposite direction.
  3825. // Example scenario: Enter a long stream/video just after a rollover occurred. The reference
  3826. // point will be set to a small number, e.g. 1. The user then seeks backwards over the
  3827. // rollover point. In loading this segment, the timestamp values will be very large,
  3828. // e.g. 2^33 - 1. Since this comes before the data we loaded previously, we want to adjust
  3829. // the time stamp to be `value - 2^33`.
  3830. direction = -1;
  3831. } // Note: A seek forwards or back that is greater than the RO_THRESH (2^32, ~13 hours) will
  3832. // cause an incorrect adjustment.
  3833. while (Math.abs(reference - value) > RO_THRESH) {
  3834. value += direction * MAX_TS;
  3835. }
  3836. return value;
  3837. };
  3838. var TimestampRolloverStream$1 = function TimestampRolloverStream(type) {
  3839. var lastDTS, referenceDTS;
  3840. TimestampRolloverStream.prototype.init.call(this); // The "shared" type is used in cases where a stream will contain muxed
  3841. // video and audio. We could use `undefined` here, but having a string
  3842. // makes debugging a little clearer.
  3843. this.type_ = type || TYPE_SHARED;
  3844. this.push = function (data) {
  3845. /**
  3846. * Rollover stream expects data from elementary stream.
  3847. * Elementary stream can push forward 2 types of data
  3848. * - Parsed Video/Audio/Timed-metadata PES (packetized elementary stream) packets
  3849. * - Tracks metadata from PMT (Program Map Table)
  3850. * Rollover stream expects pts/dts info to be available, since it stores lastDTS
  3851. * We should ignore non-PES packets since they may override lastDTS to undefined.
  3852. * lastDTS is important to signal the next segments
  3853. * about rollover from the previous segments.
  3854. */
  3855. if (data.type === 'metadata') {
  3856. this.trigger('data', data);
  3857. return;
  3858. } // Any "shared" rollover streams will accept _all_ data. Otherwise,
  3859. // streams will only accept data that matches their type.
  3860. if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
  3861. return;
  3862. }
  3863. if (referenceDTS === undefined) {
  3864. referenceDTS = data.dts;
  3865. }
  3866. data.dts = handleRollover(data.dts, referenceDTS);
  3867. data.pts = handleRollover(data.pts, referenceDTS);
  3868. lastDTS = data.dts;
  3869. this.trigger('data', data);
  3870. };
  3871. this.flush = function () {
  3872. referenceDTS = lastDTS;
  3873. this.trigger('done');
  3874. };
  3875. this.endTimeline = function () {
  3876. this.flush();
  3877. this.trigger('endedtimeline');
  3878. };
  3879. this.discontinuity = function () {
  3880. referenceDTS = void 0;
  3881. lastDTS = void 0;
  3882. };
  3883. this.reset = function () {
  3884. this.discontinuity();
  3885. this.trigger('reset');
  3886. };
  3887. };
  3888. TimestampRolloverStream$1.prototype = new stream();
  3889. var timestampRolloverStream = {
  3890. TimestampRolloverStream: TimestampRolloverStream$1,
  3891. handleRollover: handleRollover
  3892. };
  3893. var _MetadataStream;
  3894. _MetadataStream = function MetadataStream(options) {
  3895. var settings = {
  3896. // the bytes of the program-level descriptor field in MP2T
  3897. // see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
  3898. // program element descriptors"
  3899. descriptor: options && options.descriptor
  3900. },
  3901. // the total size in bytes of the ID3 tag being parsed
  3902. tagSize = 0,
  3903. // tag data that is not complete enough to be parsed
  3904. buffer = [],
  3905. // the total number of bytes currently in the buffer
  3906. bufferSize = 0,
  3907. i;
  3908. _MetadataStream.prototype.init.call(this); // calculate the text track in-band metadata track dispatch type
  3909. // https://html.spec.whatwg.org/multipage/embedded-content.html#steps-to-expose-a-media-resource-specific-text-track
  3910. this.dispatchType = streamTypes.METADATA_STREAM_TYPE.toString(16);
  3911. if (settings.descriptor) {
  3912. for (i = 0; i < settings.descriptor.length; i++) {
  3913. this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
  3914. }
  3915. }
  3916. this.push = function (chunk) {
  3917. var tag, frameStart, frameSize, frame, i, frameHeader;
  3918. if (chunk.type !== 'timed-metadata') {
  3919. return;
  3920. } // if data_alignment_indicator is set in the PES header,
  3921. // we must have the start of a new ID3 tag. Assume anything
  3922. // remaining in the buffer was malformed and throw it out
  3923. if (chunk.dataAlignmentIndicator) {
  3924. bufferSize = 0;
  3925. buffer.length = 0;
  3926. } // ignore events that don't look like ID3 data
  3927. 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))) {
  3928. this.trigger('log', {
  3929. level: 'warn',
  3930. message: 'Skipping unrecognized metadata packet'
  3931. });
  3932. return;
  3933. } // add this chunk to the data we've collected so far
  3934. buffer.push(chunk);
  3935. bufferSize += chunk.data.byteLength; // grab the size of the entire frame from the ID3 header
  3936. if (buffer.length === 1) {
  3937. // the frame size is transmitted as a 28-bit integer in the
  3938. // last four bytes of the ID3 header.
  3939. // The most significant bit of each byte is dropped and the
  3940. // results concatenated to recover the actual value.
  3941. tagSize = parseId3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  3942. // convenient for our comparisons to include it
  3943. tagSize += 10;
  3944. } // if the entire frame has not arrived, wait for more data
  3945. if (bufferSize < tagSize) {
  3946. return;
  3947. } // collect the entire frame so it can be parsed
  3948. tag = {
  3949. data: new Uint8Array(tagSize),
  3950. frames: [],
  3951. pts: buffer[0].pts,
  3952. dts: buffer[0].dts
  3953. };
  3954. for (i = 0; i < tagSize;) {
  3955. tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
  3956. i += buffer[0].data.byteLength;
  3957. bufferSize -= buffer[0].data.byteLength;
  3958. buffer.shift();
  3959. } // find the start of the first frame and the end of the tag
  3960. frameStart = 10;
  3961. if (tag.data[5] & 0x40) {
  3962. // advance the frame start past the extended header
  3963. frameStart += 4; // header size field
  3964. frameStart += parseId3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end
  3965. tagSize -= parseId3.parseSyncSafeInteger(tag.data.subarray(16, 20));
  3966. } // parse one or more ID3 frames
  3967. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  3968. do {
  3969. // determine the number of bytes in this frame
  3970. frameSize = parseId3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
  3971. if (frameSize < 1) {
  3972. this.trigger('log', {
  3973. level: 'warn',
  3974. message: 'Malformed ID3 frame encountered. Skipping remaining metadata parsing.'
  3975. }); // If the frame is malformed, don't parse any further frames but allow previous valid parsed frames
  3976. // to be sent along.
  3977. break;
  3978. }
  3979. frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
  3980. frame = {
  3981. id: frameHeader,
  3982. data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
  3983. };
  3984. frame.key = frame.id; // parse frame values
  3985. if (parseId3.frameParsers[frame.id]) {
  3986. // use frame specific parser
  3987. parseId3.frameParsers[frame.id](frame);
  3988. } else if (frame.id[0] === 'T') {
  3989. // use text frame generic parser
  3990. parseId3.frameParsers['T*'](frame);
  3991. } else if (frame.id[0] === 'W') {
  3992. // use URL link frame generic parser
  3993. parseId3.frameParsers['W*'](frame);
  3994. } // handle the special PRIV frame used to indicate the start
  3995. // time for raw AAC data
  3996. if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
  3997. var d = frame.data,
  3998. size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  3999. size *= 4;
  4000. size += d[7] & 0x03;
  4001. frame.timeStamp = size; // in raw AAC, all subsequent data will be timestamped based
  4002. // on the value of this frame
  4003. // we couldn't have known the appropriate pts and dts before
  4004. // parsing this ID3 tag so set those values now
  4005. if (tag.pts === undefined && tag.dts === undefined) {
  4006. tag.pts = frame.timeStamp;
  4007. tag.dts = frame.timeStamp;
  4008. }
  4009. this.trigger('timestamp', frame);
  4010. }
  4011. tag.frames.push(frame);
  4012. frameStart += 10; // advance past the frame header
  4013. frameStart += frameSize; // advance past the frame body
  4014. } while (frameStart < tagSize);
  4015. this.trigger('data', tag);
  4016. };
  4017. };
  4018. _MetadataStream.prototype = new stream();
  4019. var metadataStream = _MetadataStream;
  4020. var TimestampRolloverStream = timestampRolloverStream.TimestampRolloverStream; // object types
  4021. var _TransportPacketStream, _TransportParseStream, _ElementaryStream; // constants
  4022. var MP2T_PACKET_LENGTH = 188,
  4023. // bytes
  4024. SYNC_BYTE = 0x47;
  4025. /**
  4026. * Splits an incoming stream of binary data into MPEG-2 Transport
  4027. * Stream packets.
  4028. */
  4029. _TransportPacketStream = function TransportPacketStream() {
  4030. var buffer = new Uint8Array(MP2T_PACKET_LENGTH),
  4031. bytesInBuffer = 0;
  4032. _TransportPacketStream.prototype.init.call(this); // Deliver new bytes to the stream.
  4033. /**
  4034. * Split a stream of data into M2TS packets
  4035. **/
  4036. this.push = function (bytes) {
  4037. var startIndex = 0,
  4038. endIndex = MP2T_PACKET_LENGTH,
  4039. everything; // If there are bytes remaining from the last segment, prepend them to the
  4040. // bytes that were pushed in
  4041. if (bytesInBuffer) {
  4042. everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
  4043. everything.set(buffer.subarray(0, bytesInBuffer));
  4044. everything.set(bytes, bytesInBuffer);
  4045. bytesInBuffer = 0;
  4046. } else {
  4047. everything = bytes;
  4048. } // While we have enough data for a packet
  4049. while (endIndex < everything.byteLength) {
  4050. // Look for a pair of start and end sync bytes in the data..
  4051. if (everything[startIndex] === SYNC_BYTE && everything[endIndex] === SYNC_BYTE) {
  4052. // We found a packet so emit it and jump one whole packet forward in
  4053. // the stream
  4054. this.trigger('data', everything.subarray(startIndex, endIndex));
  4055. startIndex += MP2T_PACKET_LENGTH;
  4056. endIndex += MP2T_PACKET_LENGTH;
  4057. continue;
  4058. } // If we get here, we have somehow become de-synchronized and we need to step
  4059. // forward one byte at a time until we find a pair of sync bytes that denote
  4060. // a packet
  4061. startIndex++;
  4062. endIndex++;
  4063. } // If there was some data left over at the end of the segment that couldn't
  4064. // possibly be a whole packet, keep it because it might be the start of a packet
  4065. // that continues in the next segment
  4066. if (startIndex < everything.byteLength) {
  4067. buffer.set(everything.subarray(startIndex), 0);
  4068. bytesInBuffer = everything.byteLength - startIndex;
  4069. }
  4070. };
  4071. /**
  4072. * Passes identified M2TS packets to the TransportParseStream to be parsed
  4073. **/
  4074. this.flush = function () {
  4075. // If the buffer contains a whole packet when we are being flushed, emit it
  4076. // and empty the buffer. Otherwise hold onto the data because it may be
  4077. // important for decoding the next segment
  4078. if (bytesInBuffer === MP2T_PACKET_LENGTH && buffer[0] === SYNC_BYTE) {
  4079. this.trigger('data', buffer);
  4080. bytesInBuffer = 0;
  4081. }
  4082. this.trigger('done');
  4083. };
  4084. this.endTimeline = function () {
  4085. this.flush();
  4086. this.trigger('endedtimeline');
  4087. };
  4088. this.reset = function () {
  4089. bytesInBuffer = 0;
  4090. this.trigger('reset');
  4091. };
  4092. };
  4093. _TransportPacketStream.prototype = new stream();
  4094. /**
  4095. * Accepts an MP2T TransportPacketStream and emits data events with parsed
  4096. * forms of the individual transport stream packets.
  4097. */
  4098. _TransportParseStream = function TransportParseStream() {
  4099. var parsePsi, parsePat, parsePmt, self;
  4100. _TransportParseStream.prototype.init.call(this);
  4101. self = this;
  4102. this.packetsWaitingForPmt = [];
  4103. this.programMapTable = undefined;
  4104. parsePsi = function parsePsi(payload, psi) {
  4105. var offset = 0; // PSI packets may be split into multiple sections and those
  4106. // sections may be split into multiple packets. If a PSI
  4107. // section starts in this packet, the payload_unit_start_indicator
  4108. // will be true and the first byte of the payload will indicate
  4109. // the offset from the current position to the start of the
  4110. // section.
  4111. if (psi.payloadUnitStartIndicator) {
  4112. offset += payload[offset] + 1;
  4113. }
  4114. if (psi.type === 'pat') {
  4115. parsePat(payload.subarray(offset), psi);
  4116. } else {
  4117. parsePmt(payload.subarray(offset), psi);
  4118. }
  4119. };
  4120. parsePat = function parsePat(payload, pat) {
  4121. pat.section_number = payload[7]; // eslint-disable-line camelcase
  4122. pat.last_section_number = payload[8]; // eslint-disable-line camelcase
  4123. // skip the PSI header and parse the first PMT entry
  4124. self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
  4125. pat.pmtPid = self.pmtPid;
  4126. };
  4127. /**
  4128. * Parse out the relevant fields of a Program Map Table (PMT).
  4129. * @param payload {Uint8Array} the PMT-specific portion of an MP2T
  4130. * packet. The first byte in this array should be the table_id
  4131. * field.
  4132. * @param pmt {object} the object that should be decorated with
  4133. * fields parsed from the PMT.
  4134. */
  4135. parsePmt = function parsePmt(payload, pmt) {
  4136. var sectionLength, tableEnd, programInfoLength, offset; // PMTs can be sent ahead of the time when they should actually
  4137. // take effect. We don't believe this should ever be the case
  4138. // for HLS but we'll ignore "forward" PMT declarations if we see
  4139. // them. Future PMT declarations have the current_next_indicator
  4140. // set to zero.
  4141. if (!(payload[5] & 0x01)) {
  4142. return;
  4143. } // overwrite any existing program map table
  4144. self.programMapTable = {
  4145. video: null,
  4146. audio: null,
  4147. 'timed-metadata': {}
  4148. }; // the mapping table ends at the end of the current section
  4149. sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
  4150. tableEnd = 3 + sectionLength - 4; // to determine where the table is, we have to figure out how
  4151. // long the program info descriptors are
  4152. programInfoLength = (payload[10] & 0x0f) << 8 | payload[11]; // advance the offset to the first entry in the mapping table
  4153. offset = 12 + programInfoLength;
  4154. while (offset < tableEnd) {
  4155. var streamType = payload[offset];
  4156. var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2]; // only map a single elementary_pid for audio and video stream types
  4157. // TODO: should this be done for metadata too? for now maintain behavior of
  4158. // multiple metadata streams
  4159. if (streamType === streamTypes.H264_STREAM_TYPE && self.programMapTable.video === null) {
  4160. self.programMapTable.video = pid;
  4161. } else if (streamType === streamTypes.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
  4162. self.programMapTable.audio = pid;
  4163. } else if (streamType === streamTypes.METADATA_STREAM_TYPE) {
  4164. // map pid to stream type for metadata streams
  4165. self.programMapTable['timed-metadata'][pid] = streamType;
  4166. } // move to the next table entry
  4167. // skip past the elementary stream descriptors, if present
  4168. offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
  4169. } // record the map on the packet as well
  4170. pmt.programMapTable = self.programMapTable;
  4171. };
  4172. /**
  4173. * Deliver a new MP2T packet to the next stream in the pipeline.
  4174. */
  4175. this.push = function (packet) {
  4176. var result = {},
  4177. offset = 4;
  4178. result.payloadUnitStartIndicator = !!(packet[1] & 0x40); // pid is a 13-bit field starting at the last bit of packet[1]
  4179. result.pid = packet[1] & 0x1f;
  4180. result.pid <<= 8;
  4181. result.pid |= packet[2]; // if an adaption field is present, its length is specified by the
  4182. // fifth byte of the TS packet header. The adaptation field is
  4183. // used to add stuffing to PES packets that don't fill a complete
  4184. // TS packet, and to specify some forms of timing and control data
  4185. // that we do not currently use.
  4186. if ((packet[3] & 0x30) >>> 4 > 0x01) {
  4187. offset += packet[offset] + 1;
  4188. } // parse the rest of the packet based on the type
  4189. if (result.pid === 0) {
  4190. result.type = 'pat';
  4191. parsePsi(packet.subarray(offset), result);
  4192. this.trigger('data', result);
  4193. } else if (result.pid === this.pmtPid) {
  4194. result.type = 'pmt';
  4195. parsePsi(packet.subarray(offset), result);
  4196. this.trigger('data', result); // if there are any packets waiting for a PMT to be found, process them now
  4197. while (this.packetsWaitingForPmt.length) {
  4198. this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
  4199. }
  4200. } else if (this.programMapTable === undefined) {
  4201. // When we have not seen a PMT yet, defer further processing of
  4202. // PES packets until one has been parsed
  4203. this.packetsWaitingForPmt.push([packet, offset, result]);
  4204. } else {
  4205. this.processPes_(packet, offset, result);
  4206. }
  4207. };
  4208. this.processPes_ = function (packet, offset, result) {
  4209. // set the appropriate stream type
  4210. if (result.pid === this.programMapTable.video) {
  4211. result.streamType = streamTypes.H264_STREAM_TYPE;
  4212. } else if (result.pid === this.programMapTable.audio) {
  4213. result.streamType = streamTypes.ADTS_STREAM_TYPE;
  4214. } else {
  4215. // if not video or audio, it is timed-metadata or unknown
  4216. // if unknown, streamType will be undefined
  4217. result.streamType = this.programMapTable['timed-metadata'][result.pid];
  4218. }
  4219. result.type = 'pes';
  4220. result.data = packet.subarray(offset);
  4221. this.trigger('data', result);
  4222. };
  4223. };
  4224. _TransportParseStream.prototype = new stream();
  4225. _TransportParseStream.STREAM_TYPES = {
  4226. h264: 0x1b,
  4227. adts: 0x0f
  4228. };
  4229. /**
  4230. * Reconsistutes program elementary stream (PES) packets from parsed
  4231. * transport stream packets. That is, if you pipe an
  4232. * mp2t.TransportParseStream into a mp2t.ElementaryStream, the output
  4233. * events will be events which capture the bytes for individual PES
  4234. * packets plus relevant metadata that has been extracted from the
  4235. * container.
  4236. */
  4237. _ElementaryStream = function ElementaryStream() {
  4238. var self = this,
  4239. segmentHadPmt = false,
  4240. // PES packet fragments
  4241. video = {
  4242. data: [],
  4243. size: 0
  4244. },
  4245. audio = {
  4246. data: [],
  4247. size: 0
  4248. },
  4249. timedMetadata = {
  4250. data: [],
  4251. size: 0
  4252. },
  4253. programMapTable,
  4254. parsePes = function parsePes(payload, pes) {
  4255. var ptsDtsFlags;
  4256. var startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]; // default to an empty array
  4257. pes.data = new Uint8Array(); // In certain live streams, the start of a TS fragment has ts packets
  4258. // that are frame data that is continuing from the previous fragment. This
  4259. // is to check that the pes data is the start of a new pes payload
  4260. if (startPrefix !== 1) {
  4261. return;
  4262. } // get the packet length, this will be 0 for video
  4263. pes.packetLength = 6 + (payload[4] << 8 | payload[5]); // find out if this packets starts a new keyframe
  4264. pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0; // PES packets may be annotated with a PTS value, or a PTS value
  4265. // and a DTS value. Determine what combination of values is
  4266. // available to work with.
  4267. ptsDtsFlags = payload[7]; // PTS and DTS are normally stored as a 33-bit number. Javascript
  4268. // performs all bitwise operations on 32-bit integers but javascript
  4269. // supports a much greater range (52-bits) of integer using standard
  4270. // mathematical operations.
  4271. // We construct a 31-bit value using bitwise operators over the 31
  4272. // most significant bits and then multiply by 4 (equal to a left-shift
  4273. // of 2) before we add the final 2 least significant bits of the
  4274. // timestamp (equal to an OR.)
  4275. if (ptsDtsFlags & 0xC0) {
  4276. // the PTS and DTS are not written out directly. For information
  4277. // on how they are encoded, see
  4278. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  4279. pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
  4280. pes.pts *= 4; // Left shift by 2
  4281. pes.pts += (payload[13] & 0x06) >>> 1; // OR by the two LSBs
  4282. pes.dts = pes.pts;
  4283. if (ptsDtsFlags & 0x40) {
  4284. pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
  4285. pes.dts *= 4; // Left shift by 2
  4286. pes.dts += (payload[18] & 0x06) >>> 1; // OR by the two LSBs
  4287. }
  4288. } // the data section starts immediately after the PES header.
  4289. // pes_header_data_length specifies the number of header bytes
  4290. // that follow the last byte of the field.
  4291. pes.data = payload.subarray(9 + payload[8]);
  4292. },
  4293. /**
  4294. * Pass completely parsed PES packets to the next stream in the pipeline
  4295. **/
  4296. flushStream = function flushStream(stream, type, forceFlush) {
  4297. var packetData = new Uint8Array(stream.size),
  4298. event = {
  4299. type: type
  4300. },
  4301. i = 0,
  4302. offset = 0,
  4303. packetFlushable = false,
  4304. fragment; // do nothing if there is not enough buffered data for a complete
  4305. // PES header
  4306. if (!stream.data.length || stream.size < 9) {
  4307. return;
  4308. }
  4309. event.trackId = stream.data[0].pid; // reassemble the packet
  4310. for (i = 0; i < stream.data.length; i++) {
  4311. fragment = stream.data[i];
  4312. packetData.set(fragment.data, offset);
  4313. offset += fragment.data.byteLength;
  4314. } // parse assembled packet's PES header
  4315. parsePes(packetData, event); // non-video PES packets MUST have a non-zero PES_packet_length
  4316. // check that there is enough stream data to fill the packet
  4317. packetFlushable = type === 'video' || event.packetLength <= stream.size; // flush pending packets if the conditions are right
  4318. if (forceFlush || packetFlushable) {
  4319. stream.size = 0;
  4320. stream.data.length = 0;
  4321. } // only emit packets that are complete. this is to avoid assembling
  4322. // incomplete PES packets due to poor segmentation
  4323. if (packetFlushable) {
  4324. self.trigger('data', event);
  4325. }
  4326. };
  4327. _ElementaryStream.prototype.init.call(this);
  4328. /**
  4329. * Identifies M2TS packet types and parses PES packets using metadata
  4330. * parsed from the PMT
  4331. **/
  4332. this.push = function (data) {
  4333. ({
  4334. pat: function pat() {// we have to wait for the PMT to arrive as well before we
  4335. // have any meaningful metadata
  4336. },
  4337. pes: function pes() {
  4338. var stream, streamType;
  4339. switch (data.streamType) {
  4340. case streamTypes.H264_STREAM_TYPE:
  4341. stream = video;
  4342. streamType = 'video';
  4343. break;
  4344. case streamTypes.ADTS_STREAM_TYPE:
  4345. stream = audio;
  4346. streamType = 'audio';
  4347. break;
  4348. case streamTypes.METADATA_STREAM_TYPE:
  4349. stream = timedMetadata;
  4350. streamType = 'timed-metadata';
  4351. break;
  4352. default:
  4353. // ignore unknown stream types
  4354. return;
  4355. } // if a new packet is starting, we can flush the completed
  4356. // packet
  4357. if (data.payloadUnitStartIndicator) {
  4358. flushStream(stream, streamType, true);
  4359. } // buffer this fragment until we are sure we've received the
  4360. // complete payload
  4361. stream.data.push(data);
  4362. stream.size += data.data.byteLength;
  4363. },
  4364. pmt: function pmt() {
  4365. var event = {
  4366. type: 'metadata',
  4367. tracks: []
  4368. };
  4369. programMapTable = data.programMapTable; // translate audio and video streams to tracks
  4370. if (programMapTable.video !== null) {
  4371. event.tracks.push({
  4372. timelineStartInfo: {
  4373. baseMediaDecodeTime: 0
  4374. },
  4375. id: +programMapTable.video,
  4376. codec: 'avc',
  4377. type: 'video'
  4378. });
  4379. }
  4380. if (programMapTable.audio !== null) {
  4381. event.tracks.push({
  4382. timelineStartInfo: {
  4383. baseMediaDecodeTime: 0
  4384. },
  4385. id: +programMapTable.audio,
  4386. codec: 'adts',
  4387. type: 'audio'
  4388. });
  4389. }
  4390. segmentHadPmt = true;
  4391. self.trigger('data', event);
  4392. }
  4393. })[data.type]();
  4394. };
  4395. this.reset = function () {
  4396. video.size = 0;
  4397. video.data.length = 0;
  4398. audio.size = 0;
  4399. audio.data.length = 0;
  4400. this.trigger('reset');
  4401. };
  4402. /**
  4403. * Flush any remaining input. Video PES packets may be of variable
  4404. * length. Normally, the start of a new video packet can trigger the
  4405. * finalization of the previous packet. That is not possible if no
  4406. * more video is forthcoming, however. In that case, some other
  4407. * mechanism (like the end of the file) has to be employed. When it is
  4408. * clear that no additional data is forthcoming, calling this method
  4409. * will flush the buffered packets.
  4410. */
  4411. this.flushStreams_ = function () {
  4412. // !!THIS ORDER IS IMPORTANT!!
  4413. // video first then audio
  4414. flushStream(video, 'video');
  4415. flushStream(audio, 'audio');
  4416. flushStream(timedMetadata, 'timed-metadata');
  4417. };
  4418. this.flush = function () {
  4419. // if on flush we haven't had a pmt emitted
  4420. // and we have a pmt to emit. emit the pmt
  4421. // so that we trigger a trackinfo downstream.
  4422. if (!segmentHadPmt && programMapTable) {
  4423. var pmt = {
  4424. type: 'metadata',
  4425. tracks: []
  4426. }; // translate audio and video streams to tracks
  4427. if (programMapTable.video !== null) {
  4428. pmt.tracks.push({
  4429. timelineStartInfo: {
  4430. baseMediaDecodeTime: 0
  4431. },
  4432. id: +programMapTable.video,
  4433. codec: 'avc',
  4434. type: 'video'
  4435. });
  4436. }
  4437. if (programMapTable.audio !== null) {
  4438. pmt.tracks.push({
  4439. timelineStartInfo: {
  4440. baseMediaDecodeTime: 0
  4441. },
  4442. id: +programMapTable.audio,
  4443. codec: 'adts',
  4444. type: 'audio'
  4445. });
  4446. }
  4447. self.trigger('data', pmt);
  4448. }
  4449. segmentHadPmt = false;
  4450. this.flushStreams_();
  4451. this.trigger('done');
  4452. };
  4453. };
  4454. _ElementaryStream.prototype = new stream();
  4455. var m2ts = {
  4456. PAT_PID: 0x0000,
  4457. MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH,
  4458. TransportPacketStream: _TransportPacketStream,
  4459. TransportParseStream: _TransportParseStream,
  4460. ElementaryStream: _ElementaryStream,
  4461. TimestampRolloverStream: TimestampRolloverStream,
  4462. CaptionStream: captionStream.CaptionStream,
  4463. Cea608Stream: captionStream.Cea608Stream,
  4464. Cea708Stream: captionStream.Cea708Stream,
  4465. MetadataStream: metadataStream
  4466. };
  4467. for (var type in streamTypes) {
  4468. if (streamTypes.hasOwnProperty(type)) {
  4469. m2ts[type] = streamTypes[type];
  4470. }
  4471. }
  4472. var m2ts_1 = m2ts;
  4473. var ONE_SECOND_IN_TS$1 = clock.ONE_SECOND_IN_TS;
  4474. var _AdtsStream;
  4475. var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  4476. /*
  4477. * Accepts a ElementaryStream and emits data events with parsed
  4478. * AAC Audio Frames of the individual packets. Input audio in ADTS
  4479. * format is unpacked and re-emitted as AAC frames.
  4480. *
  4481. * @see http://wiki.multimedia.cx/index.php?title=ADTS
  4482. * @see http://wiki.multimedia.cx/?title=Understanding_AAC
  4483. */
  4484. _AdtsStream = function AdtsStream(handlePartialSegments) {
  4485. var buffer,
  4486. frameNum = 0;
  4487. _AdtsStream.prototype.init.call(this);
  4488. this.skipWarn_ = function (start, end) {
  4489. this.trigger('log', {
  4490. level: 'warn',
  4491. message: "adts skiping bytes " + start + " to " + end + " in frame " + frameNum + " outside syncword"
  4492. });
  4493. };
  4494. this.push = function (packet) {
  4495. var i = 0,
  4496. frameLength,
  4497. protectionSkipBytes,
  4498. oldBuffer,
  4499. sampleCount,
  4500. adtsFrameDuration;
  4501. if (!handlePartialSegments) {
  4502. frameNum = 0;
  4503. }
  4504. if (packet.type !== 'audio') {
  4505. // ignore non-audio data
  4506. return;
  4507. } // Prepend any data in the buffer to the input data so that we can parse
  4508. // aac frames the cross a PES packet boundary
  4509. if (buffer && buffer.length) {
  4510. oldBuffer = buffer;
  4511. buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
  4512. buffer.set(oldBuffer);
  4513. buffer.set(packet.data, oldBuffer.byteLength);
  4514. } else {
  4515. buffer = packet.data;
  4516. } // unpack any ADTS frames which have been fully received
  4517. // for details on the ADTS header, see http://wiki.multimedia.cx/index.php?title=ADTS
  4518. var skip; // We use i + 7 here because we want to be able to parse the entire header.
  4519. // If we don't have enough bytes to do that, then we definitely won't have a full frame.
  4520. while (i + 7 < buffer.length) {
  4521. // Look for the start of an ADTS header..
  4522. if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
  4523. if (typeof skip !== 'number') {
  4524. skip = i;
  4525. } // If a valid header was not found, jump one forward and attempt to
  4526. // find a valid ADTS header starting at the next byte
  4527. i++;
  4528. continue;
  4529. }
  4530. if (typeof skip === 'number') {
  4531. this.skipWarn_(skip, i);
  4532. skip = null;
  4533. } // The protection skip bit tells us if we have 2 bytes of CRC data at the
  4534. // end of the ADTS header
  4535. protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2; // Frame length is a 13 bit integer starting 16 bits from the
  4536. // end of the sync sequence
  4537. // NOTE: frame length includes the size of the header
  4538. frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
  4539. sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
  4540. adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$1 / ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2]; // If we don't have enough data to actually finish this ADTS frame,
  4541. // then we have to wait for more data
  4542. if (buffer.byteLength - i < frameLength) {
  4543. break;
  4544. } // Otherwise, deliver the complete AAC frame
  4545. this.trigger('data', {
  4546. pts: packet.pts + frameNum * adtsFrameDuration,
  4547. dts: packet.dts + frameNum * adtsFrameDuration,
  4548. sampleCount: sampleCount,
  4549. audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
  4550. channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
  4551. samplerate: ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2],
  4552. samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
  4553. // assume ISO/IEC 14496-12 AudioSampleEntry default of 16
  4554. samplesize: 16,
  4555. // data is the frame without it's header
  4556. data: buffer.subarray(i + 7 + protectionSkipBytes, i + frameLength)
  4557. });
  4558. frameNum++;
  4559. i += frameLength;
  4560. }
  4561. if (typeof skip === 'number') {
  4562. this.skipWarn_(skip, i);
  4563. skip = null;
  4564. } // remove processed bytes from the buffer.
  4565. buffer = buffer.subarray(i);
  4566. };
  4567. this.flush = function () {
  4568. frameNum = 0;
  4569. this.trigger('done');
  4570. };
  4571. this.reset = function () {
  4572. buffer = void 0;
  4573. this.trigger('reset');
  4574. };
  4575. this.endTimeline = function () {
  4576. buffer = void 0;
  4577. this.trigger('endedtimeline');
  4578. };
  4579. };
  4580. _AdtsStream.prototype = new stream();
  4581. var adts = _AdtsStream;
  4582. /**
  4583. * mux.js
  4584. *
  4585. * Copyright (c) Brightcove
  4586. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4587. */
  4588. var ExpGolomb;
  4589. /**
  4590. * Parser for exponential Golomb codes, a variable-bitwidth number encoding
  4591. * scheme used by h264.
  4592. */
  4593. ExpGolomb = function ExpGolomb(workingData) {
  4594. var // the number of bytes left to examine in workingData
  4595. workingBytesAvailable = workingData.byteLength,
  4596. // the current word being examined
  4597. workingWord = 0,
  4598. // :uint
  4599. // the number of bits left to examine in the current word
  4600. workingBitsAvailable = 0; // :uint;
  4601. // ():uint
  4602. this.length = function () {
  4603. return 8 * workingBytesAvailable;
  4604. }; // ():uint
  4605. this.bitsAvailable = function () {
  4606. return 8 * workingBytesAvailable + workingBitsAvailable;
  4607. }; // ():void
  4608. this.loadWord = function () {
  4609. var position = workingData.byteLength - workingBytesAvailable,
  4610. workingBytes = new Uint8Array(4),
  4611. availableBytes = Math.min(4, workingBytesAvailable);
  4612. if (availableBytes === 0) {
  4613. throw new Error('no bytes available');
  4614. }
  4615. workingBytes.set(workingData.subarray(position, position + availableBytes));
  4616. workingWord = new DataView(workingBytes.buffer).getUint32(0); // track the amount of workingData that has been processed
  4617. workingBitsAvailable = availableBytes * 8;
  4618. workingBytesAvailable -= availableBytes;
  4619. }; // (count:int):void
  4620. this.skipBits = function (count) {
  4621. var skipBytes; // :int
  4622. if (workingBitsAvailable > count) {
  4623. workingWord <<= count;
  4624. workingBitsAvailable -= count;
  4625. } else {
  4626. count -= workingBitsAvailable;
  4627. skipBytes = Math.floor(count / 8);
  4628. count -= skipBytes * 8;
  4629. workingBytesAvailable -= skipBytes;
  4630. this.loadWord();
  4631. workingWord <<= count;
  4632. workingBitsAvailable -= count;
  4633. }
  4634. }; // (size:int):uint
  4635. this.readBits = function (size) {
  4636. var bits = Math.min(workingBitsAvailable, size),
  4637. // :uint
  4638. valu = workingWord >>> 32 - bits; // :uint
  4639. // if size > 31, handle error
  4640. workingBitsAvailable -= bits;
  4641. if (workingBitsAvailable > 0) {
  4642. workingWord <<= bits;
  4643. } else if (workingBytesAvailable > 0) {
  4644. this.loadWord();
  4645. }
  4646. bits = size - bits;
  4647. if (bits > 0) {
  4648. return valu << bits | this.readBits(bits);
  4649. }
  4650. return valu;
  4651. }; // ():uint
  4652. this.skipLeadingZeros = function () {
  4653. var leadingZeroCount; // :uint
  4654. for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
  4655. if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
  4656. // the first bit of working word is 1
  4657. workingWord <<= leadingZeroCount;
  4658. workingBitsAvailable -= leadingZeroCount;
  4659. return leadingZeroCount;
  4660. }
  4661. } // we exhausted workingWord and still have not found a 1
  4662. this.loadWord();
  4663. return leadingZeroCount + this.skipLeadingZeros();
  4664. }; // ():void
  4665. this.skipUnsignedExpGolomb = function () {
  4666. this.skipBits(1 + this.skipLeadingZeros());
  4667. }; // ():void
  4668. this.skipExpGolomb = function () {
  4669. this.skipBits(1 + this.skipLeadingZeros());
  4670. }; // ():uint
  4671. this.readUnsignedExpGolomb = function () {
  4672. var clz = this.skipLeadingZeros(); // :uint
  4673. return this.readBits(clz + 1) - 1;
  4674. }; // ():int
  4675. this.readExpGolomb = function () {
  4676. var valu = this.readUnsignedExpGolomb(); // :int
  4677. if (0x01 & valu) {
  4678. // the number is odd if the low order bit is set
  4679. return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
  4680. }
  4681. return -1 * (valu >>> 1); // divide by two then make it negative
  4682. }; // Some convenience functions
  4683. // :Boolean
  4684. this.readBoolean = function () {
  4685. return this.readBits(1) === 1;
  4686. }; // ():int
  4687. this.readUnsignedByte = function () {
  4688. return this.readBits(8);
  4689. };
  4690. this.loadWord();
  4691. };
  4692. var expGolomb = ExpGolomb;
  4693. var _H264Stream, _NalByteStream;
  4694. var PROFILES_WITH_OPTIONAL_SPS_DATA;
  4695. /**
  4696. * Accepts a NAL unit byte stream and unpacks the embedded NAL units.
  4697. */
  4698. _NalByteStream = function NalByteStream() {
  4699. var syncPoint = 0,
  4700. i,
  4701. buffer;
  4702. _NalByteStream.prototype.init.call(this);
  4703. /*
  4704. * Scans a byte stream and triggers a data event with the NAL units found.
  4705. * @param {Object} data Event received from H264Stream
  4706. * @param {Uint8Array} data.data The h264 byte stream to be scanned
  4707. *
  4708. * @see H264Stream.push
  4709. */
  4710. this.push = function (data) {
  4711. var swapBuffer;
  4712. if (!buffer) {
  4713. buffer = data.data;
  4714. } else {
  4715. swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
  4716. swapBuffer.set(buffer);
  4717. swapBuffer.set(data.data, buffer.byteLength);
  4718. buffer = swapBuffer;
  4719. }
  4720. var len = buffer.byteLength; // Rec. ITU-T H.264, Annex B
  4721. // scan for NAL unit boundaries
  4722. // a match looks like this:
  4723. // 0 0 1 .. NAL .. 0 0 1
  4724. // ^ sync point ^ i
  4725. // or this:
  4726. // 0 0 1 .. NAL .. 0 0 0
  4727. // ^ sync point ^ i
  4728. // advance the sync point to a NAL start, if necessary
  4729. for (; syncPoint < len - 3; syncPoint++) {
  4730. if (buffer[syncPoint + 2] === 1) {
  4731. // the sync point is properly aligned
  4732. i = syncPoint + 5;
  4733. break;
  4734. }
  4735. }
  4736. while (i < len) {
  4737. // look at the current byte to determine if we've hit the end of
  4738. // a NAL unit boundary
  4739. switch (buffer[i]) {
  4740. case 0:
  4741. // skip past non-sync sequences
  4742. if (buffer[i - 1] !== 0) {
  4743. i += 2;
  4744. break;
  4745. } else if (buffer[i - 2] !== 0) {
  4746. i++;
  4747. break;
  4748. } // deliver the NAL unit if it isn't empty
  4749. if (syncPoint + 3 !== i - 2) {
  4750. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  4751. } // drop trailing zeroes
  4752. do {
  4753. i++;
  4754. } while (buffer[i] !== 1 && i < len);
  4755. syncPoint = i - 2;
  4756. i += 3;
  4757. break;
  4758. case 1:
  4759. // skip past non-sync sequences
  4760. if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
  4761. i += 3;
  4762. break;
  4763. } // deliver the NAL unit
  4764. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  4765. syncPoint = i - 2;
  4766. i += 3;
  4767. break;
  4768. default:
  4769. // the current byte isn't a one or zero, so it cannot be part
  4770. // of a sync sequence
  4771. i += 3;
  4772. break;
  4773. }
  4774. } // filter out the NAL units that were delivered
  4775. buffer = buffer.subarray(syncPoint);
  4776. i -= syncPoint;
  4777. syncPoint = 0;
  4778. };
  4779. this.reset = function () {
  4780. buffer = null;
  4781. syncPoint = 0;
  4782. this.trigger('reset');
  4783. };
  4784. this.flush = function () {
  4785. // deliver the last buffered NAL unit
  4786. if (buffer && buffer.byteLength > 3) {
  4787. this.trigger('data', buffer.subarray(syncPoint + 3));
  4788. } // reset the stream state
  4789. buffer = null;
  4790. syncPoint = 0;
  4791. this.trigger('done');
  4792. };
  4793. this.endTimeline = function () {
  4794. this.flush();
  4795. this.trigger('endedtimeline');
  4796. };
  4797. };
  4798. _NalByteStream.prototype = new stream(); // values of profile_idc that indicate additional fields are included in the SPS
  4799. // see Recommendation ITU-T H.264 (4/2013),
  4800. // 7.3.2.1.1 Sequence parameter set data syntax
  4801. PROFILES_WITH_OPTIONAL_SPS_DATA = {
  4802. 100: true,
  4803. 110: true,
  4804. 122: true,
  4805. 244: true,
  4806. 44: true,
  4807. 83: true,
  4808. 86: true,
  4809. 118: true,
  4810. 128: true,
  4811. // TODO: the three profiles below don't
  4812. // appear to have sps data in the specificiation anymore?
  4813. 138: true,
  4814. 139: true,
  4815. 134: true
  4816. };
  4817. /**
  4818. * Accepts input from a ElementaryStream and produces H.264 NAL unit data
  4819. * events.
  4820. */
  4821. _H264Stream = function H264Stream() {
  4822. var nalByteStream = new _NalByteStream(),
  4823. self,
  4824. trackId,
  4825. currentPts,
  4826. currentDts,
  4827. discardEmulationPreventionBytes,
  4828. readSequenceParameterSet,
  4829. skipScalingList;
  4830. _H264Stream.prototype.init.call(this);
  4831. self = this;
  4832. /*
  4833. * Pushes a packet from a stream onto the NalByteStream
  4834. *
  4835. * @param {Object} packet - A packet received from a stream
  4836. * @param {Uint8Array} packet.data - The raw bytes of the packet
  4837. * @param {Number} packet.dts - Decode timestamp of the packet
  4838. * @param {Number} packet.pts - Presentation timestamp of the packet
  4839. * @param {Number} packet.trackId - The id of the h264 track this packet came from
  4840. * @param {('video'|'audio')} packet.type - The type of packet
  4841. *
  4842. */
  4843. this.push = function (packet) {
  4844. if (packet.type !== 'video') {
  4845. return;
  4846. }
  4847. trackId = packet.trackId;
  4848. currentPts = packet.pts;
  4849. currentDts = packet.dts;
  4850. nalByteStream.push(packet);
  4851. };
  4852. /*
  4853. * Identify NAL unit types and pass on the NALU, trackId, presentation and decode timestamps
  4854. * for the NALUs to the next stream component.
  4855. * Also, preprocess caption and sequence parameter NALUs.
  4856. *
  4857. * @param {Uint8Array} data - A NAL unit identified by `NalByteStream.push`
  4858. * @see NalByteStream.push
  4859. */
  4860. nalByteStream.on('data', function (data) {
  4861. var event = {
  4862. trackId: trackId,
  4863. pts: currentPts,
  4864. dts: currentDts,
  4865. data: data,
  4866. nalUnitTypeCode: data[0] & 0x1f
  4867. };
  4868. switch (event.nalUnitTypeCode) {
  4869. case 0x05:
  4870. event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
  4871. break;
  4872. case 0x06:
  4873. event.nalUnitType = 'sei_rbsp';
  4874. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  4875. break;
  4876. case 0x07:
  4877. event.nalUnitType = 'seq_parameter_set_rbsp';
  4878. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  4879. event.config = readSequenceParameterSet(event.escapedRBSP);
  4880. break;
  4881. case 0x08:
  4882. event.nalUnitType = 'pic_parameter_set_rbsp';
  4883. break;
  4884. case 0x09:
  4885. event.nalUnitType = 'access_unit_delimiter_rbsp';
  4886. break;
  4887. } // This triggers data on the H264Stream
  4888. self.trigger('data', event);
  4889. });
  4890. nalByteStream.on('done', function () {
  4891. self.trigger('done');
  4892. });
  4893. nalByteStream.on('partialdone', function () {
  4894. self.trigger('partialdone');
  4895. });
  4896. nalByteStream.on('reset', function () {
  4897. self.trigger('reset');
  4898. });
  4899. nalByteStream.on('endedtimeline', function () {
  4900. self.trigger('endedtimeline');
  4901. });
  4902. this.flush = function () {
  4903. nalByteStream.flush();
  4904. };
  4905. this.partialFlush = function () {
  4906. nalByteStream.partialFlush();
  4907. };
  4908. this.reset = function () {
  4909. nalByteStream.reset();
  4910. };
  4911. this.endTimeline = function () {
  4912. nalByteStream.endTimeline();
  4913. };
  4914. /**
  4915. * Advance the ExpGolomb decoder past a scaling list. The scaling
  4916. * list is optionally transmitted as part of a sequence parameter
  4917. * set and is not relevant to transmuxing.
  4918. * @param count {number} the number of entries in this scaling list
  4919. * @param expGolombDecoder {object} an ExpGolomb pointed to the
  4920. * start of a scaling list
  4921. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  4922. */
  4923. skipScalingList = function skipScalingList(count, expGolombDecoder) {
  4924. var lastScale = 8,
  4925. nextScale = 8,
  4926. j,
  4927. deltaScale;
  4928. for (j = 0; j < count; j++) {
  4929. if (nextScale !== 0) {
  4930. deltaScale = expGolombDecoder.readExpGolomb();
  4931. nextScale = (lastScale + deltaScale + 256) % 256;
  4932. }
  4933. lastScale = nextScale === 0 ? lastScale : nextScale;
  4934. }
  4935. };
  4936. /**
  4937. * Expunge any "Emulation Prevention" bytes from a "Raw Byte
  4938. * Sequence Payload"
  4939. * @param data {Uint8Array} the bytes of a RBSP from a NAL
  4940. * unit
  4941. * @return {Uint8Array} the RBSP without any Emulation
  4942. * Prevention Bytes
  4943. */
  4944. discardEmulationPreventionBytes = function discardEmulationPreventionBytes(data) {
  4945. var length = data.byteLength,
  4946. emulationPreventionBytesPositions = [],
  4947. i = 1,
  4948. newLength,
  4949. newData; // Find all `Emulation Prevention Bytes`
  4950. while (i < length - 2) {
  4951. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  4952. emulationPreventionBytesPositions.push(i + 2);
  4953. i += 2;
  4954. } else {
  4955. i++;
  4956. }
  4957. } // If no Emulation Prevention Bytes were found just return the original
  4958. // array
  4959. if (emulationPreventionBytesPositions.length === 0) {
  4960. return data;
  4961. } // Create a new array to hold the NAL unit data
  4962. newLength = length - emulationPreventionBytesPositions.length;
  4963. newData = new Uint8Array(newLength);
  4964. var sourceIndex = 0;
  4965. for (i = 0; i < newLength; sourceIndex++, i++) {
  4966. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  4967. // Skip this byte
  4968. sourceIndex++; // Remove this position index
  4969. emulationPreventionBytesPositions.shift();
  4970. }
  4971. newData[i] = data[sourceIndex];
  4972. }
  4973. return newData;
  4974. };
  4975. /**
  4976. * Read a sequence parameter set and return some interesting video
  4977. * properties. A sequence parameter set is the H264 metadata that
  4978. * describes the properties of upcoming video frames.
  4979. * @param data {Uint8Array} the bytes of a sequence parameter set
  4980. * @return {object} an object with configuration parsed from the
  4981. * sequence parameter set, including the dimensions of the
  4982. * associated video frames.
  4983. */
  4984. readSequenceParameterSet = function readSequenceParameterSet(data) {
  4985. var frameCropLeftOffset = 0,
  4986. frameCropRightOffset = 0,
  4987. frameCropTopOffset = 0,
  4988. frameCropBottomOffset = 0,
  4989. expGolombDecoder,
  4990. profileIdc,
  4991. levelIdc,
  4992. profileCompatibility,
  4993. chromaFormatIdc,
  4994. picOrderCntType,
  4995. numRefFramesInPicOrderCntCycle,
  4996. picWidthInMbsMinus1,
  4997. picHeightInMapUnitsMinus1,
  4998. frameMbsOnlyFlag,
  4999. scalingListCount,
  5000. sarRatio = [1, 1],
  5001. aspectRatioIdc,
  5002. i;
  5003. expGolombDecoder = new expGolomb(data);
  5004. profileIdc = expGolombDecoder.readUnsignedByte(); // profile_idc
  5005. profileCompatibility = expGolombDecoder.readUnsignedByte(); // constraint_set[0-5]_flag
  5006. levelIdc = expGolombDecoder.readUnsignedByte(); // level_idc u(8)
  5007. expGolombDecoder.skipUnsignedExpGolomb(); // seq_parameter_set_id
  5008. // some profiles have more optional data we don't need
  5009. if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
  5010. chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
  5011. if (chromaFormatIdc === 3) {
  5012. expGolombDecoder.skipBits(1); // separate_colour_plane_flag
  5013. }
  5014. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_luma_minus8
  5015. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_chroma_minus8
  5016. expGolombDecoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
  5017. if (expGolombDecoder.readBoolean()) {
  5018. // seq_scaling_matrix_present_flag
  5019. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  5020. for (i = 0; i < scalingListCount; i++) {
  5021. if (expGolombDecoder.readBoolean()) {
  5022. // seq_scaling_list_present_flag[ i ]
  5023. if (i < 6) {
  5024. skipScalingList(16, expGolombDecoder);
  5025. } else {
  5026. skipScalingList(64, expGolombDecoder);
  5027. }
  5028. }
  5029. }
  5030. }
  5031. }
  5032. expGolombDecoder.skipUnsignedExpGolomb(); // log2_max_frame_num_minus4
  5033. picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
  5034. if (picOrderCntType === 0) {
  5035. expGolombDecoder.readUnsignedExpGolomb(); // log2_max_pic_order_cnt_lsb_minus4
  5036. } else if (picOrderCntType === 1) {
  5037. expGolombDecoder.skipBits(1); // delta_pic_order_always_zero_flag
  5038. expGolombDecoder.skipExpGolomb(); // offset_for_non_ref_pic
  5039. expGolombDecoder.skipExpGolomb(); // offset_for_top_to_bottom_field
  5040. numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
  5041. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  5042. expGolombDecoder.skipExpGolomb(); // offset_for_ref_frame[ i ]
  5043. }
  5044. }
  5045. expGolombDecoder.skipUnsignedExpGolomb(); // max_num_ref_frames
  5046. expGolombDecoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
  5047. picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  5048. picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  5049. frameMbsOnlyFlag = expGolombDecoder.readBits(1);
  5050. if (frameMbsOnlyFlag === 0) {
  5051. expGolombDecoder.skipBits(1); // mb_adaptive_frame_field_flag
  5052. }
  5053. expGolombDecoder.skipBits(1); // direct_8x8_inference_flag
  5054. if (expGolombDecoder.readBoolean()) {
  5055. // frame_cropping_flag
  5056. frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
  5057. frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
  5058. frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
  5059. frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
  5060. }
  5061. if (expGolombDecoder.readBoolean()) {
  5062. // vui_parameters_present_flag
  5063. if (expGolombDecoder.readBoolean()) {
  5064. // aspect_ratio_info_present_flag
  5065. aspectRatioIdc = expGolombDecoder.readUnsignedByte();
  5066. switch (aspectRatioIdc) {
  5067. case 1:
  5068. sarRatio = [1, 1];
  5069. break;
  5070. case 2:
  5071. sarRatio = [12, 11];
  5072. break;
  5073. case 3:
  5074. sarRatio = [10, 11];
  5075. break;
  5076. case 4:
  5077. sarRatio = [16, 11];
  5078. break;
  5079. case 5:
  5080. sarRatio = [40, 33];
  5081. break;
  5082. case 6:
  5083. sarRatio = [24, 11];
  5084. break;
  5085. case 7:
  5086. sarRatio = [20, 11];
  5087. break;
  5088. case 8:
  5089. sarRatio = [32, 11];
  5090. break;
  5091. case 9:
  5092. sarRatio = [80, 33];
  5093. break;
  5094. case 10:
  5095. sarRatio = [18, 11];
  5096. break;
  5097. case 11:
  5098. sarRatio = [15, 11];
  5099. break;
  5100. case 12:
  5101. sarRatio = [64, 33];
  5102. break;
  5103. case 13:
  5104. sarRatio = [160, 99];
  5105. break;
  5106. case 14:
  5107. sarRatio = [4, 3];
  5108. break;
  5109. case 15:
  5110. sarRatio = [3, 2];
  5111. break;
  5112. case 16:
  5113. sarRatio = [2, 1];
  5114. break;
  5115. case 255:
  5116. {
  5117. sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
  5118. break;
  5119. }
  5120. }
  5121. if (sarRatio) {
  5122. sarRatio[0] / sarRatio[1];
  5123. }
  5124. }
  5125. }
  5126. return {
  5127. profileIdc: profileIdc,
  5128. levelIdc: levelIdc,
  5129. profileCompatibility: profileCompatibility,
  5130. width: (picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2,
  5131. height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
  5132. // sar is sample aspect ratio
  5133. sarRatio: sarRatio
  5134. };
  5135. };
  5136. };
  5137. _H264Stream.prototype = new stream();
  5138. var h264 = {
  5139. H264Stream: _H264Stream,
  5140. NalByteStream: _NalByteStream
  5141. };
  5142. /**
  5143. * mux.js
  5144. *
  5145. * Copyright (c) Brightcove
  5146. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  5147. *
  5148. * Utilities to detect basic properties and metadata about Aac data.
  5149. */
  5150. var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  5151. var parseId3TagSize = function parseId3TagSize(header, byteIndex) {
  5152. var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
  5153. flags = header[byteIndex + 5],
  5154. footerPresent = (flags & 16) >> 4; // if we get a negative returnSize clamp it to 0
  5155. returnSize = returnSize >= 0 ? returnSize : 0;
  5156. if (footerPresent) {
  5157. return returnSize + 20;
  5158. }
  5159. return returnSize + 10;
  5160. };
  5161. var getId3Offset = function getId3Offset(data, offset) {
  5162. if (data.length - offset < 10 || data[offset] !== 'I'.charCodeAt(0) || data[offset + 1] !== 'D'.charCodeAt(0) || data[offset + 2] !== '3'.charCodeAt(0)) {
  5163. return offset;
  5164. }
  5165. offset += parseId3TagSize(data, offset);
  5166. return getId3Offset(data, offset);
  5167. }; // TODO: use vhs-utils
  5168. var isLikelyAacData$1 = function isLikelyAacData(data) {
  5169. var offset = getId3Offset(data, 0);
  5170. return data.length >= offset + 2 && (data[offset] & 0xFF) === 0xFF && (data[offset + 1] & 0xF0) === 0xF0 && // verify that the 2 layer bits are 0, aka this
  5171. // is not mp3 data but aac data.
  5172. (data[offset + 1] & 0x16) === 0x10;
  5173. };
  5174. var parseSyncSafeInteger = function parseSyncSafeInteger(data) {
  5175. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  5176. }; // return a percent-encoded representation of the specified byte range
  5177. // @see http://en.wikipedia.org/wiki/Percent-encoding
  5178. var percentEncode = function percentEncode(bytes, start, end) {
  5179. var i,
  5180. result = '';
  5181. for (i = start; i < end; i++) {
  5182. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  5183. }
  5184. return result;
  5185. }; // return the string representation of the specified byte range,
  5186. // interpreted as ISO-8859-1.
  5187. var parseIso88591 = function parseIso88591(bytes, start, end) {
  5188. return unescape(percentEncode(bytes, start, end)); // jshint ignore:line
  5189. };
  5190. var parseAdtsSize = function parseAdtsSize(header, byteIndex) {
  5191. var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
  5192. middle = header[byteIndex + 4] << 3,
  5193. highTwo = header[byteIndex + 3] & 0x3 << 11;
  5194. return highTwo | middle | lowThree;
  5195. };
  5196. var parseType = function parseType(header, byteIndex) {
  5197. if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
  5198. return 'timed-metadata';
  5199. } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
  5200. return 'audio';
  5201. }
  5202. return null;
  5203. };
  5204. var parseSampleRate = function parseSampleRate(packet) {
  5205. var i = 0;
  5206. while (i + 5 < packet.length) {
  5207. if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
  5208. // If a valid header was not found, jump one forward and attempt to
  5209. // find a valid ADTS header starting at the next byte
  5210. i++;
  5211. continue;
  5212. }
  5213. return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
  5214. }
  5215. return null;
  5216. };
  5217. var parseAacTimestamp = function parseAacTimestamp(packet) {
  5218. var frameStart, frameSize, frame, frameHeader; // find the start of the first frame and the end of the tag
  5219. frameStart = 10;
  5220. if (packet[5] & 0x40) {
  5221. // advance the frame start past the extended header
  5222. frameStart += 4; // header size field
  5223. frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
  5224. } // parse one or more ID3 frames
  5225. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  5226. do {
  5227. // determine the number of bytes in this frame
  5228. frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
  5229. if (frameSize < 1) {
  5230. return null;
  5231. }
  5232. frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
  5233. if (frameHeader === 'PRIV') {
  5234. frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
  5235. for (var i = 0; i < frame.byteLength; i++) {
  5236. if (frame[i] === 0) {
  5237. var owner = parseIso88591(frame, 0, i);
  5238. if (owner === 'com.apple.streaming.transportStreamTimestamp') {
  5239. var d = frame.subarray(i + 1);
  5240. var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  5241. size *= 4;
  5242. size += d[7] & 0x03;
  5243. return size;
  5244. }
  5245. break;
  5246. }
  5247. }
  5248. }
  5249. frameStart += 10; // advance past the frame header
  5250. frameStart += frameSize; // advance past the frame body
  5251. } while (frameStart < packet.byteLength);
  5252. return null;
  5253. };
  5254. var utils = {
  5255. isLikelyAacData: isLikelyAacData$1,
  5256. parseId3TagSize: parseId3TagSize,
  5257. parseAdtsSize: parseAdtsSize,
  5258. parseType: parseType,
  5259. parseSampleRate: parseSampleRate,
  5260. parseAacTimestamp: parseAacTimestamp
  5261. };
  5262. var _AacStream;
  5263. /**
  5264. * Splits an incoming stream of binary data into ADTS and ID3 Frames.
  5265. */
  5266. _AacStream = function AacStream() {
  5267. var everything = new Uint8Array(),
  5268. timeStamp = 0;
  5269. _AacStream.prototype.init.call(this);
  5270. this.setTimestamp = function (timestamp) {
  5271. timeStamp = timestamp;
  5272. };
  5273. this.push = function (bytes) {
  5274. var frameSize = 0,
  5275. byteIndex = 0,
  5276. bytesLeft,
  5277. chunk,
  5278. packet,
  5279. tempLength; // If there are bytes remaining from the last segment, prepend them to the
  5280. // bytes that were pushed in
  5281. if (everything.length) {
  5282. tempLength = everything.length;
  5283. everything = new Uint8Array(bytes.byteLength + tempLength);
  5284. everything.set(everything.subarray(0, tempLength));
  5285. everything.set(bytes, tempLength);
  5286. } else {
  5287. everything = bytes;
  5288. }
  5289. while (everything.length - byteIndex >= 3) {
  5290. if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
  5291. // Exit early because we don't have enough to parse
  5292. // the ID3 tag header
  5293. if (everything.length - byteIndex < 10) {
  5294. break;
  5295. } // check framesize
  5296. frameSize = utils.parseId3TagSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  5297. // to emit a full packet
  5298. // Add to byteIndex to support multiple ID3 tags in sequence
  5299. if (byteIndex + frameSize > everything.length) {
  5300. break;
  5301. }
  5302. chunk = {
  5303. type: 'timed-metadata',
  5304. data: everything.subarray(byteIndex, byteIndex + frameSize)
  5305. };
  5306. this.trigger('data', chunk);
  5307. byteIndex += frameSize;
  5308. continue;
  5309. } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
  5310. // Exit early because we don't have enough to parse
  5311. // the ADTS frame header
  5312. if (everything.length - byteIndex < 7) {
  5313. break;
  5314. }
  5315. frameSize = utils.parseAdtsSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  5316. // to emit a full packet
  5317. if (byteIndex + frameSize > everything.length) {
  5318. break;
  5319. }
  5320. packet = {
  5321. type: 'audio',
  5322. data: everything.subarray(byteIndex, byteIndex + frameSize),
  5323. pts: timeStamp,
  5324. dts: timeStamp
  5325. };
  5326. this.trigger('data', packet);
  5327. byteIndex += frameSize;
  5328. continue;
  5329. }
  5330. byteIndex++;
  5331. }
  5332. bytesLeft = everything.length - byteIndex;
  5333. if (bytesLeft > 0) {
  5334. everything = everything.subarray(byteIndex);
  5335. } else {
  5336. everything = new Uint8Array();
  5337. }
  5338. };
  5339. this.reset = function () {
  5340. everything = new Uint8Array();
  5341. this.trigger('reset');
  5342. };
  5343. this.endTimeline = function () {
  5344. everything = new Uint8Array();
  5345. this.trigger('endedtimeline');
  5346. };
  5347. };
  5348. _AacStream.prototype = new stream();
  5349. var aac = _AacStream;
  5350. // constants
  5351. var AUDIO_PROPERTIES = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
  5352. var audioProperties = AUDIO_PROPERTIES;
  5353. var VIDEO_PROPERTIES = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
  5354. var videoProperties = VIDEO_PROPERTIES;
  5355. var H264Stream = h264.H264Stream;
  5356. var isLikelyAacData = utils.isLikelyAacData;
  5357. var ONE_SECOND_IN_TS = clock.ONE_SECOND_IN_TS; // object types
  5358. var _VideoSegmentStream, _AudioSegmentStream, _Transmuxer, _CoalesceStream;
  5359. var retriggerForStream = function retriggerForStream(key, event) {
  5360. event.stream = key;
  5361. this.trigger('log', event);
  5362. };
  5363. var addPipelineLogRetriggers = function addPipelineLogRetriggers(transmuxer, pipeline) {
  5364. var keys = Object.keys(pipeline);
  5365. for (var i = 0; i < keys.length; i++) {
  5366. var key = keys[i]; // skip non-stream keys and headOfPipeline
  5367. // which is just a duplicate
  5368. if (key === 'headOfPipeline' || !pipeline[key].on) {
  5369. continue;
  5370. }
  5371. pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
  5372. }
  5373. };
  5374. /**
  5375. * Compare two arrays (even typed) for same-ness
  5376. */
  5377. var arrayEquals = function arrayEquals(a, b) {
  5378. var i;
  5379. if (a.length !== b.length) {
  5380. return false;
  5381. } // compare the value of each element in the array
  5382. for (i = 0; i < a.length; i++) {
  5383. if (a[i] !== b[i]) {
  5384. return false;
  5385. }
  5386. }
  5387. return true;
  5388. };
  5389. var generateSegmentTimingInfo = function generateSegmentTimingInfo(baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
  5390. var ptsOffsetFromDts = startPts - startDts,
  5391. decodeDuration = endDts - startDts,
  5392. presentationDuration = endPts - startPts; // The PTS and DTS values are based on the actual stream times from the segment,
  5393. // however, the player time values will reflect a start from the baseMediaDecodeTime.
  5394. // In order to provide relevant values for the player times, base timing info on the
  5395. // baseMediaDecodeTime and the DTS and PTS durations of the segment.
  5396. return {
  5397. start: {
  5398. dts: baseMediaDecodeTime,
  5399. pts: baseMediaDecodeTime + ptsOffsetFromDts
  5400. },
  5401. end: {
  5402. dts: baseMediaDecodeTime + decodeDuration,
  5403. pts: baseMediaDecodeTime + presentationDuration
  5404. },
  5405. prependedContentDuration: prependedContentDuration,
  5406. baseMediaDecodeTime: baseMediaDecodeTime
  5407. };
  5408. };
  5409. /**
  5410. * Constructs a single-track, ISO BMFF media segment from AAC data
  5411. * events. The output of this stream can be fed to a SourceBuffer
  5412. * configured with a suitable initialization segment.
  5413. * @param track {object} track metadata configuration
  5414. * @param options {object} transmuxer options object
  5415. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5416. * in the source; false to adjust the first segment to start at 0.
  5417. */
  5418. _AudioSegmentStream = function AudioSegmentStream(track, options) {
  5419. var adtsFrames = [],
  5420. sequenceNumber,
  5421. earliestAllowedDts = 0,
  5422. audioAppendStartTs = 0,
  5423. videoBaseMediaDecodeTime = Infinity;
  5424. options = options || {};
  5425. sequenceNumber = options.firstSequenceNumber || 0;
  5426. _AudioSegmentStream.prototype.init.call(this);
  5427. this.push = function (data) {
  5428. trackDecodeInfo.collectDtsInfo(track, data);
  5429. if (track) {
  5430. audioProperties.forEach(function (prop) {
  5431. track[prop] = data[prop];
  5432. });
  5433. } // buffer audio data until end() is called
  5434. adtsFrames.push(data);
  5435. };
  5436. this.setEarliestDts = function (earliestDts) {
  5437. earliestAllowedDts = earliestDts;
  5438. };
  5439. this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  5440. videoBaseMediaDecodeTime = baseMediaDecodeTime;
  5441. };
  5442. this.setAudioAppendStart = function (timestamp) {
  5443. audioAppendStartTs = timestamp;
  5444. };
  5445. this.flush = function () {
  5446. var frames, moof, mdat, boxes, frameDuration, segmentDuration, videoClockCyclesOfSilencePrefixed; // return early if no audio data has been observed
  5447. if (adtsFrames.length === 0) {
  5448. this.trigger('done', 'AudioSegmentStream');
  5449. return;
  5450. }
  5451. frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
  5452. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps); // amount of audio filled but the value is in video clock rather than audio clock
  5453. videoClockCyclesOfSilencePrefixed = audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime); // we have to build the index from byte locations to
  5454. // samples (that is, adts frames) in the audio data
  5455. track.samples = audioFrameUtils.generateSampleTable(frames); // concatenate the audio data to constuct the mdat
  5456. mdat = mp4Generator.mdat(audioFrameUtils.concatenateFrameData(frames));
  5457. adtsFrames = [];
  5458. moof = mp4Generator.moof(sequenceNumber, [track]);
  5459. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // bump the sequence number for next time
  5460. sequenceNumber++;
  5461. boxes.set(moof);
  5462. boxes.set(mdat, moof.byteLength);
  5463. trackDecodeInfo.clearDtsInfo(track);
  5464. frameDuration = Math.ceil(ONE_SECOND_IN_TS * 1024 / track.samplerate); // TODO this check was added to maintain backwards compatibility (particularly with
  5465. // tests) on adding the timingInfo event. However, it seems unlikely that there's a
  5466. // valid use-case where an init segment/data should be triggered without associated
  5467. // frames. Leaving for now, but should be looked into.
  5468. if (frames.length) {
  5469. segmentDuration = frames.length * frameDuration;
  5470. this.trigger('segmentTimingInfo', generateSegmentTimingInfo( // The audio track's baseMediaDecodeTime is in audio clock cycles, but the
  5471. // frame info is in video clock cycles. Convert to match expectation of
  5472. // listeners (that all timestamps will be based on video clock cycles).
  5473. clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate), // frame times are already in video clock, as is segment duration
  5474. frames[0].dts, frames[0].pts, frames[0].dts + segmentDuration, frames[0].pts + segmentDuration, videoClockCyclesOfSilencePrefixed || 0));
  5475. this.trigger('timingInfo', {
  5476. start: frames[0].pts,
  5477. end: frames[0].pts + segmentDuration
  5478. });
  5479. }
  5480. this.trigger('data', {
  5481. track: track,
  5482. boxes: boxes
  5483. });
  5484. this.trigger('done', 'AudioSegmentStream');
  5485. };
  5486. this.reset = function () {
  5487. trackDecodeInfo.clearDtsInfo(track);
  5488. adtsFrames = [];
  5489. this.trigger('reset');
  5490. };
  5491. };
  5492. _AudioSegmentStream.prototype = new stream();
  5493. /**
  5494. * Constructs a single-track, ISO BMFF media segment from H264 data
  5495. * events. The output of this stream can be fed to a SourceBuffer
  5496. * configured with a suitable initialization segment.
  5497. * @param track {object} track metadata configuration
  5498. * @param options {object} transmuxer options object
  5499. * @param options.alignGopsAtEnd {boolean} If true, start from the end of the
  5500. * gopsToAlignWith list when attempting to align gop pts
  5501. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5502. * in the source; false to adjust the first segment to start at 0.
  5503. */
  5504. _VideoSegmentStream = function VideoSegmentStream(track, options) {
  5505. var sequenceNumber,
  5506. nalUnits = [],
  5507. gopsToAlignWith = [],
  5508. config,
  5509. pps;
  5510. options = options || {};
  5511. sequenceNumber = options.firstSequenceNumber || 0;
  5512. _VideoSegmentStream.prototype.init.call(this);
  5513. delete track.minPTS;
  5514. this.gopCache_ = [];
  5515. /**
  5516. * Constructs a ISO BMFF segment given H264 nalUnits
  5517. * @param {Object} nalUnit A data event representing a nalUnit
  5518. * @param {String} nalUnit.nalUnitType
  5519. * @param {Object} nalUnit.config Properties for a mp4 track
  5520. * @param {Uint8Array} nalUnit.data The nalUnit bytes
  5521. * @see lib/codecs/h264.js
  5522. **/
  5523. this.push = function (nalUnit) {
  5524. trackDecodeInfo.collectDtsInfo(track, nalUnit); // record the track config
  5525. if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
  5526. config = nalUnit.config;
  5527. track.sps = [nalUnit.data];
  5528. videoProperties.forEach(function (prop) {
  5529. track[prop] = config[prop];
  5530. }, this);
  5531. }
  5532. if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
  5533. pps = nalUnit.data;
  5534. track.pps = [nalUnit.data];
  5535. } // buffer video until flush() is called
  5536. nalUnits.push(nalUnit);
  5537. };
  5538. /**
  5539. * Pass constructed ISO BMFF track and boxes on to the
  5540. * next stream in the pipeline
  5541. **/
  5542. this.flush = function () {
  5543. var frames,
  5544. gopForFusion,
  5545. gops,
  5546. moof,
  5547. mdat,
  5548. boxes,
  5549. prependedContentDuration = 0,
  5550. firstGop,
  5551. lastGop; // Throw away nalUnits at the start of the byte stream until
  5552. // we find the first AUD
  5553. while (nalUnits.length) {
  5554. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  5555. break;
  5556. }
  5557. nalUnits.shift();
  5558. } // Return early if no video data has been observed
  5559. if (nalUnits.length === 0) {
  5560. this.resetStream_();
  5561. this.trigger('done', 'VideoSegmentStream');
  5562. return;
  5563. } // Organize the raw nal-units into arrays that represent
  5564. // higher-level constructs such as frames and gops
  5565. // (group-of-pictures)
  5566. frames = frameUtils.groupNalsIntoFrames(nalUnits);
  5567. gops = frameUtils.groupFramesIntoGops(frames); // If the first frame of this fragment is not a keyframe we have
  5568. // a problem since MSE (on Chrome) requires a leading keyframe.
  5569. //
  5570. // We have two approaches to repairing this situation:
  5571. // 1) GOP-FUSION:
  5572. // This is where we keep track of the GOPS (group-of-pictures)
  5573. // from previous fragments and attempt to find one that we can
  5574. // prepend to the current fragment in order to create a valid
  5575. // fragment.
  5576. // 2) KEYFRAME-PULLING:
  5577. // Here we search for the first keyframe in the fragment and
  5578. // throw away all the frames between the start of the fragment
  5579. // and that keyframe. We then extend the duration and pull the
  5580. // PTS of the keyframe forward so that it covers the time range
  5581. // of the frames that were disposed of.
  5582. //
  5583. // #1 is far prefereable over #2 which can cause "stuttering" but
  5584. // requires more things to be just right.
  5585. if (!gops[0][0].keyFrame) {
  5586. // Search for a gop for fusion from our gopCache
  5587. gopForFusion = this.getGopForFusion_(nalUnits[0], track);
  5588. if (gopForFusion) {
  5589. // in order to provide more accurate timing information about the segment, save
  5590. // the number of seconds prepended to the original segment due to GOP fusion
  5591. prependedContentDuration = gopForFusion.duration;
  5592. gops.unshift(gopForFusion); // Adjust Gops' metadata to account for the inclusion of the
  5593. // new gop at the beginning
  5594. gops.byteLength += gopForFusion.byteLength;
  5595. gops.nalCount += gopForFusion.nalCount;
  5596. gops.pts = gopForFusion.pts;
  5597. gops.dts = gopForFusion.dts;
  5598. gops.duration += gopForFusion.duration;
  5599. } else {
  5600. // If we didn't find a candidate gop fall back to keyframe-pulling
  5601. gops = frameUtils.extendFirstKeyFrame(gops);
  5602. }
  5603. } // Trim gops to align with gopsToAlignWith
  5604. if (gopsToAlignWith.length) {
  5605. var alignedGops;
  5606. if (options.alignGopsAtEnd) {
  5607. alignedGops = this.alignGopsAtEnd_(gops);
  5608. } else {
  5609. alignedGops = this.alignGopsAtStart_(gops);
  5610. }
  5611. if (!alignedGops) {
  5612. // save all the nals in the last GOP into the gop cache
  5613. this.gopCache_.unshift({
  5614. gop: gops.pop(),
  5615. pps: track.pps,
  5616. sps: track.sps
  5617. }); // Keep a maximum of 6 GOPs in the cache
  5618. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  5619. nalUnits = []; // return early no gops can be aligned with desired gopsToAlignWith
  5620. this.resetStream_();
  5621. this.trigger('done', 'VideoSegmentStream');
  5622. return;
  5623. } // Some gops were trimmed. clear dts info so minSegmentDts and pts are correct
  5624. // when recalculated before sending off to CoalesceStream
  5625. trackDecodeInfo.clearDtsInfo(track);
  5626. gops = alignedGops;
  5627. }
  5628. trackDecodeInfo.collectDtsInfo(track, gops); // First, we have to build the index from byte locations to
  5629. // samples (that is, frames) in the video data
  5630. track.samples = frameUtils.generateSampleTable(gops); // Concatenate the video data and construct the mdat
  5631. mdat = mp4Generator.mdat(frameUtils.concatenateNalData(gops));
  5632. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
  5633. this.trigger('processedGopsInfo', gops.map(function (gop) {
  5634. return {
  5635. pts: gop.pts,
  5636. dts: gop.dts,
  5637. byteLength: gop.byteLength
  5638. };
  5639. }));
  5640. firstGop = gops[0];
  5641. lastGop = gops[gops.length - 1];
  5642. this.trigger('segmentTimingInfo', generateSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
  5643. this.trigger('timingInfo', {
  5644. start: gops[0].pts,
  5645. end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
  5646. }); // save all the nals in the last GOP into the gop cache
  5647. this.gopCache_.unshift({
  5648. gop: gops.pop(),
  5649. pps: track.pps,
  5650. sps: track.sps
  5651. }); // Keep a maximum of 6 GOPs in the cache
  5652. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  5653. nalUnits = [];
  5654. this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
  5655. this.trigger('timelineStartInfo', track.timelineStartInfo);
  5656. moof = mp4Generator.moof(sequenceNumber, [track]); // it would be great to allocate this array up front instead of
  5657. // throwing away hundreds of media segment fragments
  5658. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // Bump the sequence number for next time
  5659. sequenceNumber++;
  5660. boxes.set(moof);
  5661. boxes.set(mdat, moof.byteLength);
  5662. this.trigger('data', {
  5663. track: track,
  5664. boxes: boxes
  5665. });
  5666. this.resetStream_(); // Continue with the flush process now
  5667. this.trigger('done', 'VideoSegmentStream');
  5668. };
  5669. this.reset = function () {
  5670. this.resetStream_();
  5671. nalUnits = [];
  5672. this.gopCache_.length = 0;
  5673. gopsToAlignWith.length = 0;
  5674. this.trigger('reset');
  5675. };
  5676. this.resetStream_ = function () {
  5677. trackDecodeInfo.clearDtsInfo(track); // reset config and pps because they may differ across segments
  5678. // for instance, when we are rendition switching
  5679. config = undefined;
  5680. pps = undefined;
  5681. }; // Search for a candidate Gop for gop-fusion from the gop cache and
  5682. // return it or return null if no good candidate was found
  5683. this.getGopForFusion_ = function (nalUnit) {
  5684. var halfSecond = 45000,
  5685. // Half-a-second in a 90khz clock
  5686. allowableOverlap = 10000,
  5687. // About 3 frames @ 30fps
  5688. nearestDistance = Infinity,
  5689. dtsDistance,
  5690. nearestGopObj,
  5691. currentGop,
  5692. currentGopObj,
  5693. i; // Search for the GOP nearest to the beginning of this nal unit
  5694. for (i = 0; i < this.gopCache_.length; i++) {
  5695. currentGopObj = this.gopCache_[i];
  5696. currentGop = currentGopObj.gop; // Reject Gops with different SPS or PPS
  5697. if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
  5698. continue;
  5699. } // Reject Gops that would require a negative baseMediaDecodeTime
  5700. if (currentGop.dts < track.timelineStartInfo.dts) {
  5701. continue;
  5702. } // The distance between the end of the gop and the start of the nalUnit
  5703. dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration; // Only consider GOPS that start before the nal unit and end within
  5704. // a half-second of the nal unit
  5705. if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
  5706. // Always use the closest GOP we found if there is more than
  5707. // one candidate
  5708. if (!nearestGopObj || nearestDistance > dtsDistance) {
  5709. nearestGopObj = currentGopObj;
  5710. nearestDistance = dtsDistance;
  5711. }
  5712. }
  5713. }
  5714. if (nearestGopObj) {
  5715. return nearestGopObj.gop;
  5716. }
  5717. return null;
  5718. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  5719. // of gopsToAlignWith starting from the START of the list
  5720. this.alignGopsAtStart_ = function (gops) {
  5721. var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
  5722. byteLength = gops.byteLength;
  5723. nalCount = gops.nalCount;
  5724. duration = gops.duration;
  5725. alignIndex = gopIndex = 0;
  5726. while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
  5727. align = gopsToAlignWith[alignIndex];
  5728. gop = gops[gopIndex];
  5729. if (align.pts === gop.pts) {
  5730. break;
  5731. }
  5732. if (gop.pts > align.pts) {
  5733. // this current gop starts after the current gop we want to align on, so increment
  5734. // align index
  5735. alignIndex++;
  5736. continue;
  5737. } // current gop starts before the current gop we want to align on. so increment gop
  5738. // index
  5739. gopIndex++;
  5740. byteLength -= gop.byteLength;
  5741. nalCount -= gop.nalCount;
  5742. duration -= gop.duration;
  5743. }
  5744. if (gopIndex === 0) {
  5745. // no gops to trim
  5746. return gops;
  5747. }
  5748. if (gopIndex === gops.length) {
  5749. // all gops trimmed, skip appending all gops
  5750. return null;
  5751. }
  5752. alignedGops = gops.slice(gopIndex);
  5753. alignedGops.byteLength = byteLength;
  5754. alignedGops.duration = duration;
  5755. alignedGops.nalCount = nalCount;
  5756. alignedGops.pts = alignedGops[0].pts;
  5757. alignedGops.dts = alignedGops[0].dts;
  5758. return alignedGops;
  5759. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  5760. // of gopsToAlignWith starting from the END of the list
  5761. this.alignGopsAtEnd_ = function (gops) {
  5762. var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
  5763. alignIndex = gopsToAlignWith.length - 1;
  5764. gopIndex = gops.length - 1;
  5765. alignEndIndex = null;
  5766. matchFound = false;
  5767. while (alignIndex >= 0 && gopIndex >= 0) {
  5768. align = gopsToAlignWith[alignIndex];
  5769. gop = gops[gopIndex];
  5770. if (align.pts === gop.pts) {
  5771. matchFound = true;
  5772. break;
  5773. }
  5774. if (align.pts > gop.pts) {
  5775. alignIndex--;
  5776. continue;
  5777. }
  5778. if (alignIndex === gopsToAlignWith.length - 1) {
  5779. // gop.pts is greater than the last alignment candidate. If no match is found
  5780. // by the end of this loop, we still want to append gops that come after this
  5781. // point
  5782. alignEndIndex = gopIndex;
  5783. }
  5784. gopIndex--;
  5785. }
  5786. if (!matchFound && alignEndIndex === null) {
  5787. return null;
  5788. }
  5789. var trimIndex;
  5790. if (matchFound) {
  5791. trimIndex = gopIndex;
  5792. } else {
  5793. trimIndex = alignEndIndex;
  5794. }
  5795. if (trimIndex === 0) {
  5796. return gops;
  5797. }
  5798. var alignedGops = gops.slice(trimIndex);
  5799. var metadata = alignedGops.reduce(function (total, gop) {
  5800. total.byteLength += gop.byteLength;
  5801. total.duration += gop.duration;
  5802. total.nalCount += gop.nalCount;
  5803. return total;
  5804. }, {
  5805. byteLength: 0,
  5806. duration: 0,
  5807. nalCount: 0
  5808. });
  5809. alignedGops.byteLength = metadata.byteLength;
  5810. alignedGops.duration = metadata.duration;
  5811. alignedGops.nalCount = metadata.nalCount;
  5812. alignedGops.pts = alignedGops[0].pts;
  5813. alignedGops.dts = alignedGops[0].dts;
  5814. return alignedGops;
  5815. };
  5816. this.alignGopsWith = function (newGopsToAlignWith) {
  5817. gopsToAlignWith = newGopsToAlignWith;
  5818. };
  5819. };
  5820. _VideoSegmentStream.prototype = new stream();
  5821. /**
  5822. * A Stream that can combine multiple streams (ie. audio & video)
  5823. * into a single output segment for MSE. Also supports audio-only
  5824. * and video-only streams.
  5825. * @param options {object} transmuxer options object
  5826. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  5827. * in the source; false to adjust the first segment to start at media timeline start.
  5828. */
  5829. _CoalesceStream = function CoalesceStream(options, metadataStream) {
  5830. // Number of Tracks per output segment
  5831. // If greater than 1, we combine multiple
  5832. // tracks into a single segment
  5833. this.numberOfTracks = 0;
  5834. this.metadataStream = metadataStream;
  5835. options = options || {};
  5836. if (typeof options.remux !== 'undefined') {
  5837. this.remuxTracks = !!options.remux;
  5838. } else {
  5839. this.remuxTracks = true;
  5840. }
  5841. if (typeof options.keepOriginalTimestamps === 'boolean') {
  5842. this.keepOriginalTimestamps = options.keepOriginalTimestamps;
  5843. } else {
  5844. this.keepOriginalTimestamps = false;
  5845. }
  5846. this.pendingTracks = [];
  5847. this.videoTrack = null;
  5848. this.pendingBoxes = [];
  5849. this.pendingCaptions = [];
  5850. this.pendingMetadata = [];
  5851. this.pendingBytes = 0;
  5852. this.emittedTracks = 0;
  5853. _CoalesceStream.prototype.init.call(this); // Take output from multiple
  5854. this.push = function (output) {
  5855. // buffer incoming captions until the associated video segment
  5856. // finishes
  5857. if (output.content || output.text) {
  5858. return this.pendingCaptions.push(output);
  5859. } // buffer incoming id3 tags until the final flush
  5860. if (output.frames) {
  5861. return this.pendingMetadata.push(output);
  5862. } // Add this track to the list of pending tracks and store
  5863. // important information required for the construction of
  5864. // the final segment
  5865. this.pendingTracks.push(output.track);
  5866. this.pendingBytes += output.boxes.byteLength; // TODO: is there an issue for this against chrome?
  5867. // We unshift audio and push video because
  5868. // as of Chrome 75 when switching from
  5869. // one init segment to another if the video
  5870. // mdat does not appear after the audio mdat
  5871. // only audio will play for the duration of our transmux.
  5872. if (output.track.type === 'video') {
  5873. this.videoTrack = output.track;
  5874. this.pendingBoxes.push(output.boxes);
  5875. }
  5876. if (output.track.type === 'audio') {
  5877. this.audioTrack = output.track;
  5878. this.pendingBoxes.unshift(output.boxes);
  5879. }
  5880. };
  5881. };
  5882. _CoalesceStream.prototype = new stream();
  5883. _CoalesceStream.prototype.flush = function (flushSource) {
  5884. var offset = 0,
  5885. event = {
  5886. captions: [],
  5887. captionStreams: {},
  5888. metadata: [],
  5889. info: {}
  5890. },
  5891. caption,
  5892. id3,
  5893. initSegment,
  5894. timelineStartPts = 0,
  5895. i;
  5896. if (this.pendingTracks.length < this.numberOfTracks) {
  5897. if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
  5898. // Return because we haven't received a flush from a data-generating
  5899. // portion of the segment (meaning that we have only recieved meta-data
  5900. // or captions.)
  5901. return;
  5902. } else if (this.remuxTracks) {
  5903. // Return until we have enough tracks from the pipeline to remux (if we
  5904. // are remuxing audio and video into a single MP4)
  5905. return;
  5906. } else if (this.pendingTracks.length === 0) {
  5907. // In the case where we receive a flush without any data having been
  5908. // received we consider it an emitted track for the purposes of coalescing
  5909. // `done` events.
  5910. // We do this for the case where there is an audio and video track in the
  5911. // segment but no audio data. (seen in several playlists with alternate
  5912. // audio tracks and no audio present in the main TS segments.)
  5913. this.emittedTracks++;
  5914. if (this.emittedTracks >= this.numberOfTracks) {
  5915. this.trigger('done');
  5916. this.emittedTracks = 0;
  5917. }
  5918. return;
  5919. }
  5920. }
  5921. if (this.videoTrack) {
  5922. timelineStartPts = this.videoTrack.timelineStartInfo.pts;
  5923. videoProperties.forEach(function (prop) {
  5924. event.info[prop] = this.videoTrack[prop];
  5925. }, this);
  5926. } else if (this.audioTrack) {
  5927. timelineStartPts = this.audioTrack.timelineStartInfo.pts;
  5928. audioProperties.forEach(function (prop) {
  5929. event.info[prop] = this.audioTrack[prop];
  5930. }, this);
  5931. }
  5932. if (this.videoTrack || this.audioTrack) {
  5933. if (this.pendingTracks.length === 1) {
  5934. event.type = this.pendingTracks[0].type;
  5935. } else {
  5936. event.type = 'combined';
  5937. }
  5938. this.emittedTracks += this.pendingTracks.length;
  5939. initSegment = mp4Generator.initSegment(this.pendingTracks); // Create a new typed array to hold the init segment
  5940. event.initSegment = new Uint8Array(initSegment.byteLength); // Create an init segment containing a moov
  5941. // and track definitions
  5942. event.initSegment.set(initSegment); // Create a new typed array to hold the moof+mdats
  5943. event.data = new Uint8Array(this.pendingBytes); // Append each moof+mdat (one per track) together
  5944. for (i = 0; i < this.pendingBoxes.length; i++) {
  5945. event.data.set(this.pendingBoxes[i], offset);
  5946. offset += this.pendingBoxes[i].byteLength;
  5947. } // Translate caption PTS times into second offsets to match the
  5948. // video timeline for the segment, and add track info
  5949. for (i = 0; i < this.pendingCaptions.length; i++) {
  5950. caption = this.pendingCaptions[i];
  5951. caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
  5952. caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
  5953. event.captionStreams[caption.stream] = true;
  5954. event.captions.push(caption);
  5955. } // Translate ID3 frame PTS times into second offsets to match the
  5956. // video timeline for the segment
  5957. for (i = 0; i < this.pendingMetadata.length; i++) {
  5958. id3 = this.pendingMetadata[i];
  5959. id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
  5960. event.metadata.push(id3);
  5961. } // We add this to every single emitted segment even though we only need
  5962. // it for the first
  5963. event.metadata.dispatchType = this.metadataStream.dispatchType; // Reset stream state
  5964. this.pendingTracks.length = 0;
  5965. this.videoTrack = null;
  5966. this.pendingBoxes.length = 0;
  5967. this.pendingCaptions.length = 0;
  5968. this.pendingBytes = 0;
  5969. this.pendingMetadata.length = 0; // Emit the built segment
  5970. // We include captions and ID3 tags for backwards compatibility,
  5971. // ideally we should send only video and audio in the data event
  5972. this.trigger('data', event); // Emit each caption to the outside world
  5973. // Ideally, this would happen immediately on parsing captions,
  5974. // but we need to ensure that video data is sent back first
  5975. // so that caption timing can be adjusted to match video timing
  5976. for (i = 0; i < event.captions.length; i++) {
  5977. caption = event.captions[i];
  5978. this.trigger('caption', caption);
  5979. } // Emit each id3 tag to the outside world
  5980. // Ideally, this would happen immediately on parsing the tag,
  5981. // but we need to ensure that video data is sent back first
  5982. // so that ID3 frame timing can be adjusted to match video timing
  5983. for (i = 0; i < event.metadata.length; i++) {
  5984. id3 = event.metadata[i];
  5985. this.trigger('id3Frame', id3);
  5986. }
  5987. } // Only emit `done` if all tracks have been flushed and emitted
  5988. if (this.emittedTracks >= this.numberOfTracks) {
  5989. this.trigger('done');
  5990. this.emittedTracks = 0;
  5991. }
  5992. };
  5993. _CoalesceStream.prototype.setRemux = function (val) {
  5994. this.remuxTracks = val;
  5995. };
  5996. /**
  5997. * A Stream that expects MP2T binary data as input and produces
  5998. * corresponding media segments, suitable for use with Media Source
  5999. * Extension (MSE) implementations that support the ISO BMFF byte
  6000. * stream format, like Chrome.
  6001. */
  6002. _Transmuxer = function Transmuxer(options) {
  6003. var self = this,
  6004. hasFlushed = true,
  6005. videoTrack,
  6006. audioTrack;
  6007. _Transmuxer.prototype.init.call(this);
  6008. options = options || {};
  6009. this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
  6010. this.transmuxPipeline_ = {};
  6011. this.setupAacPipeline = function () {
  6012. var pipeline = {};
  6013. this.transmuxPipeline_ = pipeline;
  6014. pipeline.type = 'aac';
  6015. pipeline.metadataStream = new m2ts_1.MetadataStream(); // set up the parsing pipeline
  6016. pipeline.aacStream = new aac();
  6017. pipeline.audioTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('audio');
  6018. pipeline.timedMetadataTimestampRolloverStream = new m2ts_1.TimestampRolloverStream('timed-metadata');
  6019. pipeline.adtsStream = new adts();
  6020. pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
  6021. pipeline.headOfPipeline = pipeline.aacStream;
  6022. pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
  6023. pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
  6024. pipeline.metadataStream.on('timestamp', function (frame) {
  6025. pipeline.aacStream.setTimestamp(frame.timeStamp);
  6026. });
  6027. pipeline.aacStream.on('data', function (data) {
  6028. if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
  6029. return;
  6030. }
  6031. audioTrack = audioTrack || {
  6032. timelineStartInfo: {
  6033. baseMediaDecodeTime: self.baseMediaDecodeTime
  6034. },
  6035. codec: 'adts',
  6036. type: 'audio'
  6037. }; // hook up the audio segment stream to the first track with aac data
  6038. pipeline.coalesceStream.numberOfTracks++;
  6039. pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
  6040. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  6041. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo')); // Set up the final part of the audio pipeline
  6042. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream); // emit pmt info
  6043. self.trigger('trackinfo', {
  6044. hasAudio: !!audioTrack,
  6045. hasVideo: !!videoTrack
  6046. });
  6047. }); // Re-emit any data coming from the coalesce stream to the outside world
  6048. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data')); // Let the consumer know we have finished flushing the entire pipeline
  6049. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  6050. addPipelineLogRetriggers(this, pipeline);
  6051. };
  6052. this.setupTsPipeline = function () {
  6053. var pipeline = {};
  6054. this.transmuxPipeline_ = pipeline;
  6055. pipeline.type = 'ts';
  6056. pipeline.metadataStream = new m2ts_1.MetadataStream(); // set up the parsing pipeline
  6057. pipeline.packetStream = new m2ts_1.TransportPacketStream();
  6058. pipeline.parseStream = new m2ts_1.TransportParseStream();
  6059. pipeline.elementaryStream = new m2ts_1.ElementaryStream();
  6060. pipeline.timestampRolloverStream = new m2ts_1.TimestampRolloverStream();
  6061. pipeline.adtsStream = new adts();
  6062. pipeline.h264Stream = new H264Stream();
  6063. pipeline.captionStream = new m2ts_1.CaptionStream(options);
  6064. pipeline.coalesceStream = new _CoalesceStream(options, pipeline.metadataStream);
  6065. pipeline.headOfPipeline = pipeline.packetStream; // disassemble MPEG2-TS packets into elementary streams
  6066. pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream); // !!THIS ORDER IS IMPORTANT!!
  6067. // demux the streams
  6068. pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
  6069. pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
  6070. pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream); // Hook up CEA-608/708 caption stream
  6071. pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
  6072. pipeline.elementaryStream.on('data', function (data) {
  6073. var i;
  6074. if (data.type === 'metadata') {
  6075. i = data.tracks.length; // scan the tracks listed in the metadata
  6076. while (i--) {
  6077. if (!videoTrack && data.tracks[i].type === 'video') {
  6078. videoTrack = data.tracks[i];
  6079. videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  6080. } else if (!audioTrack && data.tracks[i].type === 'audio') {
  6081. audioTrack = data.tracks[i];
  6082. audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  6083. }
  6084. } // hook up the video segment stream to the first track with h264 data
  6085. if (videoTrack && !pipeline.videoSegmentStream) {
  6086. pipeline.coalesceStream.numberOfTracks++;
  6087. pipeline.videoSegmentStream = new _VideoSegmentStream(videoTrack, options);
  6088. pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));
  6089. pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
  6090. // When video emits timelineStartInfo data after a flush, we forward that
  6091. // info to the AudioSegmentStream, if it exists, because video timeline
  6092. // data takes precedence. Do not do this if keepOriginalTimestamps is set,
  6093. // because this is a particularly subtle form of timestamp alteration.
  6094. if (audioTrack && !options.keepOriginalTimestamps) {
  6095. audioTrack.timelineStartInfo = timelineStartInfo; // On the first segment we trim AAC frames that exist before the
  6096. // very earliest DTS we have seen in video because Chrome will
  6097. // interpret any video track with a baseMediaDecodeTime that is
  6098. // non-zero as a gap.
  6099. pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
  6100. }
  6101. });
  6102. pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
  6103. pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
  6104. pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
  6105. if (audioTrack) {
  6106. pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
  6107. }
  6108. });
  6109. pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo')); // Set up the final part of the video pipeline
  6110. pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
  6111. }
  6112. if (audioTrack && !pipeline.audioSegmentStream) {
  6113. // hook up the audio segment stream to the first track with aac data
  6114. pipeline.coalesceStream.numberOfTracks++;
  6115. pipeline.audioSegmentStream = new _AudioSegmentStream(audioTrack, options);
  6116. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  6117. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
  6118. pipeline.audioSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'audioSegmentTimingInfo')); // Set up the final part of the audio pipeline
  6119. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
  6120. } // emit pmt info
  6121. self.trigger('trackinfo', {
  6122. hasAudio: !!audioTrack,
  6123. hasVideo: !!videoTrack
  6124. });
  6125. }
  6126. }); // Re-emit any data coming from the coalesce stream to the outside world
  6127. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
  6128. pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
  6129. id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
  6130. self.trigger('id3Frame', id3Frame);
  6131. });
  6132. pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption')); // Let the consumer know we have finished flushing the entire pipeline
  6133. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  6134. addPipelineLogRetriggers(this, pipeline);
  6135. }; // hook up the segment streams once track metadata is delivered
  6136. this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  6137. var pipeline = this.transmuxPipeline_;
  6138. if (!options.keepOriginalTimestamps) {
  6139. this.baseMediaDecodeTime = baseMediaDecodeTime;
  6140. }
  6141. if (audioTrack) {
  6142. audioTrack.timelineStartInfo.dts = undefined;
  6143. audioTrack.timelineStartInfo.pts = undefined;
  6144. trackDecodeInfo.clearDtsInfo(audioTrack);
  6145. if (pipeline.audioTimestampRolloverStream) {
  6146. pipeline.audioTimestampRolloverStream.discontinuity();
  6147. }
  6148. }
  6149. if (videoTrack) {
  6150. if (pipeline.videoSegmentStream) {
  6151. pipeline.videoSegmentStream.gopCache_ = [];
  6152. }
  6153. videoTrack.timelineStartInfo.dts = undefined;
  6154. videoTrack.timelineStartInfo.pts = undefined;
  6155. trackDecodeInfo.clearDtsInfo(videoTrack);
  6156. pipeline.captionStream.reset();
  6157. }
  6158. if (pipeline.timestampRolloverStream) {
  6159. pipeline.timestampRolloverStream.discontinuity();
  6160. }
  6161. };
  6162. this.setAudioAppendStart = function (timestamp) {
  6163. if (audioTrack) {
  6164. this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
  6165. }
  6166. };
  6167. this.setRemux = function (val) {
  6168. var pipeline = this.transmuxPipeline_;
  6169. options.remux = val;
  6170. if (pipeline && pipeline.coalesceStream) {
  6171. pipeline.coalesceStream.setRemux(val);
  6172. }
  6173. };
  6174. this.alignGopsWith = function (gopsToAlignWith) {
  6175. if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
  6176. this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
  6177. }
  6178. };
  6179. this.getLogTrigger_ = function (key) {
  6180. var self = this;
  6181. return function (event) {
  6182. event.stream = key;
  6183. self.trigger('log', event);
  6184. };
  6185. }; // feed incoming data to the front of the parsing pipeline
  6186. this.push = function (data) {
  6187. if (hasFlushed) {
  6188. var isAac = isLikelyAacData(data);
  6189. if (isAac && this.transmuxPipeline_.type !== 'aac') {
  6190. this.setupAacPipeline();
  6191. } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
  6192. this.setupTsPipeline();
  6193. }
  6194. hasFlushed = false;
  6195. }
  6196. this.transmuxPipeline_.headOfPipeline.push(data);
  6197. }; // flush any buffered data
  6198. this.flush = function () {
  6199. hasFlushed = true; // Start at the top of the pipeline and flush all pending work
  6200. this.transmuxPipeline_.headOfPipeline.flush();
  6201. };
  6202. this.endTimeline = function () {
  6203. this.transmuxPipeline_.headOfPipeline.endTimeline();
  6204. };
  6205. this.reset = function () {
  6206. if (this.transmuxPipeline_.headOfPipeline) {
  6207. this.transmuxPipeline_.headOfPipeline.reset();
  6208. }
  6209. }; // Caption data has to be reset when seeking outside buffered range
  6210. this.resetCaptions = function () {
  6211. if (this.transmuxPipeline_.captionStream) {
  6212. this.transmuxPipeline_.captionStream.reset();
  6213. }
  6214. };
  6215. };
  6216. _Transmuxer.prototype = new stream();
  6217. var transmuxer = {
  6218. Transmuxer: _Transmuxer,
  6219. VideoSegmentStream: _VideoSegmentStream,
  6220. AudioSegmentStream: _AudioSegmentStream,
  6221. AUDIO_PROPERTIES: audioProperties,
  6222. VIDEO_PROPERTIES: videoProperties,
  6223. // exported for testing
  6224. generateSegmentTimingInfo: generateSegmentTimingInfo
  6225. };
  6226. var discardEmulationPreventionBytes = captionPacketParser.discardEmulationPreventionBytes;
  6227. var CaptionStream = captionStream.CaptionStream;
  6228. /**
  6229. * Maps an offset in the mdat to a sample based on the the size of the samples.
  6230. * Assumes that `parseSamples` has been called first.
  6231. *
  6232. * @param {Number} offset - The offset into the mdat
  6233. * @param {Object[]} samples - An array of samples, parsed using `parseSamples`
  6234. * @return {?Object} The matching sample, or null if no match was found.
  6235. *
  6236. * @see ISO-BMFF-12/2015, Section 8.8.8
  6237. **/
  6238. var mapToSample = function mapToSample(offset, samples) {
  6239. var approximateOffset = offset;
  6240. for (var i = 0; i < samples.length; i++) {
  6241. var sample = samples[i];
  6242. if (approximateOffset < sample.size) {
  6243. return sample;
  6244. }
  6245. approximateOffset -= sample.size;
  6246. }
  6247. return null;
  6248. };
  6249. /**
  6250. * Finds SEI nal units contained in a Media Data Box.
  6251. * Assumes that `parseSamples` has been called first.
  6252. *
  6253. * @param {Uint8Array} avcStream - The bytes of the mdat
  6254. * @param {Object[]} samples - The samples parsed out by `parseSamples`
  6255. * @param {Number} trackId - The trackId of this video track
  6256. * @return {Object[]} seiNals - the parsed SEI NALUs found.
  6257. * The contents of the seiNal should match what is expected by
  6258. * CaptionStream.push (nalUnitType, size, data, escapedRBSP, pts, dts)
  6259. *
  6260. * @see ISO-BMFF-12/2015, Section 8.1.1
  6261. * @see Rec. ITU-T H.264, 7.3.2.3.1
  6262. **/
  6263. var findSeiNals = function findSeiNals(avcStream, samples, trackId) {
  6264. var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
  6265. result = {
  6266. logs: [],
  6267. seiNals: []
  6268. },
  6269. seiNal,
  6270. i,
  6271. length,
  6272. lastMatchedSample;
  6273. for (i = 0; i + 4 < avcStream.length; i += length) {
  6274. length = avcView.getUint32(i);
  6275. i += 4; // Bail if this doesn't appear to be an H264 stream
  6276. if (length <= 0) {
  6277. continue;
  6278. }
  6279. switch (avcStream[i] & 0x1F) {
  6280. case 0x06:
  6281. var data = avcStream.subarray(i + 1, i + 1 + length);
  6282. var matchingSample = mapToSample(i, samples);
  6283. seiNal = {
  6284. nalUnitType: 'sei_rbsp',
  6285. size: length,
  6286. data: data,
  6287. escapedRBSP: discardEmulationPreventionBytes(data),
  6288. trackId: trackId
  6289. };
  6290. if (matchingSample) {
  6291. seiNal.pts = matchingSample.pts;
  6292. seiNal.dts = matchingSample.dts;
  6293. lastMatchedSample = matchingSample;
  6294. } else if (lastMatchedSample) {
  6295. // If a matching sample cannot be found, use the last
  6296. // sample's values as they should be as close as possible
  6297. seiNal.pts = lastMatchedSample.pts;
  6298. seiNal.dts = lastMatchedSample.dts;
  6299. } else {
  6300. result.logs.push({
  6301. level: 'warn',
  6302. message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
  6303. });
  6304. break;
  6305. }
  6306. result.seiNals.push(seiNal);
  6307. break;
  6308. }
  6309. }
  6310. return result;
  6311. };
  6312. /**
  6313. * Parses sample information out of Track Run Boxes and calculates
  6314. * the absolute presentation and decode timestamps of each sample.
  6315. *
  6316. * @param {Array<Uint8Array>} truns - The Trun Run boxes to be parsed
  6317. * @param {Number|BigInt} baseMediaDecodeTime - base media decode time from tfdt
  6318. @see ISO-BMFF-12/2015, Section 8.8.12
  6319. * @param {Object} tfhd - The parsed Track Fragment Header
  6320. * @see inspect.parseTfhd
  6321. * @return {Object[]} the parsed samples
  6322. *
  6323. * @see ISO-BMFF-12/2015, Section 8.8.8
  6324. **/
  6325. var parseSamples = function parseSamples(truns, baseMediaDecodeTime, tfhd) {
  6326. var currentDts = baseMediaDecodeTime;
  6327. var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
  6328. var defaultSampleSize = tfhd.defaultSampleSize || 0;
  6329. var trackId = tfhd.trackId;
  6330. var allSamples = [];
  6331. truns.forEach(function (trun) {
  6332. // Note: We currently do not parse the sample table as well
  6333. // as the trun. It's possible some sources will require this.
  6334. // moov > trak > mdia > minf > stbl
  6335. var trackRun = parseTrun(trun);
  6336. var samples = trackRun.samples;
  6337. samples.forEach(function (sample) {
  6338. if (sample.duration === undefined) {
  6339. sample.duration = defaultSampleDuration;
  6340. }
  6341. if (sample.size === undefined) {
  6342. sample.size = defaultSampleSize;
  6343. }
  6344. sample.trackId = trackId;
  6345. sample.dts = currentDts;
  6346. if (sample.compositionTimeOffset === undefined) {
  6347. sample.compositionTimeOffset = 0;
  6348. }
  6349. if (typeof currentDts === 'bigint') {
  6350. sample.pts = currentDts + window__default['default'].BigInt(sample.compositionTimeOffset);
  6351. currentDts += window__default['default'].BigInt(sample.duration);
  6352. } else {
  6353. sample.pts = currentDts + sample.compositionTimeOffset;
  6354. currentDts += sample.duration;
  6355. }
  6356. });
  6357. allSamples = allSamples.concat(samples);
  6358. });
  6359. return allSamples;
  6360. };
  6361. /**
  6362. * Parses out caption nals from an FMP4 segment's video tracks.
  6363. *
  6364. * @param {Uint8Array} segment - The bytes of a single segment
  6365. * @param {Number} videoTrackId - The trackId of a video track in the segment
  6366. * @return {Object.<Number, Object[]>} A mapping of video trackId to
  6367. * a list of seiNals found in that track
  6368. **/
  6369. var parseCaptionNals = function parseCaptionNals(segment, videoTrackId) {
  6370. // To get the samples
  6371. var trafs = findBox_1(segment, ['moof', 'traf']); // To get SEI NAL units
  6372. var mdats = findBox_1(segment, ['mdat']);
  6373. var captionNals = {};
  6374. var mdatTrafPairs = []; // Pair up each traf with a mdat as moofs and mdats are in pairs
  6375. mdats.forEach(function (mdat, index) {
  6376. var matchingTraf = trafs[index];
  6377. mdatTrafPairs.push({
  6378. mdat: mdat,
  6379. traf: matchingTraf
  6380. });
  6381. });
  6382. mdatTrafPairs.forEach(function (pair) {
  6383. var mdat = pair.mdat;
  6384. var traf = pair.traf;
  6385. var tfhd = findBox_1(traf, ['tfhd']); // Exactly 1 tfhd per traf
  6386. var headerInfo = parseTfhd(tfhd[0]);
  6387. var trackId = headerInfo.trackId;
  6388. var tfdt = findBox_1(traf, ['tfdt']); // Either 0 or 1 tfdt per traf
  6389. var baseMediaDecodeTime = tfdt.length > 0 ? parseTfdt(tfdt[0]).baseMediaDecodeTime : 0;
  6390. var truns = findBox_1(traf, ['trun']);
  6391. var samples;
  6392. var result; // Only parse video data for the chosen video track
  6393. if (videoTrackId === trackId && truns.length > 0) {
  6394. samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
  6395. result = findSeiNals(mdat, samples, trackId);
  6396. if (!captionNals[trackId]) {
  6397. captionNals[trackId] = {
  6398. seiNals: [],
  6399. logs: []
  6400. };
  6401. }
  6402. captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
  6403. captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
  6404. }
  6405. });
  6406. return captionNals;
  6407. };
  6408. /**
  6409. * Parses out inband captions from an MP4 container and returns
  6410. * caption objects that can be used by WebVTT and the TextTrack API.
  6411. * @see https://developer.mozilla.org/en-US/docs/Web/API/VTTCue
  6412. * @see https://developer.mozilla.org/en-US/docs/Web/API/TextTrack
  6413. * Assumes that `probe.getVideoTrackIds` and `probe.timescale` have been called first
  6414. *
  6415. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  6416. * @param {Number} trackId - The id of the video track to parse
  6417. * @param {Number} timescale - The timescale for the video track from the init segment
  6418. *
  6419. * @return {?Object[]} parsedCaptions - A list of captions or null if no video tracks
  6420. * @return {Number} parsedCaptions[].startTime - The time to show the caption in seconds
  6421. * @return {Number} parsedCaptions[].endTime - The time to stop showing the caption in seconds
  6422. * @return {Object[]} parsedCaptions[].content - A list of individual caption segments
  6423. * @return {String} parsedCaptions[].content.text - The visible content of the caption segment
  6424. * @return {Number} parsedCaptions[].content.line - The line height from 1-15 for positioning of the caption segment
  6425. * @return {Number} parsedCaptions[].content.position - The column indent percentage for cue positioning from 10-80
  6426. **/
  6427. var parseEmbeddedCaptions = function parseEmbeddedCaptions(segment, trackId, timescale) {
  6428. var captionNals; // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  6429. if (trackId === null) {
  6430. return null;
  6431. }
  6432. captionNals = parseCaptionNals(segment, trackId);
  6433. var trackNals = captionNals[trackId] || {};
  6434. return {
  6435. seiNals: trackNals.seiNals,
  6436. logs: trackNals.logs,
  6437. timescale: timescale
  6438. };
  6439. };
  6440. /**
  6441. * Converts SEI NALUs into captions that can be used by video.js
  6442. **/
  6443. var CaptionParser = function CaptionParser() {
  6444. var isInitialized = false;
  6445. var captionStream; // Stores segments seen before trackId and timescale are set
  6446. var segmentCache; // Stores video track ID of the track being parsed
  6447. var trackId; // Stores the timescale of the track being parsed
  6448. var timescale; // Stores captions parsed so far
  6449. var parsedCaptions; // Stores whether we are receiving partial data or not
  6450. var parsingPartial;
  6451. /**
  6452. * A method to indicate whether a CaptionParser has been initalized
  6453. * @returns {Boolean}
  6454. **/
  6455. this.isInitialized = function () {
  6456. return isInitialized;
  6457. };
  6458. /**
  6459. * Initializes the underlying CaptionStream, SEI NAL parsing
  6460. * and management, and caption collection
  6461. **/
  6462. this.init = function (options) {
  6463. captionStream = new CaptionStream();
  6464. isInitialized = true;
  6465. parsingPartial = options ? options.isPartial : false; // Collect dispatched captions
  6466. captionStream.on('data', function (event) {
  6467. // Convert to seconds in the source's timescale
  6468. event.startTime = event.startPts / timescale;
  6469. event.endTime = event.endPts / timescale;
  6470. parsedCaptions.captions.push(event);
  6471. parsedCaptions.captionStreams[event.stream] = true;
  6472. });
  6473. captionStream.on('log', function (log) {
  6474. parsedCaptions.logs.push(log);
  6475. });
  6476. };
  6477. /**
  6478. * Determines if a new video track will be selected
  6479. * or if the timescale changed
  6480. * @return {Boolean}
  6481. **/
  6482. this.isNewInit = function (videoTrackIds, timescales) {
  6483. if (videoTrackIds && videoTrackIds.length === 0 || timescales && typeof timescales === 'object' && Object.keys(timescales).length === 0) {
  6484. return false;
  6485. }
  6486. return trackId !== videoTrackIds[0] || timescale !== timescales[trackId];
  6487. };
  6488. /**
  6489. * Parses out SEI captions and interacts with underlying
  6490. * CaptionStream to return dispatched captions
  6491. *
  6492. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  6493. * @param {Number[]} videoTrackIds - A list of video tracks found in the init segment
  6494. * @param {Object.<Number, Number>} timescales - The timescales found in the init segment
  6495. * @see parseEmbeddedCaptions
  6496. * @see m2ts/caption-stream.js
  6497. **/
  6498. this.parse = function (segment, videoTrackIds, timescales) {
  6499. var parsedData;
  6500. if (!this.isInitialized()) {
  6501. return null; // This is not likely to be a video segment
  6502. } else if (!videoTrackIds || !timescales) {
  6503. return null;
  6504. } else if (this.isNewInit(videoTrackIds, timescales)) {
  6505. // Use the first video track only as there is no
  6506. // mechanism to switch to other video tracks
  6507. trackId = videoTrackIds[0];
  6508. timescale = timescales[trackId]; // If an init segment has not been seen yet, hold onto segment
  6509. // data until we have one.
  6510. // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  6511. } else if (trackId === null || !timescale) {
  6512. segmentCache.push(segment);
  6513. return null;
  6514. } // Now that a timescale and trackId is set, parse cached segments
  6515. while (segmentCache.length > 0) {
  6516. var cachedSegment = segmentCache.shift();
  6517. this.parse(cachedSegment, videoTrackIds, timescales);
  6518. }
  6519. parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
  6520. if (parsedData && parsedData.logs) {
  6521. parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
  6522. }
  6523. if (parsedData === null || !parsedData.seiNals) {
  6524. if (parsedCaptions.logs.length) {
  6525. return {
  6526. logs: parsedCaptions.logs,
  6527. captions: [],
  6528. captionStreams: []
  6529. };
  6530. }
  6531. return null;
  6532. }
  6533. this.pushNals(parsedData.seiNals); // Force the parsed captions to be dispatched
  6534. this.flushStream();
  6535. return parsedCaptions;
  6536. };
  6537. /**
  6538. * Pushes SEI NALUs onto CaptionStream
  6539. * @param {Object[]} nals - A list of SEI nals parsed using `parseCaptionNals`
  6540. * Assumes that `parseCaptionNals` has been called first
  6541. * @see m2ts/caption-stream.js
  6542. **/
  6543. this.pushNals = function (nals) {
  6544. if (!this.isInitialized() || !nals || nals.length === 0) {
  6545. return null;
  6546. }
  6547. nals.forEach(function (nal) {
  6548. captionStream.push(nal);
  6549. });
  6550. };
  6551. /**
  6552. * Flushes underlying CaptionStream to dispatch processed, displayable captions
  6553. * @see m2ts/caption-stream.js
  6554. **/
  6555. this.flushStream = function () {
  6556. if (!this.isInitialized()) {
  6557. return null;
  6558. }
  6559. if (!parsingPartial) {
  6560. captionStream.flush();
  6561. } else {
  6562. captionStream.partialFlush();
  6563. }
  6564. };
  6565. /**
  6566. * Reset caption buckets for new data
  6567. **/
  6568. this.clearParsedCaptions = function () {
  6569. parsedCaptions.captions = [];
  6570. parsedCaptions.captionStreams = {};
  6571. parsedCaptions.logs = [];
  6572. };
  6573. /**
  6574. * Resets underlying CaptionStream
  6575. * @see m2ts/caption-stream.js
  6576. **/
  6577. this.resetCaptionStream = function () {
  6578. if (!this.isInitialized()) {
  6579. return null;
  6580. }
  6581. captionStream.reset();
  6582. };
  6583. /**
  6584. * Convenience method to clear all captions flushed from the
  6585. * CaptionStream and still being parsed
  6586. * @see m2ts/caption-stream.js
  6587. **/
  6588. this.clearAllCaptions = function () {
  6589. this.clearParsedCaptions();
  6590. this.resetCaptionStream();
  6591. };
  6592. /**
  6593. * Reset caption parser
  6594. **/
  6595. this.reset = function () {
  6596. segmentCache = [];
  6597. trackId = null;
  6598. timescale = null;
  6599. if (!parsedCaptions) {
  6600. parsedCaptions = {
  6601. captions: [],
  6602. // CC1, CC2, CC3, CC4
  6603. captionStreams: {},
  6604. logs: []
  6605. };
  6606. } else {
  6607. this.clearParsedCaptions();
  6608. }
  6609. this.resetCaptionStream();
  6610. };
  6611. this.reset();
  6612. };
  6613. var captionParser = CaptionParser;
  6614. /**
  6615. * mux.js
  6616. *
  6617. * Copyright (c) Brightcove
  6618. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6619. */
  6620. var mp4 = {
  6621. generator: mp4Generator,
  6622. probe: probe,
  6623. Transmuxer: transmuxer.Transmuxer,
  6624. AudioSegmentStream: transmuxer.AudioSegmentStream,
  6625. VideoSegmentStream: transmuxer.VideoSegmentStream,
  6626. CaptionParser: captionParser
  6627. };
  6628. return mp4;
  6629. })));