hls.js 1.1 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285
  1. (function __HLS_WORKER_BUNDLE__(__IN_WORKER__){
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  4. typeof define === 'function' && define.amd ? define(factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Hls = factory());
  6. })(this, (function () { 'use strict';
  7. function ownKeys(e, r) {
  8. var t = Object.keys(e);
  9. if (Object.getOwnPropertySymbols) {
  10. var o = Object.getOwnPropertySymbols(e);
  11. r && (o = o.filter(function (r) {
  12. return Object.getOwnPropertyDescriptor(e, r).enumerable;
  13. })), t.push.apply(t, o);
  14. }
  15. return t;
  16. }
  17. function _objectSpread2(e) {
  18. for (var r = 1; r < arguments.length; r++) {
  19. var t = null != arguments[r] ? arguments[r] : {};
  20. r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
  21. _defineProperty(e, r, t[r]);
  22. }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
  23. Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
  24. });
  25. }
  26. return e;
  27. }
  28. function _toPrimitive(t, r) {
  29. if ("object" != typeof t || !t) return t;
  30. var e = t[Symbol.toPrimitive];
  31. if (void 0 !== e) {
  32. var i = e.call(t, r || "default");
  33. if ("object" != typeof i) return i;
  34. throw new TypeError("@@toPrimitive must return a primitive value.");
  35. }
  36. return ("string" === r ? String : Number)(t);
  37. }
  38. function _toPropertyKey(t) {
  39. var i = _toPrimitive(t, "string");
  40. return "symbol" == typeof i ? i : String(i);
  41. }
  42. function _defineProperties(target, props) {
  43. for (var i = 0; i < props.length; i++) {
  44. var descriptor = props[i];
  45. descriptor.enumerable = descriptor.enumerable || false;
  46. descriptor.configurable = true;
  47. if ("value" in descriptor) descriptor.writable = true;
  48. Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
  49. }
  50. }
  51. function _createClass(Constructor, protoProps, staticProps) {
  52. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  53. if (staticProps) _defineProperties(Constructor, staticProps);
  54. Object.defineProperty(Constructor, "prototype", {
  55. writable: false
  56. });
  57. return Constructor;
  58. }
  59. function _defineProperty(obj, key, value) {
  60. key = _toPropertyKey(key);
  61. if (key in obj) {
  62. Object.defineProperty(obj, key, {
  63. value: value,
  64. enumerable: true,
  65. configurable: true,
  66. writable: true
  67. });
  68. } else {
  69. obj[key] = value;
  70. }
  71. return obj;
  72. }
  73. function _extends() {
  74. _extends = Object.assign ? Object.assign.bind() : function (target) {
  75. for (var i = 1; i < arguments.length; i++) {
  76. var source = arguments[i];
  77. for (var key in source) {
  78. if (Object.prototype.hasOwnProperty.call(source, key)) {
  79. target[key] = source[key];
  80. }
  81. }
  82. }
  83. return target;
  84. };
  85. return _extends.apply(this, arguments);
  86. }
  87. function _inheritsLoose(subClass, superClass) {
  88. subClass.prototype = Object.create(superClass.prototype);
  89. subClass.prototype.constructor = subClass;
  90. _setPrototypeOf(subClass, superClass);
  91. }
  92. function _getPrototypeOf(o) {
  93. _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
  94. return o.__proto__ || Object.getPrototypeOf(o);
  95. };
  96. return _getPrototypeOf(o);
  97. }
  98. function _setPrototypeOf(o, p) {
  99. _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
  100. o.__proto__ = p;
  101. return o;
  102. };
  103. return _setPrototypeOf(o, p);
  104. }
  105. function _isNativeReflectConstruct() {
  106. if (typeof Reflect === "undefined" || !Reflect.construct) return false;
  107. if (Reflect.construct.sham) return false;
  108. if (typeof Proxy === "function") return true;
  109. try {
  110. Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  111. return true;
  112. } catch (e) {
  113. return false;
  114. }
  115. }
  116. function _construct(Parent, args, Class) {
  117. if (_isNativeReflectConstruct()) {
  118. _construct = Reflect.construct.bind();
  119. } else {
  120. _construct = function _construct(Parent, args, Class) {
  121. var a = [null];
  122. a.push.apply(a, args);
  123. var Constructor = Function.bind.apply(Parent, a);
  124. var instance = new Constructor();
  125. if (Class) _setPrototypeOf(instance, Class.prototype);
  126. return instance;
  127. };
  128. }
  129. return _construct.apply(null, arguments);
  130. }
  131. function _isNativeFunction(fn) {
  132. try {
  133. return Function.toString.call(fn).indexOf("[native code]") !== -1;
  134. } catch (e) {
  135. return typeof fn === "function";
  136. }
  137. }
  138. function _wrapNativeSuper(Class) {
  139. var _cache = typeof Map === "function" ? new Map() : undefined;
  140. _wrapNativeSuper = function _wrapNativeSuper(Class) {
  141. if (Class === null || !_isNativeFunction(Class)) return Class;
  142. if (typeof Class !== "function") {
  143. throw new TypeError("Super expression must either be null or a function");
  144. }
  145. if (typeof _cache !== "undefined") {
  146. if (_cache.has(Class)) return _cache.get(Class);
  147. _cache.set(Class, Wrapper);
  148. }
  149. function Wrapper() {
  150. return _construct(Class, arguments, _getPrototypeOf(this).constructor);
  151. }
  152. Wrapper.prototype = Object.create(Class.prototype, {
  153. constructor: {
  154. value: Wrapper,
  155. enumerable: false,
  156. writable: true,
  157. configurable: true
  158. }
  159. });
  160. return _setPrototypeOf(Wrapper, Class);
  161. };
  162. return _wrapNativeSuper(Class);
  163. }
  164. function _assertThisInitialized(self) {
  165. if (self === void 0) {
  166. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  167. }
  168. return self;
  169. }
  170. function _unsupportedIterableToArray(o, minLen) {
  171. if (!o) return;
  172. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  173. var n = Object.prototype.toString.call(o).slice(8, -1);
  174. if (n === "Object" && o.constructor) n = o.constructor.name;
  175. if (n === "Map" || n === "Set") return Array.from(o);
  176. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  177. }
  178. function _arrayLikeToArray(arr, len) {
  179. if (len == null || len > arr.length) len = arr.length;
  180. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  181. return arr2;
  182. }
  183. function _createForOfIteratorHelperLoose(o, allowArrayLike) {
  184. var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
  185. if (it) return (it = it.call(o)).next.bind(it);
  186. if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
  187. if (it) o = it;
  188. var i = 0;
  189. return function () {
  190. if (i >= o.length) return {
  191. done: true
  192. };
  193. return {
  194. done: false,
  195. value: o[i++]
  196. };
  197. };
  198. }
  199. throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  200. }
  201. function getDefaultExportFromCjs (x) {
  202. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  203. }
  204. var urlToolkit = {exports: {}};
  205. (function (module, exports) {
  206. // see https://tools.ietf.org/html/rfc1808
  207. (function (root) {
  208. var URL_REGEX =
  209. /^(?=((?:[a-zA-Z0-9+\-.]+:)?))\1(?=((?:\/\/[^\/?#]*)?))\2(?=((?:(?:[^?#\/]*\/)*[^;?#\/]*)?))\3((?:;[^?#]*)?)(\?[^#]*)?(#[^]*)?$/;
  210. var FIRST_SEGMENT_REGEX = /^(?=([^\/?#]*))\1([^]*)$/;
  211. var SLASH_DOT_REGEX = /(?:\/|^)\.(?=\/)/g;
  212. var SLASH_DOT_DOT_REGEX = /(?:\/|^)\.\.\/(?!\.\.\/)[^\/]*(?=\/)/g;
  213. var URLToolkit = {
  214. // If opts.alwaysNormalize is true then the path will always be normalized even when it starts with / or //
  215. // E.g
  216. // With opts.alwaysNormalize = false (default, spec compliant)
  217. // http://a.com/b/cd + /e/f/../g => http://a.com/e/f/../g
  218. // With opts.alwaysNormalize = true (not spec compliant)
  219. // http://a.com/b/cd + /e/f/../g => http://a.com/e/g
  220. buildAbsoluteURL: function (baseURL, relativeURL, opts) {
  221. opts = opts || {};
  222. // remove any remaining space and CRLF
  223. baseURL = baseURL.trim();
  224. relativeURL = relativeURL.trim();
  225. if (!relativeURL) {
  226. // 2a) If the embedded URL is entirely empty, it inherits the
  227. // entire base URL (i.e., is set equal to the base URL)
  228. // and we are done.
  229. if (!opts.alwaysNormalize) {
  230. return baseURL;
  231. }
  232. var basePartsForNormalise = URLToolkit.parseURL(baseURL);
  233. if (!basePartsForNormalise) {
  234. throw new Error('Error trying to parse base URL.');
  235. }
  236. basePartsForNormalise.path = URLToolkit.normalizePath(
  237. basePartsForNormalise.path
  238. );
  239. return URLToolkit.buildURLFromParts(basePartsForNormalise);
  240. }
  241. var relativeParts = URLToolkit.parseURL(relativeURL);
  242. if (!relativeParts) {
  243. throw new Error('Error trying to parse relative URL.');
  244. }
  245. if (relativeParts.scheme) {
  246. // 2b) If the embedded URL starts with a scheme name, it is
  247. // interpreted as an absolute URL and we are done.
  248. if (!opts.alwaysNormalize) {
  249. return relativeURL;
  250. }
  251. relativeParts.path = URLToolkit.normalizePath(relativeParts.path);
  252. return URLToolkit.buildURLFromParts(relativeParts);
  253. }
  254. var baseParts = URLToolkit.parseURL(baseURL);
  255. if (!baseParts) {
  256. throw new Error('Error trying to parse base URL.');
  257. }
  258. if (!baseParts.netLoc && baseParts.path && baseParts.path[0] !== '/') {
  259. // If netLoc missing and path doesn't start with '/', assume everthing before the first '/' is the netLoc
  260. // This causes 'example.com/a' to be handled as '//example.com/a' instead of '/example.com/a'
  261. var pathParts = FIRST_SEGMENT_REGEX.exec(baseParts.path);
  262. baseParts.netLoc = pathParts[1];
  263. baseParts.path = pathParts[2];
  264. }
  265. if (baseParts.netLoc && !baseParts.path) {
  266. baseParts.path = '/';
  267. }
  268. var builtParts = {
  269. // 2c) Otherwise, the embedded URL inherits the scheme of
  270. // the base URL.
  271. scheme: baseParts.scheme,
  272. netLoc: relativeParts.netLoc,
  273. path: null,
  274. params: relativeParts.params,
  275. query: relativeParts.query,
  276. fragment: relativeParts.fragment,
  277. };
  278. if (!relativeParts.netLoc) {
  279. // 3) If the embedded URL's <net_loc> is non-empty, we skip to
  280. // Step 7. Otherwise, the embedded URL inherits the <net_loc>
  281. // (if any) of the base URL.
  282. builtParts.netLoc = baseParts.netLoc;
  283. // 4) If the embedded URL path is preceded by a slash "/", the
  284. // path is not relative and we skip to Step 7.
  285. if (relativeParts.path[0] !== '/') {
  286. if (!relativeParts.path) {
  287. // 5) If the embedded URL path is empty (and not preceded by a
  288. // slash), then the embedded URL inherits the base URL path
  289. builtParts.path = baseParts.path;
  290. // 5a) if the embedded URL's <params> is non-empty, we skip to
  291. // step 7; otherwise, it inherits the <params> of the base
  292. // URL (if any) and
  293. if (!relativeParts.params) {
  294. builtParts.params = baseParts.params;
  295. // 5b) if the embedded URL's <query> is non-empty, we skip to
  296. // step 7; otherwise, it inherits the <query> of the base
  297. // URL (if any) and we skip to step 7.
  298. if (!relativeParts.query) {
  299. builtParts.query = baseParts.query;
  300. }
  301. }
  302. } else {
  303. // 6) The last segment of the base URL's path (anything
  304. // following the rightmost slash "/", or the entire path if no
  305. // slash is present) is removed and the embedded URL's path is
  306. // appended in its place.
  307. var baseURLPath = baseParts.path;
  308. var newPath =
  309. baseURLPath.substring(0, baseURLPath.lastIndexOf('/') + 1) +
  310. relativeParts.path;
  311. builtParts.path = URLToolkit.normalizePath(newPath);
  312. }
  313. }
  314. }
  315. if (builtParts.path === null) {
  316. builtParts.path = opts.alwaysNormalize
  317. ? URLToolkit.normalizePath(relativeParts.path)
  318. : relativeParts.path;
  319. }
  320. return URLToolkit.buildURLFromParts(builtParts);
  321. },
  322. parseURL: function (url) {
  323. var parts = URL_REGEX.exec(url);
  324. if (!parts) {
  325. return null;
  326. }
  327. return {
  328. scheme: parts[1] || '',
  329. netLoc: parts[2] || '',
  330. path: parts[3] || '',
  331. params: parts[4] || '',
  332. query: parts[5] || '',
  333. fragment: parts[6] || '',
  334. };
  335. },
  336. normalizePath: function (path) {
  337. // The following operations are
  338. // then applied, in order, to the new path:
  339. // 6a) All occurrences of "./", where "." is a complete path
  340. // segment, are removed.
  341. // 6b) If the path ends with "." as a complete path segment,
  342. // that "." is removed.
  343. path = path.split('').reverse().join('').replace(SLASH_DOT_REGEX, '');
  344. // 6c) All occurrences of "<segment>/../", where <segment> is a
  345. // complete path segment not equal to "..", are removed.
  346. // Removal of these path segments is performed iteratively,
  347. // removing the leftmost matching pattern on each iteration,
  348. // until no matching pattern remains.
  349. // 6d) If the path ends with "<segment>/..", where <segment> is a
  350. // complete path segment not equal to "..", that
  351. // "<segment>/.." is removed.
  352. while (
  353. path.length !== (path = path.replace(SLASH_DOT_DOT_REGEX, '')).length
  354. ) {}
  355. return path.split('').reverse().join('');
  356. },
  357. buildURLFromParts: function (parts) {
  358. return (
  359. parts.scheme +
  360. parts.netLoc +
  361. parts.path +
  362. parts.params +
  363. parts.query +
  364. parts.fragment
  365. );
  366. },
  367. };
  368. module.exports = URLToolkit;
  369. })();
  370. } (urlToolkit));
  371. var urlToolkitExports = urlToolkit.exports;
  372. // https://caniuse.com/mdn-javascript_builtins_number_isfinite
  373. var isFiniteNumber = Number.isFinite || function (value) {
  374. return typeof value === 'number' && isFinite(value);
  375. };
  376. // https://caniuse.com/mdn-javascript_builtins_number_issafeinteger
  377. var isSafeInteger = Number.isSafeInteger || function (value) {
  378. return typeof value === 'number' && Math.abs(value) <= MAX_SAFE_INTEGER;
  379. };
  380. var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || 9007199254740991;
  381. var Events = /*#__PURE__*/function (Events) {
  382. Events["MEDIA_ATTACHING"] = "hlsMediaAttaching";
  383. Events["MEDIA_ATTACHED"] = "hlsMediaAttached";
  384. Events["MEDIA_DETACHING"] = "hlsMediaDetaching";
  385. Events["MEDIA_DETACHED"] = "hlsMediaDetached";
  386. Events["BUFFER_RESET"] = "hlsBufferReset";
  387. Events["BUFFER_CODECS"] = "hlsBufferCodecs";
  388. Events["BUFFER_CREATED"] = "hlsBufferCreated";
  389. Events["BUFFER_APPENDING"] = "hlsBufferAppending";
  390. Events["BUFFER_APPENDED"] = "hlsBufferAppended";
  391. Events["BUFFER_EOS"] = "hlsBufferEos";
  392. Events["BUFFER_FLUSHING"] = "hlsBufferFlushing";
  393. Events["BUFFER_FLUSHED"] = "hlsBufferFlushed";
  394. Events["MANIFEST_LOADING"] = "hlsManifestLoading";
  395. Events["MANIFEST_LOADED"] = "hlsManifestLoaded";
  396. Events["MANIFEST_PARSED"] = "hlsManifestParsed";
  397. Events["LEVEL_SWITCHING"] = "hlsLevelSwitching";
  398. Events["LEVEL_SWITCHED"] = "hlsLevelSwitched";
  399. Events["LEVEL_LOADING"] = "hlsLevelLoading";
  400. Events["LEVEL_LOADED"] = "hlsLevelLoaded";
  401. Events["LEVEL_UPDATED"] = "hlsLevelUpdated";
  402. Events["LEVEL_PTS_UPDATED"] = "hlsLevelPtsUpdated";
  403. Events["LEVELS_UPDATED"] = "hlsLevelsUpdated";
  404. Events["AUDIO_TRACKS_UPDATED"] = "hlsAudioTracksUpdated";
  405. Events["AUDIO_TRACK_SWITCHING"] = "hlsAudioTrackSwitching";
  406. Events["AUDIO_TRACK_SWITCHED"] = "hlsAudioTrackSwitched";
  407. Events["AUDIO_TRACK_LOADING"] = "hlsAudioTrackLoading";
  408. Events["AUDIO_TRACK_LOADED"] = "hlsAudioTrackLoaded";
  409. Events["SUBTITLE_TRACKS_UPDATED"] = "hlsSubtitleTracksUpdated";
  410. Events["SUBTITLE_TRACKS_CLEARED"] = "hlsSubtitleTracksCleared";
  411. Events["SUBTITLE_TRACK_SWITCH"] = "hlsSubtitleTrackSwitch";
  412. Events["SUBTITLE_TRACK_LOADING"] = "hlsSubtitleTrackLoading";
  413. Events["SUBTITLE_TRACK_LOADED"] = "hlsSubtitleTrackLoaded";
  414. Events["SUBTITLE_FRAG_PROCESSED"] = "hlsSubtitleFragProcessed";
  415. Events["CUES_PARSED"] = "hlsCuesParsed";
  416. Events["NON_NATIVE_TEXT_TRACKS_FOUND"] = "hlsNonNativeTextTracksFound";
  417. Events["INIT_PTS_FOUND"] = "hlsInitPtsFound";
  418. Events["FRAG_LOADING"] = "hlsFragLoading";
  419. Events["FRAG_LOAD_EMERGENCY_ABORTED"] = "hlsFragLoadEmergencyAborted";
  420. Events["FRAG_LOADED"] = "hlsFragLoaded";
  421. Events["FRAG_DECRYPTED"] = "hlsFragDecrypted";
  422. Events["FRAG_PARSING_INIT_SEGMENT"] = "hlsFragParsingInitSegment";
  423. Events["FRAG_PARSING_USERDATA"] = "hlsFragParsingUserdata";
  424. Events["FRAG_PARSING_METADATA"] = "hlsFragParsingMetadata";
  425. Events["FRAG_PARSED"] = "hlsFragParsed";
  426. Events["FRAG_BUFFERED"] = "hlsFragBuffered";
  427. Events["FRAG_CHANGED"] = "hlsFragChanged";
  428. Events["FPS_DROP"] = "hlsFpsDrop";
  429. Events["FPS_DROP_LEVEL_CAPPING"] = "hlsFpsDropLevelCapping";
  430. Events["MAX_AUTO_LEVEL_UPDATED"] = "hlsMaxAutoLevelUpdated";
  431. Events["ERROR"] = "hlsError";
  432. Events["DESTROYING"] = "hlsDestroying";
  433. Events["KEY_LOADING"] = "hlsKeyLoading";
  434. Events["KEY_LOADED"] = "hlsKeyLoaded";
  435. Events["LIVE_BACK_BUFFER_REACHED"] = "hlsLiveBackBufferReached";
  436. Events["BACK_BUFFER_REACHED"] = "hlsBackBufferReached";
  437. Events["STEERING_MANIFEST_LOADED"] = "hlsSteeringManifestLoaded";
  438. return Events;
  439. }({});
  440. /**
  441. * Defines each Event type and payload by Event name. Used in {@link hls.js#HlsEventEmitter} to strongly type the event listener API.
  442. */
  443. var ErrorTypes = /*#__PURE__*/function (ErrorTypes) {
  444. ErrorTypes["NETWORK_ERROR"] = "networkError";
  445. ErrorTypes["MEDIA_ERROR"] = "mediaError";
  446. ErrorTypes["KEY_SYSTEM_ERROR"] = "keySystemError";
  447. ErrorTypes["MUX_ERROR"] = "muxError";
  448. ErrorTypes["OTHER_ERROR"] = "otherError";
  449. return ErrorTypes;
  450. }({});
  451. var ErrorDetails = /*#__PURE__*/function (ErrorDetails) {
  452. ErrorDetails["KEY_SYSTEM_NO_KEYS"] = "keySystemNoKeys";
  453. ErrorDetails["KEY_SYSTEM_NO_ACCESS"] = "keySystemNoAccess";
  454. ErrorDetails["KEY_SYSTEM_NO_SESSION"] = "keySystemNoSession";
  455. ErrorDetails["KEY_SYSTEM_NO_CONFIGURED_LICENSE"] = "keySystemNoConfiguredLicense";
  456. ErrorDetails["KEY_SYSTEM_LICENSE_REQUEST_FAILED"] = "keySystemLicenseRequestFailed";
  457. ErrorDetails["KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED"] = "keySystemServerCertificateRequestFailed";
  458. ErrorDetails["KEY_SYSTEM_SERVER_CERTIFICATE_UPDATE_FAILED"] = "keySystemServerCertificateUpdateFailed";
  459. ErrorDetails["KEY_SYSTEM_SESSION_UPDATE_FAILED"] = "keySystemSessionUpdateFailed";
  460. ErrorDetails["KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED"] = "keySystemStatusOutputRestricted";
  461. ErrorDetails["KEY_SYSTEM_STATUS_INTERNAL_ERROR"] = "keySystemStatusInternalError";
  462. ErrorDetails["MANIFEST_LOAD_ERROR"] = "manifestLoadError";
  463. ErrorDetails["MANIFEST_LOAD_TIMEOUT"] = "manifestLoadTimeOut";
  464. ErrorDetails["MANIFEST_PARSING_ERROR"] = "manifestParsingError";
  465. ErrorDetails["MANIFEST_INCOMPATIBLE_CODECS_ERROR"] = "manifestIncompatibleCodecsError";
  466. ErrorDetails["LEVEL_EMPTY_ERROR"] = "levelEmptyError";
  467. ErrorDetails["LEVEL_LOAD_ERROR"] = "levelLoadError";
  468. ErrorDetails["LEVEL_LOAD_TIMEOUT"] = "levelLoadTimeOut";
  469. ErrorDetails["LEVEL_PARSING_ERROR"] = "levelParsingError";
  470. ErrorDetails["LEVEL_SWITCH_ERROR"] = "levelSwitchError";
  471. ErrorDetails["AUDIO_TRACK_LOAD_ERROR"] = "audioTrackLoadError";
  472. ErrorDetails["AUDIO_TRACK_LOAD_TIMEOUT"] = "audioTrackLoadTimeOut";
  473. ErrorDetails["SUBTITLE_LOAD_ERROR"] = "subtitleTrackLoadError";
  474. ErrorDetails["SUBTITLE_TRACK_LOAD_TIMEOUT"] = "subtitleTrackLoadTimeOut";
  475. ErrorDetails["FRAG_LOAD_ERROR"] = "fragLoadError";
  476. ErrorDetails["FRAG_LOAD_TIMEOUT"] = "fragLoadTimeOut";
  477. ErrorDetails["FRAG_DECRYPT_ERROR"] = "fragDecryptError";
  478. ErrorDetails["FRAG_PARSING_ERROR"] = "fragParsingError";
  479. ErrorDetails["FRAG_GAP"] = "fragGap";
  480. ErrorDetails["REMUX_ALLOC_ERROR"] = "remuxAllocError";
  481. ErrorDetails["KEY_LOAD_ERROR"] = "keyLoadError";
  482. ErrorDetails["KEY_LOAD_TIMEOUT"] = "keyLoadTimeOut";
  483. ErrorDetails["BUFFER_ADD_CODEC_ERROR"] = "bufferAddCodecError";
  484. ErrorDetails["BUFFER_INCOMPATIBLE_CODECS_ERROR"] = "bufferIncompatibleCodecsError";
  485. ErrorDetails["BUFFER_APPEND_ERROR"] = "bufferAppendError";
  486. ErrorDetails["BUFFER_APPENDING_ERROR"] = "bufferAppendingError";
  487. ErrorDetails["BUFFER_STALLED_ERROR"] = "bufferStalledError";
  488. ErrorDetails["BUFFER_FULL_ERROR"] = "bufferFullError";
  489. ErrorDetails["BUFFER_SEEK_OVER_HOLE"] = "bufferSeekOverHole";
  490. ErrorDetails["BUFFER_NUDGE_ON_STALL"] = "bufferNudgeOnStall";
  491. ErrorDetails["INTERNAL_EXCEPTION"] = "internalException";
  492. ErrorDetails["INTERNAL_ABORTED"] = "aborted";
  493. ErrorDetails["UNKNOWN"] = "unknown";
  494. return ErrorDetails;
  495. }({});
  496. var noop = function noop() {};
  497. var fakeLogger = {
  498. trace: noop,
  499. debug: noop,
  500. log: noop,
  501. warn: noop,
  502. info: noop,
  503. error: noop
  504. };
  505. var exportedLogger = fakeLogger;
  506. // let lastCallTime;
  507. // function formatMsgWithTimeInfo(type, msg) {
  508. // const now = Date.now();
  509. // const diff = lastCallTime ? '+' + (now - lastCallTime) : '0';
  510. // lastCallTime = now;
  511. // msg = (new Date(now)).toISOString() + ' | [' + type + '] > ' + msg + ' ( ' + diff + ' ms )';
  512. // return msg;
  513. // }
  514. function consolePrintFn(type) {
  515. var func = self.console[type];
  516. if (func) {
  517. return func.bind(self.console, "[" + type + "] >");
  518. }
  519. return noop;
  520. }
  521. function exportLoggerFunctions(debugConfig) {
  522. for (var _len = arguments.length, functions = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  523. functions[_key - 1] = arguments[_key];
  524. }
  525. functions.forEach(function (type) {
  526. exportedLogger[type] = debugConfig[type] ? debugConfig[type].bind(debugConfig) : consolePrintFn(type);
  527. });
  528. }
  529. function enableLogs(debugConfig, id) {
  530. // check that console is available
  531. if (typeof console === 'object' && debugConfig === true || typeof debugConfig === 'object') {
  532. exportLoggerFunctions(debugConfig,
  533. // Remove out from list here to hard-disable a log-level
  534. // 'trace',
  535. 'debug', 'log', 'info', 'warn', 'error');
  536. // Some browsers don't allow to use bind on console object anyway
  537. // fallback to default if needed
  538. try {
  539. exportedLogger.log("Debug logs enabled for \"" + id + "\" in hls.js version " + "1.5.11");
  540. } catch (e) {
  541. exportedLogger = fakeLogger;
  542. }
  543. } else {
  544. exportedLogger = fakeLogger;
  545. }
  546. }
  547. var logger = exportedLogger;
  548. var DECIMAL_RESOLUTION_REGEX = /^(\d+)x(\d+)$/;
  549. var ATTR_LIST_REGEX = /(.+?)=(".*?"|.*?)(?:,|$)/g;
  550. // adapted from https://github.com/kanongil/node-m3u8parse/blob/master/attrlist.js
  551. var AttrList = /*#__PURE__*/function () {
  552. function AttrList(attrs) {
  553. if (typeof attrs === 'string') {
  554. attrs = AttrList.parseAttrList(attrs);
  555. }
  556. _extends(this, attrs);
  557. }
  558. var _proto = AttrList.prototype;
  559. _proto.decimalInteger = function decimalInteger(attrName) {
  560. var intValue = parseInt(this[attrName], 10);
  561. if (intValue > Number.MAX_SAFE_INTEGER) {
  562. return Infinity;
  563. }
  564. return intValue;
  565. };
  566. _proto.hexadecimalInteger = function hexadecimalInteger(attrName) {
  567. if (this[attrName]) {
  568. var stringValue = (this[attrName] || '0x').slice(2);
  569. stringValue = (stringValue.length & 1 ? '0' : '') + stringValue;
  570. var value = new Uint8Array(stringValue.length / 2);
  571. for (var i = 0; i < stringValue.length / 2; i++) {
  572. value[i] = parseInt(stringValue.slice(i * 2, i * 2 + 2), 16);
  573. }
  574. return value;
  575. } else {
  576. return null;
  577. }
  578. };
  579. _proto.hexadecimalIntegerAsNumber = function hexadecimalIntegerAsNumber(attrName) {
  580. var intValue = parseInt(this[attrName], 16);
  581. if (intValue > Number.MAX_SAFE_INTEGER) {
  582. return Infinity;
  583. }
  584. return intValue;
  585. };
  586. _proto.decimalFloatingPoint = function decimalFloatingPoint(attrName) {
  587. return parseFloat(this[attrName]);
  588. };
  589. _proto.optionalFloat = function optionalFloat(attrName, defaultValue) {
  590. var value = this[attrName];
  591. return value ? parseFloat(value) : defaultValue;
  592. };
  593. _proto.enumeratedString = function enumeratedString(attrName) {
  594. return this[attrName];
  595. };
  596. _proto.bool = function bool(attrName) {
  597. return this[attrName] === 'YES';
  598. };
  599. _proto.decimalResolution = function decimalResolution(attrName) {
  600. var res = DECIMAL_RESOLUTION_REGEX.exec(this[attrName]);
  601. if (res === null) {
  602. return undefined;
  603. }
  604. return {
  605. width: parseInt(res[1], 10),
  606. height: parseInt(res[2], 10)
  607. };
  608. };
  609. AttrList.parseAttrList = function parseAttrList(input) {
  610. var match;
  611. var attrs = {};
  612. var quote = '"';
  613. ATTR_LIST_REGEX.lastIndex = 0;
  614. while ((match = ATTR_LIST_REGEX.exec(input)) !== null) {
  615. var value = match[2];
  616. if (value.indexOf(quote) === 0 && value.lastIndexOf(quote) === value.length - 1) {
  617. value = value.slice(1, -1);
  618. }
  619. var name = match[1].trim();
  620. attrs[name] = value;
  621. }
  622. return attrs;
  623. };
  624. _createClass(AttrList, [{
  625. key: "clientAttrs",
  626. get: function get() {
  627. return Object.keys(this).filter(function (attr) {
  628. return attr.substring(0, 2) === 'X-';
  629. });
  630. }
  631. }]);
  632. return AttrList;
  633. }();
  634. // Avoid exporting const enum so that these values can be inlined
  635. function isDateRangeCueAttribute(attrName) {
  636. return attrName !== "ID" && attrName !== "CLASS" && attrName !== "START-DATE" && attrName !== "DURATION" && attrName !== "END-DATE" && attrName !== "END-ON-NEXT";
  637. }
  638. function isSCTE35Attribute(attrName) {
  639. return attrName === "SCTE35-OUT" || attrName === "SCTE35-IN";
  640. }
  641. var DateRange = /*#__PURE__*/function () {
  642. function DateRange(dateRangeAttr, dateRangeWithSameId) {
  643. this.attr = void 0;
  644. this._startDate = void 0;
  645. this._endDate = void 0;
  646. this._badValueForSameId = void 0;
  647. if (dateRangeWithSameId) {
  648. var previousAttr = dateRangeWithSameId.attr;
  649. for (var key in previousAttr) {
  650. if (Object.prototype.hasOwnProperty.call(dateRangeAttr, key) && dateRangeAttr[key] !== previousAttr[key]) {
  651. logger.warn("DATERANGE tag attribute: \"" + key + "\" does not match for tags with ID: \"" + dateRangeAttr.ID + "\"");
  652. this._badValueForSameId = key;
  653. break;
  654. }
  655. }
  656. // Merge DateRange tags with the same ID
  657. dateRangeAttr = _extends(new AttrList({}), previousAttr, dateRangeAttr);
  658. }
  659. this.attr = dateRangeAttr;
  660. this._startDate = new Date(dateRangeAttr["START-DATE"]);
  661. if ("END-DATE" in this.attr) {
  662. var endDate = new Date(this.attr["END-DATE"]);
  663. if (isFiniteNumber(endDate.getTime())) {
  664. this._endDate = endDate;
  665. }
  666. }
  667. }
  668. _createClass(DateRange, [{
  669. key: "id",
  670. get: function get() {
  671. return this.attr.ID;
  672. }
  673. }, {
  674. key: "class",
  675. get: function get() {
  676. return this.attr.CLASS;
  677. }
  678. }, {
  679. key: "startDate",
  680. get: function get() {
  681. return this._startDate;
  682. }
  683. }, {
  684. key: "endDate",
  685. get: function get() {
  686. if (this._endDate) {
  687. return this._endDate;
  688. }
  689. var duration = this.duration;
  690. if (duration !== null) {
  691. return new Date(this._startDate.getTime() + duration * 1000);
  692. }
  693. return null;
  694. }
  695. }, {
  696. key: "duration",
  697. get: function get() {
  698. if ("DURATION" in this.attr) {
  699. var duration = this.attr.decimalFloatingPoint("DURATION");
  700. if (isFiniteNumber(duration)) {
  701. return duration;
  702. }
  703. } else if (this._endDate) {
  704. return (this._endDate.getTime() - this._startDate.getTime()) / 1000;
  705. }
  706. return null;
  707. }
  708. }, {
  709. key: "plannedDuration",
  710. get: function get() {
  711. if ("PLANNED-DURATION" in this.attr) {
  712. return this.attr.decimalFloatingPoint("PLANNED-DURATION");
  713. }
  714. return null;
  715. }
  716. }, {
  717. key: "endOnNext",
  718. get: function get() {
  719. return this.attr.bool("END-ON-NEXT");
  720. }
  721. }, {
  722. key: "isValid",
  723. get: function get() {
  724. return !!this.id && !this._badValueForSameId && isFiniteNumber(this.startDate.getTime()) && (this.duration === null || this.duration >= 0) && (!this.endOnNext || !!this.class);
  725. }
  726. }]);
  727. return DateRange;
  728. }();
  729. var LoadStats = function LoadStats() {
  730. this.aborted = false;
  731. this.loaded = 0;
  732. this.retry = 0;
  733. this.total = 0;
  734. this.chunkCount = 0;
  735. this.bwEstimate = 0;
  736. this.loading = {
  737. start: 0,
  738. first: 0,
  739. end: 0
  740. };
  741. this.parsing = {
  742. start: 0,
  743. end: 0
  744. };
  745. this.buffering = {
  746. start: 0,
  747. first: 0,
  748. end: 0
  749. };
  750. };
  751. var ElementaryStreamTypes = {
  752. AUDIO: "audio",
  753. VIDEO: "video",
  754. AUDIOVIDEO: "audiovideo"
  755. };
  756. var BaseSegment = /*#__PURE__*/function () {
  757. function BaseSegment(baseurl) {
  758. var _this$elementaryStrea;
  759. this._byteRange = null;
  760. this._url = null;
  761. // baseurl is the URL to the playlist
  762. this.baseurl = void 0;
  763. // relurl is the portion of the URL that comes from inside the playlist.
  764. this.relurl = void 0;
  765. // Holds the types of data this fragment supports
  766. this.elementaryStreams = (_this$elementaryStrea = {}, _this$elementaryStrea[ElementaryStreamTypes.AUDIO] = null, _this$elementaryStrea[ElementaryStreamTypes.VIDEO] = null, _this$elementaryStrea[ElementaryStreamTypes.AUDIOVIDEO] = null, _this$elementaryStrea);
  767. this.baseurl = baseurl;
  768. }
  769. // setByteRange converts a EXT-X-BYTERANGE attribute into a two element array
  770. var _proto = BaseSegment.prototype;
  771. _proto.setByteRange = function setByteRange(value, previous) {
  772. var params = value.split('@', 2);
  773. var start;
  774. if (params.length === 1) {
  775. start = (previous == null ? void 0 : previous.byteRangeEndOffset) || 0;
  776. } else {
  777. start = parseInt(params[1]);
  778. }
  779. this._byteRange = [start, parseInt(params[0]) + start];
  780. };
  781. _createClass(BaseSegment, [{
  782. key: "byteRange",
  783. get: function get() {
  784. if (!this._byteRange) {
  785. return [];
  786. }
  787. return this._byteRange;
  788. }
  789. }, {
  790. key: "byteRangeStartOffset",
  791. get: function get() {
  792. return this.byteRange[0];
  793. }
  794. }, {
  795. key: "byteRangeEndOffset",
  796. get: function get() {
  797. return this.byteRange[1];
  798. }
  799. }, {
  800. key: "url",
  801. get: function get() {
  802. if (!this._url && this.baseurl && this.relurl) {
  803. this._url = urlToolkitExports.buildAbsoluteURL(this.baseurl, this.relurl, {
  804. alwaysNormalize: true
  805. });
  806. }
  807. return this._url || '';
  808. },
  809. set: function set(value) {
  810. this._url = value;
  811. }
  812. }]);
  813. return BaseSegment;
  814. }();
  815. /**
  816. * Object representing parsed data from an HLS Segment. Found in {@link hls.js#LevelDetails.fragments}.
  817. */
  818. var Fragment = /*#__PURE__*/function (_BaseSegment) {
  819. _inheritsLoose(Fragment, _BaseSegment);
  820. function Fragment(type, baseurl) {
  821. var _this;
  822. _this = _BaseSegment.call(this, baseurl) || this;
  823. _this._decryptdata = null;
  824. _this.rawProgramDateTime = null;
  825. _this.programDateTime = null;
  826. _this.tagList = [];
  827. // EXTINF has to be present for a m3u8 to be considered valid
  828. _this.duration = 0;
  829. // sn notates the sequence number for a segment, and if set to a string can be 'initSegment'
  830. _this.sn = 0;
  831. // levelkeys are the EXT-X-KEY tags that apply to this segment for decryption
  832. // core difference from the private field _decryptdata is the lack of the initialized IV
  833. // _decryptdata will set the IV for this segment based on the segment number in the fragment
  834. _this.levelkeys = void 0;
  835. // A string representing the fragment type
  836. _this.type = void 0;
  837. // A reference to the loader. Set while the fragment is loading, and removed afterwards. Used to abort fragment loading
  838. _this.loader = null;
  839. // A reference to the key loader. Set while the key is loading, and removed afterwards. Used to abort key loading
  840. _this.keyLoader = null;
  841. // The level/track index to which the fragment belongs
  842. _this.level = -1;
  843. // The continuity counter of the fragment
  844. _this.cc = 0;
  845. // The starting Presentation Time Stamp (PTS) of the fragment. Set after transmux complete.
  846. _this.startPTS = void 0;
  847. // The ending Presentation Time Stamp (PTS) of the fragment. Set after transmux complete.
  848. _this.endPTS = void 0;
  849. // The starting Decode Time Stamp (DTS) of the fragment. Set after transmux complete.
  850. _this.startDTS = void 0;
  851. // The ending Decode Time Stamp (DTS) of the fragment. Set after transmux complete.
  852. _this.endDTS = void 0;
  853. // The start time of the fragment, as listed in the manifest. Updated after transmux complete.
  854. _this.start = 0;
  855. // Set by `updateFragPTSDTS` in level-helper
  856. _this.deltaPTS = void 0;
  857. // The maximum starting Presentation Time Stamp (audio/video PTS) of the fragment. Set after transmux complete.
  858. _this.maxStartPTS = void 0;
  859. // The minimum ending Presentation Time Stamp (audio/video PTS) of the fragment. Set after transmux complete.
  860. _this.minEndPTS = void 0;
  861. // Load/parse timing information
  862. _this.stats = new LoadStats();
  863. // Init Segment bytes (unset for media segments)
  864. _this.data = void 0;
  865. // A flag indicating whether the segment was downloaded in order to test bitrate, and was not buffered
  866. _this.bitrateTest = false;
  867. // #EXTINF segment title
  868. _this.title = null;
  869. // The Media Initialization Section for this segment
  870. _this.initSegment = null;
  871. // Fragment is the last fragment in the media playlist
  872. _this.endList = void 0;
  873. // Fragment is marked by an EXT-X-GAP tag indicating that it does not contain media data and should not be loaded
  874. _this.gap = void 0;
  875. // Deprecated
  876. _this.urlId = 0;
  877. _this.type = type;
  878. return _this;
  879. }
  880. var _proto2 = Fragment.prototype;
  881. _proto2.setKeyFormat = function setKeyFormat(keyFormat) {
  882. if (this.levelkeys) {
  883. var _key = this.levelkeys[keyFormat];
  884. if (_key && !this._decryptdata) {
  885. this._decryptdata = _key.getDecryptData(this.sn);
  886. }
  887. }
  888. };
  889. _proto2.abortRequests = function abortRequests() {
  890. var _this$loader, _this$keyLoader;
  891. (_this$loader = this.loader) == null ? void 0 : _this$loader.abort();
  892. (_this$keyLoader = this.keyLoader) == null ? void 0 : _this$keyLoader.abort();
  893. };
  894. _proto2.setElementaryStreamInfo = function setElementaryStreamInfo(type, startPTS, endPTS, startDTS, endDTS, partial) {
  895. if (partial === void 0) {
  896. partial = false;
  897. }
  898. var elementaryStreams = this.elementaryStreams;
  899. var info = elementaryStreams[type];
  900. if (!info) {
  901. elementaryStreams[type] = {
  902. startPTS: startPTS,
  903. endPTS: endPTS,
  904. startDTS: startDTS,
  905. endDTS: endDTS,
  906. partial: partial
  907. };
  908. return;
  909. }
  910. info.startPTS = Math.min(info.startPTS, startPTS);
  911. info.endPTS = Math.max(info.endPTS, endPTS);
  912. info.startDTS = Math.min(info.startDTS, startDTS);
  913. info.endDTS = Math.max(info.endDTS, endDTS);
  914. };
  915. _proto2.clearElementaryStreamInfo = function clearElementaryStreamInfo() {
  916. var elementaryStreams = this.elementaryStreams;
  917. elementaryStreams[ElementaryStreamTypes.AUDIO] = null;
  918. elementaryStreams[ElementaryStreamTypes.VIDEO] = null;
  919. elementaryStreams[ElementaryStreamTypes.AUDIOVIDEO] = null;
  920. };
  921. _createClass(Fragment, [{
  922. key: "decryptdata",
  923. get: function get() {
  924. var levelkeys = this.levelkeys;
  925. if (!levelkeys && !this._decryptdata) {
  926. return null;
  927. }
  928. if (!this._decryptdata && this.levelkeys && !this.levelkeys.NONE) {
  929. var _key2 = this.levelkeys.identity;
  930. if (_key2) {
  931. this._decryptdata = _key2.getDecryptData(this.sn);
  932. } else {
  933. var keyFormats = Object.keys(this.levelkeys);
  934. if (keyFormats.length === 1) {
  935. return this._decryptdata = this.levelkeys[keyFormats[0]].getDecryptData(this.sn);
  936. }
  937. }
  938. }
  939. return this._decryptdata;
  940. }
  941. }, {
  942. key: "end",
  943. get: function get() {
  944. return this.start + this.duration;
  945. }
  946. }, {
  947. key: "endProgramDateTime",
  948. get: function get() {
  949. if (this.programDateTime === null) {
  950. return null;
  951. }
  952. if (!isFiniteNumber(this.programDateTime)) {
  953. return null;
  954. }
  955. var duration = !isFiniteNumber(this.duration) ? 0 : this.duration;
  956. return this.programDateTime + duration * 1000;
  957. }
  958. }, {
  959. key: "encrypted",
  960. get: function get() {
  961. var _this$_decryptdata;
  962. // At the m3u8-parser level we need to add support for manifest signalled keyformats
  963. // when we want the fragment to start reporting that it is encrypted.
  964. // Currently, keyFormat will only be set for identity keys
  965. if ((_this$_decryptdata = this._decryptdata) != null && _this$_decryptdata.encrypted) {
  966. return true;
  967. } else if (this.levelkeys) {
  968. var keyFormats = Object.keys(this.levelkeys);
  969. var len = keyFormats.length;
  970. if (len > 1 || len === 1 && this.levelkeys[keyFormats[0]].encrypted) {
  971. return true;
  972. }
  973. }
  974. return false;
  975. }
  976. }]);
  977. return Fragment;
  978. }(BaseSegment);
  979. /**
  980. * Object representing parsed data from an HLS Partial Segment. Found in {@link hls.js#LevelDetails.partList}.
  981. */
  982. var Part = /*#__PURE__*/function (_BaseSegment2) {
  983. _inheritsLoose(Part, _BaseSegment2);
  984. function Part(partAttrs, frag, baseurl, index, previous) {
  985. var _this2;
  986. _this2 = _BaseSegment2.call(this, baseurl) || this;
  987. _this2.fragOffset = 0;
  988. _this2.duration = 0;
  989. _this2.gap = false;
  990. _this2.independent = false;
  991. _this2.relurl = void 0;
  992. _this2.fragment = void 0;
  993. _this2.index = void 0;
  994. _this2.stats = new LoadStats();
  995. _this2.duration = partAttrs.decimalFloatingPoint('DURATION');
  996. _this2.gap = partAttrs.bool('GAP');
  997. _this2.independent = partAttrs.bool('INDEPENDENT');
  998. _this2.relurl = partAttrs.enumeratedString('URI');
  999. _this2.fragment = frag;
  1000. _this2.index = index;
  1001. var byteRange = partAttrs.enumeratedString('BYTERANGE');
  1002. if (byteRange) {
  1003. _this2.setByteRange(byteRange, previous);
  1004. }
  1005. if (previous) {
  1006. _this2.fragOffset = previous.fragOffset + previous.duration;
  1007. }
  1008. return _this2;
  1009. }
  1010. _createClass(Part, [{
  1011. key: "start",
  1012. get: function get() {
  1013. return this.fragment.start + this.fragOffset;
  1014. }
  1015. }, {
  1016. key: "end",
  1017. get: function get() {
  1018. return this.start + this.duration;
  1019. }
  1020. }, {
  1021. key: "loaded",
  1022. get: function get() {
  1023. var elementaryStreams = this.elementaryStreams;
  1024. return !!(elementaryStreams.audio || elementaryStreams.video || elementaryStreams.audiovideo);
  1025. }
  1026. }]);
  1027. return Part;
  1028. }(BaseSegment);
  1029. var DEFAULT_TARGET_DURATION = 10;
  1030. /**
  1031. * Object representing parsed data from an HLS Media Playlist. Found in {@link hls.js#Level.details}.
  1032. */
  1033. var LevelDetails = /*#__PURE__*/function () {
  1034. function LevelDetails(baseUrl) {
  1035. this.PTSKnown = false;
  1036. this.alignedSliding = false;
  1037. this.averagetargetduration = void 0;
  1038. this.endCC = 0;
  1039. this.endSN = 0;
  1040. this.fragments = void 0;
  1041. this.fragmentHint = void 0;
  1042. this.partList = null;
  1043. this.dateRanges = void 0;
  1044. this.live = true;
  1045. this.ageHeader = 0;
  1046. this.advancedDateTime = void 0;
  1047. this.updated = true;
  1048. this.advanced = true;
  1049. this.availabilityDelay = void 0;
  1050. // Manifest reload synchronization
  1051. this.misses = 0;
  1052. this.startCC = 0;
  1053. this.startSN = 0;
  1054. this.startTimeOffset = null;
  1055. this.targetduration = 0;
  1056. this.totalduration = 0;
  1057. this.type = null;
  1058. this.url = void 0;
  1059. this.m3u8 = '';
  1060. this.version = null;
  1061. this.canBlockReload = false;
  1062. this.canSkipUntil = 0;
  1063. this.canSkipDateRanges = false;
  1064. this.skippedSegments = 0;
  1065. this.recentlyRemovedDateranges = void 0;
  1066. this.partHoldBack = 0;
  1067. this.holdBack = 0;
  1068. this.partTarget = 0;
  1069. this.preloadHint = void 0;
  1070. this.renditionReports = void 0;
  1071. this.tuneInGoal = 0;
  1072. this.deltaUpdateFailed = void 0;
  1073. this.driftStartTime = 0;
  1074. this.driftEndTime = 0;
  1075. this.driftStart = 0;
  1076. this.driftEnd = 0;
  1077. this.encryptedFragments = void 0;
  1078. this.playlistParsingError = null;
  1079. this.variableList = null;
  1080. this.hasVariableRefs = false;
  1081. this.fragments = [];
  1082. this.encryptedFragments = [];
  1083. this.dateRanges = {};
  1084. this.url = baseUrl;
  1085. }
  1086. var _proto = LevelDetails.prototype;
  1087. _proto.reloaded = function reloaded(previous) {
  1088. if (!previous) {
  1089. this.advanced = true;
  1090. this.updated = true;
  1091. return;
  1092. }
  1093. var partSnDiff = this.lastPartSn - previous.lastPartSn;
  1094. var partIndexDiff = this.lastPartIndex - previous.lastPartIndex;
  1095. this.updated = this.endSN !== previous.endSN || !!partIndexDiff || !!partSnDiff || !this.live;
  1096. this.advanced = this.endSN > previous.endSN || partSnDiff > 0 || partSnDiff === 0 && partIndexDiff > 0;
  1097. if (this.updated || this.advanced) {
  1098. this.misses = Math.floor(previous.misses * 0.6);
  1099. } else {
  1100. this.misses = previous.misses + 1;
  1101. }
  1102. this.availabilityDelay = previous.availabilityDelay;
  1103. };
  1104. _createClass(LevelDetails, [{
  1105. key: "hasProgramDateTime",
  1106. get: function get() {
  1107. if (this.fragments.length) {
  1108. return isFiniteNumber(this.fragments[this.fragments.length - 1].programDateTime);
  1109. }
  1110. return false;
  1111. }
  1112. }, {
  1113. key: "levelTargetDuration",
  1114. get: function get() {
  1115. return this.averagetargetduration || this.targetduration || DEFAULT_TARGET_DURATION;
  1116. }
  1117. }, {
  1118. key: "drift",
  1119. get: function get() {
  1120. var runTime = this.driftEndTime - this.driftStartTime;
  1121. if (runTime > 0) {
  1122. var runDuration = this.driftEnd - this.driftStart;
  1123. return runDuration * 1000 / runTime;
  1124. }
  1125. return 1;
  1126. }
  1127. }, {
  1128. key: "edge",
  1129. get: function get() {
  1130. return this.partEnd || this.fragmentEnd;
  1131. }
  1132. }, {
  1133. key: "partEnd",
  1134. get: function get() {
  1135. var _this$partList;
  1136. if ((_this$partList = this.partList) != null && _this$partList.length) {
  1137. return this.partList[this.partList.length - 1].end;
  1138. }
  1139. return this.fragmentEnd;
  1140. }
  1141. }, {
  1142. key: "fragmentEnd",
  1143. get: function get() {
  1144. var _this$fragments;
  1145. if ((_this$fragments = this.fragments) != null && _this$fragments.length) {
  1146. return this.fragments[this.fragments.length - 1].end;
  1147. }
  1148. return 0;
  1149. }
  1150. }, {
  1151. key: "age",
  1152. get: function get() {
  1153. if (this.advancedDateTime) {
  1154. return Math.max(Date.now() - this.advancedDateTime, 0) / 1000;
  1155. }
  1156. return 0;
  1157. }
  1158. }, {
  1159. key: "lastPartIndex",
  1160. get: function get() {
  1161. var _this$partList2;
  1162. if ((_this$partList2 = this.partList) != null && _this$partList2.length) {
  1163. return this.partList[this.partList.length - 1].index;
  1164. }
  1165. return -1;
  1166. }
  1167. }, {
  1168. key: "lastPartSn",
  1169. get: function get() {
  1170. var _this$partList3;
  1171. if ((_this$partList3 = this.partList) != null && _this$partList3.length) {
  1172. return this.partList[this.partList.length - 1].fragment.sn;
  1173. }
  1174. return this.endSN;
  1175. }
  1176. }]);
  1177. return LevelDetails;
  1178. }();
  1179. function base64Decode(base64encodedStr) {
  1180. return Uint8Array.from(atob(base64encodedStr), function (c) {
  1181. return c.charCodeAt(0);
  1182. });
  1183. }
  1184. function getKeyIdBytes(str) {
  1185. var keyIdbytes = strToUtf8array(str).subarray(0, 16);
  1186. var paddedkeyIdbytes = new Uint8Array(16);
  1187. paddedkeyIdbytes.set(keyIdbytes, 16 - keyIdbytes.length);
  1188. return paddedkeyIdbytes;
  1189. }
  1190. function changeEndianness(keyId) {
  1191. var swap = function swap(array, from, to) {
  1192. var cur = array[from];
  1193. array[from] = array[to];
  1194. array[to] = cur;
  1195. };
  1196. swap(keyId, 0, 3);
  1197. swap(keyId, 1, 2);
  1198. swap(keyId, 4, 5);
  1199. swap(keyId, 6, 7);
  1200. }
  1201. function convertDataUriToArrayBytes(uri) {
  1202. // data:[<media type][;attribute=value][;base64],<data>
  1203. var colonsplit = uri.split(':');
  1204. var keydata = null;
  1205. if (colonsplit[0] === 'data' && colonsplit.length === 2) {
  1206. var semicolonsplit = colonsplit[1].split(';');
  1207. var commasplit = semicolonsplit[semicolonsplit.length - 1].split(',');
  1208. if (commasplit.length === 2) {
  1209. var isbase64 = commasplit[0] === 'base64';
  1210. var data = commasplit[1];
  1211. if (isbase64) {
  1212. semicolonsplit.splice(-1, 1); // remove from processing
  1213. keydata = base64Decode(data);
  1214. } else {
  1215. keydata = getKeyIdBytes(data);
  1216. }
  1217. }
  1218. }
  1219. return keydata;
  1220. }
  1221. function strToUtf8array(str) {
  1222. return Uint8Array.from(unescape(encodeURIComponent(str)), function (c) {
  1223. return c.charCodeAt(0);
  1224. });
  1225. }
  1226. /** returns `undefined` is `self` is missing, e.g. in node */
  1227. var optionalSelf = typeof self !== 'undefined' ? self : undefined;
  1228. /**
  1229. * @see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/requestMediaKeySystemAccess
  1230. */
  1231. var KeySystems = {
  1232. CLEARKEY: "org.w3.clearkey",
  1233. FAIRPLAY: "com.apple.fps",
  1234. PLAYREADY: "com.microsoft.playready",
  1235. WIDEVINE: "com.widevine.alpha"
  1236. };
  1237. // Playlist #EXT-X-KEY KEYFORMAT values
  1238. var KeySystemFormats = {
  1239. CLEARKEY: "org.w3.clearkey",
  1240. FAIRPLAY: "com.apple.streamingkeydelivery",
  1241. PLAYREADY: "com.microsoft.playready",
  1242. WIDEVINE: "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"
  1243. };
  1244. function keySystemFormatToKeySystemDomain(format) {
  1245. switch (format) {
  1246. case KeySystemFormats.FAIRPLAY:
  1247. return KeySystems.FAIRPLAY;
  1248. case KeySystemFormats.PLAYREADY:
  1249. return KeySystems.PLAYREADY;
  1250. case KeySystemFormats.WIDEVINE:
  1251. return KeySystems.WIDEVINE;
  1252. case KeySystemFormats.CLEARKEY:
  1253. return KeySystems.CLEARKEY;
  1254. }
  1255. }
  1256. // System IDs for which we can extract a key ID from "encrypted" event PSSH
  1257. var KeySystemIds = {
  1258. WIDEVINE: "edef8ba979d64acea3c827dcd51d21ed"
  1259. };
  1260. function keySystemIdToKeySystemDomain(systemId) {
  1261. if (systemId === KeySystemIds.WIDEVINE) {
  1262. return KeySystems.WIDEVINE;
  1263. // } else if (systemId === KeySystemIds.PLAYREADY) {
  1264. // return KeySystems.PLAYREADY;
  1265. // } else if (systemId === KeySystemIds.CENC || systemId === KeySystemIds.CLEARKEY) {
  1266. // return KeySystems.CLEARKEY;
  1267. }
  1268. }
  1269. function keySystemDomainToKeySystemFormat(keySystem) {
  1270. switch (keySystem) {
  1271. case KeySystems.FAIRPLAY:
  1272. return KeySystemFormats.FAIRPLAY;
  1273. case KeySystems.PLAYREADY:
  1274. return KeySystemFormats.PLAYREADY;
  1275. case KeySystems.WIDEVINE:
  1276. return KeySystemFormats.WIDEVINE;
  1277. case KeySystems.CLEARKEY:
  1278. return KeySystemFormats.CLEARKEY;
  1279. }
  1280. }
  1281. function getKeySystemsForConfig(config) {
  1282. var drmSystems = config.drmSystems,
  1283. widevineLicenseUrl = config.widevineLicenseUrl;
  1284. var keySystemsToAttempt = drmSystems ? [KeySystems.FAIRPLAY, KeySystems.WIDEVINE, KeySystems.PLAYREADY, KeySystems.CLEARKEY].filter(function (keySystem) {
  1285. return !!drmSystems[keySystem];
  1286. }) : [];
  1287. if (!keySystemsToAttempt[KeySystems.WIDEVINE] && widevineLicenseUrl) {
  1288. keySystemsToAttempt.push(KeySystems.WIDEVINE);
  1289. }
  1290. return keySystemsToAttempt;
  1291. }
  1292. var requestMediaKeySystemAccess = function (_optionalSelf$navigat) {
  1293. if (optionalSelf != null && (_optionalSelf$navigat = optionalSelf.navigator) != null && _optionalSelf$navigat.requestMediaKeySystemAccess) {
  1294. return self.navigator.requestMediaKeySystemAccess.bind(self.navigator);
  1295. } else {
  1296. return null;
  1297. }
  1298. }();
  1299. /**
  1300. * @see https://developer.mozilla.org/en-US/docs/Web/API/MediaKeySystemConfiguration
  1301. */
  1302. function getSupportedMediaKeySystemConfigurations(keySystem, audioCodecs, videoCodecs, drmSystemOptions) {
  1303. var initDataTypes;
  1304. switch (keySystem) {
  1305. case KeySystems.FAIRPLAY:
  1306. initDataTypes = ['cenc', 'sinf'];
  1307. break;
  1308. case KeySystems.WIDEVINE:
  1309. case KeySystems.PLAYREADY:
  1310. initDataTypes = ['cenc'];
  1311. break;
  1312. case KeySystems.CLEARKEY:
  1313. initDataTypes = ['cenc', 'keyids'];
  1314. break;
  1315. default:
  1316. throw new Error("Unknown key-system: " + keySystem);
  1317. }
  1318. return createMediaKeySystemConfigurations(initDataTypes, audioCodecs, videoCodecs, drmSystemOptions);
  1319. }
  1320. function createMediaKeySystemConfigurations(initDataTypes, audioCodecs, videoCodecs, drmSystemOptions) {
  1321. var baseConfig = {
  1322. initDataTypes: initDataTypes,
  1323. persistentState: drmSystemOptions.persistentState || 'optional',
  1324. distinctiveIdentifier: drmSystemOptions.distinctiveIdentifier || 'optional',
  1325. sessionTypes: drmSystemOptions.sessionTypes || [drmSystemOptions.sessionType || 'temporary'],
  1326. audioCapabilities: audioCodecs.map(function (codec) {
  1327. return {
  1328. contentType: "audio/mp4; codecs=\"" + codec + "\"",
  1329. robustness: drmSystemOptions.audioRobustness || '',
  1330. encryptionScheme: drmSystemOptions.audioEncryptionScheme || null
  1331. };
  1332. }),
  1333. videoCapabilities: videoCodecs.map(function (codec) {
  1334. return {
  1335. contentType: "video/mp4; codecs=\"" + codec + "\"",
  1336. robustness: drmSystemOptions.videoRobustness || '',
  1337. encryptionScheme: drmSystemOptions.videoEncryptionScheme || null
  1338. };
  1339. })
  1340. };
  1341. return [baseConfig];
  1342. }
  1343. function sliceUint8(array, start, end) {
  1344. // @ts-expect-error This polyfills IE11 usage of Uint8Array slice.
  1345. // It always exists in the TypeScript definition so fails, but it fails at runtime on IE11.
  1346. return Uint8Array.prototype.slice ? array.slice(start, end) : new Uint8Array(Array.prototype.slice.call(array, start, end));
  1347. }
  1348. // breaking up those two types in order to clarify what is happening in the decoding path.
  1349. /**
  1350. * Returns true if an ID3 header can be found at offset in data
  1351. * @param data - The data to search
  1352. * @param offset - The offset at which to start searching
  1353. */
  1354. var isHeader$2 = function isHeader(data, offset) {
  1355. /*
  1356. * http://id3.org/id3v2.3.0
  1357. * [0] = 'I'
  1358. * [1] = 'D'
  1359. * [2] = '3'
  1360. * [3,4] = {Version}
  1361. * [5] = {Flags}
  1362. * [6-9] = {ID3 Size}
  1363. *
  1364. * An ID3v2 tag can be detected with the following pattern:
  1365. * $49 44 33 yy yy xx zz zz zz zz
  1366. * Where yy is less than $FF, xx is the 'flags' byte and zz is less than $80
  1367. */
  1368. if (offset + 10 <= data.length) {
  1369. // look for 'ID3' identifier
  1370. if (data[offset] === 0x49 && data[offset + 1] === 0x44 && data[offset + 2] === 0x33) {
  1371. // check version is within range
  1372. if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
  1373. // check size is within range
  1374. if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) {
  1375. return true;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. return false;
  1381. };
  1382. /**
  1383. * Returns true if an ID3 footer can be found at offset in data
  1384. * @param data - The data to search
  1385. * @param offset - The offset at which to start searching
  1386. */
  1387. var isFooter = function isFooter(data, offset) {
  1388. /*
  1389. * The footer is a copy of the header, but with a different identifier
  1390. */
  1391. if (offset + 10 <= data.length) {
  1392. // look for '3DI' identifier
  1393. if (data[offset] === 0x33 && data[offset + 1] === 0x44 && data[offset + 2] === 0x49) {
  1394. // check version is within range
  1395. if (data[offset + 3] < 0xff && data[offset + 4] < 0xff) {
  1396. // check size is within range
  1397. if (data[offset + 6] < 0x80 && data[offset + 7] < 0x80 && data[offset + 8] < 0x80 && data[offset + 9] < 0x80) {
  1398. return true;
  1399. }
  1400. }
  1401. }
  1402. }
  1403. return false;
  1404. };
  1405. /**
  1406. * Returns any adjacent ID3 tags found in data starting at offset, as one block of data
  1407. * @param data - The data to search in
  1408. * @param offset - The offset at which to start searching
  1409. * @returns the block of data containing any ID3 tags found
  1410. * or *undefined* if no header is found at the starting offset
  1411. */
  1412. var getID3Data = function getID3Data(data, offset) {
  1413. var front = offset;
  1414. var length = 0;
  1415. while (isHeader$2(data, offset)) {
  1416. // ID3 header is 10 bytes
  1417. length += 10;
  1418. var size = readSize(data, offset + 6);
  1419. length += size;
  1420. if (isFooter(data, offset + 10)) {
  1421. // ID3 footer is 10 bytes
  1422. length += 10;
  1423. }
  1424. offset += length;
  1425. }
  1426. if (length > 0) {
  1427. return data.subarray(front, front + length);
  1428. }
  1429. return undefined;
  1430. };
  1431. var readSize = function readSize(data, offset) {
  1432. var size = 0;
  1433. size = (data[offset] & 0x7f) << 21;
  1434. size |= (data[offset + 1] & 0x7f) << 14;
  1435. size |= (data[offset + 2] & 0x7f) << 7;
  1436. size |= data[offset + 3] & 0x7f;
  1437. return size;
  1438. };
  1439. var canParse$2 = function canParse(data, offset) {
  1440. return isHeader$2(data, offset) && readSize(data, offset + 6) + 10 <= data.length - offset;
  1441. };
  1442. /**
  1443. * Searches for the Elementary Stream timestamp found in the ID3 data chunk
  1444. * @param data - Block of data containing one or more ID3 tags
  1445. */
  1446. var getTimeStamp = function getTimeStamp(data) {
  1447. var frames = getID3Frames(data);
  1448. for (var i = 0; i < frames.length; i++) {
  1449. var frame = frames[i];
  1450. if (isTimeStampFrame(frame)) {
  1451. return readTimeStamp(frame);
  1452. }
  1453. }
  1454. return undefined;
  1455. };
  1456. /**
  1457. * Returns true if the ID3 frame is an Elementary Stream timestamp frame
  1458. */
  1459. var isTimeStampFrame = function isTimeStampFrame(frame) {
  1460. return frame && frame.key === 'PRIV' && frame.info === 'com.apple.streaming.transportStreamTimestamp';
  1461. };
  1462. var getFrameData = function getFrameData(data) {
  1463. /*
  1464. Frame ID $xx xx xx xx (four characters)
  1465. Size $xx xx xx xx
  1466. Flags $xx xx
  1467. */
  1468. var type = String.fromCharCode(data[0], data[1], data[2], data[3]);
  1469. var size = readSize(data, 4);
  1470. // skip frame id, size, and flags
  1471. var offset = 10;
  1472. return {
  1473. type: type,
  1474. size: size,
  1475. data: data.subarray(offset, offset + size)
  1476. };
  1477. };
  1478. /**
  1479. * Returns an array of ID3 frames found in all the ID3 tags in the id3Data
  1480. * @param id3Data - The ID3 data containing one or more ID3 tags
  1481. */
  1482. var getID3Frames = function getID3Frames(id3Data) {
  1483. var offset = 0;
  1484. var frames = [];
  1485. while (isHeader$2(id3Data, offset)) {
  1486. var size = readSize(id3Data, offset + 6);
  1487. // skip past ID3 header
  1488. offset += 10;
  1489. var end = offset + size;
  1490. // loop through frames in the ID3 tag
  1491. while (offset + 8 < end) {
  1492. var frameData = getFrameData(id3Data.subarray(offset));
  1493. var frame = decodeFrame(frameData);
  1494. if (frame) {
  1495. frames.push(frame);
  1496. }
  1497. // skip frame header and frame data
  1498. offset += frameData.size + 10;
  1499. }
  1500. if (isFooter(id3Data, offset)) {
  1501. offset += 10;
  1502. }
  1503. }
  1504. return frames;
  1505. };
  1506. var decodeFrame = function decodeFrame(frame) {
  1507. if (frame.type === 'PRIV') {
  1508. return decodePrivFrame(frame);
  1509. } else if (frame.type[0] === 'W') {
  1510. return decodeURLFrame(frame);
  1511. }
  1512. return decodeTextFrame(frame);
  1513. };
  1514. var decodePrivFrame = function decodePrivFrame(frame) {
  1515. /*
  1516. Format: <text string>\0<binary data>
  1517. */
  1518. if (frame.size < 2) {
  1519. return undefined;
  1520. }
  1521. var owner = utf8ArrayToStr(frame.data, true);
  1522. var privateData = new Uint8Array(frame.data.subarray(owner.length + 1));
  1523. return {
  1524. key: frame.type,
  1525. info: owner,
  1526. data: privateData.buffer
  1527. };
  1528. };
  1529. var decodeTextFrame = function decodeTextFrame(frame) {
  1530. if (frame.size < 2) {
  1531. return undefined;
  1532. }
  1533. if (frame.type === 'TXXX') {
  1534. /*
  1535. Format:
  1536. [0] = {Text Encoding}
  1537. [1-?] = {Description}\0{Value}
  1538. */
  1539. var index = 1;
  1540. var description = utf8ArrayToStr(frame.data.subarray(index), true);
  1541. index += description.length + 1;
  1542. var value = utf8ArrayToStr(frame.data.subarray(index));
  1543. return {
  1544. key: frame.type,
  1545. info: description,
  1546. data: value
  1547. };
  1548. }
  1549. /*
  1550. Format:
  1551. [0] = {Text Encoding}
  1552. [1-?] = {Value}
  1553. */
  1554. var text = utf8ArrayToStr(frame.data.subarray(1));
  1555. return {
  1556. key: frame.type,
  1557. data: text
  1558. };
  1559. };
  1560. var decodeURLFrame = function decodeURLFrame(frame) {
  1561. if (frame.type === 'WXXX') {
  1562. /*
  1563. Format:
  1564. [0] = {Text Encoding}
  1565. [1-?] = {Description}\0{URL}
  1566. */
  1567. if (frame.size < 2) {
  1568. return undefined;
  1569. }
  1570. var index = 1;
  1571. var description = utf8ArrayToStr(frame.data.subarray(index), true);
  1572. index += description.length + 1;
  1573. var value = utf8ArrayToStr(frame.data.subarray(index));
  1574. return {
  1575. key: frame.type,
  1576. info: description,
  1577. data: value
  1578. };
  1579. }
  1580. /*
  1581. Format:
  1582. [0-?] = {URL}
  1583. */
  1584. var url = utf8ArrayToStr(frame.data);
  1585. return {
  1586. key: frame.type,
  1587. data: url
  1588. };
  1589. };
  1590. var readTimeStamp = function readTimeStamp(timeStampFrame) {
  1591. if (timeStampFrame.data.byteLength === 8) {
  1592. var data = new Uint8Array(timeStampFrame.data);
  1593. // timestamp is 33 bit expressed as a big-endian eight-octet number,
  1594. // with the upper 31 bits set to zero.
  1595. var pts33Bit = data[3] & 0x1;
  1596. var timestamp = (data[4] << 23) + (data[5] << 15) + (data[6] << 7) + data[7];
  1597. timestamp /= 45;
  1598. if (pts33Bit) {
  1599. timestamp += 47721858.84;
  1600. } // 2^32 / 90
  1601. return Math.round(timestamp);
  1602. }
  1603. return undefined;
  1604. };
  1605. // http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197
  1606. // http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
  1607. /* utf.js - UTF-8 <=> UTF-16 convertion
  1608. *
  1609. * Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
  1610. * Version: 1.0
  1611. * LastModified: Dec 25 1999
  1612. * This library is free. You can redistribute it and/or modify it.
  1613. */
  1614. var utf8ArrayToStr = function utf8ArrayToStr(array, exitOnNull) {
  1615. if (exitOnNull === void 0) {
  1616. exitOnNull = false;
  1617. }
  1618. var decoder = getTextDecoder();
  1619. if (decoder) {
  1620. var decoded = decoder.decode(array);
  1621. if (exitOnNull) {
  1622. // grab up to the first null
  1623. var idx = decoded.indexOf('\0');
  1624. return idx !== -1 ? decoded.substring(0, idx) : decoded;
  1625. }
  1626. // remove any null characters
  1627. return decoded.replace(/\0/g, '');
  1628. }
  1629. var len = array.length;
  1630. var c;
  1631. var char2;
  1632. var char3;
  1633. var out = '';
  1634. var i = 0;
  1635. while (i < len) {
  1636. c = array[i++];
  1637. if (c === 0x00 && exitOnNull) {
  1638. return out;
  1639. } else if (c === 0x00 || c === 0x03) {
  1640. // If the character is 3 (END_OF_TEXT) or 0 (NULL) then skip it
  1641. continue;
  1642. }
  1643. switch (c >> 4) {
  1644. case 0:
  1645. case 1:
  1646. case 2:
  1647. case 3:
  1648. case 4:
  1649. case 5:
  1650. case 6:
  1651. case 7:
  1652. // 0xxxxxxx
  1653. out += String.fromCharCode(c);
  1654. break;
  1655. case 12:
  1656. case 13:
  1657. // 110x xxxx 10xx xxxx
  1658. char2 = array[i++];
  1659. out += String.fromCharCode((c & 0x1f) << 6 | char2 & 0x3f);
  1660. break;
  1661. case 14:
  1662. // 1110 xxxx 10xx xxxx 10xx xxxx
  1663. char2 = array[i++];
  1664. char3 = array[i++];
  1665. out += String.fromCharCode((c & 0x0f) << 12 | (char2 & 0x3f) << 6 | (char3 & 0x3f) << 0);
  1666. break;
  1667. }
  1668. }
  1669. return out;
  1670. };
  1671. var decoder;
  1672. function getTextDecoder() {
  1673. // On Play Station 4, TextDecoder is defined but partially implemented.
  1674. // Manual decoding option is preferable
  1675. if (navigator.userAgent.includes('PlayStation 4')) {
  1676. return;
  1677. }
  1678. if (!decoder && typeof self.TextDecoder !== 'undefined') {
  1679. decoder = new self.TextDecoder('utf-8');
  1680. }
  1681. return decoder;
  1682. }
  1683. /**
  1684. * hex dump helper class
  1685. */
  1686. var Hex = {
  1687. hexDump: function hexDump(array) {
  1688. var str = '';
  1689. for (var i = 0; i < array.length; i++) {
  1690. var h = array[i].toString(16);
  1691. if (h.length < 2) {
  1692. h = '0' + h;
  1693. }
  1694. str += h;
  1695. }
  1696. return str;
  1697. }
  1698. };
  1699. var UINT32_MAX$1 = Math.pow(2, 32) - 1;
  1700. var push = [].push;
  1701. // We are using fixed track IDs for driving the MP4 remuxer
  1702. // instead of following the TS PIDs.
  1703. // There is no reason not to do this and some browsers/SourceBuffer-demuxers
  1704. // may not like if there are TrackID "switches"
  1705. // See https://github.com/video-dev/hls.js/issues/1331
  1706. // Here we are mapping our internal track types to constant MP4 track IDs
  1707. // With MSE currently one can only have one track of each, and we are muxing
  1708. // whatever video/audio rendition in them.
  1709. var RemuxerTrackIdConfig = {
  1710. video: 1,
  1711. audio: 2,
  1712. id3: 3,
  1713. text: 4
  1714. };
  1715. function bin2str(data) {
  1716. return String.fromCharCode.apply(null, data);
  1717. }
  1718. function readUint16(buffer, offset) {
  1719. var val = buffer[offset] << 8 | buffer[offset + 1];
  1720. return val < 0 ? 65536 + val : val;
  1721. }
  1722. function readUint32(buffer, offset) {
  1723. var val = readSint32(buffer, offset);
  1724. return val < 0 ? 4294967296 + val : val;
  1725. }
  1726. function readUint64(buffer, offset) {
  1727. var result = readUint32(buffer, offset);
  1728. result *= Math.pow(2, 32);
  1729. result += readUint32(buffer, offset + 4);
  1730. return result;
  1731. }
  1732. function readSint32(buffer, offset) {
  1733. return buffer[offset] << 24 | buffer[offset + 1] << 16 | buffer[offset + 2] << 8 | buffer[offset + 3];
  1734. }
  1735. function writeUint32(buffer, offset, value) {
  1736. buffer[offset] = value >> 24;
  1737. buffer[offset + 1] = value >> 16 & 0xff;
  1738. buffer[offset + 2] = value >> 8 & 0xff;
  1739. buffer[offset + 3] = value & 0xff;
  1740. }
  1741. // Find "moof" box
  1742. function hasMoofData(data) {
  1743. var end = data.byteLength;
  1744. for (var i = 0; i < end;) {
  1745. var size = readUint32(data, i);
  1746. if (size > 8 && data[i + 4] === 0x6d && data[i + 5] === 0x6f && data[i + 6] === 0x6f && data[i + 7] === 0x66) {
  1747. return true;
  1748. }
  1749. i = size > 1 ? i + size : end;
  1750. }
  1751. return false;
  1752. }
  1753. // Find the data for a box specified by its path
  1754. function findBox(data, path) {
  1755. var results = [];
  1756. if (!path.length) {
  1757. // short-circuit the search for empty paths
  1758. return results;
  1759. }
  1760. var end = data.byteLength;
  1761. for (var i = 0; i < end;) {
  1762. var size = readUint32(data, i);
  1763. var type = bin2str(data.subarray(i + 4, i + 8));
  1764. var endbox = size > 1 ? i + size : end;
  1765. if (type === path[0]) {
  1766. if (path.length === 1) {
  1767. // this is the end of the path and we've found the box we were
  1768. // looking for
  1769. results.push(data.subarray(i + 8, endbox));
  1770. } else {
  1771. // recursively search for the next box along the path
  1772. var subresults = findBox(data.subarray(i + 8, endbox), path.slice(1));
  1773. if (subresults.length) {
  1774. push.apply(results, subresults);
  1775. }
  1776. }
  1777. }
  1778. i = endbox;
  1779. }
  1780. // we've finished searching all of data
  1781. return results;
  1782. }
  1783. function parseSegmentIndex(sidx) {
  1784. var references = [];
  1785. var version = sidx[0];
  1786. // set initial offset, we skip the reference ID (not needed)
  1787. var index = 8;
  1788. var timescale = readUint32(sidx, index);
  1789. index += 4;
  1790. var earliestPresentationTime = 0;
  1791. var firstOffset = 0;
  1792. if (version === 0) {
  1793. earliestPresentationTime = readUint32(sidx, index);
  1794. firstOffset = readUint32(sidx, index + 4);
  1795. index += 8;
  1796. } else {
  1797. earliestPresentationTime = readUint64(sidx, index);
  1798. firstOffset = readUint64(sidx, index + 8);
  1799. index += 16;
  1800. }
  1801. // skip reserved
  1802. index += 2;
  1803. var startByte = sidx.length + firstOffset;
  1804. var referencesCount = readUint16(sidx, index);
  1805. index += 2;
  1806. for (var i = 0; i < referencesCount; i++) {
  1807. var referenceIndex = index;
  1808. var referenceInfo = readUint32(sidx, referenceIndex);
  1809. referenceIndex += 4;
  1810. var referenceSize = referenceInfo & 0x7fffffff;
  1811. var referenceType = (referenceInfo & 0x80000000) >>> 31;
  1812. if (referenceType === 1) {
  1813. logger.warn('SIDX has hierarchical references (not supported)');
  1814. return null;
  1815. }
  1816. var subsegmentDuration = readUint32(sidx, referenceIndex);
  1817. referenceIndex += 4;
  1818. references.push({
  1819. referenceSize: referenceSize,
  1820. subsegmentDuration: subsegmentDuration,
  1821. // unscaled
  1822. info: {
  1823. duration: subsegmentDuration / timescale,
  1824. start: startByte,
  1825. end: startByte + referenceSize - 1
  1826. }
  1827. });
  1828. startByte += referenceSize;
  1829. // Skipping 1 bit for |startsWithSap|, 3 bits for |sapType|, and 28 bits
  1830. // for |sapDelta|.
  1831. referenceIndex += 4;
  1832. // skip to next ref
  1833. index = referenceIndex;
  1834. }
  1835. return {
  1836. earliestPresentationTime: earliestPresentationTime,
  1837. timescale: timescale,
  1838. version: version,
  1839. referencesCount: referencesCount,
  1840. references: references
  1841. };
  1842. }
  1843. /**
  1844. * Parses an MP4 initialization segment and extracts stream type and
  1845. * timescale values for any declared tracks. Timescale values indicate the
  1846. * number of clock ticks per second to assume for time-based values
  1847. * elsewhere in the MP4.
  1848. *
  1849. * To determine the start time of an MP4, you need two pieces of
  1850. * information: the timescale unit and the earliest base media decode
  1851. * time. Multiple timescales can be specified within an MP4 but the
  1852. * base media decode time is always expressed in the timescale from
  1853. * the media header box for the track:
  1854. * ```
  1855. * moov > trak > mdia > mdhd.timescale
  1856. * moov > trak > mdia > hdlr
  1857. * ```
  1858. * @param initSegment the bytes of the init segment
  1859. * @returns a hash of track type to timescale values or null if
  1860. * the init segment is malformed.
  1861. */
  1862. function parseInitSegment(initSegment) {
  1863. var result = [];
  1864. var traks = findBox(initSegment, ['moov', 'trak']);
  1865. for (var i = 0; i < traks.length; i++) {
  1866. var trak = traks[i];
  1867. var tkhd = findBox(trak, ['tkhd'])[0];
  1868. if (tkhd) {
  1869. var version = tkhd[0];
  1870. var trackId = readUint32(tkhd, version === 0 ? 12 : 20);
  1871. var mdhd = findBox(trak, ['mdia', 'mdhd'])[0];
  1872. if (mdhd) {
  1873. version = mdhd[0];
  1874. var timescale = readUint32(mdhd, version === 0 ? 12 : 20);
  1875. var hdlr = findBox(trak, ['mdia', 'hdlr'])[0];
  1876. if (hdlr) {
  1877. var hdlrType = bin2str(hdlr.subarray(8, 12));
  1878. var type = {
  1879. soun: ElementaryStreamTypes.AUDIO,
  1880. vide: ElementaryStreamTypes.VIDEO
  1881. }[hdlrType];
  1882. if (type) {
  1883. // Parse codec details
  1884. var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
  1885. var stsdData = parseStsd(stsd);
  1886. result[trackId] = {
  1887. timescale: timescale,
  1888. type: type
  1889. };
  1890. result[type] = _objectSpread2({
  1891. timescale: timescale,
  1892. id: trackId
  1893. }, stsdData);
  1894. }
  1895. }
  1896. }
  1897. }
  1898. }
  1899. var trex = findBox(initSegment, ['moov', 'mvex', 'trex']);
  1900. trex.forEach(function (trex) {
  1901. var trackId = readUint32(trex, 4);
  1902. var track = result[trackId];
  1903. if (track) {
  1904. track.default = {
  1905. duration: readUint32(trex, 12),
  1906. flags: readUint32(trex, 20)
  1907. };
  1908. }
  1909. });
  1910. return result;
  1911. }
  1912. function parseStsd(stsd) {
  1913. var sampleEntries = stsd.subarray(8);
  1914. var sampleEntriesEnd = sampleEntries.subarray(8 + 78);
  1915. var fourCC = bin2str(sampleEntries.subarray(4, 8));
  1916. var codec = fourCC;
  1917. var encrypted = fourCC === 'enca' || fourCC === 'encv';
  1918. if (encrypted) {
  1919. var encBox = findBox(sampleEntries, [fourCC])[0];
  1920. var encBoxChildren = encBox.subarray(fourCC === 'enca' ? 28 : 78);
  1921. var sinfs = findBox(encBoxChildren, ['sinf']);
  1922. sinfs.forEach(function (sinf) {
  1923. var schm = findBox(sinf, ['schm'])[0];
  1924. if (schm) {
  1925. var scheme = bin2str(schm.subarray(4, 8));
  1926. if (scheme === 'cbcs' || scheme === 'cenc') {
  1927. var frma = findBox(sinf, ['frma'])[0];
  1928. if (frma) {
  1929. // for encrypted content codec fourCC will be in frma
  1930. codec = bin2str(frma);
  1931. }
  1932. }
  1933. }
  1934. });
  1935. }
  1936. switch (codec) {
  1937. case 'avc1':
  1938. case 'avc2':
  1939. case 'avc3':
  1940. case 'avc4':
  1941. {
  1942. // extract profile + compatibility + level out of avcC box
  1943. var avcCBox = findBox(sampleEntriesEnd, ['avcC'])[0];
  1944. codec += '.' + toHex(avcCBox[1]) + toHex(avcCBox[2]) + toHex(avcCBox[3]);
  1945. break;
  1946. }
  1947. case 'mp4a':
  1948. {
  1949. var codecBox = findBox(sampleEntries, [fourCC])[0];
  1950. var esdsBox = findBox(codecBox.subarray(28), ['esds'])[0];
  1951. if (esdsBox && esdsBox.length > 12) {
  1952. var i = 4;
  1953. // ES Descriptor tag
  1954. if (esdsBox[i++] !== 0x03) {
  1955. break;
  1956. }
  1957. i = skipBERInteger(esdsBox, i);
  1958. i += 2; // skip es_id;
  1959. var flags = esdsBox[i++];
  1960. if (flags & 0x80) {
  1961. i += 2; // skip dependency es_id
  1962. }
  1963. if (flags & 0x40) {
  1964. i += esdsBox[i++]; // skip URL
  1965. }
  1966. // Decoder config descriptor
  1967. if (esdsBox[i++] !== 0x04) {
  1968. break;
  1969. }
  1970. i = skipBERInteger(esdsBox, i);
  1971. var objectType = esdsBox[i++];
  1972. if (objectType === 0x40) {
  1973. codec += '.' + toHex(objectType);
  1974. } else {
  1975. break;
  1976. }
  1977. i += 12;
  1978. // Decoder specific info
  1979. if (esdsBox[i++] !== 0x05) {
  1980. break;
  1981. }
  1982. i = skipBERInteger(esdsBox, i);
  1983. var firstByte = esdsBox[i++];
  1984. var audioObjectType = (firstByte & 0xf8) >> 3;
  1985. if (audioObjectType === 31) {
  1986. audioObjectType += 1 + ((firstByte & 0x7) << 3) + ((esdsBox[i] & 0xe0) >> 5);
  1987. }
  1988. codec += '.' + audioObjectType;
  1989. }
  1990. break;
  1991. }
  1992. case 'hvc1':
  1993. case 'hev1':
  1994. {
  1995. var hvcCBox = findBox(sampleEntriesEnd, ['hvcC'])[0];
  1996. var profileByte = hvcCBox[1];
  1997. var profileSpace = ['', 'A', 'B', 'C'][profileByte >> 6];
  1998. var generalProfileIdc = profileByte & 0x1f;
  1999. var profileCompat = readUint32(hvcCBox, 2);
  2000. var tierFlag = (profileByte & 0x20) >> 5 ? 'H' : 'L';
  2001. var levelIDC = hvcCBox[12];
  2002. var constraintIndicator = hvcCBox.subarray(6, 12);
  2003. codec += '.' + profileSpace + generalProfileIdc;
  2004. codec += '.' + profileCompat.toString(16).toUpperCase();
  2005. codec += '.' + tierFlag + levelIDC;
  2006. var constraintString = '';
  2007. for (var _i = constraintIndicator.length; _i--;) {
  2008. var _byte = constraintIndicator[_i];
  2009. if (_byte || constraintString) {
  2010. var encodedByte = _byte.toString(16).toUpperCase();
  2011. constraintString = '.' + encodedByte + constraintString;
  2012. }
  2013. }
  2014. codec += constraintString;
  2015. break;
  2016. }
  2017. case 'dvh1':
  2018. case 'dvhe':
  2019. {
  2020. var dvcCBox = findBox(sampleEntriesEnd, ['dvcC'])[0];
  2021. var profile = dvcCBox[2] >> 1 & 0x7f;
  2022. var level = dvcCBox[2] << 5 & 0x20 | dvcCBox[3] >> 3 & 0x1f;
  2023. codec += '.' + addLeadingZero(profile) + '.' + addLeadingZero(level);
  2024. break;
  2025. }
  2026. case 'vp09':
  2027. {
  2028. var vpcCBox = findBox(sampleEntriesEnd, ['vpcC'])[0];
  2029. var _profile = vpcCBox[4];
  2030. var _level = vpcCBox[5];
  2031. var bitDepth = vpcCBox[6] >> 4 & 0x0f;
  2032. codec += '.' + addLeadingZero(_profile) + '.' + addLeadingZero(_level) + '.' + addLeadingZero(bitDepth);
  2033. break;
  2034. }
  2035. case 'av01':
  2036. {
  2037. var av1CBox = findBox(sampleEntriesEnd, ['av1C'])[0];
  2038. var _profile2 = av1CBox[1] >>> 5;
  2039. var _level2 = av1CBox[1] & 0x1f;
  2040. var _tierFlag = av1CBox[2] >>> 7 ? 'H' : 'M';
  2041. var highBitDepth = (av1CBox[2] & 0x40) >> 6;
  2042. var twelveBit = (av1CBox[2] & 0x20) >> 5;
  2043. var _bitDepth = _profile2 === 2 && highBitDepth ? twelveBit ? 12 : 10 : highBitDepth ? 10 : 8;
  2044. var monochrome = (av1CBox[2] & 0x10) >> 4;
  2045. var chromaSubsamplingX = (av1CBox[2] & 0x08) >> 3;
  2046. var chromaSubsamplingY = (av1CBox[2] & 0x04) >> 2;
  2047. var chromaSamplePosition = av1CBox[2] & 0x03;
  2048. // TODO: parse color_description_present_flag
  2049. // default it to BT.709/limited range for now
  2050. // more info https://aomediacodec.github.io/av1-isobmff/#av1codecconfigurationbox-syntax
  2051. var colorPrimaries = 1;
  2052. var transferCharacteristics = 1;
  2053. var matrixCoefficients = 1;
  2054. var videoFullRangeFlag = 0;
  2055. codec += '.' + _profile2 + '.' + addLeadingZero(_level2) + _tierFlag + '.' + addLeadingZero(_bitDepth) + '.' + monochrome + '.' + chromaSubsamplingX + chromaSubsamplingY + chromaSamplePosition + '.' + addLeadingZero(colorPrimaries) + '.' + addLeadingZero(transferCharacteristics) + '.' + addLeadingZero(matrixCoefficients) + '.' + videoFullRangeFlag;
  2056. break;
  2057. }
  2058. }
  2059. return {
  2060. codec: codec,
  2061. encrypted: encrypted
  2062. };
  2063. }
  2064. function skipBERInteger(bytes, i) {
  2065. var limit = i + 5;
  2066. while (bytes[i++] & 0x80 && i < limit) {}
  2067. return i;
  2068. }
  2069. function toHex(x) {
  2070. return ('0' + x.toString(16).toUpperCase()).slice(-2);
  2071. }
  2072. function addLeadingZero(num) {
  2073. return (num < 10 ? '0' : '') + num;
  2074. }
  2075. function patchEncyptionData(initSegment, decryptdata) {
  2076. if (!initSegment || !decryptdata) {
  2077. return initSegment;
  2078. }
  2079. var keyId = decryptdata.keyId;
  2080. if (keyId && decryptdata.isCommonEncryption) {
  2081. var traks = findBox(initSegment, ['moov', 'trak']);
  2082. traks.forEach(function (trak) {
  2083. var stsd = findBox(trak, ['mdia', 'minf', 'stbl', 'stsd'])[0];
  2084. // skip the sample entry count
  2085. var sampleEntries = stsd.subarray(8);
  2086. var encBoxes = findBox(sampleEntries, ['enca']);
  2087. var isAudio = encBoxes.length > 0;
  2088. if (!isAudio) {
  2089. encBoxes = findBox(sampleEntries, ['encv']);
  2090. }
  2091. encBoxes.forEach(function (enc) {
  2092. var encBoxChildren = isAudio ? enc.subarray(28) : enc.subarray(78);
  2093. var sinfBoxes = findBox(encBoxChildren, ['sinf']);
  2094. sinfBoxes.forEach(function (sinf) {
  2095. var tenc = parseSinf(sinf);
  2096. if (tenc) {
  2097. // Look for default key id (keyID offset is always 8 within the tenc box):
  2098. var tencKeyId = tenc.subarray(8, 24);
  2099. if (!tencKeyId.some(function (b) {
  2100. return b !== 0;
  2101. })) {
  2102. logger.log("[eme] Patching keyId in 'enc" + (isAudio ? 'a' : 'v') + ">sinf>>tenc' box: " + Hex.hexDump(tencKeyId) + " -> " + Hex.hexDump(keyId));
  2103. tenc.set(keyId, 8);
  2104. }
  2105. }
  2106. });
  2107. });
  2108. });
  2109. }
  2110. return initSegment;
  2111. }
  2112. function parseSinf(sinf) {
  2113. var schm = findBox(sinf, ['schm'])[0];
  2114. if (schm) {
  2115. var scheme = bin2str(schm.subarray(4, 8));
  2116. if (scheme === 'cbcs' || scheme === 'cenc') {
  2117. return findBox(sinf, ['schi', 'tenc'])[0];
  2118. }
  2119. }
  2120. logger.error("[eme] missing 'schm' box");
  2121. return null;
  2122. }
  2123. /**
  2124. * Determine the base media decode start time, in seconds, for an MP4
  2125. * fragment. If multiple fragments are specified, the earliest time is
  2126. * returned.
  2127. *
  2128. * The base media decode time can be parsed from track fragment
  2129. * metadata:
  2130. * ```
  2131. * moof > traf > tfdt.baseMediaDecodeTime
  2132. * ```
  2133. * It requires the timescale value from the mdhd to interpret.
  2134. *
  2135. * @param initData - a hash of track type to timescale values
  2136. * @param fmp4 - the bytes of the mp4 fragment
  2137. * @returns the earliest base media decode start time for the
  2138. * fragment, in seconds
  2139. */
  2140. function getStartDTS(initData, fmp4) {
  2141. // we need info from two children of each track fragment box
  2142. return findBox(fmp4, ['moof', 'traf']).reduce(function (result, traf) {
  2143. var tfdt = findBox(traf, ['tfdt'])[0];
  2144. var version = tfdt[0];
  2145. var start = findBox(traf, ['tfhd']).reduce(function (result, tfhd) {
  2146. // get the track id from the tfhd
  2147. var id = readUint32(tfhd, 4);
  2148. var track = initData[id];
  2149. if (track) {
  2150. var baseTime = readUint32(tfdt, 4);
  2151. if (version === 1) {
  2152. // If value is too large, assume signed 64-bit. Negative track fragment decode times are invalid, but they exist in the wild.
  2153. // This prevents large values from being used for initPTS, which can cause playlist sync issues.
  2154. // https://github.com/video-dev/hls.js/issues/5303
  2155. if (baseTime === UINT32_MAX$1) {
  2156. logger.warn("[mp4-demuxer]: Ignoring assumed invalid signed 64-bit track fragment decode time");
  2157. return result;
  2158. }
  2159. baseTime *= UINT32_MAX$1 + 1;
  2160. baseTime += readUint32(tfdt, 8);
  2161. }
  2162. // assume a 90kHz clock if no timescale was specified
  2163. var scale = track.timescale || 90e3;
  2164. // convert base time to seconds
  2165. var startTime = baseTime / scale;
  2166. if (isFiniteNumber(startTime) && (result === null || startTime < result)) {
  2167. return startTime;
  2168. }
  2169. }
  2170. return result;
  2171. }, null);
  2172. if (start !== null && isFiniteNumber(start) && (result === null || start < result)) {
  2173. return start;
  2174. }
  2175. return result;
  2176. }, null);
  2177. }
  2178. /*
  2179. For Reference:
  2180. aligned(8) class TrackFragmentHeaderBox
  2181. extends FullBox(‘tfhd’, 0, tf_flags){
  2182. unsigned int(32) track_ID;
  2183. // all the following are optional fields
  2184. unsigned int(64) base_data_offset;
  2185. unsigned int(32) sample_description_index;
  2186. unsigned int(32) default_sample_duration;
  2187. unsigned int(32) default_sample_size;
  2188. unsigned int(32) default_sample_flags
  2189. }
  2190. */
  2191. function getDuration(data, initData) {
  2192. var rawDuration = 0;
  2193. var videoDuration = 0;
  2194. var audioDuration = 0;
  2195. var trafs = findBox(data, ['moof', 'traf']);
  2196. for (var i = 0; i < trafs.length; i++) {
  2197. var traf = trafs[i];
  2198. // There is only one tfhd & trun per traf
  2199. // This is true for CMAF style content, and we should perhaps check the ftyp
  2200. // and only look for a single trun then, but for ISOBMFF we should check
  2201. // for multiple track runs.
  2202. var tfhd = findBox(traf, ['tfhd'])[0];
  2203. // get the track id from the tfhd
  2204. var id = readUint32(tfhd, 4);
  2205. var track = initData[id];
  2206. if (!track) {
  2207. continue;
  2208. }
  2209. var trackDefault = track.default;
  2210. var tfhdFlags = readUint32(tfhd, 0) | (trackDefault == null ? void 0 : trackDefault.flags);
  2211. var sampleDuration = trackDefault == null ? void 0 : trackDefault.duration;
  2212. if (tfhdFlags & 0x000008) {
  2213. // 0x000008 indicates the presence of the default_sample_duration field
  2214. if (tfhdFlags & 0x000002) {
  2215. // 0x000002 indicates the presence of the sample_description_index field, which precedes default_sample_duration
  2216. // If present, the default_sample_duration exists at byte offset 12
  2217. sampleDuration = readUint32(tfhd, 12);
  2218. } else {
  2219. // Otherwise, the duration is at byte offset 8
  2220. sampleDuration = readUint32(tfhd, 8);
  2221. }
  2222. }
  2223. // assume a 90kHz clock if no timescale was specified
  2224. var timescale = track.timescale || 90e3;
  2225. var truns = findBox(traf, ['trun']);
  2226. for (var j = 0; j < truns.length; j++) {
  2227. rawDuration = computeRawDurationFromSamples(truns[j]);
  2228. if (!rawDuration && sampleDuration) {
  2229. var sampleCount = readUint32(truns[j], 4);
  2230. rawDuration = sampleDuration * sampleCount;
  2231. }
  2232. if (track.type === ElementaryStreamTypes.VIDEO) {
  2233. videoDuration += rawDuration / timescale;
  2234. } else if (track.type === ElementaryStreamTypes.AUDIO) {
  2235. audioDuration += rawDuration / timescale;
  2236. }
  2237. }
  2238. }
  2239. if (videoDuration === 0 && audioDuration === 0) {
  2240. // If duration samples are not available in the traf use sidx subsegment_duration
  2241. var sidxMinStart = Infinity;
  2242. var sidxMaxEnd = 0;
  2243. var sidxDuration = 0;
  2244. var sidxs = findBox(data, ['sidx']);
  2245. for (var _i2 = 0; _i2 < sidxs.length; _i2++) {
  2246. var sidx = parseSegmentIndex(sidxs[_i2]);
  2247. if (sidx != null && sidx.references) {
  2248. sidxMinStart = Math.min(sidxMinStart, sidx.earliestPresentationTime / sidx.timescale);
  2249. var subSegmentDuration = sidx.references.reduce(function (dur, ref) {
  2250. return dur + ref.info.duration || 0;
  2251. }, 0);
  2252. sidxMaxEnd = Math.max(sidxMaxEnd, subSegmentDuration + sidx.earliestPresentationTime / sidx.timescale);
  2253. sidxDuration = sidxMaxEnd - sidxMinStart;
  2254. }
  2255. }
  2256. if (sidxDuration && isFiniteNumber(sidxDuration)) {
  2257. return sidxDuration;
  2258. }
  2259. }
  2260. if (videoDuration) {
  2261. return videoDuration;
  2262. }
  2263. return audioDuration;
  2264. }
  2265. /*
  2266. For Reference:
  2267. aligned(8) class TrackRunBox
  2268. extends FullBox(‘trun’, version, tr_flags) {
  2269. unsigned int(32) sample_count;
  2270. // the following are optional fields
  2271. signed int(32) data_offset;
  2272. unsigned int(32) first_sample_flags;
  2273. // all fields in the following array are optional
  2274. {
  2275. unsigned int(32) sample_duration;
  2276. unsigned int(32) sample_size;
  2277. unsigned int(32) sample_flags
  2278. if (version == 0)
  2279. { unsigned int(32)
  2280. else
  2281. { signed int(32)
  2282. }[ sample_count ]
  2283. }
  2284. */
  2285. function computeRawDurationFromSamples(trun) {
  2286. var flags = readUint32(trun, 0);
  2287. // Flags are at offset 0, non-optional sample_count is at offset 4. Therefore we start 8 bytes in.
  2288. // Each field is an int32, which is 4 bytes
  2289. var offset = 8;
  2290. // data-offset-present flag
  2291. if (flags & 0x000001) {
  2292. offset += 4;
  2293. }
  2294. // first-sample-flags-present flag
  2295. if (flags & 0x000004) {
  2296. offset += 4;
  2297. }
  2298. var duration = 0;
  2299. var sampleCount = readUint32(trun, 4);
  2300. for (var i = 0; i < sampleCount; i++) {
  2301. // sample-duration-present flag
  2302. if (flags & 0x000100) {
  2303. var sampleDuration = readUint32(trun, offset);
  2304. duration += sampleDuration;
  2305. offset += 4;
  2306. }
  2307. // sample-size-present flag
  2308. if (flags & 0x000200) {
  2309. offset += 4;
  2310. }
  2311. // sample-flags-present flag
  2312. if (flags & 0x000400) {
  2313. offset += 4;
  2314. }
  2315. // sample-composition-time-offsets-present flag
  2316. if (flags & 0x000800) {
  2317. offset += 4;
  2318. }
  2319. }
  2320. return duration;
  2321. }
  2322. function offsetStartDTS(initData, fmp4, timeOffset) {
  2323. findBox(fmp4, ['moof', 'traf']).forEach(function (traf) {
  2324. findBox(traf, ['tfhd']).forEach(function (tfhd) {
  2325. // get the track id from the tfhd
  2326. var id = readUint32(tfhd, 4);
  2327. var track = initData[id];
  2328. if (!track) {
  2329. return;
  2330. }
  2331. // assume a 90kHz clock if no timescale was specified
  2332. var timescale = track.timescale || 90e3;
  2333. // get the base media decode time from the tfdt
  2334. findBox(traf, ['tfdt']).forEach(function (tfdt) {
  2335. var version = tfdt[0];
  2336. var offset = timeOffset * timescale;
  2337. if (offset) {
  2338. var baseMediaDecodeTime = readUint32(tfdt, 4);
  2339. if (version === 0) {
  2340. baseMediaDecodeTime -= offset;
  2341. baseMediaDecodeTime = Math.max(baseMediaDecodeTime, 0);
  2342. writeUint32(tfdt, 4, baseMediaDecodeTime);
  2343. } else {
  2344. baseMediaDecodeTime *= Math.pow(2, 32);
  2345. baseMediaDecodeTime += readUint32(tfdt, 8);
  2346. baseMediaDecodeTime -= offset;
  2347. baseMediaDecodeTime = Math.max(baseMediaDecodeTime, 0);
  2348. var upper = Math.floor(baseMediaDecodeTime / (UINT32_MAX$1 + 1));
  2349. var lower = Math.floor(baseMediaDecodeTime % (UINT32_MAX$1 + 1));
  2350. writeUint32(tfdt, 4, upper);
  2351. writeUint32(tfdt, 8, lower);
  2352. }
  2353. }
  2354. });
  2355. });
  2356. });
  2357. }
  2358. // TODO: Check if the last moof+mdat pair is part of the valid range
  2359. function segmentValidRange(data) {
  2360. var segmentedRange = {
  2361. valid: null,
  2362. remainder: null
  2363. };
  2364. var moofs = findBox(data, ['moof']);
  2365. if (moofs.length < 2) {
  2366. segmentedRange.remainder = data;
  2367. return segmentedRange;
  2368. }
  2369. var last = moofs[moofs.length - 1];
  2370. // Offset by 8 bytes; findBox offsets the start by as much
  2371. segmentedRange.valid = sliceUint8(data, 0, last.byteOffset - 8);
  2372. segmentedRange.remainder = sliceUint8(data, last.byteOffset - 8);
  2373. return segmentedRange;
  2374. }
  2375. function appendUint8Array(data1, data2) {
  2376. var temp = new Uint8Array(data1.length + data2.length);
  2377. temp.set(data1);
  2378. temp.set(data2, data1.length);
  2379. return temp;
  2380. }
  2381. function parseSamples(timeOffset, track) {
  2382. var seiSamples = [];
  2383. var videoData = track.samples;
  2384. var timescale = track.timescale;
  2385. var trackId = track.id;
  2386. var isHEVCFlavor = false;
  2387. var moofs = findBox(videoData, ['moof']);
  2388. moofs.map(function (moof) {
  2389. var moofOffset = moof.byteOffset - 8;
  2390. var trafs = findBox(moof, ['traf']);
  2391. trafs.map(function (traf) {
  2392. // get the base media decode time from the tfdt
  2393. var baseTime = findBox(traf, ['tfdt']).map(function (tfdt) {
  2394. var version = tfdt[0];
  2395. var result = readUint32(tfdt, 4);
  2396. if (version === 1) {
  2397. result *= Math.pow(2, 32);
  2398. result += readUint32(tfdt, 8);
  2399. }
  2400. return result / timescale;
  2401. })[0];
  2402. if (baseTime !== undefined) {
  2403. timeOffset = baseTime;
  2404. }
  2405. return findBox(traf, ['tfhd']).map(function (tfhd) {
  2406. var id = readUint32(tfhd, 4);
  2407. var tfhdFlags = readUint32(tfhd, 0) & 0xffffff;
  2408. var baseDataOffsetPresent = (tfhdFlags & 0x000001) !== 0;
  2409. var sampleDescriptionIndexPresent = (tfhdFlags & 0x000002) !== 0;
  2410. var defaultSampleDurationPresent = (tfhdFlags & 0x000008) !== 0;
  2411. var defaultSampleDuration = 0;
  2412. var defaultSampleSizePresent = (tfhdFlags & 0x000010) !== 0;
  2413. var defaultSampleSize = 0;
  2414. var defaultSampleFlagsPresent = (tfhdFlags & 0x000020) !== 0;
  2415. var tfhdOffset = 8;
  2416. if (id === trackId) {
  2417. if (baseDataOffsetPresent) {
  2418. tfhdOffset += 8;
  2419. }
  2420. if (sampleDescriptionIndexPresent) {
  2421. tfhdOffset += 4;
  2422. }
  2423. if (defaultSampleDurationPresent) {
  2424. defaultSampleDuration = readUint32(tfhd, tfhdOffset);
  2425. tfhdOffset += 4;
  2426. }
  2427. if (defaultSampleSizePresent) {
  2428. defaultSampleSize = readUint32(tfhd, tfhdOffset);
  2429. tfhdOffset += 4;
  2430. }
  2431. if (defaultSampleFlagsPresent) {
  2432. tfhdOffset += 4;
  2433. }
  2434. if (track.type === 'video') {
  2435. isHEVCFlavor = isHEVC(track.codec);
  2436. }
  2437. findBox(traf, ['trun']).map(function (trun) {
  2438. var version = trun[0];
  2439. var flags = readUint32(trun, 0) & 0xffffff;
  2440. var dataOffsetPresent = (flags & 0x000001) !== 0;
  2441. var dataOffset = 0;
  2442. var firstSampleFlagsPresent = (flags & 0x000004) !== 0;
  2443. var sampleDurationPresent = (flags & 0x000100) !== 0;
  2444. var sampleDuration = 0;
  2445. var sampleSizePresent = (flags & 0x000200) !== 0;
  2446. var sampleSize = 0;
  2447. var sampleFlagsPresent = (flags & 0x000400) !== 0;
  2448. var sampleCompositionOffsetsPresent = (flags & 0x000800) !== 0;
  2449. var compositionOffset = 0;
  2450. var sampleCount = readUint32(trun, 4);
  2451. var trunOffset = 8; // past version, flags, and sample count
  2452. if (dataOffsetPresent) {
  2453. dataOffset = readUint32(trun, trunOffset);
  2454. trunOffset += 4;
  2455. }
  2456. if (firstSampleFlagsPresent) {
  2457. trunOffset += 4;
  2458. }
  2459. var sampleOffset = dataOffset + moofOffset;
  2460. for (var ix = 0; ix < sampleCount; ix++) {
  2461. if (sampleDurationPresent) {
  2462. sampleDuration = readUint32(trun, trunOffset);
  2463. trunOffset += 4;
  2464. } else {
  2465. sampleDuration = defaultSampleDuration;
  2466. }
  2467. if (sampleSizePresent) {
  2468. sampleSize = readUint32(trun, trunOffset);
  2469. trunOffset += 4;
  2470. } else {
  2471. sampleSize = defaultSampleSize;
  2472. }
  2473. if (sampleFlagsPresent) {
  2474. trunOffset += 4;
  2475. }
  2476. if (sampleCompositionOffsetsPresent) {
  2477. if (version === 0) {
  2478. compositionOffset = readUint32(trun, trunOffset);
  2479. } else {
  2480. compositionOffset = readSint32(trun, trunOffset);
  2481. }
  2482. trunOffset += 4;
  2483. }
  2484. if (track.type === ElementaryStreamTypes.VIDEO) {
  2485. var naluTotalSize = 0;
  2486. while (naluTotalSize < sampleSize) {
  2487. var naluSize = readUint32(videoData, sampleOffset);
  2488. sampleOffset += 4;
  2489. if (isSEIMessage(isHEVCFlavor, videoData[sampleOffset])) {
  2490. var data = videoData.subarray(sampleOffset, sampleOffset + naluSize);
  2491. parseSEIMessageFromNALu(data, isHEVCFlavor ? 2 : 1, timeOffset + compositionOffset / timescale, seiSamples);
  2492. }
  2493. sampleOffset += naluSize;
  2494. naluTotalSize += naluSize + 4;
  2495. }
  2496. }
  2497. timeOffset += sampleDuration / timescale;
  2498. }
  2499. });
  2500. }
  2501. });
  2502. });
  2503. });
  2504. return seiSamples;
  2505. }
  2506. function isHEVC(codec) {
  2507. if (!codec) {
  2508. return false;
  2509. }
  2510. var delimit = codec.indexOf('.');
  2511. var baseCodec = delimit < 0 ? codec : codec.substring(0, delimit);
  2512. return baseCodec === 'hvc1' || baseCodec === 'hev1' ||
  2513. // Dolby Vision
  2514. baseCodec === 'dvh1' || baseCodec === 'dvhe';
  2515. }
  2516. function isSEIMessage(isHEVCFlavor, naluHeader) {
  2517. if (isHEVCFlavor) {
  2518. var naluType = naluHeader >> 1 & 0x3f;
  2519. return naluType === 39 || naluType === 40;
  2520. } else {
  2521. var _naluType = naluHeader & 0x1f;
  2522. return _naluType === 6;
  2523. }
  2524. }
  2525. function parseSEIMessageFromNALu(unescapedData, headerSize, pts, samples) {
  2526. var data = discardEPB(unescapedData);
  2527. var seiPtr = 0;
  2528. // skip nal header
  2529. seiPtr += headerSize;
  2530. var payloadType = 0;
  2531. var payloadSize = 0;
  2532. var b = 0;
  2533. while (seiPtr < data.length) {
  2534. payloadType = 0;
  2535. do {
  2536. if (seiPtr >= data.length) {
  2537. break;
  2538. }
  2539. b = data[seiPtr++];
  2540. payloadType += b;
  2541. } while (b === 0xff);
  2542. // Parse payload size.
  2543. payloadSize = 0;
  2544. do {
  2545. if (seiPtr >= data.length) {
  2546. break;
  2547. }
  2548. b = data[seiPtr++];
  2549. payloadSize += b;
  2550. } while (b === 0xff);
  2551. var leftOver = data.length - seiPtr;
  2552. // Create a variable to process the payload
  2553. var payPtr = seiPtr;
  2554. // Increment the seiPtr to the end of the payload
  2555. if (payloadSize < leftOver) {
  2556. seiPtr += payloadSize;
  2557. } else if (payloadSize > leftOver) {
  2558. // Some type of corruption has happened?
  2559. logger.error("Malformed SEI payload. " + payloadSize + " is too small, only " + leftOver + " bytes left to parse.");
  2560. // We might be able to parse some data, but let's be safe and ignore it.
  2561. break;
  2562. }
  2563. if (payloadType === 4) {
  2564. var countryCode = data[payPtr++];
  2565. if (countryCode === 181) {
  2566. var providerCode = readUint16(data, payPtr);
  2567. payPtr += 2;
  2568. if (providerCode === 49) {
  2569. var userStructure = readUint32(data, payPtr);
  2570. payPtr += 4;
  2571. if (userStructure === 0x47413934) {
  2572. var userDataType = data[payPtr++];
  2573. // Raw CEA-608 bytes wrapped in CEA-708 packet
  2574. if (userDataType === 3) {
  2575. var firstByte = data[payPtr++];
  2576. var totalCCs = 0x1f & firstByte;
  2577. var enabled = 0x40 & firstByte;
  2578. var totalBytes = enabled ? 2 + totalCCs * 3 : 0;
  2579. var byteArray = new Uint8Array(totalBytes);
  2580. if (enabled) {
  2581. byteArray[0] = firstByte;
  2582. for (var i = 1; i < totalBytes; i++) {
  2583. byteArray[i] = data[payPtr++];
  2584. }
  2585. }
  2586. samples.push({
  2587. type: userDataType,
  2588. payloadType: payloadType,
  2589. pts: pts,
  2590. bytes: byteArray
  2591. });
  2592. }
  2593. }
  2594. }
  2595. }
  2596. } else if (payloadType === 5) {
  2597. if (payloadSize > 16) {
  2598. var uuidStrArray = [];
  2599. for (var _i3 = 0; _i3 < 16; _i3++) {
  2600. var _b = data[payPtr++].toString(16);
  2601. uuidStrArray.push(_b.length == 1 ? '0' + _b : _b);
  2602. if (_i3 === 3 || _i3 === 5 || _i3 === 7 || _i3 === 9) {
  2603. uuidStrArray.push('-');
  2604. }
  2605. }
  2606. var length = payloadSize - 16;
  2607. var userDataBytes = new Uint8Array(length);
  2608. for (var _i4 = 0; _i4 < length; _i4++) {
  2609. userDataBytes[_i4] = data[payPtr++];
  2610. }
  2611. samples.push({
  2612. payloadType: payloadType,
  2613. pts: pts,
  2614. uuid: uuidStrArray.join(''),
  2615. userData: utf8ArrayToStr(userDataBytes),
  2616. userDataBytes: userDataBytes
  2617. });
  2618. }
  2619. }
  2620. }
  2621. }
  2622. /**
  2623. * remove Emulation Prevention bytes from a RBSP
  2624. */
  2625. function discardEPB(data) {
  2626. var length = data.byteLength;
  2627. var EPBPositions = [];
  2628. var i = 1;
  2629. // Find all `Emulation Prevention Bytes`
  2630. while (i < length - 2) {
  2631. if (data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  2632. EPBPositions.push(i + 2);
  2633. i += 2;
  2634. } else {
  2635. i++;
  2636. }
  2637. }
  2638. // If no Emulation Prevention Bytes were found just return the original
  2639. // array
  2640. if (EPBPositions.length === 0) {
  2641. return data;
  2642. }
  2643. // Create a new array to hold the NAL unit data
  2644. var newLength = length - EPBPositions.length;
  2645. var newData = new Uint8Array(newLength);
  2646. var sourceIndex = 0;
  2647. for (i = 0; i < newLength; sourceIndex++, i++) {
  2648. if (sourceIndex === EPBPositions[0]) {
  2649. // Skip this byte
  2650. sourceIndex++;
  2651. // Remove this position index
  2652. EPBPositions.shift();
  2653. }
  2654. newData[i] = data[sourceIndex];
  2655. }
  2656. return newData;
  2657. }
  2658. function parseEmsg(data) {
  2659. var version = data[0];
  2660. var schemeIdUri = '';
  2661. var value = '';
  2662. var timeScale = 0;
  2663. var presentationTimeDelta = 0;
  2664. var presentationTime = 0;
  2665. var eventDuration = 0;
  2666. var id = 0;
  2667. var offset = 0;
  2668. if (version === 0) {
  2669. while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
  2670. schemeIdUri += bin2str(data.subarray(offset, offset + 1));
  2671. offset += 1;
  2672. }
  2673. schemeIdUri += bin2str(data.subarray(offset, offset + 1));
  2674. offset += 1;
  2675. while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
  2676. value += bin2str(data.subarray(offset, offset + 1));
  2677. offset += 1;
  2678. }
  2679. value += bin2str(data.subarray(offset, offset + 1));
  2680. offset += 1;
  2681. timeScale = readUint32(data, 12);
  2682. presentationTimeDelta = readUint32(data, 16);
  2683. eventDuration = readUint32(data, 20);
  2684. id = readUint32(data, 24);
  2685. offset = 28;
  2686. } else if (version === 1) {
  2687. offset += 4;
  2688. timeScale = readUint32(data, offset);
  2689. offset += 4;
  2690. var leftPresentationTime = readUint32(data, offset);
  2691. offset += 4;
  2692. var rightPresentationTime = readUint32(data, offset);
  2693. offset += 4;
  2694. presentationTime = Math.pow(2, 32) * leftPresentationTime + rightPresentationTime;
  2695. if (!isSafeInteger(presentationTime)) {
  2696. presentationTime = Number.MAX_SAFE_INTEGER;
  2697. logger.warn('Presentation time exceeds safe integer limit and wrapped to max safe integer in parsing emsg box');
  2698. }
  2699. eventDuration = readUint32(data, offset);
  2700. offset += 4;
  2701. id = readUint32(data, offset);
  2702. offset += 4;
  2703. while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
  2704. schemeIdUri += bin2str(data.subarray(offset, offset + 1));
  2705. offset += 1;
  2706. }
  2707. schemeIdUri += bin2str(data.subarray(offset, offset + 1));
  2708. offset += 1;
  2709. while (bin2str(data.subarray(offset, offset + 1)) !== '\0') {
  2710. value += bin2str(data.subarray(offset, offset + 1));
  2711. offset += 1;
  2712. }
  2713. value += bin2str(data.subarray(offset, offset + 1));
  2714. offset += 1;
  2715. }
  2716. var payload = data.subarray(offset, data.byteLength);
  2717. return {
  2718. schemeIdUri: schemeIdUri,
  2719. value: value,
  2720. timeScale: timeScale,
  2721. presentationTime: presentationTime,
  2722. presentationTimeDelta: presentationTimeDelta,
  2723. eventDuration: eventDuration,
  2724. id: id,
  2725. payload: payload
  2726. };
  2727. }
  2728. function mp4Box(type) {
  2729. for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  2730. payload[_key - 1] = arguments[_key];
  2731. }
  2732. var len = payload.length;
  2733. var size = 8;
  2734. var i = len;
  2735. while (i--) {
  2736. size += payload[i].byteLength;
  2737. }
  2738. var result = new Uint8Array(size);
  2739. result[0] = size >> 24 & 0xff;
  2740. result[1] = size >> 16 & 0xff;
  2741. result[2] = size >> 8 & 0xff;
  2742. result[3] = size & 0xff;
  2743. result.set(type, 4);
  2744. for (i = 0, size = 8; i < len; i++) {
  2745. result.set(payload[i], size);
  2746. size += payload[i].byteLength;
  2747. }
  2748. return result;
  2749. }
  2750. function mp4pssh(systemId, keyids, data) {
  2751. if (systemId.byteLength !== 16) {
  2752. throw new RangeError('Invalid system id');
  2753. }
  2754. var version;
  2755. var kids;
  2756. if (keyids) {
  2757. version = 1;
  2758. kids = new Uint8Array(keyids.length * 16);
  2759. for (var ix = 0; ix < keyids.length; ix++) {
  2760. var k = keyids[ix]; // uint8array
  2761. if (k.byteLength !== 16) {
  2762. throw new RangeError('Invalid key');
  2763. }
  2764. kids.set(k, ix * 16);
  2765. }
  2766. } else {
  2767. version = 0;
  2768. kids = new Uint8Array();
  2769. }
  2770. var kidCount;
  2771. if (version > 0) {
  2772. kidCount = new Uint8Array(4);
  2773. if (keyids.length > 0) {
  2774. new DataView(kidCount.buffer).setUint32(0, keyids.length, false);
  2775. }
  2776. } else {
  2777. kidCount = new Uint8Array();
  2778. }
  2779. var dataSize = new Uint8Array(4);
  2780. if (data && data.byteLength > 0) {
  2781. new DataView(dataSize.buffer).setUint32(0, data.byteLength, false);
  2782. }
  2783. return mp4Box([112, 115, 115, 104], new Uint8Array([version, 0x00, 0x00, 0x00 // Flags
  2784. ]), systemId,
  2785. // 16 bytes
  2786. kidCount, kids, dataSize, data || new Uint8Array());
  2787. }
  2788. function parsePssh(initData) {
  2789. if (!(initData instanceof ArrayBuffer) || initData.byteLength < 32) {
  2790. return null;
  2791. }
  2792. var result = {
  2793. version: 0,
  2794. systemId: '',
  2795. kids: null,
  2796. data: null
  2797. };
  2798. var view = new DataView(initData);
  2799. var boxSize = view.getUint32(0);
  2800. if (initData.byteLength !== boxSize && boxSize > 44) {
  2801. return null;
  2802. }
  2803. var type = view.getUint32(4);
  2804. if (type !== 0x70737368) {
  2805. return null;
  2806. }
  2807. result.version = view.getUint32(8) >>> 24;
  2808. if (result.version > 1) {
  2809. return null;
  2810. }
  2811. result.systemId = Hex.hexDump(new Uint8Array(initData, 12, 16));
  2812. var dataSizeOrKidCount = view.getUint32(28);
  2813. if (result.version === 0) {
  2814. if (boxSize - 32 < dataSizeOrKidCount) {
  2815. return null;
  2816. }
  2817. result.data = new Uint8Array(initData, 32, dataSizeOrKidCount);
  2818. } else if (result.version === 1) {
  2819. result.kids = [];
  2820. for (var i = 0; i < dataSizeOrKidCount; i++) {
  2821. result.kids.push(new Uint8Array(initData, 32 + i * 16, 16));
  2822. }
  2823. }
  2824. return result;
  2825. }
  2826. var keyUriToKeyIdMap = {};
  2827. var LevelKey = /*#__PURE__*/function () {
  2828. LevelKey.clearKeyUriToKeyIdMap = function clearKeyUriToKeyIdMap() {
  2829. keyUriToKeyIdMap = {};
  2830. };
  2831. function LevelKey(method, uri, format, formatversions, iv) {
  2832. if (formatversions === void 0) {
  2833. formatversions = [1];
  2834. }
  2835. if (iv === void 0) {
  2836. iv = null;
  2837. }
  2838. this.uri = void 0;
  2839. this.method = void 0;
  2840. this.keyFormat = void 0;
  2841. this.keyFormatVersions = void 0;
  2842. this.encrypted = void 0;
  2843. this.isCommonEncryption = void 0;
  2844. this.iv = null;
  2845. this.key = null;
  2846. this.keyId = null;
  2847. this.pssh = null;
  2848. this.method = method;
  2849. this.uri = uri;
  2850. this.keyFormat = format;
  2851. this.keyFormatVersions = formatversions;
  2852. this.iv = iv;
  2853. this.encrypted = method ? method !== 'NONE' : false;
  2854. this.isCommonEncryption = this.encrypted && method !== 'AES-128';
  2855. }
  2856. var _proto = LevelKey.prototype;
  2857. _proto.isSupported = function isSupported() {
  2858. // If it's Segment encryption or No encryption, just select that key system
  2859. if (this.method) {
  2860. if (this.method === 'AES-128' || this.method === 'NONE') {
  2861. return true;
  2862. }
  2863. if (this.keyFormat === 'identity') {
  2864. // Maintain support for clear SAMPLE-AES with MPEG-3 TS
  2865. return this.method === 'SAMPLE-AES';
  2866. } else {
  2867. switch (this.keyFormat) {
  2868. case KeySystemFormats.FAIRPLAY:
  2869. case KeySystemFormats.WIDEVINE:
  2870. case KeySystemFormats.PLAYREADY:
  2871. case KeySystemFormats.CLEARKEY:
  2872. return ['ISO-23001-7', 'SAMPLE-AES', 'SAMPLE-AES-CENC', 'SAMPLE-AES-CTR'].indexOf(this.method) !== -1;
  2873. }
  2874. }
  2875. }
  2876. return false;
  2877. };
  2878. _proto.getDecryptData = function getDecryptData(sn) {
  2879. if (!this.encrypted || !this.uri) {
  2880. return null;
  2881. }
  2882. if (this.method === 'AES-128' && this.uri && !this.iv) {
  2883. if (typeof sn !== 'number') {
  2884. // We are fetching decryption data for a initialization segment
  2885. // If the segment was encrypted with AES-128
  2886. // It must have an IV defined. We cannot substitute the Segment Number in.
  2887. if (this.method === 'AES-128' && !this.iv) {
  2888. logger.warn("missing IV for initialization segment with method=\"" + this.method + "\" - compliance issue");
  2889. }
  2890. // Explicitly set sn to resulting value from implicit conversions 'initSegment' values for IV generation.
  2891. sn = 0;
  2892. }
  2893. var iv = createInitializationVector(sn);
  2894. var decryptdata = new LevelKey(this.method, this.uri, 'identity', this.keyFormatVersions, iv);
  2895. return decryptdata;
  2896. }
  2897. // Initialize keyId if possible
  2898. var keyBytes = convertDataUriToArrayBytes(this.uri);
  2899. if (keyBytes) {
  2900. switch (this.keyFormat) {
  2901. case KeySystemFormats.WIDEVINE:
  2902. this.pssh = keyBytes;
  2903. // In case of widevine keyID is embedded in PSSH box. Read Key ID.
  2904. if (keyBytes.length >= 22) {
  2905. this.keyId = keyBytes.subarray(keyBytes.length - 22, keyBytes.length - 6);
  2906. }
  2907. break;
  2908. case KeySystemFormats.PLAYREADY:
  2909. {
  2910. var PlayReadyKeySystemUUID = new Uint8Array([0x9a, 0x04, 0xf0, 0x79, 0x98, 0x40, 0x42, 0x86, 0xab, 0x92, 0xe6, 0x5b, 0xe0, 0x88, 0x5f, 0x95]);
  2911. this.pssh = mp4pssh(PlayReadyKeySystemUUID, null, keyBytes);
  2912. var keyBytesUtf16 = new Uint16Array(keyBytes.buffer, keyBytes.byteOffset, keyBytes.byteLength / 2);
  2913. var keyByteStr = String.fromCharCode.apply(null, Array.from(keyBytesUtf16));
  2914. // Parse Playready WRMHeader XML
  2915. var xmlKeyBytes = keyByteStr.substring(keyByteStr.indexOf('<'), keyByteStr.length);
  2916. var parser = new DOMParser();
  2917. var xmlDoc = parser.parseFromString(xmlKeyBytes, 'text/xml');
  2918. var keyData = xmlDoc.getElementsByTagName('KID')[0];
  2919. if (keyData) {
  2920. var keyId = keyData.childNodes[0] ? keyData.childNodes[0].nodeValue : keyData.getAttribute('VALUE');
  2921. if (keyId) {
  2922. var keyIdArray = base64Decode(keyId).subarray(0, 16);
  2923. // KID value in PRO is a base64-encoded little endian GUID interpretation of UUID
  2924. // KID value in ‘tenc’ is a big endian UUID GUID interpretation of UUID
  2925. changeEndianness(keyIdArray);
  2926. this.keyId = keyIdArray;
  2927. }
  2928. }
  2929. break;
  2930. }
  2931. default:
  2932. {
  2933. var keydata = keyBytes.subarray(0, 16);
  2934. if (keydata.length !== 16) {
  2935. var padded = new Uint8Array(16);
  2936. padded.set(keydata, 16 - keydata.length);
  2937. keydata = padded;
  2938. }
  2939. this.keyId = keydata;
  2940. break;
  2941. }
  2942. }
  2943. }
  2944. // Default behavior: assign a new keyId for each uri
  2945. if (!this.keyId || this.keyId.byteLength !== 16) {
  2946. var _keyId = keyUriToKeyIdMap[this.uri];
  2947. if (!_keyId) {
  2948. var val = Object.keys(keyUriToKeyIdMap).length % Number.MAX_SAFE_INTEGER;
  2949. _keyId = new Uint8Array(16);
  2950. var dv = new DataView(_keyId.buffer, 12, 4); // Just set the last 4 bytes
  2951. dv.setUint32(0, val);
  2952. keyUriToKeyIdMap[this.uri] = _keyId;
  2953. }
  2954. this.keyId = _keyId;
  2955. }
  2956. return this;
  2957. };
  2958. return LevelKey;
  2959. }();
  2960. function createInitializationVector(segmentNumber) {
  2961. var uint8View = new Uint8Array(16);
  2962. for (var i = 12; i < 16; i++) {
  2963. uint8View[i] = segmentNumber >> 8 * (15 - i) & 0xff;
  2964. }
  2965. return uint8View;
  2966. }
  2967. var VARIABLE_REPLACEMENT_REGEX = /\{\$([a-zA-Z0-9-_]+)\}/g;
  2968. function hasVariableReferences(str) {
  2969. return VARIABLE_REPLACEMENT_REGEX.test(str);
  2970. }
  2971. function substituteVariablesInAttributes(parsed, attr, attributeNames) {
  2972. if (parsed.variableList !== null || parsed.hasVariableRefs) {
  2973. for (var i = attributeNames.length; i--;) {
  2974. var name = attributeNames[i];
  2975. var value = attr[name];
  2976. if (value) {
  2977. attr[name] = substituteVariables(parsed, value);
  2978. }
  2979. }
  2980. }
  2981. }
  2982. function substituteVariables(parsed, value) {
  2983. if (parsed.variableList !== null || parsed.hasVariableRefs) {
  2984. var variableList = parsed.variableList;
  2985. return value.replace(VARIABLE_REPLACEMENT_REGEX, function (variableReference) {
  2986. var variableName = variableReference.substring(2, variableReference.length - 1);
  2987. var variableValue = variableList == null ? void 0 : variableList[variableName];
  2988. if (variableValue === undefined) {
  2989. parsed.playlistParsingError || (parsed.playlistParsingError = new Error("Missing preceding EXT-X-DEFINE tag for Variable Reference: \"" + variableName + "\""));
  2990. return variableReference;
  2991. }
  2992. return variableValue;
  2993. });
  2994. }
  2995. return value;
  2996. }
  2997. function addVariableDefinition(parsed, attr, parentUrl) {
  2998. var variableList = parsed.variableList;
  2999. if (!variableList) {
  3000. parsed.variableList = variableList = {};
  3001. }
  3002. var NAME;
  3003. var VALUE;
  3004. if ('QUERYPARAM' in attr) {
  3005. NAME = attr.QUERYPARAM;
  3006. try {
  3007. var searchParams = new self.URL(parentUrl).searchParams;
  3008. if (searchParams.has(NAME)) {
  3009. VALUE = searchParams.get(NAME);
  3010. } else {
  3011. throw new Error("\"" + NAME + "\" does not match any query parameter in URI: \"" + parentUrl + "\"");
  3012. }
  3013. } catch (error) {
  3014. parsed.playlistParsingError || (parsed.playlistParsingError = new Error("EXT-X-DEFINE QUERYPARAM: " + error.message));
  3015. }
  3016. } else {
  3017. NAME = attr.NAME;
  3018. VALUE = attr.VALUE;
  3019. }
  3020. if (NAME in variableList) {
  3021. parsed.playlistParsingError || (parsed.playlistParsingError = new Error("EXT-X-DEFINE duplicate Variable Name declarations: \"" + NAME + "\""));
  3022. } else {
  3023. variableList[NAME] = VALUE || '';
  3024. }
  3025. }
  3026. function importVariableDefinition(parsed, attr, sourceVariableList) {
  3027. var IMPORT = attr.IMPORT;
  3028. if (sourceVariableList && IMPORT in sourceVariableList) {
  3029. var variableList = parsed.variableList;
  3030. if (!variableList) {
  3031. parsed.variableList = variableList = {};
  3032. }
  3033. variableList[IMPORT] = sourceVariableList[IMPORT];
  3034. } else {
  3035. parsed.playlistParsingError || (parsed.playlistParsingError = new Error("EXT-X-DEFINE IMPORT attribute not found in Multivariant Playlist: \"" + IMPORT + "\""));
  3036. }
  3037. }
  3038. /**
  3039. * MediaSource helper
  3040. */
  3041. function getMediaSource(preferManagedMediaSource) {
  3042. if (preferManagedMediaSource === void 0) {
  3043. preferManagedMediaSource = true;
  3044. }
  3045. if (typeof self === 'undefined') return undefined;
  3046. var mms = (preferManagedMediaSource || !self.MediaSource) && self.ManagedMediaSource;
  3047. return mms || self.MediaSource || self.WebKitMediaSource;
  3048. }
  3049. function isManagedMediaSource(source) {
  3050. return typeof self !== 'undefined' && source === self.ManagedMediaSource;
  3051. }
  3052. // from http://mp4ra.org/codecs.html
  3053. // values indicate codec selection preference (lower is higher priority)
  3054. var sampleEntryCodesISO = {
  3055. audio: {
  3056. a3ds: 1,
  3057. 'ac-3': 0.95,
  3058. 'ac-4': 1,
  3059. alac: 0.9,
  3060. alaw: 1,
  3061. dra1: 1,
  3062. 'dts+': 1,
  3063. 'dts-': 1,
  3064. dtsc: 1,
  3065. dtse: 1,
  3066. dtsh: 1,
  3067. 'ec-3': 0.9,
  3068. enca: 1,
  3069. fLaC: 0.9,
  3070. // MP4-RA listed codec entry for FLAC
  3071. flac: 0.9,
  3072. // legacy browser codec name for FLAC
  3073. FLAC: 0.9,
  3074. // some manifests may list "FLAC" with Apple's tools
  3075. g719: 1,
  3076. g726: 1,
  3077. m4ae: 1,
  3078. mha1: 1,
  3079. mha2: 1,
  3080. mhm1: 1,
  3081. mhm2: 1,
  3082. mlpa: 1,
  3083. mp4a: 1,
  3084. 'raw ': 1,
  3085. Opus: 1,
  3086. opus: 1,
  3087. // browsers expect this to be lowercase despite MP4RA says 'Opus'
  3088. samr: 1,
  3089. sawb: 1,
  3090. sawp: 1,
  3091. sevc: 1,
  3092. sqcp: 1,
  3093. ssmv: 1,
  3094. twos: 1,
  3095. ulaw: 1
  3096. },
  3097. video: {
  3098. avc1: 1,
  3099. avc2: 1,
  3100. avc3: 1,
  3101. avc4: 1,
  3102. avcp: 1,
  3103. av01: 0.8,
  3104. drac: 1,
  3105. dva1: 1,
  3106. dvav: 1,
  3107. dvh1: 0.7,
  3108. dvhe: 0.7,
  3109. encv: 1,
  3110. hev1: 0.75,
  3111. hvc1: 0.75,
  3112. mjp2: 1,
  3113. mp4v: 1,
  3114. mvc1: 1,
  3115. mvc2: 1,
  3116. mvc3: 1,
  3117. mvc4: 1,
  3118. resv: 1,
  3119. rv60: 1,
  3120. s263: 1,
  3121. svc1: 1,
  3122. svc2: 1,
  3123. 'vc-1': 1,
  3124. vp08: 1,
  3125. vp09: 0.9
  3126. },
  3127. text: {
  3128. stpp: 1,
  3129. wvtt: 1
  3130. }
  3131. };
  3132. function isCodecType(codec, type) {
  3133. var typeCodes = sampleEntryCodesISO[type];
  3134. return !!typeCodes && !!typeCodes[codec.slice(0, 4)];
  3135. }
  3136. function areCodecsMediaSourceSupported(codecs, type, preferManagedMediaSource) {
  3137. if (preferManagedMediaSource === void 0) {
  3138. preferManagedMediaSource = true;
  3139. }
  3140. return !codecs.split(',').some(function (codec) {
  3141. return !isCodecMediaSourceSupported(codec, type, preferManagedMediaSource);
  3142. });
  3143. }
  3144. function isCodecMediaSourceSupported(codec, type, preferManagedMediaSource) {
  3145. var _MediaSource$isTypeSu;
  3146. if (preferManagedMediaSource === void 0) {
  3147. preferManagedMediaSource = true;
  3148. }
  3149. var MediaSource = getMediaSource(preferManagedMediaSource);
  3150. return (_MediaSource$isTypeSu = MediaSource == null ? void 0 : MediaSource.isTypeSupported(mimeTypeForCodec(codec, type))) != null ? _MediaSource$isTypeSu : false;
  3151. }
  3152. function mimeTypeForCodec(codec, type) {
  3153. return type + "/mp4;codecs=\"" + codec + "\"";
  3154. }
  3155. function videoCodecPreferenceValue(videoCodec) {
  3156. if (videoCodec) {
  3157. var fourCC = videoCodec.substring(0, 4);
  3158. return sampleEntryCodesISO.video[fourCC];
  3159. }
  3160. return 2;
  3161. }
  3162. function codecsSetSelectionPreferenceValue(codecSet) {
  3163. return codecSet.split(',').reduce(function (num, fourCC) {
  3164. var preferenceValue = sampleEntryCodesISO.video[fourCC];
  3165. if (preferenceValue) {
  3166. return (preferenceValue * 2 + num) / (num ? 3 : 2);
  3167. }
  3168. return (sampleEntryCodesISO.audio[fourCC] + num) / (num ? 2 : 1);
  3169. }, 0);
  3170. }
  3171. var CODEC_COMPATIBLE_NAMES = {};
  3172. function getCodecCompatibleNameLower(lowerCaseCodec, preferManagedMediaSource) {
  3173. if (preferManagedMediaSource === void 0) {
  3174. preferManagedMediaSource = true;
  3175. }
  3176. if (CODEC_COMPATIBLE_NAMES[lowerCaseCodec]) {
  3177. return CODEC_COMPATIBLE_NAMES[lowerCaseCodec];
  3178. }
  3179. // Idealy fLaC and Opus would be first (spec-compliant) but
  3180. // some browsers will report that fLaC is supported then fail.
  3181. // see: https://bugs.chromium.org/p/chromium/issues/detail?id=1422728
  3182. var codecsToCheck = {
  3183. flac: ['flac', 'fLaC', 'FLAC'],
  3184. opus: ['opus', 'Opus']
  3185. }[lowerCaseCodec];
  3186. for (var i = 0; i < codecsToCheck.length; i++) {
  3187. if (isCodecMediaSourceSupported(codecsToCheck[i], 'audio', preferManagedMediaSource)) {
  3188. CODEC_COMPATIBLE_NAMES[lowerCaseCodec] = codecsToCheck[i];
  3189. return codecsToCheck[i];
  3190. }
  3191. }
  3192. return lowerCaseCodec;
  3193. }
  3194. var AUDIO_CODEC_REGEXP = /flac|opus/i;
  3195. function getCodecCompatibleName(codec, preferManagedMediaSource) {
  3196. if (preferManagedMediaSource === void 0) {
  3197. preferManagedMediaSource = true;
  3198. }
  3199. return codec.replace(AUDIO_CODEC_REGEXP, function (m) {
  3200. return getCodecCompatibleNameLower(m.toLowerCase(), preferManagedMediaSource);
  3201. });
  3202. }
  3203. function pickMostCompleteCodecName(parsedCodec, levelCodec) {
  3204. // Parsing of mp4a codecs strings in mp4-tools from media is incomplete as of d8c6c7a
  3205. // so use level codec is parsed codec is unavailable or incomplete
  3206. if (parsedCodec && parsedCodec !== 'mp4a') {
  3207. return parsedCodec;
  3208. }
  3209. return levelCodec ? levelCodec.split(',')[0] : levelCodec;
  3210. }
  3211. function convertAVC1ToAVCOTI(codec) {
  3212. // Convert avc1 codec string from RFC-4281 to RFC-6381 for MediaSource.isTypeSupported
  3213. var avcdata = codec.split('.');
  3214. if (avcdata.length > 2) {
  3215. var result = avcdata.shift() + '.';
  3216. result += parseInt(avcdata.shift()).toString(16);
  3217. result += ('000' + parseInt(avcdata.shift()).toString(16)).slice(-4);
  3218. return result;
  3219. }
  3220. return codec;
  3221. }
  3222. var MASTER_PLAYLIST_REGEX = /#EXT-X-STREAM-INF:([^\r\n]*)(?:[\r\n](?:#[^\r\n]*)?)*([^\r\n]+)|#EXT-X-(SESSION-DATA|SESSION-KEY|DEFINE|CONTENT-STEERING|START):([^\r\n]*)[\r\n]+/g;
  3223. var MASTER_PLAYLIST_MEDIA_REGEX = /#EXT-X-MEDIA:(.*)/g;
  3224. var IS_MEDIA_PLAYLIST = /^#EXT(?:INF|-X-TARGETDURATION):/m; // Handle empty Media Playlist (first EXTINF not signaled, but TARGETDURATION present)
  3225. var LEVEL_PLAYLIST_REGEX_FAST = new RegExp([/#EXTINF:\s*(\d*(?:\.\d+)?)(?:,(.*)\s+)?/.source,
  3226. // duration (#EXTINF:<duration>,<title>), group 1 => duration, group 2 => title
  3227. /(?!#) *(\S[^\r\n]*)/.source,
  3228. // segment URI, group 3 => the URI (note newline is not eaten)
  3229. /#EXT-X-BYTERANGE:*(.+)/.source,
  3230. // next segment's byterange, group 4 => range spec (x@y)
  3231. /#EXT-X-PROGRAM-DATE-TIME:(.+)/.source,
  3232. // next segment's program date/time group 5 => the datetime spec
  3233. /#.*/.source // All other non-segment oriented tags will match with all groups empty
  3234. ].join('|'), 'g');
  3235. var LEVEL_PLAYLIST_REGEX_SLOW = new RegExp([/#(EXTM3U)/.source, /#EXT-X-(DATERANGE|DEFINE|KEY|MAP|PART|PART-INF|PLAYLIST-TYPE|PRELOAD-HINT|RENDITION-REPORT|SERVER-CONTROL|SKIP|START):(.+)/.source, /#EXT-X-(BITRATE|DISCONTINUITY-SEQUENCE|MEDIA-SEQUENCE|TARGETDURATION|VERSION): *(\d+)/.source, /#EXT-X-(DISCONTINUITY|ENDLIST|GAP|INDEPENDENT-SEGMENTS)/.source, /(#)([^:]*):(.*)/.source, /(#)(.*)(?:.*)\r?\n?/.source].join('|'));
  3236. var M3U8Parser = /*#__PURE__*/function () {
  3237. function M3U8Parser() {}
  3238. M3U8Parser.findGroup = function findGroup(groups, mediaGroupId) {
  3239. for (var i = 0; i < groups.length; i++) {
  3240. var group = groups[i];
  3241. if (group.id === mediaGroupId) {
  3242. return group;
  3243. }
  3244. }
  3245. };
  3246. M3U8Parser.resolve = function resolve(url, baseUrl) {
  3247. return urlToolkitExports.buildAbsoluteURL(baseUrl, url, {
  3248. alwaysNormalize: true
  3249. });
  3250. };
  3251. M3U8Parser.isMediaPlaylist = function isMediaPlaylist(str) {
  3252. return IS_MEDIA_PLAYLIST.test(str);
  3253. };
  3254. M3U8Parser.parseMasterPlaylist = function parseMasterPlaylist(string, baseurl) {
  3255. var hasVariableRefs = hasVariableReferences(string) ;
  3256. var parsed = {
  3257. contentSteering: null,
  3258. levels: [],
  3259. playlistParsingError: null,
  3260. sessionData: null,
  3261. sessionKeys: null,
  3262. startTimeOffset: null,
  3263. variableList: null,
  3264. hasVariableRefs: hasVariableRefs
  3265. };
  3266. var levelsWithKnownCodecs = [];
  3267. MASTER_PLAYLIST_REGEX.lastIndex = 0;
  3268. var result;
  3269. while ((result = MASTER_PLAYLIST_REGEX.exec(string)) != null) {
  3270. if (result[1]) {
  3271. var _level$unknownCodecs;
  3272. // '#EXT-X-STREAM-INF' is found, parse level tag in group 1
  3273. var attrs = new AttrList(result[1]);
  3274. {
  3275. substituteVariablesInAttributes(parsed, attrs, ['CODECS', 'SUPPLEMENTAL-CODECS', 'ALLOWED-CPC', 'PATHWAY-ID', 'STABLE-VARIANT-ID', 'AUDIO', 'VIDEO', 'SUBTITLES', 'CLOSED-CAPTIONS', 'NAME']);
  3276. }
  3277. var uri = substituteVariables(parsed, result[2]) ;
  3278. var level = {
  3279. attrs: attrs,
  3280. bitrate: attrs.decimalInteger('BANDWIDTH') || attrs.decimalInteger('AVERAGE-BANDWIDTH'),
  3281. name: attrs.NAME,
  3282. url: M3U8Parser.resolve(uri, baseurl)
  3283. };
  3284. var resolution = attrs.decimalResolution('RESOLUTION');
  3285. if (resolution) {
  3286. level.width = resolution.width;
  3287. level.height = resolution.height;
  3288. }
  3289. setCodecs(attrs.CODECS, level);
  3290. if (!((_level$unknownCodecs = level.unknownCodecs) != null && _level$unknownCodecs.length)) {
  3291. levelsWithKnownCodecs.push(level);
  3292. }
  3293. parsed.levels.push(level);
  3294. } else if (result[3]) {
  3295. var tag = result[3];
  3296. var attributes = result[4];
  3297. switch (tag) {
  3298. case 'SESSION-DATA':
  3299. {
  3300. // #EXT-X-SESSION-DATA
  3301. var sessionAttrs = new AttrList(attributes);
  3302. {
  3303. substituteVariablesInAttributes(parsed, sessionAttrs, ['DATA-ID', 'LANGUAGE', 'VALUE', 'URI']);
  3304. }
  3305. var dataId = sessionAttrs['DATA-ID'];
  3306. if (dataId) {
  3307. if (parsed.sessionData === null) {
  3308. parsed.sessionData = {};
  3309. }
  3310. parsed.sessionData[dataId] = sessionAttrs;
  3311. }
  3312. break;
  3313. }
  3314. case 'SESSION-KEY':
  3315. {
  3316. // #EXT-X-SESSION-KEY
  3317. var sessionKey = parseKey(attributes, baseurl, parsed);
  3318. if (sessionKey.encrypted && sessionKey.isSupported()) {
  3319. if (parsed.sessionKeys === null) {
  3320. parsed.sessionKeys = [];
  3321. }
  3322. parsed.sessionKeys.push(sessionKey);
  3323. } else {
  3324. logger.warn("[Keys] Ignoring invalid EXT-X-SESSION-KEY tag: \"" + attributes + "\"");
  3325. }
  3326. break;
  3327. }
  3328. case 'DEFINE':
  3329. {
  3330. // #EXT-X-DEFINE
  3331. {
  3332. var variableAttributes = new AttrList(attributes);
  3333. substituteVariablesInAttributes(parsed, variableAttributes, ['NAME', 'VALUE', 'QUERYPARAM']);
  3334. addVariableDefinition(parsed, variableAttributes, baseurl);
  3335. }
  3336. break;
  3337. }
  3338. case 'CONTENT-STEERING':
  3339. {
  3340. // #EXT-X-CONTENT-STEERING
  3341. var contentSteeringAttributes = new AttrList(attributes);
  3342. {
  3343. substituteVariablesInAttributes(parsed, contentSteeringAttributes, ['SERVER-URI', 'PATHWAY-ID']);
  3344. }
  3345. parsed.contentSteering = {
  3346. uri: M3U8Parser.resolve(contentSteeringAttributes['SERVER-URI'], baseurl),
  3347. pathwayId: contentSteeringAttributes['PATHWAY-ID'] || '.'
  3348. };
  3349. break;
  3350. }
  3351. case 'START':
  3352. {
  3353. // #EXT-X-START
  3354. parsed.startTimeOffset = parseStartTimeOffset(attributes);
  3355. break;
  3356. }
  3357. }
  3358. }
  3359. }
  3360. // Filter out levels with unknown codecs if it does not remove all levels
  3361. var stripUnknownCodecLevels = levelsWithKnownCodecs.length > 0 && levelsWithKnownCodecs.length < parsed.levels.length;
  3362. parsed.levels = stripUnknownCodecLevels ? levelsWithKnownCodecs : parsed.levels;
  3363. if (parsed.levels.length === 0) {
  3364. parsed.playlistParsingError = new Error('no levels found in manifest');
  3365. }
  3366. return parsed;
  3367. };
  3368. M3U8Parser.parseMasterPlaylistMedia = function parseMasterPlaylistMedia(string, baseurl, parsed) {
  3369. var result;
  3370. var results = {};
  3371. var levels = parsed.levels;
  3372. var groupsByType = {
  3373. AUDIO: levels.map(function (level) {
  3374. return {
  3375. id: level.attrs.AUDIO,
  3376. audioCodec: level.audioCodec
  3377. };
  3378. }),
  3379. SUBTITLES: levels.map(function (level) {
  3380. return {
  3381. id: level.attrs.SUBTITLES,
  3382. textCodec: level.textCodec
  3383. };
  3384. }),
  3385. 'CLOSED-CAPTIONS': []
  3386. };
  3387. var id = 0;
  3388. MASTER_PLAYLIST_MEDIA_REGEX.lastIndex = 0;
  3389. while ((result = MASTER_PLAYLIST_MEDIA_REGEX.exec(string)) !== null) {
  3390. var attrs = new AttrList(result[1]);
  3391. var type = attrs.TYPE;
  3392. if (type) {
  3393. var groups = groupsByType[type];
  3394. var medias = results[type] || [];
  3395. results[type] = medias;
  3396. {
  3397. substituteVariablesInAttributes(parsed, attrs, ['URI', 'GROUP-ID', 'LANGUAGE', 'ASSOC-LANGUAGE', 'STABLE-RENDITION-ID', 'NAME', 'INSTREAM-ID', 'CHARACTERISTICS', 'CHANNELS']);
  3398. }
  3399. var lang = attrs.LANGUAGE;
  3400. var assocLang = attrs['ASSOC-LANGUAGE'];
  3401. var channels = attrs.CHANNELS;
  3402. var characteristics = attrs.CHARACTERISTICS;
  3403. var instreamId = attrs['INSTREAM-ID'];
  3404. var media = {
  3405. attrs: attrs,
  3406. bitrate: 0,
  3407. id: id++,
  3408. groupId: attrs['GROUP-ID'] || '',
  3409. name: attrs.NAME || lang || '',
  3410. type: type,
  3411. default: attrs.bool('DEFAULT'),
  3412. autoselect: attrs.bool('AUTOSELECT'),
  3413. forced: attrs.bool('FORCED'),
  3414. lang: lang,
  3415. url: attrs.URI ? M3U8Parser.resolve(attrs.URI, baseurl) : ''
  3416. };
  3417. if (assocLang) {
  3418. media.assocLang = assocLang;
  3419. }
  3420. if (channels) {
  3421. media.channels = channels;
  3422. }
  3423. if (characteristics) {
  3424. media.characteristics = characteristics;
  3425. }
  3426. if (instreamId) {
  3427. media.instreamId = instreamId;
  3428. }
  3429. if (groups != null && groups.length) {
  3430. // If there are audio or text groups signalled in the manifest, let's look for a matching codec string for this track
  3431. // If we don't find the track signalled, lets use the first audio groups codec we have
  3432. // Acting as a best guess
  3433. var groupCodec = M3U8Parser.findGroup(groups, media.groupId) || groups[0];
  3434. assignCodec(media, groupCodec, 'audioCodec');
  3435. assignCodec(media, groupCodec, 'textCodec');
  3436. }
  3437. medias.push(media);
  3438. }
  3439. }
  3440. return results;
  3441. };
  3442. M3U8Parser.parseLevelPlaylist = function parseLevelPlaylist(string, baseurl, id, type, levelUrlId, multivariantVariableList) {
  3443. var level = new LevelDetails(baseurl);
  3444. var fragments = level.fragments;
  3445. // The most recent init segment seen (applies to all subsequent segments)
  3446. var currentInitSegment = null;
  3447. var currentSN = 0;
  3448. var currentPart = 0;
  3449. var totalduration = 0;
  3450. var discontinuityCounter = 0;
  3451. var prevFrag = null;
  3452. var frag = new Fragment(type, baseurl);
  3453. var result;
  3454. var i;
  3455. var levelkeys;
  3456. var firstPdtIndex = -1;
  3457. var createNextFrag = false;
  3458. var nextByteRange = null;
  3459. LEVEL_PLAYLIST_REGEX_FAST.lastIndex = 0;
  3460. level.m3u8 = string;
  3461. level.hasVariableRefs = hasVariableReferences(string) ;
  3462. while ((result = LEVEL_PLAYLIST_REGEX_FAST.exec(string)) !== null) {
  3463. if (createNextFrag) {
  3464. createNextFrag = false;
  3465. frag = new Fragment(type, baseurl);
  3466. // setup the next fragment for part loading
  3467. frag.start = totalduration;
  3468. frag.sn = currentSN;
  3469. frag.cc = discontinuityCounter;
  3470. frag.level = id;
  3471. if (currentInitSegment) {
  3472. frag.initSegment = currentInitSegment;
  3473. frag.rawProgramDateTime = currentInitSegment.rawProgramDateTime;
  3474. currentInitSegment.rawProgramDateTime = null;
  3475. if (nextByteRange) {
  3476. frag.setByteRange(nextByteRange);
  3477. nextByteRange = null;
  3478. }
  3479. }
  3480. }
  3481. var duration = result[1];
  3482. if (duration) {
  3483. // INF
  3484. frag.duration = parseFloat(duration);
  3485. // avoid sliced strings https://github.com/video-dev/hls.js/issues/939
  3486. var title = (' ' + result[2]).slice(1);
  3487. frag.title = title || null;
  3488. frag.tagList.push(title ? ['INF', duration, title] : ['INF', duration]);
  3489. } else if (result[3]) {
  3490. // url
  3491. if (isFiniteNumber(frag.duration)) {
  3492. frag.start = totalduration;
  3493. if (levelkeys) {
  3494. setFragLevelKeys(frag, levelkeys, level);
  3495. }
  3496. frag.sn = currentSN;
  3497. frag.level = id;
  3498. frag.cc = discontinuityCounter;
  3499. fragments.push(frag);
  3500. // avoid sliced strings https://github.com/video-dev/hls.js/issues/939
  3501. var uri = (' ' + result[3]).slice(1);
  3502. frag.relurl = substituteVariables(level, uri) ;
  3503. assignProgramDateTime(frag, prevFrag);
  3504. prevFrag = frag;
  3505. totalduration += frag.duration;
  3506. currentSN++;
  3507. currentPart = 0;
  3508. createNextFrag = true;
  3509. }
  3510. } else if (result[4]) {
  3511. // X-BYTERANGE
  3512. var data = (' ' + result[4]).slice(1);
  3513. if (prevFrag) {
  3514. frag.setByteRange(data, prevFrag);
  3515. } else {
  3516. frag.setByteRange(data);
  3517. }
  3518. } else if (result[5]) {
  3519. // PROGRAM-DATE-TIME
  3520. // avoid sliced strings https://github.com/video-dev/hls.js/issues/939
  3521. frag.rawProgramDateTime = (' ' + result[5]).slice(1);
  3522. frag.tagList.push(['PROGRAM-DATE-TIME', frag.rawProgramDateTime]);
  3523. if (firstPdtIndex === -1) {
  3524. firstPdtIndex = fragments.length;
  3525. }
  3526. } else {
  3527. result = result[0].match(LEVEL_PLAYLIST_REGEX_SLOW);
  3528. if (!result) {
  3529. logger.warn('No matches on slow regex match for level playlist!');
  3530. continue;
  3531. }
  3532. for (i = 1; i < result.length; i++) {
  3533. if (typeof result[i] !== 'undefined') {
  3534. break;
  3535. }
  3536. }
  3537. // avoid sliced strings https://github.com/video-dev/hls.js/issues/939
  3538. var tag = (' ' + result[i]).slice(1);
  3539. var value1 = (' ' + result[i + 1]).slice(1);
  3540. var value2 = result[i + 2] ? (' ' + result[i + 2]).slice(1) : '';
  3541. switch (tag) {
  3542. case 'PLAYLIST-TYPE':
  3543. level.type = value1.toUpperCase();
  3544. break;
  3545. case 'MEDIA-SEQUENCE':
  3546. currentSN = level.startSN = parseInt(value1);
  3547. break;
  3548. case 'SKIP':
  3549. {
  3550. var skipAttrs = new AttrList(value1);
  3551. {
  3552. substituteVariablesInAttributes(level, skipAttrs, ['RECENTLY-REMOVED-DATERANGES']);
  3553. }
  3554. var skippedSegments = skipAttrs.decimalInteger('SKIPPED-SEGMENTS');
  3555. if (isFiniteNumber(skippedSegments)) {
  3556. level.skippedSegments = skippedSegments;
  3557. // This will result in fragments[] containing undefined values, which we will fill in with `mergeDetails`
  3558. for (var _i = skippedSegments; _i--;) {
  3559. fragments.unshift(null);
  3560. }
  3561. currentSN += skippedSegments;
  3562. }
  3563. var recentlyRemovedDateranges = skipAttrs.enumeratedString('RECENTLY-REMOVED-DATERANGES');
  3564. if (recentlyRemovedDateranges) {
  3565. level.recentlyRemovedDateranges = recentlyRemovedDateranges.split('\t');
  3566. }
  3567. break;
  3568. }
  3569. case 'TARGETDURATION':
  3570. level.targetduration = Math.max(parseInt(value1), 1);
  3571. break;
  3572. case 'VERSION':
  3573. level.version = parseInt(value1);
  3574. break;
  3575. case 'INDEPENDENT-SEGMENTS':
  3576. case 'EXTM3U':
  3577. break;
  3578. case 'ENDLIST':
  3579. level.live = false;
  3580. break;
  3581. case '#':
  3582. if (value1 || value2) {
  3583. frag.tagList.push(value2 ? [value1, value2] : [value1]);
  3584. }
  3585. break;
  3586. case 'DISCONTINUITY':
  3587. discontinuityCounter++;
  3588. frag.tagList.push(['DIS']);
  3589. break;
  3590. case 'GAP':
  3591. frag.gap = true;
  3592. frag.tagList.push([tag]);
  3593. break;
  3594. case 'BITRATE':
  3595. frag.tagList.push([tag, value1]);
  3596. break;
  3597. case 'DATERANGE':
  3598. {
  3599. var dateRangeAttr = new AttrList(value1);
  3600. {
  3601. substituteVariablesInAttributes(level, dateRangeAttr, ['ID', 'CLASS', 'START-DATE', 'END-DATE', 'SCTE35-CMD', 'SCTE35-OUT', 'SCTE35-IN']);
  3602. substituteVariablesInAttributes(level, dateRangeAttr, dateRangeAttr.clientAttrs);
  3603. }
  3604. var dateRange = new DateRange(dateRangeAttr, level.dateRanges[dateRangeAttr.ID]);
  3605. if (dateRange.isValid || level.skippedSegments) {
  3606. level.dateRanges[dateRange.id] = dateRange;
  3607. } else {
  3608. logger.warn("Ignoring invalid DATERANGE tag: \"" + value1 + "\"");
  3609. }
  3610. // Add to fragment tag list for backwards compatibility (< v1.2.0)
  3611. frag.tagList.push(['EXT-X-DATERANGE', value1]);
  3612. break;
  3613. }
  3614. case 'DEFINE':
  3615. {
  3616. {
  3617. var variableAttributes = new AttrList(value1);
  3618. substituteVariablesInAttributes(level, variableAttributes, ['NAME', 'VALUE', 'IMPORT', 'QUERYPARAM']);
  3619. if ('IMPORT' in variableAttributes) {
  3620. importVariableDefinition(level, variableAttributes, multivariantVariableList);
  3621. } else {
  3622. addVariableDefinition(level, variableAttributes, baseurl);
  3623. }
  3624. }
  3625. break;
  3626. }
  3627. case 'DISCONTINUITY-SEQUENCE':
  3628. discontinuityCounter = parseInt(value1);
  3629. break;
  3630. case 'KEY':
  3631. {
  3632. var levelKey = parseKey(value1, baseurl, level);
  3633. if (levelKey.isSupported()) {
  3634. if (levelKey.method === 'NONE') {
  3635. levelkeys = undefined;
  3636. break;
  3637. }
  3638. if (!levelkeys) {
  3639. levelkeys = {};
  3640. }
  3641. if (levelkeys[levelKey.keyFormat]) {
  3642. levelkeys = _extends({}, levelkeys);
  3643. }
  3644. levelkeys[levelKey.keyFormat] = levelKey;
  3645. } else {
  3646. logger.warn("[Keys] Ignoring invalid EXT-X-KEY tag: \"" + value1 + "\"");
  3647. }
  3648. break;
  3649. }
  3650. case 'START':
  3651. level.startTimeOffset = parseStartTimeOffset(value1);
  3652. break;
  3653. case 'MAP':
  3654. {
  3655. var mapAttrs = new AttrList(value1);
  3656. {
  3657. substituteVariablesInAttributes(level, mapAttrs, ['BYTERANGE', 'URI']);
  3658. }
  3659. if (frag.duration) {
  3660. // Initial segment tag is after segment duration tag.
  3661. // #EXTINF: 6.0
  3662. // #EXT-X-MAP:URI="init.mp4
  3663. var init = new Fragment(type, baseurl);
  3664. setInitSegment(init, mapAttrs, id, levelkeys);
  3665. currentInitSegment = init;
  3666. frag.initSegment = currentInitSegment;
  3667. if (currentInitSegment.rawProgramDateTime && !frag.rawProgramDateTime) {
  3668. frag.rawProgramDateTime = currentInitSegment.rawProgramDateTime;
  3669. }
  3670. } else {
  3671. // Initial segment tag is before segment duration tag
  3672. // Handle case where EXT-X-MAP is declared after EXT-X-BYTERANGE
  3673. var end = frag.byteRangeEndOffset;
  3674. if (end) {
  3675. var start = frag.byteRangeStartOffset;
  3676. nextByteRange = end - start + "@" + start;
  3677. } else {
  3678. nextByteRange = null;
  3679. }
  3680. setInitSegment(frag, mapAttrs, id, levelkeys);
  3681. currentInitSegment = frag;
  3682. createNextFrag = true;
  3683. }
  3684. break;
  3685. }
  3686. case 'SERVER-CONTROL':
  3687. {
  3688. var serverControlAttrs = new AttrList(value1);
  3689. level.canBlockReload = serverControlAttrs.bool('CAN-BLOCK-RELOAD');
  3690. level.canSkipUntil = serverControlAttrs.optionalFloat('CAN-SKIP-UNTIL', 0);
  3691. level.canSkipDateRanges = level.canSkipUntil > 0 && serverControlAttrs.bool('CAN-SKIP-DATERANGES');
  3692. level.partHoldBack = serverControlAttrs.optionalFloat('PART-HOLD-BACK', 0);
  3693. level.holdBack = serverControlAttrs.optionalFloat('HOLD-BACK', 0);
  3694. break;
  3695. }
  3696. case 'PART-INF':
  3697. {
  3698. var partInfAttrs = new AttrList(value1);
  3699. level.partTarget = partInfAttrs.decimalFloatingPoint('PART-TARGET');
  3700. break;
  3701. }
  3702. case 'PART':
  3703. {
  3704. var partList = level.partList;
  3705. if (!partList) {
  3706. partList = level.partList = [];
  3707. }
  3708. var previousFragmentPart = currentPart > 0 ? partList[partList.length - 1] : undefined;
  3709. var index = currentPart++;
  3710. var partAttrs = new AttrList(value1);
  3711. {
  3712. substituteVariablesInAttributes(level, partAttrs, ['BYTERANGE', 'URI']);
  3713. }
  3714. var part = new Part(partAttrs, frag, baseurl, index, previousFragmentPart);
  3715. partList.push(part);
  3716. frag.duration += part.duration;
  3717. break;
  3718. }
  3719. case 'PRELOAD-HINT':
  3720. {
  3721. var preloadHintAttrs = new AttrList(value1);
  3722. {
  3723. substituteVariablesInAttributes(level, preloadHintAttrs, ['URI']);
  3724. }
  3725. level.preloadHint = preloadHintAttrs;
  3726. break;
  3727. }
  3728. case 'RENDITION-REPORT':
  3729. {
  3730. var renditionReportAttrs = new AttrList(value1);
  3731. {
  3732. substituteVariablesInAttributes(level, renditionReportAttrs, ['URI']);
  3733. }
  3734. level.renditionReports = level.renditionReports || [];
  3735. level.renditionReports.push(renditionReportAttrs);
  3736. break;
  3737. }
  3738. default:
  3739. logger.warn("line parsed but not handled: " + result);
  3740. break;
  3741. }
  3742. }
  3743. }
  3744. if (prevFrag && !prevFrag.relurl) {
  3745. fragments.pop();
  3746. totalduration -= prevFrag.duration;
  3747. if (level.partList) {
  3748. level.fragmentHint = prevFrag;
  3749. }
  3750. } else if (level.partList) {
  3751. assignProgramDateTime(frag, prevFrag);
  3752. frag.cc = discontinuityCounter;
  3753. level.fragmentHint = frag;
  3754. if (levelkeys) {
  3755. setFragLevelKeys(frag, levelkeys, level);
  3756. }
  3757. }
  3758. var fragmentLength = fragments.length;
  3759. var firstFragment = fragments[0];
  3760. var lastFragment = fragments[fragmentLength - 1];
  3761. totalduration += level.skippedSegments * level.targetduration;
  3762. if (totalduration > 0 && fragmentLength && lastFragment) {
  3763. level.averagetargetduration = totalduration / fragmentLength;
  3764. var lastSn = lastFragment.sn;
  3765. level.endSN = lastSn !== 'initSegment' ? lastSn : 0;
  3766. if (!level.live) {
  3767. lastFragment.endList = true;
  3768. }
  3769. if (firstFragment) {
  3770. level.startCC = firstFragment.cc;
  3771. }
  3772. } else {
  3773. level.endSN = 0;
  3774. level.startCC = 0;
  3775. }
  3776. if (level.fragmentHint) {
  3777. totalduration += level.fragmentHint.duration;
  3778. }
  3779. level.totalduration = totalduration;
  3780. level.endCC = discontinuityCounter;
  3781. /**
  3782. * Backfill any missing PDT values
  3783. * "If the first EXT-X-PROGRAM-DATE-TIME tag in a Playlist appears after
  3784. * one or more Media Segment URIs, the client SHOULD extrapolate
  3785. * backward from that tag (using EXTINF durations and/or media
  3786. * timestamps) to associate dates with those segments."
  3787. * We have already extrapolated forward, but all fragments up to the first instance of PDT do not have their PDTs
  3788. * computed.
  3789. */
  3790. if (firstPdtIndex > 0) {
  3791. backfillProgramDateTimes(fragments, firstPdtIndex);
  3792. }
  3793. return level;
  3794. };
  3795. return M3U8Parser;
  3796. }();
  3797. function parseKey(keyTagAttributes, baseurl, parsed) {
  3798. var _keyAttrs$METHOD, _keyAttrs$KEYFORMAT;
  3799. // https://tools.ietf.org/html/rfc8216#section-4.3.2.4
  3800. var keyAttrs = new AttrList(keyTagAttributes);
  3801. {
  3802. substituteVariablesInAttributes(parsed, keyAttrs, ['KEYFORMAT', 'KEYFORMATVERSIONS', 'URI', 'IV', 'URI']);
  3803. }
  3804. var decryptmethod = (_keyAttrs$METHOD = keyAttrs.METHOD) != null ? _keyAttrs$METHOD : '';
  3805. var decrypturi = keyAttrs.URI;
  3806. var decryptiv = keyAttrs.hexadecimalInteger('IV');
  3807. var decryptkeyformatversions = keyAttrs.KEYFORMATVERSIONS;
  3808. // From RFC: This attribute is OPTIONAL; its absence indicates an implicit value of "identity".
  3809. var decryptkeyformat = (_keyAttrs$KEYFORMAT = keyAttrs.KEYFORMAT) != null ? _keyAttrs$KEYFORMAT : 'identity';
  3810. if (decrypturi && keyAttrs.IV && !decryptiv) {
  3811. logger.error("Invalid IV: " + keyAttrs.IV);
  3812. }
  3813. // If decrypturi is a URI with a scheme, then baseurl will be ignored
  3814. // No uri is allowed when METHOD is NONE
  3815. var resolvedUri = decrypturi ? M3U8Parser.resolve(decrypturi, baseurl) : '';
  3816. var keyFormatVersions = (decryptkeyformatversions ? decryptkeyformatversions : '1').split('/').map(Number).filter(Number.isFinite);
  3817. return new LevelKey(decryptmethod, resolvedUri, decryptkeyformat, keyFormatVersions, decryptiv);
  3818. }
  3819. function parseStartTimeOffset(startAttributes) {
  3820. var startAttrs = new AttrList(startAttributes);
  3821. var startTimeOffset = startAttrs.decimalFloatingPoint('TIME-OFFSET');
  3822. if (isFiniteNumber(startTimeOffset)) {
  3823. return startTimeOffset;
  3824. }
  3825. return null;
  3826. }
  3827. function setCodecs(codecsAttributeValue, level) {
  3828. var codecs = (codecsAttributeValue || '').split(/[ ,]+/).filter(function (c) {
  3829. return c;
  3830. });
  3831. ['video', 'audio', 'text'].forEach(function (type) {
  3832. var filtered = codecs.filter(function (codec) {
  3833. return isCodecType(codec, type);
  3834. });
  3835. if (filtered.length) {
  3836. // Comma separated list of all codecs for type
  3837. level[type + "Codec"] = filtered.join(',');
  3838. // Remove known codecs so that only unknownCodecs are left after iterating through each type
  3839. codecs = codecs.filter(function (codec) {
  3840. return filtered.indexOf(codec) === -1;
  3841. });
  3842. }
  3843. });
  3844. level.unknownCodecs = codecs;
  3845. }
  3846. function assignCodec(media, groupItem, codecProperty) {
  3847. var codecValue = groupItem[codecProperty];
  3848. if (codecValue) {
  3849. media[codecProperty] = codecValue;
  3850. }
  3851. }
  3852. function backfillProgramDateTimes(fragments, firstPdtIndex) {
  3853. var fragPrev = fragments[firstPdtIndex];
  3854. for (var i = firstPdtIndex; i--;) {
  3855. var frag = fragments[i];
  3856. // Exit on delta-playlist skipped segments
  3857. if (!frag) {
  3858. return;
  3859. }
  3860. frag.programDateTime = fragPrev.programDateTime - frag.duration * 1000;
  3861. fragPrev = frag;
  3862. }
  3863. }
  3864. function assignProgramDateTime(frag, prevFrag) {
  3865. if (frag.rawProgramDateTime) {
  3866. frag.programDateTime = Date.parse(frag.rawProgramDateTime);
  3867. } else if (prevFrag != null && prevFrag.programDateTime) {
  3868. frag.programDateTime = prevFrag.endProgramDateTime;
  3869. }
  3870. if (!isFiniteNumber(frag.programDateTime)) {
  3871. frag.programDateTime = null;
  3872. frag.rawProgramDateTime = null;
  3873. }
  3874. }
  3875. function setInitSegment(frag, mapAttrs, id, levelkeys) {
  3876. frag.relurl = mapAttrs.URI;
  3877. if (mapAttrs.BYTERANGE) {
  3878. frag.setByteRange(mapAttrs.BYTERANGE);
  3879. }
  3880. frag.level = id;
  3881. frag.sn = 'initSegment';
  3882. if (levelkeys) {
  3883. frag.levelkeys = levelkeys;
  3884. }
  3885. frag.initSegment = null;
  3886. }
  3887. function setFragLevelKeys(frag, levelkeys, level) {
  3888. frag.levelkeys = levelkeys;
  3889. var encryptedFragments = level.encryptedFragments;
  3890. if ((!encryptedFragments.length || encryptedFragments[encryptedFragments.length - 1].levelkeys !== levelkeys) && Object.keys(levelkeys).some(function (format) {
  3891. return levelkeys[format].isCommonEncryption;
  3892. })) {
  3893. encryptedFragments.push(frag);
  3894. }
  3895. }
  3896. var PlaylistContextType = {
  3897. MANIFEST: "manifest",
  3898. LEVEL: "level",
  3899. AUDIO_TRACK: "audioTrack",
  3900. SUBTITLE_TRACK: "subtitleTrack"
  3901. };
  3902. var PlaylistLevelType = {
  3903. MAIN: "main",
  3904. AUDIO: "audio",
  3905. SUBTITLE: "subtitle"
  3906. };
  3907. function mapContextToLevelType(context) {
  3908. var type = context.type;
  3909. switch (type) {
  3910. case PlaylistContextType.AUDIO_TRACK:
  3911. return PlaylistLevelType.AUDIO;
  3912. case PlaylistContextType.SUBTITLE_TRACK:
  3913. return PlaylistLevelType.SUBTITLE;
  3914. default:
  3915. return PlaylistLevelType.MAIN;
  3916. }
  3917. }
  3918. function getResponseUrl(response, context) {
  3919. var url = response.url;
  3920. // responseURL not supported on some browsers (it is used to detect URL redirection)
  3921. // data-uri mode also not supported (but no need to detect redirection)
  3922. if (url === undefined || url.indexOf('data:') === 0) {
  3923. // fallback to initial URL
  3924. url = context.url;
  3925. }
  3926. return url;
  3927. }
  3928. var PlaylistLoader = /*#__PURE__*/function () {
  3929. function PlaylistLoader(hls) {
  3930. this.hls = void 0;
  3931. this.loaders = Object.create(null);
  3932. this.variableList = null;
  3933. this.hls = hls;
  3934. this.registerListeners();
  3935. }
  3936. var _proto = PlaylistLoader.prototype;
  3937. _proto.startLoad = function startLoad(startPosition) {};
  3938. _proto.stopLoad = function stopLoad() {
  3939. this.destroyInternalLoaders();
  3940. };
  3941. _proto.registerListeners = function registerListeners() {
  3942. var hls = this.hls;
  3943. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  3944. hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
  3945. hls.on(Events.AUDIO_TRACK_LOADING, this.onAudioTrackLoading, this);
  3946. hls.on(Events.SUBTITLE_TRACK_LOADING, this.onSubtitleTrackLoading, this);
  3947. };
  3948. _proto.unregisterListeners = function unregisterListeners() {
  3949. var hls = this.hls;
  3950. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  3951. hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
  3952. hls.off(Events.AUDIO_TRACK_LOADING, this.onAudioTrackLoading, this);
  3953. hls.off(Events.SUBTITLE_TRACK_LOADING, this.onSubtitleTrackLoading, this);
  3954. }
  3955. /**
  3956. * Returns defaults or configured loader-type overloads (pLoader and loader config params)
  3957. */;
  3958. _proto.createInternalLoader = function createInternalLoader(context) {
  3959. var config = this.hls.config;
  3960. var PLoader = config.pLoader;
  3961. var Loader = config.loader;
  3962. var InternalLoader = PLoader || Loader;
  3963. var loader = new InternalLoader(config);
  3964. this.loaders[context.type] = loader;
  3965. return loader;
  3966. };
  3967. _proto.getInternalLoader = function getInternalLoader(context) {
  3968. return this.loaders[context.type];
  3969. };
  3970. _proto.resetInternalLoader = function resetInternalLoader(contextType) {
  3971. if (this.loaders[contextType]) {
  3972. delete this.loaders[contextType];
  3973. }
  3974. }
  3975. /**
  3976. * Call `destroy` on all internal loader instances mapped (one per context type)
  3977. */;
  3978. _proto.destroyInternalLoaders = function destroyInternalLoaders() {
  3979. for (var contextType in this.loaders) {
  3980. var loader = this.loaders[contextType];
  3981. if (loader) {
  3982. loader.destroy();
  3983. }
  3984. this.resetInternalLoader(contextType);
  3985. }
  3986. };
  3987. _proto.destroy = function destroy() {
  3988. this.variableList = null;
  3989. this.unregisterListeners();
  3990. this.destroyInternalLoaders();
  3991. };
  3992. _proto.onManifestLoading = function onManifestLoading(event, data) {
  3993. var url = data.url;
  3994. this.variableList = null;
  3995. this.load({
  3996. id: null,
  3997. level: 0,
  3998. responseType: 'text',
  3999. type: PlaylistContextType.MANIFEST,
  4000. url: url,
  4001. deliveryDirectives: null
  4002. });
  4003. };
  4004. _proto.onLevelLoading = function onLevelLoading(event, data) {
  4005. var id = data.id,
  4006. level = data.level,
  4007. pathwayId = data.pathwayId,
  4008. url = data.url,
  4009. deliveryDirectives = data.deliveryDirectives;
  4010. this.load({
  4011. id: id,
  4012. level: level,
  4013. pathwayId: pathwayId,
  4014. responseType: 'text',
  4015. type: PlaylistContextType.LEVEL,
  4016. url: url,
  4017. deliveryDirectives: deliveryDirectives
  4018. });
  4019. };
  4020. _proto.onAudioTrackLoading = function onAudioTrackLoading(event, data) {
  4021. var id = data.id,
  4022. groupId = data.groupId,
  4023. url = data.url,
  4024. deliveryDirectives = data.deliveryDirectives;
  4025. this.load({
  4026. id: id,
  4027. groupId: groupId,
  4028. level: null,
  4029. responseType: 'text',
  4030. type: PlaylistContextType.AUDIO_TRACK,
  4031. url: url,
  4032. deliveryDirectives: deliveryDirectives
  4033. });
  4034. };
  4035. _proto.onSubtitleTrackLoading = function onSubtitleTrackLoading(event, data) {
  4036. var id = data.id,
  4037. groupId = data.groupId,
  4038. url = data.url,
  4039. deliveryDirectives = data.deliveryDirectives;
  4040. this.load({
  4041. id: id,
  4042. groupId: groupId,
  4043. level: null,
  4044. responseType: 'text',
  4045. type: PlaylistContextType.SUBTITLE_TRACK,
  4046. url: url,
  4047. deliveryDirectives: deliveryDirectives
  4048. });
  4049. };
  4050. _proto.load = function load(context) {
  4051. var _context$deliveryDire,
  4052. _this = this;
  4053. var config = this.hls.config;
  4054. // logger.debug(`[playlist-loader]: Loading playlist of type ${context.type}, level: ${context.level}, id: ${context.id}`);
  4055. // Check if a loader for this context already exists
  4056. var loader = this.getInternalLoader(context);
  4057. if (loader) {
  4058. var loaderContext = loader.context;
  4059. if (loaderContext && loaderContext.url === context.url && loaderContext.level === context.level) {
  4060. // same URL can't overlap
  4061. logger.trace('[playlist-loader]: playlist request ongoing');
  4062. return;
  4063. }
  4064. logger.log("[playlist-loader]: aborting previous loader for type: " + context.type);
  4065. loader.abort();
  4066. }
  4067. // apply different configs for retries depending on
  4068. // context (manifest, level, audio/subs playlist)
  4069. var loadPolicy;
  4070. if (context.type === PlaylistContextType.MANIFEST) {
  4071. loadPolicy = config.manifestLoadPolicy.default;
  4072. } else {
  4073. loadPolicy = _extends({}, config.playlistLoadPolicy.default, {
  4074. timeoutRetry: null,
  4075. errorRetry: null
  4076. });
  4077. }
  4078. loader = this.createInternalLoader(context);
  4079. // Override level/track timeout for LL-HLS requests
  4080. // (the default of 10000ms is counter productive to blocking playlist reload requests)
  4081. if (isFiniteNumber((_context$deliveryDire = context.deliveryDirectives) == null ? void 0 : _context$deliveryDire.part)) {
  4082. var levelDetails;
  4083. if (context.type === PlaylistContextType.LEVEL && context.level !== null) {
  4084. levelDetails = this.hls.levels[context.level].details;
  4085. } else if (context.type === PlaylistContextType.AUDIO_TRACK && context.id !== null) {
  4086. levelDetails = this.hls.audioTracks[context.id].details;
  4087. } else if (context.type === PlaylistContextType.SUBTITLE_TRACK && context.id !== null) {
  4088. levelDetails = this.hls.subtitleTracks[context.id].details;
  4089. }
  4090. if (levelDetails) {
  4091. var partTarget = levelDetails.partTarget;
  4092. var targetDuration = levelDetails.targetduration;
  4093. if (partTarget && targetDuration) {
  4094. var maxLowLatencyPlaylistRefresh = Math.max(partTarget * 3, targetDuration * 0.8) * 1000;
  4095. loadPolicy = _extends({}, loadPolicy, {
  4096. maxTimeToFirstByteMs: Math.min(maxLowLatencyPlaylistRefresh, loadPolicy.maxTimeToFirstByteMs),
  4097. maxLoadTimeMs: Math.min(maxLowLatencyPlaylistRefresh, loadPolicy.maxTimeToFirstByteMs)
  4098. });
  4099. }
  4100. }
  4101. }
  4102. var legacyRetryCompatibility = loadPolicy.errorRetry || loadPolicy.timeoutRetry || {};
  4103. var loaderConfig = {
  4104. loadPolicy: loadPolicy,
  4105. timeout: loadPolicy.maxLoadTimeMs,
  4106. maxRetry: legacyRetryCompatibility.maxNumRetry || 0,
  4107. retryDelay: legacyRetryCompatibility.retryDelayMs || 0,
  4108. maxRetryDelay: legacyRetryCompatibility.maxRetryDelayMs || 0
  4109. };
  4110. var loaderCallbacks = {
  4111. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  4112. var loader = _this.getInternalLoader(context);
  4113. _this.resetInternalLoader(context.type);
  4114. var string = response.data;
  4115. // Validate if it is an M3U8 at all
  4116. if (string.indexOf('#EXTM3U') !== 0) {
  4117. _this.handleManifestParsingError(response, context, new Error('no EXTM3U delimiter'), networkDetails || null, stats);
  4118. return;
  4119. }
  4120. stats.parsing.start = performance.now();
  4121. if (M3U8Parser.isMediaPlaylist(string)) {
  4122. _this.handleTrackOrLevelPlaylist(response, stats, context, networkDetails || null, loader);
  4123. } else {
  4124. _this.handleMasterPlaylist(response, stats, context, networkDetails);
  4125. }
  4126. },
  4127. onError: function onError(response, context, networkDetails, stats) {
  4128. _this.handleNetworkError(context, networkDetails, false, response, stats);
  4129. },
  4130. onTimeout: function onTimeout(stats, context, networkDetails) {
  4131. _this.handleNetworkError(context, networkDetails, true, undefined, stats);
  4132. }
  4133. };
  4134. // logger.debug(`[playlist-loader]: Calling internal loader delegate for URL: ${context.url}`);
  4135. loader.load(context, loaderConfig, loaderCallbacks);
  4136. };
  4137. _proto.handleMasterPlaylist = function handleMasterPlaylist(response, stats, context, networkDetails) {
  4138. var hls = this.hls;
  4139. var string = response.data;
  4140. var url = getResponseUrl(response, context);
  4141. var parsedResult = M3U8Parser.parseMasterPlaylist(string, url);
  4142. if (parsedResult.playlistParsingError) {
  4143. this.handleManifestParsingError(response, context, parsedResult.playlistParsingError, networkDetails, stats);
  4144. return;
  4145. }
  4146. var contentSteering = parsedResult.contentSteering,
  4147. levels = parsedResult.levels,
  4148. sessionData = parsedResult.sessionData,
  4149. sessionKeys = parsedResult.sessionKeys,
  4150. startTimeOffset = parsedResult.startTimeOffset,
  4151. variableList = parsedResult.variableList;
  4152. this.variableList = variableList;
  4153. var _M3U8Parser$parseMast = M3U8Parser.parseMasterPlaylistMedia(string, url, parsedResult),
  4154. _M3U8Parser$parseMast2 = _M3U8Parser$parseMast.AUDIO,
  4155. audioTracks = _M3U8Parser$parseMast2 === void 0 ? [] : _M3U8Parser$parseMast2,
  4156. subtitles = _M3U8Parser$parseMast.SUBTITLES,
  4157. captions = _M3U8Parser$parseMast['CLOSED-CAPTIONS'];
  4158. if (audioTracks.length) {
  4159. // check if we have found an audio track embedded in main playlist (audio track without URI attribute)
  4160. var embeddedAudioFound = audioTracks.some(function (audioTrack) {
  4161. return !audioTrack.url;
  4162. });
  4163. // if no embedded audio track defined, but audio codec signaled in quality level,
  4164. // we need to signal this main audio track this could happen with playlists with
  4165. // alt audio rendition in which quality levels (main)
  4166. // contains both audio+video. but with mixed audio track not signaled
  4167. if (!embeddedAudioFound && levels[0].audioCodec && !levels[0].attrs.AUDIO) {
  4168. logger.log('[playlist-loader]: audio codec signaled in quality level, but no embedded audio track signaled, create one');
  4169. audioTracks.unshift({
  4170. type: 'main',
  4171. name: 'main',
  4172. groupId: 'main',
  4173. default: false,
  4174. autoselect: false,
  4175. forced: false,
  4176. id: -1,
  4177. attrs: new AttrList({}),
  4178. bitrate: 0,
  4179. url: ''
  4180. });
  4181. }
  4182. }
  4183. hls.trigger(Events.MANIFEST_LOADED, {
  4184. levels: levels,
  4185. audioTracks: audioTracks,
  4186. subtitles: subtitles,
  4187. captions: captions,
  4188. contentSteering: contentSteering,
  4189. url: url,
  4190. stats: stats,
  4191. networkDetails: networkDetails,
  4192. sessionData: sessionData,
  4193. sessionKeys: sessionKeys,
  4194. startTimeOffset: startTimeOffset,
  4195. variableList: variableList
  4196. });
  4197. };
  4198. _proto.handleTrackOrLevelPlaylist = function handleTrackOrLevelPlaylist(response, stats, context, networkDetails, loader) {
  4199. var hls = this.hls;
  4200. var id = context.id,
  4201. level = context.level,
  4202. type = context.type;
  4203. var url = getResponseUrl(response, context);
  4204. var levelUrlId = 0;
  4205. var levelId = isFiniteNumber(level) ? level : isFiniteNumber(id) ? id : 0;
  4206. var levelType = mapContextToLevelType(context);
  4207. var levelDetails = M3U8Parser.parseLevelPlaylist(response.data, url, levelId, levelType, levelUrlId, this.variableList);
  4208. // We have done our first request (Manifest-type) and receive
  4209. // not a master playlist but a chunk-list (track/level)
  4210. // We fire the manifest-loaded event anyway with the parsed level-details
  4211. // by creating a single-level structure for it.
  4212. if (type === PlaylistContextType.MANIFEST) {
  4213. var singleLevel = {
  4214. attrs: new AttrList({}),
  4215. bitrate: 0,
  4216. details: levelDetails,
  4217. name: '',
  4218. url: url
  4219. };
  4220. hls.trigger(Events.MANIFEST_LOADED, {
  4221. levels: [singleLevel],
  4222. audioTracks: [],
  4223. url: url,
  4224. stats: stats,
  4225. networkDetails: networkDetails,
  4226. sessionData: null,
  4227. sessionKeys: null,
  4228. contentSteering: null,
  4229. startTimeOffset: null,
  4230. variableList: null
  4231. });
  4232. }
  4233. // save parsing time
  4234. stats.parsing.end = performance.now();
  4235. // extend the context with the new levelDetails property
  4236. context.levelDetails = levelDetails;
  4237. this.handlePlaylistLoaded(levelDetails, response, stats, context, networkDetails, loader);
  4238. };
  4239. _proto.handleManifestParsingError = function handleManifestParsingError(response, context, error, networkDetails, stats) {
  4240. this.hls.trigger(Events.ERROR, {
  4241. type: ErrorTypes.NETWORK_ERROR,
  4242. details: ErrorDetails.MANIFEST_PARSING_ERROR,
  4243. fatal: context.type === PlaylistContextType.MANIFEST,
  4244. url: response.url,
  4245. err: error,
  4246. error: error,
  4247. reason: error.message,
  4248. response: response,
  4249. context: context,
  4250. networkDetails: networkDetails,
  4251. stats: stats
  4252. });
  4253. };
  4254. _proto.handleNetworkError = function handleNetworkError(context, networkDetails, timeout, response, stats) {
  4255. if (timeout === void 0) {
  4256. timeout = false;
  4257. }
  4258. var message = "A network " + (timeout ? 'timeout' : 'error' + (response ? ' (status ' + response.code + ')' : '')) + " occurred while loading " + context.type;
  4259. if (context.type === PlaylistContextType.LEVEL) {
  4260. message += ": " + context.level + " id: " + context.id;
  4261. } else if (context.type === PlaylistContextType.AUDIO_TRACK || context.type === PlaylistContextType.SUBTITLE_TRACK) {
  4262. message += " id: " + context.id + " group-id: \"" + context.groupId + "\"";
  4263. }
  4264. var error = new Error(message);
  4265. logger.warn("[playlist-loader]: " + message);
  4266. var details = ErrorDetails.UNKNOWN;
  4267. var fatal = false;
  4268. var loader = this.getInternalLoader(context);
  4269. switch (context.type) {
  4270. case PlaylistContextType.MANIFEST:
  4271. details = timeout ? ErrorDetails.MANIFEST_LOAD_TIMEOUT : ErrorDetails.MANIFEST_LOAD_ERROR;
  4272. fatal = true;
  4273. break;
  4274. case PlaylistContextType.LEVEL:
  4275. details = timeout ? ErrorDetails.LEVEL_LOAD_TIMEOUT : ErrorDetails.LEVEL_LOAD_ERROR;
  4276. fatal = false;
  4277. break;
  4278. case PlaylistContextType.AUDIO_TRACK:
  4279. details = timeout ? ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT : ErrorDetails.AUDIO_TRACK_LOAD_ERROR;
  4280. fatal = false;
  4281. break;
  4282. case PlaylistContextType.SUBTITLE_TRACK:
  4283. details = timeout ? ErrorDetails.SUBTITLE_TRACK_LOAD_TIMEOUT : ErrorDetails.SUBTITLE_LOAD_ERROR;
  4284. fatal = false;
  4285. break;
  4286. }
  4287. if (loader) {
  4288. this.resetInternalLoader(context.type);
  4289. }
  4290. var errorData = {
  4291. type: ErrorTypes.NETWORK_ERROR,
  4292. details: details,
  4293. fatal: fatal,
  4294. url: context.url,
  4295. loader: loader,
  4296. context: context,
  4297. error: error,
  4298. networkDetails: networkDetails,
  4299. stats: stats
  4300. };
  4301. if (response) {
  4302. var url = (networkDetails == null ? void 0 : networkDetails.url) || context.url;
  4303. errorData.response = _objectSpread2({
  4304. url: url,
  4305. data: undefined
  4306. }, response);
  4307. }
  4308. this.hls.trigger(Events.ERROR, errorData);
  4309. };
  4310. _proto.handlePlaylistLoaded = function handlePlaylistLoaded(levelDetails, response, stats, context, networkDetails, loader) {
  4311. var hls = this.hls;
  4312. var type = context.type,
  4313. level = context.level,
  4314. id = context.id,
  4315. groupId = context.groupId,
  4316. deliveryDirectives = context.deliveryDirectives;
  4317. var url = getResponseUrl(response, context);
  4318. var parent = mapContextToLevelType(context);
  4319. var levelIndex = typeof context.level === 'number' && parent === PlaylistLevelType.MAIN ? level : undefined;
  4320. if (!levelDetails.fragments.length) {
  4321. var _error = new Error('No Segments found in Playlist');
  4322. hls.trigger(Events.ERROR, {
  4323. type: ErrorTypes.NETWORK_ERROR,
  4324. details: ErrorDetails.LEVEL_EMPTY_ERROR,
  4325. fatal: false,
  4326. url: url,
  4327. error: _error,
  4328. reason: _error.message,
  4329. response: response,
  4330. context: context,
  4331. level: levelIndex,
  4332. parent: parent,
  4333. networkDetails: networkDetails,
  4334. stats: stats
  4335. });
  4336. return;
  4337. }
  4338. if (!levelDetails.targetduration) {
  4339. levelDetails.playlistParsingError = new Error('Missing Target Duration');
  4340. }
  4341. var error = levelDetails.playlistParsingError;
  4342. if (error) {
  4343. hls.trigger(Events.ERROR, {
  4344. type: ErrorTypes.NETWORK_ERROR,
  4345. details: ErrorDetails.LEVEL_PARSING_ERROR,
  4346. fatal: false,
  4347. url: url,
  4348. error: error,
  4349. reason: error.message,
  4350. response: response,
  4351. context: context,
  4352. level: levelIndex,
  4353. parent: parent,
  4354. networkDetails: networkDetails,
  4355. stats: stats
  4356. });
  4357. return;
  4358. }
  4359. if (levelDetails.live && loader) {
  4360. if (loader.getCacheAge) {
  4361. levelDetails.ageHeader = loader.getCacheAge() || 0;
  4362. }
  4363. if (!loader.getCacheAge || isNaN(levelDetails.ageHeader)) {
  4364. levelDetails.ageHeader = 0;
  4365. }
  4366. }
  4367. switch (type) {
  4368. case PlaylistContextType.MANIFEST:
  4369. case PlaylistContextType.LEVEL:
  4370. hls.trigger(Events.LEVEL_LOADED, {
  4371. details: levelDetails,
  4372. level: levelIndex || 0,
  4373. id: id || 0,
  4374. stats: stats,
  4375. networkDetails: networkDetails,
  4376. deliveryDirectives: deliveryDirectives
  4377. });
  4378. break;
  4379. case PlaylistContextType.AUDIO_TRACK:
  4380. hls.trigger(Events.AUDIO_TRACK_LOADED, {
  4381. details: levelDetails,
  4382. id: id || 0,
  4383. groupId: groupId || '',
  4384. stats: stats,
  4385. networkDetails: networkDetails,
  4386. deliveryDirectives: deliveryDirectives
  4387. });
  4388. break;
  4389. case PlaylistContextType.SUBTITLE_TRACK:
  4390. hls.trigger(Events.SUBTITLE_TRACK_LOADED, {
  4391. details: levelDetails,
  4392. id: id || 0,
  4393. groupId: groupId || '',
  4394. stats: stats,
  4395. networkDetails: networkDetails,
  4396. deliveryDirectives: deliveryDirectives
  4397. });
  4398. break;
  4399. }
  4400. };
  4401. return PlaylistLoader;
  4402. }();
  4403. function sendAddTrackEvent(track, videoEl) {
  4404. var event;
  4405. try {
  4406. event = new Event('addtrack');
  4407. } catch (err) {
  4408. // for IE11
  4409. event = document.createEvent('Event');
  4410. event.initEvent('addtrack', false, false);
  4411. }
  4412. event.track = track;
  4413. videoEl.dispatchEvent(event);
  4414. }
  4415. function addCueToTrack(track, cue) {
  4416. // Sometimes there are cue overlaps on segmented vtts so the same
  4417. // cue can appear more than once in different vtt files.
  4418. // This avoid showing duplicated cues with same timecode and text.
  4419. var mode = track.mode;
  4420. if (mode === 'disabled') {
  4421. track.mode = 'hidden';
  4422. }
  4423. if (track.cues && !track.cues.getCueById(cue.id)) {
  4424. try {
  4425. track.addCue(cue);
  4426. if (!track.cues.getCueById(cue.id)) {
  4427. throw new Error("addCue is failed for: " + cue);
  4428. }
  4429. } catch (err) {
  4430. logger.debug("[texttrack-utils]: " + err);
  4431. try {
  4432. var textTrackCue = new self.TextTrackCue(cue.startTime, cue.endTime, cue.text);
  4433. textTrackCue.id = cue.id;
  4434. track.addCue(textTrackCue);
  4435. } catch (err2) {
  4436. logger.debug("[texttrack-utils]: Legacy TextTrackCue fallback failed: " + err2);
  4437. }
  4438. }
  4439. }
  4440. if (mode === 'disabled') {
  4441. track.mode = mode;
  4442. }
  4443. }
  4444. function clearCurrentCues(track) {
  4445. // When track.mode is disabled, track.cues will be null.
  4446. // To guarantee the removal of cues, we need to temporarily
  4447. // change the mode to hidden
  4448. var mode = track.mode;
  4449. if (mode === 'disabled') {
  4450. track.mode = 'hidden';
  4451. }
  4452. if (track.cues) {
  4453. for (var i = track.cues.length; i--;) {
  4454. track.removeCue(track.cues[i]);
  4455. }
  4456. }
  4457. if (mode === 'disabled') {
  4458. track.mode = mode;
  4459. }
  4460. }
  4461. function removeCuesInRange(track, start, end, predicate) {
  4462. var mode = track.mode;
  4463. if (mode === 'disabled') {
  4464. track.mode = 'hidden';
  4465. }
  4466. if (track.cues && track.cues.length > 0) {
  4467. var cues = getCuesInRange(track.cues, start, end);
  4468. for (var i = 0; i < cues.length; i++) {
  4469. if (!predicate || predicate(cues[i])) {
  4470. track.removeCue(cues[i]);
  4471. }
  4472. }
  4473. }
  4474. if (mode === 'disabled') {
  4475. track.mode = mode;
  4476. }
  4477. }
  4478. // Find first cue starting after given time.
  4479. // Modified version of binary search O(log(n)).
  4480. function getFirstCueIndexAfterTime(cues, time) {
  4481. // If first cue starts after time, start there
  4482. if (time < cues[0].startTime) {
  4483. return 0;
  4484. }
  4485. // If the last cue ends before time there is no overlap
  4486. var len = cues.length - 1;
  4487. if (time > cues[len].endTime) {
  4488. return -1;
  4489. }
  4490. var left = 0;
  4491. var right = len;
  4492. while (left <= right) {
  4493. var mid = Math.floor((right + left) / 2);
  4494. if (time < cues[mid].startTime) {
  4495. right = mid - 1;
  4496. } else if (time > cues[mid].startTime && left < len) {
  4497. left = mid + 1;
  4498. } else {
  4499. // If it's not lower or higher, it must be equal.
  4500. return mid;
  4501. }
  4502. }
  4503. // At this point, left and right have swapped.
  4504. // No direct match was found, left or right element must be the closest. Check which one has the smallest diff.
  4505. return cues[left].startTime - time < time - cues[right].startTime ? left : right;
  4506. }
  4507. function getCuesInRange(cues, start, end) {
  4508. var cuesFound = [];
  4509. var firstCueInRange = getFirstCueIndexAfterTime(cues, start);
  4510. if (firstCueInRange > -1) {
  4511. for (var i = firstCueInRange, len = cues.length; i < len; i++) {
  4512. var _cue = cues[i];
  4513. if (_cue.startTime >= start && _cue.endTime <= end) {
  4514. cuesFound.push(_cue);
  4515. } else if (_cue.startTime > end) {
  4516. return cuesFound;
  4517. }
  4518. }
  4519. }
  4520. return cuesFound;
  4521. }
  4522. function filterSubtitleTracks(textTrackList) {
  4523. var tracks = [];
  4524. for (var i = 0; i < textTrackList.length; i++) {
  4525. var track = textTrackList[i];
  4526. // Edge adds a track without a label; we don't want to use it
  4527. if ((track.kind === 'subtitles' || track.kind === 'captions') && track.label) {
  4528. tracks.push(textTrackList[i]);
  4529. }
  4530. }
  4531. return tracks;
  4532. }
  4533. var MetadataSchema = {
  4534. audioId3: "org.id3",
  4535. dateRange: "com.apple.quicktime.HLS",
  4536. emsg: "https://aomedia.org/emsg/ID3"
  4537. };
  4538. var MIN_CUE_DURATION = 0.25;
  4539. function getCueClass() {
  4540. if (typeof self === 'undefined') return undefined;
  4541. return self.VTTCue || self.TextTrackCue;
  4542. }
  4543. function createCueWithDataFields(Cue, startTime, endTime, data, type) {
  4544. var cue = new Cue(startTime, endTime, '');
  4545. try {
  4546. cue.value = data;
  4547. if (type) {
  4548. cue.type = type;
  4549. }
  4550. } catch (e) {
  4551. cue = new Cue(startTime, endTime, JSON.stringify(type ? _objectSpread2({
  4552. type: type
  4553. }, data) : data));
  4554. }
  4555. return cue;
  4556. }
  4557. // VTTCue latest draft allows an infinite duration, fallback
  4558. // to MAX_VALUE if necessary
  4559. var MAX_CUE_ENDTIME = function () {
  4560. var Cue = getCueClass();
  4561. try {
  4562. Cue && new Cue(0, Number.POSITIVE_INFINITY, '');
  4563. } catch (e) {
  4564. return Number.MAX_VALUE;
  4565. }
  4566. return Number.POSITIVE_INFINITY;
  4567. }();
  4568. function dateRangeDateToTimelineSeconds(date, offset) {
  4569. return date.getTime() / 1000 - offset;
  4570. }
  4571. function hexToArrayBuffer(str) {
  4572. return Uint8Array.from(str.replace(/^0x/, '').replace(/([\da-fA-F]{2}) ?/g, '0x$1 ').replace(/ +$/, '').split(' ')).buffer;
  4573. }
  4574. var ID3TrackController = /*#__PURE__*/function () {
  4575. function ID3TrackController(hls) {
  4576. this.hls = void 0;
  4577. this.id3Track = null;
  4578. this.media = null;
  4579. this.dateRangeCuesAppended = {};
  4580. this.hls = hls;
  4581. this._registerListeners();
  4582. }
  4583. var _proto = ID3TrackController.prototype;
  4584. _proto.destroy = function destroy() {
  4585. this._unregisterListeners();
  4586. this.id3Track = null;
  4587. this.media = null;
  4588. this.dateRangeCuesAppended = {};
  4589. // @ts-ignore
  4590. this.hls = null;
  4591. };
  4592. _proto._registerListeners = function _registerListeners() {
  4593. var hls = this.hls;
  4594. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  4595. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  4596. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  4597. hls.on(Events.FRAG_PARSING_METADATA, this.onFragParsingMetadata, this);
  4598. hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  4599. hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  4600. };
  4601. _proto._unregisterListeners = function _unregisterListeners() {
  4602. var hls = this.hls;
  4603. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  4604. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  4605. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  4606. hls.off(Events.FRAG_PARSING_METADATA, this.onFragParsingMetadata, this);
  4607. hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  4608. hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  4609. }
  4610. // Add ID3 metatadata text track.
  4611. ;
  4612. _proto.onMediaAttached = function onMediaAttached(event, data) {
  4613. this.media = data.media;
  4614. };
  4615. _proto.onMediaDetaching = function onMediaDetaching() {
  4616. if (!this.id3Track) {
  4617. return;
  4618. }
  4619. clearCurrentCues(this.id3Track);
  4620. this.id3Track = null;
  4621. this.media = null;
  4622. this.dateRangeCuesAppended = {};
  4623. };
  4624. _proto.onManifestLoading = function onManifestLoading() {
  4625. this.dateRangeCuesAppended = {};
  4626. };
  4627. _proto.createTrack = function createTrack(media) {
  4628. var track = this.getID3Track(media.textTracks);
  4629. track.mode = 'hidden';
  4630. return track;
  4631. };
  4632. _proto.getID3Track = function getID3Track(textTracks) {
  4633. if (!this.media) {
  4634. return;
  4635. }
  4636. for (var i = 0; i < textTracks.length; i++) {
  4637. var textTrack = textTracks[i];
  4638. if (textTrack.kind === 'metadata' && textTrack.label === 'id3') {
  4639. // send 'addtrack' when reusing the textTrack for metadata,
  4640. // same as what we do for captions
  4641. sendAddTrackEvent(textTrack, this.media);
  4642. return textTrack;
  4643. }
  4644. }
  4645. return this.media.addTextTrack('metadata', 'id3');
  4646. };
  4647. _proto.onFragParsingMetadata = function onFragParsingMetadata(event, data) {
  4648. if (!this.media) {
  4649. return;
  4650. }
  4651. var _this$hls$config = this.hls.config,
  4652. enableEmsgMetadataCues = _this$hls$config.enableEmsgMetadataCues,
  4653. enableID3MetadataCues = _this$hls$config.enableID3MetadataCues;
  4654. if (!enableEmsgMetadataCues && !enableID3MetadataCues) {
  4655. return;
  4656. }
  4657. var samples = data.samples;
  4658. // create track dynamically
  4659. if (!this.id3Track) {
  4660. this.id3Track = this.createTrack(this.media);
  4661. }
  4662. var Cue = getCueClass();
  4663. if (!Cue) {
  4664. return;
  4665. }
  4666. for (var i = 0; i < samples.length; i++) {
  4667. var type = samples[i].type;
  4668. if (type === MetadataSchema.emsg && !enableEmsgMetadataCues || !enableID3MetadataCues) {
  4669. continue;
  4670. }
  4671. var frames = getID3Frames(samples[i].data);
  4672. if (frames) {
  4673. var startTime = samples[i].pts;
  4674. var endTime = startTime + samples[i].duration;
  4675. if (endTime > MAX_CUE_ENDTIME) {
  4676. endTime = MAX_CUE_ENDTIME;
  4677. }
  4678. var timeDiff = endTime - startTime;
  4679. if (timeDiff <= 0) {
  4680. endTime = startTime + MIN_CUE_DURATION;
  4681. }
  4682. for (var j = 0; j < frames.length; j++) {
  4683. var frame = frames[j];
  4684. // Safari doesn't put the timestamp frame in the TextTrack
  4685. if (!isTimeStampFrame(frame)) {
  4686. // add a bounds to any unbounded cues
  4687. this.updateId3CueEnds(startTime, type);
  4688. var cue = createCueWithDataFields(Cue, startTime, endTime, frame, type);
  4689. if (cue) {
  4690. this.id3Track.addCue(cue);
  4691. }
  4692. }
  4693. }
  4694. }
  4695. }
  4696. };
  4697. _proto.updateId3CueEnds = function updateId3CueEnds(startTime, type) {
  4698. var _this$id3Track;
  4699. var cues = (_this$id3Track = this.id3Track) == null ? void 0 : _this$id3Track.cues;
  4700. if (cues) {
  4701. for (var i = cues.length; i--;) {
  4702. var cue = cues[i];
  4703. if (cue.type === type && cue.startTime < startTime && cue.endTime === MAX_CUE_ENDTIME) {
  4704. cue.endTime = startTime;
  4705. }
  4706. }
  4707. }
  4708. };
  4709. _proto.onBufferFlushing = function onBufferFlushing(event, _ref) {
  4710. var startOffset = _ref.startOffset,
  4711. endOffset = _ref.endOffset,
  4712. type = _ref.type;
  4713. var id3Track = this.id3Track,
  4714. hls = this.hls;
  4715. if (!hls) {
  4716. return;
  4717. }
  4718. var _hls$config = hls.config,
  4719. enableEmsgMetadataCues = _hls$config.enableEmsgMetadataCues,
  4720. enableID3MetadataCues = _hls$config.enableID3MetadataCues;
  4721. if (id3Track && (enableEmsgMetadataCues || enableID3MetadataCues)) {
  4722. var predicate;
  4723. if (type === 'audio') {
  4724. predicate = function predicate(cue) {
  4725. return cue.type === MetadataSchema.audioId3 && enableID3MetadataCues;
  4726. };
  4727. } else if (type === 'video') {
  4728. predicate = function predicate(cue) {
  4729. return cue.type === MetadataSchema.emsg && enableEmsgMetadataCues;
  4730. };
  4731. } else {
  4732. predicate = function predicate(cue) {
  4733. return cue.type === MetadataSchema.audioId3 && enableID3MetadataCues || cue.type === MetadataSchema.emsg && enableEmsgMetadataCues;
  4734. };
  4735. }
  4736. removeCuesInRange(id3Track, startOffset, endOffset, predicate);
  4737. }
  4738. };
  4739. _proto.onLevelUpdated = function onLevelUpdated(event, _ref2) {
  4740. var _this = this;
  4741. var details = _ref2.details;
  4742. if (!this.media || !details.hasProgramDateTime || !this.hls.config.enableDateRangeMetadataCues) {
  4743. return;
  4744. }
  4745. var dateRangeCuesAppended = this.dateRangeCuesAppended,
  4746. id3Track = this.id3Track;
  4747. var dateRanges = details.dateRanges;
  4748. var ids = Object.keys(dateRanges);
  4749. // Remove cues from track not found in details.dateRanges
  4750. if (id3Track) {
  4751. var idsToRemove = Object.keys(dateRangeCuesAppended).filter(function (id) {
  4752. return !ids.includes(id);
  4753. });
  4754. var _loop = function _loop() {
  4755. var id = idsToRemove[i];
  4756. Object.keys(dateRangeCuesAppended[id].cues).forEach(function (key) {
  4757. id3Track.removeCue(dateRangeCuesAppended[id].cues[key]);
  4758. });
  4759. delete dateRangeCuesAppended[id];
  4760. };
  4761. for (var i = idsToRemove.length; i--;) {
  4762. _loop();
  4763. }
  4764. }
  4765. // Exit if the playlist does not have Date Ranges or does not have Program Date Time
  4766. var lastFragment = details.fragments[details.fragments.length - 1];
  4767. if (ids.length === 0 || !isFiniteNumber(lastFragment == null ? void 0 : lastFragment.programDateTime)) {
  4768. return;
  4769. }
  4770. if (!this.id3Track) {
  4771. this.id3Track = this.createTrack(this.media);
  4772. }
  4773. var dateTimeOffset = lastFragment.programDateTime / 1000 - lastFragment.start;
  4774. var Cue = getCueClass();
  4775. var _loop2 = function _loop2() {
  4776. var id = ids[_i];
  4777. var dateRange = dateRanges[id];
  4778. var startTime = dateRangeDateToTimelineSeconds(dateRange.startDate, dateTimeOffset);
  4779. // Process DateRanges to determine end-time (known DURATION, END-DATE, or END-ON-NEXT)
  4780. var appendedDateRangeCues = dateRangeCuesAppended[id];
  4781. var cues = (appendedDateRangeCues == null ? void 0 : appendedDateRangeCues.cues) || {};
  4782. var durationKnown = (appendedDateRangeCues == null ? void 0 : appendedDateRangeCues.durationKnown) || false;
  4783. var endTime = MAX_CUE_ENDTIME;
  4784. var endDate = dateRange.endDate;
  4785. if (endDate) {
  4786. endTime = dateRangeDateToTimelineSeconds(endDate, dateTimeOffset);
  4787. durationKnown = true;
  4788. } else if (dateRange.endOnNext && !durationKnown) {
  4789. var nextDateRangeWithSameClass = ids.reduce(function (candidateDateRange, id) {
  4790. if (id !== dateRange.id) {
  4791. var otherDateRange = dateRanges[id];
  4792. if (otherDateRange.class === dateRange.class && otherDateRange.startDate > dateRange.startDate && (!candidateDateRange || dateRange.startDate < candidateDateRange.startDate)) {
  4793. return otherDateRange;
  4794. }
  4795. }
  4796. return candidateDateRange;
  4797. }, null);
  4798. if (nextDateRangeWithSameClass) {
  4799. endTime = dateRangeDateToTimelineSeconds(nextDateRangeWithSameClass.startDate, dateTimeOffset);
  4800. durationKnown = true;
  4801. }
  4802. }
  4803. // Create TextTrack Cues for each MetadataGroup Item (select DateRange attribute)
  4804. // This is to emulate Safari HLS playback handling of DateRange tags
  4805. var attributes = Object.keys(dateRange.attr);
  4806. for (var j = 0; j < attributes.length; j++) {
  4807. var key = attributes[j];
  4808. if (!isDateRangeCueAttribute(key)) {
  4809. continue;
  4810. }
  4811. var cue = cues[key];
  4812. if (cue) {
  4813. if (durationKnown && !appendedDateRangeCues.durationKnown) {
  4814. cue.endTime = endTime;
  4815. }
  4816. } else if (Cue) {
  4817. var data = dateRange.attr[key];
  4818. if (isSCTE35Attribute(key)) {
  4819. data = hexToArrayBuffer(data);
  4820. }
  4821. var _cue = createCueWithDataFields(Cue, startTime, endTime, {
  4822. key: key,
  4823. data: data
  4824. }, MetadataSchema.dateRange);
  4825. if (_cue) {
  4826. _cue.id = id;
  4827. _this.id3Track.addCue(_cue);
  4828. cues[key] = _cue;
  4829. }
  4830. }
  4831. }
  4832. // Keep track of processed DateRanges by ID for updating cues with new DateRange tag attributes
  4833. dateRangeCuesAppended[id] = {
  4834. cues: cues,
  4835. dateRange: dateRange,
  4836. durationKnown: durationKnown
  4837. };
  4838. };
  4839. for (var _i = 0; _i < ids.length; _i++) {
  4840. _loop2();
  4841. }
  4842. };
  4843. return ID3TrackController;
  4844. }();
  4845. var LatencyController = /*#__PURE__*/function () {
  4846. function LatencyController(hls) {
  4847. var _this = this;
  4848. this.hls = void 0;
  4849. this.config = void 0;
  4850. this.media = null;
  4851. this.levelDetails = null;
  4852. this.currentTime = 0;
  4853. this.stallCount = 0;
  4854. this._latency = null;
  4855. this.timeupdateHandler = function () {
  4856. return _this.timeupdate();
  4857. };
  4858. this.hls = hls;
  4859. this.config = hls.config;
  4860. this.registerListeners();
  4861. }
  4862. var _proto = LatencyController.prototype;
  4863. _proto.destroy = function destroy() {
  4864. this.unregisterListeners();
  4865. this.onMediaDetaching();
  4866. this.levelDetails = null;
  4867. // @ts-ignore
  4868. this.hls = this.timeupdateHandler = null;
  4869. };
  4870. _proto.registerListeners = function registerListeners() {
  4871. this.hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  4872. this.hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  4873. this.hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  4874. this.hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  4875. this.hls.on(Events.ERROR, this.onError, this);
  4876. };
  4877. _proto.unregisterListeners = function unregisterListeners() {
  4878. this.hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  4879. this.hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  4880. this.hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  4881. this.hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  4882. this.hls.off(Events.ERROR, this.onError, this);
  4883. };
  4884. _proto.onMediaAttached = function onMediaAttached(event, data) {
  4885. this.media = data.media;
  4886. this.media.addEventListener('timeupdate', this.timeupdateHandler);
  4887. };
  4888. _proto.onMediaDetaching = function onMediaDetaching() {
  4889. if (this.media) {
  4890. this.media.removeEventListener('timeupdate', this.timeupdateHandler);
  4891. this.media = null;
  4892. }
  4893. };
  4894. _proto.onManifestLoading = function onManifestLoading() {
  4895. this.levelDetails = null;
  4896. this._latency = null;
  4897. this.stallCount = 0;
  4898. };
  4899. _proto.onLevelUpdated = function onLevelUpdated(event, _ref) {
  4900. var details = _ref.details;
  4901. this.levelDetails = details;
  4902. if (details.advanced) {
  4903. this.timeupdate();
  4904. }
  4905. if (!details.live && this.media) {
  4906. this.media.removeEventListener('timeupdate', this.timeupdateHandler);
  4907. }
  4908. };
  4909. _proto.onError = function onError(event, data) {
  4910. var _this$levelDetails;
  4911. if (data.details !== ErrorDetails.BUFFER_STALLED_ERROR) {
  4912. return;
  4913. }
  4914. this.stallCount++;
  4915. if ((_this$levelDetails = this.levelDetails) != null && _this$levelDetails.live) {
  4916. logger.warn('[playback-rate-controller]: Stall detected, adjusting target latency');
  4917. }
  4918. };
  4919. _proto.timeupdate = function timeupdate() {
  4920. var media = this.media,
  4921. levelDetails = this.levelDetails;
  4922. if (!media || !levelDetails) {
  4923. return;
  4924. }
  4925. this.currentTime = media.currentTime;
  4926. var latency = this.computeLatency();
  4927. if (latency === null) {
  4928. return;
  4929. }
  4930. this._latency = latency;
  4931. // Adapt playbackRate to meet target latency in low-latency mode
  4932. var _this$config = this.config,
  4933. lowLatencyMode = _this$config.lowLatencyMode,
  4934. maxLiveSyncPlaybackRate = _this$config.maxLiveSyncPlaybackRate;
  4935. if (!lowLatencyMode || maxLiveSyncPlaybackRate === 1 || !levelDetails.live) {
  4936. return;
  4937. }
  4938. var targetLatency = this.targetLatency;
  4939. if (targetLatency === null) {
  4940. return;
  4941. }
  4942. var distanceFromTarget = latency - targetLatency;
  4943. // Only adjust playbackRate when within one target duration of targetLatency
  4944. // and more than one second from under-buffering.
  4945. // Playback further than one target duration from target can be considered DVR playback.
  4946. var liveMinLatencyDuration = Math.min(this.maxLatency, targetLatency + levelDetails.targetduration);
  4947. var inLiveRange = distanceFromTarget < liveMinLatencyDuration;
  4948. if (inLiveRange && distanceFromTarget > 0.05 && this.forwardBufferLength > 1) {
  4949. var max = Math.min(2, Math.max(1.0, maxLiveSyncPlaybackRate));
  4950. var rate = Math.round(2 / (1 + Math.exp(-0.75 * distanceFromTarget - this.edgeStalled)) * 20) / 20;
  4951. media.playbackRate = Math.min(max, Math.max(1, rate));
  4952. } else if (media.playbackRate !== 1 && media.playbackRate !== 0) {
  4953. media.playbackRate = 1;
  4954. }
  4955. };
  4956. _proto.estimateLiveEdge = function estimateLiveEdge() {
  4957. var levelDetails = this.levelDetails;
  4958. if (levelDetails === null) {
  4959. return null;
  4960. }
  4961. return levelDetails.edge + levelDetails.age;
  4962. };
  4963. _proto.computeLatency = function computeLatency() {
  4964. var liveEdge = this.estimateLiveEdge();
  4965. if (liveEdge === null) {
  4966. return null;
  4967. }
  4968. return liveEdge - this.currentTime;
  4969. };
  4970. _createClass(LatencyController, [{
  4971. key: "latency",
  4972. get: function get() {
  4973. return this._latency || 0;
  4974. }
  4975. }, {
  4976. key: "maxLatency",
  4977. get: function get() {
  4978. var config = this.config,
  4979. levelDetails = this.levelDetails;
  4980. if (config.liveMaxLatencyDuration !== undefined) {
  4981. return config.liveMaxLatencyDuration;
  4982. }
  4983. return levelDetails ? config.liveMaxLatencyDurationCount * levelDetails.targetduration : 0;
  4984. }
  4985. }, {
  4986. key: "targetLatency",
  4987. get: function get() {
  4988. var levelDetails = this.levelDetails;
  4989. if (levelDetails === null) {
  4990. return null;
  4991. }
  4992. var holdBack = levelDetails.holdBack,
  4993. partHoldBack = levelDetails.partHoldBack,
  4994. targetduration = levelDetails.targetduration;
  4995. var _this$config2 = this.config,
  4996. liveSyncDuration = _this$config2.liveSyncDuration,
  4997. liveSyncDurationCount = _this$config2.liveSyncDurationCount,
  4998. lowLatencyMode = _this$config2.lowLatencyMode;
  4999. var userConfig = this.hls.userConfig;
  5000. var targetLatency = lowLatencyMode ? partHoldBack || holdBack : holdBack;
  5001. if (userConfig.liveSyncDuration || userConfig.liveSyncDurationCount || targetLatency === 0) {
  5002. targetLatency = liveSyncDuration !== undefined ? liveSyncDuration : liveSyncDurationCount * targetduration;
  5003. }
  5004. var maxLiveSyncOnStallIncrease = targetduration;
  5005. var liveSyncOnStallIncrease = 1.0;
  5006. return targetLatency + Math.min(this.stallCount * liveSyncOnStallIncrease, maxLiveSyncOnStallIncrease);
  5007. }
  5008. }, {
  5009. key: "liveSyncPosition",
  5010. get: function get() {
  5011. var liveEdge = this.estimateLiveEdge();
  5012. var targetLatency = this.targetLatency;
  5013. var levelDetails = this.levelDetails;
  5014. if (liveEdge === null || targetLatency === null || levelDetails === null) {
  5015. return null;
  5016. }
  5017. var edge = levelDetails.edge;
  5018. var syncPosition = liveEdge - targetLatency - this.edgeStalled;
  5019. var min = edge - levelDetails.totalduration;
  5020. var max = edge - (this.config.lowLatencyMode && levelDetails.partTarget || levelDetails.targetduration);
  5021. return Math.min(Math.max(min, syncPosition), max);
  5022. }
  5023. }, {
  5024. key: "drift",
  5025. get: function get() {
  5026. var levelDetails = this.levelDetails;
  5027. if (levelDetails === null) {
  5028. return 1;
  5029. }
  5030. return levelDetails.drift;
  5031. }
  5032. }, {
  5033. key: "edgeStalled",
  5034. get: function get() {
  5035. var levelDetails = this.levelDetails;
  5036. if (levelDetails === null) {
  5037. return 0;
  5038. }
  5039. var maxLevelUpdateAge = (this.config.lowLatencyMode && levelDetails.partTarget || levelDetails.targetduration) * 3;
  5040. return Math.max(levelDetails.age - maxLevelUpdateAge, 0);
  5041. }
  5042. }, {
  5043. key: "forwardBufferLength",
  5044. get: function get() {
  5045. var media = this.media,
  5046. levelDetails = this.levelDetails;
  5047. if (!media || !levelDetails) {
  5048. return 0;
  5049. }
  5050. var bufferedRanges = media.buffered.length;
  5051. return (bufferedRanges ? media.buffered.end(bufferedRanges - 1) : levelDetails.edge) - this.currentTime;
  5052. }
  5053. }]);
  5054. return LatencyController;
  5055. }();
  5056. var HdcpLevels = ['NONE', 'TYPE-0', 'TYPE-1', null];
  5057. function isHdcpLevel(value) {
  5058. return HdcpLevels.indexOf(value) > -1;
  5059. }
  5060. var VideoRangeValues = ['SDR', 'PQ', 'HLG'];
  5061. function isVideoRange(value) {
  5062. return !!value && VideoRangeValues.indexOf(value) > -1;
  5063. }
  5064. var HlsSkip = {
  5065. No: "",
  5066. Yes: "YES",
  5067. v2: "v2"
  5068. };
  5069. function getSkipValue(details) {
  5070. var canSkipUntil = details.canSkipUntil,
  5071. canSkipDateRanges = details.canSkipDateRanges,
  5072. age = details.age;
  5073. // A Client SHOULD NOT request a Playlist Delta Update unless it already
  5074. // has a version of the Playlist that is no older than one-half of the Skip Boundary.
  5075. // @see: https://datatracker.ietf.org/doc/html/draft-pantos-hls-rfc8216bis#section-6.3.7
  5076. var playlistRecentEnough = age < canSkipUntil / 2;
  5077. if (canSkipUntil && playlistRecentEnough) {
  5078. if (canSkipDateRanges) {
  5079. return HlsSkip.v2;
  5080. }
  5081. return HlsSkip.Yes;
  5082. }
  5083. return HlsSkip.No;
  5084. }
  5085. var HlsUrlParameters = /*#__PURE__*/function () {
  5086. function HlsUrlParameters(msn, part, skip) {
  5087. this.msn = void 0;
  5088. this.part = void 0;
  5089. this.skip = void 0;
  5090. this.msn = msn;
  5091. this.part = part;
  5092. this.skip = skip;
  5093. }
  5094. var _proto = HlsUrlParameters.prototype;
  5095. _proto.addDirectives = function addDirectives(uri) {
  5096. var url = new self.URL(uri);
  5097. if (this.msn !== undefined) {
  5098. url.searchParams.set('_HLS_msn', this.msn.toString());
  5099. }
  5100. if (this.part !== undefined) {
  5101. url.searchParams.set('_HLS_part', this.part.toString());
  5102. }
  5103. if (this.skip) {
  5104. url.searchParams.set('_HLS_skip', this.skip);
  5105. }
  5106. return url.href;
  5107. };
  5108. return HlsUrlParameters;
  5109. }();
  5110. var Level = /*#__PURE__*/function () {
  5111. function Level(data) {
  5112. this._attrs = void 0;
  5113. this.audioCodec = void 0;
  5114. this.bitrate = void 0;
  5115. this.codecSet = void 0;
  5116. this.url = void 0;
  5117. this.frameRate = void 0;
  5118. this.height = void 0;
  5119. this.id = void 0;
  5120. this.name = void 0;
  5121. this.videoCodec = void 0;
  5122. this.width = void 0;
  5123. this.details = void 0;
  5124. this.fragmentError = 0;
  5125. this.loadError = 0;
  5126. this.loaded = void 0;
  5127. this.realBitrate = 0;
  5128. this.supportedPromise = void 0;
  5129. this.supportedResult = void 0;
  5130. this._avgBitrate = 0;
  5131. this._audioGroups = void 0;
  5132. this._subtitleGroups = void 0;
  5133. // Deprecated (retained for backwards compatibility)
  5134. this._urlId = 0;
  5135. this.url = [data.url];
  5136. this._attrs = [data.attrs];
  5137. this.bitrate = data.bitrate;
  5138. if (data.details) {
  5139. this.details = data.details;
  5140. }
  5141. this.id = data.id || 0;
  5142. this.name = data.name;
  5143. this.width = data.width || 0;
  5144. this.height = data.height || 0;
  5145. this.frameRate = data.attrs.optionalFloat('FRAME-RATE', 0);
  5146. this._avgBitrate = data.attrs.decimalInteger('AVERAGE-BANDWIDTH');
  5147. this.audioCodec = data.audioCodec;
  5148. this.videoCodec = data.videoCodec;
  5149. this.codecSet = [data.videoCodec, data.audioCodec].filter(function (c) {
  5150. return !!c;
  5151. }).map(function (s) {
  5152. return s.substring(0, 4);
  5153. }).join(',');
  5154. this.addGroupId('audio', data.attrs.AUDIO);
  5155. this.addGroupId('text', data.attrs.SUBTITLES);
  5156. }
  5157. var _proto2 = Level.prototype;
  5158. _proto2.hasAudioGroup = function hasAudioGroup(groupId) {
  5159. return hasGroup(this._audioGroups, groupId);
  5160. };
  5161. _proto2.hasSubtitleGroup = function hasSubtitleGroup(groupId) {
  5162. return hasGroup(this._subtitleGroups, groupId);
  5163. };
  5164. _proto2.addGroupId = function addGroupId(type, groupId) {
  5165. if (!groupId) {
  5166. return;
  5167. }
  5168. if (type === 'audio') {
  5169. var audioGroups = this._audioGroups;
  5170. if (!audioGroups) {
  5171. audioGroups = this._audioGroups = [];
  5172. }
  5173. if (audioGroups.indexOf(groupId) === -1) {
  5174. audioGroups.push(groupId);
  5175. }
  5176. } else if (type === 'text') {
  5177. var subtitleGroups = this._subtitleGroups;
  5178. if (!subtitleGroups) {
  5179. subtitleGroups = this._subtitleGroups = [];
  5180. }
  5181. if (subtitleGroups.indexOf(groupId) === -1) {
  5182. subtitleGroups.push(groupId);
  5183. }
  5184. }
  5185. }
  5186. // Deprecated methods (retained for backwards compatibility)
  5187. ;
  5188. _proto2.addFallback = function addFallback() {};
  5189. _createClass(Level, [{
  5190. key: "maxBitrate",
  5191. get: function get() {
  5192. return Math.max(this.realBitrate, this.bitrate);
  5193. }
  5194. }, {
  5195. key: "averageBitrate",
  5196. get: function get() {
  5197. return this._avgBitrate || this.realBitrate || this.bitrate;
  5198. }
  5199. }, {
  5200. key: "attrs",
  5201. get: function get() {
  5202. return this._attrs[0];
  5203. }
  5204. }, {
  5205. key: "codecs",
  5206. get: function get() {
  5207. return this.attrs.CODECS || '';
  5208. }
  5209. }, {
  5210. key: "pathwayId",
  5211. get: function get() {
  5212. return this.attrs['PATHWAY-ID'] || '.';
  5213. }
  5214. }, {
  5215. key: "videoRange",
  5216. get: function get() {
  5217. return this.attrs['VIDEO-RANGE'] || 'SDR';
  5218. }
  5219. }, {
  5220. key: "score",
  5221. get: function get() {
  5222. return this.attrs.optionalFloat('SCORE', 0);
  5223. }
  5224. }, {
  5225. key: "uri",
  5226. get: function get() {
  5227. return this.url[0] || '';
  5228. }
  5229. }, {
  5230. key: "audioGroups",
  5231. get: function get() {
  5232. return this._audioGroups;
  5233. }
  5234. }, {
  5235. key: "subtitleGroups",
  5236. get: function get() {
  5237. return this._subtitleGroups;
  5238. }
  5239. }, {
  5240. key: "urlId",
  5241. get: function get() {
  5242. return 0;
  5243. },
  5244. set: function set(value) {}
  5245. }, {
  5246. key: "audioGroupIds",
  5247. get: function get() {
  5248. return this.audioGroups ? [this.audioGroupId] : undefined;
  5249. }
  5250. }, {
  5251. key: "textGroupIds",
  5252. get: function get() {
  5253. return this.subtitleGroups ? [this.textGroupId] : undefined;
  5254. }
  5255. }, {
  5256. key: "audioGroupId",
  5257. get: function get() {
  5258. var _this$audioGroups;
  5259. return (_this$audioGroups = this.audioGroups) == null ? void 0 : _this$audioGroups[0];
  5260. }
  5261. }, {
  5262. key: "textGroupId",
  5263. get: function get() {
  5264. var _this$subtitleGroups;
  5265. return (_this$subtitleGroups = this.subtitleGroups) == null ? void 0 : _this$subtitleGroups[0];
  5266. }
  5267. }]);
  5268. return Level;
  5269. }();
  5270. function hasGroup(groups, groupId) {
  5271. if (!groupId || !groups) {
  5272. return false;
  5273. }
  5274. return groups.indexOf(groupId) !== -1;
  5275. }
  5276. function updateFromToPTS(fragFrom, fragTo) {
  5277. var fragToPTS = fragTo.startPTS;
  5278. // if we know startPTS[toIdx]
  5279. if (isFiniteNumber(fragToPTS)) {
  5280. // update fragment duration.
  5281. // it helps to fix drifts between playlist reported duration and fragment real duration
  5282. var duration = 0;
  5283. var frag;
  5284. if (fragTo.sn > fragFrom.sn) {
  5285. duration = fragToPTS - fragFrom.start;
  5286. frag = fragFrom;
  5287. } else {
  5288. duration = fragFrom.start - fragToPTS;
  5289. frag = fragTo;
  5290. }
  5291. if (frag.duration !== duration) {
  5292. frag.duration = duration;
  5293. }
  5294. // we dont know startPTS[toIdx]
  5295. } else if (fragTo.sn > fragFrom.sn) {
  5296. var contiguous = fragFrom.cc === fragTo.cc;
  5297. // TODO: With part-loading end/durations we need to confirm the whole fragment is loaded before using (or setting) minEndPTS
  5298. if (contiguous && fragFrom.minEndPTS) {
  5299. fragTo.start = fragFrom.start + (fragFrom.minEndPTS - fragFrom.start);
  5300. } else {
  5301. fragTo.start = fragFrom.start + fragFrom.duration;
  5302. }
  5303. } else {
  5304. fragTo.start = Math.max(fragFrom.start - fragTo.duration, 0);
  5305. }
  5306. }
  5307. function updateFragPTSDTS(details, frag, startPTS, endPTS, startDTS, endDTS) {
  5308. var parsedMediaDuration = endPTS - startPTS;
  5309. if (parsedMediaDuration <= 0) {
  5310. logger.warn('Fragment should have a positive duration', frag);
  5311. endPTS = startPTS + frag.duration;
  5312. endDTS = startDTS + frag.duration;
  5313. }
  5314. var maxStartPTS = startPTS;
  5315. var minEndPTS = endPTS;
  5316. var fragStartPts = frag.startPTS;
  5317. var fragEndPts = frag.endPTS;
  5318. if (isFiniteNumber(fragStartPts)) {
  5319. // delta PTS between audio and video
  5320. var deltaPTS = Math.abs(fragStartPts - startPTS);
  5321. if (!isFiniteNumber(frag.deltaPTS)) {
  5322. frag.deltaPTS = deltaPTS;
  5323. } else {
  5324. frag.deltaPTS = Math.max(deltaPTS, frag.deltaPTS);
  5325. }
  5326. maxStartPTS = Math.max(startPTS, fragStartPts);
  5327. startPTS = Math.min(startPTS, fragStartPts);
  5328. startDTS = Math.min(startDTS, frag.startDTS);
  5329. minEndPTS = Math.min(endPTS, fragEndPts);
  5330. endPTS = Math.max(endPTS, fragEndPts);
  5331. endDTS = Math.max(endDTS, frag.endDTS);
  5332. }
  5333. var drift = startPTS - frag.start;
  5334. if (frag.start !== 0) {
  5335. frag.start = startPTS;
  5336. }
  5337. frag.duration = endPTS - frag.start;
  5338. frag.startPTS = startPTS;
  5339. frag.maxStartPTS = maxStartPTS;
  5340. frag.startDTS = startDTS;
  5341. frag.endPTS = endPTS;
  5342. frag.minEndPTS = minEndPTS;
  5343. frag.endDTS = endDTS;
  5344. var sn = frag.sn; // 'initSegment'
  5345. // exit if sn out of range
  5346. if (!details || sn < details.startSN || sn > details.endSN) {
  5347. return 0;
  5348. }
  5349. var i;
  5350. var fragIdx = sn - details.startSN;
  5351. var fragments = details.fragments;
  5352. // update frag reference in fragments array
  5353. // rationale is that fragments array might not contain this frag object.
  5354. // this will happen if playlist has been refreshed between frag loading and call to updateFragPTSDTS()
  5355. // if we don't update frag, we won't be able to propagate PTS info on the playlist
  5356. // resulting in invalid sliding computation
  5357. fragments[fragIdx] = frag;
  5358. // adjust fragment PTS/duration from seqnum-1 to frag 0
  5359. for (i = fragIdx; i > 0; i--) {
  5360. updateFromToPTS(fragments[i], fragments[i - 1]);
  5361. }
  5362. // adjust fragment PTS/duration from seqnum to last frag
  5363. for (i = fragIdx; i < fragments.length - 1; i++) {
  5364. updateFromToPTS(fragments[i], fragments[i + 1]);
  5365. }
  5366. if (details.fragmentHint) {
  5367. updateFromToPTS(fragments[fragments.length - 1], details.fragmentHint);
  5368. }
  5369. details.PTSKnown = details.alignedSliding = true;
  5370. return drift;
  5371. }
  5372. function mergeDetails(oldDetails, newDetails) {
  5373. // Track the last initSegment processed. Initialize it to the last one on the timeline.
  5374. var currentInitSegment = null;
  5375. var oldFragments = oldDetails.fragments;
  5376. for (var i = oldFragments.length - 1; i >= 0; i--) {
  5377. var oldInit = oldFragments[i].initSegment;
  5378. if (oldInit) {
  5379. currentInitSegment = oldInit;
  5380. break;
  5381. }
  5382. }
  5383. if (oldDetails.fragmentHint) {
  5384. // prevent PTS and duration from being adjusted on the next hint
  5385. delete oldDetails.fragmentHint.endPTS;
  5386. }
  5387. // check if old/new playlists have fragments in common
  5388. // loop through overlapping SN and update startPTS , cc, and duration if any found
  5389. var ccOffset = 0;
  5390. var PTSFrag;
  5391. mapFragmentIntersection(oldDetails, newDetails, function (oldFrag, newFrag) {
  5392. if (oldFrag.relurl) {
  5393. // Do not compare CC if the old fragment has no url. This is a level.fragmentHint used by LL-HLS parts.
  5394. // It maybe be off by 1 if it was created before any parts or discontinuity tags were appended to the end
  5395. // of the playlist.
  5396. ccOffset = oldFrag.cc - newFrag.cc;
  5397. }
  5398. if (isFiniteNumber(oldFrag.startPTS) && isFiniteNumber(oldFrag.endPTS)) {
  5399. newFrag.start = newFrag.startPTS = oldFrag.startPTS;
  5400. newFrag.startDTS = oldFrag.startDTS;
  5401. newFrag.maxStartPTS = oldFrag.maxStartPTS;
  5402. newFrag.endPTS = oldFrag.endPTS;
  5403. newFrag.endDTS = oldFrag.endDTS;
  5404. newFrag.minEndPTS = oldFrag.minEndPTS;
  5405. newFrag.duration = oldFrag.endPTS - oldFrag.startPTS;
  5406. if (newFrag.duration) {
  5407. PTSFrag = newFrag;
  5408. }
  5409. // PTS is known when any segment has startPTS and endPTS
  5410. newDetails.PTSKnown = newDetails.alignedSliding = true;
  5411. }
  5412. newFrag.elementaryStreams = oldFrag.elementaryStreams;
  5413. newFrag.loader = oldFrag.loader;
  5414. newFrag.stats = oldFrag.stats;
  5415. if (oldFrag.initSegment) {
  5416. newFrag.initSegment = oldFrag.initSegment;
  5417. currentInitSegment = oldFrag.initSegment;
  5418. }
  5419. });
  5420. if (currentInitSegment) {
  5421. var fragmentsToCheck = newDetails.fragmentHint ? newDetails.fragments.concat(newDetails.fragmentHint) : newDetails.fragments;
  5422. fragmentsToCheck.forEach(function (frag) {
  5423. var _currentInitSegment;
  5424. if (frag && (!frag.initSegment || frag.initSegment.relurl === ((_currentInitSegment = currentInitSegment) == null ? void 0 : _currentInitSegment.relurl))) {
  5425. frag.initSegment = currentInitSegment;
  5426. }
  5427. });
  5428. }
  5429. if (newDetails.skippedSegments) {
  5430. newDetails.deltaUpdateFailed = newDetails.fragments.some(function (frag) {
  5431. return !frag;
  5432. });
  5433. if (newDetails.deltaUpdateFailed) {
  5434. logger.warn('[level-helper] Previous playlist missing segments skipped in delta playlist');
  5435. for (var _i = newDetails.skippedSegments; _i--;) {
  5436. newDetails.fragments.shift();
  5437. }
  5438. newDetails.startSN = newDetails.fragments[0].sn;
  5439. newDetails.startCC = newDetails.fragments[0].cc;
  5440. } else if (newDetails.canSkipDateRanges) {
  5441. newDetails.dateRanges = mergeDateRanges(oldDetails.dateRanges, newDetails.dateRanges, newDetails.recentlyRemovedDateranges);
  5442. }
  5443. }
  5444. var newFragments = newDetails.fragments;
  5445. if (ccOffset) {
  5446. logger.warn('discontinuity sliding from playlist, take drift into account');
  5447. for (var _i2 = 0; _i2 < newFragments.length; _i2++) {
  5448. newFragments[_i2].cc += ccOffset;
  5449. }
  5450. }
  5451. if (newDetails.skippedSegments) {
  5452. newDetails.startCC = newDetails.fragments[0].cc;
  5453. }
  5454. // Merge parts
  5455. mapPartIntersection(oldDetails.partList, newDetails.partList, function (oldPart, newPart) {
  5456. newPart.elementaryStreams = oldPart.elementaryStreams;
  5457. newPart.stats = oldPart.stats;
  5458. });
  5459. // if at least one fragment contains PTS info, recompute PTS information for all fragments
  5460. if (PTSFrag) {
  5461. updateFragPTSDTS(newDetails, PTSFrag, PTSFrag.startPTS, PTSFrag.endPTS, PTSFrag.startDTS, PTSFrag.endDTS);
  5462. } else {
  5463. // ensure that delta is within oldFragments range
  5464. // also adjust sliding in case delta is 0 (we could have old=[50-60] and new=old=[50-61])
  5465. // in that case we also need to adjust start offset of all fragments
  5466. adjustSliding(oldDetails, newDetails);
  5467. }
  5468. if (newFragments.length) {
  5469. newDetails.totalduration = newDetails.edge - newFragments[0].start;
  5470. }
  5471. newDetails.driftStartTime = oldDetails.driftStartTime;
  5472. newDetails.driftStart = oldDetails.driftStart;
  5473. var advancedDateTime = newDetails.advancedDateTime;
  5474. if (newDetails.advanced && advancedDateTime) {
  5475. var edge = newDetails.edge;
  5476. if (!newDetails.driftStart) {
  5477. newDetails.driftStartTime = advancedDateTime;
  5478. newDetails.driftStart = edge;
  5479. }
  5480. newDetails.driftEndTime = advancedDateTime;
  5481. newDetails.driftEnd = edge;
  5482. } else {
  5483. newDetails.driftEndTime = oldDetails.driftEndTime;
  5484. newDetails.driftEnd = oldDetails.driftEnd;
  5485. newDetails.advancedDateTime = oldDetails.advancedDateTime;
  5486. }
  5487. }
  5488. function mergeDateRanges(oldDateRanges, deltaDateRanges, recentlyRemovedDateranges) {
  5489. var dateRanges = _extends({}, oldDateRanges);
  5490. if (recentlyRemovedDateranges) {
  5491. recentlyRemovedDateranges.forEach(function (id) {
  5492. delete dateRanges[id];
  5493. });
  5494. }
  5495. Object.keys(deltaDateRanges).forEach(function (id) {
  5496. var dateRange = new DateRange(deltaDateRanges[id].attr, dateRanges[id]);
  5497. if (dateRange.isValid) {
  5498. dateRanges[id] = dateRange;
  5499. } else {
  5500. logger.warn("Ignoring invalid Playlist Delta Update DATERANGE tag: \"" + JSON.stringify(deltaDateRanges[id].attr) + "\"");
  5501. }
  5502. });
  5503. return dateRanges;
  5504. }
  5505. function mapPartIntersection(oldParts, newParts, intersectionFn) {
  5506. if (oldParts && newParts) {
  5507. var delta = 0;
  5508. for (var i = 0, len = oldParts.length; i <= len; i++) {
  5509. var _oldPart = oldParts[i];
  5510. var _newPart = newParts[i + delta];
  5511. if (_oldPart && _newPart && _oldPart.index === _newPart.index && _oldPart.fragment.sn === _newPart.fragment.sn) {
  5512. intersectionFn(_oldPart, _newPart);
  5513. } else {
  5514. delta--;
  5515. }
  5516. }
  5517. }
  5518. }
  5519. function mapFragmentIntersection(oldDetails, newDetails, intersectionFn) {
  5520. var skippedSegments = newDetails.skippedSegments;
  5521. var start = Math.max(oldDetails.startSN, newDetails.startSN) - newDetails.startSN;
  5522. var end = (oldDetails.fragmentHint ? 1 : 0) + (skippedSegments ? newDetails.endSN : Math.min(oldDetails.endSN, newDetails.endSN)) - newDetails.startSN;
  5523. var delta = newDetails.startSN - oldDetails.startSN;
  5524. var newFrags = newDetails.fragmentHint ? newDetails.fragments.concat(newDetails.fragmentHint) : newDetails.fragments;
  5525. var oldFrags = oldDetails.fragmentHint ? oldDetails.fragments.concat(oldDetails.fragmentHint) : oldDetails.fragments;
  5526. for (var i = start; i <= end; i++) {
  5527. var _oldFrag = oldFrags[delta + i];
  5528. var _newFrag = newFrags[i];
  5529. if (skippedSegments && !_newFrag && i < skippedSegments) {
  5530. // Fill in skipped segments in delta playlist
  5531. _newFrag = newDetails.fragments[i] = _oldFrag;
  5532. }
  5533. if (_oldFrag && _newFrag) {
  5534. intersectionFn(_oldFrag, _newFrag);
  5535. }
  5536. }
  5537. }
  5538. function adjustSliding(oldDetails, newDetails) {
  5539. var delta = newDetails.startSN + newDetails.skippedSegments - oldDetails.startSN;
  5540. var oldFragments = oldDetails.fragments;
  5541. if (delta < 0 || delta >= oldFragments.length) {
  5542. return;
  5543. }
  5544. addSliding(newDetails, oldFragments[delta].start);
  5545. }
  5546. function addSliding(details, start) {
  5547. if (start) {
  5548. var fragments = details.fragments;
  5549. for (var i = details.skippedSegments; i < fragments.length; i++) {
  5550. fragments[i].start += start;
  5551. }
  5552. if (details.fragmentHint) {
  5553. details.fragmentHint.start += start;
  5554. }
  5555. }
  5556. }
  5557. function computeReloadInterval(newDetails, distanceToLiveEdgeMs) {
  5558. if (distanceToLiveEdgeMs === void 0) {
  5559. distanceToLiveEdgeMs = Infinity;
  5560. }
  5561. var reloadInterval = 1000 * newDetails.targetduration;
  5562. if (newDetails.updated) {
  5563. // Use last segment duration when shorter than target duration and near live edge
  5564. var fragments = newDetails.fragments;
  5565. var liveEdgeMaxTargetDurations = 4;
  5566. if (fragments.length && reloadInterval * liveEdgeMaxTargetDurations > distanceToLiveEdgeMs) {
  5567. var lastSegmentDuration = fragments[fragments.length - 1].duration * 1000;
  5568. if (lastSegmentDuration < reloadInterval) {
  5569. reloadInterval = lastSegmentDuration;
  5570. }
  5571. }
  5572. } else {
  5573. // estimate = 'miss half average';
  5574. // follow HLS Spec, If the client reloads a Playlist file and finds that it has not
  5575. // changed then it MUST wait for a period of one-half the target
  5576. // duration before retrying.
  5577. reloadInterval /= 2;
  5578. }
  5579. return Math.round(reloadInterval);
  5580. }
  5581. function getFragmentWithSN(level, sn, fragCurrent) {
  5582. if (!(level != null && level.details)) {
  5583. return null;
  5584. }
  5585. var levelDetails = level.details;
  5586. var fragment = levelDetails.fragments[sn - levelDetails.startSN];
  5587. if (fragment) {
  5588. return fragment;
  5589. }
  5590. fragment = levelDetails.fragmentHint;
  5591. if (fragment && fragment.sn === sn) {
  5592. return fragment;
  5593. }
  5594. if (sn < levelDetails.startSN && fragCurrent && fragCurrent.sn === sn) {
  5595. return fragCurrent;
  5596. }
  5597. return null;
  5598. }
  5599. function getPartWith(level, sn, partIndex) {
  5600. var _level$details;
  5601. if (!(level != null && level.details)) {
  5602. return null;
  5603. }
  5604. return findPart((_level$details = level.details) == null ? void 0 : _level$details.partList, sn, partIndex);
  5605. }
  5606. function findPart(partList, sn, partIndex) {
  5607. if (partList) {
  5608. for (var i = partList.length; i--;) {
  5609. var part = partList[i];
  5610. if (part.index === partIndex && part.fragment.sn === sn) {
  5611. return part;
  5612. }
  5613. }
  5614. }
  5615. return null;
  5616. }
  5617. function reassignFragmentLevelIndexes(levels) {
  5618. levels.forEach(function (level, index) {
  5619. var details = level.details;
  5620. if (details != null && details.fragments) {
  5621. details.fragments.forEach(function (fragment) {
  5622. fragment.level = index;
  5623. });
  5624. }
  5625. });
  5626. }
  5627. function isTimeoutError(error) {
  5628. switch (error.details) {
  5629. case ErrorDetails.FRAG_LOAD_TIMEOUT:
  5630. case ErrorDetails.KEY_LOAD_TIMEOUT:
  5631. case ErrorDetails.LEVEL_LOAD_TIMEOUT:
  5632. case ErrorDetails.MANIFEST_LOAD_TIMEOUT:
  5633. return true;
  5634. }
  5635. return false;
  5636. }
  5637. function getRetryConfig(loadPolicy, error) {
  5638. var isTimeout = isTimeoutError(error);
  5639. return loadPolicy.default[(isTimeout ? 'timeout' : 'error') + "Retry"];
  5640. }
  5641. function getRetryDelay(retryConfig, retryCount) {
  5642. // exponential backoff capped to max retry delay
  5643. var backoffFactor = retryConfig.backoff === 'linear' ? 1 : Math.pow(2, retryCount);
  5644. return Math.min(backoffFactor * retryConfig.retryDelayMs, retryConfig.maxRetryDelayMs);
  5645. }
  5646. function getLoaderConfigWithoutReties(loderConfig) {
  5647. return _objectSpread2(_objectSpread2({}, loderConfig), {
  5648. errorRetry: null,
  5649. timeoutRetry: null
  5650. });
  5651. }
  5652. function shouldRetry(retryConfig, retryCount, isTimeout, loaderResponse) {
  5653. if (!retryConfig) {
  5654. return false;
  5655. }
  5656. var httpStatus = loaderResponse == null ? void 0 : loaderResponse.code;
  5657. var retry = retryCount < retryConfig.maxNumRetry && (retryForHttpStatus(httpStatus) || !!isTimeout);
  5658. return retryConfig.shouldRetry ? retryConfig.shouldRetry(retryConfig, retryCount, isTimeout, loaderResponse, retry) : retry;
  5659. }
  5660. function retryForHttpStatus(httpStatus) {
  5661. // Do not retry on status 4xx, status 0 (CORS error), or undefined (decrypt/gap/parse error)
  5662. return httpStatus === 0 && navigator.onLine === false || !!httpStatus && (httpStatus < 400 || httpStatus > 499);
  5663. }
  5664. var BinarySearch = {
  5665. /**
  5666. * Searches for an item in an array which matches a certain condition.
  5667. * This requires the condition to only match one item in the array,
  5668. * and for the array to be ordered.
  5669. *
  5670. * @param list The array to search.
  5671. * @param comparisonFn
  5672. * Called and provided a candidate item as the first argument.
  5673. * Should return:
  5674. * > -1 if the item should be located at a lower index than the provided item.
  5675. * > 1 if the item should be located at a higher index than the provided item.
  5676. * > 0 if the item is the item you're looking for.
  5677. *
  5678. * @returns the object if found, otherwise returns null
  5679. */
  5680. search: function search(list, comparisonFn) {
  5681. var minIndex = 0;
  5682. var maxIndex = list.length - 1;
  5683. var currentIndex = null;
  5684. var currentElement = null;
  5685. while (minIndex <= maxIndex) {
  5686. currentIndex = (minIndex + maxIndex) / 2 | 0;
  5687. currentElement = list[currentIndex];
  5688. var comparisonResult = comparisonFn(currentElement);
  5689. if (comparisonResult > 0) {
  5690. minIndex = currentIndex + 1;
  5691. } else if (comparisonResult < 0) {
  5692. maxIndex = currentIndex - 1;
  5693. } else {
  5694. return currentElement;
  5695. }
  5696. }
  5697. return null;
  5698. }
  5699. };
  5700. /**
  5701. * Returns first fragment whose endPdt value exceeds the given PDT, or null.
  5702. * @param fragments - The array of candidate fragments
  5703. * @param PDTValue - The PDT value which must be exceeded
  5704. * @param maxFragLookUpTolerance - The amount of time that a fragment's start/end can be within in order to be considered contiguous
  5705. */
  5706. function findFragmentByPDT(fragments, PDTValue, maxFragLookUpTolerance) {
  5707. if (PDTValue === null || !Array.isArray(fragments) || !fragments.length || !isFiniteNumber(PDTValue)) {
  5708. return null;
  5709. }
  5710. // if less than start
  5711. var startPDT = fragments[0].programDateTime;
  5712. if (PDTValue < (startPDT || 0)) {
  5713. return null;
  5714. }
  5715. var endPDT = fragments[fragments.length - 1].endProgramDateTime;
  5716. if (PDTValue >= (endPDT || 0)) {
  5717. return null;
  5718. }
  5719. maxFragLookUpTolerance = maxFragLookUpTolerance || 0;
  5720. for (var seg = 0; seg < fragments.length; ++seg) {
  5721. var frag = fragments[seg];
  5722. if (pdtWithinToleranceTest(PDTValue, maxFragLookUpTolerance, frag)) {
  5723. return frag;
  5724. }
  5725. }
  5726. return null;
  5727. }
  5728. /**
  5729. * Finds a fragment based on the SN of the previous fragment; or based on the needs of the current buffer.
  5730. * This method compensates for small buffer gaps by applying a tolerance to the start of any candidate fragment, thus
  5731. * breaking any traps which would cause the same fragment to be continuously selected within a small range.
  5732. * @param fragPrevious - The last frag successfully appended
  5733. * @param fragments - The array of candidate fragments
  5734. * @param bufferEnd - The end of the contiguous buffered range the playhead is currently within
  5735. * @param maxFragLookUpTolerance - The amount of time that a fragment's start/end can be within in order to be considered contiguous
  5736. * @returns a matching fragment or null
  5737. */
  5738. function findFragmentByPTS(fragPrevious, fragments, bufferEnd, maxFragLookUpTolerance, nextFragLookupTolerance) {
  5739. if (bufferEnd === void 0) {
  5740. bufferEnd = 0;
  5741. }
  5742. if (maxFragLookUpTolerance === void 0) {
  5743. maxFragLookUpTolerance = 0;
  5744. }
  5745. if (nextFragLookupTolerance === void 0) {
  5746. nextFragLookupTolerance = 0.005;
  5747. }
  5748. var fragNext = null;
  5749. if (fragPrevious) {
  5750. fragNext = fragments[fragPrevious.sn - fragments[0].sn + 1] || null;
  5751. // check for buffer-end rounding error
  5752. var bufferEdgeError = fragPrevious.endDTS - bufferEnd;
  5753. if (bufferEdgeError > 0 && bufferEdgeError < 0.0000015) {
  5754. bufferEnd += 0.0000015;
  5755. }
  5756. } else if (bufferEnd === 0 && fragments[0].start === 0) {
  5757. fragNext = fragments[0];
  5758. }
  5759. // Prefer the next fragment if it's within tolerance
  5760. if (fragNext && ((!fragPrevious || fragPrevious.level === fragNext.level) && fragmentWithinToleranceTest(bufferEnd, maxFragLookUpTolerance, fragNext) === 0 || fragmentWithinFastStartSwitch(fragNext, fragPrevious, Math.min(nextFragLookupTolerance, maxFragLookUpTolerance)))) {
  5761. return fragNext;
  5762. }
  5763. // We might be seeking past the tolerance so find the best match
  5764. var foundFragment = BinarySearch.search(fragments, fragmentWithinToleranceTest.bind(null, bufferEnd, maxFragLookUpTolerance));
  5765. if (foundFragment && (foundFragment !== fragPrevious || !fragNext)) {
  5766. return foundFragment;
  5767. }
  5768. // If no match was found return the next fragment after fragPrevious, or null
  5769. return fragNext;
  5770. }
  5771. function fragmentWithinFastStartSwitch(fragNext, fragPrevious, nextFragLookupTolerance) {
  5772. if (fragPrevious && fragPrevious.start === 0 && fragPrevious.level < fragNext.level && (fragPrevious.endPTS || 0) > 0) {
  5773. var firstDuration = fragPrevious.tagList.reduce(function (duration, tag) {
  5774. if (tag[0] === 'INF') {
  5775. duration += parseFloat(tag[1]);
  5776. }
  5777. return duration;
  5778. }, nextFragLookupTolerance);
  5779. return fragNext.start <= firstDuration;
  5780. }
  5781. return false;
  5782. }
  5783. /**
  5784. * The test function used by the findFragmentBySn's BinarySearch to look for the best match to the current buffer conditions.
  5785. * @param candidate - The fragment to test
  5786. * @param bufferEnd - The end of the current buffered range the playhead is currently within
  5787. * @param maxFragLookUpTolerance - The amount of time that a fragment's start can be within in order to be considered contiguous
  5788. * @returns 0 if it matches, 1 if too low, -1 if too high
  5789. */
  5790. function fragmentWithinToleranceTest(bufferEnd, maxFragLookUpTolerance, candidate) {
  5791. if (bufferEnd === void 0) {
  5792. bufferEnd = 0;
  5793. }
  5794. if (maxFragLookUpTolerance === void 0) {
  5795. maxFragLookUpTolerance = 0;
  5796. }
  5797. // eagerly accept an accurate match (no tolerance)
  5798. if (candidate.start <= bufferEnd && candidate.start + candidate.duration > bufferEnd) {
  5799. return 0;
  5800. }
  5801. // offset should be within fragment boundary - config.maxFragLookUpTolerance
  5802. // this is to cope with situations like
  5803. // bufferEnd = 9.991
  5804. // frag[Ø] : [0,10]
  5805. // frag[1] : [10,20]
  5806. // bufferEnd is within frag[0] range ... although what we are expecting is to return frag[1] here
  5807. // frag start frag start+duration
  5808. // |-----------------------------|
  5809. // <---> <--->
  5810. // ...--------><-----------------------------><---------....
  5811. // previous frag matching fragment next frag
  5812. // return -1 return 0 return 1
  5813. // logger.log(`level/sn/start/end/bufEnd:${level}/${candidate.sn}/${candidate.start}/${(candidate.start+candidate.duration)}/${bufferEnd}`);
  5814. // Set the lookup tolerance to be small enough to detect the current segment - ensures we don't skip over very small segments
  5815. var candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration + (candidate.deltaPTS ? candidate.deltaPTS : 0));
  5816. if (candidate.start + candidate.duration - candidateLookupTolerance <= bufferEnd) {
  5817. return 1;
  5818. } else if (candidate.start - candidateLookupTolerance > bufferEnd && candidate.start) {
  5819. // if maxFragLookUpTolerance will have negative value then don't return -1 for first element
  5820. return -1;
  5821. }
  5822. return 0;
  5823. }
  5824. /**
  5825. * The test function used by the findFragmentByPdt's BinarySearch to look for the best match to the current buffer conditions.
  5826. * This function tests the candidate's program date time values, as represented in Unix time
  5827. * @param candidate - The fragment to test
  5828. * @param pdtBufferEnd - The Unix time representing the end of the current buffered range
  5829. * @param maxFragLookUpTolerance - The amount of time that a fragment's start can be within in order to be considered contiguous
  5830. * @returns true if contiguous, false otherwise
  5831. */
  5832. function pdtWithinToleranceTest(pdtBufferEnd, maxFragLookUpTolerance, candidate) {
  5833. var candidateLookupTolerance = Math.min(maxFragLookUpTolerance, candidate.duration + (candidate.deltaPTS ? candidate.deltaPTS : 0)) * 1000;
  5834. // endProgramDateTime can be null, default to zero
  5835. var endProgramDateTime = candidate.endProgramDateTime || 0;
  5836. return endProgramDateTime - candidateLookupTolerance > pdtBufferEnd;
  5837. }
  5838. function findFragWithCC(fragments, cc) {
  5839. return BinarySearch.search(fragments, function (candidate) {
  5840. if (candidate.cc < cc) {
  5841. return 1;
  5842. } else if (candidate.cc > cc) {
  5843. return -1;
  5844. } else {
  5845. return 0;
  5846. }
  5847. });
  5848. }
  5849. var NetworkErrorAction = {
  5850. DoNothing: 0,
  5851. SendEndCallback: 1,
  5852. SendAlternateToPenaltyBox: 2,
  5853. RemoveAlternatePermanently: 3,
  5854. InsertDiscontinuity: 4,
  5855. RetryRequest: 5
  5856. };
  5857. var ErrorActionFlags = {
  5858. None: 0,
  5859. MoveAllAlternatesMatchingHost: 1,
  5860. MoveAllAlternatesMatchingHDCP: 2,
  5861. SwitchToSDR: 4
  5862. }; // Reserved for future use
  5863. var ErrorController = /*#__PURE__*/function () {
  5864. function ErrorController(hls) {
  5865. this.hls = void 0;
  5866. this.playlistError = 0;
  5867. this.penalizedRenditions = {};
  5868. this.log = void 0;
  5869. this.warn = void 0;
  5870. this.error = void 0;
  5871. this.hls = hls;
  5872. this.log = logger.log.bind(logger, "[info]:");
  5873. this.warn = logger.warn.bind(logger, "[warning]:");
  5874. this.error = logger.error.bind(logger, "[error]:");
  5875. this.registerListeners();
  5876. }
  5877. var _proto = ErrorController.prototype;
  5878. _proto.registerListeners = function registerListeners() {
  5879. var hls = this.hls;
  5880. hls.on(Events.ERROR, this.onError, this);
  5881. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  5882. hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  5883. };
  5884. _proto.unregisterListeners = function unregisterListeners() {
  5885. var hls = this.hls;
  5886. if (!hls) {
  5887. return;
  5888. }
  5889. hls.off(Events.ERROR, this.onError, this);
  5890. hls.off(Events.ERROR, this.onErrorOut, this);
  5891. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  5892. hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  5893. };
  5894. _proto.destroy = function destroy() {
  5895. this.unregisterListeners();
  5896. // @ts-ignore
  5897. this.hls = null;
  5898. this.penalizedRenditions = {};
  5899. };
  5900. _proto.startLoad = function startLoad(startPosition) {};
  5901. _proto.stopLoad = function stopLoad() {
  5902. this.playlistError = 0;
  5903. };
  5904. _proto.getVariantLevelIndex = function getVariantLevelIndex(frag) {
  5905. return (frag == null ? void 0 : frag.type) === PlaylistLevelType.MAIN ? frag.level : this.hls.loadLevel;
  5906. };
  5907. _proto.onManifestLoading = function onManifestLoading() {
  5908. this.playlistError = 0;
  5909. this.penalizedRenditions = {};
  5910. };
  5911. _proto.onLevelUpdated = function onLevelUpdated() {
  5912. this.playlistError = 0;
  5913. };
  5914. _proto.onError = function onError(event, data) {
  5915. var _data$frag, _data$level;
  5916. if (data.fatal) {
  5917. return;
  5918. }
  5919. var hls = this.hls;
  5920. var context = data.context;
  5921. switch (data.details) {
  5922. case ErrorDetails.FRAG_LOAD_ERROR:
  5923. case ErrorDetails.FRAG_LOAD_TIMEOUT:
  5924. case ErrorDetails.KEY_LOAD_ERROR:
  5925. case ErrorDetails.KEY_LOAD_TIMEOUT:
  5926. data.errorAction = this.getFragRetryOrSwitchAction(data);
  5927. return;
  5928. case ErrorDetails.FRAG_PARSING_ERROR:
  5929. // ignore empty segment errors marked as gap
  5930. if ((_data$frag = data.frag) != null && _data$frag.gap) {
  5931. data.errorAction = {
  5932. action: NetworkErrorAction.DoNothing,
  5933. flags: ErrorActionFlags.None
  5934. };
  5935. return;
  5936. }
  5937. // falls through
  5938. case ErrorDetails.FRAG_GAP:
  5939. case ErrorDetails.FRAG_DECRYPT_ERROR:
  5940. {
  5941. // Switch level if possible, otherwise allow retry count to reach max error retries
  5942. data.errorAction = this.getFragRetryOrSwitchAction(data);
  5943. data.errorAction.action = NetworkErrorAction.SendAlternateToPenaltyBox;
  5944. return;
  5945. }
  5946. case ErrorDetails.LEVEL_EMPTY_ERROR:
  5947. case ErrorDetails.LEVEL_PARSING_ERROR:
  5948. {
  5949. var _data$context, _data$context$levelDe;
  5950. // Only retry when empty and live
  5951. var levelIndex = data.parent === PlaylistLevelType.MAIN ? data.level : hls.loadLevel;
  5952. if (data.details === ErrorDetails.LEVEL_EMPTY_ERROR && !!((_data$context = data.context) != null && (_data$context$levelDe = _data$context.levelDetails) != null && _data$context$levelDe.live)) {
  5953. data.errorAction = this.getPlaylistRetryOrSwitchAction(data, levelIndex);
  5954. } else {
  5955. // Escalate to fatal if not retrying or switching
  5956. data.levelRetry = false;
  5957. data.errorAction = this.getLevelSwitchAction(data, levelIndex);
  5958. }
  5959. }
  5960. return;
  5961. case ErrorDetails.LEVEL_LOAD_ERROR:
  5962. case ErrorDetails.LEVEL_LOAD_TIMEOUT:
  5963. if (typeof (context == null ? void 0 : context.level) === 'number') {
  5964. data.errorAction = this.getPlaylistRetryOrSwitchAction(data, context.level);
  5965. }
  5966. return;
  5967. case ErrorDetails.AUDIO_TRACK_LOAD_ERROR:
  5968. case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT:
  5969. case ErrorDetails.SUBTITLE_LOAD_ERROR:
  5970. case ErrorDetails.SUBTITLE_TRACK_LOAD_TIMEOUT:
  5971. if (context) {
  5972. var level = hls.levels[hls.loadLevel];
  5973. if (level && (context.type === PlaylistContextType.AUDIO_TRACK && level.hasAudioGroup(context.groupId) || context.type === PlaylistContextType.SUBTITLE_TRACK && level.hasSubtitleGroup(context.groupId))) {
  5974. // Perform Pathway switch or Redundant failover if possible for fastest recovery
  5975. // otherwise allow playlist retry count to reach max error retries
  5976. data.errorAction = this.getPlaylistRetryOrSwitchAction(data, hls.loadLevel);
  5977. data.errorAction.action = NetworkErrorAction.SendAlternateToPenaltyBox;
  5978. data.errorAction.flags = ErrorActionFlags.MoveAllAlternatesMatchingHost;
  5979. return;
  5980. }
  5981. }
  5982. return;
  5983. case ErrorDetails.KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED:
  5984. {
  5985. var _level = hls.levels[hls.loadLevel];
  5986. var restrictedHdcpLevel = _level == null ? void 0 : _level.attrs['HDCP-LEVEL'];
  5987. if (restrictedHdcpLevel) {
  5988. data.errorAction = {
  5989. action: NetworkErrorAction.SendAlternateToPenaltyBox,
  5990. flags: ErrorActionFlags.MoveAllAlternatesMatchingHDCP,
  5991. hdcpLevel: restrictedHdcpLevel
  5992. };
  5993. } else {
  5994. this.keySystemError(data);
  5995. }
  5996. }
  5997. return;
  5998. case ErrorDetails.BUFFER_ADD_CODEC_ERROR:
  5999. case ErrorDetails.REMUX_ALLOC_ERROR:
  6000. case ErrorDetails.BUFFER_APPEND_ERROR:
  6001. data.errorAction = this.getLevelSwitchAction(data, (_data$level = data.level) != null ? _data$level : hls.loadLevel);
  6002. return;
  6003. case ErrorDetails.INTERNAL_EXCEPTION:
  6004. case ErrorDetails.BUFFER_APPENDING_ERROR:
  6005. case ErrorDetails.BUFFER_FULL_ERROR:
  6006. case ErrorDetails.LEVEL_SWITCH_ERROR:
  6007. case ErrorDetails.BUFFER_STALLED_ERROR:
  6008. case ErrorDetails.BUFFER_SEEK_OVER_HOLE:
  6009. case ErrorDetails.BUFFER_NUDGE_ON_STALL:
  6010. data.errorAction = {
  6011. action: NetworkErrorAction.DoNothing,
  6012. flags: ErrorActionFlags.None
  6013. };
  6014. return;
  6015. }
  6016. if (data.type === ErrorTypes.KEY_SYSTEM_ERROR) {
  6017. this.keySystemError(data);
  6018. }
  6019. };
  6020. _proto.keySystemError = function keySystemError(data) {
  6021. var levelIndex = this.getVariantLevelIndex(data.frag);
  6022. // Do not retry level. Escalate to fatal if switching levels fails.
  6023. data.levelRetry = false;
  6024. data.errorAction = this.getLevelSwitchAction(data, levelIndex);
  6025. };
  6026. _proto.getPlaylistRetryOrSwitchAction = function getPlaylistRetryOrSwitchAction(data, levelIndex) {
  6027. var hls = this.hls;
  6028. var retryConfig = getRetryConfig(hls.config.playlistLoadPolicy, data);
  6029. var retryCount = this.playlistError++;
  6030. var retry = shouldRetry(retryConfig, retryCount, isTimeoutError(data), data.response);
  6031. if (retry) {
  6032. return {
  6033. action: NetworkErrorAction.RetryRequest,
  6034. flags: ErrorActionFlags.None,
  6035. retryConfig: retryConfig,
  6036. retryCount: retryCount
  6037. };
  6038. }
  6039. var errorAction = this.getLevelSwitchAction(data, levelIndex);
  6040. if (retryConfig) {
  6041. errorAction.retryConfig = retryConfig;
  6042. errorAction.retryCount = retryCount;
  6043. }
  6044. return errorAction;
  6045. };
  6046. _proto.getFragRetryOrSwitchAction = function getFragRetryOrSwitchAction(data) {
  6047. var hls = this.hls;
  6048. // Share fragment error count accross media options (main, audio, subs)
  6049. // This allows for level based rendition switching when media option assets fail
  6050. var variantLevelIndex = this.getVariantLevelIndex(data.frag);
  6051. var level = hls.levels[variantLevelIndex];
  6052. var _hls$config = hls.config,
  6053. fragLoadPolicy = _hls$config.fragLoadPolicy,
  6054. keyLoadPolicy = _hls$config.keyLoadPolicy;
  6055. var retryConfig = getRetryConfig(data.details.startsWith('key') ? keyLoadPolicy : fragLoadPolicy, data);
  6056. var fragmentErrors = hls.levels.reduce(function (acc, level) {
  6057. return acc + level.fragmentError;
  6058. }, 0);
  6059. // Switch levels when out of retried or level index out of bounds
  6060. if (level) {
  6061. if (data.details !== ErrorDetails.FRAG_GAP) {
  6062. level.fragmentError++;
  6063. }
  6064. var retry = shouldRetry(retryConfig, fragmentErrors, isTimeoutError(data), data.response);
  6065. if (retry) {
  6066. return {
  6067. action: NetworkErrorAction.RetryRequest,
  6068. flags: ErrorActionFlags.None,
  6069. retryConfig: retryConfig,
  6070. retryCount: fragmentErrors
  6071. };
  6072. }
  6073. }
  6074. // Reach max retry count, or Missing level reference
  6075. // Switch to valid index
  6076. var errorAction = this.getLevelSwitchAction(data, variantLevelIndex);
  6077. // Add retry details to allow skipping of FRAG_PARSING_ERROR
  6078. if (retryConfig) {
  6079. errorAction.retryConfig = retryConfig;
  6080. errorAction.retryCount = fragmentErrors;
  6081. }
  6082. return errorAction;
  6083. };
  6084. _proto.getLevelSwitchAction = function getLevelSwitchAction(data, levelIndex) {
  6085. var hls = this.hls;
  6086. if (levelIndex === null || levelIndex === undefined) {
  6087. levelIndex = hls.loadLevel;
  6088. }
  6089. var level = this.hls.levels[levelIndex];
  6090. if (level) {
  6091. var _data$frag2, _data$context2;
  6092. var errorDetails = data.details;
  6093. level.loadError++;
  6094. if (errorDetails === ErrorDetails.BUFFER_APPEND_ERROR) {
  6095. level.fragmentError++;
  6096. }
  6097. // Search for next level to retry
  6098. var nextLevel = -1;
  6099. var levels = hls.levels,
  6100. loadLevel = hls.loadLevel,
  6101. minAutoLevel = hls.minAutoLevel,
  6102. maxAutoLevel = hls.maxAutoLevel;
  6103. if (!hls.autoLevelEnabled) {
  6104. hls.loadLevel = -1;
  6105. }
  6106. var fragErrorType = (_data$frag2 = data.frag) == null ? void 0 : _data$frag2.type;
  6107. // Find alternate audio codec if available on audio codec error
  6108. var isAudioCodecError = fragErrorType === PlaylistLevelType.AUDIO && errorDetails === ErrorDetails.FRAG_PARSING_ERROR || data.sourceBufferName === 'audio' && (errorDetails === ErrorDetails.BUFFER_ADD_CODEC_ERROR || errorDetails === ErrorDetails.BUFFER_APPEND_ERROR);
  6109. var findAudioCodecAlternate = isAudioCodecError && levels.some(function (_ref) {
  6110. var audioCodec = _ref.audioCodec;
  6111. return level.audioCodec !== audioCodec;
  6112. });
  6113. // Find alternate video codec if available on video codec error
  6114. var isVideoCodecError = data.sourceBufferName === 'video' && (errorDetails === ErrorDetails.BUFFER_ADD_CODEC_ERROR || errorDetails === ErrorDetails.BUFFER_APPEND_ERROR);
  6115. var findVideoCodecAlternate = isVideoCodecError && levels.some(function (_ref2) {
  6116. var codecSet = _ref2.codecSet,
  6117. audioCodec = _ref2.audioCodec;
  6118. return level.codecSet !== codecSet && level.audioCodec === audioCodec;
  6119. });
  6120. var _ref3 = (_data$context2 = data.context) != null ? _data$context2 : {},
  6121. playlistErrorType = _ref3.type,
  6122. playlistErrorGroupId = _ref3.groupId;
  6123. var _loop = function _loop() {
  6124. var candidate = (i + loadLevel) % levels.length;
  6125. if (candidate !== loadLevel && candidate >= minAutoLevel && candidate <= maxAutoLevel && levels[candidate].loadError === 0) {
  6126. var _level$audioGroups, _level$subtitleGroups;
  6127. var levelCandidate = levels[candidate];
  6128. // Skip level switch if GAP tag is found in next level at same position
  6129. if (errorDetails === ErrorDetails.FRAG_GAP && fragErrorType === PlaylistLevelType.MAIN && data.frag) {
  6130. var levelDetails = levels[candidate].details;
  6131. if (levelDetails) {
  6132. var fragCandidate = findFragmentByPTS(data.frag, levelDetails.fragments, data.frag.start);
  6133. if (fragCandidate != null && fragCandidate.gap) {
  6134. return 0; // continue
  6135. }
  6136. }
  6137. } else if (playlistErrorType === PlaylistContextType.AUDIO_TRACK && levelCandidate.hasAudioGroup(playlistErrorGroupId) || playlistErrorType === PlaylistContextType.SUBTITLE_TRACK && levelCandidate.hasSubtitleGroup(playlistErrorGroupId)) {
  6138. // For audio/subs playlist errors find another group ID or fallthrough to redundant fail-over
  6139. return 0; // continue
  6140. } else if (fragErrorType === PlaylistLevelType.AUDIO && (_level$audioGroups = level.audioGroups) != null && _level$audioGroups.some(function (groupId) {
  6141. return levelCandidate.hasAudioGroup(groupId);
  6142. }) || fragErrorType === PlaylistLevelType.SUBTITLE && (_level$subtitleGroups = level.subtitleGroups) != null && _level$subtitleGroups.some(function (groupId) {
  6143. return levelCandidate.hasSubtitleGroup(groupId);
  6144. }) || findAudioCodecAlternate && level.audioCodec === levelCandidate.audioCodec || !findAudioCodecAlternate && level.audioCodec !== levelCandidate.audioCodec || findVideoCodecAlternate && level.codecSet === levelCandidate.codecSet) {
  6145. // For video/audio/subs frag errors find another group ID or fallthrough to redundant fail-over
  6146. return 0; // continue
  6147. }
  6148. nextLevel = candidate;
  6149. return 1; // break
  6150. }
  6151. },
  6152. _ret;
  6153. for (var i = levels.length; i--;) {
  6154. _ret = _loop();
  6155. if (_ret === 0) continue;
  6156. if (_ret === 1) break;
  6157. }
  6158. if (nextLevel > -1 && hls.loadLevel !== nextLevel) {
  6159. data.levelRetry = true;
  6160. this.playlistError = 0;
  6161. return {
  6162. action: NetworkErrorAction.SendAlternateToPenaltyBox,
  6163. flags: ErrorActionFlags.None,
  6164. nextAutoLevel: nextLevel
  6165. };
  6166. }
  6167. }
  6168. // No levels to switch / Manual level selection / Level not found
  6169. // Resolve with Pathway switch, Redundant fail-over, or stay on lowest Level
  6170. return {
  6171. action: NetworkErrorAction.SendAlternateToPenaltyBox,
  6172. flags: ErrorActionFlags.MoveAllAlternatesMatchingHost
  6173. };
  6174. };
  6175. _proto.onErrorOut = function onErrorOut(event, data) {
  6176. var _data$errorAction;
  6177. switch ((_data$errorAction = data.errorAction) == null ? void 0 : _data$errorAction.action) {
  6178. case NetworkErrorAction.DoNothing:
  6179. break;
  6180. case NetworkErrorAction.SendAlternateToPenaltyBox:
  6181. this.sendAlternateToPenaltyBox(data);
  6182. if (!data.errorAction.resolved && data.details !== ErrorDetails.FRAG_GAP) {
  6183. data.fatal = true;
  6184. } else if (/MediaSource readyState: ended/.test(data.error.message)) {
  6185. this.warn("MediaSource ended after \"" + data.sourceBufferName + "\" sourceBuffer append error. Attempting to recover from media error.");
  6186. this.hls.recoverMediaError();
  6187. }
  6188. break;
  6189. }
  6190. if (data.fatal) {
  6191. this.hls.stopLoad();
  6192. return;
  6193. }
  6194. };
  6195. _proto.sendAlternateToPenaltyBox = function sendAlternateToPenaltyBox(data) {
  6196. var hls = this.hls;
  6197. var errorAction = data.errorAction;
  6198. if (!errorAction) {
  6199. return;
  6200. }
  6201. var flags = errorAction.flags,
  6202. hdcpLevel = errorAction.hdcpLevel,
  6203. nextAutoLevel = errorAction.nextAutoLevel;
  6204. switch (flags) {
  6205. case ErrorActionFlags.None:
  6206. this.switchLevel(data, nextAutoLevel);
  6207. break;
  6208. case ErrorActionFlags.MoveAllAlternatesMatchingHDCP:
  6209. if (hdcpLevel) {
  6210. hls.maxHdcpLevel = HdcpLevels[HdcpLevels.indexOf(hdcpLevel) - 1];
  6211. errorAction.resolved = true;
  6212. }
  6213. this.warn("Restricting playback to HDCP-LEVEL of \"" + hls.maxHdcpLevel + "\" or lower");
  6214. break;
  6215. }
  6216. // If not resolved by previous actions try to switch to next level
  6217. if (!errorAction.resolved) {
  6218. this.switchLevel(data, nextAutoLevel);
  6219. }
  6220. };
  6221. _proto.switchLevel = function switchLevel(data, levelIndex) {
  6222. if (levelIndex !== undefined && data.errorAction) {
  6223. this.warn("switching to level " + levelIndex + " after " + data.details);
  6224. this.hls.nextAutoLevel = levelIndex;
  6225. data.errorAction.resolved = true;
  6226. // Stream controller is responsible for this but won't switch on false start
  6227. this.hls.nextLoadLevel = this.hls.nextAutoLevel;
  6228. }
  6229. };
  6230. return ErrorController;
  6231. }();
  6232. var BasePlaylistController = /*#__PURE__*/function () {
  6233. function BasePlaylistController(hls, logPrefix) {
  6234. this.hls = void 0;
  6235. this.timer = -1;
  6236. this.requestScheduled = -1;
  6237. this.canLoad = false;
  6238. this.log = void 0;
  6239. this.warn = void 0;
  6240. this.log = logger.log.bind(logger, logPrefix + ":");
  6241. this.warn = logger.warn.bind(logger, logPrefix + ":");
  6242. this.hls = hls;
  6243. }
  6244. var _proto = BasePlaylistController.prototype;
  6245. _proto.destroy = function destroy() {
  6246. this.clearTimer();
  6247. // @ts-ignore
  6248. this.hls = this.log = this.warn = null;
  6249. };
  6250. _proto.clearTimer = function clearTimer() {
  6251. if (this.timer !== -1) {
  6252. self.clearTimeout(this.timer);
  6253. this.timer = -1;
  6254. }
  6255. };
  6256. _proto.startLoad = function startLoad() {
  6257. this.canLoad = true;
  6258. this.requestScheduled = -1;
  6259. this.loadPlaylist();
  6260. };
  6261. _proto.stopLoad = function stopLoad() {
  6262. this.canLoad = false;
  6263. this.clearTimer();
  6264. };
  6265. _proto.switchParams = function switchParams(playlistUri, previous, current) {
  6266. var renditionReports = previous == null ? void 0 : previous.renditionReports;
  6267. if (renditionReports) {
  6268. var foundIndex = -1;
  6269. for (var i = 0; i < renditionReports.length; i++) {
  6270. var attr = renditionReports[i];
  6271. var uri = void 0;
  6272. try {
  6273. uri = new self.URL(attr.URI, previous.url).href;
  6274. } catch (error) {
  6275. logger.warn("Could not construct new URL for Rendition Report: " + error);
  6276. uri = attr.URI || '';
  6277. }
  6278. // Use exact match. Otherwise, the last partial match, if any, will be used
  6279. // (Playlist URI includes a query string that the Rendition Report does not)
  6280. if (uri === playlistUri) {
  6281. foundIndex = i;
  6282. break;
  6283. } else if (uri === playlistUri.substring(0, uri.length)) {
  6284. foundIndex = i;
  6285. }
  6286. }
  6287. if (foundIndex !== -1) {
  6288. var _attr = renditionReports[foundIndex];
  6289. var msn = parseInt(_attr['LAST-MSN']) || (previous == null ? void 0 : previous.lastPartSn);
  6290. var part = parseInt(_attr['LAST-PART']) || (previous == null ? void 0 : previous.lastPartIndex);
  6291. if (this.hls.config.lowLatencyMode) {
  6292. var currentGoal = Math.min(previous.age - previous.partTarget, previous.targetduration);
  6293. if (part >= 0 && currentGoal > previous.partTarget) {
  6294. part += 1;
  6295. }
  6296. }
  6297. var skip = current && getSkipValue(current);
  6298. return new HlsUrlParameters(msn, part >= 0 ? part : undefined, skip);
  6299. }
  6300. }
  6301. };
  6302. _proto.loadPlaylist = function loadPlaylist(hlsUrlParameters) {
  6303. if (this.requestScheduled === -1) {
  6304. this.requestScheduled = self.performance.now();
  6305. }
  6306. // Loading is handled by the subclasses
  6307. };
  6308. _proto.shouldLoadPlaylist = function shouldLoadPlaylist(playlist) {
  6309. return this.canLoad && !!playlist && !!playlist.url && (!playlist.details || playlist.details.live);
  6310. };
  6311. _proto.shouldReloadPlaylist = function shouldReloadPlaylist(playlist) {
  6312. return this.timer === -1 && this.requestScheduled === -1 && this.shouldLoadPlaylist(playlist);
  6313. };
  6314. _proto.playlistLoaded = function playlistLoaded(index, data, previousDetails) {
  6315. var _this = this;
  6316. var details = data.details,
  6317. stats = data.stats;
  6318. // Set last updated date-time
  6319. var now = self.performance.now();
  6320. var elapsed = stats.loading.first ? Math.max(0, now - stats.loading.first) : 0;
  6321. details.advancedDateTime = Date.now() - elapsed;
  6322. // if current playlist is a live playlist, arm a timer to reload it
  6323. if (details.live || previousDetails != null && previousDetails.live) {
  6324. details.reloaded(previousDetails);
  6325. if (previousDetails) {
  6326. this.log("live playlist " + index + " " + (details.advanced ? 'REFRESHED ' + details.lastPartSn + '-' + details.lastPartIndex : details.updated ? 'UPDATED' : 'MISSED'));
  6327. }
  6328. // Merge live playlists to adjust fragment starts and fill in delta playlist skipped segments
  6329. if (previousDetails && details.fragments.length > 0) {
  6330. mergeDetails(previousDetails, details);
  6331. }
  6332. if (!this.canLoad || !details.live) {
  6333. return;
  6334. }
  6335. var deliveryDirectives;
  6336. var msn = undefined;
  6337. var part = undefined;
  6338. if (details.canBlockReload && details.endSN && details.advanced) {
  6339. // Load level with LL-HLS delivery directives
  6340. var lowLatencyMode = this.hls.config.lowLatencyMode;
  6341. var lastPartSn = details.lastPartSn;
  6342. var endSn = details.endSN;
  6343. var lastPartIndex = details.lastPartIndex;
  6344. var hasParts = lastPartIndex !== -1;
  6345. var lastPart = lastPartSn === endSn;
  6346. // When low latency mode is disabled, we'll skip part requests once the last part index is found
  6347. var nextSnStartIndex = lowLatencyMode ? 0 : lastPartIndex;
  6348. if (hasParts) {
  6349. msn = lastPart ? endSn + 1 : lastPartSn;
  6350. part = lastPart ? nextSnStartIndex : lastPartIndex + 1;
  6351. } else {
  6352. msn = endSn + 1;
  6353. }
  6354. // Low-Latency CDN Tune-in: "age" header and time since load indicates we're behind by more than one part
  6355. // Update directives to obtain the Playlist that has the estimated additional duration of media
  6356. var lastAdvanced = details.age;
  6357. var cdnAge = lastAdvanced + details.ageHeader;
  6358. var currentGoal = Math.min(cdnAge - details.partTarget, details.targetduration * 1.5);
  6359. if (currentGoal > 0) {
  6360. if (previousDetails && currentGoal > previousDetails.tuneInGoal) {
  6361. // If we attempted to get the next or latest playlist update, but currentGoal increased,
  6362. // then we either can't catchup, or the "age" header cannot be trusted.
  6363. this.warn("CDN Tune-in goal increased from: " + previousDetails.tuneInGoal + " to: " + currentGoal + " with playlist age: " + details.age);
  6364. currentGoal = 0;
  6365. } else {
  6366. var segments = Math.floor(currentGoal / details.targetduration);
  6367. msn += segments;
  6368. if (part !== undefined) {
  6369. var parts = Math.round(currentGoal % details.targetduration / details.partTarget);
  6370. part += parts;
  6371. }
  6372. this.log("CDN Tune-in age: " + details.ageHeader + "s last advanced " + lastAdvanced.toFixed(2) + "s goal: " + currentGoal + " skip sn " + segments + " to part " + part);
  6373. }
  6374. details.tuneInGoal = currentGoal;
  6375. }
  6376. deliveryDirectives = this.getDeliveryDirectives(details, data.deliveryDirectives, msn, part);
  6377. if (lowLatencyMode || !lastPart) {
  6378. this.loadPlaylist(deliveryDirectives);
  6379. return;
  6380. }
  6381. } else if (details.canBlockReload || details.canSkipUntil) {
  6382. deliveryDirectives = this.getDeliveryDirectives(details, data.deliveryDirectives, msn, part);
  6383. }
  6384. var bufferInfo = this.hls.mainForwardBufferInfo;
  6385. var position = bufferInfo ? bufferInfo.end - bufferInfo.len : 0;
  6386. var distanceToLiveEdgeMs = (details.edge - position) * 1000;
  6387. var reloadInterval = computeReloadInterval(details, distanceToLiveEdgeMs);
  6388. if (details.updated && now > this.requestScheduled + reloadInterval) {
  6389. this.requestScheduled = stats.loading.start;
  6390. }
  6391. if (msn !== undefined && details.canBlockReload) {
  6392. this.requestScheduled = stats.loading.first + reloadInterval - (details.partTarget * 1000 || 1000);
  6393. } else if (this.requestScheduled === -1 || this.requestScheduled + reloadInterval < now) {
  6394. this.requestScheduled = now;
  6395. } else if (this.requestScheduled - now <= 0) {
  6396. this.requestScheduled += reloadInterval;
  6397. }
  6398. var estimatedTimeUntilUpdate = this.requestScheduled - now;
  6399. estimatedTimeUntilUpdate = Math.max(0, estimatedTimeUntilUpdate);
  6400. this.log("reload live playlist " + index + " in " + Math.round(estimatedTimeUntilUpdate) + " ms");
  6401. // this.log(
  6402. // `live reload ${details.updated ? 'REFRESHED' : 'MISSED'}
  6403. // reload in ${estimatedTimeUntilUpdate / 1000}
  6404. // round trip ${(stats.loading.end - stats.loading.start) / 1000}
  6405. // diff ${
  6406. // (reloadInterval -
  6407. // (estimatedTimeUntilUpdate +
  6408. // stats.loading.end -
  6409. // stats.loading.start)) /
  6410. // 1000
  6411. // }
  6412. // reload interval ${reloadInterval / 1000}
  6413. // target duration ${details.targetduration}
  6414. // distance to edge ${distanceToLiveEdgeMs / 1000}`
  6415. // );
  6416. this.timer = self.setTimeout(function () {
  6417. return _this.loadPlaylist(deliveryDirectives);
  6418. }, estimatedTimeUntilUpdate);
  6419. } else {
  6420. this.clearTimer();
  6421. }
  6422. };
  6423. _proto.getDeliveryDirectives = function getDeliveryDirectives(details, previousDeliveryDirectives, msn, part) {
  6424. var skip = getSkipValue(details);
  6425. if (previousDeliveryDirectives != null && previousDeliveryDirectives.skip && details.deltaUpdateFailed) {
  6426. msn = previousDeliveryDirectives.msn;
  6427. part = previousDeliveryDirectives.part;
  6428. skip = HlsSkip.No;
  6429. }
  6430. return new HlsUrlParameters(msn, part, skip);
  6431. };
  6432. _proto.checkRetry = function checkRetry(errorEvent) {
  6433. var _this2 = this;
  6434. var errorDetails = errorEvent.details;
  6435. var isTimeout = isTimeoutError(errorEvent);
  6436. var errorAction = errorEvent.errorAction;
  6437. var _ref = errorAction || {},
  6438. action = _ref.action,
  6439. _ref$retryCount = _ref.retryCount,
  6440. retryCount = _ref$retryCount === void 0 ? 0 : _ref$retryCount,
  6441. retryConfig = _ref.retryConfig;
  6442. var retry = !!errorAction && !!retryConfig && (action === NetworkErrorAction.RetryRequest || !errorAction.resolved && action === NetworkErrorAction.SendAlternateToPenaltyBox);
  6443. if (retry) {
  6444. var _errorEvent$context;
  6445. this.requestScheduled = -1;
  6446. if (retryCount >= retryConfig.maxNumRetry) {
  6447. return false;
  6448. }
  6449. if (isTimeout && (_errorEvent$context = errorEvent.context) != null && _errorEvent$context.deliveryDirectives) {
  6450. // The LL-HLS request already timed out so retry immediately
  6451. this.warn("Retrying playlist loading " + (retryCount + 1) + "/" + retryConfig.maxNumRetry + " after \"" + errorDetails + "\" without delivery-directives");
  6452. this.loadPlaylist();
  6453. } else {
  6454. var delay = getRetryDelay(retryConfig, retryCount);
  6455. // Schedule level/track reload
  6456. this.timer = self.setTimeout(function () {
  6457. return _this2.loadPlaylist();
  6458. }, delay);
  6459. this.warn("Retrying playlist loading " + (retryCount + 1) + "/" + retryConfig.maxNumRetry + " after \"" + errorDetails + "\" in " + delay + "ms");
  6460. }
  6461. // `levelRetry = true` used to inform other controllers that a retry is happening
  6462. errorEvent.levelRetry = true;
  6463. errorAction.resolved = true;
  6464. }
  6465. return retry;
  6466. };
  6467. return BasePlaylistController;
  6468. }();
  6469. /*
  6470. * compute an Exponential Weighted moving average
  6471. * - https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
  6472. * - heavily inspired from shaka-player
  6473. */
  6474. var EWMA = /*#__PURE__*/function () {
  6475. // About half of the estimated value will be from the last |halfLife| samples by weight.
  6476. function EWMA(halfLife, estimate, weight) {
  6477. if (estimate === void 0) {
  6478. estimate = 0;
  6479. }
  6480. if (weight === void 0) {
  6481. weight = 0;
  6482. }
  6483. this.halfLife = void 0;
  6484. this.alpha_ = void 0;
  6485. this.estimate_ = void 0;
  6486. this.totalWeight_ = void 0;
  6487. this.halfLife = halfLife;
  6488. // Larger values of alpha expire historical data more slowly.
  6489. this.alpha_ = halfLife ? Math.exp(Math.log(0.5) / halfLife) : 0;
  6490. this.estimate_ = estimate;
  6491. this.totalWeight_ = weight;
  6492. }
  6493. var _proto = EWMA.prototype;
  6494. _proto.sample = function sample(weight, value) {
  6495. var adjAlpha = Math.pow(this.alpha_, weight);
  6496. this.estimate_ = value * (1 - adjAlpha) + adjAlpha * this.estimate_;
  6497. this.totalWeight_ += weight;
  6498. };
  6499. _proto.getTotalWeight = function getTotalWeight() {
  6500. return this.totalWeight_;
  6501. };
  6502. _proto.getEstimate = function getEstimate() {
  6503. if (this.alpha_) {
  6504. var zeroFactor = 1 - Math.pow(this.alpha_, this.totalWeight_);
  6505. if (zeroFactor) {
  6506. return this.estimate_ / zeroFactor;
  6507. }
  6508. }
  6509. return this.estimate_;
  6510. };
  6511. return EWMA;
  6512. }();
  6513. /*
  6514. * EWMA Bandwidth Estimator
  6515. * - heavily inspired from shaka-player
  6516. * Tracks bandwidth samples and estimates available bandwidth.
  6517. * Based on the minimum of two exponentially-weighted moving averages with
  6518. * different half-lives.
  6519. */
  6520. var EwmaBandWidthEstimator = /*#__PURE__*/function () {
  6521. function EwmaBandWidthEstimator(slow, fast, defaultEstimate, defaultTTFB) {
  6522. if (defaultTTFB === void 0) {
  6523. defaultTTFB = 100;
  6524. }
  6525. this.defaultEstimate_ = void 0;
  6526. this.minWeight_ = void 0;
  6527. this.minDelayMs_ = void 0;
  6528. this.slow_ = void 0;
  6529. this.fast_ = void 0;
  6530. this.defaultTTFB_ = void 0;
  6531. this.ttfb_ = void 0;
  6532. this.defaultEstimate_ = defaultEstimate;
  6533. this.minWeight_ = 0.001;
  6534. this.minDelayMs_ = 50;
  6535. this.slow_ = new EWMA(slow);
  6536. this.fast_ = new EWMA(fast);
  6537. this.defaultTTFB_ = defaultTTFB;
  6538. this.ttfb_ = new EWMA(slow);
  6539. }
  6540. var _proto = EwmaBandWidthEstimator.prototype;
  6541. _proto.update = function update(slow, fast) {
  6542. var slow_ = this.slow_,
  6543. fast_ = this.fast_,
  6544. ttfb_ = this.ttfb_;
  6545. if (slow_.halfLife !== slow) {
  6546. this.slow_ = new EWMA(slow, slow_.getEstimate(), slow_.getTotalWeight());
  6547. }
  6548. if (fast_.halfLife !== fast) {
  6549. this.fast_ = new EWMA(fast, fast_.getEstimate(), fast_.getTotalWeight());
  6550. }
  6551. if (ttfb_.halfLife !== slow) {
  6552. this.ttfb_ = new EWMA(slow, ttfb_.getEstimate(), ttfb_.getTotalWeight());
  6553. }
  6554. };
  6555. _proto.sample = function sample(durationMs, numBytes) {
  6556. durationMs = Math.max(durationMs, this.minDelayMs_);
  6557. var numBits = 8 * numBytes;
  6558. // weight is duration in seconds
  6559. var durationS = durationMs / 1000;
  6560. // value is bandwidth in bits/s
  6561. var bandwidthInBps = numBits / durationS;
  6562. this.fast_.sample(durationS, bandwidthInBps);
  6563. this.slow_.sample(durationS, bandwidthInBps);
  6564. };
  6565. _proto.sampleTTFB = function sampleTTFB(ttfb) {
  6566. // weight is frequency curve applied to TTFB in seconds
  6567. // (longer times have less weight with expected input under 1 second)
  6568. var seconds = ttfb / 1000;
  6569. var weight = Math.sqrt(2) * Math.exp(-Math.pow(seconds, 2) / 2);
  6570. this.ttfb_.sample(weight, Math.max(ttfb, 5));
  6571. };
  6572. _proto.canEstimate = function canEstimate() {
  6573. return this.fast_.getTotalWeight() >= this.minWeight_;
  6574. };
  6575. _proto.getEstimate = function getEstimate() {
  6576. if (this.canEstimate()) {
  6577. // console.log('slow estimate:'+ Math.round(this.slow_.getEstimate()));
  6578. // console.log('fast estimate:'+ Math.round(this.fast_.getEstimate()));
  6579. // Take the minimum of these two estimates. This should have the effect of
  6580. // adapting down quickly, but up more slowly.
  6581. return Math.min(this.fast_.getEstimate(), this.slow_.getEstimate());
  6582. } else {
  6583. return this.defaultEstimate_;
  6584. }
  6585. };
  6586. _proto.getEstimateTTFB = function getEstimateTTFB() {
  6587. if (this.ttfb_.getTotalWeight() >= this.minWeight_) {
  6588. return this.ttfb_.getEstimate();
  6589. } else {
  6590. return this.defaultTTFB_;
  6591. }
  6592. };
  6593. _proto.destroy = function destroy() {};
  6594. return EwmaBandWidthEstimator;
  6595. }();
  6596. var SUPPORTED_INFO_DEFAULT = {
  6597. supported: true,
  6598. configurations: [],
  6599. decodingInfoResults: [{
  6600. supported: true,
  6601. powerEfficient: true,
  6602. smooth: true
  6603. }]
  6604. };
  6605. var SUPPORTED_INFO_CACHE = {};
  6606. function requiresMediaCapabilitiesDecodingInfo(level, audioTracksByGroup, currentVideoRange, currentFrameRate, currentBw, audioPreference) {
  6607. // Only test support when configuration is exceeds minimum options
  6608. var audioGroups = level.audioCodec ? level.audioGroups : null;
  6609. var audioCodecPreference = audioPreference == null ? void 0 : audioPreference.audioCodec;
  6610. var channelsPreference = audioPreference == null ? void 0 : audioPreference.channels;
  6611. var maxChannels = channelsPreference ? parseInt(channelsPreference) : audioCodecPreference ? Infinity : 2;
  6612. var audioChannels = null;
  6613. if (audioGroups != null && audioGroups.length) {
  6614. try {
  6615. if (audioGroups.length === 1 && audioGroups[0]) {
  6616. audioChannels = audioTracksByGroup.groups[audioGroups[0]].channels;
  6617. } else {
  6618. audioChannels = audioGroups.reduce(function (acc, groupId) {
  6619. if (groupId) {
  6620. var audioTrackGroup = audioTracksByGroup.groups[groupId];
  6621. if (!audioTrackGroup) {
  6622. throw new Error("Audio track group " + groupId + " not found");
  6623. }
  6624. // Sum all channel key values
  6625. Object.keys(audioTrackGroup.channels).forEach(function (key) {
  6626. acc[key] = (acc[key] || 0) + audioTrackGroup.channels[key];
  6627. });
  6628. }
  6629. return acc;
  6630. }, {
  6631. 2: 0
  6632. });
  6633. }
  6634. } catch (error) {
  6635. return true;
  6636. }
  6637. }
  6638. return level.videoCodec !== undefined && (level.width > 1920 && level.height > 1088 || level.height > 1920 && level.width > 1088 || level.frameRate > Math.max(currentFrameRate, 30) || level.videoRange !== 'SDR' && level.videoRange !== currentVideoRange || level.bitrate > Math.max(currentBw, 8e6)) || !!audioChannels && isFiniteNumber(maxChannels) && Object.keys(audioChannels).some(function (channels) {
  6639. return parseInt(channels) > maxChannels;
  6640. });
  6641. }
  6642. function getMediaDecodingInfoPromise(level, audioTracksByGroup, mediaCapabilities) {
  6643. var videoCodecs = level.videoCodec;
  6644. var audioCodecs = level.audioCodec;
  6645. if (!videoCodecs || !audioCodecs || !mediaCapabilities) {
  6646. return Promise.resolve(SUPPORTED_INFO_DEFAULT);
  6647. }
  6648. var baseVideoConfiguration = {
  6649. width: level.width,
  6650. height: level.height,
  6651. bitrate: Math.ceil(Math.max(level.bitrate * 0.9, level.averageBitrate)),
  6652. // Assume a framerate of 30fps since MediaCapabilities will not accept Level default of 0.
  6653. framerate: level.frameRate || 30
  6654. };
  6655. var videoRange = level.videoRange;
  6656. if (videoRange !== 'SDR') {
  6657. baseVideoConfiguration.transferFunction = videoRange.toLowerCase();
  6658. }
  6659. var configurations = videoCodecs.split(',').map(function (videoCodec) {
  6660. return {
  6661. type: 'media-source',
  6662. video: _objectSpread2(_objectSpread2({}, baseVideoConfiguration), {}, {
  6663. contentType: mimeTypeForCodec(videoCodec, 'video')
  6664. })
  6665. };
  6666. });
  6667. if (audioCodecs && level.audioGroups) {
  6668. level.audioGroups.forEach(function (audioGroupId) {
  6669. var _audioTracksByGroup$g;
  6670. if (!audioGroupId) {
  6671. return;
  6672. }
  6673. (_audioTracksByGroup$g = audioTracksByGroup.groups[audioGroupId]) == null ? void 0 : _audioTracksByGroup$g.tracks.forEach(function (audioTrack) {
  6674. if (audioTrack.groupId === audioGroupId) {
  6675. var channels = audioTrack.channels || '';
  6676. var channelsNumber = parseFloat(channels);
  6677. if (isFiniteNumber(channelsNumber) && channelsNumber > 2) {
  6678. configurations.push.apply(configurations, audioCodecs.split(',').map(function (audioCodec) {
  6679. return {
  6680. type: 'media-source',
  6681. audio: {
  6682. contentType: mimeTypeForCodec(audioCodec, 'audio'),
  6683. channels: '' + channelsNumber
  6684. // spatialRendering:
  6685. // audioCodec === 'ec-3' && channels.indexOf('JOC'),
  6686. }
  6687. };
  6688. }));
  6689. }
  6690. }
  6691. });
  6692. });
  6693. }
  6694. return Promise.all(configurations.map(function (configuration) {
  6695. // Cache MediaCapabilities promises
  6696. var decodingInfoKey = getMediaDecodingInfoKey(configuration);
  6697. return SUPPORTED_INFO_CACHE[decodingInfoKey] || (SUPPORTED_INFO_CACHE[decodingInfoKey] = mediaCapabilities.decodingInfo(configuration));
  6698. })).then(function (decodingInfoResults) {
  6699. return {
  6700. supported: !decodingInfoResults.some(function (info) {
  6701. return !info.supported;
  6702. }),
  6703. configurations: configurations,
  6704. decodingInfoResults: decodingInfoResults
  6705. };
  6706. }).catch(function (error) {
  6707. return {
  6708. supported: false,
  6709. configurations: configurations,
  6710. decodingInfoResults: [],
  6711. error: error
  6712. };
  6713. });
  6714. }
  6715. function getMediaDecodingInfoKey(config) {
  6716. var audio = config.audio,
  6717. video = config.video;
  6718. var mediaConfig = video || audio;
  6719. if (mediaConfig) {
  6720. var codec = mediaConfig.contentType.split('"')[1];
  6721. if (video) {
  6722. return "r" + video.height + "x" + video.width + "f" + Math.ceil(video.framerate) + (video.transferFunction || 'sd') + "_" + codec + "_" + Math.ceil(video.bitrate / 1e5);
  6723. }
  6724. if (audio) {
  6725. return "c" + audio.channels + (audio.spatialRendering ? 's' : 'n') + "_" + codec;
  6726. }
  6727. }
  6728. return '';
  6729. }
  6730. /**
  6731. * @returns Whether we can detect and validate HDR capability within the window context
  6732. */
  6733. function isHdrSupported() {
  6734. if (typeof matchMedia === 'function') {
  6735. var mediaQueryList = matchMedia('(dynamic-range: high)');
  6736. var badQuery = matchMedia('bad query');
  6737. if (mediaQueryList.media !== badQuery.media) {
  6738. return mediaQueryList.matches === true;
  6739. }
  6740. }
  6741. return false;
  6742. }
  6743. /**
  6744. * Sanitizes inputs to return the active video selection options for HDR/SDR.
  6745. * When both inputs are null:
  6746. *
  6747. * `{ preferHDR: false, allowedVideoRanges: [] }`
  6748. *
  6749. * When `currentVideoRange` non-null, maintain the active range:
  6750. *
  6751. * `{ preferHDR: currentVideoRange !== 'SDR', allowedVideoRanges: [currentVideoRange] }`
  6752. *
  6753. * When VideoSelectionOption non-null:
  6754. *
  6755. * - Allow all video ranges if `allowedVideoRanges` unspecified.
  6756. * - If `preferHDR` is non-null use the value to filter `allowedVideoRanges`.
  6757. * - Else check window for HDR support and set `preferHDR` to the result.
  6758. *
  6759. * @param currentVideoRange
  6760. * @param videoPreference
  6761. */
  6762. function getVideoSelectionOptions(currentVideoRange, videoPreference) {
  6763. var preferHDR = false;
  6764. var allowedVideoRanges = [];
  6765. if (currentVideoRange) {
  6766. preferHDR = currentVideoRange !== 'SDR';
  6767. allowedVideoRanges = [currentVideoRange];
  6768. }
  6769. if (videoPreference) {
  6770. allowedVideoRanges = videoPreference.allowedVideoRanges || VideoRangeValues.slice(0);
  6771. preferHDR = videoPreference.preferHDR !== undefined ? videoPreference.preferHDR : isHdrSupported();
  6772. if (preferHDR) {
  6773. allowedVideoRanges = allowedVideoRanges.filter(function (range) {
  6774. return range !== 'SDR';
  6775. });
  6776. } else {
  6777. allowedVideoRanges = ['SDR'];
  6778. }
  6779. }
  6780. return {
  6781. preferHDR: preferHDR,
  6782. allowedVideoRanges: allowedVideoRanges
  6783. };
  6784. }
  6785. function getStartCodecTier(codecTiers, currentVideoRange, currentBw, audioPreference, videoPreference) {
  6786. var codecSets = Object.keys(codecTiers);
  6787. var channelsPreference = audioPreference == null ? void 0 : audioPreference.channels;
  6788. var audioCodecPreference = audioPreference == null ? void 0 : audioPreference.audioCodec;
  6789. var preferStereo = channelsPreference && parseInt(channelsPreference) === 2;
  6790. // Use first level set to determine stereo, and minimum resolution and framerate
  6791. var hasStereo = true;
  6792. var hasCurrentVideoRange = false;
  6793. var minHeight = Infinity;
  6794. var minFramerate = Infinity;
  6795. var minBitrate = Infinity;
  6796. var selectedScore = 0;
  6797. var videoRanges = [];
  6798. var _getVideoSelectionOpt = getVideoSelectionOptions(currentVideoRange, videoPreference),
  6799. preferHDR = _getVideoSelectionOpt.preferHDR,
  6800. allowedVideoRanges = _getVideoSelectionOpt.allowedVideoRanges;
  6801. var _loop = function _loop() {
  6802. var tier = codecTiers[codecSets[i]];
  6803. hasStereo = tier.channels[2] > 0;
  6804. minHeight = Math.min(minHeight, tier.minHeight);
  6805. minFramerate = Math.min(minFramerate, tier.minFramerate);
  6806. minBitrate = Math.min(minBitrate, tier.minBitrate);
  6807. var matchingVideoRanges = allowedVideoRanges.filter(function (range) {
  6808. return tier.videoRanges[range] > 0;
  6809. });
  6810. if (matchingVideoRanges.length > 0) {
  6811. hasCurrentVideoRange = true;
  6812. videoRanges = matchingVideoRanges;
  6813. }
  6814. };
  6815. for (var i = codecSets.length; i--;) {
  6816. _loop();
  6817. }
  6818. minHeight = isFiniteNumber(minHeight) ? minHeight : 0;
  6819. minFramerate = isFiniteNumber(minFramerate) ? minFramerate : 0;
  6820. var maxHeight = Math.max(1080, minHeight);
  6821. var maxFramerate = Math.max(30, minFramerate);
  6822. minBitrate = isFiniteNumber(minBitrate) ? minBitrate : currentBw;
  6823. currentBw = Math.max(minBitrate, currentBw);
  6824. // If there are no variants with matching preference, set currentVideoRange to undefined
  6825. if (!hasCurrentVideoRange) {
  6826. currentVideoRange = undefined;
  6827. videoRanges = [];
  6828. }
  6829. var codecSet = codecSets.reduce(function (selected, candidate) {
  6830. // Remove candiates which do not meet bitrate, default audio, stereo or channels preference, 1080p or lower, 30fps or lower, or SDR/HDR selection if present
  6831. var candidateTier = codecTiers[candidate];
  6832. if (candidate === selected) {
  6833. return selected;
  6834. }
  6835. if (candidateTier.minBitrate > currentBw) {
  6836. logStartCodecCandidateIgnored(candidate, "min bitrate of " + candidateTier.minBitrate + " > current estimate of " + currentBw);
  6837. return selected;
  6838. }
  6839. if (!candidateTier.hasDefaultAudio) {
  6840. logStartCodecCandidateIgnored(candidate, "no renditions with default or auto-select sound found");
  6841. return selected;
  6842. }
  6843. if (audioCodecPreference && candidate.indexOf(audioCodecPreference.substring(0, 4)) % 5 !== 0) {
  6844. logStartCodecCandidateIgnored(candidate, "audio codec preference \"" + audioCodecPreference + "\" not found");
  6845. return selected;
  6846. }
  6847. if (channelsPreference && !preferStereo) {
  6848. if (!candidateTier.channels[channelsPreference]) {
  6849. logStartCodecCandidateIgnored(candidate, "no renditions with " + channelsPreference + " channel sound found (channels options: " + Object.keys(candidateTier.channels) + ")");
  6850. return selected;
  6851. }
  6852. } else if ((!audioCodecPreference || preferStereo) && hasStereo && candidateTier.channels['2'] === 0) {
  6853. logStartCodecCandidateIgnored(candidate, "no renditions with stereo sound found");
  6854. return selected;
  6855. }
  6856. if (candidateTier.minHeight > maxHeight) {
  6857. logStartCodecCandidateIgnored(candidate, "min resolution of " + candidateTier.minHeight + " > maximum of " + maxHeight);
  6858. return selected;
  6859. }
  6860. if (candidateTier.minFramerate > maxFramerate) {
  6861. logStartCodecCandidateIgnored(candidate, "min framerate of " + candidateTier.minFramerate + " > maximum of " + maxFramerate);
  6862. return selected;
  6863. }
  6864. if (!videoRanges.some(function (range) {
  6865. return candidateTier.videoRanges[range] > 0;
  6866. })) {
  6867. logStartCodecCandidateIgnored(candidate, "no variants with VIDEO-RANGE of " + JSON.stringify(videoRanges) + " found");
  6868. return selected;
  6869. }
  6870. if (candidateTier.maxScore < selectedScore) {
  6871. logStartCodecCandidateIgnored(candidate, "max score of " + candidateTier.maxScore + " < selected max of " + selectedScore);
  6872. return selected;
  6873. }
  6874. // Remove candiates with less preferred codecs or more errors
  6875. if (selected && (codecsSetSelectionPreferenceValue(candidate) >= codecsSetSelectionPreferenceValue(selected) || candidateTier.fragmentError > codecTiers[selected].fragmentError)) {
  6876. return selected;
  6877. }
  6878. selectedScore = candidateTier.maxScore;
  6879. return candidate;
  6880. }, undefined);
  6881. return {
  6882. codecSet: codecSet,
  6883. videoRanges: videoRanges,
  6884. preferHDR: preferHDR,
  6885. minFramerate: minFramerate,
  6886. minBitrate: minBitrate
  6887. };
  6888. }
  6889. function logStartCodecCandidateIgnored(codeSet, reason) {
  6890. logger.log("[abr] start candidates with \"" + codeSet + "\" ignored because " + reason);
  6891. }
  6892. function getAudioTracksByGroup(allAudioTracks) {
  6893. return allAudioTracks.reduce(function (audioTracksByGroup, track) {
  6894. var trackGroup = audioTracksByGroup.groups[track.groupId];
  6895. if (!trackGroup) {
  6896. trackGroup = audioTracksByGroup.groups[track.groupId] = {
  6897. tracks: [],
  6898. channels: {
  6899. 2: 0
  6900. },
  6901. hasDefault: false,
  6902. hasAutoSelect: false
  6903. };
  6904. }
  6905. trackGroup.tracks.push(track);
  6906. var channelsKey = track.channels || '2';
  6907. trackGroup.channels[channelsKey] = (trackGroup.channels[channelsKey] || 0) + 1;
  6908. trackGroup.hasDefault = trackGroup.hasDefault || track.default;
  6909. trackGroup.hasAutoSelect = trackGroup.hasAutoSelect || track.autoselect;
  6910. if (trackGroup.hasDefault) {
  6911. audioTracksByGroup.hasDefaultAudio = true;
  6912. }
  6913. if (trackGroup.hasAutoSelect) {
  6914. audioTracksByGroup.hasAutoSelectAudio = true;
  6915. }
  6916. return audioTracksByGroup;
  6917. }, {
  6918. hasDefaultAudio: false,
  6919. hasAutoSelectAudio: false,
  6920. groups: {}
  6921. });
  6922. }
  6923. function getCodecTiers(levels, audioTracksByGroup, minAutoLevel, maxAutoLevel) {
  6924. return levels.slice(minAutoLevel, maxAutoLevel + 1).reduce(function (tiers, level) {
  6925. if (!level.codecSet) {
  6926. return tiers;
  6927. }
  6928. var audioGroups = level.audioGroups;
  6929. var tier = tiers[level.codecSet];
  6930. if (!tier) {
  6931. tiers[level.codecSet] = tier = {
  6932. minBitrate: Infinity,
  6933. minHeight: Infinity,
  6934. minFramerate: Infinity,
  6935. maxScore: 0,
  6936. videoRanges: {
  6937. SDR: 0
  6938. },
  6939. channels: {
  6940. '2': 0
  6941. },
  6942. hasDefaultAudio: !audioGroups,
  6943. fragmentError: 0
  6944. };
  6945. }
  6946. tier.minBitrate = Math.min(tier.minBitrate, level.bitrate);
  6947. var lesserWidthOrHeight = Math.min(level.height, level.width);
  6948. tier.minHeight = Math.min(tier.minHeight, lesserWidthOrHeight);
  6949. tier.minFramerate = Math.min(tier.minFramerate, level.frameRate);
  6950. tier.maxScore = Math.max(tier.maxScore, level.score);
  6951. tier.fragmentError += level.fragmentError;
  6952. tier.videoRanges[level.videoRange] = (tier.videoRanges[level.videoRange] || 0) + 1;
  6953. if (audioGroups) {
  6954. audioGroups.forEach(function (audioGroupId) {
  6955. if (!audioGroupId) {
  6956. return;
  6957. }
  6958. var audioGroup = audioTracksByGroup.groups[audioGroupId];
  6959. if (!audioGroup) {
  6960. return;
  6961. }
  6962. // Default audio is any group with DEFAULT=YES, or if missing then any group with AUTOSELECT=YES, or all variants
  6963. tier.hasDefaultAudio = tier.hasDefaultAudio || audioTracksByGroup.hasDefaultAudio ? audioGroup.hasDefault : audioGroup.hasAutoSelect || !audioTracksByGroup.hasDefaultAudio && !audioTracksByGroup.hasAutoSelectAudio;
  6964. Object.keys(audioGroup.channels).forEach(function (channels) {
  6965. tier.channels[channels] = (tier.channels[channels] || 0) + audioGroup.channels[channels];
  6966. });
  6967. });
  6968. }
  6969. return tiers;
  6970. }, {});
  6971. }
  6972. function findMatchingOption(option, tracks, matchPredicate) {
  6973. if ('attrs' in option) {
  6974. var index = tracks.indexOf(option);
  6975. if (index !== -1) {
  6976. return index;
  6977. }
  6978. }
  6979. for (var i = 0; i < tracks.length; i++) {
  6980. var _track = tracks[i];
  6981. if (matchesOption(option, _track, matchPredicate)) {
  6982. return i;
  6983. }
  6984. }
  6985. return -1;
  6986. }
  6987. function matchesOption(option, track, matchPredicate) {
  6988. var groupId = option.groupId,
  6989. name = option.name,
  6990. lang = option.lang,
  6991. assocLang = option.assocLang,
  6992. characteristics = option.characteristics,
  6993. isDefault = option.default;
  6994. var forced = option.forced;
  6995. return (groupId === undefined || track.groupId === groupId) && (name === undefined || track.name === name) && (lang === undefined || track.lang === lang) && (lang === undefined || track.assocLang === assocLang) && (isDefault === undefined || track.default === isDefault) && (forced === undefined || track.forced === forced) && (characteristics === undefined || characteristicsMatch(characteristics, track.characteristics)) && (matchPredicate === undefined || matchPredicate(option, track));
  6996. }
  6997. function characteristicsMatch(characteristicsA, characteristicsB) {
  6998. if (characteristicsB === void 0) {
  6999. characteristicsB = '';
  7000. }
  7001. var arrA = characteristicsA.split(',');
  7002. var arrB = characteristicsB.split(',');
  7003. // Expects each item to be unique:
  7004. return arrA.length === arrB.length && !arrA.some(function (el) {
  7005. return arrB.indexOf(el) === -1;
  7006. });
  7007. }
  7008. function audioMatchPredicate(option, track) {
  7009. var audioCodec = option.audioCodec,
  7010. channels = option.channels;
  7011. return (audioCodec === undefined || (track.audioCodec || '').substring(0, 4) === audioCodec.substring(0, 4)) && (channels === undefined || channels === (track.channels || '2'));
  7012. }
  7013. function findClosestLevelWithAudioGroup(option, levels, allAudioTracks, searchIndex, matchPredicate) {
  7014. var currentLevel = levels[searchIndex];
  7015. // Are there variants with same URI as current level?
  7016. // If so, find a match that does not require any level URI change
  7017. var variants = levels.reduce(function (variantMap, level, index) {
  7018. var uri = level.uri;
  7019. var renditions = variantMap[uri] || (variantMap[uri] = []);
  7020. renditions.push(index);
  7021. return variantMap;
  7022. }, {});
  7023. var renditions = variants[currentLevel.uri];
  7024. if (renditions.length > 1) {
  7025. searchIndex = Math.max.apply(Math, renditions);
  7026. }
  7027. // Find best match
  7028. var currentVideoRange = currentLevel.videoRange;
  7029. var currentFrameRate = currentLevel.frameRate;
  7030. var currentVideoCodec = currentLevel.codecSet.substring(0, 4);
  7031. var matchingVideo = searchDownAndUpList(levels, searchIndex, function (level) {
  7032. if (level.videoRange !== currentVideoRange || level.frameRate !== currentFrameRate || level.codecSet.substring(0, 4) !== currentVideoCodec) {
  7033. return false;
  7034. }
  7035. var audioGroups = level.audioGroups;
  7036. var tracks = allAudioTracks.filter(function (track) {
  7037. return !audioGroups || audioGroups.indexOf(track.groupId) !== -1;
  7038. });
  7039. return findMatchingOption(option, tracks, matchPredicate) > -1;
  7040. });
  7041. if (matchingVideo > -1) {
  7042. return matchingVideo;
  7043. }
  7044. return searchDownAndUpList(levels, searchIndex, function (level) {
  7045. var audioGroups = level.audioGroups;
  7046. var tracks = allAudioTracks.filter(function (track) {
  7047. return !audioGroups || audioGroups.indexOf(track.groupId) !== -1;
  7048. });
  7049. return findMatchingOption(option, tracks, matchPredicate) > -1;
  7050. });
  7051. }
  7052. function searchDownAndUpList(arr, searchIndex, predicate) {
  7053. for (var i = searchIndex; i; i--) {
  7054. if (predicate(arr[i])) {
  7055. return i;
  7056. }
  7057. }
  7058. for (var _i = searchIndex + 1; _i < arr.length; _i++) {
  7059. if (predicate(arr[_i])) {
  7060. return _i;
  7061. }
  7062. }
  7063. return -1;
  7064. }
  7065. var AbrController = /*#__PURE__*/function () {
  7066. function AbrController(_hls) {
  7067. var _this = this;
  7068. this.hls = void 0;
  7069. this.lastLevelLoadSec = 0;
  7070. this.lastLoadedFragLevel = -1;
  7071. this.firstSelection = -1;
  7072. this._nextAutoLevel = -1;
  7073. this.nextAutoLevelKey = '';
  7074. this.audioTracksByGroup = null;
  7075. this.codecTiers = null;
  7076. this.timer = -1;
  7077. this.fragCurrent = null;
  7078. this.partCurrent = null;
  7079. this.bitrateTestDelay = 0;
  7080. this.bwEstimator = void 0;
  7081. /*
  7082. This method monitors the download rate of the current fragment, and will downswitch if that fragment will not load
  7083. quickly enough to prevent underbuffering
  7084. */
  7085. this._abandonRulesCheck = function () {
  7086. var frag = _this.fragCurrent,
  7087. part = _this.partCurrent,
  7088. hls = _this.hls;
  7089. var autoLevelEnabled = hls.autoLevelEnabled,
  7090. media = hls.media;
  7091. if (!frag || !media) {
  7092. return;
  7093. }
  7094. var now = performance.now();
  7095. var stats = part ? part.stats : frag.stats;
  7096. var duration = part ? part.duration : frag.duration;
  7097. var timeLoading = now - stats.loading.start;
  7098. var minAutoLevel = hls.minAutoLevel;
  7099. // If frag loading is aborted, complete, or from lowest level, stop timer and return
  7100. if (stats.aborted || stats.loaded && stats.loaded === stats.total || frag.level <= minAutoLevel) {
  7101. _this.clearTimer();
  7102. // reset forced auto level value so that next level will be selected
  7103. _this._nextAutoLevel = -1;
  7104. return;
  7105. }
  7106. // This check only runs if we're in ABR mode and actually playing
  7107. if (!autoLevelEnabled || media.paused || !media.playbackRate || !media.readyState) {
  7108. return;
  7109. }
  7110. var bufferInfo = hls.mainForwardBufferInfo;
  7111. if (bufferInfo === null) {
  7112. return;
  7113. }
  7114. var ttfbEstimate = _this.bwEstimator.getEstimateTTFB();
  7115. var playbackRate = Math.abs(media.playbackRate);
  7116. // To maintain stable adaptive playback, only begin monitoring frag loading after half or more of its playback duration has passed
  7117. if (timeLoading <= Math.max(ttfbEstimate, 1000 * (duration / (playbackRate * 2)))) {
  7118. return;
  7119. }
  7120. // bufferStarvationDelay is an estimate of the amount time (in seconds) it will take to exhaust the buffer
  7121. var bufferStarvationDelay = bufferInfo.len / playbackRate;
  7122. var ttfb = stats.loading.first ? stats.loading.first - stats.loading.start : -1;
  7123. var loadedFirstByte = stats.loaded && ttfb > -1;
  7124. var bwEstimate = _this.getBwEstimate();
  7125. var levels = hls.levels;
  7126. var level = levels[frag.level];
  7127. var expectedLen = stats.total || Math.max(stats.loaded, Math.round(duration * level.averageBitrate / 8));
  7128. var timeStreaming = loadedFirstByte ? timeLoading - ttfb : timeLoading;
  7129. if (timeStreaming < 1 && loadedFirstByte) {
  7130. timeStreaming = Math.min(timeLoading, stats.loaded * 8 / bwEstimate);
  7131. }
  7132. var loadRate = loadedFirstByte ? stats.loaded * 1000 / timeStreaming : 0;
  7133. // fragLoadDelay is an estimate of the time (in seconds) it will take to buffer the remainder of the fragment
  7134. var fragLoadedDelay = loadRate ? (expectedLen - stats.loaded) / loadRate : expectedLen * 8 / bwEstimate + ttfbEstimate / 1000;
  7135. // Only downswitch if the time to finish loading the current fragment is greater than the amount of buffer left
  7136. if (fragLoadedDelay <= bufferStarvationDelay) {
  7137. return;
  7138. }
  7139. var bwe = loadRate ? loadRate * 8 : bwEstimate;
  7140. var fragLevelNextLoadedDelay = Number.POSITIVE_INFINITY;
  7141. var nextLoadLevel;
  7142. // Iterate through lower level and try to find the largest one that avoids rebuffering
  7143. for (nextLoadLevel = frag.level - 1; nextLoadLevel > minAutoLevel; nextLoadLevel--) {
  7144. // compute time to load next fragment at lower level
  7145. // 8 = bits per byte (bps/Bps)
  7146. var levelNextBitrate = levels[nextLoadLevel].maxBitrate;
  7147. fragLevelNextLoadedDelay = _this.getTimeToLoadFrag(ttfbEstimate / 1000, bwe, duration * levelNextBitrate, !levels[nextLoadLevel].details);
  7148. if (fragLevelNextLoadedDelay < bufferStarvationDelay) {
  7149. break;
  7150. }
  7151. }
  7152. // Only emergency switch down if it takes less time to load a new fragment at lowest level instead of continuing
  7153. // to load the current one
  7154. if (fragLevelNextLoadedDelay >= fragLoadedDelay) {
  7155. return;
  7156. }
  7157. // if estimated load time of new segment is completely unreasonable, ignore and do not emergency switch down
  7158. if (fragLevelNextLoadedDelay > duration * 10) {
  7159. return;
  7160. }
  7161. hls.nextLoadLevel = hls.nextAutoLevel = nextLoadLevel;
  7162. if (loadedFirstByte) {
  7163. // If there has been loading progress, sample bandwidth using loading time offset by minimum TTFB time
  7164. _this.bwEstimator.sample(timeLoading - Math.min(ttfbEstimate, ttfb), stats.loaded);
  7165. } else {
  7166. // If there has been no loading progress, sample TTFB
  7167. _this.bwEstimator.sampleTTFB(timeLoading);
  7168. }
  7169. var nextLoadLevelBitrate = levels[nextLoadLevel].maxBitrate;
  7170. if (_this.getBwEstimate() * _this.hls.config.abrBandWidthUpFactor > nextLoadLevelBitrate) {
  7171. _this.resetEstimator(nextLoadLevelBitrate);
  7172. }
  7173. _this.clearTimer();
  7174. logger.warn("[abr] Fragment " + frag.sn + (part ? ' part ' + part.index : '') + " of level " + frag.level + " is loading too slowly;\n Time to underbuffer: " + bufferStarvationDelay.toFixed(3) + " s\n Estimated load time for current fragment: " + fragLoadedDelay.toFixed(3) + " s\n Estimated load time for down switch fragment: " + fragLevelNextLoadedDelay.toFixed(3) + " s\n TTFB estimate: " + (ttfb | 0) + " ms\n Current BW estimate: " + (isFiniteNumber(bwEstimate) ? bwEstimate | 0 : 'Unknown') + " bps\n New BW estimate: " + (_this.getBwEstimate() | 0) + " bps\n Switching to level " + nextLoadLevel + " @ " + (nextLoadLevelBitrate | 0) + " bps");
  7175. hls.trigger(Events.FRAG_LOAD_EMERGENCY_ABORTED, {
  7176. frag: frag,
  7177. part: part,
  7178. stats: stats
  7179. });
  7180. };
  7181. this.hls = _hls;
  7182. this.bwEstimator = this.initEstimator();
  7183. this.registerListeners();
  7184. }
  7185. var _proto = AbrController.prototype;
  7186. _proto.resetEstimator = function resetEstimator(abrEwmaDefaultEstimate) {
  7187. if (abrEwmaDefaultEstimate) {
  7188. logger.log("setting initial bwe to " + abrEwmaDefaultEstimate);
  7189. this.hls.config.abrEwmaDefaultEstimate = abrEwmaDefaultEstimate;
  7190. }
  7191. this.firstSelection = -1;
  7192. this.bwEstimator = this.initEstimator();
  7193. };
  7194. _proto.initEstimator = function initEstimator() {
  7195. var config = this.hls.config;
  7196. return new EwmaBandWidthEstimator(config.abrEwmaSlowVoD, config.abrEwmaFastVoD, config.abrEwmaDefaultEstimate);
  7197. };
  7198. _proto.registerListeners = function registerListeners() {
  7199. var hls = this.hls;
  7200. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  7201. hls.on(Events.FRAG_LOADING, this.onFragLoading, this);
  7202. hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
  7203. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  7204. hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  7205. hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  7206. hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  7207. hls.on(Events.MAX_AUTO_LEVEL_UPDATED, this.onMaxAutoLevelUpdated, this);
  7208. hls.on(Events.ERROR, this.onError, this);
  7209. };
  7210. _proto.unregisterListeners = function unregisterListeners() {
  7211. var hls = this.hls;
  7212. if (!hls) {
  7213. return;
  7214. }
  7215. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  7216. hls.off(Events.FRAG_LOADING, this.onFragLoading, this);
  7217. hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
  7218. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  7219. hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  7220. hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  7221. hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  7222. hls.off(Events.MAX_AUTO_LEVEL_UPDATED, this.onMaxAutoLevelUpdated, this);
  7223. hls.off(Events.ERROR, this.onError, this);
  7224. };
  7225. _proto.destroy = function destroy() {
  7226. this.unregisterListeners();
  7227. this.clearTimer();
  7228. // @ts-ignore
  7229. this.hls = this._abandonRulesCheck = null;
  7230. this.fragCurrent = this.partCurrent = null;
  7231. };
  7232. _proto.onManifestLoading = function onManifestLoading(event, data) {
  7233. this.lastLoadedFragLevel = -1;
  7234. this.firstSelection = -1;
  7235. this.lastLevelLoadSec = 0;
  7236. this.fragCurrent = this.partCurrent = null;
  7237. this.onLevelsUpdated();
  7238. this.clearTimer();
  7239. };
  7240. _proto.onLevelsUpdated = function onLevelsUpdated() {
  7241. if (this.lastLoadedFragLevel > -1 && this.fragCurrent) {
  7242. this.lastLoadedFragLevel = this.fragCurrent.level;
  7243. }
  7244. this._nextAutoLevel = -1;
  7245. this.onMaxAutoLevelUpdated();
  7246. this.codecTiers = null;
  7247. this.audioTracksByGroup = null;
  7248. };
  7249. _proto.onMaxAutoLevelUpdated = function onMaxAutoLevelUpdated() {
  7250. this.firstSelection = -1;
  7251. this.nextAutoLevelKey = '';
  7252. };
  7253. _proto.onFragLoading = function onFragLoading(event, data) {
  7254. var frag = data.frag;
  7255. if (this.ignoreFragment(frag)) {
  7256. return;
  7257. }
  7258. if (!frag.bitrateTest) {
  7259. var _data$part;
  7260. this.fragCurrent = frag;
  7261. this.partCurrent = (_data$part = data.part) != null ? _data$part : null;
  7262. }
  7263. this.clearTimer();
  7264. this.timer = self.setInterval(this._abandonRulesCheck, 100);
  7265. };
  7266. _proto.onLevelSwitching = function onLevelSwitching(event, data) {
  7267. this.clearTimer();
  7268. };
  7269. _proto.onError = function onError(event, data) {
  7270. if (data.fatal) {
  7271. return;
  7272. }
  7273. switch (data.details) {
  7274. case ErrorDetails.BUFFER_ADD_CODEC_ERROR:
  7275. case ErrorDetails.BUFFER_APPEND_ERROR:
  7276. // Reset last loaded level so that a new selection can be made after calling recoverMediaError
  7277. this.lastLoadedFragLevel = -1;
  7278. this.firstSelection = -1;
  7279. break;
  7280. case ErrorDetails.FRAG_LOAD_TIMEOUT:
  7281. {
  7282. var frag = data.frag;
  7283. var fragCurrent = this.fragCurrent,
  7284. part = this.partCurrent;
  7285. if (frag && fragCurrent && frag.sn === fragCurrent.sn && frag.level === fragCurrent.level) {
  7286. var now = performance.now();
  7287. var stats = part ? part.stats : frag.stats;
  7288. var timeLoading = now - stats.loading.start;
  7289. var ttfb = stats.loading.first ? stats.loading.first - stats.loading.start : -1;
  7290. var loadedFirstByte = stats.loaded && ttfb > -1;
  7291. if (loadedFirstByte) {
  7292. var ttfbEstimate = this.bwEstimator.getEstimateTTFB();
  7293. this.bwEstimator.sample(timeLoading - Math.min(ttfbEstimate, ttfb), stats.loaded);
  7294. } else {
  7295. this.bwEstimator.sampleTTFB(timeLoading);
  7296. }
  7297. }
  7298. break;
  7299. }
  7300. }
  7301. };
  7302. _proto.getTimeToLoadFrag = function getTimeToLoadFrag(timeToFirstByteSec, bandwidth, fragSizeBits, isSwitch) {
  7303. var fragLoadSec = timeToFirstByteSec + fragSizeBits / bandwidth;
  7304. var playlistLoadSec = isSwitch ? this.lastLevelLoadSec : 0;
  7305. return fragLoadSec + playlistLoadSec;
  7306. };
  7307. _proto.onLevelLoaded = function onLevelLoaded(event, data) {
  7308. var config = this.hls.config;
  7309. var loading = data.stats.loading;
  7310. var timeLoadingMs = loading.end - loading.start;
  7311. if (isFiniteNumber(timeLoadingMs)) {
  7312. this.lastLevelLoadSec = timeLoadingMs / 1000;
  7313. }
  7314. if (data.details.live) {
  7315. this.bwEstimator.update(config.abrEwmaSlowLive, config.abrEwmaFastLive);
  7316. } else {
  7317. this.bwEstimator.update(config.abrEwmaSlowVoD, config.abrEwmaFastVoD);
  7318. }
  7319. };
  7320. _proto.onFragLoaded = function onFragLoaded(event, _ref) {
  7321. var frag = _ref.frag,
  7322. part = _ref.part;
  7323. var stats = part ? part.stats : frag.stats;
  7324. if (frag.type === PlaylistLevelType.MAIN) {
  7325. this.bwEstimator.sampleTTFB(stats.loading.first - stats.loading.start);
  7326. }
  7327. if (this.ignoreFragment(frag)) {
  7328. return;
  7329. }
  7330. // stop monitoring bw once frag loaded
  7331. this.clearTimer();
  7332. // reset forced auto level value so that next level will be selected
  7333. if (frag.level === this._nextAutoLevel) {
  7334. this._nextAutoLevel = -1;
  7335. }
  7336. this.firstSelection = -1;
  7337. // compute level average bitrate
  7338. if (this.hls.config.abrMaxWithRealBitrate) {
  7339. var duration = part ? part.duration : frag.duration;
  7340. var level = this.hls.levels[frag.level];
  7341. var loadedBytes = (level.loaded ? level.loaded.bytes : 0) + stats.loaded;
  7342. var loadedDuration = (level.loaded ? level.loaded.duration : 0) + duration;
  7343. level.loaded = {
  7344. bytes: loadedBytes,
  7345. duration: loadedDuration
  7346. };
  7347. level.realBitrate = Math.round(8 * loadedBytes / loadedDuration);
  7348. }
  7349. if (frag.bitrateTest) {
  7350. var fragBufferedData = {
  7351. stats: stats,
  7352. frag: frag,
  7353. part: part,
  7354. id: frag.type
  7355. };
  7356. this.onFragBuffered(Events.FRAG_BUFFERED, fragBufferedData);
  7357. frag.bitrateTest = false;
  7358. } else {
  7359. // store level id after successful fragment load for playback
  7360. this.lastLoadedFragLevel = frag.level;
  7361. }
  7362. };
  7363. _proto.onFragBuffered = function onFragBuffered(event, data) {
  7364. var frag = data.frag,
  7365. part = data.part;
  7366. var stats = part != null && part.stats.loaded ? part.stats : frag.stats;
  7367. if (stats.aborted) {
  7368. return;
  7369. }
  7370. if (this.ignoreFragment(frag)) {
  7371. return;
  7372. }
  7373. // Use the difference between parsing and request instead of buffering and request to compute fragLoadingProcessing;
  7374. // rationale is that buffer appending only happens once media is attached. This can happen when config.startFragPrefetch
  7375. // is used. If we used buffering in that case, our BW estimate sample will be very large.
  7376. var processingMs = stats.parsing.end - stats.loading.start - Math.min(stats.loading.first - stats.loading.start, this.bwEstimator.getEstimateTTFB());
  7377. this.bwEstimator.sample(processingMs, stats.loaded);
  7378. stats.bwEstimate = this.getBwEstimate();
  7379. if (frag.bitrateTest) {
  7380. this.bitrateTestDelay = processingMs / 1000;
  7381. } else {
  7382. this.bitrateTestDelay = 0;
  7383. }
  7384. };
  7385. _proto.ignoreFragment = function ignoreFragment(frag) {
  7386. // Only count non-alt-audio frags which were actually buffered in our BW calculations
  7387. return frag.type !== PlaylistLevelType.MAIN || frag.sn === 'initSegment';
  7388. };
  7389. _proto.clearTimer = function clearTimer() {
  7390. if (this.timer > -1) {
  7391. self.clearInterval(this.timer);
  7392. this.timer = -1;
  7393. }
  7394. };
  7395. _proto.getAutoLevelKey = function getAutoLevelKey() {
  7396. return this.getBwEstimate() + "_" + this.getStarvationDelay().toFixed(2);
  7397. };
  7398. _proto.getNextABRAutoLevel = function getNextABRAutoLevel() {
  7399. var fragCurrent = this.fragCurrent,
  7400. partCurrent = this.partCurrent,
  7401. hls = this.hls;
  7402. var maxAutoLevel = hls.maxAutoLevel,
  7403. config = hls.config,
  7404. minAutoLevel = hls.minAutoLevel;
  7405. var currentFragDuration = partCurrent ? partCurrent.duration : fragCurrent ? fragCurrent.duration : 0;
  7406. var avgbw = this.getBwEstimate();
  7407. // bufferStarvationDelay is the wall-clock time left until the playback buffer is exhausted.
  7408. var bufferStarvationDelay = this.getStarvationDelay();
  7409. var bwFactor = config.abrBandWidthFactor;
  7410. var bwUpFactor = config.abrBandWidthUpFactor;
  7411. // First, look to see if we can find a level matching with our avg bandwidth AND that could also guarantee no rebuffering at all
  7412. if (bufferStarvationDelay) {
  7413. var _bestLevel = this.findBestLevel(avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay, 0, bwFactor, bwUpFactor);
  7414. if (_bestLevel >= 0) {
  7415. return _bestLevel;
  7416. }
  7417. }
  7418. // not possible to get rid of rebuffering... try to find level that will guarantee less than maxStarvationDelay of rebuffering
  7419. var maxStarvationDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxStarvationDelay) : config.maxStarvationDelay;
  7420. if (!bufferStarvationDelay) {
  7421. // in case buffer is empty, let's check if previous fragment was loaded to perform a bitrate test
  7422. var bitrateTestDelay = this.bitrateTestDelay;
  7423. if (bitrateTestDelay) {
  7424. // if it is the case, then we need to adjust our max starvation delay using maxLoadingDelay config value
  7425. // max video loading delay used in automatic start level selection :
  7426. // in that mode ABR controller will ensure that video loading time (ie the time to fetch the first fragment at lowest quality level +
  7427. // the time to fetch the fragment at the appropriate quality level is less than ```maxLoadingDelay``` )
  7428. // cap maxLoadingDelay and ensure it is not bigger 'than bitrate test' frag duration
  7429. var maxLoadingDelay = currentFragDuration ? Math.min(currentFragDuration, config.maxLoadingDelay) : config.maxLoadingDelay;
  7430. maxStarvationDelay = maxLoadingDelay - bitrateTestDelay;
  7431. logger.info("[abr] bitrate test took " + Math.round(1000 * bitrateTestDelay) + "ms, set first fragment max fetchDuration to " + Math.round(1000 * maxStarvationDelay) + " ms");
  7432. // don't use conservative factor on bitrate test
  7433. bwFactor = bwUpFactor = 1;
  7434. }
  7435. }
  7436. var bestLevel = this.findBestLevel(avgbw, minAutoLevel, maxAutoLevel, bufferStarvationDelay, maxStarvationDelay, bwFactor, bwUpFactor);
  7437. logger.info("[abr] " + (bufferStarvationDelay ? 'rebuffering expected' : 'buffer is empty') + ", optimal quality level " + bestLevel);
  7438. if (bestLevel > -1) {
  7439. return bestLevel;
  7440. }
  7441. // If no matching level found, see if min auto level would be a better option
  7442. var minLevel = hls.levels[minAutoLevel];
  7443. var autoLevel = hls.levels[hls.loadLevel];
  7444. if ((minLevel == null ? void 0 : minLevel.bitrate) < (autoLevel == null ? void 0 : autoLevel.bitrate)) {
  7445. return minAutoLevel;
  7446. }
  7447. // or if bitrate is not lower, continue to use loadLevel
  7448. return hls.loadLevel;
  7449. };
  7450. _proto.getStarvationDelay = function getStarvationDelay() {
  7451. var hls = this.hls;
  7452. var media = hls.media;
  7453. if (!media) {
  7454. return Infinity;
  7455. }
  7456. // playbackRate is the absolute value of the playback rate; if media.playbackRate is 0, we use 1 to load as
  7457. // if we're playing back at the normal rate.
  7458. var playbackRate = media && media.playbackRate !== 0 ? Math.abs(media.playbackRate) : 1.0;
  7459. var bufferInfo = hls.mainForwardBufferInfo;
  7460. return (bufferInfo ? bufferInfo.len : 0) / playbackRate;
  7461. };
  7462. _proto.getBwEstimate = function getBwEstimate() {
  7463. return this.bwEstimator.canEstimate() ? this.bwEstimator.getEstimate() : this.hls.config.abrEwmaDefaultEstimate;
  7464. };
  7465. _proto.findBestLevel = function findBestLevel(currentBw, minAutoLevel, maxAutoLevel, bufferStarvationDelay, maxStarvationDelay, bwFactor, bwUpFactor) {
  7466. var _level$details,
  7467. _this2 = this;
  7468. var maxFetchDuration = bufferStarvationDelay + maxStarvationDelay;
  7469. var lastLoadedFragLevel = this.lastLoadedFragLevel;
  7470. var selectionBaseLevel = lastLoadedFragLevel === -1 ? this.hls.firstLevel : lastLoadedFragLevel;
  7471. var fragCurrent = this.fragCurrent,
  7472. partCurrent = this.partCurrent;
  7473. var _this$hls = this.hls,
  7474. levels = _this$hls.levels,
  7475. allAudioTracks = _this$hls.allAudioTracks,
  7476. loadLevel = _this$hls.loadLevel,
  7477. config = _this$hls.config;
  7478. if (levels.length === 1) {
  7479. return 0;
  7480. }
  7481. var level = levels[selectionBaseLevel];
  7482. var live = !!(level != null && (_level$details = level.details) != null && _level$details.live);
  7483. var firstSelection = loadLevel === -1 || lastLoadedFragLevel === -1;
  7484. var currentCodecSet;
  7485. var currentVideoRange = 'SDR';
  7486. var currentFrameRate = (level == null ? void 0 : level.frameRate) || 0;
  7487. var audioPreference = config.audioPreference,
  7488. videoPreference = config.videoPreference;
  7489. var audioTracksByGroup = this.audioTracksByGroup || (this.audioTracksByGroup = getAudioTracksByGroup(allAudioTracks));
  7490. if (firstSelection) {
  7491. if (this.firstSelection !== -1) {
  7492. return this.firstSelection;
  7493. }
  7494. var codecTiers = this.codecTiers || (this.codecTiers = getCodecTiers(levels, audioTracksByGroup, minAutoLevel, maxAutoLevel));
  7495. var startTier = getStartCodecTier(codecTiers, currentVideoRange, currentBw, audioPreference, videoPreference);
  7496. var codecSet = startTier.codecSet,
  7497. videoRanges = startTier.videoRanges,
  7498. minFramerate = startTier.minFramerate,
  7499. minBitrate = startTier.minBitrate,
  7500. preferHDR = startTier.preferHDR;
  7501. currentCodecSet = codecSet;
  7502. currentVideoRange = preferHDR ? videoRanges[videoRanges.length - 1] : videoRanges[0];
  7503. currentFrameRate = minFramerate;
  7504. currentBw = Math.max(currentBw, minBitrate);
  7505. logger.log("[abr] picked start tier " + JSON.stringify(startTier));
  7506. } else {
  7507. currentCodecSet = level == null ? void 0 : level.codecSet;
  7508. currentVideoRange = level == null ? void 0 : level.videoRange;
  7509. }
  7510. var currentFragDuration = partCurrent ? partCurrent.duration : fragCurrent ? fragCurrent.duration : 0;
  7511. var ttfbEstimateSec = this.bwEstimator.getEstimateTTFB() / 1000;
  7512. var levelsSkipped = [];
  7513. var _loop = function _loop() {
  7514. var _levelInfo$supportedR;
  7515. var levelInfo = levels[i];
  7516. var upSwitch = i > selectionBaseLevel;
  7517. if (!levelInfo) {
  7518. return 0; // continue
  7519. }
  7520. if (config.useMediaCapabilities && !levelInfo.supportedResult && !levelInfo.supportedPromise) {
  7521. var mediaCapabilities = navigator.mediaCapabilities;
  7522. if (typeof (mediaCapabilities == null ? void 0 : mediaCapabilities.decodingInfo) === 'function' && requiresMediaCapabilitiesDecodingInfo(levelInfo, audioTracksByGroup, currentVideoRange, currentFrameRate, currentBw, audioPreference)) {
  7523. levelInfo.supportedPromise = getMediaDecodingInfoPromise(levelInfo, audioTracksByGroup, mediaCapabilities);
  7524. levelInfo.supportedPromise.then(function (decodingInfo) {
  7525. if (!_this2.hls) {
  7526. return;
  7527. }
  7528. levelInfo.supportedResult = decodingInfo;
  7529. var levels = _this2.hls.levels;
  7530. var index = levels.indexOf(levelInfo);
  7531. if (decodingInfo.error) {
  7532. logger.warn("[abr] MediaCapabilities decodingInfo error: \"" + decodingInfo.error + "\" for level " + index + " " + JSON.stringify(decodingInfo));
  7533. } else if (!decodingInfo.supported) {
  7534. logger.warn("[abr] Unsupported MediaCapabilities decodingInfo result for level " + index + " " + JSON.stringify(decodingInfo));
  7535. if (index > -1 && levels.length > 1) {
  7536. logger.log("[abr] Removing unsupported level " + index);
  7537. _this2.hls.removeLevel(index);
  7538. }
  7539. }
  7540. });
  7541. } else {
  7542. levelInfo.supportedResult = SUPPORTED_INFO_DEFAULT;
  7543. }
  7544. }
  7545. // skip candidates which change codec-family or video-range,
  7546. // and which decrease or increase frame-rate for up and down-switch respectfully
  7547. if (currentCodecSet && levelInfo.codecSet !== currentCodecSet || currentVideoRange && levelInfo.videoRange !== currentVideoRange || upSwitch && currentFrameRate > levelInfo.frameRate || !upSwitch && currentFrameRate > 0 && currentFrameRate < levelInfo.frameRate || levelInfo.supportedResult && !((_levelInfo$supportedR = levelInfo.supportedResult.decodingInfoResults) != null && _levelInfo$supportedR[0].smooth)) {
  7548. levelsSkipped.push(i);
  7549. return 0; // continue
  7550. }
  7551. var levelDetails = levelInfo.details;
  7552. var avgDuration = (partCurrent ? levelDetails == null ? void 0 : levelDetails.partTarget : levelDetails == null ? void 0 : levelDetails.averagetargetduration) || currentFragDuration;
  7553. var adjustedbw;
  7554. // follow algorithm captured from stagefright :
  7555. // https://android.googlesource.com/platform/frameworks/av/+/master/media/libstagefright/httplive/LiveSession.cpp
  7556. // Pick the highest bandwidth stream below or equal to estimated bandwidth.
  7557. // consider only 80% of the available bandwidth, but if we are switching up,
  7558. // be even more conservative (70%) to avoid overestimating and immediately
  7559. // switching back.
  7560. if (!upSwitch) {
  7561. adjustedbw = bwFactor * currentBw;
  7562. } else {
  7563. adjustedbw = bwUpFactor * currentBw;
  7564. }
  7565. // Use average bitrate when starvation delay (buffer length) is gt or eq two segment durations and rebuffering is not expected (maxStarvationDelay > 0)
  7566. var bitrate = currentFragDuration && bufferStarvationDelay >= currentFragDuration * 2 && maxStarvationDelay === 0 ? levels[i].averageBitrate : levels[i].maxBitrate;
  7567. var fetchDuration = _this2.getTimeToLoadFrag(ttfbEstimateSec, adjustedbw, bitrate * avgDuration, levelDetails === undefined);
  7568. var canSwitchWithinTolerance =
  7569. // if adjusted bw is greater than level bitrate AND
  7570. adjustedbw >= bitrate && (
  7571. // no level change, or new level has no error history
  7572. i === lastLoadedFragLevel || levelInfo.loadError === 0 && levelInfo.fragmentError === 0) && (
  7573. // fragment fetchDuration unknown OR live stream OR fragment fetchDuration less than max allowed fetch duration, then this level matches
  7574. // we don't account for max Fetch Duration for live streams, this is to avoid switching down when near the edge of live sliding window ...
  7575. // special case to support startLevel = -1 (bitrateTest) on live streams : in that case we should not exit loop so that findBestLevel will return -1
  7576. fetchDuration <= ttfbEstimateSec || !isFiniteNumber(fetchDuration) || live && !_this2.bitrateTestDelay || fetchDuration < maxFetchDuration);
  7577. if (canSwitchWithinTolerance) {
  7578. var forcedAutoLevel = _this2.forcedAutoLevel;
  7579. if (i !== loadLevel && (forcedAutoLevel === -1 || forcedAutoLevel !== loadLevel)) {
  7580. if (levelsSkipped.length) {
  7581. logger.trace("[abr] Skipped level(s) " + levelsSkipped.join(',') + " of " + maxAutoLevel + " max with CODECS and VIDEO-RANGE:\"" + levels[levelsSkipped[0]].codecs + "\" " + levels[levelsSkipped[0]].videoRange + "; not compatible with \"" + level.codecs + "\" " + currentVideoRange);
  7582. }
  7583. logger.info("[abr] switch candidate:" + selectionBaseLevel + "->" + i + " adjustedbw(" + Math.round(adjustedbw) + ")-bitrate=" + Math.round(adjustedbw - bitrate) + " ttfb:" + ttfbEstimateSec.toFixed(1) + " avgDuration:" + avgDuration.toFixed(1) + " maxFetchDuration:" + maxFetchDuration.toFixed(1) + " fetchDuration:" + fetchDuration.toFixed(1) + " firstSelection:" + firstSelection + " codecSet:" + currentCodecSet + " videoRange:" + currentVideoRange + " hls.loadLevel:" + loadLevel);
  7584. }
  7585. if (firstSelection) {
  7586. _this2.firstSelection = i;
  7587. }
  7588. // as we are looping from highest to lowest, this will return the best achievable quality level
  7589. return {
  7590. v: i
  7591. };
  7592. }
  7593. },
  7594. _ret;
  7595. for (var i = maxAutoLevel; i >= minAutoLevel; i--) {
  7596. _ret = _loop();
  7597. if (_ret === 0) continue;
  7598. if (_ret) return _ret.v;
  7599. }
  7600. // not enough time budget even with quality level 0 ... rebuffering might happen
  7601. return -1;
  7602. };
  7603. _createClass(AbrController, [{
  7604. key: "firstAutoLevel",
  7605. get: function get() {
  7606. var _this$hls2 = this.hls,
  7607. maxAutoLevel = _this$hls2.maxAutoLevel,
  7608. minAutoLevel = _this$hls2.minAutoLevel;
  7609. var bwEstimate = this.getBwEstimate();
  7610. var maxStartDelay = this.hls.config.maxStarvationDelay;
  7611. var abrAutoLevel = this.findBestLevel(bwEstimate, minAutoLevel, maxAutoLevel, 0, maxStartDelay, 1, 1);
  7612. if (abrAutoLevel > -1) {
  7613. return abrAutoLevel;
  7614. }
  7615. var firstLevel = this.hls.firstLevel;
  7616. var clamped = Math.min(Math.max(firstLevel, minAutoLevel), maxAutoLevel);
  7617. logger.warn("[abr] Could not find best starting auto level. Defaulting to first in playlist " + firstLevel + " clamped to " + clamped);
  7618. return clamped;
  7619. }
  7620. }, {
  7621. key: "forcedAutoLevel",
  7622. get: function get() {
  7623. if (this.nextAutoLevelKey) {
  7624. return -1;
  7625. }
  7626. return this._nextAutoLevel;
  7627. }
  7628. // return next auto level
  7629. }, {
  7630. key: "nextAutoLevel",
  7631. get: function get() {
  7632. var forcedAutoLevel = this.forcedAutoLevel;
  7633. var bwEstimator = this.bwEstimator;
  7634. var useEstimate = bwEstimator.canEstimate();
  7635. var loadedFirstFrag = this.lastLoadedFragLevel > -1;
  7636. // in case next auto level has been forced, and bw not available or not reliable, return forced value
  7637. if (forcedAutoLevel !== -1 && (!useEstimate || !loadedFirstFrag || this.nextAutoLevelKey === this.getAutoLevelKey())) {
  7638. return forcedAutoLevel;
  7639. }
  7640. // compute next level using ABR logic
  7641. var nextABRAutoLevel = useEstimate && loadedFirstFrag ? this.getNextABRAutoLevel() : this.firstAutoLevel;
  7642. // use forced auto level while it hasn't errored more than ABR selection
  7643. if (forcedAutoLevel !== -1) {
  7644. var levels = this.hls.levels;
  7645. if (levels.length > Math.max(forcedAutoLevel, nextABRAutoLevel) && levels[forcedAutoLevel].loadError <= levels[nextABRAutoLevel].loadError) {
  7646. return forcedAutoLevel;
  7647. }
  7648. }
  7649. // save result until state has changed
  7650. this._nextAutoLevel = nextABRAutoLevel;
  7651. this.nextAutoLevelKey = this.getAutoLevelKey();
  7652. return nextABRAutoLevel;
  7653. },
  7654. set: function set(nextLevel) {
  7655. var _this$hls3 = this.hls,
  7656. maxAutoLevel = _this$hls3.maxAutoLevel,
  7657. minAutoLevel = _this$hls3.minAutoLevel;
  7658. var value = Math.min(Math.max(nextLevel, minAutoLevel), maxAutoLevel);
  7659. if (this._nextAutoLevel !== value) {
  7660. this.nextAutoLevelKey = '';
  7661. this._nextAutoLevel = value;
  7662. }
  7663. }
  7664. }]);
  7665. return AbrController;
  7666. }();
  7667. /**
  7668. * @ignore
  7669. * Sub-class specialization of EventHandler base class.
  7670. *
  7671. * TaskLoop allows to schedule a task function being called (optionnaly repeatedly) on the main loop,
  7672. * scheduled asynchroneously, avoiding recursive calls in the same tick.
  7673. *
  7674. * The task itself is implemented in `doTick`. It can be requested and called for single execution
  7675. * using the `tick` method.
  7676. *
  7677. * It will be assured that the task execution method (`tick`) only gets called once per main loop "tick",
  7678. * no matter how often it gets requested for execution. Execution in further ticks will be scheduled accordingly.
  7679. *
  7680. * If further execution requests have already been scheduled on the next tick, it can be checked with `hasNextTick`,
  7681. * and cancelled with `clearNextTick`.
  7682. *
  7683. * The task can be scheduled as an interval repeatedly with a period as parameter (see `setInterval`, `clearInterval`).
  7684. *
  7685. * Sub-classes need to implement the `doTick` method which will effectively have the task execution routine.
  7686. *
  7687. * Further explanations:
  7688. *
  7689. * The baseclass has a `tick` method that will schedule the doTick call. It may be called synchroneously
  7690. * only for a stack-depth of one. On re-entrant calls, sub-sequent calls are scheduled for next main loop ticks.
  7691. *
  7692. * When the task execution (`tick` method) is called in re-entrant way this is detected and
  7693. * we are limiting the task execution per call stack to exactly one, but scheduling/post-poning further
  7694. * task processing on the next main loop iteration (also known as "next tick" in the Node/JS runtime lingo).
  7695. */
  7696. var TaskLoop = /*#__PURE__*/function () {
  7697. function TaskLoop() {
  7698. this._boundTick = void 0;
  7699. this._tickTimer = null;
  7700. this._tickInterval = null;
  7701. this._tickCallCount = 0;
  7702. this._boundTick = this.tick.bind(this);
  7703. }
  7704. var _proto = TaskLoop.prototype;
  7705. _proto.destroy = function destroy() {
  7706. this.onHandlerDestroying();
  7707. this.onHandlerDestroyed();
  7708. };
  7709. _proto.onHandlerDestroying = function onHandlerDestroying() {
  7710. // clear all timers before unregistering from event bus
  7711. this.clearNextTick();
  7712. this.clearInterval();
  7713. };
  7714. _proto.onHandlerDestroyed = function onHandlerDestroyed() {};
  7715. _proto.hasInterval = function hasInterval() {
  7716. return !!this._tickInterval;
  7717. };
  7718. _proto.hasNextTick = function hasNextTick() {
  7719. return !!this._tickTimer;
  7720. }
  7721. /**
  7722. * @param millis - Interval time (ms)
  7723. * @eturns True when interval has been scheduled, false when already scheduled (no effect)
  7724. */;
  7725. _proto.setInterval = function setInterval(millis) {
  7726. if (!this._tickInterval) {
  7727. this._tickCallCount = 0;
  7728. this._tickInterval = self.setInterval(this._boundTick, millis);
  7729. return true;
  7730. }
  7731. return false;
  7732. }
  7733. /**
  7734. * @returns True when interval was cleared, false when none was set (no effect)
  7735. */;
  7736. _proto.clearInterval = function clearInterval() {
  7737. if (this._tickInterval) {
  7738. self.clearInterval(this._tickInterval);
  7739. this._tickInterval = null;
  7740. return true;
  7741. }
  7742. return false;
  7743. }
  7744. /**
  7745. * @returns True when timeout was cleared, false when none was set (no effect)
  7746. */;
  7747. _proto.clearNextTick = function clearNextTick() {
  7748. if (this._tickTimer) {
  7749. self.clearTimeout(this._tickTimer);
  7750. this._tickTimer = null;
  7751. return true;
  7752. }
  7753. return false;
  7754. }
  7755. /**
  7756. * Will call the subclass doTick implementation in this main loop tick
  7757. * or in the next one (via setTimeout(,0)) in case it has already been called
  7758. * in this tick (in case this is a re-entrant call).
  7759. */;
  7760. _proto.tick = function tick() {
  7761. this._tickCallCount++;
  7762. if (this._tickCallCount === 1) {
  7763. this.doTick();
  7764. // re-entrant call to tick from previous doTick call stack
  7765. // -> schedule a call on the next main loop iteration to process this task processing request
  7766. if (this._tickCallCount > 1) {
  7767. // make sure only one timer exists at any time at max
  7768. this.tickImmediate();
  7769. }
  7770. this._tickCallCount = 0;
  7771. }
  7772. };
  7773. _proto.tickImmediate = function tickImmediate() {
  7774. this.clearNextTick();
  7775. this._tickTimer = self.setTimeout(this._boundTick, 0);
  7776. }
  7777. /**
  7778. * For subclass to implement task logic
  7779. * @abstract
  7780. */;
  7781. _proto.doTick = function doTick() {};
  7782. return TaskLoop;
  7783. }();
  7784. var FragmentState = {
  7785. NOT_LOADED: "NOT_LOADED",
  7786. APPENDING: "APPENDING",
  7787. PARTIAL: "PARTIAL",
  7788. OK: "OK"
  7789. };
  7790. var FragmentTracker = /*#__PURE__*/function () {
  7791. function FragmentTracker(hls) {
  7792. this.activePartLists = Object.create(null);
  7793. this.endListFragments = Object.create(null);
  7794. this.fragments = Object.create(null);
  7795. this.timeRanges = Object.create(null);
  7796. this.bufferPadding = 0.2;
  7797. this.hls = void 0;
  7798. this.hasGaps = false;
  7799. this.hls = hls;
  7800. this._registerListeners();
  7801. }
  7802. var _proto = FragmentTracker.prototype;
  7803. _proto._registerListeners = function _registerListeners() {
  7804. var hls = this.hls;
  7805. hls.on(Events.BUFFER_APPENDED, this.onBufferAppended, this);
  7806. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  7807. hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
  7808. };
  7809. _proto._unregisterListeners = function _unregisterListeners() {
  7810. var hls = this.hls;
  7811. hls.off(Events.BUFFER_APPENDED, this.onBufferAppended, this);
  7812. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  7813. hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
  7814. };
  7815. _proto.destroy = function destroy() {
  7816. this._unregisterListeners();
  7817. // @ts-ignore
  7818. this.fragments =
  7819. // @ts-ignore
  7820. this.activePartLists =
  7821. // @ts-ignore
  7822. this.endListFragments = this.timeRanges = null;
  7823. }
  7824. /**
  7825. * Return a Fragment or Part with an appended range that matches the position and levelType
  7826. * Otherwise, return null
  7827. */;
  7828. _proto.getAppendedFrag = function getAppendedFrag(position, levelType) {
  7829. var activeParts = this.activePartLists[levelType];
  7830. if (activeParts) {
  7831. for (var i = activeParts.length; i--;) {
  7832. var activePart = activeParts[i];
  7833. if (!activePart) {
  7834. break;
  7835. }
  7836. var appendedPTS = activePart.end;
  7837. if (activePart.start <= position && appendedPTS !== null && position <= appendedPTS) {
  7838. return activePart;
  7839. }
  7840. }
  7841. }
  7842. return this.getBufferedFrag(position, levelType);
  7843. }
  7844. /**
  7845. * Return a buffered Fragment that matches the position and levelType.
  7846. * A buffered Fragment is one whose loading, parsing and appending is done (completed or "partial" meaning aborted).
  7847. * If not found any Fragment, return null
  7848. */;
  7849. _proto.getBufferedFrag = function getBufferedFrag(position, levelType) {
  7850. var fragments = this.fragments;
  7851. var keys = Object.keys(fragments);
  7852. for (var i = keys.length; i--;) {
  7853. var fragmentEntity = fragments[keys[i]];
  7854. if ((fragmentEntity == null ? void 0 : fragmentEntity.body.type) === levelType && fragmentEntity.buffered) {
  7855. var frag = fragmentEntity.body;
  7856. if (frag.start <= position && position <= frag.end) {
  7857. return frag;
  7858. }
  7859. }
  7860. }
  7861. return null;
  7862. }
  7863. /**
  7864. * Partial fragments effected by coded frame eviction will be removed
  7865. * The browser will unload parts of the buffer to free up memory for new buffer data
  7866. * Fragments will need to be reloaded when the buffer is freed up, removing partial fragments will allow them to reload(since there might be parts that are still playable)
  7867. */;
  7868. _proto.detectEvictedFragments = function detectEvictedFragments(elementaryStream, timeRange, playlistType, appendedPart) {
  7869. var _this = this;
  7870. if (this.timeRanges) {
  7871. this.timeRanges[elementaryStream] = timeRange;
  7872. }
  7873. // Check if any flagged fragments have been unloaded
  7874. // excluding anything newer than appendedPartSn
  7875. var appendedPartSn = (appendedPart == null ? void 0 : appendedPart.fragment.sn) || -1;
  7876. Object.keys(this.fragments).forEach(function (key) {
  7877. var fragmentEntity = _this.fragments[key];
  7878. if (!fragmentEntity) {
  7879. return;
  7880. }
  7881. if (appendedPartSn >= fragmentEntity.body.sn) {
  7882. return;
  7883. }
  7884. if (!fragmentEntity.buffered && !fragmentEntity.loaded) {
  7885. if (fragmentEntity.body.type === playlistType) {
  7886. _this.removeFragment(fragmentEntity.body);
  7887. }
  7888. return;
  7889. }
  7890. var esData = fragmentEntity.range[elementaryStream];
  7891. if (!esData) {
  7892. return;
  7893. }
  7894. esData.time.some(function (time) {
  7895. var isNotBuffered = !_this.isTimeBuffered(time.startPTS, time.endPTS, timeRange);
  7896. if (isNotBuffered) {
  7897. // Unregister partial fragment as it needs to load again to be reused
  7898. _this.removeFragment(fragmentEntity.body);
  7899. }
  7900. return isNotBuffered;
  7901. });
  7902. });
  7903. }
  7904. /**
  7905. * Checks if the fragment passed in is loaded in the buffer properly
  7906. * Partially loaded fragments will be registered as a partial fragment
  7907. */;
  7908. _proto.detectPartialFragments = function detectPartialFragments(data) {
  7909. var _this2 = this;
  7910. var timeRanges = this.timeRanges;
  7911. var frag = data.frag,
  7912. part = data.part;
  7913. if (!timeRanges || frag.sn === 'initSegment') {
  7914. return;
  7915. }
  7916. var fragKey = getFragmentKey(frag);
  7917. var fragmentEntity = this.fragments[fragKey];
  7918. if (!fragmentEntity || fragmentEntity.buffered && frag.gap) {
  7919. return;
  7920. }
  7921. var isFragHint = !frag.relurl;
  7922. Object.keys(timeRanges).forEach(function (elementaryStream) {
  7923. var streamInfo = frag.elementaryStreams[elementaryStream];
  7924. if (!streamInfo) {
  7925. return;
  7926. }
  7927. var timeRange = timeRanges[elementaryStream];
  7928. var partial = isFragHint || streamInfo.partial === true;
  7929. fragmentEntity.range[elementaryStream] = _this2.getBufferedTimes(frag, part, partial, timeRange);
  7930. });
  7931. fragmentEntity.loaded = null;
  7932. if (Object.keys(fragmentEntity.range).length) {
  7933. fragmentEntity.buffered = true;
  7934. var endList = fragmentEntity.body.endList = frag.endList || fragmentEntity.body.endList;
  7935. if (endList) {
  7936. this.endListFragments[fragmentEntity.body.type] = fragmentEntity;
  7937. }
  7938. if (!isPartial(fragmentEntity)) {
  7939. // Remove older fragment parts from lookup after frag is tracked as buffered
  7940. this.removeParts(frag.sn - 1, frag.type);
  7941. }
  7942. } else {
  7943. // remove fragment if nothing was appended
  7944. this.removeFragment(fragmentEntity.body);
  7945. }
  7946. };
  7947. _proto.removeParts = function removeParts(snToKeep, levelType) {
  7948. var activeParts = this.activePartLists[levelType];
  7949. if (!activeParts) {
  7950. return;
  7951. }
  7952. this.activePartLists[levelType] = activeParts.filter(function (part) {
  7953. return part.fragment.sn >= snToKeep;
  7954. });
  7955. };
  7956. _proto.fragBuffered = function fragBuffered(frag, force) {
  7957. var fragKey = getFragmentKey(frag);
  7958. var fragmentEntity = this.fragments[fragKey];
  7959. if (!fragmentEntity && force) {
  7960. fragmentEntity = this.fragments[fragKey] = {
  7961. body: frag,
  7962. appendedPTS: null,
  7963. loaded: null,
  7964. buffered: false,
  7965. range: Object.create(null)
  7966. };
  7967. if (frag.gap) {
  7968. this.hasGaps = true;
  7969. }
  7970. }
  7971. if (fragmentEntity) {
  7972. fragmentEntity.loaded = null;
  7973. fragmentEntity.buffered = true;
  7974. }
  7975. };
  7976. _proto.getBufferedTimes = function getBufferedTimes(fragment, part, partial, timeRange) {
  7977. var buffered = {
  7978. time: [],
  7979. partial: partial
  7980. };
  7981. var startPTS = fragment.start;
  7982. var endPTS = fragment.end;
  7983. var minEndPTS = fragment.minEndPTS || endPTS;
  7984. var maxStartPTS = fragment.maxStartPTS || startPTS;
  7985. for (var i = 0; i < timeRange.length; i++) {
  7986. var startTime = timeRange.start(i) - this.bufferPadding;
  7987. var endTime = timeRange.end(i) + this.bufferPadding;
  7988. if (maxStartPTS >= startTime && minEndPTS <= endTime) {
  7989. // Fragment is entirely contained in buffer
  7990. // No need to check the other timeRange times since it's completely playable
  7991. buffered.time.push({
  7992. startPTS: Math.max(startPTS, timeRange.start(i)),
  7993. endPTS: Math.min(endPTS, timeRange.end(i))
  7994. });
  7995. break;
  7996. } else if (startPTS < endTime && endPTS > startTime) {
  7997. var start = Math.max(startPTS, timeRange.start(i));
  7998. var end = Math.min(endPTS, timeRange.end(i));
  7999. if (end > start) {
  8000. buffered.partial = true;
  8001. // Check for intersection with buffer
  8002. // Get playable sections of the fragment
  8003. buffered.time.push({
  8004. startPTS: start,
  8005. endPTS: end
  8006. });
  8007. }
  8008. } else if (endPTS <= startTime) {
  8009. // No need to check the rest of the timeRange as it is in order
  8010. break;
  8011. }
  8012. }
  8013. return buffered;
  8014. }
  8015. /**
  8016. * Gets the partial fragment for a certain time
  8017. */;
  8018. _proto.getPartialFragment = function getPartialFragment(time) {
  8019. var bestFragment = null;
  8020. var timePadding;
  8021. var startTime;
  8022. var endTime;
  8023. var bestOverlap = 0;
  8024. var bufferPadding = this.bufferPadding,
  8025. fragments = this.fragments;
  8026. Object.keys(fragments).forEach(function (key) {
  8027. var fragmentEntity = fragments[key];
  8028. if (!fragmentEntity) {
  8029. return;
  8030. }
  8031. if (isPartial(fragmentEntity)) {
  8032. startTime = fragmentEntity.body.start - bufferPadding;
  8033. endTime = fragmentEntity.body.end + bufferPadding;
  8034. if (time >= startTime && time <= endTime) {
  8035. // Use the fragment that has the most padding from start and end time
  8036. timePadding = Math.min(time - startTime, endTime - time);
  8037. if (bestOverlap <= timePadding) {
  8038. bestFragment = fragmentEntity.body;
  8039. bestOverlap = timePadding;
  8040. }
  8041. }
  8042. }
  8043. });
  8044. return bestFragment;
  8045. };
  8046. _proto.isEndListAppended = function isEndListAppended(type) {
  8047. var lastFragmentEntity = this.endListFragments[type];
  8048. return lastFragmentEntity !== undefined && (lastFragmentEntity.buffered || isPartial(lastFragmentEntity));
  8049. };
  8050. _proto.getState = function getState(fragment) {
  8051. var fragKey = getFragmentKey(fragment);
  8052. var fragmentEntity = this.fragments[fragKey];
  8053. if (fragmentEntity) {
  8054. if (!fragmentEntity.buffered) {
  8055. return FragmentState.APPENDING;
  8056. } else if (isPartial(fragmentEntity)) {
  8057. return FragmentState.PARTIAL;
  8058. } else {
  8059. return FragmentState.OK;
  8060. }
  8061. }
  8062. return FragmentState.NOT_LOADED;
  8063. };
  8064. _proto.isTimeBuffered = function isTimeBuffered(startPTS, endPTS, timeRange) {
  8065. var startTime;
  8066. var endTime;
  8067. for (var i = 0; i < timeRange.length; i++) {
  8068. startTime = timeRange.start(i) - this.bufferPadding;
  8069. endTime = timeRange.end(i) + this.bufferPadding;
  8070. if (startPTS >= startTime && endPTS <= endTime) {
  8071. return true;
  8072. }
  8073. if (endPTS <= startTime) {
  8074. // No need to check the rest of the timeRange as it is in order
  8075. return false;
  8076. }
  8077. }
  8078. return false;
  8079. };
  8080. _proto.onFragLoaded = function onFragLoaded(event, data) {
  8081. var frag = data.frag,
  8082. part = data.part;
  8083. // don't track initsegment (for which sn is not a number)
  8084. // don't track frags used for bitrateTest, they're irrelevant.
  8085. if (frag.sn === 'initSegment' || frag.bitrateTest) {
  8086. return;
  8087. }
  8088. // Fragment entity `loaded` FragLoadedData is null when loading parts
  8089. var loaded = part ? null : data;
  8090. var fragKey = getFragmentKey(frag);
  8091. this.fragments[fragKey] = {
  8092. body: frag,
  8093. appendedPTS: null,
  8094. loaded: loaded,
  8095. buffered: false,
  8096. range: Object.create(null)
  8097. };
  8098. };
  8099. _proto.onBufferAppended = function onBufferAppended(event, data) {
  8100. var _this3 = this;
  8101. var frag = data.frag,
  8102. part = data.part,
  8103. timeRanges = data.timeRanges;
  8104. if (frag.sn === 'initSegment') {
  8105. return;
  8106. }
  8107. var playlistType = frag.type;
  8108. if (part) {
  8109. var activeParts = this.activePartLists[playlistType];
  8110. if (!activeParts) {
  8111. this.activePartLists[playlistType] = activeParts = [];
  8112. }
  8113. activeParts.push(part);
  8114. }
  8115. // Store the latest timeRanges loaded in the buffer
  8116. this.timeRanges = timeRanges;
  8117. Object.keys(timeRanges).forEach(function (elementaryStream) {
  8118. var timeRange = timeRanges[elementaryStream];
  8119. _this3.detectEvictedFragments(elementaryStream, timeRange, playlistType, part);
  8120. });
  8121. };
  8122. _proto.onFragBuffered = function onFragBuffered(event, data) {
  8123. this.detectPartialFragments(data);
  8124. };
  8125. _proto.hasFragment = function hasFragment(fragment) {
  8126. var fragKey = getFragmentKey(fragment);
  8127. return !!this.fragments[fragKey];
  8128. };
  8129. _proto.hasParts = function hasParts(type) {
  8130. var _this$activePartLists;
  8131. return !!((_this$activePartLists = this.activePartLists[type]) != null && _this$activePartLists.length);
  8132. };
  8133. _proto.removeFragmentsInRange = function removeFragmentsInRange(start, end, playlistType, withGapOnly, unbufferedOnly) {
  8134. var _this4 = this;
  8135. if (withGapOnly && !this.hasGaps) {
  8136. return;
  8137. }
  8138. Object.keys(this.fragments).forEach(function (key) {
  8139. var fragmentEntity = _this4.fragments[key];
  8140. if (!fragmentEntity) {
  8141. return;
  8142. }
  8143. var frag = fragmentEntity.body;
  8144. if (frag.type !== playlistType || withGapOnly && !frag.gap) {
  8145. return;
  8146. }
  8147. if (frag.start < end && frag.end > start && (fragmentEntity.buffered || unbufferedOnly)) {
  8148. _this4.removeFragment(frag);
  8149. }
  8150. });
  8151. };
  8152. _proto.removeFragment = function removeFragment(fragment) {
  8153. var fragKey = getFragmentKey(fragment);
  8154. fragment.stats.loaded = 0;
  8155. fragment.clearElementaryStreamInfo();
  8156. var activeParts = this.activePartLists[fragment.type];
  8157. if (activeParts) {
  8158. var snToRemove = fragment.sn;
  8159. this.activePartLists[fragment.type] = activeParts.filter(function (part) {
  8160. return part.fragment.sn !== snToRemove;
  8161. });
  8162. }
  8163. delete this.fragments[fragKey];
  8164. if (fragment.endList) {
  8165. delete this.endListFragments[fragment.type];
  8166. }
  8167. };
  8168. _proto.removeAllFragments = function removeAllFragments() {
  8169. this.fragments = Object.create(null);
  8170. this.endListFragments = Object.create(null);
  8171. this.activePartLists = Object.create(null);
  8172. this.hasGaps = false;
  8173. };
  8174. return FragmentTracker;
  8175. }();
  8176. function isPartial(fragmentEntity) {
  8177. var _fragmentEntity$range, _fragmentEntity$range2, _fragmentEntity$range3;
  8178. return fragmentEntity.buffered && (fragmentEntity.body.gap || ((_fragmentEntity$range = fragmentEntity.range.video) == null ? void 0 : _fragmentEntity$range.partial) || ((_fragmentEntity$range2 = fragmentEntity.range.audio) == null ? void 0 : _fragmentEntity$range2.partial) || ((_fragmentEntity$range3 = fragmentEntity.range.audiovideo) == null ? void 0 : _fragmentEntity$range3.partial));
  8179. }
  8180. function getFragmentKey(fragment) {
  8181. return fragment.type + "_" + fragment.level + "_" + fragment.sn;
  8182. }
  8183. /**
  8184. * Provides methods dealing with buffer length retrieval for example.
  8185. *
  8186. * In general, a helper around HTML5 MediaElement TimeRanges gathered from `buffered` property.
  8187. *
  8188. * Also @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/buffered
  8189. */
  8190. var noopBuffered = {
  8191. length: 0,
  8192. start: function start() {
  8193. return 0;
  8194. },
  8195. end: function end() {
  8196. return 0;
  8197. }
  8198. };
  8199. var BufferHelper = /*#__PURE__*/function () {
  8200. function BufferHelper() {}
  8201. /**
  8202. * Return true if `media`'s buffered include `position`
  8203. */
  8204. BufferHelper.isBuffered = function isBuffered(media, position) {
  8205. try {
  8206. if (media) {
  8207. var buffered = BufferHelper.getBuffered(media);
  8208. for (var i = 0; i < buffered.length; i++) {
  8209. if (position >= buffered.start(i) && position <= buffered.end(i)) {
  8210. return true;
  8211. }
  8212. }
  8213. }
  8214. } catch (error) {
  8215. // this is to catch
  8216. // InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
  8217. // This SourceBuffer has been removed from the parent media source
  8218. }
  8219. return false;
  8220. };
  8221. BufferHelper.bufferInfo = function bufferInfo(media, pos, maxHoleDuration) {
  8222. try {
  8223. if (media) {
  8224. var vbuffered = BufferHelper.getBuffered(media);
  8225. var buffered = [];
  8226. var i;
  8227. for (i = 0; i < vbuffered.length; i++) {
  8228. buffered.push({
  8229. start: vbuffered.start(i),
  8230. end: vbuffered.end(i)
  8231. });
  8232. }
  8233. return this.bufferedInfo(buffered, pos, maxHoleDuration);
  8234. }
  8235. } catch (error) {
  8236. // this is to catch
  8237. // InvalidStateError: Failed to read the 'buffered' property from 'SourceBuffer':
  8238. // This SourceBuffer has been removed from the parent media source
  8239. }
  8240. return {
  8241. len: 0,
  8242. start: pos,
  8243. end: pos,
  8244. nextStart: undefined
  8245. };
  8246. };
  8247. BufferHelper.bufferedInfo = function bufferedInfo(buffered, pos, maxHoleDuration) {
  8248. pos = Math.max(0, pos);
  8249. // sort on buffer.start/smaller end (IE does not always return sorted buffered range)
  8250. buffered.sort(function (a, b) {
  8251. var diff = a.start - b.start;
  8252. if (diff) {
  8253. return diff;
  8254. } else {
  8255. return b.end - a.end;
  8256. }
  8257. });
  8258. var buffered2 = [];
  8259. if (maxHoleDuration) {
  8260. // there might be some small holes between buffer time range
  8261. // consider that holes smaller than maxHoleDuration are irrelevant and build another
  8262. // buffer time range representations that discards those holes
  8263. for (var i = 0; i < buffered.length; i++) {
  8264. var buf2len = buffered2.length;
  8265. if (buf2len) {
  8266. var buf2end = buffered2[buf2len - 1].end;
  8267. // if small hole (value between 0 or maxHoleDuration ) or overlapping (negative)
  8268. if (buffered[i].start - buf2end < maxHoleDuration) {
  8269. // merge overlapping time ranges
  8270. // update lastRange.end only if smaller than item.end
  8271. // e.g. [ 1, 15] with [ 2,8] => [ 1,15] (no need to modify lastRange.end)
  8272. // whereas [ 1, 8] with [ 2,15] => [ 1,15] ( lastRange should switch from [1,8] to [1,15])
  8273. if (buffered[i].end > buf2end) {
  8274. buffered2[buf2len - 1].end = buffered[i].end;
  8275. }
  8276. } else {
  8277. // big hole
  8278. buffered2.push(buffered[i]);
  8279. }
  8280. } else {
  8281. // first value
  8282. buffered2.push(buffered[i]);
  8283. }
  8284. }
  8285. } else {
  8286. buffered2 = buffered;
  8287. }
  8288. var bufferLen = 0;
  8289. // bufferStartNext can possibly be undefined based on the conditional logic below
  8290. var bufferStartNext;
  8291. // bufferStart and bufferEnd are buffer boundaries around current video position
  8292. var bufferStart = pos;
  8293. var bufferEnd = pos;
  8294. for (var _i = 0; _i < buffered2.length; _i++) {
  8295. var start = buffered2[_i].start;
  8296. var end = buffered2[_i].end;
  8297. // logger.log('buf start/end:' + buffered.start(i) + '/' + buffered.end(i));
  8298. if (pos + maxHoleDuration >= start && pos < end) {
  8299. // play position is inside this buffer TimeRange, retrieve end of buffer position and buffer length
  8300. bufferStart = start;
  8301. bufferEnd = end;
  8302. bufferLen = bufferEnd - pos;
  8303. } else if (pos + maxHoleDuration < start) {
  8304. bufferStartNext = start;
  8305. break;
  8306. }
  8307. }
  8308. return {
  8309. len: bufferLen,
  8310. start: bufferStart || 0,
  8311. end: bufferEnd || 0,
  8312. nextStart: bufferStartNext
  8313. };
  8314. }
  8315. /**
  8316. * Safe method to get buffered property.
  8317. * SourceBuffer.buffered may throw if SourceBuffer is removed from it's MediaSource
  8318. */;
  8319. BufferHelper.getBuffered = function getBuffered(media) {
  8320. try {
  8321. return media.buffered;
  8322. } catch (e) {
  8323. logger.log('failed to get media.buffered', e);
  8324. return noopBuffered;
  8325. }
  8326. };
  8327. return BufferHelper;
  8328. }();
  8329. var ChunkMetadata = function ChunkMetadata(level, sn, id, size, part, partial) {
  8330. if (size === void 0) {
  8331. size = 0;
  8332. }
  8333. if (part === void 0) {
  8334. part = -1;
  8335. }
  8336. if (partial === void 0) {
  8337. partial = false;
  8338. }
  8339. this.level = void 0;
  8340. this.sn = void 0;
  8341. this.part = void 0;
  8342. this.id = void 0;
  8343. this.size = void 0;
  8344. this.partial = void 0;
  8345. this.transmuxing = getNewPerformanceTiming();
  8346. this.buffering = {
  8347. audio: getNewPerformanceTiming(),
  8348. video: getNewPerformanceTiming(),
  8349. audiovideo: getNewPerformanceTiming()
  8350. };
  8351. this.level = level;
  8352. this.sn = sn;
  8353. this.id = id;
  8354. this.size = size;
  8355. this.part = part;
  8356. this.partial = partial;
  8357. };
  8358. function getNewPerformanceTiming() {
  8359. return {
  8360. start: 0,
  8361. executeStart: 0,
  8362. executeEnd: 0,
  8363. end: 0
  8364. };
  8365. }
  8366. function findFirstFragWithCC(fragments, cc) {
  8367. for (var i = 0, len = fragments.length; i < len; i++) {
  8368. var _fragments$i;
  8369. if (((_fragments$i = fragments[i]) == null ? void 0 : _fragments$i.cc) === cc) {
  8370. return fragments[i];
  8371. }
  8372. }
  8373. return null;
  8374. }
  8375. function shouldAlignOnDiscontinuities(lastFrag, switchDetails, details) {
  8376. if (switchDetails) {
  8377. if (details.endCC > details.startCC || lastFrag && lastFrag.cc < details.startCC) {
  8378. return true;
  8379. }
  8380. }
  8381. return false;
  8382. }
  8383. // Find the first frag in the previous level which matches the CC of the first frag of the new level
  8384. function findDiscontinuousReferenceFrag(prevDetails, curDetails) {
  8385. var prevFrags = prevDetails.fragments;
  8386. var curFrags = curDetails.fragments;
  8387. if (!curFrags.length || !prevFrags.length) {
  8388. logger.log('No fragments to align');
  8389. return;
  8390. }
  8391. var prevStartFrag = findFirstFragWithCC(prevFrags, curFrags[0].cc);
  8392. if (!prevStartFrag || prevStartFrag && !prevStartFrag.startPTS) {
  8393. logger.log('No frag in previous level to align on');
  8394. return;
  8395. }
  8396. return prevStartFrag;
  8397. }
  8398. function adjustFragmentStart(frag, sliding) {
  8399. if (frag) {
  8400. var start = frag.start + sliding;
  8401. frag.start = frag.startPTS = start;
  8402. frag.endPTS = start + frag.duration;
  8403. }
  8404. }
  8405. function adjustSlidingStart(sliding, details) {
  8406. // Update segments
  8407. var fragments = details.fragments;
  8408. for (var i = 0, len = fragments.length; i < len; i++) {
  8409. adjustFragmentStart(fragments[i], sliding);
  8410. }
  8411. // Update LL-HLS parts at the end of the playlist
  8412. if (details.fragmentHint) {
  8413. adjustFragmentStart(details.fragmentHint, sliding);
  8414. }
  8415. details.alignedSliding = true;
  8416. }
  8417. /**
  8418. * Using the parameters of the last level, this function computes PTS' of the new fragments so that they form a
  8419. * contiguous stream with the last fragments.
  8420. * The PTS of a fragment lets Hls.js know where it fits into a stream - by knowing every PTS, we know which fragment to
  8421. * download at any given time. PTS is normally computed when the fragment is demuxed, so taking this step saves us time
  8422. * and an extra download.
  8423. * @param lastFrag
  8424. * @param lastLevel
  8425. * @param details
  8426. */
  8427. function alignStream(lastFrag, switchDetails, details) {
  8428. if (!switchDetails) {
  8429. return;
  8430. }
  8431. alignDiscontinuities(lastFrag, details, switchDetails);
  8432. if (!details.alignedSliding && switchDetails) {
  8433. // If the PTS wasn't figured out via discontinuity sequence that means there was no CC increase within the level.
  8434. // Aligning via Program Date Time should therefore be reliable, since PDT should be the same within the same
  8435. // discontinuity sequence.
  8436. alignMediaPlaylistByPDT(details, switchDetails);
  8437. }
  8438. if (!details.alignedSliding && switchDetails && !details.skippedSegments) {
  8439. // Try to align on sn so that we pick a better start fragment.
  8440. // Do not perform this on playlists with delta updates as this is only to align levels on switch
  8441. // and adjustSliding only adjusts fragments after skippedSegments.
  8442. adjustSliding(switchDetails, details);
  8443. }
  8444. }
  8445. /**
  8446. * Computes the PTS if a new level's fragments using the PTS of a fragment in the last level which shares the same
  8447. * discontinuity sequence.
  8448. * @param lastFrag - The last Fragment which shares the same discontinuity sequence
  8449. * @param lastLevel - The details of the last loaded level
  8450. * @param details - The details of the new level
  8451. */
  8452. function alignDiscontinuities(lastFrag, details, switchDetails) {
  8453. if (shouldAlignOnDiscontinuities(lastFrag, switchDetails, details)) {
  8454. var referenceFrag = findDiscontinuousReferenceFrag(switchDetails, details);
  8455. if (referenceFrag && isFiniteNumber(referenceFrag.start)) {
  8456. logger.log("Adjusting PTS using last level due to CC increase within current level " + details.url);
  8457. adjustSlidingStart(referenceFrag.start, details);
  8458. }
  8459. }
  8460. }
  8461. /**
  8462. * Ensures appropriate time-alignment between renditions based on PDT.
  8463. * This function assumes the timelines represented in `refDetails` are accurate, including the PDTs
  8464. * for the last discontinuity sequence number shared by both playlists when present,
  8465. * and uses the "wallclock"/PDT timeline as a cross-reference to `details`, adjusting the presentation
  8466. * times/timelines of `details` accordingly.
  8467. * Given the asynchronous nature of fetches and initial loads of live `main` and audio/subtitle tracks,
  8468. * the primary purpose of this function is to ensure the "local timelines" of audio/subtitle tracks
  8469. * are aligned to the main/video timeline, using PDT as the cross-reference/"anchor" that should
  8470. * be consistent across playlists, per the HLS spec.
  8471. * @param details - The details of the rendition you'd like to time-align (e.g. an audio rendition).
  8472. * @param refDetails - The details of the reference rendition with start and PDT times for alignment.
  8473. */
  8474. function alignMediaPlaylistByPDT(details, refDetails) {
  8475. if (!details.hasProgramDateTime || !refDetails.hasProgramDateTime) {
  8476. return;
  8477. }
  8478. var fragments = details.fragments;
  8479. var refFragments = refDetails.fragments;
  8480. if (!fragments.length || !refFragments.length) {
  8481. return;
  8482. }
  8483. // Calculate a delta to apply to all fragments according to the delta in PDT times and start times
  8484. // of a fragment in the reference details, and a fragment in the target details of the same discontinuity.
  8485. // If a fragment of the same discontinuity was not found use the middle fragment of both.
  8486. var refFrag;
  8487. var frag;
  8488. var targetCC = Math.min(refDetails.endCC, details.endCC);
  8489. if (refDetails.startCC < targetCC && details.startCC < targetCC) {
  8490. refFrag = findFirstFragWithCC(refFragments, targetCC);
  8491. frag = findFirstFragWithCC(fragments, targetCC);
  8492. }
  8493. if (!refFrag || !frag) {
  8494. refFrag = refFragments[Math.floor(refFragments.length / 2)];
  8495. frag = findFirstFragWithCC(fragments, refFrag.cc) || fragments[Math.floor(fragments.length / 2)];
  8496. }
  8497. var refPDT = refFrag.programDateTime;
  8498. var targetPDT = frag.programDateTime;
  8499. if (!refPDT || !targetPDT) {
  8500. return;
  8501. }
  8502. var delta = (targetPDT - refPDT) / 1000 - (frag.start - refFrag.start);
  8503. adjustSlidingStart(delta, details);
  8504. }
  8505. var MIN_CHUNK_SIZE = Math.pow(2, 17); // 128kb
  8506. var FragmentLoader = /*#__PURE__*/function () {
  8507. function FragmentLoader(config) {
  8508. this.config = void 0;
  8509. this.loader = null;
  8510. this.partLoadTimeout = -1;
  8511. this.config = config;
  8512. }
  8513. var _proto = FragmentLoader.prototype;
  8514. _proto.destroy = function destroy() {
  8515. if (this.loader) {
  8516. this.loader.destroy();
  8517. this.loader = null;
  8518. }
  8519. };
  8520. _proto.abort = function abort() {
  8521. if (this.loader) {
  8522. // Abort the loader for current fragment. Only one may load at any given time
  8523. this.loader.abort();
  8524. }
  8525. };
  8526. _proto.load = function load(frag, _onProgress) {
  8527. var _this = this;
  8528. var url = frag.url;
  8529. if (!url) {
  8530. return Promise.reject(new LoadError({
  8531. type: ErrorTypes.NETWORK_ERROR,
  8532. details: ErrorDetails.FRAG_LOAD_ERROR,
  8533. fatal: false,
  8534. frag: frag,
  8535. error: new Error("Fragment does not have a " + (url ? 'part list' : 'url')),
  8536. networkDetails: null
  8537. }));
  8538. }
  8539. this.abort();
  8540. var config = this.config;
  8541. var FragmentILoader = config.fLoader;
  8542. var DefaultILoader = config.loader;
  8543. return new Promise(function (resolve, reject) {
  8544. if (_this.loader) {
  8545. _this.loader.destroy();
  8546. }
  8547. if (frag.gap) {
  8548. if (frag.tagList.some(function (tags) {
  8549. return tags[0] === 'GAP';
  8550. })) {
  8551. reject(createGapLoadError(frag));
  8552. return;
  8553. } else {
  8554. // Reset temporary treatment as GAP tag
  8555. frag.gap = false;
  8556. }
  8557. }
  8558. var loader = _this.loader = frag.loader = FragmentILoader ? new FragmentILoader(config) : new DefaultILoader(config);
  8559. var loaderContext = createLoaderContext(frag);
  8560. var loadPolicy = getLoaderConfigWithoutReties(config.fragLoadPolicy.default);
  8561. var loaderConfig = {
  8562. loadPolicy: loadPolicy,
  8563. timeout: loadPolicy.maxLoadTimeMs,
  8564. maxRetry: 0,
  8565. retryDelay: 0,
  8566. maxRetryDelay: 0,
  8567. highWaterMark: frag.sn === 'initSegment' ? Infinity : MIN_CHUNK_SIZE
  8568. };
  8569. // Assign frag stats to the loader's stats reference
  8570. frag.stats = loader.stats;
  8571. loader.load(loaderContext, loaderConfig, {
  8572. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  8573. _this.resetLoader(frag, loader);
  8574. var payload = response.data;
  8575. if (context.resetIV && frag.decryptdata) {
  8576. frag.decryptdata.iv = new Uint8Array(payload.slice(0, 16));
  8577. payload = payload.slice(16);
  8578. }
  8579. resolve({
  8580. frag: frag,
  8581. part: null,
  8582. payload: payload,
  8583. networkDetails: networkDetails
  8584. });
  8585. },
  8586. onError: function onError(response, context, networkDetails, stats) {
  8587. _this.resetLoader(frag, loader);
  8588. reject(new LoadError({
  8589. type: ErrorTypes.NETWORK_ERROR,
  8590. details: ErrorDetails.FRAG_LOAD_ERROR,
  8591. fatal: false,
  8592. frag: frag,
  8593. response: _objectSpread2({
  8594. url: url,
  8595. data: undefined
  8596. }, response),
  8597. error: new Error("HTTP Error " + response.code + " " + response.text),
  8598. networkDetails: networkDetails,
  8599. stats: stats
  8600. }));
  8601. },
  8602. onAbort: function onAbort(stats, context, networkDetails) {
  8603. _this.resetLoader(frag, loader);
  8604. reject(new LoadError({
  8605. type: ErrorTypes.NETWORK_ERROR,
  8606. details: ErrorDetails.INTERNAL_ABORTED,
  8607. fatal: false,
  8608. frag: frag,
  8609. error: new Error('Aborted'),
  8610. networkDetails: networkDetails,
  8611. stats: stats
  8612. }));
  8613. },
  8614. onTimeout: function onTimeout(stats, context, networkDetails) {
  8615. _this.resetLoader(frag, loader);
  8616. reject(new LoadError({
  8617. type: ErrorTypes.NETWORK_ERROR,
  8618. details: ErrorDetails.FRAG_LOAD_TIMEOUT,
  8619. fatal: false,
  8620. frag: frag,
  8621. error: new Error("Timeout after " + loaderConfig.timeout + "ms"),
  8622. networkDetails: networkDetails,
  8623. stats: stats
  8624. }));
  8625. },
  8626. onProgress: function onProgress(stats, context, data, networkDetails) {
  8627. if (_onProgress) {
  8628. _onProgress({
  8629. frag: frag,
  8630. part: null,
  8631. payload: data,
  8632. networkDetails: networkDetails
  8633. });
  8634. }
  8635. }
  8636. });
  8637. });
  8638. };
  8639. _proto.loadPart = function loadPart(frag, part, onProgress) {
  8640. var _this2 = this;
  8641. this.abort();
  8642. var config = this.config;
  8643. var FragmentILoader = config.fLoader;
  8644. var DefaultILoader = config.loader;
  8645. return new Promise(function (resolve, reject) {
  8646. if (_this2.loader) {
  8647. _this2.loader.destroy();
  8648. }
  8649. if (frag.gap || part.gap) {
  8650. reject(createGapLoadError(frag, part));
  8651. return;
  8652. }
  8653. var loader = _this2.loader = frag.loader = FragmentILoader ? new FragmentILoader(config) : new DefaultILoader(config);
  8654. var loaderContext = createLoaderContext(frag, part);
  8655. // Should we define another load policy for parts?
  8656. var loadPolicy = getLoaderConfigWithoutReties(config.fragLoadPolicy.default);
  8657. var loaderConfig = {
  8658. loadPolicy: loadPolicy,
  8659. timeout: loadPolicy.maxLoadTimeMs,
  8660. maxRetry: 0,
  8661. retryDelay: 0,
  8662. maxRetryDelay: 0,
  8663. highWaterMark: MIN_CHUNK_SIZE
  8664. };
  8665. // Assign part stats to the loader's stats reference
  8666. part.stats = loader.stats;
  8667. loader.load(loaderContext, loaderConfig, {
  8668. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  8669. _this2.resetLoader(frag, loader);
  8670. _this2.updateStatsFromPart(frag, part);
  8671. var partLoadedData = {
  8672. frag: frag,
  8673. part: part,
  8674. payload: response.data,
  8675. networkDetails: networkDetails
  8676. };
  8677. onProgress(partLoadedData);
  8678. resolve(partLoadedData);
  8679. },
  8680. onError: function onError(response, context, networkDetails, stats) {
  8681. _this2.resetLoader(frag, loader);
  8682. reject(new LoadError({
  8683. type: ErrorTypes.NETWORK_ERROR,
  8684. details: ErrorDetails.FRAG_LOAD_ERROR,
  8685. fatal: false,
  8686. frag: frag,
  8687. part: part,
  8688. response: _objectSpread2({
  8689. url: loaderContext.url,
  8690. data: undefined
  8691. }, response),
  8692. error: new Error("HTTP Error " + response.code + " " + response.text),
  8693. networkDetails: networkDetails,
  8694. stats: stats
  8695. }));
  8696. },
  8697. onAbort: function onAbort(stats, context, networkDetails) {
  8698. frag.stats.aborted = part.stats.aborted;
  8699. _this2.resetLoader(frag, loader);
  8700. reject(new LoadError({
  8701. type: ErrorTypes.NETWORK_ERROR,
  8702. details: ErrorDetails.INTERNAL_ABORTED,
  8703. fatal: false,
  8704. frag: frag,
  8705. part: part,
  8706. error: new Error('Aborted'),
  8707. networkDetails: networkDetails,
  8708. stats: stats
  8709. }));
  8710. },
  8711. onTimeout: function onTimeout(stats, context, networkDetails) {
  8712. _this2.resetLoader(frag, loader);
  8713. reject(new LoadError({
  8714. type: ErrorTypes.NETWORK_ERROR,
  8715. details: ErrorDetails.FRAG_LOAD_TIMEOUT,
  8716. fatal: false,
  8717. frag: frag,
  8718. part: part,
  8719. error: new Error("Timeout after " + loaderConfig.timeout + "ms"),
  8720. networkDetails: networkDetails,
  8721. stats: stats
  8722. }));
  8723. }
  8724. });
  8725. });
  8726. };
  8727. _proto.updateStatsFromPart = function updateStatsFromPart(frag, part) {
  8728. var fragStats = frag.stats;
  8729. var partStats = part.stats;
  8730. var partTotal = partStats.total;
  8731. fragStats.loaded += partStats.loaded;
  8732. if (partTotal) {
  8733. var estTotalParts = Math.round(frag.duration / part.duration);
  8734. var estLoadedParts = Math.min(Math.round(fragStats.loaded / partTotal), estTotalParts);
  8735. var estRemainingParts = estTotalParts - estLoadedParts;
  8736. var estRemainingBytes = estRemainingParts * Math.round(fragStats.loaded / estLoadedParts);
  8737. fragStats.total = fragStats.loaded + estRemainingBytes;
  8738. } else {
  8739. fragStats.total = Math.max(fragStats.loaded, fragStats.total);
  8740. }
  8741. var fragLoading = fragStats.loading;
  8742. var partLoading = partStats.loading;
  8743. if (fragLoading.start) {
  8744. // add to fragment loader latency
  8745. fragLoading.first += partLoading.first - partLoading.start;
  8746. } else {
  8747. fragLoading.start = partLoading.start;
  8748. fragLoading.first = partLoading.first;
  8749. }
  8750. fragLoading.end = partLoading.end;
  8751. };
  8752. _proto.resetLoader = function resetLoader(frag, loader) {
  8753. frag.loader = null;
  8754. if (this.loader === loader) {
  8755. self.clearTimeout(this.partLoadTimeout);
  8756. this.loader = null;
  8757. }
  8758. loader.destroy();
  8759. };
  8760. return FragmentLoader;
  8761. }();
  8762. function createLoaderContext(frag, part) {
  8763. if (part === void 0) {
  8764. part = null;
  8765. }
  8766. var segment = part || frag;
  8767. var loaderContext = {
  8768. frag: frag,
  8769. part: part,
  8770. responseType: 'arraybuffer',
  8771. url: segment.url,
  8772. headers: {},
  8773. rangeStart: 0,
  8774. rangeEnd: 0
  8775. };
  8776. var start = segment.byteRangeStartOffset;
  8777. var end = segment.byteRangeEndOffset;
  8778. if (isFiniteNumber(start) && isFiniteNumber(end)) {
  8779. var _frag$decryptdata;
  8780. var byteRangeStart = start;
  8781. var byteRangeEnd = end;
  8782. if (frag.sn === 'initSegment' && ((_frag$decryptdata = frag.decryptdata) == null ? void 0 : _frag$decryptdata.method) === 'AES-128') {
  8783. // MAP segment encrypted with method 'AES-128', when served with HTTP Range,
  8784. // has the unencrypted size specified in the range.
  8785. // Ref: https://tools.ietf.org/html/draft-pantos-hls-rfc8216bis-08#section-6.3.6
  8786. var fragmentLen = end - start;
  8787. if (fragmentLen % 16) {
  8788. byteRangeEnd = end + (16 - fragmentLen % 16);
  8789. }
  8790. if (start !== 0) {
  8791. loaderContext.resetIV = true;
  8792. byteRangeStart = start - 16;
  8793. }
  8794. }
  8795. loaderContext.rangeStart = byteRangeStart;
  8796. loaderContext.rangeEnd = byteRangeEnd;
  8797. }
  8798. return loaderContext;
  8799. }
  8800. function createGapLoadError(frag, part) {
  8801. var error = new Error("GAP " + (frag.gap ? 'tag' : 'attribute') + " found");
  8802. var errorData = {
  8803. type: ErrorTypes.MEDIA_ERROR,
  8804. details: ErrorDetails.FRAG_GAP,
  8805. fatal: false,
  8806. frag: frag,
  8807. error: error,
  8808. networkDetails: null
  8809. };
  8810. if (part) {
  8811. errorData.part = part;
  8812. }
  8813. (part ? part : frag).stats.aborted = true;
  8814. return new LoadError(errorData);
  8815. }
  8816. var LoadError = /*#__PURE__*/function (_Error) {
  8817. _inheritsLoose(LoadError, _Error);
  8818. function LoadError(data) {
  8819. var _this3;
  8820. _this3 = _Error.call(this, data.error.message) || this;
  8821. _this3.data = void 0;
  8822. _this3.data = data;
  8823. return _this3;
  8824. }
  8825. return LoadError;
  8826. }( /*#__PURE__*/_wrapNativeSuper(Error));
  8827. var AESCrypto = /*#__PURE__*/function () {
  8828. function AESCrypto(subtle, iv) {
  8829. this.subtle = void 0;
  8830. this.aesIV = void 0;
  8831. this.subtle = subtle;
  8832. this.aesIV = iv;
  8833. }
  8834. var _proto = AESCrypto.prototype;
  8835. _proto.decrypt = function decrypt(data, key) {
  8836. return this.subtle.decrypt({
  8837. name: 'AES-CBC',
  8838. iv: this.aesIV
  8839. }, key, data);
  8840. };
  8841. return AESCrypto;
  8842. }();
  8843. var FastAESKey = /*#__PURE__*/function () {
  8844. function FastAESKey(subtle, key) {
  8845. this.subtle = void 0;
  8846. this.key = void 0;
  8847. this.subtle = subtle;
  8848. this.key = key;
  8849. }
  8850. var _proto = FastAESKey.prototype;
  8851. _proto.expandKey = function expandKey() {
  8852. return this.subtle.importKey('raw', this.key, {
  8853. name: 'AES-CBC'
  8854. }, false, ['encrypt', 'decrypt']);
  8855. };
  8856. return FastAESKey;
  8857. }();
  8858. // PKCS7
  8859. function removePadding(array) {
  8860. var outputBytes = array.byteLength;
  8861. var paddingBytes = outputBytes && new DataView(array.buffer).getUint8(outputBytes - 1);
  8862. if (paddingBytes) {
  8863. return sliceUint8(array, 0, outputBytes - paddingBytes);
  8864. }
  8865. return array;
  8866. }
  8867. var AESDecryptor = /*#__PURE__*/function () {
  8868. function AESDecryptor() {
  8869. this.rcon = [0x0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  8870. this.subMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
  8871. this.invSubMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
  8872. this.sBox = new Uint32Array(256);
  8873. this.invSBox = new Uint32Array(256);
  8874. this.key = new Uint32Array(0);
  8875. this.ksRows = 0;
  8876. this.keySize = 0;
  8877. this.keySchedule = void 0;
  8878. this.invKeySchedule = void 0;
  8879. this.initTable();
  8880. }
  8881. // Using view.getUint32() also swaps the byte order.
  8882. var _proto = AESDecryptor.prototype;
  8883. _proto.uint8ArrayToUint32Array_ = function uint8ArrayToUint32Array_(arrayBuffer) {
  8884. var view = new DataView(arrayBuffer);
  8885. var newArray = new Uint32Array(4);
  8886. for (var i = 0; i < 4; i++) {
  8887. newArray[i] = view.getUint32(i * 4);
  8888. }
  8889. return newArray;
  8890. };
  8891. _proto.initTable = function initTable() {
  8892. var sBox = this.sBox;
  8893. var invSBox = this.invSBox;
  8894. var subMix = this.subMix;
  8895. var subMix0 = subMix[0];
  8896. var subMix1 = subMix[1];
  8897. var subMix2 = subMix[2];
  8898. var subMix3 = subMix[3];
  8899. var invSubMix = this.invSubMix;
  8900. var invSubMix0 = invSubMix[0];
  8901. var invSubMix1 = invSubMix[1];
  8902. var invSubMix2 = invSubMix[2];
  8903. var invSubMix3 = invSubMix[3];
  8904. var d = new Uint32Array(256);
  8905. var x = 0;
  8906. var xi = 0;
  8907. var i = 0;
  8908. for (i = 0; i < 256; i++) {
  8909. if (i < 128) {
  8910. d[i] = i << 1;
  8911. } else {
  8912. d[i] = i << 1 ^ 0x11b;
  8913. }
  8914. }
  8915. for (i = 0; i < 256; i++) {
  8916. var sx = xi ^ xi << 1 ^ xi << 2 ^ xi << 3 ^ xi << 4;
  8917. sx = sx >>> 8 ^ sx & 0xff ^ 0x63;
  8918. sBox[x] = sx;
  8919. invSBox[sx] = x;
  8920. // Compute multiplication
  8921. var x2 = d[x];
  8922. var x4 = d[x2];
  8923. var x8 = d[x4];
  8924. // Compute sub/invSub bytes, mix columns tables
  8925. var t = d[sx] * 0x101 ^ sx * 0x1010100;
  8926. subMix0[x] = t << 24 | t >>> 8;
  8927. subMix1[x] = t << 16 | t >>> 16;
  8928. subMix2[x] = t << 8 | t >>> 24;
  8929. subMix3[x] = t;
  8930. // Compute inv sub bytes, inv mix columns tables
  8931. t = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
  8932. invSubMix0[sx] = t << 24 | t >>> 8;
  8933. invSubMix1[sx] = t << 16 | t >>> 16;
  8934. invSubMix2[sx] = t << 8 | t >>> 24;
  8935. invSubMix3[sx] = t;
  8936. // Compute next counter
  8937. if (!x) {
  8938. x = xi = 1;
  8939. } else {
  8940. x = x2 ^ d[d[d[x8 ^ x2]]];
  8941. xi ^= d[d[xi]];
  8942. }
  8943. }
  8944. };
  8945. _proto.expandKey = function expandKey(keyBuffer) {
  8946. // convert keyBuffer to Uint32Array
  8947. var key = this.uint8ArrayToUint32Array_(keyBuffer);
  8948. var sameKey = true;
  8949. var offset = 0;
  8950. while (offset < key.length && sameKey) {
  8951. sameKey = key[offset] === this.key[offset];
  8952. offset++;
  8953. }
  8954. if (sameKey) {
  8955. return;
  8956. }
  8957. this.key = key;
  8958. var keySize = this.keySize = key.length;
  8959. if (keySize !== 4 && keySize !== 6 && keySize !== 8) {
  8960. throw new Error('Invalid aes key size=' + keySize);
  8961. }
  8962. var ksRows = this.ksRows = (keySize + 6 + 1) * 4;
  8963. var ksRow;
  8964. var invKsRow;
  8965. var keySchedule = this.keySchedule = new Uint32Array(ksRows);
  8966. var invKeySchedule = this.invKeySchedule = new Uint32Array(ksRows);
  8967. var sbox = this.sBox;
  8968. var rcon = this.rcon;
  8969. var invSubMix = this.invSubMix;
  8970. var invSubMix0 = invSubMix[0];
  8971. var invSubMix1 = invSubMix[1];
  8972. var invSubMix2 = invSubMix[2];
  8973. var invSubMix3 = invSubMix[3];
  8974. var prev;
  8975. var t;
  8976. for (ksRow = 0; ksRow < ksRows; ksRow++) {
  8977. if (ksRow < keySize) {
  8978. prev = keySchedule[ksRow] = key[ksRow];
  8979. continue;
  8980. }
  8981. t = prev;
  8982. if (ksRow % keySize === 0) {
  8983. // Rot word
  8984. t = t << 8 | t >>> 24;
  8985. // Sub word
  8986. t = sbox[t >>> 24] << 24 | sbox[t >>> 16 & 0xff] << 16 | sbox[t >>> 8 & 0xff] << 8 | sbox[t & 0xff];
  8987. // Mix Rcon
  8988. t ^= rcon[ksRow / keySize | 0] << 24;
  8989. } else if (keySize > 6 && ksRow % keySize === 4) {
  8990. // Sub word
  8991. t = sbox[t >>> 24] << 24 | sbox[t >>> 16 & 0xff] << 16 | sbox[t >>> 8 & 0xff] << 8 | sbox[t & 0xff];
  8992. }
  8993. keySchedule[ksRow] = prev = (keySchedule[ksRow - keySize] ^ t) >>> 0;
  8994. }
  8995. for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  8996. ksRow = ksRows - invKsRow;
  8997. if (invKsRow & 3) {
  8998. t = keySchedule[ksRow];
  8999. } else {
  9000. t = keySchedule[ksRow - 4];
  9001. }
  9002. if (invKsRow < 4 || ksRow <= 4) {
  9003. invKeySchedule[invKsRow] = t;
  9004. } else {
  9005. invKeySchedule[invKsRow] = invSubMix0[sbox[t >>> 24]] ^ invSubMix1[sbox[t >>> 16 & 0xff]] ^ invSubMix2[sbox[t >>> 8 & 0xff]] ^ invSubMix3[sbox[t & 0xff]];
  9006. }
  9007. invKeySchedule[invKsRow] = invKeySchedule[invKsRow] >>> 0;
  9008. }
  9009. }
  9010. // Adding this as a method greatly improves performance.
  9011. ;
  9012. _proto.networkToHostOrderSwap = function networkToHostOrderSwap(word) {
  9013. return word << 24 | (word & 0xff00) << 8 | (word & 0xff0000) >> 8 | word >>> 24;
  9014. };
  9015. _proto.decrypt = function decrypt(inputArrayBuffer, offset, aesIV) {
  9016. var nRounds = this.keySize + 6;
  9017. var invKeySchedule = this.invKeySchedule;
  9018. var invSBOX = this.invSBox;
  9019. var invSubMix = this.invSubMix;
  9020. var invSubMix0 = invSubMix[0];
  9021. var invSubMix1 = invSubMix[1];
  9022. var invSubMix2 = invSubMix[2];
  9023. var invSubMix3 = invSubMix[3];
  9024. var initVector = this.uint8ArrayToUint32Array_(aesIV);
  9025. var initVector0 = initVector[0];
  9026. var initVector1 = initVector[1];
  9027. var initVector2 = initVector[2];
  9028. var initVector3 = initVector[3];
  9029. var inputInt32 = new Int32Array(inputArrayBuffer);
  9030. var outputInt32 = new Int32Array(inputInt32.length);
  9031. var t0, t1, t2, t3;
  9032. var s0, s1, s2, s3;
  9033. var inputWords0, inputWords1, inputWords2, inputWords3;
  9034. var ksRow, i;
  9035. var swapWord = this.networkToHostOrderSwap;
  9036. while (offset < inputInt32.length) {
  9037. inputWords0 = swapWord(inputInt32[offset]);
  9038. inputWords1 = swapWord(inputInt32[offset + 1]);
  9039. inputWords2 = swapWord(inputInt32[offset + 2]);
  9040. inputWords3 = swapWord(inputInt32[offset + 3]);
  9041. s0 = inputWords0 ^ invKeySchedule[0];
  9042. s1 = inputWords3 ^ invKeySchedule[1];
  9043. s2 = inputWords2 ^ invKeySchedule[2];
  9044. s3 = inputWords1 ^ invKeySchedule[3];
  9045. ksRow = 4;
  9046. // Iterate through the rounds of decryption
  9047. for (i = 1; i < nRounds; i++) {
  9048. t0 = invSubMix0[s0 >>> 24] ^ invSubMix1[s1 >> 16 & 0xff] ^ invSubMix2[s2 >> 8 & 0xff] ^ invSubMix3[s3 & 0xff] ^ invKeySchedule[ksRow];
  9049. t1 = invSubMix0[s1 >>> 24] ^ invSubMix1[s2 >> 16 & 0xff] ^ invSubMix2[s3 >> 8 & 0xff] ^ invSubMix3[s0 & 0xff] ^ invKeySchedule[ksRow + 1];
  9050. t2 = invSubMix0[s2 >>> 24] ^ invSubMix1[s3 >> 16 & 0xff] ^ invSubMix2[s0 >> 8 & 0xff] ^ invSubMix3[s1 & 0xff] ^ invKeySchedule[ksRow + 2];
  9051. t3 = invSubMix0[s3 >>> 24] ^ invSubMix1[s0 >> 16 & 0xff] ^ invSubMix2[s1 >> 8 & 0xff] ^ invSubMix3[s2 & 0xff] ^ invKeySchedule[ksRow + 3];
  9052. // Update state
  9053. s0 = t0;
  9054. s1 = t1;
  9055. s2 = t2;
  9056. s3 = t3;
  9057. ksRow = ksRow + 4;
  9058. }
  9059. // Shift rows, sub bytes, add round key
  9060. t0 = invSBOX[s0 >>> 24] << 24 ^ invSBOX[s1 >> 16 & 0xff] << 16 ^ invSBOX[s2 >> 8 & 0xff] << 8 ^ invSBOX[s3 & 0xff] ^ invKeySchedule[ksRow];
  9061. t1 = invSBOX[s1 >>> 24] << 24 ^ invSBOX[s2 >> 16 & 0xff] << 16 ^ invSBOX[s3 >> 8 & 0xff] << 8 ^ invSBOX[s0 & 0xff] ^ invKeySchedule[ksRow + 1];
  9062. t2 = invSBOX[s2 >>> 24] << 24 ^ invSBOX[s3 >> 16 & 0xff] << 16 ^ invSBOX[s0 >> 8 & 0xff] << 8 ^ invSBOX[s1 & 0xff] ^ invKeySchedule[ksRow + 2];
  9063. t3 = invSBOX[s3 >>> 24] << 24 ^ invSBOX[s0 >> 16 & 0xff] << 16 ^ invSBOX[s1 >> 8 & 0xff] << 8 ^ invSBOX[s2 & 0xff] ^ invKeySchedule[ksRow + 3];
  9064. // Write
  9065. outputInt32[offset] = swapWord(t0 ^ initVector0);
  9066. outputInt32[offset + 1] = swapWord(t3 ^ initVector1);
  9067. outputInt32[offset + 2] = swapWord(t2 ^ initVector2);
  9068. outputInt32[offset + 3] = swapWord(t1 ^ initVector3);
  9069. // reset initVector to last 4 unsigned int
  9070. initVector0 = inputWords0;
  9071. initVector1 = inputWords1;
  9072. initVector2 = inputWords2;
  9073. initVector3 = inputWords3;
  9074. offset = offset + 4;
  9075. }
  9076. return outputInt32.buffer;
  9077. };
  9078. return AESDecryptor;
  9079. }();
  9080. var CHUNK_SIZE = 16; // 16 bytes, 128 bits
  9081. var Decrypter = /*#__PURE__*/function () {
  9082. function Decrypter(config, _temp) {
  9083. var _ref = _temp === void 0 ? {} : _temp,
  9084. _ref$removePKCS7Paddi = _ref.removePKCS7Padding,
  9085. removePKCS7Padding = _ref$removePKCS7Paddi === void 0 ? true : _ref$removePKCS7Paddi;
  9086. this.logEnabled = true;
  9087. this.removePKCS7Padding = void 0;
  9088. this.subtle = null;
  9089. this.softwareDecrypter = null;
  9090. this.key = null;
  9091. this.fastAesKey = null;
  9092. this.remainderData = null;
  9093. this.currentIV = null;
  9094. this.currentResult = null;
  9095. this.useSoftware = void 0;
  9096. this.useSoftware = config.enableSoftwareAES;
  9097. this.removePKCS7Padding = removePKCS7Padding;
  9098. // built in decryptor expects PKCS7 padding
  9099. if (removePKCS7Padding) {
  9100. try {
  9101. var browserCrypto = self.crypto;
  9102. if (browserCrypto) {
  9103. this.subtle = browserCrypto.subtle || browserCrypto.webkitSubtle;
  9104. }
  9105. } catch (e) {
  9106. /* no-op */
  9107. }
  9108. }
  9109. this.useSoftware = !this.subtle;
  9110. }
  9111. var _proto = Decrypter.prototype;
  9112. _proto.destroy = function destroy() {
  9113. this.subtle = null;
  9114. this.softwareDecrypter = null;
  9115. this.key = null;
  9116. this.fastAesKey = null;
  9117. this.remainderData = null;
  9118. this.currentIV = null;
  9119. this.currentResult = null;
  9120. };
  9121. _proto.isSync = function isSync() {
  9122. return this.useSoftware;
  9123. };
  9124. _proto.flush = function flush() {
  9125. var currentResult = this.currentResult,
  9126. remainderData = this.remainderData;
  9127. if (!currentResult || remainderData) {
  9128. this.reset();
  9129. return null;
  9130. }
  9131. var data = new Uint8Array(currentResult);
  9132. this.reset();
  9133. if (this.removePKCS7Padding) {
  9134. return removePadding(data);
  9135. }
  9136. return data;
  9137. };
  9138. _proto.reset = function reset() {
  9139. this.currentResult = null;
  9140. this.currentIV = null;
  9141. this.remainderData = null;
  9142. if (this.softwareDecrypter) {
  9143. this.softwareDecrypter = null;
  9144. }
  9145. };
  9146. _proto.decrypt = function decrypt(data, key, iv) {
  9147. var _this = this;
  9148. if (this.useSoftware) {
  9149. return new Promise(function (resolve, reject) {
  9150. _this.softwareDecrypt(new Uint8Array(data), key, iv);
  9151. var decryptResult = _this.flush();
  9152. if (decryptResult) {
  9153. resolve(decryptResult.buffer);
  9154. } else {
  9155. reject(new Error('[softwareDecrypt] Failed to decrypt data'));
  9156. }
  9157. });
  9158. }
  9159. return this.webCryptoDecrypt(new Uint8Array(data), key, iv);
  9160. }
  9161. // Software decryption is progressive. Progressive decryption may not return a result on each call. Any cached
  9162. // data is handled in the flush() call
  9163. ;
  9164. _proto.softwareDecrypt = function softwareDecrypt(data, key, iv) {
  9165. var currentIV = this.currentIV,
  9166. currentResult = this.currentResult,
  9167. remainderData = this.remainderData;
  9168. this.logOnce('JS AES decrypt');
  9169. // The output is staggered during progressive parsing - the current result is cached, and emitted on the next call
  9170. // This is done in order to strip PKCS7 padding, which is found at the end of each segment. We only know we've reached
  9171. // the end on flush(), but by that time we have already received all bytes for the segment.
  9172. // Progressive decryption does not work with WebCrypto
  9173. if (remainderData) {
  9174. data = appendUint8Array(remainderData, data);
  9175. this.remainderData = null;
  9176. }
  9177. // Byte length must be a multiple of 16 (AES-128 = 128 bit blocks = 16 bytes)
  9178. var currentChunk = this.getValidChunk(data);
  9179. if (!currentChunk.length) {
  9180. return null;
  9181. }
  9182. if (currentIV) {
  9183. iv = currentIV;
  9184. }
  9185. var softwareDecrypter = this.softwareDecrypter;
  9186. if (!softwareDecrypter) {
  9187. softwareDecrypter = this.softwareDecrypter = new AESDecryptor();
  9188. }
  9189. softwareDecrypter.expandKey(key);
  9190. var result = currentResult;
  9191. this.currentResult = softwareDecrypter.decrypt(currentChunk.buffer, 0, iv);
  9192. this.currentIV = sliceUint8(currentChunk, -16).buffer;
  9193. if (!result) {
  9194. return null;
  9195. }
  9196. return result;
  9197. };
  9198. _proto.webCryptoDecrypt = function webCryptoDecrypt(data, key, iv) {
  9199. var _this2 = this;
  9200. if (this.key !== key || !this.fastAesKey) {
  9201. if (!this.subtle) {
  9202. return Promise.resolve(this.onWebCryptoError(data, key, iv));
  9203. }
  9204. this.key = key;
  9205. this.fastAesKey = new FastAESKey(this.subtle, key);
  9206. }
  9207. return this.fastAesKey.expandKey().then(function (aesKey) {
  9208. // decrypt using web crypto
  9209. if (!_this2.subtle) {
  9210. return Promise.reject(new Error('web crypto not initialized'));
  9211. }
  9212. _this2.logOnce('WebCrypto AES decrypt');
  9213. var crypto = new AESCrypto(_this2.subtle, new Uint8Array(iv));
  9214. return crypto.decrypt(data.buffer, aesKey);
  9215. }).catch(function (err) {
  9216. logger.warn("[decrypter]: WebCrypto Error, disable WebCrypto API, " + err.name + ": " + err.message);
  9217. return _this2.onWebCryptoError(data, key, iv);
  9218. });
  9219. };
  9220. _proto.onWebCryptoError = function onWebCryptoError(data, key, iv) {
  9221. this.useSoftware = true;
  9222. this.logEnabled = true;
  9223. this.softwareDecrypt(data, key, iv);
  9224. var decryptResult = this.flush();
  9225. if (decryptResult) {
  9226. return decryptResult.buffer;
  9227. }
  9228. throw new Error('WebCrypto and softwareDecrypt: failed to decrypt data');
  9229. };
  9230. _proto.getValidChunk = function getValidChunk(data) {
  9231. var currentChunk = data;
  9232. var splitPoint = data.length - data.length % CHUNK_SIZE;
  9233. if (splitPoint !== data.length) {
  9234. currentChunk = sliceUint8(data, 0, splitPoint);
  9235. this.remainderData = sliceUint8(data, splitPoint);
  9236. }
  9237. return currentChunk;
  9238. };
  9239. _proto.logOnce = function logOnce(msg) {
  9240. if (!this.logEnabled) {
  9241. return;
  9242. }
  9243. logger.log("[decrypter]: " + msg);
  9244. this.logEnabled = false;
  9245. };
  9246. return Decrypter;
  9247. }();
  9248. /**
  9249. * TimeRanges to string helper
  9250. */
  9251. var TimeRanges = {
  9252. toString: function toString(r) {
  9253. var log = '';
  9254. var len = r.length;
  9255. for (var i = 0; i < len; i++) {
  9256. log += "[" + r.start(i).toFixed(3) + "-" + r.end(i).toFixed(3) + "]";
  9257. }
  9258. return log;
  9259. }
  9260. };
  9261. var State = {
  9262. STOPPED: 'STOPPED',
  9263. IDLE: 'IDLE',
  9264. KEY_LOADING: 'KEY_LOADING',
  9265. FRAG_LOADING: 'FRAG_LOADING',
  9266. FRAG_LOADING_WAITING_RETRY: 'FRAG_LOADING_WAITING_RETRY',
  9267. WAITING_TRACK: 'WAITING_TRACK',
  9268. PARSING: 'PARSING',
  9269. PARSED: 'PARSED',
  9270. ENDED: 'ENDED',
  9271. ERROR: 'ERROR',
  9272. WAITING_INIT_PTS: 'WAITING_INIT_PTS',
  9273. WAITING_LEVEL: 'WAITING_LEVEL'
  9274. };
  9275. var BaseStreamController = /*#__PURE__*/function (_TaskLoop) {
  9276. _inheritsLoose(BaseStreamController, _TaskLoop);
  9277. function BaseStreamController(hls, fragmentTracker, keyLoader, logPrefix, playlistType) {
  9278. var _this;
  9279. _this = _TaskLoop.call(this) || this;
  9280. _this.hls = void 0;
  9281. _this.fragPrevious = null;
  9282. _this.fragCurrent = null;
  9283. _this.fragmentTracker = void 0;
  9284. _this.transmuxer = null;
  9285. _this._state = State.STOPPED;
  9286. _this.playlistType = void 0;
  9287. _this.media = null;
  9288. _this.mediaBuffer = null;
  9289. _this.config = void 0;
  9290. _this.bitrateTest = false;
  9291. _this.lastCurrentTime = 0;
  9292. _this.nextLoadPosition = 0;
  9293. _this.startPosition = 0;
  9294. _this.startTimeOffset = null;
  9295. _this.loadedmetadata = false;
  9296. _this.retryDate = 0;
  9297. _this.levels = null;
  9298. _this.fragmentLoader = void 0;
  9299. _this.keyLoader = void 0;
  9300. _this.levelLastLoaded = null;
  9301. _this.startFragRequested = false;
  9302. _this.decrypter = void 0;
  9303. _this.initPTS = [];
  9304. _this.onvseeking = null;
  9305. _this.onvended = null;
  9306. _this.logPrefix = '';
  9307. _this.log = void 0;
  9308. _this.warn = void 0;
  9309. _this.playlistType = playlistType;
  9310. _this.logPrefix = logPrefix;
  9311. _this.log = logger.log.bind(logger, logPrefix + ":");
  9312. _this.warn = logger.warn.bind(logger, logPrefix + ":");
  9313. _this.hls = hls;
  9314. _this.fragmentLoader = new FragmentLoader(hls.config);
  9315. _this.keyLoader = keyLoader;
  9316. _this.fragmentTracker = fragmentTracker;
  9317. _this.config = hls.config;
  9318. _this.decrypter = new Decrypter(hls.config);
  9319. hls.on(Events.MANIFEST_LOADED, _this.onManifestLoaded, _assertThisInitialized(_this));
  9320. return _this;
  9321. }
  9322. var _proto = BaseStreamController.prototype;
  9323. _proto.doTick = function doTick() {
  9324. this.onTickEnd();
  9325. };
  9326. _proto.onTickEnd = function onTickEnd() {}
  9327. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  9328. ;
  9329. _proto.startLoad = function startLoad(startPosition) {};
  9330. _proto.stopLoad = function stopLoad() {
  9331. this.fragmentLoader.abort();
  9332. this.keyLoader.abort(this.playlistType);
  9333. var frag = this.fragCurrent;
  9334. if (frag != null && frag.loader) {
  9335. frag.abortRequests();
  9336. this.fragmentTracker.removeFragment(frag);
  9337. }
  9338. this.resetTransmuxer();
  9339. this.fragCurrent = null;
  9340. this.fragPrevious = null;
  9341. this.clearInterval();
  9342. this.clearNextTick();
  9343. this.state = State.STOPPED;
  9344. };
  9345. _proto._streamEnded = function _streamEnded(bufferInfo, levelDetails) {
  9346. // If playlist is live, there is another buffered range after the current range, nothing buffered, media is detached,
  9347. // of nothing loading/loaded return false
  9348. if (levelDetails.live || bufferInfo.nextStart || !bufferInfo.end || !this.media) {
  9349. return false;
  9350. }
  9351. var partList = levelDetails.partList;
  9352. // Since the last part isn't guaranteed to correspond to the last playlist segment for Low-Latency HLS,
  9353. // check instead if the last part is buffered.
  9354. if (partList != null && partList.length) {
  9355. var lastPart = partList[partList.length - 1];
  9356. // Checking the midpoint of the part for potential margin of error and related issues.
  9357. // NOTE: Technically I believe parts could yield content that is < the computed duration (including potential a duration of 0)
  9358. // and still be spec-compliant, so there may still be edge cases here. Likewise, there could be issues in end of stream
  9359. // part mismatches for independent audio and video playlists/segments.
  9360. var lastPartBuffered = BufferHelper.isBuffered(this.media, lastPart.start + lastPart.duration / 2);
  9361. return lastPartBuffered;
  9362. }
  9363. var playlistType = levelDetails.fragments[levelDetails.fragments.length - 1].type;
  9364. return this.fragmentTracker.isEndListAppended(playlistType);
  9365. };
  9366. _proto.getLevelDetails = function getLevelDetails() {
  9367. if (this.levels && this.levelLastLoaded !== null) {
  9368. var _this$levelLastLoaded;
  9369. return (_this$levelLastLoaded = this.levelLastLoaded) == null ? void 0 : _this$levelLastLoaded.details;
  9370. }
  9371. };
  9372. _proto.onMediaAttached = function onMediaAttached(event, data) {
  9373. var media = this.media = this.mediaBuffer = data.media;
  9374. this.onvseeking = this.onMediaSeeking.bind(this);
  9375. this.onvended = this.onMediaEnded.bind(this);
  9376. media.addEventListener('seeking', this.onvseeking);
  9377. media.addEventListener('ended', this.onvended);
  9378. var config = this.config;
  9379. if (this.levels && config.autoStartLoad && this.state === State.STOPPED) {
  9380. this.startLoad(config.startPosition);
  9381. }
  9382. };
  9383. _proto.onMediaDetaching = function onMediaDetaching() {
  9384. var media = this.media;
  9385. if (media != null && media.ended) {
  9386. this.log('MSE detaching and video ended, reset startPosition');
  9387. this.startPosition = this.lastCurrentTime = 0;
  9388. }
  9389. // remove video listeners
  9390. if (media && this.onvseeking && this.onvended) {
  9391. media.removeEventListener('seeking', this.onvseeking);
  9392. media.removeEventListener('ended', this.onvended);
  9393. this.onvseeking = this.onvended = null;
  9394. }
  9395. if (this.keyLoader) {
  9396. this.keyLoader.detach();
  9397. }
  9398. this.media = this.mediaBuffer = null;
  9399. this.loadedmetadata = false;
  9400. this.fragmentTracker.removeAllFragments();
  9401. this.stopLoad();
  9402. };
  9403. _proto.onMediaSeeking = function onMediaSeeking() {
  9404. var config = this.config,
  9405. fragCurrent = this.fragCurrent,
  9406. media = this.media,
  9407. mediaBuffer = this.mediaBuffer,
  9408. state = this.state;
  9409. var currentTime = media ? media.currentTime : 0;
  9410. var bufferInfo = BufferHelper.bufferInfo(mediaBuffer ? mediaBuffer : media, currentTime, config.maxBufferHole);
  9411. this.log("media seeking to " + (isFiniteNumber(currentTime) ? currentTime.toFixed(3) : currentTime) + ", state: " + state);
  9412. if (this.state === State.ENDED) {
  9413. this.resetLoadingState();
  9414. } else if (fragCurrent) {
  9415. // Seeking while frag load is in progress
  9416. var tolerance = config.maxFragLookUpTolerance;
  9417. var fragStartOffset = fragCurrent.start - tolerance;
  9418. var fragEndOffset = fragCurrent.start + fragCurrent.duration + tolerance;
  9419. // if seeking out of buffered range or into new one
  9420. if (!bufferInfo.len || fragEndOffset < bufferInfo.start || fragStartOffset > bufferInfo.end) {
  9421. var pastFragment = currentTime > fragEndOffset;
  9422. // if the seek position is outside the current fragment range
  9423. if (currentTime < fragStartOffset || pastFragment) {
  9424. if (pastFragment && fragCurrent.loader) {
  9425. this.log('seeking outside of buffer while fragment load in progress, cancel fragment load');
  9426. fragCurrent.abortRequests();
  9427. this.resetLoadingState();
  9428. }
  9429. this.fragPrevious = null;
  9430. }
  9431. }
  9432. }
  9433. if (media) {
  9434. // Remove gap fragments
  9435. this.fragmentTracker.removeFragmentsInRange(currentTime, Infinity, this.playlistType, true);
  9436. this.lastCurrentTime = currentTime;
  9437. }
  9438. // in case seeking occurs although no media buffered, adjust startPosition and nextLoadPosition to seek target
  9439. if (!this.loadedmetadata && !bufferInfo.len) {
  9440. this.nextLoadPosition = this.startPosition = currentTime;
  9441. }
  9442. // Async tick to speed up processing
  9443. this.tickImmediate();
  9444. };
  9445. _proto.onMediaEnded = function onMediaEnded() {
  9446. // reset startPosition and lastCurrentTime to restart playback @ stream beginning
  9447. this.startPosition = this.lastCurrentTime = 0;
  9448. };
  9449. _proto.onManifestLoaded = function onManifestLoaded(event, data) {
  9450. this.startTimeOffset = data.startTimeOffset;
  9451. this.initPTS = [];
  9452. };
  9453. _proto.onHandlerDestroying = function onHandlerDestroying() {
  9454. this.hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  9455. this.stopLoad();
  9456. _TaskLoop.prototype.onHandlerDestroying.call(this);
  9457. // @ts-ignore
  9458. this.hls = null;
  9459. };
  9460. _proto.onHandlerDestroyed = function onHandlerDestroyed() {
  9461. this.state = State.STOPPED;
  9462. if (this.fragmentLoader) {
  9463. this.fragmentLoader.destroy();
  9464. }
  9465. if (this.keyLoader) {
  9466. this.keyLoader.destroy();
  9467. }
  9468. if (this.decrypter) {
  9469. this.decrypter.destroy();
  9470. }
  9471. this.hls = this.log = this.warn = this.decrypter = this.keyLoader = this.fragmentLoader = this.fragmentTracker = null;
  9472. _TaskLoop.prototype.onHandlerDestroyed.call(this);
  9473. };
  9474. _proto.loadFragment = function loadFragment(frag, level, targetBufferTime) {
  9475. this._loadFragForPlayback(frag, level, targetBufferTime);
  9476. };
  9477. _proto._loadFragForPlayback = function _loadFragForPlayback(frag, level, targetBufferTime) {
  9478. var _this2 = this;
  9479. var progressCallback = function progressCallback(data) {
  9480. if (_this2.fragContextChanged(frag)) {
  9481. _this2.warn("Fragment " + frag.sn + (data.part ? ' p: ' + data.part.index : '') + " of level " + frag.level + " was dropped during download.");
  9482. _this2.fragmentTracker.removeFragment(frag);
  9483. return;
  9484. }
  9485. frag.stats.chunkCount++;
  9486. _this2._handleFragmentLoadProgress(data);
  9487. };
  9488. this._doFragLoad(frag, level, targetBufferTime, progressCallback).then(function (data) {
  9489. if (!data) {
  9490. // if we're here we probably needed to backtrack or are waiting for more parts
  9491. return;
  9492. }
  9493. var state = _this2.state;
  9494. if (_this2.fragContextChanged(frag)) {
  9495. if (state === State.FRAG_LOADING || !_this2.fragCurrent && state === State.PARSING) {
  9496. _this2.fragmentTracker.removeFragment(frag);
  9497. _this2.state = State.IDLE;
  9498. }
  9499. return;
  9500. }
  9501. if ('payload' in data) {
  9502. _this2.log("Loaded fragment " + frag.sn + " of level " + frag.level);
  9503. _this2.hls.trigger(Events.FRAG_LOADED, data);
  9504. }
  9505. // Pass through the whole payload; controllers not implementing progressive loading receive data from this callback
  9506. _this2._handleFragmentLoadComplete(data);
  9507. }).catch(function (reason) {
  9508. if (_this2.state === State.STOPPED || _this2.state === State.ERROR) {
  9509. return;
  9510. }
  9511. _this2.warn("Frag error: " + ((reason == null ? void 0 : reason.message) || reason));
  9512. _this2.resetFragmentLoading(frag);
  9513. });
  9514. };
  9515. _proto.clearTrackerIfNeeded = function clearTrackerIfNeeded(frag) {
  9516. var _this$mediaBuffer;
  9517. var fragmentTracker = this.fragmentTracker;
  9518. var fragState = fragmentTracker.getState(frag);
  9519. if (fragState === FragmentState.APPENDING) {
  9520. // Lower the max buffer length and try again
  9521. var playlistType = frag.type;
  9522. var bufferedInfo = this.getFwdBufferInfo(this.mediaBuffer, playlistType);
  9523. var minForwardBufferLength = Math.max(frag.duration, bufferedInfo ? bufferedInfo.len : this.config.maxBufferLength);
  9524. // If backtracking, always remove from the tracker without reducing max buffer length
  9525. var backtrackFragment = this.backtrackFragment;
  9526. var backtracked = backtrackFragment ? frag.sn - backtrackFragment.sn : 0;
  9527. if (backtracked === 1 || this.reduceMaxBufferLength(minForwardBufferLength)) {
  9528. fragmentTracker.removeFragment(frag);
  9529. }
  9530. } else if (((_this$mediaBuffer = this.mediaBuffer) == null ? void 0 : _this$mediaBuffer.buffered.length) === 0) {
  9531. // Stop gap for bad tracker / buffer flush behavior
  9532. fragmentTracker.removeAllFragments();
  9533. } else if (fragmentTracker.hasParts(frag.type)) {
  9534. // In low latency mode, remove fragments for which only some parts were buffered
  9535. fragmentTracker.detectPartialFragments({
  9536. frag: frag,
  9537. part: null,
  9538. stats: frag.stats,
  9539. id: frag.type
  9540. });
  9541. if (fragmentTracker.getState(frag) === FragmentState.PARTIAL) {
  9542. fragmentTracker.removeFragment(frag);
  9543. }
  9544. }
  9545. };
  9546. _proto.checkLiveUpdate = function checkLiveUpdate(details) {
  9547. if (details.updated && !details.live) {
  9548. // Live stream ended, update fragment tracker
  9549. var lastFragment = details.fragments[details.fragments.length - 1];
  9550. this.fragmentTracker.detectPartialFragments({
  9551. frag: lastFragment,
  9552. part: null,
  9553. stats: lastFragment.stats,
  9554. id: lastFragment.type
  9555. });
  9556. }
  9557. if (!details.fragments[0]) {
  9558. details.deltaUpdateFailed = true;
  9559. }
  9560. };
  9561. _proto.flushMainBuffer = function flushMainBuffer(startOffset, endOffset, type) {
  9562. if (type === void 0) {
  9563. type = null;
  9564. }
  9565. if (!(startOffset - endOffset)) {
  9566. return;
  9567. }
  9568. // When alternate audio is playing, the audio-stream-controller is responsible for the audio buffer. Otherwise,
  9569. // passing a null type flushes both buffers
  9570. var flushScope = {
  9571. startOffset: startOffset,
  9572. endOffset: endOffset,
  9573. type: type
  9574. };
  9575. this.hls.trigger(Events.BUFFER_FLUSHING, flushScope);
  9576. };
  9577. _proto._loadInitSegment = function _loadInitSegment(frag, level) {
  9578. var _this3 = this;
  9579. this._doFragLoad(frag, level).then(function (data) {
  9580. if (!data || _this3.fragContextChanged(frag) || !_this3.levels) {
  9581. throw new Error('init load aborted');
  9582. }
  9583. return data;
  9584. }).then(function (data) {
  9585. var hls = _this3.hls;
  9586. var payload = data.payload;
  9587. var decryptData = frag.decryptdata;
  9588. // check to see if the payload needs to be decrypted
  9589. if (payload && payload.byteLength > 0 && decryptData != null && decryptData.key && decryptData.iv && decryptData.method === 'AES-128') {
  9590. var startTime = self.performance.now();
  9591. // decrypt init segment data
  9592. return _this3.decrypter.decrypt(new Uint8Array(payload), decryptData.key.buffer, decryptData.iv.buffer).catch(function (err) {
  9593. hls.trigger(Events.ERROR, {
  9594. type: ErrorTypes.MEDIA_ERROR,
  9595. details: ErrorDetails.FRAG_DECRYPT_ERROR,
  9596. fatal: false,
  9597. error: err,
  9598. reason: err.message,
  9599. frag: frag
  9600. });
  9601. throw err;
  9602. }).then(function (decryptedData) {
  9603. var endTime = self.performance.now();
  9604. hls.trigger(Events.FRAG_DECRYPTED, {
  9605. frag: frag,
  9606. payload: decryptedData,
  9607. stats: {
  9608. tstart: startTime,
  9609. tdecrypt: endTime
  9610. }
  9611. });
  9612. data.payload = decryptedData;
  9613. return _this3.completeInitSegmentLoad(data);
  9614. });
  9615. }
  9616. return _this3.completeInitSegmentLoad(data);
  9617. }).catch(function (reason) {
  9618. if (_this3.state === State.STOPPED || _this3.state === State.ERROR) {
  9619. return;
  9620. }
  9621. _this3.warn(reason);
  9622. _this3.resetFragmentLoading(frag);
  9623. });
  9624. };
  9625. _proto.completeInitSegmentLoad = function completeInitSegmentLoad(data) {
  9626. var levels = this.levels;
  9627. if (!levels) {
  9628. throw new Error('init load aborted, missing levels');
  9629. }
  9630. var stats = data.frag.stats;
  9631. this.state = State.IDLE;
  9632. data.frag.data = new Uint8Array(data.payload);
  9633. stats.parsing.start = stats.buffering.start = self.performance.now();
  9634. stats.parsing.end = stats.buffering.end = self.performance.now();
  9635. this.tick();
  9636. };
  9637. _proto.fragContextChanged = function fragContextChanged(frag) {
  9638. var fragCurrent = this.fragCurrent;
  9639. return !frag || !fragCurrent || frag.sn !== fragCurrent.sn || frag.level !== fragCurrent.level;
  9640. };
  9641. _proto.fragBufferedComplete = function fragBufferedComplete(frag, part) {
  9642. var _frag$startPTS, _frag$endPTS, _this$fragCurrent, _this$fragPrevious;
  9643. var media = this.mediaBuffer ? this.mediaBuffer : this.media;
  9644. this.log("Buffered " + frag.type + " sn: " + frag.sn + (part ? ' part: ' + part.index : '') + " of " + (this.playlistType === PlaylistLevelType.MAIN ? 'level' : 'track') + " " + frag.level + " (frag:[" + ((_frag$startPTS = frag.startPTS) != null ? _frag$startPTS : NaN).toFixed(3) + "-" + ((_frag$endPTS = frag.endPTS) != null ? _frag$endPTS : NaN).toFixed(3) + "] > buffer:" + (media ? TimeRanges.toString(BufferHelper.getBuffered(media)) : '(detached)') + ")");
  9645. if (frag.sn !== 'initSegment') {
  9646. var _this$levels;
  9647. if (frag.type !== PlaylistLevelType.SUBTITLE) {
  9648. var el = frag.elementaryStreams;
  9649. if (!Object.keys(el).some(function (type) {
  9650. return !!el[type];
  9651. })) {
  9652. // empty segment
  9653. this.state = State.IDLE;
  9654. return;
  9655. }
  9656. }
  9657. var level = (_this$levels = this.levels) == null ? void 0 : _this$levels[frag.level];
  9658. if (level != null && level.fragmentError) {
  9659. this.log("Resetting level fragment error count of " + level.fragmentError + " on frag buffered");
  9660. level.fragmentError = 0;
  9661. }
  9662. }
  9663. this.state = State.IDLE;
  9664. if (!media) {
  9665. return;
  9666. }
  9667. if (!this.loadedmetadata && frag.type == PlaylistLevelType.MAIN && media.buffered.length && ((_this$fragCurrent = this.fragCurrent) == null ? void 0 : _this$fragCurrent.sn) === ((_this$fragPrevious = this.fragPrevious) == null ? void 0 : _this$fragPrevious.sn)) {
  9668. this.loadedmetadata = true;
  9669. this.seekToStartPos();
  9670. }
  9671. this.tick();
  9672. };
  9673. _proto.seekToStartPos = function seekToStartPos() {};
  9674. _proto._handleFragmentLoadComplete = function _handleFragmentLoadComplete(fragLoadedEndData) {
  9675. var transmuxer = this.transmuxer;
  9676. if (!transmuxer) {
  9677. return;
  9678. }
  9679. var frag = fragLoadedEndData.frag,
  9680. part = fragLoadedEndData.part,
  9681. partsLoaded = fragLoadedEndData.partsLoaded;
  9682. // If we did not load parts, or loaded all parts, we have complete (not partial) fragment data
  9683. var complete = !partsLoaded || partsLoaded.length === 0 || partsLoaded.some(function (fragLoaded) {
  9684. return !fragLoaded;
  9685. });
  9686. var chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount + 1, 0, part ? part.index : -1, !complete);
  9687. transmuxer.flush(chunkMeta);
  9688. }
  9689. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  9690. ;
  9691. _proto._handleFragmentLoadProgress = function _handleFragmentLoadProgress(frag) {};
  9692. _proto._doFragLoad = function _doFragLoad(frag, level, targetBufferTime, progressCallback) {
  9693. var _frag$decryptdata,
  9694. _this4 = this;
  9695. if (targetBufferTime === void 0) {
  9696. targetBufferTime = null;
  9697. }
  9698. var details = level == null ? void 0 : level.details;
  9699. if (!this.levels || !details) {
  9700. throw new Error("frag load aborted, missing level" + (details ? '' : ' detail') + "s");
  9701. }
  9702. var keyLoadingPromise = null;
  9703. if (frag.encrypted && !((_frag$decryptdata = frag.decryptdata) != null && _frag$decryptdata.key)) {
  9704. this.log("Loading key for " + frag.sn + " of [" + details.startSN + "-" + details.endSN + "], " + (this.logPrefix === '[stream-controller]' ? 'level' : 'track') + " " + frag.level);
  9705. this.state = State.KEY_LOADING;
  9706. this.fragCurrent = frag;
  9707. keyLoadingPromise = this.keyLoader.load(frag).then(function (keyLoadedData) {
  9708. if (!_this4.fragContextChanged(keyLoadedData.frag)) {
  9709. _this4.hls.trigger(Events.KEY_LOADED, keyLoadedData);
  9710. if (_this4.state === State.KEY_LOADING) {
  9711. _this4.state = State.IDLE;
  9712. }
  9713. return keyLoadedData;
  9714. }
  9715. });
  9716. this.hls.trigger(Events.KEY_LOADING, {
  9717. frag: frag
  9718. });
  9719. if (this.fragCurrent === null) {
  9720. keyLoadingPromise = Promise.reject(new Error("frag load aborted, context changed in KEY_LOADING"));
  9721. }
  9722. } else if (!frag.encrypted && details.encryptedFragments.length) {
  9723. this.keyLoader.loadClear(frag, details.encryptedFragments);
  9724. }
  9725. targetBufferTime = Math.max(frag.start, targetBufferTime || 0);
  9726. if (this.config.lowLatencyMode && frag.sn !== 'initSegment') {
  9727. var partList = details.partList;
  9728. if (partList && progressCallback) {
  9729. if (targetBufferTime > frag.end && details.fragmentHint) {
  9730. frag = details.fragmentHint;
  9731. }
  9732. var partIndex = this.getNextPart(partList, frag, targetBufferTime);
  9733. if (partIndex > -1) {
  9734. var part = partList[partIndex];
  9735. this.log("Loading part sn: " + frag.sn + " p: " + part.index + " cc: " + frag.cc + " of playlist [" + details.startSN + "-" + details.endSN + "] parts [0-" + partIndex + "-" + (partList.length - 1) + "] " + (this.logPrefix === '[stream-controller]' ? 'level' : 'track') + ": " + frag.level + ", target: " + parseFloat(targetBufferTime.toFixed(3)));
  9736. this.nextLoadPosition = part.start + part.duration;
  9737. this.state = State.FRAG_LOADING;
  9738. var _result;
  9739. if (keyLoadingPromise) {
  9740. _result = keyLoadingPromise.then(function (keyLoadedData) {
  9741. if (!keyLoadedData || _this4.fragContextChanged(keyLoadedData.frag)) {
  9742. return null;
  9743. }
  9744. return _this4.doFragPartsLoad(frag, part, level, progressCallback);
  9745. }).catch(function (error) {
  9746. return _this4.handleFragLoadError(error);
  9747. });
  9748. } else {
  9749. _result = this.doFragPartsLoad(frag, part, level, progressCallback).catch(function (error) {
  9750. return _this4.handleFragLoadError(error);
  9751. });
  9752. }
  9753. this.hls.trigger(Events.FRAG_LOADING, {
  9754. frag: frag,
  9755. part: part,
  9756. targetBufferTime: targetBufferTime
  9757. });
  9758. if (this.fragCurrent === null) {
  9759. return Promise.reject(new Error("frag load aborted, context changed in FRAG_LOADING parts"));
  9760. }
  9761. return _result;
  9762. } else if (!frag.url || this.loadedEndOfParts(partList, targetBufferTime)) {
  9763. // Fragment hint has no parts
  9764. return Promise.resolve(null);
  9765. }
  9766. }
  9767. }
  9768. this.log("Loading fragment " + frag.sn + " cc: " + frag.cc + " " + (details ? 'of [' + details.startSN + '-' + details.endSN + '] ' : '') + (this.logPrefix === '[stream-controller]' ? 'level' : 'track') + ": " + frag.level + ", target: " + parseFloat(targetBufferTime.toFixed(3)));
  9769. // Don't update nextLoadPosition for fragments which are not buffered
  9770. if (isFiniteNumber(frag.sn) && !this.bitrateTest) {
  9771. this.nextLoadPosition = frag.start + frag.duration;
  9772. }
  9773. this.state = State.FRAG_LOADING;
  9774. // Load key before streaming fragment data
  9775. var dataOnProgress = this.config.progressive;
  9776. var result;
  9777. if (dataOnProgress && keyLoadingPromise) {
  9778. result = keyLoadingPromise.then(function (keyLoadedData) {
  9779. if (!keyLoadedData || _this4.fragContextChanged(keyLoadedData == null ? void 0 : keyLoadedData.frag)) {
  9780. return null;
  9781. }
  9782. return _this4.fragmentLoader.load(frag, progressCallback);
  9783. }).catch(function (error) {
  9784. return _this4.handleFragLoadError(error);
  9785. });
  9786. } else {
  9787. // load unencrypted fragment data with progress event,
  9788. // or handle fragment result after key and fragment are finished loading
  9789. result = Promise.all([this.fragmentLoader.load(frag, dataOnProgress ? progressCallback : undefined), keyLoadingPromise]).then(function (_ref) {
  9790. var fragLoadedData = _ref[0];
  9791. if (!dataOnProgress && fragLoadedData && progressCallback) {
  9792. progressCallback(fragLoadedData);
  9793. }
  9794. return fragLoadedData;
  9795. }).catch(function (error) {
  9796. return _this4.handleFragLoadError(error);
  9797. });
  9798. }
  9799. this.hls.trigger(Events.FRAG_LOADING, {
  9800. frag: frag,
  9801. targetBufferTime: targetBufferTime
  9802. });
  9803. if (this.fragCurrent === null) {
  9804. return Promise.reject(new Error("frag load aborted, context changed in FRAG_LOADING"));
  9805. }
  9806. return result;
  9807. };
  9808. _proto.doFragPartsLoad = function doFragPartsLoad(frag, fromPart, level, progressCallback) {
  9809. var _this5 = this;
  9810. return new Promise(function (resolve, reject) {
  9811. var _level$details;
  9812. var partsLoaded = [];
  9813. var initialPartList = (_level$details = level.details) == null ? void 0 : _level$details.partList;
  9814. var loadPart = function loadPart(part) {
  9815. _this5.fragmentLoader.loadPart(frag, part, progressCallback).then(function (partLoadedData) {
  9816. partsLoaded[part.index] = partLoadedData;
  9817. var loadedPart = partLoadedData.part;
  9818. _this5.hls.trigger(Events.FRAG_LOADED, partLoadedData);
  9819. var nextPart = getPartWith(level, frag.sn, part.index + 1) || findPart(initialPartList, frag.sn, part.index + 1);
  9820. if (nextPart) {
  9821. loadPart(nextPart);
  9822. } else {
  9823. return resolve({
  9824. frag: frag,
  9825. part: loadedPart,
  9826. partsLoaded: partsLoaded
  9827. });
  9828. }
  9829. }).catch(reject);
  9830. };
  9831. loadPart(fromPart);
  9832. });
  9833. };
  9834. _proto.handleFragLoadError = function handleFragLoadError(error) {
  9835. if ('data' in error) {
  9836. var data = error.data;
  9837. if (error.data && data.details === ErrorDetails.INTERNAL_ABORTED) {
  9838. this.handleFragLoadAborted(data.frag, data.part);
  9839. } else {
  9840. this.hls.trigger(Events.ERROR, data);
  9841. }
  9842. } else {
  9843. this.hls.trigger(Events.ERROR, {
  9844. type: ErrorTypes.OTHER_ERROR,
  9845. details: ErrorDetails.INTERNAL_EXCEPTION,
  9846. err: error,
  9847. error: error,
  9848. fatal: true
  9849. });
  9850. }
  9851. return null;
  9852. };
  9853. _proto._handleTransmuxerFlush = function _handleTransmuxerFlush(chunkMeta) {
  9854. var context = this.getCurrentContext(chunkMeta);
  9855. if (!context || this.state !== State.PARSING) {
  9856. if (!this.fragCurrent && this.state !== State.STOPPED && this.state !== State.ERROR) {
  9857. this.state = State.IDLE;
  9858. }
  9859. return;
  9860. }
  9861. var frag = context.frag,
  9862. part = context.part,
  9863. level = context.level;
  9864. var now = self.performance.now();
  9865. frag.stats.parsing.end = now;
  9866. if (part) {
  9867. part.stats.parsing.end = now;
  9868. }
  9869. this.updateLevelTiming(frag, part, level, chunkMeta.partial);
  9870. };
  9871. _proto.getCurrentContext = function getCurrentContext(chunkMeta) {
  9872. var levels = this.levels,
  9873. fragCurrent = this.fragCurrent;
  9874. var levelIndex = chunkMeta.level,
  9875. sn = chunkMeta.sn,
  9876. partIndex = chunkMeta.part;
  9877. if (!(levels != null && levels[levelIndex])) {
  9878. this.warn("Levels object was unset while buffering fragment " + sn + " of level " + levelIndex + ". The current chunk will not be buffered.");
  9879. return null;
  9880. }
  9881. var level = levels[levelIndex];
  9882. var part = partIndex > -1 ? getPartWith(level, sn, partIndex) : null;
  9883. var frag = part ? part.fragment : getFragmentWithSN(level, sn, fragCurrent);
  9884. if (!frag) {
  9885. return null;
  9886. }
  9887. if (fragCurrent && fragCurrent !== frag) {
  9888. frag.stats = fragCurrent.stats;
  9889. }
  9890. return {
  9891. frag: frag,
  9892. part: part,
  9893. level: level
  9894. };
  9895. };
  9896. _proto.bufferFragmentData = function bufferFragmentData(data, frag, part, chunkMeta, noBacktracking) {
  9897. var _buffer;
  9898. if (!data || this.state !== State.PARSING) {
  9899. return;
  9900. }
  9901. var data1 = data.data1,
  9902. data2 = data.data2;
  9903. var buffer = data1;
  9904. if (data1 && data2) {
  9905. // Combine the moof + mdat so that we buffer with a single append
  9906. buffer = appendUint8Array(data1, data2);
  9907. }
  9908. if (!((_buffer = buffer) != null && _buffer.length)) {
  9909. return;
  9910. }
  9911. var segment = {
  9912. type: data.type,
  9913. frag: frag,
  9914. part: part,
  9915. chunkMeta: chunkMeta,
  9916. parent: frag.type,
  9917. data: buffer
  9918. };
  9919. this.hls.trigger(Events.BUFFER_APPENDING, segment);
  9920. if (data.dropped && data.independent && !part) {
  9921. if (noBacktracking) {
  9922. return;
  9923. }
  9924. // Clear buffer so that we reload previous segments sequentially if required
  9925. this.flushBufferGap(frag);
  9926. }
  9927. };
  9928. _proto.flushBufferGap = function flushBufferGap(frag) {
  9929. var media = this.media;
  9930. if (!media) {
  9931. return;
  9932. }
  9933. // If currentTime is not buffered, clear the back buffer so that we can backtrack as much as needed
  9934. if (!BufferHelper.isBuffered(media, media.currentTime)) {
  9935. this.flushMainBuffer(0, frag.start);
  9936. return;
  9937. }
  9938. // Remove back-buffer without interrupting playback to allow back tracking
  9939. var currentTime = media.currentTime;
  9940. var bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
  9941. var fragDuration = frag.duration;
  9942. var segmentFraction = Math.min(this.config.maxFragLookUpTolerance * 2, fragDuration * 0.25);
  9943. var start = Math.max(Math.min(frag.start - segmentFraction, bufferInfo.end - segmentFraction), currentTime + segmentFraction);
  9944. if (frag.start - start > segmentFraction) {
  9945. this.flushMainBuffer(start, frag.start);
  9946. }
  9947. };
  9948. _proto.getFwdBufferInfo = function getFwdBufferInfo(bufferable, type) {
  9949. var pos = this.getLoadPosition();
  9950. if (!isFiniteNumber(pos)) {
  9951. return null;
  9952. }
  9953. return this.getFwdBufferInfoAtPos(bufferable, pos, type);
  9954. };
  9955. _proto.getFwdBufferInfoAtPos = function getFwdBufferInfoAtPos(bufferable, pos, type) {
  9956. var maxBufferHole = this.config.maxBufferHole;
  9957. var bufferInfo = BufferHelper.bufferInfo(bufferable, pos, maxBufferHole);
  9958. // Workaround flaw in getting forward buffer when maxBufferHole is smaller than gap at current pos
  9959. if (bufferInfo.len === 0 && bufferInfo.nextStart !== undefined) {
  9960. var bufferedFragAtPos = this.fragmentTracker.getBufferedFrag(pos, type);
  9961. if (bufferedFragAtPos && bufferInfo.nextStart < bufferedFragAtPos.end) {
  9962. return BufferHelper.bufferInfo(bufferable, pos, Math.max(bufferInfo.nextStart, maxBufferHole));
  9963. }
  9964. }
  9965. return bufferInfo;
  9966. };
  9967. _proto.getMaxBufferLength = function getMaxBufferLength(levelBitrate) {
  9968. var config = this.config;
  9969. var maxBufLen;
  9970. if (levelBitrate) {
  9971. maxBufLen = Math.max(8 * config.maxBufferSize / levelBitrate, config.maxBufferLength);
  9972. } else {
  9973. maxBufLen = config.maxBufferLength;
  9974. }
  9975. return Math.min(maxBufLen, config.maxMaxBufferLength);
  9976. };
  9977. _proto.reduceMaxBufferLength = function reduceMaxBufferLength(threshold) {
  9978. var config = this.config;
  9979. var minLength = threshold || config.maxBufferLength;
  9980. var reducedLength = config.maxMaxBufferLength / 2;
  9981. if (reducedLength >= minLength) {
  9982. // reduce max buffer length as it might be too high. we do this to avoid loop flushing ...
  9983. config.maxMaxBufferLength = reducedLength;
  9984. this.warn("Reduce max buffer length to " + reducedLength + "s");
  9985. return true;
  9986. }
  9987. return false;
  9988. };
  9989. _proto.getAppendedFrag = function getAppendedFrag(position, playlistType) {
  9990. var fragOrPart = this.fragmentTracker.getAppendedFrag(position, PlaylistLevelType.MAIN);
  9991. if (fragOrPart && 'fragment' in fragOrPart) {
  9992. return fragOrPart.fragment;
  9993. }
  9994. return fragOrPart;
  9995. };
  9996. _proto.getNextFragment = function getNextFragment(pos, levelDetails) {
  9997. var fragments = levelDetails.fragments;
  9998. var fragLen = fragments.length;
  9999. if (!fragLen) {
  10000. return null;
  10001. }
  10002. // find fragment index, contiguous with end of buffer position
  10003. var config = this.config;
  10004. var start = fragments[0].start;
  10005. var frag;
  10006. if (levelDetails.live) {
  10007. var initialLiveManifestSize = config.initialLiveManifestSize;
  10008. if (fragLen < initialLiveManifestSize) {
  10009. this.warn("Not enough fragments to start playback (have: " + fragLen + ", need: " + initialLiveManifestSize + ")");
  10010. return null;
  10011. }
  10012. // The real fragment start times for a live stream are only known after the PTS range for that level is known.
  10013. // In order to discover the range, we load the best matching fragment for that level and demux it.
  10014. // Do not load using live logic if the starting frag is requested - we want to use getFragmentAtPosition() so that
  10015. // we get the fragment matching that start time
  10016. if (!levelDetails.PTSKnown && !this.startFragRequested && this.startPosition === -1 || pos < start) {
  10017. frag = this.getInitialLiveFragment(levelDetails, fragments);
  10018. this.startPosition = this.nextLoadPosition = frag ? this.hls.liveSyncPosition || frag.start : pos;
  10019. }
  10020. } else if (pos <= start) {
  10021. // VoD playlist: if loadPosition before start of playlist, load first fragment
  10022. frag = fragments[0];
  10023. }
  10024. // If we haven't run into any special cases already, just load the fragment most closely matching the requested position
  10025. if (!frag) {
  10026. var end = config.lowLatencyMode ? levelDetails.partEnd : levelDetails.fragmentEnd;
  10027. frag = this.getFragmentAtPosition(pos, end, levelDetails);
  10028. }
  10029. return this.mapToInitFragWhenRequired(frag);
  10030. };
  10031. _proto.isLoopLoading = function isLoopLoading(frag, targetBufferTime) {
  10032. var trackerState = this.fragmentTracker.getState(frag);
  10033. return (trackerState === FragmentState.OK || trackerState === FragmentState.PARTIAL && !!frag.gap) && this.nextLoadPosition > targetBufferTime;
  10034. };
  10035. _proto.getNextFragmentLoopLoading = function getNextFragmentLoopLoading(frag, levelDetails, bufferInfo, playlistType, maxBufLen) {
  10036. var gapStart = frag.gap;
  10037. var nextFragment = this.getNextFragment(this.nextLoadPosition, levelDetails);
  10038. if (nextFragment === null) {
  10039. return nextFragment;
  10040. }
  10041. frag = nextFragment;
  10042. if (gapStart && frag && !frag.gap && bufferInfo.nextStart) {
  10043. // Media buffered after GAP tags should not make the next buffer timerange exceed forward buffer length
  10044. var nextbufferInfo = this.getFwdBufferInfoAtPos(this.mediaBuffer ? this.mediaBuffer : this.media, bufferInfo.nextStart, playlistType);
  10045. if (nextbufferInfo !== null && bufferInfo.len + nextbufferInfo.len >= maxBufLen) {
  10046. // Returning here might result in not finding an audio and video candiate to skip to
  10047. this.log("buffer full after gaps in \"" + playlistType + "\" playlist starting at sn: " + frag.sn);
  10048. return null;
  10049. }
  10050. }
  10051. return frag;
  10052. };
  10053. _proto.mapToInitFragWhenRequired = function mapToInitFragWhenRequired(frag) {
  10054. // If an initSegment is present, it must be buffered first
  10055. if (frag != null && frag.initSegment && !(frag != null && frag.initSegment.data) && !this.bitrateTest) {
  10056. return frag.initSegment;
  10057. }
  10058. return frag;
  10059. };
  10060. _proto.getNextPart = function getNextPart(partList, frag, targetBufferTime) {
  10061. var nextPart = -1;
  10062. var contiguous = false;
  10063. var independentAttrOmitted = true;
  10064. for (var i = 0, len = partList.length; i < len; i++) {
  10065. var part = partList[i];
  10066. independentAttrOmitted = independentAttrOmitted && !part.independent;
  10067. if (nextPart > -1 && targetBufferTime < part.start) {
  10068. break;
  10069. }
  10070. var loaded = part.loaded;
  10071. if (loaded) {
  10072. nextPart = -1;
  10073. } else if ((contiguous || part.independent || independentAttrOmitted) && part.fragment === frag) {
  10074. nextPart = i;
  10075. }
  10076. contiguous = loaded;
  10077. }
  10078. return nextPart;
  10079. };
  10080. _proto.loadedEndOfParts = function loadedEndOfParts(partList, targetBufferTime) {
  10081. var lastPart = partList[partList.length - 1];
  10082. return lastPart && targetBufferTime > lastPart.start && lastPart.loaded;
  10083. }
  10084. /*
  10085. This method is used find the best matching first fragment for a live playlist. This fragment is used to calculate the
  10086. "sliding" of the playlist, which is its offset from the start of playback. After sliding we can compute the real
  10087. start and end times for each fragment in the playlist (after which this method will not need to be called).
  10088. */;
  10089. _proto.getInitialLiveFragment = function getInitialLiveFragment(levelDetails, fragments) {
  10090. var fragPrevious = this.fragPrevious;
  10091. var frag = null;
  10092. if (fragPrevious) {
  10093. if (levelDetails.hasProgramDateTime) {
  10094. // Prefer using PDT, because it can be accurate enough to choose the correct fragment without knowing the level sliding
  10095. this.log("Live playlist, switching playlist, load frag with same PDT: " + fragPrevious.programDateTime);
  10096. frag = findFragmentByPDT(fragments, fragPrevious.endProgramDateTime, this.config.maxFragLookUpTolerance);
  10097. }
  10098. if (!frag) {
  10099. // SN does not need to be accurate between renditions, but depending on the packaging it may be so.
  10100. var targetSN = fragPrevious.sn + 1;
  10101. if (targetSN >= levelDetails.startSN && targetSN <= levelDetails.endSN) {
  10102. var fragNext = fragments[targetSN - levelDetails.startSN];
  10103. // Ensure that we're staying within the continuity range, since PTS resets upon a new range
  10104. if (fragPrevious.cc === fragNext.cc) {
  10105. frag = fragNext;
  10106. this.log("Live playlist, switching playlist, load frag with next SN: " + frag.sn);
  10107. }
  10108. }
  10109. // It's important to stay within the continuity range if available; otherwise the fragments in the playlist
  10110. // will have the wrong start times
  10111. if (!frag) {
  10112. frag = findFragWithCC(fragments, fragPrevious.cc);
  10113. if (frag) {
  10114. this.log("Live playlist, switching playlist, load frag with same CC: " + frag.sn);
  10115. }
  10116. }
  10117. }
  10118. } else {
  10119. // Find a new start fragment when fragPrevious is null
  10120. var liveStart = this.hls.liveSyncPosition;
  10121. if (liveStart !== null) {
  10122. frag = this.getFragmentAtPosition(liveStart, this.bitrateTest ? levelDetails.fragmentEnd : levelDetails.edge, levelDetails);
  10123. }
  10124. }
  10125. return frag;
  10126. }
  10127. /*
  10128. This method finds the best matching fragment given the provided position.
  10129. */;
  10130. _proto.getFragmentAtPosition = function getFragmentAtPosition(bufferEnd, end, levelDetails) {
  10131. var config = this.config;
  10132. var fragPrevious = this.fragPrevious;
  10133. var fragments = levelDetails.fragments,
  10134. endSN = levelDetails.endSN;
  10135. var fragmentHint = levelDetails.fragmentHint;
  10136. var maxFragLookUpTolerance = config.maxFragLookUpTolerance;
  10137. var partList = levelDetails.partList;
  10138. var loadingParts = !!(config.lowLatencyMode && partList != null && partList.length && fragmentHint);
  10139. if (loadingParts && fragmentHint && !this.bitrateTest) {
  10140. // Include incomplete fragment with parts at end
  10141. fragments = fragments.concat(fragmentHint);
  10142. endSN = fragmentHint.sn;
  10143. }
  10144. var frag;
  10145. if (bufferEnd < end) {
  10146. var lookupTolerance = bufferEnd > end - maxFragLookUpTolerance ? 0 : maxFragLookUpTolerance;
  10147. // Remove the tolerance if it would put the bufferEnd past the actual end of stream
  10148. // Uses buffer and sequence number to calculate switch segment (required if using EXT-X-DISCONTINUITY-SEQUENCE)
  10149. frag = findFragmentByPTS(fragPrevious, fragments, bufferEnd, lookupTolerance);
  10150. } else {
  10151. // reach end of playlist
  10152. frag = fragments[fragments.length - 1];
  10153. }
  10154. if (frag) {
  10155. var curSNIdx = frag.sn - levelDetails.startSN;
  10156. // Move fragPrevious forward to support forcing the next fragment to load
  10157. // when the buffer catches up to a previously buffered range.
  10158. var fragState = this.fragmentTracker.getState(frag);
  10159. if (fragState === FragmentState.OK || fragState === FragmentState.PARTIAL && frag.gap) {
  10160. fragPrevious = frag;
  10161. }
  10162. if (fragPrevious && frag.sn === fragPrevious.sn && (!loadingParts || partList[0].fragment.sn > frag.sn)) {
  10163. // Force the next fragment to load if the previous one was already selected. This can occasionally happen with
  10164. // non-uniform fragment durations
  10165. var sameLevel = fragPrevious && frag.level === fragPrevious.level;
  10166. if (sameLevel) {
  10167. var nextFrag = fragments[curSNIdx + 1];
  10168. if (frag.sn < endSN && this.fragmentTracker.getState(nextFrag) !== FragmentState.OK) {
  10169. frag = nextFrag;
  10170. } else {
  10171. frag = null;
  10172. }
  10173. }
  10174. }
  10175. }
  10176. return frag;
  10177. };
  10178. _proto.synchronizeToLiveEdge = function synchronizeToLiveEdge(levelDetails) {
  10179. var config = this.config,
  10180. media = this.media;
  10181. if (!media) {
  10182. return;
  10183. }
  10184. var liveSyncPosition = this.hls.liveSyncPosition;
  10185. var currentTime = media.currentTime;
  10186. var start = levelDetails.fragments[0].start;
  10187. var end = levelDetails.edge;
  10188. var withinSlidingWindow = currentTime >= start - config.maxFragLookUpTolerance && currentTime <= end;
  10189. // Continue if we can seek forward to sync position or if current time is outside of sliding window
  10190. if (liveSyncPosition !== null && media.duration > liveSyncPosition && (currentTime < liveSyncPosition || !withinSlidingWindow)) {
  10191. // Continue if buffer is starving or if current time is behind max latency
  10192. var maxLatency = config.liveMaxLatencyDuration !== undefined ? config.liveMaxLatencyDuration : config.liveMaxLatencyDurationCount * levelDetails.targetduration;
  10193. if (!withinSlidingWindow && media.readyState < 4 || currentTime < end - maxLatency) {
  10194. if (!this.loadedmetadata) {
  10195. this.nextLoadPosition = liveSyncPosition;
  10196. }
  10197. // Only seek if ready and there is not a significant forward buffer available for playback
  10198. if (media.readyState) {
  10199. this.warn("Playback: " + currentTime.toFixed(3) + " is located too far from the end of live sliding playlist: " + end + ", reset currentTime to : " + liveSyncPosition.toFixed(3));
  10200. media.currentTime = liveSyncPosition;
  10201. }
  10202. }
  10203. }
  10204. };
  10205. _proto.alignPlaylists = function alignPlaylists(details, previousDetails, switchDetails) {
  10206. // FIXME: If not for `shouldAlignOnDiscontinuities` requiring fragPrevious.cc,
  10207. // this could all go in level-helper mergeDetails()
  10208. var length = details.fragments.length;
  10209. if (!length) {
  10210. this.warn("No fragments in live playlist");
  10211. return 0;
  10212. }
  10213. var slidingStart = details.fragments[0].start;
  10214. var firstLevelLoad = !previousDetails;
  10215. var aligned = details.alignedSliding && isFiniteNumber(slidingStart);
  10216. if (firstLevelLoad || !aligned && !slidingStart) {
  10217. var fragPrevious = this.fragPrevious;
  10218. alignStream(fragPrevious, switchDetails, details);
  10219. var alignedSlidingStart = details.fragments[0].start;
  10220. this.log("Live playlist sliding: " + alignedSlidingStart.toFixed(2) + " start-sn: " + (previousDetails ? previousDetails.startSN : 'na') + "->" + details.startSN + " prev-sn: " + (fragPrevious ? fragPrevious.sn : 'na') + " fragments: " + length);
  10221. return alignedSlidingStart;
  10222. }
  10223. return slidingStart;
  10224. };
  10225. _proto.waitForCdnTuneIn = function waitForCdnTuneIn(details) {
  10226. // Wait for Low-Latency CDN Tune-in to get an updated playlist
  10227. var advancePartLimit = 3;
  10228. return details.live && details.canBlockReload && details.partTarget && details.tuneInGoal > Math.max(details.partHoldBack, details.partTarget * advancePartLimit);
  10229. };
  10230. _proto.setStartPosition = function setStartPosition(details, sliding) {
  10231. // compute start position if set to -1. use it straight away if value is defined
  10232. var startPosition = this.startPosition;
  10233. if (startPosition < sliding) {
  10234. startPosition = -1;
  10235. }
  10236. if (startPosition === -1 || this.lastCurrentTime === -1) {
  10237. // Use Playlist EXT-X-START:TIME-OFFSET when set
  10238. // Prioritize Multivariant Playlist offset so that main, audio, and subtitle stream-controller start times match
  10239. var offsetInMultivariantPlaylist = this.startTimeOffset !== null;
  10240. var startTimeOffset = offsetInMultivariantPlaylist ? this.startTimeOffset : details.startTimeOffset;
  10241. if (startTimeOffset !== null && isFiniteNumber(startTimeOffset)) {
  10242. startPosition = sliding + startTimeOffset;
  10243. if (startTimeOffset < 0) {
  10244. startPosition += details.totalduration;
  10245. }
  10246. startPosition = Math.min(Math.max(sliding, startPosition), sliding + details.totalduration);
  10247. this.log("Start time offset " + startTimeOffset + " found in " + (offsetInMultivariantPlaylist ? 'multivariant' : 'media') + " playlist, adjust startPosition to " + startPosition);
  10248. this.startPosition = startPosition;
  10249. } else if (details.live) {
  10250. // Leave this.startPosition at -1, so that we can use `getInitialLiveFragment` logic when startPosition has
  10251. // not been specified via the config or an as an argument to startLoad (#3736).
  10252. startPosition = this.hls.liveSyncPosition || sliding;
  10253. } else {
  10254. this.startPosition = startPosition = 0;
  10255. }
  10256. this.lastCurrentTime = startPosition;
  10257. }
  10258. this.nextLoadPosition = startPosition;
  10259. };
  10260. _proto.getLoadPosition = function getLoadPosition() {
  10261. var media = this.media;
  10262. // if we have not yet loaded any fragment, start loading from start position
  10263. var pos = 0;
  10264. if (this.loadedmetadata && media) {
  10265. pos = media.currentTime;
  10266. } else if (this.nextLoadPosition) {
  10267. pos = this.nextLoadPosition;
  10268. }
  10269. return pos;
  10270. };
  10271. _proto.handleFragLoadAborted = function handleFragLoadAborted(frag, part) {
  10272. if (this.transmuxer && frag.sn !== 'initSegment' && frag.stats.aborted) {
  10273. this.warn("Fragment " + frag.sn + (part ? ' part ' + part.index : '') + " of level " + frag.level + " was aborted");
  10274. this.resetFragmentLoading(frag);
  10275. }
  10276. };
  10277. _proto.resetFragmentLoading = function resetFragmentLoading(frag) {
  10278. if (!this.fragCurrent || !this.fragContextChanged(frag) && this.state !== State.FRAG_LOADING_WAITING_RETRY) {
  10279. this.state = State.IDLE;
  10280. }
  10281. };
  10282. _proto.onFragmentOrKeyLoadError = function onFragmentOrKeyLoadError(filterType, data) {
  10283. if (data.chunkMeta && !data.frag) {
  10284. var context = this.getCurrentContext(data.chunkMeta);
  10285. if (context) {
  10286. data.frag = context.frag;
  10287. }
  10288. }
  10289. var frag = data.frag;
  10290. // Handle frag error related to caller's filterType
  10291. if (!frag || frag.type !== filterType || !this.levels) {
  10292. return;
  10293. }
  10294. if (this.fragContextChanged(frag)) {
  10295. var _this$fragCurrent2;
  10296. this.warn("Frag load error must match current frag to retry " + frag.url + " > " + ((_this$fragCurrent2 = this.fragCurrent) == null ? void 0 : _this$fragCurrent2.url));
  10297. return;
  10298. }
  10299. var gapTagEncountered = data.details === ErrorDetails.FRAG_GAP;
  10300. if (gapTagEncountered) {
  10301. this.fragmentTracker.fragBuffered(frag, true);
  10302. }
  10303. // keep retrying until the limit will be reached
  10304. var errorAction = data.errorAction;
  10305. var _ref2 = errorAction || {},
  10306. action = _ref2.action,
  10307. _ref2$retryCount = _ref2.retryCount,
  10308. retryCount = _ref2$retryCount === void 0 ? 0 : _ref2$retryCount,
  10309. retryConfig = _ref2.retryConfig;
  10310. if (errorAction && action === NetworkErrorAction.RetryRequest && retryConfig) {
  10311. this.resetStartWhenNotLoaded(this.levelLastLoaded);
  10312. var delay = getRetryDelay(retryConfig, retryCount);
  10313. this.warn("Fragment " + frag.sn + " of " + filterType + " " + frag.level + " errored with " + data.details + ", retrying loading " + (retryCount + 1) + "/" + retryConfig.maxNumRetry + " in " + delay + "ms");
  10314. errorAction.resolved = true;
  10315. this.retryDate = self.performance.now() + delay;
  10316. this.state = State.FRAG_LOADING_WAITING_RETRY;
  10317. } else if (retryConfig && errorAction) {
  10318. this.resetFragmentErrors(filterType);
  10319. if (retryCount < retryConfig.maxNumRetry) {
  10320. // Network retry is skipped when level switch is preferred
  10321. if (!gapTagEncountered && action !== NetworkErrorAction.RemoveAlternatePermanently) {
  10322. errorAction.resolved = true;
  10323. }
  10324. } else {
  10325. logger.warn(data.details + " reached or exceeded max retry (" + retryCount + ")");
  10326. return;
  10327. }
  10328. } else if ((errorAction == null ? void 0 : errorAction.action) === NetworkErrorAction.SendAlternateToPenaltyBox) {
  10329. this.state = State.WAITING_LEVEL;
  10330. } else {
  10331. this.state = State.ERROR;
  10332. }
  10333. // Perform next async tick sooner to speed up error action resolution
  10334. this.tickImmediate();
  10335. };
  10336. _proto.reduceLengthAndFlushBuffer = function reduceLengthAndFlushBuffer(data) {
  10337. // if in appending state
  10338. if (this.state === State.PARSING || this.state === State.PARSED) {
  10339. var playlistType = data.parent;
  10340. var bufferedInfo = this.getFwdBufferInfo(this.mediaBuffer, playlistType);
  10341. // 0.5 : tolerance needed as some browsers stalls playback before reaching buffered end
  10342. // reduce max buf len if current position is buffered
  10343. var buffered = bufferedInfo && bufferedInfo.len > 0.5;
  10344. if (buffered) {
  10345. this.reduceMaxBufferLength(bufferedInfo.len);
  10346. }
  10347. var flushBuffer = !buffered;
  10348. if (flushBuffer) {
  10349. // current position is not buffered, but browser is still complaining about buffer full error
  10350. // this happens on IE/Edge, refer to https://github.com/video-dev/hls.js/pull/708
  10351. // in that case flush the whole audio buffer to recover
  10352. this.warn("Buffer full error while media.currentTime is not buffered, flush " + playlistType + " buffer");
  10353. }
  10354. if (data.frag) {
  10355. this.fragmentTracker.removeFragment(data.frag);
  10356. this.nextLoadPosition = data.frag.start;
  10357. }
  10358. this.resetLoadingState();
  10359. return flushBuffer;
  10360. }
  10361. return false;
  10362. };
  10363. _proto.resetFragmentErrors = function resetFragmentErrors(filterType) {
  10364. if (filterType === PlaylistLevelType.AUDIO) {
  10365. // Reset current fragment since audio track audio is essential and may not have a fail-over track
  10366. this.fragCurrent = null;
  10367. }
  10368. // Fragment errors that result in a level switch or redundant fail-over
  10369. // should reset the stream controller state to idle
  10370. if (!this.loadedmetadata) {
  10371. this.startFragRequested = false;
  10372. }
  10373. if (this.state !== State.STOPPED) {
  10374. this.state = State.IDLE;
  10375. }
  10376. };
  10377. _proto.afterBufferFlushed = function afterBufferFlushed(media, bufferType, playlistType) {
  10378. if (!media) {
  10379. return;
  10380. }
  10381. // After successful buffer flushing, filter flushed fragments from bufferedFrags use mediaBuffered instead of media
  10382. // (so that we will check against video.buffered ranges in case of alt audio track)
  10383. var bufferedTimeRanges = BufferHelper.getBuffered(media);
  10384. this.fragmentTracker.detectEvictedFragments(bufferType, bufferedTimeRanges, playlistType);
  10385. if (this.state === State.ENDED) {
  10386. this.resetLoadingState();
  10387. }
  10388. };
  10389. _proto.resetLoadingState = function resetLoadingState() {
  10390. this.log('Reset loading state');
  10391. this.fragCurrent = null;
  10392. this.fragPrevious = null;
  10393. this.state = State.IDLE;
  10394. };
  10395. _proto.resetStartWhenNotLoaded = function resetStartWhenNotLoaded(level) {
  10396. // if loadedmetadata is not set, it means that first frag request failed
  10397. // in that case, reset startFragRequested flag
  10398. if (!this.loadedmetadata) {
  10399. this.startFragRequested = false;
  10400. var details = level ? level.details : null;
  10401. if (details != null && details.live) {
  10402. // Update the start position and return to IDLE to recover live start
  10403. this.startPosition = -1;
  10404. this.setStartPosition(details, 0);
  10405. this.resetLoadingState();
  10406. } else {
  10407. this.nextLoadPosition = this.startPosition;
  10408. }
  10409. }
  10410. };
  10411. _proto.resetWhenMissingContext = function resetWhenMissingContext(chunkMeta) {
  10412. this.warn("The loading context changed while buffering fragment " + chunkMeta.sn + " of level " + chunkMeta.level + ". This chunk will not be buffered.");
  10413. this.removeUnbufferedFrags();
  10414. this.resetStartWhenNotLoaded(this.levelLastLoaded);
  10415. this.resetLoadingState();
  10416. };
  10417. _proto.removeUnbufferedFrags = function removeUnbufferedFrags(start) {
  10418. if (start === void 0) {
  10419. start = 0;
  10420. }
  10421. this.fragmentTracker.removeFragmentsInRange(start, Infinity, this.playlistType, false, true);
  10422. };
  10423. _proto.updateLevelTiming = function updateLevelTiming(frag, part, level, partial) {
  10424. var _this6 = this,
  10425. _this$transmuxer;
  10426. var details = level.details;
  10427. if (!details) {
  10428. this.warn('level.details undefined');
  10429. return;
  10430. }
  10431. var parsed = Object.keys(frag.elementaryStreams).reduce(function (result, type) {
  10432. var info = frag.elementaryStreams[type];
  10433. if (info) {
  10434. var parsedDuration = info.endPTS - info.startPTS;
  10435. if (parsedDuration <= 0) {
  10436. // Destroy the transmuxer after it's next time offset failed to advance because duration was <= 0.
  10437. // The new transmuxer will be configured with a time offset matching the next fragment start,
  10438. // preventing the timeline from shifting.
  10439. _this6.warn("Could not parse fragment " + frag.sn + " " + type + " duration reliably (" + parsedDuration + ")");
  10440. return result || false;
  10441. }
  10442. var drift = partial ? 0 : updateFragPTSDTS(details, frag, info.startPTS, info.endPTS, info.startDTS, info.endDTS);
  10443. _this6.hls.trigger(Events.LEVEL_PTS_UPDATED, {
  10444. details: details,
  10445. level: level,
  10446. drift: drift,
  10447. type: type,
  10448. frag: frag,
  10449. start: info.startPTS,
  10450. end: info.endPTS
  10451. });
  10452. return true;
  10453. }
  10454. return result;
  10455. }, false);
  10456. if (!parsed && ((_this$transmuxer = this.transmuxer) == null ? void 0 : _this$transmuxer.error) === null) {
  10457. var error = new Error("Found no media in fragment " + frag.sn + " of level " + frag.level + " resetting transmuxer to fallback to playlist timing");
  10458. if (level.fragmentError === 0) {
  10459. // Mark and track the odd empty segment as a gap to avoid reloading
  10460. level.fragmentError++;
  10461. frag.gap = true;
  10462. this.fragmentTracker.removeFragment(frag);
  10463. this.fragmentTracker.fragBuffered(frag, true);
  10464. }
  10465. this.warn(error.message);
  10466. this.hls.trigger(Events.ERROR, {
  10467. type: ErrorTypes.MEDIA_ERROR,
  10468. details: ErrorDetails.FRAG_PARSING_ERROR,
  10469. fatal: false,
  10470. error: error,
  10471. frag: frag,
  10472. reason: "Found no media in msn " + frag.sn + " of level \"" + level.url + "\""
  10473. });
  10474. if (!this.hls) {
  10475. return;
  10476. }
  10477. this.resetTransmuxer();
  10478. // For this error fallthrough. Marking parsed will allow advancing to next fragment.
  10479. }
  10480. this.state = State.PARSED;
  10481. this.hls.trigger(Events.FRAG_PARSED, {
  10482. frag: frag,
  10483. part: part
  10484. });
  10485. };
  10486. _proto.resetTransmuxer = function resetTransmuxer() {
  10487. if (this.transmuxer) {
  10488. this.transmuxer.destroy();
  10489. this.transmuxer = null;
  10490. }
  10491. };
  10492. _proto.recoverWorkerError = function recoverWorkerError(data) {
  10493. if (data.event === 'demuxerWorker') {
  10494. this.fragmentTracker.removeAllFragments();
  10495. this.resetTransmuxer();
  10496. this.resetStartWhenNotLoaded(this.levelLastLoaded);
  10497. this.resetLoadingState();
  10498. }
  10499. };
  10500. _createClass(BaseStreamController, [{
  10501. key: "state",
  10502. get: function get() {
  10503. return this._state;
  10504. },
  10505. set: function set(nextState) {
  10506. var previousState = this._state;
  10507. if (previousState !== nextState) {
  10508. this._state = nextState;
  10509. this.log(previousState + "->" + nextState);
  10510. }
  10511. }
  10512. }]);
  10513. return BaseStreamController;
  10514. }(TaskLoop);
  10515. var ChunkCache = /*#__PURE__*/function () {
  10516. function ChunkCache() {
  10517. this.chunks = [];
  10518. this.dataLength = 0;
  10519. }
  10520. var _proto = ChunkCache.prototype;
  10521. _proto.push = function push(chunk) {
  10522. this.chunks.push(chunk);
  10523. this.dataLength += chunk.length;
  10524. };
  10525. _proto.flush = function flush() {
  10526. var chunks = this.chunks,
  10527. dataLength = this.dataLength;
  10528. var result;
  10529. if (!chunks.length) {
  10530. return new Uint8Array(0);
  10531. } else if (chunks.length === 1) {
  10532. result = chunks[0];
  10533. } else {
  10534. result = concatUint8Arrays(chunks, dataLength);
  10535. }
  10536. this.reset();
  10537. return result;
  10538. };
  10539. _proto.reset = function reset() {
  10540. this.chunks.length = 0;
  10541. this.dataLength = 0;
  10542. };
  10543. return ChunkCache;
  10544. }();
  10545. function concatUint8Arrays(chunks, dataLength) {
  10546. var result = new Uint8Array(dataLength);
  10547. var offset = 0;
  10548. for (var i = 0; i < chunks.length; i++) {
  10549. var chunk = chunks[i];
  10550. result.set(chunk, offset);
  10551. offset += chunk.length;
  10552. }
  10553. return result;
  10554. }
  10555. function dummyTrack(type, inputTimeScale) {
  10556. if (type === void 0) {
  10557. type = '';
  10558. }
  10559. if (inputTimeScale === void 0) {
  10560. inputTimeScale = 90000;
  10561. }
  10562. return {
  10563. type: type,
  10564. id: -1,
  10565. pid: -1,
  10566. inputTimeScale: inputTimeScale,
  10567. sequenceNumber: -1,
  10568. samples: [],
  10569. dropped: 0
  10570. };
  10571. }
  10572. var BaseAudioDemuxer = /*#__PURE__*/function () {
  10573. function BaseAudioDemuxer() {
  10574. this._audioTrack = void 0;
  10575. this._id3Track = void 0;
  10576. this.frameIndex = 0;
  10577. this.cachedData = null;
  10578. this.basePTS = null;
  10579. this.initPTS = null;
  10580. this.lastPTS = null;
  10581. }
  10582. var _proto = BaseAudioDemuxer.prototype;
  10583. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  10584. this._id3Track = {
  10585. type: 'id3',
  10586. id: 3,
  10587. pid: -1,
  10588. inputTimeScale: 90000,
  10589. sequenceNumber: 0,
  10590. samples: [],
  10591. dropped: 0
  10592. };
  10593. };
  10594. _proto.resetTimeStamp = function resetTimeStamp(deaultTimestamp) {
  10595. this.initPTS = deaultTimestamp;
  10596. this.resetContiguity();
  10597. };
  10598. _proto.resetContiguity = function resetContiguity() {
  10599. this.basePTS = null;
  10600. this.lastPTS = null;
  10601. this.frameIndex = 0;
  10602. };
  10603. _proto.canParse = function canParse(data, offset) {
  10604. return false;
  10605. };
  10606. _proto.appendFrame = function appendFrame(track, data, offset) {}
  10607. // feed incoming data to the front of the parsing pipeline
  10608. ;
  10609. _proto.demux = function demux(data, timeOffset) {
  10610. if (this.cachedData) {
  10611. data = appendUint8Array(this.cachedData, data);
  10612. this.cachedData = null;
  10613. }
  10614. var id3Data = getID3Data(data, 0);
  10615. var offset = id3Data ? id3Data.length : 0;
  10616. var lastDataIndex;
  10617. var track = this._audioTrack;
  10618. var id3Track = this._id3Track;
  10619. var timestamp = id3Data ? getTimeStamp(id3Data) : undefined;
  10620. var length = data.length;
  10621. if (this.basePTS === null || this.frameIndex === 0 && isFiniteNumber(timestamp)) {
  10622. this.basePTS = initPTSFn(timestamp, timeOffset, this.initPTS);
  10623. this.lastPTS = this.basePTS;
  10624. }
  10625. if (this.lastPTS === null) {
  10626. this.lastPTS = this.basePTS;
  10627. }
  10628. // more expressive than alternative: id3Data?.length
  10629. if (id3Data && id3Data.length > 0) {
  10630. id3Track.samples.push({
  10631. pts: this.lastPTS,
  10632. dts: this.lastPTS,
  10633. data: id3Data,
  10634. type: MetadataSchema.audioId3,
  10635. duration: Number.POSITIVE_INFINITY
  10636. });
  10637. }
  10638. while (offset < length) {
  10639. if (this.canParse(data, offset)) {
  10640. var frame = this.appendFrame(track, data, offset);
  10641. if (frame) {
  10642. this.frameIndex++;
  10643. this.lastPTS = frame.sample.pts;
  10644. offset += frame.length;
  10645. lastDataIndex = offset;
  10646. } else {
  10647. offset = length;
  10648. }
  10649. } else if (canParse$2(data, offset)) {
  10650. // after a ID3.canParse, a call to ID3.getID3Data *should* always returns some data
  10651. id3Data = getID3Data(data, offset);
  10652. id3Track.samples.push({
  10653. pts: this.lastPTS,
  10654. dts: this.lastPTS,
  10655. data: id3Data,
  10656. type: MetadataSchema.audioId3,
  10657. duration: Number.POSITIVE_INFINITY
  10658. });
  10659. offset += id3Data.length;
  10660. lastDataIndex = offset;
  10661. } else {
  10662. offset++;
  10663. }
  10664. if (offset === length && lastDataIndex !== length) {
  10665. var partialData = sliceUint8(data, lastDataIndex);
  10666. if (this.cachedData) {
  10667. this.cachedData = appendUint8Array(this.cachedData, partialData);
  10668. } else {
  10669. this.cachedData = partialData;
  10670. }
  10671. }
  10672. }
  10673. return {
  10674. audioTrack: track,
  10675. videoTrack: dummyTrack(),
  10676. id3Track: id3Track,
  10677. textTrack: dummyTrack()
  10678. };
  10679. };
  10680. _proto.demuxSampleAes = function demuxSampleAes(data, keyData, timeOffset) {
  10681. return Promise.reject(new Error("[" + this + "] This demuxer does not support Sample-AES decryption"));
  10682. };
  10683. _proto.flush = function flush(timeOffset) {
  10684. // Parse cache in case of remaining frames.
  10685. var cachedData = this.cachedData;
  10686. if (cachedData) {
  10687. this.cachedData = null;
  10688. this.demux(cachedData, 0);
  10689. }
  10690. return {
  10691. audioTrack: this._audioTrack,
  10692. videoTrack: dummyTrack(),
  10693. id3Track: this._id3Track,
  10694. textTrack: dummyTrack()
  10695. };
  10696. };
  10697. _proto.destroy = function destroy() {};
  10698. return BaseAudioDemuxer;
  10699. }();
  10700. /**
  10701. * Initialize PTS
  10702. * <p>
  10703. * use timestamp unless it is undefined, NaN or Infinity
  10704. * </p>
  10705. */
  10706. var initPTSFn = function initPTSFn(timestamp, timeOffset, initPTS) {
  10707. if (isFiniteNumber(timestamp)) {
  10708. return timestamp * 90;
  10709. }
  10710. var init90kHz = initPTS ? initPTS.baseTime * 90000 / initPTS.timescale : 0;
  10711. return timeOffset * 90000 + init90kHz;
  10712. };
  10713. /**
  10714. * ADTS parser helper
  10715. * @link https://wiki.multimedia.cx/index.php?title=ADTS
  10716. */
  10717. function getAudioConfig(observer, data, offset, audioCodec) {
  10718. var adtsObjectType;
  10719. var adtsExtensionSamplingIndex;
  10720. var adtsChannelConfig;
  10721. var config;
  10722. var userAgent = navigator.userAgent.toLowerCase();
  10723. var manifestCodec = audioCodec;
  10724. var adtsSamplingRates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  10725. // byte 2
  10726. adtsObjectType = ((data[offset + 2] & 0xc0) >>> 6) + 1;
  10727. var adtsSamplingIndex = (data[offset + 2] & 0x3c) >>> 2;
  10728. if (adtsSamplingIndex > adtsSamplingRates.length - 1) {
  10729. var error = new Error("invalid ADTS sampling index:" + adtsSamplingIndex);
  10730. observer.emit(Events.ERROR, Events.ERROR, {
  10731. type: ErrorTypes.MEDIA_ERROR,
  10732. details: ErrorDetails.FRAG_PARSING_ERROR,
  10733. fatal: true,
  10734. error: error,
  10735. reason: error.message
  10736. });
  10737. return;
  10738. }
  10739. adtsChannelConfig = (data[offset + 2] & 0x01) << 2;
  10740. // byte 3
  10741. adtsChannelConfig |= (data[offset + 3] & 0xc0) >>> 6;
  10742. logger.log("manifest codec:" + audioCodec + ", ADTS type:" + adtsObjectType + ", samplingIndex:" + adtsSamplingIndex);
  10743. // firefox: freq less than 24kHz = AAC SBR (HE-AAC)
  10744. if (/firefox/i.test(userAgent)) {
  10745. if (adtsSamplingIndex >= 6) {
  10746. adtsObjectType = 5;
  10747. config = new Array(4);
  10748. // HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
  10749. // there is a factor 2 between frame sample rate and output sample rate
  10750. // multiply frequency by 2 (see table below, equivalent to substract 3)
  10751. adtsExtensionSamplingIndex = adtsSamplingIndex - 3;
  10752. } else {
  10753. adtsObjectType = 2;
  10754. config = new Array(2);
  10755. adtsExtensionSamplingIndex = adtsSamplingIndex;
  10756. }
  10757. // Android : always use AAC
  10758. } else if (userAgent.indexOf('android') !== -1) {
  10759. adtsObjectType = 2;
  10760. config = new Array(2);
  10761. adtsExtensionSamplingIndex = adtsSamplingIndex;
  10762. } else {
  10763. /* for other browsers (Chrome/Vivaldi/Opera ...)
  10764. always force audio type to be HE-AAC SBR, as some browsers do not support audio codec switch properly (like Chrome ...)
  10765. */
  10766. adtsObjectType = 5;
  10767. config = new Array(4);
  10768. // if (manifest codec is HE-AAC or HE-AACv2) OR (manifest codec not specified AND frequency less than 24kHz)
  10769. if (audioCodec && (audioCodec.indexOf('mp4a.40.29') !== -1 || audioCodec.indexOf('mp4a.40.5') !== -1) || !audioCodec && adtsSamplingIndex >= 6) {
  10770. // HE-AAC uses SBR (Spectral Band Replication) , high frequencies are constructed from low frequencies
  10771. // there is a factor 2 between frame sample rate and output sample rate
  10772. // multiply frequency by 2 (see table below, equivalent to substract 3)
  10773. adtsExtensionSamplingIndex = adtsSamplingIndex - 3;
  10774. } else {
  10775. // if (manifest codec is AAC) AND (frequency less than 24kHz AND nb channel is 1) OR (manifest codec not specified and mono audio)
  10776. // Chrome fails to play back with low frequency AAC LC mono when initialized with HE-AAC. This is not a problem with stereo.
  10777. if (audioCodec && audioCodec.indexOf('mp4a.40.2') !== -1 && (adtsSamplingIndex >= 6 && adtsChannelConfig === 1 || /vivaldi/i.test(userAgent)) || !audioCodec && adtsChannelConfig === 1) {
  10778. adtsObjectType = 2;
  10779. config = new Array(2);
  10780. }
  10781. adtsExtensionSamplingIndex = adtsSamplingIndex;
  10782. }
  10783. }
  10784. /* refer to http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Audio_Specific_Config
  10785. ISO 14496-3 (AAC).pdf - Table 1.13 — Syntax of AudioSpecificConfig()
  10786. Audio Profile / Audio Object Type
  10787. 0: Null
  10788. 1: AAC Main
  10789. 2: AAC LC (Low Complexity)
  10790. 3: AAC SSR (Scalable Sample Rate)
  10791. 4: AAC LTP (Long Term Prediction)
  10792. 5: SBR (Spectral Band Replication)
  10793. 6: AAC Scalable
  10794. sampling freq
  10795. 0: 96000 Hz
  10796. 1: 88200 Hz
  10797. 2: 64000 Hz
  10798. 3: 48000 Hz
  10799. 4: 44100 Hz
  10800. 5: 32000 Hz
  10801. 6: 24000 Hz
  10802. 7: 22050 Hz
  10803. 8: 16000 Hz
  10804. 9: 12000 Hz
  10805. 10: 11025 Hz
  10806. 11: 8000 Hz
  10807. 12: 7350 Hz
  10808. 13: Reserved
  10809. 14: Reserved
  10810. 15: frequency is written explictly
  10811. Channel Configurations
  10812. These are the channel configurations:
  10813. 0: Defined in AOT Specifc Config
  10814. 1: 1 channel: front-center
  10815. 2: 2 channels: front-left, front-right
  10816. */
  10817. // audioObjectType = profile => profile, the MPEG-4 Audio Object Type minus 1
  10818. config[0] = adtsObjectType << 3;
  10819. // samplingFrequencyIndex
  10820. config[0] |= (adtsSamplingIndex & 0x0e) >> 1;
  10821. config[1] |= (adtsSamplingIndex & 0x01) << 7;
  10822. // channelConfiguration
  10823. config[1] |= adtsChannelConfig << 3;
  10824. if (adtsObjectType === 5) {
  10825. // adtsExtensionSamplingIndex
  10826. config[1] |= (adtsExtensionSamplingIndex & 0x0e) >> 1;
  10827. config[2] = (adtsExtensionSamplingIndex & 0x01) << 7;
  10828. // adtsObjectType (force to 2, chrome is checking that object type is less than 5 ???
  10829. // https://chromium.googlesource.com/chromium/src.git/+/master/media/formats/mp4/aac.cc
  10830. config[2] |= 2 << 2;
  10831. config[3] = 0;
  10832. }
  10833. return {
  10834. config: config,
  10835. samplerate: adtsSamplingRates[adtsSamplingIndex],
  10836. channelCount: adtsChannelConfig,
  10837. codec: 'mp4a.40.' + adtsObjectType,
  10838. manifestCodec: manifestCodec
  10839. };
  10840. }
  10841. function isHeaderPattern$1(data, offset) {
  10842. return data[offset] === 0xff && (data[offset + 1] & 0xf6) === 0xf0;
  10843. }
  10844. function getHeaderLength(data, offset) {
  10845. return data[offset + 1] & 0x01 ? 7 : 9;
  10846. }
  10847. function getFullFrameLength(data, offset) {
  10848. return (data[offset + 3] & 0x03) << 11 | data[offset + 4] << 3 | (data[offset + 5] & 0xe0) >>> 5;
  10849. }
  10850. function canGetFrameLength(data, offset) {
  10851. return offset + 5 < data.length;
  10852. }
  10853. function isHeader$1(data, offset) {
  10854. // Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1
  10855. // Layer bits (position 14 and 15) in header should be always 0 for ADTS
  10856. // More info https://wiki.multimedia.cx/index.php?title=ADTS
  10857. return offset + 1 < data.length && isHeaderPattern$1(data, offset);
  10858. }
  10859. function canParse$1(data, offset) {
  10860. return canGetFrameLength(data, offset) && isHeaderPattern$1(data, offset) && getFullFrameLength(data, offset) <= data.length - offset;
  10861. }
  10862. function probe$1(data, offset) {
  10863. // same as isHeader but we also check that ADTS frame follows last ADTS frame
  10864. // or end of data is reached
  10865. if (isHeader$1(data, offset)) {
  10866. // ADTS header Length
  10867. var headerLength = getHeaderLength(data, offset);
  10868. if (offset + headerLength >= data.length) {
  10869. return false;
  10870. }
  10871. // ADTS frame Length
  10872. var frameLength = getFullFrameLength(data, offset);
  10873. if (frameLength <= headerLength) {
  10874. return false;
  10875. }
  10876. var newOffset = offset + frameLength;
  10877. return newOffset === data.length || isHeader$1(data, newOffset);
  10878. }
  10879. return false;
  10880. }
  10881. function initTrackConfig(track, observer, data, offset, audioCodec) {
  10882. if (!track.samplerate) {
  10883. var config = getAudioConfig(observer, data, offset, audioCodec);
  10884. if (!config) {
  10885. return;
  10886. }
  10887. track.config = config.config;
  10888. track.samplerate = config.samplerate;
  10889. track.channelCount = config.channelCount;
  10890. track.codec = config.codec;
  10891. track.manifestCodec = config.manifestCodec;
  10892. logger.log("parsed codec:" + track.codec + ", rate:" + config.samplerate + ", channels:" + config.channelCount);
  10893. }
  10894. }
  10895. function getFrameDuration(samplerate) {
  10896. return 1024 * 90000 / samplerate;
  10897. }
  10898. function parseFrameHeader(data, offset) {
  10899. // The protection skip bit tells us if we have 2 bytes of CRC data at the end of the ADTS header
  10900. var headerLength = getHeaderLength(data, offset);
  10901. if (offset + headerLength <= data.length) {
  10902. // retrieve frame size
  10903. var frameLength = getFullFrameLength(data, offset) - headerLength;
  10904. if (frameLength > 0) {
  10905. // logger.log(`AAC frame, offset/length/total/pts:${offset+headerLength}/${frameLength}/${data.byteLength}`);
  10906. return {
  10907. headerLength: headerLength,
  10908. frameLength: frameLength
  10909. };
  10910. }
  10911. }
  10912. }
  10913. function appendFrame$1(track, data, offset, pts, frameIndex) {
  10914. var frameDuration = getFrameDuration(track.samplerate);
  10915. var stamp = pts + frameIndex * frameDuration;
  10916. var header = parseFrameHeader(data, offset);
  10917. var unit;
  10918. if (header) {
  10919. var frameLength = header.frameLength,
  10920. headerLength = header.headerLength;
  10921. var _length = headerLength + frameLength;
  10922. var missing = Math.max(0, offset + _length - data.length);
  10923. // logger.log(`AAC frame ${frameIndex}, pts:${stamp} length@offset/total: ${frameLength}@${offset+headerLength}/${data.byteLength} missing: ${missing}`);
  10924. if (missing) {
  10925. unit = new Uint8Array(_length - headerLength);
  10926. unit.set(data.subarray(offset + headerLength, data.length), 0);
  10927. } else {
  10928. unit = data.subarray(offset + headerLength, offset + _length);
  10929. }
  10930. var _sample = {
  10931. unit: unit,
  10932. pts: stamp
  10933. };
  10934. if (!missing) {
  10935. track.samples.push(_sample);
  10936. }
  10937. return {
  10938. sample: _sample,
  10939. length: _length,
  10940. missing: missing
  10941. };
  10942. }
  10943. // overflow incomplete header
  10944. var length = data.length - offset;
  10945. unit = new Uint8Array(length);
  10946. unit.set(data.subarray(offset, data.length), 0);
  10947. var sample = {
  10948. unit: unit,
  10949. pts: stamp
  10950. };
  10951. return {
  10952. sample: sample,
  10953. length: length,
  10954. missing: -1
  10955. };
  10956. }
  10957. /**
  10958. * MPEG parser helper
  10959. */
  10960. var chromeVersion$1 = null;
  10961. var BitratesMap = [32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160];
  10962. var SamplingRateMap = [44100, 48000, 32000, 22050, 24000, 16000, 11025, 12000, 8000];
  10963. var SamplesCoefficients = [
  10964. // MPEG 2.5
  10965. [0,
  10966. // Reserved
  10967. 72,
  10968. // Layer3
  10969. 144,
  10970. // Layer2
  10971. 12 // Layer1
  10972. ],
  10973. // Reserved
  10974. [0,
  10975. // Reserved
  10976. 0,
  10977. // Layer3
  10978. 0,
  10979. // Layer2
  10980. 0 // Layer1
  10981. ],
  10982. // MPEG 2
  10983. [0,
  10984. // Reserved
  10985. 72,
  10986. // Layer3
  10987. 144,
  10988. // Layer2
  10989. 12 // Layer1
  10990. ],
  10991. // MPEG 1
  10992. [0,
  10993. // Reserved
  10994. 144,
  10995. // Layer3
  10996. 144,
  10997. // Layer2
  10998. 12 // Layer1
  10999. ]];
  11000. var BytesInSlot = [0,
  11001. // Reserved
  11002. 1,
  11003. // Layer3
  11004. 1,
  11005. // Layer2
  11006. 4 // Layer1
  11007. ];
  11008. function appendFrame(track, data, offset, pts, frameIndex) {
  11009. // Using http://www.datavoyage.com/mpgscript/mpeghdr.htm as a reference
  11010. if (offset + 24 > data.length) {
  11011. return;
  11012. }
  11013. var header = parseHeader(data, offset);
  11014. if (header && offset + header.frameLength <= data.length) {
  11015. var frameDuration = header.samplesPerFrame * 90000 / header.sampleRate;
  11016. var stamp = pts + frameIndex * frameDuration;
  11017. var sample = {
  11018. unit: data.subarray(offset, offset + header.frameLength),
  11019. pts: stamp,
  11020. dts: stamp
  11021. };
  11022. track.config = [];
  11023. track.channelCount = header.channelCount;
  11024. track.samplerate = header.sampleRate;
  11025. track.samples.push(sample);
  11026. return {
  11027. sample: sample,
  11028. length: header.frameLength,
  11029. missing: 0
  11030. };
  11031. }
  11032. }
  11033. function parseHeader(data, offset) {
  11034. var mpegVersion = data[offset + 1] >> 3 & 3;
  11035. var mpegLayer = data[offset + 1] >> 1 & 3;
  11036. var bitRateIndex = data[offset + 2] >> 4 & 15;
  11037. var sampleRateIndex = data[offset + 2] >> 2 & 3;
  11038. if (mpegVersion !== 1 && bitRateIndex !== 0 && bitRateIndex !== 15 && sampleRateIndex !== 3) {
  11039. var paddingBit = data[offset + 2] >> 1 & 1;
  11040. var channelMode = data[offset + 3] >> 6;
  11041. var columnInBitrates = mpegVersion === 3 ? 3 - mpegLayer : mpegLayer === 3 ? 3 : 4;
  11042. var bitRate = BitratesMap[columnInBitrates * 14 + bitRateIndex - 1] * 1000;
  11043. var columnInSampleRates = mpegVersion === 3 ? 0 : mpegVersion === 2 ? 1 : 2;
  11044. var sampleRate = SamplingRateMap[columnInSampleRates * 3 + sampleRateIndex];
  11045. var channelCount = channelMode === 3 ? 1 : 2; // If bits of channel mode are `11` then it is a single channel (Mono)
  11046. var sampleCoefficient = SamplesCoefficients[mpegVersion][mpegLayer];
  11047. var bytesInSlot = BytesInSlot[mpegLayer];
  11048. var samplesPerFrame = sampleCoefficient * 8 * bytesInSlot;
  11049. var frameLength = Math.floor(sampleCoefficient * bitRate / sampleRate + paddingBit) * bytesInSlot;
  11050. if (chromeVersion$1 === null) {
  11051. var userAgent = navigator.userAgent || '';
  11052. var result = userAgent.match(/Chrome\/(\d+)/i);
  11053. chromeVersion$1 = result ? parseInt(result[1]) : 0;
  11054. }
  11055. var needChromeFix = !!chromeVersion$1 && chromeVersion$1 <= 87;
  11056. if (needChromeFix && mpegLayer === 2 && bitRate >= 224000 && channelMode === 0) {
  11057. // Work around bug in Chromium by setting channelMode to dual-channel (01) instead of stereo (00)
  11058. data[offset + 3] = data[offset + 3] | 0x80;
  11059. }
  11060. return {
  11061. sampleRate: sampleRate,
  11062. channelCount: channelCount,
  11063. frameLength: frameLength,
  11064. samplesPerFrame: samplesPerFrame
  11065. };
  11066. }
  11067. }
  11068. function isHeaderPattern(data, offset) {
  11069. return data[offset] === 0xff && (data[offset + 1] & 0xe0) === 0xe0 && (data[offset + 1] & 0x06) !== 0x00;
  11070. }
  11071. function isHeader(data, offset) {
  11072. // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
  11073. // Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
  11074. // More info http://www.mp3-tech.org/programmer/frame_header.html
  11075. return offset + 1 < data.length && isHeaderPattern(data, offset);
  11076. }
  11077. function canParse(data, offset) {
  11078. var headerSize = 4;
  11079. return isHeaderPattern(data, offset) && headerSize <= data.length - offset;
  11080. }
  11081. function probe(data, offset) {
  11082. // same as isHeader but we also check that MPEG frame follows last MPEG frame
  11083. // or end of data is reached
  11084. if (offset + 1 < data.length && isHeaderPattern(data, offset)) {
  11085. // MPEG header Length
  11086. var headerLength = 4;
  11087. // MPEG frame Length
  11088. var header = parseHeader(data, offset);
  11089. var frameLength = headerLength;
  11090. if (header != null && header.frameLength) {
  11091. frameLength = header.frameLength;
  11092. }
  11093. var newOffset = offset + frameLength;
  11094. return newOffset === data.length || isHeader(data, newOffset);
  11095. }
  11096. return false;
  11097. }
  11098. var AACDemuxer = /*#__PURE__*/function (_BaseAudioDemuxer) {
  11099. _inheritsLoose(AACDemuxer, _BaseAudioDemuxer);
  11100. function AACDemuxer(observer, config) {
  11101. var _this;
  11102. _this = _BaseAudioDemuxer.call(this) || this;
  11103. _this.observer = void 0;
  11104. _this.config = void 0;
  11105. _this.observer = observer;
  11106. _this.config = config;
  11107. return _this;
  11108. }
  11109. var _proto = AACDemuxer.prototype;
  11110. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  11111. _BaseAudioDemuxer.prototype.resetInitSegment.call(this, initSegment, audioCodec, videoCodec, trackDuration);
  11112. this._audioTrack = {
  11113. container: 'audio/adts',
  11114. type: 'audio',
  11115. id: 2,
  11116. pid: -1,
  11117. sequenceNumber: 0,
  11118. segmentCodec: 'aac',
  11119. samples: [],
  11120. manifestCodec: audioCodec,
  11121. duration: trackDuration,
  11122. inputTimeScale: 90000,
  11123. dropped: 0
  11124. };
  11125. }
  11126. // Source for probe info - https://wiki.multimedia.cx/index.php?title=ADTS
  11127. ;
  11128. AACDemuxer.probe = function probe$2(data) {
  11129. if (!data) {
  11130. return false;
  11131. }
  11132. // Check for the ADTS sync word
  11133. // Look for ADTS header | 1111 1111 | 1111 X00X | where X can be either 0 or 1
  11134. // Layer bits (position 14 and 15) in header should be always 0 for ADTS
  11135. // More info https://wiki.multimedia.cx/index.php?title=ADTS
  11136. var id3Data = getID3Data(data, 0);
  11137. var offset = (id3Data == null ? void 0 : id3Data.length) || 0;
  11138. if (probe(data, offset)) {
  11139. return false;
  11140. }
  11141. for (var length = data.length; offset < length; offset++) {
  11142. if (probe$1(data, offset)) {
  11143. logger.log('ADTS sync word found !');
  11144. return true;
  11145. }
  11146. }
  11147. return false;
  11148. };
  11149. _proto.canParse = function canParse(data, offset) {
  11150. return canParse$1(data, offset);
  11151. };
  11152. _proto.appendFrame = function appendFrame(track, data, offset) {
  11153. initTrackConfig(track, this.observer, data, offset, track.manifestCodec);
  11154. var frame = appendFrame$1(track, data, offset, this.basePTS, this.frameIndex);
  11155. if (frame && frame.missing === 0) {
  11156. return frame;
  11157. }
  11158. };
  11159. return AACDemuxer;
  11160. }(BaseAudioDemuxer);
  11161. var emsgSchemePattern = /\/emsg[-/]ID3/i;
  11162. var MP4Demuxer = /*#__PURE__*/function () {
  11163. function MP4Demuxer(observer, config) {
  11164. this.remainderData = null;
  11165. this.timeOffset = 0;
  11166. this.config = void 0;
  11167. this.videoTrack = void 0;
  11168. this.audioTrack = void 0;
  11169. this.id3Track = void 0;
  11170. this.txtTrack = void 0;
  11171. this.config = config;
  11172. }
  11173. var _proto = MP4Demuxer.prototype;
  11174. _proto.resetTimeStamp = function resetTimeStamp() {};
  11175. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  11176. var videoTrack = this.videoTrack = dummyTrack('video', 1);
  11177. var audioTrack = this.audioTrack = dummyTrack('audio', 1);
  11178. var captionTrack = this.txtTrack = dummyTrack('text', 1);
  11179. this.id3Track = dummyTrack('id3', 1);
  11180. this.timeOffset = 0;
  11181. if (!(initSegment != null && initSegment.byteLength)) {
  11182. return;
  11183. }
  11184. var initData = parseInitSegment(initSegment);
  11185. if (initData.video) {
  11186. var _initData$video = initData.video,
  11187. id = _initData$video.id,
  11188. timescale = _initData$video.timescale,
  11189. codec = _initData$video.codec;
  11190. videoTrack.id = id;
  11191. videoTrack.timescale = captionTrack.timescale = timescale;
  11192. videoTrack.codec = codec;
  11193. }
  11194. if (initData.audio) {
  11195. var _initData$audio = initData.audio,
  11196. _id = _initData$audio.id,
  11197. _timescale = _initData$audio.timescale,
  11198. _codec = _initData$audio.codec;
  11199. audioTrack.id = _id;
  11200. audioTrack.timescale = _timescale;
  11201. audioTrack.codec = _codec;
  11202. }
  11203. captionTrack.id = RemuxerTrackIdConfig.text;
  11204. videoTrack.sampleDuration = 0;
  11205. videoTrack.duration = audioTrack.duration = trackDuration;
  11206. };
  11207. _proto.resetContiguity = function resetContiguity() {
  11208. this.remainderData = null;
  11209. };
  11210. MP4Demuxer.probe = function probe(data) {
  11211. return hasMoofData(data);
  11212. };
  11213. _proto.demux = function demux(data, timeOffset) {
  11214. this.timeOffset = timeOffset;
  11215. // Load all data into the avc track. The CMAF remuxer will look for the data in the samples object; the rest of the fields do not matter
  11216. var videoSamples = data;
  11217. var videoTrack = this.videoTrack;
  11218. var textTrack = this.txtTrack;
  11219. if (this.config.progressive) {
  11220. // Split the bytestream into two ranges: one encompassing all data up until the start of the last moof, and everything else.
  11221. // This is done to guarantee that we're sending valid data to MSE - when demuxing progressively, we have no guarantee
  11222. // that the fetch loader gives us flush moof+mdat pairs. If we push jagged data to MSE, it will throw an exception.
  11223. if (this.remainderData) {
  11224. videoSamples = appendUint8Array(this.remainderData, data);
  11225. }
  11226. var segmentedData = segmentValidRange(videoSamples);
  11227. this.remainderData = segmentedData.remainder;
  11228. videoTrack.samples = segmentedData.valid || new Uint8Array();
  11229. } else {
  11230. videoTrack.samples = videoSamples;
  11231. }
  11232. var id3Track = this.extractID3Track(videoTrack, timeOffset);
  11233. textTrack.samples = parseSamples(timeOffset, videoTrack);
  11234. return {
  11235. videoTrack: videoTrack,
  11236. audioTrack: this.audioTrack,
  11237. id3Track: id3Track,
  11238. textTrack: this.txtTrack
  11239. };
  11240. };
  11241. _proto.flush = function flush() {
  11242. var timeOffset = this.timeOffset;
  11243. var videoTrack = this.videoTrack;
  11244. var textTrack = this.txtTrack;
  11245. videoTrack.samples = this.remainderData || new Uint8Array();
  11246. this.remainderData = null;
  11247. var id3Track = this.extractID3Track(videoTrack, this.timeOffset);
  11248. textTrack.samples = parseSamples(timeOffset, videoTrack);
  11249. return {
  11250. videoTrack: videoTrack,
  11251. audioTrack: dummyTrack(),
  11252. id3Track: id3Track,
  11253. textTrack: dummyTrack()
  11254. };
  11255. };
  11256. _proto.extractID3Track = function extractID3Track(videoTrack, timeOffset) {
  11257. var id3Track = this.id3Track;
  11258. if (videoTrack.samples.length) {
  11259. var emsgs = findBox(videoTrack.samples, ['emsg']);
  11260. if (emsgs) {
  11261. emsgs.forEach(function (data) {
  11262. var emsgInfo = parseEmsg(data);
  11263. if (emsgSchemePattern.test(emsgInfo.schemeIdUri)) {
  11264. var pts = isFiniteNumber(emsgInfo.presentationTime) ? emsgInfo.presentationTime / emsgInfo.timeScale : timeOffset + emsgInfo.presentationTimeDelta / emsgInfo.timeScale;
  11265. var duration = emsgInfo.eventDuration === 0xffffffff ? Number.POSITIVE_INFINITY : emsgInfo.eventDuration / emsgInfo.timeScale;
  11266. // Safari takes anything <= 0.001 seconds and maps it to Infinity
  11267. if (duration <= 0.001) {
  11268. duration = Number.POSITIVE_INFINITY;
  11269. }
  11270. var payload = emsgInfo.payload;
  11271. id3Track.samples.push({
  11272. data: payload,
  11273. len: payload.byteLength,
  11274. dts: pts,
  11275. pts: pts,
  11276. type: MetadataSchema.emsg,
  11277. duration: duration
  11278. });
  11279. }
  11280. });
  11281. }
  11282. }
  11283. return id3Track;
  11284. };
  11285. _proto.demuxSampleAes = function demuxSampleAes(data, keyData, timeOffset) {
  11286. return Promise.reject(new Error('The MP4 demuxer does not support SAMPLE-AES decryption'));
  11287. };
  11288. _proto.destroy = function destroy() {};
  11289. return MP4Demuxer;
  11290. }();
  11291. var getAudioBSID = function getAudioBSID(data, offset) {
  11292. // check the bsid to confirm ac-3 | ec-3
  11293. var bsid = 0;
  11294. var numBits = 5;
  11295. offset += numBits;
  11296. var temp = new Uint32Array(1); // unsigned 32 bit for temporary storage
  11297. var mask = new Uint32Array(1); // unsigned 32 bit mask value
  11298. var _byte = new Uint8Array(1); // unsigned 8 bit for temporary storage
  11299. while (numBits > 0) {
  11300. _byte[0] = data[offset];
  11301. // read remaining bits, upto 8 bits at a time
  11302. var bits = Math.min(numBits, 8);
  11303. var shift = 8 - bits;
  11304. mask[0] = 0xff000000 >>> 24 + shift << shift;
  11305. temp[0] = (_byte[0] & mask[0]) >> shift;
  11306. bsid = !bsid ? temp[0] : bsid << bits | temp[0];
  11307. offset += 1;
  11308. numBits -= bits;
  11309. }
  11310. return bsid;
  11311. };
  11312. var AC3Demuxer = /*#__PURE__*/function (_BaseAudioDemuxer) {
  11313. _inheritsLoose(AC3Demuxer, _BaseAudioDemuxer);
  11314. function AC3Demuxer(observer) {
  11315. var _this;
  11316. _this = _BaseAudioDemuxer.call(this) || this;
  11317. _this.observer = void 0;
  11318. _this.observer = observer;
  11319. return _this;
  11320. }
  11321. var _proto = AC3Demuxer.prototype;
  11322. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  11323. _BaseAudioDemuxer.prototype.resetInitSegment.call(this, initSegment, audioCodec, videoCodec, trackDuration);
  11324. this._audioTrack = {
  11325. container: 'audio/ac-3',
  11326. type: 'audio',
  11327. id: 2,
  11328. pid: -1,
  11329. sequenceNumber: 0,
  11330. segmentCodec: 'ac3',
  11331. samples: [],
  11332. manifestCodec: audioCodec,
  11333. duration: trackDuration,
  11334. inputTimeScale: 90000,
  11335. dropped: 0
  11336. };
  11337. };
  11338. _proto.canParse = function canParse(data, offset) {
  11339. return offset + 64 < data.length;
  11340. };
  11341. _proto.appendFrame = function appendFrame(track, data, offset) {
  11342. var frameLength = _appendFrame(track, data, offset, this.basePTS, this.frameIndex);
  11343. if (frameLength !== -1) {
  11344. var sample = track.samples[track.samples.length - 1];
  11345. return {
  11346. sample: sample,
  11347. length: frameLength,
  11348. missing: 0
  11349. };
  11350. }
  11351. };
  11352. AC3Demuxer.probe = function probe(data) {
  11353. if (!data) {
  11354. return false;
  11355. }
  11356. var id3Data = getID3Data(data, 0);
  11357. if (!id3Data) {
  11358. return false;
  11359. }
  11360. // look for the ac-3 sync bytes
  11361. var offset = id3Data.length;
  11362. if (data[offset] === 0x0b && data[offset + 1] === 0x77 && getTimeStamp(id3Data) !== undefined &&
  11363. // check the bsid to confirm ac-3
  11364. getAudioBSID(data, offset) < 16) {
  11365. return true;
  11366. }
  11367. return false;
  11368. };
  11369. return AC3Demuxer;
  11370. }(BaseAudioDemuxer);
  11371. function _appendFrame(track, data, start, pts, frameIndex) {
  11372. if (start + 8 > data.length) {
  11373. return -1; // not enough bytes left
  11374. }
  11375. if (data[start] !== 0x0b || data[start + 1] !== 0x77) {
  11376. return -1; // invalid magic
  11377. }
  11378. // get sample rate
  11379. var samplingRateCode = data[start + 4] >> 6;
  11380. if (samplingRateCode >= 3) {
  11381. return -1; // invalid sampling rate
  11382. }
  11383. var samplingRateMap = [48000, 44100, 32000];
  11384. var sampleRate = samplingRateMap[samplingRateCode];
  11385. // get frame size
  11386. var frameSizeCode = data[start + 4] & 0x3f;
  11387. var frameSizeMap = [64, 69, 96, 64, 70, 96, 80, 87, 120, 80, 88, 120, 96, 104, 144, 96, 105, 144, 112, 121, 168, 112, 122, 168, 128, 139, 192, 128, 140, 192, 160, 174, 240, 160, 175, 240, 192, 208, 288, 192, 209, 288, 224, 243, 336, 224, 244, 336, 256, 278, 384, 256, 279, 384, 320, 348, 480, 320, 349, 480, 384, 417, 576, 384, 418, 576, 448, 487, 672, 448, 488, 672, 512, 557, 768, 512, 558, 768, 640, 696, 960, 640, 697, 960, 768, 835, 1152, 768, 836, 1152, 896, 975, 1344, 896, 976, 1344, 1024, 1114, 1536, 1024, 1115, 1536, 1152, 1253, 1728, 1152, 1254, 1728, 1280, 1393, 1920, 1280, 1394, 1920];
  11388. var frameLength = frameSizeMap[frameSizeCode * 3 + samplingRateCode] * 2;
  11389. if (start + frameLength > data.length) {
  11390. return -1;
  11391. }
  11392. // get channel count
  11393. var channelMode = data[start + 6] >> 5;
  11394. var skipCount = 0;
  11395. if (channelMode === 2) {
  11396. skipCount += 2;
  11397. } else {
  11398. if (channelMode & 1 && channelMode !== 1) {
  11399. skipCount += 2;
  11400. }
  11401. if (channelMode & 4) {
  11402. skipCount += 2;
  11403. }
  11404. }
  11405. var lfeon = (data[start + 6] << 8 | data[start + 7]) >> 12 - skipCount & 1;
  11406. var channelsMap = [2, 1, 2, 3, 3, 4, 4, 5];
  11407. var channelCount = channelsMap[channelMode] + lfeon;
  11408. // build dac3 box
  11409. var bsid = data[start + 5] >> 3;
  11410. var bsmod = data[start + 5] & 7;
  11411. var config = new Uint8Array([samplingRateCode << 6 | bsid << 1 | bsmod >> 2, (bsmod & 3) << 6 | channelMode << 3 | lfeon << 2 | frameSizeCode >> 4, frameSizeCode << 4 & 0xe0]);
  11412. var frameDuration = 1536 / sampleRate * 90000;
  11413. var stamp = pts + frameIndex * frameDuration;
  11414. var unit = data.subarray(start, start + frameLength);
  11415. track.config = config;
  11416. track.channelCount = channelCount;
  11417. track.samplerate = sampleRate;
  11418. track.samples.push({
  11419. unit: unit,
  11420. pts: stamp
  11421. });
  11422. return frameLength;
  11423. }
  11424. var BaseVideoParser = /*#__PURE__*/function () {
  11425. function BaseVideoParser() {
  11426. this.VideoSample = null;
  11427. }
  11428. var _proto = BaseVideoParser.prototype;
  11429. _proto.createVideoSample = function createVideoSample(key, pts, dts, debug) {
  11430. return {
  11431. key: key,
  11432. frame: false,
  11433. pts: pts,
  11434. dts: dts,
  11435. units: [],
  11436. debug: debug,
  11437. length: 0
  11438. };
  11439. };
  11440. _proto.getLastNalUnit = function getLastNalUnit(samples) {
  11441. var _VideoSample;
  11442. var VideoSample = this.VideoSample;
  11443. var lastUnit;
  11444. // try to fallback to previous sample if current one is empty
  11445. if (!VideoSample || VideoSample.units.length === 0) {
  11446. VideoSample = samples[samples.length - 1];
  11447. }
  11448. if ((_VideoSample = VideoSample) != null && _VideoSample.units) {
  11449. var units = VideoSample.units;
  11450. lastUnit = units[units.length - 1];
  11451. }
  11452. return lastUnit;
  11453. };
  11454. _proto.pushAccessUnit = function pushAccessUnit(VideoSample, videoTrack) {
  11455. if (VideoSample.units.length && VideoSample.frame) {
  11456. // if sample does not have PTS/DTS, patch with last sample PTS/DTS
  11457. if (VideoSample.pts === undefined) {
  11458. var samples = videoTrack.samples;
  11459. var nbSamples = samples.length;
  11460. if (nbSamples) {
  11461. var lastSample = samples[nbSamples - 1];
  11462. VideoSample.pts = lastSample.pts;
  11463. VideoSample.dts = lastSample.dts;
  11464. } else {
  11465. // dropping samples, no timestamp found
  11466. videoTrack.dropped++;
  11467. return;
  11468. }
  11469. }
  11470. videoTrack.samples.push(VideoSample);
  11471. }
  11472. if (VideoSample.debug.length) {
  11473. logger.log(VideoSample.pts + '/' + VideoSample.dts + ':' + VideoSample.debug);
  11474. }
  11475. };
  11476. return BaseVideoParser;
  11477. }();
  11478. /**
  11479. * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
  11480. */
  11481. var ExpGolomb = /*#__PURE__*/function () {
  11482. function ExpGolomb(data) {
  11483. this.data = void 0;
  11484. this.bytesAvailable = void 0;
  11485. this.word = void 0;
  11486. this.bitsAvailable = void 0;
  11487. this.data = data;
  11488. // the number of bytes left to examine in this.data
  11489. this.bytesAvailable = data.byteLength;
  11490. // the current word being examined
  11491. this.word = 0; // :uint
  11492. // the number of bits left to examine in the current word
  11493. this.bitsAvailable = 0; // :uint
  11494. }
  11495. // ():void
  11496. var _proto = ExpGolomb.prototype;
  11497. _proto.loadWord = function loadWord() {
  11498. var data = this.data;
  11499. var bytesAvailable = this.bytesAvailable;
  11500. var position = data.byteLength - bytesAvailable;
  11501. var workingBytes = new Uint8Array(4);
  11502. var availableBytes = Math.min(4, bytesAvailable);
  11503. if (availableBytes === 0) {
  11504. throw new Error('no bytes available');
  11505. }
  11506. workingBytes.set(data.subarray(position, position + availableBytes));
  11507. this.word = new DataView(workingBytes.buffer).getUint32(0);
  11508. // track the amount of this.data that has been processed
  11509. this.bitsAvailable = availableBytes * 8;
  11510. this.bytesAvailable -= availableBytes;
  11511. }
  11512. // (count:int):void
  11513. ;
  11514. _proto.skipBits = function skipBits(count) {
  11515. var skipBytes; // :int
  11516. count = Math.min(count, this.bytesAvailable * 8 + this.bitsAvailable);
  11517. if (this.bitsAvailable > count) {
  11518. this.word <<= count;
  11519. this.bitsAvailable -= count;
  11520. } else {
  11521. count -= this.bitsAvailable;
  11522. skipBytes = count >> 3;
  11523. count -= skipBytes << 3;
  11524. this.bytesAvailable -= skipBytes;
  11525. this.loadWord();
  11526. this.word <<= count;
  11527. this.bitsAvailable -= count;
  11528. }
  11529. }
  11530. // (size:int):uint
  11531. ;
  11532. _proto.readBits = function readBits(size) {
  11533. var bits = Math.min(this.bitsAvailable, size); // :uint
  11534. var valu = this.word >>> 32 - bits; // :uint
  11535. if (size > 32) {
  11536. logger.error('Cannot read more than 32 bits at a time');
  11537. }
  11538. this.bitsAvailable -= bits;
  11539. if (this.bitsAvailable > 0) {
  11540. this.word <<= bits;
  11541. } else if (this.bytesAvailable > 0) {
  11542. this.loadWord();
  11543. } else {
  11544. throw new Error('no bits available');
  11545. }
  11546. bits = size - bits;
  11547. if (bits > 0 && this.bitsAvailable) {
  11548. return valu << bits | this.readBits(bits);
  11549. } else {
  11550. return valu;
  11551. }
  11552. }
  11553. // ():uint
  11554. ;
  11555. _proto.skipLZ = function skipLZ() {
  11556. var leadingZeroCount; // :uint
  11557. for (leadingZeroCount = 0; leadingZeroCount < this.bitsAvailable; ++leadingZeroCount) {
  11558. if ((this.word & 0x80000000 >>> leadingZeroCount) !== 0) {
  11559. // the first bit of working word is 1
  11560. this.word <<= leadingZeroCount;
  11561. this.bitsAvailable -= leadingZeroCount;
  11562. return leadingZeroCount;
  11563. }
  11564. }
  11565. // we exhausted word and still have not found a 1
  11566. this.loadWord();
  11567. return leadingZeroCount + this.skipLZ();
  11568. }
  11569. // ():void
  11570. ;
  11571. _proto.skipUEG = function skipUEG() {
  11572. this.skipBits(1 + this.skipLZ());
  11573. }
  11574. // ():void
  11575. ;
  11576. _proto.skipEG = function skipEG() {
  11577. this.skipBits(1 + this.skipLZ());
  11578. }
  11579. // ():uint
  11580. ;
  11581. _proto.readUEG = function readUEG() {
  11582. var clz = this.skipLZ(); // :uint
  11583. return this.readBits(clz + 1) - 1;
  11584. }
  11585. // ():int
  11586. ;
  11587. _proto.readEG = function readEG() {
  11588. var valu = this.readUEG(); // :int
  11589. if (0x01 & valu) {
  11590. // the number is odd if the low order bit is set
  11591. return 1 + valu >>> 1; // add 1 to make it even, and divide by 2
  11592. } else {
  11593. return -1 * (valu >>> 1); // divide by two then make it negative
  11594. }
  11595. }
  11596. // Some convenience functions
  11597. // :Boolean
  11598. ;
  11599. _proto.readBoolean = function readBoolean() {
  11600. return this.readBits(1) === 1;
  11601. }
  11602. // ():int
  11603. ;
  11604. _proto.readUByte = function readUByte() {
  11605. return this.readBits(8);
  11606. }
  11607. // ():int
  11608. ;
  11609. _proto.readUShort = function readUShort() {
  11610. return this.readBits(16);
  11611. }
  11612. // ():int
  11613. ;
  11614. _proto.readUInt = function readUInt() {
  11615. return this.readBits(32);
  11616. }
  11617. /**
  11618. * Advance the ExpGolomb decoder past a scaling list. The scaling
  11619. * list is optionally transmitted as part of a sequence parameter
  11620. * set and is not relevant to transmuxing.
  11621. * @param count the number of entries in this scaling list
  11622. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  11623. */;
  11624. _proto.skipScalingList = function skipScalingList(count) {
  11625. var lastScale = 8;
  11626. var nextScale = 8;
  11627. var deltaScale;
  11628. for (var j = 0; j < count; j++) {
  11629. if (nextScale !== 0) {
  11630. deltaScale = this.readEG();
  11631. nextScale = (lastScale + deltaScale + 256) % 256;
  11632. }
  11633. lastScale = nextScale === 0 ? lastScale : nextScale;
  11634. }
  11635. }
  11636. /**
  11637. * Read a sequence parameter set and return some interesting video
  11638. * properties. A sequence parameter set is the H264 metadata that
  11639. * describes the properties of upcoming video frames.
  11640. * @returns an object with configuration parsed from the
  11641. * sequence parameter set, including the dimensions of the
  11642. * associated video frames.
  11643. */;
  11644. _proto.readSPS = function readSPS() {
  11645. var frameCropLeftOffset = 0;
  11646. var frameCropRightOffset = 0;
  11647. var frameCropTopOffset = 0;
  11648. var frameCropBottomOffset = 0;
  11649. var numRefFramesInPicOrderCntCycle;
  11650. var scalingListCount;
  11651. var i;
  11652. var readUByte = this.readUByte.bind(this);
  11653. var readBits = this.readBits.bind(this);
  11654. var readUEG = this.readUEG.bind(this);
  11655. var readBoolean = this.readBoolean.bind(this);
  11656. var skipBits = this.skipBits.bind(this);
  11657. var skipEG = this.skipEG.bind(this);
  11658. var skipUEG = this.skipUEG.bind(this);
  11659. var skipScalingList = this.skipScalingList.bind(this);
  11660. readUByte();
  11661. var profileIdc = readUByte(); // profile_idc
  11662. readBits(5); // profileCompat constraint_set[0-4]_flag, u(5)
  11663. skipBits(3); // reserved_zero_3bits u(3),
  11664. readUByte(); // level_idc u(8)
  11665. skipUEG(); // seq_parameter_set_id
  11666. // some profiles have more optional data we don't need
  11667. if (profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || profileIdc === 44 || profileIdc === 83 || profileIdc === 86 || profileIdc === 118 || profileIdc === 128) {
  11668. var chromaFormatIdc = readUEG();
  11669. if (chromaFormatIdc === 3) {
  11670. skipBits(1);
  11671. } // separate_colour_plane_flag
  11672. skipUEG(); // bit_depth_luma_minus8
  11673. skipUEG(); // bit_depth_chroma_minus8
  11674. skipBits(1); // qpprime_y_zero_transform_bypass_flag
  11675. if (readBoolean()) {
  11676. // seq_scaling_matrix_present_flag
  11677. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  11678. for (i = 0; i < scalingListCount; i++) {
  11679. if (readBoolean()) {
  11680. // seq_scaling_list_present_flag[ i ]
  11681. if (i < 6) {
  11682. skipScalingList(16);
  11683. } else {
  11684. skipScalingList(64);
  11685. }
  11686. }
  11687. }
  11688. }
  11689. }
  11690. skipUEG(); // log2_max_frame_num_minus4
  11691. var picOrderCntType = readUEG();
  11692. if (picOrderCntType === 0) {
  11693. readUEG(); // log2_max_pic_order_cnt_lsb_minus4
  11694. } else if (picOrderCntType === 1) {
  11695. skipBits(1); // delta_pic_order_always_zero_flag
  11696. skipEG(); // offset_for_non_ref_pic
  11697. skipEG(); // offset_for_top_to_bottom_field
  11698. numRefFramesInPicOrderCntCycle = readUEG();
  11699. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  11700. skipEG();
  11701. } // offset_for_ref_frame[ i ]
  11702. }
  11703. skipUEG(); // max_num_ref_frames
  11704. skipBits(1); // gaps_in_frame_num_value_allowed_flag
  11705. var picWidthInMbsMinus1 = readUEG();
  11706. var picHeightInMapUnitsMinus1 = readUEG();
  11707. var frameMbsOnlyFlag = readBits(1);
  11708. if (frameMbsOnlyFlag === 0) {
  11709. skipBits(1);
  11710. } // mb_adaptive_frame_field_flag
  11711. skipBits(1); // direct_8x8_inference_flag
  11712. if (readBoolean()) {
  11713. // frame_cropping_flag
  11714. frameCropLeftOffset = readUEG();
  11715. frameCropRightOffset = readUEG();
  11716. frameCropTopOffset = readUEG();
  11717. frameCropBottomOffset = readUEG();
  11718. }
  11719. var pixelRatio = [1, 1];
  11720. if (readBoolean()) {
  11721. // vui_parameters_present_flag
  11722. if (readBoolean()) {
  11723. // aspect_ratio_info_present_flag
  11724. var aspectRatioIdc = readUByte();
  11725. switch (aspectRatioIdc) {
  11726. case 1:
  11727. pixelRatio = [1, 1];
  11728. break;
  11729. case 2:
  11730. pixelRatio = [12, 11];
  11731. break;
  11732. case 3:
  11733. pixelRatio = [10, 11];
  11734. break;
  11735. case 4:
  11736. pixelRatio = [16, 11];
  11737. break;
  11738. case 5:
  11739. pixelRatio = [40, 33];
  11740. break;
  11741. case 6:
  11742. pixelRatio = [24, 11];
  11743. break;
  11744. case 7:
  11745. pixelRatio = [20, 11];
  11746. break;
  11747. case 8:
  11748. pixelRatio = [32, 11];
  11749. break;
  11750. case 9:
  11751. pixelRatio = [80, 33];
  11752. break;
  11753. case 10:
  11754. pixelRatio = [18, 11];
  11755. break;
  11756. case 11:
  11757. pixelRatio = [15, 11];
  11758. break;
  11759. case 12:
  11760. pixelRatio = [64, 33];
  11761. break;
  11762. case 13:
  11763. pixelRatio = [160, 99];
  11764. break;
  11765. case 14:
  11766. pixelRatio = [4, 3];
  11767. break;
  11768. case 15:
  11769. pixelRatio = [3, 2];
  11770. break;
  11771. case 16:
  11772. pixelRatio = [2, 1];
  11773. break;
  11774. case 255:
  11775. {
  11776. pixelRatio = [readUByte() << 8 | readUByte(), readUByte() << 8 | readUByte()];
  11777. break;
  11778. }
  11779. }
  11780. }
  11781. }
  11782. return {
  11783. width: Math.ceil((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2),
  11784. height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - (frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset),
  11785. pixelRatio: pixelRatio
  11786. };
  11787. };
  11788. _proto.readSliceType = function readSliceType() {
  11789. // skip NALu type
  11790. this.readUByte();
  11791. // discard first_mb_in_slice
  11792. this.readUEG();
  11793. // return slice_type
  11794. return this.readUEG();
  11795. };
  11796. return ExpGolomb;
  11797. }();
  11798. var AvcVideoParser = /*#__PURE__*/function (_BaseVideoParser) {
  11799. _inheritsLoose(AvcVideoParser, _BaseVideoParser);
  11800. function AvcVideoParser() {
  11801. return _BaseVideoParser.apply(this, arguments) || this;
  11802. }
  11803. var _proto = AvcVideoParser.prototype;
  11804. _proto.parseAVCPES = function parseAVCPES(track, textTrack, pes, last, duration) {
  11805. var _this = this;
  11806. var units = this.parseAVCNALu(track, pes.data);
  11807. var VideoSample = this.VideoSample;
  11808. var push;
  11809. var spsfound = false;
  11810. // free pes.data to save up some memory
  11811. pes.data = null;
  11812. // if new NAL units found and last sample still there, let's push ...
  11813. // this helps parsing streams with missing AUD (only do this if AUD never found)
  11814. if (VideoSample && units.length && !track.audFound) {
  11815. this.pushAccessUnit(VideoSample, track);
  11816. VideoSample = this.VideoSample = this.createVideoSample(false, pes.pts, pes.dts, '');
  11817. }
  11818. units.forEach(function (unit) {
  11819. var _VideoSample2;
  11820. switch (unit.type) {
  11821. // NDR
  11822. case 1:
  11823. {
  11824. var iskey = false;
  11825. push = true;
  11826. var data = unit.data;
  11827. // only check slice type to detect KF in case SPS found in same packet (any keyframe is preceded by SPS ...)
  11828. if (spsfound && data.length > 4) {
  11829. // retrieve slice type by parsing beginning of NAL unit (follow H264 spec, slice_header definition) to detect keyframe embedded in NDR
  11830. var sliceType = new ExpGolomb(data).readSliceType();
  11831. // 2 : I slice, 4 : SI slice, 7 : I slice, 9: SI slice
  11832. // SI slice : A slice that is coded using intra prediction only and using quantisation of the prediction samples.
  11833. // An SI slice can be coded such that its decoded samples can be constructed identically to an SP slice.
  11834. // I slice: A slice that is not an SI slice that is decoded using intra prediction only.
  11835. // if (sliceType === 2 || sliceType === 7) {
  11836. if (sliceType === 2 || sliceType === 4 || sliceType === 7 || sliceType === 9) {
  11837. iskey = true;
  11838. }
  11839. }
  11840. if (iskey) {
  11841. var _VideoSample;
  11842. // if we have non-keyframe data already, that cannot belong to the same frame as a keyframe, so force a push
  11843. if ((_VideoSample = VideoSample) != null && _VideoSample.frame && !VideoSample.key) {
  11844. _this.pushAccessUnit(VideoSample, track);
  11845. VideoSample = _this.VideoSample = null;
  11846. }
  11847. }
  11848. if (!VideoSample) {
  11849. VideoSample = _this.VideoSample = _this.createVideoSample(true, pes.pts, pes.dts, '');
  11850. }
  11851. VideoSample.frame = true;
  11852. VideoSample.key = iskey;
  11853. break;
  11854. // IDR
  11855. }
  11856. case 5:
  11857. push = true;
  11858. // handle PES not starting with AUD
  11859. // if we have frame data already, that cannot belong to the same frame, so force a push
  11860. if ((_VideoSample2 = VideoSample) != null && _VideoSample2.frame && !VideoSample.key) {
  11861. _this.pushAccessUnit(VideoSample, track);
  11862. VideoSample = _this.VideoSample = null;
  11863. }
  11864. if (!VideoSample) {
  11865. VideoSample = _this.VideoSample = _this.createVideoSample(true, pes.pts, pes.dts, '');
  11866. }
  11867. VideoSample.key = true;
  11868. VideoSample.frame = true;
  11869. break;
  11870. // SEI
  11871. case 6:
  11872. {
  11873. push = true;
  11874. parseSEIMessageFromNALu(unit.data, 1, pes.pts, textTrack.samples);
  11875. break;
  11876. // SPS
  11877. }
  11878. case 7:
  11879. {
  11880. var _track$pixelRatio, _track$pixelRatio2;
  11881. push = true;
  11882. spsfound = true;
  11883. var sps = unit.data;
  11884. var expGolombDecoder = new ExpGolomb(sps);
  11885. var config = expGolombDecoder.readSPS();
  11886. if (!track.sps || track.width !== config.width || track.height !== config.height || ((_track$pixelRatio = track.pixelRatio) == null ? void 0 : _track$pixelRatio[0]) !== config.pixelRatio[0] || ((_track$pixelRatio2 = track.pixelRatio) == null ? void 0 : _track$pixelRatio2[1]) !== config.pixelRatio[1]) {
  11887. track.width = config.width;
  11888. track.height = config.height;
  11889. track.pixelRatio = config.pixelRatio;
  11890. track.sps = [sps];
  11891. track.duration = duration;
  11892. var codecarray = sps.subarray(1, 4);
  11893. var codecstring = 'avc1.';
  11894. for (var i = 0; i < 3; i++) {
  11895. var h = codecarray[i].toString(16);
  11896. if (h.length < 2) {
  11897. h = '0' + h;
  11898. }
  11899. codecstring += h;
  11900. }
  11901. track.codec = codecstring;
  11902. }
  11903. break;
  11904. }
  11905. // PPS
  11906. case 8:
  11907. push = true;
  11908. track.pps = [unit.data];
  11909. break;
  11910. // AUD
  11911. case 9:
  11912. push = true;
  11913. track.audFound = true;
  11914. if (VideoSample) {
  11915. _this.pushAccessUnit(VideoSample, track);
  11916. }
  11917. VideoSample = _this.VideoSample = _this.createVideoSample(false, pes.pts, pes.dts, '');
  11918. break;
  11919. // Filler Data
  11920. case 12:
  11921. push = true;
  11922. break;
  11923. default:
  11924. push = false;
  11925. if (VideoSample) {
  11926. VideoSample.debug += 'unknown NAL ' + unit.type + ' ';
  11927. }
  11928. break;
  11929. }
  11930. if (VideoSample && push) {
  11931. var _units = VideoSample.units;
  11932. _units.push(unit);
  11933. }
  11934. });
  11935. // if last PES packet, push samples
  11936. if (last && VideoSample) {
  11937. this.pushAccessUnit(VideoSample, track);
  11938. this.VideoSample = null;
  11939. }
  11940. };
  11941. _proto.parseAVCNALu = function parseAVCNALu(track, array) {
  11942. var len = array.byteLength;
  11943. var state = track.naluState || 0;
  11944. var lastState = state;
  11945. var units = [];
  11946. var i = 0;
  11947. var value;
  11948. var overflow;
  11949. var unitType;
  11950. var lastUnitStart = -1;
  11951. var lastUnitType = 0;
  11952. // logger.log('PES:' + Hex.hexDump(array));
  11953. if (state === -1) {
  11954. // special use case where we found 3 or 4-byte start codes exactly at the end of previous PES packet
  11955. lastUnitStart = 0;
  11956. // NALu type is value read from offset 0
  11957. lastUnitType = array[0] & 0x1f;
  11958. state = 0;
  11959. i = 1;
  11960. }
  11961. while (i < len) {
  11962. value = array[i++];
  11963. // optimization. state 0 and 1 are the predominant case. let's handle them outside of the switch/case
  11964. if (!state) {
  11965. state = value ? 0 : 1;
  11966. continue;
  11967. }
  11968. if (state === 1) {
  11969. state = value ? 0 : 2;
  11970. continue;
  11971. }
  11972. // here we have state either equal to 2 or 3
  11973. if (!value) {
  11974. state = 3;
  11975. } else if (value === 1) {
  11976. overflow = i - state - 1;
  11977. if (lastUnitStart >= 0) {
  11978. var unit = {
  11979. data: array.subarray(lastUnitStart, overflow),
  11980. type: lastUnitType
  11981. };
  11982. // logger.log('pushing NALU, type/size:' + unit.type + '/' + unit.data.byteLength);
  11983. units.push(unit);
  11984. } else {
  11985. // lastUnitStart is undefined => this is the first start code found in this PES packet
  11986. // first check if start code delimiter is overlapping between 2 PES packets,
  11987. // ie it started in last packet (lastState not zero)
  11988. // and ended at the beginning of this PES packet (i <= 4 - lastState)
  11989. var lastUnit = this.getLastNalUnit(track.samples);
  11990. if (lastUnit) {
  11991. if (lastState && i <= 4 - lastState) {
  11992. // start delimiter overlapping between PES packets
  11993. // strip start delimiter bytes from the end of last NAL unit
  11994. // check if lastUnit had a state different from zero
  11995. if (lastUnit.state) {
  11996. // strip last bytes
  11997. lastUnit.data = lastUnit.data.subarray(0, lastUnit.data.byteLength - lastState);
  11998. }
  11999. }
  12000. // If NAL units are not starting right at the beginning of the PES packet, push preceding data into previous NAL unit.
  12001. if (overflow > 0) {
  12002. // logger.log('first NALU found with overflow:' + overflow);
  12003. lastUnit.data = appendUint8Array(lastUnit.data, array.subarray(0, overflow));
  12004. lastUnit.state = 0;
  12005. }
  12006. }
  12007. }
  12008. // check if we can read unit type
  12009. if (i < len) {
  12010. unitType = array[i] & 0x1f;
  12011. // logger.log('find NALU @ offset:' + i + ',type:' + unitType);
  12012. lastUnitStart = i;
  12013. lastUnitType = unitType;
  12014. state = 0;
  12015. } else {
  12016. // not enough byte to read unit type. let's read it on next PES parsing
  12017. state = -1;
  12018. }
  12019. } else {
  12020. state = 0;
  12021. }
  12022. }
  12023. if (lastUnitStart >= 0 && state >= 0) {
  12024. var _unit = {
  12025. data: array.subarray(lastUnitStart, len),
  12026. type: lastUnitType,
  12027. state: state
  12028. };
  12029. units.push(_unit);
  12030. // logger.log('pushing NALU, type/size/state:' + unit.type + '/' + unit.data.byteLength + '/' + state);
  12031. }
  12032. // no NALu found
  12033. if (units.length === 0) {
  12034. // append pes.data to previous NAL unit
  12035. var _lastUnit = this.getLastNalUnit(track.samples);
  12036. if (_lastUnit) {
  12037. _lastUnit.data = appendUint8Array(_lastUnit.data, array);
  12038. }
  12039. }
  12040. track.naluState = state;
  12041. return units;
  12042. };
  12043. return AvcVideoParser;
  12044. }(BaseVideoParser);
  12045. /**
  12046. * SAMPLE-AES decrypter
  12047. */
  12048. var SampleAesDecrypter = /*#__PURE__*/function () {
  12049. function SampleAesDecrypter(observer, config, keyData) {
  12050. this.keyData = void 0;
  12051. this.decrypter = void 0;
  12052. this.keyData = keyData;
  12053. this.decrypter = new Decrypter(config, {
  12054. removePKCS7Padding: false
  12055. });
  12056. }
  12057. var _proto = SampleAesDecrypter.prototype;
  12058. _proto.decryptBuffer = function decryptBuffer(encryptedData) {
  12059. return this.decrypter.decrypt(encryptedData, this.keyData.key.buffer, this.keyData.iv.buffer);
  12060. }
  12061. // AAC - encrypt all full 16 bytes blocks starting from offset 16
  12062. ;
  12063. _proto.decryptAacSample = function decryptAacSample(samples, sampleIndex, callback) {
  12064. var _this = this;
  12065. var curUnit = samples[sampleIndex].unit;
  12066. if (curUnit.length <= 16) {
  12067. // No encrypted portion in this sample (first 16 bytes is not
  12068. // encrypted, see https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/HLS_Sample_Encryption/Encryption/Encryption.html),
  12069. return;
  12070. }
  12071. var encryptedData = curUnit.subarray(16, curUnit.length - curUnit.length % 16);
  12072. var encryptedBuffer = encryptedData.buffer.slice(encryptedData.byteOffset, encryptedData.byteOffset + encryptedData.length);
  12073. this.decryptBuffer(encryptedBuffer).then(function (decryptedBuffer) {
  12074. var decryptedData = new Uint8Array(decryptedBuffer);
  12075. curUnit.set(decryptedData, 16);
  12076. if (!_this.decrypter.isSync()) {
  12077. _this.decryptAacSamples(samples, sampleIndex + 1, callback);
  12078. }
  12079. });
  12080. };
  12081. _proto.decryptAacSamples = function decryptAacSamples(samples, sampleIndex, callback) {
  12082. for (;; sampleIndex++) {
  12083. if (sampleIndex >= samples.length) {
  12084. callback();
  12085. return;
  12086. }
  12087. if (samples[sampleIndex].unit.length < 32) {
  12088. continue;
  12089. }
  12090. this.decryptAacSample(samples, sampleIndex, callback);
  12091. if (!this.decrypter.isSync()) {
  12092. return;
  12093. }
  12094. }
  12095. }
  12096. // AVC - encrypt one 16 bytes block out of ten, starting from offset 32
  12097. ;
  12098. _proto.getAvcEncryptedData = function getAvcEncryptedData(decodedData) {
  12099. var encryptedDataLen = Math.floor((decodedData.length - 48) / 160) * 16 + 16;
  12100. var encryptedData = new Int8Array(encryptedDataLen);
  12101. var outputPos = 0;
  12102. for (var inputPos = 32; inputPos < decodedData.length - 16; inputPos += 160, outputPos += 16) {
  12103. encryptedData.set(decodedData.subarray(inputPos, inputPos + 16), outputPos);
  12104. }
  12105. return encryptedData;
  12106. };
  12107. _proto.getAvcDecryptedUnit = function getAvcDecryptedUnit(decodedData, decryptedData) {
  12108. var uint8DecryptedData = new Uint8Array(decryptedData);
  12109. var inputPos = 0;
  12110. for (var outputPos = 32; outputPos < decodedData.length - 16; outputPos += 160, inputPos += 16) {
  12111. decodedData.set(uint8DecryptedData.subarray(inputPos, inputPos + 16), outputPos);
  12112. }
  12113. return decodedData;
  12114. };
  12115. _proto.decryptAvcSample = function decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit) {
  12116. var _this2 = this;
  12117. var decodedData = discardEPB(curUnit.data);
  12118. var encryptedData = this.getAvcEncryptedData(decodedData);
  12119. this.decryptBuffer(encryptedData.buffer).then(function (decryptedBuffer) {
  12120. curUnit.data = _this2.getAvcDecryptedUnit(decodedData, decryptedBuffer);
  12121. if (!_this2.decrypter.isSync()) {
  12122. _this2.decryptAvcSamples(samples, sampleIndex, unitIndex + 1, callback);
  12123. }
  12124. });
  12125. };
  12126. _proto.decryptAvcSamples = function decryptAvcSamples(samples, sampleIndex, unitIndex, callback) {
  12127. if (samples instanceof Uint8Array) {
  12128. throw new Error('Cannot decrypt samples of type Uint8Array');
  12129. }
  12130. for (;; sampleIndex++, unitIndex = 0) {
  12131. if (sampleIndex >= samples.length) {
  12132. callback();
  12133. return;
  12134. }
  12135. var curUnits = samples[sampleIndex].units;
  12136. for (;; unitIndex++) {
  12137. if (unitIndex >= curUnits.length) {
  12138. break;
  12139. }
  12140. var curUnit = curUnits[unitIndex];
  12141. if (curUnit.data.length <= 48 || curUnit.type !== 1 && curUnit.type !== 5) {
  12142. continue;
  12143. }
  12144. this.decryptAvcSample(samples, sampleIndex, unitIndex, callback, curUnit);
  12145. if (!this.decrypter.isSync()) {
  12146. return;
  12147. }
  12148. }
  12149. }
  12150. };
  12151. return SampleAesDecrypter;
  12152. }();
  12153. var PACKET_LENGTH = 188;
  12154. var TSDemuxer = /*#__PURE__*/function () {
  12155. function TSDemuxer(observer, config, typeSupported) {
  12156. this.observer = void 0;
  12157. this.config = void 0;
  12158. this.typeSupported = void 0;
  12159. this.sampleAes = null;
  12160. this.pmtParsed = false;
  12161. this.audioCodec = void 0;
  12162. this.videoCodec = void 0;
  12163. this._duration = 0;
  12164. this._pmtId = -1;
  12165. this._videoTrack = void 0;
  12166. this._audioTrack = void 0;
  12167. this._id3Track = void 0;
  12168. this._txtTrack = void 0;
  12169. this.aacOverFlow = null;
  12170. this.remainderData = null;
  12171. this.videoParser = void 0;
  12172. this.observer = observer;
  12173. this.config = config;
  12174. this.typeSupported = typeSupported;
  12175. this.videoParser = new AvcVideoParser();
  12176. }
  12177. TSDemuxer.probe = function probe(data) {
  12178. var syncOffset = TSDemuxer.syncOffset(data);
  12179. if (syncOffset > 0) {
  12180. logger.warn("MPEG2-TS detected but first sync word found @ offset " + syncOffset);
  12181. }
  12182. return syncOffset !== -1;
  12183. };
  12184. TSDemuxer.syncOffset = function syncOffset(data) {
  12185. var length = data.length;
  12186. var scanwindow = Math.min(PACKET_LENGTH * 5, length - PACKET_LENGTH) + 1;
  12187. var i = 0;
  12188. while (i < scanwindow) {
  12189. // a TS init segment should contain at least 2 TS packets: PAT and PMT, each starting with 0x47
  12190. var foundPat = false;
  12191. var packetStart = -1;
  12192. var tsPackets = 0;
  12193. for (var j = i; j < length; j += PACKET_LENGTH) {
  12194. if (data[j] === 0x47 && (length - j === PACKET_LENGTH || data[j + PACKET_LENGTH] === 0x47)) {
  12195. tsPackets++;
  12196. if (packetStart === -1) {
  12197. packetStart = j;
  12198. // First sync word found at offset, increase scan length (#5251)
  12199. if (packetStart !== 0) {
  12200. scanwindow = Math.min(packetStart + PACKET_LENGTH * 99, data.length - PACKET_LENGTH) + 1;
  12201. }
  12202. }
  12203. if (!foundPat) {
  12204. foundPat = parsePID(data, j) === 0;
  12205. }
  12206. // Sync word found at 0 with 3 packets, or found at offset least 2 packets up to scanwindow (#5501)
  12207. if (foundPat && tsPackets > 1 && (packetStart === 0 && tsPackets > 2 || j + PACKET_LENGTH > scanwindow)) {
  12208. return packetStart;
  12209. }
  12210. } else if (tsPackets) {
  12211. // Exit if sync word found, but does not contain contiguous packets
  12212. return -1;
  12213. } else {
  12214. break;
  12215. }
  12216. }
  12217. i++;
  12218. }
  12219. return -1;
  12220. }
  12221. /**
  12222. * Creates a track model internal to demuxer used to drive remuxing input
  12223. */;
  12224. TSDemuxer.createTrack = function createTrack(type, duration) {
  12225. return {
  12226. container: type === 'video' || type === 'audio' ? 'video/mp2t' : undefined,
  12227. type: type,
  12228. id: RemuxerTrackIdConfig[type],
  12229. pid: -1,
  12230. inputTimeScale: 90000,
  12231. sequenceNumber: 0,
  12232. samples: [],
  12233. dropped: 0,
  12234. duration: type === 'audio' ? duration : undefined
  12235. };
  12236. }
  12237. /**
  12238. * Initializes a new init segment on the demuxer/remuxer interface. Needed for discontinuities/track-switches (or at stream start)
  12239. * Resets all internal track instances of the demuxer.
  12240. */;
  12241. var _proto = TSDemuxer.prototype;
  12242. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  12243. this.pmtParsed = false;
  12244. this._pmtId = -1;
  12245. this._videoTrack = TSDemuxer.createTrack('video');
  12246. this._audioTrack = TSDemuxer.createTrack('audio', trackDuration);
  12247. this._id3Track = TSDemuxer.createTrack('id3');
  12248. this._txtTrack = TSDemuxer.createTrack('text');
  12249. this._audioTrack.segmentCodec = 'aac';
  12250. // flush any partial content
  12251. this.aacOverFlow = null;
  12252. this.remainderData = null;
  12253. this.audioCodec = audioCodec;
  12254. this.videoCodec = videoCodec;
  12255. this._duration = trackDuration;
  12256. };
  12257. _proto.resetTimeStamp = function resetTimeStamp() {};
  12258. _proto.resetContiguity = function resetContiguity() {
  12259. var _audioTrack = this._audioTrack,
  12260. _videoTrack = this._videoTrack,
  12261. _id3Track = this._id3Track;
  12262. if (_audioTrack) {
  12263. _audioTrack.pesData = null;
  12264. }
  12265. if (_videoTrack) {
  12266. _videoTrack.pesData = null;
  12267. }
  12268. if (_id3Track) {
  12269. _id3Track.pesData = null;
  12270. }
  12271. this.aacOverFlow = null;
  12272. this.remainderData = null;
  12273. };
  12274. _proto.demux = function demux(data, timeOffset, isSampleAes, flush) {
  12275. if (isSampleAes === void 0) {
  12276. isSampleAes = false;
  12277. }
  12278. if (flush === void 0) {
  12279. flush = false;
  12280. }
  12281. if (!isSampleAes) {
  12282. this.sampleAes = null;
  12283. }
  12284. var pes;
  12285. var videoTrack = this._videoTrack;
  12286. var audioTrack = this._audioTrack;
  12287. var id3Track = this._id3Track;
  12288. var textTrack = this._txtTrack;
  12289. var videoPid = videoTrack.pid;
  12290. var videoData = videoTrack.pesData;
  12291. var audioPid = audioTrack.pid;
  12292. var id3Pid = id3Track.pid;
  12293. var audioData = audioTrack.pesData;
  12294. var id3Data = id3Track.pesData;
  12295. var unknownPID = null;
  12296. var pmtParsed = this.pmtParsed;
  12297. var pmtId = this._pmtId;
  12298. var len = data.length;
  12299. if (this.remainderData) {
  12300. data = appendUint8Array(this.remainderData, data);
  12301. len = data.length;
  12302. this.remainderData = null;
  12303. }
  12304. if (len < PACKET_LENGTH && !flush) {
  12305. this.remainderData = data;
  12306. return {
  12307. audioTrack: audioTrack,
  12308. videoTrack: videoTrack,
  12309. id3Track: id3Track,
  12310. textTrack: textTrack
  12311. };
  12312. }
  12313. var syncOffset = Math.max(0, TSDemuxer.syncOffset(data));
  12314. len -= (len - syncOffset) % PACKET_LENGTH;
  12315. if (len < data.byteLength && !flush) {
  12316. this.remainderData = new Uint8Array(data.buffer, len, data.buffer.byteLength - len);
  12317. }
  12318. // loop through TS packets
  12319. var tsPacketErrors = 0;
  12320. for (var start = syncOffset; start < len; start += PACKET_LENGTH) {
  12321. if (data[start] === 0x47) {
  12322. var stt = !!(data[start + 1] & 0x40);
  12323. var pid = parsePID(data, start);
  12324. var atf = (data[start + 3] & 0x30) >> 4;
  12325. // if an adaption field is present, its length is specified by the fifth byte of the TS packet header.
  12326. var offset = void 0;
  12327. if (atf > 1) {
  12328. offset = start + 5 + data[start + 4];
  12329. // continue if there is only adaptation field
  12330. if (offset === start + PACKET_LENGTH) {
  12331. continue;
  12332. }
  12333. } else {
  12334. offset = start + 4;
  12335. }
  12336. switch (pid) {
  12337. case videoPid:
  12338. if (stt) {
  12339. if (videoData && (pes = parsePES(videoData))) {
  12340. this.videoParser.parseAVCPES(videoTrack, textTrack, pes, false, this._duration);
  12341. }
  12342. videoData = {
  12343. data: [],
  12344. size: 0
  12345. };
  12346. }
  12347. if (videoData) {
  12348. videoData.data.push(data.subarray(offset, start + PACKET_LENGTH));
  12349. videoData.size += start + PACKET_LENGTH - offset;
  12350. }
  12351. break;
  12352. case audioPid:
  12353. if (stt) {
  12354. if (audioData && (pes = parsePES(audioData))) {
  12355. switch (audioTrack.segmentCodec) {
  12356. case 'aac':
  12357. this.parseAACPES(audioTrack, pes);
  12358. break;
  12359. case 'mp3':
  12360. this.parseMPEGPES(audioTrack, pes);
  12361. break;
  12362. case 'ac3':
  12363. {
  12364. this.parseAC3PES(audioTrack, pes);
  12365. }
  12366. break;
  12367. }
  12368. }
  12369. audioData = {
  12370. data: [],
  12371. size: 0
  12372. };
  12373. }
  12374. if (audioData) {
  12375. audioData.data.push(data.subarray(offset, start + PACKET_LENGTH));
  12376. audioData.size += start + PACKET_LENGTH - offset;
  12377. }
  12378. break;
  12379. case id3Pid:
  12380. if (stt) {
  12381. if (id3Data && (pes = parsePES(id3Data))) {
  12382. this.parseID3PES(id3Track, pes);
  12383. }
  12384. id3Data = {
  12385. data: [],
  12386. size: 0
  12387. };
  12388. }
  12389. if (id3Data) {
  12390. id3Data.data.push(data.subarray(offset, start + PACKET_LENGTH));
  12391. id3Data.size += start + PACKET_LENGTH - offset;
  12392. }
  12393. break;
  12394. case 0:
  12395. if (stt) {
  12396. offset += data[offset] + 1;
  12397. }
  12398. pmtId = this._pmtId = parsePAT(data, offset);
  12399. // logger.log('PMT PID:' + this._pmtId);
  12400. break;
  12401. case pmtId:
  12402. {
  12403. if (stt) {
  12404. offset += data[offset] + 1;
  12405. }
  12406. var parsedPIDs = parsePMT(data, offset, this.typeSupported, isSampleAes, this.observer);
  12407. // only update track id if track PID found while parsing PMT
  12408. // this is to avoid resetting the PID to -1 in case
  12409. // track PID transiently disappears from the stream
  12410. // this could happen in case of transient missing audio samples for example
  12411. // NOTE this is only the PID of the track as found in TS,
  12412. // but we are not using this for MP4 track IDs.
  12413. videoPid = parsedPIDs.videoPid;
  12414. if (videoPid > 0) {
  12415. videoTrack.pid = videoPid;
  12416. videoTrack.segmentCodec = parsedPIDs.segmentVideoCodec;
  12417. }
  12418. audioPid = parsedPIDs.audioPid;
  12419. if (audioPid > 0) {
  12420. audioTrack.pid = audioPid;
  12421. audioTrack.segmentCodec = parsedPIDs.segmentAudioCodec;
  12422. }
  12423. id3Pid = parsedPIDs.id3Pid;
  12424. if (id3Pid > 0) {
  12425. id3Track.pid = id3Pid;
  12426. }
  12427. if (unknownPID !== null && !pmtParsed) {
  12428. logger.warn("MPEG-TS PMT found at " + start + " after unknown PID '" + unknownPID + "'. Backtracking to sync byte @" + syncOffset + " to parse all TS packets.");
  12429. unknownPID = null;
  12430. // we set it to -188, the += 188 in the for loop will reset start to 0
  12431. start = syncOffset - 188;
  12432. }
  12433. pmtParsed = this.pmtParsed = true;
  12434. break;
  12435. }
  12436. case 0x11:
  12437. case 0x1fff:
  12438. break;
  12439. default:
  12440. unknownPID = pid;
  12441. break;
  12442. }
  12443. } else {
  12444. tsPacketErrors++;
  12445. }
  12446. }
  12447. if (tsPacketErrors > 0) {
  12448. emitParsingError(this.observer, new Error("Found " + tsPacketErrors + " TS packet/s that do not start with 0x47"));
  12449. }
  12450. videoTrack.pesData = videoData;
  12451. audioTrack.pesData = audioData;
  12452. id3Track.pesData = id3Data;
  12453. var demuxResult = {
  12454. audioTrack: audioTrack,
  12455. videoTrack: videoTrack,
  12456. id3Track: id3Track,
  12457. textTrack: textTrack
  12458. };
  12459. if (flush) {
  12460. this.extractRemainingSamples(demuxResult);
  12461. }
  12462. return demuxResult;
  12463. };
  12464. _proto.flush = function flush() {
  12465. var remainderData = this.remainderData;
  12466. this.remainderData = null;
  12467. var result;
  12468. if (remainderData) {
  12469. result = this.demux(remainderData, -1, false, true);
  12470. } else {
  12471. result = {
  12472. videoTrack: this._videoTrack,
  12473. audioTrack: this._audioTrack,
  12474. id3Track: this._id3Track,
  12475. textTrack: this._txtTrack
  12476. };
  12477. }
  12478. this.extractRemainingSamples(result);
  12479. if (this.sampleAes) {
  12480. return this.decrypt(result, this.sampleAes);
  12481. }
  12482. return result;
  12483. };
  12484. _proto.extractRemainingSamples = function extractRemainingSamples(demuxResult) {
  12485. var audioTrack = demuxResult.audioTrack,
  12486. videoTrack = demuxResult.videoTrack,
  12487. id3Track = demuxResult.id3Track,
  12488. textTrack = demuxResult.textTrack;
  12489. var videoData = videoTrack.pesData;
  12490. var audioData = audioTrack.pesData;
  12491. var id3Data = id3Track.pesData;
  12492. // try to parse last PES packets
  12493. var pes;
  12494. if (videoData && (pes = parsePES(videoData))) {
  12495. this.videoParser.parseAVCPES(videoTrack, textTrack, pes, true, this._duration);
  12496. videoTrack.pesData = null;
  12497. } else {
  12498. // either avcData null or PES truncated, keep it for next frag parsing
  12499. videoTrack.pesData = videoData;
  12500. }
  12501. if (audioData && (pes = parsePES(audioData))) {
  12502. switch (audioTrack.segmentCodec) {
  12503. case 'aac':
  12504. this.parseAACPES(audioTrack, pes);
  12505. break;
  12506. case 'mp3':
  12507. this.parseMPEGPES(audioTrack, pes);
  12508. break;
  12509. case 'ac3':
  12510. {
  12511. this.parseAC3PES(audioTrack, pes);
  12512. }
  12513. break;
  12514. }
  12515. audioTrack.pesData = null;
  12516. } else {
  12517. if (audioData != null && audioData.size) {
  12518. logger.log('last AAC PES packet truncated,might overlap between fragments');
  12519. }
  12520. // either audioData null or PES truncated, keep it for next frag parsing
  12521. audioTrack.pesData = audioData;
  12522. }
  12523. if (id3Data && (pes = parsePES(id3Data))) {
  12524. this.parseID3PES(id3Track, pes);
  12525. id3Track.pesData = null;
  12526. } else {
  12527. // either id3Data null or PES truncated, keep it for next frag parsing
  12528. id3Track.pesData = id3Data;
  12529. }
  12530. };
  12531. _proto.demuxSampleAes = function demuxSampleAes(data, keyData, timeOffset) {
  12532. var demuxResult = this.demux(data, timeOffset, true, !this.config.progressive);
  12533. var sampleAes = this.sampleAes = new SampleAesDecrypter(this.observer, this.config, keyData);
  12534. return this.decrypt(demuxResult, sampleAes);
  12535. };
  12536. _proto.decrypt = function decrypt(demuxResult, sampleAes) {
  12537. return new Promise(function (resolve) {
  12538. var audioTrack = demuxResult.audioTrack,
  12539. videoTrack = demuxResult.videoTrack;
  12540. if (audioTrack.samples && audioTrack.segmentCodec === 'aac') {
  12541. sampleAes.decryptAacSamples(audioTrack.samples, 0, function () {
  12542. if (videoTrack.samples) {
  12543. sampleAes.decryptAvcSamples(videoTrack.samples, 0, 0, function () {
  12544. resolve(demuxResult);
  12545. });
  12546. } else {
  12547. resolve(demuxResult);
  12548. }
  12549. });
  12550. } else if (videoTrack.samples) {
  12551. sampleAes.decryptAvcSamples(videoTrack.samples, 0, 0, function () {
  12552. resolve(demuxResult);
  12553. });
  12554. }
  12555. });
  12556. };
  12557. _proto.destroy = function destroy() {
  12558. this._duration = 0;
  12559. };
  12560. _proto.parseAACPES = function parseAACPES(track, pes) {
  12561. var startOffset = 0;
  12562. var aacOverFlow = this.aacOverFlow;
  12563. var data = pes.data;
  12564. if (aacOverFlow) {
  12565. this.aacOverFlow = null;
  12566. var frameMissingBytes = aacOverFlow.missing;
  12567. var sampleLength = aacOverFlow.sample.unit.byteLength;
  12568. // logger.log(`AAC: append overflowing ${sampleLength} bytes to beginning of new PES`);
  12569. if (frameMissingBytes === -1) {
  12570. data = appendUint8Array(aacOverFlow.sample.unit, data);
  12571. } else {
  12572. var frameOverflowBytes = sampleLength - frameMissingBytes;
  12573. aacOverFlow.sample.unit.set(data.subarray(0, frameMissingBytes), frameOverflowBytes);
  12574. track.samples.push(aacOverFlow.sample);
  12575. startOffset = aacOverFlow.missing;
  12576. }
  12577. }
  12578. // look for ADTS header (0xFFFx)
  12579. var offset;
  12580. var len;
  12581. for (offset = startOffset, len = data.length; offset < len - 1; offset++) {
  12582. if (isHeader$1(data, offset)) {
  12583. break;
  12584. }
  12585. }
  12586. // if ADTS header does not start straight from the beginning of the PES payload, raise an error
  12587. if (offset !== startOffset) {
  12588. var reason;
  12589. var recoverable = offset < len - 1;
  12590. if (recoverable) {
  12591. reason = "AAC PES did not start with ADTS header,offset:" + offset;
  12592. } else {
  12593. reason = 'No ADTS header found in AAC PES';
  12594. }
  12595. emitParsingError(this.observer, new Error(reason), recoverable);
  12596. if (!recoverable) {
  12597. return;
  12598. }
  12599. }
  12600. initTrackConfig(track, this.observer, data, offset, this.audioCodec);
  12601. var pts;
  12602. if (pes.pts !== undefined) {
  12603. pts = pes.pts;
  12604. } else if (aacOverFlow) {
  12605. // if last AAC frame is overflowing, we should ensure timestamps are contiguous:
  12606. // first sample PTS should be equal to last sample PTS + frameDuration
  12607. var frameDuration = getFrameDuration(track.samplerate);
  12608. pts = aacOverFlow.sample.pts + frameDuration;
  12609. } else {
  12610. logger.warn('[tsdemuxer]: AAC PES unknown PTS');
  12611. return;
  12612. }
  12613. // scan for aac samples
  12614. var frameIndex = 0;
  12615. var frame;
  12616. while (offset < len) {
  12617. frame = appendFrame$1(track, data, offset, pts, frameIndex);
  12618. offset += frame.length;
  12619. if (!frame.missing) {
  12620. frameIndex++;
  12621. for (; offset < len - 1; offset++) {
  12622. if (isHeader$1(data, offset)) {
  12623. break;
  12624. }
  12625. }
  12626. } else {
  12627. this.aacOverFlow = frame;
  12628. break;
  12629. }
  12630. }
  12631. };
  12632. _proto.parseMPEGPES = function parseMPEGPES(track, pes) {
  12633. var data = pes.data;
  12634. var length = data.length;
  12635. var frameIndex = 0;
  12636. var offset = 0;
  12637. var pts = pes.pts;
  12638. if (pts === undefined) {
  12639. logger.warn('[tsdemuxer]: MPEG PES unknown PTS');
  12640. return;
  12641. }
  12642. while (offset < length) {
  12643. if (isHeader(data, offset)) {
  12644. var frame = appendFrame(track, data, offset, pts, frameIndex);
  12645. if (frame) {
  12646. offset += frame.length;
  12647. frameIndex++;
  12648. } else {
  12649. // logger.log('Unable to parse Mpeg audio frame');
  12650. break;
  12651. }
  12652. } else {
  12653. // nothing found, keep looking
  12654. offset++;
  12655. }
  12656. }
  12657. };
  12658. _proto.parseAC3PES = function parseAC3PES(track, pes) {
  12659. {
  12660. var data = pes.data;
  12661. var pts = pes.pts;
  12662. if (pts === undefined) {
  12663. logger.warn('[tsdemuxer]: AC3 PES unknown PTS');
  12664. return;
  12665. }
  12666. var length = data.length;
  12667. var frameIndex = 0;
  12668. var offset = 0;
  12669. var parsed;
  12670. while (offset < length && (parsed = _appendFrame(track, data, offset, pts, frameIndex++)) > 0) {
  12671. offset += parsed;
  12672. }
  12673. }
  12674. };
  12675. _proto.parseID3PES = function parseID3PES(id3Track, pes) {
  12676. if (pes.pts === undefined) {
  12677. logger.warn('[tsdemuxer]: ID3 PES unknown PTS');
  12678. return;
  12679. }
  12680. var id3Sample = _extends({}, pes, {
  12681. type: this._videoTrack ? MetadataSchema.emsg : MetadataSchema.audioId3,
  12682. duration: Number.POSITIVE_INFINITY
  12683. });
  12684. id3Track.samples.push(id3Sample);
  12685. };
  12686. return TSDemuxer;
  12687. }();
  12688. function parsePID(data, offset) {
  12689. // pid is a 13-bit field starting at the last bit of TS[1]
  12690. return ((data[offset + 1] & 0x1f) << 8) + data[offset + 2];
  12691. }
  12692. function parsePAT(data, offset) {
  12693. // skip the PSI header and parse the first PMT entry
  12694. return (data[offset + 10] & 0x1f) << 8 | data[offset + 11];
  12695. }
  12696. function parsePMT(data, offset, typeSupported, isSampleAes, observer) {
  12697. var result = {
  12698. audioPid: -1,
  12699. videoPid: -1,
  12700. id3Pid: -1,
  12701. segmentVideoCodec: 'avc',
  12702. segmentAudioCodec: 'aac'
  12703. };
  12704. var sectionLength = (data[offset + 1] & 0x0f) << 8 | data[offset + 2];
  12705. var tableEnd = offset + 3 + sectionLength - 4;
  12706. // to determine where the table is, we have to figure out how
  12707. // long the program info descriptors are
  12708. var programInfoLength = (data[offset + 10] & 0x0f) << 8 | data[offset + 11];
  12709. // advance the offset to the first entry in the mapping table
  12710. offset += 12 + programInfoLength;
  12711. while (offset < tableEnd) {
  12712. var pid = parsePID(data, offset);
  12713. var esInfoLength = (data[offset + 3] & 0x0f) << 8 | data[offset + 4];
  12714. switch (data[offset]) {
  12715. case 0xcf:
  12716. // SAMPLE-AES AAC
  12717. if (!isSampleAes) {
  12718. logEncryptedSamplesFoundInUnencryptedStream('ADTS AAC');
  12719. break;
  12720. }
  12721. /* falls through */
  12722. case 0x0f:
  12723. // ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio)
  12724. // logger.log('AAC PID:' + pid);
  12725. if (result.audioPid === -1) {
  12726. result.audioPid = pid;
  12727. }
  12728. break;
  12729. // Packetized metadata (ID3)
  12730. case 0x15:
  12731. // logger.log('ID3 PID:' + pid);
  12732. if (result.id3Pid === -1) {
  12733. result.id3Pid = pid;
  12734. }
  12735. break;
  12736. case 0xdb:
  12737. // SAMPLE-AES AVC
  12738. if (!isSampleAes) {
  12739. logEncryptedSamplesFoundInUnencryptedStream('H.264');
  12740. break;
  12741. }
  12742. /* falls through */
  12743. case 0x1b:
  12744. // ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video)
  12745. // logger.log('AVC PID:' + pid);
  12746. if (result.videoPid === -1) {
  12747. result.videoPid = pid;
  12748. result.segmentVideoCodec = 'avc';
  12749. }
  12750. break;
  12751. // ISO/IEC 11172-3 (MPEG-1 audio)
  12752. // or ISO/IEC 13818-3 (MPEG-2 halved sample rate audio)
  12753. case 0x03:
  12754. case 0x04:
  12755. // logger.log('MPEG PID:' + pid);
  12756. if (!typeSupported.mpeg && !typeSupported.mp3) {
  12757. logger.log('MPEG audio found, not supported in this browser');
  12758. } else if (result.audioPid === -1) {
  12759. result.audioPid = pid;
  12760. result.segmentAudioCodec = 'mp3';
  12761. }
  12762. break;
  12763. case 0xc1:
  12764. // SAMPLE-AES AC3
  12765. if (!isSampleAes) {
  12766. logEncryptedSamplesFoundInUnencryptedStream('AC-3');
  12767. break;
  12768. }
  12769. /* falls through */
  12770. case 0x81:
  12771. {
  12772. if (!typeSupported.ac3) {
  12773. logger.log('AC-3 audio found, not supported in this browser');
  12774. } else if (result.audioPid === -1) {
  12775. result.audioPid = pid;
  12776. result.segmentAudioCodec = 'ac3';
  12777. }
  12778. }
  12779. break;
  12780. case 0x06:
  12781. // stream_type 6 can mean a lot of different things in case of DVB.
  12782. // We need to look at the descriptors. Right now, we're only interested
  12783. // in AC-3 audio, so we do the descriptor parsing only when we don't have
  12784. // an audio PID yet.
  12785. if (result.audioPid === -1 && esInfoLength > 0) {
  12786. var parsePos = offset + 5;
  12787. var remaining = esInfoLength;
  12788. while (remaining > 2) {
  12789. var descriptorId = data[parsePos];
  12790. switch (descriptorId) {
  12791. case 0x6a:
  12792. // DVB Descriptor for AC-3
  12793. {
  12794. if (typeSupported.ac3 !== true) {
  12795. logger.log('AC-3 audio found, not supported in this browser for now');
  12796. } else {
  12797. result.audioPid = pid;
  12798. result.segmentAudioCodec = 'ac3';
  12799. }
  12800. }
  12801. break;
  12802. }
  12803. var descriptorLen = data[parsePos + 1] + 2;
  12804. parsePos += descriptorLen;
  12805. remaining -= descriptorLen;
  12806. }
  12807. }
  12808. break;
  12809. case 0xc2: // SAMPLE-AES EC3
  12810. /* falls through */
  12811. case 0x87:
  12812. emitParsingError(observer, new Error('Unsupported EC-3 in M2TS found'));
  12813. return result;
  12814. case 0x24:
  12815. emitParsingError(observer, new Error('Unsupported HEVC in M2TS found'));
  12816. return result;
  12817. }
  12818. // move to the next table entry
  12819. // skip past the elementary stream descriptors, if present
  12820. offset += esInfoLength + 5;
  12821. }
  12822. return result;
  12823. }
  12824. function emitParsingError(observer, error, levelRetry) {
  12825. logger.warn("parsing error: " + error.message);
  12826. observer.emit(Events.ERROR, Events.ERROR, {
  12827. type: ErrorTypes.MEDIA_ERROR,
  12828. details: ErrorDetails.FRAG_PARSING_ERROR,
  12829. fatal: false,
  12830. levelRetry: levelRetry,
  12831. error: error,
  12832. reason: error.message
  12833. });
  12834. }
  12835. function logEncryptedSamplesFoundInUnencryptedStream(type) {
  12836. logger.log(type + " with AES-128-CBC encryption found in unencrypted stream");
  12837. }
  12838. function parsePES(stream) {
  12839. var i = 0;
  12840. var frag;
  12841. var pesLen;
  12842. var pesHdrLen;
  12843. var pesPts;
  12844. var pesDts;
  12845. var data = stream.data;
  12846. // safety check
  12847. if (!stream || stream.size === 0) {
  12848. return null;
  12849. }
  12850. // we might need up to 19 bytes to read PES header
  12851. // if first chunk of data is less than 19 bytes, let's merge it with following ones until we get 19 bytes
  12852. // usually only one merge is needed (and this is rare ...)
  12853. while (data[0].length < 19 && data.length > 1) {
  12854. data[0] = appendUint8Array(data[0], data[1]);
  12855. data.splice(1, 1);
  12856. }
  12857. // retrieve PTS/DTS from first fragment
  12858. frag = data[0];
  12859. var pesPrefix = (frag[0] << 16) + (frag[1] << 8) + frag[2];
  12860. if (pesPrefix === 1) {
  12861. pesLen = (frag[4] << 8) + frag[5];
  12862. // if PES parsed length is not zero and greater than total received length, stop parsing. PES might be truncated
  12863. // minus 6 : PES header size
  12864. if (pesLen && pesLen > stream.size - 6) {
  12865. return null;
  12866. }
  12867. var pesFlags = frag[7];
  12868. if (pesFlags & 0xc0) {
  12869. /* PES header described here : http://dvd.sourceforge.net/dvdinfo/pes-hdr.html
  12870. as PTS / DTS is 33 bit we cannot use bitwise operator in JS,
  12871. as Bitwise operators treat their operands as a sequence of 32 bits */
  12872. pesPts = (frag[9] & 0x0e) * 536870912 +
  12873. // 1 << 29
  12874. (frag[10] & 0xff) * 4194304 +
  12875. // 1 << 22
  12876. (frag[11] & 0xfe) * 16384 +
  12877. // 1 << 14
  12878. (frag[12] & 0xff) * 128 +
  12879. // 1 << 7
  12880. (frag[13] & 0xfe) / 2;
  12881. if (pesFlags & 0x40) {
  12882. pesDts = (frag[14] & 0x0e) * 536870912 +
  12883. // 1 << 29
  12884. (frag[15] & 0xff) * 4194304 +
  12885. // 1 << 22
  12886. (frag[16] & 0xfe) * 16384 +
  12887. // 1 << 14
  12888. (frag[17] & 0xff) * 128 +
  12889. // 1 << 7
  12890. (frag[18] & 0xfe) / 2;
  12891. if (pesPts - pesDts > 60 * 90000) {
  12892. logger.warn(Math.round((pesPts - pesDts) / 90000) + "s delta between PTS and DTS, align them");
  12893. pesPts = pesDts;
  12894. }
  12895. } else {
  12896. pesDts = pesPts;
  12897. }
  12898. }
  12899. pesHdrLen = frag[8];
  12900. // 9 bytes : 6 bytes for PES header + 3 bytes for PES extension
  12901. var payloadStartOffset = pesHdrLen + 9;
  12902. if (stream.size <= payloadStartOffset) {
  12903. return null;
  12904. }
  12905. stream.size -= payloadStartOffset;
  12906. // reassemble PES packet
  12907. var pesData = new Uint8Array(stream.size);
  12908. for (var j = 0, dataLen = data.length; j < dataLen; j++) {
  12909. frag = data[j];
  12910. var len = frag.byteLength;
  12911. if (payloadStartOffset) {
  12912. if (payloadStartOffset > len) {
  12913. // trim full frag if PES header bigger than frag
  12914. payloadStartOffset -= len;
  12915. continue;
  12916. } else {
  12917. // trim partial frag if PES header smaller than frag
  12918. frag = frag.subarray(payloadStartOffset);
  12919. len -= payloadStartOffset;
  12920. payloadStartOffset = 0;
  12921. }
  12922. }
  12923. pesData.set(frag, i);
  12924. i += len;
  12925. }
  12926. if (pesLen) {
  12927. // payload size : remove PES header + PES extension
  12928. pesLen -= pesHdrLen + 3;
  12929. }
  12930. return {
  12931. data: pesData,
  12932. pts: pesPts,
  12933. dts: pesDts,
  12934. len: pesLen
  12935. };
  12936. }
  12937. return null;
  12938. }
  12939. var MP3Demuxer = /*#__PURE__*/function (_BaseAudioDemuxer) {
  12940. _inheritsLoose(MP3Demuxer, _BaseAudioDemuxer);
  12941. function MP3Demuxer() {
  12942. return _BaseAudioDemuxer.apply(this, arguments) || this;
  12943. }
  12944. var _proto = MP3Demuxer.prototype;
  12945. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, trackDuration) {
  12946. _BaseAudioDemuxer.prototype.resetInitSegment.call(this, initSegment, audioCodec, videoCodec, trackDuration);
  12947. this._audioTrack = {
  12948. container: 'audio/mpeg',
  12949. type: 'audio',
  12950. id: 2,
  12951. pid: -1,
  12952. sequenceNumber: 0,
  12953. segmentCodec: 'mp3',
  12954. samples: [],
  12955. manifestCodec: audioCodec,
  12956. duration: trackDuration,
  12957. inputTimeScale: 90000,
  12958. dropped: 0
  12959. };
  12960. };
  12961. MP3Demuxer.probe = function probe$1(data) {
  12962. if (!data) {
  12963. return false;
  12964. }
  12965. // check if data contains ID3 timestamp and MPEG sync word
  12966. // Look for MPEG header | 1111 1111 | 111X XYZX | where X can be either 0 or 1 and Y or Z should be 1
  12967. // Layer bits (position 14 and 15) in header should be always different from 0 (Layer I or Layer II or Layer III)
  12968. // More info http://www.mp3-tech.org/programmer/frame_header.html
  12969. var id3Data = getID3Data(data, 0);
  12970. var offset = (id3Data == null ? void 0 : id3Data.length) || 0;
  12971. // Check for ac-3|ec-3 sync bytes and return false if present
  12972. if (id3Data && data[offset] === 0x0b && data[offset + 1] === 0x77 && getTimeStamp(id3Data) !== undefined &&
  12973. // check the bsid to confirm ac-3 or ec-3 (not mp3)
  12974. getAudioBSID(data, offset) <= 16) {
  12975. return false;
  12976. }
  12977. for (var length = data.length; offset < length; offset++) {
  12978. if (probe(data, offset)) {
  12979. logger.log('MPEG Audio sync word found !');
  12980. return true;
  12981. }
  12982. }
  12983. return false;
  12984. };
  12985. _proto.canParse = function canParse$1(data, offset) {
  12986. return canParse(data, offset);
  12987. };
  12988. _proto.appendFrame = function appendFrame$1(track, data, offset) {
  12989. if (this.basePTS === null) {
  12990. return;
  12991. }
  12992. return appendFrame(track, data, offset, this.basePTS, this.frameIndex);
  12993. };
  12994. return MP3Demuxer;
  12995. }(BaseAudioDemuxer);
  12996. /**
  12997. * AAC helper
  12998. */
  12999. var AAC = /*#__PURE__*/function () {
  13000. function AAC() {}
  13001. AAC.getSilentFrame = function getSilentFrame(codec, channelCount) {
  13002. switch (codec) {
  13003. case 'mp4a.40.2':
  13004. if (channelCount === 1) {
  13005. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]);
  13006. } else if (channelCount === 2) {
  13007. return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]);
  13008. } else if (channelCount === 3) {
  13009. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]);
  13010. } else if (channelCount === 4) {
  13011. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]);
  13012. } else if (channelCount === 5) {
  13013. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]);
  13014. } else if (channelCount === 6) {
  13015. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]);
  13016. }
  13017. break;
  13018. // handle HE-AAC below (mp4a.40.5 / mp4a.40.29)
  13019. default:
  13020. if (channelCount === 1) {
  13021. // ffmpeg -y -f lavfi -i "aevalsrc=0:d=0.05" -c:a libfdk_aac -profile:a aac_he -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  13022. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x4e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x1c, 0x6, 0xf1, 0xc1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  13023. } else if (channelCount === 2) {
  13024. // ffmpeg -y -f lavfi -i "aevalsrc=0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  13025. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  13026. } else if (channelCount === 3) {
  13027. // ffmpeg -y -f lavfi -i "aevalsrc=0|0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  13028. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  13029. }
  13030. break;
  13031. }
  13032. return undefined;
  13033. };
  13034. return AAC;
  13035. }();
  13036. /**
  13037. * Generate MP4 Box
  13038. */
  13039. var UINT32_MAX = Math.pow(2, 32) - 1;
  13040. var MP4 = /*#__PURE__*/function () {
  13041. function MP4() {}
  13042. MP4.init = function init() {
  13043. MP4.types = {
  13044. avc1: [],
  13045. // codingname
  13046. avcC: [],
  13047. btrt: [],
  13048. dinf: [],
  13049. dref: [],
  13050. esds: [],
  13051. ftyp: [],
  13052. hdlr: [],
  13053. mdat: [],
  13054. mdhd: [],
  13055. mdia: [],
  13056. mfhd: [],
  13057. minf: [],
  13058. moof: [],
  13059. moov: [],
  13060. mp4a: [],
  13061. '.mp3': [],
  13062. dac3: [],
  13063. 'ac-3': [],
  13064. mvex: [],
  13065. mvhd: [],
  13066. pasp: [],
  13067. sdtp: [],
  13068. stbl: [],
  13069. stco: [],
  13070. stsc: [],
  13071. stsd: [],
  13072. stsz: [],
  13073. stts: [],
  13074. tfdt: [],
  13075. tfhd: [],
  13076. traf: [],
  13077. trak: [],
  13078. trun: [],
  13079. trex: [],
  13080. tkhd: [],
  13081. vmhd: [],
  13082. smhd: []
  13083. };
  13084. var i;
  13085. for (i in MP4.types) {
  13086. if (MP4.types.hasOwnProperty(i)) {
  13087. MP4.types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
  13088. }
  13089. }
  13090. var videoHdlr = new Uint8Array([0x00,
  13091. // version 0
  13092. 0x00, 0x00, 0x00,
  13093. // flags
  13094. 0x00, 0x00, 0x00, 0x00,
  13095. // pre_defined
  13096. 0x76, 0x69, 0x64, 0x65,
  13097. // handler_type: 'vide'
  13098. 0x00, 0x00, 0x00, 0x00,
  13099. // reserved
  13100. 0x00, 0x00, 0x00, 0x00,
  13101. // reserved
  13102. 0x00, 0x00, 0x00, 0x00,
  13103. // reserved
  13104. 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
  13105. ]);
  13106. var audioHdlr = new Uint8Array([0x00,
  13107. // version 0
  13108. 0x00, 0x00, 0x00,
  13109. // flags
  13110. 0x00, 0x00, 0x00, 0x00,
  13111. // pre_defined
  13112. 0x73, 0x6f, 0x75, 0x6e,
  13113. // handler_type: 'soun'
  13114. 0x00, 0x00, 0x00, 0x00,
  13115. // reserved
  13116. 0x00, 0x00, 0x00, 0x00,
  13117. // reserved
  13118. 0x00, 0x00, 0x00, 0x00,
  13119. // reserved
  13120. 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
  13121. ]);
  13122. MP4.HDLR_TYPES = {
  13123. video: videoHdlr,
  13124. audio: audioHdlr
  13125. };
  13126. var dref = new Uint8Array([0x00,
  13127. // version 0
  13128. 0x00, 0x00, 0x00,
  13129. // flags
  13130. 0x00, 0x00, 0x00, 0x01,
  13131. // entry_count
  13132. 0x00, 0x00, 0x00, 0x0c,
  13133. // entry_size
  13134. 0x75, 0x72, 0x6c, 0x20,
  13135. // 'url' type
  13136. 0x00,
  13137. // version 0
  13138. 0x00, 0x00, 0x01 // entry_flags
  13139. ]);
  13140. var stco = new Uint8Array([0x00,
  13141. // version
  13142. 0x00, 0x00, 0x00,
  13143. // flags
  13144. 0x00, 0x00, 0x00, 0x00 // entry_count
  13145. ]);
  13146. MP4.STTS = MP4.STSC = MP4.STCO = stco;
  13147. MP4.STSZ = new Uint8Array([0x00,
  13148. // version
  13149. 0x00, 0x00, 0x00,
  13150. // flags
  13151. 0x00, 0x00, 0x00, 0x00,
  13152. // sample_size
  13153. 0x00, 0x00, 0x00, 0x00 // sample_count
  13154. ]);
  13155. MP4.VMHD = new Uint8Array([0x00,
  13156. // version
  13157. 0x00, 0x00, 0x01,
  13158. // flags
  13159. 0x00, 0x00,
  13160. // graphicsmode
  13161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
  13162. ]);
  13163. MP4.SMHD = new Uint8Array([0x00,
  13164. // version
  13165. 0x00, 0x00, 0x00,
  13166. // flags
  13167. 0x00, 0x00,
  13168. // balance
  13169. 0x00, 0x00 // reserved
  13170. ]);
  13171. MP4.STSD = new Uint8Array([0x00,
  13172. // version 0
  13173. 0x00, 0x00, 0x00,
  13174. // flags
  13175. 0x00, 0x00, 0x00, 0x01]); // entry_count
  13176. var majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
  13177. var avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1
  13178. var minorVersion = new Uint8Array([0, 0, 0, 1]);
  13179. MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand);
  13180. MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
  13181. };
  13182. MP4.box = function box(type) {
  13183. var size = 8;
  13184. for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  13185. payload[_key - 1] = arguments[_key];
  13186. }
  13187. var i = payload.length;
  13188. var len = i;
  13189. // calculate the total size we need to allocate
  13190. while (i--) {
  13191. size += payload[i].byteLength;
  13192. }
  13193. var result = new Uint8Array(size);
  13194. result[0] = size >> 24 & 0xff;
  13195. result[1] = size >> 16 & 0xff;
  13196. result[2] = size >> 8 & 0xff;
  13197. result[3] = size & 0xff;
  13198. result.set(type, 4);
  13199. // copy the payload into the result
  13200. for (i = 0, size = 8; i < len; i++) {
  13201. // copy payload[i] array @ offset size
  13202. result.set(payload[i], size);
  13203. size += payload[i].byteLength;
  13204. }
  13205. return result;
  13206. };
  13207. MP4.hdlr = function hdlr(type) {
  13208. return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
  13209. };
  13210. MP4.mdat = function mdat(data) {
  13211. return MP4.box(MP4.types.mdat, data);
  13212. };
  13213. MP4.mdhd = function mdhd(timescale, duration) {
  13214. duration *= timescale;
  13215. var upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
  13216. var lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
  13217. return MP4.box(MP4.types.mdhd, new Uint8Array([0x01,
  13218. // version 1
  13219. 0x00, 0x00, 0x00,
  13220. // flags
  13221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  13222. // creation_time
  13223. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
  13224. // modification_time
  13225. timescale >> 24 & 0xff, timescale >> 16 & 0xff, timescale >> 8 & 0xff, timescale & 0xff,
  13226. // timescale
  13227. upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x55, 0xc4,
  13228. // 'und' language (undetermined)
  13229. 0x00, 0x00]));
  13230. };
  13231. MP4.mdia = function mdia(track) {
  13232. return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track));
  13233. };
  13234. MP4.mfhd = function mfhd(sequenceNumber) {
  13235. return MP4.box(MP4.types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
  13236. // flags
  13237. sequenceNumber >> 24, sequenceNumber >> 16 & 0xff, sequenceNumber >> 8 & 0xff, sequenceNumber & 0xff // sequence_number
  13238. ]));
  13239. };
  13240. MP4.minf = function minf(track) {
  13241. if (track.type === 'audio') {
  13242. return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track));
  13243. } else {
  13244. return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
  13245. }
  13246. };
  13247. MP4.moof = function moof(sn, baseMediaDecodeTime, track) {
  13248. return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
  13249. };
  13250. MP4.moov = function moov(tracks) {
  13251. var i = tracks.length;
  13252. var boxes = [];
  13253. while (i--) {
  13254. boxes[i] = MP4.trak(tracks[i]);
  13255. }
  13256. return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(tracks[0].timescale, tracks[0].duration)].concat(boxes).concat(MP4.mvex(tracks)));
  13257. };
  13258. MP4.mvex = function mvex(tracks) {
  13259. var i = tracks.length;
  13260. var boxes = [];
  13261. while (i--) {
  13262. boxes[i] = MP4.trex(tracks[i]);
  13263. }
  13264. return MP4.box.apply(null, [MP4.types.mvex].concat(boxes));
  13265. };
  13266. MP4.mvhd = function mvhd(timescale, duration) {
  13267. duration *= timescale;
  13268. var upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
  13269. var lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
  13270. var bytes = new Uint8Array([0x01,
  13271. // version 1
  13272. 0x00, 0x00, 0x00,
  13273. // flags
  13274. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  13275. // creation_time
  13276. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
  13277. // modification_time
  13278. timescale >> 24 & 0xff, timescale >> 16 & 0xff, timescale >> 8 & 0xff, timescale & 0xff,
  13279. // timescale
  13280. upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x00, 0x01, 0x00, 0x00,
  13281. // 1.0 rate
  13282. 0x01, 0x00,
  13283. // 1.0 volume
  13284. 0x00, 0x00,
  13285. // reserved
  13286. 0x00, 0x00, 0x00, 0x00,
  13287. // reserved
  13288. 0x00, 0x00, 0x00, 0x00,
  13289. // reserved
  13290. 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,
  13291. // transformation: unity matrix
  13292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13293. // pre_defined
  13294. 0xff, 0xff, 0xff, 0xff // next_track_ID
  13295. ]);
  13296. return MP4.box(MP4.types.mvhd, bytes);
  13297. };
  13298. MP4.sdtp = function sdtp(track) {
  13299. var samples = track.samples || [];
  13300. var bytes = new Uint8Array(4 + samples.length);
  13301. var i;
  13302. var flags;
  13303. // leave the full box header (4 bytes) all zero
  13304. // write the sample table
  13305. for (i = 0; i < samples.length; i++) {
  13306. flags = samples[i].flags;
  13307. bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
  13308. }
  13309. return MP4.box(MP4.types.sdtp, bytes);
  13310. };
  13311. MP4.stbl = function stbl(track) {
  13312. return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
  13313. };
  13314. MP4.avc1 = function avc1(track) {
  13315. var sps = [];
  13316. var pps = [];
  13317. var i;
  13318. var data;
  13319. var len;
  13320. // assemble the SPSs
  13321. for (i = 0; i < track.sps.length; i++) {
  13322. data = track.sps[i];
  13323. len = data.byteLength;
  13324. sps.push(len >>> 8 & 0xff);
  13325. sps.push(len & 0xff);
  13326. // SPS
  13327. sps = sps.concat(Array.prototype.slice.call(data));
  13328. }
  13329. // assemble the PPSs
  13330. for (i = 0; i < track.pps.length; i++) {
  13331. data = track.pps[i];
  13332. len = data.byteLength;
  13333. pps.push(len >>> 8 & 0xff);
  13334. pps.push(len & 0xff);
  13335. pps = pps.concat(Array.prototype.slice.call(data));
  13336. }
  13337. var avcc = MP4.box(MP4.types.avcC, new Uint8Array([0x01,
  13338. // version
  13339. sps[3],
  13340. // profile
  13341. sps[4],
  13342. // profile compat
  13343. sps[5],
  13344. // level
  13345. 0xfc | 3,
  13346. // lengthSizeMinusOne, hard-coded to 4 bytes
  13347. 0xe0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets
  13348. ].concat(sps).concat([track.pps.length // numOfPictureParameterSets
  13349. ]).concat(pps))); // "PPS"
  13350. var width = track.width;
  13351. var height = track.height;
  13352. var hSpacing = track.pixelRatio[0];
  13353. var vSpacing = track.pixelRatio[1];
  13354. return MP4.box(MP4.types.avc1, new Uint8Array([0x00, 0x00, 0x00,
  13355. // reserved
  13356. 0x00, 0x00, 0x00,
  13357. // reserved
  13358. 0x00, 0x01,
  13359. // data_reference_index
  13360. 0x00, 0x00,
  13361. // pre_defined
  13362. 0x00, 0x00,
  13363. // reserved
  13364. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13365. // pre_defined
  13366. width >> 8 & 0xff, width & 0xff,
  13367. // width
  13368. height >> 8 & 0xff, height & 0xff,
  13369. // height
  13370. 0x00, 0x48, 0x00, 0x00,
  13371. // horizresolution
  13372. 0x00, 0x48, 0x00, 0x00,
  13373. // vertresolution
  13374. 0x00, 0x00, 0x00, 0x00,
  13375. // reserved
  13376. 0x00, 0x01,
  13377. // frame_count
  13378. 0x12, 0x64, 0x61, 0x69, 0x6c,
  13379. // dailymotion/hls.js
  13380. 0x79, 0x6d, 0x6f, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x68, 0x6c, 0x73, 0x2e, 0x6a, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13381. // compressorname
  13382. 0x00, 0x18,
  13383. // depth = 24
  13384. 0x11, 0x11]),
  13385. // pre_defined = -1
  13386. avcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
  13387. // bufferSizeDB
  13388. 0x00, 0x2d, 0xc6, 0xc0,
  13389. // maxBitrate
  13390. 0x00, 0x2d, 0xc6, 0xc0])),
  13391. // avgBitrate
  13392. MP4.box(MP4.types.pasp, new Uint8Array([hSpacing >> 24,
  13393. // hSpacing
  13394. hSpacing >> 16 & 0xff, hSpacing >> 8 & 0xff, hSpacing & 0xff, vSpacing >> 24,
  13395. // vSpacing
  13396. vSpacing >> 16 & 0xff, vSpacing >> 8 & 0xff, vSpacing & 0xff])));
  13397. };
  13398. MP4.esds = function esds(track) {
  13399. var configlen = track.config.length;
  13400. return new Uint8Array([0x00,
  13401. // version 0
  13402. 0x00, 0x00, 0x00,
  13403. // flags
  13404. 0x03,
  13405. // descriptor_type
  13406. 0x17 + configlen,
  13407. // length
  13408. 0x00, 0x01,
  13409. // es_id
  13410. 0x00,
  13411. // stream_priority
  13412. 0x04,
  13413. // descriptor_type
  13414. 0x0f + configlen,
  13415. // length
  13416. 0x40,
  13417. // codec : mpeg4_audio
  13418. 0x15,
  13419. // stream_type
  13420. 0x00, 0x00, 0x00,
  13421. // buffer_size
  13422. 0x00, 0x00, 0x00, 0x00,
  13423. // maxBitrate
  13424. 0x00, 0x00, 0x00, 0x00,
  13425. // avgBitrate
  13426. 0x05 // descriptor_type
  13427. ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor
  13428. };
  13429. MP4.audioStsd = function audioStsd(track) {
  13430. var samplerate = track.samplerate;
  13431. return new Uint8Array([0x00, 0x00, 0x00,
  13432. // reserved
  13433. 0x00, 0x00, 0x00,
  13434. // reserved
  13435. 0x00, 0x01,
  13436. // data_reference_index
  13437. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13438. // reserved
  13439. 0x00, track.channelCount,
  13440. // channelcount
  13441. 0x00, 0x10,
  13442. // sampleSize:16bits
  13443. 0x00, 0x00, 0x00, 0x00,
  13444. // reserved2
  13445. samplerate >> 8 & 0xff, samplerate & 0xff,
  13446. //
  13447. 0x00, 0x00]);
  13448. };
  13449. MP4.mp4a = function mp4a(track) {
  13450. return MP4.box(MP4.types.mp4a, MP4.audioStsd(track), MP4.box(MP4.types.esds, MP4.esds(track)));
  13451. };
  13452. MP4.mp3 = function mp3(track) {
  13453. return MP4.box(MP4.types['.mp3'], MP4.audioStsd(track));
  13454. };
  13455. MP4.ac3 = function ac3(track) {
  13456. return MP4.box(MP4.types['ac-3'], MP4.audioStsd(track), MP4.box(MP4.types.dac3, track.config));
  13457. };
  13458. MP4.stsd = function stsd(track) {
  13459. if (track.type === 'audio') {
  13460. if (track.segmentCodec === 'mp3' && track.codec === 'mp3') {
  13461. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp3(track));
  13462. }
  13463. if (track.segmentCodec === 'ac3') {
  13464. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.ac3(track));
  13465. }
  13466. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
  13467. } else {
  13468. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
  13469. }
  13470. };
  13471. MP4.tkhd = function tkhd(track) {
  13472. var id = track.id;
  13473. var duration = track.duration * track.timescale;
  13474. var width = track.width;
  13475. var height = track.height;
  13476. var upperWordDuration = Math.floor(duration / (UINT32_MAX + 1));
  13477. var lowerWordDuration = Math.floor(duration % (UINT32_MAX + 1));
  13478. return MP4.box(MP4.types.tkhd, new Uint8Array([0x01,
  13479. // version 1
  13480. 0x00, 0x00, 0x07,
  13481. // flags
  13482. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  13483. // creation_time
  13484. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
  13485. // modification_time
  13486. id >> 24 & 0xff, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff,
  13487. // track_ID
  13488. 0x00, 0x00, 0x00, 0x00,
  13489. // reserved
  13490. upperWordDuration >> 24, upperWordDuration >> 16 & 0xff, upperWordDuration >> 8 & 0xff, upperWordDuration & 0xff, lowerWordDuration >> 24, lowerWordDuration >> 16 & 0xff, lowerWordDuration >> 8 & 0xff, lowerWordDuration & 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  13491. // reserved
  13492. 0x00, 0x00,
  13493. // layer
  13494. 0x00, 0x00,
  13495. // alternate_group
  13496. 0x00, 0x00,
  13497. // non-audio track volume
  13498. 0x00, 0x00,
  13499. // reserved
  13500. 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,
  13501. // transformation: unity matrix
  13502. width >> 8 & 0xff, width & 0xff, 0x00, 0x00,
  13503. // width
  13504. height >> 8 & 0xff, height & 0xff, 0x00, 0x00 // height
  13505. ]));
  13506. };
  13507. MP4.traf = function traf(track, baseMediaDecodeTime) {
  13508. var sampleDependencyTable = MP4.sdtp(track);
  13509. var id = track.id;
  13510. var upperWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime / (UINT32_MAX + 1));
  13511. var lowerWordBaseMediaDecodeTime = Math.floor(baseMediaDecodeTime % (UINT32_MAX + 1));
  13512. return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([0x00,
  13513. // version 0
  13514. 0x00, 0x00, 0x00,
  13515. // flags
  13516. id >> 24, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff // track_ID
  13517. ])), MP4.box(MP4.types.tfdt, new Uint8Array([0x01,
  13518. // version 1
  13519. 0x00, 0x00, 0x00,
  13520. // flags
  13521. upperWordBaseMediaDecodeTime >> 24, upperWordBaseMediaDecodeTime >> 16 & 0xff, upperWordBaseMediaDecodeTime >> 8 & 0xff, upperWordBaseMediaDecodeTime & 0xff, lowerWordBaseMediaDecodeTime >> 24, lowerWordBaseMediaDecodeTime >> 16 & 0xff, lowerWordBaseMediaDecodeTime >> 8 & 0xff, lowerWordBaseMediaDecodeTime & 0xff])), MP4.trun(track, sampleDependencyTable.length + 16 +
  13522. // tfhd
  13523. 20 +
  13524. // tfdt
  13525. 8 +
  13526. // traf header
  13527. 16 +
  13528. // mfhd
  13529. 8 +
  13530. // moof header
  13531. 8),
  13532. // mdat header
  13533. sampleDependencyTable);
  13534. }
  13535. /**
  13536. * Generate a track box.
  13537. * @param track a track definition
  13538. */;
  13539. MP4.trak = function trak(track) {
  13540. track.duration = track.duration || 0xffffffff;
  13541. return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
  13542. };
  13543. MP4.trex = function trex(track) {
  13544. var id = track.id;
  13545. return MP4.box(MP4.types.trex, new Uint8Array([0x00,
  13546. // version 0
  13547. 0x00, 0x00, 0x00,
  13548. // flags
  13549. id >> 24, id >> 16 & 0xff, id >> 8 & 0xff, id & 0xff,
  13550. // track_ID
  13551. 0x00, 0x00, 0x00, 0x01,
  13552. // default_sample_description_index
  13553. 0x00, 0x00, 0x00, 0x00,
  13554. // default_sample_duration
  13555. 0x00, 0x00, 0x00, 0x00,
  13556. // default_sample_size
  13557. 0x00, 0x01, 0x00, 0x01 // default_sample_flags
  13558. ]));
  13559. };
  13560. MP4.trun = function trun(track, offset) {
  13561. var samples = track.samples || [];
  13562. var len = samples.length;
  13563. var arraylen = 12 + 16 * len;
  13564. var array = new Uint8Array(arraylen);
  13565. var i;
  13566. var sample;
  13567. var duration;
  13568. var size;
  13569. var flags;
  13570. var cts;
  13571. offset += 8 + arraylen;
  13572. array.set([track.type === 'video' ? 0x01 : 0x00,
  13573. // version 1 for video with signed-int sample_composition_time_offset
  13574. 0x00, 0x0f, 0x01,
  13575. // flags
  13576. len >>> 24 & 0xff, len >>> 16 & 0xff, len >>> 8 & 0xff, len & 0xff,
  13577. // sample_count
  13578. offset >>> 24 & 0xff, offset >>> 16 & 0xff, offset >>> 8 & 0xff, offset & 0xff // data_offset
  13579. ], 0);
  13580. for (i = 0; i < len; i++) {
  13581. sample = samples[i];
  13582. duration = sample.duration;
  13583. size = sample.size;
  13584. flags = sample.flags;
  13585. cts = sample.cts;
  13586. array.set([duration >>> 24 & 0xff, duration >>> 16 & 0xff, duration >>> 8 & 0xff, duration & 0xff,
  13587. // sample_duration
  13588. size >>> 24 & 0xff, size >>> 16 & 0xff, size >>> 8 & 0xff, size & 0xff,
  13589. // sample_size
  13590. flags.isLeading << 2 | flags.dependsOn, flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.paddingValue << 1 | flags.isNonSync, flags.degradPrio & 0xf0 << 8, flags.degradPrio & 0x0f,
  13591. // sample_flags
  13592. cts >>> 24 & 0xff, cts >>> 16 & 0xff, cts >>> 8 & 0xff, cts & 0xff // sample_composition_time_offset
  13593. ], 12 + 16 * i);
  13594. }
  13595. return MP4.box(MP4.types.trun, array);
  13596. };
  13597. MP4.initSegment = function initSegment(tracks) {
  13598. if (!MP4.types) {
  13599. MP4.init();
  13600. }
  13601. var movie = MP4.moov(tracks);
  13602. var result = appendUint8Array(MP4.FTYP, movie);
  13603. return result;
  13604. };
  13605. return MP4;
  13606. }();
  13607. MP4.types = void 0;
  13608. MP4.HDLR_TYPES = void 0;
  13609. MP4.STTS = void 0;
  13610. MP4.STSC = void 0;
  13611. MP4.STCO = void 0;
  13612. MP4.STSZ = void 0;
  13613. MP4.VMHD = void 0;
  13614. MP4.SMHD = void 0;
  13615. MP4.STSD = void 0;
  13616. MP4.FTYP = void 0;
  13617. MP4.DINF = void 0;
  13618. var MPEG_TS_CLOCK_FREQ_HZ = 90000;
  13619. function toTimescaleFromBase(baseTime, destScale, srcBase, round) {
  13620. if (srcBase === void 0) {
  13621. srcBase = 1;
  13622. }
  13623. if (round === void 0) {
  13624. round = false;
  13625. }
  13626. var result = baseTime * destScale * srcBase; // equivalent to `(value * scale) / (1 / base)`
  13627. return round ? Math.round(result) : result;
  13628. }
  13629. function toTimescaleFromScale(baseTime, destScale, srcScale, round) {
  13630. if (srcScale === void 0) {
  13631. srcScale = 1;
  13632. }
  13633. if (round === void 0) {
  13634. round = false;
  13635. }
  13636. return toTimescaleFromBase(baseTime, destScale, 1 / srcScale, round);
  13637. }
  13638. function toMsFromMpegTsClock(baseTime, round) {
  13639. if (round === void 0) {
  13640. round = false;
  13641. }
  13642. return toTimescaleFromBase(baseTime, 1000, 1 / MPEG_TS_CLOCK_FREQ_HZ, round);
  13643. }
  13644. function toMpegTsClockFromTimescale(baseTime, srcScale) {
  13645. if (srcScale === void 0) {
  13646. srcScale = 1;
  13647. }
  13648. return toTimescaleFromBase(baseTime, MPEG_TS_CLOCK_FREQ_HZ, 1 / srcScale);
  13649. }
  13650. var MAX_SILENT_FRAME_DURATION = 10 * 1000; // 10 seconds
  13651. var AAC_SAMPLES_PER_FRAME = 1024;
  13652. var MPEG_AUDIO_SAMPLE_PER_FRAME = 1152;
  13653. var AC3_SAMPLES_PER_FRAME = 1536;
  13654. var chromeVersion = null;
  13655. var safariWebkitVersion = null;
  13656. var MP4Remuxer = /*#__PURE__*/function () {
  13657. function MP4Remuxer(observer, config, typeSupported, vendor) {
  13658. this.observer = void 0;
  13659. this.config = void 0;
  13660. this.typeSupported = void 0;
  13661. this.ISGenerated = false;
  13662. this._initPTS = null;
  13663. this._initDTS = null;
  13664. this.nextAvcDts = null;
  13665. this.nextAudioPts = null;
  13666. this.videoSampleDuration = null;
  13667. this.isAudioContiguous = false;
  13668. this.isVideoContiguous = false;
  13669. this.videoTrackConfig = void 0;
  13670. this.observer = observer;
  13671. this.config = config;
  13672. this.typeSupported = typeSupported;
  13673. this.ISGenerated = false;
  13674. if (chromeVersion === null) {
  13675. var userAgent = navigator.userAgent || '';
  13676. var result = userAgent.match(/Chrome\/(\d+)/i);
  13677. chromeVersion = result ? parseInt(result[1]) : 0;
  13678. }
  13679. if (safariWebkitVersion === null) {
  13680. var _result = navigator.userAgent.match(/Safari\/(\d+)/i);
  13681. safariWebkitVersion = _result ? parseInt(_result[1]) : 0;
  13682. }
  13683. }
  13684. var _proto = MP4Remuxer.prototype;
  13685. _proto.destroy = function destroy() {
  13686. // @ts-ignore
  13687. this.config = this.videoTrackConfig = this._initPTS = this._initDTS = null;
  13688. };
  13689. _proto.resetTimeStamp = function resetTimeStamp(defaultTimeStamp) {
  13690. logger.log('[mp4-remuxer]: initPTS & initDTS reset');
  13691. this._initPTS = this._initDTS = defaultTimeStamp;
  13692. };
  13693. _proto.resetNextTimestamp = function resetNextTimestamp() {
  13694. logger.log('[mp4-remuxer]: reset next timestamp');
  13695. this.isVideoContiguous = false;
  13696. this.isAudioContiguous = false;
  13697. };
  13698. _proto.resetInitSegment = function resetInitSegment() {
  13699. logger.log('[mp4-remuxer]: ISGenerated flag reset');
  13700. this.ISGenerated = false;
  13701. this.videoTrackConfig = undefined;
  13702. };
  13703. _proto.getVideoStartPts = function getVideoStartPts(videoSamples) {
  13704. var rolloverDetected = false;
  13705. var startPTS = videoSamples.reduce(function (minPTS, sample) {
  13706. var delta = sample.pts - minPTS;
  13707. if (delta < -4294967296) {
  13708. // 2^32, see PTSNormalize for reasoning, but we're hitting a rollover here, and we don't want that to impact the timeOffset calculation
  13709. rolloverDetected = true;
  13710. return normalizePts(minPTS, sample.pts);
  13711. } else if (delta > 0) {
  13712. return minPTS;
  13713. } else {
  13714. return sample.pts;
  13715. }
  13716. }, videoSamples[0].pts);
  13717. if (rolloverDetected) {
  13718. logger.debug('PTS rollover detected');
  13719. }
  13720. return startPTS;
  13721. };
  13722. _proto.remux = function remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, flush, playlistType) {
  13723. var video;
  13724. var audio;
  13725. var initSegment;
  13726. var text;
  13727. var id3;
  13728. var independent;
  13729. var audioTimeOffset = timeOffset;
  13730. var videoTimeOffset = timeOffset;
  13731. // If we're remuxing audio and video progressively, wait until we've received enough samples for each track before proceeding.
  13732. // This is done to synchronize the audio and video streams. We know if the current segment will have samples if the "pid"
  13733. // parameter is greater than -1. The pid is set when the PMT is parsed, which contains the tracks list.
  13734. // However, if the initSegment has already been generated, or we've reached the end of a segment (flush),
  13735. // then we can remux one track without waiting for the other.
  13736. var hasAudio = audioTrack.pid > -1;
  13737. var hasVideo = videoTrack.pid > -1;
  13738. var length = videoTrack.samples.length;
  13739. var enoughAudioSamples = audioTrack.samples.length > 0;
  13740. var enoughVideoSamples = flush && length > 0 || length > 1;
  13741. var canRemuxAvc = (!hasAudio || enoughAudioSamples) && (!hasVideo || enoughVideoSamples) || this.ISGenerated || flush;
  13742. if (canRemuxAvc) {
  13743. if (this.ISGenerated) {
  13744. var _videoTrack$pixelRati, _config$pixelRatio, _videoTrack$pixelRati2, _config$pixelRatio2;
  13745. var config = this.videoTrackConfig;
  13746. if (config && (videoTrack.width !== config.width || videoTrack.height !== config.height || ((_videoTrack$pixelRati = videoTrack.pixelRatio) == null ? void 0 : _videoTrack$pixelRati[0]) !== ((_config$pixelRatio = config.pixelRatio) == null ? void 0 : _config$pixelRatio[0]) || ((_videoTrack$pixelRati2 = videoTrack.pixelRatio) == null ? void 0 : _videoTrack$pixelRati2[1]) !== ((_config$pixelRatio2 = config.pixelRatio) == null ? void 0 : _config$pixelRatio2[1]))) {
  13747. this.resetInitSegment();
  13748. }
  13749. } else {
  13750. initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
  13751. }
  13752. var isVideoContiguous = this.isVideoContiguous;
  13753. var firstKeyFrameIndex = -1;
  13754. var firstKeyFramePTS;
  13755. if (enoughVideoSamples) {
  13756. firstKeyFrameIndex = findKeyframeIndex(videoTrack.samples);
  13757. if (!isVideoContiguous && this.config.forceKeyFrameOnDiscontinuity) {
  13758. independent = true;
  13759. if (firstKeyFrameIndex > 0) {
  13760. logger.warn("[mp4-remuxer]: Dropped " + firstKeyFrameIndex + " out of " + length + " video samples due to a missing keyframe");
  13761. var startPTS = this.getVideoStartPts(videoTrack.samples);
  13762. videoTrack.samples = videoTrack.samples.slice(firstKeyFrameIndex);
  13763. videoTrack.dropped += firstKeyFrameIndex;
  13764. videoTimeOffset += (videoTrack.samples[0].pts - startPTS) / videoTrack.inputTimeScale;
  13765. firstKeyFramePTS = videoTimeOffset;
  13766. } else if (firstKeyFrameIndex === -1) {
  13767. logger.warn("[mp4-remuxer]: No keyframe found out of " + length + " video samples");
  13768. independent = false;
  13769. }
  13770. }
  13771. }
  13772. if (this.ISGenerated) {
  13773. if (enoughAudioSamples && enoughVideoSamples) {
  13774. // timeOffset is expected to be the offset of the first timestamp of this fragment (first DTS)
  13775. // if first audio DTS is not aligned with first video DTS then we need to take that into account
  13776. // when providing timeOffset to remuxAudio / remuxVideo. if we don't do that, there might be a permanent / small
  13777. // drift between audio and video streams
  13778. var _startPTS = this.getVideoStartPts(videoTrack.samples);
  13779. var tsDelta = normalizePts(audioTrack.samples[0].pts, _startPTS) - _startPTS;
  13780. var audiovideoTimestampDelta = tsDelta / videoTrack.inputTimeScale;
  13781. audioTimeOffset += Math.max(0, audiovideoTimestampDelta);
  13782. videoTimeOffset += Math.max(0, -audiovideoTimestampDelta);
  13783. }
  13784. // Purposefully remuxing audio before video, so that remuxVideo can use nextAudioPts, which is calculated in remuxAudio.
  13785. if (enoughAudioSamples) {
  13786. // if initSegment was generated without audio samples, regenerate it again
  13787. if (!audioTrack.samplerate) {
  13788. logger.warn('[mp4-remuxer]: regenerate InitSegment as audio detected');
  13789. initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
  13790. }
  13791. audio = this.remuxAudio(audioTrack, audioTimeOffset, this.isAudioContiguous, accurateTimeOffset, hasVideo || enoughVideoSamples || playlistType === PlaylistLevelType.AUDIO ? videoTimeOffset : undefined);
  13792. if (enoughVideoSamples) {
  13793. var audioTrackLength = audio ? audio.endPTS - audio.startPTS : 0;
  13794. // if initSegment was generated without video samples, regenerate it again
  13795. if (!videoTrack.inputTimeScale) {
  13796. logger.warn('[mp4-remuxer]: regenerate InitSegment as video detected');
  13797. initSegment = this.generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset);
  13798. }
  13799. video = this.remuxVideo(videoTrack, videoTimeOffset, isVideoContiguous, audioTrackLength);
  13800. }
  13801. } else if (enoughVideoSamples) {
  13802. video = this.remuxVideo(videoTrack, videoTimeOffset, isVideoContiguous, 0);
  13803. }
  13804. if (video) {
  13805. video.firstKeyFrame = firstKeyFrameIndex;
  13806. video.independent = firstKeyFrameIndex !== -1;
  13807. video.firstKeyFramePTS = firstKeyFramePTS;
  13808. }
  13809. }
  13810. }
  13811. // Allow ID3 and text to remux, even if more audio/video samples are required
  13812. if (this.ISGenerated && this._initPTS && this._initDTS) {
  13813. if (id3Track.samples.length) {
  13814. id3 = flushTextTrackMetadataCueSamples(id3Track, timeOffset, this._initPTS, this._initDTS);
  13815. }
  13816. if (textTrack.samples.length) {
  13817. text = flushTextTrackUserdataCueSamples(textTrack, timeOffset, this._initPTS);
  13818. }
  13819. }
  13820. return {
  13821. audio: audio,
  13822. video: video,
  13823. initSegment: initSegment,
  13824. independent: independent,
  13825. text: text,
  13826. id3: id3
  13827. };
  13828. };
  13829. _proto.generateIS = function generateIS(audioTrack, videoTrack, timeOffset, accurateTimeOffset) {
  13830. var audioSamples = audioTrack.samples;
  13831. var videoSamples = videoTrack.samples;
  13832. var typeSupported = this.typeSupported;
  13833. var tracks = {};
  13834. var _initPTS = this._initPTS;
  13835. var computePTSDTS = !_initPTS || accurateTimeOffset;
  13836. var container = 'audio/mp4';
  13837. var initPTS;
  13838. var initDTS;
  13839. var timescale;
  13840. if (computePTSDTS) {
  13841. initPTS = initDTS = Infinity;
  13842. }
  13843. if (audioTrack.config && audioSamples.length) {
  13844. // let's use audio sampling rate as MP4 time scale.
  13845. // rationale is that there is a integer nb of audio frames per audio sample (1024 for AAC)
  13846. // using audio sampling rate here helps having an integer MP4 frame duration
  13847. // this avoids potential rounding issue and AV sync issue
  13848. audioTrack.timescale = audioTrack.samplerate;
  13849. switch (audioTrack.segmentCodec) {
  13850. case 'mp3':
  13851. if (typeSupported.mpeg) {
  13852. // Chrome and Safari
  13853. container = 'audio/mpeg';
  13854. audioTrack.codec = '';
  13855. } else if (typeSupported.mp3) {
  13856. // Firefox
  13857. audioTrack.codec = 'mp3';
  13858. }
  13859. break;
  13860. case 'ac3':
  13861. audioTrack.codec = 'ac-3';
  13862. break;
  13863. }
  13864. tracks.audio = {
  13865. id: 'audio',
  13866. container: container,
  13867. codec: audioTrack.codec,
  13868. initSegment: audioTrack.segmentCodec === 'mp3' && typeSupported.mpeg ? new Uint8Array(0) : MP4.initSegment([audioTrack]),
  13869. metadata: {
  13870. channelCount: audioTrack.channelCount
  13871. }
  13872. };
  13873. if (computePTSDTS) {
  13874. timescale = audioTrack.inputTimeScale;
  13875. if (!_initPTS || timescale !== _initPTS.timescale) {
  13876. // remember first PTS of this demuxing context. for audio, PTS = DTS
  13877. initPTS = initDTS = audioSamples[0].pts - Math.round(timescale * timeOffset);
  13878. } else {
  13879. computePTSDTS = false;
  13880. }
  13881. }
  13882. }
  13883. if (videoTrack.sps && videoTrack.pps && videoSamples.length) {
  13884. // let's use input time scale as MP4 video timescale
  13885. // we use input time scale straight away to avoid rounding issues on frame duration / cts computation
  13886. videoTrack.timescale = videoTrack.inputTimeScale;
  13887. tracks.video = {
  13888. id: 'main',
  13889. container: 'video/mp4',
  13890. codec: videoTrack.codec,
  13891. initSegment: MP4.initSegment([videoTrack]),
  13892. metadata: {
  13893. width: videoTrack.width,
  13894. height: videoTrack.height
  13895. }
  13896. };
  13897. if (computePTSDTS) {
  13898. timescale = videoTrack.inputTimeScale;
  13899. if (!_initPTS || timescale !== _initPTS.timescale) {
  13900. var startPTS = this.getVideoStartPts(videoSamples);
  13901. var startOffset = Math.round(timescale * timeOffset);
  13902. initDTS = Math.min(initDTS, normalizePts(videoSamples[0].dts, startPTS) - startOffset);
  13903. initPTS = Math.min(initPTS, startPTS - startOffset);
  13904. } else {
  13905. computePTSDTS = false;
  13906. }
  13907. }
  13908. this.videoTrackConfig = {
  13909. width: videoTrack.width,
  13910. height: videoTrack.height,
  13911. pixelRatio: videoTrack.pixelRatio
  13912. };
  13913. }
  13914. if (Object.keys(tracks).length) {
  13915. this.ISGenerated = true;
  13916. if (computePTSDTS) {
  13917. this._initPTS = {
  13918. baseTime: initPTS,
  13919. timescale: timescale
  13920. };
  13921. this._initDTS = {
  13922. baseTime: initDTS,
  13923. timescale: timescale
  13924. };
  13925. } else {
  13926. initPTS = timescale = undefined;
  13927. }
  13928. return {
  13929. tracks: tracks,
  13930. initPTS: initPTS,
  13931. timescale: timescale
  13932. };
  13933. }
  13934. };
  13935. _proto.remuxVideo = function remuxVideo(track, timeOffset, contiguous, audioTrackLength) {
  13936. var timeScale = track.inputTimeScale;
  13937. var inputSamples = track.samples;
  13938. var outputSamples = [];
  13939. var nbSamples = inputSamples.length;
  13940. var initPTS = this._initPTS;
  13941. var nextAvcDts = this.nextAvcDts;
  13942. var offset = 8;
  13943. var mp4SampleDuration = this.videoSampleDuration;
  13944. var firstDTS;
  13945. var lastDTS;
  13946. var minPTS = Number.POSITIVE_INFINITY;
  13947. var maxPTS = Number.NEGATIVE_INFINITY;
  13948. var sortSamples = false;
  13949. // if parsed fragment is contiguous with last one, let's use last DTS value as reference
  13950. if (!contiguous || nextAvcDts === null) {
  13951. var pts = timeOffset * timeScale;
  13952. var cts = inputSamples[0].pts - normalizePts(inputSamples[0].dts, inputSamples[0].pts);
  13953. if (chromeVersion && nextAvcDts !== null && Math.abs(pts - cts - nextAvcDts) < 15000) {
  13954. // treat as contigous to adjust samples that would otherwise produce video buffer gaps in Chrome
  13955. contiguous = true;
  13956. } else {
  13957. // if not contiguous, let's use target timeOffset
  13958. nextAvcDts = pts - cts;
  13959. }
  13960. }
  13961. // PTS is coded on 33bits, and can loop from -2^32 to 2^32
  13962. // PTSNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value
  13963. var initTime = initPTS.baseTime * timeScale / initPTS.timescale;
  13964. for (var i = 0; i < nbSamples; i++) {
  13965. var sample = inputSamples[i];
  13966. sample.pts = normalizePts(sample.pts - initTime, nextAvcDts);
  13967. sample.dts = normalizePts(sample.dts - initTime, nextAvcDts);
  13968. if (sample.dts < inputSamples[i > 0 ? i - 1 : i].dts) {
  13969. sortSamples = true;
  13970. }
  13971. }
  13972. // sort video samples by DTS then PTS then demux id order
  13973. if (sortSamples) {
  13974. inputSamples.sort(function (a, b) {
  13975. var deltadts = a.dts - b.dts;
  13976. var deltapts = a.pts - b.pts;
  13977. return deltadts || deltapts;
  13978. });
  13979. }
  13980. // Get first/last DTS
  13981. firstDTS = inputSamples[0].dts;
  13982. lastDTS = inputSamples[inputSamples.length - 1].dts;
  13983. // Sample duration (as expected by trun MP4 boxes), should be the delta between sample DTS
  13984. // set this constant duration as being the avg delta between consecutive DTS.
  13985. var inputDuration = lastDTS - firstDTS;
  13986. var averageSampleDuration = inputDuration ? Math.round(inputDuration / (nbSamples - 1)) : mp4SampleDuration || track.inputTimeScale / 30;
  13987. // if fragment are contiguous, detect hole/overlapping between fragments
  13988. if (contiguous) {
  13989. // check timestamp continuity across consecutive fragments (this is to remove inter-fragment gap/hole)
  13990. var delta = firstDTS - nextAvcDts;
  13991. var foundHole = delta > averageSampleDuration;
  13992. var foundOverlap = delta < -1;
  13993. if (foundHole || foundOverlap) {
  13994. if (foundHole) {
  13995. logger.warn("AVC: " + toMsFromMpegTsClock(delta, true) + " ms (" + delta + "dts) hole between fragments detected at " + timeOffset.toFixed(3));
  13996. } else {
  13997. logger.warn("AVC: " + toMsFromMpegTsClock(-delta, true) + " ms (" + delta + "dts) overlapping between fragments detected at " + timeOffset.toFixed(3));
  13998. }
  13999. if (!foundOverlap || nextAvcDts >= inputSamples[0].pts || chromeVersion) {
  14000. firstDTS = nextAvcDts;
  14001. var firstPTS = inputSamples[0].pts - delta;
  14002. if (foundHole) {
  14003. inputSamples[0].dts = firstDTS;
  14004. inputSamples[0].pts = firstPTS;
  14005. } else {
  14006. for (var _i = 0; _i < inputSamples.length; _i++) {
  14007. if (inputSamples[_i].dts > firstPTS) {
  14008. break;
  14009. }
  14010. inputSamples[_i].dts -= delta;
  14011. inputSamples[_i].pts -= delta;
  14012. }
  14013. }
  14014. logger.log("Video: Initial PTS/DTS adjusted: " + toMsFromMpegTsClock(firstPTS, true) + "/" + toMsFromMpegTsClock(firstDTS, true) + ", delta: " + toMsFromMpegTsClock(delta, true) + " ms");
  14015. }
  14016. }
  14017. }
  14018. firstDTS = Math.max(0, firstDTS);
  14019. var nbNalu = 0;
  14020. var naluLen = 0;
  14021. var dtsStep = firstDTS;
  14022. for (var _i2 = 0; _i2 < nbSamples; _i2++) {
  14023. // compute total/avc sample length and nb of NAL units
  14024. var _sample = inputSamples[_i2];
  14025. var units = _sample.units;
  14026. var nbUnits = units.length;
  14027. var sampleLen = 0;
  14028. for (var j = 0; j < nbUnits; j++) {
  14029. sampleLen += units[j].data.length;
  14030. }
  14031. naluLen += sampleLen;
  14032. nbNalu += nbUnits;
  14033. _sample.length = sampleLen;
  14034. // ensure sample monotonic DTS
  14035. if (_sample.dts < dtsStep) {
  14036. _sample.dts = dtsStep;
  14037. dtsStep += averageSampleDuration / 4 | 0 || 1;
  14038. } else {
  14039. dtsStep = _sample.dts;
  14040. }
  14041. minPTS = Math.min(_sample.pts, minPTS);
  14042. maxPTS = Math.max(_sample.pts, maxPTS);
  14043. }
  14044. lastDTS = inputSamples[nbSamples - 1].dts;
  14045. /* concatenate the video data and construct the mdat in place
  14046. (need 8 more bytes to fill length and mpdat type) */
  14047. var mdatSize = naluLen + 4 * nbNalu + 8;
  14048. var mdat;
  14049. try {
  14050. mdat = new Uint8Array(mdatSize);
  14051. } catch (err) {
  14052. this.observer.emit(Events.ERROR, Events.ERROR, {
  14053. type: ErrorTypes.MUX_ERROR,
  14054. details: ErrorDetails.REMUX_ALLOC_ERROR,
  14055. fatal: false,
  14056. error: err,
  14057. bytes: mdatSize,
  14058. reason: "fail allocating video mdat " + mdatSize
  14059. });
  14060. return;
  14061. }
  14062. var view = new DataView(mdat.buffer);
  14063. view.setUint32(0, mdatSize);
  14064. mdat.set(MP4.types.mdat, 4);
  14065. var stretchedLastFrame = false;
  14066. var minDtsDelta = Number.POSITIVE_INFINITY;
  14067. var minPtsDelta = Number.POSITIVE_INFINITY;
  14068. var maxDtsDelta = Number.NEGATIVE_INFINITY;
  14069. var maxPtsDelta = Number.NEGATIVE_INFINITY;
  14070. for (var _i3 = 0; _i3 < nbSamples; _i3++) {
  14071. var _VideoSample = inputSamples[_i3];
  14072. var VideoSampleUnits = _VideoSample.units;
  14073. var mp4SampleLength = 0;
  14074. // convert NALU bitstream to MP4 format (prepend NALU with size field)
  14075. for (var _j = 0, _nbUnits = VideoSampleUnits.length; _j < _nbUnits; _j++) {
  14076. var unit = VideoSampleUnits[_j];
  14077. var unitData = unit.data;
  14078. var unitDataLen = unit.data.byteLength;
  14079. view.setUint32(offset, unitDataLen);
  14080. offset += 4;
  14081. mdat.set(unitData, offset);
  14082. offset += unitDataLen;
  14083. mp4SampleLength += 4 + unitDataLen;
  14084. }
  14085. // expected sample duration is the Decoding Timestamp diff of consecutive samples
  14086. var ptsDelta = void 0;
  14087. if (_i3 < nbSamples - 1) {
  14088. mp4SampleDuration = inputSamples[_i3 + 1].dts - _VideoSample.dts;
  14089. ptsDelta = inputSamples[_i3 + 1].pts - _VideoSample.pts;
  14090. } else {
  14091. var config = this.config;
  14092. var lastFrameDuration = _i3 > 0 ? _VideoSample.dts - inputSamples[_i3 - 1].dts : averageSampleDuration;
  14093. ptsDelta = _i3 > 0 ? _VideoSample.pts - inputSamples[_i3 - 1].pts : averageSampleDuration;
  14094. if (config.stretchShortVideoTrack && this.nextAudioPts !== null) {
  14095. // In some cases, a segment's audio track duration may exceed the video track duration.
  14096. // Since we've already remuxed audio, and we know how long the audio track is, we look to
  14097. // see if the delta to the next segment is longer than maxBufferHole.
  14098. // If so, playback would potentially get stuck, so we artificially inflate
  14099. // the duration of the last frame to minimize any potential gap between segments.
  14100. var gapTolerance = Math.floor(config.maxBufferHole * timeScale);
  14101. var deltaToFrameEnd = (audioTrackLength ? minPTS + audioTrackLength * timeScale : this.nextAudioPts) - _VideoSample.pts;
  14102. if (deltaToFrameEnd > gapTolerance) {
  14103. // We subtract lastFrameDuration from deltaToFrameEnd to try to prevent any video
  14104. // frame overlap. maxBufferHole should be >> lastFrameDuration anyway.
  14105. mp4SampleDuration = deltaToFrameEnd - lastFrameDuration;
  14106. if (mp4SampleDuration < 0) {
  14107. mp4SampleDuration = lastFrameDuration;
  14108. } else {
  14109. stretchedLastFrame = true;
  14110. }
  14111. logger.log("[mp4-remuxer]: It is approximately " + deltaToFrameEnd / 90 + " ms to the next segment; using duration " + mp4SampleDuration / 90 + " ms for the last video frame.");
  14112. } else {
  14113. mp4SampleDuration = lastFrameDuration;
  14114. }
  14115. } else {
  14116. mp4SampleDuration = lastFrameDuration;
  14117. }
  14118. }
  14119. var compositionTimeOffset = Math.round(_VideoSample.pts - _VideoSample.dts);
  14120. minDtsDelta = Math.min(minDtsDelta, mp4SampleDuration);
  14121. maxDtsDelta = Math.max(maxDtsDelta, mp4SampleDuration);
  14122. minPtsDelta = Math.min(minPtsDelta, ptsDelta);
  14123. maxPtsDelta = Math.max(maxPtsDelta, ptsDelta);
  14124. outputSamples.push(new Mp4Sample(_VideoSample.key, mp4SampleDuration, mp4SampleLength, compositionTimeOffset));
  14125. }
  14126. if (outputSamples.length) {
  14127. if (chromeVersion) {
  14128. if (chromeVersion < 70) {
  14129. // Chrome workaround, mark first sample as being a Random Access Point (keyframe) to avoid sourcebuffer append issue
  14130. // https://code.google.com/p/chromium/issues/detail?id=229412
  14131. var flags = outputSamples[0].flags;
  14132. flags.dependsOn = 2;
  14133. flags.isNonSync = 0;
  14134. }
  14135. } else if (safariWebkitVersion) {
  14136. // Fix for "CNN special report, with CC" in test-streams (Safari browser only)
  14137. // Ignore DTS when frame durations are irregular. Safari MSE does not handle this leading to gaps.
  14138. if (maxPtsDelta - minPtsDelta < maxDtsDelta - minDtsDelta && averageSampleDuration / maxDtsDelta < 0.025 && outputSamples[0].cts === 0) {
  14139. logger.warn('Found irregular gaps in sample duration. Using PTS instead of DTS to determine MP4 sample duration.');
  14140. var dts = firstDTS;
  14141. for (var _i4 = 0, len = outputSamples.length; _i4 < len; _i4++) {
  14142. var nextDts = dts + outputSamples[_i4].duration;
  14143. var _pts = dts + outputSamples[_i4].cts;
  14144. if (_i4 < len - 1) {
  14145. var nextPts = nextDts + outputSamples[_i4 + 1].cts;
  14146. outputSamples[_i4].duration = nextPts - _pts;
  14147. } else {
  14148. outputSamples[_i4].duration = _i4 ? outputSamples[_i4 - 1].duration : averageSampleDuration;
  14149. }
  14150. outputSamples[_i4].cts = 0;
  14151. dts = nextDts;
  14152. }
  14153. }
  14154. }
  14155. }
  14156. // next AVC sample DTS should be equal to last sample DTS + last sample duration (in PES timescale)
  14157. mp4SampleDuration = stretchedLastFrame || !mp4SampleDuration ? averageSampleDuration : mp4SampleDuration;
  14158. this.nextAvcDts = nextAvcDts = lastDTS + mp4SampleDuration;
  14159. this.videoSampleDuration = mp4SampleDuration;
  14160. this.isVideoContiguous = true;
  14161. var moof = MP4.moof(track.sequenceNumber++, firstDTS, _extends({}, track, {
  14162. samples: outputSamples
  14163. }));
  14164. var type = 'video';
  14165. var data = {
  14166. data1: moof,
  14167. data2: mdat,
  14168. startPTS: minPTS / timeScale,
  14169. endPTS: (maxPTS + mp4SampleDuration) / timeScale,
  14170. startDTS: firstDTS / timeScale,
  14171. endDTS: nextAvcDts / timeScale,
  14172. type: type,
  14173. hasAudio: false,
  14174. hasVideo: true,
  14175. nb: outputSamples.length,
  14176. dropped: track.dropped
  14177. };
  14178. track.samples = [];
  14179. track.dropped = 0;
  14180. return data;
  14181. };
  14182. _proto.getSamplesPerFrame = function getSamplesPerFrame(track) {
  14183. switch (track.segmentCodec) {
  14184. case 'mp3':
  14185. return MPEG_AUDIO_SAMPLE_PER_FRAME;
  14186. case 'ac3':
  14187. return AC3_SAMPLES_PER_FRAME;
  14188. default:
  14189. return AAC_SAMPLES_PER_FRAME;
  14190. }
  14191. };
  14192. _proto.remuxAudio = function remuxAudio(track, timeOffset, contiguous, accurateTimeOffset, videoTimeOffset) {
  14193. var inputTimeScale = track.inputTimeScale;
  14194. var mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale;
  14195. var scaleFactor = inputTimeScale / mp4timeScale;
  14196. var mp4SampleDuration = this.getSamplesPerFrame(track);
  14197. var inputSampleDuration = mp4SampleDuration * scaleFactor;
  14198. var initPTS = this._initPTS;
  14199. var rawMPEG = track.segmentCodec === 'mp3' && this.typeSupported.mpeg;
  14200. var outputSamples = [];
  14201. var alignedWithVideo = videoTimeOffset !== undefined;
  14202. var inputSamples = track.samples;
  14203. var offset = rawMPEG ? 0 : 8;
  14204. var nextAudioPts = this.nextAudioPts || -1;
  14205. // window.audioSamples ? window.audioSamples.push(inputSamples.map(s => s.pts)) : (window.audioSamples = [inputSamples.map(s => s.pts)]);
  14206. // for audio samples, also consider consecutive fragments as being contiguous (even if a level switch occurs),
  14207. // for sake of clarity:
  14208. // consecutive fragments are frags with
  14209. // - less than 100ms gaps between new time offset (if accurate) and next expected PTS OR
  14210. // - less than 20 audio frames distance
  14211. // contiguous fragments are consecutive fragments from same quality level (same level, new SN = old SN + 1)
  14212. // this helps ensuring audio continuity
  14213. // and this also avoids audio glitches/cut when switching quality, or reporting wrong duration on first audio frame
  14214. var timeOffsetMpegTS = timeOffset * inputTimeScale;
  14215. var initTime = initPTS.baseTime * inputTimeScale / initPTS.timescale;
  14216. this.isAudioContiguous = contiguous = contiguous || inputSamples.length && nextAudioPts > 0 && (accurateTimeOffset && Math.abs(timeOffsetMpegTS - nextAudioPts) < 9000 || Math.abs(normalizePts(inputSamples[0].pts - initTime, timeOffsetMpegTS) - nextAudioPts) < 20 * inputSampleDuration);
  14217. // compute normalized PTS
  14218. inputSamples.forEach(function (sample) {
  14219. sample.pts = normalizePts(sample.pts - initTime, timeOffsetMpegTS);
  14220. });
  14221. if (!contiguous || nextAudioPts < 0) {
  14222. // filter out sample with negative PTS that are not playable anyway
  14223. // if we don't remove these negative samples, they will shift all audio samples forward.
  14224. // leading to audio overlap between current / next fragment
  14225. inputSamples = inputSamples.filter(function (sample) {
  14226. return sample.pts >= 0;
  14227. });
  14228. // in case all samples have negative PTS, and have been filtered out, return now
  14229. if (!inputSamples.length) {
  14230. return;
  14231. }
  14232. if (videoTimeOffset === 0) {
  14233. // Set the start to 0 to match video so that start gaps larger than inputSampleDuration are filled with silence
  14234. nextAudioPts = 0;
  14235. } else if (accurateTimeOffset && !alignedWithVideo) {
  14236. // When not seeking, not live, and LevelDetails.PTSKnown, use fragment start as predicted next audio PTS
  14237. nextAudioPts = Math.max(0, timeOffsetMpegTS);
  14238. } else {
  14239. // if frags are not contiguous and if we cant trust time offset, let's use first sample PTS as next audio PTS
  14240. nextAudioPts = inputSamples[0].pts;
  14241. }
  14242. }
  14243. // If the audio track is missing samples, the frames seem to get "left-shifted" within the
  14244. // resulting mp4 segment, causing sync issues and leaving gaps at the end of the audio segment.
  14245. // In an effort to prevent this from happening, we inject frames here where there are gaps.
  14246. // When possible, we inject a silent frame; when that's not possible, we duplicate the last
  14247. // frame.
  14248. if (track.segmentCodec === 'aac') {
  14249. var maxAudioFramesDrift = this.config.maxAudioFramesDrift;
  14250. for (var i = 0, nextPts = nextAudioPts; i < inputSamples.length; i++) {
  14251. // First, let's see how far off this frame is from where we expect it to be
  14252. var sample = inputSamples[i];
  14253. var pts = sample.pts;
  14254. var delta = pts - nextPts;
  14255. var duration = Math.abs(1000 * delta / inputTimeScale);
  14256. // When remuxing with video, if we're overlapping by more than a duration, drop this sample to stay in sync
  14257. if (delta <= -maxAudioFramesDrift * inputSampleDuration && alignedWithVideo) {
  14258. if (i === 0) {
  14259. logger.warn("Audio frame @ " + (pts / inputTimeScale).toFixed(3) + "s overlaps nextAudioPts by " + Math.round(1000 * delta / inputTimeScale) + " ms.");
  14260. this.nextAudioPts = nextAudioPts = nextPts = pts;
  14261. }
  14262. } // eslint-disable-line brace-style
  14263. // Insert missing frames if:
  14264. // 1: We're more than maxAudioFramesDrift frame away
  14265. // 2: Not more than MAX_SILENT_FRAME_DURATION away
  14266. // 3: currentTime (aka nextPtsNorm) is not 0
  14267. // 4: remuxing with video (videoTimeOffset !== undefined)
  14268. else if (delta >= maxAudioFramesDrift * inputSampleDuration && duration < MAX_SILENT_FRAME_DURATION && alignedWithVideo) {
  14269. var missing = Math.round(delta / inputSampleDuration);
  14270. // Adjust nextPts so that silent samples are aligned with media pts. This will prevent media samples from
  14271. // later being shifted if nextPts is based on timeOffset and delta is not a multiple of inputSampleDuration.
  14272. nextPts = pts - missing * inputSampleDuration;
  14273. if (nextPts < 0) {
  14274. missing--;
  14275. nextPts += inputSampleDuration;
  14276. }
  14277. if (i === 0) {
  14278. this.nextAudioPts = nextAudioPts = nextPts;
  14279. }
  14280. logger.warn("[mp4-remuxer]: Injecting " + missing + " audio frame @ " + (nextPts / inputTimeScale).toFixed(3) + "s due to " + Math.round(1000 * delta / inputTimeScale) + " ms gap.");
  14281. for (var j = 0; j < missing; j++) {
  14282. var newStamp = Math.max(nextPts, 0);
  14283. var fillFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount);
  14284. if (!fillFrame) {
  14285. logger.log('[mp4-remuxer]: Unable to get silent frame for given audio codec; duplicating last frame instead.');
  14286. fillFrame = sample.unit.subarray();
  14287. }
  14288. inputSamples.splice(i, 0, {
  14289. unit: fillFrame,
  14290. pts: newStamp
  14291. });
  14292. nextPts += inputSampleDuration;
  14293. i++;
  14294. }
  14295. }
  14296. sample.pts = nextPts;
  14297. nextPts += inputSampleDuration;
  14298. }
  14299. }
  14300. var firstPTS = null;
  14301. var lastPTS = null;
  14302. var mdat;
  14303. var mdatSize = 0;
  14304. var sampleLength = inputSamples.length;
  14305. while (sampleLength--) {
  14306. mdatSize += inputSamples[sampleLength].unit.byteLength;
  14307. }
  14308. for (var _j2 = 0, _nbSamples = inputSamples.length; _j2 < _nbSamples; _j2++) {
  14309. var audioSample = inputSamples[_j2];
  14310. var unit = audioSample.unit;
  14311. var _pts2 = audioSample.pts;
  14312. if (lastPTS !== null) {
  14313. // If we have more than one sample, set the duration of the sample to the "real" duration; the PTS diff with
  14314. // the previous sample
  14315. var prevSample = outputSamples[_j2 - 1];
  14316. prevSample.duration = Math.round((_pts2 - lastPTS) / scaleFactor);
  14317. } else {
  14318. if (contiguous && track.segmentCodec === 'aac') {
  14319. // set PTS/DTS to expected PTS/DTS
  14320. _pts2 = nextAudioPts;
  14321. }
  14322. // remember first PTS of our audioSamples
  14323. firstPTS = _pts2;
  14324. if (mdatSize > 0) {
  14325. /* concatenate the audio data and construct the mdat in place
  14326. (need 8 more bytes to fill length and mdat type) */
  14327. mdatSize += offset;
  14328. try {
  14329. mdat = new Uint8Array(mdatSize);
  14330. } catch (err) {
  14331. this.observer.emit(Events.ERROR, Events.ERROR, {
  14332. type: ErrorTypes.MUX_ERROR,
  14333. details: ErrorDetails.REMUX_ALLOC_ERROR,
  14334. fatal: false,
  14335. error: err,
  14336. bytes: mdatSize,
  14337. reason: "fail allocating audio mdat " + mdatSize
  14338. });
  14339. return;
  14340. }
  14341. if (!rawMPEG) {
  14342. var view = new DataView(mdat.buffer);
  14343. view.setUint32(0, mdatSize);
  14344. mdat.set(MP4.types.mdat, 4);
  14345. }
  14346. } else {
  14347. // no audio samples
  14348. return;
  14349. }
  14350. }
  14351. mdat.set(unit, offset);
  14352. var unitLen = unit.byteLength;
  14353. offset += unitLen;
  14354. // Default the sample's duration to the computed mp4SampleDuration, which will either be 1024 for AAC or 1152 for MPEG
  14355. // In the case that we have 1 sample, this will be the duration. If we have more than one sample, the duration
  14356. // becomes the PTS diff with the previous sample
  14357. outputSamples.push(new Mp4Sample(true, mp4SampleDuration, unitLen, 0));
  14358. lastPTS = _pts2;
  14359. }
  14360. // We could end up with no audio samples if all input samples were overlapping with the previously remuxed ones
  14361. var nbSamples = outputSamples.length;
  14362. if (!nbSamples) {
  14363. return;
  14364. }
  14365. // The next audio sample PTS should be equal to last sample PTS + duration
  14366. var lastSample = outputSamples[outputSamples.length - 1];
  14367. this.nextAudioPts = nextAudioPts = lastPTS + scaleFactor * lastSample.duration;
  14368. // Set the track samples from inputSamples to outputSamples before remuxing
  14369. var moof = rawMPEG ? new Uint8Array(0) : MP4.moof(track.sequenceNumber++, firstPTS / scaleFactor, _extends({}, track, {
  14370. samples: outputSamples
  14371. }));
  14372. // Clear the track samples. This also clears the samples array in the demuxer, since the reference is shared
  14373. track.samples = [];
  14374. var start = firstPTS / inputTimeScale;
  14375. var end = nextAudioPts / inputTimeScale;
  14376. var type = 'audio';
  14377. var audioData = {
  14378. data1: moof,
  14379. data2: mdat,
  14380. startPTS: start,
  14381. endPTS: end,
  14382. startDTS: start,
  14383. endDTS: end,
  14384. type: type,
  14385. hasAudio: true,
  14386. hasVideo: false,
  14387. nb: nbSamples
  14388. };
  14389. this.isAudioContiguous = true;
  14390. return audioData;
  14391. };
  14392. _proto.remuxEmptyAudio = function remuxEmptyAudio(track, timeOffset, contiguous, videoData) {
  14393. var inputTimeScale = track.inputTimeScale;
  14394. var mp4timeScale = track.samplerate ? track.samplerate : inputTimeScale;
  14395. var scaleFactor = inputTimeScale / mp4timeScale;
  14396. var nextAudioPts = this.nextAudioPts;
  14397. // sync with video's timestamp
  14398. var initDTS = this._initDTS;
  14399. var init90kHz = initDTS.baseTime * 90000 / initDTS.timescale;
  14400. var startDTS = (nextAudioPts !== null ? nextAudioPts : videoData.startDTS * inputTimeScale) + init90kHz;
  14401. var endDTS = videoData.endDTS * inputTimeScale + init90kHz;
  14402. // one sample's duration value
  14403. var frameDuration = scaleFactor * AAC_SAMPLES_PER_FRAME;
  14404. // samples count of this segment's duration
  14405. var nbSamples = Math.ceil((endDTS - startDTS) / frameDuration);
  14406. // silent frame
  14407. var silentFrame = AAC.getSilentFrame(track.manifestCodec || track.codec, track.channelCount);
  14408. logger.warn('[mp4-remuxer]: remux empty Audio');
  14409. // Can't remux if we can't generate a silent frame...
  14410. if (!silentFrame) {
  14411. logger.trace('[mp4-remuxer]: Unable to remuxEmptyAudio since we were unable to get a silent frame for given audio codec');
  14412. return;
  14413. }
  14414. var samples = [];
  14415. for (var i = 0; i < nbSamples; i++) {
  14416. var stamp = startDTS + i * frameDuration;
  14417. samples.push({
  14418. unit: silentFrame,
  14419. pts: stamp,
  14420. dts: stamp
  14421. });
  14422. }
  14423. track.samples = samples;
  14424. return this.remuxAudio(track, timeOffset, contiguous, false);
  14425. };
  14426. return MP4Remuxer;
  14427. }();
  14428. function normalizePts(value, reference) {
  14429. var offset;
  14430. if (reference === null) {
  14431. return value;
  14432. }
  14433. if (reference < value) {
  14434. // - 2^33
  14435. offset = -8589934592;
  14436. } else {
  14437. // + 2^33
  14438. offset = 8589934592;
  14439. }
  14440. /* PTS is 33bit (from 0 to 2^33 -1)
  14441. if diff between value and reference is bigger than half of the amplitude (2^32) then it means that
  14442. PTS looping occured. fill the gap */
  14443. while (Math.abs(value - reference) > 4294967296) {
  14444. value += offset;
  14445. }
  14446. return value;
  14447. }
  14448. function findKeyframeIndex(samples) {
  14449. for (var i = 0; i < samples.length; i++) {
  14450. if (samples[i].key) {
  14451. return i;
  14452. }
  14453. }
  14454. return -1;
  14455. }
  14456. function flushTextTrackMetadataCueSamples(track, timeOffset, initPTS, initDTS) {
  14457. var length = track.samples.length;
  14458. if (!length) {
  14459. return;
  14460. }
  14461. var inputTimeScale = track.inputTimeScale;
  14462. for (var index = 0; index < length; index++) {
  14463. var sample = track.samples[index];
  14464. // setting id3 pts, dts to relative time
  14465. // using this._initPTS and this._initDTS to calculate relative time
  14466. sample.pts = normalizePts(sample.pts - initPTS.baseTime * inputTimeScale / initPTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
  14467. sample.dts = normalizePts(sample.dts - initDTS.baseTime * inputTimeScale / initDTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
  14468. }
  14469. var samples = track.samples;
  14470. track.samples = [];
  14471. return {
  14472. samples: samples
  14473. };
  14474. }
  14475. function flushTextTrackUserdataCueSamples(track, timeOffset, initPTS) {
  14476. var length = track.samples.length;
  14477. if (!length) {
  14478. return;
  14479. }
  14480. var inputTimeScale = track.inputTimeScale;
  14481. for (var index = 0; index < length; index++) {
  14482. var sample = track.samples[index];
  14483. // setting text pts, dts to relative time
  14484. // using this._initPTS and this._initDTS to calculate relative time
  14485. sample.pts = normalizePts(sample.pts - initPTS.baseTime * inputTimeScale / initPTS.timescale, timeOffset * inputTimeScale) / inputTimeScale;
  14486. }
  14487. track.samples.sort(function (a, b) {
  14488. return a.pts - b.pts;
  14489. });
  14490. var samples = track.samples;
  14491. track.samples = [];
  14492. return {
  14493. samples: samples
  14494. };
  14495. }
  14496. var Mp4Sample = function Mp4Sample(isKeyframe, duration, size, cts) {
  14497. this.size = void 0;
  14498. this.duration = void 0;
  14499. this.cts = void 0;
  14500. this.flags = void 0;
  14501. this.duration = duration;
  14502. this.size = size;
  14503. this.cts = cts;
  14504. this.flags = {
  14505. isLeading: 0,
  14506. isDependedOn: 0,
  14507. hasRedundancy: 0,
  14508. degradPrio: 0,
  14509. dependsOn: isKeyframe ? 2 : 1,
  14510. isNonSync: isKeyframe ? 0 : 1
  14511. };
  14512. };
  14513. var PassThroughRemuxer = /*#__PURE__*/function () {
  14514. function PassThroughRemuxer() {
  14515. this.emitInitSegment = false;
  14516. this.audioCodec = void 0;
  14517. this.videoCodec = void 0;
  14518. this.initData = void 0;
  14519. this.initPTS = null;
  14520. this.initTracks = void 0;
  14521. this.lastEndTime = null;
  14522. }
  14523. var _proto = PassThroughRemuxer.prototype;
  14524. _proto.destroy = function destroy() {};
  14525. _proto.resetTimeStamp = function resetTimeStamp(defaultInitPTS) {
  14526. this.initPTS = defaultInitPTS;
  14527. this.lastEndTime = null;
  14528. };
  14529. _proto.resetNextTimestamp = function resetNextTimestamp() {
  14530. this.lastEndTime = null;
  14531. };
  14532. _proto.resetInitSegment = function resetInitSegment(initSegment, audioCodec, videoCodec, decryptdata) {
  14533. this.audioCodec = audioCodec;
  14534. this.videoCodec = videoCodec;
  14535. this.generateInitSegment(patchEncyptionData(initSegment, decryptdata));
  14536. this.emitInitSegment = true;
  14537. };
  14538. _proto.generateInitSegment = function generateInitSegment(initSegment) {
  14539. var audioCodec = this.audioCodec,
  14540. videoCodec = this.videoCodec;
  14541. if (!(initSegment != null && initSegment.byteLength)) {
  14542. this.initTracks = undefined;
  14543. this.initData = undefined;
  14544. return;
  14545. }
  14546. var initData = this.initData = parseInitSegment(initSegment);
  14547. // Get codec from initSegment or fallback to default
  14548. if (initData.audio) {
  14549. audioCodec = getParsedTrackCodec(initData.audio, ElementaryStreamTypes.AUDIO);
  14550. }
  14551. if (initData.video) {
  14552. videoCodec = getParsedTrackCodec(initData.video, ElementaryStreamTypes.VIDEO);
  14553. }
  14554. var tracks = {};
  14555. if (initData.audio && initData.video) {
  14556. tracks.audiovideo = {
  14557. container: 'video/mp4',
  14558. codec: audioCodec + ',' + videoCodec,
  14559. initSegment: initSegment,
  14560. id: 'main'
  14561. };
  14562. } else if (initData.audio) {
  14563. tracks.audio = {
  14564. container: 'audio/mp4',
  14565. codec: audioCodec,
  14566. initSegment: initSegment,
  14567. id: 'audio'
  14568. };
  14569. } else if (initData.video) {
  14570. tracks.video = {
  14571. container: 'video/mp4',
  14572. codec: videoCodec,
  14573. initSegment: initSegment,
  14574. id: 'main'
  14575. };
  14576. } else {
  14577. logger.warn('[passthrough-remuxer.ts]: initSegment does not contain moov or trak boxes.');
  14578. }
  14579. this.initTracks = tracks;
  14580. };
  14581. _proto.remux = function remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset) {
  14582. var _initData, _initData2;
  14583. var initPTS = this.initPTS,
  14584. lastEndTime = this.lastEndTime;
  14585. var result = {
  14586. audio: undefined,
  14587. video: undefined,
  14588. text: textTrack,
  14589. id3: id3Track,
  14590. initSegment: undefined
  14591. };
  14592. // If we haven't yet set a lastEndDTS, or it was reset, set it to the provided timeOffset. We want to use the
  14593. // lastEndDTS over timeOffset whenever possible; during progressive playback, the media source will not update
  14594. // the media duration (which is what timeOffset is provided as) before we need to process the next chunk.
  14595. if (!isFiniteNumber(lastEndTime)) {
  14596. lastEndTime = this.lastEndTime = timeOffset || 0;
  14597. }
  14598. // The binary segment data is added to the videoTrack in the mp4demuxer. We don't check to see if the data is only
  14599. // audio or video (or both); adding it to video was an arbitrary choice.
  14600. var data = videoTrack.samples;
  14601. if (!(data != null && data.length)) {
  14602. return result;
  14603. }
  14604. var initSegment = {
  14605. initPTS: undefined,
  14606. timescale: 1
  14607. };
  14608. var initData = this.initData;
  14609. if (!((_initData = initData) != null && _initData.length)) {
  14610. this.generateInitSegment(data);
  14611. initData = this.initData;
  14612. }
  14613. if (!((_initData2 = initData) != null && _initData2.length)) {
  14614. // We can't remux if the initSegment could not be generated
  14615. logger.warn('[passthrough-remuxer.ts]: Failed to generate initSegment.');
  14616. return result;
  14617. }
  14618. if (this.emitInitSegment) {
  14619. initSegment.tracks = this.initTracks;
  14620. this.emitInitSegment = false;
  14621. }
  14622. var duration = getDuration(data, initData);
  14623. var startDTS = getStartDTS(initData, data);
  14624. var decodeTime = startDTS === null ? timeOffset : startDTS;
  14625. if (isInvalidInitPts(initPTS, decodeTime, timeOffset, duration) || initSegment.timescale !== initPTS.timescale && accurateTimeOffset) {
  14626. initSegment.initPTS = decodeTime - timeOffset;
  14627. if (initPTS && initPTS.timescale === 1) {
  14628. logger.warn("Adjusting initPTS by " + (initSegment.initPTS - initPTS.baseTime));
  14629. }
  14630. this.initPTS = initPTS = {
  14631. baseTime: initSegment.initPTS,
  14632. timescale: 1
  14633. };
  14634. }
  14635. var startTime = audioTrack ? decodeTime - initPTS.baseTime / initPTS.timescale : lastEndTime;
  14636. var endTime = startTime + duration;
  14637. offsetStartDTS(initData, data, initPTS.baseTime / initPTS.timescale);
  14638. if (duration > 0) {
  14639. this.lastEndTime = endTime;
  14640. } else {
  14641. logger.warn('Duration parsed from mp4 should be greater than zero');
  14642. this.resetNextTimestamp();
  14643. }
  14644. var hasAudio = !!initData.audio;
  14645. var hasVideo = !!initData.video;
  14646. var type = '';
  14647. if (hasAudio) {
  14648. type += 'audio';
  14649. }
  14650. if (hasVideo) {
  14651. type += 'video';
  14652. }
  14653. var track = {
  14654. data1: data,
  14655. startPTS: startTime,
  14656. startDTS: startTime,
  14657. endPTS: endTime,
  14658. endDTS: endTime,
  14659. type: type,
  14660. hasAudio: hasAudio,
  14661. hasVideo: hasVideo,
  14662. nb: 1,
  14663. dropped: 0
  14664. };
  14665. result.audio = track.type === 'audio' ? track : undefined;
  14666. result.video = track.type !== 'audio' ? track : undefined;
  14667. result.initSegment = initSegment;
  14668. result.id3 = flushTextTrackMetadataCueSamples(id3Track, timeOffset, initPTS, initPTS);
  14669. if (textTrack.samples.length) {
  14670. result.text = flushTextTrackUserdataCueSamples(textTrack, timeOffset, initPTS);
  14671. }
  14672. return result;
  14673. };
  14674. return PassThroughRemuxer;
  14675. }();
  14676. function isInvalidInitPts(initPTS, startDTS, timeOffset, duration) {
  14677. if (initPTS === null) {
  14678. return true;
  14679. }
  14680. // InitPTS is invalid when distance from program would be more than segment duration or a minimum of one second
  14681. var minDuration = Math.max(duration, 1);
  14682. var startTime = startDTS - initPTS.baseTime / initPTS.timescale;
  14683. return Math.abs(startTime - timeOffset) > minDuration;
  14684. }
  14685. function getParsedTrackCodec(track, type) {
  14686. var parsedCodec = track == null ? void 0 : track.codec;
  14687. if (parsedCodec && parsedCodec.length > 4) {
  14688. return parsedCodec;
  14689. }
  14690. if (type === ElementaryStreamTypes.AUDIO) {
  14691. if (parsedCodec === 'ec-3' || parsedCodec === 'ac-3' || parsedCodec === 'alac') {
  14692. return parsedCodec;
  14693. }
  14694. if (parsedCodec === 'fLaC' || parsedCodec === 'Opus') {
  14695. // Opting not to get `preferManagedMediaSource` from player config for isSupported() check for simplicity
  14696. var preferManagedMediaSource = false;
  14697. return getCodecCompatibleName(parsedCodec, preferManagedMediaSource);
  14698. }
  14699. var result = 'mp4a.40.5';
  14700. logger.info("Parsed audio codec \"" + parsedCodec + "\" or audio object type not handled. Using \"" + result + "\"");
  14701. return result;
  14702. }
  14703. // Provide defaults based on codec type
  14704. // This allows for some playback of some fmp4 playlists without CODECS defined in manifest
  14705. logger.warn("Unhandled video codec \"" + parsedCodec + "\"");
  14706. if (parsedCodec === 'hvc1' || parsedCodec === 'hev1') {
  14707. return 'hvc1.1.6.L120.90';
  14708. }
  14709. if (parsedCodec === 'av01') {
  14710. return 'av01.0.04M.08';
  14711. }
  14712. return 'avc1.42e01e';
  14713. }
  14714. var now;
  14715. // performance.now() not available on WebWorker, at least on Safari Desktop
  14716. try {
  14717. now = self.performance.now.bind(self.performance);
  14718. } catch (err) {
  14719. logger.debug('Unable to use Performance API on this environment');
  14720. now = optionalSelf == null ? void 0 : optionalSelf.Date.now;
  14721. }
  14722. var muxConfig = [{
  14723. demux: MP4Demuxer,
  14724. remux: PassThroughRemuxer
  14725. }, {
  14726. demux: TSDemuxer,
  14727. remux: MP4Remuxer
  14728. }, {
  14729. demux: AACDemuxer,
  14730. remux: MP4Remuxer
  14731. }, {
  14732. demux: MP3Demuxer,
  14733. remux: MP4Remuxer
  14734. }];
  14735. {
  14736. muxConfig.splice(2, 0, {
  14737. demux: AC3Demuxer,
  14738. remux: MP4Remuxer
  14739. });
  14740. }
  14741. var Transmuxer = /*#__PURE__*/function () {
  14742. function Transmuxer(observer, typeSupported, config, vendor, id) {
  14743. this.async = false;
  14744. this.observer = void 0;
  14745. this.typeSupported = void 0;
  14746. this.config = void 0;
  14747. this.vendor = void 0;
  14748. this.id = void 0;
  14749. this.demuxer = void 0;
  14750. this.remuxer = void 0;
  14751. this.decrypter = void 0;
  14752. this.probe = void 0;
  14753. this.decryptionPromise = null;
  14754. this.transmuxConfig = void 0;
  14755. this.currentTransmuxState = void 0;
  14756. this.observer = observer;
  14757. this.typeSupported = typeSupported;
  14758. this.config = config;
  14759. this.vendor = vendor;
  14760. this.id = id;
  14761. }
  14762. var _proto = Transmuxer.prototype;
  14763. _proto.configure = function configure(transmuxConfig) {
  14764. this.transmuxConfig = transmuxConfig;
  14765. if (this.decrypter) {
  14766. this.decrypter.reset();
  14767. }
  14768. };
  14769. _proto.push = function push(data, decryptdata, chunkMeta, state) {
  14770. var _this = this;
  14771. var stats = chunkMeta.transmuxing;
  14772. stats.executeStart = now();
  14773. var uintData = new Uint8Array(data);
  14774. var currentTransmuxState = this.currentTransmuxState,
  14775. transmuxConfig = this.transmuxConfig;
  14776. if (state) {
  14777. this.currentTransmuxState = state;
  14778. }
  14779. var _ref = state || currentTransmuxState,
  14780. contiguous = _ref.contiguous,
  14781. discontinuity = _ref.discontinuity,
  14782. trackSwitch = _ref.trackSwitch,
  14783. accurateTimeOffset = _ref.accurateTimeOffset,
  14784. timeOffset = _ref.timeOffset,
  14785. initSegmentChange = _ref.initSegmentChange;
  14786. var audioCodec = transmuxConfig.audioCodec,
  14787. videoCodec = transmuxConfig.videoCodec,
  14788. defaultInitPts = transmuxConfig.defaultInitPts,
  14789. duration = transmuxConfig.duration,
  14790. initSegmentData = transmuxConfig.initSegmentData;
  14791. var keyData = getEncryptionType(uintData, decryptdata);
  14792. if (keyData && keyData.method === 'AES-128') {
  14793. var decrypter = this.getDecrypter();
  14794. // Software decryption is synchronous; webCrypto is not
  14795. if (decrypter.isSync()) {
  14796. // Software decryption is progressive. Progressive decryption may not return a result on each call. Any cached
  14797. // data is handled in the flush() call
  14798. var decryptedData = decrypter.softwareDecrypt(uintData, keyData.key.buffer, keyData.iv.buffer);
  14799. // For Low-Latency HLS Parts, decrypt in place, since part parsing is expected on push progress
  14800. var loadingParts = chunkMeta.part > -1;
  14801. if (loadingParts) {
  14802. decryptedData = decrypter.flush();
  14803. }
  14804. if (!decryptedData) {
  14805. stats.executeEnd = now();
  14806. return emptyResult(chunkMeta);
  14807. }
  14808. uintData = new Uint8Array(decryptedData);
  14809. } else {
  14810. this.decryptionPromise = decrypter.webCryptoDecrypt(uintData, keyData.key.buffer, keyData.iv.buffer).then(function (decryptedData) {
  14811. // Calling push here is important; if flush() is called while this is still resolving, this ensures that
  14812. // the decrypted data has been transmuxed
  14813. var result = _this.push(decryptedData, null, chunkMeta);
  14814. _this.decryptionPromise = null;
  14815. return result;
  14816. });
  14817. return this.decryptionPromise;
  14818. }
  14819. }
  14820. var resetMuxers = this.needsProbing(discontinuity, trackSwitch);
  14821. if (resetMuxers) {
  14822. var error = this.configureTransmuxer(uintData);
  14823. if (error) {
  14824. logger.warn("[transmuxer] " + error.message);
  14825. this.observer.emit(Events.ERROR, Events.ERROR, {
  14826. type: ErrorTypes.MEDIA_ERROR,
  14827. details: ErrorDetails.FRAG_PARSING_ERROR,
  14828. fatal: false,
  14829. error: error,
  14830. reason: error.message
  14831. });
  14832. stats.executeEnd = now();
  14833. return emptyResult(chunkMeta);
  14834. }
  14835. }
  14836. if (discontinuity || trackSwitch || initSegmentChange || resetMuxers) {
  14837. this.resetInitSegment(initSegmentData, audioCodec, videoCodec, duration, decryptdata);
  14838. }
  14839. if (discontinuity || initSegmentChange || resetMuxers) {
  14840. this.resetInitialTimestamp(defaultInitPts);
  14841. }
  14842. if (!contiguous) {
  14843. this.resetContiguity();
  14844. }
  14845. var result = this.transmux(uintData, keyData, timeOffset, accurateTimeOffset, chunkMeta);
  14846. var currentState = this.currentTransmuxState;
  14847. currentState.contiguous = true;
  14848. currentState.discontinuity = false;
  14849. currentState.trackSwitch = false;
  14850. stats.executeEnd = now();
  14851. return result;
  14852. }
  14853. // Due to data caching, flush calls can produce more than one TransmuxerResult (hence the Array type)
  14854. ;
  14855. _proto.flush = function flush(chunkMeta) {
  14856. var _this2 = this;
  14857. var stats = chunkMeta.transmuxing;
  14858. stats.executeStart = now();
  14859. var decrypter = this.decrypter,
  14860. currentTransmuxState = this.currentTransmuxState,
  14861. decryptionPromise = this.decryptionPromise;
  14862. if (decryptionPromise) {
  14863. // Upon resolution, the decryption promise calls push() and returns its TransmuxerResult up the stack. Therefore
  14864. // only flushing is required for async decryption
  14865. return decryptionPromise.then(function () {
  14866. return _this2.flush(chunkMeta);
  14867. });
  14868. }
  14869. var transmuxResults = [];
  14870. var timeOffset = currentTransmuxState.timeOffset;
  14871. if (decrypter) {
  14872. // The decrypter may have data cached, which needs to be demuxed. In this case we'll have two TransmuxResults
  14873. // This happens in the case that we receive only 1 push call for a segment (either for non-progressive downloads,
  14874. // or for progressive downloads with small segments)
  14875. var decryptedData = decrypter.flush();
  14876. if (decryptedData) {
  14877. // Push always returns a TransmuxerResult if decryptdata is null
  14878. transmuxResults.push(this.push(decryptedData, null, chunkMeta));
  14879. }
  14880. }
  14881. var demuxer = this.demuxer,
  14882. remuxer = this.remuxer;
  14883. if (!demuxer || !remuxer) {
  14884. // If probing failed, then Hls.js has been given content its not able to handle
  14885. stats.executeEnd = now();
  14886. return [emptyResult(chunkMeta)];
  14887. }
  14888. var demuxResultOrPromise = demuxer.flush(timeOffset);
  14889. if (isPromise(demuxResultOrPromise)) {
  14890. // Decrypt final SAMPLE-AES samples
  14891. return demuxResultOrPromise.then(function (demuxResult) {
  14892. _this2.flushRemux(transmuxResults, demuxResult, chunkMeta);
  14893. return transmuxResults;
  14894. });
  14895. }
  14896. this.flushRemux(transmuxResults, demuxResultOrPromise, chunkMeta);
  14897. return transmuxResults;
  14898. };
  14899. _proto.flushRemux = function flushRemux(transmuxResults, demuxResult, chunkMeta) {
  14900. var audioTrack = demuxResult.audioTrack,
  14901. videoTrack = demuxResult.videoTrack,
  14902. id3Track = demuxResult.id3Track,
  14903. textTrack = demuxResult.textTrack;
  14904. var _this$currentTransmux = this.currentTransmuxState,
  14905. accurateTimeOffset = _this$currentTransmux.accurateTimeOffset,
  14906. timeOffset = _this$currentTransmux.timeOffset;
  14907. logger.log("[transmuxer.ts]: Flushed fragment " + chunkMeta.sn + (chunkMeta.part > -1 ? ' p: ' + chunkMeta.part : '') + " of level " + chunkMeta.level);
  14908. var remuxResult = this.remuxer.remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, true, this.id);
  14909. transmuxResults.push({
  14910. remuxResult: remuxResult,
  14911. chunkMeta: chunkMeta
  14912. });
  14913. chunkMeta.transmuxing.executeEnd = now();
  14914. };
  14915. _proto.resetInitialTimestamp = function resetInitialTimestamp(defaultInitPts) {
  14916. var demuxer = this.demuxer,
  14917. remuxer = this.remuxer;
  14918. if (!demuxer || !remuxer) {
  14919. return;
  14920. }
  14921. demuxer.resetTimeStamp(defaultInitPts);
  14922. remuxer.resetTimeStamp(defaultInitPts);
  14923. };
  14924. _proto.resetContiguity = function resetContiguity() {
  14925. var demuxer = this.demuxer,
  14926. remuxer = this.remuxer;
  14927. if (!demuxer || !remuxer) {
  14928. return;
  14929. }
  14930. demuxer.resetContiguity();
  14931. remuxer.resetNextTimestamp();
  14932. };
  14933. _proto.resetInitSegment = function resetInitSegment(initSegmentData, audioCodec, videoCodec, trackDuration, decryptdata) {
  14934. var demuxer = this.demuxer,
  14935. remuxer = this.remuxer;
  14936. if (!demuxer || !remuxer) {
  14937. return;
  14938. }
  14939. demuxer.resetInitSegment(initSegmentData, audioCodec, videoCodec, trackDuration);
  14940. remuxer.resetInitSegment(initSegmentData, audioCodec, videoCodec, decryptdata);
  14941. };
  14942. _proto.destroy = function destroy() {
  14943. if (this.demuxer) {
  14944. this.demuxer.destroy();
  14945. this.demuxer = undefined;
  14946. }
  14947. if (this.remuxer) {
  14948. this.remuxer.destroy();
  14949. this.remuxer = undefined;
  14950. }
  14951. };
  14952. _proto.transmux = function transmux(data, keyData, timeOffset, accurateTimeOffset, chunkMeta) {
  14953. var result;
  14954. if (keyData && keyData.method === 'SAMPLE-AES') {
  14955. result = this.transmuxSampleAes(data, keyData, timeOffset, accurateTimeOffset, chunkMeta);
  14956. } else {
  14957. result = this.transmuxUnencrypted(data, timeOffset, accurateTimeOffset, chunkMeta);
  14958. }
  14959. return result;
  14960. };
  14961. _proto.transmuxUnencrypted = function transmuxUnencrypted(data, timeOffset, accurateTimeOffset, chunkMeta) {
  14962. var _demux = this.demuxer.demux(data, timeOffset, false, !this.config.progressive),
  14963. audioTrack = _demux.audioTrack,
  14964. videoTrack = _demux.videoTrack,
  14965. id3Track = _demux.id3Track,
  14966. textTrack = _demux.textTrack;
  14967. var remuxResult = this.remuxer.remux(audioTrack, videoTrack, id3Track, textTrack, timeOffset, accurateTimeOffset, false, this.id);
  14968. return {
  14969. remuxResult: remuxResult,
  14970. chunkMeta: chunkMeta
  14971. };
  14972. };
  14973. _proto.transmuxSampleAes = function transmuxSampleAes(data, decryptData, timeOffset, accurateTimeOffset, chunkMeta) {
  14974. var _this3 = this;
  14975. return this.demuxer.demuxSampleAes(data, decryptData, timeOffset).then(function (demuxResult) {
  14976. var remuxResult = _this3.remuxer.remux(demuxResult.audioTrack, demuxResult.videoTrack, demuxResult.id3Track, demuxResult.textTrack, timeOffset, accurateTimeOffset, false, _this3.id);
  14977. return {
  14978. remuxResult: remuxResult,
  14979. chunkMeta: chunkMeta
  14980. };
  14981. });
  14982. };
  14983. _proto.configureTransmuxer = function configureTransmuxer(data) {
  14984. var config = this.config,
  14985. observer = this.observer,
  14986. typeSupported = this.typeSupported,
  14987. vendor = this.vendor;
  14988. // probe for content type
  14989. var mux;
  14990. for (var i = 0, len = muxConfig.length; i < len; i++) {
  14991. var _muxConfig$i$demux;
  14992. if ((_muxConfig$i$demux = muxConfig[i].demux) != null && _muxConfig$i$demux.probe(data)) {
  14993. mux = muxConfig[i];
  14994. break;
  14995. }
  14996. }
  14997. if (!mux) {
  14998. return new Error('Failed to find demuxer by probing fragment data');
  14999. }
  15000. // so let's check that current remuxer and demuxer are still valid
  15001. var demuxer = this.demuxer;
  15002. var remuxer = this.remuxer;
  15003. var Remuxer = mux.remux;
  15004. var Demuxer = mux.demux;
  15005. if (!remuxer || !(remuxer instanceof Remuxer)) {
  15006. this.remuxer = new Remuxer(observer, config, typeSupported, vendor);
  15007. }
  15008. if (!demuxer || !(demuxer instanceof Demuxer)) {
  15009. this.demuxer = new Demuxer(observer, config, typeSupported);
  15010. this.probe = Demuxer.probe;
  15011. }
  15012. };
  15013. _proto.needsProbing = function needsProbing(discontinuity, trackSwitch) {
  15014. // in case of continuity change, or track switch
  15015. // we might switch from content type (AAC container to TS container, or TS to fmp4 for example)
  15016. return !this.demuxer || !this.remuxer || discontinuity || trackSwitch;
  15017. };
  15018. _proto.getDecrypter = function getDecrypter() {
  15019. var decrypter = this.decrypter;
  15020. if (!decrypter) {
  15021. decrypter = this.decrypter = new Decrypter(this.config);
  15022. }
  15023. return decrypter;
  15024. };
  15025. return Transmuxer;
  15026. }();
  15027. function getEncryptionType(data, decryptData) {
  15028. var encryptionType = null;
  15029. if (data.byteLength > 0 && (decryptData == null ? void 0 : decryptData.key) != null && decryptData.iv !== null && decryptData.method != null) {
  15030. encryptionType = decryptData;
  15031. }
  15032. return encryptionType;
  15033. }
  15034. var emptyResult = function emptyResult(chunkMeta) {
  15035. return {
  15036. remuxResult: {},
  15037. chunkMeta: chunkMeta
  15038. };
  15039. };
  15040. function isPromise(p) {
  15041. return 'then' in p && p.then instanceof Function;
  15042. }
  15043. var TransmuxConfig = function TransmuxConfig(audioCodec, videoCodec, initSegmentData, duration, defaultInitPts) {
  15044. this.audioCodec = void 0;
  15045. this.videoCodec = void 0;
  15046. this.initSegmentData = void 0;
  15047. this.duration = void 0;
  15048. this.defaultInitPts = void 0;
  15049. this.audioCodec = audioCodec;
  15050. this.videoCodec = videoCodec;
  15051. this.initSegmentData = initSegmentData;
  15052. this.duration = duration;
  15053. this.defaultInitPts = defaultInitPts || null;
  15054. };
  15055. var TransmuxState = function TransmuxState(discontinuity, contiguous, accurateTimeOffset, trackSwitch, timeOffset, initSegmentChange) {
  15056. this.discontinuity = void 0;
  15057. this.contiguous = void 0;
  15058. this.accurateTimeOffset = void 0;
  15059. this.trackSwitch = void 0;
  15060. this.timeOffset = void 0;
  15061. this.initSegmentChange = void 0;
  15062. this.discontinuity = discontinuity;
  15063. this.contiguous = contiguous;
  15064. this.accurateTimeOffset = accurateTimeOffset;
  15065. this.trackSwitch = trackSwitch;
  15066. this.timeOffset = timeOffset;
  15067. this.initSegmentChange = initSegmentChange;
  15068. };
  15069. var eventemitter3 = {exports: {}};
  15070. (function (module) {
  15071. var has = Object.prototype.hasOwnProperty
  15072. , prefix = '~';
  15073. /**
  15074. * Constructor to create a storage for our `EE` objects.
  15075. * An `Events` instance is a plain object whose properties are event names.
  15076. *
  15077. * @constructor
  15078. * @private
  15079. */
  15080. function Events() {}
  15081. //
  15082. // We try to not inherit from `Object.prototype`. In some engines creating an
  15083. // instance in this way is faster than calling `Object.create(null)` directly.
  15084. // If `Object.create(null)` is not supported we prefix the event names with a
  15085. // character to make sure that the built-in object properties are not
  15086. // overridden or used as an attack vector.
  15087. //
  15088. if (Object.create) {
  15089. Events.prototype = Object.create(null);
  15090. //
  15091. // This hack is needed because the `__proto__` property is still inherited in
  15092. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  15093. //
  15094. if (!new Events().__proto__) prefix = false;
  15095. }
  15096. /**
  15097. * Representation of a single event listener.
  15098. *
  15099. * @param {Function} fn The listener function.
  15100. * @param {*} context The context to invoke the listener with.
  15101. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  15102. * @constructor
  15103. * @private
  15104. */
  15105. function EE(fn, context, once) {
  15106. this.fn = fn;
  15107. this.context = context;
  15108. this.once = once || false;
  15109. }
  15110. /**
  15111. * Add a listener for a given event.
  15112. *
  15113. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  15114. * @param {(String|Symbol)} event The event name.
  15115. * @param {Function} fn The listener function.
  15116. * @param {*} context The context to invoke the listener with.
  15117. * @param {Boolean} once Specify if the listener is a one-time listener.
  15118. * @returns {EventEmitter}
  15119. * @private
  15120. */
  15121. function addListener(emitter, event, fn, context, once) {
  15122. if (typeof fn !== 'function') {
  15123. throw new TypeError('The listener must be a function');
  15124. }
  15125. var listener = new EE(fn, context || emitter, once)
  15126. , evt = prefix ? prefix + event : event;
  15127. if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
  15128. else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
  15129. else emitter._events[evt] = [emitter._events[evt], listener];
  15130. return emitter;
  15131. }
  15132. /**
  15133. * Clear event by name.
  15134. *
  15135. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  15136. * @param {(String|Symbol)} evt The Event name.
  15137. * @private
  15138. */
  15139. function clearEvent(emitter, evt) {
  15140. if (--emitter._eventsCount === 0) emitter._events = new Events();
  15141. else delete emitter._events[evt];
  15142. }
  15143. /**
  15144. * Minimal `EventEmitter` interface that is molded against the Node.js
  15145. * `EventEmitter` interface.
  15146. *
  15147. * @constructor
  15148. * @public
  15149. */
  15150. function EventEmitter() {
  15151. this._events = new Events();
  15152. this._eventsCount = 0;
  15153. }
  15154. /**
  15155. * Return an array listing the events for which the emitter has registered
  15156. * listeners.
  15157. *
  15158. * @returns {Array}
  15159. * @public
  15160. */
  15161. EventEmitter.prototype.eventNames = function eventNames() {
  15162. var names = []
  15163. , events
  15164. , name;
  15165. if (this._eventsCount === 0) return names;
  15166. for (name in (events = this._events)) {
  15167. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  15168. }
  15169. if (Object.getOwnPropertySymbols) {
  15170. return names.concat(Object.getOwnPropertySymbols(events));
  15171. }
  15172. return names;
  15173. };
  15174. /**
  15175. * Return the listeners registered for a given event.
  15176. *
  15177. * @param {(String|Symbol)} event The event name.
  15178. * @returns {Array} The registered listeners.
  15179. * @public
  15180. */
  15181. EventEmitter.prototype.listeners = function listeners(event) {
  15182. var evt = prefix ? prefix + event : event
  15183. , handlers = this._events[evt];
  15184. if (!handlers) return [];
  15185. if (handlers.fn) return [handlers.fn];
  15186. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  15187. ee[i] = handlers[i].fn;
  15188. }
  15189. return ee;
  15190. };
  15191. /**
  15192. * Return the number of listeners listening to a given event.
  15193. *
  15194. * @param {(String|Symbol)} event The event name.
  15195. * @returns {Number} The number of listeners.
  15196. * @public
  15197. */
  15198. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  15199. var evt = prefix ? prefix + event : event
  15200. , listeners = this._events[evt];
  15201. if (!listeners) return 0;
  15202. if (listeners.fn) return 1;
  15203. return listeners.length;
  15204. };
  15205. /**
  15206. * Calls each of the listeners registered for a given event.
  15207. *
  15208. * @param {(String|Symbol)} event The event name.
  15209. * @returns {Boolean} `true` if the event had listeners, else `false`.
  15210. * @public
  15211. */
  15212. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  15213. var evt = prefix ? prefix + event : event;
  15214. if (!this._events[evt]) return false;
  15215. var listeners = this._events[evt]
  15216. , len = arguments.length
  15217. , args
  15218. , i;
  15219. if (listeners.fn) {
  15220. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  15221. switch (len) {
  15222. case 1: return listeners.fn.call(listeners.context), true;
  15223. case 2: return listeners.fn.call(listeners.context, a1), true;
  15224. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  15225. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  15226. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  15227. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  15228. }
  15229. for (i = 1, args = new Array(len -1); i < len; i++) {
  15230. args[i - 1] = arguments[i];
  15231. }
  15232. listeners.fn.apply(listeners.context, args);
  15233. } else {
  15234. var length = listeners.length
  15235. , j;
  15236. for (i = 0; i < length; i++) {
  15237. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  15238. switch (len) {
  15239. case 1: listeners[i].fn.call(listeners[i].context); break;
  15240. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  15241. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  15242. case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
  15243. default:
  15244. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  15245. args[j - 1] = arguments[j];
  15246. }
  15247. listeners[i].fn.apply(listeners[i].context, args);
  15248. }
  15249. }
  15250. }
  15251. return true;
  15252. };
  15253. /**
  15254. * Add a listener for a given event.
  15255. *
  15256. * @param {(String|Symbol)} event The event name.
  15257. * @param {Function} fn The listener function.
  15258. * @param {*} [context=this] The context to invoke the listener with.
  15259. * @returns {EventEmitter} `this`.
  15260. * @public
  15261. */
  15262. EventEmitter.prototype.on = function on(event, fn, context) {
  15263. return addListener(this, event, fn, context, false);
  15264. };
  15265. /**
  15266. * Add a one-time listener for a given event.
  15267. *
  15268. * @param {(String|Symbol)} event The event name.
  15269. * @param {Function} fn The listener function.
  15270. * @param {*} [context=this] The context to invoke the listener with.
  15271. * @returns {EventEmitter} `this`.
  15272. * @public
  15273. */
  15274. EventEmitter.prototype.once = function once(event, fn, context) {
  15275. return addListener(this, event, fn, context, true);
  15276. };
  15277. /**
  15278. * Remove the listeners of a given event.
  15279. *
  15280. * @param {(String|Symbol)} event The event name.
  15281. * @param {Function} fn Only remove the listeners that match this function.
  15282. * @param {*} context Only remove the listeners that have this context.
  15283. * @param {Boolean} once Only remove one-time listeners.
  15284. * @returns {EventEmitter} `this`.
  15285. * @public
  15286. */
  15287. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  15288. var evt = prefix ? prefix + event : event;
  15289. if (!this._events[evt]) return this;
  15290. if (!fn) {
  15291. clearEvent(this, evt);
  15292. return this;
  15293. }
  15294. var listeners = this._events[evt];
  15295. if (listeners.fn) {
  15296. if (
  15297. listeners.fn === fn &&
  15298. (!once || listeners.once) &&
  15299. (!context || listeners.context === context)
  15300. ) {
  15301. clearEvent(this, evt);
  15302. }
  15303. } else {
  15304. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  15305. if (
  15306. listeners[i].fn !== fn ||
  15307. (once && !listeners[i].once) ||
  15308. (context && listeners[i].context !== context)
  15309. ) {
  15310. events.push(listeners[i]);
  15311. }
  15312. }
  15313. //
  15314. // Reset the array, or remove it completely if we have no more listeners.
  15315. //
  15316. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
  15317. else clearEvent(this, evt);
  15318. }
  15319. return this;
  15320. };
  15321. /**
  15322. * Remove all listeners, or those of the specified event.
  15323. *
  15324. * @param {(String|Symbol)} [event] The event name.
  15325. * @returns {EventEmitter} `this`.
  15326. * @public
  15327. */
  15328. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  15329. var evt;
  15330. if (event) {
  15331. evt = prefix ? prefix + event : event;
  15332. if (this._events[evt]) clearEvent(this, evt);
  15333. } else {
  15334. this._events = new Events();
  15335. this._eventsCount = 0;
  15336. }
  15337. return this;
  15338. };
  15339. //
  15340. // Alias methods names because people roll like that.
  15341. //
  15342. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  15343. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  15344. //
  15345. // Expose the prefix.
  15346. //
  15347. EventEmitter.prefixed = prefix;
  15348. //
  15349. // Allow `EventEmitter` to be imported as module namespace.
  15350. //
  15351. EventEmitter.EventEmitter = EventEmitter;
  15352. //
  15353. // Expose the module.
  15354. //
  15355. {
  15356. module.exports = EventEmitter;
  15357. }
  15358. } (eventemitter3));
  15359. var eventemitter3Exports = eventemitter3.exports;
  15360. var EventEmitter = /*@__PURE__*/getDefaultExportFromCjs(eventemitter3Exports);
  15361. if (typeof __IN_WORKER__ !== 'undefined' && __IN_WORKER__) {
  15362. startWorker(self);
  15363. }
  15364. function startWorker(self) {
  15365. var observer = new EventEmitter();
  15366. var forwardMessage = function forwardMessage(ev, data) {
  15367. self.postMessage({
  15368. event: ev,
  15369. data: data
  15370. });
  15371. };
  15372. // forward events to main thread
  15373. observer.on(Events.FRAG_DECRYPTED, forwardMessage);
  15374. observer.on(Events.ERROR, forwardMessage);
  15375. // forward logger events to main thread
  15376. var forwardWorkerLogs = function forwardWorkerLogs() {
  15377. var _loop = function _loop(logFn) {
  15378. var func = function func(message) {
  15379. forwardMessage('workerLog', {
  15380. logType: logFn,
  15381. message: message
  15382. });
  15383. };
  15384. logger[logFn] = func;
  15385. };
  15386. for (var logFn in logger) {
  15387. _loop(logFn);
  15388. }
  15389. };
  15390. self.addEventListener('message', function (ev) {
  15391. var data = ev.data;
  15392. switch (data.cmd) {
  15393. case 'init':
  15394. {
  15395. var config = JSON.parse(data.config);
  15396. self.transmuxer = new Transmuxer(observer, data.typeSupported, config, '', data.id);
  15397. enableLogs(config.debug, data.id);
  15398. forwardWorkerLogs();
  15399. forwardMessage('init', null);
  15400. break;
  15401. }
  15402. case 'configure':
  15403. {
  15404. self.transmuxer.configure(data.config);
  15405. break;
  15406. }
  15407. case 'demux':
  15408. {
  15409. var transmuxResult = self.transmuxer.push(data.data, data.decryptdata, data.chunkMeta, data.state);
  15410. if (isPromise(transmuxResult)) {
  15411. self.transmuxer.async = true;
  15412. transmuxResult.then(function (data) {
  15413. emitTransmuxComplete(self, data);
  15414. }).catch(function (error) {
  15415. forwardMessage(Events.ERROR, {
  15416. type: ErrorTypes.MEDIA_ERROR,
  15417. details: ErrorDetails.FRAG_PARSING_ERROR,
  15418. chunkMeta: data.chunkMeta,
  15419. fatal: false,
  15420. error: error,
  15421. err: error,
  15422. reason: "transmuxer-worker push error"
  15423. });
  15424. });
  15425. } else {
  15426. self.transmuxer.async = false;
  15427. emitTransmuxComplete(self, transmuxResult);
  15428. }
  15429. break;
  15430. }
  15431. case 'flush':
  15432. {
  15433. var id = data.chunkMeta;
  15434. var _transmuxResult = self.transmuxer.flush(id);
  15435. var asyncFlush = isPromise(_transmuxResult);
  15436. if (asyncFlush || self.transmuxer.async) {
  15437. if (!isPromise(_transmuxResult)) {
  15438. _transmuxResult = Promise.resolve(_transmuxResult);
  15439. }
  15440. _transmuxResult.then(function (results) {
  15441. handleFlushResult(self, results, id);
  15442. }).catch(function (error) {
  15443. forwardMessage(Events.ERROR, {
  15444. type: ErrorTypes.MEDIA_ERROR,
  15445. details: ErrorDetails.FRAG_PARSING_ERROR,
  15446. chunkMeta: data.chunkMeta,
  15447. fatal: false,
  15448. error: error,
  15449. err: error,
  15450. reason: "transmuxer-worker flush error"
  15451. });
  15452. });
  15453. } else {
  15454. handleFlushResult(self, _transmuxResult, id);
  15455. }
  15456. break;
  15457. }
  15458. }
  15459. });
  15460. }
  15461. function emitTransmuxComplete(self, transmuxResult) {
  15462. if (isEmptyResult(transmuxResult.remuxResult)) {
  15463. return false;
  15464. }
  15465. var transferable = [];
  15466. var _transmuxResult$remux = transmuxResult.remuxResult,
  15467. audio = _transmuxResult$remux.audio,
  15468. video = _transmuxResult$remux.video;
  15469. if (audio) {
  15470. addToTransferable(transferable, audio);
  15471. }
  15472. if (video) {
  15473. addToTransferable(transferable, video);
  15474. }
  15475. self.postMessage({
  15476. event: 'transmuxComplete',
  15477. data: transmuxResult
  15478. }, transferable);
  15479. return true;
  15480. }
  15481. // Converts data to a transferable object https://developers.google.com/web/updates/2011/12/Transferable-Objects-Lightning-Fast)
  15482. // in order to minimize message passing overhead
  15483. function addToTransferable(transferable, track) {
  15484. if (track.data1) {
  15485. transferable.push(track.data1.buffer);
  15486. }
  15487. if (track.data2) {
  15488. transferable.push(track.data2.buffer);
  15489. }
  15490. }
  15491. function handleFlushResult(self, results, chunkMeta) {
  15492. var parsed = results.reduce(function (parsed, result) {
  15493. return emitTransmuxComplete(self, result) || parsed;
  15494. }, false);
  15495. if (!parsed) {
  15496. // Emit at least one "transmuxComplete" message even if media is not found to update stream-controller state to PARSING
  15497. self.postMessage({
  15498. event: 'transmuxComplete',
  15499. data: results[0]
  15500. });
  15501. }
  15502. self.postMessage({
  15503. event: 'flush',
  15504. data: chunkMeta
  15505. });
  15506. }
  15507. function isEmptyResult(remuxResult) {
  15508. return !remuxResult.audio && !remuxResult.video && !remuxResult.text && !remuxResult.id3 && !remuxResult.initSegment;
  15509. }
  15510. // ensure the worker ends up in the bundle
  15511. // If the worker should not be included this gets aliased to empty.js
  15512. function hasUMDWorker() {
  15513. return typeof __HLS_WORKER_BUNDLE__ === 'function';
  15514. }
  15515. function injectWorker() {
  15516. var blob = new self.Blob(["var exports={};var module={exports:exports};function define(f){f()};define.amd=true;(" + __HLS_WORKER_BUNDLE__.toString() + ")(true);"], {
  15517. type: 'text/javascript'
  15518. });
  15519. var objectURL = self.URL.createObjectURL(blob);
  15520. var worker = new self.Worker(objectURL);
  15521. return {
  15522. worker: worker,
  15523. objectURL: objectURL
  15524. };
  15525. }
  15526. function loadWorker(path) {
  15527. var scriptURL = new self.URL(path, self.location.href).href;
  15528. var worker = new self.Worker(scriptURL);
  15529. return {
  15530. worker: worker,
  15531. scriptURL: scriptURL
  15532. };
  15533. }
  15534. var TransmuxerInterface = /*#__PURE__*/function () {
  15535. function TransmuxerInterface(hls, id, onTransmuxComplete, onFlush) {
  15536. var _this = this;
  15537. this.error = null;
  15538. this.hls = void 0;
  15539. this.id = void 0;
  15540. this.observer = void 0;
  15541. this.frag = null;
  15542. this.part = null;
  15543. this.useWorker = void 0;
  15544. this.workerContext = null;
  15545. this.onwmsg = void 0;
  15546. this.transmuxer = null;
  15547. this.onTransmuxComplete = void 0;
  15548. this.onFlush = void 0;
  15549. var config = hls.config;
  15550. this.hls = hls;
  15551. this.id = id;
  15552. this.useWorker = !!config.enableWorker;
  15553. this.onTransmuxComplete = onTransmuxComplete;
  15554. this.onFlush = onFlush;
  15555. var forwardMessage = function forwardMessage(ev, data) {
  15556. data = data || {};
  15557. data.frag = _this.frag;
  15558. data.id = _this.id;
  15559. if (ev === Events.ERROR) {
  15560. _this.error = data.error;
  15561. }
  15562. _this.hls.trigger(ev, data);
  15563. };
  15564. // forward events to main thread
  15565. this.observer = new EventEmitter();
  15566. this.observer.on(Events.FRAG_DECRYPTED, forwardMessage);
  15567. this.observer.on(Events.ERROR, forwardMessage);
  15568. var MediaSource = getMediaSource(config.preferManagedMediaSource) || {
  15569. isTypeSupported: function isTypeSupported() {
  15570. return false;
  15571. }
  15572. };
  15573. var m2tsTypeSupported = {
  15574. mpeg: MediaSource.isTypeSupported('audio/mpeg'),
  15575. mp3: MediaSource.isTypeSupported('audio/mp4; codecs="mp3"'),
  15576. ac3: MediaSource.isTypeSupported('audio/mp4; codecs="ac-3"')
  15577. };
  15578. if (this.useWorker && typeof Worker !== 'undefined') {
  15579. var canCreateWorker = config.workerPath || hasUMDWorker();
  15580. if (canCreateWorker) {
  15581. try {
  15582. if (config.workerPath) {
  15583. logger.log("loading Web Worker " + config.workerPath + " for \"" + id + "\"");
  15584. this.workerContext = loadWorker(config.workerPath);
  15585. } else {
  15586. logger.log("injecting Web Worker for \"" + id + "\"");
  15587. this.workerContext = injectWorker();
  15588. }
  15589. this.onwmsg = function (event) {
  15590. return _this.onWorkerMessage(event);
  15591. };
  15592. var worker = this.workerContext.worker;
  15593. worker.addEventListener('message', this.onwmsg);
  15594. worker.onerror = function (event) {
  15595. var error = new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
  15596. config.enableWorker = false;
  15597. logger.warn("Error in \"" + id + "\" Web Worker, fallback to inline");
  15598. _this.hls.trigger(Events.ERROR, {
  15599. type: ErrorTypes.OTHER_ERROR,
  15600. details: ErrorDetails.INTERNAL_EXCEPTION,
  15601. fatal: false,
  15602. event: 'demuxerWorker',
  15603. error: error
  15604. });
  15605. };
  15606. worker.postMessage({
  15607. cmd: 'init',
  15608. typeSupported: m2tsTypeSupported,
  15609. vendor: '',
  15610. id: id,
  15611. config: JSON.stringify(config)
  15612. });
  15613. } catch (err) {
  15614. logger.warn("Error setting up \"" + id + "\" Web Worker, fallback to inline", err);
  15615. this.resetWorker();
  15616. this.error = null;
  15617. this.transmuxer = new Transmuxer(this.observer, m2tsTypeSupported, config, '', id);
  15618. }
  15619. return;
  15620. }
  15621. }
  15622. this.transmuxer = new Transmuxer(this.observer, m2tsTypeSupported, config, '', id);
  15623. }
  15624. var _proto = TransmuxerInterface.prototype;
  15625. _proto.resetWorker = function resetWorker() {
  15626. if (this.workerContext) {
  15627. var _this$workerContext = this.workerContext,
  15628. worker = _this$workerContext.worker,
  15629. objectURL = _this$workerContext.objectURL;
  15630. if (objectURL) {
  15631. // revoke the Object URL that was used to create transmuxer worker, so as not to leak it
  15632. self.URL.revokeObjectURL(objectURL);
  15633. }
  15634. worker.removeEventListener('message', this.onwmsg);
  15635. worker.onerror = null;
  15636. worker.terminate();
  15637. this.workerContext = null;
  15638. }
  15639. };
  15640. _proto.destroy = function destroy() {
  15641. if (this.workerContext) {
  15642. this.resetWorker();
  15643. this.onwmsg = undefined;
  15644. } else {
  15645. var transmuxer = this.transmuxer;
  15646. if (transmuxer) {
  15647. transmuxer.destroy();
  15648. this.transmuxer = null;
  15649. }
  15650. }
  15651. var observer = this.observer;
  15652. if (observer) {
  15653. observer.removeAllListeners();
  15654. }
  15655. this.frag = null;
  15656. // @ts-ignore
  15657. this.observer = null;
  15658. // @ts-ignore
  15659. this.hls = null;
  15660. };
  15661. _proto.push = function push(data, initSegmentData, audioCodec, videoCodec, frag, part, duration, accurateTimeOffset, chunkMeta, defaultInitPTS) {
  15662. var _frag$initSegment,
  15663. _lastFrag$initSegment,
  15664. _this2 = this;
  15665. chunkMeta.transmuxing.start = self.performance.now();
  15666. var transmuxer = this.transmuxer;
  15667. var timeOffset = part ? part.start : frag.start;
  15668. // TODO: push "clear-lead" decrypt data for unencrypted fragments in streams with encrypted ones
  15669. var decryptdata = frag.decryptdata;
  15670. var lastFrag = this.frag;
  15671. var discontinuity = !(lastFrag && frag.cc === lastFrag.cc);
  15672. var trackSwitch = !(lastFrag && chunkMeta.level === lastFrag.level);
  15673. var snDiff = lastFrag ? chunkMeta.sn - lastFrag.sn : -1;
  15674. var partDiff = this.part ? chunkMeta.part - this.part.index : -1;
  15675. var progressive = snDiff === 0 && chunkMeta.id > 1 && chunkMeta.id === (lastFrag == null ? void 0 : lastFrag.stats.chunkCount);
  15676. var contiguous = !trackSwitch && (snDiff === 1 || snDiff === 0 && (partDiff === 1 || progressive && partDiff <= 0));
  15677. var now = self.performance.now();
  15678. if (trackSwitch || snDiff || frag.stats.parsing.start === 0) {
  15679. frag.stats.parsing.start = now;
  15680. }
  15681. if (part && (partDiff || !contiguous)) {
  15682. part.stats.parsing.start = now;
  15683. }
  15684. var initSegmentChange = !(lastFrag && ((_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.url) === ((_lastFrag$initSegment = lastFrag.initSegment) == null ? void 0 : _lastFrag$initSegment.url));
  15685. var state = new TransmuxState(discontinuity, contiguous, accurateTimeOffset, trackSwitch, timeOffset, initSegmentChange);
  15686. if (!contiguous || discontinuity || initSegmentChange) {
  15687. logger.log("[transmuxer-interface, " + frag.type + "]: Starting new transmux session for sn: " + chunkMeta.sn + " p: " + chunkMeta.part + " level: " + chunkMeta.level + " id: " + chunkMeta.id + "\n discontinuity: " + discontinuity + "\n trackSwitch: " + trackSwitch + "\n contiguous: " + contiguous + "\n accurateTimeOffset: " + accurateTimeOffset + "\n timeOffset: " + timeOffset + "\n initSegmentChange: " + initSegmentChange);
  15688. var config = new TransmuxConfig(audioCodec, videoCodec, initSegmentData, duration, defaultInitPTS);
  15689. this.configureTransmuxer(config);
  15690. }
  15691. this.frag = frag;
  15692. this.part = part;
  15693. // Frags with sn of 'initSegment' are not transmuxed
  15694. if (this.workerContext) {
  15695. // post fragment payload as transferable objects for ArrayBuffer (no copy)
  15696. this.workerContext.worker.postMessage({
  15697. cmd: 'demux',
  15698. data: data,
  15699. decryptdata: decryptdata,
  15700. chunkMeta: chunkMeta,
  15701. state: state
  15702. }, data instanceof ArrayBuffer ? [data] : []);
  15703. } else if (transmuxer) {
  15704. var _transmuxResult = transmuxer.push(data, decryptdata, chunkMeta, state);
  15705. if (isPromise(_transmuxResult)) {
  15706. transmuxer.async = true;
  15707. _transmuxResult.then(function (data) {
  15708. _this2.handleTransmuxComplete(data);
  15709. }).catch(function (error) {
  15710. _this2.transmuxerError(error, chunkMeta, 'transmuxer-interface push error');
  15711. });
  15712. } else {
  15713. transmuxer.async = false;
  15714. this.handleTransmuxComplete(_transmuxResult);
  15715. }
  15716. }
  15717. };
  15718. _proto.flush = function flush(chunkMeta) {
  15719. var _this3 = this;
  15720. chunkMeta.transmuxing.start = self.performance.now();
  15721. var transmuxer = this.transmuxer;
  15722. if (this.workerContext) {
  15723. this.workerContext.worker.postMessage({
  15724. cmd: 'flush',
  15725. chunkMeta: chunkMeta
  15726. });
  15727. } else if (transmuxer) {
  15728. var _transmuxResult2 = transmuxer.flush(chunkMeta);
  15729. var asyncFlush = isPromise(_transmuxResult2);
  15730. if (asyncFlush || transmuxer.async) {
  15731. if (!isPromise(_transmuxResult2)) {
  15732. _transmuxResult2 = Promise.resolve(_transmuxResult2);
  15733. }
  15734. _transmuxResult2.then(function (data) {
  15735. _this3.handleFlushResult(data, chunkMeta);
  15736. }).catch(function (error) {
  15737. _this3.transmuxerError(error, chunkMeta, 'transmuxer-interface flush error');
  15738. });
  15739. } else {
  15740. this.handleFlushResult(_transmuxResult2, chunkMeta);
  15741. }
  15742. }
  15743. };
  15744. _proto.transmuxerError = function transmuxerError(error, chunkMeta, reason) {
  15745. if (!this.hls) {
  15746. return;
  15747. }
  15748. this.error = error;
  15749. this.hls.trigger(Events.ERROR, {
  15750. type: ErrorTypes.MEDIA_ERROR,
  15751. details: ErrorDetails.FRAG_PARSING_ERROR,
  15752. chunkMeta: chunkMeta,
  15753. frag: this.frag || undefined,
  15754. fatal: false,
  15755. error: error,
  15756. err: error,
  15757. reason: reason
  15758. });
  15759. };
  15760. _proto.handleFlushResult = function handleFlushResult(results, chunkMeta) {
  15761. var _this4 = this;
  15762. results.forEach(function (result) {
  15763. _this4.handleTransmuxComplete(result);
  15764. });
  15765. this.onFlush(chunkMeta);
  15766. };
  15767. _proto.onWorkerMessage = function onWorkerMessage(event) {
  15768. var data = event.data;
  15769. if (!(data != null && data.event)) {
  15770. logger.warn("worker message received with no " + (data ? 'event name' : 'data'));
  15771. return;
  15772. }
  15773. var hls = this.hls;
  15774. if (!this.hls) {
  15775. return;
  15776. }
  15777. switch (data.event) {
  15778. case 'init':
  15779. {
  15780. var _this$workerContext2;
  15781. var objectURL = (_this$workerContext2 = this.workerContext) == null ? void 0 : _this$workerContext2.objectURL;
  15782. if (objectURL) {
  15783. // revoke the Object URL that was used to create transmuxer worker, so as not to leak it
  15784. self.URL.revokeObjectURL(objectURL);
  15785. }
  15786. break;
  15787. }
  15788. case 'transmuxComplete':
  15789. {
  15790. this.handleTransmuxComplete(data.data);
  15791. break;
  15792. }
  15793. case 'flush':
  15794. {
  15795. this.onFlush(data.data);
  15796. break;
  15797. }
  15798. // pass logs from the worker thread to the main logger
  15799. case 'workerLog':
  15800. if (logger[data.data.logType]) {
  15801. logger[data.data.logType](data.data.message);
  15802. }
  15803. break;
  15804. default:
  15805. {
  15806. data.data = data.data || {};
  15807. data.data.frag = this.frag;
  15808. data.data.id = this.id;
  15809. hls.trigger(data.event, data.data);
  15810. break;
  15811. }
  15812. }
  15813. };
  15814. _proto.configureTransmuxer = function configureTransmuxer(config) {
  15815. var transmuxer = this.transmuxer;
  15816. if (this.workerContext) {
  15817. this.workerContext.worker.postMessage({
  15818. cmd: 'configure',
  15819. config: config
  15820. });
  15821. } else if (transmuxer) {
  15822. transmuxer.configure(config);
  15823. }
  15824. };
  15825. _proto.handleTransmuxComplete = function handleTransmuxComplete(result) {
  15826. result.chunkMeta.transmuxing.end = self.performance.now();
  15827. this.onTransmuxComplete(result);
  15828. };
  15829. return TransmuxerInterface;
  15830. }();
  15831. function subtitleOptionsIdentical(trackList1, trackList2) {
  15832. if (trackList1.length !== trackList2.length) {
  15833. return false;
  15834. }
  15835. for (var i = 0; i < trackList1.length; i++) {
  15836. if (!mediaAttributesIdentical(trackList1[i].attrs, trackList2[i].attrs)) {
  15837. return false;
  15838. }
  15839. }
  15840. return true;
  15841. }
  15842. function mediaAttributesIdentical(attrs1, attrs2, customAttributes) {
  15843. // Media options with the same rendition ID must be bit identical
  15844. var stableRenditionId = attrs1['STABLE-RENDITION-ID'];
  15845. if (stableRenditionId && !customAttributes) {
  15846. return stableRenditionId === attrs2['STABLE-RENDITION-ID'];
  15847. }
  15848. // When rendition ID is not present, compare attributes
  15849. return !(customAttributes || ['LANGUAGE', 'NAME', 'CHARACTERISTICS', 'AUTOSELECT', 'DEFAULT', 'FORCED', 'ASSOC-LANGUAGE']).some(function (subtitleAttribute) {
  15850. return attrs1[subtitleAttribute] !== attrs2[subtitleAttribute];
  15851. });
  15852. }
  15853. function subtitleTrackMatchesTextTrack(subtitleTrack, textTrack) {
  15854. return textTrack.label.toLowerCase() === subtitleTrack.name.toLowerCase() && (!textTrack.language || textTrack.language.toLowerCase() === (subtitleTrack.lang || '').toLowerCase());
  15855. }
  15856. var TICK_INTERVAL$2 = 100; // how often to tick in ms
  15857. var AudioStreamController = /*#__PURE__*/function (_BaseStreamController) {
  15858. _inheritsLoose(AudioStreamController, _BaseStreamController);
  15859. function AudioStreamController(hls, fragmentTracker, keyLoader) {
  15860. var _this;
  15861. _this = _BaseStreamController.call(this, hls, fragmentTracker, keyLoader, '[audio-stream-controller]', PlaylistLevelType.AUDIO) || this;
  15862. _this.videoBuffer = null;
  15863. _this.videoTrackCC = -1;
  15864. _this.waitingVideoCC = -1;
  15865. _this.bufferedTrack = null;
  15866. _this.switchingTrack = null;
  15867. _this.trackId = -1;
  15868. _this.waitingData = null;
  15869. _this.mainDetails = null;
  15870. _this.flushing = false;
  15871. _this.bufferFlushed = false;
  15872. _this.cachedTrackLoadedData = null;
  15873. _this._registerListeners();
  15874. return _this;
  15875. }
  15876. var _proto = AudioStreamController.prototype;
  15877. _proto.onHandlerDestroying = function onHandlerDestroying() {
  15878. this._unregisterListeners();
  15879. _BaseStreamController.prototype.onHandlerDestroying.call(this);
  15880. this.mainDetails = null;
  15881. this.bufferedTrack = null;
  15882. this.switchingTrack = null;
  15883. };
  15884. _proto._registerListeners = function _registerListeners() {
  15885. var hls = this.hls;
  15886. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  15887. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  15888. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  15889. hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  15890. hls.on(Events.AUDIO_TRACKS_UPDATED, this.onAudioTracksUpdated, this);
  15891. hls.on(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
  15892. hls.on(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
  15893. hls.on(Events.ERROR, this.onError, this);
  15894. hls.on(Events.BUFFER_RESET, this.onBufferReset, this);
  15895. hls.on(Events.BUFFER_CREATED, this.onBufferCreated, this);
  15896. hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  15897. hls.on(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
  15898. hls.on(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
  15899. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  15900. };
  15901. _proto._unregisterListeners = function _unregisterListeners() {
  15902. var hls = this.hls;
  15903. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  15904. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  15905. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  15906. hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  15907. hls.off(Events.AUDIO_TRACKS_UPDATED, this.onAudioTracksUpdated, this);
  15908. hls.off(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
  15909. hls.off(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
  15910. hls.off(Events.ERROR, this.onError, this);
  15911. hls.off(Events.BUFFER_RESET, this.onBufferReset, this);
  15912. hls.off(Events.BUFFER_CREATED, this.onBufferCreated, this);
  15913. hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  15914. hls.off(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
  15915. hls.off(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
  15916. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  15917. }
  15918. // INIT_PTS_FOUND is triggered when the video track parsed in the stream-controller has a new PTS value
  15919. ;
  15920. _proto.onInitPtsFound = function onInitPtsFound(event, _ref) {
  15921. var frag = _ref.frag,
  15922. id = _ref.id,
  15923. initPTS = _ref.initPTS,
  15924. timescale = _ref.timescale;
  15925. // Always update the new INIT PTS
  15926. // Can change due level switch
  15927. if (id === 'main') {
  15928. var cc = frag.cc;
  15929. this.initPTS[frag.cc] = {
  15930. baseTime: initPTS,
  15931. timescale: timescale
  15932. };
  15933. this.log("InitPTS for cc: " + cc + " found from main: " + initPTS);
  15934. this.videoTrackCC = cc;
  15935. // If we are waiting, tick immediately to unblock audio fragment transmuxing
  15936. if (this.state === State.WAITING_INIT_PTS) {
  15937. this.tick();
  15938. }
  15939. }
  15940. };
  15941. _proto.startLoad = function startLoad(startPosition) {
  15942. if (!this.levels) {
  15943. this.startPosition = startPosition;
  15944. this.state = State.STOPPED;
  15945. return;
  15946. }
  15947. var lastCurrentTime = this.lastCurrentTime;
  15948. this.stopLoad();
  15949. this.setInterval(TICK_INTERVAL$2);
  15950. if (lastCurrentTime > 0 && startPosition === -1) {
  15951. this.log("Override startPosition with lastCurrentTime @" + lastCurrentTime.toFixed(3));
  15952. startPosition = lastCurrentTime;
  15953. this.state = State.IDLE;
  15954. } else {
  15955. this.loadedmetadata = false;
  15956. this.state = State.WAITING_TRACK;
  15957. }
  15958. this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
  15959. this.tick();
  15960. };
  15961. _proto.doTick = function doTick() {
  15962. switch (this.state) {
  15963. case State.IDLE:
  15964. this.doTickIdle();
  15965. break;
  15966. case State.WAITING_TRACK:
  15967. {
  15968. var _levels$trackId;
  15969. var levels = this.levels,
  15970. trackId = this.trackId;
  15971. var details = levels == null ? void 0 : (_levels$trackId = levels[trackId]) == null ? void 0 : _levels$trackId.details;
  15972. if (details) {
  15973. if (this.waitForCdnTuneIn(details)) {
  15974. break;
  15975. }
  15976. this.state = State.WAITING_INIT_PTS;
  15977. }
  15978. break;
  15979. }
  15980. case State.FRAG_LOADING_WAITING_RETRY:
  15981. {
  15982. var _this$media;
  15983. var now = performance.now();
  15984. var retryDate = this.retryDate;
  15985. // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading
  15986. if (!retryDate || now >= retryDate || (_this$media = this.media) != null && _this$media.seeking) {
  15987. var _levels = this.levels,
  15988. _trackId = this.trackId;
  15989. this.log('RetryDate reached, switch back to IDLE state');
  15990. this.resetStartWhenNotLoaded((_levels == null ? void 0 : _levels[_trackId]) || null);
  15991. this.state = State.IDLE;
  15992. }
  15993. break;
  15994. }
  15995. case State.WAITING_INIT_PTS:
  15996. {
  15997. // Ensure we don't get stuck in the WAITING_INIT_PTS state if the waiting frag CC doesn't match any initPTS
  15998. var waitingData = this.waitingData;
  15999. if (waitingData) {
  16000. var frag = waitingData.frag,
  16001. part = waitingData.part,
  16002. cache = waitingData.cache,
  16003. complete = waitingData.complete;
  16004. if (this.initPTS[frag.cc] !== undefined) {
  16005. this.waitingData = null;
  16006. this.waitingVideoCC = -1;
  16007. this.state = State.FRAG_LOADING;
  16008. var payload = cache.flush();
  16009. var data = {
  16010. frag: frag,
  16011. part: part,
  16012. payload: payload,
  16013. networkDetails: null
  16014. };
  16015. this._handleFragmentLoadProgress(data);
  16016. if (complete) {
  16017. _BaseStreamController.prototype._handleFragmentLoadComplete.call(this, data);
  16018. }
  16019. } else if (this.videoTrackCC !== this.waitingVideoCC) {
  16020. // Drop waiting fragment if videoTrackCC has changed since waitingFragment was set and initPTS was not found
  16021. this.log("Waiting fragment cc (" + frag.cc + ") cancelled because video is at cc " + this.videoTrackCC);
  16022. this.clearWaitingFragment();
  16023. } else {
  16024. // Drop waiting fragment if an earlier fragment is needed
  16025. var pos = this.getLoadPosition();
  16026. var bufferInfo = BufferHelper.bufferInfo(this.mediaBuffer, pos, this.config.maxBufferHole);
  16027. var waitingFragmentAtPosition = fragmentWithinToleranceTest(bufferInfo.end, this.config.maxFragLookUpTolerance, frag);
  16028. if (waitingFragmentAtPosition < 0) {
  16029. this.log("Waiting fragment cc (" + frag.cc + ") @ " + frag.start + " cancelled because another fragment at " + bufferInfo.end + " is needed");
  16030. this.clearWaitingFragment();
  16031. }
  16032. }
  16033. } else {
  16034. this.state = State.IDLE;
  16035. }
  16036. }
  16037. }
  16038. this.onTickEnd();
  16039. };
  16040. _proto.clearWaitingFragment = function clearWaitingFragment() {
  16041. var waitingData = this.waitingData;
  16042. if (waitingData) {
  16043. this.fragmentTracker.removeFragment(waitingData.frag);
  16044. this.waitingData = null;
  16045. this.waitingVideoCC = -1;
  16046. this.state = State.IDLE;
  16047. }
  16048. };
  16049. _proto.resetLoadingState = function resetLoadingState() {
  16050. this.clearWaitingFragment();
  16051. _BaseStreamController.prototype.resetLoadingState.call(this);
  16052. };
  16053. _proto.onTickEnd = function onTickEnd() {
  16054. var media = this.media;
  16055. if (!(media != null && media.readyState)) {
  16056. // Exit early if we don't have media or if the media hasn't buffered anything yet (readyState 0)
  16057. return;
  16058. }
  16059. this.lastCurrentTime = media.currentTime;
  16060. };
  16061. _proto.doTickIdle = function doTickIdle() {
  16062. var hls = this.hls,
  16063. levels = this.levels,
  16064. media = this.media,
  16065. trackId = this.trackId;
  16066. var config = hls.config;
  16067. // 1. if video not attached AND
  16068. // start fragment already requested OR start frag prefetch not enabled
  16069. // 2. if tracks or track not loaded and selected
  16070. // then exit loop
  16071. // => if media not attached but start frag prefetch is enabled and start frag not requested yet, we will not exit loop
  16072. if (!media && (this.startFragRequested || !config.startFragPrefetch) || !(levels != null && levels[trackId])) {
  16073. return;
  16074. }
  16075. var levelInfo = levels[trackId];
  16076. var trackDetails = levelInfo.details;
  16077. if (!trackDetails || trackDetails.live && this.levelLastLoaded !== levelInfo || this.waitForCdnTuneIn(trackDetails)) {
  16078. this.state = State.WAITING_TRACK;
  16079. return;
  16080. }
  16081. var bufferable = this.mediaBuffer ? this.mediaBuffer : this.media;
  16082. if (this.bufferFlushed && bufferable) {
  16083. this.bufferFlushed = false;
  16084. this.afterBufferFlushed(bufferable, ElementaryStreamTypes.AUDIO, PlaylistLevelType.AUDIO);
  16085. }
  16086. var bufferInfo = this.getFwdBufferInfo(bufferable, PlaylistLevelType.AUDIO);
  16087. if (bufferInfo === null) {
  16088. return;
  16089. }
  16090. var bufferedTrack = this.bufferedTrack,
  16091. switchingTrack = this.switchingTrack;
  16092. if (!switchingTrack && this._streamEnded(bufferInfo, trackDetails)) {
  16093. hls.trigger(Events.BUFFER_EOS, {
  16094. type: 'audio'
  16095. });
  16096. this.state = State.ENDED;
  16097. return;
  16098. }
  16099. var mainBufferInfo = this.getFwdBufferInfo(this.videoBuffer ? this.videoBuffer : this.media, PlaylistLevelType.MAIN);
  16100. var bufferLen = bufferInfo.len;
  16101. var maxBufLen = this.getMaxBufferLength(mainBufferInfo == null ? void 0 : mainBufferInfo.len);
  16102. var fragments = trackDetails.fragments;
  16103. var start = fragments[0].start;
  16104. var targetBufferTime = this.flushing ? this.getLoadPosition() : bufferInfo.end;
  16105. if (switchingTrack && media) {
  16106. var pos = this.getLoadPosition();
  16107. // STABLE
  16108. if (bufferedTrack && !mediaAttributesIdentical(switchingTrack.attrs, bufferedTrack.attrs)) {
  16109. targetBufferTime = pos;
  16110. }
  16111. // if currentTime (pos) is less than alt audio playlist start time, it means that alt audio is ahead of currentTime
  16112. if (trackDetails.PTSKnown && pos < start) {
  16113. // if everything is buffered from pos to start or if audio buffer upfront, let's seek to start
  16114. if (bufferInfo.end > start || bufferInfo.nextStart) {
  16115. this.log('Alt audio track ahead of main track, seek to start of alt audio track');
  16116. media.currentTime = start + 0.05;
  16117. }
  16118. }
  16119. }
  16120. // if buffer length is less than maxBufLen, or near the end, find a fragment to load
  16121. if (bufferLen >= maxBufLen && !switchingTrack && targetBufferTime < fragments[fragments.length - 1].start) {
  16122. return;
  16123. }
  16124. var frag = this.getNextFragment(targetBufferTime, trackDetails);
  16125. var atGap = false;
  16126. // Avoid loop loading by using nextLoadPosition set for backtracking and skipping consecutive GAP tags
  16127. if (frag && this.isLoopLoading(frag, targetBufferTime)) {
  16128. atGap = !!frag.gap;
  16129. frag = this.getNextFragmentLoopLoading(frag, trackDetails, bufferInfo, PlaylistLevelType.MAIN, maxBufLen);
  16130. }
  16131. if (!frag) {
  16132. this.bufferFlushed = true;
  16133. return;
  16134. }
  16135. // Buffer audio up to one target duration ahead of main buffer
  16136. var atBufferSyncLimit = mainBufferInfo && frag.start > mainBufferInfo.end + trackDetails.targetduration;
  16137. if (atBufferSyncLimit ||
  16138. // Or wait for main buffer after buffing some audio
  16139. !(mainBufferInfo != null && mainBufferInfo.len) && bufferInfo.len) {
  16140. // Check fragment-tracker for main fragments since GAP segments do not show up in bufferInfo
  16141. var mainFrag = this.getAppendedFrag(frag.start, PlaylistLevelType.MAIN);
  16142. if (mainFrag === null) {
  16143. return;
  16144. }
  16145. // Bridge gaps in main buffer
  16146. atGap || (atGap = !!mainFrag.gap || !!atBufferSyncLimit && mainBufferInfo.len === 0);
  16147. if (atBufferSyncLimit && !atGap || atGap && bufferInfo.nextStart && bufferInfo.nextStart < mainFrag.end) {
  16148. return;
  16149. }
  16150. }
  16151. this.loadFragment(frag, levelInfo, targetBufferTime);
  16152. };
  16153. _proto.getMaxBufferLength = function getMaxBufferLength(mainBufferLength) {
  16154. var maxConfigBuffer = _BaseStreamController.prototype.getMaxBufferLength.call(this);
  16155. if (!mainBufferLength) {
  16156. return maxConfigBuffer;
  16157. }
  16158. return Math.min(Math.max(maxConfigBuffer, mainBufferLength), this.config.maxMaxBufferLength);
  16159. };
  16160. _proto.onMediaDetaching = function onMediaDetaching() {
  16161. this.videoBuffer = null;
  16162. this.bufferFlushed = this.flushing = false;
  16163. _BaseStreamController.prototype.onMediaDetaching.call(this);
  16164. };
  16165. _proto.onAudioTracksUpdated = function onAudioTracksUpdated(event, _ref2) {
  16166. var audioTracks = _ref2.audioTracks;
  16167. // Reset tranxmuxer is essential for large context switches (Content Steering)
  16168. this.resetTransmuxer();
  16169. this.levels = audioTracks.map(function (mediaPlaylist) {
  16170. return new Level(mediaPlaylist);
  16171. });
  16172. };
  16173. _proto.onAudioTrackSwitching = function onAudioTrackSwitching(event, data) {
  16174. // if any URL found on new audio track, it is an alternate audio track
  16175. var altAudio = !!data.url;
  16176. this.trackId = data.id;
  16177. var fragCurrent = this.fragCurrent;
  16178. if (fragCurrent) {
  16179. fragCurrent.abortRequests();
  16180. this.removeUnbufferedFrags(fragCurrent.start);
  16181. }
  16182. this.resetLoadingState();
  16183. // destroy useless transmuxer when switching audio to main
  16184. if (!altAudio) {
  16185. this.resetTransmuxer();
  16186. } else {
  16187. // switching to audio track, start timer if not already started
  16188. this.setInterval(TICK_INTERVAL$2);
  16189. }
  16190. // should we switch tracks ?
  16191. if (altAudio) {
  16192. this.switchingTrack = data;
  16193. // main audio track are handled by stream-controller, just do something if switching to alt audio track
  16194. this.state = State.IDLE;
  16195. this.flushAudioIfNeeded(data);
  16196. } else {
  16197. this.switchingTrack = null;
  16198. this.bufferedTrack = data;
  16199. this.state = State.STOPPED;
  16200. }
  16201. this.tick();
  16202. };
  16203. _proto.onManifestLoading = function onManifestLoading() {
  16204. this.fragmentTracker.removeAllFragments();
  16205. this.startPosition = this.lastCurrentTime = 0;
  16206. this.bufferFlushed = this.flushing = false;
  16207. this.levels = this.mainDetails = this.waitingData = this.bufferedTrack = this.cachedTrackLoadedData = this.switchingTrack = null;
  16208. this.startFragRequested = false;
  16209. this.trackId = this.videoTrackCC = this.waitingVideoCC = -1;
  16210. };
  16211. _proto.onLevelLoaded = function onLevelLoaded(event, data) {
  16212. this.mainDetails = data.details;
  16213. if (this.cachedTrackLoadedData !== null) {
  16214. this.hls.trigger(Events.AUDIO_TRACK_LOADED, this.cachedTrackLoadedData);
  16215. this.cachedTrackLoadedData = null;
  16216. }
  16217. };
  16218. _proto.onAudioTrackLoaded = function onAudioTrackLoaded(event, data) {
  16219. var _track$details;
  16220. if (this.mainDetails == null) {
  16221. this.cachedTrackLoadedData = data;
  16222. return;
  16223. }
  16224. var levels = this.levels;
  16225. var newDetails = data.details,
  16226. trackId = data.id;
  16227. if (!levels) {
  16228. this.warn("Audio tracks were reset while loading level " + trackId);
  16229. return;
  16230. }
  16231. this.log("Audio track " + trackId + " loaded [" + newDetails.startSN + "," + newDetails.endSN + "]" + (newDetails.lastPartSn ? "[part-" + newDetails.lastPartSn + "-" + newDetails.lastPartIndex + "]" : '') + ",duration:" + newDetails.totalduration);
  16232. var track = levels[trackId];
  16233. var sliding = 0;
  16234. if (newDetails.live || (_track$details = track.details) != null && _track$details.live) {
  16235. this.checkLiveUpdate(newDetails);
  16236. var mainDetails = this.mainDetails;
  16237. if (newDetails.deltaUpdateFailed || !mainDetails) {
  16238. return;
  16239. }
  16240. if (!track.details && newDetails.hasProgramDateTime && mainDetails.hasProgramDateTime) {
  16241. // Make sure our audio rendition is aligned with the "main" rendition, using
  16242. // pdt as our reference times.
  16243. alignMediaPlaylistByPDT(newDetails, mainDetails);
  16244. sliding = newDetails.fragments[0].start;
  16245. } else {
  16246. var _this$levelLastLoaded;
  16247. sliding = this.alignPlaylists(newDetails, track.details, (_this$levelLastLoaded = this.levelLastLoaded) == null ? void 0 : _this$levelLastLoaded.details);
  16248. }
  16249. }
  16250. track.details = newDetails;
  16251. this.levelLastLoaded = track;
  16252. // compute start position if we are aligned with the main playlist
  16253. if (!this.startFragRequested && (this.mainDetails || !newDetails.live)) {
  16254. this.setStartPosition(this.mainDetails || newDetails, sliding);
  16255. }
  16256. // only switch back to IDLE state if we were waiting for track to start downloading a new fragment
  16257. if (this.state === State.WAITING_TRACK && !this.waitForCdnTuneIn(newDetails)) {
  16258. this.state = State.IDLE;
  16259. }
  16260. // trigger handler right now
  16261. this.tick();
  16262. };
  16263. _proto._handleFragmentLoadProgress = function _handleFragmentLoadProgress(data) {
  16264. var _frag$initSegment;
  16265. var frag = data.frag,
  16266. part = data.part,
  16267. payload = data.payload;
  16268. var config = this.config,
  16269. trackId = this.trackId,
  16270. levels = this.levels;
  16271. if (!levels) {
  16272. this.warn("Audio tracks were reset while fragment load was in progress. Fragment " + frag.sn + " of level " + frag.level + " will not be buffered");
  16273. return;
  16274. }
  16275. var track = levels[trackId];
  16276. if (!track) {
  16277. this.warn('Audio track is undefined on fragment load progress');
  16278. return;
  16279. }
  16280. var details = track.details;
  16281. if (!details) {
  16282. this.warn('Audio track details undefined on fragment load progress');
  16283. this.removeUnbufferedFrags(frag.start);
  16284. return;
  16285. }
  16286. var audioCodec = config.defaultAudioCodec || track.audioCodec || 'mp4a.40.2';
  16287. var transmuxer = this.transmuxer;
  16288. if (!transmuxer) {
  16289. transmuxer = this.transmuxer = new TransmuxerInterface(this.hls, PlaylistLevelType.AUDIO, this._handleTransmuxComplete.bind(this), this._handleTransmuxerFlush.bind(this));
  16290. }
  16291. // Check if we have video initPTS
  16292. // If not we need to wait for it
  16293. var initPTS = this.initPTS[frag.cc];
  16294. var initSegmentData = (_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.data;
  16295. if (initPTS !== undefined) {
  16296. // this.log(`Transmuxing ${sn} of [${details.startSN} ,${details.endSN}],track ${trackId}`);
  16297. // time Offset is accurate if level PTS is known, or if playlist is not sliding (not live)
  16298. var accurateTimeOffset = false; // details.PTSKnown || !details.live;
  16299. var partIndex = part ? part.index : -1;
  16300. var partial = partIndex !== -1;
  16301. var chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount, payload.byteLength, partIndex, partial);
  16302. transmuxer.push(payload, initSegmentData, audioCodec, '', frag, part, details.totalduration, accurateTimeOffset, chunkMeta, initPTS);
  16303. } else {
  16304. this.log("Unknown video PTS for cc " + frag.cc + ", waiting for video PTS before demuxing audio frag " + frag.sn + " of [" + details.startSN + " ," + details.endSN + "],track " + trackId);
  16305. var _this$waitingData = this.waitingData = this.waitingData || {
  16306. frag: frag,
  16307. part: part,
  16308. cache: new ChunkCache(),
  16309. complete: false
  16310. },
  16311. cache = _this$waitingData.cache;
  16312. cache.push(new Uint8Array(payload));
  16313. this.waitingVideoCC = this.videoTrackCC;
  16314. this.state = State.WAITING_INIT_PTS;
  16315. }
  16316. };
  16317. _proto._handleFragmentLoadComplete = function _handleFragmentLoadComplete(fragLoadedData) {
  16318. if (this.waitingData) {
  16319. this.waitingData.complete = true;
  16320. return;
  16321. }
  16322. _BaseStreamController.prototype._handleFragmentLoadComplete.call(this, fragLoadedData);
  16323. };
  16324. _proto.onBufferReset = function onBufferReset( /* event: Events.BUFFER_RESET */
  16325. ) {
  16326. // reset reference to sourcebuffers
  16327. this.mediaBuffer = this.videoBuffer = null;
  16328. this.loadedmetadata = false;
  16329. };
  16330. _proto.onBufferCreated = function onBufferCreated(event, data) {
  16331. var audioTrack = data.tracks.audio;
  16332. if (audioTrack) {
  16333. this.mediaBuffer = audioTrack.buffer || null;
  16334. }
  16335. if (data.tracks.video) {
  16336. this.videoBuffer = data.tracks.video.buffer || null;
  16337. }
  16338. };
  16339. _proto.onFragBuffered = function onFragBuffered(event, data) {
  16340. var frag = data.frag,
  16341. part = data.part;
  16342. if (frag.type !== PlaylistLevelType.AUDIO) {
  16343. if (!this.loadedmetadata && frag.type === PlaylistLevelType.MAIN) {
  16344. var bufferable = this.videoBuffer || this.media;
  16345. if (bufferable) {
  16346. var bufferedTimeRanges = BufferHelper.getBuffered(bufferable);
  16347. if (bufferedTimeRanges.length) {
  16348. this.loadedmetadata = true;
  16349. }
  16350. }
  16351. }
  16352. return;
  16353. }
  16354. if (this.fragContextChanged(frag)) {
  16355. // If a level switch was requested while a fragment was buffering, it will emit the FRAG_BUFFERED event upon completion
  16356. // Avoid setting state back to IDLE or concluding the audio switch; otherwise, the switched-to track will not buffer
  16357. this.warn("Fragment " + frag.sn + (part ? ' p: ' + part.index : '') + " of level " + frag.level + " finished buffering, but was aborted. state: " + this.state + ", audioSwitch: " + (this.switchingTrack ? this.switchingTrack.name : 'false'));
  16358. return;
  16359. }
  16360. if (frag.sn !== 'initSegment') {
  16361. this.fragPrevious = frag;
  16362. var track = this.switchingTrack;
  16363. if (track) {
  16364. this.bufferedTrack = track;
  16365. this.switchingTrack = null;
  16366. this.hls.trigger(Events.AUDIO_TRACK_SWITCHED, _objectSpread2({}, track));
  16367. }
  16368. }
  16369. this.fragBufferedComplete(frag, part);
  16370. };
  16371. _proto.onError = function onError(event, data) {
  16372. var _data$context;
  16373. if (data.fatal) {
  16374. this.state = State.ERROR;
  16375. return;
  16376. }
  16377. switch (data.details) {
  16378. case ErrorDetails.FRAG_GAP:
  16379. case ErrorDetails.FRAG_PARSING_ERROR:
  16380. case ErrorDetails.FRAG_DECRYPT_ERROR:
  16381. case ErrorDetails.FRAG_LOAD_ERROR:
  16382. case ErrorDetails.FRAG_LOAD_TIMEOUT:
  16383. case ErrorDetails.KEY_LOAD_ERROR:
  16384. case ErrorDetails.KEY_LOAD_TIMEOUT:
  16385. this.onFragmentOrKeyLoadError(PlaylistLevelType.AUDIO, data);
  16386. break;
  16387. case ErrorDetails.AUDIO_TRACK_LOAD_ERROR:
  16388. case ErrorDetails.AUDIO_TRACK_LOAD_TIMEOUT:
  16389. case ErrorDetails.LEVEL_PARSING_ERROR:
  16390. // in case of non fatal error while loading track, if not retrying to load track, switch back to IDLE
  16391. if (!data.levelRetry && this.state === State.WAITING_TRACK && ((_data$context = data.context) == null ? void 0 : _data$context.type) === PlaylistContextType.AUDIO_TRACK) {
  16392. this.state = State.IDLE;
  16393. }
  16394. break;
  16395. case ErrorDetails.BUFFER_APPEND_ERROR:
  16396. case ErrorDetails.BUFFER_FULL_ERROR:
  16397. if (!data.parent || data.parent !== 'audio') {
  16398. return;
  16399. }
  16400. if (data.details === ErrorDetails.BUFFER_APPEND_ERROR) {
  16401. this.resetLoadingState();
  16402. return;
  16403. }
  16404. if (this.reduceLengthAndFlushBuffer(data)) {
  16405. this.bufferedTrack = null;
  16406. _BaseStreamController.prototype.flushMainBuffer.call(this, 0, Number.POSITIVE_INFINITY, 'audio');
  16407. }
  16408. break;
  16409. case ErrorDetails.INTERNAL_EXCEPTION:
  16410. this.recoverWorkerError(data);
  16411. break;
  16412. }
  16413. };
  16414. _proto.onBufferFlushing = function onBufferFlushing(event, _ref3) {
  16415. var type = _ref3.type;
  16416. if (type !== ElementaryStreamTypes.VIDEO) {
  16417. this.flushing = true;
  16418. }
  16419. };
  16420. _proto.onBufferFlushed = function onBufferFlushed(event, _ref4) {
  16421. var type = _ref4.type;
  16422. if (type !== ElementaryStreamTypes.VIDEO) {
  16423. this.flushing = false;
  16424. this.bufferFlushed = true;
  16425. if (this.state === State.ENDED) {
  16426. this.state = State.IDLE;
  16427. }
  16428. var mediaBuffer = this.mediaBuffer || this.media;
  16429. if (mediaBuffer) {
  16430. this.afterBufferFlushed(mediaBuffer, type, PlaylistLevelType.AUDIO);
  16431. this.tick();
  16432. }
  16433. }
  16434. };
  16435. _proto._handleTransmuxComplete = function _handleTransmuxComplete(transmuxResult) {
  16436. var _id3$samples;
  16437. var id = 'audio';
  16438. var hls = this.hls;
  16439. var remuxResult = transmuxResult.remuxResult,
  16440. chunkMeta = transmuxResult.chunkMeta;
  16441. var context = this.getCurrentContext(chunkMeta);
  16442. if (!context) {
  16443. this.resetWhenMissingContext(chunkMeta);
  16444. return;
  16445. }
  16446. var frag = context.frag,
  16447. part = context.part,
  16448. level = context.level;
  16449. var details = level.details;
  16450. var audio = remuxResult.audio,
  16451. text = remuxResult.text,
  16452. id3 = remuxResult.id3,
  16453. initSegment = remuxResult.initSegment;
  16454. // Check if the current fragment has been aborted. We check this by first seeing if we're still playing the current level.
  16455. // If we are, subsequently check if the currently loading fragment (fragCurrent) has changed.
  16456. if (this.fragContextChanged(frag) || !details) {
  16457. this.fragmentTracker.removeFragment(frag);
  16458. return;
  16459. }
  16460. this.state = State.PARSING;
  16461. if (this.switchingTrack && audio) {
  16462. this.completeAudioSwitch(this.switchingTrack);
  16463. }
  16464. if (initSegment != null && initSegment.tracks) {
  16465. var mapFragment = frag.initSegment || frag;
  16466. this._bufferInitSegment(level, initSegment.tracks, mapFragment, chunkMeta);
  16467. hls.trigger(Events.FRAG_PARSING_INIT_SEGMENT, {
  16468. frag: mapFragment,
  16469. id: id,
  16470. tracks: initSegment.tracks
  16471. });
  16472. // Only flush audio from old audio tracks when PTS is known on new audio track
  16473. }
  16474. if (audio) {
  16475. var startPTS = audio.startPTS,
  16476. endPTS = audio.endPTS,
  16477. startDTS = audio.startDTS,
  16478. endDTS = audio.endDTS;
  16479. if (part) {
  16480. part.elementaryStreams[ElementaryStreamTypes.AUDIO] = {
  16481. startPTS: startPTS,
  16482. endPTS: endPTS,
  16483. startDTS: startDTS,
  16484. endDTS: endDTS
  16485. };
  16486. }
  16487. frag.setElementaryStreamInfo(ElementaryStreamTypes.AUDIO, startPTS, endPTS, startDTS, endDTS);
  16488. this.bufferFragmentData(audio, frag, part, chunkMeta);
  16489. }
  16490. if (id3 != null && (_id3$samples = id3.samples) != null && _id3$samples.length) {
  16491. var emittedID3 = _extends({
  16492. id: id,
  16493. frag: frag,
  16494. details: details
  16495. }, id3);
  16496. hls.trigger(Events.FRAG_PARSING_METADATA, emittedID3);
  16497. }
  16498. if (text) {
  16499. var emittedText = _extends({
  16500. id: id,
  16501. frag: frag,
  16502. details: details
  16503. }, text);
  16504. hls.trigger(Events.FRAG_PARSING_USERDATA, emittedText);
  16505. }
  16506. };
  16507. _proto._bufferInitSegment = function _bufferInitSegment(currentLevel, tracks, frag, chunkMeta) {
  16508. if (this.state !== State.PARSING) {
  16509. return;
  16510. }
  16511. // delete any video track found on audio transmuxer
  16512. if (tracks.video) {
  16513. delete tracks.video;
  16514. }
  16515. // include levelCodec in audio and video tracks
  16516. var track = tracks.audio;
  16517. if (!track) {
  16518. return;
  16519. }
  16520. track.id = 'audio';
  16521. var variantAudioCodecs = currentLevel.audioCodec;
  16522. this.log("Init audio buffer, container:" + track.container + ", codecs[level/parsed]=[" + variantAudioCodecs + "/" + track.codec + "]");
  16523. // SourceBuffer will use track.levelCodec if defined
  16524. if (variantAudioCodecs && variantAudioCodecs.split(',').length === 1) {
  16525. track.levelCodec = variantAudioCodecs;
  16526. }
  16527. this.hls.trigger(Events.BUFFER_CODECS, tracks);
  16528. var initSegment = track.initSegment;
  16529. if (initSegment != null && initSegment.byteLength) {
  16530. var segment = {
  16531. type: 'audio',
  16532. frag: frag,
  16533. part: null,
  16534. chunkMeta: chunkMeta,
  16535. parent: frag.type,
  16536. data: initSegment
  16537. };
  16538. this.hls.trigger(Events.BUFFER_APPENDING, segment);
  16539. }
  16540. // trigger handler right now
  16541. this.tickImmediate();
  16542. };
  16543. _proto.loadFragment = function loadFragment(frag, track, targetBufferTime) {
  16544. // only load if fragment is not loaded or if in audio switch
  16545. var fragState = this.fragmentTracker.getState(frag);
  16546. this.fragCurrent = frag;
  16547. // we force a frag loading in audio switch as fragment tracker might not have evicted previous frags in case of quick audio switch
  16548. if (this.switchingTrack || fragState === FragmentState.NOT_LOADED || fragState === FragmentState.PARTIAL) {
  16549. var _track$details2;
  16550. if (frag.sn === 'initSegment') {
  16551. this._loadInitSegment(frag, track);
  16552. } else if ((_track$details2 = track.details) != null && _track$details2.live && !this.initPTS[frag.cc]) {
  16553. this.log("Waiting for video PTS in continuity counter " + frag.cc + " of live stream before loading audio fragment " + frag.sn + " of level " + this.trackId);
  16554. this.state = State.WAITING_INIT_PTS;
  16555. var mainDetails = this.mainDetails;
  16556. if (mainDetails && mainDetails.fragments[0].start !== track.details.fragments[0].start) {
  16557. alignMediaPlaylistByPDT(track.details, mainDetails);
  16558. }
  16559. } else {
  16560. this.startFragRequested = true;
  16561. _BaseStreamController.prototype.loadFragment.call(this, frag, track, targetBufferTime);
  16562. }
  16563. } else {
  16564. this.clearTrackerIfNeeded(frag);
  16565. }
  16566. };
  16567. _proto.flushAudioIfNeeded = function flushAudioIfNeeded(switchingTrack) {
  16568. var media = this.media,
  16569. bufferedTrack = this.bufferedTrack;
  16570. var bufferedAttributes = bufferedTrack == null ? void 0 : bufferedTrack.attrs;
  16571. var switchAttributes = switchingTrack.attrs;
  16572. if (media && bufferedAttributes && (bufferedAttributes.CHANNELS !== switchAttributes.CHANNELS || bufferedTrack.name !== switchingTrack.name || bufferedTrack.lang !== switchingTrack.lang)) {
  16573. this.log('Switching audio track : flushing all audio');
  16574. _BaseStreamController.prototype.flushMainBuffer.call(this, 0, Number.POSITIVE_INFINITY, 'audio');
  16575. this.bufferedTrack = null;
  16576. }
  16577. };
  16578. _proto.completeAudioSwitch = function completeAudioSwitch(switchingTrack) {
  16579. var hls = this.hls;
  16580. this.flushAudioIfNeeded(switchingTrack);
  16581. this.bufferedTrack = switchingTrack;
  16582. this.switchingTrack = null;
  16583. hls.trigger(Events.AUDIO_TRACK_SWITCHED, _objectSpread2({}, switchingTrack));
  16584. };
  16585. return AudioStreamController;
  16586. }(BaseStreamController);
  16587. var AudioTrackController = /*#__PURE__*/function (_BasePlaylistControll) {
  16588. _inheritsLoose(AudioTrackController, _BasePlaylistControll);
  16589. function AudioTrackController(hls) {
  16590. var _this;
  16591. _this = _BasePlaylistControll.call(this, hls, '[audio-track-controller]') || this;
  16592. _this.tracks = [];
  16593. _this.groupIds = null;
  16594. _this.tracksInGroup = [];
  16595. _this.trackId = -1;
  16596. _this.currentTrack = null;
  16597. _this.selectDefaultTrack = true;
  16598. _this.registerListeners();
  16599. return _this;
  16600. }
  16601. var _proto = AudioTrackController.prototype;
  16602. _proto.registerListeners = function registerListeners() {
  16603. var hls = this.hls;
  16604. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  16605. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  16606. hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
  16607. hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  16608. hls.on(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
  16609. hls.on(Events.ERROR, this.onError, this);
  16610. };
  16611. _proto.unregisterListeners = function unregisterListeners() {
  16612. var hls = this.hls;
  16613. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  16614. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  16615. hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
  16616. hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  16617. hls.off(Events.AUDIO_TRACK_LOADED, this.onAudioTrackLoaded, this);
  16618. hls.off(Events.ERROR, this.onError, this);
  16619. };
  16620. _proto.destroy = function destroy() {
  16621. this.unregisterListeners();
  16622. this.tracks.length = 0;
  16623. this.tracksInGroup.length = 0;
  16624. this.currentTrack = null;
  16625. _BasePlaylistControll.prototype.destroy.call(this);
  16626. };
  16627. _proto.onManifestLoading = function onManifestLoading() {
  16628. this.tracks = [];
  16629. this.tracksInGroup = [];
  16630. this.groupIds = null;
  16631. this.currentTrack = null;
  16632. this.trackId = -1;
  16633. this.selectDefaultTrack = true;
  16634. };
  16635. _proto.onManifestParsed = function onManifestParsed(event, data) {
  16636. this.tracks = data.audioTracks || [];
  16637. };
  16638. _proto.onAudioTrackLoaded = function onAudioTrackLoaded(event, data) {
  16639. var id = data.id,
  16640. groupId = data.groupId,
  16641. details = data.details;
  16642. var trackInActiveGroup = this.tracksInGroup[id];
  16643. if (!trackInActiveGroup || trackInActiveGroup.groupId !== groupId) {
  16644. this.warn("Audio track with id:" + id + " and group:" + groupId + " not found in active group " + (trackInActiveGroup == null ? void 0 : trackInActiveGroup.groupId));
  16645. return;
  16646. }
  16647. var curDetails = trackInActiveGroup.details;
  16648. trackInActiveGroup.details = data.details;
  16649. this.log("Audio track " + id + " \"" + trackInActiveGroup.name + "\" lang:" + trackInActiveGroup.lang + " group:" + groupId + " loaded [" + details.startSN + "-" + details.endSN + "]");
  16650. if (id === this.trackId) {
  16651. this.playlistLoaded(id, data, curDetails);
  16652. }
  16653. };
  16654. _proto.onLevelLoading = function onLevelLoading(event, data) {
  16655. this.switchLevel(data.level);
  16656. };
  16657. _proto.onLevelSwitching = function onLevelSwitching(event, data) {
  16658. this.switchLevel(data.level);
  16659. };
  16660. _proto.switchLevel = function switchLevel(levelIndex) {
  16661. var levelInfo = this.hls.levels[levelIndex];
  16662. if (!levelInfo) {
  16663. return;
  16664. }
  16665. var audioGroups = levelInfo.audioGroups || null;
  16666. var currentGroups = this.groupIds;
  16667. var currentTrack = this.currentTrack;
  16668. if (!audioGroups || (currentGroups == null ? void 0 : currentGroups.length) !== (audioGroups == null ? void 0 : audioGroups.length) || audioGroups != null && audioGroups.some(function (groupId) {
  16669. return (currentGroups == null ? void 0 : currentGroups.indexOf(groupId)) === -1;
  16670. })) {
  16671. this.groupIds = audioGroups;
  16672. this.trackId = -1;
  16673. this.currentTrack = null;
  16674. var audioTracks = this.tracks.filter(function (track) {
  16675. return !audioGroups || audioGroups.indexOf(track.groupId) !== -1;
  16676. });
  16677. if (audioTracks.length) {
  16678. // Disable selectDefaultTrack if there are no default tracks
  16679. if (this.selectDefaultTrack && !audioTracks.some(function (track) {
  16680. return track.default;
  16681. })) {
  16682. this.selectDefaultTrack = false;
  16683. }
  16684. // track.id should match hls.audioTracks index
  16685. audioTracks.forEach(function (track, i) {
  16686. track.id = i;
  16687. });
  16688. } else if (!currentTrack && !this.tracksInGroup.length) {
  16689. // Do not dispatch AUDIO_TRACKS_UPDATED when there were and are no tracks
  16690. return;
  16691. }
  16692. this.tracksInGroup = audioTracks;
  16693. // Find preferred track
  16694. var audioPreference = this.hls.config.audioPreference;
  16695. if (!currentTrack && audioPreference) {
  16696. var groupIndex = findMatchingOption(audioPreference, audioTracks, audioMatchPredicate);
  16697. if (groupIndex > -1) {
  16698. currentTrack = audioTracks[groupIndex];
  16699. } else {
  16700. var allIndex = findMatchingOption(audioPreference, this.tracks);
  16701. currentTrack = this.tracks[allIndex];
  16702. }
  16703. }
  16704. // Select initial track
  16705. var trackId = this.findTrackId(currentTrack);
  16706. if (trackId === -1 && currentTrack) {
  16707. trackId = this.findTrackId(null);
  16708. }
  16709. // Dispatch events and load track if needed
  16710. var audioTracksUpdated = {
  16711. audioTracks: audioTracks
  16712. };
  16713. this.log("Updating audio tracks, " + audioTracks.length + " track(s) found in group(s): " + (audioGroups == null ? void 0 : audioGroups.join(',')));
  16714. this.hls.trigger(Events.AUDIO_TRACKS_UPDATED, audioTracksUpdated);
  16715. var selectedTrackId = this.trackId;
  16716. if (trackId !== -1 && selectedTrackId === -1) {
  16717. this.setAudioTrack(trackId);
  16718. } else if (audioTracks.length && selectedTrackId === -1) {
  16719. var _this$groupIds;
  16720. var error = new Error("No audio track selected for current audio group-ID(s): " + ((_this$groupIds = this.groupIds) == null ? void 0 : _this$groupIds.join(',')) + " track count: " + audioTracks.length);
  16721. this.warn(error.message);
  16722. this.hls.trigger(Events.ERROR, {
  16723. type: ErrorTypes.MEDIA_ERROR,
  16724. details: ErrorDetails.AUDIO_TRACK_LOAD_ERROR,
  16725. fatal: true,
  16726. error: error
  16727. });
  16728. }
  16729. } else if (this.shouldReloadPlaylist(currentTrack)) {
  16730. // Retry playlist loading if no playlist is or has been loaded yet
  16731. this.setAudioTrack(this.trackId);
  16732. }
  16733. };
  16734. _proto.onError = function onError(event, data) {
  16735. if (data.fatal || !data.context) {
  16736. return;
  16737. }
  16738. if (data.context.type === PlaylistContextType.AUDIO_TRACK && data.context.id === this.trackId && (!this.groupIds || this.groupIds.indexOf(data.context.groupId) !== -1)) {
  16739. this.requestScheduled = -1;
  16740. this.checkRetry(data);
  16741. }
  16742. };
  16743. _proto.setAudioOption = function setAudioOption(audioOption) {
  16744. var hls = this.hls;
  16745. hls.config.audioPreference = audioOption;
  16746. if (audioOption) {
  16747. var allAudioTracks = this.allAudioTracks;
  16748. this.selectDefaultTrack = false;
  16749. if (allAudioTracks.length) {
  16750. // First see if current option matches (no switch op)
  16751. var currentTrack = this.currentTrack;
  16752. if (currentTrack && matchesOption(audioOption, currentTrack, audioMatchPredicate)) {
  16753. return currentTrack;
  16754. }
  16755. // Find option in available tracks (tracksInGroup)
  16756. var groupIndex = findMatchingOption(audioOption, this.tracksInGroup, audioMatchPredicate);
  16757. if (groupIndex > -1) {
  16758. var track = this.tracksInGroup[groupIndex];
  16759. this.setAudioTrack(groupIndex);
  16760. return track;
  16761. } else if (currentTrack) {
  16762. // Find option in nearest level audio group
  16763. var searchIndex = hls.loadLevel;
  16764. if (searchIndex === -1) {
  16765. searchIndex = hls.firstAutoLevel;
  16766. }
  16767. var switchIndex = findClosestLevelWithAudioGroup(audioOption, hls.levels, allAudioTracks, searchIndex, audioMatchPredicate);
  16768. if (switchIndex === -1) {
  16769. // could not find matching variant
  16770. return null;
  16771. }
  16772. // and switch level to acheive the audio group switch
  16773. hls.nextLoadLevel = switchIndex;
  16774. }
  16775. if (audioOption.channels || audioOption.audioCodec) {
  16776. // Could not find a match with codec / channels predicate
  16777. // Find a match without channels or codec
  16778. var withoutCodecAndChannelsMatch = findMatchingOption(audioOption, allAudioTracks);
  16779. if (withoutCodecAndChannelsMatch > -1) {
  16780. return allAudioTracks[withoutCodecAndChannelsMatch];
  16781. }
  16782. }
  16783. }
  16784. }
  16785. return null;
  16786. };
  16787. _proto.setAudioTrack = function setAudioTrack(newId) {
  16788. var tracks = this.tracksInGroup;
  16789. // check if level idx is valid
  16790. if (newId < 0 || newId >= tracks.length) {
  16791. this.warn("Invalid audio track id: " + newId);
  16792. return;
  16793. }
  16794. // stopping live reloading timer if any
  16795. this.clearTimer();
  16796. this.selectDefaultTrack = false;
  16797. var lastTrack = this.currentTrack;
  16798. var track = tracks[newId];
  16799. var trackLoaded = track.details && !track.details.live;
  16800. if (newId === this.trackId && track === lastTrack && trackLoaded) {
  16801. return;
  16802. }
  16803. this.log("Switching to audio-track " + newId + " \"" + track.name + "\" lang:" + track.lang + " group:" + track.groupId + " channels:" + track.channels);
  16804. this.trackId = newId;
  16805. this.currentTrack = track;
  16806. this.hls.trigger(Events.AUDIO_TRACK_SWITCHING, _objectSpread2({}, track));
  16807. // Do not reload track unless live
  16808. if (trackLoaded) {
  16809. return;
  16810. }
  16811. var hlsUrlParameters = this.switchParams(track.url, lastTrack == null ? void 0 : lastTrack.details, track.details);
  16812. this.loadPlaylist(hlsUrlParameters);
  16813. };
  16814. _proto.findTrackId = function findTrackId(currentTrack) {
  16815. var audioTracks = this.tracksInGroup;
  16816. for (var i = 0; i < audioTracks.length; i++) {
  16817. var track = audioTracks[i];
  16818. if (this.selectDefaultTrack && !track.default) {
  16819. continue;
  16820. }
  16821. if (!currentTrack || matchesOption(currentTrack, track, audioMatchPredicate)) {
  16822. return i;
  16823. }
  16824. }
  16825. if (currentTrack) {
  16826. var name = currentTrack.name,
  16827. lang = currentTrack.lang,
  16828. assocLang = currentTrack.assocLang,
  16829. characteristics = currentTrack.characteristics,
  16830. audioCodec = currentTrack.audioCodec,
  16831. channels = currentTrack.channels;
  16832. for (var _i = 0; _i < audioTracks.length; _i++) {
  16833. var _track = audioTracks[_i];
  16834. if (matchesOption({
  16835. name: name,
  16836. lang: lang,
  16837. assocLang: assocLang,
  16838. characteristics: characteristics,
  16839. audioCodec: audioCodec,
  16840. channels: channels
  16841. }, _track, audioMatchPredicate)) {
  16842. return _i;
  16843. }
  16844. }
  16845. for (var _i2 = 0; _i2 < audioTracks.length; _i2++) {
  16846. var _track2 = audioTracks[_i2];
  16847. if (mediaAttributesIdentical(currentTrack.attrs, _track2.attrs, ['LANGUAGE', 'ASSOC-LANGUAGE', 'CHARACTERISTICS'])) {
  16848. return _i2;
  16849. }
  16850. }
  16851. for (var _i3 = 0; _i3 < audioTracks.length; _i3++) {
  16852. var _track3 = audioTracks[_i3];
  16853. if (mediaAttributesIdentical(currentTrack.attrs, _track3.attrs, ['LANGUAGE'])) {
  16854. return _i3;
  16855. }
  16856. }
  16857. }
  16858. return -1;
  16859. };
  16860. _proto.loadPlaylist = function loadPlaylist(hlsUrlParameters) {
  16861. var audioTrack = this.currentTrack;
  16862. if (this.shouldLoadPlaylist(audioTrack) && audioTrack) {
  16863. _BasePlaylistControll.prototype.loadPlaylist.call(this);
  16864. var id = audioTrack.id;
  16865. var groupId = audioTrack.groupId;
  16866. var url = audioTrack.url;
  16867. if (hlsUrlParameters) {
  16868. try {
  16869. url = hlsUrlParameters.addDirectives(url);
  16870. } catch (error) {
  16871. this.warn("Could not construct new URL with HLS Delivery Directives: " + error);
  16872. }
  16873. }
  16874. // track not retrieved yet, or live playlist we need to (re)load it
  16875. this.log("loading audio-track playlist " + id + " \"" + audioTrack.name + "\" lang:" + audioTrack.lang + " group:" + groupId);
  16876. this.clearTimer();
  16877. this.hls.trigger(Events.AUDIO_TRACK_LOADING, {
  16878. url: url,
  16879. id: id,
  16880. groupId: groupId,
  16881. deliveryDirectives: hlsUrlParameters || null
  16882. });
  16883. }
  16884. };
  16885. _createClass(AudioTrackController, [{
  16886. key: "allAudioTracks",
  16887. get: function get() {
  16888. return this.tracks;
  16889. }
  16890. }, {
  16891. key: "audioTracks",
  16892. get: function get() {
  16893. return this.tracksInGroup;
  16894. }
  16895. }, {
  16896. key: "audioTrack",
  16897. get: function get() {
  16898. return this.trackId;
  16899. },
  16900. set: function set(newId) {
  16901. // If audio track is selected from API then don't choose from the manifest default track
  16902. this.selectDefaultTrack = false;
  16903. this.setAudioTrack(newId);
  16904. }
  16905. }]);
  16906. return AudioTrackController;
  16907. }(BasePlaylistController);
  16908. var TICK_INTERVAL$1 = 500; // how often to tick in ms
  16909. var SubtitleStreamController = /*#__PURE__*/function (_BaseStreamController) {
  16910. _inheritsLoose(SubtitleStreamController, _BaseStreamController);
  16911. function SubtitleStreamController(hls, fragmentTracker, keyLoader) {
  16912. var _this;
  16913. _this = _BaseStreamController.call(this, hls, fragmentTracker, keyLoader, '[subtitle-stream-controller]', PlaylistLevelType.SUBTITLE) || this;
  16914. _this.currentTrackId = -1;
  16915. _this.tracksBuffered = [];
  16916. _this.mainDetails = null;
  16917. _this._registerListeners();
  16918. return _this;
  16919. }
  16920. var _proto = SubtitleStreamController.prototype;
  16921. _proto.onHandlerDestroying = function onHandlerDestroying() {
  16922. this._unregisterListeners();
  16923. _BaseStreamController.prototype.onHandlerDestroying.call(this);
  16924. this.mainDetails = null;
  16925. };
  16926. _proto._registerListeners = function _registerListeners() {
  16927. var hls = this.hls;
  16928. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  16929. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  16930. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  16931. hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  16932. hls.on(Events.ERROR, this.onError, this);
  16933. hls.on(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
  16934. hls.on(Events.SUBTITLE_TRACK_SWITCH, this.onSubtitleTrackSwitch, this);
  16935. hls.on(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
  16936. hls.on(Events.SUBTITLE_FRAG_PROCESSED, this.onSubtitleFragProcessed, this);
  16937. hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  16938. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  16939. };
  16940. _proto._unregisterListeners = function _unregisterListeners() {
  16941. var hls = this.hls;
  16942. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  16943. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  16944. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  16945. hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  16946. hls.off(Events.ERROR, this.onError, this);
  16947. hls.off(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
  16948. hls.off(Events.SUBTITLE_TRACK_SWITCH, this.onSubtitleTrackSwitch, this);
  16949. hls.off(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
  16950. hls.off(Events.SUBTITLE_FRAG_PROCESSED, this.onSubtitleFragProcessed, this);
  16951. hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  16952. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  16953. };
  16954. _proto.startLoad = function startLoad(startPosition) {
  16955. this.stopLoad();
  16956. this.state = State.IDLE;
  16957. this.setInterval(TICK_INTERVAL$1);
  16958. this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
  16959. this.tick();
  16960. };
  16961. _proto.onManifestLoading = function onManifestLoading() {
  16962. this.mainDetails = null;
  16963. this.fragmentTracker.removeAllFragments();
  16964. };
  16965. _proto.onMediaDetaching = function onMediaDetaching() {
  16966. this.tracksBuffered = [];
  16967. _BaseStreamController.prototype.onMediaDetaching.call(this);
  16968. };
  16969. _proto.onLevelLoaded = function onLevelLoaded(event, data) {
  16970. this.mainDetails = data.details;
  16971. };
  16972. _proto.onSubtitleFragProcessed = function onSubtitleFragProcessed(event, data) {
  16973. var frag = data.frag,
  16974. success = data.success;
  16975. this.fragPrevious = frag;
  16976. this.state = State.IDLE;
  16977. if (!success) {
  16978. return;
  16979. }
  16980. var buffered = this.tracksBuffered[this.currentTrackId];
  16981. if (!buffered) {
  16982. return;
  16983. }
  16984. // Create/update a buffered array matching the interface used by BufferHelper.bufferedInfo
  16985. // so we can re-use the logic used to detect how much has been buffered
  16986. var timeRange;
  16987. var fragStart = frag.start;
  16988. for (var i = 0; i < buffered.length; i++) {
  16989. if (fragStart >= buffered[i].start && fragStart <= buffered[i].end) {
  16990. timeRange = buffered[i];
  16991. break;
  16992. }
  16993. }
  16994. var fragEnd = frag.start + frag.duration;
  16995. if (timeRange) {
  16996. timeRange.end = fragEnd;
  16997. } else {
  16998. timeRange = {
  16999. start: fragStart,
  17000. end: fragEnd
  17001. };
  17002. buffered.push(timeRange);
  17003. }
  17004. this.fragmentTracker.fragBuffered(frag);
  17005. this.fragBufferedComplete(frag, null);
  17006. };
  17007. _proto.onBufferFlushing = function onBufferFlushing(event, data) {
  17008. var startOffset = data.startOffset,
  17009. endOffset = data.endOffset;
  17010. if (startOffset === 0 && endOffset !== Number.POSITIVE_INFINITY) {
  17011. var endOffsetSubtitles = endOffset - 1;
  17012. if (endOffsetSubtitles <= 0) {
  17013. return;
  17014. }
  17015. data.endOffsetSubtitles = Math.max(0, endOffsetSubtitles);
  17016. this.tracksBuffered.forEach(function (buffered) {
  17017. for (var i = 0; i < buffered.length;) {
  17018. if (buffered[i].end <= endOffsetSubtitles) {
  17019. buffered.shift();
  17020. continue;
  17021. } else if (buffered[i].start < endOffsetSubtitles) {
  17022. buffered[i].start = endOffsetSubtitles;
  17023. } else {
  17024. break;
  17025. }
  17026. i++;
  17027. }
  17028. });
  17029. this.fragmentTracker.removeFragmentsInRange(startOffset, endOffsetSubtitles, PlaylistLevelType.SUBTITLE);
  17030. }
  17031. };
  17032. _proto.onFragBuffered = function onFragBuffered(event, data) {
  17033. if (!this.loadedmetadata && data.frag.type === PlaylistLevelType.MAIN) {
  17034. var _this$media;
  17035. if ((_this$media = this.media) != null && _this$media.buffered.length) {
  17036. this.loadedmetadata = true;
  17037. }
  17038. }
  17039. }
  17040. // If something goes wrong, proceed to next frag, if we were processing one.
  17041. ;
  17042. _proto.onError = function onError(event, data) {
  17043. var frag = data.frag;
  17044. if ((frag == null ? void 0 : frag.type) === PlaylistLevelType.SUBTITLE) {
  17045. if (data.details === ErrorDetails.FRAG_GAP) {
  17046. this.fragmentTracker.fragBuffered(frag, true);
  17047. }
  17048. if (this.fragCurrent) {
  17049. this.fragCurrent.abortRequests();
  17050. }
  17051. if (this.state !== State.STOPPED) {
  17052. this.state = State.IDLE;
  17053. }
  17054. }
  17055. }
  17056. // Got all new subtitle levels.
  17057. ;
  17058. _proto.onSubtitleTracksUpdated = function onSubtitleTracksUpdated(event, _ref) {
  17059. var _this2 = this;
  17060. var subtitleTracks = _ref.subtitleTracks;
  17061. if (this.levels && subtitleOptionsIdentical(this.levels, subtitleTracks)) {
  17062. this.levels = subtitleTracks.map(function (mediaPlaylist) {
  17063. return new Level(mediaPlaylist);
  17064. });
  17065. return;
  17066. }
  17067. this.tracksBuffered = [];
  17068. this.levels = subtitleTracks.map(function (mediaPlaylist) {
  17069. var level = new Level(mediaPlaylist);
  17070. _this2.tracksBuffered[level.id] = [];
  17071. return level;
  17072. });
  17073. this.fragmentTracker.removeFragmentsInRange(0, Number.POSITIVE_INFINITY, PlaylistLevelType.SUBTITLE);
  17074. this.fragPrevious = null;
  17075. this.mediaBuffer = null;
  17076. };
  17077. _proto.onSubtitleTrackSwitch = function onSubtitleTrackSwitch(event, data) {
  17078. var _this$levels;
  17079. this.currentTrackId = data.id;
  17080. if (!((_this$levels = this.levels) != null && _this$levels.length) || this.currentTrackId === -1) {
  17081. this.clearInterval();
  17082. return;
  17083. }
  17084. // Check if track has the necessary details to load fragments
  17085. var currentTrack = this.levels[this.currentTrackId];
  17086. if (currentTrack != null && currentTrack.details) {
  17087. this.mediaBuffer = this.mediaBufferTimeRanges;
  17088. } else {
  17089. this.mediaBuffer = null;
  17090. }
  17091. if (currentTrack) {
  17092. this.setInterval(TICK_INTERVAL$1);
  17093. }
  17094. }
  17095. // Got a new set of subtitle fragments.
  17096. ;
  17097. _proto.onSubtitleTrackLoaded = function onSubtitleTrackLoaded(event, data) {
  17098. var _track$details;
  17099. var currentTrackId = this.currentTrackId,
  17100. levels = this.levels;
  17101. var newDetails = data.details,
  17102. trackId = data.id;
  17103. if (!levels) {
  17104. this.warn("Subtitle tracks were reset while loading level " + trackId);
  17105. return;
  17106. }
  17107. var track = levels[trackId];
  17108. if (trackId >= levels.length || !track) {
  17109. return;
  17110. }
  17111. this.log("Subtitle track " + trackId + " loaded [" + newDetails.startSN + "," + newDetails.endSN + "]" + (newDetails.lastPartSn ? "[part-" + newDetails.lastPartSn + "-" + newDetails.lastPartIndex + "]" : '') + ",duration:" + newDetails.totalduration);
  17112. this.mediaBuffer = this.mediaBufferTimeRanges;
  17113. var sliding = 0;
  17114. if (newDetails.live || (_track$details = track.details) != null && _track$details.live) {
  17115. var mainDetails = this.mainDetails;
  17116. if (newDetails.deltaUpdateFailed || !mainDetails) {
  17117. return;
  17118. }
  17119. var mainSlidingStartFragment = mainDetails.fragments[0];
  17120. if (!track.details) {
  17121. if (newDetails.hasProgramDateTime && mainDetails.hasProgramDateTime) {
  17122. alignMediaPlaylistByPDT(newDetails, mainDetails);
  17123. sliding = newDetails.fragments[0].start;
  17124. } else if (mainSlidingStartFragment) {
  17125. // line up live playlist with main so that fragments in range are loaded
  17126. sliding = mainSlidingStartFragment.start;
  17127. addSliding(newDetails, sliding);
  17128. }
  17129. } else {
  17130. var _this$levelLastLoaded;
  17131. sliding = this.alignPlaylists(newDetails, track.details, (_this$levelLastLoaded = this.levelLastLoaded) == null ? void 0 : _this$levelLastLoaded.details);
  17132. if (sliding === 0 && mainSlidingStartFragment) {
  17133. // realign with main when there is no overlap with last refresh
  17134. sliding = mainSlidingStartFragment.start;
  17135. addSliding(newDetails, sliding);
  17136. }
  17137. }
  17138. }
  17139. track.details = newDetails;
  17140. this.levelLastLoaded = track;
  17141. if (trackId !== currentTrackId) {
  17142. return;
  17143. }
  17144. if (!this.startFragRequested && (this.mainDetails || !newDetails.live)) {
  17145. this.setStartPosition(this.mainDetails || newDetails, sliding);
  17146. }
  17147. // trigger handler right now
  17148. this.tick();
  17149. // If playlist is misaligned because of bad PDT or drift, delete details to resync with main on reload
  17150. if (newDetails.live && !this.fragCurrent && this.media && this.state === State.IDLE) {
  17151. var foundFrag = findFragmentByPTS(null, newDetails.fragments, this.media.currentTime, 0);
  17152. if (!foundFrag) {
  17153. this.warn('Subtitle playlist not aligned with playback');
  17154. track.details = undefined;
  17155. }
  17156. }
  17157. };
  17158. _proto._handleFragmentLoadComplete = function _handleFragmentLoadComplete(fragLoadedData) {
  17159. var _this3 = this;
  17160. var frag = fragLoadedData.frag,
  17161. payload = fragLoadedData.payload;
  17162. var decryptData = frag.decryptdata;
  17163. var hls = this.hls;
  17164. if (this.fragContextChanged(frag)) {
  17165. return;
  17166. }
  17167. // check to see if the payload needs to be decrypted
  17168. if (payload && payload.byteLength > 0 && decryptData != null && decryptData.key && decryptData.iv && decryptData.method === 'AES-128') {
  17169. var startTime = performance.now();
  17170. // decrypt the subtitles
  17171. this.decrypter.decrypt(new Uint8Array(payload), decryptData.key.buffer, decryptData.iv.buffer).catch(function (err) {
  17172. hls.trigger(Events.ERROR, {
  17173. type: ErrorTypes.MEDIA_ERROR,
  17174. details: ErrorDetails.FRAG_DECRYPT_ERROR,
  17175. fatal: false,
  17176. error: err,
  17177. reason: err.message,
  17178. frag: frag
  17179. });
  17180. throw err;
  17181. }).then(function (decryptedData) {
  17182. var endTime = performance.now();
  17183. hls.trigger(Events.FRAG_DECRYPTED, {
  17184. frag: frag,
  17185. payload: decryptedData,
  17186. stats: {
  17187. tstart: startTime,
  17188. tdecrypt: endTime
  17189. }
  17190. });
  17191. }).catch(function (err) {
  17192. _this3.warn(err.name + ": " + err.message);
  17193. _this3.state = State.IDLE;
  17194. });
  17195. }
  17196. };
  17197. _proto.doTick = function doTick() {
  17198. if (!this.media) {
  17199. this.state = State.IDLE;
  17200. return;
  17201. }
  17202. if (this.state === State.IDLE) {
  17203. var currentTrackId = this.currentTrackId,
  17204. levels = this.levels;
  17205. var track = levels == null ? void 0 : levels[currentTrackId];
  17206. if (!track || !levels.length || !track.details) {
  17207. return;
  17208. }
  17209. var config = this.config;
  17210. var currentTime = this.getLoadPosition();
  17211. var bufferedInfo = BufferHelper.bufferedInfo(this.tracksBuffered[this.currentTrackId] || [], currentTime, config.maxBufferHole);
  17212. var targetBufferTime = bufferedInfo.end,
  17213. bufferLen = bufferedInfo.len;
  17214. var mainBufferInfo = this.getFwdBufferInfo(this.media, PlaylistLevelType.MAIN);
  17215. var trackDetails = track.details;
  17216. var maxBufLen = this.getMaxBufferLength(mainBufferInfo == null ? void 0 : mainBufferInfo.len) + trackDetails.levelTargetDuration;
  17217. if (bufferLen > maxBufLen) {
  17218. return;
  17219. }
  17220. var fragments = trackDetails.fragments;
  17221. var fragLen = fragments.length;
  17222. var end = trackDetails.edge;
  17223. var foundFrag = null;
  17224. var fragPrevious = this.fragPrevious;
  17225. if (targetBufferTime < end) {
  17226. var tolerance = config.maxFragLookUpTolerance;
  17227. var lookupTolerance = targetBufferTime > end - tolerance ? 0 : tolerance;
  17228. foundFrag = findFragmentByPTS(fragPrevious, fragments, Math.max(fragments[0].start, targetBufferTime), lookupTolerance);
  17229. if (!foundFrag && fragPrevious && fragPrevious.start < fragments[0].start) {
  17230. foundFrag = fragments[0];
  17231. }
  17232. } else {
  17233. foundFrag = fragments[fragLen - 1];
  17234. }
  17235. if (!foundFrag) {
  17236. return;
  17237. }
  17238. foundFrag = this.mapToInitFragWhenRequired(foundFrag);
  17239. if (foundFrag.sn !== 'initSegment') {
  17240. // Load earlier fragment in same discontinuity to make up for misaligned playlists and cues that extend beyond end of segment
  17241. var curSNIdx = foundFrag.sn - trackDetails.startSN;
  17242. var prevFrag = fragments[curSNIdx - 1];
  17243. if (prevFrag && prevFrag.cc === foundFrag.cc && this.fragmentTracker.getState(prevFrag) === FragmentState.NOT_LOADED) {
  17244. foundFrag = prevFrag;
  17245. }
  17246. }
  17247. if (this.fragmentTracker.getState(foundFrag) === FragmentState.NOT_LOADED) {
  17248. // only load if fragment is not loaded
  17249. this.loadFragment(foundFrag, track, targetBufferTime);
  17250. }
  17251. }
  17252. };
  17253. _proto.getMaxBufferLength = function getMaxBufferLength(mainBufferLength) {
  17254. var maxConfigBuffer = _BaseStreamController.prototype.getMaxBufferLength.call(this);
  17255. if (!mainBufferLength) {
  17256. return maxConfigBuffer;
  17257. }
  17258. return Math.max(maxConfigBuffer, mainBufferLength);
  17259. };
  17260. _proto.loadFragment = function loadFragment(frag, level, targetBufferTime) {
  17261. this.fragCurrent = frag;
  17262. if (frag.sn === 'initSegment') {
  17263. this._loadInitSegment(frag, level);
  17264. } else {
  17265. this.startFragRequested = true;
  17266. _BaseStreamController.prototype.loadFragment.call(this, frag, level, targetBufferTime);
  17267. }
  17268. };
  17269. _createClass(SubtitleStreamController, [{
  17270. key: "mediaBufferTimeRanges",
  17271. get: function get() {
  17272. return new BufferableInstance(this.tracksBuffered[this.currentTrackId] || []);
  17273. }
  17274. }]);
  17275. return SubtitleStreamController;
  17276. }(BaseStreamController);
  17277. var BufferableInstance = function BufferableInstance(timeranges) {
  17278. this.buffered = void 0;
  17279. var getRange = function getRange(name, index, length) {
  17280. index = index >>> 0;
  17281. if (index > length - 1) {
  17282. throw new DOMException("Failed to execute '" + name + "' on 'TimeRanges': The index provided (" + index + ") is greater than the maximum bound (" + length + ")");
  17283. }
  17284. return timeranges[index][name];
  17285. };
  17286. this.buffered = {
  17287. get length() {
  17288. return timeranges.length;
  17289. },
  17290. end: function end(index) {
  17291. return getRange('end', index, timeranges.length);
  17292. },
  17293. start: function start(index) {
  17294. return getRange('start', index, timeranges.length);
  17295. }
  17296. };
  17297. };
  17298. var SubtitleTrackController = /*#__PURE__*/function (_BasePlaylistControll) {
  17299. _inheritsLoose(SubtitleTrackController, _BasePlaylistControll);
  17300. function SubtitleTrackController(hls) {
  17301. var _this;
  17302. _this = _BasePlaylistControll.call(this, hls, '[subtitle-track-controller]') || this;
  17303. _this.media = null;
  17304. _this.tracks = [];
  17305. _this.groupIds = null;
  17306. _this.tracksInGroup = [];
  17307. _this.trackId = -1;
  17308. _this.currentTrack = null;
  17309. _this.selectDefaultTrack = true;
  17310. _this.queuedDefaultTrack = -1;
  17311. _this.asyncPollTrackChange = function () {
  17312. return _this.pollTrackChange(0);
  17313. };
  17314. _this.useTextTrackPolling = false;
  17315. _this.subtitlePollingInterval = -1;
  17316. _this._subtitleDisplay = true;
  17317. _this.onTextTracksChanged = function () {
  17318. if (!_this.useTextTrackPolling) {
  17319. self.clearInterval(_this.subtitlePollingInterval);
  17320. }
  17321. // Media is undefined when switching streams via loadSource()
  17322. if (!_this.media || !_this.hls.config.renderTextTracksNatively) {
  17323. return;
  17324. }
  17325. var textTrack = null;
  17326. var tracks = filterSubtitleTracks(_this.media.textTracks);
  17327. for (var i = 0; i < tracks.length; i++) {
  17328. if (tracks[i].mode === 'hidden') {
  17329. // Do not break in case there is a following track with showing.
  17330. textTrack = tracks[i];
  17331. } else if (tracks[i].mode === 'showing') {
  17332. textTrack = tracks[i];
  17333. break;
  17334. }
  17335. }
  17336. // Find internal track index for TextTrack
  17337. var trackId = _this.findTrackForTextTrack(textTrack);
  17338. if (_this.subtitleTrack !== trackId) {
  17339. _this.setSubtitleTrack(trackId);
  17340. }
  17341. };
  17342. _this.registerListeners();
  17343. return _this;
  17344. }
  17345. var _proto = SubtitleTrackController.prototype;
  17346. _proto.destroy = function destroy() {
  17347. this.unregisterListeners();
  17348. this.tracks.length = 0;
  17349. this.tracksInGroup.length = 0;
  17350. this.currentTrack = null;
  17351. this.onTextTracksChanged = this.asyncPollTrackChange = null;
  17352. _BasePlaylistControll.prototype.destroy.call(this);
  17353. };
  17354. _proto.registerListeners = function registerListeners() {
  17355. var hls = this.hls;
  17356. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  17357. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  17358. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  17359. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  17360. hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
  17361. hls.on(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  17362. hls.on(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
  17363. hls.on(Events.ERROR, this.onError, this);
  17364. };
  17365. _proto.unregisterListeners = function unregisterListeners() {
  17366. var hls = this.hls;
  17367. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  17368. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  17369. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  17370. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  17371. hls.off(Events.LEVEL_LOADING, this.onLevelLoading, this);
  17372. hls.off(Events.LEVEL_SWITCHING, this.onLevelSwitching, this);
  17373. hls.off(Events.SUBTITLE_TRACK_LOADED, this.onSubtitleTrackLoaded, this);
  17374. hls.off(Events.ERROR, this.onError, this);
  17375. }
  17376. // Listen for subtitle track change, then extract the current track ID.
  17377. ;
  17378. _proto.onMediaAttached = function onMediaAttached(event, data) {
  17379. this.media = data.media;
  17380. if (!this.media) {
  17381. return;
  17382. }
  17383. if (this.queuedDefaultTrack > -1) {
  17384. this.subtitleTrack = this.queuedDefaultTrack;
  17385. this.queuedDefaultTrack = -1;
  17386. }
  17387. this.useTextTrackPolling = !(this.media.textTracks && 'onchange' in this.media.textTracks);
  17388. if (this.useTextTrackPolling) {
  17389. this.pollTrackChange(500);
  17390. } else {
  17391. this.media.textTracks.addEventListener('change', this.asyncPollTrackChange);
  17392. }
  17393. };
  17394. _proto.pollTrackChange = function pollTrackChange(timeout) {
  17395. self.clearInterval(this.subtitlePollingInterval);
  17396. this.subtitlePollingInterval = self.setInterval(this.onTextTracksChanged, timeout);
  17397. };
  17398. _proto.onMediaDetaching = function onMediaDetaching() {
  17399. if (!this.media) {
  17400. return;
  17401. }
  17402. self.clearInterval(this.subtitlePollingInterval);
  17403. if (!this.useTextTrackPolling) {
  17404. this.media.textTracks.removeEventListener('change', this.asyncPollTrackChange);
  17405. }
  17406. if (this.trackId > -1) {
  17407. this.queuedDefaultTrack = this.trackId;
  17408. }
  17409. var textTracks = filterSubtitleTracks(this.media.textTracks);
  17410. // Clear loaded cues on media detachment from tracks
  17411. textTracks.forEach(function (track) {
  17412. clearCurrentCues(track);
  17413. });
  17414. // Disable all subtitle tracks before detachment so when reattached only tracks in that content are enabled.
  17415. this.subtitleTrack = -1;
  17416. this.media = null;
  17417. };
  17418. _proto.onManifestLoading = function onManifestLoading() {
  17419. this.tracks = [];
  17420. this.groupIds = null;
  17421. this.tracksInGroup = [];
  17422. this.trackId = -1;
  17423. this.currentTrack = null;
  17424. this.selectDefaultTrack = true;
  17425. }
  17426. // Fired whenever a new manifest is loaded.
  17427. ;
  17428. _proto.onManifestParsed = function onManifestParsed(event, data) {
  17429. this.tracks = data.subtitleTracks;
  17430. };
  17431. _proto.onSubtitleTrackLoaded = function onSubtitleTrackLoaded(event, data) {
  17432. var id = data.id,
  17433. groupId = data.groupId,
  17434. details = data.details;
  17435. var trackInActiveGroup = this.tracksInGroup[id];
  17436. if (!trackInActiveGroup || trackInActiveGroup.groupId !== groupId) {
  17437. this.warn("Subtitle track with id:" + id + " and group:" + groupId + " not found in active group " + (trackInActiveGroup == null ? void 0 : trackInActiveGroup.groupId));
  17438. return;
  17439. }
  17440. var curDetails = trackInActiveGroup.details;
  17441. trackInActiveGroup.details = data.details;
  17442. this.log("Subtitle track " + id + " \"" + trackInActiveGroup.name + "\" lang:" + trackInActiveGroup.lang + " group:" + groupId + " loaded [" + details.startSN + "-" + details.endSN + "]");
  17443. if (id === this.trackId) {
  17444. this.playlistLoaded(id, data, curDetails);
  17445. }
  17446. };
  17447. _proto.onLevelLoading = function onLevelLoading(event, data) {
  17448. this.switchLevel(data.level);
  17449. };
  17450. _proto.onLevelSwitching = function onLevelSwitching(event, data) {
  17451. this.switchLevel(data.level);
  17452. };
  17453. _proto.switchLevel = function switchLevel(levelIndex) {
  17454. var levelInfo = this.hls.levels[levelIndex];
  17455. if (!levelInfo) {
  17456. return;
  17457. }
  17458. var subtitleGroups = levelInfo.subtitleGroups || null;
  17459. var currentGroups = this.groupIds;
  17460. var currentTrack = this.currentTrack;
  17461. if (!subtitleGroups || (currentGroups == null ? void 0 : currentGroups.length) !== (subtitleGroups == null ? void 0 : subtitleGroups.length) || subtitleGroups != null && subtitleGroups.some(function (groupId) {
  17462. return (currentGroups == null ? void 0 : currentGroups.indexOf(groupId)) === -1;
  17463. })) {
  17464. this.groupIds = subtitleGroups;
  17465. this.trackId = -1;
  17466. this.currentTrack = null;
  17467. var subtitleTracks = this.tracks.filter(function (track) {
  17468. return !subtitleGroups || subtitleGroups.indexOf(track.groupId) !== -1;
  17469. });
  17470. if (subtitleTracks.length) {
  17471. // Disable selectDefaultTrack if there are no default tracks
  17472. if (this.selectDefaultTrack && !subtitleTracks.some(function (track) {
  17473. return track.default;
  17474. })) {
  17475. this.selectDefaultTrack = false;
  17476. }
  17477. // track.id should match hls.audioTracks index
  17478. subtitleTracks.forEach(function (track, i) {
  17479. track.id = i;
  17480. });
  17481. } else if (!currentTrack && !this.tracksInGroup.length) {
  17482. // Do not dispatch SUBTITLE_TRACKS_UPDATED when there were and are no tracks
  17483. return;
  17484. }
  17485. this.tracksInGroup = subtitleTracks;
  17486. // Find preferred track
  17487. var subtitlePreference = this.hls.config.subtitlePreference;
  17488. if (!currentTrack && subtitlePreference) {
  17489. this.selectDefaultTrack = false;
  17490. var groupIndex = findMatchingOption(subtitlePreference, subtitleTracks);
  17491. if (groupIndex > -1) {
  17492. currentTrack = subtitleTracks[groupIndex];
  17493. } else {
  17494. var allIndex = findMatchingOption(subtitlePreference, this.tracks);
  17495. currentTrack = this.tracks[allIndex];
  17496. }
  17497. }
  17498. // Select initial track
  17499. var trackId = this.findTrackId(currentTrack);
  17500. if (trackId === -1 && currentTrack) {
  17501. trackId = this.findTrackId(null);
  17502. }
  17503. // Dispatch events and load track if needed
  17504. var subtitleTracksUpdated = {
  17505. subtitleTracks: subtitleTracks
  17506. };
  17507. this.log("Updating subtitle tracks, " + subtitleTracks.length + " track(s) found in \"" + (subtitleGroups == null ? void 0 : subtitleGroups.join(',')) + "\" group-id");
  17508. this.hls.trigger(Events.SUBTITLE_TRACKS_UPDATED, subtitleTracksUpdated);
  17509. if (trackId !== -1 && this.trackId === -1) {
  17510. this.setSubtitleTrack(trackId);
  17511. }
  17512. } else if (this.shouldReloadPlaylist(currentTrack)) {
  17513. // Retry playlist loading if no playlist is or has been loaded yet
  17514. this.setSubtitleTrack(this.trackId);
  17515. }
  17516. };
  17517. _proto.findTrackId = function findTrackId(currentTrack) {
  17518. var tracks = this.tracksInGroup;
  17519. var selectDefault = this.selectDefaultTrack;
  17520. for (var i = 0; i < tracks.length; i++) {
  17521. var track = tracks[i];
  17522. if (selectDefault && !track.default || !selectDefault && !currentTrack) {
  17523. continue;
  17524. }
  17525. if (!currentTrack || matchesOption(track, currentTrack)) {
  17526. return i;
  17527. }
  17528. }
  17529. if (currentTrack) {
  17530. for (var _i = 0; _i < tracks.length; _i++) {
  17531. var _track = tracks[_i];
  17532. if (mediaAttributesIdentical(currentTrack.attrs, _track.attrs, ['LANGUAGE', 'ASSOC-LANGUAGE', 'CHARACTERISTICS'])) {
  17533. return _i;
  17534. }
  17535. }
  17536. for (var _i2 = 0; _i2 < tracks.length; _i2++) {
  17537. var _track2 = tracks[_i2];
  17538. if (mediaAttributesIdentical(currentTrack.attrs, _track2.attrs, ['LANGUAGE'])) {
  17539. return _i2;
  17540. }
  17541. }
  17542. }
  17543. return -1;
  17544. };
  17545. _proto.findTrackForTextTrack = function findTrackForTextTrack(textTrack) {
  17546. if (textTrack) {
  17547. var tracks = this.tracksInGroup;
  17548. for (var i = 0; i < tracks.length; i++) {
  17549. var track = tracks[i];
  17550. if (subtitleTrackMatchesTextTrack(track, textTrack)) {
  17551. return i;
  17552. }
  17553. }
  17554. }
  17555. return -1;
  17556. };
  17557. _proto.onError = function onError(event, data) {
  17558. if (data.fatal || !data.context) {
  17559. return;
  17560. }
  17561. if (data.context.type === PlaylistContextType.SUBTITLE_TRACK && data.context.id === this.trackId && (!this.groupIds || this.groupIds.indexOf(data.context.groupId) !== -1)) {
  17562. this.checkRetry(data);
  17563. }
  17564. };
  17565. _proto.setSubtitleOption = function setSubtitleOption(subtitleOption) {
  17566. this.hls.config.subtitlePreference = subtitleOption;
  17567. if (subtitleOption) {
  17568. var allSubtitleTracks = this.allSubtitleTracks;
  17569. this.selectDefaultTrack = false;
  17570. if (allSubtitleTracks.length) {
  17571. // First see if current option matches (no switch op)
  17572. var currentTrack = this.currentTrack;
  17573. if (currentTrack && matchesOption(subtitleOption, currentTrack)) {
  17574. return currentTrack;
  17575. }
  17576. // Find option in current group
  17577. var groupIndex = findMatchingOption(subtitleOption, this.tracksInGroup);
  17578. if (groupIndex > -1) {
  17579. var track = this.tracksInGroup[groupIndex];
  17580. this.setSubtitleTrack(groupIndex);
  17581. return track;
  17582. } else if (currentTrack) {
  17583. // If this is not the initial selection return null
  17584. // option should have matched one in active group
  17585. return null;
  17586. } else {
  17587. // Find the option in all tracks for initial selection
  17588. var allIndex = findMatchingOption(subtitleOption, allSubtitleTracks);
  17589. if (allIndex > -1) {
  17590. return allSubtitleTracks[allIndex];
  17591. }
  17592. }
  17593. }
  17594. }
  17595. return null;
  17596. };
  17597. _proto.loadPlaylist = function loadPlaylist(hlsUrlParameters) {
  17598. _BasePlaylistControll.prototype.loadPlaylist.call(this);
  17599. var currentTrack = this.currentTrack;
  17600. if (this.shouldLoadPlaylist(currentTrack) && currentTrack) {
  17601. var id = currentTrack.id;
  17602. var groupId = currentTrack.groupId;
  17603. var url = currentTrack.url;
  17604. if (hlsUrlParameters) {
  17605. try {
  17606. url = hlsUrlParameters.addDirectives(url);
  17607. } catch (error) {
  17608. this.warn("Could not construct new URL with HLS Delivery Directives: " + error);
  17609. }
  17610. }
  17611. this.log("Loading subtitle playlist for id " + id);
  17612. this.hls.trigger(Events.SUBTITLE_TRACK_LOADING, {
  17613. url: url,
  17614. id: id,
  17615. groupId: groupId,
  17616. deliveryDirectives: hlsUrlParameters || null
  17617. });
  17618. }
  17619. }
  17620. /**
  17621. * Disables the old subtitleTrack and sets current mode on the next subtitleTrack.
  17622. * This operates on the DOM textTracks.
  17623. * A value of -1 will disable all subtitle tracks.
  17624. */;
  17625. _proto.toggleTrackModes = function toggleTrackModes() {
  17626. var media = this.media;
  17627. if (!media) {
  17628. return;
  17629. }
  17630. var textTracks = filterSubtitleTracks(media.textTracks);
  17631. var currentTrack = this.currentTrack;
  17632. var nextTrack;
  17633. if (currentTrack) {
  17634. nextTrack = textTracks.filter(function (textTrack) {
  17635. return subtitleTrackMatchesTextTrack(currentTrack, textTrack);
  17636. })[0];
  17637. if (!nextTrack) {
  17638. this.warn("Unable to find subtitle TextTrack with name \"" + currentTrack.name + "\" and language \"" + currentTrack.lang + "\"");
  17639. }
  17640. }
  17641. [].slice.call(textTracks).forEach(function (track) {
  17642. if (track.mode !== 'disabled' && track !== nextTrack) {
  17643. track.mode = 'disabled';
  17644. }
  17645. });
  17646. if (nextTrack) {
  17647. var mode = this.subtitleDisplay ? 'showing' : 'hidden';
  17648. if (nextTrack.mode !== mode) {
  17649. nextTrack.mode = mode;
  17650. }
  17651. }
  17652. }
  17653. /**
  17654. * This method is responsible for validating the subtitle index and periodically reloading if live.
  17655. * Dispatches the SUBTITLE_TRACK_SWITCH event, which instructs the subtitle-stream-controller to load the selected track.
  17656. */;
  17657. _proto.setSubtitleTrack = function setSubtitleTrack(newId) {
  17658. var tracks = this.tracksInGroup;
  17659. // setting this.subtitleTrack will trigger internal logic
  17660. // if media has not been attached yet, it will fail
  17661. // we keep a reference to the default track id
  17662. // and we'll set subtitleTrack when onMediaAttached is triggered
  17663. if (!this.media) {
  17664. this.queuedDefaultTrack = newId;
  17665. return;
  17666. }
  17667. // exit if track id as already set or invalid
  17668. if (newId < -1 || newId >= tracks.length || !isFiniteNumber(newId)) {
  17669. this.warn("Invalid subtitle track id: " + newId);
  17670. return;
  17671. }
  17672. // stopping live reloading timer if any
  17673. this.clearTimer();
  17674. this.selectDefaultTrack = false;
  17675. var lastTrack = this.currentTrack;
  17676. var track = tracks[newId] || null;
  17677. this.trackId = newId;
  17678. this.currentTrack = track;
  17679. this.toggleTrackModes();
  17680. if (!track) {
  17681. // switch to -1
  17682. this.hls.trigger(Events.SUBTITLE_TRACK_SWITCH, {
  17683. id: newId
  17684. });
  17685. return;
  17686. }
  17687. var trackLoaded = !!track.details && !track.details.live;
  17688. if (newId === this.trackId && track === lastTrack && trackLoaded) {
  17689. return;
  17690. }
  17691. this.log("Switching to subtitle-track " + newId + (track ? " \"" + track.name + "\" lang:" + track.lang + " group:" + track.groupId : ''));
  17692. var id = track.id,
  17693. _track$groupId = track.groupId,
  17694. groupId = _track$groupId === void 0 ? '' : _track$groupId,
  17695. name = track.name,
  17696. type = track.type,
  17697. url = track.url;
  17698. this.hls.trigger(Events.SUBTITLE_TRACK_SWITCH, {
  17699. id: id,
  17700. groupId: groupId,
  17701. name: name,
  17702. type: type,
  17703. url: url
  17704. });
  17705. var hlsUrlParameters = this.switchParams(track.url, lastTrack == null ? void 0 : lastTrack.details, track.details);
  17706. this.loadPlaylist(hlsUrlParameters);
  17707. };
  17708. _createClass(SubtitleTrackController, [{
  17709. key: "subtitleDisplay",
  17710. get: function get() {
  17711. return this._subtitleDisplay;
  17712. },
  17713. set: function set(value) {
  17714. this._subtitleDisplay = value;
  17715. if (this.trackId > -1) {
  17716. this.toggleTrackModes();
  17717. }
  17718. }
  17719. }, {
  17720. key: "allSubtitleTracks",
  17721. get: function get() {
  17722. return this.tracks;
  17723. }
  17724. /** get alternate subtitle tracks list from playlist **/
  17725. }, {
  17726. key: "subtitleTracks",
  17727. get: function get() {
  17728. return this.tracksInGroup;
  17729. }
  17730. /** get/set index of the selected subtitle track (based on index in subtitle track lists) **/
  17731. }, {
  17732. key: "subtitleTrack",
  17733. get: function get() {
  17734. return this.trackId;
  17735. },
  17736. set: function set(newId) {
  17737. this.selectDefaultTrack = false;
  17738. this.setSubtitleTrack(newId);
  17739. }
  17740. }]);
  17741. return SubtitleTrackController;
  17742. }(BasePlaylistController);
  17743. var BufferOperationQueue = /*#__PURE__*/function () {
  17744. function BufferOperationQueue(sourceBufferReference) {
  17745. this.buffers = void 0;
  17746. this.queues = {
  17747. video: [],
  17748. audio: [],
  17749. audiovideo: []
  17750. };
  17751. this.buffers = sourceBufferReference;
  17752. }
  17753. var _proto = BufferOperationQueue.prototype;
  17754. _proto.append = function append(operation, type, pending) {
  17755. var queue = this.queues[type];
  17756. queue.push(operation);
  17757. if (queue.length === 1 && !pending) {
  17758. this.executeNext(type);
  17759. }
  17760. };
  17761. _proto.insertAbort = function insertAbort(operation, type) {
  17762. var queue = this.queues[type];
  17763. queue.unshift(operation);
  17764. this.executeNext(type);
  17765. };
  17766. _proto.appendBlocker = function appendBlocker(type) {
  17767. var execute;
  17768. var promise = new Promise(function (resolve) {
  17769. execute = resolve;
  17770. });
  17771. var operation = {
  17772. execute: execute,
  17773. onStart: function onStart() {},
  17774. onComplete: function onComplete() {},
  17775. onError: function onError() {}
  17776. };
  17777. this.append(operation, type);
  17778. return promise;
  17779. };
  17780. _proto.executeNext = function executeNext(type) {
  17781. var queue = this.queues[type];
  17782. if (queue.length) {
  17783. var operation = queue[0];
  17784. try {
  17785. // Operations are expected to result in an 'updateend' event being fired. If not, the queue will lock. Operations
  17786. // which do not end with this event must call _onSBUpdateEnd manually
  17787. operation.execute();
  17788. } catch (error) {
  17789. logger.warn("[buffer-operation-queue]: Exception executing \"" + type + "\" SourceBuffer operation: " + error);
  17790. operation.onError(error);
  17791. // Only shift the current operation off, otherwise the updateend handler will do this for us
  17792. var sb = this.buffers[type];
  17793. if (!(sb != null && sb.updating)) {
  17794. this.shiftAndExecuteNext(type);
  17795. }
  17796. }
  17797. }
  17798. };
  17799. _proto.shiftAndExecuteNext = function shiftAndExecuteNext(type) {
  17800. this.queues[type].shift();
  17801. this.executeNext(type);
  17802. };
  17803. _proto.current = function current(type) {
  17804. return this.queues[type][0];
  17805. };
  17806. return BufferOperationQueue;
  17807. }();
  17808. var VIDEO_CODEC_PROFILE_REPLACE = /(avc[1234]|hvc1|hev1|dvh[1e]|vp09|av01)(?:\.[^.,]+)+/;
  17809. var BufferController = /*#__PURE__*/function () {
  17810. function BufferController(hls) {
  17811. var _this = this;
  17812. // The level details used to determine duration, target-duration and live
  17813. this.details = null;
  17814. // cache the self generated object url to detect hijack of video tag
  17815. this._objectUrl = null;
  17816. // A queue of buffer operations which require the SourceBuffer to not be updating upon execution
  17817. this.operationQueue = void 0;
  17818. // References to event listeners for each SourceBuffer, so that they can be referenced for event removal
  17819. this.listeners = void 0;
  17820. this.hls = void 0;
  17821. // The number of BUFFER_CODEC events received before any sourceBuffers are created
  17822. this.bufferCodecEventsExpected = 0;
  17823. // The total number of BUFFER_CODEC events received
  17824. this._bufferCodecEventsTotal = 0;
  17825. // A reference to the attached media element
  17826. this.media = null;
  17827. // A reference to the active media source
  17828. this.mediaSource = null;
  17829. // Last MP3 audio chunk appended
  17830. this.lastMpegAudioChunk = null;
  17831. this.appendSource = void 0;
  17832. // counters
  17833. this.appendErrors = {
  17834. audio: 0,
  17835. video: 0,
  17836. audiovideo: 0
  17837. };
  17838. this.tracks = {};
  17839. this.pendingTracks = {};
  17840. this.sourceBuffer = void 0;
  17841. this.log = void 0;
  17842. this.warn = void 0;
  17843. this.error = void 0;
  17844. this._onEndStreaming = function (event) {
  17845. if (!_this.hls) {
  17846. return;
  17847. }
  17848. _this.hls.pauseBuffering();
  17849. };
  17850. this._onStartStreaming = function (event) {
  17851. if (!_this.hls) {
  17852. return;
  17853. }
  17854. _this.hls.resumeBuffering();
  17855. };
  17856. // Keep as arrow functions so that we can directly reference these functions directly as event listeners
  17857. this._onMediaSourceOpen = function () {
  17858. var media = _this.media,
  17859. mediaSource = _this.mediaSource;
  17860. _this.log('Media source opened');
  17861. if (media) {
  17862. media.removeEventListener('emptied', _this._onMediaEmptied);
  17863. _this.updateMediaElementDuration();
  17864. _this.hls.trigger(Events.MEDIA_ATTACHED, {
  17865. media: media,
  17866. mediaSource: mediaSource
  17867. });
  17868. }
  17869. if (mediaSource) {
  17870. // once received, don't listen anymore to sourceopen event
  17871. mediaSource.removeEventListener('sourceopen', _this._onMediaSourceOpen);
  17872. }
  17873. _this.checkPendingTracks();
  17874. };
  17875. this._onMediaSourceClose = function () {
  17876. _this.log('Media source closed');
  17877. };
  17878. this._onMediaSourceEnded = function () {
  17879. _this.log('Media source ended');
  17880. };
  17881. this._onMediaEmptied = function () {
  17882. var mediaSrc = _this.mediaSrc,
  17883. _objectUrl = _this._objectUrl;
  17884. if (mediaSrc !== _objectUrl) {
  17885. logger.error("Media element src was set while attaching MediaSource (" + _objectUrl + " > " + mediaSrc + ")");
  17886. }
  17887. };
  17888. this.hls = hls;
  17889. var logPrefix = '[buffer-controller]';
  17890. this.appendSource = isManagedMediaSource(getMediaSource(hls.config.preferManagedMediaSource));
  17891. this.log = logger.log.bind(logger, logPrefix);
  17892. this.warn = logger.warn.bind(logger, logPrefix);
  17893. this.error = logger.error.bind(logger, logPrefix);
  17894. this._initSourceBuffer();
  17895. this.registerListeners();
  17896. }
  17897. var _proto = BufferController.prototype;
  17898. _proto.hasSourceTypes = function hasSourceTypes() {
  17899. return this.getSourceBufferTypes().length > 0 || Object.keys(this.pendingTracks).length > 0;
  17900. };
  17901. _proto.destroy = function destroy() {
  17902. this.unregisterListeners();
  17903. this.details = null;
  17904. this.lastMpegAudioChunk = null;
  17905. // @ts-ignore
  17906. this.hls = null;
  17907. };
  17908. _proto.registerListeners = function registerListeners() {
  17909. var hls = this.hls;
  17910. hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  17911. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  17912. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  17913. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  17914. hls.on(Events.BUFFER_RESET, this.onBufferReset, this);
  17915. hls.on(Events.BUFFER_APPENDING, this.onBufferAppending, this);
  17916. hls.on(Events.BUFFER_CODECS, this.onBufferCodecs, this);
  17917. hls.on(Events.BUFFER_EOS, this.onBufferEos, this);
  17918. hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  17919. hls.on(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  17920. hls.on(Events.FRAG_PARSED, this.onFragParsed, this);
  17921. hls.on(Events.FRAG_CHANGED, this.onFragChanged, this);
  17922. };
  17923. _proto.unregisterListeners = function unregisterListeners() {
  17924. var hls = this.hls;
  17925. hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  17926. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  17927. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  17928. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  17929. hls.off(Events.BUFFER_RESET, this.onBufferReset, this);
  17930. hls.off(Events.BUFFER_APPENDING, this.onBufferAppending, this);
  17931. hls.off(Events.BUFFER_CODECS, this.onBufferCodecs, this);
  17932. hls.off(Events.BUFFER_EOS, this.onBufferEos, this);
  17933. hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  17934. hls.off(Events.LEVEL_UPDATED, this.onLevelUpdated, this);
  17935. hls.off(Events.FRAG_PARSED, this.onFragParsed, this);
  17936. hls.off(Events.FRAG_CHANGED, this.onFragChanged, this);
  17937. };
  17938. _proto._initSourceBuffer = function _initSourceBuffer() {
  17939. this.sourceBuffer = {};
  17940. this.operationQueue = new BufferOperationQueue(this.sourceBuffer);
  17941. this.listeners = {
  17942. audio: [],
  17943. video: [],
  17944. audiovideo: []
  17945. };
  17946. this.appendErrors = {
  17947. audio: 0,
  17948. video: 0,
  17949. audiovideo: 0
  17950. };
  17951. this.lastMpegAudioChunk = null;
  17952. };
  17953. _proto.onManifestLoading = function onManifestLoading() {
  17954. this.bufferCodecEventsExpected = this._bufferCodecEventsTotal = 0;
  17955. this.details = null;
  17956. };
  17957. _proto.onManifestParsed = function onManifestParsed(event, data) {
  17958. // in case of alt audio 2 BUFFER_CODECS events will be triggered, one per stream controller
  17959. // sourcebuffers will be created all at once when the expected nb of tracks will be reached
  17960. // in case alt audio is not used, only one BUFFER_CODEC event will be fired from main stream controller
  17961. // it will contain the expected nb of source buffers, no need to compute it
  17962. var codecEvents = 2;
  17963. if (data.audio && !data.video || !data.altAudio || !true) {
  17964. codecEvents = 1;
  17965. }
  17966. this.bufferCodecEventsExpected = this._bufferCodecEventsTotal = codecEvents;
  17967. this.log(this.bufferCodecEventsExpected + " bufferCodec event(s) expected");
  17968. };
  17969. _proto.onMediaAttaching = function onMediaAttaching(event, data) {
  17970. var media = this.media = data.media;
  17971. var MediaSource = getMediaSource(this.appendSource);
  17972. if (media && MediaSource) {
  17973. var _ms$constructor;
  17974. var ms = this.mediaSource = new MediaSource();
  17975. this.log("created media source: " + ((_ms$constructor = ms.constructor) == null ? void 0 : _ms$constructor.name));
  17976. // MediaSource listeners are arrow functions with a lexical scope, and do not need to be bound
  17977. ms.addEventListener('sourceopen', this._onMediaSourceOpen);
  17978. ms.addEventListener('sourceended', this._onMediaSourceEnded);
  17979. ms.addEventListener('sourceclose', this._onMediaSourceClose);
  17980. if (this.appendSource) {
  17981. ms.addEventListener('startstreaming', this._onStartStreaming);
  17982. ms.addEventListener('endstreaming', this._onEndStreaming);
  17983. }
  17984. // cache the locally generated object url
  17985. var objectUrl = this._objectUrl = self.URL.createObjectURL(ms);
  17986. // link video and media Source
  17987. if (this.appendSource) {
  17988. try {
  17989. media.removeAttribute('src');
  17990. // ManagedMediaSource will not open without disableRemotePlayback set to false or source alternatives
  17991. var MMS = self.ManagedMediaSource;
  17992. media.disableRemotePlayback = media.disableRemotePlayback || MMS && ms instanceof MMS;
  17993. removeSourceChildren(media);
  17994. addSource(media, objectUrl);
  17995. media.load();
  17996. } catch (error) {
  17997. media.src = objectUrl;
  17998. }
  17999. } else {
  18000. media.src = objectUrl;
  18001. }
  18002. media.addEventListener('emptied', this._onMediaEmptied);
  18003. }
  18004. };
  18005. _proto.onMediaDetaching = function onMediaDetaching() {
  18006. var media = this.media,
  18007. mediaSource = this.mediaSource,
  18008. _objectUrl = this._objectUrl;
  18009. if (mediaSource) {
  18010. this.log('media source detaching');
  18011. if (mediaSource.readyState === 'open') {
  18012. try {
  18013. // endOfStream could trigger exception if any sourcebuffer is in updating state
  18014. // we don't really care about checking sourcebuffer state here,
  18015. // as we are anyway detaching the MediaSource
  18016. // let's just avoid this exception to propagate
  18017. mediaSource.endOfStream();
  18018. } catch (err) {
  18019. this.warn("onMediaDetaching: " + err.message + " while calling endOfStream");
  18020. }
  18021. }
  18022. // Clean up the SourceBuffers by invoking onBufferReset
  18023. this.onBufferReset();
  18024. mediaSource.removeEventListener('sourceopen', this._onMediaSourceOpen);
  18025. mediaSource.removeEventListener('sourceended', this._onMediaSourceEnded);
  18026. mediaSource.removeEventListener('sourceclose', this._onMediaSourceClose);
  18027. if (this.appendSource) {
  18028. mediaSource.removeEventListener('startstreaming', this._onStartStreaming);
  18029. mediaSource.removeEventListener('endstreaming', this._onEndStreaming);
  18030. }
  18031. // Detach properly the MediaSource from the HTMLMediaElement as
  18032. // suggested in https://github.com/w3c/media-source/issues/53.
  18033. if (media) {
  18034. media.removeEventListener('emptied', this._onMediaEmptied);
  18035. if (_objectUrl) {
  18036. self.URL.revokeObjectURL(_objectUrl);
  18037. }
  18038. // clean up video tag src only if it's our own url. some external libraries might
  18039. // hijack the video tag and change its 'src' without destroying the Hls instance first
  18040. if (this.mediaSrc === _objectUrl) {
  18041. media.removeAttribute('src');
  18042. if (this.appendSource) {
  18043. removeSourceChildren(media);
  18044. }
  18045. media.load();
  18046. } else {
  18047. this.warn('media|source.src was changed by a third party - skip cleanup');
  18048. }
  18049. }
  18050. this.mediaSource = null;
  18051. this.media = null;
  18052. this._objectUrl = null;
  18053. this.bufferCodecEventsExpected = this._bufferCodecEventsTotal;
  18054. this.pendingTracks = {};
  18055. this.tracks = {};
  18056. }
  18057. this.hls.trigger(Events.MEDIA_DETACHED, undefined);
  18058. };
  18059. _proto.onBufferReset = function onBufferReset() {
  18060. var _this2 = this;
  18061. this.getSourceBufferTypes().forEach(function (type) {
  18062. _this2.resetBuffer(type);
  18063. });
  18064. this._initSourceBuffer();
  18065. };
  18066. _proto.resetBuffer = function resetBuffer(type) {
  18067. var sb = this.sourceBuffer[type];
  18068. try {
  18069. if (sb) {
  18070. var _this$mediaSource;
  18071. this.removeBufferListeners(type);
  18072. // Synchronously remove the SB from the map before the next call in order to prevent an async function from
  18073. // accessing it
  18074. this.sourceBuffer[type] = undefined;
  18075. if ((_this$mediaSource = this.mediaSource) != null && _this$mediaSource.sourceBuffers.length) {
  18076. this.mediaSource.removeSourceBuffer(sb);
  18077. }
  18078. }
  18079. } catch (err) {
  18080. this.warn("onBufferReset " + type, err);
  18081. }
  18082. };
  18083. _proto.onBufferCodecs = function onBufferCodecs(event, data) {
  18084. var _this3 = this;
  18085. var sourceBufferCount = this.getSourceBufferTypes().length;
  18086. var trackNames = Object.keys(data);
  18087. trackNames.forEach(function (trackName) {
  18088. if (sourceBufferCount) {
  18089. // check if SourceBuffer codec needs to change
  18090. var track = _this3.tracks[trackName];
  18091. if (track && typeof track.buffer.changeType === 'function') {
  18092. var _trackCodec;
  18093. var _data$trackName = data[trackName],
  18094. id = _data$trackName.id,
  18095. codec = _data$trackName.codec,
  18096. levelCodec = _data$trackName.levelCodec,
  18097. container = _data$trackName.container,
  18098. metadata = _data$trackName.metadata;
  18099. var currentCodecFull = pickMostCompleteCodecName(track.codec, track.levelCodec);
  18100. var currentCodec = currentCodecFull == null ? void 0 : currentCodecFull.replace(VIDEO_CODEC_PROFILE_REPLACE, '$1');
  18101. var trackCodec = pickMostCompleteCodecName(codec, levelCodec);
  18102. var nextCodec = (_trackCodec = trackCodec) == null ? void 0 : _trackCodec.replace(VIDEO_CODEC_PROFILE_REPLACE, '$1');
  18103. if (trackCodec && currentCodec !== nextCodec) {
  18104. if (trackName.slice(0, 5) === 'audio') {
  18105. trackCodec = getCodecCompatibleName(trackCodec, _this3.appendSource);
  18106. }
  18107. var mimeType = container + ";codecs=" + trackCodec;
  18108. _this3.appendChangeType(trackName, mimeType);
  18109. _this3.log("switching codec " + currentCodecFull + " to " + trackCodec);
  18110. _this3.tracks[trackName] = {
  18111. buffer: track.buffer,
  18112. codec: codec,
  18113. container: container,
  18114. levelCodec: levelCodec,
  18115. metadata: metadata,
  18116. id: id
  18117. };
  18118. }
  18119. }
  18120. } else {
  18121. // if source buffer(s) not created yet, appended buffer tracks in this.pendingTracks
  18122. _this3.pendingTracks[trackName] = data[trackName];
  18123. }
  18124. });
  18125. // if sourcebuffers already created, do nothing ...
  18126. if (sourceBufferCount) {
  18127. return;
  18128. }
  18129. var bufferCodecEventsExpected = Math.max(this.bufferCodecEventsExpected - 1, 0);
  18130. if (this.bufferCodecEventsExpected !== bufferCodecEventsExpected) {
  18131. this.log(bufferCodecEventsExpected + " bufferCodec event(s) expected " + trackNames.join(','));
  18132. this.bufferCodecEventsExpected = bufferCodecEventsExpected;
  18133. }
  18134. if (this.mediaSource && this.mediaSource.readyState === 'open') {
  18135. this.checkPendingTracks();
  18136. }
  18137. };
  18138. _proto.appendChangeType = function appendChangeType(type, mimeType) {
  18139. var _this4 = this;
  18140. var operationQueue = this.operationQueue;
  18141. var operation = {
  18142. execute: function execute() {
  18143. var sb = _this4.sourceBuffer[type];
  18144. if (sb) {
  18145. _this4.log("changing " + type + " sourceBuffer type to " + mimeType);
  18146. sb.changeType(mimeType);
  18147. }
  18148. operationQueue.shiftAndExecuteNext(type);
  18149. },
  18150. onStart: function onStart() {},
  18151. onComplete: function onComplete() {},
  18152. onError: function onError(error) {
  18153. _this4.warn("Failed to change " + type + " SourceBuffer type", error);
  18154. }
  18155. };
  18156. operationQueue.append(operation, type, !!this.pendingTracks[type]);
  18157. };
  18158. _proto.onBufferAppending = function onBufferAppending(event, eventData) {
  18159. var _this5 = this;
  18160. var hls = this.hls,
  18161. operationQueue = this.operationQueue,
  18162. tracks = this.tracks;
  18163. var data = eventData.data,
  18164. type = eventData.type,
  18165. frag = eventData.frag,
  18166. part = eventData.part,
  18167. chunkMeta = eventData.chunkMeta;
  18168. var chunkStats = chunkMeta.buffering[type];
  18169. var bufferAppendingStart = self.performance.now();
  18170. chunkStats.start = bufferAppendingStart;
  18171. var fragBuffering = frag.stats.buffering;
  18172. var partBuffering = part ? part.stats.buffering : null;
  18173. if (fragBuffering.start === 0) {
  18174. fragBuffering.start = bufferAppendingStart;
  18175. }
  18176. if (partBuffering && partBuffering.start === 0) {
  18177. partBuffering.start = bufferAppendingStart;
  18178. }
  18179. // TODO: Only update timestampOffset when audio/mpeg fragment or part is not contiguous with previously appended
  18180. // Adjusting `SourceBuffer.timestampOffset` (desired point in the timeline where the next frames should be appended)
  18181. // in Chrome browser when we detect MPEG audio container and time delta between level PTS and `SourceBuffer.timestampOffset`
  18182. // is greater than 100ms (this is enough to handle seek for VOD or level change for LIVE videos).
  18183. // More info here: https://github.com/video-dev/hls.js/issues/332#issuecomment-257986486
  18184. var audioTrack = tracks.audio;
  18185. var checkTimestampOffset = false;
  18186. if (type === 'audio' && (audioTrack == null ? void 0 : audioTrack.container) === 'audio/mpeg') {
  18187. checkTimestampOffset = !this.lastMpegAudioChunk || chunkMeta.id === 1 || this.lastMpegAudioChunk.sn !== chunkMeta.sn;
  18188. this.lastMpegAudioChunk = chunkMeta;
  18189. }
  18190. var fragStart = frag.start;
  18191. var operation = {
  18192. execute: function execute() {
  18193. chunkStats.executeStart = self.performance.now();
  18194. if (checkTimestampOffset) {
  18195. var sb = _this5.sourceBuffer[type];
  18196. if (sb) {
  18197. var delta = fragStart - sb.timestampOffset;
  18198. if (Math.abs(delta) >= 0.1) {
  18199. _this5.log("Updating audio SourceBuffer timestampOffset to " + fragStart + " (delta: " + delta + ") sn: " + frag.sn + ")");
  18200. sb.timestampOffset = fragStart;
  18201. }
  18202. }
  18203. }
  18204. _this5.appendExecutor(data, type);
  18205. },
  18206. onStart: function onStart() {
  18207. // logger.debug(`[buffer-controller]: ${type} SourceBuffer updatestart`);
  18208. },
  18209. onComplete: function onComplete() {
  18210. // logger.debug(`[buffer-controller]: ${type} SourceBuffer updateend`);
  18211. var end = self.performance.now();
  18212. chunkStats.executeEnd = chunkStats.end = end;
  18213. if (fragBuffering.first === 0) {
  18214. fragBuffering.first = end;
  18215. }
  18216. if (partBuffering && partBuffering.first === 0) {
  18217. partBuffering.first = end;
  18218. }
  18219. var sourceBuffer = _this5.sourceBuffer;
  18220. var timeRanges = {};
  18221. for (var _type in sourceBuffer) {
  18222. timeRanges[_type] = BufferHelper.getBuffered(sourceBuffer[_type]);
  18223. }
  18224. _this5.appendErrors[type] = 0;
  18225. if (type === 'audio' || type === 'video') {
  18226. _this5.appendErrors.audiovideo = 0;
  18227. } else {
  18228. _this5.appendErrors.audio = 0;
  18229. _this5.appendErrors.video = 0;
  18230. }
  18231. _this5.hls.trigger(Events.BUFFER_APPENDED, {
  18232. type: type,
  18233. frag: frag,
  18234. part: part,
  18235. chunkMeta: chunkMeta,
  18236. parent: frag.type,
  18237. timeRanges: timeRanges
  18238. });
  18239. },
  18240. onError: function onError(error) {
  18241. // in case any error occured while appending, put back segment in segments table
  18242. var event = {
  18243. type: ErrorTypes.MEDIA_ERROR,
  18244. parent: frag.type,
  18245. details: ErrorDetails.BUFFER_APPEND_ERROR,
  18246. sourceBufferName: type,
  18247. frag: frag,
  18248. part: part,
  18249. chunkMeta: chunkMeta,
  18250. error: error,
  18251. err: error,
  18252. fatal: false
  18253. };
  18254. if (error.code === DOMException.QUOTA_EXCEEDED_ERR) {
  18255. // QuotaExceededError: http://www.w3.org/TR/html5/infrastructure.html#quotaexceedederror
  18256. // let's stop appending any segments, and report BUFFER_FULL_ERROR error
  18257. event.details = ErrorDetails.BUFFER_FULL_ERROR;
  18258. } else {
  18259. var appendErrorCount = ++_this5.appendErrors[type];
  18260. event.details = ErrorDetails.BUFFER_APPEND_ERROR;
  18261. /* with UHD content, we could get loop of quota exceeded error until
  18262. browser is able to evict some data from sourcebuffer. Retrying can help recover.
  18263. */
  18264. _this5.warn("Failed " + appendErrorCount + "/" + hls.config.appendErrorMaxRetry + " times to append segment in \"" + type + "\" sourceBuffer");
  18265. if (appendErrorCount >= hls.config.appendErrorMaxRetry) {
  18266. event.fatal = true;
  18267. }
  18268. }
  18269. hls.trigger(Events.ERROR, event);
  18270. }
  18271. };
  18272. operationQueue.append(operation, type, !!this.pendingTracks[type]);
  18273. };
  18274. _proto.onBufferFlushing = function onBufferFlushing(event, data) {
  18275. var _this6 = this;
  18276. var operationQueue = this.operationQueue;
  18277. var flushOperation = function flushOperation(type) {
  18278. return {
  18279. execute: _this6.removeExecutor.bind(_this6, type, data.startOffset, data.endOffset),
  18280. onStart: function onStart() {
  18281. // logger.debug(`[buffer-controller]: Started flushing ${data.startOffset} -> ${data.endOffset} for ${type} Source Buffer`);
  18282. },
  18283. onComplete: function onComplete() {
  18284. // logger.debug(`[buffer-controller]: Finished flushing ${data.startOffset} -> ${data.endOffset} for ${type} Source Buffer`);
  18285. _this6.hls.trigger(Events.BUFFER_FLUSHED, {
  18286. type: type
  18287. });
  18288. },
  18289. onError: function onError(error) {
  18290. _this6.warn("Failed to remove from " + type + " SourceBuffer", error);
  18291. }
  18292. };
  18293. };
  18294. if (data.type) {
  18295. operationQueue.append(flushOperation(data.type), data.type);
  18296. } else {
  18297. this.getSourceBufferTypes().forEach(function (type) {
  18298. operationQueue.append(flushOperation(type), type);
  18299. });
  18300. }
  18301. };
  18302. _proto.onFragParsed = function onFragParsed(event, data) {
  18303. var _this7 = this;
  18304. var frag = data.frag,
  18305. part = data.part;
  18306. var buffersAppendedTo = [];
  18307. var elementaryStreams = part ? part.elementaryStreams : frag.elementaryStreams;
  18308. if (elementaryStreams[ElementaryStreamTypes.AUDIOVIDEO]) {
  18309. buffersAppendedTo.push('audiovideo');
  18310. } else {
  18311. if (elementaryStreams[ElementaryStreamTypes.AUDIO]) {
  18312. buffersAppendedTo.push('audio');
  18313. }
  18314. if (elementaryStreams[ElementaryStreamTypes.VIDEO]) {
  18315. buffersAppendedTo.push('video');
  18316. }
  18317. }
  18318. var onUnblocked = function onUnblocked() {
  18319. var now = self.performance.now();
  18320. frag.stats.buffering.end = now;
  18321. if (part) {
  18322. part.stats.buffering.end = now;
  18323. }
  18324. var stats = part ? part.stats : frag.stats;
  18325. _this7.hls.trigger(Events.FRAG_BUFFERED, {
  18326. frag: frag,
  18327. part: part,
  18328. stats: stats,
  18329. id: frag.type
  18330. });
  18331. };
  18332. if (buffersAppendedTo.length === 0) {
  18333. this.warn("Fragments must have at least one ElementaryStreamType set. type: " + frag.type + " level: " + frag.level + " sn: " + frag.sn);
  18334. }
  18335. this.blockBuffers(onUnblocked, buffersAppendedTo);
  18336. };
  18337. _proto.onFragChanged = function onFragChanged(event, data) {
  18338. this.trimBuffers();
  18339. }
  18340. // on BUFFER_EOS mark matching sourcebuffer(s) as ended and trigger checkEos()
  18341. // an undefined data.type will mark all buffers as EOS.
  18342. ;
  18343. _proto.onBufferEos = function onBufferEos(event, data) {
  18344. var _this8 = this;
  18345. var ended = this.getSourceBufferTypes().reduce(function (acc, type) {
  18346. var sb = _this8.sourceBuffer[type];
  18347. if (sb && (!data.type || data.type === type)) {
  18348. sb.ending = true;
  18349. if (!sb.ended) {
  18350. sb.ended = true;
  18351. _this8.log(type + " sourceBuffer now EOS");
  18352. }
  18353. }
  18354. return acc && !!(!sb || sb.ended);
  18355. }, true);
  18356. if (ended) {
  18357. this.log("Queueing mediaSource.endOfStream()");
  18358. this.blockBuffers(function () {
  18359. _this8.getSourceBufferTypes().forEach(function (type) {
  18360. var sb = _this8.sourceBuffer[type];
  18361. if (sb) {
  18362. sb.ending = false;
  18363. }
  18364. });
  18365. var mediaSource = _this8.mediaSource;
  18366. if (!mediaSource || mediaSource.readyState !== 'open') {
  18367. if (mediaSource) {
  18368. _this8.log("Could not call mediaSource.endOfStream(). mediaSource.readyState: " + mediaSource.readyState);
  18369. }
  18370. return;
  18371. }
  18372. _this8.log("Calling mediaSource.endOfStream()");
  18373. // Allow this to throw and be caught by the enqueueing function
  18374. mediaSource.endOfStream();
  18375. });
  18376. }
  18377. };
  18378. _proto.onLevelUpdated = function onLevelUpdated(event, _ref) {
  18379. var details = _ref.details;
  18380. if (!details.fragments.length) {
  18381. return;
  18382. }
  18383. this.details = details;
  18384. if (this.getSourceBufferTypes().length) {
  18385. this.blockBuffers(this.updateMediaElementDuration.bind(this));
  18386. } else {
  18387. this.updateMediaElementDuration();
  18388. }
  18389. };
  18390. _proto.trimBuffers = function trimBuffers() {
  18391. var hls = this.hls,
  18392. details = this.details,
  18393. media = this.media;
  18394. if (!media || details === null) {
  18395. return;
  18396. }
  18397. var sourceBufferTypes = this.getSourceBufferTypes();
  18398. if (!sourceBufferTypes.length) {
  18399. return;
  18400. }
  18401. var config = hls.config;
  18402. var currentTime = media.currentTime;
  18403. var targetDuration = details.levelTargetDuration;
  18404. // Support for deprecated liveBackBufferLength
  18405. var backBufferLength = details.live && config.liveBackBufferLength !== null ? config.liveBackBufferLength : config.backBufferLength;
  18406. if (isFiniteNumber(backBufferLength) && backBufferLength > 0) {
  18407. var maxBackBufferLength = Math.max(backBufferLength, targetDuration);
  18408. var targetBackBufferPosition = Math.floor(currentTime / targetDuration) * targetDuration - maxBackBufferLength;
  18409. this.flushBackBuffer(currentTime, targetDuration, targetBackBufferPosition);
  18410. }
  18411. if (isFiniteNumber(config.frontBufferFlushThreshold) && config.frontBufferFlushThreshold > 0) {
  18412. var frontBufferLength = Math.max(config.maxBufferLength, config.frontBufferFlushThreshold);
  18413. var maxFrontBufferLength = Math.max(frontBufferLength, targetDuration);
  18414. var targetFrontBufferPosition = Math.floor(currentTime / targetDuration) * targetDuration + maxFrontBufferLength;
  18415. this.flushFrontBuffer(currentTime, targetDuration, targetFrontBufferPosition);
  18416. }
  18417. };
  18418. _proto.flushBackBuffer = function flushBackBuffer(currentTime, targetDuration, targetBackBufferPosition) {
  18419. var _this9 = this;
  18420. var details = this.details,
  18421. sourceBuffer = this.sourceBuffer;
  18422. var sourceBufferTypes = this.getSourceBufferTypes();
  18423. sourceBufferTypes.forEach(function (type) {
  18424. var sb = sourceBuffer[type];
  18425. if (sb) {
  18426. var buffered = BufferHelper.getBuffered(sb);
  18427. // when target buffer start exceeds actual buffer start
  18428. if (buffered.length > 0 && targetBackBufferPosition > buffered.start(0)) {
  18429. _this9.hls.trigger(Events.BACK_BUFFER_REACHED, {
  18430. bufferEnd: targetBackBufferPosition
  18431. });
  18432. // Support for deprecated event:
  18433. if (details != null && details.live) {
  18434. _this9.hls.trigger(Events.LIVE_BACK_BUFFER_REACHED, {
  18435. bufferEnd: targetBackBufferPosition
  18436. });
  18437. } else if (sb.ended && buffered.end(buffered.length - 1) - currentTime < targetDuration * 2) {
  18438. _this9.log("Cannot flush " + type + " back buffer while SourceBuffer is in ended state");
  18439. return;
  18440. }
  18441. _this9.hls.trigger(Events.BUFFER_FLUSHING, {
  18442. startOffset: 0,
  18443. endOffset: targetBackBufferPosition,
  18444. type: type
  18445. });
  18446. }
  18447. }
  18448. });
  18449. };
  18450. _proto.flushFrontBuffer = function flushFrontBuffer(currentTime, targetDuration, targetFrontBufferPosition) {
  18451. var _this10 = this;
  18452. var sourceBuffer = this.sourceBuffer;
  18453. var sourceBufferTypes = this.getSourceBufferTypes();
  18454. sourceBufferTypes.forEach(function (type) {
  18455. var sb = sourceBuffer[type];
  18456. if (sb) {
  18457. var buffered = BufferHelper.getBuffered(sb);
  18458. var numBufferedRanges = buffered.length;
  18459. // The buffer is either empty or contiguous
  18460. if (numBufferedRanges < 2) {
  18461. return;
  18462. }
  18463. var bufferStart = buffered.start(numBufferedRanges - 1);
  18464. var bufferEnd = buffered.end(numBufferedRanges - 1);
  18465. // No flush if we can tolerate the current buffer length or the current buffer range we would flush is contiguous with current position
  18466. if (targetFrontBufferPosition > bufferStart || currentTime >= bufferStart && currentTime <= bufferEnd) {
  18467. return;
  18468. } else if (sb.ended && currentTime - bufferEnd < 2 * targetDuration) {
  18469. _this10.log("Cannot flush " + type + " front buffer while SourceBuffer is in ended state");
  18470. return;
  18471. }
  18472. _this10.hls.trigger(Events.BUFFER_FLUSHING, {
  18473. startOffset: bufferStart,
  18474. endOffset: Infinity,
  18475. type: type
  18476. });
  18477. }
  18478. });
  18479. }
  18480. /**
  18481. * Update Media Source duration to current level duration or override to Infinity if configuration parameter
  18482. * 'liveDurationInfinity` is set to `true`
  18483. * More details: https://github.com/video-dev/hls.js/issues/355
  18484. */;
  18485. _proto.updateMediaElementDuration = function updateMediaElementDuration() {
  18486. if (!this.details || !this.media || !this.mediaSource || this.mediaSource.readyState !== 'open') {
  18487. return;
  18488. }
  18489. var details = this.details,
  18490. hls = this.hls,
  18491. media = this.media,
  18492. mediaSource = this.mediaSource;
  18493. var levelDuration = details.fragments[0].start + details.totalduration;
  18494. var mediaDuration = media.duration;
  18495. var msDuration = isFiniteNumber(mediaSource.duration) ? mediaSource.duration : 0;
  18496. if (details.live && hls.config.liveDurationInfinity) {
  18497. // Override duration to Infinity
  18498. mediaSource.duration = Infinity;
  18499. this.updateSeekableRange(details);
  18500. } else if (levelDuration > msDuration && levelDuration > mediaDuration || !isFiniteNumber(mediaDuration)) {
  18501. // levelDuration was the last value we set.
  18502. // not using mediaSource.duration as the browser may tweak this value
  18503. // only update Media Source duration if its value increase, this is to avoid
  18504. // flushing already buffered portion when switching between quality level
  18505. this.log("Updating Media Source duration to " + levelDuration.toFixed(3));
  18506. mediaSource.duration = levelDuration;
  18507. }
  18508. };
  18509. _proto.updateSeekableRange = function updateSeekableRange(levelDetails) {
  18510. var mediaSource = this.mediaSource;
  18511. var fragments = levelDetails.fragments;
  18512. var len = fragments.length;
  18513. if (len && levelDetails.live && mediaSource != null && mediaSource.setLiveSeekableRange) {
  18514. var start = Math.max(0, fragments[0].start);
  18515. var end = Math.max(start, start + levelDetails.totalduration);
  18516. this.log("Media Source duration is set to " + mediaSource.duration + ". Setting seekable range to " + start + "-" + end + ".");
  18517. mediaSource.setLiveSeekableRange(start, end);
  18518. }
  18519. };
  18520. _proto.checkPendingTracks = function checkPendingTracks() {
  18521. var bufferCodecEventsExpected = this.bufferCodecEventsExpected,
  18522. operationQueue = this.operationQueue,
  18523. pendingTracks = this.pendingTracks;
  18524. // Check if we've received all of the expected bufferCodec events. When none remain, create all the sourceBuffers at once.
  18525. // This is important because the MSE spec allows implementations to throw QuotaExceededErrors if creating new sourceBuffers after
  18526. // data has been appended to existing ones.
  18527. // 2 tracks is the max (one for audio, one for video). If we've reach this max go ahead and create the buffers.
  18528. var pendingTracksCount = Object.keys(pendingTracks).length;
  18529. if (pendingTracksCount && (!bufferCodecEventsExpected || pendingTracksCount === 2 || 'audiovideo' in pendingTracks)) {
  18530. // ok, let's create them now !
  18531. this.createSourceBuffers(pendingTracks);
  18532. this.pendingTracks = {};
  18533. // append any pending segments now !
  18534. var buffers = this.getSourceBufferTypes();
  18535. if (buffers.length) {
  18536. this.hls.trigger(Events.BUFFER_CREATED, {
  18537. tracks: this.tracks
  18538. });
  18539. buffers.forEach(function (type) {
  18540. operationQueue.executeNext(type);
  18541. });
  18542. } else {
  18543. var error = new Error('could not create source buffer for media codec(s)');
  18544. this.hls.trigger(Events.ERROR, {
  18545. type: ErrorTypes.MEDIA_ERROR,
  18546. details: ErrorDetails.BUFFER_INCOMPATIBLE_CODECS_ERROR,
  18547. fatal: true,
  18548. error: error,
  18549. reason: error.message
  18550. });
  18551. }
  18552. }
  18553. };
  18554. _proto.createSourceBuffers = function createSourceBuffers(tracks) {
  18555. var _this11 = this;
  18556. var sourceBuffer = this.sourceBuffer,
  18557. mediaSource = this.mediaSource;
  18558. if (!mediaSource) {
  18559. throw Error('createSourceBuffers called when mediaSource was null');
  18560. }
  18561. var _loop = function _loop(trackName) {
  18562. if (!sourceBuffer[trackName]) {
  18563. var _track$levelCodec;
  18564. var track = tracks[trackName];
  18565. if (!track) {
  18566. throw Error("source buffer exists for track " + trackName + ", however track does not");
  18567. }
  18568. // use levelCodec as first priority unless it contains multiple comma-separated codec values
  18569. var codec = ((_track$levelCodec = track.levelCodec) == null ? void 0 : _track$levelCodec.indexOf(',')) === -1 ? track.levelCodec : track.codec;
  18570. if (codec) {
  18571. if (trackName.slice(0, 5) === 'audio') {
  18572. codec = getCodecCompatibleName(codec, _this11.appendSource);
  18573. }
  18574. }
  18575. var mimeType = track.container + ";codecs=" + codec;
  18576. _this11.log("creating sourceBuffer(" + mimeType + ")");
  18577. try {
  18578. var sb = sourceBuffer[trackName] = mediaSource.addSourceBuffer(mimeType);
  18579. var sbName = trackName;
  18580. _this11.addBufferListener(sbName, 'updatestart', _this11._onSBUpdateStart);
  18581. _this11.addBufferListener(sbName, 'updateend', _this11._onSBUpdateEnd);
  18582. _this11.addBufferListener(sbName, 'error', _this11._onSBUpdateError);
  18583. // ManagedSourceBuffer bufferedchange event
  18584. if (_this11.appendSource) {
  18585. _this11.addBufferListener(sbName, 'bufferedchange', function (type, event) {
  18586. // If media was ejected check for a change. Added ranges are redundant with changes on 'updateend' event.
  18587. var removedRanges = event.removedRanges;
  18588. if (removedRanges != null && removedRanges.length) {
  18589. _this11.hls.trigger(Events.BUFFER_FLUSHED, {
  18590. type: trackName
  18591. });
  18592. }
  18593. });
  18594. }
  18595. _this11.tracks[trackName] = {
  18596. buffer: sb,
  18597. codec: codec,
  18598. container: track.container,
  18599. levelCodec: track.levelCodec,
  18600. metadata: track.metadata,
  18601. id: track.id
  18602. };
  18603. } catch (err) {
  18604. _this11.error("error while trying to add sourceBuffer: " + err.message);
  18605. _this11.hls.trigger(Events.ERROR, {
  18606. type: ErrorTypes.MEDIA_ERROR,
  18607. details: ErrorDetails.BUFFER_ADD_CODEC_ERROR,
  18608. fatal: false,
  18609. error: err,
  18610. sourceBufferName: trackName,
  18611. mimeType: mimeType
  18612. });
  18613. }
  18614. }
  18615. };
  18616. for (var trackName in tracks) {
  18617. _loop(trackName);
  18618. }
  18619. };
  18620. _proto._onSBUpdateStart = function _onSBUpdateStart(type) {
  18621. var operationQueue = this.operationQueue;
  18622. var operation = operationQueue.current(type);
  18623. operation.onStart();
  18624. };
  18625. _proto._onSBUpdateEnd = function _onSBUpdateEnd(type) {
  18626. var _this$mediaSource2;
  18627. if (((_this$mediaSource2 = this.mediaSource) == null ? void 0 : _this$mediaSource2.readyState) === 'closed') {
  18628. this.resetBuffer(type);
  18629. return;
  18630. }
  18631. var operationQueue = this.operationQueue;
  18632. var operation = operationQueue.current(type);
  18633. operation.onComplete();
  18634. operationQueue.shiftAndExecuteNext(type);
  18635. };
  18636. _proto._onSBUpdateError = function _onSBUpdateError(type, event) {
  18637. var _this$mediaSource3;
  18638. var error = new Error(type + " SourceBuffer error. MediaSource readyState: " + ((_this$mediaSource3 = this.mediaSource) == null ? void 0 : _this$mediaSource3.readyState));
  18639. this.error("" + error, event);
  18640. // according to http://www.w3.org/TR/media-source/#sourcebuffer-append-error
  18641. // SourceBuffer errors are not necessarily fatal; if so, the HTMLMediaElement will fire an error event
  18642. this.hls.trigger(Events.ERROR, {
  18643. type: ErrorTypes.MEDIA_ERROR,
  18644. details: ErrorDetails.BUFFER_APPENDING_ERROR,
  18645. sourceBufferName: type,
  18646. error: error,
  18647. fatal: false
  18648. });
  18649. // updateend is always fired after error, so we'll allow that to shift the current operation off of the queue
  18650. var operation = this.operationQueue.current(type);
  18651. if (operation) {
  18652. operation.onError(error);
  18653. }
  18654. }
  18655. // This method must result in an updateend event; if remove is not called, _onSBUpdateEnd must be called manually
  18656. ;
  18657. _proto.removeExecutor = function removeExecutor(type, startOffset, endOffset) {
  18658. var media = this.media,
  18659. mediaSource = this.mediaSource,
  18660. operationQueue = this.operationQueue,
  18661. sourceBuffer = this.sourceBuffer;
  18662. var sb = sourceBuffer[type];
  18663. if (!media || !mediaSource || !sb) {
  18664. this.warn("Attempting to remove from the " + type + " SourceBuffer, but it does not exist");
  18665. operationQueue.shiftAndExecuteNext(type);
  18666. return;
  18667. }
  18668. var mediaDuration = isFiniteNumber(media.duration) ? media.duration : Infinity;
  18669. var msDuration = isFiniteNumber(mediaSource.duration) ? mediaSource.duration : Infinity;
  18670. var removeStart = Math.max(0, startOffset);
  18671. var removeEnd = Math.min(endOffset, mediaDuration, msDuration);
  18672. if (removeEnd > removeStart && (!sb.ending || sb.ended)) {
  18673. sb.ended = false;
  18674. this.log("Removing [" + removeStart + "," + removeEnd + "] from the " + type + " SourceBuffer");
  18675. sb.remove(removeStart, removeEnd);
  18676. } else {
  18677. // Cycle the queue
  18678. operationQueue.shiftAndExecuteNext(type);
  18679. }
  18680. }
  18681. // This method must result in an updateend event; if append is not called, _onSBUpdateEnd must be called manually
  18682. ;
  18683. _proto.appendExecutor = function appendExecutor(data, type) {
  18684. var sb = this.sourceBuffer[type];
  18685. if (!sb) {
  18686. if (!this.pendingTracks[type]) {
  18687. throw new Error("Attempting to append to the " + type + " SourceBuffer, but it does not exist");
  18688. }
  18689. return;
  18690. }
  18691. sb.ended = false;
  18692. sb.appendBuffer(data);
  18693. }
  18694. // Enqueues an operation to each SourceBuffer queue which, upon execution, resolves a promise. When all promises
  18695. // resolve, the onUnblocked function is executed. Functions calling this method do not need to unblock the queue
  18696. // upon completion, since we already do it here
  18697. ;
  18698. _proto.blockBuffers = function blockBuffers(onUnblocked, buffers) {
  18699. var _this12 = this;
  18700. if (buffers === void 0) {
  18701. buffers = this.getSourceBufferTypes();
  18702. }
  18703. if (!buffers.length) {
  18704. this.log('Blocking operation requested, but no SourceBuffers exist');
  18705. Promise.resolve().then(onUnblocked);
  18706. return;
  18707. }
  18708. var operationQueue = this.operationQueue;
  18709. // logger.debug(`[buffer-controller]: Blocking ${buffers} SourceBuffer`);
  18710. var blockingOperations = buffers.map(function (type) {
  18711. return operationQueue.appendBlocker(type);
  18712. });
  18713. Promise.all(blockingOperations).then(function () {
  18714. // logger.debug(`[buffer-controller]: Blocking operation resolved; unblocking ${buffers} SourceBuffer`);
  18715. onUnblocked();
  18716. buffers.forEach(function (type) {
  18717. var sb = _this12.sourceBuffer[type];
  18718. // Only cycle the queue if the SB is not updating. There's a bug in Chrome which sets the SB updating flag to
  18719. // true when changing the MediaSource duration (https://bugs.chromium.org/p/chromium/issues/detail?id=959359&can=2&q=mediasource%20duration)
  18720. // While this is a workaround, it's probably useful to have around
  18721. if (!(sb != null && sb.updating)) {
  18722. operationQueue.shiftAndExecuteNext(type);
  18723. }
  18724. });
  18725. });
  18726. };
  18727. _proto.getSourceBufferTypes = function getSourceBufferTypes() {
  18728. return Object.keys(this.sourceBuffer);
  18729. };
  18730. _proto.addBufferListener = function addBufferListener(type, event, fn) {
  18731. var buffer = this.sourceBuffer[type];
  18732. if (!buffer) {
  18733. return;
  18734. }
  18735. var listener = fn.bind(this, type);
  18736. this.listeners[type].push({
  18737. event: event,
  18738. listener: listener
  18739. });
  18740. buffer.addEventListener(event, listener);
  18741. };
  18742. _proto.removeBufferListeners = function removeBufferListeners(type) {
  18743. var buffer = this.sourceBuffer[type];
  18744. if (!buffer) {
  18745. return;
  18746. }
  18747. this.listeners[type].forEach(function (l) {
  18748. buffer.removeEventListener(l.event, l.listener);
  18749. });
  18750. };
  18751. _createClass(BufferController, [{
  18752. key: "mediaSrc",
  18753. get: function get() {
  18754. var _this$media;
  18755. var media = ((_this$media = this.media) == null ? void 0 : _this$media.firstChild) || this.media;
  18756. return media == null ? void 0 : media.src;
  18757. }
  18758. }]);
  18759. return BufferController;
  18760. }();
  18761. function removeSourceChildren(node) {
  18762. var sourceChildren = node.querySelectorAll('source');
  18763. [].slice.call(sourceChildren).forEach(function (source) {
  18764. node.removeChild(source);
  18765. });
  18766. }
  18767. function addSource(media, url) {
  18768. var source = self.document.createElement('source');
  18769. source.type = 'video/mp4';
  18770. source.src = url;
  18771. media.appendChild(source);
  18772. }
  18773. /**
  18774. *
  18775. * This code was ported from the dash.js project at:
  18776. * https://github.com/Dash-Industry-Forum/dash.js/blob/development/externals/cea608-parser.js
  18777. * https://github.com/Dash-Industry-Forum/dash.js/commit/8269b26a761e0853bb21d78780ed945144ecdd4d#diff-71bc295a2d6b6b7093a1d3290d53a4b2
  18778. *
  18779. * The original copyright appears below:
  18780. *
  18781. * The copyright in this software is being made available under the BSD License,
  18782. * included below. This software may be subject to other third party and contributor
  18783. * rights, including patent rights, and no such rights are granted under this license.
  18784. *
  18785. * Copyright (c) 2015-2016, DASH Industry Forum.
  18786. * All rights reserved.
  18787. *
  18788. * Redistribution and use in source and binary forms, with or without modification,
  18789. * are permitted provided that the following conditions are met:
  18790. * 1. Redistributions of source code must retain the above copyright notice, this
  18791. * list of conditions and the following disclaimer.
  18792. * * Redistributions in binary form must reproduce the above copyright notice,
  18793. * this list of conditions and the following disclaimer in the documentation and/or
  18794. * other materials provided with the distribution.
  18795. * 2. Neither the name of Dash Industry Forum nor the names of its
  18796. * contributors may be used to endorse or promote products derived from this software
  18797. * without specific prior written permission.
  18798. *
  18799. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY
  18800. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18801. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18802. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  18803. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  18804. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  18805. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  18806. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  18807. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  18808. * POSSIBILITY OF SUCH DAMAGE.
  18809. */
  18810. /**
  18811. * Exceptions from regular ASCII. CodePoints are mapped to UTF-16 codes
  18812. */
  18813. var specialCea608CharsCodes = {
  18814. 0x2a: 0xe1,
  18815. // lowercase a, acute accent
  18816. 0x5c: 0xe9,
  18817. // lowercase e, acute accent
  18818. 0x5e: 0xed,
  18819. // lowercase i, acute accent
  18820. 0x5f: 0xf3,
  18821. // lowercase o, acute accent
  18822. 0x60: 0xfa,
  18823. // lowercase u, acute accent
  18824. 0x7b: 0xe7,
  18825. // lowercase c with cedilla
  18826. 0x7c: 0xf7,
  18827. // division symbol
  18828. 0x7d: 0xd1,
  18829. // uppercase N tilde
  18830. 0x7e: 0xf1,
  18831. // lowercase n tilde
  18832. 0x7f: 0x2588,
  18833. // Full block
  18834. // THIS BLOCK INCLUDES THE 16 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
  18835. // THAT COME FROM HI BYTE=0x11 AND LOW BETWEEN 0x30 AND 0x3F
  18836. // THIS MEANS THAT \x50 MUST BE ADDED TO THE VALUES
  18837. 0x80: 0xae,
  18838. // Registered symbol (R)
  18839. 0x81: 0xb0,
  18840. // degree sign
  18841. 0x82: 0xbd,
  18842. // 1/2 symbol
  18843. 0x83: 0xbf,
  18844. // Inverted (open) question mark
  18845. 0x84: 0x2122,
  18846. // Trademark symbol (TM)
  18847. 0x85: 0xa2,
  18848. // Cents symbol
  18849. 0x86: 0xa3,
  18850. // Pounds sterling
  18851. 0x87: 0x266a,
  18852. // Music 8'th note
  18853. 0x88: 0xe0,
  18854. // lowercase a, grave accent
  18855. 0x89: 0x20,
  18856. // transparent space (regular)
  18857. 0x8a: 0xe8,
  18858. // lowercase e, grave accent
  18859. 0x8b: 0xe2,
  18860. // lowercase a, circumflex accent
  18861. 0x8c: 0xea,
  18862. // lowercase e, circumflex accent
  18863. 0x8d: 0xee,
  18864. // lowercase i, circumflex accent
  18865. 0x8e: 0xf4,
  18866. // lowercase o, circumflex accent
  18867. 0x8f: 0xfb,
  18868. // lowercase u, circumflex accent
  18869. // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
  18870. // THAT COME FROM HI BYTE=0x12 AND LOW BETWEEN 0x20 AND 0x3F
  18871. 0x90: 0xc1,
  18872. // capital letter A with acute
  18873. 0x91: 0xc9,
  18874. // capital letter E with acute
  18875. 0x92: 0xd3,
  18876. // capital letter O with acute
  18877. 0x93: 0xda,
  18878. // capital letter U with acute
  18879. 0x94: 0xdc,
  18880. // capital letter U with diaresis
  18881. 0x95: 0xfc,
  18882. // lowercase letter U with diaeresis
  18883. 0x96: 0x2018,
  18884. // opening single quote
  18885. 0x97: 0xa1,
  18886. // inverted exclamation mark
  18887. 0x98: 0x2a,
  18888. // asterisk
  18889. 0x99: 0x2019,
  18890. // closing single quote
  18891. 0x9a: 0x2501,
  18892. // box drawings heavy horizontal
  18893. 0x9b: 0xa9,
  18894. // copyright sign
  18895. 0x9c: 0x2120,
  18896. // Service mark
  18897. 0x9d: 0x2022,
  18898. // (round) bullet
  18899. 0x9e: 0x201c,
  18900. // Left double quotation mark
  18901. 0x9f: 0x201d,
  18902. // Right double quotation mark
  18903. 0xa0: 0xc0,
  18904. // uppercase A, grave accent
  18905. 0xa1: 0xc2,
  18906. // uppercase A, circumflex
  18907. 0xa2: 0xc7,
  18908. // uppercase C with cedilla
  18909. 0xa3: 0xc8,
  18910. // uppercase E, grave accent
  18911. 0xa4: 0xca,
  18912. // uppercase E, circumflex
  18913. 0xa5: 0xcb,
  18914. // capital letter E with diaresis
  18915. 0xa6: 0xeb,
  18916. // lowercase letter e with diaresis
  18917. 0xa7: 0xce,
  18918. // uppercase I, circumflex
  18919. 0xa8: 0xcf,
  18920. // uppercase I, with diaresis
  18921. 0xa9: 0xef,
  18922. // lowercase i, with diaresis
  18923. 0xaa: 0xd4,
  18924. // uppercase O, circumflex
  18925. 0xab: 0xd9,
  18926. // uppercase U, grave accent
  18927. 0xac: 0xf9,
  18928. // lowercase u, grave accent
  18929. 0xad: 0xdb,
  18930. // uppercase U, circumflex
  18931. 0xae: 0xab,
  18932. // left-pointing double angle quotation mark
  18933. 0xaf: 0xbb,
  18934. // right-pointing double angle quotation mark
  18935. // THIS BLOCK INCLUDES THE 32 EXTENDED (TWO-BYTE) LINE 21 CHARACTERS
  18936. // THAT COME FROM HI BYTE=0x13 AND LOW BETWEEN 0x20 AND 0x3F
  18937. 0xb0: 0xc3,
  18938. // Uppercase A, tilde
  18939. 0xb1: 0xe3,
  18940. // Lowercase a, tilde
  18941. 0xb2: 0xcd,
  18942. // Uppercase I, acute accent
  18943. 0xb3: 0xcc,
  18944. // Uppercase I, grave accent
  18945. 0xb4: 0xec,
  18946. // Lowercase i, grave accent
  18947. 0xb5: 0xd2,
  18948. // Uppercase O, grave accent
  18949. 0xb6: 0xf2,
  18950. // Lowercase o, grave accent
  18951. 0xb7: 0xd5,
  18952. // Uppercase O, tilde
  18953. 0xb8: 0xf5,
  18954. // Lowercase o, tilde
  18955. 0xb9: 0x7b,
  18956. // Open curly brace
  18957. 0xba: 0x7d,
  18958. // Closing curly brace
  18959. 0xbb: 0x5c,
  18960. // Backslash
  18961. 0xbc: 0x5e,
  18962. // Caret
  18963. 0xbd: 0x5f,
  18964. // Underscore
  18965. 0xbe: 0x7c,
  18966. // Pipe (vertical line)
  18967. 0xbf: 0x223c,
  18968. // Tilde operator
  18969. 0xc0: 0xc4,
  18970. // Uppercase A, umlaut
  18971. 0xc1: 0xe4,
  18972. // Lowercase A, umlaut
  18973. 0xc2: 0xd6,
  18974. // Uppercase O, umlaut
  18975. 0xc3: 0xf6,
  18976. // Lowercase o, umlaut
  18977. 0xc4: 0xdf,
  18978. // Esszett (sharp S)
  18979. 0xc5: 0xa5,
  18980. // Yen symbol
  18981. 0xc6: 0xa4,
  18982. // Generic currency sign
  18983. 0xc7: 0x2503,
  18984. // Box drawings heavy vertical
  18985. 0xc8: 0xc5,
  18986. // Uppercase A, ring
  18987. 0xc9: 0xe5,
  18988. // Lowercase A, ring
  18989. 0xca: 0xd8,
  18990. // Uppercase O, stroke
  18991. 0xcb: 0xf8,
  18992. // Lowercase o, strok
  18993. 0xcc: 0x250f,
  18994. // Box drawings heavy down and right
  18995. 0xcd: 0x2513,
  18996. // Box drawings heavy down and left
  18997. 0xce: 0x2517,
  18998. // Box drawings heavy up and right
  18999. 0xcf: 0x251b // Box drawings heavy up and left
  19000. };
  19001. /**
  19002. * Utils
  19003. */
  19004. var getCharForByte = function getCharForByte(_byte) {
  19005. return String.fromCharCode(specialCea608CharsCodes[_byte] || _byte);
  19006. };
  19007. var NR_ROWS = 15;
  19008. var NR_COLS = 100;
  19009. // Tables to look up row from PAC data
  19010. var rowsLowCh1 = {
  19011. 0x11: 1,
  19012. 0x12: 3,
  19013. 0x15: 5,
  19014. 0x16: 7,
  19015. 0x17: 9,
  19016. 0x10: 11,
  19017. 0x13: 12,
  19018. 0x14: 14
  19019. };
  19020. var rowsHighCh1 = {
  19021. 0x11: 2,
  19022. 0x12: 4,
  19023. 0x15: 6,
  19024. 0x16: 8,
  19025. 0x17: 10,
  19026. 0x13: 13,
  19027. 0x14: 15
  19028. };
  19029. var rowsLowCh2 = {
  19030. 0x19: 1,
  19031. 0x1a: 3,
  19032. 0x1d: 5,
  19033. 0x1e: 7,
  19034. 0x1f: 9,
  19035. 0x18: 11,
  19036. 0x1b: 12,
  19037. 0x1c: 14
  19038. };
  19039. var rowsHighCh2 = {
  19040. 0x19: 2,
  19041. 0x1a: 4,
  19042. 0x1d: 6,
  19043. 0x1e: 8,
  19044. 0x1f: 10,
  19045. 0x1b: 13,
  19046. 0x1c: 15
  19047. };
  19048. var backgroundColors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'black', 'transparent'];
  19049. var CaptionsLogger = /*#__PURE__*/function () {
  19050. function CaptionsLogger() {
  19051. this.time = null;
  19052. this.verboseLevel = 0;
  19053. }
  19054. var _proto = CaptionsLogger.prototype;
  19055. _proto.log = function log(severity, msg) {
  19056. if (this.verboseLevel >= severity) {
  19057. var m = typeof msg === 'function' ? msg() : msg;
  19058. logger.log(this.time + " [" + severity + "] " + m);
  19059. }
  19060. };
  19061. return CaptionsLogger;
  19062. }();
  19063. var numArrayToHexArray = function numArrayToHexArray(numArray) {
  19064. var hexArray = [];
  19065. for (var j = 0; j < numArray.length; j++) {
  19066. hexArray.push(numArray[j].toString(16));
  19067. }
  19068. return hexArray;
  19069. };
  19070. var PenState = /*#__PURE__*/function () {
  19071. function PenState() {
  19072. this.foreground = 'white';
  19073. this.underline = false;
  19074. this.italics = false;
  19075. this.background = 'black';
  19076. this.flash = false;
  19077. }
  19078. var _proto2 = PenState.prototype;
  19079. _proto2.reset = function reset() {
  19080. this.foreground = 'white';
  19081. this.underline = false;
  19082. this.italics = false;
  19083. this.background = 'black';
  19084. this.flash = false;
  19085. };
  19086. _proto2.setStyles = function setStyles(styles) {
  19087. var attribs = ['foreground', 'underline', 'italics', 'background', 'flash'];
  19088. for (var i = 0; i < attribs.length; i++) {
  19089. var style = attribs[i];
  19090. if (styles.hasOwnProperty(style)) {
  19091. this[style] = styles[style];
  19092. }
  19093. }
  19094. };
  19095. _proto2.isDefault = function isDefault() {
  19096. return this.foreground === 'white' && !this.underline && !this.italics && this.background === 'black' && !this.flash;
  19097. };
  19098. _proto2.equals = function equals(other) {
  19099. return this.foreground === other.foreground && this.underline === other.underline && this.italics === other.italics && this.background === other.background && this.flash === other.flash;
  19100. };
  19101. _proto2.copy = function copy(newPenState) {
  19102. this.foreground = newPenState.foreground;
  19103. this.underline = newPenState.underline;
  19104. this.italics = newPenState.italics;
  19105. this.background = newPenState.background;
  19106. this.flash = newPenState.flash;
  19107. };
  19108. _proto2.toString = function toString() {
  19109. return 'color=' + this.foreground + ', underline=' + this.underline + ', italics=' + this.italics + ', background=' + this.background + ', flash=' + this.flash;
  19110. };
  19111. return PenState;
  19112. }();
  19113. /**
  19114. * Unicode character with styling and background.
  19115. * @constructor
  19116. */
  19117. var StyledUnicodeChar = /*#__PURE__*/function () {
  19118. function StyledUnicodeChar() {
  19119. this.uchar = ' ';
  19120. this.penState = new PenState();
  19121. }
  19122. var _proto3 = StyledUnicodeChar.prototype;
  19123. _proto3.reset = function reset() {
  19124. this.uchar = ' ';
  19125. this.penState.reset();
  19126. };
  19127. _proto3.setChar = function setChar(uchar, newPenState) {
  19128. this.uchar = uchar;
  19129. this.penState.copy(newPenState);
  19130. };
  19131. _proto3.setPenState = function setPenState(newPenState) {
  19132. this.penState.copy(newPenState);
  19133. };
  19134. _proto3.equals = function equals(other) {
  19135. return this.uchar === other.uchar && this.penState.equals(other.penState);
  19136. };
  19137. _proto3.copy = function copy(newChar) {
  19138. this.uchar = newChar.uchar;
  19139. this.penState.copy(newChar.penState);
  19140. };
  19141. _proto3.isEmpty = function isEmpty() {
  19142. return this.uchar === ' ' && this.penState.isDefault();
  19143. };
  19144. return StyledUnicodeChar;
  19145. }();
  19146. /**
  19147. * CEA-608 row consisting of NR_COLS instances of StyledUnicodeChar.
  19148. * @constructor
  19149. */
  19150. var Row = /*#__PURE__*/function () {
  19151. function Row(logger) {
  19152. this.chars = [];
  19153. this.pos = 0;
  19154. this.currPenState = new PenState();
  19155. this.cueStartTime = null;
  19156. this.logger = void 0;
  19157. for (var i = 0; i < NR_COLS; i++) {
  19158. this.chars.push(new StyledUnicodeChar());
  19159. }
  19160. this.logger = logger;
  19161. }
  19162. var _proto4 = Row.prototype;
  19163. _proto4.equals = function equals(other) {
  19164. for (var i = 0; i < NR_COLS; i++) {
  19165. if (!this.chars[i].equals(other.chars[i])) {
  19166. return false;
  19167. }
  19168. }
  19169. return true;
  19170. };
  19171. _proto4.copy = function copy(other) {
  19172. for (var i = 0; i < NR_COLS; i++) {
  19173. this.chars[i].copy(other.chars[i]);
  19174. }
  19175. };
  19176. _proto4.isEmpty = function isEmpty() {
  19177. var empty = true;
  19178. for (var i = 0; i < NR_COLS; i++) {
  19179. if (!this.chars[i].isEmpty()) {
  19180. empty = false;
  19181. break;
  19182. }
  19183. }
  19184. return empty;
  19185. }
  19186. /**
  19187. * Set the cursor to a valid column.
  19188. */;
  19189. _proto4.setCursor = function setCursor(absPos) {
  19190. if (this.pos !== absPos) {
  19191. this.pos = absPos;
  19192. }
  19193. if (this.pos < 0) {
  19194. this.logger.log(3, 'Negative cursor position ' + this.pos);
  19195. this.pos = 0;
  19196. } else if (this.pos > NR_COLS) {
  19197. this.logger.log(3, 'Too large cursor position ' + this.pos);
  19198. this.pos = NR_COLS;
  19199. }
  19200. }
  19201. /**
  19202. * Move the cursor relative to current position.
  19203. */;
  19204. _proto4.moveCursor = function moveCursor(relPos) {
  19205. var newPos = this.pos + relPos;
  19206. if (relPos > 1) {
  19207. for (var i = this.pos + 1; i < newPos + 1; i++) {
  19208. this.chars[i].setPenState(this.currPenState);
  19209. }
  19210. }
  19211. this.setCursor(newPos);
  19212. }
  19213. /**
  19214. * Backspace, move one step back and clear character.
  19215. */;
  19216. _proto4.backSpace = function backSpace() {
  19217. this.moveCursor(-1);
  19218. this.chars[this.pos].setChar(' ', this.currPenState);
  19219. };
  19220. _proto4.insertChar = function insertChar(_byte2) {
  19221. var _this = this;
  19222. if (_byte2 >= 0x90) {
  19223. // Extended char
  19224. this.backSpace();
  19225. }
  19226. var _char = getCharForByte(_byte2);
  19227. if (this.pos >= NR_COLS) {
  19228. this.logger.log(0, function () {
  19229. return 'Cannot insert ' + _byte2.toString(16) + ' (' + _char + ') at position ' + _this.pos + '. Skipping it!';
  19230. });
  19231. return;
  19232. }
  19233. this.chars[this.pos].setChar(_char, this.currPenState);
  19234. this.moveCursor(1);
  19235. };
  19236. _proto4.clearFromPos = function clearFromPos(startPos) {
  19237. var i;
  19238. for (i = startPos; i < NR_COLS; i++) {
  19239. this.chars[i].reset();
  19240. }
  19241. };
  19242. _proto4.clear = function clear() {
  19243. this.clearFromPos(0);
  19244. this.pos = 0;
  19245. this.currPenState.reset();
  19246. };
  19247. _proto4.clearToEndOfRow = function clearToEndOfRow() {
  19248. this.clearFromPos(this.pos);
  19249. };
  19250. _proto4.getTextString = function getTextString() {
  19251. var chars = [];
  19252. var empty = true;
  19253. for (var i = 0; i < NR_COLS; i++) {
  19254. var _char2 = this.chars[i].uchar;
  19255. if (_char2 !== ' ') {
  19256. empty = false;
  19257. }
  19258. chars.push(_char2);
  19259. }
  19260. if (empty) {
  19261. return '';
  19262. } else {
  19263. return chars.join('');
  19264. }
  19265. };
  19266. _proto4.setPenStyles = function setPenStyles(styles) {
  19267. this.currPenState.setStyles(styles);
  19268. var currChar = this.chars[this.pos];
  19269. currChar.setPenState(this.currPenState);
  19270. };
  19271. return Row;
  19272. }();
  19273. /**
  19274. * Keep a CEA-608 screen of 32x15 styled characters
  19275. * @constructor
  19276. */
  19277. var CaptionScreen = /*#__PURE__*/function () {
  19278. function CaptionScreen(logger) {
  19279. this.rows = [];
  19280. this.currRow = NR_ROWS - 1;
  19281. this.nrRollUpRows = null;
  19282. this.lastOutputScreen = null;
  19283. this.logger = void 0;
  19284. for (var i = 0; i < NR_ROWS; i++) {
  19285. this.rows.push(new Row(logger));
  19286. }
  19287. this.logger = logger;
  19288. }
  19289. var _proto5 = CaptionScreen.prototype;
  19290. _proto5.reset = function reset() {
  19291. for (var i = 0; i < NR_ROWS; i++) {
  19292. this.rows[i].clear();
  19293. }
  19294. this.currRow = NR_ROWS - 1;
  19295. };
  19296. _proto5.equals = function equals(other) {
  19297. var equal = true;
  19298. for (var i = 0; i < NR_ROWS; i++) {
  19299. if (!this.rows[i].equals(other.rows[i])) {
  19300. equal = false;
  19301. break;
  19302. }
  19303. }
  19304. return equal;
  19305. };
  19306. _proto5.copy = function copy(other) {
  19307. for (var i = 0; i < NR_ROWS; i++) {
  19308. this.rows[i].copy(other.rows[i]);
  19309. }
  19310. };
  19311. _proto5.isEmpty = function isEmpty() {
  19312. var empty = true;
  19313. for (var i = 0; i < NR_ROWS; i++) {
  19314. if (!this.rows[i].isEmpty()) {
  19315. empty = false;
  19316. break;
  19317. }
  19318. }
  19319. return empty;
  19320. };
  19321. _proto5.backSpace = function backSpace() {
  19322. var row = this.rows[this.currRow];
  19323. row.backSpace();
  19324. };
  19325. _proto5.clearToEndOfRow = function clearToEndOfRow() {
  19326. var row = this.rows[this.currRow];
  19327. row.clearToEndOfRow();
  19328. }
  19329. /**
  19330. * Insert a character (without styling) in the current row.
  19331. */;
  19332. _proto5.insertChar = function insertChar(_char3) {
  19333. var row = this.rows[this.currRow];
  19334. row.insertChar(_char3);
  19335. };
  19336. _proto5.setPen = function setPen(styles) {
  19337. var row = this.rows[this.currRow];
  19338. row.setPenStyles(styles);
  19339. };
  19340. _proto5.moveCursor = function moveCursor(relPos) {
  19341. var row = this.rows[this.currRow];
  19342. row.moveCursor(relPos);
  19343. };
  19344. _proto5.setCursor = function setCursor(absPos) {
  19345. this.logger.log(2, 'setCursor: ' + absPos);
  19346. var row = this.rows[this.currRow];
  19347. row.setCursor(absPos);
  19348. };
  19349. _proto5.setPAC = function setPAC(pacData) {
  19350. this.logger.log(2, function () {
  19351. return 'pacData = ' + JSON.stringify(pacData);
  19352. });
  19353. var newRow = pacData.row - 1;
  19354. if (this.nrRollUpRows && newRow < this.nrRollUpRows - 1) {
  19355. newRow = this.nrRollUpRows - 1;
  19356. }
  19357. // Make sure this only affects Roll-up Captions by checking this.nrRollUpRows
  19358. if (this.nrRollUpRows && this.currRow !== newRow) {
  19359. // clear all rows first
  19360. for (var i = 0; i < NR_ROWS; i++) {
  19361. this.rows[i].clear();
  19362. }
  19363. // Copy this.nrRollUpRows rows from lastOutputScreen and place it in the newRow location
  19364. // topRowIndex - the start of rows to copy (inclusive index)
  19365. var topRowIndex = this.currRow + 1 - this.nrRollUpRows;
  19366. // We only copy if the last position was already shown.
  19367. // We use the cueStartTime value to check this.
  19368. var lastOutputScreen = this.lastOutputScreen;
  19369. if (lastOutputScreen) {
  19370. var prevLineTime = lastOutputScreen.rows[topRowIndex].cueStartTime;
  19371. var time = this.logger.time;
  19372. if (prevLineTime !== null && time !== null && prevLineTime < time) {
  19373. for (var _i = 0; _i < this.nrRollUpRows; _i++) {
  19374. this.rows[newRow - this.nrRollUpRows + _i + 1].copy(lastOutputScreen.rows[topRowIndex + _i]);
  19375. }
  19376. }
  19377. }
  19378. }
  19379. this.currRow = newRow;
  19380. var row = this.rows[this.currRow];
  19381. if (pacData.indent !== null) {
  19382. var indent = pacData.indent;
  19383. var prevPos = Math.max(indent - 1, 0);
  19384. row.setCursor(pacData.indent);
  19385. pacData.color = row.chars[prevPos].penState.foreground;
  19386. }
  19387. var styles = {
  19388. foreground: pacData.color,
  19389. underline: pacData.underline,
  19390. italics: pacData.italics,
  19391. background: 'black',
  19392. flash: false
  19393. };
  19394. this.setPen(styles);
  19395. }
  19396. /**
  19397. * Set background/extra foreground, but first do back_space, and then insert space (backwards compatibility).
  19398. */;
  19399. _proto5.setBkgData = function setBkgData(bkgData) {
  19400. this.logger.log(2, function () {
  19401. return 'bkgData = ' + JSON.stringify(bkgData);
  19402. });
  19403. this.backSpace();
  19404. this.setPen(bkgData);
  19405. this.insertChar(0x20); // Space
  19406. };
  19407. _proto5.setRollUpRows = function setRollUpRows(nrRows) {
  19408. this.nrRollUpRows = nrRows;
  19409. };
  19410. _proto5.rollUp = function rollUp() {
  19411. var _this2 = this;
  19412. if (this.nrRollUpRows === null) {
  19413. this.logger.log(3, 'roll_up but nrRollUpRows not set yet');
  19414. return; // Not properly setup
  19415. }
  19416. this.logger.log(1, function () {
  19417. return _this2.getDisplayText();
  19418. });
  19419. var topRowIndex = this.currRow + 1 - this.nrRollUpRows;
  19420. var topRow = this.rows.splice(topRowIndex, 1)[0];
  19421. topRow.clear();
  19422. this.rows.splice(this.currRow, 0, topRow);
  19423. this.logger.log(2, 'Rolling up');
  19424. // this.logger.log(VerboseLevel.TEXT, this.get_display_text())
  19425. }
  19426. /**
  19427. * Get all non-empty rows with as unicode text.
  19428. */;
  19429. _proto5.getDisplayText = function getDisplayText(asOneRow) {
  19430. asOneRow = asOneRow || false;
  19431. var displayText = [];
  19432. var text = '';
  19433. var rowNr = -1;
  19434. for (var i = 0; i < NR_ROWS; i++) {
  19435. var rowText = this.rows[i].getTextString();
  19436. if (rowText) {
  19437. rowNr = i + 1;
  19438. if (asOneRow) {
  19439. displayText.push('Row ' + rowNr + ": '" + rowText + "'");
  19440. } else {
  19441. displayText.push(rowText.trim());
  19442. }
  19443. }
  19444. }
  19445. if (displayText.length > 0) {
  19446. if (asOneRow) {
  19447. text = '[' + displayText.join(' | ') + ']';
  19448. } else {
  19449. text = displayText.join('\n');
  19450. }
  19451. }
  19452. return text;
  19453. };
  19454. _proto5.getTextAndFormat = function getTextAndFormat() {
  19455. return this.rows;
  19456. };
  19457. return CaptionScreen;
  19458. }();
  19459. // var modes = ['MODE_ROLL-UP', 'MODE_POP-ON', 'MODE_PAINT-ON', 'MODE_TEXT'];
  19460. var Cea608Channel = /*#__PURE__*/function () {
  19461. function Cea608Channel(channelNumber, outputFilter, logger) {
  19462. this.chNr = void 0;
  19463. this.outputFilter = void 0;
  19464. this.mode = void 0;
  19465. this.verbose = void 0;
  19466. this.displayedMemory = void 0;
  19467. this.nonDisplayedMemory = void 0;
  19468. this.lastOutputScreen = void 0;
  19469. this.currRollUpRow = void 0;
  19470. this.writeScreen = void 0;
  19471. this.cueStartTime = void 0;
  19472. this.logger = void 0;
  19473. this.chNr = channelNumber;
  19474. this.outputFilter = outputFilter;
  19475. this.mode = null;
  19476. this.verbose = 0;
  19477. this.displayedMemory = new CaptionScreen(logger);
  19478. this.nonDisplayedMemory = new CaptionScreen(logger);
  19479. this.lastOutputScreen = new CaptionScreen(logger);
  19480. this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
  19481. this.writeScreen = this.displayedMemory;
  19482. this.mode = null;
  19483. this.cueStartTime = null; // Keeps track of where a cue started.
  19484. this.logger = logger;
  19485. }
  19486. var _proto6 = Cea608Channel.prototype;
  19487. _proto6.reset = function reset() {
  19488. this.mode = null;
  19489. this.displayedMemory.reset();
  19490. this.nonDisplayedMemory.reset();
  19491. this.lastOutputScreen.reset();
  19492. this.outputFilter.reset();
  19493. this.currRollUpRow = this.displayedMemory.rows[NR_ROWS - 1];
  19494. this.writeScreen = this.displayedMemory;
  19495. this.mode = null;
  19496. this.cueStartTime = null;
  19497. };
  19498. _proto6.getHandler = function getHandler() {
  19499. return this.outputFilter;
  19500. };
  19501. _proto6.setHandler = function setHandler(newHandler) {
  19502. this.outputFilter = newHandler;
  19503. };
  19504. _proto6.setPAC = function setPAC(pacData) {
  19505. this.writeScreen.setPAC(pacData);
  19506. };
  19507. _proto6.setBkgData = function setBkgData(bkgData) {
  19508. this.writeScreen.setBkgData(bkgData);
  19509. };
  19510. _proto6.setMode = function setMode(newMode) {
  19511. if (newMode === this.mode) {
  19512. return;
  19513. }
  19514. this.mode = newMode;
  19515. this.logger.log(2, function () {
  19516. return 'MODE=' + newMode;
  19517. });
  19518. if (this.mode === 'MODE_POP-ON') {
  19519. this.writeScreen = this.nonDisplayedMemory;
  19520. } else {
  19521. this.writeScreen = this.displayedMemory;
  19522. this.writeScreen.reset();
  19523. }
  19524. if (this.mode !== 'MODE_ROLL-UP') {
  19525. this.displayedMemory.nrRollUpRows = null;
  19526. this.nonDisplayedMemory.nrRollUpRows = null;
  19527. }
  19528. this.mode = newMode;
  19529. };
  19530. _proto6.insertChars = function insertChars(chars) {
  19531. var _this3 = this;
  19532. for (var i = 0; i < chars.length; i++) {
  19533. this.writeScreen.insertChar(chars[i]);
  19534. }
  19535. var screen = this.writeScreen === this.displayedMemory ? 'DISP' : 'NON_DISP';
  19536. this.logger.log(2, function () {
  19537. return screen + ': ' + _this3.writeScreen.getDisplayText(true);
  19538. });
  19539. if (this.mode === 'MODE_PAINT-ON' || this.mode === 'MODE_ROLL-UP') {
  19540. this.logger.log(1, function () {
  19541. return 'DISPLAYED: ' + _this3.displayedMemory.getDisplayText(true);
  19542. });
  19543. this.outputDataUpdate();
  19544. }
  19545. };
  19546. _proto6.ccRCL = function ccRCL() {
  19547. // Resume Caption Loading (switch mode to Pop On)
  19548. this.logger.log(2, 'RCL - Resume Caption Loading');
  19549. this.setMode('MODE_POP-ON');
  19550. };
  19551. _proto6.ccBS = function ccBS() {
  19552. // BackSpace
  19553. this.logger.log(2, 'BS - BackSpace');
  19554. if (this.mode === 'MODE_TEXT') {
  19555. return;
  19556. }
  19557. this.writeScreen.backSpace();
  19558. if (this.writeScreen === this.displayedMemory) {
  19559. this.outputDataUpdate();
  19560. }
  19561. };
  19562. _proto6.ccAOF = function ccAOF() {
  19563. // Reserved (formerly Alarm Off)
  19564. };
  19565. _proto6.ccAON = function ccAON() {
  19566. // Reserved (formerly Alarm On)
  19567. };
  19568. _proto6.ccDER = function ccDER() {
  19569. // Delete to End of Row
  19570. this.logger.log(2, 'DER- Delete to End of Row');
  19571. this.writeScreen.clearToEndOfRow();
  19572. this.outputDataUpdate();
  19573. };
  19574. _proto6.ccRU = function ccRU(nrRows) {
  19575. // Roll-Up Captions-2,3,or 4 Rows
  19576. this.logger.log(2, 'RU(' + nrRows + ') - Roll Up');
  19577. this.writeScreen = this.displayedMemory;
  19578. this.setMode('MODE_ROLL-UP');
  19579. this.writeScreen.setRollUpRows(nrRows);
  19580. };
  19581. _proto6.ccFON = function ccFON() {
  19582. // Flash On
  19583. this.logger.log(2, 'FON - Flash On');
  19584. this.writeScreen.setPen({
  19585. flash: true
  19586. });
  19587. };
  19588. _proto6.ccRDC = function ccRDC() {
  19589. // Resume Direct Captioning (switch mode to PaintOn)
  19590. this.logger.log(2, 'RDC - Resume Direct Captioning');
  19591. this.setMode('MODE_PAINT-ON');
  19592. };
  19593. _proto6.ccTR = function ccTR() {
  19594. // Text Restart in text mode (not supported, however)
  19595. this.logger.log(2, 'TR');
  19596. this.setMode('MODE_TEXT');
  19597. };
  19598. _proto6.ccRTD = function ccRTD() {
  19599. // Resume Text Display in Text mode (not supported, however)
  19600. this.logger.log(2, 'RTD');
  19601. this.setMode('MODE_TEXT');
  19602. };
  19603. _proto6.ccEDM = function ccEDM() {
  19604. // Erase Displayed Memory
  19605. this.logger.log(2, 'EDM - Erase Displayed Memory');
  19606. this.displayedMemory.reset();
  19607. this.outputDataUpdate(true);
  19608. };
  19609. _proto6.ccCR = function ccCR() {
  19610. // Carriage Return
  19611. this.logger.log(2, 'CR - Carriage Return');
  19612. this.writeScreen.rollUp();
  19613. this.outputDataUpdate(true);
  19614. };
  19615. _proto6.ccENM = function ccENM() {
  19616. // Erase Non-Displayed Memory
  19617. this.logger.log(2, 'ENM - Erase Non-displayed Memory');
  19618. this.nonDisplayedMemory.reset();
  19619. };
  19620. _proto6.ccEOC = function ccEOC() {
  19621. var _this4 = this;
  19622. // End of Caption (Flip Memories)
  19623. this.logger.log(2, 'EOC - End Of Caption');
  19624. if (this.mode === 'MODE_POP-ON') {
  19625. var tmp = this.displayedMemory;
  19626. this.displayedMemory = this.nonDisplayedMemory;
  19627. this.nonDisplayedMemory = tmp;
  19628. this.writeScreen = this.nonDisplayedMemory;
  19629. this.logger.log(1, function () {
  19630. return 'DISP: ' + _this4.displayedMemory.getDisplayText();
  19631. });
  19632. }
  19633. this.outputDataUpdate(true);
  19634. };
  19635. _proto6.ccTO = function ccTO(nrCols) {
  19636. // Tab Offset 1,2, or 3 columns
  19637. this.logger.log(2, 'TO(' + nrCols + ') - Tab Offset');
  19638. this.writeScreen.moveCursor(nrCols);
  19639. };
  19640. _proto6.ccMIDROW = function ccMIDROW(secondByte) {
  19641. // Parse MIDROW command
  19642. var styles = {
  19643. flash: false
  19644. };
  19645. styles.underline = secondByte % 2 === 1;
  19646. styles.italics = secondByte >= 0x2e;
  19647. if (!styles.italics) {
  19648. var colorIndex = Math.floor(secondByte / 2) - 0x10;
  19649. var colors = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta'];
  19650. styles.foreground = colors[colorIndex];
  19651. } else {
  19652. styles.foreground = 'white';
  19653. }
  19654. this.logger.log(2, 'MIDROW: ' + JSON.stringify(styles));
  19655. this.writeScreen.setPen(styles);
  19656. };
  19657. _proto6.outputDataUpdate = function outputDataUpdate(dispatch) {
  19658. if (dispatch === void 0) {
  19659. dispatch = false;
  19660. }
  19661. var time = this.logger.time;
  19662. if (time === null) {
  19663. return;
  19664. }
  19665. if (this.outputFilter) {
  19666. if (this.cueStartTime === null && !this.displayedMemory.isEmpty()) {
  19667. // Start of a new cue
  19668. this.cueStartTime = time;
  19669. } else {
  19670. if (!this.displayedMemory.equals(this.lastOutputScreen)) {
  19671. this.outputFilter.newCue(this.cueStartTime, time, this.lastOutputScreen);
  19672. if (dispatch && this.outputFilter.dispatchCue) {
  19673. this.outputFilter.dispatchCue();
  19674. }
  19675. this.cueStartTime = this.displayedMemory.isEmpty() ? null : time;
  19676. }
  19677. }
  19678. this.lastOutputScreen.copy(this.displayedMemory);
  19679. }
  19680. };
  19681. _proto6.cueSplitAtTime = function cueSplitAtTime(t) {
  19682. if (this.outputFilter) {
  19683. if (!this.displayedMemory.isEmpty()) {
  19684. if (this.outputFilter.newCue) {
  19685. this.outputFilter.newCue(this.cueStartTime, t, this.displayedMemory);
  19686. }
  19687. this.cueStartTime = t;
  19688. }
  19689. }
  19690. };
  19691. return Cea608Channel;
  19692. }(); // Will be 1 or 2 when parsing captions
  19693. var Cea608Parser = /*#__PURE__*/function () {
  19694. function Cea608Parser(field, out1, out2) {
  19695. this.channels = void 0;
  19696. this.currentChannel = 0;
  19697. this.cmdHistory = createCmdHistory();
  19698. this.logger = void 0;
  19699. var logger = this.logger = new CaptionsLogger();
  19700. this.channels = [null, new Cea608Channel(field, out1, logger), new Cea608Channel(field + 1, out2, logger)];
  19701. }
  19702. var _proto7 = Cea608Parser.prototype;
  19703. _proto7.getHandler = function getHandler(channel) {
  19704. return this.channels[channel].getHandler();
  19705. };
  19706. _proto7.setHandler = function setHandler(channel, newHandler) {
  19707. this.channels[channel].setHandler(newHandler);
  19708. }
  19709. /**
  19710. * Add data for time t in forms of list of bytes (unsigned ints). The bytes are treated as pairs.
  19711. */;
  19712. _proto7.addData = function addData(time, byteList) {
  19713. var _this5 = this;
  19714. this.logger.time = time;
  19715. var _loop = function _loop(i) {
  19716. var a = byteList[i] & 0x7f;
  19717. var b = byteList[i + 1] & 0x7f;
  19718. var cmdFound = false;
  19719. var charsFound = null;
  19720. if (a === 0 && b === 0) {
  19721. return 0; // continue
  19722. } else {
  19723. _this5.logger.log(3, function () {
  19724. return '[' + numArrayToHexArray([byteList[i], byteList[i + 1]]) + '] -> (' + numArrayToHexArray([a, b]) + ')';
  19725. });
  19726. }
  19727. var cmdHistory = _this5.cmdHistory;
  19728. var isControlCode = a >= 0x10 && a <= 0x1f;
  19729. if (isControlCode) {
  19730. // Skip redundant control codes
  19731. if (hasCmdRepeated(a, b, cmdHistory)) {
  19732. setLastCmd(null, null, cmdHistory);
  19733. _this5.logger.log(3, function () {
  19734. return 'Repeated command (' + numArrayToHexArray([a, b]) + ') is dropped';
  19735. });
  19736. return 0; // continue
  19737. }
  19738. setLastCmd(a, b, _this5.cmdHistory);
  19739. cmdFound = _this5.parseCmd(a, b);
  19740. if (!cmdFound) {
  19741. cmdFound = _this5.parseMidrow(a, b);
  19742. }
  19743. if (!cmdFound) {
  19744. cmdFound = _this5.parsePAC(a, b);
  19745. }
  19746. if (!cmdFound) {
  19747. cmdFound = _this5.parseBackgroundAttributes(a, b);
  19748. }
  19749. } else {
  19750. setLastCmd(null, null, cmdHistory);
  19751. }
  19752. if (!cmdFound) {
  19753. charsFound = _this5.parseChars(a, b);
  19754. if (charsFound) {
  19755. var currChNr = _this5.currentChannel;
  19756. if (currChNr && currChNr > 0) {
  19757. var channel = _this5.channels[currChNr];
  19758. channel.insertChars(charsFound);
  19759. } else {
  19760. _this5.logger.log(2, 'No channel found yet. TEXT-MODE?');
  19761. }
  19762. }
  19763. }
  19764. if (!cmdFound && !charsFound) {
  19765. _this5.logger.log(2, function () {
  19766. return "Couldn't parse cleaned data " + numArrayToHexArray([a, b]) + ' orig: ' + numArrayToHexArray([byteList[i], byteList[i + 1]]);
  19767. });
  19768. }
  19769. },
  19770. _ret;
  19771. for (var i = 0; i < byteList.length; i += 2) {
  19772. _ret = _loop(i);
  19773. if (_ret === 0) continue;
  19774. }
  19775. }
  19776. /**
  19777. * Parse Command.
  19778. * @returns True if a command was found
  19779. */;
  19780. _proto7.parseCmd = function parseCmd(a, b) {
  19781. var cond1 = (a === 0x14 || a === 0x1c || a === 0x15 || a === 0x1d) && b >= 0x20 && b <= 0x2f;
  19782. var cond2 = (a === 0x17 || a === 0x1f) && b >= 0x21 && b <= 0x23;
  19783. if (!(cond1 || cond2)) {
  19784. return false;
  19785. }
  19786. var chNr = a === 0x14 || a === 0x15 || a === 0x17 ? 1 : 2;
  19787. var channel = this.channels[chNr];
  19788. if (a === 0x14 || a === 0x15 || a === 0x1c || a === 0x1d) {
  19789. if (b === 0x20) {
  19790. channel.ccRCL();
  19791. } else if (b === 0x21) {
  19792. channel.ccBS();
  19793. } else if (b === 0x22) {
  19794. channel.ccAOF();
  19795. } else if (b === 0x23) {
  19796. channel.ccAON();
  19797. } else if (b === 0x24) {
  19798. channel.ccDER();
  19799. } else if (b === 0x25) {
  19800. channel.ccRU(2);
  19801. } else if (b === 0x26) {
  19802. channel.ccRU(3);
  19803. } else if (b === 0x27) {
  19804. channel.ccRU(4);
  19805. } else if (b === 0x28) {
  19806. channel.ccFON();
  19807. } else if (b === 0x29) {
  19808. channel.ccRDC();
  19809. } else if (b === 0x2a) {
  19810. channel.ccTR();
  19811. } else if (b === 0x2b) {
  19812. channel.ccRTD();
  19813. } else if (b === 0x2c) {
  19814. channel.ccEDM();
  19815. } else if (b === 0x2d) {
  19816. channel.ccCR();
  19817. } else if (b === 0x2e) {
  19818. channel.ccENM();
  19819. } else if (b === 0x2f) {
  19820. channel.ccEOC();
  19821. }
  19822. } else {
  19823. // a == 0x17 || a == 0x1F
  19824. channel.ccTO(b - 0x20);
  19825. }
  19826. this.currentChannel = chNr;
  19827. return true;
  19828. }
  19829. /**
  19830. * Parse midrow styling command
  19831. */;
  19832. _proto7.parseMidrow = function parseMidrow(a, b) {
  19833. var chNr = 0;
  19834. if ((a === 0x11 || a === 0x19) && b >= 0x20 && b <= 0x2f) {
  19835. if (a === 0x11) {
  19836. chNr = 1;
  19837. } else {
  19838. chNr = 2;
  19839. }
  19840. if (chNr !== this.currentChannel) {
  19841. this.logger.log(0, 'Mismatch channel in midrow parsing');
  19842. return false;
  19843. }
  19844. var channel = this.channels[chNr];
  19845. if (!channel) {
  19846. return false;
  19847. }
  19848. channel.ccMIDROW(b);
  19849. this.logger.log(3, function () {
  19850. return 'MIDROW (' + numArrayToHexArray([a, b]) + ')';
  19851. });
  19852. return true;
  19853. }
  19854. return false;
  19855. }
  19856. /**
  19857. * Parse Preable Access Codes (Table 53).
  19858. * @returns {Boolean} Tells if PAC found
  19859. */;
  19860. _proto7.parsePAC = function parsePAC(a, b) {
  19861. var row;
  19862. var case1 = (a >= 0x11 && a <= 0x17 || a >= 0x19 && a <= 0x1f) && b >= 0x40 && b <= 0x7f;
  19863. var case2 = (a === 0x10 || a === 0x18) && b >= 0x40 && b <= 0x5f;
  19864. if (!(case1 || case2)) {
  19865. return false;
  19866. }
  19867. var chNr = a <= 0x17 ? 1 : 2;
  19868. if (b >= 0x40 && b <= 0x5f) {
  19869. row = chNr === 1 ? rowsLowCh1[a] : rowsLowCh2[a];
  19870. } else {
  19871. // 0x60 <= b <= 0x7F
  19872. row = chNr === 1 ? rowsHighCh1[a] : rowsHighCh2[a];
  19873. }
  19874. var channel = this.channels[chNr];
  19875. if (!channel) {
  19876. return false;
  19877. }
  19878. channel.setPAC(this.interpretPAC(row, b));
  19879. this.currentChannel = chNr;
  19880. return true;
  19881. }
  19882. /**
  19883. * Interpret the second byte of the pac, and return the information.
  19884. * @returns pacData with style parameters
  19885. */;
  19886. _proto7.interpretPAC = function interpretPAC(row, _byte3) {
  19887. var pacIndex;
  19888. var pacData = {
  19889. color: null,
  19890. italics: false,
  19891. indent: null,
  19892. underline: false,
  19893. row: row
  19894. };
  19895. if (_byte3 > 0x5f) {
  19896. pacIndex = _byte3 - 0x60;
  19897. } else {
  19898. pacIndex = _byte3 - 0x40;
  19899. }
  19900. pacData.underline = (pacIndex & 1) === 1;
  19901. if (pacIndex <= 0xd) {
  19902. pacData.color = ['white', 'green', 'blue', 'cyan', 'red', 'yellow', 'magenta', 'white'][Math.floor(pacIndex / 2)];
  19903. } else if (pacIndex <= 0xf) {
  19904. pacData.italics = true;
  19905. pacData.color = 'white';
  19906. } else {
  19907. pacData.indent = Math.floor((pacIndex - 0x10) / 2) * 4;
  19908. }
  19909. return pacData; // Note that row has zero offset. The spec uses 1.
  19910. }
  19911. /**
  19912. * Parse characters.
  19913. * @returns An array with 1 to 2 codes corresponding to chars, if found. null otherwise.
  19914. */;
  19915. _proto7.parseChars = function parseChars(a, b) {
  19916. var channelNr;
  19917. var charCodes = null;
  19918. var charCode1 = null;
  19919. if (a >= 0x19) {
  19920. channelNr = 2;
  19921. charCode1 = a - 8;
  19922. } else {
  19923. channelNr = 1;
  19924. charCode1 = a;
  19925. }
  19926. if (charCode1 >= 0x11 && charCode1 <= 0x13) {
  19927. // Special character
  19928. var oneCode;
  19929. if (charCode1 === 0x11) {
  19930. oneCode = b + 0x50;
  19931. } else if (charCode1 === 0x12) {
  19932. oneCode = b + 0x70;
  19933. } else {
  19934. oneCode = b + 0x90;
  19935. }
  19936. this.logger.log(2, function () {
  19937. return "Special char '" + getCharForByte(oneCode) + "' in channel " + channelNr;
  19938. });
  19939. charCodes = [oneCode];
  19940. } else if (a >= 0x20 && a <= 0x7f) {
  19941. charCodes = b === 0 ? [a] : [a, b];
  19942. }
  19943. if (charCodes) {
  19944. this.logger.log(3, function () {
  19945. return 'Char codes = ' + numArrayToHexArray(charCodes).join(',');
  19946. });
  19947. }
  19948. return charCodes;
  19949. }
  19950. /**
  19951. * Parse extended background attributes as well as new foreground color black.
  19952. * @returns True if background attributes are found
  19953. */;
  19954. _proto7.parseBackgroundAttributes = function parseBackgroundAttributes(a, b) {
  19955. var case1 = (a === 0x10 || a === 0x18) && b >= 0x20 && b <= 0x2f;
  19956. var case2 = (a === 0x17 || a === 0x1f) && b >= 0x2d && b <= 0x2f;
  19957. if (!(case1 || case2)) {
  19958. return false;
  19959. }
  19960. var index;
  19961. var bkgData = {};
  19962. if (a === 0x10 || a === 0x18) {
  19963. index = Math.floor((b - 0x20) / 2);
  19964. bkgData.background = backgroundColors[index];
  19965. if (b % 2 === 1) {
  19966. bkgData.background = bkgData.background + '_semi';
  19967. }
  19968. } else if (b === 0x2d) {
  19969. bkgData.background = 'transparent';
  19970. } else {
  19971. bkgData.foreground = 'black';
  19972. if (b === 0x2f) {
  19973. bkgData.underline = true;
  19974. }
  19975. }
  19976. var chNr = a <= 0x17 ? 1 : 2;
  19977. var channel = this.channels[chNr];
  19978. channel.setBkgData(bkgData);
  19979. return true;
  19980. }
  19981. /**
  19982. * Reset state of parser and its channels.
  19983. */;
  19984. _proto7.reset = function reset() {
  19985. for (var i = 0; i < Object.keys(this.channels).length; i++) {
  19986. var channel = this.channels[i];
  19987. if (channel) {
  19988. channel.reset();
  19989. }
  19990. }
  19991. setLastCmd(null, null, this.cmdHistory);
  19992. }
  19993. /**
  19994. * Trigger the generation of a cue, and the start of a new one if displayScreens are not empty.
  19995. */;
  19996. _proto7.cueSplitAtTime = function cueSplitAtTime(t) {
  19997. for (var i = 0; i < this.channels.length; i++) {
  19998. var channel = this.channels[i];
  19999. if (channel) {
  20000. channel.cueSplitAtTime(t);
  20001. }
  20002. }
  20003. };
  20004. return Cea608Parser;
  20005. }();
  20006. function setLastCmd(a, b, cmdHistory) {
  20007. cmdHistory.a = a;
  20008. cmdHistory.b = b;
  20009. }
  20010. function hasCmdRepeated(a, b, cmdHistory) {
  20011. return cmdHistory.a === a && cmdHistory.b === b;
  20012. }
  20013. function createCmdHistory() {
  20014. return {
  20015. a: null,
  20016. b: null
  20017. };
  20018. }
  20019. var OutputFilter = /*#__PURE__*/function () {
  20020. function OutputFilter(timelineController, trackName) {
  20021. this.timelineController = void 0;
  20022. this.cueRanges = [];
  20023. this.trackName = void 0;
  20024. this.startTime = null;
  20025. this.endTime = null;
  20026. this.screen = null;
  20027. this.timelineController = timelineController;
  20028. this.trackName = trackName;
  20029. }
  20030. var _proto = OutputFilter.prototype;
  20031. _proto.dispatchCue = function dispatchCue() {
  20032. if (this.startTime === null) {
  20033. return;
  20034. }
  20035. this.timelineController.addCues(this.trackName, this.startTime, this.endTime, this.screen, this.cueRanges);
  20036. this.startTime = null;
  20037. };
  20038. _proto.newCue = function newCue(startTime, endTime, screen) {
  20039. if (this.startTime === null || this.startTime > startTime) {
  20040. this.startTime = startTime;
  20041. }
  20042. this.endTime = endTime;
  20043. this.screen = screen;
  20044. this.timelineController.createCaptionsTrack(this.trackName);
  20045. };
  20046. _proto.reset = function reset() {
  20047. this.cueRanges = [];
  20048. this.startTime = null;
  20049. };
  20050. return OutputFilter;
  20051. }();
  20052. /**
  20053. * Copyright 2013 vtt.js Contributors
  20054. *
  20055. * Licensed under the Apache License, Version 2.0 (the 'License');
  20056. * you may not use this file except in compliance with the License.
  20057. * You may obtain a copy of the License at
  20058. *
  20059. * http://www.apache.org/licenses/LICENSE-2.0
  20060. *
  20061. * Unless required by applicable law or agreed to in writing, software
  20062. * distributed under the License is distributed on an 'AS IS' BASIS,
  20063. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20064. * See the License for the specific language governing permissions and
  20065. * limitations under the License.
  20066. */
  20067. var VTTCue = (function () {
  20068. if (optionalSelf != null && optionalSelf.VTTCue) {
  20069. return self.VTTCue;
  20070. }
  20071. var AllowedDirections = ['', 'lr', 'rl'];
  20072. var AllowedAlignments = ['start', 'middle', 'end', 'left', 'right'];
  20073. function isAllowedValue(allowed, value) {
  20074. if (typeof value !== 'string') {
  20075. return false;
  20076. }
  20077. // necessary for assuring the generic conforms to the Array interface
  20078. if (!Array.isArray(allowed)) {
  20079. return false;
  20080. }
  20081. // reset the type so that the next narrowing works well
  20082. var lcValue = value.toLowerCase();
  20083. // use the allow list to narrow the type to a specific subset of strings
  20084. if (~allowed.indexOf(lcValue)) {
  20085. return lcValue;
  20086. }
  20087. return false;
  20088. }
  20089. function findDirectionSetting(value) {
  20090. return isAllowedValue(AllowedDirections, value);
  20091. }
  20092. function findAlignSetting(value) {
  20093. return isAllowedValue(AllowedAlignments, value);
  20094. }
  20095. function extend(obj) {
  20096. for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  20097. rest[_key - 1] = arguments[_key];
  20098. }
  20099. var i = 1;
  20100. for (; i < arguments.length; i++) {
  20101. var cobj = arguments[i];
  20102. for (var p in cobj) {
  20103. obj[p] = cobj[p];
  20104. }
  20105. }
  20106. return obj;
  20107. }
  20108. function VTTCue(startTime, endTime, text) {
  20109. var cue = this;
  20110. var baseObj = {
  20111. enumerable: true
  20112. };
  20113. /**
  20114. * Shim implementation specific properties. These properties are not in
  20115. * the spec.
  20116. */
  20117. // Lets us know when the VTTCue's data has changed in such a way that we need
  20118. // to recompute its display state. This lets us compute its display state
  20119. // lazily.
  20120. cue.hasBeenReset = false;
  20121. /**
  20122. * VTTCue and TextTrackCue properties
  20123. * http://dev.w3.org/html5/webvtt/#vttcue-interface
  20124. */
  20125. var _id = '';
  20126. var _pauseOnExit = false;
  20127. var _startTime = startTime;
  20128. var _endTime = endTime;
  20129. var _text = text;
  20130. var _region = null;
  20131. var _vertical = '';
  20132. var _snapToLines = true;
  20133. var _line = 'auto';
  20134. var _lineAlign = 'start';
  20135. var _position = 50;
  20136. var _positionAlign = 'middle';
  20137. var _size = 50;
  20138. var _align = 'middle';
  20139. Object.defineProperty(cue, 'id', extend({}, baseObj, {
  20140. get: function get() {
  20141. return _id;
  20142. },
  20143. set: function set(value) {
  20144. _id = '' + value;
  20145. }
  20146. }));
  20147. Object.defineProperty(cue, 'pauseOnExit', extend({}, baseObj, {
  20148. get: function get() {
  20149. return _pauseOnExit;
  20150. },
  20151. set: function set(value) {
  20152. _pauseOnExit = !!value;
  20153. }
  20154. }));
  20155. Object.defineProperty(cue, 'startTime', extend({}, baseObj, {
  20156. get: function get() {
  20157. return _startTime;
  20158. },
  20159. set: function set(value) {
  20160. if (typeof value !== 'number') {
  20161. throw new TypeError('Start time must be set to a number.');
  20162. }
  20163. _startTime = value;
  20164. this.hasBeenReset = true;
  20165. }
  20166. }));
  20167. Object.defineProperty(cue, 'endTime', extend({}, baseObj, {
  20168. get: function get() {
  20169. return _endTime;
  20170. },
  20171. set: function set(value) {
  20172. if (typeof value !== 'number') {
  20173. throw new TypeError('End time must be set to a number.');
  20174. }
  20175. _endTime = value;
  20176. this.hasBeenReset = true;
  20177. }
  20178. }));
  20179. Object.defineProperty(cue, 'text', extend({}, baseObj, {
  20180. get: function get() {
  20181. return _text;
  20182. },
  20183. set: function set(value) {
  20184. _text = '' + value;
  20185. this.hasBeenReset = true;
  20186. }
  20187. }));
  20188. // todo: implement VTTRegion polyfill?
  20189. Object.defineProperty(cue, 'region', extend({}, baseObj, {
  20190. get: function get() {
  20191. return _region;
  20192. },
  20193. set: function set(value) {
  20194. _region = value;
  20195. this.hasBeenReset = true;
  20196. }
  20197. }));
  20198. Object.defineProperty(cue, 'vertical', extend({}, baseObj, {
  20199. get: function get() {
  20200. return _vertical;
  20201. },
  20202. set: function set(value) {
  20203. var setting = findDirectionSetting(value);
  20204. // Have to check for false because the setting an be an empty string.
  20205. if (setting === false) {
  20206. throw new SyntaxError('An invalid or illegal string was specified.');
  20207. }
  20208. _vertical = setting;
  20209. this.hasBeenReset = true;
  20210. }
  20211. }));
  20212. Object.defineProperty(cue, 'snapToLines', extend({}, baseObj, {
  20213. get: function get() {
  20214. return _snapToLines;
  20215. },
  20216. set: function set(value) {
  20217. _snapToLines = !!value;
  20218. this.hasBeenReset = true;
  20219. }
  20220. }));
  20221. Object.defineProperty(cue, 'line', extend({}, baseObj, {
  20222. get: function get() {
  20223. return _line;
  20224. },
  20225. set: function set(value) {
  20226. if (typeof value !== 'number' && value !== 'auto') {
  20227. throw new SyntaxError('An invalid number or illegal string was specified.');
  20228. }
  20229. _line = value;
  20230. this.hasBeenReset = true;
  20231. }
  20232. }));
  20233. Object.defineProperty(cue, 'lineAlign', extend({}, baseObj, {
  20234. get: function get() {
  20235. return _lineAlign;
  20236. },
  20237. set: function set(value) {
  20238. var setting = findAlignSetting(value);
  20239. if (!setting) {
  20240. throw new SyntaxError('An invalid or illegal string was specified.');
  20241. }
  20242. _lineAlign = setting;
  20243. this.hasBeenReset = true;
  20244. }
  20245. }));
  20246. Object.defineProperty(cue, 'position', extend({}, baseObj, {
  20247. get: function get() {
  20248. return _position;
  20249. },
  20250. set: function set(value) {
  20251. if (value < 0 || value > 100) {
  20252. throw new Error('Position must be between 0 and 100.');
  20253. }
  20254. _position = value;
  20255. this.hasBeenReset = true;
  20256. }
  20257. }));
  20258. Object.defineProperty(cue, 'positionAlign', extend({}, baseObj, {
  20259. get: function get() {
  20260. return _positionAlign;
  20261. },
  20262. set: function set(value) {
  20263. var setting = findAlignSetting(value);
  20264. if (!setting) {
  20265. throw new SyntaxError('An invalid or illegal string was specified.');
  20266. }
  20267. _positionAlign = setting;
  20268. this.hasBeenReset = true;
  20269. }
  20270. }));
  20271. Object.defineProperty(cue, 'size', extend({}, baseObj, {
  20272. get: function get() {
  20273. return _size;
  20274. },
  20275. set: function set(value) {
  20276. if (value < 0 || value > 100) {
  20277. throw new Error('Size must be between 0 and 100.');
  20278. }
  20279. _size = value;
  20280. this.hasBeenReset = true;
  20281. }
  20282. }));
  20283. Object.defineProperty(cue, 'align', extend({}, baseObj, {
  20284. get: function get() {
  20285. return _align;
  20286. },
  20287. set: function set(value) {
  20288. var setting = findAlignSetting(value);
  20289. if (!setting) {
  20290. throw new SyntaxError('An invalid or illegal string was specified.');
  20291. }
  20292. _align = setting;
  20293. this.hasBeenReset = true;
  20294. }
  20295. }));
  20296. /**
  20297. * Other <track> spec defined properties
  20298. */
  20299. // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-video-element.html#text-track-cue-display-state
  20300. cue.displayState = undefined;
  20301. }
  20302. /**
  20303. * VTTCue methods
  20304. */
  20305. VTTCue.prototype.getCueAsHTML = function () {
  20306. // Assume WebVTT.convertCueToDOMTree is on the global.
  20307. var WebVTT = self.WebVTT;
  20308. return WebVTT.convertCueToDOMTree(self, this.text);
  20309. };
  20310. // this is a polyfill hack
  20311. return VTTCue;
  20312. })();
  20313. /*
  20314. * Source: https://github.com/mozilla/vtt.js/blob/master/dist/vtt.js
  20315. */
  20316. var StringDecoder = /*#__PURE__*/function () {
  20317. function StringDecoder() {}
  20318. var _proto = StringDecoder.prototype;
  20319. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  20320. _proto.decode = function decode(data, options) {
  20321. if (!data) {
  20322. return '';
  20323. }
  20324. if (typeof data !== 'string') {
  20325. throw new Error('Error - expected string data.');
  20326. }
  20327. return decodeURIComponent(encodeURIComponent(data));
  20328. };
  20329. return StringDecoder;
  20330. }(); // Try to parse input as a time stamp.
  20331. function parseTimeStamp(input) {
  20332. function computeSeconds(h, m, s, f) {
  20333. return (h | 0) * 3600 + (m | 0) * 60 + (s | 0) + parseFloat(f || 0);
  20334. }
  20335. var m = input.match(/^(?:(\d+):)?(\d{2}):(\d{2})(\.\d+)?/);
  20336. if (!m) {
  20337. return null;
  20338. }
  20339. if (parseFloat(m[2]) > 59) {
  20340. // Timestamp takes the form of [hours]:[minutes].[milliseconds]
  20341. // First position is hours as it's over 59.
  20342. return computeSeconds(m[2], m[3], 0, m[4]);
  20343. }
  20344. // Timestamp takes the form of [hours (optional)]:[minutes]:[seconds].[milliseconds]
  20345. return computeSeconds(m[1], m[2], m[3], m[4]);
  20346. }
  20347. // A settings object holds key/value pairs and will ignore anything but the first
  20348. // assignment to a specific key.
  20349. var Settings = /*#__PURE__*/function () {
  20350. function Settings() {
  20351. this.values = Object.create(null);
  20352. }
  20353. var _proto2 = Settings.prototype;
  20354. // Only accept the first assignment to any key.
  20355. _proto2.set = function set(k, v) {
  20356. if (!this.get(k) && v !== '') {
  20357. this.values[k] = v;
  20358. }
  20359. }
  20360. // Return the value for a key, or a default value.
  20361. // If 'defaultKey' is passed then 'dflt' is assumed to be an object with
  20362. // a number of possible default values as properties where 'defaultKey' is
  20363. // the key of the property that will be chosen; otherwise it's assumed to be
  20364. // a single value.
  20365. ;
  20366. _proto2.get = function get(k, dflt, defaultKey) {
  20367. if (defaultKey) {
  20368. return this.has(k) ? this.values[k] : dflt[defaultKey];
  20369. }
  20370. return this.has(k) ? this.values[k] : dflt;
  20371. }
  20372. // Check whether we have a value for a key.
  20373. ;
  20374. _proto2.has = function has(k) {
  20375. return k in this.values;
  20376. }
  20377. // Accept a setting if its one of the given alternatives.
  20378. ;
  20379. _proto2.alt = function alt(k, v, a) {
  20380. for (var n = 0; n < a.length; ++n) {
  20381. if (v === a[n]) {
  20382. this.set(k, v);
  20383. break;
  20384. }
  20385. }
  20386. }
  20387. // Accept a setting if its a valid (signed) integer.
  20388. ;
  20389. _proto2.integer = function integer(k, v) {
  20390. if (/^-?\d+$/.test(v)) {
  20391. // integer
  20392. this.set(k, parseInt(v, 10));
  20393. }
  20394. }
  20395. // Accept a setting if its a valid percentage.
  20396. ;
  20397. _proto2.percent = function percent(k, v) {
  20398. if (/^([\d]{1,3})(\.[\d]*)?%$/.test(v)) {
  20399. var percent = parseFloat(v);
  20400. if (percent >= 0 && percent <= 100) {
  20401. this.set(k, percent);
  20402. return true;
  20403. }
  20404. }
  20405. return false;
  20406. };
  20407. return Settings;
  20408. }(); // Helper function to parse input into groups separated by 'groupDelim', and
  20409. // interpret each group as a key/value pair separated by 'keyValueDelim'.
  20410. function parseOptions(input, callback, keyValueDelim, groupDelim) {
  20411. var groups = groupDelim ? input.split(groupDelim) : [input];
  20412. for (var i in groups) {
  20413. if (typeof groups[i] !== 'string') {
  20414. continue;
  20415. }
  20416. var kv = groups[i].split(keyValueDelim);
  20417. if (kv.length !== 2) {
  20418. continue;
  20419. }
  20420. var _k = kv[0];
  20421. var _v = kv[1];
  20422. callback(_k, _v);
  20423. }
  20424. }
  20425. var defaults = new VTTCue(0, 0, '');
  20426. // 'middle' was changed to 'center' in the spec: https://github.com/w3c/webvtt/pull/244
  20427. // Safari doesn't yet support this change, but FF and Chrome do.
  20428. var center = defaults.align === 'middle' ? 'middle' : 'center';
  20429. function parseCue(input, cue, regionList) {
  20430. // Remember the original input if we need to throw an error.
  20431. var oInput = input;
  20432. // 4.1 WebVTT timestamp
  20433. function consumeTimeStamp() {
  20434. var ts = parseTimeStamp(input);
  20435. if (ts === null) {
  20436. throw new Error('Malformed timestamp: ' + oInput);
  20437. }
  20438. // Remove time stamp from input.
  20439. input = input.replace(/^[^\sa-zA-Z-]+/, '');
  20440. return ts;
  20441. }
  20442. // 4.4.2 WebVTT cue settings
  20443. function consumeCueSettings(input, cue) {
  20444. var settings = new Settings();
  20445. parseOptions(input, function (k, v) {
  20446. var vals;
  20447. switch (k) {
  20448. case 'region':
  20449. // Find the last region we parsed with the same region id.
  20450. for (var i = regionList.length - 1; i >= 0; i--) {
  20451. if (regionList[i].id === v) {
  20452. settings.set(k, regionList[i].region);
  20453. break;
  20454. }
  20455. }
  20456. break;
  20457. case 'vertical':
  20458. settings.alt(k, v, ['rl', 'lr']);
  20459. break;
  20460. case 'line':
  20461. vals = v.split(',');
  20462. settings.integer(k, vals[0]);
  20463. if (settings.percent(k, vals[0])) {
  20464. settings.set('snapToLines', false);
  20465. }
  20466. settings.alt(k, vals[0], ['auto']);
  20467. if (vals.length === 2) {
  20468. settings.alt('lineAlign', vals[1], ['start', center, 'end']);
  20469. }
  20470. break;
  20471. case 'position':
  20472. vals = v.split(',');
  20473. settings.percent(k, vals[0]);
  20474. if (vals.length === 2) {
  20475. settings.alt('positionAlign', vals[1], ['start', center, 'end', 'line-left', 'line-right', 'auto']);
  20476. }
  20477. break;
  20478. case 'size':
  20479. settings.percent(k, v);
  20480. break;
  20481. case 'align':
  20482. settings.alt(k, v, ['start', center, 'end', 'left', 'right']);
  20483. break;
  20484. }
  20485. }, /:/, /\s/);
  20486. // Apply default values for any missing fields.
  20487. cue.region = settings.get('region', null);
  20488. cue.vertical = settings.get('vertical', '');
  20489. var line = settings.get('line', 'auto');
  20490. if (line === 'auto' && defaults.line === -1) {
  20491. // set numeric line number for Safari
  20492. line = -1;
  20493. }
  20494. cue.line = line;
  20495. cue.lineAlign = settings.get('lineAlign', 'start');
  20496. cue.snapToLines = settings.get('snapToLines', true);
  20497. cue.size = settings.get('size', 100);
  20498. cue.align = settings.get('align', center);
  20499. var position = settings.get('position', 'auto');
  20500. if (position === 'auto' && defaults.position === 50) {
  20501. // set numeric position for Safari
  20502. position = cue.align === 'start' || cue.align === 'left' ? 0 : cue.align === 'end' || cue.align === 'right' ? 100 : 50;
  20503. }
  20504. cue.position = position;
  20505. }
  20506. function skipWhitespace() {
  20507. input = input.replace(/^\s+/, '');
  20508. }
  20509. // 4.1 WebVTT cue timings.
  20510. skipWhitespace();
  20511. cue.startTime = consumeTimeStamp(); // (1) collect cue start time
  20512. skipWhitespace();
  20513. if (input.slice(0, 3) !== '-->') {
  20514. // (3) next characters must match '-->'
  20515. throw new Error("Malformed time stamp (time stamps must be separated by '-->'): " + oInput);
  20516. }
  20517. input = input.slice(3);
  20518. skipWhitespace();
  20519. cue.endTime = consumeTimeStamp(); // (5) collect cue end time
  20520. // 4.1 WebVTT cue settings list.
  20521. skipWhitespace();
  20522. consumeCueSettings(input, cue);
  20523. }
  20524. function fixLineBreaks(input) {
  20525. return input.replace(/<br(?: \/)?>/gi, '\n');
  20526. }
  20527. var VTTParser = /*#__PURE__*/function () {
  20528. function VTTParser() {
  20529. this.state = 'INITIAL';
  20530. this.buffer = '';
  20531. this.decoder = new StringDecoder();
  20532. this.regionList = [];
  20533. this.cue = null;
  20534. this.oncue = void 0;
  20535. this.onparsingerror = void 0;
  20536. this.onflush = void 0;
  20537. }
  20538. var _proto3 = VTTParser.prototype;
  20539. _proto3.parse = function parse(data) {
  20540. var _this = this;
  20541. // If there is no data then we won't decode it, but will just try to parse
  20542. // whatever is in buffer already. This may occur in circumstances, for
  20543. // example when flush() is called.
  20544. if (data) {
  20545. // Try to decode the data that we received.
  20546. _this.buffer += _this.decoder.decode(data, {
  20547. stream: true
  20548. });
  20549. }
  20550. function collectNextLine() {
  20551. var buffer = _this.buffer;
  20552. var pos = 0;
  20553. buffer = fixLineBreaks(buffer);
  20554. while (pos < buffer.length && buffer[pos] !== '\r' && buffer[pos] !== '\n') {
  20555. ++pos;
  20556. }
  20557. var line = buffer.slice(0, pos);
  20558. // Advance the buffer early in case we fail below.
  20559. if (buffer[pos] === '\r') {
  20560. ++pos;
  20561. }
  20562. if (buffer[pos] === '\n') {
  20563. ++pos;
  20564. }
  20565. _this.buffer = buffer.slice(pos);
  20566. return line;
  20567. }
  20568. // 3.2 WebVTT metadata header syntax
  20569. function parseHeader(input) {
  20570. parseOptions(input, function (k, v) {
  20571. // switch (k) {
  20572. // case 'region':
  20573. // 3.3 WebVTT region metadata header syntax
  20574. // console.log('parse region', v);
  20575. // parseRegion(v);
  20576. // break;
  20577. // }
  20578. }, /:/);
  20579. }
  20580. // 5.1 WebVTT file parsing.
  20581. try {
  20582. var line = '';
  20583. if (_this.state === 'INITIAL') {
  20584. // We can't start parsing until we have the first line.
  20585. if (!/\r\n|\n/.test(_this.buffer)) {
  20586. return this;
  20587. }
  20588. line = collectNextLine();
  20589. // strip of UTF-8 BOM if any
  20590. // https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
  20591. var m = line.match(/^()?WEBVTT([ \t].*)?$/);
  20592. if (!(m != null && m[0])) {
  20593. throw new Error('Malformed WebVTT signature.');
  20594. }
  20595. _this.state = 'HEADER';
  20596. }
  20597. var alreadyCollectedLine = false;
  20598. while (_this.buffer) {
  20599. // We can't parse a line until we have the full line.
  20600. if (!/\r\n|\n/.test(_this.buffer)) {
  20601. return this;
  20602. }
  20603. if (!alreadyCollectedLine) {
  20604. line = collectNextLine();
  20605. } else {
  20606. alreadyCollectedLine = false;
  20607. }
  20608. switch (_this.state) {
  20609. case 'HEADER':
  20610. // 13-18 - Allow a header (metadata) under the WEBVTT line.
  20611. if (/:/.test(line)) {
  20612. parseHeader(line);
  20613. } else if (!line) {
  20614. // An empty line terminates the header and starts the body (cues).
  20615. _this.state = 'ID';
  20616. }
  20617. continue;
  20618. case 'NOTE':
  20619. // Ignore NOTE blocks.
  20620. if (!line) {
  20621. _this.state = 'ID';
  20622. }
  20623. continue;
  20624. case 'ID':
  20625. // Check for the start of NOTE blocks.
  20626. if (/^NOTE($|[ \t])/.test(line)) {
  20627. _this.state = 'NOTE';
  20628. break;
  20629. }
  20630. // 19-29 - Allow any number of line terminators, then initialize new cue values.
  20631. if (!line) {
  20632. continue;
  20633. }
  20634. _this.cue = new VTTCue(0, 0, '');
  20635. _this.state = 'CUE';
  20636. // 30-39 - Check if self line contains an optional identifier or timing data.
  20637. if (line.indexOf('-->') === -1) {
  20638. _this.cue.id = line;
  20639. continue;
  20640. }
  20641. // Process line as start of a cue.
  20642. /* falls through */
  20643. case 'CUE':
  20644. // 40 - Collect cue timings and settings.
  20645. if (!_this.cue) {
  20646. _this.state = 'BADCUE';
  20647. continue;
  20648. }
  20649. try {
  20650. parseCue(line, _this.cue, _this.regionList);
  20651. } catch (e) {
  20652. // In case of an error ignore rest of the cue.
  20653. _this.cue = null;
  20654. _this.state = 'BADCUE';
  20655. continue;
  20656. }
  20657. _this.state = 'CUETEXT';
  20658. continue;
  20659. case 'CUETEXT':
  20660. {
  20661. var hasSubstring = line.indexOf('-->') !== -1;
  20662. // 34 - If we have an empty line then report the cue.
  20663. // 35 - If we have the special substring '-->' then report the cue,
  20664. // but do not collect the line as we need to process the current
  20665. // one as a new cue.
  20666. if (!line || hasSubstring && (alreadyCollectedLine = true)) {
  20667. // We are done parsing self cue.
  20668. if (_this.oncue && _this.cue) {
  20669. _this.oncue(_this.cue);
  20670. }
  20671. _this.cue = null;
  20672. _this.state = 'ID';
  20673. continue;
  20674. }
  20675. if (_this.cue === null) {
  20676. continue;
  20677. }
  20678. if (_this.cue.text) {
  20679. _this.cue.text += '\n';
  20680. }
  20681. _this.cue.text += line;
  20682. }
  20683. continue;
  20684. case 'BADCUE':
  20685. // 54-62 - Collect and discard the remaining cue.
  20686. if (!line) {
  20687. _this.state = 'ID';
  20688. }
  20689. }
  20690. }
  20691. } catch (e) {
  20692. // If we are currently parsing a cue, report what we have.
  20693. if (_this.state === 'CUETEXT' && _this.cue && _this.oncue) {
  20694. _this.oncue(_this.cue);
  20695. }
  20696. _this.cue = null;
  20697. // Enter BADWEBVTT state if header was not parsed correctly otherwise
  20698. // another exception occurred so enter BADCUE state.
  20699. _this.state = _this.state === 'INITIAL' ? 'BADWEBVTT' : 'BADCUE';
  20700. }
  20701. return this;
  20702. };
  20703. _proto3.flush = function flush() {
  20704. var _this = this;
  20705. try {
  20706. // Finish decoding the stream.
  20707. // _this.buffer += _this.decoder.decode();
  20708. // Synthesize the end of the current cue or region.
  20709. if (_this.cue || _this.state === 'HEADER') {
  20710. _this.buffer += '\n\n';
  20711. _this.parse();
  20712. }
  20713. // If we've flushed, parsed, and we're still on the INITIAL state then
  20714. // that means we don't have enough of the stream to parse the first
  20715. // line.
  20716. if (_this.state === 'INITIAL' || _this.state === 'BADWEBVTT') {
  20717. throw new Error('Malformed WebVTT signature.');
  20718. }
  20719. } catch (e) {
  20720. if (_this.onparsingerror) {
  20721. _this.onparsingerror(e);
  20722. }
  20723. }
  20724. if (_this.onflush) {
  20725. _this.onflush();
  20726. }
  20727. return this;
  20728. };
  20729. return VTTParser;
  20730. }();
  20731. var LINEBREAKS = /\r\n|\n\r|\n|\r/g;
  20732. // String.prototype.startsWith is not supported in IE11
  20733. var startsWith = function startsWith(inputString, searchString, position) {
  20734. if (position === void 0) {
  20735. position = 0;
  20736. }
  20737. return inputString.slice(position, position + searchString.length) === searchString;
  20738. };
  20739. var cueString2millis = function cueString2millis(timeString) {
  20740. var ts = parseInt(timeString.slice(-3));
  20741. var secs = parseInt(timeString.slice(-6, -4));
  20742. var mins = parseInt(timeString.slice(-9, -7));
  20743. var hours = timeString.length > 9 ? parseInt(timeString.substring(0, timeString.indexOf(':'))) : 0;
  20744. if (!isFiniteNumber(ts) || !isFiniteNumber(secs) || !isFiniteNumber(mins) || !isFiniteNumber(hours)) {
  20745. throw Error("Malformed X-TIMESTAMP-MAP: Local:" + timeString);
  20746. }
  20747. ts += 1000 * secs;
  20748. ts += 60 * 1000 * mins;
  20749. ts += 60 * 60 * 1000 * hours;
  20750. return ts;
  20751. };
  20752. // From https://github.com/darkskyapp/string-hash
  20753. var hash = function hash(text) {
  20754. var hash = 5381;
  20755. var i = text.length;
  20756. while (i) {
  20757. hash = hash * 33 ^ text.charCodeAt(--i);
  20758. }
  20759. return (hash >>> 0).toString();
  20760. };
  20761. // Create a unique hash id for a cue based on start/end times and text.
  20762. // This helps timeline-controller to avoid showing repeated captions.
  20763. function generateCueId(startTime, endTime, text) {
  20764. return hash(startTime.toString()) + hash(endTime.toString()) + hash(text);
  20765. }
  20766. var calculateOffset = function calculateOffset(vttCCs, cc, presentationTime) {
  20767. var currCC = vttCCs[cc];
  20768. var prevCC = vttCCs[currCC.prevCC];
  20769. // This is the first discontinuity or cues have been processed since the last discontinuity
  20770. // Offset = current discontinuity time
  20771. if (!prevCC || !prevCC.new && currCC.new) {
  20772. vttCCs.ccOffset = vttCCs.presentationOffset = currCC.start;
  20773. currCC.new = false;
  20774. return;
  20775. }
  20776. // There have been discontinuities since cues were last parsed.
  20777. // Offset = time elapsed
  20778. while ((_prevCC = prevCC) != null && _prevCC.new) {
  20779. var _prevCC;
  20780. vttCCs.ccOffset += currCC.start - prevCC.start;
  20781. currCC.new = false;
  20782. currCC = prevCC;
  20783. prevCC = vttCCs[currCC.prevCC];
  20784. }
  20785. vttCCs.presentationOffset = presentationTime;
  20786. };
  20787. function parseWebVTT(vttByteArray, initPTS, vttCCs, cc, timeOffset, callBack, errorCallBack) {
  20788. var parser = new VTTParser();
  20789. // Convert byteArray into string, replacing any somewhat exotic linefeeds with "\n", then split on that character.
  20790. // Uint8Array.prototype.reduce is not implemented in IE11
  20791. var vttLines = utf8ArrayToStr(new Uint8Array(vttByteArray)).trim().replace(LINEBREAKS, '\n').split('\n');
  20792. var cues = [];
  20793. var init90kHz = initPTS ? toMpegTsClockFromTimescale(initPTS.baseTime, initPTS.timescale) : 0;
  20794. var cueTime = '00:00.000';
  20795. var timestampMapMPEGTS = 0;
  20796. var timestampMapLOCAL = 0;
  20797. var parsingError;
  20798. var inHeader = true;
  20799. parser.oncue = function (cue) {
  20800. // Adjust cue timing; clamp cues to start no earlier than - and drop cues that don't end after - 0 on timeline.
  20801. var currCC = vttCCs[cc];
  20802. var cueOffset = vttCCs.ccOffset;
  20803. // Calculate subtitle PTS offset
  20804. var webVttMpegTsMapOffset = (timestampMapMPEGTS - init90kHz) / 90000;
  20805. // Update offsets for new discontinuities
  20806. if (currCC != null && currCC.new) {
  20807. if (timestampMapLOCAL !== undefined) {
  20808. // When local time is provided, offset = discontinuity start time - local time
  20809. cueOffset = vttCCs.ccOffset = currCC.start;
  20810. } else {
  20811. calculateOffset(vttCCs, cc, webVttMpegTsMapOffset);
  20812. }
  20813. }
  20814. if (webVttMpegTsMapOffset) {
  20815. if (!initPTS) {
  20816. parsingError = new Error('Missing initPTS for VTT MPEGTS');
  20817. return;
  20818. }
  20819. // If we have MPEGTS, offset = presentation time + discontinuity offset
  20820. cueOffset = webVttMpegTsMapOffset - vttCCs.presentationOffset;
  20821. }
  20822. var duration = cue.endTime - cue.startTime;
  20823. var startTime = normalizePts((cue.startTime + cueOffset - timestampMapLOCAL) * 90000, timeOffset * 90000) / 90000;
  20824. cue.startTime = Math.max(startTime, 0);
  20825. cue.endTime = Math.max(startTime + duration, 0);
  20826. //trim trailing webvtt block whitespaces
  20827. var text = cue.text.trim();
  20828. // Fix encoding of special characters
  20829. cue.text = decodeURIComponent(encodeURIComponent(text));
  20830. // If the cue was not assigned an id from the VTT file (line above the content), create one.
  20831. if (!cue.id) {
  20832. cue.id = generateCueId(cue.startTime, cue.endTime, text);
  20833. }
  20834. if (cue.endTime > 0) {
  20835. cues.push(cue);
  20836. }
  20837. };
  20838. parser.onparsingerror = function (error) {
  20839. parsingError = error;
  20840. };
  20841. parser.onflush = function () {
  20842. if (parsingError) {
  20843. errorCallBack(parsingError);
  20844. return;
  20845. }
  20846. callBack(cues);
  20847. };
  20848. // Go through contents line by line.
  20849. vttLines.forEach(function (line) {
  20850. if (inHeader) {
  20851. // Look for X-TIMESTAMP-MAP in header.
  20852. if (startsWith(line, 'X-TIMESTAMP-MAP=')) {
  20853. // Once found, no more are allowed anyway, so stop searching.
  20854. inHeader = false;
  20855. // Extract LOCAL and MPEGTS.
  20856. line.slice(16).split(',').forEach(function (timestamp) {
  20857. if (startsWith(timestamp, 'LOCAL:')) {
  20858. cueTime = timestamp.slice(6);
  20859. } else if (startsWith(timestamp, 'MPEGTS:')) {
  20860. timestampMapMPEGTS = parseInt(timestamp.slice(7));
  20861. }
  20862. });
  20863. try {
  20864. // Convert cue time to seconds
  20865. timestampMapLOCAL = cueString2millis(cueTime) / 1000;
  20866. } catch (error) {
  20867. parsingError = error;
  20868. }
  20869. // Return without parsing X-TIMESTAMP-MAP line.
  20870. return;
  20871. } else if (line === '') {
  20872. inHeader = false;
  20873. }
  20874. }
  20875. // Parse line by default.
  20876. parser.parse(line + '\n');
  20877. });
  20878. parser.flush();
  20879. }
  20880. var IMSC1_CODEC = 'stpp.ttml.im1t';
  20881. // Time format: h:m:s:frames(.subframes)
  20882. var HMSF_REGEX = /^(\d{2,}):(\d{2}):(\d{2}):(\d{2})\.?(\d+)?$/;
  20883. // Time format: hours, minutes, seconds, milliseconds, frames, ticks
  20884. var TIME_UNIT_REGEX = /^(\d*(?:\.\d*)?)(h|m|s|ms|f|t)$/;
  20885. var textAlignToLineAlign = {
  20886. left: 'start',
  20887. center: 'center',
  20888. right: 'end',
  20889. start: 'start',
  20890. end: 'end'
  20891. };
  20892. function parseIMSC1(payload, initPTS, callBack, errorCallBack) {
  20893. var results = findBox(new Uint8Array(payload), ['mdat']);
  20894. if (results.length === 0) {
  20895. errorCallBack(new Error('Could not parse IMSC1 mdat'));
  20896. return;
  20897. }
  20898. var ttmlList = results.map(function (mdat) {
  20899. return utf8ArrayToStr(mdat);
  20900. });
  20901. var syncTime = toTimescaleFromScale(initPTS.baseTime, 1, initPTS.timescale);
  20902. try {
  20903. ttmlList.forEach(function (ttml) {
  20904. return callBack(parseTTML(ttml, syncTime));
  20905. });
  20906. } catch (error) {
  20907. errorCallBack(error);
  20908. }
  20909. }
  20910. function parseTTML(ttml, syncTime) {
  20911. var parser = new DOMParser();
  20912. var xmlDoc = parser.parseFromString(ttml, 'text/xml');
  20913. var tt = xmlDoc.getElementsByTagName('tt')[0];
  20914. if (!tt) {
  20915. throw new Error('Invalid ttml');
  20916. }
  20917. var defaultRateInfo = {
  20918. frameRate: 30,
  20919. subFrameRate: 1,
  20920. frameRateMultiplier: 0,
  20921. tickRate: 0
  20922. };
  20923. var rateInfo = Object.keys(defaultRateInfo).reduce(function (result, key) {
  20924. result[key] = tt.getAttribute("ttp:" + key) || defaultRateInfo[key];
  20925. return result;
  20926. }, {});
  20927. var trim = tt.getAttribute('xml:space') !== 'preserve';
  20928. var styleElements = collectionToDictionary(getElementCollection(tt, 'styling', 'style'));
  20929. var regionElements = collectionToDictionary(getElementCollection(tt, 'layout', 'region'));
  20930. var cueElements = getElementCollection(tt, 'body', '[begin]');
  20931. return [].map.call(cueElements, function (cueElement) {
  20932. var cueText = getTextContent(cueElement, trim);
  20933. if (!cueText || !cueElement.hasAttribute('begin')) {
  20934. return null;
  20935. }
  20936. var startTime = parseTtmlTime(cueElement.getAttribute('begin'), rateInfo);
  20937. var duration = parseTtmlTime(cueElement.getAttribute('dur'), rateInfo);
  20938. var endTime = parseTtmlTime(cueElement.getAttribute('end'), rateInfo);
  20939. if (startTime === null) {
  20940. throw timestampParsingError(cueElement);
  20941. }
  20942. if (endTime === null) {
  20943. if (duration === null) {
  20944. throw timestampParsingError(cueElement);
  20945. }
  20946. endTime = startTime + duration;
  20947. }
  20948. var cue = new VTTCue(startTime - syncTime, endTime - syncTime, cueText);
  20949. cue.id = generateCueId(cue.startTime, cue.endTime, cue.text);
  20950. var region = regionElements[cueElement.getAttribute('region')];
  20951. var style = styleElements[cueElement.getAttribute('style')];
  20952. // Apply styles to cue
  20953. var styles = getTtmlStyles(region, style, styleElements);
  20954. var textAlign = styles.textAlign;
  20955. if (textAlign) {
  20956. // cue.positionAlign not settable in FF~2016
  20957. var lineAlign = textAlignToLineAlign[textAlign];
  20958. if (lineAlign) {
  20959. cue.lineAlign = lineAlign;
  20960. }
  20961. cue.align = textAlign;
  20962. }
  20963. _extends(cue, styles);
  20964. return cue;
  20965. }).filter(function (cue) {
  20966. return cue !== null;
  20967. });
  20968. }
  20969. function getElementCollection(fromElement, parentName, childName) {
  20970. var parent = fromElement.getElementsByTagName(parentName)[0];
  20971. if (parent) {
  20972. return [].slice.call(parent.querySelectorAll(childName));
  20973. }
  20974. return [];
  20975. }
  20976. function collectionToDictionary(elementsWithId) {
  20977. return elementsWithId.reduce(function (dict, element) {
  20978. var id = element.getAttribute('xml:id');
  20979. if (id) {
  20980. dict[id] = element;
  20981. }
  20982. return dict;
  20983. }, {});
  20984. }
  20985. function getTextContent(element, trim) {
  20986. return [].slice.call(element.childNodes).reduce(function (str, node, i) {
  20987. var _node$childNodes;
  20988. if (node.nodeName === 'br' && i) {
  20989. return str + '\n';
  20990. }
  20991. if ((_node$childNodes = node.childNodes) != null && _node$childNodes.length) {
  20992. return getTextContent(node, trim);
  20993. } else if (trim) {
  20994. return str + node.textContent.trim().replace(/\s+/g, ' ');
  20995. }
  20996. return str + node.textContent;
  20997. }, '');
  20998. }
  20999. function getTtmlStyles(region, style, styleElements) {
  21000. var ttsNs = 'http://www.w3.org/ns/ttml#styling';
  21001. var regionStyle = null;
  21002. var styleAttributes = ['displayAlign', 'textAlign', 'color', 'backgroundColor', 'fontSize', 'fontFamily'
  21003. // 'fontWeight',
  21004. // 'lineHeight',
  21005. // 'wrapOption',
  21006. // 'fontStyle',
  21007. // 'direction',
  21008. // 'writingMode'
  21009. ];
  21010. var regionStyleName = region != null && region.hasAttribute('style') ? region.getAttribute('style') : null;
  21011. if (regionStyleName && styleElements.hasOwnProperty(regionStyleName)) {
  21012. regionStyle = styleElements[regionStyleName];
  21013. }
  21014. return styleAttributes.reduce(function (styles, name) {
  21015. var value = getAttributeNS(style, ttsNs, name) || getAttributeNS(region, ttsNs, name) || getAttributeNS(regionStyle, ttsNs, name);
  21016. if (value) {
  21017. styles[name] = value;
  21018. }
  21019. return styles;
  21020. }, {});
  21021. }
  21022. function getAttributeNS(element, ns, name) {
  21023. if (!element) {
  21024. return null;
  21025. }
  21026. return element.hasAttributeNS(ns, name) ? element.getAttributeNS(ns, name) : null;
  21027. }
  21028. function timestampParsingError(node) {
  21029. return new Error("Could not parse ttml timestamp " + node);
  21030. }
  21031. function parseTtmlTime(timeAttributeValue, rateInfo) {
  21032. if (!timeAttributeValue) {
  21033. return null;
  21034. }
  21035. var seconds = parseTimeStamp(timeAttributeValue);
  21036. if (seconds === null) {
  21037. if (HMSF_REGEX.test(timeAttributeValue)) {
  21038. seconds = parseHoursMinutesSecondsFrames(timeAttributeValue, rateInfo);
  21039. } else if (TIME_UNIT_REGEX.test(timeAttributeValue)) {
  21040. seconds = parseTimeUnits(timeAttributeValue, rateInfo);
  21041. }
  21042. }
  21043. return seconds;
  21044. }
  21045. function parseHoursMinutesSecondsFrames(timeAttributeValue, rateInfo) {
  21046. var m = HMSF_REGEX.exec(timeAttributeValue);
  21047. var frames = (m[4] | 0) + (m[5] | 0) / rateInfo.subFrameRate;
  21048. return (m[1] | 0) * 3600 + (m[2] | 0) * 60 + (m[3] | 0) + frames / rateInfo.frameRate;
  21049. }
  21050. function parseTimeUnits(timeAttributeValue, rateInfo) {
  21051. var m = TIME_UNIT_REGEX.exec(timeAttributeValue);
  21052. var value = Number(m[1]);
  21053. var unit = m[2];
  21054. switch (unit) {
  21055. case 'h':
  21056. return value * 3600;
  21057. case 'm':
  21058. return value * 60;
  21059. case 'ms':
  21060. return value * 1000;
  21061. case 'f':
  21062. return value / rateInfo.frameRate;
  21063. case 't':
  21064. return value / rateInfo.tickRate;
  21065. }
  21066. return value;
  21067. }
  21068. var TimelineController = /*#__PURE__*/function () {
  21069. function TimelineController(hls) {
  21070. this.hls = void 0;
  21071. this.media = null;
  21072. this.config = void 0;
  21073. this.enabled = true;
  21074. this.Cues = void 0;
  21075. this.textTracks = [];
  21076. this.tracks = [];
  21077. this.initPTS = [];
  21078. this.unparsedVttFrags = [];
  21079. this.captionsTracks = {};
  21080. this.nonNativeCaptionsTracks = {};
  21081. this.cea608Parser1 = void 0;
  21082. this.cea608Parser2 = void 0;
  21083. this.lastCc = -1;
  21084. // Last video (CEA-608) fragment CC
  21085. this.lastSn = -1;
  21086. // Last video (CEA-608) fragment MSN
  21087. this.lastPartIndex = -1;
  21088. // Last video (CEA-608) fragment Part Index
  21089. this.prevCC = -1;
  21090. // Last subtitle fragment CC
  21091. this.vttCCs = newVTTCCs();
  21092. this.captionsProperties = void 0;
  21093. this.hls = hls;
  21094. this.config = hls.config;
  21095. this.Cues = hls.config.cueHandler;
  21096. this.captionsProperties = {
  21097. textTrack1: {
  21098. label: this.config.captionsTextTrack1Label,
  21099. languageCode: this.config.captionsTextTrack1LanguageCode
  21100. },
  21101. textTrack2: {
  21102. label: this.config.captionsTextTrack2Label,
  21103. languageCode: this.config.captionsTextTrack2LanguageCode
  21104. },
  21105. textTrack3: {
  21106. label: this.config.captionsTextTrack3Label,
  21107. languageCode: this.config.captionsTextTrack3LanguageCode
  21108. },
  21109. textTrack4: {
  21110. label: this.config.captionsTextTrack4Label,
  21111. languageCode: this.config.captionsTextTrack4LanguageCode
  21112. }
  21113. };
  21114. hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21115. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  21116. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  21117. hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  21118. hls.on(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
  21119. hls.on(Events.FRAG_LOADING, this.onFragLoading, this);
  21120. hls.on(Events.FRAG_LOADED, this.onFragLoaded, this);
  21121. hls.on(Events.FRAG_PARSING_USERDATA, this.onFragParsingUserdata, this);
  21122. hls.on(Events.FRAG_DECRYPTED, this.onFragDecrypted, this);
  21123. hls.on(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
  21124. hls.on(Events.SUBTITLE_TRACKS_CLEARED, this.onSubtitleTracksCleared, this);
  21125. hls.on(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  21126. }
  21127. var _proto = TimelineController.prototype;
  21128. _proto.destroy = function destroy() {
  21129. var hls = this.hls;
  21130. hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21131. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  21132. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  21133. hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  21134. hls.off(Events.SUBTITLE_TRACKS_UPDATED, this.onSubtitleTracksUpdated, this);
  21135. hls.off(Events.FRAG_LOADING, this.onFragLoading, this);
  21136. hls.off(Events.FRAG_LOADED, this.onFragLoaded, this);
  21137. hls.off(Events.FRAG_PARSING_USERDATA, this.onFragParsingUserdata, this);
  21138. hls.off(Events.FRAG_DECRYPTED, this.onFragDecrypted, this);
  21139. hls.off(Events.INIT_PTS_FOUND, this.onInitPtsFound, this);
  21140. hls.off(Events.SUBTITLE_TRACKS_CLEARED, this.onSubtitleTracksCleared, this);
  21141. hls.off(Events.BUFFER_FLUSHING, this.onBufferFlushing, this);
  21142. // @ts-ignore
  21143. this.hls = this.config = null;
  21144. this.cea608Parser1 = this.cea608Parser2 = undefined;
  21145. };
  21146. _proto.initCea608Parsers = function initCea608Parsers() {
  21147. if (this.config.enableCEA708Captions && (!this.cea608Parser1 || !this.cea608Parser2)) {
  21148. var channel1 = new OutputFilter(this, 'textTrack1');
  21149. var channel2 = new OutputFilter(this, 'textTrack2');
  21150. var channel3 = new OutputFilter(this, 'textTrack3');
  21151. var channel4 = new OutputFilter(this, 'textTrack4');
  21152. this.cea608Parser1 = new Cea608Parser(1, channel1, channel2);
  21153. this.cea608Parser2 = new Cea608Parser(3, channel3, channel4);
  21154. }
  21155. };
  21156. _proto.addCues = function addCues(trackName, startTime, endTime, screen, cueRanges) {
  21157. // skip cues which overlap more than 50% with previously parsed time ranges
  21158. var merged = false;
  21159. for (var i = cueRanges.length; i--;) {
  21160. var cueRange = cueRanges[i];
  21161. var overlap = intersection(cueRange[0], cueRange[1], startTime, endTime);
  21162. if (overlap >= 0) {
  21163. cueRange[0] = Math.min(cueRange[0], startTime);
  21164. cueRange[1] = Math.max(cueRange[1], endTime);
  21165. merged = true;
  21166. if (overlap / (endTime - startTime) > 0.5) {
  21167. return;
  21168. }
  21169. }
  21170. }
  21171. if (!merged) {
  21172. cueRanges.push([startTime, endTime]);
  21173. }
  21174. if (this.config.renderTextTracksNatively) {
  21175. var track = this.captionsTracks[trackName];
  21176. this.Cues.newCue(track, startTime, endTime, screen);
  21177. } else {
  21178. var cues = this.Cues.newCue(null, startTime, endTime, screen);
  21179. this.hls.trigger(Events.CUES_PARSED, {
  21180. type: 'captions',
  21181. cues: cues,
  21182. track: trackName
  21183. });
  21184. }
  21185. }
  21186. // Triggered when an initial PTS is found; used for synchronisation of WebVTT.
  21187. ;
  21188. _proto.onInitPtsFound = function onInitPtsFound(event, _ref) {
  21189. var _this = this;
  21190. var frag = _ref.frag,
  21191. id = _ref.id,
  21192. initPTS = _ref.initPTS,
  21193. timescale = _ref.timescale;
  21194. var unparsedVttFrags = this.unparsedVttFrags;
  21195. if (id === 'main') {
  21196. this.initPTS[frag.cc] = {
  21197. baseTime: initPTS,
  21198. timescale: timescale
  21199. };
  21200. }
  21201. // Due to asynchronous processing, initial PTS may arrive later than the first VTT fragments are loaded.
  21202. // Parse any unparsed fragments upon receiving the initial PTS.
  21203. if (unparsedVttFrags.length) {
  21204. this.unparsedVttFrags = [];
  21205. unparsedVttFrags.forEach(function (frag) {
  21206. _this.onFragLoaded(Events.FRAG_LOADED, frag);
  21207. });
  21208. }
  21209. };
  21210. _proto.getExistingTrack = function getExistingTrack(label, language) {
  21211. var media = this.media;
  21212. if (media) {
  21213. for (var i = 0; i < media.textTracks.length; i++) {
  21214. var textTrack = media.textTracks[i];
  21215. if (canReuseVttTextTrack(textTrack, {
  21216. name: label,
  21217. lang: language,
  21218. attrs: {}
  21219. })) {
  21220. return textTrack;
  21221. }
  21222. }
  21223. }
  21224. return null;
  21225. };
  21226. _proto.createCaptionsTrack = function createCaptionsTrack(trackName) {
  21227. if (this.config.renderTextTracksNatively) {
  21228. this.createNativeTrack(trackName);
  21229. } else {
  21230. this.createNonNativeTrack(trackName);
  21231. }
  21232. };
  21233. _proto.createNativeTrack = function createNativeTrack(trackName) {
  21234. if (this.captionsTracks[trackName]) {
  21235. return;
  21236. }
  21237. var captionsProperties = this.captionsProperties,
  21238. captionsTracks = this.captionsTracks,
  21239. media = this.media;
  21240. var _captionsProperties$t = captionsProperties[trackName],
  21241. label = _captionsProperties$t.label,
  21242. languageCode = _captionsProperties$t.languageCode;
  21243. // Enable reuse of existing text track.
  21244. var existingTrack = this.getExistingTrack(label, languageCode);
  21245. if (!existingTrack) {
  21246. var textTrack = this.createTextTrack('captions', label, languageCode);
  21247. if (textTrack) {
  21248. // Set a special property on the track so we know it's managed by Hls.js
  21249. textTrack[trackName] = true;
  21250. captionsTracks[trackName] = textTrack;
  21251. }
  21252. } else {
  21253. captionsTracks[trackName] = existingTrack;
  21254. clearCurrentCues(captionsTracks[trackName]);
  21255. sendAddTrackEvent(captionsTracks[trackName], media);
  21256. }
  21257. };
  21258. _proto.createNonNativeTrack = function createNonNativeTrack(trackName) {
  21259. if (this.nonNativeCaptionsTracks[trackName]) {
  21260. return;
  21261. }
  21262. // Create a list of a single track for the provider to consume
  21263. var trackProperties = this.captionsProperties[trackName];
  21264. if (!trackProperties) {
  21265. return;
  21266. }
  21267. var label = trackProperties.label;
  21268. var track = {
  21269. _id: trackName,
  21270. label: label,
  21271. kind: 'captions',
  21272. default: trackProperties.media ? !!trackProperties.media.default : false,
  21273. closedCaptions: trackProperties.media
  21274. };
  21275. this.nonNativeCaptionsTracks[trackName] = track;
  21276. this.hls.trigger(Events.NON_NATIVE_TEXT_TRACKS_FOUND, {
  21277. tracks: [track]
  21278. });
  21279. };
  21280. _proto.createTextTrack = function createTextTrack(kind, label, lang) {
  21281. var media = this.media;
  21282. if (!media) {
  21283. return;
  21284. }
  21285. return media.addTextTrack(kind, label, lang);
  21286. };
  21287. _proto.onMediaAttaching = function onMediaAttaching(event, data) {
  21288. this.media = data.media;
  21289. this._cleanTracks();
  21290. };
  21291. _proto.onMediaDetaching = function onMediaDetaching() {
  21292. var captionsTracks = this.captionsTracks;
  21293. Object.keys(captionsTracks).forEach(function (trackName) {
  21294. clearCurrentCues(captionsTracks[trackName]);
  21295. delete captionsTracks[trackName];
  21296. });
  21297. this.nonNativeCaptionsTracks = {};
  21298. };
  21299. _proto.onManifestLoading = function onManifestLoading() {
  21300. // Detect discontinuity in video fragment (CEA-608) parsing
  21301. this.lastCc = -1;
  21302. this.lastSn = -1;
  21303. this.lastPartIndex = -1;
  21304. // Detect discontinuity in subtitle manifests
  21305. this.prevCC = -1;
  21306. this.vttCCs = newVTTCCs();
  21307. // Reset tracks
  21308. this._cleanTracks();
  21309. this.tracks = [];
  21310. this.captionsTracks = {};
  21311. this.nonNativeCaptionsTracks = {};
  21312. this.textTracks = [];
  21313. this.unparsedVttFrags = [];
  21314. this.initPTS = [];
  21315. if (this.cea608Parser1 && this.cea608Parser2) {
  21316. this.cea608Parser1.reset();
  21317. this.cea608Parser2.reset();
  21318. }
  21319. };
  21320. _proto._cleanTracks = function _cleanTracks() {
  21321. // clear outdated subtitles
  21322. var media = this.media;
  21323. if (!media) {
  21324. return;
  21325. }
  21326. var textTracks = media.textTracks;
  21327. if (textTracks) {
  21328. for (var i = 0; i < textTracks.length; i++) {
  21329. clearCurrentCues(textTracks[i]);
  21330. }
  21331. }
  21332. };
  21333. _proto.onSubtitleTracksUpdated = function onSubtitleTracksUpdated(event, data) {
  21334. var _this2 = this;
  21335. var tracks = data.subtitleTracks || [];
  21336. var hasIMSC1 = tracks.some(function (track) {
  21337. return track.textCodec === IMSC1_CODEC;
  21338. });
  21339. if (this.config.enableWebVTT || hasIMSC1 && this.config.enableIMSC1) {
  21340. var listIsIdentical = subtitleOptionsIdentical(this.tracks, tracks);
  21341. if (listIsIdentical) {
  21342. this.tracks = tracks;
  21343. return;
  21344. }
  21345. this.textTracks = [];
  21346. this.tracks = tracks;
  21347. if (this.config.renderTextTracksNatively) {
  21348. var media = this.media;
  21349. var inUseTracks = media ? filterSubtitleTracks(media.textTracks) : null;
  21350. this.tracks.forEach(function (track, index) {
  21351. // Reuse tracks with the same label and lang, but do not reuse 608/708 tracks
  21352. var textTrack;
  21353. if (inUseTracks) {
  21354. var inUseTrack = null;
  21355. for (var i = 0; i < inUseTracks.length; i++) {
  21356. if (inUseTracks[i] && canReuseVttTextTrack(inUseTracks[i], track)) {
  21357. inUseTrack = inUseTracks[i];
  21358. inUseTracks[i] = null;
  21359. break;
  21360. }
  21361. }
  21362. if (inUseTrack) {
  21363. textTrack = inUseTrack;
  21364. }
  21365. }
  21366. if (textTrack) {
  21367. clearCurrentCues(textTrack);
  21368. } else {
  21369. var textTrackKind = captionsOrSubtitlesFromCharacteristics(track);
  21370. textTrack = _this2.createTextTrack(textTrackKind, track.name, track.lang);
  21371. if (textTrack) {
  21372. textTrack.mode = 'disabled';
  21373. }
  21374. }
  21375. if (textTrack) {
  21376. _this2.textTracks.push(textTrack);
  21377. }
  21378. });
  21379. // Warn when video element has captions or subtitle TextTracks carried over from another source
  21380. if (inUseTracks != null && inUseTracks.length) {
  21381. var unusedTextTracks = inUseTracks.filter(function (t) {
  21382. return t !== null;
  21383. }).map(function (t) {
  21384. return t.label;
  21385. });
  21386. if (unusedTextTracks.length) {
  21387. logger.warn("Media element contains unused subtitle tracks: " + unusedTextTracks.join(', ') + ". Replace media element for each source to clear TextTracks and captions menu.");
  21388. }
  21389. }
  21390. } else if (this.tracks.length) {
  21391. // Create a list of tracks for the provider to consume
  21392. var tracksList = this.tracks.map(function (track) {
  21393. return {
  21394. label: track.name,
  21395. kind: track.type.toLowerCase(),
  21396. default: track.default,
  21397. subtitleTrack: track
  21398. };
  21399. });
  21400. this.hls.trigger(Events.NON_NATIVE_TEXT_TRACKS_FOUND, {
  21401. tracks: tracksList
  21402. });
  21403. }
  21404. }
  21405. };
  21406. _proto.onManifestLoaded = function onManifestLoaded(event, data) {
  21407. var _this3 = this;
  21408. if (this.config.enableCEA708Captions && data.captions) {
  21409. data.captions.forEach(function (captionsTrack) {
  21410. var instreamIdMatch = /(?:CC|SERVICE)([1-4])/.exec(captionsTrack.instreamId);
  21411. if (!instreamIdMatch) {
  21412. return;
  21413. }
  21414. var trackName = "textTrack" + instreamIdMatch[1];
  21415. var trackProperties = _this3.captionsProperties[trackName];
  21416. if (!trackProperties) {
  21417. return;
  21418. }
  21419. trackProperties.label = captionsTrack.name;
  21420. if (captionsTrack.lang) {
  21421. // optional attribute
  21422. trackProperties.languageCode = captionsTrack.lang;
  21423. }
  21424. trackProperties.media = captionsTrack;
  21425. });
  21426. }
  21427. };
  21428. _proto.closedCaptionsForLevel = function closedCaptionsForLevel(frag) {
  21429. var level = this.hls.levels[frag.level];
  21430. return level == null ? void 0 : level.attrs['CLOSED-CAPTIONS'];
  21431. };
  21432. _proto.onFragLoading = function onFragLoading(event, data) {
  21433. // if this frag isn't contiguous, clear the parser so cues with bad start/end times aren't added to the textTrack
  21434. if (this.enabled && data.frag.type === PlaylistLevelType.MAIN) {
  21435. var _data$part$index, _data$part;
  21436. var cea608Parser1 = this.cea608Parser1,
  21437. cea608Parser2 = this.cea608Parser2,
  21438. lastSn = this.lastSn;
  21439. var _data$frag = data.frag,
  21440. cc = _data$frag.cc,
  21441. sn = _data$frag.sn;
  21442. var partIndex = (_data$part$index = (_data$part = data.part) == null ? void 0 : _data$part.index) != null ? _data$part$index : -1;
  21443. if (cea608Parser1 && cea608Parser2) {
  21444. if (sn !== lastSn + 1 || sn === lastSn && partIndex !== this.lastPartIndex + 1 || cc !== this.lastCc) {
  21445. cea608Parser1.reset();
  21446. cea608Parser2.reset();
  21447. }
  21448. }
  21449. this.lastCc = cc;
  21450. this.lastSn = sn;
  21451. this.lastPartIndex = partIndex;
  21452. }
  21453. };
  21454. _proto.onFragLoaded = function onFragLoaded(event, data) {
  21455. var frag = data.frag,
  21456. payload = data.payload;
  21457. if (frag.type === PlaylistLevelType.SUBTITLE) {
  21458. // If fragment is subtitle type, parse as WebVTT.
  21459. if (payload.byteLength) {
  21460. var decryptData = frag.decryptdata;
  21461. // fragment after decryption has a stats object
  21462. var decrypted = ('stats' in data);
  21463. // If the subtitles are not encrypted, parse VTTs now. Otherwise, we need to wait.
  21464. if (decryptData == null || !decryptData.encrypted || decrypted) {
  21465. var trackPlaylistMedia = this.tracks[frag.level];
  21466. var vttCCs = this.vttCCs;
  21467. if (!vttCCs[frag.cc]) {
  21468. vttCCs[frag.cc] = {
  21469. start: frag.start,
  21470. prevCC: this.prevCC,
  21471. new: true
  21472. };
  21473. this.prevCC = frag.cc;
  21474. }
  21475. if (trackPlaylistMedia && trackPlaylistMedia.textCodec === IMSC1_CODEC) {
  21476. this._parseIMSC1(frag, payload);
  21477. } else {
  21478. this._parseVTTs(data);
  21479. }
  21480. }
  21481. } else {
  21482. // In case there is no payload, finish unsuccessfully.
  21483. this.hls.trigger(Events.SUBTITLE_FRAG_PROCESSED, {
  21484. success: false,
  21485. frag: frag,
  21486. error: new Error('Empty subtitle payload')
  21487. });
  21488. }
  21489. }
  21490. };
  21491. _proto._parseIMSC1 = function _parseIMSC1(frag, payload) {
  21492. var _this4 = this;
  21493. var hls = this.hls;
  21494. parseIMSC1(payload, this.initPTS[frag.cc], function (cues) {
  21495. _this4._appendCues(cues, frag.level);
  21496. hls.trigger(Events.SUBTITLE_FRAG_PROCESSED, {
  21497. success: true,
  21498. frag: frag
  21499. });
  21500. }, function (error) {
  21501. logger.log("Failed to parse IMSC1: " + error);
  21502. hls.trigger(Events.SUBTITLE_FRAG_PROCESSED, {
  21503. success: false,
  21504. frag: frag,
  21505. error: error
  21506. });
  21507. });
  21508. };
  21509. _proto._parseVTTs = function _parseVTTs(data) {
  21510. var _frag$initSegment,
  21511. _this5 = this;
  21512. var frag = data.frag,
  21513. payload = data.payload;
  21514. // We need an initial synchronisation PTS. Store fragments as long as none has arrived
  21515. var initPTS = this.initPTS,
  21516. unparsedVttFrags = this.unparsedVttFrags;
  21517. var maxAvCC = initPTS.length - 1;
  21518. if (!initPTS[frag.cc] && maxAvCC === -1) {
  21519. unparsedVttFrags.push(data);
  21520. return;
  21521. }
  21522. var hls = this.hls;
  21523. // Parse the WebVTT file contents.
  21524. var payloadWebVTT = (_frag$initSegment = frag.initSegment) != null && _frag$initSegment.data ? appendUint8Array(frag.initSegment.data, new Uint8Array(payload)) : payload;
  21525. parseWebVTT(payloadWebVTT, this.initPTS[frag.cc], this.vttCCs, frag.cc, frag.start, function (cues) {
  21526. _this5._appendCues(cues, frag.level);
  21527. hls.trigger(Events.SUBTITLE_FRAG_PROCESSED, {
  21528. success: true,
  21529. frag: frag
  21530. });
  21531. }, function (error) {
  21532. var missingInitPTS = error.message === 'Missing initPTS for VTT MPEGTS';
  21533. if (missingInitPTS) {
  21534. unparsedVttFrags.push(data);
  21535. } else {
  21536. _this5._fallbackToIMSC1(frag, payload);
  21537. }
  21538. // Something went wrong while parsing. Trigger event with success false.
  21539. logger.log("Failed to parse VTT cue: " + error);
  21540. if (missingInitPTS && maxAvCC > frag.cc) {
  21541. return;
  21542. }
  21543. hls.trigger(Events.SUBTITLE_FRAG_PROCESSED, {
  21544. success: false,
  21545. frag: frag,
  21546. error: error
  21547. });
  21548. });
  21549. };
  21550. _proto._fallbackToIMSC1 = function _fallbackToIMSC1(frag, payload) {
  21551. var _this6 = this;
  21552. // If textCodec is unknown, try parsing as IMSC1. Set textCodec based on the result
  21553. var trackPlaylistMedia = this.tracks[frag.level];
  21554. if (!trackPlaylistMedia.textCodec) {
  21555. parseIMSC1(payload, this.initPTS[frag.cc], function () {
  21556. trackPlaylistMedia.textCodec = IMSC1_CODEC;
  21557. _this6._parseIMSC1(frag, payload);
  21558. }, function () {
  21559. trackPlaylistMedia.textCodec = 'wvtt';
  21560. });
  21561. }
  21562. };
  21563. _proto._appendCues = function _appendCues(cues, fragLevel) {
  21564. var hls = this.hls;
  21565. if (this.config.renderTextTracksNatively) {
  21566. var textTrack = this.textTracks[fragLevel];
  21567. // WebVTTParser.parse is an async method and if the currently selected text track mode is set to "disabled"
  21568. // before parsing is done then don't try to access currentTrack.cues.getCueById as cues will be null
  21569. // and trying to access getCueById method of cues will throw an exception
  21570. // Because we check if the mode is disabled, we can force check `cues` below. They can't be null.
  21571. if (!textTrack || textTrack.mode === 'disabled') {
  21572. return;
  21573. }
  21574. cues.forEach(function (cue) {
  21575. return addCueToTrack(textTrack, cue);
  21576. });
  21577. } else {
  21578. var currentTrack = this.tracks[fragLevel];
  21579. if (!currentTrack) {
  21580. return;
  21581. }
  21582. var track = currentTrack.default ? 'default' : 'subtitles' + fragLevel;
  21583. hls.trigger(Events.CUES_PARSED, {
  21584. type: 'subtitles',
  21585. cues: cues,
  21586. track: track
  21587. });
  21588. }
  21589. };
  21590. _proto.onFragDecrypted = function onFragDecrypted(event, data) {
  21591. var frag = data.frag;
  21592. if (frag.type === PlaylistLevelType.SUBTITLE) {
  21593. this.onFragLoaded(Events.FRAG_LOADED, data);
  21594. }
  21595. };
  21596. _proto.onSubtitleTracksCleared = function onSubtitleTracksCleared() {
  21597. this.tracks = [];
  21598. this.captionsTracks = {};
  21599. };
  21600. _proto.onFragParsingUserdata = function onFragParsingUserdata(event, data) {
  21601. this.initCea608Parsers();
  21602. var cea608Parser1 = this.cea608Parser1,
  21603. cea608Parser2 = this.cea608Parser2;
  21604. if (!this.enabled || !cea608Parser1 || !cea608Parser2) {
  21605. return;
  21606. }
  21607. var frag = data.frag,
  21608. samples = data.samples;
  21609. if (frag.type === PlaylistLevelType.MAIN && this.closedCaptionsForLevel(frag) === 'NONE') {
  21610. return;
  21611. }
  21612. // If the event contains captions (found in the bytes property), push all bytes into the parser immediately
  21613. // It will create the proper timestamps based on the PTS value
  21614. for (var i = 0; i < samples.length; i++) {
  21615. var ccBytes = samples[i].bytes;
  21616. if (ccBytes) {
  21617. var ccdatas = this.extractCea608Data(ccBytes);
  21618. cea608Parser1.addData(samples[i].pts, ccdatas[0]);
  21619. cea608Parser2.addData(samples[i].pts, ccdatas[1]);
  21620. }
  21621. }
  21622. };
  21623. _proto.onBufferFlushing = function onBufferFlushing(event, _ref2) {
  21624. var startOffset = _ref2.startOffset,
  21625. endOffset = _ref2.endOffset,
  21626. endOffsetSubtitles = _ref2.endOffsetSubtitles,
  21627. type = _ref2.type;
  21628. var media = this.media;
  21629. if (!media || media.currentTime < endOffset) {
  21630. return;
  21631. }
  21632. // Clear 608 caption cues from the captions TextTracks when the video back buffer is flushed
  21633. // Forward cues are never removed because we can loose streamed 608 content from recent fragments
  21634. if (!type || type === 'video') {
  21635. var captionsTracks = this.captionsTracks;
  21636. Object.keys(captionsTracks).forEach(function (trackName) {
  21637. return removeCuesInRange(captionsTracks[trackName], startOffset, endOffset);
  21638. });
  21639. }
  21640. if (this.config.renderTextTracksNatively) {
  21641. // Clear VTT/IMSC1 subtitle cues from the subtitle TextTracks when the back buffer is flushed
  21642. if (startOffset === 0 && endOffsetSubtitles !== undefined) {
  21643. var textTracks = this.textTracks;
  21644. Object.keys(textTracks).forEach(function (trackName) {
  21645. return removeCuesInRange(textTracks[trackName], startOffset, endOffsetSubtitles);
  21646. });
  21647. }
  21648. }
  21649. };
  21650. _proto.extractCea608Data = function extractCea608Data(byteArray) {
  21651. var actualCCBytes = [[], []];
  21652. var count = byteArray[0] & 0x1f;
  21653. var position = 2;
  21654. for (var j = 0; j < count; j++) {
  21655. var tmpByte = byteArray[position++];
  21656. var ccbyte1 = 0x7f & byteArray[position++];
  21657. var ccbyte2 = 0x7f & byteArray[position++];
  21658. if (ccbyte1 === 0 && ccbyte2 === 0) {
  21659. continue;
  21660. }
  21661. var ccValid = (0x04 & tmpByte) !== 0; // Support all four channels
  21662. if (ccValid) {
  21663. var ccType = 0x03 & tmpByte;
  21664. if (0x00 /* CEA608 field1*/ === ccType || 0x01 /* CEA608 field2*/ === ccType) {
  21665. // Exclude CEA708 CC data.
  21666. actualCCBytes[ccType].push(ccbyte1);
  21667. actualCCBytes[ccType].push(ccbyte2);
  21668. }
  21669. }
  21670. }
  21671. return actualCCBytes;
  21672. };
  21673. return TimelineController;
  21674. }();
  21675. function captionsOrSubtitlesFromCharacteristics(track) {
  21676. if (track.characteristics) {
  21677. if (/transcribes-spoken-dialog/gi.test(track.characteristics) && /describes-music-and-sound/gi.test(track.characteristics)) {
  21678. return 'captions';
  21679. }
  21680. }
  21681. return 'subtitles';
  21682. }
  21683. function canReuseVttTextTrack(inUseTrack, manifestTrack) {
  21684. return !!inUseTrack && inUseTrack.kind === captionsOrSubtitlesFromCharacteristics(manifestTrack) && subtitleTrackMatchesTextTrack(manifestTrack, inUseTrack);
  21685. }
  21686. function intersection(x1, x2, y1, y2) {
  21687. return Math.min(x2, y2) - Math.max(x1, y1);
  21688. }
  21689. function newVTTCCs() {
  21690. return {
  21691. ccOffset: 0,
  21692. presentationOffset: 0,
  21693. 0: {
  21694. start: 0,
  21695. prevCC: -1,
  21696. new: true
  21697. }
  21698. };
  21699. }
  21700. var CapLevelController = /*#__PURE__*/function () {
  21701. function CapLevelController(hls) {
  21702. this.hls = void 0;
  21703. this.autoLevelCapping = void 0;
  21704. this.firstLevel = void 0;
  21705. this.media = void 0;
  21706. this.restrictedLevels = void 0;
  21707. this.timer = void 0;
  21708. this.clientRect = void 0;
  21709. this.streamController = void 0;
  21710. this.hls = hls;
  21711. this.autoLevelCapping = Number.POSITIVE_INFINITY;
  21712. this.firstLevel = -1;
  21713. this.media = null;
  21714. this.restrictedLevels = [];
  21715. this.timer = undefined;
  21716. this.clientRect = null;
  21717. this.registerListeners();
  21718. }
  21719. var _proto = CapLevelController.prototype;
  21720. _proto.setStreamController = function setStreamController(streamController) {
  21721. this.streamController = streamController;
  21722. };
  21723. _proto.destroy = function destroy() {
  21724. if (this.hls) {
  21725. this.unregisterListener();
  21726. }
  21727. if (this.timer) {
  21728. this.stopCapping();
  21729. }
  21730. this.media = null;
  21731. this.clientRect = null;
  21732. // @ts-ignore
  21733. this.hls = this.streamController = null;
  21734. };
  21735. _proto.registerListeners = function registerListeners() {
  21736. var hls = this.hls;
  21737. hls.on(Events.FPS_DROP_LEVEL_CAPPING, this.onFpsDropLevelCapping, this);
  21738. hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21739. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  21740. hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  21741. hls.on(Events.BUFFER_CODECS, this.onBufferCodecs, this);
  21742. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  21743. };
  21744. _proto.unregisterListener = function unregisterListener() {
  21745. var hls = this.hls;
  21746. hls.off(Events.FPS_DROP_LEVEL_CAPPING, this.onFpsDropLevelCapping, this);
  21747. hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21748. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  21749. hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  21750. hls.off(Events.BUFFER_CODECS, this.onBufferCodecs, this);
  21751. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  21752. };
  21753. _proto.onFpsDropLevelCapping = function onFpsDropLevelCapping(event, data) {
  21754. // Don't add a restricted level more than once
  21755. var level = this.hls.levels[data.droppedLevel];
  21756. if (this.isLevelAllowed(level)) {
  21757. this.restrictedLevels.push({
  21758. bitrate: level.bitrate,
  21759. height: level.height,
  21760. width: level.width
  21761. });
  21762. }
  21763. };
  21764. _proto.onMediaAttaching = function onMediaAttaching(event, data) {
  21765. this.media = data.media instanceof HTMLVideoElement ? data.media : null;
  21766. this.clientRect = null;
  21767. if (this.timer && this.hls.levels.length) {
  21768. this.detectPlayerSize();
  21769. }
  21770. };
  21771. _proto.onManifestParsed = function onManifestParsed(event, data) {
  21772. var hls = this.hls;
  21773. this.restrictedLevels = [];
  21774. this.firstLevel = data.firstLevel;
  21775. if (hls.config.capLevelToPlayerSize && data.video) {
  21776. // Start capping immediately if the manifest has signaled video codecs
  21777. this.startCapping();
  21778. }
  21779. };
  21780. _proto.onLevelsUpdated = function onLevelsUpdated(event, data) {
  21781. if (this.timer && isFiniteNumber(this.autoLevelCapping)) {
  21782. this.detectPlayerSize();
  21783. }
  21784. }
  21785. // Only activate capping when playing a video stream; otherwise, multi-bitrate audio-only streams will be restricted
  21786. // to the first level
  21787. ;
  21788. _proto.onBufferCodecs = function onBufferCodecs(event, data) {
  21789. var hls = this.hls;
  21790. if (hls.config.capLevelToPlayerSize && data.video) {
  21791. // If the manifest did not signal a video codec capping has been deferred until we're certain video is present
  21792. this.startCapping();
  21793. }
  21794. };
  21795. _proto.onMediaDetaching = function onMediaDetaching() {
  21796. this.stopCapping();
  21797. };
  21798. _proto.detectPlayerSize = function detectPlayerSize() {
  21799. if (this.media) {
  21800. if (this.mediaHeight <= 0 || this.mediaWidth <= 0) {
  21801. this.clientRect = null;
  21802. return;
  21803. }
  21804. var levels = this.hls.levels;
  21805. if (levels.length) {
  21806. var hls = this.hls;
  21807. var maxLevel = this.getMaxLevel(levels.length - 1);
  21808. if (maxLevel !== this.autoLevelCapping) {
  21809. logger.log("Setting autoLevelCapping to " + maxLevel + ": " + levels[maxLevel].height + "p@" + levels[maxLevel].bitrate + " for media " + this.mediaWidth + "x" + this.mediaHeight);
  21810. }
  21811. hls.autoLevelCapping = maxLevel;
  21812. if (hls.autoLevelCapping > this.autoLevelCapping && this.streamController) {
  21813. // if auto level capping has a higher value for the previous one, flush the buffer using nextLevelSwitch
  21814. // usually happen when the user go to the fullscreen mode.
  21815. this.streamController.nextLevelSwitch();
  21816. }
  21817. this.autoLevelCapping = hls.autoLevelCapping;
  21818. }
  21819. }
  21820. }
  21821. /*
  21822. * returns level should be the one with the dimensions equal or greater than the media (player) dimensions (so the video will be downscaled)
  21823. */;
  21824. _proto.getMaxLevel = function getMaxLevel(capLevelIndex) {
  21825. var _this = this;
  21826. var levels = this.hls.levels;
  21827. if (!levels.length) {
  21828. return -1;
  21829. }
  21830. var validLevels = levels.filter(function (level, index) {
  21831. return _this.isLevelAllowed(level) && index <= capLevelIndex;
  21832. });
  21833. this.clientRect = null;
  21834. return CapLevelController.getMaxLevelByMediaSize(validLevels, this.mediaWidth, this.mediaHeight);
  21835. };
  21836. _proto.startCapping = function startCapping() {
  21837. if (this.timer) {
  21838. // Don't reset capping if started twice; this can happen if the manifest signals a video codec
  21839. return;
  21840. }
  21841. this.autoLevelCapping = Number.POSITIVE_INFINITY;
  21842. self.clearInterval(this.timer);
  21843. this.timer = self.setInterval(this.detectPlayerSize.bind(this), 1000);
  21844. this.detectPlayerSize();
  21845. };
  21846. _proto.stopCapping = function stopCapping() {
  21847. this.restrictedLevels = [];
  21848. this.firstLevel = -1;
  21849. this.autoLevelCapping = Number.POSITIVE_INFINITY;
  21850. if (this.timer) {
  21851. self.clearInterval(this.timer);
  21852. this.timer = undefined;
  21853. }
  21854. };
  21855. _proto.getDimensions = function getDimensions() {
  21856. if (this.clientRect) {
  21857. return this.clientRect;
  21858. }
  21859. var media = this.media;
  21860. var boundsRect = {
  21861. width: 0,
  21862. height: 0
  21863. };
  21864. if (media) {
  21865. var clientRect = media.getBoundingClientRect();
  21866. boundsRect.width = clientRect.width;
  21867. boundsRect.height = clientRect.height;
  21868. if (!boundsRect.width && !boundsRect.height) {
  21869. // When the media element has no width or height (equivalent to not being in the DOM),
  21870. // then use its width and height attributes (media.width, media.height)
  21871. boundsRect.width = clientRect.right - clientRect.left || media.width || 0;
  21872. boundsRect.height = clientRect.bottom - clientRect.top || media.height || 0;
  21873. }
  21874. }
  21875. this.clientRect = boundsRect;
  21876. return boundsRect;
  21877. };
  21878. _proto.isLevelAllowed = function isLevelAllowed(level) {
  21879. var restrictedLevels = this.restrictedLevels;
  21880. return !restrictedLevels.some(function (restrictedLevel) {
  21881. return level.bitrate === restrictedLevel.bitrate && level.width === restrictedLevel.width && level.height === restrictedLevel.height;
  21882. });
  21883. };
  21884. CapLevelController.getMaxLevelByMediaSize = function getMaxLevelByMediaSize(levels, width, height) {
  21885. if (!(levels != null && levels.length)) {
  21886. return -1;
  21887. }
  21888. // Levels can have the same dimensions but differing bandwidths - since levels are ordered, we can look to the next
  21889. // to determine whether we've chosen the greatest bandwidth for the media's dimensions
  21890. var atGreatestBandwidth = function atGreatestBandwidth(curLevel, nextLevel) {
  21891. if (!nextLevel) {
  21892. return true;
  21893. }
  21894. return curLevel.width !== nextLevel.width || curLevel.height !== nextLevel.height;
  21895. };
  21896. // If we run through the loop without breaking, the media's dimensions are greater than every level, so default to
  21897. // the max level
  21898. var maxLevelIndex = levels.length - 1;
  21899. // Prevent changes in aspect-ratio from causing capping to toggle back and forth
  21900. var squareSize = Math.max(width, height);
  21901. for (var i = 0; i < levels.length; i += 1) {
  21902. var level = levels[i];
  21903. if ((level.width >= squareSize || level.height >= squareSize) && atGreatestBandwidth(level, levels[i + 1])) {
  21904. maxLevelIndex = i;
  21905. break;
  21906. }
  21907. }
  21908. return maxLevelIndex;
  21909. };
  21910. _createClass(CapLevelController, [{
  21911. key: "mediaWidth",
  21912. get: function get() {
  21913. return this.getDimensions().width * this.contentScaleFactor;
  21914. }
  21915. }, {
  21916. key: "mediaHeight",
  21917. get: function get() {
  21918. return this.getDimensions().height * this.contentScaleFactor;
  21919. }
  21920. }, {
  21921. key: "contentScaleFactor",
  21922. get: function get() {
  21923. var pixelRatio = 1;
  21924. if (!this.hls.config.ignoreDevicePixelRatio) {
  21925. try {
  21926. pixelRatio = self.devicePixelRatio;
  21927. } catch (e) {
  21928. /* no-op */
  21929. }
  21930. }
  21931. return pixelRatio;
  21932. }
  21933. }]);
  21934. return CapLevelController;
  21935. }();
  21936. var FPSController = /*#__PURE__*/function () {
  21937. function FPSController(hls) {
  21938. this.hls = void 0;
  21939. this.isVideoPlaybackQualityAvailable = false;
  21940. this.timer = void 0;
  21941. this.media = null;
  21942. this.lastTime = void 0;
  21943. this.lastDroppedFrames = 0;
  21944. this.lastDecodedFrames = 0;
  21945. // stream controller must be provided as a dependency!
  21946. this.streamController = void 0;
  21947. this.hls = hls;
  21948. this.registerListeners();
  21949. }
  21950. var _proto = FPSController.prototype;
  21951. _proto.setStreamController = function setStreamController(streamController) {
  21952. this.streamController = streamController;
  21953. };
  21954. _proto.registerListeners = function registerListeners() {
  21955. this.hls.on(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21956. };
  21957. _proto.unregisterListeners = function unregisterListeners() {
  21958. this.hls.off(Events.MEDIA_ATTACHING, this.onMediaAttaching, this);
  21959. };
  21960. _proto.destroy = function destroy() {
  21961. if (this.timer) {
  21962. clearInterval(this.timer);
  21963. }
  21964. this.unregisterListeners();
  21965. this.isVideoPlaybackQualityAvailable = false;
  21966. this.media = null;
  21967. };
  21968. _proto.onMediaAttaching = function onMediaAttaching(event, data) {
  21969. var config = this.hls.config;
  21970. if (config.capLevelOnFPSDrop) {
  21971. var media = data.media instanceof self.HTMLVideoElement ? data.media : null;
  21972. this.media = media;
  21973. if (media && typeof media.getVideoPlaybackQuality === 'function') {
  21974. this.isVideoPlaybackQualityAvailable = true;
  21975. }
  21976. self.clearInterval(this.timer);
  21977. this.timer = self.setInterval(this.checkFPSInterval.bind(this), config.fpsDroppedMonitoringPeriod);
  21978. }
  21979. };
  21980. _proto.checkFPS = function checkFPS(video, decodedFrames, droppedFrames) {
  21981. var currentTime = performance.now();
  21982. if (decodedFrames) {
  21983. if (this.lastTime) {
  21984. var currentPeriod = currentTime - this.lastTime;
  21985. var currentDropped = droppedFrames - this.lastDroppedFrames;
  21986. var currentDecoded = decodedFrames - this.lastDecodedFrames;
  21987. var droppedFPS = 1000 * currentDropped / currentPeriod;
  21988. var hls = this.hls;
  21989. hls.trigger(Events.FPS_DROP, {
  21990. currentDropped: currentDropped,
  21991. currentDecoded: currentDecoded,
  21992. totalDroppedFrames: droppedFrames
  21993. });
  21994. if (droppedFPS > 0) {
  21995. // logger.log('checkFPS : droppedFPS/decodedFPS:' + droppedFPS/(1000 * currentDecoded / currentPeriod));
  21996. if (currentDropped > hls.config.fpsDroppedMonitoringThreshold * currentDecoded) {
  21997. var currentLevel = hls.currentLevel;
  21998. logger.warn('drop FPS ratio greater than max allowed value for currentLevel: ' + currentLevel);
  21999. if (currentLevel > 0 && (hls.autoLevelCapping === -1 || hls.autoLevelCapping >= currentLevel)) {
  22000. currentLevel = currentLevel - 1;
  22001. hls.trigger(Events.FPS_DROP_LEVEL_CAPPING, {
  22002. level: currentLevel,
  22003. droppedLevel: hls.currentLevel
  22004. });
  22005. hls.autoLevelCapping = currentLevel;
  22006. this.streamController.nextLevelSwitch();
  22007. }
  22008. }
  22009. }
  22010. }
  22011. this.lastTime = currentTime;
  22012. this.lastDroppedFrames = droppedFrames;
  22013. this.lastDecodedFrames = decodedFrames;
  22014. }
  22015. };
  22016. _proto.checkFPSInterval = function checkFPSInterval() {
  22017. var video = this.media;
  22018. if (video) {
  22019. if (this.isVideoPlaybackQualityAvailable) {
  22020. var videoPlaybackQuality = video.getVideoPlaybackQuality();
  22021. this.checkFPS(video, videoPlaybackQuality.totalVideoFrames, videoPlaybackQuality.droppedVideoFrames);
  22022. } else {
  22023. // HTMLVideoElement doesn't include the webkit types
  22024. this.checkFPS(video, video.webkitDecodedFrameCount, video.webkitDroppedFrameCount);
  22025. }
  22026. }
  22027. };
  22028. return FPSController;
  22029. }();
  22030. var LOGGER_PREFIX = '[eme]';
  22031. /**
  22032. * Controller to deal with encrypted media extensions (EME)
  22033. * @see https://developer.mozilla.org/en-US/docs/Web/API/Encrypted_Media_Extensions_API
  22034. *
  22035. * @class
  22036. * @constructor
  22037. */
  22038. var EMEController = /*#__PURE__*/function () {
  22039. function EMEController(hls) {
  22040. this.hls = void 0;
  22041. this.config = void 0;
  22042. this.media = null;
  22043. this.keyFormatPromise = null;
  22044. this.keySystemAccessPromises = {};
  22045. this._requestLicenseFailureCount = 0;
  22046. this.mediaKeySessions = [];
  22047. this.keyIdToKeySessionPromise = {};
  22048. this.setMediaKeysQueue = EMEController.CDMCleanupPromise ? [EMEController.CDMCleanupPromise] : [];
  22049. this.onMediaEncrypted = this._onMediaEncrypted.bind(this);
  22050. this.onWaitingForKey = this._onWaitingForKey.bind(this);
  22051. this.debug = logger.debug.bind(logger, LOGGER_PREFIX);
  22052. this.log = logger.log.bind(logger, LOGGER_PREFIX);
  22053. this.warn = logger.warn.bind(logger, LOGGER_PREFIX);
  22054. this.error = logger.error.bind(logger, LOGGER_PREFIX);
  22055. this.hls = hls;
  22056. this.config = hls.config;
  22057. this.registerListeners();
  22058. }
  22059. var _proto = EMEController.prototype;
  22060. _proto.destroy = function destroy() {
  22061. this.unregisterListeners();
  22062. this.onMediaDetached();
  22063. // Remove any references that could be held in config options or callbacks
  22064. var config = this.config;
  22065. config.requestMediaKeySystemAccessFunc = null;
  22066. config.licenseXhrSetup = config.licenseResponseCallback = undefined;
  22067. config.drmSystems = config.drmSystemOptions = {};
  22068. // @ts-ignore
  22069. this.hls = this.onMediaEncrypted = this.onWaitingForKey = this.keyIdToKeySessionPromise = null;
  22070. // @ts-ignore
  22071. this.config = null;
  22072. };
  22073. _proto.registerListeners = function registerListeners() {
  22074. this.hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  22075. this.hls.on(Events.MEDIA_DETACHED, this.onMediaDetached, this);
  22076. this.hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  22077. this.hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  22078. };
  22079. _proto.unregisterListeners = function unregisterListeners() {
  22080. this.hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  22081. this.hls.off(Events.MEDIA_DETACHED, this.onMediaDetached, this);
  22082. this.hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  22083. this.hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  22084. };
  22085. _proto.getLicenseServerUrl = function getLicenseServerUrl(keySystem) {
  22086. var _this$config = this.config,
  22087. drmSystems = _this$config.drmSystems,
  22088. widevineLicenseUrl = _this$config.widevineLicenseUrl;
  22089. var keySystemConfiguration = drmSystems[keySystem];
  22090. if (keySystemConfiguration) {
  22091. return keySystemConfiguration.licenseUrl;
  22092. }
  22093. // For backward compatibility
  22094. if (keySystem === KeySystems.WIDEVINE && widevineLicenseUrl) {
  22095. return widevineLicenseUrl;
  22096. }
  22097. throw new Error("no license server URL configured for key-system \"" + keySystem + "\"");
  22098. };
  22099. _proto.getServerCertificateUrl = function getServerCertificateUrl(keySystem) {
  22100. var drmSystems = this.config.drmSystems;
  22101. var keySystemConfiguration = drmSystems[keySystem];
  22102. if (keySystemConfiguration) {
  22103. return keySystemConfiguration.serverCertificateUrl;
  22104. } else {
  22105. this.log("No Server Certificate in config.drmSystems[\"" + keySystem + "\"]");
  22106. }
  22107. };
  22108. _proto.attemptKeySystemAccess = function attemptKeySystemAccess(keySystemsToAttempt) {
  22109. var _this = this;
  22110. var levels = this.hls.levels;
  22111. var uniqueCodec = function uniqueCodec(value, i, a) {
  22112. return !!value && a.indexOf(value) === i;
  22113. };
  22114. var audioCodecs = levels.map(function (level) {
  22115. return level.audioCodec;
  22116. }).filter(uniqueCodec);
  22117. var videoCodecs = levels.map(function (level) {
  22118. return level.videoCodec;
  22119. }).filter(uniqueCodec);
  22120. if (audioCodecs.length + videoCodecs.length === 0) {
  22121. videoCodecs.push('avc1.42e01e');
  22122. }
  22123. return new Promise(function (resolve, reject) {
  22124. var attempt = function attempt(keySystems) {
  22125. var keySystem = keySystems.shift();
  22126. _this.getMediaKeysPromise(keySystem, audioCodecs, videoCodecs).then(function (mediaKeys) {
  22127. return resolve({
  22128. keySystem: keySystem,
  22129. mediaKeys: mediaKeys
  22130. });
  22131. }).catch(function (error) {
  22132. if (keySystems.length) {
  22133. attempt(keySystems);
  22134. } else if (error instanceof EMEKeyError) {
  22135. reject(error);
  22136. } else {
  22137. reject(new EMEKeyError({
  22138. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22139. details: ErrorDetails.KEY_SYSTEM_NO_ACCESS,
  22140. error: error,
  22141. fatal: true
  22142. }, error.message));
  22143. }
  22144. });
  22145. };
  22146. attempt(keySystemsToAttempt);
  22147. });
  22148. };
  22149. _proto.requestMediaKeySystemAccess = function requestMediaKeySystemAccess$1(keySystem, supportedConfigurations) {
  22150. var requestMediaKeySystemAccessFunc = this.config.requestMediaKeySystemAccessFunc;
  22151. if (!(typeof requestMediaKeySystemAccessFunc === 'function')) {
  22152. var errMessage = "Configured requestMediaKeySystemAccess is not a function " + requestMediaKeySystemAccessFunc;
  22153. if (requestMediaKeySystemAccess === null && self.location.protocol === 'http:') {
  22154. errMessage = "navigator.requestMediaKeySystemAccess is not available over insecure protocol " + location.protocol;
  22155. }
  22156. return Promise.reject(new Error(errMessage));
  22157. }
  22158. return requestMediaKeySystemAccessFunc(keySystem, supportedConfigurations);
  22159. };
  22160. _proto.getMediaKeysPromise = function getMediaKeysPromise(keySystem, audioCodecs, videoCodecs) {
  22161. var _this2 = this;
  22162. // This can throw, but is caught in event handler callpath
  22163. var mediaKeySystemConfigs = getSupportedMediaKeySystemConfigurations(keySystem, audioCodecs, videoCodecs, this.config.drmSystemOptions);
  22164. var keySystemAccessPromises = this.keySystemAccessPromises[keySystem];
  22165. var keySystemAccess = keySystemAccessPromises == null ? void 0 : keySystemAccessPromises.keySystemAccess;
  22166. if (!keySystemAccess) {
  22167. this.log("Requesting encrypted media \"" + keySystem + "\" key-system access with config: " + JSON.stringify(mediaKeySystemConfigs));
  22168. keySystemAccess = this.requestMediaKeySystemAccess(keySystem, mediaKeySystemConfigs);
  22169. var _keySystemAccessPromises = this.keySystemAccessPromises[keySystem] = {
  22170. keySystemAccess: keySystemAccess
  22171. };
  22172. keySystemAccess.catch(function (error) {
  22173. _this2.log("Failed to obtain access to key-system \"" + keySystem + "\": " + error);
  22174. });
  22175. return keySystemAccess.then(function (mediaKeySystemAccess) {
  22176. _this2.log("Access for key-system \"" + mediaKeySystemAccess.keySystem + "\" obtained");
  22177. var certificateRequest = _this2.fetchServerCertificate(keySystem);
  22178. _this2.log("Create media-keys for \"" + keySystem + "\"");
  22179. _keySystemAccessPromises.mediaKeys = mediaKeySystemAccess.createMediaKeys().then(function (mediaKeys) {
  22180. _this2.log("Media-keys created for \"" + keySystem + "\"");
  22181. return certificateRequest.then(function (certificate) {
  22182. if (certificate) {
  22183. return _this2.setMediaKeysServerCertificate(mediaKeys, keySystem, certificate);
  22184. }
  22185. return mediaKeys;
  22186. });
  22187. });
  22188. _keySystemAccessPromises.mediaKeys.catch(function (error) {
  22189. _this2.error("Failed to create media-keys for \"" + keySystem + "\"}: " + error);
  22190. });
  22191. return _keySystemAccessPromises.mediaKeys;
  22192. });
  22193. }
  22194. return keySystemAccess.then(function () {
  22195. return keySystemAccessPromises.mediaKeys;
  22196. });
  22197. };
  22198. _proto.createMediaKeySessionContext = function createMediaKeySessionContext(_ref) {
  22199. var decryptdata = _ref.decryptdata,
  22200. keySystem = _ref.keySystem,
  22201. mediaKeys = _ref.mediaKeys;
  22202. this.log("Creating key-system session \"" + keySystem + "\" keyId: " + Hex.hexDump(decryptdata.keyId || []));
  22203. var mediaKeysSession = mediaKeys.createSession();
  22204. var mediaKeySessionContext = {
  22205. decryptdata: decryptdata,
  22206. keySystem: keySystem,
  22207. mediaKeys: mediaKeys,
  22208. mediaKeysSession: mediaKeysSession,
  22209. keyStatus: 'status-pending'
  22210. };
  22211. this.mediaKeySessions.push(mediaKeySessionContext);
  22212. return mediaKeySessionContext;
  22213. };
  22214. _proto.renewKeySession = function renewKeySession(mediaKeySessionContext) {
  22215. var decryptdata = mediaKeySessionContext.decryptdata;
  22216. if (decryptdata.pssh) {
  22217. var keySessionContext = this.createMediaKeySessionContext(mediaKeySessionContext);
  22218. var _keyId = this.getKeyIdString(decryptdata);
  22219. var scheme = 'cenc';
  22220. this.keyIdToKeySessionPromise[_keyId] = this.generateRequestWithPreferredKeySession(keySessionContext, scheme, decryptdata.pssh, 'expired');
  22221. } else {
  22222. this.warn("Could not renew expired session. Missing pssh initData.");
  22223. }
  22224. this.removeSession(mediaKeySessionContext);
  22225. };
  22226. _proto.getKeyIdString = function getKeyIdString(decryptdata) {
  22227. if (!decryptdata) {
  22228. throw new Error('Could not read keyId of undefined decryptdata');
  22229. }
  22230. if (decryptdata.keyId === null) {
  22231. throw new Error('keyId is null');
  22232. }
  22233. return Hex.hexDump(decryptdata.keyId);
  22234. };
  22235. _proto.updateKeySession = function updateKeySession(mediaKeySessionContext, data) {
  22236. var _mediaKeySessionConte;
  22237. var keySession = mediaKeySessionContext.mediaKeysSession;
  22238. this.log("Updating key-session \"" + keySession.sessionId + "\" for keyID " + Hex.hexDump(((_mediaKeySessionConte = mediaKeySessionContext.decryptdata) == null ? void 0 : _mediaKeySessionConte.keyId) || []) + "\n } (data length: " + (data ? data.byteLength : data) + ")");
  22239. return keySession.update(data);
  22240. };
  22241. _proto.selectKeySystemFormat = function selectKeySystemFormat(frag) {
  22242. var keyFormats = Object.keys(frag.levelkeys || {});
  22243. if (!this.keyFormatPromise) {
  22244. this.log("Selecting key-system from fragment (sn: " + frag.sn + " " + frag.type + ": " + frag.level + ") key formats " + keyFormats.join(', '));
  22245. this.keyFormatPromise = this.getKeyFormatPromise(keyFormats);
  22246. }
  22247. return this.keyFormatPromise;
  22248. };
  22249. _proto.getKeyFormatPromise = function getKeyFormatPromise(keyFormats) {
  22250. var _this3 = this;
  22251. return new Promise(function (resolve, reject) {
  22252. var keySystemsInConfig = getKeySystemsForConfig(_this3.config);
  22253. var keySystemsToAttempt = keyFormats.map(keySystemFormatToKeySystemDomain).filter(function (value) {
  22254. return !!value && keySystemsInConfig.indexOf(value) !== -1;
  22255. });
  22256. return _this3.getKeySystemSelectionPromise(keySystemsToAttempt).then(function (_ref2) {
  22257. var keySystem = _ref2.keySystem;
  22258. var keySystemFormat = keySystemDomainToKeySystemFormat(keySystem);
  22259. if (keySystemFormat) {
  22260. resolve(keySystemFormat);
  22261. } else {
  22262. reject(new Error("Unable to find format for key-system \"" + keySystem + "\""));
  22263. }
  22264. }).catch(reject);
  22265. });
  22266. };
  22267. _proto.loadKey = function loadKey(data) {
  22268. var _this4 = this;
  22269. var decryptdata = data.keyInfo.decryptdata;
  22270. var keyId = this.getKeyIdString(decryptdata);
  22271. var keyDetails = "(keyId: " + keyId + " format: \"" + decryptdata.keyFormat + "\" method: " + decryptdata.method + " uri: " + decryptdata.uri + ")";
  22272. this.log("Starting session for key " + keyDetails);
  22273. var keySessionContextPromise = this.keyIdToKeySessionPromise[keyId];
  22274. if (!keySessionContextPromise) {
  22275. keySessionContextPromise = this.keyIdToKeySessionPromise[keyId] = this.getKeySystemForKeyPromise(decryptdata).then(function (_ref3) {
  22276. var keySystem = _ref3.keySystem,
  22277. mediaKeys = _ref3.mediaKeys;
  22278. _this4.throwIfDestroyed();
  22279. _this4.log("Handle encrypted media sn: " + data.frag.sn + " " + data.frag.type + ": " + data.frag.level + " using key " + keyDetails);
  22280. return _this4.attemptSetMediaKeys(keySystem, mediaKeys).then(function () {
  22281. _this4.throwIfDestroyed();
  22282. var keySessionContext = _this4.createMediaKeySessionContext({
  22283. keySystem: keySystem,
  22284. mediaKeys: mediaKeys,
  22285. decryptdata: decryptdata
  22286. });
  22287. var scheme = 'cenc';
  22288. return _this4.generateRequestWithPreferredKeySession(keySessionContext, scheme, decryptdata.pssh, 'playlist-key');
  22289. });
  22290. });
  22291. keySessionContextPromise.catch(function (error) {
  22292. return _this4.handleError(error);
  22293. });
  22294. }
  22295. return keySessionContextPromise;
  22296. };
  22297. _proto.throwIfDestroyed = function throwIfDestroyed(message) {
  22298. if (!this.hls) {
  22299. throw new Error('invalid state');
  22300. }
  22301. };
  22302. _proto.handleError = function handleError(error) {
  22303. if (!this.hls) {
  22304. return;
  22305. }
  22306. this.error(error.message);
  22307. if (error instanceof EMEKeyError) {
  22308. this.hls.trigger(Events.ERROR, error.data);
  22309. } else {
  22310. this.hls.trigger(Events.ERROR, {
  22311. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22312. details: ErrorDetails.KEY_SYSTEM_NO_KEYS,
  22313. error: error,
  22314. fatal: true
  22315. });
  22316. }
  22317. };
  22318. _proto.getKeySystemForKeyPromise = function getKeySystemForKeyPromise(decryptdata) {
  22319. var keyId = this.getKeyIdString(decryptdata);
  22320. var mediaKeySessionContext = this.keyIdToKeySessionPromise[keyId];
  22321. if (!mediaKeySessionContext) {
  22322. var keySystem = keySystemFormatToKeySystemDomain(decryptdata.keyFormat);
  22323. var keySystemsToAttempt = keySystem ? [keySystem] : getKeySystemsForConfig(this.config);
  22324. return this.attemptKeySystemAccess(keySystemsToAttempt);
  22325. }
  22326. return mediaKeySessionContext;
  22327. };
  22328. _proto.getKeySystemSelectionPromise = function getKeySystemSelectionPromise(keySystemsToAttempt) {
  22329. if (!keySystemsToAttempt.length) {
  22330. keySystemsToAttempt = getKeySystemsForConfig(this.config);
  22331. }
  22332. if (keySystemsToAttempt.length === 0) {
  22333. throw new EMEKeyError({
  22334. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22335. details: ErrorDetails.KEY_SYSTEM_NO_CONFIGURED_LICENSE,
  22336. fatal: true
  22337. }, "Missing key-system license configuration options " + JSON.stringify({
  22338. drmSystems: this.config.drmSystems
  22339. }));
  22340. }
  22341. return this.attemptKeySystemAccess(keySystemsToAttempt);
  22342. };
  22343. _proto._onMediaEncrypted = function _onMediaEncrypted(event) {
  22344. var _this5 = this;
  22345. var initDataType = event.initDataType,
  22346. initData = event.initData;
  22347. this.debug("\"" + event.type + "\" event: init data type: \"" + initDataType + "\"");
  22348. // Ignore event when initData is null
  22349. if (initData === null) {
  22350. return;
  22351. }
  22352. var keyId;
  22353. var keySystemDomain;
  22354. if (initDataType === 'sinf' && this.config.drmSystems[KeySystems.FAIRPLAY]) {
  22355. // Match sinf keyId to playlist skd://keyId=
  22356. var json = bin2str(new Uint8Array(initData));
  22357. try {
  22358. var sinf = base64Decode(JSON.parse(json).sinf);
  22359. var tenc = parseSinf(new Uint8Array(sinf));
  22360. if (!tenc) {
  22361. return;
  22362. }
  22363. keyId = tenc.subarray(8, 24);
  22364. keySystemDomain = KeySystems.FAIRPLAY;
  22365. } catch (error) {
  22366. this.warn('Failed to parse sinf "encrypted" event message initData');
  22367. return;
  22368. }
  22369. } else {
  22370. // Support clear-lead key-session creation (otherwise depend on playlist keys)
  22371. var psshInfo = parsePssh(initData);
  22372. if (psshInfo === null) {
  22373. return;
  22374. }
  22375. if (psshInfo.version === 0 && psshInfo.systemId === KeySystemIds.WIDEVINE && psshInfo.data) {
  22376. keyId = psshInfo.data.subarray(8, 24);
  22377. }
  22378. keySystemDomain = keySystemIdToKeySystemDomain(psshInfo.systemId);
  22379. }
  22380. if (!keySystemDomain || !keyId) {
  22381. return;
  22382. }
  22383. var keyIdHex = Hex.hexDump(keyId);
  22384. var keyIdToKeySessionPromise = this.keyIdToKeySessionPromise,
  22385. mediaKeySessions = this.mediaKeySessions;
  22386. var keySessionContextPromise = keyIdToKeySessionPromise[keyIdHex];
  22387. var _loop = function _loop() {
  22388. // Match playlist key
  22389. var keyContext = mediaKeySessions[i];
  22390. var decryptdata = keyContext.decryptdata;
  22391. if (decryptdata.pssh || !decryptdata.keyId) {
  22392. return 0; // continue
  22393. }
  22394. var oldKeyIdHex = Hex.hexDump(decryptdata.keyId);
  22395. if (keyIdHex === oldKeyIdHex || decryptdata.uri.replace(/-/g, '').indexOf(keyIdHex) !== -1) {
  22396. keySessionContextPromise = keyIdToKeySessionPromise[oldKeyIdHex];
  22397. delete keyIdToKeySessionPromise[oldKeyIdHex];
  22398. decryptdata.pssh = new Uint8Array(initData);
  22399. decryptdata.keyId = keyId;
  22400. keySessionContextPromise = keyIdToKeySessionPromise[keyIdHex] = keySessionContextPromise.then(function () {
  22401. return _this5.generateRequestWithPreferredKeySession(keyContext, initDataType, initData, 'encrypted-event-key-match');
  22402. });
  22403. return 1; // break
  22404. }
  22405. },
  22406. _ret;
  22407. for (var i = 0; i < mediaKeySessions.length; i++) {
  22408. _ret = _loop();
  22409. if (_ret === 0) continue;
  22410. if (_ret === 1) break;
  22411. }
  22412. if (!keySessionContextPromise) {
  22413. // Clear-lead key (not encountered in playlist)
  22414. keySessionContextPromise = keyIdToKeySessionPromise[keyIdHex] = this.getKeySystemSelectionPromise([keySystemDomain]).then(function (_ref4) {
  22415. var _keySystemToKeySystem;
  22416. var keySystem = _ref4.keySystem,
  22417. mediaKeys = _ref4.mediaKeys;
  22418. _this5.throwIfDestroyed();
  22419. var decryptdata = new LevelKey('ISO-23001-7', keyIdHex, (_keySystemToKeySystem = keySystemDomainToKeySystemFormat(keySystem)) != null ? _keySystemToKeySystem : '');
  22420. decryptdata.pssh = new Uint8Array(initData);
  22421. decryptdata.keyId = keyId;
  22422. return _this5.attemptSetMediaKeys(keySystem, mediaKeys).then(function () {
  22423. _this5.throwIfDestroyed();
  22424. var keySessionContext = _this5.createMediaKeySessionContext({
  22425. decryptdata: decryptdata,
  22426. keySystem: keySystem,
  22427. mediaKeys: mediaKeys
  22428. });
  22429. return _this5.generateRequestWithPreferredKeySession(keySessionContext, initDataType, initData, 'encrypted-event-no-match');
  22430. });
  22431. });
  22432. }
  22433. keySessionContextPromise.catch(function (error) {
  22434. return _this5.handleError(error);
  22435. });
  22436. };
  22437. _proto._onWaitingForKey = function _onWaitingForKey(event) {
  22438. this.log("\"" + event.type + "\" event");
  22439. };
  22440. _proto.attemptSetMediaKeys = function attemptSetMediaKeys(keySystem, mediaKeys) {
  22441. var _this6 = this;
  22442. var queue = this.setMediaKeysQueue.slice();
  22443. this.log("Setting media-keys for \"" + keySystem + "\"");
  22444. // Only one setMediaKeys() can run at one time, and multiple setMediaKeys() operations
  22445. // can be queued for execution for multiple key sessions.
  22446. var setMediaKeysPromise = Promise.all(queue).then(function () {
  22447. if (!_this6.media) {
  22448. throw new Error('Attempted to set mediaKeys without media element attached');
  22449. }
  22450. return _this6.media.setMediaKeys(mediaKeys);
  22451. });
  22452. this.setMediaKeysQueue.push(setMediaKeysPromise);
  22453. return setMediaKeysPromise.then(function () {
  22454. _this6.log("Media-keys set for \"" + keySystem + "\"");
  22455. queue.push(setMediaKeysPromise);
  22456. _this6.setMediaKeysQueue = _this6.setMediaKeysQueue.filter(function (p) {
  22457. return queue.indexOf(p) === -1;
  22458. });
  22459. });
  22460. };
  22461. _proto.generateRequestWithPreferredKeySession = function generateRequestWithPreferredKeySession(context, initDataType, initData, reason) {
  22462. var _this$config$drmSyste,
  22463. _this$config$drmSyste2,
  22464. _this7 = this;
  22465. var generateRequestFilter = (_this$config$drmSyste = this.config.drmSystems) == null ? void 0 : (_this$config$drmSyste2 = _this$config$drmSyste[context.keySystem]) == null ? void 0 : _this$config$drmSyste2.generateRequest;
  22466. if (generateRequestFilter) {
  22467. try {
  22468. var mappedInitData = generateRequestFilter.call(this.hls, initDataType, initData, context);
  22469. if (!mappedInitData) {
  22470. throw new Error('Invalid response from configured generateRequest filter');
  22471. }
  22472. initDataType = mappedInitData.initDataType;
  22473. initData = context.decryptdata.pssh = mappedInitData.initData ? new Uint8Array(mappedInitData.initData) : null;
  22474. } catch (error) {
  22475. var _this$hls;
  22476. this.warn(error.message);
  22477. if ((_this$hls = this.hls) != null && _this$hls.config.debug) {
  22478. throw error;
  22479. }
  22480. }
  22481. }
  22482. if (initData === null) {
  22483. this.log("Skipping key-session request for \"" + reason + "\" (no initData)");
  22484. return Promise.resolve(context);
  22485. }
  22486. var keyId = this.getKeyIdString(context.decryptdata);
  22487. this.log("Generating key-session request for \"" + reason + "\": " + keyId + " (init data type: " + initDataType + " length: " + (initData ? initData.byteLength : null) + ")");
  22488. var licenseStatus = new EventEmitter();
  22489. var onmessage = context._onmessage = function (event) {
  22490. var keySession = context.mediaKeysSession;
  22491. if (!keySession) {
  22492. licenseStatus.emit('error', new Error('invalid state'));
  22493. return;
  22494. }
  22495. var messageType = event.messageType,
  22496. message = event.message;
  22497. _this7.log("\"" + messageType + "\" message event for session \"" + keySession.sessionId + "\" message size: " + message.byteLength);
  22498. if (messageType === 'license-request' || messageType === 'license-renewal') {
  22499. _this7.renewLicense(context, message).catch(function (error) {
  22500. _this7.handleError(error);
  22501. licenseStatus.emit('error', error);
  22502. });
  22503. } else if (messageType === 'license-release') {
  22504. if (context.keySystem === KeySystems.FAIRPLAY) {
  22505. _this7.updateKeySession(context, strToUtf8array('acknowledged'));
  22506. _this7.removeSession(context);
  22507. }
  22508. } else {
  22509. _this7.warn("unhandled media key message type \"" + messageType + "\"");
  22510. }
  22511. };
  22512. var onkeystatuseschange = context._onkeystatuseschange = function (event) {
  22513. var keySession = context.mediaKeysSession;
  22514. if (!keySession) {
  22515. licenseStatus.emit('error', new Error('invalid state'));
  22516. return;
  22517. }
  22518. _this7.onKeyStatusChange(context);
  22519. var keyStatus = context.keyStatus;
  22520. licenseStatus.emit('keyStatus', keyStatus);
  22521. if (keyStatus === 'expired') {
  22522. _this7.warn(context.keySystem + " expired for key " + keyId);
  22523. _this7.renewKeySession(context);
  22524. }
  22525. };
  22526. context.mediaKeysSession.addEventListener('message', onmessage);
  22527. context.mediaKeysSession.addEventListener('keystatuseschange', onkeystatuseschange);
  22528. var keyUsablePromise = new Promise(function (resolve, reject) {
  22529. licenseStatus.on('error', reject);
  22530. licenseStatus.on('keyStatus', function (keyStatus) {
  22531. if (keyStatus.startsWith('usable')) {
  22532. resolve();
  22533. } else if (keyStatus === 'output-restricted') {
  22534. reject(new EMEKeyError({
  22535. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22536. details: ErrorDetails.KEY_SYSTEM_STATUS_OUTPUT_RESTRICTED,
  22537. fatal: false
  22538. }, 'HDCP level output restricted'));
  22539. } else if (keyStatus === 'internal-error') {
  22540. reject(new EMEKeyError({
  22541. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22542. details: ErrorDetails.KEY_SYSTEM_STATUS_INTERNAL_ERROR,
  22543. fatal: true
  22544. }, "key status changed to \"" + keyStatus + "\""));
  22545. } else if (keyStatus === 'expired') {
  22546. reject(new Error('key expired while generating request'));
  22547. } else {
  22548. _this7.warn("unhandled key status change \"" + keyStatus + "\"");
  22549. }
  22550. });
  22551. });
  22552. return context.mediaKeysSession.generateRequest(initDataType, initData).then(function () {
  22553. var _context$mediaKeysSes;
  22554. _this7.log("Request generated for key-session \"" + ((_context$mediaKeysSes = context.mediaKeysSession) == null ? void 0 : _context$mediaKeysSes.sessionId) + "\" keyId: " + keyId);
  22555. }).catch(function (error) {
  22556. throw new EMEKeyError({
  22557. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22558. details: ErrorDetails.KEY_SYSTEM_NO_SESSION,
  22559. error: error,
  22560. fatal: false
  22561. }, "Error generating key-session request: " + error);
  22562. }).then(function () {
  22563. return keyUsablePromise;
  22564. }).catch(function (error) {
  22565. licenseStatus.removeAllListeners();
  22566. _this7.removeSession(context);
  22567. throw error;
  22568. }).then(function () {
  22569. licenseStatus.removeAllListeners();
  22570. return context;
  22571. });
  22572. };
  22573. _proto.onKeyStatusChange = function onKeyStatusChange(mediaKeySessionContext) {
  22574. var _this8 = this;
  22575. mediaKeySessionContext.mediaKeysSession.keyStatuses.forEach(function (status, keyId) {
  22576. _this8.log("key status change \"" + status + "\" for keyStatuses keyId: " + Hex.hexDump('buffer' in keyId ? new Uint8Array(keyId.buffer, keyId.byteOffset, keyId.byteLength) : new Uint8Array(keyId)) + " session keyId: " + Hex.hexDump(new Uint8Array(mediaKeySessionContext.decryptdata.keyId || [])) + " uri: " + mediaKeySessionContext.decryptdata.uri);
  22577. mediaKeySessionContext.keyStatus = status;
  22578. });
  22579. };
  22580. _proto.fetchServerCertificate = function fetchServerCertificate(keySystem) {
  22581. var config = this.config;
  22582. var Loader = config.loader;
  22583. var certLoader = new Loader(config);
  22584. var url = this.getServerCertificateUrl(keySystem);
  22585. if (!url) {
  22586. return Promise.resolve();
  22587. }
  22588. this.log("Fetching server certificate for \"" + keySystem + "\"");
  22589. return new Promise(function (resolve, reject) {
  22590. var loaderContext = {
  22591. responseType: 'arraybuffer',
  22592. url: url
  22593. };
  22594. var loadPolicy = config.certLoadPolicy.default;
  22595. var loaderConfig = {
  22596. loadPolicy: loadPolicy,
  22597. timeout: loadPolicy.maxLoadTimeMs,
  22598. maxRetry: 0,
  22599. retryDelay: 0,
  22600. maxRetryDelay: 0
  22601. };
  22602. var loaderCallbacks = {
  22603. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  22604. resolve(response.data);
  22605. },
  22606. onError: function onError(response, contex, networkDetails, stats) {
  22607. reject(new EMEKeyError({
  22608. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22609. details: ErrorDetails.KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED,
  22610. fatal: true,
  22611. networkDetails: networkDetails,
  22612. response: _objectSpread2({
  22613. url: loaderContext.url,
  22614. data: undefined
  22615. }, response)
  22616. }, "\"" + keySystem + "\" certificate request failed (" + url + "). Status: " + response.code + " (" + response.text + ")"));
  22617. },
  22618. onTimeout: function onTimeout(stats, context, networkDetails) {
  22619. reject(new EMEKeyError({
  22620. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22621. details: ErrorDetails.KEY_SYSTEM_SERVER_CERTIFICATE_REQUEST_FAILED,
  22622. fatal: true,
  22623. networkDetails: networkDetails,
  22624. response: {
  22625. url: loaderContext.url,
  22626. data: undefined
  22627. }
  22628. }, "\"" + keySystem + "\" certificate request timed out (" + url + ")"));
  22629. },
  22630. onAbort: function onAbort(stats, context, networkDetails) {
  22631. reject(new Error('aborted'));
  22632. }
  22633. };
  22634. certLoader.load(loaderContext, loaderConfig, loaderCallbacks);
  22635. });
  22636. };
  22637. _proto.setMediaKeysServerCertificate = function setMediaKeysServerCertificate(mediaKeys, keySystem, cert) {
  22638. var _this9 = this;
  22639. return new Promise(function (resolve, reject) {
  22640. mediaKeys.setServerCertificate(cert).then(function (success) {
  22641. _this9.log("setServerCertificate " + (success ? 'success' : 'not supported by CDM') + " (" + (cert == null ? void 0 : cert.byteLength) + ") on \"" + keySystem + "\"");
  22642. resolve(mediaKeys);
  22643. }).catch(function (error) {
  22644. reject(new EMEKeyError({
  22645. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22646. details: ErrorDetails.KEY_SYSTEM_SERVER_CERTIFICATE_UPDATE_FAILED,
  22647. error: error,
  22648. fatal: true
  22649. }, error.message));
  22650. });
  22651. });
  22652. };
  22653. _proto.renewLicense = function renewLicense(context, keyMessage) {
  22654. var _this10 = this;
  22655. return this.requestLicense(context, new Uint8Array(keyMessage)).then(function (data) {
  22656. return _this10.updateKeySession(context, new Uint8Array(data)).catch(function (error) {
  22657. throw new EMEKeyError({
  22658. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22659. details: ErrorDetails.KEY_SYSTEM_SESSION_UPDATE_FAILED,
  22660. error: error,
  22661. fatal: true
  22662. }, error.message);
  22663. });
  22664. });
  22665. };
  22666. _proto.unpackPlayReadyKeyMessage = function unpackPlayReadyKeyMessage(xhr, licenseChallenge) {
  22667. // On Edge, the raw license message is UTF-16-encoded XML. We need
  22668. // to unpack the Challenge element (base64-encoded string containing the
  22669. // actual license request) and any HttpHeader elements (sent as request
  22670. // headers).
  22671. // For PlayReady CDMs, we need to dig the Challenge out of the XML.
  22672. var xmlString = String.fromCharCode.apply(null, new Uint16Array(licenseChallenge.buffer));
  22673. if (!xmlString.includes('PlayReadyKeyMessage')) {
  22674. // This does not appear to be a wrapped message as on Edge. Some
  22675. // clients do not need this unwrapping, so we will assume this is one of
  22676. // them. Note that "xml" at this point probably looks like random
  22677. // garbage, since we interpreted UTF-8 as UTF-16.
  22678. xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
  22679. return licenseChallenge;
  22680. }
  22681. var keyMessageXml = new DOMParser().parseFromString(xmlString, 'application/xml');
  22682. // Set request headers.
  22683. var headers = keyMessageXml.querySelectorAll('HttpHeader');
  22684. if (headers.length > 0) {
  22685. var header;
  22686. for (var i = 0, len = headers.length; i < len; i++) {
  22687. var _header$querySelector, _header$querySelector2;
  22688. header = headers[i];
  22689. var name = (_header$querySelector = header.querySelector('name')) == null ? void 0 : _header$querySelector.textContent;
  22690. var _value = (_header$querySelector2 = header.querySelector('value')) == null ? void 0 : _header$querySelector2.textContent;
  22691. if (name && _value) {
  22692. xhr.setRequestHeader(name, _value);
  22693. }
  22694. }
  22695. }
  22696. var challengeElement = keyMessageXml.querySelector('Challenge');
  22697. var challengeText = challengeElement == null ? void 0 : challengeElement.textContent;
  22698. if (!challengeText) {
  22699. throw new Error("Cannot find <Challenge> in key message");
  22700. }
  22701. return strToUtf8array(atob(challengeText));
  22702. };
  22703. _proto.setupLicenseXHR = function setupLicenseXHR(xhr, url, keysListItem, licenseChallenge) {
  22704. var _this11 = this;
  22705. var licenseXhrSetup = this.config.licenseXhrSetup;
  22706. if (!licenseXhrSetup) {
  22707. xhr.open('POST', url, true);
  22708. return Promise.resolve({
  22709. xhr: xhr,
  22710. licenseChallenge: licenseChallenge
  22711. });
  22712. }
  22713. return Promise.resolve().then(function () {
  22714. if (!keysListItem.decryptdata) {
  22715. throw new Error('Key removed');
  22716. }
  22717. return licenseXhrSetup.call(_this11.hls, xhr, url, keysListItem, licenseChallenge);
  22718. }).catch(function (error) {
  22719. if (!keysListItem.decryptdata) {
  22720. // Key session removed. Cancel license request.
  22721. throw error;
  22722. }
  22723. // let's try to open before running setup
  22724. xhr.open('POST', url, true);
  22725. return licenseXhrSetup.call(_this11.hls, xhr, url, keysListItem, licenseChallenge);
  22726. }).then(function (licenseXhrSetupResult) {
  22727. // if licenseXhrSetup did not yet call open, let's do it now
  22728. if (!xhr.readyState) {
  22729. xhr.open('POST', url, true);
  22730. }
  22731. var finalLicenseChallenge = licenseXhrSetupResult ? licenseXhrSetupResult : licenseChallenge;
  22732. return {
  22733. xhr: xhr,
  22734. licenseChallenge: finalLicenseChallenge
  22735. };
  22736. });
  22737. };
  22738. _proto.requestLicense = function requestLicense(keySessionContext, licenseChallenge) {
  22739. var _this12 = this;
  22740. var keyLoadPolicy = this.config.keyLoadPolicy.default;
  22741. return new Promise(function (resolve, reject) {
  22742. var url = _this12.getLicenseServerUrl(keySessionContext.keySystem);
  22743. _this12.log("Sending license request to URL: " + url);
  22744. var xhr = new XMLHttpRequest();
  22745. xhr.responseType = 'arraybuffer';
  22746. xhr.onreadystatechange = function () {
  22747. if (!_this12.hls || !keySessionContext.mediaKeysSession) {
  22748. return reject(new Error('invalid state'));
  22749. }
  22750. if (xhr.readyState === 4) {
  22751. if (xhr.status === 200) {
  22752. _this12._requestLicenseFailureCount = 0;
  22753. var data = xhr.response;
  22754. _this12.log("License received " + (data instanceof ArrayBuffer ? data.byteLength : data));
  22755. var licenseResponseCallback = _this12.config.licenseResponseCallback;
  22756. if (licenseResponseCallback) {
  22757. try {
  22758. data = licenseResponseCallback.call(_this12.hls, xhr, url, keySessionContext);
  22759. } catch (error) {
  22760. _this12.error(error);
  22761. }
  22762. }
  22763. resolve(data);
  22764. } else {
  22765. var retryConfig = keyLoadPolicy.errorRetry;
  22766. var maxNumRetry = retryConfig ? retryConfig.maxNumRetry : 0;
  22767. _this12._requestLicenseFailureCount++;
  22768. if (_this12._requestLicenseFailureCount > maxNumRetry || xhr.status >= 400 && xhr.status < 500) {
  22769. reject(new EMEKeyError({
  22770. type: ErrorTypes.KEY_SYSTEM_ERROR,
  22771. details: ErrorDetails.KEY_SYSTEM_LICENSE_REQUEST_FAILED,
  22772. fatal: true,
  22773. networkDetails: xhr,
  22774. response: {
  22775. url: url,
  22776. data: undefined,
  22777. code: xhr.status,
  22778. text: xhr.statusText
  22779. }
  22780. }, "License Request XHR failed (" + url + "). Status: " + xhr.status + " (" + xhr.statusText + ")"));
  22781. } else {
  22782. var attemptsLeft = maxNumRetry - _this12._requestLicenseFailureCount + 1;
  22783. _this12.warn("Retrying license request, " + attemptsLeft + " attempts left");
  22784. _this12.requestLicense(keySessionContext, licenseChallenge).then(resolve, reject);
  22785. }
  22786. }
  22787. }
  22788. };
  22789. if (keySessionContext.licenseXhr && keySessionContext.licenseXhr.readyState !== XMLHttpRequest.DONE) {
  22790. keySessionContext.licenseXhr.abort();
  22791. }
  22792. keySessionContext.licenseXhr = xhr;
  22793. _this12.setupLicenseXHR(xhr, url, keySessionContext, licenseChallenge).then(function (_ref5) {
  22794. var xhr = _ref5.xhr,
  22795. licenseChallenge = _ref5.licenseChallenge;
  22796. if (keySessionContext.keySystem == KeySystems.PLAYREADY) {
  22797. licenseChallenge = _this12.unpackPlayReadyKeyMessage(xhr, licenseChallenge);
  22798. }
  22799. xhr.send(licenseChallenge);
  22800. });
  22801. });
  22802. };
  22803. _proto.onMediaAttached = function onMediaAttached(event, data) {
  22804. if (!this.config.emeEnabled) {
  22805. return;
  22806. }
  22807. var media = data.media;
  22808. // keep reference of media
  22809. this.media = media;
  22810. media.addEventListener('encrypted', this.onMediaEncrypted);
  22811. media.addEventListener('waitingforkey', this.onWaitingForKey);
  22812. };
  22813. _proto.onMediaDetached = function onMediaDetached() {
  22814. var _this13 = this;
  22815. var media = this.media;
  22816. var mediaKeysList = this.mediaKeySessions;
  22817. if (media) {
  22818. media.removeEventListener('encrypted', this.onMediaEncrypted);
  22819. media.removeEventListener('waitingforkey', this.onWaitingForKey);
  22820. this.media = null;
  22821. }
  22822. this._requestLicenseFailureCount = 0;
  22823. this.setMediaKeysQueue = [];
  22824. this.mediaKeySessions = [];
  22825. this.keyIdToKeySessionPromise = {};
  22826. LevelKey.clearKeyUriToKeyIdMap();
  22827. // Close all sessions and remove media keys from the video element.
  22828. var keySessionCount = mediaKeysList.length;
  22829. EMEController.CDMCleanupPromise = Promise.all(mediaKeysList.map(function (mediaKeySessionContext) {
  22830. return _this13.removeSession(mediaKeySessionContext);
  22831. }).concat(media == null ? void 0 : media.setMediaKeys(null).catch(function (error) {
  22832. _this13.log("Could not clear media keys: " + error);
  22833. }))).then(function () {
  22834. if (keySessionCount) {
  22835. _this13.log('finished closing key sessions and clearing media keys');
  22836. mediaKeysList.length = 0;
  22837. }
  22838. }).catch(function (error) {
  22839. _this13.log("Could not close sessions and clear media keys: " + error);
  22840. });
  22841. };
  22842. _proto.onManifestLoading = function onManifestLoading() {
  22843. this.keyFormatPromise = null;
  22844. };
  22845. _proto.onManifestLoaded = function onManifestLoaded(event, _ref6) {
  22846. var sessionKeys = _ref6.sessionKeys;
  22847. if (!sessionKeys || !this.config.emeEnabled) {
  22848. return;
  22849. }
  22850. if (!this.keyFormatPromise) {
  22851. var keyFormats = sessionKeys.reduce(function (formats, sessionKey) {
  22852. if (formats.indexOf(sessionKey.keyFormat) === -1) {
  22853. formats.push(sessionKey.keyFormat);
  22854. }
  22855. return formats;
  22856. }, []);
  22857. this.log("Selecting key-system from session-keys " + keyFormats.join(', '));
  22858. this.keyFormatPromise = this.getKeyFormatPromise(keyFormats);
  22859. }
  22860. };
  22861. _proto.removeSession = function removeSession(mediaKeySessionContext) {
  22862. var _this14 = this;
  22863. var mediaKeysSession = mediaKeySessionContext.mediaKeysSession,
  22864. licenseXhr = mediaKeySessionContext.licenseXhr;
  22865. if (mediaKeysSession) {
  22866. this.log("Remove licenses and keys and close session " + mediaKeysSession.sessionId);
  22867. if (mediaKeySessionContext._onmessage) {
  22868. mediaKeysSession.removeEventListener('message', mediaKeySessionContext._onmessage);
  22869. mediaKeySessionContext._onmessage = undefined;
  22870. }
  22871. if (mediaKeySessionContext._onkeystatuseschange) {
  22872. mediaKeysSession.removeEventListener('keystatuseschange', mediaKeySessionContext._onkeystatuseschange);
  22873. mediaKeySessionContext._onkeystatuseschange = undefined;
  22874. }
  22875. if (licenseXhr && licenseXhr.readyState !== XMLHttpRequest.DONE) {
  22876. licenseXhr.abort();
  22877. }
  22878. mediaKeySessionContext.mediaKeysSession = mediaKeySessionContext.decryptdata = mediaKeySessionContext.licenseXhr = undefined;
  22879. var index = this.mediaKeySessions.indexOf(mediaKeySessionContext);
  22880. if (index > -1) {
  22881. this.mediaKeySessions.splice(index, 1);
  22882. }
  22883. return mediaKeysSession.remove().catch(function (error) {
  22884. _this14.log("Could not remove session: " + error);
  22885. }).then(function () {
  22886. return mediaKeysSession.close();
  22887. }).catch(function (error) {
  22888. _this14.log("Could not close session: " + error);
  22889. });
  22890. }
  22891. };
  22892. return EMEController;
  22893. }();
  22894. EMEController.CDMCleanupPromise = void 0;
  22895. var EMEKeyError = /*#__PURE__*/function (_Error) {
  22896. _inheritsLoose(EMEKeyError, _Error);
  22897. function EMEKeyError(data, message) {
  22898. var _this15;
  22899. _this15 = _Error.call(this, message) || this;
  22900. _this15.data = void 0;
  22901. data.error || (data.error = new Error(message));
  22902. _this15.data = data;
  22903. data.err = data.error;
  22904. return _this15;
  22905. }
  22906. return EMEKeyError;
  22907. }( /*#__PURE__*/_wrapNativeSuper(Error));
  22908. /**
  22909. * Common Media Object Type
  22910. *
  22911. * @group CMCD
  22912. * @group CMSD
  22913. *
  22914. * @beta
  22915. */
  22916. var CmObjectType;
  22917. (function (CmObjectType) {
  22918. /**
  22919. * text file, such as a manifest or playlist
  22920. */
  22921. CmObjectType["MANIFEST"] = "m";
  22922. /**
  22923. * audio only
  22924. */
  22925. CmObjectType["AUDIO"] = "a";
  22926. /**
  22927. * video only
  22928. */
  22929. CmObjectType["VIDEO"] = "v";
  22930. /**
  22931. * muxed audio and video
  22932. */
  22933. CmObjectType["MUXED"] = "av";
  22934. /**
  22935. * init segment
  22936. */
  22937. CmObjectType["INIT"] = "i";
  22938. /**
  22939. * caption or subtitle
  22940. */
  22941. CmObjectType["CAPTION"] = "c";
  22942. /**
  22943. * ISOBMFF timed text track
  22944. */
  22945. CmObjectType["TIMED_TEXT"] = "tt";
  22946. /**
  22947. * cryptographic key, license or certificate.
  22948. */
  22949. CmObjectType["KEY"] = "k";
  22950. /**
  22951. * other
  22952. */
  22953. CmObjectType["OTHER"] = "o";
  22954. })(CmObjectType || (CmObjectType = {}));
  22955. /**
  22956. * Common Media Streaming Format
  22957. *
  22958. * @group CMCD
  22959. * @group CMSD
  22960. *
  22961. * @beta
  22962. */
  22963. var CmStreamingFormat;
  22964. (function (CmStreamingFormat) {
  22965. /**
  22966. * MPEG DASH
  22967. */
  22968. CmStreamingFormat["DASH"] = "d";
  22969. /**
  22970. * HTTP Live Streaming (HLS)
  22971. */
  22972. CmStreamingFormat["HLS"] = "h";
  22973. /**
  22974. * Smooth Streaming
  22975. */
  22976. CmStreamingFormat["SMOOTH"] = "s";
  22977. /**
  22978. * Other
  22979. */
  22980. CmStreamingFormat["OTHER"] = "o";
  22981. })(CmStreamingFormat || (CmStreamingFormat = {}));
  22982. /**
  22983. * CMCD header fields.
  22984. *
  22985. * @group CMCD
  22986. *
  22987. * @beta
  22988. */
  22989. var CmcdHeaderField;
  22990. (function (CmcdHeaderField) {
  22991. /**
  22992. * keys whose values vary with the object being requested.
  22993. */
  22994. CmcdHeaderField["OBJECT"] = "CMCD-Object";
  22995. /**
  22996. * keys whose values vary with each request.
  22997. */
  22998. CmcdHeaderField["REQUEST"] = "CMCD-Request";
  22999. /**
  23000. * keys whose values are expected to be invariant over the life of the session.
  23001. */
  23002. CmcdHeaderField["SESSION"] = "CMCD-Session";
  23003. /**
  23004. * keys whose values do not vary with every request or object.
  23005. */
  23006. CmcdHeaderField["STATUS"] = "CMCD-Status";
  23007. })(CmcdHeaderField || (CmcdHeaderField = {}));
  23008. var _CmcdHeaderMap;
  23009. /**
  23010. * The map of CMCD header fields to official CMCD keys.
  23011. *
  23012. * @internal
  23013. *
  23014. * @group CMCD
  23015. */
  23016. var CmcdHeaderMap = (_CmcdHeaderMap = {}, _CmcdHeaderMap[CmcdHeaderField.OBJECT] = ['br', 'd', 'ot', 'tb'], _CmcdHeaderMap[CmcdHeaderField.REQUEST] = ['bl', 'dl', 'mtp', 'nor', 'nrr', 'su'], _CmcdHeaderMap[CmcdHeaderField.SESSION] = ['cid', 'pr', 'sf', 'sid', 'st', 'v'], _CmcdHeaderMap[CmcdHeaderField.STATUS] = ['bs', 'rtp'], _CmcdHeaderMap);
  23017. /**
  23018. * Structured Field Item
  23019. *
  23020. * @group Structured Field
  23021. *
  23022. * @beta
  23023. */
  23024. var SfItem = function SfItem(value, params) {
  23025. this.value = void 0;
  23026. this.params = void 0;
  23027. if (Array.isArray(value)) {
  23028. value = value.map(function (v) {
  23029. return v instanceof SfItem ? v : new SfItem(v);
  23030. });
  23031. }
  23032. this.value = value;
  23033. this.params = params;
  23034. };
  23035. /**
  23036. * A class to represent structured field tokens when `Symbol` is not available.
  23037. *
  23038. * @group Structured Field
  23039. *
  23040. * @beta
  23041. */
  23042. var SfToken = function SfToken(description) {
  23043. this.description = void 0;
  23044. this.description = description;
  23045. };
  23046. var DICT = 'Dict';
  23047. function format(value) {
  23048. if (Array.isArray(value)) {
  23049. return JSON.stringify(value);
  23050. }
  23051. if (value instanceof Map) {
  23052. return 'Map{}';
  23053. }
  23054. if (value instanceof Set) {
  23055. return 'Set{}';
  23056. }
  23057. if (typeof value === 'object') {
  23058. return JSON.stringify(value);
  23059. }
  23060. return String(value);
  23061. }
  23062. function throwError(action, src, type, cause) {
  23063. return new Error("failed to " + action + " \"" + format(src) + "\" as " + type, {
  23064. cause: cause
  23065. });
  23066. }
  23067. var BARE_ITEM = 'Bare Item';
  23068. var BOOLEAN = 'Boolean';
  23069. var BYTES = 'Byte Sequence';
  23070. var DECIMAL = 'Decimal';
  23071. var INTEGER = 'Integer';
  23072. function isInvalidInt(value) {
  23073. return value < -999999999999999 || 999999999999999 < value;
  23074. }
  23075. var STRING_REGEX = /[\x00-\x1f\x7f]+/; // eslint-disable-line no-control-regex
  23076. var TOKEN = 'Token';
  23077. var KEY = 'Key';
  23078. function serializeError(src, type, cause) {
  23079. return throwError('serialize', src, type, cause);
  23080. }
  23081. // 4.1.9. Serializing a Boolean
  23082. //
  23083. // Given a Boolean as input_boolean, return an ASCII string suitable for
  23084. // use in a HTTP field value.
  23085. //
  23086. // 1. If input_boolean is not a boolean, fail serialization.
  23087. //
  23088. // 2. Let output be an empty string.
  23089. //
  23090. // 3. Append "?" to output.
  23091. //
  23092. // 4. If input_boolean is true, append "1" to output.
  23093. //
  23094. // 5. If input_boolean is false, append "0" to output.
  23095. //
  23096. // 6. Return output.
  23097. function serializeBoolean(value) {
  23098. if (typeof value !== 'boolean') {
  23099. throw serializeError(value, BOOLEAN);
  23100. }
  23101. return value ? '?1' : '?0';
  23102. }
  23103. /**
  23104. * Encodes binary data to base64
  23105. *
  23106. * @param binary - The binary data to encode
  23107. * @returns The base64 encoded string
  23108. *
  23109. * @group Utils
  23110. *
  23111. * @beta
  23112. */
  23113. function base64encode(binary) {
  23114. return btoa(String.fromCharCode.apply(String, binary));
  23115. }
  23116. // 4.1.8. Serializing a Byte Sequence
  23117. //
  23118. // Given a Byte Sequence as input_bytes, return an ASCII string suitable
  23119. // for use in a HTTP field value.
  23120. //
  23121. // 1. If input_bytes is not a sequence of bytes, fail serialization.
  23122. //
  23123. // 2. Let output be an empty string.
  23124. //
  23125. // 3. Append ":" to output.
  23126. //
  23127. // 4. Append the result of base64-encoding input_bytes as per
  23128. // [RFC4648], Section 4, taking account of the requirements below.
  23129. //
  23130. // 5. Append ":" to output.
  23131. //
  23132. // 6. Return output.
  23133. //
  23134. // The encoded data is required to be padded with "=", as per [RFC4648],
  23135. // Section 3.2.
  23136. //
  23137. // Likewise, encoded data SHOULD have pad bits set to zero, as per
  23138. // [RFC4648], Section 3.5, unless it is not possible to do so due to
  23139. // implementation constraints.
  23140. function serializeByteSequence(value) {
  23141. if (ArrayBuffer.isView(value) === false) {
  23142. throw serializeError(value, BYTES);
  23143. }
  23144. return ":" + base64encode(value) + ":";
  23145. }
  23146. // 4.1.4. Serializing an Integer
  23147. //
  23148. // Given an Integer as input_integer, return an ASCII string suitable
  23149. // for use in a HTTP field value.
  23150. //
  23151. // 1. If input_integer is not an integer in the range of
  23152. // -999,999,999,999,999 to 999,999,999,999,999 inclusive, fail
  23153. // serialization.
  23154. //
  23155. // 2. Let output be an empty string.
  23156. //
  23157. // 3. If input_integer is less than (but not equal to) 0, append "-" to
  23158. // output.
  23159. //
  23160. // 4. Append input_integer's numeric value represented in base 10 using
  23161. // only decimal digits to output.
  23162. //
  23163. // 5. Return output.
  23164. function serializeInteger(value) {
  23165. if (isInvalidInt(value)) {
  23166. throw serializeError(value, INTEGER);
  23167. }
  23168. return value.toString();
  23169. }
  23170. // 4.1.10. Serializing a Date
  23171. //
  23172. // Given a Date as input_integer, return an ASCII string suitable for
  23173. // use in an HTTP field value.
  23174. // 1. Let output be "@".
  23175. // 2. Append to output the result of running Serializing an Integer
  23176. // with input_date (Section 4.1.4).
  23177. // 3. Return output.
  23178. function serializeDate(value) {
  23179. return "@" + serializeInteger(value.getTime() / 1000);
  23180. }
  23181. /**
  23182. * This implements the rounding procedure described in step 2 of the "Serializing a Decimal" specification.
  23183. * This rounding style is known as "even rounding", "banker's rounding", or "commercial rounding".
  23184. *
  23185. * @param value - The value to round
  23186. * @param precision - The number of decimal places to round to
  23187. * @returns The rounded value
  23188. *
  23189. * @group Utils
  23190. *
  23191. * @beta
  23192. */
  23193. function roundToEven(value, precision) {
  23194. if (value < 0) {
  23195. return -roundToEven(-value, precision);
  23196. }
  23197. var decimalShift = Math.pow(10, precision);
  23198. var isEquidistant = Math.abs(value * decimalShift % 1 - 0.5) < Number.EPSILON;
  23199. if (isEquidistant) {
  23200. // If the tail of the decimal place is 'equidistant' we round to the nearest even value
  23201. var flooredValue = Math.floor(value * decimalShift);
  23202. return (flooredValue % 2 === 0 ? flooredValue : flooredValue + 1) / decimalShift;
  23203. } else {
  23204. // Otherwise, proceed as normal
  23205. return Math.round(value * decimalShift) / decimalShift;
  23206. }
  23207. }
  23208. // 4.1.5. Serializing a Decimal
  23209. //
  23210. // Given a decimal number as input_decimal, return an ASCII string
  23211. // suitable for use in a HTTP field value.
  23212. //
  23213. // 1. If input_decimal is not a decimal number, fail serialization.
  23214. //
  23215. // 2. If input_decimal has more than three significant digits to the
  23216. // right of the decimal point, round it to three decimal places,
  23217. // rounding the final digit to the nearest value, or to the even
  23218. // value if it is equidistant.
  23219. //
  23220. // 3. If input_decimal has more than 12 significant digits to the left
  23221. // of the decimal point after rounding, fail serialization.
  23222. //
  23223. // 4. Let output be an empty string.
  23224. //
  23225. // 5. If input_decimal is less than (but not equal to) 0, append "-"
  23226. // to output.
  23227. //
  23228. // 6. Append input_decimal's integer component represented in base 10
  23229. // (using only decimal digits) to output; if it is zero, append
  23230. // "0".
  23231. //
  23232. // 7. Append "." to output.
  23233. //
  23234. // 8. If input_decimal's fractional component is zero, append "0" to
  23235. // output.
  23236. //
  23237. // 9. Otherwise, append the significant digits of input_decimal's
  23238. // fractional component represented in base 10 (using only decimal
  23239. // digits) to output.
  23240. //
  23241. // 10. Return output.
  23242. function serializeDecimal(value) {
  23243. var roundedValue = roundToEven(value, 3); // round to 3 decimal places
  23244. if (Math.floor(Math.abs(roundedValue)).toString().length > 12) {
  23245. throw serializeError(value, DECIMAL);
  23246. }
  23247. var stringValue = roundedValue.toString();
  23248. return stringValue.includes('.') ? stringValue : stringValue + ".0";
  23249. }
  23250. var STRING = 'String';
  23251. // 4.1.6. Serializing a String
  23252. //
  23253. // Given a String as input_string, return an ASCII string suitable for
  23254. // use in a HTTP field value.
  23255. //
  23256. // 1. Convert input_string into a sequence of ASCII characters; if
  23257. // conversion fails, fail serialization.
  23258. //
  23259. // 2. If input_string contains characters in the range %x00-1f or %x7f
  23260. // (i.e., not in VCHAR or SP), fail serialization.
  23261. //
  23262. // 3. Let output be the string DQUOTE.
  23263. //
  23264. // 4. For each character char in input_string:
  23265. //
  23266. // 1. If char is "\" or DQUOTE:
  23267. //
  23268. // 1. Append "\" to output.
  23269. //
  23270. // 2. Append char to output.
  23271. //
  23272. // 5. Append DQUOTE to output.
  23273. //
  23274. // 6. Return output.
  23275. function serializeString(value) {
  23276. if (STRING_REGEX.test(value)) {
  23277. throw serializeError(value, STRING);
  23278. }
  23279. return "\"" + value.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";
  23280. }
  23281. function symbolToStr(symbol) {
  23282. return symbol.description || symbol.toString().slice(7, -1);
  23283. }
  23284. function serializeToken(token) {
  23285. var value = symbolToStr(token);
  23286. if (/^([a-zA-Z*])([!#$%&'*+\-.^_`|~\w:/]*)$/.test(value) === false) {
  23287. throw serializeError(value, TOKEN);
  23288. }
  23289. return value;
  23290. }
  23291. // 4.1.3.1. Serializing a Bare Item
  23292. //
  23293. // Given an Item as input_item, return an ASCII string suitable for use
  23294. // in a HTTP field value.
  23295. //
  23296. // 1. If input_item is an Integer, return the result of running
  23297. // Serializing an Integer (Section 4.1.4) with input_item.
  23298. //
  23299. // 2. If input_item is a Decimal, return the result of running
  23300. // Serializing a Decimal (Section 4.1.5) with input_item.
  23301. //
  23302. // 3. If input_item is a String, return the result of running
  23303. // Serializing a String (Section 4.1.6) with input_item.
  23304. //
  23305. // 4. If input_item is a Token, return the result of running
  23306. // Serializing a Token (Section 4.1.7) with input_item.
  23307. //
  23308. // 5. If input_item is a Boolean, return the result of running
  23309. // Serializing a Boolean (Section 4.1.9) with input_item.
  23310. //
  23311. // 6. If input_item is a Byte Sequence, return the result of running
  23312. // Serializing a Byte Sequence (Section 4.1.8) with input_item.
  23313. //
  23314. // 7. If input_item is a Date, return the result of running Serializing
  23315. // a Date (Section 4.1.10) with input_item.
  23316. //
  23317. // 8. Otherwise, fail serialization.
  23318. function serializeBareItem(value) {
  23319. switch (typeof value) {
  23320. case 'number':
  23321. if (!isFiniteNumber(value)) {
  23322. throw serializeError(value, BARE_ITEM);
  23323. }
  23324. if (Number.isInteger(value)) {
  23325. return serializeInteger(value);
  23326. }
  23327. return serializeDecimal(value);
  23328. case 'string':
  23329. return serializeString(value);
  23330. case 'symbol':
  23331. return serializeToken(value);
  23332. case 'boolean':
  23333. return serializeBoolean(value);
  23334. case 'object':
  23335. if (value instanceof Date) {
  23336. return serializeDate(value);
  23337. }
  23338. if (value instanceof Uint8Array) {
  23339. return serializeByteSequence(value);
  23340. }
  23341. if (value instanceof SfToken) {
  23342. return serializeToken(value);
  23343. }
  23344. default:
  23345. // fail
  23346. throw serializeError(value, BARE_ITEM);
  23347. }
  23348. }
  23349. // 4.1.1.3. Serializing a Key
  23350. //
  23351. // Given a key as input_key, return an ASCII string suitable for use in
  23352. // a HTTP field value.
  23353. //
  23354. // 1. Convert input_key into a sequence of ASCII characters; if
  23355. // conversion fails, fail serialization.
  23356. //
  23357. // 2. If input_key contains characters not in lcalpha, DIGIT, "_", "-",
  23358. // ".", or "*" fail serialization.
  23359. //
  23360. // 3. If the first character of input_key is not lcalpha or "*", fail
  23361. // serialization.
  23362. //
  23363. // 4. Let output be an empty string.
  23364. //
  23365. // 5. Append input_key to output.
  23366. //
  23367. // 6. Return output.
  23368. function serializeKey(value) {
  23369. if (/^[a-z*][a-z0-9\-_.*]*$/.test(value) === false) {
  23370. throw serializeError(value, KEY);
  23371. }
  23372. return value;
  23373. }
  23374. // 4.1.1.2. Serializing Parameters
  23375. //
  23376. // Given an ordered Dictionary as input_parameters (each member having a
  23377. // param_name and a param_value), return an ASCII string suitable for
  23378. // use in a HTTP field value.
  23379. //
  23380. // 1. Let output be an empty string.
  23381. //
  23382. // 2. For each param_name with a value of param_value in
  23383. // input_parameters:
  23384. //
  23385. // 1. Append ";" to output.
  23386. //
  23387. // 2. Append the result of running Serializing a Key
  23388. // (Section 4.1.1.3) with param_name to output.
  23389. //
  23390. // 3. If param_value is not Boolean true:
  23391. //
  23392. // 1. Append "=" to output.
  23393. //
  23394. // 2. Append the result of running Serializing a bare Item
  23395. // (Section 4.1.3.1) with param_value to output.
  23396. //
  23397. // 3. Return output.
  23398. function serializeParams(params) {
  23399. if (params == null) {
  23400. return '';
  23401. }
  23402. return Object.entries(params).map(function (_ref) {
  23403. var key = _ref[0],
  23404. value = _ref[1];
  23405. if (value === true) {
  23406. return ";" + serializeKey(key); // omit true
  23407. }
  23408. return ";" + serializeKey(key) + "=" + serializeBareItem(value);
  23409. }).join('');
  23410. }
  23411. // 4.1.3. Serializing an Item
  23412. //
  23413. // Given an Item as bare_item and Parameters as item_parameters, return
  23414. // an ASCII string suitable for use in a HTTP field value.
  23415. //
  23416. // 1. Let output be an empty string.
  23417. //
  23418. // 2. Append the result of running Serializing a Bare Item
  23419. // Section 4.1.3.1 with bare_item to output.
  23420. //
  23421. // 3. Append the result of running Serializing Parameters
  23422. // Section 4.1.1.2 with item_parameters to output.
  23423. //
  23424. // 4. Return output.
  23425. function serializeItem(value) {
  23426. if (value instanceof SfItem) {
  23427. return "" + serializeBareItem(value.value) + serializeParams(value.params);
  23428. } else {
  23429. return serializeBareItem(value);
  23430. }
  23431. }
  23432. // 4.1.1.1. Serializing an Inner List
  23433. //
  23434. // Given an array of (member_value, parameters) tuples as inner_list,
  23435. // and parameters as list_parameters, return an ASCII string suitable
  23436. // for use in a HTTP field value.
  23437. //
  23438. // 1. Let output be the string "(".
  23439. //
  23440. // 2. For each (member_value, parameters) of inner_list:
  23441. //
  23442. // 1. Append the result of running Serializing an Item
  23443. // (Section 4.1.3) with (member_value, parameters) to output.
  23444. //
  23445. // 2. If more values remain in inner_list, append a single SP to
  23446. // output.
  23447. //
  23448. // 3. Append ")" to output.
  23449. //
  23450. // 4. Append the result of running Serializing Parameters
  23451. // (Section 4.1.1.2) with list_parameters to output.
  23452. //
  23453. // 5. Return output.
  23454. function serializeInnerList(value) {
  23455. return "(" + value.value.map(serializeItem).join(' ') + ")" + serializeParams(value.params);
  23456. }
  23457. // 4.1.2. Serializing a Dictionary
  23458. //
  23459. // Given an ordered Dictionary as input_dictionary (each member having a
  23460. // member_name and a tuple value of (member_value, parameters)), return
  23461. // an ASCII string suitable for use in a HTTP field value.
  23462. //
  23463. // 1. Let output be an empty string.
  23464. //
  23465. // 2. For each member_name with a value of (member_value, parameters)
  23466. // in input_dictionary:
  23467. //
  23468. // 1. Append the result of running Serializing a Key
  23469. // (Section 4.1.1.3) with member's member_name to output.
  23470. //
  23471. // 2. If member_value is Boolean true:
  23472. //
  23473. // 1. Append the result of running Serializing Parameters
  23474. // (Section 4.1.1.2) with parameters to output.
  23475. //
  23476. // 3. Otherwise:
  23477. //
  23478. // 1. Append "=" to output.
  23479. //
  23480. // 2. If member_value is an array, append the result of running
  23481. // Serializing an Inner List (Section 4.1.1.1) with
  23482. // (member_value, parameters) to output.
  23483. //
  23484. // 3. Otherwise, append the result of running Serializing an
  23485. // Item (Section 4.1.3) with (member_value, parameters) to
  23486. // output.
  23487. //
  23488. // 4. If more members remain in input_dictionary:
  23489. //
  23490. // 1. Append "," to output.
  23491. //
  23492. // 2. Append a single SP to output.
  23493. //
  23494. // 3. Return output.
  23495. function serializeDict(dict, options) {
  23496. var _options;
  23497. if (options === void 0) {
  23498. options = {
  23499. whitespace: true
  23500. };
  23501. }
  23502. if (typeof dict !== 'object') {
  23503. throw serializeError(dict, DICT);
  23504. }
  23505. var entries = dict instanceof Map ? dict.entries() : Object.entries(dict);
  23506. var optionalWhiteSpace = (_options = options) != null && _options.whitespace ? ' ' : '';
  23507. return Array.from(entries).map(function (_ref) {
  23508. var key = _ref[0],
  23509. item = _ref[1];
  23510. if (item instanceof SfItem === false) {
  23511. item = new SfItem(item);
  23512. }
  23513. var output = serializeKey(key);
  23514. if (item.value === true) {
  23515. output += serializeParams(item.params);
  23516. } else {
  23517. output += '=';
  23518. if (Array.isArray(item.value)) {
  23519. output += serializeInnerList(item);
  23520. } else {
  23521. output += serializeItem(item);
  23522. }
  23523. }
  23524. return output;
  23525. }).join("," + optionalWhiteSpace);
  23526. }
  23527. /**
  23528. * Encode an object into a structured field dictionary
  23529. *
  23530. * @param input - The structured field dictionary to encode
  23531. * @returns The structured field string
  23532. *
  23533. * @group Structured Field
  23534. *
  23535. * @beta
  23536. */
  23537. function encodeSfDict(value, options) {
  23538. return serializeDict(value, options);
  23539. }
  23540. /**
  23541. * Checks if the given key is a token field.
  23542. *
  23543. * @param key - The key to check.
  23544. *
  23545. * @returns `true` if the key is a token field.
  23546. *
  23547. * @internal
  23548. *
  23549. * @group CMCD
  23550. */
  23551. var isTokenField = function isTokenField(key) {
  23552. return key === 'ot' || key === 'sf' || key === 'st';
  23553. };
  23554. var isValid = function isValid(value) {
  23555. if (typeof value === 'number') {
  23556. return isFiniteNumber(value);
  23557. }
  23558. return value != null && value !== '' && value !== false;
  23559. };
  23560. /**
  23561. * Constructs a relative path from a URL.
  23562. *
  23563. * @param url - The destination URL
  23564. * @param base - The base URL
  23565. * @returns The relative path
  23566. *
  23567. * @group Utils
  23568. *
  23569. * @beta
  23570. */
  23571. function urlToRelativePath(url, base) {
  23572. var to = new URL(url);
  23573. var from = new URL(base);
  23574. if (to.origin !== from.origin) {
  23575. return url;
  23576. }
  23577. var toPath = to.pathname.split('/').slice(1);
  23578. var fromPath = from.pathname.split('/').slice(1, -1);
  23579. // remove common parents
  23580. while (toPath[0] === fromPath[0]) {
  23581. toPath.shift();
  23582. fromPath.shift();
  23583. }
  23584. // add back paths
  23585. while (fromPath.length) {
  23586. fromPath.shift();
  23587. toPath.unshift('..');
  23588. }
  23589. return toPath.join('/');
  23590. }
  23591. /**
  23592. * Generate a random v4 UUID
  23593. *
  23594. * @returns A random v4 UUID
  23595. *
  23596. * @group Utils
  23597. *
  23598. * @beta
  23599. */
  23600. function uuid() {
  23601. try {
  23602. return crypto.randomUUID();
  23603. } catch (error) {
  23604. try {
  23605. var url = URL.createObjectURL(new Blob());
  23606. var _uuid = url.toString();
  23607. URL.revokeObjectURL(url);
  23608. return _uuid.slice(_uuid.lastIndexOf('/') + 1);
  23609. } catch (error) {
  23610. var dt = new Date().getTime();
  23611. var _uuid2 = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  23612. var r = (dt + Math.random() * 16) % 16 | 0;
  23613. dt = Math.floor(dt / 16);
  23614. return (c == 'x' ? r : r & 0x3 | 0x8).toString(16);
  23615. });
  23616. return _uuid2;
  23617. }
  23618. }
  23619. }
  23620. var toRounded = function toRounded(value) {
  23621. return Math.round(value);
  23622. };
  23623. var toUrlSafe = function toUrlSafe(value, options) {
  23624. if (options != null && options.baseUrl) {
  23625. value = urlToRelativePath(value, options.baseUrl);
  23626. }
  23627. return encodeURIComponent(value);
  23628. };
  23629. var toHundred = function toHundred(value) {
  23630. return toRounded(value / 100) * 100;
  23631. };
  23632. /**
  23633. * The default formatters for CMCD values.
  23634. *
  23635. * @group CMCD
  23636. *
  23637. * @beta
  23638. */
  23639. var CmcdFormatters = {
  23640. /**
  23641. * Bitrate (kbps) rounded integer
  23642. */
  23643. br: toRounded,
  23644. /**
  23645. * Duration (milliseconds) rounded integer
  23646. */
  23647. d: toRounded,
  23648. /**
  23649. * Buffer Length (milliseconds) rounded nearest 100ms
  23650. */
  23651. bl: toHundred,
  23652. /**
  23653. * Deadline (milliseconds) rounded nearest 100ms
  23654. */
  23655. dl: toHundred,
  23656. /**
  23657. * Measured Throughput (kbps) rounded nearest 100kbps
  23658. */
  23659. mtp: toHundred,
  23660. /**
  23661. * Next Object Request URL encoded
  23662. */
  23663. nor: toUrlSafe,
  23664. /**
  23665. * Requested maximum throughput (kbps) rounded nearest 100kbps
  23666. */
  23667. rtp: toHundred,
  23668. /**
  23669. * Top Bitrate (kbps) rounded integer
  23670. */
  23671. tb: toRounded
  23672. };
  23673. /**
  23674. * Internal CMCD processing function.
  23675. *
  23676. * @param obj - The CMCD object to process.
  23677. * @param map - The mapping function to use.
  23678. * @param options - Options for encoding.
  23679. *
  23680. * @internal
  23681. *
  23682. * @group CMCD
  23683. */
  23684. function processCmcd(obj, options) {
  23685. var results = {};
  23686. if (obj == null || typeof obj !== 'object') {
  23687. return results;
  23688. }
  23689. var keys = Object.keys(obj).sort();
  23690. var formatters = _extends({}, CmcdFormatters, options == null ? void 0 : options.formatters);
  23691. var filter = options == null ? void 0 : options.filter;
  23692. keys.forEach(function (key) {
  23693. if (filter != null && filter(key)) {
  23694. return;
  23695. }
  23696. var value = obj[key];
  23697. var formatter = formatters[key];
  23698. if (formatter) {
  23699. value = formatter(value, options);
  23700. }
  23701. // Version should only be reported if not equal to 1.
  23702. if (key === 'v' && value === 1) {
  23703. return;
  23704. }
  23705. // Playback rate should only be sent if not equal to 1.
  23706. if (key == 'pr' && value === 1) {
  23707. return;
  23708. }
  23709. // ignore invalid values
  23710. if (!isValid(value)) {
  23711. return;
  23712. }
  23713. if (isTokenField(key) && typeof value === 'string') {
  23714. value = new SfToken(value);
  23715. }
  23716. results[key] = value;
  23717. });
  23718. return results;
  23719. }
  23720. /**
  23721. * Encode a CMCD object to a string.
  23722. *
  23723. * @param cmcd - The CMCD object to encode.
  23724. * @param options - Options for encoding.
  23725. *
  23726. * @returns The encoded CMCD string.
  23727. *
  23728. * @group CMCD
  23729. *
  23730. * @beta
  23731. */
  23732. function encodeCmcd(cmcd, options) {
  23733. if (options === void 0) {
  23734. options = {};
  23735. }
  23736. if (!cmcd) {
  23737. return '';
  23738. }
  23739. return encodeSfDict(processCmcd(cmcd, options), _extends({
  23740. whitespace: false
  23741. }, options));
  23742. }
  23743. /**
  23744. * Convert a CMCD data object to request headers
  23745. *
  23746. * @param cmcd - The CMCD data object to convert.
  23747. * @param options - Options for encoding the CMCD object.
  23748. *
  23749. * @returns The CMCD header shards.
  23750. *
  23751. * @group CMCD
  23752. *
  23753. * @beta
  23754. */
  23755. function toCmcdHeaders(cmcd, options) {
  23756. var _options;
  23757. if (options === void 0) {
  23758. options = {};
  23759. }
  23760. if (!cmcd) {
  23761. return {};
  23762. }
  23763. var entries = Object.entries(cmcd);
  23764. var headerMap = Object.entries(CmcdHeaderMap).concat(Object.entries(((_options = options) == null ? void 0 : _options.customHeaderMap) || {}));
  23765. var shards = entries.reduce(function (acc, entry) {
  23766. var _headerMap$find, _acc$field;
  23767. var key = entry[0],
  23768. value = entry[1];
  23769. var field = ((_headerMap$find = headerMap.find(function (entry) {
  23770. return entry[1].includes(key);
  23771. })) == null ? void 0 : _headerMap$find[0]) || CmcdHeaderField.REQUEST;
  23772. (_acc$field = acc[field]) != null ? _acc$field : acc[field] = {};
  23773. acc[field][key] = value;
  23774. return acc;
  23775. }, {});
  23776. return Object.entries(shards).reduce(function (acc, _ref) {
  23777. var field = _ref[0],
  23778. value = _ref[1];
  23779. acc[field] = encodeCmcd(value, options);
  23780. return acc;
  23781. }, {});
  23782. }
  23783. /**
  23784. * Append CMCD query args to a header object.
  23785. *
  23786. * @param headers - The headers to append to.
  23787. * @param cmcd - The CMCD object to append.
  23788. * @param customHeaderMap - A map of custom CMCD keys to header fields.
  23789. *
  23790. * @returns The headers with the CMCD header shards appended.
  23791. *
  23792. * @group CMCD
  23793. *
  23794. * @beta
  23795. */
  23796. function appendCmcdHeaders(headers, cmcd, options) {
  23797. return _extends(headers, toCmcdHeaders(cmcd, options));
  23798. }
  23799. /**
  23800. * CMCD parameter name.
  23801. *
  23802. * @group CMCD
  23803. *
  23804. * @beta
  23805. */
  23806. var CMCD_PARAM = 'CMCD';
  23807. /**
  23808. * Convert a CMCD data object to a query arg.
  23809. *
  23810. * @param cmcd - The CMCD object to convert.
  23811. * @param options - Options for encoding the CMCD object.
  23812. *
  23813. * @returns The CMCD query arg.
  23814. *
  23815. * @group CMCD
  23816. *
  23817. * @beta
  23818. */
  23819. function toCmcdQuery(cmcd, options) {
  23820. if (options === void 0) {
  23821. options = {};
  23822. }
  23823. if (!cmcd) {
  23824. return '';
  23825. }
  23826. var params = encodeCmcd(cmcd, options);
  23827. return CMCD_PARAM + "=" + encodeURIComponent(params);
  23828. }
  23829. var REGEX = /CMCD=[^&#]+/;
  23830. /**
  23831. * Append CMCD query args to a URL.
  23832. *
  23833. * @param url - The URL to append to.
  23834. * @param cmcd - The CMCD object to append.
  23835. * @param options - Options for encoding the CMCD object.
  23836. *
  23837. * @returns The URL with the CMCD query args appended.
  23838. *
  23839. * @group CMCD
  23840. *
  23841. * @beta
  23842. */
  23843. function appendCmcdQuery(url, cmcd, options) {
  23844. // TODO: Replace with URLSearchParams once we drop Safari < 10.1 & Chrome < 49 support.
  23845. // https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
  23846. var query = toCmcdQuery(cmcd, options);
  23847. if (!query) {
  23848. return url;
  23849. }
  23850. if (REGEX.test(url)) {
  23851. return url.replace(REGEX, query);
  23852. }
  23853. var separator = url.includes('?') ? '&' : '?';
  23854. return "" + url + separator + query;
  23855. }
  23856. /**
  23857. * Controller to deal with Common Media Client Data (CMCD)
  23858. * @see https://cdn.cta.tech/cta/media/media/resources/standards/pdfs/cta-5004-final.pdf
  23859. */
  23860. var CMCDController = /*#__PURE__*/function () {
  23861. // eslint-disable-line no-restricted-globals
  23862. function CMCDController(hls) {
  23863. var _this = this;
  23864. this.hls = void 0;
  23865. this.config = void 0;
  23866. this.media = void 0;
  23867. this.sid = void 0;
  23868. this.cid = void 0;
  23869. this.useHeaders = false;
  23870. this.includeKeys = void 0;
  23871. this.initialized = false;
  23872. this.starved = false;
  23873. this.buffering = true;
  23874. this.audioBuffer = void 0;
  23875. // eslint-disable-line no-restricted-globals
  23876. this.videoBuffer = void 0;
  23877. this.onWaiting = function () {
  23878. if (_this.initialized) {
  23879. _this.starved = true;
  23880. }
  23881. _this.buffering = true;
  23882. };
  23883. this.onPlaying = function () {
  23884. if (!_this.initialized) {
  23885. _this.initialized = true;
  23886. }
  23887. _this.buffering = false;
  23888. };
  23889. /**
  23890. * Apply CMCD data to a manifest request.
  23891. */
  23892. this.applyPlaylistData = function (context) {
  23893. try {
  23894. _this.apply(context, {
  23895. ot: CmObjectType.MANIFEST,
  23896. su: !_this.initialized
  23897. });
  23898. } catch (error) {
  23899. logger.warn('Could not generate manifest CMCD data.', error);
  23900. }
  23901. };
  23902. /**
  23903. * Apply CMCD data to a segment request
  23904. */
  23905. this.applyFragmentData = function (context) {
  23906. try {
  23907. var fragment = context.frag;
  23908. var level = _this.hls.levels[fragment.level];
  23909. var ot = _this.getObjectType(fragment);
  23910. var data = {
  23911. d: fragment.duration * 1000,
  23912. ot: ot
  23913. };
  23914. if (ot === CmObjectType.VIDEO || ot === CmObjectType.AUDIO || ot == CmObjectType.MUXED) {
  23915. data.br = level.bitrate / 1000;
  23916. data.tb = _this.getTopBandwidth(ot) / 1000;
  23917. data.bl = _this.getBufferLength(ot);
  23918. }
  23919. _this.apply(context, data);
  23920. } catch (error) {
  23921. logger.warn('Could not generate segment CMCD data.', error);
  23922. }
  23923. };
  23924. this.hls = hls;
  23925. var config = this.config = hls.config;
  23926. var cmcd = config.cmcd;
  23927. if (cmcd != null) {
  23928. config.pLoader = this.createPlaylistLoader();
  23929. config.fLoader = this.createFragmentLoader();
  23930. this.sid = cmcd.sessionId || uuid();
  23931. this.cid = cmcd.contentId;
  23932. this.useHeaders = cmcd.useHeaders === true;
  23933. this.includeKeys = cmcd.includeKeys;
  23934. this.registerListeners();
  23935. }
  23936. }
  23937. var _proto = CMCDController.prototype;
  23938. _proto.registerListeners = function registerListeners() {
  23939. var hls = this.hls;
  23940. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  23941. hls.on(Events.MEDIA_DETACHED, this.onMediaDetached, this);
  23942. hls.on(Events.BUFFER_CREATED, this.onBufferCreated, this);
  23943. };
  23944. _proto.unregisterListeners = function unregisterListeners() {
  23945. var hls = this.hls;
  23946. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  23947. hls.off(Events.MEDIA_DETACHED, this.onMediaDetached, this);
  23948. hls.off(Events.BUFFER_CREATED, this.onBufferCreated, this);
  23949. };
  23950. _proto.destroy = function destroy() {
  23951. this.unregisterListeners();
  23952. this.onMediaDetached();
  23953. // @ts-ignore
  23954. this.hls = this.config = this.audioBuffer = this.videoBuffer = null;
  23955. // @ts-ignore
  23956. this.onWaiting = this.onPlaying = null;
  23957. };
  23958. _proto.onMediaAttached = function onMediaAttached(event, data) {
  23959. this.media = data.media;
  23960. this.media.addEventListener('waiting', this.onWaiting);
  23961. this.media.addEventListener('playing', this.onPlaying);
  23962. };
  23963. _proto.onMediaDetached = function onMediaDetached() {
  23964. if (!this.media) {
  23965. return;
  23966. }
  23967. this.media.removeEventListener('waiting', this.onWaiting);
  23968. this.media.removeEventListener('playing', this.onPlaying);
  23969. // @ts-ignore
  23970. this.media = null;
  23971. };
  23972. _proto.onBufferCreated = function onBufferCreated(event, data) {
  23973. var _data$tracks$audio, _data$tracks$video;
  23974. this.audioBuffer = (_data$tracks$audio = data.tracks.audio) == null ? void 0 : _data$tracks$audio.buffer;
  23975. this.videoBuffer = (_data$tracks$video = data.tracks.video) == null ? void 0 : _data$tracks$video.buffer;
  23976. };
  23977. /**
  23978. * Create baseline CMCD data
  23979. */
  23980. _proto.createData = function createData() {
  23981. var _this$media;
  23982. return {
  23983. v: 1,
  23984. sf: CmStreamingFormat.HLS,
  23985. sid: this.sid,
  23986. cid: this.cid,
  23987. pr: (_this$media = this.media) == null ? void 0 : _this$media.playbackRate,
  23988. mtp: this.hls.bandwidthEstimate / 1000
  23989. };
  23990. }
  23991. /**
  23992. * Apply CMCD data to a request.
  23993. */;
  23994. _proto.apply = function apply(context, data) {
  23995. if (data === void 0) {
  23996. data = {};
  23997. }
  23998. // apply baseline data
  23999. _extends(data, this.createData());
  24000. var isVideo = data.ot === CmObjectType.INIT || data.ot === CmObjectType.VIDEO || data.ot === CmObjectType.MUXED;
  24001. if (this.starved && isVideo) {
  24002. data.bs = true;
  24003. data.su = true;
  24004. this.starved = false;
  24005. }
  24006. if (data.su == null) {
  24007. data.su = this.buffering;
  24008. }
  24009. // TODO: Implement rtp, nrr, nor, dl
  24010. var includeKeys = this.includeKeys;
  24011. if (includeKeys) {
  24012. data = Object.keys(data).reduce(function (acc, key) {
  24013. includeKeys.includes(key) && (acc[key] = data[key]);
  24014. return acc;
  24015. }, {});
  24016. }
  24017. if (this.useHeaders) {
  24018. if (!context.headers) {
  24019. context.headers = {};
  24020. }
  24021. appendCmcdHeaders(context.headers, data);
  24022. } else {
  24023. context.url = appendCmcdQuery(context.url, data);
  24024. }
  24025. };
  24026. /**
  24027. * The CMCD object type.
  24028. */
  24029. _proto.getObjectType = function getObjectType(fragment) {
  24030. var type = fragment.type;
  24031. if (type === 'subtitle') {
  24032. return CmObjectType.TIMED_TEXT;
  24033. }
  24034. if (fragment.sn === 'initSegment') {
  24035. return CmObjectType.INIT;
  24036. }
  24037. if (type === 'audio') {
  24038. return CmObjectType.AUDIO;
  24039. }
  24040. if (type === 'main') {
  24041. if (!this.hls.audioTracks.length) {
  24042. return CmObjectType.MUXED;
  24043. }
  24044. return CmObjectType.VIDEO;
  24045. }
  24046. return undefined;
  24047. }
  24048. /**
  24049. * Get the highest bitrate.
  24050. */;
  24051. _proto.getTopBandwidth = function getTopBandwidth(type) {
  24052. var bitrate = 0;
  24053. var levels;
  24054. var hls = this.hls;
  24055. if (type === CmObjectType.AUDIO) {
  24056. levels = hls.audioTracks;
  24057. } else {
  24058. var max = hls.maxAutoLevel;
  24059. var len = max > -1 ? max + 1 : hls.levels.length;
  24060. levels = hls.levels.slice(0, len);
  24061. }
  24062. for (var _iterator = _createForOfIteratorHelperLoose(levels), _step; !(_step = _iterator()).done;) {
  24063. var level = _step.value;
  24064. if (level.bitrate > bitrate) {
  24065. bitrate = level.bitrate;
  24066. }
  24067. }
  24068. return bitrate > 0 ? bitrate : NaN;
  24069. }
  24070. /**
  24071. * Get the buffer length for a media type in milliseconds
  24072. */;
  24073. _proto.getBufferLength = function getBufferLength(type) {
  24074. var media = this.hls.media;
  24075. var buffer = type === CmObjectType.AUDIO ? this.audioBuffer : this.videoBuffer;
  24076. if (!buffer || !media) {
  24077. return NaN;
  24078. }
  24079. var info = BufferHelper.bufferInfo(buffer, media.currentTime, this.config.maxBufferHole);
  24080. return info.len * 1000;
  24081. }
  24082. /**
  24083. * Create a playlist loader
  24084. */;
  24085. _proto.createPlaylistLoader = function createPlaylistLoader() {
  24086. var pLoader = this.config.pLoader;
  24087. var apply = this.applyPlaylistData;
  24088. var Ctor = pLoader || this.config.loader;
  24089. return /*#__PURE__*/function () {
  24090. function CmcdPlaylistLoader(config) {
  24091. this.loader = void 0;
  24092. this.loader = new Ctor(config);
  24093. }
  24094. var _proto2 = CmcdPlaylistLoader.prototype;
  24095. _proto2.destroy = function destroy() {
  24096. this.loader.destroy();
  24097. };
  24098. _proto2.abort = function abort() {
  24099. this.loader.abort();
  24100. };
  24101. _proto2.load = function load(context, config, callbacks) {
  24102. apply(context);
  24103. this.loader.load(context, config, callbacks);
  24104. };
  24105. _createClass(CmcdPlaylistLoader, [{
  24106. key: "stats",
  24107. get: function get() {
  24108. return this.loader.stats;
  24109. }
  24110. }, {
  24111. key: "context",
  24112. get: function get() {
  24113. return this.loader.context;
  24114. }
  24115. }]);
  24116. return CmcdPlaylistLoader;
  24117. }();
  24118. }
  24119. /**
  24120. * Create a playlist loader
  24121. */;
  24122. _proto.createFragmentLoader = function createFragmentLoader() {
  24123. var fLoader = this.config.fLoader;
  24124. var apply = this.applyFragmentData;
  24125. var Ctor = fLoader || this.config.loader;
  24126. return /*#__PURE__*/function () {
  24127. function CmcdFragmentLoader(config) {
  24128. this.loader = void 0;
  24129. this.loader = new Ctor(config);
  24130. }
  24131. var _proto3 = CmcdFragmentLoader.prototype;
  24132. _proto3.destroy = function destroy() {
  24133. this.loader.destroy();
  24134. };
  24135. _proto3.abort = function abort() {
  24136. this.loader.abort();
  24137. };
  24138. _proto3.load = function load(context, config, callbacks) {
  24139. apply(context);
  24140. this.loader.load(context, config, callbacks);
  24141. };
  24142. _createClass(CmcdFragmentLoader, [{
  24143. key: "stats",
  24144. get: function get() {
  24145. return this.loader.stats;
  24146. }
  24147. }, {
  24148. key: "context",
  24149. get: function get() {
  24150. return this.loader.context;
  24151. }
  24152. }]);
  24153. return CmcdFragmentLoader;
  24154. }();
  24155. };
  24156. return CMCDController;
  24157. }();
  24158. var PATHWAY_PENALTY_DURATION_MS = 300000;
  24159. var ContentSteeringController = /*#__PURE__*/function () {
  24160. function ContentSteeringController(hls) {
  24161. this.hls = void 0;
  24162. this.log = void 0;
  24163. this.loader = null;
  24164. this.uri = null;
  24165. this.pathwayId = '.';
  24166. this.pathwayPriority = null;
  24167. this.timeToLoad = 300;
  24168. this.reloadTimer = -1;
  24169. this.updated = 0;
  24170. this.started = false;
  24171. this.enabled = true;
  24172. this.levels = null;
  24173. this.audioTracks = null;
  24174. this.subtitleTracks = null;
  24175. this.penalizedPathways = {};
  24176. this.hls = hls;
  24177. this.log = logger.log.bind(logger, "[content-steering]:");
  24178. this.registerListeners();
  24179. }
  24180. var _proto = ContentSteeringController.prototype;
  24181. _proto.registerListeners = function registerListeners() {
  24182. var hls = this.hls;
  24183. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  24184. hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  24185. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  24186. hls.on(Events.ERROR, this.onError, this);
  24187. };
  24188. _proto.unregisterListeners = function unregisterListeners() {
  24189. var hls = this.hls;
  24190. if (!hls) {
  24191. return;
  24192. }
  24193. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  24194. hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  24195. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  24196. hls.off(Events.ERROR, this.onError, this);
  24197. };
  24198. _proto.startLoad = function startLoad() {
  24199. this.started = true;
  24200. this.clearTimeout();
  24201. if (this.enabled && this.uri) {
  24202. if (this.updated) {
  24203. var ttl = this.timeToLoad * 1000 - (performance.now() - this.updated);
  24204. if (ttl > 0) {
  24205. this.scheduleRefresh(this.uri, ttl);
  24206. return;
  24207. }
  24208. }
  24209. this.loadSteeringManifest(this.uri);
  24210. }
  24211. };
  24212. _proto.stopLoad = function stopLoad() {
  24213. this.started = false;
  24214. if (this.loader) {
  24215. this.loader.destroy();
  24216. this.loader = null;
  24217. }
  24218. this.clearTimeout();
  24219. };
  24220. _proto.clearTimeout = function clearTimeout() {
  24221. if (this.reloadTimer !== -1) {
  24222. self.clearTimeout(this.reloadTimer);
  24223. this.reloadTimer = -1;
  24224. }
  24225. };
  24226. _proto.destroy = function destroy() {
  24227. this.unregisterListeners();
  24228. this.stopLoad();
  24229. // @ts-ignore
  24230. this.hls = null;
  24231. this.levels = this.audioTracks = this.subtitleTracks = null;
  24232. };
  24233. _proto.removeLevel = function removeLevel(levelToRemove) {
  24234. var levels = this.levels;
  24235. if (levels) {
  24236. this.levels = levels.filter(function (level) {
  24237. return level !== levelToRemove;
  24238. });
  24239. }
  24240. };
  24241. _proto.onManifestLoading = function onManifestLoading() {
  24242. this.stopLoad();
  24243. this.enabled = true;
  24244. this.timeToLoad = 300;
  24245. this.updated = 0;
  24246. this.uri = null;
  24247. this.pathwayId = '.';
  24248. this.levels = this.audioTracks = this.subtitleTracks = null;
  24249. };
  24250. _proto.onManifestLoaded = function onManifestLoaded(event, data) {
  24251. var contentSteering = data.contentSteering;
  24252. if (contentSteering === null) {
  24253. return;
  24254. }
  24255. this.pathwayId = contentSteering.pathwayId;
  24256. this.uri = contentSteering.uri;
  24257. if (this.started) {
  24258. this.startLoad();
  24259. }
  24260. };
  24261. _proto.onManifestParsed = function onManifestParsed(event, data) {
  24262. this.audioTracks = data.audioTracks;
  24263. this.subtitleTracks = data.subtitleTracks;
  24264. };
  24265. _proto.onError = function onError(event, data) {
  24266. var errorAction = data.errorAction;
  24267. if ((errorAction == null ? void 0 : errorAction.action) === NetworkErrorAction.SendAlternateToPenaltyBox && errorAction.flags === ErrorActionFlags.MoveAllAlternatesMatchingHost) {
  24268. var levels = this.levels;
  24269. var pathwayPriority = this.pathwayPriority;
  24270. var errorPathway = this.pathwayId;
  24271. if (data.context) {
  24272. var _data$context = data.context,
  24273. groupId = _data$context.groupId,
  24274. _pathwayId = _data$context.pathwayId,
  24275. type = _data$context.type;
  24276. if (groupId && levels) {
  24277. errorPathway = this.getPathwayForGroupId(groupId, type, errorPathway);
  24278. } else if (_pathwayId) {
  24279. errorPathway = _pathwayId;
  24280. }
  24281. }
  24282. if (!(errorPathway in this.penalizedPathways)) {
  24283. this.penalizedPathways[errorPathway] = performance.now();
  24284. }
  24285. if (!pathwayPriority && levels) {
  24286. // If PATHWAY-PRIORITY was not provided, list pathways for error handling
  24287. pathwayPriority = levels.reduce(function (pathways, level) {
  24288. if (pathways.indexOf(level.pathwayId) === -1) {
  24289. pathways.push(level.pathwayId);
  24290. }
  24291. return pathways;
  24292. }, []);
  24293. }
  24294. if (pathwayPriority && pathwayPriority.length > 1) {
  24295. this.updatePathwayPriority(pathwayPriority);
  24296. errorAction.resolved = this.pathwayId !== errorPathway;
  24297. }
  24298. if (!errorAction.resolved) {
  24299. logger.warn("Could not resolve " + data.details + " (\"" + data.error.message + "\") with content-steering for Pathway: " + errorPathway + " levels: " + (levels ? levels.length : levels) + " priorities: " + JSON.stringify(pathwayPriority) + " penalized: " + JSON.stringify(this.penalizedPathways));
  24300. }
  24301. }
  24302. };
  24303. _proto.filterParsedLevels = function filterParsedLevels(levels) {
  24304. // Filter levels to only include those that are in the initial pathway
  24305. this.levels = levels;
  24306. var pathwayLevels = this.getLevelsForPathway(this.pathwayId);
  24307. if (pathwayLevels.length === 0) {
  24308. var _pathwayId2 = levels[0].pathwayId;
  24309. this.log("No levels found in Pathway " + this.pathwayId + ". Setting initial Pathway to \"" + _pathwayId2 + "\"");
  24310. pathwayLevels = this.getLevelsForPathway(_pathwayId2);
  24311. this.pathwayId = _pathwayId2;
  24312. }
  24313. if (pathwayLevels.length !== levels.length) {
  24314. this.log("Found " + pathwayLevels.length + "/" + levels.length + " levels in Pathway \"" + this.pathwayId + "\"");
  24315. return pathwayLevels;
  24316. }
  24317. return levels;
  24318. };
  24319. _proto.getLevelsForPathway = function getLevelsForPathway(pathwayId) {
  24320. if (this.levels === null) {
  24321. return [];
  24322. }
  24323. return this.levels.filter(function (level) {
  24324. return pathwayId === level.pathwayId;
  24325. });
  24326. };
  24327. _proto.updatePathwayPriority = function updatePathwayPriority(pathwayPriority) {
  24328. this.pathwayPriority = pathwayPriority;
  24329. var levels;
  24330. // Evaluate if we should remove the pathway from the penalized list
  24331. var penalizedPathways = this.penalizedPathways;
  24332. var now = performance.now();
  24333. Object.keys(penalizedPathways).forEach(function (pathwayId) {
  24334. if (now - penalizedPathways[pathwayId] > PATHWAY_PENALTY_DURATION_MS) {
  24335. delete penalizedPathways[pathwayId];
  24336. }
  24337. });
  24338. for (var i = 0; i < pathwayPriority.length; i++) {
  24339. var _pathwayId3 = pathwayPriority[i];
  24340. if (_pathwayId3 in penalizedPathways) {
  24341. continue;
  24342. }
  24343. if (_pathwayId3 === this.pathwayId) {
  24344. return;
  24345. }
  24346. var selectedIndex = this.hls.nextLoadLevel;
  24347. var selectedLevel = this.hls.levels[selectedIndex];
  24348. levels = this.getLevelsForPathway(_pathwayId3);
  24349. if (levels.length > 0) {
  24350. this.log("Setting Pathway to \"" + _pathwayId3 + "\"");
  24351. this.pathwayId = _pathwayId3;
  24352. reassignFragmentLevelIndexes(levels);
  24353. this.hls.trigger(Events.LEVELS_UPDATED, {
  24354. levels: levels
  24355. });
  24356. // Set LevelController's level to trigger LEVEL_SWITCHING which loads playlist if needed
  24357. var levelAfterChange = this.hls.levels[selectedIndex];
  24358. if (selectedLevel && levelAfterChange && this.levels) {
  24359. if (levelAfterChange.attrs['STABLE-VARIANT-ID'] !== selectedLevel.attrs['STABLE-VARIANT-ID'] && levelAfterChange.bitrate !== selectedLevel.bitrate) {
  24360. this.log("Unstable Pathways change from bitrate " + selectedLevel.bitrate + " to " + levelAfterChange.bitrate);
  24361. }
  24362. this.hls.nextLoadLevel = selectedIndex;
  24363. }
  24364. break;
  24365. }
  24366. }
  24367. };
  24368. _proto.getPathwayForGroupId = function getPathwayForGroupId(groupId, type, defaultPathway) {
  24369. var levels = this.getLevelsForPathway(defaultPathway).concat(this.levels || []);
  24370. for (var i = 0; i < levels.length; i++) {
  24371. if (type === PlaylistContextType.AUDIO_TRACK && levels[i].hasAudioGroup(groupId) || type === PlaylistContextType.SUBTITLE_TRACK && levels[i].hasSubtitleGroup(groupId)) {
  24372. return levels[i].pathwayId;
  24373. }
  24374. }
  24375. return defaultPathway;
  24376. };
  24377. _proto.clonePathways = function clonePathways(pathwayClones) {
  24378. var _this = this;
  24379. var levels = this.levels;
  24380. if (!levels) {
  24381. return;
  24382. }
  24383. var audioGroupCloneMap = {};
  24384. var subtitleGroupCloneMap = {};
  24385. pathwayClones.forEach(function (pathwayClone) {
  24386. var cloneId = pathwayClone.ID,
  24387. baseId = pathwayClone['BASE-ID'],
  24388. uriReplacement = pathwayClone['URI-REPLACEMENT'];
  24389. if (levels.some(function (level) {
  24390. return level.pathwayId === cloneId;
  24391. })) {
  24392. return;
  24393. }
  24394. var clonedVariants = _this.getLevelsForPathway(baseId).map(function (baseLevel) {
  24395. var attributes = new AttrList(baseLevel.attrs);
  24396. attributes['PATHWAY-ID'] = cloneId;
  24397. var clonedAudioGroupId = attributes.AUDIO && attributes.AUDIO + "_clone_" + cloneId;
  24398. var clonedSubtitleGroupId = attributes.SUBTITLES && attributes.SUBTITLES + "_clone_" + cloneId;
  24399. if (clonedAudioGroupId) {
  24400. audioGroupCloneMap[attributes.AUDIO] = clonedAudioGroupId;
  24401. attributes.AUDIO = clonedAudioGroupId;
  24402. }
  24403. if (clonedSubtitleGroupId) {
  24404. subtitleGroupCloneMap[attributes.SUBTITLES] = clonedSubtitleGroupId;
  24405. attributes.SUBTITLES = clonedSubtitleGroupId;
  24406. }
  24407. var url = performUriReplacement(baseLevel.uri, attributes['STABLE-VARIANT-ID'], 'PER-VARIANT-URIS', uriReplacement);
  24408. var clonedLevel = new Level({
  24409. attrs: attributes,
  24410. audioCodec: baseLevel.audioCodec,
  24411. bitrate: baseLevel.bitrate,
  24412. height: baseLevel.height,
  24413. name: baseLevel.name,
  24414. url: url,
  24415. videoCodec: baseLevel.videoCodec,
  24416. width: baseLevel.width
  24417. });
  24418. if (baseLevel.audioGroups) {
  24419. for (var i = 1; i < baseLevel.audioGroups.length; i++) {
  24420. clonedLevel.addGroupId('audio', baseLevel.audioGroups[i] + "_clone_" + cloneId);
  24421. }
  24422. }
  24423. if (baseLevel.subtitleGroups) {
  24424. for (var _i = 1; _i < baseLevel.subtitleGroups.length; _i++) {
  24425. clonedLevel.addGroupId('text', baseLevel.subtitleGroups[_i] + "_clone_" + cloneId);
  24426. }
  24427. }
  24428. return clonedLevel;
  24429. });
  24430. levels.push.apply(levels, clonedVariants);
  24431. cloneRenditionGroups(_this.audioTracks, audioGroupCloneMap, uriReplacement, cloneId);
  24432. cloneRenditionGroups(_this.subtitleTracks, subtitleGroupCloneMap, uriReplacement, cloneId);
  24433. });
  24434. };
  24435. _proto.loadSteeringManifest = function loadSteeringManifest(uri) {
  24436. var _this2 = this;
  24437. var config = this.hls.config;
  24438. var Loader = config.loader;
  24439. if (this.loader) {
  24440. this.loader.destroy();
  24441. }
  24442. this.loader = new Loader(config);
  24443. var url;
  24444. try {
  24445. url = new self.URL(uri);
  24446. } catch (error) {
  24447. this.enabled = false;
  24448. this.log("Failed to parse Steering Manifest URI: " + uri);
  24449. return;
  24450. }
  24451. if (url.protocol !== 'data:') {
  24452. var throughput = (this.hls.bandwidthEstimate || config.abrEwmaDefaultEstimate) | 0;
  24453. url.searchParams.set('_HLS_pathway', this.pathwayId);
  24454. url.searchParams.set('_HLS_throughput', '' + throughput);
  24455. }
  24456. var context = {
  24457. responseType: 'json',
  24458. url: url.href
  24459. };
  24460. var loadPolicy = config.steeringManifestLoadPolicy.default;
  24461. var legacyRetryCompatibility = loadPolicy.errorRetry || loadPolicy.timeoutRetry || {};
  24462. var loaderConfig = {
  24463. loadPolicy: loadPolicy,
  24464. timeout: loadPolicy.maxLoadTimeMs,
  24465. maxRetry: legacyRetryCompatibility.maxNumRetry || 0,
  24466. retryDelay: legacyRetryCompatibility.retryDelayMs || 0,
  24467. maxRetryDelay: legacyRetryCompatibility.maxRetryDelayMs || 0
  24468. };
  24469. var callbacks = {
  24470. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  24471. _this2.log("Loaded steering manifest: \"" + url + "\"");
  24472. var steeringData = response.data;
  24473. if (steeringData.VERSION !== 1) {
  24474. _this2.log("Steering VERSION " + steeringData.VERSION + " not supported!");
  24475. return;
  24476. }
  24477. _this2.updated = performance.now();
  24478. _this2.timeToLoad = steeringData.TTL;
  24479. var reloadUri = steeringData['RELOAD-URI'],
  24480. pathwayClones = steeringData['PATHWAY-CLONES'],
  24481. pathwayPriority = steeringData['PATHWAY-PRIORITY'];
  24482. if (reloadUri) {
  24483. try {
  24484. _this2.uri = new self.URL(reloadUri, url).href;
  24485. } catch (error) {
  24486. _this2.enabled = false;
  24487. _this2.log("Failed to parse Steering Manifest RELOAD-URI: " + reloadUri);
  24488. return;
  24489. }
  24490. }
  24491. _this2.scheduleRefresh(_this2.uri || context.url);
  24492. if (pathwayClones) {
  24493. _this2.clonePathways(pathwayClones);
  24494. }
  24495. var loadedSteeringData = {
  24496. steeringManifest: steeringData,
  24497. url: url.toString()
  24498. };
  24499. _this2.hls.trigger(Events.STEERING_MANIFEST_LOADED, loadedSteeringData);
  24500. if (pathwayPriority) {
  24501. _this2.updatePathwayPriority(pathwayPriority);
  24502. }
  24503. },
  24504. onError: function onError(error, context, networkDetails, stats) {
  24505. _this2.log("Error loading steering manifest: " + error.code + " " + error.text + " (" + context.url + ")");
  24506. _this2.stopLoad();
  24507. if (error.code === 410) {
  24508. _this2.enabled = false;
  24509. _this2.log("Steering manifest " + context.url + " no longer available");
  24510. return;
  24511. }
  24512. var ttl = _this2.timeToLoad * 1000;
  24513. if (error.code === 429) {
  24514. var loader = _this2.loader;
  24515. if (typeof (loader == null ? void 0 : loader.getResponseHeader) === 'function') {
  24516. var retryAfter = loader.getResponseHeader('Retry-After');
  24517. if (retryAfter) {
  24518. ttl = parseFloat(retryAfter) * 1000;
  24519. }
  24520. }
  24521. _this2.log("Steering manifest " + context.url + " rate limited");
  24522. return;
  24523. }
  24524. _this2.scheduleRefresh(_this2.uri || context.url, ttl);
  24525. },
  24526. onTimeout: function onTimeout(stats, context, networkDetails) {
  24527. _this2.log("Timeout loading steering manifest (" + context.url + ")");
  24528. _this2.scheduleRefresh(_this2.uri || context.url);
  24529. }
  24530. };
  24531. this.log("Requesting steering manifest: " + url);
  24532. this.loader.load(context, loaderConfig, callbacks);
  24533. };
  24534. _proto.scheduleRefresh = function scheduleRefresh(uri, ttlMs) {
  24535. var _this3 = this;
  24536. if (ttlMs === void 0) {
  24537. ttlMs = this.timeToLoad * 1000;
  24538. }
  24539. this.clearTimeout();
  24540. this.reloadTimer = self.setTimeout(function () {
  24541. var _this3$hls;
  24542. var media = (_this3$hls = _this3.hls) == null ? void 0 : _this3$hls.media;
  24543. if (media && !media.ended) {
  24544. _this3.loadSteeringManifest(uri);
  24545. return;
  24546. }
  24547. _this3.scheduleRefresh(uri, _this3.timeToLoad * 1000);
  24548. }, ttlMs);
  24549. };
  24550. return ContentSteeringController;
  24551. }();
  24552. function cloneRenditionGroups(tracks, groupCloneMap, uriReplacement, cloneId) {
  24553. if (!tracks) {
  24554. return;
  24555. }
  24556. Object.keys(groupCloneMap).forEach(function (audioGroupId) {
  24557. var clonedTracks = tracks.filter(function (track) {
  24558. return track.groupId === audioGroupId;
  24559. }).map(function (track) {
  24560. var clonedTrack = _extends({}, track);
  24561. clonedTrack.details = undefined;
  24562. clonedTrack.attrs = new AttrList(clonedTrack.attrs);
  24563. clonedTrack.url = clonedTrack.attrs.URI = performUriReplacement(track.url, track.attrs['STABLE-RENDITION-ID'], 'PER-RENDITION-URIS', uriReplacement);
  24564. clonedTrack.groupId = clonedTrack.attrs['GROUP-ID'] = groupCloneMap[audioGroupId];
  24565. clonedTrack.attrs['PATHWAY-ID'] = cloneId;
  24566. return clonedTrack;
  24567. });
  24568. tracks.push.apply(tracks, clonedTracks);
  24569. });
  24570. }
  24571. function performUriReplacement(uri, stableId, perOptionKey, uriReplacement) {
  24572. var host = uriReplacement.HOST,
  24573. params = uriReplacement.PARAMS,
  24574. perOptionUris = uriReplacement[perOptionKey];
  24575. var perVariantUri;
  24576. if (stableId) {
  24577. perVariantUri = perOptionUris == null ? void 0 : perOptionUris[stableId];
  24578. if (perVariantUri) {
  24579. uri = perVariantUri;
  24580. }
  24581. }
  24582. var url = new self.URL(uri);
  24583. if (host && !perVariantUri) {
  24584. url.host = host;
  24585. }
  24586. if (params) {
  24587. Object.keys(params).sort().forEach(function (key) {
  24588. if (key) {
  24589. url.searchParams.set(key, params[key]);
  24590. }
  24591. });
  24592. }
  24593. return url.href;
  24594. }
  24595. var AGE_HEADER_LINE_REGEX = /^age:\s*[\d.]+\s*$/im;
  24596. var XhrLoader = /*#__PURE__*/function () {
  24597. function XhrLoader(config) {
  24598. this.xhrSetup = void 0;
  24599. this.requestTimeout = void 0;
  24600. this.retryTimeout = void 0;
  24601. this.retryDelay = void 0;
  24602. this.config = null;
  24603. this.callbacks = null;
  24604. this.context = null;
  24605. this.loader = null;
  24606. this.stats = void 0;
  24607. this.xhrSetup = config ? config.xhrSetup || null : null;
  24608. this.stats = new LoadStats();
  24609. this.retryDelay = 0;
  24610. }
  24611. var _proto = XhrLoader.prototype;
  24612. _proto.destroy = function destroy() {
  24613. this.callbacks = null;
  24614. this.abortInternal();
  24615. this.loader = null;
  24616. this.config = null;
  24617. this.context = null;
  24618. this.xhrSetup = null;
  24619. };
  24620. _proto.abortInternal = function abortInternal() {
  24621. var loader = this.loader;
  24622. self.clearTimeout(this.requestTimeout);
  24623. self.clearTimeout(this.retryTimeout);
  24624. if (loader) {
  24625. loader.onreadystatechange = null;
  24626. loader.onprogress = null;
  24627. if (loader.readyState !== 4) {
  24628. this.stats.aborted = true;
  24629. loader.abort();
  24630. }
  24631. }
  24632. };
  24633. _proto.abort = function abort() {
  24634. var _this$callbacks;
  24635. this.abortInternal();
  24636. if ((_this$callbacks = this.callbacks) != null && _this$callbacks.onAbort) {
  24637. this.callbacks.onAbort(this.stats, this.context, this.loader);
  24638. }
  24639. };
  24640. _proto.load = function load(context, config, callbacks) {
  24641. if (this.stats.loading.start) {
  24642. throw new Error('Loader can only be used once.');
  24643. }
  24644. this.stats.loading.start = self.performance.now();
  24645. this.context = context;
  24646. this.config = config;
  24647. this.callbacks = callbacks;
  24648. this.loadInternal();
  24649. };
  24650. _proto.loadInternal = function loadInternal() {
  24651. var _this = this;
  24652. var config = this.config,
  24653. context = this.context;
  24654. if (!config || !context) {
  24655. return;
  24656. }
  24657. var xhr = this.loader = new self.XMLHttpRequest();
  24658. var stats = this.stats;
  24659. stats.loading.first = 0;
  24660. stats.loaded = 0;
  24661. stats.aborted = false;
  24662. var xhrSetup = this.xhrSetup;
  24663. if (xhrSetup) {
  24664. Promise.resolve().then(function () {
  24665. if (_this.loader !== xhr || _this.stats.aborted) return;
  24666. return xhrSetup(xhr, context.url);
  24667. }).catch(function (error) {
  24668. if (_this.loader !== xhr || _this.stats.aborted) return;
  24669. xhr.open('GET', context.url, true);
  24670. return xhrSetup(xhr, context.url);
  24671. }).then(function () {
  24672. if (_this.loader !== xhr || _this.stats.aborted) return;
  24673. _this.openAndSendXhr(xhr, context, config);
  24674. }).catch(function (error) {
  24675. // IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS
  24676. _this.callbacks.onError({
  24677. code: xhr.status,
  24678. text: error.message
  24679. }, context, xhr, stats);
  24680. return;
  24681. });
  24682. } else {
  24683. this.openAndSendXhr(xhr, context, config);
  24684. }
  24685. };
  24686. _proto.openAndSendXhr = function openAndSendXhr(xhr, context, config) {
  24687. if (!xhr.readyState) {
  24688. xhr.open('GET', context.url, true);
  24689. }
  24690. var headers = context.headers;
  24691. var _config$loadPolicy = config.loadPolicy,
  24692. maxTimeToFirstByteMs = _config$loadPolicy.maxTimeToFirstByteMs,
  24693. maxLoadTimeMs = _config$loadPolicy.maxLoadTimeMs;
  24694. if (headers) {
  24695. for (var header in headers) {
  24696. xhr.setRequestHeader(header, headers[header]);
  24697. }
  24698. }
  24699. if (context.rangeEnd) {
  24700. xhr.setRequestHeader('Range', 'bytes=' + context.rangeStart + '-' + (context.rangeEnd - 1));
  24701. }
  24702. xhr.onreadystatechange = this.readystatechange.bind(this);
  24703. xhr.onprogress = this.loadprogress.bind(this);
  24704. xhr.responseType = context.responseType;
  24705. // setup timeout before we perform request
  24706. self.clearTimeout(this.requestTimeout);
  24707. config.timeout = maxTimeToFirstByteMs && isFiniteNumber(maxTimeToFirstByteMs) ? maxTimeToFirstByteMs : maxLoadTimeMs;
  24708. this.requestTimeout = self.setTimeout(this.loadtimeout.bind(this), config.timeout);
  24709. xhr.send();
  24710. };
  24711. _proto.readystatechange = function readystatechange() {
  24712. var context = this.context,
  24713. xhr = this.loader,
  24714. stats = this.stats;
  24715. if (!context || !xhr) {
  24716. return;
  24717. }
  24718. var readyState = xhr.readyState;
  24719. var config = this.config;
  24720. // don't proceed if xhr has been aborted
  24721. if (stats.aborted) {
  24722. return;
  24723. }
  24724. // >= HEADERS_RECEIVED
  24725. if (readyState >= 2) {
  24726. if (stats.loading.first === 0) {
  24727. stats.loading.first = Math.max(self.performance.now(), stats.loading.start);
  24728. // readyState >= 2 AND readyState !==4 (readyState = HEADERS_RECEIVED || LOADING) rearm timeout as xhr not finished yet
  24729. if (config.timeout !== config.loadPolicy.maxLoadTimeMs) {
  24730. self.clearTimeout(this.requestTimeout);
  24731. config.timeout = config.loadPolicy.maxLoadTimeMs;
  24732. this.requestTimeout = self.setTimeout(this.loadtimeout.bind(this), config.loadPolicy.maxLoadTimeMs - (stats.loading.first - stats.loading.start));
  24733. }
  24734. }
  24735. if (readyState === 4) {
  24736. self.clearTimeout(this.requestTimeout);
  24737. xhr.onreadystatechange = null;
  24738. xhr.onprogress = null;
  24739. var _status = xhr.status;
  24740. // http status between 200 to 299 are all successful
  24741. var useResponse = xhr.responseType !== 'text';
  24742. if (_status >= 200 && _status < 300 && (useResponse && xhr.response || xhr.responseText !== null)) {
  24743. stats.loading.end = Math.max(self.performance.now(), stats.loading.first);
  24744. var data = useResponse ? xhr.response : xhr.responseText;
  24745. var len = xhr.responseType === 'arraybuffer' ? data.byteLength : data.length;
  24746. stats.loaded = stats.total = len;
  24747. stats.bwEstimate = stats.total * 8000 / (stats.loading.end - stats.loading.first);
  24748. if (!this.callbacks) {
  24749. return;
  24750. }
  24751. var onProgress = this.callbacks.onProgress;
  24752. if (onProgress) {
  24753. onProgress(stats, context, data, xhr);
  24754. }
  24755. if (!this.callbacks) {
  24756. return;
  24757. }
  24758. var response = {
  24759. url: xhr.responseURL,
  24760. data: data,
  24761. code: _status
  24762. };
  24763. this.callbacks.onSuccess(response, stats, context, xhr);
  24764. } else {
  24765. var retryConfig = config.loadPolicy.errorRetry;
  24766. var retryCount = stats.retry;
  24767. // if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error
  24768. var _response = {
  24769. url: context.url,
  24770. data: undefined,
  24771. code: _status
  24772. };
  24773. if (shouldRetry(retryConfig, retryCount, false, _response)) {
  24774. this.retry(retryConfig);
  24775. } else {
  24776. logger.error(_status + " while loading " + context.url);
  24777. this.callbacks.onError({
  24778. code: _status,
  24779. text: xhr.statusText
  24780. }, context, xhr, stats);
  24781. }
  24782. }
  24783. }
  24784. }
  24785. };
  24786. _proto.loadtimeout = function loadtimeout() {
  24787. if (!this.config) return;
  24788. var retryConfig = this.config.loadPolicy.timeoutRetry;
  24789. var retryCount = this.stats.retry;
  24790. if (shouldRetry(retryConfig, retryCount, true)) {
  24791. this.retry(retryConfig);
  24792. } else {
  24793. var _this$context;
  24794. logger.warn("timeout while loading " + ((_this$context = this.context) == null ? void 0 : _this$context.url));
  24795. var callbacks = this.callbacks;
  24796. if (callbacks) {
  24797. this.abortInternal();
  24798. callbacks.onTimeout(this.stats, this.context, this.loader);
  24799. }
  24800. }
  24801. };
  24802. _proto.retry = function retry(retryConfig) {
  24803. var context = this.context,
  24804. stats = this.stats;
  24805. this.retryDelay = getRetryDelay(retryConfig, stats.retry);
  24806. stats.retry++;
  24807. logger.warn((status ? 'HTTP Status ' + status : 'Timeout') + " while loading " + (context == null ? void 0 : context.url) + ", retrying " + stats.retry + "/" + retryConfig.maxNumRetry + " in " + this.retryDelay + "ms");
  24808. // abort and reset internal state
  24809. this.abortInternal();
  24810. this.loader = null;
  24811. // schedule retry
  24812. self.clearTimeout(this.retryTimeout);
  24813. this.retryTimeout = self.setTimeout(this.loadInternal.bind(this), this.retryDelay);
  24814. };
  24815. _proto.loadprogress = function loadprogress(event) {
  24816. var stats = this.stats;
  24817. stats.loaded = event.loaded;
  24818. if (event.lengthComputable) {
  24819. stats.total = event.total;
  24820. }
  24821. };
  24822. _proto.getCacheAge = function getCacheAge() {
  24823. var result = null;
  24824. if (this.loader && AGE_HEADER_LINE_REGEX.test(this.loader.getAllResponseHeaders())) {
  24825. var ageHeader = this.loader.getResponseHeader('age');
  24826. result = ageHeader ? parseFloat(ageHeader) : null;
  24827. }
  24828. return result;
  24829. };
  24830. _proto.getResponseHeader = function getResponseHeader(name) {
  24831. if (this.loader && new RegExp("^" + name + ":\\s*[\\d.]+\\s*$", 'im').test(this.loader.getAllResponseHeaders())) {
  24832. return this.loader.getResponseHeader(name);
  24833. }
  24834. return null;
  24835. };
  24836. return XhrLoader;
  24837. }();
  24838. function fetchSupported() {
  24839. if (
  24840. // @ts-ignore
  24841. self.fetch && self.AbortController && self.ReadableStream && self.Request) {
  24842. try {
  24843. new self.ReadableStream({}); // eslint-disable-line no-new
  24844. return true;
  24845. } catch (e) {
  24846. /* noop */
  24847. }
  24848. }
  24849. return false;
  24850. }
  24851. var BYTERANGE = /(\d+)-(\d+)\/(\d+)/;
  24852. var FetchLoader = /*#__PURE__*/function () {
  24853. function FetchLoader(config /* HlsConfig */) {
  24854. this.fetchSetup = void 0;
  24855. this.requestTimeout = void 0;
  24856. this.request = null;
  24857. this.response = null;
  24858. this.controller = void 0;
  24859. this.context = null;
  24860. this.config = null;
  24861. this.callbacks = null;
  24862. this.stats = void 0;
  24863. this.loader = null;
  24864. this.fetchSetup = config.fetchSetup || getRequest;
  24865. this.controller = new self.AbortController();
  24866. this.stats = new LoadStats();
  24867. }
  24868. var _proto = FetchLoader.prototype;
  24869. _proto.destroy = function destroy() {
  24870. this.loader = this.callbacks = this.context = this.config = this.request = null;
  24871. this.abortInternal();
  24872. this.response = null;
  24873. // @ts-ignore
  24874. this.fetchSetup = this.controller = this.stats = null;
  24875. };
  24876. _proto.abortInternal = function abortInternal() {
  24877. if (this.controller && !this.stats.loading.end) {
  24878. this.stats.aborted = true;
  24879. this.controller.abort();
  24880. }
  24881. };
  24882. _proto.abort = function abort() {
  24883. var _this$callbacks;
  24884. this.abortInternal();
  24885. if ((_this$callbacks = this.callbacks) != null && _this$callbacks.onAbort) {
  24886. this.callbacks.onAbort(this.stats, this.context, this.response);
  24887. }
  24888. };
  24889. _proto.load = function load(context, config, callbacks) {
  24890. var _this = this;
  24891. var stats = this.stats;
  24892. if (stats.loading.start) {
  24893. throw new Error('Loader can only be used once.');
  24894. }
  24895. stats.loading.start = self.performance.now();
  24896. var initParams = getRequestParameters(context, this.controller.signal);
  24897. var onProgress = callbacks.onProgress;
  24898. var isArrayBuffer = context.responseType === 'arraybuffer';
  24899. var LENGTH = isArrayBuffer ? 'byteLength' : 'length';
  24900. var _config$loadPolicy = config.loadPolicy,
  24901. maxTimeToFirstByteMs = _config$loadPolicy.maxTimeToFirstByteMs,
  24902. maxLoadTimeMs = _config$loadPolicy.maxLoadTimeMs;
  24903. this.context = context;
  24904. this.config = config;
  24905. this.callbacks = callbacks;
  24906. this.request = this.fetchSetup(context, initParams);
  24907. self.clearTimeout(this.requestTimeout);
  24908. config.timeout = maxTimeToFirstByteMs && isFiniteNumber(maxTimeToFirstByteMs) ? maxTimeToFirstByteMs : maxLoadTimeMs;
  24909. this.requestTimeout = self.setTimeout(function () {
  24910. _this.abortInternal();
  24911. callbacks.onTimeout(stats, context, _this.response);
  24912. }, config.timeout);
  24913. self.fetch(this.request).then(function (response) {
  24914. _this.response = _this.loader = response;
  24915. var first = Math.max(self.performance.now(), stats.loading.start);
  24916. self.clearTimeout(_this.requestTimeout);
  24917. config.timeout = maxLoadTimeMs;
  24918. _this.requestTimeout = self.setTimeout(function () {
  24919. _this.abortInternal();
  24920. callbacks.onTimeout(stats, context, _this.response);
  24921. }, maxLoadTimeMs - (first - stats.loading.start));
  24922. if (!response.ok) {
  24923. var status = response.status,
  24924. statusText = response.statusText;
  24925. throw new FetchError(statusText || 'fetch, bad network response', status, response);
  24926. }
  24927. stats.loading.first = first;
  24928. stats.total = getContentLength(response.headers) || stats.total;
  24929. if (onProgress && isFiniteNumber(config.highWaterMark)) {
  24930. return _this.loadProgressively(response, stats, context, config.highWaterMark, onProgress);
  24931. }
  24932. if (isArrayBuffer) {
  24933. return response.arrayBuffer();
  24934. }
  24935. if (context.responseType === 'json') {
  24936. return response.json();
  24937. }
  24938. return response.text();
  24939. }).then(function (responseData) {
  24940. var response = _this.response;
  24941. if (!response) {
  24942. throw new Error('loader destroyed');
  24943. }
  24944. self.clearTimeout(_this.requestTimeout);
  24945. stats.loading.end = Math.max(self.performance.now(), stats.loading.first);
  24946. var total = responseData[LENGTH];
  24947. if (total) {
  24948. stats.loaded = stats.total = total;
  24949. }
  24950. var loaderResponse = {
  24951. url: response.url,
  24952. data: responseData,
  24953. code: response.status
  24954. };
  24955. if (onProgress && !isFiniteNumber(config.highWaterMark)) {
  24956. onProgress(stats, context, responseData, response);
  24957. }
  24958. callbacks.onSuccess(loaderResponse, stats, context, response);
  24959. }).catch(function (error) {
  24960. self.clearTimeout(_this.requestTimeout);
  24961. if (stats.aborted) {
  24962. return;
  24963. }
  24964. // CORS errors result in an undefined code. Set it to 0 here to align with XHR's behavior
  24965. // when destroying, 'error' itself can be undefined
  24966. var code = !error ? 0 : error.code || 0;
  24967. var text = !error ? null : error.message;
  24968. callbacks.onError({
  24969. code: code,
  24970. text: text
  24971. }, context, error ? error.details : null, stats);
  24972. });
  24973. };
  24974. _proto.getCacheAge = function getCacheAge() {
  24975. var result = null;
  24976. if (this.response) {
  24977. var ageHeader = this.response.headers.get('age');
  24978. result = ageHeader ? parseFloat(ageHeader) : null;
  24979. }
  24980. return result;
  24981. };
  24982. _proto.getResponseHeader = function getResponseHeader(name) {
  24983. return this.response ? this.response.headers.get(name) : null;
  24984. };
  24985. _proto.loadProgressively = function loadProgressively(response, stats, context, highWaterMark, onProgress) {
  24986. if (highWaterMark === void 0) {
  24987. highWaterMark = 0;
  24988. }
  24989. var chunkCache = new ChunkCache();
  24990. var reader = response.body.getReader();
  24991. var pump = function pump() {
  24992. return reader.read().then(function (data) {
  24993. if (data.done) {
  24994. if (chunkCache.dataLength) {
  24995. onProgress(stats, context, chunkCache.flush(), response);
  24996. }
  24997. return Promise.resolve(new ArrayBuffer(0));
  24998. }
  24999. var chunk = data.value;
  25000. var len = chunk.length;
  25001. stats.loaded += len;
  25002. if (len < highWaterMark || chunkCache.dataLength) {
  25003. // The current chunk is too small to to be emitted or the cache already has data
  25004. // Push it to the cache
  25005. chunkCache.push(chunk);
  25006. if (chunkCache.dataLength >= highWaterMark) {
  25007. // flush in order to join the typed arrays
  25008. onProgress(stats, context, chunkCache.flush(), response);
  25009. }
  25010. } else {
  25011. // If there's nothing cached already, and the chache is large enough
  25012. // just emit the progress event
  25013. onProgress(stats, context, chunk, response);
  25014. }
  25015. return pump();
  25016. }).catch(function () {
  25017. /* aborted */
  25018. return Promise.reject();
  25019. });
  25020. };
  25021. return pump();
  25022. };
  25023. return FetchLoader;
  25024. }();
  25025. function getRequestParameters(context, signal) {
  25026. var initParams = {
  25027. method: 'GET',
  25028. mode: 'cors',
  25029. credentials: 'same-origin',
  25030. signal: signal,
  25031. headers: new self.Headers(_extends({}, context.headers))
  25032. };
  25033. if (context.rangeEnd) {
  25034. initParams.headers.set('Range', 'bytes=' + context.rangeStart + '-' + String(context.rangeEnd - 1));
  25035. }
  25036. return initParams;
  25037. }
  25038. function getByteRangeLength(byteRangeHeader) {
  25039. var result = BYTERANGE.exec(byteRangeHeader);
  25040. if (result) {
  25041. return parseInt(result[2]) - parseInt(result[1]) + 1;
  25042. }
  25043. }
  25044. function getContentLength(headers) {
  25045. var contentRange = headers.get('Content-Range');
  25046. if (contentRange) {
  25047. var byteRangeLength = getByteRangeLength(contentRange);
  25048. if (isFiniteNumber(byteRangeLength)) {
  25049. return byteRangeLength;
  25050. }
  25051. }
  25052. var contentLength = headers.get('Content-Length');
  25053. if (contentLength) {
  25054. return parseInt(contentLength);
  25055. }
  25056. }
  25057. function getRequest(context, initParams) {
  25058. return new self.Request(context.url, initParams);
  25059. }
  25060. var FetchError = /*#__PURE__*/function (_Error) {
  25061. _inheritsLoose(FetchError, _Error);
  25062. function FetchError(message, code, details) {
  25063. var _this2;
  25064. _this2 = _Error.call(this, message) || this;
  25065. _this2.code = void 0;
  25066. _this2.details = void 0;
  25067. _this2.code = code;
  25068. _this2.details = details;
  25069. return _this2;
  25070. }
  25071. return FetchError;
  25072. }( /*#__PURE__*/_wrapNativeSuper(Error));
  25073. var WHITESPACE_CHAR = /\s/;
  25074. var Cues = {
  25075. newCue: function newCue(track, startTime, endTime, captionScreen) {
  25076. var result = [];
  25077. var row;
  25078. // the type data states this is VTTCue, but it can potentially be a TextTrackCue on old browsers
  25079. var cue;
  25080. var indenting;
  25081. var indent;
  25082. var text;
  25083. var Cue = self.VTTCue || self.TextTrackCue;
  25084. for (var r = 0; r < captionScreen.rows.length; r++) {
  25085. row = captionScreen.rows[r];
  25086. indenting = true;
  25087. indent = 0;
  25088. text = '';
  25089. if (!row.isEmpty()) {
  25090. var _track$cues;
  25091. for (var c = 0; c < row.chars.length; c++) {
  25092. if (WHITESPACE_CHAR.test(row.chars[c].uchar) && indenting) {
  25093. indent++;
  25094. } else {
  25095. text += row.chars[c].uchar;
  25096. indenting = false;
  25097. }
  25098. }
  25099. // To be used for cleaning-up orphaned roll-up captions
  25100. row.cueStartTime = startTime;
  25101. // Give a slight bump to the endTime if it's equal to startTime to avoid a SyntaxError in IE
  25102. if (startTime === endTime) {
  25103. endTime += 0.0001;
  25104. }
  25105. if (indent >= 16) {
  25106. indent--;
  25107. } else {
  25108. indent++;
  25109. }
  25110. var cueText = fixLineBreaks(text.trim());
  25111. var id = generateCueId(startTime, endTime, cueText);
  25112. // If this cue already exists in the track do not push it
  25113. if (!(track != null && (_track$cues = track.cues) != null && _track$cues.getCueById(id))) {
  25114. cue = new Cue(startTime, endTime, cueText);
  25115. cue.id = id;
  25116. cue.line = r + 1;
  25117. cue.align = 'left';
  25118. // Clamp the position between 10 and 80 percent (CEA-608 PAC indent code)
  25119. // https://dvcs.w3.org/hg/text-tracks/raw-file/default/608toVTT/608toVTT.html#positioning-in-cea-608
  25120. // Firefox throws an exception and captions break with out of bounds 0-100 values
  25121. cue.position = 10 + Math.min(80, Math.floor(indent * 8 / 32) * 10);
  25122. result.push(cue);
  25123. }
  25124. }
  25125. }
  25126. if (track && result.length) {
  25127. // Sort bottom cues in reverse order so that they render in line order when overlapping in Chrome
  25128. result.sort(function (cueA, cueB) {
  25129. if (cueA.line === 'auto' || cueB.line === 'auto') {
  25130. return 0;
  25131. }
  25132. if (cueA.line > 8 && cueB.line > 8) {
  25133. return cueB.line - cueA.line;
  25134. }
  25135. return cueA.line - cueB.line;
  25136. });
  25137. result.forEach(function (cue) {
  25138. return addCueToTrack(track, cue);
  25139. });
  25140. }
  25141. return result;
  25142. }
  25143. };
  25144. /**
  25145. * @deprecated use fragLoadPolicy.default
  25146. */
  25147. /**
  25148. * @deprecated use manifestLoadPolicy.default and playlistLoadPolicy.default
  25149. */
  25150. var defaultLoadPolicy = {
  25151. maxTimeToFirstByteMs: 8000,
  25152. maxLoadTimeMs: 20000,
  25153. timeoutRetry: null,
  25154. errorRetry: null
  25155. };
  25156. /**
  25157. * @ignore
  25158. * If possible, keep hlsDefaultConfig shallow
  25159. * It is cloned whenever a new Hls instance is created, by keeping the config
  25160. * shallow the properties are cloned, and we don't end up manipulating the default
  25161. */
  25162. var hlsDefaultConfig = _objectSpread2(_objectSpread2({
  25163. autoStartLoad: true,
  25164. // used by stream-controller
  25165. startPosition: -1,
  25166. // used by stream-controller
  25167. defaultAudioCodec: undefined,
  25168. // used by stream-controller
  25169. debug: false,
  25170. // used by logger
  25171. capLevelOnFPSDrop: false,
  25172. // used by fps-controller
  25173. capLevelToPlayerSize: false,
  25174. // used by cap-level-controller
  25175. ignoreDevicePixelRatio: false,
  25176. // used by cap-level-controller
  25177. preferManagedMediaSource: true,
  25178. initialLiveManifestSize: 1,
  25179. // used by stream-controller
  25180. maxBufferLength: 30,
  25181. // used by stream-controller
  25182. backBufferLength: Infinity,
  25183. // used by buffer-controller
  25184. frontBufferFlushThreshold: Infinity,
  25185. maxBufferSize: 60 * 1000 * 1000,
  25186. // used by stream-controller
  25187. maxBufferHole: 0.1,
  25188. // used by stream-controller
  25189. highBufferWatchdogPeriod: 2,
  25190. // used by stream-controller
  25191. nudgeOffset: 0.1,
  25192. // used by stream-controller
  25193. nudgeMaxRetry: 3,
  25194. // used by stream-controller
  25195. maxFragLookUpTolerance: 0.25,
  25196. // used by stream-controller
  25197. liveSyncDurationCount: 3,
  25198. // used by latency-controller
  25199. liveMaxLatencyDurationCount: Infinity,
  25200. // used by latency-controller
  25201. liveSyncDuration: undefined,
  25202. // used by latency-controller
  25203. liveMaxLatencyDuration: undefined,
  25204. // used by latency-controller
  25205. maxLiveSyncPlaybackRate: 1,
  25206. // used by latency-controller
  25207. liveDurationInfinity: false,
  25208. // used by buffer-controller
  25209. /**
  25210. * @deprecated use backBufferLength
  25211. */
  25212. liveBackBufferLength: null,
  25213. // used by buffer-controller
  25214. maxMaxBufferLength: 600,
  25215. // used by stream-controller
  25216. enableWorker: true,
  25217. // used by transmuxer
  25218. workerPath: null,
  25219. // used by transmuxer
  25220. enableSoftwareAES: true,
  25221. // used by decrypter
  25222. startLevel: undefined,
  25223. // used by level-controller
  25224. startFragPrefetch: false,
  25225. // used by stream-controller
  25226. fpsDroppedMonitoringPeriod: 5000,
  25227. // used by fps-controller
  25228. fpsDroppedMonitoringThreshold: 0.2,
  25229. // used by fps-controller
  25230. appendErrorMaxRetry: 3,
  25231. // used by buffer-controller
  25232. loader: XhrLoader,
  25233. // loader: FetchLoader,
  25234. fLoader: undefined,
  25235. // used by fragment-loader
  25236. pLoader: undefined,
  25237. // used by playlist-loader
  25238. xhrSetup: undefined,
  25239. // used by xhr-loader
  25240. licenseXhrSetup: undefined,
  25241. // used by eme-controller
  25242. licenseResponseCallback: undefined,
  25243. // used by eme-controller
  25244. abrController: AbrController,
  25245. bufferController: BufferController,
  25246. capLevelController: CapLevelController,
  25247. errorController: ErrorController,
  25248. fpsController: FPSController,
  25249. stretchShortVideoTrack: false,
  25250. // used by mp4-remuxer
  25251. maxAudioFramesDrift: 1,
  25252. // used by mp4-remuxer
  25253. forceKeyFrameOnDiscontinuity: true,
  25254. // used by ts-demuxer
  25255. abrEwmaFastLive: 3,
  25256. // used by abr-controller
  25257. abrEwmaSlowLive: 9,
  25258. // used by abr-controller
  25259. abrEwmaFastVoD: 3,
  25260. // used by abr-controller
  25261. abrEwmaSlowVoD: 9,
  25262. // used by abr-controller
  25263. abrEwmaDefaultEstimate: 5e5,
  25264. // 500 kbps // used by abr-controller
  25265. abrEwmaDefaultEstimateMax: 5e6,
  25266. // 5 mbps
  25267. abrBandWidthFactor: 0.95,
  25268. // used by abr-controller
  25269. abrBandWidthUpFactor: 0.7,
  25270. // used by abr-controller
  25271. abrMaxWithRealBitrate: false,
  25272. // used by abr-controller
  25273. maxStarvationDelay: 4,
  25274. // used by abr-controller
  25275. maxLoadingDelay: 4,
  25276. // used by abr-controller
  25277. minAutoBitrate: 0,
  25278. // used by hls
  25279. emeEnabled: false,
  25280. // used by eme-controller
  25281. widevineLicenseUrl: undefined,
  25282. // used by eme-controller
  25283. drmSystems: {},
  25284. // used by eme-controller
  25285. drmSystemOptions: {},
  25286. // used by eme-controller
  25287. requestMediaKeySystemAccessFunc: requestMediaKeySystemAccess ,
  25288. // used by eme-controller
  25289. testBandwidth: true,
  25290. progressive: false,
  25291. lowLatencyMode: true,
  25292. cmcd: undefined,
  25293. enableDateRangeMetadataCues: true,
  25294. enableEmsgMetadataCues: true,
  25295. enableID3MetadataCues: true,
  25296. useMediaCapabilities: true,
  25297. certLoadPolicy: {
  25298. default: defaultLoadPolicy
  25299. },
  25300. keyLoadPolicy: {
  25301. default: {
  25302. maxTimeToFirstByteMs: 8000,
  25303. maxLoadTimeMs: 20000,
  25304. timeoutRetry: {
  25305. maxNumRetry: 1,
  25306. retryDelayMs: 1000,
  25307. maxRetryDelayMs: 20000,
  25308. backoff: 'linear'
  25309. },
  25310. errorRetry: {
  25311. maxNumRetry: 8,
  25312. retryDelayMs: 1000,
  25313. maxRetryDelayMs: 20000,
  25314. backoff: 'linear'
  25315. }
  25316. }
  25317. },
  25318. manifestLoadPolicy: {
  25319. default: {
  25320. maxTimeToFirstByteMs: Infinity,
  25321. maxLoadTimeMs: 20000,
  25322. timeoutRetry: {
  25323. maxNumRetry: 2,
  25324. retryDelayMs: 0,
  25325. maxRetryDelayMs: 0
  25326. },
  25327. errorRetry: {
  25328. maxNumRetry: 1,
  25329. retryDelayMs: 1000,
  25330. maxRetryDelayMs: 8000
  25331. }
  25332. }
  25333. },
  25334. playlistLoadPolicy: {
  25335. default: {
  25336. maxTimeToFirstByteMs: 10000,
  25337. maxLoadTimeMs: 20000,
  25338. timeoutRetry: {
  25339. maxNumRetry: 2,
  25340. retryDelayMs: 0,
  25341. maxRetryDelayMs: 0
  25342. },
  25343. errorRetry: {
  25344. maxNumRetry: 2,
  25345. retryDelayMs: 1000,
  25346. maxRetryDelayMs: 8000
  25347. }
  25348. }
  25349. },
  25350. fragLoadPolicy: {
  25351. default: {
  25352. maxTimeToFirstByteMs: 10000,
  25353. maxLoadTimeMs: 120000,
  25354. timeoutRetry: {
  25355. maxNumRetry: 4,
  25356. retryDelayMs: 0,
  25357. maxRetryDelayMs: 0
  25358. },
  25359. errorRetry: {
  25360. maxNumRetry: 6,
  25361. retryDelayMs: 1000,
  25362. maxRetryDelayMs: 8000
  25363. }
  25364. }
  25365. },
  25366. steeringManifestLoadPolicy: {
  25367. default: {
  25368. maxTimeToFirstByteMs: 10000,
  25369. maxLoadTimeMs: 20000,
  25370. timeoutRetry: {
  25371. maxNumRetry: 2,
  25372. retryDelayMs: 0,
  25373. maxRetryDelayMs: 0
  25374. },
  25375. errorRetry: {
  25376. maxNumRetry: 1,
  25377. retryDelayMs: 1000,
  25378. maxRetryDelayMs: 8000
  25379. }
  25380. }
  25381. },
  25382. // These default settings are deprecated in favor of the above policies
  25383. // and are maintained for backwards compatibility
  25384. manifestLoadingTimeOut: 10000,
  25385. manifestLoadingMaxRetry: 1,
  25386. manifestLoadingRetryDelay: 1000,
  25387. manifestLoadingMaxRetryTimeout: 64000,
  25388. levelLoadingTimeOut: 10000,
  25389. levelLoadingMaxRetry: 4,
  25390. levelLoadingRetryDelay: 1000,
  25391. levelLoadingMaxRetryTimeout: 64000,
  25392. fragLoadingTimeOut: 20000,
  25393. fragLoadingMaxRetry: 6,
  25394. fragLoadingRetryDelay: 1000,
  25395. fragLoadingMaxRetryTimeout: 64000
  25396. }, timelineConfig()), {}, {
  25397. subtitleStreamController: SubtitleStreamController ,
  25398. subtitleTrackController: SubtitleTrackController ,
  25399. timelineController: TimelineController ,
  25400. audioStreamController: AudioStreamController ,
  25401. audioTrackController: AudioTrackController ,
  25402. emeController: EMEController ,
  25403. cmcdController: CMCDController ,
  25404. contentSteeringController: ContentSteeringController
  25405. });
  25406. function timelineConfig() {
  25407. return {
  25408. cueHandler: Cues,
  25409. // used by timeline-controller
  25410. enableWebVTT: true,
  25411. // used by timeline-controller
  25412. enableIMSC1: true,
  25413. // used by timeline-controller
  25414. enableCEA708Captions: true,
  25415. // used by timeline-controller
  25416. captionsTextTrack1Label: 'English',
  25417. // used by timeline-controller
  25418. captionsTextTrack1LanguageCode: 'en',
  25419. // used by timeline-controller
  25420. captionsTextTrack2Label: 'Spanish',
  25421. // used by timeline-controller
  25422. captionsTextTrack2LanguageCode: 'es',
  25423. // used by timeline-controller
  25424. captionsTextTrack3Label: 'Unknown CC',
  25425. // used by timeline-controller
  25426. captionsTextTrack3LanguageCode: '',
  25427. // used by timeline-controller
  25428. captionsTextTrack4Label: 'Unknown CC',
  25429. // used by timeline-controller
  25430. captionsTextTrack4LanguageCode: '',
  25431. // used by timeline-controller
  25432. renderTextTracksNatively: true
  25433. };
  25434. }
  25435. /**
  25436. * @ignore
  25437. */
  25438. function mergeConfig(defaultConfig, userConfig) {
  25439. if ((userConfig.liveSyncDurationCount || userConfig.liveMaxLatencyDurationCount) && (userConfig.liveSyncDuration || userConfig.liveMaxLatencyDuration)) {
  25440. throw new Error("Illegal hls.js config: don't mix up liveSyncDurationCount/liveMaxLatencyDurationCount and liveSyncDuration/liveMaxLatencyDuration");
  25441. }
  25442. if (userConfig.liveMaxLatencyDurationCount !== undefined && (userConfig.liveSyncDurationCount === undefined || userConfig.liveMaxLatencyDurationCount <= userConfig.liveSyncDurationCount)) {
  25443. throw new Error('Illegal hls.js config: "liveMaxLatencyDurationCount" must be greater than "liveSyncDurationCount"');
  25444. }
  25445. if (userConfig.liveMaxLatencyDuration !== undefined && (userConfig.liveSyncDuration === undefined || userConfig.liveMaxLatencyDuration <= userConfig.liveSyncDuration)) {
  25446. throw new Error('Illegal hls.js config: "liveMaxLatencyDuration" must be greater than "liveSyncDuration"');
  25447. }
  25448. var defaultsCopy = deepCpy(defaultConfig);
  25449. // Backwards compatibility with deprecated config values
  25450. var deprecatedSettingTypes = ['manifest', 'level', 'frag'];
  25451. var deprecatedSettings = ['TimeOut', 'MaxRetry', 'RetryDelay', 'MaxRetryTimeout'];
  25452. deprecatedSettingTypes.forEach(function (type) {
  25453. var policyName = (type === 'level' ? 'playlist' : type) + "LoadPolicy";
  25454. var policyNotSet = userConfig[policyName] === undefined;
  25455. var report = [];
  25456. deprecatedSettings.forEach(function (setting) {
  25457. var deprecatedSetting = type + "Loading" + setting;
  25458. var value = userConfig[deprecatedSetting];
  25459. if (value !== undefined && policyNotSet) {
  25460. report.push(deprecatedSetting);
  25461. var settings = defaultsCopy[policyName].default;
  25462. userConfig[policyName] = {
  25463. default: settings
  25464. };
  25465. switch (setting) {
  25466. case 'TimeOut':
  25467. settings.maxLoadTimeMs = value;
  25468. settings.maxTimeToFirstByteMs = value;
  25469. break;
  25470. case 'MaxRetry':
  25471. settings.errorRetry.maxNumRetry = value;
  25472. settings.timeoutRetry.maxNumRetry = value;
  25473. break;
  25474. case 'RetryDelay':
  25475. settings.errorRetry.retryDelayMs = value;
  25476. settings.timeoutRetry.retryDelayMs = value;
  25477. break;
  25478. case 'MaxRetryTimeout':
  25479. settings.errorRetry.maxRetryDelayMs = value;
  25480. settings.timeoutRetry.maxRetryDelayMs = value;
  25481. break;
  25482. }
  25483. }
  25484. });
  25485. if (report.length) {
  25486. logger.warn("hls.js config: \"" + report.join('", "') + "\" setting(s) are deprecated, use \"" + policyName + "\": " + JSON.stringify(userConfig[policyName]));
  25487. }
  25488. });
  25489. return _objectSpread2(_objectSpread2({}, defaultsCopy), userConfig);
  25490. }
  25491. function deepCpy(obj) {
  25492. if (obj && typeof obj === 'object') {
  25493. if (Array.isArray(obj)) {
  25494. return obj.map(deepCpy);
  25495. }
  25496. return Object.keys(obj).reduce(function (result, key) {
  25497. result[key] = deepCpy(obj[key]);
  25498. return result;
  25499. }, {});
  25500. }
  25501. return obj;
  25502. }
  25503. /**
  25504. * @ignore
  25505. */
  25506. function enableStreamingMode(config) {
  25507. var currentLoader = config.loader;
  25508. if (currentLoader !== FetchLoader && currentLoader !== XhrLoader) {
  25509. // If a developer has configured their own loader, respect that choice
  25510. logger.log('[config]: Custom loader detected, cannot enable progressive streaming');
  25511. config.progressive = false;
  25512. } else {
  25513. var canStreamProgressively = fetchSupported();
  25514. if (canStreamProgressively) {
  25515. config.loader = FetchLoader;
  25516. config.progressive = true;
  25517. config.enableSoftwareAES = true;
  25518. logger.log('[config]: Progressive streaming enabled, using FetchLoader');
  25519. }
  25520. }
  25521. }
  25522. var chromeOrFirefox;
  25523. var LevelController = /*#__PURE__*/function (_BasePlaylistControll) {
  25524. _inheritsLoose(LevelController, _BasePlaylistControll);
  25525. function LevelController(hls, contentSteeringController) {
  25526. var _this;
  25527. _this = _BasePlaylistControll.call(this, hls, '[level-controller]') || this;
  25528. _this._levels = [];
  25529. _this._firstLevel = -1;
  25530. _this._maxAutoLevel = -1;
  25531. _this._startLevel = void 0;
  25532. _this.currentLevel = null;
  25533. _this.currentLevelIndex = -1;
  25534. _this.manualLevelIndex = -1;
  25535. _this.steering = void 0;
  25536. _this.onParsedComplete = void 0;
  25537. _this.steering = contentSteeringController;
  25538. _this._registerListeners();
  25539. return _this;
  25540. }
  25541. var _proto = LevelController.prototype;
  25542. _proto._registerListeners = function _registerListeners() {
  25543. var hls = this.hls;
  25544. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  25545. hls.on(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  25546. hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  25547. hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  25548. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  25549. hls.on(Events.ERROR, this.onError, this);
  25550. };
  25551. _proto._unregisterListeners = function _unregisterListeners() {
  25552. var hls = this.hls;
  25553. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  25554. hls.off(Events.MANIFEST_LOADED, this.onManifestLoaded, this);
  25555. hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  25556. hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  25557. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  25558. hls.off(Events.ERROR, this.onError, this);
  25559. };
  25560. _proto.destroy = function destroy() {
  25561. this._unregisterListeners();
  25562. this.steering = null;
  25563. this.resetLevels();
  25564. _BasePlaylistControll.prototype.destroy.call(this);
  25565. };
  25566. _proto.stopLoad = function stopLoad() {
  25567. var levels = this._levels;
  25568. // clean up live level details to force reload them, and reset load errors
  25569. levels.forEach(function (level) {
  25570. level.loadError = 0;
  25571. level.fragmentError = 0;
  25572. });
  25573. _BasePlaylistControll.prototype.stopLoad.call(this);
  25574. };
  25575. _proto.resetLevels = function resetLevels() {
  25576. this._startLevel = undefined;
  25577. this.manualLevelIndex = -1;
  25578. this.currentLevelIndex = -1;
  25579. this.currentLevel = null;
  25580. this._levels = [];
  25581. this._maxAutoLevel = -1;
  25582. };
  25583. _proto.onManifestLoading = function onManifestLoading(event, data) {
  25584. this.resetLevels();
  25585. };
  25586. _proto.onManifestLoaded = function onManifestLoaded(event, data) {
  25587. var preferManagedMediaSource = this.hls.config.preferManagedMediaSource;
  25588. var levels = [];
  25589. var redundantSet = {};
  25590. var generatePathwaySet = {};
  25591. var resolutionFound = false;
  25592. var videoCodecFound = false;
  25593. var audioCodecFound = false;
  25594. data.levels.forEach(function (levelParsed) {
  25595. var _audioCodec, _videoCodec;
  25596. var attributes = levelParsed.attrs;
  25597. // erase audio codec info if browser does not support mp4a.40.34.
  25598. // demuxer will autodetect codec and fallback to mpeg/audio
  25599. var audioCodec = levelParsed.audioCodec,
  25600. videoCodec = levelParsed.videoCodec;
  25601. if (((_audioCodec = audioCodec) == null ? void 0 : _audioCodec.indexOf('mp4a.40.34')) !== -1) {
  25602. chromeOrFirefox || (chromeOrFirefox = /chrome|firefox/i.test(navigator.userAgent));
  25603. if (chromeOrFirefox) {
  25604. levelParsed.audioCodec = audioCodec = undefined;
  25605. }
  25606. }
  25607. if (audioCodec) {
  25608. levelParsed.audioCodec = audioCodec = getCodecCompatibleName(audioCodec, preferManagedMediaSource);
  25609. }
  25610. if (((_videoCodec = videoCodec) == null ? void 0 : _videoCodec.indexOf('avc1')) === 0) {
  25611. videoCodec = levelParsed.videoCodec = convertAVC1ToAVCOTI(videoCodec);
  25612. }
  25613. // only keep levels with supported audio/video codecs
  25614. var width = levelParsed.width,
  25615. height = levelParsed.height,
  25616. unknownCodecs = levelParsed.unknownCodecs;
  25617. resolutionFound || (resolutionFound = !!(width && height));
  25618. videoCodecFound || (videoCodecFound = !!videoCodec);
  25619. audioCodecFound || (audioCodecFound = !!audioCodec);
  25620. if (unknownCodecs != null && unknownCodecs.length || audioCodec && !areCodecsMediaSourceSupported(audioCodec, 'audio', preferManagedMediaSource) || videoCodec && !areCodecsMediaSourceSupported(videoCodec, 'video', preferManagedMediaSource)) {
  25621. return;
  25622. }
  25623. var CODECS = attributes.CODECS,
  25624. FRAMERATE = attributes['FRAME-RATE'],
  25625. HDCP = attributes['HDCP-LEVEL'],
  25626. PATHWAY = attributes['PATHWAY-ID'],
  25627. RESOLUTION = attributes.RESOLUTION,
  25628. VIDEO_RANGE = attributes['VIDEO-RANGE'];
  25629. var contentSteeringPrefix = (PATHWAY || '.') + "-";
  25630. var levelKey = "" + contentSteeringPrefix + levelParsed.bitrate + "-" + RESOLUTION + "-" + FRAMERATE + "-" + CODECS + "-" + VIDEO_RANGE + "-" + HDCP;
  25631. if (!redundantSet[levelKey]) {
  25632. var level = new Level(levelParsed);
  25633. redundantSet[levelKey] = level;
  25634. generatePathwaySet[levelKey] = 1;
  25635. levels.push(level);
  25636. } else if (redundantSet[levelKey].uri !== levelParsed.url && !levelParsed.attrs['PATHWAY-ID']) {
  25637. // Assign Pathway IDs to Redundant Streams (default Pathways is ".". Redundant Streams "..", "...", and so on.)
  25638. // Content Steering controller to handles Pathway fallback on error
  25639. var pathwayCount = generatePathwaySet[levelKey] += 1;
  25640. levelParsed.attrs['PATHWAY-ID'] = new Array(pathwayCount + 1).join('.');
  25641. var _level = new Level(levelParsed);
  25642. redundantSet[levelKey] = _level;
  25643. levels.push(_level);
  25644. } else {
  25645. redundantSet[levelKey].addGroupId('audio', attributes.AUDIO);
  25646. redundantSet[levelKey].addGroupId('text', attributes.SUBTITLES);
  25647. }
  25648. });
  25649. this.filterAndSortMediaOptions(levels, data, resolutionFound, videoCodecFound, audioCodecFound);
  25650. };
  25651. _proto.filterAndSortMediaOptions = function filterAndSortMediaOptions(filteredLevels, data, resolutionFound, videoCodecFound, audioCodecFound) {
  25652. var _this2 = this;
  25653. var audioTracks = [];
  25654. var subtitleTracks = [];
  25655. var levels = filteredLevels;
  25656. // remove audio-only and invalid video-range levels if we also have levels with video codecs or RESOLUTION signalled
  25657. if ((resolutionFound || videoCodecFound) && audioCodecFound) {
  25658. levels = levels.filter(function (_ref) {
  25659. var videoCodec = _ref.videoCodec,
  25660. videoRange = _ref.videoRange,
  25661. width = _ref.width,
  25662. height = _ref.height;
  25663. return (!!videoCodec || !!(width && height)) && isVideoRange(videoRange);
  25664. });
  25665. }
  25666. if (levels.length === 0) {
  25667. // Dispatch error after MANIFEST_LOADED is done propagating
  25668. Promise.resolve().then(function () {
  25669. if (_this2.hls) {
  25670. if (data.levels.length) {
  25671. _this2.warn("One or more CODECS in variant not supported: " + JSON.stringify(data.levels[0].attrs));
  25672. }
  25673. var error = new Error('no level with compatible codecs found in manifest');
  25674. _this2.hls.trigger(Events.ERROR, {
  25675. type: ErrorTypes.MEDIA_ERROR,
  25676. details: ErrorDetails.MANIFEST_INCOMPATIBLE_CODECS_ERROR,
  25677. fatal: true,
  25678. url: data.url,
  25679. error: error,
  25680. reason: error.message
  25681. });
  25682. }
  25683. });
  25684. return;
  25685. }
  25686. if (data.audioTracks) {
  25687. var preferManagedMediaSource = this.hls.config.preferManagedMediaSource;
  25688. audioTracks = data.audioTracks.filter(function (track) {
  25689. return !track.audioCodec || areCodecsMediaSourceSupported(track.audioCodec, 'audio', preferManagedMediaSource);
  25690. });
  25691. // Assign ids after filtering as array indices by group-id
  25692. assignTrackIdsByGroup(audioTracks);
  25693. }
  25694. if (data.subtitles) {
  25695. subtitleTracks = data.subtitles;
  25696. assignTrackIdsByGroup(subtitleTracks);
  25697. }
  25698. // start bitrate is the first bitrate of the manifest
  25699. var unsortedLevels = levels.slice(0);
  25700. // sort levels from lowest to highest
  25701. levels.sort(function (a, b) {
  25702. if (a.attrs['HDCP-LEVEL'] !== b.attrs['HDCP-LEVEL']) {
  25703. return (a.attrs['HDCP-LEVEL'] || '') > (b.attrs['HDCP-LEVEL'] || '') ? 1 : -1;
  25704. }
  25705. // sort on height before bitrate for cap-level-controller
  25706. if (resolutionFound && a.height !== b.height) {
  25707. return a.height - b.height;
  25708. }
  25709. if (a.frameRate !== b.frameRate) {
  25710. return a.frameRate - b.frameRate;
  25711. }
  25712. if (a.videoRange !== b.videoRange) {
  25713. return VideoRangeValues.indexOf(a.videoRange) - VideoRangeValues.indexOf(b.videoRange);
  25714. }
  25715. if (a.videoCodec !== b.videoCodec) {
  25716. var valueA = videoCodecPreferenceValue(a.videoCodec);
  25717. var valueB = videoCodecPreferenceValue(b.videoCodec);
  25718. if (valueA !== valueB) {
  25719. return valueB - valueA;
  25720. }
  25721. }
  25722. if (a.uri === b.uri && a.codecSet !== b.codecSet) {
  25723. var _valueA = codecsSetSelectionPreferenceValue(a.codecSet);
  25724. var _valueB = codecsSetSelectionPreferenceValue(b.codecSet);
  25725. if (_valueA !== _valueB) {
  25726. return _valueB - _valueA;
  25727. }
  25728. }
  25729. if (a.averageBitrate !== b.averageBitrate) {
  25730. return a.averageBitrate - b.averageBitrate;
  25731. }
  25732. return 0;
  25733. });
  25734. var firstLevelInPlaylist = unsortedLevels[0];
  25735. if (this.steering) {
  25736. levels = this.steering.filterParsedLevels(levels);
  25737. if (levels.length !== unsortedLevels.length) {
  25738. for (var i = 0; i < unsortedLevels.length; i++) {
  25739. if (unsortedLevels[i].pathwayId === levels[0].pathwayId) {
  25740. firstLevelInPlaylist = unsortedLevels[i];
  25741. break;
  25742. }
  25743. }
  25744. }
  25745. }
  25746. this._levels = levels;
  25747. // find index of first level in sorted levels
  25748. for (var _i = 0; _i < levels.length; _i++) {
  25749. if (levels[_i] === firstLevelInPlaylist) {
  25750. var _this$hls$userConfig;
  25751. this._firstLevel = _i;
  25752. var firstLevelBitrate = firstLevelInPlaylist.bitrate;
  25753. var bandwidthEstimate = this.hls.bandwidthEstimate;
  25754. this.log("manifest loaded, " + levels.length + " level(s) found, first bitrate: " + firstLevelBitrate);
  25755. // Update default bwe to first variant bitrate as long it has not been configured or set
  25756. if (((_this$hls$userConfig = this.hls.userConfig) == null ? void 0 : _this$hls$userConfig.abrEwmaDefaultEstimate) === undefined) {
  25757. var startingBwEstimate = Math.min(firstLevelBitrate, this.hls.config.abrEwmaDefaultEstimateMax);
  25758. if (startingBwEstimate > bandwidthEstimate && bandwidthEstimate === hlsDefaultConfig.abrEwmaDefaultEstimate) {
  25759. this.hls.bandwidthEstimate = startingBwEstimate;
  25760. }
  25761. }
  25762. break;
  25763. }
  25764. }
  25765. // Audio is only alternate if manifest include a URI along with the audio group tag,
  25766. // and this is not an audio-only stream where levels contain audio-only
  25767. var audioOnly = audioCodecFound && !videoCodecFound;
  25768. var edata = {
  25769. levels: levels,
  25770. audioTracks: audioTracks,
  25771. subtitleTracks: subtitleTracks,
  25772. sessionData: data.sessionData,
  25773. sessionKeys: data.sessionKeys,
  25774. firstLevel: this._firstLevel,
  25775. stats: data.stats,
  25776. audio: audioCodecFound,
  25777. video: videoCodecFound,
  25778. altAudio: !audioOnly && audioTracks.some(function (t) {
  25779. return !!t.url;
  25780. })
  25781. };
  25782. this.hls.trigger(Events.MANIFEST_PARSED, edata);
  25783. // Initiate loading after all controllers have received MANIFEST_PARSED
  25784. if (this.hls.config.autoStartLoad || this.hls.forceStartLoad) {
  25785. this.hls.startLoad(this.hls.config.startPosition);
  25786. }
  25787. };
  25788. _proto.onError = function onError(event, data) {
  25789. if (data.fatal || !data.context) {
  25790. return;
  25791. }
  25792. if (data.context.type === PlaylistContextType.LEVEL && data.context.level === this.level) {
  25793. this.checkRetry(data);
  25794. }
  25795. }
  25796. // reset errors on the successful load of a fragment
  25797. ;
  25798. _proto.onFragBuffered = function onFragBuffered(event, _ref2) {
  25799. var frag = _ref2.frag;
  25800. if (frag !== undefined && frag.type === PlaylistLevelType.MAIN) {
  25801. var el = frag.elementaryStreams;
  25802. if (!Object.keys(el).some(function (type) {
  25803. return !!el[type];
  25804. })) {
  25805. return;
  25806. }
  25807. var level = this._levels[frag.level];
  25808. if (level != null && level.loadError) {
  25809. this.log("Resetting level error count of " + level.loadError + " on frag buffered");
  25810. level.loadError = 0;
  25811. }
  25812. }
  25813. };
  25814. _proto.onLevelLoaded = function onLevelLoaded(event, data) {
  25815. var _data$deliveryDirecti2;
  25816. var level = data.level,
  25817. details = data.details;
  25818. var curLevel = this._levels[level];
  25819. if (!curLevel) {
  25820. var _data$deliveryDirecti;
  25821. this.warn("Invalid level index " + level);
  25822. if ((_data$deliveryDirecti = data.deliveryDirectives) != null && _data$deliveryDirecti.skip) {
  25823. details.deltaUpdateFailed = true;
  25824. }
  25825. return;
  25826. }
  25827. // only process level loaded events matching with expected level
  25828. if (level === this.currentLevelIndex) {
  25829. // reset level load error counter on successful level loaded only if there is no issues with fragments
  25830. if (curLevel.fragmentError === 0) {
  25831. curLevel.loadError = 0;
  25832. }
  25833. this.playlistLoaded(level, data, curLevel.details);
  25834. } else if ((_data$deliveryDirecti2 = data.deliveryDirectives) != null && _data$deliveryDirecti2.skip) {
  25835. // received a delta playlist update that cannot be merged
  25836. details.deltaUpdateFailed = true;
  25837. }
  25838. };
  25839. _proto.loadPlaylist = function loadPlaylist(hlsUrlParameters) {
  25840. _BasePlaylistControll.prototype.loadPlaylist.call(this);
  25841. var currentLevelIndex = this.currentLevelIndex;
  25842. var currentLevel = this.currentLevel;
  25843. if (currentLevel && this.shouldLoadPlaylist(currentLevel)) {
  25844. var url = currentLevel.uri;
  25845. if (hlsUrlParameters) {
  25846. try {
  25847. url = hlsUrlParameters.addDirectives(url);
  25848. } catch (error) {
  25849. this.warn("Could not construct new URL with HLS Delivery Directives: " + error);
  25850. }
  25851. }
  25852. var pathwayId = currentLevel.attrs['PATHWAY-ID'];
  25853. this.log("Loading level index " + currentLevelIndex + ((hlsUrlParameters == null ? void 0 : hlsUrlParameters.msn) !== undefined ? ' at sn ' + hlsUrlParameters.msn + ' part ' + hlsUrlParameters.part : '') + " with" + (pathwayId ? ' Pathway ' + pathwayId : '') + " " + url);
  25854. // console.log('Current audio track group ID:', this.hls.audioTracks[this.hls.audioTrack].groupId);
  25855. // console.log('New video quality level audio group id:', levelObject.attrs.AUDIO, level);
  25856. this.clearTimer();
  25857. this.hls.trigger(Events.LEVEL_LOADING, {
  25858. url: url,
  25859. level: currentLevelIndex,
  25860. pathwayId: currentLevel.attrs['PATHWAY-ID'],
  25861. id: 0,
  25862. // Deprecated Level urlId
  25863. deliveryDirectives: hlsUrlParameters || null
  25864. });
  25865. }
  25866. };
  25867. _proto.removeLevel = function removeLevel(levelIndex) {
  25868. var _this3 = this,
  25869. _this$currentLevel;
  25870. var levels = this._levels.filter(function (level, index) {
  25871. if (index !== levelIndex) {
  25872. return true;
  25873. }
  25874. if (_this3.steering) {
  25875. _this3.steering.removeLevel(level);
  25876. }
  25877. if (level === _this3.currentLevel) {
  25878. _this3.currentLevel = null;
  25879. _this3.currentLevelIndex = -1;
  25880. if (level.details) {
  25881. level.details.fragments.forEach(function (f) {
  25882. return f.level = -1;
  25883. });
  25884. }
  25885. }
  25886. return false;
  25887. });
  25888. reassignFragmentLevelIndexes(levels);
  25889. this._levels = levels;
  25890. if (this.currentLevelIndex > -1 && (_this$currentLevel = this.currentLevel) != null && _this$currentLevel.details) {
  25891. this.currentLevelIndex = this.currentLevel.details.fragments[0].level;
  25892. }
  25893. this.hls.trigger(Events.LEVELS_UPDATED, {
  25894. levels: levels
  25895. });
  25896. };
  25897. _proto.onLevelsUpdated = function onLevelsUpdated(event, _ref3) {
  25898. var levels = _ref3.levels;
  25899. this._levels = levels;
  25900. };
  25901. _proto.checkMaxAutoUpdated = function checkMaxAutoUpdated() {
  25902. var _this$hls = this.hls,
  25903. autoLevelCapping = _this$hls.autoLevelCapping,
  25904. maxAutoLevel = _this$hls.maxAutoLevel,
  25905. maxHdcpLevel = _this$hls.maxHdcpLevel;
  25906. if (this._maxAutoLevel !== maxAutoLevel) {
  25907. this._maxAutoLevel = maxAutoLevel;
  25908. this.hls.trigger(Events.MAX_AUTO_LEVEL_UPDATED, {
  25909. autoLevelCapping: autoLevelCapping,
  25910. levels: this.levels,
  25911. maxAutoLevel: maxAutoLevel,
  25912. minAutoLevel: this.hls.minAutoLevel,
  25913. maxHdcpLevel: maxHdcpLevel
  25914. });
  25915. }
  25916. };
  25917. _createClass(LevelController, [{
  25918. key: "levels",
  25919. get: function get() {
  25920. if (this._levels.length === 0) {
  25921. return null;
  25922. }
  25923. return this._levels;
  25924. }
  25925. }, {
  25926. key: "level",
  25927. get: function get() {
  25928. return this.currentLevelIndex;
  25929. },
  25930. set: function set(newLevel) {
  25931. var levels = this._levels;
  25932. if (levels.length === 0) {
  25933. return;
  25934. }
  25935. // check if level idx is valid
  25936. if (newLevel < 0 || newLevel >= levels.length) {
  25937. // invalid level id given, trigger error
  25938. var error = new Error('invalid level idx');
  25939. var fatal = newLevel < 0;
  25940. this.hls.trigger(Events.ERROR, {
  25941. type: ErrorTypes.OTHER_ERROR,
  25942. details: ErrorDetails.LEVEL_SWITCH_ERROR,
  25943. level: newLevel,
  25944. fatal: fatal,
  25945. error: error,
  25946. reason: error.message
  25947. });
  25948. if (fatal) {
  25949. return;
  25950. }
  25951. newLevel = Math.min(newLevel, levels.length - 1);
  25952. }
  25953. var lastLevelIndex = this.currentLevelIndex;
  25954. var lastLevel = this.currentLevel;
  25955. var lastPathwayId = lastLevel ? lastLevel.attrs['PATHWAY-ID'] : undefined;
  25956. var level = levels[newLevel];
  25957. var pathwayId = level.attrs['PATHWAY-ID'];
  25958. this.currentLevelIndex = newLevel;
  25959. this.currentLevel = level;
  25960. if (lastLevelIndex === newLevel && level.details && lastLevel && lastPathwayId === pathwayId) {
  25961. return;
  25962. }
  25963. this.log("Switching to level " + newLevel + " (" + (level.height ? level.height + 'p ' : '') + (level.videoRange ? level.videoRange + ' ' : '') + (level.codecSet ? level.codecSet + ' ' : '') + "@" + level.bitrate + ")" + (pathwayId ? ' with Pathway ' + pathwayId : '') + " from level " + lastLevelIndex + (lastPathwayId ? ' with Pathway ' + lastPathwayId : ''));
  25964. var levelSwitchingData = {
  25965. level: newLevel,
  25966. attrs: level.attrs,
  25967. details: level.details,
  25968. bitrate: level.bitrate,
  25969. averageBitrate: level.averageBitrate,
  25970. maxBitrate: level.maxBitrate,
  25971. realBitrate: level.realBitrate,
  25972. width: level.width,
  25973. height: level.height,
  25974. codecSet: level.codecSet,
  25975. audioCodec: level.audioCodec,
  25976. videoCodec: level.videoCodec,
  25977. audioGroups: level.audioGroups,
  25978. subtitleGroups: level.subtitleGroups,
  25979. loaded: level.loaded,
  25980. loadError: level.loadError,
  25981. fragmentError: level.fragmentError,
  25982. name: level.name,
  25983. id: level.id,
  25984. uri: level.uri,
  25985. url: level.url,
  25986. urlId: 0,
  25987. audioGroupIds: level.audioGroupIds,
  25988. textGroupIds: level.textGroupIds
  25989. };
  25990. this.hls.trigger(Events.LEVEL_SWITCHING, levelSwitchingData);
  25991. // check if we need to load playlist for this level
  25992. var levelDetails = level.details;
  25993. if (!levelDetails || levelDetails.live) {
  25994. // level not retrieved yet, or live playlist we need to (re)load it
  25995. var hlsUrlParameters = this.switchParams(level.uri, lastLevel == null ? void 0 : lastLevel.details, levelDetails);
  25996. this.loadPlaylist(hlsUrlParameters);
  25997. }
  25998. }
  25999. }, {
  26000. key: "manualLevel",
  26001. get: function get() {
  26002. return this.manualLevelIndex;
  26003. },
  26004. set: function set(newLevel) {
  26005. this.manualLevelIndex = newLevel;
  26006. if (this._startLevel === undefined) {
  26007. this._startLevel = newLevel;
  26008. }
  26009. if (newLevel !== -1) {
  26010. this.level = newLevel;
  26011. }
  26012. }
  26013. }, {
  26014. key: "firstLevel",
  26015. get: function get() {
  26016. return this._firstLevel;
  26017. },
  26018. set: function set(newLevel) {
  26019. this._firstLevel = newLevel;
  26020. }
  26021. }, {
  26022. key: "startLevel",
  26023. get: function get() {
  26024. // Setting hls.startLevel (this._startLevel) overrides config.startLevel
  26025. if (this._startLevel === undefined) {
  26026. var configStartLevel = this.hls.config.startLevel;
  26027. if (configStartLevel !== undefined) {
  26028. return configStartLevel;
  26029. }
  26030. return this.hls.firstAutoLevel;
  26031. }
  26032. return this._startLevel;
  26033. },
  26034. set: function set(newLevel) {
  26035. this._startLevel = newLevel;
  26036. }
  26037. }, {
  26038. key: "nextLoadLevel",
  26039. get: function get() {
  26040. if (this.manualLevelIndex !== -1) {
  26041. return this.manualLevelIndex;
  26042. } else {
  26043. return this.hls.nextAutoLevel;
  26044. }
  26045. },
  26046. set: function set(nextLevel) {
  26047. this.level = nextLevel;
  26048. if (this.manualLevelIndex === -1) {
  26049. this.hls.nextAutoLevel = nextLevel;
  26050. }
  26051. }
  26052. }]);
  26053. return LevelController;
  26054. }(BasePlaylistController);
  26055. function assignTrackIdsByGroup(tracks) {
  26056. var groups = {};
  26057. tracks.forEach(function (track) {
  26058. var groupId = track.groupId || '';
  26059. track.id = groups[groupId] = groups[groupId] || 0;
  26060. groups[groupId]++;
  26061. });
  26062. }
  26063. var KeyLoader = /*#__PURE__*/function () {
  26064. function KeyLoader(config) {
  26065. this.config = void 0;
  26066. this.keyUriToKeyInfo = {};
  26067. this.emeController = null;
  26068. this.config = config;
  26069. }
  26070. var _proto = KeyLoader.prototype;
  26071. _proto.abort = function abort(type) {
  26072. for (var uri in this.keyUriToKeyInfo) {
  26073. var loader = this.keyUriToKeyInfo[uri].loader;
  26074. if (loader) {
  26075. var _loader$context;
  26076. if (type && type !== ((_loader$context = loader.context) == null ? void 0 : _loader$context.frag.type)) {
  26077. return;
  26078. }
  26079. loader.abort();
  26080. }
  26081. }
  26082. };
  26083. _proto.detach = function detach() {
  26084. for (var uri in this.keyUriToKeyInfo) {
  26085. var keyInfo = this.keyUriToKeyInfo[uri];
  26086. // Remove cached EME keys on detach
  26087. if (keyInfo.mediaKeySessionContext || keyInfo.decryptdata.isCommonEncryption) {
  26088. delete this.keyUriToKeyInfo[uri];
  26089. }
  26090. }
  26091. };
  26092. _proto.destroy = function destroy() {
  26093. this.detach();
  26094. for (var uri in this.keyUriToKeyInfo) {
  26095. var loader = this.keyUriToKeyInfo[uri].loader;
  26096. if (loader) {
  26097. loader.destroy();
  26098. }
  26099. }
  26100. this.keyUriToKeyInfo = {};
  26101. };
  26102. _proto.createKeyLoadError = function createKeyLoadError(frag, details, error, networkDetails, response) {
  26103. if (details === void 0) {
  26104. details = ErrorDetails.KEY_LOAD_ERROR;
  26105. }
  26106. return new LoadError({
  26107. type: ErrorTypes.NETWORK_ERROR,
  26108. details: details,
  26109. fatal: false,
  26110. frag: frag,
  26111. response: response,
  26112. error: error,
  26113. networkDetails: networkDetails
  26114. });
  26115. };
  26116. _proto.loadClear = function loadClear(loadingFrag, encryptedFragments) {
  26117. var _this = this;
  26118. if (this.emeController && this.config.emeEnabled) {
  26119. // access key-system with nearest key on start (loaidng frag is unencrypted)
  26120. var sn = loadingFrag.sn,
  26121. cc = loadingFrag.cc;
  26122. var _loop = function _loop() {
  26123. var frag = encryptedFragments[i];
  26124. if (cc <= frag.cc && (sn === 'initSegment' || frag.sn === 'initSegment' || sn < frag.sn)) {
  26125. _this.emeController.selectKeySystemFormat(frag).then(function (keySystemFormat) {
  26126. frag.setKeyFormat(keySystemFormat);
  26127. });
  26128. return 1; // break
  26129. }
  26130. };
  26131. for (var i = 0; i < encryptedFragments.length; i++) {
  26132. if (_loop()) break;
  26133. }
  26134. }
  26135. };
  26136. _proto.load = function load(frag) {
  26137. var _this2 = this;
  26138. if (!frag.decryptdata && frag.encrypted && this.emeController) {
  26139. // Multiple keys, but none selected, resolve in eme-controller
  26140. return this.emeController.selectKeySystemFormat(frag).then(function (keySystemFormat) {
  26141. return _this2.loadInternal(frag, keySystemFormat);
  26142. });
  26143. }
  26144. return this.loadInternal(frag);
  26145. };
  26146. _proto.loadInternal = function loadInternal(frag, keySystemFormat) {
  26147. var _keyInfo, _keyInfo2;
  26148. if (keySystemFormat) {
  26149. frag.setKeyFormat(keySystemFormat);
  26150. }
  26151. var decryptdata = frag.decryptdata;
  26152. if (!decryptdata) {
  26153. var error = new Error(keySystemFormat ? "Expected frag.decryptdata to be defined after setting format " + keySystemFormat : 'Missing decryption data on fragment in onKeyLoading');
  26154. return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, error));
  26155. }
  26156. var uri = decryptdata.uri;
  26157. if (!uri) {
  26158. return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error("Invalid key URI: \"" + uri + "\"")));
  26159. }
  26160. var keyInfo = this.keyUriToKeyInfo[uri];
  26161. if ((_keyInfo = keyInfo) != null && _keyInfo.decryptdata.key) {
  26162. decryptdata.key = keyInfo.decryptdata.key;
  26163. return Promise.resolve({
  26164. frag: frag,
  26165. keyInfo: keyInfo
  26166. });
  26167. }
  26168. // Return key load promise as long as it does not have a mediakey session with an unusable key status
  26169. if ((_keyInfo2 = keyInfo) != null && _keyInfo2.keyLoadPromise) {
  26170. var _keyInfo$mediaKeySess;
  26171. switch ((_keyInfo$mediaKeySess = keyInfo.mediaKeySessionContext) == null ? void 0 : _keyInfo$mediaKeySess.keyStatus) {
  26172. case undefined:
  26173. case 'status-pending':
  26174. case 'usable':
  26175. case 'usable-in-future':
  26176. return keyInfo.keyLoadPromise.then(function (keyLoadedData) {
  26177. // Return the correct fragment with updated decryptdata key and loaded keyInfo
  26178. decryptdata.key = keyLoadedData.keyInfo.decryptdata.key;
  26179. return {
  26180. frag: frag,
  26181. keyInfo: keyInfo
  26182. };
  26183. });
  26184. }
  26185. // If we have a key session and status and it is not pending or usable, continue
  26186. // This will go back to the eme-controller for expired keys to get a new keyLoadPromise
  26187. }
  26188. // Load the key or return the loading promise
  26189. keyInfo = this.keyUriToKeyInfo[uri] = {
  26190. decryptdata: decryptdata,
  26191. keyLoadPromise: null,
  26192. loader: null,
  26193. mediaKeySessionContext: null
  26194. };
  26195. switch (decryptdata.method) {
  26196. case 'ISO-23001-7':
  26197. case 'SAMPLE-AES':
  26198. case 'SAMPLE-AES-CENC':
  26199. case 'SAMPLE-AES-CTR':
  26200. if (decryptdata.keyFormat === 'identity') {
  26201. // loadKeyHTTP handles http(s) and data URLs
  26202. return this.loadKeyHTTP(keyInfo, frag);
  26203. }
  26204. return this.loadKeyEME(keyInfo, frag);
  26205. case 'AES-128':
  26206. return this.loadKeyHTTP(keyInfo, frag);
  26207. default:
  26208. return Promise.reject(this.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error("Key supplied with unsupported METHOD: \"" + decryptdata.method + "\"")));
  26209. }
  26210. };
  26211. _proto.loadKeyEME = function loadKeyEME(keyInfo, frag) {
  26212. var keyLoadedData = {
  26213. frag: frag,
  26214. keyInfo: keyInfo
  26215. };
  26216. if (this.emeController && this.config.emeEnabled) {
  26217. var keySessionContextPromise = this.emeController.loadKey(keyLoadedData);
  26218. if (keySessionContextPromise) {
  26219. return (keyInfo.keyLoadPromise = keySessionContextPromise.then(function (keySessionContext) {
  26220. keyInfo.mediaKeySessionContext = keySessionContext;
  26221. return keyLoadedData;
  26222. })).catch(function (error) {
  26223. // Remove promise for license renewal or retry
  26224. keyInfo.keyLoadPromise = null;
  26225. throw error;
  26226. });
  26227. }
  26228. }
  26229. return Promise.resolve(keyLoadedData);
  26230. };
  26231. _proto.loadKeyHTTP = function loadKeyHTTP(keyInfo, frag) {
  26232. var _this3 = this;
  26233. var config = this.config;
  26234. var Loader = config.loader;
  26235. var keyLoader = new Loader(config);
  26236. frag.keyLoader = keyInfo.loader = keyLoader;
  26237. return keyInfo.keyLoadPromise = new Promise(function (resolve, reject) {
  26238. var loaderContext = {
  26239. keyInfo: keyInfo,
  26240. frag: frag,
  26241. responseType: 'arraybuffer',
  26242. url: keyInfo.decryptdata.uri
  26243. };
  26244. // maxRetry is 0 so that instead of retrying the same key on the same variant multiple times,
  26245. // key-loader will trigger an error and rely on stream-controller to handle retry logic.
  26246. // this will also align retry logic with fragment-loader
  26247. var loadPolicy = config.keyLoadPolicy.default;
  26248. var loaderConfig = {
  26249. loadPolicy: loadPolicy,
  26250. timeout: loadPolicy.maxLoadTimeMs,
  26251. maxRetry: 0,
  26252. retryDelay: 0,
  26253. maxRetryDelay: 0
  26254. };
  26255. var loaderCallbacks = {
  26256. onSuccess: function onSuccess(response, stats, context, networkDetails) {
  26257. var frag = context.frag,
  26258. keyInfo = context.keyInfo,
  26259. uri = context.url;
  26260. if (!frag.decryptdata || keyInfo !== _this3.keyUriToKeyInfo[uri]) {
  26261. return reject(_this3.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error('after key load, decryptdata unset or changed'), networkDetails));
  26262. }
  26263. keyInfo.decryptdata.key = frag.decryptdata.key = new Uint8Array(response.data);
  26264. // detach fragment key loader on load success
  26265. frag.keyLoader = null;
  26266. keyInfo.loader = null;
  26267. resolve({
  26268. frag: frag,
  26269. keyInfo: keyInfo
  26270. });
  26271. },
  26272. onError: function onError(response, context, networkDetails, stats) {
  26273. _this3.resetLoader(context);
  26274. reject(_this3.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_ERROR, new Error("HTTP Error " + response.code + " loading key " + response.text), networkDetails, _objectSpread2({
  26275. url: loaderContext.url,
  26276. data: undefined
  26277. }, response)));
  26278. },
  26279. onTimeout: function onTimeout(stats, context, networkDetails) {
  26280. _this3.resetLoader(context);
  26281. reject(_this3.createKeyLoadError(frag, ErrorDetails.KEY_LOAD_TIMEOUT, new Error('key loading timed out'), networkDetails));
  26282. },
  26283. onAbort: function onAbort(stats, context, networkDetails) {
  26284. _this3.resetLoader(context);
  26285. reject(_this3.createKeyLoadError(frag, ErrorDetails.INTERNAL_ABORTED, new Error('key loading aborted'), networkDetails));
  26286. }
  26287. };
  26288. keyLoader.load(loaderContext, loaderConfig, loaderCallbacks);
  26289. });
  26290. };
  26291. _proto.resetLoader = function resetLoader(context) {
  26292. var frag = context.frag,
  26293. keyInfo = context.keyInfo,
  26294. uri = context.url;
  26295. var loader = keyInfo.loader;
  26296. if (frag.keyLoader === loader) {
  26297. frag.keyLoader = null;
  26298. keyInfo.loader = null;
  26299. }
  26300. delete this.keyUriToKeyInfo[uri];
  26301. if (loader) {
  26302. loader.destroy();
  26303. }
  26304. };
  26305. return KeyLoader;
  26306. }();
  26307. function getSourceBuffer() {
  26308. return self.SourceBuffer || self.WebKitSourceBuffer;
  26309. }
  26310. function isMSESupported() {
  26311. var mediaSource = getMediaSource();
  26312. if (!mediaSource) {
  26313. return false;
  26314. }
  26315. // if SourceBuffer is exposed ensure its API is valid
  26316. // Older browsers do not expose SourceBuffer globally so checking SourceBuffer.prototype is impossible
  26317. var sourceBuffer = getSourceBuffer();
  26318. return !sourceBuffer || sourceBuffer.prototype && typeof sourceBuffer.prototype.appendBuffer === 'function' && typeof sourceBuffer.prototype.remove === 'function';
  26319. }
  26320. function isSupported() {
  26321. if (!isMSESupported()) {
  26322. return false;
  26323. }
  26324. var mediaSource = getMediaSource();
  26325. return typeof (mediaSource == null ? void 0 : mediaSource.isTypeSupported) === 'function' && (['avc1.42E01E,mp4a.40.2', 'av01.0.01M.08', 'vp09.00.50.08'].some(function (codecsForVideoContainer) {
  26326. return mediaSource.isTypeSupported(mimeTypeForCodec(codecsForVideoContainer, 'video'));
  26327. }) || ['mp4a.40.2', 'fLaC'].some(function (codecForAudioContainer) {
  26328. return mediaSource.isTypeSupported(mimeTypeForCodec(codecForAudioContainer, 'audio'));
  26329. }));
  26330. }
  26331. function changeTypeSupported() {
  26332. var _sourceBuffer$prototy;
  26333. var sourceBuffer = getSourceBuffer();
  26334. return typeof (sourceBuffer == null ? void 0 : (_sourceBuffer$prototy = sourceBuffer.prototype) == null ? void 0 : _sourceBuffer$prototy.changeType) === 'function';
  26335. }
  26336. var STALL_MINIMUM_DURATION_MS = 250;
  26337. var MAX_START_GAP_JUMP = 2.0;
  26338. var SKIP_BUFFER_HOLE_STEP_SECONDS = 0.1;
  26339. var SKIP_BUFFER_RANGE_START = 0.05;
  26340. var GapController = /*#__PURE__*/function () {
  26341. function GapController(config, media, fragmentTracker, hls) {
  26342. this.config = void 0;
  26343. this.media = null;
  26344. this.fragmentTracker = void 0;
  26345. this.hls = void 0;
  26346. this.nudgeRetry = 0;
  26347. this.stallReported = false;
  26348. this.stalled = null;
  26349. this.moved = false;
  26350. this.seeking = false;
  26351. this.config = config;
  26352. this.media = media;
  26353. this.fragmentTracker = fragmentTracker;
  26354. this.hls = hls;
  26355. }
  26356. var _proto = GapController.prototype;
  26357. _proto.destroy = function destroy() {
  26358. this.media = null;
  26359. // @ts-ignore
  26360. this.hls = this.fragmentTracker = null;
  26361. }
  26362. /**
  26363. * Checks if the playhead is stuck within a gap, and if so, attempts to free it.
  26364. * A gap is an unbuffered range between two buffered ranges (or the start and the first buffered range).
  26365. *
  26366. * @param lastCurrentTime - Previously read playhead position
  26367. */;
  26368. _proto.poll = function poll(lastCurrentTime, activeFrag) {
  26369. var config = this.config,
  26370. media = this.media,
  26371. stalled = this.stalled;
  26372. if (media === null) {
  26373. return;
  26374. }
  26375. var currentTime = media.currentTime,
  26376. seeking = media.seeking;
  26377. var seeked = this.seeking && !seeking;
  26378. var beginSeek = !this.seeking && seeking;
  26379. this.seeking = seeking;
  26380. // The playhead is moving, no-op
  26381. if (currentTime !== lastCurrentTime) {
  26382. this.moved = true;
  26383. if (!seeking) {
  26384. this.nudgeRetry = 0;
  26385. }
  26386. if (stalled !== null) {
  26387. // The playhead is now moving, but was previously stalled
  26388. if (this.stallReported) {
  26389. var _stalledDuration = self.performance.now() - stalled;
  26390. logger.warn("playback not stuck anymore @" + currentTime + ", after " + Math.round(_stalledDuration) + "ms");
  26391. this.stallReported = false;
  26392. }
  26393. this.stalled = null;
  26394. }
  26395. return;
  26396. }
  26397. // Clear stalled state when beginning or finishing seeking so that we don't report stalls coming out of a seek
  26398. if (beginSeek || seeked) {
  26399. this.stalled = null;
  26400. return;
  26401. }
  26402. // The playhead should not be moving
  26403. if (media.paused && !seeking || media.ended || media.playbackRate === 0 || !BufferHelper.getBuffered(media).length) {
  26404. this.nudgeRetry = 0;
  26405. return;
  26406. }
  26407. var bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
  26408. var nextStart = bufferInfo.nextStart || 0;
  26409. if (seeking) {
  26410. // Waiting for seeking in a buffered range to complete
  26411. var hasEnoughBuffer = bufferInfo.len > MAX_START_GAP_JUMP;
  26412. // Next buffered range is too far ahead to jump to while still seeking
  26413. var noBufferGap = !nextStart || activeFrag && activeFrag.start <= currentTime || nextStart - currentTime > MAX_START_GAP_JUMP && !this.fragmentTracker.getPartialFragment(currentTime);
  26414. if (hasEnoughBuffer || noBufferGap) {
  26415. return;
  26416. }
  26417. // Reset moved state when seeking to a point in or before a gap
  26418. this.moved = false;
  26419. }
  26420. // Skip start gaps if we haven't played, but the last poll detected the start of a stall
  26421. // The addition poll gives the browser a chance to jump the gap for us
  26422. if (!this.moved && this.stalled !== null) {
  26423. var _level$details;
  26424. // There is no playable buffer (seeked, waiting for buffer)
  26425. var isBuffered = bufferInfo.len > 0;
  26426. if (!isBuffered && !nextStart) {
  26427. return;
  26428. }
  26429. // Jump start gaps within jump threshold
  26430. var startJump = Math.max(nextStart, bufferInfo.start || 0) - currentTime;
  26431. // When joining a live stream with audio tracks, account for live playlist window sliding by allowing
  26432. // a larger jump over start gaps caused by the audio-stream-controller buffering a start fragment
  26433. // that begins over 1 target duration after the video start position.
  26434. var level = this.hls.levels ? this.hls.levels[this.hls.currentLevel] : null;
  26435. var isLive = level == null ? void 0 : (_level$details = level.details) == null ? void 0 : _level$details.live;
  26436. var maxStartGapJump = isLive ? level.details.targetduration * 2 : MAX_START_GAP_JUMP;
  26437. var partialOrGap = this.fragmentTracker.getPartialFragment(currentTime);
  26438. if (startJump > 0 && (startJump <= maxStartGapJump || partialOrGap)) {
  26439. if (!media.paused) {
  26440. this._trySkipBufferHole(partialOrGap);
  26441. }
  26442. return;
  26443. }
  26444. }
  26445. // Start tracking stall time
  26446. var tnow = self.performance.now();
  26447. if (stalled === null) {
  26448. this.stalled = tnow;
  26449. return;
  26450. }
  26451. var stalledDuration = tnow - stalled;
  26452. if (!seeking && stalledDuration >= STALL_MINIMUM_DURATION_MS) {
  26453. // Report stalling after trying to fix
  26454. this._reportStall(bufferInfo);
  26455. if (!this.media) {
  26456. return;
  26457. }
  26458. }
  26459. var bufferedWithHoles = BufferHelper.bufferInfo(media, currentTime, config.maxBufferHole);
  26460. this._tryFixBufferStall(bufferedWithHoles, stalledDuration);
  26461. }
  26462. /**
  26463. * Detects and attempts to fix known buffer stalling issues.
  26464. * @param bufferInfo - The properties of the current buffer.
  26465. * @param stalledDurationMs - The amount of time Hls.js has been stalling for.
  26466. * @private
  26467. */;
  26468. _proto._tryFixBufferStall = function _tryFixBufferStall(bufferInfo, stalledDurationMs) {
  26469. var config = this.config,
  26470. fragmentTracker = this.fragmentTracker,
  26471. media = this.media;
  26472. if (media === null) {
  26473. return;
  26474. }
  26475. var currentTime = media.currentTime;
  26476. var partial = fragmentTracker.getPartialFragment(currentTime);
  26477. if (partial) {
  26478. // Try to skip over the buffer hole caused by a partial fragment
  26479. // This method isn't limited by the size of the gap between buffered ranges
  26480. var targetTime = this._trySkipBufferHole(partial);
  26481. // we return here in this case, meaning
  26482. // the branch below only executes when we haven't seeked to a new position
  26483. if (targetTime || !this.media) {
  26484. return;
  26485. }
  26486. }
  26487. // if we haven't had to skip over a buffer hole of a partial fragment
  26488. // we may just have to "nudge" the playlist as the browser decoding/rendering engine
  26489. // needs to cross some sort of threshold covering all source-buffers content
  26490. // to start playing properly.
  26491. if ((bufferInfo.len > config.maxBufferHole || bufferInfo.nextStart && bufferInfo.nextStart - currentTime < config.maxBufferHole) && stalledDurationMs > config.highBufferWatchdogPeriod * 1000) {
  26492. logger.warn('Trying to nudge playhead over buffer-hole');
  26493. // Try to nudge currentTime over a buffer hole if we've been stalling for the configured amount of seconds
  26494. // We only try to jump the hole if it's under the configured size
  26495. // Reset stalled so to rearm watchdog timer
  26496. this.stalled = null;
  26497. this._tryNudgeBuffer();
  26498. }
  26499. }
  26500. /**
  26501. * Triggers a BUFFER_STALLED_ERROR event, but only once per stall period.
  26502. * @param bufferLen - The playhead distance from the end of the current buffer segment.
  26503. * @private
  26504. */;
  26505. _proto._reportStall = function _reportStall(bufferInfo) {
  26506. var hls = this.hls,
  26507. media = this.media,
  26508. stallReported = this.stallReported;
  26509. if (!stallReported && media) {
  26510. // Report stalled error once
  26511. this.stallReported = true;
  26512. var error = new Error("Playback stalling at @" + media.currentTime + " due to low buffer (" + JSON.stringify(bufferInfo) + ")");
  26513. logger.warn(error.message);
  26514. hls.trigger(Events.ERROR, {
  26515. type: ErrorTypes.MEDIA_ERROR,
  26516. details: ErrorDetails.BUFFER_STALLED_ERROR,
  26517. fatal: false,
  26518. error: error,
  26519. buffer: bufferInfo.len
  26520. });
  26521. }
  26522. }
  26523. /**
  26524. * Attempts to fix buffer stalls by jumping over known gaps caused by partial fragments
  26525. * @param partial - The partial fragment found at the current time (where playback is stalling).
  26526. * @private
  26527. */;
  26528. _proto._trySkipBufferHole = function _trySkipBufferHole(partial) {
  26529. var config = this.config,
  26530. hls = this.hls,
  26531. media = this.media;
  26532. if (media === null) {
  26533. return 0;
  26534. }
  26535. // Check if currentTime is between unbuffered regions of partial fragments
  26536. var currentTime = media.currentTime;
  26537. var bufferInfo = BufferHelper.bufferInfo(media, currentTime, 0);
  26538. var startTime = currentTime < bufferInfo.start ? bufferInfo.start : bufferInfo.nextStart;
  26539. if (startTime) {
  26540. var bufferStarved = bufferInfo.len <= config.maxBufferHole;
  26541. var waiting = bufferInfo.len > 0 && bufferInfo.len < 1 && media.readyState < 3;
  26542. var gapLength = startTime - currentTime;
  26543. if (gapLength > 0 && (bufferStarved || waiting)) {
  26544. // Only allow large gaps to be skipped if it is a start gap, or all fragments in skip range are partial
  26545. if (gapLength > config.maxBufferHole) {
  26546. var fragmentTracker = this.fragmentTracker;
  26547. var startGap = false;
  26548. if (currentTime === 0) {
  26549. var startFrag = fragmentTracker.getAppendedFrag(0, PlaylistLevelType.MAIN);
  26550. if (startFrag && startTime < startFrag.end) {
  26551. startGap = true;
  26552. }
  26553. }
  26554. if (!startGap) {
  26555. var startProvisioned = partial || fragmentTracker.getAppendedFrag(currentTime, PlaylistLevelType.MAIN);
  26556. if (startProvisioned) {
  26557. var moreToLoad = false;
  26558. var pos = startProvisioned.end;
  26559. while (pos < startTime) {
  26560. var provisioned = fragmentTracker.getPartialFragment(pos);
  26561. if (provisioned) {
  26562. pos += provisioned.duration;
  26563. } else {
  26564. moreToLoad = true;
  26565. break;
  26566. }
  26567. }
  26568. if (moreToLoad) {
  26569. return 0;
  26570. }
  26571. }
  26572. }
  26573. }
  26574. var targetTime = Math.max(startTime + SKIP_BUFFER_RANGE_START, currentTime + SKIP_BUFFER_HOLE_STEP_SECONDS);
  26575. logger.warn("skipping hole, adjusting currentTime from " + currentTime + " to " + targetTime);
  26576. this.moved = true;
  26577. this.stalled = null;
  26578. media.currentTime = targetTime;
  26579. if (partial && !partial.gap) {
  26580. var error = new Error("fragment loaded with buffer holes, seeking from " + currentTime + " to " + targetTime);
  26581. hls.trigger(Events.ERROR, {
  26582. type: ErrorTypes.MEDIA_ERROR,
  26583. details: ErrorDetails.BUFFER_SEEK_OVER_HOLE,
  26584. fatal: false,
  26585. error: error,
  26586. reason: error.message,
  26587. frag: partial
  26588. });
  26589. }
  26590. return targetTime;
  26591. }
  26592. }
  26593. return 0;
  26594. }
  26595. /**
  26596. * Attempts to fix buffer stalls by advancing the mediaElement's current time by a small amount.
  26597. * @private
  26598. */;
  26599. _proto._tryNudgeBuffer = function _tryNudgeBuffer() {
  26600. var config = this.config,
  26601. hls = this.hls,
  26602. media = this.media,
  26603. nudgeRetry = this.nudgeRetry;
  26604. if (media === null) {
  26605. return;
  26606. }
  26607. var currentTime = media.currentTime;
  26608. this.nudgeRetry++;
  26609. if (nudgeRetry < config.nudgeMaxRetry) {
  26610. var targetTime = currentTime + (nudgeRetry + 1) * config.nudgeOffset;
  26611. // playback stalled in buffered area ... let's nudge currentTime to try to overcome this
  26612. var error = new Error("Nudging 'currentTime' from " + currentTime + " to " + targetTime);
  26613. logger.warn(error.message);
  26614. media.currentTime = targetTime;
  26615. hls.trigger(Events.ERROR, {
  26616. type: ErrorTypes.MEDIA_ERROR,
  26617. details: ErrorDetails.BUFFER_NUDGE_ON_STALL,
  26618. error: error,
  26619. fatal: false
  26620. });
  26621. } else {
  26622. var _error = new Error("Playhead still not moving while enough data buffered @" + currentTime + " after " + config.nudgeMaxRetry + " nudges");
  26623. logger.error(_error.message);
  26624. hls.trigger(Events.ERROR, {
  26625. type: ErrorTypes.MEDIA_ERROR,
  26626. details: ErrorDetails.BUFFER_STALLED_ERROR,
  26627. error: _error,
  26628. fatal: true
  26629. });
  26630. }
  26631. };
  26632. return GapController;
  26633. }();
  26634. var TICK_INTERVAL = 100; // how often to tick in ms
  26635. var StreamController = /*#__PURE__*/function (_BaseStreamController) {
  26636. _inheritsLoose(StreamController, _BaseStreamController);
  26637. function StreamController(hls, fragmentTracker, keyLoader) {
  26638. var _this;
  26639. _this = _BaseStreamController.call(this, hls, fragmentTracker, keyLoader, '[stream-controller]', PlaylistLevelType.MAIN) || this;
  26640. _this.audioCodecSwap = false;
  26641. _this.gapController = null;
  26642. _this.level = -1;
  26643. _this._forceStartLoad = false;
  26644. _this.altAudio = false;
  26645. _this.audioOnly = false;
  26646. _this.fragPlaying = null;
  26647. _this.onvplaying = null;
  26648. _this.onvseeked = null;
  26649. _this.fragLastKbps = 0;
  26650. _this.couldBacktrack = false;
  26651. _this.backtrackFragment = null;
  26652. _this.audioCodecSwitch = false;
  26653. _this.videoBuffer = null;
  26654. _this._registerListeners();
  26655. return _this;
  26656. }
  26657. var _proto = StreamController.prototype;
  26658. _proto._registerListeners = function _registerListeners() {
  26659. var hls = this.hls;
  26660. hls.on(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  26661. hls.on(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  26662. hls.on(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  26663. hls.on(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  26664. hls.on(Events.LEVEL_LOADING, this.onLevelLoading, this);
  26665. hls.on(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  26666. hls.on(Events.FRAG_LOAD_EMERGENCY_ABORTED, this.onFragLoadEmergencyAborted, this);
  26667. hls.on(Events.ERROR, this.onError, this);
  26668. hls.on(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
  26669. hls.on(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
  26670. hls.on(Events.BUFFER_CREATED, this.onBufferCreated, this);
  26671. hls.on(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
  26672. hls.on(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  26673. hls.on(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  26674. };
  26675. _proto._unregisterListeners = function _unregisterListeners() {
  26676. var hls = this.hls;
  26677. hls.off(Events.MEDIA_ATTACHED, this.onMediaAttached, this);
  26678. hls.off(Events.MEDIA_DETACHING, this.onMediaDetaching, this);
  26679. hls.off(Events.MANIFEST_LOADING, this.onManifestLoading, this);
  26680. hls.off(Events.MANIFEST_PARSED, this.onManifestParsed, this);
  26681. hls.off(Events.LEVEL_LOADED, this.onLevelLoaded, this);
  26682. hls.off(Events.FRAG_LOAD_EMERGENCY_ABORTED, this.onFragLoadEmergencyAborted, this);
  26683. hls.off(Events.ERROR, this.onError, this);
  26684. hls.off(Events.AUDIO_TRACK_SWITCHING, this.onAudioTrackSwitching, this);
  26685. hls.off(Events.AUDIO_TRACK_SWITCHED, this.onAudioTrackSwitched, this);
  26686. hls.off(Events.BUFFER_CREATED, this.onBufferCreated, this);
  26687. hls.off(Events.BUFFER_FLUSHED, this.onBufferFlushed, this);
  26688. hls.off(Events.LEVELS_UPDATED, this.onLevelsUpdated, this);
  26689. hls.off(Events.FRAG_BUFFERED, this.onFragBuffered, this);
  26690. };
  26691. _proto.onHandlerDestroying = function onHandlerDestroying() {
  26692. this._unregisterListeners();
  26693. _BaseStreamController.prototype.onHandlerDestroying.call(this);
  26694. };
  26695. _proto.startLoad = function startLoad(startPosition) {
  26696. if (this.levels) {
  26697. var lastCurrentTime = this.lastCurrentTime,
  26698. hls = this.hls;
  26699. this.stopLoad();
  26700. this.setInterval(TICK_INTERVAL);
  26701. this.level = -1;
  26702. if (!this.startFragRequested) {
  26703. // determine load level
  26704. var startLevel = hls.startLevel;
  26705. if (startLevel === -1) {
  26706. if (hls.config.testBandwidth && this.levels.length > 1) {
  26707. // -1 : guess start Level by doing a bitrate test by loading first fragment of lowest quality level
  26708. startLevel = 0;
  26709. this.bitrateTest = true;
  26710. } else {
  26711. startLevel = hls.firstAutoLevel;
  26712. }
  26713. }
  26714. // set new level to playlist loader : this will trigger start level load
  26715. // hls.nextLoadLevel remains until it is set to a new value or until a new frag is successfully loaded
  26716. hls.nextLoadLevel = startLevel;
  26717. this.level = hls.loadLevel;
  26718. this.loadedmetadata = false;
  26719. }
  26720. // if startPosition undefined but lastCurrentTime set, set startPosition to last currentTime
  26721. if (lastCurrentTime > 0 && startPosition === -1) {
  26722. this.log("Override startPosition with lastCurrentTime @" + lastCurrentTime.toFixed(3));
  26723. startPosition = lastCurrentTime;
  26724. }
  26725. this.state = State.IDLE;
  26726. this.nextLoadPosition = this.startPosition = this.lastCurrentTime = startPosition;
  26727. this.tick();
  26728. } else {
  26729. this._forceStartLoad = true;
  26730. this.state = State.STOPPED;
  26731. }
  26732. };
  26733. _proto.stopLoad = function stopLoad() {
  26734. this._forceStartLoad = false;
  26735. _BaseStreamController.prototype.stopLoad.call(this);
  26736. };
  26737. _proto.doTick = function doTick() {
  26738. switch (this.state) {
  26739. case State.WAITING_LEVEL:
  26740. {
  26741. var levels = this.levels,
  26742. level = this.level;
  26743. var currentLevel = levels == null ? void 0 : levels[level];
  26744. var details = currentLevel == null ? void 0 : currentLevel.details;
  26745. if (details && (!details.live || this.levelLastLoaded === currentLevel)) {
  26746. if (this.waitForCdnTuneIn(details)) {
  26747. break;
  26748. }
  26749. this.state = State.IDLE;
  26750. break;
  26751. } else if (this.hls.nextLoadLevel !== this.level) {
  26752. this.state = State.IDLE;
  26753. break;
  26754. }
  26755. break;
  26756. }
  26757. case State.FRAG_LOADING_WAITING_RETRY:
  26758. {
  26759. var _this$media;
  26760. var now = self.performance.now();
  26761. var retryDate = this.retryDate;
  26762. // if current time is gt than retryDate, or if media seeking let's switch to IDLE state to retry loading
  26763. if (!retryDate || now >= retryDate || (_this$media = this.media) != null && _this$media.seeking) {
  26764. var _levels = this.levels,
  26765. _level = this.level;
  26766. var _currentLevel = _levels == null ? void 0 : _levels[_level];
  26767. this.resetStartWhenNotLoaded(_currentLevel || null);
  26768. this.state = State.IDLE;
  26769. }
  26770. }
  26771. break;
  26772. }
  26773. if (this.state === State.IDLE) {
  26774. this.doTickIdle();
  26775. }
  26776. this.onTickEnd();
  26777. };
  26778. _proto.onTickEnd = function onTickEnd() {
  26779. _BaseStreamController.prototype.onTickEnd.call(this);
  26780. this.checkBuffer();
  26781. this.checkFragmentChanged();
  26782. };
  26783. _proto.doTickIdle = function doTickIdle() {
  26784. var hls = this.hls,
  26785. levelLastLoaded = this.levelLastLoaded,
  26786. levels = this.levels,
  26787. media = this.media;
  26788. // if start level not parsed yet OR
  26789. // if video not attached AND start fragment already requested OR start frag prefetch not enabled
  26790. // exit loop, as we either need more info (level not parsed) or we need media to be attached to load new fragment
  26791. if (levelLastLoaded === null || !media && (this.startFragRequested || !hls.config.startFragPrefetch)) {
  26792. return;
  26793. }
  26794. // If the "main" level is audio-only but we are loading an alternate track in the same group, do not load anything
  26795. if (this.altAudio && this.audioOnly) {
  26796. return;
  26797. }
  26798. var level = hls.nextLoadLevel;
  26799. if (!(levels != null && levels[level])) {
  26800. return;
  26801. }
  26802. var levelInfo = levels[level];
  26803. // if buffer length is less than maxBufLen try to load a new fragment
  26804. var bufferInfo = this.getMainFwdBufferInfo();
  26805. if (bufferInfo === null) {
  26806. return;
  26807. }
  26808. var lastDetails = this.getLevelDetails();
  26809. if (lastDetails && this._streamEnded(bufferInfo, lastDetails)) {
  26810. var data = {};
  26811. if (this.altAudio) {
  26812. data.type = 'video';
  26813. }
  26814. this.hls.trigger(Events.BUFFER_EOS, data);
  26815. this.state = State.ENDED;
  26816. return;
  26817. }
  26818. // set next load level : this will trigger a playlist load if needed
  26819. if (hls.loadLevel !== level && hls.manualLevel === -1) {
  26820. this.log("Adapting to level " + level + " from level " + this.level);
  26821. }
  26822. this.level = hls.nextLoadLevel = level;
  26823. var levelDetails = levelInfo.details;
  26824. // if level info not retrieved yet, switch state and wait for level retrieval
  26825. // if live playlist, ensure that new playlist has been refreshed to avoid loading/try to load
  26826. // a useless and outdated fragment (that might even introduce load error if it is already out of the live playlist)
  26827. if (!levelDetails || this.state === State.WAITING_LEVEL || levelDetails.live && this.levelLastLoaded !== levelInfo) {
  26828. this.level = level;
  26829. this.state = State.WAITING_LEVEL;
  26830. return;
  26831. }
  26832. var bufferLen = bufferInfo.len;
  26833. // compute max Buffer Length that we could get from this load level, based on level bitrate. don't buffer more than 60 MB and more than 30s
  26834. var maxBufLen = this.getMaxBufferLength(levelInfo.maxBitrate);
  26835. // Stay idle if we are still with buffer margins
  26836. if (bufferLen >= maxBufLen) {
  26837. return;
  26838. }
  26839. if (this.backtrackFragment && this.backtrackFragment.start > bufferInfo.end) {
  26840. this.backtrackFragment = null;
  26841. }
  26842. var targetBufferTime = this.backtrackFragment ? this.backtrackFragment.start : bufferInfo.end;
  26843. var frag = this.getNextFragment(targetBufferTime, levelDetails);
  26844. // Avoid backtracking by loading an earlier segment in streams with segments that do not start with a key frame (flagged by `couldBacktrack`)
  26845. if (this.couldBacktrack && !this.fragPrevious && frag && frag.sn !== 'initSegment' && this.fragmentTracker.getState(frag) !== FragmentState.OK) {
  26846. var _this$backtrackFragme;
  26847. var backtrackSn = ((_this$backtrackFragme = this.backtrackFragment) != null ? _this$backtrackFragme : frag).sn;
  26848. var fragIdx = backtrackSn - levelDetails.startSN;
  26849. var backtrackFrag = levelDetails.fragments[fragIdx - 1];
  26850. if (backtrackFrag && frag.cc === backtrackFrag.cc) {
  26851. frag = backtrackFrag;
  26852. this.fragmentTracker.removeFragment(backtrackFrag);
  26853. }
  26854. } else if (this.backtrackFragment && bufferInfo.len) {
  26855. this.backtrackFragment = null;
  26856. }
  26857. // Avoid loop loading by using nextLoadPosition set for backtracking and skipping consecutive GAP tags
  26858. if (frag && this.isLoopLoading(frag, targetBufferTime)) {
  26859. var gapStart = frag.gap;
  26860. if (!gapStart) {
  26861. // Cleanup the fragment tracker before trying to find the next unbuffered fragment
  26862. var type = this.audioOnly && !this.altAudio ? ElementaryStreamTypes.AUDIO : ElementaryStreamTypes.VIDEO;
  26863. var mediaBuffer = (type === ElementaryStreamTypes.VIDEO ? this.videoBuffer : this.mediaBuffer) || this.media;
  26864. if (mediaBuffer) {
  26865. this.afterBufferFlushed(mediaBuffer, type, PlaylistLevelType.MAIN);
  26866. }
  26867. }
  26868. frag = this.getNextFragmentLoopLoading(frag, levelDetails, bufferInfo, PlaylistLevelType.MAIN, maxBufLen);
  26869. }
  26870. if (!frag) {
  26871. return;
  26872. }
  26873. if (frag.initSegment && !frag.initSegment.data && !this.bitrateTest) {
  26874. frag = frag.initSegment;
  26875. }
  26876. this.loadFragment(frag, levelInfo, targetBufferTime);
  26877. };
  26878. _proto.loadFragment = function loadFragment(frag, level, targetBufferTime) {
  26879. // Check if fragment is not loaded
  26880. var fragState = this.fragmentTracker.getState(frag);
  26881. this.fragCurrent = frag;
  26882. if (fragState === FragmentState.NOT_LOADED || fragState === FragmentState.PARTIAL) {
  26883. if (frag.sn === 'initSegment') {
  26884. this._loadInitSegment(frag, level);
  26885. } else if (this.bitrateTest) {
  26886. this.log("Fragment " + frag.sn + " of level " + frag.level + " is being downloaded to test bitrate and will not be buffered");
  26887. this._loadBitrateTestFrag(frag, level);
  26888. } else {
  26889. this.startFragRequested = true;
  26890. _BaseStreamController.prototype.loadFragment.call(this, frag, level, targetBufferTime);
  26891. }
  26892. } else {
  26893. this.clearTrackerIfNeeded(frag);
  26894. }
  26895. };
  26896. _proto.getBufferedFrag = function getBufferedFrag(position) {
  26897. return this.fragmentTracker.getBufferedFrag(position, PlaylistLevelType.MAIN);
  26898. };
  26899. _proto.followingBufferedFrag = function followingBufferedFrag(frag) {
  26900. if (frag) {
  26901. // try to get range of next fragment (500ms after this range)
  26902. return this.getBufferedFrag(frag.end + 0.5);
  26903. }
  26904. return null;
  26905. }
  26906. /*
  26907. on immediate level switch :
  26908. - pause playback if playing
  26909. - cancel any pending load request
  26910. - and trigger a buffer flush
  26911. */;
  26912. _proto.immediateLevelSwitch = function immediateLevelSwitch() {
  26913. this.abortCurrentFrag();
  26914. this.flushMainBuffer(0, Number.POSITIVE_INFINITY);
  26915. }
  26916. /**
  26917. * try to switch ASAP without breaking video playback:
  26918. * in order to ensure smooth but quick level switching,
  26919. * we need to find the next flushable buffer range
  26920. * we should take into account new segment fetch time
  26921. */;
  26922. _proto.nextLevelSwitch = function nextLevelSwitch() {
  26923. var levels = this.levels,
  26924. media = this.media;
  26925. // ensure that media is defined and that metadata are available (to retrieve currentTime)
  26926. if (media != null && media.readyState) {
  26927. var fetchdelay;
  26928. var fragPlayingCurrent = this.getAppendedFrag(media.currentTime);
  26929. if (fragPlayingCurrent && fragPlayingCurrent.start > 1) {
  26930. // flush buffer preceding current fragment (flush until current fragment start offset)
  26931. // minus 1s to avoid video freezing, that could happen if we flush keyframe of current video ...
  26932. this.flushMainBuffer(0, fragPlayingCurrent.start - 1);
  26933. }
  26934. var levelDetails = this.getLevelDetails();
  26935. if (levelDetails != null && levelDetails.live) {
  26936. var bufferInfo = this.getMainFwdBufferInfo();
  26937. // Do not flush in live stream with low buffer
  26938. if (!bufferInfo || bufferInfo.len < levelDetails.targetduration * 2) {
  26939. return;
  26940. }
  26941. }
  26942. if (!media.paused && levels) {
  26943. // add a safety delay of 1s
  26944. var nextLevelId = this.hls.nextLoadLevel;
  26945. var nextLevel = levels[nextLevelId];
  26946. var fragLastKbps = this.fragLastKbps;
  26947. if (fragLastKbps && this.fragCurrent) {
  26948. fetchdelay = this.fragCurrent.duration * nextLevel.maxBitrate / (1000 * fragLastKbps) + 1;
  26949. } else {
  26950. fetchdelay = 0;
  26951. }
  26952. } else {
  26953. fetchdelay = 0;
  26954. }
  26955. // this.log('fetchdelay:'+fetchdelay);
  26956. // find buffer range that will be reached once new fragment will be fetched
  26957. var bufferedFrag = this.getBufferedFrag(media.currentTime + fetchdelay);
  26958. if (bufferedFrag) {
  26959. // we can flush buffer range following this one without stalling playback
  26960. var nextBufferedFrag = this.followingBufferedFrag(bufferedFrag);
  26961. if (nextBufferedFrag) {
  26962. // if we are here, we can also cancel any loading/demuxing in progress, as they are useless
  26963. this.abortCurrentFrag();
  26964. // start flush position is in next buffered frag. Leave some padding for non-independent segments and smoother playback.
  26965. var maxStart = nextBufferedFrag.maxStartPTS ? nextBufferedFrag.maxStartPTS : nextBufferedFrag.start;
  26966. var fragDuration = nextBufferedFrag.duration;
  26967. var startPts = Math.max(bufferedFrag.end, maxStart + Math.min(Math.max(fragDuration - this.config.maxFragLookUpTolerance, fragDuration * (this.couldBacktrack ? 0.5 : 0.125)), fragDuration * (this.couldBacktrack ? 0.75 : 0.25)));
  26968. this.flushMainBuffer(startPts, Number.POSITIVE_INFINITY);
  26969. }
  26970. }
  26971. }
  26972. };
  26973. _proto.abortCurrentFrag = function abortCurrentFrag() {
  26974. var fragCurrent = this.fragCurrent;
  26975. this.fragCurrent = null;
  26976. this.backtrackFragment = null;
  26977. if (fragCurrent) {
  26978. fragCurrent.abortRequests();
  26979. this.fragmentTracker.removeFragment(fragCurrent);
  26980. }
  26981. switch (this.state) {
  26982. case State.KEY_LOADING:
  26983. case State.FRAG_LOADING:
  26984. case State.FRAG_LOADING_WAITING_RETRY:
  26985. case State.PARSING:
  26986. case State.PARSED:
  26987. this.state = State.IDLE;
  26988. break;
  26989. }
  26990. this.nextLoadPosition = this.getLoadPosition();
  26991. };
  26992. _proto.flushMainBuffer = function flushMainBuffer(startOffset, endOffset) {
  26993. _BaseStreamController.prototype.flushMainBuffer.call(this, startOffset, endOffset, this.altAudio ? 'video' : null);
  26994. };
  26995. _proto.onMediaAttached = function onMediaAttached(event, data) {
  26996. _BaseStreamController.prototype.onMediaAttached.call(this, event, data);
  26997. var media = data.media;
  26998. this.onvplaying = this.onMediaPlaying.bind(this);
  26999. this.onvseeked = this.onMediaSeeked.bind(this);
  27000. media.addEventListener('playing', this.onvplaying);
  27001. media.addEventListener('seeked', this.onvseeked);
  27002. this.gapController = new GapController(this.config, media, this.fragmentTracker, this.hls);
  27003. };
  27004. _proto.onMediaDetaching = function onMediaDetaching() {
  27005. var media = this.media;
  27006. if (media && this.onvplaying && this.onvseeked) {
  27007. media.removeEventListener('playing', this.onvplaying);
  27008. media.removeEventListener('seeked', this.onvseeked);
  27009. this.onvplaying = this.onvseeked = null;
  27010. this.videoBuffer = null;
  27011. }
  27012. this.fragPlaying = null;
  27013. if (this.gapController) {
  27014. this.gapController.destroy();
  27015. this.gapController = null;
  27016. }
  27017. _BaseStreamController.prototype.onMediaDetaching.call(this);
  27018. };
  27019. _proto.onMediaPlaying = function onMediaPlaying() {
  27020. // tick to speed up FRAG_CHANGED triggering
  27021. this.tick();
  27022. };
  27023. _proto.onMediaSeeked = function onMediaSeeked() {
  27024. var media = this.media;
  27025. var currentTime = media ? media.currentTime : null;
  27026. if (isFiniteNumber(currentTime)) {
  27027. this.log("Media seeked to " + currentTime.toFixed(3));
  27028. }
  27029. // If seeked was issued before buffer was appended do not tick immediately
  27030. var bufferInfo = this.getMainFwdBufferInfo();
  27031. if (bufferInfo === null || bufferInfo.len === 0) {
  27032. this.warn("Main forward buffer length on \"seeked\" event " + (bufferInfo ? bufferInfo.len : 'empty') + ")");
  27033. return;
  27034. }
  27035. // tick to speed up FRAG_CHANGED triggering
  27036. this.tick();
  27037. };
  27038. _proto.onManifestLoading = function onManifestLoading() {
  27039. // reset buffer on manifest loading
  27040. this.log('Trigger BUFFER_RESET');
  27041. this.hls.trigger(Events.BUFFER_RESET, undefined);
  27042. this.fragmentTracker.removeAllFragments();
  27043. this.couldBacktrack = false;
  27044. this.startPosition = this.lastCurrentTime = this.fragLastKbps = 0;
  27045. this.levels = this.fragPlaying = this.backtrackFragment = this.levelLastLoaded = null;
  27046. this.altAudio = this.audioOnly = this.startFragRequested = false;
  27047. };
  27048. _proto.onManifestParsed = function onManifestParsed(event, data) {
  27049. // detect if we have different kind of audio codecs used amongst playlists
  27050. var aac = false;
  27051. var heaac = false;
  27052. data.levels.forEach(function (level) {
  27053. var codec = level.audioCodec;
  27054. if (codec) {
  27055. aac = aac || codec.indexOf('mp4a.40.2') !== -1;
  27056. heaac = heaac || codec.indexOf('mp4a.40.5') !== -1;
  27057. }
  27058. });
  27059. this.audioCodecSwitch = aac && heaac && !changeTypeSupported();
  27060. if (this.audioCodecSwitch) {
  27061. this.log('Both AAC/HE-AAC audio found in levels; declaring level codec as HE-AAC');
  27062. }
  27063. this.levels = data.levels;
  27064. this.startFragRequested = false;
  27065. };
  27066. _proto.onLevelLoading = function onLevelLoading(event, data) {
  27067. var levels = this.levels;
  27068. if (!levels || this.state !== State.IDLE) {
  27069. return;
  27070. }
  27071. var level = levels[data.level];
  27072. if (!level.details || level.details.live && this.levelLastLoaded !== level || this.waitForCdnTuneIn(level.details)) {
  27073. this.state = State.WAITING_LEVEL;
  27074. }
  27075. };
  27076. _proto.onLevelLoaded = function onLevelLoaded(event, data) {
  27077. var _curLevel$details;
  27078. var levels = this.levels;
  27079. var newLevelId = data.level;
  27080. var newDetails = data.details;
  27081. var duration = newDetails.totalduration;
  27082. if (!levels) {
  27083. this.warn("Levels were reset while loading level " + newLevelId);
  27084. return;
  27085. }
  27086. this.log("Level " + newLevelId + " loaded [" + newDetails.startSN + "," + newDetails.endSN + "]" + (newDetails.lastPartSn ? "[part-" + newDetails.lastPartSn + "-" + newDetails.lastPartIndex + "]" : '') + ", cc [" + newDetails.startCC + ", " + newDetails.endCC + "] duration:" + duration);
  27087. var curLevel = levels[newLevelId];
  27088. var fragCurrent = this.fragCurrent;
  27089. if (fragCurrent && (this.state === State.FRAG_LOADING || this.state === State.FRAG_LOADING_WAITING_RETRY)) {
  27090. if (fragCurrent.level !== data.level && fragCurrent.loader) {
  27091. this.abortCurrentFrag();
  27092. }
  27093. }
  27094. var sliding = 0;
  27095. if (newDetails.live || (_curLevel$details = curLevel.details) != null && _curLevel$details.live) {
  27096. var _this$levelLastLoaded;
  27097. this.checkLiveUpdate(newDetails);
  27098. if (newDetails.deltaUpdateFailed) {
  27099. return;
  27100. }
  27101. sliding = this.alignPlaylists(newDetails, curLevel.details, (_this$levelLastLoaded = this.levelLastLoaded) == null ? void 0 : _this$levelLastLoaded.details);
  27102. }
  27103. // override level info
  27104. curLevel.details = newDetails;
  27105. this.levelLastLoaded = curLevel;
  27106. this.hls.trigger(Events.LEVEL_UPDATED, {
  27107. details: newDetails,
  27108. level: newLevelId
  27109. });
  27110. // only switch back to IDLE state if we were waiting for level to start downloading a new fragment
  27111. if (this.state === State.WAITING_LEVEL) {
  27112. if (this.waitForCdnTuneIn(newDetails)) {
  27113. // Wait for Low-Latency CDN Tune-in
  27114. return;
  27115. }
  27116. this.state = State.IDLE;
  27117. }
  27118. if (!this.startFragRequested) {
  27119. this.setStartPosition(newDetails, sliding);
  27120. } else if (newDetails.live) {
  27121. this.synchronizeToLiveEdge(newDetails);
  27122. }
  27123. // trigger handler right now
  27124. this.tick();
  27125. };
  27126. _proto._handleFragmentLoadProgress = function _handleFragmentLoadProgress(data) {
  27127. var _frag$initSegment;
  27128. var frag = data.frag,
  27129. part = data.part,
  27130. payload = data.payload;
  27131. var levels = this.levels;
  27132. if (!levels) {
  27133. this.warn("Levels were reset while fragment load was in progress. Fragment " + frag.sn + " of level " + frag.level + " will not be buffered");
  27134. return;
  27135. }
  27136. var currentLevel = levels[frag.level];
  27137. var details = currentLevel.details;
  27138. if (!details) {
  27139. this.warn("Dropping fragment " + frag.sn + " of level " + frag.level + " after level details were reset");
  27140. this.fragmentTracker.removeFragment(frag);
  27141. return;
  27142. }
  27143. var videoCodec = currentLevel.videoCodec;
  27144. // time Offset is accurate if level PTS is known, or if playlist is not sliding (not live)
  27145. var accurateTimeOffset = details.PTSKnown || !details.live;
  27146. var initSegmentData = (_frag$initSegment = frag.initSegment) == null ? void 0 : _frag$initSegment.data;
  27147. var audioCodec = this._getAudioCodec(currentLevel);
  27148. // transmux the MPEG-TS data to ISO-BMFF segments
  27149. // this.log(`Transmuxing ${frag.sn} of [${details.startSN} ,${details.endSN}],level ${frag.level}, cc ${frag.cc}`);
  27150. var transmuxer = this.transmuxer = this.transmuxer || new TransmuxerInterface(this.hls, PlaylistLevelType.MAIN, this._handleTransmuxComplete.bind(this), this._handleTransmuxerFlush.bind(this));
  27151. var partIndex = part ? part.index : -1;
  27152. var partial = partIndex !== -1;
  27153. var chunkMeta = new ChunkMetadata(frag.level, frag.sn, frag.stats.chunkCount, payload.byteLength, partIndex, partial);
  27154. var initPTS = this.initPTS[frag.cc];
  27155. transmuxer.push(payload, initSegmentData, audioCodec, videoCodec, frag, part, details.totalduration, accurateTimeOffset, chunkMeta, initPTS);
  27156. };
  27157. _proto.onAudioTrackSwitching = function onAudioTrackSwitching(event, data) {
  27158. // if any URL found on new audio track, it is an alternate audio track
  27159. var fromAltAudio = this.altAudio;
  27160. var altAudio = !!data.url;
  27161. // if we switch on main audio, ensure that main fragment scheduling is synced with media.buffered
  27162. // don't do anything if we switch to alt audio: audio stream controller is handling it.
  27163. // we will just have to change buffer scheduling on audioTrackSwitched
  27164. if (!altAudio) {
  27165. if (this.mediaBuffer !== this.media) {
  27166. this.log('Switching on main audio, use media.buffered to schedule main fragment loading');
  27167. this.mediaBuffer = this.media;
  27168. var fragCurrent = this.fragCurrent;
  27169. // we need to refill audio buffer from main: cancel any frag loading to speed up audio switch
  27170. if (fragCurrent) {
  27171. this.log('Switching to main audio track, cancel main fragment load');
  27172. fragCurrent.abortRequests();
  27173. this.fragmentTracker.removeFragment(fragCurrent);
  27174. }
  27175. // destroy transmuxer to force init segment generation (following audio switch)
  27176. this.resetTransmuxer();
  27177. // switch to IDLE state to load new fragment
  27178. this.resetLoadingState();
  27179. } else if (this.audioOnly) {
  27180. // Reset audio transmuxer so when switching back to main audio we're not still appending where we left off
  27181. this.resetTransmuxer();
  27182. }
  27183. var hls = this.hls;
  27184. // If switching from alt to main audio, flush all audio and trigger track switched
  27185. if (fromAltAudio) {
  27186. hls.trigger(Events.BUFFER_FLUSHING, {
  27187. startOffset: 0,
  27188. endOffset: Number.POSITIVE_INFINITY,
  27189. type: null
  27190. });
  27191. this.fragmentTracker.removeAllFragments();
  27192. }
  27193. hls.trigger(Events.AUDIO_TRACK_SWITCHED, data);
  27194. }
  27195. };
  27196. _proto.onAudioTrackSwitched = function onAudioTrackSwitched(event, data) {
  27197. var trackId = data.id;
  27198. var altAudio = !!this.hls.audioTracks[trackId].url;
  27199. if (altAudio) {
  27200. var videoBuffer = this.videoBuffer;
  27201. // if we switched on alternate audio, ensure that main fragment scheduling is synced with video sourcebuffer buffered
  27202. if (videoBuffer && this.mediaBuffer !== videoBuffer) {
  27203. this.log('Switching on alternate audio, use video.buffered to schedule main fragment loading');
  27204. this.mediaBuffer = videoBuffer;
  27205. }
  27206. }
  27207. this.altAudio = altAudio;
  27208. this.tick();
  27209. };
  27210. _proto.onBufferCreated = function onBufferCreated(event, data) {
  27211. var tracks = data.tracks;
  27212. var mediaTrack;
  27213. var name;
  27214. var alternate = false;
  27215. for (var type in tracks) {
  27216. var track = tracks[type];
  27217. if (track.id === 'main') {
  27218. name = type;
  27219. mediaTrack = track;
  27220. // keep video source buffer reference
  27221. if (type === 'video') {
  27222. var videoTrack = tracks[type];
  27223. if (videoTrack) {
  27224. this.videoBuffer = videoTrack.buffer;
  27225. }
  27226. }
  27227. } else {
  27228. alternate = true;
  27229. }
  27230. }
  27231. if (alternate && mediaTrack) {
  27232. this.log("Alternate track found, use " + name + ".buffered to schedule main fragment loading");
  27233. this.mediaBuffer = mediaTrack.buffer;
  27234. } else {
  27235. this.mediaBuffer = this.media;
  27236. }
  27237. };
  27238. _proto.onFragBuffered = function onFragBuffered(event, data) {
  27239. var frag = data.frag,
  27240. part = data.part;
  27241. if (frag && frag.type !== PlaylistLevelType.MAIN) {
  27242. return;
  27243. }
  27244. if (this.fragContextChanged(frag)) {
  27245. // If a level switch was requested while a fragment was buffering, it will emit the FRAG_BUFFERED event upon completion
  27246. // Avoid setting state back to IDLE, since that will interfere with a level switch
  27247. this.warn("Fragment " + frag.sn + (part ? ' p: ' + part.index : '') + " of level " + frag.level + " finished buffering, but was aborted. state: " + this.state);
  27248. if (this.state === State.PARSED) {
  27249. this.state = State.IDLE;
  27250. }
  27251. return;
  27252. }
  27253. var stats = part ? part.stats : frag.stats;
  27254. this.fragLastKbps = Math.round(8 * stats.total / (stats.buffering.end - stats.loading.first));
  27255. if (frag.sn !== 'initSegment') {
  27256. this.fragPrevious = frag;
  27257. }
  27258. this.fragBufferedComplete(frag, part);
  27259. };
  27260. _proto.onError = function onError(event, data) {
  27261. var _data$context;
  27262. if (data.fatal) {
  27263. this.state = State.ERROR;
  27264. return;
  27265. }
  27266. switch (data.details) {
  27267. case ErrorDetails.FRAG_GAP:
  27268. case ErrorDetails.FRAG_PARSING_ERROR:
  27269. case ErrorDetails.FRAG_DECRYPT_ERROR:
  27270. case ErrorDetails.FRAG_LOAD_ERROR:
  27271. case ErrorDetails.FRAG_LOAD_TIMEOUT:
  27272. case ErrorDetails.KEY_LOAD_ERROR:
  27273. case ErrorDetails.KEY_LOAD_TIMEOUT:
  27274. this.onFragmentOrKeyLoadError(PlaylistLevelType.MAIN, data);
  27275. break;
  27276. case ErrorDetails.LEVEL_LOAD_ERROR:
  27277. case ErrorDetails.LEVEL_LOAD_TIMEOUT:
  27278. case ErrorDetails.LEVEL_PARSING_ERROR:
  27279. // in case of non fatal error while loading level, if level controller is not retrying to load level, switch back to IDLE
  27280. if (!data.levelRetry && this.state === State.WAITING_LEVEL && ((_data$context = data.context) == null ? void 0 : _data$context.type) === PlaylistContextType.LEVEL) {
  27281. this.state = State.IDLE;
  27282. }
  27283. break;
  27284. case ErrorDetails.BUFFER_APPEND_ERROR:
  27285. case ErrorDetails.BUFFER_FULL_ERROR:
  27286. if (!data.parent || data.parent !== 'main') {
  27287. return;
  27288. }
  27289. if (data.details === ErrorDetails.BUFFER_APPEND_ERROR) {
  27290. this.resetLoadingState();
  27291. return;
  27292. }
  27293. if (this.reduceLengthAndFlushBuffer(data)) {
  27294. this.flushMainBuffer(0, Number.POSITIVE_INFINITY);
  27295. }
  27296. break;
  27297. case ErrorDetails.INTERNAL_EXCEPTION:
  27298. this.recoverWorkerError(data);
  27299. break;
  27300. }
  27301. }
  27302. // Checks the health of the buffer and attempts to resolve playback stalls.
  27303. ;
  27304. _proto.checkBuffer = function checkBuffer() {
  27305. var media = this.media,
  27306. gapController = this.gapController;
  27307. if (!media || !gapController || !media.readyState) {
  27308. // Exit early if we don't have media or if the media hasn't buffered anything yet (readyState 0)
  27309. return;
  27310. }
  27311. if (this.loadedmetadata || !BufferHelper.getBuffered(media).length) {
  27312. // Resolve gaps using the main buffer, whose ranges are the intersections of the A/V sourcebuffers
  27313. var activeFrag = this.state !== State.IDLE ? this.fragCurrent : null;
  27314. gapController.poll(this.lastCurrentTime, activeFrag);
  27315. }
  27316. this.lastCurrentTime = media.currentTime;
  27317. };
  27318. _proto.onFragLoadEmergencyAborted = function onFragLoadEmergencyAborted() {
  27319. this.state = State.IDLE;
  27320. // if loadedmetadata is not set, it means that we are emergency switch down on first frag
  27321. // in that case, reset startFragRequested flag
  27322. if (!this.loadedmetadata) {
  27323. this.startFragRequested = false;
  27324. this.nextLoadPosition = this.startPosition;
  27325. }
  27326. this.tickImmediate();
  27327. };
  27328. _proto.onBufferFlushed = function onBufferFlushed(event, _ref) {
  27329. var type = _ref.type;
  27330. if (type !== ElementaryStreamTypes.AUDIO || this.audioOnly && !this.altAudio) {
  27331. var mediaBuffer = (type === ElementaryStreamTypes.VIDEO ? this.videoBuffer : this.mediaBuffer) || this.media;
  27332. this.afterBufferFlushed(mediaBuffer, type, PlaylistLevelType.MAIN);
  27333. this.tick();
  27334. }
  27335. };
  27336. _proto.onLevelsUpdated = function onLevelsUpdated(event, data) {
  27337. if (this.level > -1 && this.fragCurrent) {
  27338. this.level = this.fragCurrent.level;
  27339. }
  27340. this.levels = data.levels;
  27341. };
  27342. _proto.swapAudioCodec = function swapAudioCodec() {
  27343. this.audioCodecSwap = !this.audioCodecSwap;
  27344. }
  27345. /**
  27346. * Seeks to the set startPosition if not equal to the mediaElement's current time.
  27347. */;
  27348. _proto.seekToStartPos = function seekToStartPos() {
  27349. var media = this.media;
  27350. if (!media) {
  27351. return;
  27352. }
  27353. var currentTime = media.currentTime;
  27354. var startPosition = this.startPosition;
  27355. // only adjust currentTime if different from startPosition or if startPosition not buffered
  27356. // at that stage, there should be only one buffered range, as we reach that code after first fragment has been buffered
  27357. if (startPosition >= 0 && currentTime < startPosition) {
  27358. if (media.seeking) {
  27359. this.log("could not seek to " + startPosition + ", already seeking at " + currentTime);
  27360. return;
  27361. }
  27362. var buffered = BufferHelper.getBuffered(media);
  27363. var bufferStart = buffered.length ? buffered.start(0) : 0;
  27364. var delta = bufferStart - startPosition;
  27365. if (delta > 0 && (delta < this.config.maxBufferHole || delta < this.config.maxFragLookUpTolerance)) {
  27366. this.log("adjusting start position by " + delta + " to match buffer start");
  27367. startPosition += delta;
  27368. this.startPosition = startPosition;
  27369. }
  27370. this.log("seek to target start position " + startPosition + " from current time " + currentTime);
  27371. media.currentTime = startPosition;
  27372. }
  27373. };
  27374. _proto._getAudioCodec = function _getAudioCodec(currentLevel) {
  27375. var audioCodec = this.config.defaultAudioCodec || currentLevel.audioCodec;
  27376. if (this.audioCodecSwap && audioCodec) {
  27377. this.log('Swapping audio codec');
  27378. if (audioCodec.indexOf('mp4a.40.5') !== -1) {
  27379. audioCodec = 'mp4a.40.2';
  27380. } else {
  27381. audioCodec = 'mp4a.40.5';
  27382. }
  27383. }
  27384. return audioCodec;
  27385. };
  27386. _proto._loadBitrateTestFrag = function _loadBitrateTestFrag(frag, level) {
  27387. var _this2 = this;
  27388. frag.bitrateTest = true;
  27389. this._doFragLoad(frag, level).then(function (data) {
  27390. var hls = _this2.hls;
  27391. if (!data || _this2.fragContextChanged(frag)) {
  27392. return;
  27393. }
  27394. level.fragmentError = 0;
  27395. _this2.state = State.IDLE;
  27396. _this2.startFragRequested = false;
  27397. _this2.bitrateTest = false;
  27398. var stats = frag.stats;
  27399. // Bitrate tests fragments are neither parsed nor buffered
  27400. stats.parsing.start = stats.parsing.end = stats.buffering.start = stats.buffering.end = self.performance.now();
  27401. hls.trigger(Events.FRAG_LOADED, data);
  27402. frag.bitrateTest = false;
  27403. });
  27404. };
  27405. _proto._handleTransmuxComplete = function _handleTransmuxComplete(transmuxResult) {
  27406. var _id3$samples;
  27407. var id = 'main';
  27408. var hls = this.hls;
  27409. var remuxResult = transmuxResult.remuxResult,
  27410. chunkMeta = transmuxResult.chunkMeta;
  27411. var context = this.getCurrentContext(chunkMeta);
  27412. if (!context) {
  27413. this.resetWhenMissingContext(chunkMeta);
  27414. return;
  27415. }
  27416. var frag = context.frag,
  27417. part = context.part,
  27418. level = context.level;
  27419. var video = remuxResult.video,
  27420. text = remuxResult.text,
  27421. id3 = remuxResult.id3,
  27422. initSegment = remuxResult.initSegment;
  27423. var details = level.details;
  27424. // The audio-stream-controller handles audio buffering if Hls.js is playing an alternate audio track
  27425. var audio = this.altAudio ? undefined : remuxResult.audio;
  27426. // Check if the current fragment has been aborted. We check this by first seeing if we're still playing the current level.
  27427. // If we are, subsequently check if the currently loading fragment (fragCurrent) has changed.
  27428. if (this.fragContextChanged(frag)) {
  27429. this.fragmentTracker.removeFragment(frag);
  27430. return;
  27431. }
  27432. this.state = State.PARSING;
  27433. if (initSegment) {
  27434. if (initSegment != null && initSegment.tracks) {
  27435. var mapFragment = frag.initSegment || frag;
  27436. this._bufferInitSegment(level, initSegment.tracks, mapFragment, chunkMeta);
  27437. hls.trigger(Events.FRAG_PARSING_INIT_SEGMENT, {
  27438. frag: mapFragment,
  27439. id: id,
  27440. tracks: initSegment.tracks
  27441. });
  27442. }
  27443. // This would be nice if Number.isFinite acted as a typeguard, but it doesn't. See: https://github.com/Microsoft/TypeScript/issues/10038
  27444. var initPTS = initSegment.initPTS;
  27445. var timescale = initSegment.timescale;
  27446. if (isFiniteNumber(initPTS)) {
  27447. this.initPTS[frag.cc] = {
  27448. baseTime: initPTS,
  27449. timescale: timescale
  27450. };
  27451. hls.trigger(Events.INIT_PTS_FOUND, {
  27452. frag: frag,
  27453. id: id,
  27454. initPTS: initPTS,
  27455. timescale: timescale
  27456. });
  27457. }
  27458. }
  27459. // Avoid buffering if backtracking this fragment
  27460. if (video && details && frag.sn !== 'initSegment') {
  27461. var prevFrag = details.fragments[frag.sn - 1 - details.startSN];
  27462. var isFirstFragment = frag.sn === details.startSN;
  27463. var isFirstInDiscontinuity = !prevFrag || frag.cc > prevFrag.cc;
  27464. if (remuxResult.independent !== false) {
  27465. var startPTS = video.startPTS,
  27466. endPTS = video.endPTS,
  27467. startDTS = video.startDTS,
  27468. endDTS = video.endDTS;
  27469. if (part) {
  27470. part.elementaryStreams[video.type] = {
  27471. startPTS: startPTS,
  27472. endPTS: endPTS,
  27473. startDTS: startDTS,
  27474. endDTS: endDTS
  27475. };
  27476. } else {
  27477. if (video.firstKeyFrame && video.independent && chunkMeta.id === 1 && !isFirstInDiscontinuity) {
  27478. this.couldBacktrack = true;
  27479. }
  27480. if (video.dropped && video.independent) {
  27481. // Backtrack if dropped frames create a gap after currentTime
  27482. var bufferInfo = this.getMainFwdBufferInfo();
  27483. var targetBufferTime = (bufferInfo ? bufferInfo.end : this.getLoadPosition()) + this.config.maxBufferHole;
  27484. var startTime = video.firstKeyFramePTS ? video.firstKeyFramePTS : startPTS;
  27485. if (!isFirstFragment && targetBufferTime < startTime - this.config.maxBufferHole && !isFirstInDiscontinuity) {
  27486. this.backtrack(frag);
  27487. return;
  27488. } else if (isFirstInDiscontinuity) {
  27489. // Mark segment with a gap to avoid loop loading
  27490. frag.gap = true;
  27491. }
  27492. // Set video stream start to fragment start so that truncated samples do not distort the timeline, and mark it partial
  27493. frag.setElementaryStreamInfo(video.type, frag.start, endPTS, frag.start, endDTS, true);
  27494. } else if (isFirstFragment && startPTS > MAX_START_GAP_JUMP) {
  27495. // Mark segment with a gap to skip large start gap
  27496. frag.gap = true;
  27497. }
  27498. }
  27499. frag.setElementaryStreamInfo(video.type, startPTS, endPTS, startDTS, endDTS);
  27500. if (this.backtrackFragment) {
  27501. this.backtrackFragment = frag;
  27502. }
  27503. this.bufferFragmentData(video, frag, part, chunkMeta, isFirstFragment || isFirstInDiscontinuity);
  27504. } else if (isFirstFragment || isFirstInDiscontinuity) {
  27505. // Mark segment with a gap to avoid loop loading
  27506. frag.gap = true;
  27507. } else {
  27508. this.backtrack(frag);
  27509. return;
  27510. }
  27511. }
  27512. if (audio) {
  27513. var _startPTS = audio.startPTS,
  27514. _endPTS = audio.endPTS,
  27515. _startDTS = audio.startDTS,
  27516. _endDTS = audio.endDTS;
  27517. if (part) {
  27518. part.elementaryStreams[ElementaryStreamTypes.AUDIO] = {
  27519. startPTS: _startPTS,
  27520. endPTS: _endPTS,
  27521. startDTS: _startDTS,
  27522. endDTS: _endDTS
  27523. };
  27524. }
  27525. frag.setElementaryStreamInfo(ElementaryStreamTypes.AUDIO, _startPTS, _endPTS, _startDTS, _endDTS);
  27526. this.bufferFragmentData(audio, frag, part, chunkMeta);
  27527. }
  27528. if (details && id3 != null && (_id3$samples = id3.samples) != null && _id3$samples.length) {
  27529. var emittedID3 = {
  27530. id: id,
  27531. frag: frag,
  27532. details: details,
  27533. samples: id3.samples
  27534. };
  27535. hls.trigger(Events.FRAG_PARSING_METADATA, emittedID3);
  27536. }
  27537. if (details && text) {
  27538. var emittedText = {
  27539. id: id,
  27540. frag: frag,
  27541. details: details,
  27542. samples: text.samples
  27543. };
  27544. hls.trigger(Events.FRAG_PARSING_USERDATA, emittedText);
  27545. }
  27546. };
  27547. _proto._bufferInitSegment = function _bufferInitSegment(currentLevel, tracks, frag, chunkMeta) {
  27548. var _this3 = this;
  27549. if (this.state !== State.PARSING) {
  27550. return;
  27551. }
  27552. this.audioOnly = !!tracks.audio && !tracks.video;
  27553. // if audio track is expected to come from audio stream controller, discard any coming from main
  27554. if (this.altAudio && !this.audioOnly) {
  27555. delete tracks.audio;
  27556. }
  27557. // include levelCodec in audio and video tracks
  27558. var audio = tracks.audio,
  27559. video = tracks.video,
  27560. audiovideo = tracks.audiovideo;
  27561. if (audio) {
  27562. var audioCodec = currentLevel.audioCodec;
  27563. var ua = navigator.userAgent.toLowerCase();
  27564. if (this.audioCodecSwitch) {
  27565. if (audioCodec) {
  27566. if (audioCodec.indexOf('mp4a.40.5') !== -1) {
  27567. audioCodec = 'mp4a.40.2';
  27568. } else {
  27569. audioCodec = 'mp4a.40.5';
  27570. }
  27571. }
  27572. // In the case that AAC and HE-AAC audio codecs are signalled in manifest,
  27573. // force HE-AAC, as it seems that most browsers prefers it.
  27574. // don't force HE-AAC if mono stream, or in Firefox
  27575. var audioMetadata = audio.metadata;
  27576. if (audioMetadata && 'channelCount' in audioMetadata && (audioMetadata.channelCount || 1) !== 1 && ua.indexOf('firefox') === -1) {
  27577. audioCodec = 'mp4a.40.5';
  27578. }
  27579. }
  27580. // HE-AAC is broken on Android, always signal audio codec as AAC even if variant manifest states otherwise
  27581. if (audioCodec && audioCodec.indexOf('mp4a.40.5') !== -1 && ua.indexOf('android') !== -1 && audio.container !== 'audio/mpeg') {
  27582. // Exclude mpeg audio
  27583. audioCodec = 'mp4a.40.2';
  27584. this.log("Android: force audio codec to " + audioCodec);
  27585. }
  27586. if (currentLevel.audioCodec && currentLevel.audioCodec !== audioCodec) {
  27587. this.log("Swapping manifest audio codec \"" + currentLevel.audioCodec + "\" for \"" + audioCodec + "\"");
  27588. }
  27589. audio.levelCodec = audioCodec;
  27590. audio.id = 'main';
  27591. this.log("Init audio buffer, container:" + audio.container + ", codecs[selected/level/parsed]=[" + (audioCodec || '') + "/" + (currentLevel.audioCodec || '') + "/" + audio.codec + "]");
  27592. }
  27593. if (video) {
  27594. video.levelCodec = currentLevel.videoCodec;
  27595. video.id = 'main';
  27596. this.log("Init video buffer, container:" + video.container + ", codecs[level/parsed]=[" + (currentLevel.videoCodec || '') + "/" + video.codec + "]");
  27597. }
  27598. if (audiovideo) {
  27599. this.log("Init audiovideo buffer, container:" + audiovideo.container + ", codecs[level/parsed]=[" + currentLevel.codecs + "/" + audiovideo.codec + "]");
  27600. }
  27601. this.hls.trigger(Events.BUFFER_CODECS, tracks);
  27602. // loop through tracks that are going to be provided to bufferController
  27603. Object.keys(tracks).forEach(function (trackName) {
  27604. var track = tracks[trackName];
  27605. var initSegment = track.initSegment;
  27606. if (initSegment != null && initSegment.byteLength) {
  27607. _this3.hls.trigger(Events.BUFFER_APPENDING, {
  27608. type: trackName,
  27609. data: initSegment,
  27610. frag: frag,
  27611. part: null,
  27612. chunkMeta: chunkMeta,
  27613. parent: frag.type
  27614. });
  27615. }
  27616. });
  27617. // trigger handler right now
  27618. this.tickImmediate();
  27619. };
  27620. _proto.getMainFwdBufferInfo = function getMainFwdBufferInfo() {
  27621. return this.getFwdBufferInfo(this.mediaBuffer ? this.mediaBuffer : this.media, PlaylistLevelType.MAIN);
  27622. };
  27623. _proto.backtrack = function backtrack(frag) {
  27624. this.couldBacktrack = true;
  27625. // Causes findFragments to backtrack through fragments to find the keyframe
  27626. this.backtrackFragment = frag;
  27627. this.resetTransmuxer();
  27628. this.flushBufferGap(frag);
  27629. this.fragmentTracker.removeFragment(frag);
  27630. this.fragPrevious = null;
  27631. this.nextLoadPosition = frag.start;
  27632. this.state = State.IDLE;
  27633. };
  27634. _proto.checkFragmentChanged = function checkFragmentChanged() {
  27635. var video = this.media;
  27636. var fragPlayingCurrent = null;
  27637. if (video && video.readyState > 1 && video.seeking === false) {
  27638. var currentTime = video.currentTime;
  27639. /* if video element is in seeked state, currentTime can only increase.
  27640. (assuming that playback rate is positive ...)
  27641. As sometimes currentTime jumps back to zero after a
  27642. media decode error, check this, to avoid seeking back to
  27643. wrong position after a media decode error
  27644. */
  27645. if (BufferHelper.isBuffered(video, currentTime)) {
  27646. fragPlayingCurrent = this.getAppendedFrag(currentTime);
  27647. } else if (BufferHelper.isBuffered(video, currentTime + 0.1)) {
  27648. /* ensure that FRAG_CHANGED event is triggered at startup,
  27649. when first video frame is displayed and playback is paused.
  27650. add a tolerance of 100ms, in case current position is not buffered,
  27651. check if current pos+100ms is buffered and use that buffer range
  27652. for FRAG_CHANGED event reporting */
  27653. fragPlayingCurrent = this.getAppendedFrag(currentTime + 0.1);
  27654. }
  27655. if (fragPlayingCurrent) {
  27656. this.backtrackFragment = null;
  27657. var fragPlaying = this.fragPlaying;
  27658. var fragCurrentLevel = fragPlayingCurrent.level;
  27659. if (!fragPlaying || fragPlayingCurrent.sn !== fragPlaying.sn || fragPlaying.level !== fragCurrentLevel) {
  27660. this.fragPlaying = fragPlayingCurrent;
  27661. this.hls.trigger(Events.FRAG_CHANGED, {
  27662. frag: fragPlayingCurrent
  27663. });
  27664. if (!fragPlaying || fragPlaying.level !== fragCurrentLevel) {
  27665. this.hls.trigger(Events.LEVEL_SWITCHED, {
  27666. level: fragCurrentLevel
  27667. });
  27668. }
  27669. }
  27670. }
  27671. }
  27672. };
  27673. _createClass(StreamController, [{
  27674. key: "nextLevel",
  27675. get: function get() {
  27676. var frag = this.nextBufferedFrag;
  27677. if (frag) {
  27678. return frag.level;
  27679. }
  27680. return -1;
  27681. }
  27682. }, {
  27683. key: "currentFrag",
  27684. get: function get() {
  27685. var media = this.media;
  27686. if (media) {
  27687. return this.fragPlaying || this.getAppendedFrag(media.currentTime);
  27688. }
  27689. return null;
  27690. }
  27691. }, {
  27692. key: "currentProgramDateTime",
  27693. get: function get() {
  27694. var media = this.media;
  27695. if (media) {
  27696. var currentTime = media.currentTime;
  27697. var frag = this.currentFrag;
  27698. if (frag && isFiniteNumber(currentTime) && isFiniteNumber(frag.programDateTime)) {
  27699. var epocMs = frag.programDateTime + (currentTime - frag.start) * 1000;
  27700. return new Date(epocMs);
  27701. }
  27702. }
  27703. return null;
  27704. }
  27705. }, {
  27706. key: "currentLevel",
  27707. get: function get() {
  27708. var frag = this.currentFrag;
  27709. if (frag) {
  27710. return frag.level;
  27711. }
  27712. return -1;
  27713. }
  27714. }, {
  27715. key: "nextBufferedFrag",
  27716. get: function get() {
  27717. var frag = this.currentFrag;
  27718. if (frag) {
  27719. return this.followingBufferedFrag(frag);
  27720. }
  27721. return null;
  27722. }
  27723. }, {
  27724. key: "forceStartLoad",
  27725. get: function get() {
  27726. return this._forceStartLoad;
  27727. }
  27728. }]);
  27729. return StreamController;
  27730. }(BaseStreamController);
  27731. /**
  27732. * The `Hls` class is the core of the HLS.js library used to instantiate player instances.
  27733. * @public
  27734. */
  27735. var Hls = /*#__PURE__*/function () {
  27736. /**
  27737. * Check if the required MediaSource Extensions are available.
  27738. */
  27739. Hls.isMSESupported = function isMSESupported$1() {
  27740. return isMSESupported();
  27741. }
  27742. /**
  27743. * Check if MediaSource Extensions are available and isTypeSupported checks pass for any baseline codecs.
  27744. */;
  27745. Hls.isSupported = function isSupported$1() {
  27746. return isSupported();
  27747. }
  27748. /**
  27749. * Get the MediaSource global used for MSE playback (ManagedMediaSource, MediaSource, or WebKitMediaSource).
  27750. */;
  27751. Hls.getMediaSource = function getMediaSource$1() {
  27752. return getMediaSource();
  27753. };
  27754. /**
  27755. * Creates an instance of an HLS client that can attach to exactly one `HTMLMediaElement`.
  27756. * @param userConfig - Configuration options applied over `Hls.DefaultConfig`
  27757. */
  27758. function Hls(userConfig) {
  27759. if (userConfig === void 0) {
  27760. userConfig = {};
  27761. }
  27762. /**
  27763. * The runtime configuration used by the player. At instantiation this is combination of `hls.userConfig` merged over `Hls.DefaultConfig`.
  27764. */
  27765. this.config = void 0;
  27766. /**
  27767. * The configuration object provided on player instantiation.
  27768. */
  27769. this.userConfig = void 0;
  27770. this.coreComponents = void 0;
  27771. this.networkControllers = void 0;
  27772. this.started = false;
  27773. this._emitter = new EventEmitter();
  27774. this._autoLevelCapping = -1;
  27775. this._maxHdcpLevel = null;
  27776. this.abrController = void 0;
  27777. this.bufferController = void 0;
  27778. this.capLevelController = void 0;
  27779. this.latencyController = void 0;
  27780. this.levelController = void 0;
  27781. this.streamController = void 0;
  27782. this.audioTrackController = void 0;
  27783. this.subtitleTrackController = void 0;
  27784. this.emeController = void 0;
  27785. this.cmcdController = void 0;
  27786. this._media = null;
  27787. this.url = null;
  27788. this.triggeringException = void 0;
  27789. enableLogs(userConfig.debug || false, 'Hls instance');
  27790. var config = this.config = mergeConfig(Hls.DefaultConfig, userConfig);
  27791. this.userConfig = userConfig;
  27792. if (config.progressive) {
  27793. enableStreamingMode(config);
  27794. }
  27795. // core controllers and network loaders
  27796. var ConfigAbrController = config.abrController,
  27797. ConfigBufferController = config.bufferController,
  27798. ConfigCapLevelController = config.capLevelController,
  27799. ConfigErrorController = config.errorController,
  27800. ConfigFpsController = config.fpsController;
  27801. var errorController = new ConfigErrorController(this);
  27802. var abrController = this.abrController = new ConfigAbrController(this);
  27803. var bufferController = this.bufferController = new ConfigBufferController(this);
  27804. var capLevelController = this.capLevelController = new ConfigCapLevelController(this);
  27805. var fpsController = new ConfigFpsController(this);
  27806. var playListLoader = new PlaylistLoader(this);
  27807. var id3TrackController = new ID3TrackController(this);
  27808. var ConfigContentSteeringController = config.contentSteeringController;
  27809. // ConentSteeringController is defined before LevelController to receive Multivariant Playlist events first
  27810. var contentSteering = ConfigContentSteeringController ? new ConfigContentSteeringController(this) : null;
  27811. var levelController = this.levelController = new LevelController(this, contentSteering);
  27812. // FragmentTracker must be defined before StreamController because the order of event handling is important
  27813. var fragmentTracker = new FragmentTracker(this);
  27814. var keyLoader = new KeyLoader(this.config);
  27815. var streamController = this.streamController = new StreamController(this, fragmentTracker, keyLoader);
  27816. // Cap level controller uses streamController to flush the buffer
  27817. capLevelController.setStreamController(streamController);
  27818. // fpsController uses streamController to switch when frames are being dropped
  27819. fpsController.setStreamController(streamController);
  27820. var networkControllers = [playListLoader, levelController, streamController];
  27821. if (contentSteering) {
  27822. networkControllers.splice(1, 0, contentSteering);
  27823. }
  27824. this.networkControllers = networkControllers;
  27825. var coreComponents = [abrController, bufferController, capLevelController, fpsController, id3TrackController, fragmentTracker];
  27826. this.audioTrackController = this.createController(config.audioTrackController, networkControllers);
  27827. var AudioStreamControllerClass = config.audioStreamController;
  27828. if (AudioStreamControllerClass) {
  27829. networkControllers.push(new AudioStreamControllerClass(this, fragmentTracker, keyLoader));
  27830. }
  27831. // subtitleTrackController must be defined before subtitleStreamController because the order of event handling is important
  27832. this.subtitleTrackController = this.createController(config.subtitleTrackController, networkControllers);
  27833. var SubtitleStreamControllerClass = config.subtitleStreamController;
  27834. if (SubtitleStreamControllerClass) {
  27835. networkControllers.push(new SubtitleStreamControllerClass(this, fragmentTracker, keyLoader));
  27836. }
  27837. this.createController(config.timelineController, coreComponents);
  27838. keyLoader.emeController = this.emeController = this.createController(config.emeController, coreComponents);
  27839. this.cmcdController = this.createController(config.cmcdController, coreComponents);
  27840. this.latencyController = this.createController(LatencyController, coreComponents);
  27841. this.coreComponents = coreComponents;
  27842. // Error controller handles errors before and after all other controllers
  27843. // This listener will be invoked after all other controllers error listeners
  27844. networkControllers.push(errorController);
  27845. var onErrorOut = errorController.onErrorOut;
  27846. if (typeof onErrorOut === 'function') {
  27847. this.on(Events.ERROR, onErrorOut, errorController);
  27848. }
  27849. }
  27850. var _proto = Hls.prototype;
  27851. _proto.createController = function createController(ControllerClass, components) {
  27852. if (ControllerClass) {
  27853. var controllerInstance = new ControllerClass(this);
  27854. if (components) {
  27855. components.push(controllerInstance);
  27856. }
  27857. return controllerInstance;
  27858. }
  27859. return null;
  27860. }
  27861. // Delegate the EventEmitter through the public API of Hls.js
  27862. ;
  27863. _proto.on = function on(event, listener, context) {
  27864. if (context === void 0) {
  27865. context = this;
  27866. }
  27867. this._emitter.on(event, listener, context);
  27868. };
  27869. _proto.once = function once(event, listener, context) {
  27870. if (context === void 0) {
  27871. context = this;
  27872. }
  27873. this._emitter.once(event, listener, context);
  27874. };
  27875. _proto.removeAllListeners = function removeAllListeners(event) {
  27876. this._emitter.removeAllListeners(event);
  27877. };
  27878. _proto.off = function off(event, listener, context, once) {
  27879. if (context === void 0) {
  27880. context = this;
  27881. }
  27882. this._emitter.off(event, listener, context, once);
  27883. };
  27884. _proto.listeners = function listeners(event) {
  27885. return this._emitter.listeners(event);
  27886. };
  27887. _proto.emit = function emit(event, name, eventObject) {
  27888. return this._emitter.emit(event, name, eventObject);
  27889. };
  27890. _proto.trigger = function trigger(event, eventObject) {
  27891. if (this.config.debug) {
  27892. return this.emit(event, event, eventObject);
  27893. } else {
  27894. try {
  27895. return this.emit(event, event, eventObject);
  27896. } catch (error) {
  27897. logger.error('An internal error happened while handling event ' + event + '. Error message: "' + error.message + '". Here is a stacktrace:', error);
  27898. // Prevent recursion in error event handlers that throw #5497
  27899. if (!this.triggeringException) {
  27900. this.triggeringException = true;
  27901. var fatal = event === Events.ERROR;
  27902. this.trigger(Events.ERROR, {
  27903. type: ErrorTypes.OTHER_ERROR,
  27904. details: ErrorDetails.INTERNAL_EXCEPTION,
  27905. fatal: fatal,
  27906. event: event,
  27907. error: error
  27908. });
  27909. this.triggeringException = false;
  27910. }
  27911. }
  27912. }
  27913. return false;
  27914. };
  27915. _proto.listenerCount = function listenerCount(event) {
  27916. return this._emitter.listenerCount(event);
  27917. }
  27918. /**
  27919. * Dispose of the instance
  27920. */;
  27921. _proto.destroy = function destroy() {
  27922. logger.log('destroy');
  27923. this.trigger(Events.DESTROYING, undefined);
  27924. this.detachMedia();
  27925. this.removeAllListeners();
  27926. this._autoLevelCapping = -1;
  27927. this.url = null;
  27928. this.networkControllers.forEach(function (component) {
  27929. return component.destroy();
  27930. });
  27931. this.networkControllers.length = 0;
  27932. this.coreComponents.forEach(function (component) {
  27933. return component.destroy();
  27934. });
  27935. this.coreComponents.length = 0;
  27936. // Remove any references that could be held in config options or callbacks
  27937. var config = this.config;
  27938. config.xhrSetup = config.fetchSetup = undefined;
  27939. // @ts-ignore
  27940. this.userConfig = null;
  27941. }
  27942. /**
  27943. * Attaches Hls.js to a media element
  27944. */;
  27945. _proto.attachMedia = function attachMedia(media) {
  27946. logger.log('attachMedia');
  27947. this._media = media;
  27948. this.trigger(Events.MEDIA_ATTACHING, {
  27949. media: media
  27950. });
  27951. }
  27952. /**
  27953. * Detach Hls.js from the media
  27954. */;
  27955. _proto.detachMedia = function detachMedia() {
  27956. logger.log('detachMedia');
  27957. this.trigger(Events.MEDIA_DETACHING, undefined);
  27958. this._media = null;
  27959. }
  27960. /**
  27961. * Set the source URL. Can be relative or absolute.
  27962. */;
  27963. _proto.loadSource = function loadSource(url) {
  27964. this.stopLoad();
  27965. var media = this.media;
  27966. var loadedSource = this.url;
  27967. var loadingSource = this.url = urlToolkitExports.buildAbsoluteURL(self.location.href, url, {
  27968. alwaysNormalize: true
  27969. });
  27970. this._autoLevelCapping = -1;
  27971. this._maxHdcpLevel = null;
  27972. logger.log("loadSource:" + loadingSource);
  27973. if (media && loadedSource && (loadedSource !== loadingSource || this.bufferController.hasSourceTypes())) {
  27974. this.detachMedia();
  27975. this.attachMedia(media);
  27976. }
  27977. // when attaching to a source URL, trigger a playlist load
  27978. this.trigger(Events.MANIFEST_LOADING, {
  27979. url: url
  27980. });
  27981. }
  27982. /**
  27983. * Start loading data from the stream source.
  27984. * Depending on default config, client starts loading automatically when a source is set.
  27985. *
  27986. * @param startPosition - Set the start position to stream from.
  27987. * Defaults to -1 (None: starts from earliest point)
  27988. */;
  27989. _proto.startLoad = function startLoad(startPosition) {
  27990. if (startPosition === void 0) {
  27991. startPosition = -1;
  27992. }
  27993. logger.log("startLoad(" + startPosition + ")");
  27994. this.started = true;
  27995. this.networkControllers.forEach(function (controller) {
  27996. controller.startLoad(startPosition);
  27997. });
  27998. }
  27999. /**
  28000. * Stop loading of any stream data.
  28001. */;
  28002. _proto.stopLoad = function stopLoad() {
  28003. logger.log('stopLoad');
  28004. this.started = false;
  28005. this.networkControllers.forEach(function (controller) {
  28006. controller.stopLoad();
  28007. });
  28008. }
  28009. /**
  28010. * Resumes stream controller segment loading if previously started.
  28011. */;
  28012. _proto.resumeBuffering = function resumeBuffering() {
  28013. if (this.started) {
  28014. this.networkControllers.forEach(function (controller) {
  28015. if ('fragmentLoader' in controller) {
  28016. controller.startLoad(-1);
  28017. }
  28018. });
  28019. }
  28020. }
  28021. /**
  28022. * Stops stream controller segment loading without changing 'started' state like stopLoad().
  28023. * This allows for media buffering to be paused without interupting playlist loading.
  28024. */;
  28025. _proto.pauseBuffering = function pauseBuffering() {
  28026. this.networkControllers.forEach(function (controller) {
  28027. if ('fragmentLoader' in controller) {
  28028. controller.stopLoad();
  28029. }
  28030. });
  28031. }
  28032. /**
  28033. * Swap through possible audio codecs in the stream (for example to switch from stereo to 5.1)
  28034. */;
  28035. _proto.swapAudioCodec = function swapAudioCodec() {
  28036. logger.log('swapAudioCodec');
  28037. this.streamController.swapAudioCodec();
  28038. }
  28039. /**
  28040. * When the media-element fails, this allows to detach and then re-attach it
  28041. * as one call (convenience method).
  28042. *
  28043. * Automatic recovery of media-errors by this process is configurable.
  28044. */;
  28045. _proto.recoverMediaError = function recoverMediaError() {
  28046. logger.log('recoverMediaError');
  28047. var media = this._media;
  28048. this.detachMedia();
  28049. if (media) {
  28050. this.attachMedia(media);
  28051. }
  28052. };
  28053. _proto.removeLevel = function removeLevel(levelIndex) {
  28054. this.levelController.removeLevel(levelIndex);
  28055. }
  28056. /**
  28057. * @returns an array of levels (variants) sorted by HDCP-LEVEL, RESOLUTION (height), FRAME-RATE, CODECS, VIDEO-RANGE, and BANDWIDTH
  28058. */;
  28059. /**
  28060. * Find and select the best matching audio track, making a level switch when a Group change is necessary.
  28061. * Updates `hls.config.audioPreference`. Returns the selected track, or null when no matching track is found.
  28062. */
  28063. _proto.setAudioOption = function setAudioOption(audioOption) {
  28064. var _this$audioTrackContr;
  28065. return (_this$audioTrackContr = this.audioTrackController) == null ? void 0 : _this$audioTrackContr.setAudioOption(audioOption);
  28066. }
  28067. /**
  28068. * Find and select the best matching subtitle track, making a level switch when a Group change is necessary.
  28069. * Updates `hls.config.subtitlePreference`. Returns the selected track, or null when no matching track is found.
  28070. */;
  28071. _proto.setSubtitleOption = function setSubtitleOption(subtitleOption) {
  28072. var _this$subtitleTrackCo;
  28073. (_this$subtitleTrackCo = this.subtitleTrackController) == null ? void 0 : _this$subtitleTrackCo.setSubtitleOption(subtitleOption);
  28074. return null;
  28075. }
  28076. /**
  28077. * Get the complete list of audio tracks across all media groups
  28078. */;
  28079. _createClass(Hls, [{
  28080. key: "levels",
  28081. get: function get() {
  28082. var levels = this.levelController.levels;
  28083. return levels ? levels : [];
  28084. }
  28085. /**
  28086. * Index of quality level (variant) currently played
  28087. */
  28088. }, {
  28089. key: "currentLevel",
  28090. get: function get() {
  28091. return this.streamController.currentLevel;
  28092. }
  28093. /**
  28094. * Set quality level index immediately. This will flush the current buffer to replace the quality asap. That means playback will interrupt at least shortly to re-buffer and re-sync eventually. Set to -1 for automatic level selection.
  28095. */,
  28096. set: function set(newLevel) {
  28097. logger.log("set currentLevel:" + newLevel);
  28098. this.levelController.manualLevel = newLevel;
  28099. this.streamController.immediateLevelSwitch();
  28100. }
  28101. /**
  28102. * Index of next quality level loaded as scheduled by stream controller.
  28103. */
  28104. }, {
  28105. key: "nextLevel",
  28106. get: function get() {
  28107. return this.streamController.nextLevel;
  28108. }
  28109. /**
  28110. * Set quality level index for next loaded data.
  28111. * This will switch the video quality asap, without interrupting playback.
  28112. * May abort current loading of data, and flush parts of buffer (outside currently played fragment region).
  28113. * @param newLevel - Pass -1 for automatic level selection
  28114. */,
  28115. set: function set(newLevel) {
  28116. logger.log("set nextLevel:" + newLevel);
  28117. this.levelController.manualLevel = newLevel;
  28118. this.streamController.nextLevelSwitch();
  28119. }
  28120. /**
  28121. * Return the quality level of the currently or last (of none is loaded currently) segment
  28122. */
  28123. }, {
  28124. key: "loadLevel",
  28125. get: function get() {
  28126. return this.levelController.level;
  28127. }
  28128. /**
  28129. * Set quality level index for next loaded data in a conservative way.
  28130. * This will switch the quality without flushing, but interrupt current loading.
  28131. * Thus the moment when the quality switch will appear in effect will only be after the already existing buffer.
  28132. * @param newLevel - Pass -1 for automatic level selection
  28133. */,
  28134. set: function set(newLevel) {
  28135. logger.log("set loadLevel:" + newLevel);
  28136. this.levelController.manualLevel = newLevel;
  28137. }
  28138. /**
  28139. * get next quality level loaded
  28140. */
  28141. }, {
  28142. key: "nextLoadLevel",
  28143. get: function get() {
  28144. return this.levelController.nextLoadLevel;
  28145. }
  28146. /**
  28147. * Set quality level of next loaded segment in a fully "non-destructive" way.
  28148. * Same as `loadLevel` but will wait for next switch (until current loading is done).
  28149. */,
  28150. set: function set(level) {
  28151. this.levelController.nextLoadLevel = level;
  28152. }
  28153. /**
  28154. * Return "first level": like a default level, if not set,
  28155. * falls back to index of first level referenced in manifest
  28156. */
  28157. }, {
  28158. key: "firstLevel",
  28159. get: function get() {
  28160. return Math.max(this.levelController.firstLevel, this.minAutoLevel);
  28161. }
  28162. /**
  28163. * Sets "first-level", see getter.
  28164. */,
  28165. set: function set(newLevel) {
  28166. logger.log("set firstLevel:" + newLevel);
  28167. this.levelController.firstLevel = newLevel;
  28168. }
  28169. /**
  28170. * Return the desired start level for the first fragment that will be loaded.
  28171. * The default value of -1 indicates automatic start level selection.
  28172. * Setting hls.nextAutoLevel without setting a startLevel will result in
  28173. * the nextAutoLevel value being used for one fragment load.
  28174. */
  28175. }, {
  28176. key: "startLevel",
  28177. get: function get() {
  28178. var startLevel = this.levelController.startLevel;
  28179. if (startLevel === -1 && this.abrController.forcedAutoLevel > -1) {
  28180. return this.abrController.forcedAutoLevel;
  28181. }
  28182. return startLevel;
  28183. }
  28184. /**
  28185. * set start level (level of first fragment that will be played back)
  28186. * if not overrided by user, first level appearing in manifest will be used as start level
  28187. * if -1 : automatic start level selection, playback will start from level matching download bandwidth
  28188. * (determined from download of first segment)
  28189. */,
  28190. set: function set(newLevel) {
  28191. logger.log("set startLevel:" + newLevel);
  28192. // if not in automatic start level detection, ensure startLevel is greater than minAutoLevel
  28193. if (newLevel !== -1) {
  28194. newLevel = Math.max(newLevel, this.minAutoLevel);
  28195. }
  28196. this.levelController.startLevel = newLevel;
  28197. }
  28198. /**
  28199. * Whether level capping is enabled.
  28200. * Default value is set via `config.capLevelToPlayerSize`.
  28201. */
  28202. }, {
  28203. key: "capLevelToPlayerSize",
  28204. get: function get() {
  28205. return this.config.capLevelToPlayerSize;
  28206. }
  28207. /**
  28208. * Enables or disables level capping. If disabled after previously enabled, `nextLevelSwitch` will be immediately called.
  28209. */,
  28210. set: function set(shouldStartCapping) {
  28211. var newCapLevelToPlayerSize = !!shouldStartCapping;
  28212. if (newCapLevelToPlayerSize !== this.config.capLevelToPlayerSize) {
  28213. if (newCapLevelToPlayerSize) {
  28214. this.capLevelController.startCapping(); // If capping occurs, nextLevelSwitch will happen based on size.
  28215. } else {
  28216. this.capLevelController.stopCapping();
  28217. this.autoLevelCapping = -1;
  28218. this.streamController.nextLevelSwitch(); // Now we're uncapped, get the next level asap.
  28219. }
  28220. this.config.capLevelToPlayerSize = newCapLevelToPlayerSize;
  28221. }
  28222. }
  28223. /**
  28224. * Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
  28225. */
  28226. }, {
  28227. key: "autoLevelCapping",
  28228. get: function get() {
  28229. return this._autoLevelCapping;
  28230. }
  28231. /**
  28232. * Returns the current bandwidth estimate in bits per second, when available. Otherwise, `NaN` is returned.
  28233. */,
  28234. set:
  28235. /**
  28236. * Capping/max level value that should be used by automatic level selection algorithm (`ABRController`)
  28237. */
  28238. function set(newLevel) {
  28239. if (this._autoLevelCapping !== newLevel) {
  28240. logger.log("set autoLevelCapping:" + newLevel);
  28241. this._autoLevelCapping = newLevel;
  28242. this.levelController.checkMaxAutoUpdated();
  28243. }
  28244. }
  28245. }, {
  28246. key: "bandwidthEstimate",
  28247. get: function get() {
  28248. var bwEstimator = this.abrController.bwEstimator;
  28249. if (!bwEstimator) {
  28250. return NaN;
  28251. }
  28252. return bwEstimator.getEstimate();
  28253. },
  28254. set: function set(abrEwmaDefaultEstimate) {
  28255. this.abrController.resetEstimator(abrEwmaDefaultEstimate);
  28256. }
  28257. /**
  28258. * get time to first byte estimate
  28259. * @type {number}
  28260. */
  28261. }, {
  28262. key: "ttfbEstimate",
  28263. get: function get() {
  28264. var bwEstimator = this.abrController.bwEstimator;
  28265. if (!bwEstimator) {
  28266. return NaN;
  28267. }
  28268. return bwEstimator.getEstimateTTFB();
  28269. }
  28270. }, {
  28271. key: "maxHdcpLevel",
  28272. get: function get() {
  28273. return this._maxHdcpLevel;
  28274. },
  28275. set: function set(value) {
  28276. if (isHdcpLevel(value) && this._maxHdcpLevel !== value) {
  28277. this._maxHdcpLevel = value;
  28278. this.levelController.checkMaxAutoUpdated();
  28279. }
  28280. }
  28281. /**
  28282. * True when automatic level selection enabled
  28283. */
  28284. }, {
  28285. key: "autoLevelEnabled",
  28286. get: function get() {
  28287. return this.levelController.manualLevel === -1;
  28288. }
  28289. /**
  28290. * Level set manually (if any)
  28291. */
  28292. }, {
  28293. key: "manualLevel",
  28294. get: function get() {
  28295. return this.levelController.manualLevel;
  28296. }
  28297. /**
  28298. * min level selectable in auto mode according to config.minAutoBitrate
  28299. */
  28300. }, {
  28301. key: "minAutoLevel",
  28302. get: function get() {
  28303. var levels = this.levels,
  28304. minAutoBitrate = this.config.minAutoBitrate;
  28305. if (!levels) return 0;
  28306. var len = levels.length;
  28307. for (var i = 0; i < len; i++) {
  28308. if (levels[i].maxBitrate >= minAutoBitrate) {
  28309. return i;
  28310. }
  28311. }
  28312. return 0;
  28313. }
  28314. /**
  28315. * max level selectable in auto mode according to autoLevelCapping
  28316. */
  28317. }, {
  28318. key: "maxAutoLevel",
  28319. get: function get() {
  28320. var levels = this.levels,
  28321. autoLevelCapping = this.autoLevelCapping,
  28322. maxHdcpLevel = this.maxHdcpLevel;
  28323. var maxAutoLevel;
  28324. if (autoLevelCapping === -1 && levels != null && levels.length) {
  28325. maxAutoLevel = levels.length - 1;
  28326. } else {
  28327. maxAutoLevel = autoLevelCapping;
  28328. }
  28329. if (maxHdcpLevel) {
  28330. for (var i = maxAutoLevel; i--;) {
  28331. var hdcpLevel = levels[i].attrs['HDCP-LEVEL'];
  28332. if (hdcpLevel && hdcpLevel <= maxHdcpLevel) {
  28333. return i;
  28334. }
  28335. }
  28336. }
  28337. return maxAutoLevel;
  28338. }
  28339. }, {
  28340. key: "firstAutoLevel",
  28341. get: function get() {
  28342. return this.abrController.firstAutoLevel;
  28343. }
  28344. /**
  28345. * next automatically selected quality level
  28346. */
  28347. }, {
  28348. key: "nextAutoLevel",
  28349. get: function get() {
  28350. return this.abrController.nextAutoLevel;
  28351. }
  28352. /**
  28353. * this setter is used to force next auto level.
  28354. * this is useful to force a switch down in auto mode:
  28355. * in case of load error on level N, hls.js can set nextAutoLevel to N-1 for example)
  28356. * forced value is valid for one fragment. upon successful frag loading at forced level,
  28357. * this value will be resetted to -1 by ABR controller.
  28358. */,
  28359. set: function set(nextLevel) {
  28360. this.abrController.nextAutoLevel = nextLevel;
  28361. }
  28362. /**
  28363. * get the datetime value relative to media.currentTime for the active level Program Date Time if present
  28364. */
  28365. }, {
  28366. key: "playingDate",
  28367. get: function get() {
  28368. return this.streamController.currentProgramDateTime;
  28369. }
  28370. }, {
  28371. key: "mainForwardBufferInfo",
  28372. get: function get() {
  28373. return this.streamController.getMainFwdBufferInfo();
  28374. }
  28375. }, {
  28376. key: "allAudioTracks",
  28377. get: function get() {
  28378. var audioTrackController = this.audioTrackController;
  28379. return audioTrackController ? audioTrackController.allAudioTracks : [];
  28380. }
  28381. /**
  28382. * Get the list of selectable audio tracks
  28383. */
  28384. }, {
  28385. key: "audioTracks",
  28386. get: function get() {
  28387. var audioTrackController = this.audioTrackController;
  28388. return audioTrackController ? audioTrackController.audioTracks : [];
  28389. }
  28390. /**
  28391. * index of the selected audio track (index in audio track lists)
  28392. */
  28393. }, {
  28394. key: "audioTrack",
  28395. get: function get() {
  28396. var audioTrackController = this.audioTrackController;
  28397. return audioTrackController ? audioTrackController.audioTrack : -1;
  28398. }
  28399. /**
  28400. * selects an audio track, based on its index in audio track lists
  28401. */,
  28402. set: function set(audioTrackId) {
  28403. var audioTrackController = this.audioTrackController;
  28404. if (audioTrackController) {
  28405. audioTrackController.audioTrack = audioTrackId;
  28406. }
  28407. }
  28408. /**
  28409. * get the complete list of subtitle tracks across all media groups
  28410. */
  28411. }, {
  28412. key: "allSubtitleTracks",
  28413. get: function get() {
  28414. var subtitleTrackController = this.subtitleTrackController;
  28415. return subtitleTrackController ? subtitleTrackController.allSubtitleTracks : [];
  28416. }
  28417. /**
  28418. * get alternate subtitle tracks list from playlist
  28419. */
  28420. }, {
  28421. key: "subtitleTracks",
  28422. get: function get() {
  28423. var subtitleTrackController = this.subtitleTrackController;
  28424. return subtitleTrackController ? subtitleTrackController.subtitleTracks : [];
  28425. }
  28426. /**
  28427. * index of the selected subtitle track (index in subtitle track lists)
  28428. */
  28429. }, {
  28430. key: "subtitleTrack",
  28431. get: function get() {
  28432. var subtitleTrackController = this.subtitleTrackController;
  28433. return subtitleTrackController ? subtitleTrackController.subtitleTrack : -1;
  28434. },
  28435. set:
  28436. /**
  28437. * select an subtitle track, based on its index in subtitle track lists
  28438. */
  28439. function set(subtitleTrackId) {
  28440. var subtitleTrackController = this.subtitleTrackController;
  28441. if (subtitleTrackController) {
  28442. subtitleTrackController.subtitleTrack = subtitleTrackId;
  28443. }
  28444. }
  28445. /**
  28446. * Whether subtitle display is enabled or not
  28447. */
  28448. }, {
  28449. key: "media",
  28450. get: function get() {
  28451. return this._media;
  28452. }
  28453. }, {
  28454. key: "subtitleDisplay",
  28455. get: function get() {
  28456. var subtitleTrackController = this.subtitleTrackController;
  28457. return subtitleTrackController ? subtitleTrackController.subtitleDisplay : false;
  28458. }
  28459. /**
  28460. * Enable/disable subtitle display rendering
  28461. */,
  28462. set: function set(value) {
  28463. var subtitleTrackController = this.subtitleTrackController;
  28464. if (subtitleTrackController) {
  28465. subtitleTrackController.subtitleDisplay = value;
  28466. }
  28467. }
  28468. /**
  28469. * get mode for Low-Latency HLS loading
  28470. */
  28471. }, {
  28472. key: "lowLatencyMode",
  28473. get: function get() {
  28474. return this.config.lowLatencyMode;
  28475. }
  28476. /**
  28477. * Enable/disable Low-Latency HLS part playlist and segment loading, and start live streams at playlist PART-HOLD-BACK rather than HOLD-BACK.
  28478. */,
  28479. set: function set(mode) {
  28480. this.config.lowLatencyMode = mode;
  28481. }
  28482. /**
  28483. * Position (in seconds) of live sync point (ie edge of live position minus safety delay defined by ```hls.config.liveSyncDuration```)
  28484. * @returns null prior to loading live Playlist
  28485. */
  28486. }, {
  28487. key: "liveSyncPosition",
  28488. get: function get() {
  28489. return this.latencyController.liveSyncPosition;
  28490. }
  28491. /**
  28492. * Estimated position (in seconds) of live edge (ie edge of live playlist plus time sync playlist advanced)
  28493. * @returns 0 before first playlist is loaded
  28494. */
  28495. }, {
  28496. key: "latency",
  28497. get: function get() {
  28498. return this.latencyController.latency;
  28499. }
  28500. /**
  28501. * maximum distance from the edge before the player seeks forward to ```hls.liveSyncPosition```
  28502. * configured using ```liveMaxLatencyDurationCount``` (multiple of target duration) or ```liveMaxLatencyDuration```
  28503. * @returns 0 before first playlist is loaded
  28504. */
  28505. }, {
  28506. key: "maxLatency",
  28507. get: function get() {
  28508. return this.latencyController.maxLatency;
  28509. }
  28510. /**
  28511. * target distance from the edge as calculated by the latency controller
  28512. */
  28513. }, {
  28514. key: "targetLatency",
  28515. get: function get() {
  28516. return this.latencyController.targetLatency;
  28517. }
  28518. /**
  28519. * the rate at which the edge of the current live playlist is advancing or 1 if there is none
  28520. */
  28521. }, {
  28522. key: "drift",
  28523. get: function get() {
  28524. return this.latencyController.drift;
  28525. }
  28526. /**
  28527. * set to true when startLoad is called before MANIFEST_PARSED event
  28528. */
  28529. }, {
  28530. key: "forceStartLoad",
  28531. get: function get() {
  28532. return this.streamController.forceStartLoad;
  28533. }
  28534. }], [{
  28535. key: "version",
  28536. get:
  28537. /**
  28538. * Get the video-dev/hls.js package version.
  28539. */
  28540. function get() {
  28541. return "1.5.11";
  28542. }
  28543. }, {
  28544. key: "Events",
  28545. get: function get() {
  28546. return Events;
  28547. }
  28548. }, {
  28549. key: "ErrorTypes",
  28550. get: function get() {
  28551. return ErrorTypes;
  28552. }
  28553. }, {
  28554. key: "ErrorDetails",
  28555. get: function get() {
  28556. return ErrorDetails;
  28557. }
  28558. /**
  28559. * Get the default configuration applied to new instances.
  28560. */
  28561. }, {
  28562. key: "DefaultConfig",
  28563. get: function get() {
  28564. if (!Hls.defaultConfig) {
  28565. return hlsDefaultConfig;
  28566. }
  28567. return Hls.defaultConfig;
  28568. }
  28569. /**
  28570. * Replace the default configuration applied to new instances.
  28571. */,
  28572. set: function set(defaultConfig) {
  28573. Hls.defaultConfig = defaultConfig;
  28574. }
  28575. }]);
  28576. return Hls;
  28577. }();
  28578. Hls.defaultConfig = void 0;
  28579. return Hls;
  28580. }));
  28581. })(false);
  28582. //# sourceMappingURL=hls.js.map