videojs-http-streaming.cjs.js 909 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464
  1. /*! @name @videojs/http-streaming @version 3.10.0 @license Apache-2.0 */
  2. 'use strict';
  3. Object.defineProperty(exports, '__esModule', { value: true });
  4. var document = require('global/document');
  5. var window$1 = require('global/window');
  6. var _extends = require('@babel/runtime/helpers/extends');
  7. var _resolveUrl = require('@videojs/vhs-utils/cjs/resolve-url.js');
  8. var videojs = require('video.js');
  9. var m3u8Parser = require('m3u8-parser');
  10. var codecs_js = require('@videojs/vhs-utils/cjs/codecs.js');
  11. var mediaTypes_js = require('@videojs/vhs-utils/cjs/media-types.js');
  12. var byteHelpers = require('@videojs/vhs-utils/cjs/byte-helpers');
  13. var mpdParser = require('mpd-parser');
  14. var parseSidx = require('mux.js/lib/tools/parse-sidx');
  15. var id3Helpers = require('@videojs/vhs-utils/cjs/id3-helpers');
  16. var containers = require('@videojs/vhs-utils/cjs/containers');
  17. var clock = require('mux.js/lib/utils/clock');
  18. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  19. var document__default = /*#__PURE__*/_interopDefaultLegacy(document);
  20. var window__default = /*#__PURE__*/_interopDefaultLegacy(window$1);
  21. var _extends__default = /*#__PURE__*/_interopDefaultLegacy(_extends);
  22. var _resolveUrl__default = /*#__PURE__*/_interopDefaultLegacy(_resolveUrl);
  23. var videojs__default = /*#__PURE__*/_interopDefaultLegacy(videojs);
  24. var parseSidx__default = /*#__PURE__*/_interopDefaultLegacy(parseSidx);
  25. /**
  26. * @file resolve-url.js - Handling how URLs are resolved and manipulated
  27. */
  28. const resolveUrl = _resolveUrl__default["default"];
  29. /**
  30. * If the xhr request was redirected, return the responseURL, otherwise,
  31. * return the original url.
  32. *
  33. * @api private
  34. *
  35. * @param {string} url - an url being requested
  36. * @param {XMLHttpRequest} req - xhr request result
  37. *
  38. * @return {string}
  39. */
  40. const resolveManifestRedirect = (url, req) => {
  41. // To understand how the responseURL below is set and generated:
  42. // - https://fetch.spec.whatwg.org/#concept-response-url
  43. // - https://fetch.spec.whatwg.org/#atomic-http-redirect-handling
  44. if (req && req.responseURL && url !== req.responseURL) {
  45. return req.responseURL;
  46. }
  47. return url;
  48. };
  49. const logger = source => {
  50. if (videojs__default["default"].log.debug) {
  51. return videojs__default["default"].log.debug.bind(videojs__default["default"], 'VHS:', `${source} >`);
  52. }
  53. return function () {};
  54. };
  55. /**
  56. * Provides a compatibility layer between Video.js 7 and 8 API changes for VHS.
  57. */
  58. /**
  59. * Delegates to videojs.obj.merge (Video.js 8) or
  60. * videojs.mergeOptions (Video.js 7).
  61. */
  62. function merge(...args) {
  63. const context = videojs__default["default"].obj || videojs__default["default"];
  64. const fn = context.merge || context.mergeOptions;
  65. return fn.apply(context, args);
  66. }
  67. /**
  68. * Delegates to videojs.time.createTimeRanges (Video.js 8) or
  69. * videojs.createTimeRanges (Video.js 7).
  70. */
  71. function createTimeRanges(...args) {
  72. const context = videojs__default["default"].time || videojs__default["default"];
  73. const fn = context.createTimeRanges || context.createTimeRanges;
  74. return fn.apply(context, args);
  75. }
  76. /**
  77. * ranges
  78. *
  79. * Utilities for working with TimeRanges.
  80. *
  81. */
  82. const TIME_FUDGE_FACTOR = 1 / 30; // Comparisons between time values such as current time and the end of the buffered range
  83. // can be misleading because of precision differences or when the current media has poorly
  84. // aligned audio and video, which can cause values to be slightly off from what you would
  85. // expect. This value is what we consider to be safe to use in such comparisons to account
  86. // for these scenarios.
  87. const SAFE_TIME_DELTA = TIME_FUDGE_FACTOR * 3;
  88. const filterRanges = function (timeRanges, predicate) {
  89. const results = [];
  90. let i;
  91. if (timeRanges && timeRanges.length) {
  92. // Search for ranges that match the predicate
  93. for (i = 0; i < timeRanges.length; i++) {
  94. if (predicate(timeRanges.start(i), timeRanges.end(i))) {
  95. results.push([timeRanges.start(i), timeRanges.end(i)]);
  96. }
  97. }
  98. }
  99. return createTimeRanges(results);
  100. };
  101. /**
  102. * Attempts to find the buffered TimeRange that contains the specified
  103. * time.
  104. *
  105. * @param {TimeRanges} buffered - the TimeRanges object to query
  106. * @param {number} time - the time to filter on.
  107. * @return {TimeRanges} a new TimeRanges object
  108. */
  109. const findRange = function (buffered, time) {
  110. return filterRanges(buffered, function (start, end) {
  111. return start - SAFE_TIME_DELTA <= time && end + SAFE_TIME_DELTA >= time;
  112. });
  113. };
  114. /**
  115. * Returns the TimeRanges that begin later than the specified time.
  116. *
  117. * @param {TimeRanges} timeRanges - the TimeRanges object to query
  118. * @param {number} time - the time to filter on.
  119. * @return {TimeRanges} a new TimeRanges object.
  120. */
  121. const findNextRange = function (timeRanges, time) {
  122. return filterRanges(timeRanges, function (start) {
  123. return start - TIME_FUDGE_FACTOR >= time;
  124. });
  125. };
  126. /**
  127. * Returns gaps within a list of TimeRanges
  128. *
  129. * @param {TimeRanges} buffered - the TimeRanges object
  130. * @return {TimeRanges} a TimeRanges object of gaps
  131. */
  132. const findGaps = function (buffered) {
  133. if (buffered.length < 2) {
  134. return createTimeRanges();
  135. }
  136. const ranges = [];
  137. for (let i = 1; i < buffered.length; i++) {
  138. const start = buffered.end(i - 1);
  139. const end = buffered.start(i);
  140. ranges.push([start, end]);
  141. }
  142. return createTimeRanges(ranges);
  143. };
  144. /**
  145. * Calculate the intersection of two TimeRanges
  146. *
  147. * @param {TimeRanges} bufferA
  148. * @param {TimeRanges} bufferB
  149. * @return {TimeRanges} The interesection of `bufferA` with `bufferB`
  150. */
  151. const bufferIntersection = function (bufferA, bufferB) {
  152. let start = null;
  153. let end = null;
  154. let arity = 0;
  155. const extents = [];
  156. const ranges = [];
  157. if (!bufferA || !bufferA.length || !bufferB || !bufferB.length) {
  158. return createTimeRanges();
  159. } // Handle the case where we have both buffers and create an
  160. // intersection of the two
  161. let count = bufferA.length; // A) Gather up all start and end times
  162. while (count--) {
  163. extents.push({
  164. time: bufferA.start(count),
  165. type: 'start'
  166. });
  167. extents.push({
  168. time: bufferA.end(count),
  169. type: 'end'
  170. });
  171. }
  172. count = bufferB.length;
  173. while (count--) {
  174. extents.push({
  175. time: bufferB.start(count),
  176. type: 'start'
  177. });
  178. extents.push({
  179. time: bufferB.end(count),
  180. type: 'end'
  181. });
  182. } // B) Sort them by time
  183. extents.sort(function (a, b) {
  184. return a.time - b.time;
  185. }); // C) Go along one by one incrementing arity for start and decrementing
  186. // arity for ends
  187. for (count = 0; count < extents.length; count++) {
  188. if (extents[count].type === 'start') {
  189. arity++; // D) If arity is ever incremented to 2 we are entering an
  190. // overlapping range
  191. if (arity === 2) {
  192. start = extents[count].time;
  193. }
  194. } else if (extents[count].type === 'end') {
  195. arity--; // E) If arity is ever decremented to 1 we leaving an
  196. // overlapping range
  197. if (arity === 1) {
  198. end = extents[count].time;
  199. }
  200. } // F) Record overlapping ranges
  201. if (start !== null && end !== null) {
  202. ranges.push([start, end]);
  203. start = null;
  204. end = null;
  205. }
  206. }
  207. return createTimeRanges(ranges);
  208. };
  209. /**
  210. * Gets a human readable string for a TimeRange
  211. *
  212. * @param {TimeRange} range
  213. * @return {string} a human readable string
  214. */
  215. const printableRange = range => {
  216. const strArr = [];
  217. if (!range || !range.length) {
  218. return '';
  219. }
  220. for (let i = 0; i < range.length; i++) {
  221. strArr.push(range.start(i) + ' => ' + range.end(i));
  222. }
  223. return strArr.join(', ');
  224. };
  225. /**
  226. * Calculates the amount of time left in seconds until the player hits the end of the
  227. * buffer and causes a rebuffer
  228. *
  229. * @param {TimeRange} buffered
  230. * The state of the buffer
  231. * @param {Numnber} currentTime
  232. * The current time of the player
  233. * @param {number} playbackRate
  234. * The current playback rate of the player. Defaults to 1.
  235. * @return {number}
  236. * Time until the player has to start rebuffering in seconds.
  237. * @function timeUntilRebuffer
  238. */
  239. const timeUntilRebuffer = function (buffered, currentTime, playbackRate = 1) {
  240. const bufferedEnd = buffered.length ? buffered.end(buffered.length - 1) : 0;
  241. return (bufferedEnd - currentTime) / playbackRate;
  242. };
  243. /**
  244. * Converts a TimeRanges object into an array representation
  245. *
  246. * @param {TimeRanges} timeRanges
  247. * @return {Array}
  248. */
  249. const timeRangesToArray = timeRanges => {
  250. const timeRangesList = [];
  251. for (let i = 0; i < timeRanges.length; i++) {
  252. timeRangesList.push({
  253. start: timeRanges.start(i),
  254. end: timeRanges.end(i)
  255. });
  256. }
  257. return timeRangesList;
  258. };
  259. /**
  260. * Determines if two time range objects are different.
  261. *
  262. * @param {TimeRange} a
  263. * the first time range object to check
  264. *
  265. * @param {TimeRange} b
  266. * the second time range object to check
  267. *
  268. * @return {Boolean}
  269. * Whether the time range objects differ
  270. */
  271. const isRangeDifferent = function (a, b) {
  272. // same object
  273. if (a === b) {
  274. return false;
  275. } // one or the other is undefined
  276. if (!a && b || !b && a) {
  277. return true;
  278. } // length is different
  279. if (a.length !== b.length) {
  280. return true;
  281. } // see if any start/end pair is different
  282. for (let i = 0; i < a.length; i++) {
  283. if (a.start(i) !== b.start(i) || a.end(i) !== b.end(i)) {
  284. return true;
  285. }
  286. } // if the length and every pair is the same
  287. // this is the same time range
  288. return false;
  289. };
  290. const lastBufferedEnd = function (a) {
  291. if (!a || !a.length || !a.end) {
  292. return;
  293. }
  294. return a.end(a.length - 1);
  295. };
  296. /**
  297. * A utility function to add up the amount of time in a timeRange
  298. * after a specified startTime.
  299. * ie:[[0, 10], [20, 40], [50, 60]] with a startTime 0
  300. * would return 40 as there are 40s seconds after 0 in the timeRange
  301. *
  302. * @param {TimeRange} range
  303. * The range to check against
  304. * @param {number} startTime
  305. * The time in the time range that you should start counting from
  306. *
  307. * @return {number}
  308. * The number of seconds in the buffer passed the specified time.
  309. */
  310. const timeAheadOf = function (range, startTime) {
  311. let time = 0;
  312. if (!range || !range.length) {
  313. return time;
  314. }
  315. for (let i = 0; i < range.length; i++) {
  316. const start = range.start(i);
  317. const end = range.end(i); // startTime is after this range entirely
  318. if (startTime > end) {
  319. continue;
  320. } // startTime is within this range
  321. if (startTime > start && startTime <= end) {
  322. time += end - startTime;
  323. continue;
  324. } // startTime is before this range.
  325. time += end - start;
  326. }
  327. return time;
  328. };
  329. /**
  330. * @file playlist.js
  331. *
  332. * Playlist related utilities.
  333. */
  334. /**
  335. * Get the duration of a segment, with special cases for
  336. * llhls segments that do not have a duration yet.
  337. *
  338. * @param {Object} playlist
  339. * the playlist that the segment belongs to.
  340. * @param {Object} segment
  341. * the segment to get a duration for.
  342. *
  343. * @return {number}
  344. * the segment duration
  345. */
  346. const segmentDurationWithParts = (playlist, segment) => {
  347. // if this isn't a preload segment
  348. // then we will have a segment duration that is accurate.
  349. if (!segment.preload) {
  350. return segment.duration;
  351. } // otherwise we have to add up parts and preload hints
  352. // to get an up to date duration.
  353. let result = 0;
  354. (segment.parts || []).forEach(function (p) {
  355. result += p.duration;
  356. }); // for preload hints we have to use partTargetDuration
  357. // as they won't even have a duration yet.
  358. (segment.preloadHints || []).forEach(function (p) {
  359. if (p.type === 'PART') {
  360. result += playlist.partTargetDuration;
  361. }
  362. });
  363. return result;
  364. };
  365. /**
  366. * A function to get a combined list of parts and segments with durations
  367. * and indexes.
  368. *
  369. * @param {Playlist} playlist the playlist to get the list for.
  370. *
  371. * @return {Array} The part/segment list.
  372. */
  373. const getPartsAndSegments = playlist => (playlist.segments || []).reduce((acc, segment, si) => {
  374. if (segment.parts) {
  375. segment.parts.forEach(function (part, pi) {
  376. acc.push({
  377. duration: part.duration,
  378. segmentIndex: si,
  379. partIndex: pi,
  380. part,
  381. segment
  382. });
  383. });
  384. } else {
  385. acc.push({
  386. duration: segment.duration,
  387. segmentIndex: si,
  388. partIndex: null,
  389. segment,
  390. part: null
  391. });
  392. }
  393. return acc;
  394. }, []);
  395. const getLastParts = media => {
  396. const lastSegment = media.segments && media.segments.length && media.segments[media.segments.length - 1];
  397. return lastSegment && lastSegment.parts || [];
  398. };
  399. const getKnownPartCount = ({
  400. preloadSegment
  401. }) => {
  402. if (!preloadSegment) {
  403. return;
  404. }
  405. const {
  406. parts,
  407. preloadHints
  408. } = preloadSegment;
  409. let partCount = (preloadHints || []).reduce((count, hint) => count + (hint.type === 'PART' ? 1 : 0), 0);
  410. partCount += parts && parts.length ? parts.length : 0;
  411. return partCount;
  412. };
  413. /**
  414. * Get the number of seconds to delay from the end of a
  415. * live playlist.
  416. *
  417. * @param {Playlist} main the main playlist
  418. * @param {Playlist} media the media playlist
  419. * @return {number} the hold back in seconds.
  420. */
  421. const liveEdgeDelay = (main, media) => {
  422. if (media.endList) {
  423. return 0;
  424. } // dash suggestedPresentationDelay trumps everything
  425. if (main && main.suggestedPresentationDelay) {
  426. return main.suggestedPresentationDelay;
  427. }
  428. const hasParts = getLastParts(media).length > 0; // look for "part" delays from ll-hls first
  429. if (hasParts && media.serverControl && media.serverControl.partHoldBack) {
  430. return media.serverControl.partHoldBack;
  431. } else if (hasParts && media.partTargetDuration) {
  432. return media.partTargetDuration * 3; // finally look for full segment delays
  433. } else if (media.serverControl && media.serverControl.holdBack) {
  434. return media.serverControl.holdBack;
  435. } else if (media.targetDuration) {
  436. return media.targetDuration * 3;
  437. }
  438. return 0;
  439. };
  440. /**
  441. * walk backward until we find a duration we can use
  442. * or return a failure
  443. *
  444. * @param {Playlist} playlist the playlist to walk through
  445. * @param {Number} endSequence the mediaSequence to stop walking on
  446. */
  447. const backwardDuration = function (playlist, endSequence) {
  448. let result = 0;
  449. let i = endSequence - playlist.mediaSequence; // if a start time is available for segment immediately following
  450. // the interval, use it
  451. let segment = playlist.segments[i]; // Walk backward until we find the latest segment with timeline
  452. // information that is earlier than endSequence
  453. if (segment) {
  454. if (typeof segment.start !== 'undefined') {
  455. return {
  456. result: segment.start,
  457. precise: true
  458. };
  459. }
  460. if (typeof segment.end !== 'undefined') {
  461. return {
  462. result: segment.end - segment.duration,
  463. precise: true
  464. };
  465. }
  466. }
  467. while (i--) {
  468. segment = playlist.segments[i];
  469. if (typeof segment.end !== 'undefined') {
  470. return {
  471. result: result + segment.end,
  472. precise: true
  473. };
  474. }
  475. result += segmentDurationWithParts(playlist, segment);
  476. if (typeof segment.start !== 'undefined') {
  477. return {
  478. result: result + segment.start,
  479. precise: true
  480. };
  481. }
  482. }
  483. return {
  484. result,
  485. precise: false
  486. };
  487. };
  488. /**
  489. * walk forward until we find a duration we can use
  490. * or return a failure
  491. *
  492. * @param {Playlist} playlist the playlist to walk through
  493. * @param {number} endSequence the mediaSequence to stop walking on
  494. */
  495. const forwardDuration = function (playlist, endSequence) {
  496. let result = 0;
  497. let segment;
  498. let i = endSequence - playlist.mediaSequence; // Walk forward until we find the earliest segment with timeline
  499. // information
  500. for (; i < playlist.segments.length; i++) {
  501. segment = playlist.segments[i];
  502. if (typeof segment.start !== 'undefined') {
  503. return {
  504. result: segment.start - result,
  505. precise: true
  506. };
  507. }
  508. result += segmentDurationWithParts(playlist, segment);
  509. if (typeof segment.end !== 'undefined') {
  510. return {
  511. result: segment.end - result,
  512. precise: true
  513. };
  514. }
  515. } // indicate we didn't find a useful duration estimate
  516. return {
  517. result: -1,
  518. precise: false
  519. };
  520. };
  521. /**
  522. * Calculate the media duration from the segments associated with a
  523. * playlist. The duration of a subinterval of the available segments
  524. * may be calculated by specifying an end index.
  525. *
  526. * @param {Object} playlist a media playlist object
  527. * @param {number=} endSequence an exclusive upper boundary
  528. * for the playlist. Defaults to playlist length.
  529. * @param {number} expired the amount of time that has dropped
  530. * off the front of the playlist in a live scenario
  531. * @return {number} the duration between the first available segment
  532. * and end index.
  533. */
  534. const intervalDuration = function (playlist, endSequence, expired) {
  535. if (typeof endSequence === 'undefined') {
  536. endSequence = playlist.mediaSequence + playlist.segments.length;
  537. }
  538. if (endSequence < playlist.mediaSequence) {
  539. return 0;
  540. } // do a backward walk to estimate the duration
  541. const backward = backwardDuration(playlist, endSequence);
  542. if (backward.precise) {
  543. // if we were able to base our duration estimate on timing
  544. // information provided directly from the Media Source, return
  545. // it
  546. return backward.result;
  547. } // walk forward to see if a precise duration estimate can be made
  548. // that way
  549. const forward = forwardDuration(playlist, endSequence);
  550. if (forward.precise) {
  551. // we found a segment that has been buffered and so it's
  552. // position is known precisely
  553. return forward.result;
  554. } // return the less-precise, playlist-based duration estimate
  555. return backward.result + expired;
  556. };
  557. /**
  558. * Calculates the duration of a playlist. If a start and end index
  559. * are specified, the duration will be for the subset of the media
  560. * timeline between those two indices. The total duration for live
  561. * playlists is always Infinity.
  562. *
  563. * @param {Object} playlist a media playlist object
  564. * @param {number=} endSequence an exclusive upper
  565. * boundary for the playlist. Defaults to the playlist media
  566. * sequence number plus its length.
  567. * @param {number=} expired the amount of time that has
  568. * dropped off the front of the playlist in a live scenario
  569. * @return {number} the duration between the start index and end
  570. * index.
  571. */
  572. const duration = function (playlist, endSequence, expired) {
  573. if (!playlist) {
  574. return 0;
  575. }
  576. if (typeof expired !== 'number') {
  577. expired = 0;
  578. } // if a slice of the total duration is not requested, use
  579. // playlist-level duration indicators when they're present
  580. if (typeof endSequence === 'undefined') {
  581. // if present, use the duration specified in the playlist
  582. if (playlist.totalDuration) {
  583. return playlist.totalDuration;
  584. } // duration should be Infinity for live playlists
  585. if (!playlist.endList) {
  586. return window__default["default"].Infinity;
  587. }
  588. } // calculate the total duration based on the segment durations
  589. return intervalDuration(playlist, endSequence, expired);
  590. };
  591. /**
  592. * Calculate the time between two indexes in the current playlist
  593. * neight the start- nor the end-index need to be within the current
  594. * playlist in which case, the targetDuration of the playlist is used
  595. * to approximate the durations of the segments
  596. *
  597. * @param {Array} options.durationList list to iterate over for durations.
  598. * @param {number} options.defaultDuration duration to use for elements before or after the durationList
  599. * @param {number} options.startIndex partsAndSegments index to start
  600. * @param {number} options.endIndex partsAndSegments index to end.
  601. * @return {number} the number of seconds between startIndex and endIndex
  602. */
  603. const sumDurations = function ({
  604. defaultDuration,
  605. durationList,
  606. startIndex,
  607. endIndex
  608. }) {
  609. let durations = 0;
  610. if (startIndex > endIndex) {
  611. [startIndex, endIndex] = [endIndex, startIndex];
  612. }
  613. if (startIndex < 0) {
  614. for (let i = startIndex; i < Math.min(0, endIndex); i++) {
  615. durations += defaultDuration;
  616. }
  617. startIndex = 0;
  618. }
  619. for (let i = startIndex; i < endIndex; i++) {
  620. durations += durationList[i].duration;
  621. }
  622. return durations;
  623. };
  624. /**
  625. * Calculates the playlist end time
  626. *
  627. * @param {Object} playlist a media playlist object
  628. * @param {number=} expired the amount of time that has
  629. * dropped off the front of the playlist in a live scenario
  630. * @param {boolean|false} useSafeLiveEnd a boolean value indicating whether or not the
  631. * playlist end calculation should consider the safe live end
  632. * (truncate the playlist end by three segments). This is normally
  633. * used for calculating the end of the playlist's seekable range.
  634. * This takes into account the value of liveEdgePadding.
  635. * Setting liveEdgePadding to 0 is equivalent to setting this to false.
  636. * @param {number} liveEdgePadding a number indicating how far from the end of the playlist we should be in seconds.
  637. * If this is provided, it is used in the safe live end calculation.
  638. * Setting useSafeLiveEnd=false or liveEdgePadding=0 are equivalent.
  639. * Corresponds to suggestedPresentationDelay in DASH manifests.
  640. * @return {number} the end time of playlist
  641. * @function playlistEnd
  642. */
  643. const playlistEnd = function (playlist, expired, useSafeLiveEnd, liveEdgePadding) {
  644. if (!playlist || !playlist.segments) {
  645. return null;
  646. }
  647. if (playlist.endList) {
  648. return duration(playlist);
  649. }
  650. if (expired === null) {
  651. return null;
  652. }
  653. expired = expired || 0;
  654. let lastSegmentEndTime = intervalDuration(playlist, playlist.mediaSequence + playlist.segments.length, expired);
  655. if (useSafeLiveEnd) {
  656. liveEdgePadding = typeof liveEdgePadding === 'number' ? liveEdgePadding : liveEdgeDelay(null, playlist);
  657. lastSegmentEndTime -= liveEdgePadding;
  658. } // don't return a time less than zero
  659. return Math.max(0, lastSegmentEndTime);
  660. };
  661. /**
  662. * Calculates the interval of time that is currently seekable in a
  663. * playlist. The returned time ranges are relative to the earliest
  664. * moment in the specified playlist that is still available. A full
  665. * seekable implementation for live streams would need to offset
  666. * these values by the duration of content that has expired from the
  667. * stream.
  668. *
  669. * @param {Object} playlist a media playlist object
  670. * dropped off the front of the playlist in a live scenario
  671. * @param {number=} expired the amount of time that has
  672. * dropped off the front of the playlist in a live scenario
  673. * @param {number} liveEdgePadding how far from the end of the playlist we should be in seconds.
  674. * Corresponds to suggestedPresentationDelay in DASH manifests.
  675. * @return {TimeRanges} the periods of time that are valid targets
  676. * for seeking
  677. */
  678. const seekable = function (playlist, expired, liveEdgePadding) {
  679. const useSafeLiveEnd = true;
  680. const seekableStart = expired || 0;
  681. let seekableEnd = playlistEnd(playlist, expired, useSafeLiveEnd, liveEdgePadding);
  682. if (seekableEnd === null) {
  683. return createTimeRanges();
  684. } // Clamp seekable end since it can not be less than the seekable start
  685. if (seekableEnd < seekableStart) {
  686. seekableEnd = seekableStart;
  687. }
  688. return createTimeRanges(seekableStart, seekableEnd);
  689. };
  690. /**
  691. * Determine the index and estimated starting time of the segment that
  692. * contains a specified playback position in a media playlist.
  693. *
  694. * @param {Object} options.playlist the media playlist to query
  695. * @param {number} options.currentTime The number of seconds since the earliest
  696. * possible position to determine the containing segment for
  697. * @param {number} options.startTime the time when the segment/part starts
  698. * @param {number} options.startingSegmentIndex the segment index to start looking at.
  699. * @param {number?} [options.startingPartIndex] the part index to look at within the segment.
  700. *
  701. * @return {Object} an object with partIndex, segmentIndex, and startTime.
  702. */
  703. const getMediaInfoForTime = function ({
  704. playlist,
  705. currentTime,
  706. startingSegmentIndex,
  707. startingPartIndex,
  708. startTime,
  709. exactManifestTimings
  710. }) {
  711. let time = currentTime - startTime;
  712. const partsAndSegments = getPartsAndSegments(playlist);
  713. let startIndex = 0;
  714. for (let i = 0; i < partsAndSegments.length; i++) {
  715. const partAndSegment = partsAndSegments[i];
  716. if (startingSegmentIndex !== partAndSegment.segmentIndex) {
  717. continue;
  718. } // skip this if part index does not match.
  719. if (typeof startingPartIndex === 'number' && typeof partAndSegment.partIndex === 'number' && startingPartIndex !== partAndSegment.partIndex) {
  720. continue;
  721. }
  722. startIndex = i;
  723. break;
  724. }
  725. if (time < 0) {
  726. // Walk backward from startIndex in the playlist, adding durations
  727. // until we find a segment that contains `time` and return it
  728. if (startIndex > 0) {
  729. for (let i = startIndex - 1; i >= 0; i--) {
  730. const partAndSegment = partsAndSegments[i];
  731. time += partAndSegment.duration;
  732. if (exactManifestTimings) {
  733. if (time < 0) {
  734. continue;
  735. }
  736. } else if (time + TIME_FUDGE_FACTOR <= 0) {
  737. continue;
  738. }
  739. return {
  740. partIndex: partAndSegment.partIndex,
  741. segmentIndex: partAndSegment.segmentIndex,
  742. startTime: startTime - sumDurations({
  743. defaultDuration: playlist.targetDuration,
  744. durationList: partsAndSegments,
  745. startIndex,
  746. endIndex: i
  747. })
  748. };
  749. }
  750. } // We were unable to find a good segment within the playlist
  751. // so select the first segment
  752. return {
  753. partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
  754. segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
  755. startTime: currentTime
  756. };
  757. } // When startIndex is negative, we first walk forward to first segment
  758. // adding target durations. If we "run out of time" before getting to
  759. // the first segment, return the first segment
  760. if (startIndex < 0) {
  761. for (let i = startIndex; i < 0; i++) {
  762. time -= playlist.targetDuration;
  763. if (time < 0) {
  764. return {
  765. partIndex: partsAndSegments[0] && partsAndSegments[0].partIndex || null,
  766. segmentIndex: partsAndSegments[0] && partsAndSegments[0].segmentIndex || 0,
  767. startTime: currentTime
  768. };
  769. }
  770. }
  771. startIndex = 0;
  772. } // Walk forward from startIndex in the playlist, subtracting durations
  773. // until we find a segment that contains `time` and return it
  774. for (let i = startIndex; i < partsAndSegments.length; i++) {
  775. const partAndSegment = partsAndSegments[i];
  776. time -= partAndSegment.duration;
  777. const canUseFudgeFactor = partAndSegment.duration > TIME_FUDGE_FACTOR;
  778. const isExactlyAtTheEnd = time === 0;
  779. const isExtremelyCloseToTheEnd = canUseFudgeFactor && time + TIME_FUDGE_FACTOR >= 0;
  780. if (isExactlyAtTheEnd || isExtremelyCloseToTheEnd) {
  781. // 1) We are exactly at the end of the current segment.
  782. // 2) We are extremely close to the end of the current segment (The difference is less than 1 / 30).
  783. // We may encounter this situation when
  784. // we don't have exact match between segment duration info in the manifest and the actual duration of the segment
  785. // For example:
  786. // We appended 3 segments 10 seconds each, meaning we should have 30 sec buffered,
  787. // but we the actual buffered is 29.99999
  788. //
  789. // In both cases:
  790. // if we passed current time -> it means that we already played current segment
  791. // if we passed buffered.end -> it means that this segment is already loaded and buffered
  792. // we should select the next segment if we have one:
  793. if (i !== partsAndSegments.length - 1) {
  794. continue;
  795. }
  796. }
  797. if (exactManifestTimings) {
  798. if (time > 0) {
  799. continue;
  800. }
  801. } else if (time - TIME_FUDGE_FACTOR >= 0) {
  802. continue;
  803. }
  804. return {
  805. partIndex: partAndSegment.partIndex,
  806. segmentIndex: partAndSegment.segmentIndex,
  807. startTime: startTime + sumDurations({
  808. defaultDuration: playlist.targetDuration,
  809. durationList: partsAndSegments,
  810. startIndex,
  811. endIndex: i
  812. })
  813. };
  814. } // We are out of possible candidates so load the last one...
  815. return {
  816. segmentIndex: partsAndSegments[partsAndSegments.length - 1].segmentIndex,
  817. partIndex: partsAndSegments[partsAndSegments.length - 1].partIndex,
  818. startTime: currentTime
  819. };
  820. };
  821. /**
  822. * Check whether the playlist is excluded or not.
  823. *
  824. * @param {Object} playlist the media playlist object
  825. * @return {boolean} whether the playlist is excluded or not
  826. * @function isExcluded
  827. */
  828. const isExcluded = function (playlist) {
  829. return playlist.excludeUntil && playlist.excludeUntil > Date.now();
  830. };
  831. /**
  832. * Check whether the playlist is compatible with current playback configuration or has
  833. * been excluded permanently for being incompatible.
  834. *
  835. * @param {Object} playlist the media playlist object
  836. * @return {boolean} whether the playlist is incompatible or not
  837. * @function isIncompatible
  838. */
  839. const isIncompatible = function (playlist) {
  840. return playlist.excludeUntil && playlist.excludeUntil === Infinity;
  841. };
  842. /**
  843. * Check whether the playlist is enabled or not.
  844. *
  845. * @param {Object} playlist the media playlist object
  846. * @return {boolean} whether the playlist is enabled or not
  847. * @function isEnabled
  848. */
  849. const isEnabled = function (playlist) {
  850. const excluded = isExcluded(playlist);
  851. return !playlist.disabled && !excluded;
  852. };
  853. /**
  854. * Check whether the playlist has been manually disabled through the representations api.
  855. *
  856. * @param {Object} playlist the media playlist object
  857. * @return {boolean} whether the playlist is disabled manually or not
  858. * @function isDisabled
  859. */
  860. const isDisabled = function (playlist) {
  861. return playlist.disabled;
  862. };
  863. /**
  864. * Returns whether the current playlist is an AES encrypted HLS stream
  865. *
  866. * @return {boolean} true if it's an AES encrypted HLS stream
  867. */
  868. const isAes = function (media) {
  869. for (let i = 0; i < media.segments.length; i++) {
  870. if (media.segments[i].key) {
  871. return true;
  872. }
  873. }
  874. return false;
  875. };
  876. /**
  877. * Checks if the playlist has a value for the specified attribute
  878. *
  879. * @param {string} attr
  880. * Attribute to check for
  881. * @param {Object} playlist
  882. * The media playlist object
  883. * @return {boolean}
  884. * Whether the playlist contains a value for the attribute or not
  885. * @function hasAttribute
  886. */
  887. const hasAttribute = function (attr, playlist) {
  888. return playlist.attributes && playlist.attributes[attr];
  889. };
  890. /**
  891. * Estimates the time required to complete a segment download from the specified playlist
  892. *
  893. * @param {number} segmentDuration
  894. * Duration of requested segment
  895. * @param {number} bandwidth
  896. * Current measured bandwidth of the player
  897. * @param {Object} playlist
  898. * The media playlist object
  899. * @param {number=} bytesReceived
  900. * Number of bytes already received for the request. Defaults to 0
  901. * @return {number|NaN}
  902. * The estimated time to request the segment. NaN if bandwidth information for
  903. * the given playlist is unavailable
  904. * @function estimateSegmentRequestTime
  905. */
  906. const estimateSegmentRequestTime = function (segmentDuration, bandwidth, playlist, bytesReceived = 0) {
  907. if (!hasAttribute('BANDWIDTH', playlist)) {
  908. return NaN;
  909. }
  910. const size = segmentDuration * playlist.attributes.BANDWIDTH;
  911. return (size - bytesReceived * 8) / bandwidth;
  912. };
  913. /*
  914. * Returns whether the current playlist is the lowest rendition
  915. *
  916. * @return {Boolean} true if on lowest rendition
  917. */
  918. const isLowestEnabledRendition = (main, media) => {
  919. if (main.playlists.length === 1) {
  920. return true;
  921. }
  922. const currentBandwidth = media.attributes.BANDWIDTH || Number.MAX_VALUE;
  923. return main.playlists.filter(playlist => {
  924. if (!isEnabled(playlist)) {
  925. return false;
  926. }
  927. return (playlist.attributes.BANDWIDTH || 0) < currentBandwidth;
  928. }).length === 0;
  929. };
  930. const playlistMatch = (a, b) => {
  931. // both playlits are null
  932. // or only one playlist is non-null
  933. // no match
  934. if (!a && !b || !a && b || a && !b) {
  935. return false;
  936. } // playlist objects are the same, match
  937. if (a === b) {
  938. return true;
  939. } // first try to use id as it should be the most
  940. // accurate
  941. if (a.id && b.id && a.id === b.id) {
  942. return true;
  943. } // next try to use reslovedUri as it should be the
  944. // second most accurate.
  945. if (a.resolvedUri && b.resolvedUri && a.resolvedUri === b.resolvedUri) {
  946. return true;
  947. } // finally try to use uri as it should be accurate
  948. // but might miss a few cases for relative uris
  949. if (a.uri && b.uri && a.uri === b.uri) {
  950. return true;
  951. }
  952. return false;
  953. };
  954. const someAudioVariant = function (main, callback) {
  955. const AUDIO = main && main.mediaGroups && main.mediaGroups.AUDIO || {};
  956. let found = false;
  957. for (const groupName in AUDIO) {
  958. for (const label in AUDIO[groupName]) {
  959. found = callback(AUDIO[groupName][label]);
  960. if (found) {
  961. break;
  962. }
  963. }
  964. if (found) {
  965. break;
  966. }
  967. }
  968. return !!found;
  969. };
  970. const isAudioOnly = main => {
  971. // we are audio only if we have no main playlists but do
  972. // have media group playlists.
  973. if (!main || !main.playlists || !main.playlists.length) {
  974. // without audio variants or playlists this
  975. // is not an audio only main.
  976. const found = someAudioVariant(main, variant => variant.playlists && variant.playlists.length || variant.uri);
  977. return found;
  978. } // if every playlist has only an audio codec it is audio only
  979. for (let i = 0; i < main.playlists.length; i++) {
  980. const playlist = main.playlists[i];
  981. const CODECS = playlist.attributes && playlist.attributes.CODECS; // all codecs are audio, this is an audio playlist.
  982. if (CODECS && CODECS.split(',').every(c => codecs_js.isAudioCodec(c))) {
  983. continue;
  984. } // playlist is in an audio group it is audio only
  985. const found = someAudioVariant(main, variant => playlistMatch(playlist, variant));
  986. if (found) {
  987. continue;
  988. } // if we make it here this playlist isn't audio and we
  989. // are not audio only
  990. return false;
  991. } // if we make it past every playlist without returning, then
  992. // this is an audio only playlist.
  993. return true;
  994. }; // exports
  995. var Playlist = {
  996. liveEdgeDelay,
  997. duration,
  998. seekable,
  999. getMediaInfoForTime,
  1000. isEnabled,
  1001. isDisabled,
  1002. isExcluded,
  1003. isIncompatible,
  1004. playlistEnd,
  1005. isAes,
  1006. hasAttribute,
  1007. estimateSegmentRequestTime,
  1008. isLowestEnabledRendition,
  1009. isAudioOnly,
  1010. playlistMatch,
  1011. segmentDurationWithParts
  1012. };
  1013. const {
  1014. log
  1015. } = videojs__default["default"];
  1016. const createPlaylistID = (index, uri) => {
  1017. return `${index}-${uri}`;
  1018. }; // default function for creating a group id
  1019. const groupID = (type, group, label) => {
  1020. return `placeholder-uri-${type}-${group}-${label}`;
  1021. };
  1022. /**
  1023. * Parses a given m3u8 playlist
  1024. *
  1025. * @param {Function} [onwarn]
  1026. * a function to call when the parser triggers a warning event.
  1027. * @param {Function} [oninfo]
  1028. * a function to call when the parser triggers an info event.
  1029. * @param {string} manifestString
  1030. * The downloaded manifest string
  1031. * @param {Object[]} [customTagParsers]
  1032. * An array of custom tag parsers for the m3u8-parser instance
  1033. * @param {Object[]} [customTagMappers]
  1034. * An array of custom tag mappers for the m3u8-parser instance
  1035. * @param {boolean} [llhls]
  1036. * Whether to keep ll-hls features in the manifest after parsing.
  1037. * @return {Object}
  1038. * The manifest object
  1039. */
  1040. const parseManifest = ({
  1041. onwarn,
  1042. oninfo,
  1043. manifestString,
  1044. customTagParsers = [],
  1045. customTagMappers = [],
  1046. llhls
  1047. }) => {
  1048. const parser = new m3u8Parser.Parser();
  1049. if (onwarn) {
  1050. parser.on('warn', onwarn);
  1051. }
  1052. if (oninfo) {
  1053. parser.on('info', oninfo);
  1054. }
  1055. customTagParsers.forEach(customParser => parser.addParser(customParser));
  1056. customTagMappers.forEach(mapper => parser.addTagMapper(mapper));
  1057. parser.push(manifestString);
  1058. parser.end();
  1059. const manifest = parser.manifest; // remove llhls features from the parsed manifest
  1060. // if we don't want llhls support.
  1061. if (!llhls) {
  1062. ['preloadSegment', 'skip', 'serverControl', 'renditionReports', 'partInf', 'partTargetDuration'].forEach(function (k) {
  1063. if (manifest.hasOwnProperty(k)) {
  1064. delete manifest[k];
  1065. }
  1066. });
  1067. if (manifest.segments) {
  1068. manifest.segments.forEach(function (segment) {
  1069. ['parts', 'preloadHints'].forEach(function (k) {
  1070. if (segment.hasOwnProperty(k)) {
  1071. delete segment[k];
  1072. }
  1073. });
  1074. });
  1075. }
  1076. }
  1077. if (!manifest.targetDuration) {
  1078. let targetDuration = 10;
  1079. if (manifest.segments && manifest.segments.length) {
  1080. targetDuration = manifest.segments.reduce((acc, s) => Math.max(acc, s.duration), 0);
  1081. }
  1082. if (onwarn) {
  1083. onwarn({
  1084. message: `manifest has no targetDuration defaulting to ${targetDuration}`
  1085. });
  1086. }
  1087. manifest.targetDuration = targetDuration;
  1088. }
  1089. const parts = getLastParts(manifest);
  1090. if (parts.length && !manifest.partTargetDuration) {
  1091. const partTargetDuration = parts.reduce((acc, p) => Math.max(acc, p.duration), 0);
  1092. if (onwarn) {
  1093. onwarn({
  1094. message: `manifest has no partTargetDuration defaulting to ${partTargetDuration}`
  1095. });
  1096. log.error('LL-HLS manifest has parts but lacks required #EXT-X-PART-INF:PART-TARGET value. See https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis-09#section-4.4.3.7. Playback is not guaranteed.');
  1097. }
  1098. manifest.partTargetDuration = partTargetDuration;
  1099. }
  1100. return manifest;
  1101. };
  1102. /**
  1103. * Loops through all supported media groups in main and calls the provided
  1104. * callback for each group
  1105. *
  1106. * @param {Object} main
  1107. * The parsed main manifest object
  1108. * @param {Function} callback
  1109. * Callback to call for each media group
  1110. */
  1111. const forEachMediaGroup = (main, callback) => {
  1112. if (!main.mediaGroups) {
  1113. return;
  1114. }
  1115. ['AUDIO', 'SUBTITLES'].forEach(mediaType => {
  1116. if (!main.mediaGroups[mediaType]) {
  1117. return;
  1118. }
  1119. for (const groupKey in main.mediaGroups[mediaType]) {
  1120. for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
  1121. const mediaProperties = main.mediaGroups[mediaType][groupKey][labelKey];
  1122. callback(mediaProperties, mediaType, groupKey, labelKey);
  1123. }
  1124. }
  1125. });
  1126. };
  1127. /**
  1128. * Adds properties and attributes to the playlist to keep consistent functionality for
  1129. * playlists throughout VHS.
  1130. *
  1131. * @param {Object} config
  1132. * Arguments object
  1133. * @param {Object} config.playlist
  1134. * The media playlist
  1135. * @param {string} [config.uri]
  1136. * The uri to the media playlist (if media playlist is not from within a main
  1137. * playlist)
  1138. * @param {string} id
  1139. * ID to use for the playlist
  1140. */
  1141. const setupMediaPlaylist = ({
  1142. playlist,
  1143. uri,
  1144. id
  1145. }) => {
  1146. playlist.id = id;
  1147. playlist.playlistErrors_ = 0;
  1148. if (uri) {
  1149. // For media playlists, m3u8-parser does not have access to a URI, as HLS media
  1150. // playlists do not contain their own source URI, but one is needed for consistency in
  1151. // VHS.
  1152. playlist.uri = uri;
  1153. } // For HLS main playlists, even though certain attributes MUST be defined, the
  1154. // stream may still be played without them.
  1155. // For HLS media playlists, m3u8-parser does not attach an attributes object to the
  1156. // manifest.
  1157. //
  1158. // To avoid undefined reference errors through the project, and make the code easier
  1159. // to write/read, add an empty attributes object for these cases.
  1160. playlist.attributes = playlist.attributes || {};
  1161. };
  1162. /**
  1163. * Adds ID, resolvedUri, and attributes properties to each playlist of the main, where
  1164. * necessary. In addition, creates playlist IDs for each playlist and adds playlist ID to
  1165. * playlist references to the playlists array.
  1166. *
  1167. * @param {Object} main
  1168. * The main playlist
  1169. */
  1170. const setupMediaPlaylists = main => {
  1171. let i = main.playlists.length;
  1172. while (i--) {
  1173. const playlist = main.playlists[i];
  1174. setupMediaPlaylist({
  1175. playlist,
  1176. id: createPlaylistID(i, playlist.uri)
  1177. });
  1178. playlist.resolvedUri = resolveUrl(main.uri, playlist.uri);
  1179. main.playlists[playlist.id] = playlist; // URI reference added for backwards compatibility
  1180. main.playlists[playlist.uri] = playlist; // Although the spec states an #EXT-X-STREAM-INF tag MUST have a BANDWIDTH attribute,
  1181. // the stream can be played without it. Although an attributes property may have been
  1182. // added to the playlist to prevent undefined references, issue a warning to fix the
  1183. // manifest.
  1184. if (!playlist.attributes.BANDWIDTH) {
  1185. log.warn('Invalid playlist STREAM-INF detected. Missing BANDWIDTH attribute.');
  1186. }
  1187. }
  1188. };
  1189. /**
  1190. * Adds resolvedUri properties to each media group.
  1191. *
  1192. * @param {Object} main
  1193. * The main playlist
  1194. */
  1195. const resolveMediaGroupUris = main => {
  1196. forEachMediaGroup(main, properties => {
  1197. if (properties.uri) {
  1198. properties.resolvedUri = resolveUrl(main.uri, properties.uri);
  1199. }
  1200. });
  1201. };
  1202. /**
  1203. * Creates a main playlist wrapper to insert a sole media playlist into.
  1204. *
  1205. * @param {Object} media
  1206. * Media playlist
  1207. * @param {string} uri
  1208. * The media URI
  1209. *
  1210. * @return {Object}
  1211. * main playlist
  1212. */
  1213. const mainForMedia = (media, uri) => {
  1214. const id = createPlaylistID(0, uri);
  1215. const main = {
  1216. mediaGroups: {
  1217. 'AUDIO': {},
  1218. 'VIDEO': {},
  1219. 'CLOSED-CAPTIONS': {},
  1220. 'SUBTITLES': {}
  1221. },
  1222. uri: window__default["default"].location.href,
  1223. resolvedUri: window__default["default"].location.href,
  1224. playlists: [{
  1225. uri,
  1226. id,
  1227. resolvedUri: uri,
  1228. // m3u8-parser does not attach an attributes property to media playlists so make
  1229. // sure that the property is attached to avoid undefined reference errors
  1230. attributes: {}
  1231. }]
  1232. }; // set up ID reference
  1233. main.playlists[id] = main.playlists[0]; // URI reference added for backwards compatibility
  1234. main.playlists[uri] = main.playlists[0];
  1235. return main;
  1236. };
  1237. /**
  1238. * Does an in-place update of the main manifest to add updated playlist URI references
  1239. * as well as other properties needed by VHS that aren't included by the parser.
  1240. *
  1241. * @param {Object} main
  1242. * main manifest object
  1243. * @param {string} uri
  1244. * The source URI
  1245. * @param {function} createGroupID
  1246. * A function to determine how to create the groupID for mediaGroups
  1247. */
  1248. const addPropertiesToMain = (main, uri, createGroupID = groupID) => {
  1249. main.uri = uri;
  1250. for (let i = 0; i < main.playlists.length; i++) {
  1251. if (!main.playlists[i].uri) {
  1252. // Set up phony URIs for the playlists since playlists are referenced by their URIs
  1253. // throughout VHS, but some formats (e.g., DASH) don't have external URIs
  1254. // TODO: consider adding dummy URIs in mpd-parser
  1255. const phonyUri = `placeholder-uri-${i}`;
  1256. main.playlists[i].uri = phonyUri;
  1257. }
  1258. }
  1259. const audioOnlyMain = isAudioOnly(main);
  1260. forEachMediaGroup(main, (properties, mediaType, groupKey, labelKey) => {
  1261. // add a playlist array under properties
  1262. if (!properties.playlists || !properties.playlists.length) {
  1263. // If the manifest is audio only and this media group does not have a uri, check
  1264. // if the media group is located in the main list of playlists. If it is, don't add
  1265. // placeholder properties as it shouldn't be considered an alternate audio track.
  1266. if (audioOnlyMain && mediaType === 'AUDIO' && !properties.uri) {
  1267. for (let i = 0; i < main.playlists.length; i++) {
  1268. const p = main.playlists[i];
  1269. if (p.attributes && p.attributes.AUDIO && p.attributes.AUDIO === groupKey) {
  1270. return;
  1271. }
  1272. }
  1273. }
  1274. properties.playlists = [_extends__default["default"]({}, properties)];
  1275. }
  1276. properties.playlists.forEach(function (p, i) {
  1277. const groupId = createGroupID(mediaType, groupKey, labelKey, p);
  1278. const id = createPlaylistID(i, groupId);
  1279. if (p.uri) {
  1280. p.resolvedUri = p.resolvedUri || resolveUrl(main.uri, p.uri);
  1281. } else {
  1282. // DEPRECATED, this has been added to prevent a breaking change.
  1283. // previously we only ever had a single media group playlist, so
  1284. // we mark the first playlist uri without prepending the index as we used to
  1285. // ideally we would do all of the playlists the same way.
  1286. p.uri = i === 0 ? groupId : id; // don't resolve a placeholder uri to an absolute url, just use
  1287. // the placeholder again
  1288. p.resolvedUri = p.uri;
  1289. }
  1290. p.id = p.id || id; // add an empty attributes object, all playlists are
  1291. // expected to have this.
  1292. p.attributes = p.attributes || {}; // setup ID and URI references (URI for backwards compatibility)
  1293. main.playlists[p.id] = p;
  1294. main.playlists[p.uri] = p;
  1295. });
  1296. });
  1297. setupMediaPlaylists(main);
  1298. resolveMediaGroupUris(main);
  1299. };
  1300. class DateRangesStorage {
  1301. constructor() {
  1302. this.offset_ = null;
  1303. this.pendingDateRanges_ = new Map();
  1304. this.processedDateRanges_ = new Map();
  1305. }
  1306. setOffset(segments = []) {
  1307. // already set
  1308. if (this.offset_ !== null) {
  1309. return;
  1310. } // no segment to process
  1311. if (!segments.length) {
  1312. return;
  1313. }
  1314. const [firstSegment] = segments; // no program date time
  1315. if (firstSegment.programDateTime === undefined) {
  1316. return;
  1317. } // Set offset as ProgramDateTime for the very first segment of the very first playlist load:
  1318. this.offset_ = firstSegment.programDateTime / 1000;
  1319. }
  1320. setPendingDateRanges(dateRanges = []) {
  1321. if (!dateRanges.length) {
  1322. return;
  1323. }
  1324. const [dateRange] = dateRanges;
  1325. const startTime = dateRange.startDate.getTime();
  1326. this.trimProcessedDateRanges_(startTime);
  1327. this.pendingDateRanges_ = dateRanges.reduce((map, pendingDateRange) => {
  1328. map.set(pendingDateRange.id, pendingDateRange);
  1329. return map;
  1330. }, new Map());
  1331. }
  1332. processDateRange(dateRange) {
  1333. this.pendingDateRanges_.delete(dateRange.id);
  1334. this.processedDateRanges_.set(dateRange.id, dateRange);
  1335. }
  1336. getDateRangesToProcess() {
  1337. if (this.offset_ === null) {
  1338. return [];
  1339. }
  1340. const dateRangeClasses = {};
  1341. const dateRangesToProcess = [];
  1342. this.pendingDateRanges_.forEach((dateRange, id) => {
  1343. if (this.processedDateRanges_.has(id)) {
  1344. return;
  1345. }
  1346. dateRange.startTime = dateRange.startDate.getTime() / 1000 - this.offset_;
  1347. dateRange.processDateRange = () => this.processDateRange(dateRange);
  1348. dateRangesToProcess.push(dateRange);
  1349. if (!dateRange.class) {
  1350. return;
  1351. }
  1352. if (dateRangeClasses[dateRange.class]) {
  1353. const length = dateRangeClasses[dateRange.class].push(dateRange);
  1354. dateRange.classListIndex = length - 1;
  1355. } else {
  1356. dateRangeClasses[dateRange.class] = [dateRange];
  1357. dateRange.classListIndex = 0;
  1358. }
  1359. });
  1360. for (const dateRange of dateRangesToProcess) {
  1361. const classList = dateRangeClasses[dateRange.class] || [];
  1362. if (dateRange.endDate) {
  1363. dateRange.endTime = dateRange.endDate.getTime() / 1000 - this.offset_;
  1364. } else if (dateRange.endOnNext && classList[dateRange.classListIndex + 1]) {
  1365. dateRange.endTime = classList[dateRange.classListIndex + 1].startTime;
  1366. } else if (dateRange.duration) {
  1367. dateRange.endTime = dateRange.startTime + dateRange.duration;
  1368. } else if (dateRange.plannedDuration) {
  1369. dateRange.endTime = dateRange.startTime + dateRange.plannedDuration;
  1370. } else {
  1371. dateRange.endTime = dateRange.startTime;
  1372. }
  1373. }
  1374. return dateRangesToProcess;
  1375. }
  1376. trimProcessedDateRanges_(startTime) {
  1377. const copy = new Map(this.processedDateRanges_);
  1378. copy.forEach((dateRange, id) => {
  1379. if (dateRange.startDate.getTime() < startTime) {
  1380. this.processedDateRanges_.delete(id);
  1381. }
  1382. });
  1383. }
  1384. }
  1385. const {
  1386. EventTarget: EventTarget$1
  1387. } = videojs__default["default"];
  1388. const addLLHLSQueryDirectives = (uri, media) => {
  1389. if (media.endList || !media.serverControl) {
  1390. return uri;
  1391. }
  1392. const parameters = {};
  1393. if (media.serverControl.canBlockReload) {
  1394. const {
  1395. preloadSegment
  1396. } = media; // next msn is a zero based value, length is not.
  1397. let nextMSN = media.mediaSequence + media.segments.length; // If preload segment has parts then it is likely
  1398. // that we are going to request a part of that preload segment.
  1399. // the logic below is used to determine that.
  1400. if (preloadSegment) {
  1401. const parts = preloadSegment.parts || []; // _HLS_part is a zero based index
  1402. const nextPart = getKnownPartCount(media) - 1; // if nextPart is > -1 and not equal to just the
  1403. // length of parts, then we know we had part preload hints
  1404. // and we need to add the _HLS_part= query
  1405. if (nextPart > -1 && nextPart !== parts.length - 1) {
  1406. // add existing parts to our preload hints
  1407. // eslint-disable-next-line
  1408. parameters._HLS_part = nextPart;
  1409. } // this if statement makes sure that we request the msn
  1410. // of the preload segment if:
  1411. // 1. the preload segment had parts (and was not yet a full segment)
  1412. // but was added to our segments array
  1413. // 2. the preload segment had preload hints for parts that are not in
  1414. // the manifest yet.
  1415. // in all other cases we want the segment after the preload segment
  1416. // which will be given by using media.segments.length because it is 1 based
  1417. // rather than 0 based.
  1418. if (nextPart > -1 || parts.length) {
  1419. nextMSN--;
  1420. }
  1421. } // add _HLS_msn= in front of any _HLS_part query
  1422. // eslint-disable-next-line
  1423. parameters._HLS_msn = nextMSN;
  1424. }
  1425. if (media.serverControl && media.serverControl.canSkipUntil) {
  1426. // add _HLS_skip= infront of all other queries.
  1427. // eslint-disable-next-line
  1428. parameters._HLS_skip = media.serverControl.canSkipDateranges ? 'v2' : 'YES';
  1429. }
  1430. if (Object.keys(parameters).length) {
  1431. const parsedUri = new window__default["default"].URL(uri);
  1432. ['_HLS_skip', '_HLS_msn', '_HLS_part'].forEach(function (name) {
  1433. if (!parameters.hasOwnProperty(name)) {
  1434. return;
  1435. }
  1436. parsedUri.searchParams.set(name, parameters[name]);
  1437. });
  1438. uri = parsedUri.toString();
  1439. }
  1440. return uri;
  1441. };
  1442. /**
  1443. * Returns a new segment object with properties and
  1444. * the parts array merged.
  1445. *
  1446. * @param {Object} a the old segment
  1447. * @param {Object} b the new segment
  1448. *
  1449. * @return {Object} the merged segment
  1450. */
  1451. const updateSegment = (a, b) => {
  1452. if (!a) {
  1453. return b;
  1454. }
  1455. const result = merge(a, b); // if only the old segment has preload hints
  1456. // and the new one does not, remove preload hints.
  1457. if (a.preloadHints && !b.preloadHints) {
  1458. delete result.preloadHints;
  1459. } // if only the old segment has parts
  1460. // then the parts are no longer valid
  1461. if (a.parts && !b.parts) {
  1462. delete result.parts; // if both segments have parts
  1463. // copy part propeties from the old segment
  1464. // to the new one.
  1465. } else if (a.parts && b.parts) {
  1466. for (let i = 0; i < b.parts.length; i++) {
  1467. if (a.parts && a.parts[i]) {
  1468. result.parts[i] = merge(a.parts[i], b.parts[i]);
  1469. }
  1470. }
  1471. } // set skipped to false for segments that have
  1472. // have had information merged from the old segment.
  1473. if (!a.skipped && b.skipped) {
  1474. result.skipped = false;
  1475. } // set preload to false for segments that have
  1476. // had information added in the new segment.
  1477. if (a.preload && !b.preload) {
  1478. result.preload = false;
  1479. }
  1480. return result;
  1481. };
  1482. /**
  1483. * Returns a new array of segments that is the result of merging
  1484. * properties from an older list of segments onto an updated
  1485. * list. No properties on the updated playlist will be ovewritten.
  1486. *
  1487. * @param {Array} original the outdated list of segments
  1488. * @param {Array} update the updated list of segments
  1489. * @param {number=} offset the index of the first update
  1490. * segment in the original segment list. For non-live playlists,
  1491. * this should always be zero and does not need to be
  1492. * specified. For live playlists, it should be the difference
  1493. * between the media sequence numbers in the original and updated
  1494. * playlists.
  1495. * @return {Array} a list of merged segment objects
  1496. */
  1497. const updateSegments = (original, update, offset) => {
  1498. const oldSegments = original.slice();
  1499. const newSegments = update.slice();
  1500. offset = offset || 0;
  1501. const result = [];
  1502. let currentMap;
  1503. for (let newIndex = 0; newIndex < newSegments.length; newIndex++) {
  1504. const oldSegment = oldSegments[newIndex + offset];
  1505. const newSegment = newSegments[newIndex];
  1506. if (oldSegment) {
  1507. currentMap = oldSegment.map || currentMap;
  1508. result.push(updateSegment(oldSegment, newSegment));
  1509. } else {
  1510. // carry over map to new segment if it is missing
  1511. if (currentMap && !newSegment.map) {
  1512. newSegment.map = currentMap;
  1513. }
  1514. result.push(newSegment);
  1515. }
  1516. }
  1517. return result;
  1518. };
  1519. const resolveSegmentUris = (segment, baseUri) => {
  1520. // preloadSegment will not have a uri at all
  1521. // as the segment isn't actually in the manifest yet, only parts
  1522. if (!segment.resolvedUri && segment.uri) {
  1523. segment.resolvedUri = resolveUrl(baseUri, segment.uri);
  1524. }
  1525. if (segment.key && !segment.key.resolvedUri) {
  1526. segment.key.resolvedUri = resolveUrl(baseUri, segment.key.uri);
  1527. }
  1528. if (segment.map && !segment.map.resolvedUri) {
  1529. segment.map.resolvedUri = resolveUrl(baseUri, segment.map.uri);
  1530. }
  1531. if (segment.map && segment.map.key && !segment.map.key.resolvedUri) {
  1532. segment.map.key.resolvedUri = resolveUrl(baseUri, segment.map.key.uri);
  1533. }
  1534. if (segment.parts && segment.parts.length) {
  1535. segment.parts.forEach(p => {
  1536. if (p.resolvedUri) {
  1537. return;
  1538. }
  1539. p.resolvedUri = resolveUrl(baseUri, p.uri);
  1540. });
  1541. }
  1542. if (segment.preloadHints && segment.preloadHints.length) {
  1543. segment.preloadHints.forEach(p => {
  1544. if (p.resolvedUri) {
  1545. return;
  1546. }
  1547. p.resolvedUri = resolveUrl(baseUri, p.uri);
  1548. });
  1549. }
  1550. };
  1551. const getAllSegments = function (media) {
  1552. const segments = media.segments || [];
  1553. const preloadSegment = media.preloadSegment; // a preloadSegment with only preloadHints is not currently
  1554. // a usable segment, only include a preloadSegment that has
  1555. // parts.
  1556. if (preloadSegment && preloadSegment.parts && preloadSegment.parts.length) {
  1557. // if preloadHints has a MAP that means that the
  1558. // init segment is going to change. We cannot use any of the parts
  1559. // from this preload segment.
  1560. if (preloadSegment.preloadHints) {
  1561. for (let i = 0; i < preloadSegment.preloadHints.length; i++) {
  1562. if (preloadSegment.preloadHints[i].type === 'MAP') {
  1563. return segments;
  1564. }
  1565. }
  1566. } // set the duration for our preload segment to target duration.
  1567. preloadSegment.duration = media.targetDuration;
  1568. preloadSegment.preload = true;
  1569. segments.push(preloadSegment);
  1570. }
  1571. return segments;
  1572. }; // consider the playlist unchanged if the playlist object is the same or
  1573. // the number of segments is equal, the media sequence number is unchanged,
  1574. // and this playlist hasn't become the end of the playlist
  1575. const isPlaylistUnchanged = (a, b) => a === b || a.segments && b.segments && a.segments.length === b.segments.length && a.endList === b.endList && a.mediaSequence === b.mediaSequence && a.preloadSegment === b.preloadSegment;
  1576. /**
  1577. * Returns a new main playlist that is the result of merging an
  1578. * updated media playlist into the original version. If the
  1579. * updated media playlist does not match any of the playlist
  1580. * entries in the original main playlist, null is returned.
  1581. *
  1582. * @param {Object} main a parsed main M3U8 object
  1583. * @param {Object} media a parsed media M3U8 object
  1584. * @return {Object} a new object that represents the original
  1585. * main playlist with the updated media playlist merged in, or
  1586. * null if the merge produced no change.
  1587. */
  1588. const updateMain$1 = (main, newMedia, unchangedCheck = isPlaylistUnchanged) => {
  1589. const result = merge(main, {});
  1590. const oldMedia = result.playlists[newMedia.id];
  1591. if (!oldMedia) {
  1592. return null;
  1593. }
  1594. if (unchangedCheck(oldMedia, newMedia)) {
  1595. return null;
  1596. }
  1597. newMedia.segments = getAllSegments(newMedia);
  1598. const mergedPlaylist = merge(oldMedia, newMedia); // always use the new media's preload segment
  1599. if (mergedPlaylist.preloadSegment && !newMedia.preloadSegment) {
  1600. delete mergedPlaylist.preloadSegment;
  1601. } // if the update could overlap existing segment information, merge the two segment lists
  1602. if (oldMedia.segments) {
  1603. if (newMedia.skip) {
  1604. newMedia.segments = newMedia.segments || []; // add back in objects for skipped segments, so that we merge
  1605. // old properties into the new segments
  1606. for (let i = 0; i < newMedia.skip.skippedSegments; i++) {
  1607. newMedia.segments.unshift({
  1608. skipped: true
  1609. });
  1610. }
  1611. }
  1612. mergedPlaylist.segments = updateSegments(oldMedia.segments, newMedia.segments, newMedia.mediaSequence - oldMedia.mediaSequence);
  1613. } // resolve any segment URIs to prevent us from having to do it later
  1614. mergedPlaylist.segments.forEach(segment => {
  1615. resolveSegmentUris(segment, mergedPlaylist.resolvedUri);
  1616. }); // TODO Right now in the playlists array there are two references to each playlist, one
  1617. // that is referenced by index, and one by URI. The index reference may no longer be
  1618. // necessary.
  1619. for (let i = 0; i < result.playlists.length; i++) {
  1620. if (result.playlists[i].id === newMedia.id) {
  1621. result.playlists[i] = mergedPlaylist;
  1622. }
  1623. }
  1624. result.playlists[newMedia.id] = mergedPlaylist; // URI reference added for backwards compatibility
  1625. result.playlists[newMedia.uri] = mergedPlaylist; // update media group playlist references.
  1626. forEachMediaGroup(main, (properties, mediaType, groupKey, labelKey) => {
  1627. if (!properties.playlists) {
  1628. return;
  1629. }
  1630. for (let i = 0; i < properties.playlists.length; i++) {
  1631. if (newMedia.id === properties.playlists[i].id) {
  1632. properties.playlists[i] = mergedPlaylist;
  1633. }
  1634. }
  1635. });
  1636. return result;
  1637. };
  1638. /**
  1639. * Calculates the time to wait before refreshing a live playlist
  1640. *
  1641. * @param {Object} media
  1642. * The current media
  1643. * @param {boolean} update
  1644. * True if there were any updates from the last refresh, false otherwise
  1645. * @return {number}
  1646. * The time in ms to wait before refreshing the live playlist
  1647. */
  1648. const refreshDelay = (media, update) => {
  1649. const segments = media.segments || [];
  1650. const lastSegment = segments[segments.length - 1];
  1651. const lastPart = lastSegment && lastSegment.parts && lastSegment.parts[lastSegment.parts.length - 1];
  1652. const lastDuration = lastPart && lastPart.duration || lastSegment && lastSegment.duration;
  1653. if (update && lastDuration) {
  1654. return lastDuration * 1000;
  1655. } // if the playlist is unchanged since the last reload or last segment duration
  1656. // cannot be determined, try again after half the target duration
  1657. return (media.partTargetDuration || media.targetDuration || 10) * 500;
  1658. };
  1659. /**
  1660. * Load a playlist from a remote location
  1661. *
  1662. * @class PlaylistLoader
  1663. * @extends Stream
  1664. * @param {string|Object} src url or object of manifest
  1665. * @param {boolean} withCredentials the withCredentials xhr option
  1666. * @class
  1667. */
  1668. class PlaylistLoader extends EventTarget$1 {
  1669. constructor(src, vhs, options = {}) {
  1670. super();
  1671. if (!src) {
  1672. throw new Error('A non-empty playlist URL or object is required');
  1673. }
  1674. this.logger_ = logger('PlaylistLoader');
  1675. const {
  1676. withCredentials = false
  1677. } = options;
  1678. this.src = src;
  1679. this.vhs_ = vhs;
  1680. this.withCredentials = withCredentials;
  1681. this.addDateRangesToTextTrack_ = options.addDateRangesToTextTrack;
  1682. const vhsOptions = vhs.options_;
  1683. this.customTagParsers = vhsOptions && vhsOptions.customTagParsers || [];
  1684. this.customTagMappers = vhsOptions && vhsOptions.customTagMappers || [];
  1685. this.llhls = vhsOptions && vhsOptions.llhls;
  1686. this.dateRangesStorage_ = new DateRangesStorage(); // initialize the loader state
  1687. this.state = 'HAVE_NOTHING'; // live playlist staleness timeout
  1688. this.handleMediaupdatetimeout_ = this.handleMediaupdatetimeout_.bind(this);
  1689. this.on('mediaupdatetimeout', this.handleMediaupdatetimeout_);
  1690. this.on('loadedplaylist', this.handleLoadedPlaylist_.bind(this));
  1691. }
  1692. handleLoadedPlaylist_() {
  1693. const mediaPlaylist = this.media();
  1694. if (!mediaPlaylist) {
  1695. return;
  1696. }
  1697. this.dateRangesStorage_.setOffset(mediaPlaylist.segments);
  1698. this.dateRangesStorage_.setPendingDateRanges(mediaPlaylist.dateRanges);
  1699. const availableDateRanges = this.dateRangesStorage_.getDateRangesToProcess();
  1700. if (!availableDateRanges.length || !this.addDateRangesToTextTrack_) {
  1701. return;
  1702. }
  1703. this.addDateRangesToTextTrack_(availableDateRanges);
  1704. }
  1705. handleMediaupdatetimeout_() {
  1706. if (this.state !== 'HAVE_METADATA') {
  1707. // only refresh the media playlist if no other activity is going on
  1708. return;
  1709. }
  1710. const media = this.media();
  1711. let uri = resolveUrl(this.main.uri, media.uri);
  1712. if (this.llhls) {
  1713. uri = addLLHLSQueryDirectives(uri, media);
  1714. }
  1715. this.state = 'HAVE_CURRENT_METADATA';
  1716. this.request = this.vhs_.xhr({
  1717. uri,
  1718. withCredentials: this.withCredentials
  1719. }, (error, req) => {
  1720. // disposed
  1721. if (!this.request) {
  1722. return;
  1723. }
  1724. if (error) {
  1725. return this.playlistRequestError(this.request, this.media(), 'HAVE_METADATA');
  1726. }
  1727. this.haveMetadata({
  1728. playlistString: this.request.responseText,
  1729. url: this.media().uri,
  1730. id: this.media().id
  1731. });
  1732. });
  1733. }
  1734. playlistRequestError(xhr, playlist, startingState) {
  1735. const {
  1736. uri,
  1737. id
  1738. } = playlist; // any in-flight request is now finished
  1739. this.request = null;
  1740. if (startingState) {
  1741. this.state = startingState;
  1742. }
  1743. this.error = {
  1744. playlist: this.main.playlists[id],
  1745. status: xhr.status,
  1746. message: `HLS playlist request error at URL: ${uri}.`,
  1747. responseText: xhr.responseText,
  1748. code: xhr.status >= 500 ? 4 : 2
  1749. };
  1750. this.trigger('error');
  1751. }
  1752. parseManifest_({
  1753. url,
  1754. manifestString
  1755. }) {
  1756. return parseManifest({
  1757. onwarn: ({
  1758. message
  1759. }) => this.logger_(`m3u8-parser warn for ${url}: ${message}`),
  1760. oninfo: ({
  1761. message
  1762. }) => this.logger_(`m3u8-parser info for ${url}: ${message}`),
  1763. manifestString,
  1764. customTagParsers: this.customTagParsers,
  1765. customTagMappers: this.customTagMappers,
  1766. llhls: this.llhls
  1767. });
  1768. }
  1769. /**
  1770. * Update the playlist loader's state in response to a new or updated playlist.
  1771. *
  1772. * @param {string} [playlistString]
  1773. * Playlist string (if playlistObject is not provided)
  1774. * @param {Object} [playlistObject]
  1775. * Playlist object (if playlistString is not provided)
  1776. * @param {string} url
  1777. * URL of playlist
  1778. * @param {string} id
  1779. * ID to use for playlist
  1780. */
  1781. haveMetadata({
  1782. playlistString,
  1783. playlistObject,
  1784. url,
  1785. id
  1786. }) {
  1787. // any in-flight request is now finished
  1788. this.request = null;
  1789. this.state = 'HAVE_METADATA';
  1790. const playlist = playlistObject || this.parseManifest_({
  1791. url,
  1792. manifestString: playlistString
  1793. });
  1794. playlist.lastRequest = Date.now();
  1795. setupMediaPlaylist({
  1796. playlist,
  1797. uri: url,
  1798. id
  1799. }); // merge this playlist into the main manifest
  1800. const update = updateMain$1(this.main, playlist);
  1801. this.targetDuration = playlist.partTargetDuration || playlist.targetDuration;
  1802. this.pendingMedia_ = null;
  1803. if (update) {
  1804. this.main = update;
  1805. this.media_ = this.main.playlists[id];
  1806. } else {
  1807. this.trigger('playlistunchanged');
  1808. }
  1809. this.updateMediaUpdateTimeout_(refreshDelay(this.media(), !!update));
  1810. this.trigger('loadedplaylist');
  1811. }
  1812. /**
  1813. * Abort any outstanding work and clean up.
  1814. */
  1815. dispose() {
  1816. this.trigger('dispose');
  1817. this.stopRequest();
  1818. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  1819. window__default["default"].clearTimeout(this.finalRenditionTimeout);
  1820. this.dateRangesStorage_ = new DateRangesStorage();
  1821. this.off();
  1822. }
  1823. stopRequest() {
  1824. if (this.request) {
  1825. const oldRequest = this.request;
  1826. this.request = null;
  1827. oldRequest.onreadystatechange = null;
  1828. oldRequest.abort();
  1829. }
  1830. }
  1831. /**
  1832. * When called without any arguments, returns the currently
  1833. * active media playlist. When called with a single argument,
  1834. * triggers the playlist loader to asynchronously switch to the
  1835. * specified media playlist. Calling this method while the
  1836. * loader is in the HAVE_NOTHING causes an error to be emitted
  1837. * but otherwise has no effect.
  1838. *
  1839. * @param {Object=} playlist the parsed media playlist
  1840. * object to switch to
  1841. * @param {boolean=} shouldDelay whether we should delay the request by half target duration
  1842. *
  1843. * @return {Playlist} the current loaded media
  1844. */
  1845. media(playlist, shouldDelay) {
  1846. // getter
  1847. if (!playlist) {
  1848. return this.media_;
  1849. } // setter
  1850. if (this.state === 'HAVE_NOTHING') {
  1851. throw new Error('Cannot switch media playlist from ' + this.state);
  1852. } // find the playlist object if the target playlist has been
  1853. // specified by URI
  1854. if (typeof playlist === 'string') {
  1855. if (!this.main.playlists[playlist]) {
  1856. throw new Error('Unknown playlist URI: ' + playlist);
  1857. }
  1858. playlist = this.main.playlists[playlist];
  1859. }
  1860. window__default["default"].clearTimeout(this.finalRenditionTimeout);
  1861. if (shouldDelay) {
  1862. const delay = (playlist.partTargetDuration || playlist.targetDuration) / 2 * 1000 || 5 * 1000;
  1863. this.finalRenditionTimeout = window__default["default"].setTimeout(this.media.bind(this, playlist, false), delay);
  1864. return;
  1865. }
  1866. const startingState = this.state;
  1867. const mediaChange = !this.media_ || playlist.id !== this.media_.id;
  1868. const mainPlaylistRef = this.main.playlists[playlist.id]; // switch to fully loaded playlists immediately
  1869. if (mainPlaylistRef && mainPlaylistRef.endList || // handle the case of a playlist object (e.g., if using vhs-json with a resolved
  1870. // media playlist or, for the case of demuxed audio, a resolved audio media group)
  1871. playlist.endList && playlist.segments.length) {
  1872. // abort outstanding playlist requests
  1873. if (this.request) {
  1874. this.request.onreadystatechange = null;
  1875. this.request.abort();
  1876. this.request = null;
  1877. }
  1878. this.state = 'HAVE_METADATA';
  1879. this.media_ = playlist; // trigger media change if the active media has been updated
  1880. if (mediaChange) {
  1881. this.trigger('mediachanging');
  1882. if (startingState === 'HAVE_MAIN_MANIFEST') {
  1883. // The initial playlist was a main manifest, and the first media selected was
  1884. // also provided (in the form of a resolved playlist object) as part of the
  1885. // source object (rather than just a URL). Therefore, since the media playlist
  1886. // doesn't need to be requested, loadedmetadata won't trigger as part of the
  1887. // normal flow, and needs an explicit trigger here.
  1888. this.trigger('loadedmetadata');
  1889. } else {
  1890. this.trigger('mediachange');
  1891. }
  1892. }
  1893. return;
  1894. } // We update/set the timeout here so that live playlists
  1895. // that are not a media change will "start" the loader as expected.
  1896. // We expect that this function will start the media update timeout
  1897. // cycle again. This also prevents a playlist switch failure from
  1898. // causing us to stall during live.
  1899. this.updateMediaUpdateTimeout_(refreshDelay(playlist, true)); // switching to the active playlist is a no-op
  1900. if (!mediaChange) {
  1901. return;
  1902. }
  1903. this.state = 'SWITCHING_MEDIA'; // there is already an outstanding playlist request
  1904. if (this.request) {
  1905. if (playlist.resolvedUri === this.request.url) {
  1906. // requesting to switch to the same playlist multiple times
  1907. // has no effect after the first
  1908. return;
  1909. }
  1910. this.request.onreadystatechange = null;
  1911. this.request.abort();
  1912. this.request = null;
  1913. } // request the new playlist
  1914. if (this.media_) {
  1915. this.trigger('mediachanging');
  1916. }
  1917. this.pendingMedia_ = playlist;
  1918. this.request = this.vhs_.xhr({
  1919. uri: playlist.resolvedUri,
  1920. withCredentials: this.withCredentials
  1921. }, (error, req) => {
  1922. // disposed
  1923. if (!this.request) {
  1924. return;
  1925. }
  1926. playlist.lastRequest = Date.now();
  1927. playlist.resolvedUri = resolveManifestRedirect(playlist.resolvedUri, req);
  1928. if (error) {
  1929. return this.playlistRequestError(this.request, playlist, startingState);
  1930. }
  1931. this.haveMetadata({
  1932. playlistString: req.responseText,
  1933. url: playlist.uri,
  1934. id: playlist.id
  1935. }); // fire loadedmetadata the first time a media playlist is loaded
  1936. if (startingState === 'HAVE_MAIN_MANIFEST') {
  1937. this.trigger('loadedmetadata');
  1938. } else {
  1939. this.trigger('mediachange');
  1940. }
  1941. });
  1942. }
  1943. /**
  1944. * pause loading of the playlist
  1945. */
  1946. pause() {
  1947. if (this.mediaUpdateTimeout) {
  1948. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  1949. this.mediaUpdateTimeout = null;
  1950. }
  1951. this.stopRequest();
  1952. if (this.state === 'HAVE_NOTHING') {
  1953. // If we pause the loader before any data has been retrieved, its as if we never
  1954. // started, so reset to an unstarted state.
  1955. this.started = false;
  1956. } // Need to restore state now that no activity is happening
  1957. if (this.state === 'SWITCHING_MEDIA') {
  1958. // if the loader was in the process of switching media, it should either return to
  1959. // HAVE_MAIN_MANIFEST or HAVE_METADATA depending on if the loader has loaded a media
  1960. // playlist yet. This is determined by the existence of loader.media_
  1961. if (this.media_) {
  1962. this.state = 'HAVE_METADATA';
  1963. } else {
  1964. this.state = 'HAVE_MAIN_MANIFEST';
  1965. }
  1966. } else if (this.state === 'HAVE_CURRENT_METADATA') {
  1967. this.state = 'HAVE_METADATA';
  1968. }
  1969. }
  1970. /**
  1971. * start loading of the playlist
  1972. */
  1973. load(shouldDelay) {
  1974. if (this.mediaUpdateTimeout) {
  1975. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  1976. this.mediaUpdateTimeout = null;
  1977. }
  1978. const media = this.media();
  1979. if (shouldDelay) {
  1980. const delay = media ? (media.partTargetDuration || media.targetDuration) / 2 * 1000 : 5 * 1000;
  1981. this.mediaUpdateTimeout = window__default["default"].setTimeout(() => {
  1982. this.mediaUpdateTimeout = null;
  1983. this.load();
  1984. }, delay);
  1985. return;
  1986. }
  1987. if (!this.started) {
  1988. this.start();
  1989. return;
  1990. }
  1991. if (media && !media.endList) {
  1992. this.trigger('mediaupdatetimeout');
  1993. } else {
  1994. this.trigger('loadedplaylist');
  1995. }
  1996. }
  1997. updateMediaUpdateTimeout_(delay) {
  1998. if (this.mediaUpdateTimeout) {
  1999. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  2000. this.mediaUpdateTimeout = null;
  2001. } // we only have use mediaupdatetimeout for live playlists.
  2002. if (!this.media() || this.media().endList) {
  2003. return;
  2004. }
  2005. this.mediaUpdateTimeout = window__default["default"].setTimeout(() => {
  2006. this.mediaUpdateTimeout = null;
  2007. this.trigger('mediaupdatetimeout');
  2008. this.updateMediaUpdateTimeout_(delay);
  2009. }, delay);
  2010. }
  2011. /**
  2012. * start loading of the playlist
  2013. */
  2014. start() {
  2015. this.started = true;
  2016. if (typeof this.src === 'object') {
  2017. // in the case of an entirely constructed manifest object (meaning there's no actual
  2018. // manifest on a server), default the uri to the page's href
  2019. if (!this.src.uri) {
  2020. this.src.uri = window__default["default"].location.href;
  2021. } // resolvedUri is added on internally after the initial request. Since there's no
  2022. // request for pre-resolved manifests, add on resolvedUri here.
  2023. this.src.resolvedUri = this.src.uri; // Since a manifest object was passed in as the source (instead of a URL), the first
  2024. // request can be skipped (since the top level of the manifest, at a minimum, is
  2025. // already available as a parsed manifest object). However, if the manifest object
  2026. // represents a main playlist, some media playlists may need to be resolved before
  2027. // the starting segment list is available. Therefore, go directly to setup of the
  2028. // initial playlist, and let the normal flow continue from there.
  2029. //
  2030. // Note that the call to setup is asynchronous, as other sections of VHS may assume
  2031. // that the first request is asynchronous.
  2032. setTimeout(() => {
  2033. this.setupInitialPlaylist(this.src);
  2034. }, 0);
  2035. return;
  2036. } // request the specified URL
  2037. this.request = this.vhs_.xhr({
  2038. uri: this.src,
  2039. withCredentials: this.withCredentials
  2040. }, (error, req) => {
  2041. // disposed
  2042. if (!this.request) {
  2043. return;
  2044. } // clear the loader's request reference
  2045. this.request = null;
  2046. if (error) {
  2047. this.error = {
  2048. status: req.status,
  2049. message: `HLS playlist request error at URL: ${this.src}.`,
  2050. responseText: req.responseText,
  2051. // MEDIA_ERR_NETWORK
  2052. code: 2
  2053. };
  2054. if (this.state === 'HAVE_NOTHING') {
  2055. this.started = false;
  2056. }
  2057. return this.trigger('error');
  2058. }
  2059. this.src = resolveManifestRedirect(this.src, req);
  2060. const manifest = this.parseManifest_({
  2061. manifestString: req.responseText,
  2062. url: this.src
  2063. });
  2064. this.setupInitialPlaylist(manifest);
  2065. });
  2066. }
  2067. srcUri() {
  2068. return typeof this.src === 'string' ? this.src : this.src.uri;
  2069. }
  2070. /**
  2071. * Given a manifest object that's either a main or media playlist, trigger the proper
  2072. * events and set the state of the playlist loader.
  2073. *
  2074. * If the manifest object represents a main playlist, `loadedplaylist` will be
  2075. * triggered to allow listeners to select a playlist. If none is selected, the loader
  2076. * will default to the first one in the playlists array.
  2077. *
  2078. * If the manifest object represents a media playlist, `loadedplaylist` will be
  2079. * triggered followed by `loadedmetadata`, as the only available playlist is loaded.
  2080. *
  2081. * In the case of a media playlist, a main playlist object wrapper with one playlist
  2082. * will be created so that all logic can handle playlists in the same fashion (as an
  2083. * assumed manifest object schema).
  2084. *
  2085. * @param {Object} manifest
  2086. * The parsed manifest object
  2087. */
  2088. setupInitialPlaylist(manifest) {
  2089. this.state = 'HAVE_MAIN_MANIFEST';
  2090. if (manifest.playlists) {
  2091. this.main = manifest;
  2092. addPropertiesToMain(this.main, this.srcUri()); // If the initial main playlist has playlists wtih segments already resolved,
  2093. // then resolve URIs in advance, as they are usually done after a playlist request,
  2094. // which may not happen if the playlist is resolved.
  2095. manifest.playlists.forEach(playlist => {
  2096. playlist.segments = getAllSegments(playlist);
  2097. playlist.segments.forEach(segment => {
  2098. resolveSegmentUris(segment, playlist.resolvedUri);
  2099. });
  2100. });
  2101. this.trigger('loadedplaylist');
  2102. if (!this.request) {
  2103. // no media playlist was specifically selected so start
  2104. // from the first listed one
  2105. this.media(this.main.playlists[0]);
  2106. }
  2107. return;
  2108. } // In order to support media playlists passed in as vhs-json, the case where the uri
  2109. // is not provided as part of the manifest should be considered, and an appropriate
  2110. // default used.
  2111. const uri = this.srcUri() || window__default["default"].location.href;
  2112. this.main = mainForMedia(manifest, uri);
  2113. this.haveMetadata({
  2114. playlistObject: manifest,
  2115. url: uri,
  2116. id: this.main.playlists[0].id
  2117. });
  2118. this.trigger('loadedmetadata');
  2119. }
  2120. /**
  2121. * Updates or deletes a preexisting pathway clone.
  2122. * Ensures that all playlists related to the old pathway clone are
  2123. * either updated or deleted.
  2124. *
  2125. * @param {Object} clone On update, the pathway clone object for the newly updated pathway clone.
  2126. * On delete, the old pathway clone object to be deleted.
  2127. * @param {boolean} isUpdate True if the pathway is to be updated,
  2128. * false if it is meant to be deleted.
  2129. */
  2130. updateOrDeleteClone(clone, isUpdate) {
  2131. const main = this.main;
  2132. const pathway = clone.ID;
  2133. let i = main.playlists.length; // Iterate backwards through the playlist so we can remove playlists if necessary.
  2134. while (i--) {
  2135. const p = main.playlists[i];
  2136. if (p.attributes['PATHWAY-ID'] === pathway) {
  2137. const oldPlaylistUri = p.resolvedUri;
  2138. const oldPlaylistId = p.id; // update the indexed playlist and add new playlists by ID and URI
  2139. if (isUpdate) {
  2140. const newPlaylistUri = this.createCloneURI_(p.resolvedUri, clone);
  2141. const newPlaylistId = createPlaylistID(pathway, newPlaylistUri);
  2142. const attributes = this.createCloneAttributes_(pathway, p.attributes);
  2143. const updatedPlaylist = this.createClonePlaylist_(p, newPlaylistId, clone, attributes);
  2144. main.playlists[i] = updatedPlaylist;
  2145. main.playlists[newPlaylistId] = updatedPlaylist;
  2146. main.playlists[newPlaylistUri] = updatedPlaylist;
  2147. } else {
  2148. // Remove the indexed playlist.
  2149. main.playlists.splice(i, 1);
  2150. } // Remove playlists by the old ID and URI.
  2151. delete main.playlists[oldPlaylistId];
  2152. delete main.playlists[oldPlaylistUri];
  2153. }
  2154. }
  2155. this.updateOrDeleteCloneMedia(clone, isUpdate);
  2156. }
  2157. /**
  2158. * Updates or deletes media data based on the pathway clone object.
  2159. * Due to the complexity of the media groups and playlists, in all cases
  2160. * we remove all of the old media groups and playlists.
  2161. * On updates, we then create new media groups and playlists based on the
  2162. * new pathway clone object.
  2163. *
  2164. * @param {Object} clone The pathway clone object for the newly updated pathway clone.
  2165. * @param {boolean} isUpdate True if the pathway is to be updated,
  2166. * false if it is meant to be deleted.
  2167. */
  2168. updateOrDeleteCloneMedia(clone, isUpdate) {
  2169. const main = this.main;
  2170. const id = clone.ID;
  2171. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
  2172. if (!main.mediaGroups[mediaType] || !main.mediaGroups[mediaType][id]) {
  2173. return;
  2174. }
  2175. for (const groupKey in main.mediaGroups[mediaType]) {
  2176. // Remove all media playlists for the media group for this pathway clone.
  2177. if (groupKey === id) {
  2178. for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
  2179. const oldMedia = main.mediaGroups[mediaType][groupKey][labelKey];
  2180. oldMedia.playlists.forEach((p, i) => {
  2181. const oldMediaPlaylist = main.playlists[p.id];
  2182. const oldPlaylistId = oldMediaPlaylist.id;
  2183. const oldPlaylistUri = oldMediaPlaylist.resolvedUri;
  2184. delete main.playlists[oldPlaylistId];
  2185. delete main.playlists[oldPlaylistUri];
  2186. });
  2187. } // Delete the old media group.
  2188. delete main.mediaGroups[mediaType][groupKey];
  2189. }
  2190. }
  2191. }); // Create the new media groups and playlists if there is an update.
  2192. if (isUpdate) {
  2193. this.createClonedMediaGroups_(clone);
  2194. }
  2195. }
  2196. /**
  2197. * Given a pathway clone object, clones all necessary playlists.
  2198. *
  2199. * @param {Object} clone The pathway clone object.
  2200. * @param {Object} basePlaylist The original playlist to clone from.
  2201. */
  2202. addClonePathway(clone, basePlaylist = {}) {
  2203. const main = this.main;
  2204. const index = main.playlists.length;
  2205. const uri = this.createCloneURI_(basePlaylist.resolvedUri, clone);
  2206. const playlistId = createPlaylistID(clone.ID, uri);
  2207. const attributes = this.createCloneAttributes_(clone.ID, basePlaylist.attributes);
  2208. const playlist = this.createClonePlaylist_(basePlaylist, playlistId, clone, attributes);
  2209. main.playlists[index] = playlist; // add playlist by ID and URI
  2210. main.playlists[playlistId] = playlist;
  2211. main.playlists[uri] = playlist;
  2212. this.createClonedMediaGroups_(clone);
  2213. }
  2214. /**
  2215. * Given a pathway clone object we create clones of all media.
  2216. * In this function, all necessary information and updated playlists
  2217. * are added to the `mediaGroup` object.
  2218. * Playlists are also added to the `playlists` array so the media groups
  2219. * will be properly linked.
  2220. *
  2221. * @param {Object} clone The pathway clone object.
  2222. */
  2223. createClonedMediaGroups_(clone) {
  2224. const id = clone.ID;
  2225. const baseID = clone['BASE-ID'];
  2226. const main = this.main;
  2227. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
  2228. // If the media type doesn't exist, or there is already a clone, skip
  2229. // to the next media type.
  2230. if (!main.mediaGroups[mediaType] || main.mediaGroups[mediaType][id]) {
  2231. return;
  2232. }
  2233. for (const groupKey in main.mediaGroups[mediaType]) {
  2234. if (groupKey === baseID) {
  2235. // Create the group.
  2236. main.mediaGroups[mediaType][id] = {};
  2237. } else {
  2238. // There is no need to iterate over label keys in this case.
  2239. continue;
  2240. }
  2241. for (const labelKey in main.mediaGroups[mediaType][groupKey]) {
  2242. const oldMedia = main.mediaGroups[mediaType][groupKey][labelKey];
  2243. main.mediaGroups[mediaType][id][labelKey] = _extends__default["default"]({}, oldMedia);
  2244. const newMedia = main.mediaGroups[mediaType][id][labelKey]; // update URIs on the media
  2245. const newUri = this.createCloneURI_(oldMedia.resolvedUri, clone);
  2246. newMedia.resolvedUri = newUri;
  2247. newMedia.uri = newUri; // Reset playlists in the new media group.
  2248. newMedia.playlists = []; // Create new playlists in the newly cloned media group.
  2249. oldMedia.playlists.forEach((p, i) => {
  2250. const oldMediaPlaylist = main.playlists[p.id];
  2251. const group = groupID(mediaType, id, labelKey);
  2252. const newPlaylistID = createPlaylistID(id, group); // Check to see if it already exists
  2253. if (oldMediaPlaylist && !main.playlists[newPlaylistID]) {
  2254. const newMediaPlaylist = this.createClonePlaylist_(oldMediaPlaylist, newPlaylistID, clone);
  2255. const newPlaylistUri = newMediaPlaylist.resolvedUri;
  2256. main.playlists[newPlaylistID] = newMediaPlaylist;
  2257. main.playlists[newPlaylistUri] = newMediaPlaylist;
  2258. }
  2259. newMedia.playlists[i] = this.createClonePlaylist_(p, newPlaylistID, clone);
  2260. });
  2261. }
  2262. }
  2263. });
  2264. }
  2265. /**
  2266. * Using the original playlist to be cloned, and the pathway clone object
  2267. * information, we create a new playlist.
  2268. *
  2269. * @param {Object} basePlaylist The original playlist to be cloned from.
  2270. * @param {string} id The desired id of the newly cloned playlist.
  2271. * @param {Object} clone The pathway clone object.
  2272. * @param {Object} attributes An optional object to populate the `attributes` property in the playlist.
  2273. *
  2274. * @return {Object} The combined cloned playlist.
  2275. */
  2276. createClonePlaylist_(basePlaylist, id, clone, attributes) {
  2277. const uri = this.createCloneURI_(basePlaylist.resolvedUri, clone);
  2278. const newProps = {
  2279. resolvedUri: uri,
  2280. uri,
  2281. id
  2282. }; // Remove all segments from previous playlist in the clone.
  2283. if (basePlaylist.segments) {
  2284. newProps.segments = [];
  2285. }
  2286. if (attributes) {
  2287. newProps.attributes = attributes;
  2288. }
  2289. return merge(basePlaylist, newProps);
  2290. }
  2291. /**
  2292. * Generates an updated URI for a cloned pathway based on the original
  2293. * pathway's URI and the paramaters from the pathway clone object in the
  2294. * content steering server response.
  2295. *
  2296. * @param {string} baseUri URI to be updated in the cloned pathway.
  2297. * @param {Object} clone The pathway clone object.
  2298. *
  2299. * @return {string} The updated URI for the cloned pathway.
  2300. */
  2301. createCloneURI_(baseURI, clone) {
  2302. const uri = new URL(baseURI);
  2303. uri.hostname = clone['URI-REPLACEMENT'].HOST;
  2304. const params = clone['URI-REPLACEMENT'].PARAMS; // Add params to the cloned URL.
  2305. for (const key of Object.keys(params)) {
  2306. uri.searchParams.set(key, params[key]);
  2307. }
  2308. return uri.href;
  2309. }
  2310. /**
  2311. * Helper function to create the attributes needed for the new clone.
  2312. * This mainly adds the necessary media attributes.
  2313. *
  2314. * @param {string} id The pathway clone object ID.
  2315. * @param {Object} oldAttributes The old attributes to compare to.
  2316. * @return {Object} The new attributes to add to the playlist.
  2317. */
  2318. createCloneAttributes_(id, oldAttributes) {
  2319. const attributes = {
  2320. ['PATHWAY-ID']: id
  2321. };
  2322. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(mediaType => {
  2323. if (oldAttributes[mediaType]) {
  2324. attributes[mediaType] = id;
  2325. }
  2326. });
  2327. return attributes;
  2328. }
  2329. /**
  2330. * Returns the key ID set from a playlist
  2331. *
  2332. * @param {playlist} playlist to fetch the key ID set from.
  2333. * @return a Set of 32 digit hex strings that represent the unique keyIds for that playlist.
  2334. */
  2335. getKeyIdSet(playlist) {
  2336. if (playlist.contentProtection) {
  2337. const keyIds = new Set();
  2338. for (const keysystem in playlist.contentProtection) {
  2339. const keyId = playlist.contentProtection[keysystem].attributes.keyId;
  2340. if (keyId) {
  2341. keyIds.add(keyId.toLowerCase());
  2342. }
  2343. }
  2344. return keyIds;
  2345. }
  2346. }
  2347. }
  2348. /**
  2349. * @file xhr.js
  2350. */
  2351. const {
  2352. xhr: videojsXHR
  2353. } = videojs__default["default"];
  2354. const callbackWrapper = function (request, error, response, callback) {
  2355. const reqResponse = request.responseType === 'arraybuffer' ? request.response : request.responseText;
  2356. if (!error && reqResponse) {
  2357. request.responseTime = Date.now();
  2358. request.roundTripTime = request.responseTime - request.requestTime;
  2359. request.bytesReceived = reqResponse.byteLength || reqResponse.length;
  2360. if (!request.bandwidth) {
  2361. request.bandwidth = Math.floor(request.bytesReceived / request.roundTripTime * 8 * 1000);
  2362. }
  2363. }
  2364. if (response.headers) {
  2365. request.responseHeaders = response.headers;
  2366. } // videojs.xhr now uses a specific code on the error
  2367. // object to signal that a request has timed out instead
  2368. // of setting a boolean on the request object
  2369. if (error && error.code === 'ETIMEDOUT') {
  2370. request.timedout = true;
  2371. } // videojs.xhr no longer considers status codes outside of 200 and 0
  2372. // (for file uris) to be errors, but the old XHR did, so emulate that
  2373. // behavior. Status 206 may be used in response to byterange requests.
  2374. if (!error && !request.aborted && response.statusCode !== 200 && response.statusCode !== 206 && response.statusCode !== 0) {
  2375. error = new Error('XHR Failed with a response of: ' + (request && (reqResponse || request.responseText)));
  2376. }
  2377. callback(error, request);
  2378. };
  2379. /**
  2380. * Iterates over the request hooks Set and calls them in order
  2381. *
  2382. * @param {Set} hooks the hook Set to iterate over
  2383. * @param {Object} options the request options to pass to the xhr wrapper
  2384. * @return the callback hook function return value, the modified or new options Object.
  2385. */
  2386. const callAllRequestHooks = (requestSet, options) => {
  2387. if (!requestSet || !requestSet.size) {
  2388. return;
  2389. }
  2390. let newOptions = options;
  2391. requestSet.forEach(requestCallback => {
  2392. newOptions = requestCallback(newOptions);
  2393. });
  2394. return newOptions;
  2395. };
  2396. /**
  2397. * Iterates over the response hooks Set and calls them in order.
  2398. *
  2399. * @param {Set} hooks the hook Set to iterate over
  2400. * @param {Object} request the xhr request object
  2401. * @param {Object} error the xhr error object
  2402. * @param {Object} response the xhr response object
  2403. */
  2404. const callAllResponseHooks = (responseSet, request, error, response) => {
  2405. if (!responseSet || !responseSet.size) {
  2406. return;
  2407. }
  2408. responseSet.forEach(responseCallback => {
  2409. responseCallback(request, error, response);
  2410. });
  2411. };
  2412. const xhrFactory = function () {
  2413. const xhr = function XhrFunction(options, callback) {
  2414. // Add a default timeout
  2415. options = merge({
  2416. timeout: 45e3
  2417. }, options); // Allow an optional user-specified function to modify the option
  2418. // object before we construct the xhr request
  2419. // TODO: Remove beforeRequest in the next major release.
  2420. const beforeRequest = XhrFunction.beforeRequest || videojs__default["default"].Vhs.xhr.beforeRequest; // onRequest and onResponse hooks as a Set, at either the player or global level.
  2421. // TODO: new Set added here for beforeRequest alias. Remove this when beforeRequest is removed.
  2422. const _requestCallbackSet = XhrFunction._requestCallbackSet || videojs__default["default"].Vhs.xhr._requestCallbackSet || new Set();
  2423. const _responseCallbackSet = XhrFunction._responseCallbackSet || videojs__default["default"].Vhs.xhr._responseCallbackSet;
  2424. if (beforeRequest && typeof beforeRequest === 'function') {
  2425. videojs__default["default"].log.warn('beforeRequest is deprecated, use onRequest instead.');
  2426. _requestCallbackSet.add(beforeRequest);
  2427. } // Use the standard videojs.xhr() method unless `videojs.Vhs.xhr` has been overriden
  2428. // TODO: switch back to videojs.Vhs.xhr.name === 'XhrFunction' when we drop IE11
  2429. const xhrMethod = videojs__default["default"].Vhs.xhr.original === true ? videojsXHR : videojs__default["default"].Vhs.xhr; // call all registered onRequest hooks, assign new options.
  2430. const beforeRequestOptions = callAllRequestHooks(_requestCallbackSet, options); // Remove the beforeRequest function from the hooks set so stale beforeRequest functions are not called.
  2431. _requestCallbackSet.delete(beforeRequest); // xhrMethod will call XMLHttpRequest.open and XMLHttpRequest.send
  2432. const request = xhrMethod(beforeRequestOptions || options, function (error, response) {
  2433. // call all registered onResponse hooks
  2434. callAllResponseHooks(_responseCallbackSet, request, error, response);
  2435. return callbackWrapper(request, error, response, callback);
  2436. });
  2437. const originalAbort = request.abort;
  2438. request.abort = function () {
  2439. request.aborted = true;
  2440. return originalAbort.apply(request, arguments);
  2441. };
  2442. request.uri = options.uri;
  2443. request.requestTime = Date.now();
  2444. return request;
  2445. };
  2446. xhr.original = true;
  2447. return xhr;
  2448. };
  2449. /**
  2450. * Turns segment byterange into a string suitable for use in
  2451. * HTTP Range requests
  2452. *
  2453. * @param {Object} byterange - an object with two values defining the start and end
  2454. * of a byte-range
  2455. */
  2456. const byterangeStr = function (byterange) {
  2457. // `byterangeEnd` is one less than `offset + length` because the HTTP range
  2458. // header uses inclusive ranges
  2459. let byterangeEnd;
  2460. const byterangeStart = byterange.offset;
  2461. if (typeof byterange.offset === 'bigint' || typeof byterange.length === 'bigint') {
  2462. byterangeEnd = window__default["default"].BigInt(byterange.offset) + window__default["default"].BigInt(byterange.length) - window__default["default"].BigInt(1);
  2463. } else {
  2464. byterangeEnd = byterange.offset + byterange.length - 1;
  2465. }
  2466. return 'bytes=' + byterangeStart + '-' + byterangeEnd;
  2467. };
  2468. /**
  2469. * Defines headers for use in the xhr request for a particular segment.
  2470. *
  2471. * @param {Object} segment - a simplified copy of the segmentInfo object
  2472. * from SegmentLoader
  2473. */
  2474. const segmentXhrHeaders = function (segment) {
  2475. const headers = {};
  2476. if (segment.byterange) {
  2477. headers.Range = byterangeStr(segment.byterange);
  2478. }
  2479. return headers;
  2480. };
  2481. /**
  2482. * @file bin-utils.js
  2483. */
  2484. /**
  2485. * convert a TimeRange to text
  2486. *
  2487. * @param {TimeRange} range the timerange to use for conversion
  2488. * @param {number} i the iterator on the range to convert
  2489. * @return {string} the range in string format
  2490. */
  2491. const textRange = function (range, i) {
  2492. return range.start(i) + '-' + range.end(i);
  2493. };
  2494. /**
  2495. * format a number as hex string
  2496. *
  2497. * @param {number} e The number
  2498. * @param {number} i the iterator
  2499. * @return {string} the hex formatted number as a string
  2500. */
  2501. const formatHexString = function (e, i) {
  2502. const value = e.toString(16);
  2503. return '00'.substring(0, 2 - value.length) + value + (i % 2 ? ' ' : '');
  2504. };
  2505. const formatAsciiString = function (e) {
  2506. if (e >= 0x20 && e < 0x7e) {
  2507. return String.fromCharCode(e);
  2508. }
  2509. return '.';
  2510. };
  2511. /**
  2512. * Creates an object for sending to a web worker modifying properties that are TypedArrays
  2513. * into a new object with seperated properties for the buffer, byteOffset, and byteLength.
  2514. *
  2515. * @param {Object} message
  2516. * Object of properties and values to send to the web worker
  2517. * @return {Object}
  2518. * Modified message with TypedArray values expanded
  2519. * @function createTransferableMessage
  2520. */
  2521. const createTransferableMessage = function (message) {
  2522. const transferable = {};
  2523. Object.keys(message).forEach(key => {
  2524. const value = message[key];
  2525. if (byteHelpers.isArrayBufferView(value)) {
  2526. transferable[key] = {
  2527. bytes: value.buffer,
  2528. byteOffset: value.byteOffset,
  2529. byteLength: value.byteLength
  2530. };
  2531. } else {
  2532. transferable[key] = value;
  2533. }
  2534. });
  2535. return transferable;
  2536. };
  2537. /**
  2538. * Returns a unique string identifier for a media initialization
  2539. * segment.
  2540. *
  2541. * @param {Object} initSegment
  2542. * the init segment object.
  2543. *
  2544. * @return {string} the generated init segment id
  2545. */
  2546. const initSegmentId = function (initSegment) {
  2547. const byterange = initSegment.byterange || {
  2548. length: Infinity,
  2549. offset: 0
  2550. };
  2551. return [byterange.length, byterange.offset, initSegment.resolvedUri].join(',');
  2552. };
  2553. /**
  2554. * Returns a unique string identifier for a media segment key.
  2555. *
  2556. * @param {Object} key the encryption key
  2557. * @return {string} the unique id for the media segment key.
  2558. */
  2559. const segmentKeyId = function (key) {
  2560. return key.resolvedUri;
  2561. };
  2562. /**
  2563. * utils to help dump binary data to the console
  2564. *
  2565. * @param {Array|TypedArray} data
  2566. * data to dump to a string
  2567. *
  2568. * @return {string} the data as a hex string.
  2569. */
  2570. const hexDump = data => {
  2571. const bytes = Array.prototype.slice.call(data);
  2572. const step = 16;
  2573. let result = '';
  2574. let hex;
  2575. let ascii;
  2576. for (let j = 0; j < bytes.length / step; j++) {
  2577. hex = bytes.slice(j * step, j * step + step).map(formatHexString).join('');
  2578. ascii = bytes.slice(j * step, j * step + step).map(formatAsciiString).join('');
  2579. result += hex + ' ' + ascii + '\n';
  2580. }
  2581. return result;
  2582. };
  2583. const tagDump = ({
  2584. bytes
  2585. }) => hexDump(bytes);
  2586. const textRanges = ranges => {
  2587. let result = '';
  2588. let i;
  2589. for (i = 0; i < ranges.length; i++) {
  2590. result += textRange(ranges, i) + ' ';
  2591. }
  2592. return result;
  2593. };
  2594. var utils = /*#__PURE__*/Object.freeze({
  2595. __proto__: null,
  2596. createTransferableMessage: createTransferableMessage,
  2597. initSegmentId: initSegmentId,
  2598. segmentKeyId: segmentKeyId,
  2599. hexDump: hexDump,
  2600. tagDump: tagDump,
  2601. textRanges: textRanges
  2602. });
  2603. // TODO handle fmp4 case where the timing info is accurate and doesn't involve transmux
  2604. // 25% was arbitrarily chosen, and may need to be refined over time.
  2605. const SEGMENT_END_FUDGE_PERCENT = 0.25;
  2606. /**
  2607. * Converts a player time (any time that can be gotten/set from player.currentTime(),
  2608. * e.g., any time within player.seekable().start(0) to player.seekable().end(0)) to a
  2609. * program time (any time referencing the real world (e.g., EXT-X-PROGRAM-DATE-TIME)).
  2610. *
  2611. * The containing segment is required as the EXT-X-PROGRAM-DATE-TIME serves as an "anchor
  2612. * point" (a point where we have a mapping from program time to player time, with player
  2613. * time being the post transmux start of the segment).
  2614. *
  2615. * For more details, see [this doc](../../docs/program-time-from-player-time.md).
  2616. *
  2617. * @param {number} playerTime the player time
  2618. * @param {Object} segment the segment which contains the player time
  2619. * @return {Date} program time
  2620. */
  2621. const playerTimeToProgramTime = (playerTime, segment) => {
  2622. if (!segment.dateTimeObject) {
  2623. // Can't convert without an "anchor point" for the program time (i.e., a time that can
  2624. // be used to map the start of a segment with a real world time).
  2625. return null;
  2626. }
  2627. const transmuxerPrependedSeconds = segment.videoTimingInfo.transmuxerPrependedSeconds;
  2628. const transmuxedStart = segment.videoTimingInfo.transmuxedPresentationStart; // get the start of the content from before old content is prepended
  2629. const startOfSegment = transmuxedStart + transmuxerPrependedSeconds;
  2630. const offsetFromSegmentStart = playerTime - startOfSegment;
  2631. return new Date(segment.dateTimeObject.getTime() + offsetFromSegmentStart * 1000);
  2632. };
  2633. const originalSegmentVideoDuration = videoTimingInfo => {
  2634. return videoTimingInfo.transmuxedPresentationEnd - videoTimingInfo.transmuxedPresentationStart - videoTimingInfo.transmuxerPrependedSeconds;
  2635. };
  2636. /**
  2637. * Finds a segment that contains the time requested given as an ISO-8601 string. The
  2638. * returned segment might be an estimate or an accurate match.
  2639. *
  2640. * @param {string} programTime The ISO-8601 programTime to find a match for
  2641. * @param {Object} playlist A playlist object to search within
  2642. */
  2643. const findSegmentForProgramTime = (programTime, playlist) => {
  2644. // Assumptions:
  2645. // - verifyProgramDateTimeTags has already been run
  2646. // - live streams have been started
  2647. let dateTimeObject;
  2648. try {
  2649. dateTimeObject = new Date(programTime);
  2650. } catch (e) {
  2651. return null;
  2652. }
  2653. if (!playlist || !playlist.segments || playlist.segments.length === 0) {
  2654. return null;
  2655. }
  2656. let segment = playlist.segments[0];
  2657. if (dateTimeObject < new Date(segment.dateTimeObject)) {
  2658. // Requested time is before stream start.
  2659. return null;
  2660. }
  2661. for (let i = 0; i < playlist.segments.length - 1; i++) {
  2662. segment = playlist.segments[i];
  2663. const nextSegmentStart = new Date(playlist.segments[i + 1].dateTimeObject);
  2664. if (dateTimeObject < nextSegmentStart) {
  2665. break;
  2666. }
  2667. }
  2668. const lastSegment = playlist.segments[playlist.segments.length - 1];
  2669. const lastSegmentStart = lastSegment.dateTimeObject;
  2670. const lastSegmentDuration = lastSegment.videoTimingInfo ? originalSegmentVideoDuration(lastSegment.videoTimingInfo) : lastSegment.duration + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT;
  2671. const lastSegmentEnd = new Date(lastSegmentStart.getTime() + lastSegmentDuration * 1000);
  2672. if (dateTimeObject > lastSegmentEnd) {
  2673. // Beyond the end of the stream, or our best guess of the end of the stream.
  2674. return null;
  2675. }
  2676. if (dateTimeObject > new Date(lastSegmentStart)) {
  2677. segment = lastSegment;
  2678. }
  2679. return {
  2680. segment,
  2681. estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : Playlist.duration(playlist, playlist.mediaSequence + playlist.segments.indexOf(segment)),
  2682. // Although, given that all segments have accurate date time objects, the segment
  2683. // selected should be accurate, unless the video has been transmuxed at some point
  2684. // (determined by the presence of the videoTimingInfo object), the segment's "player
  2685. // time" (the start time in the player) can't be considered accurate.
  2686. type: segment.videoTimingInfo ? 'accurate' : 'estimate'
  2687. };
  2688. };
  2689. /**
  2690. * Finds a segment that contains the given player time(in seconds).
  2691. *
  2692. * @param {number} time The player time to find a match for
  2693. * @param {Object} playlist A playlist object to search within
  2694. */
  2695. const findSegmentForPlayerTime = (time, playlist) => {
  2696. // Assumptions:
  2697. // - there will always be a segment.duration
  2698. // - we can start from zero
  2699. // - segments are in time order
  2700. if (!playlist || !playlist.segments || playlist.segments.length === 0) {
  2701. return null;
  2702. }
  2703. let segmentEnd = 0;
  2704. let segment;
  2705. for (let i = 0; i < playlist.segments.length; i++) {
  2706. segment = playlist.segments[i]; // videoTimingInfo is set after the segment is downloaded and transmuxed, and
  2707. // should contain the most accurate values we have for the segment's player times.
  2708. //
  2709. // Use the accurate transmuxedPresentationEnd value if it is available, otherwise fall
  2710. // back to an estimate based on the manifest derived (inaccurate) segment.duration, to
  2711. // calculate an end value.
  2712. segmentEnd = segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationEnd : segmentEnd + segment.duration;
  2713. if (time <= segmentEnd) {
  2714. break;
  2715. }
  2716. }
  2717. const lastSegment = playlist.segments[playlist.segments.length - 1];
  2718. if (lastSegment.videoTimingInfo && lastSegment.videoTimingInfo.transmuxedPresentationEnd < time) {
  2719. // The time requested is beyond the stream end.
  2720. return null;
  2721. }
  2722. if (time > segmentEnd) {
  2723. // The time is within or beyond the last segment.
  2724. //
  2725. // Check to see if the time is beyond a reasonable guess of the end of the stream.
  2726. if (time > segmentEnd + lastSegment.duration * SEGMENT_END_FUDGE_PERCENT) {
  2727. // Technically, because the duration value is only an estimate, the time may still
  2728. // exist in the last segment, however, there isn't enough information to make even
  2729. // a reasonable estimate.
  2730. return null;
  2731. }
  2732. segment = lastSegment;
  2733. }
  2734. return {
  2735. segment,
  2736. estimatedStart: segment.videoTimingInfo ? segment.videoTimingInfo.transmuxedPresentationStart : segmentEnd - segment.duration,
  2737. // Because videoTimingInfo is only set after transmux, it is the only way to get
  2738. // accurate timing values.
  2739. type: segment.videoTimingInfo ? 'accurate' : 'estimate'
  2740. };
  2741. };
  2742. /**
  2743. * Gives the offset of the comparisonTimestamp from the programTime timestamp in seconds.
  2744. * If the offset returned is positive, the programTime occurs after the
  2745. * comparisonTimestamp.
  2746. * If the offset is negative, the programTime occurs before the comparisonTimestamp.
  2747. *
  2748. * @param {string} comparisonTimeStamp An ISO-8601 timestamp to compare against
  2749. * @param {string} programTime The programTime as an ISO-8601 string
  2750. * @return {number} offset
  2751. */
  2752. const getOffsetFromTimestamp = (comparisonTimeStamp, programTime) => {
  2753. let segmentDateTime;
  2754. let programDateTime;
  2755. try {
  2756. segmentDateTime = new Date(comparisonTimeStamp);
  2757. programDateTime = new Date(programTime);
  2758. } catch (e) {// TODO handle error
  2759. }
  2760. const segmentTimeEpoch = segmentDateTime.getTime();
  2761. const programTimeEpoch = programDateTime.getTime();
  2762. return (programTimeEpoch - segmentTimeEpoch) / 1000;
  2763. };
  2764. /**
  2765. * Checks that all segments in this playlist have programDateTime tags.
  2766. *
  2767. * @param {Object} playlist A playlist object
  2768. */
  2769. const verifyProgramDateTimeTags = playlist => {
  2770. if (!playlist.segments || playlist.segments.length === 0) {
  2771. return false;
  2772. }
  2773. for (let i = 0; i < playlist.segments.length; i++) {
  2774. const segment = playlist.segments[i];
  2775. if (!segment.dateTimeObject) {
  2776. return false;
  2777. }
  2778. }
  2779. return true;
  2780. };
  2781. /**
  2782. * Returns the programTime of the media given a playlist and a playerTime.
  2783. * The playlist must have programDateTime tags for a programDateTime tag to be returned.
  2784. * If the segments containing the time requested have not been buffered yet, an estimate
  2785. * may be returned to the callback.
  2786. *
  2787. * @param {Object} args
  2788. * @param {Object} args.playlist A playlist object to search within
  2789. * @param {number} time A playerTime in seconds
  2790. * @param {Function} callback(err, programTime)
  2791. * @return {string} err.message A detailed error message
  2792. * @return {Object} programTime
  2793. * @return {number} programTime.mediaSeconds The streamTime in seconds
  2794. * @return {string} programTime.programDateTime The programTime as an ISO-8601 String
  2795. */
  2796. const getProgramTime = ({
  2797. playlist,
  2798. time = undefined,
  2799. callback
  2800. }) => {
  2801. if (!callback) {
  2802. throw new Error('getProgramTime: callback must be provided');
  2803. }
  2804. if (!playlist || time === undefined) {
  2805. return callback({
  2806. message: 'getProgramTime: playlist and time must be provided'
  2807. });
  2808. }
  2809. const matchedSegment = findSegmentForPlayerTime(time, playlist);
  2810. if (!matchedSegment) {
  2811. return callback({
  2812. message: 'valid programTime was not found'
  2813. });
  2814. }
  2815. if (matchedSegment.type === 'estimate') {
  2816. return callback({
  2817. message: 'Accurate programTime could not be determined.' + ' Please seek to e.seekTime and try again',
  2818. seekTime: matchedSegment.estimatedStart
  2819. });
  2820. }
  2821. const programTimeObject = {
  2822. mediaSeconds: time
  2823. };
  2824. const programTime = playerTimeToProgramTime(time, matchedSegment.segment);
  2825. if (programTime) {
  2826. programTimeObject.programDateTime = programTime.toISOString();
  2827. }
  2828. return callback(null, programTimeObject);
  2829. };
  2830. /**
  2831. * Seeks in the player to a time that matches the given programTime ISO-8601 string.
  2832. *
  2833. * @param {Object} args
  2834. * @param {string} args.programTime A programTime to seek to as an ISO-8601 String
  2835. * @param {Object} args.playlist A playlist to look within
  2836. * @param {number} args.retryCount The number of times to try for an accurate seek. Default is 2.
  2837. * @param {Function} args.seekTo A method to perform a seek
  2838. * @param {boolean} args.pauseAfterSeek Whether to end in a paused state after seeking. Default is true.
  2839. * @param {Object} args.tech The tech to seek on
  2840. * @param {Function} args.callback(err, newTime) A callback to return the new time to
  2841. * @return {string} err.message A detailed error message
  2842. * @return {number} newTime The exact time that was seeked to in seconds
  2843. */
  2844. const seekToProgramTime = ({
  2845. programTime,
  2846. playlist,
  2847. retryCount = 2,
  2848. seekTo,
  2849. pauseAfterSeek = true,
  2850. tech,
  2851. callback
  2852. }) => {
  2853. if (!callback) {
  2854. throw new Error('seekToProgramTime: callback must be provided');
  2855. }
  2856. if (typeof programTime === 'undefined' || !playlist || !seekTo) {
  2857. return callback({
  2858. message: 'seekToProgramTime: programTime, seekTo and playlist must be provided'
  2859. });
  2860. }
  2861. if (!playlist.endList && !tech.hasStarted_) {
  2862. return callback({
  2863. message: 'player must be playing a live stream to start buffering'
  2864. });
  2865. }
  2866. if (!verifyProgramDateTimeTags(playlist)) {
  2867. return callback({
  2868. message: 'programDateTime tags must be provided in the manifest ' + playlist.resolvedUri
  2869. });
  2870. }
  2871. const matchedSegment = findSegmentForProgramTime(programTime, playlist); // no match
  2872. if (!matchedSegment) {
  2873. return callback({
  2874. message: `${programTime} was not found in the stream`
  2875. });
  2876. }
  2877. const segment = matchedSegment.segment;
  2878. const mediaOffset = getOffsetFromTimestamp(segment.dateTimeObject, programTime);
  2879. if (matchedSegment.type === 'estimate') {
  2880. // we've run out of retries
  2881. if (retryCount === 0) {
  2882. return callback({
  2883. message: `${programTime} is not buffered yet. Try again`
  2884. });
  2885. }
  2886. seekTo(matchedSegment.estimatedStart + mediaOffset);
  2887. tech.one('seeked', () => {
  2888. seekToProgramTime({
  2889. programTime,
  2890. playlist,
  2891. retryCount: retryCount - 1,
  2892. seekTo,
  2893. pauseAfterSeek,
  2894. tech,
  2895. callback
  2896. });
  2897. });
  2898. return;
  2899. } // Since the segment.start value is determined from the buffered end or ending time
  2900. // of the prior segment, the seekToTime doesn't need to account for any transmuxer
  2901. // modifications.
  2902. const seekToTime = segment.start + mediaOffset;
  2903. const seekedCallback = () => {
  2904. return callback(null, tech.currentTime());
  2905. }; // listen for seeked event
  2906. tech.one('seeked', seekedCallback); // pause before seeking as video.js will restore this state
  2907. if (pauseAfterSeek) {
  2908. tech.pause();
  2909. }
  2910. seekTo(seekToTime);
  2911. };
  2912. // which will only happen if the request is complete.
  2913. const callbackOnCompleted = (request, cb) => {
  2914. if (request.readyState === 4) {
  2915. return cb();
  2916. }
  2917. return;
  2918. };
  2919. const containerRequest = (uri, xhr, cb) => {
  2920. let bytes = [];
  2921. let id3Offset;
  2922. let finished = false;
  2923. const endRequestAndCallback = function (err, req, type, _bytes) {
  2924. req.abort();
  2925. finished = true;
  2926. return cb(err, req, type, _bytes);
  2927. };
  2928. const progressListener = function (error, request) {
  2929. if (finished) {
  2930. return;
  2931. }
  2932. if (error) {
  2933. return endRequestAndCallback(error, request, '', bytes);
  2934. } // grap the new part of content that was just downloaded
  2935. const newPart = request.responseText.substring(bytes && bytes.byteLength || 0, request.responseText.length); // add that onto bytes
  2936. bytes = byteHelpers.concatTypedArrays(bytes, byteHelpers.stringToBytes(newPart, true));
  2937. id3Offset = id3Offset || id3Helpers.getId3Offset(bytes); // we need at least 10 bytes to determine a type
  2938. // or we need at least two bytes after an id3Offset
  2939. if (bytes.length < 10 || id3Offset && bytes.length < id3Offset + 2) {
  2940. return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
  2941. }
  2942. const type = containers.detectContainerForBytes(bytes); // if this looks like a ts segment but we don't have enough data
  2943. // to see the second sync byte, wait until we have enough data
  2944. // before declaring it ts
  2945. if (type === 'ts' && bytes.length < 188) {
  2946. return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
  2947. } // this may be an unsynced ts segment
  2948. // wait for 376 bytes before detecting no container
  2949. if (!type && bytes.length < 376) {
  2950. return callbackOnCompleted(request, () => endRequestAndCallback(error, request, '', bytes));
  2951. }
  2952. return endRequestAndCallback(null, request, type, bytes);
  2953. };
  2954. const options = {
  2955. uri,
  2956. beforeSend(request) {
  2957. // this forces the browser to pass the bytes to us unprocessed
  2958. request.overrideMimeType('text/plain; charset=x-user-defined');
  2959. request.addEventListener('progress', function ({
  2960. total,
  2961. loaded
  2962. }) {
  2963. return callbackWrapper(request, null, {
  2964. statusCode: request.status
  2965. }, progressListener);
  2966. });
  2967. }
  2968. };
  2969. const request = xhr(options, function (error, response) {
  2970. return callbackWrapper(request, error, response, progressListener);
  2971. });
  2972. return request;
  2973. };
  2974. const {
  2975. EventTarget
  2976. } = videojs__default["default"];
  2977. const dashPlaylistUnchanged = function (a, b) {
  2978. if (!isPlaylistUnchanged(a, b)) {
  2979. return false;
  2980. } // for dash the above check will often return true in scenarios where
  2981. // the playlist actually has changed because mediaSequence isn't a
  2982. // dash thing, and we often set it to 1. So if the playlists have the same amount
  2983. // of segments we return true.
  2984. // So for dash we need to make sure that the underlying segments are different.
  2985. // if sidx changed then the playlists are different.
  2986. if (a.sidx && b.sidx && (a.sidx.offset !== b.sidx.offset || a.sidx.length !== b.sidx.length)) {
  2987. return false;
  2988. } else if (!a.sidx && b.sidx || a.sidx && !b.sidx) {
  2989. return false;
  2990. } // one or the other does not have segments
  2991. // there was a change.
  2992. if (a.segments && !b.segments || !a.segments && b.segments) {
  2993. return false;
  2994. } // neither has segments nothing changed
  2995. if (!a.segments && !b.segments) {
  2996. return true;
  2997. } // check segments themselves
  2998. for (let i = 0; i < a.segments.length; i++) {
  2999. const aSegment = a.segments[i];
  3000. const bSegment = b.segments[i]; // if uris are different between segments there was a change
  3001. if (aSegment.uri !== bSegment.uri) {
  3002. return false;
  3003. } // neither segment has a byterange, there will be no byterange change.
  3004. if (!aSegment.byterange && !bSegment.byterange) {
  3005. continue;
  3006. }
  3007. const aByterange = aSegment.byterange;
  3008. const bByterange = bSegment.byterange; // if byterange only exists on one of the segments, there was a change.
  3009. if (aByterange && !bByterange || !aByterange && bByterange) {
  3010. return false;
  3011. } // if both segments have byterange with different offsets, there was a change.
  3012. if (aByterange.offset !== bByterange.offset || aByterange.length !== bByterange.length) {
  3013. return false;
  3014. }
  3015. } // if everything was the same with segments, this is the same playlist.
  3016. return true;
  3017. };
  3018. /**
  3019. * Use the representation IDs from the mpd object to create groupIDs, the NAME is set to mandatory representation
  3020. * ID in the parser. This allows for continuous playout across periods with the same representation IDs
  3021. * (continuous periods as defined in DASH-IF 3.2.12). This is assumed in the mpd-parser as well. If we want to support
  3022. * periods without continuous playback this function may need modification as well as the parser.
  3023. */
  3024. const dashGroupId = (type, group, label, playlist) => {
  3025. // If the manifest somehow does not have an ID (non-dash compliant), use the label.
  3026. const playlistId = playlist.attributes.NAME || label;
  3027. return `placeholder-uri-${type}-${group}-${playlistId}`;
  3028. };
  3029. /**
  3030. * Parses the main XML string and updates playlist URI references.
  3031. *
  3032. * @param {Object} config
  3033. * Object of arguments
  3034. * @param {string} config.mainXml
  3035. * The mpd XML
  3036. * @param {string} config.srcUrl
  3037. * The mpd URL
  3038. * @param {Date} config.clientOffset
  3039. * A time difference between server and client
  3040. * @param {Object} config.sidxMapping
  3041. * SIDX mappings for moof/mdat URIs and byte ranges
  3042. * @return {Object}
  3043. * The parsed mpd manifest object
  3044. */
  3045. const parseMainXml = ({
  3046. mainXml,
  3047. srcUrl,
  3048. clientOffset,
  3049. sidxMapping,
  3050. previousManifest
  3051. }) => {
  3052. const manifest = mpdParser.parse(mainXml, {
  3053. manifestUri: srcUrl,
  3054. clientOffset,
  3055. sidxMapping,
  3056. previousManifest
  3057. });
  3058. addPropertiesToMain(manifest, srcUrl, dashGroupId);
  3059. return manifest;
  3060. };
  3061. /**
  3062. * Removes any mediaGroup labels that no longer exist in the newMain
  3063. *
  3064. * @param {Object} update
  3065. * The previous mpd object being updated
  3066. * @param {Object} newMain
  3067. * The new mpd object
  3068. */
  3069. const removeOldMediaGroupLabels = (update, newMain) => {
  3070. forEachMediaGroup(update, (properties, type, group, label) => {
  3071. if (!(label in newMain.mediaGroups[type][group])) {
  3072. delete update.mediaGroups[type][group][label];
  3073. }
  3074. });
  3075. };
  3076. /**
  3077. * Returns a new main manifest that is the result of merging an updated main manifest
  3078. * into the original version.
  3079. *
  3080. * @param {Object} oldMain
  3081. * The old parsed mpd object
  3082. * @param {Object} newMain
  3083. * The updated parsed mpd object
  3084. * @return {Object}
  3085. * A new object representing the original main manifest with the updated media
  3086. * playlists merged in
  3087. */
  3088. const updateMain = (oldMain, newMain, sidxMapping) => {
  3089. let noChanges = true;
  3090. let update = merge(oldMain, {
  3091. // These are top level properties that can be updated
  3092. duration: newMain.duration,
  3093. minimumUpdatePeriod: newMain.minimumUpdatePeriod,
  3094. timelineStarts: newMain.timelineStarts
  3095. }); // First update the playlists in playlist list
  3096. for (let i = 0; i < newMain.playlists.length; i++) {
  3097. const playlist = newMain.playlists[i];
  3098. if (playlist.sidx) {
  3099. const sidxKey = mpdParser.generateSidxKey(playlist.sidx); // add sidx segments to the playlist if we have all the sidx info already
  3100. if (sidxMapping && sidxMapping[sidxKey] && sidxMapping[sidxKey].sidx) {
  3101. mpdParser.addSidxSegmentsToPlaylist(playlist, sidxMapping[sidxKey].sidx, playlist.sidx.resolvedUri);
  3102. }
  3103. }
  3104. const playlistUpdate = updateMain$1(update, playlist, dashPlaylistUnchanged);
  3105. if (playlistUpdate) {
  3106. update = playlistUpdate;
  3107. noChanges = false;
  3108. }
  3109. } // Then update media group playlists
  3110. forEachMediaGroup(newMain, (properties, type, group, label) => {
  3111. if (properties.playlists && properties.playlists.length) {
  3112. const id = properties.playlists[0].id;
  3113. const playlistUpdate = updateMain$1(update, properties.playlists[0], dashPlaylistUnchanged);
  3114. if (playlistUpdate) {
  3115. update = playlistUpdate; // add new mediaGroup label if it doesn't exist and assign the new mediaGroup.
  3116. if (!(label in update.mediaGroups[type][group])) {
  3117. update.mediaGroups[type][group][label] = properties;
  3118. } // update the playlist reference within media groups
  3119. update.mediaGroups[type][group][label].playlists[0] = update.playlists[id];
  3120. noChanges = false;
  3121. }
  3122. }
  3123. }); // remove mediaGroup labels and references that no longer exist in the newMain
  3124. removeOldMediaGroupLabels(update, newMain);
  3125. if (newMain.minimumUpdatePeriod !== oldMain.minimumUpdatePeriod) {
  3126. noChanges = false;
  3127. }
  3128. if (noChanges) {
  3129. return null;
  3130. }
  3131. return update;
  3132. }; // SIDX should be equivalent if the URI and byteranges of the SIDX match.
  3133. // If the SIDXs have maps, the two maps should match,
  3134. // both `a` and `b` missing SIDXs is considered matching.
  3135. // If `a` or `b` but not both have a map, they aren't matching.
  3136. const equivalentSidx = (a, b) => {
  3137. const neitherMap = Boolean(!a.map && !b.map);
  3138. const equivalentMap = neitherMap || Boolean(a.map && b.map && a.map.byterange.offset === b.map.byterange.offset && a.map.byterange.length === b.map.byterange.length);
  3139. return equivalentMap && a.uri === b.uri && a.byterange.offset === b.byterange.offset && a.byterange.length === b.byterange.length;
  3140. }; // exported for testing
  3141. const compareSidxEntry = (playlists, oldSidxMapping) => {
  3142. const newSidxMapping = {};
  3143. for (const id in playlists) {
  3144. const playlist = playlists[id];
  3145. const currentSidxInfo = playlist.sidx;
  3146. if (currentSidxInfo) {
  3147. const key = mpdParser.generateSidxKey(currentSidxInfo);
  3148. if (!oldSidxMapping[key]) {
  3149. break;
  3150. }
  3151. const savedSidxInfo = oldSidxMapping[key].sidxInfo;
  3152. if (equivalentSidx(savedSidxInfo, currentSidxInfo)) {
  3153. newSidxMapping[key] = oldSidxMapping[key];
  3154. }
  3155. }
  3156. }
  3157. return newSidxMapping;
  3158. };
  3159. /**
  3160. * A function that filters out changed items as they need to be requested separately.
  3161. *
  3162. * The method is exported for testing
  3163. *
  3164. * @param {Object} main the parsed mpd XML returned via mpd-parser
  3165. * @param {Object} oldSidxMapping the SIDX to compare against
  3166. */
  3167. const filterChangedSidxMappings = (main, oldSidxMapping) => {
  3168. const videoSidx = compareSidxEntry(main.playlists, oldSidxMapping);
  3169. let mediaGroupSidx = videoSidx;
  3170. forEachMediaGroup(main, (properties, mediaType, groupKey, labelKey) => {
  3171. if (properties.playlists && properties.playlists.length) {
  3172. const playlists = properties.playlists;
  3173. mediaGroupSidx = merge(mediaGroupSidx, compareSidxEntry(playlists, oldSidxMapping));
  3174. }
  3175. });
  3176. return mediaGroupSidx;
  3177. };
  3178. class DashPlaylistLoader extends EventTarget {
  3179. // DashPlaylistLoader must accept either a src url or a playlist because subsequent
  3180. // playlist loader setups from media groups will expect to be able to pass a playlist
  3181. // (since there aren't external URLs to media playlists with DASH)
  3182. constructor(srcUrlOrPlaylist, vhs, options = {}, mainPlaylistLoader) {
  3183. super();
  3184. this.mainPlaylistLoader_ = mainPlaylistLoader || this;
  3185. if (!mainPlaylistLoader) {
  3186. this.isMain_ = true;
  3187. }
  3188. const {
  3189. withCredentials = false
  3190. } = options;
  3191. this.vhs_ = vhs;
  3192. this.withCredentials = withCredentials;
  3193. this.addMetadataToTextTrack = options.addMetadataToTextTrack;
  3194. if (!srcUrlOrPlaylist) {
  3195. throw new Error('A non-empty playlist URL or object is required');
  3196. } // event naming?
  3197. this.on('minimumUpdatePeriod', () => {
  3198. this.refreshXml_();
  3199. }); // live playlist staleness timeout
  3200. this.on('mediaupdatetimeout', () => {
  3201. this.refreshMedia_(this.media().id);
  3202. });
  3203. this.state = 'HAVE_NOTHING';
  3204. this.loadedPlaylists_ = {};
  3205. this.logger_ = logger('DashPlaylistLoader'); // initialize the loader state
  3206. // The mainPlaylistLoader will be created with a string
  3207. if (this.isMain_) {
  3208. this.mainPlaylistLoader_.srcUrl = srcUrlOrPlaylist; // TODO: reset sidxMapping between period changes
  3209. // once multi-period is refactored
  3210. this.mainPlaylistLoader_.sidxMapping_ = {};
  3211. } else {
  3212. this.childPlaylist_ = srcUrlOrPlaylist;
  3213. }
  3214. }
  3215. requestErrored_(err, request, startingState) {
  3216. // disposed
  3217. if (!this.request) {
  3218. return true;
  3219. } // pending request is cleared
  3220. this.request = null;
  3221. if (err) {
  3222. // use the provided error object or create one
  3223. // based on the request/response
  3224. this.error = typeof err === 'object' && !(err instanceof Error) ? err : {
  3225. status: request.status,
  3226. message: 'DASH request error at URL: ' + request.uri,
  3227. response: request.response,
  3228. // MEDIA_ERR_NETWORK
  3229. code: 2
  3230. };
  3231. if (startingState) {
  3232. this.state = startingState;
  3233. }
  3234. this.trigger('error');
  3235. return true;
  3236. }
  3237. }
  3238. /**
  3239. * Verify that the container of the sidx segment can be parsed
  3240. * and if it can, get and parse that segment.
  3241. */
  3242. addSidxSegments_(playlist, startingState, cb) {
  3243. const sidxKey = playlist.sidx && mpdParser.generateSidxKey(playlist.sidx); // playlist lacks sidx or sidx segments were added to this playlist already.
  3244. if (!playlist.sidx || !sidxKey || this.mainPlaylistLoader_.sidxMapping_[sidxKey]) {
  3245. // keep this function async
  3246. this.mediaRequest_ = window__default["default"].setTimeout(() => cb(false), 0);
  3247. return;
  3248. } // resolve the segment URL relative to the playlist
  3249. const uri = resolveManifestRedirect(playlist.sidx.resolvedUri);
  3250. const fin = (err, request) => {
  3251. if (this.requestErrored_(err, request, startingState)) {
  3252. return;
  3253. }
  3254. const sidxMapping = this.mainPlaylistLoader_.sidxMapping_;
  3255. let sidx;
  3256. try {
  3257. sidx = parseSidx__default["default"](byteHelpers.toUint8(request.response).subarray(8));
  3258. } catch (e) {
  3259. // sidx parsing failed.
  3260. this.requestErrored_(e, request, startingState);
  3261. return;
  3262. }
  3263. sidxMapping[sidxKey] = {
  3264. sidxInfo: playlist.sidx,
  3265. sidx
  3266. };
  3267. mpdParser.addSidxSegmentsToPlaylist(playlist, sidx, playlist.sidx.resolvedUri);
  3268. return cb(true);
  3269. };
  3270. this.request = containerRequest(uri, this.vhs_.xhr, (err, request, container, bytes) => {
  3271. if (err) {
  3272. return fin(err, request);
  3273. }
  3274. if (!container || container !== 'mp4') {
  3275. return fin({
  3276. status: request.status,
  3277. message: `Unsupported ${container || 'unknown'} container type for sidx segment at URL: ${uri}`,
  3278. // response is just bytes in this case
  3279. // but we really don't want to return that.
  3280. response: '',
  3281. playlist,
  3282. internal: true,
  3283. playlistExclusionDuration: Infinity,
  3284. // MEDIA_ERR_NETWORK
  3285. code: 2
  3286. }, request);
  3287. } // if we already downloaded the sidx bytes in the container request, use them
  3288. const {
  3289. offset,
  3290. length
  3291. } = playlist.sidx.byterange;
  3292. if (bytes.length >= length + offset) {
  3293. return fin(err, {
  3294. response: bytes.subarray(offset, offset + length),
  3295. status: request.status,
  3296. uri: request.uri
  3297. });
  3298. } // otherwise request sidx bytes
  3299. this.request = this.vhs_.xhr({
  3300. uri,
  3301. responseType: 'arraybuffer',
  3302. headers: segmentXhrHeaders({
  3303. byterange: playlist.sidx.byterange
  3304. })
  3305. }, fin);
  3306. });
  3307. }
  3308. dispose() {
  3309. this.trigger('dispose');
  3310. this.stopRequest();
  3311. this.loadedPlaylists_ = {};
  3312. window__default["default"].clearTimeout(this.minimumUpdatePeriodTimeout_);
  3313. window__default["default"].clearTimeout(this.mediaRequest_);
  3314. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  3315. this.mediaUpdateTimeout = null;
  3316. this.mediaRequest_ = null;
  3317. this.minimumUpdatePeriodTimeout_ = null;
  3318. if (this.mainPlaylistLoader_.createMupOnMedia_) {
  3319. this.off('loadedmetadata', this.mainPlaylistLoader_.createMupOnMedia_);
  3320. this.mainPlaylistLoader_.createMupOnMedia_ = null;
  3321. }
  3322. this.off();
  3323. }
  3324. hasPendingRequest() {
  3325. return this.request || this.mediaRequest_;
  3326. }
  3327. stopRequest() {
  3328. if (this.request) {
  3329. const oldRequest = this.request;
  3330. this.request = null;
  3331. oldRequest.onreadystatechange = null;
  3332. oldRequest.abort();
  3333. }
  3334. }
  3335. media(playlist) {
  3336. // getter
  3337. if (!playlist) {
  3338. return this.media_;
  3339. } // setter
  3340. if (this.state === 'HAVE_NOTHING') {
  3341. throw new Error('Cannot switch media playlist from ' + this.state);
  3342. }
  3343. const startingState = this.state; // find the playlist object if the target playlist has been specified by URI
  3344. if (typeof playlist === 'string') {
  3345. if (!this.mainPlaylistLoader_.main.playlists[playlist]) {
  3346. throw new Error('Unknown playlist URI: ' + playlist);
  3347. }
  3348. playlist = this.mainPlaylistLoader_.main.playlists[playlist];
  3349. }
  3350. const mediaChange = !this.media_ || playlist.id !== this.media_.id; // switch to previously loaded playlists immediately
  3351. if (mediaChange && this.loadedPlaylists_[playlist.id] && this.loadedPlaylists_[playlist.id].endList) {
  3352. this.state = 'HAVE_METADATA';
  3353. this.media_ = playlist; // trigger media change if the active media has been updated
  3354. if (mediaChange) {
  3355. this.trigger('mediachanging');
  3356. this.trigger('mediachange');
  3357. }
  3358. return;
  3359. } // switching to the active playlist is a no-op
  3360. if (!mediaChange) {
  3361. return;
  3362. } // switching from an already loaded playlist
  3363. if (this.media_) {
  3364. this.trigger('mediachanging');
  3365. }
  3366. this.addSidxSegments_(playlist, startingState, sidxChanged => {
  3367. // everything is ready just continue to haveMetadata
  3368. this.haveMetadata({
  3369. startingState,
  3370. playlist
  3371. });
  3372. });
  3373. }
  3374. haveMetadata({
  3375. startingState,
  3376. playlist
  3377. }) {
  3378. this.state = 'HAVE_METADATA';
  3379. this.loadedPlaylists_[playlist.id] = playlist;
  3380. this.mediaRequest_ = null; // This will trigger loadedplaylist
  3381. this.refreshMedia_(playlist.id); // fire loadedmetadata the first time a media playlist is loaded
  3382. // to resolve setup of media groups
  3383. if (startingState === 'HAVE_MAIN_MANIFEST') {
  3384. this.trigger('loadedmetadata');
  3385. } else {
  3386. // trigger media change if the active media has been updated
  3387. this.trigger('mediachange');
  3388. }
  3389. }
  3390. pause() {
  3391. if (this.mainPlaylistLoader_.createMupOnMedia_) {
  3392. this.off('loadedmetadata', this.mainPlaylistLoader_.createMupOnMedia_);
  3393. this.mainPlaylistLoader_.createMupOnMedia_ = null;
  3394. }
  3395. this.stopRequest();
  3396. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  3397. this.mediaUpdateTimeout = null;
  3398. if (this.isMain_) {
  3399. window__default["default"].clearTimeout(this.mainPlaylistLoader_.minimumUpdatePeriodTimeout_);
  3400. this.mainPlaylistLoader_.minimumUpdatePeriodTimeout_ = null;
  3401. }
  3402. if (this.state === 'HAVE_NOTHING') {
  3403. // If we pause the loader before any data has been retrieved, its as if we never
  3404. // started, so reset to an unstarted state.
  3405. this.started = false;
  3406. }
  3407. }
  3408. load(isFinalRendition) {
  3409. window__default["default"].clearTimeout(this.mediaUpdateTimeout);
  3410. this.mediaUpdateTimeout = null;
  3411. const media = this.media();
  3412. if (isFinalRendition) {
  3413. const delay = media ? media.targetDuration / 2 * 1000 : 5 * 1000;
  3414. this.mediaUpdateTimeout = window__default["default"].setTimeout(() => this.load(), delay);
  3415. return;
  3416. } // because the playlists are internal to the manifest, load should either load the
  3417. // main manifest, or do nothing but trigger an event
  3418. if (!this.started) {
  3419. this.start();
  3420. return;
  3421. }
  3422. if (media && !media.endList) {
  3423. // Check to see if this is the main loader and the MUP was cleared (this happens
  3424. // when the loader was paused). `media` should be set at this point since one is always
  3425. // set during `start()`.
  3426. if (this.isMain_ && !this.minimumUpdatePeriodTimeout_) {
  3427. // Trigger minimumUpdatePeriod to refresh the main manifest
  3428. this.trigger('minimumUpdatePeriod'); // Since there was no prior minimumUpdatePeriodTimeout it should be recreated
  3429. this.updateMinimumUpdatePeriodTimeout_();
  3430. }
  3431. this.trigger('mediaupdatetimeout');
  3432. } else {
  3433. this.trigger('loadedplaylist');
  3434. }
  3435. }
  3436. start() {
  3437. this.started = true; // We don't need to request the main manifest again
  3438. // Call this asynchronously to match the xhr request behavior below
  3439. if (!this.isMain_) {
  3440. this.mediaRequest_ = window__default["default"].setTimeout(() => this.haveMain_(), 0);
  3441. return;
  3442. }
  3443. this.requestMain_((req, mainChanged) => {
  3444. this.haveMain_();
  3445. if (!this.hasPendingRequest() && !this.media_) {
  3446. this.media(this.mainPlaylistLoader_.main.playlists[0]);
  3447. }
  3448. });
  3449. }
  3450. requestMain_(cb) {
  3451. this.request = this.vhs_.xhr({
  3452. uri: this.mainPlaylistLoader_.srcUrl,
  3453. withCredentials: this.withCredentials
  3454. }, (error, req) => {
  3455. if (this.requestErrored_(error, req)) {
  3456. if (this.state === 'HAVE_NOTHING') {
  3457. this.started = false;
  3458. }
  3459. return;
  3460. }
  3461. const mainChanged = req.responseText !== this.mainPlaylistLoader_.mainXml_;
  3462. this.mainPlaylistLoader_.mainXml_ = req.responseText;
  3463. if (req.responseHeaders && req.responseHeaders.date) {
  3464. this.mainLoaded_ = Date.parse(req.responseHeaders.date);
  3465. } else {
  3466. this.mainLoaded_ = Date.now();
  3467. }
  3468. this.mainPlaylistLoader_.srcUrl = resolveManifestRedirect(this.mainPlaylistLoader_.srcUrl, req);
  3469. if (mainChanged) {
  3470. this.handleMain_();
  3471. this.syncClientServerClock_(() => {
  3472. return cb(req, mainChanged);
  3473. });
  3474. return;
  3475. }
  3476. return cb(req, mainChanged);
  3477. });
  3478. }
  3479. /**
  3480. * Parses the main xml for UTCTiming node to sync the client clock to the server
  3481. * clock. If the UTCTiming node requires a HEAD or GET request, that request is made.
  3482. *
  3483. * @param {Function} done
  3484. * Function to call when clock sync has completed
  3485. */
  3486. syncClientServerClock_(done) {
  3487. const utcTiming = mpdParser.parseUTCTiming(this.mainPlaylistLoader_.mainXml_); // No UTCTiming element found in the mpd. Use Date header from mpd request as the
  3488. // server clock
  3489. if (utcTiming === null) {
  3490. this.mainPlaylistLoader_.clientOffset_ = this.mainLoaded_ - Date.now();
  3491. return done();
  3492. }
  3493. if (utcTiming.method === 'DIRECT') {
  3494. this.mainPlaylistLoader_.clientOffset_ = utcTiming.value - Date.now();
  3495. return done();
  3496. }
  3497. this.request = this.vhs_.xhr({
  3498. uri: resolveUrl(this.mainPlaylistLoader_.srcUrl, utcTiming.value),
  3499. method: utcTiming.method,
  3500. withCredentials: this.withCredentials
  3501. }, (error, req) => {
  3502. // disposed
  3503. if (!this.request) {
  3504. return;
  3505. }
  3506. if (error) {
  3507. // sync request failed, fall back to using date header from mpd
  3508. // TODO: log warning
  3509. this.mainPlaylistLoader_.clientOffset_ = this.mainLoaded_ - Date.now();
  3510. return done();
  3511. }
  3512. let serverTime;
  3513. if (utcTiming.method === 'HEAD') {
  3514. if (!req.responseHeaders || !req.responseHeaders.date) {
  3515. // expected date header not preset, fall back to using date header from mpd
  3516. // TODO: log warning
  3517. serverTime = this.mainLoaded_;
  3518. } else {
  3519. serverTime = Date.parse(req.responseHeaders.date);
  3520. }
  3521. } else {
  3522. serverTime = Date.parse(req.responseText);
  3523. }
  3524. this.mainPlaylistLoader_.clientOffset_ = serverTime - Date.now();
  3525. done();
  3526. });
  3527. }
  3528. haveMain_() {
  3529. this.state = 'HAVE_MAIN_MANIFEST';
  3530. if (this.isMain_) {
  3531. // We have the main playlist at this point, so
  3532. // trigger this to allow PlaylistController
  3533. // to make an initial playlist selection
  3534. this.trigger('loadedplaylist');
  3535. } else if (!this.media_) {
  3536. // no media playlist was specifically selected so select
  3537. // the one the child playlist loader was created with
  3538. this.media(this.childPlaylist_);
  3539. }
  3540. }
  3541. handleMain_() {
  3542. // clear media request
  3543. this.mediaRequest_ = null;
  3544. const oldMain = this.mainPlaylistLoader_.main;
  3545. let newMain = parseMainXml({
  3546. mainXml: this.mainPlaylistLoader_.mainXml_,
  3547. srcUrl: this.mainPlaylistLoader_.srcUrl,
  3548. clientOffset: this.mainPlaylistLoader_.clientOffset_,
  3549. sidxMapping: this.mainPlaylistLoader_.sidxMapping_,
  3550. previousManifest: oldMain
  3551. }); // if we have an old main to compare the new main against
  3552. if (oldMain) {
  3553. newMain = updateMain(oldMain, newMain, this.mainPlaylistLoader_.sidxMapping_);
  3554. } // only update main if we have a new main
  3555. this.mainPlaylistLoader_.main = newMain ? newMain : oldMain;
  3556. const location = this.mainPlaylistLoader_.main.locations && this.mainPlaylistLoader_.main.locations[0];
  3557. if (location && location !== this.mainPlaylistLoader_.srcUrl) {
  3558. this.mainPlaylistLoader_.srcUrl = location;
  3559. }
  3560. if (!oldMain || newMain && newMain.minimumUpdatePeriod !== oldMain.minimumUpdatePeriod) {
  3561. this.updateMinimumUpdatePeriodTimeout_();
  3562. }
  3563. this.addEventStreamToMetadataTrack_(newMain);
  3564. return Boolean(newMain);
  3565. }
  3566. updateMinimumUpdatePeriodTimeout_() {
  3567. const mpl = this.mainPlaylistLoader_; // cancel any pending creation of mup on media
  3568. // a new one will be added if needed.
  3569. if (mpl.createMupOnMedia_) {
  3570. mpl.off('loadedmetadata', mpl.createMupOnMedia_);
  3571. mpl.createMupOnMedia_ = null;
  3572. } // clear any pending timeouts
  3573. if (mpl.minimumUpdatePeriodTimeout_) {
  3574. window__default["default"].clearTimeout(mpl.minimumUpdatePeriodTimeout_);
  3575. mpl.minimumUpdatePeriodTimeout_ = null;
  3576. }
  3577. let mup = mpl.main && mpl.main.minimumUpdatePeriod; // If the minimumUpdatePeriod has a value of 0, that indicates that the current
  3578. // MPD has no future validity, so a new one will need to be acquired when new
  3579. // media segments are to be made available. Thus, we use the target duration
  3580. // in this case
  3581. if (mup === 0) {
  3582. if (mpl.media()) {
  3583. mup = mpl.media().targetDuration * 1000;
  3584. } else {
  3585. mpl.createMupOnMedia_ = mpl.updateMinimumUpdatePeriodTimeout_;
  3586. mpl.one('loadedmetadata', mpl.createMupOnMedia_);
  3587. }
  3588. } // if minimumUpdatePeriod is invalid or <= zero, which
  3589. // can happen when a live video becomes VOD. skip timeout
  3590. // creation.
  3591. if (typeof mup !== 'number' || mup <= 0) {
  3592. if (mup < 0) {
  3593. this.logger_(`found invalid minimumUpdatePeriod of ${mup}, not setting a timeout`);
  3594. }
  3595. return;
  3596. }
  3597. this.createMUPTimeout_(mup);
  3598. }
  3599. createMUPTimeout_(mup) {
  3600. const mpl = this.mainPlaylistLoader_;
  3601. mpl.minimumUpdatePeriodTimeout_ = window__default["default"].setTimeout(() => {
  3602. mpl.minimumUpdatePeriodTimeout_ = null;
  3603. mpl.trigger('minimumUpdatePeriod');
  3604. mpl.createMUPTimeout_(mup);
  3605. }, mup);
  3606. }
  3607. /**
  3608. * Sends request to refresh the main xml and updates the parsed main manifest
  3609. */
  3610. refreshXml_() {
  3611. this.requestMain_((req, mainChanged) => {
  3612. if (!mainChanged) {
  3613. return;
  3614. }
  3615. if (this.media_) {
  3616. this.media_ = this.mainPlaylistLoader_.main.playlists[this.media_.id];
  3617. } // This will filter out updated sidx info from the mapping
  3618. this.mainPlaylistLoader_.sidxMapping_ = filterChangedSidxMappings(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.sidxMapping_);
  3619. this.addSidxSegments_(this.media(), this.state, sidxChanged => {
  3620. // TODO: do we need to reload the current playlist?
  3621. this.refreshMedia_(this.media().id);
  3622. });
  3623. });
  3624. }
  3625. /**
  3626. * Refreshes the media playlist by re-parsing the main xml and updating playlist
  3627. * references. If this is an alternate loader, the updated parsed manifest is retrieved
  3628. * from the main loader.
  3629. */
  3630. refreshMedia_(mediaID) {
  3631. if (!mediaID) {
  3632. throw new Error('refreshMedia_ must take a media id');
  3633. } // for main we have to reparse the main xml
  3634. // to re-create segments based on current timing values
  3635. // which may change media. We only skip updating the main manifest
  3636. // if this is the first time this.media_ is being set.
  3637. // as main was just parsed in that case.
  3638. if (this.media_ && this.isMain_) {
  3639. this.handleMain_();
  3640. }
  3641. const playlists = this.mainPlaylistLoader_.main.playlists;
  3642. const mediaChanged = !this.media_ || this.media_ !== playlists[mediaID];
  3643. if (mediaChanged) {
  3644. this.media_ = playlists[mediaID];
  3645. } else {
  3646. this.trigger('playlistunchanged');
  3647. }
  3648. if (!this.mediaUpdateTimeout) {
  3649. const createMediaUpdateTimeout = () => {
  3650. if (this.media().endList) {
  3651. return;
  3652. }
  3653. this.mediaUpdateTimeout = window__default["default"].setTimeout(() => {
  3654. this.trigger('mediaupdatetimeout');
  3655. createMediaUpdateTimeout();
  3656. }, refreshDelay(this.media(), Boolean(mediaChanged)));
  3657. };
  3658. createMediaUpdateTimeout();
  3659. }
  3660. this.trigger('loadedplaylist');
  3661. }
  3662. /**
  3663. * Takes eventstream data from a parsed DASH manifest and adds it to the metadata text track.
  3664. *
  3665. * @param {manifest} newMain the newly parsed manifest
  3666. */
  3667. addEventStreamToMetadataTrack_(newMain) {
  3668. // Only add new event stream metadata if we have a new manifest.
  3669. if (newMain && this.mainPlaylistLoader_.main.eventStream) {
  3670. // convert EventStream to ID3-like data.
  3671. const metadataArray = this.mainPlaylistLoader_.main.eventStream.map(eventStreamNode => {
  3672. return {
  3673. cueTime: eventStreamNode.start,
  3674. frames: [{
  3675. data: eventStreamNode.messageData
  3676. }]
  3677. };
  3678. });
  3679. this.addMetadataToTextTrack('EventStream', metadataArray, this.mainPlaylistLoader_.main.duration);
  3680. }
  3681. }
  3682. /**
  3683. * Returns the key ID set from a playlist
  3684. *
  3685. * @param {playlist} playlist to fetch the key ID set from.
  3686. * @return a Set of 32 digit hex strings that represent the unique keyIds for that playlist.
  3687. */
  3688. getKeyIdSet(playlist) {
  3689. if (playlist.contentProtection) {
  3690. const keyIds = new Set();
  3691. for (const keysystem in playlist.contentProtection) {
  3692. const defaultKID = playlist.contentProtection[keysystem].attributes['cenc:default_KID'];
  3693. if (defaultKID) {
  3694. // DASH keyIds are separated by dashes.
  3695. keyIds.add(defaultKID.replace(/-/g, '').toLowerCase());
  3696. }
  3697. }
  3698. return keyIds;
  3699. }
  3700. }
  3701. }
  3702. var Config = {
  3703. GOAL_BUFFER_LENGTH: 30,
  3704. MAX_GOAL_BUFFER_LENGTH: 60,
  3705. BACK_BUFFER_LENGTH: 30,
  3706. GOAL_BUFFER_LENGTH_RATE: 1,
  3707. // 0.5 MB/s
  3708. INITIAL_BANDWIDTH: 4194304,
  3709. // A fudge factor to apply to advertised playlist bitrates to account for
  3710. // temporary flucations in client bandwidth
  3711. BANDWIDTH_VARIANCE: 1.2,
  3712. // How much of the buffer must be filled before we consider upswitching
  3713. BUFFER_LOW_WATER_LINE: 0,
  3714. MAX_BUFFER_LOW_WATER_LINE: 30,
  3715. // TODO: Remove this when experimentalBufferBasedABR is removed
  3716. EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE: 16,
  3717. BUFFER_LOW_WATER_LINE_RATE: 1,
  3718. // If the buffer is greater than the high water line, we won't switch down
  3719. BUFFER_HIGH_WATER_LINE: 30
  3720. };
  3721. const stringToArrayBuffer = string => {
  3722. const view = new Uint8Array(new ArrayBuffer(string.length));
  3723. for (let i = 0; i < string.length; i++) {
  3724. view[i] = string.charCodeAt(i);
  3725. }
  3726. return view.buffer;
  3727. };
  3728. /* global Blob, BlobBuilder, Worker */
  3729. // unify worker interface
  3730. const browserWorkerPolyFill = function (workerObj) {
  3731. // node only supports on/off
  3732. workerObj.on = workerObj.addEventListener;
  3733. workerObj.off = workerObj.removeEventListener;
  3734. return workerObj;
  3735. };
  3736. const createObjectURL = function (str) {
  3737. try {
  3738. return URL.createObjectURL(new Blob([str], {
  3739. type: 'application/javascript'
  3740. }));
  3741. } catch (e) {
  3742. const blob = new BlobBuilder();
  3743. blob.append(str);
  3744. return URL.createObjectURL(blob.getBlob());
  3745. }
  3746. };
  3747. const factory = function (code) {
  3748. return function () {
  3749. const objectUrl = createObjectURL(code);
  3750. const worker = browserWorkerPolyFill(new Worker(objectUrl));
  3751. worker.objURL = objectUrl;
  3752. const terminate = worker.terminate;
  3753. worker.on = worker.addEventListener;
  3754. worker.off = worker.removeEventListener;
  3755. worker.terminate = function () {
  3756. URL.revokeObjectURL(objectUrl);
  3757. return terminate.call(this);
  3758. };
  3759. return worker;
  3760. };
  3761. };
  3762. const transform = function (code) {
  3763. return `var browserWorkerPolyFill = ${browserWorkerPolyFill.toString()};\n` + 'browserWorkerPolyFill(self);\n' + code;
  3764. };
  3765. const getWorkerString = function (fn) {
  3766. return fn.toString().replace(/^function.+?{/, '').slice(0, -1);
  3767. };
  3768. /* rollup-plugin-worker-factory start for worker!/home/runner/work/http-streaming/http-streaming/src/transmuxer-worker.js */
  3769. const workerCode$1 = transform(getWorkerString(function () {
  3770. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  3771. /**
  3772. * mux.js
  3773. *
  3774. * Copyright (c) Brightcove
  3775. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  3776. *
  3777. * A lightweight readable stream implemention that handles event dispatching.
  3778. * Objects that inherit from streams should call init in their constructors.
  3779. */
  3780. var Stream$8 = function () {
  3781. this.init = function () {
  3782. var listeners = {};
  3783. /**
  3784. * Add a listener for a specified event type.
  3785. * @param type {string} the event name
  3786. * @param listener {function} the callback to be invoked when an event of
  3787. * the specified type occurs
  3788. */
  3789. this.on = function (type, listener) {
  3790. if (!listeners[type]) {
  3791. listeners[type] = [];
  3792. }
  3793. listeners[type] = listeners[type].concat(listener);
  3794. };
  3795. /**
  3796. * Remove a listener for a specified event type.
  3797. * @param type {string} the event name
  3798. * @param listener {function} a function previously registered for this
  3799. * type of event through `on`
  3800. */
  3801. this.off = function (type, listener) {
  3802. var index;
  3803. if (!listeners[type]) {
  3804. return false;
  3805. }
  3806. index = listeners[type].indexOf(listener);
  3807. listeners[type] = listeners[type].slice();
  3808. listeners[type].splice(index, 1);
  3809. return index > -1;
  3810. };
  3811. /**
  3812. * Trigger an event of the specified type on this stream. Any additional
  3813. * arguments to this function are passed as parameters to event listeners.
  3814. * @param type {string} the event name
  3815. */
  3816. this.trigger = function (type) {
  3817. var callbacks, i, length, args;
  3818. callbacks = listeners[type];
  3819. if (!callbacks) {
  3820. return;
  3821. } // Slicing the arguments on every invocation of this method
  3822. // can add a significant amount of overhead. Avoid the
  3823. // intermediate object creation for the common case of a
  3824. // single callback argument
  3825. if (arguments.length === 2) {
  3826. length = callbacks.length;
  3827. for (i = 0; i < length; ++i) {
  3828. callbacks[i].call(this, arguments[1]);
  3829. }
  3830. } else {
  3831. args = [];
  3832. i = arguments.length;
  3833. for (i = 1; i < arguments.length; ++i) {
  3834. args.push(arguments[i]);
  3835. }
  3836. length = callbacks.length;
  3837. for (i = 0; i < length; ++i) {
  3838. callbacks[i].apply(this, args);
  3839. }
  3840. }
  3841. };
  3842. /**
  3843. * Destroys the stream and cleans up.
  3844. */
  3845. this.dispose = function () {
  3846. listeners = {};
  3847. };
  3848. };
  3849. };
  3850. /**
  3851. * Forwards all `data` events on this stream to the destination stream. The
  3852. * destination stream should provide a method `push` to receive the data
  3853. * events as they arrive.
  3854. * @param destination {stream} the stream that will receive all `data` events
  3855. * @param autoFlush {boolean} if false, we will not call `flush` on the destination
  3856. * when the current stream emits a 'done' event
  3857. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  3858. */
  3859. Stream$8.prototype.pipe = function (destination) {
  3860. this.on('data', function (data) {
  3861. destination.push(data);
  3862. });
  3863. this.on('done', function (flushSource) {
  3864. destination.flush(flushSource);
  3865. });
  3866. this.on('partialdone', function (flushSource) {
  3867. destination.partialFlush(flushSource);
  3868. });
  3869. this.on('endedtimeline', function (flushSource) {
  3870. destination.endTimeline(flushSource);
  3871. });
  3872. this.on('reset', function (flushSource) {
  3873. destination.reset(flushSource);
  3874. });
  3875. return destination;
  3876. }; // Default stream functions that are expected to be overridden to perform
  3877. // actual work. These are provided by the prototype as a sort of no-op
  3878. // implementation so that we don't have to check for their existence in the
  3879. // `pipe` function above.
  3880. Stream$8.prototype.push = function (data) {
  3881. this.trigger('data', data);
  3882. };
  3883. Stream$8.prototype.flush = function (flushSource) {
  3884. this.trigger('done', flushSource);
  3885. };
  3886. Stream$8.prototype.partialFlush = function (flushSource) {
  3887. this.trigger('partialdone', flushSource);
  3888. };
  3889. Stream$8.prototype.endTimeline = function (flushSource) {
  3890. this.trigger('endedtimeline', flushSource);
  3891. };
  3892. Stream$8.prototype.reset = function (flushSource) {
  3893. this.trigger('reset', flushSource);
  3894. };
  3895. var stream = Stream$8;
  3896. var MAX_UINT32$1 = Math.pow(2, 32);
  3897. var getUint64$3 = function (uint8) {
  3898. var dv = new DataView(uint8.buffer, uint8.byteOffset, uint8.byteLength);
  3899. var value;
  3900. if (dv.getBigUint64) {
  3901. value = dv.getBigUint64(0);
  3902. if (value < Number.MAX_SAFE_INTEGER) {
  3903. return Number(value);
  3904. }
  3905. return value;
  3906. }
  3907. return dv.getUint32(0) * MAX_UINT32$1 + dv.getUint32(4);
  3908. };
  3909. var numbers = {
  3910. getUint64: getUint64$3,
  3911. MAX_UINT32: MAX_UINT32$1
  3912. };
  3913. /**
  3914. * mux.js
  3915. *
  3916. * Copyright (c) Brightcove
  3917. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  3918. *
  3919. * Functions that generate fragmented MP4s suitable for use with Media
  3920. * Source Extensions.
  3921. */
  3922. var MAX_UINT32 = numbers.MAX_UINT32;
  3923. 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
  3924. (function () {
  3925. var i;
  3926. types = {
  3927. avc1: [],
  3928. // codingname
  3929. avcC: [],
  3930. btrt: [],
  3931. dinf: [],
  3932. dref: [],
  3933. esds: [],
  3934. ftyp: [],
  3935. hdlr: [],
  3936. mdat: [],
  3937. mdhd: [],
  3938. mdia: [],
  3939. mfhd: [],
  3940. minf: [],
  3941. moof: [],
  3942. moov: [],
  3943. mp4a: [],
  3944. // codingname
  3945. mvex: [],
  3946. mvhd: [],
  3947. pasp: [],
  3948. sdtp: [],
  3949. smhd: [],
  3950. stbl: [],
  3951. stco: [],
  3952. stsc: [],
  3953. stsd: [],
  3954. stsz: [],
  3955. stts: [],
  3956. styp: [],
  3957. tfdt: [],
  3958. tfhd: [],
  3959. traf: [],
  3960. trak: [],
  3961. trun: [],
  3962. trex: [],
  3963. tkhd: [],
  3964. vmhd: []
  3965. }; // In environments where Uint8Array is undefined (e.g., IE8), skip set up so that we
  3966. // don't throw an error
  3967. if (typeof Uint8Array === 'undefined') {
  3968. return;
  3969. }
  3970. for (i in types) {
  3971. if (types.hasOwnProperty(i)) {
  3972. types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
  3973. }
  3974. }
  3975. MAJOR_BRAND = new Uint8Array(['i'.charCodeAt(0), 's'.charCodeAt(0), 'o'.charCodeAt(0), 'm'.charCodeAt(0)]);
  3976. AVC1_BRAND = new Uint8Array(['a'.charCodeAt(0), 'v'.charCodeAt(0), 'c'.charCodeAt(0), '1'.charCodeAt(0)]);
  3977. MINOR_VERSION = new Uint8Array([0, 0, 0, 1]);
  3978. VIDEO_HDLR = new Uint8Array([0x00, // version 0
  3979. 0x00, 0x00, 0x00, // flags
  3980. 0x00, 0x00, 0x00, 0x00, // pre_defined
  3981. 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
  3982. 0x00, 0x00, 0x00, 0x00, // reserved
  3983. 0x00, 0x00, 0x00, 0x00, // reserved
  3984. 0x00, 0x00, 0x00, 0x00, // reserved
  3985. 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
  3986. ]);
  3987. AUDIO_HDLR = new Uint8Array([0x00, // version 0
  3988. 0x00, 0x00, 0x00, // flags
  3989. 0x00, 0x00, 0x00, 0x00, // pre_defined
  3990. 0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
  3991. 0x00, 0x00, 0x00, 0x00, // reserved
  3992. 0x00, 0x00, 0x00, 0x00, // reserved
  3993. 0x00, 0x00, 0x00, 0x00, // reserved
  3994. 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
  3995. ]);
  3996. HDLR_TYPES = {
  3997. video: VIDEO_HDLR,
  3998. audio: AUDIO_HDLR
  3999. };
  4000. DREF = new Uint8Array([0x00, // version 0
  4001. 0x00, 0x00, 0x00, // flags
  4002. 0x00, 0x00, 0x00, 0x01, // entry_count
  4003. 0x00, 0x00, 0x00, 0x0c, // entry_size
  4004. 0x75, 0x72, 0x6c, 0x20, // 'url' type
  4005. 0x00, // version 0
  4006. 0x00, 0x00, 0x01 // entry_flags
  4007. ]);
  4008. SMHD = new Uint8Array([0x00, // version
  4009. 0x00, 0x00, 0x00, // flags
  4010. 0x00, 0x00, // balance, 0 means centered
  4011. 0x00, 0x00 // reserved
  4012. ]);
  4013. STCO = new Uint8Array([0x00, // version
  4014. 0x00, 0x00, 0x00, // flags
  4015. 0x00, 0x00, 0x00, 0x00 // entry_count
  4016. ]);
  4017. STSC = STCO;
  4018. STSZ = new Uint8Array([0x00, // version
  4019. 0x00, 0x00, 0x00, // flags
  4020. 0x00, 0x00, 0x00, 0x00, // sample_size
  4021. 0x00, 0x00, 0x00, 0x00 // sample_count
  4022. ]);
  4023. STTS = STCO;
  4024. VMHD = new Uint8Array([0x00, // version
  4025. 0x00, 0x00, 0x01, // flags
  4026. 0x00, 0x00, // graphicsmode
  4027. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
  4028. ]);
  4029. })();
  4030. box = function (type) {
  4031. var payload = [],
  4032. size = 0,
  4033. i,
  4034. result,
  4035. view;
  4036. for (i = 1; i < arguments.length; i++) {
  4037. payload.push(arguments[i]);
  4038. }
  4039. i = payload.length; // calculate the total size we need to allocate
  4040. while (i--) {
  4041. size += payload[i].byteLength;
  4042. }
  4043. result = new Uint8Array(size + 8);
  4044. view = new DataView(result.buffer, result.byteOffset, result.byteLength);
  4045. view.setUint32(0, result.byteLength);
  4046. result.set(type, 4); // copy the payload into the result
  4047. for (i = 0, size = 8; i < payload.length; i++) {
  4048. result.set(payload[i], size);
  4049. size += payload[i].byteLength;
  4050. }
  4051. return result;
  4052. };
  4053. dinf = function () {
  4054. return box(types.dinf, box(types.dref, DREF));
  4055. };
  4056. esds = function (track) {
  4057. return box(types.esds, new Uint8Array([0x00, // version
  4058. 0x00, 0x00, 0x00, // flags
  4059. // ES_Descriptor
  4060. 0x03, // tag, ES_DescrTag
  4061. 0x19, // length
  4062. 0x00, 0x00, // ES_ID
  4063. 0x00, // streamDependenceFlag, URL_flag, reserved, streamPriority
  4064. // DecoderConfigDescriptor
  4065. 0x04, // tag, DecoderConfigDescrTag
  4066. 0x11, // length
  4067. 0x40, // object type
  4068. 0x15, // streamType
  4069. 0x00, 0x06, 0x00, // bufferSizeDB
  4070. 0x00, 0x00, 0xda, 0xc0, // maxBitrate
  4071. 0x00, 0x00, 0xda, 0xc0, // avgBitrate
  4072. // DecoderSpecificInfo
  4073. 0x05, // tag, DecoderSpecificInfoTag
  4074. 0x02, // length
  4075. // ISO/IEC 14496-3, AudioSpecificConfig
  4076. // for samplingFrequencyIndex see ISO/IEC 13818-7:2006, 8.1.3.2.2, Table 35
  4077. track.audioobjecttype << 3 | track.samplingfrequencyindex >>> 1, track.samplingfrequencyindex << 7 | track.channelcount << 3, 0x06, 0x01, 0x02 // GASpecificConfig
  4078. ]));
  4079. };
  4080. ftyp = function () {
  4081. return box(types.ftyp, MAJOR_BRAND, MINOR_VERSION, MAJOR_BRAND, AVC1_BRAND);
  4082. };
  4083. hdlr = function (type) {
  4084. return box(types.hdlr, HDLR_TYPES[type]);
  4085. };
  4086. mdat = function (data) {
  4087. return box(types.mdat, data);
  4088. };
  4089. mdhd = function (track) {
  4090. var result = new Uint8Array([0x00, // version 0
  4091. 0x00, 0x00, 0x00, // flags
  4092. 0x00, 0x00, 0x00, 0x02, // creation_time
  4093. 0x00, 0x00, 0x00, 0x03, // modification_time
  4094. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  4095. track.duration >>> 24 & 0xFF, track.duration >>> 16 & 0xFF, track.duration >>> 8 & 0xFF, track.duration & 0xFF, // duration
  4096. 0x55, 0xc4, // 'und' language (undetermined)
  4097. 0x00, 0x00]); // Use the sample rate from the track metadata, when it is
  4098. // defined. The sample rate can be parsed out of an ADTS header, for
  4099. // instance.
  4100. if (track.samplerate) {
  4101. result[12] = track.samplerate >>> 24 & 0xFF;
  4102. result[13] = track.samplerate >>> 16 & 0xFF;
  4103. result[14] = track.samplerate >>> 8 & 0xFF;
  4104. result[15] = track.samplerate & 0xFF;
  4105. }
  4106. return box(types.mdhd, result);
  4107. };
  4108. mdia = function (track) {
  4109. return box(types.mdia, mdhd(track), hdlr(track.type), minf(track));
  4110. };
  4111. mfhd = function (sequenceNumber) {
  4112. return box(types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // flags
  4113. (sequenceNumber & 0xFF000000) >> 24, (sequenceNumber & 0xFF0000) >> 16, (sequenceNumber & 0xFF00) >> 8, sequenceNumber & 0xFF // sequence_number
  4114. ]));
  4115. };
  4116. minf = function (track) {
  4117. return box(types.minf, track.type === 'video' ? box(types.vmhd, VMHD) : box(types.smhd, SMHD), dinf(), stbl(track));
  4118. };
  4119. moof = function (sequenceNumber, tracks) {
  4120. var trackFragments = [],
  4121. i = tracks.length; // build traf boxes for each track fragment
  4122. while (i--) {
  4123. trackFragments[i] = traf(tracks[i]);
  4124. }
  4125. return box.apply(null, [types.moof, mfhd(sequenceNumber)].concat(trackFragments));
  4126. };
  4127. /**
  4128. * Returns a movie box.
  4129. * @param tracks {array} the tracks associated with this movie
  4130. * @see ISO/IEC 14496-12:2012(E), section 8.2.1
  4131. */
  4132. moov = function (tracks) {
  4133. var i = tracks.length,
  4134. boxes = [];
  4135. while (i--) {
  4136. boxes[i] = trak(tracks[i]);
  4137. }
  4138. return box.apply(null, [types.moov, mvhd(0xffffffff)].concat(boxes).concat(mvex(tracks)));
  4139. };
  4140. mvex = function (tracks) {
  4141. var i = tracks.length,
  4142. boxes = [];
  4143. while (i--) {
  4144. boxes[i] = trex(tracks[i]);
  4145. }
  4146. return box.apply(null, [types.mvex].concat(boxes));
  4147. };
  4148. mvhd = function (duration) {
  4149. var bytes = new Uint8Array([0x00, // version 0
  4150. 0x00, 0x00, 0x00, // flags
  4151. 0x00, 0x00, 0x00, 0x01, // creation_time
  4152. 0x00, 0x00, 0x00, 0x02, // modification_time
  4153. 0x00, 0x01, 0x5f, 0x90, // timescale, 90,000 "ticks" per second
  4154. (duration & 0xFF000000) >> 24, (duration & 0xFF0000) >> 16, (duration & 0xFF00) >> 8, duration & 0xFF, // duration
  4155. 0x00, 0x01, 0x00, 0x00, // 1.0 rate
  4156. 0x01, 0x00, // 1.0 volume
  4157. 0x00, 0x00, // reserved
  4158. 0x00, 0x00, 0x00, 0x00, // reserved
  4159. 0x00, 0x00, 0x00, 0x00, // reserved
  4160. 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
  4161. 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
  4162. 0xff, 0xff, 0xff, 0xff // next_track_ID
  4163. ]);
  4164. return box(types.mvhd, bytes);
  4165. };
  4166. sdtp = function (track) {
  4167. var samples = track.samples || [],
  4168. bytes = new Uint8Array(4 + samples.length),
  4169. flags,
  4170. i; // leave the full box header (4 bytes) all zero
  4171. // write the sample table
  4172. for (i = 0; i < samples.length; i++) {
  4173. flags = samples[i].flags;
  4174. bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
  4175. }
  4176. return box(types.sdtp, bytes);
  4177. };
  4178. stbl = function (track) {
  4179. return box(types.stbl, stsd(track), box(types.stts, STTS), box(types.stsc, STSC), box(types.stsz, STSZ), box(types.stco, STCO));
  4180. };
  4181. (function () {
  4182. var videoSample, audioSample;
  4183. stsd = function (track) {
  4184. return box(types.stsd, new Uint8Array([0x00, // version 0
  4185. 0x00, 0x00, 0x00, // flags
  4186. 0x00, 0x00, 0x00, 0x01]), track.type === 'video' ? videoSample(track) : audioSample(track));
  4187. };
  4188. videoSample = function (track) {
  4189. var sps = track.sps || [],
  4190. pps = track.pps || [],
  4191. sequenceParameterSets = [],
  4192. pictureParameterSets = [],
  4193. i,
  4194. avc1Box; // assemble the SPSs
  4195. for (i = 0; i < sps.length; i++) {
  4196. sequenceParameterSets.push((sps[i].byteLength & 0xFF00) >>> 8);
  4197. sequenceParameterSets.push(sps[i].byteLength & 0xFF); // sequenceParameterSetLength
  4198. sequenceParameterSets = sequenceParameterSets.concat(Array.prototype.slice.call(sps[i])); // SPS
  4199. } // assemble the PPSs
  4200. for (i = 0; i < pps.length; i++) {
  4201. pictureParameterSets.push((pps[i].byteLength & 0xFF00) >>> 8);
  4202. pictureParameterSets.push(pps[i].byteLength & 0xFF);
  4203. pictureParameterSets = pictureParameterSets.concat(Array.prototype.slice.call(pps[i]));
  4204. }
  4205. avc1Box = [types.avc1, new Uint8Array([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  4206. 0x00, 0x01, // data_reference_index
  4207. 0x00, 0x00, // pre_defined
  4208. 0x00, 0x00, // reserved
  4209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  4210. (track.width & 0xff00) >> 8, track.width & 0xff, // width
  4211. (track.height & 0xff00) >> 8, track.height & 0xff, // height
  4212. 0x00, 0x48, 0x00, 0x00, // horizresolution
  4213. 0x00, 0x48, 0x00, 0x00, // vertresolution
  4214. 0x00, 0x00, 0x00, 0x00, // reserved
  4215. 0x00, 0x01, // frame_count
  4216. 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
  4217. 0x00, 0x18, // depth = 24
  4218. 0x11, 0x11 // pre_defined = -1
  4219. ]), box(types.avcC, new Uint8Array([0x01, // configurationVersion
  4220. track.profileIdc, // AVCProfileIndication
  4221. track.profileCompatibility, // profile_compatibility
  4222. track.levelIdc, // AVCLevelIndication
  4223. 0xff // lengthSizeMinusOne, hard-coded to 4 bytes
  4224. ].concat([sps.length], // numOfSequenceParameterSets
  4225. sequenceParameterSets, // "SPS"
  4226. [pps.length], // numOfPictureParameterSets
  4227. pictureParameterSets // "PPS"
  4228. ))), box(types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
  4229. 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
  4230. 0x00, 0x2d, 0xc6, 0xc0 // avgBitrate
  4231. ]))];
  4232. if (track.sarRatio) {
  4233. var hSpacing = track.sarRatio[0],
  4234. vSpacing = track.sarRatio[1];
  4235. 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])));
  4236. }
  4237. return box.apply(null, avc1Box);
  4238. };
  4239. audioSample = function (track) {
  4240. return box(types.mp4a, new Uint8Array([// SampleEntry, ISO/IEC 14496-12
  4241. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  4242. 0x00, 0x01, // data_reference_index
  4243. // AudioSampleEntry, ISO/IEC 14496-12
  4244. 0x00, 0x00, 0x00, 0x00, // reserved
  4245. 0x00, 0x00, 0x00, 0x00, // reserved
  4246. (track.channelcount & 0xff00) >> 8, track.channelcount & 0xff, // channelcount
  4247. (track.samplesize & 0xff00) >> 8, track.samplesize & 0xff, // samplesize
  4248. 0x00, 0x00, // pre_defined
  4249. 0x00, 0x00, // reserved
  4250. (track.samplerate & 0xff00) >> 8, track.samplerate & 0xff, 0x00, 0x00 // samplerate, 16.16
  4251. // MP4AudioSampleEntry, ISO/IEC 14496-14
  4252. ]), esds(track));
  4253. };
  4254. })();
  4255. tkhd = function (track) {
  4256. var result = new Uint8Array([0x00, // version 0
  4257. 0x00, 0x00, 0x07, // flags
  4258. 0x00, 0x00, 0x00, 0x00, // creation_time
  4259. 0x00, 0x00, 0x00, 0x00, // modification_time
  4260. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  4261. 0x00, 0x00, 0x00, 0x00, // reserved
  4262. (track.duration & 0xFF000000) >> 24, (track.duration & 0xFF0000) >> 16, (track.duration & 0xFF00) >> 8, track.duration & 0xFF, // duration
  4263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  4264. 0x00, 0x00, // layer
  4265. 0x00, 0x00, // alternate_group
  4266. 0x01, 0x00, // non-audio track volume
  4267. 0x00, 0x00, // reserved
  4268. 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
  4269. (track.width & 0xFF00) >> 8, track.width & 0xFF, 0x00, 0x00, // width
  4270. (track.height & 0xFF00) >> 8, track.height & 0xFF, 0x00, 0x00 // height
  4271. ]);
  4272. return box(types.tkhd, result);
  4273. };
  4274. /**
  4275. * Generate a track fragment (traf) box. A traf box collects metadata
  4276. * about tracks in a movie fragment (moof) box.
  4277. */
  4278. traf = function (track) {
  4279. var trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable, dataOffset, upperWordBaseMediaDecodeTime, lowerWordBaseMediaDecodeTime;
  4280. trackFragmentHeader = box(types.tfhd, new Uint8Array([0x00, // version 0
  4281. 0x00, 0x00, 0x3a, // flags
  4282. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  4283. 0x00, 0x00, 0x00, 0x01, // sample_description_index
  4284. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  4285. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  4286. 0x00, 0x00, 0x00, 0x00 // default_sample_flags
  4287. ]));
  4288. upperWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime / MAX_UINT32);
  4289. lowerWordBaseMediaDecodeTime = Math.floor(track.baseMediaDecodeTime % MAX_UINT32);
  4290. trackFragmentDecodeTime = box(types.tfdt, new Uint8Array([0x01, // version 1
  4291. 0x00, 0x00, 0x00, // flags
  4292. // baseMediaDecodeTime
  4293. 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
  4294. // the containing moof to the first payload byte of the associated
  4295. // mdat
  4296. dataOffset = 32 + // tfhd
  4297. 20 + // tfdt
  4298. 8 + // traf header
  4299. 16 + // mfhd
  4300. 8 + // moof header
  4301. 8; // mdat header
  4302. // audio tracks require less metadata
  4303. if (track.type === 'audio') {
  4304. trackFragmentRun = trun$1(track, dataOffset);
  4305. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun);
  4306. } // video tracks should contain an independent and disposable samples
  4307. // box (sdtp)
  4308. // generate one and adjust offsets to match
  4309. sampleDependencyTable = sdtp(track);
  4310. trackFragmentRun = trun$1(track, sampleDependencyTable.length + dataOffset);
  4311. return box(types.traf, trackFragmentHeader, trackFragmentDecodeTime, trackFragmentRun, sampleDependencyTable);
  4312. };
  4313. /**
  4314. * Generate a track box.
  4315. * @param track {object} a track definition
  4316. * @return {Uint8Array} the track box
  4317. */
  4318. trak = function (track) {
  4319. track.duration = track.duration || 0xffffffff;
  4320. return box(types.trak, tkhd(track), mdia(track));
  4321. };
  4322. trex = function (track) {
  4323. var result = new Uint8Array([0x00, // version 0
  4324. 0x00, 0x00, 0x00, // flags
  4325. (track.id & 0xFF000000) >> 24, (track.id & 0xFF0000) >> 16, (track.id & 0xFF00) >> 8, track.id & 0xFF, // track_ID
  4326. 0x00, 0x00, 0x00, 0x01, // default_sample_description_index
  4327. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  4328. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  4329. 0x00, 0x01, 0x00, 0x01 // default_sample_flags
  4330. ]); // the last two bytes of default_sample_flags is the sample
  4331. // degradation priority, a hint about the importance of this sample
  4332. // relative to others. Lower the degradation priority for all sample
  4333. // types other than video.
  4334. if (track.type !== 'video') {
  4335. result[result.length - 1] = 0x00;
  4336. }
  4337. return box(types.trex, result);
  4338. };
  4339. (function () {
  4340. var audioTrun, videoTrun, trunHeader; // This method assumes all samples are uniform. That is, if a
  4341. // duration is present for the first sample, it will be present for
  4342. // all subsequent samples.
  4343. // see ISO/IEC 14496-12:2012, Section 8.8.8.1
  4344. trunHeader = function (samples, offset) {
  4345. var durationPresent = 0,
  4346. sizePresent = 0,
  4347. flagsPresent = 0,
  4348. compositionTimeOffset = 0; // trun flag constants
  4349. if (samples.length) {
  4350. if (samples[0].duration !== undefined) {
  4351. durationPresent = 0x1;
  4352. }
  4353. if (samples[0].size !== undefined) {
  4354. sizePresent = 0x2;
  4355. }
  4356. if (samples[0].flags !== undefined) {
  4357. flagsPresent = 0x4;
  4358. }
  4359. if (samples[0].compositionTimeOffset !== undefined) {
  4360. compositionTimeOffset = 0x8;
  4361. }
  4362. }
  4363. return [0x00, // version 0
  4364. 0x00, durationPresent | sizePresent | flagsPresent | compositionTimeOffset, 0x01, // flags
  4365. (samples.length & 0xFF000000) >>> 24, (samples.length & 0xFF0000) >>> 16, (samples.length & 0xFF00) >>> 8, samples.length & 0xFF, // sample_count
  4366. (offset & 0xFF000000) >>> 24, (offset & 0xFF0000) >>> 16, (offset & 0xFF00) >>> 8, offset & 0xFF // data_offset
  4367. ];
  4368. };
  4369. videoTrun = function (track, offset) {
  4370. var bytesOffest, bytes, header, samples, sample, i;
  4371. samples = track.samples || [];
  4372. offset += 8 + 12 + 16 * samples.length;
  4373. header = trunHeader(samples, offset);
  4374. bytes = new Uint8Array(header.length + samples.length * 16);
  4375. bytes.set(header);
  4376. bytesOffest = header.length;
  4377. for (i = 0; i < samples.length; i++) {
  4378. sample = samples[i];
  4379. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  4380. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  4381. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  4382. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  4383. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  4384. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  4385. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  4386. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  4387. bytes[bytesOffest++] = sample.flags.isLeading << 2 | sample.flags.dependsOn;
  4388. bytes[bytesOffest++] = sample.flags.isDependedOn << 6 | sample.flags.hasRedundancy << 4 | sample.flags.paddingValue << 1 | sample.flags.isNonSyncSample;
  4389. bytes[bytesOffest++] = sample.flags.degradationPriority & 0xF0 << 8;
  4390. bytes[bytesOffest++] = sample.flags.degradationPriority & 0x0F; // sample_flags
  4391. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF000000) >>> 24;
  4392. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF0000) >>> 16;
  4393. bytes[bytesOffest++] = (sample.compositionTimeOffset & 0xFF00) >>> 8;
  4394. bytes[bytesOffest++] = sample.compositionTimeOffset & 0xFF; // sample_composition_time_offset
  4395. }
  4396. return box(types.trun, bytes);
  4397. };
  4398. audioTrun = function (track, offset) {
  4399. var bytes, bytesOffest, header, samples, sample, i;
  4400. samples = track.samples || [];
  4401. offset += 8 + 12 + 8 * samples.length;
  4402. header = trunHeader(samples, offset);
  4403. bytes = new Uint8Array(header.length + samples.length * 8);
  4404. bytes.set(header);
  4405. bytesOffest = header.length;
  4406. for (i = 0; i < samples.length; i++) {
  4407. sample = samples[i];
  4408. bytes[bytesOffest++] = (sample.duration & 0xFF000000) >>> 24;
  4409. bytes[bytesOffest++] = (sample.duration & 0xFF0000) >>> 16;
  4410. bytes[bytesOffest++] = (sample.duration & 0xFF00) >>> 8;
  4411. bytes[bytesOffest++] = sample.duration & 0xFF; // sample_duration
  4412. bytes[bytesOffest++] = (sample.size & 0xFF000000) >>> 24;
  4413. bytes[bytesOffest++] = (sample.size & 0xFF0000) >>> 16;
  4414. bytes[bytesOffest++] = (sample.size & 0xFF00) >>> 8;
  4415. bytes[bytesOffest++] = sample.size & 0xFF; // sample_size
  4416. }
  4417. return box(types.trun, bytes);
  4418. };
  4419. trun$1 = function (track, offset) {
  4420. if (track.type === 'audio') {
  4421. return audioTrun(track, offset);
  4422. }
  4423. return videoTrun(track, offset);
  4424. };
  4425. })();
  4426. var mp4Generator = {
  4427. ftyp: ftyp,
  4428. mdat: mdat,
  4429. moof: moof,
  4430. moov: moov,
  4431. initSegment: function (tracks) {
  4432. var fileType = ftyp(),
  4433. movie = moov(tracks),
  4434. result;
  4435. result = new Uint8Array(fileType.byteLength + movie.byteLength);
  4436. result.set(fileType);
  4437. result.set(movie, fileType.byteLength);
  4438. return result;
  4439. }
  4440. };
  4441. /**
  4442. * mux.js
  4443. *
  4444. * Copyright (c) Brightcove
  4445. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4446. */
  4447. // composed of the nal units that make up that frame
  4448. // Also keep track of cummulative data about the frame from the nal units such
  4449. // as the frame duration, starting pts, etc.
  4450. var groupNalsIntoFrames = function (nalUnits) {
  4451. var i,
  4452. currentNal,
  4453. currentFrame = [],
  4454. frames = []; // TODO added for LHLS, make sure this is OK
  4455. frames.byteLength = 0;
  4456. frames.nalCount = 0;
  4457. frames.duration = 0;
  4458. currentFrame.byteLength = 0;
  4459. for (i = 0; i < nalUnits.length; i++) {
  4460. currentNal = nalUnits[i]; // Split on 'aud'-type nal units
  4461. if (currentNal.nalUnitType === 'access_unit_delimiter_rbsp') {
  4462. // Since the very first nal unit is expected to be an AUD
  4463. // only push to the frames array when currentFrame is not empty
  4464. if (currentFrame.length) {
  4465. currentFrame.duration = currentNal.dts - currentFrame.dts; // TODO added for LHLS, make sure this is OK
  4466. frames.byteLength += currentFrame.byteLength;
  4467. frames.nalCount += currentFrame.length;
  4468. frames.duration += currentFrame.duration;
  4469. frames.push(currentFrame);
  4470. }
  4471. currentFrame = [currentNal];
  4472. currentFrame.byteLength = currentNal.data.byteLength;
  4473. currentFrame.pts = currentNal.pts;
  4474. currentFrame.dts = currentNal.dts;
  4475. } else {
  4476. // Specifically flag key frames for ease of use later
  4477. if (currentNal.nalUnitType === 'slice_layer_without_partitioning_rbsp_idr') {
  4478. currentFrame.keyFrame = true;
  4479. }
  4480. currentFrame.duration = currentNal.dts - currentFrame.dts;
  4481. currentFrame.byteLength += currentNal.data.byteLength;
  4482. currentFrame.push(currentNal);
  4483. }
  4484. } // For the last frame, use the duration of the previous frame if we
  4485. // have nothing better to go on
  4486. if (frames.length && (!currentFrame.duration || currentFrame.duration <= 0)) {
  4487. currentFrame.duration = frames[frames.length - 1].duration;
  4488. } // Push the final frame
  4489. // TODO added for LHLS, make sure this is OK
  4490. frames.byteLength += currentFrame.byteLength;
  4491. frames.nalCount += currentFrame.length;
  4492. frames.duration += currentFrame.duration;
  4493. frames.push(currentFrame);
  4494. return frames;
  4495. }; // Convert an array of frames into an array of Gop with each Gop being composed
  4496. // of the frames that make up that Gop
  4497. // Also keep track of cummulative data about the Gop from the frames such as the
  4498. // Gop duration, starting pts, etc.
  4499. var groupFramesIntoGops = function (frames) {
  4500. var i,
  4501. currentFrame,
  4502. currentGop = [],
  4503. gops = []; // We must pre-set some of the values on the Gop since we
  4504. // keep running totals of these values
  4505. currentGop.byteLength = 0;
  4506. currentGop.nalCount = 0;
  4507. currentGop.duration = 0;
  4508. currentGop.pts = frames[0].pts;
  4509. currentGop.dts = frames[0].dts; // store some metadata about all the Gops
  4510. gops.byteLength = 0;
  4511. gops.nalCount = 0;
  4512. gops.duration = 0;
  4513. gops.pts = frames[0].pts;
  4514. gops.dts = frames[0].dts;
  4515. for (i = 0; i < frames.length; i++) {
  4516. currentFrame = frames[i];
  4517. if (currentFrame.keyFrame) {
  4518. // Since the very first frame is expected to be an keyframe
  4519. // only push to the gops array when currentGop is not empty
  4520. if (currentGop.length) {
  4521. gops.push(currentGop);
  4522. gops.byteLength += currentGop.byteLength;
  4523. gops.nalCount += currentGop.nalCount;
  4524. gops.duration += currentGop.duration;
  4525. }
  4526. currentGop = [currentFrame];
  4527. currentGop.nalCount = currentFrame.length;
  4528. currentGop.byteLength = currentFrame.byteLength;
  4529. currentGop.pts = currentFrame.pts;
  4530. currentGop.dts = currentFrame.dts;
  4531. currentGop.duration = currentFrame.duration;
  4532. } else {
  4533. currentGop.duration += currentFrame.duration;
  4534. currentGop.nalCount += currentFrame.length;
  4535. currentGop.byteLength += currentFrame.byteLength;
  4536. currentGop.push(currentFrame);
  4537. }
  4538. }
  4539. if (gops.length && currentGop.duration <= 0) {
  4540. currentGop.duration = gops[gops.length - 1].duration;
  4541. }
  4542. gops.byteLength += currentGop.byteLength;
  4543. gops.nalCount += currentGop.nalCount;
  4544. gops.duration += currentGop.duration; // push the final Gop
  4545. gops.push(currentGop);
  4546. return gops;
  4547. };
  4548. /*
  4549. * Search for the first keyframe in the GOPs and throw away all frames
  4550. * until that keyframe. Then extend the duration of the pulled keyframe
  4551. * and pull the PTS and DTS of the keyframe so that it covers the time
  4552. * range of the frames that were disposed.
  4553. *
  4554. * @param {Array} gops video GOPs
  4555. * @returns {Array} modified video GOPs
  4556. */
  4557. var extendFirstKeyFrame = function (gops) {
  4558. var currentGop;
  4559. if (!gops[0][0].keyFrame && gops.length > 1) {
  4560. // Remove the first GOP
  4561. currentGop = gops.shift();
  4562. gops.byteLength -= currentGop.byteLength;
  4563. gops.nalCount -= currentGop.nalCount; // Extend the first frame of what is now the
  4564. // first gop to cover the time period of the
  4565. // frames we just removed
  4566. gops[0][0].dts = currentGop.dts;
  4567. gops[0][0].pts = currentGop.pts;
  4568. gops[0][0].duration += currentGop.duration;
  4569. }
  4570. return gops;
  4571. };
  4572. /**
  4573. * Default sample object
  4574. * see ISO/IEC 14496-12:2012, section 8.6.4.3
  4575. */
  4576. var createDefaultSample = function () {
  4577. return {
  4578. size: 0,
  4579. flags: {
  4580. isLeading: 0,
  4581. dependsOn: 1,
  4582. isDependedOn: 0,
  4583. hasRedundancy: 0,
  4584. degradationPriority: 0,
  4585. isNonSyncSample: 1
  4586. }
  4587. };
  4588. };
  4589. /*
  4590. * Collates information from a video frame into an object for eventual
  4591. * entry into an MP4 sample table.
  4592. *
  4593. * @param {Object} frame the video frame
  4594. * @param {Number} dataOffset the byte offset to position the sample
  4595. * @return {Object} object containing sample table info for a frame
  4596. */
  4597. var sampleForFrame = function (frame, dataOffset) {
  4598. var sample = createDefaultSample();
  4599. sample.dataOffset = dataOffset;
  4600. sample.compositionTimeOffset = frame.pts - frame.dts;
  4601. sample.duration = frame.duration;
  4602. sample.size = 4 * frame.length; // Space for nal unit size
  4603. sample.size += frame.byteLength;
  4604. if (frame.keyFrame) {
  4605. sample.flags.dependsOn = 2;
  4606. sample.flags.isNonSyncSample = 0;
  4607. }
  4608. return sample;
  4609. }; // generate the track's sample table from an array of gops
  4610. var generateSampleTable$1 = function (gops, baseDataOffset) {
  4611. var h,
  4612. i,
  4613. sample,
  4614. currentGop,
  4615. currentFrame,
  4616. dataOffset = baseDataOffset || 0,
  4617. samples = [];
  4618. for (h = 0; h < gops.length; h++) {
  4619. currentGop = gops[h];
  4620. for (i = 0; i < currentGop.length; i++) {
  4621. currentFrame = currentGop[i];
  4622. sample = sampleForFrame(currentFrame, dataOffset);
  4623. dataOffset += sample.size;
  4624. samples.push(sample);
  4625. }
  4626. }
  4627. return samples;
  4628. }; // generate the track's raw mdat data from an array of gops
  4629. var concatenateNalData = function (gops) {
  4630. var h,
  4631. i,
  4632. j,
  4633. currentGop,
  4634. currentFrame,
  4635. currentNal,
  4636. dataOffset = 0,
  4637. nalsByteLength = gops.byteLength,
  4638. numberOfNals = gops.nalCount,
  4639. totalByteLength = nalsByteLength + 4 * numberOfNals,
  4640. data = new Uint8Array(totalByteLength),
  4641. view = new DataView(data.buffer); // For each Gop..
  4642. for (h = 0; h < gops.length; h++) {
  4643. currentGop = gops[h]; // For each Frame..
  4644. for (i = 0; i < currentGop.length; i++) {
  4645. currentFrame = currentGop[i]; // For each NAL..
  4646. for (j = 0; j < currentFrame.length; j++) {
  4647. currentNal = currentFrame[j];
  4648. view.setUint32(dataOffset, currentNal.data.byteLength);
  4649. dataOffset += 4;
  4650. data.set(currentNal.data, dataOffset);
  4651. dataOffset += currentNal.data.byteLength;
  4652. }
  4653. }
  4654. }
  4655. return data;
  4656. }; // generate the track's sample table from a frame
  4657. var generateSampleTableForFrame = function (frame, baseDataOffset) {
  4658. var sample,
  4659. dataOffset = baseDataOffset || 0,
  4660. samples = [];
  4661. sample = sampleForFrame(frame, dataOffset);
  4662. samples.push(sample);
  4663. return samples;
  4664. }; // generate the track's raw mdat data from a frame
  4665. var concatenateNalDataForFrame = function (frame) {
  4666. var i,
  4667. currentNal,
  4668. dataOffset = 0,
  4669. nalsByteLength = frame.byteLength,
  4670. numberOfNals = frame.length,
  4671. totalByteLength = nalsByteLength + 4 * numberOfNals,
  4672. data = new Uint8Array(totalByteLength),
  4673. view = new DataView(data.buffer); // For each NAL..
  4674. for (i = 0; i < frame.length; i++) {
  4675. currentNal = frame[i];
  4676. view.setUint32(dataOffset, currentNal.data.byteLength);
  4677. dataOffset += 4;
  4678. data.set(currentNal.data, dataOffset);
  4679. dataOffset += currentNal.data.byteLength;
  4680. }
  4681. return data;
  4682. };
  4683. var frameUtils$1 = {
  4684. groupNalsIntoFrames: groupNalsIntoFrames,
  4685. groupFramesIntoGops: groupFramesIntoGops,
  4686. extendFirstKeyFrame: extendFirstKeyFrame,
  4687. generateSampleTable: generateSampleTable$1,
  4688. concatenateNalData: concatenateNalData,
  4689. generateSampleTableForFrame: generateSampleTableForFrame,
  4690. concatenateNalDataForFrame: concatenateNalDataForFrame
  4691. };
  4692. /**
  4693. * mux.js
  4694. *
  4695. * Copyright (c) Brightcove
  4696. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4697. */
  4698. var highPrefix = [33, 16, 5, 32, 164, 27];
  4699. var lowPrefix = [33, 65, 108, 84, 1, 2, 4, 8, 168, 2, 4, 8, 17, 191, 252];
  4700. var zeroFill = function (count) {
  4701. var a = [];
  4702. while (count--) {
  4703. a.push(0);
  4704. }
  4705. return a;
  4706. };
  4707. var makeTable = function (metaTable) {
  4708. return Object.keys(metaTable).reduce(function (obj, key) {
  4709. obj[key] = new Uint8Array(metaTable[key].reduce(function (arr, part) {
  4710. return arr.concat(part);
  4711. }, []));
  4712. return obj;
  4713. }, {});
  4714. };
  4715. var silence;
  4716. var silence_1 = function () {
  4717. if (!silence) {
  4718. // Frames-of-silence to use for filling in missing AAC frames
  4719. var coneOfSilence = {
  4720. 96000: [highPrefix, [227, 64], zeroFill(154), [56]],
  4721. 88200: [highPrefix, [231], zeroFill(170), [56]],
  4722. 64000: [highPrefix, [248, 192], zeroFill(240), [56]],
  4723. 48000: [highPrefix, [255, 192], zeroFill(268), [55, 148, 128], zeroFill(54), [112]],
  4724. 44100: [highPrefix, [255, 192], zeroFill(268), [55, 163, 128], zeroFill(84), [112]],
  4725. 32000: [highPrefix, [255, 192], zeroFill(268), [55, 234], zeroFill(226), [112]],
  4726. 24000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 112], zeroFill(126), [224]],
  4727. 16000: [highPrefix, [255, 192], zeroFill(268), [55, 255, 128], zeroFill(268), [111, 255], zeroFill(269), [223, 108], zeroFill(195), [1, 192]],
  4728. 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]],
  4729. 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]],
  4730. 8000: [lowPrefix, zeroFill(268), [3, 121, 16], zeroFill(47), [7]]
  4731. };
  4732. silence = makeTable(coneOfSilence);
  4733. }
  4734. return silence;
  4735. };
  4736. /**
  4737. * mux.js
  4738. *
  4739. * Copyright (c) Brightcove
  4740. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4741. */
  4742. var ONE_SECOND_IN_TS$4 = 90000,
  4743. // 90kHz clock
  4744. secondsToVideoTs,
  4745. secondsToAudioTs,
  4746. videoTsToSeconds,
  4747. audioTsToSeconds,
  4748. audioTsToVideoTs,
  4749. videoTsToAudioTs,
  4750. metadataTsToSeconds;
  4751. secondsToVideoTs = function (seconds) {
  4752. return seconds * ONE_SECOND_IN_TS$4;
  4753. };
  4754. secondsToAudioTs = function (seconds, sampleRate) {
  4755. return seconds * sampleRate;
  4756. };
  4757. videoTsToSeconds = function (timestamp) {
  4758. return timestamp / ONE_SECOND_IN_TS$4;
  4759. };
  4760. audioTsToSeconds = function (timestamp, sampleRate) {
  4761. return timestamp / sampleRate;
  4762. };
  4763. audioTsToVideoTs = function (timestamp, sampleRate) {
  4764. return secondsToVideoTs(audioTsToSeconds(timestamp, sampleRate));
  4765. };
  4766. videoTsToAudioTs = function (timestamp, sampleRate) {
  4767. return secondsToAudioTs(videoTsToSeconds(timestamp), sampleRate);
  4768. };
  4769. /**
  4770. * Adjust ID3 tag or caption timing information by the timeline pts values
  4771. * (if keepOriginalTimestamps is false) and convert to seconds
  4772. */
  4773. metadataTsToSeconds = function (timestamp, timelineStartPts, keepOriginalTimestamps) {
  4774. return videoTsToSeconds(keepOriginalTimestamps ? timestamp : timestamp - timelineStartPts);
  4775. };
  4776. var clock$2 = {
  4777. ONE_SECOND_IN_TS: ONE_SECOND_IN_TS$4,
  4778. secondsToVideoTs: secondsToVideoTs,
  4779. secondsToAudioTs: secondsToAudioTs,
  4780. videoTsToSeconds: videoTsToSeconds,
  4781. audioTsToSeconds: audioTsToSeconds,
  4782. audioTsToVideoTs: audioTsToVideoTs,
  4783. videoTsToAudioTs: videoTsToAudioTs,
  4784. metadataTsToSeconds: metadataTsToSeconds
  4785. };
  4786. /**
  4787. * mux.js
  4788. *
  4789. * Copyright (c) Brightcove
  4790. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4791. */
  4792. var coneOfSilence = silence_1;
  4793. var clock$1 = clock$2;
  4794. /**
  4795. * Sum the `byteLength` properties of the data in each AAC frame
  4796. */
  4797. var sumFrameByteLengths = function (array) {
  4798. var i,
  4799. currentObj,
  4800. sum = 0; // sum the byteLength's all each nal unit in the frame
  4801. for (i = 0; i < array.length; i++) {
  4802. currentObj = array[i];
  4803. sum += currentObj.data.byteLength;
  4804. }
  4805. return sum;
  4806. }; // Possibly pad (prefix) the audio track with silence if appending this track
  4807. // would lead to the introduction of a gap in the audio buffer
  4808. var prefixWithSilence = function (track, frames, audioAppendStartTs, videoBaseMediaDecodeTime) {
  4809. var baseMediaDecodeTimeTs,
  4810. frameDuration = 0,
  4811. audioGapDuration = 0,
  4812. audioFillFrameCount = 0,
  4813. audioFillDuration = 0,
  4814. silentFrame,
  4815. i,
  4816. firstFrame;
  4817. if (!frames.length) {
  4818. return;
  4819. }
  4820. baseMediaDecodeTimeTs = clock$1.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate); // determine frame clock duration based on sample rate, round up to avoid overfills
  4821. frameDuration = Math.ceil(clock$1.ONE_SECOND_IN_TS / (track.samplerate / 1024));
  4822. if (audioAppendStartTs && videoBaseMediaDecodeTime) {
  4823. // insert the shortest possible amount (audio gap or audio to video gap)
  4824. audioGapDuration = baseMediaDecodeTimeTs - Math.max(audioAppendStartTs, videoBaseMediaDecodeTime); // number of full frames in the audio gap
  4825. audioFillFrameCount = Math.floor(audioGapDuration / frameDuration);
  4826. audioFillDuration = audioFillFrameCount * frameDuration;
  4827. } // don't attempt to fill gaps smaller than a single frame or larger
  4828. // than a half second
  4829. if (audioFillFrameCount < 1 || audioFillDuration > clock$1.ONE_SECOND_IN_TS / 2) {
  4830. return;
  4831. }
  4832. silentFrame = coneOfSilence()[track.samplerate];
  4833. if (!silentFrame) {
  4834. // we don't have a silent frame pregenerated for the sample rate, so use a frame
  4835. // from the content instead
  4836. silentFrame = frames[0].data;
  4837. }
  4838. for (i = 0; i < audioFillFrameCount; i++) {
  4839. firstFrame = frames[0];
  4840. frames.splice(0, 0, {
  4841. data: silentFrame,
  4842. dts: firstFrame.dts - frameDuration,
  4843. pts: firstFrame.pts - frameDuration
  4844. });
  4845. }
  4846. track.baseMediaDecodeTime -= Math.floor(clock$1.videoTsToAudioTs(audioFillDuration, track.samplerate));
  4847. return audioFillDuration;
  4848. }; // If the audio segment extends before the earliest allowed dts
  4849. // value, remove AAC frames until starts at or after the earliest
  4850. // allowed DTS so that we don't end up with a negative baseMedia-
  4851. // DecodeTime for the audio track
  4852. var trimAdtsFramesByEarliestDts = function (adtsFrames, track, earliestAllowedDts) {
  4853. if (track.minSegmentDts >= earliestAllowedDts) {
  4854. return adtsFrames;
  4855. } // We will need to recalculate the earliest segment Dts
  4856. track.minSegmentDts = Infinity;
  4857. return adtsFrames.filter(function (currentFrame) {
  4858. // If this is an allowed frame, keep it and record it's Dts
  4859. if (currentFrame.dts >= earliestAllowedDts) {
  4860. track.minSegmentDts = Math.min(track.minSegmentDts, currentFrame.dts);
  4861. track.minSegmentPts = track.minSegmentDts;
  4862. return true;
  4863. } // Otherwise, discard it
  4864. return false;
  4865. });
  4866. }; // generate the track's raw mdat data from an array of frames
  4867. var generateSampleTable = function (frames) {
  4868. var i,
  4869. currentFrame,
  4870. samples = [];
  4871. for (i = 0; i < frames.length; i++) {
  4872. currentFrame = frames[i];
  4873. samples.push({
  4874. size: currentFrame.data.byteLength,
  4875. duration: 1024 // For AAC audio, all samples contain 1024 samples
  4876. });
  4877. }
  4878. return samples;
  4879. }; // generate the track's sample table from an array of frames
  4880. var concatenateFrameData = function (frames) {
  4881. var i,
  4882. currentFrame,
  4883. dataOffset = 0,
  4884. data = new Uint8Array(sumFrameByteLengths(frames));
  4885. for (i = 0; i < frames.length; i++) {
  4886. currentFrame = frames[i];
  4887. data.set(currentFrame.data, dataOffset);
  4888. dataOffset += currentFrame.data.byteLength;
  4889. }
  4890. return data;
  4891. };
  4892. var audioFrameUtils$1 = {
  4893. prefixWithSilence: prefixWithSilence,
  4894. trimAdtsFramesByEarliestDts: trimAdtsFramesByEarliestDts,
  4895. generateSampleTable: generateSampleTable,
  4896. concatenateFrameData: concatenateFrameData
  4897. };
  4898. /**
  4899. * mux.js
  4900. *
  4901. * Copyright (c) Brightcove
  4902. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4903. */
  4904. var ONE_SECOND_IN_TS$3 = clock$2.ONE_SECOND_IN_TS;
  4905. /**
  4906. * Store information about the start and end of the track and the
  4907. * duration for each frame/sample we process in order to calculate
  4908. * the baseMediaDecodeTime
  4909. */
  4910. var collectDtsInfo = function (track, data) {
  4911. if (typeof data.pts === 'number') {
  4912. if (track.timelineStartInfo.pts === undefined) {
  4913. track.timelineStartInfo.pts = data.pts;
  4914. }
  4915. if (track.minSegmentPts === undefined) {
  4916. track.minSegmentPts = data.pts;
  4917. } else {
  4918. track.minSegmentPts = Math.min(track.minSegmentPts, data.pts);
  4919. }
  4920. if (track.maxSegmentPts === undefined) {
  4921. track.maxSegmentPts = data.pts;
  4922. } else {
  4923. track.maxSegmentPts = Math.max(track.maxSegmentPts, data.pts);
  4924. }
  4925. }
  4926. if (typeof data.dts === 'number') {
  4927. if (track.timelineStartInfo.dts === undefined) {
  4928. track.timelineStartInfo.dts = data.dts;
  4929. }
  4930. if (track.minSegmentDts === undefined) {
  4931. track.minSegmentDts = data.dts;
  4932. } else {
  4933. track.minSegmentDts = Math.min(track.minSegmentDts, data.dts);
  4934. }
  4935. if (track.maxSegmentDts === undefined) {
  4936. track.maxSegmentDts = data.dts;
  4937. } else {
  4938. track.maxSegmentDts = Math.max(track.maxSegmentDts, data.dts);
  4939. }
  4940. }
  4941. };
  4942. /**
  4943. * Clear values used to calculate the baseMediaDecodeTime between
  4944. * tracks
  4945. */
  4946. var clearDtsInfo = function (track) {
  4947. delete track.minSegmentDts;
  4948. delete track.maxSegmentDts;
  4949. delete track.minSegmentPts;
  4950. delete track.maxSegmentPts;
  4951. };
  4952. /**
  4953. * Calculate the track's baseMediaDecodeTime based on the earliest
  4954. * DTS the transmuxer has ever seen and the minimum DTS for the
  4955. * current track
  4956. * @param track {object} track metadata configuration
  4957. * @param keepOriginalTimestamps {boolean} If true, keep the timestamps
  4958. * in the source; false to adjust the first segment to start at 0.
  4959. */
  4960. var calculateTrackBaseMediaDecodeTime = function (track, keepOriginalTimestamps) {
  4961. var baseMediaDecodeTime,
  4962. scale,
  4963. minSegmentDts = track.minSegmentDts; // Optionally adjust the time so the first segment starts at zero.
  4964. if (!keepOriginalTimestamps) {
  4965. minSegmentDts -= track.timelineStartInfo.dts;
  4966. } // track.timelineStartInfo.baseMediaDecodeTime is the location, in time, where
  4967. // we want the start of the first segment to be placed
  4968. baseMediaDecodeTime = track.timelineStartInfo.baseMediaDecodeTime; // Add to that the distance this segment is from the very first
  4969. baseMediaDecodeTime += minSegmentDts; // baseMediaDecodeTime must not become negative
  4970. baseMediaDecodeTime = Math.max(0, baseMediaDecodeTime);
  4971. if (track.type === 'audio') {
  4972. // Audio has a different clock equal to the sampling_rate so we need to
  4973. // scale the PTS values into the clock rate of the track
  4974. scale = track.samplerate / ONE_SECOND_IN_TS$3;
  4975. baseMediaDecodeTime *= scale;
  4976. baseMediaDecodeTime = Math.floor(baseMediaDecodeTime);
  4977. }
  4978. return baseMediaDecodeTime;
  4979. };
  4980. var trackDecodeInfo$1 = {
  4981. clearDtsInfo: clearDtsInfo,
  4982. calculateTrackBaseMediaDecodeTime: calculateTrackBaseMediaDecodeTime,
  4983. collectDtsInfo: collectDtsInfo
  4984. };
  4985. /**
  4986. * mux.js
  4987. *
  4988. * Copyright (c) Brightcove
  4989. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  4990. *
  4991. * Reads in-band caption information from a video elementary
  4992. * stream. Captions must follow the CEA-708 standard for injection
  4993. * into an MPEG-2 transport streams.
  4994. * @see https://en.wikipedia.org/wiki/CEA-708
  4995. * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
  4996. */
  4997. // payload type field to indicate how they are to be
  4998. // interpreted. CEAS-708 caption content is always transmitted with
  4999. // payload type 0x04.
  5000. var USER_DATA_REGISTERED_ITU_T_T35 = 4,
  5001. RBSP_TRAILING_BITS = 128;
  5002. /**
  5003. * Parse a supplemental enhancement information (SEI) NAL unit.
  5004. * Stops parsing once a message of type ITU T T35 has been found.
  5005. *
  5006. * @param bytes {Uint8Array} the bytes of a SEI NAL unit
  5007. * @return {object} the parsed SEI payload
  5008. * @see Rec. ITU-T H.264, 7.3.2.3.1
  5009. */
  5010. var parseSei = function (bytes) {
  5011. var i = 0,
  5012. result = {
  5013. payloadType: -1,
  5014. payloadSize: 0
  5015. },
  5016. payloadType = 0,
  5017. payloadSize = 0; // go through the sei_rbsp parsing each each individual sei_message
  5018. while (i < bytes.byteLength) {
  5019. // stop once we have hit the end of the sei_rbsp
  5020. if (bytes[i] === RBSP_TRAILING_BITS) {
  5021. break;
  5022. } // Parse payload type
  5023. while (bytes[i] === 0xFF) {
  5024. payloadType += 255;
  5025. i++;
  5026. }
  5027. payloadType += bytes[i++]; // Parse payload size
  5028. while (bytes[i] === 0xFF) {
  5029. payloadSize += 255;
  5030. i++;
  5031. }
  5032. payloadSize += bytes[i++]; // this sei_message is a 608/708 caption so save it and break
  5033. // there can only ever be one caption message in a frame's sei
  5034. if (!result.payload && payloadType === USER_DATA_REGISTERED_ITU_T_T35) {
  5035. var userIdentifier = String.fromCharCode(bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6]);
  5036. if (userIdentifier === 'GA94') {
  5037. result.payloadType = payloadType;
  5038. result.payloadSize = payloadSize;
  5039. result.payload = bytes.subarray(i, i + payloadSize);
  5040. break;
  5041. } else {
  5042. result.payload = void 0;
  5043. }
  5044. } // skip the payload and parse the next message
  5045. i += payloadSize;
  5046. payloadType = 0;
  5047. payloadSize = 0;
  5048. }
  5049. return result;
  5050. }; // see ANSI/SCTE 128-1 (2013), section 8.1
  5051. var parseUserData = function (sei) {
  5052. // itu_t_t35_contry_code must be 181 (United States) for
  5053. // captions
  5054. if (sei.payload[0] !== 181) {
  5055. return null;
  5056. } // itu_t_t35_provider_code should be 49 (ATSC) for captions
  5057. if ((sei.payload[1] << 8 | sei.payload[2]) !== 49) {
  5058. return null;
  5059. } // the user_identifier should be "GA94" to indicate ATSC1 data
  5060. if (String.fromCharCode(sei.payload[3], sei.payload[4], sei.payload[5], sei.payload[6]) !== 'GA94') {
  5061. return null;
  5062. } // finally, user_data_type_code should be 0x03 for caption data
  5063. if (sei.payload[7] !== 0x03) {
  5064. return null;
  5065. } // return the user_data_type_structure and strip the trailing
  5066. // marker bits
  5067. return sei.payload.subarray(8, sei.payload.length - 1);
  5068. }; // see CEA-708-D, section 4.4
  5069. var parseCaptionPackets = function (pts, userData) {
  5070. var results = [],
  5071. i,
  5072. count,
  5073. offset,
  5074. data; // if this is just filler, return immediately
  5075. if (!(userData[0] & 0x40)) {
  5076. return results;
  5077. } // parse out the cc_data_1 and cc_data_2 fields
  5078. count = userData[0] & 0x1f;
  5079. for (i = 0; i < count; i++) {
  5080. offset = i * 3;
  5081. data = {
  5082. type: userData[offset + 2] & 0x03,
  5083. pts: pts
  5084. }; // capture cc data when cc_valid is 1
  5085. if (userData[offset + 2] & 0x04) {
  5086. data.ccData = userData[offset + 3] << 8 | userData[offset + 4];
  5087. results.push(data);
  5088. }
  5089. }
  5090. return results;
  5091. };
  5092. var discardEmulationPreventionBytes$1 = function (data) {
  5093. var length = data.byteLength,
  5094. emulationPreventionBytesPositions = [],
  5095. i = 1,
  5096. newLength,
  5097. newData; // Find all `Emulation Prevention Bytes`
  5098. while (i < length - 2) {
  5099. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  5100. emulationPreventionBytesPositions.push(i + 2);
  5101. i += 2;
  5102. } else {
  5103. i++;
  5104. }
  5105. } // If no Emulation Prevention Bytes were found just return the original
  5106. // array
  5107. if (emulationPreventionBytesPositions.length === 0) {
  5108. return data;
  5109. } // Create a new array to hold the NAL unit data
  5110. newLength = length - emulationPreventionBytesPositions.length;
  5111. newData = new Uint8Array(newLength);
  5112. var sourceIndex = 0;
  5113. for (i = 0; i < newLength; sourceIndex++, i++) {
  5114. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  5115. // Skip this byte
  5116. sourceIndex++; // Remove this position index
  5117. emulationPreventionBytesPositions.shift();
  5118. }
  5119. newData[i] = data[sourceIndex];
  5120. }
  5121. return newData;
  5122. }; // exports
  5123. var captionPacketParser = {
  5124. parseSei: parseSei,
  5125. parseUserData: parseUserData,
  5126. parseCaptionPackets: parseCaptionPackets,
  5127. discardEmulationPreventionBytes: discardEmulationPreventionBytes$1,
  5128. USER_DATA_REGISTERED_ITU_T_T35: USER_DATA_REGISTERED_ITU_T_T35
  5129. };
  5130. /**
  5131. * mux.js
  5132. *
  5133. * Copyright (c) Brightcove
  5134. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  5135. *
  5136. * Reads in-band caption information from a video elementary
  5137. * stream. Captions must follow the CEA-708 standard for injection
  5138. * into an MPEG-2 transport streams.
  5139. * @see https://en.wikipedia.org/wiki/CEA-708
  5140. * @see https://www.gpo.gov/fdsys/pkg/CFR-2007-title47-vol1/pdf/CFR-2007-title47-vol1-sec15-119.pdf
  5141. */
  5142. // Link To Transport
  5143. // -----------------
  5144. var Stream$7 = stream;
  5145. var cea708Parser = captionPacketParser;
  5146. var CaptionStream$2 = function (options) {
  5147. options = options || {};
  5148. CaptionStream$2.prototype.init.call(this); // parse708captions flag, default to true
  5149. this.parse708captions_ = typeof options.parse708captions === 'boolean' ? options.parse708captions : true;
  5150. this.captionPackets_ = [];
  5151. this.ccStreams_ = [new Cea608Stream(0, 0), // eslint-disable-line no-use-before-define
  5152. new Cea608Stream(0, 1), // eslint-disable-line no-use-before-define
  5153. new Cea608Stream(1, 0), // eslint-disable-line no-use-before-define
  5154. new Cea608Stream(1, 1) // eslint-disable-line no-use-before-define
  5155. ];
  5156. if (this.parse708captions_) {
  5157. this.cc708Stream_ = new Cea708Stream({
  5158. captionServices: options.captionServices
  5159. }); // eslint-disable-line no-use-before-define
  5160. }
  5161. this.reset(); // forward data and done events from CCs to this CaptionStream
  5162. this.ccStreams_.forEach(function (cc) {
  5163. cc.on('data', this.trigger.bind(this, 'data'));
  5164. cc.on('partialdone', this.trigger.bind(this, 'partialdone'));
  5165. cc.on('done', this.trigger.bind(this, 'done'));
  5166. }, this);
  5167. if (this.parse708captions_) {
  5168. this.cc708Stream_.on('data', this.trigger.bind(this, 'data'));
  5169. this.cc708Stream_.on('partialdone', this.trigger.bind(this, 'partialdone'));
  5170. this.cc708Stream_.on('done', this.trigger.bind(this, 'done'));
  5171. }
  5172. };
  5173. CaptionStream$2.prototype = new Stream$7();
  5174. CaptionStream$2.prototype.push = function (event) {
  5175. var sei, userData, newCaptionPackets; // only examine SEI NALs
  5176. if (event.nalUnitType !== 'sei_rbsp') {
  5177. return;
  5178. } // parse the sei
  5179. sei = cea708Parser.parseSei(event.escapedRBSP); // no payload data, skip
  5180. if (!sei.payload) {
  5181. return;
  5182. } // ignore everything but user_data_registered_itu_t_t35
  5183. if (sei.payloadType !== cea708Parser.USER_DATA_REGISTERED_ITU_T_T35) {
  5184. return;
  5185. } // parse out the user data payload
  5186. userData = cea708Parser.parseUserData(sei); // ignore unrecognized userData
  5187. if (!userData) {
  5188. return;
  5189. } // Sometimes, the same segment # will be downloaded twice. To stop the
  5190. // caption data from being processed twice, we track the latest dts we've
  5191. // received and ignore everything with a dts before that. However, since
  5192. // data for a specific dts can be split across packets on either side of
  5193. // a segment boundary, we need to make sure we *don't* ignore the packets
  5194. // from the *next* segment that have dts === this.latestDts_. By constantly
  5195. // tracking the number of packets received with dts === this.latestDts_, we
  5196. // know how many should be ignored once we start receiving duplicates.
  5197. if (event.dts < this.latestDts_) {
  5198. // We've started getting older data, so set the flag.
  5199. this.ignoreNextEqualDts_ = true;
  5200. return;
  5201. } else if (event.dts === this.latestDts_ && this.ignoreNextEqualDts_) {
  5202. this.numSameDts_--;
  5203. if (!this.numSameDts_) {
  5204. // We've received the last duplicate packet, time to start processing again
  5205. this.ignoreNextEqualDts_ = false;
  5206. }
  5207. return;
  5208. } // parse out CC data packets and save them for later
  5209. newCaptionPackets = cea708Parser.parseCaptionPackets(event.pts, userData);
  5210. this.captionPackets_ = this.captionPackets_.concat(newCaptionPackets);
  5211. if (this.latestDts_ !== event.dts) {
  5212. this.numSameDts_ = 0;
  5213. }
  5214. this.numSameDts_++;
  5215. this.latestDts_ = event.dts;
  5216. };
  5217. CaptionStream$2.prototype.flushCCStreams = function (flushType) {
  5218. this.ccStreams_.forEach(function (cc) {
  5219. return flushType === 'flush' ? cc.flush() : cc.partialFlush();
  5220. }, this);
  5221. };
  5222. CaptionStream$2.prototype.flushStream = function (flushType) {
  5223. // make sure we actually parsed captions before proceeding
  5224. if (!this.captionPackets_.length) {
  5225. this.flushCCStreams(flushType);
  5226. return;
  5227. } // In Chrome, the Array#sort function is not stable so add a
  5228. // presortIndex that we can use to ensure we get a stable-sort
  5229. this.captionPackets_.forEach(function (elem, idx) {
  5230. elem.presortIndex = idx;
  5231. }); // sort caption byte-pairs based on their PTS values
  5232. this.captionPackets_.sort(function (a, b) {
  5233. if (a.pts === b.pts) {
  5234. return a.presortIndex - b.presortIndex;
  5235. }
  5236. return a.pts - b.pts;
  5237. });
  5238. this.captionPackets_.forEach(function (packet) {
  5239. if (packet.type < 2) {
  5240. // Dispatch packet to the right Cea608Stream
  5241. this.dispatchCea608Packet(packet);
  5242. } else {
  5243. // Dispatch packet to the Cea708Stream
  5244. this.dispatchCea708Packet(packet);
  5245. }
  5246. }, this);
  5247. this.captionPackets_.length = 0;
  5248. this.flushCCStreams(flushType);
  5249. };
  5250. CaptionStream$2.prototype.flush = function () {
  5251. return this.flushStream('flush');
  5252. }; // Only called if handling partial data
  5253. CaptionStream$2.prototype.partialFlush = function () {
  5254. return this.flushStream('partialFlush');
  5255. };
  5256. CaptionStream$2.prototype.reset = function () {
  5257. this.latestDts_ = null;
  5258. this.ignoreNextEqualDts_ = false;
  5259. this.numSameDts_ = 0;
  5260. this.activeCea608Channel_ = [null, null];
  5261. this.ccStreams_.forEach(function (ccStream) {
  5262. ccStream.reset();
  5263. });
  5264. }; // From the CEA-608 spec:
  5265. /*
  5266. * When XDS sub-packets are interleaved with other services, the end of each sub-packet shall be followed
  5267. * by a control pair to change to a different service. When any of the control codes from 0x10 to 0x1F is
  5268. * used to begin a control code pair, it indicates the return to captioning or Text data. The control code pair
  5269. * and subsequent data should then be processed according to the FCC rules. It may be necessary for the
  5270. * line 21 data encoder to automatically insert a control code pair (i.e. RCL, RU2, RU3, RU4, RDC, or RTD)
  5271. * to switch to captioning or Text.
  5272. */
  5273. // With that in mind, we ignore any data between an XDS control code and a
  5274. // subsequent closed-captioning control code.
  5275. CaptionStream$2.prototype.dispatchCea608Packet = function (packet) {
  5276. // NOTE: packet.type is the CEA608 field
  5277. if (this.setsTextOrXDSActive(packet)) {
  5278. this.activeCea608Channel_[packet.type] = null;
  5279. } else if (this.setsChannel1Active(packet)) {
  5280. this.activeCea608Channel_[packet.type] = 0;
  5281. } else if (this.setsChannel2Active(packet)) {
  5282. this.activeCea608Channel_[packet.type] = 1;
  5283. }
  5284. if (this.activeCea608Channel_[packet.type] === null) {
  5285. // If we haven't received anything to set the active channel, or the
  5286. // packets are Text/XDS data, discard the data; we don't want jumbled
  5287. // captions
  5288. return;
  5289. }
  5290. this.ccStreams_[(packet.type << 1) + this.activeCea608Channel_[packet.type]].push(packet);
  5291. };
  5292. CaptionStream$2.prototype.setsChannel1Active = function (packet) {
  5293. return (packet.ccData & 0x7800) === 0x1000;
  5294. };
  5295. CaptionStream$2.prototype.setsChannel2Active = function (packet) {
  5296. return (packet.ccData & 0x7800) === 0x1800;
  5297. };
  5298. CaptionStream$2.prototype.setsTextOrXDSActive = function (packet) {
  5299. return (packet.ccData & 0x7100) === 0x0100 || (packet.ccData & 0x78fe) === 0x102a || (packet.ccData & 0x78fe) === 0x182a;
  5300. };
  5301. CaptionStream$2.prototype.dispatchCea708Packet = function (packet) {
  5302. if (this.parse708captions_) {
  5303. this.cc708Stream_.push(packet);
  5304. }
  5305. }; // ----------------------
  5306. // Session to Application
  5307. // ----------------------
  5308. // This hash maps special and extended character codes to their
  5309. // proper Unicode equivalent. The first one-byte key is just a
  5310. // non-standard character code. The two-byte keys that follow are
  5311. // the extended CEA708 character codes, along with the preceding
  5312. // 0x10 extended character byte to distinguish these codes from
  5313. // non-extended character codes. Every CEA708 character code that
  5314. // is not in this object maps directly to a standard unicode
  5315. // character code.
  5316. // The transparent space and non-breaking transparent space are
  5317. // technically not fully supported since there is no code to
  5318. // make them transparent, so they have normal non-transparent
  5319. // stand-ins.
  5320. // The special closed caption (CC) character isn't a standard
  5321. // unicode character, so a fairly similar unicode character was
  5322. // chosen in it's place.
  5323. var CHARACTER_TRANSLATION_708 = {
  5324. 0x7f: 0x266a,
  5325. // ♪
  5326. 0x1020: 0x20,
  5327. // Transparent Space
  5328. 0x1021: 0xa0,
  5329. // Nob-breaking Transparent Space
  5330. 0x1025: 0x2026,
  5331. // …
  5332. 0x102a: 0x0160,
  5333. // Š
  5334. 0x102c: 0x0152,
  5335. // Œ
  5336. 0x1030: 0x2588,
  5337. // █
  5338. 0x1031: 0x2018,
  5339. // ‘
  5340. 0x1032: 0x2019,
  5341. // ’
  5342. 0x1033: 0x201c,
  5343. // “
  5344. 0x1034: 0x201d,
  5345. // ”
  5346. 0x1035: 0x2022,
  5347. // •
  5348. 0x1039: 0x2122,
  5349. // ™
  5350. 0x103a: 0x0161,
  5351. // š
  5352. 0x103c: 0x0153,
  5353. // œ
  5354. 0x103d: 0x2120,
  5355. // ℠
  5356. 0x103f: 0x0178,
  5357. // Ÿ
  5358. 0x1076: 0x215b,
  5359. // ⅛
  5360. 0x1077: 0x215c,
  5361. // ⅜
  5362. 0x1078: 0x215d,
  5363. // ⅝
  5364. 0x1079: 0x215e,
  5365. // ⅞
  5366. 0x107a: 0x23d0,
  5367. // ⏐
  5368. 0x107b: 0x23a4,
  5369. // ⎤
  5370. 0x107c: 0x23a3,
  5371. // ⎣
  5372. 0x107d: 0x23af,
  5373. // ⎯
  5374. 0x107e: 0x23a6,
  5375. // ⎦
  5376. 0x107f: 0x23a1,
  5377. // ⎡
  5378. 0x10a0: 0x3138 // ㄸ (CC char)
  5379. };
  5380. var get708CharFromCode = function (code) {
  5381. var newCode = CHARACTER_TRANSLATION_708[code] || code;
  5382. if (code & 0x1000 && code === newCode) {
  5383. // Invalid extended code
  5384. return '';
  5385. }
  5386. return String.fromCharCode(newCode);
  5387. };
  5388. var within708TextBlock = function (b) {
  5389. return 0x20 <= b && b <= 0x7f || 0xa0 <= b && b <= 0xff;
  5390. };
  5391. var Cea708Window = function (windowNum) {
  5392. this.windowNum = windowNum;
  5393. this.reset();
  5394. };
  5395. Cea708Window.prototype.reset = function () {
  5396. this.clearText();
  5397. this.pendingNewLine = false;
  5398. this.winAttr = {};
  5399. this.penAttr = {};
  5400. this.penLoc = {};
  5401. this.penColor = {}; // These default values are arbitrary,
  5402. // defineWindow will usually override them
  5403. this.visible = 0;
  5404. this.rowLock = 0;
  5405. this.columnLock = 0;
  5406. this.priority = 0;
  5407. this.relativePositioning = 0;
  5408. this.anchorVertical = 0;
  5409. this.anchorHorizontal = 0;
  5410. this.anchorPoint = 0;
  5411. this.rowCount = 1;
  5412. this.virtualRowCount = this.rowCount + 1;
  5413. this.columnCount = 41;
  5414. this.windowStyle = 0;
  5415. this.penStyle = 0;
  5416. };
  5417. Cea708Window.prototype.getText = function () {
  5418. return this.rows.join('\n');
  5419. };
  5420. Cea708Window.prototype.clearText = function () {
  5421. this.rows = [''];
  5422. this.rowIdx = 0;
  5423. };
  5424. Cea708Window.prototype.newLine = function (pts) {
  5425. if (this.rows.length >= this.virtualRowCount && typeof this.beforeRowOverflow === 'function') {
  5426. this.beforeRowOverflow(pts);
  5427. }
  5428. if (this.rows.length > 0) {
  5429. this.rows.push('');
  5430. this.rowIdx++;
  5431. } // Show all virtual rows since there's no visible scrolling
  5432. while (this.rows.length > this.virtualRowCount) {
  5433. this.rows.shift();
  5434. this.rowIdx--;
  5435. }
  5436. };
  5437. Cea708Window.prototype.isEmpty = function () {
  5438. if (this.rows.length === 0) {
  5439. return true;
  5440. } else if (this.rows.length === 1) {
  5441. return this.rows[0] === '';
  5442. }
  5443. return false;
  5444. };
  5445. Cea708Window.prototype.addText = function (text) {
  5446. this.rows[this.rowIdx] += text;
  5447. };
  5448. Cea708Window.prototype.backspace = function () {
  5449. if (!this.isEmpty()) {
  5450. var row = this.rows[this.rowIdx];
  5451. this.rows[this.rowIdx] = row.substr(0, row.length - 1);
  5452. }
  5453. };
  5454. var Cea708Service = function (serviceNum, encoding, stream) {
  5455. this.serviceNum = serviceNum;
  5456. this.text = '';
  5457. this.currentWindow = new Cea708Window(-1);
  5458. this.windows = [];
  5459. this.stream = stream; // Try to setup a TextDecoder if an `encoding` value was provided
  5460. if (typeof encoding === 'string') {
  5461. this.createTextDecoder(encoding);
  5462. }
  5463. };
  5464. /**
  5465. * Initialize service windows
  5466. * Must be run before service use
  5467. *
  5468. * @param {Integer} pts PTS value
  5469. * @param {Function} beforeRowOverflow Function to execute before row overflow of a window
  5470. */
  5471. Cea708Service.prototype.init = function (pts, beforeRowOverflow) {
  5472. this.startPts = pts;
  5473. for (var win = 0; win < 8; win++) {
  5474. this.windows[win] = new Cea708Window(win);
  5475. if (typeof beforeRowOverflow === 'function') {
  5476. this.windows[win].beforeRowOverflow = beforeRowOverflow;
  5477. }
  5478. }
  5479. };
  5480. /**
  5481. * Set current window of service to be affected by commands
  5482. *
  5483. * @param {Integer} windowNum Window number
  5484. */
  5485. Cea708Service.prototype.setCurrentWindow = function (windowNum) {
  5486. this.currentWindow = this.windows[windowNum];
  5487. };
  5488. /**
  5489. * Try to create a TextDecoder if it is natively supported
  5490. */
  5491. Cea708Service.prototype.createTextDecoder = function (encoding) {
  5492. if (typeof TextDecoder === 'undefined') {
  5493. this.stream.trigger('log', {
  5494. level: 'warn',
  5495. message: 'The `encoding` option is unsupported without TextDecoder support'
  5496. });
  5497. } else {
  5498. try {
  5499. this.textDecoder_ = new TextDecoder(encoding);
  5500. } catch (error) {
  5501. this.stream.trigger('log', {
  5502. level: 'warn',
  5503. message: 'TextDecoder could not be created with ' + encoding + ' encoding. ' + error
  5504. });
  5505. }
  5506. }
  5507. };
  5508. var Cea708Stream = function (options) {
  5509. options = options || {};
  5510. Cea708Stream.prototype.init.call(this);
  5511. var self = this;
  5512. var captionServices = options.captionServices || {};
  5513. var captionServiceEncodings = {};
  5514. var serviceProps; // Get service encodings from captionServices option block
  5515. Object.keys(captionServices).forEach(serviceName => {
  5516. serviceProps = captionServices[serviceName];
  5517. if (/^SERVICE/.test(serviceName)) {
  5518. captionServiceEncodings[serviceName] = serviceProps.encoding;
  5519. }
  5520. });
  5521. this.serviceEncodings = captionServiceEncodings;
  5522. this.current708Packet = null;
  5523. this.services = {};
  5524. this.push = function (packet) {
  5525. if (packet.type === 3) {
  5526. // 708 packet start
  5527. self.new708Packet();
  5528. self.add708Bytes(packet);
  5529. } else {
  5530. if (self.current708Packet === null) {
  5531. // This should only happen at the start of a file if there's no packet start.
  5532. self.new708Packet();
  5533. }
  5534. self.add708Bytes(packet);
  5535. }
  5536. };
  5537. };
  5538. Cea708Stream.prototype = new Stream$7();
  5539. /**
  5540. * Push current 708 packet, create new 708 packet.
  5541. */
  5542. Cea708Stream.prototype.new708Packet = function () {
  5543. if (this.current708Packet !== null) {
  5544. this.push708Packet();
  5545. }
  5546. this.current708Packet = {
  5547. data: [],
  5548. ptsVals: []
  5549. };
  5550. };
  5551. /**
  5552. * Add pts and both bytes from packet into current 708 packet.
  5553. */
  5554. Cea708Stream.prototype.add708Bytes = function (packet) {
  5555. var data = packet.ccData;
  5556. var byte0 = data >>> 8;
  5557. var byte1 = data & 0xff; // I would just keep a list of packets instead of bytes, but it isn't clear in the spec
  5558. // that service blocks will always line up with byte pairs.
  5559. this.current708Packet.ptsVals.push(packet.pts);
  5560. this.current708Packet.data.push(byte0);
  5561. this.current708Packet.data.push(byte1);
  5562. };
  5563. /**
  5564. * Parse completed 708 packet into service blocks and push each service block.
  5565. */
  5566. Cea708Stream.prototype.push708Packet = function () {
  5567. var packet708 = this.current708Packet;
  5568. var packetData = packet708.data;
  5569. var serviceNum = null;
  5570. var blockSize = null;
  5571. var i = 0;
  5572. var b = packetData[i++];
  5573. packet708.seq = b >> 6;
  5574. packet708.sizeCode = b & 0x3f; // 0b00111111;
  5575. for (; i < packetData.length; i++) {
  5576. b = packetData[i++];
  5577. serviceNum = b >> 5;
  5578. blockSize = b & 0x1f; // 0b00011111
  5579. if (serviceNum === 7 && blockSize > 0) {
  5580. // Extended service num
  5581. b = packetData[i++];
  5582. serviceNum = b;
  5583. }
  5584. this.pushServiceBlock(serviceNum, i, blockSize);
  5585. if (blockSize > 0) {
  5586. i += blockSize - 1;
  5587. }
  5588. }
  5589. };
  5590. /**
  5591. * Parse service block, execute commands, read text.
  5592. *
  5593. * Note: While many of these commands serve important purposes,
  5594. * many others just parse out the parameters or attributes, but
  5595. * nothing is done with them because this is not a full and complete
  5596. * implementation of the entire 708 spec.
  5597. *
  5598. * @param {Integer} serviceNum Service number
  5599. * @param {Integer} start Start index of the 708 packet data
  5600. * @param {Integer} size Block size
  5601. */
  5602. Cea708Stream.prototype.pushServiceBlock = function (serviceNum, start, size) {
  5603. var b;
  5604. var i = start;
  5605. var packetData = this.current708Packet.data;
  5606. var service = this.services[serviceNum];
  5607. if (!service) {
  5608. service = this.initService(serviceNum, i);
  5609. }
  5610. for (; i < start + size && i < packetData.length; i++) {
  5611. b = packetData[i];
  5612. if (within708TextBlock(b)) {
  5613. i = this.handleText(i, service);
  5614. } else if (b === 0x18) {
  5615. i = this.multiByteCharacter(i, service);
  5616. } else if (b === 0x10) {
  5617. i = this.extendedCommands(i, service);
  5618. } else if (0x80 <= b && b <= 0x87) {
  5619. i = this.setCurrentWindow(i, service);
  5620. } else if (0x98 <= b && b <= 0x9f) {
  5621. i = this.defineWindow(i, service);
  5622. } else if (b === 0x88) {
  5623. i = this.clearWindows(i, service);
  5624. } else if (b === 0x8c) {
  5625. i = this.deleteWindows(i, service);
  5626. } else if (b === 0x89) {
  5627. i = this.displayWindows(i, service);
  5628. } else if (b === 0x8a) {
  5629. i = this.hideWindows(i, service);
  5630. } else if (b === 0x8b) {
  5631. i = this.toggleWindows(i, service);
  5632. } else if (b === 0x97) {
  5633. i = this.setWindowAttributes(i, service);
  5634. } else if (b === 0x90) {
  5635. i = this.setPenAttributes(i, service);
  5636. } else if (b === 0x91) {
  5637. i = this.setPenColor(i, service);
  5638. } else if (b === 0x92) {
  5639. i = this.setPenLocation(i, service);
  5640. } else if (b === 0x8f) {
  5641. service = this.reset(i, service);
  5642. } else if (b === 0x08) {
  5643. // BS: Backspace
  5644. service.currentWindow.backspace();
  5645. } else if (b === 0x0c) {
  5646. // FF: Form feed
  5647. service.currentWindow.clearText();
  5648. } else if (b === 0x0d) {
  5649. // CR: Carriage return
  5650. service.currentWindow.pendingNewLine = true;
  5651. } else if (b === 0x0e) {
  5652. // HCR: Horizontal carriage return
  5653. service.currentWindow.clearText();
  5654. } else if (b === 0x8d) {
  5655. // DLY: Delay, nothing to do
  5656. i++;
  5657. } else ;
  5658. }
  5659. };
  5660. /**
  5661. * Execute an extended command
  5662. *
  5663. * @param {Integer} i Current index in the 708 packet
  5664. * @param {Service} service The service object to be affected
  5665. * @return {Integer} New index after parsing
  5666. */
  5667. Cea708Stream.prototype.extendedCommands = function (i, service) {
  5668. var packetData = this.current708Packet.data;
  5669. var b = packetData[++i];
  5670. if (within708TextBlock(b)) {
  5671. i = this.handleText(i, service, {
  5672. isExtended: true
  5673. });
  5674. }
  5675. return i;
  5676. };
  5677. /**
  5678. * Get PTS value of a given byte index
  5679. *
  5680. * @param {Integer} byteIndex Index of the byte
  5681. * @return {Integer} PTS
  5682. */
  5683. Cea708Stream.prototype.getPts = function (byteIndex) {
  5684. // There's 1 pts value per 2 bytes
  5685. return this.current708Packet.ptsVals[Math.floor(byteIndex / 2)];
  5686. };
  5687. /**
  5688. * Initializes a service
  5689. *
  5690. * @param {Integer} serviceNum Service number
  5691. * @return {Service} Initialized service object
  5692. */
  5693. Cea708Stream.prototype.initService = function (serviceNum, i) {
  5694. var serviceName = 'SERVICE' + serviceNum;
  5695. var self = this;
  5696. var serviceName;
  5697. var encoding;
  5698. if (serviceName in this.serviceEncodings) {
  5699. encoding = this.serviceEncodings[serviceName];
  5700. }
  5701. this.services[serviceNum] = new Cea708Service(serviceNum, encoding, self);
  5702. this.services[serviceNum].init(this.getPts(i), function (pts) {
  5703. self.flushDisplayed(pts, self.services[serviceNum]);
  5704. });
  5705. return this.services[serviceNum];
  5706. };
  5707. /**
  5708. * Execute text writing to current window
  5709. *
  5710. * @param {Integer} i Current index in the 708 packet
  5711. * @param {Service} service The service object to be affected
  5712. * @return {Integer} New index after parsing
  5713. */
  5714. Cea708Stream.prototype.handleText = function (i, service, options) {
  5715. var isExtended = options && options.isExtended;
  5716. var isMultiByte = options && options.isMultiByte;
  5717. var packetData = this.current708Packet.data;
  5718. var extended = isExtended ? 0x1000 : 0x0000;
  5719. var currentByte = packetData[i];
  5720. var nextByte = packetData[i + 1];
  5721. var win = service.currentWindow;
  5722. var char;
  5723. var charCodeArray; // Converts an array of bytes to a unicode hex string.
  5724. function toHexString(byteArray) {
  5725. return byteArray.map(byte => {
  5726. return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  5727. }).join('');
  5728. }
  5729. if (isMultiByte) {
  5730. charCodeArray = [currentByte, nextByte];
  5731. i++;
  5732. } else {
  5733. charCodeArray = [currentByte];
  5734. } // Use the TextDecoder if one was created for this service
  5735. if (service.textDecoder_ && !isExtended) {
  5736. char = service.textDecoder_.decode(new Uint8Array(charCodeArray));
  5737. } else {
  5738. // We assume any multi-byte char without a decoder is unicode.
  5739. if (isMultiByte) {
  5740. const unicode = toHexString(charCodeArray); // Takes a unicode hex string and creates a single character.
  5741. char = String.fromCharCode(parseInt(unicode, 16));
  5742. } else {
  5743. char = get708CharFromCode(extended | currentByte);
  5744. }
  5745. }
  5746. if (win.pendingNewLine && !win.isEmpty()) {
  5747. win.newLine(this.getPts(i));
  5748. }
  5749. win.pendingNewLine = false;
  5750. win.addText(char);
  5751. return i;
  5752. };
  5753. /**
  5754. * Handle decoding of multibyte character
  5755. *
  5756. * @param {Integer} i Current index in the 708 packet
  5757. * @param {Service} service The service object to be affected
  5758. * @return {Integer} New index after parsing
  5759. */
  5760. Cea708Stream.prototype.multiByteCharacter = function (i, service) {
  5761. var packetData = this.current708Packet.data;
  5762. var firstByte = packetData[i + 1];
  5763. var secondByte = packetData[i + 2];
  5764. if (within708TextBlock(firstByte) && within708TextBlock(secondByte)) {
  5765. i = this.handleText(++i, service, {
  5766. isMultiByte: true
  5767. });
  5768. }
  5769. return i;
  5770. };
  5771. /**
  5772. * Parse and execute the CW# command.
  5773. *
  5774. * Set the current window.
  5775. *
  5776. * @param {Integer} i Current index in the 708 packet
  5777. * @param {Service} service The service object to be affected
  5778. * @return {Integer} New index after parsing
  5779. */
  5780. Cea708Stream.prototype.setCurrentWindow = function (i, service) {
  5781. var packetData = this.current708Packet.data;
  5782. var b = packetData[i];
  5783. var windowNum = b & 0x07;
  5784. service.setCurrentWindow(windowNum);
  5785. return i;
  5786. };
  5787. /**
  5788. * Parse and execute the DF# command.
  5789. *
  5790. * Define a window and set it as the current window.
  5791. *
  5792. * @param {Integer} i Current index in the 708 packet
  5793. * @param {Service} service The service object to be affected
  5794. * @return {Integer} New index after parsing
  5795. */
  5796. Cea708Stream.prototype.defineWindow = function (i, service) {
  5797. var packetData = this.current708Packet.data;
  5798. var b = packetData[i];
  5799. var windowNum = b & 0x07;
  5800. service.setCurrentWindow(windowNum);
  5801. var win = service.currentWindow;
  5802. b = packetData[++i];
  5803. win.visible = (b & 0x20) >> 5; // v
  5804. win.rowLock = (b & 0x10) >> 4; // rl
  5805. win.columnLock = (b & 0x08) >> 3; // cl
  5806. win.priority = b & 0x07; // p
  5807. b = packetData[++i];
  5808. win.relativePositioning = (b & 0x80) >> 7; // rp
  5809. win.anchorVertical = b & 0x7f; // av
  5810. b = packetData[++i];
  5811. win.anchorHorizontal = b; // ah
  5812. b = packetData[++i];
  5813. win.anchorPoint = (b & 0xf0) >> 4; // ap
  5814. win.rowCount = b & 0x0f; // rc
  5815. b = packetData[++i];
  5816. win.columnCount = b & 0x3f; // cc
  5817. b = packetData[++i];
  5818. win.windowStyle = (b & 0x38) >> 3; // ws
  5819. win.penStyle = b & 0x07; // ps
  5820. // The spec says there are (rowCount+1) "virtual rows"
  5821. win.virtualRowCount = win.rowCount + 1;
  5822. return i;
  5823. };
  5824. /**
  5825. * Parse and execute the SWA command.
  5826. *
  5827. * Set attributes of the current window.
  5828. *
  5829. * @param {Integer} i Current index in the 708 packet
  5830. * @param {Service} service The service object to be affected
  5831. * @return {Integer} New index after parsing
  5832. */
  5833. Cea708Stream.prototype.setWindowAttributes = function (i, service) {
  5834. var packetData = this.current708Packet.data;
  5835. var b = packetData[i];
  5836. var winAttr = service.currentWindow.winAttr;
  5837. b = packetData[++i];
  5838. winAttr.fillOpacity = (b & 0xc0) >> 6; // fo
  5839. winAttr.fillRed = (b & 0x30) >> 4; // fr
  5840. winAttr.fillGreen = (b & 0x0c) >> 2; // fg
  5841. winAttr.fillBlue = b & 0x03; // fb
  5842. b = packetData[++i];
  5843. winAttr.borderType = (b & 0xc0) >> 6; // bt
  5844. winAttr.borderRed = (b & 0x30) >> 4; // br
  5845. winAttr.borderGreen = (b & 0x0c) >> 2; // bg
  5846. winAttr.borderBlue = b & 0x03; // bb
  5847. b = packetData[++i];
  5848. winAttr.borderType += (b & 0x80) >> 5; // bt
  5849. winAttr.wordWrap = (b & 0x40) >> 6; // ww
  5850. winAttr.printDirection = (b & 0x30) >> 4; // pd
  5851. winAttr.scrollDirection = (b & 0x0c) >> 2; // sd
  5852. winAttr.justify = b & 0x03; // j
  5853. b = packetData[++i];
  5854. winAttr.effectSpeed = (b & 0xf0) >> 4; // es
  5855. winAttr.effectDirection = (b & 0x0c) >> 2; // ed
  5856. winAttr.displayEffect = b & 0x03; // de
  5857. return i;
  5858. };
  5859. /**
  5860. * Gather text from all displayed windows and push a caption to output.
  5861. *
  5862. * @param {Integer} i Current index in the 708 packet
  5863. * @param {Service} service The service object to be affected
  5864. */
  5865. Cea708Stream.prototype.flushDisplayed = function (pts, service) {
  5866. var displayedText = []; // TODO: Positioning not supported, displaying multiple windows will not necessarily
  5867. // display text in the correct order, but sample files so far have not shown any issue.
  5868. for (var winId = 0; winId < 8; winId++) {
  5869. if (service.windows[winId].visible && !service.windows[winId].isEmpty()) {
  5870. displayedText.push(service.windows[winId].getText());
  5871. }
  5872. }
  5873. service.endPts = pts;
  5874. service.text = displayedText.join('\n\n');
  5875. this.pushCaption(service);
  5876. service.startPts = pts;
  5877. };
  5878. /**
  5879. * Push a caption to output if the caption contains text.
  5880. *
  5881. * @param {Service} service The service object to be affected
  5882. */
  5883. Cea708Stream.prototype.pushCaption = function (service) {
  5884. if (service.text !== '') {
  5885. this.trigger('data', {
  5886. startPts: service.startPts,
  5887. endPts: service.endPts,
  5888. text: service.text,
  5889. stream: 'cc708_' + service.serviceNum
  5890. });
  5891. service.text = '';
  5892. service.startPts = service.endPts;
  5893. }
  5894. };
  5895. /**
  5896. * Parse and execute the DSW command.
  5897. *
  5898. * Set visible property of windows based on the parsed bitmask.
  5899. *
  5900. * @param {Integer} i Current index in the 708 packet
  5901. * @param {Service} service The service object to be affected
  5902. * @return {Integer} New index after parsing
  5903. */
  5904. Cea708Stream.prototype.displayWindows = function (i, service) {
  5905. var packetData = this.current708Packet.data;
  5906. var b = packetData[++i];
  5907. var pts = this.getPts(i);
  5908. this.flushDisplayed(pts, service);
  5909. for (var winId = 0; winId < 8; winId++) {
  5910. if (b & 0x01 << winId) {
  5911. service.windows[winId].visible = 1;
  5912. }
  5913. }
  5914. return i;
  5915. };
  5916. /**
  5917. * Parse and execute the HDW command.
  5918. *
  5919. * Set visible property of windows based on the parsed bitmask.
  5920. *
  5921. * @param {Integer} i Current index in the 708 packet
  5922. * @param {Service} service The service object to be affected
  5923. * @return {Integer} New index after parsing
  5924. */
  5925. Cea708Stream.prototype.hideWindows = function (i, service) {
  5926. var packetData = this.current708Packet.data;
  5927. var b = packetData[++i];
  5928. var pts = this.getPts(i);
  5929. this.flushDisplayed(pts, service);
  5930. for (var winId = 0; winId < 8; winId++) {
  5931. if (b & 0x01 << winId) {
  5932. service.windows[winId].visible = 0;
  5933. }
  5934. }
  5935. return i;
  5936. };
  5937. /**
  5938. * Parse and execute the TGW command.
  5939. *
  5940. * Set visible property of windows based on the parsed bitmask.
  5941. *
  5942. * @param {Integer} i Current index in the 708 packet
  5943. * @param {Service} service The service object to be affected
  5944. * @return {Integer} New index after parsing
  5945. */
  5946. Cea708Stream.prototype.toggleWindows = function (i, service) {
  5947. var packetData = this.current708Packet.data;
  5948. var b = packetData[++i];
  5949. var pts = this.getPts(i);
  5950. this.flushDisplayed(pts, service);
  5951. for (var winId = 0; winId < 8; winId++) {
  5952. if (b & 0x01 << winId) {
  5953. service.windows[winId].visible ^= 1;
  5954. }
  5955. }
  5956. return i;
  5957. };
  5958. /**
  5959. * Parse and execute the CLW command.
  5960. *
  5961. * Clear text of windows based on the parsed bitmask.
  5962. *
  5963. * @param {Integer} i Current index in the 708 packet
  5964. * @param {Service} service The service object to be affected
  5965. * @return {Integer} New index after parsing
  5966. */
  5967. Cea708Stream.prototype.clearWindows = function (i, service) {
  5968. var packetData = this.current708Packet.data;
  5969. var b = packetData[++i];
  5970. var pts = this.getPts(i);
  5971. this.flushDisplayed(pts, service);
  5972. for (var winId = 0; winId < 8; winId++) {
  5973. if (b & 0x01 << winId) {
  5974. service.windows[winId].clearText();
  5975. }
  5976. }
  5977. return i;
  5978. };
  5979. /**
  5980. * Parse and execute the DLW command.
  5981. *
  5982. * Re-initialize windows based on the parsed bitmask.
  5983. *
  5984. * @param {Integer} i Current index in the 708 packet
  5985. * @param {Service} service The service object to be affected
  5986. * @return {Integer} New index after parsing
  5987. */
  5988. Cea708Stream.prototype.deleteWindows = function (i, service) {
  5989. var packetData = this.current708Packet.data;
  5990. var b = packetData[++i];
  5991. var pts = this.getPts(i);
  5992. this.flushDisplayed(pts, service);
  5993. for (var winId = 0; winId < 8; winId++) {
  5994. if (b & 0x01 << winId) {
  5995. service.windows[winId].reset();
  5996. }
  5997. }
  5998. return i;
  5999. };
  6000. /**
  6001. * Parse and execute the SPA command.
  6002. *
  6003. * Set pen attributes of the current window.
  6004. *
  6005. * @param {Integer} i Current index in the 708 packet
  6006. * @param {Service} service The service object to be affected
  6007. * @return {Integer} New index after parsing
  6008. */
  6009. Cea708Stream.prototype.setPenAttributes = function (i, service) {
  6010. var packetData = this.current708Packet.data;
  6011. var b = packetData[i];
  6012. var penAttr = service.currentWindow.penAttr;
  6013. b = packetData[++i];
  6014. penAttr.textTag = (b & 0xf0) >> 4; // tt
  6015. penAttr.offset = (b & 0x0c) >> 2; // o
  6016. penAttr.penSize = b & 0x03; // s
  6017. b = packetData[++i];
  6018. penAttr.italics = (b & 0x80) >> 7; // i
  6019. penAttr.underline = (b & 0x40) >> 6; // u
  6020. penAttr.edgeType = (b & 0x38) >> 3; // et
  6021. penAttr.fontStyle = b & 0x07; // fs
  6022. return i;
  6023. };
  6024. /**
  6025. * Parse and execute the SPC command.
  6026. *
  6027. * Set pen color of the current window.
  6028. *
  6029. * @param {Integer} i Current index in the 708 packet
  6030. * @param {Service} service The service object to be affected
  6031. * @return {Integer} New index after parsing
  6032. */
  6033. Cea708Stream.prototype.setPenColor = function (i, service) {
  6034. var packetData = this.current708Packet.data;
  6035. var b = packetData[i];
  6036. var penColor = service.currentWindow.penColor;
  6037. b = packetData[++i];
  6038. penColor.fgOpacity = (b & 0xc0) >> 6; // fo
  6039. penColor.fgRed = (b & 0x30) >> 4; // fr
  6040. penColor.fgGreen = (b & 0x0c) >> 2; // fg
  6041. penColor.fgBlue = b & 0x03; // fb
  6042. b = packetData[++i];
  6043. penColor.bgOpacity = (b & 0xc0) >> 6; // bo
  6044. penColor.bgRed = (b & 0x30) >> 4; // br
  6045. penColor.bgGreen = (b & 0x0c) >> 2; // bg
  6046. penColor.bgBlue = b & 0x03; // bb
  6047. b = packetData[++i];
  6048. penColor.edgeRed = (b & 0x30) >> 4; // er
  6049. penColor.edgeGreen = (b & 0x0c) >> 2; // eg
  6050. penColor.edgeBlue = b & 0x03; // eb
  6051. return i;
  6052. };
  6053. /**
  6054. * Parse and execute the SPL command.
  6055. *
  6056. * Set pen location of the current window.
  6057. *
  6058. * @param {Integer} i Current index in the 708 packet
  6059. * @param {Service} service The service object to be affected
  6060. * @return {Integer} New index after parsing
  6061. */
  6062. Cea708Stream.prototype.setPenLocation = function (i, service) {
  6063. var packetData = this.current708Packet.data;
  6064. var b = packetData[i];
  6065. var penLoc = service.currentWindow.penLoc; // Positioning isn't really supported at the moment, so this essentially just inserts a linebreak
  6066. service.currentWindow.pendingNewLine = true;
  6067. b = packetData[++i];
  6068. penLoc.row = b & 0x0f; // r
  6069. b = packetData[++i];
  6070. penLoc.column = b & 0x3f; // c
  6071. return i;
  6072. };
  6073. /**
  6074. * Execute the RST command.
  6075. *
  6076. * Reset service to a clean slate. Re-initialize.
  6077. *
  6078. * @param {Integer} i Current index in the 708 packet
  6079. * @param {Service} service The service object to be affected
  6080. * @return {Service} Re-initialized service
  6081. */
  6082. Cea708Stream.prototype.reset = function (i, service) {
  6083. var pts = this.getPts(i);
  6084. this.flushDisplayed(pts, service);
  6085. return this.initService(service.serviceNum, i);
  6086. }; // This hash maps non-ASCII, special, and extended character codes to their
  6087. // proper Unicode equivalent. The first keys that are only a single byte
  6088. // are the non-standard ASCII characters, which simply map the CEA608 byte
  6089. // to the standard ASCII/Unicode. The two-byte keys that follow are the CEA608
  6090. // character codes, but have their MSB bitmasked with 0x03 so that a lookup
  6091. // can be performed regardless of the field and data channel on which the
  6092. // character code was received.
  6093. var CHARACTER_TRANSLATION = {
  6094. 0x2a: 0xe1,
  6095. // á
  6096. 0x5c: 0xe9,
  6097. // é
  6098. 0x5e: 0xed,
  6099. // í
  6100. 0x5f: 0xf3,
  6101. // ó
  6102. 0x60: 0xfa,
  6103. // ú
  6104. 0x7b: 0xe7,
  6105. // ç
  6106. 0x7c: 0xf7,
  6107. // ÷
  6108. 0x7d: 0xd1,
  6109. // Ñ
  6110. 0x7e: 0xf1,
  6111. // ñ
  6112. 0x7f: 0x2588,
  6113. // █
  6114. 0x0130: 0xae,
  6115. // ®
  6116. 0x0131: 0xb0,
  6117. // °
  6118. 0x0132: 0xbd,
  6119. // ½
  6120. 0x0133: 0xbf,
  6121. // ¿
  6122. 0x0134: 0x2122,
  6123. // ™
  6124. 0x0135: 0xa2,
  6125. // ¢
  6126. 0x0136: 0xa3,
  6127. // £
  6128. 0x0137: 0x266a,
  6129. // ♪
  6130. 0x0138: 0xe0,
  6131. // à
  6132. 0x0139: 0xa0,
  6133. //
  6134. 0x013a: 0xe8,
  6135. // è
  6136. 0x013b: 0xe2,
  6137. // â
  6138. 0x013c: 0xea,
  6139. // ê
  6140. 0x013d: 0xee,
  6141. // î
  6142. 0x013e: 0xf4,
  6143. // ô
  6144. 0x013f: 0xfb,
  6145. // û
  6146. 0x0220: 0xc1,
  6147. // Á
  6148. 0x0221: 0xc9,
  6149. // É
  6150. 0x0222: 0xd3,
  6151. // Ó
  6152. 0x0223: 0xda,
  6153. // Ú
  6154. 0x0224: 0xdc,
  6155. // Ü
  6156. 0x0225: 0xfc,
  6157. // ü
  6158. 0x0226: 0x2018,
  6159. // ‘
  6160. 0x0227: 0xa1,
  6161. // ¡
  6162. 0x0228: 0x2a,
  6163. // *
  6164. 0x0229: 0x27,
  6165. // '
  6166. 0x022a: 0x2014,
  6167. // —
  6168. 0x022b: 0xa9,
  6169. // ©
  6170. 0x022c: 0x2120,
  6171. // ℠
  6172. 0x022d: 0x2022,
  6173. // •
  6174. 0x022e: 0x201c,
  6175. // “
  6176. 0x022f: 0x201d,
  6177. // ”
  6178. 0x0230: 0xc0,
  6179. // À
  6180. 0x0231: 0xc2,
  6181. // Â
  6182. 0x0232: 0xc7,
  6183. // Ç
  6184. 0x0233: 0xc8,
  6185. // È
  6186. 0x0234: 0xca,
  6187. // Ê
  6188. 0x0235: 0xcb,
  6189. // Ë
  6190. 0x0236: 0xeb,
  6191. // ë
  6192. 0x0237: 0xce,
  6193. // Î
  6194. 0x0238: 0xcf,
  6195. // Ï
  6196. 0x0239: 0xef,
  6197. // ï
  6198. 0x023a: 0xd4,
  6199. // Ô
  6200. 0x023b: 0xd9,
  6201. // Ù
  6202. 0x023c: 0xf9,
  6203. // ù
  6204. 0x023d: 0xdb,
  6205. // Û
  6206. 0x023e: 0xab,
  6207. // «
  6208. 0x023f: 0xbb,
  6209. // »
  6210. 0x0320: 0xc3,
  6211. // Ã
  6212. 0x0321: 0xe3,
  6213. // ã
  6214. 0x0322: 0xcd,
  6215. // Í
  6216. 0x0323: 0xcc,
  6217. // Ì
  6218. 0x0324: 0xec,
  6219. // ì
  6220. 0x0325: 0xd2,
  6221. // Ò
  6222. 0x0326: 0xf2,
  6223. // ò
  6224. 0x0327: 0xd5,
  6225. // Õ
  6226. 0x0328: 0xf5,
  6227. // õ
  6228. 0x0329: 0x7b,
  6229. // {
  6230. 0x032a: 0x7d,
  6231. // }
  6232. 0x032b: 0x5c,
  6233. // \
  6234. 0x032c: 0x5e,
  6235. // ^
  6236. 0x032d: 0x5f,
  6237. // _
  6238. 0x032e: 0x7c,
  6239. // |
  6240. 0x032f: 0x7e,
  6241. // ~
  6242. 0x0330: 0xc4,
  6243. // Ä
  6244. 0x0331: 0xe4,
  6245. // ä
  6246. 0x0332: 0xd6,
  6247. // Ö
  6248. 0x0333: 0xf6,
  6249. // ö
  6250. 0x0334: 0xdf,
  6251. // ß
  6252. 0x0335: 0xa5,
  6253. // ¥
  6254. 0x0336: 0xa4,
  6255. // ¤
  6256. 0x0337: 0x2502,
  6257. // │
  6258. 0x0338: 0xc5,
  6259. // Å
  6260. 0x0339: 0xe5,
  6261. // å
  6262. 0x033a: 0xd8,
  6263. // Ø
  6264. 0x033b: 0xf8,
  6265. // ø
  6266. 0x033c: 0x250c,
  6267. // ┌
  6268. 0x033d: 0x2510,
  6269. // ┐
  6270. 0x033e: 0x2514,
  6271. // └
  6272. 0x033f: 0x2518 // ┘
  6273. };
  6274. var getCharFromCode = function (code) {
  6275. if (code === null) {
  6276. return '';
  6277. }
  6278. code = CHARACTER_TRANSLATION[code] || code;
  6279. return String.fromCharCode(code);
  6280. }; // the index of the last row in a CEA-608 display buffer
  6281. var BOTTOM_ROW = 14; // This array is used for mapping PACs -> row #, since there's no way of
  6282. // getting it through bit logic.
  6283. 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
  6284. // cells. The "bottom" row is the last element in the outer array.
  6285. // We keep track of positioning information as we go by storing the
  6286. // number of indentations and the tab offset in this buffer.
  6287. var createDisplayBuffer = function () {
  6288. var result = [],
  6289. i = BOTTOM_ROW + 1;
  6290. while (i--) {
  6291. result.push({
  6292. text: '',
  6293. indent: 0,
  6294. offset: 0
  6295. });
  6296. }
  6297. return result;
  6298. };
  6299. var Cea608Stream = function (field, dataChannel) {
  6300. Cea608Stream.prototype.init.call(this);
  6301. this.field_ = field || 0;
  6302. this.dataChannel_ = dataChannel || 0;
  6303. this.name_ = 'CC' + ((this.field_ << 1 | this.dataChannel_) + 1);
  6304. this.setConstants();
  6305. this.reset();
  6306. this.push = function (packet) {
  6307. var data, swap, char0, char1, text; // remove the parity bits
  6308. data = packet.ccData & 0x7f7f; // ignore duplicate control codes; the spec demands they're sent twice
  6309. if (data === this.lastControlCode_) {
  6310. this.lastControlCode_ = null;
  6311. return;
  6312. } // Store control codes
  6313. if ((data & 0xf000) === 0x1000) {
  6314. this.lastControlCode_ = data;
  6315. } else if (data !== this.PADDING_) {
  6316. this.lastControlCode_ = null;
  6317. }
  6318. char0 = data >>> 8;
  6319. char1 = data & 0xff;
  6320. if (data === this.PADDING_) {
  6321. return;
  6322. } else if (data === this.RESUME_CAPTION_LOADING_) {
  6323. this.mode_ = 'popOn';
  6324. } else if (data === this.END_OF_CAPTION_) {
  6325. // If an EOC is received while in paint-on mode, the displayed caption
  6326. // text should be swapped to non-displayed memory as if it was a pop-on
  6327. // caption. Because of that, we should explicitly switch back to pop-on
  6328. // mode
  6329. this.mode_ = 'popOn';
  6330. this.clearFormatting(packet.pts); // if a caption was being displayed, it's gone now
  6331. this.flushDisplayed(packet.pts); // flip memory
  6332. swap = this.displayed_;
  6333. this.displayed_ = this.nonDisplayed_;
  6334. this.nonDisplayed_ = swap; // start measuring the time to display the caption
  6335. this.startPts_ = packet.pts;
  6336. } else if (data === this.ROLL_UP_2_ROWS_) {
  6337. this.rollUpRows_ = 2;
  6338. this.setRollUp(packet.pts);
  6339. } else if (data === this.ROLL_UP_3_ROWS_) {
  6340. this.rollUpRows_ = 3;
  6341. this.setRollUp(packet.pts);
  6342. } else if (data === this.ROLL_UP_4_ROWS_) {
  6343. this.rollUpRows_ = 4;
  6344. this.setRollUp(packet.pts);
  6345. } else if (data === this.CARRIAGE_RETURN_) {
  6346. this.clearFormatting(packet.pts);
  6347. this.flushDisplayed(packet.pts);
  6348. this.shiftRowsUp_();
  6349. this.startPts_ = packet.pts;
  6350. } else if (data === this.BACKSPACE_) {
  6351. if (this.mode_ === 'popOn') {
  6352. this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
  6353. } else {
  6354. this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
  6355. }
  6356. } else if (data === this.ERASE_DISPLAYED_MEMORY_) {
  6357. this.flushDisplayed(packet.pts);
  6358. this.displayed_ = createDisplayBuffer();
  6359. } else if (data === this.ERASE_NON_DISPLAYED_MEMORY_) {
  6360. this.nonDisplayed_ = createDisplayBuffer();
  6361. } else if (data === this.RESUME_DIRECT_CAPTIONING_) {
  6362. if (this.mode_ !== 'paintOn') {
  6363. // NOTE: This should be removed when proper caption positioning is
  6364. // implemented
  6365. this.flushDisplayed(packet.pts);
  6366. this.displayed_ = createDisplayBuffer();
  6367. }
  6368. this.mode_ = 'paintOn';
  6369. this.startPts_ = packet.pts; // Append special characters to caption text
  6370. } else if (this.isSpecialCharacter(char0, char1)) {
  6371. // Bitmask char0 so that we can apply character transformations
  6372. // regardless of field and data channel.
  6373. // Then byte-shift to the left and OR with char1 so we can pass the
  6374. // entire character code to `getCharFromCode`.
  6375. char0 = (char0 & 0x03) << 8;
  6376. text = getCharFromCode(char0 | char1);
  6377. this[this.mode_](packet.pts, text);
  6378. this.column_++; // Append extended characters to caption text
  6379. } else if (this.isExtCharacter(char0, char1)) {
  6380. // Extended characters always follow their "non-extended" equivalents.
  6381. // IE if a "è" is desired, you'll always receive "eè"; non-compliant
  6382. // decoders are supposed to drop the "è", while compliant decoders
  6383. // backspace the "e" and insert "è".
  6384. // Delete the previous character
  6385. if (this.mode_ === 'popOn') {
  6386. this.nonDisplayed_[this.row_].text = this.nonDisplayed_[this.row_].text.slice(0, -1);
  6387. } else {
  6388. this.displayed_[this.row_].text = this.displayed_[this.row_].text.slice(0, -1);
  6389. } // Bitmask char0 so that we can apply character transformations
  6390. // regardless of field and data channel.
  6391. // Then byte-shift to the left and OR with char1 so we can pass the
  6392. // entire character code to `getCharFromCode`.
  6393. char0 = (char0 & 0x03) << 8;
  6394. text = getCharFromCode(char0 | char1);
  6395. this[this.mode_](packet.pts, text);
  6396. this.column_++; // Process mid-row codes
  6397. } else if (this.isMidRowCode(char0, char1)) {
  6398. // Attributes are not additive, so clear all formatting
  6399. this.clearFormatting(packet.pts); // According to the standard, mid-row codes
  6400. // should be replaced with spaces, so add one now
  6401. this[this.mode_](packet.pts, ' ');
  6402. this.column_++;
  6403. if ((char1 & 0xe) === 0xe) {
  6404. this.addFormatting(packet.pts, ['i']);
  6405. }
  6406. if ((char1 & 0x1) === 0x1) {
  6407. this.addFormatting(packet.pts, ['u']);
  6408. } // Detect offset control codes and adjust cursor
  6409. } else if (this.isOffsetControlCode(char0, char1)) {
  6410. // Cursor position is set by indent PAC (see below) in 4-column
  6411. // increments, with an additional offset code of 1-3 to reach any
  6412. // of the 32 columns specified by CEA-608. So all we need to do
  6413. // here is increment the column cursor by the given offset.
  6414. const offset = char1 & 0x03; // For an offest value 1-3, set the offset for that caption
  6415. // in the non-displayed array.
  6416. this.nonDisplayed_[this.row_].offset = offset;
  6417. this.column_ += offset; // Detect PACs (Preamble Address Codes)
  6418. } else if (this.isPAC(char0, char1)) {
  6419. // There's no logic for PAC -> row mapping, so we have to just
  6420. // find the row code in an array and use its index :(
  6421. var row = ROWS.indexOf(data & 0x1f20); // Configure the caption window if we're in roll-up mode
  6422. if (this.mode_ === 'rollUp') {
  6423. // This implies that the base row is incorrectly set.
  6424. // As per the recommendation in CEA-608(Base Row Implementation), defer to the number
  6425. // of roll-up rows set.
  6426. if (row - this.rollUpRows_ + 1 < 0) {
  6427. row = this.rollUpRows_ - 1;
  6428. }
  6429. this.setRollUp(packet.pts, row);
  6430. }
  6431. if (row !== this.row_) {
  6432. // formatting is only persistent for current row
  6433. this.clearFormatting(packet.pts);
  6434. this.row_ = row;
  6435. } // All PACs can apply underline, so detect and apply
  6436. // (All odd-numbered second bytes set underline)
  6437. if (char1 & 0x1 && this.formatting_.indexOf('u') === -1) {
  6438. this.addFormatting(packet.pts, ['u']);
  6439. }
  6440. if ((data & 0x10) === 0x10) {
  6441. // We've got an indent level code. Each successive even number
  6442. // increments the column cursor by 4, so we can get the desired
  6443. // column position by bit-shifting to the right (to get n/2)
  6444. // and multiplying by 4.
  6445. const indentations = (data & 0xe) >> 1;
  6446. this.column_ = indentations * 4; // add to the number of indentations for positioning
  6447. this.nonDisplayed_[this.row_].indent += indentations;
  6448. }
  6449. if (this.isColorPAC(char1)) {
  6450. // it's a color code, though we only support white, which
  6451. // can be either normal or italicized. white italics can be
  6452. // either 0x4e or 0x6e depending on the row, so we just
  6453. // bitwise-and with 0xe to see if italics should be turned on
  6454. if ((char1 & 0xe) === 0xe) {
  6455. this.addFormatting(packet.pts, ['i']);
  6456. }
  6457. } // We have a normal character in char0, and possibly one in char1
  6458. } else if (this.isNormalChar(char0)) {
  6459. if (char1 === 0x00) {
  6460. char1 = null;
  6461. }
  6462. text = getCharFromCode(char0);
  6463. text += getCharFromCode(char1);
  6464. this[this.mode_](packet.pts, text);
  6465. this.column_ += text.length;
  6466. } // finish data processing
  6467. };
  6468. };
  6469. Cea608Stream.prototype = new Stream$7(); // Trigger a cue point that captures the current state of the
  6470. // display buffer
  6471. Cea608Stream.prototype.flushDisplayed = function (pts) {
  6472. const logWarning = index => {
  6473. this.trigger('log', {
  6474. level: 'warn',
  6475. message: 'Skipping a malformed 608 caption at index ' + index + '.'
  6476. });
  6477. };
  6478. const content = [];
  6479. this.displayed_.forEach((row, i) => {
  6480. if (row && row.text && row.text.length) {
  6481. try {
  6482. // remove spaces from the start and end of the string
  6483. row.text = row.text.trim();
  6484. } catch (e) {
  6485. // Ordinarily, this shouldn't happen. However, caption
  6486. // parsing errors should not throw exceptions and
  6487. // break playback.
  6488. logWarning(i);
  6489. } // See the below link for more details on the following fields:
  6490. // https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/608toVTT.html#positioning-in-cea-608
  6491. if (row.text.length) {
  6492. content.push({
  6493. // The text to be displayed in the caption from this specific row, with whitespace removed.
  6494. text: row.text,
  6495. // Value between 1 and 15 representing the PAC row used to calculate line height.
  6496. line: i + 1,
  6497. // A number representing the indent position by percentage (CEA-608 PAC indent code).
  6498. // The value will be a number between 10 and 80. Offset is used to add an aditional
  6499. // value to the position if necessary.
  6500. position: 10 + Math.min(70, row.indent * 10) + row.offset * 2.5
  6501. });
  6502. }
  6503. } else if (row === undefined || row === null) {
  6504. logWarning(i);
  6505. }
  6506. });
  6507. if (content.length) {
  6508. this.trigger('data', {
  6509. startPts: this.startPts_,
  6510. endPts: pts,
  6511. content,
  6512. stream: this.name_
  6513. });
  6514. }
  6515. };
  6516. /**
  6517. * Zero out the data, used for startup and on seek
  6518. */
  6519. Cea608Stream.prototype.reset = function () {
  6520. this.mode_ = 'popOn'; // When in roll-up mode, the index of the last row that will
  6521. // actually display captions. If a caption is shifted to a row
  6522. // with a lower index than this, it is cleared from the display
  6523. // buffer
  6524. this.topRow_ = 0;
  6525. this.startPts_ = 0;
  6526. this.displayed_ = createDisplayBuffer();
  6527. this.nonDisplayed_ = createDisplayBuffer();
  6528. this.lastControlCode_ = null; // Track row and column for proper line-breaking and spacing
  6529. this.column_ = 0;
  6530. this.row_ = BOTTOM_ROW;
  6531. this.rollUpRows_ = 2; // This variable holds currently-applied formatting
  6532. this.formatting_ = [];
  6533. };
  6534. /**
  6535. * Sets up control code and related constants for this instance
  6536. */
  6537. Cea608Stream.prototype.setConstants = function () {
  6538. // The following attributes have these uses:
  6539. // ext_ : char0 for mid-row codes, and the base for extended
  6540. // chars (ext_+0, ext_+1, and ext_+2 are char0s for
  6541. // extended codes)
  6542. // control_: char0 for control codes, except byte-shifted to the
  6543. // left so that we can do this.control_ | CONTROL_CODE
  6544. // offset_: char0 for tab offset codes
  6545. //
  6546. // It's also worth noting that control codes, and _only_ control codes,
  6547. // differ between field 1 and field2. Field 2 control codes are always
  6548. // their field 1 value plus 1. That's why there's the "| field" on the
  6549. // control value.
  6550. if (this.dataChannel_ === 0) {
  6551. this.BASE_ = 0x10;
  6552. this.EXT_ = 0x11;
  6553. this.CONTROL_ = (0x14 | this.field_) << 8;
  6554. this.OFFSET_ = 0x17;
  6555. } else if (this.dataChannel_ === 1) {
  6556. this.BASE_ = 0x18;
  6557. this.EXT_ = 0x19;
  6558. this.CONTROL_ = (0x1c | this.field_) << 8;
  6559. this.OFFSET_ = 0x1f;
  6560. } // Constants for the LSByte command codes recognized by Cea608Stream. This
  6561. // list is not exhaustive. For a more comprehensive listing and semantics see
  6562. // http://www.gpo.gov/fdsys/pkg/CFR-2010-title47-vol1/pdf/CFR-2010-title47-vol1-sec15-119.pdf
  6563. // Padding
  6564. this.PADDING_ = 0x0000; // Pop-on Mode
  6565. this.RESUME_CAPTION_LOADING_ = this.CONTROL_ | 0x20;
  6566. this.END_OF_CAPTION_ = this.CONTROL_ | 0x2f; // Roll-up Mode
  6567. this.ROLL_UP_2_ROWS_ = this.CONTROL_ | 0x25;
  6568. this.ROLL_UP_3_ROWS_ = this.CONTROL_ | 0x26;
  6569. this.ROLL_UP_4_ROWS_ = this.CONTROL_ | 0x27;
  6570. this.CARRIAGE_RETURN_ = this.CONTROL_ | 0x2d; // paint-on mode
  6571. this.RESUME_DIRECT_CAPTIONING_ = this.CONTROL_ | 0x29; // Erasure
  6572. this.BACKSPACE_ = this.CONTROL_ | 0x21;
  6573. this.ERASE_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2c;
  6574. this.ERASE_NON_DISPLAYED_MEMORY_ = this.CONTROL_ | 0x2e;
  6575. };
  6576. /**
  6577. * Detects if the 2-byte packet data is a special character
  6578. *
  6579. * Special characters have a second byte in the range 0x30 to 0x3f,
  6580. * with the first byte being 0x11 (for data channel 1) or 0x19 (for
  6581. * data channel 2).
  6582. *
  6583. * @param {Integer} char0 The first byte
  6584. * @param {Integer} char1 The second byte
  6585. * @return {Boolean} Whether the 2 bytes are an special character
  6586. */
  6587. Cea608Stream.prototype.isSpecialCharacter = function (char0, char1) {
  6588. return char0 === this.EXT_ && char1 >= 0x30 && char1 <= 0x3f;
  6589. };
  6590. /**
  6591. * Detects if the 2-byte packet data is an extended character
  6592. *
  6593. * Extended characters have a second byte in the range 0x20 to 0x3f,
  6594. * with the first byte being 0x12 or 0x13 (for data channel 1) or
  6595. * 0x1a or 0x1b (for data channel 2).
  6596. *
  6597. * @param {Integer} char0 The first byte
  6598. * @param {Integer} char1 The second byte
  6599. * @return {Boolean} Whether the 2 bytes are an extended character
  6600. */
  6601. Cea608Stream.prototype.isExtCharacter = function (char0, char1) {
  6602. return (char0 === this.EXT_ + 1 || char0 === this.EXT_ + 2) && char1 >= 0x20 && char1 <= 0x3f;
  6603. };
  6604. /**
  6605. * Detects if the 2-byte packet is a mid-row code
  6606. *
  6607. * Mid-row codes have a second byte in the range 0x20 to 0x2f, with
  6608. * the first byte being 0x11 (for data channel 1) or 0x19 (for data
  6609. * channel 2).
  6610. *
  6611. * @param {Integer} char0 The first byte
  6612. * @param {Integer} char1 The second byte
  6613. * @return {Boolean} Whether the 2 bytes are a mid-row code
  6614. */
  6615. Cea608Stream.prototype.isMidRowCode = function (char0, char1) {
  6616. return char0 === this.EXT_ && char1 >= 0x20 && char1 <= 0x2f;
  6617. };
  6618. /**
  6619. * Detects if the 2-byte packet is an offset control code
  6620. *
  6621. * Offset control codes have a second byte in the range 0x21 to 0x23,
  6622. * with the first byte being 0x17 (for data channel 1) or 0x1f (for
  6623. * data channel 2).
  6624. *
  6625. * @param {Integer} char0 The first byte
  6626. * @param {Integer} char1 The second byte
  6627. * @return {Boolean} Whether the 2 bytes are an offset control code
  6628. */
  6629. Cea608Stream.prototype.isOffsetControlCode = function (char0, char1) {
  6630. return char0 === this.OFFSET_ && char1 >= 0x21 && char1 <= 0x23;
  6631. };
  6632. /**
  6633. * Detects if the 2-byte packet is a Preamble Address Code
  6634. *
  6635. * PACs have a first byte in the range 0x10 to 0x17 (for data channel 1)
  6636. * or 0x18 to 0x1f (for data channel 2), with the second byte in the
  6637. * range 0x40 to 0x7f.
  6638. *
  6639. * @param {Integer} char0 The first byte
  6640. * @param {Integer} char1 The second byte
  6641. * @return {Boolean} Whether the 2 bytes are a PAC
  6642. */
  6643. Cea608Stream.prototype.isPAC = function (char0, char1) {
  6644. return char0 >= this.BASE_ && char0 < this.BASE_ + 8 && char1 >= 0x40 && char1 <= 0x7f;
  6645. };
  6646. /**
  6647. * Detects if a packet's second byte is in the range of a PAC color code
  6648. *
  6649. * PAC color codes have the second byte be in the range 0x40 to 0x4f, or
  6650. * 0x60 to 0x6f.
  6651. *
  6652. * @param {Integer} char1 The second byte
  6653. * @return {Boolean} Whether the byte is a color PAC
  6654. */
  6655. Cea608Stream.prototype.isColorPAC = function (char1) {
  6656. return char1 >= 0x40 && char1 <= 0x4f || char1 >= 0x60 && char1 <= 0x7f;
  6657. };
  6658. /**
  6659. * Detects if a single byte is in the range of a normal character
  6660. *
  6661. * Normal text bytes are in the range 0x20 to 0x7f.
  6662. *
  6663. * @param {Integer} char The byte
  6664. * @return {Boolean} Whether the byte is a normal character
  6665. */
  6666. Cea608Stream.prototype.isNormalChar = function (char) {
  6667. return char >= 0x20 && char <= 0x7f;
  6668. };
  6669. /**
  6670. * Configures roll-up
  6671. *
  6672. * @param {Integer} pts Current PTS
  6673. * @param {Integer} newBaseRow Used by PACs to slide the current window to
  6674. * a new position
  6675. */
  6676. Cea608Stream.prototype.setRollUp = function (pts, newBaseRow) {
  6677. // Reset the base row to the bottom row when switching modes
  6678. if (this.mode_ !== 'rollUp') {
  6679. this.row_ = BOTTOM_ROW;
  6680. this.mode_ = 'rollUp'; // Spec says to wipe memories when switching to roll-up
  6681. this.flushDisplayed(pts);
  6682. this.nonDisplayed_ = createDisplayBuffer();
  6683. this.displayed_ = createDisplayBuffer();
  6684. }
  6685. if (newBaseRow !== undefined && newBaseRow !== this.row_) {
  6686. // move currently displayed captions (up or down) to the new base row
  6687. for (var i = 0; i < this.rollUpRows_; i++) {
  6688. this.displayed_[newBaseRow - i] = this.displayed_[this.row_ - i];
  6689. this.displayed_[this.row_ - i] = {
  6690. text: '',
  6691. indent: 0,
  6692. offset: 0
  6693. };
  6694. }
  6695. }
  6696. if (newBaseRow === undefined) {
  6697. newBaseRow = this.row_;
  6698. }
  6699. this.topRow_ = newBaseRow - this.rollUpRows_ + 1;
  6700. }; // Adds the opening HTML tag for the passed character to the caption text,
  6701. // and keeps track of it for later closing
  6702. Cea608Stream.prototype.addFormatting = function (pts, format) {
  6703. this.formatting_ = this.formatting_.concat(format);
  6704. var text = format.reduce(function (text, format) {
  6705. return text + '<' + format + '>';
  6706. }, '');
  6707. this[this.mode_](pts, text);
  6708. }; // Adds HTML closing tags for current formatting to caption text and
  6709. // clears remembered formatting
  6710. Cea608Stream.prototype.clearFormatting = function (pts) {
  6711. if (!this.formatting_.length) {
  6712. return;
  6713. }
  6714. var text = this.formatting_.reverse().reduce(function (text, format) {
  6715. return text + '</' + format + '>';
  6716. }, '');
  6717. this.formatting_ = [];
  6718. this[this.mode_](pts, text);
  6719. }; // Mode Implementations
  6720. Cea608Stream.prototype.popOn = function (pts, text) {
  6721. var baseRow = this.nonDisplayed_[this.row_].text; // buffer characters
  6722. baseRow += text;
  6723. this.nonDisplayed_[this.row_].text = baseRow;
  6724. };
  6725. Cea608Stream.prototype.rollUp = function (pts, text) {
  6726. var baseRow = this.displayed_[this.row_].text;
  6727. baseRow += text;
  6728. this.displayed_[this.row_].text = baseRow;
  6729. };
  6730. Cea608Stream.prototype.shiftRowsUp_ = function () {
  6731. var i; // clear out inactive rows
  6732. for (i = 0; i < this.topRow_; i++) {
  6733. this.displayed_[i] = {
  6734. text: '',
  6735. indent: 0,
  6736. offset: 0
  6737. };
  6738. }
  6739. for (i = this.row_ + 1; i < BOTTOM_ROW + 1; i++) {
  6740. this.displayed_[i] = {
  6741. text: '',
  6742. indent: 0,
  6743. offset: 0
  6744. };
  6745. } // shift displayed rows up
  6746. for (i = this.topRow_; i < this.row_; i++) {
  6747. this.displayed_[i] = this.displayed_[i + 1];
  6748. } // clear out the bottom row
  6749. this.displayed_[this.row_] = {
  6750. text: '',
  6751. indent: 0,
  6752. offset: 0
  6753. };
  6754. };
  6755. Cea608Stream.prototype.paintOn = function (pts, text) {
  6756. var baseRow = this.displayed_[this.row_].text;
  6757. baseRow += text;
  6758. this.displayed_[this.row_].text = baseRow;
  6759. }; // exports
  6760. var captionStream = {
  6761. CaptionStream: CaptionStream$2,
  6762. Cea608Stream: Cea608Stream,
  6763. Cea708Stream: Cea708Stream
  6764. };
  6765. /**
  6766. * mux.js
  6767. *
  6768. * Copyright (c) Brightcove
  6769. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6770. */
  6771. var streamTypes = {
  6772. H264_STREAM_TYPE: 0x1B,
  6773. ADTS_STREAM_TYPE: 0x0F,
  6774. METADATA_STREAM_TYPE: 0x15
  6775. };
  6776. /**
  6777. * mux.js
  6778. *
  6779. * Copyright (c) Brightcove
  6780. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6781. *
  6782. * Accepts program elementary stream (PES) data events and corrects
  6783. * decode and presentation time stamps to account for a rollover
  6784. * of the 33 bit value.
  6785. */
  6786. var Stream$6 = stream;
  6787. var MAX_TS = 8589934592;
  6788. var RO_THRESH = 4294967296;
  6789. var TYPE_SHARED = 'shared';
  6790. var handleRollover$1 = function (value, reference) {
  6791. var direction = 1;
  6792. if (value > reference) {
  6793. // If the current timestamp value is greater than our reference timestamp and we detect a
  6794. // timestamp rollover, this means the roll over is happening in the opposite direction.
  6795. // Example scenario: Enter a long stream/video just after a rollover occurred. The reference
  6796. // point will be set to a small number, e.g. 1. The user then seeks backwards over the
  6797. // rollover point. In loading this segment, the timestamp values will be very large,
  6798. // e.g. 2^33 - 1. Since this comes before the data we loaded previously, we want to adjust
  6799. // the time stamp to be `value - 2^33`.
  6800. direction = -1;
  6801. } // Note: A seek forwards or back that is greater than the RO_THRESH (2^32, ~13 hours) will
  6802. // cause an incorrect adjustment.
  6803. while (Math.abs(reference - value) > RO_THRESH) {
  6804. value += direction * MAX_TS;
  6805. }
  6806. return value;
  6807. };
  6808. var TimestampRolloverStream$1 = function (type) {
  6809. var lastDTS, referenceDTS;
  6810. TimestampRolloverStream$1.prototype.init.call(this); // The "shared" type is used in cases where a stream will contain muxed
  6811. // video and audio. We could use `undefined` here, but having a string
  6812. // makes debugging a little clearer.
  6813. this.type_ = type || TYPE_SHARED;
  6814. this.push = function (data) {
  6815. /**
  6816. * Rollover stream expects data from elementary stream.
  6817. * Elementary stream can push forward 2 types of data
  6818. * - Parsed Video/Audio/Timed-metadata PES (packetized elementary stream) packets
  6819. * - Tracks metadata from PMT (Program Map Table)
  6820. * Rollover stream expects pts/dts info to be available, since it stores lastDTS
  6821. * We should ignore non-PES packets since they may override lastDTS to undefined.
  6822. * lastDTS is important to signal the next segments
  6823. * about rollover from the previous segments.
  6824. */
  6825. if (data.type === 'metadata') {
  6826. this.trigger('data', data);
  6827. return;
  6828. } // Any "shared" rollover streams will accept _all_ data. Otherwise,
  6829. // streams will only accept data that matches their type.
  6830. if (this.type_ !== TYPE_SHARED && data.type !== this.type_) {
  6831. return;
  6832. }
  6833. if (referenceDTS === undefined) {
  6834. referenceDTS = data.dts;
  6835. }
  6836. data.dts = handleRollover$1(data.dts, referenceDTS);
  6837. data.pts = handleRollover$1(data.pts, referenceDTS);
  6838. lastDTS = data.dts;
  6839. this.trigger('data', data);
  6840. };
  6841. this.flush = function () {
  6842. referenceDTS = lastDTS;
  6843. this.trigger('done');
  6844. };
  6845. this.endTimeline = function () {
  6846. this.flush();
  6847. this.trigger('endedtimeline');
  6848. };
  6849. this.discontinuity = function () {
  6850. referenceDTS = void 0;
  6851. lastDTS = void 0;
  6852. };
  6853. this.reset = function () {
  6854. this.discontinuity();
  6855. this.trigger('reset');
  6856. };
  6857. };
  6858. TimestampRolloverStream$1.prototype = new Stream$6();
  6859. var timestampRolloverStream = {
  6860. TimestampRolloverStream: TimestampRolloverStream$1,
  6861. handleRollover: handleRollover$1
  6862. }; // Once IE11 support is dropped, this function should be removed.
  6863. var typedArrayIndexOf$1 = (typedArray, element, fromIndex) => {
  6864. if (!typedArray) {
  6865. return -1;
  6866. }
  6867. var currentIndex = fromIndex;
  6868. for (; currentIndex < typedArray.length; currentIndex++) {
  6869. if (typedArray[currentIndex] === element) {
  6870. return currentIndex;
  6871. }
  6872. }
  6873. return -1;
  6874. };
  6875. var typedArray = {
  6876. typedArrayIndexOf: typedArrayIndexOf$1
  6877. };
  6878. /**
  6879. * mux.js
  6880. *
  6881. * Copyright (c) Brightcove
  6882. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  6883. *
  6884. * Tools for parsing ID3 frame data
  6885. * @see http://id3.org/id3v2.3.0
  6886. */
  6887. var typedArrayIndexOf = typedArray.typedArrayIndexOf,
  6888. // Frames that allow different types of text encoding contain a text
  6889. // encoding description byte [ID3v2.4.0 section 4.]
  6890. textEncodingDescriptionByte = {
  6891. Iso88591: 0x00,
  6892. // ISO-8859-1, terminated with \0.
  6893. Utf16: 0x01,
  6894. // UTF-16 encoded Unicode BOM, terminated with \0\0
  6895. Utf16be: 0x02,
  6896. // UTF-16BE encoded Unicode, without BOM, terminated with \0\0
  6897. Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0
  6898. },
  6899. // return a percent-encoded representation of the specified byte range
  6900. // @see http://en.wikipedia.org/wiki/Percent-encoding
  6901. percentEncode$1 = function (bytes, start, end) {
  6902. var i,
  6903. result = '';
  6904. for (i = start; i < end; i++) {
  6905. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  6906. }
  6907. return result;
  6908. },
  6909. // return the string representation of the specified byte range,
  6910. // interpreted as UTf-8.
  6911. parseUtf8 = function (bytes, start, end) {
  6912. return decodeURIComponent(percentEncode$1(bytes, start, end));
  6913. },
  6914. // return the string representation of the specified byte range,
  6915. // interpreted as ISO-8859-1.
  6916. parseIso88591$1 = function (bytes, start, end) {
  6917. return unescape(percentEncode$1(bytes, start, end)); // jshint ignore:line
  6918. },
  6919. parseSyncSafeInteger$1 = function (data) {
  6920. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  6921. },
  6922. frameParsers = {
  6923. 'APIC': function (frame) {
  6924. var i = 1,
  6925. mimeTypeEndIndex,
  6926. descriptionEndIndex,
  6927. LINK_MIME_TYPE = '-->';
  6928. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  6929. // ignore frames with unrecognized character encodings
  6930. return;
  6931. } // parsing fields [ID3v2.4.0 section 4.14.]
  6932. mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i);
  6933. if (mimeTypeEndIndex < 0) {
  6934. // malformed frame
  6935. return;
  6936. } // parsing Mime type field (terminated with \0)
  6937. frame.mimeType = parseIso88591$1(frame.data, i, mimeTypeEndIndex);
  6938. i = mimeTypeEndIndex + 1; // parsing 1-byte Picture Type field
  6939. frame.pictureType = frame.data[i];
  6940. i++;
  6941. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i);
  6942. if (descriptionEndIndex < 0) {
  6943. // malformed frame
  6944. return;
  6945. } // parsing Description field (terminated with \0)
  6946. frame.description = parseUtf8(frame.data, i, descriptionEndIndex);
  6947. i = descriptionEndIndex + 1;
  6948. if (frame.mimeType === LINK_MIME_TYPE) {
  6949. // parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.])
  6950. frame.url = parseIso88591$1(frame.data, i, frame.data.length);
  6951. } else {
  6952. // parsing Picture Data field as binary data
  6953. frame.pictureData = frame.data.subarray(i, frame.data.length);
  6954. }
  6955. },
  6956. 'T*': function (frame) {
  6957. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  6958. // ignore frames with unrecognized character encodings
  6959. return;
  6960. } // parse text field, do not include null terminator in the frame value
  6961. // frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.]
  6962. 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.]
  6963. frame.values = frame.value.split('\0');
  6964. },
  6965. 'TXXX': function (frame) {
  6966. var descriptionEndIndex;
  6967. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  6968. // ignore frames with unrecognized character encodings
  6969. return;
  6970. }
  6971. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  6972. if (descriptionEndIndex === -1) {
  6973. return;
  6974. } // parse the text fields
  6975. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // do not include the null terminator in the tag value
  6976. // frames that allow different types of encoding contain terminated text
  6977. // [ID3v2.4.0 section 4.]
  6978. frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, '');
  6979. frame.data = frame.value;
  6980. },
  6981. 'W*': function (frame) {
  6982. // parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  6983. // if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3]
  6984. frame.url = parseIso88591$1(frame.data, 0, frame.data.length).replace(/\0.*$/, '');
  6985. },
  6986. 'WXXX': function (frame) {
  6987. var descriptionEndIndex;
  6988. if (frame.data[0] !== textEncodingDescriptionByte.Utf8) {
  6989. // ignore frames with unrecognized character encodings
  6990. return;
  6991. }
  6992. descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1);
  6993. if (descriptionEndIndex === -1) {
  6994. return;
  6995. } // parse the description and URL fields
  6996. frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.]
  6997. // if the value is followed by a string termination all the following information
  6998. // should be ignored [ID3v2.4.0 section 4.3]
  6999. frame.url = parseIso88591$1(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, '');
  7000. },
  7001. 'PRIV': function (frame) {
  7002. var i;
  7003. for (i = 0; i < frame.data.length; i++) {
  7004. if (frame.data[i] === 0) {
  7005. // parse the description and URL fields
  7006. frame.owner = parseIso88591$1(frame.data, 0, i);
  7007. break;
  7008. }
  7009. }
  7010. frame.privateData = frame.data.subarray(i + 1);
  7011. frame.data = frame.privateData;
  7012. }
  7013. };
  7014. var parseId3Frames$1 = function (data) {
  7015. var frameSize,
  7016. frameHeader,
  7017. frameStart = 10,
  7018. tagSize = 0,
  7019. frames = []; // If we don't have enough data for a header, 10 bytes,
  7020. // or 'ID3' in the first 3 bytes this is not a valid ID3 tag.
  7021. if (data.length < 10 || data[0] !== 'I'.charCodeAt(0) || data[1] !== 'D'.charCodeAt(0) || data[2] !== '3'.charCodeAt(0)) {
  7022. return;
  7023. } // the frame size is transmitted as a 28-bit integer in the
  7024. // last four bytes of the ID3 header.
  7025. // The most significant bit of each byte is dropped and the
  7026. // results concatenated to recover the actual value.
  7027. tagSize = parseSyncSafeInteger$1(data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  7028. // convenient for our comparisons to include it
  7029. tagSize += 10; // check bit 6 of byte 5 for the extended header flag.
  7030. var hasExtendedHeader = data[5] & 0x40;
  7031. if (hasExtendedHeader) {
  7032. // advance the frame start past the extended header
  7033. frameStart += 4; // header size field
  7034. frameStart += parseSyncSafeInteger$1(data.subarray(10, 14));
  7035. tagSize -= parseSyncSafeInteger$1(data.subarray(16, 20)); // clip any padding off the end
  7036. } // parse one or more ID3 frames
  7037. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  7038. do {
  7039. // determine the number of bytes in this frame
  7040. frameSize = parseSyncSafeInteger$1(data.subarray(frameStart + 4, frameStart + 8));
  7041. if (frameSize < 1) {
  7042. break;
  7043. }
  7044. frameHeader = String.fromCharCode(data[frameStart], data[frameStart + 1], data[frameStart + 2], data[frameStart + 3]);
  7045. var frame = {
  7046. id: frameHeader,
  7047. data: data.subarray(frameStart + 10, frameStart + frameSize + 10)
  7048. };
  7049. frame.key = frame.id; // parse frame values
  7050. if (frameParsers[frame.id]) {
  7051. // use frame specific parser
  7052. frameParsers[frame.id](frame);
  7053. } else if (frame.id[0] === 'T') {
  7054. // use text frame generic parser
  7055. frameParsers['T*'](frame);
  7056. } else if (frame.id[0] === 'W') {
  7057. // use URL link frame generic parser
  7058. frameParsers['W*'](frame);
  7059. }
  7060. frames.push(frame);
  7061. frameStart += 10; // advance past the frame header
  7062. frameStart += frameSize; // advance past the frame body
  7063. } while (frameStart < tagSize);
  7064. return frames;
  7065. };
  7066. var parseId3 = {
  7067. parseId3Frames: parseId3Frames$1,
  7068. parseSyncSafeInteger: parseSyncSafeInteger$1,
  7069. frameParsers: frameParsers
  7070. };
  7071. /**
  7072. * mux.js
  7073. *
  7074. * Copyright (c) Brightcove
  7075. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7076. *
  7077. * Accepts program elementary stream (PES) data events and parses out
  7078. * ID3 metadata from them, if present.
  7079. * @see http://id3.org/id3v2.3.0
  7080. */
  7081. var Stream$5 = stream,
  7082. StreamTypes$3 = streamTypes,
  7083. id3 = parseId3,
  7084. MetadataStream;
  7085. MetadataStream = function (options) {
  7086. var settings = {
  7087. // the bytes of the program-level descriptor field in MP2T
  7088. // see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
  7089. // program element descriptors"
  7090. descriptor: options && options.descriptor
  7091. },
  7092. // the total size in bytes of the ID3 tag being parsed
  7093. tagSize = 0,
  7094. // tag data that is not complete enough to be parsed
  7095. buffer = [],
  7096. // the total number of bytes currently in the buffer
  7097. bufferSize = 0,
  7098. i;
  7099. MetadataStream.prototype.init.call(this); // calculate the text track in-band metadata track dispatch type
  7100. // https://html.spec.whatwg.org/multipage/embedded-content.html#steps-to-expose-a-media-resource-specific-text-track
  7101. this.dispatchType = StreamTypes$3.METADATA_STREAM_TYPE.toString(16);
  7102. if (settings.descriptor) {
  7103. for (i = 0; i < settings.descriptor.length; i++) {
  7104. this.dispatchType += ('00' + settings.descriptor[i].toString(16)).slice(-2);
  7105. }
  7106. }
  7107. this.push = function (chunk) {
  7108. var tag, frameStart, frameSize, frame, i, frameHeader;
  7109. if (chunk.type !== 'timed-metadata') {
  7110. return;
  7111. } // if data_alignment_indicator is set in the PES header,
  7112. // we must have the start of a new ID3 tag. Assume anything
  7113. // remaining in the buffer was malformed and throw it out
  7114. if (chunk.dataAlignmentIndicator) {
  7115. bufferSize = 0;
  7116. buffer.length = 0;
  7117. } // ignore events that don't look like ID3 data
  7118. 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))) {
  7119. this.trigger('log', {
  7120. level: 'warn',
  7121. message: 'Skipping unrecognized metadata packet'
  7122. });
  7123. return;
  7124. } // add this chunk to the data we've collected so far
  7125. buffer.push(chunk);
  7126. bufferSize += chunk.data.byteLength; // grab the size of the entire frame from the ID3 header
  7127. if (buffer.length === 1) {
  7128. // the frame size is transmitted as a 28-bit integer in the
  7129. // last four bytes of the ID3 header.
  7130. // The most significant bit of each byte is dropped and the
  7131. // results concatenated to recover the actual value.
  7132. tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more
  7133. // convenient for our comparisons to include it
  7134. tagSize += 10;
  7135. } // if the entire frame has not arrived, wait for more data
  7136. if (bufferSize < tagSize) {
  7137. return;
  7138. } // collect the entire frame so it can be parsed
  7139. tag = {
  7140. data: new Uint8Array(tagSize),
  7141. frames: [],
  7142. pts: buffer[0].pts,
  7143. dts: buffer[0].dts
  7144. };
  7145. for (i = 0; i < tagSize;) {
  7146. tag.data.set(buffer[0].data.subarray(0, tagSize - i), i);
  7147. i += buffer[0].data.byteLength;
  7148. bufferSize -= buffer[0].data.byteLength;
  7149. buffer.shift();
  7150. } // find the start of the first frame and the end of the tag
  7151. frameStart = 10;
  7152. if (tag.data[5] & 0x40) {
  7153. // advance the frame start past the extended header
  7154. frameStart += 4; // header size field
  7155. frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end
  7156. tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20));
  7157. } // parse one or more ID3 frames
  7158. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  7159. do {
  7160. // determine the number of bytes in this frame
  7161. frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
  7162. if (frameSize < 1) {
  7163. this.trigger('log', {
  7164. level: 'warn',
  7165. message: 'Malformed ID3 frame encountered. Skipping remaining metadata parsing.'
  7166. }); // If the frame is malformed, don't parse any further frames but allow previous valid parsed frames
  7167. // to be sent along.
  7168. break;
  7169. }
  7170. frameHeader = String.fromCharCode(tag.data[frameStart], tag.data[frameStart + 1], tag.data[frameStart + 2], tag.data[frameStart + 3]);
  7171. frame = {
  7172. id: frameHeader,
  7173. data: tag.data.subarray(frameStart + 10, frameStart + frameSize + 10)
  7174. };
  7175. frame.key = frame.id; // parse frame values
  7176. if (id3.frameParsers[frame.id]) {
  7177. // use frame specific parser
  7178. id3.frameParsers[frame.id](frame);
  7179. } else if (frame.id[0] === 'T') {
  7180. // use text frame generic parser
  7181. id3.frameParsers['T*'](frame);
  7182. } else if (frame.id[0] === 'W') {
  7183. // use URL link frame generic parser
  7184. id3.frameParsers['W*'](frame);
  7185. } // handle the special PRIV frame used to indicate the start
  7186. // time for raw AAC data
  7187. if (frame.owner === 'com.apple.streaming.transportStreamTimestamp') {
  7188. var d = frame.data,
  7189. size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  7190. size *= 4;
  7191. size += d[7] & 0x03;
  7192. frame.timeStamp = size; // in raw AAC, all subsequent data will be timestamped based
  7193. // on the value of this frame
  7194. // we couldn't have known the appropriate pts and dts before
  7195. // parsing this ID3 tag so set those values now
  7196. if (tag.pts === undefined && tag.dts === undefined) {
  7197. tag.pts = frame.timeStamp;
  7198. tag.dts = frame.timeStamp;
  7199. }
  7200. this.trigger('timestamp', frame);
  7201. }
  7202. tag.frames.push(frame);
  7203. frameStart += 10; // advance past the frame header
  7204. frameStart += frameSize; // advance past the frame body
  7205. } while (frameStart < tagSize);
  7206. this.trigger('data', tag);
  7207. };
  7208. };
  7209. MetadataStream.prototype = new Stream$5();
  7210. var metadataStream = MetadataStream;
  7211. /**
  7212. * mux.js
  7213. *
  7214. * Copyright (c) Brightcove
  7215. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7216. *
  7217. * A stream-based mp2t to mp4 converter. This utility can be used to
  7218. * deliver mp4s to a SourceBuffer on platforms that support native
  7219. * Media Source Extensions.
  7220. */
  7221. var Stream$4 = stream,
  7222. CaptionStream$1 = captionStream,
  7223. StreamTypes$2 = streamTypes,
  7224. TimestampRolloverStream = timestampRolloverStream.TimestampRolloverStream; // object types
  7225. var TransportPacketStream, TransportParseStream, ElementaryStream; // constants
  7226. var MP2T_PACKET_LENGTH$1 = 188,
  7227. // bytes
  7228. SYNC_BYTE$1 = 0x47;
  7229. /**
  7230. * Splits an incoming stream of binary data into MPEG-2 Transport
  7231. * Stream packets.
  7232. */
  7233. TransportPacketStream = function () {
  7234. var buffer = new Uint8Array(MP2T_PACKET_LENGTH$1),
  7235. bytesInBuffer = 0;
  7236. TransportPacketStream.prototype.init.call(this); // Deliver new bytes to the stream.
  7237. /**
  7238. * Split a stream of data into M2TS packets
  7239. **/
  7240. this.push = function (bytes) {
  7241. var startIndex = 0,
  7242. endIndex = MP2T_PACKET_LENGTH$1,
  7243. everything; // If there are bytes remaining from the last segment, prepend them to the
  7244. // bytes that were pushed in
  7245. if (bytesInBuffer) {
  7246. everything = new Uint8Array(bytes.byteLength + bytesInBuffer);
  7247. everything.set(buffer.subarray(0, bytesInBuffer));
  7248. everything.set(bytes, bytesInBuffer);
  7249. bytesInBuffer = 0;
  7250. } else {
  7251. everything = bytes;
  7252. } // While we have enough data for a packet
  7253. while (endIndex < everything.byteLength) {
  7254. // Look for a pair of start and end sync bytes in the data..
  7255. if (everything[startIndex] === SYNC_BYTE$1 && everything[endIndex] === SYNC_BYTE$1) {
  7256. // We found a packet so emit it and jump one whole packet forward in
  7257. // the stream
  7258. this.trigger('data', everything.subarray(startIndex, endIndex));
  7259. startIndex += MP2T_PACKET_LENGTH$1;
  7260. endIndex += MP2T_PACKET_LENGTH$1;
  7261. continue;
  7262. } // If we get here, we have somehow become de-synchronized and we need to step
  7263. // forward one byte at a time until we find a pair of sync bytes that denote
  7264. // a packet
  7265. startIndex++;
  7266. endIndex++;
  7267. } // If there was some data left over at the end of the segment that couldn't
  7268. // possibly be a whole packet, keep it because it might be the start of a packet
  7269. // that continues in the next segment
  7270. if (startIndex < everything.byteLength) {
  7271. buffer.set(everything.subarray(startIndex), 0);
  7272. bytesInBuffer = everything.byteLength - startIndex;
  7273. }
  7274. };
  7275. /**
  7276. * Passes identified M2TS packets to the TransportParseStream to be parsed
  7277. **/
  7278. this.flush = function () {
  7279. // If the buffer contains a whole packet when we are being flushed, emit it
  7280. // and empty the buffer. Otherwise hold onto the data because it may be
  7281. // important for decoding the next segment
  7282. if (bytesInBuffer === MP2T_PACKET_LENGTH$1 && buffer[0] === SYNC_BYTE$1) {
  7283. this.trigger('data', buffer);
  7284. bytesInBuffer = 0;
  7285. }
  7286. this.trigger('done');
  7287. };
  7288. this.endTimeline = function () {
  7289. this.flush();
  7290. this.trigger('endedtimeline');
  7291. };
  7292. this.reset = function () {
  7293. bytesInBuffer = 0;
  7294. this.trigger('reset');
  7295. };
  7296. };
  7297. TransportPacketStream.prototype = new Stream$4();
  7298. /**
  7299. * Accepts an MP2T TransportPacketStream and emits data events with parsed
  7300. * forms of the individual transport stream packets.
  7301. */
  7302. TransportParseStream = function () {
  7303. var parsePsi, parsePat, parsePmt, self;
  7304. TransportParseStream.prototype.init.call(this);
  7305. self = this;
  7306. this.packetsWaitingForPmt = [];
  7307. this.programMapTable = undefined;
  7308. parsePsi = function (payload, psi) {
  7309. var offset = 0; // PSI packets may be split into multiple sections and those
  7310. // sections may be split into multiple packets. If a PSI
  7311. // section starts in this packet, the payload_unit_start_indicator
  7312. // will be true and the first byte of the payload will indicate
  7313. // the offset from the current position to the start of the
  7314. // section.
  7315. if (psi.payloadUnitStartIndicator) {
  7316. offset += payload[offset] + 1;
  7317. }
  7318. if (psi.type === 'pat') {
  7319. parsePat(payload.subarray(offset), psi);
  7320. } else {
  7321. parsePmt(payload.subarray(offset), psi);
  7322. }
  7323. };
  7324. parsePat = function (payload, pat) {
  7325. pat.section_number = payload[7]; // eslint-disable-line camelcase
  7326. pat.last_section_number = payload[8]; // eslint-disable-line camelcase
  7327. // skip the PSI header and parse the first PMT entry
  7328. self.pmtPid = (payload[10] & 0x1F) << 8 | payload[11];
  7329. pat.pmtPid = self.pmtPid;
  7330. };
  7331. /**
  7332. * Parse out the relevant fields of a Program Map Table (PMT).
  7333. * @param payload {Uint8Array} the PMT-specific portion of an MP2T
  7334. * packet. The first byte in this array should be the table_id
  7335. * field.
  7336. * @param pmt {object} the object that should be decorated with
  7337. * fields parsed from the PMT.
  7338. */
  7339. parsePmt = function (payload, pmt) {
  7340. var sectionLength, tableEnd, programInfoLength, offset; // PMTs can be sent ahead of the time when they should actually
  7341. // take effect. We don't believe this should ever be the case
  7342. // for HLS but we'll ignore "forward" PMT declarations if we see
  7343. // them. Future PMT declarations have the current_next_indicator
  7344. // set to zero.
  7345. if (!(payload[5] & 0x01)) {
  7346. return;
  7347. } // overwrite any existing program map table
  7348. self.programMapTable = {
  7349. video: null,
  7350. audio: null,
  7351. 'timed-metadata': {}
  7352. }; // the mapping table ends at the end of the current section
  7353. sectionLength = (payload[1] & 0x0f) << 8 | payload[2];
  7354. tableEnd = 3 + sectionLength - 4; // to determine where the table is, we have to figure out how
  7355. // long the program info descriptors are
  7356. programInfoLength = (payload[10] & 0x0f) << 8 | payload[11]; // advance the offset to the first entry in the mapping table
  7357. offset = 12 + programInfoLength;
  7358. while (offset < tableEnd) {
  7359. var streamType = payload[offset];
  7360. var pid = (payload[offset + 1] & 0x1F) << 8 | payload[offset + 2]; // only map a single elementary_pid for audio and video stream types
  7361. // TODO: should this be done for metadata too? for now maintain behavior of
  7362. // multiple metadata streams
  7363. if (streamType === StreamTypes$2.H264_STREAM_TYPE && self.programMapTable.video === null) {
  7364. self.programMapTable.video = pid;
  7365. } else if (streamType === StreamTypes$2.ADTS_STREAM_TYPE && self.programMapTable.audio === null) {
  7366. self.programMapTable.audio = pid;
  7367. } else if (streamType === StreamTypes$2.METADATA_STREAM_TYPE) {
  7368. // map pid to stream type for metadata streams
  7369. self.programMapTable['timed-metadata'][pid] = streamType;
  7370. } // move to the next table entry
  7371. // skip past the elementary stream descriptors, if present
  7372. offset += ((payload[offset + 3] & 0x0F) << 8 | payload[offset + 4]) + 5;
  7373. } // record the map on the packet as well
  7374. pmt.programMapTable = self.programMapTable;
  7375. };
  7376. /**
  7377. * Deliver a new MP2T packet to the next stream in the pipeline.
  7378. */
  7379. this.push = function (packet) {
  7380. var result = {},
  7381. offset = 4;
  7382. result.payloadUnitStartIndicator = !!(packet[1] & 0x40); // pid is a 13-bit field starting at the last bit of packet[1]
  7383. result.pid = packet[1] & 0x1f;
  7384. result.pid <<= 8;
  7385. result.pid |= packet[2]; // if an adaption field is present, its length is specified by the
  7386. // fifth byte of the TS packet header. The adaptation field is
  7387. // used to add stuffing to PES packets that don't fill a complete
  7388. // TS packet, and to specify some forms of timing and control data
  7389. // that we do not currently use.
  7390. if ((packet[3] & 0x30) >>> 4 > 0x01) {
  7391. offset += packet[offset] + 1;
  7392. } // parse the rest of the packet based on the type
  7393. if (result.pid === 0) {
  7394. result.type = 'pat';
  7395. parsePsi(packet.subarray(offset), result);
  7396. this.trigger('data', result);
  7397. } else if (result.pid === this.pmtPid) {
  7398. result.type = 'pmt';
  7399. parsePsi(packet.subarray(offset), result);
  7400. this.trigger('data', result); // if there are any packets waiting for a PMT to be found, process them now
  7401. while (this.packetsWaitingForPmt.length) {
  7402. this.processPes_.apply(this, this.packetsWaitingForPmt.shift());
  7403. }
  7404. } else if (this.programMapTable === undefined) {
  7405. // When we have not seen a PMT yet, defer further processing of
  7406. // PES packets until one has been parsed
  7407. this.packetsWaitingForPmt.push([packet, offset, result]);
  7408. } else {
  7409. this.processPes_(packet, offset, result);
  7410. }
  7411. };
  7412. this.processPes_ = function (packet, offset, result) {
  7413. // set the appropriate stream type
  7414. if (result.pid === this.programMapTable.video) {
  7415. result.streamType = StreamTypes$2.H264_STREAM_TYPE;
  7416. } else if (result.pid === this.programMapTable.audio) {
  7417. result.streamType = StreamTypes$2.ADTS_STREAM_TYPE;
  7418. } else {
  7419. // if not video or audio, it is timed-metadata or unknown
  7420. // if unknown, streamType will be undefined
  7421. result.streamType = this.programMapTable['timed-metadata'][result.pid];
  7422. }
  7423. result.type = 'pes';
  7424. result.data = packet.subarray(offset);
  7425. this.trigger('data', result);
  7426. };
  7427. };
  7428. TransportParseStream.prototype = new Stream$4();
  7429. TransportParseStream.STREAM_TYPES = {
  7430. h264: 0x1b,
  7431. adts: 0x0f
  7432. };
  7433. /**
  7434. * Reconsistutes program elementary stream (PES) packets from parsed
  7435. * transport stream packets. That is, if you pipe an
  7436. * mp2t.TransportParseStream into a mp2t.ElementaryStream, the output
  7437. * events will be events which capture the bytes for individual PES
  7438. * packets plus relevant metadata that has been extracted from the
  7439. * container.
  7440. */
  7441. ElementaryStream = function () {
  7442. var self = this,
  7443. segmentHadPmt = false,
  7444. // PES packet fragments
  7445. video = {
  7446. data: [],
  7447. size: 0
  7448. },
  7449. audio = {
  7450. data: [],
  7451. size: 0
  7452. },
  7453. timedMetadata = {
  7454. data: [],
  7455. size: 0
  7456. },
  7457. programMapTable,
  7458. parsePes = function (payload, pes) {
  7459. var ptsDtsFlags;
  7460. const startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]; // default to an empty array
  7461. pes.data = new Uint8Array(); // In certain live streams, the start of a TS fragment has ts packets
  7462. // that are frame data that is continuing from the previous fragment. This
  7463. // is to check that the pes data is the start of a new pes payload
  7464. if (startPrefix !== 1) {
  7465. return;
  7466. } // get the packet length, this will be 0 for video
  7467. pes.packetLength = 6 + (payload[4] << 8 | payload[5]); // find out if this packets starts a new keyframe
  7468. pes.dataAlignmentIndicator = (payload[6] & 0x04) !== 0; // PES packets may be annotated with a PTS value, or a PTS value
  7469. // and a DTS value. Determine what combination of values is
  7470. // available to work with.
  7471. ptsDtsFlags = payload[7]; // PTS and DTS are normally stored as a 33-bit number. Javascript
  7472. // performs all bitwise operations on 32-bit integers but javascript
  7473. // supports a much greater range (52-bits) of integer using standard
  7474. // mathematical operations.
  7475. // We construct a 31-bit value using bitwise operators over the 31
  7476. // most significant bits and then multiply by 4 (equal to a left-shift
  7477. // of 2) before we add the final 2 least significant bits of the
  7478. // timestamp (equal to an OR.)
  7479. if (ptsDtsFlags & 0xC0) {
  7480. // the PTS and DTS are not written out directly. For information
  7481. // on how they are encoded, see
  7482. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  7483. pes.pts = (payload[9] & 0x0E) << 27 | (payload[10] & 0xFF) << 20 | (payload[11] & 0xFE) << 12 | (payload[12] & 0xFF) << 5 | (payload[13] & 0xFE) >>> 3;
  7484. pes.pts *= 4; // Left shift by 2
  7485. pes.pts += (payload[13] & 0x06) >>> 1; // OR by the two LSBs
  7486. pes.dts = pes.pts;
  7487. if (ptsDtsFlags & 0x40) {
  7488. pes.dts = (payload[14] & 0x0E) << 27 | (payload[15] & 0xFF) << 20 | (payload[16] & 0xFE) << 12 | (payload[17] & 0xFF) << 5 | (payload[18] & 0xFE) >>> 3;
  7489. pes.dts *= 4; // Left shift by 2
  7490. pes.dts += (payload[18] & 0x06) >>> 1; // OR by the two LSBs
  7491. }
  7492. } // the data section starts immediately after the PES header.
  7493. // pes_header_data_length specifies the number of header bytes
  7494. // that follow the last byte of the field.
  7495. pes.data = payload.subarray(9 + payload[8]);
  7496. },
  7497. /**
  7498. * Pass completely parsed PES packets to the next stream in the pipeline
  7499. **/
  7500. flushStream = function (stream, type, forceFlush) {
  7501. var packetData = new Uint8Array(stream.size),
  7502. event = {
  7503. type: type
  7504. },
  7505. i = 0,
  7506. offset = 0,
  7507. packetFlushable = false,
  7508. fragment; // do nothing if there is not enough buffered data for a complete
  7509. // PES header
  7510. if (!stream.data.length || stream.size < 9) {
  7511. return;
  7512. }
  7513. event.trackId = stream.data[0].pid; // reassemble the packet
  7514. for (i = 0; i < stream.data.length; i++) {
  7515. fragment = stream.data[i];
  7516. packetData.set(fragment.data, offset);
  7517. offset += fragment.data.byteLength;
  7518. } // parse assembled packet's PES header
  7519. parsePes(packetData, event); // non-video PES packets MUST have a non-zero PES_packet_length
  7520. // check that there is enough stream data to fill the packet
  7521. packetFlushable = type === 'video' || event.packetLength <= stream.size; // flush pending packets if the conditions are right
  7522. if (forceFlush || packetFlushable) {
  7523. stream.size = 0;
  7524. stream.data.length = 0;
  7525. } // only emit packets that are complete. this is to avoid assembling
  7526. // incomplete PES packets due to poor segmentation
  7527. if (packetFlushable) {
  7528. self.trigger('data', event);
  7529. }
  7530. };
  7531. ElementaryStream.prototype.init.call(this);
  7532. /**
  7533. * Identifies M2TS packet types and parses PES packets using metadata
  7534. * parsed from the PMT
  7535. **/
  7536. this.push = function (data) {
  7537. ({
  7538. pat: function () {// we have to wait for the PMT to arrive as well before we
  7539. // have any meaningful metadata
  7540. },
  7541. pes: function () {
  7542. var stream, streamType;
  7543. switch (data.streamType) {
  7544. case StreamTypes$2.H264_STREAM_TYPE:
  7545. stream = video;
  7546. streamType = 'video';
  7547. break;
  7548. case StreamTypes$2.ADTS_STREAM_TYPE:
  7549. stream = audio;
  7550. streamType = 'audio';
  7551. break;
  7552. case StreamTypes$2.METADATA_STREAM_TYPE:
  7553. stream = timedMetadata;
  7554. streamType = 'timed-metadata';
  7555. break;
  7556. default:
  7557. // ignore unknown stream types
  7558. return;
  7559. } // if a new packet is starting, we can flush the completed
  7560. // packet
  7561. if (data.payloadUnitStartIndicator) {
  7562. flushStream(stream, streamType, true);
  7563. } // buffer this fragment until we are sure we've received the
  7564. // complete payload
  7565. stream.data.push(data);
  7566. stream.size += data.data.byteLength;
  7567. },
  7568. pmt: function () {
  7569. var event = {
  7570. type: 'metadata',
  7571. tracks: []
  7572. };
  7573. programMapTable = data.programMapTable; // translate audio and video streams to tracks
  7574. if (programMapTable.video !== null) {
  7575. event.tracks.push({
  7576. timelineStartInfo: {
  7577. baseMediaDecodeTime: 0
  7578. },
  7579. id: +programMapTable.video,
  7580. codec: 'avc',
  7581. type: 'video'
  7582. });
  7583. }
  7584. if (programMapTable.audio !== null) {
  7585. event.tracks.push({
  7586. timelineStartInfo: {
  7587. baseMediaDecodeTime: 0
  7588. },
  7589. id: +programMapTable.audio,
  7590. codec: 'adts',
  7591. type: 'audio'
  7592. });
  7593. }
  7594. segmentHadPmt = true;
  7595. self.trigger('data', event);
  7596. }
  7597. })[data.type]();
  7598. };
  7599. this.reset = function () {
  7600. video.size = 0;
  7601. video.data.length = 0;
  7602. audio.size = 0;
  7603. audio.data.length = 0;
  7604. this.trigger('reset');
  7605. };
  7606. /**
  7607. * Flush any remaining input. Video PES packets may be of variable
  7608. * length. Normally, the start of a new video packet can trigger the
  7609. * finalization of the previous packet. That is not possible if no
  7610. * more video is forthcoming, however. In that case, some other
  7611. * mechanism (like the end of the file) has to be employed. When it is
  7612. * clear that no additional data is forthcoming, calling this method
  7613. * will flush the buffered packets.
  7614. */
  7615. this.flushStreams_ = function () {
  7616. // !!THIS ORDER IS IMPORTANT!!
  7617. // video first then audio
  7618. flushStream(video, 'video');
  7619. flushStream(audio, 'audio');
  7620. flushStream(timedMetadata, 'timed-metadata');
  7621. };
  7622. this.flush = function () {
  7623. // if on flush we haven't had a pmt emitted
  7624. // and we have a pmt to emit. emit the pmt
  7625. // so that we trigger a trackinfo downstream.
  7626. if (!segmentHadPmt && programMapTable) {
  7627. var pmt = {
  7628. type: 'metadata',
  7629. tracks: []
  7630. }; // translate audio and video streams to tracks
  7631. if (programMapTable.video !== null) {
  7632. pmt.tracks.push({
  7633. timelineStartInfo: {
  7634. baseMediaDecodeTime: 0
  7635. },
  7636. id: +programMapTable.video,
  7637. codec: 'avc',
  7638. type: 'video'
  7639. });
  7640. }
  7641. if (programMapTable.audio !== null) {
  7642. pmt.tracks.push({
  7643. timelineStartInfo: {
  7644. baseMediaDecodeTime: 0
  7645. },
  7646. id: +programMapTable.audio,
  7647. codec: 'adts',
  7648. type: 'audio'
  7649. });
  7650. }
  7651. self.trigger('data', pmt);
  7652. }
  7653. segmentHadPmt = false;
  7654. this.flushStreams_();
  7655. this.trigger('done');
  7656. };
  7657. };
  7658. ElementaryStream.prototype = new Stream$4();
  7659. var m2ts$1 = {
  7660. PAT_PID: 0x0000,
  7661. MP2T_PACKET_LENGTH: MP2T_PACKET_LENGTH$1,
  7662. TransportPacketStream: TransportPacketStream,
  7663. TransportParseStream: TransportParseStream,
  7664. ElementaryStream: ElementaryStream,
  7665. TimestampRolloverStream: TimestampRolloverStream,
  7666. CaptionStream: CaptionStream$1.CaptionStream,
  7667. Cea608Stream: CaptionStream$1.Cea608Stream,
  7668. Cea708Stream: CaptionStream$1.Cea708Stream,
  7669. MetadataStream: metadataStream
  7670. };
  7671. for (var type in StreamTypes$2) {
  7672. if (StreamTypes$2.hasOwnProperty(type)) {
  7673. m2ts$1[type] = StreamTypes$2[type];
  7674. }
  7675. }
  7676. var m2ts_1 = m2ts$1;
  7677. /**
  7678. * mux.js
  7679. *
  7680. * Copyright (c) Brightcove
  7681. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7682. */
  7683. var Stream$3 = stream;
  7684. var ONE_SECOND_IN_TS$2 = clock$2.ONE_SECOND_IN_TS;
  7685. var AdtsStream$1;
  7686. var ADTS_SAMPLING_FREQUENCIES$1 = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  7687. /*
  7688. * Accepts a ElementaryStream and emits data events with parsed
  7689. * AAC Audio Frames of the individual packets. Input audio in ADTS
  7690. * format is unpacked and re-emitted as AAC frames.
  7691. *
  7692. * @see http://wiki.multimedia.cx/index.php?title=ADTS
  7693. * @see http://wiki.multimedia.cx/?title=Understanding_AAC
  7694. */
  7695. AdtsStream$1 = function (handlePartialSegments) {
  7696. var buffer,
  7697. frameNum = 0;
  7698. AdtsStream$1.prototype.init.call(this);
  7699. this.skipWarn_ = function (start, end) {
  7700. this.trigger('log', {
  7701. level: 'warn',
  7702. message: `adts skiping bytes ${start} to ${end} in frame ${frameNum} outside syncword`
  7703. });
  7704. };
  7705. this.push = function (packet) {
  7706. var i = 0,
  7707. frameLength,
  7708. protectionSkipBytes,
  7709. oldBuffer,
  7710. sampleCount,
  7711. adtsFrameDuration;
  7712. if (!handlePartialSegments) {
  7713. frameNum = 0;
  7714. }
  7715. if (packet.type !== 'audio') {
  7716. // ignore non-audio data
  7717. return;
  7718. } // Prepend any data in the buffer to the input data so that we can parse
  7719. // aac frames the cross a PES packet boundary
  7720. if (buffer && buffer.length) {
  7721. oldBuffer = buffer;
  7722. buffer = new Uint8Array(oldBuffer.byteLength + packet.data.byteLength);
  7723. buffer.set(oldBuffer);
  7724. buffer.set(packet.data, oldBuffer.byteLength);
  7725. } else {
  7726. buffer = packet.data;
  7727. } // unpack any ADTS frames which have been fully received
  7728. // for details on the ADTS header, see http://wiki.multimedia.cx/index.php?title=ADTS
  7729. var skip; // We use i + 7 here because we want to be able to parse the entire header.
  7730. // If we don't have enough bytes to do that, then we definitely won't have a full frame.
  7731. while (i + 7 < buffer.length) {
  7732. // Look for the start of an ADTS header..
  7733. if (buffer[i] !== 0xFF || (buffer[i + 1] & 0xF6) !== 0xF0) {
  7734. if (typeof skip !== 'number') {
  7735. skip = i;
  7736. } // If a valid header was not found, jump one forward and attempt to
  7737. // find a valid ADTS header starting at the next byte
  7738. i++;
  7739. continue;
  7740. }
  7741. if (typeof skip === 'number') {
  7742. this.skipWarn_(skip, i);
  7743. skip = null;
  7744. } // The protection skip bit tells us if we have 2 bytes of CRC data at the
  7745. // end of the ADTS header
  7746. protectionSkipBytes = (~buffer[i + 1] & 0x01) * 2; // Frame length is a 13 bit integer starting 16 bits from the
  7747. // end of the sync sequence
  7748. // NOTE: frame length includes the size of the header
  7749. frameLength = (buffer[i + 3] & 0x03) << 11 | buffer[i + 4] << 3 | (buffer[i + 5] & 0xe0) >> 5;
  7750. sampleCount = ((buffer[i + 6] & 0x03) + 1) * 1024;
  7751. adtsFrameDuration = sampleCount * ONE_SECOND_IN_TS$2 / ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2]; // If we don't have enough data to actually finish this ADTS frame,
  7752. // then we have to wait for more data
  7753. if (buffer.byteLength - i < frameLength) {
  7754. break;
  7755. } // Otherwise, deliver the complete AAC frame
  7756. this.trigger('data', {
  7757. pts: packet.pts + frameNum * adtsFrameDuration,
  7758. dts: packet.dts + frameNum * adtsFrameDuration,
  7759. sampleCount: sampleCount,
  7760. audioobjecttype: (buffer[i + 2] >>> 6 & 0x03) + 1,
  7761. channelcount: (buffer[i + 2] & 1) << 2 | (buffer[i + 3] & 0xc0) >>> 6,
  7762. samplerate: ADTS_SAMPLING_FREQUENCIES$1[(buffer[i + 2] & 0x3c) >>> 2],
  7763. samplingfrequencyindex: (buffer[i + 2] & 0x3c) >>> 2,
  7764. // assume ISO/IEC 14496-12 AudioSampleEntry default of 16
  7765. samplesize: 16,
  7766. // data is the frame without it's header
  7767. data: buffer.subarray(i + 7 + protectionSkipBytes, i + frameLength)
  7768. });
  7769. frameNum++;
  7770. i += frameLength;
  7771. }
  7772. if (typeof skip === 'number') {
  7773. this.skipWarn_(skip, i);
  7774. skip = null;
  7775. } // remove processed bytes from the buffer.
  7776. buffer = buffer.subarray(i);
  7777. };
  7778. this.flush = function () {
  7779. frameNum = 0;
  7780. this.trigger('done');
  7781. };
  7782. this.reset = function () {
  7783. buffer = void 0;
  7784. this.trigger('reset');
  7785. };
  7786. this.endTimeline = function () {
  7787. buffer = void 0;
  7788. this.trigger('endedtimeline');
  7789. };
  7790. };
  7791. AdtsStream$1.prototype = new Stream$3();
  7792. var adts = AdtsStream$1;
  7793. /**
  7794. * mux.js
  7795. *
  7796. * Copyright (c) Brightcove
  7797. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7798. */
  7799. var ExpGolomb$1;
  7800. /**
  7801. * Parser for exponential Golomb codes, a variable-bitwidth number encoding
  7802. * scheme used by h264.
  7803. */
  7804. ExpGolomb$1 = function (workingData) {
  7805. var // the number of bytes left to examine in workingData
  7806. workingBytesAvailable = workingData.byteLength,
  7807. // the current word being examined
  7808. workingWord = 0,
  7809. // :uint
  7810. // the number of bits left to examine in the current word
  7811. workingBitsAvailable = 0; // :uint;
  7812. // ():uint
  7813. this.length = function () {
  7814. return 8 * workingBytesAvailable;
  7815. }; // ():uint
  7816. this.bitsAvailable = function () {
  7817. return 8 * workingBytesAvailable + workingBitsAvailable;
  7818. }; // ():void
  7819. this.loadWord = function () {
  7820. var position = workingData.byteLength - workingBytesAvailable,
  7821. workingBytes = new Uint8Array(4),
  7822. availableBytes = Math.min(4, workingBytesAvailable);
  7823. if (availableBytes === 0) {
  7824. throw new Error('no bytes available');
  7825. }
  7826. workingBytes.set(workingData.subarray(position, position + availableBytes));
  7827. workingWord = new DataView(workingBytes.buffer).getUint32(0); // track the amount of workingData that has been processed
  7828. workingBitsAvailable = availableBytes * 8;
  7829. workingBytesAvailable -= availableBytes;
  7830. }; // (count:int):void
  7831. this.skipBits = function (count) {
  7832. var skipBytes; // :int
  7833. if (workingBitsAvailable > count) {
  7834. workingWord <<= count;
  7835. workingBitsAvailable -= count;
  7836. } else {
  7837. count -= workingBitsAvailable;
  7838. skipBytes = Math.floor(count / 8);
  7839. count -= skipBytes * 8;
  7840. workingBytesAvailable -= skipBytes;
  7841. this.loadWord();
  7842. workingWord <<= count;
  7843. workingBitsAvailable -= count;
  7844. }
  7845. }; // (size:int):uint
  7846. this.readBits = function (size) {
  7847. var bits = Math.min(workingBitsAvailable, size),
  7848. // :uint
  7849. valu = workingWord >>> 32 - bits; // :uint
  7850. // if size > 31, handle error
  7851. workingBitsAvailable -= bits;
  7852. if (workingBitsAvailable > 0) {
  7853. workingWord <<= bits;
  7854. } else if (workingBytesAvailable > 0) {
  7855. this.loadWord();
  7856. }
  7857. bits = size - bits;
  7858. if (bits > 0) {
  7859. return valu << bits | this.readBits(bits);
  7860. }
  7861. return valu;
  7862. }; // ():uint
  7863. this.skipLeadingZeros = function () {
  7864. var leadingZeroCount; // :uint
  7865. for (leadingZeroCount = 0; leadingZeroCount < workingBitsAvailable; ++leadingZeroCount) {
  7866. if ((workingWord & 0x80000000 >>> leadingZeroCount) !== 0) {
  7867. // the first bit of working word is 1
  7868. workingWord <<= leadingZeroCount;
  7869. workingBitsAvailable -= leadingZeroCount;
  7870. return leadingZeroCount;
  7871. }
  7872. } // we exhausted workingWord and still have not found a 1
  7873. this.loadWord();
  7874. return leadingZeroCount + this.skipLeadingZeros();
  7875. }; // ():void
  7876. this.skipUnsignedExpGolomb = function () {
  7877. this.skipBits(1 + this.skipLeadingZeros());
  7878. }; // ():void
  7879. this.skipExpGolomb = function () {
  7880. this.skipBits(1 + this.skipLeadingZeros());
  7881. }; // ():uint
  7882. this.readUnsignedExpGolomb = function () {
  7883. var clz = this.skipLeadingZeros(); // :uint
  7884. return this.readBits(clz + 1) - 1;
  7885. }; // ():int
  7886. this.readExpGolomb = function () {
  7887. var valu = this.readUnsignedExpGolomb(); // :int
  7888. if (0x01 & valu) {
  7889. // the number is odd if the low order bit is set
  7890. return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
  7891. }
  7892. return -1 * (valu >>> 1); // divide by two then make it negative
  7893. }; // Some convenience functions
  7894. // :Boolean
  7895. this.readBoolean = function () {
  7896. return this.readBits(1) === 1;
  7897. }; // ():int
  7898. this.readUnsignedByte = function () {
  7899. return this.readBits(8);
  7900. };
  7901. this.loadWord();
  7902. };
  7903. var expGolomb = ExpGolomb$1;
  7904. /**
  7905. * mux.js
  7906. *
  7907. * Copyright (c) Brightcove
  7908. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  7909. */
  7910. var Stream$2 = stream;
  7911. var ExpGolomb = expGolomb;
  7912. var H264Stream$1, NalByteStream;
  7913. var PROFILES_WITH_OPTIONAL_SPS_DATA;
  7914. /**
  7915. * Accepts a NAL unit byte stream and unpacks the embedded NAL units.
  7916. */
  7917. NalByteStream = function () {
  7918. var syncPoint = 0,
  7919. i,
  7920. buffer;
  7921. NalByteStream.prototype.init.call(this);
  7922. /*
  7923. * Scans a byte stream and triggers a data event with the NAL units found.
  7924. * @param {Object} data Event received from H264Stream
  7925. * @param {Uint8Array} data.data The h264 byte stream to be scanned
  7926. *
  7927. * @see H264Stream.push
  7928. */
  7929. this.push = function (data) {
  7930. var swapBuffer;
  7931. if (!buffer) {
  7932. buffer = data.data;
  7933. } else {
  7934. swapBuffer = new Uint8Array(buffer.byteLength + data.data.byteLength);
  7935. swapBuffer.set(buffer);
  7936. swapBuffer.set(data.data, buffer.byteLength);
  7937. buffer = swapBuffer;
  7938. }
  7939. var len = buffer.byteLength; // Rec. ITU-T H.264, Annex B
  7940. // scan for NAL unit boundaries
  7941. // a match looks like this:
  7942. // 0 0 1 .. NAL .. 0 0 1
  7943. // ^ sync point ^ i
  7944. // or this:
  7945. // 0 0 1 .. NAL .. 0 0 0
  7946. // ^ sync point ^ i
  7947. // advance the sync point to a NAL start, if necessary
  7948. for (; syncPoint < len - 3; syncPoint++) {
  7949. if (buffer[syncPoint + 2] === 1) {
  7950. // the sync point is properly aligned
  7951. i = syncPoint + 5;
  7952. break;
  7953. }
  7954. }
  7955. while (i < len) {
  7956. // look at the current byte to determine if we've hit the end of
  7957. // a NAL unit boundary
  7958. switch (buffer[i]) {
  7959. case 0:
  7960. // skip past non-sync sequences
  7961. if (buffer[i - 1] !== 0) {
  7962. i += 2;
  7963. break;
  7964. } else if (buffer[i - 2] !== 0) {
  7965. i++;
  7966. break;
  7967. } // deliver the NAL unit if it isn't empty
  7968. if (syncPoint + 3 !== i - 2) {
  7969. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  7970. } // drop trailing zeroes
  7971. do {
  7972. i++;
  7973. } while (buffer[i] !== 1 && i < len);
  7974. syncPoint = i - 2;
  7975. i += 3;
  7976. break;
  7977. case 1:
  7978. // skip past non-sync sequences
  7979. if (buffer[i - 1] !== 0 || buffer[i - 2] !== 0) {
  7980. i += 3;
  7981. break;
  7982. } // deliver the NAL unit
  7983. this.trigger('data', buffer.subarray(syncPoint + 3, i - 2));
  7984. syncPoint = i - 2;
  7985. i += 3;
  7986. break;
  7987. default:
  7988. // the current byte isn't a one or zero, so it cannot be part
  7989. // of a sync sequence
  7990. i += 3;
  7991. break;
  7992. }
  7993. } // filter out the NAL units that were delivered
  7994. buffer = buffer.subarray(syncPoint);
  7995. i -= syncPoint;
  7996. syncPoint = 0;
  7997. };
  7998. this.reset = function () {
  7999. buffer = null;
  8000. syncPoint = 0;
  8001. this.trigger('reset');
  8002. };
  8003. this.flush = function () {
  8004. // deliver the last buffered NAL unit
  8005. if (buffer && buffer.byteLength > 3) {
  8006. this.trigger('data', buffer.subarray(syncPoint + 3));
  8007. } // reset the stream state
  8008. buffer = null;
  8009. syncPoint = 0;
  8010. this.trigger('done');
  8011. };
  8012. this.endTimeline = function () {
  8013. this.flush();
  8014. this.trigger('endedtimeline');
  8015. };
  8016. };
  8017. NalByteStream.prototype = new Stream$2(); // values of profile_idc that indicate additional fields are included in the SPS
  8018. // see Recommendation ITU-T H.264 (4/2013),
  8019. // 7.3.2.1.1 Sequence parameter set data syntax
  8020. PROFILES_WITH_OPTIONAL_SPS_DATA = {
  8021. 100: true,
  8022. 110: true,
  8023. 122: true,
  8024. 244: true,
  8025. 44: true,
  8026. 83: true,
  8027. 86: true,
  8028. 118: true,
  8029. 128: true,
  8030. // TODO: the three profiles below don't
  8031. // appear to have sps data in the specificiation anymore?
  8032. 138: true,
  8033. 139: true,
  8034. 134: true
  8035. };
  8036. /**
  8037. * Accepts input from a ElementaryStream and produces H.264 NAL unit data
  8038. * events.
  8039. */
  8040. H264Stream$1 = function () {
  8041. var nalByteStream = new NalByteStream(),
  8042. self,
  8043. trackId,
  8044. currentPts,
  8045. currentDts,
  8046. discardEmulationPreventionBytes,
  8047. readSequenceParameterSet,
  8048. skipScalingList;
  8049. H264Stream$1.prototype.init.call(this);
  8050. self = this;
  8051. /*
  8052. * Pushes a packet from a stream onto the NalByteStream
  8053. *
  8054. * @param {Object} packet - A packet received from a stream
  8055. * @param {Uint8Array} packet.data - The raw bytes of the packet
  8056. * @param {Number} packet.dts - Decode timestamp of the packet
  8057. * @param {Number} packet.pts - Presentation timestamp of the packet
  8058. * @param {Number} packet.trackId - The id of the h264 track this packet came from
  8059. * @param {('video'|'audio')} packet.type - The type of packet
  8060. *
  8061. */
  8062. this.push = function (packet) {
  8063. if (packet.type !== 'video') {
  8064. return;
  8065. }
  8066. trackId = packet.trackId;
  8067. currentPts = packet.pts;
  8068. currentDts = packet.dts;
  8069. nalByteStream.push(packet);
  8070. };
  8071. /*
  8072. * Identify NAL unit types and pass on the NALU, trackId, presentation and decode timestamps
  8073. * for the NALUs to the next stream component.
  8074. * Also, preprocess caption and sequence parameter NALUs.
  8075. *
  8076. * @param {Uint8Array} data - A NAL unit identified by `NalByteStream.push`
  8077. * @see NalByteStream.push
  8078. */
  8079. nalByteStream.on('data', function (data) {
  8080. var event = {
  8081. trackId: trackId,
  8082. pts: currentPts,
  8083. dts: currentDts,
  8084. data: data,
  8085. nalUnitTypeCode: data[0] & 0x1f
  8086. };
  8087. switch (event.nalUnitTypeCode) {
  8088. case 0x05:
  8089. event.nalUnitType = 'slice_layer_without_partitioning_rbsp_idr';
  8090. break;
  8091. case 0x06:
  8092. event.nalUnitType = 'sei_rbsp';
  8093. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  8094. break;
  8095. case 0x07:
  8096. event.nalUnitType = 'seq_parameter_set_rbsp';
  8097. event.escapedRBSP = discardEmulationPreventionBytes(data.subarray(1));
  8098. event.config = readSequenceParameterSet(event.escapedRBSP);
  8099. break;
  8100. case 0x08:
  8101. event.nalUnitType = 'pic_parameter_set_rbsp';
  8102. break;
  8103. case 0x09:
  8104. event.nalUnitType = 'access_unit_delimiter_rbsp';
  8105. break;
  8106. } // This triggers data on the H264Stream
  8107. self.trigger('data', event);
  8108. });
  8109. nalByteStream.on('done', function () {
  8110. self.trigger('done');
  8111. });
  8112. nalByteStream.on('partialdone', function () {
  8113. self.trigger('partialdone');
  8114. });
  8115. nalByteStream.on('reset', function () {
  8116. self.trigger('reset');
  8117. });
  8118. nalByteStream.on('endedtimeline', function () {
  8119. self.trigger('endedtimeline');
  8120. });
  8121. this.flush = function () {
  8122. nalByteStream.flush();
  8123. };
  8124. this.partialFlush = function () {
  8125. nalByteStream.partialFlush();
  8126. };
  8127. this.reset = function () {
  8128. nalByteStream.reset();
  8129. };
  8130. this.endTimeline = function () {
  8131. nalByteStream.endTimeline();
  8132. };
  8133. /**
  8134. * Advance the ExpGolomb decoder past a scaling list. The scaling
  8135. * list is optionally transmitted as part of a sequence parameter
  8136. * set and is not relevant to transmuxing.
  8137. * @param count {number} the number of entries in this scaling list
  8138. * @param expGolombDecoder {object} an ExpGolomb pointed to the
  8139. * start of a scaling list
  8140. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  8141. */
  8142. skipScalingList = function (count, expGolombDecoder) {
  8143. var lastScale = 8,
  8144. nextScale = 8,
  8145. j,
  8146. deltaScale;
  8147. for (j = 0; j < count; j++) {
  8148. if (nextScale !== 0) {
  8149. deltaScale = expGolombDecoder.readExpGolomb();
  8150. nextScale = (lastScale + deltaScale + 256) % 256;
  8151. }
  8152. lastScale = nextScale === 0 ? lastScale : nextScale;
  8153. }
  8154. };
  8155. /**
  8156. * Expunge any "Emulation Prevention" bytes from a "Raw Byte
  8157. * Sequence Payload"
  8158. * @param data {Uint8Array} the bytes of a RBSP from a NAL
  8159. * unit
  8160. * @return {Uint8Array} the RBSP without any Emulation
  8161. * Prevention Bytes
  8162. */
  8163. discardEmulationPreventionBytes = function (data) {
  8164. var length = data.byteLength,
  8165. emulationPreventionBytesPositions = [],
  8166. i = 1,
  8167. newLength,
  8168. newData; // Find all `Emulation Prevention Bytes`
  8169. while (i < length - 2) {
  8170. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  8171. emulationPreventionBytesPositions.push(i + 2);
  8172. i += 2;
  8173. } else {
  8174. i++;
  8175. }
  8176. } // If no Emulation Prevention Bytes were found just return the original
  8177. // array
  8178. if (emulationPreventionBytesPositions.length === 0) {
  8179. return data;
  8180. } // Create a new array to hold the NAL unit data
  8181. newLength = length - emulationPreventionBytesPositions.length;
  8182. newData = new Uint8Array(newLength);
  8183. var sourceIndex = 0;
  8184. for (i = 0; i < newLength; sourceIndex++, i++) {
  8185. if (sourceIndex === emulationPreventionBytesPositions[0]) {
  8186. // Skip this byte
  8187. sourceIndex++; // Remove this position index
  8188. emulationPreventionBytesPositions.shift();
  8189. }
  8190. newData[i] = data[sourceIndex];
  8191. }
  8192. return newData;
  8193. };
  8194. /**
  8195. * Read a sequence parameter set and return some interesting video
  8196. * properties. A sequence parameter set is the H264 metadata that
  8197. * describes the properties of upcoming video frames.
  8198. * @param data {Uint8Array} the bytes of a sequence parameter set
  8199. * @return {object} an object with configuration parsed from the
  8200. * sequence parameter set, including the dimensions of the
  8201. * associated video frames.
  8202. */
  8203. readSequenceParameterSet = function (data) {
  8204. var frameCropLeftOffset = 0,
  8205. frameCropRightOffset = 0,
  8206. frameCropTopOffset = 0,
  8207. frameCropBottomOffset = 0,
  8208. expGolombDecoder,
  8209. profileIdc,
  8210. levelIdc,
  8211. profileCompatibility,
  8212. chromaFormatIdc,
  8213. picOrderCntType,
  8214. numRefFramesInPicOrderCntCycle,
  8215. picWidthInMbsMinus1,
  8216. picHeightInMapUnitsMinus1,
  8217. frameMbsOnlyFlag,
  8218. scalingListCount,
  8219. sarRatio = [1, 1],
  8220. aspectRatioIdc,
  8221. i;
  8222. expGolombDecoder = new ExpGolomb(data);
  8223. profileIdc = expGolombDecoder.readUnsignedByte(); // profile_idc
  8224. profileCompatibility = expGolombDecoder.readUnsignedByte(); // constraint_set[0-5]_flag
  8225. levelIdc = expGolombDecoder.readUnsignedByte(); // level_idc u(8)
  8226. expGolombDecoder.skipUnsignedExpGolomb(); // seq_parameter_set_id
  8227. // some profiles have more optional data we don't need
  8228. if (PROFILES_WITH_OPTIONAL_SPS_DATA[profileIdc]) {
  8229. chromaFormatIdc = expGolombDecoder.readUnsignedExpGolomb();
  8230. if (chromaFormatIdc === 3) {
  8231. expGolombDecoder.skipBits(1); // separate_colour_plane_flag
  8232. }
  8233. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_luma_minus8
  8234. expGolombDecoder.skipUnsignedExpGolomb(); // bit_depth_chroma_minus8
  8235. expGolombDecoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
  8236. if (expGolombDecoder.readBoolean()) {
  8237. // seq_scaling_matrix_present_flag
  8238. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  8239. for (i = 0; i < scalingListCount; i++) {
  8240. if (expGolombDecoder.readBoolean()) {
  8241. // seq_scaling_list_present_flag[ i ]
  8242. if (i < 6) {
  8243. skipScalingList(16, expGolombDecoder);
  8244. } else {
  8245. skipScalingList(64, expGolombDecoder);
  8246. }
  8247. }
  8248. }
  8249. }
  8250. }
  8251. expGolombDecoder.skipUnsignedExpGolomb(); // log2_max_frame_num_minus4
  8252. picOrderCntType = expGolombDecoder.readUnsignedExpGolomb();
  8253. if (picOrderCntType === 0) {
  8254. expGolombDecoder.readUnsignedExpGolomb(); // log2_max_pic_order_cnt_lsb_minus4
  8255. } else if (picOrderCntType === 1) {
  8256. expGolombDecoder.skipBits(1); // delta_pic_order_always_zero_flag
  8257. expGolombDecoder.skipExpGolomb(); // offset_for_non_ref_pic
  8258. expGolombDecoder.skipExpGolomb(); // offset_for_top_to_bottom_field
  8259. numRefFramesInPicOrderCntCycle = expGolombDecoder.readUnsignedExpGolomb();
  8260. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  8261. expGolombDecoder.skipExpGolomb(); // offset_for_ref_frame[ i ]
  8262. }
  8263. }
  8264. expGolombDecoder.skipUnsignedExpGolomb(); // max_num_ref_frames
  8265. expGolombDecoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
  8266. picWidthInMbsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  8267. picHeightInMapUnitsMinus1 = expGolombDecoder.readUnsignedExpGolomb();
  8268. frameMbsOnlyFlag = expGolombDecoder.readBits(1);
  8269. if (frameMbsOnlyFlag === 0) {
  8270. expGolombDecoder.skipBits(1); // mb_adaptive_frame_field_flag
  8271. }
  8272. expGolombDecoder.skipBits(1); // direct_8x8_inference_flag
  8273. if (expGolombDecoder.readBoolean()) {
  8274. // frame_cropping_flag
  8275. frameCropLeftOffset = expGolombDecoder.readUnsignedExpGolomb();
  8276. frameCropRightOffset = expGolombDecoder.readUnsignedExpGolomb();
  8277. frameCropTopOffset = expGolombDecoder.readUnsignedExpGolomb();
  8278. frameCropBottomOffset = expGolombDecoder.readUnsignedExpGolomb();
  8279. }
  8280. if (expGolombDecoder.readBoolean()) {
  8281. // vui_parameters_present_flag
  8282. if (expGolombDecoder.readBoolean()) {
  8283. // aspect_ratio_info_present_flag
  8284. aspectRatioIdc = expGolombDecoder.readUnsignedByte();
  8285. switch (aspectRatioIdc) {
  8286. case 1:
  8287. sarRatio = [1, 1];
  8288. break;
  8289. case 2:
  8290. sarRatio = [12, 11];
  8291. break;
  8292. case 3:
  8293. sarRatio = [10, 11];
  8294. break;
  8295. case 4:
  8296. sarRatio = [16, 11];
  8297. break;
  8298. case 5:
  8299. sarRatio = [40, 33];
  8300. break;
  8301. case 6:
  8302. sarRatio = [24, 11];
  8303. break;
  8304. case 7:
  8305. sarRatio = [20, 11];
  8306. break;
  8307. case 8:
  8308. sarRatio = [32, 11];
  8309. break;
  8310. case 9:
  8311. sarRatio = [80, 33];
  8312. break;
  8313. case 10:
  8314. sarRatio = [18, 11];
  8315. break;
  8316. case 11:
  8317. sarRatio = [15, 11];
  8318. break;
  8319. case 12:
  8320. sarRatio = [64, 33];
  8321. break;
  8322. case 13:
  8323. sarRatio = [160, 99];
  8324. break;
  8325. case 14:
  8326. sarRatio = [4, 3];
  8327. break;
  8328. case 15:
  8329. sarRatio = [3, 2];
  8330. break;
  8331. case 16:
  8332. sarRatio = [2, 1];
  8333. break;
  8334. case 255:
  8335. {
  8336. sarRatio = [expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte(), expGolombDecoder.readUnsignedByte() << 8 | expGolombDecoder.readUnsignedByte()];
  8337. break;
  8338. }
  8339. }
  8340. if (sarRatio) {
  8341. sarRatio[0] / sarRatio[1];
  8342. }
  8343. }
  8344. }
  8345. return {
  8346. profileIdc: profileIdc,
  8347. levelIdc: levelIdc,
  8348. profileCompatibility: profileCompatibility,
  8349. width: (picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2,
  8350. height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - frameCropTopOffset * 2 - frameCropBottomOffset * 2,
  8351. // sar is sample aspect ratio
  8352. sarRatio: sarRatio
  8353. };
  8354. };
  8355. };
  8356. H264Stream$1.prototype = new Stream$2();
  8357. var h264 = {
  8358. H264Stream: H264Stream$1,
  8359. NalByteStream: NalByteStream
  8360. };
  8361. /**
  8362. * mux.js
  8363. *
  8364. * Copyright (c) Brightcove
  8365. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  8366. *
  8367. * Utilities to detect basic properties and metadata about Aac data.
  8368. */
  8369. var ADTS_SAMPLING_FREQUENCIES = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  8370. var parseId3TagSize = function (header, byteIndex) {
  8371. var returnSize = header[byteIndex + 6] << 21 | header[byteIndex + 7] << 14 | header[byteIndex + 8] << 7 | header[byteIndex + 9],
  8372. flags = header[byteIndex + 5],
  8373. footerPresent = (flags & 16) >> 4; // if we get a negative returnSize clamp it to 0
  8374. returnSize = returnSize >= 0 ? returnSize : 0;
  8375. if (footerPresent) {
  8376. return returnSize + 20;
  8377. }
  8378. return returnSize + 10;
  8379. };
  8380. var getId3Offset = function (data, offset) {
  8381. if (data.length - offset < 10 || data[offset] !== 'I'.charCodeAt(0) || data[offset + 1] !== 'D'.charCodeAt(0) || data[offset + 2] !== '3'.charCodeAt(0)) {
  8382. return offset;
  8383. }
  8384. offset += parseId3TagSize(data, offset);
  8385. return getId3Offset(data, offset);
  8386. }; // TODO: use vhs-utils
  8387. var isLikelyAacData$1 = function (data) {
  8388. var offset = getId3Offset(data, 0);
  8389. return data.length >= offset + 2 && (data[offset] & 0xFF) === 0xFF && (data[offset + 1] & 0xF0) === 0xF0 && // verify that the 2 layer bits are 0, aka this
  8390. // is not mp3 data but aac data.
  8391. (data[offset + 1] & 0x16) === 0x10;
  8392. };
  8393. var parseSyncSafeInteger = function (data) {
  8394. return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3];
  8395. }; // return a percent-encoded representation of the specified byte range
  8396. // @see http://en.wikipedia.org/wiki/Percent-encoding
  8397. var percentEncode = function (bytes, start, end) {
  8398. var i,
  8399. result = '';
  8400. for (i = start; i < end; i++) {
  8401. result += '%' + ('00' + bytes[i].toString(16)).slice(-2);
  8402. }
  8403. return result;
  8404. }; // return the string representation of the specified byte range,
  8405. // interpreted as ISO-8859-1.
  8406. var parseIso88591 = function (bytes, start, end) {
  8407. return unescape(percentEncode(bytes, start, end)); // jshint ignore:line
  8408. };
  8409. var parseAdtsSize = function (header, byteIndex) {
  8410. var lowThree = (header[byteIndex + 5] & 0xE0) >> 5,
  8411. middle = header[byteIndex + 4] << 3,
  8412. highTwo = header[byteIndex + 3] & 0x3 << 11;
  8413. return highTwo | middle | lowThree;
  8414. };
  8415. var parseType$4 = function (header, byteIndex) {
  8416. if (header[byteIndex] === 'I'.charCodeAt(0) && header[byteIndex + 1] === 'D'.charCodeAt(0) && header[byteIndex + 2] === '3'.charCodeAt(0)) {
  8417. return 'timed-metadata';
  8418. } else if (header[byteIndex] & 0xff === 0xff && (header[byteIndex + 1] & 0xf0) === 0xf0) {
  8419. return 'audio';
  8420. }
  8421. return null;
  8422. };
  8423. var parseSampleRate = function (packet) {
  8424. var i = 0;
  8425. while (i + 5 < packet.length) {
  8426. if (packet[i] !== 0xFF || (packet[i + 1] & 0xF6) !== 0xF0) {
  8427. // If a valid header was not found, jump one forward and attempt to
  8428. // find a valid ADTS header starting at the next byte
  8429. i++;
  8430. continue;
  8431. }
  8432. return ADTS_SAMPLING_FREQUENCIES[(packet[i + 2] & 0x3c) >>> 2];
  8433. }
  8434. return null;
  8435. };
  8436. var parseAacTimestamp = function (packet) {
  8437. var frameStart, frameSize, frame, frameHeader; // find the start of the first frame and the end of the tag
  8438. frameStart = 10;
  8439. if (packet[5] & 0x40) {
  8440. // advance the frame start past the extended header
  8441. frameStart += 4; // header size field
  8442. frameStart += parseSyncSafeInteger(packet.subarray(10, 14));
  8443. } // parse one or more ID3 frames
  8444. // http://id3.org/id3v2.3.0#ID3v2_frame_overview
  8445. do {
  8446. // determine the number of bytes in this frame
  8447. frameSize = parseSyncSafeInteger(packet.subarray(frameStart + 4, frameStart + 8));
  8448. if (frameSize < 1) {
  8449. return null;
  8450. }
  8451. frameHeader = String.fromCharCode(packet[frameStart], packet[frameStart + 1], packet[frameStart + 2], packet[frameStart + 3]);
  8452. if (frameHeader === 'PRIV') {
  8453. frame = packet.subarray(frameStart + 10, frameStart + frameSize + 10);
  8454. for (var i = 0; i < frame.byteLength; i++) {
  8455. if (frame[i] === 0) {
  8456. var owner = parseIso88591(frame, 0, i);
  8457. if (owner === 'com.apple.streaming.transportStreamTimestamp') {
  8458. var d = frame.subarray(i + 1);
  8459. var size = (d[3] & 0x01) << 30 | d[4] << 22 | d[5] << 14 | d[6] << 6 | d[7] >>> 2;
  8460. size *= 4;
  8461. size += d[7] & 0x03;
  8462. return size;
  8463. }
  8464. break;
  8465. }
  8466. }
  8467. }
  8468. frameStart += 10; // advance past the frame header
  8469. frameStart += frameSize; // advance past the frame body
  8470. } while (frameStart < packet.byteLength);
  8471. return null;
  8472. };
  8473. var utils = {
  8474. isLikelyAacData: isLikelyAacData$1,
  8475. parseId3TagSize: parseId3TagSize,
  8476. parseAdtsSize: parseAdtsSize,
  8477. parseType: parseType$4,
  8478. parseSampleRate: parseSampleRate,
  8479. parseAacTimestamp: parseAacTimestamp
  8480. };
  8481. /**
  8482. * mux.js
  8483. *
  8484. * Copyright (c) Brightcove
  8485. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  8486. *
  8487. * A stream-based aac to mp4 converter. This utility can be used to
  8488. * deliver mp4s to a SourceBuffer on platforms that support native
  8489. * Media Source Extensions.
  8490. */
  8491. var Stream$1 = stream;
  8492. var aacUtils = utils; // Constants
  8493. var AacStream$1;
  8494. /**
  8495. * Splits an incoming stream of binary data into ADTS and ID3 Frames.
  8496. */
  8497. AacStream$1 = function () {
  8498. var everything = new Uint8Array(),
  8499. timeStamp = 0;
  8500. AacStream$1.prototype.init.call(this);
  8501. this.setTimestamp = function (timestamp) {
  8502. timeStamp = timestamp;
  8503. };
  8504. this.push = function (bytes) {
  8505. var frameSize = 0,
  8506. byteIndex = 0,
  8507. bytesLeft,
  8508. chunk,
  8509. packet,
  8510. tempLength; // If there are bytes remaining from the last segment, prepend them to the
  8511. // bytes that were pushed in
  8512. if (everything.length) {
  8513. tempLength = everything.length;
  8514. everything = new Uint8Array(bytes.byteLength + tempLength);
  8515. everything.set(everything.subarray(0, tempLength));
  8516. everything.set(bytes, tempLength);
  8517. } else {
  8518. everything = bytes;
  8519. }
  8520. while (everything.length - byteIndex >= 3) {
  8521. if (everything[byteIndex] === 'I'.charCodeAt(0) && everything[byteIndex + 1] === 'D'.charCodeAt(0) && everything[byteIndex + 2] === '3'.charCodeAt(0)) {
  8522. // Exit early because we don't have enough to parse
  8523. // the ID3 tag header
  8524. if (everything.length - byteIndex < 10) {
  8525. break;
  8526. } // check framesize
  8527. frameSize = aacUtils.parseId3TagSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  8528. // to emit a full packet
  8529. // Add to byteIndex to support multiple ID3 tags in sequence
  8530. if (byteIndex + frameSize > everything.length) {
  8531. break;
  8532. }
  8533. chunk = {
  8534. type: 'timed-metadata',
  8535. data: everything.subarray(byteIndex, byteIndex + frameSize)
  8536. };
  8537. this.trigger('data', chunk);
  8538. byteIndex += frameSize;
  8539. continue;
  8540. } else if ((everything[byteIndex] & 0xff) === 0xff && (everything[byteIndex + 1] & 0xf0) === 0xf0) {
  8541. // Exit early because we don't have enough to parse
  8542. // the ADTS frame header
  8543. if (everything.length - byteIndex < 7) {
  8544. break;
  8545. }
  8546. frameSize = aacUtils.parseAdtsSize(everything, byteIndex); // Exit early if we don't have enough in the buffer
  8547. // to emit a full packet
  8548. if (byteIndex + frameSize > everything.length) {
  8549. break;
  8550. }
  8551. packet = {
  8552. type: 'audio',
  8553. data: everything.subarray(byteIndex, byteIndex + frameSize),
  8554. pts: timeStamp,
  8555. dts: timeStamp
  8556. };
  8557. this.trigger('data', packet);
  8558. byteIndex += frameSize;
  8559. continue;
  8560. }
  8561. byteIndex++;
  8562. }
  8563. bytesLeft = everything.length - byteIndex;
  8564. if (bytesLeft > 0) {
  8565. everything = everything.subarray(byteIndex);
  8566. } else {
  8567. everything = new Uint8Array();
  8568. }
  8569. };
  8570. this.reset = function () {
  8571. everything = new Uint8Array();
  8572. this.trigger('reset');
  8573. };
  8574. this.endTimeline = function () {
  8575. everything = new Uint8Array();
  8576. this.trigger('endedtimeline');
  8577. };
  8578. };
  8579. AacStream$1.prototype = new Stream$1();
  8580. var aac = AacStream$1;
  8581. var AUDIO_PROPERTIES$1 = ['audioobjecttype', 'channelcount', 'samplerate', 'samplingfrequencyindex', 'samplesize'];
  8582. var audioProperties = AUDIO_PROPERTIES$1;
  8583. var VIDEO_PROPERTIES$1 = ['width', 'height', 'profileIdc', 'levelIdc', 'profileCompatibility', 'sarRatio'];
  8584. var videoProperties = VIDEO_PROPERTIES$1;
  8585. /**
  8586. * mux.js
  8587. *
  8588. * Copyright (c) Brightcove
  8589. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  8590. *
  8591. * A stream-based mp2t to mp4 converter. This utility can be used to
  8592. * deliver mp4s to a SourceBuffer on platforms that support native
  8593. * Media Source Extensions.
  8594. */
  8595. var Stream = stream;
  8596. var mp4 = mp4Generator;
  8597. var frameUtils = frameUtils$1;
  8598. var audioFrameUtils = audioFrameUtils$1;
  8599. var trackDecodeInfo = trackDecodeInfo$1;
  8600. var m2ts = m2ts_1;
  8601. var clock = clock$2;
  8602. var AdtsStream = adts;
  8603. var H264Stream = h264.H264Stream;
  8604. var AacStream = aac;
  8605. var isLikelyAacData = utils.isLikelyAacData;
  8606. var ONE_SECOND_IN_TS$1 = clock$2.ONE_SECOND_IN_TS;
  8607. var AUDIO_PROPERTIES = audioProperties;
  8608. var VIDEO_PROPERTIES = videoProperties; // object types
  8609. var VideoSegmentStream, AudioSegmentStream, Transmuxer, CoalesceStream;
  8610. var retriggerForStream = function (key, event) {
  8611. event.stream = key;
  8612. this.trigger('log', event);
  8613. };
  8614. var addPipelineLogRetriggers = function (transmuxer, pipeline) {
  8615. var keys = Object.keys(pipeline);
  8616. for (var i = 0; i < keys.length; i++) {
  8617. var key = keys[i]; // skip non-stream keys and headOfPipeline
  8618. // which is just a duplicate
  8619. if (key === 'headOfPipeline' || !pipeline[key].on) {
  8620. continue;
  8621. }
  8622. pipeline[key].on('log', retriggerForStream.bind(transmuxer, key));
  8623. }
  8624. };
  8625. /**
  8626. * Compare two arrays (even typed) for same-ness
  8627. */
  8628. var arrayEquals = function (a, b) {
  8629. var i;
  8630. if (a.length !== b.length) {
  8631. return false;
  8632. } // compare the value of each element in the array
  8633. for (i = 0; i < a.length; i++) {
  8634. if (a[i] !== b[i]) {
  8635. return false;
  8636. }
  8637. }
  8638. return true;
  8639. };
  8640. var generateSegmentTimingInfo = function (baseMediaDecodeTime, startDts, startPts, endDts, endPts, prependedContentDuration) {
  8641. var ptsOffsetFromDts = startPts - startDts,
  8642. decodeDuration = endDts - startDts,
  8643. presentationDuration = endPts - startPts; // The PTS and DTS values are based on the actual stream times from the segment,
  8644. // however, the player time values will reflect a start from the baseMediaDecodeTime.
  8645. // In order to provide relevant values for the player times, base timing info on the
  8646. // baseMediaDecodeTime and the DTS and PTS durations of the segment.
  8647. return {
  8648. start: {
  8649. dts: baseMediaDecodeTime,
  8650. pts: baseMediaDecodeTime + ptsOffsetFromDts
  8651. },
  8652. end: {
  8653. dts: baseMediaDecodeTime + decodeDuration,
  8654. pts: baseMediaDecodeTime + presentationDuration
  8655. },
  8656. prependedContentDuration: prependedContentDuration,
  8657. baseMediaDecodeTime: baseMediaDecodeTime
  8658. };
  8659. };
  8660. /**
  8661. * Constructs a single-track, ISO BMFF media segment from AAC data
  8662. * events. The output of this stream can be fed to a SourceBuffer
  8663. * configured with a suitable initialization segment.
  8664. * @param track {object} track metadata configuration
  8665. * @param options {object} transmuxer options object
  8666. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  8667. * in the source; false to adjust the first segment to start at 0.
  8668. */
  8669. AudioSegmentStream = function (track, options) {
  8670. var adtsFrames = [],
  8671. sequenceNumber,
  8672. earliestAllowedDts = 0,
  8673. audioAppendStartTs = 0,
  8674. videoBaseMediaDecodeTime = Infinity;
  8675. options = options || {};
  8676. sequenceNumber = options.firstSequenceNumber || 0;
  8677. AudioSegmentStream.prototype.init.call(this);
  8678. this.push = function (data) {
  8679. trackDecodeInfo.collectDtsInfo(track, data);
  8680. if (track) {
  8681. AUDIO_PROPERTIES.forEach(function (prop) {
  8682. track[prop] = data[prop];
  8683. });
  8684. } // buffer audio data until end() is called
  8685. adtsFrames.push(data);
  8686. };
  8687. this.setEarliestDts = function (earliestDts) {
  8688. earliestAllowedDts = earliestDts;
  8689. };
  8690. this.setVideoBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  8691. videoBaseMediaDecodeTime = baseMediaDecodeTime;
  8692. };
  8693. this.setAudioAppendStart = function (timestamp) {
  8694. audioAppendStartTs = timestamp;
  8695. };
  8696. this.flush = function () {
  8697. var frames, moof, mdat, boxes, frameDuration, segmentDuration, videoClockCyclesOfSilencePrefixed; // return early if no audio data has been observed
  8698. if (adtsFrames.length === 0) {
  8699. this.trigger('done', 'AudioSegmentStream');
  8700. return;
  8701. }
  8702. frames = audioFrameUtils.trimAdtsFramesByEarliestDts(adtsFrames, track, earliestAllowedDts);
  8703. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps); // amount of audio filled but the value is in video clock rather than audio clock
  8704. videoClockCyclesOfSilencePrefixed = audioFrameUtils.prefixWithSilence(track, frames, audioAppendStartTs, videoBaseMediaDecodeTime); // we have to build the index from byte locations to
  8705. // samples (that is, adts frames) in the audio data
  8706. track.samples = audioFrameUtils.generateSampleTable(frames); // concatenate the audio data to constuct the mdat
  8707. mdat = mp4.mdat(audioFrameUtils.concatenateFrameData(frames));
  8708. adtsFrames = [];
  8709. moof = mp4.moof(sequenceNumber, [track]);
  8710. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // bump the sequence number for next time
  8711. sequenceNumber++;
  8712. boxes.set(moof);
  8713. boxes.set(mdat, moof.byteLength);
  8714. trackDecodeInfo.clearDtsInfo(track);
  8715. frameDuration = Math.ceil(ONE_SECOND_IN_TS$1 * 1024 / track.samplerate); // TODO this check was added to maintain backwards compatibility (particularly with
  8716. // tests) on adding the timingInfo event. However, it seems unlikely that there's a
  8717. // valid use-case where an init segment/data should be triggered without associated
  8718. // frames. Leaving for now, but should be looked into.
  8719. if (frames.length) {
  8720. segmentDuration = frames.length * frameDuration;
  8721. this.trigger('segmentTimingInfo', generateSegmentTimingInfo( // The audio track's baseMediaDecodeTime is in audio clock cycles, but the
  8722. // frame info is in video clock cycles. Convert to match expectation of
  8723. // listeners (that all timestamps will be based on video clock cycles).
  8724. clock.audioTsToVideoTs(track.baseMediaDecodeTime, track.samplerate), // frame times are already in video clock, as is segment duration
  8725. frames[0].dts, frames[0].pts, frames[0].dts + segmentDuration, frames[0].pts + segmentDuration, videoClockCyclesOfSilencePrefixed || 0));
  8726. this.trigger('timingInfo', {
  8727. start: frames[0].pts,
  8728. end: frames[0].pts + segmentDuration
  8729. });
  8730. }
  8731. this.trigger('data', {
  8732. track: track,
  8733. boxes: boxes
  8734. });
  8735. this.trigger('done', 'AudioSegmentStream');
  8736. };
  8737. this.reset = function () {
  8738. trackDecodeInfo.clearDtsInfo(track);
  8739. adtsFrames = [];
  8740. this.trigger('reset');
  8741. };
  8742. };
  8743. AudioSegmentStream.prototype = new Stream();
  8744. /**
  8745. * Constructs a single-track, ISO BMFF media segment from H264 data
  8746. * events. The output of this stream can be fed to a SourceBuffer
  8747. * configured with a suitable initialization segment.
  8748. * @param track {object} track metadata configuration
  8749. * @param options {object} transmuxer options object
  8750. * @param options.alignGopsAtEnd {boolean} If true, start from the end of the
  8751. * gopsToAlignWith list when attempting to align gop pts
  8752. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  8753. * in the source; false to adjust the first segment to start at 0.
  8754. */
  8755. VideoSegmentStream = function (track, options) {
  8756. var sequenceNumber,
  8757. nalUnits = [],
  8758. gopsToAlignWith = [],
  8759. config,
  8760. pps;
  8761. options = options || {};
  8762. sequenceNumber = options.firstSequenceNumber || 0;
  8763. VideoSegmentStream.prototype.init.call(this);
  8764. delete track.minPTS;
  8765. this.gopCache_ = [];
  8766. /**
  8767. * Constructs a ISO BMFF segment given H264 nalUnits
  8768. * @param {Object} nalUnit A data event representing a nalUnit
  8769. * @param {String} nalUnit.nalUnitType
  8770. * @param {Object} nalUnit.config Properties for a mp4 track
  8771. * @param {Uint8Array} nalUnit.data The nalUnit bytes
  8772. * @see lib/codecs/h264.js
  8773. **/
  8774. this.push = function (nalUnit) {
  8775. trackDecodeInfo.collectDtsInfo(track, nalUnit); // record the track config
  8776. if (nalUnit.nalUnitType === 'seq_parameter_set_rbsp' && !config) {
  8777. config = nalUnit.config;
  8778. track.sps = [nalUnit.data];
  8779. VIDEO_PROPERTIES.forEach(function (prop) {
  8780. track[prop] = config[prop];
  8781. }, this);
  8782. }
  8783. if (nalUnit.nalUnitType === 'pic_parameter_set_rbsp' && !pps) {
  8784. pps = nalUnit.data;
  8785. track.pps = [nalUnit.data];
  8786. } // buffer video until flush() is called
  8787. nalUnits.push(nalUnit);
  8788. };
  8789. /**
  8790. * Pass constructed ISO BMFF track and boxes on to the
  8791. * next stream in the pipeline
  8792. **/
  8793. this.flush = function () {
  8794. var frames,
  8795. gopForFusion,
  8796. gops,
  8797. moof,
  8798. mdat,
  8799. boxes,
  8800. prependedContentDuration = 0,
  8801. firstGop,
  8802. lastGop; // Throw away nalUnits at the start of the byte stream until
  8803. // we find the first AUD
  8804. while (nalUnits.length) {
  8805. if (nalUnits[0].nalUnitType === 'access_unit_delimiter_rbsp') {
  8806. break;
  8807. }
  8808. nalUnits.shift();
  8809. } // Return early if no video data has been observed
  8810. if (nalUnits.length === 0) {
  8811. this.resetStream_();
  8812. this.trigger('done', 'VideoSegmentStream');
  8813. return;
  8814. } // Organize the raw nal-units into arrays that represent
  8815. // higher-level constructs such as frames and gops
  8816. // (group-of-pictures)
  8817. frames = frameUtils.groupNalsIntoFrames(nalUnits);
  8818. gops = frameUtils.groupFramesIntoGops(frames); // If the first frame of this fragment is not a keyframe we have
  8819. // a problem since MSE (on Chrome) requires a leading keyframe.
  8820. //
  8821. // We have two approaches to repairing this situation:
  8822. // 1) GOP-FUSION:
  8823. // This is where we keep track of the GOPS (group-of-pictures)
  8824. // from previous fragments and attempt to find one that we can
  8825. // prepend to the current fragment in order to create a valid
  8826. // fragment.
  8827. // 2) KEYFRAME-PULLING:
  8828. // Here we search for the first keyframe in the fragment and
  8829. // throw away all the frames between the start of the fragment
  8830. // and that keyframe. We then extend the duration and pull the
  8831. // PTS of the keyframe forward so that it covers the time range
  8832. // of the frames that were disposed of.
  8833. //
  8834. // #1 is far prefereable over #2 which can cause "stuttering" but
  8835. // requires more things to be just right.
  8836. if (!gops[0][0].keyFrame) {
  8837. // Search for a gop for fusion from our gopCache
  8838. gopForFusion = this.getGopForFusion_(nalUnits[0], track);
  8839. if (gopForFusion) {
  8840. // in order to provide more accurate timing information about the segment, save
  8841. // the number of seconds prepended to the original segment due to GOP fusion
  8842. prependedContentDuration = gopForFusion.duration;
  8843. gops.unshift(gopForFusion); // Adjust Gops' metadata to account for the inclusion of the
  8844. // new gop at the beginning
  8845. gops.byteLength += gopForFusion.byteLength;
  8846. gops.nalCount += gopForFusion.nalCount;
  8847. gops.pts = gopForFusion.pts;
  8848. gops.dts = gopForFusion.dts;
  8849. gops.duration += gopForFusion.duration;
  8850. } else {
  8851. // If we didn't find a candidate gop fall back to keyframe-pulling
  8852. gops = frameUtils.extendFirstKeyFrame(gops);
  8853. }
  8854. } // Trim gops to align with gopsToAlignWith
  8855. if (gopsToAlignWith.length) {
  8856. var alignedGops;
  8857. if (options.alignGopsAtEnd) {
  8858. alignedGops = this.alignGopsAtEnd_(gops);
  8859. } else {
  8860. alignedGops = this.alignGopsAtStart_(gops);
  8861. }
  8862. if (!alignedGops) {
  8863. // save all the nals in the last GOP into the gop cache
  8864. this.gopCache_.unshift({
  8865. gop: gops.pop(),
  8866. pps: track.pps,
  8867. sps: track.sps
  8868. }); // Keep a maximum of 6 GOPs in the cache
  8869. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  8870. nalUnits = []; // return early no gops can be aligned with desired gopsToAlignWith
  8871. this.resetStream_();
  8872. this.trigger('done', 'VideoSegmentStream');
  8873. return;
  8874. } // Some gops were trimmed. clear dts info so minSegmentDts and pts are correct
  8875. // when recalculated before sending off to CoalesceStream
  8876. trackDecodeInfo.clearDtsInfo(track);
  8877. gops = alignedGops;
  8878. }
  8879. trackDecodeInfo.collectDtsInfo(track, gops); // First, we have to build the index from byte locations to
  8880. // samples (that is, frames) in the video data
  8881. track.samples = frameUtils.generateSampleTable(gops); // Concatenate the video data and construct the mdat
  8882. mdat = mp4.mdat(frameUtils.concatenateNalData(gops));
  8883. track.baseMediaDecodeTime = trackDecodeInfo.calculateTrackBaseMediaDecodeTime(track, options.keepOriginalTimestamps);
  8884. this.trigger('processedGopsInfo', gops.map(function (gop) {
  8885. return {
  8886. pts: gop.pts,
  8887. dts: gop.dts,
  8888. byteLength: gop.byteLength
  8889. };
  8890. }));
  8891. firstGop = gops[0];
  8892. lastGop = gops[gops.length - 1];
  8893. this.trigger('segmentTimingInfo', generateSegmentTimingInfo(track.baseMediaDecodeTime, firstGop.dts, firstGop.pts, lastGop.dts + lastGop.duration, lastGop.pts + lastGop.duration, prependedContentDuration));
  8894. this.trigger('timingInfo', {
  8895. start: gops[0].pts,
  8896. end: gops[gops.length - 1].pts + gops[gops.length - 1].duration
  8897. }); // save all the nals in the last GOP into the gop cache
  8898. this.gopCache_.unshift({
  8899. gop: gops.pop(),
  8900. pps: track.pps,
  8901. sps: track.sps
  8902. }); // Keep a maximum of 6 GOPs in the cache
  8903. this.gopCache_.length = Math.min(6, this.gopCache_.length); // Clear nalUnits
  8904. nalUnits = [];
  8905. this.trigger('baseMediaDecodeTime', track.baseMediaDecodeTime);
  8906. this.trigger('timelineStartInfo', track.timelineStartInfo);
  8907. moof = mp4.moof(sequenceNumber, [track]); // it would be great to allocate this array up front instead of
  8908. // throwing away hundreds of media segment fragments
  8909. boxes = new Uint8Array(moof.byteLength + mdat.byteLength); // Bump the sequence number for next time
  8910. sequenceNumber++;
  8911. boxes.set(moof);
  8912. boxes.set(mdat, moof.byteLength);
  8913. this.trigger('data', {
  8914. track: track,
  8915. boxes: boxes
  8916. });
  8917. this.resetStream_(); // Continue with the flush process now
  8918. this.trigger('done', 'VideoSegmentStream');
  8919. };
  8920. this.reset = function () {
  8921. this.resetStream_();
  8922. nalUnits = [];
  8923. this.gopCache_.length = 0;
  8924. gopsToAlignWith.length = 0;
  8925. this.trigger('reset');
  8926. };
  8927. this.resetStream_ = function () {
  8928. trackDecodeInfo.clearDtsInfo(track); // reset config and pps because they may differ across segments
  8929. // for instance, when we are rendition switching
  8930. config = undefined;
  8931. pps = undefined;
  8932. }; // Search for a candidate Gop for gop-fusion from the gop cache and
  8933. // return it or return null if no good candidate was found
  8934. this.getGopForFusion_ = function (nalUnit) {
  8935. var halfSecond = 45000,
  8936. // Half-a-second in a 90khz clock
  8937. allowableOverlap = 10000,
  8938. // About 3 frames @ 30fps
  8939. nearestDistance = Infinity,
  8940. dtsDistance,
  8941. nearestGopObj,
  8942. currentGop,
  8943. currentGopObj,
  8944. i; // Search for the GOP nearest to the beginning of this nal unit
  8945. for (i = 0; i < this.gopCache_.length; i++) {
  8946. currentGopObj = this.gopCache_[i];
  8947. currentGop = currentGopObj.gop; // Reject Gops with different SPS or PPS
  8948. if (!(track.pps && arrayEquals(track.pps[0], currentGopObj.pps[0])) || !(track.sps && arrayEquals(track.sps[0], currentGopObj.sps[0]))) {
  8949. continue;
  8950. } // Reject Gops that would require a negative baseMediaDecodeTime
  8951. if (currentGop.dts < track.timelineStartInfo.dts) {
  8952. continue;
  8953. } // The distance between the end of the gop and the start of the nalUnit
  8954. dtsDistance = nalUnit.dts - currentGop.dts - currentGop.duration; // Only consider GOPS that start before the nal unit and end within
  8955. // a half-second of the nal unit
  8956. if (dtsDistance >= -allowableOverlap && dtsDistance <= halfSecond) {
  8957. // Always use the closest GOP we found if there is more than
  8958. // one candidate
  8959. if (!nearestGopObj || nearestDistance > dtsDistance) {
  8960. nearestGopObj = currentGopObj;
  8961. nearestDistance = dtsDistance;
  8962. }
  8963. }
  8964. }
  8965. if (nearestGopObj) {
  8966. return nearestGopObj.gop;
  8967. }
  8968. return null;
  8969. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  8970. // of gopsToAlignWith starting from the START of the list
  8971. this.alignGopsAtStart_ = function (gops) {
  8972. var alignIndex, gopIndex, align, gop, byteLength, nalCount, duration, alignedGops;
  8973. byteLength = gops.byteLength;
  8974. nalCount = gops.nalCount;
  8975. duration = gops.duration;
  8976. alignIndex = gopIndex = 0;
  8977. while (alignIndex < gopsToAlignWith.length && gopIndex < gops.length) {
  8978. align = gopsToAlignWith[alignIndex];
  8979. gop = gops[gopIndex];
  8980. if (align.pts === gop.pts) {
  8981. break;
  8982. }
  8983. if (gop.pts > align.pts) {
  8984. // this current gop starts after the current gop we want to align on, so increment
  8985. // align index
  8986. alignIndex++;
  8987. continue;
  8988. } // current gop starts before the current gop we want to align on. so increment gop
  8989. // index
  8990. gopIndex++;
  8991. byteLength -= gop.byteLength;
  8992. nalCount -= gop.nalCount;
  8993. duration -= gop.duration;
  8994. }
  8995. if (gopIndex === 0) {
  8996. // no gops to trim
  8997. return gops;
  8998. }
  8999. if (gopIndex === gops.length) {
  9000. // all gops trimmed, skip appending all gops
  9001. return null;
  9002. }
  9003. alignedGops = gops.slice(gopIndex);
  9004. alignedGops.byteLength = byteLength;
  9005. alignedGops.duration = duration;
  9006. alignedGops.nalCount = nalCount;
  9007. alignedGops.pts = alignedGops[0].pts;
  9008. alignedGops.dts = alignedGops[0].dts;
  9009. return alignedGops;
  9010. }; // trim gop list to the first gop found that has a matching pts with a gop in the list
  9011. // of gopsToAlignWith starting from the END of the list
  9012. this.alignGopsAtEnd_ = function (gops) {
  9013. var alignIndex, gopIndex, align, gop, alignEndIndex, matchFound;
  9014. alignIndex = gopsToAlignWith.length - 1;
  9015. gopIndex = gops.length - 1;
  9016. alignEndIndex = null;
  9017. matchFound = false;
  9018. while (alignIndex >= 0 && gopIndex >= 0) {
  9019. align = gopsToAlignWith[alignIndex];
  9020. gop = gops[gopIndex];
  9021. if (align.pts === gop.pts) {
  9022. matchFound = true;
  9023. break;
  9024. }
  9025. if (align.pts > gop.pts) {
  9026. alignIndex--;
  9027. continue;
  9028. }
  9029. if (alignIndex === gopsToAlignWith.length - 1) {
  9030. // gop.pts is greater than the last alignment candidate. If no match is found
  9031. // by the end of this loop, we still want to append gops that come after this
  9032. // point
  9033. alignEndIndex = gopIndex;
  9034. }
  9035. gopIndex--;
  9036. }
  9037. if (!matchFound && alignEndIndex === null) {
  9038. return null;
  9039. }
  9040. var trimIndex;
  9041. if (matchFound) {
  9042. trimIndex = gopIndex;
  9043. } else {
  9044. trimIndex = alignEndIndex;
  9045. }
  9046. if (trimIndex === 0) {
  9047. return gops;
  9048. }
  9049. var alignedGops = gops.slice(trimIndex);
  9050. var metadata = alignedGops.reduce(function (total, gop) {
  9051. total.byteLength += gop.byteLength;
  9052. total.duration += gop.duration;
  9053. total.nalCount += gop.nalCount;
  9054. return total;
  9055. }, {
  9056. byteLength: 0,
  9057. duration: 0,
  9058. nalCount: 0
  9059. });
  9060. alignedGops.byteLength = metadata.byteLength;
  9061. alignedGops.duration = metadata.duration;
  9062. alignedGops.nalCount = metadata.nalCount;
  9063. alignedGops.pts = alignedGops[0].pts;
  9064. alignedGops.dts = alignedGops[0].dts;
  9065. return alignedGops;
  9066. };
  9067. this.alignGopsWith = function (newGopsToAlignWith) {
  9068. gopsToAlignWith = newGopsToAlignWith;
  9069. };
  9070. };
  9071. VideoSegmentStream.prototype = new Stream();
  9072. /**
  9073. * A Stream that can combine multiple streams (ie. audio & video)
  9074. * into a single output segment for MSE. Also supports audio-only
  9075. * and video-only streams.
  9076. * @param options {object} transmuxer options object
  9077. * @param options.keepOriginalTimestamps {boolean} If true, keep the timestamps
  9078. * in the source; false to adjust the first segment to start at media timeline start.
  9079. */
  9080. CoalesceStream = function (options, metadataStream) {
  9081. // Number of Tracks per output segment
  9082. // If greater than 1, we combine multiple
  9083. // tracks into a single segment
  9084. this.numberOfTracks = 0;
  9085. this.metadataStream = metadataStream;
  9086. options = options || {};
  9087. if (typeof options.remux !== 'undefined') {
  9088. this.remuxTracks = !!options.remux;
  9089. } else {
  9090. this.remuxTracks = true;
  9091. }
  9092. if (typeof options.keepOriginalTimestamps === 'boolean') {
  9093. this.keepOriginalTimestamps = options.keepOriginalTimestamps;
  9094. } else {
  9095. this.keepOriginalTimestamps = false;
  9096. }
  9097. this.pendingTracks = [];
  9098. this.videoTrack = null;
  9099. this.pendingBoxes = [];
  9100. this.pendingCaptions = [];
  9101. this.pendingMetadata = [];
  9102. this.pendingBytes = 0;
  9103. this.emittedTracks = 0;
  9104. CoalesceStream.prototype.init.call(this); // Take output from multiple
  9105. this.push = function (output) {
  9106. // buffer incoming captions until the associated video segment
  9107. // finishes
  9108. if (output.content || output.text) {
  9109. return this.pendingCaptions.push(output);
  9110. } // buffer incoming id3 tags until the final flush
  9111. if (output.frames) {
  9112. return this.pendingMetadata.push(output);
  9113. } // Add this track to the list of pending tracks and store
  9114. // important information required for the construction of
  9115. // the final segment
  9116. this.pendingTracks.push(output.track);
  9117. this.pendingBytes += output.boxes.byteLength; // TODO: is there an issue for this against chrome?
  9118. // We unshift audio and push video because
  9119. // as of Chrome 75 when switching from
  9120. // one init segment to another if the video
  9121. // mdat does not appear after the audio mdat
  9122. // only audio will play for the duration of our transmux.
  9123. if (output.track.type === 'video') {
  9124. this.videoTrack = output.track;
  9125. this.pendingBoxes.push(output.boxes);
  9126. }
  9127. if (output.track.type === 'audio') {
  9128. this.audioTrack = output.track;
  9129. this.pendingBoxes.unshift(output.boxes);
  9130. }
  9131. };
  9132. };
  9133. CoalesceStream.prototype = new Stream();
  9134. CoalesceStream.prototype.flush = function (flushSource) {
  9135. var offset = 0,
  9136. event = {
  9137. captions: [],
  9138. captionStreams: {},
  9139. metadata: [],
  9140. info: {}
  9141. },
  9142. caption,
  9143. id3,
  9144. initSegment,
  9145. timelineStartPts = 0,
  9146. i;
  9147. if (this.pendingTracks.length < this.numberOfTracks) {
  9148. if (flushSource !== 'VideoSegmentStream' && flushSource !== 'AudioSegmentStream') {
  9149. // Return because we haven't received a flush from a data-generating
  9150. // portion of the segment (meaning that we have only recieved meta-data
  9151. // or captions.)
  9152. return;
  9153. } else if (this.remuxTracks) {
  9154. // Return until we have enough tracks from the pipeline to remux (if we
  9155. // are remuxing audio and video into a single MP4)
  9156. return;
  9157. } else if (this.pendingTracks.length === 0) {
  9158. // In the case where we receive a flush without any data having been
  9159. // received we consider it an emitted track for the purposes of coalescing
  9160. // `done` events.
  9161. // We do this for the case where there is an audio and video track in the
  9162. // segment but no audio data. (seen in several playlists with alternate
  9163. // audio tracks and no audio present in the main TS segments.)
  9164. this.emittedTracks++;
  9165. if (this.emittedTracks >= this.numberOfTracks) {
  9166. this.trigger('done');
  9167. this.emittedTracks = 0;
  9168. }
  9169. return;
  9170. }
  9171. }
  9172. if (this.videoTrack) {
  9173. timelineStartPts = this.videoTrack.timelineStartInfo.pts;
  9174. VIDEO_PROPERTIES.forEach(function (prop) {
  9175. event.info[prop] = this.videoTrack[prop];
  9176. }, this);
  9177. } else if (this.audioTrack) {
  9178. timelineStartPts = this.audioTrack.timelineStartInfo.pts;
  9179. AUDIO_PROPERTIES.forEach(function (prop) {
  9180. event.info[prop] = this.audioTrack[prop];
  9181. }, this);
  9182. }
  9183. if (this.videoTrack || this.audioTrack) {
  9184. if (this.pendingTracks.length === 1) {
  9185. event.type = this.pendingTracks[0].type;
  9186. } else {
  9187. event.type = 'combined';
  9188. }
  9189. this.emittedTracks += this.pendingTracks.length;
  9190. initSegment = mp4.initSegment(this.pendingTracks); // Create a new typed array to hold the init segment
  9191. event.initSegment = new Uint8Array(initSegment.byteLength); // Create an init segment containing a moov
  9192. // and track definitions
  9193. event.initSegment.set(initSegment); // Create a new typed array to hold the moof+mdats
  9194. event.data = new Uint8Array(this.pendingBytes); // Append each moof+mdat (one per track) together
  9195. for (i = 0; i < this.pendingBoxes.length; i++) {
  9196. event.data.set(this.pendingBoxes[i], offset);
  9197. offset += this.pendingBoxes[i].byteLength;
  9198. } // Translate caption PTS times into second offsets to match the
  9199. // video timeline for the segment, and add track info
  9200. for (i = 0; i < this.pendingCaptions.length; i++) {
  9201. caption = this.pendingCaptions[i];
  9202. caption.startTime = clock.metadataTsToSeconds(caption.startPts, timelineStartPts, this.keepOriginalTimestamps);
  9203. caption.endTime = clock.metadataTsToSeconds(caption.endPts, timelineStartPts, this.keepOriginalTimestamps);
  9204. event.captionStreams[caption.stream] = true;
  9205. event.captions.push(caption);
  9206. } // Translate ID3 frame PTS times into second offsets to match the
  9207. // video timeline for the segment
  9208. for (i = 0; i < this.pendingMetadata.length; i++) {
  9209. id3 = this.pendingMetadata[i];
  9210. id3.cueTime = clock.metadataTsToSeconds(id3.pts, timelineStartPts, this.keepOriginalTimestamps);
  9211. event.metadata.push(id3);
  9212. } // We add this to every single emitted segment even though we only need
  9213. // it for the first
  9214. event.metadata.dispatchType = this.metadataStream.dispatchType; // Reset stream state
  9215. this.pendingTracks.length = 0;
  9216. this.videoTrack = null;
  9217. this.pendingBoxes.length = 0;
  9218. this.pendingCaptions.length = 0;
  9219. this.pendingBytes = 0;
  9220. this.pendingMetadata.length = 0; // Emit the built segment
  9221. // We include captions and ID3 tags for backwards compatibility,
  9222. // ideally we should send only video and audio in the data event
  9223. this.trigger('data', event); // Emit each caption to the outside world
  9224. // Ideally, this would happen immediately on parsing captions,
  9225. // but we need to ensure that video data is sent back first
  9226. // so that caption timing can be adjusted to match video timing
  9227. for (i = 0; i < event.captions.length; i++) {
  9228. caption = event.captions[i];
  9229. this.trigger('caption', caption);
  9230. } // Emit each id3 tag to the outside world
  9231. // Ideally, this would happen immediately on parsing the tag,
  9232. // but we need to ensure that video data is sent back first
  9233. // so that ID3 frame timing can be adjusted to match video timing
  9234. for (i = 0; i < event.metadata.length; i++) {
  9235. id3 = event.metadata[i];
  9236. this.trigger('id3Frame', id3);
  9237. }
  9238. } // Only emit `done` if all tracks have been flushed and emitted
  9239. if (this.emittedTracks >= this.numberOfTracks) {
  9240. this.trigger('done');
  9241. this.emittedTracks = 0;
  9242. }
  9243. };
  9244. CoalesceStream.prototype.setRemux = function (val) {
  9245. this.remuxTracks = val;
  9246. };
  9247. /**
  9248. * A Stream that expects MP2T binary data as input and produces
  9249. * corresponding media segments, suitable for use with Media Source
  9250. * Extension (MSE) implementations that support the ISO BMFF byte
  9251. * stream format, like Chrome.
  9252. */
  9253. Transmuxer = function (options) {
  9254. var self = this,
  9255. hasFlushed = true,
  9256. videoTrack,
  9257. audioTrack;
  9258. Transmuxer.prototype.init.call(this);
  9259. options = options || {};
  9260. this.baseMediaDecodeTime = options.baseMediaDecodeTime || 0;
  9261. this.transmuxPipeline_ = {};
  9262. this.setupAacPipeline = function () {
  9263. var pipeline = {};
  9264. this.transmuxPipeline_ = pipeline;
  9265. pipeline.type = 'aac';
  9266. pipeline.metadataStream = new m2ts.MetadataStream(); // set up the parsing pipeline
  9267. pipeline.aacStream = new AacStream();
  9268. pipeline.audioTimestampRolloverStream = new m2ts.TimestampRolloverStream('audio');
  9269. pipeline.timedMetadataTimestampRolloverStream = new m2ts.TimestampRolloverStream('timed-metadata');
  9270. pipeline.adtsStream = new AdtsStream();
  9271. pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
  9272. pipeline.headOfPipeline = pipeline.aacStream;
  9273. pipeline.aacStream.pipe(pipeline.audioTimestampRolloverStream).pipe(pipeline.adtsStream);
  9274. pipeline.aacStream.pipe(pipeline.timedMetadataTimestampRolloverStream).pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream);
  9275. pipeline.metadataStream.on('timestamp', function (frame) {
  9276. pipeline.aacStream.setTimestamp(frame.timeStamp);
  9277. });
  9278. pipeline.aacStream.on('data', function (data) {
  9279. if (data.type !== 'timed-metadata' && data.type !== 'audio' || pipeline.audioSegmentStream) {
  9280. return;
  9281. }
  9282. audioTrack = audioTrack || {
  9283. timelineStartInfo: {
  9284. baseMediaDecodeTime: self.baseMediaDecodeTime
  9285. },
  9286. codec: 'adts',
  9287. type: 'audio'
  9288. }; // hook up the audio segment stream to the first track with aac data
  9289. pipeline.coalesceStream.numberOfTracks++;
  9290. pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
  9291. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  9292. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo')); // Set up the final part of the audio pipeline
  9293. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream); // emit pmt info
  9294. self.trigger('trackinfo', {
  9295. hasAudio: !!audioTrack,
  9296. hasVideo: !!videoTrack
  9297. });
  9298. }); // Re-emit any data coming from the coalesce stream to the outside world
  9299. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data')); // Let the consumer know we have finished flushing the entire pipeline
  9300. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  9301. addPipelineLogRetriggers(this, pipeline);
  9302. };
  9303. this.setupTsPipeline = function () {
  9304. var pipeline = {};
  9305. this.transmuxPipeline_ = pipeline;
  9306. pipeline.type = 'ts';
  9307. pipeline.metadataStream = new m2ts.MetadataStream(); // set up the parsing pipeline
  9308. pipeline.packetStream = new m2ts.TransportPacketStream();
  9309. pipeline.parseStream = new m2ts.TransportParseStream();
  9310. pipeline.elementaryStream = new m2ts.ElementaryStream();
  9311. pipeline.timestampRolloverStream = new m2ts.TimestampRolloverStream();
  9312. pipeline.adtsStream = new AdtsStream();
  9313. pipeline.h264Stream = new H264Stream();
  9314. pipeline.captionStream = new m2ts.CaptionStream(options);
  9315. pipeline.coalesceStream = new CoalesceStream(options, pipeline.metadataStream);
  9316. pipeline.headOfPipeline = pipeline.packetStream; // disassemble MPEG2-TS packets into elementary streams
  9317. pipeline.packetStream.pipe(pipeline.parseStream).pipe(pipeline.elementaryStream).pipe(pipeline.timestampRolloverStream); // !!THIS ORDER IS IMPORTANT!!
  9318. // demux the streams
  9319. pipeline.timestampRolloverStream.pipe(pipeline.h264Stream);
  9320. pipeline.timestampRolloverStream.pipe(pipeline.adtsStream);
  9321. pipeline.timestampRolloverStream.pipe(pipeline.metadataStream).pipe(pipeline.coalesceStream); // Hook up CEA-608/708 caption stream
  9322. pipeline.h264Stream.pipe(pipeline.captionStream).pipe(pipeline.coalesceStream);
  9323. pipeline.elementaryStream.on('data', function (data) {
  9324. var i;
  9325. if (data.type === 'metadata') {
  9326. i = data.tracks.length; // scan the tracks listed in the metadata
  9327. while (i--) {
  9328. if (!videoTrack && data.tracks[i].type === 'video') {
  9329. videoTrack = data.tracks[i];
  9330. videoTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  9331. } else if (!audioTrack && data.tracks[i].type === 'audio') {
  9332. audioTrack = data.tracks[i];
  9333. audioTrack.timelineStartInfo.baseMediaDecodeTime = self.baseMediaDecodeTime;
  9334. }
  9335. } // hook up the video segment stream to the first track with h264 data
  9336. if (videoTrack && !pipeline.videoSegmentStream) {
  9337. pipeline.coalesceStream.numberOfTracks++;
  9338. pipeline.videoSegmentStream = new VideoSegmentStream(videoTrack, options);
  9339. pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));
  9340. pipeline.videoSegmentStream.on('timelineStartInfo', function (timelineStartInfo) {
  9341. // When video emits timelineStartInfo data after a flush, we forward that
  9342. // info to the AudioSegmentStream, if it exists, because video timeline
  9343. // data takes precedence. Do not do this if keepOriginalTimestamps is set,
  9344. // because this is a particularly subtle form of timestamp alteration.
  9345. if (audioTrack && !options.keepOriginalTimestamps) {
  9346. audioTrack.timelineStartInfo = timelineStartInfo; // On the first segment we trim AAC frames that exist before the
  9347. // very earliest DTS we have seen in video because Chrome will
  9348. // interpret any video track with a baseMediaDecodeTime that is
  9349. // non-zero as a gap.
  9350. pipeline.audioSegmentStream.setEarliestDts(timelineStartInfo.dts - self.baseMediaDecodeTime);
  9351. }
  9352. });
  9353. pipeline.videoSegmentStream.on('processedGopsInfo', self.trigger.bind(self, 'gopInfo'));
  9354. pipeline.videoSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'videoSegmentTimingInfo'));
  9355. pipeline.videoSegmentStream.on('baseMediaDecodeTime', function (baseMediaDecodeTime) {
  9356. if (audioTrack) {
  9357. pipeline.audioSegmentStream.setVideoBaseMediaDecodeTime(baseMediaDecodeTime);
  9358. }
  9359. });
  9360. pipeline.videoSegmentStream.on('timingInfo', self.trigger.bind(self, 'videoTimingInfo')); // Set up the final part of the video pipeline
  9361. pipeline.h264Stream.pipe(pipeline.videoSegmentStream).pipe(pipeline.coalesceStream);
  9362. }
  9363. if (audioTrack && !pipeline.audioSegmentStream) {
  9364. // hook up the audio segment stream to the first track with aac data
  9365. pipeline.coalesceStream.numberOfTracks++;
  9366. pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
  9367. pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));
  9368. pipeline.audioSegmentStream.on('timingInfo', self.trigger.bind(self, 'audioTimingInfo'));
  9369. pipeline.audioSegmentStream.on('segmentTimingInfo', self.trigger.bind(self, 'audioSegmentTimingInfo')); // Set up the final part of the audio pipeline
  9370. pipeline.adtsStream.pipe(pipeline.audioSegmentStream).pipe(pipeline.coalesceStream);
  9371. } // emit pmt info
  9372. self.trigger('trackinfo', {
  9373. hasAudio: !!audioTrack,
  9374. hasVideo: !!videoTrack
  9375. });
  9376. }
  9377. }); // Re-emit any data coming from the coalesce stream to the outside world
  9378. pipeline.coalesceStream.on('data', this.trigger.bind(this, 'data'));
  9379. pipeline.coalesceStream.on('id3Frame', function (id3Frame) {
  9380. id3Frame.dispatchType = pipeline.metadataStream.dispatchType;
  9381. self.trigger('id3Frame', id3Frame);
  9382. });
  9383. pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption')); // Let the consumer know we have finished flushing the entire pipeline
  9384. pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
  9385. addPipelineLogRetriggers(this, pipeline);
  9386. }; // hook up the segment streams once track metadata is delivered
  9387. this.setBaseMediaDecodeTime = function (baseMediaDecodeTime) {
  9388. var pipeline = this.transmuxPipeline_;
  9389. if (!options.keepOriginalTimestamps) {
  9390. this.baseMediaDecodeTime = baseMediaDecodeTime;
  9391. }
  9392. if (audioTrack) {
  9393. audioTrack.timelineStartInfo.dts = undefined;
  9394. audioTrack.timelineStartInfo.pts = undefined;
  9395. trackDecodeInfo.clearDtsInfo(audioTrack);
  9396. if (pipeline.audioTimestampRolloverStream) {
  9397. pipeline.audioTimestampRolloverStream.discontinuity();
  9398. }
  9399. }
  9400. if (videoTrack) {
  9401. if (pipeline.videoSegmentStream) {
  9402. pipeline.videoSegmentStream.gopCache_ = [];
  9403. }
  9404. videoTrack.timelineStartInfo.dts = undefined;
  9405. videoTrack.timelineStartInfo.pts = undefined;
  9406. trackDecodeInfo.clearDtsInfo(videoTrack);
  9407. pipeline.captionStream.reset();
  9408. }
  9409. if (pipeline.timestampRolloverStream) {
  9410. pipeline.timestampRolloverStream.discontinuity();
  9411. }
  9412. };
  9413. this.setAudioAppendStart = function (timestamp) {
  9414. if (audioTrack) {
  9415. this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(timestamp);
  9416. }
  9417. };
  9418. this.setRemux = function (val) {
  9419. var pipeline = this.transmuxPipeline_;
  9420. options.remux = val;
  9421. if (pipeline && pipeline.coalesceStream) {
  9422. pipeline.coalesceStream.setRemux(val);
  9423. }
  9424. };
  9425. this.alignGopsWith = function (gopsToAlignWith) {
  9426. if (videoTrack && this.transmuxPipeline_.videoSegmentStream) {
  9427. this.transmuxPipeline_.videoSegmentStream.alignGopsWith(gopsToAlignWith);
  9428. }
  9429. };
  9430. this.getLogTrigger_ = function (key) {
  9431. var self = this;
  9432. return function (event) {
  9433. event.stream = key;
  9434. self.trigger('log', event);
  9435. };
  9436. }; // feed incoming data to the front of the parsing pipeline
  9437. this.push = function (data) {
  9438. if (hasFlushed) {
  9439. var isAac = isLikelyAacData(data);
  9440. if (isAac && this.transmuxPipeline_.type !== 'aac') {
  9441. this.setupAacPipeline();
  9442. } else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
  9443. this.setupTsPipeline();
  9444. }
  9445. hasFlushed = false;
  9446. }
  9447. this.transmuxPipeline_.headOfPipeline.push(data);
  9448. }; // flush any buffered data
  9449. this.flush = function () {
  9450. hasFlushed = true; // Start at the top of the pipeline and flush all pending work
  9451. this.transmuxPipeline_.headOfPipeline.flush();
  9452. };
  9453. this.endTimeline = function () {
  9454. this.transmuxPipeline_.headOfPipeline.endTimeline();
  9455. };
  9456. this.reset = function () {
  9457. if (this.transmuxPipeline_.headOfPipeline) {
  9458. this.transmuxPipeline_.headOfPipeline.reset();
  9459. }
  9460. }; // Caption data has to be reset when seeking outside buffered range
  9461. this.resetCaptions = function () {
  9462. if (this.transmuxPipeline_.captionStream) {
  9463. this.transmuxPipeline_.captionStream.reset();
  9464. }
  9465. };
  9466. };
  9467. Transmuxer.prototype = new Stream();
  9468. var transmuxer = {
  9469. Transmuxer: Transmuxer,
  9470. VideoSegmentStream: VideoSegmentStream,
  9471. AudioSegmentStream: AudioSegmentStream,
  9472. AUDIO_PROPERTIES: AUDIO_PROPERTIES,
  9473. VIDEO_PROPERTIES: VIDEO_PROPERTIES,
  9474. // exported for testing
  9475. generateSegmentTimingInfo: generateSegmentTimingInfo
  9476. };
  9477. /**
  9478. * mux.js
  9479. *
  9480. * Copyright (c) Brightcove
  9481. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  9482. */
  9483. var toUnsigned$3 = function (value) {
  9484. return value >>> 0;
  9485. };
  9486. var toHexString$1 = function (value) {
  9487. return ('00' + value.toString(16)).slice(-2);
  9488. };
  9489. var bin = {
  9490. toUnsigned: toUnsigned$3,
  9491. toHexString: toHexString$1
  9492. };
  9493. var parseType$3 = function (buffer) {
  9494. var result = '';
  9495. result += String.fromCharCode(buffer[0]);
  9496. result += String.fromCharCode(buffer[1]);
  9497. result += String.fromCharCode(buffer[2]);
  9498. result += String.fromCharCode(buffer[3]);
  9499. return result;
  9500. };
  9501. var parseType_1 = parseType$3;
  9502. var toUnsigned$2 = bin.toUnsigned;
  9503. var parseType$2 = parseType_1;
  9504. var findBox$2 = function (data, path) {
  9505. var results = [],
  9506. i,
  9507. size,
  9508. type,
  9509. end,
  9510. subresults;
  9511. if (!path.length) {
  9512. // short-circuit the search for empty paths
  9513. return null;
  9514. }
  9515. for (i = 0; i < data.byteLength;) {
  9516. size = toUnsigned$2(data[i] << 24 | data[i + 1] << 16 | data[i + 2] << 8 | data[i + 3]);
  9517. type = parseType$2(data.subarray(i + 4, i + 8));
  9518. end = size > 1 ? i + size : data.byteLength;
  9519. if (type === path[0]) {
  9520. if (path.length === 1) {
  9521. // this is the end of the path and we've found the box we were
  9522. // looking for
  9523. results.push(data.subarray(i + 8, end));
  9524. } else {
  9525. // recursively search for the next box along the path
  9526. subresults = findBox$2(data.subarray(i + 8, end), path.slice(1));
  9527. if (subresults.length) {
  9528. results = results.concat(subresults);
  9529. }
  9530. }
  9531. }
  9532. i = end;
  9533. } // we've finished searching all of data
  9534. return results;
  9535. };
  9536. var findBox_1 = findBox$2;
  9537. var toUnsigned$1 = bin.toUnsigned;
  9538. var getUint64$2 = numbers.getUint64;
  9539. var tfdt = function (data) {
  9540. var result = {
  9541. version: data[0],
  9542. flags: new Uint8Array(data.subarray(1, 4))
  9543. };
  9544. if (result.version === 1) {
  9545. result.baseMediaDecodeTime = getUint64$2(data.subarray(4));
  9546. } else {
  9547. result.baseMediaDecodeTime = toUnsigned$1(data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]);
  9548. }
  9549. return result;
  9550. };
  9551. var parseTfdt$2 = tfdt;
  9552. var parseSampleFlags$1 = function (flags) {
  9553. return {
  9554. isLeading: (flags[0] & 0x0c) >>> 2,
  9555. dependsOn: flags[0] & 0x03,
  9556. isDependedOn: (flags[1] & 0xc0) >>> 6,
  9557. hasRedundancy: (flags[1] & 0x30) >>> 4,
  9558. paddingValue: (flags[1] & 0x0e) >>> 1,
  9559. isNonSyncSample: flags[1] & 0x01,
  9560. degradationPriority: flags[2] << 8 | flags[3]
  9561. };
  9562. };
  9563. var parseSampleFlags_1 = parseSampleFlags$1;
  9564. var parseSampleFlags = parseSampleFlags_1;
  9565. var trun = function (data) {
  9566. var result = {
  9567. version: data[0],
  9568. flags: new Uint8Array(data.subarray(1, 4)),
  9569. samples: []
  9570. },
  9571. view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  9572. // Flag interpretation
  9573. dataOffsetPresent = result.flags[2] & 0x01,
  9574. // compare with 2nd byte of 0x1
  9575. firstSampleFlagsPresent = result.flags[2] & 0x04,
  9576. // compare with 2nd byte of 0x4
  9577. sampleDurationPresent = result.flags[1] & 0x01,
  9578. // compare with 2nd byte of 0x100
  9579. sampleSizePresent = result.flags[1] & 0x02,
  9580. // compare with 2nd byte of 0x200
  9581. sampleFlagsPresent = result.flags[1] & 0x04,
  9582. // compare with 2nd byte of 0x400
  9583. sampleCompositionTimeOffsetPresent = result.flags[1] & 0x08,
  9584. // compare with 2nd byte of 0x800
  9585. sampleCount = view.getUint32(4),
  9586. offset = 8,
  9587. sample;
  9588. if (dataOffsetPresent) {
  9589. // 32 bit signed integer
  9590. result.dataOffset = view.getInt32(offset);
  9591. offset += 4;
  9592. } // Overrides the flags for the first sample only. The order of
  9593. // optional values will be: duration, size, compositionTimeOffset
  9594. if (firstSampleFlagsPresent && sampleCount) {
  9595. sample = {
  9596. flags: parseSampleFlags(data.subarray(offset, offset + 4))
  9597. };
  9598. offset += 4;
  9599. if (sampleDurationPresent) {
  9600. sample.duration = view.getUint32(offset);
  9601. offset += 4;
  9602. }
  9603. if (sampleSizePresent) {
  9604. sample.size = view.getUint32(offset);
  9605. offset += 4;
  9606. }
  9607. if (sampleCompositionTimeOffsetPresent) {
  9608. if (result.version === 1) {
  9609. sample.compositionTimeOffset = view.getInt32(offset);
  9610. } else {
  9611. sample.compositionTimeOffset = view.getUint32(offset);
  9612. }
  9613. offset += 4;
  9614. }
  9615. result.samples.push(sample);
  9616. sampleCount--;
  9617. }
  9618. while (sampleCount--) {
  9619. sample = {};
  9620. if (sampleDurationPresent) {
  9621. sample.duration = view.getUint32(offset);
  9622. offset += 4;
  9623. }
  9624. if (sampleSizePresent) {
  9625. sample.size = view.getUint32(offset);
  9626. offset += 4;
  9627. }
  9628. if (sampleFlagsPresent) {
  9629. sample.flags = parseSampleFlags(data.subarray(offset, offset + 4));
  9630. offset += 4;
  9631. }
  9632. if (sampleCompositionTimeOffsetPresent) {
  9633. if (result.version === 1) {
  9634. sample.compositionTimeOffset = view.getInt32(offset);
  9635. } else {
  9636. sample.compositionTimeOffset = view.getUint32(offset);
  9637. }
  9638. offset += 4;
  9639. }
  9640. result.samples.push(sample);
  9641. }
  9642. return result;
  9643. };
  9644. var parseTrun$2 = trun;
  9645. var tfhd = function (data) {
  9646. var view = new DataView(data.buffer, data.byteOffset, data.byteLength),
  9647. result = {
  9648. version: data[0],
  9649. flags: new Uint8Array(data.subarray(1, 4)),
  9650. trackId: view.getUint32(4)
  9651. },
  9652. baseDataOffsetPresent = result.flags[2] & 0x01,
  9653. sampleDescriptionIndexPresent = result.flags[2] & 0x02,
  9654. defaultSampleDurationPresent = result.flags[2] & 0x08,
  9655. defaultSampleSizePresent = result.flags[2] & 0x10,
  9656. defaultSampleFlagsPresent = result.flags[2] & 0x20,
  9657. durationIsEmpty = result.flags[0] & 0x010000,
  9658. defaultBaseIsMoof = result.flags[0] & 0x020000,
  9659. i;
  9660. i = 8;
  9661. if (baseDataOffsetPresent) {
  9662. i += 4; // truncate top 4 bytes
  9663. // FIXME: should we read the full 64 bits?
  9664. result.baseDataOffset = view.getUint32(12);
  9665. i += 4;
  9666. }
  9667. if (sampleDescriptionIndexPresent) {
  9668. result.sampleDescriptionIndex = view.getUint32(i);
  9669. i += 4;
  9670. }
  9671. if (defaultSampleDurationPresent) {
  9672. result.defaultSampleDuration = view.getUint32(i);
  9673. i += 4;
  9674. }
  9675. if (defaultSampleSizePresent) {
  9676. result.defaultSampleSize = view.getUint32(i);
  9677. i += 4;
  9678. }
  9679. if (defaultSampleFlagsPresent) {
  9680. result.defaultSampleFlags = view.getUint32(i);
  9681. }
  9682. if (durationIsEmpty) {
  9683. result.durationIsEmpty = true;
  9684. }
  9685. if (!baseDataOffsetPresent && defaultBaseIsMoof) {
  9686. result.baseDataOffsetIsMoof = true;
  9687. }
  9688. return result;
  9689. };
  9690. var parseTfhd$2 = tfhd;
  9691. var win;
  9692. if (typeof window !== "undefined") {
  9693. win = window;
  9694. } else if (typeof commonjsGlobal !== "undefined") {
  9695. win = commonjsGlobal;
  9696. } else if (typeof self !== "undefined") {
  9697. win = self;
  9698. } else {
  9699. win = {};
  9700. }
  9701. var window_1 = win;
  9702. /**
  9703. * mux.js
  9704. *
  9705. * Copyright (c) Brightcove
  9706. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  9707. *
  9708. * Reads in-band CEA-708 captions out of FMP4 segments.
  9709. * @see https://en.wikipedia.org/wiki/CEA-708
  9710. */
  9711. var discardEmulationPreventionBytes = captionPacketParser.discardEmulationPreventionBytes;
  9712. var CaptionStream = captionStream.CaptionStream;
  9713. var findBox$1 = findBox_1;
  9714. var parseTfdt$1 = parseTfdt$2;
  9715. var parseTrun$1 = parseTrun$2;
  9716. var parseTfhd$1 = parseTfhd$2;
  9717. var window$2 = window_1;
  9718. /**
  9719. * Maps an offset in the mdat to a sample based on the the size of the samples.
  9720. * Assumes that `parseSamples` has been called first.
  9721. *
  9722. * @param {Number} offset - The offset into the mdat
  9723. * @param {Object[]} samples - An array of samples, parsed using `parseSamples`
  9724. * @return {?Object} The matching sample, or null if no match was found.
  9725. *
  9726. * @see ISO-BMFF-12/2015, Section 8.8.8
  9727. **/
  9728. var mapToSample = function (offset, samples) {
  9729. var approximateOffset = offset;
  9730. for (var i = 0; i < samples.length; i++) {
  9731. var sample = samples[i];
  9732. if (approximateOffset < sample.size) {
  9733. return sample;
  9734. }
  9735. approximateOffset -= sample.size;
  9736. }
  9737. return null;
  9738. };
  9739. /**
  9740. * Finds SEI nal units contained in a Media Data Box.
  9741. * Assumes that `parseSamples` has been called first.
  9742. *
  9743. * @param {Uint8Array} avcStream - The bytes of the mdat
  9744. * @param {Object[]} samples - The samples parsed out by `parseSamples`
  9745. * @param {Number} trackId - The trackId of this video track
  9746. * @return {Object[]} seiNals - the parsed SEI NALUs found.
  9747. * The contents of the seiNal should match what is expected by
  9748. * CaptionStream.push (nalUnitType, size, data, escapedRBSP, pts, dts)
  9749. *
  9750. * @see ISO-BMFF-12/2015, Section 8.1.1
  9751. * @see Rec. ITU-T H.264, 7.3.2.3.1
  9752. **/
  9753. var findSeiNals = function (avcStream, samples, trackId) {
  9754. var avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
  9755. result = {
  9756. logs: [],
  9757. seiNals: []
  9758. },
  9759. seiNal,
  9760. i,
  9761. length,
  9762. lastMatchedSample;
  9763. for (i = 0; i + 4 < avcStream.length; i += length) {
  9764. length = avcView.getUint32(i);
  9765. i += 4; // Bail if this doesn't appear to be an H264 stream
  9766. if (length <= 0) {
  9767. continue;
  9768. }
  9769. switch (avcStream[i] & 0x1F) {
  9770. case 0x06:
  9771. var data = avcStream.subarray(i + 1, i + 1 + length);
  9772. var matchingSample = mapToSample(i, samples);
  9773. seiNal = {
  9774. nalUnitType: 'sei_rbsp',
  9775. size: length,
  9776. data: data,
  9777. escapedRBSP: discardEmulationPreventionBytes(data),
  9778. trackId: trackId
  9779. };
  9780. if (matchingSample) {
  9781. seiNal.pts = matchingSample.pts;
  9782. seiNal.dts = matchingSample.dts;
  9783. lastMatchedSample = matchingSample;
  9784. } else if (lastMatchedSample) {
  9785. // If a matching sample cannot be found, use the last
  9786. // sample's values as they should be as close as possible
  9787. seiNal.pts = lastMatchedSample.pts;
  9788. seiNal.dts = lastMatchedSample.dts;
  9789. } else {
  9790. result.logs.push({
  9791. level: 'warn',
  9792. message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
  9793. });
  9794. break;
  9795. }
  9796. result.seiNals.push(seiNal);
  9797. break;
  9798. }
  9799. }
  9800. return result;
  9801. };
  9802. /**
  9803. * Parses sample information out of Track Run Boxes and calculates
  9804. * the absolute presentation and decode timestamps of each sample.
  9805. *
  9806. * @param {Array<Uint8Array>} truns - The Trun Run boxes to be parsed
  9807. * @param {Number|BigInt} baseMediaDecodeTime - base media decode time from tfdt
  9808. @see ISO-BMFF-12/2015, Section 8.8.12
  9809. * @param {Object} tfhd - The parsed Track Fragment Header
  9810. * @see inspect.parseTfhd
  9811. * @return {Object[]} the parsed samples
  9812. *
  9813. * @see ISO-BMFF-12/2015, Section 8.8.8
  9814. **/
  9815. var parseSamples = function (truns, baseMediaDecodeTime, tfhd) {
  9816. var currentDts = baseMediaDecodeTime;
  9817. var defaultSampleDuration = tfhd.defaultSampleDuration || 0;
  9818. var defaultSampleSize = tfhd.defaultSampleSize || 0;
  9819. var trackId = tfhd.trackId;
  9820. var allSamples = [];
  9821. truns.forEach(function (trun) {
  9822. // Note: We currently do not parse the sample table as well
  9823. // as the trun. It's possible some sources will require this.
  9824. // moov > trak > mdia > minf > stbl
  9825. var trackRun = parseTrun$1(trun);
  9826. var samples = trackRun.samples;
  9827. samples.forEach(function (sample) {
  9828. if (sample.duration === undefined) {
  9829. sample.duration = defaultSampleDuration;
  9830. }
  9831. if (sample.size === undefined) {
  9832. sample.size = defaultSampleSize;
  9833. }
  9834. sample.trackId = trackId;
  9835. sample.dts = currentDts;
  9836. if (sample.compositionTimeOffset === undefined) {
  9837. sample.compositionTimeOffset = 0;
  9838. }
  9839. if (typeof currentDts === 'bigint') {
  9840. sample.pts = currentDts + window$2.BigInt(sample.compositionTimeOffset);
  9841. currentDts += window$2.BigInt(sample.duration);
  9842. } else {
  9843. sample.pts = currentDts + sample.compositionTimeOffset;
  9844. currentDts += sample.duration;
  9845. }
  9846. });
  9847. allSamples = allSamples.concat(samples);
  9848. });
  9849. return allSamples;
  9850. };
  9851. /**
  9852. * Parses out caption nals from an FMP4 segment's video tracks.
  9853. *
  9854. * @param {Uint8Array} segment - The bytes of a single segment
  9855. * @param {Number} videoTrackId - The trackId of a video track in the segment
  9856. * @return {Object.<Number, Object[]>} A mapping of video trackId to
  9857. * a list of seiNals found in that track
  9858. **/
  9859. var parseCaptionNals = function (segment, videoTrackId) {
  9860. // To get the samples
  9861. var trafs = findBox$1(segment, ['moof', 'traf']); // To get SEI NAL units
  9862. var mdats = findBox$1(segment, ['mdat']);
  9863. var captionNals = {};
  9864. var mdatTrafPairs = []; // Pair up each traf with a mdat as moofs and mdats are in pairs
  9865. mdats.forEach(function (mdat, index) {
  9866. var matchingTraf = trafs[index];
  9867. mdatTrafPairs.push({
  9868. mdat: mdat,
  9869. traf: matchingTraf
  9870. });
  9871. });
  9872. mdatTrafPairs.forEach(function (pair) {
  9873. var mdat = pair.mdat;
  9874. var traf = pair.traf;
  9875. var tfhd = findBox$1(traf, ['tfhd']); // Exactly 1 tfhd per traf
  9876. var headerInfo = parseTfhd$1(tfhd[0]);
  9877. var trackId = headerInfo.trackId;
  9878. var tfdt = findBox$1(traf, ['tfdt']); // Either 0 or 1 tfdt per traf
  9879. var baseMediaDecodeTime = tfdt.length > 0 ? parseTfdt$1(tfdt[0]).baseMediaDecodeTime : 0;
  9880. var truns = findBox$1(traf, ['trun']);
  9881. var samples;
  9882. var result; // Only parse video data for the chosen video track
  9883. if (videoTrackId === trackId && truns.length > 0) {
  9884. samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);
  9885. result = findSeiNals(mdat, samples, trackId);
  9886. if (!captionNals[trackId]) {
  9887. captionNals[trackId] = {
  9888. seiNals: [],
  9889. logs: []
  9890. };
  9891. }
  9892. captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
  9893. captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
  9894. }
  9895. });
  9896. return captionNals;
  9897. };
  9898. /**
  9899. * Parses out inband captions from an MP4 container and returns
  9900. * caption objects that can be used by WebVTT and the TextTrack API.
  9901. * @see https://developer.mozilla.org/en-US/docs/Web/API/VTTCue
  9902. * @see https://developer.mozilla.org/en-US/docs/Web/API/TextTrack
  9903. * Assumes that `probe.getVideoTrackIds` and `probe.timescale` have been called first
  9904. *
  9905. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  9906. * @param {Number} trackId - The id of the video track to parse
  9907. * @param {Number} timescale - The timescale for the video track from the init segment
  9908. *
  9909. * @return {?Object[]} parsedCaptions - A list of captions or null if no video tracks
  9910. * @return {Number} parsedCaptions[].startTime - The time to show the caption in seconds
  9911. * @return {Number} parsedCaptions[].endTime - The time to stop showing the caption in seconds
  9912. * @return {Object[]} parsedCaptions[].content - A list of individual caption segments
  9913. * @return {String} parsedCaptions[].content.text - The visible content of the caption segment
  9914. * @return {Number} parsedCaptions[].content.line - The line height from 1-15 for positioning of the caption segment
  9915. * @return {Number} parsedCaptions[].content.position - The column indent percentage for cue positioning from 10-80
  9916. **/
  9917. var parseEmbeddedCaptions = function (segment, trackId, timescale) {
  9918. var captionNals; // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  9919. if (trackId === null) {
  9920. return null;
  9921. }
  9922. captionNals = parseCaptionNals(segment, trackId);
  9923. var trackNals = captionNals[trackId] || {};
  9924. return {
  9925. seiNals: trackNals.seiNals,
  9926. logs: trackNals.logs,
  9927. timescale: timescale
  9928. };
  9929. };
  9930. /**
  9931. * Converts SEI NALUs into captions that can be used by video.js
  9932. **/
  9933. var CaptionParser = function () {
  9934. var isInitialized = false;
  9935. var captionStream; // Stores segments seen before trackId and timescale are set
  9936. var segmentCache; // Stores video track ID of the track being parsed
  9937. var trackId; // Stores the timescale of the track being parsed
  9938. var timescale; // Stores captions parsed so far
  9939. var parsedCaptions; // Stores whether we are receiving partial data or not
  9940. var parsingPartial;
  9941. /**
  9942. * A method to indicate whether a CaptionParser has been initalized
  9943. * @returns {Boolean}
  9944. **/
  9945. this.isInitialized = function () {
  9946. return isInitialized;
  9947. };
  9948. /**
  9949. * Initializes the underlying CaptionStream, SEI NAL parsing
  9950. * and management, and caption collection
  9951. **/
  9952. this.init = function (options) {
  9953. captionStream = new CaptionStream();
  9954. isInitialized = true;
  9955. parsingPartial = options ? options.isPartial : false; // Collect dispatched captions
  9956. captionStream.on('data', function (event) {
  9957. // Convert to seconds in the source's timescale
  9958. event.startTime = event.startPts / timescale;
  9959. event.endTime = event.endPts / timescale;
  9960. parsedCaptions.captions.push(event);
  9961. parsedCaptions.captionStreams[event.stream] = true;
  9962. });
  9963. captionStream.on('log', function (log) {
  9964. parsedCaptions.logs.push(log);
  9965. });
  9966. };
  9967. /**
  9968. * Determines if a new video track will be selected
  9969. * or if the timescale changed
  9970. * @return {Boolean}
  9971. **/
  9972. this.isNewInit = function (videoTrackIds, timescales) {
  9973. if (videoTrackIds && videoTrackIds.length === 0 || timescales && typeof timescales === 'object' && Object.keys(timescales).length === 0) {
  9974. return false;
  9975. }
  9976. return trackId !== videoTrackIds[0] || timescale !== timescales[trackId];
  9977. };
  9978. /**
  9979. * Parses out SEI captions and interacts with underlying
  9980. * CaptionStream to return dispatched captions
  9981. *
  9982. * @param {Uint8Array} segment - The fmp4 segment containing embedded captions
  9983. * @param {Number[]} videoTrackIds - A list of video tracks found in the init segment
  9984. * @param {Object.<Number, Number>} timescales - The timescales found in the init segment
  9985. * @see parseEmbeddedCaptions
  9986. * @see m2ts/caption-stream.js
  9987. **/
  9988. this.parse = function (segment, videoTrackIds, timescales) {
  9989. var parsedData;
  9990. if (!this.isInitialized()) {
  9991. return null; // This is not likely to be a video segment
  9992. } else if (!videoTrackIds || !timescales) {
  9993. return null;
  9994. } else if (this.isNewInit(videoTrackIds, timescales)) {
  9995. // Use the first video track only as there is no
  9996. // mechanism to switch to other video tracks
  9997. trackId = videoTrackIds[0];
  9998. timescale = timescales[trackId]; // If an init segment has not been seen yet, hold onto segment
  9999. // data until we have one.
  10000. // the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
  10001. } else if (trackId === null || !timescale) {
  10002. segmentCache.push(segment);
  10003. return null;
  10004. } // Now that a timescale and trackId is set, parse cached segments
  10005. while (segmentCache.length > 0) {
  10006. var cachedSegment = segmentCache.shift();
  10007. this.parse(cachedSegment, videoTrackIds, timescales);
  10008. }
  10009. parsedData = parseEmbeddedCaptions(segment, trackId, timescale);
  10010. if (parsedData && parsedData.logs) {
  10011. parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
  10012. }
  10013. if (parsedData === null || !parsedData.seiNals) {
  10014. if (parsedCaptions.logs.length) {
  10015. return {
  10016. logs: parsedCaptions.logs,
  10017. captions: [],
  10018. captionStreams: []
  10019. };
  10020. }
  10021. return null;
  10022. }
  10023. this.pushNals(parsedData.seiNals); // Force the parsed captions to be dispatched
  10024. this.flushStream();
  10025. return parsedCaptions;
  10026. };
  10027. /**
  10028. * Pushes SEI NALUs onto CaptionStream
  10029. * @param {Object[]} nals - A list of SEI nals parsed using `parseCaptionNals`
  10030. * Assumes that `parseCaptionNals` has been called first
  10031. * @see m2ts/caption-stream.js
  10032. **/
  10033. this.pushNals = function (nals) {
  10034. if (!this.isInitialized() || !nals || nals.length === 0) {
  10035. return null;
  10036. }
  10037. nals.forEach(function (nal) {
  10038. captionStream.push(nal);
  10039. });
  10040. };
  10041. /**
  10042. * Flushes underlying CaptionStream to dispatch processed, displayable captions
  10043. * @see m2ts/caption-stream.js
  10044. **/
  10045. this.flushStream = function () {
  10046. if (!this.isInitialized()) {
  10047. return null;
  10048. }
  10049. if (!parsingPartial) {
  10050. captionStream.flush();
  10051. } else {
  10052. captionStream.partialFlush();
  10053. }
  10054. };
  10055. /**
  10056. * Reset caption buckets for new data
  10057. **/
  10058. this.clearParsedCaptions = function () {
  10059. parsedCaptions.captions = [];
  10060. parsedCaptions.captionStreams = {};
  10061. parsedCaptions.logs = [];
  10062. };
  10063. /**
  10064. * Resets underlying CaptionStream
  10065. * @see m2ts/caption-stream.js
  10066. **/
  10067. this.resetCaptionStream = function () {
  10068. if (!this.isInitialized()) {
  10069. return null;
  10070. }
  10071. captionStream.reset();
  10072. };
  10073. /**
  10074. * Convenience method to clear all captions flushed from the
  10075. * CaptionStream and still being parsed
  10076. * @see m2ts/caption-stream.js
  10077. **/
  10078. this.clearAllCaptions = function () {
  10079. this.clearParsedCaptions();
  10080. this.resetCaptionStream();
  10081. };
  10082. /**
  10083. * Reset caption parser
  10084. **/
  10085. this.reset = function () {
  10086. segmentCache = [];
  10087. trackId = null;
  10088. timescale = null;
  10089. if (!parsedCaptions) {
  10090. parsedCaptions = {
  10091. captions: [],
  10092. // CC1, CC2, CC3, CC4
  10093. captionStreams: {},
  10094. logs: []
  10095. };
  10096. } else {
  10097. this.clearParsedCaptions();
  10098. }
  10099. this.resetCaptionStream();
  10100. };
  10101. this.reset();
  10102. };
  10103. var captionParser = CaptionParser;
  10104. /**
  10105. * Returns the first string in the data array ending with a null char '\0'
  10106. * @param {UInt8} data
  10107. * @returns the string with the null char
  10108. */
  10109. var uint8ToCString$1 = function (data) {
  10110. var index = 0;
  10111. var curChar = String.fromCharCode(data[index]);
  10112. var retString = '';
  10113. while (curChar !== '\0') {
  10114. retString += curChar;
  10115. index++;
  10116. curChar = String.fromCharCode(data[index]);
  10117. } // Add nullChar
  10118. retString += curChar;
  10119. return retString;
  10120. };
  10121. var string = {
  10122. uint8ToCString: uint8ToCString$1
  10123. };
  10124. var uint8ToCString = string.uint8ToCString;
  10125. var getUint64$1 = numbers.getUint64;
  10126. /**
  10127. * Based on: ISO/IEC 23009 Section: 5.10.3.3
  10128. * References:
  10129. * https://dashif-documents.azurewebsites.net/Events/master/event.html#emsg-format
  10130. * https://aomediacodec.github.io/id3-emsg/
  10131. *
  10132. * Takes emsg box data as a uint8 array and returns a emsg box object
  10133. * @param {UInt8Array} boxData data from emsg box
  10134. * @returns A parsed emsg box object
  10135. */
  10136. var parseEmsgBox = function (boxData) {
  10137. // version + flags
  10138. var offset = 4;
  10139. var version = boxData[0];
  10140. var scheme_id_uri, value, timescale, presentation_time, presentation_time_delta, event_duration, id, message_data;
  10141. if (version === 0) {
  10142. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  10143. offset += scheme_id_uri.length;
  10144. value = uint8ToCString(boxData.subarray(offset));
  10145. offset += value.length;
  10146. var dv = new DataView(boxData.buffer);
  10147. timescale = dv.getUint32(offset);
  10148. offset += 4;
  10149. presentation_time_delta = dv.getUint32(offset);
  10150. offset += 4;
  10151. event_duration = dv.getUint32(offset);
  10152. offset += 4;
  10153. id = dv.getUint32(offset);
  10154. offset += 4;
  10155. } else if (version === 1) {
  10156. var dv = new DataView(boxData.buffer);
  10157. timescale = dv.getUint32(offset);
  10158. offset += 4;
  10159. presentation_time = getUint64$1(boxData.subarray(offset));
  10160. offset += 8;
  10161. event_duration = dv.getUint32(offset);
  10162. offset += 4;
  10163. id = dv.getUint32(offset);
  10164. offset += 4;
  10165. scheme_id_uri = uint8ToCString(boxData.subarray(offset));
  10166. offset += scheme_id_uri.length;
  10167. value = uint8ToCString(boxData.subarray(offset));
  10168. offset += value.length;
  10169. }
  10170. message_data = new Uint8Array(boxData.subarray(offset, boxData.byteLength));
  10171. var emsgBox = {
  10172. scheme_id_uri,
  10173. value,
  10174. // if timescale is undefined or 0 set to 1
  10175. timescale: timescale ? timescale : 1,
  10176. presentation_time,
  10177. presentation_time_delta,
  10178. event_duration,
  10179. id,
  10180. message_data
  10181. };
  10182. return isValidEmsgBox(version, emsgBox) ? emsgBox : undefined;
  10183. };
  10184. /**
  10185. * Scales a presentation time or time delta with an offset with a provided timescale
  10186. * @param {number} presentationTime
  10187. * @param {number} timescale
  10188. * @param {number} timeDelta
  10189. * @param {number} offset
  10190. * @returns the scaled time as a number
  10191. */
  10192. var scaleTime = function (presentationTime, timescale, timeDelta, offset) {
  10193. return presentationTime || presentationTime === 0 ? presentationTime / timescale : offset + timeDelta / timescale;
  10194. };
  10195. /**
  10196. * Checks the emsg box data for validity based on the version
  10197. * @param {number} version of the emsg box to validate
  10198. * @param {Object} emsg the emsg data to validate
  10199. * @returns if the box is valid as a boolean
  10200. */
  10201. var isValidEmsgBox = function (version, emsg) {
  10202. var hasScheme = emsg.scheme_id_uri !== '\0';
  10203. var isValidV0Box = version === 0 && isDefined(emsg.presentation_time_delta) && hasScheme;
  10204. var isValidV1Box = version === 1 && isDefined(emsg.presentation_time) && hasScheme; // Only valid versions of emsg are 0 and 1
  10205. return !(version > 1) && isValidV0Box || isValidV1Box;
  10206. }; // Utility function to check if an object is defined
  10207. var isDefined = function (data) {
  10208. return data !== undefined || data !== null;
  10209. };
  10210. var emsg$1 = {
  10211. parseEmsgBox: parseEmsgBox,
  10212. scaleTime: scaleTime
  10213. };
  10214. /**
  10215. * mux.js
  10216. *
  10217. * Copyright (c) Brightcove
  10218. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  10219. *
  10220. * Utilities to detect basic properties and metadata about MP4s.
  10221. */
  10222. var toUnsigned = bin.toUnsigned;
  10223. var toHexString = bin.toHexString;
  10224. var findBox = findBox_1;
  10225. var parseType$1 = parseType_1;
  10226. var emsg = emsg$1;
  10227. var parseTfhd = parseTfhd$2;
  10228. var parseTrun = parseTrun$2;
  10229. var parseTfdt = parseTfdt$2;
  10230. var getUint64 = numbers.getUint64;
  10231. var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3;
  10232. var window$1 = window_1;
  10233. var parseId3Frames = parseId3.parseId3Frames;
  10234. /**
  10235. * Parses an MP4 initialization segment and extracts the timescale
  10236. * values for any declared tracks. Timescale values indicate the
  10237. * number of clock ticks per second to assume for time-based values
  10238. * elsewhere in the MP4.
  10239. *
  10240. * To determine the start time of an MP4, you need two pieces of
  10241. * information: the timescale unit and the earliest base media decode
  10242. * time. Multiple timescales can be specified within an MP4 but the
  10243. * base media decode time is always expressed in the timescale from
  10244. * the media header box for the track:
  10245. * ```
  10246. * moov > trak > mdia > mdhd.timescale
  10247. * ```
  10248. * @param init {Uint8Array} the bytes of the init segment
  10249. * @return {object} a hash of track ids to timescale values or null if
  10250. * the init segment is malformed.
  10251. */
  10252. timescale = function (init) {
  10253. var result = {},
  10254. traks = findBox(init, ['moov', 'trak']); // mdhd timescale
  10255. return traks.reduce(function (result, trak) {
  10256. var tkhd, version, index, id, mdhd;
  10257. tkhd = findBox(trak, ['tkhd'])[0];
  10258. if (!tkhd) {
  10259. return null;
  10260. }
  10261. version = tkhd[0];
  10262. index = version === 0 ? 12 : 20;
  10263. id = toUnsigned(tkhd[index] << 24 | tkhd[index + 1] << 16 | tkhd[index + 2] << 8 | tkhd[index + 3]);
  10264. mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
  10265. if (!mdhd) {
  10266. return null;
  10267. }
  10268. version = mdhd[0];
  10269. index = version === 0 ? 12 : 20;
  10270. result[id] = toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  10271. return result;
  10272. }, result);
  10273. };
  10274. /**
  10275. * Determine the base media decode start time, in seconds, for an MP4
  10276. * fragment. If multiple fragments are specified, the earliest time is
  10277. * returned.
  10278. *
  10279. * The base media decode time can be parsed from track fragment
  10280. * metadata:
  10281. * ```
  10282. * moof > traf > tfdt.baseMediaDecodeTime
  10283. * ```
  10284. * It requires the timescale value from the mdhd to interpret.
  10285. *
  10286. * @param timescale {object} a hash of track ids to timescale values.
  10287. * @return {number} the earliest base media decode start time for the
  10288. * fragment, in seconds
  10289. */
  10290. startTime = function (timescale, fragment) {
  10291. var trafs; // we need info from two childrend of each track fragment box
  10292. trafs = findBox(fragment, ['moof', 'traf']); // determine the start times for each track
  10293. var lowestTime = trafs.reduce(function (acc, traf) {
  10294. var tfhd = findBox(traf, ['tfhd'])[0]; // get the track id from the tfhd
  10295. var id = toUnsigned(tfhd[4] << 24 | tfhd[5] << 16 | tfhd[6] << 8 | tfhd[7]); // assume a 90kHz clock if no timescale was specified
  10296. var scale = timescale[id] || 90e3; // get the base media decode time from the tfdt
  10297. var tfdt = findBox(traf, ['tfdt'])[0];
  10298. var dv = new DataView(tfdt.buffer, tfdt.byteOffset, tfdt.byteLength);
  10299. var baseTime; // version 1 is 64 bit
  10300. if (tfdt[0] === 1) {
  10301. baseTime = getUint64(tfdt.subarray(4, 12));
  10302. } else {
  10303. baseTime = dv.getUint32(4);
  10304. } // convert base time to seconds if it is a valid number.
  10305. let seconds;
  10306. if (typeof baseTime === 'bigint') {
  10307. seconds = baseTime / window$1.BigInt(scale);
  10308. } else if (typeof baseTime === 'number' && !isNaN(baseTime)) {
  10309. seconds = baseTime / scale;
  10310. }
  10311. if (seconds < Number.MAX_SAFE_INTEGER) {
  10312. seconds = Number(seconds);
  10313. }
  10314. if (seconds < acc) {
  10315. acc = seconds;
  10316. }
  10317. return acc;
  10318. }, Infinity);
  10319. return typeof lowestTime === 'bigint' || isFinite(lowestTime) ? lowestTime : 0;
  10320. };
  10321. /**
  10322. * Determine the composition start, in seconds, for an MP4
  10323. * fragment.
  10324. *
  10325. * The composition start time of a fragment can be calculated using the base
  10326. * media decode time, composition time offset, and timescale, as follows:
  10327. *
  10328. * compositionStartTime = (baseMediaDecodeTime + compositionTimeOffset) / timescale
  10329. *
  10330. * All of the aforementioned information is contained within a media fragment's
  10331. * `traf` box, except for timescale info, which comes from the initialization
  10332. * segment, so a track id (also contained within a `traf`) is also necessary to
  10333. * associate it with a timescale
  10334. *
  10335. *
  10336. * @param timescales {object} - a hash of track ids to timescale values.
  10337. * @param fragment {Unit8Array} - the bytes of a media segment
  10338. * @return {number} the composition start time for the fragment, in seconds
  10339. **/
  10340. compositionStartTime = function (timescales, fragment) {
  10341. var trafBoxes = findBox(fragment, ['moof', 'traf']);
  10342. var baseMediaDecodeTime = 0;
  10343. var compositionTimeOffset = 0;
  10344. var trackId;
  10345. if (trafBoxes && trafBoxes.length) {
  10346. // The spec states that track run samples contained within a `traf` box are contiguous, but
  10347. // it does not explicitly state whether the `traf` boxes themselves are contiguous.
  10348. // We will assume that they are, so we only need the first to calculate start time.
  10349. var tfhd = findBox(trafBoxes[0], ['tfhd'])[0];
  10350. var trun = findBox(trafBoxes[0], ['trun'])[0];
  10351. var tfdt = findBox(trafBoxes[0], ['tfdt'])[0];
  10352. if (tfhd) {
  10353. var parsedTfhd = parseTfhd(tfhd);
  10354. trackId = parsedTfhd.trackId;
  10355. }
  10356. if (tfdt) {
  10357. var parsedTfdt = parseTfdt(tfdt);
  10358. baseMediaDecodeTime = parsedTfdt.baseMediaDecodeTime;
  10359. }
  10360. if (trun) {
  10361. var parsedTrun = parseTrun(trun);
  10362. if (parsedTrun.samples && parsedTrun.samples.length) {
  10363. compositionTimeOffset = parsedTrun.samples[0].compositionTimeOffset || 0;
  10364. }
  10365. }
  10366. } // Get timescale for this specific track. Assume a 90kHz clock if no timescale was
  10367. // specified.
  10368. var timescale = timescales[trackId] || 90e3; // return the composition start time, in seconds
  10369. if (typeof baseMediaDecodeTime === 'bigint') {
  10370. compositionTimeOffset = window$1.BigInt(compositionTimeOffset);
  10371. timescale = window$1.BigInt(timescale);
  10372. }
  10373. var result = (baseMediaDecodeTime + compositionTimeOffset) / timescale;
  10374. if (typeof result === 'bigint' && result < Number.MAX_SAFE_INTEGER) {
  10375. result = Number(result);
  10376. }
  10377. return result;
  10378. };
  10379. /**
  10380. * Find the trackIds of the video tracks in this source.
  10381. * Found by parsing the Handler Reference and Track Header Boxes:
  10382. * moov > trak > mdia > hdlr
  10383. * moov > trak > tkhd
  10384. *
  10385. * @param {Uint8Array} init - The bytes of the init segment for this source
  10386. * @return {Number[]} A list of trackIds
  10387. *
  10388. * @see ISO-BMFF-12/2015, Section 8.4.3
  10389. **/
  10390. getVideoTrackIds = function (init) {
  10391. var traks = findBox(init, ['moov', 'trak']);
  10392. var videoTrackIds = [];
  10393. traks.forEach(function (trak) {
  10394. var hdlrs = findBox(trak, ['mdia', 'hdlr']);
  10395. var tkhds = findBox(trak, ['tkhd']);
  10396. hdlrs.forEach(function (hdlr, index) {
  10397. var handlerType = parseType$1(hdlr.subarray(8, 12));
  10398. var tkhd = tkhds[index];
  10399. var view;
  10400. var version;
  10401. var trackId;
  10402. if (handlerType === 'vide') {
  10403. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  10404. version = view.getUint8(0);
  10405. trackId = version === 0 ? view.getUint32(12) : view.getUint32(20);
  10406. videoTrackIds.push(trackId);
  10407. }
  10408. });
  10409. });
  10410. return videoTrackIds;
  10411. };
  10412. getTimescaleFromMediaHeader = function (mdhd) {
  10413. // mdhd is a FullBox, meaning it will have its own version as the first byte
  10414. var version = mdhd[0];
  10415. var index = version === 0 ? 12 : 20;
  10416. return toUnsigned(mdhd[index] << 24 | mdhd[index + 1] << 16 | mdhd[index + 2] << 8 | mdhd[index + 3]);
  10417. };
  10418. /**
  10419. * Get all the video, audio, and hint tracks from a non fragmented
  10420. * mp4 segment
  10421. */
  10422. getTracks = function (init) {
  10423. var traks = findBox(init, ['moov', 'trak']);
  10424. var tracks = [];
  10425. traks.forEach(function (trak) {
  10426. var track = {};
  10427. var tkhd = findBox(trak, ['tkhd'])[0];
  10428. var view, tkhdVersion; // id
  10429. if (tkhd) {
  10430. view = new DataView(tkhd.buffer, tkhd.byteOffset, tkhd.byteLength);
  10431. tkhdVersion = view.getUint8(0);
  10432. track.id = tkhdVersion === 0 ? view.getUint32(12) : view.getUint32(20);
  10433. }
  10434. var hdlr = findBox(trak, ['mdia', 'hdlr'])[0]; // type
  10435. if (hdlr) {
  10436. var type = parseType$1(hdlr.subarray(8, 12));
  10437. if (type === 'vide') {
  10438. track.type = 'video';
  10439. } else if (type === 'soun') {
  10440. track.type = 'audio';
  10441. } else {
  10442. track.type = type;
  10443. }
  10444. } // codec
  10445. var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
  10446. if (stsd) {
  10447. var sampleDescriptions = stsd.subarray(8); // gives the codec type string
  10448. track.codec = parseType$1(sampleDescriptions.subarray(4, 8));
  10449. var codecBox = findBox(sampleDescriptions, [track.codec])[0];
  10450. var codecConfig, codecConfigType;
  10451. if (codecBox) {
  10452. // https://tools.ietf.org/html/rfc6381#section-3.3
  10453. if (/^[asm]vc[1-9]$/i.test(track.codec)) {
  10454. // we don't need anything but the "config" parameter of the
  10455. // avc1 codecBox
  10456. codecConfig = codecBox.subarray(78);
  10457. codecConfigType = parseType$1(codecConfig.subarray(4, 8));
  10458. if (codecConfigType === 'avcC' && codecConfig.length > 11) {
  10459. track.codec += '.'; // left padded with zeroes for single digit hex
  10460. // profile idc
  10461. track.codec += toHexString(codecConfig[9]); // the byte containing the constraint_set flags
  10462. track.codec += toHexString(codecConfig[10]); // level idc
  10463. track.codec += toHexString(codecConfig[11]);
  10464. } else {
  10465. // TODO: show a warning that we couldn't parse the codec
  10466. // and are using the default
  10467. track.codec = 'avc1.4d400d';
  10468. }
  10469. } else if (/^mp4[a,v]$/i.test(track.codec)) {
  10470. // we do not need anything but the streamDescriptor of the mp4a codecBox
  10471. codecConfig = codecBox.subarray(28);
  10472. codecConfigType = parseType$1(codecConfig.subarray(4, 8));
  10473. if (codecConfigType === 'esds' && codecConfig.length > 20 && codecConfig[19] !== 0) {
  10474. track.codec += '.' + toHexString(codecConfig[19]); // this value is only a single digit
  10475. track.codec += '.' + toHexString(codecConfig[20] >>> 2 & 0x3f).replace(/^0/, '');
  10476. } else {
  10477. // TODO: show a warning that we couldn't parse the codec
  10478. // and are using the default
  10479. track.codec = 'mp4a.40.2';
  10480. }
  10481. } else {
  10482. // flac, opus, etc
  10483. track.codec = track.codec.toLowerCase();
  10484. }
  10485. }
  10486. }
  10487. var mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
  10488. if (mdhd) {
  10489. track.timescale = getTimescaleFromMediaHeader(mdhd);
  10490. }
  10491. tracks.push(track);
  10492. });
  10493. return tracks;
  10494. };
  10495. /**
  10496. * Returns an array of emsg ID3 data from the provided segmentData.
  10497. * An offset can also be provided as the Latest Arrival Time to calculate
  10498. * the Event Start Time of v0 EMSG boxes.
  10499. * See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing
  10500. *
  10501. * @param {Uint8Array} segmentData the segment byte array.
  10502. * @param {number} offset the segment start time or Latest Arrival Time,
  10503. * @return {Object[]} an array of ID3 parsed from EMSG boxes
  10504. */
  10505. getEmsgID3 = function (segmentData, offset = 0) {
  10506. var emsgBoxes = findBox(segmentData, ['emsg']);
  10507. return emsgBoxes.map(data => {
  10508. var parsedBox = emsg.parseEmsgBox(new Uint8Array(data));
  10509. var parsedId3Frames = parseId3Frames(parsedBox.message_data);
  10510. return {
  10511. cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset),
  10512. duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale),
  10513. frames: parsedId3Frames
  10514. };
  10515. });
  10516. };
  10517. var probe$2 = {
  10518. // export mp4 inspector's findBox and parseType for backwards compatibility
  10519. findBox: findBox,
  10520. parseType: parseType$1,
  10521. timescale: timescale,
  10522. startTime: startTime,
  10523. compositionStartTime: compositionStartTime,
  10524. videoTrackIds: getVideoTrackIds,
  10525. tracks: getTracks,
  10526. getTimescaleFromMediaHeader: getTimescaleFromMediaHeader,
  10527. getEmsgID3: getEmsgID3
  10528. };
  10529. /**
  10530. * mux.js
  10531. *
  10532. * Copyright (c) Brightcove
  10533. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  10534. *
  10535. * Utilities to detect basic properties and metadata about TS Segments.
  10536. */
  10537. var StreamTypes$1 = streamTypes;
  10538. var parsePid = function (packet) {
  10539. var pid = packet[1] & 0x1f;
  10540. pid <<= 8;
  10541. pid |= packet[2];
  10542. return pid;
  10543. };
  10544. var parsePayloadUnitStartIndicator = function (packet) {
  10545. return !!(packet[1] & 0x40);
  10546. };
  10547. var parseAdaptionField = function (packet) {
  10548. var offset = 0; // if an adaption field is present, its length is specified by the
  10549. // fifth byte of the TS packet header. The adaptation field is
  10550. // used to add stuffing to PES packets that don't fill a complete
  10551. // TS packet, and to specify some forms of timing and control data
  10552. // that we do not currently use.
  10553. if ((packet[3] & 0x30) >>> 4 > 0x01) {
  10554. offset += packet[4] + 1;
  10555. }
  10556. return offset;
  10557. };
  10558. var parseType = function (packet, pmtPid) {
  10559. var pid = parsePid(packet);
  10560. if (pid === 0) {
  10561. return 'pat';
  10562. } else if (pid === pmtPid) {
  10563. return 'pmt';
  10564. } else if (pmtPid) {
  10565. return 'pes';
  10566. }
  10567. return null;
  10568. };
  10569. var parsePat = function (packet) {
  10570. var pusi = parsePayloadUnitStartIndicator(packet);
  10571. var offset = 4 + parseAdaptionField(packet);
  10572. if (pusi) {
  10573. offset += packet[offset] + 1;
  10574. }
  10575. return (packet[offset + 10] & 0x1f) << 8 | packet[offset + 11];
  10576. };
  10577. var parsePmt = function (packet) {
  10578. var programMapTable = {};
  10579. var pusi = parsePayloadUnitStartIndicator(packet);
  10580. var payloadOffset = 4 + parseAdaptionField(packet);
  10581. if (pusi) {
  10582. payloadOffset += packet[payloadOffset] + 1;
  10583. } // PMTs can be sent ahead of the time when they should actually
  10584. // take effect. We don't believe this should ever be the case
  10585. // for HLS but we'll ignore "forward" PMT declarations if we see
  10586. // them. Future PMT declarations have the current_next_indicator
  10587. // set to zero.
  10588. if (!(packet[payloadOffset + 5] & 0x01)) {
  10589. return;
  10590. }
  10591. var sectionLength, tableEnd, programInfoLength; // the mapping table ends at the end of the current section
  10592. sectionLength = (packet[payloadOffset + 1] & 0x0f) << 8 | packet[payloadOffset + 2];
  10593. tableEnd = 3 + sectionLength - 4; // to determine where the table is, we have to figure out how
  10594. // long the program info descriptors are
  10595. programInfoLength = (packet[payloadOffset + 10] & 0x0f) << 8 | packet[payloadOffset + 11]; // advance the offset to the first entry in the mapping table
  10596. var offset = 12 + programInfoLength;
  10597. while (offset < tableEnd) {
  10598. var i = payloadOffset + offset; // add an entry that maps the elementary_pid to the stream_type
  10599. programMapTable[(packet[i + 1] & 0x1F) << 8 | packet[i + 2]] = packet[i]; // move to the next table entry
  10600. // skip past the elementary stream descriptors, if present
  10601. offset += ((packet[i + 3] & 0x0F) << 8 | packet[i + 4]) + 5;
  10602. }
  10603. return programMapTable;
  10604. };
  10605. var parsePesType = function (packet, programMapTable) {
  10606. var pid = parsePid(packet);
  10607. var type = programMapTable[pid];
  10608. switch (type) {
  10609. case StreamTypes$1.H264_STREAM_TYPE:
  10610. return 'video';
  10611. case StreamTypes$1.ADTS_STREAM_TYPE:
  10612. return 'audio';
  10613. case StreamTypes$1.METADATA_STREAM_TYPE:
  10614. return 'timed-metadata';
  10615. default:
  10616. return null;
  10617. }
  10618. };
  10619. var parsePesTime = function (packet) {
  10620. var pusi = parsePayloadUnitStartIndicator(packet);
  10621. if (!pusi) {
  10622. return null;
  10623. }
  10624. var offset = 4 + parseAdaptionField(packet);
  10625. if (offset >= packet.byteLength) {
  10626. // From the H 222.0 MPEG-TS spec
  10627. // "For transport stream packets carrying PES packets, stuffing is needed when there
  10628. // is insufficient PES packet data to completely fill the transport stream packet
  10629. // payload bytes. Stuffing is accomplished by defining an adaptation field longer than
  10630. // the sum of the lengths of the data elements in it, so that the payload bytes
  10631. // remaining after the adaptation field exactly accommodates the available PES packet
  10632. // data."
  10633. //
  10634. // If the offset is >= the length of the packet, then the packet contains no data
  10635. // and instead is just adaption field stuffing bytes
  10636. return null;
  10637. }
  10638. var pes = null;
  10639. var ptsDtsFlags; // PES packets may be annotated with a PTS value, or a PTS value
  10640. // and a DTS value. Determine what combination of values is
  10641. // available to work with.
  10642. ptsDtsFlags = packet[offset + 7]; // PTS and DTS are normally stored as a 33-bit number. Javascript
  10643. // performs all bitwise operations on 32-bit integers but javascript
  10644. // supports a much greater range (52-bits) of integer using standard
  10645. // mathematical operations.
  10646. // We construct a 31-bit value using bitwise operators over the 31
  10647. // most significant bits and then multiply by 4 (equal to a left-shift
  10648. // of 2) before we add the final 2 least significant bits of the
  10649. // timestamp (equal to an OR.)
  10650. if (ptsDtsFlags & 0xC0) {
  10651. pes = {}; // the PTS and DTS are not written out directly. For information
  10652. // on how they are encoded, see
  10653. // http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  10654. pes.pts = (packet[offset + 9] & 0x0E) << 27 | (packet[offset + 10] & 0xFF) << 20 | (packet[offset + 11] & 0xFE) << 12 | (packet[offset + 12] & 0xFF) << 5 | (packet[offset + 13] & 0xFE) >>> 3;
  10655. pes.pts *= 4; // Left shift by 2
  10656. pes.pts += (packet[offset + 13] & 0x06) >>> 1; // OR by the two LSBs
  10657. pes.dts = pes.pts;
  10658. if (ptsDtsFlags & 0x40) {
  10659. pes.dts = (packet[offset + 14] & 0x0E) << 27 | (packet[offset + 15] & 0xFF) << 20 | (packet[offset + 16] & 0xFE) << 12 | (packet[offset + 17] & 0xFF) << 5 | (packet[offset + 18] & 0xFE) >>> 3;
  10660. pes.dts *= 4; // Left shift by 2
  10661. pes.dts += (packet[offset + 18] & 0x06) >>> 1; // OR by the two LSBs
  10662. }
  10663. }
  10664. return pes;
  10665. };
  10666. var parseNalUnitType = function (type) {
  10667. switch (type) {
  10668. case 0x05:
  10669. return 'slice_layer_without_partitioning_rbsp_idr';
  10670. case 0x06:
  10671. return 'sei_rbsp';
  10672. case 0x07:
  10673. return 'seq_parameter_set_rbsp';
  10674. case 0x08:
  10675. return 'pic_parameter_set_rbsp';
  10676. case 0x09:
  10677. return 'access_unit_delimiter_rbsp';
  10678. default:
  10679. return null;
  10680. }
  10681. };
  10682. var videoPacketContainsKeyFrame = function (packet) {
  10683. var offset = 4 + parseAdaptionField(packet);
  10684. var frameBuffer = packet.subarray(offset);
  10685. var frameI = 0;
  10686. var frameSyncPoint = 0;
  10687. var foundKeyFrame = false;
  10688. var nalType; // advance the sync point to a NAL start, if necessary
  10689. for (; frameSyncPoint < frameBuffer.byteLength - 3; frameSyncPoint++) {
  10690. if (frameBuffer[frameSyncPoint + 2] === 1) {
  10691. // the sync point is properly aligned
  10692. frameI = frameSyncPoint + 5;
  10693. break;
  10694. }
  10695. }
  10696. while (frameI < frameBuffer.byteLength) {
  10697. // look at the current byte to determine if we've hit the end of
  10698. // a NAL unit boundary
  10699. switch (frameBuffer[frameI]) {
  10700. case 0:
  10701. // skip past non-sync sequences
  10702. if (frameBuffer[frameI - 1] !== 0) {
  10703. frameI += 2;
  10704. break;
  10705. } else if (frameBuffer[frameI - 2] !== 0) {
  10706. frameI++;
  10707. break;
  10708. }
  10709. if (frameSyncPoint + 3 !== frameI - 2) {
  10710. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  10711. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  10712. foundKeyFrame = true;
  10713. }
  10714. } // drop trailing zeroes
  10715. do {
  10716. frameI++;
  10717. } while (frameBuffer[frameI] !== 1 && frameI < frameBuffer.length);
  10718. frameSyncPoint = frameI - 2;
  10719. frameI += 3;
  10720. break;
  10721. case 1:
  10722. // skip past non-sync sequences
  10723. if (frameBuffer[frameI - 1] !== 0 || frameBuffer[frameI - 2] !== 0) {
  10724. frameI += 3;
  10725. break;
  10726. }
  10727. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  10728. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  10729. foundKeyFrame = true;
  10730. }
  10731. frameSyncPoint = frameI - 2;
  10732. frameI += 3;
  10733. break;
  10734. default:
  10735. // the current byte isn't a one or zero, so it cannot be part
  10736. // of a sync sequence
  10737. frameI += 3;
  10738. break;
  10739. }
  10740. }
  10741. frameBuffer = frameBuffer.subarray(frameSyncPoint);
  10742. frameI -= frameSyncPoint;
  10743. frameSyncPoint = 0; // parse the final nal
  10744. if (frameBuffer && frameBuffer.byteLength > 3) {
  10745. nalType = parseNalUnitType(frameBuffer[frameSyncPoint + 3] & 0x1f);
  10746. if (nalType === 'slice_layer_without_partitioning_rbsp_idr') {
  10747. foundKeyFrame = true;
  10748. }
  10749. }
  10750. return foundKeyFrame;
  10751. };
  10752. var probe$1 = {
  10753. parseType: parseType,
  10754. parsePat: parsePat,
  10755. parsePmt: parsePmt,
  10756. parsePayloadUnitStartIndicator: parsePayloadUnitStartIndicator,
  10757. parsePesType: parsePesType,
  10758. parsePesTime: parsePesTime,
  10759. videoPacketContainsKeyFrame: videoPacketContainsKeyFrame
  10760. };
  10761. /**
  10762. * mux.js
  10763. *
  10764. * Copyright (c) Brightcove
  10765. * Licensed Apache-2.0 https://github.com/videojs/mux.js/blob/master/LICENSE
  10766. *
  10767. * Parse mpeg2 transport stream packets to extract basic timing information
  10768. */
  10769. var StreamTypes = streamTypes;
  10770. var handleRollover = timestampRolloverStream.handleRollover;
  10771. var probe = {};
  10772. probe.ts = probe$1;
  10773. probe.aac = utils;
  10774. var ONE_SECOND_IN_TS = clock$2.ONE_SECOND_IN_TS;
  10775. var MP2T_PACKET_LENGTH = 188,
  10776. // bytes
  10777. SYNC_BYTE = 0x47;
  10778. /**
  10779. * walks through segment data looking for pat and pmt packets to parse out
  10780. * program map table information
  10781. */
  10782. var parsePsi_ = function (bytes, pmt) {
  10783. var startIndex = 0,
  10784. endIndex = MP2T_PACKET_LENGTH,
  10785. packet,
  10786. type;
  10787. while (endIndex < bytes.byteLength) {
  10788. // Look for a pair of start and end sync bytes in the data..
  10789. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  10790. // We found a packet
  10791. packet = bytes.subarray(startIndex, endIndex);
  10792. type = probe.ts.parseType(packet, pmt.pid);
  10793. switch (type) {
  10794. case 'pat':
  10795. pmt.pid = probe.ts.parsePat(packet);
  10796. break;
  10797. case 'pmt':
  10798. var table = probe.ts.parsePmt(packet);
  10799. pmt.table = pmt.table || {};
  10800. Object.keys(table).forEach(function (key) {
  10801. pmt.table[key] = table[key];
  10802. });
  10803. break;
  10804. }
  10805. startIndex += MP2T_PACKET_LENGTH;
  10806. endIndex += MP2T_PACKET_LENGTH;
  10807. continue;
  10808. } // If we get here, we have somehow become de-synchronized and we need to step
  10809. // forward one byte at a time until we find a pair of sync bytes that denote
  10810. // a packet
  10811. startIndex++;
  10812. endIndex++;
  10813. }
  10814. };
  10815. /**
  10816. * walks through the segment data from the start and end to get timing information
  10817. * for the first and last audio pes packets
  10818. */
  10819. var parseAudioPes_ = function (bytes, pmt, result) {
  10820. var startIndex = 0,
  10821. endIndex = MP2T_PACKET_LENGTH,
  10822. packet,
  10823. type,
  10824. pesType,
  10825. pusi,
  10826. parsed;
  10827. var endLoop = false; // Start walking from start of segment to get first audio packet
  10828. while (endIndex <= bytes.byteLength) {
  10829. // Look for a pair of start and end sync bytes in the data..
  10830. if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
  10831. // We found a packet
  10832. packet = bytes.subarray(startIndex, endIndex);
  10833. type = probe.ts.parseType(packet, pmt.pid);
  10834. switch (type) {
  10835. case 'pes':
  10836. pesType = probe.ts.parsePesType(packet, pmt.table);
  10837. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  10838. if (pesType === 'audio' && pusi) {
  10839. parsed = probe.ts.parsePesTime(packet);
  10840. if (parsed) {
  10841. parsed.type = 'audio';
  10842. result.audio.push(parsed);
  10843. endLoop = true;
  10844. }
  10845. }
  10846. break;
  10847. }
  10848. if (endLoop) {
  10849. break;
  10850. }
  10851. startIndex += MP2T_PACKET_LENGTH;
  10852. endIndex += MP2T_PACKET_LENGTH;
  10853. continue;
  10854. } // If we get here, we have somehow become de-synchronized and we need to step
  10855. // forward one byte at a time until we find a pair of sync bytes that denote
  10856. // a packet
  10857. startIndex++;
  10858. endIndex++;
  10859. } // Start walking from end of segment to get last audio packet
  10860. endIndex = bytes.byteLength;
  10861. startIndex = endIndex - MP2T_PACKET_LENGTH;
  10862. endLoop = false;
  10863. while (startIndex >= 0) {
  10864. // Look for a pair of start and end sync bytes in the data..
  10865. if (bytes[startIndex] === SYNC_BYTE && (bytes[endIndex] === SYNC_BYTE || endIndex === bytes.byteLength)) {
  10866. // We found a packet
  10867. packet = bytes.subarray(startIndex, endIndex);
  10868. type = probe.ts.parseType(packet, pmt.pid);
  10869. switch (type) {
  10870. case 'pes':
  10871. pesType = probe.ts.parsePesType(packet, pmt.table);
  10872. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  10873. if (pesType === 'audio' && pusi) {
  10874. parsed = probe.ts.parsePesTime(packet);
  10875. if (parsed) {
  10876. parsed.type = 'audio';
  10877. result.audio.push(parsed);
  10878. endLoop = true;
  10879. }
  10880. }
  10881. break;
  10882. }
  10883. if (endLoop) {
  10884. break;
  10885. }
  10886. startIndex -= MP2T_PACKET_LENGTH;
  10887. endIndex -= MP2T_PACKET_LENGTH;
  10888. continue;
  10889. } // If we get here, we have somehow become de-synchronized and we need to step
  10890. // forward one byte at a time until we find a pair of sync bytes that denote
  10891. // a packet
  10892. startIndex--;
  10893. endIndex--;
  10894. }
  10895. };
  10896. /**
  10897. * walks through the segment data from the start and end to get timing information
  10898. * for the first and last video pes packets as well as timing information for the first
  10899. * key frame.
  10900. */
  10901. var parseVideoPes_ = function (bytes, pmt, result) {
  10902. var startIndex = 0,
  10903. endIndex = MP2T_PACKET_LENGTH,
  10904. packet,
  10905. type,
  10906. pesType,
  10907. pusi,
  10908. parsed,
  10909. frame,
  10910. i,
  10911. pes;
  10912. var endLoop = false;
  10913. var currentFrame = {
  10914. data: [],
  10915. size: 0
  10916. }; // Start walking from start of segment to get first video packet
  10917. while (endIndex < bytes.byteLength) {
  10918. // Look for a pair of start and end sync bytes in the data..
  10919. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  10920. // We found a packet
  10921. packet = bytes.subarray(startIndex, endIndex);
  10922. type = probe.ts.parseType(packet, pmt.pid);
  10923. switch (type) {
  10924. case 'pes':
  10925. pesType = probe.ts.parsePesType(packet, pmt.table);
  10926. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  10927. if (pesType === 'video') {
  10928. if (pusi && !endLoop) {
  10929. parsed = probe.ts.parsePesTime(packet);
  10930. if (parsed) {
  10931. parsed.type = 'video';
  10932. result.video.push(parsed);
  10933. endLoop = true;
  10934. }
  10935. }
  10936. if (!result.firstKeyFrame) {
  10937. if (pusi) {
  10938. if (currentFrame.size !== 0) {
  10939. frame = new Uint8Array(currentFrame.size);
  10940. i = 0;
  10941. while (currentFrame.data.length) {
  10942. pes = currentFrame.data.shift();
  10943. frame.set(pes, i);
  10944. i += pes.byteLength;
  10945. }
  10946. if (probe.ts.videoPacketContainsKeyFrame(frame)) {
  10947. var firstKeyFrame = probe.ts.parsePesTime(frame); // PTS/DTS may not be available. Simply *not* setting
  10948. // the keyframe seems to work fine with HLS playback
  10949. // and definitely preferable to a crash with TypeError...
  10950. if (firstKeyFrame) {
  10951. result.firstKeyFrame = firstKeyFrame;
  10952. result.firstKeyFrame.type = 'video';
  10953. } else {
  10954. // eslint-disable-next-line
  10955. console.warn('Failed to extract PTS/DTS from PES at first keyframe. ' + 'This could be an unusual TS segment, or else mux.js did not ' + 'parse your TS segment correctly. If you know your TS ' + 'segments do contain PTS/DTS on keyframes please file a bug ' + 'report! You can try ffprobe to double check for yourself.');
  10956. }
  10957. }
  10958. currentFrame.size = 0;
  10959. }
  10960. }
  10961. currentFrame.data.push(packet);
  10962. currentFrame.size += packet.byteLength;
  10963. }
  10964. }
  10965. break;
  10966. }
  10967. if (endLoop && result.firstKeyFrame) {
  10968. break;
  10969. }
  10970. startIndex += MP2T_PACKET_LENGTH;
  10971. endIndex += MP2T_PACKET_LENGTH;
  10972. continue;
  10973. } // If we get here, we have somehow become de-synchronized and we need to step
  10974. // forward one byte at a time until we find a pair of sync bytes that denote
  10975. // a packet
  10976. startIndex++;
  10977. endIndex++;
  10978. } // Start walking from end of segment to get last video packet
  10979. endIndex = bytes.byteLength;
  10980. startIndex = endIndex - MP2T_PACKET_LENGTH;
  10981. endLoop = false;
  10982. while (startIndex >= 0) {
  10983. // Look for a pair of start and end sync bytes in the data..
  10984. if (bytes[startIndex] === SYNC_BYTE && bytes[endIndex] === SYNC_BYTE) {
  10985. // We found a packet
  10986. packet = bytes.subarray(startIndex, endIndex);
  10987. type = probe.ts.parseType(packet, pmt.pid);
  10988. switch (type) {
  10989. case 'pes':
  10990. pesType = probe.ts.parsePesType(packet, pmt.table);
  10991. pusi = probe.ts.parsePayloadUnitStartIndicator(packet);
  10992. if (pesType === 'video' && pusi) {
  10993. parsed = probe.ts.parsePesTime(packet);
  10994. if (parsed) {
  10995. parsed.type = 'video';
  10996. result.video.push(parsed);
  10997. endLoop = true;
  10998. }
  10999. }
  11000. break;
  11001. }
  11002. if (endLoop) {
  11003. break;
  11004. }
  11005. startIndex -= MP2T_PACKET_LENGTH;
  11006. endIndex -= MP2T_PACKET_LENGTH;
  11007. continue;
  11008. } // If we get here, we have somehow become de-synchronized and we need to step
  11009. // forward one byte at a time until we find a pair of sync bytes that denote
  11010. // a packet
  11011. startIndex--;
  11012. endIndex--;
  11013. }
  11014. };
  11015. /**
  11016. * Adjusts the timestamp information for the segment to account for
  11017. * rollover and convert to seconds based on pes packet timescale (90khz clock)
  11018. */
  11019. var adjustTimestamp_ = function (segmentInfo, baseTimestamp) {
  11020. if (segmentInfo.audio && segmentInfo.audio.length) {
  11021. var audioBaseTimestamp = baseTimestamp;
  11022. if (typeof audioBaseTimestamp === 'undefined' || isNaN(audioBaseTimestamp)) {
  11023. audioBaseTimestamp = segmentInfo.audio[0].dts;
  11024. }
  11025. segmentInfo.audio.forEach(function (info) {
  11026. info.dts = handleRollover(info.dts, audioBaseTimestamp);
  11027. info.pts = handleRollover(info.pts, audioBaseTimestamp); // time in seconds
  11028. info.dtsTime = info.dts / ONE_SECOND_IN_TS;
  11029. info.ptsTime = info.pts / ONE_SECOND_IN_TS;
  11030. });
  11031. }
  11032. if (segmentInfo.video && segmentInfo.video.length) {
  11033. var videoBaseTimestamp = baseTimestamp;
  11034. if (typeof videoBaseTimestamp === 'undefined' || isNaN(videoBaseTimestamp)) {
  11035. videoBaseTimestamp = segmentInfo.video[0].dts;
  11036. }
  11037. segmentInfo.video.forEach(function (info) {
  11038. info.dts = handleRollover(info.dts, videoBaseTimestamp);
  11039. info.pts = handleRollover(info.pts, videoBaseTimestamp); // time in seconds
  11040. info.dtsTime = info.dts / ONE_SECOND_IN_TS;
  11041. info.ptsTime = info.pts / ONE_SECOND_IN_TS;
  11042. });
  11043. if (segmentInfo.firstKeyFrame) {
  11044. var frame = segmentInfo.firstKeyFrame;
  11045. frame.dts = handleRollover(frame.dts, videoBaseTimestamp);
  11046. frame.pts = handleRollover(frame.pts, videoBaseTimestamp); // time in seconds
  11047. frame.dtsTime = frame.dts / ONE_SECOND_IN_TS;
  11048. frame.ptsTime = frame.pts / ONE_SECOND_IN_TS;
  11049. }
  11050. }
  11051. };
  11052. /**
  11053. * inspects the aac data stream for start and end time information
  11054. */
  11055. var inspectAac_ = function (bytes) {
  11056. var endLoop = false,
  11057. audioCount = 0,
  11058. sampleRate = null,
  11059. timestamp = null,
  11060. frameSize = 0,
  11061. byteIndex = 0,
  11062. packet;
  11063. while (bytes.length - byteIndex >= 3) {
  11064. var type = probe.aac.parseType(bytes, byteIndex);
  11065. switch (type) {
  11066. case 'timed-metadata':
  11067. // Exit early because we don't have enough to parse
  11068. // the ID3 tag header
  11069. if (bytes.length - byteIndex < 10) {
  11070. endLoop = true;
  11071. break;
  11072. }
  11073. frameSize = probe.aac.parseId3TagSize(bytes, byteIndex); // Exit early if we don't have enough in the buffer
  11074. // to emit a full packet
  11075. if (frameSize > bytes.length) {
  11076. endLoop = true;
  11077. break;
  11078. }
  11079. if (timestamp === null) {
  11080. packet = bytes.subarray(byteIndex, byteIndex + frameSize);
  11081. timestamp = probe.aac.parseAacTimestamp(packet);
  11082. }
  11083. byteIndex += frameSize;
  11084. break;
  11085. case 'audio':
  11086. // Exit early because we don't have enough to parse
  11087. // the ADTS frame header
  11088. if (bytes.length - byteIndex < 7) {
  11089. endLoop = true;
  11090. break;
  11091. }
  11092. frameSize = probe.aac.parseAdtsSize(bytes, byteIndex); // Exit early if we don't have enough in the buffer
  11093. // to emit a full packet
  11094. if (frameSize > bytes.length) {
  11095. endLoop = true;
  11096. break;
  11097. }
  11098. if (sampleRate === null) {
  11099. packet = bytes.subarray(byteIndex, byteIndex + frameSize);
  11100. sampleRate = probe.aac.parseSampleRate(packet);
  11101. }
  11102. audioCount++;
  11103. byteIndex += frameSize;
  11104. break;
  11105. default:
  11106. byteIndex++;
  11107. break;
  11108. }
  11109. if (endLoop) {
  11110. return null;
  11111. }
  11112. }
  11113. if (sampleRate === null || timestamp === null) {
  11114. return null;
  11115. }
  11116. var audioTimescale = ONE_SECOND_IN_TS / sampleRate;
  11117. var result = {
  11118. audio: [{
  11119. type: 'audio',
  11120. dts: timestamp,
  11121. pts: timestamp
  11122. }, {
  11123. type: 'audio',
  11124. dts: timestamp + audioCount * 1024 * audioTimescale,
  11125. pts: timestamp + audioCount * 1024 * audioTimescale
  11126. }]
  11127. };
  11128. return result;
  11129. };
  11130. /**
  11131. * inspects the transport stream segment data for start and end time information
  11132. * of the audio and video tracks (when present) as well as the first key frame's
  11133. * start time.
  11134. */
  11135. var inspectTs_ = function (bytes) {
  11136. var pmt = {
  11137. pid: null,
  11138. table: null
  11139. };
  11140. var result = {};
  11141. parsePsi_(bytes, pmt);
  11142. for (var pid in pmt.table) {
  11143. if (pmt.table.hasOwnProperty(pid)) {
  11144. var type = pmt.table[pid];
  11145. switch (type) {
  11146. case StreamTypes.H264_STREAM_TYPE:
  11147. result.video = [];
  11148. parseVideoPes_(bytes, pmt, result);
  11149. if (result.video.length === 0) {
  11150. delete result.video;
  11151. }
  11152. break;
  11153. case StreamTypes.ADTS_STREAM_TYPE:
  11154. result.audio = [];
  11155. parseAudioPes_(bytes, pmt, result);
  11156. if (result.audio.length === 0) {
  11157. delete result.audio;
  11158. }
  11159. break;
  11160. }
  11161. }
  11162. }
  11163. return result;
  11164. };
  11165. /**
  11166. * Inspects segment byte data and returns an object with start and end timing information
  11167. *
  11168. * @param {Uint8Array} bytes The segment byte data
  11169. * @param {Number} baseTimestamp Relative reference timestamp used when adjusting frame
  11170. * timestamps for rollover. This value must be in 90khz clock.
  11171. * @return {Object} Object containing start and end frame timing info of segment.
  11172. */
  11173. var inspect = function (bytes, baseTimestamp) {
  11174. var isAacData = probe.aac.isLikelyAacData(bytes);
  11175. var result;
  11176. if (isAacData) {
  11177. result = inspectAac_(bytes);
  11178. } else {
  11179. result = inspectTs_(bytes);
  11180. }
  11181. if (!result || !result.audio && !result.video) {
  11182. return null;
  11183. }
  11184. adjustTimestamp_(result, baseTimestamp);
  11185. return result;
  11186. };
  11187. var tsInspector = {
  11188. inspect: inspect,
  11189. parseAudioPes_: parseAudioPes_
  11190. };
  11191. /* global self */
  11192. /**
  11193. * Re-emits transmuxer events by converting them into messages to the
  11194. * world outside the worker.
  11195. *
  11196. * @param {Object} transmuxer the transmuxer to wire events on
  11197. * @private
  11198. */
  11199. const wireTransmuxerEvents = function (self, transmuxer) {
  11200. transmuxer.on('data', function (segment) {
  11201. // transfer ownership of the underlying ArrayBuffer
  11202. // instead of doing a copy to save memory
  11203. // ArrayBuffers are transferable but generic TypedArrays are not
  11204. // @link https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Passing_data_by_transferring_ownership_(transferable_objects)
  11205. const initArray = segment.initSegment;
  11206. segment.initSegment = {
  11207. data: initArray.buffer,
  11208. byteOffset: initArray.byteOffset,
  11209. byteLength: initArray.byteLength
  11210. };
  11211. const typedArray = segment.data;
  11212. segment.data = typedArray.buffer;
  11213. self.postMessage({
  11214. action: 'data',
  11215. segment,
  11216. byteOffset: typedArray.byteOffset,
  11217. byteLength: typedArray.byteLength
  11218. }, [segment.data]);
  11219. });
  11220. transmuxer.on('done', function (data) {
  11221. self.postMessage({
  11222. action: 'done'
  11223. });
  11224. });
  11225. transmuxer.on('gopInfo', function (gopInfo) {
  11226. self.postMessage({
  11227. action: 'gopInfo',
  11228. gopInfo
  11229. });
  11230. });
  11231. transmuxer.on('videoSegmentTimingInfo', function (timingInfo) {
  11232. const videoSegmentTimingInfo = {
  11233. start: {
  11234. decode: clock$2.videoTsToSeconds(timingInfo.start.dts),
  11235. presentation: clock$2.videoTsToSeconds(timingInfo.start.pts)
  11236. },
  11237. end: {
  11238. decode: clock$2.videoTsToSeconds(timingInfo.end.dts),
  11239. presentation: clock$2.videoTsToSeconds(timingInfo.end.pts)
  11240. },
  11241. baseMediaDecodeTime: clock$2.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
  11242. };
  11243. if (timingInfo.prependedContentDuration) {
  11244. videoSegmentTimingInfo.prependedContentDuration = clock$2.videoTsToSeconds(timingInfo.prependedContentDuration);
  11245. }
  11246. self.postMessage({
  11247. action: 'videoSegmentTimingInfo',
  11248. videoSegmentTimingInfo
  11249. });
  11250. });
  11251. transmuxer.on('audioSegmentTimingInfo', function (timingInfo) {
  11252. // Note that all times for [audio/video]SegmentTimingInfo events are in video clock
  11253. const audioSegmentTimingInfo = {
  11254. start: {
  11255. decode: clock$2.videoTsToSeconds(timingInfo.start.dts),
  11256. presentation: clock$2.videoTsToSeconds(timingInfo.start.pts)
  11257. },
  11258. end: {
  11259. decode: clock$2.videoTsToSeconds(timingInfo.end.dts),
  11260. presentation: clock$2.videoTsToSeconds(timingInfo.end.pts)
  11261. },
  11262. baseMediaDecodeTime: clock$2.videoTsToSeconds(timingInfo.baseMediaDecodeTime)
  11263. };
  11264. if (timingInfo.prependedContentDuration) {
  11265. audioSegmentTimingInfo.prependedContentDuration = clock$2.videoTsToSeconds(timingInfo.prependedContentDuration);
  11266. }
  11267. self.postMessage({
  11268. action: 'audioSegmentTimingInfo',
  11269. audioSegmentTimingInfo
  11270. });
  11271. });
  11272. transmuxer.on('id3Frame', function (id3Frame) {
  11273. self.postMessage({
  11274. action: 'id3Frame',
  11275. id3Frame
  11276. });
  11277. });
  11278. transmuxer.on('caption', function (caption) {
  11279. self.postMessage({
  11280. action: 'caption',
  11281. caption
  11282. });
  11283. });
  11284. transmuxer.on('trackinfo', function (trackInfo) {
  11285. self.postMessage({
  11286. action: 'trackinfo',
  11287. trackInfo
  11288. });
  11289. });
  11290. transmuxer.on('audioTimingInfo', function (audioTimingInfo) {
  11291. // convert to video TS since we prioritize video time over audio
  11292. self.postMessage({
  11293. action: 'audioTimingInfo',
  11294. audioTimingInfo: {
  11295. start: clock$2.videoTsToSeconds(audioTimingInfo.start),
  11296. end: clock$2.videoTsToSeconds(audioTimingInfo.end)
  11297. }
  11298. });
  11299. });
  11300. transmuxer.on('videoTimingInfo', function (videoTimingInfo) {
  11301. self.postMessage({
  11302. action: 'videoTimingInfo',
  11303. videoTimingInfo: {
  11304. start: clock$2.videoTsToSeconds(videoTimingInfo.start),
  11305. end: clock$2.videoTsToSeconds(videoTimingInfo.end)
  11306. }
  11307. });
  11308. });
  11309. transmuxer.on('log', function (log) {
  11310. self.postMessage({
  11311. action: 'log',
  11312. log
  11313. });
  11314. });
  11315. };
  11316. /**
  11317. * All incoming messages route through this hash. If no function exists
  11318. * to handle an incoming message, then we ignore the message.
  11319. *
  11320. * @class MessageHandlers
  11321. * @param {Object} options the options to initialize with
  11322. */
  11323. class MessageHandlers {
  11324. constructor(self, options) {
  11325. this.options = options || {};
  11326. this.self = self;
  11327. this.init();
  11328. }
  11329. /**
  11330. * initialize our web worker and wire all the events.
  11331. */
  11332. init() {
  11333. if (this.transmuxer) {
  11334. this.transmuxer.dispose();
  11335. }
  11336. this.transmuxer = new transmuxer.Transmuxer(this.options);
  11337. wireTransmuxerEvents(this.self, this.transmuxer);
  11338. }
  11339. pushMp4Captions(data) {
  11340. if (!this.captionParser) {
  11341. this.captionParser = new captionParser();
  11342. this.captionParser.init();
  11343. }
  11344. const segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  11345. const parsed = this.captionParser.parse(segment, data.trackIds, data.timescales);
  11346. this.self.postMessage({
  11347. action: 'mp4Captions',
  11348. captions: parsed && parsed.captions || [],
  11349. logs: parsed && parsed.logs || [],
  11350. data: segment.buffer
  11351. }, [segment.buffer]);
  11352. }
  11353. probeMp4StartTime({
  11354. timescales,
  11355. data
  11356. }) {
  11357. const startTime = probe$2.startTime(timescales, data);
  11358. this.self.postMessage({
  11359. action: 'probeMp4StartTime',
  11360. startTime,
  11361. data
  11362. }, [data.buffer]);
  11363. }
  11364. probeMp4Tracks({
  11365. data
  11366. }) {
  11367. const tracks = probe$2.tracks(data);
  11368. this.self.postMessage({
  11369. action: 'probeMp4Tracks',
  11370. tracks,
  11371. data
  11372. }, [data.buffer]);
  11373. }
  11374. /**
  11375. * Probes an mp4 segment for EMSG boxes containing ID3 data.
  11376. * https://aomediacodec.github.io/id3-emsg/
  11377. *
  11378. * @param {Uint8Array} data segment data
  11379. * @param {number} offset segment start time
  11380. * @return {Object[]} an array of ID3 frames
  11381. */
  11382. probeEmsgID3({
  11383. data,
  11384. offset
  11385. }) {
  11386. const id3Frames = probe$2.getEmsgID3(data, offset);
  11387. this.self.postMessage({
  11388. action: 'probeEmsgID3',
  11389. id3Frames,
  11390. emsgData: data
  11391. }, [data.buffer]);
  11392. }
  11393. /**
  11394. * Probe an mpeg2-ts segment to determine the start time of the segment in it's
  11395. * internal "media time," as well as whether it contains video and/or audio.
  11396. *
  11397. * @private
  11398. * @param {Uint8Array} bytes - segment bytes
  11399. * @param {number} baseStartTime
  11400. * Relative reference timestamp used when adjusting frame timestamps for rollover.
  11401. * This value should be in seconds, as it's converted to a 90khz clock within the
  11402. * function body.
  11403. * @return {Object} The start time of the current segment in "media time" as well as
  11404. * whether it contains video and/or audio
  11405. */
  11406. probeTs({
  11407. data,
  11408. baseStartTime
  11409. }) {
  11410. const tsStartTime = typeof baseStartTime === 'number' && !isNaN(baseStartTime) ? baseStartTime * clock$2.ONE_SECOND_IN_TS : void 0;
  11411. const timeInfo = tsInspector.inspect(data, tsStartTime);
  11412. let result = null;
  11413. if (timeInfo) {
  11414. result = {
  11415. // each type's time info comes back as an array of 2 times, start and end
  11416. hasVideo: timeInfo.video && timeInfo.video.length === 2 || false,
  11417. hasAudio: timeInfo.audio && timeInfo.audio.length === 2 || false
  11418. };
  11419. if (result.hasVideo) {
  11420. result.videoStart = timeInfo.video[0].ptsTime;
  11421. }
  11422. if (result.hasAudio) {
  11423. result.audioStart = timeInfo.audio[0].ptsTime;
  11424. }
  11425. }
  11426. this.self.postMessage({
  11427. action: 'probeTs',
  11428. result,
  11429. data
  11430. }, [data.buffer]);
  11431. }
  11432. clearAllMp4Captions() {
  11433. if (this.captionParser) {
  11434. this.captionParser.clearAllCaptions();
  11435. }
  11436. }
  11437. clearParsedMp4Captions() {
  11438. if (this.captionParser) {
  11439. this.captionParser.clearParsedCaptions();
  11440. }
  11441. }
  11442. /**
  11443. * Adds data (a ts segment) to the start of the transmuxer pipeline for
  11444. * processing.
  11445. *
  11446. * @param {ArrayBuffer} data data to push into the muxer
  11447. */
  11448. push(data) {
  11449. // Cast array buffer to correct type for transmuxer
  11450. const segment = new Uint8Array(data.data, data.byteOffset, data.byteLength);
  11451. this.transmuxer.push(segment);
  11452. }
  11453. /**
  11454. * Recreate the transmuxer so that the next segment added via `push`
  11455. * start with a fresh transmuxer.
  11456. */
  11457. reset() {
  11458. this.transmuxer.reset();
  11459. }
  11460. /**
  11461. * Set the value that will be used as the `baseMediaDecodeTime` time for the
  11462. * next segment pushed in. Subsequent segments will have their `baseMediaDecodeTime`
  11463. * set relative to the first based on the PTS values.
  11464. *
  11465. * @param {Object} data used to set the timestamp offset in the muxer
  11466. */
  11467. setTimestampOffset(data) {
  11468. const timestampOffset = data.timestampOffset || 0;
  11469. this.transmuxer.setBaseMediaDecodeTime(Math.round(clock$2.secondsToVideoTs(timestampOffset)));
  11470. }
  11471. setAudioAppendStart(data) {
  11472. this.transmuxer.setAudioAppendStart(Math.ceil(clock$2.secondsToVideoTs(data.appendStart)));
  11473. }
  11474. setRemux(data) {
  11475. this.transmuxer.setRemux(data.remux);
  11476. }
  11477. /**
  11478. * Forces the pipeline to finish processing the last segment and emit it's
  11479. * results.
  11480. *
  11481. * @param {Object} data event data, not really used
  11482. */
  11483. flush(data) {
  11484. this.transmuxer.flush(); // transmuxed done action is fired after both audio/video pipelines are flushed
  11485. self.postMessage({
  11486. action: 'done',
  11487. type: 'transmuxed'
  11488. });
  11489. }
  11490. endTimeline() {
  11491. this.transmuxer.endTimeline(); // transmuxed endedtimeline action is fired after both audio/video pipelines end their
  11492. // timelines
  11493. self.postMessage({
  11494. action: 'endedtimeline',
  11495. type: 'transmuxed'
  11496. });
  11497. }
  11498. alignGopsWith(data) {
  11499. this.transmuxer.alignGopsWith(data.gopsToAlignWith.slice());
  11500. }
  11501. }
  11502. /**
  11503. * Our web worker interface so that things can talk to mux.js
  11504. * that will be running in a web worker. the scope is passed to this by
  11505. * webworkify.
  11506. *
  11507. * @param {Object} self the scope for the web worker
  11508. */
  11509. self.onmessage = function (event) {
  11510. if (event.data.action === 'init' && event.data.options) {
  11511. this.messageHandlers = new MessageHandlers(self, event.data.options);
  11512. return;
  11513. }
  11514. if (!this.messageHandlers) {
  11515. this.messageHandlers = new MessageHandlers(self);
  11516. }
  11517. if (event.data && event.data.action && event.data.action !== 'init') {
  11518. if (this.messageHandlers[event.data.action]) {
  11519. this.messageHandlers[event.data.action](event.data);
  11520. }
  11521. }
  11522. };
  11523. }));
  11524. var TransmuxWorker = factory(workerCode$1);
  11525. /* rollup-plugin-worker-factory end for worker!/home/runner/work/http-streaming/http-streaming/src/transmuxer-worker.js */
  11526. const handleData_ = (event, transmuxedData, callback) => {
  11527. const {
  11528. type,
  11529. initSegment,
  11530. captions,
  11531. captionStreams,
  11532. metadata,
  11533. videoFrameDtsTime,
  11534. videoFramePtsTime
  11535. } = event.data.segment;
  11536. transmuxedData.buffer.push({
  11537. captions,
  11538. captionStreams,
  11539. metadata
  11540. });
  11541. const boxes = event.data.segment.boxes || {
  11542. data: event.data.segment.data
  11543. };
  11544. const result = {
  11545. type,
  11546. // cast ArrayBuffer to TypedArray
  11547. data: new Uint8Array(boxes.data, boxes.data.byteOffset, boxes.data.byteLength),
  11548. initSegment: new Uint8Array(initSegment.data, initSegment.byteOffset, initSegment.byteLength)
  11549. };
  11550. if (typeof videoFrameDtsTime !== 'undefined') {
  11551. result.videoFrameDtsTime = videoFrameDtsTime;
  11552. }
  11553. if (typeof videoFramePtsTime !== 'undefined') {
  11554. result.videoFramePtsTime = videoFramePtsTime;
  11555. }
  11556. callback(result);
  11557. };
  11558. const handleDone_ = ({
  11559. transmuxedData,
  11560. callback
  11561. }) => {
  11562. // Previously we only returned data on data events,
  11563. // not on done events. Clear out the buffer to keep that consistent.
  11564. transmuxedData.buffer = []; // all buffers should have been flushed from the muxer, so start processing anything we
  11565. // have received
  11566. callback(transmuxedData);
  11567. };
  11568. const handleGopInfo_ = (event, transmuxedData) => {
  11569. transmuxedData.gopInfo = event.data.gopInfo;
  11570. };
  11571. const processTransmux = options => {
  11572. const {
  11573. transmuxer,
  11574. bytes,
  11575. audioAppendStart,
  11576. gopsToAlignWith,
  11577. remux,
  11578. onData,
  11579. onTrackInfo,
  11580. onAudioTimingInfo,
  11581. onVideoTimingInfo,
  11582. onVideoSegmentTimingInfo,
  11583. onAudioSegmentTimingInfo,
  11584. onId3,
  11585. onCaptions,
  11586. onDone,
  11587. onEndedTimeline,
  11588. onTransmuxerLog,
  11589. isEndOfTimeline
  11590. } = options;
  11591. const transmuxedData = {
  11592. buffer: []
  11593. };
  11594. let waitForEndedTimelineEvent = isEndOfTimeline;
  11595. const handleMessage = event => {
  11596. if (transmuxer.currentTransmux !== options) {
  11597. // disposed
  11598. return;
  11599. }
  11600. if (event.data.action === 'data') {
  11601. handleData_(event, transmuxedData, onData);
  11602. }
  11603. if (event.data.action === 'trackinfo') {
  11604. onTrackInfo(event.data.trackInfo);
  11605. }
  11606. if (event.data.action === 'gopInfo') {
  11607. handleGopInfo_(event, transmuxedData);
  11608. }
  11609. if (event.data.action === 'audioTimingInfo') {
  11610. onAudioTimingInfo(event.data.audioTimingInfo);
  11611. }
  11612. if (event.data.action === 'videoTimingInfo') {
  11613. onVideoTimingInfo(event.data.videoTimingInfo);
  11614. }
  11615. if (event.data.action === 'videoSegmentTimingInfo') {
  11616. onVideoSegmentTimingInfo(event.data.videoSegmentTimingInfo);
  11617. }
  11618. if (event.data.action === 'audioSegmentTimingInfo') {
  11619. onAudioSegmentTimingInfo(event.data.audioSegmentTimingInfo);
  11620. }
  11621. if (event.data.action === 'id3Frame') {
  11622. onId3([event.data.id3Frame], event.data.id3Frame.dispatchType);
  11623. }
  11624. if (event.data.action === 'caption') {
  11625. onCaptions(event.data.caption);
  11626. }
  11627. if (event.data.action === 'endedtimeline') {
  11628. waitForEndedTimelineEvent = false;
  11629. onEndedTimeline();
  11630. }
  11631. if (event.data.action === 'log') {
  11632. onTransmuxerLog(event.data.log);
  11633. } // wait for the transmuxed event since we may have audio and video
  11634. if (event.data.type !== 'transmuxed') {
  11635. return;
  11636. } // If the "endedtimeline" event has not yet fired, and this segment represents the end
  11637. // of a timeline, that means there may still be data events before the segment
  11638. // processing can be considerred complete. In that case, the final event should be
  11639. // an "endedtimeline" event with the type "transmuxed."
  11640. if (waitForEndedTimelineEvent) {
  11641. return;
  11642. }
  11643. transmuxer.onmessage = null;
  11644. handleDone_({
  11645. transmuxedData,
  11646. callback: onDone
  11647. });
  11648. /* eslint-disable no-use-before-define */
  11649. dequeue(transmuxer);
  11650. /* eslint-enable */
  11651. };
  11652. transmuxer.onmessage = handleMessage;
  11653. if (audioAppendStart) {
  11654. transmuxer.postMessage({
  11655. action: 'setAudioAppendStart',
  11656. appendStart: audioAppendStart
  11657. });
  11658. } // allow empty arrays to be passed to clear out GOPs
  11659. if (Array.isArray(gopsToAlignWith)) {
  11660. transmuxer.postMessage({
  11661. action: 'alignGopsWith',
  11662. gopsToAlignWith
  11663. });
  11664. }
  11665. if (typeof remux !== 'undefined') {
  11666. transmuxer.postMessage({
  11667. action: 'setRemux',
  11668. remux
  11669. });
  11670. }
  11671. if (bytes.byteLength) {
  11672. const buffer = bytes instanceof ArrayBuffer ? bytes : bytes.buffer;
  11673. const byteOffset = bytes instanceof ArrayBuffer ? 0 : bytes.byteOffset;
  11674. transmuxer.postMessage({
  11675. action: 'push',
  11676. // Send the typed-array of data as an ArrayBuffer so that
  11677. // it can be sent as a "Transferable" and avoid the costly
  11678. // memory copy
  11679. data: buffer,
  11680. // To recreate the original typed-array, we need information
  11681. // about what portion of the ArrayBuffer it was a view into
  11682. byteOffset,
  11683. byteLength: bytes.byteLength
  11684. }, [buffer]);
  11685. }
  11686. if (isEndOfTimeline) {
  11687. transmuxer.postMessage({
  11688. action: 'endTimeline'
  11689. });
  11690. } // even if we didn't push any bytes, we have to make sure we flush in case we reached
  11691. // the end of the segment
  11692. transmuxer.postMessage({
  11693. action: 'flush'
  11694. });
  11695. };
  11696. const dequeue = transmuxer => {
  11697. transmuxer.currentTransmux = null;
  11698. if (transmuxer.transmuxQueue.length) {
  11699. transmuxer.currentTransmux = transmuxer.transmuxQueue.shift();
  11700. if (typeof transmuxer.currentTransmux === 'function') {
  11701. transmuxer.currentTransmux();
  11702. } else {
  11703. processTransmux(transmuxer.currentTransmux);
  11704. }
  11705. }
  11706. };
  11707. const processAction = (transmuxer, action) => {
  11708. transmuxer.postMessage({
  11709. action
  11710. });
  11711. dequeue(transmuxer);
  11712. };
  11713. const enqueueAction = (action, transmuxer) => {
  11714. if (!transmuxer.currentTransmux) {
  11715. transmuxer.currentTransmux = action;
  11716. processAction(transmuxer, action);
  11717. return;
  11718. }
  11719. transmuxer.transmuxQueue.push(processAction.bind(null, transmuxer, action));
  11720. };
  11721. const reset = transmuxer => {
  11722. enqueueAction('reset', transmuxer);
  11723. };
  11724. const endTimeline = transmuxer => {
  11725. enqueueAction('endTimeline', transmuxer);
  11726. };
  11727. const transmux = options => {
  11728. if (!options.transmuxer.currentTransmux) {
  11729. options.transmuxer.currentTransmux = options;
  11730. processTransmux(options);
  11731. return;
  11732. }
  11733. options.transmuxer.transmuxQueue.push(options);
  11734. };
  11735. const createTransmuxer = options => {
  11736. const transmuxer = new TransmuxWorker();
  11737. transmuxer.currentTransmux = null;
  11738. transmuxer.transmuxQueue = [];
  11739. const term = transmuxer.terminate;
  11740. transmuxer.terminate = () => {
  11741. transmuxer.currentTransmux = null;
  11742. transmuxer.transmuxQueue.length = 0;
  11743. return term.call(transmuxer);
  11744. };
  11745. transmuxer.postMessage({
  11746. action: 'init',
  11747. options
  11748. });
  11749. return transmuxer;
  11750. };
  11751. var segmentTransmuxer = {
  11752. reset,
  11753. endTimeline,
  11754. transmux,
  11755. createTransmuxer
  11756. };
  11757. const workerCallback = function (options) {
  11758. const transmuxer = options.transmuxer;
  11759. const endAction = options.endAction || options.action;
  11760. const callback = options.callback;
  11761. const message = _extends__default["default"]({}, options, {
  11762. endAction: null,
  11763. transmuxer: null,
  11764. callback: null
  11765. });
  11766. const listenForEndEvent = event => {
  11767. if (event.data.action !== endAction) {
  11768. return;
  11769. }
  11770. transmuxer.removeEventListener('message', listenForEndEvent); // transfer ownership of bytes back to us.
  11771. if (event.data.data) {
  11772. event.data.data = new Uint8Array(event.data.data, options.byteOffset || 0, options.byteLength || event.data.data.byteLength);
  11773. if (options.data) {
  11774. options.data = event.data.data;
  11775. }
  11776. }
  11777. callback(event.data);
  11778. };
  11779. transmuxer.addEventListener('message', listenForEndEvent);
  11780. if (options.data) {
  11781. const isArrayBuffer = options.data instanceof ArrayBuffer;
  11782. message.byteOffset = isArrayBuffer ? 0 : options.data.byteOffset;
  11783. message.byteLength = options.data.byteLength;
  11784. const transfers = [isArrayBuffer ? options.data : options.data.buffer];
  11785. transmuxer.postMessage(message, transfers);
  11786. } else {
  11787. transmuxer.postMessage(message);
  11788. }
  11789. };
  11790. const REQUEST_ERRORS = {
  11791. FAILURE: 2,
  11792. TIMEOUT: -101,
  11793. ABORTED: -102
  11794. };
  11795. /**
  11796. * Abort all requests
  11797. *
  11798. * @param {Object} activeXhrs - an object that tracks all XHR requests
  11799. */
  11800. const abortAll = activeXhrs => {
  11801. activeXhrs.forEach(xhr => {
  11802. xhr.abort();
  11803. });
  11804. };
  11805. /**
  11806. * Gather important bandwidth stats once a request has completed
  11807. *
  11808. * @param {Object} request - the XHR request from which to gather stats
  11809. */
  11810. const getRequestStats = request => {
  11811. return {
  11812. bandwidth: request.bandwidth,
  11813. bytesReceived: request.bytesReceived || 0,
  11814. roundTripTime: request.roundTripTime || 0
  11815. };
  11816. };
  11817. /**
  11818. * If possible gather bandwidth stats as a request is in
  11819. * progress
  11820. *
  11821. * @param {Event} progressEvent - an event object from an XHR's progress event
  11822. */
  11823. const getProgressStats = progressEvent => {
  11824. const request = progressEvent.target;
  11825. const roundTripTime = Date.now() - request.requestTime;
  11826. const stats = {
  11827. bandwidth: Infinity,
  11828. bytesReceived: 0,
  11829. roundTripTime: roundTripTime || 0
  11830. };
  11831. stats.bytesReceived = progressEvent.loaded; // This can result in Infinity if stats.roundTripTime is 0 but that is ok
  11832. // because we should only use bandwidth stats on progress to determine when
  11833. // abort a request early due to insufficient bandwidth
  11834. stats.bandwidth = Math.floor(stats.bytesReceived / stats.roundTripTime * 8 * 1000);
  11835. return stats;
  11836. };
  11837. /**
  11838. * Handle all error conditions in one place and return an object
  11839. * with all the information
  11840. *
  11841. * @param {Error|null} error - if non-null signals an error occured with the XHR
  11842. * @param {Object} request - the XHR request that possibly generated the error
  11843. */
  11844. const handleErrors = (error, request) => {
  11845. if (request.timedout) {
  11846. return {
  11847. status: request.status,
  11848. message: 'HLS request timed-out at URL: ' + request.uri,
  11849. code: REQUEST_ERRORS.TIMEOUT,
  11850. xhr: request
  11851. };
  11852. }
  11853. if (request.aborted) {
  11854. return {
  11855. status: request.status,
  11856. message: 'HLS request aborted at URL: ' + request.uri,
  11857. code: REQUEST_ERRORS.ABORTED,
  11858. xhr: request
  11859. };
  11860. }
  11861. if (error) {
  11862. return {
  11863. status: request.status,
  11864. message: 'HLS request errored at URL: ' + request.uri,
  11865. code: REQUEST_ERRORS.FAILURE,
  11866. xhr: request
  11867. };
  11868. }
  11869. if (request.responseType === 'arraybuffer' && request.response.byteLength === 0) {
  11870. return {
  11871. status: request.status,
  11872. message: 'Empty HLS response at URL: ' + request.uri,
  11873. code: REQUEST_ERRORS.FAILURE,
  11874. xhr: request
  11875. };
  11876. }
  11877. return null;
  11878. };
  11879. /**
  11880. * Handle responses for key data and convert the key data to the correct format
  11881. * for the decryption step later
  11882. *
  11883. * @param {Object} segment - a simplified copy of the segmentInfo object
  11884. * from SegmentLoader
  11885. * @param {Array} objects - objects to add the key bytes to.
  11886. * @param {Function} finishProcessingFn - a callback to execute to continue processing
  11887. * this request
  11888. */
  11889. const handleKeyResponse = (segment, objects, finishProcessingFn) => (error, request) => {
  11890. const response = request.response;
  11891. const errorObj = handleErrors(error, request);
  11892. if (errorObj) {
  11893. return finishProcessingFn(errorObj, segment);
  11894. }
  11895. if (response.byteLength !== 16) {
  11896. return finishProcessingFn({
  11897. status: request.status,
  11898. message: 'Invalid HLS key at URL: ' + request.uri,
  11899. code: REQUEST_ERRORS.FAILURE,
  11900. xhr: request
  11901. }, segment);
  11902. }
  11903. const view = new DataView(response);
  11904. const bytes = new Uint32Array([view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12)]);
  11905. for (let i = 0; i < objects.length; i++) {
  11906. objects[i].bytes = bytes;
  11907. }
  11908. return finishProcessingFn(null, segment);
  11909. };
  11910. const parseInitSegment = (segment, callback) => {
  11911. const type = containers.detectContainerForBytes(segment.map.bytes); // TODO: We should also handle ts init segments here, but we
  11912. // only know how to parse mp4 init segments at the moment
  11913. if (type !== 'mp4') {
  11914. const uri = segment.map.resolvedUri || segment.map.uri;
  11915. return callback({
  11916. internal: true,
  11917. message: `Found unsupported ${type || 'unknown'} container for initialization segment at URL: ${uri}`,
  11918. code: REQUEST_ERRORS.FAILURE
  11919. });
  11920. }
  11921. workerCallback({
  11922. action: 'probeMp4Tracks',
  11923. data: segment.map.bytes,
  11924. transmuxer: segment.transmuxer,
  11925. callback: ({
  11926. tracks,
  11927. data
  11928. }) => {
  11929. // transfer bytes back to us
  11930. segment.map.bytes = data;
  11931. tracks.forEach(function (track) {
  11932. segment.map.tracks = segment.map.tracks || {}; // only support one track of each type for now
  11933. if (segment.map.tracks[track.type]) {
  11934. return;
  11935. }
  11936. segment.map.tracks[track.type] = track;
  11937. if (typeof track.id === 'number' && track.timescale) {
  11938. segment.map.timescales = segment.map.timescales || {};
  11939. segment.map.timescales[track.id] = track.timescale;
  11940. }
  11941. });
  11942. return callback(null);
  11943. }
  11944. });
  11945. };
  11946. /**
  11947. * Handle init-segment responses
  11948. *
  11949. * @param {Object} segment - a simplified copy of the segmentInfo object
  11950. * from SegmentLoader
  11951. * @param {Function} finishProcessingFn - a callback to execute to continue processing
  11952. * this request
  11953. */
  11954. const handleInitSegmentResponse = ({
  11955. segment,
  11956. finishProcessingFn
  11957. }) => (error, request) => {
  11958. const errorObj = handleErrors(error, request);
  11959. if (errorObj) {
  11960. return finishProcessingFn(errorObj, segment);
  11961. }
  11962. const bytes = new Uint8Array(request.response); // init segment is encypted, we will have to wait
  11963. // until the key request is done to decrypt.
  11964. if (segment.map.key) {
  11965. segment.map.encryptedBytes = bytes;
  11966. return finishProcessingFn(null, segment);
  11967. }
  11968. segment.map.bytes = bytes;
  11969. parseInitSegment(segment, function (parseError) {
  11970. if (parseError) {
  11971. parseError.xhr = request;
  11972. parseError.status = request.status;
  11973. return finishProcessingFn(parseError, segment);
  11974. }
  11975. finishProcessingFn(null, segment);
  11976. });
  11977. };
  11978. /**
  11979. * Response handler for segment-requests being sure to set the correct
  11980. * property depending on whether the segment is encryped or not
  11981. * Also records and keeps track of stats that are used for ABR purposes
  11982. *
  11983. * @param {Object} segment - a simplified copy of the segmentInfo object
  11984. * from SegmentLoader
  11985. * @param {Function} finishProcessingFn - a callback to execute to continue processing
  11986. * this request
  11987. */
  11988. const handleSegmentResponse = ({
  11989. segment,
  11990. finishProcessingFn,
  11991. responseType
  11992. }) => (error, request) => {
  11993. const errorObj = handleErrors(error, request);
  11994. if (errorObj) {
  11995. return finishProcessingFn(errorObj, segment);
  11996. }
  11997. const newBytes = // although responseText "should" exist, this guard serves to prevent an error being
  11998. // thrown for two primary cases:
  11999. // 1. the mime type override stops working, or is not implemented for a specific
  12000. // browser
  12001. // 2. when using mock XHR libraries like sinon that do not allow the override behavior
  12002. responseType === 'arraybuffer' || !request.responseText ? request.response : stringToArrayBuffer(request.responseText.substring(segment.lastReachedChar || 0));
  12003. segment.stats = getRequestStats(request);
  12004. if (segment.key) {
  12005. segment.encryptedBytes = new Uint8Array(newBytes);
  12006. } else {
  12007. segment.bytes = new Uint8Array(newBytes);
  12008. }
  12009. return finishProcessingFn(null, segment);
  12010. };
  12011. const transmuxAndNotify = ({
  12012. segment,
  12013. bytes,
  12014. trackInfoFn,
  12015. timingInfoFn,
  12016. videoSegmentTimingInfoFn,
  12017. audioSegmentTimingInfoFn,
  12018. id3Fn,
  12019. captionsFn,
  12020. isEndOfTimeline,
  12021. endedTimelineFn,
  12022. dataFn,
  12023. doneFn,
  12024. onTransmuxerLog
  12025. }) => {
  12026. const fmp4Tracks = segment.map && segment.map.tracks || {};
  12027. const isMuxed = Boolean(fmp4Tracks.audio && fmp4Tracks.video); // Keep references to each function so we can null them out after we're done with them.
  12028. // One reason for this is that in the case of full segments, we want to trust start
  12029. // times from the probe, rather than the transmuxer.
  12030. let audioStartFn = timingInfoFn.bind(null, segment, 'audio', 'start');
  12031. const audioEndFn = timingInfoFn.bind(null, segment, 'audio', 'end');
  12032. let videoStartFn = timingInfoFn.bind(null, segment, 'video', 'start');
  12033. const videoEndFn = timingInfoFn.bind(null, segment, 'video', 'end');
  12034. const finish = () => transmux({
  12035. bytes,
  12036. transmuxer: segment.transmuxer,
  12037. audioAppendStart: segment.audioAppendStart,
  12038. gopsToAlignWith: segment.gopsToAlignWith,
  12039. remux: isMuxed,
  12040. onData: result => {
  12041. result.type = result.type === 'combined' ? 'video' : result.type;
  12042. dataFn(segment, result);
  12043. },
  12044. onTrackInfo: trackInfo => {
  12045. if (trackInfoFn) {
  12046. if (isMuxed) {
  12047. trackInfo.isMuxed = true;
  12048. }
  12049. trackInfoFn(segment, trackInfo);
  12050. }
  12051. },
  12052. onAudioTimingInfo: audioTimingInfo => {
  12053. // we only want the first start value we encounter
  12054. if (audioStartFn && typeof audioTimingInfo.start !== 'undefined') {
  12055. audioStartFn(audioTimingInfo.start);
  12056. audioStartFn = null;
  12057. } // we want to continually update the end time
  12058. if (audioEndFn && typeof audioTimingInfo.end !== 'undefined') {
  12059. audioEndFn(audioTimingInfo.end);
  12060. }
  12061. },
  12062. onVideoTimingInfo: videoTimingInfo => {
  12063. // we only want the first start value we encounter
  12064. if (videoStartFn && typeof videoTimingInfo.start !== 'undefined') {
  12065. videoStartFn(videoTimingInfo.start);
  12066. videoStartFn = null;
  12067. } // we want to continually update the end time
  12068. if (videoEndFn && typeof videoTimingInfo.end !== 'undefined') {
  12069. videoEndFn(videoTimingInfo.end);
  12070. }
  12071. },
  12072. onVideoSegmentTimingInfo: videoSegmentTimingInfo => {
  12073. videoSegmentTimingInfoFn(videoSegmentTimingInfo);
  12074. },
  12075. onAudioSegmentTimingInfo: audioSegmentTimingInfo => {
  12076. audioSegmentTimingInfoFn(audioSegmentTimingInfo);
  12077. },
  12078. onId3: (id3Frames, dispatchType) => {
  12079. id3Fn(segment, id3Frames, dispatchType);
  12080. },
  12081. onCaptions: captions => {
  12082. captionsFn(segment, [captions]);
  12083. },
  12084. isEndOfTimeline,
  12085. onEndedTimeline: () => {
  12086. endedTimelineFn();
  12087. },
  12088. onTransmuxerLog,
  12089. onDone: result => {
  12090. if (!doneFn) {
  12091. return;
  12092. }
  12093. result.type = result.type === 'combined' ? 'video' : result.type;
  12094. doneFn(null, segment, result);
  12095. }
  12096. }); // In the transmuxer, we don't yet have the ability to extract a "proper" start time.
  12097. // Meaning cached frame data may corrupt our notion of where this segment
  12098. // really starts. To get around this, probe for the info needed.
  12099. workerCallback({
  12100. action: 'probeTs',
  12101. transmuxer: segment.transmuxer,
  12102. data: bytes,
  12103. baseStartTime: segment.baseStartTime,
  12104. callback: data => {
  12105. segment.bytes = bytes = data.data;
  12106. const probeResult = data.result;
  12107. if (probeResult) {
  12108. trackInfoFn(segment, {
  12109. hasAudio: probeResult.hasAudio,
  12110. hasVideo: probeResult.hasVideo,
  12111. isMuxed
  12112. });
  12113. trackInfoFn = null;
  12114. }
  12115. finish();
  12116. }
  12117. });
  12118. };
  12119. const handleSegmentBytes = ({
  12120. segment,
  12121. bytes,
  12122. trackInfoFn,
  12123. timingInfoFn,
  12124. videoSegmentTimingInfoFn,
  12125. audioSegmentTimingInfoFn,
  12126. id3Fn,
  12127. captionsFn,
  12128. isEndOfTimeline,
  12129. endedTimelineFn,
  12130. dataFn,
  12131. doneFn,
  12132. onTransmuxerLog
  12133. }) => {
  12134. let bytesAsUint8Array = new Uint8Array(bytes); // TODO:
  12135. // We should have a handler that fetches the number of bytes required
  12136. // to check if something is fmp4. This will allow us to save bandwidth
  12137. // because we can only exclude a playlist and abort requests
  12138. // by codec after trackinfo triggers.
  12139. if (containers.isLikelyFmp4MediaSegment(bytesAsUint8Array)) {
  12140. segment.isFmp4 = true;
  12141. const {
  12142. tracks
  12143. } = segment.map;
  12144. const trackInfo = {
  12145. isFmp4: true,
  12146. hasVideo: !!tracks.video,
  12147. hasAudio: !!tracks.audio
  12148. }; // if we have a audio track, with a codec that is not set to
  12149. // encrypted audio
  12150. if (tracks.audio && tracks.audio.codec && tracks.audio.codec !== 'enca') {
  12151. trackInfo.audioCodec = tracks.audio.codec;
  12152. } // if we have a video track, with a codec that is not set to
  12153. // encrypted video
  12154. if (tracks.video && tracks.video.codec && tracks.video.codec !== 'encv') {
  12155. trackInfo.videoCodec = tracks.video.codec;
  12156. }
  12157. if (tracks.video && tracks.audio) {
  12158. trackInfo.isMuxed = true;
  12159. } // since we don't support appending fmp4 data on progress, we know we have the full
  12160. // segment here
  12161. trackInfoFn(segment, trackInfo); // The probe doesn't provide the segment end time, so only callback with the start
  12162. // time. The end time can be roughly calculated by the receiver using the duration.
  12163. //
  12164. // Note that the start time returned by the probe reflects the baseMediaDecodeTime, as
  12165. // that is the true start of the segment (where the playback engine should begin
  12166. // decoding).
  12167. const finishLoading = (captions, id3Frames) => {
  12168. // if the track still has audio at this point it is only possible
  12169. // for it to be audio only. See `tracks.video && tracks.audio` if statement
  12170. // above.
  12171. // we make sure to use segment.bytes here as that
  12172. dataFn(segment, {
  12173. data: bytesAsUint8Array,
  12174. type: trackInfo.hasAudio && !trackInfo.isMuxed ? 'audio' : 'video'
  12175. });
  12176. if (id3Frames && id3Frames.length) {
  12177. id3Fn(segment, id3Frames);
  12178. }
  12179. if (captions && captions.length) {
  12180. captionsFn(segment, captions);
  12181. }
  12182. doneFn(null, segment, {});
  12183. };
  12184. workerCallback({
  12185. action: 'probeMp4StartTime',
  12186. timescales: segment.map.timescales,
  12187. data: bytesAsUint8Array,
  12188. transmuxer: segment.transmuxer,
  12189. callback: ({
  12190. data,
  12191. startTime
  12192. }) => {
  12193. // transfer bytes back to us
  12194. bytes = data.buffer;
  12195. segment.bytes = bytesAsUint8Array = data;
  12196. if (trackInfo.hasAudio && !trackInfo.isMuxed) {
  12197. timingInfoFn(segment, 'audio', 'start', startTime);
  12198. }
  12199. if (trackInfo.hasVideo) {
  12200. timingInfoFn(segment, 'video', 'start', startTime);
  12201. }
  12202. workerCallback({
  12203. action: 'probeEmsgID3',
  12204. data: bytesAsUint8Array,
  12205. transmuxer: segment.transmuxer,
  12206. offset: startTime,
  12207. callback: ({
  12208. emsgData,
  12209. id3Frames
  12210. }) => {
  12211. // transfer bytes back to us
  12212. bytes = emsgData.buffer;
  12213. segment.bytes = bytesAsUint8Array = emsgData; // Run through the CaptionParser in case there are captions.
  12214. // Initialize CaptionParser if it hasn't been yet
  12215. if (!tracks.video || !emsgData.byteLength || !segment.transmuxer) {
  12216. finishLoading(undefined, id3Frames);
  12217. return;
  12218. }
  12219. workerCallback({
  12220. action: 'pushMp4Captions',
  12221. endAction: 'mp4Captions',
  12222. transmuxer: segment.transmuxer,
  12223. data: bytesAsUint8Array,
  12224. timescales: segment.map.timescales,
  12225. trackIds: [tracks.video.id],
  12226. callback: message => {
  12227. // transfer bytes back to us
  12228. bytes = message.data.buffer;
  12229. segment.bytes = bytesAsUint8Array = message.data;
  12230. message.logs.forEach(function (log) {
  12231. onTransmuxerLog(merge(log, {
  12232. stream: 'mp4CaptionParser'
  12233. }));
  12234. });
  12235. finishLoading(message.captions, id3Frames);
  12236. }
  12237. });
  12238. }
  12239. });
  12240. }
  12241. });
  12242. return;
  12243. } // VTT or other segments that don't need processing
  12244. if (!segment.transmuxer) {
  12245. doneFn(null, segment, {});
  12246. return;
  12247. }
  12248. if (typeof segment.container === 'undefined') {
  12249. segment.container = containers.detectContainerForBytes(bytesAsUint8Array);
  12250. }
  12251. if (segment.container !== 'ts' && segment.container !== 'aac') {
  12252. trackInfoFn(segment, {
  12253. hasAudio: false,
  12254. hasVideo: false
  12255. });
  12256. doneFn(null, segment, {});
  12257. return;
  12258. } // ts or aac
  12259. transmuxAndNotify({
  12260. segment,
  12261. bytes,
  12262. trackInfoFn,
  12263. timingInfoFn,
  12264. videoSegmentTimingInfoFn,
  12265. audioSegmentTimingInfoFn,
  12266. id3Fn,
  12267. captionsFn,
  12268. isEndOfTimeline,
  12269. endedTimelineFn,
  12270. dataFn,
  12271. doneFn,
  12272. onTransmuxerLog
  12273. });
  12274. };
  12275. const decrypt = function ({
  12276. id,
  12277. key,
  12278. encryptedBytes,
  12279. decryptionWorker
  12280. }, callback) {
  12281. const decryptionHandler = event => {
  12282. if (event.data.source === id) {
  12283. decryptionWorker.removeEventListener('message', decryptionHandler);
  12284. const decrypted = event.data.decrypted;
  12285. callback(new Uint8Array(decrypted.bytes, decrypted.byteOffset, decrypted.byteLength));
  12286. }
  12287. };
  12288. decryptionWorker.addEventListener('message', decryptionHandler);
  12289. let keyBytes;
  12290. if (key.bytes.slice) {
  12291. keyBytes = key.bytes.slice();
  12292. } else {
  12293. keyBytes = new Uint32Array(Array.prototype.slice.call(key.bytes));
  12294. } // incrementally decrypt the bytes
  12295. decryptionWorker.postMessage(createTransferableMessage({
  12296. source: id,
  12297. encrypted: encryptedBytes,
  12298. key: keyBytes,
  12299. iv: key.iv
  12300. }), [encryptedBytes.buffer, keyBytes.buffer]);
  12301. };
  12302. /**
  12303. * Decrypt the segment via the decryption web worker
  12304. *
  12305. * @param {WebWorker} decryptionWorker - a WebWorker interface to AES-128 decryption
  12306. * routines
  12307. * @param {Object} segment - a simplified copy of the segmentInfo object
  12308. * from SegmentLoader
  12309. * @param {Function} trackInfoFn - a callback that receives track info
  12310. * @param {Function} timingInfoFn - a callback that receives timing info
  12311. * @param {Function} videoSegmentTimingInfoFn
  12312. * a callback that receives video timing info based on media times and
  12313. * any adjustments made by the transmuxer
  12314. * @param {Function} audioSegmentTimingInfoFn
  12315. * a callback that receives audio timing info based on media times and
  12316. * any adjustments made by the transmuxer
  12317. * @param {boolean} isEndOfTimeline
  12318. * true if this segment represents the last segment in a timeline
  12319. * @param {Function} endedTimelineFn
  12320. * a callback made when a timeline is ended, will only be called if
  12321. * isEndOfTimeline is true
  12322. * @param {Function} dataFn - a callback that is executed when segment bytes are available
  12323. * and ready to use
  12324. * @param {Function} doneFn - a callback that is executed after decryption has completed
  12325. */
  12326. const decryptSegment = ({
  12327. decryptionWorker,
  12328. segment,
  12329. trackInfoFn,
  12330. timingInfoFn,
  12331. videoSegmentTimingInfoFn,
  12332. audioSegmentTimingInfoFn,
  12333. id3Fn,
  12334. captionsFn,
  12335. isEndOfTimeline,
  12336. endedTimelineFn,
  12337. dataFn,
  12338. doneFn,
  12339. onTransmuxerLog
  12340. }) => {
  12341. decrypt({
  12342. id: segment.requestId,
  12343. key: segment.key,
  12344. encryptedBytes: segment.encryptedBytes,
  12345. decryptionWorker
  12346. }, decryptedBytes => {
  12347. segment.bytes = decryptedBytes;
  12348. handleSegmentBytes({
  12349. segment,
  12350. bytes: segment.bytes,
  12351. trackInfoFn,
  12352. timingInfoFn,
  12353. videoSegmentTimingInfoFn,
  12354. audioSegmentTimingInfoFn,
  12355. id3Fn,
  12356. captionsFn,
  12357. isEndOfTimeline,
  12358. endedTimelineFn,
  12359. dataFn,
  12360. doneFn,
  12361. onTransmuxerLog
  12362. });
  12363. });
  12364. };
  12365. /**
  12366. * This function waits for all XHRs to finish (with either success or failure)
  12367. * before continueing processing via it's callback. The function gathers errors
  12368. * from each request into a single errors array so that the error status for
  12369. * each request can be examined later.
  12370. *
  12371. * @param {Object} activeXhrs - an object that tracks all XHR requests
  12372. * @param {WebWorker} decryptionWorker - a WebWorker interface to AES-128 decryption
  12373. * routines
  12374. * @param {Function} trackInfoFn - a callback that receives track info
  12375. * @param {Function} timingInfoFn - a callback that receives timing info
  12376. * @param {Function} videoSegmentTimingInfoFn
  12377. * a callback that receives video timing info based on media times and
  12378. * any adjustments made by the transmuxer
  12379. * @param {Function} audioSegmentTimingInfoFn
  12380. * a callback that receives audio timing info based on media times and
  12381. * any adjustments made by the transmuxer
  12382. * @param {Function} id3Fn - a callback that receives ID3 metadata
  12383. * @param {Function} captionsFn - a callback that receives captions
  12384. * @param {boolean} isEndOfTimeline
  12385. * true if this segment represents the last segment in a timeline
  12386. * @param {Function} endedTimelineFn
  12387. * a callback made when a timeline is ended, will only be called if
  12388. * isEndOfTimeline is true
  12389. * @param {Function} dataFn - a callback that is executed when segment bytes are available
  12390. * and ready to use
  12391. * @param {Function} doneFn - a callback that is executed after all resources have been
  12392. * downloaded and any decryption completed
  12393. */
  12394. const waitForCompletion = ({
  12395. activeXhrs,
  12396. decryptionWorker,
  12397. trackInfoFn,
  12398. timingInfoFn,
  12399. videoSegmentTimingInfoFn,
  12400. audioSegmentTimingInfoFn,
  12401. id3Fn,
  12402. captionsFn,
  12403. isEndOfTimeline,
  12404. endedTimelineFn,
  12405. dataFn,
  12406. doneFn,
  12407. onTransmuxerLog
  12408. }) => {
  12409. let count = 0;
  12410. let didError = false;
  12411. return (error, segment) => {
  12412. if (didError) {
  12413. return;
  12414. }
  12415. if (error) {
  12416. didError = true; // If there are errors, we have to abort any outstanding requests
  12417. abortAll(activeXhrs); // Even though the requests above are aborted, and in theory we could wait until we
  12418. // handle the aborted events from those requests, there are some cases where we may
  12419. // never get an aborted event. For instance, if the network connection is lost and
  12420. // there were two requests, the first may have triggered an error immediately, while
  12421. // the second request remains unsent. In that case, the aborted algorithm will not
  12422. // trigger an abort: see https://xhr.spec.whatwg.org/#the-abort()-method
  12423. //
  12424. // We also can't rely on the ready state of the XHR, since the request that
  12425. // triggered the connection error may also show as a ready state of 0 (unsent).
  12426. // Therefore, we have to finish this group of requests immediately after the first
  12427. // seen error.
  12428. return doneFn(error, segment);
  12429. }
  12430. count += 1;
  12431. if (count === activeXhrs.length) {
  12432. const segmentFinish = function () {
  12433. if (segment.encryptedBytes) {
  12434. return decryptSegment({
  12435. decryptionWorker,
  12436. segment,
  12437. trackInfoFn,
  12438. timingInfoFn,
  12439. videoSegmentTimingInfoFn,
  12440. audioSegmentTimingInfoFn,
  12441. id3Fn,
  12442. captionsFn,
  12443. isEndOfTimeline,
  12444. endedTimelineFn,
  12445. dataFn,
  12446. doneFn,
  12447. onTransmuxerLog
  12448. });
  12449. } // Otherwise, everything is ready just continue
  12450. handleSegmentBytes({
  12451. segment,
  12452. bytes: segment.bytes,
  12453. trackInfoFn,
  12454. timingInfoFn,
  12455. videoSegmentTimingInfoFn,
  12456. audioSegmentTimingInfoFn,
  12457. id3Fn,
  12458. captionsFn,
  12459. isEndOfTimeline,
  12460. endedTimelineFn,
  12461. dataFn,
  12462. doneFn,
  12463. onTransmuxerLog
  12464. });
  12465. }; // Keep track of when *all* of the requests have completed
  12466. segment.endOfAllRequests = Date.now();
  12467. if (segment.map && segment.map.encryptedBytes && !segment.map.bytes) {
  12468. return decrypt({
  12469. decryptionWorker,
  12470. // add -init to the "id" to differentiate between segment
  12471. // and init segment decryption, just in case they happen
  12472. // at the same time at some point in the future.
  12473. id: segment.requestId + '-init',
  12474. encryptedBytes: segment.map.encryptedBytes,
  12475. key: segment.map.key
  12476. }, decryptedBytes => {
  12477. segment.map.bytes = decryptedBytes;
  12478. parseInitSegment(segment, parseError => {
  12479. if (parseError) {
  12480. abortAll(activeXhrs);
  12481. return doneFn(parseError, segment);
  12482. }
  12483. segmentFinish();
  12484. });
  12485. });
  12486. }
  12487. segmentFinish();
  12488. }
  12489. };
  12490. };
  12491. /**
  12492. * Calls the abort callback if any request within the batch was aborted. Will only call
  12493. * the callback once per batch of requests, even if multiple were aborted.
  12494. *
  12495. * @param {Object} loadendState - state to check to see if the abort function was called
  12496. * @param {Function} abortFn - callback to call for abort
  12497. */
  12498. const handleLoadEnd = ({
  12499. loadendState,
  12500. abortFn
  12501. }) => event => {
  12502. const request = event.target;
  12503. if (request.aborted && abortFn && !loadendState.calledAbortFn) {
  12504. abortFn();
  12505. loadendState.calledAbortFn = true;
  12506. }
  12507. };
  12508. /**
  12509. * Simple progress event callback handler that gathers some stats before
  12510. * executing a provided callback with the `segment` object
  12511. *
  12512. * @param {Object} segment - a simplified copy of the segmentInfo object
  12513. * from SegmentLoader
  12514. * @param {Function} progressFn - a callback that is executed each time a progress event
  12515. * is received
  12516. * @param {Function} trackInfoFn - a callback that receives track info
  12517. * @param {Function} timingInfoFn - a callback that receives timing info
  12518. * @param {Function} videoSegmentTimingInfoFn
  12519. * a callback that receives video timing info based on media times and
  12520. * any adjustments made by the transmuxer
  12521. * @param {Function} audioSegmentTimingInfoFn
  12522. * a callback that receives audio timing info based on media times and
  12523. * any adjustments made by the transmuxer
  12524. * @param {boolean} isEndOfTimeline
  12525. * true if this segment represents the last segment in a timeline
  12526. * @param {Function} endedTimelineFn
  12527. * a callback made when a timeline is ended, will only be called if
  12528. * isEndOfTimeline is true
  12529. * @param {Function} dataFn - a callback that is executed when segment bytes are available
  12530. * and ready to use
  12531. * @param {Event} event - the progress event object from XMLHttpRequest
  12532. */
  12533. const handleProgress = ({
  12534. segment,
  12535. progressFn,
  12536. trackInfoFn,
  12537. timingInfoFn,
  12538. videoSegmentTimingInfoFn,
  12539. audioSegmentTimingInfoFn,
  12540. id3Fn,
  12541. captionsFn,
  12542. isEndOfTimeline,
  12543. endedTimelineFn,
  12544. dataFn
  12545. }) => event => {
  12546. const request = event.target;
  12547. if (request.aborted) {
  12548. return;
  12549. }
  12550. segment.stats = merge(segment.stats, getProgressStats(event)); // record the time that we receive the first byte of data
  12551. if (!segment.stats.firstBytesReceivedAt && segment.stats.bytesReceived) {
  12552. segment.stats.firstBytesReceivedAt = Date.now();
  12553. }
  12554. return progressFn(event, segment);
  12555. };
  12556. /**
  12557. * Load all resources and does any processing necessary for a media-segment
  12558. *
  12559. * Features:
  12560. * decrypts the media-segment if it has a key uri and an iv
  12561. * aborts *all* requests if *any* one request fails
  12562. *
  12563. * The segment object, at minimum, has the following format:
  12564. * {
  12565. * resolvedUri: String,
  12566. * [transmuxer]: Object,
  12567. * [byterange]: {
  12568. * offset: Number,
  12569. * length: Number
  12570. * },
  12571. * [key]: {
  12572. * resolvedUri: String
  12573. * [byterange]: {
  12574. * offset: Number,
  12575. * length: Number
  12576. * },
  12577. * iv: {
  12578. * bytes: Uint32Array
  12579. * }
  12580. * },
  12581. * [map]: {
  12582. * resolvedUri: String,
  12583. * [byterange]: {
  12584. * offset: Number,
  12585. * length: Number
  12586. * },
  12587. * [bytes]: Uint8Array
  12588. * }
  12589. * }
  12590. * ...where [name] denotes optional properties
  12591. *
  12592. * @param {Function} xhr - an instance of the xhr wrapper in xhr.js
  12593. * @param {Object} xhrOptions - the base options to provide to all xhr requests
  12594. * @param {WebWorker} decryptionWorker - a WebWorker interface to AES-128
  12595. * decryption routines
  12596. * @param {Object} segment - a simplified copy of the segmentInfo object
  12597. * from SegmentLoader
  12598. * @param {Function} abortFn - a callback called (only once) if any piece of a request was
  12599. * aborted
  12600. * @param {Function} progressFn - a callback that receives progress events from the main
  12601. * segment's xhr request
  12602. * @param {Function} trackInfoFn - a callback that receives track info
  12603. * @param {Function} timingInfoFn - a callback that receives timing info
  12604. * @param {Function} videoSegmentTimingInfoFn
  12605. * a callback that receives video timing info based on media times and
  12606. * any adjustments made by the transmuxer
  12607. * @param {Function} audioSegmentTimingInfoFn
  12608. * a callback that receives audio timing info based on media times and
  12609. * any adjustments made by the transmuxer
  12610. * @param {Function} id3Fn - a callback that receives ID3 metadata
  12611. * @param {Function} captionsFn - a callback that receives captions
  12612. * @param {boolean} isEndOfTimeline
  12613. * true if this segment represents the last segment in a timeline
  12614. * @param {Function} endedTimelineFn
  12615. * a callback made when a timeline is ended, will only be called if
  12616. * isEndOfTimeline is true
  12617. * @param {Function} dataFn - a callback that receives data from the main segment's xhr
  12618. * request, transmuxed if needed
  12619. * @param {Function} doneFn - a callback that is executed only once all requests have
  12620. * succeeded or failed
  12621. * @return {Function} a function that, when invoked, immediately aborts all
  12622. * outstanding requests
  12623. */
  12624. const mediaSegmentRequest = ({
  12625. xhr,
  12626. xhrOptions,
  12627. decryptionWorker,
  12628. segment,
  12629. abortFn,
  12630. progressFn,
  12631. trackInfoFn,
  12632. timingInfoFn,
  12633. videoSegmentTimingInfoFn,
  12634. audioSegmentTimingInfoFn,
  12635. id3Fn,
  12636. captionsFn,
  12637. isEndOfTimeline,
  12638. endedTimelineFn,
  12639. dataFn,
  12640. doneFn,
  12641. onTransmuxerLog
  12642. }) => {
  12643. const activeXhrs = [];
  12644. const finishProcessingFn = waitForCompletion({
  12645. activeXhrs,
  12646. decryptionWorker,
  12647. trackInfoFn,
  12648. timingInfoFn,
  12649. videoSegmentTimingInfoFn,
  12650. audioSegmentTimingInfoFn,
  12651. id3Fn,
  12652. captionsFn,
  12653. isEndOfTimeline,
  12654. endedTimelineFn,
  12655. dataFn,
  12656. doneFn,
  12657. onTransmuxerLog
  12658. }); // optionally, request the decryption key
  12659. if (segment.key && !segment.key.bytes) {
  12660. const objects = [segment.key];
  12661. if (segment.map && !segment.map.bytes && segment.map.key && segment.map.key.resolvedUri === segment.key.resolvedUri) {
  12662. objects.push(segment.map.key);
  12663. }
  12664. const keyRequestOptions = merge(xhrOptions, {
  12665. uri: segment.key.resolvedUri,
  12666. responseType: 'arraybuffer'
  12667. });
  12668. const keyRequestCallback = handleKeyResponse(segment, objects, finishProcessingFn);
  12669. const keyXhr = xhr(keyRequestOptions, keyRequestCallback);
  12670. activeXhrs.push(keyXhr);
  12671. } // optionally, request the associated media init segment
  12672. if (segment.map && !segment.map.bytes) {
  12673. const differentMapKey = segment.map.key && (!segment.key || segment.key.resolvedUri !== segment.map.key.resolvedUri);
  12674. if (differentMapKey) {
  12675. const mapKeyRequestOptions = merge(xhrOptions, {
  12676. uri: segment.map.key.resolvedUri,
  12677. responseType: 'arraybuffer'
  12678. });
  12679. const mapKeyRequestCallback = handleKeyResponse(segment, [segment.map.key], finishProcessingFn);
  12680. const mapKeyXhr = xhr(mapKeyRequestOptions, mapKeyRequestCallback);
  12681. activeXhrs.push(mapKeyXhr);
  12682. }
  12683. const initSegmentOptions = merge(xhrOptions, {
  12684. uri: segment.map.resolvedUri,
  12685. responseType: 'arraybuffer',
  12686. headers: segmentXhrHeaders(segment.map)
  12687. });
  12688. const initSegmentRequestCallback = handleInitSegmentResponse({
  12689. segment,
  12690. finishProcessingFn
  12691. });
  12692. const initSegmentXhr = xhr(initSegmentOptions, initSegmentRequestCallback);
  12693. activeXhrs.push(initSegmentXhr);
  12694. }
  12695. const segmentRequestOptions = merge(xhrOptions, {
  12696. uri: segment.part && segment.part.resolvedUri || segment.resolvedUri,
  12697. responseType: 'arraybuffer',
  12698. headers: segmentXhrHeaders(segment)
  12699. });
  12700. const segmentRequestCallback = handleSegmentResponse({
  12701. segment,
  12702. finishProcessingFn,
  12703. responseType: segmentRequestOptions.responseType
  12704. });
  12705. const segmentXhr = xhr(segmentRequestOptions, segmentRequestCallback);
  12706. segmentXhr.addEventListener('progress', handleProgress({
  12707. segment,
  12708. progressFn,
  12709. trackInfoFn,
  12710. timingInfoFn,
  12711. videoSegmentTimingInfoFn,
  12712. audioSegmentTimingInfoFn,
  12713. id3Fn,
  12714. captionsFn,
  12715. isEndOfTimeline,
  12716. endedTimelineFn,
  12717. dataFn
  12718. }));
  12719. activeXhrs.push(segmentXhr); // since all parts of the request must be considered, but should not make callbacks
  12720. // multiple times, provide a shared state object
  12721. const loadendState = {};
  12722. activeXhrs.forEach(activeXhr => {
  12723. activeXhr.addEventListener('loadend', handleLoadEnd({
  12724. loadendState,
  12725. abortFn
  12726. }));
  12727. });
  12728. return () => abortAll(activeXhrs);
  12729. };
  12730. /**
  12731. * @file - codecs.js - Handles tasks regarding codec strings such as translating them to
  12732. * codec strings, or translating codec strings into objects that can be examined.
  12733. */
  12734. const logFn$1 = logger('CodecUtils');
  12735. /**
  12736. * Returns a set of codec strings parsed from the playlist or the default
  12737. * codec strings if no codecs were specified in the playlist
  12738. *
  12739. * @param {Playlist} media the current media playlist
  12740. * @return {Object} an object with the video and audio codecs
  12741. */
  12742. const getCodecs = function (media) {
  12743. // if the codecs were explicitly specified, use them instead of the
  12744. // defaults
  12745. const mediaAttributes = media.attributes || {};
  12746. if (mediaAttributes.CODECS) {
  12747. return codecs_js.parseCodecs(mediaAttributes.CODECS);
  12748. }
  12749. };
  12750. const isMaat = (main, media) => {
  12751. const mediaAttributes = media.attributes || {};
  12752. return main && main.mediaGroups && main.mediaGroups.AUDIO && mediaAttributes.AUDIO && main.mediaGroups.AUDIO[mediaAttributes.AUDIO];
  12753. };
  12754. const isMuxed = (main, media) => {
  12755. if (!isMaat(main, media)) {
  12756. return true;
  12757. }
  12758. const mediaAttributes = media.attributes || {};
  12759. const audioGroup = main.mediaGroups.AUDIO[mediaAttributes.AUDIO];
  12760. for (const groupId in audioGroup) {
  12761. // If an audio group has a URI (the case for HLS, as HLS will use external playlists),
  12762. // or there are listed playlists (the case for DASH, as the manifest will have already
  12763. // provided all of the details necessary to generate the audio playlist, as opposed to
  12764. // HLS' externally requested playlists), then the content is demuxed.
  12765. if (!audioGroup[groupId].uri && !audioGroup[groupId].playlists) {
  12766. return true;
  12767. }
  12768. }
  12769. return false;
  12770. };
  12771. const unwrapCodecList = function (codecList) {
  12772. const codecs = {};
  12773. codecList.forEach(({
  12774. mediaType,
  12775. type,
  12776. details
  12777. }) => {
  12778. codecs[mediaType] = codecs[mediaType] || [];
  12779. codecs[mediaType].push(codecs_js.translateLegacyCodec(`${type}${details}`));
  12780. });
  12781. Object.keys(codecs).forEach(function (mediaType) {
  12782. if (codecs[mediaType].length > 1) {
  12783. logFn$1(`multiple ${mediaType} codecs found as attributes: ${codecs[mediaType].join(', ')}. Setting playlist codecs to null so that we wait for mux.js to probe segments for real codecs.`);
  12784. codecs[mediaType] = null;
  12785. return;
  12786. }
  12787. codecs[mediaType] = codecs[mediaType][0];
  12788. });
  12789. return codecs;
  12790. };
  12791. const codecCount = function (codecObj) {
  12792. let count = 0;
  12793. if (codecObj.audio) {
  12794. count++;
  12795. }
  12796. if (codecObj.video) {
  12797. count++;
  12798. }
  12799. return count;
  12800. };
  12801. /**
  12802. * Calculates the codec strings for a working configuration of
  12803. * SourceBuffers to play variant streams in a main playlist. If
  12804. * there is no possible working configuration, an empty object will be
  12805. * returned.
  12806. *
  12807. * @param main {Object} the m3u8 object for the main playlist
  12808. * @param media {Object} the m3u8 object for the variant playlist
  12809. * @return {Object} the codec strings.
  12810. *
  12811. * @private
  12812. */
  12813. const codecsForPlaylist = function (main, media) {
  12814. const mediaAttributes = media.attributes || {};
  12815. const codecInfo = unwrapCodecList(getCodecs(media) || []); // HLS with multiple-audio tracks must always get an audio codec.
  12816. // Put another way, there is no way to have a video-only multiple-audio HLS!
  12817. if (isMaat(main, media) && !codecInfo.audio) {
  12818. if (!isMuxed(main, media)) {
  12819. // It is possible for codecs to be specified on the audio media group playlist but
  12820. // not on the rendition playlist. This is mostly the case for DASH, where audio and
  12821. // video are always separate (and separately specified).
  12822. const defaultCodecs = unwrapCodecList(codecs_js.codecsFromDefault(main, mediaAttributes.AUDIO) || []);
  12823. if (defaultCodecs.audio) {
  12824. codecInfo.audio = defaultCodecs.audio;
  12825. }
  12826. }
  12827. }
  12828. return codecInfo;
  12829. };
  12830. const logFn = logger('PlaylistSelector');
  12831. const representationToString = function (representation) {
  12832. if (!representation || !representation.playlist) {
  12833. return;
  12834. }
  12835. const playlist = representation.playlist;
  12836. return JSON.stringify({
  12837. id: playlist.id,
  12838. bandwidth: representation.bandwidth,
  12839. width: representation.width,
  12840. height: representation.height,
  12841. codecs: playlist.attributes && playlist.attributes.CODECS || ''
  12842. });
  12843. }; // Utilities
  12844. /**
  12845. * Returns the CSS value for the specified property on an element
  12846. * using `getComputedStyle`. Firefox has a long-standing issue where
  12847. * getComputedStyle() may return null when running in an iframe with
  12848. * `display: none`.
  12849. *
  12850. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  12851. * @param {HTMLElement} el the htmlelement to work on
  12852. * @param {string} the proprety to get the style for
  12853. */
  12854. const safeGetComputedStyle = function (el, property) {
  12855. if (!el) {
  12856. return '';
  12857. }
  12858. const result = window__default["default"].getComputedStyle(el);
  12859. if (!result) {
  12860. return '';
  12861. }
  12862. return result[property];
  12863. };
  12864. /**
  12865. * Resuable stable sort function
  12866. *
  12867. * @param {Playlists} array
  12868. * @param {Function} sortFn Different comparators
  12869. * @function stableSort
  12870. */
  12871. const stableSort = function (array, sortFn) {
  12872. const newArray = array.slice();
  12873. array.sort(function (left, right) {
  12874. const cmp = sortFn(left, right);
  12875. if (cmp === 0) {
  12876. return newArray.indexOf(left) - newArray.indexOf(right);
  12877. }
  12878. return cmp;
  12879. });
  12880. };
  12881. /**
  12882. * A comparator function to sort two playlist object by bandwidth.
  12883. *
  12884. * @param {Object} left a media playlist object
  12885. * @param {Object} right a media playlist object
  12886. * @return {number} Greater than zero if the bandwidth attribute of
  12887. * left is greater than the corresponding attribute of right. Less
  12888. * than zero if the bandwidth of right is greater than left and
  12889. * exactly zero if the two are equal.
  12890. */
  12891. const comparePlaylistBandwidth = function (left, right) {
  12892. let leftBandwidth;
  12893. let rightBandwidth;
  12894. if (left.attributes.BANDWIDTH) {
  12895. leftBandwidth = left.attributes.BANDWIDTH;
  12896. }
  12897. leftBandwidth = leftBandwidth || window__default["default"].Number.MAX_VALUE;
  12898. if (right.attributes.BANDWIDTH) {
  12899. rightBandwidth = right.attributes.BANDWIDTH;
  12900. }
  12901. rightBandwidth = rightBandwidth || window__default["default"].Number.MAX_VALUE;
  12902. return leftBandwidth - rightBandwidth;
  12903. };
  12904. /**
  12905. * A comparator function to sort two playlist object by resolution (width).
  12906. *
  12907. * @param {Object} left a media playlist object
  12908. * @param {Object} right a media playlist object
  12909. * @return {number} Greater than zero if the resolution.width attribute of
  12910. * left is greater than the corresponding attribute of right. Less
  12911. * than zero if the resolution.width of right is greater than left and
  12912. * exactly zero if the two are equal.
  12913. */
  12914. const comparePlaylistResolution = function (left, right) {
  12915. let leftWidth;
  12916. let rightWidth;
  12917. if (left.attributes.RESOLUTION && left.attributes.RESOLUTION.width) {
  12918. leftWidth = left.attributes.RESOLUTION.width;
  12919. }
  12920. leftWidth = leftWidth || window__default["default"].Number.MAX_VALUE;
  12921. if (right.attributes.RESOLUTION && right.attributes.RESOLUTION.width) {
  12922. rightWidth = right.attributes.RESOLUTION.width;
  12923. }
  12924. rightWidth = rightWidth || window__default["default"].Number.MAX_VALUE; // NOTE - Fallback to bandwidth sort as appropriate in cases where multiple renditions
  12925. // have the same media dimensions/ resolution
  12926. if (leftWidth === rightWidth && left.attributes.BANDWIDTH && right.attributes.BANDWIDTH) {
  12927. return left.attributes.BANDWIDTH - right.attributes.BANDWIDTH;
  12928. }
  12929. return leftWidth - rightWidth;
  12930. };
  12931. /**
  12932. * Chooses the appropriate media playlist based on bandwidth and player size
  12933. *
  12934. * @param {Object} main
  12935. * Object representation of the main manifest
  12936. * @param {number} playerBandwidth
  12937. * Current calculated bandwidth of the player
  12938. * @param {number} playerWidth
  12939. * Current width of the player element (should account for the device pixel ratio)
  12940. * @param {number} playerHeight
  12941. * Current height of the player element (should account for the device pixel ratio)
  12942. * @param {boolean} limitRenditionByPlayerDimensions
  12943. * True if the player width and height should be used during the selection, false otherwise
  12944. * @param {Object} playlistController
  12945. * the current playlistController object
  12946. * @return {Playlist} the highest bitrate playlist less than the
  12947. * currently detected bandwidth, accounting for some amount of
  12948. * bandwidth variance
  12949. */
  12950. let simpleSelector = function (main, playerBandwidth, playerWidth, playerHeight, limitRenditionByPlayerDimensions, playlistController) {
  12951. // If we end up getting called before `main` is available, exit early
  12952. if (!main) {
  12953. return;
  12954. }
  12955. const options = {
  12956. bandwidth: playerBandwidth,
  12957. width: playerWidth,
  12958. height: playerHeight,
  12959. limitRenditionByPlayerDimensions
  12960. };
  12961. let playlists = main.playlists; // if playlist is audio only, select between currently active audio group playlists.
  12962. if (Playlist.isAudioOnly(main)) {
  12963. playlists = playlistController.getAudioTrackPlaylists_(); // add audioOnly to options so that we log audioOnly: true
  12964. // at the buttom of this function for debugging.
  12965. options.audioOnly = true;
  12966. } // convert the playlists to an intermediary representation to make comparisons easier
  12967. let sortedPlaylistReps = playlists.map(playlist => {
  12968. let bandwidth;
  12969. const width = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.width;
  12970. const height = playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height;
  12971. bandwidth = playlist.attributes && playlist.attributes.BANDWIDTH;
  12972. bandwidth = bandwidth || window__default["default"].Number.MAX_VALUE;
  12973. return {
  12974. bandwidth,
  12975. width,
  12976. height,
  12977. playlist
  12978. };
  12979. });
  12980. stableSort(sortedPlaylistReps, (left, right) => left.bandwidth - right.bandwidth); // filter out any playlists that have been excluded due to
  12981. // incompatible configurations
  12982. sortedPlaylistReps = sortedPlaylistReps.filter(rep => !Playlist.isIncompatible(rep.playlist)); // filter out any playlists that have been disabled manually through the representations
  12983. // api or excluded temporarily due to playback errors.
  12984. let enabledPlaylistReps = sortedPlaylistReps.filter(rep => Playlist.isEnabled(rep.playlist));
  12985. if (!enabledPlaylistReps.length) {
  12986. // if there are no enabled playlists, then they have all been excluded or disabled
  12987. // by the user through the representations api. In this case, ignore exclusion and
  12988. // fallback to what the user wants by using playlists the user has not disabled.
  12989. enabledPlaylistReps = sortedPlaylistReps.filter(rep => !Playlist.isDisabled(rep.playlist));
  12990. } // filter out any variant that has greater effective bitrate
  12991. // than the current estimated bandwidth
  12992. const bandwidthPlaylistReps = enabledPlaylistReps.filter(rep => rep.bandwidth * Config.BANDWIDTH_VARIANCE < playerBandwidth);
  12993. let highestRemainingBandwidthRep = bandwidthPlaylistReps[bandwidthPlaylistReps.length - 1]; // get all of the renditions with the same (highest) bandwidth
  12994. // and then taking the very first element
  12995. const bandwidthBestRep = bandwidthPlaylistReps.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0]; // if we're not going to limit renditions by player size, make an early decision.
  12996. if (limitRenditionByPlayerDimensions === false) {
  12997. const chosenRep = bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
  12998. if (chosenRep && chosenRep.playlist) {
  12999. let type = 'sortedPlaylistReps';
  13000. if (bandwidthBestRep) {
  13001. type = 'bandwidthBestRep';
  13002. }
  13003. if (enabledPlaylistReps[0]) {
  13004. type = 'enabledPlaylistReps';
  13005. }
  13006. logFn(`choosing ${representationToString(chosenRep)} using ${type} with options`, options);
  13007. return chosenRep.playlist;
  13008. }
  13009. logFn('could not choose a playlist with options', options);
  13010. return null;
  13011. } // filter out playlists without resolution information
  13012. const haveResolution = bandwidthPlaylistReps.filter(rep => rep.width && rep.height); // sort variants by resolution
  13013. stableSort(haveResolution, (left, right) => left.width - right.width); // if we have the exact resolution as the player use it
  13014. const resolutionBestRepList = haveResolution.filter(rep => rep.width === playerWidth && rep.height === playerHeight);
  13015. highestRemainingBandwidthRep = resolutionBestRepList[resolutionBestRepList.length - 1]; // ensure that we pick the highest bandwidth variant that have exact resolution
  13016. const resolutionBestRep = resolutionBestRepList.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0];
  13017. let resolutionPlusOneList;
  13018. let resolutionPlusOneSmallest;
  13019. let resolutionPlusOneRep; // find the smallest variant that is larger than the player
  13020. // if there is no match of exact resolution
  13021. if (!resolutionBestRep) {
  13022. resolutionPlusOneList = haveResolution.filter(rep => rep.width > playerWidth || rep.height > playerHeight); // find all the variants have the same smallest resolution
  13023. resolutionPlusOneSmallest = resolutionPlusOneList.filter(rep => rep.width === resolutionPlusOneList[0].width && rep.height === resolutionPlusOneList[0].height); // ensure that we also pick the highest bandwidth variant that
  13024. // is just-larger-than the video player
  13025. highestRemainingBandwidthRep = resolutionPlusOneSmallest[resolutionPlusOneSmallest.length - 1];
  13026. resolutionPlusOneRep = resolutionPlusOneSmallest.filter(rep => rep.bandwidth === highestRemainingBandwidthRep.bandwidth)[0];
  13027. }
  13028. let leastPixelDiffRep; // If this selector proves to be better than others,
  13029. // resolutionPlusOneRep and resolutionBestRep and all
  13030. // the code involving them should be removed.
  13031. if (playlistController.leastPixelDiffSelector) {
  13032. // find the variant that is closest to the player's pixel size
  13033. const leastPixelDiffList = haveResolution.map(rep => {
  13034. rep.pixelDiff = Math.abs(rep.width - playerWidth) + Math.abs(rep.height - playerHeight);
  13035. return rep;
  13036. }); // get the highest bandwidth, closest resolution playlist
  13037. stableSort(leastPixelDiffList, (left, right) => {
  13038. // sort by highest bandwidth if pixelDiff is the same
  13039. if (left.pixelDiff === right.pixelDiff) {
  13040. return right.bandwidth - left.bandwidth;
  13041. }
  13042. return left.pixelDiff - right.pixelDiff;
  13043. });
  13044. leastPixelDiffRep = leastPixelDiffList[0];
  13045. } // fallback chain of variants
  13046. const chosenRep = leastPixelDiffRep || resolutionPlusOneRep || resolutionBestRep || bandwidthBestRep || enabledPlaylistReps[0] || sortedPlaylistReps[0];
  13047. if (chosenRep && chosenRep.playlist) {
  13048. let type = 'sortedPlaylistReps';
  13049. if (leastPixelDiffRep) {
  13050. type = 'leastPixelDiffRep';
  13051. } else if (resolutionPlusOneRep) {
  13052. type = 'resolutionPlusOneRep';
  13053. } else if (resolutionBestRep) {
  13054. type = 'resolutionBestRep';
  13055. } else if (bandwidthBestRep) {
  13056. type = 'bandwidthBestRep';
  13057. } else if (enabledPlaylistReps[0]) {
  13058. type = 'enabledPlaylistReps';
  13059. }
  13060. logFn(`choosing ${representationToString(chosenRep)} using ${type} with options`, options);
  13061. return chosenRep.playlist;
  13062. }
  13063. logFn('could not choose a playlist with options', options);
  13064. return null;
  13065. };
  13066. /**
  13067. * Chooses the appropriate media playlist based on the most recent
  13068. * bandwidth estimate and the player size.
  13069. *
  13070. * Expects to be called within the context of an instance of VhsHandler
  13071. *
  13072. * @return {Playlist} the highest bitrate playlist less than the
  13073. * currently detected bandwidth, accounting for some amount of
  13074. * bandwidth variance
  13075. */
  13076. const lastBandwidthSelector = function () {
  13077. const pixelRatio = this.useDevicePixelRatio ? window__default["default"].devicePixelRatio || 1 : 1;
  13078. return simpleSelector(this.playlists.main, this.systemBandwidth, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.playlistController_);
  13079. };
  13080. /**
  13081. * Chooses the appropriate media playlist based on an
  13082. * exponential-weighted moving average of the bandwidth after
  13083. * filtering for player size.
  13084. *
  13085. * Expects to be called within the context of an instance of VhsHandler
  13086. *
  13087. * @param {number} decay - a number between 0 and 1. Higher values of
  13088. * this parameter will cause previous bandwidth estimates to lose
  13089. * significance more quickly.
  13090. * @return {Function} a function which can be invoked to create a new
  13091. * playlist selector function.
  13092. * @see https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
  13093. */
  13094. const movingAverageBandwidthSelector = function (decay) {
  13095. let average = -1;
  13096. let lastSystemBandwidth = -1;
  13097. if (decay < 0 || decay > 1) {
  13098. throw new Error('Moving average bandwidth decay must be between 0 and 1.');
  13099. }
  13100. return function () {
  13101. const pixelRatio = this.useDevicePixelRatio ? window__default["default"].devicePixelRatio || 1 : 1;
  13102. if (average < 0) {
  13103. average = this.systemBandwidth;
  13104. lastSystemBandwidth = this.systemBandwidth;
  13105. } // stop the average value from decaying for every 250ms
  13106. // when the systemBandwidth is constant
  13107. // and
  13108. // stop average from setting to a very low value when the
  13109. // systemBandwidth becomes 0 in case of chunk cancellation
  13110. if (this.systemBandwidth > 0 && this.systemBandwidth !== lastSystemBandwidth) {
  13111. average = decay * this.systemBandwidth + (1 - decay) * average;
  13112. lastSystemBandwidth = this.systemBandwidth;
  13113. }
  13114. return simpleSelector(this.playlists.main, average, parseInt(safeGetComputedStyle(this.tech_.el(), 'width'), 10) * pixelRatio, parseInt(safeGetComputedStyle(this.tech_.el(), 'height'), 10) * pixelRatio, this.limitRenditionByPlayerDimensions, this.playlistController_);
  13115. };
  13116. };
  13117. /**
  13118. * Chooses the appropriate media playlist based on the potential to rebuffer
  13119. *
  13120. * @param {Object} settings
  13121. * Object of information required to use this selector
  13122. * @param {Object} settings.main
  13123. * Object representation of the main manifest
  13124. * @param {number} settings.currentTime
  13125. * The current time of the player
  13126. * @param {number} settings.bandwidth
  13127. * Current measured bandwidth
  13128. * @param {number} settings.duration
  13129. * Duration of the media
  13130. * @param {number} settings.segmentDuration
  13131. * Segment duration to be used in round trip time calculations
  13132. * @param {number} settings.timeUntilRebuffer
  13133. * Time left in seconds until the player has to rebuffer
  13134. * @param {number} settings.currentTimeline
  13135. * The current timeline segments are being loaded from
  13136. * @param {SyncController} settings.syncController
  13137. * SyncController for determining if we have a sync point for a given playlist
  13138. * @return {Object|null}
  13139. * {Object} return.playlist
  13140. * The highest bandwidth playlist with the least amount of rebuffering
  13141. * {Number} return.rebufferingImpact
  13142. * The amount of time in seconds switching to this playlist will rebuffer. A
  13143. * negative value means that switching will cause zero rebuffering.
  13144. */
  13145. const minRebufferMaxBandwidthSelector = function (settings) {
  13146. const {
  13147. main,
  13148. currentTime,
  13149. bandwidth,
  13150. duration,
  13151. segmentDuration,
  13152. timeUntilRebuffer,
  13153. currentTimeline,
  13154. syncController
  13155. } = settings; // filter out any playlists that have been excluded due to
  13156. // incompatible configurations
  13157. const compatiblePlaylists = main.playlists.filter(playlist => !Playlist.isIncompatible(playlist)); // filter out any playlists that have been disabled manually through the representations
  13158. // api or excluded temporarily due to playback errors.
  13159. let enabledPlaylists = compatiblePlaylists.filter(Playlist.isEnabled);
  13160. if (!enabledPlaylists.length) {
  13161. // if there are no enabled playlists, then they have all been excluded or disabled
  13162. // by the user through the representations api. In this case, ignore exclusion and
  13163. // fallback to what the user wants by using playlists the user has not disabled.
  13164. enabledPlaylists = compatiblePlaylists.filter(playlist => !Playlist.isDisabled(playlist));
  13165. }
  13166. const bandwidthPlaylists = enabledPlaylists.filter(Playlist.hasAttribute.bind(null, 'BANDWIDTH'));
  13167. const rebufferingEstimates = bandwidthPlaylists.map(playlist => {
  13168. const syncPoint = syncController.getSyncPoint(playlist, duration, currentTimeline, currentTime); // If there is no sync point for this playlist, switching to it will require a
  13169. // sync request first. This will double the request time
  13170. const numRequests = syncPoint ? 1 : 2;
  13171. const requestTimeEstimate = Playlist.estimateSegmentRequestTime(segmentDuration, bandwidth, playlist);
  13172. const rebufferingImpact = requestTimeEstimate * numRequests - timeUntilRebuffer;
  13173. return {
  13174. playlist,
  13175. rebufferingImpact
  13176. };
  13177. });
  13178. const noRebufferingPlaylists = rebufferingEstimates.filter(estimate => estimate.rebufferingImpact <= 0); // Sort by bandwidth DESC
  13179. stableSort(noRebufferingPlaylists, (a, b) => comparePlaylistBandwidth(b.playlist, a.playlist));
  13180. if (noRebufferingPlaylists.length) {
  13181. return noRebufferingPlaylists[0];
  13182. }
  13183. stableSort(rebufferingEstimates, (a, b) => a.rebufferingImpact - b.rebufferingImpact);
  13184. return rebufferingEstimates[0] || null;
  13185. };
  13186. /**
  13187. * Chooses the appropriate media playlist, which in this case is the lowest bitrate
  13188. * one with video. If no renditions with video exist, return the lowest audio rendition.
  13189. *
  13190. * Expects to be called within the context of an instance of VhsHandler
  13191. *
  13192. * @return {Object|null}
  13193. * {Object} return.playlist
  13194. * The lowest bitrate playlist that contains a video codec. If no such rendition
  13195. * exists pick the lowest audio rendition.
  13196. */
  13197. const lowestBitrateCompatibleVariantSelector = function () {
  13198. // filter out any playlists that have been excluded due to
  13199. // incompatible configurations or playback errors
  13200. const playlists = this.playlists.main.playlists.filter(Playlist.isEnabled); // Sort ascending by bitrate
  13201. stableSort(playlists, (a, b) => comparePlaylistBandwidth(a, b)); // Parse and assume that playlists with no video codec have no video
  13202. // (this is not necessarily true, although it is generally true).
  13203. //
  13204. // If an entire manifest has no valid videos everything will get filtered
  13205. // out.
  13206. const playlistsWithVideo = playlists.filter(playlist => !!codecsForPlaylist(this.playlists.main, playlist).video);
  13207. return playlistsWithVideo[0] || null;
  13208. };
  13209. /**
  13210. * Combine all segments into a single Uint8Array
  13211. *
  13212. * @param {Object} segmentObj
  13213. * @return {Uint8Array} concatenated bytes
  13214. * @private
  13215. */
  13216. const concatSegments = segmentObj => {
  13217. let offset = 0;
  13218. let tempBuffer;
  13219. if (segmentObj.bytes) {
  13220. tempBuffer = new Uint8Array(segmentObj.bytes); // combine the individual segments into one large typed-array
  13221. segmentObj.segments.forEach(segment => {
  13222. tempBuffer.set(segment, offset);
  13223. offset += segment.byteLength;
  13224. });
  13225. }
  13226. return tempBuffer;
  13227. };
  13228. /**
  13229. * @file text-tracks.js
  13230. */
  13231. /**
  13232. * Create captions text tracks on video.js if they do not exist
  13233. *
  13234. * @param {Object} inbandTextTracks a reference to current inbandTextTracks
  13235. * @param {Object} tech the video.js tech
  13236. * @param {Object} captionStream the caption stream to create
  13237. * @private
  13238. */
  13239. const createCaptionsTrackIfNotExists = function (inbandTextTracks, tech, captionStream) {
  13240. if (!inbandTextTracks[captionStream]) {
  13241. tech.trigger({
  13242. type: 'usage',
  13243. name: 'vhs-608'
  13244. });
  13245. let instreamId = captionStream; // we need to translate SERVICEn for 708 to how mux.js currently labels them
  13246. if (/^cc708_/.test(captionStream)) {
  13247. instreamId = 'SERVICE' + captionStream.split('_')[1];
  13248. }
  13249. const track = tech.textTracks().getTrackById(instreamId);
  13250. if (track) {
  13251. // Resuse an existing track with a CC# id because this was
  13252. // very likely created by videojs-contrib-hls from information
  13253. // in the m3u8 for us to use
  13254. inbandTextTracks[captionStream] = track;
  13255. } else {
  13256. // This section gets called when we have caption services that aren't specified in the manifest.
  13257. // Manifest level caption services are handled in media-groups.js under CLOSED-CAPTIONS.
  13258. const captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
  13259. let label = captionStream;
  13260. let language = captionStream;
  13261. let def = false;
  13262. const captionService = captionServices[instreamId];
  13263. if (captionService) {
  13264. label = captionService.label;
  13265. language = captionService.language;
  13266. def = captionService.default;
  13267. } // Otherwise, create a track with the default `CC#` label and
  13268. // without a language
  13269. inbandTextTracks[captionStream] = tech.addRemoteTextTrack({
  13270. kind: 'captions',
  13271. id: instreamId,
  13272. // TODO: investigate why this doesn't seem to turn the caption on by default
  13273. default: def,
  13274. label,
  13275. language
  13276. }, false).track;
  13277. }
  13278. }
  13279. };
  13280. /**
  13281. * Add caption text track data to a source handler given an array of captions
  13282. *
  13283. * @param {Object}
  13284. * @param {Object} inbandTextTracks the inband text tracks
  13285. * @param {number} timestampOffset the timestamp offset of the source buffer
  13286. * @param {Array} captionArray an array of caption data
  13287. * @private
  13288. */
  13289. const addCaptionData = function ({
  13290. inbandTextTracks,
  13291. captionArray,
  13292. timestampOffset
  13293. }) {
  13294. if (!captionArray) {
  13295. return;
  13296. }
  13297. const Cue = window__default["default"].WebKitDataCue || window__default["default"].VTTCue;
  13298. captionArray.forEach(caption => {
  13299. const track = caption.stream; // in CEA 608 captions, video.js/mux.js sends a content array
  13300. // with positioning data
  13301. if (caption.content) {
  13302. caption.content.forEach(value => {
  13303. const cue = new Cue(caption.startTime + timestampOffset, caption.endTime + timestampOffset, value.text);
  13304. cue.line = value.line;
  13305. cue.align = 'left';
  13306. cue.position = value.position;
  13307. cue.positionAlign = 'line-left';
  13308. inbandTextTracks[track].addCue(cue);
  13309. });
  13310. } else {
  13311. // otherwise, a text value with combined captions is sent
  13312. inbandTextTracks[track].addCue(new Cue(caption.startTime + timestampOffset, caption.endTime + timestampOffset, caption.text));
  13313. }
  13314. });
  13315. };
  13316. /**
  13317. * Define properties on a cue for backwards compatability,
  13318. * but warn the user that the way that they are using it
  13319. * is depricated and will be removed at a later date.
  13320. *
  13321. * @param {Cue} cue the cue to add the properties on
  13322. * @private
  13323. */
  13324. const deprecateOldCue = function (cue) {
  13325. Object.defineProperties(cue.frame, {
  13326. id: {
  13327. get() {
  13328. videojs__default["default"].log.warn('cue.frame.id is deprecated. Use cue.value.key instead.');
  13329. return cue.value.key;
  13330. }
  13331. },
  13332. value: {
  13333. get() {
  13334. videojs__default["default"].log.warn('cue.frame.value is deprecated. Use cue.value.data instead.');
  13335. return cue.value.data;
  13336. }
  13337. },
  13338. privateData: {
  13339. get() {
  13340. videojs__default["default"].log.warn('cue.frame.privateData is deprecated. Use cue.value.data instead.');
  13341. return cue.value.data;
  13342. }
  13343. }
  13344. });
  13345. };
  13346. /**
  13347. * Add metadata text track data to a source handler given an array of metadata
  13348. *
  13349. * @param {Object}
  13350. * @param {Object} inbandTextTracks the inband text tracks
  13351. * @param {Array} metadataArray an array of meta data
  13352. * @param {number} timestampOffset the timestamp offset of the source buffer
  13353. * @param {number} videoDuration the duration of the video
  13354. * @private
  13355. */
  13356. const addMetadata = ({
  13357. inbandTextTracks,
  13358. metadataArray,
  13359. timestampOffset,
  13360. videoDuration
  13361. }) => {
  13362. if (!metadataArray) {
  13363. return;
  13364. }
  13365. const Cue = window__default["default"].WebKitDataCue || window__default["default"].VTTCue;
  13366. const metadataTrack = inbandTextTracks.metadataTrack_;
  13367. if (!metadataTrack) {
  13368. return;
  13369. }
  13370. metadataArray.forEach(metadata => {
  13371. const time = metadata.cueTime + timestampOffset; // if time isn't a finite number between 0 and Infinity, like NaN,
  13372. // ignore this bit of metadata.
  13373. // This likely occurs when you have an non-timed ID3 tag like TIT2,
  13374. // which is the "Title/Songname/Content description" frame
  13375. if (typeof time !== 'number' || window__default["default"].isNaN(time) || time < 0 || !(time < Infinity)) {
  13376. return;
  13377. } // If we have no frames, we can't create a cue.
  13378. if (!metadata.frames || !metadata.frames.length) {
  13379. return;
  13380. }
  13381. metadata.frames.forEach(frame => {
  13382. const cue = new Cue(time, time, frame.value || frame.url || frame.data || '');
  13383. cue.frame = frame;
  13384. cue.value = frame;
  13385. deprecateOldCue(cue);
  13386. metadataTrack.addCue(cue);
  13387. });
  13388. });
  13389. if (!metadataTrack.cues || !metadataTrack.cues.length) {
  13390. return;
  13391. } // Updating the metadeta cues so that
  13392. // the endTime of each cue is the startTime of the next cue
  13393. // the endTime of last cue is the duration of the video
  13394. const cues = metadataTrack.cues;
  13395. const cuesArray = []; // Create a copy of the TextTrackCueList...
  13396. // ...disregarding cues with a falsey value
  13397. for (let i = 0; i < cues.length; i++) {
  13398. if (cues[i]) {
  13399. cuesArray.push(cues[i]);
  13400. }
  13401. } // Group cues by their startTime value
  13402. const cuesGroupedByStartTime = cuesArray.reduce((obj, cue) => {
  13403. const timeSlot = obj[cue.startTime] || [];
  13404. timeSlot.push(cue);
  13405. obj[cue.startTime] = timeSlot;
  13406. return obj;
  13407. }, {}); // Sort startTimes by ascending order
  13408. const sortedStartTimes = Object.keys(cuesGroupedByStartTime).sort((a, b) => Number(a) - Number(b)); // Map each cue group's endTime to the next group's startTime
  13409. sortedStartTimes.forEach((startTime, idx) => {
  13410. const cueGroup = cuesGroupedByStartTime[startTime];
  13411. const finiteDuration = isFinite(videoDuration) ? videoDuration : startTime;
  13412. const nextTime = Number(sortedStartTimes[idx + 1]) || finiteDuration; // Map each cue's endTime the next group's startTime
  13413. cueGroup.forEach(cue => {
  13414. cue.endTime = nextTime;
  13415. });
  13416. });
  13417. }; // object for mapping daterange attributes
  13418. const dateRangeAttr = {
  13419. id: 'ID',
  13420. class: 'CLASS',
  13421. startDate: 'START-DATE',
  13422. duration: 'DURATION',
  13423. endDate: 'END-DATE',
  13424. endOnNext: 'END-ON-NEXT',
  13425. plannedDuration: 'PLANNED-DURATION',
  13426. scte35Out: 'SCTE35-OUT',
  13427. scte35In: 'SCTE35-IN'
  13428. };
  13429. const dateRangeKeysToOmit = new Set(['id', 'class', 'startDate', 'duration', 'endDate', 'endOnNext', 'startTime', 'endTime', 'processDateRange']);
  13430. /**
  13431. * Add DateRange metadata text track to a source handler given an array of metadata
  13432. *
  13433. * @param {Object}
  13434. * @param {Object} inbandTextTracks the inband text tracks
  13435. * @param {Array} dateRanges parsed media playlist
  13436. * @private
  13437. */
  13438. const addDateRangeMetadata = ({
  13439. inbandTextTracks,
  13440. dateRanges
  13441. }) => {
  13442. const metadataTrack = inbandTextTracks.metadataTrack_;
  13443. if (!metadataTrack) {
  13444. return;
  13445. }
  13446. const Cue = window__default["default"].WebKitDataCue || window__default["default"].VTTCue;
  13447. dateRanges.forEach(dateRange => {
  13448. // we generate multiple cues for each date range with different attributes
  13449. for (const key of Object.keys(dateRange)) {
  13450. if (dateRangeKeysToOmit.has(key)) {
  13451. continue;
  13452. }
  13453. const cue = new Cue(dateRange.startTime, dateRange.endTime, '');
  13454. cue.id = dateRange.id;
  13455. cue.type = 'com.apple.quicktime.HLS';
  13456. cue.value = {
  13457. key: dateRangeAttr[key],
  13458. data: dateRange[key]
  13459. };
  13460. if (key === 'scte35Out' || key === 'scte35In') {
  13461. cue.value.data = new Uint8Array(cue.value.data.match(/[\da-f]{2}/gi)).buffer;
  13462. }
  13463. metadataTrack.addCue(cue);
  13464. }
  13465. dateRange.processDateRange();
  13466. });
  13467. };
  13468. /**
  13469. * Create metadata text track on video.js if it does not exist
  13470. *
  13471. * @param {Object} inbandTextTracks a reference to current inbandTextTracks
  13472. * @param {string} dispatchType the inband metadata track dispatch type
  13473. * @param {Object} tech the video.js tech
  13474. * @private
  13475. */
  13476. const createMetadataTrackIfNotExists = (inbandTextTracks, dispatchType, tech) => {
  13477. if (inbandTextTracks.metadataTrack_) {
  13478. return;
  13479. }
  13480. inbandTextTracks.metadataTrack_ = tech.addRemoteTextTrack({
  13481. kind: 'metadata',
  13482. label: 'Timed Metadata'
  13483. }, false).track;
  13484. if (!videojs__default["default"].browser.IS_ANY_SAFARI) {
  13485. inbandTextTracks.metadataTrack_.inBandMetadataTrackDispatchType = dispatchType;
  13486. }
  13487. };
  13488. /**
  13489. * Remove cues from a track on video.js.
  13490. *
  13491. * @param {Double} start start of where we should remove the cue
  13492. * @param {Double} end end of where the we should remove the cue
  13493. * @param {Object} track the text track to remove the cues from
  13494. * @private
  13495. */
  13496. const removeCuesFromTrack = function (start, end, track) {
  13497. let i;
  13498. let cue;
  13499. if (!track) {
  13500. return;
  13501. }
  13502. if (!track.cues) {
  13503. return;
  13504. }
  13505. i = track.cues.length;
  13506. while (i--) {
  13507. cue = track.cues[i]; // Remove any cue within the provided start and end time
  13508. if (cue.startTime >= start && cue.endTime <= end) {
  13509. track.removeCue(cue);
  13510. }
  13511. }
  13512. };
  13513. /**
  13514. * Remove duplicate cues from a track on video.js (a cue is considered a
  13515. * duplicate if it has the same time interval and text as another)
  13516. *
  13517. * @param {Object} track the text track to remove the duplicate cues from
  13518. * @private
  13519. */
  13520. const removeDuplicateCuesFromTrack = function (track) {
  13521. const cues = track.cues;
  13522. if (!cues) {
  13523. return;
  13524. }
  13525. const uniqueCues = {};
  13526. for (let i = cues.length - 1; i >= 0; i--) {
  13527. const cue = cues[i];
  13528. const cueKey = `${cue.startTime}-${cue.endTime}-${cue.text}`;
  13529. if (uniqueCues[cueKey]) {
  13530. track.removeCue(cue);
  13531. } else {
  13532. uniqueCues[cueKey] = cue;
  13533. }
  13534. }
  13535. };
  13536. /**
  13537. * Returns a list of gops in the buffer that have a pts value of 3 seconds or more in
  13538. * front of current time.
  13539. *
  13540. * @param {Array} buffer
  13541. * The current buffer of gop information
  13542. * @param {number} currentTime
  13543. * The current time
  13544. * @param {Double} mapping
  13545. * Offset to map display time to stream presentation time
  13546. * @return {Array}
  13547. * List of gops considered safe to append over
  13548. */
  13549. const gopsSafeToAlignWith = (buffer, currentTime, mapping) => {
  13550. if (typeof currentTime === 'undefined' || currentTime === null || !buffer.length) {
  13551. return [];
  13552. } // pts value for current time + 3 seconds to give a bit more wiggle room
  13553. const currentTimePts = Math.ceil((currentTime - mapping + 3) * clock.ONE_SECOND_IN_TS);
  13554. let i;
  13555. for (i = 0; i < buffer.length; i++) {
  13556. if (buffer[i].pts > currentTimePts) {
  13557. break;
  13558. }
  13559. }
  13560. return buffer.slice(i);
  13561. };
  13562. /**
  13563. * Appends gop information (timing and byteLength) received by the transmuxer for the
  13564. * gops appended in the last call to appendBuffer
  13565. *
  13566. * @param {Array} buffer
  13567. * The current buffer of gop information
  13568. * @param {Array} gops
  13569. * List of new gop information
  13570. * @param {boolean} replace
  13571. * If true, replace the buffer with the new gop information. If false, append the
  13572. * new gop information to the buffer in the right location of time.
  13573. * @return {Array}
  13574. * Updated list of gop information
  13575. */
  13576. const updateGopBuffer = (buffer, gops, replace) => {
  13577. if (!gops.length) {
  13578. return buffer;
  13579. }
  13580. if (replace) {
  13581. // If we are in safe append mode, then completely overwrite the gop buffer
  13582. // with the most recent appeneded data. This will make sure that when appending
  13583. // future segments, we only try to align with gops that are both ahead of current
  13584. // time and in the last segment appended.
  13585. return gops.slice();
  13586. }
  13587. const start = gops[0].pts;
  13588. let i = 0;
  13589. for (i; i < buffer.length; i++) {
  13590. if (buffer[i].pts >= start) {
  13591. break;
  13592. }
  13593. }
  13594. return buffer.slice(0, i).concat(gops);
  13595. };
  13596. /**
  13597. * Removes gop information in buffer that overlaps with provided start and end
  13598. *
  13599. * @param {Array} buffer
  13600. * The current buffer of gop information
  13601. * @param {Double} start
  13602. * position to start the remove at
  13603. * @param {Double} end
  13604. * position to end the remove at
  13605. * @param {Double} mapping
  13606. * Offset to map display time to stream presentation time
  13607. */
  13608. const removeGopBuffer = (buffer, start, end, mapping) => {
  13609. const startPts = Math.ceil((start - mapping) * clock.ONE_SECOND_IN_TS);
  13610. const endPts = Math.ceil((end - mapping) * clock.ONE_SECOND_IN_TS);
  13611. const updatedBuffer = buffer.slice();
  13612. let i = buffer.length;
  13613. while (i--) {
  13614. if (buffer[i].pts <= endPts) {
  13615. break;
  13616. }
  13617. }
  13618. if (i === -1) {
  13619. // no removal because end of remove range is before start of buffer
  13620. return updatedBuffer;
  13621. }
  13622. let j = i + 1;
  13623. while (j--) {
  13624. if (buffer[j].pts <= startPts) {
  13625. break;
  13626. }
  13627. } // clamp remove range start to 0 index
  13628. j = Math.max(j, 0);
  13629. updatedBuffer.splice(j, i - j + 1);
  13630. return updatedBuffer;
  13631. };
  13632. const shallowEqual = function (a, b) {
  13633. // if both are undefined
  13634. // or one or the other is undefined
  13635. // they are not equal
  13636. if (!a && !b || !a && b || a && !b) {
  13637. return false;
  13638. } // they are the same object and thus, equal
  13639. if (a === b) {
  13640. return true;
  13641. } // sort keys so we can make sure they have
  13642. // all the same keys later.
  13643. const akeys = Object.keys(a).sort();
  13644. const bkeys = Object.keys(b).sort(); // different number of keys, not equal
  13645. if (akeys.length !== bkeys.length) {
  13646. return false;
  13647. }
  13648. for (let i = 0; i < akeys.length; i++) {
  13649. const key = akeys[i]; // different sorted keys, not equal
  13650. if (key !== bkeys[i]) {
  13651. return false;
  13652. } // different values, not equal
  13653. if (a[key] !== b[key]) {
  13654. return false;
  13655. }
  13656. }
  13657. return true;
  13658. };
  13659. // https://www.w3.org/TR/WebIDL-1/#quotaexceedederror
  13660. const QUOTA_EXCEEDED_ERR = 22;
  13661. /**
  13662. * The segment loader has no recourse except to fetch a segment in the
  13663. * current playlist and use the internal timestamps in that segment to
  13664. * generate a syncPoint. This function returns a good candidate index
  13665. * for that process.
  13666. *
  13667. * @param {Array} segments - the segments array from a playlist.
  13668. * @return {number} An index of a segment from the playlist to load
  13669. */
  13670. const getSyncSegmentCandidate = function (currentTimeline, segments, targetTime) {
  13671. segments = segments || [];
  13672. const timelineSegments = [];
  13673. let time = 0;
  13674. for (let i = 0; i < segments.length; i++) {
  13675. const segment = segments[i];
  13676. if (currentTimeline === segment.timeline) {
  13677. timelineSegments.push(i);
  13678. time += segment.duration;
  13679. if (time > targetTime) {
  13680. return i;
  13681. }
  13682. }
  13683. }
  13684. if (timelineSegments.length === 0) {
  13685. return 0;
  13686. } // default to the last timeline segment
  13687. return timelineSegments[timelineSegments.length - 1];
  13688. }; // In the event of a quota exceeded error, keep at least one second of back buffer. This
  13689. // number was arbitrarily chosen and may be updated in the future, but seemed reasonable
  13690. // as a start to prevent any potential issues with removing content too close to the
  13691. // playhead.
  13692. const MIN_BACK_BUFFER = 1; // in ms
  13693. const CHECK_BUFFER_DELAY = 500;
  13694. const finite = num => typeof num === 'number' && isFinite(num); // With most content hovering around 30fps, if a segment has a duration less than a half
  13695. // frame at 30fps or one frame at 60fps, the bandwidth and throughput calculations will
  13696. // not accurately reflect the rest of the content.
  13697. const MIN_SEGMENT_DURATION_TO_SAVE_STATS = 1 / 60;
  13698. const illegalMediaSwitch = (loaderType, startingMedia, trackInfo) => {
  13699. // Although these checks should most likely cover non 'main' types, for now it narrows
  13700. // the scope of our checks.
  13701. if (loaderType !== 'main' || !startingMedia || !trackInfo) {
  13702. return null;
  13703. }
  13704. if (!trackInfo.hasAudio && !trackInfo.hasVideo) {
  13705. return 'Neither audio nor video found in segment.';
  13706. }
  13707. if (startingMedia.hasVideo && !trackInfo.hasVideo) {
  13708. return 'Only audio found in segment when we expected video.' + ' We can\'t switch to audio only from a stream that had video.' + ' To get rid of this message, please add codec information to the manifest.';
  13709. }
  13710. if (!startingMedia.hasVideo && trackInfo.hasVideo) {
  13711. return 'Video found in segment when we expected only audio.' + ' We can\'t switch to a stream with video from an audio only stream.' + ' To get rid of this message, please add codec information to the manifest.';
  13712. }
  13713. return null;
  13714. };
  13715. /**
  13716. * Calculates a time value that is safe to remove from the back buffer without interrupting
  13717. * playback.
  13718. *
  13719. * @param {TimeRange} seekable
  13720. * The current seekable range
  13721. * @param {number} currentTime
  13722. * The current time of the player
  13723. * @param {number} targetDuration
  13724. * The target duration of the current playlist
  13725. * @return {number}
  13726. * Time that is safe to remove from the back buffer without interrupting playback
  13727. */
  13728. const safeBackBufferTrimTime = (seekable, currentTime, targetDuration) => {
  13729. // 30 seconds before the playhead provides a safe default for trimming.
  13730. //
  13731. // Choosing a reasonable default is particularly important for high bitrate content and
  13732. // VOD videos/live streams with large windows, as the buffer may end up overfilled and
  13733. // throw an APPEND_BUFFER_ERR.
  13734. let trimTime = currentTime - Config.BACK_BUFFER_LENGTH;
  13735. if (seekable.length) {
  13736. // Some live playlists may have a shorter window of content than the full allowed back
  13737. // buffer. For these playlists, don't save content that's no longer within the window.
  13738. trimTime = Math.max(trimTime, seekable.start(0));
  13739. } // Don't remove within target duration of the current time to avoid the possibility of
  13740. // removing the GOP currently being played, as removing it can cause playback stalls.
  13741. const maxTrimTime = currentTime - targetDuration;
  13742. return Math.min(maxTrimTime, trimTime);
  13743. };
  13744. const segmentInfoString = segmentInfo => {
  13745. const {
  13746. startOfSegment,
  13747. duration,
  13748. segment,
  13749. part,
  13750. playlist: {
  13751. mediaSequence: seq,
  13752. id,
  13753. segments = []
  13754. },
  13755. mediaIndex: index,
  13756. partIndex,
  13757. timeline
  13758. } = segmentInfo;
  13759. const segmentLen = segments.length - 1;
  13760. let selection = 'mediaIndex/partIndex increment';
  13761. if (segmentInfo.getMediaInfoForTime) {
  13762. selection = `getMediaInfoForTime (${segmentInfo.getMediaInfoForTime})`;
  13763. } else if (segmentInfo.isSyncRequest) {
  13764. selection = 'getSyncSegmentCandidate (isSyncRequest)';
  13765. }
  13766. if (segmentInfo.independent) {
  13767. selection += ` with independent ${segmentInfo.independent}`;
  13768. }
  13769. const hasPartIndex = typeof partIndex === 'number';
  13770. const name = segmentInfo.segment.uri ? 'segment' : 'pre-segment';
  13771. const zeroBasedPartCount = hasPartIndex ? getKnownPartCount({
  13772. preloadSegment: segment
  13773. }) - 1 : 0;
  13774. return `${name} [${seq + index}/${seq + segmentLen}]` + (hasPartIndex ? ` part [${partIndex}/${zeroBasedPartCount}]` : '') + ` segment start/end [${segment.start} => ${segment.end}]` + (hasPartIndex ? ` part start/end [${part.start} => ${part.end}]` : '') + ` startOfSegment [${startOfSegment}]` + ` duration [${duration}]` + ` timeline [${timeline}]` + ` selected by [${selection}]` + ` playlist [${id}]`;
  13775. };
  13776. const timingInfoPropertyForMedia = mediaType => `${mediaType}TimingInfo`;
  13777. /**
  13778. * Returns the timestamp offset to use for the segment.
  13779. *
  13780. * @param {number} segmentTimeline
  13781. * The timeline of the segment
  13782. * @param {number} currentTimeline
  13783. * The timeline currently being followed by the loader
  13784. * @param {number} startOfSegment
  13785. * The estimated segment start
  13786. * @param {TimeRange[]} buffered
  13787. * The loader's buffer
  13788. * @param {boolean} overrideCheck
  13789. * If true, no checks are made to see if the timestamp offset value should be set,
  13790. * but sets it directly to a value.
  13791. *
  13792. * @return {number|null}
  13793. * Either a number representing a new timestamp offset, or null if the segment is
  13794. * part of the same timeline
  13795. */
  13796. const timestampOffsetForSegment = ({
  13797. segmentTimeline,
  13798. currentTimeline,
  13799. startOfSegment,
  13800. buffered,
  13801. overrideCheck
  13802. }) => {
  13803. // Check to see if we are crossing a discontinuity to see if we need to set the
  13804. // timestamp offset on the transmuxer and source buffer.
  13805. //
  13806. // Previously, we changed the timestampOffset if the start of this segment was less than
  13807. // the currently set timestampOffset, but this isn't desirable as it can produce bad
  13808. // behavior, especially around long running live streams.
  13809. if (!overrideCheck && segmentTimeline === currentTimeline) {
  13810. return null;
  13811. } // When changing renditions, it's possible to request a segment on an older timeline. For
  13812. // instance, given two renditions with the following:
  13813. //
  13814. // #EXTINF:10
  13815. // segment1
  13816. // #EXT-X-DISCONTINUITY
  13817. // #EXTINF:10
  13818. // segment2
  13819. // #EXTINF:10
  13820. // segment3
  13821. //
  13822. // And the current player state:
  13823. //
  13824. // current time: 8
  13825. // buffer: 0 => 20
  13826. //
  13827. // The next segment on the current rendition would be segment3, filling the buffer from
  13828. // 20s onwards. However, if a rendition switch happens after segment2 was requested,
  13829. // then the next segment to be requested will be segment1 from the new rendition in
  13830. // order to fill time 8 and onwards. Using the buffered end would result in repeated
  13831. // content (since it would position segment1 of the new rendition starting at 20s). This
  13832. // case can be identified when the new segment's timeline is a prior value. Instead of
  13833. // using the buffered end, the startOfSegment can be used, which, hopefully, will be
  13834. // more accurate to the actual start time of the segment.
  13835. if (segmentTimeline < currentTimeline) {
  13836. return startOfSegment;
  13837. } // segmentInfo.startOfSegment used to be used as the timestamp offset, however, that
  13838. // value uses the end of the last segment if it is available. While this value
  13839. // should often be correct, it's better to rely on the buffered end, as the new
  13840. // content post discontinuity should line up with the buffered end as if it were
  13841. // time 0 for the new content.
  13842. return buffered.length ? buffered.end(buffered.length - 1) : startOfSegment;
  13843. };
  13844. /**
  13845. * Returns whether or not the loader should wait for a timeline change from the timeline
  13846. * change controller before processing the segment.
  13847. *
  13848. * Primary timing in VHS goes by video. This is different from most media players, as
  13849. * audio is more often used as the primary timing source. For the foreseeable future, VHS
  13850. * will continue to use video as the primary timing source, due to the current logic and
  13851. * expectations built around it.
  13852. * Since the timing follows video, in order to maintain sync, the video loader is
  13853. * responsible for setting both audio and video source buffer timestamp offsets.
  13854. *
  13855. * Setting different values for audio and video source buffers could lead to
  13856. * desyncing. The following examples demonstrate some of the situations where this
  13857. * distinction is important. Note that all of these cases involve demuxed content. When
  13858. * content is muxed, the audio and video are packaged together, therefore syncing
  13859. * separate media playlists is not an issue.
  13860. *
  13861. * CASE 1: Audio prepares to load a new timeline before video:
  13862. *
  13863. * Timeline: 0 1
  13864. * Audio Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13865. * Audio Loader: ^
  13866. * Video Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13867. * Video Loader ^
  13868. *
  13869. * In the above example, the audio loader is preparing to load the 6th segment, the first
  13870. * after a discontinuity, while the video loader is still loading the 5th segment, before
  13871. * the discontinuity.
  13872. *
  13873. * If the audio loader goes ahead and loads and appends the 6th segment before the video
  13874. * loader crosses the discontinuity, then when appended, the 6th audio segment will use
  13875. * the timestamp offset from timeline 0. This will likely lead to desyncing. In addition,
  13876. * the audio loader must provide the audioAppendStart value to trim the content in the
  13877. * transmuxer, and that value relies on the audio timestamp offset. Since the audio
  13878. * timestamp offset is set by the video (main) loader, the audio loader shouldn't load the
  13879. * segment until that value is provided.
  13880. *
  13881. * CASE 2: Video prepares to load a new timeline before audio:
  13882. *
  13883. * Timeline: 0 1
  13884. * Audio Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13885. * Audio Loader: ^
  13886. * Video Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13887. * Video Loader ^
  13888. *
  13889. * In the above example, the video loader is preparing to load the 6th segment, the first
  13890. * after a discontinuity, while the audio loader is still loading the 5th segment, before
  13891. * the discontinuity.
  13892. *
  13893. * If the video loader goes ahead and loads and appends the 6th segment, then once the
  13894. * segment is loaded and processed, both the video and audio timestamp offsets will be
  13895. * set, since video is used as the primary timing source. This is to ensure content lines
  13896. * up appropriately, as any modifications to the video timing are reflected by audio when
  13897. * the video loader sets the audio and video timestamp offsets to the same value. However,
  13898. * setting the timestamp offset for audio before audio has had a chance to change
  13899. * timelines will likely lead to desyncing, as the audio loader will append segment 5 with
  13900. * a timestamp intended to apply to segments from timeline 1 rather than timeline 0.
  13901. *
  13902. * CASE 3: When seeking, audio prepares to load a new timeline before video
  13903. *
  13904. * Timeline: 0 1
  13905. * Audio Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13906. * Audio Loader: ^
  13907. * Video Segments: 0 1 2 3 4 5 DISCO 6 7 8 9
  13908. * Video Loader ^
  13909. *
  13910. * In the above example, both audio and video loaders are loading segments from timeline
  13911. * 0, but imagine that the seek originated from timeline 1.
  13912. *
  13913. * When seeking to a new timeline, the timestamp offset will be set based on the expected
  13914. * segment start of the loaded video segment. In order to maintain sync, the audio loader
  13915. * must wait for the video loader to load its segment and update both the audio and video
  13916. * timestamp offsets before it may load and append its own segment. This is the case
  13917. * whether the seek results in a mismatched segment request (e.g., the audio loader
  13918. * chooses to load segment 3 and the video loader chooses to load segment 4) or the
  13919. * loaders choose to load the same segment index from each playlist, as the segments may
  13920. * not be aligned perfectly, even for matching segment indexes.
  13921. *
  13922. * @param {Object} timelinechangeController
  13923. * @param {number} currentTimeline
  13924. * The timeline currently being followed by the loader
  13925. * @param {number} segmentTimeline
  13926. * The timeline of the segment being loaded
  13927. * @param {('main'|'audio')} loaderType
  13928. * The loader type
  13929. * @param {boolean} audioDisabled
  13930. * Whether the audio is disabled for the loader. This should only be true when the
  13931. * loader may have muxed audio in its segment, but should not append it, e.g., for
  13932. * the main loader when an alternate audio playlist is active.
  13933. *
  13934. * @return {boolean}
  13935. * Whether the loader should wait for a timeline change from the timeline change
  13936. * controller before processing the segment
  13937. */
  13938. const shouldWaitForTimelineChange = ({
  13939. timelineChangeController,
  13940. currentTimeline,
  13941. segmentTimeline,
  13942. loaderType,
  13943. audioDisabled
  13944. }) => {
  13945. if (currentTimeline === segmentTimeline) {
  13946. return false;
  13947. }
  13948. if (loaderType === 'audio') {
  13949. const lastMainTimelineChange = timelineChangeController.lastTimelineChange({
  13950. type: 'main'
  13951. }); // Audio loader should wait if:
  13952. //
  13953. // * main hasn't had a timeline change yet (thus has not loaded its first segment)
  13954. // * main hasn't yet changed to the timeline audio is looking to load
  13955. return !lastMainTimelineChange || lastMainTimelineChange.to !== segmentTimeline;
  13956. } // The main loader only needs to wait for timeline changes if there's demuxed audio.
  13957. // Otherwise, there's nothing to wait for, since audio would be muxed into the main
  13958. // loader's segments (or the content is audio/video only and handled by the main
  13959. // loader).
  13960. if (loaderType === 'main' && audioDisabled) {
  13961. const pendingAudioTimelineChange = timelineChangeController.pendingTimelineChange({
  13962. type: 'audio'
  13963. }); // Main loader should wait for the audio loader if audio is not pending a timeline
  13964. // change to the current timeline.
  13965. //
  13966. // Since the main loader is responsible for setting the timestamp offset for both
  13967. // audio and video, the main loader must wait for audio to be about to change to its
  13968. // timeline before setting the offset, otherwise, if audio is behind in loading,
  13969. // segments from the previous timeline would be adjusted by the new timestamp offset.
  13970. //
  13971. // This requirement means that video will not cross a timeline until the audio is
  13972. // about to cross to it, so that way audio and video will always cross the timeline
  13973. // together.
  13974. //
  13975. // In addition to normal timeline changes, these rules also apply to the start of a
  13976. // stream (going from a non-existent timeline, -1, to timeline 0). It's important
  13977. // that these rules apply to the first timeline change because if they did not, it's
  13978. // possible that the main loader will cross two timelines before the audio loader has
  13979. // crossed one. Logic may be implemented to handle the startup as a special case, but
  13980. // it's easier to simply treat all timeline changes the same.
  13981. if (pendingAudioTimelineChange && pendingAudioTimelineChange.to === segmentTimeline) {
  13982. return false;
  13983. }
  13984. return true;
  13985. }
  13986. return false;
  13987. };
  13988. const mediaDuration = timingInfos => {
  13989. let maxDuration = 0;
  13990. ['video', 'audio'].forEach(function (type) {
  13991. const typeTimingInfo = timingInfos[`${type}TimingInfo`];
  13992. if (!typeTimingInfo) {
  13993. return;
  13994. }
  13995. const {
  13996. start,
  13997. end
  13998. } = typeTimingInfo;
  13999. let duration;
  14000. if (typeof start === 'bigint' || typeof end === 'bigint') {
  14001. duration = window__default["default"].BigInt(end) - window__default["default"].BigInt(start);
  14002. } else if (typeof start === 'number' && typeof end === 'number') {
  14003. duration = end - start;
  14004. }
  14005. if (typeof duration !== 'undefined' && duration > maxDuration) {
  14006. maxDuration = duration;
  14007. }
  14008. }); // convert back to a number if it is lower than MAX_SAFE_INTEGER
  14009. // as we only need BigInt when we are above that.
  14010. if (typeof maxDuration === 'bigint' && maxDuration < Number.MAX_SAFE_INTEGER) {
  14011. maxDuration = Number(maxDuration);
  14012. }
  14013. return maxDuration;
  14014. };
  14015. const segmentTooLong = ({
  14016. segmentDuration,
  14017. maxDuration
  14018. }) => {
  14019. // 0 duration segments are most likely due to metadata only segments or a lack of
  14020. // information.
  14021. if (!segmentDuration) {
  14022. return false;
  14023. } // For HLS:
  14024. //
  14025. // https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.3.1
  14026. // The EXTINF duration of each Media Segment in the Playlist
  14027. // file, when rounded to the nearest integer, MUST be less than or equal
  14028. // to the target duration; longer segments can trigger playback stalls
  14029. // or other errors.
  14030. //
  14031. // For DASH, the mpd-parser uses the largest reported segment duration as the target
  14032. // duration. Although that reported duration is occasionally approximate (i.e., not
  14033. // exact), a strict check may report that a segment is too long more often in DASH.
  14034. return Math.round(segmentDuration) > maxDuration + TIME_FUDGE_FACTOR;
  14035. };
  14036. const getTroublesomeSegmentDurationMessage = (segmentInfo, sourceType) => {
  14037. // Right now we aren't following DASH's timing model exactly, so only perform
  14038. // this check for HLS content.
  14039. if (sourceType !== 'hls') {
  14040. return null;
  14041. }
  14042. const segmentDuration = mediaDuration({
  14043. audioTimingInfo: segmentInfo.audioTimingInfo,
  14044. videoTimingInfo: segmentInfo.videoTimingInfo
  14045. }); // Don't report if we lack information.
  14046. //
  14047. // If the segment has a duration of 0 it is either a lack of information or a
  14048. // metadata only segment and shouldn't be reported here.
  14049. if (!segmentDuration) {
  14050. return null;
  14051. }
  14052. const targetDuration = segmentInfo.playlist.targetDuration;
  14053. const isSegmentWayTooLong = segmentTooLong({
  14054. segmentDuration,
  14055. maxDuration: targetDuration * 2
  14056. });
  14057. const isSegmentSlightlyTooLong = segmentTooLong({
  14058. segmentDuration,
  14059. maxDuration: targetDuration
  14060. });
  14061. const segmentTooLongMessage = `Segment with index ${segmentInfo.mediaIndex} ` + `from playlist ${segmentInfo.playlist.id} ` + `has a duration of ${segmentDuration} ` + `when the reported duration is ${segmentInfo.duration} ` + `and the target duration is ${targetDuration}. ` + 'For HLS content, a duration in excess of the target duration may result in ' + 'playback issues. See the HLS specification section on EXT-X-TARGETDURATION for ' + 'more details: ' + 'https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.3.1';
  14062. if (isSegmentWayTooLong || isSegmentSlightlyTooLong) {
  14063. return {
  14064. severity: isSegmentWayTooLong ? 'warn' : 'info',
  14065. message: segmentTooLongMessage
  14066. };
  14067. }
  14068. return null;
  14069. };
  14070. /**
  14071. * An object that manages segment loading and appending.
  14072. *
  14073. * @class SegmentLoader
  14074. * @param {Object} options required and optional options
  14075. * @extends videojs.EventTarget
  14076. */
  14077. class SegmentLoader extends videojs__default["default"].EventTarget {
  14078. constructor(settings, options = {}) {
  14079. super(); // check pre-conditions
  14080. if (!settings) {
  14081. throw new TypeError('Initialization settings are required');
  14082. }
  14083. if (typeof settings.currentTime !== 'function') {
  14084. throw new TypeError('No currentTime getter specified');
  14085. }
  14086. if (!settings.mediaSource) {
  14087. throw new TypeError('No MediaSource specified');
  14088. } // public properties
  14089. this.bandwidth = settings.bandwidth;
  14090. this.throughput = {
  14091. rate: 0,
  14092. count: 0
  14093. };
  14094. this.roundTrip = NaN;
  14095. this.resetStats_();
  14096. this.mediaIndex = null;
  14097. this.partIndex = null; // private settings
  14098. this.hasPlayed_ = settings.hasPlayed;
  14099. this.currentTime_ = settings.currentTime;
  14100. this.seekable_ = settings.seekable;
  14101. this.seeking_ = settings.seeking;
  14102. this.duration_ = settings.duration;
  14103. this.mediaSource_ = settings.mediaSource;
  14104. this.vhs_ = settings.vhs;
  14105. this.loaderType_ = settings.loaderType;
  14106. this.currentMediaInfo_ = void 0;
  14107. this.startingMediaInfo_ = void 0;
  14108. this.segmentMetadataTrack_ = settings.segmentMetadataTrack;
  14109. this.goalBufferLength_ = settings.goalBufferLength;
  14110. this.sourceType_ = settings.sourceType;
  14111. this.sourceUpdater_ = settings.sourceUpdater;
  14112. this.inbandTextTracks_ = settings.inbandTextTracks;
  14113. this.state_ = 'INIT';
  14114. this.timelineChangeController_ = settings.timelineChangeController;
  14115. this.shouldSaveSegmentTimingInfo_ = true;
  14116. this.parse708captions_ = settings.parse708captions;
  14117. this.useDtsForTimestampOffset_ = settings.useDtsForTimestampOffset;
  14118. this.captionServices_ = settings.captionServices;
  14119. this.exactManifestTimings = settings.exactManifestTimings;
  14120. this.addMetadataToTextTrack = settings.addMetadataToTextTrack; // private instance variables
  14121. this.checkBufferTimeout_ = null;
  14122. this.error_ = void 0;
  14123. this.currentTimeline_ = -1;
  14124. this.shouldForceTimestampOffsetAfterResync_ = false;
  14125. this.pendingSegment_ = null;
  14126. this.xhrOptions_ = null;
  14127. this.pendingSegments_ = [];
  14128. this.audioDisabled_ = false;
  14129. this.isPendingTimestampOffset_ = false; // TODO possibly move gopBuffer and timeMapping info to a separate controller
  14130. this.gopBuffer_ = [];
  14131. this.timeMapping_ = 0;
  14132. this.safeAppend_ = false;
  14133. this.appendInitSegment_ = {
  14134. audio: true,
  14135. video: true
  14136. };
  14137. this.playlistOfLastInitSegment_ = {
  14138. audio: null,
  14139. video: null
  14140. };
  14141. this.callQueue_ = []; // If the segment loader prepares to load a segment, but does not have enough
  14142. // information yet to start the loading process (e.g., if the audio loader wants to
  14143. // load a segment from the next timeline but the main loader hasn't yet crossed that
  14144. // timeline), then the load call will be added to the queue until it is ready to be
  14145. // processed.
  14146. this.loadQueue_ = [];
  14147. this.metadataQueue_ = {
  14148. id3: [],
  14149. caption: []
  14150. };
  14151. this.waitingOnRemove_ = false;
  14152. this.quotaExceededErrorRetryTimeout_ = null; // Fragmented mp4 playback
  14153. this.activeInitSegmentId_ = null;
  14154. this.initSegments_ = {}; // HLSe playback
  14155. this.cacheEncryptionKeys_ = settings.cacheEncryptionKeys;
  14156. this.keyCache_ = {};
  14157. this.decrypter_ = settings.decrypter; // Manages the tracking and generation of sync-points, mappings
  14158. // between a time in the display time and a segment index within
  14159. // a playlist
  14160. this.syncController_ = settings.syncController;
  14161. this.syncPoint_ = {
  14162. segmentIndex: 0,
  14163. time: 0
  14164. };
  14165. this.transmuxer_ = this.createTransmuxer_();
  14166. this.triggerSyncInfoUpdate_ = () => this.trigger('syncinfoupdate');
  14167. this.syncController_.on('syncinfoupdate', this.triggerSyncInfoUpdate_);
  14168. this.mediaSource_.addEventListener('sourceopen', () => {
  14169. if (!this.isEndOfStream_()) {
  14170. this.ended_ = false;
  14171. }
  14172. }); // ...for determining the fetch location
  14173. this.fetchAtBuffer_ = false;
  14174. this.logger_ = logger(`SegmentLoader[${this.loaderType_}]`);
  14175. Object.defineProperty(this, 'state', {
  14176. get() {
  14177. return this.state_;
  14178. },
  14179. set(newState) {
  14180. if (newState !== this.state_) {
  14181. this.logger_(`${this.state_} -> ${newState}`);
  14182. this.state_ = newState;
  14183. this.trigger('statechange');
  14184. }
  14185. }
  14186. });
  14187. this.sourceUpdater_.on('ready', () => {
  14188. if (this.hasEnoughInfoToAppend_()) {
  14189. this.processCallQueue_();
  14190. }
  14191. }); // Only the main loader needs to listen for pending timeline changes, as the main
  14192. // loader should wait for audio to be ready to change its timeline so that both main
  14193. // and audio timelines change together. For more details, see the
  14194. // shouldWaitForTimelineChange function.
  14195. if (this.loaderType_ === 'main') {
  14196. this.timelineChangeController_.on('pendingtimelinechange', () => {
  14197. if (this.hasEnoughInfoToAppend_()) {
  14198. this.processCallQueue_();
  14199. }
  14200. });
  14201. } // The main loader only listens on pending timeline changes, but the audio loader,
  14202. // since its loads follow main, needs to listen on timeline changes. For more details,
  14203. // see the shouldWaitForTimelineChange function.
  14204. if (this.loaderType_ === 'audio') {
  14205. this.timelineChangeController_.on('timelinechange', () => {
  14206. if (this.hasEnoughInfoToLoad_()) {
  14207. this.processLoadQueue_();
  14208. }
  14209. if (this.hasEnoughInfoToAppend_()) {
  14210. this.processCallQueue_();
  14211. }
  14212. });
  14213. }
  14214. }
  14215. createTransmuxer_() {
  14216. return segmentTransmuxer.createTransmuxer({
  14217. remux: false,
  14218. alignGopsAtEnd: this.safeAppend_,
  14219. keepOriginalTimestamps: true,
  14220. parse708captions: this.parse708captions_,
  14221. captionServices: this.captionServices_
  14222. });
  14223. }
  14224. /**
  14225. * reset all of our media stats
  14226. *
  14227. * @private
  14228. */
  14229. resetStats_() {
  14230. this.mediaBytesTransferred = 0;
  14231. this.mediaRequests = 0;
  14232. this.mediaRequestsAborted = 0;
  14233. this.mediaRequestsTimedout = 0;
  14234. this.mediaRequestsErrored = 0;
  14235. this.mediaTransferDuration = 0;
  14236. this.mediaSecondsLoaded = 0;
  14237. this.mediaAppends = 0;
  14238. }
  14239. /**
  14240. * dispose of the SegmentLoader and reset to the default state
  14241. */
  14242. dispose() {
  14243. this.trigger('dispose');
  14244. this.state = 'DISPOSED';
  14245. this.pause();
  14246. this.abort_();
  14247. if (this.transmuxer_) {
  14248. this.transmuxer_.terminate();
  14249. }
  14250. this.resetStats_();
  14251. if (this.checkBufferTimeout_) {
  14252. window__default["default"].clearTimeout(this.checkBufferTimeout_);
  14253. }
  14254. if (this.syncController_ && this.triggerSyncInfoUpdate_) {
  14255. this.syncController_.off('syncinfoupdate', this.triggerSyncInfoUpdate_);
  14256. }
  14257. this.off();
  14258. }
  14259. setAudio(enable) {
  14260. this.audioDisabled_ = !enable;
  14261. if (enable) {
  14262. this.appendInitSegment_.audio = true;
  14263. } else {
  14264. // remove current track audio if it gets disabled
  14265. this.sourceUpdater_.removeAudio(0, this.duration_());
  14266. }
  14267. }
  14268. /**
  14269. * abort anything that is currently doing on with the SegmentLoader
  14270. * and reset to a default state
  14271. */
  14272. abort() {
  14273. if (this.state !== 'WAITING') {
  14274. if (this.pendingSegment_) {
  14275. this.pendingSegment_ = null;
  14276. }
  14277. return;
  14278. }
  14279. this.abort_(); // We aborted the requests we were waiting on, so reset the loader's state to READY
  14280. // since we are no longer "waiting" on any requests. XHR callback is not always run
  14281. // when the request is aborted. This will prevent the loader from being stuck in the
  14282. // WAITING state indefinitely.
  14283. this.state = 'READY'; // don't wait for buffer check timeouts to begin fetching the
  14284. // next segment
  14285. if (!this.paused()) {
  14286. this.monitorBuffer_();
  14287. }
  14288. }
  14289. /**
  14290. * abort all pending xhr requests and null any pending segements
  14291. *
  14292. * @private
  14293. */
  14294. abort_() {
  14295. if (this.pendingSegment_ && this.pendingSegment_.abortRequests) {
  14296. this.pendingSegment_.abortRequests();
  14297. } // clear out the segment being processed
  14298. this.pendingSegment_ = null;
  14299. this.callQueue_ = [];
  14300. this.loadQueue_ = [];
  14301. this.metadataQueue_.id3 = [];
  14302. this.metadataQueue_.caption = [];
  14303. this.timelineChangeController_.clearPendingTimelineChange(this.loaderType_);
  14304. this.waitingOnRemove_ = false;
  14305. window__default["default"].clearTimeout(this.quotaExceededErrorRetryTimeout_);
  14306. this.quotaExceededErrorRetryTimeout_ = null;
  14307. }
  14308. checkForAbort_(requestId) {
  14309. // If the state is APPENDING, then aborts will not modify the state, meaning the first
  14310. // callback that happens should reset the state to READY so that loading can continue.
  14311. if (this.state === 'APPENDING' && !this.pendingSegment_) {
  14312. this.state = 'READY';
  14313. return true;
  14314. }
  14315. if (!this.pendingSegment_ || this.pendingSegment_.requestId !== requestId) {
  14316. return true;
  14317. }
  14318. return false;
  14319. }
  14320. /**
  14321. * set an error on the segment loader and null out any pending segements
  14322. *
  14323. * @param {Error} error the error to set on the SegmentLoader
  14324. * @return {Error} the error that was set or that is currently set
  14325. */
  14326. error(error) {
  14327. if (typeof error !== 'undefined') {
  14328. this.logger_('error occurred:', error);
  14329. this.error_ = error;
  14330. }
  14331. this.pendingSegment_ = null;
  14332. return this.error_;
  14333. }
  14334. endOfStream() {
  14335. this.ended_ = true;
  14336. if (this.transmuxer_) {
  14337. // need to clear out any cached data to prepare for the new segment
  14338. segmentTransmuxer.reset(this.transmuxer_);
  14339. }
  14340. this.gopBuffer_.length = 0;
  14341. this.pause();
  14342. this.trigger('ended');
  14343. }
  14344. /**
  14345. * Indicates which time ranges are buffered
  14346. *
  14347. * @return {TimeRange}
  14348. * TimeRange object representing the current buffered ranges
  14349. */
  14350. buffered_() {
  14351. const trackInfo = this.getMediaInfo_();
  14352. if (!this.sourceUpdater_ || !trackInfo) {
  14353. return createTimeRanges();
  14354. }
  14355. if (this.loaderType_ === 'main') {
  14356. const {
  14357. hasAudio,
  14358. hasVideo,
  14359. isMuxed
  14360. } = trackInfo;
  14361. if (hasVideo && hasAudio && !this.audioDisabled_ && !isMuxed) {
  14362. return this.sourceUpdater_.buffered();
  14363. }
  14364. if (hasVideo) {
  14365. return this.sourceUpdater_.videoBuffered();
  14366. }
  14367. } // One case that can be ignored for now is audio only with alt audio,
  14368. // as we don't yet have proper support for that.
  14369. return this.sourceUpdater_.audioBuffered();
  14370. }
  14371. /**
  14372. * Gets and sets init segment for the provided map
  14373. *
  14374. * @param {Object} map
  14375. * The map object representing the init segment to get or set
  14376. * @param {boolean=} set
  14377. * If true, the init segment for the provided map should be saved
  14378. * @return {Object}
  14379. * map object for desired init segment
  14380. */
  14381. initSegmentForMap(map, set = false) {
  14382. if (!map) {
  14383. return null;
  14384. }
  14385. const id = initSegmentId(map);
  14386. let storedMap = this.initSegments_[id];
  14387. if (set && !storedMap && map.bytes) {
  14388. this.initSegments_[id] = storedMap = {
  14389. resolvedUri: map.resolvedUri,
  14390. byterange: map.byterange,
  14391. bytes: map.bytes,
  14392. tracks: map.tracks,
  14393. timescales: map.timescales
  14394. };
  14395. }
  14396. return storedMap || map;
  14397. }
  14398. /**
  14399. * Gets and sets key for the provided key
  14400. *
  14401. * @param {Object} key
  14402. * The key object representing the key to get or set
  14403. * @param {boolean=} set
  14404. * If true, the key for the provided key should be saved
  14405. * @return {Object}
  14406. * Key object for desired key
  14407. */
  14408. segmentKey(key, set = false) {
  14409. if (!key) {
  14410. return null;
  14411. }
  14412. const id = segmentKeyId(key);
  14413. let storedKey = this.keyCache_[id]; // TODO: We should use the HTTP Expires header to invalidate our cache per
  14414. // https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-6.2.3
  14415. if (this.cacheEncryptionKeys_ && set && !storedKey && key.bytes) {
  14416. this.keyCache_[id] = storedKey = {
  14417. resolvedUri: key.resolvedUri,
  14418. bytes: key.bytes
  14419. };
  14420. }
  14421. const result = {
  14422. resolvedUri: (storedKey || key).resolvedUri
  14423. };
  14424. if (storedKey) {
  14425. result.bytes = storedKey.bytes;
  14426. }
  14427. return result;
  14428. }
  14429. /**
  14430. * Returns true if all configuration required for loading is present, otherwise false.
  14431. *
  14432. * @return {boolean} True if the all configuration is ready for loading
  14433. * @private
  14434. */
  14435. couldBeginLoading_() {
  14436. return this.playlist_ && !this.paused();
  14437. }
  14438. /**
  14439. * load a playlist and start to fill the buffer
  14440. */
  14441. load() {
  14442. // un-pause
  14443. this.monitorBuffer_(); // if we don't have a playlist yet, keep waiting for one to be
  14444. // specified
  14445. if (!this.playlist_) {
  14446. return;
  14447. } // if all the configuration is ready, initialize and begin loading
  14448. if (this.state === 'INIT' && this.couldBeginLoading_()) {
  14449. return this.init_();
  14450. } // if we're in the middle of processing a segment already, don't
  14451. // kick off an additional segment request
  14452. if (!this.couldBeginLoading_() || this.state !== 'READY' && this.state !== 'INIT') {
  14453. return;
  14454. }
  14455. this.state = 'READY';
  14456. }
  14457. /**
  14458. * Once all the starting parameters have been specified, begin
  14459. * operation. This method should only be invoked from the INIT
  14460. * state.
  14461. *
  14462. * @private
  14463. */
  14464. init_() {
  14465. this.state = 'READY'; // if this is the audio segment loader, and it hasn't been inited before, then any old
  14466. // audio data from the muxed content should be removed
  14467. this.resetEverything();
  14468. return this.monitorBuffer_();
  14469. }
  14470. /**
  14471. * set a playlist on the segment loader
  14472. *
  14473. * @param {PlaylistLoader} media the playlist to set on the segment loader
  14474. */
  14475. playlist(newPlaylist, options = {}) {
  14476. if (!newPlaylist) {
  14477. return;
  14478. }
  14479. const oldPlaylist = this.playlist_;
  14480. const segmentInfo = this.pendingSegment_;
  14481. this.playlist_ = newPlaylist;
  14482. this.xhrOptions_ = options; // when we haven't started playing yet, the start of a live playlist
  14483. // is always our zero-time so force a sync update each time the playlist
  14484. // is refreshed from the server
  14485. //
  14486. // Use the INIT state to determine if playback has started, as the playlist sync info
  14487. // should be fixed once requests begin (as sync points are generated based on sync
  14488. // info), but not before then.
  14489. if (this.state === 'INIT') {
  14490. newPlaylist.syncInfo = {
  14491. mediaSequence: newPlaylist.mediaSequence,
  14492. time: 0
  14493. }; // Setting the date time mapping means mapping the program date time (if available)
  14494. // to time 0 on the player's timeline. The playlist's syncInfo serves a similar
  14495. // purpose, mapping the initial mediaSequence to time zero. Since the syncInfo can
  14496. // be updated as the playlist is refreshed before the loader starts loading, the
  14497. // program date time mapping needs to be updated as well.
  14498. //
  14499. // This mapping is only done for the main loader because a program date time should
  14500. // map equivalently between playlists.
  14501. if (this.loaderType_ === 'main') {
  14502. this.syncController_.setDateTimeMappingForStart(newPlaylist);
  14503. }
  14504. }
  14505. let oldId = null;
  14506. if (oldPlaylist) {
  14507. if (oldPlaylist.id) {
  14508. oldId = oldPlaylist.id;
  14509. } else if (oldPlaylist.uri) {
  14510. oldId = oldPlaylist.uri;
  14511. }
  14512. }
  14513. this.logger_(`playlist update [${oldId} => ${newPlaylist.id || newPlaylist.uri}]`);
  14514. this.syncController_.updateMediaSequenceMap(newPlaylist, this.currentTime_(), this.loaderType_); // in VOD, this is always a rendition switch (or we updated our syncInfo above)
  14515. // in LIVE, we always want to update with new playlists (including refreshes)
  14516. this.trigger('syncinfoupdate'); // if we were unpaused but waiting for a playlist, start
  14517. // buffering now
  14518. if (this.state === 'INIT' && this.couldBeginLoading_()) {
  14519. return this.init_();
  14520. }
  14521. if (!oldPlaylist || oldPlaylist.uri !== newPlaylist.uri) {
  14522. if (this.mediaIndex !== null) {
  14523. // we must reset/resync the segment loader when we switch renditions and
  14524. // the segment loader is already synced to the previous rendition
  14525. // We only want to reset the loader here for LLHLS playback, as resetLoader sets fetchAtBuffer_
  14526. // to false, resulting in fetching segments at currentTime and causing repeated
  14527. // same-segment requests on playlist change. This erroneously drives up the playback watcher
  14528. // stalled segment count, as re-requesting segments at the currentTime or browser cached segments
  14529. // will not change the buffer.
  14530. // Reference for LLHLS fixes: https://github.com/videojs/http-streaming/pull/1201
  14531. const isLLHLS = !newPlaylist.endList && typeof newPlaylist.partTargetDuration === 'number';
  14532. if (isLLHLS) {
  14533. this.resetLoader();
  14534. } else {
  14535. this.resyncLoader();
  14536. }
  14537. }
  14538. this.currentMediaInfo_ = void 0;
  14539. this.trigger('playlistupdate'); // the rest of this function depends on `oldPlaylist` being defined
  14540. return;
  14541. } // we reloaded the same playlist so we are in a live scenario
  14542. // and we will likely need to adjust the mediaIndex
  14543. const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
  14544. this.logger_(`live window shift [${mediaSequenceDiff}]`); // update the mediaIndex on the SegmentLoader
  14545. // this is important because we can abort a request and this value must be
  14546. // equal to the last appended mediaIndex
  14547. if (this.mediaIndex !== null) {
  14548. this.mediaIndex -= mediaSequenceDiff; // this can happen if we are going to load the first segment, but get a playlist
  14549. // update during that. mediaIndex would go from 0 to -1 if mediaSequence in the
  14550. // new playlist was incremented by 1.
  14551. if (this.mediaIndex < 0) {
  14552. this.mediaIndex = null;
  14553. this.partIndex = null;
  14554. } else {
  14555. const segment = this.playlist_.segments[this.mediaIndex]; // partIndex should remain the same for the same segment
  14556. // unless parts fell off of the playlist for this segment.
  14557. // In that case we need to reset partIndex and resync
  14558. if (this.partIndex && (!segment.parts || !segment.parts.length || !segment.parts[this.partIndex])) {
  14559. const mediaIndex = this.mediaIndex;
  14560. this.logger_(`currently processing part (index ${this.partIndex}) no longer exists.`);
  14561. this.resetLoader(); // We want to throw away the partIndex and the data associated with it,
  14562. // as the part was dropped from our current playlists segment.
  14563. // The mediaIndex will still be valid so keep that around.
  14564. this.mediaIndex = mediaIndex;
  14565. }
  14566. }
  14567. } // update the mediaIndex on the SegmentInfo object
  14568. // this is important because we will update this.mediaIndex with this value
  14569. // in `handleAppendsDone_` after the segment has been successfully appended
  14570. if (segmentInfo) {
  14571. segmentInfo.mediaIndex -= mediaSequenceDiff;
  14572. if (segmentInfo.mediaIndex < 0) {
  14573. segmentInfo.mediaIndex = null;
  14574. segmentInfo.partIndex = null;
  14575. } else {
  14576. // we need to update the referenced segment so that timing information is
  14577. // saved for the new playlist's segment, however, if the segment fell off the
  14578. // playlist, we can leave the old reference and just lose the timing info
  14579. if (segmentInfo.mediaIndex >= 0) {
  14580. segmentInfo.segment = newPlaylist.segments[segmentInfo.mediaIndex];
  14581. }
  14582. if (segmentInfo.partIndex >= 0 && segmentInfo.segment.parts) {
  14583. segmentInfo.part = segmentInfo.segment.parts[segmentInfo.partIndex];
  14584. }
  14585. }
  14586. }
  14587. this.syncController_.saveExpiredSegmentInfo(oldPlaylist, newPlaylist);
  14588. }
  14589. /**
  14590. * Prevent the loader from fetching additional segments. If there
  14591. * is a segment request outstanding, it will finish processing
  14592. * before the loader halts. A segment loader can be unpaused by
  14593. * calling load().
  14594. */
  14595. pause() {
  14596. if (this.checkBufferTimeout_) {
  14597. window__default["default"].clearTimeout(this.checkBufferTimeout_);
  14598. this.checkBufferTimeout_ = null;
  14599. }
  14600. }
  14601. /**
  14602. * Returns whether the segment loader is fetching additional
  14603. * segments when given the opportunity. This property can be
  14604. * modified through calls to pause() and load().
  14605. */
  14606. paused() {
  14607. return this.checkBufferTimeout_ === null;
  14608. }
  14609. /**
  14610. * Delete all the buffered data and reset the SegmentLoader
  14611. *
  14612. * @param {Function} [done] an optional callback to be executed when the remove
  14613. * operation is complete
  14614. */
  14615. resetEverything(done) {
  14616. this.ended_ = false;
  14617. this.activeInitSegmentId_ = null;
  14618. this.appendInitSegment_ = {
  14619. audio: true,
  14620. video: true
  14621. };
  14622. this.resetLoader(); // remove from 0, the earliest point, to Infinity, to signify removal of everything.
  14623. // VTT Segment Loader doesn't need to do anything but in the regular SegmentLoader,
  14624. // we then clamp the value to duration if necessary.
  14625. this.remove(0, Infinity, done); // clears fmp4 captions
  14626. if (this.transmuxer_) {
  14627. this.transmuxer_.postMessage({
  14628. action: 'clearAllMp4Captions'
  14629. }); // reset the cache in the transmuxer
  14630. this.transmuxer_.postMessage({
  14631. action: 'reset'
  14632. });
  14633. }
  14634. }
  14635. /**
  14636. * Force the SegmentLoader to resync and start loading around the currentTime instead
  14637. * of starting at the end of the buffer
  14638. *
  14639. * Useful for fast quality changes
  14640. */
  14641. resetLoader() {
  14642. this.fetchAtBuffer_ = false;
  14643. this.resyncLoader();
  14644. }
  14645. /**
  14646. * Force the SegmentLoader to restart synchronization and make a conservative guess
  14647. * before returning to the simple walk-forward method
  14648. */
  14649. resyncLoader() {
  14650. if (this.transmuxer_) {
  14651. // need to clear out any cached data to prepare for the new segment
  14652. segmentTransmuxer.reset(this.transmuxer_);
  14653. }
  14654. this.mediaIndex = null;
  14655. this.partIndex = null;
  14656. this.syncPoint_ = null;
  14657. this.isPendingTimestampOffset_ = false;
  14658. this.shouldForceTimestampOffsetAfterResync_ = true;
  14659. this.callQueue_ = [];
  14660. this.loadQueue_ = [];
  14661. this.metadataQueue_.id3 = [];
  14662. this.metadataQueue_.caption = [];
  14663. this.abort();
  14664. if (this.transmuxer_) {
  14665. this.transmuxer_.postMessage({
  14666. action: 'clearParsedMp4Captions'
  14667. });
  14668. }
  14669. }
  14670. /**
  14671. * Remove any data in the source buffer between start and end times
  14672. *
  14673. * @param {number} start - the start time of the region to remove from the buffer
  14674. * @param {number} end - the end time of the region to remove from the buffer
  14675. * @param {Function} [done] - an optional callback to be executed when the remove
  14676. * @param {boolean} force - force all remove operations to happen
  14677. * operation is complete
  14678. */
  14679. remove(start, end, done = () => {}, force = false) {
  14680. // clamp end to duration if we need to remove everything.
  14681. // This is due to a browser bug that causes issues if we remove to Infinity.
  14682. // videojs/videojs-contrib-hls#1225
  14683. if (end === Infinity) {
  14684. end = this.duration_();
  14685. } // skip removes that would throw an error
  14686. // commonly happens during a rendition switch at the start of a video
  14687. // from start 0 to end 0
  14688. if (end <= start) {
  14689. this.logger_('skipping remove because end ${end} is <= start ${start}');
  14690. return;
  14691. }
  14692. if (!this.sourceUpdater_ || !this.getMediaInfo_()) {
  14693. this.logger_('skipping remove because no source updater or starting media info'); // nothing to remove if we haven't processed any media
  14694. return;
  14695. } // set it to one to complete this function's removes
  14696. let removesRemaining = 1;
  14697. const removeFinished = () => {
  14698. removesRemaining--;
  14699. if (removesRemaining === 0) {
  14700. done();
  14701. }
  14702. };
  14703. if (force || !this.audioDisabled_) {
  14704. removesRemaining++;
  14705. this.sourceUpdater_.removeAudio(start, end, removeFinished);
  14706. } // While it would be better to only remove video if the main loader has video, this
  14707. // should be safe with audio only as removeVideo will call back even if there's no
  14708. // video buffer.
  14709. //
  14710. // In theory we can check to see if there's video before calling the remove, but in
  14711. // the event that we're switching between renditions and from video to audio only
  14712. // (when we add support for that), we may need to clear the video contents despite
  14713. // what the new media will contain.
  14714. if (force || this.loaderType_ === 'main') {
  14715. this.gopBuffer_ = removeGopBuffer(this.gopBuffer_, start, end, this.timeMapping_);
  14716. removesRemaining++;
  14717. this.sourceUpdater_.removeVideo(start, end, removeFinished);
  14718. } // remove any captions and ID3 tags
  14719. for (const track in this.inbandTextTracks_) {
  14720. removeCuesFromTrack(start, end, this.inbandTextTracks_[track]);
  14721. }
  14722. removeCuesFromTrack(start, end, this.segmentMetadataTrack_); // finished this function's removes
  14723. removeFinished();
  14724. }
  14725. /**
  14726. * (re-)schedule monitorBufferTick_ to run as soon as possible
  14727. *
  14728. * @private
  14729. */
  14730. monitorBuffer_() {
  14731. if (this.checkBufferTimeout_) {
  14732. window__default["default"].clearTimeout(this.checkBufferTimeout_);
  14733. }
  14734. this.checkBufferTimeout_ = window__default["default"].setTimeout(this.monitorBufferTick_.bind(this), 1);
  14735. }
  14736. /**
  14737. * As long as the SegmentLoader is in the READY state, periodically
  14738. * invoke fillBuffer_().
  14739. *
  14740. * @private
  14741. */
  14742. monitorBufferTick_() {
  14743. if (this.state === 'READY') {
  14744. this.fillBuffer_();
  14745. }
  14746. if (this.checkBufferTimeout_) {
  14747. window__default["default"].clearTimeout(this.checkBufferTimeout_);
  14748. }
  14749. this.checkBufferTimeout_ = window__default["default"].setTimeout(this.monitorBufferTick_.bind(this), CHECK_BUFFER_DELAY);
  14750. }
  14751. /**
  14752. * fill the buffer with segements unless the sourceBuffers are
  14753. * currently updating
  14754. *
  14755. * Note: this function should only ever be called by monitorBuffer_
  14756. * and never directly
  14757. *
  14758. * @private
  14759. */
  14760. fillBuffer_() {
  14761. // TODO since the source buffer maintains a queue, and we shouldn't call this function
  14762. // except when we're ready for the next segment, this check can most likely be removed
  14763. if (this.sourceUpdater_.updating()) {
  14764. return;
  14765. } // see if we need to begin loading immediately
  14766. const segmentInfo = this.chooseNextRequest_();
  14767. if (!segmentInfo) {
  14768. return;
  14769. }
  14770. if (typeof segmentInfo.timestampOffset === 'number') {
  14771. this.isPendingTimestampOffset_ = false;
  14772. this.timelineChangeController_.pendingTimelineChange({
  14773. type: this.loaderType_,
  14774. from: this.currentTimeline_,
  14775. to: segmentInfo.timeline
  14776. });
  14777. }
  14778. this.loadSegment_(segmentInfo);
  14779. }
  14780. /**
  14781. * Determines if we should call endOfStream on the media source based
  14782. * on the state of the buffer or if appened segment was the final
  14783. * segment in the playlist.
  14784. *
  14785. * @param {number} [mediaIndex] the media index of segment we last appended
  14786. * @param {Object} [playlist] a media playlist object
  14787. * @return {boolean} do we need to call endOfStream on the MediaSource
  14788. */
  14789. isEndOfStream_(mediaIndex = this.mediaIndex, playlist = this.playlist_, partIndex = this.partIndex) {
  14790. if (!playlist || !this.mediaSource_) {
  14791. return false;
  14792. }
  14793. const segment = typeof mediaIndex === 'number' && playlist.segments[mediaIndex]; // mediaIndex is zero based but length is 1 based
  14794. const appendedLastSegment = mediaIndex + 1 === playlist.segments.length; // true if there are no parts, or this is the last part.
  14795. const appendedLastPart = !segment || !segment.parts || partIndex + 1 === segment.parts.length; // if we've buffered to the end of the video, we need to call endOfStream
  14796. // so that MediaSources can trigger the `ended` event when it runs out of
  14797. // buffered data instead of waiting for me
  14798. return playlist.endList && this.mediaSource_.readyState === 'open' && appendedLastSegment && appendedLastPart;
  14799. }
  14800. /**
  14801. * Determines what request should be made given current segment loader state.
  14802. *
  14803. * @return {Object} a request object that describes the segment/part to load
  14804. */
  14805. chooseNextRequest_() {
  14806. const buffered = this.buffered_();
  14807. const bufferedEnd = lastBufferedEnd(buffered) || 0;
  14808. const bufferedTime = timeAheadOf(buffered, this.currentTime_());
  14809. const preloaded = !this.hasPlayed_() && bufferedTime >= 1;
  14810. const haveEnoughBuffer = bufferedTime >= this.goalBufferLength_();
  14811. const segments = this.playlist_.segments; // return no segment if:
  14812. // 1. we don't have segments
  14813. // 2. The video has not yet played and we already downloaded a segment
  14814. // 3. we already have enough buffered time
  14815. if (!segments.length || preloaded || haveEnoughBuffer) {
  14816. return null;
  14817. }
  14818. this.syncPoint_ = this.syncPoint_ || this.syncController_.getSyncPoint(this.playlist_, this.duration_(), this.currentTimeline_, this.currentTime_(), this.loaderType_);
  14819. const next = {
  14820. partIndex: null,
  14821. mediaIndex: null,
  14822. startOfSegment: null,
  14823. playlist: this.playlist_,
  14824. isSyncRequest: Boolean(!this.syncPoint_)
  14825. };
  14826. if (next.isSyncRequest) {
  14827. next.mediaIndex = getSyncSegmentCandidate(this.currentTimeline_, segments, bufferedEnd);
  14828. this.logger_(`choose next request. Can not find sync point. Fallback to media Index: ${next.mediaIndex}`);
  14829. } else if (this.mediaIndex !== null) {
  14830. const segment = segments[this.mediaIndex];
  14831. const partIndex = typeof this.partIndex === 'number' ? this.partIndex : -1;
  14832. next.startOfSegment = segment.end ? segment.end : bufferedEnd;
  14833. if (segment.parts && segment.parts[partIndex + 1]) {
  14834. next.mediaIndex = this.mediaIndex;
  14835. next.partIndex = partIndex + 1;
  14836. } else {
  14837. next.mediaIndex = this.mediaIndex + 1;
  14838. }
  14839. } else {
  14840. // Find the segment containing the end of the buffer or current time.
  14841. const {
  14842. segmentIndex,
  14843. startTime,
  14844. partIndex
  14845. } = Playlist.getMediaInfoForTime({
  14846. exactManifestTimings: this.exactManifestTimings,
  14847. playlist: this.playlist_,
  14848. currentTime: this.fetchAtBuffer_ ? bufferedEnd : this.currentTime_(),
  14849. startingPartIndex: this.syncPoint_.partIndex,
  14850. startingSegmentIndex: this.syncPoint_.segmentIndex,
  14851. startTime: this.syncPoint_.time
  14852. });
  14853. next.getMediaInfoForTime = this.fetchAtBuffer_ ? `bufferedEnd ${bufferedEnd}` : `currentTime ${this.currentTime_()}`;
  14854. next.mediaIndex = segmentIndex;
  14855. next.startOfSegment = startTime;
  14856. next.partIndex = partIndex;
  14857. this.logger_(`choose next request. Playlist switched and we have a sync point. Media Index: ${next.mediaIndex} `);
  14858. }
  14859. const nextSegment = segments[next.mediaIndex];
  14860. let nextPart = nextSegment && typeof next.partIndex === 'number' && nextSegment.parts && nextSegment.parts[next.partIndex]; // if the next segment index is invalid or
  14861. // the next partIndex is invalid do not choose a next segment.
  14862. if (!nextSegment || typeof next.partIndex === 'number' && !nextPart) {
  14863. return null;
  14864. } // if the next segment has parts, and we don't have a partIndex.
  14865. // Set partIndex to 0
  14866. if (typeof next.partIndex !== 'number' && nextSegment.parts) {
  14867. next.partIndex = 0;
  14868. nextPart = nextSegment.parts[0];
  14869. } // independentSegments applies to every segment in a playlist. If independentSegments appears in a main playlist,
  14870. // it applies to each segment in each media playlist.
  14871. // https://datatracker.ietf.org/doc/html/draft-pantos-http-live-streaming-23#section-4.3.5.1
  14872. const hasIndependentSegments = this.vhs_.playlists && this.vhs_.playlists.main && this.vhs_.playlists.main.independentSegments || this.playlist_.independentSegments; // if we have no buffered data then we need to make sure
  14873. // that the next part we append is "independent" if possible.
  14874. // So we check if the previous part is independent, and request
  14875. // it if it is.
  14876. if (!bufferedTime && nextPart && !hasIndependentSegments && !nextPart.independent) {
  14877. if (next.partIndex === 0) {
  14878. const lastSegment = segments[next.mediaIndex - 1];
  14879. const lastSegmentLastPart = lastSegment.parts && lastSegment.parts.length && lastSegment.parts[lastSegment.parts.length - 1];
  14880. if (lastSegmentLastPart && lastSegmentLastPart.independent) {
  14881. next.mediaIndex -= 1;
  14882. next.partIndex = lastSegment.parts.length - 1;
  14883. next.independent = 'previous segment';
  14884. }
  14885. } else if (nextSegment.parts[next.partIndex - 1].independent) {
  14886. next.partIndex -= 1;
  14887. next.independent = 'previous part';
  14888. }
  14889. }
  14890. const ended = this.mediaSource_ && this.mediaSource_.readyState === 'ended'; // do not choose a next segment if all of the following:
  14891. // 1. this is the last segment in the playlist
  14892. // 2. end of stream has been called on the media source already
  14893. // 3. the player is not seeking
  14894. if (next.mediaIndex >= segments.length - 1 && ended && !this.seeking_()) {
  14895. return null;
  14896. }
  14897. if (this.shouldForceTimestampOffsetAfterResync_) {
  14898. this.shouldForceTimestampOffsetAfterResync_ = false;
  14899. next.forceTimestampOffset = true;
  14900. this.logger_('choose next request. Force timestamp offset after loader resync');
  14901. }
  14902. return this.generateSegmentInfo_(next);
  14903. }
  14904. generateSegmentInfo_(options) {
  14905. const {
  14906. independent,
  14907. playlist,
  14908. mediaIndex,
  14909. startOfSegment,
  14910. isSyncRequest,
  14911. partIndex,
  14912. forceTimestampOffset,
  14913. getMediaInfoForTime
  14914. } = options;
  14915. const segment = playlist.segments[mediaIndex];
  14916. const part = typeof partIndex === 'number' && segment.parts[partIndex];
  14917. const segmentInfo = {
  14918. requestId: 'segment-loader-' + Math.random(),
  14919. // resolve the segment URL relative to the playlist
  14920. uri: part && part.resolvedUri || segment.resolvedUri,
  14921. // the segment's mediaIndex at the time it was requested
  14922. mediaIndex,
  14923. partIndex: part ? partIndex : null,
  14924. // whether or not to update the SegmentLoader's state with this
  14925. // segment's mediaIndex
  14926. isSyncRequest,
  14927. startOfSegment,
  14928. // the segment's playlist
  14929. playlist,
  14930. // unencrypted bytes of the segment
  14931. bytes: null,
  14932. // when a key is defined for this segment, the encrypted bytes
  14933. encryptedBytes: null,
  14934. // The target timestampOffset for this segment when we append it
  14935. // to the source buffer
  14936. timestampOffset: null,
  14937. // The timeline that the segment is in
  14938. timeline: segment.timeline,
  14939. // The expected duration of the segment in seconds
  14940. duration: part && part.duration || segment.duration,
  14941. // retain the segment in case the playlist updates while doing an async process
  14942. segment,
  14943. part,
  14944. byteLength: 0,
  14945. transmuxer: this.transmuxer_,
  14946. // type of getMediaInfoForTime that was used to get this segment
  14947. getMediaInfoForTime,
  14948. independent
  14949. };
  14950. const overrideCheck = typeof forceTimestampOffset !== 'undefined' ? forceTimestampOffset : this.isPendingTimestampOffset_;
  14951. segmentInfo.timestampOffset = this.timestampOffsetForSegment_({
  14952. segmentTimeline: segment.timeline,
  14953. currentTimeline: this.currentTimeline_,
  14954. startOfSegment,
  14955. buffered: this.buffered_(),
  14956. overrideCheck
  14957. });
  14958. const audioBufferedEnd = lastBufferedEnd(this.sourceUpdater_.audioBuffered());
  14959. if (typeof audioBufferedEnd === 'number') {
  14960. // since the transmuxer is using the actual timing values, but the buffer is
  14961. // adjusted by the timestamp offset, we must adjust the value here
  14962. segmentInfo.audioAppendStart = audioBufferedEnd - this.sourceUpdater_.audioTimestampOffset();
  14963. }
  14964. if (this.sourceUpdater_.videoBuffered().length) {
  14965. segmentInfo.gopsToAlignWith = gopsSafeToAlignWith(this.gopBuffer_, // since the transmuxer is using the actual timing values, but the time is
  14966. // adjusted by the timestmap offset, we must adjust the value here
  14967. this.currentTime_() - this.sourceUpdater_.videoTimestampOffset(), this.timeMapping_);
  14968. }
  14969. return segmentInfo;
  14970. } // get the timestampoffset for a segment,
  14971. // added so that vtt segment loader can override and prevent
  14972. // adding timestamp offsets.
  14973. timestampOffsetForSegment_(options) {
  14974. return timestampOffsetForSegment(options);
  14975. }
  14976. /**
  14977. * Determines if the network has enough bandwidth to complete the current segment
  14978. * request in a timely manner. If not, the request will be aborted early and bandwidth
  14979. * updated to trigger a playlist switch.
  14980. *
  14981. * @param {Object} stats
  14982. * Object containing stats about the request timing and size
  14983. * @private
  14984. */
  14985. earlyAbortWhenNeeded_(stats) {
  14986. if (this.vhs_.tech_.paused() || // Don't abort if the current playlist is on the lowestEnabledRendition
  14987. // TODO: Replace using timeout with a boolean indicating whether this playlist is
  14988. // the lowestEnabledRendition.
  14989. !this.xhrOptions_.timeout || // Don't abort if we have no bandwidth information to estimate segment sizes
  14990. !this.playlist_.attributes.BANDWIDTH) {
  14991. return;
  14992. } // Wait at least 1 second since the first byte of data has been received before
  14993. // using the calculated bandwidth from the progress event to allow the bitrate
  14994. // to stabilize
  14995. if (Date.now() - (stats.firstBytesReceivedAt || Date.now()) < 1000) {
  14996. return;
  14997. }
  14998. const currentTime = this.currentTime_();
  14999. const measuredBandwidth = stats.bandwidth;
  15000. const segmentDuration = this.pendingSegment_.duration;
  15001. const requestTimeRemaining = Playlist.estimateSegmentRequestTime(segmentDuration, measuredBandwidth, this.playlist_, stats.bytesReceived); // Subtract 1 from the timeUntilRebuffer so we still consider an early abort
  15002. // if we are only left with less than 1 second when the request completes.
  15003. // A negative timeUntilRebuffering indicates we are already rebuffering
  15004. const timeUntilRebuffer$1 = timeUntilRebuffer(this.buffered_(), currentTime, this.vhs_.tech_.playbackRate()) - 1; // Only consider aborting early if the estimated time to finish the download
  15005. // is larger than the estimated time until the player runs out of forward buffer
  15006. if (requestTimeRemaining <= timeUntilRebuffer$1) {
  15007. return;
  15008. }
  15009. const switchCandidate = minRebufferMaxBandwidthSelector({
  15010. main: this.vhs_.playlists.main,
  15011. currentTime,
  15012. bandwidth: measuredBandwidth,
  15013. duration: this.duration_(),
  15014. segmentDuration,
  15015. timeUntilRebuffer: timeUntilRebuffer$1,
  15016. currentTimeline: this.currentTimeline_,
  15017. syncController: this.syncController_
  15018. });
  15019. if (!switchCandidate) {
  15020. return;
  15021. }
  15022. const rebufferingImpact = requestTimeRemaining - timeUntilRebuffer$1;
  15023. const timeSavedBySwitching = rebufferingImpact - switchCandidate.rebufferingImpact;
  15024. let minimumTimeSaving = 0.5; // If we are already rebuffering, increase the amount of variance we add to the
  15025. // potential round trip time of the new request so that we are not too aggressive
  15026. // with switching to a playlist that might save us a fraction of a second.
  15027. if (timeUntilRebuffer$1 <= TIME_FUDGE_FACTOR) {
  15028. minimumTimeSaving = 1;
  15029. }
  15030. if (!switchCandidate.playlist || switchCandidate.playlist.uri === this.playlist_.uri || timeSavedBySwitching < minimumTimeSaving) {
  15031. return;
  15032. } // set the bandwidth to that of the desired playlist being sure to scale by
  15033. // BANDWIDTH_VARIANCE and add one so the playlist selector does not exclude it
  15034. // don't trigger a bandwidthupdate as the bandwidth is artifial
  15035. this.bandwidth = switchCandidate.playlist.attributes.BANDWIDTH * Config.BANDWIDTH_VARIANCE + 1;
  15036. this.trigger('earlyabort');
  15037. }
  15038. handleAbort_(segmentInfo) {
  15039. this.logger_(`Aborting ${segmentInfoString(segmentInfo)}`);
  15040. this.mediaRequestsAborted += 1;
  15041. }
  15042. /**
  15043. * XHR `progress` event handler
  15044. *
  15045. * @param {Event}
  15046. * The XHR `progress` event
  15047. * @param {Object} simpleSegment
  15048. * A simplified segment object copy
  15049. * @private
  15050. */
  15051. handleProgress_(event, simpleSegment) {
  15052. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15053. if (this.checkForAbort_(simpleSegment.requestId)) {
  15054. return;
  15055. }
  15056. this.trigger('progress');
  15057. }
  15058. handleTrackInfo_(simpleSegment, trackInfo) {
  15059. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15060. if (this.checkForAbort_(simpleSegment.requestId)) {
  15061. return;
  15062. }
  15063. if (this.checkForIllegalMediaSwitch(trackInfo)) {
  15064. return;
  15065. }
  15066. trackInfo = trackInfo || {}; // When we have track info, determine what media types this loader is dealing with.
  15067. // Guard against cases where we're not getting track info at all until we are
  15068. // certain that all streams will provide it.
  15069. if (!shallowEqual(this.currentMediaInfo_, trackInfo)) {
  15070. this.appendInitSegment_ = {
  15071. audio: true,
  15072. video: true
  15073. };
  15074. this.startingMediaInfo_ = trackInfo;
  15075. this.currentMediaInfo_ = trackInfo;
  15076. this.logger_('trackinfo update', trackInfo);
  15077. this.trigger('trackinfo');
  15078. } // trackinfo may cause an abort if the trackinfo
  15079. // causes a codec change to an unsupported codec.
  15080. if (this.checkForAbort_(simpleSegment.requestId)) {
  15081. return;
  15082. } // set trackinfo on the pending segment so that
  15083. // it can append.
  15084. this.pendingSegment_.trackInfo = trackInfo; // check if any calls were waiting on the track info
  15085. if (this.hasEnoughInfoToAppend_()) {
  15086. this.processCallQueue_();
  15087. }
  15088. }
  15089. handleTimingInfo_(simpleSegment, mediaType, timeType, time) {
  15090. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15091. if (this.checkForAbort_(simpleSegment.requestId)) {
  15092. return;
  15093. }
  15094. const segmentInfo = this.pendingSegment_;
  15095. const timingInfoProperty = timingInfoPropertyForMedia(mediaType);
  15096. segmentInfo[timingInfoProperty] = segmentInfo[timingInfoProperty] || {};
  15097. segmentInfo[timingInfoProperty][timeType] = time;
  15098. this.logger_(`timinginfo: ${mediaType} - ${timeType} - ${time}`); // check if any calls were waiting on the timing info
  15099. if (this.hasEnoughInfoToAppend_()) {
  15100. this.processCallQueue_();
  15101. }
  15102. }
  15103. handleCaptions_(simpleSegment, captionData) {
  15104. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15105. if (this.checkForAbort_(simpleSegment.requestId)) {
  15106. return;
  15107. } // This could only happen with fmp4 segments, but
  15108. // should still not happen in general
  15109. if (captionData.length === 0) {
  15110. this.logger_('SegmentLoader received no captions from a caption event');
  15111. return;
  15112. }
  15113. const segmentInfo = this.pendingSegment_; // Wait until we have some video data so that caption timing
  15114. // can be adjusted by the timestamp offset
  15115. if (!segmentInfo.hasAppendedData_) {
  15116. this.metadataQueue_.caption.push(this.handleCaptions_.bind(this, simpleSegment, captionData));
  15117. return;
  15118. }
  15119. const timestampOffset = this.sourceUpdater_.videoTimestampOffset() === null ? this.sourceUpdater_.audioTimestampOffset() : this.sourceUpdater_.videoTimestampOffset();
  15120. const captionTracks = {}; // get total start/end and captions for each track/stream
  15121. captionData.forEach(caption => {
  15122. // caption.stream is actually a track name...
  15123. // set to the existing values in tracks or default values
  15124. captionTracks[caption.stream] = captionTracks[caption.stream] || {
  15125. // Infinity, as any other value will be less than this
  15126. startTime: Infinity,
  15127. captions: [],
  15128. // 0 as an other value will be more than this
  15129. endTime: 0
  15130. };
  15131. const captionTrack = captionTracks[caption.stream];
  15132. captionTrack.startTime = Math.min(captionTrack.startTime, caption.startTime + timestampOffset);
  15133. captionTrack.endTime = Math.max(captionTrack.endTime, caption.endTime + timestampOffset);
  15134. captionTrack.captions.push(caption);
  15135. });
  15136. Object.keys(captionTracks).forEach(trackName => {
  15137. const {
  15138. startTime,
  15139. endTime,
  15140. captions
  15141. } = captionTracks[trackName];
  15142. const inbandTextTracks = this.inbandTextTracks_;
  15143. this.logger_(`adding cues from ${startTime} -> ${endTime} for ${trackName}`);
  15144. createCaptionsTrackIfNotExists(inbandTextTracks, this.vhs_.tech_, trackName); // clear out any cues that start and end at the same time period for the same track.
  15145. // We do this because a rendition change that also changes the timescale for captions
  15146. // will result in captions being re-parsed for certain segments. If we add them again
  15147. // without clearing we will have two of the same captions visible.
  15148. removeCuesFromTrack(startTime, endTime, inbandTextTracks[trackName]);
  15149. addCaptionData({
  15150. captionArray: captions,
  15151. inbandTextTracks,
  15152. timestampOffset
  15153. });
  15154. }); // Reset stored captions since we added parsed
  15155. // captions to a text track at this point
  15156. if (this.transmuxer_) {
  15157. this.transmuxer_.postMessage({
  15158. action: 'clearParsedMp4Captions'
  15159. });
  15160. }
  15161. }
  15162. handleId3_(simpleSegment, id3Frames, dispatchType) {
  15163. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15164. if (this.checkForAbort_(simpleSegment.requestId)) {
  15165. return;
  15166. }
  15167. const segmentInfo = this.pendingSegment_; // we need to have appended data in order for the timestamp offset to be set
  15168. if (!segmentInfo.hasAppendedData_) {
  15169. this.metadataQueue_.id3.push(this.handleId3_.bind(this, simpleSegment, id3Frames, dispatchType));
  15170. return;
  15171. }
  15172. this.addMetadataToTextTrack(dispatchType, id3Frames, this.duration_());
  15173. }
  15174. processMetadataQueue_() {
  15175. this.metadataQueue_.id3.forEach(fn => fn());
  15176. this.metadataQueue_.caption.forEach(fn => fn());
  15177. this.metadataQueue_.id3 = [];
  15178. this.metadataQueue_.caption = [];
  15179. }
  15180. processCallQueue_() {
  15181. const callQueue = this.callQueue_; // Clear out the queue before the queued functions are run, since some of the
  15182. // functions may check the length of the load queue and default to pushing themselves
  15183. // back onto the queue.
  15184. this.callQueue_ = [];
  15185. callQueue.forEach(fun => fun());
  15186. }
  15187. processLoadQueue_() {
  15188. const loadQueue = this.loadQueue_; // Clear out the queue before the queued functions are run, since some of the
  15189. // functions may check the length of the load queue and default to pushing themselves
  15190. // back onto the queue.
  15191. this.loadQueue_ = [];
  15192. loadQueue.forEach(fun => fun());
  15193. }
  15194. /**
  15195. * Determines whether the loader has enough info to load the next segment.
  15196. *
  15197. * @return {boolean}
  15198. * Whether or not the loader has enough info to load the next segment
  15199. */
  15200. hasEnoughInfoToLoad_() {
  15201. // Since primary timing goes by video, only the audio loader potentially needs to wait
  15202. // to load.
  15203. if (this.loaderType_ !== 'audio') {
  15204. return true;
  15205. }
  15206. const segmentInfo = this.pendingSegment_; // A fill buffer must have already run to establish a pending segment before there's
  15207. // enough info to load.
  15208. if (!segmentInfo) {
  15209. return false;
  15210. } // The first segment can and should be loaded immediately so that source buffers are
  15211. // created together (before appending). Source buffer creation uses the presence of
  15212. // audio and video data to determine whether to create audio/video source buffers, and
  15213. // uses processed (transmuxed or parsed) media to determine the types required.
  15214. if (!this.getCurrentMediaInfo_()) {
  15215. return true;
  15216. }
  15217. if ( // Technically, instead of waiting to load a segment on timeline changes, a segment
  15218. // can be requested and downloaded and only wait before it is transmuxed or parsed.
  15219. // But in practice, there are a few reasons why it is better to wait until a loader
  15220. // is ready to append that segment before requesting and downloading:
  15221. //
  15222. // 1. Because audio and main loaders cross discontinuities together, if this loader
  15223. // is waiting for the other to catch up, then instead of requesting another
  15224. // segment and using up more bandwidth, by not yet loading, more bandwidth is
  15225. // allotted to the loader currently behind.
  15226. // 2. media-segment-request doesn't have to have logic to consider whether a segment
  15227. // is ready to be processed or not, isolating the queueing behavior to the loader.
  15228. // 3. The audio loader bases some of its segment properties on timing information
  15229. // provided by the main loader, meaning that, if the logic for waiting on
  15230. // processing was in media-segment-request, then it would also need to know how
  15231. // to re-generate the segment information after the main loader caught up.
  15232. shouldWaitForTimelineChange({
  15233. timelineChangeController: this.timelineChangeController_,
  15234. currentTimeline: this.currentTimeline_,
  15235. segmentTimeline: segmentInfo.timeline,
  15236. loaderType: this.loaderType_,
  15237. audioDisabled: this.audioDisabled_
  15238. })) {
  15239. return false;
  15240. }
  15241. return true;
  15242. }
  15243. getCurrentMediaInfo_(segmentInfo = this.pendingSegment_) {
  15244. return segmentInfo && segmentInfo.trackInfo || this.currentMediaInfo_;
  15245. }
  15246. getMediaInfo_(segmentInfo = this.pendingSegment_) {
  15247. return this.getCurrentMediaInfo_(segmentInfo) || this.startingMediaInfo_;
  15248. }
  15249. getPendingSegmentPlaylist() {
  15250. return this.pendingSegment_ ? this.pendingSegment_.playlist : null;
  15251. }
  15252. hasEnoughInfoToAppend_() {
  15253. if (!this.sourceUpdater_.ready()) {
  15254. return false;
  15255. } // If content needs to be removed or the loader is waiting on an append reattempt,
  15256. // then no additional content should be appended until the prior append is resolved.
  15257. if (this.waitingOnRemove_ || this.quotaExceededErrorRetryTimeout_) {
  15258. return false;
  15259. }
  15260. const segmentInfo = this.pendingSegment_;
  15261. const trackInfo = this.getCurrentMediaInfo_(); // no segment to append any data for or
  15262. // we do not have information on this specific
  15263. // segment yet
  15264. if (!segmentInfo || !trackInfo) {
  15265. return false;
  15266. }
  15267. const {
  15268. hasAudio,
  15269. hasVideo,
  15270. isMuxed
  15271. } = trackInfo;
  15272. if (hasVideo && !segmentInfo.videoTimingInfo) {
  15273. return false;
  15274. } // muxed content only relies on video timing information for now.
  15275. if (hasAudio && !this.audioDisabled_ && !isMuxed && !segmentInfo.audioTimingInfo) {
  15276. return false;
  15277. }
  15278. if (shouldWaitForTimelineChange({
  15279. timelineChangeController: this.timelineChangeController_,
  15280. currentTimeline: this.currentTimeline_,
  15281. segmentTimeline: segmentInfo.timeline,
  15282. loaderType: this.loaderType_,
  15283. audioDisabled: this.audioDisabled_
  15284. })) {
  15285. return false;
  15286. }
  15287. return true;
  15288. }
  15289. handleData_(simpleSegment, result) {
  15290. this.earlyAbortWhenNeeded_(simpleSegment.stats);
  15291. if (this.checkForAbort_(simpleSegment.requestId)) {
  15292. return;
  15293. } // If there's anything in the call queue, then this data came later and should be
  15294. // executed after the calls currently queued.
  15295. if (this.callQueue_.length || !this.hasEnoughInfoToAppend_()) {
  15296. this.callQueue_.push(this.handleData_.bind(this, simpleSegment, result));
  15297. return;
  15298. }
  15299. const segmentInfo = this.pendingSegment_; // update the time mapping so we can translate from display time to media time
  15300. this.setTimeMapping_(segmentInfo.timeline); // for tracking overall stats
  15301. this.updateMediaSecondsLoaded_(segmentInfo.part || segmentInfo.segment); // Note that the state isn't changed from loading to appending. This is because abort
  15302. // logic may change behavior depending on the state, and changing state too early may
  15303. // inflate our estimates of bandwidth. In the future this should be re-examined to
  15304. // note more granular states.
  15305. // don't process and append data if the mediaSource is closed
  15306. if (this.mediaSource_.readyState === 'closed') {
  15307. return;
  15308. } // if this request included an initialization segment, save that data
  15309. // to the initSegment cache
  15310. if (simpleSegment.map) {
  15311. simpleSegment.map = this.initSegmentForMap(simpleSegment.map, true); // move over init segment properties to media request
  15312. segmentInfo.segment.map = simpleSegment.map;
  15313. } // if this request included a segment key, save that data in the cache
  15314. if (simpleSegment.key) {
  15315. this.segmentKey(simpleSegment.key, true);
  15316. }
  15317. segmentInfo.isFmp4 = simpleSegment.isFmp4;
  15318. segmentInfo.timingInfo = segmentInfo.timingInfo || {};
  15319. if (segmentInfo.isFmp4) {
  15320. this.trigger('fmp4');
  15321. segmentInfo.timingInfo.start = segmentInfo[timingInfoPropertyForMedia(result.type)].start;
  15322. } else {
  15323. const trackInfo = this.getCurrentMediaInfo_();
  15324. const useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
  15325. let firstVideoFrameTimeForData;
  15326. if (useVideoTimingInfo) {
  15327. firstVideoFrameTimeForData = segmentInfo.videoTimingInfo.start;
  15328. } // Segment loader knows more about segment timing than the transmuxer (in certain
  15329. // aspects), so make any changes required for a more accurate start time.
  15330. // Don't set the end time yet, as the segment may not be finished processing.
  15331. segmentInfo.timingInfo.start = this.trueSegmentStart_({
  15332. currentStart: segmentInfo.timingInfo.start,
  15333. playlist: segmentInfo.playlist,
  15334. mediaIndex: segmentInfo.mediaIndex,
  15335. currentVideoTimestampOffset: this.sourceUpdater_.videoTimestampOffset(),
  15336. useVideoTimingInfo,
  15337. firstVideoFrameTimeForData,
  15338. videoTimingInfo: segmentInfo.videoTimingInfo,
  15339. audioTimingInfo: segmentInfo.audioTimingInfo
  15340. });
  15341. } // Init segments for audio and video only need to be appended in certain cases. Now
  15342. // that data is about to be appended, we can check the final cases to determine
  15343. // whether we should append an init segment.
  15344. this.updateAppendInitSegmentStatus(segmentInfo, result.type); // Timestamp offset should be updated once we get new data and have its timing info,
  15345. // as we use the start of the segment to offset the best guess (playlist provided)
  15346. // timestamp offset.
  15347. this.updateSourceBufferTimestampOffset_(segmentInfo); // if this is a sync request we need to determine whether it should
  15348. // be appended or not.
  15349. if (segmentInfo.isSyncRequest) {
  15350. // first save/update our timing info for this segment.
  15351. // this is what allows us to choose an accurate segment
  15352. // and the main reason we make a sync request.
  15353. this.updateTimingInfoEnd_(segmentInfo);
  15354. this.syncController_.saveSegmentTimingInfo({
  15355. segmentInfo,
  15356. shouldSaveTimelineMapping: this.loaderType_ === 'main'
  15357. });
  15358. const next = this.chooseNextRequest_(); // If the sync request isn't the segment that would be requested next
  15359. // after taking into account its timing info, do not append it.
  15360. if (next.mediaIndex !== segmentInfo.mediaIndex || next.partIndex !== segmentInfo.partIndex) {
  15361. this.logger_('sync segment was incorrect, not appending');
  15362. return;
  15363. } // otherwise append it like any other segment as our guess was correct.
  15364. this.logger_('sync segment was correct, appending');
  15365. } // Save some state so that in the future anything waiting on first append (and/or
  15366. // timestamp offset(s)) can process immediately. While the extra state isn't optimal,
  15367. // we need some notion of whether the timestamp offset or other relevant information
  15368. // has had a chance to be set.
  15369. segmentInfo.hasAppendedData_ = true; // Now that the timestamp offset should be set, we can append any waiting ID3 tags.
  15370. this.processMetadataQueue_();
  15371. this.appendData_(segmentInfo, result);
  15372. }
  15373. updateAppendInitSegmentStatus(segmentInfo, type) {
  15374. // alt audio doesn't manage timestamp offset
  15375. if (this.loaderType_ === 'main' && typeof segmentInfo.timestampOffset === 'number' && // in the case that we're handling partial data, we don't want to append an init
  15376. // segment for each chunk
  15377. !segmentInfo.changedTimestampOffset) {
  15378. // if the timestamp offset changed, the timeline may have changed, so we have to re-
  15379. // append init segments
  15380. this.appendInitSegment_ = {
  15381. audio: true,
  15382. video: true
  15383. };
  15384. }
  15385. if (this.playlistOfLastInitSegment_[type] !== segmentInfo.playlist) {
  15386. // make sure we append init segment on playlist changes, in case the media config
  15387. // changed
  15388. this.appendInitSegment_[type] = true;
  15389. }
  15390. }
  15391. getInitSegmentAndUpdateState_({
  15392. type,
  15393. initSegment,
  15394. map,
  15395. playlist
  15396. }) {
  15397. // "The EXT-X-MAP tag specifies how to obtain the Media Initialization Section
  15398. // (Section 3) required to parse the applicable Media Segments. It applies to every
  15399. // Media Segment that appears after it in the Playlist until the next EXT-X-MAP tag
  15400. // or until the end of the playlist."
  15401. // https://tools.ietf.org/html/draft-pantos-http-live-streaming-23#section-4.3.2.5
  15402. if (map) {
  15403. const id = initSegmentId(map);
  15404. if (this.activeInitSegmentId_ === id) {
  15405. // don't need to re-append the init segment if the ID matches
  15406. return null;
  15407. } // a map-specified init segment takes priority over any transmuxed (or otherwise
  15408. // obtained) init segment
  15409. //
  15410. // this also caches the init segment for later use
  15411. initSegment = this.initSegmentForMap(map, true).bytes;
  15412. this.activeInitSegmentId_ = id;
  15413. } // We used to always prepend init segments for video, however, that shouldn't be
  15414. // necessary. Instead, we should only append on changes, similar to what we've always
  15415. // done for audio. This is more important (though may not be that important) for
  15416. // frame-by-frame appending for LHLS, simply because of the increased quantity of
  15417. // appends.
  15418. if (initSegment && this.appendInitSegment_[type]) {
  15419. // Make sure we track the playlist that we last used for the init segment, so that
  15420. // we can re-append the init segment in the event that we get data from a new
  15421. // playlist. Discontinuities and track changes are handled in other sections.
  15422. this.playlistOfLastInitSegment_[type] = playlist; // Disable future init segment appends for this type. Until a change is necessary.
  15423. this.appendInitSegment_[type] = false; // we need to clear out the fmp4 active init segment id, since
  15424. // we are appending the muxer init segment
  15425. this.activeInitSegmentId_ = null;
  15426. return initSegment;
  15427. }
  15428. return null;
  15429. }
  15430. handleQuotaExceededError_({
  15431. segmentInfo,
  15432. type,
  15433. bytes
  15434. }, error) {
  15435. const audioBuffered = this.sourceUpdater_.audioBuffered();
  15436. const videoBuffered = this.sourceUpdater_.videoBuffered(); // For now we're ignoring any notion of gaps in the buffer, but they, in theory,
  15437. // should be cleared out during the buffer removals. However, log in case it helps
  15438. // debug.
  15439. if (audioBuffered.length > 1) {
  15440. this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the audio buffer: ' + timeRangesToArray(audioBuffered).join(', '));
  15441. }
  15442. if (videoBuffered.length > 1) {
  15443. this.logger_('On QUOTA_EXCEEDED_ERR, found gaps in the video buffer: ' + timeRangesToArray(videoBuffered).join(', '));
  15444. }
  15445. const audioBufferStart = audioBuffered.length ? audioBuffered.start(0) : 0;
  15446. const audioBufferEnd = audioBuffered.length ? audioBuffered.end(audioBuffered.length - 1) : 0;
  15447. const videoBufferStart = videoBuffered.length ? videoBuffered.start(0) : 0;
  15448. const videoBufferEnd = videoBuffered.length ? videoBuffered.end(videoBuffered.length - 1) : 0;
  15449. if (audioBufferEnd - audioBufferStart <= MIN_BACK_BUFFER && videoBufferEnd - videoBufferStart <= MIN_BACK_BUFFER) {
  15450. // Can't remove enough buffer to make room for new segment (or the browser doesn't
  15451. // allow for appends of segments this size). In the future, it may be possible to
  15452. // split up the segment and append in pieces, but for now, error out this playlist
  15453. // in an attempt to switch to a more manageable rendition.
  15454. this.logger_('On QUOTA_EXCEEDED_ERR, single segment too large to append to ' + 'buffer, triggering an error. ' + `Appended byte length: ${bytes.byteLength}, ` + `audio buffer: ${timeRangesToArray(audioBuffered).join(', ')}, ` + `video buffer: ${timeRangesToArray(videoBuffered).join(', ')}, `);
  15455. this.error({
  15456. message: 'Quota exceeded error with append of a single segment of content',
  15457. excludeUntil: Infinity
  15458. });
  15459. this.trigger('error');
  15460. return;
  15461. } // To try to resolve the quota exceeded error, clear back buffer and retry. This means
  15462. // that the segment-loader should block on future events until this one is handled, so
  15463. // that it doesn't keep moving onto further segments. Adding the call to the call
  15464. // queue will prevent further appends until waitingOnRemove_ and
  15465. // quotaExceededErrorRetryTimeout_ are cleared.
  15466. //
  15467. // Note that this will only block the current loader. In the case of demuxed content,
  15468. // the other load may keep filling as fast as possible. In practice, this should be
  15469. // OK, as it is a rare case when either audio has a high enough bitrate to fill up a
  15470. // source buffer, or video fills without enough room for audio to append (and without
  15471. // the availability of clearing out seconds of back buffer to make room for audio).
  15472. // But it might still be good to handle this case in the future as a TODO.
  15473. this.waitingOnRemove_ = true;
  15474. this.callQueue_.push(this.appendToSourceBuffer_.bind(this, {
  15475. segmentInfo,
  15476. type,
  15477. bytes
  15478. }));
  15479. const currentTime = this.currentTime_(); // Try to remove as much audio and video as possible to make room for new content
  15480. // before retrying.
  15481. const timeToRemoveUntil = currentTime - MIN_BACK_BUFFER;
  15482. this.logger_(`On QUOTA_EXCEEDED_ERR, removing audio/video from 0 to ${timeToRemoveUntil}`);
  15483. this.remove(0, timeToRemoveUntil, () => {
  15484. this.logger_(`On QUOTA_EXCEEDED_ERR, retrying append in ${MIN_BACK_BUFFER}s`);
  15485. this.waitingOnRemove_ = false; // wait the length of time alotted in the back buffer to prevent wasted
  15486. // attempts (since we can't clear less than the minimum)
  15487. this.quotaExceededErrorRetryTimeout_ = window__default["default"].setTimeout(() => {
  15488. this.logger_('On QUOTA_EXCEEDED_ERR, re-processing call queue');
  15489. this.quotaExceededErrorRetryTimeout_ = null;
  15490. this.processCallQueue_();
  15491. }, MIN_BACK_BUFFER * 1000);
  15492. }, true);
  15493. }
  15494. handleAppendError_({
  15495. segmentInfo,
  15496. type,
  15497. bytes
  15498. }, error) {
  15499. // if there's no error, nothing to do
  15500. if (!error) {
  15501. return;
  15502. }
  15503. if (error.code === QUOTA_EXCEEDED_ERR) {
  15504. this.handleQuotaExceededError_({
  15505. segmentInfo,
  15506. type,
  15507. bytes
  15508. }); // A quota exceeded error should be recoverable with a future re-append, so no need
  15509. // to trigger an append error.
  15510. return;
  15511. }
  15512. this.logger_('Received non QUOTA_EXCEEDED_ERR on append', error);
  15513. this.error(`${type} append of ${bytes.length}b failed for segment ` + `#${segmentInfo.mediaIndex} in playlist ${segmentInfo.playlist.id}`); // If an append errors, we often can't recover.
  15514. // (see https://w3c.github.io/media-source/#sourcebuffer-append-error).
  15515. //
  15516. // Trigger a special error so that it can be handled separately from normal,
  15517. // recoverable errors.
  15518. this.trigger('appenderror');
  15519. }
  15520. appendToSourceBuffer_({
  15521. segmentInfo,
  15522. type,
  15523. initSegment,
  15524. data,
  15525. bytes
  15526. }) {
  15527. // If this is a re-append, bytes were already created and don't need to be recreated
  15528. if (!bytes) {
  15529. const segments = [data];
  15530. let byteLength = data.byteLength;
  15531. if (initSegment) {
  15532. // if the media initialization segment is changing, append it before the content
  15533. // segment
  15534. segments.unshift(initSegment);
  15535. byteLength += initSegment.byteLength;
  15536. } // Technically we should be OK appending the init segment separately, however, we
  15537. // haven't yet tested that, and prepending is how we have always done things.
  15538. bytes = concatSegments({
  15539. bytes: byteLength,
  15540. segments
  15541. });
  15542. }
  15543. this.sourceUpdater_.appendBuffer({
  15544. segmentInfo,
  15545. type,
  15546. bytes
  15547. }, this.handleAppendError_.bind(this, {
  15548. segmentInfo,
  15549. type,
  15550. bytes
  15551. }));
  15552. }
  15553. handleSegmentTimingInfo_(type, requestId, segmentTimingInfo) {
  15554. if (!this.pendingSegment_ || requestId !== this.pendingSegment_.requestId) {
  15555. return;
  15556. }
  15557. const segment = this.pendingSegment_.segment;
  15558. const timingInfoProperty = `${type}TimingInfo`;
  15559. if (!segment[timingInfoProperty]) {
  15560. segment[timingInfoProperty] = {};
  15561. }
  15562. segment[timingInfoProperty].transmuxerPrependedSeconds = segmentTimingInfo.prependedContentDuration || 0;
  15563. segment[timingInfoProperty].transmuxedPresentationStart = segmentTimingInfo.start.presentation;
  15564. segment[timingInfoProperty].transmuxedDecodeStart = segmentTimingInfo.start.decode;
  15565. segment[timingInfoProperty].transmuxedPresentationEnd = segmentTimingInfo.end.presentation;
  15566. segment[timingInfoProperty].transmuxedDecodeEnd = segmentTimingInfo.end.decode; // mainly used as a reference for debugging
  15567. segment[timingInfoProperty].baseMediaDecodeTime = segmentTimingInfo.baseMediaDecodeTime;
  15568. }
  15569. appendData_(segmentInfo, result) {
  15570. const {
  15571. type,
  15572. data
  15573. } = result;
  15574. if (!data || !data.byteLength) {
  15575. return;
  15576. }
  15577. if (type === 'audio' && this.audioDisabled_) {
  15578. return;
  15579. }
  15580. const initSegment = this.getInitSegmentAndUpdateState_({
  15581. type,
  15582. initSegment: result.initSegment,
  15583. playlist: segmentInfo.playlist,
  15584. map: segmentInfo.isFmp4 ? segmentInfo.segment.map : null
  15585. });
  15586. this.appendToSourceBuffer_({
  15587. segmentInfo,
  15588. type,
  15589. initSegment,
  15590. data
  15591. });
  15592. }
  15593. /**
  15594. * load a specific segment from a request into the buffer
  15595. *
  15596. * @private
  15597. */
  15598. loadSegment_(segmentInfo) {
  15599. this.state = 'WAITING';
  15600. this.pendingSegment_ = segmentInfo;
  15601. this.trimBackBuffer_(segmentInfo);
  15602. if (typeof segmentInfo.timestampOffset === 'number') {
  15603. if (this.transmuxer_) {
  15604. this.transmuxer_.postMessage({
  15605. action: 'clearAllMp4Captions'
  15606. });
  15607. }
  15608. }
  15609. if (!this.hasEnoughInfoToLoad_()) {
  15610. this.loadQueue_.push(() => {
  15611. // regenerate the audioAppendStart, timestampOffset, etc as they
  15612. // may have changed since this function was added to the queue.
  15613. const options = _extends__default["default"]({}, segmentInfo, {
  15614. forceTimestampOffset: true
  15615. });
  15616. _extends__default["default"](segmentInfo, this.generateSegmentInfo_(options));
  15617. this.isPendingTimestampOffset_ = false;
  15618. this.updateTransmuxerAndRequestSegment_(segmentInfo);
  15619. });
  15620. return;
  15621. }
  15622. this.updateTransmuxerAndRequestSegment_(segmentInfo);
  15623. }
  15624. updateTransmuxerAndRequestSegment_(segmentInfo) {
  15625. // We'll update the source buffer's timestamp offset once we have transmuxed data, but
  15626. // the transmuxer still needs to be updated before then.
  15627. //
  15628. // Even though keepOriginalTimestamps is set to true for the transmuxer, timestamp
  15629. // offset must be passed to the transmuxer for stream correcting adjustments.
  15630. if (this.shouldUpdateTransmuxerTimestampOffset_(segmentInfo.timestampOffset)) {
  15631. this.gopBuffer_.length = 0; // gopsToAlignWith was set before the GOP buffer was cleared
  15632. segmentInfo.gopsToAlignWith = [];
  15633. this.timeMapping_ = 0; // reset values in the transmuxer since a discontinuity should start fresh
  15634. this.transmuxer_.postMessage({
  15635. action: 'reset'
  15636. });
  15637. this.transmuxer_.postMessage({
  15638. action: 'setTimestampOffset',
  15639. timestampOffset: segmentInfo.timestampOffset
  15640. });
  15641. }
  15642. const simpleSegment = this.createSimplifiedSegmentObj_(segmentInfo);
  15643. const isEndOfStream = this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex);
  15644. const isWalkingForward = this.mediaIndex !== null;
  15645. const isDiscontinuity = segmentInfo.timeline !== this.currentTimeline_ && // currentTimeline starts at -1, so we shouldn't end the timeline switching to 0,
  15646. // the first timeline
  15647. segmentInfo.timeline > 0;
  15648. const isEndOfTimeline = isEndOfStream || isWalkingForward && isDiscontinuity;
  15649. this.logger_(`Requesting ${segmentInfoString(segmentInfo)}`); // If there's an init segment associated with this segment, but it is not cached (identified by a lack of bytes),
  15650. // then this init segment has never been seen before and should be appended.
  15651. //
  15652. // At this point the content type (audio/video or both) is not yet known, but it should be safe to set
  15653. // both to true and leave the decision of whether to append the init segment to append time.
  15654. if (simpleSegment.map && !simpleSegment.map.bytes) {
  15655. this.logger_('going to request init segment.');
  15656. this.appendInitSegment_ = {
  15657. video: true,
  15658. audio: true
  15659. };
  15660. }
  15661. segmentInfo.abortRequests = mediaSegmentRequest({
  15662. xhr: this.vhs_.xhr,
  15663. xhrOptions: this.xhrOptions_,
  15664. decryptionWorker: this.decrypter_,
  15665. segment: simpleSegment,
  15666. abortFn: this.handleAbort_.bind(this, segmentInfo),
  15667. progressFn: this.handleProgress_.bind(this),
  15668. trackInfoFn: this.handleTrackInfo_.bind(this),
  15669. timingInfoFn: this.handleTimingInfo_.bind(this),
  15670. videoSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'video', segmentInfo.requestId),
  15671. audioSegmentTimingInfoFn: this.handleSegmentTimingInfo_.bind(this, 'audio', segmentInfo.requestId),
  15672. captionsFn: this.handleCaptions_.bind(this),
  15673. isEndOfTimeline,
  15674. endedTimelineFn: () => {
  15675. this.logger_('received endedtimeline callback');
  15676. },
  15677. id3Fn: this.handleId3_.bind(this),
  15678. dataFn: this.handleData_.bind(this),
  15679. doneFn: this.segmentRequestFinished_.bind(this),
  15680. onTransmuxerLog: ({
  15681. message,
  15682. level,
  15683. stream
  15684. }) => {
  15685. this.logger_(`${segmentInfoString(segmentInfo)} logged from transmuxer stream ${stream} as a ${level}: ${message}`);
  15686. }
  15687. });
  15688. }
  15689. /**
  15690. * trim the back buffer so that we don't have too much data
  15691. * in the source buffer
  15692. *
  15693. * @private
  15694. *
  15695. * @param {Object} segmentInfo - the current segment
  15696. */
  15697. trimBackBuffer_(segmentInfo) {
  15698. const removeToTime = safeBackBufferTrimTime(this.seekable_(), this.currentTime_(), this.playlist_.targetDuration || 10); // Chrome has a hard limit of 150MB of
  15699. // buffer and a very conservative "garbage collector"
  15700. // We manually clear out the old buffer to ensure
  15701. // we don't trigger the QuotaExceeded error
  15702. // on the source buffer during subsequent appends
  15703. if (removeToTime > 0) {
  15704. this.remove(0, removeToTime);
  15705. }
  15706. }
  15707. /**
  15708. * created a simplified copy of the segment object with just the
  15709. * information necessary to perform the XHR and decryption
  15710. *
  15711. * @private
  15712. *
  15713. * @param {Object} segmentInfo - the current segment
  15714. * @return {Object} a simplified segment object copy
  15715. */
  15716. createSimplifiedSegmentObj_(segmentInfo) {
  15717. const segment = segmentInfo.segment;
  15718. const part = segmentInfo.part;
  15719. const simpleSegment = {
  15720. resolvedUri: part ? part.resolvedUri : segment.resolvedUri,
  15721. byterange: part ? part.byterange : segment.byterange,
  15722. requestId: segmentInfo.requestId,
  15723. transmuxer: segmentInfo.transmuxer,
  15724. audioAppendStart: segmentInfo.audioAppendStart,
  15725. gopsToAlignWith: segmentInfo.gopsToAlignWith,
  15726. part: segmentInfo.part
  15727. };
  15728. const previousSegment = segmentInfo.playlist.segments[segmentInfo.mediaIndex - 1];
  15729. if (previousSegment && previousSegment.timeline === segment.timeline) {
  15730. // The baseStartTime of a segment is used to handle rollover when probing the TS
  15731. // segment to retrieve timing information. Since the probe only looks at the media's
  15732. // times (e.g., PTS and DTS values of the segment), and doesn't consider the
  15733. // player's time (e.g., player.currentTime()), baseStartTime should reflect the
  15734. // media time as well. transmuxedDecodeEnd represents the end time of a segment, in
  15735. // seconds of media time, so should be used here. The previous segment is used since
  15736. // the end of the previous segment should represent the beginning of the current
  15737. // segment, so long as they are on the same timeline.
  15738. if (previousSegment.videoTimingInfo) {
  15739. simpleSegment.baseStartTime = previousSegment.videoTimingInfo.transmuxedDecodeEnd;
  15740. } else if (previousSegment.audioTimingInfo) {
  15741. simpleSegment.baseStartTime = previousSegment.audioTimingInfo.transmuxedDecodeEnd;
  15742. }
  15743. }
  15744. if (segment.key) {
  15745. // if the media sequence is greater than 2^32, the IV will be incorrect
  15746. // assuming 10s segments, that would be about 1300 years
  15747. const iv = segment.key.iv || new Uint32Array([0, 0, 0, segmentInfo.mediaIndex + segmentInfo.playlist.mediaSequence]);
  15748. simpleSegment.key = this.segmentKey(segment.key);
  15749. simpleSegment.key.iv = iv;
  15750. }
  15751. if (segment.map) {
  15752. simpleSegment.map = this.initSegmentForMap(segment.map);
  15753. }
  15754. return simpleSegment;
  15755. }
  15756. saveTransferStats_(stats) {
  15757. // every request counts as a media request even if it has been aborted
  15758. // or canceled due to a timeout
  15759. this.mediaRequests += 1;
  15760. if (stats) {
  15761. this.mediaBytesTransferred += stats.bytesReceived;
  15762. this.mediaTransferDuration += stats.roundTripTime;
  15763. }
  15764. }
  15765. saveBandwidthRelatedStats_(duration, stats) {
  15766. // byteLength will be used for throughput, and should be based on bytes receieved,
  15767. // which we only know at the end of the request and should reflect total bytes
  15768. // downloaded rather than just bytes processed from components of the segment
  15769. this.pendingSegment_.byteLength = stats.bytesReceived;
  15770. if (duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
  15771. this.logger_(`Ignoring segment's bandwidth because its duration of ${duration}` + ` is less than the min to record ${MIN_SEGMENT_DURATION_TO_SAVE_STATS}`);
  15772. return;
  15773. }
  15774. this.bandwidth = stats.bandwidth;
  15775. this.roundTrip = stats.roundTripTime;
  15776. }
  15777. handleTimeout_() {
  15778. // although the VTT segment loader bandwidth isn't really used, it's good to
  15779. // maintain functinality between segment loaders
  15780. this.mediaRequestsTimedout += 1;
  15781. this.bandwidth = 1;
  15782. this.roundTrip = NaN;
  15783. this.trigger('bandwidthupdate');
  15784. this.trigger('timeout');
  15785. }
  15786. /**
  15787. * Handle the callback from the segmentRequest function and set the
  15788. * associated SegmentLoader state and errors if necessary
  15789. *
  15790. * @private
  15791. */
  15792. segmentRequestFinished_(error, simpleSegment, result) {
  15793. // TODO handle special cases, e.g., muxed audio/video but only audio in the segment
  15794. // check the call queue directly since this function doesn't need to deal with any
  15795. // data, and can continue even if the source buffers are not set up and we didn't get
  15796. // any data from the segment
  15797. if (this.callQueue_.length) {
  15798. this.callQueue_.push(this.segmentRequestFinished_.bind(this, error, simpleSegment, result));
  15799. return;
  15800. }
  15801. this.saveTransferStats_(simpleSegment.stats); // The request was aborted and the SegmentLoader has already been reset
  15802. if (!this.pendingSegment_) {
  15803. return;
  15804. } // the request was aborted and the SegmentLoader has already started
  15805. // another request. this can happen when the timeout for an aborted
  15806. // request triggers due to a limitation in the XHR library
  15807. // do not count this as any sort of request or we risk double-counting
  15808. if (simpleSegment.requestId !== this.pendingSegment_.requestId) {
  15809. return;
  15810. } // an error occurred from the active pendingSegment_ so reset everything
  15811. if (error) {
  15812. this.pendingSegment_ = null;
  15813. this.state = 'READY'; // aborts are not a true error condition and nothing corrective needs to be done
  15814. if (error.code === REQUEST_ERRORS.ABORTED) {
  15815. return;
  15816. }
  15817. this.pause(); // the error is really just that at least one of the requests timed-out
  15818. // set the bandwidth to a very low value and trigger an ABR switch to
  15819. // take emergency action
  15820. if (error.code === REQUEST_ERRORS.TIMEOUT) {
  15821. this.handleTimeout_();
  15822. return;
  15823. } // if control-flow has arrived here, then the error is real
  15824. // emit an error event to exclude the current playlist
  15825. this.mediaRequestsErrored += 1;
  15826. this.error(error);
  15827. this.trigger('error');
  15828. return;
  15829. }
  15830. const segmentInfo = this.pendingSegment_; // the response was a success so set any bandwidth stats the request
  15831. // generated for ABR purposes
  15832. this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats);
  15833. segmentInfo.endOfAllRequests = simpleSegment.endOfAllRequests;
  15834. if (result.gopInfo) {
  15835. this.gopBuffer_ = updateGopBuffer(this.gopBuffer_, result.gopInfo, this.safeAppend_);
  15836. } // Although we may have already started appending on progress, we shouldn't switch the
  15837. // state away from loading until we are officially done loading the segment data.
  15838. this.state = 'APPENDING'; // used for testing
  15839. this.trigger('appending');
  15840. this.waitForAppendsToComplete_(segmentInfo);
  15841. }
  15842. setTimeMapping_(timeline) {
  15843. const timelineMapping = this.syncController_.mappingForTimeline(timeline);
  15844. if (timelineMapping !== null) {
  15845. this.timeMapping_ = timelineMapping;
  15846. }
  15847. }
  15848. updateMediaSecondsLoaded_(segment) {
  15849. if (typeof segment.start === 'number' && typeof segment.end === 'number') {
  15850. this.mediaSecondsLoaded += segment.end - segment.start;
  15851. } else {
  15852. this.mediaSecondsLoaded += segment.duration;
  15853. }
  15854. }
  15855. shouldUpdateTransmuxerTimestampOffset_(timestampOffset) {
  15856. if (timestampOffset === null) {
  15857. return false;
  15858. } // note that we're potentially using the same timestamp offset for both video and
  15859. // audio
  15860. if (this.loaderType_ === 'main' && timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
  15861. return true;
  15862. }
  15863. if (!this.audioDisabled_ && timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
  15864. return true;
  15865. }
  15866. return false;
  15867. }
  15868. trueSegmentStart_({
  15869. currentStart,
  15870. playlist,
  15871. mediaIndex,
  15872. firstVideoFrameTimeForData,
  15873. currentVideoTimestampOffset,
  15874. useVideoTimingInfo,
  15875. videoTimingInfo,
  15876. audioTimingInfo
  15877. }) {
  15878. if (typeof currentStart !== 'undefined') {
  15879. // if start was set once, keep using it
  15880. return currentStart;
  15881. }
  15882. if (!useVideoTimingInfo) {
  15883. return audioTimingInfo.start;
  15884. }
  15885. const previousSegment = playlist.segments[mediaIndex - 1]; // The start of a segment should be the start of the first full frame contained
  15886. // within that segment. Since the transmuxer maintains a cache of incomplete data
  15887. // from and/or the last frame seen, the start time may reflect a frame that starts
  15888. // in the previous segment. Check for that case and ensure the start time is
  15889. // accurate for the segment.
  15890. if (mediaIndex === 0 || !previousSegment || typeof previousSegment.start === 'undefined' || previousSegment.end !== firstVideoFrameTimeForData + currentVideoTimestampOffset) {
  15891. return firstVideoFrameTimeForData;
  15892. }
  15893. return videoTimingInfo.start;
  15894. }
  15895. waitForAppendsToComplete_(segmentInfo) {
  15896. const trackInfo = this.getCurrentMediaInfo_(segmentInfo);
  15897. if (!trackInfo) {
  15898. this.error({
  15899. message: 'No starting media returned, likely due to an unsupported media format.',
  15900. playlistExclusionDuration: Infinity
  15901. });
  15902. this.trigger('error');
  15903. return;
  15904. } // Although transmuxing is done, appends may not yet be finished. Throw a marker
  15905. // on each queue this loader is responsible for to ensure that the appends are
  15906. // complete.
  15907. const {
  15908. hasAudio,
  15909. hasVideo,
  15910. isMuxed
  15911. } = trackInfo;
  15912. const waitForVideo = this.loaderType_ === 'main' && hasVideo;
  15913. const waitForAudio = !this.audioDisabled_ && hasAudio && !isMuxed;
  15914. segmentInfo.waitingOnAppends = 0; // segments with no data
  15915. if (!segmentInfo.hasAppendedData_) {
  15916. if (!segmentInfo.timingInfo && typeof segmentInfo.timestampOffset === 'number') {
  15917. // When there's no audio or video data in the segment, there's no audio or video
  15918. // timing information.
  15919. //
  15920. // If there's no audio or video timing information, then the timestamp offset
  15921. // can't be adjusted to the appropriate value for the transmuxer and source
  15922. // buffers.
  15923. //
  15924. // Therefore, the next segment should be used to set the timestamp offset.
  15925. this.isPendingTimestampOffset_ = true;
  15926. } // override settings for metadata only segments
  15927. segmentInfo.timingInfo = {
  15928. start: 0
  15929. };
  15930. segmentInfo.waitingOnAppends++;
  15931. if (!this.isPendingTimestampOffset_) {
  15932. // update the timestampoffset
  15933. this.updateSourceBufferTimestampOffset_(segmentInfo); // make sure the metadata queue is processed even though we have
  15934. // no video/audio data.
  15935. this.processMetadataQueue_();
  15936. } // append is "done" instantly with no data.
  15937. this.checkAppendsDone_(segmentInfo);
  15938. return;
  15939. } // Since source updater could call back synchronously, do the increments first.
  15940. if (waitForVideo) {
  15941. segmentInfo.waitingOnAppends++;
  15942. }
  15943. if (waitForAudio) {
  15944. segmentInfo.waitingOnAppends++;
  15945. }
  15946. if (waitForVideo) {
  15947. this.sourceUpdater_.videoQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
  15948. }
  15949. if (waitForAudio) {
  15950. this.sourceUpdater_.audioQueueCallback(this.checkAppendsDone_.bind(this, segmentInfo));
  15951. }
  15952. }
  15953. checkAppendsDone_(segmentInfo) {
  15954. if (this.checkForAbort_(segmentInfo.requestId)) {
  15955. return;
  15956. }
  15957. segmentInfo.waitingOnAppends--;
  15958. if (segmentInfo.waitingOnAppends === 0) {
  15959. this.handleAppendsDone_();
  15960. }
  15961. }
  15962. checkForIllegalMediaSwitch(trackInfo) {
  15963. const illegalMediaSwitchError = illegalMediaSwitch(this.loaderType_, this.getCurrentMediaInfo_(), trackInfo);
  15964. if (illegalMediaSwitchError) {
  15965. this.error({
  15966. message: illegalMediaSwitchError,
  15967. playlistExclusionDuration: Infinity
  15968. });
  15969. this.trigger('error');
  15970. return true;
  15971. }
  15972. return false;
  15973. }
  15974. updateSourceBufferTimestampOffset_(segmentInfo) {
  15975. if (segmentInfo.timestampOffset === null || // we don't yet have the start for whatever media type (video or audio) has
  15976. // priority, timing-wise, so we must wait
  15977. typeof segmentInfo.timingInfo.start !== 'number' || // already updated the timestamp offset for this segment
  15978. segmentInfo.changedTimestampOffset || // the alt audio loader should not be responsible for setting the timestamp offset
  15979. this.loaderType_ !== 'main') {
  15980. return;
  15981. }
  15982. let didChange = false; // Primary timing goes by video, and audio is trimmed in the transmuxer, meaning that
  15983. // the timing info here comes from video. In the event that the audio is longer than
  15984. // the video, this will trim the start of the audio.
  15985. // This also trims any offset from 0 at the beginning of the media
  15986. segmentInfo.timestampOffset -= this.getSegmentStartTimeForTimestampOffsetCalculation_({
  15987. videoTimingInfo: segmentInfo.segment.videoTimingInfo,
  15988. audioTimingInfo: segmentInfo.segment.audioTimingInfo,
  15989. timingInfo: segmentInfo.timingInfo
  15990. }); // In the event that there are part segment downloads, each will try to update the
  15991. // timestamp offset. Retaining this bit of state prevents us from updating in the
  15992. // future (within the same segment), however, there may be a better way to handle it.
  15993. segmentInfo.changedTimestampOffset = true;
  15994. if (segmentInfo.timestampOffset !== this.sourceUpdater_.videoTimestampOffset()) {
  15995. this.sourceUpdater_.videoTimestampOffset(segmentInfo.timestampOffset);
  15996. didChange = true;
  15997. }
  15998. if (segmentInfo.timestampOffset !== this.sourceUpdater_.audioTimestampOffset()) {
  15999. this.sourceUpdater_.audioTimestampOffset(segmentInfo.timestampOffset);
  16000. didChange = true;
  16001. }
  16002. if (didChange) {
  16003. this.trigger('timestampoffset');
  16004. }
  16005. }
  16006. getSegmentStartTimeForTimestampOffsetCalculation_({
  16007. videoTimingInfo,
  16008. audioTimingInfo,
  16009. timingInfo
  16010. }) {
  16011. if (!this.useDtsForTimestampOffset_) {
  16012. return timingInfo.start;
  16013. }
  16014. if (videoTimingInfo && typeof videoTimingInfo.transmuxedDecodeStart === 'number') {
  16015. return videoTimingInfo.transmuxedDecodeStart;
  16016. } // handle audio only
  16017. if (audioTimingInfo && typeof audioTimingInfo.transmuxedDecodeStart === 'number') {
  16018. return audioTimingInfo.transmuxedDecodeStart;
  16019. } // handle content not transmuxed (e.g., MP4)
  16020. return timingInfo.start;
  16021. }
  16022. updateTimingInfoEnd_(segmentInfo) {
  16023. segmentInfo.timingInfo = segmentInfo.timingInfo || {};
  16024. const trackInfo = this.getMediaInfo_();
  16025. const useVideoTimingInfo = this.loaderType_ === 'main' && trackInfo && trackInfo.hasVideo;
  16026. const prioritizedTimingInfo = useVideoTimingInfo && segmentInfo.videoTimingInfo ? segmentInfo.videoTimingInfo : segmentInfo.audioTimingInfo;
  16027. if (!prioritizedTimingInfo) {
  16028. return;
  16029. }
  16030. segmentInfo.timingInfo.end = typeof prioritizedTimingInfo.end === 'number' ? // End time may not exist in a case where we aren't parsing the full segment (one
  16031. // current example is the case of fmp4), so use the rough duration to calculate an
  16032. // end time.
  16033. prioritizedTimingInfo.end : prioritizedTimingInfo.start + segmentInfo.duration;
  16034. }
  16035. /**
  16036. * callback to run when appendBuffer is finished. detects if we are
  16037. * in a good state to do things with the data we got, or if we need
  16038. * to wait for more
  16039. *
  16040. * @private
  16041. */
  16042. handleAppendsDone_() {
  16043. // appendsdone can cause an abort
  16044. if (this.pendingSegment_) {
  16045. this.trigger('appendsdone');
  16046. }
  16047. if (!this.pendingSegment_) {
  16048. this.state = 'READY'; // TODO should this move into this.checkForAbort to speed up requests post abort in
  16049. // all appending cases?
  16050. if (!this.paused()) {
  16051. this.monitorBuffer_();
  16052. }
  16053. return;
  16054. }
  16055. const segmentInfo = this.pendingSegment_; // Now that the end of the segment has been reached, we can set the end time. It's
  16056. // best to wait until all appends are done so we're sure that the primary media is
  16057. // finished (and we have its end time).
  16058. this.updateTimingInfoEnd_(segmentInfo);
  16059. if (this.shouldSaveSegmentTimingInfo_) {
  16060. // Timeline mappings should only be saved for the main loader. This is for multiple
  16061. // reasons:
  16062. //
  16063. // 1) Only one mapping is saved per timeline, meaning that if both the audio loader
  16064. // and the main loader try to save the timeline mapping, whichever comes later
  16065. // will overwrite the first. In theory this is OK, as the mappings should be the
  16066. // same, however, it breaks for (2)
  16067. // 2) In the event of a live stream, the initial live point will make for a somewhat
  16068. // arbitrary mapping. If audio and video streams are not perfectly in-sync, then
  16069. // the mapping will be off for one of the streams, dependent on which one was
  16070. // first saved (see (1)).
  16071. // 3) Primary timing goes by video in VHS, so the mapping should be video.
  16072. //
  16073. // Since the audio loader will wait for the main loader to load the first segment,
  16074. // the main loader will save the first timeline mapping, and ensure that there won't
  16075. // be a case where audio loads two segments without saving a mapping (thus leading
  16076. // to missing segment timing info).
  16077. this.syncController_.saveSegmentTimingInfo({
  16078. segmentInfo,
  16079. shouldSaveTimelineMapping: this.loaderType_ === 'main'
  16080. });
  16081. }
  16082. const segmentDurationMessage = getTroublesomeSegmentDurationMessage(segmentInfo, this.sourceType_);
  16083. if (segmentDurationMessage) {
  16084. if (segmentDurationMessage.severity === 'warn') {
  16085. videojs__default["default"].log.warn(segmentDurationMessage.message);
  16086. } else {
  16087. this.logger_(segmentDurationMessage.message);
  16088. }
  16089. }
  16090. this.recordThroughput_(segmentInfo);
  16091. this.pendingSegment_ = null;
  16092. this.state = 'READY';
  16093. if (segmentInfo.isSyncRequest) {
  16094. this.trigger('syncinfoupdate'); // if the sync request was not appended
  16095. // then it was not the correct segment.
  16096. // throw it away and use the data it gave us
  16097. // to get the correct one.
  16098. if (!segmentInfo.hasAppendedData_) {
  16099. this.logger_(`Throwing away un-appended sync request ${segmentInfoString(segmentInfo)}`);
  16100. return;
  16101. }
  16102. }
  16103. this.logger_(`Appended ${segmentInfoString(segmentInfo)}`);
  16104. this.addSegmentMetadataCue_(segmentInfo);
  16105. this.fetchAtBuffer_ = true;
  16106. if (this.currentTimeline_ !== segmentInfo.timeline) {
  16107. this.timelineChangeController_.lastTimelineChange({
  16108. type: this.loaderType_,
  16109. from: this.currentTimeline_,
  16110. to: segmentInfo.timeline
  16111. }); // If audio is not disabled, the main segment loader is responsible for updating
  16112. // the audio timeline as well. If the content is video only, this won't have any
  16113. // impact.
  16114. if (this.loaderType_ === 'main' && !this.audioDisabled_) {
  16115. this.timelineChangeController_.lastTimelineChange({
  16116. type: 'audio',
  16117. from: this.currentTimeline_,
  16118. to: segmentInfo.timeline
  16119. });
  16120. }
  16121. }
  16122. this.currentTimeline_ = segmentInfo.timeline; // We must update the syncinfo to recalculate the seekable range before
  16123. // the following conditional otherwise it may consider this a bad "guess"
  16124. // and attempt to resync when the post-update seekable window and live
  16125. // point would mean that this was the perfect segment to fetch
  16126. this.trigger('syncinfoupdate');
  16127. const segment = segmentInfo.segment;
  16128. const part = segmentInfo.part;
  16129. const badSegmentGuess = segment.end && this.currentTime_() - segment.end > segmentInfo.playlist.targetDuration * 3;
  16130. const badPartGuess = part && part.end && this.currentTime_() - part.end > segmentInfo.playlist.partTargetDuration * 3; // If we previously appended a segment/part that ends more than 3 part/targetDurations before
  16131. // the currentTime_ that means that our conservative guess was too conservative.
  16132. // In that case, reset the loader state so that we try to use any information gained
  16133. // from the previous request to create a new, more accurate, sync-point.
  16134. if (badSegmentGuess || badPartGuess) {
  16135. this.logger_(`bad ${badSegmentGuess ? 'segment' : 'part'} ${segmentInfoString(segmentInfo)}`);
  16136. this.resetEverything();
  16137. return;
  16138. }
  16139. const isWalkingForward = this.mediaIndex !== null; // Don't do a rendition switch unless we have enough time to get a sync segment
  16140. // and conservatively guess
  16141. if (isWalkingForward) {
  16142. this.trigger('bandwidthupdate');
  16143. }
  16144. this.trigger('progress');
  16145. this.mediaIndex = segmentInfo.mediaIndex;
  16146. this.partIndex = segmentInfo.partIndex; // any time an update finishes and the last segment is in the
  16147. // buffer, end the stream. this ensures the "ended" event will
  16148. // fire if playback reaches that point.
  16149. if (this.isEndOfStream_(segmentInfo.mediaIndex, segmentInfo.playlist, segmentInfo.partIndex)) {
  16150. this.endOfStream();
  16151. } // used for testing
  16152. this.trigger('appended');
  16153. if (segmentInfo.hasAppendedData_) {
  16154. this.mediaAppends++;
  16155. }
  16156. if (!this.paused()) {
  16157. this.monitorBuffer_();
  16158. }
  16159. }
  16160. /**
  16161. * Records the current throughput of the decrypt, transmux, and append
  16162. * portion of the semgment pipeline. `throughput.rate` is a the cumulative
  16163. * moving average of the throughput. `throughput.count` is the number of
  16164. * data points in the average.
  16165. *
  16166. * @private
  16167. * @param {Object} segmentInfo the object returned by loadSegment
  16168. */
  16169. recordThroughput_(segmentInfo) {
  16170. if (segmentInfo.duration < MIN_SEGMENT_DURATION_TO_SAVE_STATS) {
  16171. this.logger_(`Ignoring segment's throughput because its duration of ${segmentInfo.duration}` + ` is less than the min to record ${MIN_SEGMENT_DURATION_TO_SAVE_STATS}`);
  16172. return;
  16173. }
  16174. const rate = this.throughput.rate; // Add one to the time to ensure that we don't accidentally attempt to divide
  16175. // by zero in the case where the throughput is ridiculously high
  16176. const segmentProcessingTime = Date.now() - segmentInfo.endOfAllRequests + 1; // Multiply by 8000 to convert from bytes/millisecond to bits/second
  16177. const segmentProcessingThroughput = Math.floor(segmentInfo.byteLength / segmentProcessingTime * 8 * 1000); // This is just a cumulative moving average calculation:
  16178. // newAvg = oldAvg + (sample - oldAvg) / (sampleCount + 1)
  16179. this.throughput.rate += (segmentProcessingThroughput - rate) / ++this.throughput.count;
  16180. }
  16181. /**
  16182. * Adds a cue to the segment-metadata track with some metadata information about the
  16183. * segment
  16184. *
  16185. * @private
  16186. * @param {Object} segmentInfo
  16187. * the object returned by loadSegment
  16188. * @method addSegmentMetadataCue_
  16189. */
  16190. addSegmentMetadataCue_(segmentInfo) {
  16191. if (!this.segmentMetadataTrack_) {
  16192. return;
  16193. }
  16194. const segment = segmentInfo.segment;
  16195. const start = segment.start;
  16196. const end = segment.end; // Do not try adding the cue if the start and end times are invalid.
  16197. if (!finite(start) || !finite(end)) {
  16198. return;
  16199. }
  16200. removeCuesFromTrack(start, end, this.segmentMetadataTrack_);
  16201. const Cue = window__default["default"].WebKitDataCue || window__default["default"].VTTCue;
  16202. const value = {
  16203. custom: segment.custom,
  16204. dateTimeObject: segment.dateTimeObject,
  16205. dateTimeString: segment.dateTimeString,
  16206. programDateTime: segment.programDateTime,
  16207. bandwidth: segmentInfo.playlist.attributes.BANDWIDTH,
  16208. resolution: segmentInfo.playlist.attributes.RESOLUTION,
  16209. codecs: segmentInfo.playlist.attributes.CODECS,
  16210. byteLength: segmentInfo.byteLength,
  16211. uri: segmentInfo.uri,
  16212. timeline: segmentInfo.timeline,
  16213. playlist: segmentInfo.playlist.id,
  16214. start,
  16215. end
  16216. };
  16217. const data = JSON.stringify(value);
  16218. const cue = new Cue(start, end, data); // Attach the metadata to the value property of the cue to keep consistency between
  16219. // the differences of WebKitDataCue in safari and VTTCue in other browsers
  16220. cue.value = value;
  16221. this.segmentMetadataTrack_.addCue(cue);
  16222. }
  16223. }
  16224. function noop() {}
  16225. const toTitleCase = function (string) {
  16226. if (typeof string !== 'string') {
  16227. return string;
  16228. }
  16229. return string.replace(/./, w => w.toUpperCase());
  16230. };
  16231. /**
  16232. * @file source-updater.js
  16233. */
  16234. const bufferTypes = ['video', 'audio'];
  16235. const updating = (type, sourceUpdater) => {
  16236. const sourceBuffer = sourceUpdater[`${type}Buffer`];
  16237. return sourceBuffer && sourceBuffer.updating || sourceUpdater.queuePending[type];
  16238. };
  16239. const nextQueueIndexOfType = (type, queue) => {
  16240. for (let i = 0; i < queue.length; i++) {
  16241. const queueEntry = queue[i];
  16242. if (queueEntry.type === 'mediaSource') {
  16243. // If the next entry is a media source entry (uses multiple source buffers), block
  16244. // processing to allow it to go through first.
  16245. return null;
  16246. }
  16247. if (queueEntry.type === type) {
  16248. return i;
  16249. }
  16250. }
  16251. return null;
  16252. };
  16253. const shiftQueue = (type, sourceUpdater) => {
  16254. if (sourceUpdater.queue.length === 0) {
  16255. return;
  16256. }
  16257. let queueIndex = 0;
  16258. let queueEntry = sourceUpdater.queue[queueIndex];
  16259. if (queueEntry.type === 'mediaSource') {
  16260. if (!sourceUpdater.updating() && sourceUpdater.mediaSource.readyState !== 'closed') {
  16261. sourceUpdater.queue.shift();
  16262. queueEntry.action(sourceUpdater);
  16263. if (queueEntry.doneFn) {
  16264. queueEntry.doneFn();
  16265. } // Only specific source buffer actions must wait for async updateend events. Media
  16266. // Source actions process synchronously. Therefore, both audio and video source
  16267. // buffers are now clear to process the next queue entries.
  16268. shiftQueue('audio', sourceUpdater);
  16269. shiftQueue('video', sourceUpdater);
  16270. } // Media Source actions require both source buffers, so if the media source action
  16271. // couldn't process yet (because one or both source buffers are busy), block other
  16272. // queue actions until both are available and the media source action can process.
  16273. return;
  16274. }
  16275. if (type === 'mediaSource') {
  16276. // If the queue was shifted by a media source action (this happens when pushing a
  16277. // media source action onto the queue), then it wasn't from an updateend event from an
  16278. // audio or video source buffer, so there's no change from previous state, and no
  16279. // processing should be done.
  16280. return;
  16281. } // Media source queue entries don't need to consider whether the source updater is
  16282. // started (i.e., source buffers are created) as they don't need the source buffers, but
  16283. // source buffer queue entries do.
  16284. if (!sourceUpdater.ready() || sourceUpdater.mediaSource.readyState === 'closed' || updating(type, sourceUpdater)) {
  16285. return;
  16286. }
  16287. if (queueEntry.type !== type) {
  16288. queueIndex = nextQueueIndexOfType(type, sourceUpdater.queue);
  16289. if (queueIndex === null) {
  16290. // Either there's no queue entry that uses this source buffer type in the queue, or
  16291. // there's a media source queue entry before the next entry of this type, in which
  16292. // case wait for that action to process first.
  16293. return;
  16294. }
  16295. queueEntry = sourceUpdater.queue[queueIndex];
  16296. }
  16297. sourceUpdater.queue.splice(queueIndex, 1); // Keep a record that this source buffer type is in use.
  16298. //
  16299. // The queue pending operation must be set before the action is performed in the event
  16300. // that the action results in a synchronous event that is acted upon. For instance, if
  16301. // an exception is thrown that can be handled, it's possible that new actions will be
  16302. // appended to an empty queue and immediately executed, but would not have the correct
  16303. // pending information if this property was set after the action was performed.
  16304. sourceUpdater.queuePending[type] = queueEntry;
  16305. queueEntry.action(type, sourceUpdater);
  16306. if (!queueEntry.doneFn) {
  16307. // synchronous operation, process next entry
  16308. sourceUpdater.queuePending[type] = null;
  16309. shiftQueue(type, sourceUpdater);
  16310. return;
  16311. }
  16312. };
  16313. const cleanupBuffer = (type, sourceUpdater) => {
  16314. const buffer = sourceUpdater[`${type}Buffer`];
  16315. const titleType = toTitleCase(type);
  16316. if (!buffer) {
  16317. return;
  16318. }
  16319. buffer.removeEventListener('updateend', sourceUpdater[`on${titleType}UpdateEnd_`]);
  16320. buffer.removeEventListener('error', sourceUpdater[`on${titleType}Error_`]);
  16321. sourceUpdater.codecs[type] = null;
  16322. sourceUpdater[`${type}Buffer`] = null;
  16323. };
  16324. const inSourceBuffers = (mediaSource, sourceBuffer) => mediaSource && sourceBuffer && Array.prototype.indexOf.call(mediaSource.sourceBuffers, sourceBuffer) !== -1;
  16325. const actions = {
  16326. appendBuffer: (bytes, segmentInfo, onError) => (type, sourceUpdater) => {
  16327. const sourceBuffer = sourceUpdater[`${type}Buffer`]; // can't do anything if the media source / source buffer is null
  16328. // or the media source does not contain this source buffer.
  16329. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16330. return;
  16331. }
  16332. sourceUpdater.logger_(`Appending segment ${segmentInfo.mediaIndex}'s ${bytes.length} bytes to ${type}Buffer`);
  16333. try {
  16334. sourceBuffer.appendBuffer(bytes);
  16335. } catch (e) {
  16336. sourceUpdater.logger_(`Error with code ${e.code} ` + (e.code === QUOTA_EXCEEDED_ERR ? '(QUOTA_EXCEEDED_ERR) ' : '') + `when appending segment ${segmentInfo.mediaIndex} to ${type}Buffer`);
  16337. sourceUpdater.queuePending[type] = null;
  16338. onError(e);
  16339. }
  16340. },
  16341. remove: (start, end) => (type, sourceUpdater) => {
  16342. const sourceBuffer = sourceUpdater[`${type}Buffer`]; // can't do anything if the media source / source buffer is null
  16343. // or the media source does not contain this source buffer.
  16344. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16345. return;
  16346. }
  16347. sourceUpdater.logger_(`Removing ${start} to ${end} from ${type}Buffer`);
  16348. try {
  16349. sourceBuffer.remove(start, end);
  16350. } catch (e) {
  16351. sourceUpdater.logger_(`Remove ${start} to ${end} from ${type}Buffer failed`);
  16352. }
  16353. },
  16354. timestampOffset: offset => (type, sourceUpdater) => {
  16355. const sourceBuffer = sourceUpdater[`${type}Buffer`]; // can't do anything if the media source / source buffer is null
  16356. // or the media source does not contain this source buffer.
  16357. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16358. return;
  16359. }
  16360. sourceUpdater.logger_(`Setting ${type}timestampOffset to ${offset}`);
  16361. sourceBuffer.timestampOffset = offset;
  16362. },
  16363. callback: callback => (type, sourceUpdater) => {
  16364. callback();
  16365. },
  16366. endOfStream: error => sourceUpdater => {
  16367. if (sourceUpdater.mediaSource.readyState !== 'open') {
  16368. return;
  16369. }
  16370. sourceUpdater.logger_(`Calling mediaSource endOfStream(${error || ''})`);
  16371. try {
  16372. sourceUpdater.mediaSource.endOfStream(error);
  16373. } catch (e) {
  16374. videojs__default["default"].log.warn('Failed to call media source endOfStream', e);
  16375. }
  16376. },
  16377. duration: duration => sourceUpdater => {
  16378. sourceUpdater.logger_(`Setting mediaSource duration to ${duration}`);
  16379. try {
  16380. sourceUpdater.mediaSource.duration = duration;
  16381. } catch (e) {
  16382. videojs__default["default"].log.warn('Failed to set media source duration', e);
  16383. }
  16384. },
  16385. abort: () => (type, sourceUpdater) => {
  16386. if (sourceUpdater.mediaSource.readyState !== 'open') {
  16387. return;
  16388. }
  16389. const sourceBuffer = sourceUpdater[`${type}Buffer`]; // can't do anything if the media source / source buffer is null
  16390. // or the media source does not contain this source buffer.
  16391. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16392. return;
  16393. }
  16394. sourceUpdater.logger_(`calling abort on ${type}Buffer`);
  16395. try {
  16396. sourceBuffer.abort();
  16397. } catch (e) {
  16398. videojs__default["default"].log.warn(`Failed to abort on ${type}Buffer`, e);
  16399. }
  16400. },
  16401. addSourceBuffer: (type, codec) => sourceUpdater => {
  16402. const titleType = toTitleCase(type);
  16403. const mime = codecs_js.getMimeForCodec(codec);
  16404. sourceUpdater.logger_(`Adding ${type}Buffer with codec ${codec} to mediaSource`);
  16405. const sourceBuffer = sourceUpdater.mediaSource.addSourceBuffer(mime);
  16406. sourceBuffer.addEventListener('updateend', sourceUpdater[`on${titleType}UpdateEnd_`]);
  16407. sourceBuffer.addEventListener('error', sourceUpdater[`on${titleType}Error_`]);
  16408. sourceUpdater.codecs[type] = codec;
  16409. sourceUpdater[`${type}Buffer`] = sourceBuffer;
  16410. },
  16411. removeSourceBuffer: type => sourceUpdater => {
  16412. const sourceBuffer = sourceUpdater[`${type}Buffer`];
  16413. cleanupBuffer(type, sourceUpdater); // can't do anything if the media source / source buffer is null
  16414. // or the media source does not contain this source buffer.
  16415. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16416. return;
  16417. }
  16418. sourceUpdater.logger_(`Removing ${type}Buffer with codec ${sourceUpdater.codecs[type]} from mediaSource`);
  16419. try {
  16420. sourceUpdater.mediaSource.removeSourceBuffer(sourceBuffer);
  16421. } catch (e) {
  16422. videojs__default["default"].log.warn(`Failed to removeSourceBuffer ${type}Buffer`, e);
  16423. }
  16424. },
  16425. changeType: codec => (type, sourceUpdater) => {
  16426. const sourceBuffer = sourceUpdater[`${type}Buffer`];
  16427. const mime = codecs_js.getMimeForCodec(codec); // can't do anything if the media source / source buffer is null
  16428. // or the media source does not contain this source buffer.
  16429. if (!inSourceBuffers(sourceUpdater.mediaSource, sourceBuffer)) {
  16430. return;
  16431. } // do not update codec if we don't need to.
  16432. if (sourceUpdater.codecs[type] === codec) {
  16433. return;
  16434. }
  16435. sourceUpdater.logger_(`changing ${type}Buffer codec from ${sourceUpdater.codecs[type]} to ${codec}`); // check if change to the provided type is supported
  16436. try {
  16437. sourceBuffer.changeType(mime);
  16438. sourceUpdater.codecs[type] = codec;
  16439. } catch (e) {
  16440. videojs__default["default"].log.warn(`Failed to changeType on ${type}Buffer`, e);
  16441. }
  16442. }
  16443. };
  16444. const pushQueue = ({
  16445. type,
  16446. sourceUpdater,
  16447. action,
  16448. doneFn,
  16449. name
  16450. }) => {
  16451. sourceUpdater.queue.push({
  16452. type,
  16453. action,
  16454. doneFn,
  16455. name
  16456. });
  16457. shiftQueue(type, sourceUpdater);
  16458. };
  16459. const onUpdateend = (type, sourceUpdater) => e => {
  16460. // Although there should, in theory, be a pending action for any updateend receieved,
  16461. // there are some actions that may trigger updateend events without set definitions in
  16462. // the w3c spec. For instance, setting the duration on the media source may trigger
  16463. // updateend events on source buffers. This does not appear to be in the spec. As such,
  16464. // if we encounter an updateend without a corresponding pending action from our queue
  16465. // for that source buffer type, process the next action.
  16466. if (sourceUpdater.queuePending[type]) {
  16467. const doneFn = sourceUpdater.queuePending[type].doneFn;
  16468. sourceUpdater.queuePending[type] = null;
  16469. if (doneFn) {
  16470. // if there's an error, report it
  16471. doneFn(sourceUpdater[`${type}Error_`]);
  16472. }
  16473. }
  16474. shiftQueue(type, sourceUpdater);
  16475. };
  16476. /**
  16477. * A queue of callbacks to be serialized and applied when a
  16478. * MediaSource and its associated SourceBuffers are not in the
  16479. * updating state. It is used by the segment loader to update the
  16480. * underlying SourceBuffers when new data is loaded, for instance.
  16481. *
  16482. * @class SourceUpdater
  16483. * @param {MediaSource} mediaSource the MediaSource to create the SourceBuffer from
  16484. * @param {string} mimeType the desired MIME type of the underlying SourceBuffer
  16485. */
  16486. class SourceUpdater extends videojs__default["default"].EventTarget {
  16487. constructor(mediaSource) {
  16488. super();
  16489. this.mediaSource = mediaSource;
  16490. this.sourceopenListener_ = () => shiftQueue('mediaSource', this);
  16491. this.mediaSource.addEventListener('sourceopen', this.sourceopenListener_);
  16492. this.logger_ = logger('SourceUpdater'); // initial timestamp offset is 0
  16493. this.audioTimestampOffset_ = 0;
  16494. this.videoTimestampOffset_ = 0;
  16495. this.queue = [];
  16496. this.queuePending = {
  16497. audio: null,
  16498. video: null
  16499. };
  16500. this.delayedAudioAppendQueue_ = [];
  16501. this.videoAppendQueued_ = false;
  16502. this.codecs = {};
  16503. this.onVideoUpdateEnd_ = onUpdateend('video', this);
  16504. this.onAudioUpdateEnd_ = onUpdateend('audio', this);
  16505. this.onVideoError_ = e => {
  16506. // used for debugging
  16507. this.videoError_ = e;
  16508. };
  16509. this.onAudioError_ = e => {
  16510. // used for debugging
  16511. this.audioError_ = e;
  16512. };
  16513. this.createdSourceBuffers_ = false;
  16514. this.initializedEme_ = false;
  16515. this.triggeredReady_ = false;
  16516. }
  16517. initializedEme() {
  16518. this.initializedEme_ = true;
  16519. this.triggerReady();
  16520. }
  16521. hasCreatedSourceBuffers() {
  16522. // if false, likely waiting on one of the segment loaders to get enough data to create
  16523. // source buffers
  16524. return this.createdSourceBuffers_;
  16525. }
  16526. hasInitializedAnyEme() {
  16527. return this.initializedEme_;
  16528. }
  16529. ready() {
  16530. return this.hasCreatedSourceBuffers() && this.hasInitializedAnyEme();
  16531. }
  16532. createSourceBuffers(codecs) {
  16533. if (this.hasCreatedSourceBuffers()) {
  16534. // already created them before
  16535. return;
  16536. } // the intial addOrChangeSourceBuffers will always be
  16537. // two add buffers.
  16538. this.addOrChangeSourceBuffers(codecs);
  16539. this.createdSourceBuffers_ = true;
  16540. this.trigger('createdsourcebuffers');
  16541. this.triggerReady();
  16542. }
  16543. triggerReady() {
  16544. // only allow ready to be triggered once, this prevents the case
  16545. // where:
  16546. // 1. we trigger createdsourcebuffers
  16547. // 2. ie 11 synchronously initializates eme
  16548. // 3. the synchronous initialization causes us to trigger ready
  16549. // 4. We go back to the ready check in createSourceBuffers and ready is triggered again.
  16550. if (this.ready() && !this.triggeredReady_) {
  16551. this.triggeredReady_ = true;
  16552. this.trigger('ready');
  16553. }
  16554. }
  16555. /**
  16556. * Add a type of source buffer to the media source.
  16557. *
  16558. * @param {string} type
  16559. * The type of source buffer to add.
  16560. *
  16561. * @param {string} codec
  16562. * The codec to add the source buffer with.
  16563. */
  16564. addSourceBuffer(type, codec) {
  16565. pushQueue({
  16566. type: 'mediaSource',
  16567. sourceUpdater: this,
  16568. action: actions.addSourceBuffer(type, codec),
  16569. name: 'addSourceBuffer'
  16570. });
  16571. }
  16572. /**
  16573. * call abort on a source buffer.
  16574. *
  16575. * @param {string} type
  16576. * The type of source buffer to call abort on.
  16577. */
  16578. abort(type) {
  16579. pushQueue({
  16580. type,
  16581. sourceUpdater: this,
  16582. action: actions.abort(type),
  16583. name: 'abort'
  16584. });
  16585. }
  16586. /**
  16587. * Call removeSourceBuffer and remove a specific type
  16588. * of source buffer on the mediaSource.
  16589. *
  16590. * @param {string} type
  16591. * The type of source buffer to remove.
  16592. */
  16593. removeSourceBuffer(type) {
  16594. if (!this.canRemoveSourceBuffer()) {
  16595. videojs__default["default"].log.error('removeSourceBuffer is not supported!');
  16596. return;
  16597. }
  16598. pushQueue({
  16599. type: 'mediaSource',
  16600. sourceUpdater: this,
  16601. action: actions.removeSourceBuffer(type),
  16602. name: 'removeSourceBuffer'
  16603. });
  16604. }
  16605. /**
  16606. * Whether or not the removeSourceBuffer function is supported
  16607. * on the mediaSource.
  16608. *
  16609. * @return {boolean}
  16610. * if removeSourceBuffer can be called.
  16611. */
  16612. canRemoveSourceBuffer() {
  16613. // As of Firefox 83 removeSourceBuffer
  16614. // throws errors, so we report that it does not support this.
  16615. return !videojs__default["default"].browser.IS_FIREFOX && window__default["default"].MediaSource && window__default["default"].MediaSource.prototype && typeof window__default["default"].MediaSource.prototype.removeSourceBuffer === 'function';
  16616. }
  16617. /**
  16618. * Whether or not the changeType function is supported
  16619. * on our SourceBuffers.
  16620. *
  16621. * @return {boolean}
  16622. * if changeType can be called.
  16623. */
  16624. static canChangeType() {
  16625. return window__default["default"].SourceBuffer && window__default["default"].SourceBuffer.prototype && typeof window__default["default"].SourceBuffer.prototype.changeType === 'function';
  16626. }
  16627. /**
  16628. * Whether or not the changeType function is supported
  16629. * on our SourceBuffers.
  16630. *
  16631. * @return {boolean}
  16632. * if changeType can be called.
  16633. */
  16634. canChangeType() {
  16635. return this.constructor.canChangeType();
  16636. }
  16637. /**
  16638. * Call the changeType function on a source buffer, given the code and type.
  16639. *
  16640. * @param {string} type
  16641. * The type of source buffer to call changeType on.
  16642. *
  16643. * @param {string} codec
  16644. * The codec string to change type with on the source buffer.
  16645. */
  16646. changeType(type, codec) {
  16647. if (!this.canChangeType()) {
  16648. videojs__default["default"].log.error('changeType is not supported!');
  16649. return;
  16650. }
  16651. pushQueue({
  16652. type,
  16653. sourceUpdater: this,
  16654. action: actions.changeType(codec),
  16655. name: 'changeType'
  16656. });
  16657. }
  16658. /**
  16659. * Add source buffers with a codec or, if they are already created,
  16660. * call changeType on source buffers using changeType.
  16661. *
  16662. * @param {Object} codecs
  16663. * Codecs to switch to
  16664. */
  16665. addOrChangeSourceBuffers(codecs) {
  16666. if (!codecs || typeof codecs !== 'object' || Object.keys(codecs).length === 0) {
  16667. throw new Error('Cannot addOrChangeSourceBuffers to undefined codecs');
  16668. }
  16669. Object.keys(codecs).forEach(type => {
  16670. const codec = codecs[type];
  16671. if (!this.hasCreatedSourceBuffers()) {
  16672. return this.addSourceBuffer(type, codec);
  16673. }
  16674. if (this.canChangeType()) {
  16675. this.changeType(type, codec);
  16676. }
  16677. });
  16678. }
  16679. /**
  16680. * Queue an update to append an ArrayBuffer.
  16681. *
  16682. * @param {MediaObject} object containing audioBytes and/or videoBytes
  16683. * @param {Function} done the function to call when done
  16684. * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-appendBuffer-void-ArrayBuffer-data
  16685. */
  16686. appendBuffer(options, doneFn) {
  16687. const {
  16688. segmentInfo,
  16689. type,
  16690. bytes
  16691. } = options;
  16692. this.processedAppend_ = true;
  16693. if (type === 'audio' && this.videoBuffer && !this.videoAppendQueued_) {
  16694. this.delayedAudioAppendQueue_.push([options, doneFn]);
  16695. this.logger_(`delayed audio append of ${bytes.length} until video append`);
  16696. return;
  16697. } // In the case of certain errors, for instance, QUOTA_EXCEEDED_ERR, updateend will
  16698. // not be fired. This means that the queue will be blocked until the next action
  16699. // taken by the segment-loader. Provide a mechanism for segment-loader to handle
  16700. // these errors by calling the doneFn with the specific error.
  16701. const onError = doneFn;
  16702. pushQueue({
  16703. type,
  16704. sourceUpdater: this,
  16705. action: actions.appendBuffer(bytes, segmentInfo || {
  16706. mediaIndex: -1
  16707. }, onError),
  16708. doneFn,
  16709. name: 'appendBuffer'
  16710. });
  16711. if (type === 'video') {
  16712. this.videoAppendQueued_ = true;
  16713. if (!this.delayedAudioAppendQueue_.length) {
  16714. return;
  16715. }
  16716. const queue = this.delayedAudioAppendQueue_.slice();
  16717. this.logger_(`queuing delayed audio ${queue.length} appendBuffers`);
  16718. this.delayedAudioAppendQueue_.length = 0;
  16719. queue.forEach(que => {
  16720. this.appendBuffer.apply(this, que);
  16721. });
  16722. }
  16723. }
  16724. /**
  16725. * Get the audio buffer's buffered timerange.
  16726. *
  16727. * @return {TimeRange}
  16728. * The audio buffer's buffered time range
  16729. */
  16730. audioBuffered() {
  16731. // no media source/source buffer or it isn't in the media sources
  16732. // source buffer list
  16733. if (!inSourceBuffers(this.mediaSource, this.audioBuffer)) {
  16734. return createTimeRanges();
  16735. }
  16736. return this.audioBuffer.buffered ? this.audioBuffer.buffered : createTimeRanges();
  16737. }
  16738. /**
  16739. * Get the video buffer's buffered timerange.
  16740. *
  16741. * @return {TimeRange}
  16742. * The video buffer's buffered time range
  16743. */
  16744. videoBuffered() {
  16745. // no media source/source buffer or it isn't in the media sources
  16746. // source buffer list
  16747. if (!inSourceBuffers(this.mediaSource, this.videoBuffer)) {
  16748. return createTimeRanges();
  16749. }
  16750. return this.videoBuffer.buffered ? this.videoBuffer.buffered : createTimeRanges();
  16751. }
  16752. /**
  16753. * Get a combined video/audio buffer's buffered timerange.
  16754. *
  16755. * @return {TimeRange}
  16756. * the combined time range
  16757. */
  16758. buffered() {
  16759. const video = inSourceBuffers(this.mediaSource, this.videoBuffer) ? this.videoBuffer : null;
  16760. const audio = inSourceBuffers(this.mediaSource, this.audioBuffer) ? this.audioBuffer : null;
  16761. if (audio && !video) {
  16762. return this.audioBuffered();
  16763. }
  16764. if (video && !audio) {
  16765. return this.videoBuffered();
  16766. }
  16767. return bufferIntersection(this.audioBuffered(), this.videoBuffered());
  16768. }
  16769. /**
  16770. * Add a callback to the queue that will set duration on the mediaSource.
  16771. *
  16772. * @param {number} duration
  16773. * The duration to set
  16774. *
  16775. * @param {Function} [doneFn]
  16776. * function to run after duration has been set.
  16777. */
  16778. setDuration(duration, doneFn = noop) {
  16779. // In order to set the duration on the media source, it's necessary to wait for all
  16780. // source buffers to no longer be updating. "If the updating attribute equals true on
  16781. // any SourceBuffer in sourceBuffers, then throw an InvalidStateError exception and
  16782. // abort these steps." (source: https://www.w3.org/TR/media-source/#attributes).
  16783. pushQueue({
  16784. type: 'mediaSource',
  16785. sourceUpdater: this,
  16786. action: actions.duration(duration),
  16787. name: 'duration',
  16788. doneFn
  16789. });
  16790. }
  16791. /**
  16792. * Add a mediaSource endOfStream call to the queue
  16793. *
  16794. * @param {Error} [error]
  16795. * Call endOfStream with an error
  16796. *
  16797. * @param {Function} [doneFn]
  16798. * A function that should be called when the
  16799. * endOfStream call has finished.
  16800. */
  16801. endOfStream(error = null, doneFn = noop) {
  16802. if (typeof error !== 'string') {
  16803. error = undefined;
  16804. } // In order to set the duration on the media source, it's necessary to wait for all
  16805. // source buffers to no longer be updating. "If the updating attribute equals true on
  16806. // any SourceBuffer in sourceBuffers, then throw an InvalidStateError exception and
  16807. // abort these steps." (source: https://www.w3.org/TR/media-source/#attributes).
  16808. pushQueue({
  16809. type: 'mediaSource',
  16810. sourceUpdater: this,
  16811. action: actions.endOfStream(error),
  16812. name: 'endOfStream',
  16813. doneFn
  16814. });
  16815. }
  16816. /**
  16817. * Queue an update to remove a time range from the buffer.
  16818. *
  16819. * @param {number} start where to start the removal
  16820. * @param {number} end where to end the removal
  16821. * @param {Function} [done=noop] optional callback to be executed when the remove
  16822. * operation is complete
  16823. * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-remove-void-double-start-unrestricted-double-end
  16824. */
  16825. removeAudio(start, end, done = noop) {
  16826. if (!this.audioBuffered().length || this.audioBuffered().end(0) === 0) {
  16827. done();
  16828. return;
  16829. }
  16830. pushQueue({
  16831. type: 'audio',
  16832. sourceUpdater: this,
  16833. action: actions.remove(start, end),
  16834. doneFn: done,
  16835. name: 'remove'
  16836. });
  16837. }
  16838. /**
  16839. * Queue an update to remove a time range from the buffer.
  16840. *
  16841. * @param {number} start where to start the removal
  16842. * @param {number} end where to end the removal
  16843. * @param {Function} [done=noop] optional callback to be executed when the remove
  16844. * operation is complete
  16845. * @see http://www.w3.org/TR/media-source/#widl-SourceBuffer-remove-void-double-start-unrestricted-double-end
  16846. */
  16847. removeVideo(start, end, done = noop) {
  16848. if (!this.videoBuffered().length || this.videoBuffered().end(0) === 0) {
  16849. done();
  16850. return;
  16851. }
  16852. pushQueue({
  16853. type: 'video',
  16854. sourceUpdater: this,
  16855. action: actions.remove(start, end),
  16856. doneFn: done,
  16857. name: 'remove'
  16858. });
  16859. }
  16860. /**
  16861. * Whether the underlying sourceBuffer is updating or not
  16862. *
  16863. * @return {boolean} the updating status of the SourceBuffer
  16864. */
  16865. updating() {
  16866. // the audio/video source buffer is updating
  16867. if (updating('audio', this) || updating('video', this)) {
  16868. return true;
  16869. }
  16870. return false;
  16871. }
  16872. /**
  16873. * Set/get the timestampoffset on the audio SourceBuffer
  16874. *
  16875. * @return {number} the timestamp offset
  16876. */
  16877. audioTimestampOffset(offset) {
  16878. if (typeof offset !== 'undefined' && this.audioBuffer && // no point in updating if it's the same
  16879. this.audioTimestampOffset_ !== offset) {
  16880. pushQueue({
  16881. type: 'audio',
  16882. sourceUpdater: this,
  16883. action: actions.timestampOffset(offset),
  16884. name: 'timestampOffset'
  16885. });
  16886. this.audioTimestampOffset_ = offset;
  16887. }
  16888. return this.audioTimestampOffset_;
  16889. }
  16890. /**
  16891. * Set/get the timestampoffset on the video SourceBuffer
  16892. *
  16893. * @return {number} the timestamp offset
  16894. */
  16895. videoTimestampOffset(offset) {
  16896. if (typeof offset !== 'undefined' && this.videoBuffer && // no point in updating if it's the same
  16897. this.videoTimestampOffset !== offset) {
  16898. pushQueue({
  16899. type: 'video',
  16900. sourceUpdater: this,
  16901. action: actions.timestampOffset(offset),
  16902. name: 'timestampOffset'
  16903. });
  16904. this.videoTimestampOffset_ = offset;
  16905. }
  16906. return this.videoTimestampOffset_;
  16907. }
  16908. /**
  16909. * Add a function to the queue that will be called
  16910. * when it is its turn to run in the audio queue.
  16911. *
  16912. * @param {Function} callback
  16913. * The callback to queue.
  16914. */
  16915. audioQueueCallback(callback) {
  16916. if (!this.audioBuffer) {
  16917. return;
  16918. }
  16919. pushQueue({
  16920. type: 'audio',
  16921. sourceUpdater: this,
  16922. action: actions.callback(callback),
  16923. name: 'callback'
  16924. });
  16925. }
  16926. /**
  16927. * Add a function to the queue that will be called
  16928. * when it is its turn to run in the video queue.
  16929. *
  16930. * @param {Function} callback
  16931. * The callback to queue.
  16932. */
  16933. videoQueueCallback(callback) {
  16934. if (!this.videoBuffer) {
  16935. return;
  16936. }
  16937. pushQueue({
  16938. type: 'video',
  16939. sourceUpdater: this,
  16940. action: actions.callback(callback),
  16941. name: 'callback'
  16942. });
  16943. }
  16944. /**
  16945. * dispose of the source updater and the underlying sourceBuffer
  16946. */
  16947. dispose() {
  16948. this.trigger('dispose');
  16949. bufferTypes.forEach(type => {
  16950. this.abort(type);
  16951. if (this.canRemoveSourceBuffer()) {
  16952. this.removeSourceBuffer(type);
  16953. } else {
  16954. this[`${type}QueueCallback`](() => cleanupBuffer(type, this));
  16955. }
  16956. });
  16957. this.videoAppendQueued_ = false;
  16958. this.delayedAudioAppendQueue_.length = 0;
  16959. if (this.sourceopenListener_) {
  16960. this.mediaSource.removeEventListener('sourceopen', this.sourceopenListener_);
  16961. }
  16962. this.off();
  16963. }
  16964. }
  16965. const uint8ToUtf8 = uintArray => decodeURIComponent(escape(String.fromCharCode.apply(null, uintArray)));
  16966. const bufferToHexString = buffer => {
  16967. const uInt8Buffer = new Uint8Array(buffer);
  16968. return Array.from(uInt8Buffer).map(byte => byte.toString(16).padStart(2, '0')).join('');
  16969. };
  16970. /**
  16971. * @file vtt-segment-loader.js
  16972. */
  16973. const VTT_LINE_TERMINATORS = new Uint8Array('\n\n'.split('').map(char => char.charCodeAt(0)));
  16974. class NoVttJsError extends Error {
  16975. constructor() {
  16976. super('Trying to parse received VTT cues, but there is no WebVTT. Make sure vtt.js is loaded.');
  16977. }
  16978. }
  16979. /**
  16980. * An object that manages segment loading and appending.
  16981. *
  16982. * @class VTTSegmentLoader
  16983. * @param {Object} options required and optional options
  16984. * @extends videojs.EventTarget
  16985. */
  16986. class VTTSegmentLoader extends SegmentLoader {
  16987. constructor(settings, options = {}) {
  16988. super(settings, options); // SegmentLoader requires a MediaSource be specified or it will throw an error;
  16989. // however, VTTSegmentLoader has no need of a media source, so delete the reference
  16990. this.mediaSource_ = null;
  16991. this.subtitlesTrack_ = null;
  16992. this.loaderType_ = 'subtitle';
  16993. this.featuresNativeTextTracks_ = settings.featuresNativeTextTracks;
  16994. this.loadVttJs = settings.loadVttJs; // The VTT segment will have its own time mappings. Saving VTT segment timing info in
  16995. // the sync controller leads to improper behavior.
  16996. this.shouldSaveSegmentTimingInfo_ = false;
  16997. }
  16998. createTransmuxer_() {
  16999. // don't need to transmux any subtitles
  17000. return null;
  17001. }
  17002. /**
  17003. * Indicates which time ranges are buffered
  17004. *
  17005. * @return {TimeRange}
  17006. * TimeRange object representing the current buffered ranges
  17007. */
  17008. buffered_() {
  17009. if (!this.subtitlesTrack_ || !this.subtitlesTrack_.cues || !this.subtitlesTrack_.cues.length) {
  17010. return createTimeRanges();
  17011. }
  17012. const cues = this.subtitlesTrack_.cues;
  17013. const start = cues[0].startTime;
  17014. const end = cues[cues.length - 1].startTime;
  17015. return createTimeRanges([[start, end]]);
  17016. }
  17017. /**
  17018. * Gets and sets init segment for the provided map
  17019. *
  17020. * @param {Object} map
  17021. * The map object representing the init segment to get or set
  17022. * @param {boolean=} set
  17023. * If true, the init segment for the provided map should be saved
  17024. * @return {Object}
  17025. * map object for desired init segment
  17026. */
  17027. initSegmentForMap(map, set = false) {
  17028. if (!map) {
  17029. return null;
  17030. }
  17031. const id = initSegmentId(map);
  17032. let storedMap = this.initSegments_[id];
  17033. if (set && !storedMap && map.bytes) {
  17034. // append WebVTT line terminators to the media initialization segment if it exists
  17035. // to follow the WebVTT spec (https://w3c.github.io/webvtt/#file-structure) that
  17036. // requires two or more WebVTT line terminators between the WebVTT header and the
  17037. // rest of the file
  17038. const combinedByteLength = VTT_LINE_TERMINATORS.byteLength + map.bytes.byteLength;
  17039. const combinedSegment = new Uint8Array(combinedByteLength);
  17040. combinedSegment.set(map.bytes);
  17041. combinedSegment.set(VTT_LINE_TERMINATORS, map.bytes.byteLength);
  17042. this.initSegments_[id] = storedMap = {
  17043. resolvedUri: map.resolvedUri,
  17044. byterange: map.byterange,
  17045. bytes: combinedSegment
  17046. };
  17047. }
  17048. return storedMap || map;
  17049. }
  17050. /**
  17051. * Returns true if all configuration required for loading is present, otherwise false.
  17052. *
  17053. * @return {boolean} True if the all configuration is ready for loading
  17054. * @private
  17055. */
  17056. couldBeginLoading_() {
  17057. return this.playlist_ && this.subtitlesTrack_ && !this.paused();
  17058. }
  17059. /**
  17060. * Once all the starting parameters have been specified, begin
  17061. * operation. This method should only be invoked from the INIT
  17062. * state.
  17063. *
  17064. * @private
  17065. */
  17066. init_() {
  17067. this.state = 'READY';
  17068. this.resetEverything();
  17069. return this.monitorBuffer_();
  17070. }
  17071. /**
  17072. * Set a subtitle track on the segment loader to add subtitles to
  17073. *
  17074. * @param {TextTrack=} track
  17075. * The text track to add loaded subtitles to
  17076. * @return {TextTrack}
  17077. * Returns the subtitles track
  17078. */
  17079. track(track) {
  17080. if (typeof track === 'undefined') {
  17081. return this.subtitlesTrack_;
  17082. }
  17083. this.subtitlesTrack_ = track; // if we were unpaused but waiting for a sourceUpdater, start
  17084. // buffering now
  17085. if (this.state === 'INIT' && this.couldBeginLoading_()) {
  17086. this.init_();
  17087. }
  17088. return this.subtitlesTrack_;
  17089. }
  17090. /**
  17091. * Remove any data in the source buffer between start and end times
  17092. *
  17093. * @param {number} start - the start time of the region to remove from the buffer
  17094. * @param {number} end - the end time of the region to remove from the buffer
  17095. */
  17096. remove(start, end) {
  17097. removeCuesFromTrack(start, end, this.subtitlesTrack_);
  17098. }
  17099. /**
  17100. * fill the buffer with segements unless the sourceBuffers are
  17101. * currently updating
  17102. *
  17103. * Note: this function should only ever be called by monitorBuffer_
  17104. * and never directly
  17105. *
  17106. * @private
  17107. */
  17108. fillBuffer_() {
  17109. // see if we need to begin loading immediately
  17110. const segmentInfo = this.chooseNextRequest_();
  17111. if (!segmentInfo) {
  17112. return;
  17113. }
  17114. if (this.syncController_.timestampOffsetForTimeline(segmentInfo.timeline) === null) {
  17115. // We don't have the timestamp offset that we need to sync subtitles.
  17116. // Rerun on a timestamp offset or user interaction.
  17117. const checkTimestampOffset = () => {
  17118. this.state = 'READY';
  17119. if (!this.paused()) {
  17120. // if not paused, queue a buffer check as soon as possible
  17121. this.monitorBuffer_();
  17122. }
  17123. };
  17124. this.syncController_.one('timestampoffset', checkTimestampOffset);
  17125. this.state = 'WAITING_ON_TIMELINE';
  17126. return;
  17127. }
  17128. this.loadSegment_(segmentInfo);
  17129. } // never set a timestamp offset for vtt segments.
  17130. timestampOffsetForSegment_() {
  17131. return null;
  17132. }
  17133. chooseNextRequest_() {
  17134. return this.skipEmptySegments_(super.chooseNextRequest_());
  17135. }
  17136. /**
  17137. * Prevents the segment loader from requesting segments we know contain no subtitles
  17138. * by walking forward until we find the next segment that we don't know whether it is
  17139. * empty or not.
  17140. *
  17141. * @param {Object} segmentInfo
  17142. * a segment info object that describes the current segment
  17143. * @return {Object}
  17144. * a segment info object that describes the current segment
  17145. */
  17146. skipEmptySegments_(segmentInfo) {
  17147. while (segmentInfo && segmentInfo.segment.empty) {
  17148. // stop at the last possible segmentInfo
  17149. if (segmentInfo.mediaIndex + 1 >= segmentInfo.playlist.segments.length) {
  17150. segmentInfo = null;
  17151. break;
  17152. }
  17153. segmentInfo = this.generateSegmentInfo_({
  17154. playlist: segmentInfo.playlist,
  17155. mediaIndex: segmentInfo.mediaIndex + 1,
  17156. startOfSegment: segmentInfo.startOfSegment + segmentInfo.duration,
  17157. isSyncRequest: segmentInfo.isSyncRequest
  17158. });
  17159. }
  17160. return segmentInfo;
  17161. }
  17162. stopForError(error) {
  17163. this.error(error);
  17164. this.state = 'READY';
  17165. this.pause();
  17166. this.trigger('error');
  17167. }
  17168. /**
  17169. * append a decrypted segement to the SourceBuffer through a SourceUpdater
  17170. *
  17171. * @private
  17172. */
  17173. segmentRequestFinished_(error, simpleSegment, result) {
  17174. if (!this.subtitlesTrack_) {
  17175. this.state = 'READY';
  17176. return;
  17177. }
  17178. this.saveTransferStats_(simpleSegment.stats); // the request was aborted
  17179. if (!this.pendingSegment_) {
  17180. this.state = 'READY';
  17181. this.mediaRequestsAborted += 1;
  17182. return;
  17183. }
  17184. if (error) {
  17185. if (error.code === REQUEST_ERRORS.TIMEOUT) {
  17186. this.handleTimeout_();
  17187. }
  17188. if (error.code === REQUEST_ERRORS.ABORTED) {
  17189. this.mediaRequestsAborted += 1;
  17190. } else {
  17191. this.mediaRequestsErrored += 1;
  17192. }
  17193. this.stopForError(error);
  17194. return;
  17195. }
  17196. const segmentInfo = this.pendingSegment_; // although the VTT segment loader bandwidth isn't really used, it's good to
  17197. // maintain functionality between segment loaders
  17198. this.saveBandwidthRelatedStats_(segmentInfo.duration, simpleSegment.stats); // if this request included a segment key, save that data in the cache
  17199. if (simpleSegment.key) {
  17200. this.segmentKey(simpleSegment.key, true);
  17201. }
  17202. this.state = 'APPENDING'; // used for tests
  17203. this.trigger('appending');
  17204. const segment = segmentInfo.segment;
  17205. if (segment.map) {
  17206. segment.map.bytes = simpleSegment.map.bytes;
  17207. }
  17208. segmentInfo.bytes = simpleSegment.bytes; // Make sure that vttjs has loaded, otherwise, load it and wait till it finished loading
  17209. if (typeof window__default["default"].WebVTT !== 'function' && typeof this.loadVttJs === 'function') {
  17210. this.state = 'WAITING_ON_VTTJS'; // should be fine to call multiple times
  17211. // script will be loaded once but multiple listeners will be added to the queue, which is expected.
  17212. this.loadVttJs().then(() => this.segmentRequestFinished_(error, simpleSegment, result), () => this.stopForError({
  17213. message: 'Error loading vtt.js'
  17214. }));
  17215. return;
  17216. }
  17217. segment.requested = true;
  17218. try {
  17219. this.parseVTTCues_(segmentInfo);
  17220. } catch (e) {
  17221. this.stopForError({
  17222. message: e.message
  17223. });
  17224. return;
  17225. }
  17226. this.updateTimeMapping_(segmentInfo, this.syncController_.timelines[segmentInfo.timeline], this.playlist_);
  17227. if (segmentInfo.cues.length) {
  17228. segmentInfo.timingInfo = {
  17229. start: segmentInfo.cues[0].startTime,
  17230. end: segmentInfo.cues[segmentInfo.cues.length - 1].endTime
  17231. };
  17232. } else {
  17233. segmentInfo.timingInfo = {
  17234. start: segmentInfo.startOfSegment,
  17235. end: segmentInfo.startOfSegment + segmentInfo.duration
  17236. };
  17237. }
  17238. if (segmentInfo.isSyncRequest) {
  17239. this.trigger('syncinfoupdate');
  17240. this.pendingSegment_ = null;
  17241. this.state = 'READY';
  17242. return;
  17243. }
  17244. segmentInfo.byteLength = segmentInfo.bytes.byteLength;
  17245. this.mediaSecondsLoaded += segment.duration; // Create VTTCue instances for each cue in the new segment and add them to
  17246. // the subtitle track
  17247. segmentInfo.cues.forEach(cue => {
  17248. this.subtitlesTrack_.addCue(this.featuresNativeTextTracks_ ? new window__default["default"].VTTCue(cue.startTime, cue.endTime, cue.text) : cue);
  17249. }); // Remove any duplicate cues from the subtitle track. The WebVTT spec allows
  17250. // cues to have identical time-intervals, but if the text is also identical
  17251. // we can safely assume it is a duplicate that can be removed (ex. when a cue
  17252. // "overlaps" VTT segments)
  17253. removeDuplicateCuesFromTrack(this.subtitlesTrack_);
  17254. this.handleAppendsDone_();
  17255. }
  17256. handleData_() {// noop as we shouldn't be getting video/audio data captions
  17257. // that we do not support here.
  17258. }
  17259. updateTimingInfoEnd_() {// noop
  17260. }
  17261. /**
  17262. * Uses the WebVTT parser to parse the segment response
  17263. *
  17264. * @throws NoVttJsError
  17265. *
  17266. * @param {Object} segmentInfo
  17267. * a segment info object that describes the current segment
  17268. * @private
  17269. */
  17270. parseVTTCues_(segmentInfo) {
  17271. let decoder;
  17272. let decodeBytesToString = false;
  17273. if (typeof window__default["default"].WebVTT !== 'function') {
  17274. // caller is responsible for exception handling.
  17275. throw new NoVttJsError();
  17276. }
  17277. if (typeof window__default["default"].TextDecoder === 'function') {
  17278. decoder = new window__default["default"].TextDecoder('utf8');
  17279. } else {
  17280. decoder = window__default["default"].WebVTT.StringDecoder();
  17281. decodeBytesToString = true;
  17282. }
  17283. const parser = new window__default["default"].WebVTT.Parser(window__default["default"], window__default["default"].vttjs, decoder);
  17284. segmentInfo.cues = [];
  17285. segmentInfo.timestampmap = {
  17286. MPEGTS: 0,
  17287. LOCAL: 0
  17288. };
  17289. parser.oncue = segmentInfo.cues.push.bind(segmentInfo.cues);
  17290. parser.ontimestampmap = map => {
  17291. segmentInfo.timestampmap = map;
  17292. };
  17293. parser.onparsingerror = error => {
  17294. videojs__default["default"].log.warn('Error encountered when parsing cues: ' + error.message);
  17295. };
  17296. if (segmentInfo.segment.map) {
  17297. let mapData = segmentInfo.segment.map.bytes;
  17298. if (decodeBytesToString) {
  17299. mapData = uint8ToUtf8(mapData);
  17300. }
  17301. parser.parse(mapData);
  17302. }
  17303. let segmentData = segmentInfo.bytes;
  17304. if (decodeBytesToString) {
  17305. segmentData = uint8ToUtf8(segmentData);
  17306. }
  17307. parser.parse(segmentData);
  17308. parser.flush();
  17309. }
  17310. /**
  17311. * Updates the start and end times of any cues parsed by the WebVTT parser using
  17312. * the information parsed from the X-TIMESTAMP-MAP header and a TS to media time mapping
  17313. * from the SyncController
  17314. *
  17315. * @param {Object} segmentInfo
  17316. * a segment info object that describes the current segment
  17317. * @param {Object} mappingObj
  17318. * object containing a mapping from TS to media time
  17319. * @param {Object} playlist
  17320. * the playlist object containing the segment
  17321. * @private
  17322. */
  17323. updateTimeMapping_(segmentInfo, mappingObj, playlist) {
  17324. const segment = segmentInfo.segment;
  17325. if (!mappingObj) {
  17326. // If the sync controller does not have a mapping of TS to Media Time for the
  17327. // timeline, then we don't have enough information to update the cue
  17328. // start/end times
  17329. return;
  17330. }
  17331. if (!segmentInfo.cues.length) {
  17332. // If there are no cues, we also do not have enough information to figure out
  17333. // segment timing. Mark that the segment contains no cues so we don't re-request
  17334. // an empty segment.
  17335. segment.empty = true;
  17336. return;
  17337. }
  17338. const {
  17339. MPEGTS,
  17340. LOCAL
  17341. } = segmentInfo.timestampmap;
  17342. /**
  17343. * From the spec:
  17344. * The MPEGTS media timestamp MUST use a 90KHz timescale,
  17345. * even when non-WebVTT Media Segments use a different timescale.
  17346. */
  17347. const mpegTsInSeconds = MPEGTS / clock.ONE_SECOND_IN_TS;
  17348. const diff = mpegTsInSeconds - LOCAL + mappingObj.mapping;
  17349. segmentInfo.cues.forEach(cue => {
  17350. const duration = cue.endTime - cue.startTime;
  17351. const startTime = MPEGTS === 0 ? cue.startTime + diff : this.handleRollover_(cue.startTime + diff, mappingObj.time);
  17352. cue.startTime = Math.max(startTime, 0);
  17353. cue.endTime = Math.max(startTime + duration, 0);
  17354. });
  17355. if (!playlist.syncInfo) {
  17356. const firstStart = segmentInfo.cues[0].startTime;
  17357. const lastStart = segmentInfo.cues[segmentInfo.cues.length - 1].startTime;
  17358. playlist.syncInfo = {
  17359. mediaSequence: playlist.mediaSequence + segmentInfo.mediaIndex,
  17360. time: Math.min(firstStart, lastStart - segment.duration)
  17361. };
  17362. }
  17363. }
  17364. /**
  17365. * MPEG-TS PES timestamps are limited to 2^33.
  17366. * Once they reach 2^33, they roll over to 0.
  17367. * mux.js handles PES timestamp rollover for the following scenarios:
  17368. * [forward rollover(right)] ->
  17369. * PES timestamps monotonically increase, and once they reach 2^33, they roll over to 0
  17370. * [backward rollover(left)] -->
  17371. * we seek back to position before rollover.
  17372. *
  17373. * According to the HLS SPEC:
  17374. * When synchronizing WebVTT with PES timestamps, clients SHOULD account
  17375. * for cases where the 33-bit PES timestamps have wrapped and the WebVTT
  17376. * cue times have not. When the PES timestamp wraps, the WebVTT Segment
  17377. * SHOULD have a X-TIMESTAMP-MAP header that maps the current WebVTT
  17378. * time to the new (low valued) PES timestamp.
  17379. *
  17380. * So we want to handle rollover here and align VTT Cue start/end time to the player's time.
  17381. */
  17382. handleRollover_(value, reference) {
  17383. if (reference === null) {
  17384. return value;
  17385. }
  17386. let valueIn90khz = value * clock.ONE_SECOND_IN_TS;
  17387. const referenceIn90khz = reference * clock.ONE_SECOND_IN_TS;
  17388. let offset;
  17389. if (referenceIn90khz < valueIn90khz) {
  17390. // - 2^33
  17391. offset = -8589934592;
  17392. } else {
  17393. // + 2^33
  17394. offset = 8589934592;
  17395. } // distance(value - reference) > 2^32
  17396. while (Math.abs(valueIn90khz - referenceIn90khz) > 4294967296) {
  17397. valueIn90khz += offset;
  17398. }
  17399. return valueIn90khz / clock.ONE_SECOND_IN_TS;
  17400. }
  17401. }
  17402. /**
  17403. * @file ad-cue-tags.js
  17404. */
  17405. /**
  17406. * Searches for an ad cue that overlaps with the given mediaTime
  17407. *
  17408. * @param {Object} track
  17409. * the track to find the cue for
  17410. *
  17411. * @param {number} mediaTime
  17412. * the time to find the cue at
  17413. *
  17414. * @return {Object|null}
  17415. * the found cue or null
  17416. */
  17417. const findAdCue = function (track, mediaTime) {
  17418. const cues = track.cues;
  17419. for (let i = 0; i < cues.length; i++) {
  17420. const cue = cues[i];
  17421. if (mediaTime >= cue.adStartTime && mediaTime <= cue.adEndTime) {
  17422. return cue;
  17423. }
  17424. }
  17425. return null;
  17426. };
  17427. const updateAdCues = function (media, track, offset = 0) {
  17428. if (!media.segments) {
  17429. return;
  17430. }
  17431. let mediaTime = offset;
  17432. let cue;
  17433. for (let i = 0; i < media.segments.length; i++) {
  17434. const segment = media.segments[i];
  17435. if (!cue) {
  17436. // Since the cues will span for at least the segment duration, adding a fudge
  17437. // factor of half segment duration will prevent duplicate cues from being
  17438. // created when timing info is not exact (e.g. cue start time initialized
  17439. // at 10.006677, but next call mediaTime is 10.003332 )
  17440. cue = findAdCue(track, mediaTime + segment.duration / 2);
  17441. }
  17442. if (cue) {
  17443. if ('cueIn' in segment) {
  17444. // Found a CUE-IN so end the cue
  17445. cue.endTime = mediaTime;
  17446. cue.adEndTime = mediaTime;
  17447. mediaTime += segment.duration;
  17448. cue = null;
  17449. continue;
  17450. }
  17451. if (mediaTime < cue.endTime) {
  17452. // Already processed this mediaTime for this cue
  17453. mediaTime += segment.duration;
  17454. continue;
  17455. } // otherwise extend cue until a CUE-IN is found
  17456. cue.endTime += segment.duration;
  17457. } else {
  17458. if ('cueOut' in segment) {
  17459. cue = new window__default["default"].VTTCue(mediaTime, mediaTime + segment.duration, segment.cueOut);
  17460. cue.adStartTime = mediaTime; // Assumes tag format to be
  17461. // #EXT-X-CUE-OUT:30
  17462. cue.adEndTime = mediaTime + parseFloat(segment.cueOut);
  17463. track.addCue(cue);
  17464. }
  17465. if ('cueOutCont' in segment) {
  17466. // Entered into the middle of an ad cue
  17467. // Assumes tag formate to be
  17468. // #EXT-X-CUE-OUT-CONT:10/30
  17469. const [adOffset, adTotal] = segment.cueOutCont.split('/').map(parseFloat);
  17470. cue = new window__default["default"].VTTCue(mediaTime, mediaTime + segment.duration, '');
  17471. cue.adStartTime = mediaTime - adOffset;
  17472. cue.adEndTime = cue.adStartTime + adTotal;
  17473. track.addCue(cue);
  17474. }
  17475. }
  17476. mediaTime += segment.duration;
  17477. }
  17478. };
  17479. /**
  17480. * @file sync-controller.js
  17481. */
  17482. // synchronize expired playlist segments.
  17483. // the max media sequence diff is 48 hours of live stream
  17484. // content with two second segments. Anything larger than that
  17485. // will likely be invalid.
  17486. const MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC = 86400;
  17487. const syncPointStrategies = [// Stategy "VOD": Handle the VOD-case where the sync-point is *always*
  17488. // the equivalence display-time 0 === segment-index 0
  17489. {
  17490. name: 'VOD',
  17491. run: (syncController, playlist, duration, currentTimeline, currentTime) => {
  17492. if (duration !== Infinity) {
  17493. const syncPoint = {
  17494. time: 0,
  17495. segmentIndex: 0,
  17496. partIndex: null
  17497. };
  17498. return syncPoint;
  17499. }
  17500. return null;
  17501. }
  17502. }, {
  17503. name: 'MediaSequence',
  17504. /**
  17505. * run media sequence strategy
  17506. *
  17507. * @param {SyncController} syncController
  17508. * @param {Object} playlist
  17509. * @param {number} duration
  17510. * @param {number} currentTimeline
  17511. * @param {number} currentTime
  17512. * @param {string} type
  17513. */
  17514. run: (syncController, playlist, duration, currentTimeline, currentTime, type) => {
  17515. if (!type) {
  17516. return null;
  17517. }
  17518. const mediaSequenceMap = syncController.getMediaSequenceMap(type);
  17519. if (!mediaSequenceMap || mediaSequenceMap.size === 0) {
  17520. return null;
  17521. }
  17522. if (playlist.mediaSequence === undefined || !Array.isArray(playlist.segments) || !playlist.segments.length) {
  17523. return null;
  17524. }
  17525. let currentMediaSequence = playlist.mediaSequence;
  17526. let segmentIndex = 0;
  17527. for (const segment of playlist.segments) {
  17528. const range = mediaSequenceMap.get(currentMediaSequence);
  17529. if (!range) {
  17530. // unexpected case
  17531. // we expect this playlist to be the same playlist in the map
  17532. // just break from the loop and move forward to the next strategy
  17533. break;
  17534. }
  17535. if (currentTime >= range.start && currentTime < range.end) {
  17536. // we found segment
  17537. if (Array.isArray(segment.parts) && segment.parts.length) {
  17538. let currentPartStart = range.start;
  17539. let partIndex = 0;
  17540. for (const part of segment.parts) {
  17541. const start = currentPartStart;
  17542. const end = start + part.duration;
  17543. if (currentTime >= start && currentTime < end) {
  17544. return {
  17545. time: range.start,
  17546. segmentIndex,
  17547. partIndex
  17548. };
  17549. }
  17550. partIndex++;
  17551. currentPartStart = end;
  17552. }
  17553. } // no parts found, return sync point for segment
  17554. return {
  17555. time: range.start,
  17556. segmentIndex,
  17557. partIndex: null
  17558. };
  17559. }
  17560. segmentIndex++;
  17561. currentMediaSequence++;
  17562. } // we didn't find any segments for provided current time
  17563. return null;
  17564. }
  17565. }, // Stategy "ProgramDateTime": We have a program-date-time tag in this playlist
  17566. {
  17567. name: 'ProgramDateTime',
  17568. run: (syncController, playlist, duration, currentTimeline, currentTime) => {
  17569. if (!Object.keys(syncController.timelineToDatetimeMappings).length) {
  17570. return null;
  17571. }
  17572. let syncPoint = null;
  17573. let lastDistance = null;
  17574. const partsAndSegments = getPartsAndSegments(playlist);
  17575. currentTime = currentTime || 0;
  17576. for (let i = 0; i < partsAndSegments.length; i++) {
  17577. // start from the end and loop backwards for live
  17578. // or start from the front and loop forwards for non-live
  17579. const index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
  17580. const partAndSegment = partsAndSegments[index];
  17581. const segment = partAndSegment.segment;
  17582. const datetimeMapping = syncController.timelineToDatetimeMappings[segment.timeline];
  17583. if (!datetimeMapping || !segment.dateTimeObject) {
  17584. continue;
  17585. }
  17586. const segmentTime = segment.dateTimeObject.getTime() / 1000;
  17587. let start = segmentTime + datetimeMapping; // take part duration into account.
  17588. if (segment.parts && typeof partAndSegment.partIndex === 'number') {
  17589. for (let z = 0; z < partAndSegment.partIndex; z++) {
  17590. start += segment.parts[z].duration;
  17591. }
  17592. }
  17593. const distance = Math.abs(currentTime - start); // Once the distance begins to increase, or if distance is 0, we have passed
  17594. // currentTime and can stop looking for better candidates
  17595. if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
  17596. break;
  17597. }
  17598. lastDistance = distance;
  17599. syncPoint = {
  17600. time: start,
  17601. segmentIndex: partAndSegment.segmentIndex,
  17602. partIndex: partAndSegment.partIndex
  17603. };
  17604. }
  17605. return syncPoint;
  17606. }
  17607. }, // Stategy "Segment": We have a known time mapping for a timeline and a
  17608. // segment in the current timeline with timing data
  17609. {
  17610. name: 'Segment',
  17611. run: (syncController, playlist, duration, currentTimeline, currentTime) => {
  17612. let syncPoint = null;
  17613. let lastDistance = null;
  17614. currentTime = currentTime || 0;
  17615. const partsAndSegments = getPartsAndSegments(playlist);
  17616. for (let i = 0; i < partsAndSegments.length; i++) {
  17617. // start from the end and loop backwards for live
  17618. // or start from the front and loop forwards for non-live
  17619. const index = playlist.endList || currentTime === 0 ? i : partsAndSegments.length - (i + 1);
  17620. const partAndSegment = partsAndSegments[index];
  17621. const segment = partAndSegment.segment;
  17622. const start = partAndSegment.part && partAndSegment.part.start || segment && segment.start;
  17623. if (segment.timeline === currentTimeline && typeof start !== 'undefined') {
  17624. const distance = Math.abs(currentTime - start); // Once the distance begins to increase, we have passed
  17625. // currentTime and can stop looking for better candidates
  17626. if (lastDistance !== null && lastDistance < distance) {
  17627. break;
  17628. }
  17629. if (!syncPoint || lastDistance === null || lastDistance >= distance) {
  17630. lastDistance = distance;
  17631. syncPoint = {
  17632. time: start,
  17633. segmentIndex: partAndSegment.segmentIndex,
  17634. partIndex: partAndSegment.partIndex
  17635. };
  17636. }
  17637. }
  17638. }
  17639. return syncPoint;
  17640. }
  17641. }, // Stategy "Discontinuity": We have a discontinuity with a known
  17642. // display-time
  17643. {
  17644. name: 'Discontinuity',
  17645. run: (syncController, playlist, duration, currentTimeline, currentTime) => {
  17646. let syncPoint = null;
  17647. currentTime = currentTime || 0;
  17648. if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
  17649. let lastDistance = null;
  17650. for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
  17651. const segmentIndex = playlist.discontinuityStarts[i];
  17652. const discontinuity = playlist.discontinuitySequence + i + 1;
  17653. const discontinuitySync = syncController.discontinuities[discontinuity];
  17654. if (discontinuitySync) {
  17655. const distance = Math.abs(currentTime - discontinuitySync.time); // Once the distance begins to increase, we have passed
  17656. // currentTime and can stop looking for better candidates
  17657. if (lastDistance !== null && lastDistance < distance) {
  17658. break;
  17659. }
  17660. if (!syncPoint || lastDistance === null || lastDistance >= distance) {
  17661. lastDistance = distance;
  17662. syncPoint = {
  17663. time: discontinuitySync.time,
  17664. segmentIndex,
  17665. partIndex: null
  17666. };
  17667. }
  17668. }
  17669. }
  17670. }
  17671. return syncPoint;
  17672. }
  17673. }, // Stategy "Playlist": We have a playlist with a known mapping of
  17674. // segment index to display time
  17675. {
  17676. name: 'Playlist',
  17677. run: (syncController, playlist, duration, currentTimeline, currentTime) => {
  17678. if (playlist.syncInfo) {
  17679. const syncPoint = {
  17680. time: playlist.syncInfo.time,
  17681. segmentIndex: playlist.syncInfo.mediaSequence - playlist.mediaSequence,
  17682. partIndex: null
  17683. };
  17684. return syncPoint;
  17685. }
  17686. return null;
  17687. }
  17688. }];
  17689. class SyncController extends videojs__default["default"].EventTarget {
  17690. constructor(options = {}) {
  17691. super(); // ...for synching across variants
  17692. this.timelines = [];
  17693. this.discontinuities = [];
  17694. this.timelineToDatetimeMappings = {};
  17695. /**
  17696. * @type {Map<string, Map<number, { start: number, end: number }>>}
  17697. * @private
  17698. */
  17699. this.mediaSequenceStorage_ = new Map();
  17700. this.logger_ = logger('SyncController');
  17701. }
  17702. /**
  17703. * Get media sequence map by type
  17704. *
  17705. * @param {string} type - segment loader type
  17706. * @return {Map<number, { start: number, end: number }> | undefined}
  17707. */
  17708. getMediaSequenceMap(type) {
  17709. return this.mediaSequenceStorage_.get(type);
  17710. }
  17711. /**
  17712. * Update Media Sequence Map -> <MediaSequence, Range>
  17713. *
  17714. * @param {Object} playlist - parsed playlist
  17715. * @param {number} currentTime - current player's time
  17716. * @param {string} type - segment loader type
  17717. * @return {void}
  17718. */
  17719. updateMediaSequenceMap(playlist, currentTime, type) {
  17720. // we should not process this playlist if it does not have mediaSequence or segments
  17721. if (playlist.mediaSequence === undefined || !Array.isArray(playlist.segments) || !playlist.segments.length) {
  17722. return;
  17723. }
  17724. const currentMap = this.getMediaSequenceMap(type);
  17725. const result = new Map();
  17726. let currentMediaSequence = playlist.mediaSequence;
  17727. let currentBaseTime;
  17728. if (!currentMap) {
  17729. // first playlist setup:
  17730. currentBaseTime = 0;
  17731. } else if (currentMap.has(playlist.mediaSequence)) {
  17732. // further playlists setup:
  17733. currentBaseTime = currentMap.get(playlist.mediaSequence).start;
  17734. } else {
  17735. // it seems like we have a gap between playlists, use current time as a fallback:
  17736. this.logger_(`MediaSequence sync for ${type} segment loader - received a gap between playlists.
  17737. Fallback base time to: ${currentTime}.
  17738. Received media sequence: ${currentMediaSequence}.
  17739. Current map: `, currentMap);
  17740. currentBaseTime = currentTime;
  17741. }
  17742. this.logger_(`MediaSequence sync for ${type} segment loader.
  17743. Received media sequence: ${currentMediaSequence}.
  17744. base time is ${currentBaseTime}
  17745. Current map: `, currentMap);
  17746. playlist.segments.forEach(segment => {
  17747. const start = currentBaseTime;
  17748. const end = start + segment.duration;
  17749. const range = {
  17750. start,
  17751. end
  17752. };
  17753. result.set(currentMediaSequence, range);
  17754. currentMediaSequence++;
  17755. currentBaseTime = end;
  17756. });
  17757. this.mediaSequenceStorage_.set(type, result);
  17758. }
  17759. /**
  17760. * Find a sync-point for the playlist specified
  17761. *
  17762. * A sync-point is defined as a known mapping from display-time to
  17763. * a segment-index in the current playlist.
  17764. *
  17765. * @param {Playlist} playlist
  17766. * The playlist that needs a sync-point
  17767. * @param {number} duration
  17768. * Duration of the MediaSource (Infinite if playing a live source)
  17769. * @param {number} currentTimeline
  17770. * The last timeline from which a segment was loaded
  17771. * @param {number} currentTime
  17772. * Current player's time
  17773. * @param {string} type
  17774. * Segment loader type
  17775. * @return {Object}
  17776. * A sync-point object
  17777. */
  17778. getSyncPoint(playlist, duration, currentTimeline, currentTime, type) {
  17779. // Always use VOD sync point for VOD
  17780. if (duration !== Infinity) {
  17781. const vodSyncPointStrategy = syncPointStrategies.find(({
  17782. name
  17783. }) => name === 'VOD');
  17784. return vodSyncPointStrategy.run(this, playlist, duration);
  17785. }
  17786. const syncPoints = this.runStrategies_(playlist, duration, currentTimeline, currentTime, type);
  17787. if (!syncPoints.length) {
  17788. // Signal that we need to attempt to get a sync-point manually
  17789. // by fetching a segment in the playlist and constructing
  17790. // a sync-point from that information
  17791. return null;
  17792. } // If we have exact match just return it instead of finding the nearest distance
  17793. for (const syncPointInfo of syncPoints) {
  17794. const {
  17795. syncPoint,
  17796. strategy
  17797. } = syncPointInfo;
  17798. const {
  17799. segmentIndex,
  17800. time
  17801. } = syncPoint;
  17802. if (segmentIndex < 0) {
  17803. continue;
  17804. }
  17805. const selectedSegment = playlist.segments[segmentIndex];
  17806. const start = time;
  17807. const end = start + selectedSegment.duration;
  17808. this.logger_(`Strategy: ${strategy}. Current time: ${currentTime}. selected segment: ${segmentIndex}. Time: [${start} -> ${end}]}`);
  17809. if (currentTime >= start && currentTime < end) {
  17810. this.logger_('Found sync point with exact match: ', syncPoint);
  17811. return syncPoint;
  17812. }
  17813. } // Now find the sync-point that is closest to the currentTime because
  17814. // that should result in the most accurate guess about which segment
  17815. // to fetch
  17816. return this.selectSyncPoint_(syncPoints, {
  17817. key: 'time',
  17818. value: currentTime
  17819. });
  17820. }
  17821. /**
  17822. * Calculate the amount of time that has expired off the playlist during playback
  17823. *
  17824. * @param {Playlist} playlist
  17825. * Playlist object to calculate expired from
  17826. * @param {number} duration
  17827. * Duration of the MediaSource (Infinity if playling a live source)
  17828. * @return {number|null}
  17829. * The amount of time that has expired off the playlist during playback. Null
  17830. * if no sync-points for the playlist can be found.
  17831. */
  17832. getExpiredTime(playlist, duration) {
  17833. if (!playlist || !playlist.segments) {
  17834. return null;
  17835. }
  17836. const syncPoints = this.runStrategies_(playlist, duration, playlist.discontinuitySequence, 0, 'main'); // Without sync-points, there is not enough information to determine the expired time
  17837. if (!syncPoints.length) {
  17838. return null;
  17839. }
  17840. const syncPoint = this.selectSyncPoint_(syncPoints, {
  17841. key: 'segmentIndex',
  17842. value: 0
  17843. }); // If the sync-point is beyond the start of the playlist, we want to subtract the
  17844. // duration from index 0 to syncPoint.segmentIndex instead of adding.
  17845. if (syncPoint.segmentIndex > 0) {
  17846. syncPoint.time *= -1;
  17847. }
  17848. return Math.abs(syncPoint.time + sumDurations({
  17849. defaultDuration: playlist.targetDuration,
  17850. durationList: playlist.segments,
  17851. startIndex: syncPoint.segmentIndex,
  17852. endIndex: 0
  17853. }));
  17854. }
  17855. /**
  17856. * Runs each sync-point strategy and returns a list of sync-points returned by the
  17857. * strategies
  17858. *
  17859. * @private
  17860. * @param {Playlist} playlist
  17861. * The playlist that needs a sync-point
  17862. * @param {number} duration
  17863. * Duration of the MediaSource (Infinity if playing a live source)
  17864. * @param {number} currentTimeline
  17865. * The last timeline from which a segment was loaded
  17866. * @param {number} currentTime
  17867. * Current player's time
  17868. * @param {string} type
  17869. * Segment loader type
  17870. * @return {Array}
  17871. * A list of sync-point objects
  17872. */
  17873. runStrategies_(playlist, duration, currentTimeline, currentTime, type) {
  17874. const syncPoints = []; // Try to find a sync-point in by utilizing various strategies...
  17875. for (let i = 0; i < syncPointStrategies.length; i++) {
  17876. const strategy = syncPointStrategies[i];
  17877. const syncPoint = strategy.run(this, playlist, duration, currentTimeline, currentTime, type);
  17878. if (syncPoint) {
  17879. syncPoint.strategy = strategy.name;
  17880. syncPoints.push({
  17881. strategy: strategy.name,
  17882. syncPoint
  17883. });
  17884. }
  17885. }
  17886. return syncPoints;
  17887. }
  17888. /**
  17889. * Selects the sync-point nearest the specified target
  17890. *
  17891. * @private
  17892. * @param {Array} syncPoints
  17893. * List of sync-points to select from
  17894. * @param {Object} target
  17895. * Object specifying the property and value we are targeting
  17896. * @param {string} target.key
  17897. * Specifies the property to target. Must be either 'time' or 'segmentIndex'
  17898. * @param {number} target.value
  17899. * The value to target for the specified key.
  17900. * @return {Object}
  17901. * The sync-point nearest the target
  17902. */
  17903. selectSyncPoint_(syncPoints, target) {
  17904. let bestSyncPoint = syncPoints[0].syncPoint;
  17905. let bestDistance = Math.abs(syncPoints[0].syncPoint[target.key] - target.value);
  17906. let bestStrategy = syncPoints[0].strategy;
  17907. for (let i = 1; i < syncPoints.length; i++) {
  17908. const newDistance = Math.abs(syncPoints[i].syncPoint[target.key] - target.value);
  17909. if (newDistance < bestDistance) {
  17910. bestDistance = newDistance;
  17911. bestSyncPoint = syncPoints[i].syncPoint;
  17912. bestStrategy = syncPoints[i].strategy;
  17913. }
  17914. }
  17915. this.logger_(`syncPoint for [${target.key}: ${target.value}] chosen with strategy` + ` [${bestStrategy}]: [time:${bestSyncPoint.time},` + ` segmentIndex:${bestSyncPoint.segmentIndex}` + (typeof bestSyncPoint.partIndex === 'number' ? `,partIndex:${bestSyncPoint.partIndex}` : '') + ']');
  17916. return bestSyncPoint;
  17917. }
  17918. /**
  17919. * Save any meta-data present on the segments when segments leave
  17920. * the live window to the playlist to allow for synchronization at the
  17921. * playlist level later.
  17922. *
  17923. * @param {Playlist} oldPlaylist - The previous active playlist
  17924. * @param {Playlist} newPlaylist - The updated and most current playlist
  17925. */
  17926. saveExpiredSegmentInfo(oldPlaylist, newPlaylist) {
  17927. const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence; // Ignore large media sequence gaps
  17928. if (mediaSequenceDiff > MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC) {
  17929. videojs__default["default"].log.warn(`Not saving expired segment info. Media sequence gap ${mediaSequenceDiff} is too large.`);
  17930. return;
  17931. } // When a segment expires from the playlist and it has a start time
  17932. // save that information as a possible sync-point reference in future
  17933. for (let i = mediaSequenceDiff - 1; i >= 0; i--) {
  17934. const lastRemovedSegment = oldPlaylist.segments[i];
  17935. if (lastRemovedSegment && typeof lastRemovedSegment.start !== 'undefined') {
  17936. newPlaylist.syncInfo = {
  17937. mediaSequence: oldPlaylist.mediaSequence + i,
  17938. time: lastRemovedSegment.start
  17939. };
  17940. this.logger_(`playlist refresh sync: [time:${newPlaylist.syncInfo.time},` + ` mediaSequence: ${newPlaylist.syncInfo.mediaSequence}]`);
  17941. this.trigger('syncinfoupdate');
  17942. break;
  17943. }
  17944. }
  17945. }
  17946. /**
  17947. * Save the mapping from playlist's ProgramDateTime to display. This should only happen
  17948. * before segments start to load.
  17949. *
  17950. * @param {Playlist} playlist - The currently active playlist
  17951. */
  17952. setDateTimeMappingForStart(playlist) {
  17953. // It's possible for the playlist to be updated before playback starts, meaning time
  17954. // zero is not yet set. If, during these playlist refreshes, a discontinuity is
  17955. // crossed, then the old time zero mapping (for the prior timeline) would be retained
  17956. // unless the mappings are cleared.
  17957. this.timelineToDatetimeMappings = {};
  17958. if (playlist.segments && playlist.segments.length && playlist.segments[0].dateTimeObject) {
  17959. const firstSegment = playlist.segments[0];
  17960. const playlistTimestamp = firstSegment.dateTimeObject.getTime() / 1000;
  17961. this.timelineToDatetimeMappings[firstSegment.timeline] = -playlistTimestamp;
  17962. }
  17963. }
  17964. /**
  17965. * Calculates and saves timeline mappings, playlist sync info, and segment timing values
  17966. * based on the latest timing information.
  17967. *
  17968. * @param {Object} options
  17969. * Options object
  17970. * @param {SegmentInfo} options.segmentInfo
  17971. * The current active request information
  17972. * @param {boolean} options.shouldSaveTimelineMapping
  17973. * If there's a timeline change, determines if the timeline mapping should be
  17974. * saved for timeline mapping and program date time mappings.
  17975. */
  17976. saveSegmentTimingInfo({
  17977. segmentInfo,
  17978. shouldSaveTimelineMapping
  17979. }) {
  17980. const didCalculateSegmentTimeMapping = this.calculateSegmentTimeMapping_(segmentInfo, segmentInfo.timingInfo, shouldSaveTimelineMapping);
  17981. const segment = segmentInfo.segment;
  17982. if (didCalculateSegmentTimeMapping) {
  17983. this.saveDiscontinuitySyncInfo_(segmentInfo); // If the playlist does not have sync information yet, record that information
  17984. // now with segment timing information
  17985. if (!segmentInfo.playlist.syncInfo) {
  17986. segmentInfo.playlist.syncInfo = {
  17987. mediaSequence: segmentInfo.playlist.mediaSequence + segmentInfo.mediaIndex,
  17988. time: segment.start
  17989. };
  17990. }
  17991. }
  17992. const dateTime = segment.dateTimeObject;
  17993. if (segment.discontinuity && shouldSaveTimelineMapping && dateTime) {
  17994. this.timelineToDatetimeMappings[segment.timeline] = -(dateTime.getTime() / 1000);
  17995. }
  17996. }
  17997. timestampOffsetForTimeline(timeline) {
  17998. if (typeof this.timelines[timeline] === 'undefined') {
  17999. return null;
  18000. }
  18001. return this.timelines[timeline].time;
  18002. }
  18003. mappingForTimeline(timeline) {
  18004. if (typeof this.timelines[timeline] === 'undefined') {
  18005. return null;
  18006. }
  18007. return this.timelines[timeline].mapping;
  18008. }
  18009. /**
  18010. * Use the "media time" for a segment to generate a mapping to "display time" and
  18011. * save that display time to the segment.
  18012. *
  18013. * @private
  18014. * @param {SegmentInfo} segmentInfo
  18015. * The current active request information
  18016. * @param {Object} timingInfo
  18017. * The start and end time of the current segment in "media time"
  18018. * @param {boolean} shouldSaveTimelineMapping
  18019. * If there's a timeline change, determines if the timeline mapping should be
  18020. * saved in timelines.
  18021. * @return {boolean}
  18022. * Returns false if segment time mapping could not be calculated
  18023. */
  18024. calculateSegmentTimeMapping_(segmentInfo, timingInfo, shouldSaveTimelineMapping) {
  18025. // TODO: remove side effects
  18026. const segment = segmentInfo.segment;
  18027. const part = segmentInfo.part;
  18028. let mappingObj = this.timelines[segmentInfo.timeline];
  18029. let start;
  18030. let end;
  18031. if (typeof segmentInfo.timestampOffset === 'number') {
  18032. mappingObj = {
  18033. time: segmentInfo.startOfSegment,
  18034. mapping: segmentInfo.startOfSegment - timingInfo.start
  18035. };
  18036. if (shouldSaveTimelineMapping) {
  18037. this.timelines[segmentInfo.timeline] = mappingObj;
  18038. this.trigger('timestampoffset');
  18039. this.logger_(`time mapping for timeline ${segmentInfo.timeline}: ` + `[time: ${mappingObj.time}] [mapping: ${mappingObj.mapping}]`);
  18040. }
  18041. start = segmentInfo.startOfSegment;
  18042. end = timingInfo.end + mappingObj.mapping;
  18043. } else if (mappingObj) {
  18044. start = timingInfo.start + mappingObj.mapping;
  18045. end = timingInfo.end + mappingObj.mapping;
  18046. } else {
  18047. return false;
  18048. }
  18049. if (part) {
  18050. part.start = start;
  18051. part.end = end;
  18052. } // If we don't have a segment start yet or the start value we got
  18053. // is less than our current segment.start value, save a new start value.
  18054. // We have to do this because parts will have segment timing info saved
  18055. // multiple times and we want segment start to be the earliest part start
  18056. // value for that segment.
  18057. if (!segment.start || start < segment.start) {
  18058. segment.start = start;
  18059. }
  18060. segment.end = end;
  18061. return true;
  18062. }
  18063. /**
  18064. * Each time we have discontinuity in the playlist, attempt to calculate the location
  18065. * in display of the start of the discontinuity and save that. We also save an accuracy
  18066. * value so that we save values with the most accuracy (closest to 0.)
  18067. *
  18068. * @private
  18069. * @param {SegmentInfo} segmentInfo - The current active request information
  18070. */
  18071. saveDiscontinuitySyncInfo_(segmentInfo) {
  18072. const playlist = segmentInfo.playlist;
  18073. const segment = segmentInfo.segment; // If the current segment is a discontinuity then we know exactly where
  18074. // the start of the range and it's accuracy is 0 (greater accuracy values
  18075. // mean more approximation)
  18076. if (segment.discontinuity) {
  18077. this.discontinuities[segment.timeline] = {
  18078. time: segment.start,
  18079. accuracy: 0
  18080. };
  18081. } else if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
  18082. // Search for future discontinuities that we can provide better timing
  18083. // information for and save that information for sync purposes
  18084. for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
  18085. const segmentIndex = playlist.discontinuityStarts[i];
  18086. const discontinuity = playlist.discontinuitySequence + i + 1;
  18087. const mediaIndexDiff = segmentIndex - segmentInfo.mediaIndex;
  18088. const accuracy = Math.abs(mediaIndexDiff);
  18089. if (!this.discontinuities[discontinuity] || this.discontinuities[discontinuity].accuracy > accuracy) {
  18090. let time;
  18091. if (mediaIndexDiff < 0) {
  18092. time = segment.start - sumDurations({
  18093. defaultDuration: playlist.targetDuration,
  18094. durationList: playlist.segments,
  18095. startIndex: segmentInfo.mediaIndex,
  18096. endIndex: segmentIndex
  18097. });
  18098. } else {
  18099. time = segment.end + sumDurations({
  18100. defaultDuration: playlist.targetDuration,
  18101. durationList: playlist.segments,
  18102. startIndex: segmentInfo.mediaIndex + 1,
  18103. endIndex: segmentIndex
  18104. });
  18105. }
  18106. this.discontinuities[discontinuity] = {
  18107. time,
  18108. accuracy
  18109. };
  18110. }
  18111. }
  18112. }
  18113. }
  18114. dispose() {
  18115. this.trigger('dispose');
  18116. this.off();
  18117. }
  18118. }
  18119. /**
  18120. * The TimelineChangeController acts as a source for segment loaders to listen for and
  18121. * keep track of latest and pending timeline changes. This is useful to ensure proper
  18122. * sync, as each loader may need to make a consideration for what timeline the other
  18123. * loader is on before making changes which could impact the other loader's media.
  18124. *
  18125. * @class TimelineChangeController
  18126. * @extends videojs.EventTarget
  18127. */
  18128. class TimelineChangeController extends videojs__default["default"].EventTarget {
  18129. constructor() {
  18130. super();
  18131. this.pendingTimelineChanges_ = {};
  18132. this.lastTimelineChanges_ = {};
  18133. }
  18134. clearPendingTimelineChange(type) {
  18135. this.pendingTimelineChanges_[type] = null;
  18136. this.trigger('pendingtimelinechange');
  18137. }
  18138. pendingTimelineChange({
  18139. type,
  18140. from,
  18141. to
  18142. }) {
  18143. if (typeof from === 'number' && typeof to === 'number') {
  18144. this.pendingTimelineChanges_[type] = {
  18145. type,
  18146. from,
  18147. to
  18148. };
  18149. this.trigger('pendingtimelinechange');
  18150. }
  18151. return this.pendingTimelineChanges_[type];
  18152. }
  18153. lastTimelineChange({
  18154. type,
  18155. from,
  18156. to
  18157. }) {
  18158. if (typeof from === 'number' && typeof to === 'number') {
  18159. this.lastTimelineChanges_[type] = {
  18160. type,
  18161. from,
  18162. to
  18163. };
  18164. delete this.pendingTimelineChanges_[type];
  18165. this.trigger('timelinechange');
  18166. }
  18167. return this.lastTimelineChanges_[type];
  18168. }
  18169. dispose() {
  18170. this.trigger('dispose');
  18171. this.pendingTimelineChanges_ = {};
  18172. this.lastTimelineChanges_ = {};
  18173. this.off();
  18174. }
  18175. }
  18176. /* rollup-plugin-worker-factory start for worker!/home/runner/work/http-streaming/http-streaming/src/decrypter-worker.js */
  18177. const workerCode = transform(getWorkerString(function () {
  18178. /**
  18179. * @file stream.js
  18180. */
  18181. /**
  18182. * A lightweight readable stream implemention that handles event dispatching.
  18183. *
  18184. * @class Stream
  18185. */
  18186. var Stream = /*#__PURE__*/function () {
  18187. function Stream() {
  18188. this.listeners = {};
  18189. }
  18190. /**
  18191. * Add a listener for a specified event type.
  18192. *
  18193. * @param {string} type the event name
  18194. * @param {Function} listener the callback to be invoked when an event of
  18195. * the specified type occurs
  18196. */
  18197. var _proto = Stream.prototype;
  18198. _proto.on = function on(type, listener) {
  18199. if (!this.listeners[type]) {
  18200. this.listeners[type] = [];
  18201. }
  18202. this.listeners[type].push(listener);
  18203. }
  18204. /**
  18205. * Remove a listener for a specified event type.
  18206. *
  18207. * @param {string} type the event name
  18208. * @param {Function} listener a function previously registered for this
  18209. * type of event through `on`
  18210. * @return {boolean} if we could turn it off or not
  18211. */
  18212. ;
  18213. _proto.off = function off(type, listener) {
  18214. if (!this.listeners[type]) {
  18215. return false;
  18216. }
  18217. var index = this.listeners[type].indexOf(listener); // TODO: which is better?
  18218. // In Video.js we slice listener functions
  18219. // on trigger so that it does not mess up the order
  18220. // while we loop through.
  18221. //
  18222. // Here we slice on off so that the loop in trigger
  18223. // can continue using it's old reference to loop without
  18224. // messing up the order.
  18225. this.listeners[type] = this.listeners[type].slice(0);
  18226. this.listeners[type].splice(index, 1);
  18227. return index > -1;
  18228. }
  18229. /**
  18230. * Trigger an event of the specified type on this stream. Any additional
  18231. * arguments to this function are passed as parameters to event listeners.
  18232. *
  18233. * @param {string} type the event name
  18234. */
  18235. ;
  18236. _proto.trigger = function trigger(type) {
  18237. var callbacks = this.listeners[type];
  18238. if (!callbacks) {
  18239. return;
  18240. } // Slicing the arguments on every invocation of this method
  18241. // can add a significant amount of overhead. Avoid the
  18242. // intermediate object creation for the common case of a
  18243. // single callback argument
  18244. if (arguments.length === 2) {
  18245. var length = callbacks.length;
  18246. for (var i = 0; i < length; ++i) {
  18247. callbacks[i].call(this, arguments[1]);
  18248. }
  18249. } else {
  18250. var args = Array.prototype.slice.call(arguments, 1);
  18251. var _length = callbacks.length;
  18252. for (var _i = 0; _i < _length; ++_i) {
  18253. callbacks[_i].apply(this, args);
  18254. }
  18255. }
  18256. }
  18257. /**
  18258. * Destroys the stream and cleans up.
  18259. */
  18260. ;
  18261. _proto.dispose = function dispose() {
  18262. this.listeners = {};
  18263. }
  18264. /**
  18265. * Forwards all `data` events on this stream to the destination stream. The
  18266. * destination stream should provide a method `push` to receive the data
  18267. * events as they arrive.
  18268. *
  18269. * @param {Stream} destination the stream that will receive all `data` events
  18270. * @see http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
  18271. */
  18272. ;
  18273. _proto.pipe = function pipe(destination) {
  18274. this.on('data', function (data) {
  18275. destination.push(data);
  18276. });
  18277. };
  18278. return Stream;
  18279. }();
  18280. /*! @name pkcs7 @version 1.0.4 @license Apache-2.0 */
  18281. /**
  18282. * Returns the subarray of a Uint8Array without PKCS#7 padding.
  18283. *
  18284. * @param padded {Uint8Array} unencrypted bytes that have been padded
  18285. * @return {Uint8Array} the unpadded bytes
  18286. * @see http://tools.ietf.org/html/rfc5652
  18287. */
  18288. function unpad(padded) {
  18289. return padded.subarray(0, padded.byteLength - padded[padded.byteLength - 1]);
  18290. }
  18291. /*! @name aes-decrypter @version 4.0.1 @license Apache-2.0 */
  18292. /**
  18293. * @file aes.js
  18294. *
  18295. * This file contains an adaptation of the AES decryption algorithm
  18296. * from the Standford Javascript Cryptography Library. That work is
  18297. * covered by the following copyright and permissions notice:
  18298. *
  18299. * Copyright 2009-2010 Emily Stark, Mike Hamburg, Dan Boneh.
  18300. * All rights reserved.
  18301. *
  18302. * Redistribution and use in source and binary forms, with or without
  18303. * modification, are permitted provided that the following conditions are
  18304. * met:
  18305. *
  18306. * 1. Redistributions of source code must retain the above copyright
  18307. * notice, this list of conditions and the following disclaimer.
  18308. *
  18309. * 2. Redistributions in binary form must reproduce the above
  18310. * copyright notice, this list of conditions and the following
  18311. * disclaimer in the documentation and/or other materials provided
  18312. * with the distribution.
  18313. *
  18314. * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
  18315. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18316. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18317. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR CONTRIBUTORS BE
  18318. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18319. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18320. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  18321. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  18322. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  18323. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  18324. * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18325. *
  18326. * The views and conclusions contained in the software and documentation
  18327. * are those of the authors and should not be interpreted as representing
  18328. * official policies, either expressed or implied, of the authors.
  18329. */
  18330. /**
  18331. * Expand the S-box tables.
  18332. *
  18333. * @private
  18334. */
  18335. const precompute = function () {
  18336. const tables = [[[], [], [], [], []], [[], [], [], [], []]];
  18337. const encTable = tables[0];
  18338. const decTable = tables[1];
  18339. const sbox = encTable[4];
  18340. const sboxInv = decTable[4];
  18341. let i;
  18342. let x;
  18343. let xInv;
  18344. const d = [];
  18345. const th = [];
  18346. let x2;
  18347. let x4;
  18348. let x8;
  18349. let s;
  18350. let tEnc;
  18351. let tDec; // Compute double and third tables
  18352. for (i = 0; i < 256; i++) {
  18353. th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
  18354. }
  18355. for (x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
  18356. // Compute sbox
  18357. s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
  18358. s = s >> 8 ^ s & 255 ^ 99;
  18359. sbox[x] = s;
  18360. sboxInv[s] = x; // Compute MixColumns
  18361. x8 = d[x4 = d[x2 = d[x]]];
  18362. tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
  18363. tEnc = d[s] * 0x101 ^ s * 0x1010100;
  18364. for (i = 0; i < 4; i++) {
  18365. encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
  18366. decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
  18367. }
  18368. } // Compactify. Considerable speedup on Firefox.
  18369. for (i = 0; i < 5; i++) {
  18370. encTable[i] = encTable[i].slice(0);
  18371. decTable[i] = decTable[i].slice(0);
  18372. }
  18373. return tables;
  18374. };
  18375. let aesTables = null;
  18376. /**
  18377. * Schedule out an AES key for both encryption and decryption. This
  18378. * is a low-level class. Use a cipher mode to do bulk encryption.
  18379. *
  18380. * @class AES
  18381. * @param key {Array} The key as an array of 4, 6 or 8 words.
  18382. */
  18383. class AES {
  18384. constructor(key) {
  18385. /**
  18386. * The expanded S-box and inverse S-box tables. These will be computed
  18387. * on the client so that we don't have to send them down the wire.
  18388. *
  18389. * There are two tables, _tables[0] is for encryption and
  18390. * _tables[1] is for decryption.
  18391. *
  18392. * The first 4 sub-tables are the expanded S-box with MixColumns. The
  18393. * last (_tables[01][4]) is the S-box itself.
  18394. *
  18395. * @private
  18396. */
  18397. // if we have yet to precompute the S-box tables
  18398. // do so now
  18399. if (!aesTables) {
  18400. aesTables = precompute();
  18401. } // then make a copy of that object for use
  18402. this._tables = [[aesTables[0][0].slice(), aesTables[0][1].slice(), aesTables[0][2].slice(), aesTables[0][3].slice(), aesTables[0][4].slice()], [aesTables[1][0].slice(), aesTables[1][1].slice(), aesTables[1][2].slice(), aesTables[1][3].slice(), aesTables[1][4].slice()]];
  18403. let i;
  18404. let j;
  18405. let tmp;
  18406. const sbox = this._tables[0][4];
  18407. const decTable = this._tables[1];
  18408. const keyLen = key.length;
  18409. let rcon = 1;
  18410. if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
  18411. throw new Error('Invalid aes key size');
  18412. }
  18413. const encKey = key.slice(0);
  18414. const decKey = [];
  18415. this._key = [encKey, decKey]; // schedule encryption keys
  18416. for (i = keyLen; i < 4 * keyLen + 28; i++) {
  18417. tmp = encKey[i - 1]; // apply sbox
  18418. if (i % keyLen === 0 || keyLen === 8 && i % keyLen === 4) {
  18419. tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255]; // shift rows and add rcon
  18420. if (i % keyLen === 0) {
  18421. tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
  18422. rcon = rcon << 1 ^ (rcon >> 7) * 283;
  18423. }
  18424. }
  18425. encKey[i] = encKey[i - keyLen] ^ tmp;
  18426. } // schedule decryption keys
  18427. for (j = 0; i; j++, i--) {
  18428. tmp = encKey[j & 3 ? i : i - 4];
  18429. if (i <= 4 || j < 4) {
  18430. decKey[j] = tmp;
  18431. } else {
  18432. decKey[j] = decTable[0][sbox[tmp >>> 24]] ^ decTable[1][sbox[tmp >> 16 & 255]] ^ decTable[2][sbox[tmp >> 8 & 255]] ^ decTable[3][sbox[tmp & 255]];
  18433. }
  18434. }
  18435. }
  18436. /**
  18437. * Decrypt 16 bytes, specified as four 32-bit words.
  18438. *
  18439. * @param {number} encrypted0 the first word to decrypt
  18440. * @param {number} encrypted1 the second word to decrypt
  18441. * @param {number} encrypted2 the third word to decrypt
  18442. * @param {number} encrypted3 the fourth word to decrypt
  18443. * @param {Int32Array} out the array to write the decrypted words
  18444. * into
  18445. * @param {number} offset the offset into the output array to start
  18446. * writing results
  18447. * @return {Array} The plaintext.
  18448. */
  18449. decrypt(encrypted0, encrypted1, encrypted2, encrypted3, out, offset) {
  18450. const key = this._key[1]; // state variables a,b,c,d are loaded with pre-whitened data
  18451. let a = encrypted0 ^ key[0];
  18452. let b = encrypted3 ^ key[1];
  18453. let c = encrypted2 ^ key[2];
  18454. let d = encrypted1 ^ key[3];
  18455. let a2;
  18456. let b2;
  18457. let c2; // key.length === 2 ?
  18458. const nInnerRounds = key.length / 4 - 2;
  18459. let i;
  18460. let kIndex = 4;
  18461. const table = this._tables[1]; // load up the tables
  18462. const table0 = table[0];
  18463. const table1 = table[1];
  18464. const table2 = table[2];
  18465. const table3 = table[3];
  18466. const sbox = table[4]; // Inner rounds. Cribbed from OpenSSL.
  18467. for (i = 0; i < nInnerRounds; i++) {
  18468. a2 = table0[a >>> 24] ^ table1[b >> 16 & 255] ^ table2[c >> 8 & 255] ^ table3[d & 255] ^ key[kIndex];
  18469. b2 = table0[b >>> 24] ^ table1[c >> 16 & 255] ^ table2[d >> 8 & 255] ^ table3[a & 255] ^ key[kIndex + 1];
  18470. c2 = table0[c >>> 24] ^ table1[d >> 16 & 255] ^ table2[a >> 8 & 255] ^ table3[b & 255] ^ key[kIndex + 2];
  18471. d = table0[d >>> 24] ^ table1[a >> 16 & 255] ^ table2[b >> 8 & 255] ^ table3[c & 255] ^ key[kIndex + 3];
  18472. kIndex += 4;
  18473. a = a2;
  18474. b = b2;
  18475. c = c2;
  18476. } // Last round.
  18477. for (i = 0; i < 4; i++) {
  18478. out[(3 & -i) + offset] = sbox[a >>> 24] << 24 ^ sbox[b >> 16 & 255] << 16 ^ sbox[c >> 8 & 255] << 8 ^ sbox[d & 255] ^ key[kIndex++];
  18479. a2 = a;
  18480. a = b;
  18481. b = c;
  18482. c = d;
  18483. d = a2;
  18484. }
  18485. }
  18486. }
  18487. /**
  18488. * @file async-stream.js
  18489. */
  18490. /**
  18491. * A wrapper around the Stream class to use setTimeout
  18492. * and run stream "jobs" Asynchronously
  18493. *
  18494. * @class AsyncStream
  18495. * @extends Stream
  18496. */
  18497. class AsyncStream extends Stream {
  18498. constructor() {
  18499. super(Stream);
  18500. this.jobs = [];
  18501. this.delay = 1;
  18502. this.timeout_ = null;
  18503. }
  18504. /**
  18505. * process an async job
  18506. *
  18507. * @private
  18508. */
  18509. processJob_() {
  18510. this.jobs.shift()();
  18511. if (this.jobs.length) {
  18512. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  18513. } else {
  18514. this.timeout_ = null;
  18515. }
  18516. }
  18517. /**
  18518. * push a job into the stream
  18519. *
  18520. * @param {Function} job the job to push into the stream
  18521. */
  18522. push(job) {
  18523. this.jobs.push(job);
  18524. if (!this.timeout_) {
  18525. this.timeout_ = setTimeout(this.processJob_.bind(this), this.delay);
  18526. }
  18527. }
  18528. }
  18529. /**
  18530. * @file decrypter.js
  18531. *
  18532. * An asynchronous implementation of AES-128 CBC decryption with
  18533. * PKCS#7 padding.
  18534. */
  18535. /**
  18536. * Convert network-order (big-endian) bytes into their little-endian
  18537. * representation.
  18538. */
  18539. const ntoh = function (word) {
  18540. return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
  18541. };
  18542. /**
  18543. * Decrypt bytes using AES-128 with CBC and PKCS#7 padding.
  18544. *
  18545. * @param {Uint8Array} encrypted the encrypted bytes
  18546. * @param {Uint32Array} key the bytes of the decryption key
  18547. * @param {Uint32Array} initVector the initialization vector (IV) to
  18548. * use for the first round of CBC.
  18549. * @return {Uint8Array} the decrypted bytes
  18550. *
  18551. * @see http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
  18552. * @see http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_Block_Chaining_.28CBC.29
  18553. * @see https://tools.ietf.org/html/rfc2315
  18554. */
  18555. const decrypt = function (encrypted, key, initVector) {
  18556. // word-level access to the encrypted bytes
  18557. const encrypted32 = new Int32Array(encrypted.buffer, encrypted.byteOffset, encrypted.byteLength >> 2);
  18558. const decipher = new AES(Array.prototype.slice.call(key)); // byte and word-level access for the decrypted output
  18559. const decrypted = new Uint8Array(encrypted.byteLength);
  18560. const decrypted32 = new Int32Array(decrypted.buffer); // temporary variables for working with the IV, encrypted, and
  18561. // decrypted data
  18562. let init0;
  18563. let init1;
  18564. let init2;
  18565. let init3;
  18566. let encrypted0;
  18567. let encrypted1;
  18568. let encrypted2;
  18569. let encrypted3; // iteration variable
  18570. let wordIx; // pull out the words of the IV to ensure we don't modify the
  18571. // passed-in reference and easier access
  18572. init0 = initVector[0];
  18573. init1 = initVector[1];
  18574. init2 = initVector[2];
  18575. init3 = initVector[3]; // decrypt four word sequences, applying cipher-block chaining (CBC)
  18576. // to each decrypted block
  18577. for (wordIx = 0; wordIx < encrypted32.length; wordIx += 4) {
  18578. // convert big-endian (network order) words into little-endian
  18579. // (javascript order)
  18580. encrypted0 = ntoh(encrypted32[wordIx]);
  18581. encrypted1 = ntoh(encrypted32[wordIx + 1]);
  18582. encrypted2 = ntoh(encrypted32[wordIx + 2]);
  18583. encrypted3 = ntoh(encrypted32[wordIx + 3]); // decrypt the block
  18584. decipher.decrypt(encrypted0, encrypted1, encrypted2, encrypted3, decrypted32, wordIx); // XOR with the IV, and restore network byte-order to obtain the
  18585. // plaintext
  18586. decrypted32[wordIx] = ntoh(decrypted32[wordIx] ^ init0);
  18587. decrypted32[wordIx + 1] = ntoh(decrypted32[wordIx + 1] ^ init1);
  18588. decrypted32[wordIx + 2] = ntoh(decrypted32[wordIx + 2] ^ init2);
  18589. decrypted32[wordIx + 3] = ntoh(decrypted32[wordIx + 3] ^ init3); // setup the IV for the next round
  18590. init0 = encrypted0;
  18591. init1 = encrypted1;
  18592. init2 = encrypted2;
  18593. init3 = encrypted3;
  18594. }
  18595. return decrypted;
  18596. };
  18597. /**
  18598. * The `Decrypter` class that manages decryption of AES
  18599. * data through `AsyncStream` objects and the `decrypt`
  18600. * function
  18601. *
  18602. * @param {Uint8Array} encrypted the encrypted bytes
  18603. * @param {Uint32Array} key the bytes of the decryption key
  18604. * @param {Uint32Array} initVector the initialization vector (IV) to
  18605. * @param {Function} done the function to run when done
  18606. * @class Decrypter
  18607. */
  18608. class Decrypter {
  18609. constructor(encrypted, key, initVector, done) {
  18610. const step = Decrypter.STEP;
  18611. const encrypted32 = new Int32Array(encrypted.buffer);
  18612. const decrypted = new Uint8Array(encrypted.byteLength);
  18613. let i = 0;
  18614. this.asyncStream_ = new AsyncStream(); // split up the encryption job and do the individual chunks asynchronously
  18615. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  18616. for (i = step; i < encrypted32.length; i += step) {
  18617. initVector = new Uint32Array([ntoh(encrypted32[i - 4]), ntoh(encrypted32[i - 3]), ntoh(encrypted32[i - 2]), ntoh(encrypted32[i - 1])]);
  18618. this.asyncStream_.push(this.decryptChunk_(encrypted32.subarray(i, i + step), key, initVector, decrypted));
  18619. } // invoke the done() callback when everything is finished
  18620. this.asyncStream_.push(function () {
  18621. // remove pkcs#7 padding from the decrypted bytes
  18622. done(null, unpad(decrypted));
  18623. });
  18624. }
  18625. /**
  18626. * a getter for step the maximum number of bytes to process at one time
  18627. *
  18628. * @return {number} the value of step 32000
  18629. */
  18630. static get STEP() {
  18631. // 4 * 8000;
  18632. return 32000;
  18633. }
  18634. /**
  18635. * @private
  18636. */
  18637. decryptChunk_(encrypted, key, initVector, decrypted) {
  18638. return function () {
  18639. const bytes = decrypt(encrypted, key, initVector);
  18640. decrypted.set(bytes, encrypted.byteOffset);
  18641. };
  18642. }
  18643. }
  18644. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  18645. var win;
  18646. if (typeof window !== "undefined") {
  18647. win = window;
  18648. } else if (typeof commonjsGlobal !== "undefined") {
  18649. win = commonjsGlobal;
  18650. } else if (typeof self !== "undefined") {
  18651. win = self;
  18652. } else {
  18653. win = {};
  18654. }
  18655. var window_1 = win;
  18656. var isArrayBufferView = function isArrayBufferView(obj) {
  18657. if (ArrayBuffer.isView === 'function') {
  18658. return ArrayBuffer.isView(obj);
  18659. }
  18660. return obj && obj.buffer instanceof ArrayBuffer;
  18661. };
  18662. var BigInt = window_1.BigInt || Number;
  18663. [BigInt('0x1'), BigInt('0x100'), BigInt('0x10000'), BigInt('0x1000000'), BigInt('0x100000000'), BigInt('0x10000000000'), BigInt('0x1000000000000'), BigInt('0x100000000000000'), BigInt('0x10000000000000000')];
  18664. (function () {
  18665. var a = new Uint16Array([0xFFCC]);
  18666. var b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
  18667. if (b[0] === 0xFF) {
  18668. return 'big';
  18669. }
  18670. if (b[0] === 0xCC) {
  18671. return 'little';
  18672. }
  18673. return 'unknown';
  18674. })();
  18675. /**
  18676. * Creates an object for sending to a web worker modifying properties that are TypedArrays
  18677. * into a new object with seperated properties for the buffer, byteOffset, and byteLength.
  18678. *
  18679. * @param {Object} message
  18680. * Object of properties and values to send to the web worker
  18681. * @return {Object}
  18682. * Modified message with TypedArray values expanded
  18683. * @function createTransferableMessage
  18684. */
  18685. const createTransferableMessage = function (message) {
  18686. const transferable = {};
  18687. Object.keys(message).forEach(key => {
  18688. const value = message[key];
  18689. if (isArrayBufferView(value)) {
  18690. transferable[key] = {
  18691. bytes: value.buffer,
  18692. byteOffset: value.byteOffset,
  18693. byteLength: value.byteLength
  18694. };
  18695. } else {
  18696. transferable[key] = value;
  18697. }
  18698. });
  18699. return transferable;
  18700. };
  18701. /* global self */
  18702. /**
  18703. * Our web worker interface so that things can talk to aes-decrypter
  18704. * that will be running in a web worker. the scope is passed to this by
  18705. * webworkify.
  18706. */
  18707. self.onmessage = function (event) {
  18708. const data = event.data;
  18709. const encrypted = new Uint8Array(data.encrypted.bytes, data.encrypted.byteOffset, data.encrypted.byteLength);
  18710. const key = new Uint32Array(data.key.bytes, data.key.byteOffset, data.key.byteLength / 4);
  18711. const iv = new Uint32Array(data.iv.bytes, data.iv.byteOffset, data.iv.byteLength / 4);
  18712. /* eslint-disable no-new, handle-callback-err */
  18713. new Decrypter(encrypted, key, iv, function (err, bytes) {
  18714. self.postMessage(createTransferableMessage({
  18715. source: data.source,
  18716. decrypted: bytes
  18717. }), [bytes.buffer]);
  18718. });
  18719. /* eslint-enable */
  18720. };
  18721. }));
  18722. var Decrypter = factory(workerCode);
  18723. /* rollup-plugin-worker-factory end for worker!/home/runner/work/http-streaming/http-streaming/src/decrypter-worker.js */
  18724. /**
  18725. * Convert the properties of an HLS track into an audioTrackKind.
  18726. *
  18727. * @private
  18728. */
  18729. const audioTrackKind_ = properties => {
  18730. let kind = properties.default ? 'main' : 'alternative';
  18731. if (properties.characteristics && properties.characteristics.indexOf('public.accessibility.describes-video') >= 0) {
  18732. kind = 'main-desc';
  18733. }
  18734. return kind;
  18735. };
  18736. /**
  18737. * Pause provided segment loader and playlist loader if active
  18738. *
  18739. * @param {SegmentLoader} segmentLoader
  18740. * SegmentLoader to pause
  18741. * @param {Object} mediaType
  18742. * Active media type
  18743. * @function stopLoaders
  18744. */
  18745. const stopLoaders = (segmentLoader, mediaType) => {
  18746. segmentLoader.abort();
  18747. segmentLoader.pause();
  18748. if (mediaType && mediaType.activePlaylistLoader) {
  18749. mediaType.activePlaylistLoader.pause();
  18750. mediaType.activePlaylistLoader = null;
  18751. }
  18752. };
  18753. /**
  18754. * Start loading provided segment loader and playlist loader
  18755. *
  18756. * @param {PlaylistLoader} playlistLoader
  18757. * PlaylistLoader to start loading
  18758. * @param {Object} mediaType
  18759. * Active media type
  18760. * @function startLoaders
  18761. */
  18762. const startLoaders = (playlistLoader, mediaType) => {
  18763. // Segment loader will be started after `loadedmetadata` or `loadedplaylist` from the
  18764. // playlist loader
  18765. mediaType.activePlaylistLoader = playlistLoader;
  18766. playlistLoader.load();
  18767. };
  18768. /**
  18769. * Returns a function to be called when the media group changes. It performs a
  18770. * non-destructive (preserve the buffer) resync of the SegmentLoader. This is because a
  18771. * change of group is merely a rendition switch of the same content at another encoding,
  18772. * rather than a change of content, such as switching audio from English to Spanish.
  18773. *
  18774. * @param {string} type
  18775. * MediaGroup type
  18776. * @param {Object} settings
  18777. * Object containing required information for media groups
  18778. * @return {Function}
  18779. * Handler for a non-destructive resync of SegmentLoader when the active media
  18780. * group changes.
  18781. * @function onGroupChanged
  18782. */
  18783. const onGroupChanged = (type, settings) => () => {
  18784. const {
  18785. segmentLoaders: {
  18786. [type]: segmentLoader,
  18787. main: mainSegmentLoader
  18788. },
  18789. mediaTypes: {
  18790. [type]: mediaType
  18791. }
  18792. } = settings;
  18793. const activeTrack = mediaType.activeTrack();
  18794. const activeGroup = mediaType.getActiveGroup();
  18795. const previousActiveLoader = mediaType.activePlaylistLoader;
  18796. const lastGroup = mediaType.lastGroup_; // the group did not change do nothing
  18797. if (activeGroup && lastGroup && activeGroup.id === lastGroup.id) {
  18798. return;
  18799. }
  18800. mediaType.lastGroup_ = activeGroup;
  18801. mediaType.lastTrack_ = activeTrack;
  18802. stopLoaders(segmentLoader, mediaType);
  18803. if (!activeGroup || activeGroup.isMainPlaylist) {
  18804. // there is no group active or active group is a main playlist and won't change
  18805. return;
  18806. }
  18807. if (!activeGroup.playlistLoader) {
  18808. if (previousActiveLoader) {
  18809. // The previous group had a playlist loader but the new active group does not
  18810. // this means we are switching from demuxed to muxed audio. In this case we want to
  18811. // do a destructive reset of the main segment loader and not restart the audio
  18812. // loaders.
  18813. mainSegmentLoader.resetEverything();
  18814. }
  18815. return;
  18816. } // Non-destructive resync
  18817. segmentLoader.resyncLoader();
  18818. startLoaders(activeGroup.playlistLoader, mediaType);
  18819. };
  18820. const onGroupChanging = (type, settings) => () => {
  18821. const {
  18822. segmentLoaders: {
  18823. [type]: segmentLoader
  18824. },
  18825. mediaTypes: {
  18826. [type]: mediaType
  18827. }
  18828. } = settings;
  18829. mediaType.lastGroup_ = null;
  18830. segmentLoader.abort();
  18831. segmentLoader.pause();
  18832. };
  18833. /**
  18834. * Returns a function to be called when the media track changes. It performs a
  18835. * destructive reset of the SegmentLoader to ensure we start loading as close to
  18836. * currentTime as possible.
  18837. *
  18838. * @param {string} type
  18839. * MediaGroup type
  18840. * @param {Object} settings
  18841. * Object containing required information for media groups
  18842. * @return {Function}
  18843. * Handler for a destructive reset of SegmentLoader when the active media
  18844. * track changes.
  18845. * @function onTrackChanged
  18846. */
  18847. const onTrackChanged = (type, settings) => () => {
  18848. const {
  18849. mainPlaylistLoader,
  18850. segmentLoaders: {
  18851. [type]: segmentLoader,
  18852. main: mainSegmentLoader
  18853. },
  18854. mediaTypes: {
  18855. [type]: mediaType
  18856. }
  18857. } = settings;
  18858. const activeTrack = mediaType.activeTrack();
  18859. const activeGroup = mediaType.getActiveGroup();
  18860. const previousActiveLoader = mediaType.activePlaylistLoader;
  18861. const lastTrack = mediaType.lastTrack_; // track did not change, do nothing
  18862. if (lastTrack && activeTrack && lastTrack.id === activeTrack.id) {
  18863. return;
  18864. }
  18865. mediaType.lastGroup_ = activeGroup;
  18866. mediaType.lastTrack_ = activeTrack;
  18867. stopLoaders(segmentLoader, mediaType);
  18868. if (!activeGroup) {
  18869. // there is no group active so we do not want to restart loaders
  18870. return;
  18871. }
  18872. if (activeGroup.isMainPlaylist) {
  18873. // track did not change, do nothing
  18874. if (!activeTrack || !lastTrack || activeTrack.id === lastTrack.id) {
  18875. return;
  18876. }
  18877. const pc = settings.vhs.playlistController_;
  18878. const newPlaylist = pc.selectPlaylist(); // media will not change do nothing
  18879. if (pc.media() === newPlaylist) {
  18880. return;
  18881. }
  18882. mediaType.logger_(`track change. Switching main audio from ${lastTrack.id} to ${activeTrack.id}`);
  18883. mainPlaylistLoader.pause();
  18884. mainSegmentLoader.resetEverything();
  18885. pc.fastQualityChange_(newPlaylist);
  18886. return;
  18887. }
  18888. if (type === 'AUDIO') {
  18889. if (!activeGroup.playlistLoader) {
  18890. // when switching from demuxed audio/video to muxed audio/video (noted by no
  18891. // playlist loader for the audio group), we want to do a destructive reset of the
  18892. // main segment loader and not restart the audio loaders
  18893. mainSegmentLoader.setAudio(true); // don't have to worry about disabling the audio of the audio segment loader since
  18894. // it should be stopped
  18895. mainSegmentLoader.resetEverything();
  18896. return;
  18897. } // although the segment loader is an audio segment loader, call the setAudio
  18898. // function to ensure it is prepared to re-append the init segment (or handle other
  18899. // config changes)
  18900. segmentLoader.setAudio(true);
  18901. mainSegmentLoader.setAudio(false);
  18902. }
  18903. if (previousActiveLoader === activeGroup.playlistLoader) {
  18904. // Nothing has actually changed. This can happen because track change events can fire
  18905. // multiple times for a "single" change. One for enabling the new active track, and
  18906. // one for disabling the track that was active
  18907. startLoaders(activeGroup.playlistLoader, mediaType);
  18908. return;
  18909. }
  18910. if (segmentLoader.track) {
  18911. // For WebVTT, set the new text track in the segmentloader
  18912. segmentLoader.track(activeTrack);
  18913. } // destructive reset
  18914. segmentLoader.resetEverything();
  18915. startLoaders(activeGroup.playlistLoader, mediaType);
  18916. };
  18917. const onError = {
  18918. /**
  18919. * Returns a function to be called when a SegmentLoader or PlaylistLoader encounters
  18920. * an error.
  18921. *
  18922. * @param {string} type
  18923. * MediaGroup type
  18924. * @param {Object} settings
  18925. * Object containing required information for media groups
  18926. * @return {Function}
  18927. * Error handler. Logs warning (or error if the playlist is excluded) to
  18928. * console and switches back to default audio track.
  18929. * @function onError.AUDIO
  18930. */
  18931. AUDIO: (type, settings) => () => {
  18932. const {
  18933. mediaTypes: {
  18934. [type]: mediaType
  18935. },
  18936. excludePlaylist
  18937. } = settings; // switch back to default audio track
  18938. const activeTrack = mediaType.activeTrack();
  18939. const activeGroup = mediaType.activeGroup();
  18940. const id = (activeGroup.filter(group => group.default)[0] || activeGroup[0]).id;
  18941. const defaultTrack = mediaType.tracks[id];
  18942. if (activeTrack === defaultTrack) {
  18943. // Default track encountered an error. All we can do now is exclude the current
  18944. // rendition and hope another will switch audio groups
  18945. excludePlaylist({
  18946. error: {
  18947. message: 'Problem encountered loading the default audio track.'
  18948. }
  18949. });
  18950. return;
  18951. }
  18952. videojs__default["default"].log.warn('Problem encountered loading the alternate audio track.' + 'Switching back to default.');
  18953. for (const trackId in mediaType.tracks) {
  18954. mediaType.tracks[trackId].enabled = mediaType.tracks[trackId] === defaultTrack;
  18955. }
  18956. mediaType.onTrackChanged();
  18957. },
  18958. /**
  18959. * Returns a function to be called when a SegmentLoader or PlaylistLoader encounters
  18960. * an error.
  18961. *
  18962. * @param {string} type
  18963. * MediaGroup type
  18964. * @param {Object} settings
  18965. * Object containing required information for media groups
  18966. * @return {Function}
  18967. * Error handler. Logs warning to console and disables the active subtitle track
  18968. * @function onError.SUBTITLES
  18969. */
  18970. SUBTITLES: (type, settings) => () => {
  18971. const {
  18972. mediaTypes: {
  18973. [type]: mediaType
  18974. }
  18975. } = settings;
  18976. videojs__default["default"].log.warn('Problem encountered loading the subtitle track.' + 'Disabling subtitle track.');
  18977. const track = mediaType.activeTrack();
  18978. if (track) {
  18979. track.mode = 'disabled';
  18980. }
  18981. mediaType.onTrackChanged();
  18982. }
  18983. };
  18984. const setupListeners = {
  18985. /**
  18986. * Setup event listeners for audio playlist loader
  18987. *
  18988. * @param {string} type
  18989. * MediaGroup type
  18990. * @param {PlaylistLoader|null} playlistLoader
  18991. * PlaylistLoader to register listeners on
  18992. * @param {Object} settings
  18993. * Object containing required information for media groups
  18994. * @function setupListeners.AUDIO
  18995. */
  18996. AUDIO: (type, playlistLoader, settings) => {
  18997. if (!playlistLoader) {
  18998. // no playlist loader means audio will be muxed with the video
  18999. return;
  19000. }
  19001. const {
  19002. tech,
  19003. requestOptions,
  19004. segmentLoaders: {
  19005. [type]: segmentLoader
  19006. }
  19007. } = settings;
  19008. playlistLoader.on('loadedmetadata', () => {
  19009. const media = playlistLoader.media();
  19010. segmentLoader.playlist(media, requestOptions); // if the video is already playing, or if this isn't a live video and preload
  19011. // permits, start downloading segments
  19012. if (!tech.paused() || media.endList && tech.preload() !== 'none') {
  19013. segmentLoader.load();
  19014. }
  19015. });
  19016. playlistLoader.on('loadedplaylist', () => {
  19017. segmentLoader.playlist(playlistLoader.media(), requestOptions); // If the player isn't paused, ensure that the segment loader is running
  19018. if (!tech.paused()) {
  19019. segmentLoader.load();
  19020. }
  19021. });
  19022. playlistLoader.on('error', onError[type](type, settings));
  19023. },
  19024. /**
  19025. * Setup event listeners for subtitle playlist loader
  19026. *
  19027. * @param {string} type
  19028. * MediaGroup type
  19029. * @param {PlaylistLoader|null} playlistLoader
  19030. * PlaylistLoader to register listeners on
  19031. * @param {Object} settings
  19032. * Object containing required information for media groups
  19033. * @function setupListeners.SUBTITLES
  19034. */
  19035. SUBTITLES: (type, playlistLoader, settings) => {
  19036. const {
  19037. tech,
  19038. requestOptions,
  19039. segmentLoaders: {
  19040. [type]: segmentLoader
  19041. },
  19042. mediaTypes: {
  19043. [type]: mediaType
  19044. }
  19045. } = settings;
  19046. playlistLoader.on('loadedmetadata', () => {
  19047. const media = playlistLoader.media();
  19048. segmentLoader.playlist(media, requestOptions);
  19049. segmentLoader.track(mediaType.activeTrack()); // if the video is already playing, or if this isn't a live video and preload
  19050. // permits, start downloading segments
  19051. if (!tech.paused() || media.endList && tech.preload() !== 'none') {
  19052. segmentLoader.load();
  19053. }
  19054. });
  19055. playlistLoader.on('loadedplaylist', () => {
  19056. segmentLoader.playlist(playlistLoader.media(), requestOptions); // If the player isn't paused, ensure that the segment loader is running
  19057. if (!tech.paused()) {
  19058. segmentLoader.load();
  19059. }
  19060. });
  19061. playlistLoader.on('error', onError[type](type, settings));
  19062. }
  19063. };
  19064. const initialize = {
  19065. /**
  19066. * Setup PlaylistLoaders and AudioTracks for the audio groups
  19067. *
  19068. * @param {string} type
  19069. * MediaGroup type
  19070. * @param {Object} settings
  19071. * Object containing required information for media groups
  19072. * @function initialize.AUDIO
  19073. */
  19074. 'AUDIO': (type, settings) => {
  19075. const {
  19076. vhs,
  19077. sourceType,
  19078. segmentLoaders: {
  19079. [type]: segmentLoader
  19080. },
  19081. requestOptions,
  19082. main: {
  19083. mediaGroups
  19084. },
  19085. mediaTypes: {
  19086. [type]: {
  19087. groups,
  19088. tracks,
  19089. logger_
  19090. }
  19091. },
  19092. mainPlaylistLoader
  19093. } = settings;
  19094. const audioOnlyMain = isAudioOnly(mainPlaylistLoader.main); // force a default if we have none
  19095. if (!mediaGroups[type] || Object.keys(mediaGroups[type]).length === 0) {
  19096. mediaGroups[type] = {
  19097. main: {
  19098. default: {
  19099. default: true
  19100. }
  19101. }
  19102. };
  19103. if (audioOnlyMain) {
  19104. mediaGroups[type].main.default.playlists = mainPlaylistLoader.main.playlists;
  19105. }
  19106. }
  19107. for (const groupId in mediaGroups[type]) {
  19108. if (!groups[groupId]) {
  19109. groups[groupId] = [];
  19110. }
  19111. for (const variantLabel in mediaGroups[type][groupId]) {
  19112. let properties = mediaGroups[type][groupId][variantLabel];
  19113. let playlistLoader;
  19114. if (audioOnlyMain) {
  19115. logger_(`AUDIO group '${groupId}' label '${variantLabel}' is a main playlist`);
  19116. properties.isMainPlaylist = true;
  19117. playlistLoader = null; // if vhs-json was provided as the source, and the media playlist was resolved,
  19118. // use the resolved media playlist object
  19119. } else if (sourceType === 'vhs-json' && properties.playlists) {
  19120. playlistLoader = new PlaylistLoader(properties.playlists[0], vhs, requestOptions);
  19121. } else if (properties.resolvedUri) {
  19122. playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions); // TODO: dash isn't the only type with properties.playlists
  19123. // should we even have properties.playlists in this check.
  19124. } else if (properties.playlists && sourceType === 'dash') {
  19125. playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, mainPlaylistLoader);
  19126. } else {
  19127. // no resolvedUri means the audio is muxed with the video when using this
  19128. // audio track
  19129. playlistLoader = null;
  19130. }
  19131. properties = merge({
  19132. id: variantLabel,
  19133. playlistLoader
  19134. }, properties);
  19135. setupListeners[type](type, properties.playlistLoader, settings);
  19136. groups[groupId].push(properties);
  19137. if (typeof tracks[variantLabel] === 'undefined') {
  19138. const track = new videojs__default["default"].AudioTrack({
  19139. id: variantLabel,
  19140. kind: audioTrackKind_(properties),
  19141. enabled: false,
  19142. language: properties.language,
  19143. default: properties.default,
  19144. label: variantLabel
  19145. });
  19146. tracks[variantLabel] = track;
  19147. }
  19148. }
  19149. } // setup single error event handler for the segment loader
  19150. segmentLoader.on('error', onError[type](type, settings));
  19151. },
  19152. /**
  19153. * Setup PlaylistLoaders and TextTracks for the subtitle groups
  19154. *
  19155. * @param {string} type
  19156. * MediaGroup type
  19157. * @param {Object} settings
  19158. * Object containing required information for media groups
  19159. * @function initialize.SUBTITLES
  19160. */
  19161. 'SUBTITLES': (type, settings) => {
  19162. const {
  19163. tech,
  19164. vhs,
  19165. sourceType,
  19166. segmentLoaders: {
  19167. [type]: segmentLoader
  19168. },
  19169. requestOptions,
  19170. main: {
  19171. mediaGroups
  19172. },
  19173. mediaTypes: {
  19174. [type]: {
  19175. groups,
  19176. tracks
  19177. }
  19178. },
  19179. mainPlaylistLoader
  19180. } = settings;
  19181. for (const groupId in mediaGroups[type]) {
  19182. if (!groups[groupId]) {
  19183. groups[groupId] = [];
  19184. }
  19185. for (const variantLabel in mediaGroups[type][groupId]) {
  19186. if (!vhs.options_.useForcedSubtitles && mediaGroups[type][groupId][variantLabel].forced) {
  19187. // Subtitle playlists with the forced attribute are not selectable in Safari.
  19188. // According to Apple's HLS Authoring Specification:
  19189. // If content has forced subtitles and regular subtitles in a given language,
  19190. // the regular subtitles track in that language MUST contain both the forced
  19191. // subtitles and the regular subtitles for that language.
  19192. // Because of this requirement and that Safari does not add forced subtitles,
  19193. // forced subtitles are skipped here to maintain consistent experience across
  19194. // all platforms
  19195. continue;
  19196. }
  19197. let properties = mediaGroups[type][groupId][variantLabel];
  19198. let playlistLoader;
  19199. if (sourceType === 'hls') {
  19200. playlistLoader = new PlaylistLoader(properties.resolvedUri, vhs, requestOptions);
  19201. } else if (sourceType === 'dash') {
  19202. const playlists = properties.playlists.filter(p => p.excludeUntil !== Infinity);
  19203. if (!playlists.length) {
  19204. return;
  19205. }
  19206. playlistLoader = new DashPlaylistLoader(properties.playlists[0], vhs, requestOptions, mainPlaylistLoader);
  19207. } else if (sourceType === 'vhs-json') {
  19208. playlistLoader = new PlaylistLoader( // if the vhs-json object included the media playlist, use the media playlist
  19209. // as provided, otherwise use the resolved URI to load the playlist
  19210. properties.playlists ? properties.playlists[0] : properties.resolvedUri, vhs, requestOptions);
  19211. }
  19212. properties = merge({
  19213. id: variantLabel,
  19214. playlistLoader
  19215. }, properties);
  19216. setupListeners[type](type, properties.playlistLoader, settings);
  19217. groups[groupId].push(properties);
  19218. if (typeof tracks[variantLabel] === 'undefined') {
  19219. const track = tech.addRemoteTextTrack({
  19220. id: variantLabel,
  19221. kind: 'subtitles',
  19222. default: properties.default && properties.autoselect,
  19223. language: properties.language,
  19224. label: variantLabel
  19225. }, false).track;
  19226. tracks[variantLabel] = track;
  19227. }
  19228. }
  19229. } // setup single error event handler for the segment loader
  19230. segmentLoader.on('error', onError[type](type, settings));
  19231. },
  19232. /**
  19233. * Setup TextTracks for the closed-caption groups
  19234. *
  19235. * @param {String} type
  19236. * MediaGroup type
  19237. * @param {Object} settings
  19238. * Object containing required information for media groups
  19239. * @function initialize['CLOSED-CAPTIONS']
  19240. */
  19241. 'CLOSED-CAPTIONS': (type, settings) => {
  19242. const {
  19243. tech,
  19244. main: {
  19245. mediaGroups
  19246. },
  19247. mediaTypes: {
  19248. [type]: {
  19249. groups,
  19250. tracks
  19251. }
  19252. }
  19253. } = settings;
  19254. for (const groupId in mediaGroups[type]) {
  19255. if (!groups[groupId]) {
  19256. groups[groupId] = [];
  19257. }
  19258. for (const variantLabel in mediaGroups[type][groupId]) {
  19259. const properties = mediaGroups[type][groupId][variantLabel]; // Look for either 608 (CCn) or 708 (SERVICEn) caption services
  19260. if (!/^(?:CC|SERVICE)/.test(properties.instreamId)) {
  19261. continue;
  19262. }
  19263. const captionServices = tech.options_.vhs && tech.options_.vhs.captionServices || {};
  19264. let newProps = {
  19265. label: variantLabel,
  19266. language: properties.language,
  19267. instreamId: properties.instreamId,
  19268. default: properties.default && properties.autoselect
  19269. };
  19270. if (captionServices[newProps.instreamId]) {
  19271. newProps = merge(newProps, captionServices[newProps.instreamId]);
  19272. }
  19273. if (newProps.default === undefined) {
  19274. delete newProps.default;
  19275. } // No PlaylistLoader is required for Closed-Captions because the captions are
  19276. // embedded within the video stream
  19277. groups[groupId].push(merge({
  19278. id: variantLabel
  19279. }, properties));
  19280. if (typeof tracks[variantLabel] === 'undefined') {
  19281. const track = tech.addRemoteTextTrack({
  19282. id: newProps.instreamId,
  19283. kind: 'captions',
  19284. default: newProps.default,
  19285. language: newProps.language,
  19286. label: newProps.label
  19287. }, false).track;
  19288. tracks[variantLabel] = track;
  19289. }
  19290. }
  19291. }
  19292. }
  19293. };
  19294. const groupMatch = (list, media) => {
  19295. for (let i = 0; i < list.length; i++) {
  19296. if (playlistMatch(media, list[i])) {
  19297. return true;
  19298. }
  19299. if (list[i].playlists && groupMatch(list[i].playlists, media)) {
  19300. return true;
  19301. }
  19302. }
  19303. return false;
  19304. };
  19305. /**
  19306. * Returns a function used to get the active group of the provided type
  19307. *
  19308. * @param {string} type
  19309. * MediaGroup type
  19310. * @param {Object} settings
  19311. * Object containing required information for media groups
  19312. * @return {Function}
  19313. * Function that returns the active media group for the provided type. Takes an
  19314. * optional parameter {TextTrack} track. If no track is provided, a list of all
  19315. * variants in the group, otherwise the variant corresponding to the provided
  19316. * track is returned.
  19317. * @function activeGroup
  19318. */
  19319. const activeGroup = (type, settings) => track => {
  19320. const {
  19321. mainPlaylistLoader,
  19322. mediaTypes: {
  19323. [type]: {
  19324. groups
  19325. }
  19326. }
  19327. } = settings;
  19328. const media = mainPlaylistLoader.media();
  19329. if (!media) {
  19330. return null;
  19331. }
  19332. let variants = null; // set to variants to main media active group
  19333. if (media.attributes[type]) {
  19334. variants = groups[media.attributes[type]];
  19335. }
  19336. const groupKeys = Object.keys(groups);
  19337. if (!variants) {
  19338. // find the mainPlaylistLoader media
  19339. // that is in a media group if we are dealing
  19340. // with audio only
  19341. if (type === 'AUDIO' && groupKeys.length > 1 && isAudioOnly(settings.main)) {
  19342. for (let i = 0; i < groupKeys.length; i++) {
  19343. const groupPropertyList = groups[groupKeys[i]];
  19344. if (groupMatch(groupPropertyList, media)) {
  19345. variants = groupPropertyList;
  19346. break;
  19347. }
  19348. } // use the main group if it exists
  19349. } else if (groups.main) {
  19350. variants = groups.main; // only one group, use that one
  19351. } else if (groupKeys.length === 1) {
  19352. variants = groups[groupKeys[0]];
  19353. }
  19354. }
  19355. if (typeof track === 'undefined') {
  19356. return variants;
  19357. }
  19358. if (track === null || !variants) {
  19359. // An active track was specified so a corresponding group is expected. track === null
  19360. // means no track is currently active so there is no corresponding group
  19361. return null;
  19362. }
  19363. return variants.filter(props => props.id === track.id)[0] || null;
  19364. };
  19365. const activeTrack = {
  19366. /**
  19367. * Returns a function used to get the active track of type provided
  19368. *
  19369. * @param {string} type
  19370. * MediaGroup type
  19371. * @param {Object} settings
  19372. * Object containing required information for media groups
  19373. * @return {Function}
  19374. * Function that returns the active media track for the provided type. Returns
  19375. * null if no track is active
  19376. * @function activeTrack.AUDIO
  19377. */
  19378. AUDIO: (type, settings) => () => {
  19379. const {
  19380. mediaTypes: {
  19381. [type]: {
  19382. tracks
  19383. }
  19384. }
  19385. } = settings;
  19386. for (const id in tracks) {
  19387. if (tracks[id].enabled) {
  19388. return tracks[id];
  19389. }
  19390. }
  19391. return null;
  19392. },
  19393. /**
  19394. * Returns a function used to get the active track of type provided
  19395. *
  19396. * @param {string} type
  19397. * MediaGroup type
  19398. * @param {Object} settings
  19399. * Object containing required information for media groups
  19400. * @return {Function}
  19401. * Function that returns the active media track for the provided type. Returns
  19402. * null if no track is active
  19403. * @function activeTrack.SUBTITLES
  19404. */
  19405. SUBTITLES: (type, settings) => () => {
  19406. const {
  19407. mediaTypes: {
  19408. [type]: {
  19409. tracks
  19410. }
  19411. }
  19412. } = settings;
  19413. for (const id in tracks) {
  19414. if (tracks[id].mode === 'showing' || tracks[id].mode === 'hidden') {
  19415. return tracks[id];
  19416. }
  19417. }
  19418. return null;
  19419. }
  19420. };
  19421. const getActiveGroup = (type, {
  19422. mediaTypes
  19423. }) => () => {
  19424. const activeTrack_ = mediaTypes[type].activeTrack();
  19425. if (!activeTrack_) {
  19426. return null;
  19427. }
  19428. return mediaTypes[type].activeGroup(activeTrack_);
  19429. };
  19430. /**
  19431. * Setup PlaylistLoaders and Tracks for media groups (Audio, Subtitles,
  19432. * Closed-Captions) specified in the main manifest.
  19433. *
  19434. * @param {Object} settings
  19435. * Object containing required information for setting up the media groups
  19436. * @param {Tech} settings.tech
  19437. * The tech of the player
  19438. * @param {Object} settings.requestOptions
  19439. * XHR request options used by the segment loaders
  19440. * @param {PlaylistLoader} settings.mainPlaylistLoader
  19441. * PlaylistLoader for the main source
  19442. * @param {VhsHandler} settings.vhs
  19443. * VHS SourceHandler
  19444. * @param {Object} settings.main
  19445. * The parsed main manifest
  19446. * @param {Object} settings.mediaTypes
  19447. * Object to store the loaders, tracks, and utility methods for each media type
  19448. * @param {Function} settings.excludePlaylist
  19449. * Excludes the current rendition and forces a rendition switch.
  19450. * @function setupMediaGroups
  19451. */
  19452. const setupMediaGroups = settings => {
  19453. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
  19454. initialize[type](type, settings);
  19455. });
  19456. const {
  19457. mediaTypes,
  19458. mainPlaylistLoader,
  19459. tech,
  19460. vhs,
  19461. segmentLoaders: {
  19462. ['AUDIO']: audioSegmentLoader,
  19463. main: mainSegmentLoader
  19464. }
  19465. } = settings; // setup active group and track getters and change event handlers
  19466. ['AUDIO', 'SUBTITLES'].forEach(type => {
  19467. mediaTypes[type].activeGroup = activeGroup(type, settings);
  19468. mediaTypes[type].activeTrack = activeTrack[type](type, settings);
  19469. mediaTypes[type].onGroupChanged = onGroupChanged(type, settings);
  19470. mediaTypes[type].onGroupChanging = onGroupChanging(type, settings);
  19471. mediaTypes[type].onTrackChanged = onTrackChanged(type, settings);
  19472. mediaTypes[type].getActiveGroup = getActiveGroup(type, settings);
  19473. }); // DO NOT enable the default subtitle or caption track.
  19474. // DO enable the default audio track
  19475. const audioGroup = mediaTypes.AUDIO.activeGroup();
  19476. if (audioGroup) {
  19477. const groupId = (audioGroup.filter(group => group.default)[0] || audioGroup[0]).id;
  19478. mediaTypes.AUDIO.tracks[groupId].enabled = true;
  19479. mediaTypes.AUDIO.onGroupChanged();
  19480. mediaTypes.AUDIO.onTrackChanged();
  19481. const activeAudioGroup = mediaTypes.AUDIO.getActiveGroup(); // a similar check for handling setAudio on each loader is run again each time the
  19482. // track is changed, but needs to be handled here since the track may not be considered
  19483. // changed on the first call to onTrackChanged
  19484. if (!activeAudioGroup.playlistLoader) {
  19485. // either audio is muxed with video or the stream is audio only
  19486. mainSegmentLoader.setAudio(true);
  19487. } else {
  19488. // audio is demuxed
  19489. mainSegmentLoader.setAudio(false);
  19490. audioSegmentLoader.setAudio(true);
  19491. }
  19492. }
  19493. mainPlaylistLoader.on('mediachange', () => {
  19494. ['AUDIO', 'SUBTITLES'].forEach(type => mediaTypes[type].onGroupChanged());
  19495. });
  19496. mainPlaylistLoader.on('mediachanging', () => {
  19497. ['AUDIO', 'SUBTITLES'].forEach(type => mediaTypes[type].onGroupChanging());
  19498. }); // custom audio track change event handler for usage event
  19499. const onAudioTrackChanged = () => {
  19500. mediaTypes.AUDIO.onTrackChanged();
  19501. tech.trigger({
  19502. type: 'usage',
  19503. name: 'vhs-audio-change'
  19504. });
  19505. };
  19506. tech.audioTracks().addEventListener('change', onAudioTrackChanged);
  19507. tech.remoteTextTracks().addEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
  19508. vhs.on('dispose', () => {
  19509. tech.audioTracks().removeEventListener('change', onAudioTrackChanged);
  19510. tech.remoteTextTracks().removeEventListener('change', mediaTypes.SUBTITLES.onTrackChanged);
  19511. }); // clear existing audio tracks and add the ones we just created
  19512. tech.clearTracks('audio');
  19513. for (const id in mediaTypes.AUDIO.tracks) {
  19514. tech.audioTracks().addTrack(mediaTypes.AUDIO.tracks[id]);
  19515. }
  19516. };
  19517. /**
  19518. * Creates skeleton object used to store the loaders, tracks, and utility methods for each
  19519. * media type
  19520. *
  19521. * @return {Object}
  19522. * Object to store the loaders, tracks, and utility methods for each media type
  19523. * @function createMediaTypes
  19524. */
  19525. const createMediaTypes = () => {
  19526. const mediaTypes = {};
  19527. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
  19528. mediaTypes[type] = {
  19529. groups: {},
  19530. tracks: {},
  19531. activePlaylistLoader: null,
  19532. activeGroup: noop,
  19533. activeTrack: noop,
  19534. getActiveGroup: noop,
  19535. onGroupChanged: noop,
  19536. onTrackChanged: noop,
  19537. lastTrack_: null,
  19538. logger_: logger(`MediaGroups[${type}]`)
  19539. };
  19540. });
  19541. return mediaTypes;
  19542. };
  19543. /**
  19544. * A utility class for setting properties and maintaining the state of the content steering manifest.
  19545. *
  19546. * Content Steering manifest format:
  19547. * VERSION: number (required) currently only version 1 is supported.
  19548. * TTL: number in seconds (optional) until the next content steering manifest reload.
  19549. * RELOAD-URI: string (optional) uri to fetch the next content steering manifest.
  19550. * SERVICE-LOCATION-PRIORITY or PATHWAY-PRIORITY a non empty array of unique string values.
  19551. * PATHWAY-CLONES: array (optional) (HLS only) pathway clone objects to copy from other playlists.
  19552. */
  19553. class SteeringManifest {
  19554. constructor() {
  19555. this.priority_ = [];
  19556. this.pathwayClones_ = new Map();
  19557. }
  19558. set version(number) {
  19559. // Only version 1 is currently supported for both DASH and HLS.
  19560. if (number === 1) {
  19561. this.version_ = number;
  19562. }
  19563. }
  19564. set ttl(seconds) {
  19565. // TTL = time-to-live, default = 300 seconds.
  19566. this.ttl_ = seconds || 300;
  19567. }
  19568. set reloadUri(uri) {
  19569. if (uri) {
  19570. // reload URI can be relative to the previous reloadUri.
  19571. this.reloadUri_ = resolveUrl(this.reloadUri_, uri);
  19572. }
  19573. }
  19574. set priority(array) {
  19575. // priority must be non-empty and unique values.
  19576. if (array && array.length) {
  19577. this.priority_ = array;
  19578. }
  19579. }
  19580. set pathwayClones(array) {
  19581. // pathwayClones must be non-empty.
  19582. if (array && array.length) {
  19583. this.pathwayClones_ = new Map(array.map(clone => [clone.ID, clone]));
  19584. }
  19585. }
  19586. get version() {
  19587. return this.version_;
  19588. }
  19589. get ttl() {
  19590. return this.ttl_;
  19591. }
  19592. get reloadUri() {
  19593. return this.reloadUri_;
  19594. }
  19595. get priority() {
  19596. return this.priority_;
  19597. }
  19598. get pathwayClones() {
  19599. return this.pathwayClones_;
  19600. }
  19601. }
  19602. /**
  19603. * This class represents a content steering manifest and associated state. See both HLS and DASH specifications.
  19604. * HLS: https://developer.apple.com/streaming/HLSContentSteeringSpecification.pdf and
  19605. * https://datatracker.ietf.org/doc/draft-pantos-hls-rfc8216bis/ section 4.4.6.6.
  19606. * DASH: https://dashif.org/docs/DASH-IF-CTS-00XX-Content-Steering-Community-Review.pdf
  19607. *
  19608. * @param {function} xhr for making a network request from the browser.
  19609. * @param {function} bandwidth for fetching the current bandwidth from the main segment loader.
  19610. */
  19611. class ContentSteeringController extends videojs__default["default"].EventTarget {
  19612. constructor(xhr, bandwidth) {
  19613. super();
  19614. this.currentPathway = null;
  19615. this.defaultPathway = null;
  19616. this.queryBeforeStart = false;
  19617. this.availablePathways_ = new Set();
  19618. this.steeringManifest = new SteeringManifest();
  19619. this.proxyServerUrl_ = null;
  19620. this.manifestType_ = null;
  19621. this.ttlTimeout_ = null;
  19622. this.request_ = null;
  19623. this.currentPathwayClones = new Map();
  19624. this.nextPathwayClones = new Map();
  19625. this.excludedSteeringManifestURLs = new Set();
  19626. this.logger_ = logger('Content Steering');
  19627. this.xhr_ = xhr;
  19628. this.getBandwidth_ = bandwidth;
  19629. }
  19630. /**
  19631. * Assigns the content steering tag properties to the steering controller
  19632. *
  19633. * @param {string} baseUrl the baseURL from the main manifest for resolving the steering manifest url
  19634. * @param {Object} steeringTag the content steering tag from the main manifest
  19635. */
  19636. assignTagProperties(baseUrl, steeringTag) {
  19637. this.manifestType_ = steeringTag.serverUri ? 'HLS' : 'DASH'; // serverUri is HLS serverURL is DASH
  19638. const steeringUri = steeringTag.serverUri || steeringTag.serverURL;
  19639. if (!steeringUri) {
  19640. this.logger_(`steering manifest URL is ${steeringUri}, cannot request steering manifest.`);
  19641. this.trigger('error');
  19642. return;
  19643. } // Content steering manifests can be encoded as a data URI. We can decode, parse and return early if that's the case.
  19644. if (steeringUri.startsWith('data:')) {
  19645. this.decodeDataUriManifest_(steeringUri.substring(steeringUri.indexOf(',') + 1));
  19646. return;
  19647. } // reloadUri is the resolution of the main manifest URL and steering URL.
  19648. this.steeringManifest.reloadUri = resolveUrl(baseUrl, steeringUri); // pathwayId is HLS defaultServiceLocation is DASH
  19649. this.defaultPathway = steeringTag.pathwayId || steeringTag.defaultServiceLocation; // currently only DASH supports the following properties on <ContentSteering> tags.
  19650. this.queryBeforeStart = steeringTag.queryBeforeStart;
  19651. this.proxyServerUrl_ = steeringTag.proxyServerURL; // trigger a steering event if we have a pathway from the content steering tag.
  19652. // this tells VHS which segment pathway to start with.
  19653. // If queryBeforeStart is true we need to wait for the steering manifest response.
  19654. if (this.defaultPathway && !this.queryBeforeStart) {
  19655. this.trigger('content-steering');
  19656. }
  19657. }
  19658. /**
  19659. * Requests the content steering manifest and parse the response. This should only be called after
  19660. * assignTagProperties was called with a content steering tag.
  19661. *
  19662. * @param {string} initialUri The optional uri to make the request with.
  19663. * If set, the request should be made with exactly what is passed in this variable.
  19664. * This scenario should only happen once on initalization.
  19665. */
  19666. requestSteeringManifest(initial) {
  19667. const reloadUri = this.steeringManifest.reloadUri;
  19668. if (!reloadUri) {
  19669. return;
  19670. } // We currently don't support passing MPD query parameters directly to the content steering URL as this requires
  19671. // ExtUrlQueryInfo tag support. See the DASH content steering spec section 8.1.
  19672. // This request URI accounts for manifest URIs that have been excluded.
  19673. const uri = initial ? reloadUri : this.getRequestURI(reloadUri); // If there are no valid manifest URIs, we should stop content steering.
  19674. if (!uri) {
  19675. this.logger_('No valid content steering manifest URIs. Stopping content steering.');
  19676. this.trigger('error');
  19677. this.dispose();
  19678. return;
  19679. }
  19680. this.request_ = this.xhr_({
  19681. uri
  19682. }, (error, errorInfo) => {
  19683. if (error) {
  19684. // If the client receives HTTP 410 Gone in response to a manifest request,
  19685. // it MUST NOT issue another request for that URI for the remainder of the
  19686. // playback session. It MAY continue to use the most-recently obtained set
  19687. // of Pathways.
  19688. if (errorInfo.status === 410) {
  19689. this.logger_(`manifest request 410 ${error}.`);
  19690. this.logger_(`There will be no more content steering requests to ${uri} this session.`);
  19691. this.excludedSteeringManifestURLs.add(uri);
  19692. return;
  19693. } // If the client receives HTTP 429 Too Many Requests with a Retry-After
  19694. // header in response to a manifest request, it SHOULD wait until the time
  19695. // specified by the Retry-After header to reissue the request.
  19696. if (errorInfo.status === 429) {
  19697. const retrySeconds = errorInfo.responseHeaders['retry-after'];
  19698. this.logger_(`manifest request 429 ${error}.`);
  19699. this.logger_(`content steering will retry in ${retrySeconds} seconds.`);
  19700. this.startTTLTimeout_(parseInt(retrySeconds, 10));
  19701. return;
  19702. } // If the Steering Manifest cannot be loaded and parsed correctly, the
  19703. // client SHOULD continue to use the previous values and attempt to reload
  19704. // it after waiting for the previously-specified TTL (or 5 minutes if
  19705. // none).
  19706. this.logger_(`manifest failed to load ${error}.`);
  19707. this.startTTLTimeout_();
  19708. return;
  19709. }
  19710. const steeringManifestJson = JSON.parse(this.request_.responseText);
  19711. this.assignSteeringProperties_(steeringManifestJson);
  19712. this.startTTLTimeout_();
  19713. });
  19714. }
  19715. /**
  19716. * Set the proxy server URL and add the steering manifest url as a URI encoded parameter.
  19717. *
  19718. * @param {string} steeringUrl the steering manifest url
  19719. * @return the steering manifest url to a proxy server with all parameters set
  19720. */
  19721. setProxyServerUrl_(steeringUrl) {
  19722. const steeringUrlObject = new window__default["default"].URL(steeringUrl);
  19723. const proxyServerUrlObject = new window__default["default"].URL(this.proxyServerUrl_);
  19724. proxyServerUrlObject.searchParams.set('url', encodeURI(steeringUrlObject.toString()));
  19725. return this.setSteeringParams_(proxyServerUrlObject.toString());
  19726. }
  19727. /**
  19728. * Decodes and parses the data uri encoded steering manifest
  19729. *
  19730. * @param {string} dataUri the data uri to be decoded and parsed.
  19731. */
  19732. decodeDataUriManifest_(dataUri) {
  19733. const steeringManifestJson = JSON.parse(window__default["default"].atob(dataUri));
  19734. this.assignSteeringProperties_(steeringManifestJson);
  19735. }
  19736. /**
  19737. * Set the HLS or DASH content steering manifest request query parameters. For example:
  19738. * _HLS_pathway="<CURRENT-PATHWAY-ID>" and _HLS_throughput=<THROUGHPUT>
  19739. * _DASH_pathway and _DASH_throughput
  19740. *
  19741. * @param {string} uri to add content steering server parameters to.
  19742. * @return a new uri as a string with the added steering query parameters.
  19743. */
  19744. setSteeringParams_(url) {
  19745. const urlObject = new window__default["default"].URL(url);
  19746. const path = this.getPathway();
  19747. const networkThroughput = this.getBandwidth_();
  19748. if (path) {
  19749. const pathwayKey = `_${this.manifestType_}_pathway`;
  19750. urlObject.searchParams.set(pathwayKey, path);
  19751. }
  19752. if (networkThroughput) {
  19753. const throughputKey = `_${this.manifestType_}_throughput`;
  19754. urlObject.searchParams.set(throughputKey, networkThroughput);
  19755. }
  19756. return urlObject.toString();
  19757. }
  19758. /**
  19759. * Assigns the current steering manifest properties and to the SteeringManifest object
  19760. *
  19761. * @param {Object} steeringJson the raw JSON steering manifest
  19762. */
  19763. assignSteeringProperties_(steeringJson) {
  19764. this.steeringManifest.version = steeringJson.VERSION;
  19765. if (!this.steeringManifest.version) {
  19766. this.logger_(`manifest version is ${steeringJson.VERSION}, which is not supported.`);
  19767. this.trigger('error');
  19768. return;
  19769. }
  19770. this.steeringManifest.ttl = steeringJson.TTL;
  19771. this.steeringManifest.reloadUri = steeringJson['RELOAD-URI']; // HLS = PATHWAY-PRIORITY required. DASH = SERVICE-LOCATION-PRIORITY optional
  19772. this.steeringManifest.priority = steeringJson['PATHWAY-PRIORITY'] || steeringJson['SERVICE-LOCATION-PRIORITY']; // Pathway clones to be created/updated in HLS.
  19773. // See section 7.2 https://datatracker.ietf.org/doc/draft-pantos-hls-rfc8216bis/
  19774. this.steeringManifest.pathwayClones = steeringJson['PATHWAY-CLONES'];
  19775. this.nextPathwayClones = this.steeringManifest.pathwayClones; // 1. apply first pathway from the array.
  19776. // 2. if first pathway doesn't exist in manifest, try next pathway.
  19777. // a. if all pathways are exhausted, ignore the steering manifest priority.
  19778. // 3. if segments fail from an established pathway, try all variants/renditions, then exclude the failed pathway.
  19779. // a. exclude a pathway for a minimum of the last TTL duration. Meaning, from the next steering response,
  19780. // the excluded pathway will be ignored.
  19781. // See excludePathway usage in excludePlaylist().
  19782. // If there are no available pathways, we need to stop content steering.
  19783. if (!this.availablePathways_.size) {
  19784. this.logger_('There are no available pathways for content steering. Ending content steering.');
  19785. this.trigger('error');
  19786. this.dispose();
  19787. }
  19788. const chooseNextPathway = pathwaysByPriority => {
  19789. for (const path of pathwaysByPriority) {
  19790. if (this.availablePathways_.has(path)) {
  19791. return path;
  19792. }
  19793. } // If no pathway matches, ignore the manifest and choose the first available.
  19794. return [...this.availablePathways_][0];
  19795. };
  19796. const nextPathway = chooseNextPathway(this.steeringManifest.priority);
  19797. if (this.currentPathway !== nextPathway) {
  19798. this.currentPathway = nextPathway;
  19799. this.trigger('content-steering');
  19800. }
  19801. }
  19802. /**
  19803. * Returns the pathway to use for steering decisions
  19804. *
  19805. * @return {string} returns the current pathway or the default
  19806. */
  19807. getPathway() {
  19808. return this.currentPathway || this.defaultPathway;
  19809. }
  19810. /**
  19811. * Chooses the manifest request URI based on proxy URIs and server URLs.
  19812. * Also accounts for exclusion on certain manifest URIs.
  19813. *
  19814. * @param {string} reloadUri the base uri before parameters
  19815. *
  19816. * @return {string} the final URI for the request to the manifest server.
  19817. */
  19818. getRequestURI(reloadUri) {
  19819. if (!reloadUri) {
  19820. return null;
  19821. }
  19822. const isExcluded = uri => this.excludedSteeringManifestURLs.has(uri);
  19823. if (this.proxyServerUrl_) {
  19824. const proxyURI = this.setProxyServerUrl_(reloadUri);
  19825. if (!isExcluded(proxyURI)) {
  19826. return proxyURI;
  19827. }
  19828. }
  19829. const steeringURI = this.setSteeringParams_(reloadUri);
  19830. if (!isExcluded(steeringURI)) {
  19831. return steeringURI;
  19832. } // Return nothing if all valid manifest URIs are excluded.
  19833. return null;
  19834. }
  19835. /**
  19836. * Start the timeout for re-requesting the steering manifest at the TTL interval.
  19837. *
  19838. * @param {number} ttl time in seconds of the timeout. Defaults to the
  19839. * ttl interval in the steering manifest
  19840. */
  19841. startTTLTimeout_(ttl = this.steeringManifest.ttl) {
  19842. // 300 (5 minutes) is the default value.
  19843. const ttlMS = ttl * 1000;
  19844. this.ttlTimeout_ = window__default["default"].setTimeout(() => {
  19845. this.requestSteeringManifest();
  19846. }, ttlMS);
  19847. }
  19848. /**
  19849. * Clear the TTL timeout if necessary.
  19850. */
  19851. clearTTLTimeout_() {
  19852. window__default["default"].clearTimeout(this.ttlTimeout_);
  19853. this.ttlTimeout_ = null;
  19854. }
  19855. /**
  19856. * aborts any current steering xhr and sets the current request object to null
  19857. */
  19858. abort() {
  19859. if (this.request_) {
  19860. this.request_.abort();
  19861. }
  19862. this.request_ = null;
  19863. }
  19864. /**
  19865. * aborts steering requests clears the ttl timeout and resets all properties.
  19866. */
  19867. dispose() {
  19868. this.off('content-steering');
  19869. this.off('error');
  19870. this.abort();
  19871. this.clearTTLTimeout_();
  19872. this.currentPathway = null;
  19873. this.defaultPathway = null;
  19874. this.queryBeforeStart = null;
  19875. this.proxyServerUrl_ = null;
  19876. this.manifestType_ = null;
  19877. this.ttlTimeout_ = null;
  19878. this.request_ = null;
  19879. this.excludedSteeringManifestURLs = new Set();
  19880. this.availablePathways_ = new Set();
  19881. this.steeringManifest = new SteeringManifest();
  19882. }
  19883. /**
  19884. * adds a pathway to the available pathways set
  19885. *
  19886. * @param {string} pathway the pathway string to add
  19887. */
  19888. addAvailablePathway(pathway) {
  19889. if (pathway) {
  19890. this.availablePathways_.add(pathway);
  19891. }
  19892. }
  19893. /**
  19894. * Clears all pathways from the available pathways set
  19895. */
  19896. clearAvailablePathways() {
  19897. this.availablePathways_.clear();
  19898. }
  19899. /**
  19900. * Removes a pathway from the available pathways set.
  19901. */
  19902. excludePathway(pathway) {
  19903. return this.availablePathways_.delete(pathway);
  19904. }
  19905. /**
  19906. * Checks the refreshed DASH manifest content steering tag for changes.
  19907. *
  19908. * @param {string} baseURL new steering tag on DASH manifest refresh
  19909. * @param {Object} newTag the new tag to check for changes
  19910. * @return a true or false whether the new tag has different values
  19911. */
  19912. didDASHTagChange(baseURL, newTag) {
  19913. return !newTag && this.steeringManifest.reloadUri || newTag && (resolveUrl(baseURL, newTag.serverURL) !== this.steeringManifest.reloadUri || newTag.defaultServiceLocation !== this.defaultPathway || newTag.queryBeforeStart !== this.queryBeforeStart || newTag.proxyServerURL !== this.proxyServerUrl_);
  19914. }
  19915. getAvailablePathways() {
  19916. return this.availablePathways_;
  19917. }
  19918. }
  19919. /**
  19920. * @file playlist-controller.js
  19921. */
  19922. const ABORT_EARLY_EXCLUSION_SECONDS = 10;
  19923. let Vhs$1; // SegmentLoader stats that need to have each loader's
  19924. // values summed to calculate the final value
  19925. const loaderStats = ['mediaRequests', 'mediaRequestsAborted', 'mediaRequestsTimedout', 'mediaRequestsErrored', 'mediaTransferDuration', 'mediaBytesTransferred', 'mediaAppends'];
  19926. const sumLoaderStat = function (stat) {
  19927. return this.audioSegmentLoader_[stat] + this.mainSegmentLoader_[stat];
  19928. };
  19929. const shouldSwitchToMedia = function ({
  19930. currentPlaylist,
  19931. buffered,
  19932. currentTime,
  19933. nextPlaylist,
  19934. bufferLowWaterLine,
  19935. bufferHighWaterLine,
  19936. duration,
  19937. bufferBasedABR,
  19938. log
  19939. }) {
  19940. // we have no other playlist to switch to
  19941. if (!nextPlaylist) {
  19942. videojs__default["default"].log.warn('We received no playlist to switch to. Please check your stream.');
  19943. return false;
  19944. }
  19945. const sharedLogLine = `allowing switch ${currentPlaylist && currentPlaylist.id || 'null'} -> ${nextPlaylist.id}`;
  19946. if (!currentPlaylist) {
  19947. log(`${sharedLogLine} as current playlist is not set`);
  19948. return true;
  19949. } // no need to switch if playlist is the same
  19950. if (nextPlaylist.id === currentPlaylist.id) {
  19951. return false;
  19952. } // determine if current time is in a buffered range.
  19953. const isBuffered = Boolean(findRange(buffered, currentTime).length); // If the playlist is live, then we want to not take low water line into account.
  19954. // This is because in LIVE, the player plays 3 segments from the end of the
  19955. // playlist, and if `BUFFER_LOW_WATER_LINE` is greater than the duration availble
  19956. // in those segments, a viewer will never experience a rendition upswitch.
  19957. if (!currentPlaylist.endList) {
  19958. // For LLHLS live streams, don't switch renditions before playback has started, as it almost
  19959. // doubles the time to first playback.
  19960. if (!isBuffered && typeof currentPlaylist.partTargetDuration === 'number') {
  19961. log(`not ${sharedLogLine} as current playlist is live llhls, but currentTime isn't in buffered.`);
  19962. return false;
  19963. }
  19964. log(`${sharedLogLine} as current playlist is live`);
  19965. return true;
  19966. }
  19967. const forwardBuffer = timeAheadOf(buffered, currentTime);
  19968. const maxBufferLowWaterLine = bufferBasedABR ? Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE : Config.MAX_BUFFER_LOW_WATER_LINE; // For the same reason as LIVE, we ignore the low water line when the VOD
  19969. // duration is below the max potential low water line
  19970. if (duration < maxBufferLowWaterLine) {
  19971. log(`${sharedLogLine} as duration < max low water line (${duration} < ${maxBufferLowWaterLine})`);
  19972. return true;
  19973. }
  19974. const nextBandwidth = nextPlaylist.attributes.BANDWIDTH;
  19975. const currBandwidth = currentPlaylist.attributes.BANDWIDTH; // when switching down, if our buffer is lower than the high water line,
  19976. // we can switch down
  19977. if (nextBandwidth < currBandwidth && (!bufferBasedABR || forwardBuffer < bufferHighWaterLine)) {
  19978. let logLine = `${sharedLogLine} as next bandwidth < current bandwidth (${nextBandwidth} < ${currBandwidth})`;
  19979. if (bufferBasedABR) {
  19980. logLine += ` and forwardBuffer < bufferHighWaterLine (${forwardBuffer} < ${bufferHighWaterLine})`;
  19981. }
  19982. log(logLine);
  19983. return true;
  19984. } // and if our buffer is higher than the low water line,
  19985. // we can switch up
  19986. if ((!bufferBasedABR || nextBandwidth > currBandwidth) && forwardBuffer >= bufferLowWaterLine) {
  19987. let logLine = `${sharedLogLine} as forwardBuffer >= bufferLowWaterLine (${forwardBuffer} >= ${bufferLowWaterLine})`;
  19988. if (bufferBasedABR) {
  19989. logLine += ` and next bandwidth > current bandwidth (${nextBandwidth} > ${currBandwidth})`;
  19990. }
  19991. log(logLine);
  19992. return true;
  19993. }
  19994. log(`not ${sharedLogLine} as no switching criteria met`);
  19995. return false;
  19996. };
  19997. /**
  19998. * the main playlist controller controller all interactons
  19999. * between playlists and segmentloaders. At this time this mainly
  20000. * involves a main playlist and a series of audio playlists
  20001. * if they are available
  20002. *
  20003. * @class PlaylistController
  20004. * @extends videojs.EventTarget
  20005. */
  20006. class PlaylistController extends videojs__default["default"].EventTarget {
  20007. constructor(options) {
  20008. super();
  20009. const {
  20010. src,
  20011. withCredentials,
  20012. tech,
  20013. bandwidth,
  20014. externVhs,
  20015. useCueTags,
  20016. playlistExclusionDuration,
  20017. enableLowInitialPlaylist,
  20018. sourceType,
  20019. cacheEncryptionKeys,
  20020. bufferBasedABR,
  20021. leastPixelDiffSelector,
  20022. captionServices
  20023. } = options;
  20024. if (!src) {
  20025. throw new Error('A non-empty playlist URL or JSON manifest string is required');
  20026. }
  20027. let {
  20028. maxPlaylistRetries
  20029. } = options;
  20030. if (maxPlaylistRetries === null || typeof maxPlaylistRetries === 'undefined') {
  20031. maxPlaylistRetries = Infinity;
  20032. }
  20033. Vhs$1 = externVhs;
  20034. this.bufferBasedABR = Boolean(bufferBasedABR);
  20035. this.leastPixelDiffSelector = Boolean(leastPixelDiffSelector);
  20036. this.withCredentials = withCredentials;
  20037. this.tech_ = tech;
  20038. this.vhs_ = tech.vhs;
  20039. this.sourceType_ = sourceType;
  20040. this.useCueTags_ = useCueTags;
  20041. this.playlistExclusionDuration = playlistExclusionDuration;
  20042. this.maxPlaylistRetries = maxPlaylistRetries;
  20043. this.enableLowInitialPlaylist = enableLowInitialPlaylist;
  20044. if (this.useCueTags_) {
  20045. this.cueTagsTrack_ = this.tech_.addTextTrack('metadata', 'ad-cues');
  20046. this.cueTagsTrack_.inBandMetadataTrackDispatchType = '';
  20047. }
  20048. this.requestOptions_ = {
  20049. withCredentials,
  20050. maxPlaylistRetries,
  20051. timeout: null
  20052. };
  20053. this.on('error', this.pauseLoading);
  20054. this.mediaTypes_ = createMediaTypes();
  20055. this.mediaSource = new window__default["default"].MediaSource();
  20056. this.handleDurationChange_ = this.handleDurationChange_.bind(this);
  20057. this.handleSourceOpen_ = this.handleSourceOpen_.bind(this);
  20058. this.handleSourceEnded_ = this.handleSourceEnded_.bind(this);
  20059. this.mediaSource.addEventListener('durationchange', this.handleDurationChange_); // load the media source into the player
  20060. this.mediaSource.addEventListener('sourceopen', this.handleSourceOpen_);
  20061. this.mediaSource.addEventListener('sourceended', this.handleSourceEnded_); // we don't have to handle sourceclose since dispose will handle termination of
  20062. // everything, and the MediaSource should not be detached without a proper disposal
  20063. this.seekable_ = createTimeRanges();
  20064. this.hasPlayed_ = false;
  20065. this.syncController_ = new SyncController(options);
  20066. this.segmentMetadataTrack_ = tech.addRemoteTextTrack({
  20067. kind: 'metadata',
  20068. label: 'segment-metadata'
  20069. }, false).track;
  20070. this.decrypter_ = new Decrypter();
  20071. this.sourceUpdater_ = new SourceUpdater(this.mediaSource);
  20072. this.inbandTextTracks_ = {};
  20073. this.timelineChangeController_ = new TimelineChangeController();
  20074. this.keyStatusMap_ = new Map();
  20075. const segmentLoaderSettings = {
  20076. vhs: this.vhs_,
  20077. parse708captions: options.parse708captions,
  20078. useDtsForTimestampOffset: options.useDtsForTimestampOffset,
  20079. captionServices,
  20080. mediaSource: this.mediaSource,
  20081. currentTime: this.tech_.currentTime.bind(this.tech_),
  20082. seekable: () => this.seekable(),
  20083. seeking: () => this.tech_.seeking(),
  20084. duration: () => this.duration(),
  20085. hasPlayed: () => this.hasPlayed_,
  20086. goalBufferLength: () => this.goalBufferLength(),
  20087. bandwidth,
  20088. syncController: this.syncController_,
  20089. decrypter: this.decrypter_,
  20090. sourceType: this.sourceType_,
  20091. inbandTextTracks: this.inbandTextTracks_,
  20092. cacheEncryptionKeys,
  20093. sourceUpdater: this.sourceUpdater_,
  20094. timelineChangeController: this.timelineChangeController_,
  20095. exactManifestTimings: options.exactManifestTimings,
  20096. addMetadataToTextTrack: this.addMetadataToTextTrack.bind(this)
  20097. }; // The source type check not only determines whether a special DASH playlist loader
  20098. // should be used, but also covers the case where the provided src is a vhs-json
  20099. // manifest object (instead of a URL). In the case of vhs-json, the default
  20100. // PlaylistLoader should be used.
  20101. this.mainPlaylistLoader_ = this.sourceType_ === 'dash' ? new DashPlaylistLoader(src, this.vhs_, merge(this.requestOptions_, {
  20102. addMetadataToTextTrack: this.addMetadataToTextTrack.bind(this)
  20103. })) : new PlaylistLoader(src, this.vhs_, merge(this.requestOptions_, {
  20104. addDateRangesToTextTrack: this.addDateRangesToTextTrack_.bind(this)
  20105. }));
  20106. this.setupMainPlaylistLoaderListeners_(); // setup segment loaders
  20107. // combined audio/video or just video when alternate audio track is selected
  20108. this.mainSegmentLoader_ = new SegmentLoader(merge(segmentLoaderSettings, {
  20109. segmentMetadataTrack: this.segmentMetadataTrack_,
  20110. loaderType: 'main'
  20111. }), options); // alternate audio track
  20112. this.audioSegmentLoader_ = new SegmentLoader(merge(segmentLoaderSettings, {
  20113. loaderType: 'audio'
  20114. }), options);
  20115. this.subtitleSegmentLoader_ = new VTTSegmentLoader(merge(segmentLoaderSettings, {
  20116. loaderType: 'vtt',
  20117. featuresNativeTextTracks: this.tech_.featuresNativeTextTracks,
  20118. loadVttJs: () => new Promise((resolve, reject) => {
  20119. function onLoad() {
  20120. tech.off('vttjserror', onError);
  20121. resolve();
  20122. }
  20123. function onError() {
  20124. tech.off('vttjsloaded', onLoad);
  20125. reject();
  20126. }
  20127. tech.one('vttjsloaded', onLoad);
  20128. tech.one('vttjserror', onError); // safe to call multiple times, script will be loaded only once:
  20129. tech.addWebVttScript_();
  20130. })
  20131. }), options);
  20132. const getBandwidth = () => {
  20133. return this.mainSegmentLoader_.bandwidth;
  20134. };
  20135. this.contentSteeringController_ = new ContentSteeringController(this.vhs_.xhr, getBandwidth);
  20136. this.setupSegmentLoaderListeners_();
  20137. if (this.bufferBasedABR) {
  20138. this.mainPlaylistLoader_.one('loadedplaylist', () => this.startABRTimer_());
  20139. this.tech_.on('pause', () => this.stopABRTimer_());
  20140. this.tech_.on('play', () => this.startABRTimer_());
  20141. } // Create SegmentLoader stat-getters
  20142. // mediaRequests_
  20143. // mediaRequestsAborted_
  20144. // mediaRequestsTimedout_
  20145. // mediaRequestsErrored_
  20146. // mediaTransferDuration_
  20147. // mediaBytesTransferred_
  20148. // mediaAppends_
  20149. loaderStats.forEach(stat => {
  20150. this[stat + '_'] = sumLoaderStat.bind(this, stat);
  20151. });
  20152. this.logger_ = logger('pc');
  20153. this.triggeredFmp4Usage = false;
  20154. if (this.tech_.preload() === 'none') {
  20155. this.loadOnPlay_ = () => {
  20156. this.loadOnPlay_ = null;
  20157. this.mainPlaylistLoader_.load();
  20158. };
  20159. this.tech_.one('play', this.loadOnPlay_);
  20160. } else {
  20161. this.mainPlaylistLoader_.load();
  20162. }
  20163. this.timeToLoadedData__ = -1;
  20164. this.mainAppendsToLoadedData__ = -1;
  20165. this.audioAppendsToLoadedData__ = -1;
  20166. const event = this.tech_.preload() === 'none' ? 'play' : 'loadstart'; // start the first frame timer on loadstart or play (for preload none)
  20167. this.tech_.one(event, () => {
  20168. const timeToLoadedDataStart = Date.now();
  20169. this.tech_.one('loadeddata', () => {
  20170. this.timeToLoadedData__ = Date.now() - timeToLoadedDataStart;
  20171. this.mainAppendsToLoadedData__ = this.mainSegmentLoader_.mediaAppends;
  20172. this.audioAppendsToLoadedData__ = this.audioSegmentLoader_.mediaAppends;
  20173. });
  20174. });
  20175. }
  20176. mainAppendsToLoadedData_() {
  20177. return this.mainAppendsToLoadedData__;
  20178. }
  20179. audioAppendsToLoadedData_() {
  20180. return this.audioAppendsToLoadedData__;
  20181. }
  20182. appendsToLoadedData_() {
  20183. const main = this.mainAppendsToLoadedData_();
  20184. const audio = this.audioAppendsToLoadedData_();
  20185. if (main === -1 || audio === -1) {
  20186. return -1;
  20187. }
  20188. return main + audio;
  20189. }
  20190. timeToLoadedData_() {
  20191. return this.timeToLoadedData__;
  20192. }
  20193. /**
  20194. * Run selectPlaylist and switch to the new playlist if we should
  20195. *
  20196. * @param {string} [reason=abr] a reason for why the ABR check is made
  20197. * @private
  20198. */
  20199. checkABR_(reason = 'abr') {
  20200. const nextPlaylist = this.selectPlaylist();
  20201. if (nextPlaylist && this.shouldSwitchToMedia_(nextPlaylist)) {
  20202. this.switchMedia_(nextPlaylist, reason);
  20203. }
  20204. }
  20205. switchMedia_(playlist, cause, delay) {
  20206. const oldMedia = this.media();
  20207. const oldId = oldMedia && (oldMedia.id || oldMedia.uri);
  20208. const newId = playlist && (playlist.id || playlist.uri);
  20209. if (oldId && oldId !== newId) {
  20210. this.logger_(`switch media ${oldId} -> ${newId} from ${cause}`);
  20211. this.tech_.trigger({
  20212. type: 'usage',
  20213. name: `vhs-rendition-change-${cause}`
  20214. });
  20215. }
  20216. this.mainPlaylistLoader_.media(playlist, delay);
  20217. }
  20218. /**
  20219. * A function that ensures we switch our playlists inside of `mediaTypes`
  20220. * to match the current `serviceLocation` provided by the contentSteering controller.
  20221. * We want to check media types of `AUDIO`, `SUBTITLES`, and `CLOSED-CAPTIONS`.
  20222. *
  20223. * This should only be called on a DASH playback scenario while using content steering.
  20224. * This is necessary due to differences in how media in HLS manifests are generally tied to
  20225. * a video playlist, where in DASH that is not always the case.
  20226. */
  20227. switchMediaForDASHContentSteering_() {
  20228. ['AUDIO', 'SUBTITLES', 'CLOSED-CAPTIONS'].forEach(type => {
  20229. const mediaType = this.mediaTypes_[type];
  20230. const activeGroup = mediaType ? mediaType.activeGroup() : null;
  20231. const pathway = this.contentSteeringController_.getPathway();
  20232. if (activeGroup && pathway) {
  20233. // activeGroup can be an array or a single group
  20234. const mediaPlaylists = activeGroup.length ? activeGroup[0].playlists : activeGroup.playlists;
  20235. const dashMediaPlaylists = mediaPlaylists.filter(p => p.attributes.serviceLocation === pathway); // Switch the current active playlist to the correct CDN
  20236. if (dashMediaPlaylists.length) {
  20237. this.mediaTypes_[type].activePlaylistLoader.media(dashMediaPlaylists[0]);
  20238. }
  20239. }
  20240. });
  20241. }
  20242. /**
  20243. * Start a timer that periodically calls checkABR_
  20244. *
  20245. * @private
  20246. */
  20247. startABRTimer_() {
  20248. this.stopABRTimer_();
  20249. this.abrTimer_ = window__default["default"].setInterval(() => this.checkABR_(), 250);
  20250. }
  20251. /**
  20252. * Stop the timer that periodically calls checkABR_
  20253. *
  20254. * @private
  20255. */
  20256. stopABRTimer_() {
  20257. // if we're scrubbing, we don't need to pause.
  20258. // This getter will be added to Video.js in version 7.11.
  20259. if (this.tech_.scrubbing && this.tech_.scrubbing()) {
  20260. return;
  20261. }
  20262. window__default["default"].clearInterval(this.abrTimer_);
  20263. this.abrTimer_ = null;
  20264. }
  20265. /**
  20266. * Get a list of playlists for the currently selected audio playlist
  20267. *
  20268. * @return {Array} the array of audio playlists
  20269. */
  20270. getAudioTrackPlaylists_() {
  20271. const main = this.main();
  20272. const defaultPlaylists = main && main.playlists || []; // if we don't have any audio groups then we can only
  20273. // assume that the audio tracks are contained in main
  20274. // playlist array, use that or an empty array.
  20275. if (!main || !main.mediaGroups || !main.mediaGroups.AUDIO) {
  20276. return defaultPlaylists;
  20277. }
  20278. const AUDIO = main.mediaGroups.AUDIO;
  20279. const groupKeys = Object.keys(AUDIO);
  20280. let track; // get the current active track
  20281. if (Object.keys(this.mediaTypes_.AUDIO.groups).length) {
  20282. track = this.mediaTypes_.AUDIO.activeTrack(); // or get the default track from main if mediaTypes_ isn't setup yet
  20283. } else {
  20284. // default group is `main` or just the first group.
  20285. const defaultGroup = AUDIO.main || groupKeys.length && AUDIO[groupKeys[0]];
  20286. for (const label in defaultGroup) {
  20287. if (defaultGroup[label].default) {
  20288. track = {
  20289. label
  20290. };
  20291. break;
  20292. }
  20293. }
  20294. } // no active track no playlists.
  20295. if (!track) {
  20296. return defaultPlaylists;
  20297. }
  20298. const playlists = []; // get all of the playlists that are possible for the
  20299. // active track.
  20300. for (const group in AUDIO) {
  20301. if (AUDIO[group][track.label]) {
  20302. const properties = AUDIO[group][track.label];
  20303. if (properties.playlists && properties.playlists.length) {
  20304. playlists.push.apply(playlists, properties.playlists);
  20305. } else if (properties.uri) {
  20306. playlists.push(properties);
  20307. } else if (main.playlists.length) {
  20308. // if an audio group does not have a uri
  20309. // see if we have main playlists that use it as a group.
  20310. // if we do then add those to the playlists list.
  20311. for (let i = 0; i < main.playlists.length; i++) {
  20312. const playlist = main.playlists[i];
  20313. if (playlist.attributes && playlist.attributes.AUDIO && playlist.attributes.AUDIO === group) {
  20314. playlists.push(playlist);
  20315. }
  20316. }
  20317. }
  20318. }
  20319. }
  20320. if (!playlists.length) {
  20321. return defaultPlaylists;
  20322. }
  20323. return playlists;
  20324. }
  20325. /**
  20326. * Register event handlers on the main playlist loader. A helper
  20327. * function for construction time.
  20328. *
  20329. * @private
  20330. */
  20331. setupMainPlaylistLoaderListeners_() {
  20332. this.mainPlaylistLoader_.on('loadedmetadata', () => {
  20333. const media = this.mainPlaylistLoader_.media();
  20334. const requestTimeout = media.targetDuration * 1.5 * 1000; // If we don't have any more available playlists, we don't want to
  20335. // timeout the request.
  20336. if (isLowestEnabledRendition(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.media())) {
  20337. this.requestOptions_.timeout = 0;
  20338. } else {
  20339. this.requestOptions_.timeout = requestTimeout;
  20340. } // if this isn't a live video and preload permits, start
  20341. // downloading segments
  20342. if (media.endList && this.tech_.preload() !== 'none') {
  20343. this.mainSegmentLoader_.playlist(media, this.requestOptions_);
  20344. this.mainSegmentLoader_.load();
  20345. }
  20346. setupMediaGroups({
  20347. sourceType: this.sourceType_,
  20348. segmentLoaders: {
  20349. AUDIO: this.audioSegmentLoader_,
  20350. SUBTITLES: this.subtitleSegmentLoader_,
  20351. main: this.mainSegmentLoader_
  20352. },
  20353. tech: this.tech_,
  20354. requestOptions: this.requestOptions_,
  20355. mainPlaylistLoader: this.mainPlaylistLoader_,
  20356. vhs: this.vhs_,
  20357. main: this.main(),
  20358. mediaTypes: this.mediaTypes_,
  20359. excludePlaylist: this.excludePlaylist.bind(this)
  20360. });
  20361. this.triggerPresenceUsage_(this.main(), media);
  20362. this.setupFirstPlay();
  20363. if (!this.mediaTypes_.AUDIO.activePlaylistLoader || this.mediaTypes_.AUDIO.activePlaylistLoader.media()) {
  20364. this.trigger('selectedinitialmedia');
  20365. } else {
  20366. // We must wait for the active audio playlist loader to
  20367. // finish setting up before triggering this event so the
  20368. // representations API and EME setup is correct
  20369. this.mediaTypes_.AUDIO.activePlaylistLoader.one('loadedmetadata', () => {
  20370. this.trigger('selectedinitialmedia');
  20371. });
  20372. }
  20373. });
  20374. this.mainPlaylistLoader_.on('loadedplaylist', () => {
  20375. if (this.loadOnPlay_) {
  20376. this.tech_.off('play', this.loadOnPlay_);
  20377. }
  20378. let updatedPlaylist = this.mainPlaylistLoader_.media();
  20379. if (!updatedPlaylist) {
  20380. // Add content steering listeners on first load and init.
  20381. this.attachContentSteeringListeners_();
  20382. this.initContentSteeringController_(); // exclude any variants that are not supported by the browser before selecting
  20383. // an initial media as the playlist selectors do not consider browser support
  20384. this.excludeUnsupportedVariants_();
  20385. let selectedMedia;
  20386. if (this.enableLowInitialPlaylist) {
  20387. selectedMedia = this.selectInitialPlaylist();
  20388. }
  20389. if (!selectedMedia) {
  20390. selectedMedia = this.selectPlaylist();
  20391. }
  20392. if (!selectedMedia || !this.shouldSwitchToMedia_(selectedMedia)) {
  20393. return;
  20394. }
  20395. this.initialMedia_ = selectedMedia;
  20396. this.switchMedia_(this.initialMedia_, 'initial'); // Under the standard case where a source URL is provided, loadedplaylist will
  20397. // fire again since the playlist will be requested. In the case of vhs-json
  20398. // (where the manifest object is provided as the source), when the media
  20399. // playlist's `segments` list is already available, a media playlist won't be
  20400. // requested, and loadedplaylist won't fire again, so the playlist handler must be
  20401. // called on its own here.
  20402. const haveJsonSource = this.sourceType_ === 'vhs-json' && this.initialMedia_.segments;
  20403. if (!haveJsonSource) {
  20404. return;
  20405. }
  20406. updatedPlaylist = this.initialMedia_;
  20407. }
  20408. this.handleUpdatedMediaPlaylist(updatedPlaylist);
  20409. });
  20410. this.mainPlaylistLoader_.on('error', () => {
  20411. const error = this.mainPlaylistLoader_.error;
  20412. this.excludePlaylist({
  20413. playlistToExclude: error.playlist,
  20414. error
  20415. });
  20416. });
  20417. this.mainPlaylistLoader_.on('mediachanging', () => {
  20418. this.mainSegmentLoader_.abort();
  20419. this.mainSegmentLoader_.pause();
  20420. });
  20421. this.mainPlaylistLoader_.on('mediachange', () => {
  20422. const media = this.mainPlaylistLoader_.media();
  20423. const requestTimeout = media.targetDuration * 1.5 * 1000; // If we don't have any more available playlists, we don't want to
  20424. // timeout the request.
  20425. if (isLowestEnabledRendition(this.mainPlaylistLoader_.main, this.mainPlaylistLoader_.media())) {
  20426. this.requestOptions_.timeout = 0;
  20427. } else {
  20428. this.requestOptions_.timeout = requestTimeout;
  20429. }
  20430. if (this.sourceType_ === 'dash') {
  20431. // we don't want to re-request the same hls playlist right after it was changed
  20432. this.mainPlaylistLoader_.load();
  20433. } // TODO: Create a new event on the PlaylistLoader that signals
  20434. // that the segments have changed in some way and use that to
  20435. // update the SegmentLoader instead of doing it twice here and
  20436. // on `loadedplaylist`
  20437. this.mainSegmentLoader_.pause();
  20438. this.mainSegmentLoader_.playlist(media, this.requestOptions_);
  20439. if (this.waitingForFastQualityPlaylistReceived_) {
  20440. this.runFastQualitySwitch_();
  20441. } else {
  20442. this.mainSegmentLoader_.load();
  20443. }
  20444. this.tech_.trigger({
  20445. type: 'mediachange',
  20446. bubbles: true
  20447. });
  20448. });
  20449. this.mainPlaylistLoader_.on('playlistunchanged', () => {
  20450. const updatedPlaylist = this.mainPlaylistLoader_.media(); // ignore unchanged playlists that have already been
  20451. // excluded for not-changing. We likely just have a really slowly updating
  20452. // playlist.
  20453. if (updatedPlaylist.lastExcludeReason_ === 'playlist-unchanged') {
  20454. return;
  20455. }
  20456. const playlistOutdated = this.stuckAtPlaylistEnd_(updatedPlaylist);
  20457. if (playlistOutdated) {
  20458. // Playlist has stopped updating and we're stuck at its end. Try to
  20459. // exclude it and switch to another playlist in the hope that that
  20460. // one is updating (and give the player a chance to re-adjust to the
  20461. // safe live point).
  20462. this.excludePlaylist({
  20463. error: {
  20464. message: 'Playlist no longer updating.',
  20465. reason: 'playlist-unchanged'
  20466. }
  20467. }); // useful for monitoring QoS
  20468. this.tech_.trigger('playliststuck');
  20469. }
  20470. });
  20471. this.mainPlaylistLoader_.on('renditiondisabled', () => {
  20472. this.tech_.trigger({
  20473. type: 'usage',
  20474. name: 'vhs-rendition-disabled'
  20475. });
  20476. });
  20477. this.mainPlaylistLoader_.on('renditionenabled', () => {
  20478. this.tech_.trigger({
  20479. type: 'usage',
  20480. name: 'vhs-rendition-enabled'
  20481. });
  20482. });
  20483. }
  20484. /**
  20485. * Given an updated media playlist (whether it was loaded for the first time, or
  20486. * refreshed for live playlists), update any relevant properties and state to reflect
  20487. * changes in the media that should be accounted for (e.g., cues and duration).
  20488. *
  20489. * @param {Object} updatedPlaylist the updated media playlist object
  20490. *
  20491. * @private
  20492. */
  20493. handleUpdatedMediaPlaylist(updatedPlaylist) {
  20494. if (this.useCueTags_) {
  20495. this.updateAdCues_(updatedPlaylist);
  20496. } // TODO: Create a new event on the PlaylistLoader that signals
  20497. // that the segments have changed in some way and use that to
  20498. // update the SegmentLoader instead of doing it twice here and
  20499. // on `mediachange`
  20500. this.mainSegmentLoader_.pause();
  20501. this.mainSegmentLoader_.playlist(updatedPlaylist, this.requestOptions_);
  20502. if (this.waitingForFastQualityPlaylistReceived_) {
  20503. this.runFastQualitySwitch_();
  20504. }
  20505. this.updateDuration(!updatedPlaylist.endList); // If the player isn't paused, ensure that the segment loader is running,
  20506. // as it is possible that it was temporarily stopped while waiting for
  20507. // a playlist (e.g., in case the playlist errored and we re-requested it).
  20508. if (!this.tech_.paused()) {
  20509. this.mainSegmentLoader_.load();
  20510. if (this.audioSegmentLoader_) {
  20511. this.audioSegmentLoader_.load();
  20512. }
  20513. }
  20514. }
  20515. /**
  20516. * A helper function for triggerring presence usage events once per source
  20517. *
  20518. * @private
  20519. */
  20520. triggerPresenceUsage_(main, media) {
  20521. const mediaGroups = main.mediaGroups || {};
  20522. let defaultDemuxed = true;
  20523. const audioGroupKeys = Object.keys(mediaGroups.AUDIO);
  20524. for (const mediaGroup in mediaGroups.AUDIO) {
  20525. for (const label in mediaGroups.AUDIO[mediaGroup]) {
  20526. const properties = mediaGroups.AUDIO[mediaGroup][label];
  20527. if (!properties.uri) {
  20528. defaultDemuxed = false;
  20529. }
  20530. }
  20531. }
  20532. if (defaultDemuxed) {
  20533. this.tech_.trigger({
  20534. type: 'usage',
  20535. name: 'vhs-demuxed'
  20536. });
  20537. }
  20538. if (Object.keys(mediaGroups.SUBTITLES).length) {
  20539. this.tech_.trigger({
  20540. type: 'usage',
  20541. name: 'vhs-webvtt'
  20542. });
  20543. }
  20544. if (Vhs$1.Playlist.isAes(media)) {
  20545. this.tech_.trigger({
  20546. type: 'usage',
  20547. name: 'vhs-aes'
  20548. });
  20549. }
  20550. if (audioGroupKeys.length && Object.keys(mediaGroups.AUDIO[audioGroupKeys[0]]).length > 1) {
  20551. this.tech_.trigger({
  20552. type: 'usage',
  20553. name: 'vhs-alternate-audio'
  20554. });
  20555. }
  20556. if (this.useCueTags_) {
  20557. this.tech_.trigger({
  20558. type: 'usage',
  20559. name: 'vhs-playlist-cue-tags'
  20560. });
  20561. }
  20562. }
  20563. shouldSwitchToMedia_(nextPlaylist) {
  20564. const currentPlaylist = this.mainPlaylistLoader_.media() || this.mainPlaylistLoader_.pendingMedia_;
  20565. const currentTime = this.tech_.currentTime();
  20566. const bufferLowWaterLine = this.bufferLowWaterLine();
  20567. const bufferHighWaterLine = this.bufferHighWaterLine();
  20568. const buffered = this.tech_.buffered();
  20569. return shouldSwitchToMedia({
  20570. buffered,
  20571. currentTime,
  20572. currentPlaylist,
  20573. nextPlaylist,
  20574. bufferLowWaterLine,
  20575. bufferHighWaterLine,
  20576. duration: this.duration(),
  20577. bufferBasedABR: this.bufferBasedABR,
  20578. log: this.logger_
  20579. });
  20580. }
  20581. /**
  20582. * Register event handlers on the segment loaders. A helper function
  20583. * for construction time.
  20584. *
  20585. * @private
  20586. */
  20587. setupSegmentLoaderListeners_() {
  20588. this.mainSegmentLoader_.on('bandwidthupdate', () => {
  20589. // Whether or not buffer based ABR or another ABR is used, on a bandwidth change it's
  20590. // useful to check to see if a rendition switch should be made.
  20591. this.checkABR_('bandwidthupdate');
  20592. this.tech_.trigger('bandwidthupdate');
  20593. });
  20594. this.mainSegmentLoader_.on('timeout', () => {
  20595. if (this.bufferBasedABR) {
  20596. // If a rendition change is needed, then it would've be done on `bandwidthupdate`.
  20597. // Here the only consideration is that for buffer based ABR there's no guarantee
  20598. // of an immediate switch (since the bandwidth is averaged with a timeout
  20599. // bandwidth value of 1), so force a load on the segment loader to keep it going.
  20600. this.mainSegmentLoader_.load();
  20601. }
  20602. }); // `progress` events are not reliable enough of a bandwidth measure to trigger buffer
  20603. // based ABR.
  20604. if (!this.bufferBasedABR) {
  20605. this.mainSegmentLoader_.on('progress', () => {
  20606. this.trigger('progress');
  20607. });
  20608. }
  20609. this.mainSegmentLoader_.on('error', () => {
  20610. const error = this.mainSegmentLoader_.error();
  20611. this.excludePlaylist({
  20612. playlistToExclude: error.playlist,
  20613. error
  20614. });
  20615. });
  20616. this.mainSegmentLoader_.on('appenderror', () => {
  20617. this.error = this.mainSegmentLoader_.error_;
  20618. this.trigger('error');
  20619. });
  20620. this.mainSegmentLoader_.on('syncinfoupdate', () => {
  20621. this.onSyncInfoUpdate_();
  20622. });
  20623. this.mainSegmentLoader_.on('timestampoffset', () => {
  20624. this.tech_.trigger({
  20625. type: 'usage',
  20626. name: 'vhs-timestamp-offset'
  20627. });
  20628. });
  20629. this.audioSegmentLoader_.on('syncinfoupdate', () => {
  20630. this.onSyncInfoUpdate_();
  20631. });
  20632. this.audioSegmentLoader_.on('appenderror', () => {
  20633. this.error = this.audioSegmentLoader_.error_;
  20634. this.trigger('error');
  20635. });
  20636. this.mainSegmentLoader_.on('ended', () => {
  20637. this.logger_('main segment loader ended');
  20638. this.onEndOfStream();
  20639. });
  20640. this.mainSegmentLoader_.on('earlyabort', event => {
  20641. // never try to early abort with the new ABR algorithm
  20642. if (this.bufferBasedABR) {
  20643. return;
  20644. }
  20645. this.delegateLoaders_('all', ['abort']);
  20646. this.excludePlaylist({
  20647. error: {
  20648. message: 'Aborted early because there isn\'t enough bandwidth to complete ' + 'the request without rebuffering.'
  20649. },
  20650. playlistExclusionDuration: ABORT_EARLY_EXCLUSION_SECONDS
  20651. });
  20652. });
  20653. const updateCodecs = () => {
  20654. if (!this.sourceUpdater_.hasCreatedSourceBuffers()) {
  20655. return this.tryToCreateSourceBuffers_();
  20656. }
  20657. const codecs = this.getCodecsOrExclude_(); // no codecs means that the playlist was excluded
  20658. if (!codecs) {
  20659. return;
  20660. }
  20661. this.sourceUpdater_.addOrChangeSourceBuffers(codecs);
  20662. };
  20663. this.mainSegmentLoader_.on('trackinfo', updateCodecs);
  20664. this.audioSegmentLoader_.on('trackinfo', updateCodecs);
  20665. this.mainSegmentLoader_.on('fmp4', () => {
  20666. if (!this.triggeredFmp4Usage) {
  20667. this.tech_.trigger({
  20668. type: 'usage',
  20669. name: 'vhs-fmp4'
  20670. });
  20671. this.triggeredFmp4Usage = true;
  20672. }
  20673. });
  20674. this.audioSegmentLoader_.on('fmp4', () => {
  20675. if (!this.triggeredFmp4Usage) {
  20676. this.tech_.trigger({
  20677. type: 'usage',
  20678. name: 'vhs-fmp4'
  20679. });
  20680. this.triggeredFmp4Usage = true;
  20681. }
  20682. });
  20683. this.audioSegmentLoader_.on('ended', () => {
  20684. this.logger_('audioSegmentLoader ended');
  20685. this.onEndOfStream();
  20686. });
  20687. }
  20688. mediaSecondsLoaded_() {
  20689. return Math.max(this.audioSegmentLoader_.mediaSecondsLoaded + this.mainSegmentLoader_.mediaSecondsLoaded);
  20690. }
  20691. /**
  20692. * Call load on our SegmentLoaders
  20693. */
  20694. load() {
  20695. this.mainSegmentLoader_.load();
  20696. if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
  20697. this.audioSegmentLoader_.load();
  20698. }
  20699. if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
  20700. this.subtitleSegmentLoader_.load();
  20701. }
  20702. }
  20703. /**
  20704. * Re-tune playback quality level for the current player
  20705. * conditions. This method will perform destructive actions like removing
  20706. * already buffered content in order to readjust the currently active
  20707. * playlist quickly. This is good for manual quality changes
  20708. *
  20709. * @private
  20710. */
  20711. fastQualityChange_(media = this.selectPlaylist()) {
  20712. if (media && media === this.mainPlaylistLoader_.media()) {
  20713. this.logger_('skipping fastQualityChange because new media is same as old');
  20714. return;
  20715. }
  20716. this.switchMedia_(media, 'fast-quality'); // we would like to avoid race condition when we call fastQuality,
  20717. // reset everything and start loading segments from prev segments instead of new because new playlist is not received yet
  20718. this.waitingForFastQualityPlaylistReceived_ = true;
  20719. }
  20720. runFastQualitySwitch_() {
  20721. this.waitingForFastQualityPlaylistReceived_ = false; // Delete all buffered data to allow an immediate quality switch, then seek to give
  20722. // the browser a kick to remove any cached frames from the previous rendtion (.04 seconds
  20723. // ahead was roughly the minimum that will accomplish this across a variety of content
  20724. // in IE and Edge, but seeking in place is sufficient on all other browsers)
  20725. // Edge/IE bug: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14600375/
  20726. // Chrome bug: https://bugs.chromium.org/p/chromium/issues/detail?id=651904
  20727. this.mainSegmentLoader_.pause();
  20728. this.mainSegmentLoader_.resetEverything(() => {
  20729. this.tech_.setCurrentTime(this.tech_.currentTime());
  20730. }); // don't need to reset audio as it is reset when media changes
  20731. }
  20732. /**
  20733. * Begin playback.
  20734. */
  20735. play() {
  20736. if (this.setupFirstPlay()) {
  20737. return;
  20738. }
  20739. if (this.tech_.ended()) {
  20740. this.tech_.setCurrentTime(0);
  20741. }
  20742. if (this.hasPlayed_) {
  20743. this.load();
  20744. }
  20745. const seekable = this.tech_.seekable(); // if the viewer has paused and we fell out of the live window,
  20746. // seek forward to the live point
  20747. if (this.tech_.duration() === Infinity) {
  20748. if (this.tech_.currentTime() < seekable.start(0)) {
  20749. return this.tech_.setCurrentTime(seekable.end(seekable.length - 1));
  20750. }
  20751. }
  20752. }
  20753. /**
  20754. * Seek to the latest media position if this is a live video and the
  20755. * player and video are loaded and initialized.
  20756. */
  20757. setupFirstPlay() {
  20758. const media = this.mainPlaylistLoader_.media(); // Check that everything is ready to begin buffering for the first call to play
  20759. // If 1) there is no active media
  20760. // 2) the player is paused
  20761. // 3) the first play has already been setup
  20762. // then exit early
  20763. if (!media || this.tech_.paused() || this.hasPlayed_) {
  20764. return false;
  20765. } // when the video is a live stream and/or has a start time
  20766. if (!media.endList || media.start) {
  20767. const seekable = this.seekable();
  20768. if (!seekable.length) {
  20769. // without a seekable range, the player cannot seek to begin buffering at the
  20770. // live or start point
  20771. return false;
  20772. }
  20773. const seekableEnd = seekable.end(0);
  20774. let startPoint = seekableEnd;
  20775. if (media.start) {
  20776. const offset = media.start.timeOffset;
  20777. if (offset < 0) {
  20778. startPoint = Math.max(seekableEnd + offset, seekable.start(0));
  20779. } else {
  20780. startPoint = Math.min(seekableEnd, offset);
  20781. }
  20782. } // trigger firstplay to inform the source handler to ignore the next seek event
  20783. this.trigger('firstplay'); // seek to the live point
  20784. this.tech_.setCurrentTime(startPoint);
  20785. }
  20786. this.hasPlayed_ = true; // we can begin loading now that everything is ready
  20787. this.load();
  20788. return true;
  20789. }
  20790. /**
  20791. * handle the sourceopen event on the MediaSource
  20792. *
  20793. * @private
  20794. */
  20795. handleSourceOpen_() {
  20796. // Only attempt to create the source buffer if none already exist.
  20797. // handleSourceOpen is also called when we are "re-opening" a source buffer
  20798. // after `endOfStream` has been called (in response to a seek for instance)
  20799. this.tryToCreateSourceBuffers_(); // if autoplay is enabled, begin playback. This is duplicative of
  20800. // code in video.js but is required because play() must be invoked
  20801. // *after* the media source has opened.
  20802. if (this.tech_.autoplay()) {
  20803. const playPromise = this.tech_.play(); // Catch/silence error when a pause interrupts a play request
  20804. // on browsers which return a promise
  20805. if (typeof playPromise !== 'undefined' && typeof playPromise.then === 'function') {
  20806. playPromise.then(null, e => {});
  20807. }
  20808. }
  20809. this.trigger('sourceopen');
  20810. }
  20811. /**
  20812. * handle the sourceended event on the MediaSource
  20813. *
  20814. * @private
  20815. */
  20816. handleSourceEnded_() {
  20817. if (!this.inbandTextTracks_.metadataTrack_) {
  20818. return;
  20819. }
  20820. const cues = this.inbandTextTracks_.metadataTrack_.cues;
  20821. if (!cues || !cues.length) {
  20822. return;
  20823. }
  20824. const duration = this.duration();
  20825. cues[cues.length - 1].endTime = isNaN(duration) || Math.abs(duration) === Infinity ? Number.MAX_VALUE : duration;
  20826. }
  20827. /**
  20828. * handle the durationchange event on the MediaSource
  20829. *
  20830. * @private
  20831. */
  20832. handleDurationChange_() {
  20833. this.tech_.trigger('durationchange');
  20834. }
  20835. /**
  20836. * Calls endOfStream on the media source when all active stream types have called
  20837. * endOfStream
  20838. *
  20839. * @param {string} streamType
  20840. * Stream type of the segment loader that called endOfStream
  20841. * @private
  20842. */
  20843. onEndOfStream() {
  20844. let isEndOfStream = this.mainSegmentLoader_.ended_;
  20845. if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
  20846. const mainMediaInfo = this.mainSegmentLoader_.getCurrentMediaInfo_(); // if the audio playlist loader exists, then alternate audio is active
  20847. if (!mainMediaInfo || mainMediaInfo.hasVideo) {
  20848. // if we do not know if the main segment loader contains video yet or if we
  20849. // definitively know the main segment loader contains video, then we need to wait
  20850. // for both main and audio segment loaders to call endOfStream
  20851. isEndOfStream = isEndOfStream && this.audioSegmentLoader_.ended_;
  20852. } else {
  20853. // otherwise just rely on the audio loader
  20854. isEndOfStream = this.audioSegmentLoader_.ended_;
  20855. }
  20856. }
  20857. if (!isEndOfStream) {
  20858. return;
  20859. }
  20860. this.stopABRTimer_();
  20861. this.sourceUpdater_.endOfStream();
  20862. }
  20863. /**
  20864. * Check if a playlist has stopped being updated
  20865. *
  20866. * @param {Object} playlist the media playlist object
  20867. * @return {boolean} whether the playlist has stopped being updated or not
  20868. */
  20869. stuckAtPlaylistEnd_(playlist) {
  20870. const seekable = this.seekable();
  20871. if (!seekable.length) {
  20872. // playlist doesn't have enough information to determine whether we are stuck
  20873. return false;
  20874. }
  20875. const expired = this.syncController_.getExpiredTime(playlist, this.duration());
  20876. if (expired === null) {
  20877. return false;
  20878. } // does not use the safe live end to calculate playlist end, since we
  20879. // don't want to say we are stuck while there is still content
  20880. const absolutePlaylistEnd = Vhs$1.Playlist.playlistEnd(playlist, expired);
  20881. const currentTime = this.tech_.currentTime();
  20882. const buffered = this.tech_.buffered();
  20883. if (!buffered.length) {
  20884. // return true if the playhead reached the absolute end of the playlist
  20885. return absolutePlaylistEnd - currentTime <= SAFE_TIME_DELTA;
  20886. }
  20887. const bufferedEnd = buffered.end(buffered.length - 1); // return true if there is too little buffer left and buffer has reached absolute
  20888. // end of playlist
  20889. return bufferedEnd - currentTime <= SAFE_TIME_DELTA && absolutePlaylistEnd - bufferedEnd <= SAFE_TIME_DELTA;
  20890. }
  20891. /**
  20892. * Exclude a playlist for a set amount of time, making it unavailable for selection by
  20893. * the rendition selection algorithm, then force a new playlist (rendition) selection.
  20894. *
  20895. * @param {Object=} playlistToExclude
  20896. * the playlist to exclude, defaults to the currently selected playlist
  20897. * @param {Object=} error
  20898. * an optional error
  20899. * @param {number=} playlistExclusionDuration
  20900. * an optional number of seconds to exclude the playlist
  20901. */
  20902. excludePlaylist({
  20903. playlistToExclude = this.mainPlaylistLoader_.media(),
  20904. error = {},
  20905. playlistExclusionDuration
  20906. }) {
  20907. // If the `error` was generated by the playlist loader, it will contain
  20908. // the playlist we were trying to load (but failed) and that should be
  20909. // excluded instead of the currently selected playlist which is likely
  20910. // out-of-date in this scenario
  20911. playlistToExclude = playlistToExclude || this.mainPlaylistLoader_.media();
  20912. playlistExclusionDuration = playlistExclusionDuration || error.playlistExclusionDuration || this.playlistExclusionDuration; // If there is no current playlist, then an error occurred while we were
  20913. // trying to load the main OR while we were disposing of the tech
  20914. if (!playlistToExclude) {
  20915. this.error = error;
  20916. if (this.mediaSource.readyState !== 'open') {
  20917. this.trigger('error');
  20918. } else {
  20919. this.sourceUpdater_.endOfStream('network');
  20920. }
  20921. return;
  20922. }
  20923. playlistToExclude.playlistErrors_++;
  20924. const playlists = this.mainPlaylistLoader_.main.playlists;
  20925. const enabledPlaylists = playlists.filter(isEnabled);
  20926. const isFinalRendition = enabledPlaylists.length === 1 && enabledPlaylists[0] === playlistToExclude; // Don't exclude the only playlist unless it was excluded
  20927. // forever
  20928. if (playlists.length === 1 && playlistExclusionDuration !== Infinity) {
  20929. videojs__default["default"].log.warn(`Problem encountered with playlist ${playlistToExclude.id}. ` + 'Trying again since it is the only playlist.');
  20930. this.tech_.trigger('retryplaylist'); // if this is a final rendition, we should delay
  20931. return this.mainPlaylistLoader_.load(isFinalRendition);
  20932. }
  20933. if (isFinalRendition) {
  20934. // If we're content steering, try other pathways.
  20935. if (this.main().contentSteering) {
  20936. const pathway = this.pathwayAttribute_(playlistToExclude); // Ignore at least 1 steering manifest refresh.
  20937. const reIncludeDelay = this.contentSteeringController_.steeringManifest.ttl * 1000;
  20938. this.contentSteeringController_.excludePathway(pathway);
  20939. this.excludeThenChangePathway_();
  20940. setTimeout(() => {
  20941. this.contentSteeringController_.addAvailablePathway(pathway);
  20942. }, reIncludeDelay);
  20943. return;
  20944. } // Since we're on the final non-excluded playlist, and we're about to exclude
  20945. // it, instead of erring the player or retrying this playlist, clear out the current
  20946. // exclusion list. This allows other playlists to be attempted in case any have been
  20947. // fixed.
  20948. let reincluded = false;
  20949. playlists.forEach(playlist => {
  20950. // skip current playlist which is about to be excluded
  20951. if (playlist === playlistToExclude) {
  20952. return;
  20953. }
  20954. const excludeUntil = playlist.excludeUntil; // a playlist cannot be reincluded if it wasn't excluded to begin with.
  20955. if (typeof excludeUntil !== 'undefined' && excludeUntil !== Infinity) {
  20956. reincluded = true;
  20957. delete playlist.excludeUntil;
  20958. }
  20959. });
  20960. if (reincluded) {
  20961. videojs__default["default"].log.warn('Removing other playlists from the exclusion list because the last ' + 'rendition is about to be excluded.'); // Technically we are retrying a playlist, in that we are simply retrying a previous
  20962. // playlist. This is needed for users relying on the retryplaylist event to catch a
  20963. // case where the player might be stuck and looping through "dead" playlists.
  20964. this.tech_.trigger('retryplaylist');
  20965. }
  20966. } // Exclude this playlist
  20967. let excludeUntil;
  20968. if (playlistToExclude.playlistErrors_ > this.maxPlaylistRetries) {
  20969. excludeUntil = Infinity;
  20970. } else {
  20971. excludeUntil = Date.now() + playlistExclusionDuration * 1000;
  20972. }
  20973. playlistToExclude.excludeUntil = excludeUntil;
  20974. if (error.reason) {
  20975. playlistToExclude.lastExcludeReason_ = error.reason;
  20976. }
  20977. this.tech_.trigger('excludeplaylist');
  20978. this.tech_.trigger({
  20979. type: 'usage',
  20980. name: 'vhs-rendition-excluded'
  20981. }); // TODO: only load a new playlist if we're excluding the current playlist
  20982. // If this function was called with a playlist that's not the current active playlist
  20983. // (e.g., media().id !== playlistToExclude.id),
  20984. // then a new playlist should not be selected and loaded, as there's nothing wrong with the current playlist.
  20985. const nextPlaylist = this.selectPlaylist();
  20986. if (!nextPlaylist) {
  20987. this.error = 'Playback cannot continue. No available working or supported playlists.';
  20988. this.trigger('error');
  20989. return;
  20990. }
  20991. const logFn = error.internal ? this.logger_ : videojs__default["default"].log.warn;
  20992. const errorMessage = error.message ? ' ' + error.message : '';
  20993. logFn(`${error.internal ? 'Internal problem' : 'Problem'} encountered with playlist ${playlistToExclude.id}.` + `${errorMessage} Switching to playlist ${nextPlaylist.id}.`); // if audio group changed reset audio loaders
  20994. if (nextPlaylist.attributes.AUDIO !== playlistToExclude.attributes.AUDIO) {
  20995. this.delegateLoaders_('audio', ['abort', 'pause']);
  20996. } // if subtitle group changed reset subtitle loaders
  20997. if (nextPlaylist.attributes.SUBTITLES !== playlistToExclude.attributes.SUBTITLES) {
  20998. this.delegateLoaders_('subtitle', ['abort', 'pause']);
  20999. }
  21000. this.delegateLoaders_('main', ['abort', 'pause']);
  21001. const delayDuration = nextPlaylist.targetDuration / 2 * 1000 || 5 * 1000;
  21002. const shouldDelay = typeof nextPlaylist.lastRequest === 'number' && Date.now() - nextPlaylist.lastRequest <= delayDuration; // delay if it's a final rendition or if the last refresh is sooner than half targetDuration
  21003. return this.switchMedia_(nextPlaylist, 'exclude', isFinalRendition || shouldDelay);
  21004. }
  21005. /**
  21006. * Pause all segment/playlist loaders
  21007. */
  21008. pauseLoading() {
  21009. this.delegateLoaders_('all', ['abort', 'pause']);
  21010. this.stopABRTimer_();
  21011. }
  21012. /**
  21013. * Call a set of functions in order on playlist loaders, segment loaders,
  21014. * or both types of loaders.
  21015. *
  21016. * @param {string} filter
  21017. * Filter loaders that should call fnNames using a string. Can be:
  21018. * * all - run on all loaders
  21019. * * audio - run on all audio loaders
  21020. * * subtitle - run on all subtitle loaders
  21021. * * main - run on the main loaders
  21022. *
  21023. * @param {Array|string} fnNames
  21024. * A string or array of function names to call.
  21025. */
  21026. delegateLoaders_(filter, fnNames) {
  21027. const loaders = [];
  21028. const dontFilterPlaylist = filter === 'all';
  21029. if (dontFilterPlaylist || filter === 'main') {
  21030. loaders.push(this.mainPlaylistLoader_);
  21031. }
  21032. const mediaTypes = [];
  21033. if (dontFilterPlaylist || filter === 'audio') {
  21034. mediaTypes.push('AUDIO');
  21035. }
  21036. if (dontFilterPlaylist || filter === 'subtitle') {
  21037. mediaTypes.push('CLOSED-CAPTIONS');
  21038. mediaTypes.push('SUBTITLES');
  21039. }
  21040. mediaTypes.forEach(mediaType => {
  21041. const loader = this.mediaTypes_[mediaType] && this.mediaTypes_[mediaType].activePlaylistLoader;
  21042. if (loader) {
  21043. loaders.push(loader);
  21044. }
  21045. });
  21046. ['main', 'audio', 'subtitle'].forEach(name => {
  21047. const loader = this[`${name}SegmentLoader_`];
  21048. if (loader && (filter === name || filter === 'all')) {
  21049. loaders.push(loader);
  21050. }
  21051. });
  21052. loaders.forEach(loader => fnNames.forEach(fnName => {
  21053. if (typeof loader[fnName] === 'function') {
  21054. loader[fnName]();
  21055. }
  21056. }));
  21057. }
  21058. /**
  21059. * set the current time on all segment loaders
  21060. *
  21061. * @param {TimeRange} currentTime the current time to set
  21062. * @return {TimeRange} the current time
  21063. */
  21064. setCurrentTime(currentTime) {
  21065. const buffered = findRange(this.tech_.buffered(), currentTime);
  21066. if (!(this.mainPlaylistLoader_ && this.mainPlaylistLoader_.media())) {
  21067. // return immediately if the metadata is not ready yet
  21068. return 0;
  21069. } // it's clearly an edge-case but don't thrown an error if asked to
  21070. // seek within an empty playlist
  21071. if (!this.mainPlaylistLoader_.media().segments) {
  21072. return 0;
  21073. } // if the seek location is already buffered, continue buffering as usual
  21074. if (buffered && buffered.length) {
  21075. return currentTime;
  21076. } // cancel outstanding requests so we begin buffering at the new
  21077. // location
  21078. this.mainSegmentLoader_.pause();
  21079. this.mainSegmentLoader_.resetEverything();
  21080. if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
  21081. this.audioSegmentLoader_.pause();
  21082. this.audioSegmentLoader_.resetEverything();
  21083. }
  21084. if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
  21085. this.subtitleSegmentLoader_.pause();
  21086. this.subtitleSegmentLoader_.resetEverything();
  21087. } // start segment loader loading in case they are paused
  21088. this.load();
  21089. }
  21090. /**
  21091. * get the current duration
  21092. *
  21093. * @return {TimeRange} the duration
  21094. */
  21095. duration() {
  21096. if (!this.mainPlaylistLoader_) {
  21097. return 0;
  21098. }
  21099. const media = this.mainPlaylistLoader_.media();
  21100. if (!media) {
  21101. // no playlists loaded yet, so can't determine a duration
  21102. return 0;
  21103. } // Don't rely on the media source for duration in the case of a live playlist since
  21104. // setting the native MediaSource's duration to infinity ends up with consequences to
  21105. // seekable behavior. See https://github.com/w3c/media-source/issues/5 for details.
  21106. //
  21107. // This is resolved in the spec by https://github.com/w3c/media-source/pull/92,
  21108. // however, few browsers have support for setLiveSeekableRange()
  21109. // https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/setLiveSeekableRange
  21110. //
  21111. // Until a time when the duration of the media source can be set to infinity, and a
  21112. // seekable range specified across browsers, just return Infinity.
  21113. if (!media.endList) {
  21114. return Infinity;
  21115. } // Since this is a VOD video, it is safe to rely on the media source's duration (if
  21116. // available). If it's not available, fall back to a playlist-calculated estimate.
  21117. if (this.mediaSource) {
  21118. return this.mediaSource.duration;
  21119. }
  21120. return Vhs$1.Playlist.duration(media);
  21121. }
  21122. /**
  21123. * check the seekable range
  21124. *
  21125. * @return {TimeRange} the seekable range
  21126. */
  21127. seekable() {
  21128. return this.seekable_;
  21129. }
  21130. onSyncInfoUpdate_() {
  21131. let audioSeekable; // TODO check for creation of both source buffers before updating seekable
  21132. //
  21133. // A fix was made to this function where a check for
  21134. // this.sourceUpdater_.hasCreatedSourceBuffers
  21135. // was added to ensure that both source buffers were created before seekable was
  21136. // updated. However, it originally had a bug where it was checking for a true and
  21137. // returning early instead of checking for false. Setting it to check for false to
  21138. // return early though created other issues. A call to play() would check for seekable
  21139. // end without verifying that a seekable range was present. In addition, even checking
  21140. // for that didn't solve some issues, as handleFirstPlay is sometimes worked around
  21141. // due to a media update calling load on the segment loaders, skipping a seek to live,
  21142. // thereby starting live streams at the beginning of the stream rather than at the end.
  21143. //
  21144. // This conditional should be fixed to wait for the creation of two source buffers at
  21145. // the same time as the other sections of code are fixed to properly seek to live and
  21146. // not throw an error due to checking for a seekable end when no seekable range exists.
  21147. //
  21148. // For now, fall back to the older behavior, with the understanding that the seekable
  21149. // range may not be completely correct, leading to a suboptimal initial live point.
  21150. if (!this.mainPlaylistLoader_) {
  21151. return;
  21152. }
  21153. let media = this.mainPlaylistLoader_.media();
  21154. if (!media) {
  21155. return;
  21156. }
  21157. let expired = this.syncController_.getExpiredTime(media, this.duration());
  21158. if (expired === null) {
  21159. // not enough information to update seekable
  21160. return;
  21161. }
  21162. const main = this.mainPlaylistLoader_.main;
  21163. const mainSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(main, media));
  21164. if (mainSeekable.length === 0) {
  21165. return;
  21166. }
  21167. if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
  21168. media = this.mediaTypes_.AUDIO.activePlaylistLoader.media();
  21169. expired = this.syncController_.getExpiredTime(media, this.duration());
  21170. if (expired === null) {
  21171. return;
  21172. }
  21173. audioSeekable = Vhs$1.Playlist.seekable(media, expired, Vhs$1.Playlist.liveEdgeDelay(main, media));
  21174. if (audioSeekable.length === 0) {
  21175. return;
  21176. }
  21177. }
  21178. let oldEnd;
  21179. let oldStart;
  21180. if (this.seekable_ && this.seekable_.length) {
  21181. oldEnd = this.seekable_.end(0);
  21182. oldStart = this.seekable_.start(0);
  21183. }
  21184. if (!audioSeekable) {
  21185. // seekable has been calculated based on buffering video data so it
  21186. // can be returned directly
  21187. this.seekable_ = mainSeekable;
  21188. } else if (audioSeekable.start(0) > mainSeekable.end(0) || mainSeekable.start(0) > audioSeekable.end(0)) {
  21189. // seekables are pretty far off, rely on main
  21190. this.seekable_ = mainSeekable;
  21191. } else {
  21192. this.seekable_ = createTimeRanges([[audioSeekable.start(0) > mainSeekable.start(0) ? audioSeekable.start(0) : mainSeekable.start(0), audioSeekable.end(0) < mainSeekable.end(0) ? audioSeekable.end(0) : mainSeekable.end(0)]]);
  21193. } // seekable is the same as last time
  21194. if (this.seekable_ && this.seekable_.length) {
  21195. if (this.seekable_.end(0) === oldEnd && this.seekable_.start(0) === oldStart) {
  21196. return;
  21197. }
  21198. }
  21199. this.logger_(`seekable updated [${printableRange(this.seekable_)}]`);
  21200. this.tech_.trigger('seekablechanged');
  21201. }
  21202. /**
  21203. * Update the player duration
  21204. */
  21205. updateDuration(isLive) {
  21206. if (this.updateDuration_) {
  21207. this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
  21208. this.updateDuration_ = null;
  21209. }
  21210. if (this.mediaSource.readyState !== 'open') {
  21211. this.updateDuration_ = this.updateDuration.bind(this, isLive);
  21212. this.mediaSource.addEventListener('sourceopen', this.updateDuration_);
  21213. return;
  21214. }
  21215. if (isLive) {
  21216. const seekable = this.seekable();
  21217. if (!seekable.length) {
  21218. return;
  21219. } // Even in the case of a live playlist, the native MediaSource's duration should not
  21220. // be set to Infinity (even though this would be expected for a live playlist), since
  21221. // setting the native MediaSource's duration to infinity ends up with consequences to
  21222. // seekable behavior. See https://github.com/w3c/media-source/issues/5 for details.
  21223. //
  21224. // This is resolved in the spec by https://github.com/w3c/media-source/pull/92,
  21225. // however, few browsers have support for setLiveSeekableRange()
  21226. // https://developer.mozilla.org/en-US/docs/Web/API/MediaSource/setLiveSeekableRange
  21227. //
  21228. // Until a time when the duration of the media source can be set to infinity, and a
  21229. // seekable range specified across browsers, the duration should be greater than or
  21230. // equal to the last possible seekable value.
  21231. // MediaSource duration starts as NaN
  21232. // It is possible (and probable) that this case will never be reached for many
  21233. // sources, since the MediaSource reports duration as the highest value without
  21234. // accounting for timestamp offset. For example, if the timestamp offset is -100 and
  21235. // we buffered times 0 to 100 with real times of 100 to 200, even though current
  21236. // time will be between 0 and 100, the native media source may report the duration
  21237. // as 200. However, since we report duration separate from the media source (as
  21238. // Infinity), and as long as the native media source duration value is greater than
  21239. // our reported seekable range, seeks will work as expected. The large number as
  21240. // duration for live is actually a strategy used by some players to work around the
  21241. // issue of live seekable ranges cited above.
  21242. if (isNaN(this.mediaSource.duration) || this.mediaSource.duration < seekable.end(seekable.length - 1)) {
  21243. this.sourceUpdater_.setDuration(seekable.end(seekable.length - 1));
  21244. }
  21245. return;
  21246. }
  21247. const buffered = this.tech_.buffered();
  21248. let duration = Vhs$1.Playlist.duration(this.mainPlaylistLoader_.media());
  21249. if (buffered.length > 0) {
  21250. duration = Math.max(duration, buffered.end(buffered.length - 1));
  21251. }
  21252. if (this.mediaSource.duration !== duration) {
  21253. this.sourceUpdater_.setDuration(duration);
  21254. }
  21255. }
  21256. /**
  21257. * dispose of the PlaylistController and everything
  21258. * that it controls
  21259. */
  21260. dispose() {
  21261. this.trigger('dispose');
  21262. this.decrypter_.terminate();
  21263. this.mainPlaylistLoader_.dispose();
  21264. this.mainSegmentLoader_.dispose();
  21265. this.contentSteeringController_.dispose();
  21266. this.keyStatusMap_.clear();
  21267. if (this.loadOnPlay_) {
  21268. this.tech_.off('play', this.loadOnPlay_);
  21269. }
  21270. ['AUDIO', 'SUBTITLES'].forEach(type => {
  21271. const groups = this.mediaTypes_[type].groups;
  21272. for (const id in groups) {
  21273. groups[id].forEach(group => {
  21274. if (group.playlistLoader) {
  21275. group.playlistLoader.dispose();
  21276. }
  21277. });
  21278. }
  21279. });
  21280. this.audioSegmentLoader_.dispose();
  21281. this.subtitleSegmentLoader_.dispose();
  21282. this.sourceUpdater_.dispose();
  21283. this.timelineChangeController_.dispose();
  21284. this.stopABRTimer_();
  21285. if (this.updateDuration_) {
  21286. this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
  21287. }
  21288. this.mediaSource.removeEventListener('durationchange', this.handleDurationChange_); // load the media source into the player
  21289. this.mediaSource.removeEventListener('sourceopen', this.handleSourceOpen_);
  21290. this.mediaSource.removeEventListener('sourceended', this.handleSourceEnded_);
  21291. this.off();
  21292. }
  21293. /**
  21294. * return the main playlist object if we have one
  21295. *
  21296. * @return {Object} the main playlist object that we parsed
  21297. */
  21298. main() {
  21299. return this.mainPlaylistLoader_.main;
  21300. }
  21301. /**
  21302. * return the currently selected playlist
  21303. *
  21304. * @return {Object} the currently selected playlist object that we parsed
  21305. */
  21306. media() {
  21307. // playlist loader will not return media if it has not been fully loaded
  21308. return this.mainPlaylistLoader_.media() || this.initialMedia_;
  21309. }
  21310. areMediaTypesKnown_() {
  21311. const usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
  21312. const hasMainMediaInfo = !!this.mainSegmentLoader_.getCurrentMediaInfo_(); // if we are not using an audio loader, then we have audio media info
  21313. // otherwise check on the segment loader.
  21314. const hasAudioMediaInfo = !usingAudioLoader ? true : !!this.audioSegmentLoader_.getCurrentMediaInfo_(); // one or both loaders has not loaded sufficently to get codecs
  21315. if (!hasMainMediaInfo || !hasAudioMediaInfo) {
  21316. return false;
  21317. }
  21318. return true;
  21319. }
  21320. getCodecsOrExclude_() {
  21321. const media = {
  21322. main: this.mainSegmentLoader_.getCurrentMediaInfo_() || {},
  21323. audio: this.audioSegmentLoader_.getCurrentMediaInfo_() || {}
  21324. };
  21325. const playlist = this.mainSegmentLoader_.getPendingSegmentPlaylist() || this.media(); // set "main" media equal to video
  21326. media.video = media.main;
  21327. const playlistCodecs = codecsForPlaylist(this.main(), playlist);
  21328. const codecs = {};
  21329. const usingAudioLoader = !!this.mediaTypes_.AUDIO.activePlaylistLoader;
  21330. if (media.main.hasVideo) {
  21331. codecs.video = playlistCodecs.video || media.main.videoCodec || codecs_js.DEFAULT_VIDEO_CODEC;
  21332. }
  21333. if (media.main.isMuxed) {
  21334. codecs.video += `,${playlistCodecs.audio || media.main.audioCodec || codecs_js.DEFAULT_AUDIO_CODEC}`;
  21335. }
  21336. if (media.main.hasAudio && !media.main.isMuxed || media.audio.hasAudio || usingAudioLoader) {
  21337. codecs.audio = playlistCodecs.audio || media.main.audioCodec || media.audio.audioCodec || codecs_js.DEFAULT_AUDIO_CODEC; // set audio isFmp4 so we use the correct "supports" function below
  21338. media.audio.isFmp4 = media.main.hasAudio && !media.main.isMuxed ? media.main.isFmp4 : media.audio.isFmp4;
  21339. } // no codecs, no playback.
  21340. if (!codecs.audio && !codecs.video) {
  21341. this.excludePlaylist({
  21342. playlistToExclude: playlist,
  21343. error: {
  21344. message: 'Could not determine codecs for playlist.'
  21345. },
  21346. playlistExclusionDuration: Infinity
  21347. });
  21348. return;
  21349. } // fmp4 relies on browser support, while ts relies on muxer support
  21350. const supportFunction = (isFmp4, codec) => isFmp4 ? codecs_js.browserSupportsCodec(codec) : codecs_js.muxerSupportsCodec(codec);
  21351. const unsupportedCodecs = {};
  21352. let unsupportedAudio;
  21353. ['video', 'audio'].forEach(function (type) {
  21354. if (codecs.hasOwnProperty(type) && !supportFunction(media[type].isFmp4, codecs[type])) {
  21355. const supporter = media[type].isFmp4 ? 'browser' : 'muxer';
  21356. unsupportedCodecs[supporter] = unsupportedCodecs[supporter] || [];
  21357. unsupportedCodecs[supporter].push(codecs[type]);
  21358. if (type === 'audio') {
  21359. unsupportedAudio = supporter;
  21360. }
  21361. }
  21362. });
  21363. if (usingAudioLoader && unsupportedAudio && playlist.attributes.AUDIO) {
  21364. const audioGroup = playlist.attributes.AUDIO;
  21365. this.main().playlists.forEach(variant => {
  21366. const variantAudioGroup = variant.attributes && variant.attributes.AUDIO;
  21367. if (variantAudioGroup === audioGroup && variant !== playlist) {
  21368. variant.excludeUntil = Infinity;
  21369. }
  21370. });
  21371. this.logger_(`excluding audio group ${audioGroup} as ${unsupportedAudio} does not support codec(s): "${codecs.audio}"`);
  21372. } // if we have any unsupported codecs exclude this playlist.
  21373. if (Object.keys(unsupportedCodecs).length) {
  21374. const message = Object.keys(unsupportedCodecs).reduce((acc, supporter) => {
  21375. if (acc) {
  21376. acc += ', ';
  21377. }
  21378. acc += `${supporter} does not support codec(s): "${unsupportedCodecs[supporter].join(',')}"`;
  21379. return acc;
  21380. }, '') + '.';
  21381. this.excludePlaylist({
  21382. playlistToExclude: playlist,
  21383. error: {
  21384. internal: true,
  21385. message
  21386. },
  21387. playlistExclusionDuration: Infinity
  21388. });
  21389. return;
  21390. } // check if codec switching is happening
  21391. if (this.sourceUpdater_.hasCreatedSourceBuffers() && !this.sourceUpdater_.canChangeType()) {
  21392. const switchMessages = [];
  21393. ['video', 'audio'].forEach(type => {
  21394. const newCodec = (codecs_js.parseCodecs(this.sourceUpdater_.codecs[type] || '')[0] || {}).type;
  21395. const oldCodec = (codecs_js.parseCodecs(codecs[type] || '')[0] || {}).type;
  21396. if (newCodec && oldCodec && newCodec.toLowerCase() !== oldCodec.toLowerCase()) {
  21397. switchMessages.push(`"${this.sourceUpdater_.codecs[type]}" -> "${codecs[type]}"`);
  21398. }
  21399. });
  21400. if (switchMessages.length) {
  21401. this.excludePlaylist({
  21402. playlistToExclude: playlist,
  21403. error: {
  21404. message: `Codec switching not supported: ${switchMessages.join(', ')}.`,
  21405. internal: true
  21406. },
  21407. playlistExclusionDuration: Infinity
  21408. });
  21409. return;
  21410. }
  21411. } // TODO: when using the muxer shouldn't we just return
  21412. // the codecs that the muxer outputs?
  21413. return codecs;
  21414. }
  21415. /**
  21416. * Create source buffers and exlude any incompatible renditions.
  21417. *
  21418. * @private
  21419. */
  21420. tryToCreateSourceBuffers_() {
  21421. // media source is not ready yet or sourceBuffers are already
  21422. // created.
  21423. if (this.mediaSource.readyState !== 'open' || this.sourceUpdater_.hasCreatedSourceBuffers()) {
  21424. return;
  21425. }
  21426. if (!this.areMediaTypesKnown_()) {
  21427. return;
  21428. }
  21429. const codecs = this.getCodecsOrExclude_(); // no codecs means that the playlist was excluded
  21430. if (!codecs) {
  21431. return;
  21432. }
  21433. this.sourceUpdater_.createSourceBuffers(codecs);
  21434. const codecString = [codecs.video, codecs.audio].filter(Boolean).join(',');
  21435. this.excludeIncompatibleVariants_(codecString);
  21436. }
  21437. /**
  21438. * Excludes playlists with codecs that are unsupported by the muxer and browser.
  21439. */
  21440. excludeUnsupportedVariants_() {
  21441. const playlists = this.main().playlists;
  21442. const ids = []; // TODO: why don't we have a property to loop through all
  21443. // playlist? Why did we ever mix indexes and keys?
  21444. Object.keys(playlists).forEach(key => {
  21445. const variant = playlists[key]; // check if we already processed this playlist.
  21446. if (ids.indexOf(variant.id) !== -1) {
  21447. return;
  21448. }
  21449. ids.push(variant.id);
  21450. const codecs = codecsForPlaylist(this.main, variant);
  21451. const unsupported = [];
  21452. if (codecs.audio && !codecs_js.muxerSupportsCodec(codecs.audio) && !codecs_js.browserSupportsCodec(codecs.audio)) {
  21453. unsupported.push(`audio codec ${codecs.audio}`);
  21454. }
  21455. if (codecs.video && !codecs_js.muxerSupportsCodec(codecs.video) && !codecs_js.browserSupportsCodec(codecs.video)) {
  21456. unsupported.push(`video codec ${codecs.video}`);
  21457. }
  21458. if (codecs.text && codecs.text === 'stpp.ttml.im1t') {
  21459. unsupported.push(`text codec ${codecs.text}`);
  21460. }
  21461. if (unsupported.length) {
  21462. variant.excludeUntil = Infinity;
  21463. this.logger_(`excluding ${variant.id} for unsupported: ${unsupported.join(', ')}`);
  21464. }
  21465. });
  21466. }
  21467. /**
  21468. * Exclude playlists that are known to be codec or
  21469. * stream-incompatible with the SourceBuffer configuration. For
  21470. * instance, Media Source Extensions would cause the video element to
  21471. * stall waiting for video data if you switched from a variant with
  21472. * video and audio to an audio-only one.
  21473. *
  21474. * @param {Object} media a media playlist compatible with the current
  21475. * set of SourceBuffers. Variants in the current main playlist that
  21476. * do not appear to have compatible codec or stream configurations
  21477. * will be excluded from the default playlist selection algorithm
  21478. * indefinitely.
  21479. * @private
  21480. */
  21481. excludeIncompatibleVariants_(codecString) {
  21482. const ids = [];
  21483. const playlists = this.main().playlists;
  21484. const codecs = unwrapCodecList(codecs_js.parseCodecs(codecString));
  21485. const codecCount_ = codecCount(codecs);
  21486. const videoDetails = codecs.video && codecs_js.parseCodecs(codecs.video)[0] || null;
  21487. const audioDetails = codecs.audio && codecs_js.parseCodecs(codecs.audio)[0] || null;
  21488. Object.keys(playlists).forEach(key => {
  21489. const variant = playlists[key]; // check if we already processed this playlist.
  21490. // or it if it is already excluded forever.
  21491. if (ids.indexOf(variant.id) !== -1 || variant.excludeUntil === Infinity) {
  21492. return;
  21493. }
  21494. ids.push(variant.id);
  21495. const exclusionReasons = []; // get codecs from the playlist for this variant
  21496. const variantCodecs = codecsForPlaylist(this.mainPlaylistLoader_.main, variant);
  21497. const variantCodecCount = codecCount(variantCodecs); // if no codecs are listed, we cannot determine that this
  21498. // variant is incompatible. Wait for mux.js to probe
  21499. if (!variantCodecs.audio && !variantCodecs.video) {
  21500. return;
  21501. } // TODO: we can support this by removing the
  21502. // old media source and creating a new one, but it will take some work.
  21503. // The number of streams cannot change
  21504. if (variantCodecCount !== codecCount_) {
  21505. exclusionReasons.push(`codec count "${variantCodecCount}" !== "${codecCount_}"`);
  21506. } // only exclude playlists by codec change, if codecs cannot switch
  21507. // during playback.
  21508. if (!this.sourceUpdater_.canChangeType()) {
  21509. const variantVideoDetails = variantCodecs.video && codecs_js.parseCodecs(variantCodecs.video)[0] || null;
  21510. const variantAudioDetails = variantCodecs.audio && codecs_js.parseCodecs(variantCodecs.audio)[0] || null; // the video codec cannot change
  21511. if (variantVideoDetails && videoDetails && variantVideoDetails.type.toLowerCase() !== videoDetails.type.toLowerCase()) {
  21512. exclusionReasons.push(`video codec "${variantVideoDetails.type}" !== "${videoDetails.type}"`);
  21513. } // the audio codec cannot change
  21514. if (variantAudioDetails && audioDetails && variantAudioDetails.type.toLowerCase() !== audioDetails.type.toLowerCase()) {
  21515. exclusionReasons.push(`audio codec "${variantAudioDetails.type}" !== "${audioDetails.type}"`);
  21516. }
  21517. }
  21518. if (exclusionReasons.length) {
  21519. variant.excludeUntil = Infinity;
  21520. this.logger_(`excluding ${variant.id}: ${exclusionReasons.join(' && ')}`);
  21521. }
  21522. });
  21523. }
  21524. updateAdCues_(media) {
  21525. let offset = 0;
  21526. const seekable = this.seekable();
  21527. if (seekable.length) {
  21528. offset = seekable.start(0);
  21529. }
  21530. updateAdCues(media, this.cueTagsTrack_, offset);
  21531. }
  21532. /**
  21533. * Calculates the desired forward buffer length based on current time
  21534. *
  21535. * @return {number} Desired forward buffer length in seconds
  21536. */
  21537. goalBufferLength() {
  21538. const currentTime = this.tech_.currentTime();
  21539. const initial = Config.GOAL_BUFFER_LENGTH;
  21540. const rate = Config.GOAL_BUFFER_LENGTH_RATE;
  21541. const max = Math.max(initial, Config.MAX_GOAL_BUFFER_LENGTH);
  21542. return Math.min(initial + currentTime * rate, max);
  21543. }
  21544. /**
  21545. * Calculates the desired buffer low water line based on current time
  21546. *
  21547. * @return {number} Desired buffer low water line in seconds
  21548. */
  21549. bufferLowWaterLine() {
  21550. const currentTime = this.tech_.currentTime();
  21551. const initial = Config.BUFFER_LOW_WATER_LINE;
  21552. const rate = Config.BUFFER_LOW_WATER_LINE_RATE;
  21553. const max = Math.max(initial, Config.MAX_BUFFER_LOW_WATER_LINE);
  21554. const newMax = Math.max(initial, Config.EXPERIMENTAL_MAX_BUFFER_LOW_WATER_LINE);
  21555. return Math.min(initial + currentTime * rate, this.bufferBasedABR ? newMax : max);
  21556. }
  21557. bufferHighWaterLine() {
  21558. return Config.BUFFER_HIGH_WATER_LINE;
  21559. }
  21560. addDateRangesToTextTrack_(dateRanges) {
  21561. createMetadataTrackIfNotExists(this.inbandTextTracks_, 'com.apple.streaming', this.tech_);
  21562. addDateRangeMetadata({
  21563. inbandTextTracks: this.inbandTextTracks_,
  21564. dateRanges
  21565. });
  21566. }
  21567. addMetadataToTextTrack(dispatchType, metadataArray, videoDuration) {
  21568. const timestampOffset = this.sourceUpdater_.videoBuffer ? this.sourceUpdater_.videoTimestampOffset() : this.sourceUpdater_.audioTimestampOffset(); // There's potentially an issue where we could double add metadata if there's a muxed
  21569. // audio/video source with a metadata track, and an alt audio with a metadata track.
  21570. // However, this probably won't happen, and if it does it can be handled then.
  21571. createMetadataTrackIfNotExists(this.inbandTextTracks_, dispatchType, this.tech_);
  21572. addMetadata({
  21573. inbandTextTracks: this.inbandTextTracks_,
  21574. metadataArray,
  21575. timestampOffset,
  21576. videoDuration
  21577. });
  21578. }
  21579. /**
  21580. * Utility for getting the pathway or service location from an HLS or DASH playlist.
  21581. *
  21582. * @param {Object} playlist for getting pathway from.
  21583. * @return the pathway attribute of a playlist
  21584. */
  21585. pathwayAttribute_(playlist) {
  21586. return playlist.attributes['PATHWAY-ID'] || playlist.attributes.serviceLocation;
  21587. }
  21588. /**
  21589. * Initialize available pathways and apply the tag properties.
  21590. */
  21591. initContentSteeringController_() {
  21592. const main = this.main();
  21593. if (!main.contentSteering) {
  21594. return;
  21595. }
  21596. for (const playlist of main.playlists) {
  21597. this.contentSteeringController_.addAvailablePathway(this.pathwayAttribute_(playlist));
  21598. }
  21599. this.contentSteeringController_.assignTagProperties(main.uri, main.contentSteering); // request the steering manifest immediately if queryBeforeStart is set.
  21600. if (this.contentSteeringController_.queryBeforeStart) {
  21601. // When queryBeforeStart is true, initial request should omit steering parameters.
  21602. this.contentSteeringController_.requestSteeringManifest(true);
  21603. return;
  21604. } // otherwise start content steering after playback starts
  21605. this.tech_.one('canplay', () => {
  21606. this.contentSteeringController_.requestSteeringManifest();
  21607. });
  21608. }
  21609. /**
  21610. * Reset the content steering controller and re-init.
  21611. */
  21612. resetContentSteeringController_() {
  21613. this.contentSteeringController_.clearAvailablePathways();
  21614. this.contentSteeringController_.dispose();
  21615. this.initContentSteeringController_();
  21616. }
  21617. /**
  21618. * Attaches the listeners for content steering.
  21619. */
  21620. attachContentSteeringListeners_() {
  21621. this.contentSteeringController_.on('content-steering', this.excludeThenChangePathway_.bind(this));
  21622. if (this.sourceType_ === 'dash') {
  21623. this.mainPlaylistLoader_.on('loadedplaylist', () => {
  21624. const main = this.main(); // check if steering tag or pathways changed.
  21625. const didDashTagChange = this.contentSteeringController_.didDASHTagChange(main.uri, main.contentSteering);
  21626. const didPathwaysChange = () => {
  21627. const availablePathways = this.contentSteeringController_.getAvailablePathways();
  21628. const newPathways = [];
  21629. for (const playlist of main.playlists) {
  21630. const serviceLocation = playlist.attributes.serviceLocation;
  21631. if (serviceLocation) {
  21632. newPathways.push(serviceLocation);
  21633. if (!availablePathways.has(serviceLocation)) {
  21634. return true;
  21635. }
  21636. }
  21637. } // If we have no new serviceLocations and previously had availablePathways
  21638. if (!newPathways.length && availablePathways.size) {
  21639. return true;
  21640. }
  21641. return false;
  21642. };
  21643. if (didDashTagChange || didPathwaysChange()) {
  21644. this.resetContentSteeringController_();
  21645. }
  21646. });
  21647. }
  21648. }
  21649. /**
  21650. * Simple exclude and change playlist logic for content steering.
  21651. */
  21652. excludeThenChangePathway_() {
  21653. const currentPathway = this.contentSteeringController_.getPathway();
  21654. if (!currentPathway) {
  21655. return;
  21656. }
  21657. this.handlePathwayClones_();
  21658. const main = this.main();
  21659. const playlists = main.playlists;
  21660. const ids = new Set();
  21661. let didEnablePlaylists = false;
  21662. Object.keys(playlists).forEach(key => {
  21663. const variant = playlists[key];
  21664. const pathwayId = this.pathwayAttribute_(variant);
  21665. const differentPathwayId = pathwayId && currentPathway !== pathwayId;
  21666. const steeringExclusion = variant.excludeUntil === Infinity && variant.lastExcludeReason_ === 'content-steering';
  21667. if (steeringExclusion && !differentPathwayId) {
  21668. delete variant.excludeUntil;
  21669. delete variant.lastExcludeReason_;
  21670. didEnablePlaylists = true;
  21671. }
  21672. const noExcludeUntil = !variant.excludeUntil && variant.excludeUntil !== Infinity;
  21673. const shouldExclude = !ids.has(variant.id) && differentPathwayId && noExcludeUntil;
  21674. if (!shouldExclude) {
  21675. return;
  21676. }
  21677. ids.add(variant.id);
  21678. variant.excludeUntil = Infinity;
  21679. variant.lastExcludeReason_ = 'content-steering'; // TODO: kind of spammy, maybe move this.
  21680. this.logger_(`excluding ${variant.id} for ${variant.lastExcludeReason_}`);
  21681. });
  21682. if (this.contentSteeringController_.manifestType_ === 'DASH') {
  21683. Object.keys(this.mediaTypes_).forEach(key => {
  21684. const type = this.mediaTypes_[key];
  21685. if (type.activePlaylistLoader) {
  21686. const currentPlaylist = type.activePlaylistLoader.media_; // Check if the current media playlist matches the current CDN
  21687. if (currentPlaylist && currentPlaylist.attributes.serviceLocation !== currentPathway) {
  21688. didEnablePlaylists = true;
  21689. }
  21690. }
  21691. });
  21692. }
  21693. if (didEnablePlaylists) {
  21694. this.changeSegmentPathway_();
  21695. }
  21696. }
  21697. /**
  21698. * Add, update, or delete playlists and media groups for
  21699. * the pathway clones for HLS Content Steering.
  21700. *
  21701. * See https://datatracker.ietf.org/doc/draft-pantos-hls-rfc8216bis/
  21702. *
  21703. * NOTE: Pathway cloning does not currently support the `PER_VARIANT_URIS` and
  21704. * `PER_RENDITION_URIS` as we do not handle `STABLE-VARIANT-ID` or
  21705. * `STABLE-RENDITION-ID` values.
  21706. */
  21707. handlePathwayClones_() {
  21708. const main = this.main();
  21709. const playlists = main.playlists;
  21710. const currentPathwayClones = this.contentSteeringController_.currentPathwayClones;
  21711. const nextPathwayClones = this.contentSteeringController_.nextPathwayClones;
  21712. const hasClones = currentPathwayClones && currentPathwayClones.size || nextPathwayClones && nextPathwayClones.size;
  21713. if (!hasClones) {
  21714. return;
  21715. }
  21716. for (const [id, clone] of currentPathwayClones.entries()) {
  21717. const newClone = nextPathwayClones.get(id); // Delete the old pathway clone.
  21718. if (!newClone) {
  21719. this.mainPlaylistLoader_.updateOrDeleteClone(clone);
  21720. this.contentSteeringController_.excludePathway(id);
  21721. }
  21722. }
  21723. for (const [id, clone] of nextPathwayClones.entries()) {
  21724. const oldClone = currentPathwayClones.get(id); // Create a new pathway if it is a new pathway clone object.
  21725. if (!oldClone) {
  21726. const playlistsToClone = playlists.filter(p => {
  21727. return p.attributes['PATHWAY-ID'] === clone['BASE-ID'];
  21728. });
  21729. playlistsToClone.forEach(p => {
  21730. this.mainPlaylistLoader_.addClonePathway(clone, p);
  21731. });
  21732. this.contentSteeringController_.addAvailablePathway(id);
  21733. continue;
  21734. } // There have not been changes to the pathway clone object, so skip.
  21735. if (this.equalPathwayClones_(oldClone, clone)) {
  21736. continue;
  21737. } // Update a preexisting cloned pathway.
  21738. // True is set for the update flag.
  21739. this.mainPlaylistLoader_.updateOrDeleteClone(clone, true);
  21740. this.contentSteeringController_.addAvailablePathway(id);
  21741. } // Deep copy contents of next to current pathways.
  21742. this.contentSteeringController_.currentPathwayClones = new Map(JSON.parse(JSON.stringify([...nextPathwayClones])));
  21743. }
  21744. /**
  21745. * Determines whether two pathway clone objects are equivalent.
  21746. *
  21747. * @param {Object} a The first pathway clone object.
  21748. * @param {Object} b The second pathway clone object.
  21749. * @return {boolean} True if the pathway clone objects are equal, false otherwise.
  21750. */
  21751. equalPathwayClones_(a, b) {
  21752. if (a['BASE-ID'] !== b['BASE-ID'] || a.ID !== b.ID || a['URI-REPLACEMENT'].HOST !== b['URI-REPLACEMENT'].HOST) {
  21753. return false;
  21754. }
  21755. const aParams = a['URI-REPLACEMENT'].PARAMS;
  21756. const bParams = b['URI-REPLACEMENT'].PARAMS; // We need to iterate through both lists of params because one could be
  21757. // missing a parameter that the other has.
  21758. for (const p in aParams) {
  21759. if (aParams[p] !== bParams[p]) {
  21760. return false;
  21761. }
  21762. }
  21763. for (const p in bParams) {
  21764. if (aParams[p] !== bParams[p]) {
  21765. return false;
  21766. }
  21767. }
  21768. return true;
  21769. }
  21770. /**
  21771. * Changes the current playlists for audio, video and subtitles after a new pathway
  21772. * is chosen from content steering.
  21773. */
  21774. changeSegmentPathway_() {
  21775. const nextPlaylist = this.selectPlaylist();
  21776. this.pauseLoading(); // Switch audio and text track playlists if necessary in DASH
  21777. if (this.contentSteeringController_.manifestType_ === 'DASH') {
  21778. this.switchMediaForDASHContentSteering_();
  21779. }
  21780. this.switchMedia_(nextPlaylist, 'content-steering');
  21781. }
  21782. /**
  21783. * Iterates through playlists and check their keyId set and compare with the
  21784. * keyStatusMap, only enable playlists that have a usable key. If the playlist
  21785. * has no keyId leave it enabled by default.
  21786. */
  21787. excludeNonUsablePlaylistsByKeyId_() {
  21788. if (!this.mainPlaylistLoader_ || !this.mainPlaylistLoader_.main) {
  21789. return;
  21790. }
  21791. let nonUsableKeyStatusCount = 0;
  21792. const NON_USABLE = 'non-usable';
  21793. this.mainPlaylistLoader_.main.playlists.forEach(playlist => {
  21794. const keyIdSet = this.mainPlaylistLoader_.getKeyIdSet(playlist); // If the playlist doesn't have keyIDs lets not exclude it.
  21795. if (!keyIdSet || !keyIdSet.size) {
  21796. return;
  21797. }
  21798. keyIdSet.forEach(key => {
  21799. const USABLE = 'usable';
  21800. const hasUsableKeyStatus = this.keyStatusMap_.has(key) && this.keyStatusMap_.get(key) === USABLE;
  21801. const nonUsableExclusion = playlist.lastExcludeReason_ === NON_USABLE && playlist.excludeUntil === Infinity;
  21802. if (!hasUsableKeyStatus) {
  21803. // Only exclude playlists that haven't already been excluded as non-usable.
  21804. if (playlist.excludeUntil !== Infinity && playlist.lastExcludeReason_ !== NON_USABLE) {
  21805. playlist.excludeUntil = Infinity;
  21806. playlist.lastExcludeReason_ = NON_USABLE;
  21807. this.logger_(`excluding playlist ${playlist.id} because the key ID ${key} doesn't exist in the keyStatusMap or is not ${USABLE}`);
  21808. } // count all nonUsableKeyStatus
  21809. nonUsableKeyStatusCount++;
  21810. } else if (hasUsableKeyStatus && nonUsableExclusion) {
  21811. delete playlist.excludeUntil;
  21812. delete playlist.lastExcludeReason_;
  21813. this.logger_(`enabling playlist ${playlist.id} because key ID ${key} is ${USABLE}`);
  21814. }
  21815. });
  21816. }); // If for whatever reason every playlist has a non usable key status. Lets try re-including the SD renditions as a failsafe.
  21817. if (nonUsableKeyStatusCount >= this.mainPlaylistLoader_.main.playlists.length) {
  21818. this.mainPlaylistLoader_.main.playlists.forEach(playlist => {
  21819. const isNonHD = playlist && playlist.attributes && playlist.attributes.RESOLUTION && playlist.attributes.RESOLUTION.height < 720;
  21820. const excludedForNonUsableKey = playlist.excludeUntil === Infinity && playlist.lastExcludeReason_ === NON_USABLE;
  21821. if (isNonHD && excludedForNonUsableKey) {
  21822. // Only delete the excludeUntil so we don't try and re-exclude these playlists.
  21823. delete playlist.excludeUntil;
  21824. videojs__default["default"].log.warn(`enabling non-HD playlist ${playlist.id} because all playlists were excluded due to ${NON_USABLE} key IDs`);
  21825. }
  21826. });
  21827. }
  21828. }
  21829. /**
  21830. * Adds a keystatus to the keystatus map, tries to convert to string if necessary.
  21831. *
  21832. * @param {any} keyId the keyId to add a status for
  21833. * @param {string} status the status of the keyId
  21834. */
  21835. addKeyStatus_(keyId, status) {
  21836. const isString = typeof keyId === 'string';
  21837. const keyIdHexString = isString ? keyId : bufferToHexString(keyId);
  21838. const formattedKeyIdString = keyIdHexString.slice(0, 32).toLowerCase();
  21839. this.logger_(`KeyStatus '${status}' with key ID ${formattedKeyIdString} added to the keyStatusMap`);
  21840. this.keyStatusMap_.set(formattedKeyIdString, status);
  21841. }
  21842. /**
  21843. * Utility function for adding key status to the keyStatusMap and filtering usable encrypted playlists.
  21844. *
  21845. * @param {any} keyId the keyId from the keystatuschange event
  21846. * @param {string} status the key status string
  21847. */
  21848. updatePlaylistByKeyStatus(keyId, status) {
  21849. this.addKeyStatus_(keyId, status);
  21850. if (!this.waitingForFastQualityPlaylistReceived_) {
  21851. this.excludeNonUsableThenChangePlaylist_();
  21852. } // Listen to loadedplaylist with a single listener and check for new contentProtection elements when a playlist is updated.
  21853. this.mainPlaylistLoader_.off('loadedplaylist', this.excludeNonUsableThenChangePlaylist_.bind(this));
  21854. this.mainPlaylistLoader_.on('loadedplaylist', this.excludeNonUsableThenChangePlaylist_.bind(this));
  21855. }
  21856. excludeNonUsableThenChangePlaylist_() {
  21857. this.excludeNonUsablePlaylistsByKeyId_();
  21858. this.fastQualityChange_();
  21859. }
  21860. }
  21861. /**
  21862. * Returns a function that acts as the Enable/disable playlist function.
  21863. *
  21864. * @param {PlaylistLoader} loader - The main playlist loader
  21865. * @param {string} playlistID - id of the playlist
  21866. * @param {Function} changePlaylistFn - A function to be called after a
  21867. * playlist's enabled-state has been changed. Will NOT be called if a
  21868. * playlist's enabled-state is unchanged
  21869. * @param {boolean=} enable - Value to set the playlist enabled-state to
  21870. * or if undefined returns the current enabled-state for the playlist
  21871. * @return {Function} Function for setting/getting enabled
  21872. */
  21873. const enableFunction = (loader, playlistID, changePlaylistFn) => enable => {
  21874. const playlist = loader.main.playlists[playlistID];
  21875. const incompatible = isIncompatible(playlist);
  21876. const currentlyEnabled = isEnabled(playlist);
  21877. if (typeof enable === 'undefined') {
  21878. return currentlyEnabled;
  21879. }
  21880. if (enable) {
  21881. delete playlist.disabled;
  21882. } else {
  21883. playlist.disabled = true;
  21884. }
  21885. if (enable !== currentlyEnabled && !incompatible) {
  21886. // Ensure the outside world knows about our changes
  21887. changePlaylistFn();
  21888. if (enable) {
  21889. loader.trigger('renditionenabled');
  21890. } else {
  21891. loader.trigger('renditiondisabled');
  21892. }
  21893. }
  21894. return enable;
  21895. };
  21896. /**
  21897. * The representation object encapsulates the publicly visible information
  21898. * in a media playlist along with a setter/getter-type function (enabled)
  21899. * for changing the enabled-state of a particular playlist entry
  21900. *
  21901. * @class Representation
  21902. */
  21903. class Representation {
  21904. constructor(vhsHandler, playlist, id) {
  21905. const {
  21906. playlistController_: pc
  21907. } = vhsHandler;
  21908. const qualityChangeFunction = pc.fastQualityChange_.bind(pc); // some playlist attributes are optional
  21909. if (playlist.attributes) {
  21910. const resolution = playlist.attributes.RESOLUTION;
  21911. this.width = resolution && resolution.width;
  21912. this.height = resolution && resolution.height;
  21913. this.bandwidth = playlist.attributes.BANDWIDTH;
  21914. this.frameRate = playlist.attributes['FRAME-RATE'];
  21915. }
  21916. this.codecs = codecsForPlaylist(pc.main(), playlist);
  21917. this.playlist = playlist; // The id is simply the ordinality of the media playlist
  21918. // within the main playlist
  21919. this.id = id; // Partially-apply the enableFunction to create a playlist-
  21920. // specific variant
  21921. this.enabled = enableFunction(vhsHandler.playlists, playlist.id, qualityChangeFunction);
  21922. }
  21923. }
  21924. /**
  21925. * A mixin function that adds the `representations` api to an instance
  21926. * of the VhsHandler class
  21927. *
  21928. * @param {VhsHandler} vhsHandler - An instance of VhsHandler to add the
  21929. * representation API into
  21930. */
  21931. const renditionSelectionMixin = function (vhsHandler) {
  21932. // Add a single API-specific function to the VhsHandler instance
  21933. vhsHandler.representations = () => {
  21934. const main = vhsHandler.playlistController_.main();
  21935. const playlists = isAudioOnly(main) ? vhsHandler.playlistController_.getAudioTrackPlaylists_() : main.playlists;
  21936. if (!playlists) {
  21937. return [];
  21938. }
  21939. return playlists.filter(media => !isIncompatible(media)).map((e, i) => new Representation(vhsHandler, e, e.id));
  21940. };
  21941. };
  21942. /**
  21943. * @file playback-watcher.js
  21944. *
  21945. * Playback starts, and now my watch begins. It shall not end until my death. I shall
  21946. * take no wait, hold no uncleared timeouts, father no bad seeks. I shall wear no crowns
  21947. * and win no glory. I shall live and die at my post. I am the corrector of the underflow.
  21948. * I am the watcher of gaps. I am the shield that guards the realms of seekable. I pledge
  21949. * my life and honor to the Playback Watch, for this Player and all the Players to come.
  21950. */
  21951. const timerCancelEvents = ['seeking', 'seeked', 'pause', 'playing', 'error'];
  21952. /**
  21953. * @class PlaybackWatcher
  21954. */
  21955. class PlaybackWatcher {
  21956. /**
  21957. * Represents an PlaybackWatcher object.
  21958. *
  21959. * @class
  21960. * @param {Object} options an object that includes the tech and settings
  21961. */
  21962. constructor(options) {
  21963. this.playlistController_ = options.playlistController;
  21964. this.tech_ = options.tech;
  21965. this.seekable = options.seekable;
  21966. this.allowSeeksWithinUnsafeLiveWindow = options.allowSeeksWithinUnsafeLiveWindow;
  21967. this.liveRangeSafeTimeDelta = options.liveRangeSafeTimeDelta;
  21968. this.media = options.media;
  21969. this.consecutiveUpdates = 0;
  21970. this.lastRecordedTime = null;
  21971. this.checkCurrentTimeTimeout_ = null;
  21972. this.logger_ = logger('PlaybackWatcher');
  21973. this.logger_('initialize');
  21974. const playHandler = () => this.monitorCurrentTime_();
  21975. const canPlayHandler = () => this.monitorCurrentTime_();
  21976. const waitingHandler = () => this.techWaiting_();
  21977. const cancelTimerHandler = () => this.resetTimeUpdate_();
  21978. const pc = this.playlistController_;
  21979. const loaderTypes = ['main', 'subtitle', 'audio'];
  21980. const loaderChecks = {};
  21981. loaderTypes.forEach(type => {
  21982. loaderChecks[type] = {
  21983. reset: () => this.resetSegmentDownloads_(type),
  21984. updateend: () => this.checkSegmentDownloads_(type)
  21985. };
  21986. pc[`${type}SegmentLoader_`].on('appendsdone', loaderChecks[type].updateend); // If a rendition switch happens during a playback stall where the buffer
  21987. // isn't changing we want to reset. We cannot assume that the new rendition
  21988. // will also be stalled, until after new appends.
  21989. pc[`${type}SegmentLoader_`].on('playlistupdate', loaderChecks[type].reset); // Playback stalls should not be detected right after seeking.
  21990. // This prevents one segment playlists (single vtt or single segment content)
  21991. // from being detected as stalling. As the buffer will not change in those cases, since
  21992. // the buffer is the entire video duration.
  21993. this.tech_.on(['seeked', 'seeking'], loaderChecks[type].reset);
  21994. });
  21995. /**
  21996. * We check if a seek was into a gap through the following steps:
  21997. * 1. We get a seeking event and we do not get a seeked event. This means that
  21998. * a seek was attempted but not completed.
  21999. * 2. We run `fixesBadSeeks_` on segment loader appends. This means that we already
  22000. * removed everything from our buffer and appended a segment, and should be ready
  22001. * to check for gaps.
  22002. */
  22003. const setSeekingHandlers = fn => {
  22004. ['main', 'audio'].forEach(type => {
  22005. pc[`${type}SegmentLoader_`][fn]('appended', this.seekingAppendCheck_);
  22006. });
  22007. };
  22008. this.seekingAppendCheck_ = () => {
  22009. if (this.fixesBadSeeks_()) {
  22010. this.consecutiveUpdates = 0;
  22011. this.lastRecordedTime = this.tech_.currentTime();
  22012. setSeekingHandlers('off');
  22013. }
  22014. };
  22015. this.clearSeekingAppendCheck_ = () => setSeekingHandlers('off');
  22016. this.watchForBadSeeking_ = () => {
  22017. this.clearSeekingAppendCheck_();
  22018. setSeekingHandlers('on');
  22019. };
  22020. this.tech_.on('seeked', this.clearSeekingAppendCheck_);
  22021. this.tech_.on('seeking', this.watchForBadSeeking_);
  22022. this.tech_.on('waiting', waitingHandler);
  22023. this.tech_.on(timerCancelEvents, cancelTimerHandler);
  22024. this.tech_.on('canplay', canPlayHandler);
  22025. /*
  22026. An edge case exists that results in gaps not being skipped when they exist at the beginning of a stream. This case
  22027. is surfaced in one of two ways:
  22028. 1) The `waiting` event is fired before the player has buffered content, making it impossible
  22029. to find or skip the gap. The `waiting` event is followed by a `play` event. On first play
  22030. we can check if playback is stalled due to a gap, and skip the gap if necessary.
  22031. 2) A source with a gap at the beginning of the stream is loaded programatically while the player
  22032. is in a playing state. To catch this case, it's important that our one-time play listener is setup
  22033. even if the player is in a playing state
  22034. */
  22035. this.tech_.one('play', playHandler); // Define the dispose function to clean up our events
  22036. this.dispose = () => {
  22037. this.clearSeekingAppendCheck_();
  22038. this.logger_('dispose');
  22039. this.tech_.off('waiting', waitingHandler);
  22040. this.tech_.off(timerCancelEvents, cancelTimerHandler);
  22041. this.tech_.off('canplay', canPlayHandler);
  22042. this.tech_.off('play', playHandler);
  22043. this.tech_.off('seeking', this.watchForBadSeeking_);
  22044. this.tech_.off('seeked', this.clearSeekingAppendCheck_);
  22045. loaderTypes.forEach(type => {
  22046. pc[`${type}SegmentLoader_`].off('appendsdone', loaderChecks[type].updateend);
  22047. pc[`${type}SegmentLoader_`].off('playlistupdate', loaderChecks[type].reset);
  22048. this.tech_.off(['seeked', 'seeking'], loaderChecks[type].reset);
  22049. });
  22050. if (this.checkCurrentTimeTimeout_) {
  22051. window__default["default"].clearTimeout(this.checkCurrentTimeTimeout_);
  22052. }
  22053. this.resetTimeUpdate_();
  22054. };
  22055. }
  22056. /**
  22057. * Periodically check current time to see if playback stopped
  22058. *
  22059. * @private
  22060. */
  22061. monitorCurrentTime_() {
  22062. this.checkCurrentTime_();
  22063. if (this.checkCurrentTimeTimeout_) {
  22064. window__default["default"].clearTimeout(this.checkCurrentTimeTimeout_);
  22065. } // 42 = 24 fps // 250 is what Webkit uses // FF uses 15
  22066. this.checkCurrentTimeTimeout_ = window__default["default"].setTimeout(this.monitorCurrentTime_.bind(this), 250);
  22067. }
  22068. /**
  22069. * Reset stalled download stats for a specific type of loader
  22070. *
  22071. * @param {string} type
  22072. * The segment loader type to check.
  22073. *
  22074. * @listens SegmentLoader#playlistupdate
  22075. * @listens Tech#seeking
  22076. * @listens Tech#seeked
  22077. */
  22078. resetSegmentDownloads_(type) {
  22079. const loader = this.playlistController_[`${type}SegmentLoader_`];
  22080. if (this[`${type}StalledDownloads_`] > 0) {
  22081. this.logger_(`resetting possible stalled download count for ${type} loader`);
  22082. }
  22083. this[`${type}StalledDownloads_`] = 0;
  22084. this[`${type}Buffered_`] = loader.buffered_();
  22085. }
  22086. /**
  22087. * Checks on every segment `appendsdone` to see
  22088. * if segment appends are making progress. If they are not
  22089. * and we are still downloading bytes. We exclude the playlist.
  22090. *
  22091. * @param {string} type
  22092. * The segment loader type to check.
  22093. *
  22094. * @listens SegmentLoader#appendsdone
  22095. */
  22096. checkSegmentDownloads_(type) {
  22097. const pc = this.playlistController_;
  22098. const loader = pc[`${type}SegmentLoader_`];
  22099. const buffered = loader.buffered_();
  22100. const isBufferedDifferent = isRangeDifferent(this[`${type}Buffered_`], buffered);
  22101. this[`${type}Buffered_`] = buffered; // if another watcher is going to fix the issue or
  22102. // the buffered value for this loader changed
  22103. // appends are working
  22104. if (isBufferedDifferent) {
  22105. this.resetSegmentDownloads_(type);
  22106. return;
  22107. }
  22108. this[`${type}StalledDownloads_`]++;
  22109. this.logger_(`found #${this[`${type}StalledDownloads_`]} ${type} appends that did not increase buffer (possible stalled download)`, {
  22110. playlistId: loader.playlist_ && loader.playlist_.id,
  22111. buffered: timeRangesToArray(buffered)
  22112. }); // after 10 possibly stalled appends with no reset, exclude
  22113. if (this[`${type}StalledDownloads_`] < 10) {
  22114. return;
  22115. }
  22116. this.logger_(`${type} loader stalled download exclusion`);
  22117. this.resetSegmentDownloads_(type);
  22118. this.tech_.trigger({
  22119. type: 'usage',
  22120. name: `vhs-${type}-download-exclusion`
  22121. });
  22122. if (type === 'subtitle') {
  22123. return;
  22124. } // TODO: should we exclude audio tracks rather than main tracks
  22125. // when type is audio?
  22126. pc.excludePlaylist({
  22127. error: {
  22128. message: `Excessive ${type} segment downloading detected.`
  22129. },
  22130. playlistExclusionDuration: Infinity
  22131. });
  22132. }
  22133. /**
  22134. * The purpose of this function is to emulate the "waiting" event on
  22135. * browsers that do not emit it when they are waiting for more
  22136. * data to continue playback
  22137. *
  22138. * @private
  22139. */
  22140. checkCurrentTime_() {
  22141. if (this.tech_.paused() || this.tech_.seeking()) {
  22142. return;
  22143. }
  22144. const currentTime = this.tech_.currentTime();
  22145. const buffered = this.tech_.buffered();
  22146. if (this.lastRecordedTime === currentTime && (!buffered.length || currentTime + SAFE_TIME_DELTA >= buffered.end(buffered.length - 1))) {
  22147. // If current time is at the end of the final buffered region, then any playback
  22148. // stall is most likely caused by buffering in a low bandwidth environment. The tech
  22149. // should fire a `waiting` event in this scenario, but due to browser and tech
  22150. // inconsistencies. Calling `techWaiting_` here allows us to simulate
  22151. // responding to a native `waiting` event when the tech fails to emit one.
  22152. return this.techWaiting_();
  22153. }
  22154. if (this.consecutiveUpdates >= 5 && currentTime === this.lastRecordedTime) {
  22155. this.consecutiveUpdates++;
  22156. this.waiting_();
  22157. } else if (currentTime === this.lastRecordedTime) {
  22158. this.consecutiveUpdates++;
  22159. } else {
  22160. this.consecutiveUpdates = 0;
  22161. this.lastRecordedTime = currentTime;
  22162. }
  22163. }
  22164. /**
  22165. * Resets the 'timeupdate' mechanism designed to detect that we are stalled
  22166. *
  22167. * @private
  22168. */
  22169. resetTimeUpdate_() {
  22170. this.consecutiveUpdates = 0;
  22171. }
  22172. /**
  22173. * Fixes situations where there's a bad seek
  22174. *
  22175. * @return {boolean} whether an action was taken to fix the seek
  22176. * @private
  22177. */
  22178. fixesBadSeeks_() {
  22179. const seeking = this.tech_.seeking();
  22180. if (!seeking) {
  22181. return false;
  22182. } // TODO: It's possible that these seekable checks should be moved out of this function
  22183. // and into a function that runs on seekablechange. It's also possible that we only need
  22184. // afterSeekableWindow as the buffered check at the bottom is good enough to handle before
  22185. // seekable range.
  22186. const seekable = this.seekable();
  22187. const currentTime = this.tech_.currentTime();
  22188. const isAfterSeekableRange = this.afterSeekableWindow_(seekable, currentTime, this.media(), this.allowSeeksWithinUnsafeLiveWindow);
  22189. let seekTo;
  22190. if (isAfterSeekableRange) {
  22191. const seekableEnd = seekable.end(seekable.length - 1); // sync to live point (if VOD, our seekable was updated and we're simply adjusting)
  22192. seekTo = seekableEnd;
  22193. }
  22194. if (this.beforeSeekableWindow_(seekable, currentTime)) {
  22195. const seekableStart = seekable.start(0); // sync to the beginning of the live window
  22196. // provide a buffer of .1 seconds to handle rounding/imprecise numbers
  22197. seekTo = seekableStart + ( // if the playlist is too short and the seekable range is an exact time (can
  22198. // happen in live with a 3 segment playlist), then don't use a time delta
  22199. seekableStart === seekable.end(0) ? 0 : SAFE_TIME_DELTA);
  22200. }
  22201. if (typeof seekTo !== 'undefined') {
  22202. this.logger_(`Trying to seek outside of seekable at time ${currentTime} with ` + `seekable range ${printableRange(seekable)}. Seeking to ` + `${seekTo}.`);
  22203. this.tech_.setCurrentTime(seekTo);
  22204. return true;
  22205. }
  22206. const sourceUpdater = this.playlistController_.sourceUpdater_;
  22207. const buffered = this.tech_.buffered();
  22208. const audioBuffered = sourceUpdater.audioBuffer ? sourceUpdater.audioBuffered() : null;
  22209. const videoBuffered = sourceUpdater.videoBuffer ? sourceUpdater.videoBuffered() : null;
  22210. const media = this.media(); // verify that at least two segment durations or one part duration have been
  22211. // appended before checking for a gap.
  22212. const minAppendedDuration = media.partTargetDuration ? media.partTargetDuration : (media.targetDuration - TIME_FUDGE_FACTOR) * 2; // verify that at least two segment durations have been
  22213. // appended before checking for a gap.
  22214. const bufferedToCheck = [audioBuffered, videoBuffered];
  22215. for (let i = 0; i < bufferedToCheck.length; i++) {
  22216. // skip null buffered
  22217. if (!bufferedToCheck[i]) {
  22218. continue;
  22219. }
  22220. const timeAhead = timeAheadOf(bufferedToCheck[i], currentTime); // if we are less than two video/audio segment durations or one part
  22221. // duration behind we haven't appended enough to call this a bad seek.
  22222. if (timeAhead < minAppendedDuration) {
  22223. return false;
  22224. }
  22225. }
  22226. const nextRange = findNextRange(buffered, currentTime); // we have appended enough content, but we don't have anything buffered
  22227. // to seek over the gap
  22228. if (nextRange.length === 0) {
  22229. return false;
  22230. }
  22231. seekTo = nextRange.start(0) + SAFE_TIME_DELTA;
  22232. this.logger_(`Buffered region starts (${nextRange.start(0)}) ` + ` just beyond seek point (${currentTime}). Seeking to ${seekTo}.`);
  22233. this.tech_.setCurrentTime(seekTo);
  22234. return true;
  22235. }
  22236. /**
  22237. * Handler for situations when we determine the player is waiting.
  22238. *
  22239. * @private
  22240. */
  22241. waiting_() {
  22242. if (this.techWaiting_()) {
  22243. return;
  22244. } // All tech waiting checks failed. Use last resort correction
  22245. const currentTime = this.tech_.currentTime();
  22246. const buffered = this.tech_.buffered();
  22247. const currentRange = findRange(buffered, currentTime); // Sometimes the player can stall for unknown reasons within a contiguous buffered
  22248. // region with no indication that anything is amiss (seen in Firefox). Seeking to
  22249. // currentTime is usually enough to kickstart the player. This checks that the player
  22250. // is currently within a buffered region before attempting a corrective seek.
  22251. // Chrome does not appear to continue `timeupdate` events after a `waiting` event
  22252. // until there is ~ 3 seconds of forward buffer available. PlaybackWatcher should also
  22253. // make sure there is ~3 seconds of forward buffer before taking any corrective action
  22254. // to avoid triggering an `unknownwaiting` event when the network is slow.
  22255. if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
  22256. this.resetTimeUpdate_();
  22257. this.tech_.setCurrentTime(currentTime);
  22258. this.logger_(`Stopped at ${currentTime} while inside a buffered region ` + `[${currentRange.start(0)} -> ${currentRange.end(0)}]. Attempting to resume ` + 'playback by seeking to the current time.'); // unknown waiting corrections may be useful for monitoring QoS
  22259. this.tech_.trigger({
  22260. type: 'usage',
  22261. name: 'vhs-unknown-waiting'
  22262. });
  22263. return;
  22264. }
  22265. }
  22266. /**
  22267. * Handler for situations when the tech fires a `waiting` event
  22268. *
  22269. * @return {boolean}
  22270. * True if an action (or none) was needed to correct the waiting. False if no
  22271. * checks passed
  22272. * @private
  22273. */
  22274. techWaiting_() {
  22275. const seekable = this.seekable();
  22276. const currentTime = this.tech_.currentTime();
  22277. if (this.tech_.seeking()) {
  22278. // Tech is seeking or already waiting on another action, no action needed
  22279. return true;
  22280. }
  22281. if (this.beforeSeekableWindow_(seekable, currentTime)) {
  22282. const livePoint = seekable.end(seekable.length - 1);
  22283. this.logger_(`Fell out of live window at time ${currentTime}. Seeking to ` + `live point (seekable end) ${livePoint}`);
  22284. this.resetTimeUpdate_();
  22285. this.tech_.setCurrentTime(livePoint); // live window resyncs may be useful for monitoring QoS
  22286. this.tech_.trigger({
  22287. type: 'usage',
  22288. name: 'vhs-live-resync'
  22289. });
  22290. return true;
  22291. }
  22292. const sourceUpdater = this.tech_.vhs.playlistController_.sourceUpdater_;
  22293. const buffered = this.tech_.buffered();
  22294. const videoUnderflow = this.videoUnderflow_({
  22295. audioBuffered: sourceUpdater.audioBuffered(),
  22296. videoBuffered: sourceUpdater.videoBuffered(),
  22297. currentTime
  22298. });
  22299. if (videoUnderflow) {
  22300. // Even though the video underflowed and was stuck in a gap, the audio overplayed
  22301. // the gap, leading currentTime into a buffered range. Seeking to currentTime
  22302. // allows the video to catch up to the audio position without losing any audio
  22303. // (only suffering ~3 seconds of frozen video and a pause in audio playback).
  22304. this.resetTimeUpdate_();
  22305. this.tech_.setCurrentTime(currentTime); // video underflow may be useful for monitoring QoS
  22306. this.tech_.trigger({
  22307. type: 'usage',
  22308. name: 'vhs-video-underflow'
  22309. });
  22310. return true;
  22311. }
  22312. const nextRange = findNextRange(buffered, currentTime); // check for gap
  22313. if (nextRange.length > 0) {
  22314. this.logger_(`Stopped at ${currentTime} and seeking to ${nextRange.start(0)}`);
  22315. this.resetTimeUpdate_();
  22316. this.skipTheGap_(currentTime);
  22317. return true;
  22318. } // All checks failed. Returning false to indicate failure to correct waiting
  22319. return false;
  22320. }
  22321. afterSeekableWindow_(seekable, currentTime, playlist, allowSeeksWithinUnsafeLiveWindow = false) {
  22322. if (!seekable.length) {
  22323. // we can't make a solid case if there's no seekable, default to false
  22324. return false;
  22325. }
  22326. let allowedEnd = seekable.end(seekable.length - 1) + SAFE_TIME_DELTA;
  22327. const isLive = !playlist.endList;
  22328. const isLLHLS = typeof playlist.partTargetDuration === 'number';
  22329. if (isLive && (isLLHLS || allowSeeksWithinUnsafeLiveWindow)) {
  22330. allowedEnd = seekable.end(seekable.length - 1) + playlist.targetDuration * 3;
  22331. }
  22332. if (currentTime > allowedEnd) {
  22333. return true;
  22334. }
  22335. return false;
  22336. }
  22337. beforeSeekableWindow_(seekable, currentTime) {
  22338. if (seekable.length && // can't fall before 0 and 0 seekable start identifies VOD stream
  22339. seekable.start(0) > 0 && currentTime < seekable.start(0) - this.liveRangeSafeTimeDelta) {
  22340. return true;
  22341. }
  22342. return false;
  22343. }
  22344. videoUnderflow_({
  22345. videoBuffered,
  22346. audioBuffered,
  22347. currentTime
  22348. }) {
  22349. // audio only content will not have video underflow :)
  22350. if (!videoBuffered) {
  22351. return;
  22352. }
  22353. let gap; // find a gap in demuxed content.
  22354. if (videoBuffered.length && audioBuffered.length) {
  22355. // in Chrome audio will continue to play for ~3s when we run out of video
  22356. // so we have to check that the video buffer did have some buffer in the
  22357. // past.
  22358. const lastVideoRange = findRange(videoBuffered, currentTime - 3);
  22359. const videoRange = findRange(videoBuffered, currentTime);
  22360. const audioRange = findRange(audioBuffered, currentTime);
  22361. if (audioRange.length && !videoRange.length && lastVideoRange.length) {
  22362. gap = {
  22363. start: lastVideoRange.end(0),
  22364. end: audioRange.end(0)
  22365. };
  22366. } // find a gap in muxed content.
  22367. } else {
  22368. const nextRange = findNextRange(videoBuffered, currentTime); // Even if there is no available next range, there is still a possibility we are
  22369. // stuck in a gap due to video underflow.
  22370. if (!nextRange.length) {
  22371. gap = this.gapFromVideoUnderflow_(videoBuffered, currentTime);
  22372. }
  22373. }
  22374. if (gap) {
  22375. this.logger_(`Encountered a gap in video from ${gap.start} to ${gap.end}. ` + `Seeking to current time ${currentTime}`);
  22376. return true;
  22377. }
  22378. return false;
  22379. }
  22380. /**
  22381. * Timer callback. If playback still has not proceeded, then we seek
  22382. * to the start of the next buffered region.
  22383. *
  22384. * @private
  22385. */
  22386. skipTheGap_(scheduledCurrentTime) {
  22387. const buffered = this.tech_.buffered();
  22388. const currentTime = this.tech_.currentTime();
  22389. const nextRange = findNextRange(buffered, currentTime);
  22390. this.resetTimeUpdate_();
  22391. if (nextRange.length === 0 || currentTime !== scheduledCurrentTime) {
  22392. return;
  22393. }
  22394. this.logger_('skipTheGap_:', 'currentTime:', currentTime, 'scheduled currentTime:', scheduledCurrentTime, 'nextRange start:', nextRange.start(0)); // only seek if we still have not played
  22395. this.tech_.setCurrentTime(nextRange.start(0) + TIME_FUDGE_FACTOR);
  22396. this.tech_.trigger({
  22397. type: 'usage',
  22398. name: 'vhs-gap-skip'
  22399. });
  22400. }
  22401. gapFromVideoUnderflow_(buffered, currentTime) {
  22402. // At least in Chrome, if there is a gap in the video buffer, the audio will continue
  22403. // playing for ~3 seconds after the video gap starts. This is done to account for
  22404. // video buffer underflow/underrun (note that this is not done when there is audio
  22405. // buffer underflow/underrun -- in that case the video will stop as soon as it
  22406. // encounters the gap, as audio stalls are more noticeable/jarring to a user than
  22407. // video stalls). The player's time will reflect the playthrough of audio, so the
  22408. // time will appear as if we are in a buffered region, even if we are stuck in a
  22409. // "gap."
  22410. //
  22411. // Example:
  22412. // video buffer: 0 => 10.1, 10.2 => 20
  22413. // audio buffer: 0 => 20
  22414. // overall buffer: 0 => 10.1, 10.2 => 20
  22415. // current time: 13
  22416. //
  22417. // Chrome's video froze at 10 seconds, where the video buffer encountered the gap,
  22418. // however, the audio continued playing until it reached ~3 seconds past the gap
  22419. // (13 seconds), at which point it stops as well. Since current time is past the
  22420. // gap, findNextRange will return no ranges.
  22421. //
  22422. // To check for this issue, we see if there is a gap that starts somewhere within
  22423. // a 3 second range (3 seconds +/- 1 second) back from our current time.
  22424. const gaps = findGaps(buffered);
  22425. for (let i = 0; i < gaps.length; i++) {
  22426. const start = gaps.start(i);
  22427. const end = gaps.end(i); // gap is starts no more than 4 seconds back
  22428. if (currentTime - start < 4 && currentTime - start > 2) {
  22429. return {
  22430. start,
  22431. end
  22432. };
  22433. }
  22434. }
  22435. return null;
  22436. }
  22437. }
  22438. const defaultOptions = {
  22439. errorInterval: 30,
  22440. getSource(next) {
  22441. const tech = this.tech({
  22442. IWillNotUseThisInPlugins: true
  22443. });
  22444. const sourceObj = tech.currentSource_ || this.currentSource();
  22445. return next(sourceObj);
  22446. }
  22447. };
  22448. /**
  22449. * Main entry point for the plugin
  22450. *
  22451. * @param {Player} player a reference to a videojs Player instance
  22452. * @param {Object} [options] an object with plugin options
  22453. * @private
  22454. */
  22455. const initPlugin = function (player, options) {
  22456. let lastCalled = 0;
  22457. let seekTo = 0;
  22458. const localOptions = merge(defaultOptions, options);
  22459. player.ready(() => {
  22460. player.trigger({
  22461. type: 'usage',
  22462. name: 'vhs-error-reload-initialized'
  22463. });
  22464. });
  22465. /**
  22466. * Player modifications to perform that must wait until `loadedmetadata`
  22467. * has been triggered
  22468. *
  22469. * @private
  22470. */
  22471. const loadedMetadataHandler = function () {
  22472. if (seekTo) {
  22473. player.currentTime(seekTo);
  22474. }
  22475. };
  22476. /**
  22477. * Set the source on the player element, play, and seek if necessary
  22478. *
  22479. * @param {Object} sourceObj An object specifying the source url and mime-type to play
  22480. * @private
  22481. */
  22482. const setSource = function (sourceObj) {
  22483. if (sourceObj === null || sourceObj === undefined) {
  22484. return;
  22485. }
  22486. seekTo = player.duration() !== Infinity && player.currentTime() || 0;
  22487. player.one('loadedmetadata', loadedMetadataHandler);
  22488. player.src(sourceObj);
  22489. player.trigger({
  22490. type: 'usage',
  22491. name: 'vhs-error-reload'
  22492. });
  22493. player.play();
  22494. };
  22495. /**
  22496. * Attempt to get a source from either the built-in getSource function
  22497. * or a custom function provided via the options
  22498. *
  22499. * @private
  22500. */
  22501. const errorHandler = function () {
  22502. // Do not attempt to reload the source if a source-reload occurred before
  22503. // 'errorInterval' time has elapsed since the last source-reload
  22504. if (Date.now() - lastCalled < localOptions.errorInterval * 1000) {
  22505. player.trigger({
  22506. type: 'usage',
  22507. name: 'vhs-error-reload-canceled'
  22508. });
  22509. return;
  22510. }
  22511. if (!localOptions.getSource || typeof localOptions.getSource !== 'function') {
  22512. videojs__default["default"].log.error('ERROR: reloadSourceOnError - The option getSource must be a function!');
  22513. return;
  22514. }
  22515. lastCalled = Date.now();
  22516. return localOptions.getSource.call(player, setSource);
  22517. };
  22518. /**
  22519. * Unbind any event handlers that were bound by the plugin
  22520. *
  22521. * @private
  22522. */
  22523. const cleanupEvents = function () {
  22524. player.off('loadedmetadata', loadedMetadataHandler);
  22525. player.off('error', errorHandler);
  22526. player.off('dispose', cleanupEvents);
  22527. };
  22528. /**
  22529. * Cleanup before re-initializing the plugin
  22530. *
  22531. * @param {Object} [newOptions] an object with plugin options
  22532. * @private
  22533. */
  22534. const reinitPlugin = function (newOptions) {
  22535. cleanupEvents();
  22536. initPlugin(player, newOptions);
  22537. };
  22538. player.on('error', errorHandler);
  22539. player.on('dispose', cleanupEvents); // Overwrite the plugin function so that we can correctly cleanup before
  22540. // initializing the plugin
  22541. player.reloadSourceOnError = reinitPlugin;
  22542. };
  22543. /**
  22544. * Reload the source when an error is detected as long as there
  22545. * wasn't an error previously within the last 30 seconds
  22546. *
  22547. * @param {Object} [options] an object with plugin options
  22548. */
  22549. const reloadSourceOnError = function (options) {
  22550. initPlugin(this, options);
  22551. };
  22552. var version$4 = "3.10.0";
  22553. var version$3 = "7.0.2";
  22554. var version$2 = "1.3.0";
  22555. var version$1 = "7.1.0";
  22556. var version = "4.0.1";
  22557. /**
  22558. * @file videojs-http-streaming.js
  22559. *
  22560. * The main file for the VHS project.
  22561. * License: https://github.com/videojs/videojs-http-streaming/blob/main/LICENSE
  22562. */
  22563. const Vhs = {
  22564. PlaylistLoader,
  22565. Playlist,
  22566. utils,
  22567. STANDARD_PLAYLIST_SELECTOR: lastBandwidthSelector,
  22568. INITIAL_PLAYLIST_SELECTOR: lowestBitrateCompatibleVariantSelector,
  22569. lastBandwidthSelector,
  22570. movingAverageBandwidthSelector,
  22571. comparePlaylistBandwidth,
  22572. comparePlaylistResolution,
  22573. xhr: xhrFactory()
  22574. }; // Define getter/setters for config properties
  22575. Object.keys(Config).forEach(prop => {
  22576. Object.defineProperty(Vhs, prop, {
  22577. get() {
  22578. videojs__default["default"].log.warn(`using Vhs.${prop} is UNSAFE be sure you know what you are doing`);
  22579. return Config[prop];
  22580. },
  22581. set(value) {
  22582. videojs__default["default"].log.warn(`using Vhs.${prop} is UNSAFE be sure you know what you are doing`);
  22583. if (typeof value !== 'number' || value < 0) {
  22584. videojs__default["default"].log.warn(`value of Vhs.${prop} must be greater than or equal to 0`);
  22585. return;
  22586. }
  22587. Config[prop] = value;
  22588. }
  22589. });
  22590. });
  22591. const LOCAL_STORAGE_KEY = 'videojs-vhs';
  22592. /**
  22593. * Updates the selectedIndex of the QualityLevelList when a mediachange happens in vhs.
  22594. *
  22595. * @param {QualityLevelList} qualityLevels The QualityLevelList to update.
  22596. * @param {PlaylistLoader} playlistLoader PlaylistLoader containing the new media info.
  22597. * @function handleVhsMediaChange
  22598. */
  22599. const handleVhsMediaChange = function (qualityLevels, playlistLoader) {
  22600. const newPlaylist = playlistLoader.media();
  22601. let selectedIndex = -1;
  22602. for (let i = 0; i < qualityLevels.length; i++) {
  22603. if (qualityLevels[i].id === newPlaylist.id) {
  22604. selectedIndex = i;
  22605. break;
  22606. }
  22607. }
  22608. qualityLevels.selectedIndex_ = selectedIndex;
  22609. qualityLevels.trigger({
  22610. selectedIndex,
  22611. type: 'change'
  22612. });
  22613. };
  22614. /**
  22615. * Adds quality levels to list once playlist metadata is available
  22616. *
  22617. * @param {QualityLevelList} qualityLevels The QualityLevelList to attach events to.
  22618. * @param {Object} vhs Vhs object to listen to for media events.
  22619. * @function handleVhsLoadedMetadata
  22620. */
  22621. const handleVhsLoadedMetadata = function (qualityLevels, vhs) {
  22622. vhs.representations().forEach(rep => {
  22623. qualityLevels.addQualityLevel(rep);
  22624. });
  22625. handleVhsMediaChange(qualityLevels, vhs.playlists);
  22626. }; // VHS is a source handler, not a tech. Make sure attempts to use it
  22627. // as one do not cause exceptions.
  22628. Vhs.canPlaySource = function () {
  22629. return videojs__default["default"].log.warn('VHS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
  22630. };
  22631. const emeKeySystems = (keySystemOptions, mainPlaylist, audioPlaylist) => {
  22632. if (!keySystemOptions) {
  22633. return keySystemOptions;
  22634. }
  22635. let codecs = {};
  22636. if (mainPlaylist && mainPlaylist.attributes && mainPlaylist.attributes.CODECS) {
  22637. codecs = unwrapCodecList(codecs_js.parseCodecs(mainPlaylist.attributes.CODECS));
  22638. }
  22639. if (audioPlaylist && audioPlaylist.attributes && audioPlaylist.attributes.CODECS) {
  22640. codecs.audio = audioPlaylist.attributes.CODECS;
  22641. }
  22642. const videoContentType = codecs_js.getMimeForCodec(codecs.video);
  22643. const audioContentType = codecs_js.getMimeForCodec(codecs.audio); // upsert the content types based on the selected playlist
  22644. const keySystemContentTypes = {};
  22645. for (const keySystem in keySystemOptions) {
  22646. keySystemContentTypes[keySystem] = {};
  22647. if (audioContentType) {
  22648. keySystemContentTypes[keySystem].audioContentType = audioContentType;
  22649. }
  22650. if (videoContentType) {
  22651. keySystemContentTypes[keySystem].videoContentType = videoContentType;
  22652. } // Default to using the video playlist's PSSH even though they may be different, as
  22653. // videojs-contrib-eme will only accept one in the options.
  22654. //
  22655. // This shouldn't be an issue for most cases as early intialization will handle all
  22656. // unique PSSH values, and if they aren't, then encrypted events should have the
  22657. // specific information needed for the unique license.
  22658. if (mainPlaylist.contentProtection && mainPlaylist.contentProtection[keySystem] && mainPlaylist.contentProtection[keySystem].pssh) {
  22659. keySystemContentTypes[keySystem].pssh = mainPlaylist.contentProtection[keySystem].pssh;
  22660. } // videojs-contrib-eme accepts the option of specifying: 'com.some.cdm': 'url'
  22661. // so we need to prevent overwriting the URL entirely
  22662. if (typeof keySystemOptions[keySystem] === 'string') {
  22663. keySystemContentTypes[keySystem].url = keySystemOptions[keySystem];
  22664. }
  22665. }
  22666. return merge(keySystemOptions, keySystemContentTypes);
  22667. };
  22668. /**
  22669. * @typedef {Object} KeySystems
  22670. *
  22671. * keySystems configuration for https://github.com/videojs/videojs-contrib-eme
  22672. * Note: not all options are listed here.
  22673. *
  22674. * @property {Uint8Array} [pssh]
  22675. * Protection System Specific Header
  22676. */
  22677. /**
  22678. * Goes through all the playlists and collects an array of KeySystems options objects
  22679. * containing each playlist's keySystems and their pssh values, if available.
  22680. *
  22681. * @param {Object[]} playlists
  22682. * The playlists to look through
  22683. * @param {string[]} keySystems
  22684. * The keySystems to collect pssh values for
  22685. *
  22686. * @return {KeySystems[]}
  22687. * An array of KeySystems objects containing available key systems and their
  22688. * pssh values
  22689. */
  22690. const getAllPsshKeySystemsOptions = (playlists, keySystems) => {
  22691. return playlists.reduce((keySystemsArr, playlist) => {
  22692. if (!playlist.contentProtection) {
  22693. return keySystemsArr;
  22694. }
  22695. const keySystemsOptions = keySystems.reduce((keySystemsObj, keySystem) => {
  22696. const keySystemOptions = playlist.contentProtection[keySystem];
  22697. if (keySystemOptions && keySystemOptions.pssh) {
  22698. keySystemsObj[keySystem] = {
  22699. pssh: keySystemOptions.pssh
  22700. };
  22701. }
  22702. return keySystemsObj;
  22703. }, {});
  22704. if (Object.keys(keySystemsOptions).length) {
  22705. keySystemsArr.push(keySystemsOptions);
  22706. }
  22707. return keySystemsArr;
  22708. }, []);
  22709. };
  22710. /**
  22711. * Returns a promise that waits for the
  22712. * [eme plugin](https://github.com/videojs/videojs-contrib-eme) to create a key session.
  22713. *
  22714. * Works around https://bugs.chromium.org/p/chromium/issues/detail?id=895449 in non-IE11
  22715. * browsers.
  22716. *
  22717. * As per the above ticket, this is particularly important for Chrome, where, if
  22718. * unencrypted content is appended before encrypted content and the key session has not
  22719. * been created, a MEDIA_ERR_DECODE will be thrown once the encrypted content is reached
  22720. * during playback.
  22721. *
  22722. * @param {Object} player
  22723. * The player instance
  22724. * @param {Object[]} sourceKeySystems
  22725. * The key systems options from the player source
  22726. * @param {Object} [audioMedia]
  22727. * The active audio media playlist (optional)
  22728. * @param {Object[]} mainPlaylists
  22729. * The playlists found on the main playlist object
  22730. *
  22731. * @return {Object}
  22732. * Promise that resolves when the key session has been created
  22733. */
  22734. const waitForKeySessionCreation = ({
  22735. player,
  22736. sourceKeySystems,
  22737. audioMedia,
  22738. mainPlaylists
  22739. }) => {
  22740. if (!player.eme.initializeMediaKeys) {
  22741. return Promise.resolve();
  22742. } // TODO should all audio PSSH values be initialized for DRM?
  22743. //
  22744. // All unique video rendition pssh values are initialized for DRM, but here only
  22745. // the initial audio playlist license is initialized. In theory, an encrypted
  22746. // event should be fired if the user switches to an alternative audio playlist
  22747. // where a license is required, but this case hasn't yet been tested. In addition, there
  22748. // may be many alternate audio playlists unlikely to be used (e.g., multiple different
  22749. // languages).
  22750. const playlists = audioMedia ? mainPlaylists.concat([audioMedia]) : mainPlaylists;
  22751. const keySystemsOptionsArr = getAllPsshKeySystemsOptions(playlists, Object.keys(sourceKeySystems));
  22752. const initializationFinishedPromises = [];
  22753. const keySessionCreatedPromises = []; // Since PSSH values are interpreted as initData, EME will dedupe any duplicates. The
  22754. // only place where it should not be deduped is for ms-prefixed APIs, but
  22755. // the existence of modern EME APIs in addition to
  22756. // ms-prefixed APIs on Edge should prevent this from being a concern.
  22757. // initializeMediaKeys also won't use the webkit-prefixed APIs.
  22758. keySystemsOptionsArr.forEach(keySystemsOptions => {
  22759. keySessionCreatedPromises.push(new Promise((resolve, reject) => {
  22760. player.tech_.one('keysessioncreated', resolve);
  22761. }));
  22762. initializationFinishedPromises.push(new Promise((resolve, reject) => {
  22763. player.eme.initializeMediaKeys({
  22764. keySystems: keySystemsOptions
  22765. }, err => {
  22766. if (err) {
  22767. reject(err);
  22768. return;
  22769. }
  22770. resolve();
  22771. });
  22772. }));
  22773. }); // The reasons Promise.race is chosen over Promise.any:
  22774. //
  22775. // * Promise.any is only available in Safari 14+.
  22776. // * None of these promises are expected to reject. If they do reject, it might be
  22777. // better here for the race to surface the rejection, rather than mask it by using
  22778. // Promise.any.
  22779. return Promise.race([// If a session was previously created, these will all finish resolving without
  22780. // creating a new session, otherwise it will take until the end of all license
  22781. // requests, which is why the key session check is used (to make setup much faster).
  22782. Promise.all(initializationFinishedPromises), // Once a single session is created, the browser knows DRM will be used.
  22783. Promise.race(keySessionCreatedPromises)]);
  22784. };
  22785. /**
  22786. * If the [eme](https://github.com/videojs/videojs-contrib-eme) plugin is available, and
  22787. * there are keySystems on the source, sets up source options to prepare the source for
  22788. * eme.
  22789. *
  22790. * @param {Object} player
  22791. * The player instance
  22792. * @param {Object[]} sourceKeySystems
  22793. * The key systems options from the player source
  22794. * @param {Object} media
  22795. * The active media playlist
  22796. * @param {Object} [audioMedia]
  22797. * The active audio media playlist (optional)
  22798. *
  22799. * @return {boolean}
  22800. * Whether or not options were configured and EME is available
  22801. */
  22802. const setupEmeOptions = ({
  22803. player,
  22804. sourceKeySystems,
  22805. media,
  22806. audioMedia
  22807. }) => {
  22808. const sourceOptions = emeKeySystems(sourceKeySystems, media, audioMedia);
  22809. if (!sourceOptions) {
  22810. return false;
  22811. }
  22812. player.currentSource().keySystems = sourceOptions; // eme handles the rest of the setup, so if it is missing
  22813. // do nothing.
  22814. if (sourceOptions && !player.eme) {
  22815. videojs__default["default"].log.warn('DRM encrypted source cannot be decrypted without a DRM plugin');
  22816. return false;
  22817. }
  22818. return true;
  22819. };
  22820. const getVhsLocalStorage = () => {
  22821. if (!window__default["default"].localStorage) {
  22822. return null;
  22823. }
  22824. const storedObject = window__default["default"].localStorage.getItem(LOCAL_STORAGE_KEY);
  22825. if (!storedObject) {
  22826. return null;
  22827. }
  22828. try {
  22829. return JSON.parse(storedObject);
  22830. } catch (e) {
  22831. // someone may have tampered with the value
  22832. return null;
  22833. }
  22834. };
  22835. const updateVhsLocalStorage = options => {
  22836. if (!window__default["default"].localStorage) {
  22837. return false;
  22838. }
  22839. let objectToStore = getVhsLocalStorage();
  22840. objectToStore = objectToStore ? merge(objectToStore, options) : options;
  22841. try {
  22842. window__default["default"].localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(objectToStore));
  22843. } catch (e) {
  22844. // Throws if storage is full (e.g., always on iOS 5+ Safari private mode, where
  22845. // storage is set to 0).
  22846. // https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem#Exceptions
  22847. // No need to perform any operation.
  22848. return false;
  22849. }
  22850. return objectToStore;
  22851. };
  22852. /**
  22853. * Parses VHS-supported media types from data URIs. See
  22854. * https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
  22855. * for information on data URIs.
  22856. *
  22857. * @param {string} dataUri
  22858. * The data URI
  22859. *
  22860. * @return {string|Object}
  22861. * The parsed object/string, or the original string if no supported media type
  22862. * was found
  22863. */
  22864. const expandDataUri = dataUri => {
  22865. if (dataUri.toLowerCase().indexOf('data:application/vnd.videojs.vhs+json,') === 0) {
  22866. return JSON.parse(dataUri.substring(dataUri.indexOf(',') + 1));
  22867. } // no known case for this data URI, return the string as-is
  22868. return dataUri;
  22869. };
  22870. /**
  22871. * Adds a request hook to an xhr object
  22872. *
  22873. * @param {Object} xhr object to add the onRequest hook to
  22874. * @param {function} callback hook function for an xhr request
  22875. */
  22876. const addOnRequestHook = (xhr, callback) => {
  22877. if (!xhr._requestCallbackSet) {
  22878. xhr._requestCallbackSet = new Set();
  22879. }
  22880. xhr._requestCallbackSet.add(callback);
  22881. };
  22882. /**
  22883. * Adds a response hook to an xhr object
  22884. *
  22885. * @param {Object} xhr object to add the onResponse hook to
  22886. * @param {function} callback hook function for an xhr response
  22887. */
  22888. const addOnResponseHook = (xhr, callback) => {
  22889. if (!xhr._responseCallbackSet) {
  22890. xhr._responseCallbackSet = new Set();
  22891. }
  22892. xhr._responseCallbackSet.add(callback);
  22893. };
  22894. /**
  22895. * Removes a request hook on an xhr object, deletes the onRequest set if empty.
  22896. *
  22897. * @param {Object} xhr object to remove the onRequest hook from
  22898. * @param {function} callback hook function to remove
  22899. */
  22900. const removeOnRequestHook = (xhr, callback) => {
  22901. if (!xhr._requestCallbackSet) {
  22902. return;
  22903. }
  22904. xhr._requestCallbackSet.delete(callback);
  22905. if (!xhr._requestCallbackSet.size) {
  22906. delete xhr._requestCallbackSet;
  22907. }
  22908. };
  22909. /**
  22910. * Removes a response hook on an xhr object, deletes the onResponse set if empty.
  22911. *
  22912. * @param {Object} xhr object to remove the onResponse hook from
  22913. * @param {function} callback hook function to remove
  22914. */
  22915. const removeOnResponseHook = (xhr, callback) => {
  22916. if (!xhr._responseCallbackSet) {
  22917. return;
  22918. }
  22919. xhr._responseCallbackSet.delete(callback);
  22920. if (!xhr._responseCallbackSet.size) {
  22921. delete xhr._responseCallbackSet;
  22922. }
  22923. };
  22924. /**
  22925. * Whether the browser has built-in HLS support.
  22926. */
  22927. Vhs.supportsNativeHls = function () {
  22928. if (!document__default["default"] || !document__default["default"].createElement) {
  22929. return false;
  22930. }
  22931. const video = document__default["default"].createElement('video'); // native HLS is definitely not supported if HTML5 video isn't
  22932. if (!videojs__default["default"].getTech('Html5').isSupported()) {
  22933. return false;
  22934. } // HLS manifests can go by many mime-types
  22935. const canPlay = [// Apple santioned
  22936. 'application/vnd.apple.mpegurl', // Apple sanctioned for backwards compatibility
  22937. 'audio/mpegurl', // Very common
  22938. 'audio/x-mpegurl', // Very common
  22939. 'application/x-mpegurl', // Included for completeness
  22940. 'video/x-mpegurl', 'video/mpegurl', 'application/mpegurl'];
  22941. return canPlay.some(function (canItPlay) {
  22942. return /maybe|probably/i.test(video.canPlayType(canItPlay));
  22943. });
  22944. }();
  22945. Vhs.supportsNativeDash = function () {
  22946. if (!document__default["default"] || !document__default["default"].createElement || !videojs__default["default"].getTech('Html5').isSupported()) {
  22947. return false;
  22948. }
  22949. return /maybe|probably/i.test(document__default["default"].createElement('video').canPlayType('application/dash+xml'));
  22950. }();
  22951. Vhs.supportsTypeNatively = type => {
  22952. if (type === 'hls') {
  22953. return Vhs.supportsNativeHls;
  22954. }
  22955. if (type === 'dash') {
  22956. return Vhs.supportsNativeDash;
  22957. }
  22958. return false;
  22959. };
  22960. /**
  22961. * VHS is a source handler, not a tech. Make sure attempts to use it
  22962. * as one do not cause exceptions.
  22963. */
  22964. Vhs.isSupported = function () {
  22965. return videojs__default["default"].log.warn('VHS is no longer a tech. Please remove it from ' + 'your player\'s techOrder.');
  22966. };
  22967. /**
  22968. * A global function for setting an onRequest hook
  22969. *
  22970. * @param {function} callback for request modifiction
  22971. */
  22972. Vhs.xhr.onRequest = function (callback) {
  22973. addOnRequestHook(Vhs.xhr, callback);
  22974. };
  22975. /**
  22976. * A global function for setting an onResponse hook
  22977. *
  22978. * @param {callback} callback for response data retrieval
  22979. */
  22980. Vhs.xhr.onResponse = function (callback) {
  22981. addOnResponseHook(Vhs.xhr, callback);
  22982. };
  22983. /**
  22984. * Deletes a global onRequest callback if it exists
  22985. *
  22986. * @param {function} callback to delete from the global set
  22987. */
  22988. Vhs.xhr.offRequest = function (callback) {
  22989. removeOnRequestHook(Vhs.xhr, callback);
  22990. };
  22991. /**
  22992. * Deletes a global onResponse callback if it exists
  22993. *
  22994. * @param {function} callback to delete from the global set
  22995. */
  22996. Vhs.xhr.offResponse = function (callback) {
  22997. removeOnResponseHook(Vhs.xhr, callback);
  22998. };
  22999. const Component = videojs__default["default"].getComponent('Component');
  23000. /**
  23001. * The Vhs Handler object, where we orchestrate all of the parts
  23002. * of VHS to interact with video.js
  23003. *
  23004. * @class VhsHandler
  23005. * @extends videojs.Component
  23006. * @param {Object} source the soruce object
  23007. * @param {Tech} tech the parent tech object
  23008. * @param {Object} options optional and required options
  23009. */
  23010. class VhsHandler extends Component {
  23011. constructor(source, tech, options) {
  23012. super(tech, options.vhs); // if a tech level `initialBandwidth` option was passed
  23013. // use that over the VHS level `bandwidth` option
  23014. if (typeof options.initialBandwidth === 'number') {
  23015. this.options_.bandwidth = options.initialBandwidth;
  23016. }
  23017. this.logger_ = logger('VhsHandler'); // we need access to the player in some cases,
  23018. // so, get it from Video.js via the `playerId`
  23019. if (tech.options_ && tech.options_.playerId) {
  23020. const _player = videojs__default["default"].getPlayer(tech.options_.playerId);
  23021. this.player_ = _player;
  23022. }
  23023. this.tech_ = tech;
  23024. this.source_ = source;
  23025. this.stats = {};
  23026. this.ignoreNextSeekingEvent_ = false;
  23027. this.setOptions_();
  23028. if (this.options_.overrideNative && tech.overrideNativeAudioTracks && tech.overrideNativeVideoTracks) {
  23029. tech.overrideNativeAudioTracks(true);
  23030. tech.overrideNativeVideoTracks(true);
  23031. } else if (this.options_.overrideNative && (tech.featuresNativeVideoTracks || tech.featuresNativeAudioTracks)) {
  23032. // overriding native VHS only works if audio tracks have been emulated
  23033. // error early if we're misconfigured
  23034. throw new Error('Overriding native VHS requires emulated tracks. ' + 'See https://git.io/vMpjB');
  23035. } // listen for fullscreenchange events for this player so that we
  23036. // can adjust our quality selection quickly
  23037. this.on(document__default["default"], ['fullscreenchange', 'webkitfullscreenchange', 'mozfullscreenchange', 'MSFullscreenChange'], event => {
  23038. const fullscreenElement = document__default["default"].fullscreenElement || document__default["default"].webkitFullscreenElement || document__default["default"].mozFullScreenElement || document__default["default"].msFullscreenElement;
  23039. if (fullscreenElement && fullscreenElement.contains(this.tech_.el())) {
  23040. this.playlistController_.fastQualityChange_();
  23041. } else {
  23042. // When leaving fullscreen, since the in page pixel dimensions should be smaller
  23043. // than full screen, see if there should be a rendition switch down to preserve
  23044. // bandwidth.
  23045. this.playlistController_.checkABR_();
  23046. }
  23047. });
  23048. this.on(this.tech_, 'seeking', function () {
  23049. if (this.ignoreNextSeekingEvent_) {
  23050. this.ignoreNextSeekingEvent_ = false;
  23051. return;
  23052. }
  23053. this.setCurrentTime(this.tech_.currentTime());
  23054. });
  23055. this.on(this.tech_, 'error', function () {
  23056. // verify that the error was real and we are loaded
  23057. // enough to have pc loaded.
  23058. if (this.tech_.error() && this.playlistController_) {
  23059. this.playlistController_.pauseLoading();
  23060. }
  23061. });
  23062. this.on(this.tech_, 'play', this.play);
  23063. }
  23064. /**
  23065. * Set VHS options based on options from configuration, as well as partial
  23066. * options to be passed at a later time.
  23067. *
  23068. * @param {Object} options A partial chunk of config options
  23069. */
  23070. setOptions_(options = {}) {
  23071. this.options_ = merge(this.options_, options); // defaults
  23072. this.options_.withCredentials = this.options_.withCredentials || false;
  23073. this.options_.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions === false ? false : true;
  23074. this.options_.useDevicePixelRatio = this.options_.useDevicePixelRatio || false;
  23075. this.options_.useBandwidthFromLocalStorage = typeof this.source_.useBandwidthFromLocalStorage !== 'undefined' ? this.source_.useBandwidthFromLocalStorage : this.options_.useBandwidthFromLocalStorage || false;
  23076. this.options_.useForcedSubtitles = this.options_.useForcedSubtitles || false;
  23077. this.options_.useNetworkInformationApi = this.options_.useNetworkInformationApi || false;
  23078. this.options_.useDtsForTimestampOffset = this.options_.useDtsForTimestampOffset || false;
  23079. this.options_.customTagParsers = this.options_.customTagParsers || [];
  23080. this.options_.customTagMappers = this.options_.customTagMappers || [];
  23081. this.options_.cacheEncryptionKeys = this.options_.cacheEncryptionKeys || false;
  23082. this.options_.llhls = this.options_.llhls === false ? false : true;
  23083. this.options_.bufferBasedABR = this.options_.bufferBasedABR || false;
  23084. if (typeof this.options_.playlistExclusionDuration !== 'number') {
  23085. this.options_.playlistExclusionDuration = 60;
  23086. }
  23087. if (typeof this.options_.bandwidth !== 'number') {
  23088. if (this.options_.useBandwidthFromLocalStorage) {
  23089. const storedObject = getVhsLocalStorage();
  23090. if (storedObject && storedObject.bandwidth) {
  23091. this.options_.bandwidth = storedObject.bandwidth;
  23092. this.tech_.trigger({
  23093. type: 'usage',
  23094. name: 'vhs-bandwidth-from-local-storage'
  23095. });
  23096. }
  23097. if (storedObject && storedObject.throughput) {
  23098. this.options_.throughput = storedObject.throughput;
  23099. this.tech_.trigger({
  23100. type: 'usage',
  23101. name: 'vhs-throughput-from-local-storage'
  23102. });
  23103. }
  23104. }
  23105. } // if bandwidth was not set by options or pulled from local storage, start playlist
  23106. // selection at a reasonable bandwidth
  23107. if (typeof this.options_.bandwidth !== 'number') {
  23108. this.options_.bandwidth = Config.INITIAL_BANDWIDTH;
  23109. } // If the bandwidth number is unchanged from the initial setting
  23110. // then this takes precedence over the enableLowInitialPlaylist option
  23111. this.options_.enableLowInitialPlaylist = this.options_.enableLowInitialPlaylist && this.options_.bandwidth === Config.INITIAL_BANDWIDTH; // grab options passed to player.src
  23112. ['withCredentials', 'useDevicePixelRatio', 'limitRenditionByPlayerDimensions', 'bandwidth', 'customTagParsers', 'customTagMappers', 'cacheEncryptionKeys', 'playlistSelector', 'initialPlaylistSelector', 'bufferBasedABR', 'liveRangeSafeTimeDelta', 'llhls', 'useForcedSubtitles', 'useNetworkInformationApi', 'useDtsForTimestampOffset', 'exactManifestTimings', 'leastPixelDiffSelector'].forEach(option => {
  23113. if (typeof this.source_[option] !== 'undefined') {
  23114. this.options_[option] = this.source_[option];
  23115. }
  23116. });
  23117. this.limitRenditionByPlayerDimensions = this.options_.limitRenditionByPlayerDimensions;
  23118. this.useDevicePixelRatio = this.options_.useDevicePixelRatio;
  23119. } // alias for public method to set options
  23120. setOptions(options = {}) {
  23121. this.setOptions_(options);
  23122. }
  23123. /**
  23124. * called when player.src gets called, handle a new source
  23125. *
  23126. * @param {Object} src the source object to handle
  23127. */
  23128. src(src, type) {
  23129. // do nothing if the src is falsey
  23130. if (!src) {
  23131. return;
  23132. }
  23133. this.setOptions_(); // add main playlist controller options
  23134. this.options_.src = expandDataUri(this.source_.src);
  23135. this.options_.tech = this.tech_;
  23136. this.options_.externVhs = Vhs;
  23137. this.options_.sourceType = mediaTypes_js.simpleTypeFromSourceType(type); // Whenever we seek internally, we should update the tech
  23138. this.options_.seekTo = time => {
  23139. this.tech_.setCurrentTime(time);
  23140. };
  23141. this.playlistController_ = new PlaylistController(this.options_);
  23142. const playbackWatcherOptions = merge({
  23143. liveRangeSafeTimeDelta: SAFE_TIME_DELTA
  23144. }, this.options_, {
  23145. seekable: () => this.seekable(),
  23146. media: () => this.playlistController_.media(),
  23147. playlistController: this.playlistController_
  23148. });
  23149. this.playbackWatcher_ = new PlaybackWatcher(playbackWatcherOptions);
  23150. this.playlistController_.on('error', () => {
  23151. const player = videojs__default["default"].players[this.tech_.options_.playerId];
  23152. let error = this.playlistController_.error;
  23153. if (typeof error === 'object' && !error.code) {
  23154. error.code = 3;
  23155. } else if (typeof error === 'string') {
  23156. error = {
  23157. message: error,
  23158. code: 3
  23159. };
  23160. }
  23161. player.error(error);
  23162. });
  23163. const defaultSelector = this.options_.bufferBasedABR ? Vhs.movingAverageBandwidthSelector(0.55) : Vhs.STANDARD_PLAYLIST_SELECTOR; // `this` in selectPlaylist should be the VhsHandler for backwards
  23164. // compatibility with < v2
  23165. this.playlistController_.selectPlaylist = this.selectPlaylist ? this.selectPlaylist.bind(this) : defaultSelector.bind(this);
  23166. this.playlistController_.selectInitialPlaylist = Vhs.INITIAL_PLAYLIST_SELECTOR.bind(this); // re-expose some internal objects for backwards compatibility with < v2
  23167. this.playlists = this.playlistController_.mainPlaylistLoader_;
  23168. this.mediaSource = this.playlistController_.mediaSource; // Proxy assignment of some properties to the main playlist
  23169. // controller. Using a custom property for backwards compatibility
  23170. // with < v2
  23171. Object.defineProperties(this, {
  23172. selectPlaylist: {
  23173. get() {
  23174. return this.playlistController_.selectPlaylist;
  23175. },
  23176. set(selectPlaylist) {
  23177. this.playlistController_.selectPlaylist = selectPlaylist.bind(this);
  23178. }
  23179. },
  23180. throughput: {
  23181. get() {
  23182. return this.playlistController_.mainSegmentLoader_.throughput.rate;
  23183. },
  23184. set(throughput) {
  23185. this.playlistController_.mainSegmentLoader_.throughput.rate = throughput; // By setting `count` to 1 the throughput value becomes the starting value
  23186. // for the cumulative average
  23187. this.playlistController_.mainSegmentLoader_.throughput.count = 1;
  23188. }
  23189. },
  23190. bandwidth: {
  23191. get() {
  23192. let playerBandwidthEst = this.playlistController_.mainSegmentLoader_.bandwidth;
  23193. const networkInformation = window__default["default"].navigator.connection || window__default["default"].navigator.mozConnection || window__default["default"].navigator.webkitConnection;
  23194. const tenMbpsAsBitsPerSecond = 10e6;
  23195. if (this.options_.useNetworkInformationApi && networkInformation) {
  23196. // downlink returns Mbps
  23197. // https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/downlink
  23198. const networkInfoBandwidthEstBitsPerSec = networkInformation.downlink * 1000 * 1000; // downlink maxes out at 10 Mbps. In the event that both networkInformationApi and the player
  23199. // estimate a bandwidth greater than 10 Mbps, use the larger of the two estimates to ensure that
  23200. // high quality streams are not filtered out.
  23201. if (networkInfoBandwidthEstBitsPerSec >= tenMbpsAsBitsPerSecond && playerBandwidthEst >= tenMbpsAsBitsPerSecond) {
  23202. playerBandwidthEst = Math.max(playerBandwidthEst, networkInfoBandwidthEstBitsPerSec);
  23203. } else {
  23204. playerBandwidthEst = networkInfoBandwidthEstBitsPerSec;
  23205. }
  23206. }
  23207. return playerBandwidthEst;
  23208. },
  23209. set(bandwidth) {
  23210. this.playlistController_.mainSegmentLoader_.bandwidth = bandwidth; // setting the bandwidth manually resets the throughput counter
  23211. // `count` is set to zero that current value of `rate` isn't included
  23212. // in the cumulative average
  23213. this.playlistController_.mainSegmentLoader_.throughput = {
  23214. rate: 0,
  23215. count: 0
  23216. };
  23217. }
  23218. },
  23219. /**
  23220. * `systemBandwidth` is a combination of two serial processes bit-rates. The first
  23221. * is the network bitrate provided by `bandwidth` and the second is the bitrate of
  23222. * the entire process after that - decryption, transmuxing, and appending - provided
  23223. * by `throughput`.
  23224. *
  23225. * Since the two process are serial, the overall system bandwidth is given by:
  23226. * sysBandwidth = 1 / (1 / bandwidth + 1 / throughput)
  23227. */
  23228. systemBandwidth: {
  23229. get() {
  23230. const invBandwidth = 1 / (this.bandwidth || 1);
  23231. let invThroughput;
  23232. if (this.throughput > 0) {
  23233. invThroughput = 1 / this.throughput;
  23234. } else {
  23235. invThroughput = 0;
  23236. }
  23237. const systemBitrate = Math.floor(1 / (invBandwidth + invThroughput));
  23238. return systemBitrate;
  23239. },
  23240. set() {
  23241. videojs__default["default"].log.error('The "systemBandwidth" property is read-only');
  23242. }
  23243. }
  23244. });
  23245. if (this.options_.bandwidth) {
  23246. this.bandwidth = this.options_.bandwidth;
  23247. }
  23248. if (this.options_.throughput) {
  23249. this.throughput = this.options_.throughput;
  23250. }
  23251. Object.defineProperties(this.stats, {
  23252. bandwidth: {
  23253. get: () => this.bandwidth || 0,
  23254. enumerable: true
  23255. },
  23256. mediaRequests: {
  23257. get: () => this.playlistController_.mediaRequests_() || 0,
  23258. enumerable: true
  23259. },
  23260. mediaRequestsAborted: {
  23261. get: () => this.playlistController_.mediaRequestsAborted_() || 0,
  23262. enumerable: true
  23263. },
  23264. mediaRequestsTimedout: {
  23265. get: () => this.playlistController_.mediaRequestsTimedout_() || 0,
  23266. enumerable: true
  23267. },
  23268. mediaRequestsErrored: {
  23269. get: () => this.playlistController_.mediaRequestsErrored_() || 0,
  23270. enumerable: true
  23271. },
  23272. mediaTransferDuration: {
  23273. get: () => this.playlistController_.mediaTransferDuration_() || 0,
  23274. enumerable: true
  23275. },
  23276. mediaBytesTransferred: {
  23277. get: () => this.playlistController_.mediaBytesTransferred_() || 0,
  23278. enumerable: true
  23279. },
  23280. mediaSecondsLoaded: {
  23281. get: () => this.playlistController_.mediaSecondsLoaded_() || 0,
  23282. enumerable: true
  23283. },
  23284. mediaAppends: {
  23285. get: () => this.playlistController_.mediaAppends_() || 0,
  23286. enumerable: true
  23287. },
  23288. mainAppendsToLoadedData: {
  23289. get: () => this.playlistController_.mainAppendsToLoadedData_() || 0,
  23290. enumerable: true
  23291. },
  23292. audioAppendsToLoadedData: {
  23293. get: () => this.playlistController_.audioAppendsToLoadedData_() || 0,
  23294. enumerable: true
  23295. },
  23296. appendsToLoadedData: {
  23297. get: () => this.playlistController_.appendsToLoadedData_() || 0,
  23298. enumerable: true
  23299. },
  23300. timeToLoadedData: {
  23301. get: () => this.playlistController_.timeToLoadedData_() || 0,
  23302. enumerable: true
  23303. },
  23304. buffered: {
  23305. get: () => timeRangesToArray(this.tech_.buffered()),
  23306. enumerable: true
  23307. },
  23308. currentTime: {
  23309. get: () => this.tech_.currentTime(),
  23310. enumerable: true
  23311. },
  23312. currentSource: {
  23313. get: () => this.tech_.currentSource_,
  23314. enumerable: true
  23315. },
  23316. currentTech: {
  23317. get: () => this.tech_.name_,
  23318. enumerable: true
  23319. },
  23320. duration: {
  23321. get: () => this.tech_.duration(),
  23322. enumerable: true
  23323. },
  23324. main: {
  23325. get: () => this.playlists.main,
  23326. enumerable: true
  23327. },
  23328. playerDimensions: {
  23329. get: () => this.tech_.currentDimensions(),
  23330. enumerable: true
  23331. },
  23332. seekable: {
  23333. get: () => timeRangesToArray(this.tech_.seekable()),
  23334. enumerable: true
  23335. },
  23336. timestamp: {
  23337. get: () => Date.now(),
  23338. enumerable: true
  23339. },
  23340. videoPlaybackQuality: {
  23341. get: () => this.tech_.getVideoPlaybackQuality(),
  23342. enumerable: true
  23343. }
  23344. });
  23345. this.tech_.one('canplay', this.playlistController_.setupFirstPlay.bind(this.playlistController_));
  23346. this.tech_.on('bandwidthupdate', () => {
  23347. if (this.options_.useBandwidthFromLocalStorage) {
  23348. updateVhsLocalStorage({
  23349. bandwidth: this.bandwidth,
  23350. throughput: Math.round(this.throughput)
  23351. });
  23352. }
  23353. });
  23354. this.playlistController_.on('selectedinitialmedia', () => {
  23355. // Add the manual rendition mix-in to VhsHandler
  23356. renditionSelectionMixin(this);
  23357. });
  23358. this.playlistController_.sourceUpdater_.on('createdsourcebuffers', () => {
  23359. this.setupEme_();
  23360. }); // the bandwidth of the primary segment loader is our best
  23361. // estimate of overall bandwidth
  23362. this.on(this.playlistController_, 'progress', function () {
  23363. this.tech_.trigger('progress');
  23364. }); // In the live case, we need to ignore the very first `seeking` event since
  23365. // that will be the result of the seek-to-live behavior
  23366. this.on(this.playlistController_, 'firstplay', function () {
  23367. this.ignoreNextSeekingEvent_ = true;
  23368. });
  23369. this.setupQualityLevels_(); // do nothing if the tech has been disposed already
  23370. // this can occur if someone sets the src in player.ready(), for instance
  23371. if (!this.tech_.el()) {
  23372. return;
  23373. }
  23374. this.mediaSourceUrl_ = window__default["default"].URL.createObjectURL(this.playlistController_.mediaSource);
  23375. this.tech_.src(this.mediaSourceUrl_);
  23376. }
  23377. createKeySessions_() {
  23378. const audioPlaylistLoader = this.playlistController_.mediaTypes_.AUDIO.activePlaylistLoader;
  23379. this.logger_('waiting for EME key session creation');
  23380. waitForKeySessionCreation({
  23381. player: this.player_,
  23382. sourceKeySystems: this.source_.keySystems,
  23383. audioMedia: audioPlaylistLoader && audioPlaylistLoader.media(),
  23384. mainPlaylists: this.playlists.main.playlists
  23385. }).then(() => {
  23386. this.logger_('created EME key session');
  23387. this.playlistController_.sourceUpdater_.initializedEme();
  23388. }).catch(err => {
  23389. this.logger_('error while creating EME key session', err);
  23390. this.player_.error({
  23391. message: 'Failed to initialize media keys for EME',
  23392. code: 3
  23393. });
  23394. });
  23395. }
  23396. handleWaitingForKey_() {
  23397. // If waitingforkey is fired, it's possible that the data that's necessary to retrieve
  23398. // the key is in the manifest. While this should've happened on initial source load, it
  23399. // may happen again in live streams where the keys change, and the manifest info
  23400. // reflects the update.
  23401. //
  23402. // Because videojs-contrib-eme compares the PSSH data we send to that of PSSH data it's
  23403. // already requested keys for, we don't have to worry about this generating extraneous
  23404. // requests.
  23405. this.logger_('waitingforkey fired, attempting to create any new key sessions');
  23406. this.createKeySessions_();
  23407. }
  23408. /**
  23409. * If necessary and EME is available, sets up EME options and waits for key session
  23410. * creation.
  23411. *
  23412. * This function also updates the source updater so taht it can be used, as for some
  23413. * browsers, EME must be configured before content is appended (if appending unencrypted
  23414. * content before encrypted content).
  23415. */
  23416. setupEme_() {
  23417. const audioPlaylistLoader = this.playlistController_.mediaTypes_.AUDIO.activePlaylistLoader;
  23418. const didSetupEmeOptions = setupEmeOptions({
  23419. player: this.player_,
  23420. sourceKeySystems: this.source_.keySystems,
  23421. media: this.playlists.media(),
  23422. audioMedia: audioPlaylistLoader && audioPlaylistLoader.media()
  23423. });
  23424. this.player_.tech_.on('keystatuschange', e => {
  23425. this.playlistController_.updatePlaylistByKeyStatus(e.keyId, e.status);
  23426. });
  23427. this.handleWaitingForKey_ = this.handleWaitingForKey_.bind(this);
  23428. this.player_.tech_.on('waitingforkey', this.handleWaitingForKey_);
  23429. if (!didSetupEmeOptions) {
  23430. // If EME options were not set up, we've done all we could to initialize EME.
  23431. this.playlistController_.sourceUpdater_.initializedEme();
  23432. return;
  23433. }
  23434. this.createKeySessions_();
  23435. }
  23436. /**
  23437. * Initializes the quality levels and sets listeners to update them.
  23438. *
  23439. * @method setupQualityLevels_
  23440. * @private
  23441. */
  23442. setupQualityLevels_() {
  23443. const player = videojs__default["default"].players[this.tech_.options_.playerId]; // if there isn't a player or there isn't a qualityLevels plugin
  23444. // or qualityLevels_ listeners have already been setup, do nothing.
  23445. if (!player || !player.qualityLevels || this.qualityLevels_) {
  23446. return;
  23447. }
  23448. this.qualityLevels_ = player.qualityLevels();
  23449. this.playlistController_.on('selectedinitialmedia', () => {
  23450. handleVhsLoadedMetadata(this.qualityLevels_, this);
  23451. });
  23452. this.playlists.on('mediachange', () => {
  23453. handleVhsMediaChange(this.qualityLevels_, this.playlists);
  23454. });
  23455. }
  23456. /**
  23457. * return the version
  23458. */
  23459. static version() {
  23460. return {
  23461. '@videojs/http-streaming': version$4,
  23462. 'mux.js': version$3,
  23463. 'mpd-parser': version$2,
  23464. 'm3u8-parser': version$1,
  23465. 'aes-decrypter': version
  23466. };
  23467. }
  23468. /**
  23469. * return the version
  23470. */
  23471. version() {
  23472. return this.constructor.version();
  23473. }
  23474. canChangeType() {
  23475. return SourceUpdater.canChangeType();
  23476. }
  23477. /**
  23478. * Begin playing the video.
  23479. */
  23480. play() {
  23481. this.playlistController_.play();
  23482. }
  23483. /**
  23484. * a wrapper around the function in PlaylistController
  23485. */
  23486. setCurrentTime(currentTime) {
  23487. this.playlistController_.setCurrentTime(currentTime);
  23488. }
  23489. /**
  23490. * a wrapper around the function in PlaylistController
  23491. */
  23492. duration() {
  23493. return this.playlistController_.duration();
  23494. }
  23495. /**
  23496. * a wrapper around the function in PlaylistController
  23497. */
  23498. seekable() {
  23499. return this.playlistController_.seekable();
  23500. }
  23501. /**
  23502. * Abort all outstanding work and cleanup.
  23503. */
  23504. dispose() {
  23505. if (this.playbackWatcher_) {
  23506. this.playbackWatcher_.dispose();
  23507. }
  23508. if (this.playlistController_) {
  23509. this.playlistController_.dispose();
  23510. }
  23511. if (this.qualityLevels_) {
  23512. this.qualityLevels_.dispose();
  23513. }
  23514. if (this.tech_ && this.tech_.vhs) {
  23515. delete this.tech_.vhs;
  23516. }
  23517. if (this.mediaSourceUrl_ && window__default["default"].URL.revokeObjectURL) {
  23518. window__default["default"].URL.revokeObjectURL(this.mediaSourceUrl_);
  23519. this.mediaSourceUrl_ = null;
  23520. }
  23521. if (this.tech_) {
  23522. this.tech_.off('waitingforkey', this.handleWaitingForKey_);
  23523. }
  23524. super.dispose();
  23525. }
  23526. convertToProgramTime(time, callback) {
  23527. return getProgramTime({
  23528. playlist: this.playlistController_.media(),
  23529. time,
  23530. callback
  23531. });
  23532. } // the player must be playing before calling this
  23533. seekToProgramTime(programTime, callback, pauseAfterSeek = true, retryCount = 2) {
  23534. return seekToProgramTime({
  23535. programTime,
  23536. playlist: this.playlistController_.media(),
  23537. retryCount,
  23538. pauseAfterSeek,
  23539. seekTo: this.options_.seekTo,
  23540. tech: this.options_.tech,
  23541. callback
  23542. });
  23543. }
  23544. /**
  23545. * Adds the onRequest, onResponse, offRequest and offResponse functions
  23546. * to the VhsHandler xhr Object.
  23547. */
  23548. setupXhrHooks_() {
  23549. /**
  23550. * A player function for setting an onRequest hook
  23551. *
  23552. * @param {function} callback for request modifiction
  23553. */
  23554. this.xhr.onRequest = callback => {
  23555. addOnRequestHook(this.xhr, callback);
  23556. };
  23557. /**
  23558. * A player function for setting an onResponse hook
  23559. *
  23560. * @param {callback} callback for response data retrieval
  23561. */
  23562. this.xhr.onResponse = callback => {
  23563. addOnResponseHook(this.xhr, callback);
  23564. };
  23565. /**
  23566. * Deletes a player onRequest callback if it exists
  23567. *
  23568. * @param {function} callback to delete from the player set
  23569. */
  23570. this.xhr.offRequest = callback => {
  23571. removeOnRequestHook(this.xhr, callback);
  23572. };
  23573. /**
  23574. * Deletes a player onResponse callback if it exists
  23575. *
  23576. * @param {function} callback to delete from the player set
  23577. */
  23578. this.xhr.offResponse = callback => {
  23579. removeOnResponseHook(this.xhr, callback);
  23580. }; // Trigger an event on the player to notify the user that vhs is ready to set xhr hooks.
  23581. // This allows hooks to be set before the source is set to vhs when handleSource is called.
  23582. this.player_.trigger('xhr-hooks-ready');
  23583. }
  23584. }
  23585. /**
  23586. * The Source Handler object, which informs video.js what additional
  23587. * MIME types are supported and sets up playback. It is registered
  23588. * automatically to the appropriate tech based on the capabilities of
  23589. * the browser it is running in. It is not necessary to use or modify
  23590. * this object in normal usage.
  23591. */
  23592. const VhsSourceHandler = {
  23593. name: 'videojs-http-streaming',
  23594. VERSION: version$4,
  23595. canHandleSource(srcObj, options = {}) {
  23596. const localOptions = merge(videojs__default["default"].options, options);
  23597. return VhsSourceHandler.canPlayType(srcObj.type, localOptions);
  23598. },
  23599. handleSource(source, tech, options = {}) {
  23600. const localOptions = merge(videojs__default["default"].options, options);
  23601. tech.vhs = new VhsHandler(source, tech, localOptions);
  23602. tech.vhs.xhr = xhrFactory();
  23603. tech.vhs.setupXhrHooks_();
  23604. tech.vhs.src(source.src, source.type);
  23605. return tech.vhs;
  23606. },
  23607. canPlayType(type, options) {
  23608. const simpleType = mediaTypes_js.simpleTypeFromSourceType(type);
  23609. if (!simpleType) {
  23610. return '';
  23611. }
  23612. const overrideNative = VhsSourceHandler.getOverrideNative(options);
  23613. const supportsTypeNatively = Vhs.supportsTypeNatively(simpleType);
  23614. const canUseMsePlayback = !supportsTypeNatively || overrideNative;
  23615. return canUseMsePlayback ? 'maybe' : '';
  23616. },
  23617. getOverrideNative(options = {}) {
  23618. const {
  23619. vhs = {}
  23620. } = options;
  23621. const defaultOverrideNative = !(videojs__default["default"].browser.IS_ANY_SAFARI || videojs__default["default"].browser.IS_IOS);
  23622. const {
  23623. overrideNative = defaultOverrideNative
  23624. } = vhs;
  23625. return overrideNative;
  23626. }
  23627. };
  23628. /**
  23629. * Check to see if the native MediaSource object exists and supports
  23630. * an MP4 container with both H.264 video and AAC-LC audio.
  23631. *
  23632. * @return {boolean} if native media sources are supported
  23633. */
  23634. const supportsNativeMediaSources = () => {
  23635. return codecs_js.browserSupportsCodec('avc1.4d400d,mp4a.40.2');
  23636. }; // register source handlers with the appropriate techs
  23637. if (supportsNativeMediaSources()) {
  23638. videojs__default["default"].getTech('Html5').registerSourceHandler(VhsSourceHandler, 0);
  23639. }
  23640. videojs__default["default"].VhsHandler = VhsHandler;
  23641. videojs__default["default"].VhsSourceHandler = VhsSourceHandler;
  23642. videojs__default["default"].Vhs = Vhs;
  23643. if (!videojs__default["default"].use) {
  23644. videojs__default["default"].registerComponent('Vhs', Vhs);
  23645. }
  23646. videojs__default["default"].options.vhs = videojs__default["default"].options.vhs || {};
  23647. if (!videojs__default["default"].getPlugin || !videojs__default["default"].getPlugin('reloadSourceOnError')) {
  23648. videojs__default["default"].registerPlugin('reloadSourceOnError', reloadSourceOnError);
  23649. }
  23650. Object.defineProperty(exports, 'simpleTypeFromSourceType', {
  23651. enumerable: true,
  23652. get: function () { return mediaTypes_js.simpleTypeFromSourceType; }
  23653. });
  23654. exports.LOCAL_STORAGE_KEY = LOCAL_STORAGE_KEY;
  23655. exports.Vhs = Vhs;
  23656. exports.VhsHandler = VhsHandler;
  23657. exports.VhsSourceHandler = VhsSourceHandler;
  23658. exports.emeKeySystems = emeKeySystems;
  23659. exports.expandDataUri = expandDataUri;
  23660. exports.getAllPsshKeySystemsOptions = getAllPsshKeySystemsOptions;
  23661. exports.setupEmeOptions = setupEmeOptions;
  23662. exports.waitForKeySessionCreation = waitForKeySessionCreation;