core.es.js 774 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147
  1. /**
  2. * @license
  3. * Video.js 8.10.0 <http://videojs.com/>
  4. * Copyright Brightcove, Inc. <https://www.brightcove.com/>
  5. * Available under Apache License Version 2.0
  6. * <https://github.com/videojs/video.js/blob/main/LICENSE>
  7. *
  8. * Includes vtt.js <https://github.com/mozilla/vtt.js>
  9. * Available under Apache License Version 2.0
  10. * <https://github.com/mozilla/vtt.js/blob/main/LICENSE>
  11. */
  12. import window from 'global/window';
  13. import document from 'global/document';
  14. import keycode from 'keycode';
  15. import safeParseTuple from 'safe-json-parse/tuple';
  16. import XHR from '@videojs/xhr';
  17. import vtt from 'videojs-vtt.js';
  18. var version = "8.10.0";
  19. /**
  20. * An Object that contains lifecycle hooks as keys which point to an array
  21. * of functions that are run when a lifecycle is triggered
  22. *
  23. * @private
  24. */
  25. const hooks_ = {};
  26. /**
  27. * Get a list of hooks for a specific lifecycle
  28. *
  29. * @param {string} type
  30. * the lifecycle to get hooks from
  31. *
  32. * @param {Function|Function[]} [fn]
  33. * Optionally add a hook (or hooks) to the lifecycle that your are getting.
  34. *
  35. * @return {Array}
  36. * an array of hooks, or an empty array if there are none.
  37. */
  38. const hooks = function (type, fn) {
  39. hooks_[type] = hooks_[type] || [];
  40. if (fn) {
  41. hooks_[type] = hooks_[type].concat(fn);
  42. }
  43. return hooks_[type];
  44. };
  45. /**
  46. * Add a function hook to a specific videojs lifecycle.
  47. *
  48. * @param {string} type
  49. * the lifecycle to hook the function to.
  50. *
  51. * @param {Function|Function[]}
  52. * The function or array of functions to attach.
  53. */
  54. const hook = function (type, fn) {
  55. hooks(type, fn);
  56. };
  57. /**
  58. * Remove a hook from a specific videojs lifecycle.
  59. *
  60. * @param {string} type
  61. * the lifecycle that the function hooked to
  62. *
  63. * @param {Function} fn
  64. * The hooked function to remove
  65. *
  66. * @return {boolean}
  67. * The function that was removed or undef
  68. */
  69. const removeHook = function (type, fn) {
  70. const index = hooks(type).indexOf(fn);
  71. if (index <= -1) {
  72. return false;
  73. }
  74. hooks_[type] = hooks_[type].slice();
  75. hooks_[type].splice(index, 1);
  76. return true;
  77. };
  78. /**
  79. * Add a function hook that will only run once to a specific videojs lifecycle.
  80. *
  81. * @param {string} type
  82. * the lifecycle to hook the function to.
  83. *
  84. * @param {Function|Function[]}
  85. * The function or array of functions to attach.
  86. */
  87. const hookOnce = function (type, fn) {
  88. hooks(type, [].concat(fn).map(original => {
  89. const wrapper = (...args) => {
  90. removeHook(type, wrapper);
  91. return original(...args);
  92. };
  93. return wrapper;
  94. }));
  95. };
  96. /**
  97. * @file fullscreen-api.js
  98. * @module fullscreen-api
  99. */
  100. /**
  101. * Store the browser-specific methods for the fullscreen API.
  102. *
  103. * @type {Object}
  104. * @see [Specification]{@link https://fullscreen.spec.whatwg.org}
  105. * @see [Map Approach From Screenfull.js]{@link https://github.com/sindresorhus/screenfull.js}
  106. */
  107. const FullscreenApi = {
  108. prefixed: true
  109. };
  110. // browser API methods
  111. const apiMap = [['requestFullscreen', 'exitFullscreen', 'fullscreenElement', 'fullscreenEnabled', 'fullscreenchange', 'fullscreenerror', 'fullscreen'],
  112. // WebKit
  113. ['webkitRequestFullscreen', 'webkitExitFullscreen', 'webkitFullscreenElement', 'webkitFullscreenEnabled', 'webkitfullscreenchange', 'webkitfullscreenerror', '-webkit-full-screen']];
  114. const specApi = apiMap[0];
  115. let browserApi;
  116. // determine the supported set of functions
  117. for (let i = 0; i < apiMap.length; i++) {
  118. // check for exitFullscreen function
  119. if (apiMap[i][1] in document) {
  120. browserApi = apiMap[i];
  121. break;
  122. }
  123. }
  124. // map the browser API names to the spec API names
  125. if (browserApi) {
  126. for (let i = 0; i < browserApi.length; i++) {
  127. FullscreenApi[specApi[i]] = browserApi[i];
  128. }
  129. FullscreenApi.prefixed = browserApi[0] !== specApi[0];
  130. }
  131. /**
  132. * @file create-logger.js
  133. * @module create-logger
  134. */
  135. // This is the private tracking variable for the logging history.
  136. let history = [];
  137. /**
  138. * Log messages to the console and history based on the type of message
  139. *
  140. * @private
  141. * @param {string} name
  142. * The name of the console method to use.
  143. *
  144. * @param {Object} log
  145. * The arguments to be passed to the matching console method.
  146. *
  147. * @param {string} [styles]
  148. * styles for name
  149. */
  150. const LogByTypeFactory = (name, log, styles) => (type, level, args) => {
  151. const lvl = log.levels[level];
  152. const lvlRegExp = new RegExp(`^(${lvl})$`);
  153. let resultName = name;
  154. if (type !== 'log') {
  155. // Add the type to the front of the message when it's not "log".
  156. args.unshift(type.toUpperCase() + ':');
  157. }
  158. if (styles) {
  159. resultName = `%c${name}`;
  160. args.unshift(styles);
  161. }
  162. // Add console prefix after adding to history.
  163. args.unshift(resultName + ':');
  164. // Add a clone of the args at this point to history.
  165. if (history) {
  166. history.push([].concat(args));
  167. // only store 1000 history entries
  168. const splice = history.length - 1000;
  169. history.splice(0, splice > 0 ? splice : 0);
  170. }
  171. // If there's no console then don't try to output messages, but they will
  172. // still be stored in history.
  173. if (!window.console) {
  174. return;
  175. }
  176. // Was setting these once outside of this function, but containing them
  177. // in the function makes it easier to test cases where console doesn't exist
  178. // when the module is executed.
  179. let fn = window.console[type];
  180. if (!fn && type === 'debug') {
  181. // Certain browsers don't have support for console.debug. For those, we
  182. // should default to the closest comparable log.
  183. fn = window.console.info || window.console.log;
  184. }
  185. // Bail out if there's no console or if this type is not allowed by the
  186. // current logging level.
  187. if (!fn || !lvl || !lvlRegExp.test(type)) {
  188. return;
  189. }
  190. fn[Array.isArray(args) ? 'apply' : 'call'](window.console, args);
  191. };
  192. function createLogger$1(name, delimiter = ':', styles = '') {
  193. // This is the private tracking variable for logging level.
  194. let level = 'info';
  195. // the curried logByType bound to the specific log and history
  196. let logByType;
  197. /**
  198. * Logs plain debug messages. Similar to `console.log`.
  199. *
  200. * Due to [limitations](https://github.com/jsdoc3/jsdoc/issues/955#issuecomment-313829149)
  201. * of our JSDoc template, we cannot properly document this as both a function
  202. * and a namespace, so its function signature is documented here.
  203. *
  204. * #### Arguments
  205. * ##### *args
  206. * *[]
  207. *
  208. * Any combination of values that could be passed to `console.log()`.
  209. *
  210. * #### Return Value
  211. *
  212. * `undefined`
  213. *
  214. * @namespace
  215. * @param {...*} args
  216. * One or more messages or objects that should be logged.
  217. */
  218. const log = function (...args) {
  219. logByType('log', level, args);
  220. };
  221. // This is the logByType helper that the logging methods below use
  222. logByType = LogByTypeFactory(name, log, styles);
  223. /**
  224. * Create a new subLogger which chains the old name to the new name.
  225. *
  226. * For example, doing `videojs.log.createLogger('player')` and then using that logger will log the following:
  227. * ```js
  228. * mylogger('foo');
  229. * // > VIDEOJS: player: foo
  230. * ```
  231. *
  232. * @param {string} subName
  233. * The name to add call the new logger
  234. * @param {string} [subDelimiter]
  235. * Optional delimiter
  236. * @param {string} [subStyles]
  237. * Optional styles
  238. * @return {Object}
  239. */
  240. log.createLogger = (subName, subDelimiter, subStyles) => {
  241. const resultDelimiter = subDelimiter !== undefined ? subDelimiter : delimiter;
  242. const resultStyles = subStyles !== undefined ? subStyles : styles;
  243. const resultName = `${name} ${resultDelimiter} ${subName}`;
  244. return createLogger$1(resultName, resultDelimiter, resultStyles);
  245. };
  246. /**
  247. * Create a new logger.
  248. *
  249. * @param {string} newName
  250. * The name for the new logger
  251. * @param {string} [newDelimiter]
  252. * Optional delimiter
  253. * @param {string} [newStyles]
  254. * Optional styles
  255. * @return {Object}
  256. */
  257. log.createNewLogger = (newName, newDelimiter, newStyles) => {
  258. return createLogger$1(newName, newDelimiter, newStyles);
  259. };
  260. /**
  261. * Enumeration of available logging levels, where the keys are the level names
  262. * and the values are `|`-separated strings containing logging methods allowed
  263. * in that logging level. These strings are used to create a regular expression
  264. * matching the function name being called.
  265. *
  266. * Levels provided by Video.js are:
  267. *
  268. * - `off`: Matches no calls. Any value that can be cast to `false` will have
  269. * this effect. The most restrictive.
  270. * - `all`: Matches only Video.js-provided functions (`debug`, `log`,
  271. * `log.warn`, and `log.error`).
  272. * - `debug`: Matches `log.debug`, `log`, `log.warn`, and `log.error` calls.
  273. * - `info` (default): Matches `log`, `log.warn`, and `log.error` calls.
  274. * - `warn`: Matches `log.warn` and `log.error` calls.
  275. * - `error`: Matches only `log.error` calls.
  276. *
  277. * @type {Object}
  278. */
  279. log.levels = {
  280. all: 'debug|log|warn|error',
  281. off: '',
  282. debug: 'debug|log|warn|error',
  283. info: 'log|warn|error',
  284. warn: 'warn|error',
  285. error: 'error',
  286. DEFAULT: level
  287. };
  288. /**
  289. * Get or set the current logging level.
  290. *
  291. * If a string matching a key from {@link module:log.levels} is provided, acts
  292. * as a setter.
  293. *
  294. * @param {'all'|'debug'|'info'|'warn'|'error'|'off'} [lvl]
  295. * Pass a valid level to set a new logging level.
  296. *
  297. * @return {string}
  298. * The current logging level.
  299. */
  300. log.level = lvl => {
  301. if (typeof lvl === 'string') {
  302. if (!log.levels.hasOwnProperty(lvl)) {
  303. throw new Error(`"${lvl}" in not a valid log level`);
  304. }
  305. level = lvl;
  306. }
  307. return level;
  308. };
  309. /**
  310. * Returns an array containing everything that has been logged to the history.
  311. *
  312. * This array is a shallow clone of the internal history record. However, its
  313. * contents are _not_ cloned; so, mutating objects inside this array will
  314. * mutate them in history.
  315. *
  316. * @return {Array}
  317. */
  318. log.history = () => history ? [].concat(history) : [];
  319. /**
  320. * Allows you to filter the history by the given logger name
  321. *
  322. * @param {string} fname
  323. * The name to filter by
  324. *
  325. * @return {Array}
  326. * The filtered list to return
  327. */
  328. log.history.filter = fname => {
  329. return (history || []).filter(historyItem => {
  330. // if the first item in each historyItem includes `fname`, then it's a match
  331. return new RegExp(`.*${fname}.*`).test(historyItem[0]);
  332. });
  333. };
  334. /**
  335. * Clears the internal history tracking, but does not prevent further history
  336. * tracking.
  337. */
  338. log.history.clear = () => {
  339. if (history) {
  340. history.length = 0;
  341. }
  342. };
  343. /**
  344. * Disable history tracking if it is currently enabled.
  345. */
  346. log.history.disable = () => {
  347. if (history !== null) {
  348. history.length = 0;
  349. history = null;
  350. }
  351. };
  352. /**
  353. * Enable history tracking if it is currently disabled.
  354. */
  355. log.history.enable = () => {
  356. if (history === null) {
  357. history = [];
  358. }
  359. };
  360. /**
  361. * Logs error messages. Similar to `console.error`.
  362. *
  363. * @param {...*} args
  364. * One or more messages or objects that should be logged as an error
  365. */
  366. log.error = (...args) => logByType('error', level, args);
  367. /**
  368. * Logs warning messages. Similar to `console.warn`.
  369. *
  370. * @param {...*} args
  371. * One or more messages or objects that should be logged as a warning.
  372. */
  373. log.warn = (...args) => logByType('warn', level, args);
  374. /**
  375. * Logs debug messages. Similar to `console.debug`, but may also act as a comparable
  376. * log if `console.debug` is not available
  377. *
  378. * @param {...*} args
  379. * One or more messages or objects that should be logged as debug.
  380. */
  381. log.debug = (...args) => logByType('debug', level, args);
  382. return log;
  383. }
  384. /**
  385. * @file log.js
  386. * @module log
  387. */
  388. const log = createLogger$1('VIDEOJS');
  389. const createLogger = log.createLogger;
  390. /**
  391. * @file obj.js
  392. * @module obj
  393. */
  394. /**
  395. * @callback obj:EachCallback
  396. *
  397. * @param {*} value
  398. * The current key for the object that is being iterated over.
  399. *
  400. * @param {string} key
  401. * The current key-value for object that is being iterated over
  402. */
  403. /**
  404. * @callback obj:ReduceCallback
  405. *
  406. * @param {*} accum
  407. * The value that is accumulating over the reduce loop.
  408. *
  409. * @param {*} value
  410. * The current key for the object that is being iterated over.
  411. *
  412. * @param {string} key
  413. * The current key-value for object that is being iterated over
  414. *
  415. * @return {*}
  416. * The new accumulated value.
  417. */
  418. const toString = Object.prototype.toString;
  419. /**
  420. * Get the keys of an Object
  421. *
  422. * @param {Object}
  423. * The Object to get the keys from
  424. *
  425. * @return {string[]}
  426. * An array of the keys from the object. Returns an empty array if the
  427. * object passed in was invalid or had no keys.
  428. *
  429. * @private
  430. */
  431. const keys = function (object) {
  432. return isObject(object) ? Object.keys(object) : [];
  433. };
  434. /**
  435. * Array-like iteration for objects.
  436. *
  437. * @param {Object} object
  438. * The object to iterate over
  439. *
  440. * @param {obj:EachCallback} fn
  441. * The callback function which is called for each key in the object.
  442. */
  443. function each(object, fn) {
  444. keys(object).forEach(key => fn(object[key], key));
  445. }
  446. /**
  447. * Array-like reduce for objects.
  448. *
  449. * @param {Object} object
  450. * The Object that you want to reduce.
  451. *
  452. * @param {Function} fn
  453. * A callback function which is called for each key in the object. It
  454. * receives the accumulated value and the per-iteration value and key
  455. * as arguments.
  456. *
  457. * @param {*} [initial = 0]
  458. * Starting value
  459. *
  460. * @return {*}
  461. * The final accumulated value.
  462. */
  463. function reduce(object, fn, initial = 0) {
  464. return keys(object).reduce((accum, key) => fn(accum, object[key], key), initial);
  465. }
  466. /**
  467. * Returns whether a value is an object of any kind - including DOM nodes,
  468. * arrays, regular expressions, etc. Not functions, though.
  469. *
  470. * This avoids the gotcha where using `typeof` on a `null` value
  471. * results in `'object'`.
  472. *
  473. * @param {Object} value
  474. * @return {boolean}
  475. */
  476. function isObject(value) {
  477. return !!value && typeof value === 'object';
  478. }
  479. /**
  480. * Returns whether an object appears to be a "plain" object - that is, a
  481. * direct instance of `Object`.
  482. *
  483. * @param {Object} value
  484. * @return {boolean}
  485. */
  486. function isPlain(value) {
  487. return isObject(value) && toString.call(value) === '[object Object]' && value.constructor === Object;
  488. }
  489. /**
  490. * Merge two objects recursively.
  491. *
  492. * Performs a deep merge like
  493. * {@link https://lodash.com/docs/4.17.10#merge|lodash.merge}, but only merges
  494. * plain objects (not arrays, elements, or anything else).
  495. *
  496. * Non-plain object values will be copied directly from the right-most
  497. * argument.
  498. *
  499. * @param {Object[]} sources
  500. * One or more objects to merge into a new object.
  501. *
  502. * @return {Object}
  503. * A new object that is the merged result of all sources.
  504. */
  505. function merge(...sources) {
  506. const result = {};
  507. sources.forEach(source => {
  508. if (!source) {
  509. return;
  510. }
  511. each(source, (value, key) => {
  512. if (!isPlain(value)) {
  513. result[key] = value;
  514. return;
  515. }
  516. if (!isPlain(result[key])) {
  517. result[key] = {};
  518. }
  519. result[key] = merge(result[key], value);
  520. });
  521. });
  522. return result;
  523. }
  524. /**
  525. * Returns an array of values for a given object
  526. *
  527. * @param {Object} source - target object
  528. * @return {Array<unknown>} - object values
  529. */
  530. function values(source = {}) {
  531. const result = [];
  532. for (const key in source) {
  533. if (source.hasOwnProperty(key)) {
  534. const value = source[key];
  535. result.push(value);
  536. }
  537. }
  538. return result;
  539. }
  540. /**
  541. * Object.defineProperty but "lazy", which means that the value is only set after
  542. * it is retrieved the first time, rather than being set right away.
  543. *
  544. * @param {Object} obj the object to set the property on
  545. * @param {string} key the key for the property to set
  546. * @param {Function} getValue the function used to get the value when it is needed.
  547. * @param {boolean} setter whether a setter should be allowed or not
  548. */
  549. function defineLazyProperty(obj, key, getValue, setter = true) {
  550. const set = value => Object.defineProperty(obj, key, {
  551. value,
  552. enumerable: true,
  553. writable: true
  554. });
  555. const options = {
  556. configurable: true,
  557. enumerable: true,
  558. get() {
  559. const value = getValue();
  560. set(value);
  561. return value;
  562. }
  563. };
  564. if (setter) {
  565. options.set = set;
  566. }
  567. return Object.defineProperty(obj, key, options);
  568. }
  569. var Obj = /*#__PURE__*/Object.freeze({
  570. __proto__: null,
  571. each: each,
  572. reduce: reduce,
  573. isObject: isObject,
  574. isPlain: isPlain,
  575. merge: merge,
  576. values: values,
  577. defineLazyProperty: defineLazyProperty
  578. });
  579. /**
  580. * @file browser.js
  581. * @module browser
  582. */
  583. /**
  584. * Whether or not this device is an iPod.
  585. *
  586. * @static
  587. * @type {Boolean}
  588. */
  589. let IS_IPOD = false;
  590. /**
  591. * The detected iOS version - or `null`.
  592. *
  593. * @static
  594. * @type {string|null}
  595. */
  596. let IOS_VERSION = null;
  597. /**
  598. * Whether or not this is an Android device.
  599. *
  600. * @static
  601. * @type {Boolean}
  602. */
  603. let IS_ANDROID = false;
  604. /**
  605. * The detected Android version - or `null` if not Android or indeterminable.
  606. *
  607. * @static
  608. * @type {number|string|null}
  609. */
  610. let ANDROID_VERSION;
  611. /**
  612. * Whether or not this is Mozilla Firefox.
  613. *
  614. * @static
  615. * @type {Boolean}
  616. */
  617. let IS_FIREFOX = false;
  618. /**
  619. * Whether or not this is Microsoft Edge.
  620. *
  621. * @static
  622. * @type {Boolean}
  623. */
  624. let IS_EDGE = false;
  625. /**
  626. * Whether or not this is any Chromium Browser
  627. *
  628. * @static
  629. * @type {Boolean}
  630. */
  631. let IS_CHROMIUM = false;
  632. /**
  633. * Whether or not this is any Chromium browser that is not Edge.
  634. *
  635. * This will also be `true` for Chrome on iOS, which will have different support
  636. * as it is actually Safari under the hood.
  637. *
  638. * Deprecated, as the behaviour to not match Edge was to prevent Legacy Edge's UA matching.
  639. * IS_CHROMIUM should be used instead.
  640. * "Chromium but not Edge" could be explicitly tested with IS_CHROMIUM && !IS_EDGE
  641. *
  642. * @static
  643. * @deprecated
  644. * @type {Boolean}
  645. */
  646. let IS_CHROME = false;
  647. /**
  648. * The detected Chromium version - or `null`.
  649. *
  650. * @static
  651. * @type {number|null}
  652. */
  653. let CHROMIUM_VERSION = null;
  654. /**
  655. * The detected Google Chrome version - or `null`.
  656. * This has always been the _Chromium_ version, i.e. would return on Chromium Edge.
  657. * Deprecated, use CHROMIUM_VERSION instead.
  658. *
  659. * @static
  660. * @deprecated
  661. * @type {number|null}
  662. */
  663. let CHROME_VERSION = null;
  664. /**
  665. * The detected Internet Explorer version - or `null`.
  666. *
  667. * @static
  668. * @deprecated
  669. * @type {number|null}
  670. */
  671. let IE_VERSION = null;
  672. /**
  673. * Whether or not this is desktop Safari.
  674. *
  675. * @static
  676. * @type {Boolean}
  677. */
  678. let IS_SAFARI = false;
  679. /**
  680. * Whether or not this is a Windows machine.
  681. *
  682. * @static
  683. * @type {Boolean}
  684. */
  685. let IS_WINDOWS = false;
  686. /**
  687. * Whether or not this device is an iPad.
  688. *
  689. * @static
  690. * @type {Boolean}
  691. */
  692. let IS_IPAD = false;
  693. /**
  694. * Whether or not this device is an iPhone.
  695. *
  696. * @static
  697. * @type {Boolean}
  698. */
  699. // The Facebook app's UIWebView identifies as both an iPhone and iPad, so
  700. // to identify iPhones, we need to exclude iPads.
  701. // http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/
  702. let IS_IPHONE = false;
  703. /**
  704. * Whether or not this device is touch-enabled.
  705. *
  706. * @static
  707. * @const
  708. * @type {Boolean}
  709. */
  710. const TOUCH_ENABLED = Boolean(isReal() && ('ontouchstart' in window || window.navigator.maxTouchPoints || window.DocumentTouch && window.document instanceof window.DocumentTouch));
  711. const UAD = window.navigator && window.navigator.userAgentData;
  712. if (UAD && UAD.platform && UAD.brands) {
  713. // If userAgentData is present, use it instead of userAgent to avoid warnings
  714. // Currently only implemented on Chromium
  715. // userAgentData does not expose Android version, so ANDROID_VERSION remains `null`
  716. IS_ANDROID = UAD.platform === 'Android';
  717. IS_EDGE = Boolean(UAD.brands.find(b => b.brand === 'Microsoft Edge'));
  718. IS_CHROMIUM = Boolean(UAD.brands.find(b => b.brand === 'Chromium'));
  719. IS_CHROME = !IS_EDGE && IS_CHROMIUM;
  720. CHROMIUM_VERSION = CHROME_VERSION = (UAD.brands.find(b => b.brand === 'Chromium') || {}).version || null;
  721. IS_WINDOWS = UAD.platform === 'Windows';
  722. }
  723. // If the browser is not Chromium, either userAgentData is not present which could be an old Chromium browser,
  724. // or it's a browser that has added userAgentData since that we don't have tests for yet. In either case,
  725. // the checks need to be made agiainst the regular userAgent string.
  726. if (!IS_CHROMIUM) {
  727. const USER_AGENT = window.navigator && window.navigator.userAgent || '';
  728. IS_IPOD = /iPod/i.test(USER_AGENT);
  729. IOS_VERSION = function () {
  730. const match = USER_AGENT.match(/OS (\d+)_/i);
  731. if (match && match[1]) {
  732. return match[1];
  733. }
  734. return null;
  735. }();
  736. IS_ANDROID = /Android/i.test(USER_AGENT);
  737. ANDROID_VERSION = function () {
  738. // This matches Android Major.Minor.Patch versions
  739. // ANDROID_VERSION is Major.Minor as a Number, if Minor isn't available, then only Major is returned
  740. const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i);
  741. if (!match) {
  742. return null;
  743. }
  744. const major = match[1] && parseFloat(match[1]);
  745. const minor = match[2] && parseFloat(match[2]);
  746. if (major && minor) {
  747. return parseFloat(match[1] + '.' + match[2]);
  748. } else if (major) {
  749. return major;
  750. }
  751. return null;
  752. }();
  753. IS_FIREFOX = /Firefox/i.test(USER_AGENT);
  754. IS_EDGE = /Edg/i.test(USER_AGENT);
  755. IS_CHROMIUM = /Chrome/i.test(USER_AGENT) || /CriOS/i.test(USER_AGENT);
  756. IS_CHROME = !IS_EDGE && IS_CHROMIUM;
  757. CHROMIUM_VERSION = CHROME_VERSION = function () {
  758. const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/);
  759. if (match && match[2]) {
  760. return parseFloat(match[2]);
  761. }
  762. return null;
  763. }();
  764. IE_VERSION = function () {
  765. const result = /MSIE\s(\d+)\.\d/.exec(USER_AGENT);
  766. let version = result && parseFloat(result[1]);
  767. if (!version && /Trident\/7.0/i.test(USER_AGENT) && /rv:11.0/.test(USER_AGENT)) {
  768. // IE 11 has a different user agent string than other IE versions
  769. version = 11.0;
  770. }
  771. return version;
  772. }();
  773. IS_SAFARI = /Safari/i.test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE;
  774. IS_WINDOWS = /Windows/i.test(USER_AGENT);
  775. IS_IPAD = /iPad/i.test(USER_AGENT) || IS_SAFARI && TOUCH_ENABLED && !/iPhone/i.test(USER_AGENT);
  776. IS_IPHONE = /iPhone/i.test(USER_AGENT) && !IS_IPAD;
  777. }
  778. /**
  779. * Whether or not this is an iOS device.
  780. *
  781. * @static
  782. * @const
  783. * @type {Boolean}
  784. */
  785. const IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;
  786. /**
  787. * Whether or not this is any flavor of Safari - including iOS.
  788. *
  789. * @static
  790. * @const
  791. * @type {Boolean}
  792. */
  793. const IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;
  794. var browser = /*#__PURE__*/Object.freeze({
  795. __proto__: null,
  796. get IS_IPOD () { return IS_IPOD; },
  797. get IOS_VERSION () { return IOS_VERSION; },
  798. get IS_ANDROID () { return IS_ANDROID; },
  799. get ANDROID_VERSION () { return ANDROID_VERSION; },
  800. get IS_FIREFOX () { return IS_FIREFOX; },
  801. get IS_EDGE () { return IS_EDGE; },
  802. get IS_CHROMIUM () { return IS_CHROMIUM; },
  803. get IS_CHROME () { return IS_CHROME; },
  804. get CHROMIUM_VERSION () { return CHROMIUM_VERSION; },
  805. get CHROME_VERSION () { return CHROME_VERSION; },
  806. get IE_VERSION () { return IE_VERSION; },
  807. get IS_SAFARI () { return IS_SAFARI; },
  808. get IS_WINDOWS () { return IS_WINDOWS; },
  809. get IS_IPAD () { return IS_IPAD; },
  810. get IS_IPHONE () { return IS_IPHONE; },
  811. TOUCH_ENABLED: TOUCH_ENABLED,
  812. IS_IOS: IS_IOS,
  813. IS_ANY_SAFARI: IS_ANY_SAFARI
  814. });
  815. /**
  816. * @file dom.js
  817. * @module dom
  818. */
  819. /**
  820. * Detect if a value is a string with any non-whitespace characters.
  821. *
  822. * @private
  823. * @param {string} str
  824. * The string to check
  825. *
  826. * @return {boolean}
  827. * Will be `true` if the string is non-blank, `false` otherwise.
  828. *
  829. */
  830. function isNonBlankString(str) {
  831. // we use str.trim as it will trim any whitespace characters
  832. // from the front or back of non-whitespace characters. aka
  833. // Any string that contains non-whitespace characters will
  834. // still contain them after `trim` but whitespace only strings
  835. // will have a length of 0, failing this check.
  836. return typeof str === 'string' && Boolean(str.trim());
  837. }
  838. /**
  839. * Throws an error if the passed string has whitespace. This is used by
  840. * class methods to be relatively consistent with the classList API.
  841. *
  842. * @private
  843. * @param {string} str
  844. * The string to check for whitespace.
  845. *
  846. * @throws {Error}
  847. * Throws an error if there is whitespace in the string.
  848. */
  849. function throwIfWhitespace(str) {
  850. // str.indexOf instead of regex because str.indexOf is faster performance wise.
  851. if (str.indexOf(' ') >= 0) {
  852. throw new Error('class has illegal whitespace characters');
  853. }
  854. }
  855. /**
  856. * Whether the current DOM interface appears to be real (i.e. not simulated).
  857. *
  858. * @return {boolean}
  859. * Will be `true` if the DOM appears to be real, `false` otherwise.
  860. */
  861. function isReal() {
  862. // Both document and window will never be undefined thanks to `global`.
  863. return document === window.document;
  864. }
  865. /**
  866. * Determines, via duck typing, whether or not a value is a DOM element.
  867. *
  868. * @param {*} value
  869. * The value to check.
  870. *
  871. * @return {boolean}
  872. * Will be `true` if the value is a DOM element, `false` otherwise.
  873. */
  874. function isEl(value) {
  875. return isObject(value) && value.nodeType === 1;
  876. }
  877. /**
  878. * Determines if the current DOM is embedded in an iframe.
  879. *
  880. * @return {boolean}
  881. * Will be `true` if the DOM is embedded in an iframe, `false`
  882. * otherwise.
  883. */
  884. function isInFrame() {
  885. // We need a try/catch here because Safari will throw errors when attempting
  886. // to get either `parent` or `self`
  887. try {
  888. return window.parent !== window.self;
  889. } catch (x) {
  890. return true;
  891. }
  892. }
  893. /**
  894. * Creates functions to query the DOM using a given method.
  895. *
  896. * @private
  897. * @param {string} method
  898. * The method to create the query with.
  899. *
  900. * @return {Function}
  901. * The query method
  902. */
  903. function createQuerier(method) {
  904. return function (selector, context) {
  905. if (!isNonBlankString(selector)) {
  906. return document[method](null);
  907. }
  908. if (isNonBlankString(context)) {
  909. context = document.querySelector(context);
  910. }
  911. const ctx = isEl(context) ? context : document;
  912. return ctx[method] && ctx[method](selector);
  913. };
  914. }
  915. /**
  916. * Creates an element and applies properties, attributes, and inserts content.
  917. *
  918. * @param {string} [tagName='div']
  919. * Name of tag to be created.
  920. *
  921. * @param {Object} [properties={}]
  922. * Element properties to be applied.
  923. *
  924. * @param {Object} [attributes={}]
  925. * Element attributes to be applied.
  926. *
  927. * @param {ContentDescriptor} [content]
  928. * A content descriptor object.
  929. *
  930. * @return {Element}
  931. * The element that was created.
  932. */
  933. function createEl(tagName = 'div', properties = {}, attributes = {}, content) {
  934. const el = document.createElement(tagName);
  935. Object.getOwnPropertyNames(properties).forEach(function (propName) {
  936. const val = properties[propName];
  937. // Handle textContent since it's not supported everywhere and we have a
  938. // method for it.
  939. if (propName === 'textContent') {
  940. textContent(el, val);
  941. } else if (el[propName] !== val || propName === 'tabIndex') {
  942. el[propName] = val;
  943. }
  944. });
  945. Object.getOwnPropertyNames(attributes).forEach(function (attrName) {
  946. el.setAttribute(attrName, attributes[attrName]);
  947. });
  948. if (content) {
  949. appendContent(el, content);
  950. }
  951. return el;
  952. }
  953. /**
  954. * Injects text into an element, replacing any existing contents entirely.
  955. *
  956. * @param {HTMLElement} el
  957. * The element to add text content into
  958. *
  959. * @param {string} text
  960. * The text content to add.
  961. *
  962. * @return {Element}
  963. * The element with added text content.
  964. */
  965. function textContent(el, text) {
  966. if (typeof el.textContent === 'undefined') {
  967. el.innerText = text;
  968. } else {
  969. el.textContent = text;
  970. }
  971. return el;
  972. }
  973. /**
  974. * Insert an element as the first child node of another
  975. *
  976. * @param {Element} child
  977. * Element to insert
  978. *
  979. * @param {Element} parent
  980. * Element to insert child into
  981. */
  982. function prependTo(child, parent) {
  983. if (parent.firstChild) {
  984. parent.insertBefore(child, parent.firstChild);
  985. } else {
  986. parent.appendChild(child);
  987. }
  988. }
  989. /**
  990. * Check if an element has a class name.
  991. *
  992. * @param {Element} element
  993. * Element to check
  994. *
  995. * @param {string} classToCheck
  996. * Class name to check for
  997. *
  998. * @return {boolean}
  999. * Will be `true` if the element has a class, `false` otherwise.
  1000. *
  1001. * @throws {Error}
  1002. * Throws an error if `classToCheck` has white space.
  1003. */
  1004. function hasClass(element, classToCheck) {
  1005. throwIfWhitespace(classToCheck);
  1006. return element.classList.contains(classToCheck);
  1007. }
  1008. /**
  1009. * Add a class name to an element.
  1010. *
  1011. * @param {Element} element
  1012. * Element to add class name to.
  1013. *
  1014. * @param {...string} classesToAdd
  1015. * One or more class name to add.
  1016. *
  1017. * @return {Element}
  1018. * The DOM element with the added class name.
  1019. */
  1020. function addClass(element, ...classesToAdd) {
  1021. element.classList.add(...classesToAdd.reduce((prev, current) => prev.concat(current.split(/\s+/)), []));
  1022. return element;
  1023. }
  1024. /**
  1025. * Remove a class name from an element.
  1026. *
  1027. * @param {Element} element
  1028. * Element to remove a class name from.
  1029. *
  1030. * @param {...string} classesToRemove
  1031. * One or more class name to remove.
  1032. *
  1033. * @return {Element}
  1034. * The DOM element with class name removed.
  1035. */
  1036. function removeClass(element, ...classesToRemove) {
  1037. // Protect in case the player gets disposed
  1038. if (!element) {
  1039. log.warn("removeClass was called with an element that doesn't exist");
  1040. return null;
  1041. }
  1042. element.classList.remove(...classesToRemove.reduce((prev, current) => prev.concat(current.split(/\s+/)), []));
  1043. return element;
  1044. }
  1045. /**
  1046. * The callback definition for toggleClass.
  1047. *
  1048. * @callback module:dom~PredicateCallback
  1049. * @param {Element} element
  1050. * The DOM element of the Component.
  1051. *
  1052. * @param {string} classToToggle
  1053. * The `className` that wants to be toggled
  1054. *
  1055. * @return {boolean|undefined}
  1056. * If `true` is returned, the `classToToggle` will be added to the
  1057. * `element`. If `false`, the `classToToggle` will be removed from
  1058. * the `element`. If `undefined`, the callback will be ignored.
  1059. */
  1060. /**
  1061. * Adds or removes a class name to/from an element depending on an optional
  1062. * condition or the presence/absence of the class name.
  1063. *
  1064. * @param {Element} element
  1065. * The element to toggle a class name on.
  1066. *
  1067. * @param {string} classToToggle
  1068. * The class that should be toggled.
  1069. *
  1070. * @param {boolean|module:dom~PredicateCallback} [predicate]
  1071. * See the return value for {@link module:dom~PredicateCallback}
  1072. *
  1073. * @return {Element}
  1074. * The element with a class that has been toggled.
  1075. */
  1076. function toggleClass(element, classToToggle, predicate) {
  1077. if (typeof predicate === 'function') {
  1078. predicate = predicate(element, classToToggle);
  1079. }
  1080. if (typeof predicate !== 'boolean') {
  1081. predicate = undefined;
  1082. }
  1083. classToToggle.split(/\s+/).forEach(className => element.classList.toggle(className, predicate));
  1084. return element;
  1085. }
  1086. /**
  1087. * Apply attributes to an HTML element.
  1088. *
  1089. * @param {Element} el
  1090. * Element to add attributes to.
  1091. *
  1092. * @param {Object} [attributes]
  1093. * Attributes to be applied.
  1094. */
  1095. function setAttributes(el, attributes) {
  1096. Object.getOwnPropertyNames(attributes).forEach(function (attrName) {
  1097. const attrValue = attributes[attrName];
  1098. if (attrValue === null || typeof attrValue === 'undefined' || attrValue === false) {
  1099. el.removeAttribute(attrName);
  1100. } else {
  1101. el.setAttribute(attrName, attrValue === true ? '' : attrValue);
  1102. }
  1103. });
  1104. }
  1105. /**
  1106. * Get an element's attribute values, as defined on the HTML tag.
  1107. *
  1108. * Attributes are not the same as properties. They're defined on the tag
  1109. * or with setAttribute.
  1110. *
  1111. * @param {Element} tag
  1112. * Element from which to get tag attributes.
  1113. *
  1114. * @return {Object}
  1115. * All attributes of the element. Boolean attributes will be `true` or
  1116. * `false`, others will be strings.
  1117. */
  1118. function getAttributes(tag) {
  1119. const obj = {};
  1120. // known boolean attributes
  1121. // we can check for matching boolean properties, but not all browsers
  1122. // and not all tags know about these attributes, so, we still want to check them manually
  1123. const knownBooleans = ['autoplay', 'controls', 'playsinline', 'loop', 'muted', 'default', 'defaultMuted'];
  1124. if (tag && tag.attributes && tag.attributes.length > 0) {
  1125. const attrs = tag.attributes;
  1126. for (let i = attrs.length - 1; i >= 0; i--) {
  1127. const attrName = attrs[i].name;
  1128. /** @type {boolean|string} */
  1129. let attrVal = attrs[i].value;
  1130. // check for known booleans
  1131. // the matching element property will return a value for typeof
  1132. if (knownBooleans.includes(attrName)) {
  1133. // the value of an included boolean attribute is typically an empty
  1134. // string ('') which would equal false if we just check for a false value.
  1135. // we also don't want support bad code like autoplay='false'
  1136. attrVal = attrVal !== null ? true : false;
  1137. }
  1138. obj[attrName] = attrVal;
  1139. }
  1140. }
  1141. return obj;
  1142. }
  1143. /**
  1144. * Get the value of an element's attribute.
  1145. *
  1146. * @param {Element} el
  1147. * A DOM element.
  1148. *
  1149. * @param {string} attribute
  1150. * Attribute to get the value of.
  1151. *
  1152. * @return {string}
  1153. * The value of the attribute.
  1154. */
  1155. function getAttribute(el, attribute) {
  1156. return el.getAttribute(attribute);
  1157. }
  1158. /**
  1159. * Set the value of an element's attribute.
  1160. *
  1161. * @param {Element} el
  1162. * A DOM element.
  1163. *
  1164. * @param {string} attribute
  1165. * Attribute to set.
  1166. *
  1167. * @param {string} value
  1168. * Value to set the attribute to.
  1169. */
  1170. function setAttribute(el, attribute, value) {
  1171. el.setAttribute(attribute, value);
  1172. }
  1173. /**
  1174. * Remove an element's attribute.
  1175. *
  1176. * @param {Element} el
  1177. * A DOM element.
  1178. *
  1179. * @param {string} attribute
  1180. * Attribute to remove.
  1181. */
  1182. function removeAttribute(el, attribute) {
  1183. el.removeAttribute(attribute);
  1184. }
  1185. /**
  1186. * Attempt to block the ability to select text.
  1187. */
  1188. function blockTextSelection() {
  1189. document.body.focus();
  1190. document.onselectstart = function () {
  1191. return false;
  1192. };
  1193. }
  1194. /**
  1195. * Turn off text selection blocking.
  1196. */
  1197. function unblockTextSelection() {
  1198. document.onselectstart = function () {
  1199. return true;
  1200. };
  1201. }
  1202. /**
  1203. * Identical to the native `getBoundingClientRect` function, but ensures that
  1204. * the method is supported at all (it is in all browsers we claim to support)
  1205. * and that the element is in the DOM before continuing.
  1206. *
  1207. * This wrapper function also shims properties which are not provided by some
  1208. * older browsers (namely, IE8).
  1209. *
  1210. * Additionally, some browsers do not support adding properties to a
  1211. * `ClientRect`/`DOMRect` object; so, we shallow-copy it with the standard
  1212. * properties (except `x` and `y` which are not widely supported). This helps
  1213. * avoid implementations where keys are non-enumerable.
  1214. *
  1215. * @param {Element} el
  1216. * Element whose `ClientRect` we want to calculate.
  1217. *
  1218. * @return {Object|undefined}
  1219. * Always returns a plain object - or `undefined` if it cannot.
  1220. */
  1221. function getBoundingClientRect(el) {
  1222. if (el && el.getBoundingClientRect && el.parentNode) {
  1223. const rect = el.getBoundingClientRect();
  1224. const result = {};
  1225. ['bottom', 'height', 'left', 'right', 'top', 'width'].forEach(k => {
  1226. if (rect[k] !== undefined) {
  1227. result[k] = rect[k];
  1228. }
  1229. });
  1230. if (!result.height) {
  1231. result.height = parseFloat(computedStyle(el, 'height'));
  1232. }
  1233. if (!result.width) {
  1234. result.width = parseFloat(computedStyle(el, 'width'));
  1235. }
  1236. return result;
  1237. }
  1238. }
  1239. /**
  1240. * Represents the position of a DOM element on the page.
  1241. *
  1242. * @typedef {Object} module:dom~Position
  1243. *
  1244. * @property {number} left
  1245. * Pixels to the left.
  1246. *
  1247. * @property {number} top
  1248. * Pixels from the top.
  1249. */
  1250. /**
  1251. * Get the position of an element in the DOM.
  1252. *
  1253. * Uses `getBoundingClientRect` technique from John Resig.
  1254. *
  1255. * @see http://ejohn.org/blog/getboundingclientrect-is-awesome/
  1256. *
  1257. * @param {Element} el
  1258. * Element from which to get offset.
  1259. *
  1260. * @return {module:dom~Position}
  1261. * The position of the element that was passed in.
  1262. */
  1263. function findPosition(el) {
  1264. if (!el || el && !el.offsetParent) {
  1265. return {
  1266. left: 0,
  1267. top: 0,
  1268. width: 0,
  1269. height: 0
  1270. };
  1271. }
  1272. const width = el.offsetWidth;
  1273. const height = el.offsetHeight;
  1274. let left = 0;
  1275. let top = 0;
  1276. while (el.offsetParent && el !== document[FullscreenApi.fullscreenElement]) {
  1277. left += el.offsetLeft;
  1278. top += el.offsetTop;
  1279. el = el.offsetParent;
  1280. }
  1281. return {
  1282. left,
  1283. top,
  1284. width,
  1285. height
  1286. };
  1287. }
  1288. /**
  1289. * Represents x and y coordinates for a DOM element or mouse pointer.
  1290. *
  1291. * @typedef {Object} module:dom~Coordinates
  1292. *
  1293. * @property {number} x
  1294. * x coordinate in pixels
  1295. *
  1296. * @property {number} y
  1297. * y coordinate in pixels
  1298. */
  1299. /**
  1300. * Get the pointer position within an element.
  1301. *
  1302. * The base on the coordinates are the bottom left of the element.
  1303. *
  1304. * @param {Element} el
  1305. * Element on which to get the pointer position on.
  1306. *
  1307. * @param {Event} event
  1308. * Event object.
  1309. *
  1310. * @return {module:dom~Coordinates}
  1311. * A coordinates object corresponding to the mouse position.
  1312. *
  1313. */
  1314. function getPointerPosition(el, event) {
  1315. const translated = {
  1316. x: 0,
  1317. y: 0
  1318. };
  1319. if (IS_IOS) {
  1320. let item = el;
  1321. while (item && item.nodeName.toLowerCase() !== 'html') {
  1322. const transform = computedStyle(item, 'transform');
  1323. if (/^matrix/.test(transform)) {
  1324. const values = transform.slice(7, -1).split(/,\s/).map(Number);
  1325. translated.x += values[4];
  1326. translated.y += values[5];
  1327. } else if (/^matrix3d/.test(transform)) {
  1328. const values = transform.slice(9, -1).split(/,\s/).map(Number);
  1329. translated.x += values[12];
  1330. translated.y += values[13];
  1331. }
  1332. item = item.parentNode;
  1333. }
  1334. }
  1335. const position = {};
  1336. const boxTarget = findPosition(event.target);
  1337. const box = findPosition(el);
  1338. const boxW = box.width;
  1339. const boxH = box.height;
  1340. let offsetY = event.offsetY - (box.top - boxTarget.top);
  1341. let offsetX = event.offsetX - (box.left - boxTarget.left);
  1342. if (event.changedTouches) {
  1343. offsetX = event.changedTouches[0].pageX - box.left;
  1344. offsetY = event.changedTouches[0].pageY + box.top;
  1345. if (IS_IOS) {
  1346. offsetX -= translated.x;
  1347. offsetY -= translated.y;
  1348. }
  1349. }
  1350. position.y = 1 - Math.max(0, Math.min(1, offsetY / boxH));
  1351. position.x = Math.max(0, Math.min(1, offsetX / boxW));
  1352. return position;
  1353. }
  1354. /**
  1355. * Determines, via duck typing, whether or not a value is a text node.
  1356. *
  1357. * @param {*} value
  1358. * Check if this value is a text node.
  1359. *
  1360. * @return {boolean}
  1361. * Will be `true` if the value is a text node, `false` otherwise.
  1362. */
  1363. function isTextNode(value) {
  1364. return isObject(value) && value.nodeType === 3;
  1365. }
  1366. /**
  1367. * Empties the contents of an element.
  1368. *
  1369. * @param {Element} el
  1370. * The element to empty children from
  1371. *
  1372. * @return {Element}
  1373. * The element with no children
  1374. */
  1375. function emptyEl(el) {
  1376. while (el.firstChild) {
  1377. el.removeChild(el.firstChild);
  1378. }
  1379. return el;
  1380. }
  1381. /**
  1382. * This is a mixed value that describes content to be injected into the DOM
  1383. * via some method. It can be of the following types:
  1384. *
  1385. * Type | Description
  1386. * -----------|-------------
  1387. * `string` | The value will be normalized into a text node.
  1388. * `Element` | The value will be accepted as-is.
  1389. * `Text` | A TextNode. The value will be accepted as-is.
  1390. * `Array` | A one-dimensional array of strings, elements, text nodes, or functions. These functions should return a string, element, or text node (any other return value, like an array, will be ignored).
  1391. * `Function` | A function, which is expected to return a string, element, text node, or array - any of the other possible values described above. This means that a content descriptor could be a function that returns an array of functions, but those second-level functions must return strings, elements, or text nodes.
  1392. *
  1393. * @typedef {string|Element|Text|Array|Function} ContentDescriptor
  1394. */
  1395. /**
  1396. * Normalizes content for eventual insertion into the DOM.
  1397. *
  1398. * This allows a wide range of content definition methods, but helps protect
  1399. * from falling into the trap of simply writing to `innerHTML`, which could
  1400. * be an XSS concern.
  1401. *
  1402. * The content for an element can be passed in multiple types and
  1403. * combinations, whose behavior is as follows:
  1404. *
  1405. * @param {ContentDescriptor} content
  1406. * A content descriptor value.
  1407. *
  1408. * @return {Array}
  1409. * All of the content that was passed in, normalized to an array of
  1410. * elements or text nodes.
  1411. */
  1412. function normalizeContent(content) {
  1413. // First, invoke content if it is a function. If it produces an array,
  1414. // that needs to happen before normalization.
  1415. if (typeof content === 'function') {
  1416. content = content();
  1417. }
  1418. // Next up, normalize to an array, so one or many items can be normalized,
  1419. // filtered, and returned.
  1420. return (Array.isArray(content) ? content : [content]).map(value => {
  1421. // First, invoke value if it is a function to produce a new value,
  1422. // which will be subsequently normalized to a Node of some kind.
  1423. if (typeof value === 'function') {
  1424. value = value();
  1425. }
  1426. if (isEl(value) || isTextNode(value)) {
  1427. return value;
  1428. }
  1429. if (typeof value === 'string' && /\S/.test(value)) {
  1430. return document.createTextNode(value);
  1431. }
  1432. }).filter(value => value);
  1433. }
  1434. /**
  1435. * Normalizes and appends content to an element.
  1436. *
  1437. * @param {Element} el
  1438. * Element to append normalized content to.
  1439. *
  1440. * @param {ContentDescriptor} content
  1441. * A content descriptor value.
  1442. *
  1443. * @return {Element}
  1444. * The element with appended normalized content.
  1445. */
  1446. function appendContent(el, content) {
  1447. normalizeContent(content).forEach(node => el.appendChild(node));
  1448. return el;
  1449. }
  1450. /**
  1451. * Normalizes and inserts content into an element; this is identical to
  1452. * `appendContent()`, except it empties the element first.
  1453. *
  1454. * @param {Element} el
  1455. * Element to insert normalized content into.
  1456. *
  1457. * @param {ContentDescriptor} content
  1458. * A content descriptor value.
  1459. *
  1460. * @return {Element}
  1461. * The element with inserted normalized content.
  1462. */
  1463. function insertContent(el, content) {
  1464. return appendContent(emptyEl(el), content);
  1465. }
  1466. /**
  1467. * Check if an event was a single left click.
  1468. *
  1469. * @param {MouseEvent} event
  1470. * Event object.
  1471. *
  1472. * @return {boolean}
  1473. * Will be `true` if a single left click, `false` otherwise.
  1474. */
  1475. function isSingleLeftClick(event) {
  1476. // Note: if you create something draggable, be sure to
  1477. // call it on both `mousedown` and `mousemove` event,
  1478. // otherwise `mousedown` should be enough for a button
  1479. if (event.button === undefined && event.buttons === undefined) {
  1480. // Why do we need `buttons` ?
  1481. // Because, middle mouse sometimes have this:
  1482. // e.button === 0 and e.buttons === 4
  1483. // Furthermore, we want to prevent combination click, something like
  1484. // HOLD middlemouse then left click, that would be
  1485. // e.button === 0, e.buttons === 5
  1486. // just `button` is not gonna work
  1487. // Alright, then what this block does ?
  1488. // this is for chrome `simulate mobile devices`
  1489. // I want to support this as well
  1490. return true;
  1491. }
  1492. if (event.button === 0 && event.buttons === undefined) {
  1493. // Touch screen, sometimes on some specific device, `buttons`
  1494. // doesn't have anything (safari on ios, blackberry...)
  1495. return true;
  1496. }
  1497. // `mouseup` event on a single left click has
  1498. // `button` and `buttons` equal to 0
  1499. if (event.type === 'mouseup' && event.button === 0 && event.buttons === 0) {
  1500. return true;
  1501. }
  1502. if (event.button !== 0 || event.buttons !== 1) {
  1503. // This is the reason we have those if else block above
  1504. // if any special case we can catch and let it slide
  1505. // we do it above, when get to here, this definitely
  1506. // is-not-left-click
  1507. return false;
  1508. }
  1509. return true;
  1510. }
  1511. /**
  1512. * Finds a single DOM element matching `selector` within the optional
  1513. * `context` of another DOM element (defaulting to `document`).
  1514. *
  1515. * @param {string} selector
  1516. * A valid CSS selector, which will be passed to `querySelector`.
  1517. *
  1518. * @param {Element|String} [context=document]
  1519. * A DOM element within which to query. Can also be a selector
  1520. * string in which case the first matching element will be used
  1521. * as context. If missing (or no element matches selector), falls
  1522. * back to `document`.
  1523. *
  1524. * @return {Element|null}
  1525. * The element that was found or null.
  1526. */
  1527. const $ = createQuerier('querySelector');
  1528. /**
  1529. * Finds a all DOM elements matching `selector` within the optional
  1530. * `context` of another DOM element (defaulting to `document`).
  1531. *
  1532. * @param {string} selector
  1533. * A valid CSS selector, which will be passed to `querySelectorAll`.
  1534. *
  1535. * @param {Element|String} [context=document]
  1536. * A DOM element within which to query. Can also be a selector
  1537. * string in which case the first matching element will be used
  1538. * as context. If missing (or no element matches selector), falls
  1539. * back to `document`.
  1540. *
  1541. * @return {NodeList}
  1542. * A element list of elements that were found. Will be empty if none
  1543. * were found.
  1544. *
  1545. */
  1546. const $$ = createQuerier('querySelectorAll');
  1547. /**
  1548. * A safe getComputedStyle.
  1549. *
  1550. * This is needed because in Firefox, if the player is loaded in an iframe with
  1551. * `display:none`, then `getComputedStyle` returns `null`, so, we do a
  1552. * null-check to make sure that the player doesn't break in these cases.
  1553. *
  1554. * @param {Element} el
  1555. * The element you want the computed style of
  1556. *
  1557. * @param {string} prop
  1558. * The property name you want
  1559. *
  1560. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  1561. */
  1562. function computedStyle(el, prop) {
  1563. if (!el || !prop) {
  1564. return '';
  1565. }
  1566. if (typeof window.getComputedStyle === 'function') {
  1567. let computedStyleValue;
  1568. try {
  1569. computedStyleValue = window.getComputedStyle(el);
  1570. } catch (e) {
  1571. return '';
  1572. }
  1573. return computedStyleValue ? computedStyleValue.getPropertyValue(prop) || computedStyleValue[prop] : '';
  1574. }
  1575. return '';
  1576. }
  1577. /**
  1578. * Copy document style sheets to another window.
  1579. *
  1580. * @param {Window} win
  1581. * The window element you want to copy the document style sheets to.
  1582. *
  1583. */
  1584. function copyStyleSheetsToWindow(win) {
  1585. [...document.styleSheets].forEach(styleSheet => {
  1586. try {
  1587. const cssRules = [...styleSheet.cssRules].map(rule => rule.cssText).join('');
  1588. const style = document.createElement('style');
  1589. style.textContent = cssRules;
  1590. win.document.head.appendChild(style);
  1591. } catch (e) {
  1592. const link = document.createElement('link');
  1593. link.rel = 'stylesheet';
  1594. link.type = styleSheet.type;
  1595. // For older Safari this has to be the string; on other browsers setting the MediaList works
  1596. link.media = styleSheet.media.mediaText;
  1597. link.href = styleSheet.href;
  1598. win.document.head.appendChild(link);
  1599. }
  1600. });
  1601. }
  1602. var Dom = /*#__PURE__*/Object.freeze({
  1603. __proto__: null,
  1604. isReal: isReal,
  1605. isEl: isEl,
  1606. isInFrame: isInFrame,
  1607. createEl: createEl,
  1608. textContent: textContent,
  1609. prependTo: prependTo,
  1610. hasClass: hasClass,
  1611. addClass: addClass,
  1612. removeClass: removeClass,
  1613. toggleClass: toggleClass,
  1614. setAttributes: setAttributes,
  1615. getAttributes: getAttributes,
  1616. getAttribute: getAttribute,
  1617. setAttribute: setAttribute,
  1618. removeAttribute: removeAttribute,
  1619. blockTextSelection: blockTextSelection,
  1620. unblockTextSelection: unblockTextSelection,
  1621. getBoundingClientRect: getBoundingClientRect,
  1622. findPosition: findPosition,
  1623. getPointerPosition: getPointerPosition,
  1624. isTextNode: isTextNode,
  1625. emptyEl: emptyEl,
  1626. normalizeContent: normalizeContent,
  1627. appendContent: appendContent,
  1628. insertContent: insertContent,
  1629. isSingleLeftClick: isSingleLeftClick,
  1630. $: $,
  1631. $$: $$,
  1632. computedStyle: computedStyle,
  1633. copyStyleSheetsToWindow: copyStyleSheetsToWindow
  1634. });
  1635. /**
  1636. * @file setup.js - Functions for setting up a player without
  1637. * user interaction based on the data-setup `attribute` of the video tag.
  1638. *
  1639. * @module setup
  1640. */
  1641. let _windowLoaded = false;
  1642. let videojs$1;
  1643. /**
  1644. * Set up any tags that have a data-setup `attribute` when the player is started.
  1645. */
  1646. const autoSetup = function () {
  1647. if (videojs$1.options.autoSetup === false) {
  1648. return;
  1649. }
  1650. const vids = Array.prototype.slice.call(document.getElementsByTagName('video'));
  1651. const audios = Array.prototype.slice.call(document.getElementsByTagName('audio'));
  1652. const divs = Array.prototype.slice.call(document.getElementsByTagName('video-js'));
  1653. const mediaEls = vids.concat(audios, divs);
  1654. // Check if any media elements exist
  1655. if (mediaEls && mediaEls.length > 0) {
  1656. for (let i = 0, e = mediaEls.length; i < e; i++) {
  1657. const mediaEl = mediaEls[i];
  1658. // Check if element exists, has getAttribute func.
  1659. if (mediaEl && mediaEl.getAttribute) {
  1660. // Make sure this player hasn't already been set up.
  1661. if (mediaEl.player === undefined) {
  1662. const options = mediaEl.getAttribute('data-setup');
  1663. // Check if data-setup attr exists.
  1664. // We only auto-setup if they've added the data-setup attr.
  1665. if (options !== null) {
  1666. // Create new video.js instance.
  1667. videojs$1(mediaEl);
  1668. }
  1669. }
  1670. // If getAttribute isn't defined, we need to wait for the DOM.
  1671. } else {
  1672. autoSetupTimeout(1);
  1673. break;
  1674. }
  1675. }
  1676. // No videos were found, so keep looping unless page is finished loading.
  1677. } else if (!_windowLoaded) {
  1678. autoSetupTimeout(1);
  1679. }
  1680. };
  1681. /**
  1682. * Wait until the page is loaded before running autoSetup. This will be called in
  1683. * autoSetup if `hasLoaded` returns false.
  1684. *
  1685. * @param {number} wait
  1686. * How long to wait in ms
  1687. *
  1688. * @param {module:videojs} [vjs]
  1689. * The videojs library function
  1690. */
  1691. function autoSetupTimeout(wait, vjs) {
  1692. // Protect against breakage in non-browser environments
  1693. if (!isReal()) {
  1694. return;
  1695. }
  1696. if (vjs) {
  1697. videojs$1 = vjs;
  1698. }
  1699. window.setTimeout(autoSetup, wait);
  1700. }
  1701. /**
  1702. * Used to set the internal tracking of window loaded state to true.
  1703. *
  1704. * @private
  1705. */
  1706. function setWindowLoaded() {
  1707. _windowLoaded = true;
  1708. window.removeEventListener('load', setWindowLoaded);
  1709. }
  1710. if (isReal()) {
  1711. if (document.readyState === 'complete') {
  1712. setWindowLoaded();
  1713. } else {
  1714. /**
  1715. * Listen for the load event on window, and set _windowLoaded to true.
  1716. *
  1717. * We use a standard event listener here to avoid incrementing the GUID
  1718. * before any players are created.
  1719. *
  1720. * @listens load
  1721. */
  1722. window.addEventListener('load', setWindowLoaded);
  1723. }
  1724. }
  1725. /**
  1726. * @file stylesheet.js
  1727. * @module stylesheet
  1728. */
  1729. /**
  1730. * Create a DOM style element given a className for it.
  1731. *
  1732. * @param {string} className
  1733. * The className to add to the created style element.
  1734. *
  1735. * @return {Element}
  1736. * The element that was created.
  1737. */
  1738. const createStyleElement = function (className) {
  1739. const style = document.createElement('style');
  1740. style.className = className;
  1741. return style;
  1742. };
  1743. /**
  1744. * Add text to a DOM element.
  1745. *
  1746. * @param {Element} el
  1747. * The Element to add text content to.
  1748. *
  1749. * @param {string} content
  1750. * The text to add to the element.
  1751. */
  1752. const setTextContent = function (el, content) {
  1753. if (el.styleSheet) {
  1754. el.styleSheet.cssText = content;
  1755. } else {
  1756. el.textContent = content;
  1757. }
  1758. };
  1759. /**
  1760. * @file dom-data.js
  1761. * @module dom-data
  1762. */
  1763. /**
  1764. * Element Data Store.
  1765. *
  1766. * Allows for binding data to an element without putting it directly on the
  1767. * element. Ex. Event listeners are stored here.
  1768. * (also from jsninja.com, slightly modified and updated for closure compiler)
  1769. *
  1770. * @type {Object}
  1771. * @private
  1772. */
  1773. var DomData = new WeakMap();
  1774. /**
  1775. * @file guid.js
  1776. * @module guid
  1777. */
  1778. // Default value for GUIDs. This allows us to reset the GUID counter in tests.
  1779. //
  1780. // The initial GUID is 3 because some users have come to rely on the first
  1781. // default player ID ending up as `vjs_video_3`.
  1782. //
  1783. // See: https://github.com/videojs/video.js/pull/6216
  1784. const _initialGuid = 3;
  1785. /**
  1786. * Unique ID for an element or function
  1787. *
  1788. * @type {Number}
  1789. */
  1790. let _guid = _initialGuid;
  1791. /**
  1792. * Get a unique auto-incrementing ID by number that has not been returned before.
  1793. *
  1794. * @return {number}
  1795. * A new unique ID.
  1796. */
  1797. function newGUID() {
  1798. return _guid++;
  1799. }
  1800. /**
  1801. * @file events.js. An Event System (John Resig - Secrets of a JS Ninja http://jsninja.com/)
  1802. * (Original book version wasn't completely usable, so fixed some things and made Closure Compiler compatible)
  1803. * This should work very similarly to jQuery's events, however it's based off the book version which isn't as
  1804. * robust as jquery's, so there's probably some differences.
  1805. *
  1806. * @file events.js
  1807. * @module events
  1808. */
  1809. /**
  1810. * Clean up the listener cache and dispatchers
  1811. *
  1812. * @param {Element|Object} elem
  1813. * Element to clean up
  1814. *
  1815. * @param {string} type
  1816. * Type of event to clean up
  1817. */
  1818. function _cleanUpEvents(elem, type) {
  1819. if (!DomData.has(elem)) {
  1820. return;
  1821. }
  1822. const data = DomData.get(elem);
  1823. // Remove the events of a particular type if there are none left
  1824. if (data.handlers[type].length === 0) {
  1825. delete data.handlers[type];
  1826. // data.handlers[type] = null;
  1827. // Setting to null was causing an error with data.handlers
  1828. // Remove the meta-handler from the element
  1829. if (elem.removeEventListener) {
  1830. elem.removeEventListener(type, data.dispatcher, false);
  1831. } else if (elem.detachEvent) {
  1832. elem.detachEvent('on' + type, data.dispatcher);
  1833. }
  1834. }
  1835. // Remove the events object if there are no types left
  1836. if (Object.getOwnPropertyNames(data.handlers).length <= 0) {
  1837. delete data.handlers;
  1838. delete data.dispatcher;
  1839. delete data.disabled;
  1840. }
  1841. // Finally remove the element data if there is no data left
  1842. if (Object.getOwnPropertyNames(data).length === 0) {
  1843. DomData.delete(elem);
  1844. }
  1845. }
  1846. /**
  1847. * Loops through an array of event types and calls the requested method for each type.
  1848. *
  1849. * @param {Function} fn
  1850. * The event method we want to use.
  1851. *
  1852. * @param {Element|Object} elem
  1853. * Element or object to bind listeners to
  1854. *
  1855. * @param {string[]} types
  1856. * Type of event to bind to.
  1857. *
  1858. * @param {Function} callback
  1859. * Event listener.
  1860. */
  1861. function _handleMultipleEvents(fn, elem, types, callback) {
  1862. types.forEach(function (type) {
  1863. // Call the event method for each one of the types
  1864. fn(elem, type, callback);
  1865. });
  1866. }
  1867. /**
  1868. * Fix a native event to have standard property values
  1869. *
  1870. * @param {Object} event
  1871. * Event object to fix.
  1872. *
  1873. * @return {Object}
  1874. * Fixed event object.
  1875. */
  1876. function fixEvent(event) {
  1877. if (event.fixed_) {
  1878. return event;
  1879. }
  1880. function returnTrue() {
  1881. return true;
  1882. }
  1883. function returnFalse() {
  1884. return false;
  1885. }
  1886. // Test if fixing up is needed
  1887. // Used to check if !event.stopPropagation instead of isPropagationStopped
  1888. // But native events return true for stopPropagation, but don't have
  1889. // other expected methods like isPropagationStopped. Seems to be a problem
  1890. // with the Javascript Ninja code. So we're just overriding all events now.
  1891. if (!event || !event.isPropagationStopped || !event.isImmediatePropagationStopped) {
  1892. const old = event || window.event;
  1893. event = {};
  1894. // Clone the old object so that we can modify the values event = {};
  1895. // IE8 Doesn't like when you mess with native event properties
  1896. // Firefox returns false for event.hasOwnProperty('type') and other props
  1897. // which makes copying more difficult.
  1898. // TODO: Probably best to create a whitelist of event props
  1899. for (const key in old) {
  1900. // Safari 6.0.3 warns you if you try to copy deprecated layerX/Y
  1901. // Chrome warns you if you try to copy deprecated keyboardEvent.keyLocation
  1902. // and webkitMovementX/Y
  1903. // Lighthouse complains if Event.path is copied
  1904. if (key !== 'layerX' && key !== 'layerY' && key !== 'keyLocation' && key !== 'webkitMovementX' && key !== 'webkitMovementY' && key !== 'path') {
  1905. // Chrome 32+ warns if you try to copy deprecated returnValue, but
  1906. // we still want to if preventDefault isn't supported (IE8).
  1907. if (!(key === 'returnValue' && old.preventDefault)) {
  1908. event[key] = old[key];
  1909. }
  1910. }
  1911. }
  1912. // The event occurred on this element
  1913. if (!event.target) {
  1914. event.target = event.srcElement || document;
  1915. }
  1916. // Handle which other element the event is related to
  1917. if (!event.relatedTarget) {
  1918. event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
  1919. }
  1920. // Stop the default browser action
  1921. event.preventDefault = function () {
  1922. if (old.preventDefault) {
  1923. old.preventDefault();
  1924. }
  1925. event.returnValue = false;
  1926. old.returnValue = false;
  1927. event.defaultPrevented = true;
  1928. };
  1929. event.defaultPrevented = false;
  1930. // Stop the event from bubbling
  1931. event.stopPropagation = function () {
  1932. if (old.stopPropagation) {
  1933. old.stopPropagation();
  1934. }
  1935. event.cancelBubble = true;
  1936. old.cancelBubble = true;
  1937. event.isPropagationStopped = returnTrue;
  1938. };
  1939. event.isPropagationStopped = returnFalse;
  1940. // Stop the event from bubbling and executing other handlers
  1941. event.stopImmediatePropagation = function () {
  1942. if (old.stopImmediatePropagation) {
  1943. old.stopImmediatePropagation();
  1944. }
  1945. event.isImmediatePropagationStopped = returnTrue;
  1946. event.stopPropagation();
  1947. };
  1948. event.isImmediatePropagationStopped = returnFalse;
  1949. // Handle mouse position
  1950. if (event.clientX !== null && event.clientX !== undefined) {
  1951. const doc = document.documentElement;
  1952. const body = document.body;
  1953. event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
  1954. event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
  1955. }
  1956. // Handle key presses
  1957. event.which = event.charCode || event.keyCode;
  1958. // Fix button for mouse clicks:
  1959. // 0 == left; 1 == middle; 2 == right
  1960. if (event.button !== null && event.button !== undefined) {
  1961. // The following is disabled because it does not pass videojs-standard
  1962. // and... yikes.
  1963. /* eslint-disable */
  1964. event.button = event.button & 1 ? 0 : event.button & 4 ? 1 : event.button & 2 ? 2 : 0;
  1965. /* eslint-enable */
  1966. }
  1967. }
  1968. event.fixed_ = true;
  1969. // Returns fixed-up instance
  1970. return event;
  1971. }
  1972. /**
  1973. * Whether passive event listeners are supported
  1974. */
  1975. let _supportsPassive;
  1976. const supportsPassive = function () {
  1977. if (typeof _supportsPassive !== 'boolean') {
  1978. _supportsPassive = false;
  1979. try {
  1980. const opts = Object.defineProperty({}, 'passive', {
  1981. get() {
  1982. _supportsPassive = true;
  1983. }
  1984. });
  1985. window.addEventListener('test', null, opts);
  1986. window.removeEventListener('test', null, opts);
  1987. } catch (e) {
  1988. // disregard
  1989. }
  1990. }
  1991. return _supportsPassive;
  1992. };
  1993. /**
  1994. * Touch events Chrome expects to be passive
  1995. */
  1996. const passiveEvents = ['touchstart', 'touchmove'];
  1997. /**
  1998. * Add an event listener to element
  1999. * It stores the handler function in a separate cache object
  2000. * and adds a generic handler to the element's event,
  2001. * along with a unique id (guid) to the element.
  2002. *
  2003. * @param {Element|Object} elem
  2004. * Element or object to bind listeners to
  2005. *
  2006. * @param {string|string[]} type
  2007. * Type of event to bind to.
  2008. *
  2009. * @param {Function} fn
  2010. * Event listener.
  2011. */
  2012. function on(elem, type, fn) {
  2013. if (Array.isArray(type)) {
  2014. return _handleMultipleEvents(on, elem, type, fn);
  2015. }
  2016. if (!DomData.has(elem)) {
  2017. DomData.set(elem, {});
  2018. }
  2019. const data = DomData.get(elem);
  2020. // We need a place to store all our handler data
  2021. if (!data.handlers) {
  2022. data.handlers = {};
  2023. }
  2024. if (!data.handlers[type]) {
  2025. data.handlers[type] = [];
  2026. }
  2027. if (!fn.guid) {
  2028. fn.guid = newGUID();
  2029. }
  2030. data.handlers[type].push(fn);
  2031. if (!data.dispatcher) {
  2032. data.disabled = false;
  2033. data.dispatcher = function (event, hash) {
  2034. if (data.disabled) {
  2035. return;
  2036. }
  2037. event = fixEvent(event);
  2038. const handlers = data.handlers[event.type];
  2039. if (handlers) {
  2040. // Copy handlers so if handlers are added/removed during the process it doesn't throw everything off.
  2041. const handlersCopy = handlers.slice(0);
  2042. for (let m = 0, n = handlersCopy.length; m < n; m++) {
  2043. if (event.isImmediatePropagationStopped()) {
  2044. break;
  2045. } else {
  2046. try {
  2047. handlersCopy[m].call(elem, event, hash);
  2048. } catch (e) {
  2049. log.error(e);
  2050. }
  2051. }
  2052. }
  2053. }
  2054. };
  2055. }
  2056. if (data.handlers[type].length === 1) {
  2057. if (elem.addEventListener) {
  2058. let options = false;
  2059. if (supportsPassive() && passiveEvents.indexOf(type) > -1) {
  2060. options = {
  2061. passive: true
  2062. };
  2063. }
  2064. elem.addEventListener(type, data.dispatcher, options);
  2065. } else if (elem.attachEvent) {
  2066. elem.attachEvent('on' + type, data.dispatcher);
  2067. }
  2068. }
  2069. }
  2070. /**
  2071. * Removes event listeners from an element
  2072. *
  2073. * @param {Element|Object} elem
  2074. * Object to remove listeners from.
  2075. *
  2076. * @param {string|string[]} [type]
  2077. * Type of listener to remove. Don't include to remove all events from element.
  2078. *
  2079. * @param {Function} [fn]
  2080. * Specific listener to remove. Don't include to remove listeners for an event
  2081. * type.
  2082. */
  2083. function off(elem, type, fn) {
  2084. // Don't want to add a cache object through getElData if not needed
  2085. if (!DomData.has(elem)) {
  2086. return;
  2087. }
  2088. const data = DomData.get(elem);
  2089. // If no events exist, nothing to unbind
  2090. if (!data.handlers) {
  2091. return;
  2092. }
  2093. if (Array.isArray(type)) {
  2094. return _handleMultipleEvents(off, elem, type, fn);
  2095. }
  2096. // Utility function
  2097. const removeType = function (el, t) {
  2098. data.handlers[t] = [];
  2099. _cleanUpEvents(el, t);
  2100. };
  2101. // Are we removing all bound events?
  2102. if (type === undefined) {
  2103. for (const t in data.handlers) {
  2104. if (Object.prototype.hasOwnProperty.call(data.handlers || {}, t)) {
  2105. removeType(elem, t);
  2106. }
  2107. }
  2108. return;
  2109. }
  2110. const handlers = data.handlers[type];
  2111. // If no handlers exist, nothing to unbind
  2112. if (!handlers) {
  2113. return;
  2114. }
  2115. // If no listener was provided, remove all listeners for type
  2116. if (!fn) {
  2117. removeType(elem, type);
  2118. return;
  2119. }
  2120. // We're only removing a single handler
  2121. if (fn.guid) {
  2122. for (let n = 0; n < handlers.length; n++) {
  2123. if (handlers[n].guid === fn.guid) {
  2124. handlers.splice(n--, 1);
  2125. }
  2126. }
  2127. }
  2128. _cleanUpEvents(elem, type);
  2129. }
  2130. /**
  2131. * Trigger an event for an element
  2132. *
  2133. * @param {Element|Object} elem
  2134. * Element to trigger an event on
  2135. *
  2136. * @param {EventTarget~Event|string} event
  2137. * A string (the type) or an event object with a type attribute
  2138. *
  2139. * @param {Object} [hash]
  2140. * data hash to pass along with the event
  2141. *
  2142. * @return {boolean|undefined}
  2143. * Returns the opposite of `defaultPrevented` if default was
  2144. * prevented. Otherwise, returns `undefined`
  2145. */
  2146. function trigger(elem, event, hash) {
  2147. // Fetches element data and a reference to the parent (for bubbling).
  2148. // Don't want to add a data object to cache for every parent,
  2149. // so checking hasElData first.
  2150. const elemData = DomData.has(elem) ? DomData.get(elem) : {};
  2151. const parent = elem.parentNode || elem.ownerDocument;
  2152. // type = event.type || event,
  2153. // handler;
  2154. // If an event name was passed as a string, creates an event out of it
  2155. if (typeof event === 'string') {
  2156. event = {
  2157. type: event,
  2158. target: elem
  2159. };
  2160. } else if (!event.target) {
  2161. event.target = elem;
  2162. }
  2163. // Normalizes the event properties.
  2164. event = fixEvent(event);
  2165. // If the passed element has a dispatcher, executes the established handlers.
  2166. if (elemData.dispatcher) {
  2167. elemData.dispatcher.call(elem, event, hash);
  2168. }
  2169. // Unless explicitly stopped or the event does not bubble (e.g. media events)
  2170. // recursively calls this function to bubble the event up the DOM.
  2171. if (parent && !event.isPropagationStopped() && event.bubbles === true) {
  2172. trigger.call(null, parent, event, hash);
  2173. // If at the top of the DOM, triggers the default action unless disabled.
  2174. } else if (!parent && !event.defaultPrevented && event.target && event.target[event.type]) {
  2175. if (!DomData.has(event.target)) {
  2176. DomData.set(event.target, {});
  2177. }
  2178. const targetData = DomData.get(event.target);
  2179. // Checks if the target has a default action for this event.
  2180. if (event.target[event.type]) {
  2181. // Temporarily disables event dispatching on the target as we have already executed the handler.
  2182. targetData.disabled = true;
  2183. // Executes the default action.
  2184. if (typeof event.target[event.type] === 'function') {
  2185. event.target[event.type]();
  2186. }
  2187. // Re-enables event dispatching.
  2188. targetData.disabled = false;
  2189. }
  2190. }
  2191. // Inform the triggerer if the default was prevented by returning false
  2192. return !event.defaultPrevented;
  2193. }
  2194. /**
  2195. * Trigger a listener only once for an event.
  2196. *
  2197. * @param {Element|Object} elem
  2198. * Element or object to bind to.
  2199. *
  2200. * @param {string|string[]} type
  2201. * Name/type of event
  2202. *
  2203. * @param {Event~EventListener} fn
  2204. * Event listener function
  2205. */
  2206. function one(elem, type, fn) {
  2207. if (Array.isArray(type)) {
  2208. return _handleMultipleEvents(one, elem, type, fn);
  2209. }
  2210. const func = function () {
  2211. off(elem, type, func);
  2212. fn.apply(this, arguments);
  2213. };
  2214. // copy the guid to the new function so it can removed using the original function's ID
  2215. func.guid = fn.guid = fn.guid || newGUID();
  2216. on(elem, type, func);
  2217. }
  2218. /**
  2219. * Trigger a listener only once and then turn if off for all
  2220. * configured events
  2221. *
  2222. * @param {Element|Object} elem
  2223. * Element or object to bind to.
  2224. *
  2225. * @param {string|string[]} type
  2226. * Name/type of event
  2227. *
  2228. * @param {Event~EventListener} fn
  2229. * Event listener function
  2230. */
  2231. function any(elem, type, fn) {
  2232. const func = function () {
  2233. off(elem, type, func);
  2234. fn.apply(this, arguments);
  2235. };
  2236. // copy the guid to the new function so it can removed using the original function's ID
  2237. func.guid = fn.guid = fn.guid || newGUID();
  2238. // multiple ons, but one off for everything
  2239. on(elem, type, func);
  2240. }
  2241. var Events = /*#__PURE__*/Object.freeze({
  2242. __proto__: null,
  2243. fixEvent: fixEvent,
  2244. on: on,
  2245. off: off,
  2246. trigger: trigger,
  2247. one: one,
  2248. any: any
  2249. });
  2250. /**
  2251. * @file fn.js
  2252. * @module fn
  2253. */
  2254. const UPDATE_REFRESH_INTERVAL = 30;
  2255. /**
  2256. * A private, internal-only function for changing the context of a function.
  2257. *
  2258. * It also stores a unique id on the function so it can be easily removed from
  2259. * events.
  2260. *
  2261. * @private
  2262. * @function
  2263. * @param {*} context
  2264. * The object to bind as scope.
  2265. *
  2266. * @param {Function} fn
  2267. * The function to be bound to a scope.
  2268. *
  2269. * @param {number} [uid]
  2270. * An optional unique ID for the function to be set
  2271. *
  2272. * @return {Function}
  2273. * The new function that will be bound into the context given
  2274. */
  2275. const bind_ = function (context, fn, uid) {
  2276. // Make sure the function has a unique ID
  2277. if (!fn.guid) {
  2278. fn.guid = newGUID();
  2279. }
  2280. // Create the new function that changes the context
  2281. const bound = fn.bind(context);
  2282. // Allow for the ability to individualize this function
  2283. // Needed in the case where multiple objects might share the same prototype
  2284. // IF both items add an event listener with the same function, then you try to remove just one
  2285. // it will remove both because they both have the same guid.
  2286. // when using this, you need to use the bind method when you remove the listener as well.
  2287. // currently used in text tracks
  2288. bound.guid = uid ? uid + '_' + fn.guid : fn.guid;
  2289. return bound;
  2290. };
  2291. /**
  2292. * Wraps the given function, `fn`, with a new function that only invokes `fn`
  2293. * at most once per every `wait` milliseconds.
  2294. *
  2295. * @function
  2296. * @param {Function} fn
  2297. * The function to be throttled.
  2298. *
  2299. * @param {number} wait
  2300. * The number of milliseconds by which to throttle.
  2301. *
  2302. * @return {Function}
  2303. */
  2304. const throttle = function (fn, wait) {
  2305. let last = window.performance.now();
  2306. const throttled = function (...args) {
  2307. const now = window.performance.now();
  2308. if (now - last >= wait) {
  2309. fn(...args);
  2310. last = now;
  2311. }
  2312. };
  2313. return throttled;
  2314. };
  2315. /**
  2316. * Creates a debounced function that delays invoking `func` until after `wait`
  2317. * milliseconds have elapsed since the last time the debounced function was
  2318. * invoked.
  2319. *
  2320. * Inspired by lodash and underscore implementations.
  2321. *
  2322. * @function
  2323. * @param {Function} func
  2324. * The function to wrap with debounce behavior.
  2325. *
  2326. * @param {number} wait
  2327. * The number of milliseconds to wait after the last invocation.
  2328. *
  2329. * @param {boolean} [immediate]
  2330. * Whether or not to invoke the function immediately upon creation.
  2331. *
  2332. * @param {Object} [context=window]
  2333. * The "context" in which the debounced function should debounce. For
  2334. * example, if this function should be tied to a Video.js player,
  2335. * the player can be passed here. Alternatively, defaults to the
  2336. * global `window` object.
  2337. *
  2338. * @return {Function}
  2339. * A debounced function.
  2340. */
  2341. const debounce = function (func, wait, immediate, context = window) {
  2342. let timeout;
  2343. const cancel = () => {
  2344. context.clearTimeout(timeout);
  2345. timeout = null;
  2346. };
  2347. /* eslint-disable consistent-this */
  2348. const debounced = function () {
  2349. const self = this;
  2350. const args = arguments;
  2351. let later = function () {
  2352. timeout = null;
  2353. later = null;
  2354. if (!immediate) {
  2355. func.apply(self, args);
  2356. }
  2357. };
  2358. if (!timeout && immediate) {
  2359. func.apply(self, args);
  2360. }
  2361. context.clearTimeout(timeout);
  2362. timeout = context.setTimeout(later, wait);
  2363. };
  2364. /* eslint-enable consistent-this */
  2365. debounced.cancel = cancel;
  2366. return debounced;
  2367. };
  2368. var Fn = /*#__PURE__*/Object.freeze({
  2369. __proto__: null,
  2370. UPDATE_REFRESH_INTERVAL: UPDATE_REFRESH_INTERVAL,
  2371. bind_: bind_,
  2372. throttle: throttle,
  2373. debounce: debounce
  2374. });
  2375. /**
  2376. * @file src/js/event-target.js
  2377. */
  2378. let EVENT_MAP;
  2379. /**
  2380. * `EventTarget` is a class that can have the same API as the DOM `EventTarget`. It
  2381. * adds shorthand functions that wrap around lengthy functions. For example:
  2382. * the `on` function is a wrapper around `addEventListener`.
  2383. *
  2384. * @see [EventTarget Spec]{@link https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget}
  2385. * @class EventTarget
  2386. */
  2387. class EventTarget {
  2388. /**
  2389. * Adds an `event listener` to an instance of an `EventTarget`. An `event listener` is a
  2390. * function that will get called when an event with a certain name gets triggered.
  2391. *
  2392. * @param {string|string[]} type
  2393. * An event name or an array of event names.
  2394. *
  2395. * @param {Function} fn
  2396. * The function to call with `EventTarget`s
  2397. */
  2398. on(type, fn) {
  2399. // Remove the addEventListener alias before calling Events.on
  2400. // so we don't get into an infinite type loop
  2401. const ael = this.addEventListener;
  2402. this.addEventListener = () => {};
  2403. on(this, type, fn);
  2404. this.addEventListener = ael;
  2405. }
  2406. /**
  2407. * Removes an `event listener` for a specific event from an instance of `EventTarget`.
  2408. * This makes it so that the `event listener` will no longer get called when the
  2409. * named event happens.
  2410. *
  2411. * @param {string|string[]} type
  2412. * An event name or an array of event names.
  2413. *
  2414. * @param {Function} fn
  2415. * The function to remove.
  2416. */
  2417. off(type, fn) {
  2418. off(this, type, fn);
  2419. }
  2420. /**
  2421. * This function will add an `event listener` that gets triggered only once. After the
  2422. * first trigger it will get removed. This is like adding an `event listener`
  2423. * with {@link EventTarget#on} that calls {@link EventTarget#off} on itself.
  2424. *
  2425. * @param {string|string[]} type
  2426. * An event name or an array of event names.
  2427. *
  2428. * @param {Function} fn
  2429. * The function to be called once for each event name.
  2430. */
  2431. one(type, fn) {
  2432. // Remove the addEventListener aliasing Events.on
  2433. // so we don't get into an infinite type loop
  2434. const ael = this.addEventListener;
  2435. this.addEventListener = () => {};
  2436. one(this, type, fn);
  2437. this.addEventListener = ael;
  2438. }
  2439. /**
  2440. * This function will add an `event listener` that gets triggered only once and is
  2441. * removed from all events. This is like adding an array of `event listener`s
  2442. * with {@link EventTarget#on} that calls {@link EventTarget#off} on all events the
  2443. * first time it is triggered.
  2444. *
  2445. * @param {string|string[]} type
  2446. * An event name or an array of event names.
  2447. *
  2448. * @param {Function} fn
  2449. * The function to be called once for each event name.
  2450. */
  2451. any(type, fn) {
  2452. // Remove the addEventListener aliasing Events.on
  2453. // so we don't get into an infinite type loop
  2454. const ael = this.addEventListener;
  2455. this.addEventListener = () => {};
  2456. any(this, type, fn);
  2457. this.addEventListener = ael;
  2458. }
  2459. /**
  2460. * This function causes an event to happen. This will then cause any `event listeners`
  2461. * that are waiting for that event, to get called. If there are no `event listeners`
  2462. * for an event then nothing will happen.
  2463. *
  2464. * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
  2465. * Trigger will also call the `on` + `uppercaseEventName` function.
  2466. *
  2467. * Example:
  2468. * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
  2469. * `onClick` if it exists.
  2470. *
  2471. * @param {string|EventTarget~Event|Object} event
  2472. * The name of the event, an `Event`, or an object with a key of type set to
  2473. * an event name.
  2474. */
  2475. trigger(event) {
  2476. const type = event.type || event;
  2477. // deprecation
  2478. // In a future version we should default target to `this`
  2479. // similar to how we default the target to `elem` in
  2480. // `Events.trigger`. Right now the default `target` will be
  2481. // `document` due to the `Event.fixEvent` call.
  2482. if (typeof event === 'string') {
  2483. event = {
  2484. type
  2485. };
  2486. }
  2487. event = fixEvent(event);
  2488. if (this.allowedEvents_[type] && this['on' + type]) {
  2489. this['on' + type](event);
  2490. }
  2491. trigger(this, event);
  2492. }
  2493. queueTrigger(event) {
  2494. // only set up EVENT_MAP if it'll be used
  2495. if (!EVENT_MAP) {
  2496. EVENT_MAP = new Map();
  2497. }
  2498. const type = event.type || event;
  2499. let map = EVENT_MAP.get(this);
  2500. if (!map) {
  2501. map = new Map();
  2502. EVENT_MAP.set(this, map);
  2503. }
  2504. const oldTimeout = map.get(type);
  2505. map.delete(type);
  2506. window.clearTimeout(oldTimeout);
  2507. const timeout = window.setTimeout(() => {
  2508. map.delete(type);
  2509. // if we cleared out all timeouts for the current target, delete its map
  2510. if (map.size === 0) {
  2511. map = null;
  2512. EVENT_MAP.delete(this);
  2513. }
  2514. this.trigger(event);
  2515. }, 0);
  2516. map.set(type, timeout);
  2517. }
  2518. }
  2519. /**
  2520. * A Custom DOM event.
  2521. *
  2522. * @typedef {CustomEvent} Event
  2523. * @see [Properties]{@link https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent}
  2524. */
  2525. /**
  2526. * All event listeners should follow the following format.
  2527. *
  2528. * @callback EventListener
  2529. * @this {EventTarget}
  2530. *
  2531. * @param {Event} event
  2532. * the event that triggered this function
  2533. *
  2534. * @param {Object} [hash]
  2535. * hash of data sent during the event
  2536. */
  2537. /**
  2538. * An object containing event names as keys and booleans as values.
  2539. *
  2540. * > NOTE: If an event name is set to a true value here {@link EventTarget#trigger}
  2541. * will have extra functionality. See that function for more information.
  2542. *
  2543. * @property EventTarget.prototype.allowedEvents_
  2544. * @protected
  2545. */
  2546. EventTarget.prototype.allowedEvents_ = {};
  2547. /**
  2548. * An alias of {@link EventTarget#on}. Allows `EventTarget` to mimic
  2549. * the standard DOM API.
  2550. *
  2551. * @function
  2552. * @see {@link EventTarget#on}
  2553. */
  2554. EventTarget.prototype.addEventListener = EventTarget.prototype.on;
  2555. /**
  2556. * An alias of {@link EventTarget#off}. Allows `EventTarget` to mimic
  2557. * the standard DOM API.
  2558. *
  2559. * @function
  2560. * @see {@link EventTarget#off}
  2561. */
  2562. EventTarget.prototype.removeEventListener = EventTarget.prototype.off;
  2563. /**
  2564. * An alias of {@link EventTarget#trigger}. Allows `EventTarget` to mimic
  2565. * the standard DOM API.
  2566. *
  2567. * @function
  2568. * @see {@link EventTarget#trigger}
  2569. */
  2570. EventTarget.prototype.dispatchEvent = EventTarget.prototype.trigger;
  2571. /**
  2572. * @file mixins/evented.js
  2573. * @module evented
  2574. */
  2575. const objName = obj => {
  2576. if (typeof obj.name === 'function') {
  2577. return obj.name();
  2578. }
  2579. if (typeof obj.name === 'string') {
  2580. return obj.name;
  2581. }
  2582. if (obj.name_) {
  2583. return obj.name_;
  2584. }
  2585. if (obj.constructor && obj.constructor.name) {
  2586. return obj.constructor.name;
  2587. }
  2588. return typeof obj;
  2589. };
  2590. /**
  2591. * Returns whether or not an object has had the evented mixin applied.
  2592. *
  2593. * @param {Object} object
  2594. * An object to test.
  2595. *
  2596. * @return {boolean}
  2597. * Whether or not the object appears to be evented.
  2598. */
  2599. const isEvented = object => object instanceof EventTarget || !!object.eventBusEl_ && ['on', 'one', 'off', 'trigger'].every(k => typeof object[k] === 'function');
  2600. /**
  2601. * Adds a callback to run after the evented mixin applied.
  2602. *
  2603. * @param {Object} target
  2604. * An object to Add
  2605. * @param {Function} callback
  2606. * The callback to run.
  2607. */
  2608. const addEventedCallback = (target, callback) => {
  2609. if (isEvented(target)) {
  2610. callback();
  2611. } else {
  2612. if (!target.eventedCallbacks) {
  2613. target.eventedCallbacks = [];
  2614. }
  2615. target.eventedCallbacks.push(callback);
  2616. }
  2617. };
  2618. /**
  2619. * Whether a value is a valid event type - non-empty string or array.
  2620. *
  2621. * @private
  2622. * @param {string|Array} type
  2623. * The type value to test.
  2624. *
  2625. * @return {boolean}
  2626. * Whether or not the type is a valid event type.
  2627. */
  2628. const isValidEventType = type =>
  2629. // The regex here verifies that the `type` contains at least one non-
  2630. // whitespace character.
  2631. typeof type === 'string' && /\S/.test(type) || Array.isArray(type) && !!type.length;
  2632. /**
  2633. * Validates a value to determine if it is a valid event target. Throws if not.
  2634. *
  2635. * @private
  2636. * @throws {Error}
  2637. * If the target does not appear to be a valid event target.
  2638. *
  2639. * @param {Object} target
  2640. * The object to test.
  2641. *
  2642. * @param {Object} obj
  2643. * The evented object we are validating for
  2644. *
  2645. * @param {string} fnName
  2646. * The name of the evented mixin function that called this.
  2647. */
  2648. const validateTarget = (target, obj, fnName) => {
  2649. if (!target || !target.nodeName && !isEvented(target)) {
  2650. throw new Error(`Invalid target for ${objName(obj)}#${fnName}; must be a DOM node or evented object.`);
  2651. }
  2652. };
  2653. /**
  2654. * Validates a value to determine if it is a valid event target. Throws if not.
  2655. *
  2656. * @private
  2657. * @throws {Error}
  2658. * If the type does not appear to be a valid event type.
  2659. *
  2660. * @param {string|Array} type
  2661. * The type to test.
  2662. *
  2663. * @param {Object} obj
  2664. * The evented object we are validating for
  2665. *
  2666. * @param {string} fnName
  2667. * The name of the evented mixin function that called this.
  2668. */
  2669. const validateEventType = (type, obj, fnName) => {
  2670. if (!isValidEventType(type)) {
  2671. throw new Error(`Invalid event type for ${objName(obj)}#${fnName}; must be a non-empty string or array.`);
  2672. }
  2673. };
  2674. /**
  2675. * Validates a value to determine if it is a valid listener. Throws if not.
  2676. *
  2677. * @private
  2678. * @throws {Error}
  2679. * If the listener is not a function.
  2680. *
  2681. * @param {Function} listener
  2682. * The listener to test.
  2683. *
  2684. * @param {Object} obj
  2685. * The evented object we are validating for
  2686. *
  2687. * @param {string} fnName
  2688. * The name of the evented mixin function that called this.
  2689. */
  2690. const validateListener = (listener, obj, fnName) => {
  2691. if (typeof listener !== 'function') {
  2692. throw new Error(`Invalid listener for ${objName(obj)}#${fnName}; must be a function.`);
  2693. }
  2694. };
  2695. /**
  2696. * Takes an array of arguments given to `on()` or `one()`, validates them, and
  2697. * normalizes them into an object.
  2698. *
  2699. * @private
  2700. * @param {Object} self
  2701. * The evented object on which `on()` or `one()` was called. This
  2702. * object will be bound as the `this` value for the listener.
  2703. *
  2704. * @param {Array} args
  2705. * An array of arguments passed to `on()` or `one()`.
  2706. *
  2707. * @param {string} fnName
  2708. * The name of the evented mixin function that called this.
  2709. *
  2710. * @return {Object}
  2711. * An object containing useful values for `on()` or `one()` calls.
  2712. */
  2713. const normalizeListenArgs = (self, args, fnName) => {
  2714. // If the number of arguments is less than 3, the target is always the
  2715. // evented object itself.
  2716. const isTargetingSelf = args.length < 3 || args[0] === self || args[0] === self.eventBusEl_;
  2717. let target;
  2718. let type;
  2719. let listener;
  2720. if (isTargetingSelf) {
  2721. target = self.eventBusEl_;
  2722. // Deal with cases where we got 3 arguments, but we are still listening to
  2723. // the evented object itself.
  2724. if (args.length >= 3) {
  2725. args.shift();
  2726. }
  2727. [type, listener] = args;
  2728. } else {
  2729. [target, type, listener] = args;
  2730. }
  2731. validateTarget(target, self, fnName);
  2732. validateEventType(type, self, fnName);
  2733. validateListener(listener, self, fnName);
  2734. listener = bind_(self, listener);
  2735. return {
  2736. isTargetingSelf,
  2737. target,
  2738. type,
  2739. listener
  2740. };
  2741. };
  2742. /**
  2743. * Adds the listener to the event type(s) on the target, normalizing for
  2744. * the type of target.
  2745. *
  2746. * @private
  2747. * @param {Element|Object} target
  2748. * A DOM node or evented object.
  2749. *
  2750. * @param {string} method
  2751. * The event binding method to use ("on" or "one").
  2752. *
  2753. * @param {string|Array} type
  2754. * One or more event type(s).
  2755. *
  2756. * @param {Function} listener
  2757. * A listener function.
  2758. */
  2759. const listen = (target, method, type, listener) => {
  2760. validateTarget(target, target, method);
  2761. if (target.nodeName) {
  2762. Events[method](target, type, listener);
  2763. } else {
  2764. target[method](type, listener);
  2765. }
  2766. };
  2767. /**
  2768. * Contains methods that provide event capabilities to an object which is passed
  2769. * to {@link module:evented|evented}.
  2770. *
  2771. * @mixin EventedMixin
  2772. */
  2773. const EventedMixin = {
  2774. /**
  2775. * Add a listener to an event (or events) on this object or another evented
  2776. * object.
  2777. *
  2778. * @param {string|Array|Element|Object} targetOrType
  2779. * If this is a string or array, it represents the event type(s)
  2780. * that will trigger the listener.
  2781. *
  2782. * Another evented object can be passed here instead, which will
  2783. * cause the listener to listen for events on _that_ object.
  2784. *
  2785. * In either case, the listener's `this` value will be bound to
  2786. * this object.
  2787. *
  2788. * @param {string|Array|Function} typeOrListener
  2789. * If the first argument was a string or array, this should be the
  2790. * listener function. Otherwise, this is a string or array of event
  2791. * type(s).
  2792. *
  2793. * @param {Function} [listener]
  2794. * If the first argument was another evented object, this will be
  2795. * the listener function.
  2796. */
  2797. on(...args) {
  2798. const {
  2799. isTargetingSelf,
  2800. target,
  2801. type,
  2802. listener
  2803. } = normalizeListenArgs(this, args, 'on');
  2804. listen(target, 'on', type, listener);
  2805. // If this object is listening to another evented object.
  2806. if (!isTargetingSelf) {
  2807. // If this object is disposed, remove the listener.
  2808. const removeListenerOnDispose = () => this.off(target, type, listener);
  2809. // Use the same function ID as the listener so we can remove it later it
  2810. // using the ID of the original listener.
  2811. removeListenerOnDispose.guid = listener.guid;
  2812. // Add a listener to the target's dispose event as well. This ensures
  2813. // that if the target is disposed BEFORE this object, we remove the
  2814. // removal listener that was just added. Otherwise, we create a memory leak.
  2815. const removeRemoverOnTargetDispose = () => this.off('dispose', removeListenerOnDispose);
  2816. // Use the same function ID as the listener so we can remove it later
  2817. // it using the ID of the original listener.
  2818. removeRemoverOnTargetDispose.guid = listener.guid;
  2819. listen(this, 'on', 'dispose', removeListenerOnDispose);
  2820. listen(target, 'on', 'dispose', removeRemoverOnTargetDispose);
  2821. }
  2822. },
  2823. /**
  2824. * Add a listener to an event (or events) on this object or another evented
  2825. * object. The listener will be called once per event and then removed.
  2826. *
  2827. * @param {string|Array|Element|Object} targetOrType
  2828. * If this is a string or array, it represents the event type(s)
  2829. * that will trigger the listener.
  2830. *
  2831. * Another evented object can be passed here instead, which will
  2832. * cause the listener to listen for events on _that_ object.
  2833. *
  2834. * In either case, the listener's `this` value will be bound to
  2835. * this object.
  2836. *
  2837. * @param {string|Array|Function} typeOrListener
  2838. * If the first argument was a string or array, this should be the
  2839. * listener function. Otherwise, this is a string or array of event
  2840. * type(s).
  2841. *
  2842. * @param {Function} [listener]
  2843. * If the first argument was another evented object, this will be
  2844. * the listener function.
  2845. */
  2846. one(...args) {
  2847. const {
  2848. isTargetingSelf,
  2849. target,
  2850. type,
  2851. listener
  2852. } = normalizeListenArgs(this, args, 'one');
  2853. // Targeting this evented object.
  2854. if (isTargetingSelf) {
  2855. listen(target, 'one', type, listener);
  2856. // Targeting another evented object.
  2857. } else {
  2858. // TODO: This wrapper is incorrect! It should only
  2859. // remove the wrapper for the event type that called it.
  2860. // Instead all listeners are removed on the first trigger!
  2861. // see https://github.com/videojs/video.js/issues/5962
  2862. const wrapper = (...largs) => {
  2863. this.off(target, type, wrapper);
  2864. listener.apply(null, largs);
  2865. };
  2866. // Use the same function ID as the listener so we can remove it later
  2867. // it using the ID of the original listener.
  2868. wrapper.guid = listener.guid;
  2869. listen(target, 'one', type, wrapper);
  2870. }
  2871. },
  2872. /**
  2873. * Add a listener to an event (or events) on this object or another evented
  2874. * object. The listener will only be called once for the first event that is triggered
  2875. * then removed.
  2876. *
  2877. * @param {string|Array|Element|Object} targetOrType
  2878. * If this is a string or array, it represents the event type(s)
  2879. * that will trigger the listener.
  2880. *
  2881. * Another evented object can be passed here instead, which will
  2882. * cause the listener to listen for events on _that_ object.
  2883. *
  2884. * In either case, the listener's `this` value will be bound to
  2885. * this object.
  2886. *
  2887. * @param {string|Array|Function} typeOrListener
  2888. * If the first argument was a string or array, this should be the
  2889. * listener function. Otherwise, this is a string or array of event
  2890. * type(s).
  2891. *
  2892. * @param {Function} [listener]
  2893. * If the first argument was another evented object, this will be
  2894. * the listener function.
  2895. */
  2896. any(...args) {
  2897. const {
  2898. isTargetingSelf,
  2899. target,
  2900. type,
  2901. listener
  2902. } = normalizeListenArgs(this, args, 'any');
  2903. // Targeting this evented object.
  2904. if (isTargetingSelf) {
  2905. listen(target, 'any', type, listener);
  2906. // Targeting another evented object.
  2907. } else {
  2908. const wrapper = (...largs) => {
  2909. this.off(target, type, wrapper);
  2910. listener.apply(null, largs);
  2911. };
  2912. // Use the same function ID as the listener so we can remove it later
  2913. // it using the ID of the original listener.
  2914. wrapper.guid = listener.guid;
  2915. listen(target, 'any', type, wrapper);
  2916. }
  2917. },
  2918. /**
  2919. * Removes listener(s) from event(s) on an evented object.
  2920. *
  2921. * @param {string|Array|Element|Object} [targetOrType]
  2922. * If this is a string or array, it represents the event type(s).
  2923. *
  2924. * Another evented object can be passed here instead, in which case
  2925. * ALL 3 arguments are _required_.
  2926. *
  2927. * @param {string|Array|Function} [typeOrListener]
  2928. * If the first argument was a string or array, this may be the
  2929. * listener function. Otherwise, this is a string or array of event
  2930. * type(s).
  2931. *
  2932. * @param {Function} [listener]
  2933. * If the first argument was another evented object, this will be
  2934. * the listener function; otherwise, _all_ listeners bound to the
  2935. * event type(s) will be removed.
  2936. */
  2937. off(targetOrType, typeOrListener, listener) {
  2938. // Targeting this evented object.
  2939. if (!targetOrType || isValidEventType(targetOrType)) {
  2940. off(this.eventBusEl_, targetOrType, typeOrListener);
  2941. // Targeting another evented object.
  2942. } else {
  2943. const target = targetOrType;
  2944. const type = typeOrListener;
  2945. // Fail fast and in a meaningful way!
  2946. validateTarget(target, this, 'off');
  2947. validateEventType(type, this, 'off');
  2948. validateListener(listener, this, 'off');
  2949. // Ensure there's at least a guid, even if the function hasn't been used
  2950. listener = bind_(this, listener);
  2951. // Remove the dispose listener on this evented object, which was given
  2952. // the same guid as the event listener in on().
  2953. this.off('dispose', listener);
  2954. if (target.nodeName) {
  2955. off(target, type, listener);
  2956. off(target, 'dispose', listener);
  2957. } else if (isEvented(target)) {
  2958. target.off(type, listener);
  2959. target.off('dispose', listener);
  2960. }
  2961. }
  2962. },
  2963. /**
  2964. * Fire an event on this evented object, causing its listeners to be called.
  2965. *
  2966. * @param {string|Object} event
  2967. * An event type or an object with a type property.
  2968. *
  2969. * @param {Object} [hash]
  2970. * An additional object to pass along to listeners.
  2971. *
  2972. * @return {boolean}
  2973. * Whether or not the default behavior was prevented.
  2974. */
  2975. trigger(event, hash) {
  2976. validateTarget(this.eventBusEl_, this, 'trigger');
  2977. const type = event && typeof event !== 'string' ? event.type : event;
  2978. if (!isValidEventType(type)) {
  2979. throw new Error(`Invalid event type for ${objName(this)}#trigger; ` + 'must be a non-empty string or object with a type key that has a non-empty value.');
  2980. }
  2981. return trigger(this.eventBusEl_, event, hash);
  2982. }
  2983. };
  2984. /**
  2985. * Applies {@link module:evented~EventedMixin|EventedMixin} to a target object.
  2986. *
  2987. * @param {Object} target
  2988. * The object to which to add event methods.
  2989. *
  2990. * @param {Object} [options={}]
  2991. * Options for customizing the mixin behavior.
  2992. *
  2993. * @param {string} [options.eventBusKey]
  2994. * By default, adds a `eventBusEl_` DOM element to the target object,
  2995. * which is used as an event bus. If the target object already has a
  2996. * DOM element that should be used, pass its key here.
  2997. *
  2998. * @return {Object}
  2999. * The target object.
  3000. */
  3001. function evented(target, options = {}) {
  3002. const {
  3003. eventBusKey
  3004. } = options;
  3005. // Set or create the eventBusEl_.
  3006. if (eventBusKey) {
  3007. if (!target[eventBusKey].nodeName) {
  3008. throw new Error(`The eventBusKey "${eventBusKey}" does not refer to an element.`);
  3009. }
  3010. target.eventBusEl_ = target[eventBusKey];
  3011. } else {
  3012. target.eventBusEl_ = createEl('span', {
  3013. className: 'vjs-event-bus'
  3014. });
  3015. }
  3016. Object.assign(target, EventedMixin);
  3017. if (target.eventedCallbacks) {
  3018. target.eventedCallbacks.forEach(callback => {
  3019. callback();
  3020. });
  3021. }
  3022. // When any evented object is disposed, it removes all its listeners.
  3023. target.on('dispose', () => {
  3024. target.off();
  3025. [target, target.el_, target.eventBusEl_].forEach(function (val) {
  3026. if (val && DomData.has(val)) {
  3027. DomData.delete(val);
  3028. }
  3029. });
  3030. window.setTimeout(() => {
  3031. target.eventBusEl_ = null;
  3032. }, 0);
  3033. });
  3034. return target;
  3035. }
  3036. /**
  3037. * @file mixins/stateful.js
  3038. * @module stateful
  3039. */
  3040. /**
  3041. * Contains methods that provide statefulness to an object which is passed
  3042. * to {@link module:stateful}.
  3043. *
  3044. * @mixin StatefulMixin
  3045. */
  3046. const StatefulMixin = {
  3047. /**
  3048. * A hash containing arbitrary keys and values representing the state of
  3049. * the object.
  3050. *
  3051. * @type {Object}
  3052. */
  3053. state: {},
  3054. /**
  3055. * Set the state of an object by mutating its
  3056. * {@link module:stateful~StatefulMixin.state|state} object in place.
  3057. *
  3058. * @fires module:stateful~StatefulMixin#statechanged
  3059. * @param {Object|Function} stateUpdates
  3060. * A new set of properties to shallow-merge into the plugin state.
  3061. * Can be a plain object or a function returning a plain object.
  3062. *
  3063. * @return {Object|undefined}
  3064. * An object containing changes that occurred. If no changes
  3065. * occurred, returns `undefined`.
  3066. */
  3067. setState(stateUpdates) {
  3068. // Support providing the `stateUpdates` state as a function.
  3069. if (typeof stateUpdates === 'function') {
  3070. stateUpdates = stateUpdates();
  3071. }
  3072. let changes;
  3073. each(stateUpdates, (value, key) => {
  3074. // Record the change if the value is different from what's in the
  3075. // current state.
  3076. if (this.state[key] !== value) {
  3077. changes = changes || {};
  3078. changes[key] = {
  3079. from: this.state[key],
  3080. to: value
  3081. };
  3082. }
  3083. this.state[key] = value;
  3084. });
  3085. // Only trigger "statechange" if there were changes AND we have a trigger
  3086. // function. This allows us to not require that the target object be an
  3087. // evented object.
  3088. if (changes && isEvented(this)) {
  3089. /**
  3090. * An event triggered on an object that is both
  3091. * {@link module:stateful|stateful} and {@link module:evented|evented}
  3092. * indicating that its state has changed.
  3093. *
  3094. * @event module:stateful~StatefulMixin#statechanged
  3095. * @type {Object}
  3096. * @property {Object} changes
  3097. * A hash containing the properties that were changed and
  3098. * the values they were changed `from` and `to`.
  3099. */
  3100. this.trigger({
  3101. changes,
  3102. type: 'statechanged'
  3103. });
  3104. }
  3105. return changes;
  3106. }
  3107. };
  3108. /**
  3109. * Applies {@link module:stateful~StatefulMixin|StatefulMixin} to a target
  3110. * object.
  3111. *
  3112. * If the target object is {@link module:evented|evented} and has a
  3113. * `handleStateChanged` method, that method will be automatically bound to the
  3114. * `statechanged` event on itself.
  3115. *
  3116. * @param {Object} target
  3117. * The object to be made stateful.
  3118. *
  3119. * @param {Object} [defaultState]
  3120. * A default set of properties to populate the newly-stateful object's
  3121. * `state` property.
  3122. *
  3123. * @return {Object}
  3124. * Returns the `target`.
  3125. */
  3126. function stateful(target, defaultState) {
  3127. Object.assign(target, StatefulMixin);
  3128. // This happens after the mixing-in because we need to replace the `state`
  3129. // added in that step.
  3130. target.state = Object.assign({}, target.state, defaultState);
  3131. // Auto-bind the `handleStateChanged` method of the target object if it exists.
  3132. if (typeof target.handleStateChanged === 'function' && isEvented(target)) {
  3133. target.on('statechanged', target.handleStateChanged);
  3134. }
  3135. return target;
  3136. }
  3137. /**
  3138. * @file str.js
  3139. * @module to-lower-case
  3140. */
  3141. /**
  3142. * Lowercase the first letter of a string.
  3143. *
  3144. * @param {string} string
  3145. * String to be lowercased
  3146. *
  3147. * @return {string}
  3148. * The string with a lowercased first letter
  3149. */
  3150. const toLowerCase = function (string) {
  3151. if (typeof string !== 'string') {
  3152. return string;
  3153. }
  3154. return string.replace(/./, w => w.toLowerCase());
  3155. };
  3156. /**
  3157. * Uppercase the first letter of a string.
  3158. *
  3159. * @param {string} string
  3160. * String to be uppercased
  3161. *
  3162. * @return {string}
  3163. * The string with an uppercased first letter
  3164. */
  3165. const toTitleCase = function (string) {
  3166. if (typeof string !== 'string') {
  3167. return string;
  3168. }
  3169. return string.replace(/./, w => w.toUpperCase());
  3170. };
  3171. /**
  3172. * Compares the TitleCase versions of the two strings for equality.
  3173. *
  3174. * @param {string} str1
  3175. * The first string to compare
  3176. *
  3177. * @param {string} str2
  3178. * The second string to compare
  3179. *
  3180. * @return {boolean}
  3181. * Whether the TitleCase versions of the strings are equal
  3182. */
  3183. const titleCaseEquals = function (str1, str2) {
  3184. return toTitleCase(str1) === toTitleCase(str2);
  3185. };
  3186. var Str = /*#__PURE__*/Object.freeze({
  3187. __proto__: null,
  3188. toLowerCase: toLowerCase,
  3189. toTitleCase: toTitleCase,
  3190. titleCaseEquals: titleCaseEquals
  3191. });
  3192. /**
  3193. * Player Component - Base class for all UI objects
  3194. *
  3195. * @file component.js
  3196. */
  3197. /**
  3198. * Base class for all UI Components.
  3199. * Components are UI objects which represent both a javascript object and an element
  3200. * in the DOM. They can be children of other components, and can have
  3201. * children themselves.
  3202. *
  3203. * Components can also use methods from {@link EventTarget}
  3204. */
  3205. class Component {
  3206. /**
  3207. * A callback that is called when a component is ready. Does not have any
  3208. * parameters and any callback value will be ignored.
  3209. *
  3210. * @callback ReadyCallback
  3211. * @this Component
  3212. */
  3213. /**
  3214. * Creates an instance of this class.
  3215. *
  3216. * @param { import('./player').default } player
  3217. * The `Player` that this class should be attached to.
  3218. *
  3219. * @param {Object} [options]
  3220. * The key/value store of component options.
  3221. *
  3222. * @param {Object[]} [options.children]
  3223. * An array of children objects to initialize this component with. Children objects have
  3224. * a name property that will be used if more than one component of the same type needs to be
  3225. * added.
  3226. *
  3227. * @param {string} [options.className]
  3228. * A class or space separated list of classes to add the component
  3229. *
  3230. * @param {ReadyCallback} [ready]
  3231. * Function that gets called when the `Component` is ready.
  3232. */
  3233. constructor(player, options, ready) {
  3234. // The component might be the player itself and we can't pass `this` to super
  3235. if (!player && this.play) {
  3236. this.player_ = player = this; // eslint-disable-line
  3237. } else {
  3238. this.player_ = player;
  3239. }
  3240. this.isDisposed_ = false;
  3241. // Hold the reference to the parent component via `addChild` method
  3242. this.parentComponent_ = null;
  3243. // Make a copy of prototype.options_ to protect against overriding defaults
  3244. this.options_ = merge({}, this.options_);
  3245. // Updated options with supplied options
  3246. options = this.options_ = merge(this.options_, options);
  3247. // Get ID from options or options element if one is supplied
  3248. this.id_ = options.id || options.el && options.el.id;
  3249. // If there was no ID from the options, generate one
  3250. if (!this.id_) {
  3251. // Don't require the player ID function in the case of mock players
  3252. const id = player && player.id && player.id() || 'no_player';
  3253. this.id_ = `${id}_component_${newGUID()}`;
  3254. }
  3255. this.name_ = options.name || null;
  3256. // Create element if one wasn't provided in options
  3257. if (options.el) {
  3258. this.el_ = options.el;
  3259. } else if (options.createEl !== false) {
  3260. this.el_ = this.createEl();
  3261. }
  3262. if (options.className && this.el_) {
  3263. options.className.split(' ').forEach(c => this.addClass(c));
  3264. }
  3265. // Remove the placeholder event methods. If the component is evented, the
  3266. // real methods are added next
  3267. ['on', 'off', 'one', 'any', 'trigger'].forEach(fn => {
  3268. this[fn] = undefined;
  3269. });
  3270. // if evented is anything except false, we want to mixin in evented
  3271. if (options.evented !== false) {
  3272. // Make this an evented object and use `el_`, if available, as its event bus
  3273. evented(this, {
  3274. eventBusKey: this.el_ ? 'el_' : null
  3275. });
  3276. this.handleLanguagechange = this.handleLanguagechange.bind(this);
  3277. this.on(this.player_, 'languagechange', this.handleLanguagechange);
  3278. }
  3279. stateful(this, this.constructor.defaultState);
  3280. this.children_ = [];
  3281. this.childIndex_ = {};
  3282. this.childNameIndex_ = {};
  3283. this.setTimeoutIds_ = new Set();
  3284. this.setIntervalIds_ = new Set();
  3285. this.rafIds_ = new Set();
  3286. this.namedRafs_ = new Map();
  3287. this.clearingTimersOnDispose_ = false;
  3288. // Add any child components in options
  3289. if (options.initChildren !== false) {
  3290. this.initChildren();
  3291. }
  3292. // Don't want to trigger ready here or it will go before init is actually
  3293. // finished for all children that run this constructor
  3294. this.ready(ready);
  3295. if (options.reportTouchActivity !== false) {
  3296. this.enableTouchActivity();
  3297. }
  3298. }
  3299. // `on`, `off`, `one`, `any` and `trigger` are here so tsc includes them in definitions.
  3300. // They are replaced or removed in the constructor
  3301. /**
  3302. * Adds an `event listener` to an instance of an `EventTarget`. An `event listener` is a
  3303. * function that will get called when an event with a certain name gets triggered.
  3304. *
  3305. * @param {string|string[]} type
  3306. * An event name or an array of event names.
  3307. *
  3308. * @param {Function} fn
  3309. * The function to call with `EventTarget`s
  3310. */
  3311. on(type, fn) {}
  3312. /**
  3313. * Removes an `event listener` for a specific event from an instance of `EventTarget`.
  3314. * This makes it so that the `event listener` will no longer get called when the
  3315. * named event happens.
  3316. *
  3317. * @param {string|string[]} type
  3318. * An event name or an array of event names.
  3319. *
  3320. * @param {Function} [fn]
  3321. * The function to remove. If not specified, all listeners managed by Video.js will be removed.
  3322. */
  3323. off(type, fn) {}
  3324. /**
  3325. * This function will add an `event listener` that gets triggered only once. After the
  3326. * first trigger it will get removed. This is like adding an `event listener`
  3327. * with {@link EventTarget#on} that calls {@link EventTarget#off} on itself.
  3328. *
  3329. * @param {string|string[]} type
  3330. * An event name or an array of event names.
  3331. *
  3332. * @param {Function} fn
  3333. * The function to be called once for each event name.
  3334. */
  3335. one(type, fn) {}
  3336. /**
  3337. * This function will add an `event listener` that gets triggered only once and is
  3338. * removed from all events. This is like adding an array of `event listener`s
  3339. * with {@link EventTarget#on} that calls {@link EventTarget#off} on all events the
  3340. * first time it is triggered.
  3341. *
  3342. * @param {string|string[]} type
  3343. * An event name or an array of event names.
  3344. *
  3345. * @param {Function} fn
  3346. * The function to be called once for each event name.
  3347. */
  3348. any(type, fn) {}
  3349. /**
  3350. * This function causes an event to happen. This will then cause any `event listeners`
  3351. * that are waiting for that event, to get called. If there are no `event listeners`
  3352. * for an event then nothing will happen.
  3353. *
  3354. * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
  3355. * Trigger will also call the `on` + `uppercaseEventName` function.
  3356. *
  3357. * Example:
  3358. * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
  3359. * `onClick` if it exists.
  3360. *
  3361. * @param {string|Event|Object} event
  3362. * The name of the event, an `Event`, or an object with a key of type set to
  3363. * an event name.
  3364. *
  3365. * @param {Object} [hash]
  3366. * Optionally extra argument to pass through to an event listener
  3367. */
  3368. trigger(event, hash) {}
  3369. /**
  3370. * Dispose of the `Component` and all child components.
  3371. *
  3372. * @fires Component#dispose
  3373. *
  3374. * @param {Object} options
  3375. * @param {Element} options.originalEl element with which to replace player element
  3376. */
  3377. dispose(options = {}) {
  3378. // Bail out if the component has already been disposed.
  3379. if (this.isDisposed_) {
  3380. return;
  3381. }
  3382. if (this.readyQueue_) {
  3383. this.readyQueue_.length = 0;
  3384. }
  3385. /**
  3386. * Triggered when a `Component` is disposed.
  3387. *
  3388. * @event Component#dispose
  3389. * @type {Event}
  3390. *
  3391. * @property {boolean} [bubbles=false]
  3392. * set to false so that the dispose event does not
  3393. * bubble up
  3394. */
  3395. this.trigger({
  3396. type: 'dispose',
  3397. bubbles: false
  3398. });
  3399. this.isDisposed_ = true;
  3400. // Dispose all children.
  3401. if (this.children_) {
  3402. for (let i = this.children_.length - 1; i >= 0; i--) {
  3403. if (this.children_[i].dispose) {
  3404. this.children_[i].dispose();
  3405. }
  3406. }
  3407. }
  3408. // Delete child references
  3409. this.children_ = null;
  3410. this.childIndex_ = null;
  3411. this.childNameIndex_ = null;
  3412. this.parentComponent_ = null;
  3413. if (this.el_) {
  3414. // Remove element from DOM
  3415. if (this.el_.parentNode) {
  3416. if (options.restoreEl) {
  3417. this.el_.parentNode.replaceChild(options.restoreEl, this.el_);
  3418. } else {
  3419. this.el_.parentNode.removeChild(this.el_);
  3420. }
  3421. }
  3422. this.el_ = null;
  3423. }
  3424. // remove reference to the player after disposing of the element
  3425. this.player_ = null;
  3426. }
  3427. /**
  3428. * Determine whether or not this component has been disposed.
  3429. *
  3430. * @return {boolean}
  3431. * If the component has been disposed, will be `true`. Otherwise, `false`.
  3432. */
  3433. isDisposed() {
  3434. return Boolean(this.isDisposed_);
  3435. }
  3436. /**
  3437. * Return the {@link Player} that the `Component` has attached to.
  3438. *
  3439. * @return { import('./player').default }
  3440. * The player that this `Component` has attached to.
  3441. */
  3442. player() {
  3443. return this.player_;
  3444. }
  3445. /**
  3446. * Deep merge of options objects with new options.
  3447. * > Note: When both `obj` and `options` contain properties whose values are objects.
  3448. * The two properties get merged using {@link module:obj.merge}
  3449. *
  3450. * @param {Object} obj
  3451. * The object that contains new options.
  3452. *
  3453. * @return {Object}
  3454. * A new object of `this.options_` and `obj` merged together.
  3455. */
  3456. options(obj) {
  3457. if (!obj) {
  3458. return this.options_;
  3459. }
  3460. this.options_ = merge(this.options_, obj);
  3461. return this.options_;
  3462. }
  3463. /**
  3464. * Get the `Component`s DOM element
  3465. *
  3466. * @return {Element}
  3467. * The DOM element for this `Component`.
  3468. */
  3469. el() {
  3470. return this.el_;
  3471. }
  3472. /**
  3473. * Create the `Component`s DOM element.
  3474. *
  3475. * @param {string} [tagName]
  3476. * Element's DOM node type. e.g. 'div'
  3477. *
  3478. * @param {Object} [properties]
  3479. * An object of properties that should be set.
  3480. *
  3481. * @param {Object} [attributes]
  3482. * An object of attributes that should be set.
  3483. *
  3484. * @return {Element}
  3485. * The element that gets created.
  3486. */
  3487. createEl(tagName, properties, attributes) {
  3488. return createEl(tagName, properties, attributes);
  3489. }
  3490. /**
  3491. * Localize a string given the string in english.
  3492. *
  3493. * If tokens are provided, it'll try and run a simple token replacement on the provided string.
  3494. * The tokens it looks for look like `{1}` with the index being 1-indexed into the tokens array.
  3495. *
  3496. * If a `defaultValue` is provided, it'll use that over `string`,
  3497. * if a value isn't found in provided language files.
  3498. * This is useful if you want to have a descriptive key for token replacement
  3499. * but have a succinct localized string and not require `en.json` to be included.
  3500. *
  3501. * Currently, it is used for the progress bar timing.
  3502. * ```js
  3503. * {
  3504. * "progress bar timing: currentTime={1} duration={2}": "{1} of {2}"
  3505. * }
  3506. * ```
  3507. * It is then used like so:
  3508. * ```js
  3509. * this.localize('progress bar timing: currentTime={1} duration{2}',
  3510. * [this.player_.currentTime(), this.player_.duration()],
  3511. * '{1} of {2}');
  3512. * ```
  3513. *
  3514. * Which outputs something like: `01:23 of 24:56`.
  3515. *
  3516. *
  3517. * @param {string} string
  3518. * The string to localize and the key to lookup in the language files.
  3519. * @param {string[]} [tokens]
  3520. * If the current item has token replacements, provide the tokens here.
  3521. * @param {string} [defaultValue]
  3522. * Defaults to `string`. Can be a default value to use for token replacement
  3523. * if the lookup key is needed to be separate.
  3524. *
  3525. * @return {string}
  3526. * The localized string or if no localization exists the english string.
  3527. */
  3528. localize(string, tokens, defaultValue = string) {
  3529. const code = this.player_.language && this.player_.language();
  3530. const languages = this.player_.languages && this.player_.languages();
  3531. const language = languages && languages[code];
  3532. const primaryCode = code && code.split('-')[0];
  3533. const primaryLang = languages && languages[primaryCode];
  3534. let localizedString = defaultValue;
  3535. if (language && language[string]) {
  3536. localizedString = language[string];
  3537. } else if (primaryLang && primaryLang[string]) {
  3538. localizedString = primaryLang[string];
  3539. }
  3540. if (tokens) {
  3541. localizedString = localizedString.replace(/\{(\d+)\}/g, function (match, index) {
  3542. const value = tokens[index - 1];
  3543. let ret = value;
  3544. if (typeof value === 'undefined') {
  3545. ret = match;
  3546. }
  3547. return ret;
  3548. });
  3549. }
  3550. return localizedString;
  3551. }
  3552. /**
  3553. * Handles language change for the player in components. Should be overridden by sub-components.
  3554. *
  3555. * @abstract
  3556. */
  3557. handleLanguagechange() {}
  3558. /**
  3559. * Return the `Component`s DOM element. This is where children get inserted.
  3560. * This will usually be the the same as the element returned in {@link Component#el}.
  3561. *
  3562. * @return {Element}
  3563. * The content element for this `Component`.
  3564. */
  3565. contentEl() {
  3566. return this.contentEl_ || this.el_;
  3567. }
  3568. /**
  3569. * Get this `Component`s ID
  3570. *
  3571. * @return {string}
  3572. * The id of this `Component`
  3573. */
  3574. id() {
  3575. return this.id_;
  3576. }
  3577. /**
  3578. * Get the `Component`s name. The name gets used to reference the `Component`
  3579. * and is set during registration.
  3580. *
  3581. * @return {string}
  3582. * The name of this `Component`.
  3583. */
  3584. name() {
  3585. return this.name_;
  3586. }
  3587. /**
  3588. * Get an array of all child components
  3589. *
  3590. * @return {Array}
  3591. * The children
  3592. */
  3593. children() {
  3594. return this.children_;
  3595. }
  3596. /**
  3597. * Returns the child `Component` with the given `id`.
  3598. *
  3599. * @param {string} id
  3600. * The id of the child `Component` to get.
  3601. *
  3602. * @return {Component|undefined}
  3603. * The child `Component` with the given `id` or undefined.
  3604. */
  3605. getChildById(id) {
  3606. return this.childIndex_[id];
  3607. }
  3608. /**
  3609. * Returns the child `Component` with the given `name`.
  3610. *
  3611. * @param {string} name
  3612. * The name of the child `Component` to get.
  3613. *
  3614. * @return {Component|undefined}
  3615. * The child `Component` with the given `name` or undefined.
  3616. */
  3617. getChild(name) {
  3618. if (!name) {
  3619. return;
  3620. }
  3621. return this.childNameIndex_[name];
  3622. }
  3623. /**
  3624. * Returns the descendant `Component` following the givent
  3625. * descendant `names`. For instance ['foo', 'bar', 'baz'] would
  3626. * try to get 'foo' on the current component, 'bar' on the 'foo'
  3627. * component and 'baz' on the 'bar' component and return undefined
  3628. * if any of those don't exist.
  3629. *
  3630. * @param {...string[]|...string} names
  3631. * The name of the child `Component` to get.
  3632. *
  3633. * @return {Component|undefined}
  3634. * The descendant `Component` following the given descendant
  3635. * `names` or undefined.
  3636. */
  3637. getDescendant(...names) {
  3638. // flatten array argument into the main array
  3639. names = names.reduce((acc, n) => acc.concat(n), []);
  3640. let currentChild = this;
  3641. for (let i = 0; i < names.length; i++) {
  3642. currentChild = currentChild.getChild(names[i]);
  3643. if (!currentChild || !currentChild.getChild) {
  3644. return;
  3645. }
  3646. }
  3647. return currentChild;
  3648. }
  3649. /**
  3650. * Adds an SVG icon element to another element or component.
  3651. *
  3652. * @param {string} iconName
  3653. * The name of icon. A list of all the icon names can be found at 'sandbox/svg-icons.html'
  3654. *
  3655. * @param {Element} [el=this.el()]
  3656. * Element to set the title on. Defaults to the current Component's element.
  3657. *
  3658. * @return {Element}
  3659. * The newly created icon element.
  3660. */
  3661. setIcon(iconName, el = this.el()) {
  3662. // TODO: In v9 of video.js, we will want to remove font icons entirely.
  3663. // This means this check, as well as the others throughout the code, and
  3664. // the unecessary CSS for font icons, will need to be removed.
  3665. // See https://github.com/videojs/video.js/pull/8260 as to which components
  3666. // need updating.
  3667. if (!this.player_.options_.experimentalSvgIcons) {
  3668. return;
  3669. }
  3670. const xmlnsURL = 'http://www.w3.org/2000/svg';
  3671. // The below creates an element in the format of:
  3672. // <span><svg><use>....</use></svg></span>
  3673. const iconContainer = createEl('span', {
  3674. className: 'vjs-icon-placeholder vjs-svg-icon'
  3675. }, {
  3676. 'aria-hidden': 'true'
  3677. });
  3678. const svgEl = document.createElementNS(xmlnsURL, 'svg');
  3679. svgEl.setAttributeNS(null, 'viewBox', '0 0 512 512');
  3680. const useEl = document.createElementNS(xmlnsURL, 'use');
  3681. svgEl.appendChild(useEl);
  3682. useEl.setAttributeNS(null, 'href', `#vjs-icon-${iconName}`);
  3683. iconContainer.appendChild(svgEl);
  3684. // Replace a pre-existing icon if one exists.
  3685. if (this.iconIsSet_) {
  3686. el.replaceChild(iconContainer, el.querySelector('.vjs-icon-placeholder'));
  3687. } else {
  3688. el.appendChild(iconContainer);
  3689. }
  3690. this.iconIsSet_ = true;
  3691. return iconContainer;
  3692. }
  3693. /**
  3694. * Add a child `Component` inside the current `Component`.
  3695. *
  3696. * @param {string|Component} child
  3697. * The name or instance of a child to add.
  3698. *
  3699. * @param {Object} [options={}]
  3700. * The key/value store of options that will get passed to children of
  3701. * the child.
  3702. *
  3703. * @param {number} [index=this.children_.length]
  3704. * The index to attempt to add a child into.
  3705. *
  3706. *
  3707. * @return {Component}
  3708. * The `Component` that gets added as a child. When using a string the
  3709. * `Component` will get created by this process.
  3710. */
  3711. addChild(child, options = {}, index = this.children_.length) {
  3712. let component;
  3713. let componentName;
  3714. // If child is a string, create component with options
  3715. if (typeof child === 'string') {
  3716. componentName = toTitleCase(child);
  3717. const componentClassName = options.componentClass || componentName;
  3718. // Set name through options
  3719. options.name = componentName;
  3720. // Create a new object & element for this controls set
  3721. // If there's no .player_, this is a player
  3722. const ComponentClass = Component.getComponent(componentClassName);
  3723. if (!ComponentClass) {
  3724. throw new Error(`Component ${componentClassName} does not exist`);
  3725. }
  3726. // data stored directly on the videojs object may be
  3727. // misidentified as a component to retain
  3728. // backwards-compatibility with 4.x. check to make sure the
  3729. // component class can be instantiated.
  3730. if (typeof ComponentClass !== 'function') {
  3731. return null;
  3732. }
  3733. component = new ComponentClass(this.player_ || this, options);
  3734. // child is a component instance
  3735. } else {
  3736. component = child;
  3737. }
  3738. if (component.parentComponent_) {
  3739. component.parentComponent_.removeChild(component);
  3740. }
  3741. this.children_.splice(index, 0, component);
  3742. component.parentComponent_ = this;
  3743. if (typeof component.id === 'function') {
  3744. this.childIndex_[component.id()] = component;
  3745. }
  3746. // If a name wasn't used to create the component, check if we can use the
  3747. // name function of the component
  3748. componentName = componentName || component.name && toTitleCase(component.name());
  3749. if (componentName) {
  3750. this.childNameIndex_[componentName] = component;
  3751. this.childNameIndex_[toLowerCase(componentName)] = component;
  3752. }
  3753. // Add the UI object's element to the container div (box)
  3754. // Having an element is not required
  3755. if (typeof component.el === 'function' && component.el()) {
  3756. // If inserting before a component, insert before that component's element
  3757. let refNode = null;
  3758. if (this.children_[index + 1]) {
  3759. // Most children are components, but the video tech is an HTML element
  3760. if (this.children_[index + 1].el_) {
  3761. refNode = this.children_[index + 1].el_;
  3762. } else if (isEl(this.children_[index + 1])) {
  3763. refNode = this.children_[index + 1];
  3764. }
  3765. }
  3766. this.contentEl().insertBefore(component.el(), refNode);
  3767. }
  3768. // Return so it can stored on parent object if desired.
  3769. return component;
  3770. }
  3771. /**
  3772. * Remove a child `Component` from this `Component`s list of children. Also removes
  3773. * the child `Component`s element from this `Component`s element.
  3774. *
  3775. * @param {Component} component
  3776. * The child `Component` to remove.
  3777. */
  3778. removeChild(component) {
  3779. if (typeof component === 'string') {
  3780. component = this.getChild(component);
  3781. }
  3782. if (!component || !this.children_) {
  3783. return;
  3784. }
  3785. let childFound = false;
  3786. for (let i = this.children_.length - 1; i >= 0; i--) {
  3787. if (this.children_[i] === component) {
  3788. childFound = true;
  3789. this.children_.splice(i, 1);
  3790. break;
  3791. }
  3792. }
  3793. if (!childFound) {
  3794. return;
  3795. }
  3796. component.parentComponent_ = null;
  3797. this.childIndex_[component.id()] = null;
  3798. this.childNameIndex_[toTitleCase(component.name())] = null;
  3799. this.childNameIndex_[toLowerCase(component.name())] = null;
  3800. const compEl = component.el();
  3801. if (compEl && compEl.parentNode === this.contentEl()) {
  3802. this.contentEl().removeChild(component.el());
  3803. }
  3804. }
  3805. /**
  3806. * Add and initialize default child `Component`s based upon options.
  3807. */
  3808. initChildren() {
  3809. const children = this.options_.children;
  3810. if (children) {
  3811. // `this` is `parent`
  3812. const parentOptions = this.options_;
  3813. const handleAdd = child => {
  3814. const name = child.name;
  3815. let opts = child.opts;
  3816. // Allow options for children to be set at the parent options
  3817. // e.g. videojs(id, { controlBar: false });
  3818. // instead of videojs(id, { children: { controlBar: false });
  3819. if (parentOptions[name] !== undefined) {
  3820. opts = parentOptions[name];
  3821. }
  3822. // Allow for disabling default components
  3823. // e.g. options['children']['posterImage'] = false
  3824. if (opts === false) {
  3825. return;
  3826. }
  3827. // Allow options to be passed as a simple boolean if no configuration
  3828. // is necessary.
  3829. if (opts === true) {
  3830. opts = {};
  3831. }
  3832. // We also want to pass the original player options
  3833. // to each component as well so they don't need to
  3834. // reach back into the player for options later.
  3835. opts.playerOptions = this.options_.playerOptions;
  3836. // Create and add the child component.
  3837. // Add a direct reference to the child by name on the parent instance.
  3838. // If two of the same component are used, different names should be supplied
  3839. // for each
  3840. const newChild = this.addChild(name, opts);
  3841. if (newChild) {
  3842. this[name] = newChild;
  3843. }
  3844. };
  3845. // Allow for an array of children details to passed in the options
  3846. let workingChildren;
  3847. const Tech = Component.getComponent('Tech');
  3848. if (Array.isArray(children)) {
  3849. workingChildren = children;
  3850. } else {
  3851. workingChildren = Object.keys(children);
  3852. }
  3853. workingChildren
  3854. // children that are in this.options_ but also in workingChildren would
  3855. // give us extra children we do not want. So, we want to filter them out.
  3856. .concat(Object.keys(this.options_).filter(function (child) {
  3857. return !workingChildren.some(function (wchild) {
  3858. if (typeof wchild === 'string') {
  3859. return child === wchild;
  3860. }
  3861. return child === wchild.name;
  3862. });
  3863. })).map(child => {
  3864. let name;
  3865. let opts;
  3866. if (typeof child === 'string') {
  3867. name = child;
  3868. opts = children[name] || this.options_[name] || {};
  3869. } else {
  3870. name = child.name;
  3871. opts = child;
  3872. }
  3873. return {
  3874. name,
  3875. opts
  3876. };
  3877. }).filter(child => {
  3878. // we have to make sure that child.name isn't in the techOrder since
  3879. // techs are registered as Components but can't aren't compatible
  3880. // See https://github.com/videojs/video.js/issues/2772
  3881. const c = Component.getComponent(child.opts.componentClass || toTitleCase(child.name));
  3882. return c && !Tech.isTech(c);
  3883. }).forEach(handleAdd);
  3884. }
  3885. }
  3886. /**
  3887. * Builds the default DOM class name. Should be overridden by sub-components.
  3888. *
  3889. * @return {string}
  3890. * The DOM class name for this object.
  3891. *
  3892. * @abstract
  3893. */
  3894. buildCSSClass() {
  3895. // Child classes can include a function that does:
  3896. // return 'CLASS NAME' + this._super();
  3897. return '';
  3898. }
  3899. /**
  3900. * Bind a listener to the component's ready state.
  3901. * Different from event listeners in that if the ready event has already happened
  3902. * it will trigger the function immediately.
  3903. *
  3904. * @param {ReadyCallback} fn
  3905. * Function that gets called when the `Component` is ready.
  3906. *
  3907. * @return {Component}
  3908. * Returns itself; method can be chained.
  3909. */
  3910. ready(fn, sync = false) {
  3911. if (!fn) {
  3912. return;
  3913. }
  3914. if (!this.isReady_) {
  3915. this.readyQueue_ = this.readyQueue_ || [];
  3916. this.readyQueue_.push(fn);
  3917. return;
  3918. }
  3919. if (sync) {
  3920. fn.call(this);
  3921. } else {
  3922. // Call the function asynchronously by default for consistency
  3923. this.setTimeout(fn, 1);
  3924. }
  3925. }
  3926. /**
  3927. * Trigger all the ready listeners for this `Component`.
  3928. *
  3929. * @fires Component#ready
  3930. */
  3931. triggerReady() {
  3932. this.isReady_ = true;
  3933. // Ensure ready is triggered asynchronously
  3934. this.setTimeout(function () {
  3935. const readyQueue = this.readyQueue_;
  3936. // Reset Ready Queue
  3937. this.readyQueue_ = [];
  3938. if (readyQueue && readyQueue.length > 0) {
  3939. readyQueue.forEach(function (fn) {
  3940. fn.call(this);
  3941. }, this);
  3942. }
  3943. // Allow for using event listeners also
  3944. /**
  3945. * Triggered when a `Component` is ready.
  3946. *
  3947. * @event Component#ready
  3948. * @type {Event}
  3949. */
  3950. this.trigger('ready');
  3951. }, 1);
  3952. }
  3953. /**
  3954. * Find a single DOM element matching a `selector`. This can be within the `Component`s
  3955. * `contentEl()` or another custom context.
  3956. *
  3957. * @param {string} selector
  3958. * A valid CSS selector, which will be passed to `querySelector`.
  3959. *
  3960. * @param {Element|string} [context=this.contentEl()]
  3961. * A DOM element within which to query. Can also be a selector string in
  3962. * which case the first matching element will get used as context. If
  3963. * missing `this.contentEl()` gets used. If `this.contentEl()` returns
  3964. * nothing it falls back to `document`.
  3965. *
  3966. * @return {Element|null}
  3967. * the dom element that was found, or null
  3968. *
  3969. * @see [Information on CSS Selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors)
  3970. */
  3971. $(selector, context) {
  3972. return $(selector, context || this.contentEl());
  3973. }
  3974. /**
  3975. * Finds all DOM element matching a `selector`. This can be within the `Component`s
  3976. * `contentEl()` or another custom context.
  3977. *
  3978. * @param {string} selector
  3979. * A valid CSS selector, which will be passed to `querySelectorAll`.
  3980. *
  3981. * @param {Element|string} [context=this.contentEl()]
  3982. * A DOM element within which to query. Can also be a selector string in
  3983. * which case the first matching element will get used as context. If
  3984. * missing `this.contentEl()` gets used. If `this.contentEl()` returns
  3985. * nothing it falls back to `document`.
  3986. *
  3987. * @return {NodeList}
  3988. * a list of dom elements that were found
  3989. *
  3990. * @see [Information on CSS Selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors)
  3991. */
  3992. $$(selector, context) {
  3993. return $$(selector, context || this.contentEl());
  3994. }
  3995. /**
  3996. * Check if a component's element has a CSS class name.
  3997. *
  3998. * @param {string} classToCheck
  3999. * CSS class name to check.
  4000. *
  4001. * @return {boolean}
  4002. * - True if the `Component` has the class.
  4003. * - False if the `Component` does not have the class`
  4004. */
  4005. hasClass(classToCheck) {
  4006. return hasClass(this.el_, classToCheck);
  4007. }
  4008. /**
  4009. * Add a CSS class name to the `Component`s element.
  4010. *
  4011. * @param {...string} classesToAdd
  4012. * One or more CSS class name to add.
  4013. */
  4014. addClass(...classesToAdd) {
  4015. addClass(this.el_, ...classesToAdd);
  4016. }
  4017. /**
  4018. * Remove a CSS class name from the `Component`s element.
  4019. *
  4020. * @param {...string} classesToRemove
  4021. * One or more CSS class name to remove.
  4022. */
  4023. removeClass(...classesToRemove) {
  4024. removeClass(this.el_, ...classesToRemove);
  4025. }
  4026. /**
  4027. * Add or remove a CSS class name from the component's element.
  4028. * - `classToToggle` gets added when {@link Component#hasClass} would return false.
  4029. * - `classToToggle` gets removed when {@link Component#hasClass} would return true.
  4030. *
  4031. * @param {string} classToToggle
  4032. * The class to add or remove based on (@link Component#hasClass}
  4033. *
  4034. * @param {boolean|Dom~predicate} [predicate]
  4035. * An {@link Dom~predicate} function or a boolean
  4036. */
  4037. toggleClass(classToToggle, predicate) {
  4038. toggleClass(this.el_, classToToggle, predicate);
  4039. }
  4040. /**
  4041. * Show the `Component`s element if it is hidden by removing the
  4042. * 'vjs-hidden' class name from it.
  4043. */
  4044. show() {
  4045. this.removeClass('vjs-hidden');
  4046. }
  4047. /**
  4048. * Hide the `Component`s element if it is currently showing by adding the
  4049. * 'vjs-hidden` class name to it.
  4050. */
  4051. hide() {
  4052. this.addClass('vjs-hidden');
  4053. }
  4054. /**
  4055. * Lock a `Component`s element in its visible state by adding the 'vjs-lock-showing'
  4056. * class name to it. Used during fadeIn/fadeOut.
  4057. *
  4058. * @private
  4059. */
  4060. lockShowing() {
  4061. this.addClass('vjs-lock-showing');
  4062. }
  4063. /**
  4064. * Unlock a `Component`s element from its visible state by removing the 'vjs-lock-showing'
  4065. * class name from it. Used during fadeIn/fadeOut.
  4066. *
  4067. * @private
  4068. */
  4069. unlockShowing() {
  4070. this.removeClass('vjs-lock-showing');
  4071. }
  4072. /**
  4073. * Get the value of an attribute on the `Component`s element.
  4074. *
  4075. * @param {string} attribute
  4076. * Name of the attribute to get the value from.
  4077. *
  4078. * @return {string|null}
  4079. * - The value of the attribute that was asked for.
  4080. * - Can be an empty string on some browsers if the attribute does not exist
  4081. * or has no value
  4082. * - Most browsers will return null if the attribute does not exist or has
  4083. * no value.
  4084. *
  4085. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute}
  4086. */
  4087. getAttribute(attribute) {
  4088. return getAttribute(this.el_, attribute);
  4089. }
  4090. /**
  4091. * Set the value of an attribute on the `Component`'s element
  4092. *
  4093. * @param {string} attribute
  4094. * Name of the attribute to set.
  4095. *
  4096. * @param {string} value
  4097. * Value to set the attribute to.
  4098. *
  4099. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute}
  4100. */
  4101. setAttribute(attribute, value) {
  4102. setAttribute(this.el_, attribute, value);
  4103. }
  4104. /**
  4105. * Remove an attribute from the `Component`s element.
  4106. *
  4107. * @param {string} attribute
  4108. * Name of the attribute to remove.
  4109. *
  4110. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute}
  4111. */
  4112. removeAttribute(attribute) {
  4113. removeAttribute(this.el_, attribute);
  4114. }
  4115. /**
  4116. * Get or set the width of the component based upon the CSS styles.
  4117. * See {@link Component#dimension} for more detailed information.
  4118. *
  4119. * @param {number|string} [num]
  4120. * The width that you want to set postfixed with '%', 'px' or nothing.
  4121. *
  4122. * @param {boolean} [skipListeners]
  4123. * Skip the componentresize event trigger
  4124. *
  4125. * @return {number|undefined}
  4126. * The width when getting, zero if there is no width
  4127. */
  4128. width(num, skipListeners) {
  4129. return this.dimension('width', num, skipListeners);
  4130. }
  4131. /**
  4132. * Get or set the height of the component based upon the CSS styles.
  4133. * See {@link Component#dimension} for more detailed information.
  4134. *
  4135. * @param {number|string} [num]
  4136. * The height that you want to set postfixed with '%', 'px' or nothing.
  4137. *
  4138. * @param {boolean} [skipListeners]
  4139. * Skip the componentresize event trigger
  4140. *
  4141. * @return {number|undefined}
  4142. * The height when getting, zero if there is no height
  4143. */
  4144. height(num, skipListeners) {
  4145. return this.dimension('height', num, skipListeners);
  4146. }
  4147. /**
  4148. * Set both the width and height of the `Component` element at the same time.
  4149. *
  4150. * @param {number|string} width
  4151. * Width to set the `Component`s element to.
  4152. *
  4153. * @param {number|string} height
  4154. * Height to set the `Component`s element to.
  4155. */
  4156. dimensions(width, height) {
  4157. // Skip componentresize listeners on width for optimization
  4158. this.width(width, true);
  4159. this.height(height);
  4160. }
  4161. /**
  4162. * Get or set width or height of the `Component` element. This is the shared code
  4163. * for the {@link Component#width} and {@link Component#height}.
  4164. *
  4165. * Things to know:
  4166. * - If the width or height in an number this will return the number postfixed with 'px'.
  4167. * - If the width/height is a percent this will return the percent postfixed with '%'
  4168. * - Hidden elements have a width of 0 with `window.getComputedStyle`. This function
  4169. * defaults to the `Component`s `style.width` and falls back to `window.getComputedStyle`.
  4170. * See [this]{@link http://www.foliotek.com/devblog/getting-the-width-of-a-hidden-element-with-jquery-using-width/}
  4171. * for more information
  4172. * - If you want the computed style of the component, use {@link Component#currentWidth}
  4173. * and {@link {Component#currentHeight}
  4174. *
  4175. * @fires Component#componentresize
  4176. *
  4177. * @param {string} widthOrHeight
  4178. 8 'width' or 'height'
  4179. *
  4180. * @param {number|string} [num]
  4181. 8 New dimension
  4182. *
  4183. * @param {boolean} [skipListeners]
  4184. * Skip componentresize event trigger
  4185. *
  4186. * @return {number|undefined}
  4187. * The dimension when getting or 0 if unset
  4188. */
  4189. dimension(widthOrHeight, num, skipListeners) {
  4190. if (num !== undefined) {
  4191. // Set to zero if null or literally NaN (NaN !== NaN)
  4192. if (num === null || num !== num) {
  4193. num = 0;
  4194. }
  4195. // Check if using css width/height (% or px) and adjust
  4196. if (('' + num).indexOf('%') !== -1 || ('' + num).indexOf('px') !== -1) {
  4197. this.el_.style[widthOrHeight] = num;
  4198. } else if (num === 'auto') {
  4199. this.el_.style[widthOrHeight] = '';
  4200. } else {
  4201. this.el_.style[widthOrHeight] = num + 'px';
  4202. }
  4203. // skipListeners allows us to avoid triggering the resize event when setting both width and height
  4204. if (!skipListeners) {
  4205. /**
  4206. * Triggered when a component is resized.
  4207. *
  4208. * @event Component#componentresize
  4209. * @type {Event}
  4210. */
  4211. this.trigger('componentresize');
  4212. }
  4213. return;
  4214. }
  4215. // Not setting a value, so getting it
  4216. // Make sure element exists
  4217. if (!this.el_) {
  4218. return 0;
  4219. }
  4220. // Get dimension value from style
  4221. const val = this.el_.style[widthOrHeight];
  4222. const pxIndex = val.indexOf('px');
  4223. if (pxIndex !== -1) {
  4224. // Return the pixel value with no 'px'
  4225. return parseInt(val.slice(0, pxIndex), 10);
  4226. }
  4227. // No px so using % or no style was set, so falling back to offsetWidth/height
  4228. // If component has display:none, offset will return 0
  4229. // TODO: handle display:none and no dimension style using px
  4230. return parseInt(this.el_['offset' + toTitleCase(widthOrHeight)], 10);
  4231. }
  4232. /**
  4233. * Get the computed width or the height of the component's element.
  4234. *
  4235. * Uses `window.getComputedStyle`.
  4236. *
  4237. * @param {string} widthOrHeight
  4238. * A string containing 'width' or 'height'. Whichever one you want to get.
  4239. *
  4240. * @return {number}
  4241. * The dimension that gets asked for or 0 if nothing was set
  4242. * for that dimension.
  4243. */
  4244. currentDimension(widthOrHeight) {
  4245. let computedWidthOrHeight = 0;
  4246. if (widthOrHeight !== 'width' && widthOrHeight !== 'height') {
  4247. throw new Error('currentDimension only accepts width or height value');
  4248. }
  4249. computedWidthOrHeight = computedStyle(this.el_, widthOrHeight);
  4250. // remove 'px' from variable and parse as integer
  4251. computedWidthOrHeight = parseFloat(computedWidthOrHeight);
  4252. // if the computed value is still 0, it's possible that the browser is lying
  4253. // and we want to check the offset values.
  4254. // This code also runs wherever getComputedStyle doesn't exist.
  4255. if (computedWidthOrHeight === 0 || isNaN(computedWidthOrHeight)) {
  4256. const rule = `offset${toTitleCase(widthOrHeight)}`;
  4257. computedWidthOrHeight = this.el_[rule];
  4258. }
  4259. return computedWidthOrHeight;
  4260. }
  4261. /**
  4262. * An object that contains width and height values of the `Component`s
  4263. * computed style. Uses `window.getComputedStyle`.
  4264. *
  4265. * @typedef {Object} Component~DimensionObject
  4266. *
  4267. * @property {number} width
  4268. * The width of the `Component`s computed style.
  4269. *
  4270. * @property {number} height
  4271. * The height of the `Component`s computed style.
  4272. */
  4273. /**
  4274. * Get an object that contains computed width and height values of the
  4275. * component's element.
  4276. *
  4277. * Uses `window.getComputedStyle`.
  4278. *
  4279. * @return {Component~DimensionObject}
  4280. * The computed dimensions of the component's element.
  4281. */
  4282. currentDimensions() {
  4283. return {
  4284. width: this.currentDimension('width'),
  4285. height: this.currentDimension('height')
  4286. };
  4287. }
  4288. /**
  4289. * Get the computed width of the component's element.
  4290. *
  4291. * Uses `window.getComputedStyle`.
  4292. *
  4293. * @return {number}
  4294. * The computed width of the component's element.
  4295. */
  4296. currentWidth() {
  4297. return this.currentDimension('width');
  4298. }
  4299. /**
  4300. * Get the computed height of the component's element.
  4301. *
  4302. * Uses `window.getComputedStyle`.
  4303. *
  4304. * @return {number}
  4305. * The computed height of the component's element.
  4306. */
  4307. currentHeight() {
  4308. return this.currentDimension('height');
  4309. }
  4310. /**
  4311. * Set the focus to this component
  4312. */
  4313. focus() {
  4314. this.el_.focus();
  4315. }
  4316. /**
  4317. * Remove the focus from this component
  4318. */
  4319. blur() {
  4320. this.el_.blur();
  4321. }
  4322. /**
  4323. * When this Component receives a `keydown` event which it does not process,
  4324. * it passes the event to the Player for handling.
  4325. *
  4326. * @param {KeyboardEvent} event
  4327. * The `keydown` event that caused this function to be called.
  4328. */
  4329. handleKeyDown(event) {
  4330. if (this.player_) {
  4331. // We only stop propagation here because we want unhandled events to fall
  4332. // back to the browser. Exclude Tab for focus trapping.
  4333. if (!keycode.isEventKey(event, 'Tab')) {
  4334. event.stopPropagation();
  4335. }
  4336. this.player_.handleKeyDown(event);
  4337. }
  4338. }
  4339. /**
  4340. * Many components used to have a `handleKeyPress` method, which was poorly
  4341. * named because it listened to a `keydown` event. This method name now
  4342. * delegates to `handleKeyDown`. This means anyone calling `handleKeyPress`
  4343. * will not see their method calls stop working.
  4344. *
  4345. * @param {KeyboardEvent} event
  4346. * The event that caused this function to be called.
  4347. */
  4348. handleKeyPress(event) {
  4349. this.handleKeyDown(event);
  4350. }
  4351. /**
  4352. * Emit a 'tap' events when touch event support gets detected. This gets used to
  4353. * support toggling the controls through a tap on the video. They get enabled
  4354. * because every sub-component would have extra overhead otherwise.
  4355. *
  4356. * @protected
  4357. * @fires Component#tap
  4358. * @listens Component#touchstart
  4359. * @listens Component#touchmove
  4360. * @listens Component#touchleave
  4361. * @listens Component#touchcancel
  4362. * @listens Component#touchend
  4363. */
  4364. emitTapEvents() {
  4365. // Track the start time so we can determine how long the touch lasted
  4366. let touchStart = 0;
  4367. let firstTouch = null;
  4368. // Maximum movement allowed during a touch event to still be considered a tap
  4369. // Other popular libs use anywhere from 2 (hammer.js) to 15,
  4370. // so 10 seems like a nice, round number.
  4371. const tapMovementThreshold = 10;
  4372. // The maximum length a touch can be while still being considered a tap
  4373. const touchTimeThreshold = 200;
  4374. let couldBeTap;
  4375. this.on('touchstart', function (event) {
  4376. // If more than one finger, don't consider treating this as a click
  4377. if (event.touches.length === 1) {
  4378. // Copy pageX/pageY from the object
  4379. firstTouch = {
  4380. pageX: event.touches[0].pageX,
  4381. pageY: event.touches[0].pageY
  4382. };
  4383. // Record start time so we can detect a tap vs. "touch and hold"
  4384. touchStart = window.performance.now();
  4385. // Reset couldBeTap tracking
  4386. couldBeTap = true;
  4387. }
  4388. });
  4389. this.on('touchmove', function (event) {
  4390. // If more than one finger, don't consider treating this as a click
  4391. if (event.touches.length > 1) {
  4392. couldBeTap = false;
  4393. } else if (firstTouch) {
  4394. // Some devices will throw touchmoves for all but the slightest of taps.
  4395. // So, if we moved only a small distance, this could still be a tap
  4396. const xdiff = event.touches[0].pageX - firstTouch.pageX;
  4397. const ydiff = event.touches[0].pageY - firstTouch.pageY;
  4398. const touchDistance = Math.sqrt(xdiff * xdiff + ydiff * ydiff);
  4399. if (touchDistance > tapMovementThreshold) {
  4400. couldBeTap = false;
  4401. }
  4402. }
  4403. });
  4404. const noTap = function () {
  4405. couldBeTap = false;
  4406. };
  4407. // TODO: Listen to the original target. http://youtu.be/DujfpXOKUp8?t=13m8s
  4408. this.on('touchleave', noTap);
  4409. this.on('touchcancel', noTap);
  4410. // When the touch ends, measure how long it took and trigger the appropriate
  4411. // event
  4412. this.on('touchend', function (event) {
  4413. firstTouch = null;
  4414. // Proceed only if the touchmove/leave/cancel event didn't happen
  4415. if (couldBeTap === true) {
  4416. // Measure how long the touch lasted
  4417. const touchTime = window.performance.now() - touchStart;
  4418. // Make sure the touch was less than the threshold to be considered a tap
  4419. if (touchTime < touchTimeThreshold) {
  4420. // Don't let browser turn this into a click
  4421. event.preventDefault();
  4422. /**
  4423. * Triggered when a `Component` is tapped.
  4424. *
  4425. * @event Component#tap
  4426. * @type {MouseEvent}
  4427. */
  4428. this.trigger('tap');
  4429. // It may be good to copy the touchend event object and change the
  4430. // type to tap, if the other event properties aren't exact after
  4431. // Events.fixEvent runs (e.g. event.target)
  4432. }
  4433. }
  4434. });
  4435. }
  4436. /**
  4437. * This function reports user activity whenever touch events happen. This can get
  4438. * turned off by any sub-components that wants touch events to act another way.
  4439. *
  4440. * Report user touch activity when touch events occur. User activity gets used to
  4441. * determine when controls should show/hide. It is simple when it comes to mouse
  4442. * events, because any mouse event should show the controls. So we capture mouse
  4443. * events that bubble up to the player and report activity when that happens.
  4444. * With touch events it isn't as easy as `touchstart` and `touchend` toggle player
  4445. * controls. So touch events can't help us at the player level either.
  4446. *
  4447. * User activity gets checked asynchronously. So what could happen is a tap event
  4448. * on the video turns the controls off. Then the `touchend` event bubbles up to
  4449. * the player. Which, if it reported user activity, would turn the controls right
  4450. * back on. We also don't want to completely block touch events from bubbling up.
  4451. * Furthermore a `touchmove` event and anything other than a tap, should not turn
  4452. * controls back on.
  4453. *
  4454. * @listens Component#touchstart
  4455. * @listens Component#touchmove
  4456. * @listens Component#touchend
  4457. * @listens Component#touchcancel
  4458. */
  4459. enableTouchActivity() {
  4460. // Don't continue if the root player doesn't support reporting user activity
  4461. if (!this.player() || !this.player().reportUserActivity) {
  4462. return;
  4463. }
  4464. // listener for reporting that the user is active
  4465. const report = bind_(this.player(), this.player().reportUserActivity);
  4466. let touchHolding;
  4467. this.on('touchstart', function () {
  4468. report();
  4469. // For as long as the they are touching the device or have their mouse down,
  4470. // we consider them active even if they're not moving their finger or mouse.
  4471. // So we want to continue to update that they are active
  4472. this.clearInterval(touchHolding);
  4473. // report at the same interval as activityCheck
  4474. touchHolding = this.setInterval(report, 250);
  4475. });
  4476. const touchEnd = function (event) {
  4477. report();
  4478. // stop the interval that maintains activity if the touch is holding
  4479. this.clearInterval(touchHolding);
  4480. };
  4481. this.on('touchmove', report);
  4482. this.on('touchend', touchEnd);
  4483. this.on('touchcancel', touchEnd);
  4484. }
  4485. /**
  4486. * A callback that has no parameters and is bound into `Component`s context.
  4487. *
  4488. * @callback Component~GenericCallback
  4489. * @this Component
  4490. */
  4491. /**
  4492. * Creates a function that runs after an `x` millisecond timeout. This function is a
  4493. * wrapper around `window.setTimeout`. There are a few reasons to use this one
  4494. * instead though:
  4495. * 1. It gets cleared via {@link Component#clearTimeout} when
  4496. * {@link Component#dispose} gets called.
  4497. * 2. The function callback will gets turned into a {@link Component~GenericCallback}
  4498. *
  4499. * > Note: You can't use `window.clearTimeout` on the id returned by this function. This
  4500. * will cause its dispose listener not to get cleaned up! Please use
  4501. * {@link Component#clearTimeout} or {@link Component#dispose} instead.
  4502. *
  4503. * @param {Component~GenericCallback} fn
  4504. * The function that will be run after `timeout`.
  4505. *
  4506. * @param {number} timeout
  4507. * Timeout in milliseconds to delay before executing the specified function.
  4508. *
  4509. * @return {number}
  4510. * Returns a timeout ID that gets used to identify the timeout. It can also
  4511. * get used in {@link Component#clearTimeout} to clear the timeout that
  4512. * was set.
  4513. *
  4514. * @listens Component#dispose
  4515. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout}
  4516. */
  4517. setTimeout(fn, timeout) {
  4518. // declare as variables so they are properly available in timeout function
  4519. // eslint-disable-next-line
  4520. var timeoutId;
  4521. fn = bind_(this, fn);
  4522. this.clearTimersOnDispose_();
  4523. timeoutId = window.setTimeout(() => {
  4524. if (this.setTimeoutIds_.has(timeoutId)) {
  4525. this.setTimeoutIds_.delete(timeoutId);
  4526. }
  4527. fn();
  4528. }, timeout);
  4529. this.setTimeoutIds_.add(timeoutId);
  4530. return timeoutId;
  4531. }
  4532. /**
  4533. * Clears a timeout that gets created via `window.setTimeout` or
  4534. * {@link Component#setTimeout}. If you set a timeout via {@link Component#setTimeout}
  4535. * use this function instead of `window.clearTimout`. If you don't your dispose
  4536. * listener will not get cleaned up until {@link Component#dispose}!
  4537. *
  4538. * @param {number} timeoutId
  4539. * The id of the timeout to clear. The return value of
  4540. * {@link Component#setTimeout} or `window.setTimeout`.
  4541. *
  4542. * @return {number}
  4543. * Returns the timeout id that was cleared.
  4544. *
  4545. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearTimeout}
  4546. */
  4547. clearTimeout(timeoutId) {
  4548. if (this.setTimeoutIds_.has(timeoutId)) {
  4549. this.setTimeoutIds_.delete(timeoutId);
  4550. window.clearTimeout(timeoutId);
  4551. }
  4552. return timeoutId;
  4553. }
  4554. /**
  4555. * Creates a function that gets run every `x` milliseconds. This function is a wrapper
  4556. * around `window.setInterval`. There are a few reasons to use this one instead though.
  4557. * 1. It gets cleared via {@link Component#clearInterval} when
  4558. * {@link Component#dispose} gets called.
  4559. * 2. The function callback will be a {@link Component~GenericCallback}
  4560. *
  4561. * @param {Component~GenericCallback} fn
  4562. * The function to run every `x` seconds.
  4563. *
  4564. * @param {number} interval
  4565. * Execute the specified function every `x` milliseconds.
  4566. *
  4567. * @return {number}
  4568. * Returns an id that can be used to identify the interval. It can also be be used in
  4569. * {@link Component#clearInterval} to clear the interval.
  4570. *
  4571. * @listens Component#dispose
  4572. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval}
  4573. */
  4574. setInterval(fn, interval) {
  4575. fn = bind_(this, fn);
  4576. this.clearTimersOnDispose_();
  4577. const intervalId = window.setInterval(fn, interval);
  4578. this.setIntervalIds_.add(intervalId);
  4579. return intervalId;
  4580. }
  4581. /**
  4582. * Clears an interval that gets created via `window.setInterval` or
  4583. * {@link Component#setInterval}. If you set an interval via {@link Component#setInterval}
  4584. * use this function instead of `window.clearInterval`. If you don't your dispose
  4585. * listener will not get cleaned up until {@link Component#dispose}!
  4586. *
  4587. * @param {number} intervalId
  4588. * The id of the interval to clear. The return value of
  4589. * {@link Component#setInterval} or `window.setInterval`.
  4590. *
  4591. * @return {number}
  4592. * Returns the interval id that was cleared.
  4593. *
  4594. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearInterval}
  4595. */
  4596. clearInterval(intervalId) {
  4597. if (this.setIntervalIds_.has(intervalId)) {
  4598. this.setIntervalIds_.delete(intervalId);
  4599. window.clearInterval(intervalId);
  4600. }
  4601. return intervalId;
  4602. }
  4603. /**
  4604. * Queues up a callback to be passed to requestAnimationFrame (rAF), but
  4605. * with a few extra bonuses:
  4606. *
  4607. * - Supports browsers that do not support rAF by falling back to
  4608. * {@link Component#setTimeout}.
  4609. *
  4610. * - The callback is turned into a {@link Component~GenericCallback} (i.e.
  4611. * bound to the component).
  4612. *
  4613. * - Automatic cancellation of the rAF callback is handled if the component
  4614. * is disposed before it is called.
  4615. *
  4616. * @param {Component~GenericCallback} fn
  4617. * A function that will be bound to this component and executed just
  4618. * before the browser's next repaint.
  4619. *
  4620. * @return {number}
  4621. * Returns an rAF ID that gets used to identify the timeout. It can
  4622. * also be used in {@link Component#cancelAnimationFrame} to cancel
  4623. * the animation frame callback.
  4624. *
  4625. * @listens Component#dispose
  4626. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame}
  4627. */
  4628. requestAnimationFrame(fn) {
  4629. this.clearTimersOnDispose_();
  4630. // declare as variables so they are properly available in rAF function
  4631. // eslint-disable-next-line
  4632. var id;
  4633. fn = bind_(this, fn);
  4634. id = window.requestAnimationFrame(() => {
  4635. if (this.rafIds_.has(id)) {
  4636. this.rafIds_.delete(id);
  4637. }
  4638. fn();
  4639. });
  4640. this.rafIds_.add(id);
  4641. return id;
  4642. }
  4643. /**
  4644. * Request an animation frame, but only one named animation
  4645. * frame will be queued. Another will never be added until
  4646. * the previous one finishes.
  4647. *
  4648. * @param {string} name
  4649. * The name to give this requestAnimationFrame
  4650. *
  4651. * @param {Component~GenericCallback} fn
  4652. * A function that will be bound to this component and executed just
  4653. * before the browser's next repaint.
  4654. */
  4655. requestNamedAnimationFrame(name, fn) {
  4656. if (this.namedRafs_.has(name)) {
  4657. return;
  4658. }
  4659. this.clearTimersOnDispose_();
  4660. fn = bind_(this, fn);
  4661. const id = this.requestAnimationFrame(() => {
  4662. fn();
  4663. if (this.namedRafs_.has(name)) {
  4664. this.namedRafs_.delete(name);
  4665. }
  4666. });
  4667. this.namedRafs_.set(name, id);
  4668. return name;
  4669. }
  4670. /**
  4671. * Cancels a current named animation frame if it exists.
  4672. *
  4673. * @param {string} name
  4674. * The name of the requestAnimationFrame to cancel.
  4675. */
  4676. cancelNamedAnimationFrame(name) {
  4677. if (!this.namedRafs_.has(name)) {
  4678. return;
  4679. }
  4680. this.cancelAnimationFrame(this.namedRafs_.get(name));
  4681. this.namedRafs_.delete(name);
  4682. }
  4683. /**
  4684. * Cancels a queued callback passed to {@link Component#requestAnimationFrame}
  4685. * (rAF).
  4686. *
  4687. * If you queue an rAF callback via {@link Component#requestAnimationFrame},
  4688. * use this function instead of `window.cancelAnimationFrame`. If you don't,
  4689. * your dispose listener will not get cleaned up until {@link Component#dispose}!
  4690. *
  4691. * @param {number} id
  4692. * The rAF ID to clear. The return value of {@link Component#requestAnimationFrame}.
  4693. *
  4694. * @return {number}
  4695. * Returns the rAF ID that was cleared.
  4696. *
  4697. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/cancelAnimationFrame}
  4698. */
  4699. cancelAnimationFrame(id) {
  4700. if (this.rafIds_.has(id)) {
  4701. this.rafIds_.delete(id);
  4702. window.cancelAnimationFrame(id);
  4703. }
  4704. return id;
  4705. }
  4706. /**
  4707. * A function to setup `requestAnimationFrame`, `setTimeout`,
  4708. * and `setInterval`, clearing on dispose.
  4709. *
  4710. * > Previously each timer added and removed dispose listeners on it's own.
  4711. * For better performance it was decided to batch them all, and use `Set`s
  4712. * to track outstanding timer ids.
  4713. *
  4714. * @private
  4715. */
  4716. clearTimersOnDispose_() {
  4717. if (this.clearingTimersOnDispose_) {
  4718. return;
  4719. }
  4720. this.clearingTimersOnDispose_ = true;
  4721. this.one('dispose', () => {
  4722. [['namedRafs_', 'cancelNamedAnimationFrame'], ['rafIds_', 'cancelAnimationFrame'], ['setTimeoutIds_', 'clearTimeout'], ['setIntervalIds_', 'clearInterval']].forEach(([idName, cancelName]) => {
  4723. // for a `Set` key will actually be the value again
  4724. // so forEach((val, val) =>` but for maps we want to use
  4725. // the key.
  4726. this[idName].forEach((val, key) => this[cancelName](key));
  4727. });
  4728. this.clearingTimersOnDispose_ = false;
  4729. });
  4730. }
  4731. /**
  4732. * Register a `Component` with `videojs` given the name and the component.
  4733. *
  4734. * > NOTE: {@link Tech}s should not be registered as a `Component`. {@link Tech}s
  4735. * should be registered using {@link Tech.registerTech} or
  4736. * {@link videojs:videojs.registerTech}.
  4737. *
  4738. * > NOTE: This function can also be seen on videojs as
  4739. * {@link videojs:videojs.registerComponent}.
  4740. *
  4741. * @param {string} name
  4742. * The name of the `Component` to register.
  4743. *
  4744. * @param {Component} ComponentToRegister
  4745. * The `Component` class to register.
  4746. *
  4747. * @return {Component}
  4748. * The `Component` that was registered.
  4749. */
  4750. static registerComponent(name, ComponentToRegister) {
  4751. if (typeof name !== 'string' || !name) {
  4752. throw new Error(`Illegal component name, "${name}"; must be a non-empty string.`);
  4753. }
  4754. const Tech = Component.getComponent('Tech');
  4755. // We need to make sure this check is only done if Tech has been registered.
  4756. const isTech = Tech && Tech.isTech(ComponentToRegister);
  4757. const isComp = Component === ComponentToRegister || Component.prototype.isPrototypeOf(ComponentToRegister.prototype);
  4758. if (isTech || !isComp) {
  4759. let reason;
  4760. if (isTech) {
  4761. reason = 'techs must be registered using Tech.registerTech()';
  4762. } else {
  4763. reason = 'must be a Component subclass';
  4764. }
  4765. throw new Error(`Illegal component, "${name}"; ${reason}.`);
  4766. }
  4767. name = toTitleCase(name);
  4768. if (!Component.components_) {
  4769. Component.components_ = {};
  4770. }
  4771. const Player = Component.getComponent('Player');
  4772. if (name === 'Player' && Player && Player.players) {
  4773. const players = Player.players;
  4774. const playerNames = Object.keys(players);
  4775. // If we have players that were disposed, then their name will still be
  4776. // in Players.players. So, we must loop through and verify that the value
  4777. // for each item is not null. This allows registration of the Player component
  4778. // after all players have been disposed or before any were created.
  4779. if (players && playerNames.length > 0 && playerNames.map(pname => players[pname]).every(Boolean)) {
  4780. throw new Error('Can not register Player component after player has been created.');
  4781. }
  4782. }
  4783. Component.components_[name] = ComponentToRegister;
  4784. Component.components_[toLowerCase(name)] = ComponentToRegister;
  4785. return ComponentToRegister;
  4786. }
  4787. /**
  4788. * Get a `Component` based on the name it was registered with.
  4789. *
  4790. * @param {string} name
  4791. * The Name of the component to get.
  4792. *
  4793. * @return {typeof Component}
  4794. * The `Component` that got registered under the given name.
  4795. */
  4796. static getComponent(name) {
  4797. if (!name || !Component.components_) {
  4798. return;
  4799. }
  4800. return Component.components_[name];
  4801. }
  4802. }
  4803. Component.registerComponent('Component', Component);
  4804. /**
  4805. * @file time.js
  4806. * @module time
  4807. */
  4808. /**
  4809. * Returns the time for the specified index at the start or end
  4810. * of a TimeRange object.
  4811. *
  4812. * @typedef {Function} TimeRangeIndex
  4813. *
  4814. * @param {number} [index=0]
  4815. * The range number to return the time for.
  4816. *
  4817. * @return {number}
  4818. * The time offset at the specified index.
  4819. *
  4820. * @deprecated The index argument must be provided.
  4821. * In the future, leaving it out will throw an error.
  4822. */
  4823. /**
  4824. * An object that contains ranges of time, which mimics {@link TimeRanges}.
  4825. *
  4826. * @typedef {Object} TimeRange
  4827. *
  4828. * @property {number} length
  4829. * The number of time ranges represented by this object.
  4830. *
  4831. * @property {module:time~TimeRangeIndex} start
  4832. * Returns the time offset at which a specified time range begins.
  4833. *
  4834. * @property {module:time~TimeRangeIndex} end
  4835. * Returns the time offset at which a specified time range ends.
  4836. *
  4837. * @see https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges
  4838. */
  4839. /**
  4840. * Check if any of the time ranges are over the maximum index.
  4841. *
  4842. * @private
  4843. * @param {string} fnName
  4844. * The function name to use for logging
  4845. *
  4846. * @param {number} index
  4847. * The index to check
  4848. *
  4849. * @param {number} maxIndex
  4850. * The maximum possible index
  4851. *
  4852. * @throws {Error} if the timeRanges provided are over the maxIndex
  4853. */
  4854. function rangeCheck(fnName, index, maxIndex) {
  4855. if (typeof index !== 'number' || index < 0 || index > maxIndex) {
  4856. throw new Error(`Failed to execute '${fnName}' on 'TimeRanges': The index provided (${index}) is non-numeric or out of bounds (0-${maxIndex}).`);
  4857. }
  4858. }
  4859. /**
  4860. * Get the time for the specified index at the start or end
  4861. * of a TimeRange object.
  4862. *
  4863. * @private
  4864. * @param {string} fnName
  4865. * The function name to use for logging
  4866. *
  4867. * @param {string} valueIndex
  4868. * The property that should be used to get the time. should be
  4869. * 'start' or 'end'
  4870. *
  4871. * @param {Array} ranges
  4872. * An array of time ranges
  4873. *
  4874. * @param {Array} [rangeIndex=0]
  4875. * The index to start the search at
  4876. *
  4877. * @return {number}
  4878. * The time that offset at the specified index.
  4879. *
  4880. * @deprecated rangeIndex must be set to a value, in the future this will throw an error.
  4881. * @throws {Error} if rangeIndex is more than the length of ranges
  4882. */
  4883. function getRange(fnName, valueIndex, ranges, rangeIndex) {
  4884. rangeCheck(fnName, rangeIndex, ranges.length - 1);
  4885. return ranges[rangeIndex][valueIndex];
  4886. }
  4887. /**
  4888. * Create a time range object given ranges of time.
  4889. *
  4890. * @private
  4891. * @param {Array} [ranges]
  4892. * An array of time ranges.
  4893. *
  4894. * @return {TimeRange}
  4895. */
  4896. function createTimeRangesObj(ranges) {
  4897. let timeRangesObj;
  4898. if (ranges === undefined || ranges.length === 0) {
  4899. timeRangesObj = {
  4900. length: 0,
  4901. start() {
  4902. throw new Error('This TimeRanges object is empty');
  4903. },
  4904. end() {
  4905. throw new Error('This TimeRanges object is empty');
  4906. }
  4907. };
  4908. } else {
  4909. timeRangesObj = {
  4910. length: ranges.length,
  4911. start: getRange.bind(null, 'start', 0, ranges),
  4912. end: getRange.bind(null, 'end', 1, ranges)
  4913. };
  4914. }
  4915. if (window.Symbol && window.Symbol.iterator) {
  4916. timeRangesObj[window.Symbol.iterator] = () => (ranges || []).values();
  4917. }
  4918. return timeRangesObj;
  4919. }
  4920. /**
  4921. * Create a `TimeRange` object which mimics an
  4922. * {@link https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges|HTML5 TimeRanges instance}.
  4923. *
  4924. * @param {number|Array[]} start
  4925. * The start of a single range (a number) or an array of ranges (an
  4926. * array of arrays of two numbers each).
  4927. *
  4928. * @param {number} end
  4929. * The end of a single range. Cannot be used with the array form of
  4930. * the `start` argument.
  4931. *
  4932. * @return {TimeRange}
  4933. */
  4934. function createTimeRanges(start, end) {
  4935. if (Array.isArray(start)) {
  4936. return createTimeRangesObj(start);
  4937. } else if (start === undefined || end === undefined) {
  4938. return createTimeRangesObj();
  4939. }
  4940. return createTimeRangesObj([[start, end]]);
  4941. }
  4942. /**
  4943. * Format seconds as a time string, H:MM:SS or M:SS. Supplying a guide (in
  4944. * seconds) will force a number of leading zeros to cover the length of the
  4945. * guide.
  4946. *
  4947. * @private
  4948. * @param {number} seconds
  4949. * Number of seconds to be turned into a string
  4950. *
  4951. * @param {number} guide
  4952. * Number (in seconds) to model the string after
  4953. *
  4954. * @return {string}
  4955. * Time formatted as H:MM:SS or M:SS
  4956. */
  4957. const defaultImplementation = function (seconds, guide) {
  4958. seconds = seconds < 0 ? 0 : seconds;
  4959. let s = Math.floor(seconds % 60);
  4960. let m = Math.floor(seconds / 60 % 60);
  4961. let h = Math.floor(seconds / 3600);
  4962. const gm = Math.floor(guide / 60 % 60);
  4963. const gh = Math.floor(guide / 3600);
  4964. // handle invalid times
  4965. if (isNaN(seconds) || seconds === Infinity) {
  4966. // '-' is false for all relational operators (e.g. <, >=) so this setting
  4967. // will add the minimum number of fields specified by the guide
  4968. h = m = s = '-';
  4969. }
  4970. // Check if we need to show hours
  4971. h = h > 0 || gh > 0 ? h + ':' : '';
  4972. // If hours are showing, we may need to add a leading zero.
  4973. // Always show at least one digit of minutes.
  4974. m = ((h || gm >= 10) && m < 10 ? '0' + m : m) + ':';
  4975. // Check if leading zero is need for seconds
  4976. s = s < 10 ? '0' + s : s;
  4977. return h + m + s;
  4978. };
  4979. // Internal pointer to the current implementation.
  4980. let implementation = defaultImplementation;
  4981. /**
  4982. * Replaces the default formatTime implementation with a custom implementation.
  4983. *
  4984. * @param {Function} customImplementation
  4985. * A function which will be used in place of the default formatTime
  4986. * implementation. Will receive the current time in seconds and the
  4987. * guide (in seconds) as arguments.
  4988. */
  4989. function setFormatTime(customImplementation) {
  4990. implementation = customImplementation;
  4991. }
  4992. /**
  4993. * Resets formatTime to the default implementation.
  4994. */
  4995. function resetFormatTime() {
  4996. implementation = defaultImplementation;
  4997. }
  4998. /**
  4999. * Delegates to either the default time formatting function or a custom
  5000. * function supplied via `setFormatTime`.
  5001. *
  5002. * Formats seconds as a time string (H:MM:SS or M:SS). Supplying a
  5003. * guide (in seconds) will force a number of leading zeros to cover the
  5004. * length of the guide.
  5005. *
  5006. * @example formatTime(125, 600) === "02:05"
  5007. * @param {number} seconds
  5008. * Number of seconds to be turned into a string
  5009. *
  5010. * @param {number} guide
  5011. * Number (in seconds) to model the string after
  5012. *
  5013. * @return {string}
  5014. * Time formatted as H:MM:SS or M:SS
  5015. */
  5016. function formatTime(seconds, guide = seconds) {
  5017. return implementation(seconds, guide);
  5018. }
  5019. var Time = /*#__PURE__*/Object.freeze({
  5020. __proto__: null,
  5021. createTimeRanges: createTimeRanges,
  5022. createTimeRange: createTimeRanges,
  5023. setFormatTime: setFormatTime,
  5024. resetFormatTime: resetFormatTime,
  5025. formatTime: formatTime
  5026. });
  5027. /**
  5028. * @file buffer.js
  5029. * @module buffer
  5030. */
  5031. /**
  5032. * Compute the percentage of the media that has been buffered.
  5033. *
  5034. * @param { import('./time').TimeRange } buffered
  5035. * The current `TimeRanges` object representing buffered time ranges
  5036. *
  5037. * @param {number} duration
  5038. * Total duration of the media
  5039. *
  5040. * @return {number}
  5041. * Percent buffered of the total duration in decimal form.
  5042. */
  5043. function bufferedPercent(buffered, duration) {
  5044. let bufferedDuration = 0;
  5045. let start;
  5046. let end;
  5047. if (!duration) {
  5048. return 0;
  5049. }
  5050. if (!buffered || !buffered.length) {
  5051. buffered = createTimeRanges(0, 0);
  5052. }
  5053. for (let i = 0; i < buffered.length; i++) {
  5054. start = buffered.start(i);
  5055. end = buffered.end(i);
  5056. // buffered end can be bigger than duration by a very small fraction
  5057. if (end > duration) {
  5058. end = duration;
  5059. }
  5060. bufferedDuration += end - start;
  5061. }
  5062. return bufferedDuration / duration;
  5063. }
  5064. /**
  5065. * @file media-error.js
  5066. */
  5067. /**
  5068. * A Custom `MediaError` class which mimics the standard HTML5 `MediaError` class.
  5069. *
  5070. * @param {number|string|Object|MediaError} value
  5071. * This can be of multiple types:
  5072. * - number: should be a standard error code
  5073. * - string: an error message (the code will be 0)
  5074. * - Object: arbitrary properties
  5075. * - `MediaError` (native): used to populate a video.js `MediaError` object
  5076. * - `MediaError` (video.js): will return itself if it's already a
  5077. * video.js `MediaError` object.
  5078. *
  5079. * @see [MediaError Spec]{@link https://dev.w3.org/html5/spec-author-view/video.html#mediaerror}
  5080. * @see [Encrypted MediaError Spec]{@link https://www.w3.org/TR/2013/WD-encrypted-media-20130510/#error-codes}
  5081. *
  5082. * @class MediaError
  5083. */
  5084. function MediaError(value) {
  5085. // Allow redundant calls to this constructor to avoid having `instanceof`
  5086. // checks peppered around the code.
  5087. if (value instanceof MediaError) {
  5088. return value;
  5089. }
  5090. if (typeof value === 'number') {
  5091. this.code = value;
  5092. } else if (typeof value === 'string') {
  5093. // default code is zero, so this is a custom error
  5094. this.message = value;
  5095. } else if (isObject(value)) {
  5096. // We assign the `code` property manually because native `MediaError` objects
  5097. // do not expose it as an own/enumerable property of the object.
  5098. if (typeof value.code === 'number') {
  5099. this.code = value.code;
  5100. }
  5101. Object.assign(this, value);
  5102. }
  5103. if (!this.message) {
  5104. this.message = MediaError.defaultMessages[this.code] || '';
  5105. }
  5106. }
  5107. /**
  5108. * The error code that refers two one of the defined `MediaError` types
  5109. *
  5110. * @type {Number}
  5111. */
  5112. MediaError.prototype.code = 0;
  5113. /**
  5114. * An optional message that to show with the error. Message is not part of the HTML5
  5115. * video spec but allows for more informative custom errors.
  5116. *
  5117. * @type {String}
  5118. */
  5119. MediaError.prototype.message = '';
  5120. /**
  5121. * An optional status code that can be set by plugins to allow even more detail about
  5122. * the error. For example a plugin might provide a specific HTTP status code and an
  5123. * error message for that code. Then when the plugin gets that error this class will
  5124. * know how to display an error message for it. This allows a custom message to show
  5125. * up on the `Player` error overlay.
  5126. *
  5127. * @type {Array}
  5128. */
  5129. MediaError.prototype.status = null;
  5130. /**
  5131. * Errors indexed by the W3C standard. The order **CANNOT CHANGE**! See the
  5132. * specification listed under {@link MediaError} for more information.
  5133. *
  5134. * @enum {array}
  5135. * @readonly
  5136. * @property {string} 0 - MEDIA_ERR_CUSTOM
  5137. * @property {string} 1 - MEDIA_ERR_ABORTED
  5138. * @property {string} 2 - MEDIA_ERR_NETWORK
  5139. * @property {string} 3 - MEDIA_ERR_DECODE
  5140. * @property {string} 4 - MEDIA_ERR_SRC_NOT_SUPPORTED
  5141. * @property {string} 5 - MEDIA_ERR_ENCRYPTED
  5142. */
  5143. MediaError.errorTypes = ['MEDIA_ERR_CUSTOM', 'MEDIA_ERR_ABORTED', 'MEDIA_ERR_NETWORK', 'MEDIA_ERR_DECODE', 'MEDIA_ERR_SRC_NOT_SUPPORTED', 'MEDIA_ERR_ENCRYPTED'];
  5144. /**
  5145. * The default `MediaError` messages based on the {@link MediaError.errorTypes}.
  5146. *
  5147. * @type {Array}
  5148. * @constant
  5149. */
  5150. MediaError.defaultMessages = {
  5151. 1: 'You aborted the media playback',
  5152. 2: 'A network error caused the media download to fail part-way.',
  5153. 3: 'The media playback was aborted due to a corruption problem or because the media used features your browser did not support.',
  5154. 4: 'The media could not be loaded, either because the server or network failed or because the format is not supported.',
  5155. 5: 'The media is encrypted and we do not have the keys to decrypt it.'
  5156. };
  5157. // Add types as properties on MediaError
  5158. // e.g. MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED = 4;
  5159. for (let errNum = 0; errNum < MediaError.errorTypes.length; errNum++) {
  5160. MediaError[MediaError.errorTypes[errNum]] = errNum;
  5161. // values should be accessible on both the class and instance
  5162. MediaError.prototype[MediaError.errorTypes[errNum]] = errNum;
  5163. }
  5164. /**
  5165. * Returns whether an object is `Promise`-like (i.e. has a `then` method).
  5166. *
  5167. * @param {Object} value
  5168. * An object that may or may not be `Promise`-like.
  5169. *
  5170. * @return {boolean}
  5171. * Whether or not the object is `Promise`-like.
  5172. */
  5173. function isPromise(value) {
  5174. return value !== undefined && value !== null && typeof value.then === 'function';
  5175. }
  5176. /**
  5177. * Silence a Promise-like object.
  5178. *
  5179. * This is useful for avoiding non-harmful, but potentially confusing "uncaught
  5180. * play promise" rejection error messages.
  5181. *
  5182. * @param {Object} value
  5183. * An object that may or may not be `Promise`-like.
  5184. */
  5185. function silencePromise(value) {
  5186. if (isPromise(value)) {
  5187. value.then(null, e => {});
  5188. }
  5189. }
  5190. /**
  5191. * @file text-track-list-converter.js Utilities for capturing text track state and
  5192. * re-creating tracks based on a capture.
  5193. *
  5194. * @module text-track-list-converter
  5195. */
  5196. /**
  5197. * Examine a single {@link TextTrack} and return a JSON-compatible javascript object that
  5198. * represents the {@link TextTrack}'s state.
  5199. *
  5200. * @param {TextTrack} track
  5201. * The text track to query.
  5202. *
  5203. * @return {Object}
  5204. * A serializable javascript representation of the TextTrack.
  5205. * @private
  5206. */
  5207. const trackToJson_ = function (track) {
  5208. const ret = ['kind', 'label', 'language', 'id', 'inBandMetadataTrackDispatchType', 'mode', 'src'].reduce((acc, prop, i) => {
  5209. if (track[prop]) {
  5210. acc[prop] = track[prop];
  5211. }
  5212. return acc;
  5213. }, {
  5214. cues: track.cues && Array.prototype.map.call(track.cues, function (cue) {
  5215. return {
  5216. startTime: cue.startTime,
  5217. endTime: cue.endTime,
  5218. text: cue.text,
  5219. id: cue.id
  5220. };
  5221. })
  5222. });
  5223. return ret;
  5224. };
  5225. /**
  5226. * Examine a {@link Tech} and return a JSON-compatible javascript array that represents the
  5227. * state of all {@link TextTrack}s currently configured. The return array is compatible with
  5228. * {@link text-track-list-converter:jsonToTextTracks}.
  5229. *
  5230. * @param { import('../tech/tech').default } tech
  5231. * The tech object to query
  5232. *
  5233. * @return {Array}
  5234. * A serializable javascript representation of the {@link Tech}s
  5235. * {@link TextTrackList}.
  5236. */
  5237. const textTracksToJson = function (tech) {
  5238. const trackEls = tech.$$('track');
  5239. const trackObjs = Array.prototype.map.call(trackEls, t => t.track);
  5240. const tracks = Array.prototype.map.call(trackEls, function (trackEl) {
  5241. const json = trackToJson_(trackEl.track);
  5242. if (trackEl.src) {
  5243. json.src = trackEl.src;
  5244. }
  5245. return json;
  5246. });
  5247. return tracks.concat(Array.prototype.filter.call(tech.textTracks(), function (track) {
  5248. return trackObjs.indexOf(track) === -1;
  5249. }).map(trackToJson_));
  5250. };
  5251. /**
  5252. * Create a set of remote {@link TextTrack}s on a {@link Tech} based on an array of javascript
  5253. * object {@link TextTrack} representations.
  5254. *
  5255. * @param {Array} json
  5256. * An array of `TextTrack` representation objects, like those that would be
  5257. * produced by `textTracksToJson`.
  5258. *
  5259. * @param {Tech} tech
  5260. * The `Tech` to create the `TextTrack`s on.
  5261. */
  5262. const jsonToTextTracks = function (json, tech) {
  5263. json.forEach(function (track) {
  5264. const addedTrack = tech.addRemoteTextTrack(track).track;
  5265. if (!track.src && track.cues) {
  5266. track.cues.forEach(cue => addedTrack.addCue(cue));
  5267. }
  5268. });
  5269. return tech.textTracks();
  5270. };
  5271. var textTrackConverter = {
  5272. textTracksToJson,
  5273. jsonToTextTracks,
  5274. trackToJson_
  5275. };
  5276. /**
  5277. * @file modal-dialog.js
  5278. */
  5279. const MODAL_CLASS_NAME = 'vjs-modal-dialog';
  5280. /**
  5281. * The `ModalDialog` displays over the video and its controls, which blocks
  5282. * interaction with the player until it is closed.
  5283. *
  5284. * Modal dialogs include a "Close" button and will close when that button
  5285. * is activated - or when ESC is pressed anywhere.
  5286. *
  5287. * @extends Component
  5288. */
  5289. class ModalDialog extends Component {
  5290. /**
  5291. * Create an instance of this class.
  5292. *
  5293. * @param { import('./player').default } player
  5294. * The `Player` that this class should be attached to.
  5295. *
  5296. * @param {Object} [options]
  5297. * The key/value store of player options.
  5298. *
  5299. * @param { import('./utils/dom').ContentDescriptor} [options.content=undefined]
  5300. * Provide customized content for this modal.
  5301. *
  5302. * @param {string} [options.description]
  5303. * A text description for the modal, primarily for accessibility.
  5304. *
  5305. * @param {boolean} [options.fillAlways=false]
  5306. * Normally, modals are automatically filled only the first time
  5307. * they open. This tells the modal to refresh its content
  5308. * every time it opens.
  5309. *
  5310. * @param {string} [options.label]
  5311. * A text label for the modal, primarily for accessibility.
  5312. *
  5313. * @param {boolean} [options.pauseOnOpen=true]
  5314. * If `true`, playback will will be paused if playing when
  5315. * the modal opens, and resumed when it closes.
  5316. *
  5317. * @param {boolean} [options.temporary=true]
  5318. * If `true`, the modal can only be opened once; it will be
  5319. * disposed as soon as it's closed.
  5320. *
  5321. * @param {boolean} [options.uncloseable=false]
  5322. * If `true`, the user will not be able to close the modal
  5323. * through the UI in the normal ways. Programmatic closing is
  5324. * still possible.
  5325. */
  5326. constructor(player, options) {
  5327. super(player, options);
  5328. this.handleKeyDown_ = e => this.handleKeyDown(e);
  5329. this.close_ = e => this.close(e);
  5330. this.opened_ = this.hasBeenOpened_ = this.hasBeenFilled_ = false;
  5331. this.closeable(!this.options_.uncloseable);
  5332. this.content(this.options_.content);
  5333. // Make sure the contentEl is defined AFTER any children are initialized
  5334. // because we only want the contents of the modal in the contentEl
  5335. // (not the UI elements like the close button).
  5336. this.contentEl_ = createEl('div', {
  5337. className: `${MODAL_CLASS_NAME}-content`
  5338. }, {
  5339. role: 'document'
  5340. });
  5341. this.descEl_ = createEl('p', {
  5342. className: `${MODAL_CLASS_NAME}-description vjs-control-text`,
  5343. id: this.el().getAttribute('aria-describedby')
  5344. });
  5345. textContent(this.descEl_, this.description());
  5346. this.el_.appendChild(this.descEl_);
  5347. this.el_.appendChild(this.contentEl_);
  5348. }
  5349. /**
  5350. * Create the `ModalDialog`'s DOM element
  5351. *
  5352. * @return {Element}
  5353. * The DOM element that gets created.
  5354. */
  5355. createEl() {
  5356. return super.createEl('div', {
  5357. className: this.buildCSSClass(),
  5358. tabIndex: -1
  5359. }, {
  5360. 'aria-describedby': `${this.id()}_description`,
  5361. 'aria-hidden': 'true',
  5362. 'aria-label': this.label(),
  5363. 'role': 'dialog'
  5364. });
  5365. }
  5366. dispose() {
  5367. this.contentEl_ = null;
  5368. this.descEl_ = null;
  5369. this.previouslyActiveEl_ = null;
  5370. super.dispose();
  5371. }
  5372. /**
  5373. * Builds the default DOM `className`.
  5374. *
  5375. * @return {string}
  5376. * The DOM `className` for this object.
  5377. */
  5378. buildCSSClass() {
  5379. return `${MODAL_CLASS_NAME} vjs-hidden ${super.buildCSSClass()}`;
  5380. }
  5381. /**
  5382. * Returns the label string for this modal. Primarily used for accessibility.
  5383. *
  5384. * @return {string}
  5385. * the localized or raw label of this modal.
  5386. */
  5387. label() {
  5388. return this.localize(this.options_.label || 'Modal Window');
  5389. }
  5390. /**
  5391. * Returns the description string for this modal. Primarily used for
  5392. * accessibility.
  5393. *
  5394. * @return {string}
  5395. * The localized or raw description of this modal.
  5396. */
  5397. description() {
  5398. let desc = this.options_.description || this.localize('This is a modal window.');
  5399. // Append a universal closeability message if the modal is closeable.
  5400. if (this.closeable()) {
  5401. desc += ' ' + this.localize('This modal can be closed by pressing the Escape key or activating the close button.');
  5402. }
  5403. return desc;
  5404. }
  5405. /**
  5406. * Opens the modal.
  5407. *
  5408. * @fires ModalDialog#beforemodalopen
  5409. * @fires ModalDialog#modalopen
  5410. */
  5411. open() {
  5412. if (!this.opened_) {
  5413. const player = this.player();
  5414. /**
  5415. * Fired just before a `ModalDialog` is opened.
  5416. *
  5417. * @event ModalDialog#beforemodalopen
  5418. * @type {Event}
  5419. */
  5420. this.trigger('beforemodalopen');
  5421. this.opened_ = true;
  5422. // Fill content if the modal has never opened before and
  5423. // never been filled.
  5424. if (this.options_.fillAlways || !this.hasBeenOpened_ && !this.hasBeenFilled_) {
  5425. this.fill();
  5426. }
  5427. // If the player was playing, pause it and take note of its previously
  5428. // playing state.
  5429. this.wasPlaying_ = !player.paused();
  5430. if (this.options_.pauseOnOpen && this.wasPlaying_) {
  5431. player.pause();
  5432. }
  5433. this.on('keydown', this.handleKeyDown_);
  5434. // Hide controls and note if they were enabled.
  5435. this.hadControls_ = player.controls();
  5436. player.controls(false);
  5437. this.show();
  5438. this.conditionalFocus_();
  5439. this.el().setAttribute('aria-hidden', 'false');
  5440. /**
  5441. * Fired just after a `ModalDialog` is opened.
  5442. *
  5443. * @event ModalDialog#modalopen
  5444. * @type {Event}
  5445. */
  5446. this.trigger('modalopen');
  5447. this.hasBeenOpened_ = true;
  5448. }
  5449. }
  5450. /**
  5451. * If the `ModalDialog` is currently open or closed.
  5452. *
  5453. * @param {boolean} [value]
  5454. * If given, it will open (`true`) or close (`false`) the modal.
  5455. *
  5456. * @return {boolean}
  5457. * the current open state of the modaldialog
  5458. */
  5459. opened(value) {
  5460. if (typeof value === 'boolean') {
  5461. this[value ? 'open' : 'close']();
  5462. }
  5463. return this.opened_;
  5464. }
  5465. /**
  5466. * Closes the modal, does nothing if the `ModalDialog` is
  5467. * not open.
  5468. *
  5469. * @fires ModalDialog#beforemodalclose
  5470. * @fires ModalDialog#modalclose
  5471. */
  5472. close() {
  5473. if (!this.opened_) {
  5474. return;
  5475. }
  5476. const player = this.player();
  5477. /**
  5478. * Fired just before a `ModalDialog` is closed.
  5479. *
  5480. * @event ModalDialog#beforemodalclose
  5481. * @type {Event}
  5482. */
  5483. this.trigger('beforemodalclose');
  5484. this.opened_ = false;
  5485. if (this.wasPlaying_ && this.options_.pauseOnOpen) {
  5486. player.play();
  5487. }
  5488. this.off('keydown', this.handleKeyDown_);
  5489. if (this.hadControls_) {
  5490. player.controls(true);
  5491. }
  5492. this.hide();
  5493. this.el().setAttribute('aria-hidden', 'true');
  5494. /**
  5495. * Fired just after a `ModalDialog` is closed.
  5496. *
  5497. * @event ModalDialog#modalclose
  5498. * @type {Event}
  5499. */
  5500. this.trigger('modalclose');
  5501. this.conditionalBlur_();
  5502. if (this.options_.temporary) {
  5503. this.dispose();
  5504. }
  5505. }
  5506. /**
  5507. * Check to see if the `ModalDialog` is closeable via the UI.
  5508. *
  5509. * @param {boolean} [value]
  5510. * If given as a boolean, it will set the `closeable` option.
  5511. *
  5512. * @return {boolean}
  5513. * Returns the final value of the closable option.
  5514. */
  5515. closeable(value) {
  5516. if (typeof value === 'boolean') {
  5517. const closeable = this.closeable_ = !!value;
  5518. let close = this.getChild('closeButton');
  5519. // If this is being made closeable and has no close button, add one.
  5520. if (closeable && !close) {
  5521. // The close button should be a child of the modal - not its
  5522. // content element, so temporarily change the content element.
  5523. const temp = this.contentEl_;
  5524. this.contentEl_ = this.el_;
  5525. close = this.addChild('closeButton', {
  5526. controlText: 'Close Modal Dialog'
  5527. });
  5528. this.contentEl_ = temp;
  5529. this.on(close, 'close', this.close_);
  5530. }
  5531. // If this is being made uncloseable and has a close button, remove it.
  5532. if (!closeable && close) {
  5533. this.off(close, 'close', this.close_);
  5534. this.removeChild(close);
  5535. close.dispose();
  5536. }
  5537. }
  5538. return this.closeable_;
  5539. }
  5540. /**
  5541. * Fill the modal's content element with the modal's "content" option.
  5542. * The content element will be emptied before this change takes place.
  5543. */
  5544. fill() {
  5545. this.fillWith(this.content());
  5546. }
  5547. /**
  5548. * Fill the modal's content element with arbitrary content.
  5549. * The content element will be emptied before this change takes place.
  5550. *
  5551. * @fires ModalDialog#beforemodalfill
  5552. * @fires ModalDialog#modalfill
  5553. *
  5554. * @param { import('./utils/dom').ContentDescriptor} [content]
  5555. * The same rules apply to this as apply to the `content` option.
  5556. */
  5557. fillWith(content) {
  5558. const contentEl = this.contentEl();
  5559. const parentEl = contentEl.parentNode;
  5560. const nextSiblingEl = contentEl.nextSibling;
  5561. /**
  5562. * Fired just before a `ModalDialog` is filled with content.
  5563. *
  5564. * @event ModalDialog#beforemodalfill
  5565. * @type {Event}
  5566. */
  5567. this.trigger('beforemodalfill');
  5568. this.hasBeenFilled_ = true;
  5569. // Detach the content element from the DOM before performing
  5570. // manipulation to avoid modifying the live DOM multiple times.
  5571. parentEl.removeChild(contentEl);
  5572. this.empty();
  5573. insertContent(contentEl, content);
  5574. /**
  5575. * Fired just after a `ModalDialog` is filled with content.
  5576. *
  5577. * @event ModalDialog#modalfill
  5578. * @type {Event}
  5579. */
  5580. this.trigger('modalfill');
  5581. // Re-inject the re-filled content element.
  5582. if (nextSiblingEl) {
  5583. parentEl.insertBefore(contentEl, nextSiblingEl);
  5584. } else {
  5585. parentEl.appendChild(contentEl);
  5586. }
  5587. // make sure that the close button is last in the dialog DOM
  5588. const closeButton = this.getChild('closeButton');
  5589. if (closeButton) {
  5590. parentEl.appendChild(closeButton.el_);
  5591. }
  5592. }
  5593. /**
  5594. * Empties the content element. This happens anytime the modal is filled.
  5595. *
  5596. * @fires ModalDialog#beforemodalempty
  5597. * @fires ModalDialog#modalempty
  5598. */
  5599. empty() {
  5600. /**
  5601. * Fired just before a `ModalDialog` is emptied.
  5602. *
  5603. * @event ModalDialog#beforemodalempty
  5604. * @type {Event}
  5605. */
  5606. this.trigger('beforemodalempty');
  5607. emptyEl(this.contentEl());
  5608. /**
  5609. * Fired just after a `ModalDialog` is emptied.
  5610. *
  5611. * @event ModalDialog#modalempty
  5612. * @type {Event}
  5613. */
  5614. this.trigger('modalempty');
  5615. }
  5616. /**
  5617. * Gets or sets the modal content, which gets normalized before being
  5618. * rendered into the DOM.
  5619. *
  5620. * This does not update the DOM or fill the modal, but it is called during
  5621. * that process.
  5622. *
  5623. * @param { import('./utils/dom').ContentDescriptor} [value]
  5624. * If defined, sets the internal content value to be used on the
  5625. * next call(s) to `fill`. This value is normalized before being
  5626. * inserted. To "clear" the internal content value, pass `null`.
  5627. *
  5628. * @return { import('./utils/dom').ContentDescriptor}
  5629. * The current content of the modal dialog
  5630. */
  5631. content(value) {
  5632. if (typeof value !== 'undefined') {
  5633. this.content_ = value;
  5634. }
  5635. return this.content_;
  5636. }
  5637. /**
  5638. * conditionally focus the modal dialog if focus was previously on the player.
  5639. *
  5640. * @private
  5641. */
  5642. conditionalFocus_() {
  5643. const activeEl = document.activeElement;
  5644. const playerEl = this.player_.el_;
  5645. this.previouslyActiveEl_ = null;
  5646. if (playerEl.contains(activeEl) || playerEl === activeEl) {
  5647. this.previouslyActiveEl_ = activeEl;
  5648. this.focus();
  5649. }
  5650. }
  5651. /**
  5652. * conditionally blur the element and refocus the last focused element
  5653. *
  5654. * @private
  5655. */
  5656. conditionalBlur_() {
  5657. if (this.previouslyActiveEl_) {
  5658. this.previouslyActiveEl_.focus();
  5659. this.previouslyActiveEl_ = null;
  5660. }
  5661. }
  5662. /**
  5663. * Keydown handler. Attached when modal is focused.
  5664. *
  5665. * @listens keydown
  5666. */
  5667. handleKeyDown(event) {
  5668. // Do not allow keydowns to reach out of the modal dialog.
  5669. event.stopPropagation();
  5670. if (keycode.isEventKey(event, 'Escape') && this.closeable()) {
  5671. event.preventDefault();
  5672. this.close();
  5673. return;
  5674. }
  5675. // exit early if it isn't a tab key
  5676. if (!keycode.isEventKey(event, 'Tab')) {
  5677. return;
  5678. }
  5679. const focusableEls = this.focusableEls_();
  5680. const activeEl = this.el_.querySelector(':focus');
  5681. let focusIndex;
  5682. for (let i = 0; i < focusableEls.length; i++) {
  5683. if (activeEl === focusableEls[i]) {
  5684. focusIndex = i;
  5685. break;
  5686. }
  5687. }
  5688. if (document.activeElement === this.el_) {
  5689. focusIndex = 0;
  5690. }
  5691. if (event.shiftKey && focusIndex === 0) {
  5692. focusableEls[focusableEls.length - 1].focus();
  5693. event.preventDefault();
  5694. } else if (!event.shiftKey && focusIndex === focusableEls.length - 1) {
  5695. focusableEls[0].focus();
  5696. event.preventDefault();
  5697. }
  5698. }
  5699. /**
  5700. * get all focusable elements
  5701. *
  5702. * @private
  5703. */
  5704. focusableEls_() {
  5705. const allChildren = this.el_.querySelectorAll('*');
  5706. return Array.prototype.filter.call(allChildren, child => {
  5707. return (child instanceof window.HTMLAnchorElement || child instanceof window.HTMLAreaElement) && child.hasAttribute('href') || (child instanceof window.HTMLInputElement || child instanceof window.HTMLSelectElement || child instanceof window.HTMLTextAreaElement || child instanceof window.HTMLButtonElement) && !child.hasAttribute('disabled') || child instanceof window.HTMLIFrameElement || child instanceof window.HTMLObjectElement || child instanceof window.HTMLEmbedElement || child.hasAttribute('tabindex') && child.getAttribute('tabindex') !== -1 || child.hasAttribute('contenteditable');
  5708. });
  5709. }
  5710. }
  5711. /**
  5712. * Default options for `ModalDialog` default options.
  5713. *
  5714. * @type {Object}
  5715. * @private
  5716. */
  5717. ModalDialog.prototype.options_ = {
  5718. pauseOnOpen: true,
  5719. temporary: true
  5720. };
  5721. Component.registerComponent('ModalDialog', ModalDialog);
  5722. /**
  5723. * @file track-list.js
  5724. */
  5725. /**
  5726. * Common functionaliy between {@link TextTrackList}, {@link AudioTrackList}, and
  5727. * {@link VideoTrackList}
  5728. *
  5729. * @extends EventTarget
  5730. */
  5731. class TrackList extends EventTarget {
  5732. /**
  5733. * Create an instance of this class
  5734. *
  5735. * @param { import('./track').default[] } tracks
  5736. * A list of tracks to initialize the list with.
  5737. *
  5738. * @abstract
  5739. */
  5740. constructor(tracks = []) {
  5741. super();
  5742. this.tracks_ = [];
  5743. /**
  5744. * @memberof TrackList
  5745. * @member {number} length
  5746. * The current number of `Track`s in the this Trackist.
  5747. * @instance
  5748. */
  5749. Object.defineProperty(this, 'length', {
  5750. get() {
  5751. return this.tracks_.length;
  5752. }
  5753. });
  5754. for (let i = 0; i < tracks.length; i++) {
  5755. this.addTrack(tracks[i]);
  5756. }
  5757. }
  5758. /**
  5759. * Add a {@link Track} to the `TrackList`
  5760. *
  5761. * @param { import('./track').default } track
  5762. * The audio, video, or text track to add to the list.
  5763. *
  5764. * @fires TrackList#addtrack
  5765. */
  5766. addTrack(track) {
  5767. const index = this.tracks_.length;
  5768. if (!('' + index in this)) {
  5769. Object.defineProperty(this, index, {
  5770. get() {
  5771. return this.tracks_[index];
  5772. }
  5773. });
  5774. }
  5775. // Do not add duplicate tracks
  5776. if (this.tracks_.indexOf(track) === -1) {
  5777. this.tracks_.push(track);
  5778. /**
  5779. * Triggered when a track is added to a track list.
  5780. *
  5781. * @event TrackList#addtrack
  5782. * @type {Event}
  5783. * @property {Track} track
  5784. * A reference to track that was added.
  5785. */
  5786. this.trigger({
  5787. track,
  5788. type: 'addtrack',
  5789. target: this
  5790. });
  5791. }
  5792. /**
  5793. * Triggered when a track label is changed.
  5794. *
  5795. * @event TrackList#addtrack
  5796. * @type {Event}
  5797. * @property {Track} track
  5798. * A reference to track that was added.
  5799. */
  5800. track.labelchange_ = () => {
  5801. this.trigger({
  5802. track,
  5803. type: 'labelchange',
  5804. target: this
  5805. });
  5806. };
  5807. if (isEvented(track)) {
  5808. track.addEventListener('labelchange', track.labelchange_);
  5809. }
  5810. }
  5811. /**
  5812. * Remove a {@link Track} from the `TrackList`
  5813. *
  5814. * @param { import('./track').default } rtrack
  5815. * The audio, video, or text track to remove from the list.
  5816. *
  5817. * @fires TrackList#removetrack
  5818. */
  5819. removeTrack(rtrack) {
  5820. let track;
  5821. for (let i = 0, l = this.length; i < l; i++) {
  5822. if (this[i] === rtrack) {
  5823. track = this[i];
  5824. if (track.off) {
  5825. track.off();
  5826. }
  5827. this.tracks_.splice(i, 1);
  5828. break;
  5829. }
  5830. }
  5831. if (!track) {
  5832. return;
  5833. }
  5834. /**
  5835. * Triggered when a track is removed from track list.
  5836. *
  5837. * @event TrackList#removetrack
  5838. * @type {Event}
  5839. * @property {Track} track
  5840. * A reference to track that was removed.
  5841. */
  5842. this.trigger({
  5843. track,
  5844. type: 'removetrack',
  5845. target: this
  5846. });
  5847. }
  5848. /**
  5849. * Get a Track from the TrackList by a tracks id
  5850. *
  5851. * @param {string} id - the id of the track to get
  5852. * @method getTrackById
  5853. * @return { import('./track').default }
  5854. * @private
  5855. */
  5856. getTrackById(id) {
  5857. let result = null;
  5858. for (let i = 0, l = this.length; i < l; i++) {
  5859. const track = this[i];
  5860. if (track.id === id) {
  5861. result = track;
  5862. break;
  5863. }
  5864. }
  5865. return result;
  5866. }
  5867. }
  5868. /**
  5869. * Triggered when a different track is selected/enabled.
  5870. *
  5871. * @event TrackList#change
  5872. * @type {Event}
  5873. */
  5874. /**
  5875. * Events that can be called with on + eventName. See {@link EventHandler}.
  5876. *
  5877. * @property {Object} TrackList#allowedEvents_
  5878. * @protected
  5879. */
  5880. TrackList.prototype.allowedEvents_ = {
  5881. change: 'change',
  5882. addtrack: 'addtrack',
  5883. removetrack: 'removetrack',
  5884. labelchange: 'labelchange'
  5885. };
  5886. // emulate attribute EventHandler support to allow for feature detection
  5887. for (const event in TrackList.prototype.allowedEvents_) {
  5888. TrackList.prototype['on' + event] = null;
  5889. }
  5890. /**
  5891. * @file audio-track-list.js
  5892. */
  5893. /**
  5894. * Anywhere we call this function we diverge from the spec
  5895. * as we only support one enabled audiotrack at a time
  5896. *
  5897. * @param {AudioTrackList} list
  5898. * list to work on
  5899. *
  5900. * @param { import('./audio-track').default } track
  5901. * The track to skip
  5902. *
  5903. * @private
  5904. */
  5905. const disableOthers$1 = function (list, track) {
  5906. for (let i = 0; i < list.length; i++) {
  5907. if (!Object.keys(list[i]).length || track.id === list[i].id) {
  5908. continue;
  5909. }
  5910. // another audio track is enabled, disable it
  5911. list[i].enabled = false;
  5912. }
  5913. };
  5914. /**
  5915. * The current list of {@link AudioTrack} for a media file.
  5916. *
  5917. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotracklist}
  5918. * @extends TrackList
  5919. */
  5920. class AudioTrackList extends TrackList {
  5921. /**
  5922. * Create an instance of this class.
  5923. *
  5924. * @param { import('./audio-track').default[] } [tracks=[]]
  5925. * A list of `AudioTrack` to instantiate the list with.
  5926. */
  5927. constructor(tracks = []) {
  5928. // make sure only 1 track is enabled
  5929. // sorted from last index to first index
  5930. for (let i = tracks.length - 1; i >= 0; i--) {
  5931. if (tracks[i].enabled) {
  5932. disableOthers$1(tracks, tracks[i]);
  5933. break;
  5934. }
  5935. }
  5936. super(tracks);
  5937. this.changing_ = false;
  5938. }
  5939. /**
  5940. * Add an {@link AudioTrack} to the `AudioTrackList`.
  5941. *
  5942. * @param { import('./audio-track').default } track
  5943. * The AudioTrack to add to the list
  5944. *
  5945. * @fires TrackList#addtrack
  5946. */
  5947. addTrack(track) {
  5948. if (track.enabled) {
  5949. disableOthers$1(this, track);
  5950. }
  5951. super.addTrack(track);
  5952. // native tracks don't have this
  5953. if (!track.addEventListener) {
  5954. return;
  5955. }
  5956. track.enabledChange_ = () => {
  5957. // when we are disabling other tracks (since we don't support
  5958. // more than one track at a time) we will set changing_
  5959. // to true so that we don't trigger additional change events
  5960. if (this.changing_) {
  5961. return;
  5962. }
  5963. this.changing_ = true;
  5964. disableOthers$1(this, track);
  5965. this.changing_ = false;
  5966. this.trigger('change');
  5967. };
  5968. /**
  5969. * @listens AudioTrack#enabledchange
  5970. * @fires TrackList#change
  5971. */
  5972. track.addEventListener('enabledchange', track.enabledChange_);
  5973. }
  5974. removeTrack(rtrack) {
  5975. super.removeTrack(rtrack);
  5976. if (rtrack.removeEventListener && rtrack.enabledChange_) {
  5977. rtrack.removeEventListener('enabledchange', rtrack.enabledChange_);
  5978. rtrack.enabledChange_ = null;
  5979. }
  5980. }
  5981. }
  5982. /**
  5983. * @file video-track-list.js
  5984. */
  5985. /**
  5986. * Un-select all other {@link VideoTrack}s that are selected.
  5987. *
  5988. * @param {VideoTrackList} list
  5989. * list to work on
  5990. *
  5991. * @param { import('./video-track').default } track
  5992. * The track to skip
  5993. *
  5994. * @private
  5995. */
  5996. const disableOthers = function (list, track) {
  5997. for (let i = 0; i < list.length; i++) {
  5998. if (!Object.keys(list[i]).length || track.id === list[i].id) {
  5999. continue;
  6000. }
  6001. // another video track is enabled, disable it
  6002. list[i].selected = false;
  6003. }
  6004. };
  6005. /**
  6006. * The current list of {@link VideoTrack} for a video.
  6007. *
  6008. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#videotracklist}
  6009. * @extends TrackList
  6010. */
  6011. class VideoTrackList extends TrackList {
  6012. /**
  6013. * Create an instance of this class.
  6014. *
  6015. * @param {VideoTrack[]} [tracks=[]]
  6016. * A list of `VideoTrack` to instantiate the list with.
  6017. */
  6018. constructor(tracks = []) {
  6019. // make sure only 1 track is enabled
  6020. // sorted from last index to first index
  6021. for (let i = tracks.length - 1; i >= 0; i--) {
  6022. if (tracks[i].selected) {
  6023. disableOthers(tracks, tracks[i]);
  6024. break;
  6025. }
  6026. }
  6027. super(tracks);
  6028. this.changing_ = false;
  6029. /**
  6030. * @member {number} VideoTrackList#selectedIndex
  6031. * The current index of the selected {@link VideoTrack`}.
  6032. */
  6033. Object.defineProperty(this, 'selectedIndex', {
  6034. get() {
  6035. for (let i = 0; i < this.length; i++) {
  6036. if (this[i].selected) {
  6037. return i;
  6038. }
  6039. }
  6040. return -1;
  6041. },
  6042. set() {}
  6043. });
  6044. }
  6045. /**
  6046. * Add a {@link VideoTrack} to the `VideoTrackList`.
  6047. *
  6048. * @param { import('./video-track').default } track
  6049. * The VideoTrack to add to the list
  6050. *
  6051. * @fires TrackList#addtrack
  6052. */
  6053. addTrack(track) {
  6054. if (track.selected) {
  6055. disableOthers(this, track);
  6056. }
  6057. super.addTrack(track);
  6058. // native tracks don't have this
  6059. if (!track.addEventListener) {
  6060. return;
  6061. }
  6062. track.selectedChange_ = () => {
  6063. if (this.changing_) {
  6064. return;
  6065. }
  6066. this.changing_ = true;
  6067. disableOthers(this, track);
  6068. this.changing_ = false;
  6069. this.trigger('change');
  6070. };
  6071. /**
  6072. * @listens VideoTrack#selectedchange
  6073. * @fires TrackList#change
  6074. */
  6075. track.addEventListener('selectedchange', track.selectedChange_);
  6076. }
  6077. removeTrack(rtrack) {
  6078. super.removeTrack(rtrack);
  6079. if (rtrack.removeEventListener && rtrack.selectedChange_) {
  6080. rtrack.removeEventListener('selectedchange', rtrack.selectedChange_);
  6081. rtrack.selectedChange_ = null;
  6082. }
  6083. }
  6084. }
  6085. /**
  6086. * @file text-track-list.js
  6087. */
  6088. /**
  6089. * The current list of {@link TextTrack} for a media file.
  6090. *
  6091. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttracklist}
  6092. * @extends TrackList
  6093. */
  6094. class TextTrackList extends TrackList {
  6095. /**
  6096. * Add a {@link TextTrack} to the `TextTrackList`
  6097. *
  6098. * @param { import('./text-track').default } track
  6099. * The text track to add to the list.
  6100. *
  6101. * @fires TrackList#addtrack
  6102. */
  6103. addTrack(track) {
  6104. super.addTrack(track);
  6105. if (!this.queueChange_) {
  6106. this.queueChange_ = () => this.queueTrigger('change');
  6107. }
  6108. if (!this.triggerSelectedlanguagechange) {
  6109. this.triggerSelectedlanguagechange_ = () => this.trigger('selectedlanguagechange');
  6110. }
  6111. /**
  6112. * @listens TextTrack#modechange
  6113. * @fires TrackList#change
  6114. */
  6115. track.addEventListener('modechange', this.queueChange_);
  6116. const nonLanguageTextTrackKind = ['metadata', 'chapters'];
  6117. if (nonLanguageTextTrackKind.indexOf(track.kind) === -1) {
  6118. track.addEventListener('modechange', this.triggerSelectedlanguagechange_);
  6119. }
  6120. }
  6121. removeTrack(rtrack) {
  6122. super.removeTrack(rtrack);
  6123. // manually remove the event handlers we added
  6124. if (rtrack.removeEventListener) {
  6125. if (this.queueChange_) {
  6126. rtrack.removeEventListener('modechange', this.queueChange_);
  6127. }
  6128. if (this.selectedlanguagechange_) {
  6129. rtrack.removeEventListener('modechange', this.triggerSelectedlanguagechange_);
  6130. }
  6131. }
  6132. }
  6133. }
  6134. /**
  6135. * @file html-track-element-list.js
  6136. */
  6137. /**
  6138. * The current list of {@link HtmlTrackElement}s.
  6139. */
  6140. class HtmlTrackElementList {
  6141. /**
  6142. * Create an instance of this class.
  6143. *
  6144. * @param {HtmlTrackElement[]} [tracks=[]]
  6145. * A list of `HtmlTrackElement` to instantiate the list with.
  6146. */
  6147. constructor(trackElements = []) {
  6148. this.trackElements_ = [];
  6149. /**
  6150. * @memberof HtmlTrackElementList
  6151. * @member {number} length
  6152. * The current number of `Track`s in the this Trackist.
  6153. * @instance
  6154. */
  6155. Object.defineProperty(this, 'length', {
  6156. get() {
  6157. return this.trackElements_.length;
  6158. }
  6159. });
  6160. for (let i = 0, length = trackElements.length; i < length; i++) {
  6161. this.addTrackElement_(trackElements[i]);
  6162. }
  6163. }
  6164. /**
  6165. * Add an {@link HtmlTrackElement} to the `HtmlTrackElementList`
  6166. *
  6167. * @param {HtmlTrackElement} trackElement
  6168. * The track element to add to the list.
  6169. *
  6170. * @private
  6171. */
  6172. addTrackElement_(trackElement) {
  6173. const index = this.trackElements_.length;
  6174. if (!('' + index in this)) {
  6175. Object.defineProperty(this, index, {
  6176. get() {
  6177. return this.trackElements_[index];
  6178. }
  6179. });
  6180. }
  6181. // Do not add duplicate elements
  6182. if (this.trackElements_.indexOf(trackElement) === -1) {
  6183. this.trackElements_.push(trackElement);
  6184. }
  6185. }
  6186. /**
  6187. * Get an {@link HtmlTrackElement} from the `HtmlTrackElementList` given an
  6188. * {@link TextTrack}.
  6189. *
  6190. * @param {TextTrack} track
  6191. * The track associated with a track element.
  6192. *
  6193. * @return {HtmlTrackElement|undefined}
  6194. * The track element that was found or undefined.
  6195. *
  6196. * @private
  6197. */
  6198. getTrackElementByTrack_(track) {
  6199. let trackElement_;
  6200. for (let i = 0, length = this.trackElements_.length; i < length; i++) {
  6201. if (track === this.trackElements_[i].track) {
  6202. trackElement_ = this.trackElements_[i];
  6203. break;
  6204. }
  6205. }
  6206. return trackElement_;
  6207. }
  6208. /**
  6209. * Remove a {@link HtmlTrackElement} from the `HtmlTrackElementList`
  6210. *
  6211. * @param {HtmlTrackElement} trackElement
  6212. * The track element to remove from the list.
  6213. *
  6214. * @private
  6215. */
  6216. removeTrackElement_(trackElement) {
  6217. for (let i = 0, length = this.trackElements_.length; i < length; i++) {
  6218. if (trackElement === this.trackElements_[i]) {
  6219. if (this.trackElements_[i].track && typeof this.trackElements_[i].track.off === 'function') {
  6220. this.trackElements_[i].track.off();
  6221. }
  6222. if (typeof this.trackElements_[i].off === 'function') {
  6223. this.trackElements_[i].off();
  6224. }
  6225. this.trackElements_.splice(i, 1);
  6226. break;
  6227. }
  6228. }
  6229. }
  6230. }
  6231. /**
  6232. * @file text-track-cue-list.js
  6233. */
  6234. /**
  6235. * @typedef {Object} TextTrackCueList~TextTrackCue
  6236. *
  6237. * @property {string} id
  6238. * The unique id for this text track cue
  6239. *
  6240. * @property {number} startTime
  6241. * The start time for this text track cue
  6242. *
  6243. * @property {number} endTime
  6244. * The end time for this text track cue
  6245. *
  6246. * @property {boolean} pauseOnExit
  6247. * Pause when the end time is reached if true.
  6248. *
  6249. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackcue}
  6250. */
  6251. /**
  6252. * A List of TextTrackCues.
  6253. *
  6254. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackcuelist}
  6255. */
  6256. class TextTrackCueList {
  6257. /**
  6258. * Create an instance of this class..
  6259. *
  6260. * @param {Array} cues
  6261. * A list of cues to be initialized with
  6262. */
  6263. constructor(cues) {
  6264. TextTrackCueList.prototype.setCues_.call(this, cues);
  6265. /**
  6266. * @memberof TextTrackCueList
  6267. * @member {number} length
  6268. * The current number of `TextTrackCue`s in the TextTrackCueList.
  6269. * @instance
  6270. */
  6271. Object.defineProperty(this, 'length', {
  6272. get() {
  6273. return this.length_;
  6274. }
  6275. });
  6276. }
  6277. /**
  6278. * A setter for cues in this list. Creates getters
  6279. * an an index for the cues.
  6280. *
  6281. * @param {Array} cues
  6282. * An array of cues to set
  6283. *
  6284. * @private
  6285. */
  6286. setCues_(cues) {
  6287. const oldLength = this.length || 0;
  6288. let i = 0;
  6289. const l = cues.length;
  6290. this.cues_ = cues;
  6291. this.length_ = cues.length;
  6292. const defineProp = function (index) {
  6293. if (!('' + index in this)) {
  6294. Object.defineProperty(this, '' + index, {
  6295. get() {
  6296. return this.cues_[index];
  6297. }
  6298. });
  6299. }
  6300. };
  6301. if (oldLength < l) {
  6302. i = oldLength;
  6303. for (; i < l; i++) {
  6304. defineProp.call(this, i);
  6305. }
  6306. }
  6307. }
  6308. /**
  6309. * Get a `TextTrackCue` that is currently in the `TextTrackCueList` by id.
  6310. *
  6311. * @param {string} id
  6312. * The id of the cue that should be searched for.
  6313. *
  6314. * @return {TextTrackCueList~TextTrackCue|null}
  6315. * A single cue or null if none was found.
  6316. */
  6317. getCueById(id) {
  6318. let result = null;
  6319. for (let i = 0, l = this.length; i < l; i++) {
  6320. const cue = this[i];
  6321. if (cue.id === id) {
  6322. result = cue;
  6323. break;
  6324. }
  6325. }
  6326. return result;
  6327. }
  6328. }
  6329. /**
  6330. * @file track-kinds.js
  6331. */
  6332. /**
  6333. * All possible `VideoTrackKind`s
  6334. *
  6335. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-videotrack-kind
  6336. * @typedef VideoTrack~Kind
  6337. * @enum
  6338. */
  6339. const VideoTrackKind = {
  6340. alternative: 'alternative',
  6341. captions: 'captions',
  6342. main: 'main',
  6343. sign: 'sign',
  6344. subtitles: 'subtitles',
  6345. commentary: 'commentary'
  6346. };
  6347. /**
  6348. * All possible `AudioTrackKind`s
  6349. *
  6350. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-audiotrack-kind
  6351. * @typedef AudioTrack~Kind
  6352. * @enum
  6353. */
  6354. const AudioTrackKind = {
  6355. 'alternative': 'alternative',
  6356. 'descriptions': 'descriptions',
  6357. 'main': 'main',
  6358. 'main-desc': 'main-desc',
  6359. 'translation': 'translation',
  6360. 'commentary': 'commentary'
  6361. };
  6362. /**
  6363. * All possible `TextTrackKind`s
  6364. *
  6365. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-texttrack-kind
  6366. * @typedef TextTrack~Kind
  6367. * @enum
  6368. */
  6369. const TextTrackKind = {
  6370. subtitles: 'subtitles',
  6371. captions: 'captions',
  6372. descriptions: 'descriptions',
  6373. chapters: 'chapters',
  6374. metadata: 'metadata'
  6375. };
  6376. /**
  6377. * All possible `TextTrackMode`s
  6378. *
  6379. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackmode
  6380. * @typedef TextTrack~Mode
  6381. * @enum
  6382. */
  6383. const TextTrackMode = {
  6384. disabled: 'disabled',
  6385. hidden: 'hidden',
  6386. showing: 'showing'
  6387. };
  6388. /**
  6389. * @file track.js
  6390. */
  6391. /**
  6392. * A Track class that contains all of the common functionality for {@link AudioTrack},
  6393. * {@link VideoTrack}, and {@link TextTrack}.
  6394. *
  6395. * > Note: This class should not be used directly
  6396. *
  6397. * @see {@link https://html.spec.whatwg.org/multipage/embedded-content.html}
  6398. * @extends EventTarget
  6399. * @abstract
  6400. */
  6401. class Track extends EventTarget {
  6402. /**
  6403. * Create an instance of this class.
  6404. *
  6405. * @param {Object} [options={}]
  6406. * Object of option names and values
  6407. *
  6408. * @param {string} [options.kind='']
  6409. * A valid kind for the track type you are creating.
  6410. *
  6411. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  6412. * A unique id for this AudioTrack.
  6413. *
  6414. * @param {string} [options.label='']
  6415. * The menu label for this track.
  6416. *
  6417. * @param {string} [options.language='']
  6418. * A valid two character language code.
  6419. *
  6420. * @abstract
  6421. */
  6422. constructor(options = {}) {
  6423. super();
  6424. const trackProps = {
  6425. id: options.id || 'vjs_track_' + newGUID(),
  6426. kind: options.kind || '',
  6427. language: options.language || ''
  6428. };
  6429. let label = options.label || '';
  6430. /**
  6431. * @memberof Track
  6432. * @member {string} id
  6433. * The id of this track. Cannot be changed after creation.
  6434. * @instance
  6435. *
  6436. * @readonly
  6437. */
  6438. /**
  6439. * @memberof Track
  6440. * @member {string} kind
  6441. * The kind of track that this is. Cannot be changed after creation.
  6442. * @instance
  6443. *
  6444. * @readonly
  6445. */
  6446. /**
  6447. * @memberof Track
  6448. * @member {string} language
  6449. * The two letter language code for this track. Cannot be changed after
  6450. * creation.
  6451. * @instance
  6452. *
  6453. * @readonly
  6454. */
  6455. for (const key in trackProps) {
  6456. Object.defineProperty(this, key, {
  6457. get() {
  6458. return trackProps[key];
  6459. },
  6460. set() {}
  6461. });
  6462. }
  6463. /**
  6464. * @memberof Track
  6465. * @member {string} label
  6466. * The label of this track. Cannot be changed after creation.
  6467. * @instance
  6468. *
  6469. * @fires Track#labelchange
  6470. */
  6471. Object.defineProperty(this, 'label', {
  6472. get() {
  6473. return label;
  6474. },
  6475. set(newLabel) {
  6476. if (newLabel !== label) {
  6477. label = newLabel;
  6478. /**
  6479. * An event that fires when label changes on this track.
  6480. *
  6481. * > Note: This is not part of the spec!
  6482. *
  6483. * @event Track#labelchange
  6484. * @type {Event}
  6485. */
  6486. this.trigger('labelchange');
  6487. }
  6488. }
  6489. });
  6490. }
  6491. }
  6492. /**
  6493. * @file url.js
  6494. * @module url
  6495. */
  6496. /**
  6497. * @typedef {Object} url:URLObject
  6498. *
  6499. * @property {string} protocol
  6500. * The protocol of the url that was parsed.
  6501. *
  6502. * @property {string} hostname
  6503. * The hostname of the url that was parsed.
  6504. *
  6505. * @property {string} port
  6506. * The port of the url that was parsed.
  6507. *
  6508. * @property {string} pathname
  6509. * The pathname of the url that was parsed.
  6510. *
  6511. * @property {string} search
  6512. * The search query of the url that was parsed.
  6513. *
  6514. * @property {string} hash
  6515. * The hash of the url that was parsed.
  6516. *
  6517. * @property {string} host
  6518. * The host of the url that was parsed.
  6519. */
  6520. /**
  6521. * Resolve and parse the elements of a URL.
  6522. *
  6523. * @function
  6524. * @param {String} url
  6525. * The url to parse
  6526. *
  6527. * @return {url:URLObject}
  6528. * An object of url details
  6529. */
  6530. const parseUrl = function (url) {
  6531. // This entire method can be replace with URL once we are able to drop IE11
  6532. const props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
  6533. // add the url to an anchor and let the browser parse the URL
  6534. const a = document.createElement('a');
  6535. a.href = url;
  6536. // Copy the specific URL properties to a new object
  6537. // This is also needed for IE because the anchor loses its
  6538. // properties when it's removed from the dom
  6539. const details = {};
  6540. for (let i = 0; i < props.length; i++) {
  6541. details[props[i]] = a[props[i]];
  6542. }
  6543. // IE adds the port to the host property unlike everyone else. If
  6544. // a port identifier is added for standard ports, strip it.
  6545. if (details.protocol === 'http:') {
  6546. details.host = details.host.replace(/:80$/, '');
  6547. }
  6548. if (details.protocol === 'https:') {
  6549. details.host = details.host.replace(/:443$/, '');
  6550. }
  6551. if (!details.protocol) {
  6552. details.protocol = window.location.protocol;
  6553. }
  6554. /* istanbul ignore if */
  6555. if (!details.host) {
  6556. details.host = window.location.host;
  6557. }
  6558. return details;
  6559. };
  6560. /**
  6561. * Get absolute version of relative URL.
  6562. *
  6563. * @function
  6564. * @param {string} url
  6565. * URL to make absolute
  6566. *
  6567. * @return {string}
  6568. * Absolute URL
  6569. *
  6570. * @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
  6571. */
  6572. const getAbsoluteURL = function (url) {
  6573. // Check if absolute URL
  6574. if (!url.match(/^https?:\/\//)) {
  6575. // Add the url to an anchor and let the browser parse it to convert to an absolute url
  6576. const a = document.createElement('a');
  6577. a.href = url;
  6578. url = a.href;
  6579. }
  6580. return url;
  6581. };
  6582. /**
  6583. * Returns the extension of the passed file name. It will return an empty string
  6584. * if passed an invalid path.
  6585. *
  6586. * @function
  6587. * @param {string} path
  6588. * The fileName path like '/path/to/file.mp4'
  6589. *
  6590. * @return {string}
  6591. * The extension in lower case or an empty string if no
  6592. * extension could be found.
  6593. */
  6594. const getFileExtension = function (path) {
  6595. if (typeof path === 'string') {
  6596. const splitPathRe = /^(\/?)([\s\S]*?)((?:\.{1,2}|[^\/]+?)(\.([^\.\/\?]+)))(?:[\/]*|[\?].*)$/;
  6597. const pathParts = splitPathRe.exec(path);
  6598. if (pathParts) {
  6599. return pathParts.pop().toLowerCase();
  6600. }
  6601. }
  6602. return '';
  6603. };
  6604. /**
  6605. * Returns whether the url passed is a cross domain request or not.
  6606. *
  6607. * @function
  6608. * @param {string} url
  6609. * The url to check.
  6610. *
  6611. * @param {Object} [winLoc]
  6612. * the domain to check the url against, defaults to window.location
  6613. *
  6614. * @param {string} [winLoc.protocol]
  6615. * The window location protocol defaults to window.location.protocol
  6616. *
  6617. * @param {string} [winLoc.host]
  6618. * The window location host defaults to window.location.host
  6619. *
  6620. * @return {boolean}
  6621. * Whether it is a cross domain request or not.
  6622. */
  6623. const isCrossOrigin = function (url, winLoc = window.location) {
  6624. const urlInfo = parseUrl(url);
  6625. // IE8 protocol relative urls will return ':' for protocol
  6626. const srcProtocol = urlInfo.protocol === ':' ? winLoc.protocol : urlInfo.protocol;
  6627. // Check if url is for another domain/origin
  6628. // IE8 doesn't know location.origin, so we won't rely on it here
  6629. const crossOrigin = srcProtocol + urlInfo.host !== winLoc.protocol + winLoc.host;
  6630. return crossOrigin;
  6631. };
  6632. var Url = /*#__PURE__*/Object.freeze({
  6633. __proto__: null,
  6634. parseUrl: parseUrl,
  6635. getAbsoluteURL: getAbsoluteURL,
  6636. getFileExtension: getFileExtension,
  6637. isCrossOrigin: isCrossOrigin
  6638. });
  6639. /**
  6640. * @file text-track.js
  6641. */
  6642. /**
  6643. * Takes a webvtt file contents and parses it into cues
  6644. *
  6645. * @param {string} srcContent
  6646. * webVTT file contents
  6647. *
  6648. * @param {TextTrack} track
  6649. * TextTrack to add cues to. Cues come from the srcContent.
  6650. *
  6651. * @private
  6652. */
  6653. const parseCues = function (srcContent, track) {
  6654. const parser = new window.WebVTT.Parser(window, window.vttjs, window.WebVTT.StringDecoder());
  6655. const errors = [];
  6656. parser.oncue = function (cue) {
  6657. track.addCue(cue);
  6658. };
  6659. parser.onparsingerror = function (error) {
  6660. errors.push(error);
  6661. };
  6662. parser.onflush = function () {
  6663. track.trigger({
  6664. type: 'loadeddata',
  6665. target: track
  6666. });
  6667. };
  6668. parser.parse(srcContent);
  6669. if (errors.length > 0) {
  6670. if (window.console && window.console.groupCollapsed) {
  6671. window.console.groupCollapsed(`Text Track parsing errors for ${track.src}`);
  6672. }
  6673. errors.forEach(error => log.error(error));
  6674. if (window.console && window.console.groupEnd) {
  6675. window.console.groupEnd();
  6676. }
  6677. }
  6678. parser.flush();
  6679. };
  6680. /**
  6681. * Load a `TextTrack` from a specified url.
  6682. *
  6683. * @param {string} src
  6684. * Url to load track from.
  6685. *
  6686. * @param {TextTrack} track
  6687. * Track to add cues to. Comes from the content at the end of `url`.
  6688. *
  6689. * @private
  6690. */
  6691. const loadTrack = function (src, track) {
  6692. const opts = {
  6693. uri: src
  6694. };
  6695. const crossOrigin = isCrossOrigin(src);
  6696. if (crossOrigin) {
  6697. opts.cors = crossOrigin;
  6698. }
  6699. const withCredentials = track.tech_.crossOrigin() === 'use-credentials';
  6700. if (withCredentials) {
  6701. opts.withCredentials = withCredentials;
  6702. }
  6703. XHR(opts, bind_(this, function (err, response, responseBody) {
  6704. if (err) {
  6705. return log.error(err, response);
  6706. }
  6707. track.loaded_ = true;
  6708. // Make sure that vttjs has loaded, otherwise, wait till it finished loading
  6709. // NOTE: this is only used for the alt/video.novtt.js build
  6710. if (typeof window.WebVTT !== 'function') {
  6711. if (track.tech_) {
  6712. // to prevent use before define eslint error, we define loadHandler
  6713. // as a let here
  6714. track.tech_.any(['vttjsloaded', 'vttjserror'], event => {
  6715. if (event.type === 'vttjserror') {
  6716. log.error(`vttjs failed to load, stopping trying to process ${track.src}`);
  6717. return;
  6718. }
  6719. return parseCues(responseBody, track);
  6720. });
  6721. }
  6722. } else {
  6723. parseCues(responseBody, track);
  6724. }
  6725. }));
  6726. };
  6727. /**
  6728. * A representation of a single `TextTrack`.
  6729. *
  6730. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrack}
  6731. * @extends Track
  6732. */
  6733. class TextTrack extends Track {
  6734. /**
  6735. * Create an instance of this class.
  6736. *
  6737. * @param {Object} options={}
  6738. * Object of option names and values
  6739. *
  6740. * @param { import('../tech/tech').default } options.tech
  6741. * A reference to the tech that owns this TextTrack.
  6742. *
  6743. * @param {TextTrack~Kind} [options.kind='subtitles']
  6744. * A valid text track kind.
  6745. *
  6746. * @param {TextTrack~Mode} [options.mode='disabled']
  6747. * A valid text track mode.
  6748. *
  6749. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  6750. * A unique id for this TextTrack.
  6751. *
  6752. * @param {string} [options.label='']
  6753. * The menu label for this track.
  6754. *
  6755. * @param {string} [options.language='']
  6756. * A valid two character language code.
  6757. *
  6758. * @param {string} [options.srclang='']
  6759. * A valid two character language code. An alternative, but deprioritized
  6760. * version of `options.language`
  6761. *
  6762. * @param {string} [options.src]
  6763. * A url to TextTrack cues.
  6764. *
  6765. * @param {boolean} [options.default]
  6766. * If this track should default to on or off.
  6767. */
  6768. constructor(options = {}) {
  6769. if (!options.tech) {
  6770. throw new Error('A tech was not provided.');
  6771. }
  6772. const settings = merge(options, {
  6773. kind: TextTrackKind[options.kind] || 'subtitles',
  6774. language: options.language || options.srclang || ''
  6775. });
  6776. let mode = TextTrackMode[settings.mode] || 'disabled';
  6777. const default_ = settings.default;
  6778. if (settings.kind === 'metadata' || settings.kind === 'chapters') {
  6779. mode = 'hidden';
  6780. }
  6781. super(settings);
  6782. this.tech_ = settings.tech;
  6783. this.cues_ = [];
  6784. this.activeCues_ = [];
  6785. this.preload_ = this.tech_.preloadTextTracks !== false;
  6786. const cues = new TextTrackCueList(this.cues_);
  6787. const activeCues = new TextTrackCueList(this.activeCues_);
  6788. let changed = false;
  6789. this.timeupdateHandler = bind_(this, function (event = {}) {
  6790. if (this.tech_.isDisposed()) {
  6791. return;
  6792. }
  6793. if (!this.tech_.isReady_) {
  6794. if (event.type !== 'timeupdate') {
  6795. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  6796. }
  6797. return;
  6798. }
  6799. // Accessing this.activeCues for the side-effects of updating itself
  6800. // due to its nature as a getter function. Do not remove or cues will
  6801. // stop updating!
  6802. // Use the setter to prevent deletion from uglify (pure_getters rule)
  6803. this.activeCues = this.activeCues;
  6804. if (changed) {
  6805. this.trigger('cuechange');
  6806. changed = false;
  6807. }
  6808. if (event.type !== 'timeupdate') {
  6809. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  6810. }
  6811. });
  6812. const disposeHandler = () => {
  6813. this.stopTracking();
  6814. };
  6815. this.tech_.one('dispose', disposeHandler);
  6816. if (mode !== 'disabled') {
  6817. this.startTracking();
  6818. }
  6819. Object.defineProperties(this, {
  6820. /**
  6821. * @memberof TextTrack
  6822. * @member {boolean} default
  6823. * If this track was set to be on or off by default. Cannot be changed after
  6824. * creation.
  6825. * @instance
  6826. *
  6827. * @readonly
  6828. */
  6829. default: {
  6830. get() {
  6831. return default_;
  6832. },
  6833. set() {}
  6834. },
  6835. /**
  6836. * @memberof TextTrack
  6837. * @member {string} mode
  6838. * Set the mode of this TextTrack to a valid {@link TextTrack~Mode}. Will
  6839. * not be set if setting to an invalid mode.
  6840. * @instance
  6841. *
  6842. * @fires TextTrack#modechange
  6843. */
  6844. mode: {
  6845. get() {
  6846. return mode;
  6847. },
  6848. set(newMode) {
  6849. if (!TextTrackMode[newMode]) {
  6850. return;
  6851. }
  6852. if (mode === newMode) {
  6853. return;
  6854. }
  6855. mode = newMode;
  6856. if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) {
  6857. // On-demand load.
  6858. loadTrack(this.src, this);
  6859. }
  6860. this.stopTracking();
  6861. if (mode !== 'disabled') {
  6862. this.startTracking();
  6863. }
  6864. /**
  6865. * An event that fires when mode changes on this track. This allows
  6866. * the TextTrackList that holds this track to act accordingly.
  6867. *
  6868. * > Note: This is not part of the spec!
  6869. *
  6870. * @event TextTrack#modechange
  6871. * @type {Event}
  6872. */
  6873. this.trigger('modechange');
  6874. }
  6875. },
  6876. /**
  6877. * @memberof TextTrack
  6878. * @member {TextTrackCueList} cues
  6879. * The text track cue list for this TextTrack.
  6880. * @instance
  6881. */
  6882. cues: {
  6883. get() {
  6884. if (!this.loaded_) {
  6885. return null;
  6886. }
  6887. return cues;
  6888. },
  6889. set() {}
  6890. },
  6891. /**
  6892. * @memberof TextTrack
  6893. * @member {TextTrackCueList} activeCues
  6894. * The list text track cues that are currently active for this TextTrack.
  6895. * @instance
  6896. */
  6897. activeCues: {
  6898. get() {
  6899. if (!this.loaded_) {
  6900. return null;
  6901. }
  6902. // nothing to do
  6903. if (this.cues.length === 0) {
  6904. return activeCues;
  6905. }
  6906. const ct = this.tech_.currentTime();
  6907. const active = [];
  6908. for (let i = 0, l = this.cues.length; i < l; i++) {
  6909. const cue = this.cues[i];
  6910. if (cue.startTime <= ct && cue.endTime >= ct) {
  6911. active.push(cue);
  6912. }
  6913. }
  6914. changed = false;
  6915. if (active.length !== this.activeCues_.length) {
  6916. changed = true;
  6917. } else {
  6918. for (let i = 0; i < active.length; i++) {
  6919. if (this.activeCues_.indexOf(active[i]) === -1) {
  6920. changed = true;
  6921. }
  6922. }
  6923. }
  6924. this.activeCues_ = active;
  6925. activeCues.setCues_(this.activeCues_);
  6926. return activeCues;
  6927. },
  6928. // /!\ Keep this setter empty (see the timeupdate handler above)
  6929. set() {}
  6930. }
  6931. });
  6932. if (settings.src) {
  6933. this.src = settings.src;
  6934. if (!this.preload_) {
  6935. // Tracks will load on-demand.
  6936. // Act like we're loaded for other purposes.
  6937. this.loaded_ = true;
  6938. }
  6939. if (this.preload_ || settings.kind !== 'subtitles' && settings.kind !== 'captions') {
  6940. loadTrack(this.src, this);
  6941. }
  6942. } else {
  6943. this.loaded_ = true;
  6944. }
  6945. }
  6946. startTracking() {
  6947. // More precise cues based on requestVideoFrameCallback with a requestAnimationFram fallback
  6948. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  6949. // Also listen to timeupdate in case rVFC/rAF stops (window in background, audio in video el)
  6950. this.tech_.on('timeupdate', this.timeupdateHandler);
  6951. }
  6952. stopTracking() {
  6953. if (this.rvf_) {
  6954. this.tech_.cancelVideoFrameCallback(this.rvf_);
  6955. this.rvf_ = undefined;
  6956. }
  6957. this.tech_.off('timeupdate', this.timeupdateHandler);
  6958. }
  6959. /**
  6960. * Add a cue to the internal list of cues.
  6961. *
  6962. * @param {TextTrack~Cue} cue
  6963. * The cue to add to our internal list
  6964. */
  6965. addCue(originalCue) {
  6966. let cue = originalCue;
  6967. // Testing if the cue is a VTTCue in a way that survives minification
  6968. if (!('getCueAsHTML' in cue)) {
  6969. cue = new window.vttjs.VTTCue(originalCue.startTime, originalCue.endTime, originalCue.text);
  6970. for (const prop in originalCue) {
  6971. if (!(prop in cue)) {
  6972. cue[prop] = originalCue[prop];
  6973. }
  6974. }
  6975. // make sure that `id` is copied over
  6976. cue.id = originalCue.id;
  6977. cue.originalCue_ = originalCue;
  6978. }
  6979. const tracks = this.tech_.textTracks();
  6980. for (let i = 0; i < tracks.length; i++) {
  6981. if (tracks[i] !== this) {
  6982. tracks[i].removeCue(cue);
  6983. }
  6984. }
  6985. this.cues_.push(cue);
  6986. this.cues.setCues_(this.cues_);
  6987. }
  6988. /**
  6989. * Remove a cue from our internal list
  6990. *
  6991. * @param {TextTrack~Cue} removeCue
  6992. * The cue to remove from our internal list
  6993. */
  6994. removeCue(removeCue) {
  6995. let i = this.cues_.length;
  6996. while (i--) {
  6997. const cue = this.cues_[i];
  6998. if (cue === removeCue || cue.originalCue_ && cue.originalCue_ === removeCue) {
  6999. this.cues_.splice(i, 1);
  7000. this.cues.setCues_(this.cues_);
  7001. break;
  7002. }
  7003. }
  7004. }
  7005. }
  7006. /**
  7007. * cuechange - One or more cues in the track have become active or stopped being active.
  7008. * @protected
  7009. */
  7010. TextTrack.prototype.allowedEvents_ = {
  7011. cuechange: 'cuechange'
  7012. };
  7013. /**
  7014. * A representation of a single `AudioTrack`. If it is part of an {@link AudioTrackList}
  7015. * only one `AudioTrack` in the list will be enabled at a time.
  7016. *
  7017. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotrack}
  7018. * @extends Track
  7019. */
  7020. class AudioTrack extends Track {
  7021. /**
  7022. * Create an instance of this class.
  7023. *
  7024. * @param {Object} [options={}]
  7025. * Object of option names and values
  7026. *
  7027. * @param {AudioTrack~Kind} [options.kind='']
  7028. * A valid audio track kind
  7029. *
  7030. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7031. * A unique id for this AudioTrack.
  7032. *
  7033. * @param {string} [options.label='']
  7034. * The menu label for this track.
  7035. *
  7036. * @param {string} [options.language='']
  7037. * A valid two character language code.
  7038. *
  7039. * @param {boolean} [options.enabled]
  7040. * If this track is the one that is currently playing. If this track is part of
  7041. * an {@link AudioTrackList}, only one {@link AudioTrack} will be enabled.
  7042. */
  7043. constructor(options = {}) {
  7044. const settings = merge(options, {
  7045. kind: AudioTrackKind[options.kind] || ''
  7046. });
  7047. super(settings);
  7048. let enabled = false;
  7049. /**
  7050. * @memberof AudioTrack
  7051. * @member {boolean} enabled
  7052. * If this `AudioTrack` is enabled or not. When setting this will
  7053. * fire {@link AudioTrack#enabledchange} if the state of enabled is changed.
  7054. * @instance
  7055. *
  7056. * @fires VideoTrack#selectedchange
  7057. */
  7058. Object.defineProperty(this, 'enabled', {
  7059. get() {
  7060. return enabled;
  7061. },
  7062. set(newEnabled) {
  7063. // an invalid or unchanged value
  7064. if (typeof newEnabled !== 'boolean' || newEnabled === enabled) {
  7065. return;
  7066. }
  7067. enabled = newEnabled;
  7068. /**
  7069. * An event that fires when enabled changes on this track. This allows
  7070. * the AudioTrackList that holds this track to act accordingly.
  7071. *
  7072. * > Note: This is not part of the spec! Native tracks will do
  7073. * this internally without an event.
  7074. *
  7075. * @event AudioTrack#enabledchange
  7076. * @type {Event}
  7077. */
  7078. this.trigger('enabledchange');
  7079. }
  7080. });
  7081. // if the user sets this track to selected then
  7082. // set selected to that true value otherwise
  7083. // we keep it false
  7084. if (settings.enabled) {
  7085. this.enabled = settings.enabled;
  7086. }
  7087. this.loaded_ = true;
  7088. }
  7089. }
  7090. /**
  7091. * A representation of a single `VideoTrack`.
  7092. *
  7093. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#videotrack}
  7094. * @extends Track
  7095. */
  7096. class VideoTrack extends Track {
  7097. /**
  7098. * Create an instance of this class.
  7099. *
  7100. * @param {Object} [options={}]
  7101. * Object of option names and values
  7102. *
  7103. * @param {string} [options.kind='']
  7104. * A valid {@link VideoTrack~Kind}
  7105. *
  7106. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7107. * A unique id for this AudioTrack.
  7108. *
  7109. * @param {string} [options.label='']
  7110. * The menu label for this track.
  7111. *
  7112. * @param {string} [options.language='']
  7113. * A valid two character language code.
  7114. *
  7115. * @param {boolean} [options.selected]
  7116. * If this track is the one that is currently playing.
  7117. */
  7118. constructor(options = {}) {
  7119. const settings = merge(options, {
  7120. kind: VideoTrackKind[options.kind] || ''
  7121. });
  7122. super(settings);
  7123. let selected = false;
  7124. /**
  7125. * @memberof VideoTrack
  7126. * @member {boolean} selected
  7127. * If this `VideoTrack` is selected or not. When setting this will
  7128. * fire {@link VideoTrack#selectedchange} if the state of selected changed.
  7129. * @instance
  7130. *
  7131. * @fires VideoTrack#selectedchange
  7132. */
  7133. Object.defineProperty(this, 'selected', {
  7134. get() {
  7135. return selected;
  7136. },
  7137. set(newSelected) {
  7138. // an invalid or unchanged value
  7139. if (typeof newSelected !== 'boolean' || newSelected === selected) {
  7140. return;
  7141. }
  7142. selected = newSelected;
  7143. /**
  7144. * An event that fires when selected changes on this track. This allows
  7145. * the VideoTrackList that holds this track to act accordingly.
  7146. *
  7147. * > Note: This is not part of the spec! Native tracks will do
  7148. * this internally without an event.
  7149. *
  7150. * @event VideoTrack#selectedchange
  7151. * @type {Event}
  7152. */
  7153. this.trigger('selectedchange');
  7154. }
  7155. });
  7156. // if the user sets this track to selected then
  7157. // set selected to that true value otherwise
  7158. // we keep it false
  7159. if (settings.selected) {
  7160. this.selected = settings.selected;
  7161. }
  7162. }
  7163. }
  7164. /**
  7165. * @file html-track-element.js
  7166. */
  7167. /**
  7168. * A single track represented in the DOM.
  7169. *
  7170. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#htmltrackelement}
  7171. * @extends EventTarget
  7172. */
  7173. class HTMLTrackElement extends EventTarget {
  7174. /**
  7175. * Create an instance of this class.
  7176. *
  7177. * @param {Object} options={}
  7178. * Object of option names and values
  7179. *
  7180. * @param { import('../tech/tech').default } options.tech
  7181. * A reference to the tech that owns this HTMLTrackElement.
  7182. *
  7183. * @param {TextTrack~Kind} [options.kind='subtitles']
  7184. * A valid text track kind.
  7185. *
  7186. * @param {TextTrack~Mode} [options.mode='disabled']
  7187. * A valid text track mode.
  7188. *
  7189. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7190. * A unique id for this TextTrack.
  7191. *
  7192. * @param {string} [options.label='']
  7193. * The menu label for this track.
  7194. *
  7195. * @param {string} [options.language='']
  7196. * A valid two character language code.
  7197. *
  7198. * @param {string} [options.srclang='']
  7199. * A valid two character language code. An alternative, but deprioritized
  7200. * version of `options.language`
  7201. *
  7202. * @param {string} [options.src]
  7203. * A url to TextTrack cues.
  7204. *
  7205. * @param {boolean} [options.default]
  7206. * If this track should default to on or off.
  7207. */
  7208. constructor(options = {}) {
  7209. super();
  7210. let readyState;
  7211. const track = new TextTrack(options);
  7212. this.kind = track.kind;
  7213. this.src = track.src;
  7214. this.srclang = track.language;
  7215. this.label = track.label;
  7216. this.default = track.default;
  7217. Object.defineProperties(this, {
  7218. /**
  7219. * @memberof HTMLTrackElement
  7220. * @member {HTMLTrackElement~ReadyState} readyState
  7221. * The current ready state of the track element.
  7222. * @instance
  7223. */
  7224. readyState: {
  7225. get() {
  7226. return readyState;
  7227. }
  7228. },
  7229. /**
  7230. * @memberof HTMLTrackElement
  7231. * @member {TextTrack} track
  7232. * The underlying TextTrack object.
  7233. * @instance
  7234. *
  7235. */
  7236. track: {
  7237. get() {
  7238. return track;
  7239. }
  7240. }
  7241. });
  7242. readyState = HTMLTrackElement.NONE;
  7243. /**
  7244. * @listens TextTrack#loadeddata
  7245. * @fires HTMLTrackElement#load
  7246. */
  7247. track.addEventListener('loadeddata', () => {
  7248. readyState = HTMLTrackElement.LOADED;
  7249. this.trigger({
  7250. type: 'load',
  7251. target: this
  7252. });
  7253. });
  7254. }
  7255. }
  7256. /**
  7257. * @protected
  7258. */
  7259. HTMLTrackElement.prototype.allowedEvents_ = {
  7260. load: 'load'
  7261. };
  7262. /**
  7263. * The text track not loaded state.
  7264. *
  7265. * @type {number}
  7266. * @static
  7267. */
  7268. HTMLTrackElement.NONE = 0;
  7269. /**
  7270. * The text track loading state.
  7271. *
  7272. * @type {number}
  7273. * @static
  7274. */
  7275. HTMLTrackElement.LOADING = 1;
  7276. /**
  7277. * The text track loaded state.
  7278. *
  7279. * @type {number}
  7280. * @static
  7281. */
  7282. HTMLTrackElement.LOADED = 2;
  7283. /**
  7284. * The text track failed to load state.
  7285. *
  7286. * @type {number}
  7287. * @static
  7288. */
  7289. HTMLTrackElement.ERROR = 3;
  7290. /*
  7291. * This file contains all track properties that are used in
  7292. * player.js, tech.js, html5.js and possibly other techs in the future.
  7293. */
  7294. const NORMAL = {
  7295. audio: {
  7296. ListClass: AudioTrackList,
  7297. TrackClass: AudioTrack,
  7298. capitalName: 'Audio'
  7299. },
  7300. video: {
  7301. ListClass: VideoTrackList,
  7302. TrackClass: VideoTrack,
  7303. capitalName: 'Video'
  7304. },
  7305. text: {
  7306. ListClass: TextTrackList,
  7307. TrackClass: TextTrack,
  7308. capitalName: 'Text'
  7309. }
  7310. };
  7311. Object.keys(NORMAL).forEach(function (type) {
  7312. NORMAL[type].getterName = `${type}Tracks`;
  7313. NORMAL[type].privateName = `${type}Tracks_`;
  7314. });
  7315. const REMOTE = {
  7316. remoteText: {
  7317. ListClass: TextTrackList,
  7318. TrackClass: TextTrack,
  7319. capitalName: 'RemoteText',
  7320. getterName: 'remoteTextTracks',
  7321. privateName: 'remoteTextTracks_'
  7322. },
  7323. remoteTextEl: {
  7324. ListClass: HtmlTrackElementList,
  7325. TrackClass: HTMLTrackElement,
  7326. capitalName: 'RemoteTextTrackEls',
  7327. getterName: 'remoteTextTrackEls',
  7328. privateName: 'remoteTextTrackEls_'
  7329. }
  7330. };
  7331. const ALL = Object.assign({}, NORMAL, REMOTE);
  7332. REMOTE.names = Object.keys(REMOTE);
  7333. NORMAL.names = Object.keys(NORMAL);
  7334. ALL.names = [].concat(REMOTE.names).concat(NORMAL.names);
  7335. /**
  7336. * @file tech.js
  7337. */
  7338. /**
  7339. * An Object containing a structure like: `{src: 'url', type: 'mimetype'}` or string
  7340. * that just contains the src url alone.
  7341. * * `var SourceObject = {src: 'http://ex.com/video.mp4', type: 'video/mp4'};`
  7342. * `var SourceString = 'http://example.com/some-video.mp4';`
  7343. *
  7344. * @typedef {Object|string} SourceObject
  7345. *
  7346. * @property {string} src
  7347. * The url to the source
  7348. *
  7349. * @property {string} type
  7350. * The mime type of the source
  7351. */
  7352. /**
  7353. * A function used by {@link Tech} to create a new {@link TextTrack}.
  7354. *
  7355. * @private
  7356. *
  7357. * @param {Tech} self
  7358. * An instance of the Tech class.
  7359. *
  7360. * @param {string} kind
  7361. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  7362. *
  7363. * @param {string} [label]
  7364. * Label to identify the text track
  7365. *
  7366. * @param {string} [language]
  7367. * Two letter language abbreviation
  7368. *
  7369. * @param {Object} [options={}]
  7370. * An object with additional text track options
  7371. *
  7372. * @return {TextTrack}
  7373. * The text track that was created.
  7374. */
  7375. function createTrackHelper(self, kind, label, language, options = {}) {
  7376. const tracks = self.textTracks();
  7377. options.kind = kind;
  7378. if (label) {
  7379. options.label = label;
  7380. }
  7381. if (language) {
  7382. options.language = language;
  7383. }
  7384. options.tech = self;
  7385. const track = new ALL.text.TrackClass(options);
  7386. tracks.addTrack(track);
  7387. return track;
  7388. }
  7389. /**
  7390. * This is the base class for media playback technology controllers, such as
  7391. * {@link HTML5}
  7392. *
  7393. * @extends Component
  7394. */
  7395. class Tech extends Component {
  7396. /**
  7397. * Create an instance of this Tech.
  7398. *
  7399. * @param {Object} [options]
  7400. * The key/value store of player options.
  7401. *
  7402. * @param {Function} [ready]
  7403. * Callback function to call when the `HTML5` Tech is ready.
  7404. */
  7405. constructor(options = {}, ready = function () {}) {
  7406. // we don't want the tech to report user activity automatically.
  7407. // This is done manually in addControlsListeners
  7408. options.reportTouchActivity = false;
  7409. super(null, options, ready);
  7410. this.onDurationChange_ = e => this.onDurationChange(e);
  7411. this.trackProgress_ = e => this.trackProgress(e);
  7412. this.trackCurrentTime_ = e => this.trackCurrentTime(e);
  7413. this.stopTrackingCurrentTime_ = e => this.stopTrackingCurrentTime(e);
  7414. this.disposeSourceHandler_ = e => this.disposeSourceHandler(e);
  7415. this.queuedHanders_ = new Set();
  7416. // keep track of whether the current source has played at all to
  7417. // implement a very limited played()
  7418. this.hasStarted_ = false;
  7419. this.on('playing', function () {
  7420. this.hasStarted_ = true;
  7421. });
  7422. this.on('loadstart', function () {
  7423. this.hasStarted_ = false;
  7424. });
  7425. ALL.names.forEach(name => {
  7426. const props = ALL[name];
  7427. if (options && options[props.getterName]) {
  7428. this[props.privateName] = options[props.getterName];
  7429. }
  7430. });
  7431. // Manually track progress in cases where the browser/tech doesn't report it.
  7432. if (!this.featuresProgressEvents) {
  7433. this.manualProgressOn();
  7434. }
  7435. // Manually track timeupdates in cases where the browser/tech doesn't report it.
  7436. if (!this.featuresTimeupdateEvents) {
  7437. this.manualTimeUpdatesOn();
  7438. }
  7439. ['Text', 'Audio', 'Video'].forEach(track => {
  7440. if (options[`native${track}Tracks`] === false) {
  7441. this[`featuresNative${track}Tracks`] = false;
  7442. }
  7443. });
  7444. if (options.nativeCaptions === false || options.nativeTextTracks === false) {
  7445. this.featuresNativeTextTracks = false;
  7446. } else if (options.nativeCaptions === true || options.nativeTextTracks === true) {
  7447. this.featuresNativeTextTracks = true;
  7448. }
  7449. if (!this.featuresNativeTextTracks) {
  7450. this.emulateTextTracks();
  7451. }
  7452. this.preloadTextTracks = options.preloadTextTracks !== false;
  7453. this.autoRemoteTextTracks_ = new ALL.text.ListClass();
  7454. this.initTrackListeners();
  7455. // Turn on component tap events only if not using native controls
  7456. if (!options.nativeControlsForTouch) {
  7457. this.emitTapEvents();
  7458. }
  7459. if (this.constructor) {
  7460. this.name_ = this.constructor.name || 'Unknown Tech';
  7461. }
  7462. }
  7463. /**
  7464. * A special function to trigger source set in a way that will allow player
  7465. * to re-trigger if the player or tech are not ready yet.
  7466. *
  7467. * @fires Tech#sourceset
  7468. * @param {string} src The source string at the time of the source changing.
  7469. */
  7470. triggerSourceset(src) {
  7471. if (!this.isReady_) {
  7472. // on initial ready we have to trigger source set
  7473. // 1ms after ready so that player can watch for it.
  7474. this.one('ready', () => this.setTimeout(() => this.triggerSourceset(src), 1));
  7475. }
  7476. /**
  7477. * Fired when the source is set on the tech causing the media element
  7478. * to reload.
  7479. *
  7480. * @see {@link Player#event:sourceset}
  7481. * @event Tech#sourceset
  7482. * @type {Event}
  7483. */
  7484. this.trigger({
  7485. src,
  7486. type: 'sourceset'
  7487. });
  7488. }
  7489. /* Fallbacks for unsupported event types
  7490. ================================================================================ */
  7491. /**
  7492. * Polyfill the `progress` event for browsers that don't support it natively.
  7493. *
  7494. * @see {@link Tech#trackProgress}
  7495. */
  7496. manualProgressOn() {
  7497. this.on('durationchange', this.onDurationChange_);
  7498. this.manualProgress = true;
  7499. // Trigger progress watching when a source begins loading
  7500. this.one('ready', this.trackProgress_);
  7501. }
  7502. /**
  7503. * Turn off the polyfill for `progress` events that was created in
  7504. * {@link Tech#manualProgressOn}
  7505. */
  7506. manualProgressOff() {
  7507. this.manualProgress = false;
  7508. this.stopTrackingProgress();
  7509. this.off('durationchange', this.onDurationChange_);
  7510. }
  7511. /**
  7512. * This is used to trigger a `progress` event when the buffered percent changes. It
  7513. * sets an interval function that will be called every 500 milliseconds to check if the
  7514. * buffer end percent has changed.
  7515. *
  7516. * > This function is called by {@link Tech#manualProgressOn}
  7517. *
  7518. * @param {Event} event
  7519. * The `ready` event that caused this to run.
  7520. *
  7521. * @listens Tech#ready
  7522. * @fires Tech#progress
  7523. */
  7524. trackProgress(event) {
  7525. this.stopTrackingProgress();
  7526. this.progressInterval = this.setInterval(bind_(this, function () {
  7527. // Don't trigger unless buffered amount is greater than last time
  7528. const numBufferedPercent = this.bufferedPercent();
  7529. if (this.bufferedPercent_ !== numBufferedPercent) {
  7530. /**
  7531. * See {@link Player#progress}
  7532. *
  7533. * @event Tech#progress
  7534. * @type {Event}
  7535. */
  7536. this.trigger('progress');
  7537. }
  7538. this.bufferedPercent_ = numBufferedPercent;
  7539. if (numBufferedPercent === 1) {
  7540. this.stopTrackingProgress();
  7541. }
  7542. }), 500);
  7543. }
  7544. /**
  7545. * Update our internal duration on a `durationchange` event by calling
  7546. * {@link Tech#duration}.
  7547. *
  7548. * @param {Event} event
  7549. * The `durationchange` event that caused this to run.
  7550. *
  7551. * @listens Tech#durationchange
  7552. */
  7553. onDurationChange(event) {
  7554. this.duration_ = this.duration();
  7555. }
  7556. /**
  7557. * Get and create a `TimeRange` object for buffering.
  7558. *
  7559. * @return { import('../utils/time').TimeRange }
  7560. * The time range object that was created.
  7561. */
  7562. buffered() {
  7563. return createTimeRanges(0, 0);
  7564. }
  7565. /**
  7566. * Get the percentage of the current video that is currently buffered.
  7567. *
  7568. * @return {number}
  7569. * A number from 0 to 1 that represents the decimal percentage of the
  7570. * video that is buffered.
  7571. *
  7572. */
  7573. bufferedPercent() {
  7574. return bufferedPercent(this.buffered(), this.duration_);
  7575. }
  7576. /**
  7577. * Turn off the polyfill for `progress` events that was created in
  7578. * {@link Tech#manualProgressOn}
  7579. * Stop manually tracking progress events by clearing the interval that was set in
  7580. * {@link Tech#trackProgress}.
  7581. */
  7582. stopTrackingProgress() {
  7583. this.clearInterval(this.progressInterval);
  7584. }
  7585. /**
  7586. * Polyfill the `timeupdate` event for browsers that don't support it.
  7587. *
  7588. * @see {@link Tech#trackCurrentTime}
  7589. */
  7590. manualTimeUpdatesOn() {
  7591. this.manualTimeUpdates = true;
  7592. this.on('play', this.trackCurrentTime_);
  7593. this.on('pause', this.stopTrackingCurrentTime_);
  7594. }
  7595. /**
  7596. * Turn off the polyfill for `timeupdate` events that was created in
  7597. * {@link Tech#manualTimeUpdatesOn}
  7598. */
  7599. manualTimeUpdatesOff() {
  7600. this.manualTimeUpdates = false;
  7601. this.stopTrackingCurrentTime();
  7602. this.off('play', this.trackCurrentTime_);
  7603. this.off('pause', this.stopTrackingCurrentTime_);
  7604. }
  7605. /**
  7606. * Sets up an interval function to track current time and trigger `timeupdate` every
  7607. * 250 milliseconds.
  7608. *
  7609. * @listens Tech#play
  7610. * @triggers Tech#timeupdate
  7611. */
  7612. trackCurrentTime() {
  7613. if (this.currentTimeInterval) {
  7614. this.stopTrackingCurrentTime();
  7615. }
  7616. this.currentTimeInterval = this.setInterval(function () {
  7617. /**
  7618. * Triggered at an interval of 250ms to indicated that time is passing in the video.
  7619. *
  7620. * @event Tech#timeupdate
  7621. * @type {Event}
  7622. */
  7623. this.trigger({
  7624. type: 'timeupdate',
  7625. target: this,
  7626. manuallyTriggered: true
  7627. });
  7628. // 42 = 24 fps // 250 is what Webkit uses // FF uses 15
  7629. }, 250);
  7630. }
  7631. /**
  7632. * Stop the interval function created in {@link Tech#trackCurrentTime} so that the
  7633. * `timeupdate` event is no longer triggered.
  7634. *
  7635. * @listens {Tech#pause}
  7636. */
  7637. stopTrackingCurrentTime() {
  7638. this.clearInterval(this.currentTimeInterval);
  7639. // #1002 - if the video ends right before the next timeupdate would happen,
  7640. // the progress bar won't make it all the way to the end
  7641. this.trigger({
  7642. type: 'timeupdate',
  7643. target: this,
  7644. manuallyTriggered: true
  7645. });
  7646. }
  7647. /**
  7648. * Turn off all event polyfills, clear the `Tech`s {@link AudioTrackList},
  7649. * {@link VideoTrackList}, and {@link TextTrackList}, and dispose of this Tech.
  7650. *
  7651. * @fires Component#dispose
  7652. */
  7653. dispose() {
  7654. // clear out all tracks because we can't reuse them between techs
  7655. this.clearTracks(NORMAL.names);
  7656. // Turn off any manual progress or timeupdate tracking
  7657. if (this.manualProgress) {
  7658. this.manualProgressOff();
  7659. }
  7660. if (this.manualTimeUpdates) {
  7661. this.manualTimeUpdatesOff();
  7662. }
  7663. super.dispose();
  7664. }
  7665. /**
  7666. * Clear out a single `TrackList` or an array of `TrackLists` given their names.
  7667. *
  7668. * > Note: Techs without source handlers should call this between sources for `video`
  7669. * & `audio` tracks. You don't want to use them between tracks!
  7670. *
  7671. * @param {string[]|string} types
  7672. * TrackList names to clear, valid names are `video`, `audio`, and
  7673. * `text`.
  7674. */
  7675. clearTracks(types) {
  7676. types = [].concat(types);
  7677. // clear out all tracks because we can't reuse them between techs
  7678. types.forEach(type => {
  7679. const list = this[`${type}Tracks`]() || [];
  7680. let i = list.length;
  7681. while (i--) {
  7682. const track = list[i];
  7683. if (type === 'text') {
  7684. this.removeRemoteTextTrack(track);
  7685. }
  7686. list.removeTrack(track);
  7687. }
  7688. });
  7689. }
  7690. /**
  7691. * Remove any TextTracks added via addRemoteTextTrack that are
  7692. * flagged for automatic garbage collection
  7693. */
  7694. cleanupAutoTextTracks() {
  7695. const list = this.autoRemoteTextTracks_ || [];
  7696. let i = list.length;
  7697. while (i--) {
  7698. const track = list[i];
  7699. this.removeRemoteTextTrack(track);
  7700. }
  7701. }
  7702. /**
  7703. * Reset the tech, which will removes all sources and reset the internal readyState.
  7704. *
  7705. * @abstract
  7706. */
  7707. reset() {}
  7708. /**
  7709. * Get the value of `crossOrigin` from the tech.
  7710. *
  7711. * @abstract
  7712. *
  7713. * @see {Html5#crossOrigin}
  7714. */
  7715. crossOrigin() {}
  7716. /**
  7717. * Set the value of `crossOrigin` on the tech.
  7718. *
  7719. * @abstract
  7720. *
  7721. * @param {string} crossOrigin the crossOrigin value
  7722. * @see {Html5#setCrossOrigin}
  7723. */
  7724. setCrossOrigin() {}
  7725. /**
  7726. * Get or set an error on the Tech.
  7727. *
  7728. * @param {MediaError} [err]
  7729. * Error to set on the Tech
  7730. *
  7731. * @return {MediaError|null}
  7732. * The current error object on the tech, or null if there isn't one.
  7733. */
  7734. error(err) {
  7735. if (err !== undefined) {
  7736. this.error_ = new MediaError(err);
  7737. this.trigger('error');
  7738. }
  7739. return this.error_;
  7740. }
  7741. /**
  7742. * Returns the `TimeRange`s that have been played through for the current source.
  7743. *
  7744. * > NOTE: This implementation is incomplete. It does not track the played `TimeRange`.
  7745. * It only checks whether the source has played at all or not.
  7746. *
  7747. * @return { import('../utils/time').TimeRange }
  7748. * - A single time range if this video has played
  7749. * - An empty set of ranges if not.
  7750. */
  7751. played() {
  7752. if (this.hasStarted_) {
  7753. return createTimeRanges(0, 0);
  7754. }
  7755. return createTimeRanges();
  7756. }
  7757. /**
  7758. * Start playback
  7759. *
  7760. * @abstract
  7761. *
  7762. * @see {Html5#play}
  7763. */
  7764. play() {}
  7765. /**
  7766. * Set whether we are scrubbing or not
  7767. *
  7768. * @abstract
  7769. * @param {boolean} _isScrubbing
  7770. * - true for we are currently scrubbing
  7771. * - false for we are no longer scrubbing
  7772. *
  7773. * @see {Html5#setScrubbing}
  7774. */
  7775. setScrubbing(_isScrubbing) {}
  7776. /**
  7777. * Get whether we are scrubbing or not
  7778. *
  7779. * @abstract
  7780. *
  7781. * @see {Html5#scrubbing}
  7782. */
  7783. scrubbing() {}
  7784. /**
  7785. * Causes a manual time update to occur if {@link Tech#manualTimeUpdatesOn} was
  7786. * previously called.
  7787. *
  7788. * @param {number} _seconds
  7789. * Set the current time of the media to this.
  7790. * @fires Tech#timeupdate
  7791. */
  7792. setCurrentTime(_seconds) {
  7793. // improve the accuracy of manual timeupdates
  7794. if (this.manualTimeUpdates) {
  7795. /**
  7796. * A manual `timeupdate` event.
  7797. *
  7798. * @event Tech#timeupdate
  7799. * @type {Event}
  7800. */
  7801. this.trigger({
  7802. type: 'timeupdate',
  7803. target: this,
  7804. manuallyTriggered: true
  7805. });
  7806. }
  7807. }
  7808. /**
  7809. * Turn on listeners for {@link VideoTrackList}, {@link {AudioTrackList}, and
  7810. * {@link TextTrackList} events.
  7811. *
  7812. * This adds {@link EventTarget~EventListeners} for `addtrack`, and `removetrack`.
  7813. *
  7814. * @fires Tech#audiotrackchange
  7815. * @fires Tech#videotrackchange
  7816. * @fires Tech#texttrackchange
  7817. */
  7818. initTrackListeners() {
  7819. /**
  7820. * Triggered when tracks are added or removed on the Tech {@link AudioTrackList}
  7821. *
  7822. * @event Tech#audiotrackchange
  7823. * @type {Event}
  7824. */
  7825. /**
  7826. * Triggered when tracks are added or removed on the Tech {@link VideoTrackList}
  7827. *
  7828. * @event Tech#videotrackchange
  7829. * @type {Event}
  7830. */
  7831. /**
  7832. * Triggered when tracks are added or removed on the Tech {@link TextTrackList}
  7833. *
  7834. * @event Tech#texttrackchange
  7835. * @type {Event}
  7836. */
  7837. NORMAL.names.forEach(name => {
  7838. const props = NORMAL[name];
  7839. const trackListChanges = () => {
  7840. this.trigger(`${name}trackchange`);
  7841. };
  7842. const tracks = this[props.getterName]();
  7843. tracks.addEventListener('removetrack', trackListChanges);
  7844. tracks.addEventListener('addtrack', trackListChanges);
  7845. this.on('dispose', () => {
  7846. tracks.removeEventListener('removetrack', trackListChanges);
  7847. tracks.removeEventListener('addtrack', trackListChanges);
  7848. });
  7849. });
  7850. }
  7851. /**
  7852. * Emulate TextTracks using vtt.js if necessary
  7853. *
  7854. * @fires Tech#vttjsloaded
  7855. * @fires Tech#vttjserror
  7856. */
  7857. addWebVttScript_() {
  7858. if (window.WebVTT) {
  7859. return;
  7860. }
  7861. // Initially, Tech.el_ is a child of a dummy-div wait until the Component system
  7862. // signals that the Tech is ready at which point Tech.el_ is part of the DOM
  7863. // before inserting the WebVTT script
  7864. if (document.body.contains(this.el())) {
  7865. // load via require if available and vtt.js script location was not passed in
  7866. // as an option. novtt builds will turn the above require call into an empty object
  7867. // which will cause this if check to always fail.
  7868. if (!this.options_['vtt.js'] && isPlain(vtt) && Object.keys(vtt).length > 0) {
  7869. this.trigger('vttjsloaded');
  7870. return;
  7871. }
  7872. // load vtt.js via the script location option or the cdn of no location was
  7873. // passed in
  7874. const script = document.createElement('script');
  7875. script.src = this.options_['vtt.js'] || 'https://vjs.zencdn.net/vttjs/0.14.1/vtt.min.js';
  7876. script.onload = () => {
  7877. /**
  7878. * Fired when vtt.js is loaded.
  7879. *
  7880. * @event Tech#vttjsloaded
  7881. * @type {Event}
  7882. */
  7883. this.trigger('vttjsloaded');
  7884. };
  7885. script.onerror = () => {
  7886. /**
  7887. * Fired when vtt.js was not loaded due to an error
  7888. *
  7889. * @event Tech#vttjsloaded
  7890. * @type {Event}
  7891. */
  7892. this.trigger('vttjserror');
  7893. };
  7894. this.on('dispose', () => {
  7895. script.onload = null;
  7896. script.onerror = null;
  7897. });
  7898. // but have not loaded yet and we set it to true before the inject so that
  7899. // we don't overwrite the injected window.WebVTT if it loads right away
  7900. window.WebVTT = true;
  7901. this.el().parentNode.appendChild(script);
  7902. } else {
  7903. this.ready(this.addWebVttScript_);
  7904. }
  7905. }
  7906. /**
  7907. * Emulate texttracks
  7908. *
  7909. */
  7910. emulateTextTracks() {
  7911. const tracks = this.textTracks();
  7912. const remoteTracks = this.remoteTextTracks();
  7913. const handleAddTrack = e => tracks.addTrack(e.track);
  7914. const handleRemoveTrack = e => tracks.removeTrack(e.track);
  7915. remoteTracks.on('addtrack', handleAddTrack);
  7916. remoteTracks.on('removetrack', handleRemoveTrack);
  7917. this.addWebVttScript_();
  7918. const updateDisplay = () => this.trigger('texttrackchange');
  7919. const textTracksChanges = () => {
  7920. updateDisplay();
  7921. for (let i = 0; i < tracks.length; i++) {
  7922. const track = tracks[i];
  7923. track.removeEventListener('cuechange', updateDisplay);
  7924. if (track.mode === 'showing') {
  7925. track.addEventListener('cuechange', updateDisplay);
  7926. }
  7927. }
  7928. };
  7929. textTracksChanges();
  7930. tracks.addEventListener('change', textTracksChanges);
  7931. tracks.addEventListener('addtrack', textTracksChanges);
  7932. tracks.addEventListener('removetrack', textTracksChanges);
  7933. this.on('dispose', function () {
  7934. remoteTracks.off('addtrack', handleAddTrack);
  7935. remoteTracks.off('removetrack', handleRemoveTrack);
  7936. tracks.removeEventListener('change', textTracksChanges);
  7937. tracks.removeEventListener('addtrack', textTracksChanges);
  7938. tracks.removeEventListener('removetrack', textTracksChanges);
  7939. for (let i = 0; i < tracks.length; i++) {
  7940. const track = tracks[i];
  7941. track.removeEventListener('cuechange', updateDisplay);
  7942. }
  7943. });
  7944. }
  7945. /**
  7946. * Create and returns a remote {@link TextTrack} object.
  7947. *
  7948. * @param {string} kind
  7949. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  7950. *
  7951. * @param {string} [label]
  7952. * Label to identify the text track
  7953. *
  7954. * @param {string} [language]
  7955. * Two letter language abbreviation
  7956. *
  7957. * @return {TextTrack}
  7958. * The TextTrack that gets created.
  7959. */
  7960. addTextTrack(kind, label, language) {
  7961. if (!kind) {
  7962. throw new Error('TextTrack kind is required but was not provided');
  7963. }
  7964. return createTrackHelper(this, kind, label, language);
  7965. }
  7966. /**
  7967. * Create an emulated TextTrack for use by addRemoteTextTrack
  7968. *
  7969. * This is intended to be overridden by classes that inherit from
  7970. * Tech in order to create native or custom TextTracks.
  7971. *
  7972. * @param {Object} options
  7973. * The object should contain the options to initialize the TextTrack with.
  7974. *
  7975. * @param {string} [options.kind]
  7976. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata).
  7977. *
  7978. * @param {string} [options.label].
  7979. * Label to identify the text track
  7980. *
  7981. * @param {string} [options.language]
  7982. * Two letter language abbreviation.
  7983. *
  7984. * @return {HTMLTrackElement}
  7985. * The track element that gets created.
  7986. */
  7987. createRemoteTextTrack(options) {
  7988. const track = merge(options, {
  7989. tech: this
  7990. });
  7991. return new REMOTE.remoteTextEl.TrackClass(track);
  7992. }
  7993. /**
  7994. * Creates a remote text track object and returns an html track element.
  7995. *
  7996. * > Note: This can be an emulated {@link HTMLTrackElement} or a native one.
  7997. *
  7998. * @param {Object} options
  7999. * See {@link Tech#createRemoteTextTrack} for more detailed properties.
  8000. *
  8001. * @param {boolean} [manualCleanup=false]
  8002. * - When false: the TextTrack will be automatically removed from the video
  8003. * element whenever the source changes
  8004. * - When True: The TextTrack will have to be cleaned up manually
  8005. *
  8006. * @return {HTMLTrackElement}
  8007. * An Html Track Element.
  8008. *
  8009. */
  8010. addRemoteTextTrack(options = {}, manualCleanup) {
  8011. const htmlTrackElement = this.createRemoteTextTrack(options);
  8012. if (typeof manualCleanup !== 'boolean') {
  8013. manualCleanup = false;
  8014. }
  8015. // store HTMLTrackElement and TextTrack to remote list
  8016. this.remoteTextTrackEls().addTrackElement_(htmlTrackElement);
  8017. this.remoteTextTracks().addTrack(htmlTrackElement.track);
  8018. if (manualCleanup === false) {
  8019. // create the TextTrackList if it doesn't exist
  8020. this.ready(() => this.autoRemoteTextTracks_.addTrack(htmlTrackElement.track));
  8021. }
  8022. return htmlTrackElement;
  8023. }
  8024. /**
  8025. * Remove a remote text track from the remote `TextTrackList`.
  8026. *
  8027. * @param {TextTrack} track
  8028. * `TextTrack` to remove from the `TextTrackList`
  8029. */
  8030. removeRemoteTextTrack(track) {
  8031. const trackElement = this.remoteTextTrackEls().getTrackElementByTrack_(track);
  8032. // remove HTMLTrackElement and TextTrack from remote list
  8033. this.remoteTextTrackEls().removeTrackElement_(trackElement);
  8034. this.remoteTextTracks().removeTrack(track);
  8035. this.autoRemoteTextTracks_.removeTrack(track);
  8036. }
  8037. /**
  8038. * Gets available media playback quality metrics as specified by the W3C's Media
  8039. * Playback Quality API.
  8040. *
  8041. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  8042. *
  8043. * @return {Object}
  8044. * An object with supported media playback quality metrics
  8045. *
  8046. * @abstract
  8047. */
  8048. getVideoPlaybackQuality() {
  8049. return {};
  8050. }
  8051. /**
  8052. * Attempt to create a floating video window always on top of other windows
  8053. * so that users may continue consuming media while they interact with other
  8054. * content sites, or applications on their device.
  8055. *
  8056. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  8057. *
  8058. * @return {Promise|undefined}
  8059. * A promise with a Picture-in-Picture window if the browser supports
  8060. * Promises (or one was passed in as an option). It returns undefined
  8061. * otherwise.
  8062. *
  8063. * @abstract
  8064. */
  8065. requestPictureInPicture() {
  8066. return Promise.reject();
  8067. }
  8068. /**
  8069. * A method to check for the value of the 'disablePictureInPicture' <video> property.
  8070. * Defaults to true, as it should be considered disabled if the tech does not support pip
  8071. *
  8072. * @abstract
  8073. */
  8074. disablePictureInPicture() {
  8075. return true;
  8076. }
  8077. /**
  8078. * A method to set or unset the 'disablePictureInPicture' <video> property.
  8079. *
  8080. * @abstract
  8081. */
  8082. setDisablePictureInPicture() {}
  8083. /**
  8084. * A fallback implementation of requestVideoFrameCallback using requestAnimationFrame
  8085. *
  8086. * @param {function} cb
  8087. * @return {number} request id
  8088. */
  8089. requestVideoFrameCallback(cb) {
  8090. const id = newGUID();
  8091. if (!this.isReady_ || this.paused()) {
  8092. this.queuedHanders_.add(id);
  8093. this.one('playing', () => {
  8094. if (this.queuedHanders_.has(id)) {
  8095. this.queuedHanders_.delete(id);
  8096. cb();
  8097. }
  8098. });
  8099. } else {
  8100. this.requestNamedAnimationFrame(id, cb);
  8101. }
  8102. return id;
  8103. }
  8104. /**
  8105. * A fallback implementation of cancelVideoFrameCallback
  8106. *
  8107. * @param {number} id id of callback to be cancelled
  8108. */
  8109. cancelVideoFrameCallback(id) {
  8110. if (this.queuedHanders_.has(id)) {
  8111. this.queuedHanders_.delete(id);
  8112. } else {
  8113. this.cancelNamedAnimationFrame(id);
  8114. }
  8115. }
  8116. /**
  8117. * A method to set a poster from a `Tech`.
  8118. *
  8119. * @abstract
  8120. */
  8121. setPoster() {}
  8122. /**
  8123. * A method to check for the presence of the 'playsinline' <video> attribute.
  8124. *
  8125. * @abstract
  8126. */
  8127. playsinline() {}
  8128. /**
  8129. * A method to set or unset the 'playsinline' <video> attribute.
  8130. *
  8131. * @abstract
  8132. */
  8133. setPlaysinline() {}
  8134. /**
  8135. * Attempt to force override of native audio tracks.
  8136. *
  8137. * @param {boolean} override - If set to true native audio will be overridden,
  8138. * otherwise native audio will potentially be used.
  8139. *
  8140. * @abstract
  8141. */
  8142. overrideNativeAudioTracks(override) {}
  8143. /**
  8144. * Attempt to force override of native video tracks.
  8145. *
  8146. * @param {boolean} override - If set to true native video will be overridden,
  8147. * otherwise native video will potentially be used.
  8148. *
  8149. * @abstract
  8150. */
  8151. overrideNativeVideoTracks(override) {}
  8152. /**
  8153. * Check if the tech can support the given mime-type.
  8154. *
  8155. * The base tech does not support any type, but source handlers might
  8156. * overwrite this.
  8157. *
  8158. * @param {string} _type
  8159. * The mimetype to check for support
  8160. *
  8161. * @return {string}
  8162. * 'probably', 'maybe', or empty string
  8163. *
  8164. * @see [Spec]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canPlayType}
  8165. *
  8166. * @abstract
  8167. */
  8168. canPlayType(_type) {
  8169. return '';
  8170. }
  8171. /**
  8172. * Check if the type is supported by this tech.
  8173. *
  8174. * The base tech does not support any type, but source handlers might
  8175. * overwrite this.
  8176. *
  8177. * @param {string} _type
  8178. * The media type to check
  8179. * @return {string} Returns the native video element's response
  8180. */
  8181. static canPlayType(_type) {
  8182. return '';
  8183. }
  8184. /**
  8185. * Check if the tech can support the given source
  8186. *
  8187. * @param {Object} srcObj
  8188. * The source object
  8189. * @param {Object} options
  8190. * The options passed to the tech
  8191. * @return {string} 'probably', 'maybe', or '' (empty string)
  8192. */
  8193. static canPlaySource(srcObj, options) {
  8194. return Tech.canPlayType(srcObj.type);
  8195. }
  8196. /*
  8197. * Return whether the argument is a Tech or not.
  8198. * Can be passed either a Class like `Html5` or a instance like `player.tech_`
  8199. *
  8200. * @param {Object} component
  8201. * The item to check
  8202. *
  8203. * @return {boolean}
  8204. * Whether it is a tech or not
  8205. * - True if it is a tech
  8206. * - False if it is not
  8207. */
  8208. static isTech(component) {
  8209. return component.prototype instanceof Tech || component instanceof Tech || component === Tech;
  8210. }
  8211. /**
  8212. * Registers a `Tech` into a shared list for videojs.
  8213. *
  8214. * @param {string} name
  8215. * Name of the `Tech` to register.
  8216. *
  8217. * @param {Object} tech
  8218. * The `Tech` class to register.
  8219. */
  8220. static registerTech(name, tech) {
  8221. if (!Tech.techs_) {
  8222. Tech.techs_ = {};
  8223. }
  8224. if (!Tech.isTech(tech)) {
  8225. throw new Error(`Tech ${name} must be a Tech`);
  8226. }
  8227. if (!Tech.canPlayType) {
  8228. throw new Error('Techs must have a static canPlayType method on them');
  8229. }
  8230. if (!Tech.canPlaySource) {
  8231. throw new Error('Techs must have a static canPlaySource method on them');
  8232. }
  8233. name = toTitleCase(name);
  8234. Tech.techs_[name] = tech;
  8235. Tech.techs_[toLowerCase(name)] = tech;
  8236. if (name !== 'Tech') {
  8237. // camel case the techName for use in techOrder
  8238. Tech.defaultTechOrder_.push(name);
  8239. }
  8240. return tech;
  8241. }
  8242. /**
  8243. * Get a `Tech` from the shared list by name.
  8244. *
  8245. * @param {string} name
  8246. * `camelCase` or `TitleCase` name of the Tech to get
  8247. *
  8248. * @return {Tech|undefined}
  8249. * The `Tech` or undefined if there was no tech with the name requested.
  8250. */
  8251. static getTech(name) {
  8252. if (!name) {
  8253. return;
  8254. }
  8255. if (Tech.techs_ && Tech.techs_[name]) {
  8256. return Tech.techs_[name];
  8257. }
  8258. name = toTitleCase(name);
  8259. if (window && window.videojs && window.videojs[name]) {
  8260. log.warn(`The ${name} tech was added to the videojs object when it should be registered using videojs.registerTech(name, tech)`);
  8261. return window.videojs[name];
  8262. }
  8263. }
  8264. }
  8265. /**
  8266. * Get the {@link VideoTrackList}
  8267. *
  8268. * @returns {VideoTrackList}
  8269. * @method Tech.prototype.videoTracks
  8270. */
  8271. /**
  8272. * Get the {@link AudioTrackList}
  8273. *
  8274. * @returns {AudioTrackList}
  8275. * @method Tech.prototype.audioTracks
  8276. */
  8277. /**
  8278. * Get the {@link TextTrackList}
  8279. *
  8280. * @returns {TextTrackList}
  8281. * @method Tech.prototype.textTracks
  8282. */
  8283. /**
  8284. * Get the remote element {@link TextTrackList}
  8285. *
  8286. * @returns {TextTrackList}
  8287. * @method Tech.prototype.remoteTextTracks
  8288. */
  8289. /**
  8290. * Get the remote element {@link HtmlTrackElementList}
  8291. *
  8292. * @returns {HtmlTrackElementList}
  8293. * @method Tech.prototype.remoteTextTrackEls
  8294. */
  8295. ALL.names.forEach(function (name) {
  8296. const props = ALL[name];
  8297. Tech.prototype[props.getterName] = function () {
  8298. this[props.privateName] = this[props.privateName] || new props.ListClass();
  8299. return this[props.privateName];
  8300. };
  8301. });
  8302. /**
  8303. * List of associated text tracks
  8304. *
  8305. * @type {TextTrackList}
  8306. * @private
  8307. * @property Tech#textTracks_
  8308. */
  8309. /**
  8310. * List of associated audio tracks.
  8311. *
  8312. * @type {AudioTrackList}
  8313. * @private
  8314. * @property Tech#audioTracks_
  8315. */
  8316. /**
  8317. * List of associated video tracks.
  8318. *
  8319. * @type {VideoTrackList}
  8320. * @private
  8321. * @property Tech#videoTracks_
  8322. */
  8323. /**
  8324. * Boolean indicating whether the `Tech` supports volume control.
  8325. *
  8326. * @type {boolean}
  8327. * @default
  8328. */
  8329. Tech.prototype.featuresVolumeControl = true;
  8330. /**
  8331. * Boolean indicating whether the `Tech` supports muting volume.
  8332. *
  8333. * @type {boolean}
  8334. * @default
  8335. */
  8336. Tech.prototype.featuresMuteControl = true;
  8337. /**
  8338. * Boolean indicating whether the `Tech` supports fullscreen resize control.
  8339. * Resizing plugins using request fullscreen reloads the plugin
  8340. *
  8341. * @type {boolean}
  8342. * @default
  8343. */
  8344. Tech.prototype.featuresFullscreenResize = false;
  8345. /**
  8346. * Boolean indicating whether the `Tech` supports changing the speed at which the video
  8347. * plays. Examples:
  8348. * - Set player to play 2x (twice) as fast
  8349. * - Set player to play 0.5x (half) as fast
  8350. *
  8351. * @type {boolean}
  8352. * @default
  8353. */
  8354. Tech.prototype.featuresPlaybackRate = false;
  8355. /**
  8356. * Boolean indicating whether the `Tech` supports the `progress` event.
  8357. * This will be used to determine if {@link Tech#manualProgressOn} should be called.
  8358. *
  8359. * @type {boolean}
  8360. * @default
  8361. */
  8362. Tech.prototype.featuresProgressEvents = false;
  8363. /**
  8364. * Boolean indicating whether the `Tech` supports the `sourceset` event.
  8365. *
  8366. * A tech should set this to `true` and then use {@link Tech#triggerSourceset}
  8367. * to trigger a {@link Tech#event:sourceset} at the earliest time after getting
  8368. * a new source.
  8369. *
  8370. * @type {boolean}
  8371. * @default
  8372. */
  8373. Tech.prototype.featuresSourceset = false;
  8374. /**
  8375. * Boolean indicating whether the `Tech` supports the `timeupdate` event.
  8376. * This will be used to determine if {@link Tech#manualTimeUpdates} should be called.
  8377. *
  8378. * @type {boolean}
  8379. * @default
  8380. */
  8381. Tech.prototype.featuresTimeupdateEvents = false;
  8382. /**
  8383. * Boolean indicating whether the `Tech` supports the native `TextTrack`s.
  8384. * This will help us integrate with native `TextTrack`s if the browser supports them.
  8385. *
  8386. * @type {boolean}
  8387. * @default
  8388. */
  8389. Tech.prototype.featuresNativeTextTracks = false;
  8390. /**
  8391. * Boolean indicating whether the `Tech` supports `requestVideoFrameCallback`.
  8392. *
  8393. * @type {boolean}
  8394. * @default
  8395. */
  8396. Tech.prototype.featuresVideoFrameCallback = false;
  8397. /**
  8398. * A functional mixin for techs that want to use the Source Handler pattern.
  8399. * Source handlers are scripts for handling specific formats.
  8400. * The source handler pattern is used for adaptive formats (HLS, DASH) that
  8401. * manually load video data and feed it into a Source Buffer (Media Source Extensions)
  8402. * Example: `Tech.withSourceHandlers.call(MyTech);`
  8403. *
  8404. * @param {Tech} _Tech
  8405. * The tech to add source handler functions to.
  8406. *
  8407. * @mixes Tech~SourceHandlerAdditions
  8408. */
  8409. Tech.withSourceHandlers = function (_Tech) {
  8410. /**
  8411. * Register a source handler
  8412. *
  8413. * @param {Function} handler
  8414. * The source handler class
  8415. *
  8416. * @param {number} [index]
  8417. * Register it at the following index
  8418. */
  8419. _Tech.registerSourceHandler = function (handler, index) {
  8420. let handlers = _Tech.sourceHandlers;
  8421. if (!handlers) {
  8422. handlers = _Tech.sourceHandlers = [];
  8423. }
  8424. if (index === undefined) {
  8425. // add to the end of the list
  8426. index = handlers.length;
  8427. }
  8428. handlers.splice(index, 0, handler);
  8429. };
  8430. /**
  8431. * Check if the tech can support the given type. Also checks the
  8432. * Techs sourceHandlers.
  8433. *
  8434. * @param {string} type
  8435. * The mimetype to check.
  8436. *
  8437. * @return {string}
  8438. * 'probably', 'maybe', or '' (empty string)
  8439. */
  8440. _Tech.canPlayType = function (type) {
  8441. const handlers = _Tech.sourceHandlers || [];
  8442. let can;
  8443. for (let i = 0; i < handlers.length; i++) {
  8444. can = handlers[i].canPlayType(type);
  8445. if (can) {
  8446. return can;
  8447. }
  8448. }
  8449. return '';
  8450. };
  8451. /**
  8452. * Returns the first source handler that supports the source.
  8453. *
  8454. * TODO: Answer question: should 'probably' be prioritized over 'maybe'
  8455. *
  8456. * @param {SourceObject} source
  8457. * The source object
  8458. *
  8459. * @param {Object} options
  8460. * The options passed to the tech
  8461. *
  8462. * @return {SourceHandler|null}
  8463. * The first source handler that supports the source or null if
  8464. * no SourceHandler supports the source
  8465. */
  8466. _Tech.selectSourceHandler = function (source, options) {
  8467. const handlers = _Tech.sourceHandlers || [];
  8468. let can;
  8469. for (let i = 0; i < handlers.length; i++) {
  8470. can = handlers[i].canHandleSource(source, options);
  8471. if (can) {
  8472. return handlers[i];
  8473. }
  8474. }
  8475. return null;
  8476. };
  8477. /**
  8478. * Check if the tech can support the given source.
  8479. *
  8480. * @param {SourceObject} srcObj
  8481. * The source object
  8482. *
  8483. * @param {Object} options
  8484. * The options passed to the tech
  8485. *
  8486. * @return {string}
  8487. * 'probably', 'maybe', or '' (empty string)
  8488. */
  8489. _Tech.canPlaySource = function (srcObj, options) {
  8490. const sh = _Tech.selectSourceHandler(srcObj, options);
  8491. if (sh) {
  8492. return sh.canHandleSource(srcObj, options);
  8493. }
  8494. return '';
  8495. };
  8496. /**
  8497. * When using a source handler, prefer its implementation of
  8498. * any function normally provided by the tech.
  8499. */
  8500. const deferrable = ['seekable', 'seeking', 'duration'];
  8501. /**
  8502. * A wrapper around {@link Tech#seekable} that will call a `SourceHandler`s seekable
  8503. * function if it exists, with a fallback to the Techs seekable function.
  8504. *
  8505. * @method _Tech.seekable
  8506. */
  8507. /**
  8508. * A wrapper around {@link Tech#duration} that will call a `SourceHandler`s duration
  8509. * function if it exists, otherwise it will fallback to the techs duration function.
  8510. *
  8511. * @method _Tech.duration
  8512. */
  8513. deferrable.forEach(function (fnName) {
  8514. const originalFn = this[fnName];
  8515. if (typeof originalFn !== 'function') {
  8516. return;
  8517. }
  8518. this[fnName] = function () {
  8519. if (this.sourceHandler_ && this.sourceHandler_[fnName]) {
  8520. return this.sourceHandler_[fnName].apply(this.sourceHandler_, arguments);
  8521. }
  8522. return originalFn.apply(this, arguments);
  8523. };
  8524. }, _Tech.prototype);
  8525. /**
  8526. * Create a function for setting the source using a source object
  8527. * and source handlers.
  8528. * Should never be called unless a source handler was found.
  8529. *
  8530. * @param {SourceObject} source
  8531. * A source object with src and type keys
  8532. */
  8533. _Tech.prototype.setSource = function (source) {
  8534. let sh = _Tech.selectSourceHandler(source, this.options_);
  8535. if (!sh) {
  8536. // Fall back to a native source handler when unsupported sources are
  8537. // deliberately set
  8538. if (_Tech.nativeSourceHandler) {
  8539. sh = _Tech.nativeSourceHandler;
  8540. } else {
  8541. log.error('No source handler found for the current source.');
  8542. }
  8543. }
  8544. // Dispose any existing source handler
  8545. this.disposeSourceHandler();
  8546. this.off('dispose', this.disposeSourceHandler_);
  8547. if (sh !== _Tech.nativeSourceHandler) {
  8548. this.currentSource_ = source;
  8549. }
  8550. this.sourceHandler_ = sh.handleSource(source, this, this.options_);
  8551. this.one('dispose', this.disposeSourceHandler_);
  8552. };
  8553. /**
  8554. * Clean up any existing SourceHandlers and listeners when the Tech is disposed.
  8555. *
  8556. * @listens Tech#dispose
  8557. */
  8558. _Tech.prototype.disposeSourceHandler = function () {
  8559. // if we have a source and get another one
  8560. // then we are loading something new
  8561. // than clear all of our current tracks
  8562. if (this.currentSource_) {
  8563. this.clearTracks(['audio', 'video']);
  8564. this.currentSource_ = null;
  8565. }
  8566. // always clean up auto-text tracks
  8567. this.cleanupAutoTextTracks();
  8568. if (this.sourceHandler_) {
  8569. if (this.sourceHandler_.dispose) {
  8570. this.sourceHandler_.dispose();
  8571. }
  8572. this.sourceHandler_ = null;
  8573. }
  8574. };
  8575. };
  8576. // The base Tech class needs to be registered as a Component. It is the only
  8577. // Tech that can be registered as a Component.
  8578. Component.registerComponent('Tech', Tech);
  8579. Tech.registerTech('Tech', Tech);
  8580. /**
  8581. * A list of techs that should be added to techOrder on Players
  8582. *
  8583. * @private
  8584. */
  8585. Tech.defaultTechOrder_ = [];
  8586. /**
  8587. * @file middleware.js
  8588. * @module middleware
  8589. */
  8590. const middlewares = {};
  8591. const middlewareInstances = {};
  8592. const TERMINATOR = {};
  8593. /**
  8594. * A middleware object is a plain JavaScript object that has methods that
  8595. * match the {@link Tech} methods found in the lists of allowed
  8596. * {@link module:middleware.allowedGetters|getters},
  8597. * {@link module:middleware.allowedSetters|setters}, and
  8598. * {@link module:middleware.allowedMediators|mediators}.
  8599. *
  8600. * @typedef {Object} MiddlewareObject
  8601. */
  8602. /**
  8603. * A middleware factory function that should return a
  8604. * {@link module:middleware~MiddlewareObject|MiddlewareObject}.
  8605. *
  8606. * This factory will be called for each player when needed, with the player
  8607. * passed in as an argument.
  8608. *
  8609. * @callback MiddlewareFactory
  8610. * @param { import('../player').default } player
  8611. * A Video.js player.
  8612. */
  8613. /**
  8614. * Define a middleware that the player should use by way of a factory function
  8615. * that returns a middleware object.
  8616. *
  8617. * @param {string} type
  8618. * The MIME type to match or `"*"` for all MIME types.
  8619. *
  8620. * @param {MiddlewareFactory} middleware
  8621. * A middleware factory function that will be executed for
  8622. * matching types.
  8623. */
  8624. function use(type, middleware) {
  8625. middlewares[type] = middlewares[type] || [];
  8626. middlewares[type].push(middleware);
  8627. }
  8628. /**
  8629. * Asynchronously sets a source using middleware by recursing through any
  8630. * matching middlewares and calling `setSource` on each, passing along the
  8631. * previous returned value each time.
  8632. *
  8633. * @param { import('../player').default } player
  8634. * A {@link Player} instance.
  8635. *
  8636. * @param {Tech~SourceObject} src
  8637. * A source object.
  8638. *
  8639. * @param {Function}
  8640. * The next middleware to run.
  8641. */
  8642. function setSource(player, src, next) {
  8643. player.setTimeout(() => setSourceHelper(src, middlewares[src.type], next, player), 1);
  8644. }
  8645. /**
  8646. * When the tech is set, passes the tech to each middleware's `setTech` method.
  8647. *
  8648. * @param {Object[]} middleware
  8649. * An array of middleware instances.
  8650. *
  8651. * @param { import('../tech/tech').default } tech
  8652. * A Video.js tech.
  8653. */
  8654. function setTech(middleware, tech) {
  8655. middleware.forEach(mw => mw.setTech && mw.setTech(tech));
  8656. }
  8657. /**
  8658. * Calls a getter on the tech first, through each middleware
  8659. * from right to left to the player.
  8660. *
  8661. * @param {Object[]} middleware
  8662. * An array of middleware instances.
  8663. *
  8664. * @param { import('../tech/tech').default } tech
  8665. * The current tech.
  8666. *
  8667. * @param {string} method
  8668. * A method name.
  8669. *
  8670. * @return {*}
  8671. * The final value from the tech after middleware has intercepted it.
  8672. */
  8673. function get(middleware, tech, method) {
  8674. return middleware.reduceRight(middlewareIterator(method), tech[method]());
  8675. }
  8676. /**
  8677. * Takes the argument given to the player and calls the setter method on each
  8678. * middleware from left to right to the tech.
  8679. *
  8680. * @param {Object[]} middleware
  8681. * An array of middleware instances.
  8682. *
  8683. * @param { import('../tech/tech').default } tech
  8684. * The current tech.
  8685. *
  8686. * @param {string} method
  8687. * A method name.
  8688. *
  8689. * @param {*} arg
  8690. * The value to set on the tech.
  8691. *
  8692. * @return {*}
  8693. * The return value of the `method` of the `tech`.
  8694. */
  8695. function set(middleware, tech, method, arg) {
  8696. return tech[method](middleware.reduce(middlewareIterator(method), arg));
  8697. }
  8698. /**
  8699. * Takes the argument given to the player and calls the `call` version of the
  8700. * method on each middleware from left to right.
  8701. *
  8702. * Then, call the passed in method on the tech and return the result unchanged
  8703. * back to the player, through middleware, this time from right to left.
  8704. *
  8705. * @param {Object[]} middleware
  8706. * An array of middleware instances.
  8707. *
  8708. * @param { import('../tech/tech').default } tech
  8709. * The current tech.
  8710. *
  8711. * @param {string} method
  8712. * A method name.
  8713. *
  8714. * @param {*} arg
  8715. * The value to set on the tech.
  8716. *
  8717. * @return {*}
  8718. * The return value of the `method` of the `tech`, regardless of the
  8719. * return values of middlewares.
  8720. */
  8721. function mediate(middleware, tech, method, arg = null) {
  8722. const callMethod = 'call' + toTitleCase(method);
  8723. const middlewareValue = middleware.reduce(middlewareIterator(callMethod), arg);
  8724. const terminated = middlewareValue === TERMINATOR;
  8725. // deprecated. The `null` return value should instead return TERMINATOR to
  8726. // prevent confusion if a techs method actually returns null.
  8727. const returnValue = terminated ? null : tech[method](middlewareValue);
  8728. executeRight(middleware, method, returnValue, terminated);
  8729. return returnValue;
  8730. }
  8731. /**
  8732. * Enumeration of allowed getters where the keys are method names.
  8733. *
  8734. * @type {Object}
  8735. */
  8736. const allowedGetters = {
  8737. buffered: 1,
  8738. currentTime: 1,
  8739. duration: 1,
  8740. muted: 1,
  8741. played: 1,
  8742. paused: 1,
  8743. seekable: 1,
  8744. volume: 1,
  8745. ended: 1
  8746. };
  8747. /**
  8748. * Enumeration of allowed setters where the keys are method names.
  8749. *
  8750. * @type {Object}
  8751. */
  8752. const allowedSetters = {
  8753. setCurrentTime: 1,
  8754. setMuted: 1,
  8755. setVolume: 1
  8756. };
  8757. /**
  8758. * Enumeration of allowed mediators where the keys are method names.
  8759. *
  8760. * @type {Object}
  8761. */
  8762. const allowedMediators = {
  8763. play: 1,
  8764. pause: 1
  8765. };
  8766. function middlewareIterator(method) {
  8767. return (value, mw) => {
  8768. // if the previous middleware terminated, pass along the termination
  8769. if (value === TERMINATOR) {
  8770. return TERMINATOR;
  8771. }
  8772. if (mw[method]) {
  8773. return mw[method](value);
  8774. }
  8775. return value;
  8776. };
  8777. }
  8778. function executeRight(mws, method, value, terminated) {
  8779. for (let i = mws.length - 1; i >= 0; i--) {
  8780. const mw = mws[i];
  8781. if (mw[method]) {
  8782. mw[method](terminated, value);
  8783. }
  8784. }
  8785. }
  8786. /**
  8787. * Clear the middleware cache for a player.
  8788. *
  8789. * @param { import('../player').default } player
  8790. * A {@link Player} instance.
  8791. */
  8792. function clearCacheForPlayer(player) {
  8793. middlewareInstances[player.id()] = null;
  8794. }
  8795. /**
  8796. * {
  8797. * [playerId]: [[mwFactory, mwInstance], ...]
  8798. * }
  8799. *
  8800. * @private
  8801. */
  8802. function getOrCreateFactory(player, mwFactory) {
  8803. const mws = middlewareInstances[player.id()];
  8804. let mw = null;
  8805. if (mws === undefined || mws === null) {
  8806. mw = mwFactory(player);
  8807. middlewareInstances[player.id()] = [[mwFactory, mw]];
  8808. return mw;
  8809. }
  8810. for (let i = 0; i < mws.length; i++) {
  8811. const [mwf, mwi] = mws[i];
  8812. if (mwf !== mwFactory) {
  8813. continue;
  8814. }
  8815. mw = mwi;
  8816. }
  8817. if (mw === null) {
  8818. mw = mwFactory(player);
  8819. mws.push([mwFactory, mw]);
  8820. }
  8821. return mw;
  8822. }
  8823. function setSourceHelper(src = {}, middleware = [], next, player, acc = [], lastRun = false) {
  8824. const [mwFactory, ...mwrest] = middleware;
  8825. // if mwFactory is a string, then we're at a fork in the road
  8826. if (typeof mwFactory === 'string') {
  8827. setSourceHelper(src, middlewares[mwFactory], next, player, acc, lastRun);
  8828. // if we have an mwFactory, call it with the player to get the mw,
  8829. // then call the mw's setSource method
  8830. } else if (mwFactory) {
  8831. const mw = getOrCreateFactory(player, mwFactory);
  8832. // if setSource isn't present, implicitly select this middleware
  8833. if (!mw.setSource) {
  8834. acc.push(mw);
  8835. return setSourceHelper(src, mwrest, next, player, acc, lastRun);
  8836. }
  8837. mw.setSource(Object.assign({}, src), function (err, _src) {
  8838. // something happened, try the next middleware on the current level
  8839. // make sure to use the old src
  8840. if (err) {
  8841. return setSourceHelper(src, mwrest, next, player, acc, lastRun);
  8842. }
  8843. // we've succeeded, now we need to go deeper
  8844. acc.push(mw);
  8845. // if it's the same type, continue down the current chain
  8846. // otherwise, we want to go down the new chain
  8847. setSourceHelper(_src, src.type === _src.type ? mwrest : middlewares[_src.type], next, player, acc, lastRun);
  8848. });
  8849. } else if (mwrest.length) {
  8850. setSourceHelper(src, mwrest, next, player, acc, lastRun);
  8851. } else if (lastRun) {
  8852. next(src, acc);
  8853. } else {
  8854. setSourceHelper(src, middlewares['*'], next, player, acc, true);
  8855. }
  8856. }
  8857. /**
  8858. * Mimetypes
  8859. *
  8860. * @see https://www.iana.org/assignments/media-types/media-types.xhtml
  8861. * @typedef Mimetypes~Kind
  8862. * @enum
  8863. */
  8864. const MimetypesKind = {
  8865. opus: 'video/ogg',
  8866. ogv: 'video/ogg',
  8867. mp4: 'video/mp4',
  8868. mov: 'video/mp4',
  8869. m4v: 'video/mp4',
  8870. mkv: 'video/x-matroska',
  8871. m4a: 'audio/mp4',
  8872. mp3: 'audio/mpeg',
  8873. aac: 'audio/aac',
  8874. caf: 'audio/x-caf',
  8875. flac: 'audio/flac',
  8876. oga: 'audio/ogg',
  8877. wav: 'audio/wav',
  8878. m3u8: 'application/x-mpegURL',
  8879. mpd: 'application/dash+xml',
  8880. jpg: 'image/jpeg',
  8881. jpeg: 'image/jpeg',
  8882. gif: 'image/gif',
  8883. png: 'image/png',
  8884. svg: 'image/svg+xml',
  8885. webp: 'image/webp'
  8886. };
  8887. /**
  8888. * Get the mimetype of a given src url if possible
  8889. *
  8890. * @param {string} src
  8891. * The url to the src
  8892. *
  8893. * @return {string}
  8894. * return the mimetype if it was known or empty string otherwise
  8895. */
  8896. const getMimetype = function (src = '') {
  8897. const ext = getFileExtension(src);
  8898. const mimetype = MimetypesKind[ext.toLowerCase()];
  8899. return mimetype || '';
  8900. };
  8901. /**
  8902. * Find the mime type of a given source string if possible. Uses the player
  8903. * source cache.
  8904. *
  8905. * @param { import('../player').default } player
  8906. * The player object
  8907. *
  8908. * @param {string} src
  8909. * The source string
  8910. *
  8911. * @return {string}
  8912. * The type that was found
  8913. */
  8914. const findMimetype = (player, src) => {
  8915. if (!src) {
  8916. return '';
  8917. }
  8918. // 1. check for the type in the `source` cache
  8919. if (player.cache_.source.src === src && player.cache_.source.type) {
  8920. return player.cache_.source.type;
  8921. }
  8922. // 2. see if we have this source in our `currentSources` cache
  8923. const matchingSources = player.cache_.sources.filter(s => s.src === src);
  8924. if (matchingSources.length) {
  8925. return matchingSources[0].type;
  8926. }
  8927. // 3. look for the src url in source elements and use the type there
  8928. const sources = player.$$('source');
  8929. for (let i = 0; i < sources.length; i++) {
  8930. const s = sources[i];
  8931. if (s.type && s.src && s.src === src) {
  8932. return s.type;
  8933. }
  8934. }
  8935. // 4. finally fallback to our list of mime types based on src url extension
  8936. return getMimetype(src);
  8937. };
  8938. /**
  8939. * @module filter-source
  8940. */
  8941. /**
  8942. * Filter out single bad source objects or multiple source objects in an
  8943. * array. Also flattens nested source object arrays into a 1 dimensional
  8944. * array of source objects.
  8945. *
  8946. * @param {Tech~SourceObject|Tech~SourceObject[]} src
  8947. * The src object to filter
  8948. *
  8949. * @return {Tech~SourceObject[]}
  8950. * An array of sourceobjects containing only valid sources
  8951. *
  8952. * @private
  8953. */
  8954. const filterSource = function (src) {
  8955. // traverse array
  8956. if (Array.isArray(src)) {
  8957. let newsrc = [];
  8958. src.forEach(function (srcobj) {
  8959. srcobj = filterSource(srcobj);
  8960. if (Array.isArray(srcobj)) {
  8961. newsrc = newsrc.concat(srcobj);
  8962. } else if (isObject(srcobj)) {
  8963. newsrc.push(srcobj);
  8964. }
  8965. });
  8966. src = newsrc;
  8967. } else if (typeof src === 'string' && src.trim()) {
  8968. // convert string into object
  8969. src = [fixSource({
  8970. src
  8971. })];
  8972. } else if (isObject(src) && typeof src.src === 'string' && src.src && src.src.trim()) {
  8973. // src is already valid
  8974. src = [fixSource(src)];
  8975. } else {
  8976. // invalid source, turn it into an empty array
  8977. src = [];
  8978. }
  8979. return src;
  8980. };
  8981. /**
  8982. * Checks src mimetype, adding it when possible
  8983. *
  8984. * @param {Tech~SourceObject} src
  8985. * The src object to check
  8986. * @return {Tech~SourceObject}
  8987. * src Object with known type
  8988. */
  8989. function fixSource(src) {
  8990. if (!src.type) {
  8991. const mimetype = getMimetype(src.src);
  8992. if (mimetype) {
  8993. src.type = mimetype;
  8994. }
  8995. }
  8996. return src;
  8997. }
  8998. var icons = "<svg xmlns=\"http://www.w3.org/2000/svg\">\n <defs>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-play\">\n <path d=\"M16 10v28l22-14z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-pause\">\n <path d=\"M12 38h8V10h-8v28zm16-28v28h8V10h-8z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-audio\">\n <path d=\"M24 2C14.06 2 6 10.06 6 20v14c0 3.31 2.69 6 6 6h6V24h-8v-4c0-7.73 6.27-14 14-14s14 6.27 14 14v4h-8v16h6c3.31 0 6-2.69 6-6V20c0-9.94-8.06-18-18-18z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-captions\">\n <path d=\"M38 8H10c-2.21 0-4 1.79-4 4v24c0 2.21 1.79 4 4 4h28c2.21 0 4-1.79 4-4V12c0-2.21-1.79-4-4-4zM22 22h-3v-1h-4v6h4v-1h3v2a2 2 0 0 1-2 2h-6a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v2zm14 0h-3v-1h-4v6h4v-1h3v2a2 2 0 0 1-2 2h-6a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v2z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-subtitles\">\n <path d=\"M40 8H8c-2.21 0-4 1.79-4 4v24c0 2.21 1.79 4 4 4h32c2.21 0 4-1.79 4-4V12c0-2.21-1.79-4-4-4zM8 24h8v4H8v-4zm20 12H8v-4h20v4zm12 0h-8v-4h8v4zm0-8H20v-4h20v4z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-fullscreen-enter\">\n <path d=\"M14 28h-4v10h10v-4h-6v-6zm-4-8h4v-6h6v-4H10v10zm24 14h-6v4h10V28h-4v6zm-6-24v4h6v6h4V10H28z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-fullscreen-exit\">\n <path d=\"M10 32h6v6h4V28H10v4zm6-16h-6v4h10V10h-4v6zm12 22h4v-6h6v-4H28v10zm4-22v-6h-4v10h10v-4h-6z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-play-circle\">\n <path d=\"M20 33l12-9-12-9v18zm4-29C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm0 36c-8.82 0-16-7.18-16-16S15.18 8 24 8s16 7.18 16 16-7.18 16-16 16z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-volume-mute\">\n <path d=\"M33 24c0-3.53-2.04-6.58-5-8.05v4.42l4.91 4.91c.06-.42.09-.85.09-1.28zm5 0c0 1.88-.41 3.65-1.08 5.28l3.03 3.03C41.25 29.82 42 27 42 24c0-8.56-5.99-15.72-14-17.54v4.13c5.78 1.72 10 7.07 10 13.41zM8.55 6L6 8.55 15.45 18H6v12h8l10 10V26.55l8.51 8.51c-1.34 1.03-2.85 1.86-4.51 2.36v4.13a17.94 17.94 0 0 0 7.37-3.62L39.45 42 42 39.45l-18-18L8.55 6zM24 8l-4.18 4.18L24 16.36V8z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-volume-low\">\n <path d=\"M14 18v12h8l10 10V8L22 18h-8z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-volume-medium\">\n <path d=\"M37 24c0-3.53-2.04-6.58-5-8.05v16.11c2.96-1.48 5-4.53 5-8.06zm-27-6v12h8l10 10V8L18 18h-8z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-volume-high\">\n <path d=\"M6 18v12h8l10 10V8L14 18H6zm27 6c0-3.53-2.04-6.58-5-8.05v16.11c2.96-1.48 5-4.53 5-8.06zM28 6.46v4.13c5.78 1.72 10 7.07 10 13.41s-4.22 11.69-10 13.41v4.13c8.01-1.82 14-8.97 14-17.54S36.01 8.28 28 6.46z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-spinner\">\n <path d=\"M18.8 21l9.53-16.51C26.94 4.18 25.49 4 24 4c-4.8 0-9.19 1.69-12.64 4.51l7.33 12.69.11-.2zm24.28-3c-1.84-5.85-6.3-10.52-11.99-12.68L23.77 18h19.31zm.52 2H28.62l.58 1 9.53 16.5C41.99 33.94 44 29.21 44 24c0-1.37-.14-2.71-.4-4zm-26.53 4l-7.8-13.5C6.01 14.06 4 18.79 4 24c0 1.37.14 2.71.4 4h14.98l-2.31-4zM4.92 30c1.84 5.85 6.3 10.52 11.99 12.68L24.23 30H4.92zm22.54 0l-7.8 13.51c1.4.31 2.85.49 4.34.49 4.8 0 9.19-1.69 12.64-4.51L29.31 26.8 27.46 30z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 24 24\" id=\"vjs-icon-hd\">\n <path d=\"M19 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-8 12H9.5v-2h-2v2H6V9h1.5v2.5h2V9H11v6zm2-6h4c.55 0 1 .45 1 1v4c0 .55-.45 1-1 1h-4V9zm1.5 4.5h2v-3h-2v3z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-chapters\">\n <path d=\"M6 26h4v-4H6v4zm0 8h4v-4H6v4zm0-16h4v-4H6v4zm8 8h28v-4H14v4zm0 8h28v-4H14v4zm0-20v4h28v-4H14z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 40 40\" id=\"vjs-icon-downloading\">\n <path d=\"M18.208 36.875q-3.208-.292-5.979-1.729-2.771-1.438-4.812-3.729-2.042-2.292-3.188-5.229-1.146-2.938-1.146-6.23 0-6.583 4.334-11.416 4.333-4.834 10.833-5.5v3.166q-5.167.75-8.583 4.646Q6.25 14.75 6.25 19.958q0 5.209 3.396 9.104 3.396 3.896 8.562 4.646zM20 28.417L11.542 20l2.083-2.083 4.917 4.916v-11.25h2.916v11.25l4.875-4.916L28.417 20zm1.792 8.458v-3.167q1.833-.25 3.541-.958 1.709-.708 3.167-1.875l2.333 2.292q-1.958 1.583-4.25 2.541-2.291.959-4.791 1.167zm6.791-27.792q-1.541-1.125-3.25-1.854-1.708-.729-3.541-1.021V3.042q2.5.25 4.77 1.208 2.271.958 4.271 2.5zm4.584 21.584l-2.25-2.25q1.166-1.5 1.854-3.209.687-1.708.937-3.541h3.209q-.292 2.5-1.229 4.791-.938 2.292-2.521 4.209zm.541-12.417q-.291-1.833-.958-3.562-.667-1.73-1.833-3.188l2.375-2.208q1.541 1.916 2.458 4.208.917 2.292 1.167 4.75z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-file-download\">\n <path d=\"M10.8 40.55q-1.35 0-2.375-1T7.4 37.15v-7.7h3.4v7.7h26.35v-7.7h3.4v7.7q0 1.4-1 2.4t-2.4 1zM24 32.1L13.9 22.05l2.45-2.45 5.95 5.95V7.15h3.4v18.4l5.95-5.95 2.45 2.45z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-file-download-done\">\n <path d=\"M9.8 40.5v-3.45h28.4v3.45zm9.2-9.05L7.4 19.85l2.45-2.35L19 26.65l19.2-19.2 2.4 2.4z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-file-download-off\">\n <path d=\"M4.9 4.75L43.25 43.1 41 45.3l-4.75-4.75q-.05.05-.075.025-.025-.025-.075-.025H10.8q-1.35 0-2.375-1T7.4 37.15v-7.7h3.4v7.7h22.05l-7-7-1.85 1.8L13.9 21.9l1.85-1.85L2.7 7zm26.75 14.7l2.45 2.45-3.75 3.8-2.45-2.5zM25.7 7.15V21.1l-3.4-3.45V7.15z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-share\">\n <path d=\"M36 32.17c-1.52 0-2.89.59-3.93 1.54L17.82 25.4c.11-.45.18-.92.18-1.4s-.07-.95-.18-1.4l14.1-8.23c1.07 1 2.5 1.62 4.08 1.62 3.31 0 6-2.69 6-6s-2.69-6-6-6-6 2.69-6 6c0 .48.07.95.18 1.4l-14.1 8.23c-1.07-1-2.5-1.62-4.08-1.62-3.31 0-6 2.69-6 6s2.69 6 6 6c1.58 0 3.01-.62 4.08-1.62l14.25 8.31c-.1.42-.16.86-.16 1.31A5.83 5.83 0 1 0 36 32.17z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-cog\">\n <path d=\"M38.86 25.95c.08-.64.14-1.29.14-1.95s-.06-1.31-.14-1.95l4.23-3.31c.38-.3.49-.84.24-1.28l-4-6.93c-.25-.43-.77-.61-1.22-.43l-4.98 2.01c-1.03-.79-2.16-1.46-3.38-1.97L29 4.84c-.09-.47-.5-.84-1-.84h-8c-.5 0-.91.37-.99.84l-.75 5.3a14.8 14.8 0 0 0-3.38 1.97L9.9 10.1a1 1 0 0 0-1.22.43l-4 6.93c-.25.43-.14.97.24 1.28l4.22 3.31C9.06 22.69 9 23.34 9 24s.06 1.31.14 1.95l-4.22 3.31c-.38.3-.49.84-.24 1.28l4 6.93c.25.43.77.61 1.22.43l4.98-2.01c1.03.79 2.16 1.46 3.38 1.97l.75 5.3c.08.47.49.84.99.84h8c.5 0 .91-.37.99-.84l.75-5.3a14.8 14.8 0 0 0 3.38-1.97l4.98 2.01a1 1 0 0 0 1.22-.43l4-6.93c.25-.43.14-.97-.24-1.28l-4.22-3.31zM24 31c-3.87 0-7-3.13-7-7s3.13-7 7-7 7 3.13 7 7-3.13 7-7 7z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-square\">\n <path d=\"M36 8H12c-2.21 0-4 1.79-4 4v24c0 2.21 1.79 4 4 4h24c2.21 0 4-1.79 4-4V12c0-2.21-1.79-4-4-4zm0 28H12V12h24v24z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-circle\">\n <circle cx=\"24\" cy=\"24\" r=\"20\"></circle>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-circle-outline\">\n <path d=\"M24 4C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm0 36c-8.82 0-16-7.18-16-16S15.18 8 24 8s16 7.18 16 16-7.18 16-16 16z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-circle-inner-circle\">\n <path d=\"M24 4C12.97 4 4 12.97 4 24s8.97 20 20 20 20-8.97 20-20S35.03 4 24 4zm0 36c-8.82 0-16-7.18-16-16S15.18 8 24 8s16 7.18 16 16-7.18 16-16 16zm6-16c0 3.31-2.69 6-6 6s-6-2.69-6-6 2.69-6 6-6 6 2.69 6 6z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-cancel\">\n <path d=\"M24 4C12.95 4 4 12.95 4 24s8.95 20 20 20 20-8.95 20-20S35.05 4 24 4zm10 27.17L31.17 34 24 26.83 16.83 34 14 31.17 21.17 24 14 16.83 16.83 14 24 21.17 31.17 14 34 16.83 26.83 24 34 31.17z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-replay\">\n <path d=\"M24 10V2L14 12l10 10v-8c6.63 0 12 5.37 12 12s-5.37 12-12 12-12-5.37-12-12H8c0 8.84 7.16 16 16 16s16-7.16 16-16-7.16-16-16-16z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-repeat\">\n <path d=\"M14 14h20v6l8-8-8-8v6H10v12h4v-8zm20 20H14v-6l-8 8 8 8v-6h24V26h-4v8z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-replay-5\">\n <path d=\"M17.689 98l-8.697 8.696 8.697 8.697 2.486-2.485-4.32-4.319h1.302c4.93 0 9.071 1.722 12.424 5.165 3.352 3.443 5.029 7.638 5.029 12.584h3.55c0-2.958-.553-5.73-1.658-8.313-1.104-2.583-2.622-4.841-4.555-6.774-1.932-1.932-4.19-3.45-6.773-4.555-2.584-1.104-5.355-1.657-8.313-1.657H15.5l4.615-4.615zm-8.08 21.659v13.861h11.357v5.008H9.609V143h12.7c.834 0 1.55-.298 2.146-.894.596-.597.895-1.31.895-2.145v-7.781c0-.835-.299-1.55-.895-2.147a2.929 2.929 0 0 0-2.147-.894h-8.227v-5.096H25.35v-4.384z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-replay-10\">\n <path d=\"M42.315 125.63c0-4.997-1.694-9.235-5.08-12.713-3.388-3.479-7.571-5.218-12.552-5.218h-1.315l4.363 4.363-2.51 2.51-8.787-8.786L25.221 97l2.45 2.45-4.662 4.663h1.375c2.988 0 5.788.557 8.397 1.673 2.61 1.116 4.892 2.65 6.844 4.602 1.953 1.953 3.487 4.234 4.602 6.844 1.116 2.61 1.674 5.41 1.674 8.398zM8.183 142v-19.657H3.176V117.8h9.643V142zm13.63 0c-1.156 0-2.127-.393-2.912-1.178-.778-.778-1.168-1.746-1.168-2.902v-16.04c0-1.156.393-2.127 1.178-2.912.779-.779 1.746-1.168 2.902-1.168h7.696c1.156 0 2.126.392 2.911 1.177.779.78 1.168 1.747 1.168 2.903v16.04c0 1.156-.392 2.127-1.177 2.912-.779.779-1.746 1.168-2.902 1.168zm.556-4.636h6.583v-15.02H22.37z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-replay-30\">\n <path d=\"M26.047 97l-8.733 8.732 8.733 8.733 2.496-2.494-4.336-4.338h1.307c4.95 0 9.108 1.73 12.474 5.187 3.367 3.458 5.051 7.668 5.051 12.635h3.565c0-2.97-.556-5.751-1.665-8.346-1.109-2.594-2.633-4.862-4.574-6.802-1.94-1.941-4.208-3.466-6.803-4.575-2.594-1.109-5.375-1.664-8.345-1.664H23.85l4.634-4.634zM2.555 117.531v4.688h10.297v5.25H5.873v4.687h6.979v5.156H2.555V142H13.36c1.061 0 1.95-.395 2.668-1.186.718-.79 1.076-1.772 1.076-2.94v-16.218c0-1.168-.358-2.149-1.076-2.94-.717-.79-1.607-1.185-2.668-1.185zm22.482.14c-1.149 0-2.11.39-2.885 1.165-.78.78-1.172 1.744-1.172 2.893v15.943c0 1.149.388 2.11 1.163 2.885.78.78 1.745 1.172 2.894 1.172h7.649c1.148 0 2.11-.388 2.884-1.163.78-.78 1.17-1.745 1.17-2.894v-15.943c0-1.15-.386-2.111-1.16-2.885-.78-.78-1.746-1.172-2.894-1.172zm.553 4.518h6.545v14.93H25.59z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-forward-5\">\n <path d=\"M29.508 97l-2.431 2.43 4.625 4.625h-1.364c-2.965 0-5.742.554-8.332 1.66-2.589 1.107-4.851 2.629-6.788 4.566-1.937 1.937-3.458 4.2-4.565 6.788-1.107 2.59-1.66 5.367-1.66 8.331h3.557c0-4.957 1.68-9.16 5.04-12.611 3.36-3.45 7.51-5.177 12.451-5.177h1.304l-4.326 4.33 2.49 2.49 8.715-8.716zm-9.783 21.61v13.89h11.382v5.018H19.725V142h12.727a2.93 2.93 0 0 0 2.15-.896 2.93 2.93 0 0 0 .896-2.15v-7.798c0-.837-.299-1.554-.896-2.152a2.93 2.93 0 0 0-2.15-.896h-8.245V123h11.29v-4.392z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-forward-10\">\n <path d=\"M23.119 97l-2.386 2.383 4.538 4.538h-1.339c-2.908 0-5.633.543-8.173 1.63-2.54 1.085-4.76 2.577-6.66 4.478-1.9 1.9-3.392 4.12-4.478 6.66-1.085 2.54-1.629 5.264-1.629 8.172h3.49c0-4.863 1.648-8.986 4.944-12.372 3.297-3.385 7.368-5.078 12.216-5.078h1.279l-4.245 4.247 2.443 2.442 8.55-8.55zm-9.52 21.45v4.42h4.871V142h4.513v-23.55zm18.136 0c-1.125 0-2.066.377-2.824 1.135-.764.764-1.148 1.709-1.148 2.834v15.612c0 1.124.38 2.066 1.139 2.824.764.764 1.708 1.145 2.833 1.145h7.489c1.125 0 2.066-.378 2.824-1.136.764-.764 1.145-1.709 1.145-2.833v-15.612c0-1.125-.378-2.067-1.136-2.825-.764-.764-1.708-1.145-2.833-1.145zm.54 4.42h6.408v14.617h-6.407z\"></path>\n </symbol>\n <symbol viewBox=\"0 96 48 48\" id=\"vjs-icon-forward-30\">\n <path d=\"M25.549 97l-2.437 2.434 4.634 4.635H26.38c-2.97 0-5.753.555-8.347 1.664-2.594 1.109-4.861 2.633-6.802 4.574-1.94 1.94-3.465 4.207-4.574 6.802-1.109 2.594-1.664 5.377-1.664 8.347h3.565c0-4.967 1.683-9.178 5.05-12.636 3.366-3.458 7.525-5.187 12.475-5.187h1.307l-4.335 4.338 2.495 2.494 8.732-8.732zm-11.553 20.53v4.689h10.297v5.249h-6.978v4.688h6.978v5.156H13.996V142h10.808c1.06 0 1.948-.395 2.666-1.186.718-.79 1.077-1.771 1.077-2.94v-16.217c0-1.169-.36-2.15-1.077-2.94-.718-.79-1.605-1.186-2.666-1.186zm21.174.168c-1.149 0-2.11.389-2.884 1.163-.78.78-1.172 1.745-1.172 2.894v15.942c0 1.15.388 2.11 1.162 2.885.78.78 1.745 1.17 2.894 1.17h7.649c1.149 0 2.11-.386 2.885-1.16.78-.78 1.17-1.746 1.17-2.895v-15.942c0-1.15-.387-2.11-1.161-2.885-.78-.78-1.745-1.172-2.894-1.172zm.552 4.516h6.542v14.931h-6.542z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 512 512\" id=\"vjs-icon-audio-description\">\n <g fill-rule=\"evenodd\"><path d=\"M227.29 381.351V162.993c50.38-1.017 89.108-3.028 117.631 17.126 27.374 19.342 48.734 56.965 44.89 105.325-4.067 51.155-41.335 94.139-89.776 98.475-24.085 2.155-71.972 0-71.972 0s-.84-1.352-.773-2.568m48.755-54.804c31.43 1.26 53.208-16.633 56.495-45.386 4.403-38.51-21.188-63.552-58.041-60.796v103.612c-.036 1.466.575 2.22 1.546 2.57\"></path><path d=\"M383.78 381.328c13.336 3.71 17.387-11.06 23.215-21.408 12.722-22.571 22.294-51.594 22.445-84.774.221-47.594-18.343-82.517-35.6-106.182h-8.51c-.587 3.874 2.226 7.315 3.865 10.276 13.166 23.762 25.367 56.553 25.54 94.194.2 43.176-14.162 79.278-30.955 107.894\"></path><path d=\"M425.154 381.328c13.336 3.71 17.384-11.061 23.215-21.408 12.721-22.571 22.291-51.594 22.445-84.774.221-47.594-18.343-82.517-35.6-106.182h-8.511c-.586 3.874 2.226 7.315 3.866 10.276 13.166 23.762 25.367 56.553 25.54 94.194.2 43.176-14.162 79.278-30.955 107.894\"></path><path d=\"M466.26 381.328c13.337 3.71 17.385-11.061 23.216-21.408 12.722-22.571 22.292-51.594 22.445-84.774.221-47.594-18.343-82.517-35.6-106.182h-8.51c-.587 3.874 2.225 7.315 3.865 10.276 13.166 23.762 25.367 56.553 25.54 94.194.2 43.176-14.162 79.278-30.955 107.894M4.477 383.005H72.58l18.573-28.484 64.169-.135s.065 19.413.065 28.62h48.756V160.307h-58.816c-5.653 9.537-140.85 222.697-140.85 222.697zm152.667-145.282v71.158l-40.453-.27 40.453-70.888z\"></path></g>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-next-item\">\n <path d=\"M12 36l17-12-17-12v24zm20-24v24h4V12h-4z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-previous-item\">\n <path d=\"M12 12h4v24h-4zm7 12l17 12V12z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-shuffle\">\n <path d=\"M21.17 18.34L10.83 8 8 10.83l10.34 10.34 2.83-2.83zM29 8l4.09 4.09L8 37.17 10.83 40l25.09-25.09L40 19V8H29zm.66 18.83l-2.83 2.83 6.26 6.26L29 40h11V29l-4.09 4.09-6.25-6.26z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-cast\">\n <path d=\"M42 6H6c-2.21 0-4 1.79-4 4v6h4v-6h36v28H28v4h14c2.21 0 4-1.79 4-4V10c0-2.21-1.79-4-4-4zM2 36v6h6c0-3.31-2.69-6-6-6zm0-8v4c5.52 0 10 4.48 10 10h4c0-7.73-6.27-14-14-14zm0-8v4c9.94 0 18 8.06 18 18h4c0-12.15-9.85-22-22-22z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 48 48\" id=\"vjs-icon-picture-in-picture-enter\">\n <path d=\"M38 22H22v11.99h16V22zm8 16V9.96C46 7.76 44.2 6 42 6H6C3.8 6 2 7.76 2 9.96V38c0 2.2 1.8 4 4 4h36c2.2 0 4-1.8 4-4zm-4 .04H6V9.94h36v28.1z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 22 18\" id=\"vjs-icon-picture-in-picture-exit\">\n <path d=\"M18 4H4v10h14V4zm4 12V1.98C22 .88 21.1 0 20 0H2C.9 0 0 .88 0 1.98V16c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2zm-2 .02H2V1.97h18v14.05z\"></path>\n <path fill=\"none\" d=\"M-1-3h24v24H-1z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 1792 1792\" id=\"vjs-icon-facebook\">\n <path d=\"M1343 12v264h-157q-86 0-116 36t-30 108v189h293l-39 296h-254v759H734V905H479V609h255V391q0-186 104-288.5T1115 0q147 0 228 12z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 1792 1792\" id=\"vjs-icon-linkedin\">\n <path d=\"M477 625v991H147V625h330zm21-306q1 73-50.5 122T312 490h-2q-82 0-132-49t-50-122q0-74 51.5-122.5T314 148t133 48.5T498 319zm1166 729v568h-329v-530q0-105-40.5-164.5T1168 862q-63 0-105.5 34.5T999 982q-11 30-11 81v553H659q2-399 2-647t-1-296l-1-48h329v144h-2q20-32 41-56t56.5-52 87-43.5T1285 602q171 0 275 113.5t104 332.5z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 1792 1792\" id=\"vjs-icon-twitter\">\n <path d=\"M1684 408q-67 98-162 167 1 14 1 42 0 130-38 259.5T1369.5 1125 1185 1335.5t-258 146-323 54.5q-271 0-496-145 35 4 78 4 225 0 401-138-105-2-188-64.5T285 1033q33 5 61 5 43 0 85-11-112-23-185.5-111.5T172 710v-4q68 38 146 41-66-44-105-115t-39-154q0-88 44-163 121 149 294.5 238.5T884 653q-8-38-8-74 0-134 94.5-228.5T1199 256q140 0 236 102 109-21 205-78-37 115-142 178 93-10 186-50z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 1792 1792\" id=\"vjs-icon-tumblr\">\n <path d=\"M1328 1329l80 237q-23 35-111 66t-177 32q-104 2-190.5-26T787 1564t-95-106-55.5-120-16.5-118V676H452V461q72-26 129-69.5t91-90 58-102 34-99T779 12q1-5 4.5-8.5T791 0h244v424h333v252h-334v518q0 30 6.5 56t22.5 52.5 49.5 41.5 81.5 14q78-2 134-29z\"></path>\n </symbol>\n <symbol viewBox=\"0 0 1792 1792\" id=\"vjs-icon-pinterest\">\n <path d=\"M1664 896q0 209-103 385.5T1281.5 1561 896 1664q-111 0-218-32 59-93 78-164 9-34 54-211 20 39 73 67.5t114 28.5q121 0 216-68.5t147-188.5 52-270q0-114-59.5-214T1180 449t-255-63q-105 0-196 29t-154.5 77-109 110.5-67 129.5T377 866q0 104 40 183t117 111q30 12 38-20 2-7 8-31t8-30q6-23-11-43-51-61-51-151 0-151 104.5-259.5T904 517q151 0 235.5 82t84.5 213q0 170-68.5 289T980 1220q-61 0-98-43.5T859 1072q8-35 26.5-93.5t30-103T927 800q0-50-27-83t-77-33q-62 0-105 57t-43 142q0 73 25 122l-99 418q-17 70-13 177-206-91-333-281T128 896q0-209 103-385.5T510.5 231 896 128t385.5 103T1561 510.5 1664 896z\"></path>\n </symbol>\n </defs>\n</svg>";
  8999. /**
  9000. * @file loader.js
  9001. */
  9002. /**
  9003. * The `MediaLoader` is the `Component` that decides which playback technology to load
  9004. * when a player is initialized.
  9005. *
  9006. * @extends Component
  9007. */
  9008. class MediaLoader extends Component {
  9009. /**
  9010. * Create an instance of this class.
  9011. *
  9012. * @param { import('../player').default } player
  9013. * The `Player` that this class should attach to.
  9014. *
  9015. * @param {Object} [options]
  9016. * The key/value store of player options.
  9017. *
  9018. * @param {Function} [ready]
  9019. * The function that is run when this component is ready.
  9020. */
  9021. constructor(player, options, ready) {
  9022. // MediaLoader has no element
  9023. const options_ = merge({
  9024. createEl: false
  9025. }, options);
  9026. super(player, options_, ready);
  9027. // If there are no sources when the player is initialized,
  9028. // load the first supported playback technology.
  9029. if (!options.playerOptions.sources || options.playerOptions.sources.length === 0) {
  9030. for (let i = 0, j = options.playerOptions.techOrder; i < j.length; i++) {
  9031. const techName = toTitleCase(j[i]);
  9032. let tech = Tech.getTech(techName);
  9033. // Support old behavior of techs being registered as components.
  9034. // Remove once that deprecated behavior is removed.
  9035. if (!techName) {
  9036. tech = Component.getComponent(techName);
  9037. }
  9038. // Check if the browser supports this technology
  9039. if (tech && tech.isSupported()) {
  9040. player.loadTech_(techName);
  9041. break;
  9042. }
  9043. }
  9044. } else {
  9045. // Loop through playback technologies (e.g. HTML5) and check for support.
  9046. // Then load the best source.
  9047. // A few assumptions here:
  9048. // All playback technologies respect preload false.
  9049. player.src(options.playerOptions.sources);
  9050. }
  9051. }
  9052. }
  9053. Component.registerComponent('MediaLoader', MediaLoader);
  9054. /**
  9055. * @file clickable-component.js
  9056. */
  9057. /**
  9058. * Component which is clickable or keyboard actionable, but is not a
  9059. * native HTML button.
  9060. *
  9061. * @extends Component
  9062. */
  9063. class ClickableComponent extends Component {
  9064. /**
  9065. * Creates an instance of this class.
  9066. *
  9067. * @param { import('./player').default } player
  9068. * The `Player` that this class should be attached to.
  9069. *
  9070. * @param {Object} [options]
  9071. * The key/value store of component options.
  9072. *
  9073. * @param {function} [options.clickHandler]
  9074. * The function to call when the button is clicked / activated
  9075. *
  9076. * @param {string} [options.controlText]
  9077. * The text to set on the button
  9078. *
  9079. * @param {string} [options.className]
  9080. * A class or space separated list of classes to add the component
  9081. *
  9082. */
  9083. constructor(player, options) {
  9084. super(player, options);
  9085. if (this.options_.controlText) {
  9086. this.controlText(this.options_.controlText);
  9087. }
  9088. this.handleMouseOver_ = e => this.handleMouseOver(e);
  9089. this.handleMouseOut_ = e => this.handleMouseOut(e);
  9090. this.handleClick_ = e => this.handleClick(e);
  9091. this.handleKeyDown_ = e => this.handleKeyDown(e);
  9092. this.emitTapEvents();
  9093. this.enable();
  9094. }
  9095. /**
  9096. * Create the `ClickableComponent`s DOM element.
  9097. *
  9098. * @param {string} [tag=div]
  9099. * The element's node type.
  9100. *
  9101. * @param {Object} [props={}]
  9102. * An object of properties that should be set on the element.
  9103. *
  9104. * @param {Object} [attributes={}]
  9105. * An object of attributes that should be set on the element.
  9106. *
  9107. * @return {Element}
  9108. * The element that gets created.
  9109. */
  9110. createEl(tag = 'div', props = {}, attributes = {}) {
  9111. props = Object.assign({
  9112. className: this.buildCSSClass(),
  9113. tabIndex: 0
  9114. }, props);
  9115. if (tag === 'button') {
  9116. log.error(`Creating a ClickableComponent with an HTML element of ${tag} is not supported; use a Button instead.`);
  9117. }
  9118. // Add ARIA attributes for clickable element which is not a native HTML button
  9119. attributes = Object.assign({
  9120. role: 'button'
  9121. }, attributes);
  9122. this.tabIndex_ = props.tabIndex;
  9123. const el = createEl(tag, props, attributes);
  9124. if (!this.player_.options_.experimentalSvgIcons) {
  9125. el.appendChild(createEl('span', {
  9126. className: 'vjs-icon-placeholder'
  9127. }, {
  9128. 'aria-hidden': true
  9129. }));
  9130. }
  9131. this.createControlTextEl(el);
  9132. return el;
  9133. }
  9134. dispose() {
  9135. // remove controlTextEl_ on dispose
  9136. this.controlTextEl_ = null;
  9137. super.dispose();
  9138. }
  9139. /**
  9140. * Create a control text element on this `ClickableComponent`
  9141. *
  9142. * @param {Element} [el]
  9143. * Parent element for the control text.
  9144. *
  9145. * @return {Element}
  9146. * The control text element that gets created.
  9147. */
  9148. createControlTextEl(el) {
  9149. this.controlTextEl_ = createEl('span', {
  9150. className: 'vjs-control-text'
  9151. }, {
  9152. // let the screen reader user know that the text of the element may change
  9153. 'aria-live': 'polite'
  9154. });
  9155. if (el) {
  9156. el.appendChild(this.controlTextEl_);
  9157. }
  9158. this.controlText(this.controlText_, el);
  9159. return this.controlTextEl_;
  9160. }
  9161. /**
  9162. * Get or set the localize text to use for the controls on the `ClickableComponent`.
  9163. *
  9164. * @param {string} [text]
  9165. * Control text for element.
  9166. *
  9167. * @param {Element} [el=this.el()]
  9168. * Element to set the title on.
  9169. *
  9170. * @return {string}
  9171. * - The control text when getting
  9172. */
  9173. controlText(text, el = this.el()) {
  9174. if (text === undefined) {
  9175. return this.controlText_ || 'Need Text';
  9176. }
  9177. const localizedText = this.localize(text);
  9178. /** @protected */
  9179. this.controlText_ = text;
  9180. textContent(this.controlTextEl_, localizedText);
  9181. if (!this.nonIconControl && !this.player_.options_.noUITitleAttributes) {
  9182. // Set title attribute if only an icon is shown
  9183. el.setAttribute('title', localizedText);
  9184. }
  9185. }
  9186. /**
  9187. * Builds the default DOM `className`.
  9188. *
  9189. * @return {string}
  9190. * The DOM `className` for this object.
  9191. */
  9192. buildCSSClass() {
  9193. return `vjs-control vjs-button ${super.buildCSSClass()}`;
  9194. }
  9195. /**
  9196. * Enable this `ClickableComponent`
  9197. */
  9198. enable() {
  9199. if (!this.enabled_) {
  9200. this.enabled_ = true;
  9201. this.removeClass('vjs-disabled');
  9202. this.el_.setAttribute('aria-disabled', 'false');
  9203. if (typeof this.tabIndex_ !== 'undefined') {
  9204. this.el_.setAttribute('tabIndex', this.tabIndex_);
  9205. }
  9206. this.on(['tap', 'click'], this.handleClick_);
  9207. this.on('keydown', this.handleKeyDown_);
  9208. }
  9209. }
  9210. /**
  9211. * Disable this `ClickableComponent`
  9212. */
  9213. disable() {
  9214. this.enabled_ = false;
  9215. this.addClass('vjs-disabled');
  9216. this.el_.setAttribute('aria-disabled', 'true');
  9217. if (typeof this.tabIndex_ !== 'undefined') {
  9218. this.el_.removeAttribute('tabIndex');
  9219. }
  9220. this.off('mouseover', this.handleMouseOver_);
  9221. this.off('mouseout', this.handleMouseOut_);
  9222. this.off(['tap', 'click'], this.handleClick_);
  9223. this.off('keydown', this.handleKeyDown_);
  9224. }
  9225. /**
  9226. * Handles language change in ClickableComponent for the player in components
  9227. *
  9228. *
  9229. */
  9230. handleLanguagechange() {
  9231. this.controlText(this.controlText_);
  9232. }
  9233. /**
  9234. * Event handler that is called when a `ClickableComponent` receives a
  9235. * `click` or `tap` event.
  9236. *
  9237. * @param {Event} event
  9238. * The `tap` or `click` event that caused this function to be called.
  9239. *
  9240. * @listens tap
  9241. * @listens click
  9242. * @abstract
  9243. */
  9244. handleClick(event) {
  9245. if (this.options_.clickHandler) {
  9246. this.options_.clickHandler.call(this, arguments);
  9247. }
  9248. }
  9249. /**
  9250. * Event handler that is called when a `ClickableComponent` receives a
  9251. * `keydown` event.
  9252. *
  9253. * By default, if the key is Space or Enter, it will trigger a `click` event.
  9254. *
  9255. * @param {KeyboardEvent} event
  9256. * The `keydown` event that caused this function to be called.
  9257. *
  9258. * @listens keydown
  9259. */
  9260. handleKeyDown(event) {
  9261. // Support Space or Enter key operation to fire a click event. Also,
  9262. // prevent the event from propagating through the DOM and triggering
  9263. // Player hotkeys.
  9264. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  9265. event.preventDefault();
  9266. event.stopPropagation();
  9267. this.trigger('click');
  9268. } else {
  9269. // Pass keypress handling up for unsupported keys
  9270. super.handleKeyDown(event);
  9271. }
  9272. }
  9273. }
  9274. Component.registerComponent('ClickableComponent', ClickableComponent);
  9275. /**
  9276. * @file poster-image.js
  9277. */
  9278. /**
  9279. * A `ClickableComponent` that handles showing the poster image for the player.
  9280. *
  9281. * @extends ClickableComponent
  9282. */
  9283. class PosterImage extends ClickableComponent {
  9284. /**
  9285. * Create an instance of this class.
  9286. *
  9287. * @param { import('./player').default } player
  9288. * The `Player` that this class should attach to.
  9289. *
  9290. * @param {Object} [options]
  9291. * The key/value store of player options.
  9292. */
  9293. constructor(player, options) {
  9294. super(player, options);
  9295. this.update();
  9296. this.update_ = e => this.update(e);
  9297. player.on('posterchange', this.update_);
  9298. }
  9299. /**
  9300. * Clean up and dispose of the `PosterImage`.
  9301. */
  9302. dispose() {
  9303. this.player().off('posterchange', this.update_);
  9304. super.dispose();
  9305. }
  9306. /**
  9307. * Create the `PosterImage`s DOM element.
  9308. *
  9309. * @return {Element}
  9310. * The element that gets created.
  9311. */
  9312. createEl() {
  9313. // The el is an empty div to keep position in the DOM
  9314. // A picture and img el will be inserted when a source is set
  9315. return createEl('div', {
  9316. className: 'vjs-poster'
  9317. });
  9318. }
  9319. /**
  9320. * Get or set the `PosterImage`'s crossOrigin option.
  9321. *
  9322. * @param {string|null} [value]
  9323. * The value to set the crossOrigin to. If an argument is
  9324. * given, must be one of `'anonymous'` or `'use-credentials'`, or 'null'.
  9325. *
  9326. * @return {string|null}
  9327. * - The current crossOrigin value of the `Player` when getting.
  9328. * - undefined when setting
  9329. */
  9330. crossOrigin(value) {
  9331. // `null` can be set to unset a value
  9332. if (typeof value === 'undefined') {
  9333. if (this.$('img')) {
  9334. // If the poster's element exists, give its value
  9335. return this.$('img').crossOrigin;
  9336. } else if (this.player_.tech_ && this.player_.tech_.isReady_) {
  9337. // If not but the tech is ready, query the tech
  9338. return this.player_.crossOrigin();
  9339. }
  9340. // Otherwise check options as the poster is usually set before the state of crossorigin
  9341. // can be retrieved by the getter
  9342. return this.player_.options_.crossOrigin || this.player_.options_.crossorigin || null;
  9343. }
  9344. if (value !== null && value !== 'anonymous' && value !== 'use-credentials') {
  9345. this.player_.log.warn(`crossOrigin must be null, "anonymous" or "use-credentials", given "${value}"`);
  9346. return;
  9347. }
  9348. if (this.$('img')) {
  9349. this.$('img').crossOrigin = value;
  9350. }
  9351. return;
  9352. }
  9353. /**
  9354. * An {@link EventTarget~EventListener} for {@link Player#posterchange} events.
  9355. *
  9356. * @listens Player#posterchange
  9357. *
  9358. * @param {Event} [event]
  9359. * The `Player#posterchange` event that triggered this function.
  9360. */
  9361. update(event) {
  9362. const url = this.player().poster();
  9363. this.setSrc(url);
  9364. // If there's no poster source we should display:none on this component
  9365. // so it's not still clickable or right-clickable
  9366. if (url) {
  9367. this.show();
  9368. } else {
  9369. this.hide();
  9370. }
  9371. }
  9372. /**
  9373. * Set the source of the `PosterImage` depending on the display method. (Re)creates
  9374. * the inner picture and img elementss when needed.
  9375. *
  9376. * @param {string} [url]
  9377. * The URL to the source for the `PosterImage`. If not specified or falsy,
  9378. * any source and ant inner picture/img are removed.
  9379. */
  9380. setSrc(url) {
  9381. if (!url) {
  9382. this.el_.textContent = '';
  9383. return;
  9384. }
  9385. if (!this.$('img')) {
  9386. this.el_.appendChild(createEl('picture', {
  9387. className: 'vjs-poster',
  9388. // Don't want poster to be tabbable.
  9389. tabIndex: -1
  9390. }, {}, createEl('img', {
  9391. loading: 'lazy',
  9392. crossOrigin: this.crossOrigin()
  9393. }, {
  9394. alt: ''
  9395. })));
  9396. }
  9397. this.$('img').src = url;
  9398. }
  9399. /**
  9400. * An {@link EventTarget~EventListener} for clicks on the `PosterImage`. See
  9401. * {@link ClickableComponent#handleClick} for instances where this will be triggered.
  9402. *
  9403. * @listens tap
  9404. * @listens click
  9405. * @listens keydown
  9406. *
  9407. * @param {Event} event
  9408. + The `click`, `tap` or `keydown` event that caused this function to be called.
  9409. */
  9410. handleClick(event) {
  9411. // We don't want a click to trigger playback when controls are disabled
  9412. if (!this.player_.controls()) {
  9413. return;
  9414. }
  9415. if (this.player_.tech(true)) {
  9416. this.player_.tech(true).focus();
  9417. }
  9418. if (this.player_.paused()) {
  9419. silencePromise(this.player_.play());
  9420. } else {
  9421. this.player_.pause();
  9422. }
  9423. }
  9424. }
  9425. /**
  9426. * Get or set the `PosterImage`'s crossorigin option. For the HTML5 player, this
  9427. * sets the `crossOrigin` property on the `<img>` tag to control the CORS
  9428. * behavior.
  9429. *
  9430. * @param {string|null} [value]
  9431. * The value to set the `PosterImages`'s crossorigin to. If an argument is
  9432. * given, must be one of `anonymous` or `use-credentials`.
  9433. *
  9434. * @return {string|null|undefined}
  9435. * - The current crossorigin value of the `Player` when getting.
  9436. * - undefined when setting
  9437. */
  9438. PosterImage.prototype.crossorigin = PosterImage.prototype.crossOrigin;
  9439. Component.registerComponent('PosterImage', PosterImage);
  9440. /**
  9441. * @file text-track-display.js
  9442. */
  9443. const darkGray = '#222';
  9444. const lightGray = '#ccc';
  9445. const fontMap = {
  9446. monospace: 'monospace',
  9447. sansSerif: 'sans-serif',
  9448. serif: 'serif',
  9449. monospaceSansSerif: '"Andale Mono", "Lucida Console", monospace',
  9450. monospaceSerif: '"Courier New", monospace',
  9451. proportionalSansSerif: 'sans-serif',
  9452. proportionalSerif: 'serif',
  9453. casual: '"Comic Sans MS", Impact, fantasy',
  9454. script: '"Monotype Corsiva", cursive',
  9455. smallcaps: '"Andale Mono", "Lucida Console", monospace, sans-serif'
  9456. };
  9457. /**
  9458. * Construct an rgba color from a given hex color code.
  9459. *
  9460. * @param {number} color
  9461. * Hex number for color, like #f0e or #f604e2.
  9462. *
  9463. * @param {number} opacity
  9464. * Value for opacity, 0.0 - 1.0.
  9465. *
  9466. * @return {string}
  9467. * The rgba color that was created, like 'rgba(255, 0, 0, 0.3)'.
  9468. */
  9469. function constructColor(color, opacity) {
  9470. let hex;
  9471. if (color.length === 4) {
  9472. // color looks like "#f0e"
  9473. hex = color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
  9474. } else if (color.length === 7) {
  9475. // color looks like "#f604e2"
  9476. hex = color.slice(1);
  9477. } else {
  9478. throw new Error('Invalid color code provided, ' + color + '; must be formatted as e.g. #f0e or #f604e2.');
  9479. }
  9480. return 'rgba(' + parseInt(hex.slice(0, 2), 16) + ',' + parseInt(hex.slice(2, 4), 16) + ',' + parseInt(hex.slice(4, 6), 16) + ',' + opacity + ')';
  9481. }
  9482. /**
  9483. * Try to update the style of a DOM element. Some style changes will throw an error,
  9484. * particularly in IE8. Those should be noops.
  9485. *
  9486. * @param {Element} el
  9487. * The DOM element to be styled.
  9488. *
  9489. * @param {string} style
  9490. * The CSS property on the element that should be styled.
  9491. *
  9492. * @param {string} rule
  9493. * The style rule that should be applied to the property.
  9494. *
  9495. * @private
  9496. */
  9497. function tryUpdateStyle(el, style, rule) {
  9498. try {
  9499. el.style[style] = rule;
  9500. } catch (e) {
  9501. // Satisfies linter.
  9502. return;
  9503. }
  9504. }
  9505. /**
  9506. * Converts the CSS top/right/bottom/left property numeric value to string in pixels.
  9507. *
  9508. * @param {number} position
  9509. * The CSS top/right/bottom/left property value.
  9510. *
  9511. * @return {string}
  9512. * The CSS property value that was created, like '10px'.
  9513. *
  9514. * @private
  9515. */
  9516. function getCSSPositionValue(position) {
  9517. return position ? `${position}px` : '';
  9518. }
  9519. /**
  9520. * The component for displaying text track cues.
  9521. *
  9522. * @extends Component
  9523. */
  9524. class TextTrackDisplay extends Component {
  9525. /**
  9526. * Creates an instance of this class.
  9527. *
  9528. * @param { import('../player').default } player
  9529. * The `Player` that this class should be attached to.
  9530. *
  9531. * @param {Object} [options]
  9532. * The key/value store of player options.
  9533. *
  9534. * @param {Function} [ready]
  9535. * The function to call when `TextTrackDisplay` is ready.
  9536. */
  9537. constructor(player, options, ready) {
  9538. super(player, options, ready);
  9539. const updateDisplayTextHandler = e => this.updateDisplay(e);
  9540. const updateDisplayHandler = e => {
  9541. this.updateDisplayOverlay();
  9542. this.updateDisplay(e);
  9543. };
  9544. player.on('loadstart', e => this.toggleDisplay(e));
  9545. player.on('texttrackchange', updateDisplayTextHandler);
  9546. player.on('loadedmetadata', e => {
  9547. this.updateDisplayOverlay();
  9548. this.preselectTrack(e);
  9549. });
  9550. // This used to be called during player init, but was causing an error
  9551. // if a track should show by default and the display hadn't loaded yet.
  9552. // Should probably be moved to an external track loader when we support
  9553. // tracks that don't need a display.
  9554. player.ready(bind_(this, function () {
  9555. if (player.tech_ && player.tech_.featuresNativeTextTracks) {
  9556. this.hide();
  9557. return;
  9558. }
  9559. player.on('fullscreenchange', updateDisplayHandler);
  9560. player.on('playerresize', updateDisplayHandler);
  9561. const screenOrientation = window.screen.orientation || window;
  9562. const changeOrientationEvent = window.screen.orientation ? 'change' : 'orientationchange';
  9563. screenOrientation.addEventListener(changeOrientationEvent, updateDisplayHandler);
  9564. player.on('dispose', () => screenOrientation.removeEventListener(changeOrientationEvent, updateDisplayHandler));
  9565. const tracks = this.options_.playerOptions.tracks || [];
  9566. for (let i = 0; i < tracks.length; i++) {
  9567. this.player_.addRemoteTextTrack(tracks[i], true);
  9568. }
  9569. this.preselectTrack();
  9570. }));
  9571. }
  9572. /**
  9573. * Preselect a track following this precedence:
  9574. * - matches the previously selected {@link TextTrack}'s language and kind
  9575. * - matches the previously selected {@link TextTrack}'s language only
  9576. * - is the first default captions track
  9577. * - is the first default descriptions track
  9578. *
  9579. * @listens Player#loadstart
  9580. */
  9581. preselectTrack() {
  9582. const modes = {
  9583. captions: 1,
  9584. subtitles: 1
  9585. };
  9586. const trackList = this.player_.textTracks();
  9587. const userPref = this.player_.cache_.selectedLanguage;
  9588. let firstDesc;
  9589. let firstCaptions;
  9590. let preferredTrack;
  9591. for (let i = 0; i < trackList.length; i++) {
  9592. const track = trackList[i];
  9593. if (userPref && userPref.enabled && userPref.language && userPref.language === track.language && track.kind in modes) {
  9594. // Always choose the track that matches both language and kind
  9595. if (track.kind === userPref.kind) {
  9596. preferredTrack = track;
  9597. // or choose the first track that matches language
  9598. } else if (!preferredTrack) {
  9599. preferredTrack = track;
  9600. }
  9601. // clear everything if offTextTrackMenuItem was clicked
  9602. } else if (userPref && !userPref.enabled) {
  9603. preferredTrack = null;
  9604. firstDesc = null;
  9605. firstCaptions = null;
  9606. } else if (track.default) {
  9607. if (track.kind === 'descriptions' && !firstDesc) {
  9608. firstDesc = track;
  9609. } else if (track.kind in modes && !firstCaptions) {
  9610. firstCaptions = track;
  9611. }
  9612. }
  9613. }
  9614. // The preferredTrack matches the user preference and takes
  9615. // precedence over all the other tracks.
  9616. // So, display the preferredTrack before the first default track
  9617. // and the subtitles/captions track before the descriptions track
  9618. if (preferredTrack) {
  9619. preferredTrack.mode = 'showing';
  9620. } else if (firstCaptions) {
  9621. firstCaptions.mode = 'showing';
  9622. } else if (firstDesc) {
  9623. firstDesc.mode = 'showing';
  9624. }
  9625. }
  9626. /**
  9627. * Turn display of {@link TextTrack}'s from the current state into the other state.
  9628. * There are only two states:
  9629. * - 'shown'
  9630. * - 'hidden'
  9631. *
  9632. * @listens Player#loadstart
  9633. */
  9634. toggleDisplay() {
  9635. if (this.player_.tech_ && this.player_.tech_.featuresNativeTextTracks) {
  9636. this.hide();
  9637. } else {
  9638. this.show();
  9639. }
  9640. }
  9641. /**
  9642. * Create the {@link Component}'s DOM element.
  9643. *
  9644. * @return {Element}
  9645. * The element that was created.
  9646. */
  9647. createEl() {
  9648. return super.createEl('div', {
  9649. className: 'vjs-text-track-display'
  9650. }, {
  9651. 'translate': 'yes',
  9652. 'aria-live': 'off',
  9653. 'aria-atomic': 'true'
  9654. });
  9655. }
  9656. /**
  9657. * Clear all displayed {@link TextTrack}s.
  9658. */
  9659. clearDisplay() {
  9660. if (typeof window.WebVTT === 'function') {
  9661. window.WebVTT.processCues(window, [], this.el_);
  9662. }
  9663. }
  9664. /**
  9665. * Update the displayed TextTrack when a either a {@link Player#texttrackchange} or
  9666. * a {@link Player#fullscreenchange} is fired.
  9667. *
  9668. * @listens Player#texttrackchange
  9669. * @listens Player#fullscreenchange
  9670. */
  9671. updateDisplay() {
  9672. const tracks = this.player_.textTracks();
  9673. const allowMultipleShowingTracks = this.options_.allowMultipleShowingTracks;
  9674. this.clearDisplay();
  9675. if (allowMultipleShowingTracks) {
  9676. const showingTracks = [];
  9677. for (let i = 0; i < tracks.length; ++i) {
  9678. const track = tracks[i];
  9679. if (track.mode !== 'showing') {
  9680. continue;
  9681. }
  9682. showingTracks.push(track);
  9683. }
  9684. this.updateForTrack(showingTracks);
  9685. return;
  9686. }
  9687. // Track display prioritization model: if multiple tracks are 'showing',
  9688. // display the first 'subtitles' or 'captions' track which is 'showing',
  9689. // otherwise display the first 'descriptions' track which is 'showing'
  9690. let descriptionsTrack = null;
  9691. let captionsSubtitlesTrack = null;
  9692. let i = tracks.length;
  9693. while (i--) {
  9694. const track = tracks[i];
  9695. if (track.mode === 'showing') {
  9696. if (track.kind === 'descriptions') {
  9697. descriptionsTrack = track;
  9698. } else {
  9699. captionsSubtitlesTrack = track;
  9700. }
  9701. }
  9702. }
  9703. if (captionsSubtitlesTrack) {
  9704. if (this.getAttribute('aria-live') !== 'off') {
  9705. this.setAttribute('aria-live', 'off');
  9706. }
  9707. this.updateForTrack(captionsSubtitlesTrack);
  9708. } else if (descriptionsTrack) {
  9709. if (this.getAttribute('aria-live') !== 'assertive') {
  9710. this.setAttribute('aria-live', 'assertive');
  9711. }
  9712. this.updateForTrack(descriptionsTrack);
  9713. }
  9714. }
  9715. /**
  9716. * Updates the displayed TextTrack to be sure it overlays the video when a either
  9717. * a {@link Player#texttrackchange} or a {@link Player#fullscreenchange} is fired.
  9718. */
  9719. updateDisplayOverlay() {
  9720. // inset-inline and inset-block are not supprted on old chrome, but these are
  9721. // only likely to be used on TV devices
  9722. if (!this.player_.videoHeight() || !window.CSS.supports('inset-inline: 10px')) {
  9723. return;
  9724. }
  9725. const playerWidth = this.player_.currentWidth();
  9726. const playerHeight = this.player_.currentHeight();
  9727. const playerAspectRatio = playerWidth / playerHeight;
  9728. const videoAspectRatio = this.player_.videoWidth() / this.player_.videoHeight();
  9729. let insetInlineMatch = 0;
  9730. let insetBlockMatch = 0;
  9731. if (Math.abs(playerAspectRatio - videoAspectRatio) > 0.1) {
  9732. if (playerAspectRatio > videoAspectRatio) {
  9733. insetInlineMatch = Math.round((playerWidth - playerHeight * videoAspectRatio) / 2);
  9734. } else {
  9735. insetBlockMatch = Math.round((playerHeight - playerWidth / videoAspectRatio) / 2);
  9736. }
  9737. }
  9738. tryUpdateStyle(this.el_, 'insetInline', getCSSPositionValue(insetInlineMatch));
  9739. tryUpdateStyle(this.el_, 'insetBlock', getCSSPositionValue(insetBlockMatch));
  9740. }
  9741. /**
  9742. * Style {@Link TextTrack} activeCues according to {@Link TextTrackSettings}.
  9743. *
  9744. * @param {TextTrack} track
  9745. * Text track object containing active cues to style.
  9746. */
  9747. updateDisplayState(track) {
  9748. const overrides = this.player_.textTrackSettings.getValues();
  9749. const cues = track.activeCues;
  9750. let i = cues.length;
  9751. while (i--) {
  9752. const cue = cues[i];
  9753. if (!cue) {
  9754. continue;
  9755. }
  9756. const cueDiv = cue.displayState;
  9757. if (overrides.color) {
  9758. cueDiv.firstChild.style.color = overrides.color;
  9759. }
  9760. if (overrides.textOpacity) {
  9761. tryUpdateStyle(cueDiv.firstChild, 'color', constructColor(overrides.color || '#fff', overrides.textOpacity));
  9762. }
  9763. if (overrides.backgroundColor) {
  9764. cueDiv.firstChild.style.backgroundColor = overrides.backgroundColor;
  9765. }
  9766. if (overrides.backgroundOpacity) {
  9767. tryUpdateStyle(cueDiv.firstChild, 'backgroundColor', constructColor(overrides.backgroundColor || '#000', overrides.backgroundOpacity));
  9768. }
  9769. if (overrides.windowColor) {
  9770. if (overrides.windowOpacity) {
  9771. tryUpdateStyle(cueDiv, 'backgroundColor', constructColor(overrides.windowColor, overrides.windowOpacity));
  9772. } else {
  9773. cueDiv.style.backgroundColor = overrides.windowColor;
  9774. }
  9775. }
  9776. if (overrides.edgeStyle) {
  9777. if (overrides.edgeStyle === 'dropshadow') {
  9778. cueDiv.firstChild.style.textShadow = `2px 2px 3px ${darkGray}, 2px 2px 4px ${darkGray}, 2px 2px 5px ${darkGray}`;
  9779. } else if (overrides.edgeStyle === 'raised') {
  9780. cueDiv.firstChild.style.textShadow = `1px 1px ${darkGray}, 2px 2px ${darkGray}, 3px 3px ${darkGray}`;
  9781. } else if (overrides.edgeStyle === 'depressed') {
  9782. cueDiv.firstChild.style.textShadow = `1px 1px ${lightGray}, 0 1px ${lightGray}, -1px -1px ${darkGray}, 0 -1px ${darkGray}`;
  9783. } else if (overrides.edgeStyle === 'uniform') {
  9784. cueDiv.firstChild.style.textShadow = `0 0 4px ${darkGray}, 0 0 4px ${darkGray}, 0 0 4px ${darkGray}, 0 0 4px ${darkGray}`;
  9785. }
  9786. }
  9787. if (overrides.fontPercent && overrides.fontPercent !== 1) {
  9788. const fontSize = window.parseFloat(cueDiv.style.fontSize);
  9789. cueDiv.style.fontSize = fontSize * overrides.fontPercent + 'px';
  9790. cueDiv.style.height = 'auto';
  9791. cueDiv.style.top = 'auto';
  9792. }
  9793. if (overrides.fontFamily && overrides.fontFamily !== 'default') {
  9794. if (overrides.fontFamily === 'small-caps') {
  9795. cueDiv.firstChild.style.fontVariant = 'small-caps';
  9796. } else {
  9797. cueDiv.firstChild.style.fontFamily = fontMap[overrides.fontFamily];
  9798. }
  9799. }
  9800. }
  9801. }
  9802. /**
  9803. * Add an {@link TextTrack} to to the {@link Tech}s {@link TextTrackList}.
  9804. *
  9805. * @param {TextTrack|TextTrack[]} tracks
  9806. * Text track object or text track array to be added to the list.
  9807. */
  9808. updateForTrack(tracks) {
  9809. if (!Array.isArray(tracks)) {
  9810. tracks = [tracks];
  9811. }
  9812. if (typeof window.WebVTT !== 'function' || tracks.every(track => {
  9813. return !track.activeCues;
  9814. })) {
  9815. return;
  9816. }
  9817. const cues = [];
  9818. // push all active track cues
  9819. for (let i = 0; i < tracks.length; ++i) {
  9820. const track = tracks[i];
  9821. for (let j = 0; j < track.activeCues.length; ++j) {
  9822. cues.push(track.activeCues[j]);
  9823. }
  9824. }
  9825. // removes all cues before it processes new ones
  9826. window.WebVTT.processCues(window, cues, this.el_);
  9827. // add unique class to each language text track & add settings styling if necessary
  9828. for (let i = 0; i < tracks.length; ++i) {
  9829. const track = tracks[i];
  9830. for (let j = 0; j < track.activeCues.length; ++j) {
  9831. const cueEl = track.activeCues[j].displayState;
  9832. addClass(cueEl, 'vjs-text-track-cue', 'vjs-text-track-cue-' + (track.language ? track.language : i));
  9833. if (track.language) {
  9834. setAttribute(cueEl, 'lang', track.language);
  9835. }
  9836. }
  9837. if (this.player_.textTrackSettings) {
  9838. this.updateDisplayState(track);
  9839. }
  9840. }
  9841. }
  9842. }
  9843. Component.registerComponent('TextTrackDisplay', TextTrackDisplay);
  9844. /**
  9845. * @file loading-spinner.js
  9846. */
  9847. /**
  9848. * A loading spinner for use during waiting/loading events.
  9849. *
  9850. * @extends Component
  9851. */
  9852. class LoadingSpinner extends Component {
  9853. /**
  9854. * Create the `LoadingSpinner`s DOM element.
  9855. *
  9856. * @return {Element}
  9857. * The dom element that gets created.
  9858. */
  9859. createEl() {
  9860. const isAudio = this.player_.isAudio();
  9861. const playerType = this.localize(isAudio ? 'Audio Player' : 'Video Player');
  9862. const controlText = createEl('span', {
  9863. className: 'vjs-control-text',
  9864. textContent: this.localize('{1} is loading.', [playerType])
  9865. });
  9866. const el = super.createEl('div', {
  9867. className: 'vjs-loading-spinner',
  9868. dir: 'ltr'
  9869. });
  9870. el.appendChild(controlText);
  9871. return el;
  9872. }
  9873. /**
  9874. * Update control text on languagechange
  9875. */
  9876. handleLanguagechange() {
  9877. this.$('.vjs-control-text').textContent = this.localize('{1} is loading.', [this.player_.isAudio() ? 'Audio Player' : 'Video Player']);
  9878. }
  9879. }
  9880. Component.registerComponent('LoadingSpinner', LoadingSpinner);
  9881. /**
  9882. * @file button.js
  9883. */
  9884. /**
  9885. * Base class for all buttons.
  9886. *
  9887. * @extends ClickableComponent
  9888. */
  9889. class Button extends ClickableComponent {
  9890. /**
  9891. * Create the `Button`s DOM element.
  9892. *
  9893. * @param {string} [tag="button"]
  9894. * The element's node type. This argument is IGNORED: no matter what
  9895. * is passed, it will always create a `button` element.
  9896. *
  9897. * @param {Object} [props={}]
  9898. * An object of properties that should be set on the element.
  9899. *
  9900. * @param {Object} [attributes={}]
  9901. * An object of attributes that should be set on the element.
  9902. *
  9903. * @return {Element}
  9904. * The element that gets created.
  9905. */
  9906. createEl(tag, props = {}, attributes = {}) {
  9907. tag = 'button';
  9908. props = Object.assign({
  9909. className: this.buildCSSClass()
  9910. }, props);
  9911. // Add attributes for button element
  9912. attributes = Object.assign({
  9913. // Necessary since the default button type is "submit"
  9914. type: 'button'
  9915. }, attributes);
  9916. const el = createEl(tag, props, attributes);
  9917. if (!this.player_.options_.experimentalSvgIcons) {
  9918. el.appendChild(createEl('span', {
  9919. className: 'vjs-icon-placeholder'
  9920. }, {
  9921. 'aria-hidden': true
  9922. }));
  9923. }
  9924. this.createControlTextEl(el);
  9925. return el;
  9926. }
  9927. /**
  9928. * Add a child `Component` inside of this `Button`.
  9929. *
  9930. * @param {string|Component} child
  9931. * The name or instance of a child to add.
  9932. *
  9933. * @param {Object} [options={}]
  9934. * The key/value store of options that will get passed to children of
  9935. * the child.
  9936. *
  9937. * @return {Component}
  9938. * The `Component` that gets added as a child. When using a string the
  9939. * `Component` will get created by this process.
  9940. *
  9941. * @deprecated since version 5
  9942. */
  9943. addChild(child, options = {}) {
  9944. const className = this.constructor.name;
  9945. log.warn(`Adding an actionable (user controllable) child to a Button (${className}) is not supported; use a ClickableComponent instead.`);
  9946. // Avoid the error message generated by ClickableComponent's addChild method
  9947. return Component.prototype.addChild.call(this, child, options);
  9948. }
  9949. /**
  9950. * Enable the `Button` element so that it can be activated or clicked. Use this with
  9951. * {@link Button#disable}.
  9952. */
  9953. enable() {
  9954. super.enable();
  9955. this.el_.removeAttribute('disabled');
  9956. }
  9957. /**
  9958. * Disable the `Button` element so that it cannot be activated or clicked. Use this with
  9959. * {@link Button#enable}.
  9960. */
  9961. disable() {
  9962. super.disable();
  9963. this.el_.setAttribute('disabled', 'disabled');
  9964. }
  9965. /**
  9966. * This gets called when a `Button` has focus and `keydown` is triggered via a key
  9967. * press.
  9968. *
  9969. * @param {KeyboardEvent} event
  9970. * The event that caused this function to get called.
  9971. *
  9972. * @listens keydown
  9973. */
  9974. handleKeyDown(event) {
  9975. // Ignore Space or Enter key operation, which is handled by the browser for
  9976. // a button - though not for its super class, ClickableComponent. Also,
  9977. // prevent the event from propagating through the DOM and triggering Player
  9978. // hotkeys. We do not preventDefault here because we _want_ the browser to
  9979. // handle it.
  9980. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  9981. event.stopPropagation();
  9982. return;
  9983. }
  9984. // Pass keypress handling up for unsupported keys
  9985. super.handleKeyDown(event);
  9986. }
  9987. }
  9988. Component.registerComponent('Button', Button);
  9989. /**
  9990. * @file big-play-button.js
  9991. */
  9992. /**
  9993. * The initial play button that shows before the video has played. The hiding of the
  9994. * `BigPlayButton` get done via CSS and `Player` states.
  9995. *
  9996. * @extends Button
  9997. */
  9998. class BigPlayButton extends Button {
  9999. constructor(player, options) {
  10000. super(player, options);
  10001. this.mouseused_ = false;
  10002. this.setIcon('play');
  10003. this.on('mousedown', e => this.handleMouseDown(e));
  10004. }
  10005. /**
  10006. * Builds the default DOM `className`.
  10007. *
  10008. * @return {string}
  10009. * The DOM `className` for this object. Always returns 'vjs-big-play-button'.
  10010. */
  10011. buildCSSClass() {
  10012. return 'vjs-big-play-button';
  10013. }
  10014. /**
  10015. * This gets called when a `BigPlayButton` "clicked". See {@link ClickableComponent}
  10016. * for more detailed information on what a click can be.
  10017. *
  10018. * @param {KeyboardEvent|MouseEvent|TouchEvent} event
  10019. * The `keydown`, `tap`, or `click` event that caused this function to be
  10020. * called.
  10021. *
  10022. * @listens tap
  10023. * @listens click
  10024. */
  10025. handleClick(event) {
  10026. const playPromise = this.player_.play();
  10027. // exit early if clicked via the mouse
  10028. if (this.mouseused_ && 'clientX' in event && 'clientY' in event) {
  10029. silencePromise(playPromise);
  10030. if (this.player_.tech(true)) {
  10031. this.player_.tech(true).focus();
  10032. }
  10033. return;
  10034. }
  10035. const cb = this.player_.getChild('controlBar');
  10036. const playToggle = cb && cb.getChild('playToggle');
  10037. if (!playToggle) {
  10038. this.player_.tech(true).focus();
  10039. return;
  10040. }
  10041. const playFocus = () => playToggle.focus();
  10042. if (isPromise(playPromise)) {
  10043. playPromise.then(playFocus, () => {});
  10044. } else {
  10045. this.setTimeout(playFocus, 1);
  10046. }
  10047. }
  10048. /**
  10049. * Event handler that is called when a `BigPlayButton` receives a
  10050. * `keydown` event.
  10051. *
  10052. * @param {KeyboardEvent} event
  10053. * The `keydown` event that caused this function to be called.
  10054. *
  10055. * @listens keydown
  10056. */
  10057. handleKeyDown(event) {
  10058. this.mouseused_ = false;
  10059. super.handleKeyDown(event);
  10060. }
  10061. /**
  10062. * Handle `mousedown` events on the `BigPlayButton`.
  10063. *
  10064. * @param {MouseEvent} event
  10065. * `mousedown` or `touchstart` event that triggered this function
  10066. *
  10067. * @listens mousedown
  10068. */
  10069. handleMouseDown(event) {
  10070. this.mouseused_ = true;
  10071. }
  10072. }
  10073. /**
  10074. * The text that should display over the `BigPlayButton`s controls. Added to for localization.
  10075. *
  10076. * @type {string}
  10077. * @protected
  10078. */
  10079. BigPlayButton.prototype.controlText_ = 'Play Video';
  10080. Component.registerComponent('BigPlayButton', BigPlayButton);
  10081. /**
  10082. * @file close-button.js
  10083. */
  10084. /**
  10085. * The `CloseButton` is a `{@link Button}` that fires a `close` event when
  10086. * it gets clicked.
  10087. *
  10088. * @extends Button
  10089. */
  10090. class CloseButton extends Button {
  10091. /**
  10092. * Creates an instance of the this class.
  10093. *
  10094. * @param { import('./player').default } player
  10095. * The `Player` that this class should be attached to.
  10096. *
  10097. * @param {Object} [options]
  10098. * The key/value store of player options.
  10099. */
  10100. constructor(player, options) {
  10101. super(player, options);
  10102. this.setIcon('cancel');
  10103. this.controlText(options && options.controlText || this.localize('Close'));
  10104. }
  10105. /**
  10106. * Builds the default DOM `className`.
  10107. *
  10108. * @return {string}
  10109. * The DOM `className` for this object.
  10110. */
  10111. buildCSSClass() {
  10112. return `vjs-close-button ${super.buildCSSClass()}`;
  10113. }
  10114. /**
  10115. * This gets called when a `CloseButton` gets clicked. See
  10116. * {@link ClickableComponent#handleClick} for more information on when
  10117. * this will be triggered
  10118. *
  10119. * @param {Event} event
  10120. * The `keydown`, `tap`, or `click` event that caused this function to be
  10121. * called.
  10122. *
  10123. * @listens tap
  10124. * @listens click
  10125. * @fires CloseButton#close
  10126. */
  10127. handleClick(event) {
  10128. /**
  10129. * Triggered when the a `CloseButton` is clicked.
  10130. *
  10131. * @event CloseButton#close
  10132. * @type {Event}
  10133. *
  10134. * @property {boolean} [bubbles=false]
  10135. * set to false so that the close event does not
  10136. * bubble up to parents if there is no listener
  10137. */
  10138. this.trigger({
  10139. type: 'close',
  10140. bubbles: false
  10141. });
  10142. }
  10143. /**
  10144. * Event handler that is called when a `CloseButton` receives a
  10145. * `keydown` event.
  10146. *
  10147. * By default, if the key is Esc, it will trigger a `click` event.
  10148. *
  10149. * @param {KeyboardEvent} event
  10150. * The `keydown` event that caused this function to be called.
  10151. *
  10152. * @listens keydown
  10153. */
  10154. handleKeyDown(event) {
  10155. // Esc button will trigger `click` event
  10156. if (keycode.isEventKey(event, 'Esc')) {
  10157. event.preventDefault();
  10158. event.stopPropagation();
  10159. this.trigger('click');
  10160. } else {
  10161. // Pass keypress handling up for unsupported keys
  10162. super.handleKeyDown(event);
  10163. }
  10164. }
  10165. }
  10166. Component.registerComponent('CloseButton', CloseButton);
  10167. /**
  10168. * @file play-toggle.js
  10169. */
  10170. /**
  10171. * Button to toggle between play and pause.
  10172. *
  10173. * @extends Button
  10174. */
  10175. class PlayToggle extends Button {
  10176. /**
  10177. * Creates an instance of this class.
  10178. *
  10179. * @param { import('./player').default } player
  10180. * The `Player` that this class should be attached to.
  10181. *
  10182. * @param {Object} [options={}]
  10183. * The key/value store of player options.
  10184. */
  10185. constructor(player, options = {}) {
  10186. super(player, options);
  10187. // show or hide replay icon
  10188. options.replay = options.replay === undefined || options.replay;
  10189. this.setIcon('play');
  10190. this.on(player, 'play', e => this.handlePlay(e));
  10191. this.on(player, 'pause', e => this.handlePause(e));
  10192. if (options.replay) {
  10193. this.on(player, 'ended', e => this.handleEnded(e));
  10194. }
  10195. }
  10196. /**
  10197. * Builds the default DOM `className`.
  10198. *
  10199. * @return {string}
  10200. * The DOM `className` for this object.
  10201. */
  10202. buildCSSClass() {
  10203. return `vjs-play-control ${super.buildCSSClass()}`;
  10204. }
  10205. /**
  10206. * This gets called when an `PlayToggle` is "clicked". See
  10207. * {@link ClickableComponent} for more detailed information on what a click can be.
  10208. *
  10209. * @param {Event} [event]
  10210. * The `keydown`, `tap`, or `click` event that caused this function to be
  10211. * called.
  10212. *
  10213. * @listens tap
  10214. * @listens click
  10215. */
  10216. handleClick(event) {
  10217. if (this.player_.paused()) {
  10218. silencePromise(this.player_.play());
  10219. } else {
  10220. this.player_.pause();
  10221. }
  10222. }
  10223. /**
  10224. * This gets called once after the video has ended and the user seeks so that
  10225. * we can change the replay button back to a play button.
  10226. *
  10227. * @param {Event} [event]
  10228. * The event that caused this function to run.
  10229. *
  10230. * @listens Player#seeked
  10231. */
  10232. handleSeeked(event) {
  10233. this.removeClass('vjs-ended');
  10234. if (this.player_.paused()) {
  10235. this.handlePause(event);
  10236. } else {
  10237. this.handlePlay(event);
  10238. }
  10239. }
  10240. /**
  10241. * Add the vjs-playing class to the element so it can change appearance.
  10242. *
  10243. * @param {Event} [event]
  10244. * The event that caused this function to run.
  10245. *
  10246. * @listens Player#play
  10247. */
  10248. handlePlay(event) {
  10249. this.removeClass('vjs-ended', 'vjs-paused');
  10250. this.addClass('vjs-playing');
  10251. // change the button text to "Pause"
  10252. this.setIcon('pause');
  10253. this.controlText('Pause');
  10254. }
  10255. /**
  10256. * Add the vjs-paused class to the element so it can change appearance.
  10257. *
  10258. * @param {Event} [event]
  10259. * The event that caused this function to run.
  10260. *
  10261. * @listens Player#pause
  10262. */
  10263. handlePause(event) {
  10264. this.removeClass('vjs-playing');
  10265. this.addClass('vjs-paused');
  10266. // change the button text to "Play"
  10267. this.setIcon('play');
  10268. this.controlText('Play');
  10269. }
  10270. /**
  10271. * Add the vjs-ended class to the element so it can change appearance
  10272. *
  10273. * @param {Event} [event]
  10274. * The event that caused this function to run.
  10275. *
  10276. * @listens Player#ended
  10277. */
  10278. handleEnded(event) {
  10279. this.removeClass('vjs-playing');
  10280. this.addClass('vjs-ended');
  10281. // change the button text to "Replay"
  10282. this.setIcon('replay');
  10283. this.controlText('Replay');
  10284. // on the next seek remove the replay button
  10285. this.one(this.player_, 'seeked', e => this.handleSeeked(e));
  10286. }
  10287. }
  10288. /**
  10289. * The text that should display over the `PlayToggle`s controls. Added for localization.
  10290. *
  10291. * @type {string}
  10292. * @protected
  10293. */
  10294. PlayToggle.prototype.controlText_ = 'Play';
  10295. Component.registerComponent('PlayToggle', PlayToggle);
  10296. /**
  10297. * @file time-display.js
  10298. */
  10299. /**
  10300. * Displays time information about the video
  10301. *
  10302. * @extends Component
  10303. */
  10304. class TimeDisplay extends Component {
  10305. /**
  10306. * Creates an instance of this class.
  10307. *
  10308. * @param { import('../../player').default } player
  10309. * The `Player` that this class should be attached to.
  10310. *
  10311. * @param {Object} [options]
  10312. * The key/value store of player options.
  10313. */
  10314. constructor(player, options) {
  10315. super(player, options);
  10316. this.on(player, ['timeupdate', 'ended', 'seeking'], e => this.update(e));
  10317. this.updateTextNode_();
  10318. }
  10319. /**
  10320. * Create the `Component`'s DOM element
  10321. *
  10322. * @return {Element}
  10323. * The element that was created.
  10324. */
  10325. createEl() {
  10326. const className = this.buildCSSClass();
  10327. const el = super.createEl('div', {
  10328. className: `${className} vjs-time-control vjs-control`
  10329. });
  10330. const span = createEl('span', {
  10331. className: 'vjs-control-text',
  10332. textContent: `${this.localize(this.labelText_)}\u00a0`
  10333. }, {
  10334. role: 'presentation'
  10335. });
  10336. el.appendChild(span);
  10337. this.contentEl_ = createEl('span', {
  10338. className: `${className}-display`
  10339. }, {
  10340. // span elements have no implicit role, but some screen readers (notably VoiceOver)
  10341. // treat them as a break between items in the DOM when using arrow keys
  10342. // (or left-to-right swipes on iOS) to read contents of a page. Using
  10343. // role='presentation' causes VoiceOver to NOT treat this span as a break.
  10344. role: 'presentation'
  10345. });
  10346. el.appendChild(this.contentEl_);
  10347. return el;
  10348. }
  10349. dispose() {
  10350. this.contentEl_ = null;
  10351. this.textNode_ = null;
  10352. super.dispose();
  10353. }
  10354. /**
  10355. * Updates the displayed time according to the `updateContent` function which is defined in the child class.
  10356. *
  10357. * @param {Event} [event]
  10358. * The `timeupdate`, `ended` or `seeking` (if enableSmoothSeeking is true) event that caused this function to be called.
  10359. */
  10360. update(event) {
  10361. if (!this.player_.options_.enableSmoothSeeking && event.type === 'seeking') {
  10362. return;
  10363. }
  10364. this.updateContent(event);
  10365. }
  10366. /**
  10367. * Updates the time display text node with a new time
  10368. *
  10369. * @param {number} [time=0] the time to update to
  10370. *
  10371. * @private
  10372. */
  10373. updateTextNode_(time = 0) {
  10374. time = formatTime(time);
  10375. if (this.formattedTime_ === time) {
  10376. return;
  10377. }
  10378. this.formattedTime_ = time;
  10379. this.requestNamedAnimationFrame('TimeDisplay#updateTextNode_', () => {
  10380. if (!this.contentEl_) {
  10381. return;
  10382. }
  10383. let oldNode = this.textNode_;
  10384. if (oldNode && this.contentEl_.firstChild !== oldNode) {
  10385. oldNode = null;
  10386. log.warn('TimeDisplay#updateTextnode_: Prevented replacement of text node element since it was no longer a child of this node. Appending a new node instead.');
  10387. }
  10388. this.textNode_ = document.createTextNode(this.formattedTime_);
  10389. if (!this.textNode_) {
  10390. return;
  10391. }
  10392. if (oldNode) {
  10393. this.contentEl_.replaceChild(this.textNode_, oldNode);
  10394. } else {
  10395. this.contentEl_.appendChild(this.textNode_);
  10396. }
  10397. });
  10398. }
  10399. /**
  10400. * To be filled out in the child class, should update the displayed time
  10401. * in accordance with the fact that the current time has changed.
  10402. *
  10403. * @param {Event} [event]
  10404. * The `timeupdate` event that caused this to run.
  10405. *
  10406. * @listens Player#timeupdate
  10407. */
  10408. updateContent(event) {}
  10409. }
  10410. /**
  10411. * The text that is added to the `TimeDisplay` for screen reader users.
  10412. *
  10413. * @type {string}
  10414. * @private
  10415. */
  10416. TimeDisplay.prototype.labelText_ = 'Time';
  10417. /**
  10418. * The text that should display over the `TimeDisplay`s controls. Added to for localization.
  10419. *
  10420. * @type {string}
  10421. * @protected
  10422. *
  10423. * @deprecated in v7; controlText_ is not used in non-active display Components
  10424. */
  10425. TimeDisplay.prototype.controlText_ = 'Time';
  10426. Component.registerComponent('TimeDisplay', TimeDisplay);
  10427. /**
  10428. * @file current-time-display.js
  10429. */
  10430. /**
  10431. * Displays the current time
  10432. *
  10433. * @extends Component
  10434. */
  10435. class CurrentTimeDisplay extends TimeDisplay {
  10436. /**
  10437. * Builds the default DOM `className`.
  10438. *
  10439. * @return {string}
  10440. * The DOM `className` for this object.
  10441. */
  10442. buildCSSClass() {
  10443. return 'vjs-current-time';
  10444. }
  10445. /**
  10446. * Update current time display
  10447. *
  10448. * @param {Event} [event]
  10449. * The `timeupdate` event that caused this function to run.
  10450. *
  10451. * @listens Player#timeupdate
  10452. */
  10453. updateContent(event) {
  10454. // Allows for smooth scrubbing, when player can't keep up.
  10455. let time;
  10456. if (this.player_.ended()) {
  10457. time = this.player_.duration();
  10458. } else {
  10459. time = this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  10460. }
  10461. this.updateTextNode_(time);
  10462. }
  10463. }
  10464. /**
  10465. * The text that is added to the `CurrentTimeDisplay` for screen reader users.
  10466. *
  10467. * @type {string}
  10468. * @private
  10469. */
  10470. CurrentTimeDisplay.prototype.labelText_ = 'Current Time';
  10471. /**
  10472. * The text that should display over the `CurrentTimeDisplay`s controls. Added to for localization.
  10473. *
  10474. * @type {string}
  10475. * @protected
  10476. *
  10477. * @deprecated in v7; controlText_ is not used in non-active display Components
  10478. */
  10479. CurrentTimeDisplay.prototype.controlText_ = 'Current Time';
  10480. Component.registerComponent('CurrentTimeDisplay', CurrentTimeDisplay);
  10481. /**
  10482. * @file duration-display.js
  10483. */
  10484. /**
  10485. * Displays the duration
  10486. *
  10487. * @extends Component
  10488. */
  10489. class DurationDisplay extends TimeDisplay {
  10490. /**
  10491. * Creates an instance of this class.
  10492. *
  10493. * @param { import('../../player').default } player
  10494. * The `Player` that this class should be attached to.
  10495. *
  10496. * @param {Object} [options]
  10497. * The key/value store of player options.
  10498. */
  10499. constructor(player, options) {
  10500. super(player, options);
  10501. const updateContent = e => this.updateContent(e);
  10502. // we do not want to/need to throttle duration changes,
  10503. // as they should always display the changed duration as
  10504. // it has changed
  10505. this.on(player, 'durationchange', updateContent);
  10506. // Listen to loadstart because the player duration is reset when a new media element is loaded,
  10507. // but the durationchange on the user agent will not fire.
  10508. // @see [Spec]{@link https://www.w3.org/TR/2011/WD-html5-20110113/video.html#media-element-load-algorithm}
  10509. this.on(player, 'loadstart', updateContent);
  10510. // Also listen for timeupdate (in the parent) and loadedmetadata because removing those
  10511. // listeners could have broken dependent applications/libraries. These
  10512. // can likely be removed for 7.0.
  10513. this.on(player, 'loadedmetadata', updateContent);
  10514. }
  10515. /**
  10516. * Builds the default DOM `className`.
  10517. *
  10518. * @return {string}
  10519. * The DOM `className` for this object.
  10520. */
  10521. buildCSSClass() {
  10522. return 'vjs-duration';
  10523. }
  10524. /**
  10525. * Update duration time display.
  10526. *
  10527. * @param {Event} [event]
  10528. * The `durationchange`, `timeupdate`, or `loadedmetadata` event that caused
  10529. * this function to be called.
  10530. *
  10531. * @listens Player#durationchange
  10532. * @listens Player#timeupdate
  10533. * @listens Player#loadedmetadata
  10534. */
  10535. updateContent(event) {
  10536. const duration = this.player_.duration();
  10537. this.updateTextNode_(duration);
  10538. }
  10539. }
  10540. /**
  10541. * The text that is added to the `DurationDisplay` for screen reader users.
  10542. *
  10543. * @type {string}
  10544. * @private
  10545. */
  10546. DurationDisplay.prototype.labelText_ = 'Duration';
  10547. /**
  10548. * The text that should display over the `DurationDisplay`s controls. Added to for localization.
  10549. *
  10550. * @type {string}
  10551. * @protected
  10552. *
  10553. * @deprecated in v7; controlText_ is not used in non-active display Components
  10554. */
  10555. DurationDisplay.prototype.controlText_ = 'Duration';
  10556. Component.registerComponent('DurationDisplay', DurationDisplay);
  10557. /**
  10558. * @file time-divider.js
  10559. */
  10560. /**
  10561. * The separator between the current time and duration.
  10562. * Can be hidden if it's not needed in the design.
  10563. *
  10564. * @extends Component
  10565. */
  10566. class TimeDivider extends Component {
  10567. /**
  10568. * Create the component's DOM element
  10569. *
  10570. * @return {Element}
  10571. * The element that was created.
  10572. */
  10573. createEl() {
  10574. const el = super.createEl('div', {
  10575. className: 'vjs-time-control vjs-time-divider'
  10576. }, {
  10577. // this element and its contents can be hidden from assistive techs since
  10578. // it is made extraneous by the announcement of the control text
  10579. // for the current time and duration displays
  10580. 'aria-hidden': true
  10581. });
  10582. const div = super.createEl('div');
  10583. const span = super.createEl('span', {
  10584. textContent: '/'
  10585. });
  10586. div.appendChild(span);
  10587. el.appendChild(div);
  10588. return el;
  10589. }
  10590. }
  10591. Component.registerComponent('TimeDivider', TimeDivider);
  10592. /**
  10593. * @file remaining-time-display.js
  10594. */
  10595. /**
  10596. * Displays the time left in the video
  10597. *
  10598. * @extends Component
  10599. */
  10600. class RemainingTimeDisplay extends TimeDisplay {
  10601. /**
  10602. * Creates an instance of this class.
  10603. *
  10604. * @param { import('../../player').default } player
  10605. * The `Player` that this class should be attached to.
  10606. *
  10607. * @param {Object} [options]
  10608. * The key/value store of player options.
  10609. */
  10610. constructor(player, options) {
  10611. super(player, options);
  10612. this.on(player, 'durationchange', e => this.updateContent(e));
  10613. }
  10614. /**
  10615. * Builds the default DOM `className`.
  10616. *
  10617. * @return {string}
  10618. * The DOM `className` for this object.
  10619. */
  10620. buildCSSClass() {
  10621. return 'vjs-remaining-time';
  10622. }
  10623. /**
  10624. * Create the `Component`'s DOM element with the "minus" character prepend to the time
  10625. *
  10626. * @return {Element}
  10627. * The element that was created.
  10628. */
  10629. createEl() {
  10630. const el = super.createEl();
  10631. if (this.options_.displayNegative !== false) {
  10632. el.insertBefore(createEl('span', {}, {
  10633. 'aria-hidden': true
  10634. }, '-'), this.contentEl_);
  10635. }
  10636. return el;
  10637. }
  10638. /**
  10639. * Update remaining time display.
  10640. *
  10641. * @param {Event} [event]
  10642. * The `timeupdate` or `durationchange` event that caused this to run.
  10643. *
  10644. * @listens Player#timeupdate
  10645. * @listens Player#durationchange
  10646. */
  10647. updateContent(event) {
  10648. if (typeof this.player_.duration() !== 'number') {
  10649. return;
  10650. }
  10651. let time;
  10652. // @deprecated We should only use remainingTimeDisplay
  10653. // as of video.js 7
  10654. if (this.player_.ended()) {
  10655. time = 0;
  10656. } else if (this.player_.remainingTimeDisplay) {
  10657. time = this.player_.remainingTimeDisplay();
  10658. } else {
  10659. time = this.player_.remainingTime();
  10660. }
  10661. this.updateTextNode_(time);
  10662. }
  10663. }
  10664. /**
  10665. * The text that is added to the `RemainingTimeDisplay` for screen reader users.
  10666. *
  10667. * @type {string}
  10668. * @private
  10669. */
  10670. RemainingTimeDisplay.prototype.labelText_ = 'Remaining Time';
  10671. /**
  10672. * The text that should display over the `RemainingTimeDisplay`s controls. Added to for localization.
  10673. *
  10674. * @type {string}
  10675. * @protected
  10676. *
  10677. * @deprecated in v7; controlText_ is not used in non-active display Components
  10678. */
  10679. RemainingTimeDisplay.prototype.controlText_ = 'Remaining Time';
  10680. Component.registerComponent('RemainingTimeDisplay', RemainingTimeDisplay);
  10681. /**
  10682. * @file live-display.js
  10683. */
  10684. // TODO - Future make it click to snap to live
  10685. /**
  10686. * Displays the live indicator when duration is Infinity.
  10687. *
  10688. * @extends Component
  10689. */
  10690. class LiveDisplay extends Component {
  10691. /**
  10692. * Creates an instance of this class.
  10693. *
  10694. * @param { import('./player').default } player
  10695. * The `Player` that this class should be attached to.
  10696. *
  10697. * @param {Object} [options]
  10698. * The key/value store of player options.
  10699. */
  10700. constructor(player, options) {
  10701. super(player, options);
  10702. this.updateShowing();
  10703. this.on(this.player(), 'durationchange', e => this.updateShowing(e));
  10704. }
  10705. /**
  10706. * Create the `Component`'s DOM element
  10707. *
  10708. * @return {Element}
  10709. * The element that was created.
  10710. */
  10711. createEl() {
  10712. const el = super.createEl('div', {
  10713. className: 'vjs-live-control vjs-control'
  10714. });
  10715. this.contentEl_ = createEl('div', {
  10716. className: 'vjs-live-display'
  10717. }, {
  10718. 'aria-live': 'off'
  10719. });
  10720. this.contentEl_.appendChild(createEl('span', {
  10721. className: 'vjs-control-text',
  10722. textContent: `${this.localize('Stream Type')}\u00a0`
  10723. }));
  10724. this.contentEl_.appendChild(document.createTextNode(this.localize('LIVE')));
  10725. el.appendChild(this.contentEl_);
  10726. return el;
  10727. }
  10728. dispose() {
  10729. this.contentEl_ = null;
  10730. super.dispose();
  10731. }
  10732. /**
  10733. * Check the duration to see if the LiveDisplay should be showing or not. Then show/hide
  10734. * it accordingly
  10735. *
  10736. * @param {Event} [event]
  10737. * The {@link Player#durationchange} event that caused this function to run.
  10738. *
  10739. * @listens Player#durationchange
  10740. */
  10741. updateShowing(event) {
  10742. if (this.player().duration() === Infinity) {
  10743. this.show();
  10744. } else {
  10745. this.hide();
  10746. }
  10747. }
  10748. }
  10749. Component.registerComponent('LiveDisplay', LiveDisplay);
  10750. /**
  10751. * @file seek-to-live.js
  10752. */
  10753. /**
  10754. * Displays the live indicator when duration is Infinity.
  10755. *
  10756. * @extends Component
  10757. */
  10758. class SeekToLive extends Button {
  10759. /**
  10760. * Creates an instance of this class.
  10761. *
  10762. * @param { import('./player').default } player
  10763. * The `Player` that this class should be attached to.
  10764. *
  10765. * @param {Object} [options]
  10766. * The key/value store of player options.
  10767. */
  10768. constructor(player, options) {
  10769. super(player, options);
  10770. this.updateLiveEdgeStatus();
  10771. if (this.player_.liveTracker) {
  10772. this.updateLiveEdgeStatusHandler_ = e => this.updateLiveEdgeStatus(e);
  10773. this.on(this.player_.liveTracker, 'liveedgechange', this.updateLiveEdgeStatusHandler_);
  10774. }
  10775. }
  10776. /**
  10777. * Create the `Component`'s DOM element
  10778. *
  10779. * @return {Element}
  10780. * The element that was created.
  10781. */
  10782. createEl() {
  10783. const el = super.createEl('button', {
  10784. className: 'vjs-seek-to-live-control vjs-control'
  10785. });
  10786. this.setIcon('circle', el);
  10787. this.textEl_ = createEl('span', {
  10788. className: 'vjs-seek-to-live-text',
  10789. textContent: this.localize('LIVE')
  10790. }, {
  10791. 'aria-hidden': 'true'
  10792. });
  10793. el.appendChild(this.textEl_);
  10794. return el;
  10795. }
  10796. /**
  10797. * Update the state of this button if we are at the live edge
  10798. * or not
  10799. */
  10800. updateLiveEdgeStatus() {
  10801. // default to live edge
  10802. if (!this.player_.liveTracker || this.player_.liveTracker.atLiveEdge()) {
  10803. this.setAttribute('aria-disabled', true);
  10804. this.addClass('vjs-at-live-edge');
  10805. this.controlText('Seek to live, currently playing live');
  10806. } else {
  10807. this.setAttribute('aria-disabled', false);
  10808. this.removeClass('vjs-at-live-edge');
  10809. this.controlText('Seek to live, currently behind live');
  10810. }
  10811. }
  10812. /**
  10813. * On click bring us as near to the live point as possible.
  10814. * This requires that we wait for the next `live-seekable-change`
  10815. * event which will happen every segment length seconds.
  10816. */
  10817. handleClick() {
  10818. this.player_.liveTracker.seekToLiveEdge();
  10819. }
  10820. /**
  10821. * Dispose of the element and stop tracking
  10822. */
  10823. dispose() {
  10824. if (this.player_.liveTracker) {
  10825. this.off(this.player_.liveTracker, 'liveedgechange', this.updateLiveEdgeStatusHandler_);
  10826. }
  10827. this.textEl_ = null;
  10828. super.dispose();
  10829. }
  10830. }
  10831. /**
  10832. * The text that should display over the `SeekToLive`s control. Added for localization.
  10833. *
  10834. * @type {string}
  10835. * @protected
  10836. */
  10837. SeekToLive.prototype.controlText_ = 'Seek to live, currently playing live';
  10838. Component.registerComponent('SeekToLive', SeekToLive);
  10839. /**
  10840. * @file num.js
  10841. * @module num
  10842. */
  10843. /**
  10844. * Keep a number between a min and a max value
  10845. *
  10846. * @param {number} number
  10847. * The number to clamp
  10848. *
  10849. * @param {number} min
  10850. * The minimum value
  10851. * @param {number} max
  10852. * The maximum value
  10853. *
  10854. * @return {number}
  10855. * the clamped number
  10856. */
  10857. function clamp(number, min, max) {
  10858. number = Number(number);
  10859. return Math.min(max, Math.max(min, isNaN(number) ? min : number));
  10860. }
  10861. var Num = /*#__PURE__*/Object.freeze({
  10862. __proto__: null,
  10863. clamp: clamp
  10864. });
  10865. /**
  10866. * @file slider.js
  10867. */
  10868. /**
  10869. * The base functionality for a slider. Can be vertical or horizontal.
  10870. * For instance the volume bar or the seek bar on a video is a slider.
  10871. *
  10872. * @extends Component
  10873. */
  10874. class Slider extends Component {
  10875. /**
  10876. * Create an instance of this class
  10877. *
  10878. * @param { import('../player').default } player
  10879. * The `Player` that this class should be attached to.
  10880. *
  10881. * @param {Object} [options]
  10882. * The key/value store of player options.
  10883. */
  10884. constructor(player, options) {
  10885. super(player, options);
  10886. this.handleMouseDown_ = e => this.handleMouseDown(e);
  10887. this.handleMouseUp_ = e => this.handleMouseUp(e);
  10888. this.handleKeyDown_ = e => this.handleKeyDown(e);
  10889. this.handleClick_ = e => this.handleClick(e);
  10890. this.handleMouseMove_ = e => this.handleMouseMove(e);
  10891. this.update_ = e => this.update(e);
  10892. // Set property names to bar to match with the child Slider class is looking for
  10893. this.bar = this.getChild(this.options_.barName);
  10894. // Set a horizontal or vertical class on the slider depending on the slider type
  10895. this.vertical(!!this.options_.vertical);
  10896. this.enable();
  10897. }
  10898. /**
  10899. * Are controls are currently enabled for this slider or not.
  10900. *
  10901. * @return {boolean}
  10902. * true if controls are enabled, false otherwise
  10903. */
  10904. enabled() {
  10905. return this.enabled_;
  10906. }
  10907. /**
  10908. * Enable controls for this slider if they are disabled
  10909. */
  10910. enable() {
  10911. if (this.enabled()) {
  10912. return;
  10913. }
  10914. this.on('mousedown', this.handleMouseDown_);
  10915. this.on('touchstart', this.handleMouseDown_);
  10916. this.on('keydown', this.handleKeyDown_);
  10917. this.on('click', this.handleClick_);
  10918. // TODO: deprecated, controlsvisible does not seem to be fired
  10919. this.on(this.player_, 'controlsvisible', this.update);
  10920. if (this.playerEvent) {
  10921. this.on(this.player_, this.playerEvent, this.update);
  10922. }
  10923. this.removeClass('disabled');
  10924. this.setAttribute('tabindex', 0);
  10925. this.enabled_ = true;
  10926. }
  10927. /**
  10928. * Disable controls for this slider if they are enabled
  10929. */
  10930. disable() {
  10931. if (!this.enabled()) {
  10932. return;
  10933. }
  10934. const doc = this.bar.el_.ownerDocument;
  10935. this.off('mousedown', this.handleMouseDown_);
  10936. this.off('touchstart', this.handleMouseDown_);
  10937. this.off('keydown', this.handleKeyDown_);
  10938. this.off('click', this.handleClick_);
  10939. this.off(this.player_, 'controlsvisible', this.update_);
  10940. this.off(doc, 'mousemove', this.handleMouseMove_);
  10941. this.off(doc, 'mouseup', this.handleMouseUp_);
  10942. this.off(doc, 'touchmove', this.handleMouseMove_);
  10943. this.off(doc, 'touchend', this.handleMouseUp_);
  10944. this.removeAttribute('tabindex');
  10945. this.addClass('disabled');
  10946. if (this.playerEvent) {
  10947. this.off(this.player_, this.playerEvent, this.update);
  10948. }
  10949. this.enabled_ = false;
  10950. }
  10951. /**
  10952. * Create the `Slider`s DOM element.
  10953. *
  10954. * @param {string} type
  10955. * Type of element to create.
  10956. *
  10957. * @param {Object} [props={}]
  10958. * List of properties in Object form.
  10959. *
  10960. * @param {Object} [attributes={}]
  10961. * list of attributes in Object form.
  10962. *
  10963. * @return {Element}
  10964. * The element that gets created.
  10965. */
  10966. createEl(type, props = {}, attributes = {}) {
  10967. // Add the slider element class to all sub classes
  10968. props.className = props.className + ' vjs-slider';
  10969. props = Object.assign({
  10970. tabIndex: 0
  10971. }, props);
  10972. attributes = Object.assign({
  10973. 'role': 'slider',
  10974. 'aria-valuenow': 0,
  10975. 'aria-valuemin': 0,
  10976. 'aria-valuemax': 100
  10977. }, attributes);
  10978. return super.createEl(type, props, attributes);
  10979. }
  10980. /**
  10981. * Handle `mousedown` or `touchstart` events on the `Slider`.
  10982. *
  10983. * @param {MouseEvent} event
  10984. * `mousedown` or `touchstart` event that triggered this function
  10985. *
  10986. * @listens mousedown
  10987. * @listens touchstart
  10988. * @fires Slider#slideractive
  10989. */
  10990. handleMouseDown(event) {
  10991. const doc = this.bar.el_.ownerDocument;
  10992. if (event.type === 'mousedown') {
  10993. event.preventDefault();
  10994. }
  10995. // Do not call preventDefault() on touchstart in Chrome
  10996. // to avoid console warnings. Use a 'touch-action: none' style
  10997. // instead to prevent unintended scrolling.
  10998. // https://developers.google.com/web/updates/2017/01/scrolling-intervention
  10999. if (event.type === 'touchstart' && !IS_CHROME) {
  11000. event.preventDefault();
  11001. }
  11002. blockTextSelection();
  11003. this.addClass('vjs-sliding');
  11004. /**
  11005. * Triggered when the slider is in an active state
  11006. *
  11007. * @event Slider#slideractive
  11008. * @type {MouseEvent}
  11009. */
  11010. this.trigger('slideractive');
  11011. this.on(doc, 'mousemove', this.handleMouseMove_);
  11012. this.on(doc, 'mouseup', this.handleMouseUp_);
  11013. this.on(doc, 'touchmove', this.handleMouseMove_);
  11014. this.on(doc, 'touchend', this.handleMouseUp_);
  11015. this.handleMouseMove(event, true);
  11016. }
  11017. /**
  11018. * Handle the `mousemove`, `touchmove`, and `mousedown` events on this `Slider`.
  11019. * The `mousemove` and `touchmove` events will only only trigger this function during
  11020. * `mousedown` and `touchstart`. This is due to {@link Slider#handleMouseDown} and
  11021. * {@link Slider#handleMouseUp}.
  11022. *
  11023. * @param {MouseEvent} event
  11024. * `mousedown`, `mousemove`, `touchstart`, or `touchmove` event that triggered
  11025. * this function
  11026. * @param {boolean} mouseDown this is a flag that should be set to true if `handleMouseMove` is called directly. It allows us to skip things that should not happen if coming from mouse down but should happen on regular mouse move handler. Defaults to false.
  11027. *
  11028. * @listens mousemove
  11029. * @listens touchmove
  11030. */
  11031. handleMouseMove(event) {}
  11032. /**
  11033. * Handle `mouseup` or `touchend` events on the `Slider`.
  11034. *
  11035. * @param {MouseEvent} event
  11036. * `mouseup` or `touchend` event that triggered this function.
  11037. *
  11038. * @listens touchend
  11039. * @listens mouseup
  11040. * @fires Slider#sliderinactive
  11041. */
  11042. handleMouseUp(event) {
  11043. const doc = this.bar.el_.ownerDocument;
  11044. unblockTextSelection();
  11045. this.removeClass('vjs-sliding');
  11046. /**
  11047. * Triggered when the slider is no longer in an active state.
  11048. *
  11049. * @event Slider#sliderinactive
  11050. * @type {Event}
  11051. */
  11052. this.trigger('sliderinactive');
  11053. this.off(doc, 'mousemove', this.handleMouseMove_);
  11054. this.off(doc, 'mouseup', this.handleMouseUp_);
  11055. this.off(doc, 'touchmove', this.handleMouseMove_);
  11056. this.off(doc, 'touchend', this.handleMouseUp_);
  11057. this.update();
  11058. }
  11059. /**
  11060. * Update the progress bar of the `Slider`.
  11061. *
  11062. * @return {number}
  11063. * The percentage of progress the progress bar represents as a
  11064. * number from 0 to 1.
  11065. */
  11066. update() {
  11067. // In VolumeBar init we have a setTimeout for update that pops and update
  11068. // to the end of the execution stack. The player is destroyed before then
  11069. // update will cause an error
  11070. // If there's no bar...
  11071. if (!this.el_ || !this.bar) {
  11072. return;
  11073. }
  11074. // clamp progress between 0 and 1
  11075. // and only round to four decimal places, as we round to two below
  11076. const progress = this.getProgress();
  11077. if (progress === this.progress_) {
  11078. return progress;
  11079. }
  11080. this.progress_ = progress;
  11081. this.requestNamedAnimationFrame('Slider#update', () => {
  11082. // Set the new bar width or height
  11083. const sizeKey = this.vertical() ? 'height' : 'width';
  11084. // Convert to a percentage for css value
  11085. this.bar.el().style[sizeKey] = (progress * 100).toFixed(2) + '%';
  11086. });
  11087. return progress;
  11088. }
  11089. /**
  11090. * Get the percentage of the bar that should be filled
  11091. * but clamped and rounded.
  11092. *
  11093. * @return {number}
  11094. * percentage filled that the slider is
  11095. */
  11096. getProgress() {
  11097. return Number(clamp(this.getPercent(), 0, 1).toFixed(4));
  11098. }
  11099. /**
  11100. * Calculate distance for slider
  11101. *
  11102. * @param {Event} event
  11103. * The event that caused this function to run.
  11104. *
  11105. * @return {number}
  11106. * The current position of the Slider.
  11107. * - position.x for vertical `Slider`s
  11108. * - position.y for horizontal `Slider`s
  11109. */
  11110. calculateDistance(event) {
  11111. const position = getPointerPosition(this.el_, event);
  11112. if (this.vertical()) {
  11113. return position.y;
  11114. }
  11115. return position.x;
  11116. }
  11117. /**
  11118. * Handle a `keydown` event on the `Slider`. Watches for left, right, up, and down
  11119. * arrow keys. This function will only be called when the slider has focus. See
  11120. * {@link Slider#handleFocus} and {@link Slider#handleBlur}.
  11121. *
  11122. * @param {KeyboardEvent} event
  11123. * the `keydown` event that caused this function to run.
  11124. *
  11125. * @listens keydown
  11126. */
  11127. handleKeyDown(event) {
  11128. // Left and Down Arrows
  11129. if (keycode.isEventKey(event, 'Left') || keycode.isEventKey(event, 'Down')) {
  11130. event.preventDefault();
  11131. event.stopPropagation();
  11132. this.stepBack();
  11133. // Up and Right Arrows
  11134. } else if (keycode.isEventKey(event, 'Right') || keycode.isEventKey(event, 'Up')) {
  11135. event.preventDefault();
  11136. event.stopPropagation();
  11137. this.stepForward();
  11138. } else {
  11139. // Pass keydown handling up for unsupported keys
  11140. super.handleKeyDown(event);
  11141. }
  11142. }
  11143. /**
  11144. * Listener for click events on slider, used to prevent clicks
  11145. * from bubbling up to parent elements like button menus.
  11146. *
  11147. * @param {Object} event
  11148. * Event that caused this object to run
  11149. */
  11150. handleClick(event) {
  11151. event.stopPropagation();
  11152. event.preventDefault();
  11153. }
  11154. /**
  11155. * Get/set if slider is horizontal for vertical
  11156. *
  11157. * @param {boolean} [bool]
  11158. * - true if slider is vertical,
  11159. * - false is horizontal
  11160. *
  11161. * @return {boolean}
  11162. * - true if slider is vertical, and getting
  11163. * - false if the slider is horizontal, and getting
  11164. */
  11165. vertical(bool) {
  11166. if (bool === undefined) {
  11167. return this.vertical_ || false;
  11168. }
  11169. this.vertical_ = !!bool;
  11170. if (this.vertical_) {
  11171. this.addClass('vjs-slider-vertical');
  11172. } else {
  11173. this.addClass('vjs-slider-horizontal');
  11174. }
  11175. }
  11176. }
  11177. Component.registerComponent('Slider', Slider);
  11178. /**
  11179. * @file load-progress-bar.js
  11180. */
  11181. // get the percent width of a time compared to the total end
  11182. const percentify = (time, end) => clamp(time / end * 100, 0, 100).toFixed(2) + '%';
  11183. /**
  11184. * Shows loading progress
  11185. *
  11186. * @extends Component
  11187. */
  11188. class LoadProgressBar extends Component {
  11189. /**
  11190. * Creates an instance of this class.
  11191. *
  11192. * @param { import('../../player').default } player
  11193. * The `Player` that this class should be attached to.
  11194. *
  11195. * @param {Object} [options]
  11196. * The key/value store of player options.
  11197. */
  11198. constructor(player, options) {
  11199. super(player, options);
  11200. this.partEls_ = [];
  11201. this.on(player, 'progress', e => this.update(e));
  11202. }
  11203. /**
  11204. * Create the `Component`'s DOM element
  11205. *
  11206. * @return {Element}
  11207. * The element that was created.
  11208. */
  11209. createEl() {
  11210. const el = super.createEl('div', {
  11211. className: 'vjs-load-progress'
  11212. });
  11213. const wrapper = createEl('span', {
  11214. className: 'vjs-control-text'
  11215. });
  11216. const loadedText = createEl('span', {
  11217. textContent: this.localize('Loaded')
  11218. });
  11219. const separator = document.createTextNode(': ');
  11220. this.percentageEl_ = createEl('span', {
  11221. className: 'vjs-control-text-loaded-percentage',
  11222. textContent: '0%'
  11223. });
  11224. el.appendChild(wrapper);
  11225. wrapper.appendChild(loadedText);
  11226. wrapper.appendChild(separator);
  11227. wrapper.appendChild(this.percentageEl_);
  11228. return el;
  11229. }
  11230. dispose() {
  11231. this.partEls_ = null;
  11232. this.percentageEl_ = null;
  11233. super.dispose();
  11234. }
  11235. /**
  11236. * Update progress bar
  11237. *
  11238. * @param {Event} [event]
  11239. * The `progress` event that caused this function to run.
  11240. *
  11241. * @listens Player#progress
  11242. */
  11243. update(event) {
  11244. this.requestNamedAnimationFrame('LoadProgressBar#update', () => {
  11245. const liveTracker = this.player_.liveTracker;
  11246. const buffered = this.player_.buffered();
  11247. const duration = liveTracker && liveTracker.isLive() ? liveTracker.seekableEnd() : this.player_.duration();
  11248. const bufferedEnd = this.player_.bufferedEnd();
  11249. const children = this.partEls_;
  11250. const percent = percentify(bufferedEnd, duration);
  11251. if (this.percent_ !== percent) {
  11252. // update the width of the progress bar
  11253. this.el_.style.width = percent;
  11254. // update the control-text
  11255. textContent(this.percentageEl_, percent);
  11256. this.percent_ = percent;
  11257. }
  11258. // add child elements to represent the individual buffered time ranges
  11259. for (let i = 0; i < buffered.length; i++) {
  11260. const start = buffered.start(i);
  11261. const end = buffered.end(i);
  11262. let part = children[i];
  11263. if (!part) {
  11264. part = this.el_.appendChild(createEl());
  11265. children[i] = part;
  11266. }
  11267. // only update if changed
  11268. if (part.dataset.start === start && part.dataset.end === end) {
  11269. continue;
  11270. }
  11271. part.dataset.start = start;
  11272. part.dataset.end = end;
  11273. // set the percent based on the width of the progress bar (bufferedEnd)
  11274. part.style.left = percentify(start, bufferedEnd);
  11275. part.style.width = percentify(end - start, bufferedEnd);
  11276. }
  11277. // remove unused buffered range elements
  11278. for (let i = children.length; i > buffered.length; i--) {
  11279. this.el_.removeChild(children[i - 1]);
  11280. }
  11281. children.length = buffered.length;
  11282. });
  11283. }
  11284. }
  11285. Component.registerComponent('LoadProgressBar', LoadProgressBar);
  11286. /**
  11287. * @file time-tooltip.js
  11288. */
  11289. /**
  11290. * Time tooltips display a time above the progress bar.
  11291. *
  11292. * @extends Component
  11293. */
  11294. class TimeTooltip extends Component {
  11295. /**
  11296. * Creates an instance of this class.
  11297. *
  11298. * @param { import('../../player').default } player
  11299. * The {@link Player} that this class should be attached to.
  11300. *
  11301. * @param {Object} [options]
  11302. * The key/value store of player options.
  11303. */
  11304. constructor(player, options) {
  11305. super(player, options);
  11306. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11307. }
  11308. /**
  11309. * Create the time tooltip DOM element
  11310. *
  11311. * @return {Element}
  11312. * The element that was created.
  11313. */
  11314. createEl() {
  11315. return super.createEl('div', {
  11316. className: 'vjs-time-tooltip'
  11317. }, {
  11318. 'aria-hidden': 'true'
  11319. });
  11320. }
  11321. /**
  11322. * Updates the position of the time tooltip relative to the `SeekBar`.
  11323. *
  11324. * @param {Object} seekBarRect
  11325. * The `ClientRect` for the {@link SeekBar} element.
  11326. *
  11327. * @param {number} seekBarPoint
  11328. * A number from 0 to 1, representing a horizontal reference point
  11329. * from the left edge of the {@link SeekBar}
  11330. */
  11331. update(seekBarRect, seekBarPoint, content) {
  11332. const tooltipRect = findPosition(this.el_);
  11333. const playerRect = getBoundingClientRect(this.player_.el());
  11334. const seekBarPointPx = seekBarRect.width * seekBarPoint;
  11335. // do nothing if either rect isn't available
  11336. // for example, if the player isn't in the DOM for testing
  11337. if (!playerRect || !tooltipRect) {
  11338. return;
  11339. }
  11340. // This is the space left of the `seekBarPoint` available within the bounds
  11341. // of the player. We calculate any gap between the left edge of the player
  11342. // and the left edge of the `SeekBar` and add the number of pixels in the
  11343. // `SeekBar` before hitting the `seekBarPoint`
  11344. const spaceLeftOfPoint = seekBarRect.left - playerRect.left + seekBarPointPx;
  11345. // This is the space right of the `seekBarPoint` available within the bounds
  11346. // of the player. We calculate the number of pixels from the `seekBarPoint`
  11347. // to the right edge of the `SeekBar` and add to that any gap between the
  11348. // right edge of the `SeekBar` and the player.
  11349. const spaceRightOfPoint = seekBarRect.width - seekBarPointPx + (playerRect.right - seekBarRect.right);
  11350. // This is the number of pixels by which the tooltip will need to be pulled
  11351. // further to the right to center it over the `seekBarPoint`.
  11352. let pullTooltipBy = tooltipRect.width / 2;
  11353. // Adjust the `pullTooltipBy` distance to the left or right depending on
  11354. // the results of the space calculations above.
  11355. if (spaceLeftOfPoint < pullTooltipBy) {
  11356. pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
  11357. } else if (spaceRightOfPoint < pullTooltipBy) {
  11358. pullTooltipBy = spaceRightOfPoint;
  11359. }
  11360. // Due to the imprecision of decimal/ratio based calculations and varying
  11361. // rounding behaviors, there are cases where the spacing adjustment is off
  11362. // by a pixel or two. This adds insurance to these calculations.
  11363. if (pullTooltipBy < 0) {
  11364. pullTooltipBy = 0;
  11365. } else if (pullTooltipBy > tooltipRect.width) {
  11366. pullTooltipBy = tooltipRect.width;
  11367. }
  11368. // prevent small width fluctuations within 0.4px from
  11369. // changing the value below.
  11370. // This really helps for live to prevent the play
  11371. // progress time tooltip from jittering
  11372. pullTooltipBy = Math.round(pullTooltipBy);
  11373. this.el_.style.right = `-${pullTooltipBy}px`;
  11374. this.write(content);
  11375. }
  11376. /**
  11377. * Write the time to the tooltip DOM element.
  11378. *
  11379. * @param {string} content
  11380. * The formatted time for the tooltip.
  11381. */
  11382. write(content) {
  11383. textContent(this.el_, content);
  11384. }
  11385. /**
  11386. * Updates the position of the time tooltip relative to the `SeekBar`.
  11387. *
  11388. * @param {Object} seekBarRect
  11389. * The `ClientRect` for the {@link SeekBar} element.
  11390. *
  11391. * @param {number} seekBarPoint
  11392. * A number from 0 to 1, representing a horizontal reference point
  11393. * from the left edge of the {@link SeekBar}
  11394. *
  11395. * @param {number} time
  11396. * The time to update the tooltip to, not used during live playback
  11397. *
  11398. * @param {Function} cb
  11399. * A function that will be called during the request animation frame
  11400. * for tooltips that need to do additional animations from the default
  11401. */
  11402. updateTime(seekBarRect, seekBarPoint, time, cb) {
  11403. this.requestNamedAnimationFrame('TimeTooltip#updateTime', () => {
  11404. let content;
  11405. const duration = this.player_.duration();
  11406. if (this.player_.liveTracker && this.player_.liveTracker.isLive()) {
  11407. const liveWindow = this.player_.liveTracker.liveWindow();
  11408. const secondsBehind = liveWindow - seekBarPoint * liveWindow;
  11409. content = (secondsBehind < 1 ? '' : '-') + formatTime(secondsBehind, liveWindow);
  11410. } else {
  11411. content = formatTime(time, duration);
  11412. }
  11413. this.update(seekBarRect, seekBarPoint, content);
  11414. if (cb) {
  11415. cb();
  11416. }
  11417. });
  11418. }
  11419. }
  11420. Component.registerComponent('TimeTooltip', TimeTooltip);
  11421. /**
  11422. * @file play-progress-bar.js
  11423. */
  11424. /**
  11425. * Used by {@link SeekBar} to display media playback progress as part of the
  11426. * {@link ProgressControl}.
  11427. *
  11428. * @extends Component
  11429. */
  11430. class PlayProgressBar extends Component {
  11431. /**
  11432. * Creates an instance of this class.
  11433. *
  11434. * @param { import('../../player').default } player
  11435. * The {@link Player} that this class should be attached to.
  11436. *
  11437. * @param {Object} [options]
  11438. * The key/value store of player options.
  11439. */
  11440. constructor(player, options) {
  11441. super(player, options);
  11442. this.setIcon('circle');
  11443. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11444. }
  11445. /**
  11446. * Create the the DOM element for this class.
  11447. *
  11448. * @return {Element}
  11449. * The element that was created.
  11450. */
  11451. createEl() {
  11452. return super.createEl('div', {
  11453. className: 'vjs-play-progress vjs-slider-bar'
  11454. }, {
  11455. 'aria-hidden': 'true'
  11456. });
  11457. }
  11458. /**
  11459. * Enqueues updates to its own DOM as well as the DOM of its
  11460. * {@link TimeTooltip} child.
  11461. *
  11462. * @param {Object} seekBarRect
  11463. * The `ClientRect` for the {@link SeekBar} element.
  11464. *
  11465. * @param {number} seekBarPoint
  11466. * A number from 0 to 1, representing a horizontal reference point
  11467. * from the left edge of the {@link SeekBar}
  11468. */
  11469. update(seekBarRect, seekBarPoint) {
  11470. const timeTooltip = this.getChild('timeTooltip');
  11471. if (!timeTooltip) {
  11472. return;
  11473. }
  11474. const time = this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  11475. timeTooltip.updateTime(seekBarRect, seekBarPoint, time);
  11476. }
  11477. }
  11478. /**
  11479. * Default options for {@link PlayProgressBar}.
  11480. *
  11481. * @type {Object}
  11482. * @private
  11483. */
  11484. PlayProgressBar.prototype.options_ = {
  11485. children: []
  11486. };
  11487. // Time tooltips should not be added to a player on mobile devices
  11488. if (!IS_IOS && !IS_ANDROID) {
  11489. PlayProgressBar.prototype.options_.children.push('timeTooltip');
  11490. }
  11491. Component.registerComponent('PlayProgressBar', PlayProgressBar);
  11492. /**
  11493. * @file mouse-time-display.js
  11494. */
  11495. /**
  11496. * The {@link MouseTimeDisplay} component tracks mouse movement over the
  11497. * {@link ProgressControl}. It displays an indicator and a {@link TimeTooltip}
  11498. * indicating the time which is represented by a given point in the
  11499. * {@link ProgressControl}.
  11500. *
  11501. * @extends Component
  11502. */
  11503. class MouseTimeDisplay extends Component {
  11504. /**
  11505. * Creates an instance of this class.
  11506. *
  11507. * @param { import('../../player').default } player
  11508. * The {@link Player} that this class should be attached to.
  11509. *
  11510. * @param {Object} [options]
  11511. * The key/value store of player options.
  11512. */
  11513. constructor(player, options) {
  11514. super(player, options);
  11515. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11516. }
  11517. /**
  11518. * Create the DOM element for this class.
  11519. *
  11520. * @return {Element}
  11521. * The element that was created.
  11522. */
  11523. createEl() {
  11524. return super.createEl('div', {
  11525. className: 'vjs-mouse-display'
  11526. });
  11527. }
  11528. /**
  11529. * Enqueues updates to its own DOM as well as the DOM of its
  11530. * {@link TimeTooltip} child.
  11531. *
  11532. * @param {Object} seekBarRect
  11533. * The `ClientRect` for the {@link SeekBar} element.
  11534. *
  11535. * @param {number} seekBarPoint
  11536. * A number from 0 to 1, representing a horizontal reference point
  11537. * from the left edge of the {@link SeekBar}
  11538. */
  11539. update(seekBarRect, seekBarPoint) {
  11540. const time = seekBarPoint * this.player_.duration();
  11541. this.getChild('timeTooltip').updateTime(seekBarRect, seekBarPoint, time, () => {
  11542. this.el_.style.left = `${seekBarRect.width * seekBarPoint}px`;
  11543. });
  11544. }
  11545. }
  11546. /**
  11547. * Default options for `MouseTimeDisplay`
  11548. *
  11549. * @type {Object}
  11550. * @private
  11551. */
  11552. MouseTimeDisplay.prototype.options_ = {
  11553. children: ['timeTooltip']
  11554. };
  11555. Component.registerComponent('MouseTimeDisplay', MouseTimeDisplay);
  11556. /**
  11557. * @file seek-bar.js
  11558. */
  11559. // The number of seconds the `step*` functions move the timeline.
  11560. const STEP_SECONDS = 5;
  11561. // The multiplier of STEP_SECONDS that PgUp/PgDown move the timeline.
  11562. const PAGE_KEY_MULTIPLIER = 12;
  11563. /**
  11564. * Seek bar and container for the progress bars. Uses {@link PlayProgressBar}
  11565. * as its `bar`.
  11566. *
  11567. * @extends Slider
  11568. */
  11569. class SeekBar extends Slider {
  11570. /**
  11571. * Creates an instance of this class.
  11572. *
  11573. * @param { import('../../player').default } player
  11574. * The `Player` that this class should be attached to.
  11575. *
  11576. * @param {Object} [options]
  11577. * The key/value store of player options.
  11578. */
  11579. constructor(player, options) {
  11580. super(player, options);
  11581. this.setEventHandlers_();
  11582. }
  11583. /**
  11584. * Sets the event handlers
  11585. *
  11586. * @private
  11587. */
  11588. setEventHandlers_() {
  11589. this.update_ = bind_(this, this.update);
  11590. this.update = throttle(this.update_, UPDATE_REFRESH_INTERVAL);
  11591. this.on(this.player_, ['ended', 'durationchange', 'timeupdate'], this.update);
  11592. if (this.player_.liveTracker) {
  11593. this.on(this.player_.liveTracker, 'liveedgechange', this.update);
  11594. }
  11595. // when playing, let's ensure we smoothly update the play progress bar
  11596. // via an interval
  11597. this.updateInterval = null;
  11598. this.enableIntervalHandler_ = e => this.enableInterval_(e);
  11599. this.disableIntervalHandler_ = e => this.disableInterval_(e);
  11600. this.on(this.player_, ['playing'], this.enableIntervalHandler_);
  11601. this.on(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
  11602. // we don't need to update the play progress if the document is hidden,
  11603. // also, this causes the CPU to spike and eventually crash the page on IE11.
  11604. if ('hidden' in document && 'visibilityState' in document) {
  11605. this.on(document, 'visibilitychange', this.toggleVisibility_);
  11606. }
  11607. }
  11608. toggleVisibility_(e) {
  11609. if (document.visibilityState === 'hidden') {
  11610. this.cancelNamedAnimationFrame('SeekBar#update');
  11611. this.cancelNamedAnimationFrame('Slider#update');
  11612. this.disableInterval_(e);
  11613. } else {
  11614. if (!this.player_.ended() && !this.player_.paused()) {
  11615. this.enableInterval_();
  11616. }
  11617. // we just switched back to the page and someone may be looking, so, update ASAP
  11618. this.update();
  11619. }
  11620. }
  11621. enableInterval_() {
  11622. if (this.updateInterval) {
  11623. return;
  11624. }
  11625. this.updateInterval = this.setInterval(this.update, UPDATE_REFRESH_INTERVAL);
  11626. }
  11627. disableInterval_(e) {
  11628. if (this.player_.liveTracker && this.player_.liveTracker.isLive() && e && e.type !== 'ended') {
  11629. return;
  11630. }
  11631. if (!this.updateInterval) {
  11632. return;
  11633. }
  11634. this.clearInterval(this.updateInterval);
  11635. this.updateInterval = null;
  11636. }
  11637. /**
  11638. * Create the `Component`'s DOM element
  11639. *
  11640. * @return {Element}
  11641. * The element that was created.
  11642. */
  11643. createEl() {
  11644. return super.createEl('div', {
  11645. className: 'vjs-progress-holder'
  11646. }, {
  11647. 'aria-label': this.localize('Progress Bar')
  11648. });
  11649. }
  11650. /**
  11651. * This function updates the play progress bar and accessibility
  11652. * attributes to whatever is passed in.
  11653. *
  11654. * @param {Event} [event]
  11655. * The `timeupdate` or `ended` event that caused this to run.
  11656. *
  11657. * @listens Player#timeupdate
  11658. *
  11659. * @return {number}
  11660. * The current percent at a number from 0-1
  11661. */
  11662. update(event) {
  11663. // ignore updates while the tab is hidden
  11664. if (document.visibilityState === 'hidden') {
  11665. return;
  11666. }
  11667. const percent = super.update();
  11668. this.requestNamedAnimationFrame('SeekBar#update', () => {
  11669. const currentTime = this.player_.ended() ? this.player_.duration() : this.getCurrentTime_();
  11670. const liveTracker = this.player_.liveTracker;
  11671. let duration = this.player_.duration();
  11672. if (liveTracker && liveTracker.isLive()) {
  11673. duration = this.player_.liveTracker.liveCurrentTime();
  11674. }
  11675. if (this.percent_ !== percent) {
  11676. // machine readable value of progress bar (percentage complete)
  11677. this.el_.setAttribute('aria-valuenow', (percent * 100).toFixed(2));
  11678. this.percent_ = percent;
  11679. }
  11680. if (this.currentTime_ !== currentTime || this.duration_ !== duration) {
  11681. // human readable value of progress bar (time complete)
  11682. this.el_.setAttribute('aria-valuetext', this.localize('progress bar timing: currentTime={1} duration={2}', [formatTime(currentTime, duration), formatTime(duration, duration)], '{1} of {2}'));
  11683. this.currentTime_ = currentTime;
  11684. this.duration_ = duration;
  11685. }
  11686. // update the progress bar time tooltip with the current time
  11687. if (this.bar) {
  11688. this.bar.update(getBoundingClientRect(this.el()), this.getProgress());
  11689. }
  11690. });
  11691. return percent;
  11692. }
  11693. /**
  11694. * Prevent liveThreshold from causing seeks to seem like they
  11695. * are not happening from a user perspective.
  11696. *
  11697. * @param {number} ct
  11698. * current time to seek to
  11699. */
  11700. userSeek_(ct) {
  11701. if (this.player_.liveTracker && this.player_.liveTracker.isLive()) {
  11702. this.player_.liveTracker.nextSeekedFromUser();
  11703. }
  11704. this.player_.currentTime(ct);
  11705. }
  11706. /**
  11707. * Get the value of current time but allows for smooth scrubbing,
  11708. * when player can't keep up.
  11709. *
  11710. * @return {number}
  11711. * The current time value to display
  11712. *
  11713. * @private
  11714. */
  11715. getCurrentTime_() {
  11716. return this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  11717. }
  11718. /**
  11719. * Get the percentage of media played so far.
  11720. *
  11721. * @return {number}
  11722. * The percentage of media played so far (0 to 1).
  11723. */
  11724. getPercent() {
  11725. const currentTime = this.getCurrentTime_();
  11726. let percent;
  11727. const liveTracker = this.player_.liveTracker;
  11728. if (liveTracker && liveTracker.isLive()) {
  11729. percent = (currentTime - liveTracker.seekableStart()) / liveTracker.liveWindow();
  11730. // prevent the percent from changing at the live edge
  11731. if (liveTracker.atLiveEdge()) {
  11732. percent = 1;
  11733. }
  11734. } else {
  11735. percent = currentTime / this.player_.duration();
  11736. }
  11737. return percent;
  11738. }
  11739. /**
  11740. * Handle mouse down on seek bar
  11741. *
  11742. * @param {MouseEvent} event
  11743. * The `mousedown` event that caused this to run.
  11744. *
  11745. * @listens mousedown
  11746. */
  11747. handleMouseDown(event) {
  11748. if (!isSingleLeftClick(event)) {
  11749. return;
  11750. }
  11751. // Stop event propagation to prevent double fire in progress-control.js
  11752. event.stopPropagation();
  11753. this.videoWasPlaying = !this.player_.paused();
  11754. this.player_.pause();
  11755. super.handleMouseDown(event);
  11756. }
  11757. /**
  11758. * Handle mouse move on seek bar
  11759. *
  11760. * @param {MouseEvent} event
  11761. * The `mousemove` event that caused this to run.
  11762. * @param {boolean} mouseDown this is a flag that should be set to true if `handleMouseMove` is called directly. It allows us to skip things that should not happen if coming from mouse down but should happen on regular mouse move handler. Defaults to false
  11763. *
  11764. * @listens mousemove
  11765. */
  11766. handleMouseMove(event, mouseDown = false) {
  11767. if (!isSingleLeftClick(event) || isNaN(this.player_.duration())) {
  11768. return;
  11769. }
  11770. if (!mouseDown && !this.player_.scrubbing()) {
  11771. this.player_.scrubbing(true);
  11772. }
  11773. let newTime;
  11774. const distance = this.calculateDistance(event);
  11775. const liveTracker = this.player_.liveTracker;
  11776. if (!liveTracker || !liveTracker.isLive()) {
  11777. newTime = distance * this.player_.duration();
  11778. // Don't let video end while scrubbing.
  11779. if (newTime === this.player_.duration()) {
  11780. newTime = newTime - 0.1;
  11781. }
  11782. } else {
  11783. if (distance >= 0.99) {
  11784. liveTracker.seekToLiveEdge();
  11785. return;
  11786. }
  11787. const seekableStart = liveTracker.seekableStart();
  11788. const seekableEnd = liveTracker.liveCurrentTime();
  11789. newTime = seekableStart + distance * liveTracker.liveWindow();
  11790. // Don't let video end while scrubbing.
  11791. if (newTime >= seekableEnd) {
  11792. newTime = seekableEnd;
  11793. }
  11794. // Compensate for precision differences so that currentTime is not less
  11795. // than seekable start
  11796. if (newTime <= seekableStart) {
  11797. newTime = seekableStart + 0.1;
  11798. }
  11799. // On android seekableEnd can be Infinity sometimes,
  11800. // this will cause newTime to be Infinity, which is
  11801. // not a valid currentTime.
  11802. if (newTime === Infinity) {
  11803. return;
  11804. }
  11805. }
  11806. // Set new time (tell player to seek to new time)
  11807. this.userSeek_(newTime);
  11808. if (this.player_.options_.enableSmoothSeeking) {
  11809. this.update();
  11810. }
  11811. }
  11812. enable() {
  11813. super.enable();
  11814. const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
  11815. if (!mouseTimeDisplay) {
  11816. return;
  11817. }
  11818. mouseTimeDisplay.show();
  11819. }
  11820. disable() {
  11821. super.disable();
  11822. const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
  11823. if (!mouseTimeDisplay) {
  11824. return;
  11825. }
  11826. mouseTimeDisplay.hide();
  11827. }
  11828. /**
  11829. * Handle mouse up on seek bar
  11830. *
  11831. * @param {MouseEvent} event
  11832. * The `mouseup` event that caused this to run.
  11833. *
  11834. * @listens mouseup
  11835. */
  11836. handleMouseUp(event) {
  11837. super.handleMouseUp(event);
  11838. // Stop event propagation to prevent double fire in progress-control.js
  11839. if (event) {
  11840. event.stopPropagation();
  11841. }
  11842. this.player_.scrubbing(false);
  11843. /**
  11844. * Trigger timeupdate because we're done seeking and the time has changed.
  11845. * This is particularly useful for if the player is paused to time the time displays.
  11846. *
  11847. * @event Tech#timeupdate
  11848. * @type {Event}
  11849. */
  11850. this.player_.trigger({
  11851. type: 'timeupdate',
  11852. target: this,
  11853. manuallyTriggered: true
  11854. });
  11855. if (this.videoWasPlaying) {
  11856. silencePromise(this.player_.play());
  11857. } else {
  11858. // We're done seeking and the time has changed.
  11859. // If the player is paused, make sure we display the correct time on the seek bar.
  11860. this.update_();
  11861. }
  11862. }
  11863. /**
  11864. * Move more quickly fast forward for keyboard-only users
  11865. */
  11866. stepForward() {
  11867. this.userSeek_(this.player_.currentTime() + STEP_SECONDS);
  11868. }
  11869. /**
  11870. * Move more quickly rewind for keyboard-only users
  11871. */
  11872. stepBack() {
  11873. this.userSeek_(this.player_.currentTime() - STEP_SECONDS);
  11874. }
  11875. /**
  11876. * Toggles the playback state of the player
  11877. * This gets called when enter or space is used on the seekbar
  11878. *
  11879. * @param {KeyboardEvent} event
  11880. * The `keydown` event that caused this function to be called
  11881. *
  11882. */
  11883. handleAction(event) {
  11884. if (this.player_.paused()) {
  11885. this.player_.play();
  11886. } else {
  11887. this.player_.pause();
  11888. }
  11889. }
  11890. /**
  11891. * Called when this SeekBar has focus and a key gets pressed down.
  11892. * Supports the following keys:
  11893. *
  11894. * Space or Enter key fire a click event
  11895. * Home key moves to start of the timeline
  11896. * End key moves to end of the timeline
  11897. * Digit "0" through "9" keys move to 0%, 10% ... 80%, 90% of the timeline
  11898. * PageDown key moves back a larger step than ArrowDown
  11899. * PageUp key moves forward a large step
  11900. *
  11901. * @param {KeyboardEvent} event
  11902. * The `keydown` event that caused this function to be called.
  11903. *
  11904. * @listens keydown
  11905. */
  11906. handleKeyDown(event) {
  11907. const liveTracker = this.player_.liveTracker;
  11908. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  11909. event.preventDefault();
  11910. event.stopPropagation();
  11911. this.handleAction(event);
  11912. } else if (keycode.isEventKey(event, 'Home')) {
  11913. event.preventDefault();
  11914. event.stopPropagation();
  11915. this.userSeek_(0);
  11916. } else if (keycode.isEventKey(event, 'End')) {
  11917. event.preventDefault();
  11918. event.stopPropagation();
  11919. if (liveTracker && liveTracker.isLive()) {
  11920. this.userSeek_(liveTracker.liveCurrentTime());
  11921. } else {
  11922. this.userSeek_(this.player_.duration());
  11923. }
  11924. } else if (/^[0-9]$/.test(keycode(event))) {
  11925. event.preventDefault();
  11926. event.stopPropagation();
  11927. const gotoFraction = (keycode.codes[keycode(event)] - keycode.codes['0']) * 10.0 / 100.0;
  11928. if (liveTracker && liveTracker.isLive()) {
  11929. this.userSeek_(liveTracker.seekableStart() + liveTracker.liveWindow() * gotoFraction);
  11930. } else {
  11931. this.userSeek_(this.player_.duration() * gotoFraction);
  11932. }
  11933. } else if (keycode.isEventKey(event, 'PgDn')) {
  11934. event.preventDefault();
  11935. event.stopPropagation();
  11936. this.userSeek_(this.player_.currentTime() - STEP_SECONDS * PAGE_KEY_MULTIPLIER);
  11937. } else if (keycode.isEventKey(event, 'PgUp')) {
  11938. event.preventDefault();
  11939. event.stopPropagation();
  11940. this.userSeek_(this.player_.currentTime() + STEP_SECONDS * PAGE_KEY_MULTIPLIER);
  11941. } else {
  11942. // Pass keydown handling up for unsupported keys
  11943. super.handleKeyDown(event);
  11944. }
  11945. }
  11946. dispose() {
  11947. this.disableInterval_();
  11948. this.off(this.player_, ['ended', 'durationchange', 'timeupdate'], this.update);
  11949. if (this.player_.liveTracker) {
  11950. this.off(this.player_.liveTracker, 'liveedgechange', this.update);
  11951. }
  11952. this.off(this.player_, ['playing'], this.enableIntervalHandler_);
  11953. this.off(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
  11954. // we don't need to update the play progress if the document is hidden,
  11955. // also, this causes the CPU to spike and eventually crash the page on IE11.
  11956. if ('hidden' in document && 'visibilityState' in document) {
  11957. this.off(document, 'visibilitychange', this.toggleVisibility_);
  11958. }
  11959. super.dispose();
  11960. }
  11961. }
  11962. /**
  11963. * Default options for the `SeekBar`
  11964. *
  11965. * @type {Object}
  11966. * @private
  11967. */
  11968. SeekBar.prototype.options_ = {
  11969. children: ['loadProgressBar', 'playProgressBar'],
  11970. barName: 'playProgressBar'
  11971. };
  11972. // MouseTimeDisplay tooltips should not be added to a player on mobile devices
  11973. if (!IS_IOS && !IS_ANDROID) {
  11974. SeekBar.prototype.options_.children.splice(1, 0, 'mouseTimeDisplay');
  11975. }
  11976. Component.registerComponent('SeekBar', SeekBar);
  11977. /**
  11978. * @file progress-control.js
  11979. */
  11980. /**
  11981. * The Progress Control component contains the seek bar, load progress,
  11982. * and play progress.
  11983. *
  11984. * @extends Component
  11985. */
  11986. class ProgressControl extends Component {
  11987. /**
  11988. * Creates an instance of this class.
  11989. *
  11990. * @param { import('../../player').default } player
  11991. * The `Player` that this class should be attached to.
  11992. *
  11993. * @param {Object} [options]
  11994. * The key/value store of player options.
  11995. */
  11996. constructor(player, options) {
  11997. super(player, options);
  11998. this.handleMouseMove = throttle(bind_(this, this.handleMouseMove), UPDATE_REFRESH_INTERVAL);
  11999. this.throttledHandleMouseSeek = throttle(bind_(this, this.handleMouseSeek), UPDATE_REFRESH_INTERVAL);
  12000. this.handleMouseUpHandler_ = e => this.handleMouseUp(e);
  12001. this.handleMouseDownHandler_ = e => this.handleMouseDown(e);
  12002. this.enable();
  12003. }
  12004. /**
  12005. * Create the `Component`'s DOM element
  12006. *
  12007. * @return {Element}
  12008. * The element that was created.
  12009. */
  12010. createEl() {
  12011. return super.createEl('div', {
  12012. className: 'vjs-progress-control vjs-control'
  12013. });
  12014. }
  12015. /**
  12016. * When the mouse moves over the `ProgressControl`, the pointer position
  12017. * gets passed down to the `MouseTimeDisplay` component.
  12018. *
  12019. * @param {Event} event
  12020. * The `mousemove` event that caused this function to run.
  12021. *
  12022. * @listen mousemove
  12023. */
  12024. handleMouseMove(event) {
  12025. const seekBar = this.getChild('seekBar');
  12026. if (!seekBar) {
  12027. return;
  12028. }
  12029. const playProgressBar = seekBar.getChild('playProgressBar');
  12030. const mouseTimeDisplay = seekBar.getChild('mouseTimeDisplay');
  12031. if (!playProgressBar && !mouseTimeDisplay) {
  12032. return;
  12033. }
  12034. const seekBarEl = seekBar.el();
  12035. const seekBarRect = findPosition(seekBarEl);
  12036. let seekBarPoint = getPointerPosition(seekBarEl, event).x;
  12037. // The default skin has a gap on either side of the `SeekBar`. This means
  12038. // that it's possible to trigger this behavior outside the boundaries of
  12039. // the `SeekBar`. This ensures we stay within it at all times.
  12040. seekBarPoint = clamp(seekBarPoint, 0, 1);
  12041. if (mouseTimeDisplay) {
  12042. mouseTimeDisplay.update(seekBarRect, seekBarPoint);
  12043. }
  12044. if (playProgressBar) {
  12045. playProgressBar.update(seekBarRect, seekBar.getProgress());
  12046. }
  12047. }
  12048. /**
  12049. * A throttled version of the {@link ProgressControl#handleMouseSeek} listener.
  12050. *
  12051. * @method ProgressControl#throttledHandleMouseSeek
  12052. * @param {Event} event
  12053. * The `mousemove` event that caused this function to run.
  12054. *
  12055. * @listen mousemove
  12056. * @listen touchmove
  12057. */
  12058. /**
  12059. * Handle `mousemove` or `touchmove` events on the `ProgressControl`.
  12060. *
  12061. * @param {Event} event
  12062. * `mousedown` or `touchstart` event that triggered this function
  12063. *
  12064. * @listens mousemove
  12065. * @listens touchmove
  12066. */
  12067. handleMouseSeek(event) {
  12068. const seekBar = this.getChild('seekBar');
  12069. if (seekBar) {
  12070. seekBar.handleMouseMove(event);
  12071. }
  12072. }
  12073. /**
  12074. * Are controls are currently enabled for this progress control.
  12075. *
  12076. * @return {boolean}
  12077. * true if controls are enabled, false otherwise
  12078. */
  12079. enabled() {
  12080. return this.enabled_;
  12081. }
  12082. /**
  12083. * Disable all controls on the progress control and its children
  12084. */
  12085. disable() {
  12086. this.children().forEach(child => child.disable && child.disable());
  12087. if (!this.enabled()) {
  12088. return;
  12089. }
  12090. this.off(['mousedown', 'touchstart'], this.handleMouseDownHandler_);
  12091. this.off(this.el_, 'mousemove', this.handleMouseMove);
  12092. this.removeListenersAddedOnMousedownAndTouchstart();
  12093. this.addClass('disabled');
  12094. this.enabled_ = false;
  12095. // Restore normal playback state if controls are disabled while scrubbing
  12096. if (this.player_.scrubbing()) {
  12097. const seekBar = this.getChild('seekBar');
  12098. this.player_.scrubbing(false);
  12099. if (seekBar.videoWasPlaying) {
  12100. silencePromise(this.player_.play());
  12101. }
  12102. }
  12103. }
  12104. /**
  12105. * Enable all controls on the progress control and its children
  12106. */
  12107. enable() {
  12108. this.children().forEach(child => child.enable && child.enable());
  12109. if (this.enabled()) {
  12110. return;
  12111. }
  12112. this.on(['mousedown', 'touchstart'], this.handleMouseDownHandler_);
  12113. this.on(this.el_, 'mousemove', this.handleMouseMove);
  12114. this.removeClass('disabled');
  12115. this.enabled_ = true;
  12116. }
  12117. /**
  12118. * Cleanup listeners after the user finishes interacting with the progress controls
  12119. */
  12120. removeListenersAddedOnMousedownAndTouchstart() {
  12121. const doc = this.el_.ownerDocument;
  12122. this.off(doc, 'mousemove', this.throttledHandleMouseSeek);
  12123. this.off(doc, 'touchmove', this.throttledHandleMouseSeek);
  12124. this.off(doc, 'mouseup', this.handleMouseUpHandler_);
  12125. this.off(doc, 'touchend', this.handleMouseUpHandler_);
  12126. }
  12127. /**
  12128. * Handle `mousedown` or `touchstart` events on the `ProgressControl`.
  12129. *
  12130. * @param {Event} event
  12131. * `mousedown` or `touchstart` event that triggered this function
  12132. *
  12133. * @listens mousedown
  12134. * @listens touchstart
  12135. */
  12136. handleMouseDown(event) {
  12137. const doc = this.el_.ownerDocument;
  12138. const seekBar = this.getChild('seekBar');
  12139. if (seekBar) {
  12140. seekBar.handleMouseDown(event);
  12141. }
  12142. this.on(doc, 'mousemove', this.throttledHandleMouseSeek);
  12143. this.on(doc, 'touchmove', this.throttledHandleMouseSeek);
  12144. this.on(doc, 'mouseup', this.handleMouseUpHandler_);
  12145. this.on(doc, 'touchend', this.handleMouseUpHandler_);
  12146. }
  12147. /**
  12148. * Handle `mouseup` or `touchend` events on the `ProgressControl`.
  12149. *
  12150. * @param {Event} event
  12151. * `mouseup` or `touchend` event that triggered this function.
  12152. *
  12153. * @listens touchend
  12154. * @listens mouseup
  12155. */
  12156. handleMouseUp(event) {
  12157. const seekBar = this.getChild('seekBar');
  12158. if (seekBar) {
  12159. seekBar.handleMouseUp(event);
  12160. }
  12161. this.removeListenersAddedOnMousedownAndTouchstart();
  12162. }
  12163. }
  12164. /**
  12165. * Default options for `ProgressControl`
  12166. *
  12167. * @type {Object}
  12168. * @private
  12169. */
  12170. ProgressControl.prototype.options_ = {
  12171. children: ['seekBar']
  12172. };
  12173. Component.registerComponent('ProgressControl', ProgressControl);
  12174. /**
  12175. * @file picture-in-picture-toggle.js
  12176. */
  12177. /**
  12178. * Toggle Picture-in-Picture mode
  12179. *
  12180. * @extends Button
  12181. */
  12182. class PictureInPictureToggle extends Button {
  12183. /**
  12184. * Creates an instance of this class.
  12185. *
  12186. * @param { import('./player').default } player
  12187. * The `Player` that this class should be attached to.
  12188. *
  12189. * @param {Object} [options]
  12190. * The key/value store of player options.
  12191. *
  12192. * @listens Player#enterpictureinpicture
  12193. * @listens Player#leavepictureinpicture
  12194. */
  12195. constructor(player, options) {
  12196. super(player, options);
  12197. this.setIcon('picture-in-picture-enter');
  12198. this.on(player, ['enterpictureinpicture', 'leavepictureinpicture'], e => this.handlePictureInPictureChange(e));
  12199. this.on(player, ['disablepictureinpicturechanged', 'loadedmetadata'], e => this.handlePictureInPictureEnabledChange(e));
  12200. this.on(player, ['loadedmetadata', 'audioonlymodechange', 'audiopostermodechange'], () => this.handlePictureInPictureAudioModeChange());
  12201. // TODO: Deactivate button on player emptied event.
  12202. this.disable();
  12203. }
  12204. /**
  12205. * Builds the default DOM `className`.
  12206. *
  12207. * @return {string}
  12208. * The DOM `className` for this object.
  12209. */
  12210. buildCSSClass() {
  12211. return `vjs-picture-in-picture-control vjs-hidden ${super.buildCSSClass()}`;
  12212. }
  12213. /**
  12214. * Displays or hides the button depending on the audio mode detection.
  12215. * Exits picture-in-picture if it is enabled when switching to audio mode.
  12216. */
  12217. handlePictureInPictureAudioModeChange() {
  12218. // This audio detection will not detect HLS or DASH audio-only streams because there was no reliable way to detect them at the time
  12219. const isSourceAudio = this.player_.currentType().substring(0, 5) === 'audio';
  12220. const isAudioMode = isSourceAudio || this.player_.audioPosterMode() || this.player_.audioOnlyMode();
  12221. if (!isAudioMode) {
  12222. this.show();
  12223. return;
  12224. }
  12225. if (this.player_.isInPictureInPicture()) {
  12226. this.player_.exitPictureInPicture();
  12227. }
  12228. this.hide();
  12229. }
  12230. /**
  12231. * Enables or disables button based on availability of a Picture-In-Picture mode.
  12232. *
  12233. * Enabled if
  12234. * - `player.options().enableDocumentPictureInPicture` is true and
  12235. * window.documentPictureInPicture is available; or
  12236. * - `player.disablePictureInPicture()` is false and
  12237. * element.requestPictureInPicture is available
  12238. */
  12239. handlePictureInPictureEnabledChange() {
  12240. if (document.pictureInPictureEnabled && this.player_.disablePictureInPicture() === false || this.player_.options_.enableDocumentPictureInPicture && 'documentPictureInPicture' in window) {
  12241. this.enable();
  12242. } else {
  12243. this.disable();
  12244. }
  12245. }
  12246. /**
  12247. * Handles enterpictureinpicture and leavepictureinpicture on the player and change control text accordingly.
  12248. *
  12249. * @param {Event} [event]
  12250. * The {@link Player#enterpictureinpicture} or {@link Player#leavepictureinpicture} event that caused this function to be
  12251. * called.
  12252. *
  12253. * @listens Player#enterpictureinpicture
  12254. * @listens Player#leavepictureinpicture
  12255. */
  12256. handlePictureInPictureChange(event) {
  12257. if (this.player_.isInPictureInPicture()) {
  12258. this.setIcon('picture-in-picture-exit');
  12259. this.controlText('Exit Picture-in-Picture');
  12260. } else {
  12261. this.setIcon('picture-in-picture-enter');
  12262. this.controlText('Picture-in-Picture');
  12263. }
  12264. this.handlePictureInPictureEnabledChange();
  12265. }
  12266. /**
  12267. * This gets called when an `PictureInPictureToggle` is "clicked". See
  12268. * {@link ClickableComponent} for more detailed information on what a click can be.
  12269. *
  12270. * @param {Event} [event]
  12271. * The `keydown`, `tap`, or `click` event that caused this function to be
  12272. * called.
  12273. *
  12274. * @listens tap
  12275. * @listens click
  12276. */
  12277. handleClick(event) {
  12278. if (!this.player_.isInPictureInPicture()) {
  12279. this.player_.requestPictureInPicture();
  12280. } else {
  12281. this.player_.exitPictureInPicture();
  12282. }
  12283. }
  12284. /**
  12285. * Show the `Component`s element if it is hidden by removing the
  12286. * 'vjs-hidden' class name from it only in browsers that support the Picture-in-Picture API.
  12287. */
  12288. show() {
  12289. // Does not allow to display the pictureInPictureToggle in browsers that do not support the Picture-in-Picture API, e.g. Firefox.
  12290. if (typeof document.exitPictureInPicture !== 'function') {
  12291. return;
  12292. }
  12293. super.show();
  12294. }
  12295. }
  12296. /**
  12297. * The text that should display over the `PictureInPictureToggle`s controls. Added for localization.
  12298. *
  12299. * @type {string}
  12300. * @protected
  12301. */
  12302. PictureInPictureToggle.prototype.controlText_ = 'Picture-in-Picture';
  12303. Component.registerComponent('PictureInPictureToggle', PictureInPictureToggle);
  12304. /**
  12305. * @file fullscreen-toggle.js
  12306. */
  12307. /**
  12308. * Toggle fullscreen video
  12309. *
  12310. * @extends Button
  12311. */
  12312. class FullscreenToggle extends Button {
  12313. /**
  12314. * Creates an instance of this class.
  12315. *
  12316. * @param { import('./player').default } player
  12317. * The `Player` that this class should be attached to.
  12318. *
  12319. * @param {Object} [options]
  12320. * The key/value store of player options.
  12321. */
  12322. constructor(player, options) {
  12323. super(player, options);
  12324. this.setIcon('fullscreen-enter');
  12325. this.on(player, 'fullscreenchange', e => this.handleFullscreenChange(e));
  12326. if (document[player.fsApi_.fullscreenEnabled] === false) {
  12327. this.disable();
  12328. }
  12329. }
  12330. /**
  12331. * Builds the default DOM `className`.
  12332. *
  12333. * @return {string}
  12334. * The DOM `className` for this object.
  12335. */
  12336. buildCSSClass() {
  12337. return `vjs-fullscreen-control ${super.buildCSSClass()}`;
  12338. }
  12339. /**
  12340. * Handles fullscreenchange on the player and change control text accordingly.
  12341. *
  12342. * @param {Event} [event]
  12343. * The {@link Player#fullscreenchange} event that caused this function to be
  12344. * called.
  12345. *
  12346. * @listens Player#fullscreenchange
  12347. */
  12348. handleFullscreenChange(event) {
  12349. if (this.player_.isFullscreen()) {
  12350. this.controlText('Exit Fullscreen');
  12351. this.setIcon('fullscreen-exit');
  12352. } else {
  12353. this.controlText('Fullscreen');
  12354. this.setIcon('fullscreen-enter');
  12355. }
  12356. }
  12357. /**
  12358. * This gets called when an `FullscreenToggle` is "clicked". See
  12359. * {@link ClickableComponent} for more detailed information on what a click can be.
  12360. *
  12361. * @param {Event} [event]
  12362. * The `keydown`, `tap`, or `click` event that caused this function to be
  12363. * called.
  12364. *
  12365. * @listens tap
  12366. * @listens click
  12367. */
  12368. handleClick(event) {
  12369. if (!this.player_.isFullscreen()) {
  12370. this.player_.requestFullscreen();
  12371. } else {
  12372. this.player_.exitFullscreen();
  12373. }
  12374. }
  12375. }
  12376. /**
  12377. * The text that should display over the `FullscreenToggle`s controls. Added for localization.
  12378. *
  12379. * @type {string}
  12380. * @protected
  12381. */
  12382. FullscreenToggle.prototype.controlText_ = 'Fullscreen';
  12383. Component.registerComponent('FullscreenToggle', FullscreenToggle);
  12384. /**
  12385. * Check if volume control is supported and if it isn't hide the
  12386. * `Component` that was passed using the `vjs-hidden` class.
  12387. *
  12388. * @param { import('../../component').default } self
  12389. * The component that should be hidden if volume is unsupported
  12390. *
  12391. * @param { import('../../player').default } player
  12392. * A reference to the player
  12393. *
  12394. * @private
  12395. */
  12396. const checkVolumeSupport = function (self, player) {
  12397. // hide volume controls when they're not supported by the current tech
  12398. if (player.tech_ && !player.tech_.featuresVolumeControl) {
  12399. self.addClass('vjs-hidden');
  12400. }
  12401. self.on(player, 'loadstart', function () {
  12402. if (!player.tech_.featuresVolumeControl) {
  12403. self.addClass('vjs-hidden');
  12404. } else {
  12405. self.removeClass('vjs-hidden');
  12406. }
  12407. });
  12408. };
  12409. /**
  12410. * @file volume-level.js
  12411. */
  12412. /**
  12413. * Shows volume level
  12414. *
  12415. * @extends Component
  12416. */
  12417. class VolumeLevel extends Component {
  12418. /**
  12419. * Create the `Component`'s DOM element
  12420. *
  12421. * @return {Element}
  12422. * The element that was created.
  12423. */
  12424. createEl() {
  12425. const el = super.createEl('div', {
  12426. className: 'vjs-volume-level'
  12427. });
  12428. this.setIcon('circle', el);
  12429. el.appendChild(super.createEl('span', {
  12430. className: 'vjs-control-text'
  12431. }));
  12432. return el;
  12433. }
  12434. }
  12435. Component.registerComponent('VolumeLevel', VolumeLevel);
  12436. /**
  12437. * @file volume-level-tooltip.js
  12438. */
  12439. /**
  12440. * Volume level tooltips display a volume above or side by side the volume bar.
  12441. *
  12442. * @extends Component
  12443. */
  12444. class VolumeLevelTooltip extends Component {
  12445. /**
  12446. * Creates an instance of this class.
  12447. *
  12448. * @param { import('../../player').default } player
  12449. * The {@link Player} that this class should be attached to.
  12450. *
  12451. * @param {Object} [options]
  12452. * The key/value store of player options.
  12453. */
  12454. constructor(player, options) {
  12455. super(player, options);
  12456. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  12457. }
  12458. /**
  12459. * Create the volume tooltip DOM element
  12460. *
  12461. * @return {Element}
  12462. * The element that was created.
  12463. */
  12464. createEl() {
  12465. return super.createEl('div', {
  12466. className: 'vjs-volume-tooltip'
  12467. }, {
  12468. 'aria-hidden': 'true'
  12469. });
  12470. }
  12471. /**
  12472. * Updates the position of the tooltip relative to the `VolumeBar` and
  12473. * its content text.
  12474. *
  12475. * @param {Object} rangeBarRect
  12476. * The `ClientRect` for the {@link VolumeBar} element.
  12477. *
  12478. * @param {number} rangeBarPoint
  12479. * A number from 0 to 1, representing a horizontal/vertical reference point
  12480. * from the left edge of the {@link VolumeBar}
  12481. *
  12482. * @param {boolean} vertical
  12483. * Referees to the Volume control position
  12484. * in the control bar{@link VolumeControl}
  12485. *
  12486. */
  12487. update(rangeBarRect, rangeBarPoint, vertical, content) {
  12488. if (!vertical) {
  12489. const tooltipRect = getBoundingClientRect(this.el_);
  12490. const playerRect = getBoundingClientRect(this.player_.el());
  12491. const volumeBarPointPx = rangeBarRect.width * rangeBarPoint;
  12492. if (!playerRect || !tooltipRect) {
  12493. return;
  12494. }
  12495. const spaceLeftOfPoint = rangeBarRect.left - playerRect.left + volumeBarPointPx;
  12496. const spaceRightOfPoint = rangeBarRect.width - volumeBarPointPx + (playerRect.right - rangeBarRect.right);
  12497. let pullTooltipBy = tooltipRect.width / 2;
  12498. if (spaceLeftOfPoint < pullTooltipBy) {
  12499. pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
  12500. } else if (spaceRightOfPoint < pullTooltipBy) {
  12501. pullTooltipBy = spaceRightOfPoint;
  12502. }
  12503. if (pullTooltipBy < 0) {
  12504. pullTooltipBy = 0;
  12505. } else if (pullTooltipBy > tooltipRect.width) {
  12506. pullTooltipBy = tooltipRect.width;
  12507. }
  12508. this.el_.style.right = `-${pullTooltipBy}px`;
  12509. }
  12510. this.write(`${content}%`);
  12511. }
  12512. /**
  12513. * Write the volume to the tooltip DOM element.
  12514. *
  12515. * @param {string} content
  12516. * The formatted volume for the tooltip.
  12517. */
  12518. write(content) {
  12519. textContent(this.el_, content);
  12520. }
  12521. /**
  12522. * Updates the position of the volume tooltip relative to the `VolumeBar`.
  12523. *
  12524. * @param {Object} rangeBarRect
  12525. * The `ClientRect` for the {@link VolumeBar} element.
  12526. *
  12527. * @param {number} rangeBarPoint
  12528. * A number from 0 to 1, representing a horizontal/vertical reference point
  12529. * from the left edge of the {@link VolumeBar}
  12530. *
  12531. * @param {boolean} vertical
  12532. * Referees to the Volume control position
  12533. * in the control bar{@link VolumeControl}
  12534. *
  12535. * @param {number} volume
  12536. * The volume level to update the tooltip to
  12537. *
  12538. * @param {Function} cb
  12539. * A function that will be called during the request animation frame
  12540. * for tooltips that need to do additional animations from the default
  12541. */
  12542. updateVolume(rangeBarRect, rangeBarPoint, vertical, volume, cb) {
  12543. this.requestNamedAnimationFrame('VolumeLevelTooltip#updateVolume', () => {
  12544. this.update(rangeBarRect, rangeBarPoint, vertical, volume.toFixed(0));
  12545. if (cb) {
  12546. cb();
  12547. }
  12548. });
  12549. }
  12550. }
  12551. Component.registerComponent('VolumeLevelTooltip', VolumeLevelTooltip);
  12552. /**
  12553. * @file mouse-volume-level-display.js
  12554. */
  12555. /**
  12556. * The {@link MouseVolumeLevelDisplay} component tracks mouse movement over the
  12557. * {@link VolumeControl}. It displays an indicator and a {@link VolumeLevelTooltip}
  12558. * indicating the volume level which is represented by a given point in the
  12559. * {@link VolumeBar}.
  12560. *
  12561. * @extends Component
  12562. */
  12563. class MouseVolumeLevelDisplay extends Component {
  12564. /**
  12565. * Creates an instance of this class.
  12566. *
  12567. * @param { import('../../player').default } player
  12568. * The {@link Player} that this class should be attached to.
  12569. *
  12570. * @param {Object} [options]
  12571. * The key/value store of player options.
  12572. */
  12573. constructor(player, options) {
  12574. super(player, options);
  12575. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  12576. }
  12577. /**
  12578. * Create the DOM element for this class.
  12579. *
  12580. * @return {Element}
  12581. * The element that was created.
  12582. */
  12583. createEl() {
  12584. return super.createEl('div', {
  12585. className: 'vjs-mouse-display'
  12586. });
  12587. }
  12588. /**
  12589. * Enquires updates to its own DOM as well as the DOM of its
  12590. * {@link VolumeLevelTooltip} child.
  12591. *
  12592. * @param {Object} rangeBarRect
  12593. * The `ClientRect` for the {@link VolumeBar} element.
  12594. *
  12595. * @param {number} rangeBarPoint
  12596. * A number from 0 to 1, representing a horizontal/vertical reference point
  12597. * from the left edge of the {@link VolumeBar}
  12598. *
  12599. * @param {boolean} vertical
  12600. * Referees to the Volume control position
  12601. * in the control bar{@link VolumeControl}
  12602. *
  12603. */
  12604. update(rangeBarRect, rangeBarPoint, vertical) {
  12605. const volume = 100 * rangeBarPoint;
  12606. this.getChild('volumeLevelTooltip').updateVolume(rangeBarRect, rangeBarPoint, vertical, volume, () => {
  12607. if (vertical) {
  12608. this.el_.style.bottom = `${rangeBarRect.height * rangeBarPoint}px`;
  12609. } else {
  12610. this.el_.style.left = `${rangeBarRect.width * rangeBarPoint}px`;
  12611. }
  12612. });
  12613. }
  12614. }
  12615. /**
  12616. * Default options for `MouseVolumeLevelDisplay`
  12617. *
  12618. * @type {Object}
  12619. * @private
  12620. */
  12621. MouseVolumeLevelDisplay.prototype.options_ = {
  12622. children: ['volumeLevelTooltip']
  12623. };
  12624. Component.registerComponent('MouseVolumeLevelDisplay', MouseVolumeLevelDisplay);
  12625. /**
  12626. * @file volume-bar.js
  12627. */
  12628. /**
  12629. * The bar that contains the volume level and can be clicked on to adjust the level
  12630. *
  12631. * @extends Slider
  12632. */
  12633. class VolumeBar extends Slider {
  12634. /**
  12635. * Creates an instance of this class.
  12636. *
  12637. * @param { import('../../player').default } player
  12638. * The `Player` that this class should be attached to.
  12639. *
  12640. * @param {Object} [options]
  12641. * The key/value store of player options.
  12642. */
  12643. constructor(player, options) {
  12644. super(player, options);
  12645. this.on('slideractive', e => this.updateLastVolume_(e));
  12646. this.on(player, 'volumechange', e => this.updateARIAAttributes(e));
  12647. player.ready(() => this.updateARIAAttributes());
  12648. }
  12649. /**
  12650. * Create the `Component`'s DOM element
  12651. *
  12652. * @return {Element}
  12653. * The element that was created.
  12654. */
  12655. createEl() {
  12656. return super.createEl('div', {
  12657. className: 'vjs-volume-bar vjs-slider-bar'
  12658. }, {
  12659. 'aria-label': this.localize('Volume Level'),
  12660. 'aria-live': 'polite'
  12661. });
  12662. }
  12663. /**
  12664. * Handle mouse down on volume bar
  12665. *
  12666. * @param {Event} event
  12667. * The `mousedown` event that caused this to run.
  12668. *
  12669. * @listens mousedown
  12670. */
  12671. handleMouseDown(event) {
  12672. if (!isSingleLeftClick(event)) {
  12673. return;
  12674. }
  12675. super.handleMouseDown(event);
  12676. }
  12677. /**
  12678. * Handle movement events on the {@link VolumeMenuButton}.
  12679. *
  12680. * @param {Event} event
  12681. * The event that caused this function to run.
  12682. *
  12683. * @listens mousemove
  12684. */
  12685. handleMouseMove(event) {
  12686. const mouseVolumeLevelDisplay = this.getChild('mouseVolumeLevelDisplay');
  12687. if (mouseVolumeLevelDisplay) {
  12688. const volumeBarEl = this.el();
  12689. const volumeBarRect = getBoundingClientRect(volumeBarEl);
  12690. const vertical = this.vertical();
  12691. let volumeBarPoint = getPointerPosition(volumeBarEl, event);
  12692. volumeBarPoint = vertical ? volumeBarPoint.y : volumeBarPoint.x;
  12693. // The default skin has a gap on either side of the `VolumeBar`. This means
  12694. // that it's possible to trigger this behavior outside the boundaries of
  12695. // the `VolumeBar`. This ensures we stay within it at all times.
  12696. volumeBarPoint = clamp(volumeBarPoint, 0, 1);
  12697. mouseVolumeLevelDisplay.update(volumeBarRect, volumeBarPoint, vertical);
  12698. }
  12699. if (!isSingleLeftClick(event)) {
  12700. return;
  12701. }
  12702. this.checkMuted();
  12703. this.player_.volume(this.calculateDistance(event));
  12704. }
  12705. /**
  12706. * If the player is muted unmute it.
  12707. */
  12708. checkMuted() {
  12709. if (this.player_.muted()) {
  12710. this.player_.muted(false);
  12711. }
  12712. }
  12713. /**
  12714. * Get percent of volume level
  12715. *
  12716. * @return {number}
  12717. * Volume level percent as a decimal number.
  12718. */
  12719. getPercent() {
  12720. if (this.player_.muted()) {
  12721. return 0;
  12722. }
  12723. return this.player_.volume();
  12724. }
  12725. /**
  12726. * Increase volume level for keyboard users
  12727. */
  12728. stepForward() {
  12729. this.checkMuted();
  12730. this.player_.volume(this.player_.volume() + 0.1);
  12731. }
  12732. /**
  12733. * Decrease volume level for keyboard users
  12734. */
  12735. stepBack() {
  12736. this.checkMuted();
  12737. this.player_.volume(this.player_.volume() - 0.1);
  12738. }
  12739. /**
  12740. * Update ARIA accessibility attributes
  12741. *
  12742. * @param {Event} [event]
  12743. * The `volumechange` event that caused this function to run.
  12744. *
  12745. * @listens Player#volumechange
  12746. */
  12747. updateARIAAttributes(event) {
  12748. const ariaValue = this.player_.muted() ? 0 : this.volumeAsPercentage_();
  12749. this.el_.setAttribute('aria-valuenow', ariaValue);
  12750. this.el_.setAttribute('aria-valuetext', ariaValue + '%');
  12751. }
  12752. /**
  12753. * Returns the current value of the player volume as a percentage
  12754. *
  12755. * @private
  12756. */
  12757. volumeAsPercentage_() {
  12758. return Math.round(this.player_.volume() * 100);
  12759. }
  12760. /**
  12761. * When user starts dragging the VolumeBar, store the volume and listen for
  12762. * the end of the drag. When the drag ends, if the volume was set to zero,
  12763. * set lastVolume to the stored volume.
  12764. *
  12765. * @listens slideractive
  12766. * @private
  12767. */
  12768. updateLastVolume_() {
  12769. const volumeBeforeDrag = this.player_.volume();
  12770. this.one('sliderinactive', () => {
  12771. if (this.player_.volume() === 0) {
  12772. this.player_.lastVolume_(volumeBeforeDrag);
  12773. }
  12774. });
  12775. }
  12776. }
  12777. /**
  12778. * Default options for the `VolumeBar`
  12779. *
  12780. * @type {Object}
  12781. * @private
  12782. */
  12783. VolumeBar.prototype.options_ = {
  12784. children: ['volumeLevel'],
  12785. barName: 'volumeLevel'
  12786. };
  12787. // MouseVolumeLevelDisplay tooltip should not be added to a player on mobile devices
  12788. if (!IS_IOS && !IS_ANDROID) {
  12789. VolumeBar.prototype.options_.children.splice(0, 0, 'mouseVolumeLevelDisplay');
  12790. }
  12791. /**
  12792. * Call the update event for this Slider when this event happens on the player.
  12793. *
  12794. * @type {string}
  12795. */
  12796. VolumeBar.prototype.playerEvent = 'volumechange';
  12797. Component.registerComponent('VolumeBar', VolumeBar);
  12798. /**
  12799. * @file volume-control.js
  12800. */
  12801. /**
  12802. * The component for controlling the volume level
  12803. *
  12804. * @extends Component
  12805. */
  12806. class VolumeControl extends Component {
  12807. /**
  12808. * Creates an instance of this class.
  12809. *
  12810. * @param { import('../../player').default } player
  12811. * The `Player` that this class should be attached to.
  12812. *
  12813. * @param {Object} [options={}]
  12814. * The key/value store of player options.
  12815. */
  12816. constructor(player, options = {}) {
  12817. options.vertical = options.vertical || false;
  12818. // Pass the vertical option down to the VolumeBar if
  12819. // the VolumeBar is turned on.
  12820. if (typeof options.volumeBar === 'undefined' || isPlain(options.volumeBar)) {
  12821. options.volumeBar = options.volumeBar || {};
  12822. options.volumeBar.vertical = options.vertical;
  12823. }
  12824. super(player, options);
  12825. // hide this control if volume support is missing
  12826. checkVolumeSupport(this, player);
  12827. this.throttledHandleMouseMove = throttle(bind_(this, this.handleMouseMove), UPDATE_REFRESH_INTERVAL);
  12828. this.handleMouseUpHandler_ = e => this.handleMouseUp(e);
  12829. this.on('mousedown', e => this.handleMouseDown(e));
  12830. this.on('touchstart', e => this.handleMouseDown(e));
  12831. this.on('mousemove', e => this.handleMouseMove(e));
  12832. // while the slider is active (the mouse has been pressed down and
  12833. // is dragging) or in focus we do not want to hide the VolumeBar
  12834. this.on(this.volumeBar, ['focus', 'slideractive'], () => {
  12835. this.volumeBar.addClass('vjs-slider-active');
  12836. this.addClass('vjs-slider-active');
  12837. this.trigger('slideractive');
  12838. });
  12839. this.on(this.volumeBar, ['blur', 'sliderinactive'], () => {
  12840. this.volumeBar.removeClass('vjs-slider-active');
  12841. this.removeClass('vjs-slider-active');
  12842. this.trigger('sliderinactive');
  12843. });
  12844. }
  12845. /**
  12846. * Create the `Component`'s DOM element
  12847. *
  12848. * @return {Element}
  12849. * The element that was created.
  12850. */
  12851. createEl() {
  12852. let orientationClass = 'vjs-volume-horizontal';
  12853. if (this.options_.vertical) {
  12854. orientationClass = 'vjs-volume-vertical';
  12855. }
  12856. return super.createEl('div', {
  12857. className: `vjs-volume-control vjs-control ${orientationClass}`
  12858. });
  12859. }
  12860. /**
  12861. * Handle `mousedown` or `touchstart` events on the `VolumeControl`.
  12862. *
  12863. * @param {Event} event
  12864. * `mousedown` or `touchstart` event that triggered this function
  12865. *
  12866. * @listens mousedown
  12867. * @listens touchstart
  12868. */
  12869. handleMouseDown(event) {
  12870. const doc = this.el_.ownerDocument;
  12871. this.on(doc, 'mousemove', this.throttledHandleMouseMove);
  12872. this.on(doc, 'touchmove', this.throttledHandleMouseMove);
  12873. this.on(doc, 'mouseup', this.handleMouseUpHandler_);
  12874. this.on(doc, 'touchend', this.handleMouseUpHandler_);
  12875. }
  12876. /**
  12877. * Handle `mouseup` or `touchend` events on the `VolumeControl`.
  12878. *
  12879. * @param {Event} event
  12880. * `mouseup` or `touchend` event that triggered this function.
  12881. *
  12882. * @listens touchend
  12883. * @listens mouseup
  12884. */
  12885. handleMouseUp(event) {
  12886. const doc = this.el_.ownerDocument;
  12887. this.off(doc, 'mousemove', this.throttledHandleMouseMove);
  12888. this.off(doc, 'touchmove', this.throttledHandleMouseMove);
  12889. this.off(doc, 'mouseup', this.handleMouseUpHandler_);
  12890. this.off(doc, 'touchend', this.handleMouseUpHandler_);
  12891. }
  12892. /**
  12893. * Handle `mousedown` or `touchstart` events on the `VolumeControl`.
  12894. *
  12895. * @param {Event} event
  12896. * `mousedown` or `touchstart` event that triggered this function
  12897. *
  12898. * @listens mousedown
  12899. * @listens touchstart
  12900. */
  12901. handleMouseMove(event) {
  12902. this.volumeBar.handleMouseMove(event);
  12903. }
  12904. }
  12905. /**
  12906. * Default options for the `VolumeControl`
  12907. *
  12908. * @type {Object}
  12909. * @private
  12910. */
  12911. VolumeControl.prototype.options_ = {
  12912. children: ['volumeBar']
  12913. };
  12914. Component.registerComponent('VolumeControl', VolumeControl);
  12915. /**
  12916. * Check if muting volume is supported and if it isn't hide the mute toggle
  12917. * button.
  12918. *
  12919. * @param { import('../../component').default } self
  12920. * A reference to the mute toggle button
  12921. *
  12922. * @param { import('../../player').default } player
  12923. * A reference to the player
  12924. *
  12925. * @private
  12926. */
  12927. const checkMuteSupport = function (self, player) {
  12928. // hide mute toggle button if it's not supported by the current tech
  12929. if (player.tech_ && !player.tech_.featuresMuteControl) {
  12930. self.addClass('vjs-hidden');
  12931. }
  12932. self.on(player, 'loadstart', function () {
  12933. if (!player.tech_.featuresMuteControl) {
  12934. self.addClass('vjs-hidden');
  12935. } else {
  12936. self.removeClass('vjs-hidden');
  12937. }
  12938. });
  12939. };
  12940. /**
  12941. * @file mute-toggle.js
  12942. */
  12943. /**
  12944. * A button component for muting the audio.
  12945. *
  12946. * @extends Button
  12947. */
  12948. class MuteToggle extends Button {
  12949. /**
  12950. * Creates an instance of this class.
  12951. *
  12952. * @param { import('./player').default } player
  12953. * The `Player` that this class should be attached to.
  12954. *
  12955. * @param {Object} [options]
  12956. * The key/value store of player options.
  12957. */
  12958. constructor(player, options) {
  12959. super(player, options);
  12960. // hide this control if volume support is missing
  12961. checkMuteSupport(this, player);
  12962. this.on(player, ['loadstart', 'volumechange'], e => this.update(e));
  12963. }
  12964. /**
  12965. * Builds the default DOM `className`.
  12966. *
  12967. * @return {string}
  12968. * The DOM `className` for this object.
  12969. */
  12970. buildCSSClass() {
  12971. return `vjs-mute-control ${super.buildCSSClass()}`;
  12972. }
  12973. /**
  12974. * This gets called when an `MuteToggle` is "clicked". See
  12975. * {@link ClickableComponent} for more detailed information on what a click can be.
  12976. *
  12977. * @param {Event} [event]
  12978. * The `keydown`, `tap`, or `click` event that caused this function to be
  12979. * called.
  12980. *
  12981. * @listens tap
  12982. * @listens click
  12983. */
  12984. handleClick(event) {
  12985. const vol = this.player_.volume();
  12986. const lastVolume = this.player_.lastVolume_();
  12987. if (vol === 0) {
  12988. const volumeToSet = lastVolume < 0.1 ? 0.1 : lastVolume;
  12989. this.player_.volume(volumeToSet);
  12990. this.player_.muted(false);
  12991. } else {
  12992. this.player_.muted(this.player_.muted() ? false : true);
  12993. }
  12994. }
  12995. /**
  12996. * Update the `MuteToggle` button based on the state of `volume` and `muted`
  12997. * on the player.
  12998. *
  12999. * @param {Event} [event]
  13000. * The {@link Player#loadstart} event if this function was called
  13001. * through an event.
  13002. *
  13003. * @listens Player#loadstart
  13004. * @listens Player#volumechange
  13005. */
  13006. update(event) {
  13007. this.updateIcon_();
  13008. this.updateControlText_();
  13009. }
  13010. /**
  13011. * Update the appearance of the `MuteToggle` icon.
  13012. *
  13013. * Possible states (given `level` variable below):
  13014. * - 0: crossed out
  13015. * - 1: zero bars of volume
  13016. * - 2: one bar of volume
  13017. * - 3: two bars of volume
  13018. *
  13019. * @private
  13020. */
  13021. updateIcon_() {
  13022. const vol = this.player_.volume();
  13023. let level = 3;
  13024. this.setIcon('volume-high');
  13025. // in iOS when a player is loaded with muted attribute
  13026. // and volume is changed with a native mute button
  13027. // we want to make sure muted state is updated
  13028. if (IS_IOS && this.player_.tech_ && this.player_.tech_.el_) {
  13029. this.player_.muted(this.player_.tech_.el_.muted);
  13030. }
  13031. if (vol === 0 || this.player_.muted()) {
  13032. this.setIcon('volume-mute');
  13033. level = 0;
  13034. } else if (vol < 0.33) {
  13035. this.setIcon('volume-low');
  13036. level = 1;
  13037. } else if (vol < 0.67) {
  13038. this.setIcon('volume-medium');
  13039. level = 2;
  13040. }
  13041. removeClass(this.el_, [0, 1, 2, 3].reduce((str, i) => str + `${i ? ' ' : ''}vjs-vol-${i}`, ''));
  13042. addClass(this.el_, `vjs-vol-${level}`);
  13043. }
  13044. /**
  13045. * If `muted` has changed on the player, update the control text
  13046. * (`title` attribute on `vjs-mute-control` element and content of
  13047. * `vjs-control-text` element).
  13048. *
  13049. * @private
  13050. */
  13051. updateControlText_() {
  13052. const soundOff = this.player_.muted() || this.player_.volume() === 0;
  13053. const text = soundOff ? 'Unmute' : 'Mute';
  13054. if (this.controlText() !== text) {
  13055. this.controlText(text);
  13056. }
  13057. }
  13058. }
  13059. /**
  13060. * The text that should display over the `MuteToggle`s controls. Added for localization.
  13061. *
  13062. * @type {string}
  13063. * @protected
  13064. */
  13065. MuteToggle.prototype.controlText_ = 'Mute';
  13066. Component.registerComponent('MuteToggle', MuteToggle);
  13067. /**
  13068. * @file volume-control.js
  13069. */
  13070. /**
  13071. * A Component to contain the MuteToggle and VolumeControl so that
  13072. * they can work together.
  13073. *
  13074. * @extends Component
  13075. */
  13076. class VolumePanel extends Component {
  13077. /**
  13078. * Creates an instance of this class.
  13079. *
  13080. * @param { import('./player').default } player
  13081. * The `Player` that this class should be attached to.
  13082. *
  13083. * @param {Object} [options={}]
  13084. * The key/value store of player options.
  13085. */
  13086. constructor(player, options = {}) {
  13087. if (typeof options.inline !== 'undefined') {
  13088. options.inline = options.inline;
  13089. } else {
  13090. options.inline = true;
  13091. }
  13092. // pass the inline option down to the VolumeControl as vertical if
  13093. // the VolumeControl is on.
  13094. if (typeof options.volumeControl === 'undefined' || isPlain(options.volumeControl)) {
  13095. options.volumeControl = options.volumeControl || {};
  13096. options.volumeControl.vertical = !options.inline;
  13097. }
  13098. super(player, options);
  13099. // this handler is used by mouse handler methods below
  13100. this.handleKeyPressHandler_ = e => this.handleKeyPress(e);
  13101. this.on(player, ['loadstart'], e => this.volumePanelState_(e));
  13102. this.on(this.muteToggle, 'keyup', e => this.handleKeyPress(e));
  13103. this.on(this.volumeControl, 'keyup', e => this.handleVolumeControlKeyUp(e));
  13104. this.on('keydown', e => this.handleKeyPress(e));
  13105. this.on('mouseover', e => this.handleMouseOver(e));
  13106. this.on('mouseout', e => this.handleMouseOut(e));
  13107. // while the slider is active (the mouse has been pressed down and
  13108. // is dragging) we do not want to hide the VolumeBar
  13109. this.on(this.volumeControl, ['slideractive'], this.sliderActive_);
  13110. this.on(this.volumeControl, ['sliderinactive'], this.sliderInactive_);
  13111. }
  13112. /**
  13113. * Add vjs-slider-active class to the VolumePanel
  13114. *
  13115. * @listens VolumeControl#slideractive
  13116. * @private
  13117. */
  13118. sliderActive_() {
  13119. this.addClass('vjs-slider-active');
  13120. }
  13121. /**
  13122. * Removes vjs-slider-active class to the VolumePanel
  13123. *
  13124. * @listens VolumeControl#sliderinactive
  13125. * @private
  13126. */
  13127. sliderInactive_() {
  13128. this.removeClass('vjs-slider-active');
  13129. }
  13130. /**
  13131. * Adds vjs-hidden or vjs-mute-toggle-only to the VolumePanel
  13132. * depending on MuteToggle and VolumeControl state
  13133. *
  13134. * @listens Player#loadstart
  13135. * @private
  13136. */
  13137. volumePanelState_() {
  13138. // hide volume panel if neither volume control or mute toggle
  13139. // are displayed
  13140. if (this.volumeControl.hasClass('vjs-hidden') && this.muteToggle.hasClass('vjs-hidden')) {
  13141. this.addClass('vjs-hidden');
  13142. }
  13143. // if only mute toggle is visible we don't want
  13144. // volume panel expanding when hovered or active
  13145. if (this.volumeControl.hasClass('vjs-hidden') && !this.muteToggle.hasClass('vjs-hidden')) {
  13146. this.addClass('vjs-mute-toggle-only');
  13147. }
  13148. }
  13149. /**
  13150. * Create the `Component`'s DOM element
  13151. *
  13152. * @return {Element}
  13153. * The element that was created.
  13154. */
  13155. createEl() {
  13156. let orientationClass = 'vjs-volume-panel-horizontal';
  13157. if (!this.options_.inline) {
  13158. orientationClass = 'vjs-volume-panel-vertical';
  13159. }
  13160. return super.createEl('div', {
  13161. className: `vjs-volume-panel vjs-control ${orientationClass}`
  13162. });
  13163. }
  13164. /**
  13165. * Dispose of the `volume-panel` and all child components.
  13166. */
  13167. dispose() {
  13168. this.handleMouseOut();
  13169. super.dispose();
  13170. }
  13171. /**
  13172. * Handles `keyup` events on the `VolumeControl`, looking for ESC, which closes
  13173. * the volume panel and sets focus on `MuteToggle`.
  13174. *
  13175. * @param {Event} event
  13176. * The `keyup` event that caused this function to be called.
  13177. *
  13178. * @listens keyup
  13179. */
  13180. handleVolumeControlKeyUp(event) {
  13181. if (keycode.isEventKey(event, 'Esc')) {
  13182. this.muteToggle.focus();
  13183. }
  13184. }
  13185. /**
  13186. * This gets called when a `VolumePanel` gains hover via a `mouseover` event.
  13187. * Turns on listening for `mouseover` event. When they happen it
  13188. * calls `this.handleMouseOver`.
  13189. *
  13190. * @param {Event} event
  13191. * The `mouseover` event that caused this function to be called.
  13192. *
  13193. * @listens mouseover
  13194. */
  13195. handleMouseOver(event) {
  13196. this.addClass('vjs-hover');
  13197. on(document, 'keyup', this.handleKeyPressHandler_);
  13198. }
  13199. /**
  13200. * This gets called when a `VolumePanel` gains hover via a `mouseout` event.
  13201. * Turns on listening for `mouseout` event. When they happen it
  13202. * calls `this.handleMouseOut`.
  13203. *
  13204. * @param {Event} event
  13205. * The `mouseout` event that caused this function to be called.
  13206. *
  13207. * @listens mouseout
  13208. */
  13209. handleMouseOut(event) {
  13210. this.removeClass('vjs-hover');
  13211. off(document, 'keyup', this.handleKeyPressHandler_);
  13212. }
  13213. /**
  13214. * Handles `keyup` event on the document or `keydown` event on the `VolumePanel`,
  13215. * looking for ESC, which hides the `VolumeControl`.
  13216. *
  13217. * @param {Event} event
  13218. * The keypress that triggered this event.
  13219. *
  13220. * @listens keydown | keyup
  13221. */
  13222. handleKeyPress(event) {
  13223. if (keycode.isEventKey(event, 'Esc')) {
  13224. this.handleMouseOut();
  13225. }
  13226. }
  13227. }
  13228. /**
  13229. * Default options for the `VolumeControl`
  13230. *
  13231. * @type {Object}
  13232. * @private
  13233. */
  13234. VolumePanel.prototype.options_ = {
  13235. children: ['muteToggle', 'volumeControl']
  13236. };
  13237. Component.registerComponent('VolumePanel', VolumePanel);
  13238. /**
  13239. * Button to skip forward a configurable amount of time
  13240. * through a video. Renders in the control bar.
  13241. *
  13242. * e.g. options: {controlBar: {skipButtons: forward: 5}}
  13243. *
  13244. * @extends Button
  13245. */
  13246. class SkipForward extends Button {
  13247. constructor(player, options) {
  13248. super(player, options);
  13249. this.validOptions = [5, 10, 30];
  13250. this.skipTime = this.getSkipForwardTime();
  13251. if (this.skipTime && this.validOptions.includes(this.skipTime)) {
  13252. this.setIcon(`forward-${this.skipTime}`);
  13253. this.controlText(this.localize('Skip forward {1} seconds', [this.skipTime]));
  13254. this.show();
  13255. } else {
  13256. this.hide();
  13257. }
  13258. }
  13259. getSkipForwardTime() {
  13260. const playerOptions = this.options_.playerOptions;
  13261. return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.forward;
  13262. }
  13263. buildCSSClass() {
  13264. return `vjs-skip-forward-${this.getSkipForwardTime()} ${super.buildCSSClass()}`;
  13265. }
  13266. /**
  13267. * On click, skips forward in the duration/seekable range by a configurable amount of seconds.
  13268. * If the time left in the duration/seekable range is less than the configured 'skip forward' time,
  13269. * skips to end of duration/seekable range.
  13270. *
  13271. * Handle a click on a `SkipForward` button
  13272. *
  13273. * @param {EventTarget~Event} event
  13274. * The `click` event that caused this function
  13275. * to be called
  13276. */
  13277. handleClick(event) {
  13278. if (isNaN(this.player_.duration())) {
  13279. return;
  13280. }
  13281. const currentVideoTime = this.player_.currentTime();
  13282. const liveTracker = this.player_.liveTracker;
  13283. const duration = liveTracker && liveTracker.isLive() ? liveTracker.seekableEnd() : this.player_.duration();
  13284. let newTime;
  13285. if (currentVideoTime + this.skipTime <= duration) {
  13286. newTime = currentVideoTime + this.skipTime;
  13287. } else {
  13288. newTime = duration;
  13289. }
  13290. this.player_.currentTime(newTime);
  13291. }
  13292. /**
  13293. * Update control text on languagechange
  13294. */
  13295. handleLanguagechange() {
  13296. this.controlText(this.localize('Skip forward {1} seconds', [this.skipTime]));
  13297. }
  13298. }
  13299. SkipForward.prototype.controlText_ = 'Skip Forward';
  13300. Component.registerComponent('SkipForward', SkipForward);
  13301. /**
  13302. * Button to skip backward a configurable amount of time
  13303. * through a video. Renders in the control bar.
  13304. *
  13305. * * e.g. options: {controlBar: {skipButtons: backward: 5}}
  13306. *
  13307. * @extends Button
  13308. */
  13309. class SkipBackward extends Button {
  13310. constructor(player, options) {
  13311. super(player, options);
  13312. this.validOptions = [5, 10, 30];
  13313. this.skipTime = this.getSkipBackwardTime();
  13314. if (this.skipTime && this.validOptions.includes(this.skipTime)) {
  13315. this.setIcon(`replay-${this.skipTime}`);
  13316. this.controlText(this.localize('Skip backward {1} seconds', [this.skipTime]));
  13317. this.show();
  13318. } else {
  13319. this.hide();
  13320. }
  13321. }
  13322. getSkipBackwardTime() {
  13323. const playerOptions = this.options_.playerOptions;
  13324. return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.backward;
  13325. }
  13326. buildCSSClass() {
  13327. return `vjs-skip-backward-${this.getSkipBackwardTime()} ${super.buildCSSClass()}`;
  13328. }
  13329. /**
  13330. * On click, skips backward in the video by a configurable amount of seconds.
  13331. * If the current time in the video is less than the configured 'skip backward' time,
  13332. * skips to beginning of video or seekable range.
  13333. *
  13334. * Handle a click on a `SkipBackward` button
  13335. *
  13336. * @param {EventTarget~Event} event
  13337. * The `click` event that caused this function
  13338. * to be called
  13339. */
  13340. handleClick(event) {
  13341. const currentVideoTime = this.player_.currentTime();
  13342. const liveTracker = this.player_.liveTracker;
  13343. const seekableStart = liveTracker && liveTracker.isLive() && liveTracker.seekableStart();
  13344. let newTime;
  13345. if (seekableStart && currentVideoTime - this.skipTime <= seekableStart) {
  13346. newTime = seekableStart;
  13347. } else if (currentVideoTime >= this.skipTime) {
  13348. newTime = currentVideoTime - this.skipTime;
  13349. } else {
  13350. newTime = 0;
  13351. }
  13352. this.player_.currentTime(newTime);
  13353. }
  13354. /**
  13355. * Update control text on languagechange
  13356. */
  13357. handleLanguagechange() {
  13358. this.controlText(this.localize('Skip backward {1} seconds', [this.skipTime]));
  13359. }
  13360. }
  13361. SkipBackward.prototype.controlText_ = 'Skip Backward';
  13362. Component.registerComponent('SkipBackward', SkipBackward);
  13363. /**
  13364. * @file menu.js
  13365. */
  13366. /**
  13367. * The Menu component is used to build popup menus, including subtitle and
  13368. * captions selection menus.
  13369. *
  13370. * @extends Component
  13371. */
  13372. class Menu extends Component {
  13373. /**
  13374. * Create an instance of this class.
  13375. *
  13376. * @param { import('../player').default } player
  13377. * the player that this component should attach to
  13378. *
  13379. * @param {Object} [options]
  13380. * Object of option names and values
  13381. *
  13382. */
  13383. constructor(player, options) {
  13384. super(player, options);
  13385. if (options) {
  13386. this.menuButton_ = options.menuButton;
  13387. }
  13388. this.focusedChild_ = -1;
  13389. this.on('keydown', e => this.handleKeyDown(e));
  13390. // All the menu item instances share the same blur handler provided by the menu container.
  13391. this.boundHandleBlur_ = e => this.handleBlur(e);
  13392. this.boundHandleTapClick_ = e => this.handleTapClick(e);
  13393. }
  13394. /**
  13395. * Add event listeners to the {@link MenuItem}.
  13396. *
  13397. * @param {Object} component
  13398. * The instance of the `MenuItem` to add listeners to.
  13399. *
  13400. */
  13401. addEventListenerForItem(component) {
  13402. if (!(component instanceof Component)) {
  13403. return;
  13404. }
  13405. this.on(component, 'blur', this.boundHandleBlur_);
  13406. this.on(component, ['tap', 'click'], this.boundHandleTapClick_);
  13407. }
  13408. /**
  13409. * Remove event listeners from the {@link MenuItem}.
  13410. *
  13411. * @param {Object} component
  13412. * The instance of the `MenuItem` to remove listeners.
  13413. *
  13414. */
  13415. removeEventListenerForItem(component) {
  13416. if (!(component instanceof Component)) {
  13417. return;
  13418. }
  13419. this.off(component, 'blur', this.boundHandleBlur_);
  13420. this.off(component, ['tap', 'click'], this.boundHandleTapClick_);
  13421. }
  13422. /**
  13423. * This method will be called indirectly when the component has been added
  13424. * before the component adds to the new menu instance by `addItem`.
  13425. * In this case, the original menu instance will remove the component
  13426. * by calling `removeChild`.
  13427. *
  13428. * @param {Object} component
  13429. * The instance of the `MenuItem`
  13430. */
  13431. removeChild(component) {
  13432. if (typeof component === 'string') {
  13433. component = this.getChild(component);
  13434. }
  13435. this.removeEventListenerForItem(component);
  13436. super.removeChild(component);
  13437. }
  13438. /**
  13439. * Add a {@link MenuItem} to the menu.
  13440. *
  13441. * @param {Object|string} component
  13442. * The name or instance of the `MenuItem` to add.
  13443. *
  13444. */
  13445. addItem(component) {
  13446. const childComponent = this.addChild(component);
  13447. if (childComponent) {
  13448. this.addEventListenerForItem(childComponent);
  13449. }
  13450. }
  13451. /**
  13452. * Create the `Menu`s DOM element.
  13453. *
  13454. * @return {Element}
  13455. * the element that was created
  13456. */
  13457. createEl() {
  13458. const contentElType = this.options_.contentElType || 'ul';
  13459. this.contentEl_ = createEl(contentElType, {
  13460. className: 'vjs-menu-content'
  13461. });
  13462. this.contentEl_.setAttribute('role', 'menu');
  13463. const el = super.createEl('div', {
  13464. append: this.contentEl_,
  13465. className: 'vjs-menu'
  13466. });
  13467. el.appendChild(this.contentEl_);
  13468. // Prevent clicks from bubbling up. Needed for Menu Buttons,
  13469. // where a click on the parent is significant
  13470. on(el, 'click', function (event) {
  13471. event.preventDefault();
  13472. event.stopImmediatePropagation();
  13473. });
  13474. return el;
  13475. }
  13476. dispose() {
  13477. this.contentEl_ = null;
  13478. this.boundHandleBlur_ = null;
  13479. this.boundHandleTapClick_ = null;
  13480. super.dispose();
  13481. }
  13482. /**
  13483. * Called when a `MenuItem` loses focus.
  13484. *
  13485. * @param {Event} event
  13486. * The `blur` event that caused this function to be called.
  13487. *
  13488. * @listens blur
  13489. */
  13490. handleBlur(event) {
  13491. const relatedTarget = event.relatedTarget || document.activeElement;
  13492. // Close menu popup when a user clicks outside the menu
  13493. if (!this.children().some(element => {
  13494. return element.el() === relatedTarget;
  13495. })) {
  13496. const btn = this.menuButton_;
  13497. if (btn && btn.buttonPressed_ && relatedTarget !== btn.el().firstChild) {
  13498. btn.unpressButton();
  13499. }
  13500. }
  13501. }
  13502. /**
  13503. * Called when a `MenuItem` gets clicked or tapped.
  13504. *
  13505. * @param {Event} event
  13506. * The `click` or `tap` event that caused this function to be called.
  13507. *
  13508. * @listens click,tap
  13509. */
  13510. handleTapClick(event) {
  13511. // Unpress the associated MenuButton, and move focus back to it
  13512. if (this.menuButton_) {
  13513. this.menuButton_.unpressButton();
  13514. const childComponents = this.children();
  13515. if (!Array.isArray(childComponents)) {
  13516. return;
  13517. }
  13518. const foundComponent = childComponents.filter(component => component.el() === event.target)[0];
  13519. if (!foundComponent) {
  13520. return;
  13521. }
  13522. // don't focus menu button if item is a caption settings item
  13523. // because focus will move elsewhere
  13524. if (foundComponent.name() !== 'CaptionSettingsMenuItem') {
  13525. this.menuButton_.focus();
  13526. }
  13527. }
  13528. }
  13529. /**
  13530. * Handle a `keydown` event on this menu. This listener is added in the constructor.
  13531. *
  13532. * @param {KeyboardEvent} event
  13533. * A `keydown` event that happened on the menu.
  13534. *
  13535. * @listens keydown
  13536. */
  13537. handleKeyDown(event) {
  13538. // Left and Down Arrows
  13539. if (keycode.isEventKey(event, 'Left') || keycode.isEventKey(event, 'Down')) {
  13540. event.preventDefault();
  13541. event.stopPropagation();
  13542. this.stepForward();
  13543. // Up and Right Arrows
  13544. } else if (keycode.isEventKey(event, 'Right') || keycode.isEventKey(event, 'Up')) {
  13545. event.preventDefault();
  13546. event.stopPropagation();
  13547. this.stepBack();
  13548. }
  13549. }
  13550. /**
  13551. * Move to next (lower) menu item for keyboard users.
  13552. */
  13553. stepForward() {
  13554. let stepChild = 0;
  13555. if (this.focusedChild_ !== undefined) {
  13556. stepChild = this.focusedChild_ + 1;
  13557. }
  13558. this.focus(stepChild);
  13559. }
  13560. /**
  13561. * Move to previous (higher) menu item for keyboard users.
  13562. */
  13563. stepBack() {
  13564. let stepChild = 0;
  13565. if (this.focusedChild_ !== undefined) {
  13566. stepChild = this.focusedChild_ - 1;
  13567. }
  13568. this.focus(stepChild);
  13569. }
  13570. /**
  13571. * Set focus on a {@link MenuItem} in the `Menu`.
  13572. *
  13573. * @param {Object|string} [item=0]
  13574. * Index of child item set focus on.
  13575. */
  13576. focus(item = 0) {
  13577. const children = this.children().slice();
  13578. const haveTitle = children.length && children[0].hasClass('vjs-menu-title');
  13579. if (haveTitle) {
  13580. children.shift();
  13581. }
  13582. if (children.length > 0) {
  13583. if (item < 0) {
  13584. item = 0;
  13585. } else if (item >= children.length) {
  13586. item = children.length - 1;
  13587. }
  13588. this.focusedChild_ = item;
  13589. children[item].el_.focus();
  13590. }
  13591. }
  13592. }
  13593. Component.registerComponent('Menu', Menu);
  13594. /**
  13595. * @file menu-button.js
  13596. */
  13597. /**
  13598. * A `MenuButton` class for any popup {@link Menu}.
  13599. *
  13600. * @extends Component
  13601. */
  13602. class MenuButton extends Component {
  13603. /**
  13604. * Creates an instance of this class.
  13605. *
  13606. * @param { import('../player').default } player
  13607. * The `Player` that this class should be attached to.
  13608. *
  13609. * @param {Object} [options={}]
  13610. * The key/value store of player options.
  13611. */
  13612. constructor(player, options = {}) {
  13613. super(player, options);
  13614. this.menuButton_ = new Button(player, options);
  13615. this.menuButton_.controlText(this.controlText_);
  13616. this.menuButton_.el_.setAttribute('aria-haspopup', 'true');
  13617. // Add buildCSSClass values to the button, not the wrapper
  13618. const buttonClass = Button.prototype.buildCSSClass();
  13619. this.menuButton_.el_.className = this.buildCSSClass() + ' ' + buttonClass;
  13620. this.menuButton_.removeClass('vjs-control');
  13621. this.addChild(this.menuButton_);
  13622. this.update();
  13623. this.enabled_ = true;
  13624. const handleClick = e => this.handleClick(e);
  13625. this.handleMenuKeyUp_ = e => this.handleMenuKeyUp(e);
  13626. this.on(this.menuButton_, 'tap', handleClick);
  13627. this.on(this.menuButton_, 'click', handleClick);
  13628. this.on(this.menuButton_, 'keydown', e => this.handleKeyDown(e));
  13629. this.on(this.menuButton_, 'mouseenter', () => {
  13630. this.addClass('vjs-hover');
  13631. this.menu.show();
  13632. on(document, 'keyup', this.handleMenuKeyUp_);
  13633. });
  13634. this.on('mouseleave', e => this.handleMouseLeave(e));
  13635. this.on('keydown', e => this.handleSubmenuKeyDown(e));
  13636. }
  13637. /**
  13638. * Update the menu based on the current state of its items.
  13639. */
  13640. update() {
  13641. const menu = this.createMenu();
  13642. if (this.menu) {
  13643. this.menu.dispose();
  13644. this.removeChild(this.menu);
  13645. }
  13646. this.menu = menu;
  13647. this.addChild(menu);
  13648. /**
  13649. * Track the state of the menu button
  13650. *
  13651. * @type {Boolean}
  13652. * @private
  13653. */
  13654. this.buttonPressed_ = false;
  13655. this.menuButton_.el_.setAttribute('aria-expanded', 'false');
  13656. if (this.items && this.items.length <= this.hideThreshold_) {
  13657. this.hide();
  13658. this.menu.contentEl_.removeAttribute('role');
  13659. } else {
  13660. this.show();
  13661. this.menu.contentEl_.setAttribute('role', 'menu');
  13662. }
  13663. }
  13664. /**
  13665. * Create the menu and add all items to it.
  13666. *
  13667. * @return {Menu}
  13668. * The constructed menu
  13669. */
  13670. createMenu() {
  13671. const menu = new Menu(this.player_, {
  13672. menuButton: this
  13673. });
  13674. /**
  13675. * Hide the menu if the number of items is less than or equal to this threshold. This defaults
  13676. * to 0 and whenever we add items which can be hidden to the menu we'll increment it. We list
  13677. * it here because every time we run `createMenu` we need to reset the value.
  13678. *
  13679. * @protected
  13680. * @type {Number}
  13681. */
  13682. this.hideThreshold_ = 0;
  13683. // Add a title list item to the top
  13684. if (this.options_.title) {
  13685. const titleEl = createEl('li', {
  13686. className: 'vjs-menu-title',
  13687. textContent: toTitleCase(this.options_.title),
  13688. tabIndex: -1
  13689. });
  13690. const titleComponent = new Component(this.player_, {
  13691. el: titleEl
  13692. });
  13693. menu.addItem(titleComponent);
  13694. }
  13695. this.items = this.createItems();
  13696. if (this.items) {
  13697. // Add menu items to the menu
  13698. for (let i = 0; i < this.items.length; i++) {
  13699. menu.addItem(this.items[i]);
  13700. }
  13701. }
  13702. return menu;
  13703. }
  13704. /**
  13705. * Create the list of menu items. Specific to each subclass.
  13706. *
  13707. * @abstract
  13708. */
  13709. createItems() {}
  13710. /**
  13711. * Create the `MenuButtons`s DOM element.
  13712. *
  13713. * @return {Element}
  13714. * The element that gets created.
  13715. */
  13716. createEl() {
  13717. return super.createEl('div', {
  13718. className: this.buildWrapperCSSClass()
  13719. }, {});
  13720. }
  13721. /**
  13722. * Overwrites the `setIcon` method from `Component`.
  13723. * In this case, we want the icon to be appended to the menuButton.
  13724. *
  13725. * @param {string} name
  13726. * The icon name to be added.
  13727. */
  13728. setIcon(name) {
  13729. super.setIcon(name, this.menuButton_.el_);
  13730. }
  13731. /**
  13732. * Allow sub components to stack CSS class names for the wrapper element
  13733. *
  13734. * @return {string}
  13735. * The constructed wrapper DOM `className`
  13736. */
  13737. buildWrapperCSSClass() {
  13738. let menuButtonClass = 'vjs-menu-button';
  13739. // If the inline option is passed, we want to use different styles altogether.
  13740. if (this.options_.inline === true) {
  13741. menuButtonClass += '-inline';
  13742. } else {
  13743. menuButtonClass += '-popup';
  13744. }
  13745. // TODO: Fix the CSS so that this isn't necessary
  13746. const buttonClass = Button.prototype.buildCSSClass();
  13747. return `vjs-menu-button ${menuButtonClass} ${buttonClass} ${super.buildCSSClass()}`;
  13748. }
  13749. /**
  13750. * Builds the default DOM `className`.
  13751. *
  13752. * @return {string}
  13753. * The DOM `className` for this object.
  13754. */
  13755. buildCSSClass() {
  13756. let menuButtonClass = 'vjs-menu-button';
  13757. // If the inline option is passed, we want to use different styles altogether.
  13758. if (this.options_.inline === true) {
  13759. menuButtonClass += '-inline';
  13760. } else {
  13761. menuButtonClass += '-popup';
  13762. }
  13763. return `vjs-menu-button ${menuButtonClass} ${super.buildCSSClass()}`;
  13764. }
  13765. /**
  13766. * Get or set the localized control text that will be used for accessibility.
  13767. *
  13768. * > NOTE: This will come from the internal `menuButton_` element.
  13769. *
  13770. * @param {string} [text]
  13771. * Control text for element.
  13772. *
  13773. * @param {Element} [el=this.menuButton_.el()]
  13774. * Element to set the title on.
  13775. *
  13776. * @return {string}
  13777. * - The control text when getting
  13778. */
  13779. controlText(text, el = this.menuButton_.el()) {
  13780. return this.menuButton_.controlText(text, el);
  13781. }
  13782. /**
  13783. * Dispose of the `menu-button` and all child components.
  13784. */
  13785. dispose() {
  13786. this.handleMouseLeave();
  13787. super.dispose();
  13788. }
  13789. /**
  13790. * Handle a click on a `MenuButton`.
  13791. * See {@link ClickableComponent#handleClick} for instances where this is called.
  13792. *
  13793. * @param {Event} event
  13794. * The `keydown`, `tap`, or `click` event that caused this function to be
  13795. * called.
  13796. *
  13797. * @listens tap
  13798. * @listens click
  13799. */
  13800. handleClick(event) {
  13801. if (this.buttonPressed_) {
  13802. this.unpressButton();
  13803. } else {
  13804. this.pressButton();
  13805. }
  13806. }
  13807. /**
  13808. * Handle `mouseleave` for `MenuButton`.
  13809. *
  13810. * @param {Event} event
  13811. * The `mouseleave` event that caused this function to be called.
  13812. *
  13813. * @listens mouseleave
  13814. */
  13815. handleMouseLeave(event) {
  13816. this.removeClass('vjs-hover');
  13817. off(document, 'keyup', this.handleMenuKeyUp_);
  13818. }
  13819. /**
  13820. * Set the focus to the actual button, not to this element
  13821. */
  13822. focus() {
  13823. this.menuButton_.focus();
  13824. }
  13825. /**
  13826. * Remove the focus from the actual button, not this element
  13827. */
  13828. blur() {
  13829. this.menuButton_.blur();
  13830. }
  13831. /**
  13832. * Handle tab, escape, down arrow, and up arrow keys for `MenuButton`. See
  13833. * {@link ClickableComponent#handleKeyDown} for instances where this is called.
  13834. *
  13835. * @param {Event} event
  13836. * The `keydown` event that caused this function to be called.
  13837. *
  13838. * @listens keydown
  13839. */
  13840. handleKeyDown(event) {
  13841. // Escape or Tab unpress the 'button'
  13842. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  13843. if (this.buttonPressed_) {
  13844. this.unpressButton();
  13845. }
  13846. // Don't preventDefault for Tab key - we still want to lose focus
  13847. if (!keycode.isEventKey(event, 'Tab')) {
  13848. event.preventDefault();
  13849. // Set focus back to the menu button's button
  13850. this.menuButton_.focus();
  13851. }
  13852. // Up Arrow or Down Arrow also 'press' the button to open the menu
  13853. } else if (keycode.isEventKey(event, 'Up') || keycode.isEventKey(event, 'Down')) {
  13854. if (!this.buttonPressed_) {
  13855. event.preventDefault();
  13856. this.pressButton();
  13857. }
  13858. }
  13859. }
  13860. /**
  13861. * Handle a `keyup` event on a `MenuButton`. The listener for this is added in
  13862. * the constructor.
  13863. *
  13864. * @param {Event} event
  13865. * Key press event
  13866. *
  13867. * @listens keyup
  13868. */
  13869. handleMenuKeyUp(event) {
  13870. // Escape hides popup menu
  13871. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  13872. this.removeClass('vjs-hover');
  13873. }
  13874. }
  13875. /**
  13876. * This method name now delegates to `handleSubmenuKeyDown`. This means
  13877. * anyone calling `handleSubmenuKeyPress` will not see their method calls
  13878. * stop working.
  13879. *
  13880. * @param {Event} event
  13881. * The event that caused this function to be called.
  13882. */
  13883. handleSubmenuKeyPress(event) {
  13884. this.handleSubmenuKeyDown(event);
  13885. }
  13886. /**
  13887. * Handle a `keydown` event on a sub-menu. The listener for this is added in
  13888. * the constructor.
  13889. *
  13890. * @param {Event} event
  13891. * Key press event
  13892. *
  13893. * @listens keydown
  13894. */
  13895. handleSubmenuKeyDown(event) {
  13896. // Escape or Tab unpress the 'button'
  13897. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  13898. if (this.buttonPressed_) {
  13899. this.unpressButton();
  13900. }
  13901. // Don't preventDefault for Tab key - we still want to lose focus
  13902. if (!keycode.isEventKey(event, 'Tab')) {
  13903. event.preventDefault();
  13904. // Set focus back to the menu button's button
  13905. this.menuButton_.focus();
  13906. }
  13907. }
  13908. }
  13909. /**
  13910. * Put the current `MenuButton` into a pressed state.
  13911. */
  13912. pressButton() {
  13913. if (this.enabled_) {
  13914. this.buttonPressed_ = true;
  13915. this.menu.show();
  13916. this.menu.lockShowing();
  13917. this.menuButton_.el_.setAttribute('aria-expanded', 'true');
  13918. // set the focus into the submenu, except on iOS where it is resulting in
  13919. // undesired scrolling behavior when the player is in an iframe
  13920. if (IS_IOS && isInFrame()) {
  13921. // Return early so that the menu isn't focused
  13922. return;
  13923. }
  13924. this.menu.focus();
  13925. }
  13926. }
  13927. /**
  13928. * Take the current `MenuButton` out of a pressed state.
  13929. */
  13930. unpressButton() {
  13931. if (this.enabled_) {
  13932. this.buttonPressed_ = false;
  13933. this.menu.unlockShowing();
  13934. this.menu.hide();
  13935. this.menuButton_.el_.setAttribute('aria-expanded', 'false');
  13936. }
  13937. }
  13938. /**
  13939. * Disable the `MenuButton`. Don't allow it to be clicked.
  13940. */
  13941. disable() {
  13942. this.unpressButton();
  13943. this.enabled_ = false;
  13944. this.addClass('vjs-disabled');
  13945. this.menuButton_.disable();
  13946. }
  13947. /**
  13948. * Enable the `MenuButton`. Allow it to be clicked.
  13949. */
  13950. enable() {
  13951. this.enabled_ = true;
  13952. this.removeClass('vjs-disabled');
  13953. this.menuButton_.enable();
  13954. }
  13955. }
  13956. Component.registerComponent('MenuButton', MenuButton);
  13957. /**
  13958. * @file track-button.js
  13959. */
  13960. /**
  13961. * The base class for buttons that toggle specific track types (e.g. subtitles).
  13962. *
  13963. * @extends MenuButton
  13964. */
  13965. class TrackButton extends MenuButton {
  13966. /**
  13967. * Creates an instance of this class.
  13968. *
  13969. * @param { import('./player').default } player
  13970. * The `Player` that this class should be attached to.
  13971. *
  13972. * @param {Object} [options]
  13973. * The key/value store of player options.
  13974. */
  13975. constructor(player, options) {
  13976. const tracks = options.tracks;
  13977. super(player, options);
  13978. if (this.items.length <= 1) {
  13979. this.hide();
  13980. }
  13981. if (!tracks) {
  13982. return;
  13983. }
  13984. const updateHandler = bind_(this, this.update);
  13985. tracks.addEventListener('removetrack', updateHandler);
  13986. tracks.addEventListener('addtrack', updateHandler);
  13987. tracks.addEventListener('labelchange', updateHandler);
  13988. this.player_.on('ready', updateHandler);
  13989. this.player_.on('dispose', function () {
  13990. tracks.removeEventListener('removetrack', updateHandler);
  13991. tracks.removeEventListener('addtrack', updateHandler);
  13992. tracks.removeEventListener('labelchange', updateHandler);
  13993. });
  13994. }
  13995. }
  13996. Component.registerComponent('TrackButton', TrackButton);
  13997. /**
  13998. * @file menu-keys.js
  13999. */
  14000. /**
  14001. * All keys used for operation of a menu (`MenuButton`, `Menu`, and `MenuItem`)
  14002. * Note that 'Enter' and 'Space' are not included here (otherwise they would
  14003. * prevent the `MenuButton` and `MenuItem` from being keyboard-clickable)
  14004. *
  14005. * @typedef MenuKeys
  14006. * @array
  14007. */
  14008. const MenuKeys = ['Tab', 'Esc', 'Up', 'Down', 'Right', 'Left'];
  14009. /**
  14010. * @file menu-item.js
  14011. */
  14012. /**
  14013. * The component for a menu item. `<li>`
  14014. *
  14015. * @extends ClickableComponent
  14016. */
  14017. class MenuItem extends ClickableComponent {
  14018. /**
  14019. * Creates an instance of the this class.
  14020. *
  14021. * @param { import('../player').default } player
  14022. * The `Player` that this class should be attached to.
  14023. *
  14024. * @param {Object} [options={}]
  14025. * The key/value store of player options.
  14026. *
  14027. */
  14028. constructor(player, options) {
  14029. super(player, options);
  14030. this.selectable = options.selectable;
  14031. this.isSelected_ = options.selected || false;
  14032. this.multiSelectable = options.multiSelectable;
  14033. this.selected(this.isSelected_);
  14034. if (this.selectable) {
  14035. if (this.multiSelectable) {
  14036. this.el_.setAttribute('role', 'menuitemcheckbox');
  14037. } else {
  14038. this.el_.setAttribute('role', 'menuitemradio');
  14039. }
  14040. } else {
  14041. this.el_.setAttribute('role', 'menuitem');
  14042. }
  14043. }
  14044. /**
  14045. * Create the `MenuItem's DOM element
  14046. *
  14047. * @param {string} [type=li]
  14048. * Element's node type, not actually used, always set to `li`.
  14049. *
  14050. * @param {Object} [props={}]
  14051. * An object of properties that should be set on the element
  14052. *
  14053. * @param {Object} [attrs={}]
  14054. * An object of attributes that should be set on the element
  14055. *
  14056. * @return {Element}
  14057. * The element that gets created.
  14058. */
  14059. createEl(type, props, attrs) {
  14060. // The control is textual, not just an icon
  14061. this.nonIconControl = true;
  14062. const el = super.createEl('li', Object.assign({
  14063. className: 'vjs-menu-item',
  14064. tabIndex: -1
  14065. }, props), attrs);
  14066. // swap icon with menu item text.
  14067. const menuItemEl = createEl('span', {
  14068. className: 'vjs-menu-item-text',
  14069. textContent: this.localize(this.options_.label)
  14070. });
  14071. // If using SVG icons, the element with vjs-icon-placeholder will be added separately.
  14072. if (this.player_.options_.experimentalSvgIcons) {
  14073. el.appendChild(menuItemEl);
  14074. } else {
  14075. el.replaceChild(menuItemEl, el.querySelector('.vjs-icon-placeholder'));
  14076. }
  14077. return el;
  14078. }
  14079. /**
  14080. * Ignore keys which are used by the menu, but pass any other ones up. See
  14081. * {@link ClickableComponent#handleKeyDown} for instances where this is called.
  14082. *
  14083. * @param {KeyboardEvent} event
  14084. * The `keydown` event that caused this function to be called.
  14085. *
  14086. * @listens keydown
  14087. */
  14088. handleKeyDown(event) {
  14089. if (!MenuKeys.some(key => keycode.isEventKey(event, key))) {
  14090. // Pass keydown handling up for unused keys
  14091. super.handleKeyDown(event);
  14092. }
  14093. }
  14094. /**
  14095. * Any click on a `MenuItem` puts it into the selected state.
  14096. * See {@link ClickableComponent#handleClick} for instances where this is called.
  14097. *
  14098. * @param {Event} event
  14099. * The `keydown`, `tap`, or `click` event that caused this function to be
  14100. * called.
  14101. *
  14102. * @listens tap
  14103. * @listens click
  14104. */
  14105. handleClick(event) {
  14106. this.selected(true);
  14107. }
  14108. /**
  14109. * Set the state for this menu item as selected or not.
  14110. *
  14111. * @param {boolean} selected
  14112. * if the menu item is selected or not
  14113. */
  14114. selected(selected) {
  14115. if (this.selectable) {
  14116. if (selected) {
  14117. this.addClass('vjs-selected');
  14118. this.el_.setAttribute('aria-checked', 'true');
  14119. // aria-checked isn't fully supported by browsers/screen readers,
  14120. // so indicate selected state to screen reader in the control text.
  14121. this.controlText(', selected');
  14122. this.isSelected_ = true;
  14123. } else {
  14124. this.removeClass('vjs-selected');
  14125. this.el_.setAttribute('aria-checked', 'false');
  14126. // Indicate un-selected state to screen reader
  14127. this.controlText('');
  14128. this.isSelected_ = false;
  14129. }
  14130. }
  14131. }
  14132. }
  14133. Component.registerComponent('MenuItem', MenuItem);
  14134. /**
  14135. * @file text-track-menu-item.js
  14136. */
  14137. /**
  14138. * The specific menu item type for selecting a language within a text track kind
  14139. *
  14140. * @extends MenuItem
  14141. */
  14142. class TextTrackMenuItem extends MenuItem {
  14143. /**
  14144. * Creates an instance of this class.
  14145. *
  14146. * @param { import('../../player').default } player
  14147. * The `Player` that this class should be attached to.
  14148. *
  14149. * @param {Object} [options]
  14150. * The key/value store of player options.
  14151. */
  14152. constructor(player, options) {
  14153. const track = options.track;
  14154. const tracks = player.textTracks();
  14155. // Modify options for parent MenuItem class's init.
  14156. options.label = track.label || track.language || 'Unknown';
  14157. options.selected = track.mode === 'showing';
  14158. super(player, options);
  14159. this.track = track;
  14160. // Determine the relevant kind(s) of tracks for this component and filter
  14161. // out empty kinds.
  14162. this.kinds = (options.kinds || [options.kind || this.track.kind]).filter(Boolean);
  14163. const changeHandler = (...args) => {
  14164. this.handleTracksChange.apply(this, args);
  14165. };
  14166. const selectedLanguageChangeHandler = (...args) => {
  14167. this.handleSelectedLanguageChange.apply(this, args);
  14168. };
  14169. player.on(['loadstart', 'texttrackchange'], changeHandler);
  14170. tracks.addEventListener('change', changeHandler);
  14171. tracks.addEventListener('selectedlanguagechange', selectedLanguageChangeHandler);
  14172. this.on('dispose', function () {
  14173. player.off(['loadstart', 'texttrackchange'], changeHandler);
  14174. tracks.removeEventListener('change', changeHandler);
  14175. tracks.removeEventListener('selectedlanguagechange', selectedLanguageChangeHandler);
  14176. });
  14177. // iOS7 doesn't dispatch change events to TextTrackLists when an
  14178. // associated track's mode changes. Without something like
  14179. // Object.observe() (also not present on iOS7), it's not
  14180. // possible to detect changes to the mode attribute and polyfill
  14181. // the change event. As a poor substitute, we manually dispatch
  14182. // change events whenever the controls modify the mode.
  14183. if (tracks.onchange === undefined) {
  14184. let event;
  14185. this.on(['tap', 'click'], function () {
  14186. if (typeof window.Event !== 'object') {
  14187. // Android 2.3 throws an Illegal Constructor error for window.Event
  14188. try {
  14189. event = new window.Event('change');
  14190. } catch (err) {
  14191. // continue regardless of error
  14192. }
  14193. }
  14194. if (!event) {
  14195. event = document.createEvent('Event');
  14196. event.initEvent('change', true, true);
  14197. }
  14198. tracks.dispatchEvent(event);
  14199. });
  14200. }
  14201. // set the default state based on current tracks
  14202. this.handleTracksChange();
  14203. }
  14204. /**
  14205. * This gets called when an `TextTrackMenuItem` is "clicked". See
  14206. * {@link ClickableComponent} for more detailed information on what a click can be.
  14207. *
  14208. * @param {Event} event
  14209. * The `keydown`, `tap`, or `click` event that caused this function to be
  14210. * called.
  14211. *
  14212. * @listens tap
  14213. * @listens click
  14214. */
  14215. handleClick(event) {
  14216. const referenceTrack = this.track;
  14217. const tracks = this.player_.textTracks();
  14218. super.handleClick(event);
  14219. if (!tracks) {
  14220. return;
  14221. }
  14222. for (let i = 0; i < tracks.length; i++) {
  14223. const track = tracks[i];
  14224. // If the track from the text tracks list is not of the right kind,
  14225. // skip it. We do not want to affect tracks of incompatible kind(s).
  14226. if (this.kinds.indexOf(track.kind) === -1) {
  14227. continue;
  14228. }
  14229. // If this text track is the component's track and it is not showing,
  14230. // set it to showing.
  14231. if (track === referenceTrack) {
  14232. if (track.mode !== 'showing') {
  14233. track.mode = 'showing';
  14234. }
  14235. // If this text track is not the component's track and it is not
  14236. // disabled, set it to disabled.
  14237. } else if (track.mode !== 'disabled') {
  14238. track.mode = 'disabled';
  14239. }
  14240. }
  14241. }
  14242. /**
  14243. * Handle text track list change
  14244. *
  14245. * @param {Event} event
  14246. * The `change` event that caused this function to be called.
  14247. *
  14248. * @listens TextTrackList#change
  14249. */
  14250. handleTracksChange(event) {
  14251. const shouldBeSelected = this.track.mode === 'showing';
  14252. // Prevent redundant selected() calls because they may cause
  14253. // screen readers to read the appended control text unnecessarily
  14254. if (shouldBeSelected !== this.isSelected_) {
  14255. this.selected(shouldBeSelected);
  14256. }
  14257. }
  14258. handleSelectedLanguageChange(event) {
  14259. if (this.track.mode === 'showing') {
  14260. const selectedLanguage = this.player_.cache_.selectedLanguage;
  14261. // Don't replace the kind of track across the same language
  14262. if (selectedLanguage && selectedLanguage.enabled && selectedLanguage.language === this.track.language && selectedLanguage.kind !== this.track.kind) {
  14263. return;
  14264. }
  14265. this.player_.cache_.selectedLanguage = {
  14266. enabled: true,
  14267. language: this.track.language,
  14268. kind: this.track.kind
  14269. };
  14270. }
  14271. }
  14272. dispose() {
  14273. // remove reference to track object on dispose
  14274. this.track = null;
  14275. super.dispose();
  14276. }
  14277. }
  14278. Component.registerComponent('TextTrackMenuItem', TextTrackMenuItem);
  14279. /**
  14280. * @file off-text-track-menu-item.js
  14281. */
  14282. /**
  14283. * A special menu item for turning off a specific type of text track
  14284. *
  14285. * @extends TextTrackMenuItem
  14286. */
  14287. class OffTextTrackMenuItem extends TextTrackMenuItem {
  14288. /**
  14289. * Creates an instance of this class.
  14290. *
  14291. * @param { import('../../player').default } player
  14292. * The `Player` that this class should be attached to.
  14293. *
  14294. * @param {Object} [options]
  14295. * The key/value store of player options.
  14296. */
  14297. constructor(player, options) {
  14298. // Create pseudo track info
  14299. // Requires options['kind']
  14300. options.track = {
  14301. player,
  14302. // it is no longer necessary to store `kind` or `kinds` on the track itself
  14303. // since they are now stored in the `kinds` property of all instances of
  14304. // TextTrackMenuItem, but this will remain for backwards compatibility
  14305. kind: options.kind,
  14306. kinds: options.kinds,
  14307. default: false,
  14308. mode: 'disabled'
  14309. };
  14310. if (!options.kinds) {
  14311. options.kinds = [options.kind];
  14312. }
  14313. if (options.label) {
  14314. options.track.label = options.label;
  14315. } else {
  14316. options.track.label = options.kinds.join(' and ') + ' off';
  14317. }
  14318. // MenuItem is selectable
  14319. options.selectable = true;
  14320. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  14321. options.multiSelectable = false;
  14322. super(player, options);
  14323. }
  14324. /**
  14325. * Handle text track change
  14326. *
  14327. * @param {Event} event
  14328. * The event that caused this function to run
  14329. */
  14330. handleTracksChange(event) {
  14331. const tracks = this.player().textTracks();
  14332. let shouldBeSelected = true;
  14333. for (let i = 0, l = tracks.length; i < l; i++) {
  14334. const track = tracks[i];
  14335. if (this.options_.kinds.indexOf(track.kind) > -1 && track.mode === 'showing') {
  14336. shouldBeSelected = false;
  14337. break;
  14338. }
  14339. }
  14340. // Prevent redundant selected() calls because they may cause
  14341. // screen readers to read the appended control text unnecessarily
  14342. if (shouldBeSelected !== this.isSelected_) {
  14343. this.selected(shouldBeSelected);
  14344. }
  14345. }
  14346. handleSelectedLanguageChange(event) {
  14347. const tracks = this.player().textTracks();
  14348. let allHidden = true;
  14349. for (let i = 0, l = tracks.length; i < l; i++) {
  14350. const track = tracks[i];
  14351. if (['captions', 'descriptions', 'subtitles'].indexOf(track.kind) > -1 && track.mode === 'showing') {
  14352. allHidden = false;
  14353. break;
  14354. }
  14355. }
  14356. if (allHidden) {
  14357. this.player_.cache_.selectedLanguage = {
  14358. enabled: false
  14359. };
  14360. }
  14361. }
  14362. /**
  14363. * Update control text and label on languagechange
  14364. */
  14365. handleLanguagechange() {
  14366. this.$('.vjs-menu-item-text').textContent = this.player_.localize(this.options_.label);
  14367. super.handleLanguagechange();
  14368. }
  14369. }
  14370. Component.registerComponent('OffTextTrackMenuItem', OffTextTrackMenuItem);
  14371. /**
  14372. * @file text-track-button.js
  14373. */
  14374. /**
  14375. * The base class for buttons that toggle specific text track types (e.g. subtitles)
  14376. *
  14377. * @extends MenuButton
  14378. */
  14379. class TextTrackButton extends TrackButton {
  14380. /**
  14381. * Creates an instance of this class.
  14382. *
  14383. * @param { import('../../player').default } player
  14384. * The `Player` that this class should be attached to.
  14385. *
  14386. * @param {Object} [options={}]
  14387. * The key/value store of player options.
  14388. */
  14389. constructor(player, options = {}) {
  14390. options.tracks = player.textTracks();
  14391. super(player, options);
  14392. }
  14393. /**
  14394. * Create a menu item for each text track
  14395. *
  14396. * @param {TextTrackMenuItem[]} [items=[]]
  14397. * Existing array of items to use during creation
  14398. *
  14399. * @return {TextTrackMenuItem[]}
  14400. * Array of menu items that were created
  14401. */
  14402. createItems(items = [], TrackMenuItem = TextTrackMenuItem) {
  14403. // Label is an override for the [track] off label
  14404. // USed to localise captions/subtitles
  14405. let label;
  14406. if (this.label_) {
  14407. label = `${this.label_} off`;
  14408. }
  14409. // Add an OFF menu item to turn all tracks off
  14410. items.push(new OffTextTrackMenuItem(this.player_, {
  14411. kinds: this.kinds_,
  14412. kind: this.kind_,
  14413. label
  14414. }));
  14415. this.hideThreshold_ += 1;
  14416. const tracks = this.player_.textTracks();
  14417. if (!Array.isArray(this.kinds_)) {
  14418. this.kinds_ = [this.kind_];
  14419. }
  14420. for (let i = 0; i < tracks.length; i++) {
  14421. const track = tracks[i];
  14422. // only add tracks that are of an appropriate kind and have a label
  14423. if (this.kinds_.indexOf(track.kind) > -1) {
  14424. const item = new TrackMenuItem(this.player_, {
  14425. track,
  14426. kinds: this.kinds_,
  14427. kind: this.kind_,
  14428. // MenuItem is selectable
  14429. selectable: true,
  14430. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  14431. multiSelectable: false
  14432. });
  14433. item.addClass(`vjs-${track.kind}-menu-item`);
  14434. items.push(item);
  14435. }
  14436. }
  14437. return items;
  14438. }
  14439. }
  14440. Component.registerComponent('TextTrackButton', TextTrackButton);
  14441. /**
  14442. * @file chapters-track-menu-item.js
  14443. */
  14444. /**
  14445. * The chapter track menu item
  14446. *
  14447. * @extends MenuItem
  14448. */
  14449. class ChaptersTrackMenuItem extends MenuItem {
  14450. /**
  14451. * Creates an instance of this class.
  14452. *
  14453. * @param { import('../../player').default } player
  14454. * The `Player` that this class should be attached to.
  14455. *
  14456. * @param {Object} [options]
  14457. * The key/value store of player options.
  14458. */
  14459. constructor(player, options) {
  14460. const track = options.track;
  14461. const cue = options.cue;
  14462. const currentTime = player.currentTime();
  14463. // Modify options for parent MenuItem class's init.
  14464. options.selectable = true;
  14465. options.multiSelectable = false;
  14466. options.label = cue.text;
  14467. options.selected = cue.startTime <= currentTime && currentTime < cue.endTime;
  14468. super(player, options);
  14469. this.track = track;
  14470. this.cue = cue;
  14471. }
  14472. /**
  14473. * This gets called when an `ChaptersTrackMenuItem` is "clicked". See
  14474. * {@link ClickableComponent} for more detailed information on what a click can be.
  14475. *
  14476. * @param {Event} [event]
  14477. * The `keydown`, `tap`, or `click` event that caused this function to be
  14478. * called.
  14479. *
  14480. * @listens tap
  14481. * @listens click
  14482. */
  14483. handleClick(event) {
  14484. super.handleClick();
  14485. this.player_.currentTime(this.cue.startTime);
  14486. }
  14487. }
  14488. Component.registerComponent('ChaptersTrackMenuItem', ChaptersTrackMenuItem);
  14489. /**
  14490. * @file chapters-button.js
  14491. */
  14492. /**
  14493. * The button component for toggling and selecting chapters
  14494. * Chapters act much differently than other text tracks
  14495. * Cues are navigation vs. other tracks of alternative languages
  14496. *
  14497. * @extends TextTrackButton
  14498. */
  14499. class ChaptersButton extends TextTrackButton {
  14500. /**
  14501. * Creates an instance of this class.
  14502. *
  14503. * @param { import('../../player').default } player
  14504. * The `Player` that this class should be attached to.
  14505. *
  14506. * @param {Object} [options]
  14507. * The key/value store of player options.
  14508. *
  14509. * @param {Function} [ready]
  14510. * The function to call when this function is ready.
  14511. */
  14512. constructor(player, options, ready) {
  14513. super(player, options, ready);
  14514. this.setIcon('chapters');
  14515. this.selectCurrentItem_ = () => {
  14516. this.items.forEach(item => {
  14517. item.selected(this.track_.activeCues[0] === item.cue);
  14518. });
  14519. };
  14520. }
  14521. /**
  14522. * Builds the default DOM `className`.
  14523. *
  14524. * @return {string}
  14525. * The DOM `className` for this object.
  14526. */
  14527. buildCSSClass() {
  14528. return `vjs-chapters-button ${super.buildCSSClass()}`;
  14529. }
  14530. buildWrapperCSSClass() {
  14531. return `vjs-chapters-button ${super.buildWrapperCSSClass()}`;
  14532. }
  14533. /**
  14534. * Update the menu based on the current state of its items.
  14535. *
  14536. * @param {Event} [event]
  14537. * An event that triggered this function to run.
  14538. *
  14539. * @listens TextTrackList#addtrack
  14540. * @listens TextTrackList#removetrack
  14541. * @listens TextTrackList#change
  14542. */
  14543. update(event) {
  14544. if (event && event.track && event.track.kind !== 'chapters') {
  14545. return;
  14546. }
  14547. const track = this.findChaptersTrack();
  14548. if (track !== this.track_) {
  14549. this.setTrack(track);
  14550. super.update();
  14551. } else if (!this.items || track && track.cues && track.cues.length !== this.items.length) {
  14552. // Update the menu initially or if the number of cues has changed since set
  14553. super.update();
  14554. }
  14555. }
  14556. /**
  14557. * Set the currently selected track for the chapters button.
  14558. *
  14559. * @param {TextTrack} track
  14560. * The new track to select. Nothing will change if this is the currently selected
  14561. * track.
  14562. */
  14563. setTrack(track) {
  14564. if (this.track_ === track) {
  14565. return;
  14566. }
  14567. if (!this.updateHandler_) {
  14568. this.updateHandler_ = this.update.bind(this);
  14569. }
  14570. // here this.track_ refers to the old track instance
  14571. if (this.track_) {
  14572. const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
  14573. if (remoteTextTrackEl) {
  14574. remoteTextTrackEl.removeEventListener('load', this.updateHandler_);
  14575. }
  14576. this.track_.removeEventListener('cuechange', this.selectCurrentItem_);
  14577. this.track_ = null;
  14578. }
  14579. this.track_ = track;
  14580. // here this.track_ refers to the new track instance
  14581. if (this.track_) {
  14582. this.track_.mode = 'hidden';
  14583. const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
  14584. if (remoteTextTrackEl) {
  14585. remoteTextTrackEl.addEventListener('load', this.updateHandler_);
  14586. }
  14587. this.track_.addEventListener('cuechange', this.selectCurrentItem_);
  14588. }
  14589. }
  14590. /**
  14591. * Find the track object that is currently in use by this ChaptersButton
  14592. *
  14593. * @return {TextTrack|undefined}
  14594. * The current track or undefined if none was found.
  14595. */
  14596. findChaptersTrack() {
  14597. const tracks = this.player_.textTracks() || [];
  14598. for (let i = tracks.length - 1; i >= 0; i--) {
  14599. // We will always choose the last track as our chaptersTrack
  14600. const track = tracks[i];
  14601. if (track.kind === this.kind_) {
  14602. return track;
  14603. }
  14604. }
  14605. }
  14606. /**
  14607. * Get the caption for the ChaptersButton based on the track label. This will also
  14608. * use the current tracks localized kind as a fallback if a label does not exist.
  14609. *
  14610. * @return {string}
  14611. * The tracks current label or the localized track kind.
  14612. */
  14613. getMenuCaption() {
  14614. if (this.track_ && this.track_.label) {
  14615. return this.track_.label;
  14616. }
  14617. return this.localize(toTitleCase(this.kind_));
  14618. }
  14619. /**
  14620. * Create menu from chapter track
  14621. *
  14622. * @return { import('../../menu/menu').default }
  14623. * New menu for the chapter buttons
  14624. */
  14625. createMenu() {
  14626. this.options_.title = this.getMenuCaption();
  14627. return super.createMenu();
  14628. }
  14629. /**
  14630. * Create a menu item for each text track
  14631. *
  14632. * @return { import('./text-track-menu-item').default[] }
  14633. * Array of menu items
  14634. */
  14635. createItems() {
  14636. const items = [];
  14637. if (!this.track_) {
  14638. return items;
  14639. }
  14640. const cues = this.track_.cues;
  14641. if (!cues) {
  14642. return items;
  14643. }
  14644. for (let i = 0, l = cues.length; i < l; i++) {
  14645. const cue = cues[i];
  14646. const mi = new ChaptersTrackMenuItem(this.player_, {
  14647. track: this.track_,
  14648. cue
  14649. });
  14650. items.push(mi);
  14651. }
  14652. return items;
  14653. }
  14654. }
  14655. /**
  14656. * `kind` of TextTrack to look for to associate it with this menu.
  14657. *
  14658. * @type {string}
  14659. * @private
  14660. */
  14661. ChaptersButton.prototype.kind_ = 'chapters';
  14662. /**
  14663. * The text that should display over the `ChaptersButton`s controls. Added for localization.
  14664. *
  14665. * @type {string}
  14666. * @protected
  14667. */
  14668. ChaptersButton.prototype.controlText_ = 'Chapters';
  14669. Component.registerComponent('ChaptersButton', ChaptersButton);
  14670. /**
  14671. * @file descriptions-button.js
  14672. */
  14673. /**
  14674. * The button component for toggling and selecting descriptions
  14675. *
  14676. * @extends TextTrackButton
  14677. */
  14678. class DescriptionsButton extends TextTrackButton {
  14679. /**
  14680. * Creates an instance of this class.
  14681. *
  14682. * @param { import('../../player').default } player
  14683. * The `Player` that this class should be attached to.
  14684. *
  14685. * @param {Object} [options]
  14686. * The key/value store of player options.
  14687. *
  14688. * @param {Function} [ready]
  14689. * The function to call when this component is ready.
  14690. */
  14691. constructor(player, options, ready) {
  14692. super(player, options, ready);
  14693. this.setIcon('audio-description');
  14694. const tracks = player.textTracks();
  14695. const changeHandler = bind_(this, this.handleTracksChange);
  14696. tracks.addEventListener('change', changeHandler);
  14697. this.on('dispose', function () {
  14698. tracks.removeEventListener('change', changeHandler);
  14699. });
  14700. }
  14701. /**
  14702. * Handle text track change
  14703. *
  14704. * @param {Event} event
  14705. * The event that caused this function to run
  14706. *
  14707. * @listens TextTrackList#change
  14708. */
  14709. handleTracksChange(event) {
  14710. const tracks = this.player().textTracks();
  14711. let disabled = false;
  14712. // Check whether a track of a different kind is showing
  14713. for (let i = 0, l = tracks.length; i < l; i++) {
  14714. const track = tracks[i];
  14715. if (track.kind !== this.kind_ && track.mode === 'showing') {
  14716. disabled = true;
  14717. break;
  14718. }
  14719. }
  14720. // If another track is showing, disable this menu button
  14721. if (disabled) {
  14722. this.disable();
  14723. } else {
  14724. this.enable();
  14725. }
  14726. }
  14727. /**
  14728. * Builds the default DOM `className`.
  14729. *
  14730. * @return {string}
  14731. * The DOM `className` for this object.
  14732. */
  14733. buildCSSClass() {
  14734. return `vjs-descriptions-button ${super.buildCSSClass()}`;
  14735. }
  14736. buildWrapperCSSClass() {
  14737. return `vjs-descriptions-button ${super.buildWrapperCSSClass()}`;
  14738. }
  14739. }
  14740. /**
  14741. * `kind` of TextTrack to look for to associate it with this menu.
  14742. *
  14743. * @type {string}
  14744. * @private
  14745. */
  14746. DescriptionsButton.prototype.kind_ = 'descriptions';
  14747. /**
  14748. * The text that should display over the `DescriptionsButton`s controls. Added for localization.
  14749. *
  14750. * @type {string}
  14751. * @protected
  14752. */
  14753. DescriptionsButton.prototype.controlText_ = 'Descriptions';
  14754. Component.registerComponent('DescriptionsButton', DescriptionsButton);
  14755. /**
  14756. * @file subtitles-button.js
  14757. */
  14758. /**
  14759. * The button component for toggling and selecting subtitles
  14760. *
  14761. * @extends TextTrackButton
  14762. */
  14763. class SubtitlesButton extends TextTrackButton {
  14764. /**
  14765. * Creates an instance of this class.
  14766. *
  14767. * @param { import('../../player').default } player
  14768. * The `Player` that this class should be attached to.
  14769. *
  14770. * @param {Object} [options]
  14771. * The key/value store of player options.
  14772. *
  14773. * @param {Function} [ready]
  14774. * The function to call when this component is ready.
  14775. */
  14776. constructor(player, options, ready) {
  14777. super(player, options, ready);
  14778. this.setIcon('subtitles');
  14779. }
  14780. /**
  14781. * Builds the default DOM `className`.
  14782. *
  14783. * @return {string}
  14784. * The DOM `className` for this object.
  14785. */
  14786. buildCSSClass() {
  14787. return `vjs-subtitles-button ${super.buildCSSClass()}`;
  14788. }
  14789. buildWrapperCSSClass() {
  14790. return `vjs-subtitles-button ${super.buildWrapperCSSClass()}`;
  14791. }
  14792. }
  14793. /**
  14794. * `kind` of TextTrack to look for to associate it with this menu.
  14795. *
  14796. * @type {string}
  14797. * @private
  14798. */
  14799. SubtitlesButton.prototype.kind_ = 'subtitles';
  14800. /**
  14801. * The text that should display over the `SubtitlesButton`s controls. Added for localization.
  14802. *
  14803. * @type {string}
  14804. * @protected
  14805. */
  14806. SubtitlesButton.prototype.controlText_ = 'Subtitles';
  14807. Component.registerComponent('SubtitlesButton', SubtitlesButton);
  14808. /**
  14809. * @file caption-settings-menu-item.js
  14810. */
  14811. /**
  14812. * The menu item for caption track settings menu
  14813. *
  14814. * @extends TextTrackMenuItem
  14815. */
  14816. class CaptionSettingsMenuItem extends TextTrackMenuItem {
  14817. /**
  14818. * Creates an instance of this class.
  14819. *
  14820. * @param { import('../../player').default } player
  14821. * The `Player` that this class should be attached to.
  14822. *
  14823. * @param {Object} [options]
  14824. * The key/value store of player options.
  14825. */
  14826. constructor(player, options) {
  14827. options.track = {
  14828. player,
  14829. kind: options.kind,
  14830. label: options.kind + ' settings',
  14831. selectable: false,
  14832. default: false,
  14833. mode: 'disabled'
  14834. };
  14835. // CaptionSettingsMenuItem has no concept of 'selected'
  14836. options.selectable = false;
  14837. options.name = 'CaptionSettingsMenuItem';
  14838. super(player, options);
  14839. this.addClass('vjs-texttrack-settings');
  14840. this.controlText(', opens ' + options.kind + ' settings dialog');
  14841. }
  14842. /**
  14843. * This gets called when an `CaptionSettingsMenuItem` is "clicked". See
  14844. * {@link ClickableComponent} for more detailed information on what a click can be.
  14845. *
  14846. * @param {Event} [event]
  14847. * The `keydown`, `tap`, or `click` event that caused this function to be
  14848. * called.
  14849. *
  14850. * @listens tap
  14851. * @listens click
  14852. */
  14853. handleClick(event) {
  14854. this.player().getChild('textTrackSettings').open();
  14855. }
  14856. /**
  14857. * Update control text and label on languagechange
  14858. */
  14859. handleLanguagechange() {
  14860. this.$('.vjs-menu-item-text').textContent = this.player_.localize(this.options_.kind + ' settings');
  14861. super.handleLanguagechange();
  14862. }
  14863. }
  14864. Component.registerComponent('CaptionSettingsMenuItem', CaptionSettingsMenuItem);
  14865. /**
  14866. * @file captions-button.js
  14867. */
  14868. /**
  14869. * The button component for toggling and selecting captions
  14870. *
  14871. * @extends TextTrackButton
  14872. */
  14873. class CaptionsButton extends TextTrackButton {
  14874. /**
  14875. * Creates an instance of this class.
  14876. *
  14877. * @param { import('../../player').default } player
  14878. * The `Player` that this class should be attached to.
  14879. *
  14880. * @param {Object} [options]
  14881. * The key/value store of player options.
  14882. *
  14883. * @param {Function} [ready]
  14884. * The function to call when this component is ready.
  14885. */
  14886. constructor(player, options, ready) {
  14887. super(player, options, ready);
  14888. this.setIcon('captions');
  14889. }
  14890. /**
  14891. * Builds the default DOM `className`.
  14892. *
  14893. * @return {string}
  14894. * The DOM `className` for this object.
  14895. */
  14896. buildCSSClass() {
  14897. return `vjs-captions-button ${super.buildCSSClass()}`;
  14898. }
  14899. buildWrapperCSSClass() {
  14900. return `vjs-captions-button ${super.buildWrapperCSSClass()}`;
  14901. }
  14902. /**
  14903. * Create caption menu items
  14904. *
  14905. * @return {CaptionSettingsMenuItem[]}
  14906. * The array of current menu items.
  14907. */
  14908. createItems() {
  14909. const items = [];
  14910. if (!(this.player().tech_ && this.player().tech_.featuresNativeTextTracks) && this.player().getChild('textTrackSettings')) {
  14911. items.push(new CaptionSettingsMenuItem(this.player_, {
  14912. kind: this.kind_
  14913. }));
  14914. this.hideThreshold_ += 1;
  14915. }
  14916. return super.createItems(items);
  14917. }
  14918. }
  14919. /**
  14920. * `kind` of TextTrack to look for to associate it with this menu.
  14921. *
  14922. * @type {string}
  14923. * @private
  14924. */
  14925. CaptionsButton.prototype.kind_ = 'captions';
  14926. /**
  14927. * The text that should display over the `CaptionsButton`s controls. Added for localization.
  14928. *
  14929. * @type {string}
  14930. * @protected
  14931. */
  14932. CaptionsButton.prototype.controlText_ = 'Captions';
  14933. Component.registerComponent('CaptionsButton', CaptionsButton);
  14934. /**
  14935. * @file subs-caps-menu-item.js
  14936. */
  14937. /**
  14938. * SubsCapsMenuItem has an [cc] icon to distinguish captions from subtitles
  14939. * in the SubsCapsMenu.
  14940. *
  14941. * @extends TextTrackMenuItem
  14942. */
  14943. class SubsCapsMenuItem extends TextTrackMenuItem {
  14944. createEl(type, props, attrs) {
  14945. const el = super.createEl(type, props, attrs);
  14946. const parentSpan = el.querySelector('.vjs-menu-item-text');
  14947. if (this.options_.track.kind === 'captions') {
  14948. if (this.player_.options_.experimentalSvgIcons) {
  14949. this.setIcon('captions', el);
  14950. } else {
  14951. parentSpan.appendChild(createEl('span', {
  14952. className: 'vjs-icon-placeholder'
  14953. }, {
  14954. 'aria-hidden': true
  14955. }));
  14956. }
  14957. parentSpan.appendChild(createEl('span', {
  14958. className: 'vjs-control-text',
  14959. // space added as the text will visually flow with the
  14960. // label
  14961. textContent: ` ${this.localize('Captions')}`
  14962. }));
  14963. }
  14964. return el;
  14965. }
  14966. }
  14967. Component.registerComponent('SubsCapsMenuItem', SubsCapsMenuItem);
  14968. /**
  14969. * @file sub-caps-button.js
  14970. */
  14971. /**
  14972. * The button component for toggling and selecting captions and/or subtitles
  14973. *
  14974. * @extends TextTrackButton
  14975. */
  14976. class SubsCapsButton extends TextTrackButton {
  14977. /**
  14978. * Creates an instance of this class.
  14979. *
  14980. * @param { import('../../player').default } player
  14981. * The `Player` that this class should be attached to.
  14982. *
  14983. * @param {Object} [options]
  14984. * The key/value store of player options.
  14985. *
  14986. * @param {Function} [ready]
  14987. * The function to call when this component is ready.
  14988. */
  14989. constructor(player, options = {}) {
  14990. super(player, options);
  14991. // Although North America uses "captions" in most cases for
  14992. // "captions and subtitles" other locales use "subtitles"
  14993. this.label_ = 'subtitles';
  14994. this.setIcon('subtitles');
  14995. if (['en', 'en-us', 'en-ca', 'fr-ca'].indexOf(this.player_.language_) > -1) {
  14996. this.label_ = 'captions';
  14997. this.setIcon('captions');
  14998. }
  14999. this.menuButton_.controlText(toTitleCase(this.label_));
  15000. }
  15001. /**
  15002. * Builds the default DOM `className`.
  15003. *
  15004. * @return {string}
  15005. * The DOM `className` for this object.
  15006. */
  15007. buildCSSClass() {
  15008. return `vjs-subs-caps-button ${super.buildCSSClass()}`;
  15009. }
  15010. buildWrapperCSSClass() {
  15011. return `vjs-subs-caps-button ${super.buildWrapperCSSClass()}`;
  15012. }
  15013. /**
  15014. * Create caption/subtitles menu items
  15015. *
  15016. * @return {CaptionSettingsMenuItem[]}
  15017. * The array of current menu items.
  15018. */
  15019. createItems() {
  15020. let items = [];
  15021. if (!(this.player().tech_ && this.player().tech_.featuresNativeTextTracks) && this.player().getChild('textTrackSettings')) {
  15022. items.push(new CaptionSettingsMenuItem(this.player_, {
  15023. kind: this.label_
  15024. }));
  15025. this.hideThreshold_ += 1;
  15026. }
  15027. items = super.createItems(items, SubsCapsMenuItem);
  15028. return items;
  15029. }
  15030. }
  15031. /**
  15032. * `kind`s of TextTrack to look for to associate it with this menu.
  15033. *
  15034. * @type {array}
  15035. * @private
  15036. */
  15037. SubsCapsButton.prototype.kinds_ = ['captions', 'subtitles'];
  15038. /**
  15039. * The text that should display over the `SubsCapsButton`s controls.
  15040. *
  15041. *
  15042. * @type {string}
  15043. * @protected
  15044. */
  15045. SubsCapsButton.prototype.controlText_ = 'Subtitles';
  15046. Component.registerComponent('SubsCapsButton', SubsCapsButton);
  15047. /**
  15048. * @file audio-track-menu-item.js
  15049. */
  15050. /**
  15051. * An {@link AudioTrack} {@link MenuItem}
  15052. *
  15053. * @extends MenuItem
  15054. */
  15055. class AudioTrackMenuItem extends MenuItem {
  15056. /**
  15057. * Creates an instance of this class.
  15058. *
  15059. * @param { import('../../player').default } player
  15060. * The `Player` that this class should be attached to.
  15061. *
  15062. * @param {Object} [options]
  15063. * The key/value store of player options.
  15064. */
  15065. constructor(player, options) {
  15066. const track = options.track;
  15067. const tracks = player.audioTracks();
  15068. // Modify options for parent MenuItem class's init.
  15069. options.label = track.label || track.language || 'Unknown';
  15070. options.selected = track.enabled;
  15071. super(player, options);
  15072. this.track = track;
  15073. this.addClass(`vjs-${track.kind}-menu-item`);
  15074. const changeHandler = (...args) => {
  15075. this.handleTracksChange.apply(this, args);
  15076. };
  15077. tracks.addEventListener('change', changeHandler);
  15078. this.on('dispose', () => {
  15079. tracks.removeEventListener('change', changeHandler);
  15080. });
  15081. }
  15082. createEl(type, props, attrs) {
  15083. const el = super.createEl(type, props, attrs);
  15084. const parentSpan = el.querySelector('.vjs-menu-item-text');
  15085. if (['main-desc', 'description'].indexOf(this.options_.track.kind) >= 0) {
  15086. parentSpan.appendChild(createEl('span', {
  15087. className: 'vjs-icon-placeholder'
  15088. }, {
  15089. 'aria-hidden': true
  15090. }));
  15091. parentSpan.appendChild(createEl('span', {
  15092. className: 'vjs-control-text',
  15093. textContent: ' ' + this.localize('Descriptions')
  15094. }));
  15095. }
  15096. return el;
  15097. }
  15098. /**
  15099. * This gets called when an `AudioTrackMenuItem is "clicked". See {@link ClickableComponent}
  15100. * for more detailed information on what a click can be.
  15101. *
  15102. * @param {Event} [event]
  15103. * The `keydown`, `tap`, or `click` event that caused this function to be
  15104. * called.
  15105. *
  15106. * @listens tap
  15107. * @listens click
  15108. */
  15109. handleClick(event) {
  15110. super.handleClick(event);
  15111. // the audio track list will automatically toggle other tracks
  15112. // off for us.
  15113. this.track.enabled = true;
  15114. // when native audio tracks are used, we want to make sure that other tracks are turned off
  15115. if (this.player_.tech_.featuresNativeAudioTracks) {
  15116. const tracks = this.player_.audioTracks();
  15117. for (let i = 0; i < tracks.length; i++) {
  15118. const track = tracks[i];
  15119. // skip the current track since we enabled it above
  15120. if (track === this.track) {
  15121. continue;
  15122. }
  15123. track.enabled = track === this.track;
  15124. }
  15125. }
  15126. }
  15127. /**
  15128. * Handle any {@link AudioTrack} change.
  15129. *
  15130. * @param {Event} [event]
  15131. * The {@link AudioTrackList#change} event that caused this to run.
  15132. *
  15133. * @listens AudioTrackList#change
  15134. */
  15135. handleTracksChange(event) {
  15136. this.selected(this.track.enabled);
  15137. }
  15138. }
  15139. Component.registerComponent('AudioTrackMenuItem', AudioTrackMenuItem);
  15140. /**
  15141. * @file audio-track-button.js
  15142. */
  15143. /**
  15144. * The base class for buttons that toggle specific {@link AudioTrack} types.
  15145. *
  15146. * @extends TrackButton
  15147. */
  15148. class AudioTrackButton extends TrackButton {
  15149. /**
  15150. * Creates an instance of this class.
  15151. *
  15152. * @param {Player} player
  15153. * The `Player` that this class should be attached to.
  15154. *
  15155. * @param {Object} [options={}]
  15156. * The key/value store of player options.
  15157. */
  15158. constructor(player, options = {}) {
  15159. options.tracks = player.audioTracks();
  15160. super(player, options);
  15161. this.setIcon('audio');
  15162. }
  15163. /**
  15164. * Builds the default DOM `className`.
  15165. *
  15166. * @return {string}
  15167. * The DOM `className` for this object.
  15168. */
  15169. buildCSSClass() {
  15170. return `vjs-audio-button ${super.buildCSSClass()}`;
  15171. }
  15172. buildWrapperCSSClass() {
  15173. return `vjs-audio-button ${super.buildWrapperCSSClass()}`;
  15174. }
  15175. /**
  15176. * Create a menu item for each audio track
  15177. *
  15178. * @param {AudioTrackMenuItem[]} [items=[]]
  15179. * An array of existing menu items to use.
  15180. *
  15181. * @return {AudioTrackMenuItem[]}
  15182. * An array of menu items
  15183. */
  15184. createItems(items = []) {
  15185. // if there's only one audio track, there no point in showing it
  15186. this.hideThreshold_ = 1;
  15187. const tracks = this.player_.audioTracks();
  15188. for (let i = 0; i < tracks.length; i++) {
  15189. const track = tracks[i];
  15190. items.push(new AudioTrackMenuItem(this.player_, {
  15191. track,
  15192. // MenuItem is selectable
  15193. selectable: true,
  15194. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  15195. multiSelectable: false
  15196. }));
  15197. }
  15198. return items;
  15199. }
  15200. }
  15201. /**
  15202. * The text that should display over the `AudioTrackButton`s controls. Added for localization.
  15203. *
  15204. * @type {string}
  15205. * @protected
  15206. */
  15207. AudioTrackButton.prototype.controlText_ = 'Audio Track';
  15208. Component.registerComponent('AudioTrackButton', AudioTrackButton);
  15209. /**
  15210. * @file playback-rate-menu-item.js
  15211. */
  15212. /**
  15213. * The specific menu item type for selecting a playback rate.
  15214. *
  15215. * @extends MenuItem
  15216. */
  15217. class PlaybackRateMenuItem extends MenuItem {
  15218. /**
  15219. * Creates an instance of this class.
  15220. *
  15221. * @param { import('../../player').default } player
  15222. * The `Player` that this class should be attached to.
  15223. *
  15224. * @param {Object} [options]
  15225. * The key/value store of player options.
  15226. */
  15227. constructor(player, options) {
  15228. const label = options.rate;
  15229. const rate = parseFloat(label, 10);
  15230. // Modify options for parent MenuItem class's init.
  15231. options.label = label;
  15232. options.selected = rate === player.playbackRate();
  15233. options.selectable = true;
  15234. options.multiSelectable = false;
  15235. super(player, options);
  15236. this.label = label;
  15237. this.rate = rate;
  15238. this.on(player, 'ratechange', e => this.update(e));
  15239. }
  15240. /**
  15241. * This gets called when an `PlaybackRateMenuItem` is "clicked". See
  15242. * {@link ClickableComponent} for more detailed information on what a click can be.
  15243. *
  15244. * @param {Event} [event]
  15245. * The `keydown`, `tap`, or `click` event that caused this function to be
  15246. * called.
  15247. *
  15248. * @listens tap
  15249. * @listens click
  15250. */
  15251. handleClick(event) {
  15252. super.handleClick();
  15253. this.player().playbackRate(this.rate);
  15254. }
  15255. /**
  15256. * Update the PlaybackRateMenuItem when the playbackrate changes.
  15257. *
  15258. * @param {Event} [event]
  15259. * The `ratechange` event that caused this function to run.
  15260. *
  15261. * @listens Player#ratechange
  15262. */
  15263. update(event) {
  15264. this.selected(this.player().playbackRate() === this.rate);
  15265. }
  15266. }
  15267. /**
  15268. * The text that should display over the `PlaybackRateMenuItem`s controls. Added for localization.
  15269. *
  15270. * @type {string}
  15271. * @private
  15272. */
  15273. PlaybackRateMenuItem.prototype.contentElType = 'button';
  15274. Component.registerComponent('PlaybackRateMenuItem', PlaybackRateMenuItem);
  15275. /**
  15276. * @file playback-rate-menu-button.js
  15277. */
  15278. /**
  15279. * The component for controlling the playback rate.
  15280. *
  15281. * @extends MenuButton
  15282. */
  15283. class PlaybackRateMenuButton extends MenuButton {
  15284. /**
  15285. * Creates an instance of this class.
  15286. *
  15287. * @param { import('../../player').default } player
  15288. * The `Player` that this class should be attached to.
  15289. *
  15290. * @param {Object} [options]
  15291. * The key/value store of player options.
  15292. */
  15293. constructor(player, options) {
  15294. super(player, options);
  15295. this.menuButton_.el_.setAttribute('aria-describedby', this.labelElId_);
  15296. this.updateVisibility();
  15297. this.updateLabel();
  15298. this.on(player, 'loadstart', e => this.updateVisibility(e));
  15299. this.on(player, 'ratechange', e => this.updateLabel(e));
  15300. this.on(player, 'playbackrateschange', e => this.handlePlaybackRateschange(e));
  15301. }
  15302. /**
  15303. * Create the `Component`'s DOM element
  15304. *
  15305. * @return {Element}
  15306. * The element that was created.
  15307. */
  15308. createEl() {
  15309. const el = super.createEl();
  15310. this.labelElId_ = 'vjs-playback-rate-value-label-' + this.id_;
  15311. this.labelEl_ = createEl('div', {
  15312. className: 'vjs-playback-rate-value',
  15313. id: this.labelElId_,
  15314. textContent: '1x'
  15315. });
  15316. el.appendChild(this.labelEl_);
  15317. return el;
  15318. }
  15319. dispose() {
  15320. this.labelEl_ = null;
  15321. super.dispose();
  15322. }
  15323. /**
  15324. * Builds the default DOM `className`.
  15325. *
  15326. * @return {string}
  15327. * The DOM `className` for this object.
  15328. */
  15329. buildCSSClass() {
  15330. return `vjs-playback-rate ${super.buildCSSClass()}`;
  15331. }
  15332. buildWrapperCSSClass() {
  15333. return `vjs-playback-rate ${super.buildWrapperCSSClass()}`;
  15334. }
  15335. /**
  15336. * Create the list of menu items. Specific to each subclass.
  15337. *
  15338. */
  15339. createItems() {
  15340. const rates = this.playbackRates();
  15341. const items = [];
  15342. for (let i = rates.length - 1; i >= 0; i--) {
  15343. items.push(new PlaybackRateMenuItem(this.player(), {
  15344. rate: rates[i] + 'x'
  15345. }));
  15346. }
  15347. return items;
  15348. }
  15349. /**
  15350. * On playbackrateschange, update the menu to account for the new items.
  15351. *
  15352. * @listens Player#playbackrateschange
  15353. */
  15354. handlePlaybackRateschange(event) {
  15355. this.update();
  15356. }
  15357. /**
  15358. * Get possible playback rates
  15359. *
  15360. * @return {Array}
  15361. * All possible playback rates
  15362. */
  15363. playbackRates() {
  15364. const player = this.player();
  15365. return player.playbackRates && player.playbackRates() || [];
  15366. }
  15367. /**
  15368. * Get whether playback rates is supported by the tech
  15369. * and an array of playback rates exists
  15370. *
  15371. * @return {boolean}
  15372. * Whether changing playback rate is supported
  15373. */
  15374. playbackRateSupported() {
  15375. return this.player().tech_ && this.player().tech_.featuresPlaybackRate && this.playbackRates() && this.playbackRates().length > 0;
  15376. }
  15377. /**
  15378. * Hide playback rate controls when they're no playback rate options to select
  15379. *
  15380. * @param {Event} [event]
  15381. * The event that caused this function to run.
  15382. *
  15383. * @listens Player#loadstart
  15384. */
  15385. updateVisibility(event) {
  15386. if (this.playbackRateSupported()) {
  15387. this.removeClass('vjs-hidden');
  15388. } else {
  15389. this.addClass('vjs-hidden');
  15390. }
  15391. }
  15392. /**
  15393. * Update button label when rate changed
  15394. *
  15395. * @param {Event} [event]
  15396. * The event that caused this function to run.
  15397. *
  15398. * @listens Player#ratechange
  15399. */
  15400. updateLabel(event) {
  15401. if (this.playbackRateSupported()) {
  15402. this.labelEl_.textContent = this.player().playbackRate() + 'x';
  15403. }
  15404. }
  15405. }
  15406. /**
  15407. * The text that should display over the `PlaybackRateMenuButton`s controls.
  15408. *
  15409. * Added for localization.
  15410. *
  15411. * @type {string}
  15412. * @protected
  15413. */
  15414. PlaybackRateMenuButton.prototype.controlText_ = 'Playback Rate';
  15415. Component.registerComponent('PlaybackRateMenuButton', PlaybackRateMenuButton);
  15416. /**
  15417. * @file spacer.js
  15418. */
  15419. /**
  15420. * Just an empty spacer element that can be used as an append point for plugins, etc.
  15421. * Also can be used to create space between elements when necessary.
  15422. *
  15423. * @extends Component
  15424. */
  15425. class Spacer extends Component {
  15426. /**
  15427. * Builds the default DOM `className`.
  15428. *
  15429. * @return {string}
  15430. * The DOM `className` for this object.
  15431. */
  15432. buildCSSClass() {
  15433. return `vjs-spacer ${super.buildCSSClass()}`;
  15434. }
  15435. /**
  15436. * Create the `Component`'s DOM element
  15437. *
  15438. * @return {Element}
  15439. * The element that was created.
  15440. */
  15441. createEl(tag = 'div', props = {}, attributes = {}) {
  15442. if (!props.className) {
  15443. props.className = this.buildCSSClass();
  15444. }
  15445. return super.createEl(tag, props, attributes);
  15446. }
  15447. }
  15448. Component.registerComponent('Spacer', Spacer);
  15449. /**
  15450. * @file custom-control-spacer.js
  15451. */
  15452. /**
  15453. * Spacer specifically meant to be used as an insertion point for new plugins, etc.
  15454. *
  15455. * @extends Spacer
  15456. */
  15457. class CustomControlSpacer extends Spacer {
  15458. /**
  15459. * Builds the default DOM `className`.
  15460. *
  15461. * @return {string}
  15462. * The DOM `className` for this object.
  15463. */
  15464. buildCSSClass() {
  15465. return `vjs-custom-control-spacer ${super.buildCSSClass()}`;
  15466. }
  15467. /**
  15468. * Create the `Component`'s DOM element
  15469. *
  15470. * @return {Element}
  15471. * The element that was created.
  15472. */
  15473. createEl() {
  15474. return super.createEl('div', {
  15475. className: this.buildCSSClass(),
  15476. // No-flex/table-cell mode requires there be some content
  15477. // in the cell to fill the remaining space of the table.
  15478. textContent: '\u00a0'
  15479. });
  15480. }
  15481. }
  15482. Component.registerComponent('CustomControlSpacer', CustomControlSpacer);
  15483. /**
  15484. * @file control-bar.js
  15485. */
  15486. /**
  15487. * Container of main controls.
  15488. *
  15489. * @extends Component
  15490. */
  15491. class ControlBar extends Component {
  15492. /**
  15493. * Create the `Component`'s DOM element
  15494. *
  15495. * @return {Element}
  15496. * The element that was created.
  15497. */
  15498. createEl() {
  15499. return super.createEl('div', {
  15500. className: 'vjs-control-bar',
  15501. dir: 'ltr'
  15502. });
  15503. }
  15504. }
  15505. /**
  15506. * Default options for `ControlBar`
  15507. *
  15508. * @type {Object}
  15509. * @private
  15510. */
  15511. ControlBar.prototype.options_ = {
  15512. children: ['playToggle', 'skipBackward', 'skipForward', 'volumePanel', 'currentTimeDisplay', 'timeDivider', 'durationDisplay', 'progressControl', 'liveDisplay', 'seekToLive', 'remainingTimeDisplay', 'customControlSpacer', 'playbackRateMenuButton', 'chaptersButton', 'descriptionsButton', 'subsCapsButton', 'audioTrackButton', 'pictureInPictureToggle', 'fullscreenToggle']
  15513. };
  15514. Component.registerComponent('ControlBar', ControlBar);
  15515. /**
  15516. * @file error-display.js
  15517. */
  15518. /**
  15519. * A display that indicates an error has occurred. This means that the video
  15520. * is unplayable.
  15521. *
  15522. * @extends ModalDialog
  15523. */
  15524. class ErrorDisplay extends ModalDialog {
  15525. /**
  15526. * Creates an instance of this class.
  15527. *
  15528. * @param { import('./player').default } player
  15529. * The `Player` that this class should be attached to.
  15530. *
  15531. * @param {Object} [options]
  15532. * The key/value store of player options.
  15533. */
  15534. constructor(player, options) {
  15535. super(player, options);
  15536. this.on(player, 'error', e => {
  15537. this.close();
  15538. this.open(e);
  15539. });
  15540. }
  15541. /**
  15542. * Builds the default DOM `className`.
  15543. *
  15544. * @return {string}
  15545. * The DOM `className` for this object.
  15546. *
  15547. * @deprecated Since version 5.
  15548. */
  15549. buildCSSClass() {
  15550. return `vjs-error-display ${super.buildCSSClass()}`;
  15551. }
  15552. /**
  15553. * Gets the localized error message based on the `Player`s error.
  15554. *
  15555. * @return {string}
  15556. * The `Player`s error message localized or an empty string.
  15557. */
  15558. content() {
  15559. const error = this.player().error();
  15560. return error ? this.localize(error.message) : '';
  15561. }
  15562. }
  15563. /**
  15564. * The default options for an `ErrorDisplay`.
  15565. *
  15566. * @private
  15567. */
  15568. ErrorDisplay.prototype.options_ = Object.assign({}, ModalDialog.prototype.options_, {
  15569. pauseOnOpen: false,
  15570. fillAlways: true,
  15571. temporary: false,
  15572. uncloseable: true
  15573. });
  15574. Component.registerComponent('ErrorDisplay', ErrorDisplay);
  15575. /**
  15576. * @file text-track-settings.js
  15577. */
  15578. const LOCAL_STORAGE_KEY = 'vjs-text-track-settings';
  15579. const COLOR_BLACK = ['#000', 'Black'];
  15580. const COLOR_BLUE = ['#00F', 'Blue'];
  15581. const COLOR_CYAN = ['#0FF', 'Cyan'];
  15582. const COLOR_GREEN = ['#0F0', 'Green'];
  15583. const COLOR_MAGENTA = ['#F0F', 'Magenta'];
  15584. const COLOR_RED = ['#F00', 'Red'];
  15585. const COLOR_WHITE = ['#FFF', 'White'];
  15586. const COLOR_YELLOW = ['#FF0', 'Yellow'];
  15587. const OPACITY_OPAQUE = ['1', 'Opaque'];
  15588. const OPACITY_SEMI = ['0.5', 'Semi-Transparent'];
  15589. const OPACITY_TRANS = ['0', 'Transparent'];
  15590. // Configuration for the various <select> elements in the DOM of this component.
  15591. //
  15592. // Possible keys include:
  15593. //
  15594. // `default`:
  15595. // The default option index. Only needs to be provided if not zero.
  15596. // `parser`:
  15597. // A function which is used to parse the value from the selected option in
  15598. // a customized way.
  15599. // `selector`:
  15600. // The selector used to find the associated <select> element.
  15601. const selectConfigs = {
  15602. backgroundColor: {
  15603. selector: '.vjs-bg-color > select',
  15604. id: 'captions-background-color-%s',
  15605. label: 'Color',
  15606. options: [COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_MAGENTA, COLOR_CYAN]
  15607. },
  15608. backgroundOpacity: {
  15609. selector: '.vjs-bg-opacity > select',
  15610. id: 'captions-background-opacity-%s',
  15611. label: 'Opacity',
  15612. options: [OPACITY_OPAQUE, OPACITY_SEMI, OPACITY_TRANS]
  15613. },
  15614. color: {
  15615. selector: '.vjs-text-color > select',
  15616. id: 'captions-foreground-color-%s',
  15617. label: 'Color',
  15618. options: [COLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_MAGENTA, COLOR_CYAN]
  15619. },
  15620. edgeStyle: {
  15621. selector: '.vjs-edge-style > select',
  15622. id: '%s',
  15623. label: 'Text Edge Style',
  15624. options: [['none', 'None'], ['raised', 'Raised'], ['depressed', 'Depressed'], ['uniform', 'Uniform'], ['dropshadow', 'Drop shadow']]
  15625. },
  15626. fontFamily: {
  15627. selector: '.vjs-font-family > select',
  15628. id: 'captions-font-family-%s',
  15629. label: 'Font Family',
  15630. options: [['proportionalSansSerif', 'Proportional Sans-Serif'], ['monospaceSansSerif', 'Monospace Sans-Serif'], ['proportionalSerif', 'Proportional Serif'], ['monospaceSerif', 'Monospace Serif'], ['casual', 'Casual'], ['script', 'Script'], ['small-caps', 'Small Caps']]
  15631. },
  15632. fontPercent: {
  15633. selector: '.vjs-font-percent > select',
  15634. id: 'captions-font-size-%s',
  15635. label: 'Font Size',
  15636. options: [['0.50', '50%'], ['0.75', '75%'], ['1.00', '100%'], ['1.25', '125%'], ['1.50', '150%'], ['1.75', '175%'], ['2.00', '200%'], ['3.00', '300%'], ['4.00', '400%']],
  15637. default: 2,
  15638. parser: v => v === '1.00' ? null : Number(v)
  15639. },
  15640. textOpacity: {
  15641. selector: '.vjs-text-opacity > select',
  15642. id: 'captions-foreground-opacity-%s',
  15643. label: 'Opacity',
  15644. options: [OPACITY_OPAQUE, OPACITY_SEMI]
  15645. },
  15646. // Options for this object are defined below.
  15647. windowColor: {
  15648. selector: '.vjs-window-color > select',
  15649. id: 'captions-window-color-%s',
  15650. label: 'Color'
  15651. },
  15652. // Options for this object are defined below.
  15653. windowOpacity: {
  15654. selector: '.vjs-window-opacity > select',
  15655. id: 'captions-window-opacity-%s',
  15656. label: 'Opacity',
  15657. options: [OPACITY_TRANS, OPACITY_SEMI, OPACITY_OPAQUE]
  15658. }
  15659. };
  15660. selectConfigs.windowColor.options = selectConfigs.backgroundColor.options;
  15661. /**
  15662. * Get the actual value of an option.
  15663. *
  15664. * @param {string} value
  15665. * The value to get
  15666. *
  15667. * @param {Function} [parser]
  15668. * Optional function to adjust the value.
  15669. *
  15670. * @return {*}
  15671. * - Will be `undefined` if no value exists
  15672. * - Will be `undefined` if the given value is "none".
  15673. * - Will be the actual value otherwise.
  15674. *
  15675. * @private
  15676. */
  15677. function parseOptionValue(value, parser) {
  15678. if (parser) {
  15679. value = parser(value);
  15680. }
  15681. if (value && value !== 'none') {
  15682. return value;
  15683. }
  15684. }
  15685. /**
  15686. * Gets the value of the selected <option> element within a <select> element.
  15687. *
  15688. * @param {Element} el
  15689. * the element to look in
  15690. *
  15691. * @param {Function} [parser]
  15692. * Optional function to adjust the value.
  15693. *
  15694. * @return {*}
  15695. * - Will be `undefined` if no value exists
  15696. * - Will be `undefined` if the given value is "none".
  15697. * - Will be the actual value otherwise.
  15698. *
  15699. * @private
  15700. */
  15701. function getSelectedOptionValue(el, parser) {
  15702. const value = el.options[el.options.selectedIndex].value;
  15703. return parseOptionValue(value, parser);
  15704. }
  15705. /**
  15706. * Sets the selected <option> element within a <select> element based on a
  15707. * given value.
  15708. *
  15709. * @param {Element} el
  15710. * The element to look in.
  15711. *
  15712. * @param {string} value
  15713. * the property to look on.
  15714. *
  15715. * @param {Function} [parser]
  15716. * Optional function to adjust the value before comparing.
  15717. *
  15718. * @private
  15719. */
  15720. function setSelectedOption(el, value, parser) {
  15721. if (!value) {
  15722. return;
  15723. }
  15724. for (let i = 0; i < el.options.length; i++) {
  15725. if (parseOptionValue(el.options[i].value, parser) === value) {
  15726. el.selectedIndex = i;
  15727. break;
  15728. }
  15729. }
  15730. }
  15731. /**
  15732. * Manipulate Text Tracks settings.
  15733. *
  15734. * @extends ModalDialog
  15735. */
  15736. class TextTrackSettings extends ModalDialog {
  15737. /**
  15738. * Creates an instance of this class.
  15739. *
  15740. * @param { import('../player').default } player
  15741. * The `Player` that this class should be attached to.
  15742. *
  15743. * @param {Object} [options]
  15744. * The key/value store of player options.
  15745. */
  15746. constructor(player, options) {
  15747. options.temporary = false;
  15748. super(player, options);
  15749. this.updateDisplay = this.updateDisplay.bind(this);
  15750. // fill the modal and pretend we have opened it
  15751. this.fill();
  15752. this.hasBeenOpened_ = this.hasBeenFilled_ = true;
  15753. this.endDialog = createEl('p', {
  15754. className: 'vjs-control-text',
  15755. textContent: this.localize('End of dialog window.')
  15756. });
  15757. this.el().appendChild(this.endDialog);
  15758. this.setDefaults();
  15759. // Grab `persistTextTrackSettings` from the player options if not passed in child options
  15760. if (options.persistTextTrackSettings === undefined) {
  15761. this.options_.persistTextTrackSettings = this.options_.playerOptions.persistTextTrackSettings;
  15762. }
  15763. this.on(this.$('.vjs-done-button'), 'click', () => {
  15764. this.saveSettings();
  15765. this.close();
  15766. });
  15767. this.on(this.$('.vjs-default-button'), 'click', () => {
  15768. this.setDefaults();
  15769. this.updateDisplay();
  15770. });
  15771. each(selectConfigs, config => {
  15772. this.on(this.$(config.selector), 'change', this.updateDisplay);
  15773. });
  15774. if (this.options_.persistTextTrackSettings) {
  15775. this.restoreSettings();
  15776. }
  15777. }
  15778. dispose() {
  15779. this.endDialog = null;
  15780. super.dispose();
  15781. }
  15782. /**
  15783. * Create a <select> element with configured options.
  15784. *
  15785. * @param {string} key
  15786. * Configuration key to use during creation.
  15787. *
  15788. * @param {string} [legendId]
  15789. * Id of associated <legend>.
  15790. *
  15791. * @param {string} [type=label]
  15792. * Type of labelling element, `label` or `legend`
  15793. *
  15794. * @return {string}
  15795. * An HTML string.
  15796. *
  15797. * @private
  15798. */
  15799. createElSelect_(key, legendId = '', type = 'label') {
  15800. const config = selectConfigs[key];
  15801. const id = config.id.replace('%s', this.id_);
  15802. const selectLabelledbyIds = [legendId, id].join(' ').trim();
  15803. const guid = `vjs_select_${newGUID()}`;
  15804. return [`<${type} id="${id}"${type === 'label' ? ` for="${guid}" class="vjs-label"` : ''}>`, this.localize(config.label), `</${type}>`, `<select aria-labelledby="${selectLabelledbyIds}" id="${guid}">`].concat(config.options.map(o => {
  15805. const optionId = id + '-' + o[1].replace(/\W+/g, '');
  15806. return [`<option id="${optionId}" value="${o[0]}" `, `aria-labelledby="${selectLabelledbyIds} ${optionId}">`, this.localize(o[1]), '</option>'].join('');
  15807. })).concat('</select>').join('');
  15808. }
  15809. /**
  15810. * Create foreground color element for the component
  15811. *
  15812. * @return {string}
  15813. * An HTML string.
  15814. *
  15815. * @private
  15816. */
  15817. createElFgColor_() {
  15818. const legendId = `captions-text-legend-${this.id_}`;
  15819. return ['<fieldset class="vjs-fg vjs-track-setting">', `<legend id="${legendId}">`, this.localize('Text'), '</legend>', '<span class="vjs-text-color">', this.createElSelect_('color', legendId), '</span>', '<span class="vjs-text-opacity vjs-opacity">', this.createElSelect_('textOpacity', legendId), '</span>', '</fieldset>'].join('');
  15820. }
  15821. /**
  15822. * Create background color element for the component
  15823. *
  15824. * @return {string}
  15825. * An HTML string.
  15826. *
  15827. * @private
  15828. */
  15829. createElBgColor_() {
  15830. const legendId = `captions-background-${this.id_}`;
  15831. return ['<fieldset class="vjs-bg vjs-track-setting">', `<legend id="${legendId}">`, this.localize('Text Background'), '</legend>', '<span class="vjs-bg-color">', this.createElSelect_('backgroundColor', legendId), '</span>', '<span class="vjs-bg-opacity vjs-opacity">', this.createElSelect_('backgroundOpacity', legendId), '</span>', '</fieldset>'].join('');
  15832. }
  15833. /**
  15834. * Create window color element for the component
  15835. *
  15836. * @return {string}
  15837. * An HTML string.
  15838. *
  15839. * @private
  15840. */
  15841. createElWinColor_() {
  15842. const legendId = `captions-window-${this.id_}`;
  15843. return ['<fieldset class="vjs-window vjs-track-setting">', `<legend id="${legendId}">`, this.localize('Caption Area Background'), '</legend>', '<span class="vjs-window-color">', this.createElSelect_('windowColor', legendId), '</span>', '<span class="vjs-window-opacity vjs-opacity">', this.createElSelect_('windowOpacity', legendId), '</span>', '</fieldset>'].join('');
  15844. }
  15845. /**
  15846. * Create color elements for the component
  15847. *
  15848. * @return {Element}
  15849. * The element that was created
  15850. *
  15851. * @private
  15852. */
  15853. createElColors_() {
  15854. return createEl('div', {
  15855. className: 'vjs-track-settings-colors',
  15856. innerHTML: [this.createElFgColor_(), this.createElBgColor_(), this.createElWinColor_()].join('')
  15857. });
  15858. }
  15859. /**
  15860. * Create font elements for the component
  15861. *
  15862. * @return {Element}
  15863. * The element that was created.
  15864. *
  15865. * @private
  15866. */
  15867. createElFont_() {
  15868. return createEl('div', {
  15869. className: 'vjs-track-settings-font',
  15870. innerHTML: ['<fieldset class="vjs-font-percent vjs-track-setting">', this.createElSelect_('fontPercent', '', 'legend'), '</fieldset>', '<fieldset class="vjs-edge-style vjs-track-setting">', this.createElSelect_('edgeStyle', '', 'legend'), '</fieldset>', '<fieldset class="vjs-font-family vjs-track-setting">', this.createElSelect_('fontFamily', '', 'legend'), '</fieldset>'].join('')
  15871. });
  15872. }
  15873. /**
  15874. * Create controls for the component
  15875. *
  15876. * @return {Element}
  15877. * The element that was created.
  15878. *
  15879. * @private
  15880. */
  15881. createElControls_() {
  15882. const defaultsDescription = this.localize('restore all settings to the default values');
  15883. return createEl('div', {
  15884. className: 'vjs-track-settings-controls',
  15885. innerHTML: [`<button type="button" class="vjs-default-button" title="${defaultsDescription}">`, this.localize('Reset'), `<span class="vjs-control-text"> ${defaultsDescription}</span>`, '</button>', `<button type="button" class="vjs-done-button">${this.localize('Done')}</button>`].join('')
  15886. });
  15887. }
  15888. content() {
  15889. return [this.createElColors_(), this.createElFont_(), this.createElControls_()];
  15890. }
  15891. label() {
  15892. return this.localize('Caption Settings Dialog');
  15893. }
  15894. description() {
  15895. return this.localize('Beginning of dialog window. Escape will cancel and close the window.');
  15896. }
  15897. buildCSSClass() {
  15898. return super.buildCSSClass() + ' vjs-text-track-settings';
  15899. }
  15900. /**
  15901. * Gets an object of text track settings (or null).
  15902. *
  15903. * @return {Object}
  15904. * An object with config values parsed from the DOM or localStorage.
  15905. */
  15906. getValues() {
  15907. return reduce(selectConfigs, (accum, config, key) => {
  15908. const value = getSelectedOptionValue(this.$(config.selector), config.parser);
  15909. if (value !== undefined) {
  15910. accum[key] = value;
  15911. }
  15912. return accum;
  15913. }, {});
  15914. }
  15915. /**
  15916. * Sets text track settings from an object of values.
  15917. *
  15918. * @param {Object} values
  15919. * An object with config values parsed from the DOM or localStorage.
  15920. */
  15921. setValues(values) {
  15922. each(selectConfigs, (config, key) => {
  15923. setSelectedOption(this.$(config.selector), values[key], config.parser);
  15924. });
  15925. }
  15926. /**
  15927. * Sets all `<select>` elements to their default values.
  15928. */
  15929. setDefaults() {
  15930. each(selectConfigs, config => {
  15931. const index = config.hasOwnProperty('default') ? config.default : 0;
  15932. this.$(config.selector).selectedIndex = index;
  15933. });
  15934. }
  15935. /**
  15936. * Restore texttrack settings from localStorage
  15937. */
  15938. restoreSettings() {
  15939. let values;
  15940. try {
  15941. values = JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_KEY));
  15942. } catch (err) {
  15943. log.warn(err);
  15944. }
  15945. if (values) {
  15946. this.setValues(values);
  15947. }
  15948. }
  15949. /**
  15950. * Save text track settings to localStorage
  15951. */
  15952. saveSettings() {
  15953. if (!this.options_.persistTextTrackSettings) {
  15954. return;
  15955. }
  15956. const values = this.getValues();
  15957. try {
  15958. if (Object.keys(values).length) {
  15959. window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(values));
  15960. } else {
  15961. window.localStorage.removeItem(LOCAL_STORAGE_KEY);
  15962. }
  15963. } catch (err) {
  15964. log.warn(err);
  15965. }
  15966. }
  15967. /**
  15968. * Update display of text track settings
  15969. */
  15970. updateDisplay() {
  15971. const ttDisplay = this.player_.getChild('textTrackDisplay');
  15972. if (ttDisplay) {
  15973. ttDisplay.updateDisplay();
  15974. }
  15975. }
  15976. /**
  15977. * conditionally blur the element and refocus the captions button
  15978. *
  15979. * @private
  15980. */
  15981. conditionalBlur_() {
  15982. this.previouslyActiveEl_ = null;
  15983. const cb = this.player_.controlBar;
  15984. const subsCapsBtn = cb && cb.subsCapsButton;
  15985. const ccBtn = cb && cb.captionsButton;
  15986. if (subsCapsBtn) {
  15987. subsCapsBtn.focus();
  15988. } else if (ccBtn) {
  15989. ccBtn.focus();
  15990. }
  15991. }
  15992. /**
  15993. * Repopulate dialog with new localizations on languagechange
  15994. */
  15995. handleLanguagechange() {
  15996. this.fill();
  15997. }
  15998. }
  15999. Component.registerComponent('TextTrackSettings', TextTrackSettings);
  16000. /**
  16001. * @file resize-manager.js
  16002. */
  16003. /**
  16004. * A Resize Manager. It is in charge of triggering `playerresize` on the player in the right conditions.
  16005. *
  16006. * It'll either create an iframe and use a debounced resize handler on it or use the new {@link https://wicg.github.io/ResizeObserver/|ResizeObserver}.
  16007. *
  16008. * If the ResizeObserver is available natively, it will be used. A polyfill can be passed in as an option.
  16009. * If a `playerresize` event is not needed, the ResizeManager component can be removed from the player, see the example below.
  16010. *
  16011. * @example <caption>How to disable the resize manager</caption>
  16012. * const player = videojs('#vid', {
  16013. * resizeManager: false
  16014. * });
  16015. *
  16016. * @see {@link https://wicg.github.io/ResizeObserver/|ResizeObserver specification}
  16017. *
  16018. * @extends Component
  16019. */
  16020. class ResizeManager extends Component {
  16021. /**
  16022. * Create the ResizeManager.
  16023. *
  16024. * @param {Object} player
  16025. * The `Player` that this class should be attached to.
  16026. *
  16027. * @param {Object} [options]
  16028. * The key/value store of ResizeManager options.
  16029. *
  16030. * @param {Object} [options.ResizeObserver]
  16031. * A polyfill for ResizeObserver can be passed in here.
  16032. * If this is set to null it will ignore the native ResizeObserver and fall back to the iframe fallback.
  16033. */
  16034. constructor(player, options) {
  16035. let RESIZE_OBSERVER_AVAILABLE = options.ResizeObserver || window.ResizeObserver;
  16036. // if `null` was passed, we want to disable the ResizeObserver
  16037. if (options.ResizeObserver === null) {
  16038. RESIZE_OBSERVER_AVAILABLE = false;
  16039. }
  16040. // Only create an element when ResizeObserver isn't available
  16041. const options_ = merge({
  16042. createEl: !RESIZE_OBSERVER_AVAILABLE,
  16043. reportTouchActivity: false
  16044. }, options);
  16045. super(player, options_);
  16046. this.ResizeObserver = options.ResizeObserver || window.ResizeObserver;
  16047. this.loadListener_ = null;
  16048. this.resizeObserver_ = null;
  16049. this.debouncedHandler_ = debounce(() => {
  16050. this.resizeHandler();
  16051. }, 100, false, this);
  16052. if (RESIZE_OBSERVER_AVAILABLE) {
  16053. this.resizeObserver_ = new this.ResizeObserver(this.debouncedHandler_);
  16054. this.resizeObserver_.observe(player.el());
  16055. } else {
  16056. this.loadListener_ = () => {
  16057. if (!this.el_ || !this.el_.contentWindow) {
  16058. return;
  16059. }
  16060. const debouncedHandler_ = this.debouncedHandler_;
  16061. let unloadListener_ = this.unloadListener_ = function () {
  16062. off(this, 'resize', debouncedHandler_);
  16063. off(this, 'unload', unloadListener_);
  16064. unloadListener_ = null;
  16065. };
  16066. // safari and edge can unload the iframe before resizemanager dispose
  16067. // we have to dispose of event handlers correctly before that happens
  16068. on(this.el_.contentWindow, 'unload', unloadListener_);
  16069. on(this.el_.contentWindow, 'resize', debouncedHandler_);
  16070. };
  16071. this.one('load', this.loadListener_);
  16072. }
  16073. }
  16074. createEl() {
  16075. return super.createEl('iframe', {
  16076. className: 'vjs-resize-manager',
  16077. tabIndex: -1,
  16078. title: this.localize('No content')
  16079. }, {
  16080. 'aria-hidden': 'true'
  16081. });
  16082. }
  16083. /**
  16084. * Called when a resize is triggered on the iframe or a resize is observed via the ResizeObserver
  16085. *
  16086. * @fires Player#playerresize
  16087. */
  16088. resizeHandler() {
  16089. /**
  16090. * Called when the player size has changed
  16091. *
  16092. * @event Player#playerresize
  16093. * @type {Event}
  16094. */
  16095. // make sure player is still around to trigger
  16096. // prevents this from causing an error after dispose
  16097. if (!this.player_ || !this.player_.trigger) {
  16098. return;
  16099. }
  16100. this.player_.trigger('playerresize');
  16101. }
  16102. dispose() {
  16103. if (this.debouncedHandler_) {
  16104. this.debouncedHandler_.cancel();
  16105. }
  16106. if (this.resizeObserver_) {
  16107. if (this.player_.el()) {
  16108. this.resizeObserver_.unobserve(this.player_.el());
  16109. }
  16110. this.resizeObserver_.disconnect();
  16111. }
  16112. if (this.loadListener_) {
  16113. this.off('load', this.loadListener_);
  16114. }
  16115. if (this.el_ && this.el_.contentWindow && this.unloadListener_) {
  16116. this.unloadListener_.call(this.el_.contentWindow);
  16117. }
  16118. this.ResizeObserver = null;
  16119. this.resizeObserver = null;
  16120. this.debouncedHandler_ = null;
  16121. this.loadListener_ = null;
  16122. super.dispose();
  16123. }
  16124. }
  16125. Component.registerComponent('ResizeManager', ResizeManager);
  16126. const defaults = {
  16127. trackingThreshold: 20,
  16128. liveTolerance: 15
  16129. };
  16130. /*
  16131. track when we are at the live edge, and other helpers for live playback */
  16132. /**
  16133. * A class for checking live current time and determining when the player
  16134. * is at or behind the live edge.
  16135. */
  16136. class LiveTracker extends Component {
  16137. /**
  16138. * Creates an instance of this class.
  16139. *
  16140. * @param { import('./player').default } player
  16141. * The `Player` that this class should be attached to.
  16142. *
  16143. * @param {Object} [options]
  16144. * The key/value store of player options.
  16145. *
  16146. * @param {number} [options.trackingThreshold=20]
  16147. * Number of seconds of live window (seekableEnd - seekableStart) that
  16148. * media needs to have before the liveui will be shown.
  16149. *
  16150. * @param {number} [options.liveTolerance=15]
  16151. * Number of seconds behind live that we have to be
  16152. * before we will be considered non-live. Note that this will only
  16153. * be used when playing at the live edge. This allows large seekable end
  16154. * changes to not effect whether we are live or not.
  16155. */
  16156. constructor(player, options) {
  16157. // LiveTracker does not need an element
  16158. const options_ = merge(defaults, options, {
  16159. createEl: false
  16160. });
  16161. super(player, options_);
  16162. this.trackLiveHandler_ = () => this.trackLive_();
  16163. this.handlePlay_ = e => this.handlePlay(e);
  16164. this.handleFirstTimeupdate_ = e => this.handleFirstTimeupdate(e);
  16165. this.handleSeeked_ = e => this.handleSeeked(e);
  16166. this.seekToLiveEdge_ = e => this.seekToLiveEdge(e);
  16167. this.reset_();
  16168. this.on(this.player_, 'durationchange', e => this.handleDurationchange(e));
  16169. // we should try to toggle tracking on canplay as native playback engines, like Safari
  16170. // may not have the proper values for things like seekableEnd until then
  16171. this.on(this.player_, 'canplay', () => this.toggleTracking());
  16172. }
  16173. /**
  16174. * all the functionality for tracking when seek end changes
  16175. * and for tracking how far past seek end we should be
  16176. */
  16177. trackLive_() {
  16178. const seekable = this.player_.seekable();
  16179. // skip undefined seekable
  16180. if (!seekable || !seekable.length) {
  16181. return;
  16182. }
  16183. const newTime = Number(window.performance.now().toFixed(4));
  16184. const deltaTime = this.lastTime_ === -1 ? 0 : (newTime - this.lastTime_) / 1000;
  16185. this.lastTime_ = newTime;
  16186. this.pastSeekEnd_ = this.pastSeekEnd() + deltaTime;
  16187. const liveCurrentTime = this.liveCurrentTime();
  16188. const currentTime = this.player_.currentTime();
  16189. // we are behind live if any are true
  16190. // 1. the player is paused
  16191. // 2. the user seeked to a location 2 seconds away from live
  16192. // 3. the difference between live and current time is greater
  16193. // liveTolerance which defaults to 15s
  16194. let isBehind = this.player_.paused() || this.seekedBehindLive_ || Math.abs(liveCurrentTime - currentTime) > this.options_.liveTolerance;
  16195. // we cannot be behind if
  16196. // 1. until we have not seen a timeupdate yet
  16197. // 2. liveCurrentTime is Infinity, which happens on Android and Native Safari
  16198. if (!this.timeupdateSeen_ || liveCurrentTime === Infinity) {
  16199. isBehind = false;
  16200. }
  16201. if (isBehind !== this.behindLiveEdge_) {
  16202. this.behindLiveEdge_ = isBehind;
  16203. this.trigger('liveedgechange');
  16204. }
  16205. }
  16206. /**
  16207. * handle a durationchange event on the player
  16208. * and start/stop tracking accordingly.
  16209. */
  16210. handleDurationchange() {
  16211. this.toggleTracking();
  16212. }
  16213. /**
  16214. * start/stop tracking
  16215. */
  16216. toggleTracking() {
  16217. if (this.player_.duration() === Infinity && this.liveWindow() >= this.options_.trackingThreshold) {
  16218. if (this.player_.options_.liveui) {
  16219. this.player_.addClass('vjs-liveui');
  16220. }
  16221. this.startTracking();
  16222. } else {
  16223. this.player_.removeClass('vjs-liveui');
  16224. this.stopTracking();
  16225. }
  16226. }
  16227. /**
  16228. * start tracking live playback
  16229. */
  16230. startTracking() {
  16231. if (this.isTracking()) {
  16232. return;
  16233. }
  16234. // If we haven't seen a timeupdate, we need to check whether playback
  16235. // began before this component started tracking. This can happen commonly
  16236. // when using autoplay.
  16237. if (!this.timeupdateSeen_) {
  16238. this.timeupdateSeen_ = this.player_.hasStarted();
  16239. }
  16240. this.trackingInterval_ = this.setInterval(this.trackLiveHandler_, UPDATE_REFRESH_INTERVAL);
  16241. this.trackLive_();
  16242. this.on(this.player_, ['play', 'pause'], this.trackLiveHandler_);
  16243. if (!this.timeupdateSeen_) {
  16244. this.one(this.player_, 'play', this.handlePlay_);
  16245. this.one(this.player_, 'timeupdate', this.handleFirstTimeupdate_);
  16246. } else {
  16247. this.on(this.player_, 'seeked', this.handleSeeked_);
  16248. }
  16249. }
  16250. /**
  16251. * handle the first timeupdate on the player if it wasn't already playing
  16252. * when live tracker started tracking.
  16253. */
  16254. handleFirstTimeupdate() {
  16255. this.timeupdateSeen_ = true;
  16256. this.on(this.player_, 'seeked', this.handleSeeked_);
  16257. }
  16258. /**
  16259. * Keep track of what time a seek starts, and listen for seeked
  16260. * to find where a seek ends.
  16261. */
  16262. handleSeeked() {
  16263. const timeDiff = Math.abs(this.liveCurrentTime() - this.player_.currentTime());
  16264. this.seekedBehindLive_ = this.nextSeekedFromUser_ && timeDiff > 2;
  16265. this.nextSeekedFromUser_ = false;
  16266. this.trackLive_();
  16267. }
  16268. /**
  16269. * handle the first play on the player, and make sure that we seek
  16270. * right to the live edge.
  16271. */
  16272. handlePlay() {
  16273. this.one(this.player_, 'timeupdate', this.seekToLiveEdge_);
  16274. }
  16275. /**
  16276. * Stop tracking, and set all internal variables to
  16277. * their initial value.
  16278. */
  16279. reset_() {
  16280. this.lastTime_ = -1;
  16281. this.pastSeekEnd_ = 0;
  16282. this.lastSeekEnd_ = -1;
  16283. this.behindLiveEdge_ = true;
  16284. this.timeupdateSeen_ = false;
  16285. this.seekedBehindLive_ = false;
  16286. this.nextSeekedFromUser_ = false;
  16287. this.clearInterval(this.trackingInterval_);
  16288. this.trackingInterval_ = null;
  16289. this.off(this.player_, ['play', 'pause'], this.trackLiveHandler_);
  16290. this.off(this.player_, 'seeked', this.handleSeeked_);
  16291. this.off(this.player_, 'play', this.handlePlay_);
  16292. this.off(this.player_, 'timeupdate', this.handleFirstTimeupdate_);
  16293. this.off(this.player_, 'timeupdate', this.seekToLiveEdge_);
  16294. }
  16295. /**
  16296. * The next seeked event is from the user. Meaning that any seek
  16297. * > 2s behind live will be considered behind live for real and
  16298. * liveTolerance will be ignored.
  16299. */
  16300. nextSeekedFromUser() {
  16301. this.nextSeekedFromUser_ = true;
  16302. }
  16303. /**
  16304. * stop tracking live playback
  16305. */
  16306. stopTracking() {
  16307. if (!this.isTracking()) {
  16308. return;
  16309. }
  16310. this.reset_();
  16311. this.trigger('liveedgechange');
  16312. }
  16313. /**
  16314. * A helper to get the player seekable end
  16315. * so that we don't have to null check everywhere
  16316. *
  16317. * @return {number}
  16318. * The furthest seekable end or Infinity.
  16319. */
  16320. seekableEnd() {
  16321. const seekable = this.player_.seekable();
  16322. const seekableEnds = [];
  16323. let i = seekable ? seekable.length : 0;
  16324. while (i--) {
  16325. seekableEnds.push(seekable.end(i));
  16326. }
  16327. // grab the furthest seekable end after sorting, or if there are none
  16328. // default to Infinity
  16329. return seekableEnds.length ? seekableEnds.sort()[seekableEnds.length - 1] : Infinity;
  16330. }
  16331. /**
  16332. * A helper to get the player seekable start
  16333. * so that we don't have to null check everywhere
  16334. *
  16335. * @return {number}
  16336. * The earliest seekable start or 0.
  16337. */
  16338. seekableStart() {
  16339. const seekable = this.player_.seekable();
  16340. const seekableStarts = [];
  16341. let i = seekable ? seekable.length : 0;
  16342. while (i--) {
  16343. seekableStarts.push(seekable.start(i));
  16344. }
  16345. // grab the first seekable start after sorting, or if there are none
  16346. // default to 0
  16347. return seekableStarts.length ? seekableStarts.sort()[0] : 0;
  16348. }
  16349. /**
  16350. * Get the live time window aka
  16351. * the amount of time between seekable start and
  16352. * live current time.
  16353. *
  16354. * @return {number}
  16355. * The amount of seconds that are seekable in
  16356. * the live video.
  16357. */
  16358. liveWindow() {
  16359. const liveCurrentTime = this.liveCurrentTime();
  16360. // if liveCurrenTime is Infinity then we don't have a liveWindow at all
  16361. if (liveCurrentTime === Infinity) {
  16362. return 0;
  16363. }
  16364. return liveCurrentTime - this.seekableStart();
  16365. }
  16366. /**
  16367. * Determines if the player is live, only checks if this component
  16368. * is tracking live playback or not
  16369. *
  16370. * @return {boolean}
  16371. * Whether liveTracker is tracking
  16372. */
  16373. isLive() {
  16374. return this.isTracking();
  16375. }
  16376. /**
  16377. * Determines if currentTime is at the live edge and won't fall behind
  16378. * on each seekableendchange
  16379. *
  16380. * @return {boolean}
  16381. * Whether playback is at the live edge
  16382. */
  16383. atLiveEdge() {
  16384. return !this.behindLiveEdge();
  16385. }
  16386. /**
  16387. * get what we expect the live current time to be
  16388. *
  16389. * @return {number}
  16390. * The expected live current time
  16391. */
  16392. liveCurrentTime() {
  16393. return this.pastSeekEnd() + this.seekableEnd();
  16394. }
  16395. /**
  16396. * The number of seconds that have occurred after seekable end
  16397. * changed. This will be reset to 0 once seekable end changes.
  16398. *
  16399. * @return {number}
  16400. * Seconds past the current seekable end
  16401. */
  16402. pastSeekEnd() {
  16403. const seekableEnd = this.seekableEnd();
  16404. if (this.lastSeekEnd_ !== -1 && seekableEnd !== this.lastSeekEnd_) {
  16405. this.pastSeekEnd_ = 0;
  16406. }
  16407. this.lastSeekEnd_ = seekableEnd;
  16408. return this.pastSeekEnd_;
  16409. }
  16410. /**
  16411. * If we are currently behind the live edge, aka currentTime will be
  16412. * behind on a seekableendchange
  16413. *
  16414. * @return {boolean}
  16415. * If we are behind the live edge
  16416. */
  16417. behindLiveEdge() {
  16418. return this.behindLiveEdge_;
  16419. }
  16420. /**
  16421. * Whether live tracker is currently tracking or not.
  16422. */
  16423. isTracking() {
  16424. return typeof this.trackingInterval_ === 'number';
  16425. }
  16426. /**
  16427. * Seek to the live edge if we are behind the live edge
  16428. */
  16429. seekToLiveEdge() {
  16430. this.seekedBehindLive_ = false;
  16431. if (this.atLiveEdge()) {
  16432. return;
  16433. }
  16434. this.nextSeekedFromUser_ = false;
  16435. this.player_.currentTime(this.liveCurrentTime());
  16436. }
  16437. /**
  16438. * Dispose of liveTracker
  16439. */
  16440. dispose() {
  16441. this.stopTracking();
  16442. super.dispose();
  16443. }
  16444. }
  16445. Component.registerComponent('LiveTracker', LiveTracker);
  16446. /**
  16447. * Displays an element over the player which contains an optional title and
  16448. * description for the current content.
  16449. *
  16450. * Much of the code for this component originated in the now obsolete
  16451. * videojs-dock plugin: https://github.com/brightcove/videojs-dock/
  16452. *
  16453. * @extends Component
  16454. */
  16455. class TitleBar extends Component {
  16456. constructor(player, options) {
  16457. super(player, options);
  16458. this.on('statechanged', e => this.updateDom_());
  16459. this.updateDom_();
  16460. }
  16461. /**
  16462. * Create the `TitleBar`'s DOM element
  16463. *
  16464. * @return {Element}
  16465. * The element that was created.
  16466. */
  16467. createEl() {
  16468. this.els = {
  16469. title: createEl('div', {
  16470. className: 'vjs-title-bar-title',
  16471. id: `vjs-title-bar-title-${newGUID()}`
  16472. }),
  16473. description: createEl('div', {
  16474. className: 'vjs-title-bar-description',
  16475. id: `vjs-title-bar-description-${newGUID()}`
  16476. })
  16477. };
  16478. return createEl('div', {
  16479. className: 'vjs-title-bar'
  16480. }, {}, values(this.els));
  16481. }
  16482. /**
  16483. * Updates the DOM based on the component's state object.
  16484. */
  16485. updateDom_() {
  16486. const tech = this.player_.tech_;
  16487. const techEl = tech && tech.el_;
  16488. const techAriaAttrs = {
  16489. title: 'aria-labelledby',
  16490. description: 'aria-describedby'
  16491. };
  16492. ['title', 'description'].forEach(k => {
  16493. const value = this.state[k];
  16494. const el = this.els[k];
  16495. const techAriaAttr = techAriaAttrs[k];
  16496. emptyEl(el);
  16497. if (value) {
  16498. textContent(el, value);
  16499. }
  16500. // If there is a tech element available, update its ARIA attributes
  16501. // according to whether a title and/or description have been provided.
  16502. if (techEl) {
  16503. techEl.removeAttribute(techAriaAttr);
  16504. if (value) {
  16505. techEl.setAttribute(techAriaAttr, el.id);
  16506. }
  16507. }
  16508. });
  16509. if (this.state.title || this.state.description) {
  16510. this.show();
  16511. } else {
  16512. this.hide();
  16513. }
  16514. }
  16515. /**
  16516. * Update the contents of the title bar component with new title and
  16517. * description text.
  16518. *
  16519. * If both title and description are missing, the title bar will be hidden.
  16520. *
  16521. * If either title or description are present, the title bar will be visible.
  16522. *
  16523. * NOTE: Any previously set value will be preserved. To unset a previously
  16524. * set value, you must pass an empty string or null.
  16525. *
  16526. * For example:
  16527. *
  16528. * ```
  16529. * update({title: 'foo', description: 'bar'}) // title: 'foo', description: 'bar'
  16530. * update({description: 'bar2'}) // title: 'foo', description: 'bar2'
  16531. * update({title: ''}) // title: '', description: 'bar2'
  16532. * update({title: 'foo', description: null}) // title: 'foo', description: null
  16533. * ```
  16534. *
  16535. * @param {Object} [options={}]
  16536. * An options object. When empty, the title bar will be hidden.
  16537. *
  16538. * @param {string} [options.title]
  16539. * A title to display in the title bar.
  16540. *
  16541. * @param {string} [options.description]
  16542. * A description to display in the title bar.
  16543. */
  16544. update(options) {
  16545. this.setState(options);
  16546. }
  16547. /**
  16548. * Dispose the component.
  16549. */
  16550. dispose() {
  16551. const tech = this.player_.tech_;
  16552. const techEl = tech && tech.el_;
  16553. if (techEl) {
  16554. techEl.removeAttribute('aria-labelledby');
  16555. techEl.removeAttribute('aria-describedby');
  16556. }
  16557. super.dispose();
  16558. this.els = null;
  16559. }
  16560. }
  16561. Component.registerComponent('TitleBar', TitleBar);
  16562. /**
  16563. * This function is used to fire a sourceset when there is something
  16564. * similar to `mediaEl.load()` being called. It will try to find the source via
  16565. * the `src` attribute and then the `<source>` elements. It will then fire `sourceset`
  16566. * with the source that was found or empty string if we cannot know. If it cannot
  16567. * find a source then `sourceset` will not be fired.
  16568. *
  16569. * @param { import('./html5').default } tech
  16570. * The tech object that sourceset was setup on
  16571. *
  16572. * @return {boolean}
  16573. * returns false if the sourceset was not fired and true otherwise.
  16574. */
  16575. const sourcesetLoad = tech => {
  16576. const el = tech.el();
  16577. // if `el.src` is set, that source will be loaded.
  16578. if (el.hasAttribute('src')) {
  16579. tech.triggerSourceset(el.src);
  16580. return true;
  16581. }
  16582. /**
  16583. * Since there isn't a src property on the media element, source elements will be used for
  16584. * implementing the source selection algorithm. This happens asynchronously and
  16585. * for most cases were there is more than one source we cannot tell what source will
  16586. * be loaded, without re-implementing the source selection algorithm. At this time we are not
  16587. * going to do that. There are three special cases that we do handle here though:
  16588. *
  16589. * 1. If there are no sources, do not fire `sourceset`.
  16590. * 2. If there is only one `<source>` with a `src` property/attribute that is our `src`
  16591. * 3. If there is more than one `<source>` but all of them have the same `src` url.
  16592. * That will be our src.
  16593. */
  16594. const sources = tech.$$('source');
  16595. const srcUrls = [];
  16596. let src = '';
  16597. // if there are no sources, do not fire sourceset
  16598. if (!sources.length) {
  16599. return false;
  16600. }
  16601. // only count valid/non-duplicate source elements
  16602. for (let i = 0; i < sources.length; i++) {
  16603. const url = sources[i].src;
  16604. if (url && srcUrls.indexOf(url) === -1) {
  16605. srcUrls.push(url);
  16606. }
  16607. }
  16608. // there were no valid sources
  16609. if (!srcUrls.length) {
  16610. return false;
  16611. }
  16612. // there is only one valid source element url
  16613. // use that
  16614. if (srcUrls.length === 1) {
  16615. src = srcUrls[0];
  16616. }
  16617. tech.triggerSourceset(src);
  16618. return true;
  16619. };
  16620. /**
  16621. * our implementation of an `innerHTML` descriptor for browsers
  16622. * that do not have one.
  16623. */
  16624. const innerHTMLDescriptorPolyfill = Object.defineProperty({}, 'innerHTML', {
  16625. get() {
  16626. return this.cloneNode(true).innerHTML;
  16627. },
  16628. set(v) {
  16629. // make a dummy node to use innerHTML on
  16630. const dummy = document.createElement(this.nodeName.toLowerCase());
  16631. // set innerHTML to the value provided
  16632. dummy.innerHTML = v;
  16633. // make a document fragment to hold the nodes from dummy
  16634. const docFrag = document.createDocumentFragment();
  16635. // copy all of the nodes created by the innerHTML on dummy
  16636. // to the document fragment
  16637. while (dummy.childNodes.length) {
  16638. docFrag.appendChild(dummy.childNodes[0]);
  16639. }
  16640. // remove content
  16641. this.innerText = '';
  16642. // now we add all of that html in one by appending the
  16643. // document fragment. This is how innerHTML does it.
  16644. window.Element.prototype.appendChild.call(this, docFrag);
  16645. // then return the result that innerHTML's setter would
  16646. return this.innerHTML;
  16647. }
  16648. });
  16649. /**
  16650. * Get a property descriptor given a list of priorities and the
  16651. * property to get.
  16652. */
  16653. const getDescriptor = (priority, prop) => {
  16654. let descriptor = {};
  16655. for (let i = 0; i < priority.length; i++) {
  16656. descriptor = Object.getOwnPropertyDescriptor(priority[i], prop);
  16657. if (descriptor && descriptor.set && descriptor.get) {
  16658. break;
  16659. }
  16660. }
  16661. descriptor.enumerable = true;
  16662. descriptor.configurable = true;
  16663. return descriptor;
  16664. };
  16665. const getInnerHTMLDescriptor = tech => getDescriptor([tech.el(), window.HTMLMediaElement.prototype, window.Element.prototype, innerHTMLDescriptorPolyfill], 'innerHTML');
  16666. /**
  16667. * Patches browser internal functions so that we can tell synchronously
  16668. * if a `<source>` was appended to the media element. For some reason this
  16669. * causes a `sourceset` if the the media element is ready and has no source.
  16670. * This happens when:
  16671. * - The page has just loaded and the media element does not have a source.
  16672. * - The media element was emptied of all sources, then `load()` was called.
  16673. *
  16674. * It does this by patching the following functions/properties when they are supported:
  16675. *
  16676. * - `append()` - can be used to add a `<source>` element to the media element
  16677. * - `appendChild()` - can be used to add a `<source>` element to the media element
  16678. * - `insertAdjacentHTML()` - can be used to add a `<source>` element to the media element
  16679. * - `innerHTML` - can be used to add a `<source>` element to the media element
  16680. *
  16681. * @param {Html5} tech
  16682. * The tech object that sourceset is being setup on.
  16683. */
  16684. const firstSourceWatch = function (tech) {
  16685. const el = tech.el();
  16686. // make sure firstSourceWatch isn't setup twice.
  16687. if (el.resetSourceWatch_) {
  16688. return;
  16689. }
  16690. const old = {};
  16691. const innerDescriptor = getInnerHTMLDescriptor(tech);
  16692. const appendWrapper = appendFn => (...args) => {
  16693. const retval = appendFn.apply(el, args);
  16694. sourcesetLoad(tech);
  16695. return retval;
  16696. };
  16697. ['append', 'appendChild', 'insertAdjacentHTML'].forEach(k => {
  16698. if (!el[k]) {
  16699. return;
  16700. }
  16701. // store the old function
  16702. old[k] = el[k];
  16703. // call the old function with a sourceset if a source
  16704. // was loaded
  16705. el[k] = appendWrapper(old[k]);
  16706. });
  16707. Object.defineProperty(el, 'innerHTML', merge(innerDescriptor, {
  16708. set: appendWrapper(innerDescriptor.set)
  16709. }));
  16710. el.resetSourceWatch_ = () => {
  16711. el.resetSourceWatch_ = null;
  16712. Object.keys(old).forEach(k => {
  16713. el[k] = old[k];
  16714. });
  16715. Object.defineProperty(el, 'innerHTML', innerDescriptor);
  16716. };
  16717. // on the first sourceset, we need to revert our changes
  16718. tech.one('sourceset', el.resetSourceWatch_);
  16719. };
  16720. /**
  16721. * our implementation of a `src` descriptor for browsers
  16722. * that do not have one
  16723. */
  16724. const srcDescriptorPolyfill = Object.defineProperty({}, 'src', {
  16725. get() {
  16726. if (this.hasAttribute('src')) {
  16727. return getAbsoluteURL(window.Element.prototype.getAttribute.call(this, 'src'));
  16728. }
  16729. return '';
  16730. },
  16731. set(v) {
  16732. window.Element.prototype.setAttribute.call(this, 'src', v);
  16733. return v;
  16734. }
  16735. });
  16736. const getSrcDescriptor = tech => getDescriptor([tech.el(), window.HTMLMediaElement.prototype, srcDescriptorPolyfill], 'src');
  16737. /**
  16738. * setup `sourceset` handling on the `Html5` tech. This function
  16739. * patches the following element properties/functions:
  16740. *
  16741. * - `src` - to determine when `src` is set
  16742. * - `setAttribute()` - to determine when `src` is set
  16743. * - `load()` - this re-triggers the source selection algorithm, and can
  16744. * cause a sourceset.
  16745. *
  16746. * If there is no source when we are adding `sourceset` support or during a `load()`
  16747. * we also patch the functions listed in `firstSourceWatch`.
  16748. *
  16749. * @param {Html5} tech
  16750. * The tech to patch
  16751. */
  16752. const setupSourceset = function (tech) {
  16753. if (!tech.featuresSourceset) {
  16754. return;
  16755. }
  16756. const el = tech.el();
  16757. // make sure sourceset isn't setup twice.
  16758. if (el.resetSourceset_) {
  16759. return;
  16760. }
  16761. const srcDescriptor = getSrcDescriptor(tech);
  16762. const oldSetAttribute = el.setAttribute;
  16763. const oldLoad = el.load;
  16764. Object.defineProperty(el, 'src', merge(srcDescriptor, {
  16765. set: v => {
  16766. const retval = srcDescriptor.set.call(el, v);
  16767. // we use the getter here to get the actual value set on src
  16768. tech.triggerSourceset(el.src);
  16769. return retval;
  16770. }
  16771. }));
  16772. el.setAttribute = (n, v) => {
  16773. const retval = oldSetAttribute.call(el, n, v);
  16774. if (/src/i.test(n)) {
  16775. tech.triggerSourceset(el.src);
  16776. }
  16777. return retval;
  16778. };
  16779. el.load = () => {
  16780. const retval = oldLoad.call(el);
  16781. // if load was called, but there was no source to fire
  16782. // sourceset on. We have to watch for a source append
  16783. // as that can trigger a `sourceset` when the media element
  16784. // has no source
  16785. if (!sourcesetLoad(tech)) {
  16786. tech.triggerSourceset('');
  16787. firstSourceWatch(tech);
  16788. }
  16789. return retval;
  16790. };
  16791. if (el.currentSrc) {
  16792. tech.triggerSourceset(el.currentSrc);
  16793. } else if (!sourcesetLoad(tech)) {
  16794. firstSourceWatch(tech);
  16795. }
  16796. el.resetSourceset_ = () => {
  16797. el.resetSourceset_ = null;
  16798. el.load = oldLoad;
  16799. el.setAttribute = oldSetAttribute;
  16800. Object.defineProperty(el, 'src', srcDescriptor);
  16801. if (el.resetSourceWatch_) {
  16802. el.resetSourceWatch_();
  16803. }
  16804. };
  16805. };
  16806. /**
  16807. * @file html5.js
  16808. */
  16809. /**
  16810. * HTML5 Media Controller - Wrapper for HTML5 Media API
  16811. *
  16812. * @mixes Tech~SourceHandlerAdditions
  16813. * @extends Tech
  16814. */
  16815. class Html5 extends Tech {
  16816. /**
  16817. * Create an instance of this Tech.
  16818. *
  16819. * @param {Object} [options]
  16820. * The key/value store of player options.
  16821. *
  16822. * @param {Function} [ready]
  16823. * Callback function to call when the `HTML5` Tech is ready.
  16824. */
  16825. constructor(options, ready) {
  16826. super(options, ready);
  16827. const source = options.source;
  16828. let crossoriginTracks = false;
  16829. this.featuresVideoFrameCallback = this.featuresVideoFrameCallback && this.el_.tagName === 'VIDEO';
  16830. // Set the source if one is provided
  16831. // 1) Check if the source is new (if not, we want to keep the original so playback isn't interrupted)
  16832. // 2) Check to see if the network state of the tag was failed at init, and if so, reset the source
  16833. // anyway so the error gets fired.
  16834. if (source && (this.el_.currentSrc !== source.src || options.tag && options.tag.initNetworkState_ === 3)) {
  16835. this.setSource(source);
  16836. } else {
  16837. this.handleLateInit_(this.el_);
  16838. }
  16839. // setup sourceset after late sourceset/init
  16840. if (options.enableSourceset) {
  16841. this.setupSourcesetHandling_();
  16842. }
  16843. this.isScrubbing_ = false;
  16844. if (this.el_.hasChildNodes()) {
  16845. const nodes = this.el_.childNodes;
  16846. let nodesLength = nodes.length;
  16847. const removeNodes = [];
  16848. while (nodesLength--) {
  16849. const node = nodes[nodesLength];
  16850. const nodeName = node.nodeName.toLowerCase();
  16851. if (nodeName === 'track') {
  16852. if (!this.featuresNativeTextTracks) {
  16853. // Empty video tag tracks so the built-in player doesn't use them also.
  16854. // This may not be fast enough to stop HTML5 browsers from reading the tags
  16855. // so we'll need to turn off any default tracks if we're manually doing
  16856. // captions and subtitles. videoElement.textTracks
  16857. removeNodes.push(node);
  16858. } else {
  16859. // store HTMLTrackElement and TextTrack to remote list
  16860. this.remoteTextTrackEls().addTrackElement_(node);
  16861. this.remoteTextTracks().addTrack(node.track);
  16862. this.textTracks().addTrack(node.track);
  16863. if (!crossoriginTracks && !this.el_.hasAttribute('crossorigin') && isCrossOrigin(node.src)) {
  16864. crossoriginTracks = true;
  16865. }
  16866. }
  16867. }
  16868. }
  16869. for (let i = 0; i < removeNodes.length; i++) {
  16870. this.el_.removeChild(removeNodes[i]);
  16871. }
  16872. }
  16873. this.proxyNativeTracks_();
  16874. if (this.featuresNativeTextTracks && crossoriginTracks) {
  16875. log.warn('Text Tracks are being loaded from another origin but the crossorigin attribute isn\'t used.\n' + 'This may prevent text tracks from loading.');
  16876. }
  16877. // prevent iOS Safari from disabling metadata text tracks during native playback
  16878. this.restoreMetadataTracksInIOSNativePlayer_();
  16879. // Determine if native controls should be used
  16880. // Our goal should be to get the custom controls on mobile solid everywhere
  16881. // so we can remove this all together. Right now this will block custom
  16882. // controls on touch enabled laptops like the Chrome Pixel
  16883. if ((TOUCH_ENABLED || IS_IPHONE) && options.nativeControlsForTouch === true) {
  16884. this.setControls(true);
  16885. }
  16886. // on iOS, we want to proxy `webkitbeginfullscreen` and `webkitendfullscreen`
  16887. // into a `fullscreenchange` event
  16888. this.proxyWebkitFullscreen_();
  16889. this.triggerReady();
  16890. }
  16891. /**
  16892. * Dispose of `HTML5` media element and remove all tracks.
  16893. */
  16894. dispose() {
  16895. if (this.el_ && this.el_.resetSourceset_) {
  16896. this.el_.resetSourceset_();
  16897. }
  16898. Html5.disposeMediaElement(this.el_);
  16899. this.options_ = null;
  16900. // tech will handle clearing of the emulated track list
  16901. super.dispose();
  16902. }
  16903. /**
  16904. * Modify the media element so that we can detect when
  16905. * the source is changed. Fires `sourceset` just after the source has changed
  16906. */
  16907. setupSourcesetHandling_() {
  16908. setupSourceset(this);
  16909. }
  16910. /**
  16911. * When a captions track is enabled in the iOS Safari native player, all other
  16912. * tracks are disabled (including metadata tracks), which nulls all of their
  16913. * associated cue points. This will restore metadata tracks to their pre-fullscreen
  16914. * state in those cases so that cue points are not needlessly lost.
  16915. *
  16916. * @private
  16917. */
  16918. restoreMetadataTracksInIOSNativePlayer_() {
  16919. const textTracks = this.textTracks();
  16920. let metadataTracksPreFullscreenState;
  16921. // captures a snapshot of every metadata track's current state
  16922. const takeMetadataTrackSnapshot = () => {
  16923. metadataTracksPreFullscreenState = [];
  16924. for (let i = 0; i < textTracks.length; i++) {
  16925. const track = textTracks[i];
  16926. if (track.kind === 'metadata') {
  16927. metadataTracksPreFullscreenState.push({
  16928. track,
  16929. storedMode: track.mode
  16930. });
  16931. }
  16932. }
  16933. };
  16934. // snapshot each metadata track's initial state, and update the snapshot
  16935. // each time there is a track 'change' event
  16936. takeMetadataTrackSnapshot();
  16937. textTracks.addEventListener('change', takeMetadataTrackSnapshot);
  16938. this.on('dispose', () => textTracks.removeEventListener('change', takeMetadataTrackSnapshot));
  16939. const restoreTrackMode = () => {
  16940. for (let i = 0; i < metadataTracksPreFullscreenState.length; i++) {
  16941. const storedTrack = metadataTracksPreFullscreenState[i];
  16942. if (storedTrack.track.mode === 'disabled' && storedTrack.track.mode !== storedTrack.storedMode) {
  16943. storedTrack.track.mode = storedTrack.storedMode;
  16944. }
  16945. }
  16946. // we only want this handler to be executed on the first 'change' event
  16947. textTracks.removeEventListener('change', restoreTrackMode);
  16948. };
  16949. // when we enter fullscreen playback, stop updating the snapshot and
  16950. // restore all track modes to their pre-fullscreen state
  16951. this.on('webkitbeginfullscreen', () => {
  16952. textTracks.removeEventListener('change', takeMetadataTrackSnapshot);
  16953. // remove the listener before adding it just in case it wasn't previously removed
  16954. textTracks.removeEventListener('change', restoreTrackMode);
  16955. textTracks.addEventListener('change', restoreTrackMode);
  16956. });
  16957. // start updating the snapshot again after leaving fullscreen
  16958. this.on('webkitendfullscreen', () => {
  16959. // remove the listener before adding it just in case it wasn't previously removed
  16960. textTracks.removeEventListener('change', takeMetadataTrackSnapshot);
  16961. textTracks.addEventListener('change', takeMetadataTrackSnapshot);
  16962. // remove the restoreTrackMode handler in case it wasn't triggered during fullscreen playback
  16963. textTracks.removeEventListener('change', restoreTrackMode);
  16964. });
  16965. }
  16966. /**
  16967. * Attempt to force override of tracks for the given type
  16968. *
  16969. * @param {string} type - Track type to override, possible values include 'Audio',
  16970. * 'Video', and 'Text'.
  16971. * @param {boolean} override - If set to true native audio/video will be overridden,
  16972. * otherwise native audio/video will potentially be used.
  16973. * @private
  16974. */
  16975. overrideNative_(type, override) {
  16976. // If there is no behavioral change don't add/remove listeners
  16977. if (override !== this[`featuresNative${type}Tracks`]) {
  16978. return;
  16979. }
  16980. const lowerCaseType = type.toLowerCase();
  16981. if (this[`${lowerCaseType}TracksListeners_`]) {
  16982. Object.keys(this[`${lowerCaseType}TracksListeners_`]).forEach(eventName => {
  16983. const elTracks = this.el()[`${lowerCaseType}Tracks`];
  16984. elTracks.removeEventListener(eventName, this[`${lowerCaseType}TracksListeners_`][eventName]);
  16985. });
  16986. }
  16987. this[`featuresNative${type}Tracks`] = !override;
  16988. this[`${lowerCaseType}TracksListeners_`] = null;
  16989. this.proxyNativeTracksForType_(lowerCaseType);
  16990. }
  16991. /**
  16992. * Attempt to force override of native audio tracks.
  16993. *
  16994. * @param {boolean} override - If set to true native audio will be overridden,
  16995. * otherwise native audio will potentially be used.
  16996. */
  16997. overrideNativeAudioTracks(override) {
  16998. this.overrideNative_('Audio', override);
  16999. }
  17000. /**
  17001. * Attempt to force override of native video tracks.
  17002. *
  17003. * @param {boolean} override - If set to true native video will be overridden,
  17004. * otherwise native video will potentially be used.
  17005. */
  17006. overrideNativeVideoTracks(override) {
  17007. this.overrideNative_('Video', override);
  17008. }
  17009. /**
  17010. * Proxy native track list events for the given type to our track
  17011. * lists if the browser we are playing in supports that type of track list.
  17012. *
  17013. * @param {string} name - Track type; values include 'audio', 'video', and 'text'
  17014. * @private
  17015. */
  17016. proxyNativeTracksForType_(name) {
  17017. const props = NORMAL[name];
  17018. const elTracks = this.el()[props.getterName];
  17019. const techTracks = this[props.getterName]();
  17020. if (!this[`featuresNative${props.capitalName}Tracks`] || !elTracks || !elTracks.addEventListener) {
  17021. return;
  17022. }
  17023. const listeners = {
  17024. change: e => {
  17025. const event = {
  17026. type: 'change',
  17027. target: techTracks,
  17028. currentTarget: techTracks,
  17029. srcElement: techTracks
  17030. };
  17031. techTracks.trigger(event);
  17032. // if we are a text track change event, we should also notify the
  17033. // remote text track list. This can potentially cause a false positive
  17034. // if we were to get a change event on a non-remote track and
  17035. // we triggered the event on the remote text track list which doesn't
  17036. // contain that track. However, best practices mean looping through the
  17037. // list of tracks and searching for the appropriate mode value, so,
  17038. // this shouldn't pose an issue
  17039. if (name === 'text') {
  17040. this[REMOTE.remoteText.getterName]().trigger(event);
  17041. }
  17042. },
  17043. addtrack(e) {
  17044. techTracks.addTrack(e.track);
  17045. },
  17046. removetrack(e) {
  17047. techTracks.removeTrack(e.track);
  17048. }
  17049. };
  17050. const removeOldTracks = function () {
  17051. const removeTracks = [];
  17052. for (let i = 0; i < techTracks.length; i++) {
  17053. let found = false;
  17054. for (let j = 0; j < elTracks.length; j++) {
  17055. if (elTracks[j] === techTracks[i]) {
  17056. found = true;
  17057. break;
  17058. }
  17059. }
  17060. if (!found) {
  17061. removeTracks.push(techTracks[i]);
  17062. }
  17063. }
  17064. while (removeTracks.length) {
  17065. techTracks.removeTrack(removeTracks.shift());
  17066. }
  17067. };
  17068. this[props.getterName + 'Listeners_'] = listeners;
  17069. Object.keys(listeners).forEach(eventName => {
  17070. const listener = listeners[eventName];
  17071. elTracks.addEventListener(eventName, listener);
  17072. this.on('dispose', e => elTracks.removeEventListener(eventName, listener));
  17073. });
  17074. // Remove (native) tracks that are not used anymore
  17075. this.on('loadstart', removeOldTracks);
  17076. this.on('dispose', e => this.off('loadstart', removeOldTracks));
  17077. }
  17078. /**
  17079. * Proxy all native track list events to our track lists if the browser we are playing
  17080. * in supports that type of track list.
  17081. *
  17082. * @private
  17083. */
  17084. proxyNativeTracks_() {
  17085. NORMAL.names.forEach(name => {
  17086. this.proxyNativeTracksForType_(name);
  17087. });
  17088. }
  17089. /**
  17090. * Create the `Html5` Tech's DOM element.
  17091. *
  17092. * @return {Element}
  17093. * The element that gets created.
  17094. */
  17095. createEl() {
  17096. let el = this.options_.tag;
  17097. // Check if this browser supports moving the element into the box.
  17098. // On the iPhone video will break if you move the element,
  17099. // So we have to create a brand new element.
  17100. // If we ingested the player div, we do not need to move the media element.
  17101. if (!el || !(this.options_.playerElIngest || this.movingMediaElementInDOM)) {
  17102. // If the original tag is still there, clone and remove it.
  17103. if (el) {
  17104. const clone = el.cloneNode(true);
  17105. if (el.parentNode) {
  17106. el.parentNode.insertBefore(clone, el);
  17107. }
  17108. Html5.disposeMediaElement(el);
  17109. el = clone;
  17110. } else {
  17111. el = document.createElement('video');
  17112. // determine if native controls should be used
  17113. const tagAttributes = this.options_.tag && getAttributes(this.options_.tag);
  17114. const attributes = merge({}, tagAttributes);
  17115. if (!TOUCH_ENABLED || this.options_.nativeControlsForTouch !== true) {
  17116. delete attributes.controls;
  17117. }
  17118. setAttributes(el, Object.assign(attributes, {
  17119. id: this.options_.techId,
  17120. class: 'vjs-tech'
  17121. }));
  17122. }
  17123. el.playerId = this.options_.playerId;
  17124. }
  17125. if (typeof this.options_.preload !== 'undefined') {
  17126. setAttribute(el, 'preload', this.options_.preload);
  17127. }
  17128. if (this.options_.disablePictureInPicture !== undefined) {
  17129. el.disablePictureInPicture = this.options_.disablePictureInPicture;
  17130. }
  17131. // Update specific tag settings, in case they were overridden
  17132. // `autoplay` has to be *last* so that `muted` and `playsinline` are present
  17133. // when iOS/Safari or other browsers attempt to autoplay.
  17134. const settingsAttrs = ['loop', 'muted', 'playsinline', 'autoplay'];
  17135. for (let i = 0; i < settingsAttrs.length; i++) {
  17136. const attr = settingsAttrs[i];
  17137. const value = this.options_[attr];
  17138. if (typeof value !== 'undefined') {
  17139. if (value) {
  17140. setAttribute(el, attr, attr);
  17141. } else {
  17142. removeAttribute(el, attr);
  17143. }
  17144. el[attr] = value;
  17145. }
  17146. }
  17147. return el;
  17148. }
  17149. /**
  17150. * This will be triggered if the loadstart event has already fired, before videojs was
  17151. * ready. Two known examples of when this can happen are:
  17152. * 1. If we're loading the playback object after it has started loading
  17153. * 2. The media is already playing the (often with autoplay on) then
  17154. *
  17155. * This function will fire another loadstart so that videojs can catchup.
  17156. *
  17157. * @fires Tech#loadstart
  17158. *
  17159. * @return {undefined}
  17160. * returns nothing.
  17161. */
  17162. handleLateInit_(el) {
  17163. if (el.networkState === 0 || el.networkState === 3) {
  17164. // The video element hasn't started loading the source yet
  17165. // or didn't find a source
  17166. return;
  17167. }
  17168. if (el.readyState === 0) {
  17169. // NetworkState is set synchronously BUT loadstart is fired at the
  17170. // end of the current stack, usually before setInterval(fn, 0).
  17171. // So at this point we know loadstart may have already fired or is
  17172. // about to fire, and either way the player hasn't seen it yet.
  17173. // We don't want to fire loadstart prematurely here and cause a
  17174. // double loadstart so we'll wait and see if it happens between now
  17175. // and the next loop, and fire it if not.
  17176. // HOWEVER, we also want to make sure it fires before loadedmetadata
  17177. // which could also happen between now and the next loop, so we'll
  17178. // watch for that also.
  17179. let loadstartFired = false;
  17180. const setLoadstartFired = function () {
  17181. loadstartFired = true;
  17182. };
  17183. this.on('loadstart', setLoadstartFired);
  17184. const triggerLoadstart = function () {
  17185. // We did miss the original loadstart. Make sure the player
  17186. // sees loadstart before loadedmetadata
  17187. if (!loadstartFired) {
  17188. this.trigger('loadstart');
  17189. }
  17190. };
  17191. this.on('loadedmetadata', triggerLoadstart);
  17192. this.ready(function () {
  17193. this.off('loadstart', setLoadstartFired);
  17194. this.off('loadedmetadata', triggerLoadstart);
  17195. if (!loadstartFired) {
  17196. // We did miss the original native loadstart. Fire it now.
  17197. this.trigger('loadstart');
  17198. }
  17199. });
  17200. return;
  17201. }
  17202. // From here on we know that loadstart already fired and we missed it.
  17203. // The other readyState events aren't as much of a problem if we double
  17204. // them, so not going to go to as much trouble as loadstart to prevent
  17205. // that unless we find reason to.
  17206. const eventsToTrigger = ['loadstart'];
  17207. // loadedmetadata: newly equal to HAVE_METADATA (1) or greater
  17208. eventsToTrigger.push('loadedmetadata');
  17209. // loadeddata: newly increased to HAVE_CURRENT_DATA (2) or greater
  17210. if (el.readyState >= 2) {
  17211. eventsToTrigger.push('loadeddata');
  17212. }
  17213. // canplay: newly increased to HAVE_FUTURE_DATA (3) or greater
  17214. if (el.readyState >= 3) {
  17215. eventsToTrigger.push('canplay');
  17216. }
  17217. // canplaythrough: newly equal to HAVE_ENOUGH_DATA (4)
  17218. if (el.readyState >= 4) {
  17219. eventsToTrigger.push('canplaythrough');
  17220. }
  17221. // We still need to give the player time to add event listeners
  17222. this.ready(function () {
  17223. eventsToTrigger.forEach(function (type) {
  17224. this.trigger(type);
  17225. }, this);
  17226. });
  17227. }
  17228. /**
  17229. * Set whether we are scrubbing or not.
  17230. * This is used to decide whether we should use `fastSeek` or not.
  17231. * `fastSeek` is used to provide trick play on Safari browsers.
  17232. *
  17233. * @param {boolean} isScrubbing
  17234. * - true for we are currently scrubbing
  17235. * - false for we are no longer scrubbing
  17236. */
  17237. setScrubbing(isScrubbing) {
  17238. this.isScrubbing_ = isScrubbing;
  17239. }
  17240. /**
  17241. * Get whether we are scrubbing or not.
  17242. *
  17243. * @return {boolean} isScrubbing
  17244. * - true for we are currently scrubbing
  17245. * - false for we are no longer scrubbing
  17246. */
  17247. scrubbing() {
  17248. return this.isScrubbing_;
  17249. }
  17250. /**
  17251. * Set current time for the `HTML5` tech.
  17252. *
  17253. * @param {number} seconds
  17254. * Set the current time of the media to this.
  17255. */
  17256. setCurrentTime(seconds) {
  17257. try {
  17258. if (this.isScrubbing_ && this.el_.fastSeek && IS_ANY_SAFARI) {
  17259. this.el_.fastSeek(seconds);
  17260. } else {
  17261. this.el_.currentTime = seconds;
  17262. }
  17263. } catch (e) {
  17264. log(e, 'Video is not ready. (Video.js)');
  17265. // this.warning(VideoJS.warnings.videoNotReady);
  17266. }
  17267. }
  17268. /**
  17269. * Get the current duration of the HTML5 media element.
  17270. *
  17271. * @return {number}
  17272. * The duration of the media or 0 if there is no duration.
  17273. */
  17274. duration() {
  17275. // Android Chrome will report duration as Infinity for VOD HLS until after
  17276. // playback has started, which triggers the live display erroneously.
  17277. // Return NaN if playback has not started and trigger a durationupdate once
  17278. // the duration can be reliably known.
  17279. if (this.el_.duration === Infinity && IS_ANDROID && IS_CHROME && this.el_.currentTime === 0) {
  17280. // Wait for the first `timeupdate` with currentTime > 0 - there may be
  17281. // several with 0
  17282. const checkProgress = () => {
  17283. if (this.el_.currentTime > 0) {
  17284. // Trigger durationchange for genuinely live video
  17285. if (this.el_.duration === Infinity) {
  17286. this.trigger('durationchange');
  17287. }
  17288. this.off('timeupdate', checkProgress);
  17289. }
  17290. };
  17291. this.on('timeupdate', checkProgress);
  17292. return NaN;
  17293. }
  17294. return this.el_.duration || NaN;
  17295. }
  17296. /**
  17297. * Get the current width of the HTML5 media element.
  17298. *
  17299. * @return {number}
  17300. * The width of the HTML5 media element.
  17301. */
  17302. width() {
  17303. return this.el_.offsetWidth;
  17304. }
  17305. /**
  17306. * Get the current height of the HTML5 media element.
  17307. *
  17308. * @return {number}
  17309. * The height of the HTML5 media element.
  17310. */
  17311. height() {
  17312. return this.el_.offsetHeight;
  17313. }
  17314. /**
  17315. * Proxy iOS `webkitbeginfullscreen` and `webkitendfullscreen` into
  17316. * `fullscreenchange` event.
  17317. *
  17318. * @private
  17319. * @fires fullscreenchange
  17320. * @listens webkitendfullscreen
  17321. * @listens webkitbeginfullscreen
  17322. * @listens webkitbeginfullscreen
  17323. */
  17324. proxyWebkitFullscreen_() {
  17325. if (!('webkitDisplayingFullscreen' in this.el_)) {
  17326. return;
  17327. }
  17328. const endFn = function () {
  17329. this.trigger('fullscreenchange', {
  17330. isFullscreen: false
  17331. });
  17332. // Safari will sometimes set controls on the videoelement when existing fullscreen.
  17333. if (this.el_.controls && !this.options_.nativeControlsForTouch && this.controls()) {
  17334. this.el_.controls = false;
  17335. }
  17336. };
  17337. const beginFn = function () {
  17338. if ('webkitPresentationMode' in this.el_ && this.el_.webkitPresentationMode !== 'picture-in-picture') {
  17339. this.one('webkitendfullscreen', endFn);
  17340. this.trigger('fullscreenchange', {
  17341. isFullscreen: true,
  17342. // set a flag in case another tech triggers fullscreenchange
  17343. nativeIOSFullscreen: true
  17344. });
  17345. }
  17346. };
  17347. this.on('webkitbeginfullscreen', beginFn);
  17348. this.on('dispose', () => {
  17349. this.off('webkitbeginfullscreen', beginFn);
  17350. this.off('webkitendfullscreen', endFn);
  17351. });
  17352. }
  17353. /**
  17354. * Check if fullscreen is supported on the video el.
  17355. *
  17356. * @return {boolean}
  17357. * - True if fullscreen is supported.
  17358. * - False if fullscreen is not supported.
  17359. */
  17360. supportsFullScreen() {
  17361. return typeof this.el_.webkitEnterFullScreen === 'function';
  17362. }
  17363. /**
  17364. * Request that the `HTML5` Tech enter fullscreen.
  17365. */
  17366. enterFullScreen() {
  17367. const video = this.el_;
  17368. if (video.paused && video.networkState <= video.HAVE_METADATA) {
  17369. // attempt to prime the video element for programmatic access
  17370. // this isn't necessary on the desktop but shouldn't hurt
  17371. silencePromise(this.el_.play());
  17372. // playing and pausing synchronously during the transition to fullscreen
  17373. // can get iOS ~6.1 devices into a play/pause loop
  17374. this.setTimeout(function () {
  17375. video.pause();
  17376. try {
  17377. video.webkitEnterFullScreen();
  17378. } catch (e) {
  17379. this.trigger('fullscreenerror', e);
  17380. }
  17381. }, 0);
  17382. } else {
  17383. try {
  17384. video.webkitEnterFullScreen();
  17385. } catch (e) {
  17386. this.trigger('fullscreenerror', e);
  17387. }
  17388. }
  17389. }
  17390. /**
  17391. * Request that the `HTML5` Tech exit fullscreen.
  17392. */
  17393. exitFullScreen() {
  17394. if (!this.el_.webkitDisplayingFullscreen) {
  17395. this.trigger('fullscreenerror', new Error('The video is not fullscreen'));
  17396. return;
  17397. }
  17398. this.el_.webkitExitFullScreen();
  17399. }
  17400. /**
  17401. * Create a floating video window always on top of other windows so that users may
  17402. * continue consuming media while they interact with other content sites, or
  17403. * applications on their device.
  17404. *
  17405. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  17406. *
  17407. * @return {Promise}
  17408. * A promise with a Picture-in-Picture window.
  17409. */
  17410. requestPictureInPicture() {
  17411. return this.el_.requestPictureInPicture();
  17412. }
  17413. /**
  17414. * Native requestVideoFrameCallback if supported by browser/tech, or fallback
  17415. * Don't use rVCF on Safari when DRM is playing, as it doesn't fire
  17416. * Needs to be checked later than the constructor
  17417. * This will be a false positive for clear sources loaded after a Fairplay source
  17418. *
  17419. * @param {function} cb function to call
  17420. * @return {number} id of request
  17421. */
  17422. requestVideoFrameCallback(cb) {
  17423. if (this.featuresVideoFrameCallback && !this.el_.webkitKeys) {
  17424. return this.el_.requestVideoFrameCallback(cb);
  17425. }
  17426. return super.requestVideoFrameCallback(cb);
  17427. }
  17428. /**
  17429. * Native or fallback requestVideoFrameCallback
  17430. *
  17431. * @param {number} id request id to cancel
  17432. */
  17433. cancelVideoFrameCallback(id) {
  17434. if (this.featuresVideoFrameCallback && !this.el_.webkitKeys) {
  17435. this.el_.cancelVideoFrameCallback(id);
  17436. } else {
  17437. super.cancelVideoFrameCallback(id);
  17438. }
  17439. }
  17440. /**
  17441. * A getter/setter for the `Html5` Tech's source object.
  17442. * > Note: Please use {@link Html5#setSource}
  17443. *
  17444. * @param {Tech~SourceObject} [src]
  17445. * The source object you want to set on the `HTML5` techs element.
  17446. *
  17447. * @return {Tech~SourceObject|undefined}
  17448. * - The current source object when a source is not passed in.
  17449. * - undefined when setting
  17450. *
  17451. * @deprecated Since version 5.
  17452. */
  17453. src(src) {
  17454. if (src === undefined) {
  17455. return this.el_.src;
  17456. }
  17457. // Setting src through `src` instead of `setSrc` will be deprecated
  17458. this.setSrc(src);
  17459. }
  17460. /**
  17461. * Reset the tech by removing all sources and then calling
  17462. * {@link Html5.resetMediaElement}.
  17463. */
  17464. reset() {
  17465. Html5.resetMediaElement(this.el_);
  17466. }
  17467. /**
  17468. * Get the current source on the `HTML5` Tech. Falls back to returning the source from
  17469. * the HTML5 media element.
  17470. *
  17471. * @return {Tech~SourceObject}
  17472. * The current source object from the HTML5 tech. With a fallback to the
  17473. * elements source.
  17474. */
  17475. currentSrc() {
  17476. if (this.currentSource_) {
  17477. return this.currentSource_.src;
  17478. }
  17479. return this.el_.currentSrc;
  17480. }
  17481. /**
  17482. * Set controls attribute for the HTML5 media Element.
  17483. *
  17484. * @param {string} val
  17485. * Value to set the controls attribute to
  17486. */
  17487. setControls(val) {
  17488. this.el_.controls = !!val;
  17489. }
  17490. /**
  17491. * Create and returns a remote {@link TextTrack} object.
  17492. *
  17493. * @param {string} kind
  17494. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  17495. *
  17496. * @param {string} [label]
  17497. * Label to identify the text track
  17498. *
  17499. * @param {string} [language]
  17500. * Two letter language abbreviation
  17501. *
  17502. * @return {TextTrack}
  17503. * The TextTrack that gets created.
  17504. */
  17505. addTextTrack(kind, label, language) {
  17506. if (!this.featuresNativeTextTracks) {
  17507. return super.addTextTrack(kind, label, language);
  17508. }
  17509. return this.el_.addTextTrack(kind, label, language);
  17510. }
  17511. /**
  17512. * Creates either native TextTrack or an emulated TextTrack depending
  17513. * on the value of `featuresNativeTextTracks`
  17514. *
  17515. * @param {Object} options
  17516. * The object should contain the options to initialize the TextTrack with.
  17517. *
  17518. * @param {string} [options.kind]
  17519. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata).
  17520. *
  17521. * @param {string} [options.label]
  17522. * Label to identify the text track
  17523. *
  17524. * @param {string} [options.language]
  17525. * Two letter language abbreviation.
  17526. *
  17527. * @param {boolean} [options.default]
  17528. * Default this track to on.
  17529. *
  17530. * @param {string} [options.id]
  17531. * The internal id to assign this track.
  17532. *
  17533. * @param {string} [options.src]
  17534. * A source url for the track.
  17535. *
  17536. * @return {HTMLTrackElement}
  17537. * The track element that gets created.
  17538. */
  17539. createRemoteTextTrack(options) {
  17540. if (!this.featuresNativeTextTracks) {
  17541. return super.createRemoteTextTrack(options);
  17542. }
  17543. const htmlTrackElement = document.createElement('track');
  17544. if (options.kind) {
  17545. htmlTrackElement.kind = options.kind;
  17546. }
  17547. if (options.label) {
  17548. htmlTrackElement.label = options.label;
  17549. }
  17550. if (options.language || options.srclang) {
  17551. htmlTrackElement.srclang = options.language || options.srclang;
  17552. }
  17553. if (options.default) {
  17554. htmlTrackElement.default = options.default;
  17555. }
  17556. if (options.id) {
  17557. htmlTrackElement.id = options.id;
  17558. }
  17559. if (options.src) {
  17560. htmlTrackElement.src = options.src;
  17561. }
  17562. return htmlTrackElement;
  17563. }
  17564. /**
  17565. * Creates a remote text track object and returns an html track element.
  17566. *
  17567. * @param {Object} options The object should contain values for
  17568. * kind, language, label, and src (location of the WebVTT file)
  17569. * @param {boolean} [manualCleanup=false] if set to true, the TextTrack
  17570. * will not be removed from the TextTrackList and HtmlTrackElementList
  17571. * after a source change
  17572. * @return {HTMLTrackElement} An Html Track Element.
  17573. * This can be an emulated {@link HTMLTrackElement} or a native one.
  17574. *
  17575. */
  17576. addRemoteTextTrack(options, manualCleanup) {
  17577. const htmlTrackElement = super.addRemoteTextTrack(options, manualCleanup);
  17578. if (this.featuresNativeTextTracks) {
  17579. this.el().appendChild(htmlTrackElement);
  17580. }
  17581. return htmlTrackElement;
  17582. }
  17583. /**
  17584. * Remove remote `TextTrack` from `TextTrackList` object
  17585. *
  17586. * @param {TextTrack} track
  17587. * `TextTrack` object to remove
  17588. */
  17589. removeRemoteTextTrack(track) {
  17590. super.removeRemoteTextTrack(track);
  17591. if (this.featuresNativeTextTracks) {
  17592. const tracks = this.$$('track');
  17593. let i = tracks.length;
  17594. while (i--) {
  17595. if (track === tracks[i] || track === tracks[i].track) {
  17596. this.el().removeChild(tracks[i]);
  17597. }
  17598. }
  17599. }
  17600. }
  17601. /**
  17602. * Gets available media playback quality metrics as specified by the W3C's Media
  17603. * Playback Quality API.
  17604. *
  17605. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  17606. *
  17607. * @return {Object}
  17608. * An object with supported media playback quality metrics
  17609. */
  17610. getVideoPlaybackQuality() {
  17611. if (typeof this.el().getVideoPlaybackQuality === 'function') {
  17612. return this.el().getVideoPlaybackQuality();
  17613. }
  17614. const videoPlaybackQuality = {};
  17615. if (typeof this.el().webkitDroppedFrameCount !== 'undefined' && typeof this.el().webkitDecodedFrameCount !== 'undefined') {
  17616. videoPlaybackQuality.droppedVideoFrames = this.el().webkitDroppedFrameCount;
  17617. videoPlaybackQuality.totalVideoFrames = this.el().webkitDecodedFrameCount;
  17618. }
  17619. if (window.performance) {
  17620. videoPlaybackQuality.creationTime = window.performance.now();
  17621. }
  17622. return videoPlaybackQuality;
  17623. }
  17624. }
  17625. /* HTML5 Support Testing ---------------------------------------------------- */
  17626. /**
  17627. * Element for testing browser HTML5 media capabilities
  17628. *
  17629. * @type {Element}
  17630. * @constant
  17631. * @private
  17632. */
  17633. defineLazyProperty(Html5, 'TEST_VID', function () {
  17634. if (!isReal()) {
  17635. return;
  17636. }
  17637. const video = document.createElement('video');
  17638. const track = document.createElement('track');
  17639. track.kind = 'captions';
  17640. track.srclang = 'en';
  17641. track.label = 'English';
  17642. video.appendChild(track);
  17643. return video;
  17644. });
  17645. /**
  17646. * Check if HTML5 media is supported by this browser/device.
  17647. *
  17648. * @return {boolean}
  17649. * - True if HTML5 media is supported.
  17650. * - False if HTML5 media is not supported.
  17651. */
  17652. Html5.isSupported = function () {
  17653. // IE with no Media Player is a LIAR! (#984)
  17654. try {
  17655. Html5.TEST_VID.volume = 0.5;
  17656. } catch (e) {
  17657. return false;
  17658. }
  17659. return !!(Html5.TEST_VID && Html5.TEST_VID.canPlayType);
  17660. };
  17661. /**
  17662. * Check if the tech can support the given type
  17663. *
  17664. * @param {string} type
  17665. * The mimetype to check
  17666. * @return {string} 'probably', 'maybe', or '' (empty string)
  17667. */
  17668. Html5.canPlayType = function (type) {
  17669. return Html5.TEST_VID.canPlayType(type);
  17670. };
  17671. /**
  17672. * Check if the tech can support the given source
  17673. *
  17674. * @param {Object} srcObj
  17675. * The source object
  17676. * @param {Object} options
  17677. * The options passed to the tech
  17678. * @return {string} 'probably', 'maybe', or '' (empty string)
  17679. */
  17680. Html5.canPlaySource = function (srcObj, options) {
  17681. return Html5.canPlayType(srcObj.type);
  17682. };
  17683. /**
  17684. * Check if the volume can be changed in this browser/device.
  17685. * Volume cannot be changed in a lot of mobile devices.
  17686. * Specifically, it can't be changed from 1 on iOS.
  17687. *
  17688. * @return {boolean}
  17689. * - True if volume can be controlled
  17690. * - False otherwise
  17691. */
  17692. Html5.canControlVolume = function () {
  17693. // IE will error if Windows Media Player not installed #3315
  17694. try {
  17695. const volume = Html5.TEST_VID.volume;
  17696. Html5.TEST_VID.volume = volume / 2 + 0.1;
  17697. const canControl = volume !== Html5.TEST_VID.volume;
  17698. // With the introduction of iOS 15, there are cases where the volume is read as
  17699. // changed but reverts back to its original state at the start of the next tick.
  17700. // To determine whether volume can be controlled on iOS,
  17701. // a timeout is set and the volume is checked asynchronously.
  17702. // Since `features` doesn't currently work asynchronously, the value is manually set.
  17703. if (canControl && IS_IOS) {
  17704. window.setTimeout(() => {
  17705. if (Html5 && Html5.prototype) {
  17706. Html5.prototype.featuresVolumeControl = volume !== Html5.TEST_VID.volume;
  17707. }
  17708. });
  17709. // default iOS to false, which will be updated in the timeout above.
  17710. return false;
  17711. }
  17712. return canControl;
  17713. } catch (e) {
  17714. return false;
  17715. }
  17716. };
  17717. /**
  17718. * Check if the volume can be muted in this browser/device.
  17719. * Some devices, e.g. iOS, don't allow changing volume
  17720. * but permits muting/unmuting.
  17721. *
  17722. * @return {boolean}
  17723. * - True if volume can be muted
  17724. * - False otherwise
  17725. */
  17726. Html5.canMuteVolume = function () {
  17727. try {
  17728. const muted = Html5.TEST_VID.muted;
  17729. // in some versions of iOS muted property doesn't always
  17730. // work, so we want to set both property and attribute
  17731. Html5.TEST_VID.muted = !muted;
  17732. if (Html5.TEST_VID.muted) {
  17733. setAttribute(Html5.TEST_VID, 'muted', 'muted');
  17734. } else {
  17735. removeAttribute(Html5.TEST_VID, 'muted', 'muted');
  17736. }
  17737. return muted !== Html5.TEST_VID.muted;
  17738. } catch (e) {
  17739. return false;
  17740. }
  17741. };
  17742. /**
  17743. * Check if the playback rate can be changed in this browser/device.
  17744. *
  17745. * @return {boolean}
  17746. * - True if playback rate can be controlled
  17747. * - False otherwise
  17748. */
  17749. Html5.canControlPlaybackRate = function () {
  17750. // Playback rate API is implemented in Android Chrome, but doesn't do anything
  17751. // https://github.com/videojs/video.js/issues/3180
  17752. if (IS_ANDROID && IS_CHROME && CHROME_VERSION < 58) {
  17753. return false;
  17754. }
  17755. // IE will error if Windows Media Player not installed #3315
  17756. try {
  17757. const playbackRate = Html5.TEST_VID.playbackRate;
  17758. Html5.TEST_VID.playbackRate = playbackRate / 2 + 0.1;
  17759. return playbackRate !== Html5.TEST_VID.playbackRate;
  17760. } catch (e) {
  17761. return false;
  17762. }
  17763. };
  17764. /**
  17765. * Check if we can override a video/audio elements attributes, with
  17766. * Object.defineProperty.
  17767. *
  17768. * @return {boolean}
  17769. * - True if builtin attributes can be overridden
  17770. * - False otherwise
  17771. */
  17772. Html5.canOverrideAttributes = function () {
  17773. // if we cannot overwrite the src/innerHTML property, there is no support
  17774. // iOS 7 safari for instance cannot do this.
  17775. try {
  17776. const noop = () => {};
  17777. Object.defineProperty(document.createElement('video'), 'src', {
  17778. get: noop,
  17779. set: noop
  17780. });
  17781. Object.defineProperty(document.createElement('audio'), 'src', {
  17782. get: noop,
  17783. set: noop
  17784. });
  17785. Object.defineProperty(document.createElement('video'), 'innerHTML', {
  17786. get: noop,
  17787. set: noop
  17788. });
  17789. Object.defineProperty(document.createElement('audio'), 'innerHTML', {
  17790. get: noop,
  17791. set: noop
  17792. });
  17793. } catch (e) {
  17794. return false;
  17795. }
  17796. return true;
  17797. };
  17798. /**
  17799. * Check to see if native `TextTrack`s are supported by this browser/device.
  17800. *
  17801. * @return {boolean}
  17802. * - True if native `TextTrack`s are supported.
  17803. * - False otherwise
  17804. */
  17805. Html5.supportsNativeTextTracks = function () {
  17806. return IS_ANY_SAFARI || IS_IOS && IS_CHROME;
  17807. };
  17808. /**
  17809. * Check to see if native `VideoTrack`s are supported by this browser/device
  17810. *
  17811. * @return {boolean}
  17812. * - True if native `VideoTrack`s are supported.
  17813. * - False otherwise
  17814. */
  17815. Html5.supportsNativeVideoTracks = function () {
  17816. return !!(Html5.TEST_VID && Html5.TEST_VID.videoTracks);
  17817. };
  17818. /**
  17819. * Check to see if native `AudioTrack`s are supported by this browser/device
  17820. *
  17821. * @return {boolean}
  17822. * - True if native `AudioTrack`s are supported.
  17823. * - False otherwise
  17824. */
  17825. Html5.supportsNativeAudioTracks = function () {
  17826. return !!(Html5.TEST_VID && Html5.TEST_VID.audioTracks);
  17827. };
  17828. /**
  17829. * An array of events available on the Html5 tech.
  17830. *
  17831. * @private
  17832. * @type {Array}
  17833. */
  17834. Html5.Events = ['loadstart', 'suspend', 'abort', 'error', 'emptied', 'stalled', 'loadedmetadata', 'loadeddata', 'canplay', 'canplaythrough', 'playing', 'waiting', 'seeking', 'seeked', 'ended', 'durationchange', 'timeupdate', 'progress', 'play', 'pause', 'ratechange', 'resize', 'volumechange'];
  17835. /**
  17836. * Boolean indicating whether the `Tech` supports volume control.
  17837. *
  17838. * @type {boolean}
  17839. * @default {@link Html5.canControlVolume}
  17840. */
  17841. /**
  17842. * Boolean indicating whether the `Tech` supports muting volume.
  17843. *
  17844. * @type {boolean}
  17845. * @default {@link Html5.canMuteVolume}
  17846. */
  17847. /**
  17848. * Boolean indicating whether the `Tech` supports changing the speed at which the media
  17849. * plays. Examples:
  17850. * - Set player to play 2x (twice) as fast
  17851. * - Set player to play 0.5x (half) as fast
  17852. *
  17853. * @type {boolean}
  17854. * @default {@link Html5.canControlPlaybackRate}
  17855. */
  17856. /**
  17857. * Boolean indicating whether the `Tech` supports the `sourceset` event.
  17858. *
  17859. * @type {boolean}
  17860. * @default
  17861. */
  17862. /**
  17863. * Boolean indicating whether the `HTML5` tech currently supports native `TextTrack`s.
  17864. *
  17865. * @type {boolean}
  17866. * @default {@link Html5.supportsNativeTextTracks}
  17867. */
  17868. /**
  17869. * Boolean indicating whether the `HTML5` tech currently supports native `VideoTrack`s.
  17870. *
  17871. * @type {boolean}
  17872. * @default {@link Html5.supportsNativeVideoTracks}
  17873. */
  17874. /**
  17875. * Boolean indicating whether the `HTML5` tech currently supports native `AudioTrack`s.
  17876. *
  17877. * @type {boolean}
  17878. * @default {@link Html5.supportsNativeAudioTracks}
  17879. */
  17880. [['featuresMuteControl', 'canMuteVolume'], ['featuresPlaybackRate', 'canControlPlaybackRate'], ['featuresSourceset', 'canOverrideAttributes'], ['featuresNativeTextTracks', 'supportsNativeTextTracks'], ['featuresNativeVideoTracks', 'supportsNativeVideoTracks'], ['featuresNativeAudioTracks', 'supportsNativeAudioTracks']].forEach(function ([key, fn]) {
  17881. defineLazyProperty(Html5.prototype, key, () => Html5[fn](), true);
  17882. });
  17883. Html5.prototype.featuresVolumeControl = Html5.canControlVolume();
  17884. /**
  17885. * Boolean indicating whether the `HTML5` tech currently supports the media element
  17886. * moving in the DOM. iOS breaks if you move the media element, so this is set this to
  17887. * false there. Everywhere else this should be true.
  17888. *
  17889. * @type {boolean}
  17890. * @default
  17891. */
  17892. Html5.prototype.movingMediaElementInDOM = !IS_IOS;
  17893. // TODO: Previous comment: No longer appears to be used. Can probably be removed.
  17894. // Is this true?
  17895. /**
  17896. * Boolean indicating whether the `HTML5` tech currently supports automatic media resize
  17897. * when going into fullscreen.
  17898. *
  17899. * @type {boolean}
  17900. * @default
  17901. */
  17902. Html5.prototype.featuresFullscreenResize = true;
  17903. /**
  17904. * Boolean indicating whether the `HTML5` tech currently supports the progress event.
  17905. * If this is false, manual `progress` events will be triggered instead.
  17906. *
  17907. * @type {boolean}
  17908. * @default
  17909. */
  17910. Html5.prototype.featuresProgressEvents = true;
  17911. /**
  17912. * Boolean indicating whether the `HTML5` tech currently supports the timeupdate event.
  17913. * If this is false, manual `timeupdate` events will be triggered instead.
  17914. *
  17915. * @default
  17916. */
  17917. Html5.prototype.featuresTimeupdateEvents = true;
  17918. /**
  17919. * Whether the HTML5 el supports `requestVideoFrameCallback`
  17920. *
  17921. * @type {boolean}
  17922. */
  17923. Html5.prototype.featuresVideoFrameCallback = !!(Html5.TEST_VID && Html5.TEST_VID.requestVideoFrameCallback);
  17924. Html5.disposeMediaElement = function (el) {
  17925. if (!el) {
  17926. return;
  17927. }
  17928. if (el.parentNode) {
  17929. el.parentNode.removeChild(el);
  17930. }
  17931. // remove any child track or source nodes to prevent their loading
  17932. while (el.hasChildNodes()) {
  17933. el.removeChild(el.firstChild);
  17934. }
  17935. // remove any src reference. not setting `src=''` because that causes a warning
  17936. // in firefox
  17937. el.removeAttribute('src');
  17938. // force the media element to update its loading state by calling load()
  17939. // however IE on Windows 7N has a bug that throws an error so need a try/catch (#793)
  17940. if (typeof el.load === 'function') {
  17941. // wrapping in an iife so it's not deoptimized (#1060#discussion_r10324473)
  17942. (function () {
  17943. try {
  17944. el.load();
  17945. } catch (e) {
  17946. // not supported
  17947. }
  17948. })();
  17949. }
  17950. };
  17951. Html5.resetMediaElement = function (el) {
  17952. if (!el) {
  17953. return;
  17954. }
  17955. const sources = el.querySelectorAll('source');
  17956. let i = sources.length;
  17957. while (i--) {
  17958. el.removeChild(sources[i]);
  17959. }
  17960. // remove any src reference.
  17961. // not setting `src=''` because that throws an error
  17962. el.removeAttribute('src');
  17963. if (typeof el.load === 'function') {
  17964. // wrapping in an iife so it's not deoptimized (#1060#discussion_r10324473)
  17965. (function () {
  17966. try {
  17967. el.load();
  17968. } catch (e) {
  17969. // satisfy linter
  17970. }
  17971. })();
  17972. }
  17973. };
  17974. /* Native HTML5 element property wrapping ----------------------------------- */
  17975. // Wrap native boolean attributes with getters that check both property and attribute
  17976. // The list is as followed:
  17977. // muted, defaultMuted, autoplay, controls, loop, playsinline
  17978. [
  17979. /**
  17980. * Get the value of `muted` from the media element. `muted` indicates
  17981. * that the volume for the media should be set to silent. This does not actually change
  17982. * the `volume` attribute.
  17983. *
  17984. * @method Html5#muted
  17985. * @return {boolean}
  17986. * - True if the value of `volume` should be ignored and the audio set to silent.
  17987. * - False if the value of `volume` should be used.
  17988. *
  17989. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-muted}
  17990. */
  17991. 'muted',
  17992. /**
  17993. * Get the value of `defaultMuted` from the media element. `defaultMuted` indicates
  17994. * whether the media should start muted or not. Only changes the default state of the
  17995. * media. `muted` and `defaultMuted` can have different values. {@link Html5#muted} indicates the
  17996. * current state.
  17997. *
  17998. * @method Html5#defaultMuted
  17999. * @return {boolean}
  18000. * - The value of `defaultMuted` from the media element.
  18001. * - True indicates that the media should start muted.
  18002. * - False indicates that the media should not start muted
  18003. *
  18004. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultmuted}
  18005. */
  18006. 'defaultMuted',
  18007. /**
  18008. * Get the value of `autoplay` from the media element. `autoplay` indicates
  18009. * that the media should start to play as soon as the page is ready.
  18010. *
  18011. * @method Html5#autoplay
  18012. * @return {boolean}
  18013. * - The value of `autoplay` from the media element.
  18014. * - True indicates that the media should start as soon as the page loads.
  18015. * - False indicates that the media should not start as soon as the page loads.
  18016. *
  18017. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-autoplay}
  18018. */
  18019. 'autoplay',
  18020. /**
  18021. * Get the value of `controls` from the media element. `controls` indicates
  18022. * whether the native media controls should be shown or hidden.
  18023. *
  18024. * @method Html5#controls
  18025. * @return {boolean}
  18026. * - The value of `controls` from the media element.
  18027. * - True indicates that native controls should be showing.
  18028. * - False indicates that native controls should be hidden.
  18029. *
  18030. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-controls}
  18031. */
  18032. 'controls',
  18033. /**
  18034. * Get the value of `loop` from the media element. `loop` indicates
  18035. * that the media should return to the start of the media and continue playing once
  18036. * it reaches the end.
  18037. *
  18038. * @method Html5#loop
  18039. * @return {boolean}
  18040. * - The value of `loop` from the media element.
  18041. * - True indicates that playback should seek back to start once
  18042. * the end of a media is reached.
  18043. * - False indicates that playback should not loop back to the start when the
  18044. * end of the media is reached.
  18045. *
  18046. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-loop}
  18047. */
  18048. 'loop',
  18049. /**
  18050. * Get the value of `playsinline` from the media element. `playsinline` indicates
  18051. * to the browser that non-fullscreen playback is preferred when fullscreen
  18052. * playback is the native default, such as in iOS Safari.
  18053. *
  18054. * @method Html5#playsinline
  18055. * @return {boolean}
  18056. * - The value of `playsinline` from the media element.
  18057. * - True indicates that the media should play inline.
  18058. * - False indicates that the media should not play inline.
  18059. *
  18060. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  18061. */
  18062. 'playsinline'].forEach(function (prop) {
  18063. Html5.prototype[prop] = function () {
  18064. return this.el_[prop] || this.el_.hasAttribute(prop);
  18065. };
  18066. });
  18067. // Wrap native boolean attributes with setters that set both property and attribute
  18068. // The list is as followed:
  18069. // setMuted, setDefaultMuted, setAutoplay, setLoop, setPlaysinline
  18070. // setControls is special-cased above
  18071. [
  18072. /**
  18073. * Set the value of `muted` on the media element. `muted` indicates that the current
  18074. * audio level should be silent.
  18075. *
  18076. * @method Html5#setMuted
  18077. * @param {boolean} muted
  18078. * - True if the audio should be set to silent
  18079. * - False otherwise
  18080. *
  18081. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-muted}
  18082. */
  18083. 'muted',
  18084. /**
  18085. * Set the value of `defaultMuted` on the media element. `defaultMuted` indicates that the current
  18086. * audio level should be silent, but will only effect the muted level on initial playback..
  18087. *
  18088. * @method Html5.prototype.setDefaultMuted
  18089. * @param {boolean} defaultMuted
  18090. * - True if the audio should be set to silent
  18091. * - False otherwise
  18092. *
  18093. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultmuted}
  18094. */
  18095. 'defaultMuted',
  18096. /**
  18097. * Set the value of `autoplay` on the media element. `autoplay` indicates
  18098. * that the media should start to play as soon as the page is ready.
  18099. *
  18100. * @method Html5#setAutoplay
  18101. * @param {boolean} autoplay
  18102. * - True indicates that the media should start as soon as the page loads.
  18103. * - False indicates that the media should not start as soon as the page loads.
  18104. *
  18105. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-autoplay}
  18106. */
  18107. 'autoplay',
  18108. /**
  18109. * Set the value of `loop` on the media element. `loop` indicates
  18110. * that the media should return to the start of the media and continue playing once
  18111. * it reaches the end.
  18112. *
  18113. * @method Html5#setLoop
  18114. * @param {boolean} loop
  18115. * - True indicates that playback should seek back to start once
  18116. * the end of a media is reached.
  18117. * - False indicates that playback should not loop back to the start when the
  18118. * end of the media is reached.
  18119. *
  18120. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-loop}
  18121. */
  18122. 'loop',
  18123. /**
  18124. * Set the value of `playsinline` from the media element. `playsinline` indicates
  18125. * to the browser that non-fullscreen playback is preferred when fullscreen
  18126. * playback is the native default, such as in iOS Safari.
  18127. *
  18128. * @method Html5#setPlaysinline
  18129. * @param {boolean} playsinline
  18130. * - True indicates that the media should play inline.
  18131. * - False indicates that the media should not play inline.
  18132. *
  18133. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  18134. */
  18135. 'playsinline'].forEach(function (prop) {
  18136. Html5.prototype['set' + toTitleCase(prop)] = function (v) {
  18137. this.el_[prop] = v;
  18138. if (v) {
  18139. this.el_.setAttribute(prop, prop);
  18140. } else {
  18141. this.el_.removeAttribute(prop);
  18142. }
  18143. };
  18144. });
  18145. // Wrap native properties with a getter
  18146. // The list is as followed
  18147. // paused, currentTime, buffered, volume, poster, preload, error, seeking
  18148. // seekable, ended, playbackRate, defaultPlaybackRate, disablePictureInPicture
  18149. // played, networkState, readyState, videoWidth, videoHeight, crossOrigin
  18150. [
  18151. /**
  18152. * Get the value of `paused` from the media element. `paused` indicates whether the media element
  18153. * is currently paused or not.
  18154. *
  18155. * @method Html5#paused
  18156. * @return {boolean}
  18157. * The value of `paused` from the media element.
  18158. *
  18159. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-paused}
  18160. */
  18161. 'paused',
  18162. /**
  18163. * Get the value of `currentTime` from the media element. `currentTime` indicates
  18164. * the current second that the media is at in playback.
  18165. *
  18166. * @method Html5#currentTime
  18167. * @return {number}
  18168. * The value of `currentTime` from the media element.
  18169. *
  18170. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-currenttime}
  18171. */
  18172. 'currentTime',
  18173. /**
  18174. * Get the value of `buffered` from the media element. `buffered` is a `TimeRange`
  18175. * object that represents the parts of the media that are already downloaded and
  18176. * available for playback.
  18177. *
  18178. * @method Html5#buffered
  18179. * @return {TimeRange}
  18180. * The value of `buffered` from the media element.
  18181. *
  18182. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-buffered}
  18183. */
  18184. 'buffered',
  18185. /**
  18186. * Get the value of `volume` from the media element. `volume` indicates
  18187. * the current playback volume of audio for a media. `volume` will be a value from 0
  18188. * (silent) to 1 (loudest and default).
  18189. *
  18190. * @method Html5#volume
  18191. * @return {number}
  18192. * The value of `volume` from the media element. Value will be between 0-1.
  18193. *
  18194. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-a-volume}
  18195. */
  18196. 'volume',
  18197. /**
  18198. * Get the value of `poster` from the media element. `poster` indicates
  18199. * that the url of an image file that can/will be shown when no media data is available.
  18200. *
  18201. * @method Html5#poster
  18202. * @return {string}
  18203. * The value of `poster` from the media element. Value will be a url to an
  18204. * image.
  18205. *
  18206. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-video-poster}
  18207. */
  18208. 'poster',
  18209. /**
  18210. * Get the value of `preload` from the media element. `preload` indicates
  18211. * what should download before the media is interacted with. It can have the following
  18212. * values:
  18213. * - none: nothing should be downloaded
  18214. * - metadata: poster and the first few frames of the media may be downloaded to get
  18215. * media dimensions and other metadata
  18216. * - auto: allow the media and metadata for the media to be downloaded before
  18217. * interaction
  18218. *
  18219. * @method Html5#preload
  18220. * @return {string}
  18221. * The value of `preload` from the media element. Will be 'none', 'metadata',
  18222. * or 'auto'.
  18223. *
  18224. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-preload}
  18225. */
  18226. 'preload',
  18227. /**
  18228. * Get the value of the `error` from the media element. `error` indicates any
  18229. * MediaError that may have occurred during playback. If error returns null there is no
  18230. * current error.
  18231. *
  18232. * @method Html5#error
  18233. * @return {MediaError|null}
  18234. * The value of `error` from the media element. Will be `MediaError` if there
  18235. * is a current error and null otherwise.
  18236. *
  18237. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-error}
  18238. */
  18239. 'error',
  18240. /**
  18241. * Get the value of `seeking` from the media element. `seeking` indicates whether the
  18242. * media is currently seeking to a new position or not.
  18243. *
  18244. * @method Html5#seeking
  18245. * @return {boolean}
  18246. * - The value of `seeking` from the media element.
  18247. * - True indicates that the media is currently seeking to a new position.
  18248. * - False indicates that the media is not seeking to a new position at this time.
  18249. *
  18250. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-seeking}
  18251. */
  18252. 'seeking',
  18253. /**
  18254. * Get the value of `seekable` from the media element. `seekable` returns a
  18255. * `TimeRange` object indicating ranges of time that can currently be `seeked` to.
  18256. *
  18257. * @method Html5#seekable
  18258. * @return {TimeRange}
  18259. * The value of `seekable` from the media element. A `TimeRange` object
  18260. * indicating the current ranges of time that can be seeked to.
  18261. *
  18262. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-seekable}
  18263. */
  18264. 'seekable',
  18265. /**
  18266. * Get the value of `ended` from the media element. `ended` indicates whether
  18267. * the media has reached the end or not.
  18268. *
  18269. * @method Html5#ended
  18270. * @return {boolean}
  18271. * - The value of `ended` from the media element.
  18272. * - True indicates that the media has ended.
  18273. * - False indicates that the media has not ended.
  18274. *
  18275. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-ended}
  18276. */
  18277. 'ended',
  18278. /**
  18279. * Get the value of `playbackRate` from the media element. `playbackRate` indicates
  18280. * the rate at which the media is currently playing back. Examples:
  18281. * - if playbackRate is set to 2, media will play twice as fast.
  18282. * - if playbackRate is set to 0.5, media will play half as fast.
  18283. *
  18284. * @method Html5#playbackRate
  18285. * @return {number}
  18286. * The value of `playbackRate` from the media element. A number indicating
  18287. * the current playback speed of the media, where 1 is normal speed.
  18288. *
  18289. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18290. */
  18291. 'playbackRate',
  18292. /**
  18293. * Get the value of `defaultPlaybackRate` from the media element. `defaultPlaybackRate` indicates
  18294. * the rate at which the media is currently playing back. This value will not indicate the current
  18295. * `playbackRate` after playback has started, use {@link Html5#playbackRate} for that.
  18296. *
  18297. * Examples:
  18298. * - if defaultPlaybackRate is set to 2, media will play twice as fast.
  18299. * - if defaultPlaybackRate is set to 0.5, media will play half as fast.
  18300. *
  18301. * @method Html5.prototype.defaultPlaybackRate
  18302. * @return {number}
  18303. * The value of `defaultPlaybackRate` from the media element. A number indicating
  18304. * the current playback speed of the media, where 1 is normal speed.
  18305. *
  18306. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18307. */
  18308. 'defaultPlaybackRate',
  18309. /**
  18310. * Get the value of 'disablePictureInPicture' from the video element.
  18311. *
  18312. * @method Html5#disablePictureInPicture
  18313. * @return {boolean} value
  18314. * - The value of `disablePictureInPicture` from the video element.
  18315. * - True indicates that the video can't be played in Picture-In-Picture mode
  18316. * - False indicates that the video can be played in Picture-In-Picture mode
  18317. *
  18318. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/#disable-pip}
  18319. */
  18320. 'disablePictureInPicture',
  18321. /**
  18322. * Get the value of `played` from the media element. `played` returns a `TimeRange`
  18323. * object representing points in the media timeline that have been played.
  18324. *
  18325. * @method Html5#played
  18326. * @return {TimeRange}
  18327. * The value of `played` from the media element. A `TimeRange` object indicating
  18328. * the ranges of time that have been played.
  18329. *
  18330. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-played}
  18331. */
  18332. 'played',
  18333. /**
  18334. * Get the value of `networkState` from the media element. `networkState` indicates
  18335. * the current network state. It returns an enumeration from the following list:
  18336. * - 0: NETWORK_EMPTY
  18337. * - 1: NETWORK_IDLE
  18338. * - 2: NETWORK_LOADING
  18339. * - 3: NETWORK_NO_SOURCE
  18340. *
  18341. * @method Html5#networkState
  18342. * @return {number}
  18343. * The value of `networkState` from the media element. This will be a number
  18344. * from the list in the description.
  18345. *
  18346. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-networkstate}
  18347. */
  18348. 'networkState',
  18349. /**
  18350. * Get the value of `readyState` from the media element. `readyState` indicates
  18351. * the current state of the media element. It returns an enumeration from the
  18352. * following list:
  18353. * - 0: HAVE_NOTHING
  18354. * - 1: HAVE_METADATA
  18355. * - 2: HAVE_CURRENT_DATA
  18356. * - 3: HAVE_FUTURE_DATA
  18357. * - 4: HAVE_ENOUGH_DATA
  18358. *
  18359. * @method Html5#readyState
  18360. * @return {number}
  18361. * The value of `readyState` from the media element. This will be a number
  18362. * from the list in the description.
  18363. *
  18364. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#ready-states}
  18365. */
  18366. 'readyState',
  18367. /**
  18368. * Get the value of `videoWidth` from the video element. `videoWidth` indicates
  18369. * the current width of the video in css pixels.
  18370. *
  18371. * @method Html5#videoWidth
  18372. * @return {number}
  18373. * The value of `videoWidth` from the video element. This will be a number
  18374. * in css pixels.
  18375. *
  18376. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-video-videowidth}
  18377. */
  18378. 'videoWidth',
  18379. /**
  18380. * Get the value of `videoHeight` from the video element. `videoHeight` indicates
  18381. * the current height of the video in css pixels.
  18382. *
  18383. * @method Html5#videoHeight
  18384. * @return {number}
  18385. * The value of `videoHeight` from the video element. This will be a number
  18386. * in css pixels.
  18387. *
  18388. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-video-videowidth}
  18389. */
  18390. 'videoHeight',
  18391. /**
  18392. * Get the value of `crossOrigin` from the media element. `crossOrigin` indicates
  18393. * to the browser that should sent the cookies along with the requests for the
  18394. * different assets/playlists
  18395. *
  18396. * @method Html5#crossOrigin
  18397. * @return {string}
  18398. * - anonymous indicates that the media should not sent cookies.
  18399. * - use-credentials indicates that the media should sent cookies along the requests.
  18400. *
  18401. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-media-crossorigin}
  18402. */
  18403. 'crossOrigin'].forEach(function (prop) {
  18404. Html5.prototype[prop] = function () {
  18405. return this.el_[prop];
  18406. };
  18407. });
  18408. // Wrap native properties with a setter in this format:
  18409. // set + toTitleCase(name)
  18410. // The list is as follows:
  18411. // setVolume, setSrc, setPoster, setPreload, setPlaybackRate, setDefaultPlaybackRate,
  18412. // setDisablePictureInPicture, setCrossOrigin
  18413. [
  18414. /**
  18415. * Set the value of `volume` on the media element. `volume` indicates the current
  18416. * audio level as a percentage in decimal form. This means that 1 is 100%, 0.5 is 50%, and
  18417. * so on.
  18418. *
  18419. * @method Html5#setVolume
  18420. * @param {number} percentAsDecimal
  18421. * The volume percent as a decimal. Valid range is from 0-1.
  18422. *
  18423. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-a-volume}
  18424. */
  18425. 'volume',
  18426. /**
  18427. * Set the value of `src` on the media element. `src` indicates the current
  18428. * {@link Tech~SourceObject} for the media.
  18429. *
  18430. * @method Html5#setSrc
  18431. * @param {Tech~SourceObject} src
  18432. * The source object to set as the current source.
  18433. *
  18434. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-src}
  18435. */
  18436. 'src',
  18437. /**
  18438. * Set the value of `poster` on the media element. `poster` is the url to
  18439. * an image file that can/will be shown when no media data is available.
  18440. *
  18441. * @method Html5#setPoster
  18442. * @param {string} poster
  18443. * The url to an image that should be used as the `poster` for the media
  18444. * element.
  18445. *
  18446. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-poster}
  18447. */
  18448. 'poster',
  18449. /**
  18450. * Set the value of `preload` on the media element. `preload` indicates
  18451. * what should download before the media is interacted with. It can have the following
  18452. * values:
  18453. * - none: nothing should be downloaded
  18454. * - metadata: poster and the first few frames of the media may be downloaded to get
  18455. * media dimensions and other metadata
  18456. * - auto: allow the media and metadata for the media to be downloaded before
  18457. * interaction
  18458. *
  18459. * @method Html5#setPreload
  18460. * @param {string} preload
  18461. * The value of `preload` to set on the media element. Must be 'none', 'metadata',
  18462. * or 'auto'.
  18463. *
  18464. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-preload}
  18465. */
  18466. 'preload',
  18467. /**
  18468. * Set the value of `playbackRate` on the media element. `playbackRate` indicates
  18469. * the rate at which the media should play back. Examples:
  18470. * - if playbackRate is set to 2, media will play twice as fast.
  18471. * - if playbackRate is set to 0.5, media will play half as fast.
  18472. *
  18473. * @method Html5#setPlaybackRate
  18474. * @return {number}
  18475. * The value of `playbackRate` from the media element. A number indicating
  18476. * the current playback speed of the media, where 1 is normal speed.
  18477. *
  18478. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18479. */
  18480. 'playbackRate',
  18481. /**
  18482. * Set the value of `defaultPlaybackRate` on the media element. `defaultPlaybackRate` indicates
  18483. * the rate at which the media should play back upon initial startup. Changing this value
  18484. * after a video has started will do nothing. Instead you should used {@link Html5#setPlaybackRate}.
  18485. *
  18486. * Example Values:
  18487. * - if playbackRate is set to 2, media will play twice as fast.
  18488. * - if playbackRate is set to 0.5, media will play half as fast.
  18489. *
  18490. * @method Html5.prototype.setDefaultPlaybackRate
  18491. * @return {number}
  18492. * The value of `defaultPlaybackRate` from the media element. A number indicating
  18493. * the current playback speed of the media, where 1 is normal speed.
  18494. *
  18495. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultplaybackrate}
  18496. */
  18497. 'defaultPlaybackRate',
  18498. /**
  18499. * Prevents the browser from suggesting a Picture-in-Picture context menu
  18500. * or to request Picture-in-Picture automatically in some cases.
  18501. *
  18502. * @method Html5#setDisablePictureInPicture
  18503. * @param {boolean} value
  18504. * The true value will disable Picture-in-Picture mode.
  18505. *
  18506. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/#disable-pip}
  18507. */
  18508. 'disablePictureInPicture',
  18509. /**
  18510. * Set the value of `crossOrigin` from the media element. `crossOrigin` indicates
  18511. * to the browser that should sent the cookies along with the requests for the
  18512. * different assets/playlists
  18513. *
  18514. * @method Html5#setCrossOrigin
  18515. * @param {string} crossOrigin
  18516. * - anonymous indicates that the media should not sent cookies.
  18517. * - use-credentials indicates that the media should sent cookies along the requests.
  18518. *
  18519. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-media-crossorigin}
  18520. */
  18521. 'crossOrigin'].forEach(function (prop) {
  18522. Html5.prototype['set' + toTitleCase(prop)] = function (v) {
  18523. this.el_[prop] = v;
  18524. };
  18525. });
  18526. // wrap native functions with a function
  18527. // The list is as follows:
  18528. // pause, load, play
  18529. [
  18530. /**
  18531. * A wrapper around the media elements `pause` function. This will call the `HTML5`
  18532. * media elements `pause` function.
  18533. *
  18534. * @method Html5#pause
  18535. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-pause}
  18536. */
  18537. 'pause',
  18538. /**
  18539. * A wrapper around the media elements `load` function. This will call the `HTML5`s
  18540. * media element `load` function.
  18541. *
  18542. * @method Html5#load
  18543. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-load}
  18544. */
  18545. 'load',
  18546. /**
  18547. * A wrapper around the media elements `play` function. This will call the `HTML5`s
  18548. * media element `play` function.
  18549. *
  18550. * @method Html5#play
  18551. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-play}
  18552. */
  18553. 'play'].forEach(function (prop) {
  18554. Html5.prototype[prop] = function () {
  18555. return this.el_[prop]();
  18556. };
  18557. });
  18558. Tech.withSourceHandlers(Html5);
  18559. /**
  18560. * Native source handler for Html5, simply passes the source to the media element.
  18561. *
  18562. * @property {Tech~SourceObject} source
  18563. * The source object
  18564. *
  18565. * @property {Html5} tech
  18566. * The instance of the HTML5 tech.
  18567. */
  18568. Html5.nativeSourceHandler = {};
  18569. /**
  18570. * Check if the media element can play the given mime type.
  18571. *
  18572. * @param {string} type
  18573. * The mimetype to check
  18574. *
  18575. * @return {string}
  18576. * 'probably', 'maybe', or '' (empty string)
  18577. */
  18578. Html5.nativeSourceHandler.canPlayType = function (type) {
  18579. // IE without MediaPlayer throws an error (#519)
  18580. try {
  18581. return Html5.TEST_VID.canPlayType(type);
  18582. } catch (e) {
  18583. return '';
  18584. }
  18585. };
  18586. /**
  18587. * Check if the media element can handle a source natively.
  18588. *
  18589. * @param {Tech~SourceObject} source
  18590. * The source object
  18591. *
  18592. * @param {Object} [options]
  18593. * Options to be passed to the tech.
  18594. *
  18595. * @return {string}
  18596. * 'probably', 'maybe', or '' (empty string).
  18597. */
  18598. Html5.nativeSourceHandler.canHandleSource = function (source, options) {
  18599. // If a type was provided we should rely on that
  18600. if (source.type) {
  18601. return Html5.nativeSourceHandler.canPlayType(source.type);
  18602. // If no type, fall back to checking 'video/[EXTENSION]'
  18603. } else if (source.src) {
  18604. const ext = getFileExtension(source.src);
  18605. return Html5.nativeSourceHandler.canPlayType(`video/${ext}`);
  18606. }
  18607. return '';
  18608. };
  18609. /**
  18610. * Pass the source to the native media element.
  18611. *
  18612. * @param {Tech~SourceObject} source
  18613. * The source object
  18614. *
  18615. * @param {Html5} tech
  18616. * The instance of the Html5 tech
  18617. *
  18618. * @param {Object} [options]
  18619. * The options to pass to the source
  18620. */
  18621. Html5.nativeSourceHandler.handleSource = function (source, tech, options) {
  18622. tech.setSrc(source.src);
  18623. };
  18624. /**
  18625. * A noop for the native dispose function, as cleanup is not needed.
  18626. */
  18627. Html5.nativeSourceHandler.dispose = function () {};
  18628. // Register the native source handler
  18629. Html5.registerSourceHandler(Html5.nativeSourceHandler);
  18630. Tech.registerTech('Html5', Html5);
  18631. /**
  18632. * @file player.js
  18633. */
  18634. // The following tech events are simply re-triggered
  18635. // on the player when they happen
  18636. const TECH_EVENTS_RETRIGGER = [
  18637. /**
  18638. * Fired while the user agent is downloading media data.
  18639. *
  18640. * @event Player#progress
  18641. * @type {Event}
  18642. */
  18643. /**
  18644. * Retrigger the `progress` event that was triggered by the {@link Tech}.
  18645. *
  18646. * @private
  18647. * @method Player#handleTechProgress_
  18648. * @fires Player#progress
  18649. * @listens Tech#progress
  18650. */
  18651. 'progress',
  18652. /**
  18653. * Fires when the loading of an audio/video is aborted.
  18654. *
  18655. * @event Player#abort
  18656. * @type {Event}
  18657. */
  18658. /**
  18659. * Retrigger the `abort` event that was triggered by the {@link Tech}.
  18660. *
  18661. * @private
  18662. * @method Player#handleTechAbort_
  18663. * @fires Player#abort
  18664. * @listens Tech#abort
  18665. */
  18666. 'abort',
  18667. /**
  18668. * Fires when the browser is intentionally not getting media data.
  18669. *
  18670. * @event Player#suspend
  18671. * @type {Event}
  18672. */
  18673. /**
  18674. * Retrigger the `suspend` event that was triggered by the {@link Tech}.
  18675. *
  18676. * @private
  18677. * @method Player#handleTechSuspend_
  18678. * @fires Player#suspend
  18679. * @listens Tech#suspend
  18680. */
  18681. 'suspend',
  18682. /**
  18683. * Fires when the current playlist is empty.
  18684. *
  18685. * @event Player#emptied
  18686. * @type {Event}
  18687. */
  18688. /**
  18689. * Retrigger the `emptied` event that was triggered by the {@link Tech}.
  18690. *
  18691. * @private
  18692. * @method Player#handleTechEmptied_
  18693. * @fires Player#emptied
  18694. * @listens Tech#emptied
  18695. */
  18696. 'emptied',
  18697. /**
  18698. * Fires when the browser is trying to get media data, but data is not available.
  18699. *
  18700. * @event Player#stalled
  18701. * @type {Event}
  18702. */
  18703. /**
  18704. * Retrigger the `stalled` event that was triggered by the {@link Tech}.
  18705. *
  18706. * @private
  18707. * @method Player#handleTechStalled_
  18708. * @fires Player#stalled
  18709. * @listens Tech#stalled
  18710. */
  18711. 'stalled',
  18712. /**
  18713. * Fires when the browser has loaded meta data for the audio/video.
  18714. *
  18715. * @event Player#loadedmetadata
  18716. * @type {Event}
  18717. */
  18718. /**
  18719. * Retrigger the `loadedmetadata` event that was triggered by the {@link Tech}.
  18720. *
  18721. * @private
  18722. * @method Player#handleTechLoadedmetadata_
  18723. * @fires Player#loadedmetadata
  18724. * @listens Tech#loadedmetadata
  18725. */
  18726. 'loadedmetadata',
  18727. /**
  18728. * Fires when the browser has loaded the current frame of the audio/video.
  18729. *
  18730. * @event Player#loadeddata
  18731. * @type {event}
  18732. */
  18733. /**
  18734. * Retrigger the `loadeddata` event that was triggered by the {@link Tech}.
  18735. *
  18736. * @private
  18737. * @method Player#handleTechLoaddeddata_
  18738. * @fires Player#loadeddata
  18739. * @listens Tech#loadeddata
  18740. */
  18741. 'loadeddata',
  18742. /**
  18743. * Fires when the current playback position has changed.
  18744. *
  18745. * @event Player#timeupdate
  18746. * @type {event}
  18747. */
  18748. /**
  18749. * Retrigger the `timeupdate` event that was triggered by the {@link Tech}.
  18750. *
  18751. * @private
  18752. * @method Player#handleTechTimeUpdate_
  18753. * @fires Player#timeupdate
  18754. * @listens Tech#timeupdate
  18755. */
  18756. 'timeupdate',
  18757. /**
  18758. * Fires when the video's intrinsic dimensions change
  18759. *
  18760. * @event Player#resize
  18761. * @type {event}
  18762. */
  18763. /**
  18764. * Retrigger the `resize` event that was triggered by the {@link Tech}.
  18765. *
  18766. * @private
  18767. * @method Player#handleTechResize_
  18768. * @fires Player#resize
  18769. * @listens Tech#resize
  18770. */
  18771. 'resize',
  18772. /**
  18773. * Fires when the volume has been changed
  18774. *
  18775. * @event Player#volumechange
  18776. * @type {event}
  18777. */
  18778. /**
  18779. * Retrigger the `volumechange` event that was triggered by the {@link Tech}.
  18780. *
  18781. * @private
  18782. * @method Player#handleTechVolumechange_
  18783. * @fires Player#volumechange
  18784. * @listens Tech#volumechange
  18785. */
  18786. 'volumechange',
  18787. /**
  18788. * Fires when the text track has been changed
  18789. *
  18790. * @event Player#texttrackchange
  18791. * @type {event}
  18792. */
  18793. /**
  18794. * Retrigger the `texttrackchange` event that was triggered by the {@link Tech}.
  18795. *
  18796. * @private
  18797. * @method Player#handleTechTexttrackchange_
  18798. * @fires Player#texttrackchange
  18799. * @listens Tech#texttrackchange
  18800. */
  18801. 'texttrackchange'];
  18802. // events to queue when playback rate is zero
  18803. // this is a hash for the sole purpose of mapping non-camel-cased event names
  18804. // to camel-cased function names
  18805. const TECH_EVENTS_QUEUE = {
  18806. canplay: 'CanPlay',
  18807. canplaythrough: 'CanPlayThrough',
  18808. playing: 'Playing',
  18809. seeked: 'Seeked'
  18810. };
  18811. const BREAKPOINT_ORDER = ['tiny', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'huge'];
  18812. const BREAKPOINT_CLASSES = {};
  18813. // grep: vjs-layout-tiny
  18814. // grep: vjs-layout-x-small
  18815. // grep: vjs-layout-small
  18816. // grep: vjs-layout-medium
  18817. // grep: vjs-layout-large
  18818. // grep: vjs-layout-x-large
  18819. // grep: vjs-layout-huge
  18820. BREAKPOINT_ORDER.forEach(k => {
  18821. const v = k.charAt(0) === 'x' ? `x-${k.substring(1)}` : k;
  18822. BREAKPOINT_CLASSES[k] = `vjs-layout-${v}`;
  18823. });
  18824. const DEFAULT_BREAKPOINTS = {
  18825. tiny: 210,
  18826. xsmall: 320,
  18827. small: 425,
  18828. medium: 768,
  18829. large: 1440,
  18830. xlarge: 2560,
  18831. huge: Infinity
  18832. };
  18833. /**
  18834. * An instance of the `Player` class is created when any of the Video.js setup methods
  18835. * are used to initialize a video.
  18836. *
  18837. * After an instance has been created it can be accessed globally in three ways:
  18838. * 1. By calling `videojs.getPlayer('example_video_1');`
  18839. * 2. By calling `videojs('example_video_1');` (not recommended)
  18840. * 2. By using it directly via `videojs.players.example_video_1;`
  18841. *
  18842. * @extends Component
  18843. * @global
  18844. */
  18845. class Player extends Component {
  18846. /**
  18847. * Create an instance of this class.
  18848. *
  18849. * @param {Element} tag
  18850. * The original video DOM element used for configuring options.
  18851. *
  18852. * @param {Object} [options]
  18853. * Object of option names and values.
  18854. *
  18855. * @param {Function} [ready]
  18856. * Ready callback function.
  18857. */
  18858. constructor(tag, options, ready) {
  18859. // Make sure tag ID exists
  18860. // also here.. probably better
  18861. tag.id = tag.id || options.id || `vjs_video_${newGUID()}`;
  18862. // Set Options
  18863. // The options argument overrides options set in the video tag
  18864. // which overrides globally set options.
  18865. // This latter part coincides with the load order
  18866. // (tag must exist before Player)
  18867. options = Object.assign(Player.getTagSettings(tag), options);
  18868. // Delay the initialization of children because we need to set up
  18869. // player properties first, and can't use `this` before `super()`
  18870. options.initChildren = false;
  18871. // Same with creating the element
  18872. options.createEl = false;
  18873. // don't auto mixin the evented mixin
  18874. options.evented = false;
  18875. // we don't want the player to report touch activity on itself
  18876. // see enableTouchActivity in Component
  18877. options.reportTouchActivity = false;
  18878. // If language is not set, get the closest lang attribute
  18879. if (!options.language) {
  18880. const closest = tag.closest('[lang]');
  18881. if (closest) {
  18882. options.language = closest.getAttribute('lang');
  18883. }
  18884. }
  18885. // Run base component initializing with new options
  18886. super(null, options, ready);
  18887. // Create bound methods for document listeners.
  18888. this.boundDocumentFullscreenChange_ = e => this.documentFullscreenChange_(e);
  18889. this.boundFullWindowOnEscKey_ = e => this.fullWindowOnEscKey(e);
  18890. this.boundUpdateStyleEl_ = e => this.updateStyleEl_(e);
  18891. this.boundApplyInitTime_ = e => this.applyInitTime_(e);
  18892. this.boundUpdateCurrentBreakpoint_ = e => this.updateCurrentBreakpoint_(e);
  18893. this.boundHandleTechClick_ = e => this.handleTechClick_(e);
  18894. this.boundHandleTechDoubleClick_ = e => this.handleTechDoubleClick_(e);
  18895. this.boundHandleTechTouchStart_ = e => this.handleTechTouchStart_(e);
  18896. this.boundHandleTechTouchMove_ = e => this.handleTechTouchMove_(e);
  18897. this.boundHandleTechTouchEnd_ = e => this.handleTechTouchEnd_(e);
  18898. this.boundHandleTechTap_ = e => this.handleTechTap_(e);
  18899. // default isFullscreen_ to false
  18900. this.isFullscreen_ = false;
  18901. // create logger
  18902. this.log = createLogger(this.id_);
  18903. // Hold our own reference to fullscreen api so it can be mocked in tests
  18904. this.fsApi_ = FullscreenApi;
  18905. // Tracks when a tech changes the poster
  18906. this.isPosterFromTech_ = false;
  18907. // Holds callback info that gets queued when playback rate is zero
  18908. // and a seek is happening
  18909. this.queuedCallbacks_ = [];
  18910. // Turn off API access because we're loading a new tech that might load asynchronously
  18911. this.isReady_ = false;
  18912. // Init state hasStarted_
  18913. this.hasStarted_ = false;
  18914. // Init state userActive_
  18915. this.userActive_ = false;
  18916. // Init debugEnabled_
  18917. this.debugEnabled_ = false;
  18918. // Init state audioOnlyMode_
  18919. this.audioOnlyMode_ = false;
  18920. // Init state audioPosterMode_
  18921. this.audioPosterMode_ = false;
  18922. // Init state audioOnlyCache_
  18923. this.audioOnlyCache_ = {
  18924. playerHeight: null,
  18925. hiddenChildren: []
  18926. };
  18927. // if the global option object was accidentally blown away by
  18928. // someone, bail early with an informative error
  18929. if (!this.options_ || !this.options_.techOrder || !this.options_.techOrder.length) {
  18930. throw new Error('No techOrder specified. Did you overwrite ' + 'videojs.options instead of just changing the ' + 'properties you want to override?');
  18931. }
  18932. // Store the original tag used to set options
  18933. this.tag = tag;
  18934. // Store the tag attributes used to restore html5 element
  18935. this.tagAttributes = tag && getAttributes(tag);
  18936. // Update current language
  18937. this.language(this.options_.language);
  18938. // Update Supported Languages
  18939. if (options.languages) {
  18940. // Normalise player option languages to lowercase
  18941. const languagesToLower = {};
  18942. Object.getOwnPropertyNames(options.languages).forEach(function (name) {
  18943. languagesToLower[name.toLowerCase()] = options.languages[name];
  18944. });
  18945. this.languages_ = languagesToLower;
  18946. } else {
  18947. this.languages_ = Player.prototype.options_.languages;
  18948. }
  18949. this.resetCache_();
  18950. // Set poster
  18951. /** @type string */
  18952. this.poster_ = options.poster || '';
  18953. // Set controls
  18954. /** @type {boolean} */
  18955. this.controls_ = !!options.controls;
  18956. // Original tag settings stored in options
  18957. // now remove immediately so native controls don't flash.
  18958. // May be turned back on by HTML5 tech if nativeControlsForTouch is true
  18959. tag.controls = false;
  18960. tag.removeAttribute('controls');
  18961. this.changingSrc_ = false;
  18962. this.playCallbacks_ = [];
  18963. this.playTerminatedQueue_ = [];
  18964. // the attribute overrides the option
  18965. if (tag.hasAttribute('autoplay')) {
  18966. this.autoplay(true);
  18967. } else {
  18968. // otherwise use the setter to validate and
  18969. // set the correct value.
  18970. this.autoplay(this.options_.autoplay);
  18971. }
  18972. // check plugins
  18973. if (options.plugins) {
  18974. Object.keys(options.plugins).forEach(name => {
  18975. if (typeof this[name] !== 'function') {
  18976. throw new Error(`plugin "${name}" does not exist`);
  18977. }
  18978. });
  18979. }
  18980. /*
  18981. * Store the internal state of scrubbing
  18982. *
  18983. * @private
  18984. * @return {Boolean} True if the user is scrubbing
  18985. */
  18986. this.scrubbing_ = false;
  18987. this.el_ = this.createEl();
  18988. // Make this an evented object and use `el_` as its event bus.
  18989. evented(this, {
  18990. eventBusKey: 'el_'
  18991. });
  18992. // listen to document and player fullscreenchange handlers so we receive those events
  18993. // before a user can receive them so we can update isFullscreen appropriately.
  18994. // make sure that we listen to fullscreenchange events before everything else to make sure that
  18995. // our isFullscreen method is updated properly for internal components as well as external.
  18996. if (this.fsApi_.requestFullscreen) {
  18997. on(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  18998. this.on(this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  18999. }
  19000. if (this.fluid_) {
  19001. this.on(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19002. }
  19003. // We also want to pass the original player options to each component and plugin
  19004. // as well so they don't need to reach back into the player for options later.
  19005. // We also need to do another copy of this.options_ so we don't end up with
  19006. // an infinite loop.
  19007. const playerOptionsCopy = merge(this.options_);
  19008. // Load plugins
  19009. if (options.plugins) {
  19010. Object.keys(options.plugins).forEach(name => {
  19011. this[name](options.plugins[name]);
  19012. });
  19013. }
  19014. // Enable debug mode to fire debugon event for all plugins.
  19015. if (options.debug) {
  19016. this.debug(true);
  19017. }
  19018. this.options_.playerOptions = playerOptionsCopy;
  19019. this.middleware_ = [];
  19020. this.playbackRates(options.playbackRates);
  19021. if (options.experimentalSvgIcons) {
  19022. // Add SVG Sprite to the DOM
  19023. const parser = new window.DOMParser();
  19024. const parsedSVG = parser.parseFromString(icons, 'image/svg+xml');
  19025. const errorNode = parsedSVG.querySelector('parsererror');
  19026. if (errorNode) {
  19027. log.warn('Failed to load SVG Icons. Falling back to Font Icons.');
  19028. this.options_.experimentalSvgIcons = null;
  19029. } else {
  19030. const sprite = parsedSVG.documentElement;
  19031. sprite.style.display = 'none';
  19032. this.el_.appendChild(sprite);
  19033. this.addClass('vjs-svg-icons-enabled');
  19034. }
  19035. }
  19036. this.initChildren();
  19037. // Set isAudio based on whether or not an audio tag was used
  19038. this.isAudio(tag.nodeName.toLowerCase() === 'audio');
  19039. // Update controls className. Can't do this when the controls are initially
  19040. // set because the element doesn't exist yet.
  19041. if (this.controls()) {
  19042. this.addClass('vjs-controls-enabled');
  19043. } else {
  19044. this.addClass('vjs-controls-disabled');
  19045. }
  19046. // Set ARIA label and region role depending on player type
  19047. this.el_.setAttribute('role', 'region');
  19048. if (this.isAudio()) {
  19049. this.el_.setAttribute('aria-label', this.localize('Audio Player'));
  19050. } else {
  19051. this.el_.setAttribute('aria-label', this.localize('Video Player'));
  19052. }
  19053. if (this.isAudio()) {
  19054. this.addClass('vjs-audio');
  19055. }
  19056. // TODO: Make this smarter. Toggle user state between touching/mousing
  19057. // using events, since devices can have both touch and mouse events.
  19058. // TODO: Make this check be performed again when the window switches between monitors
  19059. // (See https://github.com/videojs/video.js/issues/5683)
  19060. if (TOUCH_ENABLED) {
  19061. this.addClass('vjs-touch-enabled');
  19062. }
  19063. // iOS Safari has broken hover handling
  19064. if (!IS_IOS) {
  19065. this.addClass('vjs-workinghover');
  19066. }
  19067. // Make player easily findable by ID
  19068. Player.players[this.id_] = this;
  19069. // Add a major version class to aid css in plugins
  19070. const majorVersion = version.split('.')[0];
  19071. this.addClass(`vjs-v${majorVersion}`);
  19072. // When the player is first initialized, trigger activity so components
  19073. // like the control bar show themselves if needed
  19074. this.userActive(true);
  19075. this.reportUserActivity();
  19076. this.one('play', e => this.listenForUserActivity_(e));
  19077. this.on('keydown', e => this.handleKeyDown(e));
  19078. this.on('languagechange', e => this.handleLanguagechange(e));
  19079. this.breakpoints(this.options_.breakpoints);
  19080. this.responsive(this.options_.responsive);
  19081. // Calling both the audio mode methods after the player is fully
  19082. // setup to be able to listen to the events triggered by them
  19083. this.on('ready', () => {
  19084. // Calling the audioPosterMode method first so that
  19085. // the audioOnlyMode can take precedence when both options are set to true
  19086. this.audioPosterMode(this.options_.audioPosterMode);
  19087. this.audioOnlyMode(this.options_.audioOnlyMode);
  19088. });
  19089. }
  19090. /**
  19091. * Destroys the video player and does any necessary cleanup.
  19092. *
  19093. * This is especially helpful if you are dynamically adding and removing videos
  19094. * to/from the DOM.
  19095. *
  19096. * @fires Player#dispose
  19097. */
  19098. dispose() {
  19099. /**
  19100. * Called when the player is being disposed of.
  19101. *
  19102. * @event Player#dispose
  19103. * @type {Event}
  19104. */
  19105. this.trigger('dispose');
  19106. // prevent dispose from being called twice
  19107. this.off('dispose');
  19108. // Make sure all player-specific document listeners are unbound. This is
  19109. off(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  19110. off(document, 'keydown', this.boundFullWindowOnEscKey_);
  19111. if (this.styleEl_ && this.styleEl_.parentNode) {
  19112. this.styleEl_.parentNode.removeChild(this.styleEl_);
  19113. this.styleEl_ = null;
  19114. }
  19115. // Kill reference to this player
  19116. Player.players[this.id_] = null;
  19117. if (this.tag && this.tag.player) {
  19118. this.tag.player = null;
  19119. }
  19120. if (this.el_ && this.el_.player) {
  19121. this.el_.player = null;
  19122. }
  19123. if (this.tech_) {
  19124. this.tech_.dispose();
  19125. this.isPosterFromTech_ = false;
  19126. this.poster_ = '';
  19127. }
  19128. if (this.playerElIngest_) {
  19129. this.playerElIngest_ = null;
  19130. }
  19131. if (this.tag) {
  19132. this.tag = null;
  19133. }
  19134. clearCacheForPlayer(this);
  19135. // remove all event handlers for track lists
  19136. // all tracks and track listeners are removed on
  19137. // tech dispose
  19138. ALL.names.forEach(name => {
  19139. const props = ALL[name];
  19140. const list = this[props.getterName]();
  19141. // if it is not a native list
  19142. // we have to manually remove event listeners
  19143. if (list && list.off) {
  19144. list.off();
  19145. }
  19146. });
  19147. // the actual .el_ is removed here, or replaced if
  19148. super.dispose({
  19149. restoreEl: this.options_.restoreEl
  19150. });
  19151. }
  19152. /**
  19153. * Create the `Player`'s DOM element.
  19154. *
  19155. * @return {Element}
  19156. * The DOM element that gets created.
  19157. */
  19158. createEl() {
  19159. let tag = this.tag;
  19160. let el;
  19161. let playerElIngest = this.playerElIngest_ = tag.parentNode && tag.parentNode.hasAttribute && tag.parentNode.hasAttribute('data-vjs-player');
  19162. const divEmbed = this.tag.tagName.toLowerCase() === 'video-js';
  19163. if (playerElIngest) {
  19164. el = this.el_ = tag.parentNode;
  19165. } else if (!divEmbed) {
  19166. el = this.el_ = super.createEl('div');
  19167. }
  19168. // Copy over all the attributes from the tag, including ID and class
  19169. // ID will now reference player box, not the video tag
  19170. const attrs = getAttributes(tag);
  19171. if (divEmbed) {
  19172. el = this.el_ = tag;
  19173. tag = this.tag = document.createElement('video');
  19174. while (el.children.length) {
  19175. tag.appendChild(el.firstChild);
  19176. }
  19177. if (!hasClass(el, 'video-js')) {
  19178. addClass(el, 'video-js');
  19179. }
  19180. el.appendChild(tag);
  19181. playerElIngest = this.playerElIngest_ = el;
  19182. // move properties over from our custom `video-js` element
  19183. // to our new `video` element. This will move things like
  19184. // `src` or `controls` that were set via js before the player
  19185. // was initialized.
  19186. Object.keys(el).forEach(k => {
  19187. try {
  19188. tag[k] = el[k];
  19189. } catch (e) {
  19190. // we got a a property like outerHTML which we can't actually copy, ignore it
  19191. }
  19192. });
  19193. }
  19194. // set tabindex to -1 to remove the video element from the focus order
  19195. tag.setAttribute('tabindex', '-1');
  19196. attrs.tabindex = '-1';
  19197. // Workaround for #4583 on Chrome (on Windows) with JAWS.
  19198. // See https://github.com/FreedomScientific/VFO-standards-support/issues/78
  19199. // Note that we can't detect if JAWS is being used, but this ARIA attribute
  19200. // doesn't change behavior of Chrome if JAWS is not being used
  19201. if (IS_CHROME && IS_WINDOWS) {
  19202. tag.setAttribute('role', 'application');
  19203. attrs.role = 'application';
  19204. }
  19205. // Remove width/height attrs from tag so CSS can make it 100% width/height
  19206. tag.removeAttribute('width');
  19207. tag.removeAttribute('height');
  19208. if ('width' in attrs) {
  19209. delete attrs.width;
  19210. }
  19211. if ('height' in attrs) {
  19212. delete attrs.height;
  19213. }
  19214. Object.getOwnPropertyNames(attrs).forEach(function (attr) {
  19215. // don't copy over the class attribute to the player element when we're in a div embed
  19216. // the class is already set up properly in the divEmbed case
  19217. // and we want to make sure that the `video-js` class doesn't get lost
  19218. if (!(divEmbed && attr === 'class')) {
  19219. el.setAttribute(attr, attrs[attr]);
  19220. }
  19221. if (divEmbed) {
  19222. tag.setAttribute(attr, attrs[attr]);
  19223. }
  19224. });
  19225. // Update tag id/class for use as HTML5 playback tech
  19226. // Might think we should do this after embedding in container so .vjs-tech class
  19227. // doesn't flash 100% width/height, but class only applies with .video-js parent
  19228. tag.playerId = tag.id;
  19229. tag.id += '_html5_api';
  19230. tag.className = 'vjs-tech';
  19231. // Make player findable on elements
  19232. tag.player = el.player = this;
  19233. // Default state of video is paused
  19234. this.addClass('vjs-paused');
  19235. // Add a style element in the player that we'll use to set the width/height
  19236. // of the player in a way that's still overridable by CSS, just like the
  19237. // video element
  19238. if (window.VIDEOJS_NO_DYNAMIC_STYLE !== true) {
  19239. this.styleEl_ = createStyleElement('vjs-styles-dimensions');
  19240. const defaultsStyleEl = $('.vjs-styles-defaults');
  19241. const head = $('head');
  19242. head.insertBefore(this.styleEl_, defaultsStyleEl ? defaultsStyleEl.nextSibling : head.firstChild);
  19243. }
  19244. this.fill_ = false;
  19245. this.fluid_ = false;
  19246. // Pass in the width/height/aspectRatio options which will update the style el
  19247. this.width(this.options_.width);
  19248. this.height(this.options_.height);
  19249. this.fill(this.options_.fill);
  19250. this.fluid(this.options_.fluid);
  19251. this.aspectRatio(this.options_.aspectRatio);
  19252. // support both crossOrigin and crossorigin to reduce confusion and issues around the name
  19253. this.crossOrigin(this.options_.crossOrigin || this.options_.crossorigin);
  19254. // Hide any links within the video/audio tag,
  19255. // because IE doesn't hide them completely from screen readers.
  19256. const links = tag.getElementsByTagName('a');
  19257. for (let i = 0; i < links.length; i++) {
  19258. const linkEl = links.item(i);
  19259. addClass(linkEl, 'vjs-hidden');
  19260. linkEl.setAttribute('hidden', 'hidden');
  19261. }
  19262. // insertElFirst seems to cause the networkState to flicker from 3 to 2, so
  19263. // keep track of the original for later so we can know if the source originally failed
  19264. tag.initNetworkState_ = tag.networkState;
  19265. // Wrap video tag in div (el/box) container
  19266. if (tag.parentNode && !playerElIngest) {
  19267. tag.parentNode.insertBefore(el, tag);
  19268. }
  19269. // insert the tag as the first child of the player element
  19270. // then manually add it to the children array so that this.addChild
  19271. // will work properly for other components
  19272. //
  19273. // Breaks iPhone, fixed in HTML5 setup.
  19274. prependTo(tag, el);
  19275. this.children_.unshift(tag);
  19276. // Set lang attr on player to ensure CSS :lang() in consistent with player
  19277. // if it's been set to something different to the doc
  19278. this.el_.setAttribute('lang', this.language_);
  19279. this.el_.setAttribute('translate', 'no');
  19280. this.el_ = el;
  19281. return el;
  19282. }
  19283. /**
  19284. * Get or set the `Player`'s crossOrigin option. For the HTML5 player, this
  19285. * sets the `crossOrigin` property on the `<video>` tag to control the CORS
  19286. * behavior.
  19287. *
  19288. * @see [Video Element Attributes]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin}
  19289. *
  19290. * @param {string|null} [value]
  19291. * The value to set the `Player`'s crossOrigin to. If an argument is
  19292. * given, must be one of `'anonymous'` or `'use-credentials'`, or 'null'.
  19293. *
  19294. * @return {string|null|undefined}
  19295. * - The current crossOrigin value of the `Player` when getting.
  19296. * - undefined when setting
  19297. */
  19298. crossOrigin(value) {
  19299. // `null` can be set to unset a value
  19300. if (typeof value === 'undefined') {
  19301. return this.techGet_('crossOrigin');
  19302. }
  19303. if (value !== null && value !== 'anonymous' && value !== 'use-credentials') {
  19304. log.warn(`crossOrigin must be null, "anonymous" or "use-credentials", given "${value}"`);
  19305. return;
  19306. }
  19307. this.techCall_('setCrossOrigin', value);
  19308. if (this.posterImage) {
  19309. this.posterImage.crossOrigin(value);
  19310. }
  19311. return;
  19312. }
  19313. /**
  19314. * A getter/setter for the `Player`'s width. Returns the player's configured value.
  19315. * To get the current width use `currentWidth()`.
  19316. *
  19317. * @param {number|string} [value]
  19318. * CSS value to set the `Player`'s width to.
  19319. *
  19320. * @return {number|undefined}
  19321. * - The current width of the `Player` when getting.
  19322. * - Nothing when setting
  19323. */
  19324. width(value) {
  19325. return this.dimension('width', value);
  19326. }
  19327. /**
  19328. * A getter/setter for the `Player`'s height. Returns the player's configured value.
  19329. * To get the current height use `currentheight()`.
  19330. *
  19331. * @param {number|string} [value]
  19332. * CSS value to set the `Player`'s height to.
  19333. *
  19334. * @return {number|undefined}
  19335. * - The current height of the `Player` when getting.
  19336. * - Nothing when setting
  19337. */
  19338. height(value) {
  19339. return this.dimension('height', value);
  19340. }
  19341. /**
  19342. * A getter/setter for the `Player`'s width & height.
  19343. *
  19344. * @param {string} dimension
  19345. * This string can be:
  19346. * - 'width'
  19347. * - 'height'
  19348. *
  19349. * @param {number|string} [value]
  19350. * Value for dimension specified in the first argument.
  19351. *
  19352. * @return {number}
  19353. * The dimension arguments value when getting (width/height).
  19354. */
  19355. dimension(dimension, value) {
  19356. const privDimension = dimension + '_';
  19357. if (value === undefined) {
  19358. return this[privDimension] || 0;
  19359. }
  19360. if (value === '' || value === 'auto') {
  19361. // If an empty string is given, reset the dimension to be automatic
  19362. this[privDimension] = undefined;
  19363. this.updateStyleEl_();
  19364. return;
  19365. }
  19366. const parsedVal = parseFloat(value);
  19367. if (isNaN(parsedVal)) {
  19368. log.error(`Improper value "${value}" supplied for for ${dimension}`);
  19369. return;
  19370. }
  19371. this[privDimension] = parsedVal;
  19372. this.updateStyleEl_();
  19373. }
  19374. /**
  19375. * A getter/setter/toggler for the vjs-fluid `className` on the `Player`.
  19376. *
  19377. * Turning this on will turn off fill mode.
  19378. *
  19379. * @param {boolean} [bool]
  19380. * - A value of true adds the class.
  19381. * - A value of false removes the class.
  19382. * - No value will be a getter.
  19383. *
  19384. * @return {boolean|undefined}
  19385. * - The value of fluid when getting.
  19386. * - `undefined` when setting.
  19387. */
  19388. fluid(bool) {
  19389. if (bool === undefined) {
  19390. return !!this.fluid_;
  19391. }
  19392. this.fluid_ = !!bool;
  19393. if (isEvented(this)) {
  19394. this.off(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19395. }
  19396. if (bool) {
  19397. this.addClass('vjs-fluid');
  19398. this.fill(false);
  19399. addEventedCallback(this, () => {
  19400. this.on(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19401. });
  19402. } else {
  19403. this.removeClass('vjs-fluid');
  19404. }
  19405. this.updateStyleEl_();
  19406. }
  19407. /**
  19408. * A getter/setter/toggler for the vjs-fill `className` on the `Player`.
  19409. *
  19410. * Turning this on will turn off fluid mode.
  19411. *
  19412. * @param {boolean} [bool]
  19413. * - A value of true adds the class.
  19414. * - A value of false removes the class.
  19415. * - No value will be a getter.
  19416. *
  19417. * @return {boolean|undefined}
  19418. * - The value of fluid when getting.
  19419. * - `undefined` when setting.
  19420. */
  19421. fill(bool) {
  19422. if (bool === undefined) {
  19423. return !!this.fill_;
  19424. }
  19425. this.fill_ = !!bool;
  19426. if (bool) {
  19427. this.addClass('vjs-fill');
  19428. this.fluid(false);
  19429. } else {
  19430. this.removeClass('vjs-fill');
  19431. }
  19432. }
  19433. /**
  19434. * Get/Set the aspect ratio
  19435. *
  19436. * @param {string} [ratio]
  19437. * Aspect ratio for player
  19438. *
  19439. * @return {string|undefined}
  19440. * returns the current aspect ratio when getting
  19441. */
  19442. /**
  19443. * A getter/setter for the `Player`'s aspect ratio.
  19444. *
  19445. * @param {string} [ratio]
  19446. * The value to set the `Player`'s aspect ratio to.
  19447. *
  19448. * @return {string|undefined}
  19449. * - The current aspect ratio of the `Player` when getting.
  19450. * - undefined when setting
  19451. */
  19452. aspectRatio(ratio) {
  19453. if (ratio === undefined) {
  19454. return this.aspectRatio_;
  19455. }
  19456. // Check for width:height format
  19457. if (!/^\d+\:\d+$/.test(ratio)) {
  19458. throw new Error('Improper value supplied for aspect ratio. The format should be width:height, for example 16:9.');
  19459. }
  19460. this.aspectRatio_ = ratio;
  19461. // We're assuming if you set an aspect ratio you want fluid mode,
  19462. // because in fixed mode you could calculate width and height yourself.
  19463. this.fluid(true);
  19464. this.updateStyleEl_();
  19465. }
  19466. /**
  19467. * Update styles of the `Player` element (height, width and aspect ratio).
  19468. *
  19469. * @private
  19470. * @listens Tech#loadedmetadata
  19471. */
  19472. updateStyleEl_() {
  19473. if (window.VIDEOJS_NO_DYNAMIC_STYLE === true) {
  19474. const width = typeof this.width_ === 'number' ? this.width_ : this.options_.width;
  19475. const height = typeof this.height_ === 'number' ? this.height_ : this.options_.height;
  19476. const techEl = this.tech_ && this.tech_.el();
  19477. if (techEl) {
  19478. if (width >= 0) {
  19479. techEl.width = width;
  19480. }
  19481. if (height >= 0) {
  19482. techEl.height = height;
  19483. }
  19484. }
  19485. return;
  19486. }
  19487. let width;
  19488. let height;
  19489. let aspectRatio;
  19490. let idClass;
  19491. // The aspect ratio is either used directly or to calculate width and height.
  19492. if (this.aspectRatio_ !== undefined && this.aspectRatio_ !== 'auto') {
  19493. // Use any aspectRatio that's been specifically set
  19494. aspectRatio = this.aspectRatio_;
  19495. } else if (this.videoWidth() > 0) {
  19496. // Otherwise try to get the aspect ratio from the video metadata
  19497. aspectRatio = this.videoWidth() + ':' + this.videoHeight();
  19498. } else {
  19499. // Or use a default. The video element's is 2:1, but 16:9 is more common.
  19500. aspectRatio = '16:9';
  19501. }
  19502. // Get the ratio as a decimal we can use to calculate dimensions
  19503. const ratioParts = aspectRatio.split(':');
  19504. const ratioMultiplier = ratioParts[1] / ratioParts[0];
  19505. if (this.width_ !== undefined) {
  19506. // Use any width that's been specifically set
  19507. width = this.width_;
  19508. } else if (this.height_ !== undefined) {
  19509. // Or calculate the width from the aspect ratio if a height has been set
  19510. width = this.height_ / ratioMultiplier;
  19511. } else {
  19512. // Or use the video's metadata, or use the video el's default of 300
  19513. width = this.videoWidth() || 300;
  19514. }
  19515. if (this.height_ !== undefined) {
  19516. // Use any height that's been specifically set
  19517. height = this.height_;
  19518. } else {
  19519. // Otherwise calculate the height from the ratio and the width
  19520. height = width * ratioMultiplier;
  19521. }
  19522. // Ensure the CSS class is valid by starting with an alpha character
  19523. if (/^[^a-zA-Z]/.test(this.id())) {
  19524. idClass = 'dimensions-' + this.id();
  19525. } else {
  19526. idClass = this.id() + '-dimensions';
  19527. }
  19528. // Ensure the right class is still on the player for the style element
  19529. this.addClass(idClass);
  19530. setTextContent(this.styleEl_, `
  19531. .${idClass} {
  19532. width: ${width}px;
  19533. height: ${height}px;
  19534. }
  19535. .${idClass}.vjs-fluid:not(.vjs-audio-only-mode) {
  19536. padding-top: ${ratioMultiplier * 100}%;
  19537. }
  19538. `);
  19539. }
  19540. /**
  19541. * Load/Create an instance of playback {@link Tech} including element
  19542. * and API methods. Then append the `Tech` element in `Player` as a child.
  19543. *
  19544. * @param {string} techName
  19545. * name of the playback technology
  19546. *
  19547. * @param {string} source
  19548. * video source
  19549. *
  19550. * @private
  19551. */
  19552. loadTech_(techName, source) {
  19553. // Pause and remove current playback technology
  19554. if (this.tech_) {
  19555. this.unloadTech_();
  19556. }
  19557. const titleTechName = toTitleCase(techName);
  19558. const camelTechName = techName.charAt(0).toLowerCase() + techName.slice(1);
  19559. // get rid of the HTML5 video tag as soon as we are using another tech
  19560. if (titleTechName !== 'Html5' && this.tag) {
  19561. Tech.getTech('Html5').disposeMediaElement(this.tag);
  19562. this.tag.player = null;
  19563. this.tag = null;
  19564. }
  19565. this.techName_ = titleTechName;
  19566. // Turn off API access because we're loading a new tech that might load asynchronously
  19567. this.isReady_ = false;
  19568. let autoplay = this.autoplay();
  19569. // if autoplay is a string (or `true` with normalizeAutoplay: true) we pass false to the tech
  19570. // because the player is going to handle autoplay on `loadstart`
  19571. if (typeof this.autoplay() === 'string' || this.autoplay() === true && this.options_.normalizeAutoplay) {
  19572. autoplay = false;
  19573. }
  19574. // Grab tech-specific options from player options and add source and parent element to use.
  19575. const techOptions = {
  19576. source,
  19577. autoplay,
  19578. 'nativeControlsForTouch': this.options_.nativeControlsForTouch,
  19579. 'playerId': this.id(),
  19580. 'techId': `${this.id()}_${camelTechName}_api`,
  19581. 'playsinline': this.options_.playsinline,
  19582. 'preload': this.options_.preload,
  19583. 'loop': this.options_.loop,
  19584. 'disablePictureInPicture': this.options_.disablePictureInPicture,
  19585. 'muted': this.options_.muted,
  19586. 'poster': this.poster(),
  19587. 'language': this.language(),
  19588. 'playerElIngest': this.playerElIngest_ || false,
  19589. 'vtt.js': this.options_['vtt.js'],
  19590. 'canOverridePoster': !!this.options_.techCanOverridePoster,
  19591. 'enableSourceset': this.options_.enableSourceset
  19592. };
  19593. ALL.names.forEach(name => {
  19594. const props = ALL[name];
  19595. techOptions[props.getterName] = this[props.privateName];
  19596. });
  19597. Object.assign(techOptions, this.options_[titleTechName]);
  19598. Object.assign(techOptions, this.options_[camelTechName]);
  19599. Object.assign(techOptions, this.options_[techName.toLowerCase()]);
  19600. if (this.tag) {
  19601. techOptions.tag = this.tag;
  19602. }
  19603. if (source && source.src === this.cache_.src && this.cache_.currentTime > 0) {
  19604. techOptions.startTime = this.cache_.currentTime;
  19605. }
  19606. // Initialize tech instance
  19607. const TechClass = Tech.getTech(techName);
  19608. if (!TechClass) {
  19609. throw new Error(`No Tech named '${titleTechName}' exists! '${titleTechName}' should be registered using videojs.registerTech()'`);
  19610. }
  19611. this.tech_ = new TechClass(techOptions);
  19612. // player.triggerReady is always async, so don't need this to be async
  19613. this.tech_.ready(bind_(this, this.handleTechReady_), true);
  19614. textTrackConverter.jsonToTextTracks(this.textTracksJson_ || [], this.tech_);
  19615. // Listen to all HTML5-defined events and trigger them on the player
  19616. TECH_EVENTS_RETRIGGER.forEach(event => {
  19617. this.on(this.tech_, event, e => this[`handleTech${toTitleCase(event)}_`](e));
  19618. });
  19619. Object.keys(TECH_EVENTS_QUEUE).forEach(event => {
  19620. this.on(this.tech_, event, eventObj => {
  19621. if (this.tech_.playbackRate() === 0 && this.tech_.seeking()) {
  19622. this.queuedCallbacks_.push({
  19623. callback: this[`handleTech${TECH_EVENTS_QUEUE[event]}_`].bind(this),
  19624. event: eventObj
  19625. });
  19626. return;
  19627. }
  19628. this[`handleTech${TECH_EVENTS_QUEUE[event]}_`](eventObj);
  19629. });
  19630. });
  19631. this.on(this.tech_, 'loadstart', e => this.handleTechLoadStart_(e));
  19632. this.on(this.tech_, 'sourceset', e => this.handleTechSourceset_(e));
  19633. this.on(this.tech_, 'waiting', e => this.handleTechWaiting_(e));
  19634. this.on(this.tech_, 'ended', e => this.handleTechEnded_(e));
  19635. this.on(this.tech_, 'seeking', e => this.handleTechSeeking_(e));
  19636. this.on(this.tech_, 'play', e => this.handleTechPlay_(e));
  19637. this.on(this.tech_, 'pause', e => this.handleTechPause_(e));
  19638. this.on(this.tech_, 'durationchange', e => this.handleTechDurationChange_(e));
  19639. this.on(this.tech_, 'fullscreenchange', (e, data) => this.handleTechFullscreenChange_(e, data));
  19640. this.on(this.tech_, 'fullscreenerror', (e, err) => this.handleTechFullscreenError_(e, err));
  19641. this.on(this.tech_, 'enterpictureinpicture', e => this.handleTechEnterPictureInPicture_(e));
  19642. this.on(this.tech_, 'leavepictureinpicture', e => this.handleTechLeavePictureInPicture_(e));
  19643. this.on(this.tech_, 'error', e => this.handleTechError_(e));
  19644. this.on(this.tech_, 'posterchange', e => this.handleTechPosterChange_(e));
  19645. this.on(this.tech_, 'textdata', e => this.handleTechTextData_(e));
  19646. this.on(this.tech_, 'ratechange', e => this.handleTechRateChange_(e));
  19647. this.on(this.tech_, 'loadedmetadata', this.boundUpdateStyleEl_);
  19648. this.usingNativeControls(this.techGet_('controls'));
  19649. if (this.controls() && !this.usingNativeControls()) {
  19650. this.addTechControlsListeners_();
  19651. }
  19652. // Add the tech element in the DOM if it was not already there
  19653. // Make sure to not insert the original video element if using Html5
  19654. if (this.tech_.el().parentNode !== this.el() && (titleTechName !== 'Html5' || !this.tag)) {
  19655. prependTo(this.tech_.el(), this.el());
  19656. }
  19657. // Get rid of the original video tag reference after the first tech is loaded
  19658. if (this.tag) {
  19659. this.tag.player = null;
  19660. this.tag = null;
  19661. }
  19662. }
  19663. /**
  19664. * Unload and dispose of the current playback {@link Tech}.
  19665. *
  19666. * @private
  19667. */
  19668. unloadTech_() {
  19669. // Save the current text tracks so that we can reuse the same text tracks with the next tech
  19670. ALL.names.forEach(name => {
  19671. const props = ALL[name];
  19672. this[props.privateName] = this[props.getterName]();
  19673. });
  19674. this.textTracksJson_ = textTrackConverter.textTracksToJson(this.tech_);
  19675. this.isReady_ = false;
  19676. this.tech_.dispose();
  19677. this.tech_ = false;
  19678. if (this.isPosterFromTech_) {
  19679. this.poster_ = '';
  19680. this.trigger('posterchange');
  19681. }
  19682. this.isPosterFromTech_ = false;
  19683. }
  19684. /**
  19685. * Return a reference to the current {@link Tech}.
  19686. * It will print a warning by default about the danger of using the tech directly
  19687. * but any argument that is passed in will silence the warning.
  19688. *
  19689. * @param {*} [safety]
  19690. * Anything passed in to silence the warning
  19691. *
  19692. * @return {Tech}
  19693. * The Tech
  19694. */
  19695. tech(safety) {
  19696. if (safety === undefined) {
  19697. log.warn('Using the tech directly can be dangerous. I hope you know what you\'re doing.\n' + 'See https://github.com/videojs/video.js/issues/2617 for more info.\n');
  19698. }
  19699. return this.tech_;
  19700. }
  19701. /**
  19702. * An object that contains Video.js version.
  19703. *
  19704. * @typedef {Object} PlayerVersion
  19705. *
  19706. * @property {string} 'video.js' - Video.js version
  19707. */
  19708. /**
  19709. * Returns an object with Video.js version.
  19710. *
  19711. * @return {PlayerVersion}
  19712. * An object with Video.js version.
  19713. */
  19714. version() {
  19715. return {
  19716. 'video.js': version
  19717. };
  19718. }
  19719. /**
  19720. * Set up click and touch listeners for the playback element
  19721. *
  19722. * - On desktops: a click on the video itself will toggle playback
  19723. * - On mobile devices: a click on the video toggles controls
  19724. * which is done by toggling the user state between active and
  19725. * inactive
  19726. * - A tap can signal that a user has become active or has become inactive
  19727. * e.g. a quick tap on an iPhone movie should reveal the controls. Another
  19728. * quick tap should hide them again (signaling the user is in an inactive
  19729. * viewing state)
  19730. * - In addition to this, we still want the user to be considered inactive after
  19731. * a few seconds of inactivity.
  19732. *
  19733. * > Note: the only part of iOS interaction we can't mimic with this setup
  19734. * is a touch and hold on the video element counting as activity in order to
  19735. * keep the controls showing, but that shouldn't be an issue. A touch and hold
  19736. * on any controls will still keep the user active
  19737. *
  19738. * @private
  19739. */
  19740. addTechControlsListeners_() {
  19741. // Make sure to remove all the previous listeners in case we are called multiple times.
  19742. this.removeTechControlsListeners_();
  19743. this.on(this.tech_, 'click', this.boundHandleTechClick_);
  19744. this.on(this.tech_, 'dblclick', this.boundHandleTechDoubleClick_);
  19745. // If the controls were hidden we don't want that to change without a tap event
  19746. // so we'll check if the controls were already showing before reporting user
  19747. // activity
  19748. this.on(this.tech_, 'touchstart', this.boundHandleTechTouchStart_);
  19749. this.on(this.tech_, 'touchmove', this.boundHandleTechTouchMove_);
  19750. this.on(this.tech_, 'touchend', this.boundHandleTechTouchEnd_);
  19751. // The tap listener needs to come after the touchend listener because the tap
  19752. // listener cancels out any reportedUserActivity when setting userActive(false)
  19753. this.on(this.tech_, 'tap', this.boundHandleTechTap_);
  19754. }
  19755. /**
  19756. * Remove the listeners used for click and tap controls. This is needed for
  19757. * toggling to controls disabled, where a tap/touch should do nothing.
  19758. *
  19759. * @private
  19760. */
  19761. removeTechControlsListeners_() {
  19762. // We don't want to just use `this.off()` because there might be other needed
  19763. // listeners added by techs that extend this.
  19764. this.off(this.tech_, 'tap', this.boundHandleTechTap_);
  19765. this.off(this.tech_, 'touchstart', this.boundHandleTechTouchStart_);
  19766. this.off(this.tech_, 'touchmove', this.boundHandleTechTouchMove_);
  19767. this.off(this.tech_, 'touchend', this.boundHandleTechTouchEnd_);
  19768. this.off(this.tech_, 'click', this.boundHandleTechClick_);
  19769. this.off(this.tech_, 'dblclick', this.boundHandleTechDoubleClick_);
  19770. }
  19771. /**
  19772. * Player waits for the tech to be ready
  19773. *
  19774. * @private
  19775. */
  19776. handleTechReady_() {
  19777. this.triggerReady();
  19778. // Keep the same volume as before
  19779. if (this.cache_.volume) {
  19780. this.techCall_('setVolume', this.cache_.volume);
  19781. }
  19782. // Look if the tech found a higher resolution poster while loading
  19783. this.handleTechPosterChange_();
  19784. // Update the duration if available
  19785. this.handleTechDurationChange_();
  19786. }
  19787. /**
  19788. * Retrigger the `loadstart` event that was triggered by the {@link Tech}.
  19789. *
  19790. * @fires Player#loadstart
  19791. * @listens Tech#loadstart
  19792. * @private
  19793. */
  19794. handleTechLoadStart_() {
  19795. // TODO: Update to use `emptied` event instead. See #1277.
  19796. this.removeClass('vjs-ended', 'vjs-seeking');
  19797. // reset the error state
  19798. this.error(null);
  19799. // Update the duration
  19800. this.handleTechDurationChange_();
  19801. if (!this.paused()) {
  19802. /**
  19803. * Fired when the user agent begins looking for media data
  19804. *
  19805. * @event Player#loadstart
  19806. * @type {Event}
  19807. */
  19808. this.trigger('loadstart');
  19809. } else {
  19810. // reset the hasStarted state
  19811. this.hasStarted(false);
  19812. this.trigger('loadstart');
  19813. }
  19814. // autoplay happens after loadstart for the browser,
  19815. // so we mimic that behavior
  19816. this.manualAutoplay_(this.autoplay() === true && this.options_.normalizeAutoplay ? 'play' : this.autoplay());
  19817. }
  19818. /**
  19819. * Handle autoplay string values, rather than the typical boolean
  19820. * values that should be handled by the tech. Note that this is not
  19821. * part of any specification. Valid values and what they do can be
  19822. * found on the autoplay getter at Player#autoplay()
  19823. */
  19824. manualAutoplay_(type) {
  19825. if (!this.tech_ || typeof type !== 'string') {
  19826. return;
  19827. }
  19828. // Save original muted() value, set muted to true, and attempt to play().
  19829. // On promise rejection, restore muted from saved value
  19830. const resolveMuted = () => {
  19831. const previouslyMuted = this.muted();
  19832. this.muted(true);
  19833. const restoreMuted = () => {
  19834. this.muted(previouslyMuted);
  19835. };
  19836. // restore muted on play terminatation
  19837. this.playTerminatedQueue_.push(restoreMuted);
  19838. const mutedPromise = this.play();
  19839. if (!isPromise(mutedPromise)) {
  19840. return;
  19841. }
  19842. return mutedPromise.catch(err => {
  19843. restoreMuted();
  19844. throw new Error(`Rejection at manualAutoplay. Restoring muted value. ${err ? err : ''}`);
  19845. });
  19846. };
  19847. let promise;
  19848. // if muted defaults to true
  19849. // the only thing we can do is call play
  19850. if (type === 'any' && !this.muted()) {
  19851. promise = this.play();
  19852. if (isPromise(promise)) {
  19853. promise = promise.catch(resolveMuted);
  19854. }
  19855. } else if (type === 'muted' && !this.muted()) {
  19856. promise = resolveMuted();
  19857. } else {
  19858. promise = this.play();
  19859. }
  19860. if (!isPromise(promise)) {
  19861. return;
  19862. }
  19863. return promise.then(() => {
  19864. this.trigger({
  19865. type: 'autoplay-success',
  19866. autoplay: type
  19867. });
  19868. }).catch(() => {
  19869. this.trigger({
  19870. type: 'autoplay-failure',
  19871. autoplay: type
  19872. });
  19873. });
  19874. }
  19875. /**
  19876. * Update the internal source caches so that we return the correct source from
  19877. * `src()`, `currentSource()`, and `currentSources()`.
  19878. *
  19879. * > Note: `currentSources` will not be updated if the source that is passed in exists
  19880. * in the current `currentSources` cache.
  19881. *
  19882. *
  19883. * @param {Tech~SourceObject} srcObj
  19884. * A string or object source to update our caches to.
  19885. */
  19886. updateSourceCaches_(srcObj = '') {
  19887. let src = srcObj;
  19888. let type = '';
  19889. if (typeof src !== 'string') {
  19890. src = srcObj.src;
  19891. type = srcObj.type;
  19892. }
  19893. // make sure all the caches are set to default values
  19894. // to prevent null checking
  19895. this.cache_.source = this.cache_.source || {};
  19896. this.cache_.sources = this.cache_.sources || [];
  19897. // try to get the type of the src that was passed in
  19898. if (src && !type) {
  19899. type = findMimetype(this, src);
  19900. }
  19901. // update `currentSource` cache always
  19902. this.cache_.source = merge({}, srcObj, {
  19903. src,
  19904. type
  19905. });
  19906. const matchingSources = this.cache_.sources.filter(s => s.src && s.src === src);
  19907. const sourceElSources = [];
  19908. const sourceEls = this.$$('source');
  19909. const matchingSourceEls = [];
  19910. for (let i = 0; i < sourceEls.length; i++) {
  19911. const sourceObj = getAttributes(sourceEls[i]);
  19912. sourceElSources.push(sourceObj);
  19913. if (sourceObj.src && sourceObj.src === src) {
  19914. matchingSourceEls.push(sourceObj.src);
  19915. }
  19916. }
  19917. // if we have matching source els but not matching sources
  19918. // the current source cache is not up to date
  19919. if (matchingSourceEls.length && !matchingSources.length) {
  19920. this.cache_.sources = sourceElSources;
  19921. // if we don't have matching source or source els set the
  19922. // sources cache to the `currentSource` cache
  19923. } else if (!matchingSources.length) {
  19924. this.cache_.sources = [this.cache_.source];
  19925. }
  19926. // update the tech `src` cache
  19927. this.cache_.src = src;
  19928. }
  19929. /**
  19930. * *EXPERIMENTAL* Fired when the source is set or changed on the {@link Tech}
  19931. * causing the media element to reload.
  19932. *
  19933. * It will fire for the initial source and each subsequent source.
  19934. * This event is a custom event from Video.js and is triggered by the {@link Tech}.
  19935. *
  19936. * The event object for this event contains a `src` property that will contain the source
  19937. * that was available when the event was triggered. This is generally only necessary if Video.js
  19938. * is switching techs while the source was being changed.
  19939. *
  19940. * It is also fired when `load` is called on the player (or media element)
  19941. * because the {@link https://html.spec.whatwg.org/multipage/media.html#dom-media-load|specification for `load`}
  19942. * says that the resource selection algorithm needs to be aborted and restarted.
  19943. * In this case, it is very likely that the `src` property will be set to the
  19944. * empty string `""` to indicate we do not know what the source will be but
  19945. * that it is changing.
  19946. *
  19947. * *This event is currently still experimental and may change in minor releases.*
  19948. * __To use this, pass `enableSourceset` option to the player.__
  19949. *
  19950. * @event Player#sourceset
  19951. * @type {Event}
  19952. * @prop {string} src
  19953. * The source url available when the `sourceset` was triggered.
  19954. * It will be an empty string if we cannot know what the source is
  19955. * but know that the source will change.
  19956. */
  19957. /**
  19958. * Retrigger the `sourceset` event that was triggered by the {@link Tech}.
  19959. *
  19960. * @fires Player#sourceset
  19961. * @listens Tech#sourceset
  19962. * @private
  19963. */
  19964. handleTechSourceset_(event) {
  19965. // only update the source cache when the source
  19966. // was not updated using the player api
  19967. if (!this.changingSrc_) {
  19968. let updateSourceCaches = src => this.updateSourceCaches_(src);
  19969. const playerSrc = this.currentSource().src;
  19970. const eventSrc = event.src;
  19971. // if we have a playerSrc that is not a blob, and a tech src that is a blob
  19972. if (playerSrc && !/^blob:/.test(playerSrc) && /^blob:/.test(eventSrc)) {
  19973. // if both the tech source and the player source were updated we assume
  19974. // something like @videojs/http-streaming did the sourceset and skip updating the source cache.
  19975. if (!this.lastSource_ || this.lastSource_.tech !== eventSrc && this.lastSource_.player !== playerSrc) {
  19976. updateSourceCaches = () => {};
  19977. }
  19978. }
  19979. // update the source to the initial source right away
  19980. // in some cases this will be empty string
  19981. updateSourceCaches(eventSrc);
  19982. // if the `sourceset` `src` was an empty string
  19983. // wait for a `loadstart` to update the cache to `currentSrc`.
  19984. // If a sourceset happens before a `loadstart`, we reset the state
  19985. if (!event.src) {
  19986. this.tech_.any(['sourceset', 'loadstart'], e => {
  19987. // if a sourceset happens before a `loadstart` there
  19988. // is nothing to do as this `handleTechSourceset_`
  19989. // will be called again and this will be handled there.
  19990. if (e.type === 'sourceset') {
  19991. return;
  19992. }
  19993. const techSrc = this.techGet_('currentSrc');
  19994. this.lastSource_.tech = techSrc;
  19995. this.updateSourceCaches_(techSrc);
  19996. });
  19997. }
  19998. }
  19999. this.lastSource_ = {
  20000. player: this.currentSource().src,
  20001. tech: event.src
  20002. };
  20003. this.trigger({
  20004. src: event.src,
  20005. type: 'sourceset'
  20006. });
  20007. }
  20008. /**
  20009. * Add/remove the vjs-has-started class
  20010. *
  20011. *
  20012. * @param {boolean} request
  20013. * - true: adds the class
  20014. * - false: remove the class
  20015. *
  20016. * @return {boolean}
  20017. * the boolean value of hasStarted_
  20018. */
  20019. hasStarted(request) {
  20020. if (request === undefined) {
  20021. // act as getter, if we have no request to change
  20022. return this.hasStarted_;
  20023. }
  20024. if (request === this.hasStarted_) {
  20025. return;
  20026. }
  20027. this.hasStarted_ = request;
  20028. if (this.hasStarted_) {
  20029. this.addClass('vjs-has-started');
  20030. } else {
  20031. this.removeClass('vjs-has-started');
  20032. }
  20033. }
  20034. /**
  20035. * Fired whenever the media begins or resumes playback
  20036. *
  20037. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-play}
  20038. * @fires Player#play
  20039. * @listens Tech#play
  20040. * @private
  20041. */
  20042. handleTechPlay_() {
  20043. this.removeClass('vjs-ended', 'vjs-paused');
  20044. this.addClass('vjs-playing');
  20045. // hide the poster when the user hits play
  20046. this.hasStarted(true);
  20047. /**
  20048. * Triggered whenever an {@link Tech#play} event happens. Indicates that
  20049. * playback has started or resumed.
  20050. *
  20051. * @event Player#play
  20052. * @type {Event}
  20053. */
  20054. this.trigger('play');
  20055. }
  20056. /**
  20057. * Retrigger the `ratechange` event that was triggered by the {@link Tech}.
  20058. *
  20059. * If there were any events queued while the playback rate was zero, fire
  20060. * those events now.
  20061. *
  20062. * @private
  20063. * @method Player#handleTechRateChange_
  20064. * @fires Player#ratechange
  20065. * @listens Tech#ratechange
  20066. */
  20067. handleTechRateChange_() {
  20068. if (this.tech_.playbackRate() > 0 && this.cache_.lastPlaybackRate === 0) {
  20069. this.queuedCallbacks_.forEach(queued => queued.callback(queued.event));
  20070. this.queuedCallbacks_ = [];
  20071. }
  20072. this.cache_.lastPlaybackRate = this.tech_.playbackRate();
  20073. /**
  20074. * Fires when the playing speed of the audio/video is changed
  20075. *
  20076. * @event Player#ratechange
  20077. * @type {event}
  20078. */
  20079. this.trigger('ratechange');
  20080. }
  20081. /**
  20082. * Retrigger the `waiting` event that was triggered by the {@link Tech}.
  20083. *
  20084. * @fires Player#waiting
  20085. * @listens Tech#waiting
  20086. * @private
  20087. */
  20088. handleTechWaiting_() {
  20089. this.addClass('vjs-waiting');
  20090. /**
  20091. * A readyState change on the DOM element has caused playback to stop.
  20092. *
  20093. * @event Player#waiting
  20094. * @type {Event}
  20095. */
  20096. this.trigger('waiting');
  20097. // Browsers may emit a timeupdate event after a waiting event. In order to prevent
  20098. // premature removal of the waiting class, wait for the time to change.
  20099. const timeWhenWaiting = this.currentTime();
  20100. const timeUpdateListener = () => {
  20101. if (timeWhenWaiting !== this.currentTime()) {
  20102. this.removeClass('vjs-waiting');
  20103. this.off('timeupdate', timeUpdateListener);
  20104. }
  20105. };
  20106. this.on('timeupdate', timeUpdateListener);
  20107. }
  20108. /**
  20109. * Retrigger the `canplay` event that was triggered by the {@link Tech}.
  20110. * > Note: This is not consistent between browsers. See #1351
  20111. *
  20112. * @fires Player#canplay
  20113. * @listens Tech#canplay
  20114. * @private
  20115. */
  20116. handleTechCanPlay_() {
  20117. this.removeClass('vjs-waiting');
  20118. /**
  20119. * The media has a readyState of HAVE_FUTURE_DATA or greater.
  20120. *
  20121. * @event Player#canplay
  20122. * @type {Event}
  20123. */
  20124. this.trigger('canplay');
  20125. }
  20126. /**
  20127. * Retrigger the `canplaythrough` event that was triggered by the {@link Tech}.
  20128. *
  20129. * @fires Player#canplaythrough
  20130. * @listens Tech#canplaythrough
  20131. * @private
  20132. */
  20133. handleTechCanPlayThrough_() {
  20134. this.removeClass('vjs-waiting');
  20135. /**
  20136. * The media has a readyState of HAVE_ENOUGH_DATA or greater. This means that the
  20137. * entire media file can be played without buffering.
  20138. *
  20139. * @event Player#canplaythrough
  20140. * @type {Event}
  20141. */
  20142. this.trigger('canplaythrough');
  20143. }
  20144. /**
  20145. * Retrigger the `playing` event that was triggered by the {@link Tech}.
  20146. *
  20147. * @fires Player#playing
  20148. * @listens Tech#playing
  20149. * @private
  20150. */
  20151. handleTechPlaying_() {
  20152. this.removeClass('vjs-waiting');
  20153. /**
  20154. * The media is no longer blocked from playback, and has started playing.
  20155. *
  20156. * @event Player#playing
  20157. * @type {Event}
  20158. */
  20159. this.trigger('playing');
  20160. }
  20161. /**
  20162. * Retrigger the `seeking` event that was triggered by the {@link Tech}.
  20163. *
  20164. * @fires Player#seeking
  20165. * @listens Tech#seeking
  20166. * @private
  20167. */
  20168. handleTechSeeking_() {
  20169. this.addClass('vjs-seeking');
  20170. /**
  20171. * Fired whenever the player is jumping to a new time
  20172. *
  20173. * @event Player#seeking
  20174. * @type {Event}
  20175. */
  20176. this.trigger('seeking');
  20177. }
  20178. /**
  20179. * Retrigger the `seeked` event that was triggered by the {@link Tech}.
  20180. *
  20181. * @fires Player#seeked
  20182. * @listens Tech#seeked
  20183. * @private
  20184. */
  20185. handleTechSeeked_() {
  20186. this.removeClass('vjs-seeking', 'vjs-ended');
  20187. /**
  20188. * Fired when the player has finished jumping to a new time
  20189. *
  20190. * @event Player#seeked
  20191. * @type {Event}
  20192. */
  20193. this.trigger('seeked');
  20194. }
  20195. /**
  20196. * Retrigger the `pause` event that was triggered by the {@link Tech}.
  20197. *
  20198. * @fires Player#pause
  20199. * @listens Tech#pause
  20200. * @private
  20201. */
  20202. handleTechPause_() {
  20203. this.removeClass('vjs-playing');
  20204. this.addClass('vjs-paused');
  20205. /**
  20206. * Fired whenever the media has been paused
  20207. *
  20208. * @event Player#pause
  20209. * @type {Event}
  20210. */
  20211. this.trigger('pause');
  20212. }
  20213. /**
  20214. * Retrigger the `ended` event that was triggered by the {@link Tech}.
  20215. *
  20216. * @fires Player#ended
  20217. * @listens Tech#ended
  20218. * @private
  20219. */
  20220. handleTechEnded_() {
  20221. this.addClass('vjs-ended');
  20222. this.removeClass('vjs-waiting');
  20223. if (this.options_.loop) {
  20224. this.currentTime(0);
  20225. this.play();
  20226. } else if (!this.paused()) {
  20227. this.pause();
  20228. }
  20229. /**
  20230. * Fired when the end of the media resource is reached (currentTime == duration)
  20231. *
  20232. * @event Player#ended
  20233. * @type {Event}
  20234. */
  20235. this.trigger('ended');
  20236. }
  20237. /**
  20238. * Fired when the duration of the media resource is first known or changed
  20239. *
  20240. * @listens Tech#durationchange
  20241. * @private
  20242. */
  20243. handleTechDurationChange_() {
  20244. this.duration(this.techGet_('duration'));
  20245. }
  20246. /**
  20247. * Handle a click on the media element to play/pause
  20248. *
  20249. * @param {Event} event
  20250. * the event that caused this function to trigger
  20251. *
  20252. * @listens Tech#click
  20253. * @private
  20254. */
  20255. handleTechClick_(event) {
  20256. // When controls are disabled a click should not toggle playback because
  20257. // the click is considered a control
  20258. if (!this.controls_) {
  20259. return;
  20260. }
  20261. if (this.options_ === undefined || this.options_.userActions === undefined || this.options_.userActions.click === undefined || this.options_.userActions.click !== false) {
  20262. if (this.options_ !== undefined && this.options_.userActions !== undefined && typeof this.options_.userActions.click === 'function') {
  20263. this.options_.userActions.click.call(this, event);
  20264. } else if (this.paused()) {
  20265. silencePromise(this.play());
  20266. } else {
  20267. this.pause();
  20268. }
  20269. }
  20270. }
  20271. /**
  20272. * Handle a double-click on the media element to enter/exit fullscreen
  20273. *
  20274. * @param {Event} event
  20275. * the event that caused this function to trigger
  20276. *
  20277. * @listens Tech#dblclick
  20278. * @private
  20279. */
  20280. handleTechDoubleClick_(event) {
  20281. if (!this.controls_) {
  20282. return;
  20283. }
  20284. // we do not want to toggle fullscreen state
  20285. // when double-clicking inside a control bar or a modal
  20286. const inAllowedEls = Array.prototype.some.call(this.$$('.vjs-control-bar, .vjs-modal-dialog'), el => el.contains(event.target));
  20287. if (!inAllowedEls) {
  20288. /*
  20289. * options.userActions.doubleClick
  20290. *
  20291. * If `undefined` or `true`, double-click toggles fullscreen if controls are present
  20292. * Set to `false` to disable double-click handling
  20293. * Set to a function to substitute an external double-click handler
  20294. */
  20295. if (this.options_ === undefined || this.options_.userActions === undefined || this.options_.userActions.doubleClick === undefined || this.options_.userActions.doubleClick !== false) {
  20296. if (this.options_ !== undefined && this.options_.userActions !== undefined && typeof this.options_.userActions.doubleClick === 'function') {
  20297. this.options_.userActions.doubleClick.call(this, event);
  20298. } else if (this.isFullscreen()) {
  20299. this.exitFullscreen();
  20300. } else {
  20301. this.requestFullscreen();
  20302. }
  20303. }
  20304. }
  20305. }
  20306. /**
  20307. * Handle a tap on the media element. It will toggle the user
  20308. * activity state, which hides and shows the controls.
  20309. *
  20310. * @listens Tech#tap
  20311. * @private
  20312. */
  20313. handleTechTap_() {
  20314. this.userActive(!this.userActive());
  20315. }
  20316. /**
  20317. * Handle touch to start
  20318. *
  20319. * @listens Tech#touchstart
  20320. * @private
  20321. */
  20322. handleTechTouchStart_() {
  20323. this.userWasActive = this.userActive();
  20324. }
  20325. /**
  20326. * Handle touch to move
  20327. *
  20328. * @listens Tech#touchmove
  20329. * @private
  20330. */
  20331. handleTechTouchMove_() {
  20332. if (this.userWasActive) {
  20333. this.reportUserActivity();
  20334. }
  20335. }
  20336. /**
  20337. * Handle touch to end
  20338. *
  20339. * @param {Event} event
  20340. * the touchend event that triggered
  20341. * this function
  20342. *
  20343. * @listens Tech#touchend
  20344. * @private
  20345. */
  20346. handleTechTouchEnd_(event) {
  20347. // Stop the mouse events from also happening
  20348. if (event.cancelable) {
  20349. event.preventDefault();
  20350. }
  20351. }
  20352. /**
  20353. * @private
  20354. */
  20355. toggleFullscreenClass_() {
  20356. if (this.isFullscreen()) {
  20357. this.addClass('vjs-fullscreen');
  20358. } else {
  20359. this.removeClass('vjs-fullscreen');
  20360. }
  20361. }
  20362. /**
  20363. * when the document fschange event triggers it calls this
  20364. */
  20365. documentFullscreenChange_(e) {
  20366. const targetPlayer = e.target.player;
  20367. // if another player was fullscreen
  20368. // do a null check for targetPlayer because older firefox's would put document as e.target
  20369. if (targetPlayer && targetPlayer !== this) {
  20370. return;
  20371. }
  20372. const el = this.el();
  20373. let isFs = document[this.fsApi_.fullscreenElement] === el;
  20374. if (!isFs && el.matches) {
  20375. isFs = el.matches(':' + this.fsApi_.fullscreen);
  20376. }
  20377. this.isFullscreen(isFs);
  20378. }
  20379. /**
  20380. * Handle Tech Fullscreen Change
  20381. *
  20382. * @param {Event} event
  20383. * the fullscreenchange event that triggered this function
  20384. *
  20385. * @param {Object} data
  20386. * the data that was sent with the event
  20387. *
  20388. * @private
  20389. * @listens Tech#fullscreenchange
  20390. * @fires Player#fullscreenchange
  20391. */
  20392. handleTechFullscreenChange_(event, data) {
  20393. if (data) {
  20394. if (data.nativeIOSFullscreen) {
  20395. this.addClass('vjs-ios-native-fs');
  20396. this.tech_.one('webkitendfullscreen', () => {
  20397. this.removeClass('vjs-ios-native-fs');
  20398. });
  20399. }
  20400. this.isFullscreen(data.isFullscreen);
  20401. }
  20402. }
  20403. handleTechFullscreenError_(event, err) {
  20404. this.trigger('fullscreenerror', err);
  20405. }
  20406. /**
  20407. * @private
  20408. */
  20409. togglePictureInPictureClass_() {
  20410. if (this.isInPictureInPicture()) {
  20411. this.addClass('vjs-picture-in-picture');
  20412. } else {
  20413. this.removeClass('vjs-picture-in-picture');
  20414. }
  20415. }
  20416. /**
  20417. * Handle Tech Enter Picture-in-Picture.
  20418. *
  20419. * @param {Event} event
  20420. * the enterpictureinpicture event that triggered this function
  20421. *
  20422. * @private
  20423. * @listens Tech#enterpictureinpicture
  20424. */
  20425. handleTechEnterPictureInPicture_(event) {
  20426. this.isInPictureInPicture(true);
  20427. }
  20428. /**
  20429. * Handle Tech Leave Picture-in-Picture.
  20430. *
  20431. * @param {Event} event
  20432. * the leavepictureinpicture event that triggered this function
  20433. *
  20434. * @private
  20435. * @listens Tech#leavepictureinpicture
  20436. */
  20437. handleTechLeavePictureInPicture_(event) {
  20438. this.isInPictureInPicture(false);
  20439. }
  20440. /**
  20441. * Fires when an error occurred during the loading of an audio/video.
  20442. *
  20443. * @private
  20444. * @listens Tech#error
  20445. */
  20446. handleTechError_() {
  20447. const error = this.tech_.error();
  20448. if (error) {
  20449. this.error(error);
  20450. }
  20451. }
  20452. /**
  20453. * Retrigger the `textdata` event that was triggered by the {@link Tech}.
  20454. *
  20455. * @fires Player#textdata
  20456. * @listens Tech#textdata
  20457. * @private
  20458. */
  20459. handleTechTextData_() {
  20460. let data = null;
  20461. if (arguments.length > 1) {
  20462. data = arguments[1];
  20463. }
  20464. /**
  20465. * Fires when we get a textdata event from tech
  20466. *
  20467. * @event Player#textdata
  20468. * @type {Event}
  20469. */
  20470. this.trigger('textdata', data);
  20471. }
  20472. /**
  20473. * Get object for cached values.
  20474. *
  20475. * @return {Object}
  20476. * get the current object cache
  20477. */
  20478. getCache() {
  20479. return this.cache_;
  20480. }
  20481. /**
  20482. * Resets the internal cache object.
  20483. *
  20484. * Using this function outside the player constructor or reset method may
  20485. * have unintended side-effects.
  20486. *
  20487. * @private
  20488. */
  20489. resetCache_() {
  20490. this.cache_ = {
  20491. // Right now, the currentTime is not _really_ cached because it is always
  20492. // retrieved from the tech (see: currentTime). However, for completeness,
  20493. // we set it to zero here to ensure that if we do start actually caching
  20494. // it, we reset it along with everything else.
  20495. currentTime: 0,
  20496. initTime: 0,
  20497. inactivityTimeout: this.options_.inactivityTimeout,
  20498. duration: NaN,
  20499. lastVolume: 1,
  20500. lastPlaybackRate: this.defaultPlaybackRate(),
  20501. media: null,
  20502. src: '',
  20503. source: {},
  20504. sources: [],
  20505. playbackRates: [],
  20506. volume: 1
  20507. };
  20508. }
  20509. /**
  20510. * Pass values to the playback tech
  20511. *
  20512. * @param {string} [method]
  20513. * the method to call
  20514. *
  20515. * @param {Object} [arg]
  20516. * the argument to pass
  20517. *
  20518. * @private
  20519. */
  20520. techCall_(method, arg) {
  20521. // If it's not ready yet, call method when it is
  20522. this.ready(function () {
  20523. if (method in allowedSetters) {
  20524. return set(this.middleware_, this.tech_, method, arg);
  20525. } else if (method in allowedMediators) {
  20526. return mediate(this.middleware_, this.tech_, method, arg);
  20527. }
  20528. try {
  20529. if (this.tech_) {
  20530. this.tech_[method](arg);
  20531. }
  20532. } catch (e) {
  20533. log(e);
  20534. throw e;
  20535. }
  20536. }, true);
  20537. }
  20538. /**
  20539. * Mediate attempt to call playback tech method
  20540. * and return the value of the method called.
  20541. *
  20542. * @param {string} method
  20543. * Tech method
  20544. *
  20545. * @return {*}
  20546. * Value returned by the tech method called, undefined if tech
  20547. * is not ready or tech method is not present
  20548. *
  20549. * @private
  20550. */
  20551. techGet_(method) {
  20552. if (!this.tech_ || !this.tech_.isReady_) {
  20553. return;
  20554. }
  20555. if (method in allowedGetters) {
  20556. return get(this.middleware_, this.tech_, method);
  20557. } else if (method in allowedMediators) {
  20558. return mediate(this.middleware_, this.tech_, method);
  20559. }
  20560. // Log error when playback tech object is present but method
  20561. // is undefined or unavailable
  20562. try {
  20563. return this.tech_[method]();
  20564. } catch (e) {
  20565. // When building additional tech libs, an expected method may not be defined yet
  20566. if (this.tech_[method] === undefined) {
  20567. log(`Video.js: ${method} method not defined for ${this.techName_} playback technology.`, e);
  20568. throw e;
  20569. }
  20570. // When a method isn't available on the object it throws a TypeError
  20571. if (e.name === 'TypeError') {
  20572. log(`Video.js: ${method} unavailable on ${this.techName_} playback technology element.`, e);
  20573. this.tech_.isReady_ = false;
  20574. throw e;
  20575. }
  20576. // If error unknown, just log and throw
  20577. log(e);
  20578. throw e;
  20579. }
  20580. }
  20581. /**
  20582. * Attempt to begin playback at the first opportunity.
  20583. *
  20584. * @return {Promise|undefined}
  20585. * Returns a promise if the browser supports Promises (or one
  20586. * was passed in as an option). This promise will be resolved on
  20587. * the return value of play. If this is undefined it will fulfill the
  20588. * promise chain otherwise the promise chain will be fulfilled when
  20589. * the promise from play is fulfilled.
  20590. */
  20591. play() {
  20592. return new Promise(resolve => {
  20593. this.play_(resolve);
  20594. });
  20595. }
  20596. /**
  20597. * The actual logic for play, takes a callback that will be resolved on the
  20598. * return value of play. This allows us to resolve to the play promise if there
  20599. * is one on modern browsers.
  20600. *
  20601. * @private
  20602. * @param {Function} [callback]
  20603. * The callback that should be called when the techs play is actually called
  20604. */
  20605. play_(callback = silencePromise) {
  20606. this.playCallbacks_.push(callback);
  20607. const isSrcReady = Boolean(!this.changingSrc_ && (this.src() || this.currentSrc()));
  20608. const isSafariOrIOS = Boolean(IS_ANY_SAFARI || IS_IOS);
  20609. // treat calls to play_ somewhat like the `one` event function
  20610. if (this.waitToPlay_) {
  20611. this.off(['ready', 'loadstart'], this.waitToPlay_);
  20612. this.waitToPlay_ = null;
  20613. }
  20614. // if the player/tech is not ready or the src itself is not ready
  20615. // queue up a call to play on `ready` or `loadstart`
  20616. if (!this.isReady_ || !isSrcReady) {
  20617. this.waitToPlay_ = e => {
  20618. this.play_();
  20619. };
  20620. this.one(['ready', 'loadstart'], this.waitToPlay_);
  20621. // if we are in Safari, there is a high chance that loadstart will trigger after the gesture timeperiod
  20622. // in that case, we need to prime the video element by calling load so it'll be ready in time
  20623. if (!isSrcReady && isSafariOrIOS) {
  20624. this.load();
  20625. }
  20626. return;
  20627. }
  20628. // If the player/tech is ready and we have a source, we can attempt playback.
  20629. const val = this.techGet_('play');
  20630. // For native playback, reset the progress bar if we get a play call from a replay.
  20631. const isNativeReplay = isSafariOrIOS && this.hasClass('vjs-ended');
  20632. if (isNativeReplay) {
  20633. this.resetProgressBar_();
  20634. }
  20635. // play was terminated if the returned value is null
  20636. if (val === null) {
  20637. this.runPlayTerminatedQueue_();
  20638. } else {
  20639. this.runPlayCallbacks_(val);
  20640. }
  20641. }
  20642. /**
  20643. * These functions will be run when if play is terminated. If play
  20644. * runPlayCallbacks_ is run these function will not be run. This allows us
  20645. * to differentiate between a terminated play and an actual call to play.
  20646. */
  20647. runPlayTerminatedQueue_() {
  20648. const queue = this.playTerminatedQueue_.slice(0);
  20649. this.playTerminatedQueue_ = [];
  20650. queue.forEach(function (q) {
  20651. q();
  20652. });
  20653. }
  20654. /**
  20655. * When a callback to play is delayed we have to run these
  20656. * callbacks when play is actually called on the tech. This function
  20657. * runs the callbacks that were delayed and accepts the return value
  20658. * from the tech.
  20659. *
  20660. * @param {undefined|Promise} val
  20661. * The return value from the tech.
  20662. */
  20663. runPlayCallbacks_(val) {
  20664. const callbacks = this.playCallbacks_.slice(0);
  20665. this.playCallbacks_ = [];
  20666. // clear play terminatedQueue since we finished a real play
  20667. this.playTerminatedQueue_ = [];
  20668. callbacks.forEach(function (cb) {
  20669. cb(val);
  20670. });
  20671. }
  20672. /**
  20673. * Pause the video playback
  20674. */
  20675. pause() {
  20676. this.techCall_('pause');
  20677. }
  20678. /**
  20679. * Check if the player is paused or has yet to play
  20680. *
  20681. * @return {boolean}
  20682. * - false: if the media is currently playing
  20683. * - true: if media is not currently playing
  20684. */
  20685. paused() {
  20686. // The initial state of paused should be true (in Safari it's actually false)
  20687. return this.techGet_('paused') === false ? false : true;
  20688. }
  20689. /**
  20690. * Get a TimeRange object representing the current ranges of time that the user
  20691. * has played.
  20692. *
  20693. * @return { import('./utils/time').TimeRange }
  20694. * A time range object that represents all the increments of time that have
  20695. * been played.
  20696. */
  20697. played() {
  20698. return this.techGet_('played') || createTimeRanges(0, 0);
  20699. }
  20700. /**
  20701. * Sets or returns whether or not the user is "scrubbing". Scrubbing is
  20702. * when the user has clicked the progress bar handle and is
  20703. * dragging it along the progress bar.
  20704. *
  20705. * @param {boolean} [isScrubbing]
  20706. * whether the user is or is not scrubbing
  20707. *
  20708. * @return {boolean|undefined}
  20709. * - The value of scrubbing when getting
  20710. * - Nothing when setting
  20711. */
  20712. scrubbing(isScrubbing) {
  20713. if (typeof isScrubbing === 'undefined') {
  20714. return this.scrubbing_;
  20715. }
  20716. this.scrubbing_ = !!isScrubbing;
  20717. this.techCall_('setScrubbing', this.scrubbing_);
  20718. if (isScrubbing) {
  20719. this.addClass('vjs-scrubbing');
  20720. } else {
  20721. this.removeClass('vjs-scrubbing');
  20722. }
  20723. }
  20724. /**
  20725. * Get or set the current time (in seconds)
  20726. *
  20727. * @param {number|string} [seconds]
  20728. * The time to seek to in seconds
  20729. *
  20730. * @return {number|undefined}
  20731. * - the current time in seconds when getting
  20732. * - Nothing when setting
  20733. */
  20734. currentTime(seconds) {
  20735. if (seconds === undefined) {
  20736. // cache last currentTime and return. default to 0 seconds
  20737. //
  20738. // Caching the currentTime is meant to prevent a massive amount of reads on the tech's
  20739. // currentTime when scrubbing, but may not provide much performance benefit after all.
  20740. // Should be tested. Also something has to read the actual current time or the cache will
  20741. // never get updated.
  20742. this.cache_.currentTime = this.techGet_('currentTime') || 0;
  20743. return this.cache_.currentTime;
  20744. }
  20745. if (seconds < 0) {
  20746. seconds = 0;
  20747. }
  20748. if (!this.isReady_ || this.changingSrc_ || !this.tech_ || !this.tech_.isReady_) {
  20749. this.cache_.initTime = seconds;
  20750. this.off('canplay', this.boundApplyInitTime_);
  20751. this.one('canplay', this.boundApplyInitTime_);
  20752. return;
  20753. }
  20754. this.techCall_('setCurrentTime', seconds);
  20755. this.cache_.initTime = 0;
  20756. if (isFinite(seconds)) {
  20757. this.cache_.currentTime = Number(seconds);
  20758. }
  20759. }
  20760. /**
  20761. * Apply the value of initTime stored in cache as currentTime.
  20762. *
  20763. * @private
  20764. */
  20765. applyInitTime_() {
  20766. this.currentTime(this.cache_.initTime);
  20767. }
  20768. /**
  20769. * Normally gets the length in time of the video in seconds;
  20770. * in all but the rarest use cases an argument will NOT be passed to the method
  20771. *
  20772. * > **NOTE**: The video must have started loading before the duration can be
  20773. * known, and depending on preload behaviour may not be known until the video starts
  20774. * playing.
  20775. *
  20776. * @fires Player#durationchange
  20777. *
  20778. * @param {number} [seconds]
  20779. * The duration of the video to set in seconds
  20780. *
  20781. * @return {number|undefined}
  20782. * - The duration of the video in seconds when getting
  20783. * - Nothing when setting
  20784. */
  20785. duration(seconds) {
  20786. if (seconds === undefined) {
  20787. // return NaN if the duration is not known
  20788. return this.cache_.duration !== undefined ? this.cache_.duration : NaN;
  20789. }
  20790. seconds = parseFloat(seconds);
  20791. // Standardize on Infinity for signaling video is live
  20792. if (seconds < 0) {
  20793. seconds = Infinity;
  20794. }
  20795. if (seconds !== this.cache_.duration) {
  20796. // Cache the last set value for optimized scrubbing
  20797. this.cache_.duration = seconds;
  20798. if (seconds === Infinity) {
  20799. this.addClass('vjs-live');
  20800. } else {
  20801. this.removeClass('vjs-live');
  20802. }
  20803. if (!isNaN(seconds)) {
  20804. // Do not fire durationchange unless the duration value is known.
  20805. // @see [Spec]{@link https://www.w3.org/TR/2011/WD-html5-20110113/video.html#media-element-load-algorithm}
  20806. /**
  20807. * @event Player#durationchange
  20808. * @type {Event}
  20809. */
  20810. this.trigger('durationchange');
  20811. }
  20812. }
  20813. }
  20814. /**
  20815. * Calculates how much time is left in the video. Not part
  20816. * of the native video API.
  20817. *
  20818. * @return {number}
  20819. * The time remaining in seconds
  20820. */
  20821. remainingTime() {
  20822. return this.duration() - this.currentTime();
  20823. }
  20824. /**
  20825. * A remaining time function that is intended to be used when
  20826. * the time is to be displayed directly to the user.
  20827. *
  20828. * @return {number}
  20829. * The rounded time remaining in seconds
  20830. */
  20831. remainingTimeDisplay() {
  20832. return Math.floor(this.duration()) - Math.floor(this.currentTime());
  20833. }
  20834. //
  20835. // Kind of like an array of portions of the video that have been downloaded.
  20836. /**
  20837. * Get a TimeRange object with an array of the times of the video
  20838. * that have been downloaded. If you just want the percent of the
  20839. * video that's been downloaded, use bufferedPercent.
  20840. *
  20841. * @see [Buffered Spec]{@link http://dev.w3.org/html5/spec/video.html#dom-media-buffered}
  20842. *
  20843. * @return { import('./utils/time').TimeRange }
  20844. * A mock {@link TimeRanges} object (following HTML spec)
  20845. */
  20846. buffered() {
  20847. let buffered = this.techGet_('buffered');
  20848. if (!buffered || !buffered.length) {
  20849. buffered = createTimeRanges(0, 0);
  20850. }
  20851. return buffered;
  20852. }
  20853. /**
  20854. * Get the TimeRanges of the media that are currently available
  20855. * for seeking to.
  20856. *
  20857. * @see [Seekable Spec]{@link https://html.spec.whatwg.org/multipage/media.html#dom-media-seekable}
  20858. *
  20859. * @return { import('./utils/time').TimeRange }
  20860. * A mock {@link TimeRanges} object (following HTML spec)
  20861. */
  20862. seekable() {
  20863. let seekable = this.techGet_('seekable');
  20864. if (!seekable || !seekable.length) {
  20865. seekable = createTimeRanges(0, 0);
  20866. }
  20867. return seekable;
  20868. }
  20869. /**
  20870. * Returns whether the player is in the "seeking" state.
  20871. *
  20872. * @return {boolean} True if the player is in the seeking state, false if not.
  20873. */
  20874. seeking() {
  20875. return this.techGet_('seeking');
  20876. }
  20877. /**
  20878. * Returns whether the player is in the "ended" state.
  20879. *
  20880. * @return {boolean} True if the player is in the ended state, false if not.
  20881. */
  20882. ended() {
  20883. return this.techGet_('ended');
  20884. }
  20885. /**
  20886. * Returns the current state of network activity for the element, from
  20887. * the codes in the list below.
  20888. * - NETWORK_EMPTY (numeric value 0)
  20889. * The element has not yet been initialised. All attributes are in
  20890. * their initial states.
  20891. * - NETWORK_IDLE (numeric value 1)
  20892. * The element's resource selection algorithm is active and has
  20893. * selected a resource, but it is not actually using the network at
  20894. * this time.
  20895. * - NETWORK_LOADING (numeric value 2)
  20896. * The user agent is actively trying to download data.
  20897. * - NETWORK_NO_SOURCE (numeric value 3)
  20898. * The element's resource selection algorithm is active, but it has
  20899. * not yet found a resource to use.
  20900. *
  20901. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#network-states
  20902. * @return {number} the current network activity state
  20903. */
  20904. networkState() {
  20905. return this.techGet_('networkState');
  20906. }
  20907. /**
  20908. * Returns a value that expresses the current state of the element
  20909. * with respect to rendering the current playback position, from the
  20910. * codes in the list below.
  20911. * - HAVE_NOTHING (numeric value 0)
  20912. * No information regarding the media resource is available.
  20913. * - HAVE_METADATA (numeric value 1)
  20914. * Enough of the resource has been obtained that the duration of the
  20915. * resource is available.
  20916. * - HAVE_CURRENT_DATA (numeric value 2)
  20917. * Data for the immediate current playback position is available.
  20918. * - HAVE_FUTURE_DATA (numeric value 3)
  20919. * Data for the immediate current playback position is available, as
  20920. * well as enough data for the user agent to advance the current
  20921. * playback position in the direction of playback.
  20922. * - HAVE_ENOUGH_DATA (numeric value 4)
  20923. * The user agent estimates that enough data is available for
  20924. * playback to proceed uninterrupted.
  20925. *
  20926. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-readystate
  20927. * @return {number} the current playback rendering state
  20928. */
  20929. readyState() {
  20930. return this.techGet_('readyState');
  20931. }
  20932. /**
  20933. * Get the percent (as a decimal) of the video that's been downloaded.
  20934. * This method is not a part of the native HTML video API.
  20935. *
  20936. * @return {number}
  20937. * A decimal between 0 and 1 representing the percent
  20938. * that is buffered 0 being 0% and 1 being 100%
  20939. */
  20940. bufferedPercent() {
  20941. return bufferedPercent(this.buffered(), this.duration());
  20942. }
  20943. /**
  20944. * Get the ending time of the last buffered time range
  20945. * This is used in the progress bar to encapsulate all time ranges.
  20946. *
  20947. * @return {number}
  20948. * The end of the last buffered time range
  20949. */
  20950. bufferedEnd() {
  20951. const buffered = this.buffered();
  20952. const duration = this.duration();
  20953. let end = buffered.end(buffered.length - 1);
  20954. if (end > duration) {
  20955. end = duration;
  20956. }
  20957. return end;
  20958. }
  20959. /**
  20960. * Get or set the current volume of the media
  20961. *
  20962. * @param {number} [percentAsDecimal]
  20963. * The new volume as a decimal percent:
  20964. * - 0 is muted/0%/off
  20965. * - 1.0 is 100%/full
  20966. * - 0.5 is half volume or 50%
  20967. *
  20968. * @return {number|undefined}
  20969. * The current volume as a percent when getting
  20970. */
  20971. volume(percentAsDecimal) {
  20972. let vol;
  20973. if (percentAsDecimal !== undefined) {
  20974. // Force value to between 0 and 1
  20975. vol = Math.max(0, Math.min(1, percentAsDecimal));
  20976. this.cache_.volume = vol;
  20977. this.techCall_('setVolume', vol);
  20978. if (vol > 0) {
  20979. this.lastVolume_(vol);
  20980. }
  20981. return;
  20982. }
  20983. // Default to 1 when returning current volume.
  20984. vol = parseFloat(this.techGet_('volume'));
  20985. return isNaN(vol) ? 1 : vol;
  20986. }
  20987. /**
  20988. * Get the current muted state, or turn mute on or off
  20989. *
  20990. * @param {boolean} [muted]
  20991. * - true to mute
  20992. * - false to unmute
  20993. *
  20994. * @return {boolean|undefined}
  20995. * - true if mute is on and getting
  20996. * - false if mute is off and getting
  20997. * - nothing if setting
  20998. */
  20999. muted(muted) {
  21000. if (muted !== undefined) {
  21001. this.techCall_('setMuted', muted);
  21002. return;
  21003. }
  21004. return this.techGet_('muted') || false;
  21005. }
  21006. /**
  21007. * Get the current defaultMuted state, or turn defaultMuted on or off. defaultMuted
  21008. * indicates the state of muted on initial playback.
  21009. *
  21010. * ```js
  21011. * var myPlayer = videojs('some-player-id');
  21012. *
  21013. * myPlayer.src("http://www.example.com/path/to/video.mp4");
  21014. *
  21015. * // get, should be false
  21016. * console.log(myPlayer.defaultMuted());
  21017. * // set to true
  21018. * myPlayer.defaultMuted(true);
  21019. * // get should be true
  21020. * console.log(myPlayer.defaultMuted());
  21021. * ```
  21022. *
  21023. * @param {boolean} [defaultMuted]
  21024. * - true to mute
  21025. * - false to unmute
  21026. *
  21027. * @return {boolean|undefined}
  21028. * - true if defaultMuted is on and getting
  21029. * - false if defaultMuted is off and getting
  21030. * - Nothing when setting
  21031. */
  21032. defaultMuted(defaultMuted) {
  21033. if (defaultMuted !== undefined) {
  21034. this.techCall_('setDefaultMuted', defaultMuted);
  21035. }
  21036. return this.techGet_('defaultMuted') || false;
  21037. }
  21038. /**
  21039. * Get the last volume, or set it
  21040. *
  21041. * @param {number} [percentAsDecimal]
  21042. * The new last volume as a decimal percent:
  21043. * - 0 is muted/0%/off
  21044. * - 1.0 is 100%/full
  21045. * - 0.5 is half volume or 50%
  21046. *
  21047. * @return {number|undefined}
  21048. * - The current value of lastVolume as a percent when getting
  21049. * - Nothing when setting
  21050. *
  21051. * @private
  21052. */
  21053. lastVolume_(percentAsDecimal) {
  21054. if (percentAsDecimal !== undefined && percentAsDecimal !== 0) {
  21055. this.cache_.lastVolume = percentAsDecimal;
  21056. return;
  21057. }
  21058. return this.cache_.lastVolume;
  21059. }
  21060. /**
  21061. * Check if current tech can support native fullscreen
  21062. * (e.g. with built in controls like iOS)
  21063. *
  21064. * @return {boolean}
  21065. * if native fullscreen is supported
  21066. */
  21067. supportsFullScreen() {
  21068. return this.techGet_('supportsFullScreen') || false;
  21069. }
  21070. /**
  21071. * Check if the player is in fullscreen mode or tell the player that it
  21072. * is or is not in fullscreen mode.
  21073. *
  21074. * > NOTE: As of the latest HTML5 spec, isFullscreen is no longer an official
  21075. * property and instead document.fullscreenElement is used. But isFullscreen is
  21076. * still a valuable property for internal player workings.
  21077. *
  21078. * @param {boolean} [isFS]
  21079. * Set the players current fullscreen state
  21080. *
  21081. * @return {boolean|undefined}
  21082. * - true if fullscreen is on and getting
  21083. * - false if fullscreen is off and getting
  21084. * - Nothing when setting
  21085. */
  21086. isFullscreen(isFS) {
  21087. if (isFS !== undefined) {
  21088. const oldValue = this.isFullscreen_;
  21089. this.isFullscreen_ = Boolean(isFS);
  21090. // if we changed fullscreen state and we're in prefixed mode, trigger fullscreenchange
  21091. // this is the only place where we trigger fullscreenchange events for older browsers
  21092. // fullWindow mode is treated as a prefixed event and will get a fullscreenchange event as well
  21093. if (this.isFullscreen_ !== oldValue && this.fsApi_.prefixed) {
  21094. /**
  21095. * @event Player#fullscreenchange
  21096. * @type {Event}
  21097. */
  21098. this.trigger('fullscreenchange');
  21099. }
  21100. this.toggleFullscreenClass_();
  21101. return;
  21102. }
  21103. return this.isFullscreen_;
  21104. }
  21105. /**
  21106. * Increase the size of the video to full screen
  21107. * In some browsers, full screen is not supported natively, so it enters
  21108. * "full window mode", where the video fills the browser window.
  21109. * In browsers and devices that support native full screen, sometimes the
  21110. * browser's default controls will be shown, and not the Video.js custom skin.
  21111. * This includes most mobile devices (iOS, Android) and older versions of
  21112. * Safari.
  21113. *
  21114. * @param {Object} [fullscreenOptions]
  21115. * Override the player fullscreen options
  21116. *
  21117. * @fires Player#fullscreenchange
  21118. */
  21119. requestFullscreen(fullscreenOptions) {
  21120. if (this.isInPictureInPicture()) {
  21121. this.exitPictureInPicture();
  21122. }
  21123. const self = this;
  21124. return new Promise((resolve, reject) => {
  21125. function offHandler() {
  21126. self.off('fullscreenerror', errorHandler);
  21127. self.off('fullscreenchange', changeHandler);
  21128. }
  21129. function changeHandler() {
  21130. offHandler();
  21131. resolve();
  21132. }
  21133. function errorHandler(e, err) {
  21134. offHandler();
  21135. reject(err);
  21136. }
  21137. self.one('fullscreenchange', changeHandler);
  21138. self.one('fullscreenerror', errorHandler);
  21139. const promise = self.requestFullscreenHelper_(fullscreenOptions);
  21140. if (promise) {
  21141. promise.then(offHandler, offHandler);
  21142. promise.then(resolve, reject);
  21143. }
  21144. });
  21145. }
  21146. requestFullscreenHelper_(fullscreenOptions) {
  21147. let fsOptions;
  21148. // Only pass fullscreen options to requestFullscreen in spec-compliant browsers.
  21149. // Use defaults or player configured option unless passed directly to this method.
  21150. if (!this.fsApi_.prefixed) {
  21151. fsOptions = this.options_.fullscreen && this.options_.fullscreen.options || {};
  21152. if (fullscreenOptions !== undefined) {
  21153. fsOptions = fullscreenOptions;
  21154. }
  21155. }
  21156. // This method works as follows:
  21157. // 1. if a fullscreen api is available, use it
  21158. // 1. call requestFullscreen with potential options
  21159. // 2. if we got a promise from above, use it to update isFullscreen()
  21160. // 2. otherwise, if the tech supports fullscreen, call `enterFullScreen` on it.
  21161. // This is particularly used for iPhone, older iPads, and non-safari browser on iOS.
  21162. // 3. otherwise, use "fullWindow" mode
  21163. if (this.fsApi_.requestFullscreen) {
  21164. const promise = this.el_[this.fsApi_.requestFullscreen](fsOptions);
  21165. // Even on browsers with promise support this may not return a promise
  21166. if (promise) {
  21167. promise.then(() => this.isFullscreen(true), () => this.isFullscreen(false));
  21168. }
  21169. return promise;
  21170. } else if (this.tech_.supportsFullScreen() && !this.options_.preferFullWindow === true) {
  21171. // we can't take the video.js controls fullscreen but we can go fullscreen
  21172. // with native controls
  21173. this.techCall_('enterFullScreen');
  21174. } else {
  21175. // fullscreen isn't supported so we'll just stretch the video element to
  21176. // fill the viewport
  21177. this.enterFullWindow();
  21178. }
  21179. }
  21180. /**
  21181. * Return the video to its normal size after having been in full screen mode
  21182. *
  21183. * @fires Player#fullscreenchange
  21184. */
  21185. exitFullscreen() {
  21186. const self = this;
  21187. return new Promise((resolve, reject) => {
  21188. function offHandler() {
  21189. self.off('fullscreenerror', errorHandler);
  21190. self.off('fullscreenchange', changeHandler);
  21191. }
  21192. function changeHandler() {
  21193. offHandler();
  21194. resolve();
  21195. }
  21196. function errorHandler(e, err) {
  21197. offHandler();
  21198. reject(err);
  21199. }
  21200. self.one('fullscreenchange', changeHandler);
  21201. self.one('fullscreenerror', errorHandler);
  21202. const promise = self.exitFullscreenHelper_();
  21203. if (promise) {
  21204. promise.then(offHandler, offHandler);
  21205. // map the promise to our resolve/reject methods
  21206. promise.then(resolve, reject);
  21207. }
  21208. });
  21209. }
  21210. exitFullscreenHelper_() {
  21211. if (this.fsApi_.requestFullscreen) {
  21212. const promise = document[this.fsApi_.exitFullscreen]();
  21213. // Even on browsers with promise support this may not return a promise
  21214. if (promise) {
  21215. // we're splitting the promise here, so, we want to catch the
  21216. // potential error so that this chain doesn't have unhandled errors
  21217. silencePromise(promise.then(() => this.isFullscreen(false)));
  21218. }
  21219. return promise;
  21220. } else if (this.tech_.supportsFullScreen() && !this.options_.preferFullWindow === true) {
  21221. this.techCall_('exitFullScreen');
  21222. } else {
  21223. this.exitFullWindow();
  21224. }
  21225. }
  21226. /**
  21227. * When fullscreen isn't supported we can stretch the
  21228. * video container to as wide as the browser will let us.
  21229. *
  21230. * @fires Player#enterFullWindow
  21231. */
  21232. enterFullWindow() {
  21233. this.isFullscreen(true);
  21234. this.isFullWindow = true;
  21235. // Storing original doc overflow value to return to when fullscreen is off
  21236. this.docOrigOverflow = document.documentElement.style.overflow;
  21237. // Add listener for esc key to exit fullscreen
  21238. on(document, 'keydown', this.boundFullWindowOnEscKey_);
  21239. // Hide any scroll bars
  21240. document.documentElement.style.overflow = 'hidden';
  21241. // Apply fullscreen styles
  21242. addClass(document.body, 'vjs-full-window');
  21243. /**
  21244. * @event Player#enterFullWindow
  21245. * @type {Event}
  21246. */
  21247. this.trigger('enterFullWindow');
  21248. }
  21249. /**
  21250. * Check for call to either exit full window or
  21251. * full screen on ESC key
  21252. *
  21253. * @param {string} event
  21254. * Event to check for key press
  21255. */
  21256. fullWindowOnEscKey(event) {
  21257. if (keycode.isEventKey(event, 'Esc')) {
  21258. if (this.isFullscreen() === true) {
  21259. if (!this.isFullWindow) {
  21260. this.exitFullscreen();
  21261. } else {
  21262. this.exitFullWindow();
  21263. }
  21264. }
  21265. }
  21266. }
  21267. /**
  21268. * Exit full window
  21269. *
  21270. * @fires Player#exitFullWindow
  21271. */
  21272. exitFullWindow() {
  21273. this.isFullscreen(false);
  21274. this.isFullWindow = false;
  21275. off(document, 'keydown', this.boundFullWindowOnEscKey_);
  21276. // Unhide scroll bars.
  21277. document.documentElement.style.overflow = this.docOrigOverflow;
  21278. // Remove fullscreen styles
  21279. removeClass(document.body, 'vjs-full-window');
  21280. // Resize the box, controller, and poster to original sizes
  21281. // this.positionAll();
  21282. /**
  21283. * @event Player#exitFullWindow
  21284. * @type {Event}
  21285. */
  21286. this.trigger('exitFullWindow');
  21287. }
  21288. /**
  21289. * Get or set disable Picture-in-Picture mode.
  21290. *
  21291. * @param {boolean} [value]
  21292. * - true will disable Picture-in-Picture mode
  21293. * - false will enable Picture-in-Picture mode
  21294. */
  21295. disablePictureInPicture(value) {
  21296. if (value === undefined) {
  21297. return this.techGet_('disablePictureInPicture');
  21298. }
  21299. this.techCall_('setDisablePictureInPicture', value);
  21300. this.options_.disablePictureInPicture = value;
  21301. this.trigger('disablepictureinpicturechanged');
  21302. }
  21303. /**
  21304. * Check if the player is in Picture-in-Picture mode or tell the player that it
  21305. * is or is not in Picture-in-Picture mode.
  21306. *
  21307. * @param {boolean} [isPiP]
  21308. * Set the players current Picture-in-Picture state
  21309. *
  21310. * @return {boolean|undefined}
  21311. * - true if Picture-in-Picture is on and getting
  21312. * - false if Picture-in-Picture is off and getting
  21313. * - nothing if setting
  21314. */
  21315. isInPictureInPicture(isPiP) {
  21316. if (isPiP !== undefined) {
  21317. this.isInPictureInPicture_ = !!isPiP;
  21318. this.togglePictureInPictureClass_();
  21319. return;
  21320. }
  21321. return !!this.isInPictureInPicture_;
  21322. }
  21323. /**
  21324. * Create a floating video window always on top of other windows so that users may
  21325. * continue consuming media while they interact with other content sites, or
  21326. * applications on their device.
  21327. *
  21328. * This can use document picture-in-picture or element picture in picture
  21329. *
  21330. * Set `enableDocumentPictureInPicture` to `true` to use docPiP on a supported browser
  21331. * Else set `disablePictureInPicture` to `false` to disable elPiP on a supported browser
  21332. *
  21333. *
  21334. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/}
  21335. * @see [Spec]{@link https://wicg.github.io/document-picture-in-picture/}
  21336. *
  21337. * @fires Player#enterpictureinpicture
  21338. *
  21339. * @return {Promise}
  21340. * A promise with a Picture-in-Picture window.
  21341. */
  21342. requestPictureInPicture() {
  21343. if (this.options_.enableDocumentPictureInPicture && window.documentPictureInPicture) {
  21344. const pipContainer = document.createElement(this.el().tagName);
  21345. pipContainer.classList = this.el().classList;
  21346. pipContainer.classList.add('vjs-pip-container');
  21347. if (this.posterImage) {
  21348. pipContainer.appendChild(this.posterImage.el().cloneNode(true));
  21349. }
  21350. if (this.titleBar) {
  21351. pipContainer.appendChild(this.titleBar.el().cloneNode(true));
  21352. }
  21353. pipContainer.appendChild(createEl('p', {
  21354. className: 'vjs-pip-text'
  21355. }, {}, this.localize('Playing in picture-in-picture')));
  21356. return window.documentPictureInPicture.requestWindow({
  21357. // The aspect ratio won't be correct, Chrome bug https://crbug.com/1407629
  21358. width: this.videoWidth(),
  21359. height: this.videoHeight()
  21360. }).then(pipWindow => {
  21361. copyStyleSheetsToWindow(pipWindow);
  21362. this.el_.parentNode.insertBefore(pipContainer, this.el_);
  21363. pipWindow.document.body.appendChild(this.el_);
  21364. pipWindow.document.body.classList.add('vjs-pip-window');
  21365. this.player_.isInPictureInPicture(true);
  21366. this.player_.trigger('enterpictureinpicture');
  21367. // Listen for the PiP closing event to move the video back.
  21368. pipWindow.addEventListener('pagehide', event => {
  21369. const pipVideo = event.target.querySelector('.video-js');
  21370. pipContainer.parentNode.replaceChild(pipVideo, pipContainer);
  21371. this.player_.isInPictureInPicture(false);
  21372. this.player_.trigger('leavepictureinpicture');
  21373. });
  21374. return pipWindow;
  21375. });
  21376. }
  21377. if ('pictureInPictureEnabled' in document && this.disablePictureInPicture() === false) {
  21378. /**
  21379. * This event fires when the player enters picture in picture mode
  21380. *
  21381. * @event Player#enterpictureinpicture
  21382. * @type {Event}
  21383. */
  21384. return this.techGet_('requestPictureInPicture');
  21385. }
  21386. return Promise.reject('No PiP mode is available');
  21387. }
  21388. /**
  21389. * Exit Picture-in-Picture mode.
  21390. *
  21391. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  21392. *
  21393. * @fires Player#leavepictureinpicture
  21394. *
  21395. * @return {Promise}
  21396. * A promise.
  21397. */
  21398. exitPictureInPicture() {
  21399. if (window.documentPictureInPicture && window.documentPictureInPicture.window) {
  21400. // With documentPictureInPicture, Player#leavepictureinpicture is fired in the pagehide handler
  21401. window.documentPictureInPicture.window.close();
  21402. return Promise.resolve();
  21403. }
  21404. if ('pictureInPictureEnabled' in document) {
  21405. /**
  21406. * This event fires when the player leaves picture in picture mode
  21407. *
  21408. * @event Player#leavepictureinpicture
  21409. * @type {Event}
  21410. */
  21411. return document.exitPictureInPicture();
  21412. }
  21413. }
  21414. /**
  21415. * Called when this Player has focus and a key gets pressed down, or when
  21416. * any Component of this player receives a key press that it doesn't handle.
  21417. * This allows player-wide hotkeys (either as defined below, or optionally
  21418. * by an external function).
  21419. *
  21420. * @param {KeyboardEvent} event
  21421. * The `keydown` event that caused this function to be called.
  21422. *
  21423. * @listens keydown
  21424. */
  21425. handleKeyDown(event) {
  21426. const {
  21427. userActions
  21428. } = this.options_;
  21429. // Bail out if hotkeys are not configured.
  21430. if (!userActions || !userActions.hotkeys) {
  21431. return;
  21432. }
  21433. // Function that determines whether or not to exclude an element from
  21434. // hotkeys handling.
  21435. const excludeElement = el => {
  21436. const tagName = el.tagName.toLowerCase();
  21437. // The first and easiest test is for `contenteditable` elements.
  21438. if (el.isContentEditable) {
  21439. return true;
  21440. }
  21441. // Inputs matching these types will still trigger hotkey handling as
  21442. // they are not text inputs.
  21443. const allowedInputTypes = ['button', 'checkbox', 'hidden', 'radio', 'reset', 'submit'];
  21444. if (tagName === 'input') {
  21445. return allowedInputTypes.indexOf(el.type) === -1;
  21446. }
  21447. // The final test is by tag name. These tags will be excluded entirely.
  21448. const excludedTags = ['textarea'];
  21449. return excludedTags.indexOf(tagName) !== -1;
  21450. };
  21451. // Bail out if the user is focused on an interactive form element.
  21452. if (excludeElement(this.el_.ownerDocument.activeElement)) {
  21453. return;
  21454. }
  21455. if (typeof userActions.hotkeys === 'function') {
  21456. userActions.hotkeys.call(this, event);
  21457. } else {
  21458. this.handleHotkeys(event);
  21459. }
  21460. }
  21461. /**
  21462. * Called when this Player receives a hotkey keydown event.
  21463. * Supported player-wide hotkeys are:
  21464. *
  21465. * f - toggle fullscreen
  21466. * m - toggle mute
  21467. * k or Space - toggle play/pause
  21468. *
  21469. * @param {Event} event
  21470. * The `keydown` event that caused this function to be called.
  21471. */
  21472. handleHotkeys(event) {
  21473. const hotkeys = this.options_.userActions ? this.options_.userActions.hotkeys : {};
  21474. // set fullscreenKey, muteKey, playPauseKey from `hotkeys`, use defaults if not set
  21475. const {
  21476. fullscreenKey = keydownEvent => keycode.isEventKey(keydownEvent, 'f'),
  21477. muteKey = keydownEvent => keycode.isEventKey(keydownEvent, 'm'),
  21478. playPauseKey = keydownEvent => keycode.isEventKey(keydownEvent, 'k') || keycode.isEventKey(keydownEvent, 'Space')
  21479. } = hotkeys;
  21480. if (fullscreenKey.call(this, event)) {
  21481. event.preventDefault();
  21482. event.stopPropagation();
  21483. const FSToggle = Component.getComponent('FullscreenToggle');
  21484. if (document[this.fsApi_.fullscreenEnabled] !== false) {
  21485. FSToggle.prototype.handleClick.call(this, event);
  21486. }
  21487. } else if (muteKey.call(this, event)) {
  21488. event.preventDefault();
  21489. event.stopPropagation();
  21490. const MuteToggle = Component.getComponent('MuteToggle');
  21491. MuteToggle.prototype.handleClick.call(this, event);
  21492. } else if (playPauseKey.call(this, event)) {
  21493. event.preventDefault();
  21494. event.stopPropagation();
  21495. const PlayToggle = Component.getComponent('PlayToggle');
  21496. PlayToggle.prototype.handleClick.call(this, event);
  21497. }
  21498. }
  21499. /**
  21500. * Check whether the player can play a given mimetype
  21501. *
  21502. * @see https://www.w3.org/TR/2011/WD-html5-20110113/video.html#dom-navigator-canplaytype
  21503. *
  21504. * @param {string} type
  21505. * The mimetype to check
  21506. *
  21507. * @return {string}
  21508. * 'probably', 'maybe', or '' (empty string)
  21509. */
  21510. canPlayType(type) {
  21511. let can;
  21512. // Loop through each playback technology in the options order
  21513. for (let i = 0, j = this.options_.techOrder; i < j.length; i++) {
  21514. const techName = j[i];
  21515. let tech = Tech.getTech(techName);
  21516. // Support old behavior of techs being registered as components.
  21517. // Remove once that deprecated behavior is removed.
  21518. if (!tech) {
  21519. tech = Component.getComponent(techName);
  21520. }
  21521. // Check if the current tech is defined before continuing
  21522. if (!tech) {
  21523. log.error(`The "${techName}" tech is undefined. Skipped browser support check for that tech.`);
  21524. continue;
  21525. }
  21526. // Check if the browser supports this technology
  21527. if (tech.isSupported()) {
  21528. can = tech.canPlayType(type);
  21529. if (can) {
  21530. return can;
  21531. }
  21532. }
  21533. }
  21534. return '';
  21535. }
  21536. /**
  21537. * Select source based on tech-order or source-order
  21538. * Uses source-order selection if `options.sourceOrder` is truthy. Otherwise,
  21539. * defaults to tech-order selection
  21540. *
  21541. * @param {Array} sources
  21542. * The sources for a media asset
  21543. *
  21544. * @return {Object|boolean}
  21545. * Object of source and tech order or false
  21546. */
  21547. selectSource(sources) {
  21548. // Get only the techs specified in `techOrder` that exist and are supported by the
  21549. // current platform
  21550. const techs = this.options_.techOrder.map(techName => {
  21551. return [techName, Tech.getTech(techName)];
  21552. }).filter(([techName, tech]) => {
  21553. // Check if the current tech is defined before continuing
  21554. if (tech) {
  21555. // Check if the browser supports this technology
  21556. return tech.isSupported();
  21557. }
  21558. log.error(`The "${techName}" tech is undefined. Skipped browser support check for that tech.`);
  21559. return false;
  21560. });
  21561. // Iterate over each `innerArray` element once per `outerArray` element and execute
  21562. // `tester` with both. If `tester` returns a non-falsy value, exit early and return
  21563. // that value.
  21564. const findFirstPassingTechSourcePair = function (outerArray, innerArray, tester) {
  21565. let found;
  21566. outerArray.some(outerChoice => {
  21567. return innerArray.some(innerChoice => {
  21568. found = tester(outerChoice, innerChoice);
  21569. if (found) {
  21570. return true;
  21571. }
  21572. });
  21573. });
  21574. return found;
  21575. };
  21576. let foundSourceAndTech;
  21577. const flip = fn => (a, b) => fn(b, a);
  21578. const finder = ([techName, tech], source) => {
  21579. if (tech.canPlaySource(source, this.options_[techName.toLowerCase()])) {
  21580. return {
  21581. source,
  21582. tech: techName
  21583. };
  21584. }
  21585. };
  21586. // Depending on the truthiness of `options.sourceOrder`, we swap the order of techs and sources
  21587. // to select from them based on their priority.
  21588. if (this.options_.sourceOrder) {
  21589. // Source-first ordering
  21590. foundSourceAndTech = findFirstPassingTechSourcePair(sources, techs, flip(finder));
  21591. } else {
  21592. // Tech-first ordering
  21593. foundSourceAndTech = findFirstPassingTechSourcePair(techs, sources, finder);
  21594. }
  21595. return foundSourceAndTech || false;
  21596. }
  21597. /**
  21598. * Executes source setting and getting logic
  21599. *
  21600. * @param {Tech~SourceObject|Tech~SourceObject[]|string} [source]
  21601. * A SourceObject, an array of SourceObjects, or a string referencing
  21602. * a URL to a media source. It is _highly recommended_ that an object
  21603. * or array of objects is used here, so that source selection
  21604. * algorithms can take the `type` into account.
  21605. *
  21606. * If not provided, this method acts as a getter.
  21607. * @param {boolean} [isRetry]
  21608. * Indicates whether this is being called internally as a result of a retry
  21609. *
  21610. * @return {string|undefined}
  21611. * If the `source` argument is missing, returns the current source
  21612. * URL. Otherwise, returns nothing/undefined.
  21613. */
  21614. handleSrc_(source, isRetry) {
  21615. // getter usage
  21616. if (typeof source === 'undefined') {
  21617. return this.cache_.src || '';
  21618. }
  21619. // Reset retry behavior for new source
  21620. if (this.resetRetryOnError_) {
  21621. this.resetRetryOnError_();
  21622. }
  21623. // filter out invalid sources and turn our source into
  21624. // an array of source objects
  21625. const sources = filterSource(source);
  21626. // if a source was passed in then it is invalid because
  21627. // it was filtered to a zero length Array. So we have to
  21628. // show an error
  21629. if (!sources.length) {
  21630. this.setTimeout(function () {
  21631. this.error({
  21632. code: 4,
  21633. message: this.options_.notSupportedMessage
  21634. });
  21635. }, 0);
  21636. return;
  21637. }
  21638. // initial sources
  21639. this.changingSrc_ = true;
  21640. // Only update the cached source list if we are not retrying a new source after error,
  21641. // since in that case we want to include the failed source(s) in the cache
  21642. if (!isRetry) {
  21643. this.cache_.sources = sources;
  21644. }
  21645. this.updateSourceCaches_(sources[0]);
  21646. // middlewareSource is the source after it has been changed by middleware
  21647. setSource(this, sources[0], (middlewareSource, mws) => {
  21648. this.middleware_ = mws;
  21649. // since sourceSet is async we have to update the cache again after we select a source since
  21650. // the source that is selected could be out of order from the cache update above this callback.
  21651. if (!isRetry) {
  21652. this.cache_.sources = sources;
  21653. }
  21654. this.updateSourceCaches_(middlewareSource);
  21655. const err = this.src_(middlewareSource);
  21656. if (err) {
  21657. if (sources.length > 1) {
  21658. return this.handleSrc_(sources.slice(1));
  21659. }
  21660. this.changingSrc_ = false;
  21661. // We need to wrap this in a timeout to give folks a chance to add error event handlers
  21662. this.setTimeout(function () {
  21663. this.error({
  21664. code: 4,
  21665. message: this.options_.notSupportedMessage
  21666. });
  21667. }, 0);
  21668. // we could not find an appropriate tech, but let's still notify the delegate that this is it
  21669. // this needs a better comment about why this is needed
  21670. this.triggerReady();
  21671. return;
  21672. }
  21673. setTech(mws, this.tech_);
  21674. });
  21675. // Try another available source if this one fails before playback.
  21676. if (sources.length > 1) {
  21677. const retry = () => {
  21678. // Remove the error modal
  21679. this.error(null);
  21680. this.handleSrc_(sources.slice(1), true);
  21681. };
  21682. const stopListeningForErrors = () => {
  21683. this.off('error', retry);
  21684. };
  21685. this.one('error', retry);
  21686. this.one('playing', stopListeningForErrors);
  21687. this.resetRetryOnError_ = () => {
  21688. this.off('error', retry);
  21689. this.off('playing', stopListeningForErrors);
  21690. };
  21691. }
  21692. }
  21693. /**
  21694. * Get or set the video source.
  21695. *
  21696. * @param {Tech~SourceObject|Tech~SourceObject[]|string} [source]
  21697. * A SourceObject, an array of SourceObjects, or a string referencing
  21698. * a URL to a media source. It is _highly recommended_ that an object
  21699. * or array of objects is used here, so that source selection
  21700. * algorithms can take the `type` into account.
  21701. *
  21702. * If not provided, this method acts as a getter.
  21703. *
  21704. * @return {string|undefined}
  21705. * If the `source` argument is missing, returns the current source
  21706. * URL. Otherwise, returns nothing/undefined.
  21707. */
  21708. src(source) {
  21709. return this.handleSrc_(source, false);
  21710. }
  21711. /**
  21712. * Set the source object on the tech, returns a boolean that indicates whether
  21713. * there is a tech that can play the source or not
  21714. *
  21715. * @param {Tech~SourceObject} source
  21716. * The source object to set on the Tech
  21717. *
  21718. * @return {boolean}
  21719. * - True if there is no Tech to playback this source
  21720. * - False otherwise
  21721. *
  21722. * @private
  21723. */
  21724. src_(source) {
  21725. const sourceTech = this.selectSource([source]);
  21726. if (!sourceTech) {
  21727. return true;
  21728. }
  21729. if (!titleCaseEquals(sourceTech.tech, this.techName_)) {
  21730. this.changingSrc_ = true;
  21731. // load this technology with the chosen source
  21732. this.loadTech_(sourceTech.tech, sourceTech.source);
  21733. this.tech_.ready(() => {
  21734. this.changingSrc_ = false;
  21735. });
  21736. return false;
  21737. }
  21738. // wait until the tech is ready to set the source
  21739. // and set it synchronously if possible (#2326)
  21740. this.ready(function () {
  21741. // The setSource tech method was added with source handlers
  21742. // so older techs won't support it
  21743. // We need to check the direct prototype for the case where subclasses
  21744. // of the tech do not support source handlers
  21745. if (this.tech_.constructor.prototype.hasOwnProperty('setSource')) {
  21746. this.techCall_('setSource', source);
  21747. } else {
  21748. this.techCall_('src', source.src);
  21749. }
  21750. this.changingSrc_ = false;
  21751. }, true);
  21752. return false;
  21753. }
  21754. /**
  21755. * Begin loading the src data.
  21756. */
  21757. load() {
  21758. // Workaround to use the load method with the VHS.
  21759. // Does not cover the case when the load method is called directly from the mediaElement.
  21760. if (this.tech_ && this.tech_.vhs) {
  21761. this.src(this.currentSource());
  21762. return;
  21763. }
  21764. this.techCall_('load');
  21765. }
  21766. /**
  21767. * Reset the player. Loads the first tech in the techOrder,
  21768. * removes all the text tracks in the existing `tech`,
  21769. * and calls `reset` on the `tech`.
  21770. */
  21771. reset() {
  21772. if (this.paused()) {
  21773. this.doReset_();
  21774. } else {
  21775. const playPromise = this.play();
  21776. silencePromise(playPromise.then(() => this.doReset_()));
  21777. }
  21778. }
  21779. doReset_() {
  21780. if (this.tech_) {
  21781. this.tech_.clearTracks('text');
  21782. }
  21783. this.removeClass('vjs-playing');
  21784. this.addClass('vjs-paused');
  21785. this.resetCache_();
  21786. this.poster('');
  21787. this.loadTech_(this.options_.techOrder[0], null);
  21788. this.techCall_('reset');
  21789. this.resetControlBarUI_();
  21790. this.error(null);
  21791. if (this.titleBar) {
  21792. this.titleBar.update({
  21793. title: undefined,
  21794. description: undefined
  21795. });
  21796. }
  21797. if (isEvented(this)) {
  21798. this.trigger('playerreset');
  21799. }
  21800. }
  21801. /**
  21802. * Reset Control Bar's UI by calling sub-methods that reset
  21803. * all of Control Bar's components
  21804. */
  21805. resetControlBarUI_() {
  21806. this.resetProgressBar_();
  21807. this.resetPlaybackRate_();
  21808. this.resetVolumeBar_();
  21809. }
  21810. /**
  21811. * Reset tech's progress so progress bar is reset in the UI
  21812. */
  21813. resetProgressBar_() {
  21814. this.currentTime(0);
  21815. const {
  21816. currentTimeDisplay,
  21817. durationDisplay,
  21818. progressControl,
  21819. remainingTimeDisplay
  21820. } = this.controlBar || {};
  21821. const {
  21822. seekBar
  21823. } = progressControl || {};
  21824. if (currentTimeDisplay) {
  21825. currentTimeDisplay.updateContent();
  21826. }
  21827. if (durationDisplay) {
  21828. durationDisplay.updateContent();
  21829. }
  21830. if (remainingTimeDisplay) {
  21831. remainingTimeDisplay.updateContent();
  21832. }
  21833. if (seekBar) {
  21834. seekBar.update();
  21835. if (seekBar.loadProgressBar) {
  21836. seekBar.loadProgressBar.update();
  21837. }
  21838. }
  21839. }
  21840. /**
  21841. * Reset Playback ratio
  21842. */
  21843. resetPlaybackRate_() {
  21844. this.playbackRate(this.defaultPlaybackRate());
  21845. this.handleTechRateChange_();
  21846. }
  21847. /**
  21848. * Reset Volume bar
  21849. */
  21850. resetVolumeBar_() {
  21851. this.volume(1.0);
  21852. this.trigger('volumechange');
  21853. }
  21854. /**
  21855. * Returns all of the current source objects.
  21856. *
  21857. * @return {Tech~SourceObject[]}
  21858. * The current source objects
  21859. */
  21860. currentSources() {
  21861. const source = this.currentSource();
  21862. const sources = [];
  21863. // assume `{}` or `{ src }`
  21864. if (Object.keys(source).length !== 0) {
  21865. sources.push(source);
  21866. }
  21867. return this.cache_.sources || sources;
  21868. }
  21869. /**
  21870. * Returns the current source object.
  21871. *
  21872. * @return {Tech~SourceObject}
  21873. * The current source object
  21874. */
  21875. currentSource() {
  21876. return this.cache_.source || {};
  21877. }
  21878. /**
  21879. * Returns the fully qualified URL of the current source value e.g. http://mysite.com/video.mp4
  21880. * Can be used in conjunction with `currentType` to assist in rebuilding the current source object.
  21881. *
  21882. * @return {string}
  21883. * The current source
  21884. */
  21885. currentSrc() {
  21886. return this.currentSource() && this.currentSource().src || '';
  21887. }
  21888. /**
  21889. * Get the current source type e.g. video/mp4
  21890. * This can allow you rebuild the current source object so that you could load the same
  21891. * source and tech later
  21892. *
  21893. * @return {string}
  21894. * The source MIME type
  21895. */
  21896. currentType() {
  21897. return this.currentSource() && this.currentSource().type || '';
  21898. }
  21899. /**
  21900. * Get or set the preload attribute
  21901. *
  21902. * @param {'none'|'auto'|'metadata'} [value]
  21903. * Preload mode to pass to tech
  21904. *
  21905. * @return {string|undefined}
  21906. * - The preload attribute value when getting
  21907. * - Nothing when setting
  21908. */
  21909. preload(value) {
  21910. if (value !== undefined) {
  21911. this.techCall_('setPreload', value);
  21912. this.options_.preload = value;
  21913. return;
  21914. }
  21915. return this.techGet_('preload');
  21916. }
  21917. /**
  21918. * Get or set the autoplay option. When this is a boolean it will
  21919. * modify the attribute on the tech. When this is a string the attribute on
  21920. * the tech will be removed and `Player` will handle autoplay on loadstarts.
  21921. *
  21922. * @param {boolean|'play'|'muted'|'any'} [value]
  21923. * - true: autoplay using the browser behavior
  21924. * - false: do not autoplay
  21925. * - 'play': call play() on every loadstart
  21926. * - 'muted': call muted() then play() on every loadstart
  21927. * - 'any': call play() on every loadstart. if that fails call muted() then play().
  21928. * - *: values other than those listed here will be set `autoplay` to true
  21929. *
  21930. * @return {boolean|string|undefined}
  21931. * - The current value of autoplay when getting
  21932. * - Nothing when setting
  21933. */
  21934. autoplay(value) {
  21935. // getter usage
  21936. if (value === undefined) {
  21937. return this.options_.autoplay || false;
  21938. }
  21939. let techAutoplay;
  21940. // if the value is a valid string set it to that, or normalize `true` to 'play', if need be
  21941. if (typeof value === 'string' && /(any|play|muted)/.test(value) || value === true && this.options_.normalizeAutoplay) {
  21942. this.options_.autoplay = value;
  21943. this.manualAutoplay_(typeof value === 'string' ? value : 'play');
  21944. techAutoplay = false;
  21945. // any falsy value sets autoplay to false in the browser,
  21946. // lets do the same
  21947. } else if (!value) {
  21948. this.options_.autoplay = false;
  21949. // any other value (ie truthy) sets autoplay to true
  21950. } else {
  21951. this.options_.autoplay = true;
  21952. }
  21953. techAutoplay = typeof techAutoplay === 'undefined' ? this.options_.autoplay : techAutoplay;
  21954. // if we don't have a tech then we do not queue up
  21955. // a setAutoplay call on tech ready. We do this because the
  21956. // autoplay option will be passed in the constructor and we
  21957. // do not need to set it twice
  21958. if (this.tech_) {
  21959. this.techCall_('setAutoplay', techAutoplay);
  21960. }
  21961. }
  21962. /**
  21963. * Set or unset the playsinline attribute.
  21964. * Playsinline tells the browser that non-fullscreen playback is preferred.
  21965. *
  21966. * @param {boolean} [value]
  21967. * - true means that we should try to play inline by default
  21968. * - false means that we should use the browser's default playback mode,
  21969. * which in most cases is inline. iOS Safari is a notable exception
  21970. * and plays fullscreen by default.
  21971. *
  21972. * @return {string|undefined}
  21973. * - the current value of playsinline
  21974. * - Nothing when setting
  21975. *
  21976. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  21977. */
  21978. playsinline(value) {
  21979. if (value !== undefined) {
  21980. this.techCall_('setPlaysinline', value);
  21981. this.options_.playsinline = value;
  21982. }
  21983. return this.techGet_('playsinline');
  21984. }
  21985. /**
  21986. * Get or set the loop attribute on the video element.
  21987. *
  21988. * @param {boolean} [value]
  21989. * - true means that we should loop the video
  21990. * - false means that we should not loop the video
  21991. *
  21992. * @return {boolean|undefined}
  21993. * - The current value of loop when getting
  21994. * - Nothing when setting
  21995. */
  21996. loop(value) {
  21997. if (value !== undefined) {
  21998. this.techCall_('setLoop', value);
  21999. this.options_.loop = value;
  22000. return;
  22001. }
  22002. return this.techGet_('loop');
  22003. }
  22004. /**
  22005. * Get or set the poster image source url
  22006. *
  22007. * @fires Player#posterchange
  22008. *
  22009. * @param {string} [src]
  22010. * Poster image source URL
  22011. *
  22012. * @return {string|undefined}
  22013. * - The current value of poster when getting
  22014. * - Nothing when setting
  22015. */
  22016. poster(src) {
  22017. if (src === undefined) {
  22018. return this.poster_;
  22019. }
  22020. // The correct way to remove a poster is to set as an empty string
  22021. // other falsey values will throw errors
  22022. if (!src) {
  22023. src = '';
  22024. }
  22025. if (src === this.poster_) {
  22026. return;
  22027. }
  22028. // update the internal poster variable
  22029. this.poster_ = src;
  22030. // update the tech's poster
  22031. this.techCall_('setPoster', src);
  22032. this.isPosterFromTech_ = false;
  22033. // alert components that the poster has been set
  22034. /**
  22035. * This event fires when the poster image is changed on the player.
  22036. *
  22037. * @event Player#posterchange
  22038. * @type {Event}
  22039. */
  22040. this.trigger('posterchange');
  22041. }
  22042. /**
  22043. * Some techs (e.g. YouTube) can provide a poster source in an
  22044. * asynchronous way. We want the poster component to use this
  22045. * poster source so that it covers up the tech's controls.
  22046. * (YouTube's play button). However we only want to use this
  22047. * source if the player user hasn't set a poster through
  22048. * the normal APIs.
  22049. *
  22050. * @fires Player#posterchange
  22051. * @listens Tech#posterchange
  22052. * @private
  22053. */
  22054. handleTechPosterChange_() {
  22055. if ((!this.poster_ || this.options_.techCanOverridePoster) && this.tech_ && this.tech_.poster) {
  22056. const newPoster = this.tech_.poster() || '';
  22057. if (newPoster !== this.poster_) {
  22058. this.poster_ = newPoster;
  22059. this.isPosterFromTech_ = true;
  22060. // Let components know the poster has changed
  22061. this.trigger('posterchange');
  22062. }
  22063. }
  22064. }
  22065. /**
  22066. * Get or set whether or not the controls are showing.
  22067. *
  22068. * @fires Player#controlsenabled
  22069. *
  22070. * @param {boolean} [bool]
  22071. * - true to turn controls on
  22072. * - false to turn controls off
  22073. *
  22074. * @return {boolean|undefined}
  22075. * - The current value of controls when getting
  22076. * - Nothing when setting
  22077. */
  22078. controls(bool) {
  22079. if (bool === undefined) {
  22080. return !!this.controls_;
  22081. }
  22082. bool = !!bool;
  22083. // Don't trigger a change event unless it actually changed
  22084. if (this.controls_ === bool) {
  22085. return;
  22086. }
  22087. this.controls_ = bool;
  22088. if (this.usingNativeControls()) {
  22089. this.techCall_('setControls', bool);
  22090. }
  22091. if (this.controls_) {
  22092. this.removeClass('vjs-controls-disabled');
  22093. this.addClass('vjs-controls-enabled');
  22094. /**
  22095. * @event Player#controlsenabled
  22096. * @type {Event}
  22097. */
  22098. this.trigger('controlsenabled');
  22099. if (!this.usingNativeControls()) {
  22100. this.addTechControlsListeners_();
  22101. }
  22102. } else {
  22103. this.removeClass('vjs-controls-enabled');
  22104. this.addClass('vjs-controls-disabled');
  22105. /**
  22106. * @event Player#controlsdisabled
  22107. * @type {Event}
  22108. */
  22109. this.trigger('controlsdisabled');
  22110. if (!this.usingNativeControls()) {
  22111. this.removeTechControlsListeners_();
  22112. }
  22113. }
  22114. }
  22115. /**
  22116. * Toggle native controls on/off. Native controls are the controls built into
  22117. * devices (e.g. default iPhone controls) or other techs
  22118. * (e.g. Vimeo Controls)
  22119. * **This should only be set by the current tech, because only the tech knows
  22120. * if it can support native controls**
  22121. *
  22122. * @fires Player#usingnativecontrols
  22123. * @fires Player#usingcustomcontrols
  22124. *
  22125. * @param {boolean} [bool]
  22126. * - true to turn native controls on
  22127. * - false to turn native controls off
  22128. *
  22129. * @return {boolean|undefined}
  22130. * - The current value of native controls when getting
  22131. * - Nothing when setting
  22132. */
  22133. usingNativeControls(bool) {
  22134. if (bool === undefined) {
  22135. return !!this.usingNativeControls_;
  22136. }
  22137. bool = !!bool;
  22138. // Don't trigger a change event unless it actually changed
  22139. if (this.usingNativeControls_ === bool) {
  22140. return;
  22141. }
  22142. this.usingNativeControls_ = bool;
  22143. if (this.usingNativeControls_) {
  22144. this.addClass('vjs-using-native-controls');
  22145. /**
  22146. * player is using the native device controls
  22147. *
  22148. * @event Player#usingnativecontrols
  22149. * @type {Event}
  22150. */
  22151. this.trigger('usingnativecontrols');
  22152. } else {
  22153. this.removeClass('vjs-using-native-controls');
  22154. /**
  22155. * player is using the custom HTML controls
  22156. *
  22157. * @event Player#usingcustomcontrols
  22158. * @type {Event}
  22159. */
  22160. this.trigger('usingcustomcontrols');
  22161. }
  22162. }
  22163. /**
  22164. * Set or get the current MediaError
  22165. *
  22166. * @fires Player#error
  22167. *
  22168. * @param {MediaError|string|number} [err]
  22169. * A MediaError or a string/number to be turned
  22170. * into a MediaError
  22171. *
  22172. * @return {MediaError|null|undefined}
  22173. * - The current MediaError when getting (or null)
  22174. * - Nothing when setting
  22175. */
  22176. error(err) {
  22177. if (err === undefined) {
  22178. return this.error_ || null;
  22179. }
  22180. // allow hooks to modify error object
  22181. hooks('beforeerror').forEach(hookFunction => {
  22182. const newErr = hookFunction(this, err);
  22183. if (!(isObject(newErr) && !Array.isArray(newErr) || typeof newErr === 'string' || typeof newErr === 'number' || newErr === null)) {
  22184. this.log.error('please return a value that MediaError expects in beforeerror hooks');
  22185. return;
  22186. }
  22187. err = newErr;
  22188. });
  22189. // Suppress the first error message for no compatible source until
  22190. // user interaction
  22191. if (this.options_.suppressNotSupportedError && err && err.code === 4) {
  22192. const triggerSuppressedError = function () {
  22193. this.error(err);
  22194. };
  22195. this.options_.suppressNotSupportedError = false;
  22196. this.any(['click', 'touchstart'], triggerSuppressedError);
  22197. this.one('loadstart', function () {
  22198. this.off(['click', 'touchstart'], triggerSuppressedError);
  22199. });
  22200. return;
  22201. }
  22202. // restoring to default
  22203. if (err === null) {
  22204. this.error_ = null;
  22205. this.removeClass('vjs-error');
  22206. if (this.errorDisplay) {
  22207. this.errorDisplay.close();
  22208. }
  22209. return;
  22210. }
  22211. this.error_ = new MediaError(err);
  22212. // add the vjs-error classname to the player
  22213. this.addClass('vjs-error');
  22214. // log the name of the error type and any message
  22215. // IE11 logs "[object object]" and required you to expand message to see error object
  22216. log.error(`(CODE:${this.error_.code} ${MediaError.errorTypes[this.error_.code]})`, this.error_.message, this.error_);
  22217. /**
  22218. * @event Player#error
  22219. * @type {Event}
  22220. */
  22221. this.trigger('error');
  22222. // notify hooks of the per player error
  22223. hooks('error').forEach(hookFunction => hookFunction(this, this.error_));
  22224. return;
  22225. }
  22226. /**
  22227. * Report user activity
  22228. *
  22229. * @param {Object} event
  22230. * Event object
  22231. */
  22232. reportUserActivity(event) {
  22233. this.userActivity_ = true;
  22234. }
  22235. /**
  22236. * Get/set if user is active
  22237. *
  22238. * @fires Player#useractive
  22239. * @fires Player#userinactive
  22240. *
  22241. * @param {boolean} [bool]
  22242. * - true if the user is active
  22243. * - false if the user is inactive
  22244. *
  22245. * @return {boolean|undefined}
  22246. * - The current value of userActive when getting
  22247. * - Nothing when setting
  22248. */
  22249. userActive(bool) {
  22250. if (bool === undefined) {
  22251. return this.userActive_;
  22252. }
  22253. bool = !!bool;
  22254. if (bool === this.userActive_) {
  22255. return;
  22256. }
  22257. this.userActive_ = bool;
  22258. if (this.userActive_) {
  22259. this.userActivity_ = true;
  22260. this.removeClass('vjs-user-inactive');
  22261. this.addClass('vjs-user-active');
  22262. /**
  22263. * @event Player#useractive
  22264. * @type {Event}
  22265. */
  22266. this.trigger('useractive');
  22267. return;
  22268. }
  22269. // Chrome/Safari/IE have bugs where when you change the cursor it can
  22270. // trigger a mousemove event. This causes an issue when you're hiding
  22271. // the cursor when the user is inactive, and a mousemove signals user
  22272. // activity. Making it impossible to go into inactive mode. Specifically
  22273. // this happens in fullscreen when we really need to hide the cursor.
  22274. //
  22275. // When this gets resolved in ALL browsers it can be removed
  22276. // https://code.google.com/p/chromium/issues/detail?id=103041
  22277. if (this.tech_) {
  22278. this.tech_.one('mousemove', function (e) {
  22279. e.stopPropagation();
  22280. e.preventDefault();
  22281. });
  22282. }
  22283. this.userActivity_ = false;
  22284. this.removeClass('vjs-user-active');
  22285. this.addClass('vjs-user-inactive');
  22286. /**
  22287. * @event Player#userinactive
  22288. * @type {Event}
  22289. */
  22290. this.trigger('userinactive');
  22291. }
  22292. /**
  22293. * Listen for user activity based on timeout value
  22294. *
  22295. * @private
  22296. */
  22297. listenForUserActivity_() {
  22298. let mouseInProgress;
  22299. let lastMoveX;
  22300. let lastMoveY;
  22301. const handleActivity = bind_(this, this.reportUserActivity);
  22302. const handleMouseMove = function (e) {
  22303. // #1068 - Prevent mousemove spamming
  22304. // Chrome Bug: https://code.google.com/p/chromium/issues/detail?id=366970
  22305. if (e.screenX !== lastMoveX || e.screenY !== lastMoveY) {
  22306. lastMoveX = e.screenX;
  22307. lastMoveY = e.screenY;
  22308. handleActivity();
  22309. }
  22310. };
  22311. const handleMouseDown = function () {
  22312. handleActivity();
  22313. // For as long as the they are touching the device or have their mouse down,
  22314. // we consider them active even if they're not moving their finger or mouse.
  22315. // So we want to continue to update that they are active
  22316. this.clearInterval(mouseInProgress);
  22317. // Setting userActivity=true now and setting the interval to the same time
  22318. // as the activityCheck interval (250) should ensure we never miss the
  22319. // next activityCheck
  22320. mouseInProgress = this.setInterval(handleActivity, 250);
  22321. };
  22322. const handleMouseUpAndMouseLeave = function (event) {
  22323. handleActivity();
  22324. // Stop the interval that maintains activity if the mouse/touch is down
  22325. this.clearInterval(mouseInProgress);
  22326. };
  22327. // Any mouse movement will be considered user activity
  22328. this.on('mousedown', handleMouseDown);
  22329. this.on('mousemove', handleMouseMove);
  22330. this.on('mouseup', handleMouseUpAndMouseLeave);
  22331. this.on('mouseleave', handleMouseUpAndMouseLeave);
  22332. const controlBar = this.getChild('controlBar');
  22333. // Fixes bug on Android & iOS where when tapping progressBar (when control bar is displayed)
  22334. // controlBar would no longer be hidden by default timeout.
  22335. if (controlBar && !IS_IOS && !IS_ANDROID) {
  22336. controlBar.on('mouseenter', function (event) {
  22337. if (this.player().options_.inactivityTimeout !== 0) {
  22338. this.player().cache_.inactivityTimeout = this.player().options_.inactivityTimeout;
  22339. }
  22340. this.player().options_.inactivityTimeout = 0;
  22341. });
  22342. controlBar.on('mouseleave', function (event) {
  22343. this.player().options_.inactivityTimeout = this.player().cache_.inactivityTimeout;
  22344. });
  22345. }
  22346. // Listen for keyboard navigation
  22347. // Shouldn't need to use inProgress interval because of key repeat
  22348. this.on('keydown', handleActivity);
  22349. this.on('keyup', handleActivity);
  22350. // Run an interval every 250 milliseconds instead of stuffing everything into
  22351. // the mousemove/touchmove function itself, to prevent performance degradation.
  22352. // `this.reportUserActivity` simply sets this.userActivity_ to true, which
  22353. // then gets picked up by this loop
  22354. // http://ejohn.org/blog/learning-from-twitter/
  22355. let inactivityTimeout;
  22356. /** @this Player */
  22357. const activityCheck = function () {
  22358. // Check to see if mouse/touch activity has happened
  22359. if (!this.userActivity_) {
  22360. return;
  22361. }
  22362. // Reset the activity tracker
  22363. this.userActivity_ = false;
  22364. // If the user state was inactive, set the state to active
  22365. this.userActive(true);
  22366. // Clear any existing inactivity timeout to start the timer over
  22367. this.clearTimeout(inactivityTimeout);
  22368. const timeout = this.options_.inactivityTimeout;
  22369. if (timeout <= 0) {
  22370. return;
  22371. }
  22372. // In <timeout> milliseconds, if no more activity has occurred the
  22373. // user will be considered inactive
  22374. inactivityTimeout = this.setTimeout(function () {
  22375. // Protect against the case where the inactivityTimeout can trigger just
  22376. // before the next user activity is picked up by the activity check loop
  22377. // causing a flicker
  22378. if (!this.userActivity_) {
  22379. this.userActive(false);
  22380. }
  22381. }, timeout);
  22382. };
  22383. this.setInterval(activityCheck, 250);
  22384. }
  22385. /**
  22386. * Gets or sets the current playback rate. A playback rate of
  22387. * 1.0 represents normal speed and 0.5 would indicate half-speed
  22388. * playback, for instance.
  22389. *
  22390. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-playbackrate
  22391. *
  22392. * @param {number} [rate]
  22393. * New playback rate to set.
  22394. *
  22395. * @return {number|undefined}
  22396. * - The current playback rate when getting or 1.0
  22397. * - Nothing when setting
  22398. */
  22399. playbackRate(rate) {
  22400. if (rate !== undefined) {
  22401. // NOTE: this.cache_.lastPlaybackRate is set from the tech handler
  22402. // that is registered above
  22403. this.techCall_('setPlaybackRate', rate);
  22404. return;
  22405. }
  22406. if (this.tech_ && this.tech_.featuresPlaybackRate) {
  22407. return this.cache_.lastPlaybackRate || this.techGet_('playbackRate');
  22408. }
  22409. return 1.0;
  22410. }
  22411. /**
  22412. * Gets or sets the current default playback rate. A default playback rate of
  22413. * 1.0 represents normal speed and 0.5 would indicate half-speed playback, for instance.
  22414. * defaultPlaybackRate will only represent what the initial playbackRate of a video was, not
  22415. * not the current playbackRate.
  22416. *
  22417. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-defaultplaybackrate
  22418. *
  22419. * @param {number} [rate]
  22420. * New default playback rate to set.
  22421. *
  22422. * @return {number|undefined}
  22423. * - The default playback rate when getting or 1.0
  22424. * - Nothing when setting
  22425. */
  22426. defaultPlaybackRate(rate) {
  22427. if (rate !== undefined) {
  22428. return this.techCall_('setDefaultPlaybackRate', rate);
  22429. }
  22430. if (this.tech_ && this.tech_.featuresPlaybackRate) {
  22431. return this.techGet_('defaultPlaybackRate');
  22432. }
  22433. return 1.0;
  22434. }
  22435. /**
  22436. * Gets or sets the audio flag
  22437. *
  22438. * @param {boolean} [bool]
  22439. * - true signals that this is an audio player
  22440. * - false signals that this is not an audio player
  22441. *
  22442. * @return {boolean|undefined}
  22443. * - The current value of isAudio when getting
  22444. * - Nothing when setting
  22445. */
  22446. isAudio(bool) {
  22447. if (bool !== undefined) {
  22448. this.isAudio_ = !!bool;
  22449. return;
  22450. }
  22451. return !!this.isAudio_;
  22452. }
  22453. enableAudioOnlyUI_() {
  22454. // Update styling immediately to show the control bar so we can get its height
  22455. this.addClass('vjs-audio-only-mode');
  22456. const playerChildren = this.children();
  22457. const controlBar = this.getChild('ControlBar');
  22458. const controlBarHeight = controlBar && controlBar.currentHeight();
  22459. // Hide all player components except the control bar. Control bar components
  22460. // needed only for video are hidden with CSS
  22461. playerChildren.forEach(child => {
  22462. if (child === controlBar) {
  22463. return;
  22464. }
  22465. if (child.el_ && !child.hasClass('vjs-hidden')) {
  22466. child.hide();
  22467. this.audioOnlyCache_.hiddenChildren.push(child);
  22468. }
  22469. });
  22470. this.audioOnlyCache_.playerHeight = this.currentHeight();
  22471. // Set the player height the same as the control bar
  22472. this.height(controlBarHeight);
  22473. this.trigger('audioonlymodechange');
  22474. }
  22475. disableAudioOnlyUI_() {
  22476. this.removeClass('vjs-audio-only-mode');
  22477. // Show player components that were previously hidden
  22478. this.audioOnlyCache_.hiddenChildren.forEach(child => child.show());
  22479. // Reset player height
  22480. this.height(this.audioOnlyCache_.playerHeight);
  22481. this.trigger('audioonlymodechange');
  22482. }
  22483. /**
  22484. * Get the current audioOnlyMode state or set audioOnlyMode to true or false.
  22485. *
  22486. * Setting this to `true` will hide all player components except the control bar,
  22487. * as well as control bar components needed only for video.
  22488. *
  22489. * @param {boolean} [value]
  22490. * The value to set audioOnlyMode to.
  22491. *
  22492. * @return {Promise|boolean}
  22493. * A Promise is returned when setting the state, and a boolean when getting
  22494. * the present state
  22495. */
  22496. audioOnlyMode(value) {
  22497. if (typeof value !== 'boolean' || value === this.audioOnlyMode_) {
  22498. return this.audioOnlyMode_;
  22499. }
  22500. this.audioOnlyMode_ = value;
  22501. // Enable Audio Only Mode
  22502. if (value) {
  22503. const exitPromises = [];
  22504. // Fullscreen and PiP are not supported in audioOnlyMode, so exit if we need to.
  22505. if (this.isInPictureInPicture()) {
  22506. exitPromises.push(this.exitPictureInPicture());
  22507. }
  22508. if (this.isFullscreen()) {
  22509. exitPromises.push(this.exitFullscreen());
  22510. }
  22511. if (this.audioPosterMode()) {
  22512. exitPromises.push(this.audioPosterMode(false));
  22513. }
  22514. return Promise.all(exitPromises).then(() => this.enableAudioOnlyUI_());
  22515. }
  22516. // Disable Audio Only Mode
  22517. return Promise.resolve().then(() => this.disableAudioOnlyUI_());
  22518. }
  22519. enablePosterModeUI_() {
  22520. // Hide the video element and show the poster image to enable posterModeUI
  22521. const tech = this.tech_ && this.tech_;
  22522. tech.hide();
  22523. this.addClass('vjs-audio-poster-mode');
  22524. this.trigger('audiopostermodechange');
  22525. }
  22526. disablePosterModeUI_() {
  22527. // Show the video element and hide the poster image to disable posterModeUI
  22528. const tech = this.tech_ && this.tech_;
  22529. tech.show();
  22530. this.removeClass('vjs-audio-poster-mode');
  22531. this.trigger('audiopostermodechange');
  22532. }
  22533. /**
  22534. * Get the current audioPosterMode state or set audioPosterMode to true or false
  22535. *
  22536. * @param {boolean} [value]
  22537. * The value to set audioPosterMode to.
  22538. *
  22539. * @return {Promise|boolean}
  22540. * A Promise is returned when setting the state, and a boolean when getting
  22541. * the present state
  22542. */
  22543. audioPosterMode(value) {
  22544. if (typeof value !== 'boolean' || value === this.audioPosterMode_) {
  22545. return this.audioPosterMode_;
  22546. }
  22547. this.audioPosterMode_ = value;
  22548. if (value) {
  22549. if (this.audioOnlyMode()) {
  22550. const audioOnlyModePromise = this.audioOnlyMode(false);
  22551. return audioOnlyModePromise.then(() => {
  22552. // enable audio poster mode after audio only mode is disabled
  22553. this.enablePosterModeUI_();
  22554. });
  22555. }
  22556. return Promise.resolve().then(() => {
  22557. // enable audio poster mode
  22558. this.enablePosterModeUI_();
  22559. });
  22560. }
  22561. return Promise.resolve().then(() => {
  22562. // disable audio poster mode
  22563. this.disablePosterModeUI_();
  22564. });
  22565. }
  22566. /**
  22567. * A helper method for adding a {@link TextTrack} to our
  22568. * {@link TextTrackList}.
  22569. *
  22570. * In addition to the W3C settings we allow adding additional info through options.
  22571. *
  22572. * @see http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-addtexttrack
  22573. *
  22574. * @param {string} [kind]
  22575. * the kind of TextTrack you are adding
  22576. *
  22577. * @param {string} [label]
  22578. * the label to give the TextTrack label
  22579. *
  22580. * @param {string} [language]
  22581. * the language to set on the TextTrack
  22582. *
  22583. * @return {TextTrack|undefined}
  22584. * the TextTrack that was added or undefined
  22585. * if there is no tech
  22586. */
  22587. addTextTrack(kind, label, language) {
  22588. if (this.tech_) {
  22589. return this.tech_.addTextTrack(kind, label, language);
  22590. }
  22591. }
  22592. /**
  22593. * Create a remote {@link TextTrack} and an {@link HTMLTrackElement}.
  22594. *
  22595. * @param {Object} options
  22596. * Options to pass to {@link HTMLTrackElement} during creation. See
  22597. * {@link HTMLTrackElement} for object properties that you should use.
  22598. *
  22599. * @param {boolean} [manualCleanup=false] if set to true, the TextTrack will not be removed
  22600. * from the TextTrackList and HtmlTrackElementList
  22601. * after a source change
  22602. *
  22603. * @return { import('./tracks/html-track-element').default }
  22604. * the HTMLTrackElement that was created and added
  22605. * to the HtmlTrackElementList and the remote
  22606. * TextTrackList
  22607. *
  22608. */
  22609. addRemoteTextTrack(options, manualCleanup) {
  22610. if (this.tech_) {
  22611. return this.tech_.addRemoteTextTrack(options, manualCleanup);
  22612. }
  22613. }
  22614. /**
  22615. * Remove a remote {@link TextTrack} from the respective
  22616. * {@link TextTrackList} and {@link HtmlTrackElementList}.
  22617. *
  22618. * @param {Object} track
  22619. * Remote {@link TextTrack} to remove
  22620. *
  22621. * @return {undefined}
  22622. * does not return anything
  22623. */
  22624. removeRemoteTextTrack(obj = {}) {
  22625. let {
  22626. track
  22627. } = obj;
  22628. if (!track) {
  22629. track = obj;
  22630. }
  22631. // destructure the input into an object with a track argument, defaulting to arguments[0]
  22632. // default the whole argument to an empty object if nothing was passed in
  22633. if (this.tech_) {
  22634. return this.tech_.removeRemoteTextTrack(track);
  22635. }
  22636. }
  22637. /**
  22638. * Gets available media playback quality metrics as specified by the W3C's Media
  22639. * Playback Quality API.
  22640. *
  22641. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  22642. *
  22643. * @return {Object|undefined}
  22644. * An object with supported media playback quality metrics or undefined if there
  22645. * is no tech or the tech does not support it.
  22646. */
  22647. getVideoPlaybackQuality() {
  22648. return this.techGet_('getVideoPlaybackQuality');
  22649. }
  22650. /**
  22651. * Get video width
  22652. *
  22653. * @return {number}
  22654. * current video width
  22655. */
  22656. videoWidth() {
  22657. return this.tech_ && this.tech_.videoWidth && this.tech_.videoWidth() || 0;
  22658. }
  22659. /**
  22660. * Get video height
  22661. *
  22662. * @return {number}
  22663. * current video height
  22664. */
  22665. videoHeight() {
  22666. return this.tech_ && this.tech_.videoHeight && this.tech_.videoHeight() || 0;
  22667. }
  22668. /**
  22669. * Set or get the player's language code.
  22670. *
  22671. * Changing the language will trigger
  22672. * [languagechange]{@link Player#event:languagechange}
  22673. * which Components can use to update control text.
  22674. * ClickableComponent will update its control text by default on
  22675. * [languagechange]{@link Player#event:languagechange}.
  22676. *
  22677. * @fires Player#languagechange
  22678. *
  22679. * @param {string} [code]
  22680. * the language code to set the player to
  22681. *
  22682. * @return {string|undefined}
  22683. * - The current language code when getting
  22684. * - Nothing when setting
  22685. */
  22686. language(code) {
  22687. if (code === undefined) {
  22688. return this.language_;
  22689. }
  22690. if (this.language_ !== String(code).toLowerCase()) {
  22691. this.language_ = String(code).toLowerCase();
  22692. // during first init, it's possible some things won't be evented
  22693. if (isEvented(this)) {
  22694. /**
  22695. * fires when the player language change
  22696. *
  22697. * @event Player#languagechange
  22698. * @type {Event}
  22699. */
  22700. this.trigger('languagechange');
  22701. }
  22702. }
  22703. }
  22704. /**
  22705. * Get the player's language dictionary
  22706. * Merge every time, because a newly added plugin might call videojs.addLanguage() at any time
  22707. * Languages specified directly in the player options have precedence
  22708. *
  22709. * @return {Array}
  22710. * An array of of supported languages
  22711. */
  22712. languages() {
  22713. return merge(Player.prototype.options_.languages, this.languages_);
  22714. }
  22715. /**
  22716. * returns a JavaScript object representing the current track
  22717. * information. **DOES not return it as JSON**
  22718. *
  22719. * @return {Object}
  22720. * Object representing the current of track info
  22721. */
  22722. toJSON() {
  22723. const options = merge(this.options_);
  22724. const tracks = options.tracks;
  22725. options.tracks = [];
  22726. for (let i = 0; i < tracks.length; i++) {
  22727. let track = tracks[i];
  22728. // deep merge tracks and null out player so no circular references
  22729. track = merge(track);
  22730. track.player = undefined;
  22731. options.tracks[i] = track;
  22732. }
  22733. return options;
  22734. }
  22735. /**
  22736. * Creates a simple modal dialog (an instance of the {@link ModalDialog}
  22737. * component) that immediately overlays the player with arbitrary
  22738. * content and removes itself when closed.
  22739. *
  22740. * @param {string|Function|Element|Array|null} content
  22741. * Same as {@link ModalDialog#content}'s param of the same name.
  22742. * The most straight-forward usage is to provide a string or DOM
  22743. * element.
  22744. *
  22745. * @param {Object} [options]
  22746. * Extra options which will be passed on to the {@link ModalDialog}.
  22747. *
  22748. * @return {ModalDialog}
  22749. * the {@link ModalDialog} that was created
  22750. */
  22751. createModal(content, options) {
  22752. options = options || {};
  22753. options.content = content || '';
  22754. const modal = new ModalDialog(this, options);
  22755. this.addChild(modal);
  22756. modal.on('dispose', () => {
  22757. this.removeChild(modal);
  22758. });
  22759. modal.open();
  22760. return modal;
  22761. }
  22762. /**
  22763. * Change breakpoint classes when the player resizes.
  22764. *
  22765. * @private
  22766. */
  22767. updateCurrentBreakpoint_() {
  22768. if (!this.responsive()) {
  22769. return;
  22770. }
  22771. const currentBreakpoint = this.currentBreakpoint();
  22772. const currentWidth = this.currentWidth();
  22773. for (let i = 0; i < BREAKPOINT_ORDER.length; i++) {
  22774. const candidateBreakpoint = BREAKPOINT_ORDER[i];
  22775. const maxWidth = this.breakpoints_[candidateBreakpoint];
  22776. if (currentWidth <= maxWidth) {
  22777. // The current breakpoint did not change, nothing to do.
  22778. if (currentBreakpoint === candidateBreakpoint) {
  22779. return;
  22780. }
  22781. // Only remove a class if there is a current breakpoint.
  22782. if (currentBreakpoint) {
  22783. this.removeClass(BREAKPOINT_CLASSES[currentBreakpoint]);
  22784. }
  22785. this.addClass(BREAKPOINT_CLASSES[candidateBreakpoint]);
  22786. this.breakpoint_ = candidateBreakpoint;
  22787. break;
  22788. }
  22789. }
  22790. }
  22791. /**
  22792. * Removes the current breakpoint.
  22793. *
  22794. * @private
  22795. */
  22796. removeCurrentBreakpoint_() {
  22797. const className = this.currentBreakpointClass();
  22798. this.breakpoint_ = '';
  22799. if (className) {
  22800. this.removeClass(className);
  22801. }
  22802. }
  22803. /**
  22804. * Get or set breakpoints on the player.
  22805. *
  22806. * Calling this method with an object or `true` will remove any previous
  22807. * custom breakpoints and start from the defaults again.
  22808. *
  22809. * @param {Object|boolean} [breakpoints]
  22810. * If an object is given, it can be used to provide custom
  22811. * breakpoints. If `true` is given, will set default breakpoints.
  22812. * If this argument is not given, will simply return the current
  22813. * breakpoints.
  22814. *
  22815. * @param {number} [breakpoints.tiny]
  22816. * The maximum width for the "vjs-layout-tiny" class.
  22817. *
  22818. * @param {number} [breakpoints.xsmall]
  22819. * The maximum width for the "vjs-layout-x-small" class.
  22820. *
  22821. * @param {number} [breakpoints.small]
  22822. * The maximum width for the "vjs-layout-small" class.
  22823. *
  22824. * @param {number} [breakpoints.medium]
  22825. * The maximum width for the "vjs-layout-medium" class.
  22826. *
  22827. * @param {number} [breakpoints.large]
  22828. * The maximum width for the "vjs-layout-large" class.
  22829. *
  22830. * @param {number} [breakpoints.xlarge]
  22831. * The maximum width for the "vjs-layout-x-large" class.
  22832. *
  22833. * @param {number} [breakpoints.huge]
  22834. * The maximum width for the "vjs-layout-huge" class.
  22835. *
  22836. * @return {Object}
  22837. * An object mapping breakpoint names to maximum width values.
  22838. */
  22839. breakpoints(breakpoints) {
  22840. // Used as a getter.
  22841. if (breakpoints === undefined) {
  22842. return Object.assign(this.breakpoints_);
  22843. }
  22844. this.breakpoint_ = '';
  22845. this.breakpoints_ = Object.assign({}, DEFAULT_BREAKPOINTS, breakpoints);
  22846. // When breakpoint definitions change, we need to update the currently
  22847. // selected breakpoint.
  22848. this.updateCurrentBreakpoint_();
  22849. // Clone the breakpoints before returning.
  22850. return Object.assign(this.breakpoints_);
  22851. }
  22852. /**
  22853. * Get or set a flag indicating whether or not this player should adjust
  22854. * its UI based on its dimensions.
  22855. *
  22856. * @param {boolean} [value]
  22857. * Should be `true` if the player should adjust its UI based on its
  22858. * dimensions; otherwise, should be `false`.
  22859. *
  22860. * @return {boolean|undefined}
  22861. * Will be `true` if this player should adjust its UI based on its
  22862. * dimensions; otherwise, will be `false`.
  22863. * Nothing if setting
  22864. */
  22865. responsive(value) {
  22866. // Used as a getter.
  22867. if (value === undefined) {
  22868. return this.responsive_;
  22869. }
  22870. value = Boolean(value);
  22871. const current = this.responsive_;
  22872. // Nothing changed.
  22873. if (value === current) {
  22874. return;
  22875. }
  22876. // The value actually changed, set it.
  22877. this.responsive_ = value;
  22878. // Start listening for breakpoints and set the initial breakpoint if the
  22879. // player is now responsive.
  22880. if (value) {
  22881. this.on('playerresize', this.boundUpdateCurrentBreakpoint_);
  22882. this.updateCurrentBreakpoint_();
  22883. // Stop listening for breakpoints if the player is no longer responsive.
  22884. } else {
  22885. this.off('playerresize', this.boundUpdateCurrentBreakpoint_);
  22886. this.removeCurrentBreakpoint_();
  22887. }
  22888. return value;
  22889. }
  22890. /**
  22891. * Get current breakpoint name, if any.
  22892. *
  22893. * @return {string}
  22894. * If there is currently a breakpoint set, returns a the key from the
  22895. * breakpoints object matching it. Otherwise, returns an empty string.
  22896. */
  22897. currentBreakpoint() {
  22898. return this.breakpoint_;
  22899. }
  22900. /**
  22901. * Get the current breakpoint class name.
  22902. *
  22903. * @return {string}
  22904. * The matching class name (e.g. `"vjs-layout-tiny"` or
  22905. * `"vjs-layout-large"`) for the current breakpoint. Empty string if
  22906. * there is no current breakpoint.
  22907. */
  22908. currentBreakpointClass() {
  22909. return BREAKPOINT_CLASSES[this.breakpoint_] || '';
  22910. }
  22911. /**
  22912. * An object that describes a single piece of media.
  22913. *
  22914. * Properties that are not part of this type description will be retained; so,
  22915. * this can be viewed as a generic metadata storage mechanism as well.
  22916. *
  22917. * @see {@link https://wicg.github.io/mediasession/#the-mediametadata-interface}
  22918. * @typedef {Object} Player~MediaObject
  22919. *
  22920. * @property {string} [album]
  22921. * Unused, except if this object is passed to the `MediaSession`
  22922. * API.
  22923. *
  22924. * @property {string} [artist]
  22925. * Unused, except if this object is passed to the `MediaSession`
  22926. * API.
  22927. *
  22928. * @property {Object[]} [artwork]
  22929. * Unused, except if this object is passed to the `MediaSession`
  22930. * API. If not specified, will be populated via the `poster`, if
  22931. * available.
  22932. *
  22933. * @property {string} [poster]
  22934. * URL to an image that will display before playback.
  22935. *
  22936. * @property {Tech~SourceObject|Tech~SourceObject[]|string} [src]
  22937. * A single source object, an array of source objects, or a string
  22938. * referencing a URL to a media source. It is _highly recommended_
  22939. * that an object or array of objects is used here, so that source
  22940. * selection algorithms can take the `type` into account.
  22941. *
  22942. * @property {string} [title]
  22943. * Unused, except if this object is passed to the `MediaSession`
  22944. * API.
  22945. *
  22946. * @property {Object[]} [textTracks]
  22947. * An array of objects to be used to create text tracks, following
  22948. * the {@link https://www.w3.org/TR/html50/embedded-content-0.html#the-track-element|native track element format}.
  22949. * For ease of removal, these will be created as "remote" text
  22950. * tracks and set to automatically clean up on source changes.
  22951. *
  22952. * These objects may have properties like `src`, `kind`, `label`,
  22953. * and `language`, see {@link Tech#createRemoteTextTrack}.
  22954. */
  22955. /**
  22956. * Populate the player using a {@link Player~MediaObject|MediaObject}.
  22957. *
  22958. * @param {Player~MediaObject} media
  22959. * A media object.
  22960. *
  22961. * @param {Function} ready
  22962. * A callback to be called when the player is ready.
  22963. */
  22964. loadMedia(media, ready) {
  22965. if (!media || typeof media !== 'object') {
  22966. return;
  22967. }
  22968. const crossOrigin = this.crossOrigin();
  22969. this.reset();
  22970. // Clone the media object so it cannot be mutated from outside.
  22971. this.cache_.media = merge(media);
  22972. const {
  22973. artist,
  22974. artwork,
  22975. description,
  22976. poster,
  22977. src,
  22978. textTracks,
  22979. title
  22980. } = this.cache_.media;
  22981. // If `artwork` is not given, create it using `poster`.
  22982. if (!artwork && poster) {
  22983. this.cache_.media.artwork = [{
  22984. src: poster,
  22985. type: getMimetype(poster)
  22986. }];
  22987. }
  22988. if (crossOrigin) {
  22989. this.crossOrigin(crossOrigin);
  22990. }
  22991. if (src) {
  22992. this.src(src);
  22993. }
  22994. if (poster) {
  22995. this.poster(poster);
  22996. }
  22997. if (Array.isArray(textTracks)) {
  22998. textTracks.forEach(tt => this.addRemoteTextTrack(tt, false));
  22999. }
  23000. if (this.titleBar) {
  23001. this.titleBar.update({
  23002. title,
  23003. description: description || artist || ''
  23004. });
  23005. }
  23006. this.ready(ready);
  23007. }
  23008. /**
  23009. * Get a clone of the current {@link Player~MediaObject} for this player.
  23010. *
  23011. * If the `loadMedia` method has not been used, will attempt to return a
  23012. * {@link Player~MediaObject} based on the current state of the player.
  23013. *
  23014. * @return {Player~MediaObject}
  23015. */
  23016. getMedia() {
  23017. if (!this.cache_.media) {
  23018. const poster = this.poster();
  23019. const src = this.currentSources();
  23020. const textTracks = Array.prototype.map.call(this.remoteTextTracks(), tt => ({
  23021. kind: tt.kind,
  23022. label: tt.label,
  23023. language: tt.language,
  23024. src: tt.src
  23025. }));
  23026. const media = {
  23027. src,
  23028. textTracks
  23029. };
  23030. if (poster) {
  23031. media.poster = poster;
  23032. media.artwork = [{
  23033. src: media.poster,
  23034. type: getMimetype(media.poster)
  23035. }];
  23036. }
  23037. return media;
  23038. }
  23039. return merge(this.cache_.media);
  23040. }
  23041. /**
  23042. * Gets tag settings
  23043. *
  23044. * @param {Element} tag
  23045. * The player tag
  23046. *
  23047. * @return {Object}
  23048. * An object containing all of the settings
  23049. * for a player tag
  23050. */
  23051. static getTagSettings(tag) {
  23052. const baseOptions = {
  23053. sources: [],
  23054. tracks: []
  23055. };
  23056. const tagOptions = getAttributes(tag);
  23057. const dataSetup = tagOptions['data-setup'];
  23058. if (hasClass(tag, 'vjs-fill')) {
  23059. tagOptions.fill = true;
  23060. }
  23061. if (hasClass(tag, 'vjs-fluid')) {
  23062. tagOptions.fluid = true;
  23063. }
  23064. // Check if data-setup attr exists.
  23065. if (dataSetup !== null) {
  23066. // Parse options JSON
  23067. // If empty string, make it a parsable json object.
  23068. const [err, data] = safeParseTuple(dataSetup || '{}');
  23069. if (err) {
  23070. log.error(err);
  23071. }
  23072. Object.assign(tagOptions, data);
  23073. }
  23074. Object.assign(baseOptions, tagOptions);
  23075. // Get tag children settings
  23076. if (tag.hasChildNodes()) {
  23077. const children = tag.childNodes;
  23078. for (let i = 0, j = children.length; i < j; i++) {
  23079. const child = children[i];
  23080. // Change case needed: http://ejohn.org/blog/nodename-case-sensitivity/
  23081. const childName = child.nodeName.toLowerCase();
  23082. if (childName === 'source') {
  23083. baseOptions.sources.push(getAttributes(child));
  23084. } else if (childName === 'track') {
  23085. baseOptions.tracks.push(getAttributes(child));
  23086. }
  23087. }
  23088. }
  23089. return baseOptions;
  23090. }
  23091. /**
  23092. * Set debug mode to enable/disable logs at info level.
  23093. *
  23094. * @param {boolean} enabled
  23095. * @fires Player#debugon
  23096. * @fires Player#debugoff
  23097. * @return {boolean|undefined}
  23098. */
  23099. debug(enabled) {
  23100. if (enabled === undefined) {
  23101. return this.debugEnabled_;
  23102. }
  23103. if (enabled) {
  23104. this.trigger('debugon');
  23105. this.previousLogLevel_ = this.log.level;
  23106. this.log.level('debug');
  23107. this.debugEnabled_ = true;
  23108. } else {
  23109. this.trigger('debugoff');
  23110. this.log.level(this.previousLogLevel_);
  23111. this.previousLogLevel_ = undefined;
  23112. this.debugEnabled_ = false;
  23113. }
  23114. }
  23115. /**
  23116. * Set or get current playback rates.
  23117. * Takes an array and updates the playback rates menu with the new items.
  23118. * Pass in an empty array to hide the menu.
  23119. * Values other than arrays are ignored.
  23120. *
  23121. * @fires Player#playbackrateschange
  23122. * @param {number[]} newRates
  23123. * The new rates that the playback rates menu should update to.
  23124. * An empty array will hide the menu
  23125. * @return {number[]} When used as a getter will return the current playback rates
  23126. */
  23127. playbackRates(newRates) {
  23128. if (newRates === undefined) {
  23129. return this.cache_.playbackRates;
  23130. }
  23131. // ignore any value that isn't an array
  23132. if (!Array.isArray(newRates)) {
  23133. return;
  23134. }
  23135. // ignore any arrays that don't only contain numbers
  23136. if (!newRates.every(rate => typeof rate === 'number')) {
  23137. return;
  23138. }
  23139. this.cache_.playbackRates = newRates;
  23140. /**
  23141. * fires when the playback rates in a player are changed
  23142. *
  23143. * @event Player#playbackrateschange
  23144. * @type {Event}
  23145. */
  23146. this.trigger('playbackrateschange');
  23147. }
  23148. }
  23149. /**
  23150. * Get the {@link VideoTrackList}
  23151. *
  23152. * @link https://html.spec.whatwg.org/multipage/embedded-content.html#videotracklist
  23153. *
  23154. * @return {VideoTrackList}
  23155. * the current video track list
  23156. *
  23157. * @method Player.prototype.videoTracks
  23158. */
  23159. /**
  23160. * Get the {@link AudioTrackList}
  23161. *
  23162. * @link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotracklist
  23163. *
  23164. * @return {AudioTrackList}
  23165. * the current audio track list
  23166. *
  23167. * @method Player.prototype.audioTracks
  23168. */
  23169. /**
  23170. * Get the {@link TextTrackList}
  23171. *
  23172. * @link http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-texttracks
  23173. *
  23174. * @return {TextTrackList}
  23175. * the current text track list
  23176. *
  23177. * @method Player.prototype.textTracks
  23178. */
  23179. /**
  23180. * Get the remote {@link TextTrackList}
  23181. *
  23182. * @return {TextTrackList}
  23183. * The current remote text track list
  23184. *
  23185. * @method Player.prototype.remoteTextTracks
  23186. */
  23187. /**
  23188. * Get the remote {@link HtmlTrackElementList} tracks.
  23189. *
  23190. * @return {HtmlTrackElementList}
  23191. * The current remote text track element list
  23192. *
  23193. * @method Player.prototype.remoteTextTrackEls
  23194. */
  23195. ALL.names.forEach(function (name) {
  23196. const props = ALL[name];
  23197. Player.prototype[props.getterName] = function () {
  23198. if (this.tech_) {
  23199. return this.tech_[props.getterName]();
  23200. }
  23201. // if we have not yet loadTech_, we create {video,audio,text}Tracks_
  23202. // these will be passed to the tech during loading
  23203. this[props.privateName] = this[props.privateName] || new props.ListClass();
  23204. return this[props.privateName];
  23205. };
  23206. });
  23207. /**
  23208. * Get or set the `Player`'s crossorigin option. For the HTML5 player, this
  23209. * sets the `crossOrigin` property on the `<video>` tag to control the CORS
  23210. * behavior.
  23211. *
  23212. * @see [Video Element Attributes]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin}
  23213. *
  23214. * @param {string} [value]
  23215. * The value to set the `Player`'s crossorigin to. If an argument is
  23216. * given, must be one of `anonymous` or `use-credentials`.
  23217. *
  23218. * @return {string|undefined}
  23219. * - The current crossorigin value of the `Player` when getting.
  23220. * - undefined when setting
  23221. */
  23222. Player.prototype.crossorigin = Player.prototype.crossOrigin;
  23223. /**
  23224. * Global enumeration of players.
  23225. *
  23226. * The keys are the player IDs and the values are either the {@link Player}
  23227. * instance or `null` for disposed players.
  23228. *
  23229. * @type {Object}
  23230. */
  23231. Player.players = {};
  23232. const navigator = window.navigator;
  23233. /*
  23234. * Player instance options, surfaced using options
  23235. * options = Player.prototype.options_
  23236. * Make changes in options, not here.
  23237. *
  23238. * @type {Object}
  23239. * @private
  23240. */
  23241. Player.prototype.options_ = {
  23242. // Default order of fallback technology
  23243. techOrder: Tech.defaultTechOrder_,
  23244. html5: {},
  23245. // enable sourceset by default
  23246. enableSourceset: true,
  23247. // default inactivity timeout
  23248. inactivityTimeout: 2000,
  23249. // default playback rates
  23250. playbackRates: [],
  23251. // Add playback rate selection by adding rates
  23252. // 'playbackRates': [0.5, 1, 1.5, 2],
  23253. liveui: false,
  23254. // Included control sets
  23255. children: ['mediaLoader', 'posterImage', 'titleBar', 'textTrackDisplay', 'loadingSpinner', 'bigPlayButton', 'liveTracker', 'controlBar', 'errorDisplay', 'textTrackSettings', 'resizeManager'],
  23256. language: navigator && (navigator.languages && navigator.languages[0] || navigator.userLanguage || navigator.language) || 'en',
  23257. // locales and their language translations
  23258. languages: {},
  23259. // Default message to show when a video cannot be played.
  23260. notSupportedMessage: 'No compatible source was found for this media.',
  23261. normalizeAutoplay: false,
  23262. fullscreen: {
  23263. options: {
  23264. navigationUI: 'hide'
  23265. }
  23266. },
  23267. breakpoints: {},
  23268. responsive: false,
  23269. audioOnlyMode: false,
  23270. audioPosterMode: false,
  23271. // Default smooth seeking to false
  23272. enableSmoothSeeking: false
  23273. };
  23274. TECH_EVENTS_RETRIGGER.forEach(function (event) {
  23275. Player.prototype[`handleTech${toTitleCase(event)}_`] = function () {
  23276. return this.trigger(event);
  23277. };
  23278. });
  23279. /**
  23280. * Fired when the player has initial duration and dimension information
  23281. *
  23282. * @event Player#loadedmetadata
  23283. * @type {Event}
  23284. */
  23285. /**
  23286. * Fired when the player has downloaded data at the current playback position
  23287. *
  23288. * @event Player#loadeddata
  23289. * @type {Event}
  23290. */
  23291. /**
  23292. * Fired when the current playback position has changed *
  23293. * During playback this is fired every 15-250 milliseconds, depending on the
  23294. * playback technology in use.
  23295. *
  23296. * @event Player#timeupdate
  23297. * @type {Event}
  23298. */
  23299. /**
  23300. * Fired when the volume changes
  23301. *
  23302. * @event Player#volumechange
  23303. * @type {Event}
  23304. */
  23305. /**
  23306. * Reports whether or not a player has a plugin available.
  23307. *
  23308. * This does not report whether or not the plugin has ever been initialized
  23309. * on this player. For that, [usingPlugin]{@link Player#usingPlugin}.
  23310. *
  23311. * @method Player#hasPlugin
  23312. * @param {string} name
  23313. * The name of a plugin.
  23314. *
  23315. * @return {boolean}
  23316. * Whether or not this player has the requested plugin available.
  23317. */
  23318. /**
  23319. * Reports whether or not a player is using a plugin by name.
  23320. *
  23321. * For basic plugins, this only reports whether the plugin has _ever_ been
  23322. * initialized on this player.
  23323. *
  23324. * @method Player#usingPlugin
  23325. * @param {string} name
  23326. * The name of a plugin.
  23327. *
  23328. * @return {boolean}
  23329. * Whether or not this player is using the requested plugin.
  23330. */
  23331. Component.registerComponent('Player', Player);
  23332. /**
  23333. * @file plugin.js
  23334. */
  23335. /**
  23336. * The base plugin name.
  23337. *
  23338. * @private
  23339. * @constant
  23340. * @type {string}
  23341. */
  23342. const BASE_PLUGIN_NAME = 'plugin';
  23343. /**
  23344. * The key on which a player's active plugins cache is stored.
  23345. *
  23346. * @private
  23347. * @constant
  23348. * @type {string}
  23349. */
  23350. const PLUGIN_CACHE_KEY = 'activePlugins_';
  23351. /**
  23352. * Stores registered plugins in a private space.
  23353. *
  23354. * @private
  23355. * @type {Object}
  23356. */
  23357. const pluginStorage = {};
  23358. /**
  23359. * Reports whether or not a plugin has been registered.
  23360. *
  23361. * @private
  23362. * @param {string} name
  23363. * The name of a plugin.
  23364. *
  23365. * @return {boolean}
  23366. * Whether or not the plugin has been registered.
  23367. */
  23368. const pluginExists = name => pluginStorage.hasOwnProperty(name);
  23369. /**
  23370. * Get a single registered plugin by name.
  23371. *
  23372. * @private
  23373. * @param {string} name
  23374. * The name of a plugin.
  23375. *
  23376. * @return {typeof Plugin|Function|undefined}
  23377. * The plugin (or undefined).
  23378. */
  23379. const getPlugin = name => pluginExists(name) ? pluginStorage[name] : undefined;
  23380. /**
  23381. * Marks a plugin as "active" on a player.
  23382. *
  23383. * Also, ensures that the player has an object for tracking active plugins.
  23384. *
  23385. * @private
  23386. * @param {Player} player
  23387. * A Video.js player instance.
  23388. *
  23389. * @param {string} name
  23390. * The name of a plugin.
  23391. */
  23392. const markPluginAsActive = (player, name) => {
  23393. player[PLUGIN_CACHE_KEY] = player[PLUGIN_CACHE_KEY] || {};
  23394. player[PLUGIN_CACHE_KEY][name] = true;
  23395. };
  23396. /**
  23397. * Triggers a pair of plugin setup events.
  23398. *
  23399. * @private
  23400. * @param {Player} player
  23401. * A Video.js player instance.
  23402. *
  23403. * @param {PluginEventHash} hash
  23404. * A plugin event hash.
  23405. *
  23406. * @param {boolean} [before]
  23407. * If true, prefixes the event name with "before". In other words,
  23408. * use this to trigger "beforepluginsetup" instead of "pluginsetup".
  23409. */
  23410. const triggerSetupEvent = (player, hash, before) => {
  23411. const eventName = (before ? 'before' : '') + 'pluginsetup';
  23412. player.trigger(eventName, hash);
  23413. player.trigger(eventName + ':' + hash.name, hash);
  23414. };
  23415. /**
  23416. * Takes a basic plugin function and returns a wrapper function which marks
  23417. * on the player that the plugin has been activated.
  23418. *
  23419. * @private
  23420. * @param {string} name
  23421. * The name of the plugin.
  23422. *
  23423. * @param {Function} plugin
  23424. * The basic plugin.
  23425. *
  23426. * @return {Function}
  23427. * A wrapper function for the given plugin.
  23428. */
  23429. const createBasicPlugin = function (name, plugin) {
  23430. const basicPluginWrapper = function () {
  23431. // We trigger the "beforepluginsetup" and "pluginsetup" events on the player
  23432. // regardless, but we want the hash to be consistent with the hash provided
  23433. // for advanced plugins.
  23434. //
  23435. // The only potentially counter-intuitive thing here is the `instance` in
  23436. // the "pluginsetup" event is the value returned by the `plugin` function.
  23437. triggerSetupEvent(this, {
  23438. name,
  23439. plugin,
  23440. instance: null
  23441. }, true);
  23442. const instance = plugin.apply(this, arguments);
  23443. markPluginAsActive(this, name);
  23444. triggerSetupEvent(this, {
  23445. name,
  23446. plugin,
  23447. instance
  23448. });
  23449. return instance;
  23450. };
  23451. Object.keys(plugin).forEach(function (prop) {
  23452. basicPluginWrapper[prop] = plugin[prop];
  23453. });
  23454. return basicPluginWrapper;
  23455. };
  23456. /**
  23457. * Takes a plugin sub-class and returns a factory function for generating
  23458. * instances of it.
  23459. *
  23460. * This factory function will replace itself with an instance of the requested
  23461. * sub-class of Plugin.
  23462. *
  23463. * @private
  23464. * @param {string} name
  23465. * The name of the plugin.
  23466. *
  23467. * @param {Plugin} PluginSubClass
  23468. * The advanced plugin.
  23469. *
  23470. * @return {Function}
  23471. */
  23472. const createPluginFactory = (name, PluginSubClass) => {
  23473. // Add a `name` property to the plugin prototype so that each plugin can
  23474. // refer to itself by name.
  23475. PluginSubClass.prototype.name = name;
  23476. return function (...args) {
  23477. triggerSetupEvent(this, {
  23478. name,
  23479. plugin: PluginSubClass,
  23480. instance: null
  23481. }, true);
  23482. const instance = new PluginSubClass(...[this, ...args]);
  23483. // The plugin is replaced by a function that returns the current instance.
  23484. this[name] = () => instance;
  23485. triggerSetupEvent(this, instance.getEventHash());
  23486. return instance;
  23487. };
  23488. };
  23489. /**
  23490. * Parent class for all advanced plugins.
  23491. *
  23492. * @mixes module:evented~EventedMixin
  23493. * @mixes module:stateful~StatefulMixin
  23494. * @fires Player#beforepluginsetup
  23495. * @fires Player#beforepluginsetup:$name
  23496. * @fires Player#pluginsetup
  23497. * @fires Player#pluginsetup:$name
  23498. * @listens Player#dispose
  23499. * @throws {Error}
  23500. * If attempting to instantiate the base {@link Plugin} class
  23501. * directly instead of via a sub-class.
  23502. */
  23503. class Plugin {
  23504. /**
  23505. * Creates an instance of this class.
  23506. *
  23507. * Sub-classes should call `super` to ensure plugins are properly initialized.
  23508. *
  23509. * @param {Player} player
  23510. * A Video.js player instance.
  23511. */
  23512. constructor(player) {
  23513. if (this.constructor === Plugin) {
  23514. throw new Error('Plugin must be sub-classed; not directly instantiated.');
  23515. }
  23516. this.player = player;
  23517. if (!this.log) {
  23518. this.log = this.player.log.createLogger(this.name);
  23519. }
  23520. // Make this object evented, but remove the added `trigger` method so we
  23521. // use the prototype version instead.
  23522. evented(this);
  23523. delete this.trigger;
  23524. stateful(this, this.constructor.defaultState);
  23525. markPluginAsActive(player, this.name);
  23526. // Auto-bind the dispose method so we can use it as a listener and unbind
  23527. // it later easily.
  23528. this.dispose = this.dispose.bind(this);
  23529. // If the player is disposed, dispose the plugin.
  23530. player.on('dispose', this.dispose);
  23531. }
  23532. /**
  23533. * Get the version of the plugin that was set on <pluginName>.VERSION
  23534. */
  23535. version() {
  23536. return this.constructor.VERSION;
  23537. }
  23538. /**
  23539. * Each event triggered by plugins includes a hash of additional data with
  23540. * conventional properties.
  23541. *
  23542. * This returns that object or mutates an existing hash.
  23543. *
  23544. * @param {Object} [hash={}]
  23545. * An object to be used as event an event hash.
  23546. *
  23547. * @return {PluginEventHash}
  23548. * An event hash object with provided properties mixed-in.
  23549. */
  23550. getEventHash(hash = {}) {
  23551. hash.name = this.name;
  23552. hash.plugin = this.constructor;
  23553. hash.instance = this;
  23554. return hash;
  23555. }
  23556. /**
  23557. * Triggers an event on the plugin object and overrides
  23558. * {@link module:evented~EventedMixin.trigger|EventedMixin.trigger}.
  23559. *
  23560. * @param {string|Object} event
  23561. * An event type or an object with a type property.
  23562. *
  23563. * @param {Object} [hash={}]
  23564. * Additional data hash to merge with a
  23565. * {@link PluginEventHash|PluginEventHash}.
  23566. *
  23567. * @return {boolean}
  23568. * Whether or not default was prevented.
  23569. */
  23570. trigger(event, hash = {}) {
  23571. return trigger(this.eventBusEl_, event, this.getEventHash(hash));
  23572. }
  23573. /**
  23574. * Handles "statechanged" events on the plugin. No-op by default, override by
  23575. * subclassing.
  23576. *
  23577. * @abstract
  23578. * @param {Event} e
  23579. * An event object provided by a "statechanged" event.
  23580. *
  23581. * @param {Object} e.changes
  23582. * An object describing changes that occurred with the "statechanged"
  23583. * event.
  23584. */
  23585. handleStateChanged(e) {}
  23586. /**
  23587. * Disposes a plugin.
  23588. *
  23589. * Subclasses can override this if they want, but for the sake of safety,
  23590. * it's probably best to subscribe the "dispose" event.
  23591. *
  23592. * @fires Plugin#dispose
  23593. */
  23594. dispose() {
  23595. const {
  23596. name,
  23597. player
  23598. } = this;
  23599. /**
  23600. * Signals that a advanced plugin is about to be disposed.
  23601. *
  23602. * @event Plugin#dispose
  23603. * @type {Event}
  23604. */
  23605. this.trigger('dispose');
  23606. this.off();
  23607. player.off('dispose', this.dispose);
  23608. // Eliminate any possible sources of leaking memory by clearing up
  23609. // references between the player and the plugin instance and nulling out
  23610. // the plugin's state and replacing methods with a function that throws.
  23611. player[PLUGIN_CACHE_KEY][name] = false;
  23612. this.player = this.state = null;
  23613. // Finally, replace the plugin name on the player with a new factory
  23614. // function, so that the plugin is ready to be set up again.
  23615. player[name] = createPluginFactory(name, pluginStorage[name]);
  23616. }
  23617. /**
  23618. * Determines if a plugin is a basic plugin (i.e. not a sub-class of `Plugin`).
  23619. *
  23620. * @param {string|Function} plugin
  23621. * If a string, matches the name of a plugin. If a function, will be
  23622. * tested directly.
  23623. *
  23624. * @return {boolean}
  23625. * Whether or not a plugin is a basic plugin.
  23626. */
  23627. static isBasic(plugin) {
  23628. const p = typeof plugin === 'string' ? getPlugin(plugin) : plugin;
  23629. return typeof p === 'function' && !Plugin.prototype.isPrototypeOf(p.prototype);
  23630. }
  23631. /**
  23632. * Register a Video.js plugin.
  23633. *
  23634. * @param {string} name
  23635. * The name of the plugin to be registered. Must be a string and
  23636. * must not match an existing plugin or a method on the `Player`
  23637. * prototype.
  23638. *
  23639. * @param {typeof Plugin|Function} plugin
  23640. * A sub-class of `Plugin` or a function for basic plugins.
  23641. *
  23642. * @return {typeof Plugin|Function}
  23643. * For advanced plugins, a factory function for that plugin. For
  23644. * basic plugins, a wrapper function that initializes the plugin.
  23645. */
  23646. static registerPlugin(name, plugin) {
  23647. if (typeof name !== 'string') {
  23648. throw new Error(`Illegal plugin name, "${name}", must be a string, was ${typeof name}.`);
  23649. }
  23650. if (pluginExists(name)) {
  23651. log.warn(`A plugin named "${name}" already exists. You may want to avoid re-registering plugins!`);
  23652. } else if (Player.prototype.hasOwnProperty(name)) {
  23653. throw new Error(`Illegal plugin name, "${name}", cannot share a name with an existing player method!`);
  23654. }
  23655. if (typeof plugin !== 'function') {
  23656. throw new Error(`Illegal plugin for "${name}", must be a function, was ${typeof plugin}.`);
  23657. }
  23658. pluginStorage[name] = plugin;
  23659. // Add a player prototype method for all sub-classed plugins (but not for
  23660. // the base Plugin class).
  23661. if (name !== BASE_PLUGIN_NAME) {
  23662. if (Plugin.isBasic(plugin)) {
  23663. Player.prototype[name] = createBasicPlugin(name, plugin);
  23664. } else {
  23665. Player.prototype[name] = createPluginFactory(name, plugin);
  23666. }
  23667. }
  23668. return plugin;
  23669. }
  23670. /**
  23671. * De-register a Video.js plugin.
  23672. *
  23673. * @param {string} name
  23674. * The name of the plugin to be de-registered. Must be a string that
  23675. * matches an existing plugin.
  23676. *
  23677. * @throws {Error}
  23678. * If an attempt is made to de-register the base plugin.
  23679. */
  23680. static deregisterPlugin(name) {
  23681. if (name === BASE_PLUGIN_NAME) {
  23682. throw new Error('Cannot de-register base plugin.');
  23683. }
  23684. if (pluginExists(name)) {
  23685. delete pluginStorage[name];
  23686. delete Player.prototype[name];
  23687. }
  23688. }
  23689. /**
  23690. * Gets an object containing multiple Video.js plugins.
  23691. *
  23692. * @param {Array} [names]
  23693. * If provided, should be an array of plugin names. Defaults to _all_
  23694. * plugin names.
  23695. *
  23696. * @return {Object|undefined}
  23697. * An object containing plugin(s) associated with their name(s) or
  23698. * `undefined` if no matching plugins exist).
  23699. */
  23700. static getPlugins(names = Object.keys(pluginStorage)) {
  23701. let result;
  23702. names.forEach(name => {
  23703. const plugin = getPlugin(name);
  23704. if (plugin) {
  23705. result = result || {};
  23706. result[name] = plugin;
  23707. }
  23708. });
  23709. return result;
  23710. }
  23711. /**
  23712. * Gets a plugin's version, if available
  23713. *
  23714. * @param {string} name
  23715. * The name of a plugin.
  23716. *
  23717. * @return {string}
  23718. * The plugin's version or an empty string.
  23719. */
  23720. static getPluginVersion(name) {
  23721. const plugin = getPlugin(name);
  23722. return plugin && plugin.VERSION || '';
  23723. }
  23724. }
  23725. /**
  23726. * Gets a plugin by name if it exists.
  23727. *
  23728. * @static
  23729. * @method getPlugin
  23730. * @memberOf Plugin
  23731. * @param {string} name
  23732. * The name of a plugin.
  23733. *
  23734. * @returns {typeof Plugin|Function|undefined}
  23735. * The plugin (or `undefined`).
  23736. */
  23737. Plugin.getPlugin = getPlugin;
  23738. /**
  23739. * The name of the base plugin class as it is registered.
  23740. *
  23741. * @type {string}
  23742. */
  23743. Plugin.BASE_PLUGIN_NAME = BASE_PLUGIN_NAME;
  23744. Plugin.registerPlugin(BASE_PLUGIN_NAME, Plugin);
  23745. /**
  23746. * Documented in player.js
  23747. *
  23748. * @ignore
  23749. */
  23750. Player.prototype.usingPlugin = function (name) {
  23751. return !!this[PLUGIN_CACHE_KEY] && this[PLUGIN_CACHE_KEY][name] === true;
  23752. };
  23753. /**
  23754. * Documented in player.js
  23755. *
  23756. * @ignore
  23757. */
  23758. Player.prototype.hasPlugin = function (name) {
  23759. return !!pluginExists(name);
  23760. };
  23761. /**
  23762. * Signals that a plugin is about to be set up on a player.
  23763. *
  23764. * @event Player#beforepluginsetup
  23765. * @type {PluginEventHash}
  23766. */
  23767. /**
  23768. * Signals that a plugin is about to be set up on a player - by name. The name
  23769. * is the name of the plugin.
  23770. *
  23771. * @event Player#beforepluginsetup:$name
  23772. * @type {PluginEventHash}
  23773. */
  23774. /**
  23775. * Signals that a plugin has just been set up on a player.
  23776. *
  23777. * @event Player#pluginsetup
  23778. * @type {PluginEventHash}
  23779. */
  23780. /**
  23781. * Signals that a plugin has just been set up on a player - by name. The name
  23782. * is the name of the plugin.
  23783. *
  23784. * @event Player#pluginsetup:$name
  23785. * @type {PluginEventHash}
  23786. */
  23787. /**
  23788. * @typedef {Object} PluginEventHash
  23789. *
  23790. * @property {string} instance
  23791. * For basic plugins, the return value of the plugin function. For
  23792. * advanced plugins, the plugin instance on which the event is fired.
  23793. *
  23794. * @property {string} name
  23795. * The name of the plugin.
  23796. *
  23797. * @property {string} plugin
  23798. * For basic plugins, the plugin function. For advanced plugins, the
  23799. * plugin class/constructor.
  23800. */
  23801. /**
  23802. * @file deprecate.js
  23803. * @module deprecate
  23804. */
  23805. /**
  23806. * Decorate a function with a deprecation message the first time it is called.
  23807. *
  23808. * @param {string} message
  23809. * A deprecation message to log the first time the returned function
  23810. * is called.
  23811. *
  23812. * @param {Function} fn
  23813. * The function to be deprecated.
  23814. *
  23815. * @return {Function}
  23816. * A wrapper function that will log a deprecation warning the first
  23817. * time it is called. The return value will be the return value of
  23818. * the wrapped function.
  23819. */
  23820. function deprecate(message, fn) {
  23821. let warned = false;
  23822. return function (...args) {
  23823. if (!warned) {
  23824. log.warn(message);
  23825. }
  23826. warned = true;
  23827. return fn.apply(this, args);
  23828. };
  23829. }
  23830. /**
  23831. * Internal function used to mark a function as deprecated in the next major
  23832. * version with consistent messaging.
  23833. *
  23834. * @param {number} major The major version where it will be removed
  23835. * @param {string} oldName The old function name
  23836. * @param {string} newName The new function name
  23837. * @param {Function} fn The function to deprecate
  23838. * @return {Function} The decorated function
  23839. */
  23840. function deprecateForMajor(major, oldName, newName, fn) {
  23841. return deprecate(`${oldName} is deprecated and will be removed in ${major}.0; please use ${newName} instead.`, fn);
  23842. }
  23843. /**
  23844. * @file video.js
  23845. * @module videojs
  23846. */
  23847. /**
  23848. * Normalize an `id` value by trimming off a leading `#`
  23849. *
  23850. * @private
  23851. * @param {string} id
  23852. * A string, maybe with a leading `#`.
  23853. *
  23854. * @return {string}
  23855. * The string, without any leading `#`.
  23856. */
  23857. const normalizeId = id => id.indexOf('#') === 0 ? id.slice(1) : id;
  23858. /**
  23859. * A callback that is called when a component is ready. Does not have any
  23860. * parameters and any callback value will be ignored. See: {@link Component~ReadyCallback}
  23861. *
  23862. * @callback ReadyCallback
  23863. */
  23864. /**
  23865. * The `videojs()` function doubles as the main function for users to create a
  23866. * {@link Player} instance as well as the main library namespace.
  23867. *
  23868. * It can also be used as a getter for a pre-existing {@link Player} instance.
  23869. * However, we _strongly_ recommend using `videojs.getPlayer()` for this
  23870. * purpose because it avoids any potential for unintended initialization.
  23871. *
  23872. * Due to [limitations](https://github.com/jsdoc3/jsdoc/issues/955#issuecomment-313829149)
  23873. * of our JSDoc template, we cannot properly document this as both a function
  23874. * and a namespace, so its function signature is documented here.
  23875. *
  23876. * #### Arguments
  23877. * ##### id
  23878. * string|Element, **required**
  23879. *
  23880. * Video element or video element ID.
  23881. *
  23882. * ##### options
  23883. * Object, optional
  23884. *
  23885. * Options object for providing settings.
  23886. * See: [Options Guide](https://docs.videojs.com/tutorial-options.html).
  23887. *
  23888. * ##### ready
  23889. * {@link Component~ReadyCallback}, optional
  23890. *
  23891. * A function to be called when the {@link Player} and {@link Tech} are ready.
  23892. *
  23893. * #### Return Value
  23894. *
  23895. * The `videojs()` function returns a {@link Player} instance.
  23896. *
  23897. * @namespace
  23898. *
  23899. * @borrows AudioTrack as AudioTrack
  23900. * @borrows Component.getComponent as getComponent
  23901. * @borrows module:events.on as on
  23902. * @borrows module:events.one as one
  23903. * @borrows module:events.off as off
  23904. * @borrows module:events.trigger as trigger
  23905. * @borrows EventTarget as EventTarget
  23906. * @borrows module:middleware.use as use
  23907. * @borrows Player.players as players
  23908. * @borrows Plugin.registerPlugin as registerPlugin
  23909. * @borrows Plugin.deregisterPlugin as deregisterPlugin
  23910. * @borrows Plugin.getPlugins as getPlugins
  23911. * @borrows Plugin.getPlugin as getPlugin
  23912. * @borrows Plugin.getPluginVersion as getPluginVersion
  23913. * @borrows Tech.getTech as getTech
  23914. * @borrows Tech.registerTech as registerTech
  23915. * @borrows TextTrack as TextTrack
  23916. * @borrows VideoTrack as VideoTrack
  23917. *
  23918. * @param {string|Element} id
  23919. * Video element or video element ID.
  23920. *
  23921. * @param {Object} [options]
  23922. * Options object for providing settings.
  23923. * See: [Options Guide](https://docs.videojs.com/tutorial-options.html).
  23924. *
  23925. * @param {ReadyCallback} [ready]
  23926. * A function to be called when the {@link Player} and {@link Tech} are
  23927. * ready.
  23928. *
  23929. * @return {Player}
  23930. * The `videojs()` function returns a {@link Player|Player} instance.
  23931. */
  23932. function videojs(id, options, ready) {
  23933. let player = videojs.getPlayer(id);
  23934. if (player) {
  23935. if (options) {
  23936. log.warn(`Player "${id}" is already initialised. Options will not be applied.`);
  23937. }
  23938. if (ready) {
  23939. player.ready(ready);
  23940. }
  23941. return player;
  23942. }
  23943. const el = typeof id === 'string' ? $('#' + normalizeId(id)) : id;
  23944. if (!isEl(el)) {
  23945. throw new TypeError('The element or ID supplied is not valid. (videojs)');
  23946. }
  23947. // document.body.contains(el) will only check if el is contained within that one document.
  23948. // This causes problems for elements in iframes.
  23949. // Instead, use the element's ownerDocument instead of the global document.
  23950. // This will make sure that the element is indeed in the dom of that document.
  23951. // Additionally, check that the document in question has a default view.
  23952. // If the document is no longer attached to the dom, the defaultView of the document will be null.
  23953. // If element is inside Shadow DOM (e.g. is part of a Custom element), ownerDocument.body
  23954. // always returns false. Instead, use the Shadow DOM root.
  23955. const inShadowDom = 'getRootNode' in el ? el.getRootNode() instanceof window.ShadowRoot : false;
  23956. const rootNode = inShadowDom ? el.getRootNode() : el.ownerDocument.body;
  23957. if (!el.ownerDocument.defaultView || !rootNode.contains(el)) {
  23958. log.warn('The element supplied is not included in the DOM');
  23959. }
  23960. options = options || {};
  23961. // Store a copy of the el before modification, if it is to be restored in destroy()
  23962. // If div ingest, store the parent div
  23963. if (options.restoreEl === true) {
  23964. options.restoreEl = (el.parentNode && el.parentNode.hasAttribute('data-vjs-player') ? el.parentNode : el).cloneNode(true);
  23965. }
  23966. hooks('beforesetup').forEach(hookFunction => {
  23967. const opts = hookFunction(el, merge(options));
  23968. if (!isObject(opts) || Array.isArray(opts)) {
  23969. log.error('please return an object in beforesetup hooks');
  23970. return;
  23971. }
  23972. options = merge(options, opts);
  23973. });
  23974. // We get the current "Player" component here in case an integration has
  23975. // replaced it with a custom player.
  23976. const PlayerComponent = Component.getComponent('Player');
  23977. player = new PlayerComponent(el, options, ready);
  23978. hooks('setup').forEach(hookFunction => hookFunction(player));
  23979. return player;
  23980. }
  23981. videojs.hooks_ = hooks_;
  23982. videojs.hooks = hooks;
  23983. videojs.hook = hook;
  23984. videojs.hookOnce = hookOnce;
  23985. videojs.removeHook = removeHook;
  23986. // Add default styles
  23987. if (window.VIDEOJS_NO_DYNAMIC_STYLE !== true && isReal()) {
  23988. let style = $('.vjs-styles-defaults');
  23989. if (!style) {
  23990. style = createStyleElement('vjs-styles-defaults');
  23991. const head = $('head');
  23992. if (head) {
  23993. head.insertBefore(style, head.firstChild);
  23994. }
  23995. setTextContent(style, `
  23996. .video-js {
  23997. width: 300px;
  23998. height: 150px;
  23999. }
  24000. .vjs-fluid:not(.vjs-audio-only-mode) {
  24001. padding-top: 56.25%
  24002. }
  24003. `);
  24004. }
  24005. }
  24006. // Run Auto-load players
  24007. // You have to wait at least once in case this script is loaded after your
  24008. // video in the DOM (weird behavior only with minified version)
  24009. autoSetupTimeout(1, videojs);
  24010. /**
  24011. * Current Video.js version. Follows [semantic versioning](https://semver.org/).
  24012. *
  24013. * @type {string}
  24014. */
  24015. videojs.VERSION = version;
  24016. /**
  24017. * The global options object. These are the settings that take effect
  24018. * if no overrides are specified when the player is created.
  24019. *
  24020. * @type {Object}
  24021. */
  24022. videojs.options = Player.prototype.options_;
  24023. /**
  24024. * Get an object with the currently created players, keyed by player ID
  24025. *
  24026. * @return {Object}
  24027. * The created players
  24028. */
  24029. videojs.getPlayers = () => Player.players;
  24030. /**
  24031. * Get a single player based on an ID or DOM element.
  24032. *
  24033. * This is useful if you want to check if an element or ID has an associated
  24034. * Video.js player, but not create one if it doesn't.
  24035. *
  24036. * @param {string|Element} id
  24037. * An HTML element - `<video>`, `<audio>`, or `<video-js>` -
  24038. * or a string matching the `id` of such an element.
  24039. *
  24040. * @return {Player|undefined}
  24041. * A player instance or `undefined` if there is no player instance
  24042. * matching the argument.
  24043. */
  24044. videojs.getPlayer = id => {
  24045. const players = Player.players;
  24046. let tag;
  24047. if (typeof id === 'string') {
  24048. const nId = normalizeId(id);
  24049. const player = players[nId];
  24050. if (player) {
  24051. return player;
  24052. }
  24053. tag = $('#' + nId);
  24054. } else {
  24055. tag = id;
  24056. }
  24057. if (isEl(tag)) {
  24058. const {
  24059. player,
  24060. playerId
  24061. } = tag;
  24062. // Element may have a `player` property referring to an already created
  24063. // player instance. If so, return that.
  24064. if (player || players[playerId]) {
  24065. return player || players[playerId];
  24066. }
  24067. }
  24068. };
  24069. /**
  24070. * Returns an array of all current players.
  24071. *
  24072. * @return {Array}
  24073. * An array of all players. The array will be in the order that
  24074. * `Object.keys` provides, which could potentially vary between
  24075. * JavaScript engines.
  24076. *
  24077. */
  24078. videojs.getAllPlayers = () =>
  24079. // Disposed players leave a key with a `null` value, so we need to make sure
  24080. // we filter those out.
  24081. Object.keys(Player.players).map(k => Player.players[k]).filter(Boolean);
  24082. videojs.players = Player.players;
  24083. videojs.getComponent = Component.getComponent;
  24084. /**
  24085. * Register a component so it can referred to by name. Used when adding to other
  24086. * components, either through addChild `component.addChild('myComponent')` or through
  24087. * default children options `{ children: ['myComponent'] }`.
  24088. *
  24089. * > NOTE: You could also just initialize the component before adding.
  24090. * `component.addChild(new MyComponent());`
  24091. *
  24092. * @param {string} name
  24093. * The class name of the component
  24094. *
  24095. * @param {typeof Component} comp
  24096. * The component class
  24097. *
  24098. * @return {typeof Component}
  24099. * The newly registered component
  24100. */
  24101. videojs.registerComponent = (name, comp) => {
  24102. if (Tech.isTech(comp)) {
  24103. log.warn(`The ${name} tech was registered as a component. It should instead be registered using videojs.registerTech(name, tech)`);
  24104. }
  24105. return Component.registerComponent.call(Component, name, comp);
  24106. };
  24107. videojs.getTech = Tech.getTech;
  24108. videojs.registerTech = Tech.registerTech;
  24109. videojs.use = use;
  24110. /**
  24111. * An object that can be returned by a middleware to signify
  24112. * that the middleware is being terminated.
  24113. *
  24114. * @type {object}
  24115. * @property {object} middleware.TERMINATOR
  24116. */
  24117. Object.defineProperty(videojs, 'middleware', {
  24118. value: {},
  24119. writeable: false,
  24120. enumerable: true
  24121. });
  24122. Object.defineProperty(videojs.middleware, 'TERMINATOR', {
  24123. value: TERMINATOR,
  24124. writeable: false,
  24125. enumerable: true
  24126. });
  24127. /**
  24128. * A reference to the {@link module:browser|browser utility module} as an object.
  24129. *
  24130. * @type {Object}
  24131. * @see {@link module:browser|browser}
  24132. */
  24133. videojs.browser = browser;
  24134. /**
  24135. * A reference to the {@link module:obj|obj utility module} as an object.
  24136. *
  24137. * @type {Object}
  24138. * @see {@link module:obj|obj}
  24139. */
  24140. videojs.obj = Obj;
  24141. /**
  24142. * Deprecated reference to the {@link module:obj.merge|merge function}
  24143. *
  24144. * @type {Function}
  24145. * @see {@link module:obj.merge|merge}
  24146. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.obj.merge instead.
  24147. */
  24148. videojs.mergeOptions = deprecateForMajor(9, 'videojs.mergeOptions', 'videojs.obj.merge', merge);
  24149. /**
  24150. * Deprecated reference to the {@link module:obj.defineLazyProperty|defineLazyProperty function}
  24151. *
  24152. * @type {Function}
  24153. * @see {@link module:obj.defineLazyProperty|defineLazyProperty}
  24154. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.obj.defineLazyProperty instead.
  24155. */
  24156. videojs.defineLazyProperty = deprecateForMajor(9, 'videojs.defineLazyProperty', 'videojs.obj.defineLazyProperty', defineLazyProperty);
  24157. /**
  24158. * Deprecated reference to the {@link module:fn.bind_|fn.bind_ function}
  24159. *
  24160. * @type {Function}
  24161. * @see {@link module:fn.bind_|fn.bind_}
  24162. * @deprecated Deprecated and will be removed in 9.0. Please use native Function.prototype.bind instead.
  24163. */
  24164. videojs.bind = deprecateForMajor(9, 'videojs.bind', 'native Function.prototype.bind', bind_);
  24165. videojs.registerPlugin = Plugin.registerPlugin;
  24166. videojs.deregisterPlugin = Plugin.deregisterPlugin;
  24167. /**
  24168. * Deprecated method to register a plugin with Video.js
  24169. *
  24170. * @deprecated Deprecated and will be removed in 9.0. Use videojs.registerPlugin() instead.
  24171. *
  24172. * @param {string} name
  24173. * The plugin name
  24174. *
  24175. * @param {typeof Plugin|Function} plugin
  24176. * The plugin sub-class or function
  24177. *
  24178. * @return {typeof Plugin|Function}
  24179. */
  24180. videojs.plugin = (name, plugin) => {
  24181. log.warn('videojs.plugin() is deprecated; use videojs.registerPlugin() instead');
  24182. return Plugin.registerPlugin(name, plugin);
  24183. };
  24184. videojs.getPlugins = Plugin.getPlugins;
  24185. videojs.getPlugin = Plugin.getPlugin;
  24186. videojs.getPluginVersion = Plugin.getPluginVersion;
  24187. /**
  24188. * Adding languages so that they're available to all players.
  24189. * Example: `videojs.addLanguage('es', { 'Hello': 'Hola' });`
  24190. *
  24191. * @param {string} code
  24192. * The language code or dictionary property
  24193. *
  24194. * @param {Object} data
  24195. * The data values to be translated
  24196. *
  24197. * @return {Object}
  24198. * The resulting language dictionary object
  24199. */
  24200. videojs.addLanguage = function (code, data) {
  24201. code = ('' + code).toLowerCase();
  24202. videojs.options.languages = merge(videojs.options.languages, {
  24203. [code]: data
  24204. });
  24205. return videojs.options.languages[code];
  24206. };
  24207. /**
  24208. * A reference to the {@link module:log|log utility module} as an object.
  24209. *
  24210. * @type {Function}
  24211. * @see {@link module:log|log}
  24212. */
  24213. videojs.log = log;
  24214. videojs.createLogger = createLogger;
  24215. /**
  24216. * A reference to the {@link module:time|time utility module} as an object.
  24217. *
  24218. * @type {Object}
  24219. * @see {@link module:time|time}
  24220. */
  24221. videojs.time = Time;
  24222. /**
  24223. * Deprecated reference to the {@link module:time.createTimeRanges|createTimeRanges function}
  24224. *
  24225. * @type {Function}
  24226. * @see {@link module:time.createTimeRanges|createTimeRanges}
  24227. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.createTimeRanges instead.
  24228. */
  24229. videojs.createTimeRange = deprecateForMajor(9, 'videojs.createTimeRange', 'videojs.time.createTimeRanges', createTimeRanges);
  24230. /**
  24231. * Deprecated reference to the {@link module:time.createTimeRanges|createTimeRanges function}
  24232. *
  24233. * @type {Function}
  24234. * @see {@link module:time.createTimeRanges|createTimeRanges}
  24235. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.createTimeRanges instead.
  24236. */
  24237. videojs.createTimeRanges = deprecateForMajor(9, 'videojs.createTimeRanges', 'videojs.time.createTimeRanges', createTimeRanges);
  24238. /**
  24239. * Deprecated reference to the {@link module:time.formatTime|formatTime function}
  24240. *
  24241. * @type {Function}
  24242. * @see {@link module:time.formatTime|formatTime}
  24243. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.format instead.
  24244. */
  24245. videojs.formatTime = deprecateForMajor(9, 'videojs.formatTime', 'videojs.time.formatTime', formatTime);
  24246. /**
  24247. * Deprecated reference to the {@link module:time.setFormatTime|setFormatTime function}
  24248. *
  24249. * @type {Function}
  24250. * @see {@link module:time.setFormatTime|setFormatTime}
  24251. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.setFormat instead.
  24252. */
  24253. videojs.setFormatTime = deprecateForMajor(9, 'videojs.setFormatTime', 'videojs.time.setFormatTime', setFormatTime);
  24254. /**
  24255. * Deprecated reference to the {@link module:time.resetFormatTime|resetFormatTime function}
  24256. *
  24257. * @type {Function}
  24258. * @see {@link module:time.resetFormatTime|resetFormatTime}
  24259. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.resetFormat instead.
  24260. */
  24261. videojs.resetFormatTime = deprecateForMajor(9, 'videojs.resetFormatTime', 'videojs.time.resetFormatTime', resetFormatTime);
  24262. /**
  24263. * Deprecated reference to the {@link module:url.parseUrl|Url.parseUrl function}
  24264. *
  24265. * @type {Function}
  24266. * @see {@link module:url.parseUrl|parseUrl}
  24267. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.url.parseUrl instead.
  24268. */
  24269. videojs.parseUrl = deprecateForMajor(9, 'videojs.parseUrl', 'videojs.url.parseUrl', parseUrl);
  24270. /**
  24271. * Deprecated reference to the {@link module:url.isCrossOrigin|Url.isCrossOrigin function}
  24272. *
  24273. * @type {Function}
  24274. * @see {@link module:url.isCrossOrigin|isCrossOrigin}
  24275. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.url.isCrossOrigin instead.
  24276. */
  24277. videojs.isCrossOrigin = deprecateForMajor(9, 'videojs.isCrossOrigin', 'videojs.url.isCrossOrigin', isCrossOrigin);
  24278. videojs.EventTarget = EventTarget;
  24279. videojs.any = any;
  24280. videojs.on = on;
  24281. videojs.one = one;
  24282. videojs.off = off;
  24283. videojs.trigger = trigger;
  24284. /**
  24285. * A cross-browser XMLHttpRequest wrapper.
  24286. *
  24287. * @function
  24288. * @param {Object} options
  24289. * Settings for the request.
  24290. *
  24291. * @return {XMLHttpRequest|XDomainRequest}
  24292. * The request object.
  24293. *
  24294. * @see https://github.com/Raynos/xhr
  24295. */
  24296. videojs.xhr = XHR;
  24297. videojs.TextTrack = TextTrack;
  24298. videojs.AudioTrack = AudioTrack;
  24299. videojs.VideoTrack = VideoTrack;
  24300. ['isEl', 'isTextNode', 'createEl', 'hasClass', 'addClass', 'removeClass', 'toggleClass', 'setAttributes', 'getAttributes', 'emptyEl', 'appendContent', 'insertContent'].forEach(k => {
  24301. videojs[k] = function () {
  24302. log.warn(`videojs.${k}() is deprecated; use videojs.dom.${k}() instead`);
  24303. return Dom[k].apply(null, arguments);
  24304. };
  24305. });
  24306. videojs.computedStyle = deprecateForMajor(9, 'videojs.computedStyle', 'videojs.dom.computedStyle', computedStyle);
  24307. /**
  24308. * A reference to the {@link module:dom|DOM utility module} as an object.
  24309. *
  24310. * @type {Object}
  24311. * @see {@link module:dom|dom}
  24312. */
  24313. videojs.dom = Dom;
  24314. /**
  24315. * A reference to the {@link module:fn|fn utility module} as an object.
  24316. *
  24317. * @type {Object}
  24318. * @see {@link module:fn|fn}
  24319. */
  24320. videojs.fn = Fn;
  24321. /**
  24322. * A reference to the {@link module:num|num utility module} as an object.
  24323. *
  24324. * @type {Object}
  24325. * @see {@link module:num|num}
  24326. */
  24327. videojs.num = Num;
  24328. /**
  24329. * A reference to the {@link module:str|str utility module} as an object.
  24330. *
  24331. * @type {Object}
  24332. * @see {@link module:str|str}
  24333. */
  24334. videojs.str = Str;
  24335. /**
  24336. * A reference to the {@link module:url|URL utility module} as an object.
  24337. *
  24338. * @type {Object}
  24339. * @see {@link module:url|url}
  24340. */
  24341. videojs.url = Url;
  24342. export { videojs as default };