video.core.novtt.js 806 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296
  1. /**
  2. * @license
  3. * Video.js 8.3.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. (function (global, factory) {
  13. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  14. typeof define === 'function' && define.amd ? define(factory) :
  15. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.videojs = factory());
  16. })(this, (function () { 'use strict';
  17. var version = "8.3.0";
  18. /**
  19. * An Object that contains lifecycle hooks as keys which point to an array
  20. * of functions that are run when a lifecycle is triggered
  21. *
  22. * @private
  23. */
  24. const hooks_ = {};
  25. /**
  26. * Get a list of hooks for a specific lifecycle
  27. *
  28. * @param {string} type
  29. * the lifecycle to get hooks from
  30. *
  31. * @param {Function|Function[]} [fn]
  32. * Optionally add a hook (or hooks) to the lifecycle that your are getting.
  33. *
  34. * @return {Array}
  35. * an array of hooks, or an empty array if there are none.
  36. */
  37. const hooks = function (type, fn) {
  38. hooks_[type] = hooks_[type] || [];
  39. if (fn) {
  40. hooks_[type] = hooks_[type].concat(fn);
  41. }
  42. return hooks_[type];
  43. };
  44. /**
  45. * Add a function hook to a specific videojs lifecycle.
  46. *
  47. * @param {string} type
  48. * the lifecycle to hook the function to.
  49. *
  50. * @param {Function|Function[]}
  51. * The function or array of functions to attach.
  52. */
  53. const hook = function (type, fn) {
  54. hooks(type, fn);
  55. };
  56. /**
  57. * Remove a hook from a specific videojs lifecycle.
  58. *
  59. * @param {string} type
  60. * the lifecycle that the function hooked to
  61. *
  62. * @param {Function} fn
  63. * The hooked function to remove
  64. *
  65. * @return {boolean}
  66. * The function that was removed or undef
  67. */
  68. const removeHook = function (type, fn) {
  69. const index = hooks(type).indexOf(fn);
  70. if (index <= -1) {
  71. return false;
  72. }
  73. hooks_[type] = hooks_[type].slice();
  74. hooks_[type].splice(index, 1);
  75. return true;
  76. };
  77. /**
  78. * Add a function hook that will only run once to a specific videojs lifecycle.
  79. *
  80. * @param {string} type
  81. * the lifecycle to hook the function to.
  82. *
  83. * @param {Function|Function[]}
  84. * The function or array of functions to attach.
  85. */
  86. const hookOnce = function (type, fn) {
  87. hooks(type, [].concat(fn).map(original => {
  88. const wrapper = (...args) => {
  89. removeHook(type, wrapper);
  90. return original(...args);
  91. };
  92. return wrapper;
  93. }));
  94. };
  95. /**
  96. * @file fullscreen-api.js
  97. * @module fullscreen-api
  98. */
  99. /**
  100. * Store the browser-specific methods for the fullscreen API.
  101. *
  102. * @type {Object}
  103. * @see [Specification]{@link https://fullscreen.spec.whatwg.org}
  104. * @see [Map Approach From Screenfull.js]{@link https://github.com/sindresorhus/screenfull.js}
  105. */
  106. const FullscreenApi = {
  107. prefixed: true
  108. };
  109. // browser API methods
  110. const apiMap = [['requestFullscreen', 'exitFullscreen', 'fullscreenElement', 'fullscreenEnabled', 'fullscreenchange', 'fullscreenerror', 'fullscreen'],
  111. // WebKit
  112. ['webkitRequestFullscreen', 'webkitExitFullscreen', 'webkitFullscreenElement', 'webkitFullscreenEnabled', 'webkitfullscreenchange', 'webkitfullscreenerror', '-webkit-full-screen'],
  113. // Mozilla
  114. ['mozRequestFullScreen', 'mozCancelFullScreen', 'mozFullScreenElement', 'mozFullScreenEnabled', 'mozfullscreenchange', 'mozfullscreenerror', '-moz-full-screen'],
  115. // Microsoft
  116. ['msRequestFullscreen', 'msExitFullscreen', 'msFullscreenElement', 'msFullscreenEnabled', 'MSFullscreenChange', 'MSFullscreenError', '-ms-fullscreen']];
  117. const specApi = apiMap[0];
  118. let browserApi;
  119. // determine the supported set of functions
  120. for (let i = 0; i < apiMap.length; i++) {
  121. // check for exitFullscreen function
  122. if (apiMap[i][1] in document) {
  123. browserApi = apiMap[i];
  124. break;
  125. }
  126. }
  127. // map the browser API names to the spec API names
  128. if (browserApi) {
  129. for (let i = 0; i < browserApi.length; i++) {
  130. FullscreenApi[specApi[i]] = browserApi[i];
  131. }
  132. FullscreenApi.prefixed = browserApi[0] !== specApi[0];
  133. }
  134. /**
  135. * @file create-logger.js
  136. * @module create-logger
  137. */
  138. // This is the private tracking variable for the logging history.
  139. let history = [];
  140. /**
  141. * Log messages to the console and history based on the type of message
  142. *
  143. * @private
  144. * @param {string} type
  145. * The name of the console method to use.
  146. *
  147. * @param {Array} args
  148. * The arguments to be passed to the matching console method.
  149. */
  150. const LogByTypeFactory = (name, log) => (type, level, args) => {
  151. const lvl = log.levels[level];
  152. const lvlRegExp = new RegExp(`^(${lvl})$`);
  153. if (type !== 'log') {
  154. // Add the type to the front of the message when it's not "log".
  155. args.unshift(type.toUpperCase() + ':');
  156. }
  157. // Add console prefix after adding to history.
  158. args.unshift(name + ':');
  159. // Add a clone of the args at this point to history.
  160. if (history) {
  161. history.push([].concat(args));
  162. // only store 1000 history entries
  163. const splice = history.length - 1000;
  164. history.splice(0, splice > 0 ? splice : 0);
  165. }
  166. // If there's no console then don't try to output messages, but they will
  167. // still be stored in history.
  168. if (!window.console) {
  169. return;
  170. }
  171. // Was setting these once outside of this function, but containing them
  172. // in the function makes it easier to test cases where console doesn't exist
  173. // when the module is executed.
  174. let fn = window.console[type];
  175. if (!fn && type === 'debug') {
  176. // Certain browsers don't have support for console.debug. For those, we
  177. // should default to the closest comparable log.
  178. fn = window.console.info || window.console.log;
  179. }
  180. // Bail out if there's no console or if this type is not allowed by the
  181. // current logging level.
  182. if (!fn || !lvl || !lvlRegExp.test(type)) {
  183. return;
  184. }
  185. fn[Array.isArray(args) ? 'apply' : 'call'](window.console, args);
  186. };
  187. function createLogger$1(name) {
  188. // This is the private tracking variable for logging level.
  189. let level = 'info';
  190. // the curried logByType bound to the specific log and history
  191. let logByType;
  192. /**
  193. * Logs plain debug messages. Similar to `console.log`.
  194. *
  195. * Due to [limitations](https://github.com/jsdoc3/jsdoc/issues/955#issuecomment-313829149)
  196. * of our JSDoc template, we cannot properly document this as both a function
  197. * and a namespace, so its function signature is documented here.
  198. *
  199. * #### Arguments
  200. * ##### *args
  201. * *[]
  202. *
  203. * Any combination of values that could be passed to `console.log()`.
  204. *
  205. * #### Return Value
  206. *
  207. * `undefined`
  208. *
  209. * @namespace
  210. * @param {...*} args
  211. * One or more messages or objects that should be logged.
  212. */
  213. const log = function (...args) {
  214. logByType('log', level, args);
  215. };
  216. // This is the logByType helper that the logging methods below use
  217. logByType = LogByTypeFactory(name, log);
  218. /**
  219. * Create a new sublogger which chains the old name to the new name.
  220. *
  221. * For example, doing `videojs.log.createLogger('player')` and then using that logger will log the following:
  222. * ```js
  223. * mylogger('foo');
  224. * // > VIDEOJS: player: foo
  225. * ```
  226. *
  227. * @param {string} name
  228. * The name to add call the new logger
  229. * @return {Object}
  230. */
  231. log.createLogger = subname => createLogger$1(name + ': ' + subname);
  232. /**
  233. * Enumeration of available logging levels, where the keys are the level names
  234. * and the values are `|`-separated strings containing logging methods allowed
  235. * in that logging level. These strings are used to create a regular expression
  236. * matching the function name being called.
  237. *
  238. * Levels provided by Video.js are:
  239. *
  240. * - `off`: Matches no calls. Any value that can be cast to `false` will have
  241. * this effect. The most restrictive.
  242. * - `all`: Matches only Video.js-provided functions (`debug`, `log`,
  243. * `log.warn`, and `log.error`).
  244. * - `debug`: Matches `log.debug`, `log`, `log.warn`, and `log.error` calls.
  245. * - `info` (default): Matches `log`, `log.warn`, and `log.error` calls.
  246. * - `warn`: Matches `log.warn` and `log.error` calls.
  247. * - `error`: Matches only `log.error` calls.
  248. *
  249. * @type {Object}
  250. */
  251. log.levels = {
  252. all: 'debug|log|warn|error',
  253. off: '',
  254. debug: 'debug|log|warn|error',
  255. info: 'log|warn|error',
  256. warn: 'warn|error',
  257. error: 'error',
  258. DEFAULT: level
  259. };
  260. /**
  261. * Get or set the current logging level.
  262. *
  263. * If a string matching a key from {@link module:log.levels} is provided, acts
  264. * as a setter.
  265. *
  266. * @param {string} [lvl]
  267. * Pass a valid level to set a new logging level.
  268. *
  269. * @return {string}
  270. * The current logging level.
  271. */
  272. log.level = lvl => {
  273. if (typeof lvl === 'string') {
  274. if (!log.levels.hasOwnProperty(lvl)) {
  275. throw new Error(`"${lvl}" in not a valid log level`);
  276. }
  277. level = lvl;
  278. }
  279. return level;
  280. };
  281. /**
  282. * Returns an array containing everything that has been logged to the history.
  283. *
  284. * This array is a shallow clone of the internal history record. However, its
  285. * contents are _not_ cloned; so, mutating objects inside this array will
  286. * mutate them in history.
  287. *
  288. * @return {Array}
  289. */
  290. log.history = () => history ? [].concat(history) : [];
  291. /**
  292. * Allows you to filter the history by the given logger name
  293. *
  294. * @param {string} fname
  295. * The name to filter by
  296. *
  297. * @return {Array}
  298. * The filtered list to return
  299. */
  300. log.history.filter = fname => {
  301. return (history || []).filter(historyItem => {
  302. // if the first item in each historyItem includes `fname`, then it's a match
  303. return new RegExp(`.*${fname}.*`).test(historyItem[0]);
  304. });
  305. };
  306. /**
  307. * Clears the internal history tracking, but does not prevent further history
  308. * tracking.
  309. */
  310. log.history.clear = () => {
  311. if (history) {
  312. history.length = 0;
  313. }
  314. };
  315. /**
  316. * Disable history tracking if it is currently enabled.
  317. */
  318. log.history.disable = () => {
  319. if (history !== null) {
  320. history.length = 0;
  321. history = null;
  322. }
  323. };
  324. /**
  325. * Enable history tracking if it is currently disabled.
  326. */
  327. log.history.enable = () => {
  328. if (history === null) {
  329. history = [];
  330. }
  331. };
  332. /**
  333. * Logs error messages. Similar to `console.error`.
  334. *
  335. * @param {...*} args
  336. * One or more messages or objects that should be logged as an error
  337. */
  338. log.error = (...args) => logByType('error', level, args);
  339. /**
  340. * Logs warning messages. Similar to `console.warn`.
  341. *
  342. * @param {...*} args
  343. * One or more messages or objects that should be logged as a warning.
  344. */
  345. log.warn = (...args) => logByType('warn', level, args);
  346. /**
  347. * Logs debug messages. Similar to `console.debug`, but may also act as a comparable
  348. * log if `console.debug` is not available
  349. *
  350. * @param {...*} args
  351. * One or more messages or objects that should be logged as debug.
  352. */
  353. log.debug = (...args) => logByType('debug', level, args);
  354. return log;
  355. }
  356. /**
  357. * @file log.js
  358. * @module log
  359. */
  360. const log = createLogger$1('VIDEOJS');
  361. const createLogger = log.createLogger;
  362. /**
  363. * @file obj.js
  364. * @module obj
  365. */
  366. /**
  367. * @callback obj:EachCallback
  368. *
  369. * @param {*} value
  370. * The current key for the object that is being iterated over.
  371. *
  372. * @param {string} key
  373. * The current key-value for object that is being iterated over
  374. */
  375. /**
  376. * @callback obj:ReduceCallback
  377. *
  378. * @param {*} accum
  379. * The value that is accumulating over the reduce loop.
  380. *
  381. * @param {*} value
  382. * The current key for the object that is being iterated over.
  383. *
  384. * @param {string} key
  385. * The current key-value for object that is being iterated over
  386. *
  387. * @return {*}
  388. * The new accumulated value.
  389. */
  390. const toString$1 = Object.prototype.toString;
  391. /**
  392. * Get the keys of an Object
  393. *
  394. * @param {Object}
  395. * The Object to get the keys from
  396. *
  397. * @return {string[]}
  398. * An array of the keys from the object. Returns an empty array if the
  399. * object passed in was invalid or had no keys.
  400. *
  401. * @private
  402. */
  403. const keys = function (object) {
  404. return isObject(object) ? Object.keys(object) : [];
  405. };
  406. /**
  407. * Array-like iteration for objects.
  408. *
  409. * @param {Object} object
  410. * The object to iterate over
  411. *
  412. * @param {obj:EachCallback} fn
  413. * The callback function which is called for each key in the object.
  414. */
  415. function each(object, fn) {
  416. keys(object).forEach(key => fn(object[key], key));
  417. }
  418. /**
  419. * Array-like reduce for objects.
  420. *
  421. * @param {Object} object
  422. * The Object that you want to reduce.
  423. *
  424. * @param {Function} fn
  425. * A callback function which is called for each key in the object. It
  426. * receives the accumulated value and the per-iteration value and key
  427. * as arguments.
  428. *
  429. * @param {*} [initial = 0]
  430. * Starting value
  431. *
  432. * @return {*}
  433. * The final accumulated value.
  434. */
  435. function reduce(object, fn, initial = 0) {
  436. return keys(object).reduce((accum, key) => fn(accum, object[key], key), initial);
  437. }
  438. /**
  439. * Returns whether a value is an object of any kind - including DOM nodes,
  440. * arrays, regular expressions, etc. Not functions, though.
  441. *
  442. * This avoids the gotcha where using `typeof` on a `null` value
  443. * results in `'object'`.
  444. *
  445. * @param {Object} value
  446. * @return {boolean}
  447. */
  448. function isObject(value) {
  449. return !!value && typeof value === 'object';
  450. }
  451. /**
  452. * Returns whether an object appears to be a "plain" object - that is, a
  453. * direct instance of `Object`.
  454. *
  455. * @param {Object} value
  456. * @return {boolean}
  457. */
  458. function isPlain(value) {
  459. return isObject(value) && toString$1.call(value) === '[object Object]' && value.constructor === Object;
  460. }
  461. /**
  462. * Merge two objects recursively.
  463. *
  464. * Performs a deep merge like
  465. * {@link https://lodash.com/docs/4.17.10#merge|lodash.merge}, but only merges
  466. * plain objects (not arrays, elements, or anything else).
  467. *
  468. * Non-plain object values will be copied directly from the right-most
  469. * argument.
  470. *
  471. * @param {Object[]} sources
  472. * One or more objects to merge into a new object.
  473. *
  474. * @return {Object}
  475. * A new object that is the merged result of all sources.
  476. */
  477. function merge(...sources) {
  478. const result = {};
  479. sources.forEach(source => {
  480. if (!source) {
  481. return;
  482. }
  483. each(source, (value, key) => {
  484. if (!isPlain(value)) {
  485. result[key] = value;
  486. return;
  487. }
  488. if (!isPlain(result[key])) {
  489. result[key] = {};
  490. }
  491. result[key] = merge(result[key], value);
  492. });
  493. });
  494. return result;
  495. }
  496. /**
  497. * Object.defineProperty but "lazy", which means that the value is only set after
  498. * it is retrieved the first time, rather than being set right away.
  499. *
  500. * @param {Object} obj the object to set the property on
  501. * @param {string} key the key for the property to set
  502. * @param {Function} getValue the function used to get the value when it is needed.
  503. * @param {boolean} setter whether a setter should be allowed or not
  504. */
  505. function defineLazyProperty(obj, key, getValue, setter = true) {
  506. const set = value => Object.defineProperty(obj, key, {
  507. value,
  508. enumerable: true,
  509. writable: true
  510. });
  511. const options = {
  512. configurable: true,
  513. enumerable: true,
  514. get() {
  515. const value = getValue();
  516. set(value);
  517. return value;
  518. }
  519. };
  520. if (setter) {
  521. options.set = set;
  522. }
  523. return Object.defineProperty(obj, key, options);
  524. }
  525. var Obj = /*#__PURE__*/Object.freeze({
  526. __proto__: null,
  527. each: each,
  528. reduce: reduce,
  529. isObject: isObject,
  530. isPlain: isPlain,
  531. merge: merge,
  532. defineLazyProperty: defineLazyProperty
  533. });
  534. /**
  535. * @file browser.js
  536. * @module browser
  537. */
  538. /**
  539. * Whether or not this device is an iPod.
  540. *
  541. * @static
  542. * @type {Boolean}
  543. */
  544. let IS_IPOD = false;
  545. /**
  546. * The detected iOS version - or `null`.
  547. *
  548. * @static
  549. * @type {string|null}
  550. */
  551. let IOS_VERSION = null;
  552. /**
  553. * Whether or not this is an Android device.
  554. *
  555. * @static
  556. * @type {Boolean}
  557. */
  558. let IS_ANDROID = false;
  559. /**
  560. * The detected Android version - or `null` if not Android or indeterminable.
  561. *
  562. * @static
  563. * @type {number|string|null}
  564. */
  565. let ANDROID_VERSION;
  566. /**
  567. * Whether or not this is Mozilla Firefox.
  568. *
  569. * @static
  570. * @type {Boolean}
  571. */
  572. let IS_FIREFOX = false;
  573. /**
  574. * Whether or not this is Microsoft Edge.
  575. *
  576. * @static
  577. * @type {Boolean}
  578. */
  579. let IS_EDGE = false;
  580. /**
  581. * Whether or not this is any Chromium Browser
  582. *
  583. * @static
  584. * @type {Boolean}
  585. */
  586. let IS_CHROMIUM = false;
  587. /**
  588. * Whether or not this is any Chromium browser that is not Edge.
  589. *
  590. * This will also be `true` for Chrome on iOS, which will have different support
  591. * as it is actually Safari under the hood.
  592. *
  593. * Deprecated, as the behaviour to not match Edge was to prevent Legacy Edge's UA matching.
  594. * IS_CHROMIUM should be used instead.
  595. * "Chromium but not Edge" could be explicitly tested with IS_CHROMIUM && !IS_EDGE
  596. *
  597. * @static
  598. * @deprecated
  599. * @type {Boolean}
  600. */
  601. let IS_CHROME = false;
  602. /**
  603. * The detected Chromium version - or `null`.
  604. *
  605. * @static
  606. * @type {number|null}
  607. */
  608. let CHROMIUM_VERSION = null;
  609. /**
  610. * The detected Google Chrome version - or `null`.
  611. * This has always been the _Chromium_ version, i.e. would return on Chromium Edge.
  612. * Deprecated, use CHROMIUM_VERSION instead.
  613. *
  614. * @static
  615. * @deprecated
  616. * @type {number|null}
  617. */
  618. let CHROME_VERSION = null;
  619. /**
  620. * The detected Internet Explorer version - or `null`.
  621. *
  622. * @static
  623. * @deprecated
  624. * @type {number|null}
  625. */
  626. let IE_VERSION = null;
  627. /**
  628. * Whether or not this is desktop Safari.
  629. *
  630. * @static
  631. * @type {Boolean}
  632. */
  633. let IS_SAFARI = false;
  634. /**
  635. * Whether or not this is a Windows machine.
  636. *
  637. * @static
  638. * @type {Boolean}
  639. */
  640. let IS_WINDOWS = false;
  641. /**
  642. * Whether or not this device is an iPad.
  643. *
  644. * @static
  645. * @type {Boolean}
  646. */
  647. let IS_IPAD = false;
  648. /**
  649. * Whether or not this device is an iPhone.
  650. *
  651. * @static
  652. * @type {Boolean}
  653. */
  654. // The Facebook app's UIWebView identifies as both an iPhone and iPad, so
  655. // to identify iPhones, we need to exclude iPads.
  656. // http://artsy.github.io/blog/2012/10/18/the-perils-of-ios-user-agent-sniffing/
  657. let IS_IPHONE = false;
  658. /**
  659. * Whether or not this device is touch-enabled.
  660. *
  661. * @static
  662. * @const
  663. * @type {Boolean}
  664. */
  665. const TOUCH_ENABLED = Boolean(isReal() && ('ontouchstart' in window || window.navigator.maxTouchPoints || window.DocumentTouch && window.document instanceof window.DocumentTouch));
  666. const UAD = window.navigator && window.navigator.userAgentData;
  667. if (UAD) {
  668. // If userAgentData is present, use it instead of userAgent to avoid warnings
  669. // Currently only implemented on Chromium
  670. // userAgentData does not expose Android version, so ANDROID_VERSION remains `null`
  671. IS_ANDROID = UAD.platform === 'Android';
  672. IS_EDGE = Boolean(UAD.brands.find(b => b.brand === 'Microsoft Edge'));
  673. IS_CHROMIUM = Boolean(UAD.brands.find(b => b.brand === 'Chromium'));
  674. IS_CHROME = !IS_EDGE && IS_CHROMIUM;
  675. CHROMIUM_VERSION = CHROME_VERSION = (UAD.brands.find(b => b.brand === 'Chromium') || {}).version || null;
  676. IS_WINDOWS = UAD.platform === 'Windows';
  677. }
  678. // If the browser is not Chromium, either userAgentData is not present which could be an old Chromium browser,
  679. // or it's a browser that has added userAgentData since that we don't have tests for yet. In either case,
  680. // the checks need to be made agiainst the regular userAgent string.
  681. if (!IS_CHROMIUM) {
  682. const USER_AGENT = window.navigator && window.navigator.userAgent || '';
  683. IS_IPOD = /iPod/i.test(USER_AGENT);
  684. IOS_VERSION = function () {
  685. const match = USER_AGENT.match(/OS (\d+)_/i);
  686. if (match && match[1]) {
  687. return match[1];
  688. }
  689. return null;
  690. }();
  691. IS_ANDROID = /Android/i.test(USER_AGENT);
  692. ANDROID_VERSION = function () {
  693. // This matches Android Major.Minor.Patch versions
  694. // ANDROID_VERSION is Major.Minor as a Number, if Minor isn't available, then only Major is returned
  695. const match = USER_AGENT.match(/Android (\d+)(?:\.(\d+))?(?:\.(\d+))*/i);
  696. if (!match) {
  697. return null;
  698. }
  699. const major = match[1] && parseFloat(match[1]);
  700. const minor = match[2] && parseFloat(match[2]);
  701. if (major && minor) {
  702. return parseFloat(match[1] + '.' + match[2]);
  703. } else if (major) {
  704. return major;
  705. }
  706. return null;
  707. }();
  708. IS_FIREFOX = /Firefox/i.test(USER_AGENT);
  709. IS_EDGE = /Edg/i.test(USER_AGENT);
  710. IS_CHROMIUM = /Chrome/i.test(USER_AGENT) || /CriOS/i.test(USER_AGENT);
  711. IS_CHROME = !IS_EDGE && IS_CHROMIUM;
  712. CHROMIUM_VERSION = CHROME_VERSION = function () {
  713. const match = USER_AGENT.match(/(Chrome|CriOS)\/(\d+)/);
  714. if (match && match[2]) {
  715. return parseFloat(match[2]);
  716. }
  717. return null;
  718. }();
  719. IE_VERSION = function () {
  720. const result = /MSIE\s(\d+)\.\d/.exec(USER_AGENT);
  721. let version = result && parseFloat(result[1]);
  722. if (!version && /Trident\/7.0/i.test(USER_AGENT) && /rv:11.0/.test(USER_AGENT)) {
  723. // IE 11 has a different user agent string than other IE versions
  724. version = 11.0;
  725. }
  726. return version;
  727. }();
  728. IS_SAFARI = /Safari/i.test(USER_AGENT) && !IS_CHROME && !IS_ANDROID && !IS_EDGE;
  729. IS_WINDOWS = /Windows/i.test(USER_AGENT);
  730. IS_IPAD = /iPad/i.test(USER_AGENT) || IS_SAFARI && TOUCH_ENABLED && !/iPhone/i.test(USER_AGENT);
  731. IS_IPHONE = /iPhone/i.test(USER_AGENT) && !IS_IPAD;
  732. }
  733. /**
  734. * Whether or not this is an iOS device.
  735. *
  736. * @static
  737. * @const
  738. * @type {Boolean}
  739. */
  740. const IS_IOS = IS_IPHONE || IS_IPAD || IS_IPOD;
  741. /**
  742. * Whether or not this is any flavor of Safari - including iOS.
  743. *
  744. * @static
  745. * @const
  746. * @type {Boolean}
  747. */
  748. const IS_ANY_SAFARI = (IS_SAFARI || IS_IOS) && !IS_CHROME;
  749. var browser = /*#__PURE__*/Object.freeze({
  750. __proto__: null,
  751. get IS_IPOD () { return IS_IPOD; },
  752. get IOS_VERSION () { return IOS_VERSION; },
  753. get IS_ANDROID () { return IS_ANDROID; },
  754. get ANDROID_VERSION () { return ANDROID_VERSION; },
  755. get IS_FIREFOX () { return IS_FIREFOX; },
  756. get IS_EDGE () { return IS_EDGE; },
  757. get IS_CHROMIUM () { return IS_CHROMIUM; },
  758. get IS_CHROME () { return IS_CHROME; },
  759. get CHROMIUM_VERSION () { return CHROMIUM_VERSION; },
  760. get CHROME_VERSION () { return CHROME_VERSION; },
  761. get IE_VERSION () { return IE_VERSION; },
  762. get IS_SAFARI () { return IS_SAFARI; },
  763. get IS_WINDOWS () { return IS_WINDOWS; },
  764. get IS_IPAD () { return IS_IPAD; },
  765. get IS_IPHONE () { return IS_IPHONE; },
  766. TOUCH_ENABLED: TOUCH_ENABLED,
  767. IS_IOS: IS_IOS,
  768. IS_ANY_SAFARI: IS_ANY_SAFARI
  769. });
  770. /**
  771. * @file dom.js
  772. * @module dom
  773. */
  774. /**
  775. * Detect if a value is a string with any non-whitespace characters.
  776. *
  777. * @private
  778. * @param {string} str
  779. * The string to check
  780. *
  781. * @return {boolean}
  782. * Will be `true` if the string is non-blank, `false` otherwise.
  783. *
  784. */
  785. function isNonBlankString(str) {
  786. // we use str.trim as it will trim any whitespace characters
  787. // from the front or back of non-whitespace characters. aka
  788. // Any string that contains non-whitespace characters will
  789. // still contain them after `trim` but whitespace only strings
  790. // will have a length of 0, failing this check.
  791. return typeof str === 'string' && Boolean(str.trim());
  792. }
  793. /**
  794. * Throws an error if the passed string has whitespace. This is used by
  795. * class methods to be relatively consistent with the classList API.
  796. *
  797. * @private
  798. * @param {string} str
  799. * The string to check for whitespace.
  800. *
  801. * @throws {Error}
  802. * Throws an error if there is whitespace in the string.
  803. */
  804. function throwIfWhitespace(str) {
  805. // str.indexOf instead of regex because str.indexOf is faster performance wise.
  806. if (str.indexOf(' ') >= 0) {
  807. throw new Error('class has illegal whitespace characters');
  808. }
  809. }
  810. /**
  811. * Whether the current DOM interface appears to be real (i.e. not simulated).
  812. *
  813. * @return {boolean}
  814. * Will be `true` if the DOM appears to be real, `false` otherwise.
  815. */
  816. function isReal() {
  817. // Both document and window will never be undefined thanks to `global`.
  818. return document === window.document;
  819. }
  820. /**
  821. * Determines, via duck typing, whether or not a value is a DOM element.
  822. *
  823. * @param {*} value
  824. * The value to check.
  825. *
  826. * @return {boolean}
  827. * Will be `true` if the value is a DOM element, `false` otherwise.
  828. */
  829. function isEl(value) {
  830. return isObject(value) && value.nodeType === 1;
  831. }
  832. /**
  833. * Determines if the current DOM is embedded in an iframe.
  834. *
  835. * @return {boolean}
  836. * Will be `true` if the DOM is embedded in an iframe, `false`
  837. * otherwise.
  838. */
  839. function isInFrame() {
  840. // We need a try/catch here because Safari will throw errors when attempting
  841. // to get either `parent` or `self`
  842. try {
  843. return window.parent !== window.self;
  844. } catch (x) {
  845. return true;
  846. }
  847. }
  848. /**
  849. * Creates functions to query the DOM using a given method.
  850. *
  851. * @private
  852. * @param {string} method
  853. * The method to create the query with.
  854. *
  855. * @return {Function}
  856. * The query method
  857. */
  858. function createQuerier(method) {
  859. return function (selector, context) {
  860. if (!isNonBlankString(selector)) {
  861. return document[method](null);
  862. }
  863. if (isNonBlankString(context)) {
  864. context = document.querySelector(context);
  865. }
  866. const ctx = isEl(context) ? context : document;
  867. return ctx[method] && ctx[method](selector);
  868. };
  869. }
  870. /**
  871. * Creates an element and applies properties, attributes, and inserts content.
  872. *
  873. * @param {string} [tagName='div']
  874. * Name of tag to be created.
  875. *
  876. * @param {Object} [properties={}]
  877. * Element properties to be applied.
  878. *
  879. * @param {Object} [attributes={}]
  880. * Element attributes to be applied.
  881. *
  882. * @param {ContentDescriptor} [content]
  883. * A content descriptor object.
  884. *
  885. * @return {Element}
  886. * The element that was created.
  887. */
  888. function createEl(tagName = 'div', properties = {}, attributes = {}, content) {
  889. const el = document.createElement(tagName);
  890. Object.getOwnPropertyNames(properties).forEach(function (propName) {
  891. const val = properties[propName];
  892. // Handle textContent since it's not supported everywhere and we have a
  893. // method for it.
  894. if (propName === 'textContent') {
  895. textContent(el, val);
  896. } else if (el[propName] !== val || propName === 'tabIndex') {
  897. el[propName] = val;
  898. }
  899. });
  900. Object.getOwnPropertyNames(attributes).forEach(function (attrName) {
  901. el.setAttribute(attrName, attributes[attrName]);
  902. });
  903. if (content) {
  904. appendContent(el, content);
  905. }
  906. return el;
  907. }
  908. /**
  909. * Injects text into an element, replacing any existing contents entirely.
  910. *
  911. * @param {Element} el
  912. * The element to add text content into
  913. *
  914. * @param {string} text
  915. * The text content to add.
  916. *
  917. * @return {Element}
  918. * The element with added text content.
  919. */
  920. function textContent(el, text) {
  921. if (typeof el.textContent === 'undefined') {
  922. el.innerText = text;
  923. } else {
  924. el.textContent = text;
  925. }
  926. return el;
  927. }
  928. /**
  929. * Insert an element as the first child node of another
  930. *
  931. * @param {Element} child
  932. * Element to insert
  933. *
  934. * @param {Element} parent
  935. * Element to insert child into
  936. */
  937. function prependTo(child, parent) {
  938. if (parent.firstChild) {
  939. parent.insertBefore(child, parent.firstChild);
  940. } else {
  941. parent.appendChild(child);
  942. }
  943. }
  944. /**
  945. * Check if an element has a class name.
  946. *
  947. * @param {Element} element
  948. * Element to check
  949. *
  950. * @param {string} classToCheck
  951. * Class name to check for
  952. *
  953. * @return {boolean}
  954. * Will be `true` if the element has a class, `false` otherwise.
  955. *
  956. * @throws {Error}
  957. * Throws an error if `classToCheck` has white space.
  958. */
  959. function hasClass(element, classToCheck) {
  960. throwIfWhitespace(classToCheck);
  961. return element.classList.contains(classToCheck);
  962. }
  963. /**
  964. * Add a class name to an element.
  965. *
  966. * @param {Element} element
  967. * Element to add class name to.
  968. *
  969. * @param {...string} classesToAdd
  970. * One or more class name to add.
  971. *
  972. * @return {Element}
  973. * The DOM element with the added class name.
  974. */
  975. function addClass(element, ...classesToAdd) {
  976. element.classList.add(...classesToAdd.reduce((prev, current) => prev.concat(current.split(/\s+/)), []));
  977. return element;
  978. }
  979. /**
  980. * Remove a class name from an element.
  981. *
  982. * @param {Element} element
  983. * Element to remove a class name from.
  984. *
  985. * @param {...string} classesToRemove
  986. * One or more class name to remove.
  987. *
  988. * @return {Element}
  989. * The DOM element with class name removed.
  990. */
  991. function removeClass(element, ...classesToRemove) {
  992. // Protect in case the player gets disposed
  993. if (!element) {
  994. log.warn("removeClass was called with an element that doesn't exist");
  995. return null;
  996. }
  997. element.classList.remove(...classesToRemove.reduce((prev, current) => prev.concat(current.split(/\s+/)), []));
  998. return element;
  999. }
  1000. /**
  1001. * The callback definition for toggleClass.
  1002. *
  1003. * @callback module:dom~PredicateCallback
  1004. * @param {Element} element
  1005. * The DOM element of the Component.
  1006. *
  1007. * @param {string} classToToggle
  1008. * The `className` that wants to be toggled
  1009. *
  1010. * @return {boolean|undefined}
  1011. * If `true` is returned, the `classToToggle` will be added to the
  1012. * `element`. If `false`, the `classToToggle` will be removed from
  1013. * the `element`. If `undefined`, the callback will be ignored.
  1014. */
  1015. /**
  1016. * Adds or removes a class name to/from an element depending on an optional
  1017. * condition or the presence/absence of the class name.
  1018. *
  1019. * @param {Element} element
  1020. * The element to toggle a class name on.
  1021. *
  1022. * @param {string} classToToggle
  1023. * The class that should be toggled.
  1024. *
  1025. * @param {boolean|module:dom~PredicateCallback} [predicate]
  1026. * See the return value for {@link module:dom~PredicateCallback}
  1027. *
  1028. * @return {Element}
  1029. * The element with a class that has been toggled.
  1030. */
  1031. function toggleClass(element, classToToggle, predicate) {
  1032. if (typeof predicate === 'function') {
  1033. predicate = predicate(element, classToToggle);
  1034. }
  1035. if (typeof predicate !== 'boolean') {
  1036. predicate = undefined;
  1037. }
  1038. classToToggle.split(/\s+/).forEach(className => element.classList.toggle(className, predicate));
  1039. return element;
  1040. }
  1041. /**
  1042. * Apply attributes to an HTML element.
  1043. *
  1044. * @param {Element} el
  1045. * Element to add attributes to.
  1046. *
  1047. * @param {Object} [attributes]
  1048. * Attributes to be applied.
  1049. */
  1050. function setAttributes(el, attributes) {
  1051. Object.getOwnPropertyNames(attributes).forEach(function (attrName) {
  1052. const attrValue = attributes[attrName];
  1053. if (attrValue === null || typeof attrValue === 'undefined' || attrValue === false) {
  1054. el.removeAttribute(attrName);
  1055. } else {
  1056. el.setAttribute(attrName, attrValue === true ? '' : attrValue);
  1057. }
  1058. });
  1059. }
  1060. /**
  1061. * Get an element's attribute values, as defined on the HTML tag.
  1062. *
  1063. * Attributes are not the same as properties. They're defined on the tag
  1064. * or with setAttribute.
  1065. *
  1066. * @param {Element} tag
  1067. * Element from which to get tag attributes.
  1068. *
  1069. * @return {Object}
  1070. * All attributes of the element. Boolean attributes will be `true` or
  1071. * `false`, others will be strings.
  1072. */
  1073. function getAttributes(tag) {
  1074. const obj = {};
  1075. // known boolean attributes
  1076. // we can check for matching boolean properties, but not all browsers
  1077. // and not all tags know about these attributes, so, we still want to check them manually
  1078. const knownBooleans = ',' + 'autoplay,controls,playsinline,loop,muted,default,defaultMuted' + ',';
  1079. if (tag && tag.attributes && tag.attributes.length > 0) {
  1080. const attrs = tag.attributes;
  1081. for (let i = attrs.length - 1; i >= 0; i--) {
  1082. const attrName = attrs[i].name;
  1083. let attrVal = attrs[i].value;
  1084. // check for known booleans
  1085. // the matching element property will return a value for typeof
  1086. if (typeof tag[attrName] === 'boolean' || knownBooleans.indexOf(',' + attrName + ',') !== -1) {
  1087. // the value of an included boolean attribute is typically an empty
  1088. // string ('') which would equal false if we just check for a false value.
  1089. // we also don't want support bad code like autoplay='false'
  1090. attrVal = attrVal !== null ? true : false;
  1091. }
  1092. obj[attrName] = attrVal;
  1093. }
  1094. }
  1095. return obj;
  1096. }
  1097. /**
  1098. * Get the value of an element's attribute.
  1099. *
  1100. * @param {Element} el
  1101. * A DOM element.
  1102. *
  1103. * @param {string} attribute
  1104. * Attribute to get the value of.
  1105. *
  1106. * @return {string}
  1107. * The value of the attribute.
  1108. */
  1109. function getAttribute(el, attribute) {
  1110. return el.getAttribute(attribute);
  1111. }
  1112. /**
  1113. * Set the value of an element's attribute.
  1114. *
  1115. * @param {Element} el
  1116. * A DOM element.
  1117. *
  1118. * @param {string} attribute
  1119. * Attribute to set.
  1120. *
  1121. * @param {string} value
  1122. * Value to set the attribute to.
  1123. */
  1124. function setAttribute(el, attribute, value) {
  1125. el.setAttribute(attribute, value);
  1126. }
  1127. /**
  1128. * Remove an element's attribute.
  1129. *
  1130. * @param {Element} el
  1131. * A DOM element.
  1132. *
  1133. * @param {string} attribute
  1134. * Attribute to remove.
  1135. */
  1136. function removeAttribute(el, attribute) {
  1137. el.removeAttribute(attribute);
  1138. }
  1139. /**
  1140. * Attempt to block the ability to select text.
  1141. */
  1142. function blockTextSelection() {
  1143. document.body.focus();
  1144. document.onselectstart = function () {
  1145. return false;
  1146. };
  1147. }
  1148. /**
  1149. * Turn off text selection blocking.
  1150. */
  1151. function unblockTextSelection() {
  1152. document.onselectstart = function () {
  1153. return true;
  1154. };
  1155. }
  1156. /**
  1157. * Identical to the native `getBoundingClientRect` function, but ensures that
  1158. * the method is supported at all (it is in all browsers we claim to support)
  1159. * and that the element is in the DOM before continuing.
  1160. *
  1161. * This wrapper function also shims properties which are not provided by some
  1162. * older browsers (namely, IE8).
  1163. *
  1164. * Additionally, some browsers do not support adding properties to a
  1165. * `ClientRect`/`DOMRect` object; so, we shallow-copy it with the standard
  1166. * properties (except `x` and `y` which are not widely supported). This helps
  1167. * avoid implementations where keys are non-enumerable.
  1168. *
  1169. * @param {Element} el
  1170. * Element whose `ClientRect` we want to calculate.
  1171. *
  1172. * @return {Object|undefined}
  1173. * Always returns a plain object - or `undefined` if it cannot.
  1174. */
  1175. function getBoundingClientRect(el) {
  1176. if (el && el.getBoundingClientRect && el.parentNode) {
  1177. const rect = el.getBoundingClientRect();
  1178. const result = {};
  1179. ['bottom', 'height', 'left', 'right', 'top', 'width'].forEach(k => {
  1180. if (rect[k] !== undefined) {
  1181. result[k] = rect[k];
  1182. }
  1183. });
  1184. if (!result.height) {
  1185. result.height = parseFloat(computedStyle(el, 'height'));
  1186. }
  1187. if (!result.width) {
  1188. result.width = parseFloat(computedStyle(el, 'width'));
  1189. }
  1190. return result;
  1191. }
  1192. }
  1193. /**
  1194. * Represents the position of a DOM element on the page.
  1195. *
  1196. * @typedef {Object} module:dom~Position
  1197. *
  1198. * @property {number} left
  1199. * Pixels to the left.
  1200. *
  1201. * @property {number} top
  1202. * Pixels from the top.
  1203. */
  1204. /**
  1205. * Get the position of an element in the DOM.
  1206. *
  1207. * Uses `getBoundingClientRect` technique from John Resig.
  1208. *
  1209. * @see http://ejohn.org/blog/getboundingclientrect-is-awesome/
  1210. *
  1211. * @param {Element} el
  1212. * Element from which to get offset.
  1213. *
  1214. * @return {module:dom~Position}
  1215. * The position of the element that was passed in.
  1216. */
  1217. function findPosition(el) {
  1218. if (!el || el && !el.offsetParent) {
  1219. return {
  1220. left: 0,
  1221. top: 0,
  1222. width: 0,
  1223. height: 0
  1224. };
  1225. }
  1226. const width = el.offsetWidth;
  1227. const height = el.offsetHeight;
  1228. let left = 0;
  1229. let top = 0;
  1230. while (el.offsetParent && el !== document[FullscreenApi.fullscreenElement]) {
  1231. left += el.offsetLeft;
  1232. top += el.offsetTop;
  1233. el = el.offsetParent;
  1234. }
  1235. return {
  1236. left,
  1237. top,
  1238. width,
  1239. height
  1240. };
  1241. }
  1242. /**
  1243. * Represents x and y coordinates for a DOM element or mouse pointer.
  1244. *
  1245. * @typedef {Object} module:dom~Coordinates
  1246. *
  1247. * @property {number} x
  1248. * x coordinate in pixels
  1249. *
  1250. * @property {number} y
  1251. * y coordinate in pixels
  1252. */
  1253. /**
  1254. * Get the pointer position within an element.
  1255. *
  1256. * The base on the coordinates are the bottom left of the element.
  1257. *
  1258. * @param {Element} el
  1259. * Element on which to get the pointer position on.
  1260. *
  1261. * @param {Event} event
  1262. * Event object.
  1263. *
  1264. * @return {module:dom~Coordinates}
  1265. * A coordinates object corresponding to the mouse position.
  1266. *
  1267. */
  1268. function getPointerPosition(el, event) {
  1269. const translated = {
  1270. x: 0,
  1271. y: 0
  1272. };
  1273. if (IS_IOS) {
  1274. let item = el;
  1275. while (item && item.nodeName.toLowerCase() !== 'html') {
  1276. const transform = computedStyle(item, 'transform');
  1277. if (/^matrix/.test(transform)) {
  1278. const values = transform.slice(7, -1).split(/,\s/).map(Number);
  1279. translated.x += values[4];
  1280. translated.y += values[5];
  1281. } else if (/^matrix3d/.test(transform)) {
  1282. const values = transform.slice(9, -1).split(/,\s/).map(Number);
  1283. translated.x += values[12];
  1284. translated.y += values[13];
  1285. }
  1286. item = item.parentNode;
  1287. }
  1288. }
  1289. const position = {};
  1290. const boxTarget = findPosition(event.target);
  1291. const box = findPosition(el);
  1292. const boxW = box.width;
  1293. const boxH = box.height;
  1294. let offsetY = event.offsetY - (box.top - boxTarget.top);
  1295. let offsetX = event.offsetX - (box.left - boxTarget.left);
  1296. if (event.changedTouches) {
  1297. offsetX = event.changedTouches[0].pageX - box.left;
  1298. offsetY = event.changedTouches[0].pageY + box.top;
  1299. if (IS_IOS) {
  1300. offsetX -= translated.x;
  1301. offsetY -= translated.y;
  1302. }
  1303. }
  1304. position.y = 1 - Math.max(0, Math.min(1, offsetY / boxH));
  1305. position.x = Math.max(0, Math.min(1, offsetX / boxW));
  1306. return position;
  1307. }
  1308. /**
  1309. * Determines, via duck typing, whether or not a value is a text node.
  1310. *
  1311. * @param {*} value
  1312. * Check if this value is a text node.
  1313. *
  1314. * @return {boolean}
  1315. * Will be `true` if the value is a text node, `false` otherwise.
  1316. */
  1317. function isTextNode(value) {
  1318. return isObject(value) && value.nodeType === 3;
  1319. }
  1320. /**
  1321. * Empties the contents of an element.
  1322. *
  1323. * @param {Element} el
  1324. * The element to empty children from
  1325. *
  1326. * @return {Element}
  1327. * The element with no children
  1328. */
  1329. function emptyEl(el) {
  1330. while (el.firstChild) {
  1331. el.removeChild(el.firstChild);
  1332. }
  1333. return el;
  1334. }
  1335. /**
  1336. * This is a mixed value that describes content to be injected into the DOM
  1337. * via some method. It can be of the following types:
  1338. *
  1339. * Type | Description
  1340. * -----------|-------------
  1341. * `string` | The value will be normalized into a text node.
  1342. * `Element` | The value will be accepted as-is.
  1343. * `Text` | A TextNode. The value will be accepted as-is.
  1344. * `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).
  1345. * `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.
  1346. *
  1347. * @typedef {string|Element|Text|Array|Function} ContentDescriptor
  1348. */
  1349. /**
  1350. * Normalizes content for eventual insertion into the DOM.
  1351. *
  1352. * This allows a wide range of content definition methods, but helps protect
  1353. * from falling into the trap of simply writing to `innerHTML`, which could
  1354. * be an XSS concern.
  1355. *
  1356. * The content for an element can be passed in multiple types and
  1357. * combinations, whose behavior is as follows:
  1358. *
  1359. * @param {ContentDescriptor} content
  1360. * A content descriptor value.
  1361. *
  1362. * @return {Array}
  1363. * All of the content that was passed in, normalized to an array of
  1364. * elements or text nodes.
  1365. */
  1366. function normalizeContent(content) {
  1367. // First, invoke content if it is a function. If it produces an array,
  1368. // that needs to happen before normalization.
  1369. if (typeof content === 'function') {
  1370. content = content();
  1371. }
  1372. // Next up, normalize to an array, so one or many items can be normalized,
  1373. // filtered, and returned.
  1374. return (Array.isArray(content) ? content : [content]).map(value => {
  1375. // First, invoke value if it is a function to produce a new value,
  1376. // which will be subsequently normalized to a Node of some kind.
  1377. if (typeof value === 'function') {
  1378. value = value();
  1379. }
  1380. if (isEl(value) || isTextNode(value)) {
  1381. return value;
  1382. }
  1383. if (typeof value === 'string' && /\S/.test(value)) {
  1384. return document.createTextNode(value);
  1385. }
  1386. }).filter(value => value);
  1387. }
  1388. /**
  1389. * Normalizes and appends content to an element.
  1390. *
  1391. * @param {Element} el
  1392. * Element to append normalized content to.
  1393. *
  1394. * @param {ContentDescriptor} content
  1395. * A content descriptor value.
  1396. *
  1397. * @return {Element}
  1398. * The element with appended normalized content.
  1399. */
  1400. function appendContent(el, content) {
  1401. normalizeContent(content).forEach(node => el.appendChild(node));
  1402. return el;
  1403. }
  1404. /**
  1405. * Normalizes and inserts content into an element; this is identical to
  1406. * `appendContent()`, except it empties the element first.
  1407. *
  1408. * @param {Element} el
  1409. * Element to insert normalized content into.
  1410. *
  1411. * @param {ContentDescriptor} content
  1412. * A content descriptor value.
  1413. *
  1414. * @return {Element}
  1415. * The element with inserted normalized content.
  1416. */
  1417. function insertContent(el, content) {
  1418. return appendContent(emptyEl(el), content);
  1419. }
  1420. /**
  1421. * Check if an event was a single left click.
  1422. *
  1423. * @param {Event} event
  1424. * Event object.
  1425. *
  1426. * @return {boolean}
  1427. * Will be `true` if a single left click, `false` otherwise.
  1428. */
  1429. function isSingleLeftClick(event) {
  1430. // Note: if you create something draggable, be sure to
  1431. // call it on both `mousedown` and `mousemove` event,
  1432. // otherwise `mousedown` should be enough for a button
  1433. if (event.button === undefined && event.buttons === undefined) {
  1434. // Why do we need `buttons` ?
  1435. // Because, middle mouse sometimes have this:
  1436. // e.button === 0 and e.buttons === 4
  1437. // Furthermore, we want to prevent combination click, something like
  1438. // HOLD middlemouse then left click, that would be
  1439. // e.button === 0, e.buttons === 5
  1440. // just `button` is not gonna work
  1441. // Alright, then what this block does ?
  1442. // this is for chrome `simulate mobile devices`
  1443. // I want to support this as well
  1444. return true;
  1445. }
  1446. if (event.button === 0 && event.buttons === undefined) {
  1447. // Touch screen, sometimes on some specific device, `buttons`
  1448. // doesn't have anything (safari on ios, blackberry...)
  1449. return true;
  1450. }
  1451. // `mouseup` event on a single left click has
  1452. // `button` and `buttons` equal to 0
  1453. if (event.type === 'mouseup' && event.button === 0 && event.buttons === 0) {
  1454. return true;
  1455. }
  1456. if (event.button !== 0 || event.buttons !== 1) {
  1457. // This is the reason we have those if else block above
  1458. // if any special case we can catch and let it slide
  1459. // we do it above, when get to here, this definitely
  1460. // is-not-left-click
  1461. return false;
  1462. }
  1463. return true;
  1464. }
  1465. /**
  1466. * Finds a single DOM element matching `selector` within the optional
  1467. * `context` of another DOM element (defaulting to `document`).
  1468. *
  1469. * @param {string} selector
  1470. * A valid CSS selector, which will be passed to `querySelector`.
  1471. *
  1472. * @param {Element|String} [context=document]
  1473. * A DOM element within which to query. Can also be a selector
  1474. * string in which case the first matching element will be used
  1475. * as context. If missing (or no element matches selector), falls
  1476. * back to `document`.
  1477. *
  1478. * @return {Element|null}
  1479. * The element that was found or null.
  1480. */
  1481. const $ = createQuerier('querySelector');
  1482. /**
  1483. * Finds a all DOM elements matching `selector` within the optional
  1484. * `context` of another DOM element (defaulting to `document`).
  1485. *
  1486. * @param {string} selector
  1487. * A valid CSS selector, which will be passed to `querySelectorAll`.
  1488. *
  1489. * @param {Element|String} [context=document]
  1490. * A DOM element within which to query. Can also be a selector
  1491. * string in which case the first matching element will be used
  1492. * as context. If missing (or no element matches selector), falls
  1493. * back to `document`.
  1494. *
  1495. * @return {NodeList}
  1496. * A element list of elements that were found. Will be empty if none
  1497. * were found.
  1498. *
  1499. */
  1500. const $$ = createQuerier('querySelectorAll');
  1501. /**
  1502. * A safe getComputedStyle.
  1503. *
  1504. * This is needed because in Firefox, if the player is loaded in an iframe with
  1505. * `display:none`, then `getComputedStyle` returns `null`, so, we do a
  1506. * null-check to make sure that the player doesn't break in these cases.
  1507. *
  1508. * @param {Element} el
  1509. * The element you want the computed style of
  1510. *
  1511. * @param {string} prop
  1512. * The property name you want
  1513. *
  1514. * @see https://bugzilla.mozilla.org/show_bug.cgi?id=548397
  1515. */
  1516. function computedStyle(el, prop) {
  1517. if (!el || !prop) {
  1518. return '';
  1519. }
  1520. if (typeof window.getComputedStyle === 'function') {
  1521. let computedStyleValue;
  1522. try {
  1523. computedStyleValue = window.getComputedStyle(el);
  1524. } catch (e) {
  1525. return '';
  1526. }
  1527. return computedStyleValue ? computedStyleValue.getPropertyValue(prop) || computedStyleValue[prop] : '';
  1528. }
  1529. return '';
  1530. }
  1531. var Dom = /*#__PURE__*/Object.freeze({
  1532. __proto__: null,
  1533. isReal: isReal,
  1534. isEl: isEl,
  1535. isInFrame: isInFrame,
  1536. createEl: createEl,
  1537. textContent: textContent,
  1538. prependTo: prependTo,
  1539. hasClass: hasClass,
  1540. addClass: addClass,
  1541. removeClass: removeClass,
  1542. toggleClass: toggleClass,
  1543. setAttributes: setAttributes,
  1544. getAttributes: getAttributes,
  1545. getAttribute: getAttribute,
  1546. setAttribute: setAttribute,
  1547. removeAttribute: removeAttribute,
  1548. blockTextSelection: blockTextSelection,
  1549. unblockTextSelection: unblockTextSelection,
  1550. getBoundingClientRect: getBoundingClientRect,
  1551. findPosition: findPosition,
  1552. getPointerPosition: getPointerPosition,
  1553. isTextNode: isTextNode,
  1554. emptyEl: emptyEl,
  1555. normalizeContent: normalizeContent,
  1556. appendContent: appendContent,
  1557. insertContent: insertContent,
  1558. isSingleLeftClick: isSingleLeftClick,
  1559. $: $,
  1560. $$: $$,
  1561. computedStyle: computedStyle
  1562. });
  1563. /**
  1564. * @file setup.js - Functions for setting up a player without
  1565. * user interaction based on the data-setup `attribute` of the video tag.
  1566. *
  1567. * @module setup
  1568. */
  1569. let _windowLoaded = false;
  1570. let videojs$1;
  1571. /**
  1572. * Set up any tags that have a data-setup `attribute` when the player is started.
  1573. */
  1574. const autoSetup = function () {
  1575. if (videojs$1.options.autoSetup === false) {
  1576. return;
  1577. }
  1578. const vids = Array.prototype.slice.call(document.getElementsByTagName('video'));
  1579. const audios = Array.prototype.slice.call(document.getElementsByTagName('audio'));
  1580. const divs = Array.prototype.slice.call(document.getElementsByTagName('video-js'));
  1581. const mediaEls = vids.concat(audios, divs);
  1582. // Check if any media elements exist
  1583. if (mediaEls && mediaEls.length > 0) {
  1584. for (let i = 0, e = mediaEls.length; i < e; i++) {
  1585. const mediaEl = mediaEls[i];
  1586. // Check if element exists, has getAttribute func.
  1587. if (mediaEl && mediaEl.getAttribute) {
  1588. // Make sure this player hasn't already been set up.
  1589. if (mediaEl.player === undefined) {
  1590. const options = mediaEl.getAttribute('data-setup');
  1591. // Check if data-setup attr exists.
  1592. // We only auto-setup if they've added the data-setup attr.
  1593. if (options !== null) {
  1594. // Create new video.js instance.
  1595. videojs$1(mediaEl);
  1596. }
  1597. }
  1598. // If getAttribute isn't defined, we need to wait for the DOM.
  1599. } else {
  1600. autoSetupTimeout(1);
  1601. break;
  1602. }
  1603. }
  1604. // No videos were found, so keep looping unless page is finished loading.
  1605. } else if (!_windowLoaded) {
  1606. autoSetupTimeout(1);
  1607. }
  1608. };
  1609. /**
  1610. * Wait until the page is loaded before running autoSetup. This will be called in
  1611. * autoSetup if `hasLoaded` returns false.
  1612. *
  1613. * @param {number} wait
  1614. * How long to wait in ms
  1615. *
  1616. * @param {module:videojs} [vjs]
  1617. * The videojs library function
  1618. */
  1619. function autoSetupTimeout(wait, vjs) {
  1620. // Protect against breakage in non-browser environments
  1621. if (!isReal()) {
  1622. return;
  1623. }
  1624. if (vjs) {
  1625. videojs$1 = vjs;
  1626. }
  1627. window.setTimeout(autoSetup, wait);
  1628. }
  1629. /**
  1630. * Used to set the internal tracking of window loaded state to true.
  1631. *
  1632. * @private
  1633. */
  1634. function setWindowLoaded() {
  1635. _windowLoaded = true;
  1636. window.removeEventListener('load', setWindowLoaded);
  1637. }
  1638. if (isReal()) {
  1639. if (document.readyState === 'complete') {
  1640. setWindowLoaded();
  1641. } else {
  1642. /**
  1643. * Listen for the load event on window, and set _windowLoaded to true.
  1644. *
  1645. * We use a standard event listener here to avoid incrementing the GUID
  1646. * before any players are created.
  1647. *
  1648. * @listens load
  1649. */
  1650. window.addEventListener('load', setWindowLoaded);
  1651. }
  1652. }
  1653. /**
  1654. * @file stylesheet.js
  1655. * @module stylesheet
  1656. */
  1657. /**
  1658. * Create a DOM style element given a className for it.
  1659. *
  1660. * @param {string} className
  1661. * The className to add to the created style element.
  1662. *
  1663. * @return {Element}
  1664. * The element that was created.
  1665. */
  1666. const createStyleElement = function (className) {
  1667. const style = document.createElement('style');
  1668. style.className = className;
  1669. return style;
  1670. };
  1671. /**
  1672. * Add text to a DOM element.
  1673. *
  1674. * @param {Element} el
  1675. * The Element to add text content to.
  1676. *
  1677. * @param {string} content
  1678. * The text to add to the element.
  1679. */
  1680. const setTextContent = function (el, content) {
  1681. if (el.styleSheet) {
  1682. el.styleSheet.cssText = content;
  1683. } else {
  1684. el.textContent = content;
  1685. }
  1686. };
  1687. /**
  1688. * @file dom-data.js
  1689. * @module dom-data
  1690. */
  1691. /**
  1692. * Element Data Store.
  1693. *
  1694. * Allows for binding data to an element without putting it directly on the
  1695. * element. Ex. Event listeners are stored here.
  1696. * (also from jsninja.com, slightly modified and updated for closure compiler)
  1697. *
  1698. * @type {Object}
  1699. * @private
  1700. */
  1701. var DomData = new WeakMap();
  1702. /**
  1703. * @file guid.js
  1704. * @module guid
  1705. */
  1706. // Default value for GUIDs. This allows us to reset the GUID counter in tests.
  1707. //
  1708. // The initial GUID is 3 because some users have come to rely on the first
  1709. // default player ID ending up as `vjs_video_3`.
  1710. //
  1711. // See: https://github.com/videojs/video.js/pull/6216
  1712. const _initialGuid = 3;
  1713. /**
  1714. * Unique ID for an element or function
  1715. *
  1716. * @type {Number}
  1717. */
  1718. let _guid = _initialGuid;
  1719. /**
  1720. * Get a unique auto-incrementing ID by number that has not been returned before.
  1721. *
  1722. * @return {number}
  1723. * A new unique ID.
  1724. */
  1725. function newGUID() {
  1726. return _guid++;
  1727. }
  1728. /**
  1729. * @file events.js. An Event System (John Resig - Secrets of a JS Ninja http://jsninja.com/)
  1730. * (Original book version wasn't completely usable, so fixed some things and made Closure Compiler compatible)
  1731. * This should work very similarly to jQuery's events, however it's based off the book version which isn't as
  1732. * robust as jquery's, so there's probably some differences.
  1733. *
  1734. * @file events.js
  1735. * @module events
  1736. */
  1737. /**
  1738. * Clean up the listener cache and dispatchers
  1739. *
  1740. * @param {Element|Object} elem
  1741. * Element to clean up
  1742. *
  1743. * @param {string} type
  1744. * Type of event to clean up
  1745. */
  1746. function _cleanUpEvents(elem, type) {
  1747. if (!DomData.has(elem)) {
  1748. return;
  1749. }
  1750. const data = DomData.get(elem);
  1751. // Remove the events of a particular type if there are none left
  1752. if (data.handlers[type].length === 0) {
  1753. delete data.handlers[type];
  1754. // data.handlers[type] = null;
  1755. // Setting to null was causing an error with data.handlers
  1756. // Remove the meta-handler from the element
  1757. if (elem.removeEventListener) {
  1758. elem.removeEventListener(type, data.dispatcher, false);
  1759. } else if (elem.detachEvent) {
  1760. elem.detachEvent('on' + type, data.dispatcher);
  1761. }
  1762. }
  1763. // Remove the events object if there are no types left
  1764. if (Object.getOwnPropertyNames(data.handlers).length <= 0) {
  1765. delete data.handlers;
  1766. delete data.dispatcher;
  1767. delete data.disabled;
  1768. }
  1769. // Finally remove the element data if there is no data left
  1770. if (Object.getOwnPropertyNames(data).length === 0) {
  1771. DomData.delete(elem);
  1772. }
  1773. }
  1774. /**
  1775. * Loops through an array of event types and calls the requested method for each type.
  1776. *
  1777. * @param {Function} fn
  1778. * The event method we want to use.
  1779. *
  1780. * @param {Element|Object} elem
  1781. * Element or object to bind listeners to
  1782. *
  1783. * @param {string} type
  1784. * Type of event to bind to.
  1785. *
  1786. * @param {Function} callback
  1787. * Event listener.
  1788. */
  1789. function _handleMultipleEvents(fn, elem, types, callback) {
  1790. types.forEach(function (type) {
  1791. // Call the event method for each one of the types
  1792. fn(elem, type, callback);
  1793. });
  1794. }
  1795. /**
  1796. * Fix a native event to have standard property values
  1797. *
  1798. * @param {Object} event
  1799. * Event object to fix.
  1800. *
  1801. * @return {Object}
  1802. * Fixed event object.
  1803. */
  1804. function fixEvent(event) {
  1805. if (event.fixed_) {
  1806. return event;
  1807. }
  1808. function returnTrue() {
  1809. return true;
  1810. }
  1811. function returnFalse() {
  1812. return false;
  1813. }
  1814. // Test if fixing up is needed
  1815. // Used to check if !event.stopPropagation instead of isPropagationStopped
  1816. // But native events return true for stopPropagation, but don't have
  1817. // other expected methods like isPropagationStopped. Seems to be a problem
  1818. // with the Javascript Ninja code. So we're just overriding all events now.
  1819. if (!event || !event.isPropagationStopped || !event.isImmediatePropagationStopped) {
  1820. const old = event || window.event;
  1821. event = {};
  1822. // Clone the old object so that we can modify the values event = {};
  1823. // IE8 Doesn't like when you mess with native event properties
  1824. // Firefox returns false for event.hasOwnProperty('type') and other props
  1825. // which makes copying more difficult.
  1826. // TODO: Probably best to create a whitelist of event props
  1827. for (const key in old) {
  1828. // Safari 6.0.3 warns you if you try to copy deprecated layerX/Y
  1829. // Chrome warns you if you try to copy deprecated keyboardEvent.keyLocation
  1830. // and webkitMovementX/Y
  1831. // Lighthouse complains if Event.path is copied
  1832. if (key !== 'layerX' && key !== 'layerY' && key !== 'keyLocation' && key !== 'webkitMovementX' && key !== 'webkitMovementY' && key !== 'path') {
  1833. // Chrome 32+ warns if you try to copy deprecated returnValue, but
  1834. // we still want to if preventDefault isn't supported (IE8).
  1835. if (!(key === 'returnValue' && old.preventDefault)) {
  1836. event[key] = old[key];
  1837. }
  1838. }
  1839. }
  1840. // The event occurred on this element
  1841. if (!event.target) {
  1842. event.target = event.srcElement || document;
  1843. }
  1844. // Handle which other element the event is related to
  1845. if (!event.relatedTarget) {
  1846. event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
  1847. }
  1848. // Stop the default browser action
  1849. event.preventDefault = function () {
  1850. if (old.preventDefault) {
  1851. old.preventDefault();
  1852. }
  1853. event.returnValue = false;
  1854. old.returnValue = false;
  1855. event.defaultPrevented = true;
  1856. };
  1857. event.defaultPrevented = false;
  1858. // Stop the event from bubbling
  1859. event.stopPropagation = function () {
  1860. if (old.stopPropagation) {
  1861. old.stopPropagation();
  1862. }
  1863. event.cancelBubble = true;
  1864. old.cancelBubble = true;
  1865. event.isPropagationStopped = returnTrue;
  1866. };
  1867. event.isPropagationStopped = returnFalse;
  1868. // Stop the event from bubbling and executing other handlers
  1869. event.stopImmediatePropagation = function () {
  1870. if (old.stopImmediatePropagation) {
  1871. old.stopImmediatePropagation();
  1872. }
  1873. event.isImmediatePropagationStopped = returnTrue;
  1874. event.stopPropagation();
  1875. };
  1876. event.isImmediatePropagationStopped = returnFalse;
  1877. // Handle mouse position
  1878. if (event.clientX !== null && event.clientX !== undefined) {
  1879. const doc = document.documentElement;
  1880. const body = document.body;
  1881. event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
  1882. event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
  1883. }
  1884. // Handle key presses
  1885. event.which = event.charCode || event.keyCode;
  1886. // Fix button for mouse clicks:
  1887. // 0 == left; 1 == middle; 2 == right
  1888. if (event.button !== null && event.button !== undefined) {
  1889. // The following is disabled because it does not pass videojs-standard
  1890. // and... yikes.
  1891. /* eslint-disable */
  1892. event.button = event.button & 1 ? 0 : event.button & 4 ? 1 : event.button & 2 ? 2 : 0;
  1893. /* eslint-enable */
  1894. }
  1895. }
  1896. event.fixed_ = true;
  1897. // Returns fixed-up instance
  1898. return event;
  1899. }
  1900. /**
  1901. * Whether passive event listeners are supported
  1902. */
  1903. let _supportsPassive;
  1904. const supportsPassive = function () {
  1905. if (typeof _supportsPassive !== 'boolean') {
  1906. _supportsPassive = false;
  1907. try {
  1908. const opts = Object.defineProperty({}, 'passive', {
  1909. get() {
  1910. _supportsPassive = true;
  1911. }
  1912. });
  1913. window.addEventListener('test', null, opts);
  1914. window.removeEventListener('test', null, opts);
  1915. } catch (e) {
  1916. // disregard
  1917. }
  1918. }
  1919. return _supportsPassive;
  1920. };
  1921. /**
  1922. * Touch events Chrome expects to be passive
  1923. */
  1924. const passiveEvents = ['touchstart', 'touchmove'];
  1925. /**
  1926. * Add an event listener to element
  1927. * It stores the handler function in a separate cache object
  1928. * and adds a generic handler to the element's event,
  1929. * along with a unique id (guid) to the element.
  1930. *
  1931. * @param {Element|Object} elem
  1932. * Element or object to bind listeners to
  1933. *
  1934. * @param {string|string[]} type
  1935. * Type of event to bind to.
  1936. *
  1937. * @param {Function} fn
  1938. * Event listener.
  1939. */
  1940. function on(elem, type, fn) {
  1941. if (Array.isArray(type)) {
  1942. return _handleMultipleEvents(on, elem, type, fn);
  1943. }
  1944. if (!DomData.has(elem)) {
  1945. DomData.set(elem, {});
  1946. }
  1947. const data = DomData.get(elem);
  1948. // We need a place to store all our handler data
  1949. if (!data.handlers) {
  1950. data.handlers = {};
  1951. }
  1952. if (!data.handlers[type]) {
  1953. data.handlers[type] = [];
  1954. }
  1955. if (!fn.guid) {
  1956. fn.guid = newGUID();
  1957. }
  1958. data.handlers[type].push(fn);
  1959. if (!data.dispatcher) {
  1960. data.disabled = false;
  1961. data.dispatcher = function (event, hash) {
  1962. if (data.disabled) {
  1963. return;
  1964. }
  1965. event = fixEvent(event);
  1966. const handlers = data.handlers[event.type];
  1967. if (handlers) {
  1968. // Copy handlers so if handlers are added/removed during the process it doesn't throw everything off.
  1969. const handlersCopy = handlers.slice(0);
  1970. for (let m = 0, n = handlersCopy.length; m < n; m++) {
  1971. if (event.isImmediatePropagationStopped()) {
  1972. break;
  1973. } else {
  1974. try {
  1975. handlersCopy[m].call(elem, event, hash);
  1976. } catch (e) {
  1977. log.error(e);
  1978. }
  1979. }
  1980. }
  1981. }
  1982. };
  1983. }
  1984. if (data.handlers[type].length === 1) {
  1985. if (elem.addEventListener) {
  1986. let options = false;
  1987. if (supportsPassive() && passiveEvents.indexOf(type) > -1) {
  1988. options = {
  1989. passive: true
  1990. };
  1991. }
  1992. elem.addEventListener(type, data.dispatcher, options);
  1993. } else if (elem.attachEvent) {
  1994. elem.attachEvent('on' + type, data.dispatcher);
  1995. }
  1996. }
  1997. }
  1998. /**
  1999. * Removes event listeners from an element
  2000. *
  2001. * @param {Element|Object} elem
  2002. * Object to remove listeners from.
  2003. *
  2004. * @param {string|string[]} [type]
  2005. * Type of listener to remove. Don't include to remove all events from element.
  2006. *
  2007. * @param {Function} [fn]
  2008. * Specific listener to remove. Don't include to remove listeners for an event
  2009. * type.
  2010. */
  2011. function off(elem, type, fn) {
  2012. // Don't want to add a cache object through getElData if not needed
  2013. if (!DomData.has(elem)) {
  2014. return;
  2015. }
  2016. const data = DomData.get(elem);
  2017. // If no events exist, nothing to unbind
  2018. if (!data.handlers) {
  2019. return;
  2020. }
  2021. if (Array.isArray(type)) {
  2022. return _handleMultipleEvents(off, elem, type, fn);
  2023. }
  2024. // Utility function
  2025. const removeType = function (el, t) {
  2026. data.handlers[t] = [];
  2027. _cleanUpEvents(el, t);
  2028. };
  2029. // Are we removing all bound events?
  2030. if (type === undefined) {
  2031. for (const t in data.handlers) {
  2032. if (Object.prototype.hasOwnProperty.call(data.handlers || {}, t)) {
  2033. removeType(elem, t);
  2034. }
  2035. }
  2036. return;
  2037. }
  2038. const handlers = data.handlers[type];
  2039. // If no handlers exist, nothing to unbind
  2040. if (!handlers) {
  2041. return;
  2042. }
  2043. // If no listener was provided, remove all listeners for type
  2044. if (!fn) {
  2045. removeType(elem, type);
  2046. return;
  2047. }
  2048. // We're only removing a single handler
  2049. if (fn.guid) {
  2050. for (let n = 0; n < handlers.length; n++) {
  2051. if (handlers[n].guid === fn.guid) {
  2052. handlers.splice(n--, 1);
  2053. }
  2054. }
  2055. }
  2056. _cleanUpEvents(elem, type);
  2057. }
  2058. /**
  2059. * Trigger an event for an element
  2060. *
  2061. * @param {Element|Object} elem
  2062. * Element to trigger an event on
  2063. *
  2064. * @param {EventTarget~Event|string} event
  2065. * A string (the type) or an event object with a type attribute
  2066. *
  2067. * @param {Object} [hash]
  2068. * data hash to pass along with the event
  2069. *
  2070. * @return {boolean|undefined}
  2071. * Returns the opposite of `defaultPrevented` if default was
  2072. * prevented. Otherwise, returns `undefined`
  2073. */
  2074. function trigger(elem, event, hash) {
  2075. // Fetches element data and a reference to the parent (for bubbling).
  2076. // Don't want to add a data object to cache for every parent,
  2077. // so checking hasElData first.
  2078. const elemData = DomData.has(elem) ? DomData.get(elem) : {};
  2079. const parent = elem.parentNode || elem.ownerDocument;
  2080. // type = event.type || event,
  2081. // handler;
  2082. // If an event name was passed as a string, creates an event out of it
  2083. if (typeof event === 'string') {
  2084. event = {
  2085. type: event,
  2086. target: elem
  2087. };
  2088. } else if (!event.target) {
  2089. event.target = elem;
  2090. }
  2091. // Normalizes the event properties.
  2092. event = fixEvent(event);
  2093. // If the passed element has a dispatcher, executes the established handlers.
  2094. if (elemData.dispatcher) {
  2095. elemData.dispatcher.call(elem, event, hash);
  2096. }
  2097. // Unless explicitly stopped or the event does not bubble (e.g. media events)
  2098. // recursively calls this function to bubble the event up the DOM.
  2099. if (parent && !event.isPropagationStopped() && event.bubbles === true) {
  2100. trigger.call(null, parent, event, hash);
  2101. // If at the top of the DOM, triggers the default action unless disabled.
  2102. } else if (!parent && !event.defaultPrevented && event.target && event.target[event.type]) {
  2103. if (!DomData.has(event.target)) {
  2104. DomData.set(event.target, {});
  2105. }
  2106. const targetData = DomData.get(event.target);
  2107. // Checks if the target has a default action for this event.
  2108. if (event.target[event.type]) {
  2109. // Temporarily disables event dispatching on the target as we have already executed the handler.
  2110. targetData.disabled = true;
  2111. // Executes the default action.
  2112. if (typeof event.target[event.type] === 'function') {
  2113. event.target[event.type]();
  2114. }
  2115. // Re-enables event dispatching.
  2116. targetData.disabled = false;
  2117. }
  2118. }
  2119. // Inform the triggerer if the default was prevented by returning false
  2120. return !event.defaultPrevented;
  2121. }
  2122. /**
  2123. * Trigger a listener only once for an event.
  2124. *
  2125. * @param {Element|Object} elem
  2126. * Element or object to bind to.
  2127. *
  2128. * @param {string|string[]} type
  2129. * Name/type of event
  2130. *
  2131. * @param {Event~EventListener} fn
  2132. * Event listener function
  2133. */
  2134. function one(elem, type, fn) {
  2135. if (Array.isArray(type)) {
  2136. return _handleMultipleEvents(one, elem, type, fn);
  2137. }
  2138. const func = function () {
  2139. off(elem, type, func);
  2140. fn.apply(this, arguments);
  2141. };
  2142. // copy the guid to the new function so it can removed using the original function's ID
  2143. func.guid = fn.guid = fn.guid || newGUID();
  2144. on(elem, type, func);
  2145. }
  2146. /**
  2147. * Trigger a listener only once and then turn if off for all
  2148. * configured events
  2149. *
  2150. * @param {Element|Object} elem
  2151. * Element or object to bind to.
  2152. *
  2153. * @param {string|string[]} type
  2154. * Name/type of event
  2155. *
  2156. * @param {Event~EventListener} fn
  2157. * Event listener function
  2158. */
  2159. function any(elem, type, fn) {
  2160. const func = function () {
  2161. off(elem, type, func);
  2162. fn.apply(this, arguments);
  2163. };
  2164. // copy the guid to the new function so it can removed using the original function's ID
  2165. func.guid = fn.guid = fn.guid || newGUID();
  2166. // multiple ons, but one off for everything
  2167. on(elem, type, func);
  2168. }
  2169. var Events = /*#__PURE__*/Object.freeze({
  2170. __proto__: null,
  2171. fixEvent: fixEvent,
  2172. on: on,
  2173. off: off,
  2174. trigger: trigger,
  2175. one: one,
  2176. any: any
  2177. });
  2178. /**
  2179. * @file fn.js
  2180. * @module fn
  2181. */
  2182. const UPDATE_REFRESH_INTERVAL = 30;
  2183. /**
  2184. * A private, internal-only function for changing the context of a function.
  2185. *
  2186. * It also stores a unique id on the function so it can be easily removed from
  2187. * events.
  2188. *
  2189. * @private
  2190. * @function
  2191. * @param {*} context
  2192. * The object to bind as scope.
  2193. *
  2194. * @param {Function} fn
  2195. * The function to be bound to a scope.
  2196. *
  2197. * @param {number} [uid]
  2198. * An optional unique ID for the function to be set
  2199. *
  2200. * @return {Function}
  2201. * The new function that will be bound into the context given
  2202. */
  2203. const bind_ = function (context, fn, uid) {
  2204. // Make sure the function has a unique ID
  2205. if (!fn.guid) {
  2206. fn.guid = newGUID();
  2207. }
  2208. // Create the new function that changes the context
  2209. const bound = fn.bind(context);
  2210. // Allow for the ability to individualize this function
  2211. // Needed in the case where multiple objects might share the same prototype
  2212. // IF both items add an event listener with the same function, then you try to remove just one
  2213. // it will remove both because they both have the same guid.
  2214. // when using this, you need to use the bind method when you remove the listener as well.
  2215. // currently used in text tracks
  2216. bound.guid = uid ? uid + '_' + fn.guid : fn.guid;
  2217. return bound;
  2218. };
  2219. /**
  2220. * Wraps the given function, `fn`, with a new function that only invokes `fn`
  2221. * at most once per every `wait` milliseconds.
  2222. *
  2223. * @function
  2224. * @param {Function} fn
  2225. * The function to be throttled.
  2226. *
  2227. * @param {number} wait
  2228. * The number of milliseconds by which to throttle.
  2229. *
  2230. * @return {Function}
  2231. */
  2232. const throttle = function (fn, wait) {
  2233. let last = window.performance.now();
  2234. const throttled = function (...args) {
  2235. const now = window.performance.now();
  2236. if (now - last >= wait) {
  2237. fn(...args);
  2238. last = now;
  2239. }
  2240. };
  2241. return throttled;
  2242. };
  2243. /**
  2244. * Creates a debounced function that delays invoking `func` until after `wait`
  2245. * milliseconds have elapsed since the last time the debounced function was
  2246. * invoked.
  2247. *
  2248. * Inspired by lodash and underscore implementations.
  2249. *
  2250. * @function
  2251. * @param {Function} func
  2252. * The function to wrap with debounce behavior.
  2253. *
  2254. * @param {number} wait
  2255. * The number of milliseconds to wait after the last invocation.
  2256. *
  2257. * @param {boolean} [immediate]
  2258. * Whether or not to invoke the function immediately upon creation.
  2259. *
  2260. * @param {Object} [context=window]
  2261. * The "context" in which the debounced function should debounce. For
  2262. * example, if this function should be tied to a Video.js player,
  2263. * the player can be passed here. Alternatively, defaults to the
  2264. * global `window` object.
  2265. *
  2266. * @return {Function}
  2267. * A debounced function.
  2268. */
  2269. const debounce = function (func, wait, immediate, context = window) {
  2270. let timeout;
  2271. const cancel = () => {
  2272. context.clearTimeout(timeout);
  2273. timeout = null;
  2274. };
  2275. /* eslint-disable consistent-this */
  2276. const debounced = function () {
  2277. const self = this;
  2278. const args = arguments;
  2279. let later = function () {
  2280. timeout = null;
  2281. later = null;
  2282. if (!immediate) {
  2283. func.apply(self, args);
  2284. }
  2285. };
  2286. if (!timeout && immediate) {
  2287. func.apply(self, args);
  2288. }
  2289. context.clearTimeout(timeout);
  2290. timeout = context.setTimeout(later, wait);
  2291. };
  2292. /* eslint-enable consistent-this */
  2293. debounced.cancel = cancel;
  2294. return debounced;
  2295. };
  2296. var Fn = /*#__PURE__*/Object.freeze({
  2297. __proto__: null,
  2298. UPDATE_REFRESH_INTERVAL: UPDATE_REFRESH_INTERVAL,
  2299. bind_: bind_,
  2300. throttle: throttle,
  2301. debounce: debounce
  2302. });
  2303. /**
  2304. * @file src/js/event-target.js
  2305. */
  2306. let EVENT_MAP;
  2307. /**
  2308. * `EventTarget` is a class that can have the same API as the DOM `EventTarget`. It
  2309. * adds shorthand functions that wrap around lengthy functions. For example:
  2310. * the `on` function is a wrapper around `addEventListener`.
  2311. *
  2312. * @see [EventTarget Spec]{@link https://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget}
  2313. * @class EventTarget
  2314. */
  2315. class EventTarget {
  2316. /**
  2317. * Adds an `event listener` to an instance of an `EventTarget`. An `event listener` is a
  2318. * function that will get called when an event with a certain name gets triggered.
  2319. *
  2320. * @param {string|string[]} type
  2321. * An event name or an array of event names.
  2322. *
  2323. * @param {Function} fn
  2324. * The function to call with `EventTarget`s
  2325. */
  2326. on(type, fn) {
  2327. // Remove the addEventListener alias before calling Events.on
  2328. // so we don't get into an infinite type loop
  2329. const ael = this.addEventListener;
  2330. this.addEventListener = () => {};
  2331. on(this, type, fn);
  2332. this.addEventListener = ael;
  2333. }
  2334. /**
  2335. * Removes an `event listener` for a specific event from an instance of `EventTarget`.
  2336. * This makes it so that the `event listener` will no longer get called when the
  2337. * named event happens.
  2338. *
  2339. * @param {string|string[]} type
  2340. * An event name or an array of event names.
  2341. *
  2342. * @param {Function} fn
  2343. * The function to remove.
  2344. */
  2345. off(type, fn) {
  2346. off(this, type, fn);
  2347. }
  2348. /**
  2349. * This function will add an `event listener` that gets triggered only once. After the
  2350. * first trigger it will get removed. This is like adding an `event listener`
  2351. * with {@link EventTarget#on} that calls {@link EventTarget#off} on itself.
  2352. *
  2353. * @param {string|string[]} type
  2354. * An event name or an array of event names.
  2355. *
  2356. * @param {Function} fn
  2357. * The function to be called once for each event name.
  2358. */
  2359. one(type, fn) {
  2360. // Remove the addEventListener aliasing Events.on
  2361. // so we don't get into an infinite type loop
  2362. const ael = this.addEventListener;
  2363. this.addEventListener = () => {};
  2364. one(this, type, fn);
  2365. this.addEventListener = ael;
  2366. }
  2367. /**
  2368. * This function will add an `event listener` that gets triggered only once and is
  2369. * removed from all events. This is like adding an array of `event listener`s
  2370. * with {@link EventTarget#on} that calls {@link EventTarget#off} on all events the
  2371. * first time it is triggered.
  2372. *
  2373. * @param {string|string[]} type
  2374. * An event name or an array of event names.
  2375. *
  2376. * @param {Function} fn
  2377. * The function to be called once for each event name.
  2378. */
  2379. any(type, fn) {
  2380. // Remove the addEventListener aliasing Events.on
  2381. // so we don't get into an infinite type loop
  2382. const ael = this.addEventListener;
  2383. this.addEventListener = () => {};
  2384. any(this, type, fn);
  2385. this.addEventListener = ael;
  2386. }
  2387. /**
  2388. * This function causes an event to happen. This will then cause any `event listeners`
  2389. * that are waiting for that event, to get called. If there are no `event listeners`
  2390. * for an event then nothing will happen.
  2391. *
  2392. * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
  2393. * Trigger will also call the `on` + `uppercaseEventName` function.
  2394. *
  2395. * Example:
  2396. * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
  2397. * `onClick` if it exists.
  2398. *
  2399. * @param {string|EventTarget~Event|Object} event
  2400. * The name of the event, an `Event`, or an object with a key of type set to
  2401. * an event name.
  2402. */
  2403. trigger(event) {
  2404. const type = event.type || event;
  2405. // deprecation
  2406. // In a future version we should default target to `this`
  2407. // similar to how we default the target to `elem` in
  2408. // `Events.trigger`. Right now the default `target` will be
  2409. // `document` due to the `Event.fixEvent` call.
  2410. if (typeof event === 'string') {
  2411. event = {
  2412. type
  2413. };
  2414. }
  2415. event = fixEvent(event);
  2416. if (this.allowedEvents_[type] && this['on' + type]) {
  2417. this['on' + type](event);
  2418. }
  2419. trigger(this, event);
  2420. }
  2421. queueTrigger(event) {
  2422. // only set up EVENT_MAP if it'll be used
  2423. if (!EVENT_MAP) {
  2424. EVENT_MAP = new Map();
  2425. }
  2426. const type = event.type || event;
  2427. let map = EVENT_MAP.get(this);
  2428. if (!map) {
  2429. map = new Map();
  2430. EVENT_MAP.set(this, map);
  2431. }
  2432. const oldTimeout = map.get(type);
  2433. map.delete(type);
  2434. window.clearTimeout(oldTimeout);
  2435. const timeout = window.setTimeout(() => {
  2436. map.delete(type);
  2437. // if we cleared out all timeouts for the current target, delete its map
  2438. if (map.size === 0) {
  2439. map = null;
  2440. EVENT_MAP.delete(this);
  2441. }
  2442. this.trigger(event);
  2443. }, 0);
  2444. map.set(type, timeout);
  2445. }
  2446. }
  2447. /**
  2448. * A Custom DOM event.
  2449. *
  2450. * @typedef {CustomEvent} Event
  2451. * @see [Properties]{@link https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent}
  2452. */
  2453. /**
  2454. * All event listeners should follow the following format.
  2455. *
  2456. * @callback EventTarget~EventListener
  2457. * @this {EventTarget}
  2458. *
  2459. * @param {Event} event
  2460. * the event that triggered this function
  2461. *
  2462. * @param {Object} [hash]
  2463. * hash of data sent during the event
  2464. */
  2465. /**
  2466. * An object containing event names as keys and booleans as values.
  2467. *
  2468. * > NOTE: If an event name is set to a true value here {@link EventTarget#trigger}
  2469. * will have extra functionality. See that function for more information.
  2470. *
  2471. * @property EventTarget.prototype.allowedEvents_
  2472. * @private
  2473. */
  2474. EventTarget.prototype.allowedEvents_ = {};
  2475. /**
  2476. * An alias of {@link EventTarget#on}. Allows `EventTarget` to mimic
  2477. * the standard DOM API.
  2478. *
  2479. * @function
  2480. * @see {@link EventTarget#on}
  2481. */
  2482. EventTarget.prototype.addEventListener = EventTarget.prototype.on;
  2483. /**
  2484. * An alias of {@link EventTarget#off}. Allows `EventTarget` to mimic
  2485. * the standard DOM API.
  2486. *
  2487. * @function
  2488. * @see {@link EventTarget#off}
  2489. */
  2490. EventTarget.prototype.removeEventListener = EventTarget.prototype.off;
  2491. /**
  2492. * An alias of {@link EventTarget#trigger}. Allows `EventTarget` to mimic
  2493. * the standard DOM API.
  2494. *
  2495. * @function
  2496. * @see {@link EventTarget#trigger}
  2497. */
  2498. EventTarget.prototype.dispatchEvent = EventTarget.prototype.trigger;
  2499. /**
  2500. * @file mixins/evented.js
  2501. * @module evented
  2502. */
  2503. const objName = obj => {
  2504. if (typeof obj.name === 'function') {
  2505. return obj.name();
  2506. }
  2507. if (typeof obj.name === 'string') {
  2508. return obj.name;
  2509. }
  2510. if (obj.name_) {
  2511. return obj.name_;
  2512. }
  2513. if (obj.constructor && obj.constructor.name) {
  2514. return obj.constructor.name;
  2515. }
  2516. return typeof obj;
  2517. };
  2518. /**
  2519. * Returns whether or not an object has had the evented mixin applied.
  2520. *
  2521. * @param {Object} object
  2522. * An object to test.
  2523. *
  2524. * @return {boolean}
  2525. * Whether or not the object appears to be evented.
  2526. */
  2527. const isEvented = object => object instanceof EventTarget || !!object.eventBusEl_ && ['on', 'one', 'off', 'trigger'].every(k => typeof object[k] === 'function');
  2528. /**
  2529. * Adds a callback to run after the evented mixin applied.
  2530. *
  2531. * @param {Object} object
  2532. * An object to Add
  2533. * @param {Function} callback
  2534. * The callback to run.
  2535. */
  2536. const addEventedCallback = (target, callback) => {
  2537. if (isEvented(target)) {
  2538. callback();
  2539. } else {
  2540. if (!target.eventedCallbacks) {
  2541. target.eventedCallbacks = [];
  2542. }
  2543. target.eventedCallbacks.push(callback);
  2544. }
  2545. };
  2546. /**
  2547. * Whether a value is a valid event type - non-empty string or array.
  2548. *
  2549. * @private
  2550. * @param {string|Array} type
  2551. * The type value to test.
  2552. *
  2553. * @return {boolean}
  2554. * Whether or not the type is a valid event type.
  2555. */
  2556. const isValidEventType = type =>
  2557. // The regex here verifies that the `type` contains at least one non-
  2558. // whitespace character.
  2559. typeof type === 'string' && /\S/.test(type) || Array.isArray(type) && !!type.length;
  2560. /**
  2561. * Validates a value to determine if it is a valid event target. Throws if not.
  2562. *
  2563. * @private
  2564. * @throws {Error}
  2565. * If the target does not appear to be a valid event target.
  2566. *
  2567. * @param {Object} target
  2568. * The object to test.
  2569. *
  2570. * @param {Object} obj
  2571. * The evented object we are validating for
  2572. *
  2573. * @param {string} fnName
  2574. * The name of the evented mixin function that called this.
  2575. */
  2576. const validateTarget = (target, obj, fnName) => {
  2577. if (!target || !target.nodeName && !isEvented(target)) {
  2578. throw new Error(`Invalid target for ${objName(obj)}#${fnName}; must be a DOM node or evented object.`);
  2579. }
  2580. };
  2581. /**
  2582. * Validates a value to determine if it is a valid event target. Throws if not.
  2583. *
  2584. * @private
  2585. * @throws {Error}
  2586. * If the type does not appear to be a valid event type.
  2587. *
  2588. * @param {string|Array} type
  2589. * The type to test.
  2590. *
  2591. * @param {Object} obj
  2592. * The evented object we are validating for
  2593. *
  2594. * @param {string} fnName
  2595. * The name of the evented mixin function that called this.
  2596. */
  2597. const validateEventType = (type, obj, fnName) => {
  2598. if (!isValidEventType(type)) {
  2599. throw new Error(`Invalid event type for ${objName(obj)}#${fnName}; must be a non-empty string or array.`);
  2600. }
  2601. };
  2602. /**
  2603. * Validates a value to determine if it is a valid listener. Throws if not.
  2604. *
  2605. * @private
  2606. * @throws {Error}
  2607. * If the listener is not a function.
  2608. *
  2609. * @param {Function} listener
  2610. * The listener to test.
  2611. *
  2612. * @param {Object} obj
  2613. * The evented object we are validating for
  2614. *
  2615. * @param {string} fnName
  2616. * The name of the evented mixin function that called this.
  2617. */
  2618. const validateListener = (listener, obj, fnName) => {
  2619. if (typeof listener !== 'function') {
  2620. throw new Error(`Invalid listener for ${objName(obj)}#${fnName}; must be a function.`);
  2621. }
  2622. };
  2623. /**
  2624. * Takes an array of arguments given to `on()` or `one()`, validates them, and
  2625. * normalizes them into an object.
  2626. *
  2627. * @private
  2628. * @param {Object} self
  2629. * The evented object on which `on()` or `one()` was called. This
  2630. * object will be bound as the `this` value for the listener.
  2631. *
  2632. * @param {Array} args
  2633. * An array of arguments passed to `on()` or `one()`.
  2634. *
  2635. * @param {string} fnName
  2636. * The name of the evented mixin function that called this.
  2637. *
  2638. * @return {Object}
  2639. * An object containing useful values for `on()` or `one()` calls.
  2640. */
  2641. const normalizeListenArgs = (self, args, fnName) => {
  2642. // If the number of arguments is less than 3, the target is always the
  2643. // evented object itself.
  2644. const isTargetingSelf = args.length < 3 || args[0] === self || args[0] === self.eventBusEl_;
  2645. let target;
  2646. let type;
  2647. let listener;
  2648. if (isTargetingSelf) {
  2649. target = self.eventBusEl_;
  2650. // Deal with cases where we got 3 arguments, but we are still listening to
  2651. // the evented object itself.
  2652. if (args.length >= 3) {
  2653. args.shift();
  2654. }
  2655. [type, listener] = args;
  2656. } else {
  2657. [target, type, listener] = args;
  2658. }
  2659. validateTarget(target, self, fnName);
  2660. validateEventType(type, self, fnName);
  2661. validateListener(listener, self, fnName);
  2662. listener = bind_(self, listener);
  2663. return {
  2664. isTargetingSelf,
  2665. target,
  2666. type,
  2667. listener
  2668. };
  2669. };
  2670. /**
  2671. * Adds the listener to the event type(s) on the target, normalizing for
  2672. * the type of target.
  2673. *
  2674. * @private
  2675. * @param {Element|Object} target
  2676. * A DOM node or evented object.
  2677. *
  2678. * @param {string} method
  2679. * The event binding method to use ("on" or "one").
  2680. *
  2681. * @param {string|Array} type
  2682. * One or more event type(s).
  2683. *
  2684. * @param {Function} listener
  2685. * A listener function.
  2686. */
  2687. const listen = (target, method, type, listener) => {
  2688. validateTarget(target, target, method);
  2689. if (target.nodeName) {
  2690. Events[method](target, type, listener);
  2691. } else {
  2692. target[method](type, listener);
  2693. }
  2694. };
  2695. /**
  2696. * Contains methods that provide event capabilities to an object which is passed
  2697. * to {@link module:evented|evented}.
  2698. *
  2699. * @mixin EventedMixin
  2700. */
  2701. const EventedMixin = {
  2702. /**
  2703. * Add a listener to an event (or events) on this object or another evented
  2704. * object.
  2705. *
  2706. * @param {string|Array|Element|Object} targetOrType
  2707. * If this is a string or array, it represents the event type(s)
  2708. * that will trigger the listener.
  2709. *
  2710. * Another evented object can be passed here instead, which will
  2711. * cause the listener to listen for events on _that_ object.
  2712. *
  2713. * In either case, the listener's `this` value will be bound to
  2714. * this object.
  2715. *
  2716. * @param {string|Array|Function} typeOrListener
  2717. * If the first argument was a string or array, this should be the
  2718. * listener function. Otherwise, this is a string or array of event
  2719. * type(s).
  2720. *
  2721. * @param {Function} [listener]
  2722. * If the first argument was another evented object, this will be
  2723. * the listener function.
  2724. */
  2725. on(...args) {
  2726. const {
  2727. isTargetingSelf,
  2728. target,
  2729. type,
  2730. listener
  2731. } = normalizeListenArgs(this, args, 'on');
  2732. listen(target, 'on', type, listener);
  2733. // If this object is listening to another evented object.
  2734. if (!isTargetingSelf) {
  2735. // If this object is disposed, remove the listener.
  2736. const removeListenerOnDispose = () => this.off(target, type, listener);
  2737. // Use the same function ID as the listener so we can remove it later it
  2738. // using the ID of the original listener.
  2739. removeListenerOnDispose.guid = listener.guid;
  2740. // Add a listener to the target's dispose event as well. This ensures
  2741. // that if the target is disposed BEFORE this object, we remove the
  2742. // removal listener that was just added. Otherwise, we create a memory leak.
  2743. const removeRemoverOnTargetDispose = () => this.off('dispose', removeListenerOnDispose);
  2744. // Use the same function ID as the listener so we can remove it later
  2745. // it using the ID of the original listener.
  2746. removeRemoverOnTargetDispose.guid = listener.guid;
  2747. listen(this, 'on', 'dispose', removeListenerOnDispose);
  2748. listen(target, 'on', 'dispose', removeRemoverOnTargetDispose);
  2749. }
  2750. },
  2751. /**
  2752. * Add a listener to an event (or events) on this object or another evented
  2753. * object. The listener will be called once per event and then removed.
  2754. *
  2755. * @param {string|Array|Element|Object} targetOrType
  2756. * If this is a string or array, it represents the event type(s)
  2757. * that will trigger the listener.
  2758. *
  2759. * Another evented object can be passed here instead, which will
  2760. * cause the listener to listen for events on _that_ object.
  2761. *
  2762. * In either case, the listener's `this` value will be bound to
  2763. * this object.
  2764. *
  2765. * @param {string|Array|Function} typeOrListener
  2766. * If the first argument was a string or array, this should be the
  2767. * listener function. Otherwise, this is a string or array of event
  2768. * type(s).
  2769. *
  2770. * @param {Function} [listener]
  2771. * If the first argument was another evented object, this will be
  2772. * the listener function.
  2773. */
  2774. one(...args) {
  2775. const {
  2776. isTargetingSelf,
  2777. target,
  2778. type,
  2779. listener
  2780. } = normalizeListenArgs(this, args, 'one');
  2781. // Targeting this evented object.
  2782. if (isTargetingSelf) {
  2783. listen(target, 'one', type, listener);
  2784. // Targeting another evented object.
  2785. } else {
  2786. // TODO: This wrapper is incorrect! It should only
  2787. // remove the wrapper for the event type that called it.
  2788. // Instead all listeners are removed on the first trigger!
  2789. // see https://github.com/videojs/video.js/issues/5962
  2790. const wrapper = (...largs) => {
  2791. this.off(target, type, wrapper);
  2792. listener.apply(null, largs);
  2793. };
  2794. // Use the same function ID as the listener so we can remove it later
  2795. // it using the ID of the original listener.
  2796. wrapper.guid = listener.guid;
  2797. listen(target, 'one', type, wrapper);
  2798. }
  2799. },
  2800. /**
  2801. * Add a listener to an event (or events) on this object or another evented
  2802. * object. The listener will only be called once for the first event that is triggered
  2803. * then removed.
  2804. *
  2805. * @param {string|Array|Element|Object} targetOrType
  2806. * If this is a string or array, it represents the event type(s)
  2807. * that will trigger the listener.
  2808. *
  2809. * Another evented object can be passed here instead, which will
  2810. * cause the listener to listen for events on _that_ object.
  2811. *
  2812. * In either case, the listener's `this` value will be bound to
  2813. * this object.
  2814. *
  2815. * @param {string|Array|Function} typeOrListener
  2816. * If the first argument was a string or array, this should be the
  2817. * listener function. Otherwise, this is a string or array of event
  2818. * type(s).
  2819. *
  2820. * @param {Function} [listener]
  2821. * If the first argument was another evented object, this will be
  2822. * the listener function.
  2823. */
  2824. any(...args) {
  2825. const {
  2826. isTargetingSelf,
  2827. target,
  2828. type,
  2829. listener
  2830. } = normalizeListenArgs(this, args, 'any');
  2831. // Targeting this evented object.
  2832. if (isTargetingSelf) {
  2833. listen(target, 'any', type, listener);
  2834. // Targeting another evented object.
  2835. } else {
  2836. const wrapper = (...largs) => {
  2837. this.off(target, type, wrapper);
  2838. listener.apply(null, largs);
  2839. };
  2840. // Use the same function ID as the listener so we can remove it later
  2841. // it using the ID of the original listener.
  2842. wrapper.guid = listener.guid;
  2843. listen(target, 'any', type, wrapper);
  2844. }
  2845. },
  2846. /**
  2847. * Removes listener(s) from event(s) on an evented object.
  2848. *
  2849. * @param {string|Array|Element|Object} [targetOrType]
  2850. * If this is a string or array, it represents the event type(s).
  2851. *
  2852. * Another evented object can be passed here instead, in which case
  2853. * ALL 3 arguments are _required_.
  2854. *
  2855. * @param {string|Array|Function} [typeOrListener]
  2856. * If the first argument was a string or array, this may be the
  2857. * listener function. Otherwise, this is a string or array of event
  2858. * type(s).
  2859. *
  2860. * @param {Function} [listener]
  2861. * If the first argument was another evented object, this will be
  2862. * the listener function; otherwise, _all_ listeners bound to the
  2863. * event type(s) will be removed.
  2864. */
  2865. off(targetOrType, typeOrListener, listener) {
  2866. // Targeting this evented object.
  2867. if (!targetOrType || isValidEventType(targetOrType)) {
  2868. off(this.eventBusEl_, targetOrType, typeOrListener);
  2869. // Targeting another evented object.
  2870. } else {
  2871. const target = targetOrType;
  2872. const type = typeOrListener;
  2873. // Fail fast and in a meaningful way!
  2874. validateTarget(target, this, 'off');
  2875. validateEventType(type, this, 'off');
  2876. validateListener(listener, this, 'off');
  2877. // Ensure there's at least a guid, even if the function hasn't been used
  2878. listener = bind_(this, listener);
  2879. // Remove the dispose listener on this evented object, which was given
  2880. // the same guid as the event listener in on().
  2881. this.off('dispose', listener);
  2882. if (target.nodeName) {
  2883. off(target, type, listener);
  2884. off(target, 'dispose', listener);
  2885. } else if (isEvented(target)) {
  2886. target.off(type, listener);
  2887. target.off('dispose', listener);
  2888. }
  2889. }
  2890. },
  2891. /**
  2892. * Fire an event on this evented object, causing its listeners to be called.
  2893. *
  2894. * @param {string|Object} event
  2895. * An event type or an object with a type property.
  2896. *
  2897. * @param {Object} [hash]
  2898. * An additional object to pass along to listeners.
  2899. *
  2900. * @return {boolean}
  2901. * Whether or not the default behavior was prevented.
  2902. */
  2903. trigger(event, hash) {
  2904. validateTarget(this.eventBusEl_, this, 'trigger');
  2905. const type = event && typeof event !== 'string' ? event.type : event;
  2906. if (!isValidEventType(type)) {
  2907. 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.');
  2908. }
  2909. return trigger(this.eventBusEl_, event, hash);
  2910. }
  2911. };
  2912. /**
  2913. * Applies {@link module:evented~EventedMixin|EventedMixin} to a target object.
  2914. *
  2915. * @param {Object} target
  2916. * The object to which to add event methods.
  2917. *
  2918. * @param {Object} [options={}]
  2919. * Options for customizing the mixin behavior.
  2920. *
  2921. * @param {string} [options.eventBusKey]
  2922. * By default, adds a `eventBusEl_` DOM element to the target object,
  2923. * which is used as an event bus. If the target object already has a
  2924. * DOM element that should be used, pass its key here.
  2925. *
  2926. * @return {Object}
  2927. * The target object.
  2928. */
  2929. function evented(target, options = {}) {
  2930. const {
  2931. eventBusKey
  2932. } = options;
  2933. // Set or create the eventBusEl_.
  2934. if (eventBusKey) {
  2935. if (!target[eventBusKey].nodeName) {
  2936. throw new Error(`The eventBusKey "${eventBusKey}" does not refer to an element.`);
  2937. }
  2938. target.eventBusEl_ = target[eventBusKey];
  2939. } else {
  2940. target.eventBusEl_ = createEl('span', {
  2941. className: 'vjs-event-bus'
  2942. });
  2943. }
  2944. Object.assign(target, EventedMixin);
  2945. if (target.eventedCallbacks) {
  2946. target.eventedCallbacks.forEach(callback => {
  2947. callback();
  2948. });
  2949. }
  2950. // When any evented object is disposed, it removes all its listeners.
  2951. target.on('dispose', () => {
  2952. target.off();
  2953. [target, target.el_, target.eventBusEl_].forEach(function (val) {
  2954. if (val && DomData.has(val)) {
  2955. DomData.delete(val);
  2956. }
  2957. });
  2958. window.setTimeout(() => {
  2959. target.eventBusEl_ = null;
  2960. }, 0);
  2961. });
  2962. return target;
  2963. }
  2964. /**
  2965. * @file mixins/stateful.js
  2966. * @module stateful
  2967. */
  2968. /**
  2969. * Contains methods that provide statefulness to an object which is passed
  2970. * to {@link module:stateful}.
  2971. *
  2972. * @mixin StatefulMixin
  2973. */
  2974. const StatefulMixin = {
  2975. /**
  2976. * A hash containing arbitrary keys and values representing the state of
  2977. * the object.
  2978. *
  2979. * @type {Object}
  2980. */
  2981. state: {},
  2982. /**
  2983. * Set the state of an object by mutating its
  2984. * {@link module:stateful~StatefulMixin.state|state} object in place.
  2985. *
  2986. * @fires module:stateful~StatefulMixin#statechanged
  2987. * @param {Object|Function} stateUpdates
  2988. * A new set of properties to shallow-merge into the plugin state.
  2989. * Can be a plain object or a function returning a plain object.
  2990. *
  2991. * @return {Object|undefined}
  2992. * An object containing changes that occurred. If no changes
  2993. * occurred, returns `undefined`.
  2994. */
  2995. setState(stateUpdates) {
  2996. // Support providing the `stateUpdates` state as a function.
  2997. if (typeof stateUpdates === 'function') {
  2998. stateUpdates = stateUpdates();
  2999. }
  3000. let changes;
  3001. each(stateUpdates, (value, key) => {
  3002. // Record the change if the value is different from what's in the
  3003. // current state.
  3004. if (this.state[key] !== value) {
  3005. changes = changes || {};
  3006. changes[key] = {
  3007. from: this.state[key],
  3008. to: value
  3009. };
  3010. }
  3011. this.state[key] = value;
  3012. });
  3013. // Only trigger "statechange" if there were changes AND we have a trigger
  3014. // function. This allows us to not require that the target object be an
  3015. // evented object.
  3016. if (changes && isEvented(this)) {
  3017. /**
  3018. * An event triggered on an object that is both
  3019. * {@link module:stateful|stateful} and {@link module:evented|evented}
  3020. * indicating that its state has changed.
  3021. *
  3022. * @event module:stateful~StatefulMixin#statechanged
  3023. * @type {Object}
  3024. * @property {Object} changes
  3025. * A hash containing the properties that were changed and
  3026. * the values they were changed `from` and `to`.
  3027. */
  3028. this.trigger({
  3029. changes,
  3030. type: 'statechanged'
  3031. });
  3032. }
  3033. return changes;
  3034. }
  3035. };
  3036. /**
  3037. * Applies {@link module:stateful~StatefulMixin|StatefulMixin} to a target
  3038. * object.
  3039. *
  3040. * If the target object is {@link module:evented|evented} and has a
  3041. * `handleStateChanged` method, that method will be automatically bound to the
  3042. * `statechanged` event on itself.
  3043. *
  3044. * @param {Object} target
  3045. * The object to be made stateful.
  3046. *
  3047. * @param {Object} [defaultState]
  3048. * A default set of properties to populate the newly-stateful object's
  3049. * `state` property.
  3050. *
  3051. * @return {Object}
  3052. * Returns the `target`.
  3053. */
  3054. function stateful(target, defaultState) {
  3055. Object.assign(target, StatefulMixin);
  3056. // This happens after the mixing-in because we need to replace the `state`
  3057. // added in that step.
  3058. target.state = Object.assign({}, target.state, defaultState);
  3059. // Auto-bind the `handleStateChanged` method of the target object if it exists.
  3060. if (typeof target.handleStateChanged === 'function' && isEvented(target)) {
  3061. target.on('statechanged', target.handleStateChanged);
  3062. }
  3063. return target;
  3064. }
  3065. /**
  3066. * @file str.js
  3067. * @module to-lower-case
  3068. */
  3069. /**
  3070. * Lowercase the first letter of a string.
  3071. *
  3072. * @param {string} string
  3073. * String to be lowercased
  3074. *
  3075. * @return {string}
  3076. * The string with a lowercased first letter
  3077. */
  3078. const toLowerCase = function (string) {
  3079. if (typeof string !== 'string') {
  3080. return string;
  3081. }
  3082. return string.replace(/./, w => w.toLowerCase());
  3083. };
  3084. /**
  3085. * Uppercase the first letter of a string.
  3086. *
  3087. * @param {string} string
  3088. * String to be uppercased
  3089. *
  3090. * @return {string}
  3091. * The string with an uppercased first letter
  3092. */
  3093. const toTitleCase = function (string) {
  3094. if (typeof string !== 'string') {
  3095. return string;
  3096. }
  3097. return string.replace(/./, w => w.toUpperCase());
  3098. };
  3099. /**
  3100. * Compares the TitleCase versions of the two strings for equality.
  3101. *
  3102. * @param {string} str1
  3103. * The first string to compare
  3104. *
  3105. * @param {string} str2
  3106. * The second string to compare
  3107. *
  3108. * @return {boolean}
  3109. * Whether the TitleCase versions of the strings are equal
  3110. */
  3111. const titleCaseEquals = function (str1, str2) {
  3112. return toTitleCase(str1) === toTitleCase(str2);
  3113. };
  3114. var Str = /*#__PURE__*/Object.freeze({
  3115. __proto__: null,
  3116. toLowerCase: toLowerCase,
  3117. toTitleCase: toTitleCase,
  3118. titleCaseEquals: titleCaseEquals
  3119. });
  3120. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  3121. function unwrapExports (x) {
  3122. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  3123. }
  3124. function createCommonjsModule(fn, module) {
  3125. return module = { exports: {} }, fn(module, module.exports), module.exports;
  3126. }
  3127. var keycode = createCommonjsModule(function (module, exports) {
  3128. // Source: http://jsfiddle.net/vWx8V/
  3129. // http://stackoverflow.com/questions/5603195/full-list-of-javascript-keycodes
  3130. /**
  3131. * Conenience method returns corresponding value for given keyName or keyCode.
  3132. *
  3133. * @param {Mixed} keyCode {Number} or keyName {String}
  3134. * @return {Mixed}
  3135. * @api public
  3136. */
  3137. function keyCode(searchInput) {
  3138. // Keyboard Events
  3139. if (searchInput && 'object' === typeof searchInput) {
  3140. var hasKeyCode = searchInput.which || searchInput.keyCode || searchInput.charCode;
  3141. if (hasKeyCode) searchInput = hasKeyCode;
  3142. }
  3143. // Numbers
  3144. if ('number' === typeof searchInput) return names[searchInput];
  3145. // Everything else (cast to string)
  3146. var search = String(searchInput);
  3147. // check codes
  3148. var foundNamedKey = codes[search.toLowerCase()];
  3149. if (foundNamedKey) return foundNamedKey;
  3150. // check aliases
  3151. var foundNamedKey = aliases[search.toLowerCase()];
  3152. if (foundNamedKey) return foundNamedKey;
  3153. // weird character?
  3154. if (search.length === 1) return search.charCodeAt(0);
  3155. return undefined;
  3156. }
  3157. /**
  3158. * Compares a keyboard event with a given keyCode or keyName.
  3159. *
  3160. * @param {Event} event Keyboard event that should be tested
  3161. * @param {Mixed} keyCode {Number} or keyName {String}
  3162. * @return {Boolean}
  3163. * @api public
  3164. */
  3165. keyCode.isEventKey = function isEventKey(event, nameOrCode) {
  3166. if (event && 'object' === typeof event) {
  3167. var keyCode = event.which || event.keyCode || event.charCode;
  3168. if (keyCode === null || keyCode === undefined) {
  3169. return false;
  3170. }
  3171. if (typeof nameOrCode === 'string') {
  3172. // check codes
  3173. var foundNamedKey = codes[nameOrCode.toLowerCase()];
  3174. if (foundNamedKey) {
  3175. return foundNamedKey === keyCode;
  3176. }
  3177. // check aliases
  3178. var foundNamedKey = aliases[nameOrCode.toLowerCase()];
  3179. if (foundNamedKey) {
  3180. return foundNamedKey === keyCode;
  3181. }
  3182. } else if (typeof nameOrCode === 'number') {
  3183. return nameOrCode === keyCode;
  3184. }
  3185. return false;
  3186. }
  3187. };
  3188. exports = module.exports = keyCode;
  3189. /**
  3190. * Get by name
  3191. *
  3192. * exports.code['enter'] // => 13
  3193. */
  3194. var codes = exports.code = exports.codes = {
  3195. 'backspace': 8,
  3196. 'tab': 9,
  3197. 'enter': 13,
  3198. 'shift': 16,
  3199. 'ctrl': 17,
  3200. 'alt': 18,
  3201. 'pause/break': 19,
  3202. 'caps lock': 20,
  3203. 'esc': 27,
  3204. 'space': 32,
  3205. 'page up': 33,
  3206. 'page down': 34,
  3207. 'end': 35,
  3208. 'home': 36,
  3209. 'left': 37,
  3210. 'up': 38,
  3211. 'right': 39,
  3212. 'down': 40,
  3213. 'insert': 45,
  3214. 'delete': 46,
  3215. 'command': 91,
  3216. 'left command': 91,
  3217. 'right command': 93,
  3218. 'numpad *': 106,
  3219. 'numpad +': 107,
  3220. 'numpad -': 109,
  3221. 'numpad .': 110,
  3222. 'numpad /': 111,
  3223. 'num lock': 144,
  3224. 'scroll lock': 145,
  3225. 'my computer': 182,
  3226. 'my calculator': 183,
  3227. ';': 186,
  3228. '=': 187,
  3229. ',': 188,
  3230. '-': 189,
  3231. '.': 190,
  3232. '/': 191,
  3233. '`': 192,
  3234. '[': 219,
  3235. '\\': 220,
  3236. ']': 221,
  3237. "'": 222
  3238. };
  3239. // Helper aliases
  3240. var aliases = exports.aliases = {
  3241. 'windows': 91,
  3242. '⇧': 16,
  3243. '⌥': 18,
  3244. '⌃': 17,
  3245. '⌘': 91,
  3246. 'ctl': 17,
  3247. 'control': 17,
  3248. 'option': 18,
  3249. 'pause': 19,
  3250. 'break': 19,
  3251. 'caps': 20,
  3252. 'return': 13,
  3253. 'escape': 27,
  3254. 'spc': 32,
  3255. 'spacebar': 32,
  3256. 'pgup': 33,
  3257. 'pgdn': 34,
  3258. 'ins': 45,
  3259. 'del': 46,
  3260. 'cmd': 91
  3261. };
  3262. /*!
  3263. * Programatically add the following
  3264. */
  3265. // lower case chars
  3266. for (i = 97; i < 123; i++) codes[String.fromCharCode(i)] = i - 32;
  3267. // numbers
  3268. for (var i = 48; i < 58; i++) codes[i - 48] = i;
  3269. // function keys
  3270. for (i = 1; i < 13; i++) codes['f' + i] = i + 111;
  3271. // numpad keys
  3272. for (i = 0; i < 10; i++) codes['numpad ' + i] = i + 96;
  3273. /**
  3274. * Get by code
  3275. *
  3276. * exports.name[13] // => 'Enter'
  3277. */
  3278. var names = exports.names = exports.title = {}; // title for backward compat
  3279. // Create reverse mapping
  3280. for (i in codes) names[codes[i]] = i;
  3281. // Add aliases
  3282. for (var alias in aliases) {
  3283. codes[alias] = aliases[alias];
  3284. }
  3285. });
  3286. keycode.code;
  3287. keycode.codes;
  3288. keycode.aliases;
  3289. keycode.names;
  3290. keycode.title;
  3291. /**
  3292. * Player Component - Base class for all UI objects
  3293. *
  3294. * @file component.js
  3295. */
  3296. /**
  3297. * Base class for all UI Components.
  3298. * Components are UI objects which represent both a javascript object and an element
  3299. * in the DOM. They can be children of other components, and can have
  3300. * children themselves.
  3301. *
  3302. * Components can also use methods from {@link EventTarget}
  3303. */
  3304. class Component {
  3305. /**
  3306. * A callback that is called when a component is ready. Does not have any
  3307. * parameters and any callback value will be ignored.
  3308. *
  3309. * @callback ReadyCallback
  3310. * @this Component
  3311. */
  3312. /**
  3313. * Creates an instance of this class.
  3314. *
  3315. * @param { import('./player').default } player
  3316. * The `Player` that this class should be attached to.
  3317. *
  3318. * @param {Object} [options]
  3319. * The key/value store of component options.
  3320. *
  3321. * @param {Object[]} [options.children]
  3322. * An array of children objects to initialize this component with. Children objects have
  3323. * a name property that will be used if more than one component of the same type needs to be
  3324. * added.
  3325. *
  3326. * @param {string} [options.className]
  3327. * A class or space separated list of classes to add the component
  3328. *
  3329. * @param {ReadyCallback} [ready]
  3330. * Function that gets called when the `Component` is ready.
  3331. */
  3332. constructor(player, options, ready) {
  3333. // The component might be the player itself and we can't pass `this` to super
  3334. if (!player && this.play) {
  3335. this.player_ = player = this; // eslint-disable-line
  3336. } else {
  3337. this.player_ = player;
  3338. }
  3339. this.isDisposed_ = false;
  3340. // Hold the reference to the parent component via `addChild` method
  3341. this.parentComponent_ = null;
  3342. // Make a copy of prototype.options_ to protect against overriding defaults
  3343. this.options_ = merge({}, this.options_);
  3344. // Updated options with supplied options
  3345. options = this.options_ = merge(this.options_, options);
  3346. // Get ID from options or options element if one is supplied
  3347. this.id_ = options.id || options.el && options.el.id;
  3348. // If there was no ID from the options, generate one
  3349. if (!this.id_) {
  3350. // Don't require the player ID function in the case of mock players
  3351. const id = player && player.id && player.id() || 'no_player';
  3352. this.id_ = `${id}_component_${newGUID()}`;
  3353. }
  3354. this.name_ = options.name || null;
  3355. // Create element if one wasn't provided in options
  3356. if (options.el) {
  3357. this.el_ = options.el;
  3358. } else if (options.createEl !== false) {
  3359. this.el_ = this.createEl();
  3360. }
  3361. if (options.className && this.el_) {
  3362. options.className.split(' ').forEach(c => this.addClass(c));
  3363. }
  3364. // Remove the placeholder event methods. If the component is evented, the
  3365. // real methods are added next
  3366. ['on', 'off', 'one', 'any', 'trigger'].forEach(fn => {
  3367. this[fn] = undefined;
  3368. });
  3369. // if evented is anything except false, we want to mixin in evented
  3370. if (options.evented !== false) {
  3371. // Make this an evented object and use `el_`, if available, as its event bus
  3372. evented(this, {
  3373. eventBusKey: this.el_ ? 'el_' : null
  3374. });
  3375. this.handleLanguagechange = this.handleLanguagechange.bind(this);
  3376. this.on(this.player_, 'languagechange', this.handleLanguagechange);
  3377. }
  3378. stateful(this, this.constructor.defaultState);
  3379. this.children_ = [];
  3380. this.childIndex_ = {};
  3381. this.childNameIndex_ = {};
  3382. this.setTimeoutIds_ = new Set();
  3383. this.setIntervalIds_ = new Set();
  3384. this.rafIds_ = new Set();
  3385. this.namedRafs_ = new Map();
  3386. this.clearingTimersOnDispose_ = false;
  3387. // Add any child components in options
  3388. if (options.initChildren !== false) {
  3389. this.initChildren();
  3390. }
  3391. // Don't want to trigger ready here or it will go before init is actually
  3392. // finished for all children that run this constructor
  3393. this.ready(ready);
  3394. if (options.reportTouchActivity !== false) {
  3395. this.enableTouchActivity();
  3396. }
  3397. }
  3398. // `on`, `off`, `one`, `any` and `trigger` are here so tsc includes them in definitions.
  3399. // They are replaced or removed in the constructor
  3400. /**
  3401. * Adds an `event listener` to an instance of an `EventTarget`. An `event listener` is a
  3402. * function that will get called when an event with a certain name gets triggered.
  3403. *
  3404. * @param {string|string[]} type
  3405. * An event name or an array of event names.
  3406. *
  3407. * @param {Function} fn
  3408. * The function to call with `EventTarget`s
  3409. */
  3410. on(type, fn) {}
  3411. /**
  3412. * Removes an `event listener` for a specific event from an instance of `EventTarget`.
  3413. * This makes it so that the `event listener` will no longer get called when the
  3414. * named event happens.
  3415. *
  3416. * @param {string|string[]} type
  3417. * An event name or an array of event names.
  3418. *
  3419. * @param {Function} fn
  3420. * The function to remove.
  3421. */
  3422. off(type, fn) {}
  3423. /**
  3424. * This function will add an `event listener` that gets triggered only once. After the
  3425. * first trigger it will get removed. This is like adding an `event listener`
  3426. * with {@link EventTarget#on} that calls {@link EventTarget#off} on itself.
  3427. *
  3428. * @param {string|string[]} type
  3429. * An event name or an array of event names.
  3430. *
  3431. * @param {Function} fn
  3432. * The function to be called once for each event name.
  3433. */
  3434. one(type, fn) {}
  3435. /**
  3436. * This function will add an `event listener` that gets triggered only once and is
  3437. * removed from all events. This is like adding an array of `event listener`s
  3438. * with {@link EventTarget#on} that calls {@link EventTarget#off} on all events the
  3439. * first time it is triggered.
  3440. *
  3441. * @param {string|string[]} type
  3442. * An event name or an array of event names.
  3443. *
  3444. * @param {Function} fn
  3445. * The function to be called once for each event name.
  3446. */
  3447. any(type, fn) {}
  3448. /**
  3449. * This function causes an event to happen. This will then cause any `event listeners`
  3450. * that are waiting for that event, to get called. If there are no `event listeners`
  3451. * for an event then nothing will happen.
  3452. *
  3453. * If the name of the `Event` that is being triggered is in `EventTarget.allowedEvents_`.
  3454. * Trigger will also call the `on` + `uppercaseEventName` function.
  3455. *
  3456. * Example:
  3457. * 'click' is in `EventTarget.allowedEvents_`, so, trigger will attempt to call
  3458. * `onClick` if it exists.
  3459. *
  3460. * @param {string|Event|Object} event
  3461. * The name of the event, an `Event`, or an object with a key of type set to
  3462. * an event name.
  3463. */
  3464. trigger(event) {}
  3465. /**
  3466. * Dispose of the `Component` and all child components.
  3467. *
  3468. * @fires Component#dispose
  3469. *
  3470. * @param {Object} options
  3471. * @param {Element} options.originalEl element with which to replace player element
  3472. */
  3473. dispose(options = {}) {
  3474. // Bail out if the component has already been disposed.
  3475. if (this.isDisposed_) {
  3476. return;
  3477. }
  3478. if (this.readyQueue_) {
  3479. this.readyQueue_.length = 0;
  3480. }
  3481. /**
  3482. * Triggered when a `Component` is disposed.
  3483. *
  3484. * @event Component#dispose
  3485. * @type {Event}
  3486. *
  3487. * @property {boolean} [bubbles=false]
  3488. * set to false so that the dispose event does not
  3489. * bubble up
  3490. */
  3491. this.trigger({
  3492. type: 'dispose',
  3493. bubbles: false
  3494. });
  3495. this.isDisposed_ = true;
  3496. // Dispose all children.
  3497. if (this.children_) {
  3498. for (let i = this.children_.length - 1; i >= 0; i--) {
  3499. if (this.children_[i].dispose) {
  3500. this.children_[i].dispose();
  3501. }
  3502. }
  3503. }
  3504. // Delete child references
  3505. this.children_ = null;
  3506. this.childIndex_ = null;
  3507. this.childNameIndex_ = null;
  3508. this.parentComponent_ = null;
  3509. if (this.el_) {
  3510. // Remove element from DOM
  3511. if (this.el_.parentNode) {
  3512. if (options.restoreEl) {
  3513. this.el_.parentNode.replaceChild(options.restoreEl, this.el_);
  3514. } else {
  3515. this.el_.parentNode.removeChild(this.el_);
  3516. }
  3517. }
  3518. this.el_ = null;
  3519. }
  3520. // remove reference to the player after disposing of the element
  3521. this.player_ = null;
  3522. }
  3523. /**
  3524. * Determine whether or not this component has been disposed.
  3525. *
  3526. * @return {boolean}
  3527. * If the component has been disposed, will be `true`. Otherwise, `false`.
  3528. */
  3529. isDisposed() {
  3530. return Boolean(this.isDisposed_);
  3531. }
  3532. /**
  3533. * Return the {@link Player} that the `Component` has attached to.
  3534. *
  3535. * @return { import('./player').default }
  3536. * The player that this `Component` has attached to.
  3537. */
  3538. player() {
  3539. return this.player_;
  3540. }
  3541. /**
  3542. * Deep merge of options objects with new options.
  3543. * > Note: When both `obj` and `options` contain properties whose values are objects.
  3544. * The two properties get merged using {@link module:obj.merge}
  3545. *
  3546. * @param {Object} obj
  3547. * The object that contains new options.
  3548. *
  3549. * @return {Object}
  3550. * A new object of `this.options_` and `obj` merged together.
  3551. */
  3552. options(obj) {
  3553. if (!obj) {
  3554. return this.options_;
  3555. }
  3556. this.options_ = merge(this.options_, obj);
  3557. return this.options_;
  3558. }
  3559. /**
  3560. * Get the `Component`s DOM element
  3561. *
  3562. * @return {Element}
  3563. * The DOM element for this `Component`.
  3564. */
  3565. el() {
  3566. return this.el_;
  3567. }
  3568. /**
  3569. * Create the `Component`s DOM element.
  3570. *
  3571. * @param {string} [tagName]
  3572. * Element's DOM node type. e.g. 'div'
  3573. *
  3574. * @param {Object} [properties]
  3575. * An object of properties that should be set.
  3576. *
  3577. * @param {Object} [attributes]
  3578. * An object of attributes that should be set.
  3579. *
  3580. * @return {Element}
  3581. * The element that gets created.
  3582. */
  3583. createEl(tagName, properties, attributes) {
  3584. return createEl(tagName, properties, attributes);
  3585. }
  3586. /**
  3587. * Localize a string given the string in english.
  3588. *
  3589. * If tokens are provided, it'll try and run a simple token replacement on the provided string.
  3590. * The tokens it looks for look like `{1}` with the index being 1-indexed into the tokens array.
  3591. *
  3592. * If a `defaultValue` is provided, it'll use that over `string`,
  3593. * if a value isn't found in provided language files.
  3594. * This is useful if you want to have a descriptive key for token replacement
  3595. * but have a succinct localized string and not require `en.json` to be included.
  3596. *
  3597. * Currently, it is used for the progress bar timing.
  3598. * ```js
  3599. * {
  3600. * "progress bar timing: currentTime={1} duration={2}": "{1} of {2}"
  3601. * }
  3602. * ```
  3603. * It is then used like so:
  3604. * ```js
  3605. * this.localize('progress bar timing: currentTime={1} duration{2}',
  3606. * [this.player_.currentTime(), this.player_.duration()],
  3607. * '{1} of {2}');
  3608. * ```
  3609. *
  3610. * Which outputs something like: `01:23 of 24:56`.
  3611. *
  3612. *
  3613. * @param {string} string
  3614. * The string to localize and the key to lookup in the language files.
  3615. * @param {string[]} [tokens]
  3616. * If the current item has token replacements, provide the tokens here.
  3617. * @param {string} [defaultValue]
  3618. * Defaults to `string`. Can be a default value to use for token replacement
  3619. * if the lookup key is needed to be separate.
  3620. *
  3621. * @return {string}
  3622. * The localized string or if no localization exists the english string.
  3623. */
  3624. localize(string, tokens, defaultValue = string) {
  3625. const code = this.player_.language && this.player_.language();
  3626. const languages = this.player_.languages && this.player_.languages();
  3627. const language = languages && languages[code];
  3628. const primaryCode = code && code.split('-')[0];
  3629. const primaryLang = languages && languages[primaryCode];
  3630. let localizedString = defaultValue;
  3631. if (language && language[string]) {
  3632. localizedString = language[string];
  3633. } else if (primaryLang && primaryLang[string]) {
  3634. localizedString = primaryLang[string];
  3635. }
  3636. if (tokens) {
  3637. localizedString = localizedString.replace(/\{(\d+)\}/g, function (match, index) {
  3638. const value = tokens[index - 1];
  3639. let ret = value;
  3640. if (typeof value === 'undefined') {
  3641. ret = match;
  3642. }
  3643. return ret;
  3644. });
  3645. }
  3646. return localizedString;
  3647. }
  3648. /**
  3649. * Handles language change for the player in components. Should be overridden by sub-components.
  3650. *
  3651. * @abstract
  3652. */
  3653. handleLanguagechange() {}
  3654. /**
  3655. * Return the `Component`s DOM element. This is where children get inserted.
  3656. * This will usually be the the same as the element returned in {@link Component#el}.
  3657. *
  3658. * @return {Element}
  3659. * The content element for this `Component`.
  3660. */
  3661. contentEl() {
  3662. return this.contentEl_ || this.el_;
  3663. }
  3664. /**
  3665. * Get this `Component`s ID
  3666. *
  3667. * @return {string}
  3668. * The id of this `Component`
  3669. */
  3670. id() {
  3671. return this.id_;
  3672. }
  3673. /**
  3674. * Get the `Component`s name. The name gets used to reference the `Component`
  3675. * and is set during registration.
  3676. *
  3677. * @return {string}
  3678. * The name of this `Component`.
  3679. */
  3680. name() {
  3681. return this.name_;
  3682. }
  3683. /**
  3684. * Get an array of all child components
  3685. *
  3686. * @return {Array}
  3687. * The children
  3688. */
  3689. children() {
  3690. return this.children_;
  3691. }
  3692. /**
  3693. * Returns the child `Component` with the given `id`.
  3694. *
  3695. * @param {string} id
  3696. * The id of the child `Component` to get.
  3697. *
  3698. * @return {Component|undefined}
  3699. * The child `Component` with the given `id` or undefined.
  3700. */
  3701. getChildById(id) {
  3702. return this.childIndex_[id];
  3703. }
  3704. /**
  3705. * Returns the child `Component` with the given `name`.
  3706. *
  3707. * @param {string} name
  3708. * The name of the child `Component` to get.
  3709. *
  3710. * @return {Component|undefined}
  3711. * The child `Component` with the given `name` or undefined.
  3712. */
  3713. getChild(name) {
  3714. if (!name) {
  3715. return;
  3716. }
  3717. return this.childNameIndex_[name];
  3718. }
  3719. /**
  3720. * Returns the descendant `Component` following the givent
  3721. * descendant `names`. For instance ['foo', 'bar', 'baz'] would
  3722. * try to get 'foo' on the current component, 'bar' on the 'foo'
  3723. * component and 'baz' on the 'bar' component and return undefined
  3724. * if any of those don't exist.
  3725. *
  3726. * @param {...string[]|...string} names
  3727. * The name of the child `Component` to get.
  3728. *
  3729. * @return {Component|undefined}
  3730. * The descendant `Component` following the given descendant
  3731. * `names` or undefined.
  3732. */
  3733. getDescendant(...names) {
  3734. // flatten array argument into the main array
  3735. names = names.reduce((acc, n) => acc.concat(n), []);
  3736. let currentChild = this;
  3737. for (let i = 0; i < names.length; i++) {
  3738. currentChild = currentChild.getChild(names[i]);
  3739. if (!currentChild || !currentChild.getChild) {
  3740. return;
  3741. }
  3742. }
  3743. return currentChild;
  3744. }
  3745. /**
  3746. * Add a child `Component` inside the current `Component`.
  3747. *
  3748. *
  3749. * @param {string|Component} child
  3750. * The name or instance of a child to add.
  3751. *
  3752. * @param {Object} [options={}]
  3753. * The key/value store of options that will get passed to children of
  3754. * the child.
  3755. *
  3756. * @param {number} [index=this.children_.length]
  3757. * The index to attempt to add a child into.
  3758. *
  3759. * @return {Component}
  3760. * The `Component` that gets added as a child. When using a string the
  3761. * `Component` will get created by this process.
  3762. */
  3763. addChild(child, options = {}, index = this.children_.length) {
  3764. let component;
  3765. let componentName;
  3766. // If child is a string, create component with options
  3767. if (typeof child === 'string') {
  3768. componentName = toTitleCase(child);
  3769. const componentClassName = options.componentClass || componentName;
  3770. // Set name through options
  3771. options.name = componentName;
  3772. // Create a new object & element for this controls set
  3773. // If there's no .player_, this is a player
  3774. const ComponentClass = Component.getComponent(componentClassName);
  3775. if (!ComponentClass) {
  3776. throw new Error(`Component ${componentClassName} does not exist`);
  3777. }
  3778. // data stored directly on the videojs object may be
  3779. // misidentified as a component to retain
  3780. // backwards-compatibility with 4.x. check to make sure the
  3781. // component class can be instantiated.
  3782. if (typeof ComponentClass !== 'function') {
  3783. return null;
  3784. }
  3785. component = new ComponentClass(this.player_ || this, options);
  3786. // child is a component instance
  3787. } else {
  3788. component = child;
  3789. }
  3790. if (component.parentComponent_) {
  3791. component.parentComponent_.removeChild(component);
  3792. }
  3793. this.children_.splice(index, 0, component);
  3794. component.parentComponent_ = this;
  3795. if (typeof component.id === 'function') {
  3796. this.childIndex_[component.id()] = component;
  3797. }
  3798. // If a name wasn't used to create the component, check if we can use the
  3799. // name function of the component
  3800. componentName = componentName || component.name && toTitleCase(component.name());
  3801. if (componentName) {
  3802. this.childNameIndex_[componentName] = component;
  3803. this.childNameIndex_[toLowerCase(componentName)] = component;
  3804. }
  3805. // Add the UI object's element to the container div (box)
  3806. // Having an element is not required
  3807. if (typeof component.el === 'function' && component.el()) {
  3808. // If inserting before a component, insert before that component's element
  3809. let refNode = null;
  3810. if (this.children_[index + 1]) {
  3811. // Most children are components, but the video tech is an HTML element
  3812. if (this.children_[index + 1].el_) {
  3813. refNode = this.children_[index + 1].el_;
  3814. } else if (isEl(this.children_[index + 1])) {
  3815. refNode = this.children_[index + 1];
  3816. }
  3817. }
  3818. this.contentEl().insertBefore(component.el(), refNode);
  3819. }
  3820. // Return so it can stored on parent object if desired.
  3821. return component;
  3822. }
  3823. /**
  3824. * Remove a child `Component` from this `Component`s list of children. Also removes
  3825. * the child `Component`s element from this `Component`s element.
  3826. *
  3827. * @param {Component} component
  3828. * The child `Component` to remove.
  3829. */
  3830. removeChild(component) {
  3831. if (typeof component === 'string') {
  3832. component = this.getChild(component);
  3833. }
  3834. if (!component || !this.children_) {
  3835. return;
  3836. }
  3837. let childFound = false;
  3838. for (let i = this.children_.length - 1; i >= 0; i--) {
  3839. if (this.children_[i] === component) {
  3840. childFound = true;
  3841. this.children_.splice(i, 1);
  3842. break;
  3843. }
  3844. }
  3845. if (!childFound) {
  3846. return;
  3847. }
  3848. component.parentComponent_ = null;
  3849. this.childIndex_[component.id()] = null;
  3850. this.childNameIndex_[toTitleCase(component.name())] = null;
  3851. this.childNameIndex_[toLowerCase(component.name())] = null;
  3852. const compEl = component.el();
  3853. if (compEl && compEl.parentNode === this.contentEl()) {
  3854. this.contentEl().removeChild(component.el());
  3855. }
  3856. }
  3857. /**
  3858. * Add and initialize default child `Component`s based upon options.
  3859. */
  3860. initChildren() {
  3861. const children = this.options_.children;
  3862. if (children) {
  3863. // `this` is `parent`
  3864. const parentOptions = this.options_;
  3865. const handleAdd = child => {
  3866. const name = child.name;
  3867. let opts = child.opts;
  3868. // Allow options for children to be set at the parent options
  3869. // e.g. videojs(id, { controlBar: false });
  3870. // instead of videojs(id, { children: { controlBar: false });
  3871. if (parentOptions[name] !== undefined) {
  3872. opts = parentOptions[name];
  3873. }
  3874. // Allow for disabling default components
  3875. // e.g. options['children']['posterImage'] = false
  3876. if (opts === false) {
  3877. return;
  3878. }
  3879. // Allow options to be passed as a simple boolean if no configuration
  3880. // is necessary.
  3881. if (opts === true) {
  3882. opts = {};
  3883. }
  3884. // We also want to pass the original player options
  3885. // to each component as well so they don't need to
  3886. // reach back into the player for options later.
  3887. opts.playerOptions = this.options_.playerOptions;
  3888. // Create and add the child component.
  3889. // Add a direct reference to the child by name on the parent instance.
  3890. // If two of the same component are used, different names should be supplied
  3891. // for each
  3892. const newChild = this.addChild(name, opts);
  3893. if (newChild) {
  3894. this[name] = newChild;
  3895. }
  3896. };
  3897. // Allow for an array of children details to passed in the options
  3898. let workingChildren;
  3899. const Tech = Component.getComponent('Tech');
  3900. if (Array.isArray(children)) {
  3901. workingChildren = children;
  3902. } else {
  3903. workingChildren = Object.keys(children);
  3904. }
  3905. workingChildren
  3906. // children that are in this.options_ but also in workingChildren would
  3907. // give us extra children we do not want. So, we want to filter them out.
  3908. .concat(Object.keys(this.options_).filter(function (child) {
  3909. return !workingChildren.some(function (wchild) {
  3910. if (typeof wchild === 'string') {
  3911. return child === wchild;
  3912. }
  3913. return child === wchild.name;
  3914. });
  3915. })).map(child => {
  3916. let name;
  3917. let opts;
  3918. if (typeof child === 'string') {
  3919. name = child;
  3920. opts = children[name] || this.options_[name] || {};
  3921. } else {
  3922. name = child.name;
  3923. opts = child;
  3924. }
  3925. return {
  3926. name,
  3927. opts
  3928. };
  3929. }).filter(child => {
  3930. // we have to make sure that child.name isn't in the techOrder since
  3931. // techs are registered as Components but can't aren't compatible
  3932. // See https://github.com/videojs/video.js/issues/2772
  3933. const c = Component.getComponent(child.opts.componentClass || toTitleCase(child.name));
  3934. return c && !Tech.isTech(c);
  3935. }).forEach(handleAdd);
  3936. }
  3937. }
  3938. /**
  3939. * Builds the default DOM class name. Should be overridden by sub-components.
  3940. *
  3941. * @return {string}
  3942. * The DOM class name for this object.
  3943. *
  3944. * @abstract
  3945. */
  3946. buildCSSClass() {
  3947. // Child classes can include a function that does:
  3948. // return 'CLASS NAME' + this._super();
  3949. return '';
  3950. }
  3951. /**
  3952. * Bind a listener to the component's ready state.
  3953. * Different from event listeners in that if the ready event has already happened
  3954. * it will trigger the function immediately.
  3955. *
  3956. * @param {ReadyCallback} fn
  3957. * Function that gets called when the `Component` is ready.
  3958. *
  3959. * @return {Component}
  3960. * Returns itself; method can be chained.
  3961. */
  3962. ready(fn, sync = false) {
  3963. if (!fn) {
  3964. return;
  3965. }
  3966. if (!this.isReady_) {
  3967. this.readyQueue_ = this.readyQueue_ || [];
  3968. this.readyQueue_.push(fn);
  3969. return;
  3970. }
  3971. if (sync) {
  3972. fn.call(this);
  3973. } else {
  3974. // Call the function asynchronously by default for consistency
  3975. this.setTimeout(fn, 1);
  3976. }
  3977. }
  3978. /**
  3979. * Trigger all the ready listeners for this `Component`.
  3980. *
  3981. * @fires Component#ready
  3982. */
  3983. triggerReady() {
  3984. this.isReady_ = true;
  3985. // Ensure ready is triggered asynchronously
  3986. this.setTimeout(function () {
  3987. const readyQueue = this.readyQueue_;
  3988. // Reset Ready Queue
  3989. this.readyQueue_ = [];
  3990. if (readyQueue && readyQueue.length > 0) {
  3991. readyQueue.forEach(function (fn) {
  3992. fn.call(this);
  3993. }, this);
  3994. }
  3995. // Allow for using event listeners also
  3996. /**
  3997. * Triggered when a `Component` is ready.
  3998. *
  3999. * @event Component#ready
  4000. * @type {Event}
  4001. */
  4002. this.trigger('ready');
  4003. }, 1);
  4004. }
  4005. /**
  4006. * Find a single DOM element matching a `selector`. This can be within the `Component`s
  4007. * `contentEl()` or another custom context.
  4008. *
  4009. * @param {string} selector
  4010. * A valid CSS selector, which will be passed to `querySelector`.
  4011. *
  4012. * @param {Element|string} [context=this.contentEl()]
  4013. * A DOM element within which to query. Can also be a selector string in
  4014. * which case the first matching element will get used as context. If
  4015. * missing `this.contentEl()` gets used. If `this.contentEl()` returns
  4016. * nothing it falls back to `document`.
  4017. *
  4018. * @return {Element|null}
  4019. * the dom element that was found, or null
  4020. *
  4021. * @see [Information on CSS Selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors)
  4022. */
  4023. $(selector, context) {
  4024. return $(selector, context || this.contentEl());
  4025. }
  4026. /**
  4027. * Finds all DOM element matching a `selector`. This can be within the `Component`s
  4028. * `contentEl()` or another custom context.
  4029. *
  4030. * @param {string} selector
  4031. * A valid CSS selector, which will be passed to `querySelectorAll`.
  4032. *
  4033. * @param {Element|string} [context=this.contentEl()]
  4034. * A DOM element within which to query. Can also be a selector string in
  4035. * which case the first matching element will get used as context. If
  4036. * missing `this.contentEl()` gets used. If `this.contentEl()` returns
  4037. * nothing it falls back to `document`.
  4038. *
  4039. * @return {NodeList}
  4040. * a list of dom elements that were found
  4041. *
  4042. * @see [Information on CSS Selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_Started/Selectors)
  4043. */
  4044. $$(selector, context) {
  4045. return $$(selector, context || this.contentEl());
  4046. }
  4047. /**
  4048. * Check if a component's element has a CSS class name.
  4049. *
  4050. * @param {string} classToCheck
  4051. * CSS class name to check.
  4052. *
  4053. * @return {boolean}
  4054. * - True if the `Component` has the class.
  4055. * - False if the `Component` does not have the class`
  4056. */
  4057. hasClass(classToCheck) {
  4058. return hasClass(this.el_, classToCheck);
  4059. }
  4060. /**
  4061. * Add a CSS class name to the `Component`s element.
  4062. *
  4063. * @param {...string} classesToAdd
  4064. * One or more CSS class name to add.
  4065. */
  4066. addClass(...classesToAdd) {
  4067. addClass(this.el_, ...classesToAdd);
  4068. }
  4069. /**
  4070. * Remove a CSS class name from the `Component`s element.
  4071. *
  4072. * @param {...string} classesToRemove
  4073. * One or more CSS class name to remove.
  4074. */
  4075. removeClass(...classesToRemove) {
  4076. removeClass(this.el_, ...classesToRemove);
  4077. }
  4078. /**
  4079. * Add or remove a CSS class name from the component's element.
  4080. * - `classToToggle` gets added when {@link Component#hasClass} would return false.
  4081. * - `classToToggle` gets removed when {@link Component#hasClass} would return true.
  4082. *
  4083. * @param {string} classToToggle
  4084. * The class to add or remove based on (@link Component#hasClass}
  4085. *
  4086. * @param {boolean|Dom~predicate} [predicate]
  4087. * An {@link Dom~predicate} function or a boolean
  4088. */
  4089. toggleClass(classToToggle, predicate) {
  4090. toggleClass(this.el_, classToToggle, predicate);
  4091. }
  4092. /**
  4093. * Show the `Component`s element if it is hidden by removing the
  4094. * 'vjs-hidden' class name from it.
  4095. */
  4096. show() {
  4097. this.removeClass('vjs-hidden');
  4098. }
  4099. /**
  4100. * Hide the `Component`s element if it is currently showing by adding the
  4101. * 'vjs-hidden` class name to it.
  4102. */
  4103. hide() {
  4104. this.addClass('vjs-hidden');
  4105. }
  4106. /**
  4107. * Lock a `Component`s element in its visible state by adding the 'vjs-lock-showing'
  4108. * class name to it. Used during fadeIn/fadeOut.
  4109. *
  4110. * @private
  4111. */
  4112. lockShowing() {
  4113. this.addClass('vjs-lock-showing');
  4114. }
  4115. /**
  4116. * Unlock a `Component`s element from its visible state by removing the 'vjs-lock-showing'
  4117. * class name from it. Used during fadeIn/fadeOut.
  4118. *
  4119. * @private
  4120. */
  4121. unlockShowing() {
  4122. this.removeClass('vjs-lock-showing');
  4123. }
  4124. /**
  4125. * Get the value of an attribute on the `Component`s element.
  4126. *
  4127. * @param {string} attribute
  4128. * Name of the attribute to get the value from.
  4129. *
  4130. * @return {string|null}
  4131. * - The value of the attribute that was asked for.
  4132. * - Can be an empty string on some browsers if the attribute does not exist
  4133. * or has no value
  4134. * - Most browsers will return null if the attribute does not exist or has
  4135. * no value.
  4136. *
  4137. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute}
  4138. */
  4139. getAttribute(attribute) {
  4140. return getAttribute(this.el_, attribute);
  4141. }
  4142. /**
  4143. * Set the value of an attribute on the `Component`'s element
  4144. *
  4145. * @param {string} attribute
  4146. * Name of the attribute to set.
  4147. *
  4148. * @param {string} value
  4149. * Value to set the attribute to.
  4150. *
  4151. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute}
  4152. */
  4153. setAttribute(attribute, value) {
  4154. setAttribute(this.el_, attribute, value);
  4155. }
  4156. /**
  4157. * Remove an attribute from the `Component`s element.
  4158. *
  4159. * @param {string} attribute
  4160. * Name of the attribute to remove.
  4161. *
  4162. * @see [DOM API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Element/removeAttribute}
  4163. */
  4164. removeAttribute(attribute) {
  4165. removeAttribute(this.el_, attribute);
  4166. }
  4167. /**
  4168. * Get or set the width of the component based upon the CSS styles.
  4169. * See {@link Component#dimension} for more detailed information.
  4170. *
  4171. * @param {number|string} [num]
  4172. * The width that you want to set postfixed with '%', 'px' or nothing.
  4173. *
  4174. * @param {boolean} [skipListeners]
  4175. * Skip the componentresize event trigger
  4176. *
  4177. * @return {number|string}
  4178. * The width when getting, zero if there is no width. Can be a string
  4179. * postpixed with '%' or 'px'.
  4180. */
  4181. width(num, skipListeners) {
  4182. return this.dimension('width', num, skipListeners);
  4183. }
  4184. /**
  4185. * Get or set the height of the component based upon the CSS styles.
  4186. * See {@link Component#dimension} for more detailed information.
  4187. *
  4188. * @param {number|string} [num]
  4189. * The height that you want to set postfixed with '%', 'px' or nothing.
  4190. *
  4191. * @param {boolean} [skipListeners]
  4192. * Skip the componentresize event trigger
  4193. *
  4194. * @return {number|string}
  4195. * The width when getting, zero if there is no width. Can be a string
  4196. * postpixed with '%' or 'px'.
  4197. */
  4198. height(num, skipListeners) {
  4199. return this.dimension('height', num, skipListeners);
  4200. }
  4201. /**
  4202. * Set both the width and height of the `Component` element at the same time.
  4203. *
  4204. * @param {number|string} width
  4205. * Width to set the `Component`s element to.
  4206. *
  4207. * @param {number|string} height
  4208. * Height to set the `Component`s element to.
  4209. */
  4210. dimensions(width, height) {
  4211. // Skip componentresize listeners on width for optimization
  4212. this.width(width, true);
  4213. this.height(height);
  4214. }
  4215. /**
  4216. * Get or set width or height of the `Component` element. This is the shared code
  4217. * for the {@link Component#width} and {@link Component#height}.
  4218. *
  4219. * Things to know:
  4220. * - If the width or height in an number this will return the number postfixed with 'px'.
  4221. * - If the width/height is a percent this will return the percent postfixed with '%'
  4222. * - Hidden elements have a width of 0 with `window.getComputedStyle`. This function
  4223. * defaults to the `Component`s `style.width` and falls back to `window.getComputedStyle`.
  4224. * See [this]{@link http://www.foliotek.com/devblog/getting-the-width-of-a-hidden-element-with-jquery-using-width/}
  4225. * for more information
  4226. * - If you want the computed style of the component, use {@link Component#currentWidth}
  4227. * and {@link {Component#currentHeight}
  4228. *
  4229. * @fires Component#componentresize
  4230. *
  4231. * @param {string} widthOrHeight
  4232. 8 'width' or 'height'
  4233. *
  4234. * @param {number|string} [num]
  4235. 8 New dimension
  4236. *
  4237. * @param {boolean} [skipListeners]
  4238. * Skip componentresize event trigger
  4239. *
  4240. * @return {number}
  4241. * The dimension when getting or 0 if unset
  4242. */
  4243. dimension(widthOrHeight, num, skipListeners) {
  4244. if (num !== undefined) {
  4245. // Set to zero if null or literally NaN (NaN !== NaN)
  4246. if (num === null || num !== num) {
  4247. num = 0;
  4248. }
  4249. // Check if using css width/height (% or px) and adjust
  4250. if (('' + num).indexOf('%') !== -1 || ('' + num).indexOf('px') !== -1) {
  4251. this.el_.style[widthOrHeight] = num;
  4252. } else if (num === 'auto') {
  4253. this.el_.style[widthOrHeight] = '';
  4254. } else {
  4255. this.el_.style[widthOrHeight] = num + 'px';
  4256. }
  4257. // skipListeners allows us to avoid triggering the resize event when setting both width and height
  4258. if (!skipListeners) {
  4259. /**
  4260. * Triggered when a component is resized.
  4261. *
  4262. * @event Component#componentresize
  4263. * @type {Event}
  4264. */
  4265. this.trigger('componentresize');
  4266. }
  4267. return;
  4268. }
  4269. // Not setting a value, so getting it
  4270. // Make sure element exists
  4271. if (!this.el_) {
  4272. return 0;
  4273. }
  4274. // Get dimension value from style
  4275. const val = this.el_.style[widthOrHeight];
  4276. const pxIndex = val.indexOf('px');
  4277. if (pxIndex !== -1) {
  4278. // Return the pixel value with no 'px'
  4279. return parseInt(val.slice(0, pxIndex), 10);
  4280. }
  4281. // No px so using % or no style was set, so falling back to offsetWidth/height
  4282. // If component has display:none, offset will return 0
  4283. // TODO: handle display:none and no dimension style using px
  4284. return parseInt(this.el_['offset' + toTitleCase(widthOrHeight)], 10);
  4285. }
  4286. /**
  4287. * Get the computed width or the height of the component's element.
  4288. *
  4289. * Uses `window.getComputedStyle`.
  4290. *
  4291. * @param {string} widthOrHeight
  4292. * A string containing 'width' or 'height'. Whichever one you want to get.
  4293. *
  4294. * @return {number}
  4295. * The dimension that gets asked for or 0 if nothing was set
  4296. * for that dimension.
  4297. */
  4298. currentDimension(widthOrHeight) {
  4299. let computedWidthOrHeight = 0;
  4300. if (widthOrHeight !== 'width' && widthOrHeight !== 'height') {
  4301. throw new Error('currentDimension only accepts width or height value');
  4302. }
  4303. computedWidthOrHeight = computedStyle(this.el_, widthOrHeight);
  4304. // remove 'px' from variable and parse as integer
  4305. computedWidthOrHeight = parseFloat(computedWidthOrHeight);
  4306. // if the computed value is still 0, it's possible that the browser is lying
  4307. // and we want to check the offset values.
  4308. // This code also runs wherever getComputedStyle doesn't exist.
  4309. if (computedWidthOrHeight === 0 || isNaN(computedWidthOrHeight)) {
  4310. const rule = `offset${toTitleCase(widthOrHeight)}`;
  4311. computedWidthOrHeight = this.el_[rule];
  4312. }
  4313. return computedWidthOrHeight;
  4314. }
  4315. /**
  4316. * An object that contains width and height values of the `Component`s
  4317. * computed style. Uses `window.getComputedStyle`.
  4318. *
  4319. * @typedef {Object} Component~DimensionObject
  4320. *
  4321. * @property {number} width
  4322. * The width of the `Component`s computed style.
  4323. *
  4324. * @property {number} height
  4325. * The height of the `Component`s computed style.
  4326. */
  4327. /**
  4328. * Get an object that contains computed width and height values of the
  4329. * component's element.
  4330. *
  4331. * Uses `window.getComputedStyle`.
  4332. *
  4333. * @return {Component~DimensionObject}
  4334. * The computed dimensions of the component's element.
  4335. */
  4336. currentDimensions() {
  4337. return {
  4338. width: this.currentDimension('width'),
  4339. height: this.currentDimension('height')
  4340. };
  4341. }
  4342. /**
  4343. * Get the computed width of the component's element.
  4344. *
  4345. * Uses `window.getComputedStyle`.
  4346. *
  4347. * @return {number}
  4348. * The computed width of the component's element.
  4349. */
  4350. currentWidth() {
  4351. return this.currentDimension('width');
  4352. }
  4353. /**
  4354. * Get the computed height of the component's element.
  4355. *
  4356. * Uses `window.getComputedStyle`.
  4357. *
  4358. * @return {number}
  4359. * The computed height of the component's element.
  4360. */
  4361. currentHeight() {
  4362. return this.currentDimension('height');
  4363. }
  4364. /**
  4365. * Set the focus to this component
  4366. */
  4367. focus() {
  4368. this.el_.focus();
  4369. }
  4370. /**
  4371. * Remove the focus from this component
  4372. */
  4373. blur() {
  4374. this.el_.blur();
  4375. }
  4376. /**
  4377. * When this Component receives a `keydown` event which it does not process,
  4378. * it passes the event to the Player for handling.
  4379. *
  4380. * @param {KeyboardEvent} event
  4381. * The `keydown` event that caused this function to be called.
  4382. */
  4383. handleKeyDown(event) {
  4384. if (this.player_) {
  4385. // We only stop propagation here because we want unhandled events to fall
  4386. // back to the browser. Exclude Tab for focus trapping.
  4387. if (!keycode.isEventKey(event, 'Tab')) {
  4388. event.stopPropagation();
  4389. }
  4390. this.player_.handleKeyDown(event);
  4391. }
  4392. }
  4393. /**
  4394. * Many components used to have a `handleKeyPress` method, which was poorly
  4395. * named because it listened to a `keydown` event. This method name now
  4396. * delegates to `handleKeyDown`. This means anyone calling `handleKeyPress`
  4397. * will not see their method calls stop working.
  4398. *
  4399. * @param {Event} event
  4400. * The event that caused this function to be called.
  4401. */
  4402. handleKeyPress(event) {
  4403. this.handleKeyDown(event);
  4404. }
  4405. /**
  4406. * Emit a 'tap' events when touch event support gets detected. This gets used to
  4407. * support toggling the controls through a tap on the video. They get enabled
  4408. * because every sub-component would have extra overhead otherwise.
  4409. *
  4410. * @private
  4411. * @fires Component#tap
  4412. * @listens Component#touchstart
  4413. * @listens Component#touchmove
  4414. * @listens Component#touchleave
  4415. * @listens Component#touchcancel
  4416. * @listens Component#touchend
  4417. */
  4418. emitTapEvents() {
  4419. // Track the start time so we can determine how long the touch lasted
  4420. let touchStart = 0;
  4421. let firstTouch = null;
  4422. // Maximum movement allowed during a touch event to still be considered a tap
  4423. // Other popular libs use anywhere from 2 (hammer.js) to 15,
  4424. // so 10 seems like a nice, round number.
  4425. const tapMovementThreshold = 10;
  4426. // The maximum length a touch can be while still being considered a tap
  4427. const touchTimeThreshold = 200;
  4428. let couldBeTap;
  4429. this.on('touchstart', function (event) {
  4430. // If more than one finger, don't consider treating this as a click
  4431. if (event.touches.length === 1) {
  4432. // Copy pageX/pageY from the object
  4433. firstTouch = {
  4434. pageX: event.touches[0].pageX,
  4435. pageY: event.touches[0].pageY
  4436. };
  4437. // Record start time so we can detect a tap vs. "touch and hold"
  4438. touchStart = window.performance.now();
  4439. // Reset couldBeTap tracking
  4440. couldBeTap = true;
  4441. }
  4442. });
  4443. this.on('touchmove', function (event) {
  4444. // If more than one finger, don't consider treating this as a click
  4445. if (event.touches.length > 1) {
  4446. couldBeTap = false;
  4447. } else if (firstTouch) {
  4448. // Some devices will throw touchmoves for all but the slightest of taps.
  4449. // So, if we moved only a small distance, this could still be a tap
  4450. const xdiff = event.touches[0].pageX - firstTouch.pageX;
  4451. const ydiff = event.touches[0].pageY - firstTouch.pageY;
  4452. const touchDistance = Math.sqrt(xdiff * xdiff + ydiff * ydiff);
  4453. if (touchDistance > tapMovementThreshold) {
  4454. couldBeTap = false;
  4455. }
  4456. }
  4457. });
  4458. const noTap = function () {
  4459. couldBeTap = false;
  4460. };
  4461. // TODO: Listen to the original target. http://youtu.be/DujfpXOKUp8?t=13m8s
  4462. this.on('touchleave', noTap);
  4463. this.on('touchcancel', noTap);
  4464. // When the touch ends, measure how long it took and trigger the appropriate
  4465. // event
  4466. this.on('touchend', function (event) {
  4467. firstTouch = null;
  4468. // Proceed only if the touchmove/leave/cancel event didn't happen
  4469. if (couldBeTap === true) {
  4470. // Measure how long the touch lasted
  4471. const touchTime = window.performance.now() - touchStart;
  4472. // Make sure the touch was less than the threshold to be considered a tap
  4473. if (touchTime < touchTimeThreshold) {
  4474. // Don't let browser turn this into a click
  4475. event.preventDefault();
  4476. /**
  4477. * Triggered when a `Component` is tapped.
  4478. *
  4479. * @event Component#tap
  4480. * @type {MouseEvent}
  4481. */
  4482. this.trigger('tap');
  4483. // It may be good to copy the touchend event object and change the
  4484. // type to tap, if the other event properties aren't exact after
  4485. // Events.fixEvent runs (e.g. event.target)
  4486. }
  4487. }
  4488. });
  4489. }
  4490. /**
  4491. * This function reports user activity whenever touch events happen. This can get
  4492. * turned off by any sub-components that wants touch events to act another way.
  4493. *
  4494. * Report user touch activity when touch events occur. User activity gets used to
  4495. * determine when controls should show/hide. It is simple when it comes to mouse
  4496. * events, because any mouse event should show the controls. So we capture mouse
  4497. * events that bubble up to the player and report activity when that happens.
  4498. * With touch events it isn't as easy as `touchstart` and `touchend` toggle player
  4499. * controls. So touch events can't help us at the player level either.
  4500. *
  4501. * User activity gets checked asynchronously. So what could happen is a tap event
  4502. * on the video turns the controls off. Then the `touchend` event bubbles up to
  4503. * the player. Which, if it reported user activity, would turn the controls right
  4504. * back on. We also don't want to completely block touch events from bubbling up.
  4505. * Furthermore a `touchmove` event and anything other than a tap, should not turn
  4506. * controls back on.
  4507. *
  4508. * @listens Component#touchstart
  4509. * @listens Component#touchmove
  4510. * @listens Component#touchend
  4511. * @listens Component#touchcancel
  4512. */
  4513. enableTouchActivity() {
  4514. // Don't continue if the root player doesn't support reporting user activity
  4515. if (!this.player() || !this.player().reportUserActivity) {
  4516. return;
  4517. }
  4518. // listener for reporting that the user is active
  4519. const report = bind_(this.player(), this.player().reportUserActivity);
  4520. let touchHolding;
  4521. this.on('touchstart', function () {
  4522. report();
  4523. // For as long as the they are touching the device or have their mouse down,
  4524. // we consider them active even if they're not moving their finger or mouse.
  4525. // So we want to continue to update that they are active
  4526. this.clearInterval(touchHolding);
  4527. // report at the same interval as activityCheck
  4528. touchHolding = this.setInterval(report, 250);
  4529. });
  4530. const touchEnd = function (event) {
  4531. report();
  4532. // stop the interval that maintains activity if the touch is holding
  4533. this.clearInterval(touchHolding);
  4534. };
  4535. this.on('touchmove', report);
  4536. this.on('touchend', touchEnd);
  4537. this.on('touchcancel', touchEnd);
  4538. }
  4539. /**
  4540. * A callback that has no parameters and is bound into `Component`s context.
  4541. *
  4542. * @callback Component~GenericCallback
  4543. * @this Component
  4544. */
  4545. /**
  4546. * Creates a function that runs after an `x` millisecond timeout. This function is a
  4547. * wrapper around `window.setTimeout`. There are a few reasons to use this one
  4548. * instead though:
  4549. * 1. It gets cleared via {@link Component#clearTimeout} when
  4550. * {@link Component#dispose} gets called.
  4551. * 2. The function callback will gets turned into a {@link Component~GenericCallback}
  4552. *
  4553. * > Note: You can't use `window.clearTimeout` on the id returned by this function. This
  4554. * will cause its dispose listener not to get cleaned up! Please use
  4555. * {@link Component#clearTimeout} or {@link Component#dispose} instead.
  4556. *
  4557. * @param {Component~GenericCallback} fn
  4558. * The function that will be run after `timeout`.
  4559. *
  4560. * @param {number} timeout
  4561. * Timeout in milliseconds to delay before executing the specified function.
  4562. *
  4563. * @return {number}
  4564. * Returns a timeout ID that gets used to identify the timeout. It can also
  4565. * get used in {@link Component#clearTimeout} to clear the timeout that
  4566. * was set.
  4567. *
  4568. * @listens Component#dispose
  4569. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout}
  4570. */
  4571. setTimeout(fn, timeout) {
  4572. // declare as variables so they are properly available in timeout function
  4573. // eslint-disable-next-line
  4574. var timeoutId;
  4575. fn = bind_(this, fn);
  4576. this.clearTimersOnDispose_();
  4577. timeoutId = window.setTimeout(() => {
  4578. if (this.setTimeoutIds_.has(timeoutId)) {
  4579. this.setTimeoutIds_.delete(timeoutId);
  4580. }
  4581. fn();
  4582. }, timeout);
  4583. this.setTimeoutIds_.add(timeoutId);
  4584. return timeoutId;
  4585. }
  4586. /**
  4587. * Clears a timeout that gets created via `window.setTimeout` or
  4588. * {@link Component#setTimeout}. If you set a timeout via {@link Component#setTimeout}
  4589. * use this function instead of `window.clearTimout`. If you don't your dispose
  4590. * listener will not get cleaned up until {@link Component#dispose}!
  4591. *
  4592. * @param {number} timeoutId
  4593. * The id of the timeout to clear. The return value of
  4594. * {@link Component#setTimeout} or `window.setTimeout`.
  4595. *
  4596. * @return {number}
  4597. * Returns the timeout id that was cleared.
  4598. *
  4599. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearTimeout}
  4600. */
  4601. clearTimeout(timeoutId) {
  4602. if (this.setTimeoutIds_.has(timeoutId)) {
  4603. this.setTimeoutIds_.delete(timeoutId);
  4604. window.clearTimeout(timeoutId);
  4605. }
  4606. return timeoutId;
  4607. }
  4608. /**
  4609. * Creates a function that gets run every `x` milliseconds. This function is a wrapper
  4610. * around `window.setInterval`. There are a few reasons to use this one instead though.
  4611. * 1. It gets cleared via {@link Component#clearInterval} when
  4612. * {@link Component#dispose} gets called.
  4613. * 2. The function callback will be a {@link Component~GenericCallback}
  4614. *
  4615. * @param {Component~GenericCallback} fn
  4616. * The function to run every `x` seconds.
  4617. *
  4618. * @param {number} interval
  4619. * Execute the specified function every `x` milliseconds.
  4620. *
  4621. * @return {number}
  4622. * Returns an id that can be used to identify the interval. It can also be be used in
  4623. * {@link Component#clearInterval} to clear the interval.
  4624. *
  4625. * @listens Component#dispose
  4626. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval}
  4627. */
  4628. setInterval(fn, interval) {
  4629. fn = bind_(this, fn);
  4630. this.clearTimersOnDispose_();
  4631. const intervalId = window.setInterval(fn, interval);
  4632. this.setIntervalIds_.add(intervalId);
  4633. return intervalId;
  4634. }
  4635. /**
  4636. * Clears an interval that gets created via `window.setInterval` or
  4637. * {@link Component#setInterval}. If you set an interval via {@link Component#setInterval}
  4638. * use this function instead of `window.clearInterval`. If you don't your dispose
  4639. * listener will not get cleaned up until {@link Component#dispose}!
  4640. *
  4641. * @param {number} intervalId
  4642. * The id of the interval to clear. The return value of
  4643. * {@link Component#setInterval} or `window.setInterval`.
  4644. *
  4645. * @return {number}
  4646. * Returns the interval id that was cleared.
  4647. *
  4648. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearInterval}
  4649. */
  4650. clearInterval(intervalId) {
  4651. if (this.setIntervalIds_.has(intervalId)) {
  4652. this.setIntervalIds_.delete(intervalId);
  4653. window.clearInterval(intervalId);
  4654. }
  4655. return intervalId;
  4656. }
  4657. /**
  4658. * Queues up a callback to be passed to requestAnimationFrame (rAF), but
  4659. * with a few extra bonuses:
  4660. *
  4661. * - Supports browsers that do not support rAF by falling back to
  4662. * {@link Component#setTimeout}.
  4663. *
  4664. * - The callback is turned into a {@link Component~GenericCallback} (i.e.
  4665. * bound to the component).
  4666. *
  4667. * - Automatic cancellation of the rAF callback is handled if the component
  4668. * is disposed before it is called.
  4669. *
  4670. * @param {Component~GenericCallback} fn
  4671. * A function that will be bound to this component and executed just
  4672. * before the browser's next repaint.
  4673. *
  4674. * @return {number}
  4675. * Returns an rAF ID that gets used to identify the timeout. It can
  4676. * also be used in {@link Component#cancelAnimationFrame} to cancel
  4677. * the animation frame callback.
  4678. *
  4679. * @listens Component#dispose
  4680. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame}
  4681. */
  4682. requestAnimationFrame(fn) {
  4683. this.clearTimersOnDispose_();
  4684. // declare as variables so they are properly available in rAF function
  4685. // eslint-disable-next-line
  4686. var id;
  4687. fn = bind_(this, fn);
  4688. id = window.requestAnimationFrame(() => {
  4689. if (this.rafIds_.has(id)) {
  4690. this.rafIds_.delete(id);
  4691. }
  4692. fn();
  4693. });
  4694. this.rafIds_.add(id);
  4695. return id;
  4696. }
  4697. /**
  4698. * Request an animation frame, but only one named animation
  4699. * frame will be queued. Another will never be added until
  4700. * the previous one finishes.
  4701. *
  4702. * @param {string} name
  4703. * The name to give this requestAnimationFrame
  4704. *
  4705. * @param {Component~GenericCallback} fn
  4706. * A function that will be bound to this component and executed just
  4707. * before the browser's next repaint.
  4708. */
  4709. requestNamedAnimationFrame(name, fn) {
  4710. if (this.namedRafs_.has(name)) {
  4711. return;
  4712. }
  4713. this.clearTimersOnDispose_();
  4714. fn = bind_(this, fn);
  4715. const id = this.requestAnimationFrame(() => {
  4716. fn();
  4717. if (this.namedRafs_.has(name)) {
  4718. this.namedRafs_.delete(name);
  4719. }
  4720. });
  4721. this.namedRafs_.set(name, id);
  4722. return name;
  4723. }
  4724. /**
  4725. * Cancels a current named animation frame if it exists.
  4726. *
  4727. * @param {string} name
  4728. * The name of the requestAnimationFrame to cancel.
  4729. */
  4730. cancelNamedAnimationFrame(name) {
  4731. if (!this.namedRafs_.has(name)) {
  4732. return;
  4733. }
  4734. this.cancelAnimationFrame(this.namedRafs_.get(name));
  4735. this.namedRafs_.delete(name);
  4736. }
  4737. /**
  4738. * Cancels a queued callback passed to {@link Component#requestAnimationFrame}
  4739. * (rAF).
  4740. *
  4741. * If you queue an rAF callback via {@link Component#requestAnimationFrame},
  4742. * use this function instead of `window.cancelAnimationFrame`. If you don't,
  4743. * your dispose listener will not get cleaned up until {@link Component#dispose}!
  4744. *
  4745. * @param {number} id
  4746. * The rAF ID to clear. The return value of {@link Component#requestAnimationFrame}.
  4747. *
  4748. * @return {number}
  4749. * Returns the rAF ID that was cleared.
  4750. *
  4751. * @see [Similar to]{@link https://developer.mozilla.org/en-US/docs/Web/API/window/cancelAnimationFrame}
  4752. */
  4753. cancelAnimationFrame(id) {
  4754. if (this.rafIds_.has(id)) {
  4755. this.rafIds_.delete(id);
  4756. window.cancelAnimationFrame(id);
  4757. }
  4758. return id;
  4759. }
  4760. /**
  4761. * A function to setup `requestAnimationFrame`, `setTimeout`,
  4762. * and `setInterval`, clearing on dispose.
  4763. *
  4764. * > Previously each timer added and removed dispose listeners on it's own.
  4765. * For better performance it was decided to batch them all, and use `Set`s
  4766. * to track outstanding timer ids.
  4767. *
  4768. * @private
  4769. */
  4770. clearTimersOnDispose_() {
  4771. if (this.clearingTimersOnDispose_) {
  4772. return;
  4773. }
  4774. this.clearingTimersOnDispose_ = true;
  4775. this.one('dispose', () => {
  4776. [['namedRafs_', 'cancelNamedAnimationFrame'], ['rafIds_', 'cancelAnimationFrame'], ['setTimeoutIds_', 'clearTimeout'], ['setIntervalIds_', 'clearInterval']].forEach(([idName, cancelName]) => {
  4777. // for a `Set` key will actually be the value again
  4778. // so forEach((val, val) =>` but for maps we want to use
  4779. // the key.
  4780. this[idName].forEach((val, key) => this[cancelName](key));
  4781. });
  4782. this.clearingTimersOnDispose_ = false;
  4783. });
  4784. }
  4785. /**
  4786. * Register a `Component` with `videojs` given the name and the component.
  4787. *
  4788. * > NOTE: {@link Tech}s should not be registered as a `Component`. {@link Tech}s
  4789. * should be registered using {@link Tech.registerTech} or
  4790. * {@link videojs:videojs.registerTech}.
  4791. *
  4792. * > NOTE: This function can also be seen on videojs as
  4793. * {@link videojs:videojs.registerComponent}.
  4794. *
  4795. * @param {string} name
  4796. * The name of the `Component` to register.
  4797. *
  4798. * @param {Component} ComponentToRegister
  4799. * The `Component` class to register.
  4800. *
  4801. * @return {Component}
  4802. * The `Component` that was registered.
  4803. */
  4804. static registerComponent(name, ComponentToRegister) {
  4805. if (typeof name !== 'string' || !name) {
  4806. throw new Error(`Illegal component name, "${name}"; must be a non-empty string.`);
  4807. }
  4808. const Tech = Component.getComponent('Tech');
  4809. // We need to make sure this check is only done if Tech has been registered.
  4810. const isTech = Tech && Tech.isTech(ComponentToRegister);
  4811. const isComp = Component === ComponentToRegister || Component.prototype.isPrototypeOf(ComponentToRegister.prototype);
  4812. if (isTech || !isComp) {
  4813. let reason;
  4814. if (isTech) {
  4815. reason = 'techs must be registered using Tech.registerTech()';
  4816. } else {
  4817. reason = 'must be a Component subclass';
  4818. }
  4819. throw new Error(`Illegal component, "${name}"; ${reason}.`);
  4820. }
  4821. name = toTitleCase(name);
  4822. if (!Component.components_) {
  4823. Component.components_ = {};
  4824. }
  4825. const Player = Component.getComponent('Player');
  4826. if (name === 'Player' && Player && Player.players) {
  4827. const players = Player.players;
  4828. const playerNames = Object.keys(players);
  4829. // If we have players that were disposed, then their name will still be
  4830. // in Players.players. So, we must loop through and verify that the value
  4831. // for each item is not null. This allows registration of the Player component
  4832. // after all players have been disposed or before any were created.
  4833. if (players && playerNames.length > 0 && playerNames.map(pname => players[pname]).every(Boolean)) {
  4834. throw new Error('Can not register Player component after player has been created.');
  4835. }
  4836. }
  4837. Component.components_[name] = ComponentToRegister;
  4838. Component.components_[toLowerCase(name)] = ComponentToRegister;
  4839. return ComponentToRegister;
  4840. }
  4841. /**
  4842. * Get a `Component` based on the name it was registered with.
  4843. *
  4844. * @param {string} name
  4845. * The Name of the component to get.
  4846. *
  4847. * @return {Component}
  4848. * The `Component` that got registered under the given name.
  4849. */
  4850. static getComponent(name) {
  4851. if (!name || !Component.components_) {
  4852. return;
  4853. }
  4854. return Component.components_[name];
  4855. }
  4856. }
  4857. Component.registerComponent('Component', Component);
  4858. /**
  4859. * @file time.js
  4860. * @module time
  4861. */
  4862. /**
  4863. * Returns the time for the specified index at the start or end
  4864. * of a TimeRange object.
  4865. *
  4866. * @typedef {Function} TimeRangeIndex
  4867. *
  4868. * @param {number} [index=0]
  4869. * The range number to return the time for.
  4870. *
  4871. * @return {number}
  4872. * The time offset at the specified index.
  4873. *
  4874. * @deprecated The index argument must be provided.
  4875. * In the future, leaving it out will throw an error.
  4876. */
  4877. /**
  4878. * An object that contains ranges of time, which mimics {@link TimeRanges}.
  4879. *
  4880. * @typedef {Object} TimeRange
  4881. *
  4882. * @property {number} length
  4883. * The number of time ranges represented by this object.
  4884. *
  4885. * @property {module:time~TimeRangeIndex} start
  4886. * Returns the time offset at which a specified time range begins.
  4887. *
  4888. * @property {module:time~TimeRangeIndex} end
  4889. * Returns the time offset at which a specified time range ends.
  4890. *
  4891. * @see https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges
  4892. */
  4893. /**
  4894. * Check if any of the time ranges are over the maximum index.
  4895. *
  4896. * @private
  4897. * @param {string} fnName
  4898. * The function name to use for logging
  4899. *
  4900. * @param {number} index
  4901. * The index to check
  4902. *
  4903. * @param {number} maxIndex
  4904. * The maximum possible index
  4905. *
  4906. * @throws {Error} if the timeRanges provided are over the maxIndex
  4907. */
  4908. function rangeCheck(fnName, index, maxIndex) {
  4909. if (typeof index !== 'number' || index < 0 || index > maxIndex) {
  4910. throw new Error(`Failed to execute '${fnName}' on 'TimeRanges': The index provided (${index}) is non-numeric or out of bounds (0-${maxIndex}).`);
  4911. }
  4912. }
  4913. /**
  4914. * Get the time for the specified index at the start or end
  4915. * of a TimeRange object.
  4916. *
  4917. * @private
  4918. * @param {string} fnName
  4919. * The function name to use for logging
  4920. *
  4921. * @param {string} valueIndex
  4922. * The property that should be used to get the time. should be
  4923. * 'start' or 'end'
  4924. *
  4925. * @param {Array} ranges
  4926. * An array of time ranges
  4927. *
  4928. * @param {Array} [rangeIndex=0]
  4929. * The index to start the search at
  4930. *
  4931. * @return {number}
  4932. * The time that offset at the specified index.
  4933. *
  4934. * @deprecated rangeIndex must be set to a value, in the future this will throw an error.
  4935. * @throws {Error} if rangeIndex is more than the length of ranges
  4936. */
  4937. function getRange(fnName, valueIndex, ranges, rangeIndex) {
  4938. rangeCheck(fnName, rangeIndex, ranges.length - 1);
  4939. return ranges[rangeIndex][valueIndex];
  4940. }
  4941. /**
  4942. * Create a time range object given ranges of time.
  4943. *
  4944. * @private
  4945. * @param {Array} [ranges]
  4946. * An array of time ranges.
  4947. *
  4948. * @return {TimeRange}
  4949. */
  4950. function createTimeRangesObj(ranges) {
  4951. let timeRangesObj;
  4952. if (ranges === undefined || ranges.length === 0) {
  4953. timeRangesObj = {
  4954. length: 0,
  4955. start() {
  4956. throw new Error('This TimeRanges object is empty');
  4957. },
  4958. end() {
  4959. throw new Error('This TimeRanges object is empty');
  4960. }
  4961. };
  4962. } else {
  4963. timeRangesObj = {
  4964. length: ranges.length,
  4965. start: getRange.bind(null, 'start', 0, ranges),
  4966. end: getRange.bind(null, 'end', 1, ranges)
  4967. };
  4968. }
  4969. if (window.Symbol && window.Symbol.iterator) {
  4970. timeRangesObj[window.Symbol.iterator] = () => (ranges || []).values();
  4971. }
  4972. return timeRangesObj;
  4973. }
  4974. /**
  4975. * Create a `TimeRange` object which mimics an
  4976. * {@link https://developer.mozilla.org/en-US/docs/Web/API/TimeRanges|HTML5 TimeRanges instance}.
  4977. *
  4978. * @param {number|Array[]} start
  4979. * The start of a single range (a number) or an array of ranges (an
  4980. * array of arrays of two numbers each).
  4981. *
  4982. * @param {number} end
  4983. * The end of a single range. Cannot be used with the array form of
  4984. * the `start` argument.
  4985. *
  4986. * @return {TimeRange}
  4987. */
  4988. function createTimeRanges(start, end) {
  4989. if (Array.isArray(start)) {
  4990. return createTimeRangesObj(start);
  4991. } else if (start === undefined || end === undefined) {
  4992. return createTimeRangesObj();
  4993. }
  4994. return createTimeRangesObj([[start, end]]);
  4995. }
  4996. /**
  4997. * Format seconds as a time string, H:MM:SS or M:SS. Supplying a guide (in
  4998. * seconds) will force a number of leading zeros to cover the length of the
  4999. * guide.
  5000. *
  5001. * @private
  5002. * @param {number} seconds
  5003. * Number of seconds to be turned into a string
  5004. *
  5005. * @param {number} guide
  5006. * Number (in seconds) to model the string after
  5007. *
  5008. * @return {string}
  5009. * Time formatted as H:MM:SS or M:SS
  5010. */
  5011. const defaultImplementation = function (seconds, guide) {
  5012. seconds = seconds < 0 ? 0 : seconds;
  5013. let s = Math.floor(seconds % 60);
  5014. let m = Math.floor(seconds / 60 % 60);
  5015. let h = Math.floor(seconds / 3600);
  5016. const gm = Math.floor(guide / 60 % 60);
  5017. const gh = Math.floor(guide / 3600);
  5018. // handle invalid times
  5019. if (isNaN(seconds) || seconds === Infinity) {
  5020. // '-' is false for all relational operators (e.g. <, >=) so this setting
  5021. // will add the minimum number of fields specified by the guide
  5022. h = m = s = '-';
  5023. }
  5024. // Check if we need to show hours
  5025. h = h > 0 || gh > 0 ? h + ':' : '';
  5026. // If hours are showing, we may need to add a leading zero.
  5027. // Always show at least one digit of minutes.
  5028. m = ((h || gm >= 10) && m < 10 ? '0' + m : m) + ':';
  5029. // Check if leading zero is need for seconds
  5030. s = s < 10 ? '0' + s : s;
  5031. return h + m + s;
  5032. };
  5033. // Internal pointer to the current implementation.
  5034. let implementation = defaultImplementation;
  5035. /**
  5036. * Replaces the default formatTime implementation with a custom implementation.
  5037. *
  5038. * @param {Function} customImplementation
  5039. * A function which will be used in place of the default formatTime
  5040. * implementation. Will receive the current time in seconds and the
  5041. * guide (in seconds) as arguments.
  5042. */
  5043. function setFormatTime(customImplementation) {
  5044. implementation = customImplementation;
  5045. }
  5046. /**
  5047. * Resets formatTime to the default implementation.
  5048. */
  5049. function resetFormatTime() {
  5050. implementation = defaultImplementation;
  5051. }
  5052. /**
  5053. * Delegates to either the default time formatting function or a custom
  5054. * function supplied via `setFormatTime`.
  5055. *
  5056. * Formats seconds as a time string (H:MM:SS or M:SS). Supplying a
  5057. * guide (in seconds) will force a number of leading zeros to cover the
  5058. * length of the guide.
  5059. *
  5060. * @example formatTime(125, 600) === "02:05"
  5061. * @param {number} seconds
  5062. * Number of seconds to be turned into a string
  5063. *
  5064. * @param {number} guide
  5065. * Number (in seconds) to model the string after
  5066. *
  5067. * @return {string}
  5068. * Time formatted as H:MM:SS or M:SS
  5069. */
  5070. function formatTime(seconds, guide = seconds) {
  5071. return implementation(seconds, guide);
  5072. }
  5073. var Time = /*#__PURE__*/Object.freeze({
  5074. __proto__: null,
  5075. createTimeRanges: createTimeRanges,
  5076. createTimeRange: createTimeRanges,
  5077. setFormatTime: setFormatTime,
  5078. resetFormatTime: resetFormatTime,
  5079. formatTime: formatTime
  5080. });
  5081. /**
  5082. * @file buffer.js
  5083. * @module buffer
  5084. */
  5085. /**
  5086. * Compute the percentage of the media that has been buffered.
  5087. *
  5088. * @param { import('./time').TimeRange } buffered
  5089. * The current `TimeRanges` object representing buffered time ranges
  5090. *
  5091. * @param {number} duration
  5092. * Total duration of the media
  5093. *
  5094. * @return {number}
  5095. * Percent buffered of the total duration in decimal form.
  5096. */
  5097. function bufferedPercent(buffered, duration) {
  5098. let bufferedDuration = 0;
  5099. let start;
  5100. let end;
  5101. if (!duration) {
  5102. return 0;
  5103. }
  5104. if (!buffered || !buffered.length) {
  5105. buffered = createTimeRanges(0, 0);
  5106. }
  5107. for (let i = 0; i < buffered.length; i++) {
  5108. start = buffered.start(i);
  5109. end = buffered.end(i);
  5110. // buffered end can be bigger than duration by a very small fraction
  5111. if (end > duration) {
  5112. end = duration;
  5113. }
  5114. bufferedDuration += end - start;
  5115. }
  5116. return bufferedDuration / duration;
  5117. }
  5118. /**
  5119. * @file media-error.js
  5120. */
  5121. /**
  5122. * A Custom `MediaError` class which mimics the standard HTML5 `MediaError` class.
  5123. *
  5124. * @param {number|string|Object|MediaError} value
  5125. * This can be of multiple types:
  5126. * - number: should be a standard error code
  5127. * - string: an error message (the code will be 0)
  5128. * - Object: arbitrary properties
  5129. * - `MediaError` (native): used to populate a video.js `MediaError` object
  5130. * - `MediaError` (video.js): will return itself if it's already a
  5131. * video.js `MediaError` object.
  5132. *
  5133. * @see [MediaError Spec]{@link https://dev.w3.org/html5/spec-author-view/video.html#mediaerror}
  5134. * @see [Encrypted MediaError Spec]{@link https://www.w3.org/TR/2013/WD-encrypted-media-20130510/#error-codes}
  5135. *
  5136. * @class MediaError
  5137. */
  5138. function MediaError(value) {
  5139. // Allow redundant calls to this constructor to avoid having `instanceof`
  5140. // checks peppered around the code.
  5141. if (value instanceof MediaError) {
  5142. return value;
  5143. }
  5144. if (typeof value === 'number') {
  5145. this.code = value;
  5146. } else if (typeof value === 'string') {
  5147. // default code is zero, so this is a custom error
  5148. this.message = value;
  5149. } else if (isObject(value)) {
  5150. // We assign the `code` property manually because native `MediaError` objects
  5151. // do not expose it as an own/enumerable property of the object.
  5152. if (typeof value.code === 'number') {
  5153. this.code = value.code;
  5154. }
  5155. Object.assign(this, value);
  5156. }
  5157. if (!this.message) {
  5158. this.message = MediaError.defaultMessages[this.code] || '';
  5159. }
  5160. }
  5161. /**
  5162. * The error code that refers two one of the defined `MediaError` types
  5163. *
  5164. * @type {Number}
  5165. */
  5166. MediaError.prototype.code = 0;
  5167. /**
  5168. * An optional message that to show with the error. Message is not part of the HTML5
  5169. * video spec but allows for more informative custom errors.
  5170. *
  5171. * @type {String}
  5172. */
  5173. MediaError.prototype.message = '';
  5174. /**
  5175. * An optional status code that can be set by plugins to allow even more detail about
  5176. * the error. For example a plugin might provide a specific HTTP status code and an
  5177. * error message for that code. Then when the plugin gets that error this class will
  5178. * know how to display an error message for it. This allows a custom message to show
  5179. * up on the `Player` error overlay.
  5180. *
  5181. * @type {Array}
  5182. */
  5183. MediaError.prototype.status = null;
  5184. /**
  5185. * Errors indexed by the W3C standard. The order **CANNOT CHANGE**! See the
  5186. * specification listed under {@link MediaError} for more information.
  5187. *
  5188. * @enum {array}
  5189. * @readonly
  5190. * @property {string} 0 - MEDIA_ERR_CUSTOM
  5191. * @property {string} 1 - MEDIA_ERR_ABORTED
  5192. * @property {string} 2 - MEDIA_ERR_NETWORK
  5193. * @property {string} 3 - MEDIA_ERR_DECODE
  5194. * @property {string} 4 - MEDIA_ERR_SRC_NOT_SUPPORTED
  5195. * @property {string} 5 - MEDIA_ERR_ENCRYPTED
  5196. */
  5197. MediaError.errorTypes = ['MEDIA_ERR_CUSTOM', 'MEDIA_ERR_ABORTED', 'MEDIA_ERR_NETWORK', 'MEDIA_ERR_DECODE', 'MEDIA_ERR_SRC_NOT_SUPPORTED', 'MEDIA_ERR_ENCRYPTED'];
  5198. /**
  5199. * The default `MediaError` messages based on the {@link MediaError.errorTypes}.
  5200. *
  5201. * @type {Array}
  5202. * @constant
  5203. */
  5204. MediaError.defaultMessages = {
  5205. 1: 'You aborted the media playback',
  5206. 2: 'A network error caused the media download to fail part-way.',
  5207. 3: 'The media playback was aborted due to a corruption problem or because the media used features your browser did not support.',
  5208. 4: 'The media could not be loaded, either because the server or network failed or because the format is not supported.',
  5209. 5: 'The media is encrypted and we do not have the keys to decrypt it.'
  5210. };
  5211. // Add types as properties on MediaError
  5212. // e.g. MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED = 4;
  5213. for (let errNum = 0; errNum < MediaError.errorTypes.length; errNum++) {
  5214. MediaError[MediaError.errorTypes[errNum]] = errNum;
  5215. // values should be accessible on both the class and instance
  5216. MediaError.prototype[MediaError.errorTypes[errNum]] = errNum;
  5217. }
  5218. var tuple = SafeParseTuple;
  5219. function SafeParseTuple(obj, reviver) {
  5220. var json;
  5221. var error = null;
  5222. try {
  5223. json = JSON.parse(obj, reviver);
  5224. } catch (err) {
  5225. error = err;
  5226. }
  5227. return [error, json];
  5228. }
  5229. /**
  5230. * Returns whether an object is `Promise`-like (i.e. has a `then` method).
  5231. *
  5232. * @param {Object} value
  5233. * An object that may or may not be `Promise`-like.
  5234. *
  5235. * @return {boolean}
  5236. * Whether or not the object is `Promise`-like.
  5237. */
  5238. function isPromise(value) {
  5239. return value !== undefined && value !== null && typeof value.then === 'function';
  5240. }
  5241. /**
  5242. * Silence a Promise-like object.
  5243. *
  5244. * This is useful for avoiding non-harmful, but potentially confusing "uncaught
  5245. * play promise" rejection error messages.
  5246. *
  5247. * @param {Object} value
  5248. * An object that may or may not be `Promise`-like.
  5249. */
  5250. function silencePromise(value) {
  5251. if (isPromise(value)) {
  5252. value.then(null, e => {});
  5253. }
  5254. }
  5255. /**
  5256. * @file text-track-list-converter.js Utilities for capturing text track state and
  5257. * re-creating tracks based on a capture.
  5258. *
  5259. * @module text-track-list-converter
  5260. */
  5261. /**
  5262. * Examine a single {@link TextTrack} and return a JSON-compatible javascript object that
  5263. * represents the {@link TextTrack}'s state.
  5264. *
  5265. * @param {TextTrack} track
  5266. * The text track to query.
  5267. *
  5268. * @return {Object}
  5269. * A serializable javascript representation of the TextTrack.
  5270. * @private
  5271. */
  5272. const trackToJson_ = function (track) {
  5273. const ret = ['kind', 'label', 'language', 'id', 'inBandMetadataTrackDispatchType', 'mode', 'src'].reduce((acc, prop, i) => {
  5274. if (track[prop]) {
  5275. acc[prop] = track[prop];
  5276. }
  5277. return acc;
  5278. }, {
  5279. cues: track.cues && Array.prototype.map.call(track.cues, function (cue) {
  5280. return {
  5281. startTime: cue.startTime,
  5282. endTime: cue.endTime,
  5283. text: cue.text,
  5284. id: cue.id
  5285. };
  5286. })
  5287. });
  5288. return ret;
  5289. };
  5290. /**
  5291. * Examine a {@link Tech} and return a JSON-compatible javascript array that represents the
  5292. * state of all {@link TextTrack}s currently configured. The return array is compatible with
  5293. * {@link text-track-list-converter:jsonToTextTracks}.
  5294. *
  5295. * @param { import('../tech/tech').default } tech
  5296. * The tech object to query
  5297. *
  5298. * @return {Array}
  5299. * A serializable javascript representation of the {@link Tech}s
  5300. * {@link TextTrackList}.
  5301. */
  5302. const textTracksToJson = function (tech) {
  5303. const trackEls = tech.$$('track');
  5304. const trackObjs = Array.prototype.map.call(trackEls, t => t.track);
  5305. const tracks = Array.prototype.map.call(trackEls, function (trackEl) {
  5306. const json = trackToJson_(trackEl.track);
  5307. if (trackEl.src) {
  5308. json.src = trackEl.src;
  5309. }
  5310. return json;
  5311. });
  5312. return tracks.concat(Array.prototype.filter.call(tech.textTracks(), function (track) {
  5313. return trackObjs.indexOf(track) === -1;
  5314. }).map(trackToJson_));
  5315. };
  5316. /**
  5317. * Create a set of remote {@link TextTrack}s on a {@link Tech} based on an array of javascript
  5318. * object {@link TextTrack} representations.
  5319. *
  5320. * @param {Array} json
  5321. * An array of `TextTrack` representation objects, like those that would be
  5322. * produced by `textTracksToJson`.
  5323. *
  5324. * @param {Tech} tech
  5325. * The `Tech` to create the `TextTrack`s on.
  5326. */
  5327. const jsonToTextTracks = function (json, tech) {
  5328. json.forEach(function (track) {
  5329. const addedTrack = tech.addRemoteTextTrack(track).track;
  5330. if (!track.src && track.cues) {
  5331. track.cues.forEach(cue => addedTrack.addCue(cue));
  5332. }
  5333. });
  5334. return tech.textTracks();
  5335. };
  5336. var textTrackConverter = {
  5337. textTracksToJson,
  5338. jsonToTextTracks,
  5339. trackToJson_
  5340. };
  5341. /**
  5342. * @file modal-dialog.js
  5343. */
  5344. const MODAL_CLASS_NAME = 'vjs-modal-dialog';
  5345. /**
  5346. * The `ModalDialog` displays over the video and its controls, which blocks
  5347. * interaction with the player until it is closed.
  5348. *
  5349. * Modal dialogs include a "Close" button and will close when that button
  5350. * is activated - or when ESC is pressed anywhere.
  5351. *
  5352. * @extends Component
  5353. */
  5354. class ModalDialog extends Component {
  5355. /**
  5356. * Create an instance of this class.
  5357. *
  5358. * @param { import('./player').default } player
  5359. * The `Player` that this class should be attached to.
  5360. *
  5361. * @param {Object} [options]
  5362. * The key/value store of player options.
  5363. *
  5364. * @param { import('./utils/dom').ContentDescriptor} [options.content=undefined]
  5365. * Provide customized content for this modal.
  5366. *
  5367. * @param {string} [options.description]
  5368. * A text description for the modal, primarily for accessibility.
  5369. *
  5370. * @param {boolean} [options.fillAlways=false]
  5371. * Normally, modals are automatically filled only the first time
  5372. * they open. This tells the modal to refresh its content
  5373. * every time it opens.
  5374. *
  5375. * @param {string} [options.label]
  5376. * A text label for the modal, primarily for accessibility.
  5377. *
  5378. * @param {boolean} [options.pauseOnOpen=true]
  5379. * If `true`, playback will will be paused if playing when
  5380. * the modal opens, and resumed when it closes.
  5381. *
  5382. * @param {boolean} [options.temporary=true]
  5383. * If `true`, the modal can only be opened once; it will be
  5384. * disposed as soon as it's closed.
  5385. *
  5386. * @param {boolean} [options.uncloseable=false]
  5387. * If `true`, the user will not be able to close the modal
  5388. * through the UI in the normal ways. Programmatic closing is
  5389. * still possible.
  5390. */
  5391. constructor(player, options) {
  5392. super(player, options);
  5393. this.handleKeyDown_ = e => this.handleKeyDown(e);
  5394. this.close_ = e => this.close(e);
  5395. this.opened_ = this.hasBeenOpened_ = this.hasBeenFilled_ = false;
  5396. this.closeable(!this.options_.uncloseable);
  5397. this.content(this.options_.content);
  5398. // Make sure the contentEl is defined AFTER any children are initialized
  5399. // because we only want the contents of the modal in the contentEl
  5400. // (not the UI elements like the close button).
  5401. this.contentEl_ = createEl('div', {
  5402. className: `${MODAL_CLASS_NAME}-content`
  5403. }, {
  5404. role: 'document'
  5405. });
  5406. this.descEl_ = createEl('p', {
  5407. className: `${MODAL_CLASS_NAME}-description vjs-control-text`,
  5408. id: this.el().getAttribute('aria-describedby')
  5409. });
  5410. textContent(this.descEl_, this.description());
  5411. this.el_.appendChild(this.descEl_);
  5412. this.el_.appendChild(this.contentEl_);
  5413. }
  5414. /**
  5415. * Create the `ModalDialog`'s DOM element
  5416. *
  5417. * @return {Element}
  5418. * The DOM element that gets created.
  5419. */
  5420. createEl() {
  5421. return super.createEl('div', {
  5422. className: this.buildCSSClass(),
  5423. tabIndex: -1
  5424. }, {
  5425. 'aria-describedby': `${this.id()}_description`,
  5426. 'aria-hidden': 'true',
  5427. 'aria-label': this.label(),
  5428. 'role': 'dialog'
  5429. });
  5430. }
  5431. dispose() {
  5432. this.contentEl_ = null;
  5433. this.descEl_ = null;
  5434. this.previouslyActiveEl_ = null;
  5435. super.dispose();
  5436. }
  5437. /**
  5438. * Builds the default DOM `className`.
  5439. *
  5440. * @return {string}
  5441. * The DOM `className` for this object.
  5442. */
  5443. buildCSSClass() {
  5444. return `${MODAL_CLASS_NAME} vjs-hidden ${super.buildCSSClass()}`;
  5445. }
  5446. /**
  5447. * Returns the label string for this modal. Primarily used for accessibility.
  5448. *
  5449. * @return {string}
  5450. * the localized or raw label of this modal.
  5451. */
  5452. label() {
  5453. return this.localize(this.options_.label || 'Modal Window');
  5454. }
  5455. /**
  5456. * Returns the description string for this modal. Primarily used for
  5457. * accessibility.
  5458. *
  5459. * @return {string}
  5460. * The localized or raw description of this modal.
  5461. */
  5462. description() {
  5463. let desc = this.options_.description || this.localize('This is a modal window.');
  5464. // Append a universal closeability message if the modal is closeable.
  5465. if (this.closeable()) {
  5466. desc += ' ' + this.localize('This modal can be closed by pressing the Escape key or activating the close button.');
  5467. }
  5468. return desc;
  5469. }
  5470. /**
  5471. * Opens the modal.
  5472. *
  5473. * @fires ModalDialog#beforemodalopen
  5474. * @fires ModalDialog#modalopen
  5475. */
  5476. open() {
  5477. if (!this.opened_) {
  5478. const player = this.player();
  5479. /**
  5480. * Fired just before a `ModalDialog` is opened.
  5481. *
  5482. * @event ModalDialog#beforemodalopen
  5483. * @type {Event}
  5484. */
  5485. this.trigger('beforemodalopen');
  5486. this.opened_ = true;
  5487. // Fill content if the modal has never opened before and
  5488. // never been filled.
  5489. if (this.options_.fillAlways || !this.hasBeenOpened_ && !this.hasBeenFilled_) {
  5490. this.fill();
  5491. }
  5492. // If the player was playing, pause it and take note of its previously
  5493. // playing state.
  5494. this.wasPlaying_ = !player.paused();
  5495. if (this.options_.pauseOnOpen && this.wasPlaying_) {
  5496. player.pause();
  5497. }
  5498. this.on('keydown', this.handleKeyDown_);
  5499. // Hide controls and note if they were enabled.
  5500. this.hadControls_ = player.controls();
  5501. player.controls(false);
  5502. this.show();
  5503. this.conditionalFocus_();
  5504. this.el().setAttribute('aria-hidden', 'false');
  5505. /**
  5506. * Fired just after a `ModalDialog` is opened.
  5507. *
  5508. * @event ModalDialog#modalopen
  5509. * @type {Event}
  5510. */
  5511. this.trigger('modalopen');
  5512. this.hasBeenOpened_ = true;
  5513. }
  5514. }
  5515. /**
  5516. * If the `ModalDialog` is currently open or closed.
  5517. *
  5518. * @param {boolean} [value]
  5519. * If given, it will open (`true`) or close (`false`) the modal.
  5520. *
  5521. * @return {boolean}
  5522. * the current open state of the modaldialog
  5523. */
  5524. opened(value) {
  5525. if (typeof value === 'boolean') {
  5526. this[value ? 'open' : 'close']();
  5527. }
  5528. return this.opened_;
  5529. }
  5530. /**
  5531. * Closes the modal, does nothing if the `ModalDialog` is
  5532. * not open.
  5533. *
  5534. * @fires ModalDialog#beforemodalclose
  5535. * @fires ModalDialog#modalclose
  5536. */
  5537. close() {
  5538. if (!this.opened_) {
  5539. return;
  5540. }
  5541. const player = this.player();
  5542. /**
  5543. * Fired just before a `ModalDialog` is closed.
  5544. *
  5545. * @event ModalDialog#beforemodalclose
  5546. * @type {Event}
  5547. */
  5548. this.trigger('beforemodalclose');
  5549. this.opened_ = false;
  5550. if (this.wasPlaying_ && this.options_.pauseOnOpen) {
  5551. player.play();
  5552. }
  5553. this.off('keydown', this.handleKeyDown_);
  5554. if (this.hadControls_) {
  5555. player.controls(true);
  5556. }
  5557. this.hide();
  5558. this.el().setAttribute('aria-hidden', 'true');
  5559. /**
  5560. * Fired just after a `ModalDialog` is closed.
  5561. *
  5562. * @event ModalDialog#modalclose
  5563. * @type {Event}
  5564. */
  5565. this.trigger('modalclose');
  5566. this.conditionalBlur_();
  5567. if (this.options_.temporary) {
  5568. this.dispose();
  5569. }
  5570. }
  5571. /**
  5572. * Check to see if the `ModalDialog` is closeable via the UI.
  5573. *
  5574. * @param {boolean} [value]
  5575. * If given as a boolean, it will set the `closeable` option.
  5576. *
  5577. * @return {boolean}
  5578. * Returns the final value of the closable option.
  5579. */
  5580. closeable(value) {
  5581. if (typeof value === 'boolean') {
  5582. const closeable = this.closeable_ = !!value;
  5583. let close = this.getChild('closeButton');
  5584. // If this is being made closeable and has no close button, add one.
  5585. if (closeable && !close) {
  5586. // The close button should be a child of the modal - not its
  5587. // content element, so temporarily change the content element.
  5588. const temp = this.contentEl_;
  5589. this.contentEl_ = this.el_;
  5590. close = this.addChild('closeButton', {
  5591. controlText: 'Close Modal Dialog'
  5592. });
  5593. this.contentEl_ = temp;
  5594. this.on(close, 'close', this.close_);
  5595. }
  5596. // If this is being made uncloseable and has a close button, remove it.
  5597. if (!closeable && close) {
  5598. this.off(close, 'close', this.close_);
  5599. this.removeChild(close);
  5600. close.dispose();
  5601. }
  5602. }
  5603. return this.closeable_;
  5604. }
  5605. /**
  5606. * Fill the modal's content element with the modal's "content" option.
  5607. * The content element will be emptied before this change takes place.
  5608. */
  5609. fill() {
  5610. this.fillWith(this.content());
  5611. }
  5612. /**
  5613. * Fill the modal's content element with arbitrary content.
  5614. * The content element will be emptied before this change takes place.
  5615. *
  5616. * @fires ModalDialog#beforemodalfill
  5617. * @fires ModalDialog#modalfill
  5618. *
  5619. * @param { import('./utils/dom').ContentDescriptor} [content]
  5620. * The same rules apply to this as apply to the `content` option.
  5621. */
  5622. fillWith(content) {
  5623. const contentEl = this.contentEl();
  5624. const parentEl = contentEl.parentNode;
  5625. const nextSiblingEl = contentEl.nextSibling;
  5626. /**
  5627. * Fired just before a `ModalDialog` is filled with content.
  5628. *
  5629. * @event ModalDialog#beforemodalfill
  5630. * @type {Event}
  5631. */
  5632. this.trigger('beforemodalfill');
  5633. this.hasBeenFilled_ = true;
  5634. // Detach the content element from the DOM before performing
  5635. // manipulation to avoid modifying the live DOM multiple times.
  5636. parentEl.removeChild(contentEl);
  5637. this.empty();
  5638. insertContent(contentEl, content);
  5639. /**
  5640. * Fired just after a `ModalDialog` is filled with content.
  5641. *
  5642. * @event ModalDialog#modalfill
  5643. * @type {Event}
  5644. */
  5645. this.trigger('modalfill');
  5646. // Re-inject the re-filled content element.
  5647. if (nextSiblingEl) {
  5648. parentEl.insertBefore(contentEl, nextSiblingEl);
  5649. } else {
  5650. parentEl.appendChild(contentEl);
  5651. }
  5652. // make sure that the close button is last in the dialog DOM
  5653. const closeButton = this.getChild('closeButton');
  5654. if (closeButton) {
  5655. parentEl.appendChild(closeButton.el_);
  5656. }
  5657. }
  5658. /**
  5659. * Empties the content element. This happens anytime the modal is filled.
  5660. *
  5661. * @fires ModalDialog#beforemodalempty
  5662. * @fires ModalDialog#modalempty
  5663. */
  5664. empty() {
  5665. /**
  5666. * Fired just before a `ModalDialog` is emptied.
  5667. *
  5668. * @event ModalDialog#beforemodalempty
  5669. * @type {Event}
  5670. */
  5671. this.trigger('beforemodalempty');
  5672. emptyEl(this.contentEl());
  5673. /**
  5674. * Fired just after a `ModalDialog` is emptied.
  5675. *
  5676. * @event ModalDialog#modalempty
  5677. * @type {Event}
  5678. */
  5679. this.trigger('modalempty');
  5680. }
  5681. /**
  5682. * Gets or sets the modal content, which gets normalized before being
  5683. * rendered into the DOM.
  5684. *
  5685. * This does not update the DOM or fill the modal, but it is called during
  5686. * that process.
  5687. *
  5688. * @param { import('./utils/dom').ContentDescriptor} [value]
  5689. * If defined, sets the internal content value to be used on the
  5690. * next call(s) to `fill`. This value is normalized before being
  5691. * inserted. To "clear" the internal content value, pass `null`.
  5692. *
  5693. * @return { import('./utils/dom').ContentDescriptor}
  5694. * The current content of the modal dialog
  5695. */
  5696. content(value) {
  5697. if (typeof value !== 'undefined') {
  5698. this.content_ = value;
  5699. }
  5700. return this.content_;
  5701. }
  5702. /**
  5703. * conditionally focus the modal dialog if focus was previously on the player.
  5704. *
  5705. * @private
  5706. */
  5707. conditionalFocus_() {
  5708. const activeEl = document.activeElement;
  5709. const playerEl = this.player_.el_;
  5710. this.previouslyActiveEl_ = null;
  5711. if (playerEl.contains(activeEl) || playerEl === activeEl) {
  5712. this.previouslyActiveEl_ = activeEl;
  5713. this.focus();
  5714. }
  5715. }
  5716. /**
  5717. * conditionally blur the element and refocus the last focused element
  5718. *
  5719. * @private
  5720. */
  5721. conditionalBlur_() {
  5722. if (this.previouslyActiveEl_) {
  5723. this.previouslyActiveEl_.focus();
  5724. this.previouslyActiveEl_ = null;
  5725. }
  5726. }
  5727. /**
  5728. * Keydown handler. Attached when modal is focused.
  5729. *
  5730. * @listens keydown
  5731. */
  5732. handleKeyDown(event) {
  5733. // Do not allow keydowns to reach out of the modal dialog.
  5734. event.stopPropagation();
  5735. if (keycode.isEventKey(event, 'Escape') && this.closeable()) {
  5736. event.preventDefault();
  5737. this.close();
  5738. return;
  5739. }
  5740. // exit early if it isn't a tab key
  5741. if (!keycode.isEventKey(event, 'Tab')) {
  5742. return;
  5743. }
  5744. const focusableEls = this.focusableEls_();
  5745. const activeEl = this.el_.querySelector(':focus');
  5746. let focusIndex;
  5747. for (let i = 0; i < focusableEls.length; i++) {
  5748. if (activeEl === focusableEls[i]) {
  5749. focusIndex = i;
  5750. break;
  5751. }
  5752. }
  5753. if (document.activeElement === this.el_) {
  5754. focusIndex = 0;
  5755. }
  5756. if (event.shiftKey && focusIndex === 0) {
  5757. focusableEls[focusableEls.length - 1].focus();
  5758. event.preventDefault();
  5759. } else if (!event.shiftKey && focusIndex === focusableEls.length - 1) {
  5760. focusableEls[0].focus();
  5761. event.preventDefault();
  5762. }
  5763. }
  5764. /**
  5765. * get all focusable elements
  5766. *
  5767. * @private
  5768. */
  5769. focusableEls_() {
  5770. const allChildren = this.el_.querySelectorAll('*');
  5771. return Array.prototype.filter.call(allChildren, child => {
  5772. 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');
  5773. });
  5774. }
  5775. }
  5776. /**
  5777. * Default options for `ModalDialog` default options.
  5778. *
  5779. * @type {Object}
  5780. * @private
  5781. */
  5782. ModalDialog.prototype.options_ = {
  5783. pauseOnOpen: true,
  5784. temporary: true
  5785. };
  5786. Component.registerComponent('ModalDialog', ModalDialog);
  5787. /**
  5788. * @file track-list.js
  5789. */
  5790. /**
  5791. * Common functionaliy between {@link TextTrackList}, {@link AudioTrackList}, and
  5792. * {@link VideoTrackList}
  5793. *
  5794. * @extends EventTarget
  5795. */
  5796. class TrackList extends EventTarget {
  5797. /**
  5798. * Create an instance of this class
  5799. *
  5800. * @param { import('./track').default[] } tracks
  5801. * A list of tracks to initialize the list with.
  5802. *
  5803. * @abstract
  5804. */
  5805. constructor(tracks = []) {
  5806. super();
  5807. this.tracks_ = [];
  5808. /**
  5809. * @memberof TrackList
  5810. * @member {number} length
  5811. * The current number of `Track`s in the this Trackist.
  5812. * @instance
  5813. */
  5814. Object.defineProperty(this, 'length', {
  5815. get() {
  5816. return this.tracks_.length;
  5817. }
  5818. });
  5819. for (let i = 0; i < tracks.length; i++) {
  5820. this.addTrack(tracks[i]);
  5821. }
  5822. }
  5823. /**
  5824. * Add a {@link Track} to the `TrackList`
  5825. *
  5826. * @param { import('./track').default } track
  5827. * The audio, video, or text track to add to the list.
  5828. *
  5829. * @fires TrackList#addtrack
  5830. */
  5831. addTrack(track) {
  5832. const index = this.tracks_.length;
  5833. if (!('' + index in this)) {
  5834. Object.defineProperty(this, index, {
  5835. get() {
  5836. return this.tracks_[index];
  5837. }
  5838. });
  5839. }
  5840. // Do not add duplicate tracks
  5841. if (this.tracks_.indexOf(track) === -1) {
  5842. this.tracks_.push(track);
  5843. /**
  5844. * Triggered when a track is added to a track list.
  5845. *
  5846. * @event TrackList#addtrack
  5847. * @type {Event}
  5848. * @property {Track} track
  5849. * A reference to track that was added.
  5850. */
  5851. this.trigger({
  5852. track,
  5853. type: 'addtrack',
  5854. target: this
  5855. });
  5856. }
  5857. /**
  5858. * Triggered when a track label is changed.
  5859. *
  5860. * @event TrackList#addtrack
  5861. * @type {Event}
  5862. * @property {Track} track
  5863. * A reference to track that was added.
  5864. */
  5865. track.labelchange_ = () => {
  5866. this.trigger({
  5867. track,
  5868. type: 'labelchange',
  5869. target: this
  5870. });
  5871. };
  5872. if (isEvented(track)) {
  5873. track.addEventListener('labelchange', track.labelchange_);
  5874. }
  5875. }
  5876. /**
  5877. * Remove a {@link Track} from the `TrackList`
  5878. *
  5879. * @param { import('./track').default } rtrack
  5880. * The audio, video, or text track to remove from the list.
  5881. *
  5882. * @fires TrackList#removetrack
  5883. */
  5884. removeTrack(rtrack) {
  5885. let track;
  5886. for (let i = 0, l = this.length; i < l; i++) {
  5887. if (this[i] === rtrack) {
  5888. track = this[i];
  5889. if (track.off) {
  5890. track.off();
  5891. }
  5892. this.tracks_.splice(i, 1);
  5893. break;
  5894. }
  5895. }
  5896. if (!track) {
  5897. return;
  5898. }
  5899. /**
  5900. * Triggered when a track is removed from track list.
  5901. *
  5902. * @event TrackList#removetrack
  5903. * @type {Event}
  5904. * @property {Track} track
  5905. * A reference to track that was removed.
  5906. */
  5907. this.trigger({
  5908. track,
  5909. type: 'removetrack',
  5910. target: this
  5911. });
  5912. }
  5913. /**
  5914. * Get a Track from the TrackList by a tracks id
  5915. *
  5916. * @param {string} id - the id of the track to get
  5917. * @method getTrackById
  5918. * @return { import('./track').default }
  5919. * @private
  5920. */
  5921. getTrackById(id) {
  5922. let result = null;
  5923. for (let i = 0, l = this.length; i < l; i++) {
  5924. const track = this[i];
  5925. if (track.id === id) {
  5926. result = track;
  5927. break;
  5928. }
  5929. }
  5930. return result;
  5931. }
  5932. }
  5933. /**
  5934. * Triggered when a different track is selected/enabled.
  5935. *
  5936. * @event TrackList#change
  5937. * @type {Event}
  5938. */
  5939. /**
  5940. * Events that can be called with on + eventName. See {@link EventHandler}.
  5941. *
  5942. * @property {Object} TrackList#allowedEvents_
  5943. * @private
  5944. */
  5945. TrackList.prototype.allowedEvents_ = {
  5946. change: 'change',
  5947. addtrack: 'addtrack',
  5948. removetrack: 'removetrack',
  5949. labelchange: 'labelchange'
  5950. };
  5951. // emulate attribute EventHandler support to allow for feature detection
  5952. for (const event in TrackList.prototype.allowedEvents_) {
  5953. TrackList.prototype['on' + event] = null;
  5954. }
  5955. /**
  5956. * @file audio-track-list.js
  5957. */
  5958. /**
  5959. * Anywhere we call this function we diverge from the spec
  5960. * as we only support one enabled audiotrack at a time
  5961. *
  5962. * @param {AudioTrackList} list
  5963. * list to work on
  5964. *
  5965. * @param { import('./audio-track').default } track
  5966. * The track to skip
  5967. *
  5968. * @private
  5969. */
  5970. const disableOthers$1 = function (list, track) {
  5971. for (let i = 0; i < list.length; i++) {
  5972. if (!Object.keys(list[i]).length || track.id === list[i].id) {
  5973. continue;
  5974. }
  5975. // another audio track is enabled, disable it
  5976. list[i].enabled = false;
  5977. }
  5978. };
  5979. /**
  5980. * The current list of {@link AudioTrack} for a media file.
  5981. *
  5982. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotracklist}
  5983. * @extends TrackList
  5984. */
  5985. class AudioTrackList extends TrackList {
  5986. /**
  5987. * Create an instance of this class.
  5988. *
  5989. * @param {AudioTrack[]} [tracks=[]]
  5990. * A list of `AudioTrack` to instantiate the list with.
  5991. */
  5992. constructor(tracks = []) {
  5993. // make sure only 1 track is enabled
  5994. // sorted from last index to first index
  5995. for (let i = tracks.length - 1; i >= 0; i--) {
  5996. if (tracks[i].enabled) {
  5997. disableOthers$1(tracks, tracks[i]);
  5998. break;
  5999. }
  6000. }
  6001. super(tracks);
  6002. this.changing_ = false;
  6003. }
  6004. /**
  6005. * Add an {@link AudioTrack} to the `AudioTrackList`.
  6006. *
  6007. * @param { import('./audio-track').default } track
  6008. * The AudioTrack to add to the list
  6009. *
  6010. * @fires TrackList#addtrack
  6011. */
  6012. addTrack(track) {
  6013. if (track.enabled) {
  6014. disableOthers$1(this, track);
  6015. }
  6016. super.addTrack(track);
  6017. // native tracks don't have this
  6018. if (!track.addEventListener) {
  6019. return;
  6020. }
  6021. track.enabledChange_ = () => {
  6022. // when we are disabling other tracks (since we don't support
  6023. // more than one track at a time) we will set changing_
  6024. // to true so that we don't trigger additional change events
  6025. if (this.changing_) {
  6026. return;
  6027. }
  6028. this.changing_ = true;
  6029. disableOthers$1(this, track);
  6030. this.changing_ = false;
  6031. this.trigger('change');
  6032. };
  6033. /**
  6034. * @listens AudioTrack#enabledchange
  6035. * @fires TrackList#change
  6036. */
  6037. track.addEventListener('enabledchange', track.enabledChange_);
  6038. }
  6039. removeTrack(rtrack) {
  6040. super.removeTrack(rtrack);
  6041. if (rtrack.removeEventListener && rtrack.enabledChange_) {
  6042. rtrack.removeEventListener('enabledchange', rtrack.enabledChange_);
  6043. rtrack.enabledChange_ = null;
  6044. }
  6045. }
  6046. }
  6047. /**
  6048. * @file video-track-list.js
  6049. */
  6050. /**
  6051. * Un-select all other {@link VideoTrack}s that are selected.
  6052. *
  6053. * @param {VideoTrackList} list
  6054. * list to work on
  6055. *
  6056. * @param { import('./video-track').default } track
  6057. * The track to skip
  6058. *
  6059. * @private
  6060. */
  6061. const disableOthers = function (list, track) {
  6062. for (let i = 0; i < list.length; i++) {
  6063. if (!Object.keys(list[i]).length || track.id === list[i].id) {
  6064. continue;
  6065. }
  6066. // another video track is enabled, disable it
  6067. list[i].selected = false;
  6068. }
  6069. };
  6070. /**
  6071. * The current list of {@link VideoTrack} for a video.
  6072. *
  6073. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#videotracklist}
  6074. * @extends TrackList
  6075. */
  6076. class VideoTrackList extends TrackList {
  6077. /**
  6078. * Create an instance of this class.
  6079. *
  6080. * @param {VideoTrack[]} [tracks=[]]
  6081. * A list of `VideoTrack` to instantiate the list with.
  6082. */
  6083. constructor(tracks = []) {
  6084. // make sure only 1 track is enabled
  6085. // sorted from last index to first index
  6086. for (let i = tracks.length - 1; i >= 0; i--) {
  6087. if (tracks[i].selected) {
  6088. disableOthers(tracks, tracks[i]);
  6089. break;
  6090. }
  6091. }
  6092. super(tracks);
  6093. this.changing_ = false;
  6094. /**
  6095. * @member {number} VideoTrackList#selectedIndex
  6096. * The current index of the selected {@link VideoTrack`}.
  6097. */
  6098. Object.defineProperty(this, 'selectedIndex', {
  6099. get() {
  6100. for (let i = 0; i < this.length; i++) {
  6101. if (this[i].selected) {
  6102. return i;
  6103. }
  6104. }
  6105. return -1;
  6106. },
  6107. set() {}
  6108. });
  6109. }
  6110. /**
  6111. * Add a {@link VideoTrack} to the `VideoTrackList`.
  6112. *
  6113. * @param { import('./video-track').default } track
  6114. * The VideoTrack to add to the list
  6115. *
  6116. * @fires TrackList#addtrack
  6117. */
  6118. addTrack(track) {
  6119. if (track.selected) {
  6120. disableOthers(this, track);
  6121. }
  6122. super.addTrack(track);
  6123. // native tracks don't have this
  6124. if (!track.addEventListener) {
  6125. return;
  6126. }
  6127. track.selectedChange_ = () => {
  6128. if (this.changing_) {
  6129. return;
  6130. }
  6131. this.changing_ = true;
  6132. disableOthers(this, track);
  6133. this.changing_ = false;
  6134. this.trigger('change');
  6135. };
  6136. /**
  6137. * @listens VideoTrack#selectedchange
  6138. * @fires TrackList#change
  6139. */
  6140. track.addEventListener('selectedchange', track.selectedChange_);
  6141. }
  6142. removeTrack(rtrack) {
  6143. super.removeTrack(rtrack);
  6144. if (rtrack.removeEventListener && rtrack.selectedChange_) {
  6145. rtrack.removeEventListener('selectedchange', rtrack.selectedChange_);
  6146. rtrack.selectedChange_ = null;
  6147. }
  6148. }
  6149. }
  6150. /**
  6151. * @file text-track-list.js
  6152. */
  6153. /**
  6154. * The current list of {@link TextTrack} for a media file.
  6155. *
  6156. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttracklist}
  6157. * @extends TrackList
  6158. */
  6159. class TextTrackList extends TrackList {
  6160. /**
  6161. * Add a {@link TextTrack} to the `TextTrackList`
  6162. *
  6163. * @param { import('./text-track').default } track
  6164. * The text track to add to the list.
  6165. *
  6166. * @fires TrackList#addtrack
  6167. */
  6168. addTrack(track) {
  6169. super.addTrack(track);
  6170. if (!this.queueChange_) {
  6171. this.queueChange_ = () => this.queueTrigger('change');
  6172. }
  6173. if (!this.triggerSelectedlanguagechange) {
  6174. this.triggerSelectedlanguagechange_ = () => this.trigger('selectedlanguagechange');
  6175. }
  6176. /**
  6177. * @listens TextTrack#modechange
  6178. * @fires TrackList#change
  6179. */
  6180. track.addEventListener('modechange', this.queueChange_);
  6181. const nonLanguageTextTrackKind = ['metadata', 'chapters'];
  6182. if (nonLanguageTextTrackKind.indexOf(track.kind) === -1) {
  6183. track.addEventListener('modechange', this.triggerSelectedlanguagechange_);
  6184. }
  6185. }
  6186. removeTrack(rtrack) {
  6187. super.removeTrack(rtrack);
  6188. // manually remove the event handlers we added
  6189. if (rtrack.removeEventListener) {
  6190. if (this.queueChange_) {
  6191. rtrack.removeEventListener('modechange', this.queueChange_);
  6192. }
  6193. if (this.selectedlanguagechange_) {
  6194. rtrack.removeEventListener('modechange', this.triggerSelectedlanguagechange_);
  6195. }
  6196. }
  6197. }
  6198. }
  6199. /**
  6200. * @file html-track-element-list.js
  6201. */
  6202. /**
  6203. * The current list of {@link HtmlTrackElement}s.
  6204. */
  6205. class HtmlTrackElementList {
  6206. /**
  6207. * Create an instance of this class.
  6208. *
  6209. * @param {HtmlTrackElement[]} [tracks=[]]
  6210. * A list of `HtmlTrackElement` to instantiate the list with.
  6211. */
  6212. constructor(trackElements = []) {
  6213. this.trackElements_ = [];
  6214. /**
  6215. * @memberof HtmlTrackElementList
  6216. * @member {number} length
  6217. * The current number of `Track`s in the this Trackist.
  6218. * @instance
  6219. */
  6220. Object.defineProperty(this, 'length', {
  6221. get() {
  6222. return this.trackElements_.length;
  6223. }
  6224. });
  6225. for (let i = 0, length = trackElements.length; i < length; i++) {
  6226. this.addTrackElement_(trackElements[i]);
  6227. }
  6228. }
  6229. /**
  6230. * Add an {@link HtmlTrackElement} to the `HtmlTrackElementList`
  6231. *
  6232. * @param {HtmlTrackElement} trackElement
  6233. * The track element to add to the list.
  6234. *
  6235. * @private
  6236. */
  6237. addTrackElement_(trackElement) {
  6238. const index = this.trackElements_.length;
  6239. if (!('' + index in this)) {
  6240. Object.defineProperty(this, index, {
  6241. get() {
  6242. return this.trackElements_[index];
  6243. }
  6244. });
  6245. }
  6246. // Do not add duplicate elements
  6247. if (this.trackElements_.indexOf(trackElement) === -1) {
  6248. this.trackElements_.push(trackElement);
  6249. }
  6250. }
  6251. /**
  6252. * Get an {@link HtmlTrackElement} from the `HtmlTrackElementList` given an
  6253. * {@link TextTrack}.
  6254. *
  6255. * @param {TextTrack} track
  6256. * The track associated with a track element.
  6257. *
  6258. * @return {HtmlTrackElement|undefined}
  6259. * The track element that was found or undefined.
  6260. *
  6261. * @private
  6262. */
  6263. getTrackElementByTrack_(track) {
  6264. let trackElement_;
  6265. for (let i = 0, length = this.trackElements_.length; i < length; i++) {
  6266. if (track === this.trackElements_[i].track) {
  6267. trackElement_ = this.trackElements_[i];
  6268. break;
  6269. }
  6270. }
  6271. return trackElement_;
  6272. }
  6273. /**
  6274. * Remove a {@link HtmlTrackElement} from the `HtmlTrackElementList`
  6275. *
  6276. * @param {HtmlTrackElement} trackElement
  6277. * The track element to remove from the list.
  6278. *
  6279. * @private
  6280. */
  6281. removeTrackElement_(trackElement) {
  6282. for (let i = 0, length = this.trackElements_.length; i < length; i++) {
  6283. if (trackElement === this.trackElements_[i]) {
  6284. if (this.trackElements_[i].track && typeof this.trackElements_[i].track.off === 'function') {
  6285. this.trackElements_[i].track.off();
  6286. }
  6287. if (typeof this.trackElements_[i].off === 'function') {
  6288. this.trackElements_[i].off();
  6289. }
  6290. this.trackElements_.splice(i, 1);
  6291. break;
  6292. }
  6293. }
  6294. }
  6295. }
  6296. /**
  6297. * @file text-track-cue-list.js
  6298. */
  6299. /**
  6300. * @typedef {Object} TextTrackCueList~TextTrackCue
  6301. *
  6302. * @property {string} id
  6303. * The unique id for this text track cue
  6304. *
  6305. * @property {number} startTime
  6306. * The start time for this text track cue
  6307. *
  6308. * @property {number} endTime
  6309. * The end time for this text track cue
  6310. *
  6311. * @property {boolean} pauseOnExit
  6312. * Pause when the end time is reached if true.
  6313. *
  6314. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackcue}
  6315. */
  6316. /**
  6317. * A List of TextTrackCues.
  6318. *
  6319. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackcuelist}
  6320. */
  6321. class TextTrackCueList {
  6322. /**
  6323. * Create an instance of this class..
  6324. *
  6325. * @param {Array} cues
  6326. * A list of cues to be initialized with
  6327. */
  6328. constructor(cues) {
  6329. TextTrackCueList.prototype.setCues_.call(this, cues);
  6330. /**
  6331. * @memberof TextTrackCueList
  6332. * @member {number} length
  6333. * The current number of `TextTrackCue`s in the TextTrackCueList.
  6334. * @instance
  6335. */
  6336. Object.defineProperty(this, 'length', {
  6337. get() {
  6338. return this.length_;
  6339. }
  6340. });
  6341. }
  6342. /**
  6343. * A setter for cues in this list. Creates getters
  6344. * an an index for the cues.
  6345. *
  6346. * @param {Array} cues
  6347. * An array of cues to set
  6348. *
  6349. * @private
  6350. */
  6351. setCues_(cues) {
  6352. const oldLength = this.length || 0;
  6353. let i = 0;
  6354. const l = cues.length;
  6355. this.cues_ = cues;
  6356. this.length_ = cues.length;
  6357. const defineProp = function (index) {
  6358. if (!('' + index in this)) {
  6359. Object.defineProperty(this, '' + index, {
  6360. get() {
  6361. return this.cues_[index];
  6362. }
  6363. });
  6364. }
  6365. };
  6366. if (oldLength < l) {
  6367. i = oldLength;
  6368. for (; i < l; i++) {
  6369. defineProp.call(this, i);
  6370. }
  6371. }
  6372. }
  6373. /**
  6374. * Get a `TextTrackCue` that is currently in the `TextTrackCueList` by id.
  6375. *
  6376. * @param {string} id
  6377. * The id of the cue that should be searched for.
  6378. *
  6379. * @return {TextTrackCueList~TextTrackCue|null}
  6380. * A single cue or null if none was found.
  6381. */
  6382. getCueById(id) {
  6383. let result = null;
  6384. for (let i = 0, l = this.length; i < l; i++) {
  6385. const cue = this[i];
  6386. if (cue.id === id) {
  6387. result = cue;
  6388. break;
  6389. }
  6390. }
  6391. return result;
  6392. }
  6393. }
  6394. /**
  6395. * @file track-kinds.js
  6396. */
  6397. /**
  6398. * All possible `VideoTrackKind`s
  6399. *
  6400. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-videotrack-kind
  6401. * @typedef VideoTrack~Kind
  6402. * @enum
  6403. */
  6404. const VideoTrackKind = {
  6405. alternative: 'alternative',
  6406. captions: 'captions',
  6407. main: 'main',
  6408. sign: 'sign',
  6409. subtitles: 'subtitles',
  6410. commentary: 'commentary'
  6411. };
  6412. /**
  6413. * All possible `AudioTrackKind`s
  6414. *
  6415. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-audiotrack-kind
  6416. * @typedef AudioTrack~Kind
  6417. * @enum
  6418. */
  6419. const AudioTrackKind = {
  6420. 'alternative': 'alternative',
  6421. 'descriptions': 'descriptions',
  6422. 'main': 'main',
  6423. 'main-desc': 'main-desc',
  6424. 'translation': 'translation',
  6425. 'commentary': 'commentary'
  6426. };
  6427. /**
  6428. * All possible `TextTrackKind`s
  6429. *
  6430. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-texttrack-kind
  6431. * @typedef TextTrack~Kind
  6432. * @enum
  6433. */
  6434. const TextTrackKind = {
  6435. subtitles: 'subtitles',
  6436. captions: 'captions',
  6437. descriptions: 'descriptions',
  6438. chapters: 'chapters',
  6439. metadata: 'metadata'
  6440. };
  6441. /**
  6442. * All possible `TextTrackMode`s
  6443. *
  6444. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackmode
  6445. * @typedef TextTrack~Mode
  6446. * @enum
  6447. */
  6448. const TextTrackMode = {
  6449. disabled: 'disabled',
  6450. hidden: 'hidden',
  6451. showing: 'showing'
  6452. };
  6453. /**
  6454. * @file track.js
  6455. */
  6456. /**
  6457. * A Track class that contains all of the common functionality for {@link AudioTrack},
  6458. * {@link VideoTrack}, and {@link TextTrack}.
  6459. *
  6460. * > Note: This class should not be used directly
  6461. *
  6462. * @see {@link https://html.spec.whatwg.org/multipage/embedded-content.html}
  6463. * @extends EventTarget
  6464. * @abstract
  6465. */
  6466. class Track extends EventTarget {
  6467. /**
  6468. * Create an instance of this class.
  6469. *
  6470. * @param {Object} [options={}]
  6471. * Object of option names and values
  6472. *
  6473. * @param {string} [options.kind='']
  6474. * A valid kind for the track type you are creating.
  6475. *
  6476. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  6477. * A unique id for this AudioTrack.
  6478. *
  6479. * @param {string} [options.label='']
  6480. * The menu label for this track.
  6481. *
  6482. * @param {string} [options.language='']
  6483. * A valid two character language code.
  6484. *
  6485. * @abstract
  6486. */
  6487. constructor(options = {}) {
  6488. super();
  6489. const trackProps = {
  6490. id: options.id || 'vjs_track_' + newGUID(),
  6491. kind: options.kind || '',
  6492. language: options.language || ''
  6493. };
  6494. let label = options.label || '';
  6495. /**
  6496. * @memberof Track
  6497. * @member {string} id
  6498. * The id of this track. Cannot be changed after creation.
  6499. * @instance
  6500. *
  6501. * @readonly
  6502. */
  6503. /**
  6504. * @memberof Track
  6505. * @member {string} kind
  6506. * The kind of track that this is. Cannot be changed after creation.
  6507. * @instance
  6508. *
  6509. * @readonly
  6510. */
  6511. /**
  6512. * @memberof Track
  6513. * @member {string} language
  6514. * The two letter language code for this track. Cannot be changed after
  6515. * creation.
  6516. * @instance
  6517. *
  6518. * @readonly
  6519. */
  6520. for (const key in trackProps) {
  6521. Object.defineProperty(this, key, {
  6522. get() {
  6523. return trackProps[key];
  6524. },
  6525. set() {}
  6526. });
  6527. }
  6528. /**
  6529. * @memberof Track
  6530. * @member {string} label
  6531. * The label of this track. Cannot be changed after creation.
  6532. * @instance
  6533. *
  6534. * @fires Track#labelchange
  6535. */
  6536. Object.defineProperty(this, 'label', {
  6537. get() {
  6538. return label;
  6539. },
  6540. set(newLabel) {
  6541. if (newLabel !== label) {
  6542. label = newLabel;
  6543. /**
  6544. * An event that fires when label changes on this track.
  6545. *
  6546. * > Note: This is not part of the spec!
  6547. *
  6548. * @event Track#labelchange
  6549. * @type {Event}
  6550. */
  6551. this.trigger('labelchange');
  6552. }
  6553. }
  6554. });
  6555. }
  6556. }
  6557. /**
  6558. * @file url.js
  6559. * @module url
  6560. */
  6561. /**
  6562. * @typedef {Object} url:URLObject
  6563. *
  6564. * @property {string} protocol
  6565. * The protocol of the url that was parsed.
  6566. *
  6567. * @property {string} hostname
  6568. * The hostname of the url that was parsed.
  6569. *
  6570. * @property {string} port
  6571. * The port of the url that was parsed.
  6572. *
  6573. * @property {string} pathname
  6574. * The pathname of the url that was parsed.
  6575. *
  6576. * @property {string} search
  6577. * The search query of the url that was parsed.
  6578. *
  6579. * @property {string} hash
  6580. * The hash of the url that was parsed.
  6581. *
  6582. * @property {string} host
  6583. * The host of the url that was parsed.
  6584. */
  6585. /**
  6586. * Resolve and parse the elements of a URL.
  6587. *
  6588. * @function
  6589. * @param {String} url
  6590. * The url to parse
  6591. *
  6592. * @return {url:URLObject}
  6593. * An object of url details
  6594. */
  6595. const parseUrl = function (url) {
  6596. // This entire method can be replace with URL once we are able to drop IE11
  6597. const props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
  6598. // add the url to an anchor and let the browser parse the URL
  6599. const a = document.createElement('a');
  6600. a.href = url;
  6601. // Copy the specific URL properties to a new object
  6602. // This is also needed for IE because the anchor loses its
  6603. // properties when it's removed from the dom
  6604. const details = {};
  6605. for (let i = 0; i < props.length; i++) {
  6606. details[props[i]] = a[props[i]];
  6607. }
  6608. // IE adds the port to the host property unlike everyone else. If
  6609. // a port identifier is added for standard ports, strip it.
  6610. if (details.protocol === 'http:') {
  6611. details.host = details.host.replace(/:80$/, '');
  6612. }
  6613. if (details.protocol === 'https:') {
  6614. details.host = details.host.replace(/:443$/, '');
  6615. }
  6616. if (!details.protocol) {
  6617. details.protocol = window.location.protocol;
  6618. }
  6619. /* istanbul ignore if */
  6620. if (!details.host) {
  6621. details.host = window.location.host;
  6622. }
  6623. return details;
  6624. };
  6625. /**
  6626. * Get absolute version of relative URL.
  6627. *
  6628. * @function
  6629. * @param {string} url
  6630. * URL to make absolute
  6631. *
  6632. * @return {string}
  6633. * Absolute URL
  6634. *
  6635. * @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
  6636. */
  6637. const getAbsoluteURL = function (url) {
  6638. // Check if absolute URL
  6639. if (!url.match(/^https?:\/\//)) {
  6640. // Add the url to an anchor and let the browser parse it to convert to an absolute url
  6641. const a = document.createElement('a');
  6642. a.href = url;
  6643. url = a.href;
  6644. }
  6645. return url;
  6646. };
  6647. /**
  6648. * Returns the extension of the passed file name. It will return an empty string
  6649. * if passed an invalid path.
  6650. *
  6651. * @function
  6652. * @param {string} path
  6653. * The fileName path like '/path/to/file.mp4'
  6654. *
  6655. * @return {string}
  6656. * The extension in lower case or an empty string if no
  6657. * extension could be found.
  6658. */
  6659. const getFileExtension = function (path) {
  6660. if (typeof path === 'string') {
  6661. const splitPathRe = /^(\/?)([\s\S]*?)((?:\.{1,2}|[^\/]+?)(\.([^\.\/\?]+)))(?:[\/]*|[\?].*)$/;
  6662. const pathParts = splitPathRe.exec(path);
  6663. if (pathParts) {
  6664. return pathParts.pop().toLowerCase();
  6665. }
  6666. }
  6667. return '';
  6668. };
  6669. /**
  6670. * Returns whether the url passed is a cross domain request or not.
  6671. *
  6672. * @function
  6673. * @param {string} url
  6674. * The url to check.
  6675. *
  6676. * @param {Object} [winLoc]
  6677. * the domain to check the url against, defaults to window.location
  6678. *
  6679. * @param {string} [winLoc.protocol]
  6680. * The window location protocol defaults to window.location.protocol
  6681. *
  6682. * @param {string} [winLoc.host]
  6683. * The window location host defaults to window.location.host
  6684. *
  6685. * @return {boolean}
  6686. * Whether it is a cross domain request or not.
  6687. */
  6688. const isCrossOrigin = function (url, winLoc = window.location) {
  6689. const urlInfo = parseUrl(url);
  6690. // IE8 protocol relative urls will return ':' for protocol
  6691. const srcProtocol = urlInfo.protocol === ':' ? winLoc.protocol : urlInfo.protocol;
  6692. // Check if url is for another domain/origin
  6693. // IE8 doesn't know location.origin, so we won't rely on it here
  6694. const crossOrigin = srcProtocol + urlInfo.host !== winLoc.protocol + winLoc.host;
  6695. return crossOrigin;
  6696. };
  6697. var Url = /*#__PURE__*/Object.freeze({
  6698. __proto__: null,
  6699. parseUrl: parseUrl,
  6700. getAbsoluteURL: getAbsoluteURL,
  6701. getFileExtension: getFileExtension,
  6702. isCrossOrigin: isCrossOrigin
  6703. });
  6704. var win;
  6705. if (typeof window !== "undefined") {
  6706. win = window;
  6707. } else if (typeof commonjsGlobal !== "undefined") {
  6708. win = commonjsGlobal;
  6709. } else if (typeof self !== "undefined") {
  6710. win = self;
  6711. } else {
  6712. win = {};
  6713. }
  6714. var window_1 = win;
  6715. var _extends_1 = createCommonjsModule(function (module) {
  6716. function _extends() {
  6717. module.exports = _extends = Object.assign ? Object.assign.bind() : function (target) {
  6718. for (var i = 1; i < arguments.length; i++) {
  6719. var source = arguments[i];
  6720. for (var key in source) {
  6721. if (Object.prototype.hasOwnProperty.call(source, key)) {
  6722. target[key] = source[key];
  6723. }
  6724. }
  6725. }
  6726. return target;
  6727. }, module.exports.__esModule = true, module.exports["default"] = module.exports;
  6728. return _extends.apply(this, arguments);
  6729. }
  6730. module.exports = _extends, module.exports.__esModule = true, module.exports["default"] = module.exports;
  6731. });
  6732. unwrapExports(_extends_1);
  6733. var isFunction_1 = isFunction;
  6734. var toString = Object.prototype.toString;
  6735. function isFunction(fn) {
  6736. if (!fn) {
  6737. return false;
  6738. }
  6739. var string = toString.call(fn);
  6740. return string === '[object Function]' || typeof fn === 'function' && string !== '[object RegExp]' || typeof window !== 'undefined' && (
  6741. // IE8 and below
  6742. fn === window.setTimeout || fn === window.alert || fn === window.confirm || fn === window.prompt);
  6743. }
  6744. var httpResponseHandler = function httpResponseHandler(callback, decodeResponseBody) {
  6745. if (decodeResponseBody === void 0) {
  6746. decodeResponseBody = false;
  6747. }
  6748. return function (err, response, responseBody) {
  6749. // if the XHR failed, return that error
  6750. if (err) {
  6751. callback(err);
  6752. return;
  6753. } // if the HTTP status code is 4xx or 5xx, the request also failed
  6754. if (response.statusCode >= 400 && response.statusCode <= 599) {
  6755. var cause = responseBody;
  6756. if (decodeResponseBody) {
  6757. if (window_1.TextDecoder) {
  6758. var charset = getCharset(response.headers && response.headers['content-type']);
  6759. try {
  6760. cause = new TextDecoder(charset).decode(responseBody);
  6761. } catch (e) {}
  6762. } else {
  6763. cause = String.fromCharCode.apply(null, new Uint8Array(responseBody));
  6764. }
  6765. }
  6766. callback({
  6767. cause: cause
  6768. });
  6769. return;
  6770. } // otherwise, request succeeded
  6771. callback(null, responseBody);
  6772. };
  6773. };
  6774. function getCharset(contentTypeHeader) {
  6775. if (contentTypeHeader === void 0) {
  6776. contentTypeHeader = '';
  6777. }
  6778. return contentTypeHeader.toLowerCase().split(';').reduce(function (charset, contentType) {
  6779. var _contentType$split = contentType.split('='),
  6780. type = _contentType$split[0],
  6781. value = _contentType$split[1];
  6782. if (type.trim() === 'charset') {
  6783. return value.trim();
  6784. }
  6785. return charset;
  6786. }, 'utf-8');
  6787. }
  6788. var httpHandler = httpResponseHandler;
  6789. createXHR.httpHandler = httpHandler;
  6790. /**
  6791. * @license
  6792. * slighly modified parse-headers 2.0.2 <https://github.com/kesla/parse-headers/>
  6793. * Copyright (c) 2014 David Björklund
  6794. * Available under the MIT license
  6795. * <https://github.com/kesla/parse-headers/blob/master/LICENCE>
  6796. */
  6797. var parseHeaders = function parseHeaders(headers) {
  6798. var result = {};
  6799. if (!headers) {
  6800. return result;
  6801. }
  6802. headers.trim().split('\n').forEach(function (row) {
  6803. var index = row.indexOf(':');
  6804. var key = row.slice(0, index).trim().toLowerCase();
  6805. var value = row.slice(index + 1).trim();
  6806. if (typeof result[key] === 'undefined') {
  6807. result[key] = value;
  6808. } else if (Array.isArray(result[key])) {
  6809. result[key].push(value);
  6810. } else {
  6811. result[key] = [result[key], value];
  6812. }
  6813. });
  6814. return result;
  6815. };
  6816. var lib = createXHR; // Allow use of default import syntax in TypeScript
  6817. var default_1 = createXHR;
  6818. createXHR.XMLHttpRequest = window_1.XMLHttpRequest || noop;
  6819. createXHR.XDomainRequest = "withCredentials" in new createXHR.XMLHttpRequest() ? createXHR.XMLHttpRequest : window_1.XDomainRequest;
  6820. forEachArray(["get", "put", "post", "patch", "head", "delete"], function (method) {
  6821. createXHR[method === "delete" ? "del" : method] = function (uri, options, callback) {
  6822. options = initParams(uri, options, callback);
  6823. options.method = method.toUpperCase();
  6824. return _createXHR(options);
  6825. };
  6826. });
  6827. function forEachArray(array, iterator) {
  6828. for (var i = 0; i < array.length; i++) {
  6829. iterator(array[i]);
  6830. }
  6831. }
  6832. function isEmpty(obj) {
  6833. for (var i in obj) {
  6834. if (obj.hasOwnProperty(i)) return false;
  6835. }
  6836. return true;
  6837. }
  6838. function initParams(uri, options, callback) {
  6839. var params = uri;
  6840. if (isFunction_1(options)) {
  6841. callback = options;
  6842. if (typeof uri === "string") {
  6843. params = {
  6844. uri: uri
  6845. };
  6846. }
  6847. } else {
  6848. params = _extends_1({}, options, {
  6849. uri: uri
  6850. });
  6851. }
  6852. params.callback = callback;
  6853. return params;
  6854. }
  6855. function createXHR(uri, options, callback) {
  6856. options = initParams(uri, options, callback);
  6857. return _createXHR(options);
  6858. }
  6859. function _createXHR(options) {
  6860. if (typeof options.callback === "undefined") {
  6861. throw new Error("callback argument missing");
  6862. }
  6863. var called = false;
  6864. var callback = function cbOnce(err, response, body) {
  6865. if (!called) {
  6866. called = true;
  6867. options.callback(err, response, body);
  6868. }
  6869. };
  6870. function readystatechange() {
  6871. if (xhr.readyState === 4) {
  6872. setTimeout(loadFunc, 0);
  6873. }
  6874. }
  6875. function getBody() {
  6876. // Chrome with requestType=blob throws errors arround when even testing access to responseText
  6877. var body = undefined;
  6878. if (xhr.response) {
  6879. body = xhr.response;
  6880. } else {
  6881. body = xhr.responseText || getXml(xhr);
  6882. }
  6883. if (isJson) {
  6884. try {
  6885. body = JSON.parse(body);
  6886. } catch (e) {}
  6887. }
  6888. return body;
  6889. }
  6890. function errorFunc(evt) {
  6891. clearTimeout(timeoutTimer);
  6892. if (!(evt instanceof Error)) {
  6893. evt = new Error("" + (evt || "Unknown XMLHttpRequest Error"));
  6894. }
  6895. evt.statusCode = 0;
  6896. return callback(evt, failureResponse);
  6897. } // will load the data & process the response in a special response object
  6898. function loadFunc() {
  6899. if (aborted) return;
  6900. var status;
  6901. clearTimeout(timeoutTimer);
  6902. if (options.useXDR && xhr.status === undefined) {
  6903. //IE8 CORS GET successful response doesn't have a status field, but body is fine
  6904. status = 200;
  6905. } else {
  6906. status = xhr.status === 1223 ? 204 : xhr.status;
  6907. }
  6908. var response = failureResponse;
  6909. var err = null;
  6910. if (status !== 0) {
  6911. response = {
  6912. body: getBody(),
  6913. statusCode: status,
  6914. method: method,
  6915. headers: {},
  6916. url: uri,
  6917. rawRequest: xhr
  6918. };
  6919. if (xhr.getAllResponseHeaders) {
  6920. //remember xhr can in fact be XDR for CORS in IE
  6921. response.headers = parseHeaders(xhr.getAllResponseHeaders());
  6922. }
  6923. } else {
  6924. err = new Error("Internal XMLHttpRequest Error");
  6925. }
  6926. return callback(err, response, response.body);
  6927. }
  6928. var xhr = options.xhr || null;
  6929. if (!xhr) {
  6930. if (options.cors || options.useXDR) {
  6931. xhr = new createXHR.XDomainRequest();
  6932. } else {
  6933. xhr = new createXHR.XMLHttpRequest();
  6934. }
  6935. }
  6936. var key;
  6937. var aborted;
  6938. var uri = xhr.url = options.uri || options.url;
  6939. var method = xhr.method = options.method || "GET";
  6940. var body = options.body || options.data;
  6941. var headers = xhr.headers = options.headers || {};
  6942. var sync = !!options.sync;
  6943. var isJson = false;
  6944. var timeoutTimer;
  6945. var failureResponse = {
  6946. body: undefined,
  6947. headers: {},
  6948. statusCode: 0,
  6949. method: method,
  6950. url: uri,
  6951. rawRequest: xhr
  6952. };
  6953. if ("json" in options && options.json !== false) {
  6954. isJson = true;
  6955. headers["accept"] || headers["Accept"] || (headers["Accept"] = "application/json"); //Don't override existing accept header declared by user
  6956. if (method !== "GET" && method !== "HEAD") {
  6957. headers["content-type"] || headers["Content-Type"] || (headers["Content-Type"] = "application/json"); //Don't override existing accept header declared by user
  6958. body = JSON.stringify(options.json === true ? body : options.json);
  6959. }
  6960. }
  6961. xhr.onreadystatechange = readystatechange;
  6962. xhr.onload = loadFunc;
  6963. xhr.onerror = errorFunc; // IE9 must have onprogress be set to a unique function.
  6964. xhr.onprogress = function () {// IE must die
  6965. };
  6966. xhr.onabort = function () {
  6967. aborted = true;
  6968. };
  6969. xhr.ontimeout = errorFunc;
  6970. xhr.open(method, uri, !sync, options.username, options.password); //has to be after open
  6971. if (!sync) {
  6972. xhr.withCredentials = !!options.withCredentials;
  6973. } // Cannot set timeout with sync request
  6974. // not setting timeout on the xhr object, because of old webkits etc. not handling that correctly
  6975. // both npm's request and jquery 1.x use this kind of timeout, so this is being consistent
  6976. if (!sync && options.timeout > 0) {
  6977. timeoutTimer = setTimeout(function () {
  6978. if (aborted) return;
  6979. aborted = true; //IE9 may still call readystatechange
  6980. xhr.abort("timeout");
  6981. var e = new Error("XMLHttpRequest timeout");
  6982. e.code = "ETIMEDOUT";
  6983. errorFunc(e);
  6984. }, options.timeout);
  6985. }
  6986. if (xhr.setRequestHeader) {
  6987. for (key in headers) {
  6988. if (headers.hasOwnProperty(key)) {
  6989. xhr.setRequestHeader(key, headers[key]);
  6990. }
  6991. }
  6992. } else if (options.headers && !isEmpty(options.headers)) {
  6993. throw new Error("Headers cannot be set on an XDomainRequest object");
  6994. }
  6995. if ("responseType" in options) {
  6996. xhr.responseType = options.responseType;
  6997. }
  6998. if ("beforeSend" in options && typeof options.beforeSend === "function") {
  6999. options.beforeSend(xhr);
  7000. } // Microsoft Edge browser sends "undefined" when send is called with undefined value.
  7001. // XMLHttpRequest spec says to pass null as body to indicate no body
  7002. // See https://github.com/naugtur/xhr/issues/100.
  7003. xhr.send(body || null);
  7004. return xhr;
  7005. }
  7006. function getXml(xhr) {
  7007. // xhr.responseXML will throw Exception "InvalidStateError" or "DOMException"
  7008. // See https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseXML.
  7009. try {
  7010. if (xhr.responseType === "document") {
  7011. return xhr.responseXML;
  7012. }
  7013. var firefoxBugTakenEffect = xhr.responseXML && xhr.responseXML.documentElement.nodeName === "parsererror";
  7014. if (xhr.responseType === "" && !firefoxBugTakenEffect) {
  7015. return xhr.responseXML;
  7016. }
  7017. } catch (e) {}
  7018. return null;
  7019. }
  7020. function noop() {}
  7021. lib.default = default_1;
  7022. /**
  7023. * @file text-track.js
  7024. */
  7025. /**
  7026. * Takes a webvtt file contents and parses it into cues
  7027. *
  7028. * @param {string} srcContent
  7029. * webVTT file contents
  7030. *
  7031. * @param {TextTrack} track
  7032. * TextTrack to add cues to. Cues come from the srcContent.
  7033. *
  7034. * @private
  7035. */
  7036. const parseCues = function (srcContent, track) {
  7037. const parser = new window.WebVTT.Parser(window, window.vttjs, window.WebVTT.StringDecoder());
  7038. const errors = [];
  7039. parser.oncue = function (cue) {
  7040. track.addCue(cue);
  7041. };
  7042. parser.onparsingerror = function (error) {
  7043. errors.push(error);
  7044. };
  7045. parser.onflush = function () {
  7046. track.trigger({
  7047. type: 'loadeddata',
  7048. target: track
  7049. });
  7050. };
  7051. parser.parse(srcContent);
  7052. if (errors.length > 0) {
  7053. if (window.console && window.console.groupCollapsed) {
  7054. window.console.groupCollapsed(`Text Track parsing errors for ${track.src}`);
  7055. }
  7056. errors.forEach(error => log.error(error));
  7057. if (window.console && window.console.groupEnd) {
  7058. window.console.groupEnd();
  7059. }
  7060. }
  7061. parser.flush();
  7062. };
  7063. /**
  7064. * Load a `TextTrack` from a specified url.
  7065. *
  7066. * @param {string} src
  7067. * Url to load track from.
  7068. *
  7069. * @param {TextTrack} track
  7070. * Track to add cues to. Comes from the content at the end of `url`.
  7071. *
  7072. * @private
  7073. */
  7074. const loadTrack = function (src, track) {
  7075. const opts = {
  7076. uri: src
  7077. };
  7078. const crossOrigin = isCrossOrigin(src);
  7079. if (crossOrigin) {
  7080. opts.cors = crossOrigin;
  7081. }
  7082. const withCredentials = track.tech_.crossOrigin() === 'use-credentials';
  7083. if (withCredentials) {
  7084. opts.withCredentials = withCredentials;
  7085. }
  7086. lib(opts, bind_(this, function (err, response, responseBody) {
  7087. if (err) {
  7088. return log.error(err, response);
  7089. }
  7090. track.loaded_ = true;
  7091. // Make sure that vttjs has loaded, otherwise, wait till it finished loading
  7092. // NOTE: this is only used for the alt/video.novtt.js build
  7093. if (typeof window.WebVTT !== 'function') {
  7094. if (track.tech_) {
  7095. // to prevent use before define eslint error, we define loadHandler
  7096. // as a let here
  7097. track.tech_.any(['vttjsloaded', 'vttjserror'], event => {
  7098. if (event.type === 'vttjserror') {
  7099. log.error(`vttjs failed to load, stopping trying to process ${track.src}`);
  7100. return;
  7101. }
  7102. return parseCues(responseBody, track);
  7103. });
  7104. }
  7105. } else {
  7106. parseCues(responseBody, track);
  7107. }
  7108. }));
  7109. };
  7110. /**
  7111. * A representation of a single `TextTrack`.
  7112. *
  7113. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#texttrack}
  7114. * @extends Track
  7115. */
  7116. class TextTrack extends Track {
  7117. /**
  7118. * Create an instance of this class.
  7119. *
  7120. * @param {Object} options={}
  7121. * Object of option names and values
  7122. *
  7123. * @param { import('../tech/tech').default } options.tech
  7124. * A reference to the tech that owns this TextTrack.
  7125. *
  7126. * @param {TextTrack~Kind} [options.kind='subtitles']
  7127. * A valid text track kind.
  7128. *
  7129. * @param {TextTrack~Mode} [options.mode='disabled']
  7130. * A valid text track mode.
  7131. *
  7132. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7133. * A unique id for this TextTrack.
  7134. *
  7135. * @param {string} [options.label='']
  7136. * The menu label for this track.
  7137. *
  7138. * @param {string} [options.language='']
  7139. * A valid two character language code.
  7140. *
  7141. * @param {string} [options.srclang='']
  7142. * A valid two character language code. An alternative, but deprioritized
  7143. * version of `options.language`
  7144. *
  7145. * @param {string} [options.src]
  7146. * A url to TextTrack cues.
  7147. *
  7148. * @param {boolean} [options.default]
  7149. * If this track should default to on or off.
  7150. */
  7151. constructor(options = {}) {
  7152. if (!options.tech) {
  7153. throw new Error('A tech was not provided.');
  7154. }
  7155. const settings = merge(options, {
  7156. kind: TextTrackKind[options.kind] || 'subtitles',
  7157. language: options.language || options.srclang || ''
  7158. });
  7159. let mode = TextTrackMode[settings.mode] || 'disabled';
  7160. const default_ = settings.default;
  7161. if (settings.kind === 'metadata' || settings.kind === 'chapters') {
  7162. mode = 'hidden';
  7163. }
  7164. super(settings);
  7165. this.tech_ = settings.tech;
  7166. this.cues_ = [];
  7167. this.activeCues_ = [];
  7168. this.preload_ = this.tech_.preloadTextTracks !== false;
  7169. const cues = new TextTrackCueList(this.cues_);
  7170. const activeCues = new TextTrackCueList(this.activeCues_);
  7171. let changed = false;
  7172. this.timeupdateHandler = bind_(this, function (event = {}) {
  7173. if (this.tech_.isDisposed()) {
  7174. return;
  7175. }
  7176. if (!this.tech_.isReady_) {
  7177. if (event.type !== 'timeupdate') {
  7178. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  7179. }
  7180. return;
  7181. }
  7182. // Accessing this.activeCues for the side-effects of updating itself
  7183. // due to its nature as a getter function. Do not remove or cues will
  7184. // stop updating!
  7185. // Use the setter to prevent deletion from uglify (pure_getters rule)
  7186. this.activeCues = this.activeCues;
  7187. if (changed) {
  7188. this.trigger('cuechange');
  7189. changed = false;
  7190. }
  7191. if (event.type !== 'timeupdate') {
  7192. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  7193. }
  7194. });
  7195. const disposeHandler = () => {
  7196. this.stopTracking();
  7197. };
  7198. this.tech_.one('dispose', disposeHandler);
  7199. if (mode !== 'disabled') {
  7200. this.startTracking();
  7201. }
  7202. Object.defineProperties(this, {
  7203. /**
  7204. * @memberof TextTrack
  7205. * @member {boolean} default
  7206. * If this track was set to be on or off by default. Cannot be changed after
  7207. * creation.
  7208. * @instance
  7209. *
  7210. * @readonly
  7211. */
  7212. default: {
  7213. get() {
  7214. return default_;
  7215. },
  7216. set() {}
  7217. },
  7218. /**
  7219. * @memberof TextTrack
  7220. * @member {string} mode
  7221. * Set the mode of this TextTrack to a valid {@link TextTrack~Mode}. Will
  7222. * not be set if setting to an invalid mode.
  7223. * @instance
  7224. *
  7225. * @fires TextTrack#modechange
  7226. */
  7227. mode: {
  7228. get() {
  7229. return mode;
  7230. },
  7231. set(newMode) {
  7232. if (!TextTrackMode[newMode]) {
  7233. return;
  7234. }
  7235. if (mode === newMode) {
  7236. return;
  7237. }
  7238. mode = newMode;
  7239. if (!this.preload_ && mode !== 'disabled' && this.cues.length === 0) {
  7240. // On-demand load.
  7241. loadTrack(this.src, this);
  7242. }
  7243. this.stopTracking();
  7244. if (mode !== 'disabled') {
  7245. this.startTracking();
  7246. }
  7247. /**
  7248. * An event that fires when mode changes on this track. This allows
  7249. * the TextTrackList that holds this track to act accordingly.
  7250. *
  7251. * > Note: This is not part of the spec!
  7252. *
  7253. * @event TextTrack#modechange
  7254. * @type {Event}
  7255. */
  7256. this.trigger('modechange');
  7257. }
  7258. },
  7259. /**
  7260. * @memberof TextTrack
  7261. * @member {TextTrackCueList} cues
  7262. * The text track cue list for this TextTrack.
  7263. * @instance
  7264. */
  7265. cues: {
  7266. get() {
  7267. if (!this.loaded_) {
  7268. return null;
  7269. }
  7270. return cues;
  7271. },
  7272. set() {}
  7273. },
  7274. /**
  7275. * @memberof TextTrack
  7276. * @member {TextTrackCueList} activeCues
  7277. * The list text track cues that are currently active for this TextTrack.
  7278. * @instance
  7279. */
  7280. activeCues: {
  7281. get() {
  7282. if (!this.loaded_) {
  7283. return null;
  7284. }
  7285. // nothing to do
  7286. if (this.cues.length === 0) {
  7287. return activeCues;
  7288. }
  7289. const ct = this.tech_.currentTime();
  7290. const active = [];
  7291. for (let i = 0, l = this.cues.length; i < l; i++) {
  7292. const cue = this.cues[i];
  7293. if (cue.startTime <= ct && cue.endTime >= ct) {
  7294. active.push(cue);
  7295. }
  7296. }
  7297. changed = false;
  7298. if (active.length !== this.activeCues_.length) {
  7299. changed = true;
  7300. } else {
  7301. for (let i = 0; i < active.length; i++) {
  7302. if (this.activeCues_.indexOf(active[i]) === -1) {
  7303. changed = true;
  7304. }
  7305. }
  7306. }
  7307. this.activeCues_ = active;
  7308. activeCues.setCues_(this.activeCues_);
  7309. return activeCues;
  7310. },
  7311. // /!\ Keep this setter empty (see the timeupdate handler above)
  7312. set() {}
  7313. }
  7314. });
  7315. if (settings.src) {
  7316. this.src = settings.src;
  7317. if (!this.preload_) {
  7318. // Tracks will load on-demand.
  7319. // Act like we're loaded for other purposes.
  7320. this.loaded_ = true;
  7321. }
  7322. if (this.preload_ || settings.kind !== 'subtitles' && settings.kind !== 'captions') {
  7323. loadTrack(this.src, this);
  7324. }
  7325. } else {
  7326. this.loaded_ = true;
  7327. }
  7328. }
  7329. startTracking() {
  7330. // More precise cues based on requestVideoFrameCallback with a requestAnimationFram fallback
  7331. this.rvf_ = this.tech_.requestVideoFrameCallback(this.timeupdateHandler);
  7332. // Also listen to timeupdate in case rVFC/rAF stops (window in background, audio in video el)
  7333. this.tech_.on('timeupdate', this.timeupdateHandler);
  7334. }
  7335. stopTracking() {
  7336. if (this.rvf_) {
  7337. this.tech_.cancelVideoFrameCallback(this.rvf_);
  7338. this.rvf_ = undefined;
  7339. }
  7340. this.tech_.off('timeupdate', this.timeupdateHandler);
  7341. }
  7342. /**
  7343. * Add a cue to the internal list of cues.
  7344. *
  7345. * @param {TextTrack~Cue} cue
  7346. * The cue to add to our internal list
  7347. */
  7348. addCue(originalCue) {
  7349. let cue = originalCue;
  7350. if (window.vttjs && !(originalCue instanceof window.vttjs.VTTCue)) {
  7351. cue = new window.vttjs.VTTCue(originalCue.startTime, originalCue.endTime, originalCue.text);
  7352. for (const prop in originalCue) {
  7353. if (!(prop in cue)) {
  7354. cue[prop] = originalCue[prop];
  7355. }
  7356. }
  7357. // make sure that `id` is copied over
  7358. cue.id = originalCue.id;
  7359. cue.originalCue_ = originalCue;
  7360. }
  7361. const tracks = this.tech_.textTracks();
  7362. for (let i = 0; i < tracks.length; i++) {
  7363. if (tracks[i] !== this) {
  7364. tracks[i].removeCue(cue);
  7365. }
  7366. }
  7367. this.cues_.push(cue);
  7368. this.cues.setCues_(this.cues_);
  7369. }
  7370. /**
  7371. * Remove a cue from our internal list
  7372. *
  7373. * @param {TextTrack~Cue} removeCue
  7374. * The cue to remove from our internal list
  7375. */
  7376. removeCue(removeCue) {
  7377. let i = this.cues_.length;
  7378. while (i--) {
  7379. const cue = this.cues_[i];
  7380. if (cue === removeCue || cue.originalCue_ && cue.originalCue_ === removeCue) {
  7381. this.cues_.splice(i, 1);
  7382. this.cues.setCues_(this.cues_);
  7383. break;
  7384. }
  7385. }
  7386. }
  7387. }
  7388. /**
  7389. * cuechange - One or more cues in the track have become active or stopped being active.
  7390. */
  7391. TextTrack.prototype.allowedEvents_ = {
  7392. cuechange: 'cuechange'
  7393. };
  7394. /**
  7395. * A representation of a single `AudioTrack`. If it is part of an {@link AudioTrackList}
  7396. * only one `AudioTrack` in the list will be enabled at a time.
  7397. *
  7398. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotrack}
  7399. * @extends Track
  7400. */
  7401. class AudioTrack extends Track {
  7402. /**
  7403. * Create an instance of this class.
  7404. *
  7405. * @param {Object} [options={}]
  7406. * Object of option names and values
  7407. *
  7408. * @param {AudioTrack~Kind} [options.kind='']
  7409. * A valid audio track kind
  7410. *
  7411. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7412. * A unique id for this AudioTrack.
  7413. *
  7414. * @param {string} [options.label='']
  7415. * The menu label for this track.
  7416. *
  7417. * @param {string} [options.language='']
  7418. * A valid two character language code.
  7419. *
  7420. * @param {boolean} [options.enabled]
  7421. * If this track is the one that is currently playing. If this track is part of
  7422. * an {@link AudioTrackList}, only one {@link AudioTrack} will be enabled.
  7423. */
  7424. constructor(options = {}) {
  7425. const settings = merge(options, {
  7426. kind: AudioTrackKind[options.kind] || ''
  7427. });
  7428. super(settings);
  7429. let enabled = false;
  7430. /**
  7431. * @memberof AudioTrack
  7432. * @member {boolean} enabled
  7433. * If this `AudioTrack` is enabled or not. When setting this will
  7434. * fire {@link AudioTrack#enabledchange} if the state of enabled is changed.
  7435. * @instance
  7436. *
  7437. * @fires VideoTrack#selectedchange
  7438. */
  7439. Object.defineProperty(this, 'enabled', {
  7440. get() {
  7441. return enabled;
  7442. },
  7443. set(newEnabled) {
  7444. // an invalid or unchanged value
  7445. if (typeof newEnabled !== 'boolean' || newEnabled === enabled) {
  7446. return;
  7447. }
  7448. enabled = newEnabled;
  7449. /**
  7450. * An event that fires when enabled changes on this track. This allows
  7451. * the AudioTrackList that holds this track to act accordingly.
  7452. *
  7453. * > Note: This is not part of the spec! Native tracks will do
  7454. * this internally without an event.
  7455. *
  7456. * @event AudioTrack#enabledchange
  7457. * @type {Event}
  7458. */
  7459. this.trigger('enabledchange');
  7460. }
  7461. });
  7462. // if the user sets this track to selected then
  7463. // set selected to that true value otherwise
  7464. // we keep it false
  7465. if (settings.enabled) {
  7466. this.enabled = settings.enabled;
  7467. }
  7468. this.loaded_ = true;
  7469. }
  7470. }
  7471. /**
  7472. * A representation of a single `VideoTrack`.
  7473. *
  7474. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#videotrack}
  7475. * @extends Track
  7476. */
  7477. class VideoTrack extends Track {
  7478. /**
  7479. * Create an instance of this class.
  7480. *
  7481. * @param {Object} [options={}]
  7482. * Object of option names and values
  7483. *
  7484. * @param {string} [options.kind='']
  7485. * A valid {@link VideoTrack~Kind}
  7486. *
  7487. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7488. * A unique id for this AudioTrack.
  7489. *
  7490. * @param {string} [options.label='']
  7491. * The menu label for this track.
  7492. *
  7493. * @param {string} [options.language='']
  7494. * A valid two character language code.
  7495. *
  7496. * @param {boolean} [options.selected]
  7497. * If this track is the one that is currently playing.
  7498. */
  7499. constructor(options = {}) {
  7500. const settings = merge(options, {
  7501. kind: VideoTrackKind[options.kind] || ''
  7502. });
  7503. super(settings);
  7504. let selected = false;
  7505. /**
  7506. * @memberof VideoTrack
  7507. * @member {boolean} selected
  7508. * If this `VideoTrack` is selected or not. When setting this will
  7509. * fire {@link VideoTrack#selectedchange} if the state of selected changed.
  7510. * @instance
  7511. *
  7512. * @fires VideoTrack#selectedchange
  7513. */
  7514. Object.defineProperty(this, 'selected', {
  7515. get() {
  7516. return selected;
  7517. },
  7518. set(newSelected) {
  7519. // an invalid or unchanged value
  7520. if (typeof newSelected !== 'boolean' || newSelected === selected) {
  7521. return;
  7522. }
  7523. selected = newSelected;
  7524. /**
  7525. * An event that fires when selected changes on this track. This allows
  7526. * the VideoTrackList that holds this track to act accordingly.
  7527. *
  7528. * > Note: This is not part of the spec! Native tracks will do
  7529. * this internally without an event.
  7530. *
  7531. * @event VideoTrack#selectedchange
  7532. * @type {Event}
  7533. */
  7534. this.trigger('selectedchange');
  7535. }
  7536. });
  7537. // if the user sets this track to selected then
  7538. // set selected to that true value otherwise
  7539. // we keep it false
  7540. if (settings.selected) {
  7541. this.selected = settings.selected;
  7542. }
  7543. }
  7544. }
  7545. /**
  7546. * @file html-track-element.js
  7547. */
  7548. /**
  7549. * A single track represented in the DOM.
  7550. *
  7551. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#htmltrackelement}
  7552. * @extends EventTarget
  7553. */
  7554. class HTMLTrackElement extends EventTarget {
  7555. /**
  7556. * Create an instance of this class.
  7557. *
  7558. * @param {Object} options={}
  7559. * Object of option names and values
  7560. *
  7561. * @param { import('../tech/tech').default } options.tech
  7562. * A reference to the tech that owns this HTMLTrackElement.
  7563. *
  7564. * @param {TextTrack~Kind} [options.kind='subtitles']
  7565. * A valid text track kind.
  7566. *
  7567. * @param {TextTrack~Mode} [options.mode='disabled']
  7568. * A valid text track mode.
  7569. *
  7570. * @param {string} [options.id='vjs_track_' + Guid.newGUID()]
  7571. * A unique id for this TextTrack.
  7572. *
  7573. * @param {string} [options.label='']
  7574. * The menu label for this track.
  7575. *
  7576. * @param {string} [options.language='']
  7577. * A valid two character language code.
  7578. *
  7579. * @param {string} [options.srclang='']
  7580. * A valid two character language code. An alternative, but deprioritized
  7581. * version of `options.language`
  7582. *
  7583. * @param {string} [options.src]
  7584. * A url to TextTrack cues.
  7585. *
  7586. * @param {boolean} [options.default]
  7587. * If this track should default to on or off.
  7588. */
  7589. constructor(options = {}) {
  7590. super();
  7591. let readyState;
  7592. const track = new TextTrack(options);
  7593. this.kind = track.kind;
  7594. this.src = track.src;
  7595. this.srclang = track.language;
  7596. this.label = track.label;
  7597. this.default = track.default;
  7598. Object.defineProperties(this, {
  7599. /**
  7600. * @memberof HTMLTrackElement
  7601. * @member {HTMLTrackElement~ReadyState} readyState
  7602. * The current ready state of the track element.
  7603. * @instance
  7604. */
  7605. readyState: {
  7606. get() {
  7607. return readyState;
  7608. }
  7609. },
  7610. /**
  7611. * @memberof HTMLTrackElement
  7612. * @member {TextTrack} track
  7613. * The underlying TextTrack object.
  7614. * @instance
  7615. *
  7616. */
  7617. track: {
  7618. get() {
  7619. return track;
  7620. }
  7621. }
  7622. });
  7623. readyState = HTMLTrackElement.NONE;
  7624. /**
  7625. * @listens TextTrack#loadeddata
  7626. * @fires HTMLTrackElement#load
  7627. */
  7628. track.addEventListener('loadeddata', () => {
  7629. readyState = HTMLTrackElement.LOADED;
  7630. this.trigger({
  7631. type: 'load',
  7632. target: this
  7633. });
  7634. });
  7635. }
  7636. }
  7637. HTMLTrackElement.prototype.allowedEvents_ = {
  7638. load: 'load'
  7639. };
  7640. /**
  7641. * The text track not loaded state.
  7642. *
  7643. * @type {number}
  7644. * @static
  7645. */
  7646. HTMLTrackElement.NONE = 0;
  7647. /**
  7648. * The text track loading state.
  7649. *
  7650. * @type {number}
  7651. * @static
  7652. */
  7653. HTMLTrackElement.LOADING = 1;
  7654. /**
  7655. * The text track loaded state.
  7656. *
  7657. * @type {number}
  7658. * @static
  7659. */
  7660. HTMLTrackElement.LOADED = 2;
  7661. /**
  7662. * The text track failed to load state.
  7663. *
  7664. * @type {number}
  7665. * @static
  7666. */
  7667. HTMLTrackElement.ERROR = 3;
  7668. /*
  7669. * This file contains all track properties that are used in
  7670. * player.js, tech.js, html5.js and possibly other techs in the future.
  7671. */
  7672. const NORMAL = {
  7673. audio: {
  7674. ListClass: AudioTrackList,
  7675. TrackClass: AudioTrack,
  7676. capitalName: 'Audio'
  7677. },
  7678. video: {
  7679. ListClass: VideoTrackList,
  7680. TrackClass: VideoTrack,
  7681. capitalName: 'Video'
  7682. },
  7683. text: {
  7684. ListClass: TextTrackList,
  7685. TrackClass: TextTrack,
  7686. capitalName: 'Text'
  7687. }
  7688. };
  7689. Object.keys(NORMAL).forEach(function (type) {
  7690. NORMAL[type].getterName = `${type}Tracks`;
  7691. NORMAL[type].privateName = `${type}Tracks_`;
  7692. });
  7693. const REMOTE = {
  7694. remoteText: {
  7695. ListClass: TextTrackList,
  7696. TrackClass: TextTrack,
  7697. capitalName: 'RemoteText',
  7698. getterName: 'remoteTextTracks',
  7699. privateName: 'remoteTextTracks_'
  7700. },
  7701. remoteTextEl: {
  7702. ListClass: HtmlTrackElementList,
  7703. TrackClass: HTMLTrackElement,
  7704. capitalName: 'RemoteTextTrackEls',
  7705. getterName: 'remoteTextTrackEls',
  7706. privateName: 'remoteTextTrackEls_'
  7707. }
  7708. };
  7709. const ALL = Object.assign({}, NORMAL, REMOTE);
  7710. REMOTE.names = Object.keys(REMOTE);
  7711. NORMAL.names = Object.keys(NORMAL);
  7712. ALL.names = [].concat(REMOTE.names).concat(NORMAL.names);
  7713. var vtt = {};
  7714. /**
  7715. * @file tech.js
  7716. */
  7717. /**
  7718. * An Object containing a structure like: `{src: 'url', type: 'mimetype'}` or string
  7719. * that just contains the src url alone.
  7720. * * `var SourceObject = {src: 'http://ex.com/video.mp4', type: 'video/mp4'};`
  7721. * `var SourceString = 'http://example.com/some-video.mp4';`
  7722. *
  7723. * @typedef {Object|string} Tech~SourceObject
  7724. *
  7725. * @property {string} src
  7726. * The url to the source
  7727. *
  7728. * @property {string} type
  7729. * The mime type of the source
  7730. */
  7731. /**
  7732. * A function used by {@link Tech} to create a new {@link TextTrack}.
  7733. *
  7734. * @private
  7735. *
  7736. * @param {Tech} self
  7737. * An instance of the Tech class.
  7738. *
  7739. * @param {string} kind
  7740. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  7741. *
  7742. * @param {string} [label]
  7743. * Label to identify the text track
  7744. *
  7745. * @param {string} [language]
  7746. * Two letter language abbreviation
  7747. *
  7748. * @param {Object} [options={}]
  7749. * An object with additional text track options
  7750. *
  7751. * @return {TextTrack}
  7752. * The text track that was created.
  7753. */
  7754. function createTrackHelper(self, kind, label, language, options = {}) {
  7755. const tracks = self.textTracks();
  7756. options.kind = kind;
  7757. if (label) {
  7758. options.label = label;
  7759. }
  7760. if (language) {
  7761. options.language = language;
  7762. }
  7763. options.tech = self;
  7764. const track = new ALL.text.TrackClass(options);
  7765. tracks.addTrack(track);
  7766. return track;
  7767. }
  7768. /**
  7769. * This is the base class for media playback technology controllers, such as
  7770. * {@link HTML5}
  7771. *
  7772. * @extends Component
  7773. */
  7774. class Tech extends Component {
  7775. /**
  7776. * Create an instance of this Tech.
  7777. *
  7778. * @param {Object} [options]
  7779. * The key/value store of player options.
  7780. *
  7781. * @param {Function} [ready]
  7782. * Callback function to call when the `HTML5` Tech is ready.
  7783. */
  7784. constructor(options = {}, ready = function () {}) {
  7785. // we don't want the tech to report user activity automatically.
  7786. // This is done manually in addControlsListeners
  7787. options.reportTouchActivity = false;
  7788. super(null, options, ready);
  7789. this.onDurationChange_ = e => this.onDurationChange(e);
  7790. this.trackProgress_ = e => this.trackProgress(e);
  7791. this.trackCurrentTime_ = e => this.trackCurrentTime(e);
  7792. this.stopTrackingCurrentTime_ = e => this.stopTrackingCurrentTime(e);
  7793. this.disposeSourceHandler_ = e => this.disposeSourceHandler(e);
  7794. this.queuedHanders_ = new Set();
  7795. // keep track of whether the current source has played at all to
  7796. // implement a very limited played()
  7797. this.hasStarted_ = false;
  7798. this.on('playing', function () {
  7799. this.hasStarted_ = true;
  7800. });
  7801. this.on('loadstart', function () {
  7802. this.hasStarted_ = false;
  7803. });
  7804. ALL.names.forEach(name => {
  7805. const props = ALL[name];
  7806. if (options && options[props.getterName]) {
  7807. this[props.privateName] = options[props.getterName];
  7808. }
  7809. });
  7810. // Manually track progress in cases where the browser/tech doesn't report it.
  7811. if (!this.featuresProgressEvents) {
  7812. this.manualProgressOn();
  7813. }
  7814. // Manually track timeupdates in cases where the browser/tech doesn't report it.
  7815. if (!this.featuresTimeupdateEvents) {
  7816. this.manualTimeUpdatesOn();
  7817. }
  7818. ['Text', 'Audio', 'Video'].forEach(track => {
  7819. if (options[`native${track}Tracks`] === false) {
  7820. this[`featuresNative${track}Tracks`] = false;
  7821. }
  7822. });
  7823. if (options.nativeCaptions === false || options.nativeTextTracks === false) {
  7824. this.featuresNativeTextTracks = false;
  7825. } else if (options.nativeCaptions === true || options.nativeTextTracks === true) {
  7826. this.featuresNativeTextTracks = true;
  7827. }
  7828. if (!this.featuresNativeTextTracks) {
  7829. this.emulateTextTracks();
  7830. }
  7831. this.preloadTextTracks = options.preloadTextTracks !== false;
  7832. this.autoRemoteTextTracks_ = new ALL.text.ListClass();
  7833. this.initTrackListeners();
  7834. // Turn on component tap events only if not using native controls
  7835. if (!options.nativeControlsForTouch) {
  7836. this.emitTapEvents();
  7837. }
  7838. if (this.constructor) {
  7839. this.name_ = this.constructor.name || 'Unknown Tech';
  7840. }
  7841. }
  7842. /**
  7843. * A special function to trigger source set in a way that will allow player
  7844. * to re-trigger if the player or tech are not ready yet.
  7845. *
  7846. * @fires Tech#sourceset
  7847. * @param {string} src The source string at the time of the source changing.
  7848. */
  7849. triggerSourceset(src) {
  7850. if (!this.isReady_) {
  7851. // on initial ready we have to trigger source set
  7852. // 1ms after ready so that player can watch for it.
  7853. this.one('ready', () => this.setTimeout(() => this.triggerSourceset(src), 1));
  7854. }
  7855. /**
  7856. * Fired when the source is set on the tech causing the media element
  7857. * to reload.
  7858. *
  7859. * @see {@link Player#event:sourceset}
  7860. * @event Tech#sourceset
  7861. * @type {Event}
  7862. */
  7863. this.trigger({
  7864. src,
  7865. type: 'sourceset'
  7866. });
  7867. }
  7868. /* Fallbacks for unsupported event types
  7869. ================================================================================ */
  7870. /**
  7871. * Polyfill the `progress` event for browsers that don't support it natively.
  7872. *
  7873. * @see {@link Tech#trackProgress}
  7874. */
  7875. manualProgressOn() {
  7876. this.on('durationchange', this.onDurationChange_);
  7877. this.manualProgress = true;
  7878. // Trigger progress watching when a source begins loading
  7879. this.one('ready', this.trackProgress_);
  7880. }
  7881. /**
  7882. * Turn off the polyfill for `progress` events that was created in
  7883. * {@link Tech#manualProgressOn}
  7884. */
  7885. manualProgressOff() {
  7886. this.manualProgress = false;
  7887. this.stopTrackingProgress();
  7888. this.off('durationchange', this.onDurationChange_);
  7889. }
  7890. /**
  7891. * This is used to trigger a `progress` event when the buffered percent changes. It
  7892. * sets an interval function that will be called every 500 milliseconds to check if the
  7893. * buffer end percent has changed.
  7894. *
  7895. * > This function is called by {@link Tech#manualProgressOn}
  7896. *
  7897. * @param {Event} event
  7898. * The `ready` event that caused this to run.
  7899. *
  7900. * @listens Tech#ready
  7901. * @fires Tech#progress
  7902. */
  7903. trackProgress(event) {
  7904. this.stopTrackingProgress();
  7905. this.progressInterval = this.setInterval(bind_(this, function () {
  7906. // Don't trigger unless buffered amount is greater than last time
  7907. const numBufferedPercent = this.bufferedPercent();
  7908. if (this.bufferedPercent_ !== numBufferedPercent) {
  7909. /**
  7910. * See {@link Player#progress}
  7911. *
  7912. * @event Tech#progress
  7913. * @type {Event}
  7914. */
  7915. this.trigger('progress');
  7916. }
  7917. this.bufferedPercent_ = numBufferedPercent;
  7918. if (numBufferedPercent === 1) {
  7919. this.stopTrackingProgress();
  7920. }
  7921. }), 500);
  7922. }
  7923. /**
  7924. * Update our internal duration on a `durationchange` event by calling
  7925. * {@link Tech#duration}.
  7926. *
  7927. * @param {Event} event
  7928. * The `durationchange` event that caused this to run.
  7929. *
  7930. * @listens Tech#durationchange
  7931. */
  7932. onDurationChange(event) {
  7933. this.duration_ = this.duration();
  7934. }
  7935. /**
  7936. * Get and create a `TimeRange` object for buffering.
  7937. *
  7938. * @return { import('../utils/time').TimeRange }
  7939. * The time range object that was created.
  7940. */
  7941. buffered() {
  7942. return createTimeRanges(0, 0);
  7943. }
  7944. /**
  7945. * Get the percentage of the current video that is currently buffered.
  7946. *
  7947. * @return {number}
  7948. * A number from 0 to 1 that represents the decimal percentage of the
  7949. * video that is buffered.
  7950. *
  7951. */
  7952. bufferedPercent() {
  7953. return bufferedPercent(this.buffered(), this.duration_);
  7954. }
  7955. /**
  7956. * Turn off the polyfill for `progress` events that was created in
  7957. * {@link Tech#manualProgressOn}
  7958. * Stop manually tracking progress events by clearing the interval that was set in
  7959. * {@link Tech#trackProgress}.
  7960. */
  7961. stopTrackingProgress() {
  7962. this.clearInterval(this.progressInterval);
  7963. }
  7964. /**
  7965. * Polyfill the `timeupdate` event for browsers that don't support it.
  7966. *
  7967. * @see {@link Tech#trackCurrentTime}
  7968. */
  7969. manualTimeUpdatesOn() {
  7970. this.manualTimeUpdates = true;
  7971. this.on('play', this.trackCurrentTime_);
  7972. this.on('pause', this.stopTrackingCurrentTime_);
  7973. }
  7974. /**
  7975. * Turn off the polyfill for `timeupdate` events that was created in
  7976. * {@link Tech#manualTimeUpdatesOn}
  7977. */
  7978. manualTimeUpdatesOff() {
  7979. this.manualTimeUpdates = false;
  7980. this.stopTrackingCurrentTime();
  7981. this.off('play', this.trackCurrentTime_);
  7982. this.off('pause', this.stopTrackingCurrentTime_);
  7983. }
  7984. /**
  7985. * Sets up an interval function to track current time and trigger `timeupdate` every
  7986. * 250 milliseconds.
  7987. *
  7988. * @listens Tech#play
  7989. * @triggers Tech#timeupdate
  7990. */
  7991. trackCurrentTime() {
  7992. if (this.currentTimeInterval) {
  7993. this.stopTrackingCurrentTime();
  7994. }
  7995. this.currentTimeInterval = this.setInterval(function () {
  7996. /**
  7997. * Triggered at an interval of 250ms to indicated that time is passing in the video.
  7998. *
  7999. * @event Tech#timeupdate
  8000. * @type {Event}
  8001. */
  8002. this.trigger({
  8003. type: 'timeupdate',
  8004. target: this,
  8005. manuallyTriggered: true
  8006. });
  8007. // 42 = 24 fps // 250 is what Webkit uses // FF uses 15
  8008. }, 250);
  8009. }
  8010. /**
  8011. * Stop the interval function created in {@link Tech#trackCurrentTime} so that the
  8012. * `timeupdate` event is no longer triggered.
  8013. *
  8014. * @listens {Tech#pause}
  8015. */
  8016. stopTrackingCurrentTime() {
  8017. this.clearInterval(this.currentTimeInterval);
  8018. // #1002 - if the video ends right before the next timeupdate would happen,
  8019. // the progress bar won't make it all the way to the end
  8020. this.trigger({
  8021. type: 'timeupdate',
  8022. target: this,
  8023. manuallyTriggered: true
  8024. });
  8025. }
  8026. /**
  8027. * Turn off all event polyfills, clear the `Tech`s {@link AudioTrackList},
  8028. * {@link VideoTrackList}, and {@link TextTrackList}, and dispose of this Tech.
  8029. *
  8030. * @fires Component#dispose
  8031. */
  8032. dispose() {
  8033. // clear out all tracks because we can't reuse them between techs
  8034. this.clearTracks(NORMAL.names);
  8035. // Turn off any manual progress or timeupdate tracking
  8036. if (this.manualProgress) {
  8037. this.manualProgressOff();
  8038. }
  8039. if (this.manualTimeUpdates) {
  8040. this.manualTimeUpdatesOff();
  8041. }
  8042. super.dispose();
  8043. }
  8044. /**
  8045. * Clear out a single `TrackList` or an array of `TrackLists` given their names.
  8046. *
  8047. * > Note: Techs without source handlers should call this between sources for `video`
  8048. * & `audio` tracks. You don't want to use them between tracks!
  8049. *
  8050. * @param {string[]|string} types
  8051. * TrackList names to clear, valid names are `video`, `audio`, and
  8052. * `text`.
  8053. */
  8054. clearTracks(types) {
  8055. types = [].concat(types);
  8056. // clear out all tracks because we can't reuse them between techs
  8057. types.forEach(type => {
  8058. const list = this[`${type}Tracks`]() || [];
  8059. let i = list.length;
  8060. while (i--) {
  8061. const track = list[i];
  8062. if (type === 'text') {
  8063. this.removeRemoteTextTrack(track);
  8064. }
  8065. list.removeTrack(track);
  8066. }
  8067. });
  8068. }
  8069. /**
  8070. * Remove any TextTracks added via addRemoteTextTrack that are
  8071. * flagged for automatic garbage collection
  8072. */
  8073. cleanupAutoTextTracks() {
  8074. const list = this.autoRemoteTextTracks_ || [];
  8075. let i = list.length;
  8076. while (i--) {
  8077. const track = list[i];
  8078. this.removeRemoteTextTrack(track);
  8079. }
  8080. }
  8081. /**
  8082. * Reset the tech, which will removes all sources and reset the internal readyState.
  8083. *
  8084. * @abstract
  8085. */
  8086. reset() {}
  8087. /**
  8088. * Get the value of `crossOrigin` from the tech.
  8089. *
  8090. * @abstract
  8091. *
  8092. * @see {Html5#crossOrigin}
  8093. */
  8094. crossOrigin() {}
  8095. /**
  8096. * Set the value of `crossOrigin` on the tech.
  8097. *
  8098. * @abstract
  8099. *
  8100. * @param {string} crossOrigin the crossOrigin value
  8101. * @see {Html5#setCrossOrigin}
  8102. */
  8103. setCrossOrigin() {}
  8104. /**
  8105. * Get or set an error on the Tech.
  8106. *
  8107. * @param {MediaError} [err]
  8108. * Error to set on the Tech
  8109. *
  8110. * @return {MediaError|null}
  8111. * The current error object on the tech, or null if there isn't one.
  8112. */
  8113. error(err) {
  8114. if (err !== undefined) {
  8115. this.error_ = new MediaError(err);
  8116. this.trigger('error');
  8117. }
  8118. return this.error_;
  8119. }
  8120. /**
  8121. * Returns the `TimeRange`s that have been played through for the current source.
  8122. *
  8123. * > NOTE: This implementation is incomplete. It does not track the played `TimeRange`.
  8124. * It only checks whether the source has played at all or not.
  8125. *
  8126. * @return {TimeRange}
  8127. * - A single time range if this video has played
  8128. * - An empty set of ranges if not.
  8129. */
  8130. played() {
  8131. if (this.hasStarted_) {
  8132. return createTimeRanges(0, 0);
  8133. }
  8134. return createTimeRanges();
  8135. }
  8136. /**
  8137. * Start playback
  8138. *
  8139. * @abstract
  8140. *
  8141. * @see {Html5#play}
  8142. */
  8143. play() {}
  8144. /**
  8145. * Set whether we are scrubbing or not
  8146. *
  8147. * @abstract
  8148. * @param {boolean} _isScrubbing
  8149. * - true for we are currently scrubbing
  8150. * - false for we are no longer scrubbing
  8151. *
  8152. * @see {Html5#setScrubbing}
  8153. */
  8154. setScrubbing(_isScrubbing) {}
  8155. /**
  8156. * Get whether we are scrubbing or not
  8157. *
  8158. * @abstract
  8159. *
  8160. * @see {Html5#scrubbing}
  8161. */
  8162. scrubbing() {}
  8163. /**
  8164. * Causes a manual time update to occur if {@link Tech#manualTimeUpdatesOn} was
  8165. * previously called.
  8166. *
  8167. * @param {number} _seconds
  8168. * Set the current time of the media to this.
  8169. * @fires Tech#timeupdate
  8170. */
  8171. setCurrentTime(_seconds) {
  8172. // improve the accuracy of manual timeupdates
  8173. if (this.manualTimeUpdates) {
  8174. /**
  8175. * A manual `timeupdate` event.
  8176. *
  8177. * @event Tech#timeupdate
  8178. * @type {Event}
  8179. */
  8180. this.trigger({
  8181. type: 'timeupdate',
  8182. target: this,
  8183. manuallyTriggered: true
  8184. });
  8185. }
  8186. }
  8187. /**
  8188. * Turn on listeners for {@link VideoTrackList}, {@link {AudioTrackList}, and
  8189. * {@link TextTrackList} events.
  8190. *
  8191. * This adds {@link EventTarget~EventListeners} for `addtrack`, and `removetrack`.
  8192. *
  8193. * @fires Tech#audiotrackchange
  8194. * @fires Tech#videotrackchange
  8195. * @fires Tech#texttrackchange
  8196. */
  8197. initTrackListeners() {
  8198. /**
  8199. * Triggered when tracks are added or removed on the Tech {@link AudioTrackList}
  8200. *
  8201. * @event Tech#audiotrackchange
  8202. * @type {Event}
  8203. */
  8204. /**
  8205. * Triggered when tracks are added or removed on the Tech {@link VideoTrackList}
  8206. *
  8207. * @event Tech#videotrackchange
  8208. * @type {Event}
  8209. */
  8210. /**
  8211. * Triggered when tracks are added or removed on the Tech {@link TextTrackList}
  8212. *
  8213. * @event Tech#texttrackchange
  8214. * @type {Event}
  8215. */
  8216. NORMAL.names.forEach(name => {
  8217. const props = NORMAL[name];
  8218. const trackListChanges = () => {
  8219. this.trigger(`${name}trackchange`);
  8220. };
  8221. const tracks = this[props.getterName]();
  8222. tracks.addEventListener('removetrack', trackListChanges);
  8223. tracks.addEventListener('addtrack', trackListChanges);
  8224. this.on('dispose', () => {
  8225. tracks.removeEventListener('removetrack', trackListChanges);
  8226. tracks.removeEventListener('addtrack', trackListChanges);
  8227. });
  8228. });
  8229. }
  8230. /**
  8231. * Emulate TextTracks using vtt.js if necessary
  8232. *
  8233. * @fires Tech#vttjsloaded
  8234. * @fires Tech#vttjserror
  8235. */
  8236. addWebVttScript_() {
  8237. if (window.WebVTT) {
  8238. return;
  8239. }
  8240. // Initially, Tech.el_ is a child of a dummy-div wait until the Component system
  8241. // signals that the Tech is ready at which point Tech.el_ is part of the DOM
  8242. // before inserting the WebVTT script
  8243. if (document.body.contains(this.el())) {
  8244. // load via require if available and vtt.js script location was not passed in
  8245. // as an option. novtt builds will turn the above require call into an empty object
  8246. // which will cause this if check to always fail.
  8247. if (!this.options_['vtt.js'] && isPlain(vtt) && Object.keys(vtt).length > 0) {
  8248. this.trigger('vttjsloaded');
  8249. return;
  8250. }
  8251. // load vtt.js via the script location option or the cdn of no location was
  8252. // passed in
  8253. const script = document.createElement('script');
  8254. script.src = this.options_['vtt.js'] || 'https://vjs.zencdn.net/vttjs/0.14.1/vtt.min.js';
  8255. script.onload = () => {
  8256. /**
  8257. * Fired when vtt.js is loaded.
  8258. *
  8259. * @event Tech#vttjsloaded
  8260. * @type {Event}
  8261. */
  8262. this.trigger('vttjsloaded');
  8263. };
  8264. script.onerror = () => {
  8265. /**
  8266. * Fired when vtt.js was not loaded due to an error
  8267. *
  8268. * @event Tech#vttjsloaded
  8269. * @type {Event}
  8270. */
  8271. this.trigger('vttjserror');
  8272. };
  8273. this.on('dispose', () => {
  8274. script.onload = null;
  8275. script.onerror = null;
  8276. });
  8277. // but have not loaded yet and we set it to true before the inject so that
  8278. // we don't overwrite the injected window.WebVTT if it loads right away
  8279. window.WebVTT = true;
  8280. this.el().parentNode.appendChild(script);
  8281. } else {
  8282. this.ready(this.addWebVttScript_);
  8283. }
  8284. }
  8285. /**
  8286. * Emulate texttracks
  8287. *
  8288. */
  8289. emulateTextTracks() {
  8290. const tracks = this.textTracks();
  8291. const remoteTracks = this.remoteTextTracks();
  8292. const handleAddTrack = e => tracks.addTrack(e.track);
  8293. const handleRemoveTrack = e => tracks.removeTrack(e.track);
  8294. remoteTracks.on('addtrack', handleAddTrack);
  8295. remoteTracks.on('removetrack', handleRemoveTrack);
  8296. this.addWebVttScript_();
  8297. const updateDisplay = () => this.trigger('texttrackchange');
  8298. const textTracksChanges = () => {
  8299. updateDisplay();
  8300. for (let i = 0; i < tracks.length; i++) {
  8301. const track = tracks[i];
  8302. track.removeEventListener('cuechange', updateDisplay);
  8303. if (track.mode === 'showing') {
  8304. track.addEventListener('cuechange', updateDisplay);
  8305. }
  8306. }
  8307. };
  8308. textTracksChanges();
  8309. tracks.addEventListener('change', textTracksChanges);
  8310. tracks.addEventListener('addtrack', textTracksChanges);
  8311. tracks.addEventListener('removetrack', textTracksChanges);
  8312. this.on('dispose', function () {
  8313. remoteTracks.off('addtrack', handleAddTrack);
  8314. remoteTracks.off('removetrack', handleRemoveTrack);
  8315. tracks.removeEventListener('change', textTracksChanges);
  8316. tracks.removeEventListener('addtrack', textTracksChanges);
  8317. tracks.removeEventListener('removetrack', textTracksChanges);
  8318. for (let i = 0; i < tracks.length; i++) {
  8319. const track = tracks[i];
  8320. track.removeEventListener('cuechange', updateDisplay);
  8321. }
  8322. });
  8323. }
  8324. /**
  8325. * Create and returns a remote {@link TextTrack} object.
  8326. *
  8327. * @param {string} kind
  8328. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  8329. *
  8330. * @param {string} [label]
  8331. * Label to identify the text track
  8332. *
  8333. * @param {string} [language]
  8334. * Two letter language abbreviation
  8335. *
  8336. * @return {TextTrack}
  8337. * The TextTrack that gets created.
  8338. */
  8339. addTextTrack(kind, label, language) {
  8340. if (!kind) {
  8341. throw new Error('TextTrack kind is required but was not provided');
  8342. }
  8343. return createTrackHelper(this, kind, label, language);
  8344. }
  8345. /**
  8346. * Create an emulated TextTrack for use by addRemoteTextTrack
  8347. *
  8348. * This is intended to be overridden by classes that inherit from
  8349. * Tech in order to create native or custom TextTracks.
  8350. *
  8351. * @param {Object} options
  8352. * The object should contain the options to initialize the TextTrack with.
  8353. *
  8354. * @param {string} [options.kind]
  8355. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata).
  8356. *
  8357. * @param {string} [options.label].
  8358. * Label to identify the text track
  8359. *
  8360. * @param {string} [options.language]
  8361. * Two letter language abbreviation.
  8362. *
  8363. * @return {HTMLTrackElement}
  8364. * The track element that gets created.
  8365. */
  8366. createRemoteTextTrack(options) {
  8367. const track = merge(options, {
  8368. tech: this
  8369. });
  8370. return new REMOTE.remoteTextEl.TrackClass(track);
  8371. }
  8372. /**
  8373. * Creates a remote text track object and returns an html track element.
  8374. *
  8375. * > Note: This can be an emulated {@link HTMLTrackElement} or a native one.
  8376. *
  8377. * @param {Object} options
  8378. * See {@link Tech#createRemoteTextTrack} for more detailed properties.
  8379. *
  8380. * @param {boolean} [manualCleanup=false]
  8381. * - When false: the TextTrack will be automatically removed from the video
  8382. * element whenever the source changes
  8383. * - When True: The TextTrack will have to be cleaned up manually
  8384. *
  8385. * @return {HTMLTrackElement}
  8386. * An Html Track Element.
  8387. *
  8388. */
  8389. addRemoteTextTrack(options = {}, manualCleanup) {
  8390. const htmlTrackElement = this.createRemoteTextTrack(options);
  8391. if (typeof manualCleanup !== 'boolean') {
  8392. manualCleanup = false;
  8393. }
  8394. // store HTMLTrackElement and TextTrack to remote list
  8395. this.remoteTextTrackEls().addTrackElement_(htmlTrackElement);
  8396. this.remoteTextTracks().addTrack(htmlTrackElement.track);
  8397. if (manualCleanup === false) {
  8398. // create the TextTrackList if it doesn't exist
  8399. this.ready(() => this.autoRemoteTextTracks_.addTrack(htmlTrackElement.track));
  8400. }
  8401. return htmlTrackElement;
  8402. }
  8403. /**
  8404. * Remove a remote text track from the remote `TextTrackList`.
  8405. *
  8406. * @param {TextTrack} track
  8407. * `TextTrack` to remove from the `TextTrackList`
  8408. */
  8409. removeRemoteTextTrack(track) {
  8410. const trackElement = this.remoteTextTrackEls().getTrackElementByTrack_(track);
  8411. // remove HTMLTrackElement and TextTrack from remote list
  8412. this.remoteTextTrackEls().removeTrackElement_(trackElement);
  8413. this.remoteTextTracks().removeTrack(track);
  8414. this.autoRemoteTextTracks_.removeTrack(track);
  8415. }
  8416. /**
  8417. * Gets available media playback quality metrics as specified by the W3C's Media
  8418. * Playback Quality API.
  8419. *
  8420. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  8421. *
  8422. * @return {Object}
  8423. * An object with supported media playback quality metrics
  8424. *
  8425. * @abstract
  8426. */
  8427. getVideoPlaybackQuality() {
  8428. return {};
  8429. }
  8430. /**
  8431. * Attempt to create a floating video window always on top of other windows
  8432. * so that users may continue consuming media while they interact with other
  8433. * content sites, or applications on their device.
  8434. *
  8435. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  8436. *
  8437. * @return {Promise|undefined}
  8438. * A promise with a Picture-in-Picture window if the browser supports
  8439. * Promises (or one was passed in as an option). It returns undefined
  8440. * otherwise.
  8441. *
  8442. * @abstract
  8443. */
  8444. requestPictureInPicture() {
  8445. return Promise.reject();
  8446. }
  8447. /**
  8448. * A method to check for the value of the 'disablePictureInPicture' <video> property.
  8449. * Defaults to true, as it should be considered disabled if the tech does not support pip
  8450. *
  8451. * @abstract
  8452. */
  8453. disablePictureInPicture() {
  8454. return true;
  8455. }
  8456. /**
  8457. * A method to set or unset the 'disablePictureInPicture' <video> property.
  8458. *
  8459. * @abstract
  8460. */
  8461. setDisablePictureInPicture() {}
  8462. /**
  8463. * A fallback implementation of requestVideoFrameCallback using requestAnimationFrame
  8464. *
  8465. * @param {function} cb
  8466. * @return {number} request id
  8467. */
  8468. requestVideoFrameCallback(cb) {
  8469. const id = newGUID();
  8470. if (!this.isReady_ || this.paused()) {
  8471. this.queuedHanders_.add(id);
  8472. this.one('playing', () => {
  8473. if (this.queuedHanders_.has(id)) {
  8474. this.queuedHanders_.delete(id);
  8475. cb();
  8476. }
  8477. });
  8478. } else {
  8479. this.requestNamedAnimationFrame(id, cb);
  8480. }
  8481. return id;
  8482. }
  8483. /**
  8484. * A fallback implementation of cancelVideoFrameCallback
  8485. *
  8486. * @param {number} id id of callback to be cancelled
  8487. */
  8488. cancelVideoFrameCallback(id) {
  8489. if (this.queuedHanders_.has(id)) {
  8490. this.queuedHanders_.delete(id);
  8491. } else {
  8492. this.cancelNamedAnimationFrame(id);
  8493. }
  8494. }
  8495. /**
  8496. * A method to set a poster from a `Tech`.
  8497. *
  8498. * @abstract
  8499. */
  8500. setPoster() {}
  8501. /**
  8502. * A method to check for the presence of the 'playsinline' <video> attribute.
  8503. *
  8504. * @abstract
  8505. */
  8506. playsinline() {}
  8507. /**
  8508. * A method to set or unset the 'playsinline' <video> attribute.
  8509. *
  8510. * @abstract
  8511. */
  8512. setPlaysinline() {}
  8513. /**
  8514. * Attempt to force override of native audio tracks.
  8515. *
  8516. * @param {boolean} override - If set to true native audio will be overridden,
  8517. * otherwise native audio will potentially be used.
  8518. *
  8519. * @abstract
  8520. */
  8521. overrideNativeAudioTracks(override) {}
  8522. /**
  8523. * Attempt to force override of native video tracks.
  8524. *
  8525. * @param {boolean} override - If set to true native video will be overridden,
  8526. * otherwise native video will potentially be used.
  8527. *
  8528. * @abstract
  8529. */
  8530. overrideNativeVideoTracks(override) {}
  8531. /**
  8532. * Check if the tech can support the given mime-type.
  8533. *
  8534. * The base tech does not support any type, but source handlers might
  8535. * overwrite this.
  8536. *
  8537. * @param {string} _type
  8538. * The mimetype to check for support
  8539. *
  8540. * @return {string}
  8541. * 'probably', 'maybe', or empty string
  8542. *
  8543. * @see [Spec]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canPlayType}
  8544. *
  8545. * @abstract
  8546. */
  8547. canPlayType(_type) {
  8548. return '';
  8549. }
  8550. /**
  8551. * Check if the type is supported by this tech.
  8552. *
  8553. * The base tech does not support any type, but source handlers might
  8554. * overwrite this.
  8555. *
  8556. * @param {string} _type
  8557. * The media type to check
  8558. * @return {string} Returns the native video element's response
  8559. */
  8560. static canPlayType(_type) {
  8561. return '';
  8562. }
  8563. /**
  8564. * Check if the tech can support the given source
  8565. *
  8566. * @param {Object} srcObj
  8567. * The source object
  8568. * @param {Object} options
  8569. * The options passed to the tech
  8570. * @return {string} 'probably', 'maybe', or '' (empty string)
  8571. */
  8572. static canPlaySource(srcObj, options) {
  8573. return Tech.canPlayType(srcObj.type);
  8574. }
  8575. /*
  8576. * Return whether the argument is a Tech or not.
  8577. * Can be passed either a Class like `Html5` or a instance like `player.tech_`
  8578. *
  8579. * @param {Object} component
  8580. * The item to check
  8581. *
  8582. * @return {boolean}
  8583. * Whether it is a tech or not
  8584. * - True if it is a tech
  8585. * - False if it is not
  8586. */
  8587. static isTech(component) {
  8588. return component.prototype instanceof Tech || component instanceof Tech || component === Tech;
  8589. }
  8590. /**
  8591. * Registers a `Tech` into a shared list for videojs.
  8592. *
  8593. * @param {string} name
  8594. * Name of the `Tech` to register.
  8595. *
  8596. * @param {Object} tech
  8597. * The `Tech` class to register.
  8598. */
  8599. static registerTech(name, tech) {
  8600. if (!Tech.techs_) {
  8601. Tech.techs_ = {};
  8602. }
  8603. if (!Tech.isTech(tech)) {
  8604. throw new Error(`Tech ${name} must be a Tech`);
  8605. }
  8606. if (!Tech.canPlayType) {
  8607. throw new Error('Techs must have a static canPlayType method on them');
  8608. }
  8609. if (!Tech.canPlaySource) {
  8610. throw new Error('Techs must have a static canPlaySource method on them');
  8611. }
  8612. name = toTitleCase(name);
  8613. Tech.techs_[name] = tech;
  8614. Tech.techs_[toLowerCase(name)] = tech;
  8615. if (name !== 'Tech') {
  8616. // camel case the techName for use in techOrder
  8617. Tech.defaultTechOrder_.push(name);
  8618. }
  8619. return tech;
  8620. }
  8621. /**
  8622. * Get a `Tech` from the shared list by name.
  8623. *
  8624. * @param {string} name
  8625. * `camelCase` or `TitleCase` name of the Tech to get
  8626. *
  8627. * @return {Tech|undefined}
  8628. * The `Tech` or undefined if there was no tech with the name requested.
  8629. */
  8630. static getTech(name) {
  8631. if (!name) {
  8632. return;
  8633. }
  8634. if (Tech.techs_ && Tech.techs_[name]) {
  8635. return Tech.techs_[name];
  8636. }
  8637. name = toTitleCase(name);
  8638. if (window && window.videojs && window.videojs[name]) {
  8639. log.warn(`The ${name} tech was added to the videojs object when it should be registered using videojs.registerTech(name, tech)`);
  8640. return window.videojs[name];
  8641. }
  8642. }
  8643. }
  8644. /**
  8645. * Get the {@link VideoTrackList}
  8646. *
  8647. * @returns {VideoTrackList}
  8648. * @method Tech.prototype.videoTracks
  8649. */
  8650. /**
  8651. * Get the {@link AudioTrackList}
  8652. *
  8653. * @returns {AudioTrackList}
  8654. * @method Tech.prototype.audioTracks
  8655. */
  8656. /**
  8657. * Get the {@link TextTrackList}
  8658. *
  8659. * @returns {TextTrackList}
  8660. * @method Tech.prototype.textTracks
  8661. */
  8662. /**
  8663. * Get the remote element {@link TextTrackList}
  8664. *
  8665. * @returns {TextTrackList}
  8666. * @method Tech.prototype.remoteTextTracks
  8667. */
  8668. /**
  8669. * Get the remote element {@link HtmlTrackElementList}
  8670. *
  8671. * @returns {HtmlTrackElementList}
  8672. * @method Tech.prototype.remoteTextTrackEls
  8673. */
  8674. ALL.names.forEach(function (name) {
  8675. const props = ALL[name];
  8676. Tech.prototype[props.getterName] = function () {
  8677. this[props.privateName] = this[props.privateName] || new props.ListClass();
  8678. return this[props.privateName];
  8679. };
  8680. });
  8681. /**
  8682. * List of associated text tracks
  8683. *
  8684. * @type {TextTrackList}
  8685. * @private
  8686. * @property Tech#textTracks_
  8687. */
  8688. /**
  8689. * List of associated audio tracks.
  8690. *
  8691. * @type {AudioTrackList}
  8692. * @private
  8693. * @property Tech#audioTracks_
  8694. */
  8695. /**
  8696. * List of associated video tracks.
  8697. *
  8698. * @type {VideoTrackList}
  8699. * @private
  8700. * @property Tech#videoTracks_
  8701. */
  8702. /**
  8703. * Boolean indicating whether the `Tech` supports volume control.
  8704. *
  8705. * @type {boolean}
  8706. * @default
  8707. */
  8708. Tech.prototype.featuresVolumeControl = true;
  8709. /**
  8710. * Boolean indicating whether the `Tech` supports muting volume.
  8711. *
  8712. * @type {boolean}
  8713. * @default
  8714. */
  8715. Tech.prototype.featuresMuteControl = true;
  8716. /**
  8717. * Boolean indicating whether the `Tech` supports fullscreen resize control.
  8718. * Resizing plugins using request fullscreen reloads the plugin
  8719. *
  8720. * @type {boolean}
  8721. * @default
  8722. */
  8723. Tech.prototype.featuresFullscreenResize = false;
  8724. /**
  8725. * Boolean indicating whether the `Tech` supports changing the speed at which the video
  8726. * plays. Examples:
  8727. * - Set player to play 2x (twice) as fast
  8728. * - Set player to play 0.5x (half) as fast
  8729. *
  8730. * @type {boolean}
  8731. * @default
  8732. */
  8733. Tech.prototype.featuresPlaybackRate = false;
  8734. /**
  8735. * Boolean indicating whether the `Tech` supports the `progress` event.
  8736. * This will be used to determine if {@link Tech#manualProgressOn} should be called.
  8737. *
  8738. * @type {boolean}
  8739. * @default
  8740. */
  8741. Tech.prototype.featuresProgressEvents = false;
  8742. /**
  8743. * Boolean indicating whether the `Tech` supports the `sourceset` event.
  8744. *
  8745. * A tech should set this to `true` and then use {@link Tech#triggerSourceset}
  8746. * to trigger a {@link Tech#event:sourceset} at the earliest time after getting
  8747. * a new source.
  8748. *
  8749. * @type {boolean}
  8750. * @default
  8751. */
  8752. Tech.prototype.featuresSourceset = false;
  8753. /**
  8754. * Boolean indicating whether the `Tech` supports the `timeupdate` event.
  8755. * This will be used to determine if {@link Tech#manualTimeUpdates} should be called.
  8756. *
  8757. * @type {boolean}
  8758. * @default
  8759. */
  8760. Tech.prototype.featuresTimeupdateEvents = false;
  8761. /**
  8762. * Boolean indicating whether the `Tech` supports the native `TextTrack`s.
  8763. * This will help us integrate with native `TextTrack`s if the browser supports them.
  8764. *
  8765. * @type {boolean}
  8766. * @default
  8767. */
  8768. Tech.prototype.featuresNativeTextTracks = false;
  8769. /**
  8770. * Boolean indicating whether the `Tech` supports `requestVideoFrameCallback`.
  8771. *
  8772. * @type {boolean}
  8773. * @default
  8774. */
  8775. Tech.prototype.featuresVideoFrameCallback = false;
  8776. /**
  8777. * A functional mixin for techs that want to use the Source Handler pattern.
  8778. * Source handlers are scripts for handling specific formats.
  8779. * The source handler pattern is used for adaptive formats (HLS, DASH) that
  8780. * manually load video data and feed it into a Source Buffer (Media Source Extensions)
  8781. * Example: `Tech.withSourceHandlers.call(MyTech);`
  8782. *
  8783. * @param {Tech} _Tech
  8784. * The tech to add source handler functions to.
  8785. *
  8786. * @mixes Tech~SourceHandlerAdditions
  8787. */
  8788. Tech.withSourceHandlers = function (_Tech) {
  8789. /**
  8790. * Register a source handler
  8791. *
  8792. * @param {Function} handler
  8793. * The source handler class
  8794. *
  8795. * @param {number} [index]
  8796. * Register it at the following index
  8797. */
  8798. _Tech.registerSourceHandler = function (handler, index) {
  8799. let handlers = _Tech.sourceHandlers;
  8800. if (!handlers) {
  8801. handlers = _Tech.sourceHandlers = [];
  8802. }
  8803. if (index === undefined) {
  8804. // add to the end of the list
  8805. index = handlers.length;
  8806. }
  8807. handlers.splice(index, 0, handler);
  8808. };
  8809. /**
  8810. * Check if the tech can support the given type. Also checks the
  8811. * Techs sourceHandlers.
  8812. *
  8813. * @param {string} type
  8814. * The mimetype to check.
  8815. *
  8816. * @return {string}
  8817. * 'probably', 'maybe', or '' (empty string)
  8818. */
  8819. _Tech.canPlayType = function (type) {
  8820. const handlers = _Tech.sourceHandlers || [];
  8821. let can;
  8822. for (let i = 0; i < handlers.length; i++) {
  8823. can = handlers[i].canPlayType(type);
  8824. if (can) {
  8825. return can;
  8826. }
  8827. }
  8828. return '';
  8829. };
  8830. /**
  8831. * Returns the first source handler that supports the source.
  8832. *
  8833. * TODO: Answer question: should 'probably' be prioritized over 'maybe'
  8834. *
  8835. * @param {Tech~SourceObject} source
  8836. * The source object
  8837. *
  8838. * @param {Object} options
  8839. * The options passed to the tech
  8840. *
  8841. * @return {SourceHandler|null}
  8842. * The first source handler that supports the source or null if
  8843. * no SourceHandler supports the source
  8844. */
  8845. _Tech.selectSourceHandler = function (source, options) {
  8846. const handlers = _Tech.sourceHandlers || [];
  8847. let can;
  8848. for (let i = 0; i < handlers.length; i++) {
  8849. can = handlers[i].canHandleSource(source, options);
  8850. if (can) {
  8851. return handlers[i];
  8852. }
  8853. }
  8854. return null;
  8855. };
  8856. /**
  8857. * Check if the tech can support the given source.
  8858. *
  8859. * @param {Tech~SourceObject} srcObj
  8860. * The source object
  8861. *
  8862. * @param {Object} options
  8863. * The options passed to the tech
  8864. *
  8865. * @return {string}
  8866. * 'probably', 'maybe', or '' (empty string)
  8867. */
  8868. _Tech.canPlaySource = function (srcObj, options) {
  8869. const sh = _Tech.selectSourceHandler(srcObj, options);
  8870. if (sh) {
  8871. return sh.canHandleSource(srcObj, options);
  8872. }
  8873. return '';
  8874. };
  8875. /**
  8876. * When using a source handler, prefer its implementation of
  8877. * any function normally provided by the tech.
  8878. */
  8879. const deferrable = ['seekable', 'seeking', 'duration'];
  8880. /**
  8881. * A wrapper around {@link Tech#seekable} that will call a `SourceHandler`s seekable
  8882. * function if it exists, with a fallback to the Techs seekable function.
  8883. *
  8884. * @method _Tech.seekable
  8885. */
  8886. /**
  8887. * A wrapper around {@link Tech#duration} that will call a `SourceHandler`s duration
  8888. * function if it exists, otherwise it will fallback to the techs duration function.
  8889. *
  8890. * @method _Tech.duration
  8891. */
  8892. deferrable.forEach(function (fnName) {
  8893. const originalFn = this[fnName];
  8894. if (typeof originalFn !== 'function') {
  8895. return;
  8896. }
  8897. this[fnName] = function () {
  8898. if (this.sourceHandler_ && this.sourceHandler_[fnName]) {
  8899. return this.sourceHandler_[fnName].apply(this.sourceHandler_, arguments);
  8900. }
  8901. return originalFn.apply(this, arguments);
  8902. };
  8903. }, _Tech.prototype);
  8904. /**
  8905. * Create a function for setting the source using a source object
  8906. * and source handlers.
  8907. * Should never be called unless a source handler was found.
  8908. *
  8909. * @param {Tech~SourceObject} source
  8910. * A source object with src and type keys
  8911. */
  8912. _Tech.prototype.setSource = function (source) {
  8913. let sh = _Tech.selectSourceHandler(source, this.options_);
  8914. if (!sh) {
  8915. // Fall back to a native source handler when unsupported sources are
  8916. // deliberately set
  8917. if (_Tech.nativeSourceHandler) {
  8918. sh = _Tech.nativeSourceHandler;
  8919. } else {
  8920. log.error('No source handler found for the current source.');
  8921. }
  8922. }
  8923. // Dispose any existing source handler
  8924. this.disposeSourceHandler();
  8925. this.off('dispose', this.disposeSourceHandler_);
  8926. if (sh !== _Tech.nativeSourceHandler) {
  8927. this.currentSource_ = source;
  8928. }
  8929. this.sourceHandler_ = sh.handleSource(source, this, this.options_);
  8930. this.one('dispose', this.disposeSourceHandler_);
  8931. };
  8932. /**
  8933. * Clean up any existing SourceHandlers and listeners when the Tech is disposed.
  8934. *
  8935. * @listens Tech#dispose
  8936. */
  8937. _Tech.prototype.disposeSourceHandler = function () {
  8938. // if we have a source and get another one
  8939. // then we are loading something new
  8940. // than clear all of our current tracks
  8941. if (this.currentSource_) {
  8942. this.clearTracks(['audio', 'video']);
  8943. this.currentSource_ = null;
  8944. }
  8945. // always clean up auto-text tracks
  8946. this.cleanupAutoTextTracks();
  8947. if (this.sourceHandler_) {
  8948. if (this.sourceHandler_.dispose) {
  8949. this.sourceHandler_.dispose();
  8950. }
  8951. this.sourceHandler_ = null;
  8952. }
  8953. };
  8954. };
  8955. // The base Tech class needs to be registered as a Component. It is the only
  8956. // Tech that can be registered as a Component.
  8957. Component.registerComponent('Tech', Tech);
  8958. Tech.registerTech('Tech', Tech);
  8959. /**
  8960. * A list of techs that should be added to techOrder on Players
  8961. *
  8962. * @private
  8963. */
  8964. Tech.defaultTechOrder_ = [];
  8965. /**
  8966. * @file middleware.js
  8967. * @module middleware
  8968. */
  8969. const middlewares = {};
  8970. const middlewareInstances = {};
  8971. const TERMINATOR = {};
  8972. /**
  8973. * A middleware object is a plain JavaScript object that has methods that
  8974. * match the {@link Tech} methods found in the lists of allowed
  8975. * {@link module:middleware.allowedGetters|getters},
  8976. * {@link module:middleware.allowedSetters|setters}, and
  8977. * {@link module:middleware.allowedMediators|mediators}.
  8978. *
  8979. * @typedef {Object} MiddlewareObject
  8980. */
  8981. /**
  8982. * A middleware factory function that should return a
  8983. * {@link module:middleware~MiddlewareObject|MiddlewareObject}.
  8984. *
  8985. * This factory will be called for each player when needed, with the player
  8986. * passed in as an argument.
  8987. *
  8988. * @callback MiddlewareFactory
  8989. * @param { import('../player').default } player
  8990. * A Video.js player.
  8991. */
  8992. /**
  8993. * Define a middleware that the player should use by way of a factory function
  8994. * that returns a middleware object.
  8995. *
  8996. * @param {string} type
  8997. * The MIME type to match or `"*"` for all MIME types.
  8998. *
  8999. * @param {MiddlewareFactory} middleware
  9000. * A middleware factory function that will be executed for
  9001. * matching types.
  9002. */
  9003. function use(type, middleware) {
  9004. middlewares[type] = middlewares[type] || [];
  9005. middlewares[type].push(middleware);
  9006. }
  9007. /**
  9008. * Asynchronously sets a source using middleware by recursing through any
  9009. * matching middlewares and calling `setSource` on each, passing along the
  9010. * previous returned value each time.
  9011. *
  9012. * @param { import('../player').default } player
  9013. * A {@link Player} instance.
  9014. *
  9015. * @param {Tech~SourceObject} src
  9016. * A source object.
  9017. *
  9018. * @param {Function}
  9019. * The next middleware to run.
  9020. */
  9021. function setSource(player, src, next) {
  9022. player.setTimeout(() => setSourceHelper(src, middlewares[src.type], next, player), 1);
  9023. }
  9024. /**
  9025. * When the tech is set, passes the tech to each middleware's `setTech` method.
  9026. *
  9027. * @param {Object[]} middleware
  9028. * An array of middleware instances.
  9029. *
  9030. * @param { import('../tech/tech').default } tech
  9031. * A Video.js tech.
  9032. */
  9033. function setTech(middleware, tech) {
  9034. middleware.forEach(mw => mw.setTech && mw.setTech(tech));
  9035. }
  9036. /**
  9037. * Calls a getter on the tech first, through each middleware
  9038. * from right to left to the player.
  9039. *
  9040. * @param {Object[]} middleware
  9041. * An array of middleware instances.
  9042. *
  9043. * @param { import('../tech/tech').default } tech
  9044. * The current tech.
  9045. *
  9046. * @param {string} method
  9047. * A method name.
  9048. *
  9049. * @return {*}
  9050. * The final value from the tech after middleware has intercepted it.
  9051. */
  9052. function get(middleware, tech, method) {
  9053. return middleware.reduceRight(middlewareIterator(method), tech[method]());
  9054. }
  9055. /**
  9056. * Takes the argument given to the player and calls the setter method on each
  9057. * middleware from left to right to the tech.
  9058. *
  9059. * @param {Object[]} middleware
  9060. * An array of middleware instances.
  9061. *
  9062. * @param { import('../tech/tech').default } tech
  9063. * The current tech.
  9064. *
  9065. * @param {string} method
  9066. * A method name.
  9067. *
  9068. * @param {*} arg
  9069. * The value to set on the tech.
  9070. *
  9071. * @return {*}
  9072. * The return value of the `method` of the `tech`.
  9073. */
  9074. function set(middleware, tech, method, arg) {
  9075. return tech[method](middleware.reduce(middlewareIterator(method), arg));
  9076. }
  9077. /**
  9078. * Takes the argument given to the player and calls the `call` version of the
  9079. * method on each middleware from left to right.
  9080. *
  9081. * Then, call the passed in method on the tech and return the result unchanged
  9082. * back to the player, through middleware, this time from right to left.
  9083. *
  9084. * @param {Object[]} middleware
  9085. * An array of middleware instances.
  9086. *
  9087. * @param { import('../tech/tech').default } tech
  9088. * The current tech.
  9089. *
  9090. * @param {string} method
  9091. * A method name.
  9092. *
  9093. * @param {*} arg
  9094. * The value to set on the tech.
  9095. *
  9096. * @return {*}
  9097. * The return value of the `method` of the `tech`, regardless of the
  9098. * return values of middlewares.
  9099. */
  9100. function mediate(middleware, tech, method, arg = null) {
  9101. const callMethod = 'call' + toTitleCase(method);
  9102. const middlewareValue = middleware.reduce(middlewareIterator(callMethod), arg);
  9103. const terminated = middlewareValue === TERMINATOR;
  9104. // deprecated. The `null` return value should instead return TERMINATOR to
  9105. // prevent confusion if a techs method actually returns null.
  9106. const returnValue = terminated ? null : tech[method](middlewareValue);
  9107. executeRight(middleware, method, returnValue, terminated);
  9108. return returnValue;
  9109. }
  9110. /**
  9111. * Enumeration of allowed getters where the keys are method names.
  9112. *
  9113. * @type {Object}
  9114. */
  9115. const allowedGetters = {
  9116. buffered: 1,
  9117. currentTime: 1,
  9118. duration: 1,
  9119. muted: 1,
  9120. played: 1,
  9121. paused: 1,
  9122. seekable: 1,
  9123. volume: 1,
  9124. ended: 1
  9125. };
  9126. /**
  9127. * Enumeration of allowed setters where the keys are method names.
  9128. *
  9129. * @type {Object}
  9130. */
  9131. const allowedSetters = {
  9132. setCurrentTime: 1,
  9133. setMuted: 1,
  9134. setVolume: 1
  9135. };
  9136. /**
  9137. * Enumeration of allowed mediators where the keys are method names.
  9138. *
  9139. * @type {Object}
  9140. */
  9141. const allowedMediators = {
  9142. play: 1,
  9143. pause: 1
  9144. };
  9145. function middlewareIterator(method) {
  9146. return (value, mw) => {
  9147. // if the previous middleware terminated, pass along the termination
  9148. if (value === TERMINATOR) {
  9149. return TERMINATOR;
  9150. }
  9151. if (mw[method]) {
  9152. return mw[method](value);
  9153. }
  9154. return value;
  9155. };
  9156. }
  9157. function executeRight(mws, method, value, terminated) {
  9158. for (let i = mws.length - 1; i >= 0; i--) {
  9159. const mw = mws[i];
  9160. if (mw[method]) {
  9161. mw[method](terminated, value);
  9162. }
  9163. }
  9164. }
  9165. /**
  9166. * Clear the middleware cache for a player.
  9167. *
  9168. * @param { import('../player').default } player
  9169. * A {@link Player} instance.
  9170. */
  9171. function clearCacheForPlayer(player) {
  9172. middlewareInstances[player.id()] = null;
  9173. }
  9174. /**
  9175. * {
  9176. * [playerId]: [[mwFactory, mwInstance], ...]
  9177. * }
  9178. *
  9179. * @private
  9180. */
  9181. function getOrCreateFactory(player, mwFactory) {
  9182. const mws = middlewareInstances[player.id()];
  9183. let mw = null;
  9184. if (mws === undefined || mws === null) {
  9185. mw = mwFactory(player);
  9186. middlewareInstances[player.id()] = [[mwFactory, mw]];
  9187. return mw;
  9188. }
  9189. for (let i = 0; i < mws.length; i++) {
  9190. const [mwf, mwi] = mws[i];
  9191. if (mwf !== mwFactory) {
  9192. continue;
  9193. }
  9194. mw = mwi;
  9195. }
  9196. if (mw === null) {
  9197. mw = mwFactory(player);
  9198. mws.push([mwFactory, mw]);
  9199. }
  9200. return mw;
  9201. }
  9202. function setSourceHelper(src = {}, middleware = [], next, player, acc = [], lastRun = false) {
  9203. const [mwFactory, ...mwrest] = middleware;
  9204. // if mwFactory is a string, then we're at a fork in the road
  9205. if (typeof mwFactory === 'string') {
  9206. setSourceHelper(src, middlewares[mwFactory], next, player, acc, lastRun);
  9207. // if we have an mwFactory, call it with the player to get the mw,
  9208. // then call the mw's setSource method
  9209. } else if (mwFactory) {
  9210. const mw = getOrCreateFactory(player, mwFactory);
  9211. // if setSource isn't present, implicitly select this middleware
  9212. if (!mw.setSource) {
  9213. acc.push(mw);
  9214. return setSourceHelper(src, mwrest, next, player, acc, lastRun);
  9215. }
  9216. mw.setSource(Object.assign({}, src), function (err, _src) {
  9217. // something happened, try the next middleware on the current level
  9218. // make sure to use the old src
  9219. if (err) {
  9220. return setSourceHelper(src, mwrest, next, player, acc, lastRun);
  9221. }
  9222. // we've succeeded, now we need to go deeper
  9223. acc.push(mw);
  9224. // if it's the same type, continue down the current chain
  9225. // otherwise, we want to go down the new chain
  9226. setSourceHelper(_src, src.type === _src.type ? mwrest : middlewares[_src.type], next, player, acc, lastRun);
  9227. });
  9228. } else if (mwrest.length) {
  9229. setSourceHelper(src, mwrest, next, player, acc, lastRun);
  9230. } else if (lastRun) {
  9231. next(src, acc);
  9232. } else {
  9233. setSourceHelper(src, middlewares['*'], next, player, acc, true);
  9234. }
  9235. }
  9236. /**
  9237. * Mimetypes
  9238. *
  9239. * @see https://www.iana.org/assignments/media-types/media-types.xhtml
  9240. * @typedef Mimetypes~Kind
  9241. * @enum
  9242. */
  9243. const MimetypesKind = {
  9244. opus: 'video/ogg',
  9245. ogv: 'video/ogg',
  9246. mp4: 'video/mp4',
  9247. mov: 'video/mp4',
  9248. m4v: 'video/mp4',
  9249. mkv: 'video/x-matroska',
  9250. m4a: 'audio/mp4',
  9251. mp3: 'audio/mpeg',
  9252. aac: 'audio/aac',
  9253. caf: 'audio/x-caf',
  9254. flac: 'audio/flac',
  9255. oga: 'audio/ogg',
  9256. wav: 'audio/wav',
  9257. m3u8: 'application/x-mpegURL',
  9258. mpd: 'application/dash+xml',
  9259. jpg: 'image/jpeg',
  9260. jpeg: 'image/jpeg',
  9261. gif: 'image/gif',
  9262. png: 'image/png',
  9263. svg: 'image/svg+xml',
  9264. webp: 'image/webp'
  9265. };
  9266. /**
  9267. * Get the mimetype of a given src url if possible
  9268. *
  9269. * @param {string} src
  9270. * The url to the src
  9271. *
  9272. * @return {string}
  9273. * return the mimetype if it was known or empty string otherwise
  9274. */
  9275. const getMimetype = function (src = '') {
  9276. const ext = getFileExtension(src);
  9277. const mimetype = MimetypesKind[ext.toLowerCase()];
  9278. return mimetype || '';
  9279. };
  9280. /**
  9281. * Find the mime type of a given source string if possible. Uses the player
  9282. * source cache.
  9283. *
  9284. * @param { import('../player').default } player
  9285. * The player object
  9286. *
  9287. * @param {string} src
  9288. * The source string
  9289. *
  9290. * @return {string}
  9291. * The type that was found
  9292. */
  9293. const findMimetype = (player, src) => {
  9294. if (!src) {
  9295. return '';
  9296. }
  9297. // 1. check for the type in the `source` cache
  9298. if (player.cache_.source.src === src && player.cache_.source.type) {
  9299. return player.cache_.source.type;
  9300. }
  9301. // 2. see if we have this source in our `currentSources` cache
  9302. const matchingSources = player.cache_.sources.filter(s => s.src === src);
  9303. if (matchingSources.length) {
  9304. return matchingSources[0].type;
  9305. }
  9306. // 3. look for the src url in source elements and use the type there
  9307. const sources = player.$$('source');
  9308. for (let i = 0; i < sources.length; i++) {
  9309. const s = sources[i];
  9310. if (s.type && s.src && s.src === src) {
  9311. return s.type;
  9312. }
  9313. }
  9314. // 4. finally fallback to our list of mime types based on src url extension
  9315. return getMimetype(src);
  9316. };
  9317. /**
  9318. * @module filter-source
  9319. */
  9320. /**
  9321. * Filter out single bad source objects or multiple source objects in an
  9322. * array. Also flattens nested source object arrays into a 1 dimensional
  9323. * array of source objects.
  9324. *
  9325. * @param {Tech~SourceObject|Tech~SourceObject[]} src
  9326. * The src object to filter
  9327. *
  9328. * @return {Tech~SourceObject[]}
  9329. * An array of sourceobjects containing only valid sources
  9330. *
  9331. * @private
  9332. */
  9333. const filterSource = function (src) {
  9334. // traverse array
  9335. if (Array.isArray(src)) {
  9336. let newsrc = [];
  9337. src.forEach(function (srcobj) {
  9338. srcobj = filterSource(srcobj);
  9339. if (Array.isArray(srcobj)) {
  9340. newsrc = newsrc.concat(srcobj);
  9341. } else if (isObject(srcobj)) {
  9342. newsrc.push(srcobj);
  9343. }
  9344. });
  9345. src = newsrc;
  9346. } else if (typeof src === 'string' && src.trim()) {
  9347. // convert string into object
  9348. src = [fixSource({
  9349. src
  9350. })];
  9351. } else if (isObject(src) && typeof src.src === 'string' && src.src && src.src.trim()) {
  9352. // src is already valid
  9353. src = [fixSource(src)];
  9354. } else {
  9355. // invalid source, turn it into an empty array
  9356. src = [];
  9357. }
  9358. return src;
  9359. };
  9360. /**
  9361. * Checks src mimetype, adding it when possible
  9362. *
  9363. * @param {Tech~SourceObject} src
  9364. * The src object to check
  9365. * @return {Tech~SourceObject}
  9366. * src Object with known type
  9367. */
  9368. function fixSource(src) {
  9369. if (!src.type) {
  9370. const mimetype = getMimetype(src.src);
  9371. if (mimetype) {
  9372. src.type = mimetype;
  9373. }
  9374. }
  9375. return src;
  9376. }
  9377. /**
  9378. * @file loader.js
  9379. */
  9380. /**
  9381. * The `MediaLoader` is the `Component` that decides which playback technology to load
  9382. * when a player is initialized.
  9383. *
  9384. * @extends Component
  9385. */
  9386. class MediaLoader extends Component {
  9387. /**
  9388. * Create an instance of this class.
  9389. *
  9390. * @param { import('../player').default } player
  9391. * The `Player` that this class should attach to.
  9392. *
  9393. * @param {Object} [options]
  9394. * The key/value store of player options.
  9395. *
  9396. * @param {Function} [ready]
  9397. * The function that is run when this component is ready.
  9398. */
  9399. constructor(player, options, ready) {
  9400. // MediaLoader has no element
  9401. const options_ = merge({
  9402. createEl: false
  9403. }, options);
  9404. super(player, options_, ready);
  9405. // If there are no sources when the player is initialized,
  9406. // load the first supported playback technology.
  9407. if (!options.playerOptions.sources || options.playerOptions.sources.length === 0) {
  9408. for (let i = 0, j = options.playerOptions.techOrder; i < j.length; i++) {
  9409. const techName = toTitleCase(j[i]);
  9410. let tech = Tech.getTech(techName);
  9411. // Support old behavior of techs being registered as components.
  9412. // Remove once that deprecated behavior is removed.
  9413. if (!techName) {
  9414. tech = Component.getComponent(techName);
  9415. }
  9416. // Check if the browser supports this technology
  9417. if (tech && tech.isSupported()) {
  9418. player.loadTech_(techName);
  9419. break;
  9420. }
  9421. }
  9422. } else {
  9423. // Loop through playback technologies (e.g. HTML5) and check for support.
  9424. // Then load the best source.
  9425. // A few assumptions here:
  9426. // All playback technologies respect preload false.
  9427. player.src(options.playerOptions.sources);
  9428. }
  9429. }
  9430. }
  9431. Component.registerComponent('MediaLoader', MediaLoader);
  9432. /**
  9433. * @file clickable-component.js
  9434. */
  9435. /**
  9436. * Component which is clickable or keyboard actionable, but is not a
  9437. * native HTML button.
  9438. *
  9439. * @extends Component
  9440. */
  9441. class ClickableComponent extends Component {
  9442. /**
  9443. * Creates an instance of this class.
  9444. *
  9445. * @param { import('./player').default } player
  9446. * The `Player` that this class should be attached to.
  9447. *
  9448. * @param {Object} [options]
  9449. * The key/value store of component options.
  9450. *
  9451. * @param {function} [options.clickHandler]
  9452. * The function to call when the button is clicked / activated
  9453. *
  9454. * @param {string} [options.controlText]
  9455. * The text to set on the button
  9456. *
  9457. * @param {string} [options.className]
  9458. * A class or space separated list of classes to add the component
  9459. *
  9460. */
  9461. constructor(player, options) {
  9462. super(player, options);
  9463. if (this.options_.controlText) {
  9464. this.controlText(this.options_.controlText);
  9465. }
  9466. this.handleMouseOver_ = e => this.handleMouseOver(e);
  9467. this.handleMouseOut_ = e => this.handleMouseOut(e);
  9468. this.handleClick_ = e => this.handleClick(e);
  9469. this.handleKeyDown_ = e => this.handleKeyDown(e);
  9470. this.emitTapEvents();
  9471. this.enable();
  9472. }
  9473. /**
  9474. * Create the `ClickableComponent`s DOM element.
  9475. *
  9476. * @param {string} [tag=div]
  9477. * The element's node type.
  9478. *
  9479. * @param {Object} [props={}]
  9480. * An object of properties that should be set on the element.
  9481. *
  9482. * @param {Object} [attributes={}]
  9483. * An object of attributes that should be set on the element.
  9484. *
  9485. * @return {Element}
  9486. * The element that gets created.
  9487. */
  9488. createEl(tag = 'div', props = {}, attributes = {}) {
  9489. props = Object.assign({
  9490. className: this.buildCSSClass(),
  9491. tabIndex: 0
  9492. }, props);
  9493. if (tag === 'button') {
  9494. log.error(`Creating a ClickableComponent with an HTML element of ${tag} is not supported; use a Button instead.`);
  9495. }
  9496. // Add ARIA attributes for clickable element which is not a native HTML button
  9497. attributes = Object.assign({
  9498. role: 'button'
  9499. }, attributes);
  9500. this.tabIndex_ = props.tabIndex;
  9501. const el = createEl(tag, props, attributes);
  9502. el.appendChild(createEl('span', {
  9503. className: 'vjs-icon-placeholder'
  9504. }, {
  9505. 'aria-hidden': true
  9506. }));
  9507. this.createControlTextEl(el);
  9508. return el;
  9509. }
  9510. dispose() {
  9511. // remove controlTextEl_ on dispose
  9512. this.controlTextEl_ = null;
  9513. super.dispose();
  9514. }
  9515. /**
  9516. * Create a control text element on this `ClickableComponent`
  9517. *
  9518. * @param {Element} [el]
  9519. * Parent element for the control text.
  9520. *
  9521. * @return {Element}
  9522. * The control text element that gets created.
  9523. */
  9524. createControlTextEl(el) {
  9525. this.controlTextEl_ = createEl('span', {
  9526. className: 'vjs-control-text'
  9527. }, {
  9528. // let the screen reader user know that the text of the element may change
  9529. 'aria-live': 'polite'
  9530. });
  9531. if (el) {
  9532. el.appendChild(this.controlTextEl_);
  9533. }
  9534. this.controlText(this.controlText_, el);
  9535. return this.controlTextEl_;
  9536. }
  9537. /**
  9538. * Get or set the localize text to use for the controls on the `ClickableComponent`.
  9539. *
  9540. * @param {string} [text]
  9541. * Control text for element.
  9542. *
  9543. * @param {Element} [el=this.el()]
  9544. * Element to set the title on.
  9545. *
  9546. * @return {string}
  9547. * - The control text when getting
  9548. */
  9549. controlText(text, el = this.el()) {
  9550. if (text === undefined) {
  9551. return this.controlText_ || 'Need Text';
  9552. }
  9553. const localizedText = this.localize(text);
  9554. /** @protected */
  9555. this.controlText_ = text;
  9556. textContent(this.controlTextEl_, localizedText);
  9557. if (!this.nonIconControl && !this.player_.options_.noUITitleAttributes) {
  9558. // Set title attribute if only an icon is shown
  9559. el.setAttribute('title', localizedText);
  9560. }
  9561. }
  9562. /**
  9563. * Builds the default DOM `className`.
  9564. *
  9565. * @return {string}
  9566. * The DOM `className` for this object.
  9567. */
  9568. buildCSSClass() {
  9569. return `vjs-control vjs-button ${super.buildCSSClass()}`;
  9570. }
  9571. /**
  9572. * Enable this `ClickableComponent`
  9573. */
  9574. enable() {
  9575. if (!this.enabled_) {
  9576. this.enabled_ = true;
  9577. this.removeClass('vjs-disabled');
  9578. this.el_.setAttribute('aria-disabled', 'false');
  9579. if (typeof this.tabIndex_ !== 'undefined') {
  9580. this.el_.setAttribute('tabIndex', this.tabIndex_);
  9581. }
  9582. this.on(['tap', 'click'], this.handleClick_);
  9583. this.on('keydown', this.handleKeyDown_);
  9584. }
  9585. }
  9586. /**
  9587. * Disable this `ClickableComponent`
  9588. */
  9589. disable() {
  9590. this.enabled_ = false;
  9591. this.addClass('vjs-disabled');
  9592. this.el_.setAttribute('aria-disabled', 'true');
  9593. if (typeof this.tabIndex_ !== 'undefined') {
  9594. this.el_.removeAttribute('tabIndex');
  9595. }
  9596. this.off('mouseover', this.handleMouseOver_);
  9597. this.off('mouseout', this.handleMouseOut_);
  9598. this.off(['tap', 'click'], this.handleClick_);
  9599. this.off('keydown', this.handleKeyDown_);
  9600. }
  9601. /**
  9602. * Handles language change in ClickableComponent for the player in components
  9603. *
  9604. *
  9605. */
  9606. handleLanguagechange() {
  9607. this.controlText(this.controlText_);
  9608. }
  9609. /**
  9610. * Event handler that is called when a `ClickableComponent` receives a
  9611. * `click` or `tap` event.
  9612. *
  9613. * @param {Event} event
  9614. * The `tap` or `click` event that caused this function to be called.
  9615. *
  9616. * @listens tap
  9617. * @listens click
  9618. * @abstract
  9619. */
  9620. handleClick(event) {
  9621. if (this.options_.clickHandler) {
  9622. this.options_.clickHandler.call(this, arguments);
  9623. }
  9624. }
  9625. /**
  9626. * Event handler that is called when a `ClickableComponent` receives a
  9627. * `keydown` event.
  9628. *
  9629. * By default, if the key is Space or Enter, it will trigger a `click` event.
  9630. *
  9631. * @param {Event} event
  9632. * The `keydown` event that caused this function to be called.
  9633. *
  9634. * @listens keydown
  9635. */
  9636. handleKeyDown(event) {
  9637. // Support Space or Enter key operation to fire a click event. Also,
  9638. // prevent the event from propagating through the DOM and triggering
  9639. // Player hotkeys.
  9640. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  9641. event.preventDefault();
  9642. event.stopPropagation();
  9643. this.trigger('click');
  9644. } else {
  9645. // Pass keypress handling up for unsupported keys
  9646. super.handleKeyDown(event);
  9647. }
  9648. }
  9649. }
  9650. Component.registerComponent('ClickableComponent', ClickableComponent);
  9651. /**
  9652. * @file poster-image.js
  9653. */
  9654. /**
  9655. * A `ClickableComponent` that handles showing the poster image for the player.
  9656. *
  9657. * @extends ClickableComponent
  9658. */
  9659. class PosterImage extends ClickableComponent {
  9660. /**
  9661. * Create an instance of this class.
  9662. *
  9663. * @param { import('./player').default } player
  9664. * The `Player` that this class should attach to.
  9665. *
  9666. * @param {Object} [options]
  9667. * The key/value store of player options.
  9668. */
  9669. constructor(player, options) {
  9670. super(player, options);
  9671. this.update();
  9672. this.update_ = e => this.update(e);
  9673. player.on('posterchange', this.update_);
  9674. }
  9675. /**
  9676. * Clean up and dispose of the `PosterImage`.
  9677. */
  9678. dispose() {
  9679. this.player().off('posterchange', this.update_);
  9680. super.dispose();
  9681. }
  9682. /**
  9683. * Create the `PosterImage`s DOM element.
  9684. *
  9685. * @return {Element}
  9686. * The element that gets created.
  9687. */
  9688. createEl() {
  9689. // The el is an empty div to keep position in the DOM
  9690. // A picture and img el will be inserted when a source is set
  9691. return createEl('div', {
  9692. className: 'vjs-poster'
  9693. });
  9694. }
  9695. /**
  9696. * Get or set the `PosterImage`'s crossOrigin option.
  9697. *
  9698. * @param {string|null} [value]
  9699. * The value to set the crossOrigin to. If an argument is
  9700. * given, must be one of `'anonymous'` or `'use-credentials'`, or 'null'.
  9701. *
  9702. * @return {string|null}
  9703. * - The current crossOrigin value of the `Player` when getting.
  9704. * - undefined when setting
  9705. */
  9706. crossOrigin(value) {
  9707. // `null` can be set to unset a value
  9708. if (typeof value === 'undefined') {
  9709. if (this.$('img')) {
  9710. // If the poster's element exists, give its value
  9711. return this.$('img').crossOrigin;
  9712. } else if (this.player_.tech_ && this.player_.tech_.isReady_) {
  9713. // If not but the tech is ready, query the tech
  9714. return this.player_.crossOrigin();
  9715. }
  9716. // Otherwise check options as the poster is usually set before the state of crossorigin
  9717. // can be retrieved by the getter
  9718. return this.player_.options_.crossOrigin || this.player_.options_.crossorigin || null;
  9719. }
  9720. if (value !== null && value !== 'anonymous' && value !== 'use-credentials') {
  9721. this.player_.log.warn(`crossOrigin must be null, "anonymous" or "use-credentials", given "${value}"`);
  9722. return;
  9723. }
  9724. if (this.$('img')) {
  9725. this.$('img').crossOrigin = value;
  9726. }
  9727. return;
  9728. }
  9729. /**
  9730. * An {@link EventTarget~EventListener} for {@link Player#posterchange} events.
  9731. *
  9732. * @listens Player#posterchange
  9733. *
  9734. * @param {Event} [event]
  9735. * The `Player#posterchange` event that triggered this function.
  9736. */
  9737. update(event) {
  9738. const url = this.player().poster();
  9739. this.setSrc(url);
  9740. // If there's no poster source we should display:none on this component
  9741. // so it's not still clickable or right-clickable
  9742. if (url) {
  9743. this.show();
  9744. } else {
  9745. this.hide();
  9746. }
  9747. }
  9748. /**
  9749. * Set the source of the `PosterImage` depending on the display method. (Re)creates
  9750. * the inner picture and img elementss when needed.
  9751. *
  9752. * @param {string} [url]
  9753. * The URL to the source for the `PosterImage`. If not specified or falsy,
  9754. * any source and ant inner picture/img are removed.
  9755. */
  9756. setSrc(url) {
  9757. if (!url) {
  9758. this.el_.textContent = '';
  9759. return;
  9760. }
  9761. if (!this.$('img')) {
  9762. this.el_.appendChild(createEl('picture', {
  9763. className: 'vjs-poster',
  9764. // Don't want poster to be tabbable.
  9765. tabIndex: -1
  9766. }, {}, createEl('img', {
  9767. loading: 'lazy',
  9768. crossOrigin: this.crossOrigin()
  9769. }, {
  9770. alt: ''
  9771. })));
  9772. }
  9773. this.$('img').src = url;
  9774. }
  9775. /**
  9776. * An {@link EventTarget~EventListener} for clicks on the `PosterImage`. See
  9777. * {@link ClickableComponent#handleClick} for instances where this will be triggered.
  9778. *
  9779. * @listens tap
  9780. * @listens click
  9781. * @listens keydown
  9782. *
  9783. * @param {Event} event
  9784. + The `click`, `tap` or `keydown` event that caused this function to be called.
  9785. */
  9786. handleClick(event) {
  9787. // We don't want a click to trigger playback when controls are disabled
  9788. if (!this.player_.controls()) {
  9789. return;
  9790. }
  9791. if (this.player_.tech(true)) {
  9792. this.player_.tech(true).focus();
  9793. }
  9794. if (this.player_.paused()) {
  9795. silencePromise(this.player_.play());
  9796. } else {
  9797. this.player_.pause();
  9798. }
  9799. }
  9800. }
  9801. /**
  9802. * Get or set the `PosterImage`'s crossorigin option. For the HTML5 player, this
  9803. * sets the `crossOrigin` property on the `<img>` tag to control the CORS
  9804. * behavior.
  9805. *
  9806. * @param {string|null} [value]
  9807. * The value to set the `PosterImages`'s crossorigin to. If an argument is
  9808. * given, must be one of `anonymous` or `use-credentials`.
  9809. *
  9810. * @return {string|null|undefined}
  9811. * - The current crossorigin value of the `Player` when getting.
  9812. * - undefined when setting
  9813. */
  9814. PosterImage.prototype.crossorigin = PosterImage.prototype.crossOrigin;
  9815. Component.registerComponent('PosterImage', PosterImage);
  9816. /**
  9817. * @file text-track-display.js
  9818. */
  9819. const darkGray = '#222';
  9820. const lightGray = '#ccc';
  9821. const fontMap = {
  9822. monospace: 'monospace',
  9823. sansSerif: 'sans-serif',
  9824. serif: 'serif',
  9825. monospaceSansSerif: '"Andale Mono", "Lucida Console", monospace',
  9826. monospaceSerif: '"Courier New", monospace',
  9827. proportionalSansSerif: 'sans-serif',
  9828. proportionalSerif: 'serif',
  9829. casual: '"Comic Sans MS", Impact, fantasy',
  9830. script: '"Monotype Corsiva", cursive',
  9831. smallcaps: '"Andale Mono", "Lucida Console", monospace, sans-serif'
  9832. };
  9833. /**
  9834. * Construct an rgba color from a given hex color code.
  9835. *
  9836. * @param {number} color
  9837. * Hex number for color, like #f0e or #f604e2.
  9838. *
  9839. * @param {number} opacity
  9840. * Value for opacity, 0.0 - 1.0.
  9841. *
  9842. * @return {string}
  9843. * The rgba color that was created, like 'rgba(255, 0, 0, 0.3)'.
  9844. */
  9845. function constructColor(color, opacity) {
  9846. let hex;
  9847. if (color.length === 4) {
  9848. // color looks like "#f0e"
  9849. hex = color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
  9850. } else if (color.length === 7) {
  9851. // color looks like "#f604e2"
  9852. hex = color.slice(1);
  9853. } else {
  9854. throw new Error('Invalid color code provided, ' + color + '; must be formatted as e.g. #f0e or #f604e2.');
  9855. }
  9856. return 'rgba(' + parseInt(hex.slice(0, 2), 16) + ',' + parseInt(hex.slice(2, 4), 16) + ',' + parseInt(hex.slice(4, 6), 16) + ',' + opacity + ')';
  9857. }
  9858. /**
  9859. * Try to update the style of a DOM element. Some style changes will throw an error,
  9860. * particularly in IE8. Those should be noops.
  9861. *
  9862. * @param {Element} el
  9863. * The DOM element to be styled.
  9864. *
  9865. * @param {string} style
  9866. * The CSS property on the element that should be styled.
  9867. *
  9868. * @param {string} rule
  9869. * The style rule that should be applied to the property.
  9870. *
  9871. * @private
  9872. */
  9873. function tryUpdateStyle(el, style, rule) {
  9874. try {
  9875. el.style[style] = rule;
  9876. } catch (e) {
  9877. // Satisfies linter.
  9878. return;
  9879. }
  9880. }
  9881. /**
  9882. * The component for displaying text track cues.
  9883. *
  9884. * @extends Component
  9885. */
  9886. class TextTrackDisplay extends Component {
  9887. /**
  9888. * Creates an instance of this class.
  9889. *
  9890. * @param { import('../player').default } player
  9891. * The `Player` that this class should be attached to.
  9892. *
  9893. * @param {Object} [options]
  9894. * The key/value store of player options.
  9895. *
  9896. * @param {Function} [ready]
  9897. * The function to call when `TextTrackDisplay` is ready.
  9898. */
  9899. constructor(player, options, ready) {
  9900. super(player, options, ready);
  9901. const updateDisplayHandler = e => this.updateDisplay(e);
  9902. player.on('loadstart', e => this.toggleDisplay(e));
  9903. player.on('texttrackchange', updateDisplayHandler);
  9904. player.on('loadedmetadata', e => this.preselectTrack(e));
  9905. // This used to be called during player init, but was causing an error
  9906. // if a track should show by default and the display hadn't loaded yet.
  9907. // Should probably be moved to an external track loader when we support
  9908. // tracks that don't need a display.
  9909. player.ready(bind_(this, function () {
  9910. if (player.tech_ && player.tech_.featuresNativeTextTracks) {
  9911. this.hide();
  9912. return;
  9913. }
  9914. player.on('fullscreenchange', updateDisplayHandler);
  9915. player.on('playerresize', updateDisplayHandler);
  9916. const screenOrientation = window.screen.orientation || window;
  9917. const changeOrientationEvent = window.screen.orientation ? 'change' : 'orientationchange';
  9918. screenOrientation.addEventListener(changeOrientationEvent, updateDisplayHandler);
  9919. player.on('dispose', () => screenOrientation.removeEventListener(changeOrientationEvent, updateDisplayHandler));
  9920. const tracks = this.options_.playerOptions.tracks || [];
  9921. for (let i = 0; i < tracks.length; i++) {
  9922. this.player_.addRemoteTextTrack(tracks[i], true);
  9923. }
  9924. this.preselectTrack();
  9925. }));
  9926. }
  9927. /**
  9928. * Preselect a track following this precedence:
  9929. * - matches the previously selected {@link TextTrack}'s language and kind
  9930. * - matches the previously selected {@link TextTrack}'s language only
  9931. * - is the first default captions track
  9932. * - is the first default descriptions track
  9933. *
  9934. * @listens Player#loadstart
  9935. */
  9936. preselectTrack() {
  9937. const modes = {
  9938. captions: 1,
  9939. subtitles: 1
  9940. };
  9941. const trackList = this.player_.textTracks();
  9942. const userPref = this.player_.cache_.selectedLanguage;
  9943. let firstDesc;
  9944. let firstCaptions;
  9945. let preferredTrack;
  9946. for (let i = 0; i < trackList.length; i++) {
  9947. const track = trackList[i];
  9948. if (userPref && userPref.enabled && userPref.language && userPref.language === track.language && track.kind in modes) {
  9949. // Always choose the track that matches both language and kind
  9950. if (track.kind === userPref.kind) {
  9951. preferredTrack = track;
  9952. // or choose the first track that matches language
  9953. } else if (!preferredTrack) {
  9954. preferredTrack = track;
  9955. }
  9956. // clear everything if offTextTrackMenuItem was clicked
  9957. } else if (userPref && !userPref.enabled) {
  9958. preferredTrack = null;
  9959. firstDesc = null;
  9960. firstCaptions = null;
  9961. } else if (track.default) {
  9962. if (track.kind === 'descriptions' && !firstDesc) {
  9963. firstDesc = track;
  9964. } else if (track.kind in modes && !firstCaptions) {
  9965. firstCaptions = track;
  9966. }
  9967. }
  9968. }
  9969. // The preferredTrack matches the user preference and takes
  9970. // precedence over all the other tracks.
  9971. // So, display the preferredTrack before the first default track
  9972. // and the subtitles/captions track before the descriptions track
  9973. if (preferredTrack) {
  9974. preferredTrack.mode = 'showing';
  9975. } else if (firstCaptions) {
  9976. firstCaptions.mode = 'showing';
  9977. } else if (firstDesc) {
  9978. firstDesc.mode = 'showing';
  9979. }
  9980. }
  9981. /**
  9982. * Turn display of {@link TextTrack}'s from the current state into the other state.
  9983. * There are only two states:
  9984. * - 'shown'
  9985. * - 'hidden'
  9986. *
  9987. * @listens Player#loadstart
  9988. */
  9989. toggleDisplay() {
  9990. if (this.player_.tech_ && this.player_.tech_.featuresNativeTextTracks) {
  9991. this.hide();
  9992. } else {
  9993. this.show();
  9994. }
  9995. }
  9996. /**
  9997. * Create the {@link Component}'s DOM element.
  9998. *
  9999. * @return {Element}
  10000. * The element that was created.
  10001. */
  10002. createEl() {
  10003. return super.createEl('div', {
  10004. className: 'vjs-text-track-display'
  10005. }, {
  10006. 'translate': 'yes',
  10007. 'aria-live': 'off',
  10008. 'aria-atomic': 'true'
  10009. });
  10010. }
  10011. /**
  10012. * Clear all displayed {@link TextTrack}s.
  10013. */
  10014. clearDisplay() {
  10015. if (typeof window.WebVTT === 'function') {
  10016. window.WebVTT.processCues(window, [], this.el_);
  10017. }
  10018. }
  10019. /**
  10020. * Update the displayed TextTrack when a either a {@link Player#texttrackchange} or
  10021. * a {@link Player#fullscreenchange} is fired.
  10022. *
  10023. * @listens Player#texttrackchange
  10024. * @listens Player#fullscreenchange
  10025. */
  10026. updateDisplay() {
  10027. const tracks = this.player_.textTracks();
  10028. const allowMultipleShowingTracks = this.options_.allowMultipleShowingTracks;
  10029. this.clearDisplay();
  10030. if (allowMultipleShowingTracks) {
  10031. const showingTracks = [];
  10032. for (let i = 0; i < tracks.length; ++i) {
  10033. const track = tracks[i];
  10034. if (track.mode !== 'showing') {
  10035. continue;
  10036. }
  10037. showingTracks.push(track);
  10038. }
  10039. this.updateForTrack(showingTracks);
  10040. return;
  10041. }
  10042. // Track display prioritization model: if multiple tracks are 'showing',
  10043. // display the first 'subtitles' or 'captions' track which is 'showing',
  10044. // otherwise display the first 'descriptions' track which is 'showing'
  10045. let descriptionsTrack = null;
  10046. let captionsSubtitlesTrack = null;
  10047. let i = tracks.length;
  10048. while (i--) {
  10049. const track = tracks[i];
  10050. if (track.mode === 'showing') {
  10051. if (track.kind === 'descriptions') {
  10052. descriptionsTrack = track;
  10053. } else {
  10054. captionsSubtitlesTrack = track;
  10055. }
  10056. }
  10057. }
  10058. if (captionsSubtitlesTrack) {
  10059. if (this.getAttribute('aria-live') !== 'off') {
  10060. this.setAttribute('aria-live', 'off');
  10061. }
  10062. this.updateForTrack(captionsSubtitlesTrack);
  10063. } else if (descriptionsTrack) {
  10064. if (this.getAttribute('aria-live') !== 'assertive') {
  10065. this.setAttribute('aria-live', 'assertive');
  10066. }
  10067. this.updateForTrack(descriptionsTrack);
  10068. }
  10069. }
  10070. /**
  10071. * Style {@Link TextTrack} activeCues according to {@Link TextTrackSettings}.
  10072. *
  10073. * @param {TextTrack} track
  10074. * Text track object containing active cues to style.
  10075. */
  10076. updateDisplayState(track) {
  10077. const overrides = this.player_.textTrackSettings.getValues();
  10078. const cues = track.activeCues;
  10079. let i = cues.length;
  10080. while (i--) {
  10081. const cue = cues[i];
  10082. if (!cue) {
  10083. continue;
  10084. }
  10085. const cueDiv = cue.displayState;
  10086. if (overrides.color) {
  10087. cueDiv.firstChild.style.color = overrides.color;
  10088. }
  10089. if (overrides.textOpacity) {
  10090. tryUpdateStyle(cueDiv.firstChild, 'color', constructColor(overrides.color || '#fff', overrides.textOpacity));
  10091. }
  10092. if (overrides.backgroundColor) {
  10093. cueDiv.firstChild.style.backgroundColor = overrides.backgroundColor;
  10094. }
  10095. if (overrides.backgroundOpacity) {
  10096. tryUpdateStyle(cueDiv.firstChild, 'backgroundColor', constructColor(overrides.backgroundColor || '#000', overrides.backgroundOpacity));
  10097. }
  10098. if (overrides.windowColor) {
  10099. if (overrides.windowOpacity) {
  10100. tryUpdateStyle(cueDiv, 'backgroundColor', constructColor(overrides.windowColor, overrides.windowOpacity));
  10101. } else {
  10102. cueDiv.style.backgroundColor = overrides.windowColor;
  10103. }
  10104. }
  10105. if (overrides.edgeStyle) {
  10106. if (overrides.edgeStyle === 'dropshadow') {
  10107. cueDiv.firstChild.style.textShadow = `2px 2px 3px ${darkGray}, 2px 2px 4px ${darkGray}, 2px 2px 5px ${darkGray}`;
  10108. } else if (overrides.edgeStyle === 'raised') {
  10109. cueDiv.firstChild.style.textShadow = `1px 1px ${darkGray}, 2px 2px ${darkGray}, 3px 3px ${darkGray}`;
  10110. } else if (overrides.edgeStyle === 'depressed') {
  10111. cueDiv.firstChild.style.textShadow = `1px 1px ${lightGray}, 0 1px ${lightGray}, -1px -1px ${darkGray}, 0 -1px ${darkGray}`;
  10112. } else if (overrides.edgeStyle === 'uniform') {
  10113. cueDiv.firstChild.style.textShadow = `0 0 4px ${darkGray}, 0 0 4px ${darkGray}, 0 0 4px ${darkGray}, 0 0 4px ${darkGray}`;
  10114. }
  10115. }
  10116. if (overrides.fontPercent && overrides.fontPercent !== 1) {
  10117. const fontSize = window.parseFloat(cueDiv.style.fontSize);
  10118. cueDiv.style.fontSize = fontSize * overrides.fontPercent + 'px';
  10119. cueDiv.style.height = 'auto';
  10120. cueDiv.style.top = 'auto';
  10121. }
  10122. if (overrides.fontFamily && overrides.fontFamily !== 'default') {
  10123. if (overrides.fontFamily === 'small-caps') {
  10124. cueDiv.firstChild.style.fontVariant = 'small-caps';
  10125. } else {
  10126. cueDiv.firstChild.style.fontFamily = fontMap[overrides.fontFamily];
  10127. }
  10128. }
  10129. }
  10130. }
  10131. /**
  10132. * Add an {@link TextTrack} to to the {@link Tech}s {@link TextTrackList}.
  10133. *
  10134. * @param {TextTrack|TextTrack[]} tracks
  10135. * Text track object or text track array to be added to the list.
  10136. */
  10137. updateForTrack(tracks) {
  10138. if (!Array.isArray(tracks)) {
  10139. tracks = [tracks];
  10140. }
  10141. if (typeof window.WebVTT !== 'function' || tracks.every(track => {
  10142. return !track.activeCues;
  10143. })) {
  10144. return;
  10145. }
  10146. const cues = [];
  10147. // push all active track cues
  10148. for (let i = 0; i < tracks.length; ++i) {
  10149. const track = tracks[i];
  10150. for (let j = 0; j < track.activeCues.length; ++j) {
  10151. cues.push(track.activeCues[j]);
  10152. }
  10153. }
  10154. // removes all cues before it processes new ones
  10155. window.WebVTT.processCues(window, cues, this.el_);
  10156. // add unique class to each language text track & add settings styling if necessary
  10157. for (let i = 0; i < tracks.length; ++i) {
  10158. const track = tracks[i];
  10159. for (let j = 0; j < track.activeCues.length; ++j) {
  10160. const cueEl = track.activeCues[j].displayState;
  10161. addClass(cueEl, 'vjs-text-track-cue', 'vjs-text-track-cue-' + (track.language ? track.language : i));
  10162. if (track.language) {
  10163. setAttribute(cueEl, 'lang', track.language);
  10164. }
  10165. }
  10166. if (this.player_.textTrackSettings) {
  10167. this.updateDisplayState(track);
  10168. }
  10169. }
  10170. }
  10171. }
  10172. Component.registerComponent('TextTrackDisplay', TextTrackDisplay);
  10173. /**
  10174. * @file loading-spinner.js
  10175. */
  10176. /**
  10177. * A loading spinner for use during waiting/loading events.
  10178. *
  10179. * @extends Component
  10180. */
  10181. class LoadingSpinner extends Component {
  10182. /**
  10183. * Create the `LoadingSpinner`s DOM element.
  10184. *
  10185. * @return {Element}
  10186. * The dom element that gets created.
  10187. */
  10188. createEl() {
  10189. const isAudio = this.player_.isAudio();
  10190. const playerType = this.localize(isAudio ? 'Audio Player' : 'Video Player');
  10191. const controlText = createEl('span', {
  10192. className: 'vjs-control-text',
  10193. textContent: this.localize('{1} is loading.', [playerType])
  10194. });
  10195. const el = super.createEl('div', {
  10196. className: 'vjs-loading-spinner',
  10197. dir: 'ltr'
  10198. });
  10199. el.appendChild(controlText);
  10200. return el;
  10201. }
  10202. /**
  10203. * Update control text on languagechange
  10204. */
  10205. handleLanguagechange() {
  10206. this.$('.vjs-control-text').textContent = this.localize('{1} is loading.', [this.player_.isAudio() ? 'Audio Player' : 'Video Player']);
  10207. }
  10208. }
  10209. Component.registerComponent('LoadingSpinner', LoadingSpinner);
  10210. /**
  10211. * @file button.js
  10212. */
  10213. /**
  10214. * Base class for all buttons.
  10215. *
  10216. * @extends ClickableComponent
  10217. */
  10218. class Button extends ClickableComponent {
  10219. /**
  10220. * Create the `Button`s DOM element.
  10221. *
  10222. * @param {string} [tag="button"]
  10223. * The element's node type. This argument is IGNORED: no matter what
  10224. * is passed, it will always create a `button` element.
  10225. *
  10226. * @param {Object} [props={}]
  10227. * An object of properties that should be set on the element.
  10228. *
  10229. * @param {Object} [attributes={}]
  10230. * An object of attributes that should be set on the element.
  10231. *
  10232. * @return {Element}
  10233. * The element that gets created.
  10234. */
  10235. createEl(tag, props = {}, attributes = {}) {
  10236. tag = 'button';
  10237. props = Object.assign({
  10238. className: this.buildCSSClass()
  10239. }, props);
  10240. // Add attributes for button element
  10241. attributes = Object.assign({
  10242. // Necessary since the default button type is "submit"
  10243. type: 'button'
  10244. }, attributes);
  10245. const el = createEl(tag, props, attributes);
  10246. el.appendChild(createEl('span', {
  10247. className: 'vjs-icon-placeholder'
  10248. }, {
  10249. 'aria-hidden': true
  10250. }));
  10251. this.createControlTextEl(el);
  10252. return el;
  10253. }
  10254. /**
  10255. * Add a child `Component` inside of this `Button`.
  10256. *
  10257. * @param {string|Component} child
  10258. * The name or instance of a child to add.
  10259. *
  10260. * @param {Object} [options={}]
  10261. * The key/value store of options that will get passed to children of
  10262. * the child.
  10263. *
  10264. * @return {Component}
  10265. * The `Component` that gets added as a child. When using a string the
  10266. * `Component` will get created by this process.
  10267. *
  10268. * @deprecated since version 5
  10269. */
  10270. addChild(child, options = {}) {
  10271. const className = this.constructor.name;
  10272. log.warn(`Adding an actionable (user controllable) child to a Button (${className}) is not supported; use a ClickableComponent instead.`);
  10273. // Avoid the error message generated by ClickableComponent's addChild method
  10274. return Component.prototype.addChild.call(this, child, options);
  10275. }
  10276. /**
  10277. * Enable the `Button` element so that it can be activated or clicked. Use this with
  10278. * {@link Button#disable}.
  10279. */
  10280. enable() {
  10281. super.enable();
  10282. this.el_.removeAttribute('disabled');
  10283. }
  10284. /**
  10285. * Disable the `Button` element so that it cannot be activated or clicked. Use this with
  10286. * {@link Button#enable}.
  10287. */
  10288. disable() {
  10289. super.disable();
  10290. this.el_.setAttribute('disabled', 'disabled');
  10291. }
  10292. /**
  10293. * This gets called when a `Button` has focus and `keydown` is triggered via a key
  10294. * press.
  10295. *
  10296. * @param {Event} event
  10297. * The event that caused this function to get called.
  10298. *
  10299. * @listens keydown
  10300. */
  10301. handleKeyDown(event) {
  10302. // Ignore Space or Enter key operation, which is handled by the browser for
  10303. // a button - though not for its super class, ClickableComponent. Also,
  10304. // prevent the event from propagating through the DOM and triggering Player
  10305. // hotkeys. We do not preventDefault here because we _want_ the browser to
  10306. // handle it.
  10307. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  10308. event.stopPropagation();
  10309. return;
  10310. }
  10311. // Pass keypress handling up for unsupported keys
  10312. super.handleKeyDown(event);
  10313. }
  10314. }
  10315. Component.registerComponent('Button', Button);
  10316. /**
  10317. * @file big-play-button.js
  10318. */
  10319. /**
  10320. * The initial play button that shows before the video has played. The hiding of the
  10321. * `BigPlayButton` get done via CSS and `Player` states.
  10322. *
  10323. * @extends Button
  10324. */
  10325. class BigPlayButton extends Button {
  10326. constructor(player, options) {
  10327. super(player, options);
  10328. this.mouseused_ = false;
  10329. this.on('mousedown', e => this.handleMouseDown(e));
  10330. }
  10331. /**
  10332. * Builds the default DOM `className`.
  10333. *
  10334. * @return {string}
  10335. * The DOM `className` for this object. Always returns 'vjs-big-play-button'.
  10336. */
  10337. buildCSSClass() {
  10338. return 'vjs-big-play-button';
  10339. }
  10340. /**
  10341. * This gets called when a `BigPlayButton` "clicked". See {@link ClickableComponent}
  10342. * for more detailed information on what a click can be.
  10343. *
  10344. * @param {KeyboardEvent} event
  10345. * The `keydown`, `tap`, or `click` event that caused this function to be
  10346. * called.
  10347. *
  10348. * @listens tap
  10349. * @listens click
  10350. */
  10351. handleClick(event) {
  10352. const playPromise = this.player_.play();
  10353. // exit early if clicked via the mouse
  10354. if (this.mouseused_ && event.clientX && event.clientY) {
  10355. silencePromise(playPromise);
  10356. if (this.player_.tech(true)) {
  10357. this.player_.tech(true).focus();
  10358. }
  10359. return;
  10360. }
  10361. const cb = this.player_.getChild('controlBar');
  10362. const playToggle = cb && cb.getChild('playToggle');
  10363. if (!playToggle) {
  10364. this.player_.tech(true).focus();
  10365. return;
  10366. }
  10367. const playFocus = () => playToggle.focus();
  10368. if (isPromise(playPromise)) {
  10369. playPromise.then(playFocus, () => {});
  10370. } else {
  10371. this.setTimeout(playFocus, 1);
  10372. }
  10373. }
  10374. handleKeyDown(event) {
  10375. this.mouseused_ = false;
  10376. super.handleKeyDown(event);
  10377. }
  10378. handleMouseDown(event) {
  10379. this.mouseused_ = true;
  10380. }
  10381. }
  10382. /**
  10383. * The text that should display over the `BigPlayButton`s controls. Added to for localization.
  10384. *
  10385. * @type {string}
  10386. * @protected
  10387. */
  10388. BigPlayButton.prototype.controlText_ = 'Play Video';
  10389. Component.registerComponent('BigPlayButton', BigPlayButton);
  10390. /**
  10391. * @file close-button.js
  10392. */
  10393. /**
  10394. * The `CloseButton` is a `{@link Button}` that fires a `close` event when
  10395. * it gets clicked.
  10396. *
  10397. * @extends Button
  10398. */
  10399. class CloseButton extends Button {
  10400. /**
  10401. * Creates an instance of the this class.
  10402. *
  10403. * @param { import('./player').default } player
  10404. * The `Player` that this class should be attached to.
  10405. *
  10406. * @param {Object} [options]
  10407. * The key/value store of player options.
  10408. */
  10409. constructor(player, options) {
  10410. super(player, options);
  10411. this.controlText(options && options.controlText || this.localize('Close'));
  10412. }
  10413. /**
  10414. * Builds the default DOM `className`.
  10415. *
  10416. * @return {string}
  10417. * The DOM `className` for this object.
  10418. */
  10419. buildCSSClass() {
  10420. return `vjs-close-button ${super.buildCSSClass()}`;
  10421. }
  10422. /**
  10423. * This gets called when a `CloseButton` gets clicked. See
  10424. * {@link ClickableComponent#handleClick} for more information on when
  10425. * this will be triggered
  10426. *
  10427. * @param {Event} event
  10428. * The `keydown`, `tap`, or `click` event that caused this function to be
  10429. * called.
  10430. *
  10431. * @listens tap
  10432. * @listens click
  10433. * @fires CloseButton#close
  10434. */
  10435. handleClick(event) {
  10436. /**
  10437. * Triggered when the a `CloseButton` is clicked.
  10438. *
  10439. * @event CloseButton#close
  10440. * @type {Event}
  10441. *
  10442. * @property {boolean} [bubbles=false]
  10443. * set to false so that the close event does not
  10444. * bubble up to parents if there is no listener
  10445. */
  10446. this.trigger({
  10447. type: 'close',
  10448. bubbles: false
  10449. });
  10450. }
  10451. /**
  10452. * Event handler that is called when a `CloseButton` receives a
  10453. * `keydown` event.
  10454. *
  10455. * By default, if the key is Esc, it will trigger a `click` event.
  10456. *
  10457. * @param {Event} event
  10458. * The `keydown` event that caused this function to be called.
  10459. *
  10460. * @listens keydown
  10461. */
  10462. handleKeyDown(event) {
  10463. // Esc button will trigger `click` event
  10464. if (keycode.isEventKey(event, 'Esc')) {
  10465. event.preventDefault();
  10466. event.stopPropagation();
  10467. this.trigger('click');
  10468. } else {
  10469. // Pass keypress handling up for unsupported keys
  10470. super.handleKeyDown(event);
  10471. }
  10472. }
  10473. }
  10474. Component.registerComponent('CloseButton', CloseButton);
  10475. /**
  10476. * @file play-toggle.js
  10477. */
  10478. /**
  10479. * Button to toggle between play and pause.
  10480. *
  10481. * @extends Button
  10482. */
  10483. class PlayToggle extends Button {
  10484. /**
  10485. * Creates an instance of this class.
  10486. *
  10487. * @param { import('./player').default } player
  10488. * The `Player` that this class should be attached to.
  10489. *
  10490. * @param {Object} [options={}]
  10491. * The key/value store of player options.
  10492. */
  10493. constructor(player, options = {}) {
  10494. super(player, options);
  10495. // show or hide replay icon
  10496. options.replay = options.replay === undefined || options.replay;
  10497. this.on(player, 'play', e => this.handlePlay(e));
  10498. this.on(player, 'pause', e => this.handlePause(e));
  10499. if (options.replay) {
  10500. this.on(player, 'ended', e => this.handleEnded(e));
  10501. }
  10502. }
  10503. /**
  10504. * Builds the default DOM `className`.
  10505. *
  10506. * @return {string}
  10507. * The DOM `className` for this object.
  10508. */
  10509. buildCSSClass() {
  10510. return `vjs-play-control ${super.buildCSSClass()}`;
  10511. }
  10512. /**
  10513. * This gets called when an `PlayToggle` is "clicked". See
  10514. * {@link ClickableComponent} for more detailed information on what a click can be.
  10515. *
  10516. * @param {Event} [event]
  10517. * The `keydown`, `tap`, or `click` event that caused this function to be
  10518. * called.
  10519. *
  10520. * @listens tap
  10521. * @listens click
  10522. */
  10523. handleClick(event) {
  10524. if (this.player_.paused()) {
  10525. silencePromise(this.player_.play());
  10526. } else {
  10527. this.player_.pause();
  10528. }
  10529. }
  10530. /**
  10531. * This gets called once after the video has ended and the user seeks so that
  10532. * we can change the replay button back to a play button.
  10533. *
  10534. * @param {Event} [event]
  10535. * The event that caused this function to run.
  10536. *
  10537. * @listens Player#seeked
  10538. */
  10539. handleSeeked(event) {
  10540. this.removeClass('vjs-ended');
  10541. if (this.player_.paused()) {
  10542. this.handlePause(event);
  10543. } else {
  10544. this.handlePlay(event);
  10545. }
  10546. }
  10547. /**
  10548. * Add the vjs-playing class to the element so it can change appearance.
  10549. *
  10550. * @param {Event} [event]
  10551. * The event that caused this function to run.
  10552. *
  10553. * @listens Player#play
  10554. */
  10555. handlePlay(event) {
  10556. this.removeClass('vjs-ended', 'vjs-paused');
  10557. this.addClass('vjs-playing');
  10558. // change the button text to "Pause"
  10559. this.controlText('Pause');
  10560. }
  10561. /**
  10562. * Add the vjs-paused class to the element so it can change appearance.
  10563. *
  10564. * @param {Event} [event]
  10565. * The event that caused this function to run.
  10566. *
  10567. * @listens Player#pause
  10568. */
  10569. handlePause(event) {
  10570. this.removeClass('vjs-playing');
  10571. this.addClass('vjs-paused');
  10572. // change the button text to "Play"
  10573. this.controlText('Play');
  10574. }
  10575. /**
  10576. * Add the vjs-ended class to the element so it can change appearance
  10577. *
  10578. * @param {Event} [event]
  10579. * The event that caused this function to run.
  10580. *
  10581. * @listens Player#ended
  10582. */
  10583. handleEnded(event) {
  10584. this.removeClass('vjs-playing');
  10585. this.addClass('vjs-ended');
  10586. // change the button text to "Replay"
  10587. this.controlText('Replay');
  10588. // on the next seek remove the replay button
  10589. this.one(this.player_, 'seeked', e => this.handleSeeked(e));
  10590. }
  10591. }
  10592. /**
  10593. * The text that should display over the `PlayToggle`s controls. Added for localization.
  10594. *
  10595. * @type {string}
  10596. * @protected
  10597. */
  10598. PlayToggle.prototype.controlText_ = 'Play';
  10599. Component.registerComponent('PlayToggle', PlayToggle);
  10600. /**
  10601. * @file time-display.js
  10602. */
  10603. /**
  10604. * Displays time information about the video
  10605. *
  10606. * @extends Component
  10607. */
  10608. class TimeDisplay extends Component {
  10609. /**
  10610. * Creates an instance of this class.
  10611. *
  10612. * @param { import('../../player').default } player
  10613. * The `Player` that this class should be attached to.
  10614. *
  10615. * @param {Object} [options]
  10616. * The key/value store of player options.
  10617. */
  10618. constructor(player, options) {
  10619. super(player, options);
  10620. this.on(player, ['timeupdate', 'ended'], e => this.updateContent(e));
  10621. this.updateTextNode_();
  10622. }
  10623. /**
  10624. * Create the `Component`'s DOM element
  10625. *
  10626. * @return {Element}
  10627. * The element that was created.
  10628. */
  10629. createEl() {
  10630. const className = this.buildCSSClass();
  10631. const el = super.createEl('div', {
  10632. className: `${className} vjs-time-control vjs-control`
  10633. });
  10634. const span = createEl('span', {
  10635. className: 'vjs-control-text',
  10636. textContent: `${this.localize(this.labelText_)}\u00a0`
  10637. }, {
  10638. role: 'presentation'
  10639. });
  10640. el.appendChild(span);
  10641. this.contentEl_ = createEl('span', {
  10642. className: `${className}-display`
  10643. }, {
  10644. // span elements have no implicit role, but some screen readers (notably VoiceOver)
  10645. // treat them as a break between items in the DOM when using arrow keys
  10646. // (or left-to-right swipes on iOS) to read contents of a page. Using
  10647. // role='presentation' causes VoiceOver to NOT treat this span as a break.
  10648. role: 'presentation'
  10649. });
  10650. el.appendChild(this.contentEl_);
  10651. return el;
  10652. }
  10653. dispose() {
  10654. this.contentEl_ = null;
  10655. this.textNode_ = null;
  10656. super.dispose();
  10657. }
  10658. /**
  10659. * Updates the time display text node with a new time
  10660. *
  10661. * @param {number} [time=0] the time to update to
  10662. *
  10663. * @private
  10664. */
  10665. updateTextNode_(time = 0) {
  10666. time = formatTime(time);
  10667. if (this.formattedTime_ === time) {
  10668. return;
  10669. }
  10670. this.formattedTime_ = time;
  10671. this.requestNamedAnimationFrame('TimeDisplay#updateTextNode_', () => {
  10672. if (!this.contentEl_) {
  10673. return;
  10674. }
  10675. let oldNode = this.textNode_;
  10676. if (oldNode && this.contentEl_.firstChild !== oldNode) {
  10677. oldNode = null;
  10678. 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.');
  10679. }
  10680. this.textNode_ = document.createTextNode(this.formattedTime_);
  10681. if (!this.textNode_) {
  10682. return;
  10683. }
  10684. if (oldNode) {
  10685. this.contentEl_.replaceChild(this.textNode_, oldNode);
  10686. } else {
  10687. this.contentEl_.appendChild(this.textNode_);
  10688. }
  10689. });
  10690. }
  10691. /**
  10692. * To be filled out in the child class, should update the displayed time
  10693. * in accordance with the fact that the current time has changed.
  10694. *
  10695. * @param {Event} [event]
  10696. * The `timeupdate` event that caused this to run.
  10697. *
  10698. * @listens Player#timeupdate
  10699. */
  10700. updateContent(event) {}
  10701. }
  10702. /**
  10703. * The text that is added to the `TimeDisplay` for screen reader users.
  10704. *
  10705. * @type {string}
  10706. * @private
  10707. */
  10708. TimeDisplay.prototype.labelText_ = 'Time';
  10709. /**
  10710. * The text that should display over the `TimeDisplay`s controls. Added to for localization.
  10711. *
  10712. * @type {string}
  10713. * @protected
  10714. *
  10715. * @deprecated in v7; controlText_ is not used in non-active display Components
  10716. */
  10717. TimeDisplay.prototype.controlText_ = 'Time';
  10718. Component.registerComponent('TimeDisplay', TimeDisplay);
  10719. /**
  10720. * @file current-time-display.js
  10721. */
  10722. /**
  10723. * Displays the current time
  10724. *
  10725. * @extends Component
  10726. */
  10727. class CurrentTimeDisplay extends TimeDisplay {
  10728. /**
  10729. * Builds the default DOM `className`.
  10730. *
  10731. * @return {string}
  10732. * The DOM `className` for this object.
  10733. */
  10734. buildCSSClass() {
  10735. return 'vjs-current-time';
  10736. }
  10737. /**
  10738. * Update current time display
  10739. *
  10740. * @param {Event} [event]
  10741. * The `timeupdate` event that caused this function to run.
  10742. *
  10743. * @listens Player#timeupdate
  10744. */
  10745. updateContent(event) {
  10746. // Allows for smooth scrubbing, when player can't keep up.
  10747. let time;
  10748. if (this.player_.ended()) {
  10749. time = this.player_.duration();
  10750. } else {
  10751. time = this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  10752. }
  10753. this.updateTextNode_(time);
  10754. }
  10755. }
  10756. /**
  10757. * The text that is added to the `CurrentTimeDisplay` for screen reader users.
  10758. *
  10759. * @type {string}
  10760. * @private
  10761. */
  10762. CurrentTimeDisplay.prototype.labelText_ = 'Current Time';
  10763. /**
  10764. * The text that should display over the `CurrentTimeDisplay`s controls. Added to for localization.
  10765. *
  10766. * @type {string}
  10767. * @protected
  10768. *
  10769. * @deprecated in v7; controlText_ is not used in non-active display Components
  10770. */
  10771. CurrentTimeDisplay.prototype.controlText_ = 'Current Time';
  10772. Component.registerComponent('CurrentTimeDisplay', CurrentTimeDisplay);
  10773. /**
  10774. * @file duration-display.js
  10775. */
  10776. /**
  10777. * Displays the duration
  10778. *
  10779. * @extends Component
  10780. */
  10781. class DurationDisplay extends TimeDisplay {
  10782. /**
  10783. * Creates an instance of this class.
  10784. *
  10785. * @param { import('../../player').default } player
  10786. * The `Player` that this class should be attached to.
  10787. *
  10788. * @param {Object} [options]
  10789. * The key/value store of player options.
  10790. */
  10791. constructor(player, options) {
  10792. super(player, options);
  10793. const updateContent = e => this.updateContent(e);
  10794. // we do not want to/need to throttle duration changes,
  10795. // as they should always display the changed duration as
  10796. // it has changed
  10797. this.on(player, 'durationchange', updateContent);
  10798. // Listen to loadstart because the player duration is reset when a new media element is loaded,
  10799. // but the durationchange on the user agent will not fire.
  10800. // @see [Spec]{@link https://www.w3.org/TR/2011/WD-html5-20110113/video.html#media-element-load-algorithm}
  10801. this.on(player, 'loadstart', updateContent);
  10802. // Also listen for timeupdate (in the parent) and loadedmetadata because removing those
  10803. // listeners could have broken dependent applications/libraries. These
  10804. // can likely be removed for 7.0.
  10805. this.on(player, 'loadedmetadata', updateContent);
  10806. }
  10807. /**
  10808. * Builds the default DOM `className`.
  10809. *
  10810. * @return {string}
  10811. * The DOM `className` for this object.
  10812. */
  10813. buildCSSClass() {
  10814. return 'vjs-duration';
  10815. }
  10816. /**
  10817. * Update duration time display.
  10818. *
  10819. * @param {Event} [event]
  10820. * The `durationchange`, `timeupdate`, or `loadedmetadata` event that caused
  10821. * this function to be called.
  10822. *
  10823. * @listens Player#durationchange
  10824. * @listens Player#timeupdate
  10825. * @listens Player#loadedmetadata
  10826. */
  10827. updateContent(event) {
  10828. const duration = this.player_.duration();
  10829. this.updateTextNode_(duration);
  10830. }
  10831. }
  10832. /**
  10833. * The text that is added to the `DurationDisplay` for screen reader users.
  10834. *
  10835. * @type {string}
  10836. * @private
  10837. */
  10838. DurationDisplay.prototype.labelText_ = 'Duration';
  10839. /**
  10840. * The text that should display over the `DurationDisplay`s controls. Added to for localization.
  10841. *
  10842. * @type {string}
  10843. * @protected
  10844. *
  10845. * @deprecated in v7; controlText_ is not used in non-active display Components
  10846. */
  10847. DurationDisplay.prototype.controlText_ = 'Duration';
  10848. Component.registerComponent('DurationDisplay', DurationDisplay);
  10849. /**
  10850. * @file time-divider.js
  10851. */
  10852. /**
  10853. * The separator between the current time and duration.
  10854. * Can be hidden if it's not needed in the design.
  10855. *
  10856. * @extends Component
  10857. */
  10858. class TimeDivider extends Component {
  10859. /**
  10860. * Create the component's DOM element
  10861. *
  10862. * @return {Element}
  10863. * The element that was created.
  10864. */
  10865. createEl() {
  10866. const el = super.createEl('div', {
  10867. className: 'vjs-time-control vjs-time-divider'
  10868. }, {
  10869. // this element and its contents can be hidden from assistive techs since
  10870. // it is made extraneous by the announcement of the control text
  10871. // for the current time and duration displays
  10872. 'aria-hidden': true
  10873. });
  10874. const div = super.createEl('div');
  10875. const span = super.createEl('span', {
  10876. textContent: '/'
  10877. });
  10878. div.appendChild(span);
  10879. el.appendChild(div);
  10880. return el;
  10881. }
  10882. }
  10883. Component.registerComponent('TimeDivider', TimeDivider);
  10884. /**
  10885. * @file remaining-time-display.js
  10886. */
  10887. /**
  10888. * Displays the time left in the video
  10889. *
  10890. * @extends Component
  10891. */
  10892. class RemainingTimeDisplay extends TimeDisplay {
  10893. /**
  10894. * Creates an instance of this class.
  10895. *
  10896. * @param { import('../../player').default } player
  10897. * The `Player` that this class should be attached to.
  10898. *
  10899. * @param {Object} [options]
  10900. * The key/value store of player options.
  10901. */
  10902. constructor(player, options) {
  10903. super(player, options);
  10904. this.on(player, 'durationchange', e => this.updateContent(e));
  10905. }
  10906. /**
  10907. * Builds the default DOM `className`.
  10908. *
  10909. * @return {string}
  10910. * The DOM `className` for this object.
  10911. */
  10912. buildCSSClass() {
  10913. return 'vjs-remaining-time';
  10914. }
  10915. /**
  10916. * Create the `Component`'s DOM element with the "minus" character prepend to the time
  10917. *
  10918. * @return {Element}
  10919. * The element that was created.
  10920. */
  10921. createEl() {
  10922. const el = super.createEl();
  10923. if (this.options_.displayNegative !== false) {
  10924. el.insertBefore(createEl('span', {}, {
  10925. 'aria-hidden': true
  10926. }, '-'), this.contentEl_);
  10927. }
  10928. return el;
  10929. }
  10930. /**
  10931. * Update remaining time display.
  10932. *
  10933. * @param {Event} [event]
  10934. * The `timeupdate` or `durationchange` event that caused this to run.
  10935. *
  10936. * @listens Player#timeupdate
  10937. * @listens Player#durationchange
  10938. */
  10939. updateContent(event) {
  10940. if (typeof this.player_.duration() !== 'number') {
  10941. return;
  10942. }
  10943. let time;
  10944. // @deprecated We should only use remainingTimeDisplay
  10945. // as of video.js 7
  10946. if (this.player_.ended()) {
  10947. time = 0;
  10948. } else if (this.player_.remainingTimeDisplay) {
  10949. time = this.player_.remainingTimeDisplay();
  10950. } else {
  10951. time = this.player_.remainingTime();
  10952. }
  10953. this.updateTextNode_(time);
  10954. }
  10955. }
  10956. /**
  10957. * The text that is added to the `RemainingTimeDisplay` for screen reader users.
  10958. *
  10959. * @type {string}
  10960. * @private
  10961. */
  10962. RemainingTimeDisplay.prototype.labelText_ = 'Remaining Time';
  10963. /**
  10964. * The text that should display over the `RemainingTimeDisplay`s controls. Added to for localization.
  10965. *
  10966. * @type {string}
  10967. * @protected
  10968. *
  10969. * @deprecated in v7; controlText_ is not used in non-active display Components
  10970. */
  10971. RemainingTimeDisplay.prototype.controlText_ = 'Remaining Time';
  10972. Component.registerComponent('RemainingTimeDisplay', RemainingTimeDisplay);
  10973. /**
  10974. * @file live-display.js
  10975. */
  10976. // TODO - Future make it click to snap to live
  10977. /**
  10978. * Displays the live indicator when duration is Infinity.
  10979. *
  10980. * @extends Component
  10981. */
  10982. class LiveDisplay extends Component {
  10983. /**
  10984. * Creates an instance of this class.
  10985. *
  10986. * @param { import('./player').default } player
  10987. * The `Player` that this class should be attached to.
  10988. *
  10989. * @param {Object} [options]
  10990. * The key/value store of player options.
  10991. */
  10992. constructor(player, options) {
  10993. super(player, options);
  10994. this.updateShowing();
  10995. this.on(this.player(), 'durationchange', e => this.updateShowing(e));
  10996. }
  10997. /**
  10998. * Create the `Component`'s DOM element
  10999. *
  11000. * @return {Element}
  11001. * The element that was created.
  11002. */
  11003. createEl() {
  11004. const el = super.createEl('div', {
  11005. className: 'vjs-live-control vjs-control'
  11006. });
  11007. this.contentEl_ = createEl('div', {
  11008. className: 'vjs-live-display'
  11009. }, {
  11010. 'aria-live': 'off'
  11011. });
  11012. this.contentEl_.appendChild(createEl('span', {
  11013. className: 'vjs-control-text',
  11014. textContent: `${this.localize('Stream Type')}\u00a0`
  11015. }));
  11016. this.contentEl_.appendChild(document.createTextNode(this.localize('LIVE')));
  11017. el.appendChild(this.contentEl_);
  11018. return el;
  11019. }
  11020. dispose() {
  11021. this.contentEl_ = null;
  11022. super.dispose();
  11023. }
  11024. /**
  11025. * Check the duration to see if the LiveDisplay should be showing or not. Then show/hide
  11026. * it accordingly
  11027. *
  11028. * @param {Event} [event]
  11029. * The {@link Player#durationchange} event that caused this function to run.
  11030. *
  11031. * @listens Player#durationchange
  11032. */
  11033. updateShowing(event) {
  11034. if (this.player().duration() === Infinity) {
  11035. this.show();
  11036. } else {
  11037. this.hide();
  11038. }
  11039. }
  11040. }
  11041. Component.registerComponent('LiveDisplay', LiveDisplay);
  11042. /**
  11043. * @file seek-to-live.js
  11044. */
  11045. /**
  11046. * Displays the live indicator when duration is Infinity.
  11047. *
  11048. * @extends Component
  11049. */
  11050. class SeekToLive extends Button {
  11051. /**
  11052. * Creates an instance of this class.
  11053. *
  11054. * @param { import('./player').default } player
  11055. * The `Player` that this class should be attached to.
  11056. *
  11057. * @param {Object} [options]
  11058. * The key/value store of player options.
  11059. */
  11060. constructor(player, options) {
  11061. super(player, options);
  11062. this.updateLiveEdgeStatus();
  11063. if (this.player_.liveTracker) {
  11064. this.updateLiveEdgeStatusHandler_ = e => this.updateLiveEdgeStatus(e);
  11065. this.on(this.player_.liveTracker, 'liveedgechange', this.updateLiveEdgeStatusHandler_);
  11066. }
  11067. }
  11068. /**
  11069. * Create the `Component`'s DOM element
  11070. *
  11071. * @return {Element}
  11072. * The element that was created.
  11073. */
  11074. createEl() {
  11075. const el = super.createEl('button', {
  11076. className: 'vjs-seek-to-live-control vjs-control'
  11077. });
  11078. this.textEl_ = createEl('span', {
  11079. className: 'vjs-seek-to-live-text',
  11080. textContent: this.localize('LIVE')
  11081. }, {
  11082. 'aria-hidden': 'true'
  11083. });
  11084. el.appendChild(this.textEl_);
  11085. return el;
  11086. }
  11087. /**
  11088. * Update the state of this button if we are at the live edge
  11089. * or not
  11090. */
  11091. updateLiveEdgeStatus() {
  11092. // default to live edge
  11093. if (!this.player_.liveTracker || this.player_.liveTracker.atLiveEdge()) {
  11094. this.setAttribute('aria-disabled', true);
  11095. this.addClass('vjs-at-live-edge');
  11096. this.controlText('Seek to live, currently playing live');
  11097. } else {
  11098. this.setAttribute('aria-disabled', false);
  11099. this.removeClass('vjs-at-live-edge');
  11100. this.controlText('Seek to live, currently behind live');
  11101. }
  11102. }
  11103. /**
  11104. * On click bring us as near to the live point as possible.
  11105. * This requires that we wait for the next `live-seekable-change`
  11106. * event which will happen every segment length seconds.
  11107. */
  11108. handleClick() {
  11109. this.player_.liveTracker.seekToLiveEdge();
  11110. }
  11111. /**
  11112. * Dispose of the element and stop tracking
  11113. */
  11114. dispose() {
  11115. if (this.player_.liveTracker) {
  11116. this.off(this.player_.liveTracker, 'liveedgechange', this.updateLiveEdgeStatusHandler_);
  11117. }
  11118. this.textEl_ = null;
  11119. super.dispose();
  11120. }
  11121. }
  11122. /**
  11123. * The text that should display over the `SeekToLive`s control. Added for localization.
  11124. *
  11125. * @type {string}
  11126. * @protected
  11127. */
  11128. SeekToLive.prototype.controlText_ = 'Seek to live, currently playing live';
  11129. Component.registerComponent('SeekToLive', SeekToLive);
  11130. /**
  11131. * @file num.js
  11132. * @module num
  11133. */
  11134. /**
  11135. * Keep a number between a min and a max value
  11136. *
  11137. * @param {number} number
  11138. * The number to clamp
  11139. *
  11140. * @param {number} min
  11141. * The minimum value
  11142. * @param {number} max
  11143. * The maximum value
  11144. *
  11145. * @return {number}
  11146. * the clamped number
  11147. */
  11148. function clamp(number, min, max) {
  11149. number = Number(number);
  11150. return Math.min(max, Math.max(min, isNaN(number) ? min : number));
  11151. }
  11152. var Num = /*#__PURE__*/Object.freeze({
  11153. __proto__: null,
  11154. clamp: clamp
  11155. });
  11156. /**
  11157. * @file slider.js
  11158. */
  11159. /**
  11160. * The base functionality for a slider. Can be vertical or horizontal.
  11161. * For instance the volume bar or the seek bar on a video is a slider.
  11162. *
  11163. * @extends Component
  11164. */
  11165. class Slider extends Component {
  11166. /**
  11167. * Create an instance of this class
  11168. *
  11169. * @param { import('../player').default } player
  11170. * The `Player` that this class should be attached to.
  11171. *
  11172. * @param {Object} [options]
  11173. * The key/value store of player options.
  11174. */
  11175. constructor(player, options) {
  11176. super(player, options);
  11177. this.handleMouseDown_ = e => this.handleMouseDown(e);
  11178. this.handleMouseUp_ = e => this.handleMouseUp(e);
  11179. this.handleKeyDown_ = e => this.handleKeyDown(e);
  11180. this.handleClick_ = e => this.handleClick(e);
  11181. this.handleMouseMove_ = e => this.handleMouseMove(e);
  11182. this.update_ = e => this.update(e);
  11183. // Set property names to bar to match with the child Slider class is looking for
  11184. this.bar = this.getChild(this.options_.barName);
  11185. // Set a horizontal or vertical class on the slider depending on the slider type
  11186. this.vertical(!!this.options_.vertical);
  11187. this.enable();
  11188. }
  11189. /**
  11190. * Are controls are currently enabled for this slider or not.
  11191. *
  11192. * @return {boolean}
  11193. * true if controls are enabled, false otherwise
  11194. */
  11195. enabled() {
  11196. return this.enabled_;
  11197. }
  11198. /**
  11199. * Enable controls for this slider if they are disabled
  11200. */
  11201. enable() {
  11202. if (this.enabled()) {
  11203. return;
  11204. }
  11205. this.on('mousedown', this.handleMouseDown_);
  11206. this.on('touchstart', this.handleMouseDown_);
  11207. this.on('keydown', this.handleKeyDown_);
  11208. this.on('click', this.handleClick_);
  11209. // TODO: deprecated, controlsvisible does not seem to be fired
  11210. this.on(this.player_, 'controlsvisible', this.update);
  11211. if (this.playerEvent) {
  11212. this.on(this.player_, this.playerEvent, this.update);
  11213. }
  11214. this.removeClass('disabled');
  11215. this.setAttribute('tabindex', 0);
  11216. this.enabled_ = true;
  11217. }
  11218. /**
  11219. * Disable controls for this slider if they are enabled
  11220. */
  11221. disable() {
  11222. if (!this.enabled()) {
  11223. return;
  11224. }
  11225. const doc = this.bar.el_.ownerDocument;
  11226. this.off('mousedown', this.handleMouseDown_);
  11227. this.off('touchstart', this.handleMouseDown_);
  11228. this.off('keydown', this.handleKeyDown_);
  11229. this.off('click', this.handleClick_);
  11230. this.off(this.player_, 'controlsvisible', this.update_);
  11231. this.off(doc, 'mousemove', this.handleMouseMove_);
  11232. this.off(doc, 'mouseup', this.handleMouseUp_);
  11233. this.off(doc, 'touchmove', this.handleMouseMove_);
  11234. this.off(doc, 'touchend', this.handleMouseUp_);
  11235. this.removeAttribute('tabindex');
  11236. this.addClass('disabled');
  11237. if (this.playerEvent) {
  11238. this.off(this.player_, this.playerEvent, this.update);
  11239. }
  11240. this.enabled_ = false;
  11241. }
  11242. /**
  11243. * Create the `Slider`s DOM element.
  11244. *
  11245. * @param {string} type
  11246. * Type of element to create.
  11247. *
  11248. * @param {Object} [props={}]
  11249. * List of properties in Object form.
  11250. *
  11251. * @param {Object} [attributes={}]
  11252. * list of attributes in Object form.
  11253. *
  11254. * @return {Element}
  11255. * The element that gets created.
  11256. */
  11257. createEl(type, props = {}, attributes = {}) {
  11258. // Add the slider element class to all sub classes
  11259. props.className = props.className + ' vjs-slider';
  11260. props = Object.assign({
  11261. tabIndex: 0
  11262. }, props);
  11263. attributes = Object.assign({
  11264. 'role': 'slider',
  11265. 'aria-valuenow': 0,
  11266. 'aria-valuemin': 0,
  11267. 'aria-valuemax': 100
  11268. }, attributes);
  11269. return super.createEl(type, props, attributes);
  11270. }
  11271. /**
  11272. * Handle `mousedown` or `touchstart` events on the `Slider`.
  11273. *
  11274. * @param {MouseEvent} event
  11275. * `mousedown` or `touchstart` event that triggered this function
  11276. *
  11277. * @listens mousedown
  11278. * @listens touchstart
  11279. * @fires Slider#slideractive
  11280. */
  11281. handleMouseDown(event) {
  11282. const doc = this.bar.el_.ownerDocument;
  11283. if (event.type === 'mousedown') {
  11284. event.preventDefault();
  11285. }
  11286. // Do not call preventDefault() on touchstart in Chrome
  11287. // to avoid console warnings. Use a 'touch-action: none' style
  11288. // instead to prevent unintended scrolling.
  11289. // https://developers.google.com/web/updates/2017/01/scrolling-intervention
  11290. if (event.type === 'touchstart' && !IS_CHROME) {
  11291. event.preventDefault();
  11292. }
  11293. blockTextSelection();
  11294. this.addClass('vjs-sliding');
  11295. /**
  11296. * Triggered when the slider is in an active state
  11297. *
  11298. * @event Slider#slideractive
  11299. * @type {MouseEvent}
  11300. */
  11301. this.trigger('slideractive');
  11302. this.on(doc, 'mousemove', this.handleMouseMove_);
  11303. this.on(doc, 'mouseup', this.handleMouseUp_);
  11304. this.on(doc, 'touchmove', this.handleMouseMove_);
  11305. this.on(doc, 'touchend', this.handleMouseUp_);
  11306. this.handleMouseMove(event, true);
  11307. }
  11308. /**
  11309. * Handle the `mousemove`, `touchmove`, and `mousedown` events on this `Slider`.
  11310. * The `mousemove` and `touchmove` events will only only trigger this function during
  11311. * `mousedown` and `touchstart`. This is due to {@link Slider#handleMouseDown} and
  11312. * {@link Slider#handleMouseUp}.
  11313. *
  11314. * @param {MouseEvent} event
  11315. * `mousedown`, `mousemove`, `touchstart`, or `touchmove` event that triggered
  11316. * this function
  11317. * @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.
  11318. *
  11319. * @listens mousemove
  11320. * @listens touchmove
  11321. */
  11322. handleMouseMove(event) {}
  11323. /**
  11324. * Handle `mouseup` or `touchend` events on the `Slider`.
  11325. *
  11326. * @param {MouseEvent} event
  11327. * `mouseup` or `touchend` event that triggered this function.
  11328. *
  11329. * @listens touchend
  11330. * @listens mouseup
  11331. * @fires Slider#sliderinactive
  11332. */
  11333. handleMouseUp(event) {
  11334. const doc = this.bar.el_.ownerDocument;
  11335. unblockTextSelection();
  11336. this.removeClass('vjs-sliding');
  11337. /**
  11338. * Triggered when the slider is no longer in an active state.
  11339. *
  11340. * @event Slider#sliderinactive
  11341. * @type {Event}
  11342. */
  11343. this.trigger('sliderinactive');
  11344. this.off(doc, 'mousemove', this.handleMouseMove_);
  11345. this.off(doc, 'mouseup', this.handleMouseUp_);
  11346. this.off(doc, 'touchmove', this.handleMouseMove_);
  11347. this.off(doc, 'touchend', this.handleMouseUp_);
  11348. this.update();
  11349. }
  11350. /**
  11351. * Update the progress bar of the `Slider`.
  11352. *
  11353. * @return {number}
  11354. * The percentage of progress the progress bar represents as a
  11355. * number from 0 to 1.
  11356. */
  11357. update() {
  11358. // In VolumeBar init we have a setTimeout for update that pops and update
  11359. // to the end of the execution stack. The player is destroyed before then
  11360. // update will cause an error
  11361. // If there's no bar...
  11362. if (!this.el_ || !this.bar) {
  11363. return;
  11364. }
  11365. // clamp progress between 0 and 1
  11366. // and only round to four decimal places, as we round to two below
  11367. const progress = this.getProgress();
  11368. if (progress === this.progress_) {
  11369. return progress;
  11370. }
  11371. this.progress_ = progress;
  11372. this.requestNamedAnimationFrame('Slider#update', () => {
  11373. // Set the new bar width or height
  11374. const sizeKey = this.vertical() ? 'height' : 'width';
  11375. // Convert to a percentage for css value
  11376. this.bar.el().style[sizeKey] = (progress * 100).toFixed(2) + '%';
  11377. });
  11378. return progress;
  11379. }
  11380. /**
  11381. * Get the percentage of the bar that should be filled
  11382. * but clamped and rounded.
  11383. *
  11384. * @return {number}
  11385. * percentage filled that the slider is
  11386. */
  11387. getProgress() {
  11388. return Number(clamp(this.getPercent(), 0, 1).toFixed(4));
  11389. }
  11390. /**
  11391. * Calculate distance for slider
  11392. *
  11393. * @param {Event} event
  11394. * The event that caused this function to run.
  11395. *
  11396. * @return {number}
  11397. * The current position of the Slider.
  11398. * - position.x for vertical `Slider`s
  11399. * - position.y for horizontal `Slider`s
  11400. */
  11401. calculateDistance(event) {
  11402. const position = getPointerPosition(this.el_, event);
  11403. if (this.vertical()) {
  11404. return position.y;
  11405. }
  11406. return position.x;
  11407. }
  11408. /**
  11409. * Handle a `keydown` event on the `Slider`. Watches for left, right, up, and down
  11410. * arrow keys. This function will only be called when the slider has focus. See
  11411. * {@link Slider#handleFocus} and {@link Slider#handleBlur}.
  11412. *
  11413. * @param {KeyboardEvent} event
  11414. * the `keydown` event that caused this function to run.
  11415. *
  11416. * @listens keydown
  11417. */
  11418. handleKeyDown(event) {
  11419. // Left and Down Arrows
  11420. if (keycode.isEventKey(event, 'Left') || keycode.isEventKey(event, 'Down')) {
  11421. event.preventDefault();
  11422. event.stopPropagation();
  11423. this.stepBack();
  11424. // Up and Right Arrows
  11425. } else if (keycode.isEventKey(event, 'Right') || keycode.isEventKey(event, 'Up')) {
  11426. event.preventDefault();
  11427. event.stopPropagation();
  11428. this.stepForward();
  11429. } else {
  11430. // Pass keydown handling up for unsupported keys
  11431. super.handleKeyDown(event);
  11432. }
  11433. }
  11434. /**
  11435. * Listener for click events on slider, used to prevent clicks
  11436. * from bubbling up to parent elements like button menus.
  11437. *
  11438. * @param {Object} event
  11439. * Event that caused this object to run
  11440. */
  11441. handleClick(event) {
  11442. event.stopPropagation();
  11443. event.preventDefault();
  11444. }
  11445. /**
  11446. * Get/set if slider is horizontal for vertical
  11447. *
  11448. * @param {boolean} [bool]
  11449. * - true if slider is vertical,
  11450. * - false is horizontal
  11451. *
  11452. * @return {boolean}
  11453. * - true if slider is vertical, and getting
  11454. * - false if the slider is horizontal, and getting
  11455. */
  11456. vertical(bool) {
  11457. if (bool === undefined) {
  11458. return this.vertical_ || false;
  11459. }
  11460. this.vertical_ = !!bool;
  11461. if (this.vertical_) {
  11462. this.addClass('vjs-slider-vertical');
  11463. } else {
  11464. this.addClass('vjs-slider-horizontal');
  11465. }
  11466. }
  11467. }
  11468. Component.registerComponent('Slider', Slider);
  11469. /**
  11470. * @file load-progress-bar.js
  11471. */
  11472. // get the percent width of a time compared to the total end
  11473. const percentify = (time, end) => clamp(time / end * 100, 0, 100).toFixed(2) + '%';
  11474. /**
  11475. * Shows loading progress
  11476. *
  11477. * @extends Component
  11478. */
  11479. class LoadProgressBar extends Component {
  11480. /**
  11481. * Creates an instance of this class.
  11482. *
  11483. * @param { import('../../player').default } player
  11484. * The `Player` that this class should be attached to.
  11485. *
  11486. * @param {Object} [options]
  11487. * The key/value store of player options.
  11488. */
  11489. constructor(player, options) {
  11490. super(player, options);
  11491. this.partEls_ = [];
  11492. this.on(player, 'progress', e => this.update(e));
  11493. }
  11494. /**
  11495. * Create the `Component`'s DOM element
  11496. *
  11497. * @return {Element}
  11498. * The element that was created.
  11499. */
  11500. createEl() {
  11501. const el = super.createEl('div', {
  11502. className: 'vjs-load-progress'
  11503. });
  11504. const wrapper = createEl('span', {
  11505. className: 'vjs-control-text'
  11506. });
  11507. const loadedText = createEl('span', {
  11508. textContent: this.localize('Loaded')
  11509. });
  11510. const separator = document.createTextNode(': ');
  11511. this.percentageEl_ = createEl('span', {
  11512. className: 'vjs-control-text-loaded-percentage',
  11513. textContent: '0%'
  11514. });
  11515. el.appendChild(wrapper);
  11516. wrapper.appendChild(loadedText);
  11517. wrapper.appendChild(separator);
  11518. wrapper.appendChild(this.percentageEl_);
  11519. return el;
  11520. }
  11521. dispose() {
  11522. this.partEls_ = null;
  11523. this.percentageEl_ = null;
  11524. super.dispose();
  11525. }
  11526. /**
  11527. * Update progress bar
  11528. *
  11529. * @param {Event} [event]
  11530. * The `progress` event that caused this function to run.
  11531. *
  11532. * @listens Player#progress
  11533. */
  11534. update(event) {
  11535. this.requestNamedAnimationFrame('LoadProgressBar#update', () => {
  11536. const liveTracker = this.player_.liveTracker;
  11537. const buffered = this.player_.buffered();
  11538. const duration = liveTracker && liveTracker.isLive() ? liveTracker.seekableEnd() : this.player_.duration();
  11539. const bufferedEnd = this.player_.bufferedEnd();
  11540. const children = this.partEls_;
  11541. const percent = percentify(bufferedEnd, duration);
  11542. if (this.percent_ !== percent) {
  11543. // update the width of the progress bar
  11544. this.el_.style.width = percent;
  11545. // update the control-text
  11546. textContent(this.percentageEl_, percent);
  11547. this.percent_ = percent;
  11548. }
  11549. // add child elements to represent the individual buffered time ranges
  11550. for (let i = 0; i < buffered.length; i++) {
  11551. const start = buffered.start(i);
  11552. const end = buffered.end(i);
  11553. let part = children[i];
  11554. if (!part) {
  11555. part = this.el_.appendChild(createEl());
  11556. children[i] = part;
  11557. }
  11558. // only update if changed
  11559. if (part.dataset.start === start && part.dataset.end === end) {
  11560. continue;
  11561. }
  11562. part.dataset.start = start;
  11563. part.dataset.end = end;
  11564. // set the percent based on the width of the progress bar (bufferedEnd)
  11565. part.style.left = percentify(start, bufferedEnd);
  11566. part.style.width = percentify(end - start, bufferedEnd);
  11567. }
  11568. // remove unused buffered range elements
  11569. for (let i = children.length; i > buffered.length; i--) {
  11570. this.el_.removeChild(children[i - 1]);
  11571. }
  11572. children.length = buffered.length;
  11573. });
  11574. }
  11575. }
  11576. Component.registerComponent('LoadProgressBar', LoadProgressBar);
  11577. /**
  11578. * @file time-tooltip.js
  11579. */
  11580. /**
  11581. * Time tooltips display a time above the progress bar.
  11582. *
  11583. * @extends Component
  11584. */
  11585. class TimeTooltip extends Component {
  11586. /**
  11587. * Creates an instance of this class.
  11588. *
  11589. * @param { import('../../player').default } player
  11590. * The {@link Player} that this class should be attached to.
  11591. *
  11592. * @param {Object} [options]
  11593. * The key/value store of player options.
  11594. */
  11595. constructor(player, options) {
  11596. super(player, options);
  11597. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11598. }
  11599. /**
  11600. * Create the time tooltip DOM element
  11601. *
  11602. * @return {Element}
  11603. * The element that was created.
  11604. */
  11605. createEl() {
  11606. return super.createEl('div', {
  11607. className: 'vjs-time-tooltip'
  11608. }, {
  11609. 'aria-hidden': 'true'
  11610. });
  11611. }
  11612. /**
  11613. * Updates the position of the time tooltip relative to the `SeekBar`.
  11614. *
  11615. * @param {Object} seekBarRect
  11616. * The `ClientRect` for the {@link SeekBar} element.
  11617. *
  11618. * @param {number} seekBarPoint
  11619. * A number from 0 to 1, representing a horizontal reference point
  11620. * from the left edge of the {@link SeekBar}
  11621. */
  11622. update(seekBarRect, seekBarPoint, content) {
  11623. const tooltipRect = findPosition(this.el_);
  11624. const playerRect = getBoundingClientRect(this.player_.el());
  11625. const seekBarPointPx = seekBarRect.width * seekBarPoint;
  11626. // do nothing if either rect isn't available
  11627. // for example, if the player isn't in the DOM for testing
  11628. if (!playerRect || !tooltipRect) {
  11629. return;
  11630. }
  11631. // This is the space left of the `seekBarPoint` available within the bounds
  11632. // of the player. We calculate any gap between the left edge of the player
  11633. // and the left edge of the `SeekBar` and add the number of pixels in the
  11634. // `SeekBar` before hitting the `seekBarPoint`
  11635. const spaceLeftOfPoint = seekBarRect.left - playerRect.left + seekBarPointPx;
  11636. // This is the space right of the `seekBarPoint` available within the bounds
  11637. // of the player. We calculate the number of pixels from the `seekBarPoint`
  11638. // to the right edge of the `SeekBar` and add to that any gap between the
  11639. // right edge of the `SeekBar` and the player.
  11640. const spaceRightOfPoint = seekBarRect.width - seekBarPointPx + (playerRect.right - seekBarRect.right);
  11641. // This is the number of pixels by which the tooltip will need to be pulled
  11642. // further to the right to center it over the `seekBarPoint`.
  11643. let pullTooltipBy = tooltipRect.width / 2;
  11644. // Adjust the `pullTooltipBy` distance to the left or right depending on
  11645. // the results of the space calculations above.
  11646. if (spaceLeftOfPoint < pullTooltipBy) {
  11647. pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
  11648. } else if (spaceRightOfPoint < pullTooltipBy) {
  11649. pullTooltipBy = spaceRightOfPoint;
  11650. }
  11651. // Due to the imprecision of decimal/ratio based calculations and varying
  11652. // rounding behaviors, there are cases where the spacing adjustment is off
  11653. // by a pixel or two. This adds insurance to these calculations.
  11654. if (pullTooltipBy < 0) {
  11655. pullTooltipBy = 0;
  11656. } else if (pullTooltipBy > tooltipRect.width) {
  11657. pullTooltipBy = tooltipRect.width;
  11658. }
  11659. // prevent small width fluctuations within 0.4px from
  11660. // changing the value below.
  11661. // This really helps for live to prevent the play
  11662. // progress time tooltip from jittering
  11663. pullTooltipBy = Math.round(pullTooltipBy);
  11664. this.el_.style.right = `-${pullTooltipBy}px`;
  11665. this.write(content);
  11666. }
  11667. /**
  11668. * Write the time to the tooltip DOM element.
  11669. *
  11670. * @param {string} content
  11671. * The formatted time for the tooltip.
  11672. */
  11673. write(content) {
  11674. textContent(this.el_, content);
  11675. }
  11676. /**
  11677. * Updates the position of the time tooltip relative to the `SeekBar`.
  11678. *
  11679. * @param {Object} seekBarRect
  11680. * The `ClientRect` for the {@link SeekBar} element.
  11681. *
  11682. * @param {number} seekBarPoint
  11683. * A number from 0 to 1, representing a horizontal reference point
  11684. * from the left edge of the {@link SeekBar}
  11685. *
  11686. * @param {number} time
  11687. * The time to update the tooltip to, not used during live playback
  11688. *
  11689. * @param {Function} cb
  11690. * A function that will be called during the request animation frame
  11691. * for tooltips that need to do additional animations from the default
  11692. */
  11693. updateTime(seekBarRect, seekBarPoint, time, cb) {
  11694. this.requestNamedAnimationFrame('TimeTooltip#updateTime', () => {
  11695. let content;
  11696. const duration = this.player_.duration();
  11697. if (this.player_.liveTracker && this.player_.liveTracker.isLive()) {
  11698. const liveWindow = this.player_.liveTracker.liveWindow();
  11699. const secondsBehind = liveWindow - seekBarPoint * liveWindow;
  11700. content = (secondsBehind < 1 ? '' : '-') + formatTime(secondsBehind, liveWindow);
  11701. } else {
  11702. content = formatTime(time, duration);
  11703. }
  11704. this.update(seekBarRect, seekBarPoint, content);
  11705. if (cb) {
  11706. cb();
  11707. }
  11708. });
  11709. }
  11710. }
  11711. Component.registerComponent('TimeTooltip', TimeTooltip);
  11712. /**
  11713. * @file play-progress-bar.js
  11714. */
  11715. /**
  11716. * Used by {@link SeekBar} to display media playback progress as part of the
  11717. * {@link ProgressControl}.
  11718. *
  11719. * @extends Component
  11720. */
  11721. class PlayProgressBar extends Component {
  11722. /**
  11723. * Creates an instance of this class.
  11724. *
  11725. * @param { import('../../player').default } player
  11726. * The {@link Player} that this class should be attached to.
  11727. *
  11728. * @param {Object} [options]
  11729. * The key/value store of player options.
  11730. */
  11731. constructor(player, options) {
  11732. super(player, options);
  11733. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11734. }
  11735. /**
  11736. * Create the the DOM element for this class.
  11737. *
  11738. * @return {Element}
  11739. * The element that was created.
  11740. */
  11741. createEl() {
  11742. return super.createEl('div', {
  11743. className: 'vjs-play-progress vjs-slider-bar'
  11744. }, {
  11745. 'aria-hidden': 'true'
  11746. });
  11747. }
  11748. /**
  11749. * Enqueues updates to its own DOM as well as the DOM of its
  11750. * {@link TimeTooltip} child.
  11751. *
  11752. * @param {Object} seekBarRect
  11753. * The `ClientRect` for the {@link SeekBar} element.
  11754. *
  11755. * @param {number} seekBarPoint
  11756. * A number from 0 to 1, representing a horizontal reference point
  11757. * from the left edge of the {@link SeekBar}
  11758. */
  11759. update(seekBarRect, seekBarPoint) {
  11760. const timeTooltip = this.getChild('timeTooltip');
  11761. if (!timeTooltip) {
  11762. return;
  11763. }
  11764. const time = this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  11765. timeTooltip.updateTime(seekBarRect, seekBarPoint, time);
  11766. }
  11767. }
  11768. /**
  11769. * Default options for {@link PlayProgressBar}.
  11770. *
  11771. * @type {Object}
  11772. * @private
  11773. */
  11774. PlayProgressBar.prototype.options_ = {
  11775. children: []
  11776. };
  11777. // Time tooltips should not be added to a player on mobile devices
  11778. if (!IS_IOS && !IS_ANDROID) {
  11779. PlayProgressBar.prototype.options_.children.push('timeTooltip');
  11780. }
  11781. Component.registerComponent('PlayProgressBar', PlayProgressBar);
  11782. /**
  11783. * @file mouse-time-display.js
  11784. */
  11785. /**
  11786. * The {@link MouseTimeDisplay} component tracks mouse movement over the
  11787. * {@link ProgressControl}. It displays an indicator and a {@link TimeTooltip}
  11788. * indicating the time which is represented by a given point in the
  11789. * {@link ProgressControl}.
  11790. *
  11791. * @extends Component
  11792. */
  11793. class MouseTimeDisplay extends Component {
  11794. /**
  11795. * Creates an instance of this class.
  11796. *
  11797. * @param { import('../../player').default } player
  11798. * The {@link Player} that this class should be attached to.
  11799. *
  11800. * @param {Object} [options]
  11801. * The key/value store of player options.
  11802. */
  11803. constructor(player, options) {
  11804. super(player, options);
  11805. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  11806. }
  11807. /**
  11808. * Create the DOM element for this class.
  11809. *
  11810. * @return {Element}
  11811. * The element that was created.
  11812. */
  11813. createEl() {
  11814. return super.createEl('div', {
  11815. className: 'vjs-mouse-display'
  11816. });
  11817. }
  11818. /**
  11819. * Enqueues updates to its own DOM as well as the DOM of its
  11820. * {@link TimeTooltip} child.
  11821. *
  11822. * @param {Object} seekBarRect
  11823. * The `ClientRect` for the {@link SeekBar} element.
  11824. *
  11825. * @param {number} seekBarPoint
  11826. * A number from 0 to 1, representing a horizontal reference point
  11827. * from the left edge of the {@link SeekBar}
  11828. */
  11829. update(seekBarRect, seekBarPoint) {
  11830. const time = seekBarPoint * this.player_.duration();
  11831. this.getChild('timeTooltip').updateTime(seekBarRect, seekBarPoint, time, () => {
  11832. this.el_.style.left = `${seekBarRect.width * seekBarPoint}px`;
  11833. });
  11834. }
  11835. }
  11836. /**
  11837. * Default options for `MouseTimeDisplay`
  11838. *
  11839. * @type {Object}
  11840. * @private
  11841. */
  11842. MouseTimeDisplay.prototype.options_ = {
  11843. children: ['timeTooltip']
  11844. };
  11845. Component.registerComponent('MouseTimeDisplay', MouseTimeDisplay);
  11846. /**
  11847. * @file seek-bar.js
  11848. */
  11849. // The number of seconds the `step*` functions move the timeline.
  11850. const STEP_SECONDS = 5;
  11851. // The multiplier of STEP_SECONDS that PgUp/PgDown move the timeline.
  11852. const PAGE_KEY_MULTIPLIER = 12;
  11853. /**
  11854. * Seek bar and container for the progress bars. Uses {@link PlayProgressBar}
  11855. * as its `bar`.
  11856. *
  11857. * @extends Slider
  11858. */
  11859. class SeekBar extends Slider {
  11860. /**
  11861. * Creates an instance of this class.
  11862. *
  11863. * @param { import('../../player').default } player
  11864. * The `Player` that this class should be attached to.
  11865. *
  11866. * @param {Object} [options]
  11867. * The key/value store of player options.
  11868. */
  11869. constructor(player, options) {
  11870. super(player, options);
  11871. this.setEventHandlers_();
  11872. }
  11873. /**
  11874. * Sets the event handlers
  11875. *
  11876. * @private
  11877. */
  11878. setEventHandlers_() {
  11879. this.update_ = bind_(this, this.update);
  11880. this.update = throttle(this.update_, UPDATE_REFRESH_INTERVAL);
  11881. this.on(this.player_, ['ended', 'durationchange', 'timeupdate'], this.update);
  11882. if (this.player_.liveTracker) {
  11883. this.on(this.player_.liveTracker, 'liveedgechange', this.update);
  11884. }
  11885. // when playing, let's ensure we smoothly update the play progress bar
  11886. // via an interval
  11887. this.updateInterval = null;
  11888. this.enableIntervalHandler_ = e => this.enableInterval_(e);
  11889. this.disableIntervalHandler_ = e => this.disableInterval_(e);
  11890. this.on(this.player_, ['playing'], this.enableIntervalHandler_);
  11891. this.on(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
  11892. // we don't need to update the play progress if the document is hidden,
  11893. // also, this causes the CPU to spike and eventually crash the page on IE11.
  11894. if ('hidden' in document && 'visibilityState' in document) {
  11895. this.on(document, 'visibilitychange', this.toggleVisibility_);
  11896. }
  11897. }
  11898. toggleVisibility_(e) {
  11899. if (document.visibilityState === 'hidden') {
  11900. this.cancelNamedAnimationFrame('SeekBar#update');
  11901. this.cancelNamedAnimationFrame('Slider#update');
  11902. this.disableInterval_(e);
  11903. } else {
  11904. if (!this.player_.ended() && !this.player_.paused()) {
  11905. this.enableInterval_();
  11906. }
  11907. // we just switched back to the page and someone may be looking, so, update ASAP
  11908. this.update();
  11909. }
  11910. }
  11911. enableInterval_() {
  11912. if (this.updateInterval) {
  11913. return;
  11914. }
  11915. this.updateInterval = this.setInterval(this.update, UPDATE_REFRESH_INTERVAL);
  11916. }
  11917. disableInterval_(e) {
  11918. if (this.player_.liveTracker && this.player_.liveTracker.isLive() && e && e.type !== 'ended') {
  11919. return;
  11920. }
  11921. if (!this.updateInterval) {
  11922. return;
  11923. }
  11924. this.clearInterval(this.updateInterval);
  11925. this.updateInterval = null;
  11926. }
  11927. /**
  11928. * Create the `Component`'s DOM element
  11929. *
  11930. * @return {Element}
  11931. * The element that was created.
  11932. */
  11933. createEl() {
  11934. return super.createEl('div', {
  11935. className: 'vjs-progress-holder'
  11936. }, {
  11937. 'aria-label': this.localize('Progress Bar')
  11938. });
  11939. }
  11940. /**
  11941. * This function updates the play progress bar and accessibility
  11942. * attributes to whatever is passed in.
  11943. *
  11944. * @param {Event} [event]
  11945. * The `timeupdate` or `ended` event that caused this to run.
  11946. *
  11947. * @listens Player#timeupdate
  11948. *
  11949. * @return {number}
  11950. * The current percent at a number from 0-1
  11951. */
  11952. update(event) {
  11953. // ignore updates while the tab is hidden
  11954. if (document.visibilityState === 'hidden') {
  11955. return;
  11956. }
  11957. const percent = super.update();
  11958. this.requestNamedAnimationFrame('SeekBar#update', () => {
  11959. const currentTime = this.player_.ended() ? this.player_.duration() : this.getCurrentTime_();
  11960. const liveTracker = this.player_.liveTracker;
  11961. let duration = this.player_.duration();
  11962. if (liveTracker && liveTracker.isLive()) {
  11963. duration = this.player_.liveTracker.liveCurrentTime();
  11964. }
  11965. if (this.percent_ !== percent) {
  11966. // machine readable value of progress bar (percentage complete)
  11967. this.el_.setAttribute('aria-valuenow', (percent * 100).toFixed(2));
  11968. this.percent_ = percent;
  11969. }
  11970. if (this.currentTime_ !== currentTime || this.duration_ !== duration) {
  11971. // human readable value of progress bar (time complete)
  11972. this.el_.setAttribute('aria-valuetext', this.localize('progress bar timing: currentTime={1} duration={2}', [formatTime(currentTime, duration), formatTime(duration, duration)], '{1} of {2}'));
  11973. this.currentTime_ = currentTime;
  11974. this.duration_ = duration;
  11975. }
  11976. // update the progress bar time tooltip with the current time
  11977. if (this.bar) {
  11978. this.bar.update(getBoundingClientRect(this.el()), this.getProgress());
  11979. }
  11980. });
  11981. return percent;
  11982. }
  11983. /**
  11984. * Prevent liveThreshold from causing seeks to seem like they
  11985. * are not happening from a user perspective.
  11986. *
  11987. * @param {number} ct
  11988. * current time to seek to
  11989. */
  11990. userSeek_(ct) {
  11991. if (this.player_.liveTracker && this.player_.liveTracker.isLive()) {
  11992. this.player_.liveTracker.nextSeekedFromUser();
  11993. }
  11994. this.player_.currentTime(ct);
  11995. }
  11996. /**
  11997. * Get the value of current time but allows for smooth scrubbing,
  11998. * when player can't keep up.
  11999. *
  12000. * @return {number}
  12001. * The current time value to display
  12002. *
  12003. * @private
  12004. */
  12005. getCurrentTime_() {
  12006. return this.player_.scrubbing() ? this.player_.getCache().currentTime : this.player_.currentTime();
  12007. }
  12008. /**
  12009. * Get the percentage of media played so far.
  12010. *
  12011. * @return {number}
  12012. * The percentage of media played so far (0 to 1).
  12013. */
  12014. getPercent() {
  12015. const currentTime = this.getCurrentTime_();
  12016. let percent;
  12017. const liveTracker = this.player_.liveTracker;
  12018. if (liveTracker && liveTracker.isLive()) {
  12019. percent = (currentTime - liveTracker.seekableStart()) / liveTracker.liveWindow();
  12020. // prevent the percent from changing at the live edge
  12021. if (liveTracker.atLiveEdge()) {
  12022. percent = 1;
  12023. }
  12024. } else {
  12025. percent = currentTime / this.player_.duration();
  12026. }
  12027. return percent;
  12028. }
  12029. /**
  12030. * Handle mouse down on seek bar
  12031. *
  12032. * @param {MouseEvent} event
  12033. * The `mousedown` event that caused this to run.
  12034. *
  12035. * @listens mousedown
  12036. */
  12037. handleMouseDown(event) {
  12038. if (!isSingleLeftClick(event)) {
  12039. return;
  12040. }
  12041. // Stop event propagation to prevent double fire in progress-control.js
  12042. event.stopPropagation();
  12043. this.videoWasPlaying = !this.player_.paused();
  12044. this.player_.pause();
  12045. super.handleMouseDown(event);
  12046. }
  12047. /**
  12048. * Handle mouse move on seek bar
  12049. *
  12050. * @param {MouseEvent} event
  12051. * The `mousemove` event that caused this to run.
  12052. * @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
  12053. *
  12054. * @listens mousemove
  12055. */
  12056. handleMouseMove(event, mouseDown = false) {
  12057. if (!isSingleLeftClick(event)) {
  12058. return;
  12059. }
  12060. if (!mouseDown && !this.player_.scrubbing()) {
  12061. this.player_.scrubbing(true);
  12062. }
  12063. let newTime;
  12064. const distance = this.calculateDistance(event);
  12065. const liveTracker = this.player_.liveTracker;
  12066. if (!liveTracker || !liveTracker.isLive()) {
  12067. newTime = distance * this.player_.duration();
  12068. // Don't let video end while scrubbing.
  12069. if (newTime === this.player_.duration()) {
  12070. newTime = newTime - 0.1;
  12071. }
  12072. } else {
  12073. if (distance >= 0.99) {
  12074. liveTracker.seekToLiveEdge();
  12075. return;
  12076. }
  12077. const seekableStart = liveTracker.seekableStart();
  12078. const seekableEnd = liveTracker.liveCurrentTime();
  12079. newTime = seekableStart + distance * liveTracker.liveWindow();
  12080. // Don't let video end while scrubbing.
  12081. if (newTime >= seekableEnd) {
  12082. newTime = seekableEnd;
  12083. }
  12084. // Compensate for precision differences so that currentTime is not less
  12085. // than seekable start
  12086. if (newTime <= seekableStart) {
  12087. newTime = seekableStart + 0.1;
  12088. }
  12089. // On android seekableEnd can be Infinity sometimes,
  12090. // this will cause newTime to be Infinity, which is
  12091. // not a valid currentTime.
  12092. if (newTime === Infinity) {
  12093. return;
  12094. }
  12095. }
  12096. // Set new time (tell player to seek to new time)
  12097. this.userSeek_(newTime);
  12098. }
  12099. enable() {
  12100. super.enable();
  12101. const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
  12102. if (!mouseTimeDisplay) {
  12103. return;
  12104. }
  12105. mouseTimeDisplay.show();
  12106. }
  12107. disable() {
  12108. super.disable();
  12109. const mouseTimeDisplay = this.getChild('mouseTimeDisplay');
  12110. if (!mouseTimeDisplay) {
  12111. return;
  12112. }
  12113. mouseTimeDisplay.hide();
  12114. }
  12115. /**
  12116. * Handle mouse up on seek bar
  12117. *
  12118. * @param {MouseEvent} event
  12119. * The `mouseup` event that caused this to run.
  12120. *
  12121. * @listens mouseup
  12122. */
  12123. handleMouseUp(event) {
  12124. super.handleMouseUp(event);
  12125. // Stop event propagation to prevent double fire in progress-control.js
  12126. if (event) {
  12127. event.stopPropagation();
  12128. }
  12129. this.player_.scrubbing(false);
  12130. /**
  12131. * Trigger timeupdate because we're done seeking and the time has changed.
  12132. * This is particularly useful for if the player is paused to time the time displays.
  12133. *
  12134. * @event Tech#timeupdate
  12135. * @type {Event}
  12136. */
  12137. this.player_.trigger({
  12138. type: 'timeupdate',
  12139. target: this,
  12140. manuallyTriggered: true
  12141. });
  12142. if (this.videoWasPlaying) {
  12143. silencePromise(this.player_.play());
  12144. } else {
  12145. // We're done seeking and the time has changed.
  12146. // If the player is paused, make sure we display the correct time on the seek bar.
  12147. this.update_();
  12148. }
  12149. }
  12150. /**
  12151. * Move more quickly fast forward for keyboard-only users
  12152. */
  12153. stepForward() {
  12154. this.userSeek_(this.player_.currentTime() + STEP_SECONDS);
  12155. }
  12156. /**
  12157. * Move more quickly rewind for keyboard-only users
  12158. */
  12159. stepBack() {
  12160. this.userSeek_(this.player_.currentTime() - STEP_SECONDS);
  12161. }
  12162. /**
  12163. * Toggles the playback state of the player
  12164. * This gets called when enter or space is used on the seekbar
  12165. *
  12166. * @param {KeyboardEvent} event
  12167. * The `keydown` event that caused this function to be called
  12168. *
  12169. */
  12170. handleAction(event) {
  12171. if (this.player_.paused()) {
  12172. this.player_.play();
  12173. } else {
  12174. this.player_.pause();
  12175. }
  12176. }
  12177. /**
  12178. * Called when this SeekBar has focus and a key gets pressed down.
  12179. * Supports the following keys:
  12180. *
  12181. * Space or Enter key fire a click event
  12182. * Home key moves to start of the timeline
  12183. * End key moves to end of the timeline
  12184. * Digit "0" through "9" keys move to 0%, 10% ... 80%, 90% of the timeline
  12185. * PageDown key moves back a larger step than ArrowDown
  12186. * PageUp key moves forward a large step
  12187. *
  12188. * @param {KeyboardEvent} event
  12189. * The `keydown` event that caused this function to be called.
  12190. *
  12191. * @listens keydown
  12192. */
  12193. handleKeyDown(event) {
  12194. const liveTracker = this.player_.liveTracker;
  12195. if (keycode.isEventKey(event, 'Space') || keycode.isEventKey(event, 'Enter')) {
  12196. event.preventDefault();
  12197. event.stopPropagation();
  12198. this.handleAction(event);
  12199. } else if (keycode.isEventKey(event, 'Home')) {
  12200. event.preventDefault();
  12201. event.stopPropagation();
  12202. this.userSeek_(0);
  12203. } else if (keycode.isEventKey(event, 'End')) {
  12204. event.preventDefault();
  12205. event.stopPropagation();
  12206. if (liveTracker && liveTracker.isLive()) {
  12207. this.userSeek_(liveTracker.liveCurrentTime());
  12208. } else {
  12209. this.userSeek_(this.player_.duration());
  12210. }
  12211. } else if (/^[0-9]$/.test(keycode(event))) {
  12212. event.preventDefault();
  12213. event.stopPropagation();
  12214. const gotoFraction = (keycode.codes[keycode(event)] - keycode.codes['0']) * 10.0 / 100.0;
  12215. if (liveTracker && liveTracker.isLive()) {
  12216. this.userSeek_(liveTracker.seekableStart() + liveTracker.liveWindow() * gotoFraction);
  12217. } else {
  12218. this.userSeek_(this.player_.duration() * gotoFraction);
  12219. }
  12220. } else if (keycode.isEventKey(event, 'PgDn')) {
  12221. event.preventDefault();
  12222. event.stopPropagation();
  12223. this.userSeek_(this.player_.currentTime() - STEP_SECONDS * PAGE_KEY_MULTIPLIER);
  12224. } else if (keycode.isEventKey(event, 'PgUp')) {
  12225. event.preventDefault();
  12226. event.stopPropagation();
  12227. this.userSeek_(this.player_.currentTime() + STEP_SECONDS * PAGE_KEY_MULTIPLIER);
  12228. } else {
  12229. // Pass keydown handling up for unsupported keys
  12230. super.handleKeyDown(event);
  12231. }
  12232. }
  12233. dispose() {
  12234. this.disableInterval_();
  12235. this.off(this.player_, ['ended', 'durationchange', 'timeupdate'], this.update);
  12236. if (this.player_.liveTracker) {
  12237. this.off(this.player_.liveTracker, 'liveedgechange', this.update);
  12238. }
  12239. this.off(this.player_, ['playing'], this.enableIntervalHandler_);
  12240. this.off(this.player_, ['ended', 'pause', 'waiting'], this.disableIntervalHandler_);
  12241. // we don't need to update the play progress if the document is hidden,
  12242. // also, this causes the CPU to spike and eventually crash the page on IE11.
  12243. if ('hidden' in document && 'visibilityState' in document) {
  12244. this.off(document, 'visibilitychange', this.toggleVisibility_);
  12245. }
  12246. super.dispose();
  12247. }
  12248. }
  12249. /**
  12250. * Default options for the `SeekBar`
  12251. *
  12252. * @type {Object}
  12253. * @private
  12254. */
  12255. SeekBar.prototype.options_ = {
  12256. children: ['loadProgressBar', 'playProgressBar'],
  12257. barName: 'playProgressBar'
  12258. };
  12259. // MouseTimeDisplay tooltips should not be added to a player on mobile devices
  12260. if (!IS_IOS && !IS_ANDROID) {
  12261. SeekBar.prototype.options_.children.splice(1, 0, 'mouseTimeDisplay');
  12262. }
  12263. Component.registerComponent('SeekBar', SeekBar);
  12264. /**
  12265. * @file progress-control.js
  12266. */
  12267. /**
  12268. * The Progress Control component contains the seek bar, load progress,
  12269. * and play progress.
  12270. *
  12271. * @extends Component
  12272. */
  12273. class ProgressControl extends Component {
  12274. /**
  12275. * Creates an instance of this class.
  12276. *
  12277. * @param { import('../../player').default } player
  12278. * The `Player` that this class should be attached to.
  12279. *
  12280. * @param {Object} [options]
  12281. * The key/value store of player options.
  12282. */
  12283. constructor(player, options) {
  12284. super(player, options);
  12285. this.handleMouseMove = throttle(bind_(this, this.handleMouseMove), UPDATE_REFRESH_INTERVAL);
  12286. this.throttledHandleMouseSeek = throttle(bind_(this, this.handleMouseSeek), UPDATE_REFRESH_INTERVAL);
  12287. this.handleMouseUpHandler_ = e => this.handleMouseUp(e);
  12288. this.handleMouseDownHandler_ = e => this.handleMouseDown(e);
  12289. this.enable();
  12290. }
  12291. /**
  12292. * Create the `Component`'s DOM element
  12293. *
  12294. * @return {Element}
  12295. * The element that was created.
  12296. */
  12297. createEl() {
  12298. return super.createEl('div', {
  12299. className: 'vjs-progress-control vjs-control'
  12300. });
  12301. }
  12302. /**
  12303. * When the mouse moves over the `ProgressControl`, the pointer position
  12304. * gets passed down to the `MouseTimeDisplay` component.
  12305. *
  12306. * @param {Event} event
  12307. * The `mousemove` event that caused this function to run.
  12308. *
  12309. * @listen mousemove
  12310. */
  12311. handleMouseMove(event) {
  12312. const seekBar = this.getChild('seekBar');
  12313. if (!seekBar) {
  12314. return;
  12315. }
  12316. const playProgressBar = seekBar.getChild('playProgressBar');
  12317. const mouseTimeDisplay = seekBar.getChild('mouseTimeDisplay');
  12318. if (!playProgressBar && !mouseTimeDisplay) {
  12319. return;
  12320. }
  12321. const seekBarEl = seekBar.el();
  12322. const seekBarRect = findPosition(seekBarEl);
  12323. let seekBarPoint = getPointerPosition(seekBarEl, event).x;
  12324. // The default skin has a gap on either side of the `SeekBar`. This means
  12325. // that it's possible to trigger this behavior outside the boundaries of
  12326. // the `SeekBar`. This ensures we stay within it at all times.
  12327. seekBarPoint = clamp(seekBarPoint, 0, 1);
  12328. if (mouseTimeDisplay) {
  12329. mouseTimeDisplay.update(seekBarRect, seekBarPoint);
  12330. }
  12331. if (playProgressBar) {
  12332. playProgressBar.update(seekBarRect, seekBar.getProgress());
  12333. }
  12334. }
  12335. /**
  12336. * A throttled version of the {@link ProgressControl#handleMouseSeek} listener.
  12337. *
  12338. * @method ProgressControl#throttledHandleMouseSeek
  12339. * @param {Event} event
  12340. * The `mousemove` event that caused this function to run.
  12341. *
  12342. * @listen mousemove
  12343. * @listen touchmove
  12344. */
  12345. /**
  12346. * Handle `mousemove` or `touchmove` events on the `ProgressControl`.
  12347. *
  12348. * @param {Event} event
  12349. * `mousedown` or `touchstart` event that triggered this function
  12350. *
  12351. * @listens mousemove
  12352. * @listens touchmove
  12353. */
  12354. handleMouseSeek(event) {
  12355. const seekBar = this.getChild('seekBar');
  12356. if (seekBar) {
  12357. seekBar.handleMouseMove(event);
  12358. }
  12359. }
  12360. /**
  12361. * Are controls are currently enabled for this progress control.
  12362. *
  12363. * @return {boolean}
  12364. * true if controls are enabled, false otherwise
  12365. */
  12366. enabled() {
  12367. return this.enabled_;
  12368. }
  12369. /**
  12370. * Disable all controls on the progress control and its children
  12371. */
  12372. disable() {
  12373. this.children().forEach(child => child.disable && child.disable());
  12374. if (!this.enabled()) {
  12375. return;
  12376. }
  12377. this.off(['mousedown', 'touchstart'], this.handleMouseDownHandler_);
  12378. this.off(this.el_, 'mousemove', this.handleMouseMove);
  12379. this.removeListenersAddedOnMousedownAndTouchstart();
  12380. this.addClass('disabled');
  12381. this.enabled_ = false;
  12382. // Restore normal playback state if controls are disabled while scrubbing
  12383. if (this.player_.scrubbing()) {
  12384. const seekBar = this.getChild('seekBar');
  12385. this.player_.scrubbing(false);
  12386. if (seekBar.videoWasPlaying) {
  12387. silencePromise(this.player_.play());
  12388. }
  12389. }
  12390. }
  12391. /**
  12392. * Enable all controls on the progress control and its children
  12393. */
  12394. enable() {
  12395. this.children().forEach(child => child.enable && child.enable());
  12396. if (this.enabled()) {
  12397. return;
  12398. }
  12399. this.on(['mousedown', 'touchstart'], this.handleMouseDownHandler_);
  12400. this.on(this.el_, 'mousemove', this.handleMouseMove);
  12401. this.removeClass('disabled');
  12402. this.enabled_ = true;
  12403. }
  12404. /**
  12405. * Cleanup listeners after the user finishes interacting with the progress controls
  12406. */
  12407. removeListenersAddedOnMousedownAndTouchstart() {
  12408. const doc = this.el_.ownerDocument;
  12409. this.off(doc, 'mousemove', this.throttledHandleMouseSeek);
  12410. this.off(doc, 'touchmove', this.throttledHandleMouseSeek);
  12411. this.off(doc, 'mouseup', this.handleMouseUpHandler_);
  12412. this.off(doc, 'touchend', this.handleMouseUpHandler_);
  12413. }
  12414. /**
  12415. * Handle `mousedown` or `touchstart` events on the `ProgressControl`.
  12416. *
  12417. * @param {Event} event
  12418. * `mousedown` or `touchstart` event that triggered this function
  12419. *
  12420. * @listens mousedown
  12421. * @listens touchstart
  12422. */
  12423. handleMouseDown(event) {
  12424. const doc = this.el_.ownerDocument;
  12425. const seekBar = this.getChild('seekBar');
  12426. if (seekBar) {
  12427. seekBar.handleMouseDown(event);
  12428. }
  12429. this.on(doc, 'mousemove', this.throttledHandleMouseSeek);
  12430. this.on(doc, 'touchmove', this.throttledHandleMouseSeek);
  12431. this.on(doc, 'mouseup', this.handleMouseUpHandler_);
  12432. this.on(doc, 'touchend', this.handleMouseUpHandler_);
  12433. }
  12434. /**
  12435. * Handle `mouseup` or `touchend` events on the `ProgressControl`.
  12436. *
  12437. * @param {Event} event
  12438. * `mouseup` or `touchend` event that triggered this function.
  12439. *
  12440. * @listens touchend
  12441. * @listens mouseup
  12442. */
  12443. handleMouseUp(event) {
  12444. const seekBar = this.getChild('seekBar');
  12445. if (seekBar) {
  12446. seekBar.handleMouseUp(event);
  12447. }
  12448. this.removeListenersAddedOnMousedownAndTouchstart();
  12449. }
  12450. }
  12451. /**
  12452. * Default options for `ProgressControl`
  12453. *
  12454. * @type {Object}
  12455. * @private
  12456. */
  12457. ProgressControl.prototype.options_ = {
  12458. children: ['seekBar']
  12459. };
  12460. Component.registerComponent('ProgressControl', ProgressControl);
  12461. /**
  12462. * @file picture-in-picture-toggle.js
  12463. */
  12464. /**
  12465. * Toggle Picture-in-Picture mode
  12466. *
  12467. * @extends Button
  12468. */
  12469. class PictureInPictureToggle extends Button {
  12470. /**
  12471. * Creates an instance of this class.
  12472. *
  12473. * @param { import('./player').default } player
  12474. * The `Player` that this class should be attached to.
  12475. *
  12476. * @param {Object} [options]
  12477. * The key/value store of player options.
  12478. *
  12479. * @listens Player#enterpictureinpicture
  12480. * @listens Player#leavepictureinpicture
  12481. */
  12482. constructor(player, options) {
  12483. super(player, options);
  12484. this.on(player, ['enterpictureinpicture', 'leavepictureinpicture'], e => this.handlePictureInPictureChange(e));
  12485. this.on(player, ['disablepictureinpicturechanged', 'loadedmetadata'], e => this.handlePictureInPictureEnabledChange(e));
  12486. this.on(player, ['loadedmetadata', 'audioonlymodechange', 'audiopostermodechange'], () => {
  12487. // This audio detection will not detect HLS or DASH audio-only streams because there was no reliable way to detect them at the time
  12488. const isSourceAudio = player.currentType().substring(0, 5) === 'audio';
  12489. if (isSourceAudio || player.audioPosterMode() || player.audioOnlyMode()) {
  12490. if (player.isInPictureInPicture()) {
  12491. player.exitPictureInPicture();
  12492. }
  12493. this.hide();
  12494. } else {
  12495. this.show();
  12496. }
  12497. });
  12498. // TODO: Deactivate button on player emptied event.
  12499. this.disable();
  12500. }
  12501. /**
  12502. * Builds the default DOM `className`.
  12503. *
  12504. * @return {string}
  12505. * The DOM `className` for this object.
  12506. */
  12507. buildCSSClass() {
  12508. return `vjs-picture-in-picture-control ${super.buildCSSClass()}`;
  12509. }
  12510. /**
  12511. * Enables or disables button based on availability of a Picture-In-Picture mode.
  12512. *
  12513. * Enabled if
  12514. * - `player.options().enableDocumentPictureInPicture` is true and
  12515. * window.documentPictureInPicture is available; or
  12516. * - `player.disablePictureInPicture()` is false and
  12517. * element.requestPictureInPicture is available
  12518. */
  12519. handlePictureInPictureEnabledChange() {
  12520. if (document.pictureInPictureEnabled && this.player_.disablePictureInPicture() === false || this.player_.options_.enableDocumentPictureInPicture && 'documentPictureInPicture' in window) {
  12521. this.enable();
  12522. } else {
  12523. this.disable();
  12524. }
  12525. }
  12526. /**
  12527. * Handles enterpictureinpicture and leavepictureinpicture on the player and change control text accordingly.
  12528. *
  12529. * @param {Event} [event]
  12530. * The {@link Player#enterpictureinpicture} or {@link Player#leavepictureinpicture} event that caused this function to be
  12531. * called.
  12532. *
  12533. * @listens Player#enterpictureinpicture
  12534. * @listens Player#leavepictureinpicture
  12535. */
  12536. handlePictureInPictureChange(event) {
  12537. if (this.player_.isInPictureInPicture()) {
  12538. this.controlText('Exit Picture-in-Picture');
  12539. } else {
  12540. this.controlText('Picture-in-Picture');
  12541. }
  12542. this.handlePictureInPictureEnabledChange();
  12543. }
  12544. /**
  12545. * This gets called when an `PictureInPictureToggle` is "clicked". See
  12546. * {@link ClickableComponent} for more detailed information on what a click can be.
  12547. *
  12548. * @param {Event} [event]
  12549. * The `keydown`, `tap`, or `click` event that caused this function to be
  12550. * called.
  12551. *
  12552. * @listens tap
  12553. * @listens click
  12554. */
  12555. handleClick(event) {
  12556. if (!this.player_.isInPictureInPicture()) {
  12557. this.player_.requestPictureInPicture();
  12558. } else {
  12559. this.player_.exitPictureInPicture();
  12560. }
  12561. }
  12562. }
  12563. /**
  12564. * The text that should display over the `PictureInPictureToggle`s controls. Added for localization.
  12565. *
  12566. * @type {string}
  12567. * @protected
  12568. */
  12569. PictureInPictureToggle.prototype.controlText_ = 'Picture-in-Picture';
  12570. Component.registerComponent('PictureInPictureToggle', PictureInPictureToggle);
  12571. /**
  12572. * @file fullscreen-toggle.js
  12573. */
  12574. /**
  12575. * Toggle fullscreen video
  12576. *
  12577. * @extends Button
  12578. */
  12579. class FullscreenToggle extends Button {
  12580. /**
  12581. * Creates an instance of this class.
  12582. *
  12583. * @param { import('./player').default } player
  12584. * The `Player` that this class should be attached to.
  12585. *
  12586. * @param {Object} [options]
  12587. * The key/value store of player options.
  12588. */
  12589. constructor(player, options) {
  12590. super(player, options);
  12591. this.on(player, 'fullscreenchange', e => this.handleFullscreenChange(e));
  12592. if (document[player.fsApi_.fullscreenEnabled] === false) {
  12593. this.disable();
  12594. }
  12595. }
  12596. /**
  12597. * Builds the default DOM `className`.
  12598. *
  12599. * @return {string}
  12600. * The DOM `className` for this object.
  12601. */
  12602. buildCSSClass() {
  12603. return `vjs-fullscreen-control ${super.buildCSSClass()}`;
  12604. }
  12605. /**
  12606. * Handles fullscreenchange on the player and change control text accordingly.
  12607. *
  12608. * @param {Event} [event]
  12609. * The {@link Player#fullscreenchange} event that caused this function to be
  12610. * called.
  12611. *
  12612. * @listens Player#fullscreenchange
  12613. */
  12614. handleFullscreenChange(event) {
  12615. if (this.player_.isFullscreen()) {
  12616. this.controlText('Exit Fullscreen');
  12617. } else {
  12618. this.controlText('Fullscreen');
  12619. }
  12620. }
  12621. /**
  12622. * This gets called when an `FullscreenToggle` is "clicked". See
  12623. * {@link ClickableComponent} for more detailed information on what a click can be.
  12624. *
  12625. * @param {Event} [event]
  12626. * The `keydown`, `tap`, or `click` event that caused this function to be
  12627. * called.
  12628. *
  12629. * @listens tap
  12630. * @listens click
  12631. */
  12632. handleClick(event) {
  12633. if (!this.player_.isFullscreen()) {
  12634. this.player_.requestFullscreen();
  12635. } else {
  12636. this.player_.exitFullscreen();
  12637. }
  12638. }
  12639. }
  12640. /**
  12641. * The text that should display over the `FullscreenToggle`s controls. Added for localization.
  12642. *
  12643. * @type {string}
  12644. * @protected
  12645. */
  12646. FullscreenToggle.prototype.controlText_ = 'Fullscreen';
  12647. Component.registerComponent('FullscreenToggle', FullscreenToggle);
  12648. /**
  12649. * Check if volume control is supported and if it isn't hide the
  12650. * `Component` that was passed using the `vjs-hidden` class.
  12651. *
  12652. * @param { import('../../component').default } self
  12653. * The component that should be hidden if volume is unsupported
  12654. *
  12655. * @param { import('../../player').default } player
  12656. * A reference to the player
  12657. *
  12658. * @private
  12659. */
  12660. const checkVolumeSupport = function (self, player) {
  12661. // hide volume controls when they're not supported by the current tech
  12662. if (player.tech_ && !player.tech_.featuresVolumeControl) {
  12663. self.addClass('vjs-hidden');
  12664. }
  12665. self.on(player, 'loadstart', function () {
  12666. if (!player.tech_.featuresVolumeControl) {
  12667. self.addClass('vjs-hidden');
  12668. } else {
  12669. self.removeClass('vjs-hidden');
  12670. }
  12671. });
  12672. };
  12673. /**
  12674. * @file volume-level.js
  12675. */
  12676. /**
  12677. * Shows volume level
  12678. *
  12679. * @extends Component
  12680. */
  12681. class VolumeLevel extends Component {
  12682. /**
  12683. * Create the `Component`'s DOM element
  12684. *
  12685. * @return {Element}
  12686. * The element that was created.
  12687. */
  12688. createEl() {
  12689. const el = super.createEl('div', {
  12690. className: 'vjs-volume-level'
  12691. });
  12692. el.appendChild(super.createEl('span', {
  12693. className: 'vjs-control-text'
  12694. }));
  12695. return el;
  12696. }
  12697. }
  12698. Component.registerComponent('VolumeLevel', VolumeLevel);
  12699. /**
  12700. * @file volume-level-tooltip.js
  12701. */
  12702. /**
  12703. * Volume level tooltips display a volume above or side by side the volume bar.
  12704. *
  12705. * @extends Component
  12706. */
  12707. class VolumeLevelTooltip extends Component {
  12708. /**
  12709. * Creates an instance of this class.
  12710. *
  12711. * @param { import('../../player').default } player
  12712. * The {@link Player} that this class should be attached to.
  12713. *
  12714. * @param {Object} [options]
  12715. * The key/value store of player options.
  12716. */
  12717. constructor(player, options) {
  12718. super(player, options);
  12719. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  12720. }
  12721. /**
  12722. * Create the volume tooltip DOM element
  12723. *
  12724. * @return {Element}
  12725. * The element that was created.
  12726. */
  12727. createEl() {
  12728. return super.createEl('div', {
  12729. className: 'vjs-volume-tooltip'
  12730. }, {
  12731. 'aria-hidden': 'true'
  12732. });
  12733. }
  12734. /**
  12735. * Updates the position of the tooltip relative to the `VolumeBar` and
  12736. * its content text.
  12737. *
  12738. * @param {Object} rangeBarRect
  12739. * The `ClientRect` for the {@link VolumeBar} element.
  12740. *
  12741. * @param {number} rangeBarPoint
  12742. * A number from 0 to 1, representing a horizontal/vertical reference point
  12743. * from the left edge of the {@link VolumeBar}
  12744. *
  12745. * @param {boolean} vertical
  12746. * Referees to the Volume control position
  12747. * in the control bar{@link VolumeControl}
  12748. *
  12749. */
  12750. update(rangeBarRect, rangeBarPoint, vertical, content) {
  12751. if (!vertical) {
  12752. const tooltipRect = getBoundingClientRect(this.el_);
  12753. const playerRect = getBoundingClientRect(this.player_.el());
  12754. const volumeBarPointPx = rangeBarRect.width * rangeBarPoint;
  12755. if (!playerRect || !tooltipRect) {
  12756. return;
  12757. }
  12758. const spaceLeftOfPoint = rangeBarRect.left - playerRect.left + volumeBarPointPx;
  12759. const spaceRightOfPoint = rangeBarRect.width - volumeBarPointPx + (playerRect.right - rangeBarRect.right);
  12760. let pullTooltipBy = tooltipRect.width / 2;
  12761. if (spaceLeftOfPoint < pullTooltipBy) {
  12762. pullTooltipBy += pullTooltipBy - spaceLeftOfPoint;
  12763. } else if (spaceRightOfPoint < pullTooltipBy) {
  12764. pullTooltipBy = spaceRightOfPoint;
  12765. }
  12766. if (pullTooltipBy < 0) {
  12767. pullTooltipBy = 0;
  12768. } else if (pullTooltipBy > tooltipRect.width) {
  12769. pullTooltipBy = tooltipRect.width;
  12770. }
  12771. this.el_.style.right = `-${pullTooltipBy}px`;
  12772. }
  12773. this.write(`${content}%`);
  12774. }
  12775. /**
  12776. * Write the volume to the tooltip DOM element.
  12777. *
  12778. * @param {string} content
  12779. * The formatted volume for the tooltip.
  12780. */
  12781. write(content) {
  12782. textContent(this.el_, content);
  12783. }
  12784. /**
  12785. * Updates the position of the volume tooltip relative to the `VolumeBar`.
  12786. *
  12787. * @param {Object} rangeBarRect
  12788. * The `ClientRect` for the {@link VolumeBar} element.
  12789. *
  12790. * @param {number} rangeBarPoint
  12791. * A number from 0 to 1, representing a horizontal/vertical reference point
  12792. * from the left edge of the {@link VolumeBar}
  12793. *
  12794. * @param {boolean} vertical
  12795. * Referees to the Volume control position
  12796. * in the control bar{@link VolumeControl}
  12797. *
  12798. * @param {number} volume
  12799. * The volume level to update the tooltip to
  12800. *
  12801. * @param {Function} cb
  12802. * A function that will be called during the request animation frame
  12803. * for tooltips that need to do additional animations from the default
  12804. */
  12805. updateVolume(rangeBarRect, rangeBarPoint, vertical, volume, cb) {
  12806. this.requestNamedAnimationFrame('VolumeLevelTooltip#updateVolume', () => {
  12807. this.update(rangeBarRect, rangeBarPoint, vertical, volume.toFixed(0));
  12808. if (cb) {
  12809. cb();
  12810. }
  12811. });
  12812. }
  12813. }
  12814. Component.registerComponent('VolumeLevelTooltip', VolumeLevelTooltip);
  12815. /**
  12816. * @file mouse-volume-level-display.js
  12817. */
  12818. /**
  12819. * The {@link MouseVolumeLevelDisplay} component tracks mouse movement over the
  12820. * {@link VolumeControl}. It displays an indicator and a {@link VolumeLevelTooltip}
  12821. * indicating the volume level which is represented by a given point in the
  12822. * {@link VolumeBar}.
  12823. *
  12824. * @extends Component
  12825. */
  12826. class MouseVolumeLevelDisplay extends Component {
  12827. /**
  12828. * Creates an instance of this class.
  12829. *
  12830. * @param { import('../../player').default } player
  12831. * The {@link Player} that this class should be attached to.
  12832. *
  12833. * @param {Object} [options]
  12834. * The key/value store of player options.
  12835. */
  12836. constructor(player, options) {
  12837. super(player, options);
  12838. this.update = throttle(bind_(this, this.update), UPDATE_REFRESH_INTERVAL);
  12839. }
  12840. /**
  12841. * Create the DOM element for this class.
  12842. *
  12843. * @return {Element}
  12844. * The element that was created.
  12845. */
  12846. createEl() {
  12847. return super.createEl('div', {
  12848. className: 'vjs-mouse-display'
  12849. });
  12850. }
  12851. /**
  12852. * Enquires updates to its own DOM as well as the DOM of its
  12853. * {@link VolumeLevelTooltip} child.
  12854. *
  12855. * @param {Object} rangeBarRect
  12856. * The `ClientRect` for the {@link VolumeBar} element.
  12857. *
  12858. * @param {number} rangeBarPoint
  12859. * A number from 0 to 1, representing a horizontal/vertical reference point
  12860. * from the left edge of the {@link VolumeBar}
  12861. *
  12862. * @param {boolean} vertical
  12863. * Referees to the Volume control position
  12864. * in the control bar{@link VolumeControl}
  12865. *
  12866. */
  12867. update(rangeBarRect, rangeBarPoint, vertical) {
  12868. const volume = 100 * rangeBarPoint;
  12869. this.getChild('volumeLevelTooltip').updateVolume(rangeBarRect, rangeBarPoint, vertical, volume, () => {
  12870. if (vertical) {
  12871. this.el_.style.bottom = `${rangeBarRect.height * rangeBarPoint}px`;
  12872. } else {
  12873. this.el_.style.left = `${rangeBarRect.width * rangeBarPoint}px`;
  12874. }
  12875. });
  12876. }
  12877. }
  12878. /**
  12879. * Default options for `MouseVolumeLevelDisplay`
  12880. *
  12881. * @type {Object}
  12882. * @private
  12883. */
  12884. MouseVolumeLevelDisplay.prototype.options_ = {
  12885. children: ['volumeLevelTooltip']
  12886. };
  12887. Component.registerComponent('MouseVolumeLevelDisplay', MouseVolumeLevelDisplay);
  12888. /**
  12889. * @file volume-bar.js
  12890. */
  12891. /**
  12892. * The bar that contains the volume level and can be clicked on to adjust the level
  12893. *
  12894. * @extends Slider
  12895. */
  12896. class VolumeBar extends Slider {
  12897. /**
  12898. * Creates an instance of this class.
  12899. *
  12900. * @param { import('../../player').default } player
  12901. * The `Player` that this class should be attached to.
  12902. *
  12903. * @param {Object} [options]
  12904. * The key/value store of player options.
  12905. */
  12906. constructor(player, options) {
  12907. super(player, options);
  12908. this.on('slideractive', e => this.updateLastVolume_(e));
  12909. this.on(player, 'volumechange', e => this.updateARIAAttributes(e));
  12910. player.ready(() => this.updateARIAAttributes());
  12911. }
  12912. /**
  12913. * Create the `Component`'s DOM element
  12914. *
  12915. * @return {Element}
  12916. * The element that was created.
  12917. */
  12918. createEl() {
  12919. return super.createEl('div', {
  12920. className: 'vjs-volume-bar vjs-slider-bar'
  12921. }, {
  12922. 'aria-label': this.localize('Volume Level'),
  12923. 'aria-live': 'polite'
  12924. });
  12925. }
  12926. /**
  12927. * Handle mouse down on volume bar
  12928. *
  12929. * @param {Event} event
  12930. * The `mousedown` event that caused this to run.
  12931. *
  12932. * @listens mousedown
  12933. */
  12934. handleMouseDown(event) {
  12935. if (!isSingleLeftClick(event)) {
  12936. return;
  12937. }
  12938. super.handleMouseDown(event);
  12939. }
  12940. /**
  12941. * Handle movement events on the {@link VolumeMenuButton}.
  12942. *
  12943. * @param {Event} event
  12944. * The event that caused this function to run.
  12945. *
  12946. * @listens mousemove
  12947. */
  12948. handleMouseMove(event) {
  12949. const mouseVolumeLevelDisplay = this.getChild('mouseVolumeLevelDisplay');
  12950. if (mouseVolumeLevelDisplay) {
  12951. const volumeBarEl = this.el();
  12952. const volumeBarRect = getBoundingClientRect(volumeBarEl);
  12953. const vertical = this.vertical();
  12954. let volumeBarPoint = getPointerPosition(volumeBarEl, event);
  12955. volumeBarPoint = vertical ? volumeBarPoint.y : volumeBarPoint.x;
  12956. // The default skin has a gap on either side of the `VolumeBar`. This means
  12957. // that it's possible to trigger this behavior outside the boundaries of
  12958. // the `VolumeBar`. This ensures we stay within it at all times.
  12959. volumeBarPoint = clamp(volumeBarPoint, 0, 1);
  12960. mouseVolumeLevelDisplay.update(volumeBarRect, volumeBarPoint, vertical);
  12961. }
  12962. if (!isSingleLeftClick(event)) {
  12963. return;
  12964. }
  12965. this.checkMuted();
  12966. this.player_.volume(this.calculateDistance(event));
  12967. }
  12968. /**
  12969. * If the player is muted unmute it.
  12970. */
  12971. checkMuted() {
  12972. if (this.player_.muted()) {
  12973. this.player_.muted(false);
  12974. }
  12975. }
  12976. /**
  12977. * Get percent of volume level
  12978. *
  12979. * @return {number}
  12980. * Volume level percent as a decimal number.
  12981. */
  12982. getPercent() {
  12983. if (this.player_.muted()) {
  12984. return 0;
  12985. }
  12986. return this.player_.volume();
  12987. }
  12988. /**
  12989. * Increase volume level for keyboard users
  12990. */
  12991. stepForward() {
  12992. this.checkMuted();
  12993. this.player_.volume(this.player_.volume() + 0.1);
  12994. }
  12995. /**
  12996. * Decrease volume level for keyboard users
  12997. */
  12998. stepBack() {
  12999. this.checkMuted();
  13000. this.player_.volume(this.player_.volume() - 0.1);
  13001. }
  13002. /**
  13003. * Update ARIA accessibility attributes
  13004. *
  13005. * @param {Event} [event]
  13006. * The `volumechange` event that caused this function to run.
  13007. *
  13008. * @listens Player#volumechange
  13009. */
  13010. updateARIAAttributes(event) {
  13011. const ariaValue = this.player_.muted() ? 0 : this.volumeAsPercentage_();
  13012. this.el_.setAttribute('aria-valuenow', ariaValue);
  13013. this.el_.setAttribute('aria-valuetext', ariaValue + '%');
  13014. }
  13015. /**
  13016. * Returns the current value of the player volume as a percentage
  13017. *
  13018. * @private
  13019. */
  13020. volumeAsPercentage_() {
  13021. return Math.round(this.player_.volume() * 100);
  13022. }
  13023. /**
  13024. * When user starts dragging the VolumeBar, store the volume and listen for
  13025. * the end of the drag. When the drag ends, if the volume was set to zero,
  13026. * set lastVolume to the stored volume.
  13027. *
  13028. * @listens slideractive
  13029. * @private
  13030. */
  13031. updateLastVolume_() {
  13032. const volumeBeforeDrag = this.player_.volume();
  13033. this.one('sliderinactive', () => {
  13034. if (this.player_.volume() === 0) {
  13035. this.player_.lastVolume_(volumeBeforeDrag);
  13036. }
  13037. });
  13038. }
  13039. }
  13040. /**
  13041. * Default options for the `VolumeBar`
  13042. *
  13043. * @type {Object}
  13044. * @private
  13045. */
  13046. VolumeBar.prototype.options_ = {
  13047. children: ['volumeLevel'],
  13048. barName: 'volumeLevel'
  13049. };
  13050. // MouseVolumeLevelDisplay tooltip should not be added to a player on mobile devices
  13051. if (!IS_IOS && !IS_ANDROID) {
  13052. VolumeBar.prototype.options_.children.splice(0, 0, 'mouseVolumeLevelDisplay');
  13053. }
  13054. /**
  13055. * Call the update event for this Slider when this event happens on the player.
  13056. *
  13057. * @type {string}
  13058. */
  13059. VolumeBar.prototype.playerEvent = 'volumechange';
  13060. Component.registerComponent('VolumeBar', VolumeBar);
  13061. /**
  13062. * @file volume-control.js
  13063. */
  13064. /**
  13065. * The component for controlling the volume level
  13066. *
  13067. * @extends Component
  13068. */
  13069. class VolumeControl extends Component {
  13070. /**
  13071. * Creates an instance of this class.
  13072. *
  13073. * @param { import('../../player').default } player
  13074. * The `Player` that this class should be attached to.
  13075. *
  13076. * @param {Object} [options={}]
  13077. * The key/value store of player options.
  13078. */
  13079. constructor(player, options = {}) {
  13080. options.vertical = options.vertical || false;
  13081. // Pass the vertical option down to the VolumeBar if
  13082. // the VolumeBar is turned on.
  13083. if (typeof options.volumeBar === 'undefined' || isPlain(options.volumeBar)) {
  13084. options.volumeBar = options.volumeBar || {};
  13085. options.volumeBar.vertical = options.vertical;
  13086. }
  13087. super(player, options);
  13088. // hide this control if volume support is missing
  13089. checkVolumeSupport(this, player);
  13090. this.throttledHandleMouseMove = throttle(bind_(this, this.handleMouseMove), UPDATE_REFRESH_INTERVAL);
  13091. this.handleMouseUpHandler_ = e => this.handleMouseUp(e);
  13092. this.on('mousedown', e => this.handleMouseDown(e));
  13093. this.on('touchstart', e => this.handleMouseDown(e));
  13094. this.on('mousemove', e => this.handleMouseMove(e));
  13095. // while the slider is active (the mouse has been pressed down and
  13096. // is dragging) or in focus we do not want to hide the VolumeBar
  13097. this.on(this.volumeBar, ['focus', 'slideractive'], () => {
  13098. this.volumeBar.addClass('vjs-slider-active');
  13099. this.addClass('vjs-slider-active');
  13100. this.trigger('slideractive');
  13101. });
  13102. this.on(this.volumeBar, ['blur', 'sliderinactive'], () => {
  13103. this.volumeBar.removeClass('vjs-slider-active');
  13104. this.removeClass('vjs-slider-active');
  13105. this.trigger('sliderinactive');
  13106. });
  13107. }
  13108. /**
  13109. * Create the `Component`'s DOM element
  13110. *
  13111. * @return {Element}
  13112. * The element that was created.
  13113. */
  13114. createEl() {
  13115. let orientationClass = 'vjs-volume-horizontal';
  13116. if (this.options_.vertical) {
  13117. orientationClass = 'vjs-volume-vertical';
  13118. }
  13119. return super.createEl('div', {
  13120. className: `vjs-volume-control vjs-control ${orientationClass}`
  13121. });
  13122. }
  13123. /**
  13124. * Handle `mousedown` or `touchstart` events on the `VolumeControl`.
  13125. *
  13126. * @param {Event} event
  13127. * `mousedown` or `touchstart` event that triggered this function
  13128. *
  13129. * @listens mousedown
  13130. * @listens touchstart
  13131. */
  13132. handleMouseDown(event) {
  13133. const doc = this.el_.ownerDocument;
  13134. this.on(doc, 'mousemove', this.throttledHandleMouseMove);
  13135. this.on(doc, 'touchmove', this.throttledHandleMouseMove);
  13136. this.on(doc, 'mouseup', this.handleMouseUpHandler_);
  13137. this.on(doc, 'touchend', this.handleMouseUpHandler_);
  13138. }
  13139. /**
  13140. * Handle `mouseup` or `touchend` events on the `VolumeControl`.
  13141. *
  13142. * @param {Event} event
  13143. * `mouseup` or `touchend` event that triggered this function.
  13144. *
  13145. * @listens touchend
  13146. * @listens mouseup
  13147. */
  13148. handleMouseUp(event) {
  13149. const doc = this.el_.ownerDocument;
  13150. this.off(doc, 'mousemove', this.throttledHandleMouseMove);
  13151. this.off(doc, 'touchmove', this.throttledHandleMouseMove);
  13152. this.off(doc, 'mouseup', this.handleMouseUpHandler_);
  13153. this.off(doc, 'touchend', this.handleMouseUpHandler_);
  13154. }
  13155. /**
  13156. * Handle `mousedown` or `touchstart` events on the `VolumeControl`.
  13157. *
  13158. * @param {Event} event
  13159. * `mousedown` or `touchstart` event that triggered this function
  13160. *
  13161. * @listens mousedown
  13162. * @listens touchstart
  13163. */
  13164. handleMouseMove(event) {
  13165. this.volumeBar.handleMouseMove(event);
  13166. }
  13167. }
  13168. /**
  13169. * Default options for the `VolumeControl`
  13170. *
  13171. * @type {Object}
  13172. * @private
  13173. */
  13174. VolumeControl.prototype.options_ = {
  13175. children: ['volumeBar']
  13176. };
  13177. Component.registerComponent('VolumeControl', VolumeControl);
  13178. /**
  13179. * Check if muting volume is supported and if it isn't hide the mute toggle
  13180. * button.
  13181. *
  13182. * @param { import('../../component').default } self
  13183. * A reference to the mute toggle button
  13184. *
  13185. * @param { import('../../player').default } player
  13186. * A reference to the player
  13187. *
  13188. * @private
  13189. */
  13190. const checkMuteSupport = function (self, player) {
  13191. // hide mute toggle button if it's not supported by the current tech
  13192. if (player.tech_ && !player.tech_.featuresMuteControl) {
  13193. self.addClass('vjs-hidden');
  13194. }
  13195. self.on(player, 'loadstart', function () {
  13196. if (!player.tech_.featuresMuteControl) {
  13197. self.addClass('vjs-hidden');
  13198. } else {
  13199. self.removeClass('vjs-hidden');
  13200. }
  13201. });
  13202. };
  13203. /**
  13204. * @file mute-toggle.js
  13205. */
  13206. /**
  13207. * A button component for muting the audio.
  13208. *
  13209. * @extends Button
  13210. */
  13211. class MuteToggle extends Button {
  13212. /**
  13213. * Creates an instance of this class.
  13214. *
  13215. * @param { import('./player').default } player
  13216. * The `Player` that this class should be attached to.
  13217. *
  13218. * @param {Object} [options]
  13219. * The key/value store of player options.
  13220. */
  13221. constructor(player, options) {
  13222. super(player, options);
  13223. // hide this control if volume support is missing
  13224. checkMuteSupport(this, player);
  13225. this.on(player, ['loadstart', 'volumechange'], e => this.update(e));
  13226. }
  13227. /**
  13228. * Builds the default DOM `className`.
  13229. *
  13230. * @return {string}
  13231. * The DOM `className` for this object.
  13232. */
  13233. buildCSSClass() {
  13234. return `vjs-mute-control ${super.buildCSSClass()}`;
  13235. }
  13236. /**
  13237. * This gets called when an `MuteToggle` is "clicked". See
  13238. * {@link ClickableComponent} for more detailed information on what a click can be.
  13239. *
  13240. * @param {Event} [event]
  13241. * The `keydown`, `tap`, or `click` event that caused this function to be
  13242. * called.
  13243. *
  13244. * @listens tap
  13245. * @listens click
  13246. */
  13247. handleClick(event) {
  13248. const vol = this.player_.volume();
  13249. const lastVolume = this.player_.lastVolume_();
  13250. if (vol === 0) {
  13251. const volumeToSet = lastVolume < 0.1 ? 0.1 : lastVolume;
  13252. this.player_.volume(volumeToSet);
  13253. this.player_.muted(false);
  13254. } else {
  13255. this.player_.muted(this.player_.muted() ? false : true);
  13256. }
  13257. }
  13258. /**
  13259. * Update the `MuteToggle` button based on the state of `volume` and `muted`
  13260. * on the player.
  13261. *
  13262. * @param {Event} [event]
  13263. * The {@link Player#loadstart} event if this function was called
  13264. * through an event.
  13265. *
  13266. * @listens Player#loadstart
  13267. * @listens Player#volumechange
  13268. */
  13269. update(event) {
  13270. this.updateIcon_();
  13271. this.updateControlText_();
  13272. }
  13273. /**
  13274. * Update the appearance of the `MuteToggle` icon.
  13275. *
  13276. * Possible states (given `level` variable below):
  13277. * - 0: crossed out
  13278. * - 1: zero bars of volume
  13279. * - 2: one bar of volume
  13280. * - 3: two bars of volume
  13281. *
  13282. * @private
  13283. */
  13284. updateIcon_() {
  13285. const vol = this.player_.volume();
  13286. let level = 3;
  13287. // in iOS when a player is loaded with muted attribute
  13288. // and volume is changed with a native mute button
  13289. // we want to make sure muted state is updated
  13290. if (IS_IOS && this.player_.tech_ && this.player_.tech_.el_) {
  13291. this.player_.muted(this.player_.tech_.el_.muted);
  13292. }
  13293. if (vol === 0 || this.player_.muted()) {
  13294. level = 0;
  13295. } else if (vol < 0.33) {
  13296. level = 1;
  13297. } else if (vol < 0.67) {
  13298. level = 2;
  13299. }
  13300. removeClass(this.el_, [0, 1, 2, 3].reduce((str, i) => str + `${i ? ' ' : ''}vjs-vol-${i}`, ''));
  13301. addClass(this.el_, `vjs-vol-${level}`);
  13302. }
  13303. /**
  13304. * If `muted` has changed on the player, update the control text
  13305. * (`title` attribute on `vjs-mute-control` element and content of
  13306. * `vjs-control-text` element).
  13307. *
  13308. * @private
  13309. */
  13310. updateControlText_() {
  13311. const soundOff = this.player_.muted() || this.player_.volume() === 0;
  13312. const text = soundOff ? 'Unmute' : 'Mute';
  13313. if (this.controlText() !== text) {
  13314. this.controlText(text);
  13315. }
  13316. }
  13317. }
  13318. /**
  13319. * The text that should display over the `MuteToggle`s controls. Added for localization.
  13320. *
  13321. * @type {string}
  13322. * @protected
  13323. */
  13324. MuteToggle.prototype.controlText_ = 'Mute';
  13325. Component.registerComponent('MuteToggle', MuteToggle);
  13326. /**
  13327. * @file volume-control.js
  13328. */
  13329. /**
  13330. * A Component to contain the MuteToggle and VolumeControl so that
  13331. * they can work together.
  13332. *
  13333. * @extends Component
  13334. */
  13335. class VolumePanel extends Component {
  13336. /**
  13337. * Creates an instance of this class.
  13338. *
  13339. * @param { import('./player').default } player
  13340. * The `Player` that this class should be attached to.
  13341. *
  13342. * @param {Object} [options={}]
  13343. * The key/value store of player options.
  13344. */
  13345. constructor(player, options = {}) {
  13346. if (typeof options.inline !== 'undefined') {
  13347. options.inline = options.inline;
  13348. } else {
  13349. options.inline = true;
  13350. }
  13351. // pass the inline option down to the VolumeControl as vertical if
  13352. // the VolumeControl is on.
  13353. if (typeof options.volumeControl === 'undefined' || isPlain(options.volumeControl)) {
  13354. options.volumeControl = options.volumeControl || {};
  13355. options.volumeControl.vertical = !options.inline;
  13356. }
  13357. super(player, options);
  13358. // this handler is used by mouse handler methods below
  13359. this.handleKeyPressHandler_ = e => this.handleKeyPress(e);
  13360. this.on(player, ['loadstart'], e => this.volumePanelState_(e));
  13361. this.on(this.muteToggle, 'keyup', e => this.handleKeyPress(e));
  13362. this.on(this.volumeControl, 'keyup', e => this.handleVolumeControlKeyUp(e));
  13363. this.on('keydown', e => this.handleKeyPress(e));
  13364. this.on('mouseover', e => this.handleMouseOver(e));
  13365. this.on('mouseout', e => this.handleMouseOut(e));
  13366. // while the slider is active (the mouse has been pressed down and
  13367. // is dragging) we do not want to hide the VolumeBar
  13368. this.on(this.volumeControl, ['slideractive'], this.sliderActive_);
  13369. this.on(this.volumeControl, ['sliderinactive'], this.sliderInactive_);
  13370. }
  13371. /**
  13372. * Add vjs-slider-active class to the VolumePanel
  13373. *
  13374. * @listens VolumeControl#slideractive
  13375. * @private
  13376. */
  13377. sliderActive_() {
  13378. this.addClass('vjs-slider-active');
  13379. }
  13380. /**
  13381. * Removes vjs-slider-active class to the VolumePanel
  13382. *
  13383. * @listens VolumeControl#sliderinactive
  13384. * @private
  13385. */
  13386. sliderInactive_() {
  13387. this.removeClass('vjs-slider-active');
  13388. }
  13389. /**
  13390. * Adds vjs-hidden or vjs-mute-toggle-only to the VolumePanel
  13391. * depending on MuteToggle and VolumeControl state
  13392. *
  13393. * @listens Player#loadstart
  13394. * @private
  13395. */
  13396. volumePanelState_() {
  13397. // hide volume panel if neither volume control or mute toggle
  13398. // are displayed
  13399. if (this.volumeControl.hasClass('vjs-hidden') && this.muteToggle.hasClass('vjs-hidden')) {
  13400. this.addClass('vjs-hidden');
  13401. }
  13402. // if only mute toggle is visible we don't want
  13403. // volume panel expanding when hovered or active
  13404. if (this.volumeControl.hasClass('vjs-hidden') && !this.muteToggle.hasClass('vjs-hidden')) {
  13405. this.addClass('vjs-mute-toggle-only');
  13406. }
  13407. }
  13408. /**
  13409. * Create the `Component`'s DOM element
  13410. *
  13411. * @return {Element}
  13412. * The element that was created.
  13413. */
  13414. createEl() {
  13415. let orientationClass = 'vjs-volume-panel-horizontal';
  13416. if (!this.options_.inline) {
  13417. orientationClass = 'vjs-volume-panel-vertical';
  13418. }
  13419. return super.createEl('div', {
  13420. className: `vjs-volume-panel vjs-control ${orientationClass}`
  13421. });
  13422. }
  13423. /**
  13424. * Dispose of the `volume-panel` and all child components.
  13425. */
  13426. dispose() {
  13427. this.handleMouseOut();
  13428. super.dispose();
  13429. }
  13430. /**
  13431. * Handles `keyup` events on the `VolumeControl`, looking for ESC, which closes
  13432. * the volume panel and sets focus on `MuteToggle`.
  13433. *
  13434. * @param {Event} event
  13435. * The `keyup` event that caused this function to be called.
  13436. *
  13437. * @listens keyup
  13438. */
  13439. handleVolumeControlKeyUp(event) {
  13440. if (keycode.isEventKey(event, 'Esc')) {
  13441. this.muteToggle.focus();
  13442. }
  13443. }
  13444. /**
  13445. * This gets called when a `VolumePanel` gains hover via a `mouseover` event.
  13446. * Turns on listening for `mouseover` event. When they happen it
  13447. * calls `this.handleMouseOver`.
  13448. *
  13449. * @param {Event} event
  13450. * The `mouseover` event that caused this function to be called.
  13451. *
  13452. * @listens mouseover
  13453. */
  13454. handleMouseOver(event) {
  13455. this.addClass('vjs-hover');
  13456. on(document, 'keyup', this.handleKeyPressHandler_);
  13457. }
  13458. /**
  13459. * This gets called when a `VolumePanel` gains hover via a `mouseout` event.
  13460. * Turns on listening for `mouseout` event. When they happen it
  13461. * calls `this.handleMouseOut`.
  13462. *
  13463. * @param {Event} event
  13464. * The `mouseout` event that caused this function to be called.
  13465. *
  13466. * @listens mouseout
  13467. */
  13468. handleMouseOut(event) {
  13469. this.removeClass('vjs-hover');
  13470. off(document, 'keyup', this.handleKeyPressHandler_);
  13471. }
  13472. /**
  13473. * Handles `keyup` event on the document or `keydown` event on the `VolumePanel`,
  13474. * looking for ESC, which hides the `VolumeControl`.
  13475. *
  13476. * @param {Event} event
  13477. * The keypress that triggered this event.
  13478. *
  13479. * @listens keydown | keyup
  13480. */
  13481. handleKeyPress(event) {
  13482. if (keycode.isEventKey(event, 'Esc')) {
  13483. this.handleMouseOut();
  13484. }
  13485. }
  13486. }
  13487. /**
  13488. * Default options for the `VolumeControl`
  13489. *
  13490. * @type {Object}
  13491. * @private
  13492. */
  13493. VolumePanel.prototype.options_ = {
  13494. children: ['muteToggle', 'volumeControl']
  13495. };
  13496. Component.registerComponent('VolumePanel', VolumePanel);
  13497. /**
  13498. * Button to skip forward a configurable amount of time
  13499. * through a video. Renders in the control bar.
  13500. *
  13501. * e.g. options: {controlBar: {skipButtons: forward: 5}}
  13502. *
  13503. * @extends Button
  13504. */
  13505. class SkipForward extends Button {
  13506. constructor(player, options) {
  13507. super(player, options);
  13508. this.validOptions = [5, 10, 30];
  13509. this.skipTime = this.getSkipForwardTime();
  13510. if (this.skipTime && this.validOptions.includes(this.skipTime)) {
  13511. this.controlText(this.localize('Skip forward {1} seconds', [this.skipTime]));
  13512. this.show();
  13513. } else {
  13514. this.hide();
  13515. }
  13516. }
  13517. getSkipForwardTime() {
  13518. const playerOptions = this.options_.playerOptions;
  13519. return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.forward;
  13520. }
  13521. buildCSSClass() {
  13522. return `vjs-skip-forward-${this.getSkipForwardTime()} ${super.buildCSSClass()}`;
  13523. }
  13524. /**
  13525. * On click, skips forward in the duration/seekable range by a configurable amount of seconds.
  13526. * If the time left in the duration/seekable range is less than the configured 'skip forward' time,
  13527. * skips to end of duration/seekable range.
  13528. *
  13529. * Handle a click on a `SkipForward` button
  13530. *
  13531. * @param {EventTarget~Event} event
  13532. * The `click` event that caused this function
  13533. * to be called
  13534. */
  13535. handleClick(event) {
  13536. const currentVideoTime = this.player_.currentTime();
  13537. const liveTracker = this.player_.liveTracker;
  13538. const duration = liveTracker && liveTracker.isLive() ? liveTracker.seekableEnd() : this.player_.duration();
  13539. let newTime;
  13540. if (currentVideoTime + this.skipTime <= duration) {
  13541. newTime = currentVideoTime + this.skipTime;
  13542. } else {
  13543. newTime = duration;
  13544. }
  13545. this.player_.currentTime(newTime);
  13546. }
  13547. /**
  13548. * Update control text on languagechange
  13549. */
  13550. handleLanguagechange() {
  13551. this.controlText(this.localize('Skip forward {1} seconds', [this.skipTime]));
  13552. }
  13553. }
  13554. Component.registerComponent('SkipForward', SkipForward);
  13555. /**
  13556. * Button to skip backward a configurable amount of time
  13557. * through a video. Renders in the control bar.
  13558. *
  13559. * * e.g. options: {controlBar: {skipButtons: backward: 5}}
  13560. *
  13561. * @extends Button
  13562. */
  13563. class SkipBackward extends Button {
  13564. constructor(player, options) {
  13565. super(player, options);
  13566. this.validOptions = [5, 10, 30];
  13567. this.skipTime = this.getSkipBackwardTime();
  13568. if (this.skipTime && this.validOptions.includes(this.skipTime)) {
  13569. this.controlText(this.localize('Skip backward {1} seconds', [this.skipTime]));
  13570. this.show();
  13571. } else {
  13572. this.hide();
  13573. }
  13574. }
  13575. getSkipBackwardTime() {
  13576. const playerOptions = this.options_.playerOptions;
  13577. return playerOptions.controlBar && playerOptions.controlBar.skipButtons && playerOptions.controlBar.skipButtons.backward;
  13578. }
  13579. buildCSSClass() {
  13580. return `vjs-skip-backward-${this.getSkipBackwardTime()} ${super.buildCSSClass()}`;
  13581. }
  13582. /**
  13583. * On click, skips backward in the video by a configurable amount of seconds.
  13584. * If the current time in the video is less than the configured 'skip backward' time,
  13585. * skips to beginning of video or seekable range.
  13586. *
  13587. * Handle a click on a `SkipBackward` button
  13588. *
  13589. * @param {EventTarget~Event} event
  13590. * The `click` event that caused this function
  13591. * to be called
  13592. */
  13593. handleClick(event) {
  13594. const currentVideoTime = this.player_.currentTime();
  13595. const liveTracker = this.player_.liveTracker;
  13596. const seekableStart = liveTracker && liveTracker.isLive() && liveTracker.seekableStart();
  13597. let newTime;
  13598. if (seekableStart && currentVideoTime - this.skipTime <= seekableStart) {
  13599. newTime = seekableStart;
  13600. } else if (currentVideoTime >= this.skipTime) {
  13601. newTime = currentVideoTime - this.skipTime;
  13602. } else {
  13603. newTime = 0;
  13604. }
  13605. this.player_.currentTime(newTime);
  13606. }
  13607. /**
  13608. * Update control text on languagechange
  13609. */
  13610. handleLanguagechange() {
  13611. this.controlText(this.localize('Skip backward {1} seconds', [this.skipTime]));
  13612. }
  13613. }
  13614. SkipBackward.prototype.controlText_ = 'Skip Backward';
  13615. Component.registerComponent('SkipBackward', SkipBackward);
  13616. /**
  13617. * @file menu.js
  13618. */
  13619. /**
  13620. * The Menu component is used to build popup menus, including subtitle and
  13621. * captions selection menus.
  13622. *
  13623. * @extends Component
  13624. */
  13625. class Menu extends Component {
  13626. /**
  13627. * Create an instance of this class.
  13628. *
  13629. * @param { import('../player').default } player
  13630. * the player that this component should attach to
  13631. *
  13632. * @param {Object} [options]
  13633. * Object of option names and values
  13634. *
  13635. */
  13636. constructor(player, options) {
  13637. super(player, options);
  13638. if (options) {
  13639. this.menuButton_ = options.menuButton;
  13640. }
  13641. this.focusedChild_ = -1;
  13642. this.on('keydown', e => this.handleKeyDown(e));
  13643. // All the menu item instances share the same blur handler provided by the menu container.
  13644. this.boundHandleBlur_ = e => this.handleBlur(e);
  13645. this.boundHandleTapClick_ = e => this.handleTapClick(e);
  13646. }
  13647. /**
  13648. * Add event listeners to the {@link MenuItem}.
  13649. *
  13650. * @param {Object} component
  13651. * The instance of the `MenuItem` to add listeners to.
  13652. *
  13653. */
  13654. addEventListenerForItem(component) {
  13655. if (!(component instanceof Component)) {
  13656. return;
  13657. }
  13658. this.on(component, 'blur', this.boundHandleBlur_);
  13659. this.on(component, ['tap', 'click'], this.boundHandleTapClick_);
  13660. }
  13661. /**
  13662. * Remove event listeners from the {@link MenuItem}.
  13663. *
  13664. * @param {Object} component
  13665. * The instance of the `MenuItem` to remove listeners.
  13666. *
  13667. */
  13668. removeEventListenerForItem(component) {
  13669. if (!(component instanceof Component)) {
  13670. return;
  13671. }
  13672. this.off(component, 'blur', this.boundHandleBlur_);
  13673. this.off(component, ['tap', 'click'], this.boundHandleTapClick_);
  13674. }
  13675. /**
  13676. * This method will be called indirectly when the component has been added
  13677. * before the component adds to the new menu instance by `addItem`.
  13678. * In this case, the original menu instance will remove the component
  13679. * by calling `removeChild`.
  13680. *
  13681. * @param {Object} component
  13682. * The instance of the `MenuItem`
  13683. */
  13684. removeChild(component) {
  13685. if (typeof component === 'string') {
  13686. component = this.getChild(component);
  13687. }
  13688. this.removeEventListenerForItem(component);
  13689. super.removeChild(component);
  13690. }
  13691. /**
  13692. * Add a {@link MenuItem} to the menu.
  13693. *
  13694. * @param {Object|string} component
  13695. * The name or instance of the `MenuItem` to add.
  13696. *
  13697. */
  13698. addItem(component) {
  13699. const childComponent = this.addChild(component);
  13700. if (childComponent) {
  13701. this.addEventListenerForItem(childComponent);
  13702. }
  13703. }
  13704. /**
  13705. * Create the `Menu`s DOM element.
  13706. *
  13707. * @return {Element}
  13708. * the element that was created
  13709. */
  13710. createEl() {
  13711. const contentElType = this.options_.contentElType || 'ul';
  13712. this.contentEl_ = createEl(contentElType, {
  13713. className: 'vjs-menu-content'
  13714. });
  13715. this.contentEl_.setAttribute('role', 'menu');
  13716. const el = super.createEl('div', {
  13717. append: this.contentEl_,
  13718. className: 'vjs-menu'
  13719. });
  13720. el.appendChild(this.contentEl_);
  13721. // Prevent clicks from bubbling up. Needed for Menu Buttons,
  13722. // where a click on the parent is significant
  13723. on(el, 'click', function (event) {
  13724. event.preventDefault();
  13725. event.stopImmediatePropagation();
  13726. });
  13727. return el;
  13728. }
  13729. dispose() {
  13730. this.contentEl_ = null;
  13731. this.boundHandleBlur_ = null;
  13732. this.boundHandleTapClick_ = null;
  13733. super.dispose();
  13734. }
  13735. /**
  13736. * Called when a `MenuItem` loses focus.
  13737. *
  13738. * @param {Event} event
  13739. * The `blur` event that caused this function to be called.
  13740. *
  13741. * @listens blur
  13742. */
  13743. handleBlur(event) {
  13744. const relatedTarget = event.relatedTarget || document.activeElement;
  13745. // Close menu popup when a user clicks outside the menu
  13746. if (!this.children().some(element => {
  13747. return element.el() === relatedTarget;
  13748. })) {
  13749. const btn = this.menuButton_;
  13750. if (btn && btn.buttonPressed_ && relatedTarget !== btn.el().firstChild) {
  13751. btn.unpressButton();
  13752. }
  13753. }
  13754. }
  13755. /**
  13756. * Called when a `MenuItem` gets clicked or tapped.
  13757. *
  13758. * @param {Event} event
  13759. * The `click` or `tap` event that caused this function to be called.
  13760. *
  13761. * @listens click,tap
  13762. */
  13763. handleTapClick(event) {
  13764. // Unpress the associated MenuButton, and move focus back to it
  13765. if (this.menuButton_) {
  13766. this.menuButton_.unpressButton();
  13767. const childComponents = this.children();
  13768. if (!Array.isArray(childComponents)) {
  13769. return;
  13770. }
  13771. const foundComponent = childComponents.filter(component => component.el() === event.target)[0];
  13772. if (!foundComponent) {
  13773. return;
  13774. }
  13775. // don't focus menu button if item is a caption settings item
  13776. // because focus will move elsewhere
  13777. if (foundComponent.name() !== 'CaptionSettingsMenuItem') {
  13778. this.menuButton_.focus();
  13779. }
  13780. }
  13781. }
  13782. /**
  13783. * Handle a `keydown` event on this menu. This listener is added in the constructor.
  13784. *
  13785. * @param {Event} event
  13786. * A `keydown` event that happened on the menu.
  13787. *
  13788. * @listens keydown
  13789. */
  13790. handleKeyDown(event) {
  13791. // Left and Down Arrows
  13792. if (keycode.isEventKey(event, 'Left') || keycode.isEventKey(event, 'Down')) {
  13793. event.preventDefault();
  13794. event.stopPropagation();
  13795. this.stepForward();
  13796. // Up and Right Arrows
  13797. } else if (keycode.isEventKey(event, 'Right') || keycode.isEventKey(event, 'Up')) {
  13798. event.preventDefault();
  13799. event.stopPropagation();
  13800. this.stepBack();
  13801. }
  13802. }
  13803. /**
  13804. * Move to next (lower) menu item for keyboard users.
  13805. */
  13806. stepForward() {
  13807. let stepChild = 0;
  13808. if (this.focusedChild_ !== undefined) {
  13809. stepChild = this.focusedChild_ + 1;
  13810. }
  13811. this.focus(stepChild);
  13812. }
  13813. /**
  13814. * Move to previous (higher) menu item for keyboard users.
  13815. */
  13816. stepBack() {
  13817. let stepChild = 0;
  13818. if (this.focusedChild_ !== undefined) {
  13819. stepChild = this.focusedChild_ - 1;
  13820. }
  13821. this.focus(stepChild);
  13822. }
  13823. /**
  13824. * Set focus on a {@link MenuItem} in the `Menu`.
  13825. *
  13826. * @param {Object|string} [item=0]
  13827. * Index of child item set focus on.
  13828. */
  13829. focus(item = 0) {
  13830. const children = this.children().slice();
  13831. const haveTitle = children.length && children[0].hasClass('vjs-menu-title');
  13832. if (haveTitle) {
  13833. children.shift();
  13834. }
  13835. if (children.length > 0) {
  13836. if (item < 0) {
  13837. item = 0;
  13838. } else if (item >= children.length) {
  13839. item = children.length - 1;
  13840. }
  13841. this.focusedChild_ = item;
  13842. children[item].el_.focus();
  13843. }
  13844. }
  13845. }
  13846. Component.registerComponent('Menu', Menu);
  13847. /**
  13848. * @file menu-button.js
  13849. */
  13850. /**
  13851. * A `MenuButton` class for any popup {@link Menu}.
  13852. *
  13853. * @extends Component
  13854. */
  13855. class MenuButton extends Component {
  13856. /**
  13857. * Creates an instance of this class.
  13858. *
  13859. * @param { import('../player').default } player
  13860. * The `Player` that this class should be attached to.
  13861. *
  13862. * @param {Object} [options={}]
  13863. * The key/value store of player options.
  13864. */
  13865. constructor(player, options = {}) {
  13866. super(player, options);
  13867. this.menuButton_ = new Button(player, options);
  13868. this.menuButton_.controlText(this.controlText_);
  13869. this.menuButton_.el_.setAttribute('aria-haspopup', 'true');
  13870. // Add buildCSSClass values to the button, not the wrapper
  13871. const buttonClass = Button.prototype.buildCSSClass();
  13872. this.menuButton_.el_.className = this.buildCSSClass() + ' ' + buttonClass;
  13873. this.menuButton_.removeClass('vjs-control');
  13874. this.addChild(this.menuButton_);
  13875. this.update();
  13876. this.enabled_ = true;
  13877. const handleClick = e => this.handleClick(e);
  13878. this.handleMenuKeyUp_ = e => this.handleMenuKeyUp(e);
  13879. this.on(this.menuButton_, 'tap', handleClick);
  13880. this.on(this.menuButton_, 'click', handleClick);
  13881. this.on(this.menuButton_, 'keydown', e => this.handleKeyDown(e));
  13882. this.on(this.menuButton_, 'mouseenter', () => {
  13883. this.addClass('vjs-hover');
  13884. this.menu.show();
  13885. on(document, 'keyup', this.handleMenuKeyUp_);
  13886. });
  13887. this.on('mouseleave', e => this.handleMouseLeave(e));
  13888. this.on('keydown', e => this.handleSubmenuKeyDown(e));
  13889. }
  13890. /**
  13891. * Update the menu based on the current state of its items.
  13892. */
  13893. update() {
  13894. const menu = this.createMenu();
  13895. if (this.menu) {
  13896. this.menu.dispose();
  13897. this.removeChild(this.menu);
  13898. }
  13899. this.menu = menu;
  13900. this.addChild(menu);
  13901. /**
  13902. * Track the state of the menu button
  13903. *
  13904. * @type {Boolean}
  13905. * @private
  13906. */
  13907. this.buttonPressed_ = false;
  13908. this.menuButton_.el_.setAttribute('aria-expanded', 'false');
  13909. if (this.items && this.items.length <= this.hideThreshold_) {
  13910. this.hide();
  13911. this.menu.contentEl_.removeAttribute('role');
  13912. } else {
  13913. this.show();
  13914. this.menu.contentEl_.setAttribute('role', 'menu');
  13915. }
  13916. }
  13917. /**
  13918. * Create the menu and add all items to it.
  13919. *
  13920. * @return {Menu}
  13921. * The constructed menu
  13922. */
  13923. createMenu() {
  13924. const menu = new Menu(this.player_, {
  13925. menuButton: this
  13926. });
  13927. /**
  13928. * Hide the menu if the number of items is less than or equal to this threshold. This defaults
  13929. * to 0 and whenever we add items which can be hidden to the menu we'll increment it. We list
  13930. * it here because every time we run `createMenu` we need to reset the value.
  13931. *
  13932. * @protected
  13933. * @type {Number}
  13934. */
  13935. this.hideThreshold_ = 0;
  13936. // Add a title list item to the top
  13937. if (this.options_.title) {
  13938. const titleEl = createEl('li', {
  13939. className: 'vjs-menu-title',
  13940. textContent: toTitleCase(this.options_.title),
  13941. tabIndex: -1
  13942. });
  13943. const titleComponent = new Component(this.player_, {
  13944. el: titleEl
  13945. });
  13946. menu.addItem(titleComponent);
  13947. }
  13948. this.items = this.createItems();
  13949. if (this.items) {
  13950. // Add menu items to the menu
  13951. for (let i = 0; i < this.items.length; i++) {
  13952. menu.addItem(this.items[i]);
  13953. }
  13954. }
  13955. return menu;
  13956. }
  13957. /**
  13958. * Create the list of menu items. Specific to each subclass.
  13959. *
  13960. * @abstract
  13961. */
  13962. createItems() {}
  13963. /**
  13964. * Create the `MenuButtons`s DOM element.
  13965. *
  13966. * @return {Element}
  13967. * The element that gets created.
  13968. */
  13969. createEl() {
  13970. return super.createEl('div', {
  13971. className: this.buildWrapperCSSClass()
  13972. }, {});
  13973. }
  13974. /**
  13975. * Allow sub components to stack CSS class names for the wrapper element
  13976. *
  13977. * @return {string}
  13978. * The constructed wrapper DOM `className`
  13979. */
  13980. buildWrapperCSSClass() {
  13981. let menuButtonClass = 'vjs-menu-button';
  13982. // If the inline option is passed, we want to use different styles altogether.
  13983. if (this.options_.inline === true) {
  13984. menuButtonClass += '-inline';
  13985. } else {
  13986. menuButtonClass += '-popup';
  13987. }
  13988. // TODO: Fix the CSS so that this isn't necessary
  13989. const buttonClass = Button.prototype.buildCSSClass();
  13990. return `vjs-menu-button ${menuButtonClass} ${buttonClass} ${super.buildCSSClass()}`;
  13991. }
  13992. /**
  13993. * Builds the default DOM `className`.
  13994. *
  13995. * @return {string}
  13996. * The DOM `className` for this object.
  13997. */
  13998. buildCSSClass() {
  13999. let menuButtonClass = 'vjs-menu-button';
  14000. // If the inline option is passed, we want to use different styles altogether.
  14001. if (this.options_.inline === true) {
  14002. menuButtonClass += '-inline';
  14003. } else {
  14004. menuButtonClass += '-popup';
  14005. }
  14006. return `vjs-menu-button ${menuButtonClass} ${super.buildCSSClass()}`;
  14007. }
  14008. /**
  14009. * Get or set the localized control text that will be used for accessibility.
  14010. *
  14011. * > NOTE: This will come from the internal `menuButton_` element.
  14012. *
  14013. * @param {string} [text]
  14014. * Control text for element.
  14015. *
  14016. * @param {Element} [el=this.menuButton_.el()]
  14017. * Element to set the title on.
  14018. *
  14019. * @return {string}
  14020. * - The control text when getting
  14021. */
  14022. controlText(text, el = this.menuButton_.el()) {
  14023. return this.menuButton_.controlText(text, el);
  14024. }
  14025. /**
  14026. * Dispose of the `menu-button` and all child components.
  14027. */
  14028. dispose() {
  14029. this.handleMouseLeave();
  14030. super.dispose();
  14031. }
  14032. /**
  14033. * Handle a click on a `MenuButton`.
  14034. * See {@link ClickableComponent#handleClick} for instances where this is called.
  14035. *
  14036. * @param {Event} event
  14037. * The `keydown`, `tap`, or `click` event that caused this function to be
  14038. * called.
  14039. *
  14040. * @listens tap
  14041. * @listens click
  14042. */
  14043. handleClick(event) {
  14044. if (this.buttonPressed_) {
  14045. this.unpressButton();
  14046. } else {
  14047. this.pressButton();
  14048. }
  14049. }
  14050. /**
  14051. * Handle `mouseleave` for `MenuButton`.
  14052. *
  14053. * @param {Event} event
  14054. * The `mouseleave` event that caused this function to be called.
  14055. *
  14056. * @listens mouseleave
  14057. */
  14058. handleMouseLeave(event) {
  14059. this.removeClass('vjs-hover');
  14060. off(document, 'keyup', this.handleMenuKeyUp_);
  14061. }
  14062. /**
  14063. * Set the focus to the actual button, not to this element
  14064. */
  14065. focus() {
  14066. this.menuButton_.focus();
  14067. }
  14068. /**
  14069. * Remove the focus from the actual button, not this element
  14070. */
  14071. blur() {
  14072. this.menuButton_.blur();
  14073. }
  14074. /**
  14075. * Handle tab, escape, down arrow, and up arrow keys for `MenuButton`. See
  14076. * {@link ClickableComponent#handleKeyDown} for instances where this is called.
  14077. *
  14078. * @param {Event} event
  14079. * The `keydown` event that caused this function to be called.
  14080. *
  14081. * @listens keydown
  14082. */
  14083. handleKeyDown(event) {
  14084. // Escape or Tab unpress the 'button'
  14085. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  14086. if (this.buttonPressed_) {
  14087. this.unpressButton();
  14088. }
  14089. // Don't preventDefault for Tab key - we still want to lose focus
  14090. if (!keycode.isEventKey(event, 'Tab')) {
  14091. event.preventDefault();
  14092. // Set focus back to the menu button's button
  14093. this.menuButton_.focus();
  14094. }
  14095. // Up Arrow or Down Arrow also 'press' the button to open the menu
  14096. } else if (keycode.isEventKey(event, 'Up') || keycode.isEventKey(event, 'Down')) {
  14097. if (!this.buttonPressed_) {
  14098. event.preventDefault();
  14099. this.pressButton();
  14100. }
  14101. }
  14102. }
  14103. /**
  14104. * Handle a `keyup` event on a `MenuButton`. The listener for this is added in
  14105. * the constructor.
  14106. *
  14107. * @param {Event} event
  14108. * Key press event
  14109. *
  14110. * @listens keyup
  14111. */
  14112. handleMenuKeyUp(event) {
  14113. // Escape hides popup menu
  14114. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  14115. this.removeClass('vjs-hover');
  14116. }
  14117. }
  14118. /**
  14119. * This method name now delegates to `handleSubmenuKeyDown`. This means
  14120. * anyone calling `handleSubmenuKeyPress` will not see their method calls
  14121. * stop working.
  14122. *
  14123. * @param {Event} event
  14124. * The event that caused this function to be called.
  14125. */
  14126. handleSubmenuKeyPress(event) {
  14127. this.handleSubmenuKeyDown(event);
  14128. }
  14129. /**
  14130. * Handle a `keydown` event on a sub-menu. The listener for this is added in
  14131. * the constructor.
  14132. *
  14133. * @param {Event} event
  14134. * Key press event
  14135. *
  14136. * @listens keydown
  14137. */
  14138. handleSubmenuKeyDown(event) {
  14139. // Escape or Tab unpress the 'button'
  14140. if (keycode.isEventKey(event, 'Esc') || keycode.isEventKey(event, 'Tab')) {
  14141. if (this.buttonPressed_) {
  14142. this.unpressButton();
  14143. }
  14144. // Don't preventDefault for Tab key - we still want to lose focus
  14145. if (!keycode.isEventKey(event, 'Tab')) {
  14146. event.preventDefault();
  14147. // Set focus back to the menu button's button
  14148. this.menuButton_.focus();
  14149. }
  14150. }
  14151. }
  14152. /**
  14153. * Put the current `MenuButton` into a pressed state.
  14154. */
  14155. pressButton() {
  14156. if (this.enabled_) {
  14157. this.buttonPressed_ = true;
  14158. this.menu.show();
  14159. this.menu.lockShowing();
  14160. this.menuButton_.el_.setAttribute('aria-expanded', 'true');
  14161. // set the focus into the submenu, except on iOS where it is resulting in
  14162. // undesired scrolling behavior when the player is in an iframe
  14163. if (IS_IOS && isInFrame()) {
  14164. // Return early so that the menu isn't focused
  14165. return;
  14166. }
  14167. this.menu.focus();
  14168. }
  14169. }
  14170. /**
  14171. * Take the current `MenuButton` out of a pressed state.
  14172. */
  14173. unpressButton() {
  14174. if (this.enabled_) {
  14175. this.buttonPressed_ = false;
  14176. this.menu.unlockShowing();
  14177. this.menu.hide();
  14178. this.menuButton_.el_.setAttribute('aria-expanded', 'false');
  14179. }
  14180. }
  14181. /**
  14182. * Disable the `MenuButton`. Don't allow it to be clicked.
  14183. */
  14184. disable() {
  14185. this.unpressButton();
  14186. this.enabled_ = false;
  14187. this.addClass('vjs-disabled');
  14188. this.menuButton_.disable();
  14189. }
  14190. /**
  14191. * Enable the `MenuButton`. Allow it to be clicked.
  14192. */
  14193. enable() {
  14194. this.enabled_ = true;
  14195. this.removeClass('vjs-disabled');
  14196. this.menuButton_.enable();
  14197. }
  14198. }
  14199. Component.registerComponent('MenuButton', MenuButton);
  14200. /**
  14201. * @file track-button.js
  14202. */
  14203. /**
  14204. * The base class for buttons that toggle specific track types (e.g. subtitles).
  14205. *
  14206. * @extends MenuButton
  14207. */
  14208. class TrackButton extends MenuButton {
  14209. /**
  14210. * Creates an instance of this class.
  14211. *
  14212. * @param { import('./player').default } player
  14213. * The `Player` that this class should be attached to.
  14214. *
  14215. * @param {Object} [options]
  14216. * The key/value store of player options.
  14217. */
  14218. constructor(player, options) {
  14219. const tracks = options.tracks;
  14220. super(player, options);
  14221. if (this.items.length <= 1) {
  14222. this.hide();
  14223. }
  14224. if (!tracks) {
  14225. return;
  14226. }
  14227. const updateHandler = bind_(this, this.update);
  14228. tracks.addEventListener('removetrack', updateHandler);
  14229. tracks.addEventListener('addtrack', updateHandler);
  14230. tracks.addEventListener('labelchange', updateHandler);
  14231. this.player_.on('ready', updateHandler);
  14232. this.player_.on('dispose', function () {
  14233. tracks.removeEventListener('removetrack', updateHandler);
  14234. tracks.removeEventListener('addtrack', updateHandler);
  14235. tracks.removeEventListener('labelchange', updateHandler);
  14236. });
  14237. }
  14238. }
  14239. Component.registerComponent('TrackButton', TrackButton);
  14240. /**
  14241. * @file menu-keys.js
  14242. */
  14243. /**
  14244. * All keys used for operation of a menu (`MenuButton`, `Menu`, and `MenuItem`)
  14245. * Note that 'Enter' and 'Space' are not included here (otherwise they would
  14246. * prevent the `MenuButton` and `MenuItem` from being keyboard-clickable)
  14247. *
  14248. * @typedef MenuKeys
  14249. * @array
  14250. */
  14251. const MenuKeys = ['Tab', 'Esc', 'Up', 'Down', 'Right', 'Left'];
  14252. /**
  14253. * @file menu-item.js
  14254. */
  14255. /**
  14256. * The component for a menu item. `<li>`
  14257. *
  14258. * @extends ClickableComponent
  14259. */
  14260. class MenuItem extends ClickableComponent {
  14261. /**
  14262. * Creates an instance of the this class.
  14263. *
  14264. * @param { import('../player').default } player
  14265. * The `Player` that this class should be attached to.
  14266. *
  14267. * @param {Object} [options={}]
  14268. * The key/value store of player options.
  14269. *
  14270. */
  14271. constructor(player, options) {
  14272. super(player, options);
  14273. this.selectable = options.selectable;
  14274. this.isSelected_ = options.selected || false;
  14275. this.multiSelectable = options.multiSelectable;
  14276. this.selected(this.isSelected_);
  14277. if (this.selectable) {
  14278. if (this.multiSelectable) {
  14279. this.el_.setAttribute('role', 'menuitemcheckbox');
  14280. } else {
  14281. this.el_.setAttribute('role', 'menuitemradio');
  14282. }
  14283. } else {
  14284. this.el_.setAttribute('role', 'menuitem');
  14285. }
  14286. }
  14287. /**
  14288. * Create the `MenuItem's DOM element
  14289. *
  14290. * @param {string} [type=li]
  14291. * Element's node type, not actually used, always set to `li`.
  14292. *
  14293. * @param {Object} [props={}]
  14294. * An object of properties that should be set on the element
  14295. *
  14296. * @param {Object} [attrs={}]
  14297. * An object of attributes that should be set on the element
  14298. *
  14299. * @return {Element}
  14300. * The element that gets created.
  14301. */
  14302. createEl(type, props, attrs) {
  14303. // The control is textual, not just an icon
  14304. this.nonIconControl = true;
  14305. const el = super.createEl('li', Object.assign({
  14306. className: 'vjs-menu-item',
  14307. tabIndex: -1
  14308. }, props), attrs);
  14309. // swap icon with menu item text.
  14310. el.replaceChild(createEl('span', {
  14311. className: 'vjs-menu-item-text',
  14312. textContent: this.localize(this.options_.label)
  14313. }), el.querySelector('.vjs-icon-placeholder'));
  14314. return el;
  14315. }
  14316. /**
  14317. * Ignore keys which are used by the menu, but pass any other ones up. See
  14318. * {@link ClickableComponent#handleKeyDown} for instances where this is called.
  14319. *
  14320. * @param {Event} event
  14321. * The `keydown` event that caused this function to be called.
  14322. *
  14323. * @listens keydown
  14324. */
  14325. handleKeyDown(event) {
  14326. if (!MenuKeys.some(key => keycode.isEventKey(event, key))) {
  14327. // Pass keydown handling up for unused keys
  14328. super.handleKeyDown(event);
  14329. }
  14330. }
  14331. /**
  14332. * Any click on a `MenuItem` puts it into the selected state.
  14333. * See {@link ClickableComponent#handleClick} for instances where this is called.
  14334. *
  14335. * @param {Event} event
  14336. * The `keydown`, `tap`, or `click` event that caused this function to be
  14337. * called.
  14338. *
  14339. * @listens tap
  14340. * @listens click
  14341. */
  14342. handleClick(event) {
  14343. this.selected(true);
  14344. }
  14345. /**
  14346. * Set the state for this menu item as selected or not.
  14347. *
  14348. * @param {boolean} selected
  14349. * if the menu item is selected or not
  14350. */
  14351. selected(selected) {
  14352. if (this.selectable) {
  14353. if (selected) {
  14354. this.addClass('vjs-selected');
  14355. this.el_.setAttribute('aria-checked', 'true');
  14356. // aria-checked isn't fully supported by browsers/screen readers,
  14357. // so indicate selected state to screen reader in the control text.
  14358. this.controlText(', selected');
  14359. this.isSelected_ = true;
  14360. } else {
  14361. this.removeClass('vjs-selected');
  14362. this.el_.setAttribute('aria-checked', 'false');
  14363. // Indicate un-selected state to screen reader
  14364. this.controlText('');
  14365. this.isSelected_ = false;
  14366. }
  14367. }
  14368. }
  14369. }
  14370. Component.registerComponent('MenuItem', MenuItem);
  14371. /**
  14372. * @file text-track-menu-item.js
  14373. */
  14374. /**
  14375. * The specific menu item type for selecting a language within a text track kind
  14376. *
  14377. * @extends MenuItem
  14378. */
  14379. class TextTrackMenuItem extends MenuItem {
  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. const track = options.track;
  14391. const tracks = player.textTracks();
  14392. // Modify options for parent MenuItem class's init.
  14393. options.label = track.label || track.language || 'Unknown';
  14394. options.selected = track.mode === 'showing';
  14395. super(player, options);
  14396. this.track = track;
  14397. // Determine the relevant kind(s) of tracks for this component and filter
  14398. // out empty kinds.
  14399. this.kinds = (options.kinds || [options.kind || this.track.kind]).filter(Boolean);
  14400. const changeHandler = (...args) => {
  14401. this.handleTracksChange.apply(this, args);
  14402. };
  14403. const selectedLanguageChangeHandler = (...args) => {
  14404. this.handleSelectedLanguageChange.apply(this, args);
  14405. };
  14406. player.on(['loadstart', 'texttrackchange'], changeHandler);
  14407. tracks.addEventListener('change', changeHandler);
  14408. tracks.addEventListener('selectedlanguagechange', selectedLanguageChangeHandler);
  14409. this.on('dispose', function () {
  14410. player.off(['loadstart', 'texttrackchange'], changeHandler);
  14411. tracks.removeEventListener('change', changeHandler);
  14412. tracks.removeEventListener('selectedlanguagechange', selectedLanguageChangeHandler);
  14413. });
  14414. // iOS7 doesn't dispatch change events to TextTrackLists when an
  14415. // associated track's mode changes. Without something like
  14416. // Object.observe() (also not present on iOS7), it's not
  14417. // possible to detect changes to the mode attribute and polyfill
  14418. // the change event. As a poor substitute, we manually dispatch
  14419. // change events whenever the controls modify the mode.
  14420. if (tracks.onchange === undefined) {
  14421. let event;
  14422. this.on(['tap', 'click'], function () {
  14423. if (typeof window.Event !== 'object') {
  14424. // Android 2.3 throws an Illegal Constructor error for window.Event
  14425. try {
  14426. event = new window.Event('change');
  14427. } catch (err) {
  14428. // continue regardless of error
  14429. }
  14430. }
  14431. if (!event) {
  14432. event = document.createEvent('Event');
  14433. event.initEvent('change', true, true);
  14434. }
  14435. tracks.dispatchEvent(event);
  14436. });
  14437. }
  14438. // set the default state based on current tracks
  14439. this.handleTracksChange();
  14440. }
  14441. /**
  14442. * This gets called when an `TextTrackMenuItem` is "clicked". See
  14443. * {@link ClickableComponent} for more detailed information on what a click can be.
  14444. *
  14445. * @param {Event} event
  14446. * The `keydown`, `tap`, or `click` event that caused this function to be
  14447. * called.
  14448. *
  14449. * @listens tap
  14450. * @listens click
  14451. */
  14452. handleClick(event) {
  14453. const referenceTrack = this.track;
  14454. const tracks = this.player_.textTracks();
  14455. super.handleClick(event);
  14456. if (!tracks) {
  14457. return;
  14458. }
  14459. for (let i = 0; i < tracks.length; i++) {
  14460. const track = tracks[i];
  14461. // If the track from the text tracks list is not of the right kind,
  14462. // skip it. We do not want to affect tracks of incompatible kind(s).
  14463. if (this.kinds.indexOf(track.kind) === -1) {
  14464. continue;
  14465. }
  14466. // If this text track is the component's track and it is not showing,
  14467. // set it to showing.
  14468. if (track === referenceTrack) {
  14469. if (track.mode !== 'showing') {
  14470. track.mode = 'showing';
  14471. }
  14472. // If this text track is not the component's track and it is not
  14473. // disabled, set it to disabled.
  14474. } else if (track.mode !== 'disabled') {
  14475. track.mode = 'disabled';
  14476. }
  14477. }
  14478. }
  14479. /**
  14480. * Handle text track list change
  14481. *
  14482. * @param {Event} event
  14483. * The `change` event that caused this function to be called.
  14484. *
  14485. * @listens TextTrackList#change
  14486. */
  14487. handleTracksChange(event) {
  14488. const shouldBeSelected = this.track.mode === 'showing';
  14489. // Prevent redundant selected() calls because they may cause
  14490. // screen readers to read the appended control text unnecessarily
  14491. if (shouldBeSelected !== this.isSelected_) {
  14492. this.selected(shouldBeSelected);
  14493. }
  14494. }
  14495. handleSelectedLanguageChange(event) {
  14496. if (this.track.mode === 'showing') {
  14497. const selectedLanguage = this.player_.cache_.selectedLanguage;
  14498. // Don't replace the kind of track across the same language
  14499. if (selectedLanguage && selectedLanguage.enabled && selectedLanguage.language === this.track.language && selectedLanguage.kind !== this.track.kind) {
  14500. return;
  14501. }
  14502. this.player_.cache_.selectedLanguage = {
  14503. enabled: true,
  14504. language: this.track.language,
  14505. kind: this.track.kind
  14506. };
  14507. }
  14508. }
  14509. dispose() {
  14510. // remove reference to track object on dispose
  14511. this.track = null;
  14512. super.dispose();
  14513. }
  14514. }
  14515. Component.registerComponent('TextTrackMenuItem', TextTrackMenuItem);
  14516. /**
  14517. * @file off-text-track-menu-item.js
  14518. */
  14519. /**
  14520. * A special menu item for turning of a specific type of text track
  14521. *
  14522. * @extends TextTrackMenuItem
  14523. */
  14524. class OffTextTrackMenuItem extends TextTrackMenuItem {
  14525. /**
  14526. * Creates an instance of this class.
  14527. *
  14528. * @param { import('../../player').default } player
  14529. * The `Player` that this class should be attached to.
  14530. *
  14531. * @param {Object} [options]
  14532. * The key/value store of player options.
  14533. */
  14534. constructor(player, options) {
  14535. // Create pseudo track info
  14536. // Requires options['kind']
  14537. options.track = {
  14538. player,
  14539. // it is no longer necessary to store `kind` or `kinds` on the track itself
  14540. // since they are now stored in the `kinds` property of all instances of
  14541. // TextTrackMenuItem, but this will remain for backwards compatibility
  14542. kind: options.kind,
  14543. kinds: options.kinds,
  14544. default: false,
  14545. mode: 'disabled'
  14546. };
  14547. if (!options.kinds) {
  14548. options.kinds = [options.kind];
  14549. }
  14550. if (options.label) {
  14551. options.track.label = options.label;
  14552. } else {
  14553. options.track.label = options.kinds.join(' and ') + ' off';
  14554. }
  14555. // MenuItem is selectable
  14556. options.selectable = true;
  14557. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  14558. options.multiSelectable = false;
  14559. super(player, options);
  14560. }
  14561. /**
  14562. * Handle text track change
  14563. *
  14564. * @param {Event} event
  14565. * The event that caused this function to run
  14566. */
  14567. handleTracksChange(event) {
  14568. const tracks = this.player().textTracks();
  14569. let shouldBeSelected = true;
  14570. for (let i = 0, l = tracks.length; i < l; i++) {
  14571. const track = tracks[i];
  14572. if (this.options_.kinds.indexOf(track.kind) > -1 && track.mode === 'showing') {
  14573. shouldBeSelected = false;
  14574. break;
  14575. }
  14576. }
  14577. // Prevent redundant selected() calls because they may cause
  14578. // screen readers to read the appended control text unnecessarily
  14579. if (shouldBeSelected !== this.isSelected_) {
  14580. this.selected(shouldBeSelected);
  14581. }
  14582. }
  14583. handleSelectedLanguageChange(event) {
  14584. const tracks = this.player().textTracks();
  14585. let allHidden = true;
  14586. for (let i = 0, l = tracks.length; i < l; i++) {
  14587. const track = tracks[i];
  14588. if (['captions', 'descriptions', 'subtitles'].indexOf(track.kind) > -1 && track.mode === 'showing') {
  14589. allHidden = false;
  14590. break;
  14591. }
  14592. }
  14593. if (allHidden) {
  14594. this.player_.cache_.selectedLanguage = {
  14595. enabled: false
  14596. };
  14597. }
  14598. }
  14599. /**
  14600. * Update control text and label on languagechange
  14601. */
  14602. handleLanguagechange() {
  14603. this.$('.vjs-menu-item-text').textContent = this.player_.localize(this.options_.label);
  14604. super.handleLanguagechange();
  14605. }
  14606. }
  14607. Component.registerComponent('OffTextTrackMenuItem', OffTextTrackMenuItem);
  14608. /**
  14609. * @file text-track-button.js
  14610. */
  14611. /**
  14612. * The base class for buttons that toggle specific text track types (e.g. subtitles)
  14613. *
  14614. * @extends MenuButton
  14615. */
  14616. class TextTrackButton extends TrackButton {
  14617. /**
  14618. * Creates an instance of this class.
  14619. *
  14620. * @param { import('../../player').default } player
  14621. * The `Player` that this class should be attached to.
  14622. *
  14623. * @param {Object} [options={}]
  14624. * The key/value store of player options.
  14625. */
  14626. constructor(player, options = {}) {
  14627. options.tracks = player.textTracks();
  14628. super(player, options);
  14629. }
  14630. /**
  14631. * Create a menu item for each text track
  14632. *
  14633. * @param {TextTrackMenuItem[]} [items=[]]
  14634. * Existing array of items to use during creation
  14635. *
  14636. * @return {TextTrackMenuItem[]}
  14637. * Array of menu items that were created
  14638. */
  14639. createItems(items = [], TrackMenuItem = TextTrackMenuItem) {
  14640. // Label is an override for the [track] off label
  14641. // USed to localise captions/subtitles
  14642. let label;
  14643. if (this.label_) {
  14644. label = `${this.label_} off`;
  14645. }
  14646. // Add an OFF menu item to turn all tracks off
  14647. items.push(new OffTextTrackMenuItem(this.player_, {
  14648. kinds: this.kinds_,
  14649. kind: this.kind_,
  14650. label
  14651. }));
  14652. this.hideThreshold_ += 1;
  14653. const tracks = this.player_.textTracks();
  14654. if (!Array.isArray(this.kinds_)) {
  14655. this.kinds_ = [this.kind_];
  14656. }
  14657. for (let i = 0; i < tracks.length; i++) {
  14658. const track = tracks[i];
  14659. // only add tracks that are of an appropriate kind and have a label
  14660. if (this.kinds_.indexOf(track.kind) > -1) {
  14661. const item = new TrackMenuItem(this.player_, {
  14662. track,
  14663. kinds: this.kinds_,
  14664. kind: this.kind_,
  14665. // MenuItem is selectable
  14666. selectable: true,
  14667. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  14668. multiSelectable: false
  14669. });
  14670. item.addClass(`vjs-${track.kind}-menu-item`);
  14671. items.push(item);
  14672. }
  14673. }
  14674. return items;
  14675. }
  14676. }
  14677. Component.registerComponent('TextTrackButton', TextTrackButton);
  14678. /**
  14679. * @file chapters-track-menu-item.js
  14680. */
  14681. /**
  14682. * The chapter track menu item
  14683. *
  14684. * @extends MenuItem
  14685. */
  14686. class ChaptersTrackMenuItem extends MenuItem {
  14687. /**
  14688. * Creates an instance of this class.
  14689. *
  14690. * @param { import('../../player').default } player
  14691. * The `Player` that this class should be attached to.
  14692. *
  14693. * @param {Object} [options]
  14694. * The key/value store of player options.
  14695. */
  14696. constructor(player, options) {
  14697. const track = options.track;
  14698. const cue = options.cue;
  14699. const currentTime = player.currentTime();
  14700. // Modify options for parent MenuItem class's init.
  14701. options.selectable = true;
  14702. options.multiSelectable = false;
  14703. options.label = cue.text;
  14704. options.selected = cue.startTime <= currentTime && currentTime < cue.endTime;
  14705. super(player, options);
  14706. this.track = track;
  14707. this.cue = cue;
  14708. }
  14709. /**
  14710. * This gets called when an `ChaptersTrackMenuItem` is "clicked". See
  14711. * {@link ClickableComponent} for more detailed information on what a click can be.
  14712. *
  14713. * @param {Event} [event]
  14714. * The `keydown`, `tap`, or `click` event that caused this function to be
  14715. * called.
  14716. *
  14717. * @listens tap
  14718. * @listens click
  14719. */
  14720. handleClick(event) {
  14721. super.handleClick();
  14722. this.player_.currentTime(this.cue.startTime);
  14723. }
  14724. }
  14725. Component.registerComponent('ChaptersTrackMenuItem', ChaptersTrackMenuItem);
  14726. /**
  14727. * @file chapters-button.js
  14728. */
  14729. /**
  14730. * The button component for toggling and selecting chapters
  14731. * Chapters act much differently than other text tracks
  14732. * Cues are navigation vs. other tracks of alternative languages
  14733. *
  14734. * @extends TextTrackButton
  14735. */
  14736. class ChaptersButton extends TextTrackButton {
  14737. /**
  14738. * Creates an instance of this class.
  14739. *
  14740. * @param { import('../../player').default } player
  14741. * The `Player` that this class should be attached to.
  14742. *
  14743. * @param {Object} [options]
  14744. * The key/value store of player options.
  14745. *
  14746. * @param {Function} [ready]
  14747. * The function to call when this function is ready.
  14748. */
  14749. constructor(player, options, ready) {
  14750. super(player, options, ready);
  14751. this.selectCurrentItem_ = () => {
  14752. this.items.forEach(item => {
  14753. item.selected(this.track_.activeCues[0] === item.cue);
  14754. });
  14755. };
  14756. }
  14757. /**
  14758. * Builds the default DOM `className`.
  14759. *
  14760. * @return {string}
  14761. * The DOM `className` for this object.
  14762. */
  14763. buildCSSClass() {
  14764. return `vjs-chapters-button ${super.buildCSSClass()}`;
  14765. }
  14766. buildWrapperCSSClass() {
  14767. return `vjs-chapters-button ${super.buildWrapperCSSClass()}`;
  14768. }
  14769. /**
  14770. * Update the menu based on the current state of its items.
  14771. *
  14772. * @param {Event} [event]
  14773. * An event that triggered this function to run.
  14774. *
  14775. * @listens TextTrackList#addtrack
  14776. * @listens TextTrackList#removetrack
  14777. * @listens TextTrackList#change
  14778. */
  14779. update(event) {
  14780. if (event && event.track && event.track.kind !== 'chapters') {
  14781. return;
  14782. }
  14783. const track = this.findChaptersTrack();
  14784. if (track !== this.track_) {
  14785. this.setTrack(track);
  14786. super.update();
  14787. } else if (!this.items || track && track.cues && track.cues.length !== this.items.length) {
  14788. // Update the menu initially or if the number of cues has changed since set
  14789. super.update();
  14790. }
  14791. }
  14792. /**
  14793. * Set the currently selected track for the chapters button.
  14794. *
  14795. * @param {TextTrack} track
  14796. * The new track to select. Nothing will change if this is the currently selected
  14797. * track.
  14798. */
  14799. setTrack(track) {
  14800. if (this.track_ === track) {
  14801. return;
  14802. }
  14803. if (!this.updateHandler_) {
  14804. this.updateHandler_ = this.update.bind(this);
  14805. }
  14806. // here this.track_ refers to the old track instance
  14807. if (this.track_) {
  14808. const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
  14809. if (remoteTextTrackEl) {
  14810. remoteTextTrackEl.removeEventListener('load', this.updateHandler_);
  14811. }
  14812. this.track_.removeEventListener('cuechange', this.selectCurrentItem_);
  14813. this.track_ = null;
  14814. }
  14815. this.track_ = track;
  14816. // here this.track_ refers to the new track instance
  14817. if (this.track_) {
  14818. this.track_.mode = 'hidden';
  14819. const remoteTextTrackEl = this.player_.remoteTextTrackEls().getTrackElementByTrack_(this.track_);
  14820. if (remoteTextTrackEl) {
  14821. remoteTextTrackEl.addEventListener('load', this.updateHandler_);
  14822. }
  14823. this.track_.addEventListener('cuechange', this.selectCurrentItem_);
  14824. }
  14825. }
  14826. /**
  14827. * Find the track object that is currently in use by this ChaptersButton
  14828. *
  14829. * @return {TextTrack|undefined}
  14830. * The current track or undefined if none was found.
  14831. */
  14832. findChaptersTrack() {
  14833. const tracks = this.player_.textTracks() || [];
  14834. for (let i = tracks.length - 1; i >= 0; i--) {
  14835. // We will always choose the last track as our chaptersTrack
  14836. const track = tracks[i];
  14837. if (track.kind === this.kind_) {
  14838. return track;
  14839. }
  14840. }
  14841. }
  14842. /**
  14843. * Get the caption for the ChaptersButton based on the track label. This will also
  14844. * use the current tracks localized kind as a fallback if a label does not exist.
  14845. *
  14846. * @return {string}
  14847. * The tracks current label or the localized track kind.
  14848. */
  14849. getMenuCaption() {
  14850. if (this.track_ && this.track_.label) {
  14851. return this.track_.label;
  14852. }
  14853. return this.localize(toTitleCase(this.kind_));
  14854. }
  14855. /**
  14856. * Create menu from chapter track
  14857. *
  14858. * @return { import('../../menu/menu').default }
  14859. * New menu for the chapter buttons
  14860. */
  14861. createMenu() {
  14862. this.options_.title = this.getMenuCaption();
  14863. return super.createMenu();
  14864. }
  14865. /**
  14866. * Create a menu item for each text track
  14867. *
  14868. * @return { import('./text-track-menu-item').default[] }
  14869. * Array of menu items
  14870. */
  14871. createItems() {
  14872. const items = [];
  14873. if (!this.track_) {
  14874. return items;
  14875. }
  14876. const cues = this.track_.cues;
  14877. if (!cues) {
  14878. return items;
  14879. }
  14880. for (let i = 0, l = cues.length; i < l; i++) {
  14881. const cue = cues[i];
  14882. const mi = new ChaptersTrackMenuItem(this.player_, {
  14883. track: this.track_,
  14884. cue
  14885. });
  14886. items.push(mi);
  14887. }
  14888. return items;
  14889. }
  14890. }
  14891. /**
  14892. * `kind` of TextTrack to look for to associate it with this menu.
  14893. *
  14894. * @type {string}
  14895. * @private
  14896. */
  14897. ChaptersButton.prototype.kind_ = 'chapters';
  14898. /**
  14899. * The text that should display over the `ChaptersButton`s controls. Added for localization.
  14900. *
  14901. * @type {string}
  14902. * @protected
  14903. */
  14904. ChaptersButton.prototype.controlText_ = 'Chapters';
  14905. Component.registerComponent('ChaptersButton', ChaptersButton);
  14906. /**
  14907. * @file descriptions-button.js
  14908. */
  14909. /**
  14910. * The button component for toggling and selecting descriptions
  14911. *
  14912. * @extends TextTrackButton
  14913. */
  14914. class DescriptionsButton extends TextTrackButton {
  14915. /**
  14916. * Creates an instance of this class.
  14917. *
  14918. * @param { import('../../player').default } player
  14919. * The `Player` that this class should be attached to.
  14920. *
  14921. * @param {Object} [options]
  14922. * The key/value store of player options.
  14923. *
  14924. * @param {Function} [ready]
  14925. * The function to call when this component is ready.
  14926. */
  14927. constructor(player, options, ready) {
  14928. super(player, options, ready);
  14929. const tracks = player.textTracks();
  14930. const changeHandler = bind_(this, this.handleTracksChange);
  14931. tracks.addEventListener('change', changeHandler);
  14932. this.on('dispose', function () {
  14933. tracks.removeEventListener('change', changeHandler);
  14934. });
  14935. }
  14936. /**
  14937. * Handle text track change
  14938. *
  14939. * @param {Event} event
  14940. * The event that caused this function to run
  14941. *
  14942. * @listens TextTrackList#change
  14943. */
  14944. handleTracksChange(event) {
  14945. const tracks = this.player().textTracks();
  14946. let disabled = false;
  14947. // Check whether a track of a different kind is showing
  14948. for (let i = 0, l = tracks.length; i < l; i++) {
  14949. const track = tracks[i];
  14950. if (track.kind !== this.kind_ && track.mode === 'showing') {
  14951. disabled = true;
  14952. break;
  14953. }
  14954. }
  14955. // If another track is showing, disable this menu button
  14956. if (disabled) {
  14957. this.disable();
  14958. } else {
  14959. this.enable();
  14960. }
  14961. }
  14962. /**
  14963. * Builds the default DOM `className`.
  14964. *
  14965. * @return {string}
  14966. * The DOM `className` for this object.
  14967. */
  14968. buildCSSClass() {
  14969. return `vjs-descriptions-button ${super.buildCSSClass()}`;
  14970. }
  14971. buildWrapperCSSClass() {
  14972. return `vjs-descriptions-button ${super.buildWrapperCSSClass()}`;
  14973. }
  14974. }
  14975. /**
  14976. * `kind` of TextTrack to look for to associate it with this menu.
  14977. *
  14978. * @type {string}
  14979. * @private
  14980. */
  14981. DescriptionsButton.prototype.kind_ = 'descriptions';
  14982. /**
  14983. * The text that should display over the `DescriptionsButton`s controls. Added for localization.
  14984. *
  14985. * @type {string}
  14986. * @protected
  14987. */
  14988. DescriptionsButton.prototype.controlText_ = 'Descriptions';
  14989. Component.registerComponent('DescriptionsButton', DescriptionsButton);
  14990. /**
  14991. * @file subtitles-button.js
  14992. */
  14993. /**
  14994. * The button component for toggling and selecting subtitles
  14995. *
  14996. * @extends TextTrackButton
  14997. */
  14998. class SubtitlesButton extends TextTrackButton {
  14999. /**
  15000. * Creates an instance of this class.
  15001. *
  15002. * @param { import('../../player').default } player
  15003. * The `Player` that this class should be attached to.
  15004. *
  15005. * @param {Object} [options]
  15006. * The key/value store of player options.
  15007. *
  15008. * @param {Function} [ready]
  15009. * The function to call when this component is ready.
  15010. */
  15011. constructor(player, options, ready) {
  15012. super(player, options, ready);
  15013. }
  15014. /**
  15015. * Builds the default DOM `className`.
  15016. *
  15017. * @return {string}
  15018. * The DOM `className` for this object.
  15019. */
  15020. buildCSSClass() {
  15021. return `vjs-subtitles-button ${super.buildCSSClass()}`;
  15022. }
  15023. buildWrapperCSSClass() {
  15024. return `vjs-subtitles-button ${super.buildWrapperCSSClass()}`;
  15025. }
  15026. }
  15027. /**
  15028. * `kind` of TextTrack to look for to associate it with this menu.
  15029. *
  15030. * @type {string}
  15031. * @private
  15032. */
  15033. SubtitlesButton.prototype.kind_ = 'subtitles';
  15034. /**
  15035. * The text that should display over the `SubtitlesButton`s controls. Added for localization.
  15036. *
  15037. * @type {string}
  15038. * @protected
  15039. */
  15040. SubtitlesButton.prototype.controlText_ = 'Subtitles';
  15041. Component.registerComponent('SubtitlesButton', SubtitlesButton);
  15042. /**
  15043. * @file caption-settings-menu-item.js
  15044. */
  15045. /**
  15046. * The menu item for caption track settings menu
  15047. *
  15048. * @extends TextTrackMenuItem
  15049. */
  15050. class CaptionSettingsMenuItem extends TextTrackMenuItem {
  15051. /**
  15052. * Creates an instance of this class.
  15053. *
  15054. * @param { import('../../player').default } player
  15055. * The `Player` that this class should be attached to.
  15056. *
  15057. * @param {Object} [options]
  15058. * The key/value store of player options.
  15059. */
  15060. constructor(player, options) {
  15061. options.track = {
  15062. player,
  15063. kind: options.kind,
  15064. label: options.kind + ' settings',
  15065. selectable: false,
  15066. default: false,
  15067. mode: 'disabled'
  15068. };
  15069. // CaptionSettingsMenuItem has no concept of 'selected'
  15070. options.selectable = false;
  15071. options.name = 'CaptionSettingsMenuItem';
  15072. super(player, options);
  15073. this.addClass('vjs-texttrack-settings');
  15074. this.controlText(', opens ' + options.kind + ' settings dialog');
  15075. }
  15076. /**
  15077. * This gets called when an `CaptionSettingsMenuItem` is "clicked". See
  15078. * {@link ClickableComponent} for more detailed information on what a click can be.
  15079. *
  15080. * @param {Event} [event]
  15081. * The `keydown`, `tap`, or `click` event that caused this function to be
  15082. * called.
  15083. *
  15084. * @listens tap
  15085. * @listens click
  15086. */
  15087. handleClick(event) {
  15088. this.player().getChild('textTrackSettings').open();
  15089. }
  15090. /**
  15091. * Update control text and label on languagechange
  15092. */
  15093. handleLanguagechange() {
  15094. this.$('.vjs-menu-item-text').textContent = this.player_.localize(this.options_.kind + ' settings');
  15095. super.handleLanguagechange();
  15096. }
  15097. }
  15098. Component.registerComponent('CaptionSettingsMenuItem', CaptionSettingsMenuItem);
  15099. /**
  15100. * @file captions-button.js
  15101. */
  15102. /**
  15103. * The button component for toggling and selecting captions
  15104. *
  15105. * @extends TextTrackButton
  15106. */
  15107. class CaptionsButton extends TextTrackButton {
  15108. /**
  15109. * Creates an instance of this class.
  15110. *
  15111. * @param { import('../../player').default } player
  15112. * The `Player` that this class should be attached to.
  15113. *
  15114. * @param {Object} [options]
  15115. * The key/value store of player options.
  15116. *
  15117. * @param {Function} [ready]
  15118. * The function to call when this component is ready.
  15119. */
  15120. constructor(player, options, ready) {
  15121. super(player, options, ready);
  15122. }
  15123. /**
  15124. * Builds the default DOM `className`.
  15125. *
  15126. * @return {string}
  15127. * The DOM `className` for this object.
  15128. */
  15129. buildCSSClass() {
  15130. return `vjs-captions-button ${super.buildCSSClass()}`;
  15131. }
  15132. buildWrapperCSSClass() {
  15133. return `vjs-captions-button ${super.buildWrapperCSSClass()}`;
  15134. }
  15135. /**
  15136. * Create caption menu items
  15137. *
  15138. * @return {CaptionSettingsMenuItem[]}
  15139. * The array of current menu items.
  15140. */
  15141. createItems() {
  15142. const items = [];
  15143. if (!(this.player().tech_ && this.player().tech_.featuresNativeTextTracks) && this.player().getChild('textTrackSettings')) {
  15144. items.push(new CaptionSettingsMenuItem(this.player_, {
  15145. kind: this.kind_
  15146. }));
  15147. this.hideThreshold_ += 1;
  15148. }
  15149. return super.createItems(items);
  15150. }
  15151. }
  15152. /**
  15153. * `kind` of TextTrack to look for to associate it with this menu.
  15154. *
  15155. * @type {string}
  15156. * @private
  15157. */
  15158. CaptionsButton.prototype.kind_ = 'captions';
  15159. /**
  15160. * The text that should display over the `CaptionsButton`s controls. Added for localization.
  15161. *
  15162. * @type {string}
  15163. * @protected
  15164. */
  15165. CaptionsButton.prototype.controlText_ = 'Captions';
  15166. Component.registerComponent('CaptionsButton', CaptionsButton);
  15167. /**
  15168. * @file subs-caps-menu-item.js
  15169. */
  15170. /**
  15171. * SubsCapsMenuItem has an [cc] icon to distinguish captions from subtitles
  15172. * in the SubsCapsMenu.
  15173. *
  15174. * @extends TextTrackMenuItem
  15175. */
  15176. class SubsCapsMenuItem extends TextTrackMenuItem {
  15177. createEl(type, props, attrs) {
  15178. const el = super.createEl(type, props, attrs);
  15179. const parentSpan = el.querySelector('.vjs-menu-item-text');
  15180. if (this.options_.track.kind === 'captions') {
  15181. parentSpan.appendChild(createEl('span', {
  15182. className: 'vjs-icon-placeholder'
  15183. }, {
  15184. 'aria-hidden': true
  15185. }));
  15186. parentSpan.appendChild(createEl('span', {
  15187. className: 'vjs-control-text',
  15188. // space added as the text will visually flow with the
  15189. // label
  15190. textContent: ` ${this.localize('Captions')}`
  15191. }));
  15192. }
  15193. return el;
  15194. }
  15195. }
  15196. Component.registerComponent('SubsCapsMenuItem', SubsCapsMenuItem);
  15197. /**
  15198. * @file sub-caps-button.js
  15199. */
  15200. /**
  15201. * The button component for toggling and selecting captions and/or subtitles
  15202. *
  15203. * @extends TextTrackButton
  15204. */
  15205. class SubsCapsButton extends TextTrackButton {
  15206. /**
  15207. * Creates an instance of this class.
  15208. *
  15209. * @param { import('../../player').default } player
  15210. * The `Player` that this class should be attached to.
  15211. *
  15212. * @param {Object} [options]
  15213. * The key/value store of player options.
  15214. *
  15215. * @param {Function} [ready]
  15216. * The function to call when this component is ready.
  15217. */
  15218. constructor(player, options = {}) {
  15219. super(player, options);
  15220. // Although North America uses "captions" in most cases for
  15221. // "captions and subtitles" other locales use "subtitles"
  15222. this.label_ = 'subtitles';
  15223. if (['en', 'en-us', 'en-ca', 'fr-ca'].indexOf(this.player_.language_) > -1) {
  15224. this.label_ = 'captions';
  15225. }
  15226. this.menuButton_.controlText(toTitleCase(this.label_));
  15227. }
  15228. /**
  15229. * Builds the default DOM `className`.
  15230. *
  15231. * @return {string}
  15232. * The DOM `className` for this object.
  15233. */
  15234. buildCSSClass() {
  15235. return `vjs-subs-caps-button ${super.buildCSSClass()}`;
  15236. }
  15237. buildWrapperCSSClass() {
  15238. return `vjs-subs-caps-button ${super.buildWrapperCSSClass()}`;
  15239. }
  15240. /**
  15241. * Create caption/subtitles menu items
  15242. *
  15243. * @return {CaptionSettingsMenuItem[]}
  15244. * The array of current menu items.
  15245. */
  15246. createItems() {
  15247. let items = [];
  15248. if (!(this.player().tech_ && this.player().tech_.featuresNativeTextTracks) && this.player().getChild('textTrackSettings')) {
  15249. items.push(new CaptionSettingsMenuItem(this.player_, {
  15250. kind: this.label_
  15251. }));
  15252. this.hideThreshold_ += 1;
  15253. }
  15254. items = super.createItems(items, SubsCapsMenuItem);
  15255. return items;
  15256. }
  15257. }
  15258. /**
  15259. * `kind`s of TextTrack to look for to associate it with this menu.
  15260. *
  15261. * @type {array}
  15262. * @private
  15263. */
  15264. SubsCapsButton.prototype.kinds_ = ['captions', 'subtitles'];
  15265. /**
  15266. * The text that should display over the `SubsCapsButton`s controls.
  15267. *
  15268. *
  15269. * @type {string}
  15270. * @protected
  15271. */
  15272. SubsCapsButton.prototype.controlText_ = 'Subtitles';
  15273. Component.registerComponent('SubsCapsButton', SubsCapsButton);
  15274. /**
  15275. * @file audio-track-menu-item.js
  15276. */
  15277. /**
  15278. * An {@link AudioTrack} {@link MenuItem}
  15279. *
  15280. * @extends MenuItem
  15281. */
  15282. class AudioTrackMenuItem extends MenuItem {
  15283. /**
  15284. * Creates an instance of this class.
  15285. *
  15286. * @param { import('../../player').default } player
  15287. * The `Player` that this class should be attached to.
  15288. *
  15289. * @param {Object} [options]
  15290. * The key/value store of player options.
  15291. */
  15292. constructor(player, options) {
  15293. const track = options.track;
  15294. const tracks = player.audioTracks();
  15295. // Modify options for parent MenuItem class's init.
  15296. options.label = track.label || track.language || 'Unknown';
  15297. options.selected = track.enabled;
  15298. super(player, options);
  15299. this.track = track;
  15300. this.addClass(`vjs-${track.kind}-menu-item`);
  15301. const changeHandler = (...args) => {
  15302. this.handleTracksChange.apply(this, args);
  15303. };
  15304. tracks.addEventListener('change', changeHandler);
  15305. this.on('dispose', () => {
  15306. tracks.removeEventListener('change', changeHandler);
  15307. });
  15308. }
  15309. createEl(type, props, attrs) {
  15310. const el = super.createEl(type, props, attrs);
  15311. const parentSpan = el.querySelector('.vjs-menu-item-text');
  15312. if (this.options_.track.kind === 'main-desc') {
  15313. parentSpan.appendChild(createEl('span', {
  15314. className: 'vjs-icon-placeholder'
  15315. }, {
  15316. 'aria-hidden': true
  15317. }));
  15318. parentSpan.appendChild(createEl('span', {
  15319. className: 'vjs-control-text',
  15320. textContent: ' ' + this.localize('Descriptions')
  15321. }));
  15322. }
  15323. return el;
  15324. }
  15325. /**
  15326. * This gets called when an `AudioTrackMenuItem is "clicked". See {@link ClickableComponent}
  15327. * for more detailed information on what a click can be.
  15328. *
  15329. * @param {Event} [event]
  15330. * The `keydown`, `tap`, or `click` event that caused this function to be
  15331. * called.
  15332. *
  15333. * @listens tap
  15334. * @listens click
  15335. */
  15336. handleClick(event) {
  15337. super.handleClick(event);
  15338. // the audio track list will automatically toggle other tracks
  15339. // off for us.
  15340. this.track.enabled = true;
  15341. // when native audio tracks are used, we want to make sure that other tracks are turned off
  15342. if (this.player_.tech_.featuresNativeAudioTracks) {
  15343. const tracks = this.player_.audioTracks();
  15344. for (let i = 0; i < tracks.length; i++) {
  15345. const track = tracks[i];
  15346. // skip the current track since we enabled it above
  15347. if (track === this.track) {
  15348. continue;
  15349. }
  15350. track.enabled = track === this.track;
  15351. }
  15352. }
  15353. }
  15354. /**
  15355. * Handle any {@link AudioTrack} change.
  15356. *
  15357. * @param {Event} [event]
  15358. * The {@link AudioTrackList#change} event that caused this to run.
  15359. *
  15360. * @listens AudioTrackList#change
  15361. */
  15362. handleTracksChange(event) {
  15363. this.selected(this.track.enabled);
  15364. }
  15365. }
  15366. Component.registerComponent('AudioTrackMenuItem', AudioTrackMenuItem);
  15367. /**
  15368. * @file audio-track-button.js
  15369. */
  15370. /**
  15371. * The base class for buttons that toggle specific {@link AudioTrack} types.
  15372. *
  15373. * @extends TrackButton
  15374. */
  15375. class AudioTrackButton extends TrackButton {
  15376. /**
  15377. * Creates an instance of this class.
  15378. *
  15379. * @param {Player} player
  15380. * The `Player` that this class should be attached to.
  15381. *
  15382. * @param {Object} [options={}]
  15383. * The key/value store of player options.
  15384. */
  15385. constructor(player, options = {}) {
  15386. options.tracks = player.audioTracks();
  15387. super(player, options);
  15388. }
  15389. /**
  15390. * Builds the default DOM `className`.
  15391. *
  15392. * @return {string}
  15393. * The DOM `className` for this object.
  15394. */
  15395. buildCSSClass() {
  15396. return `vjs-audio-button ${super.buildCSSClass()}`;
  15397. }
  15398. buildWrapperCSSClass() {
  15399. return `vjs-audio-button ${super.buildWrapperCSSClass()}`;
  15400. }
  15401. /**
  15402. * Create a menu item for each audio track
  15403. *
  15404. * @param {AudioTrackMenuItem[]} [items=[]]
  15405. * An array of existing menu items to use.
  15406. *
  15407. * @return {AudioTrackMenuItem[]}
  15408. * An array of menu items
  15409. */
  15410. createItems(items = []) {
  15411. // if there's only one audio track, there no point in showing it
  15412. this.hideThreshold_ = 1;
  15413. const tracks = this.player_.audioTracks();
  15414. for (let i = 0; i < tracks.length; i++) {
  15415. const track = tracks[i];
  15416. items.push(new AudioTrackMenuItem(this.player_, {
  15417. track,
  15418. // MenuItem is selectable
  15419. selectable: true,
  15420. // MenuItem is NOT multiSelectable (i.e. only one can be marked "selected" at a time)
  15421. multiSelectable: false
  15422. }));
  15423. }
  15424. return items;
  15425. }
  15426. }
  15427. /**
  15428. * The text that should display over the `AudioTrackButton`s controls. Added for localization.
  15429. *
  15430. * @type {string}
  15431. * @protected
  15432. */
  15433. AudioTrackButton.prototype.controlText_ = 'Audio Track';
  15434. Component.registerComponent('AudioTrackButton', AudioTrackButton);
  15435. /**
  15436. * @file playback-rate-menu-item.js
  15437. */
  15438. /**
  15439. * The specific menu item type for selecting a playback rate.
  15440. *
  15441. * @extends MenuItem
  15442. */
  15443. class PlaybackRateMenuItem extends MenuItem {
  15444. /**
  15445. * Creates an instance of this class.
  15446. *
  15447. * @param { import('../../player').default } player
  15448. * The `Player` that this class should be attached to.
  15449. *
  15450. * @param {Object} [options]
  15451. * The key/value store of player options.
  15452. */
  15453. constructor(player, options) {
  15454. const label = options.rate;
  15455. const rate = parseFloat(label, 10);
  15456. // Modify options for parent MenuItem class's init.
  15457. options.label = label;
  15458. options.selected = rate === player.playbackRate();
  15459. options.selectable = true;
  15460. options.multiSelectable = false;
  15461. super(player, options);
  15462. this.label = label;
  15463. this.rate = rate;
  15464. this.on(player, 'ratechange', e => this.update(e));
  15465. }
  15466. /**
  15467. * This gets called when an `PlaybackRateMenuItem` is "clicked". See
  15468. * {@link ClickableComponent} for more detailed information on what a click can be.
  15469. *
  15470. * @param {Event} [event]
  15471. * The `keydown`, `tap`, or `click` event that caused this function to be
  15472. * called.
  15473. *
  15474. * @listens tap
  15475. * @listens click
  15476. */
  15477. handleClick(event) {
  15478. super.handleClick();
  15479. this.player().playbackRate(this.rate);
  15480. }
  15481. /**
  15482. * Update the PlaybackRateMenuItem when the playbackrate changes.
  15483. *
  15484. * @param {Event} [event]
  15485. * The `ratechange` event that caused this function to run.
  15486. *
  15487. * @listens Player#ratechange
  15488. */
  15489. update(event) {
  15490. this.selected(this.player().playbackRate() === this.rate);
  15491. }
  15492. }
  15493. /**
  15494. * The text that should display over the `PlaybackRateMenuItem`s controls. Added for localization.
  15495. *
  15496. * @type {string}
  15497. * @private
  15498. */
  15499. PlaybackRateMenuItem.prototype.contentElType = 'button';
  15500. Component.registerComponent('PlaybackRateMenuItem', PlaybackRateMenuItem);
  15501. /**
  15502. * @file playback-rate-menu-button.js
  15503. */
  15504. /**
  15505. * The component for controlling the playback rate.
  15506. *
  15507. * @extends MenuButton
  15508. */
  15509. class PlaybackRateMenuButton extends MenuButton {
  15510. /**
  15511. * Creates an instance of this class.
  15512. *
  15513. * @param { import('../../player').default } player
  15514. * The `Player` that this class should be attached to.
  15515. *
  15516. * @param {Object} [options]
  15517. * The key/value store of player options.
  15518. */
  15519. constructor(player, options) {
  15520. super(player, options);
  15521. this.menuButton_.el_.setAttribute('aria-describedby', this.labelElId_);
  15522. this.updateVisibility();
  15523. this.updateLabel();
  15524. this.on(player, 'loadstart', e => this.updateVisibility(e));
  15525. this.on(player, 'ratechange', e => this.updateLabel(e));
  15526. this.on(player, 'playbackrateschange', e => this.handlePlaybackRateschange(e));
  15527. }
  15528. /**
  15529. * Create the `Component`'s DOM element
  15530. *
  15531. * @return {Element}
  15532. * The element that was created.
  15533. */
  15534. createEl() {
  15535. const el = super.createEl();
  15536. this.labelElId_ = 'vjs-playback-rate-value-label-' + this.id_;
  15537. this.labelEl_ = createEl('div', {
  15538. className: 'vjs-playback-rate-value',
  15539. id: this.labelElId_,
  15540. textContent: '1x'
  15541. });
  15542. el.appendChild(this.labelEl_);
  15543. return el;
  15544. }
  15545. dispose() {
  15546. this.labelEl_ = null;
  15547. super.dispose();
  15548. }
  15549. /**
  15550. * Builds the default DOM `className`.
  15551. *
  15552. * @return {string}
  15553. * The DOM `className` for this object.
  15554. */
  15555. buildCSSClass() {
  15556. return `vjs-playback-rate ${super.buildCSSClass()}`;
  15557. }
  15558. buildWrapperCSSClass() {
  15559. return `vjs-playback-rate ${super.buildWrapperCSSClass()}`;
  15560. }
  15561. /**
  15562. * Create the list of menu items. Specific to each subclass.
  15563. *
  15564. */
  15565. createItems() {
  15566. const rates = this.playbackRates();
  15567. const items = [];
  15568. for (let i = rates.length - 1; i >= 0; i--) {
  15569. items.push(new PlaybackRateMenuItem(this.player(), {
  15570. rate: rates[i] + 'x'
  15571. }));
  15572. }
  15573. return items;
  15574. }
  15575. /**
  15576. * On playbackrateschange, update the menu to account for the new items.
  15577. *
  15578. * @listens Player#playbackrateschange
  15579. */
  15580. handlePlaybackRateschange(event) {
  15581. this.update();
  15582. }
  15583. /**
  15584. * Get possible playback rates
  15585. *
  15586. * @return {Array}
  15587. * All possible playback rates
  15588. */
  15589. playbackRates() {
  15590. const player = this.player();
  15591. return player.playbackRates && player.playbackRates() || [];
  15592. }
  15593. /**
  15594. * Get whether playback rates is supported by the tech
  15595. * and an array of playback rates exists
  15596. *
  15597. * @return {boolean}
  15598. * Whether changing playback rate is supported
  15599. */
  15600. playbackRateSupported() {
  15601. return this.player().tech_ && this.player().tech_.featuresPlaybackRate && this.playbackRates() && this.playbackRates().length > 0;
  15602. }
  15603. /**
  15604. * Hide playback rate controls when they're no playback rate options to select
  15605. *
  15606. * @param {Event} [event]
  15607. * The event that caused this function to run.
  15608. *
  15609. * @listens Player#loadstart
  15610. */
  15611. updateVisibility(event) {
  15612. if (this.playbackRateSupported()) {
  15613. this.removeClass('vjs-hidden');
  15614. } else {
  15615. this.addClass('vjs-hidden');
  15616. }
  15617. }
  15618. /**
  15619. * Update button label when rate changed
  15620. *
  15621. * @param {Event} [event]
  15622. * The event that caused this function to run.
  15623. *
  15624. * @listens Player#ratechange
  15625. */
  15626. updateLabel(event) {
  15627. if (this.playbackRateSupported()) {
  15628. this.labelEl_.textContent = this.player().playbackRate() + 'x';
  15629. }
  15630. }
  15631. }
  15632. /**
  15633. * The text that should display over the `PlaybackRateMenuButton`s controls.
  15634. *
  15635. * Added for localization.
  15636. *
  15637. * @type {string}
  15638. * @protected
  15639. */
  15640. PlaybackRateMenuButton.prototype.controlText_ = 'Playback Rate';
  15641. Component.registerComponent('PlaybackRateMenuButton', PlaybackRateMenuButton);
  15642. /**
  15643. * @file spacer.js
  15644. */
  15645. /**
  15646. * Just an empty spacer element that can be used as an append point for plugins, etc.
  15647. * Also can be used to create space between elements when necessary.
  15648. *
  15649. * @extends Component
  15650. */
  15651. class Spacer extends Component {
  15652. /**
  15653. * Builds the default DOM `className`.
  15654. *
  15655. * @return {string}
  15656. * The DOM `className` for this object.
  15657. */
  15658. buildCSSClass() {
  15659. return `vjs-spacer ${super.buildCSSClass()}`;
  15660. }
  15661. /**
  15662. * Create the `Component`'s DOM element
  15663. *
  15664. * @return {Element}
  15665. * The element that was created.
  15666. */
  15667. createEl(tag = 'div', props = {}, attributes = {}) {
  15668. if (!props.className) {
  15669. props.className = this.buildCSSClass();
  15670. }
  15671. return super.createEl(tag, props, attributes);
  15672. }
  15673. }
  15674. Component.registerComponent('Spacer', Spacer);
  15675. /**
  15676. * @file custom-control-spacer.js
  15677. */
  15678. /**
  15679. * Spacer specifically meant to be used as an insertion point for new plugins, etc.
  15680. *
  15681. * @extends Spacer
  15682. */
  15683. class CustomControlSpacer extends Spacer {
  15684. /**
  15685. * Builds the default DOM `className`.
  15686. *
  15687. * @return {string}
  15688. * The DOM `className` for this object.
  15689. */
  15690. buildCSSClass() {
  15691. return `vjs-custom-control-spacer ${super.buildCSSClass()}`;
  15692. }
  15693. /**
  15694. * Create the `Component`'s DOM element
  15695. *
  15696. * @return {Element}
  15697. * The element that was created.
  15698. */
  15699. createEl() {
  15700. return super.createEl('div', {
  15701. className: this.buildCSSClass(),
  15702. // No-flex/table-cell mode requires there be some content
  15703. // in the cell to fill the remaining space of the table.
  15704. textContent: '\u00a0'
  15705. });
  15706. }
  15707. }
  15708. Component.registerComponent('CustomControlSpacer', CustomControlSpacer);
  15709. /**
  15710. * @file control-bar.js
  15711. */
  15712. /**
  15713. * Container of main controls.
  15714. *
  15715. * @extends Component
  15716. */
  15717. class ControlBar extends Component {
  15718. /**
  15719. * Create the `Component`'s DOM element
  15720. *
  15721. * @return {Element}
  15722. * The element that was created.
  15723. */
  15724. createEl() {
  15725. return super.createEl('div', {
  15726. className: 'vjs-control-bar',
  15727. dir: 'ltr'
  15728. });
  15729. }
  15730. }
  15731. /**
  15732. * Default options for `ControlBar`
  15733. *
  15734. * @type {Object}
  15735. * @private
  15736. */
  15737. ControlBar.prototype.options_ = {
  15738. children: ['playToggle', 'skipBackward', 'skipForward', 'volumePanel', 'currentTimeDisplay', 'timeDivider', 'durationDisplay', 'progressControl', 'liveDisplay', 'seekToLive', 'remainingTimeDisplay', 'customControlSpacer', 'playbackRateMenuButton', 'chaptersButton', 'descriptionsButton', 'subsCapsButton', 'audioTrackButton', 'fullscreenToggle']
  15739. };
  15740. if ('exitPictureInPicture' in document) {
  15741. ControlBar.prototype.options_.children.splice(ControlBar.prototype.options_.children.length - 1, 0, 'pictureInPictureToggle');
  15742. }
  15743. Component.registerComponent('ControlBar', ControlBar);
  15744. /**
  15745. * @file error-display.js
  15746. */
  15747. /**
  15748. * A display that indicates an error has occurred. This means that the video
  15749. * is unplayable.
  15750. *
  15751. * @extends ModalDialog
  15752. */
  15753. class ErrorDisplay extends ModalDialog {
  15754. /**
  15755. * Creates an instance of this class.
  15756. *
  15757. * @param { import('./player').default } player
  15758. * The `Player` that this class should be attached to.
  15759. *
  15760. * @param {Object} [options]
  15761. * The key/value store of player options.
  15762. */
  15763. constructor(player, options) {
  15764. super(player, options);
  15765. this.on(player, 'error', e => this.open(e));
  15766. }
  15767. /**
  15768. * Builds the default DOM `className`.
  15769. *
  15770. * @return {string}
  15771. * The DOM `className` for this object.
  15772. *
  15773. * @deprecated Since version 5.
  15774. */
  15775. buildCSSClass() {
  15776. return `vjs-error-display ${super.buildCSSClass()}`;
  15777. }
  15778. /**
  15779. * Gets the localized error message based on the `Player`s error.
  15780. *
  15781. * @return {string}
  15782. * The `Player`s error message localized or an empty string.
  15783. */
  15784. content() {
  15785. const error = this.player().error();
  15786. return error ? this.localize(error.message) : '';
  15787. }
  15788. }
  15789. /**
  15790. * The default options for an `ErrorDisplay`.
  15791. *
  15792. * @private
  15793. */
  15794. ErrorDisplay.prototype.options_ = Object.assign({}, ModalDialog.prototype.options_, {
  15795. pauseOnOpen: false,
  15796. fillAlways: true,
  15797. temporary: false,
  15798. uncloseable: true
  15799. });
  15800. Component.registerComponent('ErrorDisplay', ErrorDisplay);
  15801. /**
  15802. * @file text-track-settings.js
  15803. */
  15804. const LOCAL_STORAGE_KEY = 'vjs-text-track-settings';
  15805. const COLOR_BLACK = ['#000', 'Black'];
  15806. const COLOR_BLUE = ['#00F', 'Blue'];
  15807. const COLOR_CYAN = ['#0FF', 'Cyan'];
  15808. const COLOR_GREEN = ['#0F0', 'Green'];
  15809. const COLOR_MAGENTA = ['#F0F', 'Magenta'];
  15810. const COLOR_RED = ['#F00', 'Red'];
  15811. const COLOR_WHITE = ['#FFF', 'White'];
  15812. const COLOR_YELLOW = ['#FF0', 'Yellow'];
  15813. const OPACITY_OPAQUE = ['1', 'Opaque'];
  15814. const OPACITY_SEMI = ['0.5', 'Semi-Transparent'];
  15815. const OPACITY_TRANS = ['0', 'Transparent'];
  15816. // Configuration for the various <select> elements in the DOM of this component.
  15817. //
  15818. // Possible keys include:
  15819. //
  15820. // `default`:
  15821. // The default option index. Only needs to be provided if not zero.
  15822. // `parser`:
  15823. // A function which is used to parse the value from the selected option in
  15824. // a customized way.
  15825. // `selector`:
  15826. // The selector used to find the associated <select> element.
  15827. const selectConfigs = {
  15828. backgroundColor: {
  15829. selector: '.vjs-bg-color > select',
  15830. id: 'captions-background-color-%s',
  15831. label: 'Color',
  15832. options: [COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_MAGENTA, COLOR_CYAN]
  15833. },
  15834. backgroundOpacity: {
  15835. selector: '.vjs-bg-opacity > select',
  15836. id: 'captions-background-opacity-%s',
  15837. label: 'Opacity',
  15838. options: [OPACITY_OPAQUE, OPACITY_SEMI, OPACITY_TRANS]
  15839. },
  15840. color: {
  15841. selector: '.vjs-text-color > select',
  15842. id: 'captions-foreground-color-%s',
  15843. label: 'Color',
  15844. options: [COLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_YELLOW, COLOR_MAGENTA, COLOR_CYAN]
  15845. },
  15846. edgeStyle: {
  15847. selector: '.vjs-edge-style > select',
  15848. id: '%s',
  15849. label: 'Text Edge Style',
  15850. options: [['none', 'None'], ['raised', 'Raised'], ['depressed', 'Depressed'], ['uniform', 'Uniform'], ['dropshadow', 'Dropshadow']]
  15851. },
  15852. fontFamily: {
  15853. selector: '.vjs-font-family > select',
  15854. id: 'captions-font-family-%s',
  15855. label: 'Font Family',
  15856. options: [['proportionalSansSerif', 'Proportional Sans-Serif'], ['monospaceSansSerif', 'Monospace Sans-Serif'], ['proportionalSerif', 'Proportional Serif'], ['monospaceSerif', 'Monospace Serif'], ['casual', 'Casual'], ['script', 'Script'], ['small-caps', 'Small Caps']]
  15857. },
  15858. fontPercent: {
  15859. selector: '.vjs-font-percent > select',
  15860. id: 'captions-font-size-%s',
  15861. label: 'Font Size',
  15862. 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%']],
  15863. default: 2,
  15864. parser: v => v === '1.00' ? null : Number(v)
  15865. },
  15866. textOpacity: {
  15867. selector: '.vjs-text-opacity > select',
  15868. id: 'captions-foreground-opacity-%s',
  15869. label: 'Opacity',
  15870. options: [OPACITY_OPAQUE, OPACITY_SEMI]
  15871. },
  15872. // Options for this object are defined below.
  15873. windowColor: {
  15874. selector: '.vjs-window-color > select',
  15875. id: 'captions-window-color-%s',
  15876. label: 'Color'
  15877. },
  15878. // Options for this object are defined below.
  15879. windowOpacity: {
  15880. selector: '.vjs-window-opacity > select',
  15881. id: 'captions-window-opacity-%s',
  15882. label: 'Opacity',
  15883. options: [OPACITY_TRANS, OPACITY_SEMI, OPACITY_OPAQUE]
  15884. }
  15885. };
  15886. selectConfigs.windowColor.options = selectConfigs.backgroundColor.options;
  15887. /**
  15888. * Get the actual value of an option.
  15889. *
  15890. * @param {string} value
  15891. * The value to get
  15892. *
  15893. * @param {Function} [parser]
  15894. * Optional function to adjust the value.
  15895. *
  15896. * @return {*}
  15897. * - Will be `undefined` if no value exists
  15898. * - Will be `undefined` if the given value is "none".
  15899. * - Will be the actual value otherwise.
  15900. *
  15901. * @private
  15902. */
  15903. function parseOptionValue(value, parser) {
  15904. if (parser) {
  15905. value = parser(value);
  15906. }
  15907. if (value && value !== 'none') {
  15908. return value;
  15909. }
  15910. }
  15911. /**
  15912. * Gets the value of the selected <option> element within a <select> element.
  15913. *
  15914. * @param {Element} el
  15915. * the element to look in
  15916. *
  15917. * @param {Function} [parser]
  15918. * Optional function to adjust the value.
  15919. *
  15920. * @return {*}
  15921. * - Will be `undefined` if no value exists
  15922. * - Will be `undefined` if the given value is "none".
  15923. * - Will be the actual value otherwise.
  15924. *
  15925. * @private
  15926. */
  15927. function getSelectedOptionValue(el, parser) {
  15928. const value = el.options[el.options.selectedIndex].value;
  15929. return parseOptionValue(value, parser);
  15930. }
  15931. /**
  15932. * Sets the selected <option> element within a <select> element based on a
  15933. * given value.
  15934. *
  15935. * @param {Element} el
  15936. * The element to look in.
  15937. *
  15938. * @param {string} value
  15939. * the property to look on.
  15940. *
  15941. * @param {Function} [parser]
  15942. * Optional function to adjust the value before comparing.
  15943. *
  15944. * @private
  15945. */
  15946. function setSelectedOption(el, value, parser) {
  15947. if (!value) {
  15948. return;
  15949. }
  15950. for (let i = 0; i < el.options.length; i++) {
  15951. if (parseOptionValue(el.options[i].value, parser) === value) {
  15952. el.selectedIndex = i;
  15953. break;
  15954. }
  15955. }
  15956. }
  15957. /**
  15958. * Manipulate Text Tracks settings.
  15959. *
  15960. * @extends ModalDialog
  15961. */
  15962. class TextTrackSettings extends ModalDialog {
  15963. /**
  15964. * Creates an instance of this class.
  15965. *
  15966. * @param { import('../player').default } player
  15967. * The `Player` that this class should be attached to.
  15968. *
  15969. * @param {Object} [options]
  15970. * The key/value store of player options.
  15971. */
  15972. constructor(player, options) {
  15973. options.temporary = false;
  15974. super(player, options);
  15975. this.updateDisplay = this.updateDisplay.bind(this);
  15976. // fill the modal and pretend we have opened it
  15977. this.fill();
  15978. this.hasBeenOpened_ = this.hasBeenFilled_ = true;
  15979. this.endDialog = createEl('p', {
  15980. className: 'vjs-control-text',
  15981. textContent: this.localize('End of dialog window.')
  15982. });
  15983. this.el().appendChild(this.endDialog);
  15984. this.setDefaults();
  15985. // Grab `persistTextTrackSettings` from the player options if not passed in child options
  15986. if (options.persistTextTrackSettings === undefined) {
  15987. this.options_.persistTextTrackSettings = this.options_.playerOptions.persistTextTrackSettings;
  15988. }
  15989. this.on(this.$('.vjs-done-button'), 'click', () => {
  15990. this.saveSettings();
  15991. this.close();
  15992. });
  15993. this.on(this.$('.vjs-default-button'), 'click', () => {
  15994. this.setDefaults();
  15995. this.updateDisplay();
  15996. });
  15997. each(selectConfigs, config => {
  15998. this.on(this.$(config.selector), 'change', this.updateDisplay);
  15999. });
  16000. if (this.options_.persistTextTrackSettings) {
  16001. this.restoreSettings();
  16002. }
  16003. }
  16004. dispose() {
  16005. this.endDialog = null;
  16006. super.dispose();
  16007. }
  16008. /**
  16009. * Create a <select> element with configured options.
  16010. *
  16011. * @param {string} key
  16012. * Configuration key to use during creation.
  16013. *
  16014. * @return {string}
  16015. * An HTML string.
  16016. *
  16017. * @private
  16018. */
  16019. createElSelect_(key, legendId = '', type = 'label') {
  16020. const config = selectConfigs[key];
  16021. const id = config.id.replace('%s', this.id_);
  16022. const selectLabelledbyIds = [legendId, id].join(' ').trim();
  16023. return [`<${type} id="${id}" class="${type === 'label' ? 'vjs-label' : ''}">`, this.localize(config.label), `</${type}>`, `<select aria-labelledby="${selectLabelledbyIds}">`].concat(config.options.map(o => {
  16024. const optionId = id + '-' + o[1].replace(/\W+/g, '');
  16025. return [`<option id="${optionId}" value="${o[0]}" `, `aria-labelledby="${selectLabelledbyIds} ${optionId}">`, this.localize(o[1]), '</option>'].join('');
  16026. })).concat('</select>').join('');
  16027. }
  16028. /**
  16029. * Create foreground color element for the component
  16030. *
  16031. * @return {string}
  16032. * An HTML string.
  16033. *
  16034. * @private
  16035. */
  16036. createElFgColor_() {
  16037. const legendId = `captions-text-legend-${this.id_}`;
  16038. 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('');
  16039. }
  16040. /**
  16041. * Create background color element for the component
  16042. *
  16043. * @return {string}
  16044. * An HTML string.
  16045. *
  16046. * @private
  16047. */
  16048. createElBgColor_() {
  16049. const legendId = `captions-background-${this.id_}`;
  16050. 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('');
  16051. }
  16052. /**
  16053. * Create window color element for the component
  16054. *
  16055. * @return {string}
  16056. * An HTML string.
  16057. *
  16058. * @private
  16059. */
  16060. createElWinColor_() {
  16061. const legendId = `captions-window-${this.id_}`;
  16062. 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('');
  16063. }
  16064. /**
  16065. * Create color elements for the component
  16066. *
  16067. * @return {Element}
  16068. * The element that was created
  16069. *
  16070. * @private
  16071. */
  16072. createElColors_() {
  16073. return createEl('div', {
  16074. className: 'vjs-track-settings-colors',
  16075. innerHTML: [this.createElFgColor_(), this.createElBgColor_(), this.createElWinColor_()].join('')
  16076. });
  16077. }
  16078. /**
  16079. * Create font elements for the component
  16080. *
  16081. * @return {Element}
  16082. * The element that was created.
  16083. *
  16084. * @private
  16085. */
  16086. createElFont_() {
  16087. return createEl('div', {
  16088. className: 'vjs-track-settings-font',
  16089. 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('')
  16090. });
  16091. }
  16092. /**
  16093. * Create controls for the component
  16094. *
  16095. * @return {Element}
  16096. * The element that was created.
  16097. *
  16098. * @private
  16099. */
  16100. createElControls_() {
  16101. const defaultsDescription = this.localize('restore all settings to the default values');
  16102. return createEl('div', {
  16103. className: 'vjs-track-settings-controls',
  16104. 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('')
  16105. });
  16106. }
  16107. content() {
  16108. return [this.createElColors_(), this.createElFont_(), this.createElControls_()];
  16109. }
  16110. label() {
  16111. return this.localize('Caption Settings Dialog');
  16112. }
  16113. description() {
  16114. return this.localize('Beginning of dialog window. Escape will cancel and close the window.');
  16115. }
  16116. buildCSSClass() {
  16117. return super.buildCSSClass() + ' vjs-text-track-settings';
  16118. }
  16119. /**
  16120. * Gets an object of text track settings (or null).
  16121. *
  16122. * @return {Object}
  16123. * An object with config values parsed from the DOM or localStorage.
  16124. */
  16125. getValues() {
  16126. return reduce(selectConfigs, (accum, config, key) => {
  16127. const value = getSelectedOptionValue(this.$(config.selector), config.parser);
  16128. if (value !== undefined) {
  16129. accum[key] = value;
  16130. }
  16131. return accum;
  16132. }, {});
  16133. }
  16134. /**
  16135. * Sets text track settings from an object of values.
  16136. *
  16137. * @param {Object} values
  16138. * An object with config values parsed from the DOM or localStorage.
  16139. */
  16140. setValues(values) {
  16141. each(selectConfigs, (config, key) => {
  16142. setSelectedOption(this.$(config.selector), values[key], config.parser);
  16143. });
  16144. }
  16145. /**
  16146. * Sets all `<select>` elements to their default values.
  16147. */
  16148. setDefaults() {
  16149. each(selectConfigs, config => {
  16150. const index = config.hasOwnProperty('default') ? config.default : 0;
  16151. this.$(config.selector).selectedIndex = index;
  16152. });
  16153. }
  16154. /**
  16155. * Restore texttrack settings from localStorage
  16156. */
  16157. restoreSettings() {
  16158. let values;
  16159. try {
  16160. values = JSON.parse(window.localStorage.getItem(LOCAL_STORAGE_KEY));
  16161. } catch (err) {
  16162. log.warn(err);
  16163. }
  16164. if (values) {
  16165. this.setValues(values);
  16166. }
  16167. }
  16168. /**
  16169. * Save text track settings to localStorage
  16170. */
  16171. saveSettings() {
  16172. if (!this.options_.persistTextTrackSettings) {
  16173. return;
  16174. }
  16175. const values = this.getValues();
  16176. try {
  16177. if (Object.keys(values).length) {
  16178. window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(values));
  16179. } else {
  16180. window.localStorage.removeItem(LOCAL_STORAGE_KEY);
  16181. }
  16182. } catch (err) {
  16183. log.warn(err);
  16184. }
  16185. }
  16186. /**
  16187. * Update display of text track settings
  16188. */
  16189. updateDisplay() {
  16190. const ttDisplay = this.player_.getChild('textTrackDisplay');
  16191. if (ttDisplay) {
  16192. ttDisplay.updateDisplay();
  16193. }
  16194. }
  16195. /**
  16196. * conditionally blur the element and refocus the captions button
  16197. *
  16198. * @private
  16199. */
  16200. conditionalBlur_() {
  16201. this.previouslyActiveEl_ = null;
  16202. const cb = this.player_.controlBar;
  16203. const subsCapsBtn = cb && cb.subsCapsButton;
  16204. const ccBtn = cb && cb.captionsButton;
  16205. if (subsCapsBtn) {
  16206. subsCapsBtn.focus();
  16207. } else if (ccBtn) {
  16208. ccBtn.focus();
  16209. }
  16210. }
  16211. /**
  16212. * Repopulate dialog with new localizations on languagechange
  16213. */
  16214. handleLanguagechange() {
  16215. this.fill();
  16216. }
  16217. }
  16218. Component.registerComponent('TextTrackSettings', TextTrackSettings);
  16219. /**
  16220. * @file resize-manager.js
  16221. */
  16222. /**
  16223. * A Resize Manager. It is in charge of triggering `playerresize` on the player in the right conditions.
  16224. *
  16225. * 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}.
  16226. *
  16227. * If the ResizeObserver is available natively, it will be used. A polyfill can be passed in as an option.
  16228. * If a `playerresize` event is not needed, the ResizeManager component can be removed from the player, see the example below.
  16229. *
  16230. * @example <caption>How to disable the resize manager</caption>
  16231. * const player = videojs('#vid', {
  16232. * resizeManager: false
  16233. * });
  16234. *
  16235. * @see {@link https://wicg.github.io/ResizeObserver/|ResizeObserver specification}
  16236. *
  16237. * @extends Component
  16238. */
  16239. class ResizeManager extends Component {
  16240. /**
  16241. * Create the ResizeManager.
  16242. *
  16243. * @param {Object} player
  16244. * The `Player` that this class should be attached to.
  16245. *
  16246. * @param {Object} [options]
  16247. * The key/value store of ResizeManager options.
  16248. *
  16249. * @param {Object} [options.ResizeObserver]
  16250. * A polyfill for ResizeObserver can be passed in here.
  16251. * If this is set to null it will ignore the native ResizeObserver and fall back to the iframe fallback.
  16252. */
  16253. constructor(player, options) {
  16254. let RESIZE_OBSERVER_AVAILABLE = options.ResizeObserver || window.ResizeObserver;
  16255. // if `null` was passed, we want to disable the ResizeObserver
  16256. if (options.ResizeObserver === null) {
  16257. RESIZE_OBSERVER_AVAILABLE = false;
  16258. }
  16259. // Only create an element when ResizeObserver isn't available
  16260. const options_ = merge({
  16261. createEl: !RESIZE_OBSERVER_AVAILABLE,
  16262. reportTouchActivity: false
  16263. }, options);
  16264. super(player, options_);
  16265. this.ResizeObserver = options.ResizeObserver || window.ResizeObserver;
  16266. this.loadListener_ = null;
  16267. this.resizeObserver_ = null;
  16268. this.debouncedHandler_ = debounce(() => {
  16269. this.resizeHandler();
  16270. }, 100, false, this);
  16271. if (RESIZE_OBSERVER_AVAILABLE) {
  16272. this.resizeObserver_ = new this.ResizeObserver(this.debouncedHandler_);
  16273. this.resizeObserver_.observe(player.el());
  16274. } else {
  16275. this.loadListener_ = () => {
  16276. if (!this.el_ || !this.el_.contentWindow) {
  16277. return;
  16278. }
  16279. const debouncedHandler_ = this.debouncedHandler_;
  16280. let unloadListener_ = this.unloadListener_ = function () {
  16281. off(this, 'resize', debouncedHandler_);
  16282. off(this, 'unload', unloadListener_);
  16283. unloadListener_ = null;
  16284. };
  16285. // safari and edge can unload the iframe before resizemanager dispose
  16286. // we have to dispose of event handlers correctly before that happens
  16287. on(this.el_.contentWindow, 'unload', unloadListener_);
  16288. on(this.el_.contentWindow, 'resize', debouncedHandler_);
  16289. };
  16290. this.one('load', this.loadListener_);
  16291. }
  16292. }
  16293. createEl() {
  16294. return super.createEl('iframe', {
  16295. className: 'vjs-resize-manager',
  16296. tabIndex: -1,
  16297. title: this.localize('No content')
  16298. }, {
  16299. 'aria-hidden': 'true'
  16300. });
  16301. }
  16302. /**
  16303. * Called when a resize is triggered on the iframe or a resize is observed via the ResizeObserver
  16304. *
  16305. * @fires Player#playerresize
  16306. */
  16307. resizeHandler() {
  16308. /**
  16309. * Called when the player size has changed
  16310. *
  16311. * @event Player#playerresize
  16312. * @type {Event}
  16313. */
  16314. // make sure player is still around to trigger
  16315. // prevents this from causing an error after dispose
  16316. if (!this.player_ || !this.player_.trigger) {
  16317. return;
  16318. }
  16319. this.player_.trigger('playerresize');
  16320. }
  16321. dispose() {
  16322. if (this.debouncedHandler_) {
  16323. this.debouncedHandler_.cancel();
  16324. }
  16325. if (this.resizeObserver_) {
  16326. if (this.player_.el()) {
  16327. this.resizeObserver_.unobserve(this.player_.el());
  16328. }
  16329. this.resizeObserver_.disconnect();
  16330. }
  16331. if (this.loadListener_) {
  16332. this.off('load', this.loadListener_);
  16333. }
  16334. if (this.el_ && this.el_.contentWindow && this.unloadListener_) {
  16335. this.unloadListener_.call(this.el_.contentWindow);
  16336. }
  16337. this.ResizeObserver = null;
  16338. this.resizeObserver = null;
  16339. this.debouncedHandler_ = null;
  16340. this.loadListener_ = null;
  16341. super.dispose();
  16342. }
  16343. }
  16344. Component.registerComponent('ResizeManager', ResizeManager);
  16345. const defaults = {
  16346. trackingThreshold: 20,
  16347. liveTolerance: 15
  16348. };
  16349. /*
  16350. track when we are at the live edge, and other helpers for live playback */
  16351. /**
  16352. * A class for checking live current time and determining when the player
  16353. * is at or behind the live edge.
  16354. */
  16355. class LiveTracker extends Component {
  16356. /**
  16357. * Creates an instance of this class.
  16358. *
  16359. * @param { import('./player').default } player
  16360. * The `Player` that this class should be attached to.
  16361. *
  16362. * @param {Object} [options]
  16363. * The key/value store of player options.
  16364. *
  16365. * @param {number} [options.trackingThreshold=20]
  16366. * Number of seconds of live window (seekableEnd - seekableStart) that
  16367. * media needs to have before the liveui will be shown.
  16368. *
  16369. * @param {number} [options.liveTolerance=15]
  16370. * Number of seconds behind live that we have to be
  16371. * before we will be considered non-live. Note that this will only
  16372. * be used when playing at the live edge. This allows large seekable end
  16373. * changes to not effect whether we are live or not.
  16374. */
  16375. constructor(player, options) {
  16376. // LiveTracker does not need an element
  16377. const options_ = merge(defaults, options, {
  16378. createEl: false
  16379. });
  16380. super(player, options_);
  16381. this.trackLiveHandler_ = () => this.trackLive_();
  16382. this.handlePlay_ = e => this.handlePlay(e);
  16383. this.handleFirstTimeupdate_ = e => this.handleFirstTimeupdate(e);
  16384. this.handleSeeked_ = e => this.handleSeeked(e);
  16385. this.seekToLiveEdge_ = e => this.seekToLiveEdge(e);
  16386. this.reset_();
  16387. this.on(this.player_, 'durationchange', e => this.handleDurationchange(e));
  16388. // we should try to toggle tracking on canplay as native playback engines, like Safari
  16389. // may not have the proper values for things like seekableEnd until then
  16390. this.on(this.player_, 'canplay', () => this.toggleTracking());
  16391. }
  16392. /**
  16393. * all the functionality for tracking when seek end changes
  16394. * and for tracking how far past seek end we should be
  16395. */
  16396. trackLive_() {
  16397. const seekable = this.player_.seekable();
  16398. // skip undefined seekable
  16399. if (!seekable || !seekable.length) {
  16400. return;
  16401. }
  16402. const newTime = Number(window.performance.now().toFixed(4));
  16403. const deltaTime = this.lastTime_ === -1 ? 0 : (newTime - this.lastTime_) / 1000;
  16404. this.lastTime_ = newTime;
  16405. this.pastSeekEnd_ = this.pastSeekEnd() + deltaTime;
  16406. const liveCurrentTime = this.liveCurrentTime();
  16407. const currentTime = this.player_.currentTime();
  16408. // we are behind live if any are true
  16409. // 1. the player is paused
  16410. // 2. the user seeked to a location 2 seconds away from live
  16411. // 3. the difference between live and current time is greater
  16412. // liveTolerance which defaults to 15s
  16413. let isBehind = this.player_.paused() || this.seekedBehindLive_ || Math.abs(liveCurrentTime - currentTime) > this.options_.liveTolerance;
  16414. // we cannot be behind if
  16415. // 1. until we have not seen a timeupdate yet
  16416. // 2. liveCurrentTime is Infinity, which happens on Android and Native Safari
  16417. if (!this.timeupdateSeen_ || liveCurrentTime === Infinity) {
  16418. isBehind = false;
  16419. }
  16420. if (isBehind !== this.behindLiveEdge_) {
  16421. this.behindLiveEdge_ = isBehind;
  16422. this.trigger('liveedgechange');
  16423. }
  16424. }
  16425. /**
  16426. * handle a durationchange event on the player
  16427. * and start/stop tracking accordingly.
  16428. */
  16429. handleDurationchange() {
  16430. this.toggleTracking();
  16431. }
  16432. /**
  16433. * start/stop tracking
  16434. */
  16435. toggleTracking() {
  16436. if (this.player_.duration() === Infinity && this.liveWindow() >= this.options_.trackingThreshold) {
  16437. if (this.player_.options_.liveui) {
  16438. this.player_.addClass('vjs-liveui');
  16439. }
  16440. this.startTracking();
  16441. } else {
  16442. this.player_.removeClass('vjs-liveui');
  16443. this.stopTracking();
  16444. }
  16445. }
  16446. /**
  16447. * start tracking live playback
  16448. */
  16449. startTracking() {
  16450. if (this.isTracking()) {
  16451. return;
  16452. }
  16453. // If we haven't seen a timeupdate, we need to check whether playback
  16454. // began before this component started tracking. This can happen commonly
  16455. // when using autoplay.
  16456. if (!this.timeupdateSeen_) {
  16457. this.timeupdateSeen_ = this.player_.hasStarted();
  16458. }
  16459. this.trackingInterval_ = this.setInterval(this.trackLiveHandler_, UPDATE_REFRESH_INTERVAL);
  16460. this.trackLive_();
  16461. this.on(this.player_, ['play', 'pause'], this.trackLiveHandler_);
  16462. if (!this.timeupdateSeen_) {
  16463. this.one(this.player_, 'play', this.handlePlay_);
  16464. this.one(this.player_, 'timeupdate', this.handleFirstTimeupdate_);
  16465. } else {
  16466. this.on(this.player_, 'seeked', this.handleSeeked_);
  16467. }
  16468. }
  16469. /**
  16470. * handle the first timeupdate on the player if it wasn't already playing
  16471. * when live tracker started tracking.
  16472. */
  16473. handleFirstTimeupdate() {
  16474. this.timeupdateSeen_ = true;
  16475. this.on(this.player_, 'seeked', this.handleSeeked_);
  16476. }
  16477. /**
  16478. * Keep track of what time a seek starts, and listen for seeked
  16479. * to find where a seek ends.
  16480. */
  16481. handleSeeked() {
  16482. const timeDiff = Math.abs(this.liveCurrentTime() - this.player_.currentTime());
  16483. this.seekedBehindLive_ = this.nextSeekedFromUser_ && timeDiff > 2;
  16484. this.nextSeekedFromUser_ = false;
  16485. this.trackLive_();
  16486. }
  16487. /**
  16488. * handle the first play on the player, and make sure that we seek
  16489. * right to the live edge.
  16490. */
  16491. handlePlay() {
  16492. this.one(this.player_, 'timeupdate', this.seekToLiveEdge_);
  16493. }
  16494. /**
  16495. * Stop tracking, and set all internal variables to
  16496. * their initial value.
  16497. */
  16498. reset_() {
  16499. this.lastTime_ = -1;
  16500. this.pastSeekEnd_ = 0;
  16501. this.lastSeekEnd_ = -1;
  16502. this.behindLiveEdge_ = true;
  16503. this.timeupdateSeen_ = false;
  16504. this.seekedBehindLive_ = false;
  16505. this.nextSeekedFromUser_ = false;
  16506. this.clearInterval(this.trackingInterval_);
  16507. this.trackingInterval_ = null;
  16508. this.off(this.player_, ['play', 'pause'], this.trackLiveHandler_);
  16509. this.off(this.player_, 'seeked', this.handleSeeked_);
  16510. this.off(this.player_, 'play', this.handlePlay_);
  16511. this.off(this.player_, 'timeupdate', this.handleFirstTimeupdate_);
  16512. this.off(this.player_, 'timeupdate', this.seekToLiveEdge_);
  16513. }
  16514. /**
  16515. * The next seeked event is from the user. Meaning that any seek
  16516. * > 2s behind live will be considered behind live for real and
  16517. * liveTolerance will be ignored.
  16518. */
  16519. nextSeekedFromUser() {
  16520. this.nextSeekedFromUser_ = true;
  16521. }
  16522. /**
  16523. * stop tracking live playback
  16524. */
  16525. stopTracking() {
  16526. if (!this.isTracking()) {
  16527. return;
  16528. }
  16529. this.reset_();
  16530. this.trigger('liveedgechange');
  16531. }
  16532. /**
  16533. * A helper to get the player seekable end
  16534. * so that we don't have to null check everywhere
  16535. *
  16536. * @return {number}
  16537. * The furthest seekable end or Infinity.
  16538. */
  16539. seekableEnd() {
  16540. const seekable = this.player_.seekable();
  16541. const seekableEnds = [];
  16542. let i = seekable ? seekable.length : 0;
  16543. while (i--) {
  16544. seekableEnds.push(seekable.end(i));
  16545. }
  16546. // grab the furthest seekable end after sorting, or if there are none
  16547. // default to Infinity
  16548. return seekableEnds.length ? seekableEnds.sort()[seekableEnds.length - 1] : Infinity;
  16549. }
  16550. /**
  16551. * A helper to get the player seekable start
  16552. * so that we don't have to null check everywhere
  16553. *
  16554. * @return {number}
  16555. * The earliest seekable start or 0.
  16556. */
  16557. seekableStart() {
  16558. const seekable = this.player_.seekable();
  16559. const seekableStarts = [];
  16560. let i = seekable ? seekable.length : 0;
  16561. while (i--) {
  16562. seekableStarts.push(seekable.start(i));
  16563. }
  16564. // grab the first seekable start after sorting, or if there are none
  16565. // default to 0
  16566. return seekableStarts.length ? seekableStarts.sort()[0] : 0;
  16567. }
  16568. /**
  16569. * Get the live time window aka
  16570. * the amount of time between seekable start and
  16571. * live current time.
  16572. *
  16573. * @return {number}
  16574. * The amount of seconds that are seekable in
  16575. * the live video.
  16576. */
  16577. liveWindow() {
  16578. const liveCurrentTime = this.liveCurrentTime();
  16579. // if liveCurrenTime is Infinity then we don't have a liveWindow at all
  16580. if (liveCurrentTime === Infinity) {
  16581. return 0;
  16582. }
  16583. return liveCurrentTime - this.seekableStart();
  16584. }
  16585. /**
  16586. * Determines if the player is live, only checks if this component
  16587. * is tracking live playback or not
  16588. *
  16589. * @return {boolean}
  16590. * Whether liveTracker is tracking
  16591. */
  16592. isLive() {
  16593. return this.isTracking();
  16594. }
  16595. /**
  16596. * Determines if currentTime is at the live edge and won't fall behind
  16597. * on each seekableendchange
  16598. *
  16599. * @return {boolean}
  16600. * Whether playback is at the live edge
  16601. */
  16602. atLiveEdge() {
  16603. return !this.behindLiveEdge();
  16604. }
  16605. /**
  16606. * get what we expect the live current time to be
  16607. *
  16608. * @return {number}
  16609. * The expected live current time
  16610. */
  16611. liveCurrentTime() {
  16612. return this.pastSeekEnd() + this.seekableEnd();
  16613. }
  16614. /**
  16615. * The number of seconds that have occurred after seekable end
  16616. * changed. This will be reset to 0 once seekable end changes.
  16617. *
  16618. * @return {number}
  16619. * Seconds past the current seekable end
  16620. */
  16621. pastSeekEnd() {
  16622. const seekableEnd = this.seekableEnd();
  16623. if (this.lastSeekEnd_ !== -1 && seekableEnd !== this.lastSeekEnd_) {
  16624. this.pastSeekEnd_ = 0;
  16625. }
  16626. this.lastSeekEnd_ = seekableEnd;
  16627. return this.pastSeekEnd_;
  16628. }
  16629. /**
  16630. * If we are currently behind the live edge, aka currentTime will be
  16631. * behind on a seekableendchange
  16632. *
  16633. * @return {boolean}
  16634. * If we are behind the live edge
  16635. */
  16636. behindLiveEdge() {
  16637. return this.behindLiveEdge_;
  16638. }
  16639. /**
  16640. * Whether live tracker is currently tracking or not.
  16641. */
  16642. isTracking() {
  16643. return typeof this.trackingInterval_ === 'number';
  16644. }
  16645. /**
  16646. * Seek to the live edge if we are behind the live edge
  16647. */
  16648. seekToLiveEdge() {
  16649. this.seekedBehindLive_ = false;
  16650. if (this.atLiveEdge()) {
  16651. return;
  16652. }
  16653. this.nextSeekedFromUser_ = false;
  16654. this.player_.currentTime(this.liveCurrentTime());
  16655. }
  16656. /**
  16657. * Dispose of liveTracker
  16658. */
  16659. dispose() {
  16660. this.stopTracking();
  16661. super.dispose();
  16662. }
  16663. }
  16664. Component.registerComponent('LiveTracker', LiveTracker);
  16665. /**
  16666. * Displays an element over the player which contains an optional title and
  16667. * description for the current content.
  16668. *
  16669. * Much of the code for this component originated in the now obsolete
  16670. * videojs-dock plugin: https://github.com/brightcove/videojs-dock/
  16671. *
  16672. * @extends Component
  16673. */
  16674. class TitleBar extends Component {
  16675. constructor(player, options) {
  16676. super(player, options);
  16677. this.on('statechanged', e => this.updateDom_());
  16678. this.updateDom_();
  16679. }
  16680. /**
  16681. * Create the `TitleBar`'s DOM element
  16682. *
  16683. * @return {Element}
  16684. * The element that was created.
  16685. */
  16686. createEl() {
  16687. this.els = {
  16688. title: createEl('div', {
  16689. className: 'vjs-title-bar-title',
  16690. id: `vjs-title-bar-title-${newGUID()}`
  16691. }),
  16692. description: createEl('div', {
  16693. className: 'vjs-title-bar-description',
  16694. id: `vjs-title-bar-description-${newGUID()}`
  16695. })
  16696. };
  16697. return createEl('div', {
  16698. className: 'vjs-title-bar'
  16699. }, {}, Object.values(this.els));
  16700. }
  16701. /**
  16702. * Updates the DOM based on the component's state object.
  16703. */
  16704. updateDom_() {
  16705. const tech = this.player_.tech_;
  16706. const techEl = tech && tech.el_;
  16707. const techAriaAttrs = {
  16708. title: 'aria-labelledby',
  16709. description: 'aria-describedby'
  16710. };
  16711. ['title', 'description'].forEach(k => {
  16712. const value = this.state[k];
  16713. const el = this.els[k];
  16714. const techAriaAttr = techAriaAttrs[k];
  16715. emptyEl(el);
  16716. if (value) {
  16717. textContent(el, value);
  16718. }
  16719. // If there is a tech element available, update its ARIA attributes
  16720. // according to whether a title and/or description have been provided.
  16721. if (techEl) {
  16722. techEl.removeAttribute(techAriaAttr);
  16723. if (value) {
  16724. techEl.setAttribute(techAriaAttr, el.id);
  16725. }
  16726. }
  16727. });
  16728. if (this.state.title || this.state.description) {
  16729. this.show();
  16730. } else {
  16731. this.hide();
  16732. }
  16733. }
  16734. /**
  16735. * Update the contents of the title bar component with new title and
  16736. * description text.
  16737. *
  16738. * If both title and description are missing, the title bar will be hidden.
  16739. *
  16740. * If either title or description are present, the title bar will be visible.
  16741. *
  16742. * NOTE: Any previously set value will be preserved. To unset a previously
  16743. * set value, you must pass an empty string or null.
  16744. *
  16745. * For example:
  16746. *
  16747. * ```
  16748. * update({title: 'foo', description: 'bar'}) // title: 'foo', description: 'bar'
  16749. * update({description: 'bar2'}) // title: 'foo', description: 'bar2'
  16750. * update({title: ''}) // title: '', description: 'bar2'
  16751. * update({title: 'foo', description: null}) // title: 'foo', description: null
  16752. * ```
  16753. *
  16754. * @param {Object} [options={}]
  16755. * An options object. When empty, the title bar will be hidden.
  16756. *
  16757. * @param {string} [options.title]
  16758. * A title to display in the title bar.
  16759. *
  16760. * @param {string} [options.description]
  16761. * A description to display in the title bar.
  16762. */
  16763. update(options) {
  16764. this.setState(options);
  16765. }
  16766. /**
  16767. * Dispose the component.
  16768. */
  16769. dispose() {
  16770. const tech = this.player_.tech_;
  16771. const techEl = tech && tech.el_;
  16772. if (techEl) {
  16773. techEl.removeAttribute('aria-labelledby');
  16774. techEl.removeAttribute('aria-describedby');
  16775. }
  16776. super.dispose();
  16777. this.els = null;
  16778. }
  16779. }
  16780. Component.registerComponent('TitleBar', TitleBar);
  16781. /**
  16782. * This function is used to fire a sourceset when there is something
  16783. * similar to `mediaEl.load()` being called. It will try to find the source via
  16784. * the `src` attribute and then the `<source>` elements. It will then fire `sourceset`
  16785. * with the source that was found or empty string if we cannot know. If it cannot
  16786. * find a source then `sourceset` will not be fired.
  16787. *
  16788. * @param { import('./html5').default } tech
  16789. * The tech object that sourceset was setup on
  16790. *
  16791. * @return {boolean}
  16792. * returns false if the sourceset was not fired and true otherwise.
  16793. */
  16794. const sourcesetLoad = tech => {
  16795. const el = tech.el();
  16796. // if `el.src` is set, that source will be loaded.
  16797. if (el.hasAttribute('src')) {
  16798. tech.triggerSourceset(el.src);
  16799. return true;
  16800. }
  16801. /**
  16802. * Since there isn't a src property on the media element, source elements will be used for
  16803. * implementing the source selection algorithm. This happens asynchronously and
  16804. * for most cases were there is more than one source we cannot tell what source will
  16805. * be loaded, without re-implementing the source selection algorithm. At this time we are not
  16806. * going to do that. There are three special cases that we do handle here though:
  16807. *
  16808. * 1. If there are no sources, do not fire `sourceset`.
  16809. * 2. If there is only one `<source>` with a `src` property/attribute that is our `src`
  16810. * 3. If there is more than one `<source>` but all of them have the same `src` url.
  16811. * That will be our src.
  16812. */
  16813. const sources = tech.$$('source');
  16814. const srcUrls = [];
  16815. let src = '';
  16816. // if there are no sources, do not fire sourceset
  16817. if (!sources.length) {
  16818. return false;
  16819. }
  16820. // only count valid/non-duplicate source elements
  16821. for (let i = 0; i < sources.length; i++) {
  16822. const url = sources[i].src;
  16823. if (url && srcUrls.indexOf(url) === -1) {
  16824. srcUrls.push(url);
  16825. }
  16826. }
  16827. // there were no valid sources
  16828. if (!srcUrls.length) {
  16829. return false;
  16830. }
  16831. // there is only one valid source element url
  16832. // use that
  16833. if (srcUrls.length === 1) {
  16834. src = srcUrls[0];
  16835. }
  16836. tech.triggerSourceset(src);
  16837. return true;
  16838. };
  16839. /**
  16840. * our implementation of an `innerHTML` descriptor for browsers
  16841. * that do not have one.
  16842. */
  16843. const innerHTMLDescriptorPolyfill = Object.defineProperty({}, 'innerHTML', {
  16844. get() {
  16845. return this.cloneNode(true).innerHTML;
  16846. },
  16847. set(v) {
  16848. // make a dummy node to use innerHTML on
  16849. const dummy = document.createElement(this.nodeName.toLowerCase());
  16850. // set innerHTML to the value provided
  16851. dummy.innerHTML = v;
  16852. // make a document fragment to hold the nodes from dummy
  16853. const docFrag = document.createDocumentFragment();
  16854. // copy all of the nodes created by the innerHTML on dummy
  16855. // to the document fragment
  16856. while (dummy.childNodes.length) {
  16857. docFrag.appendChild(dummy.childNodes[0]);
  16858. }
  16859. // remove content
  16860. this.innerText = '';
  16861. // now we add all of that html in one by appending the
  16862. // document fragment. This is how innerHTML does it.
  16863. window.Element.prototype.appendChild.call(this, docFrag);
  16864. // then return the result that innerHTML's setter would
  16865. return this.innerHTML;
  16866. }
  16867. });
  16868. /**
  16869. * Get a property descriptor given a list of priorities and the
  16870. * property to get.
  16871. */
  16872. const getDescriptor = (priority, prop) => {
  16873. let descriptor = {};
  16874. for (let i = 0; i < priority.length; i++) {
  16875. descriptor = Object.getOwnPropertyDescriptor(priority[i], prop);
  16876. if (descriptor && descriptor.set && descriptor.get) {
  16877. break;
  16878. }
  16879. }
  16880. descriptor.enumerable = true;
  16881. descriptor.configurable = true;
  16882. return descriptor;
  16883. };
  16884. const getInnerHTMLDescriptor = tech => getDescriptor([tech.el(), window.HTMLMediaElement.prototype, window.Element.prototype, innerHTMLDescriptorPolyfill], 'innerHTML');
  16885. /**
  16886. * Patches browser internal functions so that we can tell synchronously
  16887. * if a `<source>` was appended to the media element. For some reason this
  16888. * causes a `sourceset` if the the media element is ready and has no source.
  16889. * This happens when:
  16890. * - The page has just loaded and the media element does not have a source.
  16891. * - The media element was emptied of all sources, then `load()` was called.
  16892. *
  16893. * It does this by patching the following functions/properties when they are supported:
  16894. *
  16895. * - `append()` - can be used to add a `<source>` element to the media element
  16896. * - `appendChild()` - can be used to add a `<source>` element to the media element
  16897. * - `insertAdjacentHTML()` - can be used to add a `<source>` element to the media element
  16898. * - `innerHTML` - can be used to add a `<source>` element to the media element
  16899. *
  16900. * @param {Html5} tech
  16901. * The tech object that sourceset is being setup on.
  16902. */
  16903. const firstSourceWatch = function (tech) {
  16904. const el = tech.el();
  16905. // make sure firstSourceWatch isn't setup twice.
  16906. if (el.resetSourceWatch_) {
  16907. return;
  16908. }
  16909. const old = {};
  16910. const innerDescriptor = getInnerHTMLDescriptor(tech);
  16911. const appendWrapper = appendFn => (...args) => {
  16912. const retval = appendFn.apply(el, args);
  16913. sourcesetLoad(tech);
  16914. return retval;
  16915. };
  16916. ['append', 'appendChild', 'insertAdjacentHTML'].forEach(k => {
  16917. if (!el[k]) {
  16918. return;
  16919. }
  16920. // store the old function
  16921. old[k] = el[k];
  16922. // call the old function with a sourceset if a source
  16923. // was loaded
  16924. el[k] = appendWrapper(old[k]);
  16925. });
  16926. Object.defineProperty(el, 'innerHTML', merge(innerDescriptor, {
  16927. set: appendWrapper(innerDescriptor.set)
  16928. }));
  16929. el.resetSourceWatch_ = () => {
  16930. el.resetSourceWatch_ = null;
  16931. Object.keys(old).forEach(k => {
  16932. el[k] = old[k];
  16933. });
  16934. Object.defineProperty(el, 'innerHTML', innerDescriptor);
  16935. };
  16936. // on the first sourceset, we need to revert our changes
  16937. tech.one('sourceset', el.resetSourceWatch_);
  16938. };
  16939. /**
  16940. * our implementation of a `src` descriptor for browsers
  16941. * that do not have one
  16942. */
  16943. const srcDescriptorPolyfill = Object.defineProperty({}, 'src', {
  16944. get() {
  16945. if (this.hasAttribute('src')) {
  16946. return getAbsoluteURL(window.Element.prototype.getAttribute.call(this, 'src'));
  16947. }
  16948. return '';
  16949. },
  16950. set(v) {
  16951. window.Element.prototype.setAttribute.call(this, 'src', v);
  16952. return v;
  16953. }
  16954. });
  16955. const getSrcDescriptor = tech => getDescriptor([tech.el(), window.HTMLMediaElement.prototype, srcDescriptorPolyfill], 'src');
  16956. /**
  16957. * setup `sourceset` handling on the `Html5` tech. This function
  16958. * patches the following element properties/functions:
  16959. *
  16960. * - `src` - to determine when `src` is set
  16961. * - `setAttribute()` - to determine when `src` is set
  16962. * - `load()` - this re-triggers the source selection algorithm, and can
  16963. * cause a sourceset.
  16964. *
  16965. * If there is no source when we are adding `sourceset` support or during a `load()`
  16966. * we also patch the functions listed in `firstSourceWatch`.
  16967. *
  16968. * @param {Html5} tech
  16969. * The tech to patch
  16970. */
  16971. const setupSourceset = function (tech) {
  16972. if (!tech.featuresSourceset) {
  16973. return;
  16974. }
  16975. const el = tech.el();
  16976. // make sure sourceset isn't setup twice.
  16977. if (el.resetSourceset_) {
  16978. return;
  16979. }
  16980. const srcDescriptor = getSrcDescriptor(tech);
  16981. const oldSetAttribute = el.setAttribute;
  16982. const oldLoad = el.load;
  16983. Object.defineProperty(el, 'src', merge(srcDescriptor, {
  16984. set: v => {
  16985. const retval = srcDescriptor.set.call(el, v);
  16986. // we use the getter here to get the actual value set on src
  16987. tech.triggerSourceset(el.src);
  16988. return retval;
  16989. }
  16990. }));
  16991. el.setAttribute = (n, v) => {
  16992. const retval = oldSetAttribute.call(el, n, v);
  16993. if (/src/i.test(n)) {
  16994. tech.triggerSourceset(el.src);
  16995. }
  16996. return retval;
  16997. };
  16998. el.load = () => {
  16999. const retval = oldLoad.call(el);
  17000. // if load was called, but there was no source to fire
  17001. // sourceset on. We have to watch for a source append
  17002. // as that can trigger a `sourceset` when the media element
  17003. // has no source
  17004. if (!sourcesetLoad(tech)) {
  17005. tech.triggerSourceset('');
  17006. firstSourceWatch(tech);
  17007. }
  17008. return retval;
  17009. };
  17010. if (el.currentSrc) {
  17011. tech.triggerSourceset(el.currentSrc);
  17012. } else if (!sourcesetLoad(tech)) {
  17013. firstSourceWatch(tech);
  17014. }
  17015. el.resetSourceset_ = () => {
  17016. el.resetSourceset_ = null;
  17017. el.load = oldLoad;
  17018. el.setAttribute = oldSetAttribute;
  17019. Object.defineProperty(el, 'src', srcDescriptor);
  17020. if (el.resetSourceWatch_) {
  17021. el.resetSourceWatch_();
  17022. }
  17023. };
  17024. };
  17025. /**
  17026. * @file html5.js
  17027. */
  17028. /**
  17029. * HTML5 Media Controller - Wrapper for HTML5 Media API
  17030. *
  17031. * @mixes Tech~SourceHandlerAdditions
  17032. * @extends Tech
  17033. */
  17034. class Html5 extends Tech {
  17035. /**
  17036. * Create an instance of this Tech.
  17037. *
  17038. * @param {Object} [options]
  17039. * The key/value store of player options.
  17040. *
  17041. * @param {Function} [ready]
  17042. * Callback function to call when the `HTML5` Tech is ready.
  17043. */
  17044. constructor(options, ready) {
  17045. super(options, ready);
  17046. const source = options.source;
  17047. let crossoriginTracks = false;
  17048. this.featuresVideoFrameCallback = this.featuresVideoFrameCallback && this.el_.tagName === 'VIDEO';
  17049. // Set the source if one is provided
  17050. // 1) Check if the source is new (if not, we want to keep the original so playback isn't interrupted)
  17051. // 2) Check to see if the network state of the tag was failed at init, and if so, reset the source
  17052. // anyway so the error gets fired.
  17053. if (source && (this.el_.currentSrc !== source.src || options.tag && options.tag.initNetworkState_ === 3)) {
  17054. this.setSource(source);
  17055. } else {
  17056. this.handleLateInit_(this.el_);
  17057. }
  17058. // setup sourceset after late sourceset/init
  17059. if (options.enableSourceset) {
  17060. this.setupSourcesetHandling_();
  17061. }
  17062. this.isScrubbing_ = false;
  17063. if (this.el_.hasChildNodes()) {
  17064. const nodes = this.el_.childNodes;
  17065. let nodesLength = nodes.length;
  17066. const removeNodes = [];
  17067. while (nodesLength--) {
  17068. const node = nodes[nodesLength];
  17069. const nodeName = node.nodeName.toLowerCase();
  17070. if (nodeName === 'track') {
  17071. if (!this.featuresNativeTextTracks) {
  17072. // Empty video tag tracks so the built-in player doesn't use them also.
  17073. // This may not be fast enough to stop HTML5 browsers from reading the tags
  17074. // so we'll need to turn off any default tracks if we're manually doing
  17075. // captions and subtitles. videoElement.textTracks
  17076. removeNodes.push(node);
  17077. } else {
  17078. // store HTMLTrackElement and TextTrack to remote list
  17079. this.remoteTextTrackEls().addTrackElement_(node);
  17080. this.remoteTextTracks().addTrack(node.track);
  17081. this.textTracks().addTrack(node.track);
  17082. if (!crossoriginTracks && !this.el_.hasAttribute('crossorigin') && isCrossOrigin(node.src)) {
  17083. crossoriginTracks = true;
  17084. }
  17085. }
  17086. }
  17087. }
  17088. for (let i = 0; i < removeNodes.length; i++) {
  17089. this.el_.removeChild(removeNodes[i]);
  17090. }
  17091. }
  17092. this.proxyNativeTracks_();
  17093. if (this.featuresNativeTextTracks && crossoriginTracks) {
  17094. 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.');
  17095. }
  17096. // prevent iOS Safari from disabling metadata text tracks during native playback
  17097. this.restoreMetadataTracksInIOSNativePlayer_();
  17098. // Determine if native controls should be used
  17099. // Our goal should be to get the custom controls on mobile solid everywhere
  17100. // so we can remove this all together. Right now this will block custom
  17101. // controls on touch enabled laptops like the Chrome Pixel
  17102. if ((TOUCH_ENABLED || IS_IPHONE) && options.nativeControlsForTouch === true) {
  17103. this.setControls(true);
  17104. }
  17105. // on iOS, we want to proxy `webkitbeginfullscreen` and `webkitendfullscreen`
  17106. // into a `fullscreenchange` event
  17107. this.proxyWebkitFullscreen_();
  17108. this.triggerReady();
  17109. }
  17110. /**
  17111. * Dispose of `HTML5` media element and remove all tracks.
  17112. */
  17113. dispose() {
  17114. if (this.el_ && this.el_.resetSourceset_) {
  17115. this.el_.resetSourceset_();
  17116. }
  17117. Html5.disposeMediaElement(this.el_);
  17118. this.options_ = null;
  17119. // tech will handle clearing of the emulated track list
  17120. super.dispose();
  17121. }
  17122. /**
  17123. * Modify the media element so that we can detect when
  17124. * the source is changed. Fires `sourceset` just after the source has changed
  17125. */
  17126. setupSourcesetHandling_() {
  17127. setupSourceset(this);
  17128. }
  17129. /**
  17130. * When a captions track is enabled in the iOS Safari native player, all other
  17131. * tracks are disabled (including metadata tracks), which nulls all of their
  17132. * associated cue points. This will restore metadata tracks to their pre-fullscreen
  17133. * state in those cases so that cue points are not needlessly lost.
  17134. *
  17135. * @private
  17136. */
  17137. restoreMetadataTracksInIOSNativePlayer_() {
  17138. const textTracks = this.textTracks();
  17139. let metadataTracksPreFullscreenState;
  17140. // captures a snapshot of every metadata track's current state
  17141. const takeMetadataTrackSnapshot = () => {
  17142. metadataTracksPreFullscreenState = [];
  17143. for (let i = 0; i < textTracks.length; i++) {
  17144. const track = textTracks[i];
  17145. if (track.kind === 'metadata') {
  17146. metadataTracksPreFullscreenState.push({
  17147. track,
  17148. storedMode: track.mode
  17149. });
  17150. }
  17151. }
  17152. };
  17153. // snapshot each metadata track's initial state, and update the snapshot
  17154. // each time there is a track 'change' event
  17155. takeMetadataTrackSnapshot();
  17156. textTracks.addEventListener('change', takeMetadataTrackSnapshot);
  17157. this.on('dispose', () => textTracks.removeEventListener('change', takeMetadataTrackSnapshot));
  17158. const restoreTrackMode = () => {
  17159. for (let i = 0; i < metadataTracksPreFullscreenState.length; i++) {
  17160. const storedTrack = metadataTracksPreFullscreenState[i];
  17161. if (storedTrack.track.mode === 'disabled' && storedTrack.track.mode !== storedTrack.storedMode) {
  17162. storedTrack.track.mode = storedTrack.storedMode;
  17163. }
  17164. }
  17165. // we only want this handler to be executed on the first 'change' event
  17166. textTracks.removeEventListener('change', restoreTrackMode);
  17167. };
  17168. // when we enter fullscreen playback, stop updating the snapshot and
  17169. // restore all track modes to their pre-fullscreen state
  17170. this.on('webkitbeginfullscreen', () => {
  17171. textTracks.removeEventListener('change', takeMetadataTrackSnapshot);
  17172. // remove the listener before adding it just in case it wasn't previously removed
  17173. textTracks.removeEventListener('change', restoreTrackMode);
  17174. textTracks.addEventListener('change', restoreTrackMode);
  17175. });
  17176. // start updating the snapshot again after leaving fullscreen
  17177. this.on('webkitendfullscreen', () => {
  17178. // remove the listener before adding it just in case it wasn't previously removed
  17179. textTracks.removeEventListener('change', takeMetadataTrackSnapshot);
  17180. textTracks.addEventListener('change', takeMetadataTrackSnapshot);
  17181. // remove the restoreTrackMode handler in case it wasn't triggered during fullscreen playback
  17182. textTracks.removeEventListener('change', restoreTrackMode);
  17183. });
  17184. }
  17185. /**
  17186. * Attempt to force override of tracks for the given type
  17187. *
  17188. * @param {string} type - Track type to override, possible values include 'Audio',
  17189. * 'Video', and 'Text'.
  17190. * @param {boolean} override - If set to true native audio/video will be overridden,
  17191. * otherwise native audio/video will potentially be used.
  17192. * @private
  17193. */
  17194. overrideNative_(type, override) {
  17195. // If there is no behavioral change don't add/remove listeners
  17196. if (override !== this[`featuresNative${type}Tracks`]) {
  17197. return;
  17198. }
  17199. const lowerCaseType = type.toLowerCase();
  17200. if (this[`${lowerCaseType}TracksListeners_`]) {
  17201. Object.keys(this[`${lowerCaseType}TracksListeners_`]).forEach(eventName => {
  17202. const elTracks = this.el()[`${lowerCaseType}Tracks`];
  17203. elTracks.removeEventListener(eventName, this[`${lowerCaseType}TracksListeners_`][eventName]);
  17204. });
  17205. }
  17206. this[`featuresNative${type}Tracks`] = !override;
  17207. this[`${lowerCaseType}TracksListeners_`] = null;
  17208. this.proxyNativeTracksForType_(lowerCaseType);
  17209. }
  17210. /**
  17211. * Attempt to force override of native audio tracks.
  17212. *
  17213. * @param {boolean} override - If set to true native audio will be overridden,
  17214. * otherwise native audio will potentially be used.
  17215. */
  17216. overrideNativeAudioTracks(override) {
  17217. this.overrideNative_('Audio', override);
  17218. }
  17219. /**
  17220. * Attempt to force override of native video tracks.
  17221. *
  17222. * @param {boolean} override - If set to true native video will be overridden,
  17223. * otherwise native video will potentially be used.
  17224. */
  17225. overrideNativeVideoTracks(override) {
  17226. this.overrideNative_('Video', override);
  17227. }
  17228. /**
  17229. * Proxy native track list events for the given type to our track
  17230. * lists if the browser we are playing in supports that type of track list.
  17231. *
  17232. * @param {string} name - Track type; values include 'audio', 'video', and 'text'
  17233. * @private
  17234. */
  17235. proxyNativeTracksForType_(name) {
  17236. const props = NORMAL[name];
  17237. const elTracks = this.el()[props.getterName];
  17238. const techTracks = this[props.getterName]();
  17239. if (!this[`featuresNative${props.capitalName}Tracks`] || !elTracks || !elTracks.addEventListener) {
  17240. return;
  17241. }
  17242. const listeners = {
  17243. change: e => {
  17244. const event = {
  17245. type: 'change',
  17246. target: techTracks,
  17247. currentTarget: techTracks,
  17248. srcElement: techTracks
  17249. };
  17250. techTracks.trigger(event);
  17251. // if we are a text track change event, we should also notify the
  17252. // remote text track list. This can potentially cause a false positive
  17253. // if we were to get a change event on a non-remote track and
  17254. // we triggered the event on the remote text track list which doesn't
  17255. // contain that track. However, best practices mean looping through the
  17256. // list of tracks and searching for the appropriate mode value, so,
  17257. // this shouldn't pose an issue
  17258. if (name === 'text') {
  17259. this[REMOTE.remoteText.getterName]().trigger(event);
  17260. }
  17261. },
  17262. addtrack(e) {
  17263. techTracks.addTrack(e.track);
  17264. },
  17265. removetrack(e) {
  17266. techTracks.removeTrack(e.track);
  17267. }
  17268. };
  17269. const removeOldTracks = function () {
  17270. const removeTracks = [];
  17271. for (let i = 0; i < techTracks.length; i++) {
  17272. let found = false;
  17273. for (let j = 0; j < elTracks.length; j++) {
  17274. if (elTracks[j] === techTracks[i]) {
  17275. found = true;
  17276. break;
  17277. }
  17278. }
  17279. if (!found) {
  17280. removeTracks.push(techTracks[i]);
  17281. }
  17282. }
  17283. while (removeTracks.length) {
  17284. techTracks.removeTrack(removeTracks.shift());
  17285. }
  17286. };
  17287. this[props.getterName + 'Listeners_'] = listeners;
  17288. Object.keys(listeners).forEach(eventName => {
  17289. const listener = listeners[eventName];
  17290. elTracks.addEventListener(eventName, listener);
  17291. this.on('dispose', e => elTracks.removeEventListener(eventName, listener));
  17292. });
  17293. // Remove (native) tracks that are not used anymore
  17294. this.on('loadstart', removeOldTracks);
  17295. this.on('dispose', e => this.off('loadstart', removeOldTracks));
  17296. }
  17297. /**
  17298. * Proxy all native track list events to our track lists if the browser we are playing
  17299. * in supports that type of track list.
  17300. *
  17301. * @private
  17302. */
  17303. proxyNativeTracks_() {
  17304. NORMAL.names.forEach(name => {
  17305. this.proxyNativeTracksForType_(name);
  17306. });
  17307. }
  17308. /**
  17309. * Create the `Html5` Tech's DOM element.
  17310. *
  17311. * @return {Element}
  17312. * The element that gets created.
  17313. */
  17314. createEl() {
  17315. let el = this.options_.tag;
  17316. // Check if this browser supports moving the element into the box.
  17317. // On the iPhone video will break if you move the element,
  17318. // So we have to create a brand new element.
  17319. // If we ingested the player div, we do not need to move the media element.
  17320. if (!el || !(this.options_.playerElIngest || this.movingMediaElementInDOM)) {
  17321. // If the original tag is still there, clone and remove it.
  17322. if (el) {
  17323. const clone = el.cloneNode(true);
  17324. if (el.parentNode) {
  17325. el.parentNode.insertBefore(clone, el);
  17326. }
  17327. Html5.disposeMediaElement(el);
  17328. el = clone;
  17329. } else {
  17330. el = document.createElement('video');
  17331. // determine if native controls should be used
  17332. const tagAttributes = this.options_.tag && getAttributes(this.options_.tag);
  17333. const attributes = merge({}, tagAttributes);
  17334. if (!TOUCH_ENABLED || this.options_.nativeControlsForTouch !== true) {
  17335. delete attributes.controls;
  17336. }
  17337. setAttributes(el, Object.assign(attributes, {
  17338. id: this.options_.techId,
  17339. class: 'vjs-tech'
  17340. }));
  17341. }
  17342. el.playerId = this.options_.playerId;
  17343. }
  17344. if (typeof this.options_.preload !== 'undefined') {
  17345. setAttribute(el, 'preload', this.options_.preload);
  17346. }
  17347. if (this.options_.disablePictureInPicture !== undefined) {
  17348. el.disablePictureInPicture = this.options_.disablePictureInPicture;
  17349. }
  17350. // Update specific tag settings, in case they were overridden
  17351. // `autoplay` has to be *last* so that `muted` and `playsinline` are present
  17352. // when iOS/Safari or other browsers attempt to autoplay.
  17353. const settingsAttrs = ['loop', 'muted', 'playsinline', 'autoplay'];
  17354. for (let i = 0; i < settingsAttrs.length; i++) {
  17355. const attr = settingsAttrs[i];
  17356. const value = this.options_[attr];
  17357. if (typeof value !== 'undefined') {
  17358. if (value) {
  17359. setAttribute(el, attr, attr);
  17360. } else {
  17361. removeAttribute(el, attr);
  17362. }
  17363. el[attr] = value;
  17364. }
  17365. }
  17366. return el;
  17367. }
  17368. /**
  17369. * This will be triggered if the loadstart event has already fired, before videojs was
  17370. * ready. Two known examples of when this can happen are:
  17371. * 1. If we're loading the playback object after it has started loading
  17372. * 2. The media is already playing the (often with autoplay on) then
  17373. *
  17374. * This function will fire another loadstart so that videojs can catchup.
  17375. *
  17376. * @fires Tech#loadstart
  17377. *
  17378. * @return {undefined}
  17379. * returns nothing.
  17380. */
  17381. handleLateInit_(el) {
  17382. if (el.networkState === 0 || el.networkState === 3) {
  17383. // The video element hasn't started loading the source yet
  17384. // or didn't find a source
  17385. return;
  17386. }
  17387. if (el.readyState === 0) {
  17388. // NetworkState is set synchronously BUT loadstart is fired at the
  17389. // end of the current stack, usually before setInterval(fn, 0).
  17390. // So at this point we know loadstart may have already fired or is
  17391. // about to fire, and either way the player hasn't seen it yet.
  17392. // We don't want to fire loadstart prematurely here and cause a
  17393. // double loadstart so we'll wait and see if it happens between now
  17394. // and the next loop, and fire it if not.
  17395. // HOWEVER, we also want to make sure it fires before loadedmetadata
  17396. // which could also happen between now and the next loop, so we'll
  17397. // watch for that also.
  17398. let loadstartFired = false;
  17399. const setLoadstartFired = function () {
  17400. loadstartFired = true;
  17401. };
  17402. this.on('loadstart', setLoadstartFired);
  17403. const triggerLoadstart = function () {
  17404. // We did miss the original loadstart. Make sure the player
  17405. // sees loadstart before loadedmetadata
  17406. if (!loadstartFired) {
  17407. this.trigger('loadstart');
  17408. }
  17409. };
  17410. this.on('loadedmetadata', triggerLoadstart);
  17411. this.ready(function () {
  17412. this.off('loadstart', setLoadstartFired);
  17413. this.off('loadedmetadata', triggerLoadstart);
  17414. if (!loadstartFired) {
  17415. // We did miss the original native loadstart. Fire it now.
  17416. this.trigger('loadstart');
  17417. }
  17418. });
  17419. return;
  17420. }
  17421. // From here on we know that loadstart already fired and we missed it.
  17422. // The other readyState events aren't as much of a problem if we double
  17423. // them, so not going to go to as much trouble as loadstart to prevent
  17424. // that unless we find reason to.
  17425. const eventsToTrigger = ['loadstart'];
  17426. // loadedmetadata: newly equal to HAVE_METADATA (1) or greater
  17427. eventsToTrigger.push('loadedmetadata');
  17428. // loadeddata: newly increased to HAVE_CURRENT_DATA (2) or greater
  17429. if (el.readyState >= 2) {
  17430. eventsToTrigger.push('loadeddata');
  17431. }
  17432. // canplay: newly increased to HAVE_FUTURE_DATA (3) or greater
  17433. if (el.readyState >= 3) {
  17434. eventsToTrigger.push('canplay');
  17435. }
  17436. // canplaythrough: newly equal to HAVE_ENOUGH_DATA (4)
  17437. if (el.readyState >= 4) {
  17438. eventsToTrigger.push('canplaythrough');
  17439. }
  17440. // We still need to give the player time to add event listeners
  17441. this.ready(function () {
  17442. eventsToTrigger.forEach(function (type) {
  17443. this.trigger(type);
  17444. }, this);
  17445. });
  17446. }
  17447. /**
  17448. * Set whether we are scrubbing or not.
  17449. * This is used to decide whether we should use `fastSeek` or not.
  17450. * `fastSeek` is used to provide trick play on Safari browsers.
  17451. *
  17452. * @param {boolean} isScrubbing
  17453. * - true for we are currently scrubbing
  17454. * - false for we are no longer scrubbing
  17455. */
  17456. setScrubbing(isScrubbing) {
  17457. this.isScrubbing_ = isScrubbing;
  17458. }
  17459. /**
  17460. * Get whether we are scrubbing or not.
  17461. *
  17462. * @return {boolean} isScrubbing
  17463. * - true for we are currently scrubbing
  17464. * - false for we are no longer scrubbing
  17465. */
  17466. scrubbing() {
  17467. return this.isScrubbing_;
  17468. }
  17469. /**
  17470. * Set current time for the `HTML5` tech.
  17471. *
  17472. * @param {number} seconds
  17473. * Set the current time of the media to this.
  17474. */
  17475. setCurrentTime(seconds) {
  17476. try {
  17477. if (this.isScrubbing_ && this.el_.fastSeek && IS_ANY_SAFARI) {
  17478. this.el_.fastSeek(seconds);
  17479. } else {
  17480. this.el_.currentTime = seconds;
  17481. }
  17482. } catch (e) {
  17483. log(e, 'Video is not ready. (Video.js)');
  17484. // this.warning(VideoJS.warnings.videoNotReady);
  17485. }
  17486. }
  17487. /**
  17488. * Get the current duration of the HTML5 media element.
  17489. *
  17490. * @return {number}
  17491. * The duration of the media or 0 if there is no duration.
  17492. */
  17493. duration() {
  17494. // Android Chrome will report duration as Infinity for VOD HLS until after
  17495. // playback has started, which triggers the live display erroneously.
  17496. // Return NaN if playback has not started and trigger a durationupdate once
  17497. // the duration can be reliably known.
  17498. if (this.el_.duration === Infinity && IS_ANDROID && IS_CHROME && this.el_.currentTime === 0) {
  17499. // Wait for the first `timeupdate` with currentTime > 0 - there may be
  17500. // several with 0
  17501. const checkProgress = () => {
  17502. if (this.el_.currentTime > 0) {
  17503. // Trigger durationchange for genuinely live video
  17504. if (this.el_.duration === Infinity) {
  17505. this.trigger('durationchange');
  17506. }
  17507. this.off('timeupdate', checkProgress);
  17508. }
  17509. };
  17510. this.on('timeupdate', checkProgress);
  17511. return NaN;
  17512. }
  17513. return this.el_.duration || NaN;
  17514. }
  17515. /**
  17516. * Get the current width of the HTML5 media element.
  17517. *
  17518. * @return {number}
  17519. * The width of the HTML5 media element.
  17520. */
  17521. width() {
  17522. return this.el_.offsetWidth;
  17523. }
  17524. /**
  17525. * Get the current height of the HTML5 media element.
  17526. *
  17527. * @return {number}
  17528. * The height of the HTML5 media element.
  17529. */
  17530. height() {
  17531. return this.el_.offsetHeight;
  17532. }
  17533. /**
  17534. * Proxy iOS `webkitbeginfullscreen` and `webkitendfullscreen` into
  17535. * `fullscreenchange` event.
  17536. *
  17537. * @private
  17538. * @fires fullscreenchange
  17539. * @listens webkitendfullscreen
  17540. * @listens webkitbeginfullscreen
  17541. * @listens webkitbeginfullscreen
  17542. */
  17543. proxyWebkitFullscreen_() {
  17544. if (!('webkitDisplayingFullscreen' in this.el_)) {
  17545. return;
  17546. }
  17547. const endFn = function () {
  17548. this.trigger('fullscreenchange', {
  17549. isFullscreen: false
  17550. });
  17551. // Safari will sometimes set controls on the videoelement when existing fullscreen.
  17552. if (this.el_.controls && !this.options_.nativeControlsForTouch && this.controls()) {
  17553. this.el_.controls = false;
  17554. }
  17555. };
  17556. const beginFn = function () {
  17557. if ('webkitPresentationMode' in this.el_ && this.el_.webkitPresentationMode !== 'picture-in-picture') {
  17558. this.one('webkitendfullscreen', endFn);
  17559. this.trigger('fullscreenchange', {
  17560. isFullscreen: true,
  17561. // set a flag in case another tech triggers fullscreenchange
  17562. nativeIOSFullscreen: true
  17563. });
  17564. }
  17565. };
  17566. this.on('webkitbeginfullscreen', beginFn);
  17567. this.on('dispose', () => {
  17568. this.off('webkitbeginfullscreen', beginFn);
  17569. this.off('webkitendfullscreen', endFn);
  17570. });
  17571. }
  17572. /**
  17573. * Check if fullscreen is supported on the video el.
  17574. *
  17575. * @return {boolean}
  17576. * - True if fullscreen is supported.
  17577. * - False if fullscreen is not supported.
  17578. */
  17579. supportsFullScreen() {
  17580. return typeof this.el_.webkitEnterFullScreen === 'function';
  17581. }
  17582. /**
  17583. * Request that the `HTML5` Tech enter fullscreen.
  17584. */
  17585. enterFullScreen() {
  17586. const video = this.el_;
  17587. if (video.paused && video.networkState <= video.HAVE_METADATA) {
  17588. // attempt to prime the video element for programmatic access
  17589. // this isn't necessary on the desktop but shouldn't hurt
  17590. silencePromise(this.el_.play());
  17591. // playing and pausing synchronously during the transition to fullscreen
  17592. // can get iOS ~6.1 devices into a play/pause loop
  17593. this.setTimeout(function () {
  17594. video.pause();
  17595. try {
  17596. video.webkitEnterFullScreen();
  17597. } catch (e) {
  17598. this.trigger('fullscreenerror', e);
  17599. }
  17600. }, 0);
  17601. } else {
  17602. try {
  17603. video.webkitEnterFullScreen();
  17604. } catch (e) {
  17605. this.trigger('fullscreenerror', e);
  17606. }
  17607. }
  17608. }
  17609. /**
  17610. * Request that the `HTML5` Tech exit fullscreen.
  17611. */
  17612. exitFullScreen() {
  17613. if (!this.el_.webkitDisplayingFullscreen) {
  17614. this.trigger('fullscreenerror', new Error('The video is not fullscreen'));
  17615. return;
  17616. }
  17617. this.el_.webkitExitFullScreen();
  17618. }
  17619. /**
  17620. * Create a floating video window always on top of other windows so that users may
  17621. * continue consuming media while they interact with other content sites, or
  17622. * applications on their device.
  17623. *
  17624. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  17625. *
  17626. * @return {Promise}
  17627. * A promise with a Picture-in-Picture window.
  17628. */
  17629. requestPictureInPicture() {
  17630. return this.el_.requestPictureInPicture();
  17631. }
  17632. /**
  17633. * Native requestVideoFrameCallback if supported by browser/tech, or fallback
  17634. * Don't use rVCF on Safari when DRM is playing, as it doesn't fire
  17635. * Needs to be checked later than the constructor
  17636. * This will be a false positive for clear sources loaded after a Fairplay source
  17637. *
  17638. * @param {function} cb function to call
  17639. * @return {number} id of request
  17640. */
  17641. requestVideoFrameCallback(cb) {
  17642. if (this.featuresVideoFrameCallback && !this.el_.webkitKeys) {
  17643. return this.el_.requestVideoFrameCallback(cb);
  17644. }
  17645. return super.requestVideoFrameCallback(cb);
  17646. }
  17647. /**
  17648. * Native or fallback requestVideoFrameCallback
  17649. *
  17650. * @param {number} id request id to cancel
  17651. */
  17652. cancelVideoFrameCallback(id) {
  17653. if (this.featuresVideoFrameCallback && !this.el_.webkitKeys) {
  17654. this.el_.cancelVideoFrameCallback(id);
  17655. } else {
  17656. super.cancelVideoFrameCallback(id);
  17657. }
  17658. }
  17659. /**
  17660. * A getter/setter for the `Html5` Tech's source object.
  17661. * > Note: Please use {@link Html5#setSource}
  17662. *
  17663. * @param {Tech~SourceObject} [src]
  17664. * The source object you want to set on the `HTML5` techs element.
  17665. *
  17666. * @return {Tech~SourceObject|undefined}
  17667. * - The current source object when a source is not passed in.
  17668. * - undefined when setting
  17669. *
  17670. * @deprecated Since version 5.
  17671. */
  17672. src(src) {
  17673. if (src === undefined) {
  17674. return this.el_.src;
  17675. }
  17676. // Setting src through `src` instead of `setSrc` will be deprecated
  17677. this.setSrc(src);
  17678. }
  17679. /**
  17680. * Reset the tech by removing all sources and then calling
  17681. * {@link Html5.resetMediaElement}.
  17682. */
  17683. reset() {
  17684. Html5.resetMediaElement(this.el_);
  17685. }
  17686. /**
  17687. * Get the current source on the `HTML5` Tech. Falls back to returning the source from
  17688. * the HTML5 media element.
  17689. *
  17690. * @return {Tech~SourceObject}
  17691. * The current source object from the HTML5 tech. With a fallback to the
  17692. * elements source.
  17693. */
  17694. currentSrc() {
  17695. if (this.currentSource_) {
  17696. return this.currentSource_.src;
  17697. }
  17698. return this.el_.currentSrc;
  17699. }
  17700. /**
  17701. * Set controls attribute for the HTML5 media Element.
  17702. *
  17703. * @param {string} val
  17704. * Value to set the controls attribute to
  17705. */
  17706. setControls(val) {
  17707. this.el_.controls = !!val;
  17708. }
  17709. /**
  17710. * Create and returns a remote {@link TextTrack} object.
  17711. *
  17712. * @param {string} kind
  17713. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata)
  17714. *
  17715. * @param {string} [label]
  17716. * Label to identify the text track
  17717. *
  17718. * @param {string} [language]
  17719. * Two letter language abbreviation
  17720. *
  17721. * @return {TextTrack}
  17722. * The TextTrack that gets created.
  17723. */
  17724. addTextTrack(kind, label, language) {
  17725. if (!this.featuresNativeTextTracks) {
  17726. return super.addTextTrack(kind, label, language);
  17727. }
  17728. return this.el_.addTextTrack(kind, label, language);
  17729. }
  17730. /**
  17731. * Creates either native TextTrack or an emulated TextTrack depending
  17732. * on the value of `featuresNativeTextTracks`
  17733. *
  17734. * @param {Object} options
  17735. * The object should contain the options to initialize the TextTrack with.
  17736. *
  17737. * @param {string} [options.kind]
  17738. * `TextTrack` kind (subtitles, captions, descriptions, chapters, or metadata).
  17739. *
  17740. * @param {string} [options.label]
  17741. * Label to identify the text track
  17742. *
  17743. * @param {string} [options.language]
  17744. * Two letter language abbreviation.
  17745. *
  17746. * @param {boolean} [options.default]
  17747. * Default this track to on.
  17748. *
  17749. * @param {string} [options.id]
  17750. * The internal id to assign this track.
  17751. *
  17752. * @param {string} [options.src]
  17753. * A source url for the track.
  17754. *
  17755. * @return {HTMLTrackElement}
  17756. * The track element that gets created.
  17757. */
  17758. createRemoteTextTrack(options) {
  17759. if (!this.featuresNativeTextTracks) {
  17760. return super.createRemoteTextTrack(options);
  17761. }
  17762. const htmlTrackElement = document.createElement('track');
  17763. if (options.kind) {
  17764. htmlTrackElement.kind = options.kind;
  17765. }
  17766. if (options.label) {
  17767. htmlTrackElement.label = options.label;
  17768. }
  17769. if (options.language || options.srclang) {
  17770. htmlTrackElement.srclang = options.language || options.srclang;
  17771. }
  17772. if (options.default) {
  17773. htmlTrackElement.default = options.default;
  17774. }
  17775. if (options.id) {
  17776. htmlTrackElement.id = options.id;
  17777. }
  17778. if (options.src) {
  17779. htmlTrackElement.src = options.src;
  17780. }
  17781. return htmlTrackElement;
  17782. }
  17783. /**
  17784. * Creates a remote text track object and returns an html track element.
  17785. *
  17786. * @param {Object} options The object should contain values for
  17787. * kind, language, label, and src (location of the WebVTT file)
  17788. * @param {boolean} [manualCleanup=false] if set to true, the TextTrack
  17789. * will not be removed from the TextTrackList and HtmlTrackElementList
  17790. * after a source change
  17791. * @return {HTMLTrackElement} An Html Track Element.
  17792. * This can be an emulated {@link HTMLTrackElement} or a native one.
  17793. *
  17794. */
  17795. addRemoteTextTrack(options, manualCleanup) {
  17796. const htmlTrackElement = super.addRemoteTextTrack(options, manualCleanup);
  17797. if (this.featuresNativeTextTracks) {
  17798. this.el().appendChild(htmlTrackElement);
  17799. }
  17800. return htmlTrackElement;
  17801. }
  17802. /**
  17803. * Remove remote `TextTrack` from `TextTrackList` object
  17804. *
  17805. * @param {TextTrack} track
  17806. * `TextTrack` object to remove
  17807. */
  17808. removeRemoteTextTrack(track) {
  17809. super.removeRemoteTextTrack(track);
  17810. if (this.featuresNativeTextTracks) {
  17811. const tracks = this.$$('track');
  17812. let i = tracks.length;
  17813. while (i--) {
  17814. if (track === tracks[i] || track === tracks[i].track) {
  17815. this.el().removeChild(tracks[i]);
  17816. }
  17817. }
  17818. }
  17819. }
  17820. /**
  17821. * Gets available media playback quality metrics as specified by the W3C's Media
  17822. * Playback Quality API.
  17823. *
  17824. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  17825. *
  17826. * @return {Object}
  17827. * An object with supported media playback quality metrics
  17828. */
  17829. getVideoPlaybackQuality() {
  17830. if (typeof this.el().getVideoPlaybackQuality === 'function') {
  17831. return this.el().getVideoPlaybackQuality();
  17832. }
  17833. const videoPlaybackQuality = {};
  17834. if (typeof this.el().webkitDroppedFrameCount !== 'undefined' && typeof this.el().webkitDecodedFrameCount !== 'undefined') {
  17835. videoPlaybackQuality.droppedVideoFrames = this.el().webkitDroppedFrameCount;
  17836. videoPlaybackQuality.totalVideoFrames = this.el().webkitDecodedFrameCount;
  17837. }
  17838. if (window.performance) {
  17839. videoPlaybackQuality.creationTime = window.performance.now();
  17840. }
  17841. return videoPlaybackQuality;
  17842. }
  17843. }
  17844. /* HTML5 Support Testing ---------------------------------------------------- */
  17845. /**
  17846. * Element for testing browser HTML5 media capabilities
  17847. *
  17848. * @type {Element}
  17849. * @constant
  17850. * @private
  17851. */
  17852. defineLazyProperty(Html5, 'TEST_VID', function () {
  17853. if (!isReal()) {
  17854. return;
  17855. }
  17856. const video = document.createElement('video');
  17857. const track = document.createElement('track');
  17858. track.kind = 'captions';
  17859. track.srclang = 'en';
  17860. track.label = 'English';
  17861. video.appendChild(track);
  17862. return video;
  17863. });
  17864. /**
  17865. * Check if HTML5 media is supported by this browser/device.
  17866. *
  17867. * @return {boolean}
  17868. * - True if HTML5 media is supported.
  17869. * - False if HTML5 media is not supported.
  17870. */
  17871. Html5.isSupported = function () {
  17872. // IE with no Media Player is a LIAR! (#984)
  17873. try {
  17874. Html5.TEST_VID.volume = 0.5;
  17875. } catch (e) {
  17876. return false;
  17877. }
  17878. return !!(Html5.TEST_VID && Html5.TEST_VID.canPlayType);
  17879. };
  17880. /**
  17881. * Check if the tech can support the given type
  17882. *
  17883. * @param {string} type
  17884. * The mimetype to check
  17885. * @return {string} 'probably', 'maybe', or '' (empty string)
  17886. */
  17887. Html5.canPlayType = function (type) {
  17888. return Html5.TEST_VID.canPlayType(type);
  17889. };
  17890. /**
  17891. * Check if the tech can support the given source
  17892. *
  17893. * @param {Object} srcObj
  17894. * The source object
  17895. * @param {Object} options
  17896. * The options passed to the tech
  17897. * @return {string} 'probably', 'maybe', or '' (empty string)
  17898. */
  17899. Html5.canPlaySource = function (srcObj, options) {
  17900. return Html5.canPlayType(srcObj.type);
  17901. };
  17902. /**
  17903. * Check if the volume can be changed in this browser/device.
  17904. * Volume cannot be changed in a lot of mobile devices.
  17905. * Specifically, it can't be changed from 1 on iOS.
  17906. *
  17907. * @return {boolean}
  17908. * - True if volume can be controlled
  17909. * - False otherwise
  17910. */
  17911. Html5.canControlVolume = function () {
  17912. // IE will error if Windows Media Player not installed #3315
  17913. try {
  17914. const volume = Html5.TEST_VID.volume;
  17915. Html5.TEST_VID.volume = volume / 2 + 0.1;
  17916. const canControl = volume !== Html5.TEST_VID.volume;
  17917. // With the introduction of iOS 15, there are cases where the volume is read as
  17918. // changed but reverts back to its original state at the start of the next tick.
  17919. // To determine whether volume can be controlled on iOS,
  17920. // a timeout is set and the volume is checked asynchronously.
  17921. // Since `features` doesn't currently work asynchronously, the value is manually set.
  17922. if (canControl && IS_IOS) {
  17923. window.setTimeout(() => {
  17924. if (Html5 && Html5.prototype) {
  17925. Html5.prototype.featuresVolumeControl = volume !== Html5.TEST_VID.volume;
  17926. }
  17927. });
  17928. // default iOS to false, which will be updated in the timeout above.
  17929. return false;
  17930. }
  17931. return canControl;
  17932. } catch (e) {
  17933. return false;
  17934. }
  17935. };
  17936. /**
  17937. * Check if the volume can be muted in this browser/device.
  17938. * Some devices, e.g. iOS, don't allow changing volume
  17939. * but permits muting/unmuting.
  17940. *
  17941. * @return {boolean}
  17942. * - True if volume can be muted
  17943. * - False otherwise
  17944. */
  17945. Html5.canMuteVolume = function () {
  17946. try {
  17947. const muted = Html5.TEST_VID.muted;
  17948. // in some versions of iOS muted property doesn't always
  17949. // work, so we want to set both property and attribute
  17950. Html5.TEST_VID.muted = !muted;
  17951. if (Html5.TEST_VID.muted) {
  17952. setAttribute(Html5.TEST_VID, 'muted', 'muted');
  17953. } else {
  17954. removeAttribute(Html5.TEST_VID, 'muted', 'muted');
  17955. }
  17956. return muted !== Html5.TEST_VID.muted;
  17957. } catch (e) {
  17958. return false;
  17959. }
  17960. };
  17961. /**
  17962. * Check if the playback rate can be changed in this browser/device.
  17963. *
  17964. * @return {boolean}
  17965. * - True if playback rate can be controlled
  17966. * - False otherwise
  17967. */
  17968. Html5.canControlPlaybackRate = function () {
  17969. // Playback rate API is implemented in Android Chrome, but doesn't do anything
  17970. // https://github.com/videojs/video.js/issues/3180
  17971. if (IS_ANDROID && IS_CHROME && CHROME_VERSION < 58) {
  17972. return false;
  17973. }
  17974. // IE will error if Windows Media Player not installed #3315
  17975. try {
  17976. const playbackRate = Html5.TEST_VID.playbackRate;
  17977. Html5.TEST_VID.playbackRate = playbackRate / 2 + 0.1;
  17978. return playbackRate !== Html5.TEST_VID.playbackRate;
  17979. } catch (e) {
  17980. return false;
  17981. }
  17982. };
  17983. /**
  17984. * Check if we can override a video/audio elements attributes, with
  17985. * Object.defineProperty.
  17986. *
  17987. * @return {boolean}
  17988. * - True if builtin attributes can be overridden
  17989. * - False otherwise
  17990. */
  17991. Html5.canOverrideAttributes = function () {
  17992. // if we cannot overwrite the src/innerHTML property, there is no support
  17993. // iOS 7 safari for instance cannot do this.
  17994. try {
  17995. const noop = () => {};
  17996. Object.defineProperty(document.createElement('video'), 'src', {
  17997. get: noop,
  17998. set: noop
  17999. });
  18000. Object.defineProperty(document.createElement('audio'), 'src', {
  18001. get: noop,
  18002. set: noop
  18003. });
  18004. Object.defineProperty(document.createElement('video'), 'innerHTML', {
  18005. get: noop,
  18006. set: noop
  18007. });
  18008. Object.defineProperty(document.createElement('audio'), 'innerHTML', {
  18009. get: noop,
  18010. set: noop
  18011. });
  18012. } catch (e) {
  18013. return false;
  18014. }
  18015. return true;
  18016. };
  18017. /**
  18018. * Check to see if native `TextTrack`s are supported by this browser/device.
  18019. *
  18020. * @return {boolean}
  18021. * - True if native `TextTrack`s are supported.
  18022. * - False otherwise
  18023. */
  18024. Html5.supportsNativeTextTracks = function () {
  18025. return IS_ANY_SAFARI || IS_IOS && IS_CHROME;
  18026. };
  18027. /**
  18028. * Check to see if native `VideoTrack`s are supported by this browser/device
  18029. *
  18030. * @return {boolean}
  18031. * - True if native `VideoTrack`s are supported.
  18032. * - False otherwise
  18033. */
  18034. Html5.supportsNativeVideoTracks = function () {
  18035. return !!(Html5.TEST_VID && Html5.TEST_VID.videoTracks);
  18036. };
  18037. /**
  18038. * Check to see if native `AudioTrack`s are supported by this browser/device
  18039. *
  18040. * @return {boolean}
  18041. * - True if native `AudioTrack`s are supported.
  18042. * - False otherwise
  18043. */
  18044. Html5.supportsNativeAudioTracks = function () {
  18045. return !!(Html5.TEST_VID && Html5.TEST_VID.audioTracks);
  18046. };
  18047. /**
  18048. * An array of events available on the Html5 tech.
  18049. *
  18050. * @private
  18051. * @type {Array}
  18052. */
  18053. 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'];
  18054. /**
  18055. * Boolean indicating whether the `Tech` supports volume control.
  18056. *
  18057. * @type {boolean}
  18058. * @default {@link Html5.canControlVolume}
  18059. */
  18060. /**
  18061. * Boolean indicating whether the `Tech` supports muting volume.
  18062. *
  18063. * @type {boolean}
  18064. * @default {@link Html5.canMuteVolume}
  18065. */
  18066. /**
  18067. * Boolean indicating whether the `Tech` supports changing the speed at which the media
  18068. * plays. Examples:
  18069. * - Set player to play 2x (twice) as fast
  18070. * - Set player to play 0.5x (half) as fast
  18071. *
  18072. * @type {boolean}
  18073. * @default {@link Html5.canControlPlaybackRate}
  18074. */
  18075. /**
  18076. * Boolean indicating whether the `Tech` supports the `sourceset` event.
  18077. *
  18078. * @type {boolean}
  18079. * @default
  18080. */
  18081. /**
  18082. * Boolean indicating whether the `HTML5` tech currently supports native `TextTrack`s.
  18083. *
  18084. * @type {boolean}
  18085. * @default {@link Html5.supportsNativeTextTracks}
  18086. */
  18087. /**
  18088. * Boolean indicating whether the `HTML5` tech currently supports native `VideoTrack`s.
  18089. *
  18090. * @type {boolean}
  18091. * @default {@link Html5.supportsNativeVideoTracks}
  18092. */
  18093. /**
  18094. * Boolean indicating whether the `HTML5` tech currently supports native `AudioTrack`s.
  18095. *
  18096. * @type {boolean}
  18097. * @default {@link Html5.supportsNativeAudioTracks}
  18098. */
  18099. [['featuresMuteControl', 'canMuteVolume'], ['featuresPlaybackRate', 'canControlPlaybackRate'], ['featuresSourceset', 'canOverrideAttributes'], ['featuresNativeTextTracks', 'supportsNativeTextTracks'], ['featuresNativeVideoTracks', 'supportsNativeVideoTracks'], ['featuresNativeAudioTracks', 'supportsNativeAudioTracks']].forEach(function ([key, fn]) {
  18100. defineLazyProperty(Html5.prototype, key, () => Html5[fn](), true);
  18101. });
  18102. Html5.prototype.featuresVolumeControl = Html5.canControlVolume();
  18103. /**
  18104. * Boolean indicating whether the `HTML5` tech currently supports the media element
  18105. * moving in the DOM. iOS breaks if you move the media element, so this is set this to
  18106. * false there. Everywhere else this should be true.
  18107. *
  18108. * @type {boolean}
  18109. * @default
  18110. */
  18111. Html5.prototype.movingMediaElementInDOM = !IS_IOS;
  18112. // TODO: Previous comment: No longer appears to be used. Can probably be removed.
  18113. // Is this true?
  18114. /**
  18115. * Boolean indicating whether the `HTML5` tech currently supports automatic media resize
  18116. * when going into fullscreen.
  18117. *
  18118. * @type {boolean}
  18119. * @default
  18120. */
  18121. Html5.prototype.featuresFullscreenResize = true;
  18122. /**
  18123. * Boolean indicating whether the `HTML5` tech currently supports the progress event.
  18124. * If this is false, manual `progress` events will be triggered instead.
  18125. *
  18126. * @type {boolean}
  18127. * @default
  18128. */
  18129. Html5.prototype.featuresProgressEvents = true;
  18130. /**
  18131. * Boolean indicating whether the `HTML5` tech currently supports the timeupdate event.
  18132. * If this is false, manual `timeupdate` events will be triggered instead.
  18133. *
  18134. * @default
  18135. */
  18136. Html5.prototype.featuresTimeupdateEvents = true;
  18137. /**
  18138. * Whether the HTML5 el supports `requestVideoFrameCallback`
  18139. *
  18140. * @type {boolean}
  18141. */
  18142. Html5.prototype.featuresVideoFrameCallback = !!(Html5.TEST_VID && Html5.TEST_VID.requestVideoFrameCallback);
  18143. Html5.disposeMediaElement = function (el) {
  18144. if (!el) {
  18145. return;
  18146. }
  18147. if (el.parentNode) {
  18148. el.parentNode.removeChild(el);
  18149. }
  18150. // remove any child track or source nodes to prevent their loading
  18151. while (el.hasChildNodes()) {
  18152. el.removeChild(el.firstChild);
  18153. }
  18154. // remove any src reference. not setting `src=''` because that causes a warning
  18155. // in firefox
  18156. el.removeAttribute('src');
  18157. // force the media element to update its loading state by calling load()
  18158. // however IE on Windows 7N has a bug that throws an error so need a try/catch (#793)
  18159. if (typeof el.load === 'function') {
  18160. // wrapping in an iife so it's not deoptimized (#1060#discussion_r10324473)
  18161. (function () {
  18162. try {
  18163. el.load();
  18164. } catch (e) {
  18165. // not supported
  18166. }
  18167. })();
  18168. }
  18169. };
  18170. Html5.resetMediaElement = function (el) {
  18171. if (!el) {
  18172. return;
  18173. }
  18174. const sources = el.querySelectorAll('source');
  18175. let i = sources.length;
  18176. while (i--) {
  18177. el.removeChild(sources[i]);
  18178. }
  18179. // remove any src reference.
  18180. // not setting `src=''` because that throws an error
  18181. el.removeAttribute('src');
  18182. if (typeof el.load === 'function') {
  18183. // wrapping in an iife so it's not deoptimized (#1060#discussion_r10324473)
  18184. (function () {
  18185. try {
  18186. el.load();
  18187. } catch (e) {
  18188. // satisfy linter
  18189. }
  18190. })();
  18191. }
  18192. };
  18193. /* Native HTML5 element property wrapping ----------------------------------- */
  18194. // Wrap native boolean attributes with getters that check both property and attribute
  18195. // The list is as followed:
  18196. // muted, defaultMuted, autoplay, controls, loop, playsinline
  18197. [
  18198. /**
  18199. * Get the value of `muted` from the media element. `muted` indicates
  18200. * that the volume for the media should be set to silent. This does not actually change
  18201. * the `volume` attribute.
  18202. *
  18203. * @method Html5#muted
  18204. * @return {boolean}
  18205. * - True if the value of `volume` should be ignored and the audio set to silent.
  18206. * - False if the value of `volume` should be used.
  18207. *
  18208. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-muted}
  18209. */
  18210. 'muted',
  18211. /**
  18212. * Get the value of `defaultMuted` from the media element. `defaultMuted` indicates
  18213. * whether the media should start muted or not. Only changes the default state of the
  18214. * media. `muted` and `defaultMuted` can have different values. {@link Html5#muted} indicates the
  18215. * current state.
  18216. *
  18217. * @method Html5#defaultMuted
  18218. * @return {boolean}
  18219. * - The value of `defaultMuted` from the media element.
  18220. * - True indicates that the media should start muted.
  18221. * - False indicates that the media should not start muted
  18222. *
  18223. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultmuted}
  18224. */
  18225. 'defaultMuted',
  18226. /**
  18227. * Get the value of `autoplay` from the media element. `autoplay` indicates
  18228. * that the media should start to play as soon as the page is ready.
  18229. *
  18230. * @method Html5#autoplay
  18231. * @return {boolean}
  18232. * - The value of `autoplay` from the media element.
  18233. * - True indicates that the media should start as soon as the page loads.
  18234. * - False indicates that the media should not start as soon as the page loads.
  18235. *
  18236. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-autoplay}
  18237. */
  18238. 'autoplay',
  18239. /**
  18240. * Get the value of `controls` from the media element. `controls` indicates
  18241. * whether the native media controls should be shown or hidden.
  18242. *
  18243. * @method Html5#controls
  18244. * @return {boolean}
  18245. * - The value of `controls` from the media element.
  18246. * - True indicates that native controls should be showing.
  18247. * - False indicates that native controls should be hidden.
  18248. *
  18249. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-controls}
  18250. */
  18251. 'controls',
  18252. /**
  18253. * Get the value of `loop` from the media element. `loop` indicates
  18254. * that the media should return to the start of the media and continue playing once
  18255. * it reaches the end.
  18256. *
  18257. * @method Html5#loop
  18258. * @return {boolean}
  18259. * - The value of `loop` from the media element.
  18260. * - True indicates that playback should seek back to start once
  18261. * the end of a media is reached.
  18262. * - False indicates that playback should not loop back to the start when the
  18263. * end of the media is reached.
  18264. *
  18265. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-loop}
  18266. */
  18267. 'loop',
  18268. /**
  18269. * Get the value of `playsinline` from the media element. `playsinline` indicates
  18270. * to the browser that non-fullscreen playback is preferred when fullscreen
  18271. * playback is the native default, such as in iOS Safari.
  18272. *
  18273. * @method Html5#playsinline
  18274. * @return {boolean}
  18275. * - The value of `playsinline` from the media element.
  18276. * - True indicates that the media should play inline.
  18277. * - False indicates that the media should not play inline.
  18278. *
  18279. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  18280. */
  18281. 'playsinline'].forEach(function (prop) {
  18282. Html5.prototype[prop] = function () {
  18283. return this.el_[prop] || this.el_.hasAttribute(prop);
  18284. };
  18285. });
  18286. // Wrap native boolean attributes with setters that set both property and attribute
  18287. // The list is as followed:
  18288. // setMuted, setDefaultMuted, setAutoplay, setLoop, setPlaysinline
  18289. // setControls is special-cased above
  18290. [
  18291. /**
  18292. * Set the value of `muted` on the media element. `muted` indicates that the current
  18293. * audio level should be silent.
  18294. *
  18295. * @method Html5#setMuted
  18296. * @param {boolean} muted
  18297. * - True if the audio should be set to silent
  18298. * - False otherwise
  18299. *
  18300. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-muted}
  18301. */
  18302. 'muted',
  18303. /**
  18304. * Set the value of `defaultMuted` on the media element. `defaultMuted` indicates that the current
  18305. * audio level should be silent, but will only effect the muted level on initial playback..
  18306. *
  18307. * @method Html5.prototype.setDefaultMuted
  18308. * @param {boolean} defaultMuted
  18309. * - True if the audio should be set to silent
  18310. * - False otherwise
  18311. *
  18312. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultmuted}
  18313. */
  18314. 'defaultMuted',
  18315. /**
  18316. * Set the value of `autoplay` on the media element. `autoplay` indicates
  18317. * that the media should start to play as soon as the page is ready.
  18318. *
  18319. * @method Html5#setAutoplay
  18320. * @param {boolean} autoplay
  18321. * - True indicates that the media should start as soon as the page loads.
  18322. * - False indicates that the media should not start as soon as the page loads.
  18323. *
  18324. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-autoplay}
  18325. */
  18326. 'autoplay',
  18327. /**
  18328. * Set the value of `loop` on the media element. `loop` indicates
  18329. * that the media should return to the start of the media and continue playing once
  18330. * it reaches the end.
  18331. *
  18332. * @method Html5#setLoop
  18333. * @param {boolean} loop
  18334. * - True indicates that playback should seek back to start once
  18335. * the end of a media is reached.
  18336. * - False indicates that playback should not loop back to the start when the
  18337. * end of the media is reached.
  18338. *
  18339. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-loop}
  18340. */
  18341. 'loop',
  18342. /**
  18343. * Set the value of `playsinline` from the media element. `playsinline` indicates
  18344. * to the browser that non-fullscreen playback is preferred when fullscreen
  18345. * playback is the native default, such as in iOS Safari.
  18346. *
  18347. * @method Html5#setPlaysinline
  18348. * @param {boolean} playsinline
  18349. * - True indicates that the media should play inline.
  18350. * - False indicates that the media should not play inline.
  18351. *
  18352. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  18353. */
  18354. 'playsinline'].forEach(function (prop) {
  18355. Html5.prototype['set' + toTitleCase(prop)] = function (v) {
  18356. this.el_[prop] = v;
  18357. if (v) {
  18358. this.el_.setAttribute(prop, prop);
  18359. } else {
  18360. this.el_.removeAttribute(prop);
  18361. }
  18362. };
  18363. });
  18364. // Wrap native properties with a getter
  18365. // The list is as followed
  18366. // paused, currentTime, buffered, volume, poster, preload, error, seeking
  18367. // seekable, ended, playbackRate, defaultPlaybackRate, disablePictureInPicture
  18368. // played, networkState, readyState, videoWidth, videoHeight, crossOrigin
  18369. [
  18370. /**
  18371. * Get the value of `paused` from the media element. `paused` indicates whether the media element
  18372. * is currently paused or not.
  18373. *
  18374. * @method Html5#paused
  18375. * @return {boolean}
  18376. * The value of `paused` from the media element.
  18377. *
  18378. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-paused}
  18379. */
  18380. 'paused',
  18381. /**
  18382. * Get the value of `currentTime` from the media element. `currentTime` indicates
  18383. * the current second that the media is at in playback.
  18384. *
  18385. * @method Html5#currentTime
  18386. * @return {number}
  18387. * The value of `currentTime` from the media element.
  18388. *
  18389. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-currenttime}
  18390. */
  18391. 'currentTime',
  18392. /**
  18393. * Get the value of `buffered` from the media element. `buffered` is a `TimeRange`
  18394. * object that represents the parts of the media that are already downloaded and
  18395. * available for playback.
  18396. *
  18397. * @method Html5#buffered
  18398. * @return {TimeRange}
  18399. * The value of `buffered` from the media element.
  18400. *
  18401. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-buffered}
  18402. */
  18403. 'buffered',
  18404. /**
  18405. * Get the value of `volume` from the media element. `volume` indicates
  18406. * the current playback volume of audio for a media. `volume` will be a value from 0
  18407. * (silent) to 1 (loudest and default).
  18408. *
  18409. * @method Html5#volume
  18410. * @return {number}
  18411. * The value of `volume` from the media element. Value will be between 0-1.
  18412. *
  18413. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-a-volume}
  18414. */
  18415. 'volume',
  18416. /**
  18417. * Get the value of `poster` from the media element. `poster` indicates
  18418. * that the url of an image file that can/will be shown when no media data is available.
  18419. *
  18420. * @method Html5#poster
  18421. * @return {string}
  18422. * The value of `poster` from the media element. Value will be a url to an
  18423. * image.
  18424. *
  18425. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-video-poster}
  18426. */
  18427. 'poster',
  18428. /**
  18429. * Get the value of `preload` from the media element. `preload` indicates
  18430. * what should download before the media is interacted with. It can have the following
  18431. * values:
  18432. * - none: nothing should be downloaded
  18433. * - metadata: poster and the first few frames of the media may be downloaded to get
  18434. * media dimensions and other metadata
  18435. * - auto: allow the media and metadata for the media to be downloaded before
  18436. * interaction
  18437. *
  18438. * @method Html5#preload
  18439. * @return {string}
  18440. * The value of `preload` from the media element. Will be 'none', 'metadata',
  18441. * or 'auto'.
  18442. *
  18443. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-preload}
  18444. */
  18445. 'preload',
  18446. /**
  18447. * Get the value of the `error` from the media element. `error` indicates any
  18448. * MediaError that may have occurred during playback. If error returns null there is no
  18449. * current error.
  18450. *
  18451. * @method Html5#error
  18452. * @return {MediaError|null}
  18453. * The value of `error` from the media element. Will be `MediaError` if there
  18454. * is a current error and null otherwise.
  18455. *
  18456. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-error}
  18457. */
  18458. 'error',
  18459. /**
  18460. * Get the value of `seeking` from the media element. `seeking` indicates whether the
  18461. * media is currently seeking to a new position or not.
  18462. *
  18463. * @method Html5#seeking
  18464. * @return {boolean}
  18465. * - The value of `seeking` from the media element.
  18466. * - True indicates that the media is currently seeking to a new position.
  18467. * - False indicates that the media is not seeking to a new position at this time.
  18468. *
  18469. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-seeking}
  18470. */
  18471. 'seeking',
  18472. /**
  18473. * Get the value of `seekable` from the media element. `seekable` returns a
  18474. * `TimeRange` object indicating ranges of time that can currently be `seeked` to.
  18475. *
  18476. * @method Html5#seekable
  18477. * @return {TimeRange}
  18478. * The value of `seekable` from the media element. A `TimeRange` object
  18479. * indicating the current ranges of time that can be seeked to.
  18480. *
  18481. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-seekable}
  18482. */
  18483. 'seekable',
  18484. /**
  18485. * Get the value of `ended` from the media element. `ended` indicates whether
  18486. * the media has reached the end or not.
  18487. *
  18488. * @method Html5#ended
  18489. * @return {boolean}
  18490. * - The value of `ended` from the media element.
  18491. * - True indicates that the media has ended.
  18492. * - False indicates that the media has not ended.
  18493. *
  18494. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-ended}
  18495. */
  18496. 'ended',
  18497. /**
  18498. * Get the value of `playbackRate` from the media element. `playbackRate` indicates
  18499. * the rate at which the media is currently playing back. Examples:
  18500. * - if playbackRate is set to 2, media will play twice as fast.
  18501. * - if playbackRate is set to 0.5, media will play half as fast.
  18502. *
  18503. * @method Html5#playbackRate
  18504. * @return {number}
  18505. * The value of `playbackRate` from the media element. A number indicating
  18506. * the current playback speed of the media, where 1 is normal speed.
  18507. *
  18508. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18509. */
  18510. 'playbackRate',
  18511. /**
  18512. * Get the value of `defaultPlaybackRate` from the media element. `defaultPlaybackRate` indicates
  18513. * the rate at which the media is currently playing back. This value will not indicate the current
  18514. * `playbackRate` after playback has started, use {@link Html5#playbackRate} for that.
  18515. *
  18516. * Examples:
  18517. * - if defaultPlaybackRate is set to 2, media will play twice as fast.
  18518. * - if defaultPlaybackRate is set to 0.5, media will play half as fast.
  18519. *
  18520. * @method Html5.prototype.defaultPlaybackRate
  18521. * @return {number}
  18522. * The value of `defaultPlaybackRate` from the media element. A number indicating
  18523. * the current playback speed of the media, where 1 is normal speed.
  18524. *
  18525. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18526. */
  18527. 'defaultPlaybackRate',
  18528. /**
  18529. * Get the value of 'disablePictureInPicture' from the video element.
  18530. *
  18531. * @method Html5#disablePictureInPicture
  18532. * @return {boolean} value
  18533. * - The value of `disablePictureInPicture` from the video element.
  18534. * - True indicates that the video can't be played in Picture-In-Picture mode
  18535. * - False indicates that the video can be played in Picture-In-Picture mode
  18536. *
  18537. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/#disable-pip}
  18538. */
  18539. 'disablePictureInPicture',
  18540. /**
  18541. * Get the value of `played` from the media element. `played` returns a `TimeRange`
  18542. * object representing points in the media timeline that have been played.
  18543. *
  18544. * @method Html5#played
  18545. * @return {TimeRange}
  18546. * The value of `played` from the media element. A `TimeRange` object indicating
  18547. * the ranges of time that have been played.
  18548. *
  18549. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-played}
  18550. */
  18551. 'played',
  18552. /**
  18553. * Get the value of `networkState` from the media element. `networkState` indicates
  18554. * the current network state. It returns an enumeration from the following list:
  18555. * - 0: NETWORK_EMPTY
  18556. * - 1: NETWORK_IDLE
  18557. * - 2: NETWORK_LOADING
  18558. * - 3: NETWORK_NO_SOURCE
  18559. *
  18560. * @method Html5#networkState
  18561. * @return {number}
  18562. * The value of `networkState` from the media element. This will be a number
  18563. * from the list in the description.
  18564. *
  18565. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-networkstate}
  18566. */
  18567. 'networkState',
  18568. /**
  18569. * Get the value of `readyState` from the media element. `readyState` indicates
  18570. * the current state of the media element. It returns an enumeration from the
  18571. * following list:
  18572. * - 0: HAVE_NOTHING
  18573. * - 1: HAVE_METADATA
  18574. * - 2: HAVE_CURRENT_DATA
  18575. * - 3: HAVE_FUTURE_DATA
  18576. * - 4: HAVE_ENOUGH_DATA
  18577. *
  18578. * @method Html5#readyState
  18579. * @return {number}
  18580. * The value of `readyState` from the media element. This will be a number
  18581. * from the list in the description.
  18582. *
  18583. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#ready-states}
  18584. */
  18585. 'readyState',
  18586. /**
  18587. * Get the value of `videoWidth` from the video element. `videoWidth` indicates
  18588. * the current width of the video in css pixels.
  18589. *
  18590. * @method Html5#videoWidth
  18591. * @return {number}
  18592. * The value of `videoWidth` from the video element. This will be a number
  18593. * in css pixels.
  18594. *
  18595. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-video-videowidth}
  18596. */
  18597. 'videoWidth',
  18598. /**
  18599. * Get the value of `videoHeight` from the video element. `videoHeight` indicates
  18600. * the current height of the video in css pixels.
  18601. *
  18602. * @method Html5#videoHeight
  18603. * @return {number}
  18604. * The value of `videoHeight` from the video element. This will be a number
  18605. * in css pixels.
  18606. *
  18607. * @see [Spec] {@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-video-videowidth}
  18608. */
  18609. 'videoHeight',
  18610. /**
  18611. * Get the value of `crossOrigin` from the media element. `crossOrigin` indicates
  18612. * to the browser that should sent the cookies along with the requests for the
  18613. * different assets/playlists
  18614. *
  18615. * @method Html5#crossOrigin
  18616. * @return {string}
  18617. * - anonymous indicates that the media should not sent cookies.
  18618. * - use-credentials indicates that the media should sent cookies along the requests.
  18619. *
  18620. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-media-crossorigin}
  18621. */
  18622. 'crossOrigin'].forEach(function (prop) {
  18623. Html5.prototype[prop] = function () {
  18624. return this.el_[prop];
  18625. };
  18626. });
  18627. // Wrap native properties with a setter in this format:
  18628. // set + toTitleCase(name)
  18629. // The list is as follows:
  18630. // setVolume, setSrc, setPoster, setPreload, setPlaybackRate, setDefaultPlaybackRate,
  18631. // setDisablePictureInPicture, setCrossOrigin
  18632. [
  18633. /**
  18634. * Set the value of `volume` on the media element. `volume` indicates the current
  18635. * audio level as a percentage in decimal form. This means that 1 is 100%, 0.5 is 50%, and
  18636. * so on.
  18637. *
  18638. * @method Html5#setVolume
  18639. * @param {number} percentAsDecimal
  18640. * The volume percent as a decimal. Valid range is from 0-1.
  18641. *
  18642. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-a-volume}
  18643. */
  18644. 'volume',
  18645. /**
  18646. * Set the value of `src` on the media element. `src` indicates the current
  18647. * {@link Tech~SourceObject} for the media.
  18648. *
  18649. * @method Html5#setSrc
  18650. * @param {Tech~SourceObject} src
  18651. * The source object to set as the current source.
  18652. *
  18653. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-src}
  18654. */
  18655. 'src',
  18656. /**
  18657. * Set the value of `poster` on the media element. `poster` is the url to
  18658. * an image file that can/will be shown when no media data is available.
  18659. *
  18660. * @method Html5#setPoster
  18661. * @param {string} poster
  18662. * The url to an image that should be used as the `poster` for the media
  18663. * element.
  18664. *
  18665. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-poster}
  18666. */
  18667. 'poster',
  18668. /**
  18669. * Set the value of `preload` on the media element. `preload` indicates
  18670. * what should download before the media is interacted with. It can have the following
  18671. * values:
  18672. * - none: nothing should be downloaded
  18673. * - metadata: poster and the first few frames of the media may be downloaded to get
  18674. * media dimensions and other metadata
  18675. * - auto: allow the media and metadata for the media to be downloaded before
  18676. * interaction
  18677. *
  18678. * @method Html5#setPreload
  18679. * @param {string} preload
  18680. * The value of `preload` to set on the media element. Must be 'none', 'metadata',
  18681. * or 'auto'.
  18682. *
  18683. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#attr-media-preload}
  18684. */
  18685. 'preload',
  18686. /**
  18687. * Set the value of `playbackRate` on the media element. `playbackRate` indicates
  18688. * the rate at which the media should play back. Examples:
  18689. * - if playbackRate is set to 2, media will play twice as fast.
  18690. * - if playbackRate is set to 0.5, media will play half as fast.
  18691. *
  18692. * @method Html5#setPlaybackRate
  18693. * @return {number}
  18694. * The value of `playbackRate` from the media element. A number indicating
  18695. * the current playback speed of the media, where 1 is normal speed.
  18696. *
  18697. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-playbackrate}
  18698. */
  18699. 'playbackRate',
  18700. /**
  18701. * Set the value of `defaultPlaybackRate` on the media element. `defaultPlaybackRate` indicates
  18702. * the rate at which the media should play back upon initial startup. Changing this value
  18703. * after a video has started will do nothing. Instead you should used {@link Html5#setPlaybackRate}.
  18704. *
  18705. * Example Values:
  18706. * - if playbackRate is set to 2, media will play twice as fast.
  18707. * - if playbackRate is set to 0.5, media will play half as fast.
  18708. *
  18709. * @method Html5.prototype.setDefaultPlaybackRate
  18710. * @return {number}
  18711. * The value of `defaultPlaybackRate` from the media element. A number indicating
  18712. * the current playback speed of the media, where 1 is normal speed.
  18713. *
  18714. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-defaultplaybackrate}
  18715. */
  18716. 'defaultPlaybackRate',
  18717. /**
  18718. * Prevents the browser from suggesting a Picture-in-Picture context menu
  18719. * or to request Picture-in-Picture automatically in some cases.
  18720. *
  18721. * @method Html5#setDisablePictureInPicture
  18722. * @param {boolean} value
  18723. * The true value will disable Picture-in-Picture mode.
  18724. *
  18725. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/#disable-pip}
  18726. */
  18727. 'disablePictureInPicture',
  18728. /**
  18729. * Set the value of `crossOrigin` from the media element. `crossOrigin` indicates
  18730. * to the browser that should sent the cookies along with the requests for the
  18731. * different assets/playlists
  18732. *
  18733. * @method Html5#setCrossOrigin
  18734. * @param {string} crossOrigin
  18735. * - anonymous indicates that the media should not sent cookies.
  18736. * - use-credentials indicates that the media should sent cookies along the requests.
  18737. *
  18738. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-media-crossorigin}
  18739. */
  18740. 'crossOrigin'].forEach(function (prop) {
  18741. Html5.prototype['set' + toTitleCase(prop)] = function (v) {
  18742. this.el_[prop] = v;
  18743. };
  18744. });
  18745. // wrap native functions with a function
  18746. // The list is as follows:
  18747. // pause, load, play
  18748. [
  18749. /**
  18750. * A wrapper around the media elements `pause` function. This will call the `HTML5`
  18751. * media elements `pause` function.
  18752. *
  18753. * @method Html5#pause
  18754. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-pause}
  18755. */
  18756. 'pause',
  18757. /**
  18758. * A wrapper around the media elements `load` function. This will call the `HTML5`s
  18759. * media element `load` function.
  18760. *
  18761. * @method Html5#load
  18762. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-load}
  18763. */
  18764. 'load',
  18765. /**
  18766. * A wrapper around the media elements `play` function. This will call the `HTML5`s
  18767. * media element `play` function.
  18768. *
  18769. * @method Html5#play
  18770. * @see [Spec]{@link https://www.w3.org/TR/html5/embedded-content-0.html#dom-media-play}
  18771. */
  18772. 'play'].forEach(function (prop) {
  18773. Html5.prototype[prop] = function () {
  18774. return this.el_[prop]();
  18775. };
  18776. });
  18777. Tech.withSourceHandlers(Html5);
  18778. /**
  18779. * Native source handler for Html5, simply passes the source to the media element.
  18780. *
  18781. * @property {Tech~SourceObject} source
  18782. * The source object
  18783. *
  18784. * @property {Html5} tech
  18785. * The instance of the HTML5 tech.
  18786. */
  18787. Html5.nativeSourceHandler = {};
  18788. /**
  18789. * Check if the media element can play the given mime type.
  18790. *
  18791. * @param {string} type
  18792. * The mimetype to check
  18793. *
  18794. * @return {string}
  18795. * 'probably', 'maybe', or '' (empty string)
  18796. */
  18797. Html5.nativeSourceHandler.canPlayType = function (type) {
  18798. // IE without MediaPlayer throws an error (#519)
  18799. try {
  18800. return Html5.TEST_VID.canPlayType(type);
  18801. } catch (e) {
  18802. return '';
  18803. }
  18804. };
  18805. /**
  18806. * Check if the media element can handle a source natively.
  18807. *
  18808. * @param {Tech~SourceObject} source
  18809. * The source object
  18810. *
  18811. * @param {Object} [options]
  18812. * Options to be passed to the tech.
  18813. *
  18814. * @return {string}
  18815. * 'probably', 'maybe', or '' (empty string).
  18816. */
  18817. Html5.nativeSourceHandler.canHandleSource = function (source, options) {
  18818. // If a type was provided we should rely on that
  18819. if (source.type) {
  18820. return Html5.nativeSourceHandler.canPlayType(source.type);
  18821. // If no type, fall back to checking 'video/[EXTENSION]'
  18822. } else if (source.src) {
  18823. const ext = getFileExtension(source.src);
  18824. return Html5.nativeSourceHandler.canPlayType(`video/${ext}`);
  18825. }
  18826. return '';
  18827. };
  18828. /**
  18829. * Pass the source to the native media element.
  18830. *
  18831. * @param {Tech~SourceObject} source
  18832. * The source object
  18833. *
  18834. * @param {Html5} tech
  18835. * The instance of the Html5 tech
  18836. *
  18837. * @param {Object} [options]
  18838. * The options to pass to the source
  18839. */
  18840. Html5.nativeSourceHandler.handleSource = function (source, tech, options) {
  18841. tech.setSrc(source.src);
  18842. };
  18843. /**
  18844. * A noop for the native dispose function, as cleanup is not needed.
  18845. */
  18846. Html5.nativeSourceHandler.dispose = function () {};
  18847. // Register the native source handler
  18848. Html5.registerSourceHandler(Html5.nativeSourceHandler);
  18849. Tech.registerTech('Html5', Html5);
  18850. /**
  18851. * @file player.js
  18852. */
  18853. // The following tech events are simply re-triggered
  18854. // on the player when they happen
  18855. const TECH_EVENTS_RETRIGGER = [
  18856. /**
  18857. * Fired while the user agent is downloading media data.
  18858. *
  18859. * @event Player#progress
  18860. * @type {Event}
  18861. */
  18862. /**
  18863. * Retrigger the `progress` event that was triggered by the {@link Tech}.
  18864. *
  18865. * @private
  18866. * @method Player#handleTechProgress_
  18867. * @fires Player#progress
  18868. * @listens Tech#progress
  18869. */
  18870. 'progress',
  18871. /**
  18872. * Fires when the loading of an audio/video is aborted.
  18873. *
  18874. * @event Player#abort
  18875. * @type {Event}
  18876. */
  18877. /**
  18878. * Retrigger the `abort` event that was triggered by the {@link Tech}.
  18879. *
  18880. * @private
  18881. * @method Player#handleTechAbort_
  18882. * @fires Player#abort
  18883. * @listens Tech#abort
  18884. */
  18885. 'abort',
  18886. /**
  18887. * Fires when the browser is intentionally not getting media data.
  18888. *
  18889. * @event Player#suspend
  18890. * @type {Event}
  18891. */
  18892. /**
  18893. * Retrigger the `suspend` event that was triggered by the {@link Tech}.
  18894. *
  18895. * @private
  18896. * @method Player#handleTechSuspend_
  18897. * @fires Player#suspend
  18898. * @listens Tech#suspend
  18899. */
  18900. 'suspend',
  18901. /**
  18902. * Fires when the current playlist is empty.
  18903. *
  18904. * @event Player#emptied
  18905. * @type {Event}
  18906. */
  18907. /**
  18908. * Retrigger the `emptied` event that was triggered by the {@link Tech}.
  18909. *
  18910. * @private
  18911. * @method Player#handleTechEmptied_
  18912. * @fires Player#emptied
  18913. * @listens Tech#emptied
  18914. */
  18915. 'emptied',
  18916. /**
  18917. * Fires when the browser is trying to get media data, but data is not available.
  18918. *
  18919. * @event Player#stalled
  18920. * @type {Event}
  18921. */
  18922. /**
  18923. * Retrigger the `stalled` event that was triggered by the {@link Tech}.
  18924. *
  18925. * @private
  18926. * @method Player#handleTechStalled_
  18927. * @fires Player#stalled
  18928. * @listens Tech#stalled
  18929. */
  18930. 'stalled',
  18931. /**
  18932. * Fires when the browser has loaded meta data for the audio/video.
  18933. *
  18934. * @event Player#loadedmetadata
  18935. * @type {Event}
  18936. */
  18937. /**
  18938. * Retrigger the `loadedmetadata` event that was triggered by the {@link Tech}.
  18939. *
  18940. * @private
  18941. * @method Player#handleTechLoadedmetadata_
  18942. * @fires Player#loadedmetadata
  18943. * @listens Tech#loadedmetadata
  18944. */
  18945. 'loadedmetadata',
  18946. /**
  18947. * Fires when the browser has loaded the current frame of the audio/video.
  18948. *
  18949. * @event Player#loadeddata
  18950. * @type {event}
  18951. */
  18952. /**
  18953. * Retrigger the `loadeddata` event that was triggered by the {@link Tech}.
  18954. *
  18955. * @private
  18956. * @method Player#handleTechLoaddeddata_
  18957. * @fires Player#loadeddata
  18958. * @listens Tech#loadeddata
  18959. */
  18960. 'loadeddata',
  18961. /**
  18962. * Fires when the current playback position has changed.
  18963. *
  18964. * @event Player#timeupdate
  18965. * @type {event}
  18966. */
  18967. /**
  18968. * Retrigger the `timeupdate` event that was triggered by the {@link Tech}.
  18969. *
  18970. * @private
  18971. * @method Player#handleTechTimeUpdate_
  18972. * @fires Player#timeupdate
  18973. * @listens Tech#timeupdate
  18974. */
  18975. 'timeupdate',
  18976. /**
  18977. * Fires when the video's intrinsic dimensions change
  18978. *
  18979. * @event Player#resize
  18980. * @type {event}
  18981. */
  18982. /**
  18983. * Retrigger the `resize` event that was triggered by the {@link Tech}.
  18984. *
  18985. * @private
  18986. * @method Player#handleTechResize_
  18987. * @fires Player#resize
  18988. * @listens Tech#resize
  18989. */
  18990. 'resize',
  18991. /**
  18992. * Fires when the volume has been changed
  18993. *
  18994. * @event Player#volumechange
  18995. * @type {event}
  18996. */
  18997. /**
  18998. * Retrigger the `volumechange` event that was triggered by the {@link Tech}.
  18999. *
  19000. * @private
  19001. * @method Player#handleTechVolumechange_
  19002. * @fires Player#volumechange
  19003. * @listens Tech#volumechange
  19004. */
  19005. 'volumechange',
  19006. /**
  19007. * Fires when the text track has been changed
  19008. *
  19009. * @event Player#texttrackchange
  19010. * @type {event}
  19011. */
  19012. /**
  19013. * Retrigger the `texttrackchange` event that was triggered by the {@link Tech}.
  19014. *
  19015. * @private
  19016. * @method Player#handleTechTexttrackchange_
  19017. * @fires Player#texttrackchange
  19018. * @listens Tech#texttrackchange
  19019. */
  19020. 'texttrackchange'];
  19021. // events to queue when playback rate is zero
  19022. // this is a hash for the sole purpose of mapping non-camel-cased event names
  19023. // to camel-cased function names
  19024. const TECH_EVENTS_QUEUE = {
  19025. canplay: 'CanPlay',
  19026. canplaythrough: 'CanPlayThrough',
  19027. playing: 'Playing',
  19028. seeked: 'Seeked'
  19029. };
  19030. const BREAKPOINT_ORDER = ['tiny', 'xsmall', 'small', 'medium', 'large', 'xlarge', 'huge'];
  19031. const BREAKPOINT_CLASSES = {};
  19032. // grep: vjs-layout-tiny
  19033. // grep: vjs-layout-x-small
  19034. // grep: vjs-layout-small
  19035. // grep: vjs-layout-medium
  19036. // grep: vjs-layout-large
  19037. // grep: vjs-layout-x-large
  19038. // grep: vjs-layout-huge
  19039. BREAKPOINT_ORDER.forEach(k => {
  19040. const v = k.charAt(0) === 'x' ? `x-${k.substring(1)}` : k;
  19041. BREAKPOINT_CLASSES[k] = `vjs-layout-${v}`;
  19042. });
  19043. const DEFAULT_BREAKPOINTS = {
  19044. tiny: 210,
  19045. xsmall: 320,
  19046. small: 425,
  19047. medium: 768,
  19048. large: 1440,
  19049. xlarge: 2560,
  19050. huge: Infinity
  19051. };
  19052. /**
  19053. * An instance of the `Player` class is created when any of the Video.js setup methods
  19054. * are used to initialize a video.
  19055. *
  19056. * After an instance has been created it can be accessed globally in three ways:
  19057. * 1. By calling `videojs.getPlayer('example_video_1');`
  19058. * 2. By calling `videojs('example_video_1');` (not recomended)
  19059. * 2. By using it directly via `videojs.players.example_video_1;`
  19060. *
  19061. * @extends Component
  19062. * @global
  19063. */
  19064. class Player extends Component {
  19065. /**
  19066. * Create an instance of this class.
  19067. *
  19068. * @param {Element} tag
  19069. * The original video DOM element used for configuring options.
  19070. *
  19071. * @param {Object} [options]
  19072. * Object of option names and values.
  19073. *
  19074. * @param {Function} [ready]
  19075. * Ready callback function.
  19076. */
  19077. constructor(tag, options, ready) {
  19078. // Make sure tag ID exists
  19079. tag.id = tag.id || options.id || `vjs_video_${newGUID()}`;
  19080. // Set Options
  19081. // The options argument overrides options set in the video tag
  19082. // which overrides globally set options.
  19083. // This latter part coincides with the load order
  19084. // (tag must exist before Player)
  19085. options = Object.assign(Player.getTagSettings(tag), options);
  19086. // Delay the initialization of children because we need to set up
  19087. // player properties first, and can't use `this` before `super()`
  19088. options.initChildren = false;
  19089. // Same with creating the element
  19090. options.createEl = false;
  19091. // don't auto mixin the evented mixin
  19092. options.evented = false;
  19093. // we don't want the player to report touch activity on itself
  19094. // see enableTouchActivity in Component
  19095. options.reportTouchActivity = false;
  19096. // If language is not set, get the closest lang attribute
  19097. if (!options.language) {
  19098. const closest = tag.closest('[lang]');
  19099. if (closest) {
  19100. options.language = closest.getAttribute('lang');
  19101. }
  19102. }
  19103. // Run base component initializing with new options
  19104. super(null, options, ready);
  19105. // Create bound methods for document listeners.
  19106. this.boundDocumentFullscreenChange_ = e => this.documentFullscreenChange_(e);
  19107. this.boundFullWindowOnEscKey_ = e => this.fullWindowOnEscKey(e);
  19108. this.boundUpdateStyleEl_ = e => this.updateStyleEl_(e);
  19109. this.boundApplyInitTime_ = e => this.applyInitTime_(e);
  19110. this.boundUpdateCurrentBreakpoint_ = e => this.updateCurrentBreakpoint_(e);
  19111. this.boundHandleTechClick_ = e => this.handleTechClick_(e);
  19112. this.boundHandleTechDoubleClick_ = e => this.handleTechDoubleClick_(e);
  19113. this.boundHandleTechTouchStart_ = e => this.handleTechTouchStart_(e);
  19114. this.boundHandleTechTouchMove_ = e => this.handleTechTouchMove_(e);
  19115. this.boundHandleTechTouchEnd_ = e => this.handleTechTouchEnd_(e);
  19116. this.boundHandleTechTap_ = e => this.handleTechTap_(e);
  19117. // default isFullscreen_ to false
  19118. this.isFullscreen_ = false;
  19119. // create logger
  19120. this.log = createLogger(this.id_);
  19121. // Hold our own reference to fullscreen api so it can be mocked in tests
  19122. this.fsApi_ = FullscreenApi;
  19123. // Tracks when a tech changes the poster
  19124. this.isPosterFromTech_ = false;
  19125. // Holds callback info that gets queued when playback rate is zero
  19126. // and a seek is happening
  19127. this.queuedCallbacks_ = [];
  19128. // Turn off API access because we're loading a new tech that might load asynchronously
  19129. this.isReady_ = false;
  19130. // Init state hasStarted_
  19131. this.hasStarted_ = false;
  19132. // Init state userActive_
  19133. this.userActive_ = false;
  19134. // Init debugEnabled_
  19135. this.debugEnabled_ = false;
  19136. // Init state audioOnlyMode_
  19137. this.audioOnlyMode_ = false;
  19138. // Init state audioPosterMode_
  19139. this.audioPosterMode_ = false;
  19140. // Init state audioOnlyCache_
  19141. this.audioOnlyCache_ = {
  19142. playerHeight: null,
  19143. hiddenChildren: []
  19144. };
  19145. // if the global option object was accidentally blown away by
  19146. // someone, bail early with an informative error
  19147. if (!this.options_ || !this.options_.techOrder || !this.options_.techOrder.length) {
  19148. throw new Error('No techOrder specified. Did you overwrite ' + 'videojs.options instead of just changing the ' + 'properties you want to override?');
  19149. }
  19150. // Store the original tag used to set options
  19151. this.tag = tag;
  19152. // Store the tag attributes used to restore html5 element
  19153. this.tagAttributes = tag && getAttributes(tag);
  19154. // Update current language
  19155. this.language(this.options_.language);
  19156. // Update Supported Languages
  19157. if (options.languages) {
  19158. // Normalise player option languages to lowercase
  19159. const languagesToLower = {};
  19160. Object.getOwnPropertyNames(options.languages).forEach(function (name) {
  19161. languagesToLower[name.toLowerCase()] = options.languages[name];
  19162. });
  19163. this.languages_ = languagesToLower;
  19164. } else {
  19165. this.languages_ = Player.prototype.options_.languages;
  19166. }
  19167. this.resetCache_();
  19168. // Set poster
  19169. this.poster_ = options.poster || '';
  19170. // Set controls
  19171. this.controls_ = !!options.controls;
  19172. // Original tag settings stored in options
  19173. // now remove immediately so native controls don't flash.
  19174. // May be turned back on by HTML5 tech if nativeControlsForTouch is true
  19175. tag.controls = false;
  19176. tag.removeAttribute('controls');
  19177. this.changingSrc_ = false;
  19178. this.playCallbacks_ = [];
  19179. this.playTerminatedQueue_ = [];
  19180. // the attribute overrides the option
  19181. if (tag.hasAttribute('autoplay')) {
  19182. this.autoplay(true);
  19183. } else {
  19184. // otherwise use the setter to validate and
  19185. // set the correct value.
  19186. this.autoplay(this.options_.autoplay);
  19187. }
  19188. // check plugins
  19189. if (options.plugins) {
  19190. Object.keys(options.plugins).forEach(name => {
  19191. if (typeof this[name] !== 'function') {
  19192. throw new Error(`plugin "${name}" does not exist`);
  19193. }
  19194. });
  19195. }
  19196. /*
  19197. * Store the internal state of scrubbing
  19198. *
  19199. * @private
  19200. * @return {Boolean} True if the user is scrubbing
  19201. */
  19202. this.scrubbing_ = false;
  19203. this.el_ = this.createEl();
  19204. // Make this an evented object and use `el_` as its event bus.
  19205. evented(this, {
  19206. eventBusKey: 'el_'
  19207. });
  19208. // listen to document and player fullscreenchange handlers so we receive those events
  19209. // before a user can receive them so we can update isFullscreen appropriately.
  19210. // make sure that we listen to fullscreenchange events before everything else to make sure that
  19211. // our isFullscreen method is updated properly for internal components as well as external.
  19212. if (this.fsApi_.requestFullscreen) {
  19213. on(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  19214. this.on(this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  19215. }
  19216. if (this.fluid_) {
  19217. this.on(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19218. }
  19219. // We also want to pass the original player options to each component and plugin
  19220. // as well so they don't need to reach back into the player for options later.
  19221. // We also need to do another copy of this.options_ so we don't end up with
  19222. // an infinite loop.
  19223. const playerOptionsCopy = merge(this.options_);
  19224. // Load plugins
  19225. if (options.plugins) {
  19226. Object.keys(options.plugins).forEach(name => {
  19227. this[name](options.plugins[name]);
  19228. });
  19229. }
  19230. // Enable debug mode to fire debugon event for all plugins.
  19231. if (options.debug) {
  19232. this.debug(true);
  19233. }
  19234. this.options_.playerOptions = playerOptionsCopy;
  19235. this.middleware_ = [];
  19236. this.playbackRates(options.playbackRates);
  19237. this.initChildren();
  19238. // Set isAudio based on whether or not an audio tag was used
  19239. this.isAudio(tag.nodeName.toLowerCase() === 'audio');
  19240. // Update controls className. Can't do this when the controls are initially
  19241. // set because the element doesn't exist yet.
  19242. if (this.controls()) {
  19243. this.addClass('vjs-controls-enabled');
  19244. } else {
  19245. this.addClass('vjs-controls-disabled');
  19246. }
  19247. // Set ARIA label and region role depending on player type
  19248. this.el_.setAttribute('role', 'region');
  19249. if (this.isAudio()) {
  19250. this.el_.setAttribute('aria-label', this.localize('Audio Player'));
  19251. } else {
  19252. this.el_.setAttribute('aria-label', this.localize('Video Player'));
  19253. }
  19254. if (this.isAudio()) {
  19255. this.addClass('vjs-audio');
  19256. }
  19257. // TODO: Make this smarter. Toggle user state between touching/mousing
  19258. // using events, since devices can have both touch and mouse events.
  19259. // TODO: Make this check be performed again when the window switches between monitors
  19260. // (See https://github.com/videojs/video.js/issues/5683)
  19261. if (TOUCH_ENABLED) {
  19262. this.addClass('vjs-touch-enabled');
  19263. }
  19264. // iOS Safari has broken hover handling
  19265. if (!IS_IOS) {
  19266. this.addClass('vjs-workinghover');
  19267. }
  19268. // Make player easily findable by ID
  19269. Player.players[this.id_] = this;
  19270. // Add a major version class to aid css in plugins
  19271. const majorVersion = version.split('.')[0];
  19272. this.addClass(`vjs-v${majorVersion}`);
  19273. // When the player is first initialized, trigger activity so components
  19274. // like the control bar show themselves if needed
  19275. this.userActive(true);
  19276. this.reportUserActivity();
  19277. this.one('play', e => this.listenForUserActivity_(e));
  19278. this.on('keydown', e => this.handleKeyDown(e));
  19279. this.on('languagechange', e => this.handleLanguagechange(e));
  19280. this.breakpoints(this.options_.breakpoints);
  19281. this.responsive(this.options_.responsive);
  19282. // Calling both the audio mode methods after the player is fully
  19283. // setup to be able to listen to the events triggered by them
  19284. this.on('ready', () => {
  19285. // Calling the audioPosterMode method first so that
  19286. // the audioOnlyMode can take precedence when both options are set to true
  19287. this.audioPosterMode(this.options_.audioPosterMode);
  19288. this.audioOnlyMode(this.options_.audioOnlyMode);
  19289. });
  19290. }
  19291. /**
  19292. * Destroys the video player and does any necessary cleanup.
  19293. *
  19294. * This is especially helpful if you are dynamically adding and removing videos
  19295. * to/from the DOM.
  19296. *
  19297. * @fires Player#dispose
  19298. */
  19299. dispose() {
  19300. /**
  19301. * Called when the player is being disposed of.
  19302. *
  19303. * @event Player#dispose
  19304. * @type {Event}
  19305. */
  19306. this.trigger('dispose');
  19307. // prevent dispose from being called twice
  19308. this.off('dispose');
  19309. // Make sure all player-specific document listeners are unbound. This is
  19310. off(document, this.fsApi_.fullscreenchange, this.boundDocumentFullscreenChange_);
  19311. off(document, 'keydown', this.boundFullWindowOnEscKey_);
  19312. if (this.styleEl_ && this.styleEl_.parentNode) {
  19313. this.styleEl_.parentNode.removeChild(this.styleEl_);
  19314. this.styleEl_ = null;
  19315. }
  19316. // Kill reference to this player
  19317. Player.players[this.id_] = null;
  19318. if (this.tag && this.tag.player) {
  19319. this.tag.player = null;
  19320. }
  19321. if (this.el_ && this.el_.player) {
  19322. this.el_.player = null;
  19323. }
  19324. if (this.tech_) {
  19325. this.tech_.dispose();
  19326. this.isPosterFromTech_ = false;
  19327. this.poster_ = '';
  19328. }
  19329. if (this.playerElIngest_) {
  19330. this.playerElIngest_ = null;
  19331. }
  19332. if (this.tag) {
  19333. this.tag = null;
  19334. }
  19335. clearCacheForPlayer(this);
  19336. // remove all event handlers for track lists
  19337. // all tracks and track listeners are removed on
  19338. // tech dispose
  19339. ALL.names.forEach(name => {
  19340. const props = ALL[name];
  19341. const list = this[props.getterName]();
  19342. // if it is not a native list
  19343. // we have to manually remove event listeners
  19344. if (list && list.off) {
  19345. list.off();
  19346. }
  19347. });
  19348. // the actual .el_ is removed here, or replaced if
  19349. super.dispose({
  19350. restoreEl: this.options_.restoreEl
  19351. });
  19352. }
  19353. /**
  19354. * Create the `Player`'s DOM element.
  19355. *
  19356. * @return {Element}
  19357. * The DOM element that gets created.
  19358. */
  19359. createEl() {
  19360. let tag = this.tag;
  19361. let el;
  19362. let playerElIngest = this.playerElIngest_ = tag.parentNode && tag.parentNode.hasAttribute && tag.parentNode.hasAttribute('data-vjs-player');
  19363. const divEmbed = this.tag.tagName.toLowerCase() === 'video-js';
  19364. if (playerElIngest) {
  19365. el = this.el_ = tag.parentNode;
  19366. } else if (!divEmbed) {
  19367. el = this.el_ = super.createEl('div');
  19368. }
  19369. // Copy over all the attributes from the tag, including ID and class
  19370. // ID will now reference player box, not the video tag
  19371. const attrs = getAttributes(tag);
  19372. if (divEmbed) {
  19373. el = this.el_ = tag;
  19374. tag = this.tag = document.createElement('video');
  19375. while (el.children.length) {
  19376. tag.appendChild(el.firstChild);
  19377. }
  19378. if (!hasClass(el, 'video-js')) {
  19379. addClass(el, 'video-js');
  19380. }
  19381. el.appendChild(tag);
  19382. playerElIngest = this.playerElIngest_ = el;
  19383. // move properties over from our custom `video-js` element
  19384. // to our new `video` element. This will move things like
  19385. // `src` or `controls` that were set via js before the player
  19386. // was initialized.
  19387. Object.keys(el).forEach(k => {
  19388. try {
  19389. tag[k] = el[k];
  19390. } catch (e) {
  19391. // we got a a property like outerHTML which we can't actually copy, ignore it
  19392. }
  19393. });
  19394. }
  19395. // set tabindex to -1 to remove the video element from the focus order
  19396. tag.setAttribute('tabindex', '-1');
  19397. attrs.tabindex = '-1';
  19398. // Workaround for #4583 on Chrome (on Windows) with JAWS.
  19399. // See https://github.com/FreedomScientific/VFO-standards-support/issues/78
  19400. // Note that we can't detect if JAWS is being used, but this ARIA attribute
  19401. // doesn't change behavior of Chrome if JAWS is not being used
  19402. if (IS_CHROME && IS_WINDOWS) {
  19403. tag.setAttribute('role', 'application');
  19404. attrs.role = 'application';
  19405. }
  19406. // Remove width/height attrs from tag so CSS can make it 100% width/height
  19407. tag.removeAttribute('width');
  19408. tag.removeAttribute('height');
  19409. if ('width' in attrs) {
  19410. delete attrs.width;
  19411. }
  19412. if ('height' in attrs) {
  19413. delete attrs.height;
  19414. }
  19415. Object.getOwnPropertyNames(attrs).forEach(function (attr) {
  19416. // don't copy over the class attribute to the player element when we're in a div embed
  19417. // the class is already set up properly in the divEmbed case
  19418. // and we want to make sure that the `video-js` class doesn't get lost
  19419. if (!(divEmbed && attr === 'class')) {
  19420. el.setAttribute(attr, attrs[attr]);
  19421. }
  19422. if (divEmbed) {
  19423. tag.setAttribute(attr, attrs[attr]);
  19424. }
  19425. });
  19426. // Update tag id/class for use as HTML5 playback tech
  19427. // Might think we should do this after embedding in container so .vjs-tech class
  19428. // doesn't flash 100% width/height, but class only applies with .video-js parent
  19429. tag.playerId = tag.id;
  19430. tag.id += '_html5_api';
  19431. tag.className = 'vjs-tech';
  19432. // Make player findable on elements
  19433. tag.player = el.player = this;
  19434. // Default state of video is paused
  19435. this.addClass('vjs-paused');
  19436. // Add a style element in the player that we'll use to set the width/height
  19437. // of the player in a way that's still overridable by CSS, just like the
  19438. // video element
  19439. if (window.VIDEOJS_NO_DYNAMIC_STYLE !== true) {
  19440. this.styleEl_ = createStyleElement('vjs-styles-dimensions');
  19441. const defaultsStyleEl = $('.vjs-styles-defaults');
  19442. const head = $('head');
  19443. head.insertBefore(this.styleEl_, defaultsStyleEl ? defaultsStyleEl.nextSibling : head.firstChild);
  19444. }
  19445. this.fill_ = false;
  19446. this.fluid_ = false;
  19447. // Pass in the width/height/aspectRatio options which will update the style el
  19448. this.width(this.options_.width);
  19449. this.height(this.options_.height);
  19450. this.fill(this.options_.fill);
  19451. this.fluid(this.options_.fluid);
  19452. this.aspectRatio(this.options_.aspectRatio);
  19453. // support both crossOrigin and crossorigin to reduce confusion and issues around the name
  19454. this.crossOrigin(this.options_.crossOrigin || this.options_.crossorigin);
  19455. // Hide any links within the video/audio tag,
  19456. // because IE doesn't hide them completely from screen readers.
  19457. const links = tag.getElementsByTagName('a');
  19458. for (let i = 0; i < links.length; i++) {
  19459. const linkEl = links.item(i);
  19460. addClass(linkEl, 'vjs-hidden');
  19461. linkEl.setAttribute('hidden', 'hidden');
  19462. }
  19463. // insertElFirst seems to cause the networkState to flicker from 3 to 2, so
  19464. // keep track of the original for later so we can know if the source originally failed
  19465. tag.initNetworkState_ = tag.networkState;
  19466. // Wrap video tag in div (el/box) container
  19467. if (tag.parentNode && !playerElIngest) {
  19468. tag.parentNode.insertBefore(el, tag);
  19469. }
  19470. // insert the tag as the first child of the player element
  19471. // then manually add it to the children array so that this.addChild
  19472. // will work properly for other components
  19473. //
  19474. // Breaks iPhone, fixed in HTML5 setup.
  19475. prependTo(tag, el);
  19476. this.children_.unshift(tag);
  19477. // Set lang attr on player to ensure CSS :lang() in consistent with player
  19478. // if it's been set to something different to the doc
  19479. this.el_.setAttribute('lang', this.language_);
  19480. this.el_.setAttribute('translate', 'no');
  19481. this.el_ = el;
  19482. return el;
  19483. }
  19484. /**
  19485. * Get or set the `Player`'s crossOrigin option. For the HTML5 player, this
  19486. * sets the `crossOrigin` property on the `<video>` tag to control the CORS
  19487. * behavior.
  19488. *
  19489. * @see [Video Element Attributes]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin}
  19490. *
  19491. * @param {string|null} [value]
  19492. * The value to set the `Player`'s crossOrigin to. If an argument is
  19493. * given, must be one of `'anonymous'` or `'use-credentials'`, or 'null'.
  19494. *
  19495. * @return {string|null|undefined}
  19496. * - The current crossOrigin value of the `Player` when getting.
  19497. * - undefined when setting
  19498. */
  19499. crossOrigin(value) {
  19500. // `null` can be set to unset a value
  19501. if (typeof value === 'undefined') {
  19502. return this.techGet_('crossOrigin');
  19503. }
  19504. if (value !== null && value !== 'anonymous' && value !== 'use-credentials') {
  19505. log.warn(`crossOrigin must be null, "anonymous" or "use-credentials", given "${value}"`);
  19506. return;
  19507. }
  19508. this.techCall_('setCrossOrigin', value);
  19509. if (this.posterImage) {
  19510. this.posterImage.crossOrigin(value);
  19511. }
  19512. return;
  19513. }
  19514. /**
  19515. * A getter/setter for the `Player`'s width. Returns the player's configured value.
  19516. * To get the current width use `currentWidth()`.
  19517. *
  19518. * @param {number} [value]
  19519. * The value to set the `Player`'s width to.
  19520. *
  19521. * @return {number}
  19522. * The current width of the `Player` when getting.
  19523. */
  19524. width(value) {
  19525. return this.dimension('width', value);
  19526. }
  19527. /**
  19528. * A getter/setter for the `Player`'s height. Returns the player's configured value.
  19529. * To get the current height use `currentheight()`.
  19530. *
  19531. * @param {number} [value]
  19532. * The value to set the `Player`'s height to.
  19533. *
  19534. * @return {number}
  19535. * The current height of the `Player` when getting.
  19536. */
  19537. height(value) {
  19538. return this.dimension('height', value);
  19539. }
  19540. /**
  19541. * A getter/setter for the `Player`'s width & height.
  19542. *
  19543. * @param {string} dimension
  19544. * This string can be:
  19545. * - 'width'
  19546. * - 'height'
  19547. *
  19548. * @param {number} [value]
  19549. * Value for dimension specified in the first argument.
  19550. *
  19551. * @return {number}
  19552. * The dimension arguments value when getting (width/height).
  19553. */
  19554. dimension(dimension, value) {
  19555. const privDimension = dimension + '_';
  19556. if (value === undefined) {
  19557. return this[privDimension] || 0;
  19558. }
  19559. if (value === '' || value === 'auto') {
  19560. // If an empty string is given, reset the dimension to be automatic
  19561. this[privDimension] = undefined;
  19562. this.updateStyleEl_();
  19563. return;
  19564. }
  19565. const parsedVal = parseFloat(value);
  19566. if (isNaN(parsedVal)) {
  19567. log.error(`Improper value "${value}" supplied for for ${dimension}`);
  19568. return;
  19569. }
  19570. this[privDimension] = parsedVal;
  19571. this.updateStyleEl_();
  19572. }
  19573. /**
  19574. * A getter/setter/toggler for the vjs-fluid `className` on the `Player`.
  19575. *
  19576. * Turning this on will turn off fill mode.
  19577. *
  19578. * @param {boolean} [bool]
  19579. * - A value of true adds the class.
  19580. * - A value of false removes the class.
  19581. * - No value will be a getter.
  19582. *
  19583. * @return {boolean|undefined}
  19584. * - The value of fluid when getting.
  19585. * - `undefined` when setting.
  19586. */
  19587. fluid(bool) {
  19588. if (bool === undefined) {
  19589. return !!this.fluid_;
  19590. }
  19591. this.fluid_ = !!bool;
  19592. if (isEvented(this)) {
  19593. this.off(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19594. }
  19595. if (bool) {
  19596. this.addClass('vjs-fluid');
  19597. this.fill(false);
  19598. addEventedCallback(this, () => {
  19599. this.on(['playerreset', 'resize'], this.boundUpdateStyleEl_);
  19600. });
  19601. } else {
  19602. this.removeClass('vjs-fluid');
  19603. }
  19604. this.updateStyleEl_();
  19605. }
  19606. /**
  19607. * A getter/setter/toggler for the vjs-fill `className` on the `Player`.
  19608. *
  19609. * Turning this on will turn off fluid mode.
  19610. *
  19611. * @param {boolean} [bool]
  19612. * - A value of true adds the class.
  19613. * - A value of false removes the class.
  19614. * - No value will be a getter.
  19615. *
  19616. * @return {boolean|undefined}
  19617. * - The value of fluid when getting.
  19618. * - `undefined` when setting.
  19619. */
  19620. fill(bool) {
  19621. if (bool === undefined) {
  19622. return !!this.fill_;
  19623. }
  19624. this.fill_ = !!bool;
  19625. if (bool) {
  19626. this.addClass('vjs-fill');
  19627. this.fluid(false);
  19628. } else {
  19629. this.removeClass('vjs-fill');
  19630. }
  19631. }
  19632. /**
  19633. * Get/Set the aspect ratio
  19634. *
  19635. * @param {string} [ratio]
  19636. * Aspect ratio for player
  19637. *
  19638. * @return {string|undefined}
  19639. * returns the current aspect ratio when getting
  19640. */
  19641. /**
  19642. * A getter/setter for the `Player`'s aspect ratio.
  19643. *
  19644. * @param {string} [ratio]
  19645. * The value to set the `Player`'s aspect ratio to.
  19646. *
  19647. * @return {string|undefined}
  19648. * - The current aspect ratio of the `Player` when getting.
  19649. * - undefined when setting
  19650. */
  19651. aspectRatio(ratio) {
  19652. if (ratio === undefined) {
  19653. return this.aspectRatio_;
  19654. }
  19655. // Check for width:height format
  19656. if (!/^\d+\:\d+$/.test(ratio)) {
  19657. throw new Error('Improper value supplied for aspect ratio. The format should be width:height, for example 16:9.');
  19658. }
  19659. this.aspectRatio_ = ratio;
  19660. // We're assuming if you set an aspect ratio you want fluid mode,
  19661. // because in fixed mode you could calculate width and height yourself.
  19662. this.fluid(true);
  19663. this.updateStyleEl_();
  19664. }
  19665. /**
  19666. * Update styles of the `Player` element (height, width and aspect ratio).
  19667. *
  19668. * @private
  19669. * @listens Tech#loadedmetadata
  19670. */
  19671. updateStyleEl_() {
  19672. if (window.VIDEOJS_NO_DYNAMIC_STYLE === true) {
  19673. const width = typeof this.width_ === 'number' ? this.width_ : this.options_.width;
  19674. const height = typeof this.height_ === 'number' ? this.height_ : this.options_.height;
  19675. const techEl = this.tech_ && this.tech_.el();
  19676. if (techEl) {
  19677. if (width >= 0) {
  19678. techEl.width = width;
  19679. }
  19680. if (height >= 0) {
  19681. techEl.height = height;
  19682. }
  19683. }
  19684. return;
  19685. }
  19686. let width;
  19687. let height;
  19688. let aspectRatio;
  19689. let idClass;
  19690. // The aspect ratio is either used directly or to calculate width and height.
  19691. if (this.aspectRatio_ !== undefined && this.aspectRatio_ !== 'auto') {
  19692. // Use any aspectRatio that's been specifically set
  19693. aspectRatio = this.aspectRatio_;
  19694. } else if (this.videoWidth() > 0) {
  19695. // Otherwise try to get the aspect ratio from the video metadata
  19696. aspectRatio = this.videoWidth() + ':' + this.videoHeight();
  19697. } else {
  19698. // Or use a default. The video element's is 2:1, but 16:9 is more common.
  19699. aspectRatio = '16:9';
  19700. }
  19701. // Get the ratio as a decimal we can use to calculate dimensions
  19702. const ratioParts = aspectRatio.split(':');
  19703. const ratioMultiplier = ratioParts[1] / ratioParts[0];
  19704. if (this.width_ !== undefined) {
  19705. // Use any width that's been specifically set
  19706. width = this.width_;
  19707. } else if (this.height_ !== undefined) {
  19708. // Or calculate the width from the aspect ratio if a height has been set
  19709. width = this.height_ / ratioMultiplier;
  19710. } else {
  19711. // Or use the video's metadata, or use the video el's default of 300
  19712. width = this.videoWidth() || 300;
  19713. }
  19714. if (this.height_ !== undefined) {
  19715. // Use any height that's been specifically set
  19716. height = this.height_;
  19717. } else {
  19718. // Otherwise calculate the height from the ratio and the width
  19719. height = width * ratioMultiplier;
  19720. }
  19721. // Ensure the CSS class is valid by starting with an alpha character
  19722. if (/^[^a-zA-Z]/.test(this.id())) {
  19723. idClass = 'dimensions-' + this.id();
  19724. } else {
  19725. idClass = this.id() + '-dimensions';
  19726. }
  19727. // Ensure the right class is still on the player for the style element
  19728. this.addClass(idClass);
  19729. setTextContent(this.styleEl_, `
  19730. .${idClass} {
  19731. width: ${width}px;
  19732. height: ${height}px;
  19733. }
  19734. .${idClass}.vjs-fluid:not(.vjs-audio-only-mode) {
  19735. padding-top: ${ratioMultiplier * 100}%;
  19736. }
  19737. `);
  19738. }
  19739. /**
  19740. * Load/Create an instance of playback {@link Tech} including element
  19741. * and API methods. Then append the `Tech` element in `Player` as a child.
  19742. *
  19743. * @param {string} techName
  19744. * name of the playback technology
  19745. *
  19746. * @param {string} source
  19747. * video source
  19748. *
  19749. * @private
  19750. */
  19751. loadTech_(techName, source) {
  19752. // Pause and remove current playback technology
  19753. if (this.tech_) {
  19754. this.unloadTech_();
  19755. }
  19756. const titleTechName = toTitleCase(techName);
  19757. const camelTechName = techName.charAt(0).toLowerCase() + techName.slice(1);
  19758. // get rid of the HTML5 video tag as soon as we are using another tech
  19759. if (titleTechName !== 'Html5' && this.tag) {
  19760. Tech.getTech('Html5').disposeMediaElement(this.tag);
  19761. this.tag.player = null;
  19762. this.tag = null;
  19763. }
  19764. this.techName_ = titleTechName;
  19765. // Turn off API access because we're loading a new tech that might load asynchronously
  19766. this.isReady_ = false;
  19767. let autoplay = this.autoplay();
  19768. // if autoplay is a string (or `true` with normalizeAutoplay: true) we pass false to the tech
  19769. // because the player is going to handle autoplay on `loadstart`
  19770. if (typeof this.autoplay() === 'string' || this.autoplay() === true && this.options_.normalizeAutoplay) {
  19771. autoplay = false;
  19772. }
  19773. // Grab tech-specific options from player options and add source and parent element to use.
  19774. const techOptions = {
  19775. source,
  19776. autoplay,
  19777. 'nativeControlsForTouch': this.options_.nativeControlsForTouch,
  19778. 'playerId': this.id(),
  19779. 'techId': `${this.id()}_${camelTechName}_api`,
  19780. 'playsinline': this.options_.playsinline,
  19781. 'preload': this.options_.preload,
  19782. 'loop': this.options_.loop,
  19783. 'disablePictureInPicture': this.options_.disablePictureInPicture,
  19784. 'muted': this.options_.muted,
  19785. 'poster': this.poster(),
  19786. 'language': this.language(),
  19787. 'playerElIngest': this.playerElIngest_ || false,
  19788. 'vtt.js': this.options_['vtt.js'],
  19789. 'canOverridePoster': !!this.options_.techCanOverridePoster,
  19790. 'enableSourceset': this.options_.enableSourceset
  19791. };
  19792. ALL.names.forEach(name => {
  19793. const props = ALL[name];
  19794. techOptions[props.getterName] = this[props.privateName];
  19795. });
  19796. Object.assign(techOptions, this.options_[titleTechName]);
  19797. Object.assign(techOptions, this.options_[camelTechName]);
  19798. Object.assign(techOptions, this.options_[techName.toLowerCase()]);
  19799. if (this.tag) {
  19800. techOptions.tag = this.tag;
  19801. }
  19802. if (source && source.src === this.cache_.src && this.cache_.currentTime > 0) {
  19803. techOptions.startTime = this.cache_.currentTime;
  19804. }
  19805. // Initialize tech instance
  19806. const TechClass = Tech.getTech(techName);
  19807. if (!TechClass) {
  19808. throw new Error(`No Tech named '${titleTechName}' exists! '${titleTechName}' should be registered using videojs.registerTech()'`);
  19809. }
  19810. this.tech_ = new TechClass(techOptions);
  19811. // player.triggerReady is always async, so don't need this to be async
  19812. this.tech_.ready(bind_(this, this.handleTechReady_), true);
  19813. textTrackConverter.jsonToTextTracks(this.textTracksJson_ || [], this.tech_);
  19814. // Listen to all HTML5-defined events and trigger them on the player
  19815. TECH_EVENTS_RETRIGGER.forEach(event => {
  19816. this.on(this.tech_, event, e => this[`handleTech${toTitleCase(event)}_`](e));
  19817. });
  19818. Object.keys(TECH_EVENTS_QUEUE).forEach(event => {
  19819. this.on(this.tech_, event, eventObj => {
  19820. if (this.tech_.playbackRate() === 0 && this.tech_.seeking()) {
  19821. this.queuedCallbacks_.push({
  19822. callback: this[`handleTech${TECH_EVENTS_QUEUE[event]}_`].bind(this),
  19823. event: eventObj
  19824. });
  19825. return;
  19826. }
  19827. this[`handleTech${TECH_EVENTS_QUEUE[event]}_`](eventObj);
  19828. });
  19829. });
  19830. this.on(this.tech_, 'loadstart', e => this.handleTechLoadStart_(e));
  19831. this.on(this.tech_, 'sourceset', e => this.handleTechSourceset_(e));
  19832. this.on(this.tech_, 'waiting', e => this.handleTechWaiting_(e));
  19833. this.on(this.tech_, 'ended', e => this.handleTechEnded_(e));
  19834. this.on(this.tech_, 'seeking', e => this.handleTechSeeking_(e));
  19835. this.on(this.tech_, 'play', e => this.handleTechPlay_(e));
  19836. this.on(this.tech_, 'pause', e => this.handleTechPause_(e));
  19837. this.on(this.tech_, 'durationchange', e => this.handleTechDurationChange_(e));
  19838. this.on(this.tech_, 'fullscreenchange', (e, data) => this.handleTechFullscreenChange_(e, data));
  19839. this.on(this.tech_, 'fullscreenerror', (e, err) => this.handleTechFullscreenError_(e, err));
  19840. this.on(this.tech_, 'enterpictureinpicture', e => this.handleTechEnterPictureInPicture_(e));
  19841. this.on(this.tech_, 'leavepictureinpicture', e => this.handleTechLeavePictureInPicture_(e));
  19842. this.on(this.tech_, 'error', e => this.handleTechError_(e));
  19843. this.on(this.tech_, 'posterchange', e => this.handleTechPosterChange_(e));
  19844. this.on(this.tech_, 'textdata', e => this.handleTechTextData_(e));
  19845. this.on(this.tech_, 'ratechange', e => this.handleTechRateChange_(e));
  19846. this.on(this.tech_, 'loadedmetadata', this.boundUpdateStyleEl_);
  19847. this.usingNativeControls(this.techGet_('controls'));
  19848. if (this.controls() && !this.usingNativeControls()) {
  19849. this.addTechControlsListeners_();
  19850. }
  19851. // Add the tech element in the DOM if it was not already there
  19852. // Make sure to not insert the original video element if using Html5
  19853. if (this.tech_.el().parentNode !== this.el() && (titleTechName !== 'Html5' || !this.tag)) {
  19854. prependTo(this.tech_.el(), this.el());
  19855. }
  19856. // Get rid of the original video tag reference after the first tech is loaded
  19857. if (this.tag) {
  19858. this.tag.player = null;
  19859. this.tag = null;
  19860. }
  19861. }
  19862. /**
  19863. * Unload and dispose of the current playback {@link Tech}.
  19864. *
  19865. * @private
  19866. */
  19867. unloadTech_() {
  19868. // Save the current text tracks so that we can reuse the same text tracks with the next tech
  19869. ALL.names.forEach(name => {
  19870. const props = ALL[name];
  19871. this[props.privateName] = this[props.getterName]();
  19872. });
  19873. this.textTracksJson_ = textTrackConverter.textTracksToJson(this.tech_);
  19874. this.isReady_ = false;
  19875. this.tech_.dispose();
  19876. this.tech_ = false;
  19877. if (this.isPosterFromTech_) {
  19878. this.poster_ = '';
  19879. this.trigger('posterchange');
  19880. }
  19881. this.isPosterFromTech_ = false;
  19882. }
  19883. /**
  19884. * Return a reference to the current {@link Tech}.
  19885. * It will print a warning by default about the danger of using the tech directly
  19886. * but any argument that is passed in will silence the warning.
  19887. *
  19888. * @param {*} [safety]
  19889. * Anything passed in to silence the warning
  19890. *
  19891. * @return {Tech}
  19892. * The Tech
  19893. */
  19894. tech(safety) {
  19895. if (safety === undefined) {
  19896. 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');
  19897. }
  19898. return this.tech_;
  19899. }
  19900. /**
  19901. * Set up click and touch listeners for the playback element
  19902. *
  19903. * - On desktops: a click on the video itself will toggle playback
  19904. * - On mobile devices: a click on the video toggles controls
  19905. * which is done by toggling the user state between active and
  19906. * inactive
  19907. * - A tap can signal that a user has become active or has become inactive
  19908. * e.g. a quick tap on an iPhone movie should reveal the controls. Another
  19909. * quick tap should hide them again (signaling the user is in an inactive
  19910. * viewing state)
  19911. * - In addition to this, we still want the user to be considered inactive after
  19912. * a few seconds of inactivity.
  19913. *
  19914. * > Note: the only part of iOS interaction we can't mimic with this setup
  19915. * is a touch and hold on the video element counting as activity in order to
  19916. * keep the controls showing, but that shouldn't be an issue. A touch and hold
  19917. * on any controls will still keep the user active
  19918. *
  19919. * @private
  19920. */
  19921. addTechControlsListeners_() {
  19922. // Make sure to remove all the previous listeners in case we are called multiple times.
  19923. this.removeTechControlsListeners_();
  19924. this.on(this.tech_, 'click', this.boundHandleTechClick_);
  19925. this.on(this.tech_, 'dblclick', this.boundHandleTechDoubleClick_);
  19926. // If the controls were hidden we don't want that to change without a tap event
  19927. // so we'll check if the controls were already showing before reporting user
  19928. // activity
  19929. this.on(this.tech_, 'touchstart', this.boundHandleTechTouchStart_);
  19930. this.on(this.tech_, 'touchmove', this.boundHandleTechTouchMove_);
  19931. this.on(this.tech_, 'touchend', this.boundHandleTechTouchEnd_);
  19932. // The tap listener needs to come after the touchend listener because the tap
  19933. // listener cancels out any reportedUserActivity when setting userActive(false)
  19934. this.on(this.tech_, 'tap', this.boundHandleTechTap_);
  19935. }
  19936. /**
  19937. * Remove the listeners used for click and tap controls. This is needed for
  19938. * toggling to controls disabled, where a tap/touch should do nothing.
  19939. *
  19940. * @private
  19941. */
  19942. removeTechControlsListeners_() {
  19943. // We don't want to just use `this.off()` because there might be other needed
  19944. // listeners added by techs that extend this.
  19945. this.off(this.tech_, 'tap', this.boundHandleTechTap_);
  19946. this.off(this.tech_, 'touchstart', this.boundHandleTechTouchStart_);
  19947. this.off(this.tech_, 'touchmove', this.boundHandleTechTouchMove_);
  19948. this.off(this.tech_, 'touchend', this.boundHandleTechTouchEnd_);
  19949. this.off(this.tech_, 'click', this.boundHandleTechClick_);
  19950. this.off(this.tech_, 'dblclick', this.boundHandleTechDoubleClick_);
  19951. }
  19952. /**
  19953. * Player waits for the tech to be ready
  19954. *
  19955. * @private
  19956. */
  19957. handleTechReady_() {
  19958. this.triggerReady();
  19959. // Keep the same volume as before
  19960. if (this.cache_.volume) {
  19961. this.techCall_('setVolume', this.cache_.volume);
  19962. }
  19963. // Look if the tech found a higher resolution poster while loading
  19964. this.handleTechPosterChange_();
  19965. // Update the duration if available
  19966. this.handleTechDurationChange_();
  19967. }
  19968. /**
  19969. * Retrigger the `loadstart` event that was triggered by the {@link Tech}.
  19970. *
  19971. * @fires Player#loadstart
  19972. * @listens Tech#loadstart
  19973. * @private
  19974. */
  19975. handleTechLoadStart_() {
  19976. // TODO: Update to use `emptied` event instead. See #1277.
  19977. this.removeClass('vjs-ended', 'vjs-seeking');
  19978. // reset the error state
  19979. this.error(null);
  19980. // Update the duration
  19981. this.handleTechDurationChange_();
  19982. if (!this.paused()) {
  19983. /**
  19984. * Fired when the user agent begins looking for media data
  19985. *
  19986. * @event Player#loadstart
  19987. * @type {Event}
  19988. */
  19989. this.trigger('loadstart');
  19990. } else {
  19991. // reset the hasStarted state
  19992. this.hasStarted(false);
  19993. this.trigger('loadstart');
  19994. }
  19995. // autoplay happens after loadstart for the browser,
  19996. // so we mimic that behavior
  19997. this.manualAutoplay_(this.autoplay() === true && this.options_.normalizeAutoplay ? 'play' : this.autoplay());
  19998. }
  19999. /**
  20000. * Handle autoplay string values, rather than the typical boolean
  20001. * values that should be handled by the tech. Note that this is not
  20002. * part of any specification. Valid values and what they do can be
  20003. * found on the autoplay getter at Player#autoplay()
  20004. */
  20005. manualAutoplay_(type) {
  20006. if (!this.tech_ || typeof type !== 'string') {
  20007. return;
  20008. }
  20009. // Save original muted() value, set muted to true, and attempt to play().
  20010. // On promise rejection, restore muted from saved value
  20011. const resolveMuted = () => {
  20012. const previouslyMuted = this.muted();
  20013. this.muted(true);
  20014. const restoreMuted = () => {
  20015. this.muted(previouslyMuted);
  20016. };
  20017. // restore muted on play terminatation
  20018. this.playTerminatedQueue_.push(restoreMuted);
  20019. const mutedPromise = this.play();
  20020. if (!isPromise(mutedPromise)) {
  20021. return;
  20022. }
  20023. return mutedPromise.catch(err => {
  20024. restoreMuted();
  20025. throw new Error(`Rejection at manualAutoplay. Restoring muted value. ${err ? err : ''}`);
  20026. });
  20027. };
  20028. let promise;
  20029. // if muted defaults to true
  20030. // the only thing we can do is call play
  20031. if (type === 'any' && !this.muted()) {
  20032. promise = this.play();
  20033. if (isPromise(promise)) {
  20034. promise = promise.catch(resolveMuted);
  20035. }
  20036. } else if (type === 'muted' && !this.muted()) {
  20037. promise = resolveMuted();
  20038. } else {
  20039. promise = this.play();
  20040. }
  20041. if (!isPromise(promise)) {
  20042. return;
  20043. }
  20044. return promise.then(() => {
  20045. this.trigger({
  20046. type: 'autoplay-success',
  20047. autoplay: type
  20048. });
  20049. }).catch(() => {
  20050. this.trigger({
  20051. type: 'autoplay-failure',
  20052. autoplay: type
  20053. });
  20054. });
  20055. }
  20056. /**
  20057. * Update the internal source caches so that we return the correct source from
  20058. * `src()`, `currentSource()`, and `currentSources()`.
  20059. *
  20060. * > Note: `currentSources` will not be updated if the source that is passed in exists
  20061. * in the current `currentSources` cache.
  20062. *
  20063. *
  20064. * @param {Tech~SourceObject} srcObj
  20065. * A string or object source to update our caches to.
  20066. */
  20067. updateSourceCaches_(srcObj = '') {
  20068. let src = srcObj;
  20069. let type = '';
  20070. if (typeof src !== 'string') {
  20071. src = srcObj.src;
  20072. type = srcObj.type;
  20073. }
  20074. // make sure all the caches are set to default values
  20075. // to prevent null checking
  20076. this.cache_.source = this.cache_.source || {};
  20077. this.cache_.sources = this.cache_.sources || [];
  20078. // try to get the type of the src that was passed in
  20079. if (src && !type) {
  20080. type = findMimetype(this, src);
  20081. }
  20082. // update `currentSource` cache always
  20083. this.cache_.source = merge({}, srcObj, {
  20084. src,
  20085. type
  20086. });
  20087. const matchingSources = this.cache_.sources.filter(s => s.src && s.src === src);
  20088. const sourceElSources = [];
  20089. const sourceEls = this.$$('source');
  20090. const matchingSourceEls = [];
  20091. for (let i = 0; i < sourceEls.length; i++) {
  20092. const sourceObj = getAttributes(sourceEls[i]);
  20093. sourceElSources.push(sourceObj);
  20094. if (sourceObj.src && sourceObj.src === src) {
  20095. matchingSourceEls.push(sourceObj.src);
  20096. }
  20097. }
  20098. // if we have matching source els but not matching sources
  20099. // the current source cache is not up to date
  20100. if (matchingSourceEls.length && !matchingSources.length) {
  20101. this.cache_.sources = sourceElSources;
  20102. // if we don't have matching source or source els set the
  20103. // sources cache to the `currentSource` cache
  20104. } else if (!matchingSources.length) {
  20105. this.cache_.sources = [this.cache_.source];
  20106. }
  20107. // update the tech `src` cache
  20108. this.cache_.src = src;
  20109. }
  20110. /**
  20111. * *EXPERIMENTAL* Fired when the source is set or changed on the {@link Tech}
  20112. * causing the media element to reload.
  20113. *
  20114. * It will fire for the initial source and each subsequent source.
  20115. * This event is a custom event from Video.js and is triggered by the {@link Tech}.
  20116. *
  20117. * The event object for this event contains a `src` property that will contain the source
  20118. * that was available when the event was triggered. This is generally only necessary if Video.js
  20119. * is switching techs while the source was being changed.
  20120. *
  20121. * It is also fired when `load` is called on the player (or media element)
  20122. * because the {@link https://html.spec.whatwg.org/multipage/media.html#dom-media-load|specification for `load`}
  20123. * says that the resource selection algorithm needs to be aborted and restarted.
  20124. * In this case, it is very likely that the `src` property will be set to the
  20125. * empty string `""` to indicate we do not know what the source will be but
  20126. * that it is changing.
  20127. *
  20128. * *This event is currently still experimental and may change in minor releases.*
  20129. * __To use this, pass `enableSourceset` option to the player.__
  20130. *
  20131. * @event Player#sourceset
  20132. * @type {Event}
  20133. * @prop {string} src
  20134. * The source url available when the `sourceset` was triggered.
  20135. * It will be an empty string if we cannot know what the source is
  20136. * but know that the source will change.
  20137. */
  20138. /**
  20139. * Retrigger the `sourceset` event that was triggered by the {@link Tech}.
  20140. *
  20141. * @fires Player#sourceset
  20142. * @listens Tech#sourceset
  20143. * @private
  20144. */
  20145. handleTechSourceset_(event) {
  20146. // only update the source cache when the source
  20147. // was not updated using the player api
  20148. if (!this.changingSrc_) {
  20149. let updateSourceCaches = src => this.updateSourceCaches_(src);
  20150. const playerSrc = this.currentSource().src;
  20151. const eventSrc = event.src;
  20152. // if we have a playerSrc that is not a blob, and a tech src that is a blob
  20153. if (playerSrc && !/^blob:/.test(playerSrc) && /^blob:/.test(eventSrc)) {
  20154. // if both the tech source and the player source were updated we assume
  20155. // something like @videojs/http-streaming did the sourceset and skip updating the source cache.
  20156. if (!this.lastSource_ || this.lastSource_.tech !== eventSrc && this.lastSource_.player !== playerSrc) {
  20157. updateSourceCaches = () => {};
  20158. }
  20159. }
  20160. // update the source to the initial source right away
  20161. // in some cases this will be empty string
  20162. updateSourceCaches(eventSrc);
  20163. // if the `sourceset` `src` was an empty string
  20164. // wait for a `loadstart` to update the cache to `currentSrc`.
  20165. // If a sourceset happens before a `loadstart`, we reset the state
  20166. if (!event.src) {
  20167. this.tech_.any(['sourceset', 'loadstart'], e => {
  20168. // if a sourceset happens before a `loadstart` there
  20169. // is nothing to do as this `handleTechSourceset_`
  20170. // will be called again and this will be handled there.
  20171. if (e.type === 'sourceset') {
  20172. return;
  20173. }
  20174. const techSrc = this.techGet('currentSrc');
  20175. this.lastSource_.tech = techSrc;
  20176. this.updateSourceCaches_(techSrc);
  20177. });
  20178. }
  20179. }
  20180. this.lastSource_ = {
  20181. player: this.currentSource().src,
  20182. tech: event.src
  20183. };
  20184. this.trigger({
  20185. src: event.src,
  20186. type: 'sourceset'
  20187. });
  20188. }
  20189. /**
  20190. * Add/remove the vjs-has-started class
  20191. *
  20192. *
  20193. * @param {boolean} request
  20194. * - true: adds the class
  20195. * - false: remove the class
  20196. *
  20197. * @return {boolean}
  20198. * the boolean value of hasStarted_
  20199. */
  20200. hasStarted(request) {
  20201. if (request === undefined) {
  20202. // act as getter, if we have no request to change
  20203. return this.hasStarted_;
  20204. }
  20205. if (request === this.hasStarted_) {
  20206. return;
  20207. }
  20208. this.hasStarted_ = request;
  20209. if (this.hasStarted_) {
  20210. this.addClass('vjs-has-started');
  20211. } else {
  20212. this.removeClass('vjs-has-started');
  20213. }
  20214. }
  20215. /**
  20216. * Fired whenever the media begins or resumes playback
  20217. *
  20218. * @see [Spec]{@link https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-play}
  20219. * @fires Player#play
  20220. * @listens Tech#play
  20221. * @private
  20222. */
  20223. handleTechPlay_() {
  20224. this.removeClass('vjs-ended', 'vjs-paused');
  20225. this.addClass('vjs-playing');
  20226. // hide the poster when the user hits play
  20227. this.hasStarted(true);
  20228. /**
  20229. * Triggered whenever an {@link Tech#play} event happens. Indicates that
  20230. * playback has started or resumed.
  20231. *
  20232. * @event Player#play
  20233. * @type {Event}
  20234. */
  20235. this.trigger('play');
  20236. }
  20237. /**
  20238. * Retrigger the `ratechange` event that was triggered by the {@link Tech}.
  20239. *
  20240. * If there were any events queued while the playback rate was zero, fire
  20241. * those events now.
  20242. *
  20243. * @private
  20244. * @method Player#handleTechRateChange_
  20245. * @fires Player#ratechange
  20246. * @listens Tech#ratechange
  20247. */
  20248. handleTechRateChange_() {
  20249. if (this.tech_.playbackRate() > 0 && this.cache_.lastPlaybackRate === 0) {
  20250. this.queuedCallbacks_.forEach(queued => queued.callback(queued.event));
  20251. this.queuedCallbacks_ = [];
  20252. }
  20253. this.cache_.lastPlaybackRate = this.tech_.playbackRate();
  20254. /**
  20255. * Fires when the playing speed of the audio/video is changed
  20256. *
  20257. * @event Player#ratechange
  20258. * @type {event}
  20259. */
  20260. this.trigger('ratechange');
  20261. }
  20262. /**
  20263. * Retrigger the `waiting` event that was triggered by the {@link Tech}.
  20264. *
  20265. * @fires Player#waiting
  20266. * @listens Tech#waiting
  20267. * @private
  20268. */
  20269. handleTechWaiting_() {
  20270. this.addClass('vjs-waiting');
  20271. /**
  20272. * A readyState change on the DOM element has caused playback to stop.
  20273. *
  20274. * @event Player#waiting
  20275. * @type {Event}
  20276. */
  20277. this.trigger('waiting');
  20278. // Browsers may emit a timeupdate event after a waiting event. In order to prevent
  20279. // premature removal of the waiting class, wait for the time to change.
  20280. const timeWhenWaiting = this.currentTime();
  20281. const timeUpdateListener = () => {
  20282. if (timeWhenWaiting !== this.currentTime()) {
  20283. this.removeClass('vjs-waiting');
  20284. this.off('timeupdate', timeUpdateListener);
  20285. }
  20286. };
  20287. this.on('timeupdate', timeUpdateListener);
  20288. }
  20289. /**
  20290. * Retrigger the `canplay` event that was triggered by the {@link Tech}.
  20291. * > Note: This is not consistent between browsers. See #1351
  20292. *
  20293. * @fires Player#canplay
  20294. * @listens Tech#canplay
  20295. * @private
  20296. */
  20297. handleTechCanPlay_() {
  20298. this.removeClass('vjs-waiting');
  20299. /**
  20300. * The media has a readyState of HAVE_FUTURE_DATA or greater.
  20301. *
  20302. * @event Player#canplay
  20303. * @type {Event}
  20304. */
  20305. this.trigger('canplay');
  20306. }
  20307. /**
  20308. * Retrigger the `canplaythrough` event that was triggered by the {@link Tech}.
  20309. *
  20310. * @fires Player#canplaythrough
  20311. * @listens Tech#canplaythrough
  20312. * @private
  20313. */
  20314. handleTechCanPlayThrough_() {
  20315. this.removeClass('vjs-waiting');
  20316. /**
  20317. * The media has a readyState of HAVE_ENOUGH_DATA or greater. This means that the
  20318. * entire media file can be played without buffering.
  20319. *
  20320. * @event Player#canplaythrough
  20321. * @type {Event}
  20322. */
  20323. this.trigger('canplaythrough');
  20324. }
  20325. /**
  20326. * Retrigger the `playing` event that was triggered by the {@link Tech}.
  20327. *
  20328. * @fires Player#playing
  20329. * @listens Tech#playing
  20330. * @private
  20331. */
  20332. handleTechPlaying_() {
  20333. this.removeClass('vjs-waiting');
  20334. /**
  20335. * The media is no longer blocked from playback, and has started playing.
  20336. *
  20337. * @event Player#playing
  20338. * @type {Event}
  20339. */
  20340. this.trigger('playing');
  20341. }
  20342. /**
  20343. * Retrigger the `seeking` event that was triggered by the {@link Tech}.
  20344. *
  20345. * @fires Player#seeking
  20346. * @listens Tech#seeking
  20347. * @private
  20348. */
  20349. handleTechSeeking_() {
  20350. this.addClass('vjs-seeking');
  20351. /**
  20352. * Fired whenever the player is jumping to a new time
  20353. *
  20354. * @event Player#seeking
  20355. * @type {Event}
  20356. */
  20357. this.trigger('seeking');
  20358. }
  20359. /**
  20360. * Retrigger the `seeked` event that was triggered by the {@link Tech}.
  20361. *
  20362. * @fires Player#seeked
  20363. * @listens Tech#seeked
  20364. * @private
  20365. */
  20366. handleTechSeeked_() {
  20367. this.removeClass('vjs-seeking', 'vjs-ended');
  20368. /**
  20369. * Fired when the player has finished jumping to a new time
  20370. *
  20371. * @event Player#seeked
  20372. * @type {Event}
  20373. */
  20374. this.trigger('seeked');
  20375. }
  20376. /**
  20377. * Retrigger the `pause` event that was triggered by the {@link Tech}.
  20378. *
  20379. * @fires Player#pause
  20380. * @listens Tech#pause
  20381. * @private
  20382. */
  20383. handleTechPause_() {
  20384. this.removeClass('vjs-playing');
  20385. this.addClass('vjs-paused');
  20386. /**
  20387. * Fired whenever the media has been paused
  20388. *
  20389. * @event Player#pause
  20390. * @type {Event}
  20391. */
  20392. this.trigger('pause');
  20393. }
  20394. /**
  20395. * Retrigger the `ended` event that was triggered by the {@link Tech}.
  20396. *
  20397. * @fires Player#ended
  20398. * @listens Tech#ended
  20399. * @private
  20400. */
  20401. handleTechEnded_() {
  20402. this.addClass('vjs-ended');
  20403. this.removeClass('vjs-waiting');
  20404. if (this.options_.loop) {
  20405. this.currentTime(0);
  20406. this.play();
  20407. } else if (!this.paused()) {
  20408. this.pause();
  20409. }
  20410. /**
  20411. * Fired when the end of the media resource is reached (currentTime == duration)
  20412. *
  20413. * @event Player#ended
  20414. * @type {Event}
  20415. */
  20416. this.trigger('ended');
  20417. }
  20418. /**
  20419. * Fired when the duration of the media resource is first known or changed
  20420. *
  20421. * @listens Tech#durationchange
  20422. * @private
  20423. */
  20424. handleTechDurationChange_() {
  20425. this.duration(this.techGet_('duration'));
  20426. }
  20427. /**
  20428. * Handle a click on the media element to play/pause
  20429. *
  20430. * @param {Event} event
  20431. * the event that caused this function to trigger
  20432. *
  20433. * @listens Tech#click
  20434. * @private
  20435. */
  20436. handleTechClick_(event) {
  20437. // When controls are disabled a click should not toggle playback because
  20438. // the click is considered a control
  20439. if (!this.controls_) {
  20440. return;
  20441. }
  20442. if (this.options_ === undefined || this.options_.userActions === undefined || this.options_.userActions.click === undefined || this.options_.userActions.click !== false) {
  20443. if (this.options_ !== undefined && this.options_.userActions !== undefined && typeof this.options_.userActions.click === 'function') {
  20444. this.options_.userActions.click.call(this, event);
  20445. } else if (this.paused()) {
  20446. silencePromise(this.play());
  20447. } else {
  20448. this.pause();
  20449. }
  20450. }
  20451. }
  20452. /**
  20453. * Handle a double-click on the media element to enter/exit fullscreen
  20454. *
  20455. * @param {Event} event
  20456. * the event that caused this function to trigger
  20457. *
  20458. * @listens Tech#dblclick
  20459. * @private
  20460. */
  20461. handleTechDoubleClick_(event) {
  20462. if (!this.controls_) {
  20463. return;
  20464. }
  20465. // we do not want to toggle fullscreen state
  20466. // when double-clicking inside a control bar or a modal
  20467. const inAllowedEls = Array.prototype.some.call(this.$$('.vjs-control-bar, .vjs-modal-dialog'), el => el.contains(event.target));
  20468. if (!inAllowedEls) {
  20469. /*
  20470. * options.userActions.doubleClick
  20471. *
  20472. * If `undefined` or `true`, double-click toggles fullscreen if controls are present
  20473. * Set to `false` to disable double-click handling
  20474. * Set to a function to substitute an external double-click handler
  20475. */
  20476. if (this.options_ === undefined || this.options_.userActions === undefined || this.options_.userActions.doubleClick === undefined || this.options_.userActions.doubleClick !== false) {
  20477. if (this.options_ !== undefined && this.options_.userActions !== undefined && typeof this.options_.userActions.doubleClick === 'function') {
  20478. this.options_.userActions.doubleClick.call(this, event);
  20479. } else if (this.isFullscreen()) {
  20480. this.exitFullscreen();
  20481. } else {
  20482. this.requestFullscreen();
  20483. }
  20484. }
  20485. }
  20486. }
  20487. /**
  20488. * Handle a tap on the media element. It will toggle the user
  20489. * activity state, which hides and shows the controls.
  20490. *
  20491. * @listens Tech#tap
  20492. * @private
  20493. */
  20494. handleTechTap_() {
  20495. this.userActive(!this.userActive());
  20496. }
  20497. /**
  20498. * Handle touch to start
  20499. *
  20500. * @listens Tech#touchstart
  20501. * @private
  20502. */
  20503. handleTechTouchStart_() {
  20504. this.userWasActive = this.userActive();
  20505. }
  20506. /**
  20507. * Handle touch to move
  20508. *
  20509. * @listens Tech#touchmove
  20510. * @private
  20511. */
  20512. handleTechTouchMove_() {
  20513. if (this.userWasActive) {
  20514. this.reportUserActivity();
  20515. }
  20516. }
  20517. /**
  20518. * Handle touch to end
  20519. *
  20520. * @param {Event} event
  20521. * the touchend event that triggered
  20522. * this function
  20523. *
  20524. * @listens Tech#touchend
  20525. * @private
  20526. */
  20527. handleTechTouchEnd_(event) {
  20528. // Stop the mouse events from also happening
  20529. if (event.cancelable) {
  20530. event.preventDefault();
  20531. }
  20532. }
  20533. /**
  20534. * @private
  20535. */
  20536. toggleFullscreenClass_() {
  20537. if (this.isFullscreen()) {
  20538. this.addClass('vjs-fullscreen');
  20539. } else {
  20540. this.removeClass('vjs-fullscreen');
  20541. }
  20542. }
  20543. /**
  20544. * when the document fschange event triggers it calls this
  20545. */
  20546. documentFullscreenChange_(e) {
  20547. const targetPlayer = e.target.player;
  20548. // if another player was fullscreen
  20549. // do a null check for targetPlayer because older firefox's would put document as e.target
  20550. if (targetPlayer && targetPlayer !== this) {
  20551. return;
  20552. }
  20553. const el = this.el();
  20554. let isFs = document[this.fsApi_.fullscreenElement] === el;
  20555. if (!isFs && el.matches) {
  20556. isFs = el.matches(':' + this.fsApi_.fullscreen);
  20557. } else if (!isFs && el.msMatchesSelector) {
  20558. isFs = el.msMatchesSelector(':' + this.fsApi_.fullscreen);
  20559. }
  20560. this.isFullscreen(isFs);
  20561. }
  20562. /**
  20563. * Handle Tech Fullscreen Change
  20564. *
  20565. * @param {Event} event
  20566. * the fullscreenchange event that triggered this function
  20567. *
  20568. * @param {Object} data
  20569. * the data that was sent with the event
  20570. *
  20571. * @private
  20572. * @listens Tech#fullscreenchange
  20573. * @fires Player#fullscreenchange
  20574. */
  20575. handleTechFullscreenChange_(event, data) {
  20576. if (data) {
  20577. if (data.nativeIOSFullscreen) {
  20578. this.addClass('vjs-ios-native-fs');
  20579. this.tech_.one('webkitendfullscreen', () => {
  20580. this.removeClass('vjs-ios-native-fs');
  20581. });
  20582. }
  20583. this.isFullscreen(data.isFullscreen);
  20584. }
  20585. }
  20586. handleTechFullscreenError_(event, err) {
  20587. this.trigger('fullscreenerror', err);
  20588. }
  20589. /**
  20590. * @private
  20591. */
  20592. togglePictureInPictureClass_() {
  20593. if (this.isInPictureInPicture()) {
  20594. this.addClass('vjs-picture-in-picture');
  20595. } else {
  20596. this.removeClass('vjs-picture-in-picture');
  20597. }
  20598. }
  20599. /**
  20600. * Handle Tech Enter Picture-in-Picture.
  20601. *
  20602. * @param {Event} event
  20603. * the enterpictureinpicture event that triggered this function
  20604. *
  20605. * @private
  20606. * @listens Tech#enterpictureinpicture
  20607. */
  20608. handleTechEnterPictureInPicture_(event) {
  20609. this.isInPictureInPicture(true);
  20610. }
  20611. /**
  20612. * Handle Tech Leave Picture-in-Picture.
  20613. *
  20614. * @param {Event} event
  20615. * the leavepictureinpicture event that triggered this function
  20616. *
  20617. * @private
  20618. * @listens Tech#leavepictureinpicture
  20619. */
  20620. handleTechLeavePictureInPicture_(event) {
  20621. this.isInPictureInPicture(false);
  20622. }
  20623. /**
  20624. * Fires when an error occurred during the loading of an audio/video.
  20625. *
  20626. * @private
  20627. * @listens Tech#error
  20628. */
  20629. handleTechError_() {
  20630. const error = this.tech_.error();
  20631. this.error(error);
  20632. }
  20633. /**
  20634. * Retrigger the `textdata` event that was triggered by the {@link Tech}.
  20635. *
  20636. * @fires Player#textdata
  20637. * @listens Tech#textdata
  20638. * @private
  20639. */
  20640. handleTechTextData_() {
  20641. let data = null;
  20642. if (arguments.length > 1) {
  20643. data = arguments[1];
  20644. }
  20645. /**
  20646. * Fires when we get a textdata event from tech
  20647. *
  20648. * @event Player#textdata
  20649. * @type {Event}
  20650. */
  20651. this.trigger('textdata', data);
  20652. }
  20653. /**
  20654. * Get object for cached values.
  20655. *
  20656. * @return {Object}
  20657. * get the current object cache
  20658. */
  20659. getCache() {
  20660. return this.cache_;
  20661. }
  20662. /**
  20663. * Resets the internal cache object.
  20664. *
  20665. * Using this function outside the player constructor or reset method may
  20666. * have unintended side-effects.
  20667. *
  20668. * @private
  20669. */
  20670. resetCache_() {
  20671. this.cache_ = {
  20672. // Right now, the currentTime is not _really_ cached because it is always
  20673. // retrieved from the tech (see: currentTime). However, for completeness,
  20674. // we set it to zero here to ensure that if we do start actually caching
  20675. // it, we reset it along with everything else.
  20676. currentTime: 0,
  20677. initTime: 0,
  20678. inactivityTimeout: this.options_.inactivityTimeout,
  20679. duration: NaN,
  20680. lastVolume: 1,
  20681. lastPlaybackRate: this.defaultPlaybackRate(),
  20682. media: null,
  20683. src: '',
  20684. source: {},
  20685. sources: [],
  20686. playbackRates: [],
  20687. volume: 1
  20688. };
  20689. }
  20690. /**
  20691. * Pass values to the playback tech
  20692. *
  20693. * @param {string} [method]
  20694. * the method to call
  20695. *
  20696. * @param {Object} arg
  20697. * the argument to pass
  20698. *
  20699. * @private
  20700. */
  20701. techCall_(method, arg) {
  20702. // If it's not ready yet, call method when it is
  20703. this.ready(function () {
  20704. if (method in allowedSetters) {
  20705. return set(this.middleware_, this.tech_, method, arg);
  20706. } else if (method in allowedMediators) {
  20707. return mediate(this.middleware_, this.tech_, method, arg);
  20708. }
  20709. try {
  20710. if (this.tech_) {
  20711. this.tech_[method](arg);
  20712. }
  20713. } catch (e) {
  20714. log(e);
  20715. throw e;
  20716. }
  20717. }, true);
  20718. }
  20719. /**
  20720. * Mediate attempt to call playback tech method
  20721. * and return the value of the method called.
  20722. *
  20723. * @param {string} method
  20724. * Tech method
  20725. *
  20726. * @return {*}
  20727. * Value returned by the tech method called, undefined if tech
  20728. * is not ready or tech method is not present
  20729. *
  20730. * @private
  20731. */
  20732. techGet_(method) {
  20733. if (!this.tech_ || !this.tech_.isReady_) {
  20734. return;
  20735. }
  20736. if (method in allowedGetters) {
  20737. return get(this.middleware_, this.tech_, method);
  20738. } else if (method in allowedMediators) {
  20739. return mediate(this.middleware_, this.tech_, method);
  20740. }
  20741. // Log error when playback tech object is present but method
  20742. // is undefined or unavailable
  20743. try {
  20744. return this.tech_[method]();
  20745. } catch (e) {
  20746. // When building additional tech libs, an expected method may not be defined yet
  20747. if (this.tech_[method] === undefined) {
  20748. log(`Video.js: ${method} method not defined for ${this.techName_} playback technology.`, e);
  20749. throw e;
  20750. }
  20751. // When a method isn't available on the object it throws a TypeError
  20752. if (e.name === 'TypeError') {
  20753. log(`Video.js: ${method} unavailable on ${this.techName_} playback technology element.`, e);
  20754. this.tech_.isReady_ = false;
  20755. throw e;
  20756. }
  20757. // If error unknown, just log and throw
  20758. log(e);
  20759. throw e;
  20760. }
  20761. }
  20762. /**
  20763. * Attempt to begin playback at the first opportunity.
  20764. *
  20765. * @return {Promise|undefined}
  20766. * Returns a promise if the browser supports Promises (or one
  20767. * was passed in as an option). This promise will be resolved on
  20768. * the return value of play. If this is undefined it will fulfill the
  20769. * promise chain otherwise the promise chain will be fulfilled when
  20770. * the promise from play is fulfilled.
  20771. */
  20772. play() {
  20773. return new Promise(resolve => {
  20774. this.play_(resolve);
  20775. });
  20776. }
  20777. /**
  20778. * The actual logic for play, takes a callback that will be resolved on the
  20779. * return value of play. This allows us to resolve to the play promise if there
  20780. * is one on modern browsers.
  20781. *
  20782. * @private
  20783. * @param {Function} [callback]
  20784. * The callback that should be called when the techs play is actually called
  20785. */
  20786. play_(callback = silencePromise) {
  20787. this.playCallbacks_.push(callback);
  20788. const isSrcReady = Boolean(!this.changingSrc_ && (this.src() || this.currentSrc()));
  20789. const isSafariOrIOS = Boolean(IS_ANY_SAFARI || IS_IOS);
  20790. // treat calls to play_ somewhat like the `one` event function
  20791. if (this.waitToPlay_) {
  20792. this.off(['ready', 'loadstart'], this.waitToPlay_);
  20793. this.waitToPlay_ = null;
  20794. }
  20795. // if the player/tech is not ready or the src itself is not ready
  20796. // queue up a call to play on `ready` or `loadstart`
  20797. if (!this.isReady_ || !isSrcReady) {
  20798. this.waitToPlay_ = e => {
  20799. this.play_();
  20800. };
  20801. this.one(['ready', 'loadstart'], this.waitToPlay_);
  20802. // if we are in Safari, there is a high chance that loadstart will trigger after the gesture timeperiod
  20803. // in that case, we need to prime the video element by calling load so it'll be ready in time
  20804. if (!isSrcReady && isSafariOrIOS) {
  20805. this.load();
  20806. }
  20807. return;
  20808. }
  20809. // If the player/tech is ready and we have a source, we can attempt playback.
  20810. const val = this.techGet_('play');
  20811. // For native playback, reset the progress bar if we get a play call from a replay.
  20812. const isNativeReplay = isSafariOrIOS && this.hasClass('vjs-ended');
  20813. if (isNativeReplay) {
  20814. this.resetProgressBar_();
  20815. }
  20816. // play was terminated if the returned value is null
  20817. if (val === null) {
  20818. this.runPlayTerminatedQueue_();
  20819. } else {
  20820. this.runPlayCallbacks_(val);
  20821. }
  20822. }
  20823. /**
  20824. * These functions will be run when if play is terminated. If play
  20825. * runPlayCallbacks_ is run these function will not be run. This allows us
  20826. * to differentiate between a terminated play and an actual call to play.
  20827. */
  20828. runPlayTerminatedQueue_() {
  20829. const queue = this.playTerminatedQueue_.slice(0);
  20830. this.playTerminatedQueue_ = [];
  20831. queue.forEach(function (q) {
  20832. q();
  20833. });
  20834. }
  20835. /**
  20836. * When a callback to play is delayed we have to run these
  20837. * callbacks when play is actually called on the tech. This function
  20838. * runs the callbacks that were delayed and accepts the return value
  20839. * from the tech.
  20840. *
  20841. * @param {undefined|Promise} val
  20842. * The return value from the tech.
  20843. */
  20844. runPlayCallbacks_(val) {
  20845. const callbacks = this.playCallbacks_.slice(0);
  20846. this.playCallbacks_ = [];
  20847. // clear play terminatedQueue since we finished a real play
  20848. this.playTerminatedQueue_ = [];
  20849. callbacks.forEach(function (cb) {
  20850. cb(val);
  20851. });
  20852. }
  20853. /**
  20854. * Pause the video playback
  20855. *
  20856. * @return {Player}
  20857. * A reference to the player object this function was called on
  20858. */
  20859. pause() {
  20860. this.techCall_('pause');
  20861. }
  20862. /**
  20863. * Check if the player is paused or has yet to play
  20864. *
  20865. * @return {boolean}
  20866. * - false: if the media is currently playing
  20867. * - true: if media is not currently playing
  20868. */
  20869. paused() {
  20870. // The initial state of paused should be true (in Safari it's actually false)
  20871. return this.techGet_('paused') === false ? false : true;
  20872. }
  20873. /**
  20874. * Get a TimeRange object representing the current ranges of time that the user
  20875. * has played.
  20876. *
  20877. * @return { import('./utils/time').TimeRange }
  20878. * A time range object that represents all the increments of time that have
  20879. * been played.
  20880. */
  20881. played() {
  20882. return this.techGet_('played') || createTimeRanges(0, 0);
  20883. }
  20884. /**
  20885. * Returns whether or not the user is "scrubbing". Scrubbing is
  20886. * when the user has clicked the progress bar handle and is
  20887. * dragging it along the progress bar.
  20888. *
  20889. * @param {boolean} [isScrubbing]
  20890. * whether the user is or is not scrubbing
  20891. *
  20892. * @return {boolean}
  20893. * The value of scrubbing when getting
  20894. */
  20895. scrubbing(isScrubbing) {
  20896. if (typeof isScrubbing === 'undefined') {
  20897. return this.scrubbing_;
  20898. }
  20899. this.scrubbing_ = !!isScrubbing;
  20900. this.techCall_('setScrubbing', this.scrubbing_);
  20901. if (isScrubbing) {
  20902. this.addClass('vjs-scrubbing');
  20903. } else {
  20904. this.removeClass('vjs-scrubbing');
  20905. }
  20906. }
  20907. /**
  20908. * Get or set the current time (in seconds)
  20909. *
  20910. * @param {number|string} [seconds]
  20911. * The time to seek to in seconds
  20912. *
  20913. * @return {number}
  20914. * - the current time in seconds when getting
  20915. */
  20916. currentTime(seconds) {
  20917. if (typeof seconds !== 'undefined') {
  20918. if (seconds < 0) {
  20919. seconds = 0;
  20920. }
  20921. if (!this.isReady_ || this.changingSrc_ || !this.tech_ || !this.tech_.isReady_) {
  20922. this.cache_.initTime = seconds;
  20923. this.off('canplay', this.boundApplyInitTime_);
  20924. this.one('canplay', this.boundApplyInitTime_);
  20925. return;
  20926. }
  20927. this.techCall_('setCurrentTime', seconds);
  20928. this.cache_.initTime = 0;
  20929. return;
  20930. }
  20931. // cache last currentTime and return. default to 0 seconds
  20932. //
  20933. // Caching the currentTime is meant to prevent a massive amount of reads on the tech's
  20934. // currentTime when scrubbing, but may not provide much performance benefit after all.
  20935. // Should be tested. Also something has to read the actual current time or the cache will
  20936. // never get updated.
  20937. this.cache_.currentTime = this.techGet_('currentTime') || 0;
  20938. return this.cache_.currentTime;
  20939. }
  20940. /**
  20941. * Apply the value of initTime stored in cache as currentTime.
  20942. *
  20943. * @private
  20944. */
  20945. applyInitTime_() {
  20946. this.currentTime(this.cache_.initTime);
  20947. }
  20948. /**
  20949. * Normally gets the length in time of the video in seconds;
  20950. * in all but the rarest use cases an argument will NOT be passed to the method
  20951. *
  20952. * > **NOTE**: The video must have started loading before the duration can be
  20953. * known, and depending on preload behaviour may not be known until the video starts
  20954. * playing.
  20955. *
  20956. * @fires Player#durationchange
  20957. *
  20958. * @param {number} [seconds]
  20959. * The duration of the video to set in seconds
  20960. *
  20961. * @return {number}
  20962. * - The duration of the video in seconds when getting
  20963. */
  20964. duration(seconds) {
  20965. if (seconds === undefined) {
  20966. // return NaN if the duration is not known
  20967. return this.cache_.duration !== undefined ? this.cache_.duration : NaN;
  20968. }
  20969. seconds = parseFloat(seconds);
  20970. // Standardize on Infinity for signaling video is live
  20971. if (seconds < 0) {
  20972. seconds = Infinity;
  20973. }
  20974. if (seconds !== this.cache_.duration) {
  20975. // Cache the last set value for optimized scrubbing
  20976. this.cache_.duration = seconds;
  20977. if (seconds === Infinity) {
  20978. this.addClass('vjs-live');
  20979. } else {
  20980. this.removeClass('vjs-live');
  20981. }
  20982. if (!isNaN(seconds)) {
  20983. // Do not fire durationchange unless the duration value is known.
  20984. // @see [Spec]{@link https://www.w3.org/TR/2011/WD-html5-20110113/video.html#media-element-load-algorithm}
  20985. /**
  20986. * @event Player#durationchange
  20987. * @type {Event}
  20988. */
  20989. this.trigger('durationchange');
  20990. }
  20991. }
  20992. }
  20993. /**
  20994. * Calculates how much time is left in the video. Not part
  20995. * of the native video API.
  20996. *
  20997. * @return {number}
  20998. * The time remaining in seconds
  20999. */
  21000. remainingTime() {
  21001. return this.duration() - this.currentTime();
  21002. }
  21003. /**
  21004. * A remaining time function that is intended to be used when
  21005. * the time is to be displayed directly to the user.
  21006. *
  21007. * @return {number}
  21008. * The rounded time remaining in seconds
  21009. */
  21010. remainingTimeDisplay() {
  21011. return Math.floor(this.duration()) - Math.floor(this.currentTime());
  21012. }
  21013. //
  21014. // Kind of like an array of portions of the video that have been downloaded.
  21015. /**
  21016. * Get a TimeRange object with an array of the times of the video
  21017. * that have been downloaded. If you just want the percent of the
  21018. * video that's been downloaded, use bufferedPercent.
  21019. *
  21020. * @see [Buffered Spec]{@link http://dev.w3.org/html5/spec/video.html#dom-media-buffered}
  21021. *
  21022. * @return { import('./utils/time').TimeRange }
  21023. * A mock {@link TimeRanges} object (following HTML spec)
  21024. */
  21025. buffered() {
  21026. let buffered = this.techGet_('buffered');
  21027. if (!buffered || !buffered.length) {
  21028. buffered = createTimeRanges(0, 0);
  21029. }
  21030. return buffered;
  21031. }
  21032. /**
  21033. * Get the percent (as a decimal) of the video that's been downloaded.
  21034. * This method is not a part of the native HTML video API.
  21035. *
  21036. * @return {number}
  21037. * A decimal between 0 and 1 representing the percent
  21038. * that is buffered 0 being 0% and 1 being 100%
  21039. */
  21040. bufferedPercent() {
  21041. return bufferedPercent(this.buffered(), this.duration());
  21042. }
  21043. /**
  21044. * Get the ending time of the last buffered time range
  21045. * This is used in the progress bar to encapsulate all time ranges.
  21046. *
  21047. * @return {number}
  21048. * The end of the last buffered time range
  21049. */
  21050. bufferedEnd() {
  21051. const buffered = this.buffered();
  21052. const duration = this.duration();
  21053. let end = buffered.end(buffered.length - 1);
  21054. if (end > duration) {
  21055. end = duration;
  21056. }
  21057. return end;
  21058. }
  21059. /**
  21060. * Get or set the current volume of the media
  21061. *
  21062. * @param {number} [percentAsDecimal]
  21063. * The new volume as a decimal percent:
  21064. * - 0 is muted/0%/off
  21065. * - 1.0 is 100%/full
  21066. * - 0.5 is half volume or 50%
  21067. *
  21068. * @return {number}
  21069. * The current volume as a percent when getting
  21070. */
  21071. volume(percentAsDecimal) {
  21072. let vol;
  21073. if (percentAsDecimal !== undefined) {
  21074. // Force value to between 0 and 1
  21075. vol = Math.max(0, Math.min(1, parseFloat(percentAsDecimal)));
  21076. this.cache_.volume = vol;
  21077. this.techCall_('setVolume', vol);
  21078. if (vol > 0) {
  21079. this.lastVolume_(vol);
  21080. }
  21081. return;
  21082. }
  21083. // Default to 1 when returning current volume.
  21084. vol = parseFloat(this.techGet_('volume'));
  21085. return isNaN(vol) ? 1 : vol;
  21086. }
  21087. /**
  21088. * Get the current muted state, or turn mute on or off
  21089. *
  21090. * @param {boolean} [muted]
  21091. * - true to mute
  21092. * - false to unmute
  21093. *
  21094. * @return {boolean}
  21095. * - true if mute is on and getting
  21096. * - false if mute is off and getting
  21097. */
  21098. muted(muted) {
  21099. if (muted !== undefined) {
  21100. this.techCall_('setMuted', muted);
  21101. return;
  21102. }
  21103. return this.techGet_('muted') || false;
  21104. }
  21105. /**
  21106. * Get the current defaultMuted state, or turn defaultMuted on or off. defaultMuted
  21107. * indicates the state of muted on initial playback.
  21108. *
  21109. * ```js
  21110. * var myPlayer = videojs('some-player-id');
  21111. *
  21112. * myPlayer.src("http://www.example.com/path/to/video.mp4");
  21113. *
  21114. * // get, should be false
  21115. * console.log(myPlayer.defaultMuted());
  21116. * // set to true
  21117. * myPlayer.defaultMuted(true);
  21118. * // get should be true
  21119. * console.log(myPlayer.defaultMuted());
  21120. * ```
  21121. *
  21122. * @param {boolean} [defaultMuted]
  21123. * - true to mute
  21124. * - false to unmute
  21125. *
  21126. * @return {boolean|Player}
  21127. * - true if defaultMuted is on and getting
  21128. * - false if defaultMuted is off and getting
  21129. * - A reference to the current player when setting
  21130. */
  21131. defaultMuted(defaultMuted) {
  21132. if (defaultMuted !== undefined) {
  21133. return this.techCall_('setDefaultMuted', defaultMuted);
  21134. }
  21135. return this.techGet_('defaultMuted') || false;
  21136. }
  21137. /**
  21138. * Get the last volume, or set it
  21139. *
  21140. * @param {number} [percentAsDecimal]
  21141. * The new last volume as a decimal percent:
  21142. * - 0 is muted/0%/off
  21143. * - 1.0 is 100%/full
  21144. * - 0.5 is half volume or 50%
  21145. *
  21146. * @return {number}
  21147. * the current value of lastVolume as a percent when getting
  21148. *
  21149. * @private
  21150. */
  21151. lastVolume_(percentAsDecimal) {
  21152. if (percentAsDecimal !== undefined && percentAsDecimal !== 0) {
  21153. this.cache_.lastVolume = percentAsDecimal;
  21154. return;
  21155. }
  21156. return this.cache_.lastVolume;
  21157. }
  21158. /**
  21159. * Check if current tech can support native fullscreen
  21160. * (e.g. with built in controls like iOS)
  21161. *
  21162. * @return {boolean}
  21163. * if native fullscreen is supported
  21164. */
  21165. supportsFullScreen() {
  21166. return this.techGet_('supportsFullScreen') || false;
  21167. }
  21168. /**
  21169. * Check if the player is in fullscreen mode or tell the player that it
  21170. * is or is not in fullscreen mode.
  21171. *
  21172. * > NOTE: As of the latest HTML5 spec, isFullscreen is no longer an official
  21173. * property and instead document.fullscreenElement is used. But isFullscreen is
  21174. * still a valuable property for internal player workings.
  21175. *
  21176. * @param {boolean} [isFS]
  21177. * Set the players current fullscreen state
  21178. *
  21179. * @return {boolean}
  21180. * - true if fullscreen is on and getting
  21181. * - false if fullscreen is off and getting
  21182. */
  21183. isFullscreen(isFS) {
  21184. if (isFS !== undefined) {
  21185. const oldValue = this.isFullscreen_;
  21186. this.isFullscreen_ = Boolean(isFS);
  21187. // if we changed fullscreen state and we're in prefixed mode, trigger fullscreenchange
  21188. // this is the only place where we trigger fullscreenchange events for older browsers
  21189. // fullWindow mode is treated as a prefixed event and will get a fullscreenchange event as well
  21190. if (this.isFullscreen_ !== oldValue && this.fsApi_.prefixed) {
  21191. /**
  21192. * @event Player#fullscreenchange
  21193. * @type {Event}
  21194. */
  21195. this.trigger('fullscreenchange');
  21196. }
  21197. this.toggleFullscreenClass_();
  21198. return;
  21199. }
  21200. return this.isFullscreen_;
  21201. }
  21202. /**
  21203. * Increase the size of the video to full screen
  21204. * In some browsers, full screen is not supported natively, so it enters
  21205. * "full window mode", where the video fills the browser window.
  21206. * In browsers and devices that support native full screen, sometimes the
  21207. * browser's default controls will be shown, and not the Video.js custom skin.
  21208. * This includes most mobile devices (iOS, Android) and older versions of
  21209. * Safari.
  21210. *
  21211. * @param {Object} [fullscreenOptions]
  21212. * Override the player fullscreen options
  21213. *
  21214. * @fires Player#fullscreenchange
  21215. */
  21216. requestFullscreen(fullscreenOptions) {
  21217. if (this.isInPictureInPicture()) {
  21218. this.exitPictureInPicture();
  21219. }
  21220. const self = this;
  21221. return new Promise((resolve, reject) => {
  21222. function offHandler() {
  21223. self.off('fullscreenerror', errorHandler);
  21224. self.off('fullscreenchange', changeHandler);
  21225. }
  21226. function changeHandler() {
  21227. offHandler();
  21228. resolve();
  21229. }
  21230. function errorHandler(e, err) {
  21231. offHandler();
  21232. reject(err);
  21233. }
  21234. self.one('fullscreenchange', changeHandler);
  21235. self.one('fullscreenerror', errorHandler);
  21236. const promise = self.requestFullscreenHelper_(fullscreenOptions);
  21237. if (promise) {
  21238. promise.then(offHandler, offHandler);
  21239. promise.then(resolve, reject);
  21240. }
  21241. });
  21242. }
  21243. requestFullscreenHelper_(fullscreenOptions) {
  21244. let fsOptions;
  21245. // Only pass fullscreen options to requestFullscreen in spec-compliant browsers.
  21246. // Use defaults or player configured option unless passed directly to this method.
  21247. if (!this.fsApi_.prefixed) {
  21248. fsOptions = this.options_.fullscreen && this.options_.fullscreen.options || {};
  21249. if (fullscreenOptions !== undefined) {
  21250. fsOptions = fullscreenOptions;
  21251. }
  21252. }
  21253. // This method works as follows:
  21254. // 1. if a fullscreen api is available, use it
  21255. // 1. call requestFullscreen with potential options
  21256. // 2. if we got a promise from above, use it to update isFullscreen()
  21257. // 2. otherwise, if the tech supports fullscreen, call `enterFullScreen` on it.
  21258. // This is particularly used for iPhone, older iPads, and non-safari browser on iOS.
  21259. // 3. otherwise, use "fullWindow" mode
  21260. if (this.fsApi_.requestFullscreen) {
  21261. const promise = this.el_[this.fsApi_.requestFullscreen](fsOptions);
  21262. // Even on browsers with promise support this may not return a promise
  21263. if (promise) {
  21264. promise.then(() => this.isFullscreen(true), () => this.isFullscreen(false));
  21265. }
  21266. return promise;
  21267. } else if (this.tech_.supportsFullScreen() && !this.options_.preferFullWindow === true) {
  21268. // we can't take the video.js controls fullscreen but we can go fullscreen
  21269. // with native controls
  21270. this.techCall_('enterFullScreen');
  21271. } else {
  21272. // fullscreen isn't supported so we'll just stretch the video element to
  21273. // fill the viewport
  21274. this.enterFullWindow();
  21275. }
  21276. }
  21277. /**
  21278. * Return the video to its normal size after having been in full screen mode
  21279. *
  21280. * @fires Player#fullscreenchange
  21281. */
  21282. exitFullscreen() {
  21283. const self = this;
  21284. return new Promise((resolve, reject) => {
  21285. function offHandler() {
  21286. self.off('fullscreenerror', errorHandler);
  21287. self.off('fullscreenchange', changeHandler);
  21288. }
  21289. function changeHandler() {
  21290. offHandler();
  21291. resolve();
  21292. }
  21293. function errorHandler(e, err) {
  21294. offHandler();
  21295. reject(err);
  21296. }
  21297. self.one('fullscreenchange', changeHandler);
  21298. self.one('fullscreenerror', errorHandler);
  21299. const promise = self.exitFullscreenHelper_();
  21300. if (promise) {
  21301. promise.then(offHandler, offHandler);
  21302. // map the promise to our resolve/reject methods
  21303. promise.then(resolve, reject);
  21304. }
  21305. });
  21306. }
  21307. exitFullscreenHelper_() {
  21308. if (this.fsApi_.requestFullscreen) {
  21309. const promise = document[this.fsApi_.exitFullscreen]();
  21310. // Even on browsers with promise support this may not return a promise
  21311. if (promise) {
  21312. // we're splitting the promise here, so, we want to catch the
  21313. // potential error so that this chain doesn't have unhandled errors
  21314. silencePromise(promise.then(() => this.isFullscreen(false)));
  21315. }
  21316. return promise;
  21317. } else if (this.tech_.supportsFullScreen() && !this.options_.preferFullWindow === true) {
  21318. this.techCall_('exitFullScreen');
  21319. } else {
  21320. this.exitFullWindow();
  21321. }
  21322. }
  21323. /**
  21324. * When fullscreen isn't supported we can stretch the
  21325. * video container to as wide as the browser will let us.
  21326. *
  21327. * @fires Player#enterFullWindow
  21328. */
  21329. enterFullWindow() {
  21330. this.isFullscreen(true);
  21331. this.isFullWindow = true;
  21332. // Storing original doc overflow value to return to when fullscreen is off
  21333. this.docOrigOverflow = document.documentElement.style.overflow;
  21334. // Add listener for esc key to exit fullscreen
  21335. on(document, 'keydown', this.boundFullWindowOnEscKey_);
  21336. // Hide any scroll bars
  21337. document.documentElement.style.overflow = 'hidden';
  21338. // Apply fullscreen styles
  21339. addClass(document.body, 'vjs-full-window');
  21340. /**
  21341. * @event Player#enterFullWindow
  21342. * @type {Event}
  21343. */
  21344. this.trigger('enterFullWindow');
  21345. }
  21346. /**
  21347. * Check for call to either exit full window or
  21348. * full screen on ESC key
  21349. *
  21350. * @param {string} event
  21351. * Event to check for key press
  21352. */
  21353. fullWindowOnEscKey(event) {
  21354. if (keycode.isEventKey(event, 'Esc')) {
  21355. if (this.isFullscreen() === true) {
  21356. if (!this.isFullWindow) {
  21357. this.exitFullscreen();
  21358. } else {
  21359. this.exitFullWindow();
  21360. }
  21361. }
  21362. }
  21363. }
  21364. /**
  21365. * Exit full window
  21366. *
  21367. * @fires Player#exitFullWindow
  21368. */
  21369. exitFullWindow() {
  21370. this.isFullscreen(false);
  21371. this.isFullWindow = false;
  21372. off(document, 'keydown', this.boundFullWindowOnEscKey_);
  21373. // Unhide scroll bars.
  21374. document.documentElement.style.overflow = this.docOrigOverflow;
  21375. // Remove fullscreen styles
  21376. removeClass(document.body, 'vjs-full-window');
  21377. // Resize the box, controller, and poster to original sizes
  21378. // this.positionAll();
  21379. /**
  21380. * @event Player#exitFullWindow
  21381. * @type {Event}
  21382. */
  21383. this.trigger('exitFullWindow');
  21384. }
  21385. /**
  21386. * Disable Picture-in-Picture mode.
  21387. *
  21388. * @param {boolean} value
  21389. * - true will disable Picture-in-Picture mode
  21390. * - false will enable Picture-in-Picture mode
  21391. */
  21392. disablePictureInPicture(value) {
  21393. if (value === undefined) {
  21394. return this.techGet_('disablePictureInPicture');
  21395. }
  21396. this.techCall_('setDisablePictureInPicture', value);
  21397. this.options_.disablePictureInPicture = value;
  21398. this.trigger('disablepictureinpicturechanged');
  21399. }
  21400. /**
  21401. * Check if the player is in Picture-in-Picture mode or tell the player that it
  21402. * is or is not in Picture-in-Picture mode.
  21403. *
  21404. * @param {boolean} [isPiP]
  21405. * Set the players current Picture-in-Picture state
  21406. *
  21407. * @return {boolean}
  21408. * - true if Picture-in-Picture is on and getting
  21409. * - false if Picture-in-Picture is off and getting
  21410. */
  21411. isInPictureInPicture(isPiP) {
  21412. if (isPiP !== undefined) {
  21413. this.isInPictureInPicture_ = !!isPiP;
  21414. this.togglePictureInPictureClass_();
  21415. return;
  21416. }
  21417. return !!this.isInPictureInPicture_;
  21418. }
  21419. /**
  21420. * Create a floating video window always on top of other windows so that users may
  21421. * continue consuming media while they interact with other content sites, or
  21422. * applications on their device.
  21423. *
  21424. * This can use document picture-in-picture or element picture in picture
  21425. *
  21426. * Set `enableDocumentPictureInPicture` to `true` to use docPiP on a supported browser
  21427. * Else set `disablePictureInPicture` to `false` to disable elPiP on a supported browser
  21428. *
  21429. *
  21430. * @see [Spec]{@link https://w3c.github.io/picture-in-picture/}
  21431. * @see [Spec]{@link https://wicg.github.io/document-picture-in-picture/}
  21432. *
  21433. * @fires Player#enterpictureinpicture
  21434. *
  21435. * @return {Promise}
  21436. * A promise with a Picture-in-Picture window.
  21437. */
  21438. requestPictureInPicture() {
  21439. if (this.options_.enableDocumentPictureInPicture && window.documentPictureInPicture) {
  21440. const pipContainer = document.createElement(this.el().tagName);
  21441. pipContainer.classList = this.el().classList;
  21442. pipContainer.classList.add('vjs-pip-container');
  21443. if (this.posterImage) {
  21444. pipContainer.appendChild(this.posterImage.el().cloneNode(true));
  21445. }
  21446. if (this.titleBar) {
  21447. pipContainer.appendChild(this.titleBar.el().cloneNode(true));
  21448. }
  21449. pipContainer.appendChild(createEl('p', {
  21450. className: 'vjs-pip-text'
  21451. }, {}, this.localize('Playing in picture-in-picture')));
  21452. return window.documentPictureInPicture.requestWindow({
  21453. // The aspect ratio won't be correct, Chrome bug https://crbug.com/1407629
  21454. initialAspectRatio: this.videoWidth() / this.videoHeight(),
  21455. copyStyleSheets: true
  21456. }).then(pipWindow => {
  21457. this.el_.parentNode.insertBefore(pipContainer, this.el_);
  21458. pipWindow.document.body.append(this.el_);
  21459. pipWindow.document.body.classList.add('vjs-pip-window');
  21460. this.player_.isInPictureInPicture(true);
  21461. this.player_.trigger('enterpictureinpicture');
  21462. // Listen for the PiP closing event to move the video back.
  21463. pipWindow.addEventListener('unload', event => {
  21464. const pipVideo = event.target.querySelector('.video-js');
  21465. pipContainer.replaceWith(pipVideo);
  21466. this.player_.isInPictureInPicture(false);
  21467. this.player_.trigger('leavepictureinpicture');
  21468. });
  21469. return pipWindow;
  21470. });
  21471. }
  21472. if ('pictureInPictureEnabled' in document && this.disablePictureInPicture() === false) {
  21473. /**
  21474. * This event fires when the player enters picture in picture mode
  21475. *
  21476. * @event Player#enterpictureinpicture
  21477. * @type {Event}
  21478. */
  21479. return this.techGet_('requestPictureInPicture');
  21480. }
  21481. return Promise.reject('No PiP mode is available');
  21482. }
  21483. /**
  21484. * Exit Picture-in-Picture mode.
  21485. *
  21486. * @see [Spec]{@link https://wicg.github.io/picture-in-picture}
  21487. *
  21488. * @fires Player#leavepictureinpicture
  21489. *
  21490. * @return {Promise}
  21491. * A promise.
  21492. */
  21493. exitPictureInPicture() {
  21494. if (window.documentPictureInPicture && window.documentPictureInPicture.window) {
  21495. // With documentPictureInPicture, Player#leavepictureinpicture is fired in the unload handler
  21496. window.documentPictureInPicture.window.close();
  21497. return Promise.resolve();
  21498. }
  21499. if ('pictureInPictureEnabled' in document) {
  21500. /**
  21501. * This event fires when the player leaves picture in picture mode
  21502. *
  21503. * @event Player#leavepictureinpicture
  21504. * @type {Event}
  21505. */
  21506. return document.exitPictureInPicture();
  21507. }
  21508. }
  21509. /**
  21510. * Called when this Player has focus and a key gets pressed down, or when
  21511. * any Component of this player receives a key press that it doesn't handle.
  21512. * This allows player-wide hotkeys (either as defined below, or optionally
  21513. * by an external function).
  21514. *
  21515. * @param {Event} event
  21516. * The `keydown` event that caused this function to be called.
  21517. *
  21518. * @listens keydown
  21519. */
  21520. handleKeyDown(event) {
  21521. const {
  21522. userActions
  21523. } = this.options_;
  21524. // Bail out if hotkeys are not configured.
  21525. if (!userActions || !userActions.hotkeys) {
  21526. return;
  21527. }
  21528. // Function that determines whether or not to exclude an element from
  21529. // hotkeys handling.
  21530. const excludeElement = el => {
  21531. const tagName = el.tagName.toLowerCase();
  21532. // The first and easiest test is for `contenteditable` elements.
  21533. if (el.isContentEditable) {
  21534. return true;
  21535. }
  21536. // Inputs matching these types will still trigger hotkey handling as
  21537. // they are not text inputs.
  21538. const allowedInputTypes = ['button', 'checkbox', 'hidden', 'radio', 'reset', 'submit'];
  21539. if (tagName === 'input') {
  21540. return allowedInputTypes.indexOf(el.type) === -1;
  21541. }
  21542. // The final test is by tag name. These tags will be excluded entirely.
  21543. const excludedTags = ['textarea'];
  21544. return excludedTags.indexOf(tagName) !== -1;
  21545. };
  21546. // Bail out if the user is focused on an interactive form element.
  21547. if (excludeElement(this.el_.ownerDocument.activeElement)) {
  21548. return;
  21549. }
  21550. if (typeof userActions.hotkeys === 'function') {
  21551. userActions.hotkeys.call(this, event);
  21552. } else {
  21553. this.handleHotkeys(event);
  21554. }
  21555. }
  21556. /**
  21557. * Called when this Player receives a hotkey keydown event.
  21558. * Supported player-wide hotkeys are:
  21559. *
  21560. * f - toggle fullscreen
  21561. * m - toggle mute
  21562. * k or Space - toggle play/pause
  21563. *
  21564. * @param {Event} event
  21565. * The `keydown` event that caused this function to be called.
  21566. */
  21567. handleHotkeys(event) {
  21568. const hotkeys = this.options_.userActions ? this.options_.userActions.hotkeys : {};
  21569. // set fullscreenKey, muteKey, playPauseKey from `hotkeys`, use defaults if not set
  21570. const {
  21571. fullscreenKey = keydownEvent => keycode.isEventKey(keydownEvent, 'f'),
  21572. muteKey = keydownEvent => keycode.isEventKey(keydownEvent, 'm'),
  21573. playPauseKey = keydownEvent => keycode.isEventKey(keydownEvent, 'k') || keycode.isEventKey(keydownEvent, 'Space')
  21574. } = hotkeys;
  21575. if (fullscreenKey.call(this, event)) {
  21576. event.preventDefault();
  21577. event.stopPropagation();
  21578. const FSToggle = Component.getComponent('FullscreenToggle');
  21579. if (document[this.fsApi_.fullscreenEnabled] !== false) {
  21580. FSToggle.prototype.handleClick.call(this, event);
  21581. }
  21582. } else if (muteKey.call(this, event)) {
  21583. event.preventDefault();
  21584. event.stopPropagation();
  21585. const MuteToggle = Component.getComponent('MuteToggle');
  21586. MuteToggle.prototype.handleClick.call(this, event);
  21587. } else if (playPauseKey.call(this, event)) {
  21588. event.preventDefault();
  21589. event.stopPropagation();
  21590. const PlayToggle = Component.getComponent('PlayToggle');
  21591. PlayToggle.prototype.handleClick.call(this, event);
  21592. }
  21593. }
  21594. /**
  21595. * Check whether the player can play a given mimetype
  21596. *
  21597. * @see https://www.w3.org/TR/2011/WD-html5-20110113/video.html#dom-navigator-canplaytype
  21598. *
  21599. * @param {string} type
  21600. * The mimetype to check
  21601. *
  21602. * @return {string}
  21603. * 'probably', 'maybe', or '' (empty string)
  21604. */
  21605. canPlayType(type) {
  21606. let can;
  21607. // Loop through each playback technology in the options order
  21608. for (let i = 0, j = this.options_.techOrder; i < j.length; i++) {
  21609. const techName = j[i];
  21610. let tech = Tech.getTech(techName);
  21611. // Support old behavior of techs being registered as components.
  21612. // Remove once that deprecated behavior is removed.
  21613. if (!tech) {
  21614. tech = Component.getComponent(techName);
  21615. }
  21616. // Check if the current tech is defined before continuing
  21617. if (!tech) {
  21618. log.error(`The "${techName}" tech is undefined. Skipped browser support check for that tech.`);
  21619. continue;
  21620. }
  21621. // Check if the browser supports this technology
  21622. if (tech.isSupported()) {
  21623. can = tech.canPlayType(type);
  21624. if (can) {
  21625. return can;
  21626. }
  21627. }
  21628. }
  21629. return '';
  21630. }
  21631. /**
  21632. * Select source based on tech-order or source-order
  21633. * Uses source-order selection if `options.sourceOrder` is truthy. Otherwise,
  21634. * defaults to tech-order selection
  21635. *
  21636. * @param {Array} sources
  21637. * The sources for a media asset
  21638. *
  21639. * @return {Object|boolean}
  21640. * Object of source and tech order or false
  21641. */
  21642. selectSource(sources) {
  21643. // Get only the techs specified in `techOrder` that exist and are supported by the
  21644. // current platform
  21645. const techs = this.options_.techOrder.map(techName => {
  21646. return [techName, Tech.getTech(techName)];
  21647. }).filter(([techName, tech]) => {
  21648. // Check if the current tech is defined before continuing
  21649. if (tech) {
  21650. // Check if the browser supports this technology
  21651. return tech.isSupported();
  21652. }
  21653. log.error(`The "${techName}" tech is undefined. Skipped browser support check for that tech.`);
  21654. return false;
  21655. });
  21656. // Iterate over each `innerArray` element once per `outerArray` element and execute
  21657. // `tester` with both. If `tester` returns a non-falsy value, exit early and return
  21658. // that value.
  21659. const findFirstPassingTechSourcePair = function (outerArray, innerArray, tester) {
  21660. let found;
  21661. outerArray.some(outerChoice => {
  21662. return innerArray.some(innerChoice => {
  21663. found = tester(outerChoice, innerChoice);
  21664. if (found) {
  21665. return true;
  21666. }
  21667. });
  21668. });
  21669. return found;
  21670. };
  21671. let foundSourceAndTech;
  21672. const flip = fn => (a, b) => fn(b, a);
  21673. const finder = ([techName, tech], source) => {
  21674. if (tech.canPlaySource(source, this.options_[techName.toLowerCase()])) {
  21675. return {
  21676. source,
  21677. tech: techName
  21678. };
  21679. }
  21680. };
  21681. // Depending on the truthiness of `options.sourceOrder`, we swap the order of techs and sources
  21682. // to select from them based on their priority.
  21683. if (this.options_.sourceOrder) {
  21684. // Source-first ordering
  21685. foundSourceAndTech = findFirstPassingTechSourcePair(sources, techs, flip(finder));
  21686. } else {
  21687. // Tech-first ordering
  21688. foundSourceAndTech = findFirstPassingTechSourcePair(techs, sources, finder);
  21689. }
  21690. return foundSourceAndTech || false;
  21691. }
  21692. /**
  21693. * Executes source setting and getting logic
  21694. *
  21695. * @param {Tech~SourceObject|Tech~SourceObject[]|string} [source]
  21696. * A SourceObject, an array of SourceObjects, or a string referencing
  21697. * a URL to a media source. It is _highly recommended_ that an object
  21698. * or array of objects is used here, so that source selection
  21699. * algorithms can take the `type` into account.
  21700. *
  21701. * If not provided, this method acts as a getter.
  21702. * @param {boolean} isRetry
  21703. * Indicates whether this is being called internally as a result of a retry
  21704. *
  21705. * @return {string|undefined}
  21706. * If the `source` argument is missing, returns the current source
  21707. * URL. Otherwise, returns nothing/undefined.
  21708. */
  21709. handleSrc_(source, isRetry) {
  21710. // getter usage
  21711. if (typeof source === 'undefined') {
  21712. return this.cache_.src || '';
  21713. }
  21714. // Reset retry behavior for new source
  21715. if (this.resetRetryOnError_) {
  21716. this.resetRetryOnError_();
  21717. }
  21718. // filter out invalid sources and turn our source into
  21719. // an array of source objects
  21720. const sources = filterSource(source);
  21721. // if a source was passed in then it is invalid because
  21722. // it was filtered to a zero length Array. So we have to
  21723. // show an error
  21724. if (!sources.length) {
  21725. this.setTimeout(function () {
  21726. this.error({
  21727. code: 4,
  21728. message: this.options_.notSupportedMessage
  21729. });
  21730. }, 0);
  21731. return;
  21732. }
  21733. // initial sources
  21734. this.changingSrc_ = true;
  21735. // Only update the cached source list if we are not retrying a new source after error,
  21736. // since in that case we want to include the failed source(s) in the cache
  21737. if (!isRetry) {
  21738. this.cache_.sources = sources;
  21739. }
  21740. this.updateSourceCaches_(sources[0]);
  21741. // middlewareSource is the source after it has been changed by middleware
  21742. setSource(this, sources[0], (middlewareSource, mws) => {
  21743. this.middleware_ = mws;
  21744. // since sourceSet is async we have to update the cache again after we select a source since
  21745. // the source that is selected could be out of order from the cache update above this callback.
  21746. if (!isRetry) {
  21747. this.cache_.sources = sources;
  21748. }
  21749. this.updateSourceCaches_(middlewareSource);
  21750. const err = this.src_(middlewareSource);
  21751. if (err) {
  21752. if (sources.length > 1) {
  21753. return this.handleSrc_(sources.slice(1));
  21754. }
  21755. this.changingSrc_ = false;
  21756. // We need to wrap this in a timeout to give folks a chance to add error event handlers
  21757. this.setTimeout(function () {
  21758. this.error({
  21759. code: 4,
  21760. message: this.options_.notSupportedMessage
  21761. });
  21762. }, 0);
  21763. // we could not find an appropriate tech, but let's still notify the delegate that this is it
  21764. // this needs a better comment about why this is needed
  21765. this.triggerReady();
  21766. return;
  21767. }
  21768. setTech(mws, this.tech_);
  21769. });
  21770. // Try another available source if this one fails before playback.
  21771. if (sources.length > 1) {
  21772. const retry = () => {
  21773. // Remove the error modal
  21774. this.error(null);
  21775. this.handleSrc_(sources.slice(1), true);
  21776. };
  21777. const stopListeningForErrors = () => {
  21778. this.off('error', retry);
  21779. };
  21780. this.one('error', retry);
  21781. this.one('playing', stopListeningForErrors);
  21782. this.resetRetryOnError_ = () => {
  21783. this.off('error', retry);
  21784. this.off('playing', stopListeningForErrors);
  21785. };
  21786. }
  21787. }
  21788. /**
  21789. * Get or set the video source.
  21790. *
  21791. * @param {Tech~SourceObject|Tech~SourceObject[]|string} [source]
  21792. * A SourceObject, an array of SourceObjects, or a string referencing
  21793. * a URL to a media source. It is _highly recommended_ that an object
  21794. * or array of objects is used here, so that source selection
  21795. * algorithms can take the `type` into account.
  21796. *
  21797. * If not provided, this method acts as a getter.
  21798. *
  21799. * @return {string|undefined}
  21800. * If the `source` argument is missing, returns the current source
  21801. * URL. Otherwise, returns nothing/undefined.
  21802. */
  21803. src(source) {
  21804. return this.handleSrc_(source, false);
  21805. }
  21806. /**
  21807. * Set the source object on the tech, returns a boolean that indicates whether
  21808. * there is a tech that can play the source or not
  21809. *
  21810. * @param {Tech~SourceObject} source
  21811. * The source object to set on the Tech
  21812. *
  21813. * @return {boolean}
  21814. * - True if there is no Tech to playback this source
  21815. * - False otherwise
  21816. *
  21817. * @private
  21818. */
  21819. src_(source) {
  21820. const sourceTech = this.selectSource([source]);
  21821. if (!sourceTech) {
  21822. return true;
  21823. }
  21824. if (!titleCaseEquals(sourceTech.tech, this.techName_)) {
  21825. this.changingSrc_ = true;
  21826. // load this technology with the chosen source
  21827. this.loadTech_(sourceTech.tech, sourceTech.source);
  21828. this.tech_.ready(() => {
  21829. this.changingSrc_ = false;
  21830. });
  21831. return false;
  21832. }
  21833. // wait until the tech is ready to set the source
  21834. // and set it synchronously if possible (#2326)
  21835. this.ready(function () {
  21836. // The setSource tech method was added with source handlers
  21837. // so older techs won't support it
  21838. // We need to check the direct prototype for the case where subclasses
  21839. // of the tech do not support source handlers
  21840. if (this.tech_.constructor.prototype.hasOwnProperty('setSource')) {
  21841. this.techCall_('setSource', source);
  21842. } else {
  21843. this.techCall_('src', source.src);
  21844. }
  21845. this.changingSrc_ = false;
  21846. }, true);
  21847. return false;
  21848. }
  21849. /**
  21850. * Begin loading the src data.
  21851. */
  21852. load() {
  21853. this.techCall_('load');
  21854. }
  21855. /**
  21856. * Reset the player. Loads the first tech in the techOrder,
  21857. * removes all the text tracks in the existing `tech`,
  21858. * and calls `reset` on the `tech`.
  21859. */
  21860. reset() {
  21861. if (this.paused()) {
  21862. this.doReset_();
  21863. } else {
  21864. const playPromise = this.play();
  21865. silencePromise(playPromise.then(() => this.doReset_()));
  21866. }
  21867. }
  21868. doReset_() {
  21869. if (this.tech_) {
  21870. this.tech_.clearTracks('text');
  21871. }
  21872. this.resetCache_();
  21873. this.poster('');
  21874. this.loadTech_(this.options_.techOrder[0], null);
  21875. this.techCall_('reset');
  21876. this.resetControlBarUI_();
  21877. if (isEvented(this)) {
  21878. this.trigger('playerreset');
  21879. }
  21880. }
  21881. /**
  21882. * Reset Control Bar's UI by calling sub-methods that reset
  21883. * all of Control Bar's components
  21884. */
  21885. resetControlBarUI_() {
  21886. this.resetProgressBar_();
  21887. this.resetPlaybackRate_();
  21888. this.resetVolumeBar_();
  21889. }
  21890. /**
  21891. * Reset tech's progress so progress bar is reset in the UI
  21892. */
  21893. resetProgressBar_() {
  21894. this.currentTime(0);
  21895. const {
  21896. currentTimeDisplay,
  21897. durationDisplay,
  21898. progressControl,
  21899. remainingTimeDisplay
  21900. } = this.controlBar || {};
  21901. const {
  21902. seekBar
  21903. } = progressControl || {};
  21904. if (currentTimeDisplay) {
  21905. currentTimeDisplay.updateContent();
  21906. }
  21907. if (durationDisplay) {
  21908. durationDisplay.updateContent();
  21909. }
  21910. if (remainingTimeDisplay) {
  21911. remainingTimeDisplay.updateContent();
  21912. }
  21913. if (seekBar) {
  21914. seekBar.update();
  21915. if (seekBar.loadProgressBar) {
  21916. seekBar.loadProgressBar.update();
  21917. }
  21918. }
  21919. }
  21920. /**
  21921. * Reset Playback ratio
  21922. */
  21923. resetPlaybackRate_() {
  21924. this.playbackRate(this.defaultPlaybackRate());
  21925. this.handleTechRateChange_();
  21926. }
  21927. /**
  21928. * Reset Volume bar
  21929. */
  21930. resetVolumeBar_() {
  21931. this.volume(1.0);
  21932. this.trigger('volumechange');
  21933. }
  21934. /**
  21935. * Returns all of the current source objects.
  21936. *
  21937. * @return {Tech~SourceObject[]}
  21938. * The current source objects
  21939. */
  21940. currentSources() {
  21941. const source = this.currentSource();
  21942. const sources = [];
  21943. // assume `{}` or `{ src }`
  21944. if (Object.keys(source).length !== 0) {
  21945. sources.push(source);
  21946. }
  21947. return this.cache_.sources || sources;
  21948. }
  21949. /**
  21950. * Returns the current source object.
  21951. *
  21952. * @return {Tech~SourceObject}
  21953. * The current source object
  21954. */
  21955. currentSource() {
  21956. return this.cache_.source || {};
  21957. }
  21958. /**
  21959. * Returns the fully qualified URL of the current source value e.g. http://mysite.com/video.mp4
  21960. * Can be used in conjunction with `currentType` to assist in rebuilding the current source object.
  21961. *
  21962. * @return {string}
  21963. * The current source
  21964. */
  21965. currentSrc() {
  21966. return this.currentSource() && this.currentSource().src || '';
  21967. }
  21968. /**
  21969. * Get the current source type e.g. video/mp4
  21970. * This can allow you rebuild the current source object so that you could load the same
  21971. * source and tech later
  21972. *
  21973. * @return {string}
  21974. * The source MIME type
  21975. */
  21976. currentType() {
  21977. return this.currentSource() && this.currentSource().type || '';
  21978. }
  21979. /**
  21980. * Get or set the preload attribute
  21981. *
  21982. * @param {boolean} [value]
  21983. * - true means that we should preload
  21984. * - false means that we should not preload
  21985. *
  21986. * @return {string}
  21987. * The preload attribute value when getting
  21988. */
  21989. preload(value) {
  21990. if (value !== undefined) {
  21991. this.techCall_('setPreload', value);
  21992. this.options_.preload = value;
  21993. return;
  21994. }
  21995. return this.techGet_('preload');
  21996. }
  21997. /**
  21998. * Get or set the autoplay option. When this is a boolean it will
  21999. * modify the attribute on the tech. When this is a string the attribute on
  22000. * the tech will be removed and `Player` will handle autoplay on loadstarts.
  22001. *
  22002. * @param {boolean|string} [value]
  22003. * - true: autoplay using the browser behavior
  22004. * - false: do not autoplay
  22005. * - 'play': call play() on every loadstart
  22006. * - 'muted': call muted() then play() on every loadstart
  22007. * - 'any': call play() on every loadstart. if that fails call muted() then play().
  22008. * - *: values other than those listed here will be set `autoplay` to true
  22009. *
  22010. * @return {boolean|string}
  22011. * The current value of autoplay when getting
  22012. */
  22013. autoplay(value) {
  22014. // getter usage
  22015. if (value === undefined) {
  22016. return this.options_.autoplay || false;
  22017. }
  22018. let techAutoplay;
  22019. // if the value is a valid string set it to that, or normalize `true` to 'play', if need be
  22020. if (typeof value === 'string' && /(any|play|muted)/.test(value) || value === true && this.options_.normalizeAutoplay) {
  22021. this.options_.autoplay = value;
  22022. this.manualAutoplay_(typeof value === 'string' ? value : 'play');
  22023. techAutoplay = false;
  22024. // any falsy value sets autoplay to false in the browser,
  22025. // lets do the same
  22026. } else if (!value) {
  22027. this.options_.autoplay = false;
  22028. // any other value (ie truthy) sets autoplay to true
  22029. } else {
  22030. this.options_.autoplay = true;
  22031. }
  22032. techAutoplay = typeof techAutoplay === 'undefined' ? this.options_.autoplay : techAutoplay;
  22033. // if we don't have a tech then we do not queue up
  22034. // a setAutoplay call on tech ready. We do this because the
  22035. // autoplay option will be passed in the constructor and we
  22036. // do not need to set it twice
  22037. if (this.tech_) {
  22038. this.techCall_('setAutoplay', techAutoplay);
  22039. }
  22040. }
  22041. /**
  22042. * Set or unset the playsinline attribute.
  22043. * Playsinline tells the browser that non-fullscreen playback is preferred.
  22044. *
  22045. * @param {boolean} [value]
  22046. * - true means that we should try to play inline by default
  22047. * - false means that we should use the browser's default playback mode,
  22048. * which in most cases is inline. iOS Safari is a notable exception
  22049. * and plays fullscreen by default.
  22050. *
  22051. * @return {string|Player}
  22052. * - the current value of playsinline
  22053. * - the player when setting
  22054. *
  22055. * @see [Spec]{@link https://html.spec.whatwg.org/#attr-video-playsinline}
  22056. */
  22057. playsinline(value) {
  22058. if (value !== undefined) {
  22059. this.techCall_('setPlaysinline', value);
  22060. this.options_.playsinline = value;
  22061. return this;
  22062. }
  22063. return this.techGet_('playsinline');
  22064. }
  22065. /**
  22066. * Get or set the loop attribute on the video element.
  22067. *
  22068. * @param {boolean} [value]
  22069. * - true means that we should loop the video
  22070. * - false means that we should not loop the video
  22071. *
  22072. * @return {boolean}
  22073. * The current value of loop when getting
  22074. */
  22075. loop(value) {
  22076. if (value !== undefined) {
  22077. this.techCall_('setLoop', value);
  22078. this.options_.loop = value;
  22079. return;
  22080. }
  22081. return this.techGet_('loop');
  22082. }
  22083. /**
  22084. * Get or set the poster image source url
  22085. *
  22086. * @fires Player#posterchange
  22087. *
  22088. * @param {string} [src]
  22089. * Poster image source URL
  22090. *
  22091. * @return {string}
  22092. * The current value of poster when getting
  22093. */
  22094. poster(src) {
  22095. if (src === undefined) {
  22096. return this.poster_;
  22097. }
  22098. // The correct way to remove a poster is to set as an empty string
  22099. // other falsey values will throw errors
  22100. if (!src) {
  22101. src = '';
  22102. }
  22103. if (src === this.poster_) {
  22104. return;
  22105. }
  22106. // update the internal poster variable
  22107. this.poster_ = src;
  22108. // update the tech's poster
  22109. this.techCall_('setPoster', src);
  22110. this.isPosterFromTech_ = false;
  22111. // alert components that the poster has been set
  22112. /**
  22113. * This event fires when the poster image is changed on the player.
  22114. *
  22115. * @event Player#posterchange
  22116. * @type {Event}
  22117. */
  22118. this.trigger('posterchange');
  22119. }
  22120. /**
  22121. * Some techs (e.g. YouTube) can provide a poster source in an
  22122. * asynchronous way. We want the poster component to use this
  22123. * poster source so that it covers up the tech's controls.
  22124. * (YouTube's play button). However we only want to use this
  22125. * source if the player user hasn't set a poster through
  22126. * the normal APIs.
  22127. *
  22128. * @fires Player#posterchange
  22129. * @listens Tech#posterchange
  22130. * @private
  22131. */
  22132. handleTechPosterChange_() {
  22133. if ((!this.poster_ || this.options_.techCanOverridePoster) && this.tech_ && this.tech_.poster) {
  22134. const newPoster = this.tech_.poster() || '';
  22135. if (newPoster !== this.poster_) {
  22136. this.poster_ = newPoster;
  22137. this.isPosterFromTech_ = true;
  22138. // Let components know the poster has changed
  22139. this.trigger('posterchange');
  22140. }
  22141. }
  22142. }
  22143. /**
  22144. * Get or set whether or not the controls are showing.
  22145. *
  22146. * @fires Player#controlsenabled
  22147. *
  22148. * @param {boolean} [bool]
  22149. * - true to turn controls on
  22150. * - false to turn controls off
  22151. *
  22152. * @return {boolean}
  22153. * The current value of controls when getting
  22154. */
  22155. controls(bool) {
  22156. if (bool === undefined) {
  22157. return !!this.controls_;
  22158. }
  22159. bool = !!bool;
  22160. // Don't trigger a change event unless it actually changed
  22161. if (this.controls_ === bool) {
  22162. return;
  22163. }
  22164. this.controls_ = bool;
  22165. if (this.usingNativeControls()) {
  22166. this.techCall_('setControls', bool);
  22167. }
  22168. if (this.controls_) {
  22169. this.removeClass('vjs-controls-disabled');
  22170. this.addClass('vjs-controls-enabled');
  22171. /**
  22172. * @event Player#controlsenabled
  22173. * @type {Event}
  22174. */
  22175. this.trigger('controlsenabled');
  22176. if (!this.usingNativeControls()) {
  22177. this.addTechControlsListeners_();
  22178. }
  22179. } else {
  22180. this.removeClass('vjs-controls-enabled');
  22181. this.addClass('vjs-controls-disabled');
  22182. /**
  22183. * @event Player#controlsdisabled
  22184. * @type {Event}
  22185. */
  22186. this.trigger('controlsdisabled');
  22187. if (!this.usingNativeControls()) {
  22188. this.removeTechControlsListeners_();
  22189. }
  22190. }
  22191. }
  22192. /**
  22193. * Toggle native controls on/off. Native controls are the controls built into
  22194. * devices (e.g. default iPhone controls) or other techs
  22195. * (e.g. Vimeo Controls)
  22196. * **This should only be set by the current tech, because only the tech knows
  22197. * if it can support native controls**
  22198. *
  22199. * @fires Player#usingnativecontrols
  22200. * @fires Player#usingcustomcontrols
  22201. *
  22202. * @param {boolean} [bool]
  22203. * - true to turn native controls on
  22204. * - false to turn native controls off
  22205. *
  22206. * @return {boolean}
  22207. * The current value of native controls when getting
  22208. */
  22209. usingNativeControls(bool) {
  22210. if (bool === undefined) {
  22211. return !!this.usingNativeControls_;
  22212. }
  22213. bool = !!bool;
  22214. // Don't trigger a change event unless it actually changed
  22215. if (this.usingNativeControls_ === bool) {
  22216. return;
  22217. }
  22218. this.usingNativeControls_ = bool;
  22219. if (this.usingNativeControls_) {
  22220. this.addClass('vjs-using-native-controls');
  22221. /**
  22222. * player is using the native device controls
  22223. *
  22224. * @event Player#usingnativecontrols
  22225. * @type {Event}
  22226. */
  22227. this.trigger('usingnativecontrols');
  22228. } else {
  22229. this.removeClass('vjs-using-native-controls');
  22230. /**
  22231. * player is using the custom HTML controls
  22232. *
  22233. * @event Player#usingcustomcontrols
  22234. * @type {Event}
  22235. */
  22236. this.trigger('usingcustomcontrols');
  22237. }
  22238. }
  22239. /**
  22240. * Set or get the current MediaError
  22241. *
  22242. * @fires Player#error
  22243. *
  22244. * @param {MediaError|string|number} [err]
  22245. * A MediaError or a string/number to be turned
  22246. * into a MediaError
  22247. *
  22248. * @return {MediaError|null}
  22249. * The current MediaError when getting (or null)
  22250. */
  22251. error(err) {
  22252. if (err === undefined) {
  22253. return this.error_ || null;
  22254. }
  22255. // allow hooks to modify error object
  22256. hooks('beforeerror').forEach(hookFunction => {
  22257. const newErr = hookFunction(this, err);
  22258. if (!(isObject(newErr) && !Array.isArray(newErr) || typeof newErr === 'string' || typeof newErr === 'number' || newErr === null)) {
  22259. this.log.error('please return a value that MediaError expects in beforeerror hooks');
  22260. return;
  22261. }
  22262. err = newErr;
  22263. });
  22264. // Suppress the first error message for no compatible source until
  22265. // user interaction
  22266. if (this.options_.suppressNotSupportedError && err && err.code === 4) {
  22267. const triggerSuppressedError = function () {
  22268. this.error(err);
  22269. };
  22270. this.options_.suppressNotSupportedError = false;
  22271. this.any(['click', 'touchstart'], triggerSuppressedError);
  22272. this.one('loadstart', function () {
  22273. this.off(['click', 'touchstart'], triggerSuppressedError);
  22274. });
  22275. return;
  22276. }
  22277. // restoring to default
  22278. if (err === null) {
  22279. this.error_ = err;
  22280. this.removeClass('vjs-error');
  22281. if (this.errorDisplay) {
  22282. this.errorDisplay.close();
  22283. }
  22284. return;
  22285. }
  22286. this.error_ = new MediaError(err);
  22287. // add the vjs-error classname to the player
  22288. this.addClass('vjs-error');
  22289. // log the name of the error type and any message
  22290. // IE11 logs "[object object]" and required you to expand message to see error object
  22291. log.error(`(CODE:${this.error_.code} ${MediaError.errorTypes[this.error_.code]})`, this.error_.message, this.error_);
  22292. /**
  22293. * @event Player#error
  22294. * @type {Event}
  22295. */
  22296. this.trigger('error');
  22297. // notify hooks of the per player error
  22298. hooks('error').forEach(hookFunction => hookFunction(this, this.error_));
  22299. return;
  22300. }
  22301. /**
  22302. * Report user activity
  22303. *
  22304. * @param {Object} event
  22305. * Event object
  22306. */
  22307. reportUserActivity(event) {
  22308. this.userActivity_ = true;
  22309. }
  22310. /**
  22311. * Get/set if user is active
  22312. *
  22313. * @fires Player#useractive
  22314. * @fires Player#userinactive
  22315. *
  22316. * @param {boolean} [bool]
  22317. * - true if the user is active
  22318. * - false if the user is inactive
  22319. *
  22320. * @return {boolean}
  22321. * The current value of userActive when getting
  22322. */
  22323. userActive(bool) {
  22324. if (bool === undefined) {
  22325. return this.userActive_;
  22326. }
  22327. bool = !!bool;
  22328. if (bool === this.userActive_) {
  22329. return;
  22330. }
  22331. this.userActive_ = bool;
  22332. if (this.userActive_) {
  22333. this.userActivity_ = true;
  22334. this.removeClass('vjs-user-inactive');
  22335. this.addClass('vjs-user-active');
  22336. /**
  22337. * @event Player#useractive
  22338. * @type {Event}
  22339. */
  22340. this.trigger('useractive');
  22341. return;
  22342. }
  22343. // Chrome/Safari/IE have bugs where when you change the cursor it can
  22344. // trigger a mousemove event. This causes an issue when you're hiding
  22345. // the cursor when the user is inactive, and a mousemove signals user
  22346. // activity. Making it impossible to go into inactive mode. Specifically
  22347. // this happens in fullscreen when we really need to hide the cursor.
  22348. //
  22349. // When this gets resolved in ALL browsers it can be removed
  22350. // https://code.google.com/p/chromium/issues/detail?id=103041
  22351. if (this.tech_) {
  22352. this.tech_.one('mousemove', function (e) {
  22353. e.stopPropagation();
  22354. e.preventDefault();
  22355. });
  22356. }
  22357. this.userActivity_ = false;
  22358. this.removeClass('vjs-user-active');
  22359. this.addClass('vjs-user-inactive');
  22360. /**
  22361. * @event Player#userinactive
  22362. * @type {Event}
  22363. */
  22364. this.trigger('userinactive');
  22365. }
  22366. /**
  22367. * Listen for user activity based on timeout value
  22368. *
  22369. * @private
  22370. */
  22371. listenForUserActivity_() {
  22372. let mouseInProgress;
  22373. let lastMoveX;
  22374. let lastMoveY;
  22375. const handleActivity = bind_(this, this.reportUserActivity);
  22376. const handleMouseMove = function (e) {
  22377. // #1068 - Prevent mousemove spamming
  22378. // Chrome Bug: https://code.google.com/p/chromium/issues/detail?id=366970
  22379. if (e.screenX !== lastMoveX || e.screenY !== lastMoveY) {
  22380. lastMoveX = e.screenX;
  22381. lastMoveY = e.screenY;
  22382. handleActivity();
  22383. }
  22384. };
  22385. const handleMouseDown = function () {
  22386. handleActivity();
  22387. // For as long as the they are touching the device or have their mouse down,
  22388. // we consider them active even if they're not moving their finger or mouse.
  22389. // So we want to continue to update that they are active
  22390. this.clearInterval(mouseInProgress);
  22391. // Setting userActivity=true now and setting the interval to the same time
  22392. // as the activityCheck interval (250) should ensure we never miss the
  22393. // next activityCheck
  22394. mouseInProgress = this.setInterval(handleActivity, 250);
  22395. };
  22396. const handleMouseUpAndMouseLeave = function (event) {
  22397. handleActivity();
  22398. // Stop the interval that maintains activity if the mouse/touch is down
  22399. this.clearInterval(mouseInProgress);
  22400. };
  22401. // Any mouse movement will be considered user activity
  22402. this.on('mousedown', handleMouseDown);
  22403. this.on('mousemove', handleMouseMove);
  22404. this.on('mouseup', handleMouseUpAndMouseLeave);
  22405. this.on('mouseleave', handleMouseUpAndMouseLeave);
  22406. const controlBar = this.getChild('controlBar');
  22407. // Fixes bug on Android & iOS where when tapping progressBar (when control bar is displayed)
  22408. // controlBar would no longer be hidden by default timeout.
  22409. if (controlBar && !IS_IOS && !IS_ANDROID) {
  22410. controlBar.on('mouseenter', function (event) {
  22411. if (this.player().options_.inactivityTimeout !== 0) {
  22412. this.player().cache_.inactivityTimeout = this.player().options_.inactivityTimeout;
  22413. }
  22414. this.player().options_.inactivityTimeout = 0;
  22415. });
  22416. controlBar.on('mouseleave', function (event) {
  22417. this.player().options_.inactivityTimeout = this.player().cache_.inactivityTimeout;
  22418. });
  22419. }
  22420. // Listen for keyboard navigation
  22421. // Shouldn't need to use inProgress interval because of key repeat
  22422. this.on('keydown', handleActivity);
  22423. this.on('keyup', handleActivity);
  22424. // Run an interval every 250 milliseconds instead of stuffing everything into
  22425. // the mousemove/touchmove function itself, to prevent performance degradation.
  22426. // `this.reportUserActivity` simply sets this.userActivity_ to true, which
  22427. // then gets picked up by this loop
  22428. // http://ejohn.org/blog/learning-from-twitter/
  22429. let inactivityTimeout;
  22430. this.setInterval(function () {
  22431. // Check to see if mouse/touch activity has happened
  22432. if (!this.userActivity_) {
  22433. return;
  22434. }
  22435. // Reset the activity tracker
  22436. this.userActivity_ = false;
  22437. // If the user state was inactive, set the state to active
  22438. this.userActive(true);
  22439. // Clear any existing inactivity timeout to start the timer over
  22440. this.clearTimeout(inactivityTimeout);
  22441. const timeout = this.options_.inactivityTimeout;
  22442. if (timeout <= 0) {
  22443. return;
  22444. }
  22445. // In <timeout> milliseconds, if no more activity has occurred the
  22446. // user will be considered inactive
  22447. inactivityTimeout = this.setTimeout(function () {
  22448. // Protect against the case where the inactivityTimeout can trigger just
  22449. // before the next user activity is picked up by the activity check loop
  22450. // causing a flicker
  22451. if (!this.userActivity_) {
  22452. this.userActive(false);
  22453. }
  22454. }, timeout);
  22455. }, 250);
  22456. }
  22457. /**
  22458. * Gets or sets the current playback rate. A playback rate of
  22459. * 1.0 represents normal speed and 0.5 would indicate half-speed
  22460. * playback, for instance.
  22461. *
  22462. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-playbackrate
  22463. *
  22464. * @param {number} [rate]
  22465. * New playback rate to set.
  22466. *
  22467. * @return {number}
  22468. * The current playback rate when getting or 1.0
  22469. */
  22470. playbackRate(rate) {
  22471. if (rate !== undefined) {
  22472. // NOTE: this.cache_.lastPlaybackRate is set from the tech handler
  22473. // that is registered above
  22474. this.techCall_('setPlaybackRate', rate);
  22475. return;
  22476. }
  22477. if (this.tech_ && this.tech_.featuresPlaybackRate) {
  22478. return this.cache_.lastPlaybackRate || this.techGet_('playbackRate');
  22479. }
  22480. return 1.0;
  22481. }
  22482. /**
  22483. * Gets or sets the current default playback rate. A default playback rate of
  22484. * 1.0 represents normal speed and 0.5 would indicate half-speed playback, for instance.
  22485. * defaultPlaybackRate will only represent what the initial playbackRate of a video was, not
  22486. * not the current playbackRate.
  22487. *
  22488. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-defaultplaybackrate
  22489. *
  22490. * @param {number} [rate]
  22491. * New default playback rate to set.
  22492. *
  22493. * @return {number|Player}
  22494. * - The default playback rate when getting or 1.0
  22495. * - the player when setting
  22496. */
  22497. defaultPlaybackRate(rate) {
  22498. if (rate !== undefined) {
  22499. return this.techCall_('setDefaultPlaybackRate', rate);
  22500. }
  22501. if (this.tech_ && this.tech_.featuresPlaybackRate) {
  22502. return this.techGet_('defaultPlaybackRate');
  22503. }
  22504. return 1.0;
  22505. }
  22506. /**
  22507. * Gets or sets the audio flag
  22508. *
  22509. * @param {boolean} bool
  22510. * - true signals that this is an audio player
  22511. * - false signals that this is not an audio player
  22512. *
  22513. * @return {boolean}
  22514. * The current value of isAudio when getting
  22515. */
  22516. isAudio(bool) {
  22517. if (bool !== undefined) {
  22518. this.isAudio_ = !!bool;
  22519. return;
  22520. }
  22521. return !!this.isAudio_;
  22522. }
  22523. enableAudioOnlyUI_() {
  22524. // Update styling immediately to show the control bar so we can get its height
  22525. this.addClass('vjs-audio-only-mode');
  22526. const playerChildren = this.children();
  22527. const controlBar = this.getChild('ControlBar');
  22528. const controlBarHeight = controlBar && controlBar.currentHeight();
  22529. // Hide all player components except the control bar. Control bar components
  22530. // needed only for video are hidden with CSS
  22531. playerChildren.forEach(child => {
  22532. if (child === controlBar) {
  22533. return;
  22534. }
  22535. if (child.el_ && !child.hasClass('vjs-hidden')) {
  22536. child.hide();
  22537. this.audioOnlyCache_.hiddenChildren.push(child);
  22538. }
  22539. });
  22540. this.audioOnlyCache_.playerHeight = this.currentHeight();
  22541. // Set the player height the same as the control bar
  22542. this.height(controlBarHeight);
  22543. this.trigger('audioonlymodechange');
  22544. }
  22545. disableAudioOnlyUI_() {
  22546. this.removeClass('vjs-audio-only-mode');
  22547. // Show player components that were previously hidden
  22548. this.audioOnlyCache_.hiddenChildren.forEach(child => child.show());
  22549. // Reset player height
  22550. this.height(this.audioOnlyCache_.playerHeight);
  22551. this.trigger('audioonlymodechange');
  22552. }
  22553. /**
  22554. * Get the current audioOnlyMode state or set audioOnlyMode to true or false.
  22555. *
  22556. * Setting this to `true` will hide all player components except the control bar,
  22557. * as well as control bar components needed only for video.
  22558. *
  22559. * @param {boolean} [value]
  22560. * The value to set audioOnlyMode to.
  22561. *
  22562. * @return {Promise|boolean}
  22563. * A Promise is returned when setting the state, and a boolean when getting
  22564. * the present state
  22565. */
  22566. audioOnlyMode(value) {
  22567. if (typeof value !== 'boolean' || value === this.audioOnlyMode_) {
  22568. return this.audioOnlyMode_;
  22569. }
  22570. this.audioOnlyMode_ = value;
  22571. // Enable Audio Only Mode
  22572. if (value) {
  22573. const exitPromises = [];
  22574. // Fullscreen and PiP are not supported in audioOnlyMode, so exit if we need to.
  22575. if (this.isInPictureInPicture()) {
  22576. exitPromises.push(this.exitPictureInPicture());
  22577. }
  22578. if (this.isFullscreen()) {
  22579. exitPromises.push(this.exitFullscreen());
  22580. }
  22581. if (this.audioPosterMode()) {
  22582. exitPromises.push(this.audioPosterMode(false));
  22583. }
  22584. return Promise.all(exitPromises).then(() => this.enableAudioOnlyUI_());
  22585. }
  22586. // Disable Audio Only Mode
  22587. return Promise.resolve().then(() => this.disableAudioOnlyUI_());
  22588. }
  22589. enablePosterModeUI_() {
  22590. // Hide the video element and show the poster image to enable posterModeUI
  22591. const tech = this.tech_ && this.tech_;
  22592. tech.hide();
  22593. this.addClass('vjs-audio-poster-mode');
  22594. this.trigger('audiopostermodechange');
  22595. }
  22596. disablePosterModeUI_() {
  22597. // Show the video element and hide the poster image to disable posterModeUI
  22598. const tech = this.tech_ && this.tech_;
  22599. tech.show();
  22600. this.removeClass('vjs-audio-poster-mode');
  22601. this.trigger('audiopostermodechange');
  22602. }
  22603. /**
  22604. * Get the current audioPosterMode state or set audioPosterMode to true or false
  22605. *
  22606. * @param {boolean} [value]
  22607. * The value to set audioPosterMode to.
  22608. *
  22609. * @return {Promise|boolean}
  22610. * A Promise is returned when setting the state, and a boolean when getting
  22611. * the present state
  22612. */
  22613. audioPosterMode(value) {
  22614. if (typeof value !== 'boolean' || value === this.audioPosterMode_) {
  22615. return this.audioPosterMode_;
  22616. }
  22617. this.audioPosterMode_ = value;
  22618. if (value) {
  22619. if (this.audioOnlyMode()) {
  22620. const audioOnlyModePromise = this.audioOnlyMode(false);
  22621. return audioOnlyModePromise.then(() => {
  22622. // enable audio poster mode after audio only mode is disabled
  22623. this.enablePosterModeUI_();
  22624. });
  22625. }
  22626. return Promise.resolve().then(() => {
  22627. // enable audio poster mode
  22628. this.enablePosterModeUI_();
  22629. });
  22630. }
  22631. return Promise.resolve().then(() => {
  22632. // disable audio poster mode
  22633. this.disablePosterModeUI_();
  22634. });
  22635. }
  22636. /**
  22637. * A helper method for adding a {@link TextTrack} to our
  22638. * {@link TextTrackList}.
  22639. *
  22640. * In addition to the W3C settings we allow adding additional info through options.
  22641. *
  22642. * @see http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-addtexttrack
  22643. *
  22644. * @param {string} [kind]
  22645. * the kind of TextTrack you are adding
  22646. *
  22647. * @param {string} [label]
  22648. * the label to give the TextTrack label
  22649. *
  22650. * @param {string} [language]
  22651. * the language to set on the TextTrack
  22652. *
  22653. * @return {TextTrack|undefined}
  22654. * the TextTrack that was added or undefined
  22655. * if there is no tech
  22656. */
  22657. addTextTrack(kind, label, language) {
  22658. if (this.tech_) {
  22659. return this.tech_.addTextTrack(kind, label, language);
  22660. }
  22661. }
  22662. /**
  22663. * Create a remote {@link TextTrack} and an {@link HTMLTrackElement}.
  22664. *
  22665. * @param {Object} options
  22666. * Options to pass to {@link HTMLTrackElement} during creation. See
  22667. * {@link HTMLTrackElement} for object properties that you should use.
  22668. *
  22669. * @param {boolean} [manualCleanup=false] if set to true, the TextTrack will not be removed
  22670. * from the TextTrackList and HtmlTrackElementList
  22671. * after a source change
  22672. *
  22673. * @return { import('./tracks/html-track-element').default }
  22674. * the HTMLTrackElement that was created and added
  22675. * to the HtmlTrackElementList and the remote
  22676. * TextTrackList
  22677. *
  22678. */
  22679. addRemoteTextTrack(options, manualCleanup) {
  22680. if (this.tech_) {
  22681. return this.tech_.addRemoteTextTrack(options, manualCleanup);
  22682. }
  22683. }
  22684. /**
  22685. * Remove a remote {@link TextTrack} from the respective
  22686. * {@link TextTrackList} and {@link HtmlTrackElementList}.
  22687. *
  22688. * @param {Object} track
  22689. * Remote {@link TextTrack} to remove
  22690. *
  22691. * @return {undefined}
  22692. * does not return anything
  22693. */
  22694. removeRemoteTextTrack(obj = {}) {
  22695. let {
  22696. track
  22697. } = obj;
  22698. if (!track) {
  22699. track = obj;
  22700. }
  22701. // destructure the input into an object with a track argument, defaulting to arguments[0]
  22702. // default the whole argument to an empty object if nothing was passed in
  22703. if (this.tech_) {
  22704. return this.tech_.removeRemoteTextTrack(track);
  22705. }
  22706. }
  22707. /**
  22708. * Gets available media playback quality metrics as specified by the W3C's Media
  22709. * Playback Quality API.
  22710. *
  22711. * @see [Spec]{@link https://wicg.github.io/media-playback-quality}
  22712. *
  22713. * @return {Object|undefined}
  22714. * An object with supported media playback quality metrics or undefined if there
  22715. * is no tech or the tech does not support it.
  22716. */
  22717. getVideoPlaybackQuality() {
  22718. return this.techGet_('getVideoPlaybackQuality');
  22719. }
  22720. /**
  22721. * Get video width
  22722. *
  22723. * @return {number}
  22724. * current video width
  22725. */
  22726. videoWidth() {
  22727. return this.tech_ && this.tech_.videoWidth && this.tech_.videoWidth() || 0;
  22728. }
  22729. /**
  22730. * Get video height
  22731. *
  22732. * @return {number}
  22733. * current video height
  22734. */
  22735. videoHeight() {
  22736. return this.tech_ && this.tech_.videoHeight && this.tech_.videoHeight() || 0;
  22737. }
  22738. /**
  22739. * The player's language code.
  22740. *
  22741. * Changing the language will trigger
  22742. * [languagechange]{@link Player#event:languagechange}
  22743. * which Components can use to update control text.
  22744. * ClickableComponent will update its control text by default on
  22745. * [languagechange]{@link Player#event:languagechange}.
  22746. *
  22747. * @fires Player#languagechange
  22748. *
  22749. * @param {string} [code]
  22750. * the language code to set the player to
  22751. *
  22752. * @return {string}
  22753. * The current language code when getting
  22754. */
  22755. language(code) {
  22756. if (code === undefined) {
  22757. return this.language_;
  22758. }
  22759. if (this.language_ !== String(code).toLowerCase()) {
  22760. this.language_ = String(code).toLowerCase();
  22761. // during first init, it's possible some things won't be evented
  22762. if (isEvented(this)) {
  22763. /**
  22764. * fires when the player language change
  22765. *
  22766. * @event Player#languagechange
  22767. * @type {Event}
  22768. */
  22769. this.trigger('languagechange');
  22770. }
  22771. }
  22772. }
  22773. /**
  22774. * Get the player's language dictionary
  22775. * Merge every time, because a newly added plugin might call videojs.addLanguage() at any time
  22776. * Languages specified directly in the player options have precedence
  22777. *
  22778. * @return {Array}
  22779. * An array of of supported languages
  22780. */
  22781. languages() {
  22782. return merge(Player.prototype.options_.languages, this.languages_);
  22783. }
  22784. /**
  22785. * returns a JavaScript object representing the current track
  22786. * information. **DOES not return it as JSON**
  22787. *
  22788. * @return {Object}
  22789. * Object representing the current of track info
  22790. */
  22791. toJSON() {
  22792. const options = merge(this.options_);
  22793. const tracks = options.tracks;
  22794. options.tracks = [];
  22795. for (let i = 0; i < tracks.length; i++) {
  22796. let track = tracks[i];
  22797. // deep merge tracks and null out player so no circular references
  22798. track = merge(track);
  22799. track.player = undefined;
  22800. options.tracks[i] = track;
  22801. }
  22802. return options;
  22803. }
  22804. /**
  22805. * Creates a simple modal dialog (an instance of the {@link ModalDialog}
  22806. * component) that immediately overlays the player with arbitrary
  22807. * content and removes itself when closed.
  22808. *
  22809. * @param {string|Function|Element|Array|null} content
  22810. * Same as {@link ModalDialog#content}'s param of the same name.
  22811. * The most straight-forward usage is to provide a string or DOM
  22812. * element.
  22813. *
  22814. * @param {Object} [options]
  22815. * Extra options which will be passed on to the {@link ModalDialog}.
  22816. *
  22817. * @return {ModalDialog}
  22818. * the {@link ModalDialog} that was created
  22819. */
  22820. createModal(content, options) {
  22821. options = options || {};
  22822. options.content = content || '';
  22823. const modal = new ModalDialog(this, options);
  22824. this.addChild(modal);
  22825. modal.on('dispose', () => {
  22826. this.removeChild(modal);
  22827. });
  22828. modal.open();
  22829. return modal;
  22830. }
  22831. /**
  22832. * Change breakpoint classes when the player resizes.
  22833. *
  22834. * @private
  22835. */
  22836. updateCurrentBreakpoint_() {
  22837. if (!this.responsive()) {
  22838. return;
  22839. }
  22840. const currentBreakpoint = this.currentBreakpoint();
  22841. const currentWidth = this.currentWidth();
  22842. for (let i = 0; i < BREAKPOINT_ORDER.length; i++) {
  22843. const candidateBreakpoint = BREAKPOINT_ORDER[i];
  22844. const maxWidth = this.breakpoints_[candidateBreakpoint];
  22845. if (currentWidth <= maxWidth) {
  22846. // The current breakpoint did not change, nothing to do.
  22847. if (currentBreakpoint === candidateBreakpoint) {
  22848. return;
  22849. }
  22850. // Only remove a class if there is a current breakpoint.
  22851. if (currentBreakpoint) {
  22852. this.removeClass(BREAKPOINT_CLASSES[currentBreakpoint]);
  22853. }
  22854. this.addClass(BREAKPOINT_CLASSES[candidateBreakpoint]);
  22855. this.breakpoint_ = candidateBreakpoint;
  22856. break;
  22857. }
  22858. }
  22859. }
  22860. /**
  22861. * Removes the current breakpoint.
  22862. *
  22863. * @private
  22864. */
  22865. removeCurrentBreakpoint_() {
  22866. const className = this.currentBreakpointClass();
  22867. this.breakpoint_ = '';
  22868. if (className) {
  22869. this.removeClass(className);
  22870. }
  22871. }
  22872. /**
  22873. * Get or set breakpoints on the player.
  22874. *
  22875. * Calling this method with an object or `true` will remove any previous
  22876. * custom breakpoints and start from the defaults again.
  22877. *
  22878. * @param {Object|boolean} [breakpoints]
  22879. * If an object is given, it can be used to provide custom
  22880. * breakpoints. If `true` is given, will set default breakpoints.
  22881. * If this argument is not given, will simply return the current
  22882. * breakpoints.
  22883. *
  22884. * @param {number} [breakpoints.tiny]
  22885. * The maximum width for the "vjs-layout-tiny" class.
  22886. *
  22887. * @param {number} [breakpoints.xsmall]
  22888. * The maximum width for the "vjs-layout-x-small" class.
  22889. *
  22890. * @param {number} [breakpoints.small]
  22891. * The maximum width for the "vjs-layout-small" class.
  22892. *
  22893. * @param {number} [breakpoints.medium]
  22894. * The maximum width for the "vjs-layout-medium" class.
  22895. *
  22896. * @param {number} [breakpoints.large]
  22897. * The maximum width for the "vjs-layout-large" class.
  22898. *
  22899. * @param {number} [breakpoints.xlarge]
  22900. * The maximum width for the "vjs-layout-x-large" class.
  22901. *
  22902. * @param {number} [breakpoints.huge]
  22903. * The maximum width for the "vjs-layout-huge" class.
  22904. *
  22905. * @return {Object}
  22906. * An object mapping breakpoint names to maximum width values.
  22907. */
  22908. breakpoints(breakpoints) {
  22909. // Used as a getter.
  22910. if (breakpoints === undefined) {
  22911. return Object.assign(this.breakpoints_);
  22912. }
  22913. this.breakpoint_ = '';
  22914. this.breakpoints_ = Object.assign({}, DEFAULT_BREAKPOINTS, breakpoints);
  22915. // When breakpoint definitions change, we need to update the currently
  22916. // selected breakpoint.
  22917. this.updateCurrentBreakpoint_();
  22918. // Clone the breakpoints before returning.
  22919. return Object.assign(this.breakpoints_);
  22920. }
  22921. /**
  22922. * Get or set a flag indicating whether or not this player should adjust
  22923. * its UI based on its dimensions.
  22924. *
  22925. * @param {boolean} value
  22926. * Should be `true` if the player should adjust its UI based on its
  22927. * dimensions; otherwise, should be `false`.
  22928. *
  22929. * @return {boolean}
  22930. * Will be `true` if this player should adjust its UI based on its
  22931. * dimensions; otherwise, will be `false`.
  22932. */
  22933. responsive(value) {
  22934. // Used as a getter.
  22935. if (value === undefined) {
  22936. return this.responsive_;
  22937. }
  22938. value = Boolean(value);
  22939. const current = this.responsive_;
  22940. // Nothing changed.
  22941. if (value === current) {
  22942. return;
  22943. }
  22944. // The value actually changed, set it.
  22945. this.responsive_ = value;
  22946. // Start listening for breakpoints and set the initial breakpoint if the
  22947. // player is now responsive.
  22948. if (value) {
  22949. this.on('playerresize', this.boundUpdateCurrentBreakpoint_);
  22950. this.updateCurrentBreakpoint_();
  22951. // Stop listening for breakpoints if the player is no longer responsive.
  22952. } else {
  22953. this.off('playerresize', this.boundUpdateCurrentBreakpoint_);
  22954. this.removeCurrentBreakpoint_();
  22955. }
  22956. return value;
  22957. }
  22958. /**
  22959. * Get current breakpoint name, if any.
  22960. *
  22961. * @return {string}
  22962. * If there is currently a breakpoint set, returns a the key from the
  22963. * breakpoints object matching it. Otherwise, returns an empty string.
  22964. */
  22965. currentBreakpoint() {
  22966. return this.breakpoint_;
  22967. }
  22968. /**
  22969. * Get the current breakpoint class name.
  22970. *
  22971. * @return {string}
  22972. * The matching class name (e.g. `"vjs-layout-tiny"` or
  22973. * `"vjs-layout-large"`) for the current breakpoint. Empty string if
  22974. * there is no current breakpoint.
  22975. */
  22976. currentBreakpointClass() {
  22977. return BREAKPOINT_CLASSES[this.breakpoint_] || '';
  22978. }
  22979. /**
  22980. * An object that describes a single piece of media.
  22981. *
  22982. * Properties that are not part of this type description will be retained; so,
  22983. * this can be viewed as a generic metadata storage mechanism as well.
  22984. *
  22985. * @see {@link https://wicg.github.io/mediasession/#the-mediametadata-interface}
  22986. * @typedef {Object} Player~MediaObject
  22987. *
  22988. * @property {string} [album]
  22989. * Unused, except if this object is passed to the `MediaSession`
  22990. * API.
  22991. *
  22992. * @property {string} [artist]
  22993. * Unused, except if this object is passed to the `MediaSession`
  22994. * API.
  22995. *
  22996. * @property {Object[]} [artwork]
  22997. * Unused, except if this object is passed to the `MediaSession`
  22998. * API. If not specified, will be populated via the `poster`, if
  22999. * available.
  23000. *
  23001. * @property {string} [poster]
  23002. * URL to an image that will display before playback.
  23003. *
  23004. * @property {Tech~SourceObject|Tech~SourceObject[]|string} [src]
  23005. * A single source object, an array of source objects, or a string
  23006. * referencing a URL to a media source. It is _highly recommended_
  23007. * that an object or array of objects is used here, so that source
  23008. * selection algorithms can take the `type` into account.
  23009. *
  23010. * @property {string} [title]
  23011. * Unused, except if this object is passed to the `MediaSession`
  23012. * API.
  23013. *
  23014. * @property {Object[]} [textTracks]
  23015. * An array of objects to be used to create text tracks, following
  23016. * the {@link https://www.w3.org/TR/html50/embedded-content-0.html#the-track-element|native track element format}.
  23017. * For ease of removal, these will be created as "remote" text
  23018. * tracks and set to automatically clean up on source changes.
  23019. *
  23020. * These objects may have properties like `src`, `kind`, `label`,
  23021. * and `language`, see {@link Tech#createRemoteTextTrack}.
  23022. */
  23023. /**
  23024. * Populate the player using a {@link Player~MediaObject|MediaObject}.
  23025. *
  23026. * @param {Player~MediaObject} media
  23027. * A media object.
  23028. *
  23029. * @param {Function} ready
  23030. * A callback to be called when the player is ready.
  23031. */
  23032. loadMedia(media, ready) {
  23033. if (!media || typeof media !== 'object') {
  23034. return;
  23035. }
  23036. this.reset();
  23037. // Clone the media object so it cannot be mutated from outside.
  23038. this.cache_.media = merge(media);
  23039. const {
  23040. artist,
  23041. artwork,
  23042. description,
  23043. poster,
  23044. src,
  23045. textTracks,
  23046. title
  23047. } = this.cache_.media;
  23048. // If `artwork` is not given, create it using `poster`.
  23049. if (!artwork && poster) {
  23050. this.cache_.media.artwork = [{
  23051. src: poster,
  23052. type: getMimetype(poster)
  23053. }];
  23054. }
  23055. if (src) {
  23056. this.src(src);
  23057. }
  23058. if (poster) {
  23059. this.poster(poster);
  23060. }
  23061. if (Array.isArray(textTracks)) {
  23062. textTracks.forEach(tt => this.addRemoteTextTrack(tt, false));
  23063. }
  23064. if (this.titleBar) {
  23065. this.titleBar.update({
  23066. title,
  23067. description: description || artist || ''
  23068. });
  23069. }
  23070. this.ready(ready);
  23071. }
  23072. /**
  23073. * Get a clone of the current {@link Player~MediaObject} for this player.
  23074. *
  23075. * If the `loadMedia` method has not been used, will attempt to return a
  23076. * {@link Player~MediaObject} based on the current state of the player.
  23077. *
  23078. * @return {Player~MediaObject}
  23079. */
  23080. getMedia() {
  23081. if (!this.cache_.media) {
  23082. const poster = this.poster();
  23083. const src = this.currentSources();
  23084. const textTracks = Array.prototype.map.call(this.remoteTextTracks(), tt => ({
  23085. kind: tt.kind,
  23086. label: tt.label,
  23087. language: tt.language,
  23088. src: tt.src
  23089. }));
  23090. const media = {
  23091. src,
  23092. textTracks
  23093. };
  23094. if (poster) {
  23095. media.poster = poster;
  23096. media.artwork = [{
  23097. src: media.poster,
  23098. type: getMimetype(media.poster)
  23099. }];
  23100. }
  23101. return media;
  23102. }
  23103. return merge(this.cache_.media);
  23104. }
  23105. /**
  23106. * Gets tag settings
  23107. *
  23108. * @param {Element} tag
  23109. * The player tag
  23110. *
  23111. * @return {Object}
  23112. * An object containing all of the settings
  23113. * for a player tag
  23114. */
  23115. static getTagSettings(tag) {
  23116. const baseOptions = {
  23117. sources: [],
  23118. tracks: []
  23119. };
  23120. const tagOptions = getAttributes(tag);
  23121. const dataSetup = tagOptions['data-setup'];
  23122. if (hasClass(tag, 'vjs-fill')) {
  23123. tagOptions.fill = true;
  23124. }
  23125. if (hasClass(tag, 'vjs-fluid')) {
  23126. tagOptions.fluid = true;
  23127. }
  23128. // Check if data-setup attr exists.
  23129. if (dataSetup !== null) {
  23130. // Parse options JSON
  23131. // If empty string, make it a parsable json object.
  23132. const [err, data] = tuple(dataSetup || '{}');
  23133. if (err) {
  23134. log.error(err);
  23135. }
  23136. Object.assign(tagOptions, data);
  23137. }
  23138. Object.assign(baseOptions, tagOptions);
  23139. // Get tag children settings
  23140. if (tag.hasChildNodes()) {
  23141. const children = tag.childNodes;
  23142. for (let i = 0, j = children.length; i < j; i++) {
  23143. const child = children[i];
  23144. // Change case needed: http://ejohn.org/blog/nodename-case-sensitivity/
  23145. const childName = child.nodeName.toLowerCase();
  23146. if (childName === 'source') {
  23147. baseOptions.sources.push(getAttributes(child));
  23148. } else if (childName === 'track') {
  23149. baseOptions.tracks.push(getAttributes(child));
  23150. }
  23151. }
  23152. }
  23153. return baseOptions;
  23154. }
  23155. /**
  23156. * Set debug mode to enable/disable logs at info level.
  23157. *
  23158. * @param {boolean} enabled
  23159. * @fires Player#debugon
  23160. * @fires Player#debugoff
  23161. */
  23162. debug(enabled) {
  23163. if (enabled === undefined) {
  23164. return this.debugEnabled_;
  23165. }
  23166. if (enabled) {
  23167. this.trigger('debugon');
  23168. this.previousLogLevel_ = this.log.level;
  23169. this.log.level('debug');
  23170. this.debugEnabled_ = true;
  23171. } else {
  23172. this.trigger('debugoff');
  23173. this.log.level(this.previousLogLevel_);
  23174. this.previousLogLevel_ = undefined;
  23175. this.debugEnabled_ = false;
  23176. }
  23177. }
  23178. /**
  23179. * Set or get current playback rates.
  23180. * Takes an array and updates the playback rates menu with the new items.
  23181. * Pass in an empty array to hide the menu.
  23182. * Values other than arrays are ignored.
  23183. *
  23184. * @fires Player#playbackrateschange
  23185. * @param {number[]} newRates
  23186. * The new rates that the playback rates menu should update to.
  23187. * An empty array will hide the menu
  23188. * @return {number[]} When used as a getter will return the current playback rates
  23189. */
  23190. playbackRates(newRates) {
  23191. if (newRates === undefined) {
  23192. return this.cache_.playbackRates;
  23193. }
  23194. // ignore any value that isn't an array
  23195. if (!Array.isArray(newRates)) {
  23196. return;
  23197. }
  23198. // ignore any arrays that don't only contain numbers
  23199. if (!newRates.every(rate => typeof rate === 'number')) {
  23200. return;
  23201. }
  23202. this.cache_.playbackRates = newRates;
  23203. /**
  23204. * fires when the playback rates in a player are changed
  23205. *
  23206. * @event Player#playbackrateschange
  23207. * @type {Event}
  23208. */
  23209. this.trigger('playbackrateschange');
  23210. }
  23211. }
  23212. /**
  23213. * Get the {@link VideoTrackList}
  23214. *
  23215. * @link https://html.spec.whatwg.org/multipage/embedded-content.html#videotracklist
  23216. *
  23217. * @return {VideoTrackList}
  23218. * the current video track list
  23219. *
  23220. * @method Player.prototype.videoTracks
  23221. */
  23222. /**
  23223. * Get the {@link AudioTrackList}
  23224. *
  23225. * @link https://html.spec.whatwg.org/multipage/embedded-content.html#audiotracklist
  23226. *
  23227. * @return {AudioTrackList}
  23228. * the current audio track list
  23229. *
  23230. * @method Player.prototype.audioTracks
  23231. */
  23232. /**
  23233. * Get the {@link TextTrackList}
  23234. *
  23235. * @link http://www.w3.org/html/wg/drafts/html/master/embedded-content-0.html#dom-media-texttracks
  23236. *
  23237. * @return {TextTrackList}
  23238. * the current text track list
  23239. *
  23240. * @method Player.prototype.textTracks
  23241. */
  23242. /**
  23243. * Get the remote {@link TextTrackList}
  23244. *
  23245. * @return {TextTrackList}
  23246. * The current remote text track list
  23247. *
  23248. * @method Player.prototype.remoteTextTracks
  23249. */
  23250. /**
  23251. * Get the remote {@link HtmlTrackElementList} tracks.
  23252. *
  23253. * @return {HtmlTrackElementList}
  23254. * The current remote text track element list
  23255. *
  23256. * @method Player.prototype.remoteTextTrackEls
  23257. */
  23258. ALL.names.forEach(function (name) {
  23259. const props = ALL[name];
  23260. Player.prototype[props.getterName] = function () {
  23261. if (this.tech_) {
  23262. return this.tech_[props.getterName]();
  23263. }
  23264. // if we have not yet loadTech_, we create {video,audio,text}Tracks_
  23265. // these will be passed to the tech during loading
  23266. this[props.privateName] = this[props.privateName] || new props.ListClass();
  23267. return this[props.privateName];
  23268. };
  23269. });
  23270. /**
  23271. * Get or set the `Player`'s crossorigin option. For the HTML5 player, this
  23272. * sets the `crossOrigin` property on the `<video>` tag to control the CORS
  23273. * behavior.
  23274. *
  23275. * @see [Video Element Attributes]{@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video#attr-crossorigin}
  23276. *
  23277. * @param {string} [value]
  23278. * The value to set the `Player`'s crossorigin to. If an argument is
  23279. * given, must be one of `anonymous` or `use-credentials`.
  23280. *
  23281. * @return {string|undefined}
  23282. * - The current crossorigin value of the `Player` when getting.
  23283. * - undefined when setting
  23284. */
  23285. Player.prototype.crossorigin = Player.prototype.crossOrigin;
  23286. /**
  23287. * Global enumeration of players.
  23288. *
  23289. * The keys are the player IDs and the values are either the {@link Player}
  23290. * instance or `null` for disposed players.
  23291. *
  23292. * @type {Object}
  23293. */
  23294. Player.players = {};
  23295. const navigator = window.navigator;
  23296. /*
  23297. * Player instance options, surfaced using options
  23298. * options = Player.prototype.options_
  23299. * Make changes in options, not here.
  23300. *
  23301. * @type {Object}
  23302. * @private
  23303. */
  23304. Player.prototype.options_ = {
  23305. // Default order of fallback technology
  23306. techOrder: Tech.defaultTechOrder_,
  23307. html5: {},
  23308. // enable sourceset by default
  23309. enableSourceset: true,
  23310. // default inactivity timeout
  23311. inactivityTimeout: 2000,
  23312. // default playback rates
  23313. playbackRates: [],
  23314. // Add playback rate selection by adding rates
  23315. // 'playbackRates': [0.5, 1, 1.5, 2],
  23316. liveui: false,
  23317. // Included control sets
  23318. children: ['mediaLoader', 'posterImage', 'titleBar', 'textTrackDisplay', 'loadingSpinner', 'bigPlayButton', 'liveTracker', 'controlBar', 'errorDisplay', 'textTrackSettings', 'resizeManager'],
  23319. language: navigator && (navigator.languages && navigator.languages[0] || navigator.userLanguage || navigator.language) || 'en',
  23320. // locales and their language translations
  23321. languages: {},
  23322. // Default message to show when a video cannot be played.
  23323. notSupportedMessage: 'No compatible source was found for this media.',
  23324. normalizeAutoplay: false,
  23325. fullscreen: {
  23326. options: {
  23327. navigationUI: 'hide'
  23328. }
  23329. },
  23330. breakpoints: {},
  23331. responsive: false,
  23332. audioOnlyMode: false,
  23333. audioPosterMode: false
  23334. };
  23335. [
  23336. /**
  23337. * Returns whether or not the player is in the "ended" state.
  23338. *
  23339. * @return {Boolean} True if the player is in the ended state, false if not.
  23340. * @method Player#ended
  23341. */
  23342. 'ended',
  23343. /**
  23344. * Returns whether or not the player is in the "seeking" state.
  23345. *
  23346. * @return {Boolean} True if the player is in the seeking state, false if not.
  23347. * @method Player#seeking
  23348. */
  23349. 'seeking',
  23350. /**
  23351. * Returns the TimeRanges of the media that are currently available
  23352. * for seeking to.
  23353. *
  23354. * @return {TimeRanges} the seekable intervals of the media timeline
  23355. * @method Player#seekable
  23356. */
  23357. 'seekable',
  23358. /**
  23359. * Returns the current state of network activity for the element, from
  23360. * the codes in the list below.
  23361. * - NETWORK_EMPTY (numeric value 0)
  23362. * The element has not yet been initialised. All attributes are in
  23363. * their initial states.
  23364. * - NETWORK_IDLE (numeric value 1)
  23365. * The element's resource selection algorithm is active and has
  23366. * selected a resource, but it is not actually using the network at
  23367. * this time.
  23368. * - NETWORK_LOADING (numeric value 2)
  23369. * The user agent is actively trying to download data.
  23370. * - NETWORK_NO_SOURCE (numeric value 3)
  23371. * The element's resource selection algorithm is active, but it has
  23372. * not yet found a resource to use.
  23373. *
  23374. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#network-states
  23375. * @return {number} the current network activity state
  23376. * @method Player#networkState
  23377. */
  23378. 'networkState',
  23379. /**
  23380. * Returns a value that expresses the current state of the element
  23381. * with respect to rendering the current playback position, from the
  23382. * codes in the list below.
  23383. * - HAVE_NOTHING (numeric value 0)
  23384. * No information regarding the media resource is available.
  23385. * - HAVE_METADATA (numeric value 1)
  23386. * Enough of the resource has been obtained that the duration of the
  23387. * resource is available.
  23388. * - HAVE_CURRENT_DATA (numeric value 2)
  23389. * Data for the immediate current playback position is available.
  23390. * - HAVE_FUTURE_DATA (numeric value 3)
  23391. * Data for the immediate current playback position is available, as
  23392. * well as enough data for the user agent to advance the current
  23393. * playback position in the direction of playback.
  23394. * - HAVE_ENOUGH_DATA (numeric value 4)
  23395. * The user agent estimates that enough data is available for
  23396. * playback to proceed uninterrupted.
  23397. *
  23398. * @see https://html.spec.whatwg.org/multipage/embedded-content.html#dom-media-readystate
  23399. * @return {number} the current playback rendering state
  23400. * @method Player#readyState
  23401. */
  23402. 'readyState'].forEach(function (fn) {
  23403. Player.prototype[fn] = function () {
  23404. return this.techGet_(fn);
  23405. };
  23406. });
  23407. TECH_EVENTS_RETRIGGER.forEach(function (event) {
  23408. Player.prototype[`handleTech${toTitleCase(event)}_`] = function () {
  23409. return this.trigger(event);
  23410. };
  23411. });
  23412. /**
  23413. * Fired when the player has initial duration and dimension information
  23414. *
  23415. * @event Player#loadedmetadata
  23416. * @type {Event}
  23417. */
  23418. /**
  23419. * Fired when the player has downloaded data at the current playback position
  23420. *
  23421. * @event Player#loadeddata
  23422. * @type {Event}
  23423. */
  23424. /**
  23425. * Fired when the current playback position has changed *
  23426. * During playback this is fired every 15-250 milliseconds, depending on the
  23427. * playback technology in use.
  23428. *
  23429. * @event Player#timeupdate
  23430. * @type {Event}
  23431. */
  23432. /**
  23433. * Fired when the volume changes
  23434. *
  23435. * @event Player#volumechange
  23436. * @type {Event}
  23437. */
  23438. /**
  23439. * Reports whether or not a player has a plugin available.
  23440. *
  23441. * This does not report whether or not the plugin has ever been initialized
  23442. * on this player. For that, [usingPlugin]{@link Player#usingPlugin}.
  23443. *
  23444. * @method Player#hasPlugin
  23445. * @param {string} name
  23446. * The name of a plugin.
  23447. *
  23448. * @return {boolean}
  23449. * Whether or not this player has the requested plugin available.
  23450. */
  23451. /**
  23452. * Reports whether or not a player is using a plugin by name.
  23453. *
  23454. * For basic plugins, this only reports whether the plugin has _ever_ been
  23455. * initialized on this player.
  23456. *
  23457. * @method Player#usingPlugin
  23458. * @param {string} name
  23459. * The name of a plugin.
  23460. *
  23461. * @return {boolean}
  23462. * Whether or not this player is using the requested plugin.
  23463. */
  23464. Component.registerComponent('Player', Player);
  23465. /**
  23466. * @file plugin.js
  23467. */
  23468. /**
  23469. * The base plugin name.
  23470. *
  23471. * @private
  23472. * @constant
  23473. * @type {string}
  23474. */
  23475. const BASE_PLUGIN_NAME = 'plugin';
  23476. /**
  23477. * The key on which a player's active plugins cache is stored.
  23478. *
  23479. * @private
  23480. * @constant
  23481. * @type {string}
  23482. */
  23483. const PLUGIN_CACHE_KEY = 'activePlugins_';
  23484. /**
  23485. * Stores registered plugins in a private space.
  23486. *
  23487. * @private
  23488. * @type {Object}
  23489. */
  23490. const pluginStorage = {};
  23491. /**
  23492. * Reports whether or not a plugin has been registered.
  23493. *
  23494. * @private
  23495. * @param {string} name
  23496. * The name of a plugin.
  23497. *
  23498. * @return {boolean}
  23499. * Whether or not the plugin has been registered.
  23500. */
  23501. const pluginExists = name => pluginStorage.hasOwnProperty(name);
  23502. /**
  23503. * Get a single registered plugin by name.
  23504. *
  23505. * @private
  23506. * @param {string} name
  23507. * The name of a plugin.
  23508. *
  23509. * @return {typeof Plugin|Function|undefined}
  23510. * The plugin (or undefined).
  23511. */
  23512. const getPlugin = name => pluginExists(name) ? pluginStorage[name] : undefined;
  23513. /**
  23514. * Marks a plugin as "active" on a player.
  23515. *
  23516. * Also, ensures that the player has an object for tracking active plugins.
  23517. *
  23518. * @private
  23519. * @param {Player} player
  23520. * A Video.js player instance.
  23521. *
  23522. * @param {string} name
  23523. * The name of a plugin.
  23524. */
  23525. const markPluginAsActive = (player, name) => {
  23526. player[PLUGIN_CACHE_KEY] = player[PLUGIN_CACHE_KEY] || {};
  23527. player[PLUGIN_CACHE_KEY][name] = true;
  23528. };
  23529. /**
  23530. * Triggers a pair of plugin setup events.
  23531. *
  23532. * @private
  23533. * @param {Player} player
  23534. * A Video.js player instance.
  23535. *
  23536. * @param {Plugin~PluginEventHash} hash
  23537. * A plugin event hash.
  23538. *
  23539. * @param {boolean} [before]
  23540. * If true, prefixes the event name with "before". In other words,
  23541. * use this to trigger "beforepluginsetup" instead of "pluginsetup".
  23542. */
  23543. const triggerSetupEvent = (player, hash, before) => {
  23544. const eventName = (before ? 'before' : '') + 'pluginsetup';
  23545. player.trigger(eventName, hash);
  23546. player.trigger(eventName + ':' + hash.name, hash);
  23547. };
  23548. /**
  23549. * Takes a basic plugin function and returns a wrapper function which marks
  23550. * on the player that the plugin has been activated.
  23551. *
  23552. * @private
  23553. * @param {string} name
  23554. * The name of the plugin.
  23555. *
  23556. * @param {Function} plugin
  23557. * The basic plugin.
  23558. *
  23559. * @return {Function}
  23560. * A wrapper function for the given plugin.
  23561. */
  23562. const createBasicPlugin = function (name, plugin) {
  23563. const basicPluginWrapper = function () {
  23564. // We trigger the "beforepluginsetup" and "pluginsetup" events on the player
  23565. // regardless, but we want the hash to be consistent with the hash provided
  23566. // for advanced plugins.
  23567. //
  23568. // The only potentially counter-intuitive thing here is the `instance` in
  23569. // the "pluginsetup" event is the value returned by the `plugin` function.
  23570. triggerSetupEvent(this, {
  23571. name,
  23572. plugin,
  23573. instance: null
  23574. }, true);
  23575. const instance = plugin.apply(this, arguments);
  23576. markPluginAsActive(this, name);
  23577. triggerSetupEvent(this, {
  23578. name,
  23579. plugin,
  23580. instance
  23581. });
  23582. return instance;
  23583. };
  23584. Object.keys(plugin).forEach(function (prop) {
  23585. basicPluginWrapper[prop] = plugin[prop];
  23586. });
  23587. return basicPluginWrapper;
  23588. };
  23589. /**
  23590. * Takes a plugin sub-class and returns a factory function for generating
  23591. * instances of it.
  23592. *
  23593. * This factory function will replace itself with an instance of the requested
  23594. * sub-class of Plugin.
  23595. *
  23596. * @private
  23597. * @param {string} name
  23598. * The name of the plugin.
  23599. *
  23600. * @param {Plugin} PluginSubClass
  23601. * The advanced plugin.
  23602. *
  23603. * @return {Function}
  23604. */
  23605. const createPluginFactory = (name, PluginSubClass) => {
  23606. // Add a `name` property to the plugin prototype so that each plugin can
  23607. // refer to itself by name.
  23608. PluginSubClass.prototype.name = name;
  23609. return function (...args) {
  23610. triggerSetupEvent(this, {
  23611. name,
  23612. plugin: PluginSubClass,
  23613. instance: null
  23614. }, true);
  23615. const instance = new PluginSubClass(...[this, ...args]);
  23616. // The plugin is replaced by a function that returns the current instance.
  23617. this[name] = () => instance;
  23618. triggerSetupEvent(this, instance.getEventHash());
  23619. return instance;
  23620. };
  23621. };
  23622. /**
  23623. * Parent class for all advanced plugins.
  23624. *
  23625. * @mixes module:evented~EventedMixin
  23626. * @mixes module:stateful~StatefulMixin
  23627. * @fires Player#beforepluginsetup
  23628. * @fires Player#beforepluginsetup:$name
  23629. * @fires Player#pluginsetup
  23630. * @fires Player#pluginsetup:$name
  23631. * @listens Player#dispose
  23632. * @throws {Error}
  23633. * If attempting to instantiate the base {@link Plugin} class
  23634. * directly instead of via a sub-class.
  23635. */
  23636. class Plugin {
  23637. /**
  23638. * Creates an instance of this class.
  23639. *
  23640. * Sub-classes should call `super` to ensure plugins are properly initialized.
  23641. *
  23642. * @param {Player} player
  23643. * A Video.js player instance.
  23644. */
  23645. constructor(player) {
  23646. if (this.constructor === Plugin) {
  23647. throw new Error('Plugin must be sub-classed; not directly instantiated.');
  23648. }
  23649. this.player = player;
  23650. if (!this.log) {
  23651. this.log = this.player.log.createLogger(this.name);
  23652. }
  23653. // Make this object evented, but remove the added `trigger` method so we
  23654. // use the prototype version instead.
  23655. evented(this);
  23656. delete this.trigger;
  23657. stateful(this, this.constructor.defaultState);
  23658. markPluginAsActive(player, this.name);
  23659. // Auto-bind the dispose method so we can use it as a listener and unbind
  23660. // it later easily.
  23661. this.dispose = this.dispose.bind(this);
  23662. // If the player is disposed, dispose the plugin.
  23663. player.on('dispose', this.dispose);
  23664. }
  23665. /**
  23666. * Get the version of the plugin that was set on <pluginName>.VERSION
  23667. */
  23668. version() {
  23669. return this.constructor.VERSION;
  23670. }
  23671. /**
  23672. * Each event triggered by plugins includes a hash of additional data with
  23673. * conventional properties.
  23674. *
  23675. * This returns that object or mutates an existing hash.
  23676. *
  23677. * @param {Object} [hash={}]
  23678. * An object to be used as event an event hash.
  23679. *
  23680. * @return {Plugin~PluginEventHash}
  23681. * An event hash object with provided properties mixed-in.
  23682. */
  23683. getEventHash(hash = {}) {
  23684. hash.name = this.name;
  23685. hash.plugin = this.constructor;
  23686. hash.instance = this;
  23687. return hash;
  23688. }
  23689. /**
  23690. * Triggers an event on the plugin object and overrides
  23691. * {@link module:evented~EventedMixin.trigger|EventedMixin.trigger}.
  23692. *
  23693. * @param {string|Object} event
  23694. * An event type or an object with a type property.
  23695. *
  23696. * @param {Object} [hash={}]
  23697. * Additional data hash to merge with a
  23698. * {@link Plugin~PluginEventHash|PluginEventHash}.
  23699. *
  23700. * @return {boolean}
  23701. * Whether or not default was prevented.
  23702. */
  23703. trigger(event, hash = {}) {
  23704. return trigger(this.eventBusEl_, event, this.getEventHash(hash));
  23705. }
  23706. /**
  23707. * Handles "statechanged" events on the plugin. No-op by default, override by
  23708. * subclassing.
  23709. *
  23710. * @abstract
  23711. * @param {Event} e
  23712. * An event object provided by a "statechanged" event.
  23713. *
  23714. * @param {Object} e.changes
  23715. * An object describing changes that occurred with the "statechanged"
  23716. * event.
  23717. */
  23718. handleStateChanged(e) {}
  23719. /**
  23720. * Disposes a plugin.
  23721. *
  23722. * Subclasses can override this if they want, but for the sake of safety,
  23723. * it's probably best to subscribe the "dispose" event.
  23724. *
  23725. * @fires Plugin#dispose
  23726. */
  23727. dispose() {
  23728. const {
  23729. name,
  23730. player
  23731. } = this;
  23732. /**
  23733. * Signals that a advanced plugin is about to be disposed.
  23734. *
  23735. * @event Plugin#dispose
  23736. * @type {Event}
  23737. */
  23738. this.trigger('dispose');
  23739. this.off();
  23740. player.off('dispose', this.dispose);
  23741. // Eliminate any possible sources of leaking memory by clearing up
  23742. // references between the player and the plugin instance and nulling out
  23743. // the plugin's state and replacing methods with a function that throws.
  23744. player[PLUGIN_CACHE_KEY][name] = false;
  23745. this.player = this.state = null;
  23746. // Finally, replace the plugin name on the player with a new factory
  23747. // function, so that the plugin is ready to be set up again.
  23748. player[name] = createPluginFactory(name, pluginStorage[name]);
  23749. }
  23750. /**
  23751. * Determines if a plugin is a basic plugin (i.e. not a sub-class of `Plugin`).
  23752. *
  23753. * @param {string|Function} plugin
  23754. * If a string, matches the name of a plugin. If a function, will be
  23755. * tested directly.
  23756. *
  23757. * @return {boolean}
  23758. * Whether or not a plugin is a basic plugin.
  23759. */
  23760. static isBasic(plugin) {
  23761. const p = typeof plugin === 'string' ? getPlugin(plugin) : plugin;
  23762. return typeof p === 'function' && !Plugin.prototype.isPrototypeOf(p.prototype);
  23763. }
  23764. /**
  23765. * Register a Video.js plugin.
  23766. *
  23767. * @param {string} name
  23768. * The name of the plugin to be registered. Must be a string and
  23769. * must not match an existing plugin or a method on the `Player`
  23770. * prototype.
  23771. *
  23772. * @param {typeof Plugin|Function} plugin
  23773. * A sub-class of `Plugin` or a function for basic plugins.
  23774. *
  23775. * @return {typeof Plugin|Function}
  23776. * For advanced plugins, a factory function for that plugin. For
  23777. * basic plugins, a wrapper function that initializes the plugin.
  23778. */
  23779. static registerPlugin(name, plugin) {
  23780. if (typeof name !== 'string') {
  23781. throw new Error(`Illegal plugin name, "${name}", must be a string, was ${typeof name}.`);
  23782. }
  23783. if (pluginExists(name)) {
  23784. log.warn(`A plugin named "${name}" already exists. You may want to avoid re-registering plugins!`);
  23785. } else if (Player.prototype.hasOwnProperty(name)) {
  23786. throw new Error(`Illegal plugin name, "${name}", cannot share a name with an existing player method!`);
  23787. }
  23788. if (typeof plugin !== 'function') {
  23789. throw new Error(`Illegal plugin for "${name}", must be a function, was ${typeof plugin}.`);
  23790. }
  23791. pluginStorage[name] = plugin;
  23792. // Add a player prototype method for all sub-classed plugins (but not for
  23793. // the base Plugin class).
  23794. if (name !== BASE_PLUGIN_NAME) {
  23795. if (Plugin.isBasic(plugin)) {
  23796. Player.prototype[name] = createBasicPlugin(name, plugin);
  23797. } else {
  23798. Player.prototype[name] = createPluginFactory(name, plugin);
  23799. }
  23800. }
  23801. return plugin;
  23802. }
  23803. /**
  23804. * De-register a Video.js plugin.
  23805. *
  23806. * @param {string} name
  23807. * The name of the plugin to be de-registered. Must be a string that
  23808. * matches an existing plugin.
  23809. *
  23810. * @throws {Error}
  23811. * If an attempt is made to de-register the base plugin.
  23812. */
  23813. static deregisterPlugin(name) {
  23814. if (name === BASE_PLUGIN_NAME) {
  23815. throw new Error('Cannot de-register base plugin.');
  23816. }
  23817. if (pluginExists(name)) {
  23818. delete pluginStorage[name];
  23819. delete Player.prototype[name];
  23820. }
  23821. }
  23822. /**
  23823. * Gets an object containing multiple Video.js plugins.
  23824. *
  23825. * @param {Array} [names]
  23826. * If provided, should be an array of plugin names. Defaults to _all_
  23827. * plugin names.
  23828. *
  23829. * @return {Object|undefined}
  23830. * An object containing plugin(s) associated with their name(s) or
  23831. * `undefined` if no matching plugins exist).
  23832. */
  23833. static getPlugins(names = Object.keys(pluginStorage)) {
  23834. let result;
  23835. names.forEach(name => {
  23836. const plugin = getPlugin(name);
  23837. if (plugin) {
  23838. result = result || {};
  23839. result[name] = plugin;
  23840. }
  23841. });
  23842. return result;
  23843. }
  23844. /**
  23845. * Gets a plugin's version, if available
  23846. *
  23847. * @param {string} name
  23848. * The name of a plugin.
  23849. *
  23850. * @return {string}
  23851. * The plugin's version or an empty string.
  23852. */
  23853. static getPluginVersion(name) {
  23854. const plugin = getPlugin(name);
  23855. return plugin && plugin.VERSION || '';
  23856. }
  23857. }
  23858. /**
  23859. * Gets a plugin by name if it exists.
  23860. *
  23861. * @static
  23862. * @method getPlugin
  23863. * @memberOf Plugin
  23864. * @param {string} name
  23865. * The name of a plugin.
  23866. *
  23867. * @returns {typeof Plugin|Function|undefined}
  23868. * The plugin (or `undefined`).
  23869. */
  23870. Plugin.getPlugin = getPlugin;
  23871. /**
  23872. * The name of the base plugin class as it is registered.
  23873. *
  23874. * @type {string}
  23875. */
  23876. Plugin.BASE_PLUGIN_NAME = BASE_PLUGIN_NAME;
  23877. Plugin.registerPlugin(BASE_PLUGIN_NAME, Plugin);
  23878. /**
  23879. * Documented in player.js
  23880. *
  23881. * @ignore
  23882. */
  23883. Player.prototype.usingPlugin = function (name) {
  23884. return !!this[PLUGIN_CACHE_KEY] && this[PLUGIN_CACHE_KEY][name] === true;
  23885. };
  23886. /**
  23887. * Documented in player.js
  23888. *
  23889. * @ignore
  23890. */
  23891. Player.prototype.hasPlugin = function (name) {
  23892. return !!pluginExists(name);
  23893. };
  23894. /**
  23895. * Signals that a plugin is about to be set up on a player.
  23896. *
  23897. * @event Player#beforepluginsetup
  23898. * @type {Plugin~PluginEventHash}
  23899. */
  23900. /**
  23901. * Signals that a plugin is about to be set up on a player - by name. The name
  23902. * is the name of the plugin.
  23903. *
  23904. * @event Player#beforepluginsetup:$name
  23905. * @type {Plugin~PluginEventHash}
  23906. */
  23907. /**
  23908. * Signals that a plugin has just been set up on a player.
  23909. *
  23910. * @event Player#pluginsetup
  23911. * @type {Plugin~PluginEventHash}
  23912. */
  23913. /**
  23914. * Signals that a plugin has just been set up on a player - by name. The name
  23915. * is the name of the plugin.
  23916. *
  23917. * @event Player#pluginsetup:$name
  23918. * @type {Plugin~PluginEventHash}
  23919. */
  23920. /**
  23921. * @typedef {Object} Plugin~PluginEventHash
  23922. *
  23923. * @property {string} instance
  23924. * For basic plugins, the return value of the plugin function. For
  23925. * advanced plugins, the plugin instance on which the event is fired.
  23926. *
  23927. * @property {string} name
  23928. * The name of the plugin.
  23929. *
  23930. * @property {string} plugin
  23931. * For basic plugins, the plugin function. For advanced plugins, the
  23932. * plugin class/constructor.
  23933. */
  23934. /**
  23935. * @file deprecate.js
  23936. * @module deprecate
  23937. */
  23938. /**
  23939. * Decorate a function with a deprecation message the first time it is called.
  23940. *
  23941. * @param {string} message
  23942. * A deprecation message to log the first time the returned function
  23943. * is called.
  23944. *
  23945. * @param {Function} fn
  23946. * The function to be deprecated.
  23947. *
  23948. * @return {Function}
  23949. * A wrapper function that will log a deprecation warning the first
  23950. * time it is called. The return value will be the return value of
  23951. * the wrapped function.
  23952. */
  23953. function deprecate(message, fn) {
  23954. let warned = false;
  23955. return function (...args) {
  23956. if (!warned) {
  23957. log.warn(message);
  23958. }
  23959. warned = true;
  23960. return fn.apply(this, args);
  23961. };
  23962. }
  23963. /**
  23964. * Internal function used to mark a function as deprecated in the next major
  23965. * version with consistent messaging.
  23966. *
  23967. * @param {number} major The major version where it will be removed
  23968. * @param {string} oldName The old function name
  23969. * @param {string} newName The new function name
  23970. * @param {Function} fn The function to deprecate
  23971. * @return {Function} The decorated function
  23972. */
  23973. function deprecateForMajor(major, oldName, newName, fn) {
  23974. return deprecate(`${oldName} is deprecated and will be removed in ${major}.0; please use ${newName} instead.`, fn);
  23975. }
  23976. /**
  23977. * @file video.js
  23978. * @module videojs
  23979. */
  23980. /**
  23981. * Normalize an `id` value by trimming off a leading `#`
  23982. *
  23983. * @private
  23984. * @param {string} id
  23985. * A string, maybe with a leading `#`.
  23986. *
  23987. * @return {string}
  23988. * The string, without any leading `#`.
  23989. */
  23990. const normalizeId = id => id.indexOf('#') === 0 ? id.slice(1) : id;
  23991. /**
  23992. * A callback that is called when a component is ready. Does not have any
  23993. * parameters and any callback value will be ignored. See: {@link Component~ReadyCallback}
  23994. *
  23995. * @callback ReadyCallback
  23996. */
  23997. /**
  23998. * The `videojs()` function doubles as the main function for users to create a
  23999. * {@link Player} instance as well as the main library namespace.
  24000. *
  24001. * It can also be used as a getter for a pre-existing {@link Player} instance.
  24002. * However, we _strongly_ recommend using `videojs.getPlayer()` for this
  24003. * purpose because it avoids any potential for unintended initialization.
  24004. *
  24005. * Due to [limitations](https://github.com/jsdoc3/jsdoc/issues/955#issuecomment-313829149)
  24006. * of our JSDoc template, we cannot properly document this as both a function
  24007. * and a namespace, so its function signature is documented here.
  24008. *
  24009. * #### Arguments
  24010. * ##### id
  24011. * string|Element, **required**
  24012. *
  24013. * Video element or video element ID.
  24014. *
  24015. * ##### options
  24016. * Object, optional
  24017. *
  24018. * Options object for providing settings.
  24019. * See: [Options Guide](https://docs.videojs.com/tutorial-options.html).
  24020. *
  24021. * ##### ready
  24022. * {@link Component~ReadyCallback}, optional
  24023. *
  24024. * A function to be called when the {@link Player} and {@link Tech} are ready.
  24025. *
  24026. * #### Return Value
  24027. *
  24028. * The `videojs()` function returns a {@link Player} instance.
  24029. *
  24030. * @namespace
  24031. *
  24032. * @borrows AudioTrack as AudioTrack
  24033. * @borrows Component.getComponent as getComponent
  24034. * @borrows module:events.on as on
  24035. * @borrows module:events.one as one
  24036. * @borrows module:events.off as off
  24037. * @borrows module:events.trigger as trigger
  24038. * @borrows EventTarget as EventTarget
  24039. * @borrows module:middleware.use as use
  24040. * @borrows Player.players as players
  24041. * @borrows Plugin.registerPlugin as registerPlugin
  24042. * @borrows Plugin.deregisterPlugin as deregisterPlugin
  24043. * @borrows Plugin.getPlugins as getPlugins
  24044. * @borrows Plugin.getPlugin as getPlugin
  24045. * @borrows Plugin.getPluginVersion as getPluginVersion
  24046. * @borrows Tech.getTech as getTech
  24047. * @borrows Tech.registerTech as registerTech
  24048. * @borrows TextTrack as TextTrack
  24049. * @borrows VideoTrack as VideoTrack
  24050. *
  24051. * @param {string|Element} id
  24052. * Video element or video element ID.
  24053. *
  24054. * @param {Object} [options]
  24055. * Options object for providing settings.
  24056. * See: [Options Guide](https://docs.videojs.com/tutorial-options.html).
  24057. *
  24058. * @param {ReadyCallback} [ready]
  24059. * A function to be called when the {@link Player} and {@link Tech} are
  24060. * ready.
  24061. *
  24062. * @return {Player}
  24063. * The `videojs()` function returns a {@link Player|Player} instance.
  24064. */
  24065. function videojs(id, options, ready) {
  24066. let player = videojs.getPlayer(id);
  24067. if (player) {
  24068. if (options) {
  24069. log.warn(`Player "${id}" is already initialised. Options will not be applied.`);
  24070. }
  24071. if (ready) {
  24072. player.ready(ready);
  24073. }
  24074. return player;
  24075. }
  24076. const el = typeof id === 'string' ? $('#' + normalizeId(id)) : id;
  24077. if (!isEl(el)) {
  24078. throw new TypeError('The element or ID supplied is not valid. (videojs)');
  24079. }
  24080. // document.body.contains(el) will only check if el is contained within that one document.
  24081. // This causes problems for elements in iframes.
  24082. // Instead, use the element's ownerDocument instead of the global document.
  24083. // This will make sure that the element is indeed in the dom of that document.
  24084. // Additionally, check that the document in question has a default view.
  24085. // If the document is no longer attached to the dom, the defaultView of the document will be null.
  24086. if (!el.ownerDocument.defaultView || !el.ownerDocument.body.contains(el)) {
  24087. log.warn('The element supplied is not included in the DOM');
  24088. }
  24089. options = options || {};
  24090. // Store a copy of the el before modification, if it is to be restored in destroy()
  24091. // If div ingest, store the parent div
  24092. if (options.restoreEl === true) {
  24093. options.restoreEl = (el.parentNode && el.parentNode.hasAttribute('data-vjs-player') ? el.parentNode : el).cloneNode(true);
  24094. }
  24095. hooks('beforesetup').forEach(hookFunction => {
  24096. const opts = hookFunction(el, merge(options));
  24097. if (!isObject(opts) || Array.isArray(opts)) {
  24098. log.error('please return an object in beforesetup hooks');
  24099. return;
  24100. }
  24101. options = merge(options, opts);
  24102. });
  24103. // We get the current "Player" component here in case an integration has
  24104. // replaced it with a custom player.
  24105. const PlayerComponent = Component.getComponent('Player');
  24106. player = new PlayerComponent(el, options, ready);
  24107. hooks('setup').forEach(hookFunction => hookFunction(player));
  24108. return player;
  24109. }
  24110. videojs.hooks_ = hooks_;
  24111. videojs.hooks = hooks;
  24112. videojs.hook = hook;
  24113. videojs.hookOnce = hookOnce;
  24114. videojs.removeHook = removeHook;
  24115. // Add default styles
  24116. if (window.VIDEOJS_NO_DYNAMIC_STYLE !== true && isReal()) {
  24117. let style = $('.vjs-styles-defaults');
  24118. if (!style) {
  24119. style = createStyleElement('vjs-styles-defaults');
  24120. const head = $('head');
  24121. if (head) {
  24122. head.insertBefore(style, head.firstChild);
  24123. }
  24124. setTextContent(style, `
  24125. .video-js {
  24126. width: 300px;
  24127. height: 150px;
  24128. }
  24129. .vjs-fluid:not(.vjs-audio-only-mode) {
  24130. padding-top: 56.25%
  24131. }
  24132. `);
  24133. }
  24134. }
  24135. // Run Auto-load players
  24136. // You have to wait at least once in case this script is loaded after your
  24137. // video in the DOM (weird behavior only with minified version)
  24138. autoSetupTimeout(1, videojs);
  24139. /**
  24140. * Current Video.js version. Follows [semantic versioning](https://semver.org/).
  24141. *
  24142. * @type {string}
  24143. */
  24144. videojs.VERSION = version;
  24145. /**
  24146. * The global options object. These are the settings that take effect
  24147. * if no overrides are specified when the player is created.
  24148. *
  24149. * @type {Object}
  24150. */
  24151. videojs.options = Player.prototype.options_;
  24152. /**
  24153. * Get an object with the currently created players, keyed by player ID
  24154. *
  24155. * @return {Object}
  24156. * The created players
  24157. */
  24158. videojs.getPlayers = () => Player.players;
  24159. /**
  24160. * Get a single player based on an ID or DOM element.
  24161. *
  24162. * This is useful if you want to check if an element or ID has an associated
  24163. * Video.js player, but not create one if it doesn't.
  24164. *
  24165. * @param {string|Element} id
  24166. * An HTML element - `<video>`, `<audio>`, or `<video-js>` -
  24167. * or a string matching the `id` of such an element.
  24168. *
  24169. * @return {Player|undefined}
  24170. * A player instance or `undefined` if there is no player instance
  24171. * matching the argument.
  24172. */
  24173. videojs.getPlayer = id => {
  24174. const players = Player.players;
  24175. let tag;
  24176. if (typeof id === 'string') {
  24177. const nId = normalizeId(id);
  24178. const player = players[nId];
  24179. if (player) {
  24180. return player;
  24181. }
  24182. tag = $('#' + nId);
  24183. } else {
  24184. tag = id;
  24185. }
  24186. if (isEl(tag)) {
  24187. const {
  24188. player,
  24189. playerId
  24190. } = tag;
  24191. // Element may have a `player` property referring to an already created
  24192. // player instance. If so, return that.
  24193. if (player || players[playerId]) {
  24194. return player || players[playerId];
  24195. }
  24196. }
  24197. };
  24198. /**
  24199. * Returns an array of all current players.
  24200. *
  24201. * @return {Array}
  24202. * An array of all players. The array will be in the order that
  24203. * `Object.keys` provides, which could potentially vary between
  24204. * JavaScript engines.
  24205. *
  24206. */
  24207. videojs.getAllPlayers = () =>
  24208. // Disposed players leave a key with a `null` value, so we need to make sure
  24209. // we filter those out.
  24210. Object.keys(Player.players).map(k => Player.players[k]).filter(Boolean);
  24211. videojs.players = Player.players;
  24212. videojs.getComponent = Component.getComponent;
  24213. /**
  24214. * Register a component so it can referred to by name. Used when adding to other
  24215. * components, either through addChild `component.addChild('myComponent')` or through
  24216. * default children options `{ children: ['myComponent'] }`.
  24217. *
  24218. * > NOTE: You could also just initialize the component before adding.
  24219. * `component.addChild(new MyComponent());`
  24220. *
  24221. * @param {string} name
  24222. * The class name of the component
  24223. *
  24224. * @param {Component} comp
  24225. * The component class
  24226. *
  24227. * @return {Component}
  24228. * The newly registered component
  24229. */
  24230. videojs.registerComponent = (name, comp) => {
  24231. if (Tech.isTech(comp)) {
  24232. log.warn(`The ${name} tech was registered as a component. It should instead be registered using videojs.registerTech(name, tech)`);
  24233. }
  24234. Component.registerComponent.call(Component, name, comp);
  24235. };
  24236. videojs.getTech = Tech.getTech;
  24237. videojs.registerTech = Tech.registerTech;
  24238. videojs.use = use;
  24239. /**
  24240. * An object that can be returned by a middleware to signify
  24241. * that the middleware is being terminated.
  24242. *
  24243. * @type {object}
  24244. * @property {object} middleware.TERMINATOR
  24245. */
  24246. Object.defineProperty(videojs, 'middleware', {
  24247. value: {},
  24248. writeable: false,
  24249. enumerable: true
  24250. });
  24251. Object.defineProperty(videojs.middleware, 'TERMINATOR', {
  24252. value: TERMINATOR,
  24253. writeable: false,
  24254. enumerable: true
  24255. });
  24256. /**
  24257. * A reference to the {@link module:browser|browser utility module} as an object.
  24258. *
  24259. * @type {Object}
  24260. * @see {@link module:browser|browser}
  24261. */
  24262. videojs.browser = browser;
  24263. /**
  24264. * A reference to the {@link module:obj|obj utility module} as an object.
  24265. *
  24266. * @type {Object}
  24267. * @see {@link module:obj|obj}
  24268. */
  24269. videojs.obj = Obj;
  24270. /**
  24271. * Deprecated reference to the {@link module:obj.merge|merge function}
  24272. *
  24273. * @type {Function}
  24274. * @see {@link module:obj.merge|merge}
  24275. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.obj.merge instead.
  24276. */
  24277. videojs.mergeOptions = deprecateForMajor(9, 'videojs.mergeOptions', 'videojs.obj.merge', merge);
  24278. /**
  24279. * Deprecated reference to the {@link module:obj.defineLazyProperty|defineLazyProperty function}
  24280. *
  24281. * @type {Function}
  24282. * @see {@link module:obj.defineLazyProperty|defineLazyProperty}
  24283. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.obj.defineLazyProperty instead.
  24284. */
  24285. videojs.defineLazyProperty = deprecateForMajor(9, 'videojs.defineLazyProperty', 'videojs.obj.defineLazyProperty', defineLazyProperty);
  24286. /**
  24287. * Deprecated reference to the {@link module:fn.bind_|fn.bind_ function}
  24288. *
  24289. * @type {Function}
  24290. * @see {@link module:fn.bind_|fn.bind_}
  24291. * @deprecated Deprecated and will be removed in 9.0. Please use native Function.prototype.bind instead.
  24292. */
  24293. videojs.bind = deprecateForMajor(9, 'videojs.bind', 'native Function.prototype.bind', bind_);
  24294. videojs.registerPlugin = Plugin.registerPlugin;
  24295. videojs.deregisterPlugin = Plugin.deregisterPlugin;
  24296. /**
  24297. * Deprecated method to register a plugin with Video.js
  24298. *
  24299. * @deprecated Deprecated and will be removed in 9.0. Use videojs.registerPlugin() instead.
  24300. *
  24301. * @param {string} name
  24302. * The plugin name
  24303. *
  24304. * @param {Plugin|Function} plugin
  24305. * The plugin sub-class or function
  24306. */
  24307. videojs.plugin = (name, plugin) => {
  24308. log.warn('videojs.plugin() is deprecated; use videojs.registerPlugin() instead');
  24309. return Plugin.registerPlugin(name, plugin);
  24310. };
  24311. videojs.getPlugins = Plugin.getPlugins;
  24312. videojs.getPlugin = Plugin.getPlugin;
  24313. videojs.getPluginVersion = Plugin.getPluginVersion;
  24314. /**
  24315. * Adding languages so that they're available to all players.
  24316. * Example: `videojs.addLanguage('es', { 'Hello': 'Hola' });`
  24317. *
  24318. * @param {string} code
  24319. * The language code or dictionary property
  24320. *
  24321. * @param {Object} data
  24322. * The data values to be translated
  24323. *
  24324. * @return {Object}
  24325. * The resulting language dictionary object
  24326. */
  24327. videojs.addLanguage = function (code, data) {
  24328. code = ('' + code).toLowerCase();
  24329. videojs.options.languages = merge(videojs.options.languages, {
  24330. [code]: data
  24331. });
  24332. return videojs.options.languages[code];
  24333. };
  24334. /**
  24335. * A reference to the {@link module:log|log utility module} as an object.
  24336. *
  24337. * @type {Function}
  24338. * @see {@link module:log|log}
  24339. */
  24340. videojs.log = log;
  24341. videojs.createLogger = createLogger;
  24342. /**
  24343. * A reference to the {@link module:time|time utility module} as an object.
  24344. *
  24345. * @type {Object}
  24346. * @see {@link module:time|time}
  24347. */
  24348. videojs.time = Time;
  24349. /**
  24350. * Deprecated reference to the {@link module:time.createTimeRanges|createTimeRanges function}
  24351. *
  24352. * @type {Function}
  24353. * @see {@link module:time.createTimeRanges|createTimeRanges}
  24354. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.createTimeRanges instead.
  24355. */
  24356. videojs.createTimeRange = deprecateForMajor(9, 'videojs.createTimeRange', 'videojs.time.createTimeRanges', createTimeRanges);
  24357. /**
  24358. * Deprecated reference to the {@link module:time.createTimeRanges|createTimeRanges function}
  24359. *
  24360. * @type {Function}
  24361. * @see {@link module:time.createTimeRanges|createTimeRanges}
  24362. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.createTimeRanges instead.
  24363. */
  24364. videojs.createTimeRanges = deprecateForMajor(9, 'videojs.createTimeRanges', 'videojs.time.createTimeRanges', createTimeRanges);
  24365. /**
  24366. * Deprecated reference to the {@link module:time.formatTime|formatTime function}
  24367. *
  24368. * @type {Function}
  24369. * @see {@link module:time.formatTime|formatTime}
  24370. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.format instead.
  24371. */
  24372. videojs.formatTime = deprecateForMajor(9, 'videojs.formatTime', 'videojs.time.formatTime', formatTime);
  24373. /**
  24374. * Deprecated reference to the {@link module:time.setFormatTime|setFormatTime function}
  24375. *
  24376. * @type {Function}
  24377. * @see {@link module:time.setFormatTime|setFormatTime}
  24378. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.setFormat instead.
  24379. */
  24380. videojs.setFormatTime = deprecateForMajor(9, 'videojs.setFormatTime', 'videojs.time.setFormatTime', setFormatTime);
  24381. /**
  24382. * Deprecated reference to the {@link module:time.resetFormatTime|resetFormatTime function}
  24383. *
  24384. * @type {Function}
  24385. * @see {@link module:time.resetFormatTime|resetFormatTime}
  24386. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.time.resetFormat instead.
  24387. */
  24388. videojs.resetFormatTime = deprecateForMajor(9, 'videojs.resetFormatTime', 'videojs.time.resetFormatTime', resetFormatTime);
  24389. /**
  24390. * Deprecated reference to the {@link module:url.parseUrl|Url.parseUrl function}
  24391. *
  24392. * @type {Function}
  24393. * @see {@link module:url.parseUrl|parseUrl}
  24394. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.url.parseUrl instead.
  24395. */
  24396. videojs.parseUrl = deprecateForMajor(9, 'videojs.parseUrl', 'videojs.url.parseUrl', parseUrl);
  24397. /**
  24398. * Deprecated reference to the {@link module:url.isCrossOrigin|Url.isCrossOrigin function}
  24399. *
  24400. * @type {Function}
  24401. * @see {@link module:url.isCrossOrigin|isCrossOrigin}
  24402. * @deprecated Deprecated and will be removed in 9.0. Please use videojs.url.isCrossOrigin instead.
  24403. */
  24404. videojs.isCrossOrigin = deprecateForMajor(9, 'videojs.isCrossOrigin', 'videojs.url.isCrossOrigin', isCrossOrigin);
  24405. videojs.EventTarget = EventTarget;
  24406. videojs.any = any;
  24407. videojs.on = on;
  24408. videojs.one = one;
  24409. videojs.off = off;
  24410. videojs.trigger = trigger;
  24411. /**
  24412. * A cross-browser XMLHttpRequest wrapper.
  24413. *
  24414. * @function
  24415. * @param {Object} options
  24416. * Settings for the request.
  24417. *
  24418. * @return {XMLHttpRequest|XDomainRequest}
  24419. * The request object.
  24420. *
  24421. * @see https://github.com/Raynos/xhr
  24422. */
  24423. videojs.xhr = lib;
  24424. videojs.TextTrack = TextTrack;
  24425. videojs.AudioTrack = AudioTrack;
  24426. videojs.VideoTrack = VideoTrack;
  24427. ['isEl', 'isTextNode', 'createEl', 'hasClass', 'addClass', 'removeClass', 'toggleClass', 'setAttributes', 'getAttributes', 'emptyEl', 'appendContent', 'insertContent'].forEach(k => {
  24428. videojs[k] = function () {
  24429. log.warn(`videojs.${k}() is deprecated; use videojs.dom.${k}() instead`);
  24430. return Dom[k].apply(null, arguments);
  24431. };
  24432. });
  24433. videojs.computedStyle = deprecateForMajor(9, 'videojs.computedStyle', 'videojs.dom.computedStyle', computedStyle);
  24434. /**
  24435. * A reference to the {@link module:dom|DOM utility module} as an object.
  24436. *
  24437. * @type {Object}
  24438. * @see {@link module:dom|dom}
  24439. */
  24440. videojs.dom = Dom;
  24441. /**
  24442. * A reference to the {@link module:fn|fn utility module} as an object.
  24443. *
  24444. * @type {Object}
  24445. * @see {@link module:fn|fn}
  24446. */
  24447. videojs.fn = Fn;
  24448. /**
  24449. * A reference to the {@link module:num|num utility module} as an object.
  24450. *
  24451. * @type {Object}
  24452. * @see {@link module:num|num}
  24453. */
  24454. videojs.num = Num;
  24455. /**
  24456. * A reference to the {@link module:str|str utility module} as an object.
  24457. *
  24458. * @type {Object}
  24459. * @see {@link module:str|str}
  24460. */
  24461. videojs.str = Str;
  24462. /**
  24463. * A reference to the {@link module:url|URL utility module} as an object.
  24464. *
  24465. * @type {Object}
  24466. * @see {@link module:url|url}
  24467. */
  24468. videojs.url = Url;
  24469. return videojs;
  24470. }));