flv.min.js 614 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.flvjs = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(_dereq_,module,exports){
  2. (function (process,global){
  3. /*!
  4. * @overview es6-promise - a tiny implementation of Promises/A+.
  5. * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
  6. * @license Licensed under MIT license
  7. * See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE
  8. * @version v4.2.4+314e4831
  9. */
  10. (function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  12. typeof define === 'function' && define.amd ? define(factory) :
  13. (global.ES6Promise = factory());
  14. }(this, (function () { 'use strict';
  15. function objectOrFunction(x) {
  16. var type = typeof x;
  17. return x !== null && (type === 'object' || type === 'function');
  18. }
  19. function isFunction(x) {
  20. return typeof x === 'function';
  21. }
  22. var _isArray = void 0;
  23. if (Array.isArray) {
  24. _isArray = Array.isArray;
  25. } else {
  26. _isArray = function (x) {
  27. return Object.prototype.toString.call(x) === '[object Array]';
  28. };
  29. }
  30. var isArray = _isArray;
  31. var len = 0;
  32. var vertxNext = void 0;
  33. var customSchedulerFn = void 0;
  34. var asap = function asap(callback, arg) {
  35. queue[len] = callback;
  36. queue[len + 1] = arg;
  37. len += 2;
  38. if (len === 2) {
  39. // If len is 2, that means that we need to schedule an async flush.
  40. // If additional callbacks are queued before the queue is flushed, they
  41. // will be processed by this flush that we are scheduling.
  42. if (customSchedulerFn) {
  43. customSchedulerFn(flush);
  44. } else {
  45. scheduleFlush();
  46. }
  47. }
  48. };
  49. function setScheduler(scheduleFn) {
  50. customSchedulerFn = scheduleFn;
  51. }
  52. function setAsap(asapFn) {
  53. asap = asapFn;
  54. }
  55. var browserWindow = typeof window !== 'undefined' ? window : undefined;
  56. var browserGlobal = browserWindow || {};
  57. var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
  58. var isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
  59. // test for web worker but not in IE10
  60. var isWorker = typeof Uint8ClampedArray !== 'undefined' && typeof importScripts !== 'undefined' && typeof MessageChannel !== 'undefined';
  61. // node
  62. function useNextTick() {
  63. // node version 0.10.x displays a deprecation warning when nextTick is used recursively
  64. // see https://github.com/cujojs/when/issues/410 for details
  65. return function () {
  66. return process.nextTick(flush);
  67. };
  68. }
  69. // vertx
  70. function useVertxTimer() {
  71. if (typeof vertxNext !== 'undefined') {
  72. return function () {
  73. vertxNext(flush);
  74. };
  75. }
  76. return useSetTimeout();
  77. }
  78. function useMutationObserver() {
  79. var iterations = 0;
  80. var observer = new BrowserMutationObserver(flush);
  81. var node = document.createTextNode('');
  82. observer.observe(node, { characterData: true });
  83. return function () {
  84. node.data = iterations = ++iterations % 2;
  85. };
  86. }
  87. // web worker
  88. function useMessageChannel() {
  89. var channel = new MessageChannel();
  90. channel.port1.onmessage = flush;
  91. return function () {
  92. return channel.port2.postMessage(0);
  93. };
  94. }
  95. function useSetTimeout() {
  96. // Store setTimeout reference so es6-promise will be unaffected by
  97. // other code modifying setTimeout (like sinon.useFakeTimers())
  98. var globalSetTimeout = setTimeout;
  99. return function () {
  100. return globalSetTimeout(flush, 1);
  101. };
  102. }
  103. var queue = new Array(1000);
  104. function flush() {
  105. for (var i = 0; i < len; i += 2) {
  106. var callback = queue[i];
  107. var arg = queue[i + 1];
  108. callback(arg);
  109. queue[i] = undefined;
  110. queue[i + 1] = undefined;
  111. }
  112. len = 0;
  113. }
  114. function attemptVertx() {
  115. try {
  116. var vertx = Function('return this')().require('vertx');
  117. vertxNext = vertx.runOnLoop || vertx.runOnContext;
  118. return useVertxTimer();
  119. } catch (e) {
  120. return useSetTimeout();
  121. }
  122. }
  123. var scheduleFlush = void 0;
  124. // Decide what async method to use to triggering processing of queued callbacks:
  125. if (isNode) {
  126. scheduleFlush = useNextTick();
  127. } else if (BrowserMutationObserver) {
  128. scheduleFlush = useMutationObserver();
  129. } else if (isWorker) {
  130. scheduleFlush = useMessageChannel();
  131. } else if (browserWindow === undefined && typeof _dereq_ === 'function') {
  132. scheduleFlush = attemptVertx();
  133. } else {
  134. scheduleFlush = useSetTimeout();
  135. }
  136. function then(onFulfillment, onRejection) {
  137. var parent = this;
  138. var child = new this.constructor(noop);
  139. if (child[PROMISE_ID] === undefined) {
  140. makePromise(child);
  141. }
  142. var _state = parent._state;
  143. if (_state) {
  144. var callback = arguments[_state - 1];
  145. asap(function () {
  146. return invokeCallback(_state, child, callback, parent._result);
  147. });
  148. } else {
  149. subscribe(parent, child, onFulfillment, onRejection);
  150. }
  151. return child;
  152. }
  153. /**
  154. `Promise.resolve` returns a promise that will become resolved with the
  155. passed `value`. It is shorthand for the following:
  156. ```javascript
  157. let promise = new Promise(function(resolve, reject){
  158. resolve(1);
  159. });
  160. promise.then(function(value){
  161. // value === 1
  162. });
  163. ```
  164. Instead of writing the above, your code now simply becomes the following:
  165. ```javascript
  166. let promise = Promise.resolve(1);
  167. promise.then(function(value){
  168. // value === 1
  169. });
  170. ```
  171. @method resolve
  172. @static
  173. @param {Any} value value that the returned promise will be resolved with
  174. Useful for tooling.
  175. @return {Promise} a promise that will become fulfilled with the given
  176. `value`
  177. */
  178. function resolve$1(object) {
  179. /*jshint validthis:true */
  180. var Constructor = this;
  181. if (object && typeof object === 'object' && object.constructor === Constructor) {
  182. return object;
  183. }
  184. var promise = new Constructor(noop);
  185. resolve(promise, object);
  186. return promise;
  187. }
  188. var PROMISE_ID = Math.random().toString(36).substring(2);
  189. function noop() {}
  190. var PENDING = void 0;
  191. var FULFILLED = 1;
  192. var REJECTED = 2;
  193. var TRY_CATCH_ERROR = { error: null };
  194. function selfFulfillment() {
  195. return new TypeError("You cannot resolve a promise with itself");
  196. }
  197. function cannotReturnOwn() {
  198. return new TypeError('A promises callback cannot return that same promise.');
  199. }
  200. function getThen(promise) {
  201. try {
  202. return promise.then;
  203. } catch (error) {
  204. TRY_CATCH_ERROR.error = error;
  205. return TRY_CATCH_ERROR;
  206. }
  207. }
  208. function tryThen(then$$1, value, fulfillmentHandler, rejectionHandler) {
  209. try {
  210. then$$1.call(value, fulfillmentHandler, rejectionHandler);
  211. } catch (e) {
  212. return e;
  213. }
  214. }
  215. function handleForeignThenable(promise, thenable, then$$1) {
  216. asap(function (promise) {
  217. var sealed = false;
  218. var error = tryThen(then$$1, thenable, function (value) {
  219. if (sealed) {
  220. return;
  221. }
  222. sealed = true;
  223. if (thenable !== value) {
  224. resolve(promise, value);
  225. } else {
  226. fulfill(promise, value);
  227. }
  228. }, function (reason) {
  229. if (sealed) {
  230. return;
  231. }
  232. sealed = true;
  233. reject(promise, reason);
  234. }, 'Settle: ' + (promise._label || ' unknown promise'));
  235. if (!sealed && error) {
  236. sealed = true;
  237. reject(promise, error);
  238. }
  239. }, promise);
  240. }
  241. function handleOwnThenable(promise, thenable) {
  242. if (thenable._state === FULFILLED) {
  243. fulfill(promise, thenable._result);
  244. } else if (thenable._state === REJECTED) {
  245. reject(promise, thenable._result);
  246. } else {
  247. subscribe(thenable, undefined, function (value) {
  248. return resolve(promise, value);
  249. }, function (reason) {
  250. return reject(promise, reason);
  251. });
  252. }
  253. }
  254. function handleMaybeThenable(promise, maybeThenable, then$$1) {
  255. if (maybeThenable.constructor === promise.constructor && then$$1 === then && maybeThenable.constructor.resolve === resolve$1) {
  256. handleOwnThenable(promise, maybeThenable);
  257. } else {
  258. if (then$$1 === TRY_CATCH_ERROR) {
  259. reject(promise, TRY_CATCH_ERROR.error);
  260. TRY_CATCH_ERROR.error = null;
  261. } else if (then$$1 === undefined) {
  262. fulfill(promise, maybeThenable);
  263. } else if (isFunction(then$$1)) {
  264. handleForeignThenable(promise, maybeThenable, then$$1);
  265. } else {
  266. fulfill(promise, maybeThenable);
  267. }
  268. }
  269. }
  270. function resolve(promise, value) {
  271. if (promise === value) {
  272. reject(promise, selfFulfillment());
  273. } else if (objectOrFunction(value)) {
  274. handleMaybeThenable(promise, value, getThen(value));
  275. } else {
  276. fulfill(promise, value);
  277. }
  278. }
  279. function publishRejection(promise) {
  280. if (promise._onerror) {
  281. promise._onerror(promise._result);
  282. }
  283. publish(promise);
  284. }
  285. function fulfill(promise, value) {
  286. if (promise._state !== PENDING) {
  287. return;
  288. }
  289. promise._result = value;
  290. promise._state = FULFILLED;
  291. if (promise._subscribers.length !== 0) {
  292. asap(publish, promise);
  293. }
  294. }
  295. function reject(promise, reason) {
  296. if (promise._state !== PENDING) {
  297. return;
  298. }
  299. promise._state = REJECTED;
  300. promise._result = reason;
  301. asap(publishRejection, promise);
  302. }
  303. function subscribe(parent, child, onFulfillment, onRejection) {
  304. var _subscribers = parent._subscribers;
  305. var length = _subscribers.length;
  306. parent._onerror = null;
  307. _subscribers[length] = child;
  308. _subscribers[length + FULFILLED] = onFulfillment;
  309. _subscribers[length + REJECTED] = onRejection;
  310. if (length === 0 && parent._state) {
  311. asap(publish, parent);
  312. }
  313. }
  314. function publish(promise) {
  315. var subscribers = promise._subscribers;
  316. var settled = promise._state;
  317. if (subscribers.length === 0) {
  318. return;
  319. }
  320. var child = void 0,
  321. callback = void 0,
  322. detail = promise._result;
  323. for (var i = 0; i < subscribers.length; i += 3) {
  324. child = subscribers[i];
  325. callback = subscribers[i + settled];
  326. if (child) {
  327. invokeCallback(settled, child, callback, detail);
  328. } else {
  329. callback(detail);
  330. }
  331. }
  332. promise._subscribers.length = 0;
  333. }
  334. function tryCatch(callback, detail) {
  335. try {
  336. return callback(detail);
  337. } catch (e) {
  338. TRY_CATCH_ERROR.error = e;
  339. return TRY_CATCH_ERROR;
  340. }
  341. }
  342. function invokeCallback(settled, promise, callback, detail) {
  343. var hasCallback = isFunction(callback),
  344. value = void 0,
  345. error = void 0,
  346. succeeded = void 0,
  347. failed = void 0;
  348. if (hasCallback) {
  349. value = tryCatch(callback, detail);
  350. if (value === TRY_CATCH_ERROR) {
  351. failed = true;
  352. error = value.error;
  353. value.error = null;
  354. } else {
  355. succeeded = true;
  356. }
  357. if (promise === value) {
  358. reject(promise, cannotReturnOwn());
  359. return;
  360. }
  361. } else {
  362. value = detail;
  363. succeeded = true;
  364. }
  365. if (promise._state !== PENDING) {
  366. // noop
  367. } else if (hasCallback && succeeded) {
  368. resolve(promise, value);
  369. } else if (failed) {
  370. reject(promise, error);
  371. } else if (settled === FULFILLED) {
  372. fulfill(promise, value);
  373. } else if (settled === REJECTED) {
  374. reject(promise, value);
  375. }
  376. }
  377. function initializePromise(promise, resolver) {
  378. try {
  379. resolver(function resolvePromise(value) {
  380. resolve(promise, value);
  381. }, function rejectPromise(reason) {
  382. reject(promise, reason);
  383. });
  384. } catch (e) {
  385. reject(promise, e);
  386. }
  387. }
  388. var id = 0;
  389. function nextId() {
  390. return id++;
  391. }
  392. function makePromise(promise) {
  393. promise[PROMISE_ID] = id++;
  394. promise._state = undefined;
  395. promise._result = undefined;
  396. promise._subscribers = [];
  397. }
  398. function validationError() {
  399. return new Error('Array Methods must be provided an Array');
  400. }
  401. var Enumerator = function () {
  402. function Enumerator(Constructor, input) {
  403. this._instanceConstructor = Constructor;
  404. this.promise = new Constructor(noop);
  405. if (!this.promise[PROMISE_ID]) {
  406. makePromise(this.promise);
  407. }
  408. if (isArray(input)) {
  409. this.length = input.length;
  410. this._remaining = input.length;
  411. this._result = new Array(this.length);
  412. if (this.length === 0) {
  413. fulfill(this.promise, this._result);
  414. } else {
  415. this.length = this.length || 0;
  416. this._enumerate(input);
  417. if (this._remaining === 0) {
  418. fulfill(this.promise, this._result);
  419. }
  420. }
  421. } else {
  422. reject(this.promise, validationError());
  423. }
  424. }
  425. Enumerator.prototype._enumerate = function _enumerate(input) {
  426. for (var i = 0; this._state === PENDING && i < input.length; i++) {
  427. this._eachEntry(input[i], i);
  428. }
  429. };
  430. Enumerator.prototype._eachEntry = function _eachEntry(entry, i) {
  431. var c = this._instanceConstructor;
  432. var resolve$$1 = c.resolve;
  433. if (resolve$$1 === resolve$1) {
  434. var _then = getThen(entry);
  435. if (_then === then && entry._state !== PENDING) {
  436. this._settledAt(entry._state, i, entry._result);
  437. } else if (typeof _then !== 'function') {
  438. this._remaining--;
  439. this._result[i] = entry;
  440. } else if (c === Promise$1) {
  441. var promise = new c(noop);
  442. handleMaybeThenable(promise, entry, _then);
  443. this._willSettleAt(promise, i);
  444. } else {
  445. this._willSettleAt(new c(function (resolve$$1) {
  446. return resolve$$1(entry);
  447. }), i);
  448. }
  449. } else {
  450. this._willSettleAt(resolve$$1(entry), i);
  451. }
  452. };
  453. Enumerator.prototype._settledAt = function _settledAt(state, i, value) {
  454. var promise = this.promise;
  455. if (promise._state === PENDING) {
  456. this._remaining--;
  457. if (state === REJECTED) {
  458. reject(promise, value);
  459. } else {
  460. this._result[i] = value;
  461. }
  462. }
  463. if (this._remaining === 0) {
  464. fulfill(promise, this._result);
  465. }
  466. };
  467. Enumerator.prototype._willSettleAt = function _willSettleAt(promise, i) {
  468. var enumerator = this;
  469. subscribe(promise, undefined, function (value) {
  470. return enumerator._settledAt(FULFILLED, i, value);
  471. }, function (reason) {
  472. return enumerator._settledAt(REJECTED, i, reason);
  473. });
  474. };
  475. return Enumerator;
  476. }();
  477. /**
  478. `Promise.all` accepts an array of promises, and returns a new promise which
  479. is fulfilled with an array of fulfillment values for the passed promises, or
  480. rejected with the reason of the first passed promise to be rejected. It casts all
  481. elements of the passed iterable to promises as it runs this algorithm.
  482. Example:
  483. ```javascript
  484. let promise1 = resolve(1);
  485. let promise2 = resolve(2);
  486. let promise3 = resolve(3);
  487. let promises = [ promise1, promise2, promise3 ];
  488. Promise.all(promises).then(function(array){
  489. // The array here would be [ 1, 2, 3 ];
  490. });
  491. ```
  492. If any of the `promises` given to `all` are rejected, the first promise
  493. that is rejected will be given as an argument to the returned promises's
  494. rejection handler. For example:
  495. Example:
  496. ```javascript
  497. let promise1 = resolve(1);
  498. let promise2 = reject(new Error("2"));
  499. let promise3 = reject(new Error("3"));
  500. let promises = [ promise1, promise2, promise3 ];
  501. Promise.all(promises).then(function(array){
  502. // Code here never runs because there are rejected promises!
  503. }, function(error) {
  504. // error.message === "2"
  505. });
  506. ```
  507. @method all
  508. @static
  509. @param {Array} entries array of promises
  510. @param {String} label optional string for labeling the promise.
  511. Useful for tooling.
  512. @return {Promise} promise that is fulfilled when all `promises` have been
  513. fulfilled, or rejected if any of them become rejected.
  514. @static
  515. */
  516. function all(entries) {
  517. return new Enumerator(this, entries).promise;
  518. }
  519. /**
  520. `Promise.race` returns a new promise which is settled in the same way as the
  521. first passed promise to settle.
  522. Example:
  523. ```javascript
  524. let promise1 = new Promise(function(resolve, reject){
  525. setTimeout(function(){
  526. resolve('promise 1');
  527. }, 200);
  528. });
  529. let promise2 = new Promise(function(resolve, reject){
  530. setTimeout(function(){
  531. resolve('promise 2');
  532. }, 100);
  533. });
  534. Promise.race([promise1, promise2]).then(function(result){
  535. // result === 'promise 2' because it was resolved before promise1
  536. // was resolved.
  537. });
  538. ```
  539. `Promise.race` is deterministic in that only the state of the first
  540. settled promise matters. For example, even if other promises given to the
  541. `promises` array argument are resolved, but the first settled promise has
  542. become rejected before the other promises became fulfilled, the returned
  543. promise will become rejected:
  544. ```javascript
  545. let promise1 = new Promise(function(resolve, reject){
  546. setTimeout(function(){
  547. resolve('promise 1');
  548. }, 200);
  549. });
  550. let promise2 = new Promise(function(resolve, reject){
  551. setTimeout(function(){
  552. reject(new Error('promise 2'));
  553. }, 100);
  554. });
  555. Promise.race([promise1, promise2]).then(function(result){
  556. // Code here never runs
  557. }, function(reason){
  558. // reason.message === 'promise 2' because promise 2 became rejected before
  559. // promise 1 became fulfilled
  560. });
  561. ```
  562. An example real-world use case is implementing timeouts:
  563. ```javascript
  564. Promise.race([ajax('foo.json'), timeout(5000)])
  565. ```
  566. @method race
  567. @static
  568. @param {Array} promises array of promises to observe
  569. Useful for tooling.
  570. @return {Promise} a promise which settles in the same way as the first passed
  571. promise to settle.
  572. */
  573. function race(entries) {
  574. /*jshint validthis:true */
  575. var Constructor = this;
  576. if (!isArray(entries)) {
  577. return new Constructor(function (_, reject) {
  578. return reject(new TypeError('You must pass an array to race.'));
  579. });
  580. } else {
  581. return new Constructor(function (resolve, reject) {
  582. var length = entries.length;
  583. for (var i = 0; i < length; i++) {
  584. Constructor.resolve(entries[i]).then(resolve, reject);
  585. }
  586. });
  587. }
  588. }
  589. /**
  590. `Promise.reject` returns a promise rejected with the passed `reason`.
  591. It is shorthand for the following:
  592. ```javascript
  593. let promise = new Promise(function(resolve, reject){
  594. reject(new Error('WHOOPS'));
  595. });
  596. promise.then(function(value){
  597. // Code here doesn't run because the promise is rejected!
  598. }, function(reason){
  599. // reason.message === 'WHOOPS'
  600. });
  601. ```
  602. Instead of writing the above, your code now simply becomes the following:
  603. ```javascript
  604. let promise = Promise.reject(new Error('WHOOPS'));
  605. promise.then(function(value){
  606. // Code here doesn't run because the promise is rejected!
  607. }, function(reason){
  608. // reason.message === 'WHOOPS'
  609. });
  610. ```
  611. @method reject
  612. @static
  613. @param {Any} reason value that the returned promise will be rejected with.
  614. Useful for tooling.
  615. @return {Promise} a promise rejected with the given `reason`.
  616. */
  617. function reject$1(reason) {
  618. /*jshint validthis:true */
  619. var Constructor = this;
  620. var promise = new Constructor(noop);
  621. reject(promise, reason);
  622. return promise;
  623. }
  624. function needsResolver() {
  625. throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
  626. }
  627. function needsNew() {
  628. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  629. }
  630. /**
  631. Promise objects represent the eventual result of an asynchronous operation. The
  632. primary way of interacting with a promise is through its `then` method, which
  633. registers callbacks to receive either a promise's eventual value or the reason
  634. why the promise cannot be fulfilled.
  635. Terminology
  636. -----------
  637. - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
  638. - `thenable` is an object or function that defines a `then` method.
  639. - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
  640. - `exception` is a value that is thrown using the throw statement.
  641. - `reason` is a value that indicates why a promise was rejected.
  642. - `settled` the final resting state of a promise, fulfilled or rejected.
  643. A promise can be in one of three states: pending, fulfilled, or rejected.
  644. Promises that are fulfilled have a fulfillment value and are in the fulfilled
  645. state. Promises that are rejected have a rejection reason and are in the
  646. rejected state. A fulfillment value is never a thenable.
  647. Promises can also be said to *resolve* a value. If this value is also a
  648. promise, then the original promise's settled state will match the value's
  649. settled state. So a promise that *resolves* a promise that rejects will
  650. itself reject, and a promise that *resolves* a promise that fulfills will
  651. itself fulfill.
  652. Basic Usage:
  653. ------------
  654. ```js
  655. let promise = new Promise(function(resolve, reject) {
  656. // on success
  657. resolve(value);
  658. // on failure
  659. reject(reason);
  660. });
  661. promise.then(function(value) {
  662. // on fulfillment
  663. }, function(reason) {
  664. // on rejection
  665. });
  666. ```
  667. Advanced Usage:
  668. ---------------
  669. Promises shine when abstracting away asynchronous interactions such as
  670. `XMLHttpRequest`s.
  671. ```js
  672. function getJSON(url) {
  673. return new Promise(function(resolve, reject){
  674. let xhr = new XMLHttpRequest();
  675. xhr.open('GET', url);
  676. xhr.onreadystatechange = handler;
  677. xhr.responseType = 'json';
  678. xhr.setRequestHeader('Accept', 'application/json');
  679. xhr.send();
  680. function handler() {
  681. if (this.readyState === this.DONE) {
  682. if (this.status === 200) {
  683. resolve(this.response);
  684. } else {
  685. reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
  686. }
  687. }
  688. };
  689. });
  690. }
  691. getJSON('/posts.json').then(function(json) {
  692. // on fulfillment
  693. }, function(reason) {
  694. // on rejection
  695. });
  696. ```
  697. Unlike callbacks, promises are great composable primitives.
  698. ```js
  699. Promise.all([
  700. getJSON('/posts'),
  701. getJSON('/comments')
  702. ]).then(function(values){
  703. values[0] // => postsJSON
  704. values[1] // => commentsJSON
  705. return values;
  706. });
  707. ```
  708. @class Promise
  709. @param {Function} resolver
  710. Useful for tooling.
  711. @constructor
  712. */
  713. var Promise$1 = function () {
  714. function Promise(resolver) {
  715. this[PROMISE_ID] = nextId();
  716. this._result = this._state = undefined;
  717. this._subscribers = [];
  718. if (noop !== resolver) {
  719. typeof resolver !== 'function' && needsResolver();
  720. this instanceof Promise ? initializePromise(this, resolver) : needsNew();
  721. }
  722. }
  723. /**
  724. The primary way of interacting with a promise is through its `then` method,
  725. which registers callbacks to receive either a promise's eventual value or the
  726. reason why the promise cannot be fulfilled.
  727. ```js
  728. findUser().then(function(user){
  729. // user is available
  730. }, function(reason){
  731. // user is unavailable, and you are given the reason why
  732. });
  733. ```
  734. Chaining
  735. --------
  736. The return value of `then` is itself a promise. This second, 'downstream'
  737. promise is resolved with the return value of the first promise's fulfillment
  738. or rejection handler, or rejected if the handler throws an exception.
  739. ```js
  740. findUser().then(function (user) {
  741. return user.name;
  742. }, function (reason) {
  743. return 'default name';
  744. }).then(function (userName) {
  745. // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
  746. // will be `'default name'`
  747. });
  748. findUser().then(function (user) {
  749. throw new Error('Found user, but still unhappy');
  750. }, function (reason) {
  751. throw new Error('`findUser` rejected and we're unhappy');
  752. }).then(function (value) {
  753. // never reached
  754. }, function (reason) {
  755. // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
  756. // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
  757. });
  758. ```
  759. If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
  760. ```js
  761. findUser().then(function (user) {
  762. throw new PedagogicalException('Upstream error');
  763. }).then(function (value) {
  764. // never reached
  765. }).then(function (value) {
  766. // never reached
  767. }, function (reason) {
  768. // The `PedgagocialException` is propagated all the way down to here
  769. });
  770. ```
  771. Assimilation
  772. ------------
  773. Sometimes the value you want to propagate to a downstream promise can only be
  774. retrieved asynchronously. This can be achieved by returning a promise in the
  775. fulfillment or rejection handler. The downstream promise will then be pending
  776. until the returned promise is settled. This is called *assimilation*.
  777. ```js
  778. findUser().then(function (user) {
  779. return findCommentsByAuthor(user);
  780. }).then(function (comments) {
  781. // The user's comments are now available
  782. });
  783. ```
  784. If the assimliated promise rejects, then the downstream promise will also reject.
  785. ```js
  786. findUser().then(function (user) {
  787. return findCommentsByAuthor(user);
  788. }).then(function (comments) {
  789. // If `findCommentsByAuthor` fulfills, we'll have the value here
  790. }, function (reason) {
  791. // If `findCommentsByAuthor` rejects, we'll have the reason here
  792. });
  793. ```
  794. Simple Example
  795. --------------
  796. Synchronous Example
  797. ```javascript
  798. let result;
  799. try {
  800. result = findResult();
  801. // success
  802. } catch(reason) {
  803. // failure
  804. }
  805. ```
  806. Errback Example
  807. ```js
  808. findResult(function(result, err){
  809. if (err) {
  810. // failure
  811. } else {
  812. // success
  813. }
  814. });
  815. ```
  816. Promise Example;
  817. ```javascript
  818. findResult().then(function(result){
  819. // success
  820. }, function(reason){
  821. // failure
  822. });
  823. ```
  824. Advanced Example
  825. --------------
  826. Synchronous Example
  827. ```javascript
  828. let author, books;
  829. try {
  830. author = findAuthor();
  831. books = findBooksByAuthor(author);
  832. // success
  833. } catch(reason) {
  834. // failure
  835. }
  836. ```
  837. Errback Example
  838. ```js
  839. function foundBooks(books) {
  840. }
  841. function failure(reason) {
  842. }
  843. findAuthor(function(author, err){
  844. if (err) {
  845. failure(err);
  846. // failure
  847. } else {
  848. try {
  849. findBoooksByAuthor(author, function(books, err) {
  850. if (err) {
  851. failure(err);
  852. } else {
  853. try {
  854. foundBooks(books);
  855. } catch(reason) {
  856. failure(reason);
  857. }
  858. }
  859. });
  860. } catch(error) {
  861. failure(err);
  862. }
  863. // success
  864. }
  865. });
  866. ```
  867. Promise Example;
  868. ```javascript
  869. findAuthor().
  870. then(findBooksByAuthor).
  871. then(function(books){
  872. // found books
  873. }).catch(function(reason){
  874. // something went wrong
  875. });
  876. ```
  877. @method then
  878. @param {Function} onFulfilled
  879. @param {Function} onRejected
  880. Useful for tooling.
  881. @return {Promise}
  882. */
  883. /**
  884. `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
  885. as the catch block of a try/catch statement.
  886. ```js
  887. function findAuthor(){
  888. throw new Error('couldn't find that author');
  889. }
  890. // synchronous
  891. try {
  892. findAuthor();
  893. } catch(reason) {
  894. // something went wrong
  895. }
  896. // async with promises
  897. findAuthor().catch(function(reason){
  898. // something went wrong
  899. });
  900. ```
  901. @method catch
  902. @param {Function} onRejection
  903. Useful for tooling.
  904. @return {Promise}
  905. */
  906. Promise.prototype.catch = function _catch(onRejection) {
  907. return this.then(null, onRejection);
  908. };
  909. /**
  910. `finally` will be invoked regardless of the promise's fate just as native
  911. try/catch/finally behaves
  912. Synchronous example:
  913. ```js
  914. findAuthor() {
  915. if (Math.random() > 0.5) {
  916. throw new Error();
  917. }
  918. return new Author();
  919. }
  920. try {
  921. return findAuthor(); // succeed or fail
  922. } catch(error) {
  923. return findOtherAuther();
  924. } finally {
  925. // always runs
  926. // doesn't affect the return value
  927. }
  928. ```
  929. Asynchronous example:
  930. ```js
  931. findAuthor().catch(function(reason){
  932. return findOtherAuther();
  933. }).finally(function(){
  934. // author was either found, or not
  935. });
  936. ```
  937. @method finally
  938. @param {Function} callback
  939. @return {Promise}
  940. */
  941. Promise.prototype.finally = function _finally(callback) {
  942. var promise = this;
  943. var constructor = promise.constructor;
  944. return promise.then(function (value) {
  945. return constructor.resolve(callback()).then(function () {
  946. return value;
  947. });
  948. }, function (reason) {
  949. return constructor.resolve(callback()).then(function () {
  950. throw reason;
  951. });
  952. });
  953. };
  954. return Promise;
  955. }();
  956. Promise$1.prototype.then = then;
  957. Promise$1.all = all;
  958. Promise$1.race = race;
  959. Promise$1.resolve = resolve$1;
  960. Promise$1.reject = reject$1;
  961. Promise$1._setScheduler = setScheduler;
  962. Promise$1._setAsap = setAsap;
  963. Promise$1._asap = asap;
  964. /*global self*/
  965. function polyfill() {
  966. var local = void 0;
  967. if (typeof global !== 'undefined') {
  968. local = global;
  969. } else if (typeof self !== 'undefined') {
  970. local = self;
  971. } else {
  972. try {
  973. local = Function('return this')();
  974. } catch (e) {
  975. throw new Error('polyfill failed because global object is unavailable in this environment');
  976. }
  977. }
  978. var P = local.Promise;
  979. if (P) {
  980. var promiseToString = null;
  981. try {
  982. promiseToString = Object.prototype.toString.call(P.resolve());
  983. } catch (e) {
  984. // silently ignored
  985. }
  986. if (promiseToString === '[object Promise]' && !P.cast) {
  987. return;
  988. }
  989. }
  990. local.Promise = Promise$1;
  991. }
  992. // Strange compat..
  993. Promise$1.polyfill = polyfill;
  994. Promise$1.Promise = Promise$1;
  995. return Promise$1;
  996. })));
  997. }).call(this,_dereq_('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  998. },{"_process":3}],2:[function(_dereq_,module,exports){
  999. // Copyright Joyent, Inc. and other Node contributors.
  1000. //
  1001. // Permission is hereby granted, free of charge, to any person obtaining a
  1002. // copy of this software and associated documentation files (the
  1003. // "Software"), to deal in the Software without restriction, including
  1004. // without limitation the rights to use, copy, modify, merge, publish,
  1005. // distribute, sublicense, and/or sell copies of the Software, and to permit
  1006. // persons to whom the Software is furnished to do so, subject to the
  1007. // following conditions:
  1008. //
  1009. // The above copyright notice and this permission notice shall be included
  1010. // in all copies or substantial portions of the Software.
  1011. //
  1012. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  1013. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1014. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  1015. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  1016. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  1017. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  1018. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  1019. function EventEmitter() {
  1020. this._events = this._events || {};
  1021. this._maxListeners = this._maxListeners || undefined;
  1022. }
  1023. module.exports = EventEmitter;
  1024. // Backwards-compat with node 0.10.x
  1025. EventEmitter.EventEmitter = EventEmitter;
  1026. EventEmitter.prototype._events = undefined;
  1027. EventEmitter.prototype._maxListeners = undefined;
  1028. // By default EventEmitters will print a warning if more than 10 listeners are
  1029. // added to it. This is a useful default which helps finding memory leaks.
  1030. EventEmitter.defaultMaxListeners = 10;
  1031. // Obviously not all Emitters should be limited to 10. This function allows
  1032. // that to be increased. Set to zero for unlimited.
  1033. EventEmitter.prototype.setMaxListeners = function(n) {
  1034. if (!isNumber(n) || n < 0 || isNaN(n))
  1035. throw TypeError('n must be a positive number');
  1036. this._maxListeners = n;
  1037. return this;
  1038. };
  1039. EventEmitter.prototype.emit = function(type) {
  1040. var er, handler, len, args, i, listeners;
  1041. if (!this._events)
  1042. this._events = {};
  1043. // If there is no 'error' event listener then throw.
  1044. if (type === 'error') {
  1045. if (!this._events.error ||
  1046. (isObject(this._events.error) && !this._events.error.length)) {
  1047. er = arguments[1];
  1048. if (er instanceof Error) {
  1049. throw er; // Unhandled 'error' event
  1050. } else {
  1051. // At least give some kind of context to the user
  1052. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  1053. err.context = er;
  1054. throw err;
  1055. }
  1056. }
  1057. }
  1058. handler = this._events[type];
  1059. if (isUndefined(handler))
  1060. return false;
  1061. if (isFunction(handler)) {
  1062. switch (arguments.length) {
  1063. // fast cases
  1064. case 1:
  1065. handler.call(this);
  1066. break;
  1067. case 2:
  1068. handler.call(this, arguments[1]);
  1069. break;
  1070. case 3:
  1071. handler.call(this, arguments[1], arguments[2]);
  1072. break;
  1073. // slower
  1074. default:
  1075. args = Array.prototype.slice.call(arguments, 1);
  1076. handler.apply(this, args);
  1077. }
  1078. } else if (isObject(handler)) {
  1079. args = Array.prototype.slice.call(arguments, 1);
  1080. listeners = handler.slice();
  1081. len = listeners.length;
  1082. for (i = 0; i < len; i++)
  1083. listeners[i].apply(this, args);
  1084. }
  1085. return true;
  1086. };
  1087. EventEmitter.prototype.addListener = function(type, listener) {
  1088. var m;
  1089. if (!isFunction(listener))
  1090. throw TypeError('listener must be a function');
  1091. if (!this._events)
  1092. this._events = {};
  1093. // To avoid recursion in the case that type === "newListener"! Before
  1094. // adding it to the listeners, first emit "newListener".
  1095. if (this._events.newListener)
  1096. this.emit('newListener', type,
  1097. isFunction(listener.listener) ?
  1098. listener.listener : listener);
  1099. if (!this._events[type])
  1100. // Optimize the case of one listener. Don't need the extra array object.
  1101. this._events[type] = listener;
  1102. else if (isObject(this._events[type]))
  1103. // If we've already got an array, just append.
  1104. this._events[type].push(listener);
  1105. else
  1106. // Adding the second element, need to change to array.
  1107. this._events[type] = [this._events[type], listener];
  1108. // Check for listener leak
  1109. if (isObject(this._events[type]) && !this._events[type].warned) {
  1110. if (!isUndefined(this._maxListeners)) {
  1111. m = this._maxListeners;
  1112. } else {
  1113. m = EventEmitter.defaultMaxListeners;
  1114. }
  1115. if (m && m > 0 && this._events[type].length > m) {
  1116. this._events[type].warned = true;
  1117. console.error('(node) warning: possible EventEmitter memory ' +
  1118. 'leak detected. %d listeners added. ' +
  1119. 'Use emitter.setMaxListeners() to increase limit.',
  1120. this._events[type].length);
  1121. if (typeof console.trace === 'function') {
  1122. // not supported in IE 10
  1123. console.trace();
  1124. }
  1125. }
  1126. }
  1127. return this;
  1128. };
  1129. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  1130. EventEmitter.prototype.once = function(type, listener) {
  1131. if (!isFunction(listener))
  1132. throw TypeError('listener must be a function');
  1133. var fired = false;
  1134. function g() {
  1135. this.removeListener(type, g);
  1136. if (!fired) {
  1137. fired = true;
  1138. listener.apply(this, arguments);
  1139. }
  1140. }
  1141. g.listener = listener;
  1142. this.on(type, g);
  1143. return this;
  1144. };
  1145. // emits a 'removeListener' event iff the listener was removed
  1146. EventEmitter.prototype.removeListener = function(type, listener) {
  1147. var list, position, length, i;
  1148. if (!isFunction(listener))
  1149. throw TypeError('listener must be a function');
  1150. if (!this._events || !this._events[type])
  1151. return this;
  1152. list = this._events[type];
  1153. length = list.length;
  1154. position = -1;
  1155. if (list === listener ||
  1156. (isFunction(list.listener) && list.listener === listener)) {
  1157. delete this._events[type];
  1158. if (this._events.removeListener)
  1159. this.emit('removeListener', type, listener);
  1160. } else if (isObject(list)) {
  1161. for (i = length; i-- > 0;) {
  1162. if (list[i] === listener ||
  1163. (list[i].listener && list[i].listener === listener)) {
  1164. position = i;
  1165. break;
  1166. }
  1167. }
  1168. if (position < 0)
  1169. return this;
  1170. if (list.length === 1) {
  1171. list.length = 0;
  1172. delete this._events[type];
  1173. } else {
  1174. list.splice(position, 1);
  1175. }
  1176. if (this._events.removeListener)
  1177. this.emit('removeListener', type, listener);
  1178. }
  1179. return this;
  1180. };
  1181. EventEmitter.prototype.removeAllListeners = function(type) {
  1182. var key, listeners;
  1183. if (!this._events)
  1184. return this;
  1185. // not listening for removeListener, no need to emit
  1186. if (!this._events.removeListener) {
  1187. if (arguments.length === 0)
  1188. this._events = {};
  1189. else if (this._events[type])
  1190. delete this._events[type];
  1191. return this;
  1192. }
  1193. // emit removeListener for all listeners on all events
  1194. if (arguments.length === 0) {
  1195. for (key in this._events) {
  1196. if (key === 'removeListener') continue;
  1197. this.removeAllListeners(key);
  1198. }
  1199. this.removeAllListeners('removeListener');
  1200. this._events = {};
  1201. return this;
  1202. }
  1203. listeners = this._events[type];
  1204. if (isFunction(listeners)) {
  1205. this.removeListener(type, listeners);
  1206. } else if (listeners) {
  1207. // LIFO order
  1208. while (listeners.length)
  1209. this.removeListener(type, listeners[listeners.length - 1]);
  1210. }
  1211. delete this._events[type];
  1212. return this;
  1213. };
  1214. EventEmitter.prototype.listeners = function(type) {
  1215. var ret;
  1216. if (!this._events || !this._events[type])
  1217. ret = [];
  1218. else if (isFunction(this._events[type]))
  1219. ret = [this._events[type]];
  1220. else
  1221. ret = this._events[type].slice();
  1222. return ret;
  1223. };
  1224. EventEmitter.prototype.listenerCount = function(type) {
  1225. if (this._events) {
  1226. var evlistener = this._events[type];
  1227. if (isFunction(evlistener))
  1228. return 1;
  1229. else if (evlistener)
  1230. return evlistener.length;
  1231. }
  1232. return 0;
  1233. };
  1234. EventEmitter.listenerCount = function(emitter, type) {
  1235. return emitter.listenerCount(type);
  1236. };
  1237. function isFunction(arg) {
  1238. return typeof arg === 'function';
  1239. }
  1240. function isNumber(arg) {
  1241. return typeof arg === 'number';
  1242. }
  1243. function isObject(arg) {
  1244. return typeof arg === 'object' && arg !== null;
  1245. }
  1246. function isUndefined(arg) {
  1247. return arg === void 0;
  1248. }
  1249. },{}],3:[function(_dereq_,module,exports){
  1250. // shim for using process in browser
  1251. var process = module.exports = {};
  1252. // cached from whatever global is present so that test runners that stub it
  1253. // don't break things. But we need to wrap it in a try catch in case it is
  1254. // wrapped in strict mode code which doesn't define any globals. It's inside a
  1255. // function because try/catches deoptimize in certain engines.
  1256. var cachedSetTimeout;
  1257. var cachedClearTimeout;
  1258. function defaultSetTimout() {
  1259. throw new Error('setTimeout has not been defined');
  1260. }
  1261. function defaultClearTimeout () {
  1262. throw new Error('clearTimeout has not been defined');
  1263. }
  1264. (function () {
  1265. try {
  1266. if (typeof setTimeout === 'function') {
  1267. cachedSetTimeout = setTimeout;
  1268. } else {
  1269. cachedSetTimeout = defaultSetTimout;
  1270. }
  1271. } catch (e) {
  1272. cachedSetTimeout = defaultSetTimout;
  1273. }
  1274. try {
  1275. if (typeof clearTimeout === 'function') {
  1276. cachedClearTimeout = clearTimeout;
  1277. } else {
  1278. cachedClearTimeout = defaultClearTimeout;
  1279. }
  1280. } catch (e) {
  1281. cachedClearTimeout = defaultClearTimeout;
  1282. }
  1283. } ())
  1284. function runTimeout(fun) {
  1285. if (cachedSetTimeout === setTimeout) {
  1286. //normal enviroments in sane situations
  1287. return setTimeout(fun, 0);
  1288. }
  1289. // if setTimeout wasn't available but was latter defined
  1290. if ((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  1291. cachedSetTimeout = setTimeout;
  1292. return setTimeout(fun, 0);
  1293. }
  1294. try {
  1295. // when when somebody has screwed with setTimeout but no I.E. maddness
  1296. return cachedSetTimeout(fun, 0);
  1297. } catch(e){
  1298. try {
  1299. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1300. return cachedSetTimeout.call(null, fun, 0);
  1301. } catch(e){
  1302. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error
  1303. return cachedSetTimeout.call(this, fun, 0);
  1304. }
  1305. }
  1306. }
  1307. function runClearTimeout(marker) {
  1308. if (cachedClearTimeout === clearTimeout) {
  1309. //normal enviroments in sane situations
  1310. return clearTimeout(marker);
  1311. }
  1312. // if clearTimeout wasn't available but was latter defined
  1313. if ((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  1314. cachedClearTimeout = clearTimeout;
  1315. return clearTimeout(marker);
  1316. }
  1317. try {
  1318. // when when somebody has screwed with setTimeout but no I.E. maddness
  1319. return cachedClearTimeout(marker);
  1320. } catch (e){
  1321. try {
  1322. // When we are in I.E. but the script has been evaled so I.E. doesn't trust the global object when called normally
  1323. return cachedClearTimeout.call(null, marker);
  1324. } catch (e){
  1325. // same as above but when it's a version of I.E. that must have the global object for 'this', hopfully our context correct otherwise it will throw a global error.
  1326. // Some versions of I.E. have different rules for clearTimeout vs setTimeout
  1327. return cachedClearTimeout.call(this, marker);
  1328. }
  1329. }
  1330. }
  1331. var queue = [];
  1332. var draining = false;
  1333. var currentQueue;
  1334. var queueIndex = -1;
  1335. function cleanUpNextTick() {
  1336. if (!draining || !currentQueue) {
  1337. return;
  1338. }
  1339. draining = false;
  1340. if (currentQueue.length) {
  1341. queue = currentQueue.concat(queue);
  1342. } else {
  1343. queueIndex = -1;
  1344. }
  1345. if (queue.length) {
  1346. drainQueue();
  1347. }
  1348. }
  1349. function drainQueue() {
  1350. if (draining) {
  1351. return;
  1352. }
  1353. var timeout = runTimeout(cleanUpNextTick);
  1354. draining = true;
  1355. var len = queue.length;
  1356. while(len) {
  1357. currentQueue = queue;
  1358. queue = [];
  1359. while (++queueIndex < len) {
  1360. if (currentQueue) {
  1361. currentQueue[queueIndex].run();
  1362. }
  1363. }
  1364. queueIndex = -1;
  1365. len = queue.length;
  1366. }
  1367. currentQueue = null;
  1368. draining = false;
  1369. runClearTimeout(timeout);
  1370. }
  1371. process.nextTick = function (fun) {
  1372. var args = new Array(arguments.length - 1);
  1373. if (arguments.length > 1) {
  1374. for (var i = 1; i < arguments.length; i++) {
  1375. args[i - 1] = arguments[i];
  1376. }
  1377. }
  1378. queue.push(new Item(fun, args));
  1379. if (queue.length === 1 && !draining) {
  1380. runTimeout(drainQueue);
  1381. }
  1382. };
  1383. // v8 likes predictible objects
  1384. function Item(fun, array) {
  1385. this.fun = fun;
  1386. this.array = array;
  1387. }
  1388. Item.prototype.run = function () {
  1389. this.fun.apply(null, this.array);
  1390. };
  1391. process.title = 'browser';
  1392. process.browser = true;
  1393. process.env = {};
  1394. process.argv = [];
  1395. process.version = ''; // empty string to avoid regexp issues
  1396. process.versions = {};
  1397. function noop() {}
  1398. process.on = noop;
  1399. process.addListener = noop;
  1400. process.once = noop;
  1401. process.off = noop;
  1402. process.removeListener = noop;
  1403. process.removeAllListeners = noop;
  1404. process.emit = noop;
  1405. process.prependListener = noop;
  1406. process.prependOnceListener = noop;
  1407. process.listeners = function (name) { return [] }
  1408. process.binding = function (name) {
  1409. throw new Error('process.binding is not supported');
  1410. };
  1411. process.cwd = function () { return '/' };
  1412. process.chdir = function (dir) {
  1413. throw new Error('process.chdir is not supported');
  1414. };
  1415. process.umask = function() { return 0; };
  1416. },{}],4:[function(_dereq_,module,exports){
  1417. var bundleFn = arguments[3];
  1418. var sources = arguments[4];
  1419. var cache = arguments[5];
  1420. var stringify = JSON.stringify;
  1421. module.exports = function (fn, options) {
  1422. var wkey;
  1423. var cacheKeys = Object.keys(cache);
  1424. for (var i = 0, l = cacheKeys.length; i < l; i++) {
  1425. var key = cacheKeys[i];
  1426. var exp = cache[key].exports;
  1427. // Using babel as a transpiler to use esmodule, the export will always
  1428. // be an object with the default export as a property of it. To ensure
  1429. // the existing api and babel esmodule exports are both supported we
  1430. // check for both
  1431. if (exp === fn || exp && exp.default === fn) {
  1432. wkey = key;
  1433. break;
  1434. }
  1435. }
  1436. if (!wkey) {
  1437. wkey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16);
  1438. var wcache = {};
  1439. for (var i = 0, l = cacheKeys.length; i < l; i++) {
  1440. var key = cacheKeys[i];
  1441. wcache[key] = key;
  1442. }
  1443. sources[wkey] = [
  1444. 'function(require,module,exports){' + fn + '(self); }',
  1445. wcache
  1446. ];
  1447. }
  1448. var skey = Math.floor(Math.pow(16, 8) * Math.random()).toString(16);
  1449. var scache = {}; scache[wkey] = wkey;
  1450. sources[skey] = [
  1451. 'function(require,module,exports){' +
  1452. // try to call default if defined to also support babel esmodule exports
  1453. 'var f = require(' + stringify(wkey) + ');' +
  1454. '(f.default ? f.default : f)(self);' +
  1455. '}',
  1456. scache
  1457. ];
  1458. var workerSources = {};
  1459. resolveSources(skey);
  1460. function resolveSources(key) {
  1461. workerSources[key] = true;
  1462. for (var depPath in sources[key][1]) {
  1463. var depKey = sources[key][1][depPath];
  1464. if (!workerSources[depKey]) {
  1465. resolveSources(depKey);
  1466. }
  1467. }
  1468. }
  1469. var src = '(' + bundleFn + ')({'
  1470. + Object.keys(workerSources).map(function (key) {
  1471. return stringify(key) + ':['
  1472. + sources[key][0]
  1473. + ',' + stringify(sources[key][1]) + ']'
  1474. ;
  1475. }).join(',')
  1476. + '},{},[' + stringify(skey) + '])'
  1477. ;
  1478. var URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
  1479. var blob = new Blob([src], { type: 'text/javascript' });
  1480. if (options && options.bare) { return blob; }
  1481. var workerUrl = URL.createObjectURL(blob);
  1482. var worker = new Worker(workerUrl);
  1483. worker.objectURL = workerUrl;
  1484. return worker;
  1485. };
  1486. },{}],5:[function(_dereq_,module,exports){
  1487. 'use strict';
  1488. Object.defineProperty(exports, "__esModule", {
  1489. value: true
  1490. });
  1491. exports.createDefaultConfig = createDefaultConfig;
  1492. /*
  1493. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  1494. *
  1495. * @author zheng qian <xqq@xqq.im>
  1496. *
  1497. * Licensed under the Apache License, Version 2.0 (the "License");
  1498. * you may not use this file except in compliance with the License.
  1499. * You may obtain a copy of the License at
  1500. *
  1501. * http://www.apache.org/licenses/LICENSE-2.0
  1502. *
  1503. * Unless required by applicable law or agreed to in writing, software
  1504. * distributed under the License is distributed on an "AS IS" BASIS,
  1505. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1506. * See the License for the specific language governing permissions and
  1507. * limitations under the License.
  1508. */
  1509. var defaultConfig = exports.defaultConfig = {
  1510. enableWorker: false,
  1511. enableStashBuffer: true,
  1512. stashInitialSize: undefined,
  1513. isLive: false,
  1514. lazyLoad: true,
  1515. lazyLoadMaxDuration: 3 * 60,
  1516. lazyLoadRecoverDuration: 30,
  1517. deferLoadAfterSourceOpen: true,
  1518. // autoCleanupSourceBuffer: default as false, leave unspecified
  1519. autoCleanupMaxBackwardDuration: 3 * 60,
  1520. autoCleanupMinBackwardDuration: 2 * 60,
  1521. statisticsInfoReportInterval: 600,
  1522. fixAudioTimestampGap: true,
  1523. accurateSeek: false,
  1524. seekType: 'range', // [range, param, custom]
  1525. seekParamStart: 'bstart',
  1526. seekParamEnd: 'bend',
  1527. rangeLoadZeroStart: false,
  1528. customSeekHandler: undefined,
  1529. reuseRedirectedURL: false,
  1530. // referrerPolicy: leave as unspecified
  1531. customLoader: undefined
  1532. };
  1533. function createDefaultConfig() {
  1534. return Object.assign({}, defaultConfig);
  1535. }
  1536. },{}],6:[function(_dereq_,module,exports){
  1537. 'use strict';
  1538. Object.defineProperty(exports, "__esModule", {
  1539. value: true
  1540. });
  1541. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  1542. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  1543. *
  1544. * @author zheng qian <xqq@xqq.im>
  1545. *
  1546. * Licensed under the Apache License, Version 2.0 (the "License");
  1547. * you may not use this file except in compliance with the License.
  1548. * You may obtain a copy of the License at
  1549. *
  1550. * http://www.apache.org/licenses/LICENSE-2.0
  1551. *
  1552. * Unless required by applicable law or agreed to in writing, software
  1553. * distributed under the License is distributed on an "AS IS" BASIS,
  1554. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1555. * See the License for the specific language governing permissions and
  1556. * limitations under the License.
  1557. */
  1558. var _ioController = _dereq_('../io/io-controller.js');
  1559. var _ioController2 = _interopRequireDefault(_ioController);
  1560. var _config = _dereq_('../config.js');
  1561. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  1562. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1563. var Features = function () {
  1564. function Features() {
  1565. _classCallCheck(this, Features);
  1566. }
  1567. _createClass(Features, null, [{
  1568. key: 'supportMSEH264Playback',
  1569. value: function supportMSEH264Playback() {
  1570. return window.MediaSource && window.MediaSource.isTypeSupported('video/mp4; codecs="avc1.42E01E,mp4a.40.2"');
  1571. }
  1572. }, {
  1573. key: 'supportNetworkStreamIO',
  1574. value: function supportNetworkStreamIO() {
  1575. var ioctl = new _ioController2.default({}, (0, _config.createDefaultConfig)());
  1576. var loaderType = ioctl.loaderType;
  1577. ioctl.destroy();
  1578. return loaderType == 'fetch-stream-loader' || loaderType == 'xhr-moz-chunked-loader';
  1579. }
  1580. }, {
  1581. key: 'getNetworkLoaderTypeName',
  1582. value: function getNetworkLoaderTypeName() {
  1583. var ioctl = new _ioController2.default({}, (0, _config.createDefaultConfig)());
  1584. var loaderType = ioctl.loaderType;
  1585. ioctl.destroy();
  1586. return loaderType;
  1587. }
  1588. }, {
  1589. key: 'supportNativeMediaPlayback',
  1590. value: function supportNativeMediaPlayback(mimeType) {
  1591. if (Features.videoElement == undefined) {
  1592. Features.videoElement = window.document.createElement('video');
  1593. }
  1594. var canPlay = Features.videoElement.canPlayType(mimeType);
  1595. return canPlay === 'probably' || canPlay == 'maybe';
  1596. }
  1597. }, {
  1598. key: 'getFeatureList',
  1599. value: function getFeatureList() {
  1600. var features = {
  1601. mseFlvPlayback: false,
  1602. mseLiveFlvPlayback: false,
  1603. networkStreamIO: false,
  1604. networkLoaderName: '',
  1605. nativeMP4H264Playback: false,
  1606. nativeWebmVP8Playback: false,
  1607. nativeWebmVP9Playback: false
  1608. };
  1609. features.mseFlvPlayback = Features.supportMSEH264Playback();
  1610. features.networkStreamIO = Features.supportNetworkStreamIO();
  1611. features.networkLoaderName = Features.getNetworkLoaderTypeName();
  1612. features.mseLiveFlvPlayback = features.mseFlvPlayback && features.networkStreamIO;
  1613. features.nativeMP4H264Playback = Features.supportNativeMediaPlayback('video/mp4; codecs="avc1.42001E, mp4a.40.2"');
  1614. features.nativeWebmVP8Playback = Features.supportNativeMediaPlayback('video/webm; codecs="vp8.0, vorbis"');
  1615. features.nativeWebmVP9Playback = Features.supportNativeMediaPlayback('video/webm; codecs="vp9"');
  1616. return features;
  1617. }
  1618. }]);
  1619. return Features;
  1620. }();
  1621. exports.default = Features;
  1622. },{"../config.js":5,"../io/io-controller.js":23}],7:[function(_dereq_,module,exports){
  1623. "use strict";
  1624. Object.defineProperty(exports, "__esModule", {
  1625. value: true
  1626. });
  1627. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  1628. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1629. /*
  1630. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  1631. *
  1632. * @author zheng qian <xqq@xqq.im>
  1633. *
  1634. * Licensed under the Apache License, Version 2.0 (the "License");
  1635. * you may not use this file except in compliance with the License.
  1636. * You may obtain a copy of the License at
  1637. *
  1638. * http://www.apache.org/licenses/LICENSE-2.0
  1639. *
  1640. * Unless required by applicable law or agreed to in writing, software
  1641. * distributed under the License is distributed on an "AS IS" BASIS,
  1642. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1643. * See the License for the specific language governing permissions and
  1644. * limitations under the License.
  1645. */
  1646. var MediaInfo = function () {
  1647. function MediaInfo() {
  1648. _classCallCheck(this, MediaInfo);
  1649. this.mimeType = null;
  1650. this.duration = null;
  1651. this.hasAudio = null;
  1652. this.hasVideo = null;
  1653. this.audioCodec = null;
  1654. this.videoCodec = null;
  1655. this.audioDataRate = null;
  1656. this.videoDataRate = null;
  1657. this.audioSampleRate = null;
  1658. this.audioChannelCount = null;
  1659. this.width = null;
  1660. this.height = null;
  1661. this.fps = null;
  1662. this.profile = null;
  1663. this.level = null;
  1664. this.refFrames = null;
  1665. this.chromaFormat = null;
  1666. this.sarNum = null;
  1667. this.sarDen = null;
  1668. this.metadata = null;
  1669. this.segments = null; // MediaInfo[]
  1670. this.segmentCount = null;
  1671. this.hasKeyframesIndex = null;
  1672. this.keyframesIndex = null;
  1673. }
  1674. _createClass(MediaInfo, [{
  1675. key: "isComplete",
  1676. value: function isComplete() {
  1677. var audioInfoComplete = this.hasAudio === false || this.hasAudio === true && this.audioCodec != null && this.audioSampleRate != null && this.audioChannelCount != null;
  1678. var videoInfoComplete = this.hasVideo === false || this.hasVideo === true && this.videoCodec != null && this.width != null && this.height != null && this.fps != null && this.profile != null && this.level != null && this.refFrames != null && this.chromaFormat != null && this.sarNum != null && this.sarDen != null;
  1679. // keyframesIndex may not be present
  1680. return this.mimeType != null && this.duration != null && this.metadata != null && this.hasKeyframesIndex != null && audioInfoComplete && videoInfoComplete;
  1681. }
  1682. }, {
  1683. key: "isSeekable",
  1684. value: function isSeekable() {
  1685. return this.hasKeyframesIndex === true;
  1686. }
  1687. }, {
  1688. key: "getNearestKeyframe",
  1689. value: function getNearestKeyframe(milliseconds) {
  1690. if (this.keyframesIndex == null) {
  1691. return null;
  1692. }
  1693. var table = this.keyframesIndex;
  1694. var keyframeIdx = this._search(table.times, milliseconds);
  1695. return {
  1696. index: keyframeIdx,
  1697. milliseconds: table.times[keyframeIdx],
  1698. fileposition: table.filepositions[keyframeIdx]
  1699. };
  1700. }
  1701. }, {
  1702. key: "_search",
  1703. value: function _search(list, value) {
  1704. var idx = 0;
  1705. var last = list.length - 1;
  1706. var mid = 0;
  1707. var lbound = 0;
  1708. var ubound = last;
  1709. if (value < list[0]) {
  1710. idx = 0;
  1711. lbound = ubound + 1; // skip search
  1712. }
  1713. while (lbound <= ubound) {
  1714. mid = lbound + Math.floor((ubound - lbound) / 2);
  1715. if (mid === last || value >= list[mid] && value < list[mid + 1]) {
  1716. idx = mid;
  1717. break;
  1718. } else if (list[mid] < value) {
  1719. lbound = mid + 1;
  1720. } else {
  1721. ubound = mid - 1;
  1722. }
  1723. }
  1724. return idx;
  1725. }
  1726. }]);
  1727. return MediaInfo;
  1728. }();
  1729. exports.default = MediaInfo;
  1730. },{}],8:[function(_dereq_,module,exports){
  1731. "use strict";
  1732. Object.defineProperty(exports, "__esModule", {
  1733. value: true
  1734. });
  1735. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  1736. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1737. /*
  1738. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  1739. *
  1740. * @author zheng qian <xqq@xqq.im>
  1741. *
  1742. * Licensed under the Apache License, Version 2.0 (the "License");
  1743. * you may not use this file except in compliance with the License.
  1744. * You may obtain a copy of the License at
  1745. *
  1746. * http://www.apache.org/licenses/LICENSE-2.0
  1747. *
  1748. * Unless required by applicable law or agreed to in writing, software
  1749. * distributed under the License is distributed on an "AS IS" BASIS,
  1750. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1751. * See the License for the specific language governing permissions and
  1752. * limitations under the License.
  1753. */
  1754. // Represents an media sample (audio / video)
  1755. var SampleInfo = exports.SampleInfo = function SampleInfo(dts, pts, duration, originalDts, isSync) {
  1756. _classCallCheck(this, SampleInfo);
  1757. this.dts = dts;
  1758. this.pts = pts;
  1759. this.duration = duration;
  1760. this.originalDts = originalDts;
  1761. this.isSyncPoint = isSync;
  1762. this.fileposition = null;
  1763. };
  1764. // Media Segment concept is defined in Media Source Extensions spec.
  1765. // Particularly in ISO BMFF format, an Media Segment contains a moof box followed by a mdat box.
  1766. var MediaSegmentInfo = exports.MediaSegmentInfo = function () {
  1767. function MediaSegmentInfo() {
  1768. _classCallCheck(this, MediaSegmentInfo);
  1769. this.beginDts = 0;
  1770. this.endDts = 0;
  1771. this.beginPts = 0;
  1772. this.endPts = 0;
  1773. this.originalBeginDts = 0;
  1774. this.originalEndDts = 0;
  1775. this.syncPoints = []; // SampleInfo[n], for video IDR frames only
  1776. this.firstSample = null; // SampleInfo
  1777. this.lastSample = null; // SampleInfo
  1778. }
  1779. _createClass(MediaSegmentInfo, [{
  1780. key: "appendSyncPoint",
  1781. value: function appendSyncPoint(sampleInfo) {
  1782. // also called Random Access Point
  1783. sampleInfo.isSyncPoint = true;
  1784. this.syncPoints.push(sampleInfo);
  1785. }
  1786. }]);
  1787. return MediaSegmentInfo;
  1788. }();
  1789. // Ordered list for recording video IDR frames, sorted by originalDts
  1790. var IDRSampleList = exports.IDRSampleList = function () {
  1791. function IDRSampleList() {
  1792. _classCallCheck(this, IDRSampleList);
  1793. this._list = [];
  1794. }
  1795. _createClass(IDRSampleList, [{
  1796. key: "clear",
  1797. value: function clear() {
  1798. this._list = [];
  1799. }
  1800. }, {
  1801. key: "appendArray",
  1802. value: function appendArray(syncPoints) {
  1803. var list = this._list;
  1804. if (syncPoints.length === 0) {
  1805. return;
  1806. }
  1807. if (list.length > 0 && syncPoints[0].originalDts < list[list.length - 1].originalDts) {
  1808. this.clear();
  1809. }
  1810. Array.prototype.push.apply(list, syncPoints);
  1811. }
  1812. }, {
  1813. key: "getLastSyncPointBeforeDts",
  1814. value: function getLastSyncPointBeforeDts(dts) {
  1815. if (this._list.length == 0) {
  1816. return null;
  1817. }
  1818. var list = this._list;
  1819. var idx = 0;
  1820. var last = list.length - 1;
  1821. var mid = 0;
  1822. var lbound = 0;
  1823. var ubound = last;
  1824. if (dts < list[0].dts) {
  1825. idx = 0;
  1826. lbound = ubound + 1;
  1827. }
  1828. while (lbound <= ubound) {
  1829. mid = lbound + Math.floor((ubound - lbound) / 2);
  1830. if (mid === last || dts >= list[mid].dts && dts < list[mid + 1].dts) {
  1831. idx = mid;
  1832. break;
  1833. } else if (list[mid].dts < dts) {
  1834. lbound = mid + 1;
  1835. } else {
  1836. ubound = mid - 1;
  1837. }
  1838. }
  1839. return this._list[idx];
  1840. }
  1841. }]);
  1842. return IDRSampleList;
  1843. }();
  1844. // Data structure for recording information of media segments in single track.
  1845. var MediaSegmentInfoList = exports.MediaSegmentInfoList = function () {
  1846. function MediaSegmentInfoList(type) {
  1847. _classCallCheck(this, MediaSegmentInfoList);
  1848. this._type = type;
  1849. this._list = [];
  1850. this._lastAppendLocation = -1; // cached last insert location
  1851. }
  1852. _createClass(MediaSegmentInfoList, [{
  1853. key: "isEmpty",
  1854. value: function isEmpty() {
  1855. return this._list.length === 0;
  1856. }
  1857. }, {
  1858. key: "clear",
  1859. value: function clear() {
  1860. this._list = [];
  1861. this._lastAppendLocation = -1;
  1862. }
  1863. }, {
  1864. key: "_searchNearestSegmentBefore",
  1865. value: function _searchNearestSegmentBefore(originalBeginDts) {
  1866. var list = this._list;
  1867. if (list.length === 0) {
  1868. return -2;
  1869. }
  1870. var last = list.length - 1;
  1871. var mid = 0;
  1872. var lbound = 0;
  1873. var ubound = last;
  1874. var idx = 0;
  1875. if (originalBeginDts < list[0].originalBeginDts) {
  1876. idx = -1;
  1877. return idx;
  1878. }
  1879. while (lbound <= ubound) {
  1880. mid = lbound + Math.floor((ubound - lbound) / 2);
  1881. if (mid === last || originalBeginDts > list[mid].lastSample.originalDts && originalBeginDts < list[mid + 1].originalBeginDts) {
  1882. idx = mid;
  1883. break;
  1884. } else if (list[mid].originalBeginDts < originalBeginDts) {
  1885. lbound = mid + 1;
  1886. } else {
  1887. ubound = mid - 1;
  1888. }
  1889. }
  1890. return idx;
  1891. }
  1892. }, {
  1893. key: "_searchNearestSegmentAfter",
  1894. value: function _searchNearestSegmentAfter(originalBeginDts) {
  1895. return this._searchNearestSegmentBefore(originalBeginDts) + 1;
  1896. }
  1897. }, {
  1898. key: "append",
  1899. value: function append(mediaSegmentInfo) {
  1900. var list = this._list;
  1901. var msi = mediaSegmentInfo;
  1902. var lastAppendIdx = this._lastAppendLocation;
  1903. var insertIdx = 0;
  1904. if (lastAppendIdx !== -1 && lastAppendIdx < list.length && msi.originalBeginDts >= list[lastAppendIdx].lastSample.originalDts && (lastAppendIdx === list.length - 1 || lastAppendIdx < list.length - 1 && msi.originalBeginDts < list[lastAppendIdx + 1].originalBeginDts)) {
  1905. insertIdx = lastAppendIdx + 1; // use cached location idx
  1906. } else {
  1907. if (list.length > 0) {
  1908. insertIdx = this._searchNearestSegmentBefore(msi.originalBeginDts) + 1;
  1909. }
  1910. }
  1911. this._lastAppendLocation = insertIdx;
  1912. this._list.splice(insertIdx, 0, msi);
  1913. }
  1914. }, {
  1915. key: "getLastSegmentBefore",
  1916. value: function getLastSegmentBefore(originalBeginDts) {
  1917. var idx = this._searchNearestSegmentBefore(originalBeginDts);
  1918. if (idx >= 0) {
  1919. return this._list[idx];
  1920. } else {
  1921. // -1
  1922. return null;
  1923. }
  1924. }
  1925. }, {
  1926. key: "getLastSampleBefore",
  1927. value: function getLastSampleBefore(originalBeginDts) {
  1928. var segment = this.getLastSegmentBefore(originalBeginDts);
  1929. if (segment != null) {
  1930. return segment.lastSample;
  1931. } else {
  1932. return null;
  1933. }
  1934. }
  1935. }, {
  1936. key: "getLastSyncPointBefore",
  1937. value: function getLastSyncPointBefore(originalBeginDts) {
  1938. var segmentIdx = this._searchNearestSegmentBefore(originalBeginDts);
  1939. var syncPoints = this._list[segmentIdx].syncPoints;
  1940. while (syncPoints.length === 0 && segmentIdx > 0) {
  1941. segmentIdx--;
  1942. syncPoints = this._list[segmentIdx].syncPoints;
  1943. }
  1944. if (syncPoints.length > 0) {
  1945. return syncPoints[syncPoints.length - 1];
  1946. } else {
  1947. return null;
  1948. }
  1949. }
  1950. }, {
  1951. key: "type",
  1952. get: function get() {
  1953. return this._type;
  1954. }
  1955. }, {
  1956. key: "length",
  1957. get: function get() {
  1958. return this._list.length;
  1959. }
  1960. }]);
  1961. return MediaSegmentInfoList;
  1962. }();
  1963. },{}],9:[function(_dereq_,module,exports){
  1964. 'use strict';
  1965. Object.defineProperty(exports, "__esModule", {
  1966. value: true
  1967. });
  1968. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  1969. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  1970. *
  1971. * @author zheng qian <xqq@xqq.im>
  1972. *
  1973. * Licensed under the Apache License, Version 2.0 (the "License");
  1974. * you may not use this file except in compliance with the License.
  1975. * You may obtain a copy of the License at
  1976. *
  1977. * http://www.apache.org/licenses/LICENSE-2.0
  1978. *
  1979. * Unless required by applicable law or agreed to in writing, software
  1980. * distributed under the License is distributed on an "AS IS" BASIS,
  1981. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  1982. * See the License for the specific language governing permissions and
  1983. * limitations under the License.
  1984. */
  1985. var _events = _dereq_('events');
  1986. var _events2 = _interopRequireDefault(_events);
  1987. var _logger = _dereq_('../utils/logger.js');
  1988. var _logger2 = _interopRequireDefault(_logger);
  1989. var _browser = _dereq_('../utils/browser.js');
  1990. var _browser2 = _interopRequireDefault(_browser);
  1991. var _mseEvents = _dereq_('./mse-events.js');
  1992. var _mseEvents2 = _interopRequireDefault(_mseEvents);
  1993. var _mediaSegmentInfo = _dereq_('./media-segment-info.js');
  1994. var _exception = _dereq_('../utils/exception.js');
  1995. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  1996. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  1997. // Media Source Extensions controller
  1998. var MSEController = function () {
  1999. function MSEController(config) {
  2000. _classCallCheck(this, MSEController);
  2001. this.TAG = 'MSEController';
  2002. this._config = config;
  2003. this._emitter = new _events2.default();
  2004. if (this._config.isLive && this._config.autoCleanupSourceBuffer == undefined) {
  2005. // For live stream, do auto cleanup by default
  2006. this._config.autoCleanupSourceBuffer = true;
  2007. }
  2008. this.e = {
  2009. onSourceOpen: this._onSourceOpen.bind(this),
  2010. onSourceEnded: this._onSourceEnded.bind(this),
  2011. onSourceClose: this._onSourceClose.bind(this),
  2012. onSourceBufferError: this._onSourceBufferError.bind(this),
  2013. onSourceBufferUpdateEnd: this._onSourceBufferUpdateEnd.bind(this)
  2014. };
  2015. this._mediaSource = null;
  2016. this._mediaSourceObjectURL = null;
  2017. this._mediaElement = null;
  2018. this._isBufferFull = false;
  2019. this._hasPendingEos = false;
  2020. this._requireSetMediaDuration = false;
  2021. this._pendingMediaDuration = 0;
  2022. this._pendingSourceBufferInit = [];
  2023. this._mimeTypes = {
  2024. video: null,
  2025. audio: null
  2026. };
  2027. this._sourceBuffers = {
  2028. video: null,
  2029. audio: null
  2030. };
  2031. this._lastInitSegments = {
  2032. video: null,
  2033. audio: null
  2034. };
  2035. this._pendingSegments = {
  2036. video: [],
  2037. audio: []
  2038. };
  2039. this._pendingRemoveRanges = {
  2040. video: [],
  2041. audio: []
  2042. };
  2043. this._idrList = new _mediaSegmentInfo.IDRSampleList();
  2044. }
  2045. _createClass(MSEController, [{
  2046. key: 'destroy',
  2047. value: function destroy() {
  2048. if (this._mediaElement || this._mediaSource) {
  2049. this.detachMediaElement();
  2050. }
  2051. this.e = null;
  2052. this._emitter.removeAllListeners();
  2053. this._emitter = null;
  2054. }
  2055. }, {
  2056. key: 'on',
  2057. value: function on(event, listener) {
  2058. this._emitter.addListener(event, listener);
  2059. }
  2060. }, {
  2061. key: 'off',
  2062. value: function off(event, listener) {
  2063. this._emitter.removeListener(event, listener);
  2064. }
  2065. }, {
  2066. key: 'attachMediaElement',
  2067. value: function attachMediaElement(mediaElement) {
  2068. if (this._mediaSource) {
  2069. throw new _exception.IllegalStateException('MediaSource has been attached to an HTMLMediaElement!');
  2070. }
  2071. var ms = this._mediaSource = new window.MediaSource();
  2072. ms.addEventListener('sourceopen', this.e.onSourceOpen);
  2073. ms.addEventListener('sourceended', this.e.onSourceEnded);
  2074. ms.addEventListener('sourceclose', this.e.onSourceClose);
  2075. this._mediaElement = mediaElement;
  2076. this._mediaSourceObjectURL = window.URL.createObjectURL(this._mediaSource);
  2077. mediaElement.src = this._mediaSourceObjectURL;
  2078. }
  2079. }, {
  2080. key: 'detachMediaElement',
  2081. value: function detachMediaElement() {
  2082. if (this._mediaSource) {
  2083. var ms = this._mediaSource;
  2084. for (var type in this._sourceBuffers) {
  2085. // pending segments should be discard
  2086. var ps = this._pendingSegments[type];
  2087. ps.splice(0, ps.length);
  2088. this._pendingSegments[type] = null;
  2089. this._pendingRemoveRanges[type] = null;
  2090. this._lastInitSegments[type] = null;
  2091. // remove all sourcebuffers
  2092. var sb = this._sourceBuffers[type];
  2093. if (sb) {
  2094. if (ms.readyState !== 'closed') {
  2095. // ms edge can throw an error: Unexpected call to method or property access
  2096. try {
  2097. ms.removeSourceBuffer(sb);
  2098. } catch (error) {
  2099. _logger2.default.e(this.TAG, error.message);
  2100. }
  2101. sb.removeEventListener('error', this.e.onSourceBufferError);
  2102. sb.removeEventListener('updateend', this.e.onSourceBufferUpdateEnd);
  2103. }
  2104. this._mimeTypes[type] = null;
  2105. this._sourceBuffers[type] = null;
  2106. }
  2107. }
  2108. if (ms.readyState === 'open') {
  2109. try {
  2110. ms.endOfStream();
  2111. } catch (error) {
  2112. _logger2.default.e(this.TAG, error.message);
  2113. }
  2114. }
  2115. ms.removeEventListener('sourceopen', this.e.onSourceOpen);
  2116. ms.removeEventListener('sourceended', this.e.onSourceEnded);
  2117. ms.removeEventListener('sourceclose', this.e.onSourceClose);
  2118. this._pendingSourceBufferInit = [];
  2119. this._isBufferFull = false;
  2120. this._idrList.clear();
  2121. this._mediaSource = null;
  2122. }
  2123. if (this._mediaElement) {
  2124. this._mediaElement.src = '';
  2125. this._mediaElement.removeAttribute('src');
  2126. this._mediaElement = null;
  2127. }
  2128. if (this._mediaSourceObjectURL) {
  2129. window.URL.revokeObjectURL(this._mediaSourceObjectURL);
  2130. this._mediaSourceObjectURL = null;
  2131. }
  2132. }
  2133. }, {
  2134. key: 'appendInitSegment',
  2135. value: function appendInitSegment(initSegment, deferred) {
  2136. if (!this._mediaSource || this._mediaSource.readyState !== 'open') {
  2137. // sourcebuffer creation requires mediaSource.readyState === 'open'
  2138. // so we defer the sourcebuffer creation, until sourceopen event triggered
  2139. this._pendingSourceBufferInit.push(initSegment);
  2140. // make sure that this InitSegment is in the front of pending segments queue
  2141. this._pendingSegments[initSegment.type].push(initSegment);
  2142. return;
  2143. }
  2144. var is = initSegment;
  2145. var mimeType = '' + is.container;
  2146. if (is.codec && is.codec.length > 0) {
  2147. mimeType += ';codecs=' + is.codec;
  2148. }
  2149. var firstInitSegment = false;
  2150. _logger2.default.v(this.TAG, 'Received Initialization Segment, mimeType: ' + mimeType);
  2151. this._lastInitSegments[is.type] = is;
  2152. if (mimeType !== this._mimeTypes[is.type]) {
  2153. if (!this._mimeTypes[is.type]) {
  2154. // empty, first chance create sourcebuffer
  2155. firstInitSegment = true;
  2156. try {
  2157. var sb = this._sourceBuffers[is.type] = this._mediaSource.addSourceBuffer(mimeType);
  2158. sb.addEventListener('error', this.e.onSourceBufferError);
  2159. sb.addEventListener('updateend', this.e.onSourceBufferUpdateEnd);
  2160. } catch (error) {
  2161. _logger2.default.e(this.TAG, error.message);
  2162. this._emitter.emit(_mseEvents2.default.ERROR, { code: error.code, msg: error.message });
  2163. return;
  2164. }
  2165. } else {
  2166. _logger2.default.v(this.TAG, 'Notice: ' + is.type + ' mimeType changed, origin: ' + this._mimeTypes[is.type] + ', target: ' + mimeType);
  2167. }
  2168. this._mimeTypes[is.type] = mimeType;
  2169. }
  2170. if (!deferred) {
  2171. // deferred means this InitSegment has been pushed to pendingSegments queue
  2172. this._pendingSegments[is.type].push(is);
  2173. }
  2174. if (!firstInitSegment) {
  2175. // append immediately only if init segment in subsequence
  2176. if (this._sourceBuffers[is.type] && !this._sourceBuffers[is.type].updating) {
  2177. this._doAppendSegments();
  2178. }
  2179. }
  2180. if (_browser2.default.safari && is.container === 'audio/mpeg' && is.mediaDuration > 0) {
  2181. // 'audio/mpeg' track under Safari may cause MediaElement's duration to be NaN
  2182. // Manually correct MediaSource.duration to make progress bar seekable, and report right duration
  2183. this._requireSetMediaDuration = true;
  2184. this._pendingMediaDuration = is.mediaDuration / 1000; // in seconds
  2185. this._updateMediaSourceDuration();
  2186. }
  2187. }
  2188. }, {
  2189. key: 'appendMediaSegment',
  2190. value: function appendMediaSegment(mediaSegment) {
  2191. var ms = mediaSegment;
  2192. this._pendingSegments[ms.type].push(ms);
  2193. if (this._config.autoCleanupSourceBuffer && this._needCleanupSourceBuffer()) {
  2194. this._doCleanupSourceBuffer();
  2195. }
  2196. var sb = this._sourceBuffers[ms.type];
  2197. if (sb && !sb.updating && !this._hasPendingRemoveRanges()) {
  2198. this._doAppendSegments();
  2199. }
  2200. }
  2201. }, {
  2202. key: 'seek',
  2203. value: function seek(seconds) {
  2204. // remove all appended buffers
  2205. for (var type in this._sourceBuffers) {
  2206. if (!this._sourceBuffers[type]) {
  2207. continue;
  2208. }
  2209. // abort current buffer append algorithm
  2210. var sb = this._sourceBuffers[type];
  2211. if (this._mediaSource.readyState === 'open') {
  2212. try {
  2213. // If range removal algorithm is running, InvalidStateError will be throwed
  2214. // Ignore it.
  2215. sb.abort();
  2216. } catch (error) {
  2217. _logger2.default.e(this.TAG, error.message);
  2218. }
  2219. }
  2220. // IDRList should be clear
  2221. this._idrList.clear();
  2222. // pending segments should be discard
  2223. var ps = this._pendingSegments[type];
  2224. ps.splice(0, ps.length);
  2225. if (this._mediaSource.readyState === 'closed') {
  2226. // Parent MediaSource object has been detached from HTMLMediaElement
  2227. continue;
  2228. }
  2229. // record ranges to be remove from SourceBuffer
  2230. for (var i = 0; i < sb.buffered.length; i++) {
  2231. var start = sb.buffered.start(i);
  2232. var end = sb.buffered.end(i);
  2233. this._pendingRemoveRanges[type].push({ start: start, end: end });
  2234. }
  2235. // if sb is not updating, let's remove ranges now!
  2236. if (!sb.updating) {
  2237. this._doRemoveRanges();
  2238. }
  2239. // Safari 10 may get InvalidStateError in the later appendBuffer() after SourceBuffer.remove() call
  2240. // Internal parser's state may be invalid at this time. Re-append last InitSegment to workaround.
  2241. // Related issue: https://bugs.webkit.org/show_bug.cgi?id=159230
  2242. if (_browser2.default.safari) {
  2243. var lastInitSegment = this._lastInitSegments[type];
  2244. if (lastInitSegment) {
  2245. this._pendingSegments[type].push(lastInitSegment);
  2246. if (!sb.updating) {
  2247. this._doAppendSegments();
  2248. }
  2249. }
  2250. }
  2251. }
  2252. }
  2253. }, {
  2254. key: 'endOfStream',
  2255. value: function endOfStream() {
  2256. var ms = this._mediaSource;
  2257. var sb = this._sourceBuffers;
  2258. if (!ms || ms.readyState !== 'open') {
  2259. if (ms && ms.readyState === 'closed' && this._hasPendingSegments()) {
  2260. // If MediaSource hasn't turned into open state, and there're pending segments
  2261. // Mark pending endOfStream, defer call until all pending segments appended complete
  2262. this._hasPendingEos = true;
  2263. }
  2264. return;
  2265. }
  2266. if (sb.video && sb.video.updating || sb.audio && sb.audio.updating) {
  2267. // If any sourcebuffer is updating, defer endOfStream operation
  2268. // See _onSourceBufferUpdateEnd()
  2269. this._hasPendingEos = true;
  2270. } else {
  2271. this._hasPendingEos = false;
  2272. // Notify media data loading complete
  2273. // This is helpful for correcting total duration to match last media segment
  2274. // Otherwise MediaElement's ended event may not be triggered
  2275. ms.endOfStream();
  2276. }
  2277. }
  2278. }, {
  2279. key: 'getNearestKeyframe',
  2280. value: function getNearestKeyframe(dts) {
  2281. return this._idrList.getLastSyncPointBeforeDts(dts);
  2282. }
  2283. }, {
  2284. key: '_needCleanupSourceBuffer',
  2285. value: function _needCleanupSourceBuffer() {
  2286. if (!this._config.autoCleanupSourceBuffer) {
  2287. return false;
  2288. }
  2289. var currentTime = this._mediaElement.currentTime;
  2290. for (var type in this._sourceBuffers) {
  2291. var sb = this._sourceBuffers[type];
  2292. if (sb) {
  2293. var buffered = sb.buffered;
  2294. if (buffered.length >= 1) {
  2295. if (currentTime - buffered.start(0) >= this._config.autoCleanupMaxBackwardDuration) {
  2296. return true;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. return false;
  2302. }
  2303. }, {
  2304. key: '_doCleanupSourceBuffer',
  2305. value: function _doCleanupSourceBuffer() {
  2306. var currentTime = this._mediaElement.currentTime;
  2307. for (var type in this._sourceBuffers) {
  2308. var sb = this._sourceBuffers[type];
  2309. if (sb) {
  2310. var buffered = sb.buffered;
  2311. var doRemove = false;
  2312. for (var i = 0; i < buffered.length; i++) {
  2313. var start = buffered.start(i);
  2314. var end = buffered.end(i);
  2315. if (start <= currentTime && currentTime < end + 3) {
  2316. // padding 3 seconds
  2317. if (currentTime - start >= this._config.autoCleanupMaxBackwardDuration) {
  2318. doRemove = true;
  2319. var removeEnd = currentTime - this._config.autoCleanupMinBackwardDuration;
  2320. this._pendingRemoveRanges[type].push({ start: start, end: removeEnd });
  2321. }
  2322. } else if (end < currentTime) {
  2323. doRemove = true;
  2324. this._pendingRemoveRanges[type].push({ start: start, end: end });
  2325. }
  2326. }
  2327. if (doRemove && !sb.updating) {
  2328. this._doRemoveRanges();
  2329. }
  2330. }
  2331. }
  2332. }
  2333. }, {
  2334. key: '_updateMediaSourceDuration',
  2335. value: function _updateMediaSourceDuration() {
  2336. var sb = this._sourceBuffers;
  2337. if (this._mediaElement.readyState === 0 || this._mediaSource.readyState !== 'open') {
  2338. return;
  2339. }
  2340. if (sb.video && sb.video.updating || sb.audio && sb.audio.updating) {
  2341. return;
  2342. }
  2343. var current = this._mediaSource.duration;
  2344. var target = this._pendingMediaDuration;
  2345. if (target > 0 && (isNaN(current) || target > current)) {
  2346. _logger2.default.v(this.TAG, 'Update MediaSource duration from ' + current + ' to ' + target);
  2347. this._mediaSource.duration = target;
  2348. }
  2349. this._requireSetMediaDuration = false;
  2350. this._pendingMediaDuration = 0;
  2351. }
  2352. }, {
  2353. key: '_doRemoveRanges',
  2354. value: function _doRemoveRanges() {
  2355. for (var type in this._pendingRemoveRanges) {
  2356. if (!this._sourceBuffers[type] || this._sourceBuffers[type].updating) {
  2357. continue;
  2358. }
  2359. var sb = this._sourceBuffers[type];
  2360. var ranges = this._pendingRemoveRanges[type];
  2361. while (ranges.length && !sb.updating) {
  2362. var range = ranges.shift();
  2363. sb.remove(range.start, range.end);
  2364. }
  2365. }
  2366. }
  2367. }, {
  2368. key: '_doAppendSegments',
  2369. value: function _doAppendSegments() {
  2370. var pendingSegments = this._pendingSegments;
  2371. for (var type in pendingSegments) {
  2372. if (!this._sourceBuffers[type] || this._sourceBuffers[type].updating) {
  2373. continue;
  2374. }
  2375. if (pendingSegments[type].length > 0) {
  2376. var segment = pendingSegments[type].shift();
  2377. if (segment.timestampOffset) {
  2378. // For MPEG audio stream in MSE, if unbuffered-seeking occurred
  2379. // We need explicitly set timestampOffset to the desired point in timeline for mpeg SourceBuffer.
  2380. var currentOffset = this._sourceBuffers[type].timestampOffset;
  2381. var targetOffset = segment.timestampOffset / 1000; // in seconds
  2382. var delta = Math.abs(currentOffset - targetOffset);
  2383. if (delta > 0.1) {
  2384. // If time delta > 100ms
  2385. _logger2.default.v(this.TAG, 'Update MPEG audio timestampOffset from ' + currentOffset + ' to ' + targetOffset);
  2386. this._sourceBuffers[type].timestampOffset = targetOffset;
  2387. }
  2388. delete segment.timestampOffset;
  2389. }
  2390. if (!segment.data || segment.data.byteLength === 0) {
  2391. // Ignore empty buffer
  2392. continue;
  2393. }
  2394. try {
  2395. this._sourceBuffers[type].appendBuffer(segment.data);
  2396. this._isBufferFull = false;
  2397. if (type === 'video' && segment.hasOwnProperty('info')) {
  2398. this._idrList.appendArray(segment.info.syncPoints);
  2399. }
  2400. } catch (error) {
  2401. this._pendingSegments[type].unshift(segment);
  2402. if (error.code === 22) {
  2403. // QuotaExceededError
  2404. /* Notice that FireFox may not throw QuotaExceededError if SourceBuffer is full
  2405. * Currently we can only do lazy-load to avoid SourceBuffer become scattered.
  2406. * SourceBuffer eviction policy may be changed in future version of FireFox.
  2407. *
  2408. * Related issues:
  2409. * https://bugzilla.mozilla.org/show_bug.cgi?id=1279885
  2410. * https://bugzilla.mozilla.org/show_bug.cgi?id=1280023
  2411. */
  2412. // report buffer full, abort network IO
  2413. if (!this._isBufferFull) {
  2414. this._emitter.emit(_mseEvents2.default.BUFFER_FULL);
  2415. }
  2416. this._isBufferFull = true;
  2417. } else {
  2418. _logger2.default.e(this.TAG, error.message);
  2419. this._emitter.emit(_mseEvents2.default.ERROR, { code: error.code, msg: error.message });
  2420. }
  2421. }
  2422. }
  2423. }
  2424. }
  2425. }, {
  2426. key: '_onSourceOpen',
  2427. value: function _onSourceOpen() {
  2428. _logger2.default.v(this.TAG, 'MediaSource onSourceOpen');
  2429. this._mediaSource.removeEventListener('sourceopen', this.e.onSourceOpen);
  2430. // deferred sourcebuffer creation / initialization
  2431. if (this._pendingSourceBufferInit.length > 0) {
  2432. var pendings = this._pendingSourceBufferInit;
  2433. while (pendings.length) {
  2434. var segment = pendings.shift();
  2435. this.appendInitSegment(segment, true);
  2436. }
  2437. }
  2438. // there may be some pending media segments, append them
  2439. if (this._hasPendingSegments()) {
  2440. this._doAppendSegments();
  2441. }
  2442. this._emitter.emit(_mseEvents2.default.SOURCE_OPEN);
  2443. }
  2444. }, {
  2445. key: '_onSourceEnded',
  2446. value: function _onSourceEnded() {
  2447. // fired on endOfStream
  2448. _logger2.default.v(this.TAG, 'MediaSource onSourceEnded');
  2449. }
  2450. }, {
  2451. key: '_onSourceClose',
  2452. value: function _onSourceClose() {
  2453. // fired on detaching from media element
  2454. _logger2.default.v(this.TAG, 'MediaSource onSourceClose');
  2455. if (this._mediaSource && this.e != null) {
  2456. this._mediaSource.removeEventListener('sourceopen', this.e.onSourceOpen);
  2457. this._mediaSource.removeEventListener('sourceended', this.e.onSourceEnded);
  2458. this._mediaSource.removeEventListener('sourceclose', this.e.onSourceClose);
  2459. }
  2460. }
  2461. }, {
  2462. key: '_hasPendingSegments',
  2463. value: function _hasPendingSegments() {
  2464. var ps = this._pendingSegments;
  2465. return ps.video.length > 0 || ps.audio.length > 0;
  2466. }
  2467. }, {
  2468. key: '_hasPendingRemoveRanges',
  2469. value: function _hasPendingRemoveRanges() {
  2470. var prr = this._pendingRemoveRanges;
  2471. return prr.video.length > 0 || prr.audio.length > 0;
  2472. }
  2473. }, {
  2474. key: '_onSourceBufferUpdateEnd',
  2475. value: function _onSourceBufferUpdateEnd() {
  2476. if (this._requireSetMediaDuration) {
  2477. this._updateMediaSourceDuration();
  2478. } else if (this._hasPendingRemoveRanges()) {
  2479. this._doRemoveRanges();
  2480. } else if (this._hasPendingSegments()) {
  2481. this._doAppendSegments();
  2482. } else if (this._hasPendingEos) {
  2483. this.endOfStream();
  2484. }
  2485. this._emitter.emit(_mseEvents2.default.UPDATE_END);
  2486. }
  2487. }, {
  2488. key: '_onSourceBufferError',
  2489. value: function _onSourceBufferError(e) {
  2490. _logger2.default.e(this.TAG, 'SourceBuffer Error: ' + e);
  2491. // this error might not always be fatal, just ignore it
  2492. }
  2493. }]);
  2494. return MSEController;
  2495. }();
  2496. exports.default = MSEController;
  2497. },{"../utils/browser.js":39,"../utils/exception.js":40,"../utils/logger.js":41,"./media-segment-info.js":8,"./mse-events.js":10,"events":2}],10:[function(_dereq_,module,exports){
  2498. 'use strict';
  2499. Object.defineProperty(exports, "__esModule", {
  2500. value: true
  2501. });
  2502. /*
  2503. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  2504. *
  2505. * @author zheng qian <xqq@xqq.im>
  2506. *
  2507. * Licensed under the Apache License, Version 2.0 (the "License");
  2508. * you may not use this file except in compliance with the License.
  2509. * You may obtain a copy of the License at
  2510. *
  2511. * http://www.apache.org/licenses/LICENSE-2.0
  2512. *
  2513. * Unless required by applicable law or agreed to in writing, software
  2514. * distributed under the License is distributed on an "AS IS" BASIS,
  2515. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2516. * See the License for the specific language governing permissions and
  2517. * limitations under the License.
  2518. */
  2519. var MSEEvents = {
  2520. ERROR: 'error',
  2521. SOURCE_OPEN: 'source_open',
  2522. UPDATE_END: 'update_end',
  2523. BUFFER_FULL: 'buffer_full'
  2524. };
  2525. exports.default = MSEEvents;
  2526. },{}],11:[function(_dereq_,module,exports){
  2527. 'use strict';
  2528. Object.defineProperty(exports, "__esModule", {
  2529. value: true
  2530. });
  2531. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  2532. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  2533. *
  2534. * @author zheng qian <xqq@xqq.im>
  2535. *
  2536. * Licensed under the Apache License, Version 2.0 (the "License");
  2537. * you may not use this file except in compliance with the License.
  2538. * You may obtain a copy of the License at
  2539. *
  2540. * http://www.apache.org/licenses/LICENSE-2.0
  2541. *
  2542. * Unless required by applicable law or agreed to in writing, software
  2543. * distributed under the License is distributed on an "AS IS" BASIS,
  2544. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2545. * See the License for the specific language governing permissions and
  2546. * limitations under the License.
  2547. */
  2548. var _events = _dereq_('events');
  2549. var _events2 = _interopRequireDefault(_events);
  2550. var _logger = _dereq_('../utils/logger.js');
  2551. var _logger2 = _interopRequireDefault(_logger);
  2552. var _loggingControl = _dereq_('../utils/logging-control.js');
  2553. var _loggingControl2 = _interopRequireDefault(_loggingControl);
  2554. var _transmuxingController = _dereq_('./transmuxing-controller.js');
  2555. var _transmuxingController2 = _interopRequireDefault(_transmuxingController);
  2556. var _transmuxingEvents = _dereq_('./transmuxing-events.js');
  2557. var _transmuxingEvents2 = _interopRequireDefault(_transmuxingEvents);
  2558. var _transmuxingWorker = _dereq_('./transmuxing-worker.js');
  2559. var _transmuxingWorker2 = _interopRequireDefault(_transmuxingWorker);
  2560. var _mediaInfo = _dereq_('./media-info.js');
  2561. var _mediaInfo2 = _interopRequireDefault(_mediaInfo);
  2562. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  2563. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2564. var Transmuxer = function () {
  2565. function Transmuxer(mediaDataSource, config) {
  2566. _classCallCheck(this, Transmuxer);
  2567. this.TAG = 'Transmuxer';
  2568. this._emitter = new _events2.default();
  2569. if (config.enableWorker && typeof Worker !== 'undefined') {
  2570. try {
  2571. var work = _dereq_('webworkify');
  2572. this._worker = work(_transmuxingWorker2.default);
  2573. this._workerDestroying = false;
  2574. this._worker.addEventListener('message', this._onWorkerMessage.bind(this));
  2575. this._worker.postMessage({ cmd: 'init', param: [mediaDataSource, config] });
  2576. this.e = {
  2577. onLoggingConfigChanged: this._onLoggingConfigChanged.bind(this)
  2578. };
  2579. _loggingControl2.default.registerListener(this.e.onLoggingConfigChanged);
  2580. this._worker.postMessage({ cmd: 'logging_config', param: _loggingControl2.default.getConfig() });
  2581. } catch (error) {
  2582. _logger2.default.e(this.TAG, 'Error while initialize transmuxing worker, fallback to inline transmuxing');
  2583. this._worker = null;
  2584. this._controller = new _transmuxingController2.default(mediaDataSource, config);
  2585. }
  2586. } else {
  2587. this._controller = new _transmuxingController2.default(mediaDataSource, config);
  2588. }
  2589. if (this._controller) {
  2590. var ctl = this._controller;
  2591. ctl.on(_transmuxingEvents2.default.IO_ERROR, this._onIOError.bind(this));
  2592. ctl.on(_transmuxingEvents2.default.DEMUX_ERROR, this._onDemuxError.bind(this));
  2593. ctl.on(_transmuxingEvents2.default.INIT_SEGMENT, this._onInitSegment.bind(this));
  2594. ctl.on(_transmuxingEvents2.default.MEDIA_SEGMENT, this._onMediaSegment.bind(this));
  2595. ctl.on(_transmuxingEvents2.default.LOADING_COMPLETE, this._onLoadingComplete.bind(this));
  2596. ctl.on(_transmuxingEvents2.default.RECOVERED_EARLY_EOF, this._onRecoveredEarlyEof.bind(this));
  2597. ctl.on(_transmuxingEvents2.default.MEDIA_INFO, this._onMediaInfo.bind(this));
  2598. ctl.on(_transmuxingEvents2.default.METADATA_ARRIVED, this._onMetaDataArrived.bind(this));
  2599. ctl.on(_transmuxingEvents2.default.STATISTICS_INFO, this._onStatisticsInfo.bind(this));
  2600. ctl.on(_transmuxingEvents2.default.RECOMMEND_SEEKPOINT, this._onRecommendSeekpoint.bind(this));
  2601. }
  2602. }
  2603. _createClass(Transmuxer, [{
  2604. key: 'destroy',
  2605. value: function destroy() {
  2606. if (this._worker) {
  2607. if (!this._workerDestroying) {
  2608. this._workerDestroying = true;
  2609. this._worker.postMessage({ cmd: 'destroy' });
  2610. _loggingControl2.default.removeListener(this.e.onLoggingConfigChanged);
  2611. this.e = null;
  2612. }
  2613. } else {
  2614. this._controller.destroy();
  2615. this._controller = null;
  2616. }
  2617. this._emitter.removeAllListeners();
  2618. this._emitter = null;
  2619. }
  2620. }, {
  2621. key: 'on',
  2622. value: function on(event, listener) {
  2623. this._emitter.addListener(event, listener);
  2624. }
  2625. }, {
  2626. key: 'off',
  2627. value: function off(event, listener) {
  2628. this._emitter.removeListener(event, listener);
  2629. }
  2630. }, {
  2631. key: 'hasWorker',
  2632. value: function hasWorker() {
  2633. return this._worker != null;
  2634. }
  2635. }, {
  2636. key: 'open',
  2637. value: function open() {
  2638. if (this._worker) {
  2639. this._worker.postMessage({ cmd: 'start' });
  2640. } else {
  2641. this._controller.start();
  2642. }
  2643. }
  2644. }, {
  2645. key: 'close',
  2646. value: function close() {
  2647. if (this._worker) {
  2648. this._worker.postMessage({ cmd: 'stop' });
  2649. } else {
  2650. this._controller.stop();
  2651. }
  2652. }
  2653. }, {
  2654. key: 'seek',
  2655. value: function seek(milliseconds) {
  2656. if (this._worker) {
  2657. this._worker.postMessage({ cmd: 'seek', param: milliseconds });
  2658. } else {
  2659. this._controller.seek(milliseconds);
  2660. }
  2661. }
  2662. }, {
  2663. key: 'pause',
  2664. value: function pause() {
  2665. if (this._worker) {
  2666. this._worker.postMessage({ cmd: 'pause' });
  2667. } else {
  2668. this._controller.pause();
  2669. }
  2670. }
  2671. }, {
  2672. key: 'resume',
  2673. value: function resume() {
  2674. if (this._worker) {
  2675. this._worker.postMessage({ cmd: 'resume' });
  2676. } else {
  2677. this._controller.resume();
  2678. }
  2679. }
  2680. }, {
  2681. key: '_onInitSegment',
  2682. value: function _onInitSegment(type, initSegment) {
  2683. var _this = this;
  2684. // do async invoke
  2685. Promise.resolve().then(function () {
  2686. _this._emitter.emit(_transmuxingEvents2.default.INIT_SEGMENT, type, initSegment);
  2687. });
  2688. }
  2689. }, {
  2690. key: '_onMediaSegment',
  2691. value: function _onMediaSegment(type, mediaSegment) {
  2692. var _this2 = this;
  2693. Promise.resolve().then(function () {
  2694. _this2._emitter.emit(_transmuxingEvents2.default.MEDIA_SEGMENT, type, mediaSegment);
  2695. });
  2696. }
  2697. }, {
  2698. key: '_onLoadingComplete',
  2699. value: function _onLoadingComplete() {
  2700. var _this3 = this;
  2701. Promise.resolve().then(function () {
  2702. _this3._emitter.emit(_transmuxingEvents2.default.LOADING_COMPLETE);
  2703. });
  2704. }
  2705. }, {
  2706. key: '_onRecoveredEarlyEof',
  2707. value: function _onRecoveredEarlyEof() {
  2708. var _this4 = this;
  2709. Promise.resolve().then(function () {
  2710. _this4._emitter.emit(_transmuxingEvents2.default.RECOVERED_EARLY_EOF);
  2711. });
  2712. }
  2713. }, {
  2714. key: '_onMediaInfo',
  2715. value: function _onMediaInfo(mediaInfo) {
  2716. var _this5 = this;
  2717. Promise.resolve().then(function () {
  2718. _this5._emitter.emit(_transmuxingEvents2.default.MEDIA_INFO, mediaInfo);
  2719. });
  2720. }
  2721. }, {
  2722. key: '_onMetaDataArrived',
  2723. value: function _onMetaDataArrived(metadata) {
  2724. var _this6 = this;
  2725. Promise.resolve().then(function () {
  2726. _this6._emitter.emit(_transmuxingEvents2.default.METADATA_ARRIVED, metadata);
  2727. });
  2728. }
  2729. }, {
  2730. key: '_onStatisticsInfo',
  2731. value: function _onStatisticsInfo(statisticsInfo) {
  2732. var _this7 = this;
  2733. Promise.resolve().then(function () {
  2734. _this7._emitter.emit(_transmuxingEvents2.default.STATISTICS_INFO, statisticsInfo);
  2735. });
  2736. }
  2737. }, {
  2738. key: '_onIOError',
  2739. value: function _onIOError(type, info) {
  2740. var _this8 = this;
  2741. Promise.resolve().then(function () {
  2742. _this8._emitter.emit(_transmuxingEvents2.default.IO_ERROR, type, info);
  2743. });
  2744. }
  2745. }, {
  2746. key: '_onDemuxError',
  2747. value: function _onDemuxError(type, info) {
  2748. var _this9 = this;
  2749. Promise.resolve().then(function () {
  2750. _this9._emitter.emit(_transmuxingEvents2.default.DEMUX_ERROR, type, info);
  2751. });
  2752. }
  2753. }, {
  2754. key: '_onRecommendSeekpoint',
  2755. value: function _onRecommendSeekpoint(milliseconds) {
  2756. var _this10 = this;
  2757. Promise.resolve().then(function () {
  2758. _this10._emitter.emit(_transmuxingEvents2.default.RECOMMEND_SEEKPOINT, milliseconds);
  2759. });
  2760. }
  2761. }, {
  2762. key: '_onLoggingConfigChanged',
  2763. value: function _onLoggingConfigChanged(config) {
  2764. if (this._worker) {
  2765. this._worker.postMessage({ cmd: 'logging_config', param: config });
  2766. }
  2767. }
  2768. }, {
  2769. key: '_onWorkerMessage',
  2770. value: function _onWorkerMessage(e) {
  2771. var message = e.data;
  2772. var data = message.data;
  2773. if (message.msg === 'destroyed' || this._workerDestroying) {
  2774. this._workerDestroying = false;
  2775. this._worker.terminate();
  2776. this._worker = null;
  2777. return;
  2778. }
  2779. switch (message.msg) {
  2780. case _transmuxingEvents2.default.INIT_SEGMENT:
  2781. case _transmuxingEvents2.default.MEDIA_SEGMENT:
  2782. this._emitter.emit(message.msg, data.type, data.data);
  2783. break;
  2784. case _transmuxingEvents2.default.LOADING_COMPLETE:
  2785. case _transmuxingEvents2.default.RECOVERED_EARLY_EOF:
  2786. this._emitter.emit(message.msg);
  2787. break;
  2788. case _transmuxingEvents2.default.MEDIA_INFO:
  2789. Object.setPrototypeOf(data, _mediaInfo2.default.prototype);
  2790. this._emitter.emit(message.msg, data);
  2791. break;
  2792. case _transmuxingEvents2.default.METADATA_ARRIVED:
  2793. case _transmuxingEvents2.default.STATISTICS_INFO:
  2794. this._emitter.emit(message.msg, data);
  2795. break;
  2796. case _transmuxingEvents2.default.IO_ERROR:
  2797. case _transmuxingEvents2.default.DEMUX_ERROR:
  2798. this._emitter.emit(message.msg, data.type, data.info);
  2799. break;
  2800. case _transmuxingEvents2.default.RECOMMEND_SEEKPOINT:
  2801. this._emitter.emit(message.msg, data);
  2802. break;
  2803. case 'logcat_callback':
  2804. _logger2.default.emitter.emit('log', data.type, data.logcat);
  2805. break;
  2806. default:
  2807. break;
  2808. }
  2809. }
  2810. }]);
  2811. return Transmuxer;
  2812. }();
  2813. exports.default = Transmuxer;
  2814. },{"../utils/logger.js":41,"../utils/logging-control.js":42,"./media-info.js":7,"./transmuxing-controller.js":12,"./transmuxing-events.js":13,"./transmuxing-worker.js":14,"events":2,"webworkify":4}],12:[function(_dereq_,module,exports){
  2815. 'use strict';
  2816. Object.defineProperty(exports, "__esModule", {
  2817. value: true
  2818. });
  2819. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  2820. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  2821. *
  2822. * @author zheng qian <xqq@xqq.im>
  2823. *
  2824. * Licensed under the Apache License, Version 2.0 (the "License");
  2825. * you may not use this file except in compliance with the License.
  2826. * You may obtain a copy of the License at
  2827. *
  2828. * http://www.apache.org/licenses/LICENSE-2.0
  2829. *
  2830. * Unless required by applicable law or agreed to in writing, software
  2831. * distributed under the License is distributed on an "AS IS" BASIS,
  2832. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  2833. * See the License for the specific language governing permissions and
  2834. * limitations under the License.
  2835. */
  2836. var _events = _dereq_('events');
  2837. var _events2 = _interopRequireDefault(_events);
  2838. var _logger = _dereq_('../utils/logger.js');
  2839. var _logger2 = _interopRequireDefault(_logger);
  2840. var _browser = _dereq_('../utils/browser.js');
  2841. var _browser2 = _interopRequireDefault(_browser);
  2842. var _mediaInfo = _dereq_('./media-info.js');
  2843. var _mediaInfo2 = _interopRequireDefault(_mediaInfo);
  2844. var _flvDemuxer = _dereq_('../demux/flv-demuxer.js');
  2845. var _flvDemuxer2 = _interopRequireDefault(_flvDemuxer);
  2846. var _mp4Remuxer = _dereq_('../remux/mp4-remuxer.js');
  2847. var _mp4Remuxer2 = _interopRequireDefault(_mp4Remuxer);
  2848. var _demuxErrors = _dereq_('../demux/demux-errors.js');
  2849. var _demuxErrors2 = _interopRequireDefault(_demuxErrors);
  2850. var _ioController = _dereq_('../io/io-controller.js');
  2851. var _ioController2 = _interopRequireDefault(_ioController);
  2852. var _transmuxingEvents = _dereq_('./transmuxing-events.js');
  2853. var _transmuxingEvents2 = _interopRequireDefault(_transmuxingEvents);
  2854. var _loader = _dereq_('../io/loader.js');
  2855. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  2856. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  2857. // Transmuxing (IO, Demuxing, Remuxing) controller, with multipart support
  2858. var TransmuxingController = function () {
  2859. function TransmuxingController(mediaDataSource, config) {
  2860. _classCallCheck(this, TransmuxingController);
  2861. this.TAG = 'TransmuxingController';
  2862. this._emitter = new _events2.default();
  2863. this._config = config;
  2864. // treat single part media as multipart media, which has only one segment
  2865. if (!mediaDataSource.segments) {
  2866. mediaDataSource.segments = [{
  2867. duration: mediaDataSource.duration,
  2868. filesize: mediaDataSource.filesize,
  2869. url: mediaDataSource.url
  2870. }];
  2871. }
  2872. // fill in default IO params if not exists
  2873. if (typeof mediaDataSource.cors !== 'boolean') {
  2874. mediaDataSource.cors = true;
  2875. }
  2876. if (typeof mediaDataSource.withCredentials !== 'boolean') {
  2877. mediaDataSource.withCredentials = false;
  2878. }
  2879. this._mediaDataSource = mediaDataSource;
  2880. this._currentSegmentIndex = 0;
  2881. var totalDuration = 0;
  2882. this._mediaDataSource.segments.forEach(function (segment) {
  2883. // timestampBase for each segment, and calculate total duration
  2884. segment.timestampBase = totalDuration;
  2885. totalDuration += segment.duration;
  2886. // params needed by IOController
  2887. segment.cors = mediaDataSource.cors;
  2888. segment.withCredentials = mediaDataSource.withCredentials;
  2889. // referrer policy control, if exist
  2890. if (config.referrerPolicy) {
  2891. segment.referrerPolicy = config.referrerPolicy;
  2892. }
  2893. });
  2894. if (!isNaN(totalDuration) && this._mediaDataSource.duration !== totalDuration) {
  2895. this._mediaDataSource.duration = totalDuration;
  2896. }
  2897. this._mediaInfo = null;
  2898. this._demuxer = null;
  2899. this._remuxer = null;
  2900. this._ioctl = null;
  2901. this._pendingSeekTime = null;
  2902. this._pendingResolveSeekPoint = null;
  2903. this._statisticsReporter = null;
  2904. }
  2905. _createClass(TransmuxingController, [{
  2906. key: 'destroy',
  2907. value: function destroy() {
  2908. this._mediaInfo = null;
  2909. this._mediaDataSource = null;
  2910. if (this._statisticsReporter) {
  2911. this._disableStatisticsReporter();
  2912. }
  2913. if (this._ioctl) {
  2914. this._ioctl.destroy();
  2915. this._ioctl = null;
  2916. }
  2917. if (this._demuxer) {
  2918. this._demuxer.destroy();
  2919. this._demuxer = null;
  2920. }
  2921. if (this._remuxer) {
  2922. this._remuxer.destroy();
  2923. this._remuxer = null;
  2924. }
  2925. this._emitter.removeAllListeners();
  2926. this._emitter = null;
  2927. }
  2928. }, {
  2929. key: 'on',
  2930. value: function on(event, listener) {
  2931. this._emitter.addListener(event, listener);
  2932. }
  2933. }, {
  2934. key: 'off',
  2935. value: function off(event, listener) {
  2936. this._emitter.removeListener(event, listener);
  2937. }
  2938. }, {
  2939. key: 'start',
  2940. value: function start() {
  2941. this._loadSegment(0);
  2942. this._enableStatisticsReporter();
  2943. }
  2944. }, {
  2945. key: '_loadSegment',
  2946. value: function _loadSegment(segmentIndex, optionalFrom) {
  2947. this._currentSegmentIndex = segmentIndex;
  2948. var dataSource = this._mediaDataSource.segments[segmentIndex];
  2949. var ioctl = this._ioctl = new _ioController2.default(dataSource, this._config, segmentIndex);
  2950. ioctl.onError = this._onIOException.bind(this);
  2951. ioctl.onSeeked = this._onIOSeeked.bind(this);
  2952. ioctl.onComplete = this._onIOComplete.bind(this);
  2953. ioctl.onRedirect = this._onIORedirect.bind(this);
  2954. ioctl.onRecoveredEarlyEof = this._onIORecoveredEarlyEof.bind(this);
  2955. if (optionalFrom) {
  2956. this._demuxer.bindDataSource(this._ioctl);
  2957. } else {
  2958. ioctl.onDataArrival = this._onInitChunkArrival.bind(this);
  2959. }
  2960. ioctl.open(optionalFrom);
  2961. }
  2962. }, {
  2963. key: 'stop',
  2964. value: function stop() {
  2965. this._internalAbort();
  2966. this._disableStatisticsReporter();
  2967. }
  2968. }, {
  2969. key: '_internalAbort',
  2970. value: function _internalAbort() {
  2971. if (this._ioctl) {
  2972. this._ioctl.destroy();
  2973. this._ioctl = null;
  2974. }
  2975. }
  2976. }, {
  2977. key: 'pause',
  2978. value: function pause() {
  2979. // take a rest
  2980. if (this._ioctl && this._ioctl.isWorking()) {
  2981. this._ioctl.pause();
  2982. this._disableStatisticsReporter();
  2983. }
  2984. }
  2985. }, {
  2986. key: 'resume',
  2987. value: function resume() {
  2988. if (this._ioctl && this._ioctl.isPaused()) {
  2989. this._ioctl.resume();
  2990. this._enableStatisticsReporter();
  2991. }
  2992. }
  2993. }, {
  2994. key: 'seek',
  2995. value: function seek(milliseconds) {
  2996. if (this._mediaInfo == null || !this._mediaInfo.isSeekable()) {
  2997. return;
  2998. }
  2999. var targetSegmentIndex = this._searchSegmentIndexContains(milliseconds);
  3000. if (targetSegmentIndex === this._currentSegmentIndex) {
  3001. // intra-segment seeking
  3002. var segmentInfo = this._mediaInfo.segments[targetSegmentIndex];
  3003. if (segmentInfo == undefined) {
  3004. // current segment loading started, but mediainfo hasn't received yet
  3005. // wait for the metadata loaded, then seek to expected position
  3006. this._pendingSeekTime = milliseconds;
  3007. } else {
  3008. var keyframe = segmentInfo.getNearestKeyframe(milliseconds);
  3009. this._remuxer.seek(keyframe.milliseconds);
  3010. this._ioctl.seek(keyframe.fileposition);
  3011. // Will be resolved in _onRemuxerMediaSegmentArrival()
  3012. this._pendingResolveSeekPoint = keyframe.milliseconds;
  3013. }
  3014. } else {
  3015. // cross-segment seeking
  3016. var targetSegmentInfo = this._mediaInfo.segments[targetSegmentIndex];
  3017. if (targetSegmentInfo == undefined) {
  3018. // target segment hasn't been loaded. We need metadata then seek to expected time
  3019. this._pendingSeekTime = milliseconds;
  3020. this._internalAbort();
  3021. this._remuxer.seek();
  3022. this._remuxer.insertDiscontinuity();
  3023. this._loadSegment(targetSegmentIndex);
  3024. // Here we wait for the metadata loaded, then seek to expected position
  3025. } else {
  3026. // We have target segment's metadata, direct seek to target position
  3027. var _keyframe = targetSegmentInfo.getNearestKeyframe(milliseconds);
  3028. this._internalAbort();
  3029. this._remuxer.seek(milliseconds);
  3030. this._remuxer.insertDiscontinuity();
  3031. this._demuxer.resetMediaInfo();
  3032. this._demuxer.timestampBase = this._mediaDataSource.segments[targetSegmentIndex].timestampBase;
  3033. this._loadSegment(targetSegmentIndex, _keyframe.fileposition);
  3034. this._pendingResolveSeekPoint = _keyframe.milliseconds;
  3035. this._reportSegmentMediaInfo(targetSegmentIndex);
  3036. }
  3037. }
  3038. this._enableStatisticsReporter();
  3039. }
  3040. }, {
  3041. key: '_searchSegmentIndexContains',
  3042. value: function _searchSegmentIndexContains(milliseconds) {
  3043. var segments = this._mediaDataSource.segments;
  3044. var idx = segments.length - 1;
  3045. for (var i = 0; i < segments.length; i++) {
  3046. if (milliseconds < segments[i].timestampBase) {
  3047. idx = i - 1;
  3048. break;
  3049. }
  3050. }
  3051. return idx;
  3052. }
  3053. }, {
  3054. key: '_onInitChunkArrival',
  3055. value: function _onInitChunkArrival(data, byteStart) {
  3056. var _this = this;
  3057. var probeData = null;
  3058. var consumed = 0;
  3059. if (byteStart > 0) {
  3060. // IOController seeked immediately after opened, byteStart > 0 callback may received
  3061. this._demuxer.bindDataSource(this._ioctl);
  3062. this._demuxer.timestampBase = this._mediaDataSource.segments[this._currentSegmentIndex].timestampBase;
  3063. consumed = this._demuxer.parseChunks(data, byteStart);
  3064. } else if ((probeData = _flvDemuxer2.default.probe(data)).match) {
  3065. // Always create new FLVDemuxer
  3066. this._demuxer = new _flvDemuxer2.default(probeData, this._config);
  3067. if (!this._remuxer) {
  3068. this._remuxer = new _mp4Remuxer2.default(this._config);
  3069. }
  3070. var mds = this._mediaDataSource;
  3071. if (mds.duration != undefined && !isNaN(mds.duration)) {
  3072. this._demuxer.overridedDuration = mds.duration;
  3073. }
  3074. if (typeof mds.hasAudio === 'boolean') {
  3075. this._demuxer.overridedHasAudio = mds.hasAudio;
  3076. }
  3077. if (typeof mds.hasVideo === 'boolean') {
  3078. this._demuxer.overridedHasVideo = mds.hasVideo;
  3079. }
  3080. this._demuxer.timestampBase = mds.segments[this._currentSegmentIndex].timestampBase;
  3081. this._demuxer.onError = this._onDemuxException.bind(this);
  3082. this._demuxer.onMediaInfo = this._onMediaInfo.bind(this);
  3083. this._demuxer.onMetaDataArrived = this._onMetaDataArrived.bind(this);
  3084. this._remuxer.bindDataSource(this._demuxer.bindDataSource(this._ioctl));
  3085. this._remuxer.onInitSegment = this._onRemuxerInitSegmentArrival.bind(this);
  3086. this._remuxer.onMediaSegment = this._onRemuxerMediaSegmentArrival.bind(this);
  3087. consumed = this._demuxer.parseChunks(data, byteStart);
  3088. } else {
  3089. probeData = null;
  3090. _logger2.default.e(this.TAG, 'Non-FLV, Unsupported media type!');
  3091. Promise.resolve().then(function () {
  3092. _this._internalAbort();
  3093. });
  3094. this._emitter.emit(_transmuxingEvents2.default.DEMUX_ERROR, _demuxErrors2.default.FORMAT_UNSUPPORTED, 'Non-FLV, Unsupported media type');
  3095. consumed = 0;
  3096. }
  3097. return consumed;
  3098. }
  3099. }, {
  3100. key: '_onMediaInfo',
  3101. value: function _onMediaInfo(mediaInfo) {
  3102. var _this2 = this;
  3103. if (this._mediaInfo == null) {
  3104. // Store first segment's mediainfo as global mediaInfo
  3105. this._mediaInfo = Object.assign({}, mediaInfo);
  3106. this._mediaInfo.keyframesIndex = null;
  3107. this._mediaInfo.segments = [];
  3108. this._mediaInfo.segmentCount = this._mediaDataSource.segments.length;
  3109. Object.setPrototypeOf(this._mediaInfo, _mediaInfo2.default.prototype);
  3110. }
  3111. var segmentInfo = Object.assign({}, mediaInfo);
  3112. Object.setPrototypeOf(segmentInfo, _mediaInfo2.default.prototype);
  3113. this._mediaInfo.segments[this._currentSegmentIndex] = segmentInfo;
  3114. // notify mediaInfo update
  3115. this._reportSegmentMediaInfo(this._currentSegmentIndex);
  3116. if (this._pendingSeekTime != null) {
  3117. Promise.resolve().then(function () {
  3118. var target = _this2._pendingSeekTime;
  3119. _this2._pendingSeekTime = null;
  3120. _this2.seek(target);
  3121. });
  3122. }
  3123. }
  3124. }, {
  3125. key: '_onMetaDataArrived',
  3126. value: function _onMetaDataArrived(metadata) {
  3127. this._emitter.emit(_transmuxingEvents2.default.METADATA_ARRIVED, metadata);
  3128. }
  3129. }, {
  3130. key: '_onIOSeeked',
  3131. value: function _onIOSeeked() {
  3132. this._remuxer.insertDiscontinuity();
  3133. }
  3134. }, {
  3135. key: '_onIOComplete',
  3136. value: function _onIOComplete(extraData) {
  3137. var segmentIndex = extraData;
  3138. var nextSegmentIndex = segmentIndex + 1;
  3139. if (nextSegmentIndex < this._mediaDataSource.segments.length) {
  3140. this._internalAbort();
  3141. this._remuxer.flushStashedSamples();
  3142. this._loadSegment(nextSegmentIndex);
  3143. } else {
  3144. this._remuxer.flushStashedSamples();
  3145. this._emitter.emit(_transmuxingEvents2.default.LOADING_COMPLETE);
  3146. this._disableStatisticsReporter();
  3147. }
  3148. }
  3149. }, {
  3150. key: '_onIORedirect',
  3151. value: function _onIORedirect(redirectedURL) {
  3152. var segmentIndex = this._ioctl.extraData;
  3153. this._mediaDataSource.segments[segmentIndex].redirectedURL = redirectedURL;
  3154. }
  3155. }, {
  3156. key: '_onIORecoveredEarlyEof',
  3157. value: function _onIORecoveredEarlyEof() {
  3158. this._emitter.emit(_transmuxingEvents2.default.RECOVERED_EARLY_EOF);
  3159. }
  3160. }, {
  3161. key: '_onIOException',
  3162. value: function _onIOException(type, info) {
  3163. _logger2.default.e(this.TAG, 'IOException: type = ' + type + ', code = ' + info.code + ', msg = ' + info.msg);
  3164. this._emitter.emit(_transmuxingEvents2.default.IO_ERROR, type, info);
  3165. this._disableStatisticsReporter();
  3166. }
  3167. }, {
  3168. key: '_onDemuxException',
  3169. value: function _onDemuxException(type, info) {
  3170. _logger2.default.e(this.TAG, 'DemuxException: type = ' + type + ', info = ' + info);
  3171. this._emitter.emit(_transmuxingEvents2.default.DEMUX_ERROR, type, info);
  3172. }
  3173. }, {
  3174. key: '_onRemuxerInitSegmentArrival',
  3175. value: function _onRemuxerInitSegmentArrival(type, initSegment) {
  3176. this._emitter.emit(_transmuxingEvents2.default.INIT_SEGMENT, type, initSegment);
  3177. }
  3178. }, {
  3179. key: '_onRemuxerMediaSegmentArrival',
  3180. value: function _onRemuxerMediaSegmentArrival(type, mediaSegment) {
  3181. if (this._pendingSeekTime != null) {
  3182. // Media segments after new-segment cross-seeking should be dropped.
  3183. return;
  3184. }
  3185. this._emitter.emit(_transmuxingEvents2.default.MEDIA_SEGMENT, type, mediaSegment);
  3186. // Resolve pending seekPoint
  3187. if (this._pendingResolveSeekPoint != null && type === 'video') {
  3188. var syncPoints = mediaSegment.info.syncPoints;
  3189. var seekpoint = this._pendingResolveSeekPoint;
  3190. this._pendingResolveSeekPoint = null;
  3191. // Safari: Pass PTS for recommend_seekpoint
  3192. if (_browser2.default.safari && syncPoints.length > 0 && syncPoints[0].originalDts === seekpoint) {
  3193. seekpoint = syncPoints[0].pts;
  3194. }
  3195. // else: use original DTS (keyframe.milliseconds)
  3196. this._emitter.emit(_transmuxingEvents2.default.RECOMMEND_SEEKPOINT, seekpoint);
  3197. }
  3198. }
  3199. }, {
  3200. key: '_enableStatisticsReporter',
  3201. value: function _enableStatisticsReporter() {
  3202. if (this._statisticsReporter == null) {
  3203. this._statisticsReporter = self.setInterval(this._reportStatisticsInfo.bind(this), this._config.statisticsInfoReportInterval);
  3204. }
  3205. }
  3206. }, {
  3207. key: '_disableStatisticsReporter',
  3208. value: function _disableStatisticsReporter() {
  3209. if (this._statisticsReporter) {
  3210. self.clearInterval(this._statisticsReporter);
  3211. this._statisticsReporter = null;
  3212. }
  3213. }
  3214. }, {
  3215. key: '_reportSegmentMediaInfo',
  3216. value: function _reportSegmentMediaInfo(segmentIndex) {
  3217. var segmentInfo = this._mediaInfo.segments[segmentIndex];
  3218. var exportInfo = Object.assign({}, segmentInfo);
  3219. exportInfo.duration = this._mediaInfo.duration;
  3220. exportInfo.segmentCount = this._mediaInfo.segmentCount;
  3221. delete exportInfo.segments;
  3222. delete exportInfo.keyframesIndex;
  3223. this._emitter.emit(_transmuxingEvents2.default.MEDIA_INFO, exportInfo);
  3224. }
  3225. }, {
  3226. key: '_reportStatisticsInfo',
  3227. value: function _reportStatisticsInfo() {
  3228. var info = {};
  3229. info.url = this._ioctl.currentURL;
  3230. info.hasRedirect = this._ioctl.hasRedirect;
  3231. if (info.hasRedirect) {
  3232. info.redirectedURL = this._ioctl.currentRedirectedURL;
  3233. }
  3234. info.speed = this._ioctl.currentSpeed;
  3235. info.loaderType = this._ioctl.loaderType;
  3236. info.currentSegmentIndex = this._currentSegmentIndex;
  3237. info.totalSegmentCount = this._mediaDataSource.segments.length;
  3238. this._emitter.emit(_transmuxingEvents2.default.STATISTICS_INFO, info);
  3239. }
  3240. }]);
  3241. return TransmuxingController;
  3242. }();
  3243. exports.default = TransmuxingController;
  3244. },{"../demux/demux-errors.js":16,"../demux/flv-demuxer.js":18,"../io/io-controller.js":23,"../io/loader.js":24,"../remux/mp4-remuxer.js":38,"../utils/browser.js":39,"../utils/logger.js":41,"./media-info.js":7,"./transmuxing-events.js":13,"events":2}],13:[function(_dereq_,module,exports){
  3245. 'use strict';
  3246. Object.defineProperty(exports, "__esModule", {
  3247. value: true
  3248. });
  3249. /*
  3250. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3251. *
  3252. * @author zheng qian <xqq@xqq.im>
  3253. *
  3254. * Licensed under the Apache License, Version 2.0 (the "License");
  3255. * you may not use this file except in compliance with the License.
  3256. * You may obtain a copy of the License at
  3257. *
  3258. * http://www.apache.org/licenses/LICENSE-2.0
  3259. *
  3260. * Unless required by applicable law or agreed to in writing, software
  3261. * distributed under the License is distributed on an "AS IS" BASIS,
  3262. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3263. * See the License for the specific language governing permissions and
  3264. * limitations under the License.
  3265. */
  3266. var TransmuxingEvents = {
  3267. IO_ERROR: 'io_error',
  3268. DEMUX_ERROR: 'demux_error',
  3269. INIT_SEGMENT: 'init_segment',
  3270. MEDIA_SEGMENT: 'media_segment',
  3271. LOADING_COMPLETE: 'loading_complete',
  3272. RECOVERED_EARLY_EOF: 'recovered_early_eof',
  3273. MEDIA_INFO: 'media_info',
  3274. METADATA_ARRIVED: 'metadata_arrived',
  3275. STATISTICS_INFO: 'statistics_info',
  3276. RECOMMEND_SEEKPOINT: 'recommend_seekpoint'
  3277. };
  3278. exports.default = TransmuxingEvents;
  3279. },{}],14:[function(_dereq_,module,exports){
  3280. 'use strict';
  3281. Object.defineProperty(exports, "__esModule", {
  3282. value: true
  3283. });
  3284. var _logger = _dereq_('../utils/logger.js');
  3285. var _logger2 = _interopRequireDefault(_logger);
  3286. var _loggingControl = _dereq_('../utils/logging-control.js');
  3287. var _loggingControl2 = _interopRequireDefault(_loggingControl);
  3288. var _polyfill = _dereq_('../utils/polyfill.js');
  3289. var _polyfill2 = _interopRequireDefault(_polyfill);
  3290. var _transmuxingController = _dereq_('./transmuxing-controller.js');
  3291. var _transmuxingController2 = _interopRequireDefault(_transmuxingController);
  3292. var _transmuxingEvents = _dereq_('./transmuxing-events.js');
  3293. var _transmuxingEvents2 = _interopRequireDefault(_transmuxingEvents);
  3294. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  3295. /* post message to worker:
  3296. data: {
  3297. cmd: string
  3298. param: any
  3299. }
  3300. receive message from worker:
  3301. data: {
  3302. msg: string,
  3303. data: any
  3304. }
  3305. */
  3306. var TransmuxingWorker = function TransmuxingWorker(self) {
  3307. var TAG = 'TransmuxingWorker';
  3308. var controller = null;
  3309. var logcatListener = onLogcatCallback.bind(this);
  3310. _polyfill2.default.install();
  3311. self.addEventListener('message', function (e) {
  3312. switch (e.data.cmd) {
  3313. case 'init':
  3314. controller = new _transmuxingController2.default(e.data.param[0], e.data.param[1]);
  3315. controller.on(_transmuxingEvents2.default.IO_ERROR, onIOError.bind(this));
  3316. controller.on(_transmuxingEvents2.default.DEMUX_ERROR, onDemuxError.bind(this));
  3317. controller.on(_transmuxingEvents2.default.INIT_SEGMENT, onInitSegment.bind(this));
  3318. controller.on(_transmuxingEvents2.default.MEDIA_SEGMENT, onMediaSegment.bind(this));
  3319. controller.on(_transmuxingEvents2.default.LOADING_COMPLETE, onLoadingComplete.bind(this));
  3320. controller.on(_transmuxingEvents2.default.RECOVERED_EARLY_EOF, onRecoveredEarlyEof.bind(this));
  3321. controller.on(_transmuxingEvents2.default.MEDIA_INFO, onMediaInfo.bind(this));
  3322. controller.on(_transmuxingEvents2.default.METADATA_ARRIVED, onMetaDataArrived.bind(this));
  3323. controller.on(_transmuxingEvents2.default.STATISTICS_INFO, onStatisticsInfo.bind(this));
  3324. controller.on(_transmuxingEvents2.default.RECOMMEND_SEEKPOINT, onRecommendSeekpoint.bind(this));
  3325. break;
  3326. case 'destroy':
  3327. if (controller) {
  3328. controller.destroy();
  3329. controller = null;
  3330. }
  3331. self.postMessage({ msg: 'destroyed' });
  3332. break;
  3333. case 'start':
  3334. controller.start();
  3335. break;
  3336. case 'stop':
  3337. controller.stop();
  3338. break;
  3339. case 'seek':
  3340. controller.seek(e.data.param);
  3341. break;
  3342. case 'pause':
  3343. controller.pause();
  3344. break;
  3345. case 'resume':
  3346. controller.resume();
  3347. break;
  3348. case 'logging_config':
  3349. {
  3350. var config = e.data.param;
  3351. _loggingControl2.default.applyConfig(config);
  3352. if (config.enableCallback === true) {
  3353. _loggingControl2.default.addLogListener(logcatListener);
  3354. } else {
  3355. _loggingControl2.default.removeLogListener(logcatListener);
  3356. }
  3357. break;
  3358. }
  3359. }
  3360. });
  3361. function onInitSegment(type, initSegment) {
  3362. var obj = {
  3363. msg: _transmuxingEvents2.default.INIT_SEGMENT,
  3364. data: {
  3365. type: type,
  3366. data: initSegment
  3367. }
  3368. };
  3369. self.postMessage(obj, [initSegment.data]); // data: ArrayBuffer
  3370. }
  3371. function onMediaSegment(type, mediaSegment) {
  3372. var obj = {
  3373. msg: _transmuxingEvents2.default.MEDIA_SEGMENT,
  3374. data: {
  3375. type: type,
  3376. data: mediaSegment
  3377. }
  3378. };
  3379. self.postMessage(obj, [mediaSegment.data]); // data: ArrayBuffer
  3380. }
  3381. function onLoadingComplete() {
  3382. var obj = {
  3383. msg: _transmuxingEvents2.default.LOADING_COMPLETE
  3384. };
  3385. self.postMessage(obj);
  3386. }
  3387. function onRecoveredEarlyEof() {
  3388. var obj = {
  3389. msg: _transmuxingEvents2.default.RECOVERED_EARLY_EOF
  3390. };
  3391. self.postMessage(obj);
  3392. }
  3393. function onMediaInfo(mediaInfo) {
  3394. var obj = {
  3395. msg: _transmuxingEvents2.default.MEDIA_INFO,
  3396. data: mediaInfo
  3397. };
  3398. self.postMessage(obj);
  3399. }
  3400. function onMetaDataArrived(metadata) {
  3401. var obj = {
  3402. msg: _transmuxingEvents2.default.METADATA_ARRIVED,
  3403. data: metadata
  3404. };
  3405. self.postMessage(obj);
  3406. }
  3407. function onStatisticsInfo(statInfo) {
  3408. var obj = {
  3409. msg: _transmuxingEvents2.default.STATISTICS_INFO,
  3410. data: statInfo
  3411. };
  3412. self.postMessage(obj);
  3413. }
  3414. function onIOError(type, info) {
  3415. self.postMessage({
  3416. msg: _transmuxingEvents2.default.IO_ERROR,
  3417. data: {
  3418. type: type,
  3419. info: info
  3420. }
  3421. });
  3422. }
  3423. function onDemuxError(type, info) {
  3424. self.postMessage({
  3425. msg: _transmuxingEvents2.default.DEMUX_ERROR,
  3426. data: {
  3427. type: type,
  3428. info: info
  3429. }
  3430. });
  3431. }
  3432. function onRecommendSeekpoint(milliseconds) {
  3433. self.postMessage({
  3434. msg: _transmuxingEvents2.default.RECOMMEND_SEEKPOINT,
  3435. data: milliseconds
  3436. });
  3437. }
  3438. function onLogcatCallback(type, str) {
  3439. self.postMessage({
  3440. msg: 'logcat_callback',
  3441. data: {
  3442. type: type,
  3443. logcat: str
  3444. }
  3445. });
  3446. }
  3447. }; /*
  3448. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3449. *
  3450. * @author zheng qian <xqq@xqq.im>
  3451. *
  3452. * Licensed under the Apache License, Version 2.0 (the "License");
  3453. * you may not use this file except in compliance with the License.
  3454. * You may obtain a copy of the License at
  3455. *
  3456. * http://www.apache.org/licenses/LICENSE-2.0
  3457. *
  3458. * Unless required by applicable law or agreed to in writing, software
  3459. * distributed under the License is distributed on an "AS IS" BASIS,
  3460. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3461. * See the License for the specific language governing permissions and
  3462. * limitations under the License.
  3463. */
  3464. exports.default = TransmuxingWorker;
  3465. },{"../utils/logger.js":41,"../utils/logging-control.js":42,"../utils/polyfill.js":43,"./transmuxing-controller.js":12,"./transmuxing-events.js":13}],15:[function(_dereq_,module,exports){
  3466. 'use strict';
  3467. Object.defineProperty(exports, "__esModule", {
  3468. value: true
  3469. });
  3470. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  3471. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3472. *
  3473. * @author zheng qian <xqq@xqq.im>
  3474. *
  3475. * Licensed under the Apache License, Version 2.0 (the "License");
  3476. * you may not use this file except in compliance with the License.
  3477. * You may obtain a copy of the License at
  3478. *
  3479. * http://www.apache.org/licenses/LICENSE-2.0
  3480. *
  3481. * Unless required by applicable law or agreed to in writing, software
  3482. * distributed under the License is distributed on an "AS IS" BASIS,
  3483. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3484. * See the License for the specific language governing permissions and
  3485. * limitations under the License.
  3486. */
  3487. var _logger = _dereq_('../utils/logger.js');
  3488. var _logger2 = _interopRequireDefault(_logger);
  3489. var _utf8Conv = _dereq_('../utils/utf8-conv.js');
  3490. var _utf8Conv2 = _interopRequireDefault(_utf8Conv);
  3491. var _exception = _dereq_('../utils/exception.js');
  3492. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  3493. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3494. var le = function () {
  3495. var buf = new ArrayBuffer(2);
  3496. new DataView(buf).setInt16(0, 256, true); // little-endian write
  3497. return new Int16Array(buf)[0] === 256; // platform-spec read, if equal then LE
  3498. }();
  3499. var AMF = function () {
  3500. function AMF() {
  3501. _classCallCheck(this, AMF);
  3502. }
  3503. _createClass(AMF, null, [{
  3504. key: 'parseScriptData',
  3505. value: function parseScriptData(arrayBuffer, dataOffset, dataSize) {
  3506. var data = {};
  3507. try {
  3508. var name = AMF.parseValue(arrayBuffer, dataOffset, dataSize);
  3509. var value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
  3510. data[name.data] = value.data;
  3511. } catch (e) {
  3512. _logger2.default.e('AMF', e.toString());
  3513. }
  3514. return data;
  3515. }
  3516. }, {
  3517. key: 'parseObject',
  3518. value: function parseObject(arrayBuffer, dataOffset, dataSize) {
  3519. if (dataSize < 3) {
  3520. throw new _exception.IllegalStateException('Data not enough when parse ScriptDataObject');
  3521. }
  3522. var name = AMF.parseString(arrayBuffer, dataOffset, dataSize);
  3523. var value = AMF.parseValue(arrayBuffer, dataOffset + name.size, dataSize - name.size);
  3524. var isObjectEnd = value.objectEnd;
  3525. return {
  3526. data: {
  3527. name: name.data,
  3528. value: value.data
  3529. },
  3530. size: name.size + value.size,
  3531. objectEnd: isObjectEnd
  3532. };
  3533. }
  3534. }, {
  3535. key: 'parseVariable',
  3536. value: function parseVariable(arrayBuffer, dataOffset, dataSize) {
  3537. return AMF.parseObject(arrayBuffer, dataOffset, dataSize);
  3538. }
  3539. }, {
  3540. key: 'parseString',
  3541. value: function parseString(arrayBuffer, dataOffset, dataSize) {
  3542. if (dataSize < 2) {
  3543. throw new _exception.IllegalStateException('Data not enough when parse String');
  3544. }
  3545. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  3546. var length = v.getUint16(0, !le);
  3547. var str = void 0;
  3548. if (length > 0) {
  3549. str = (0, _utf8Conv2.default)(new Uint8Array(arrayBuffer, dataOffset + 2, length));
  3550. } else {
  3551. str = '';
  3552. }
  3553. return {
  3554. data: str,
  3555. size: 2 + length
  3556. };
  3557. }
  3558. }, {
  3559. key: 'parseLongString',
  3560. value: function parseLongString(arrayBuffer, dataOffset, dataSize) {
  3561. if (dataSize < 4) {
  3562. throw new _exception.IllegalStateException('Data not enough when parse LongString');
  3563. }
  3564. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  3565. var length = v.getUint32(0, !le);
  3566. var str = void 0;
  3567. if (length > 0) {
  3568. str = (0, _utf8Conv2.default)(new Uint8Array(arrayBuffer, dataOffset + 4, length));
  3569. } else {
  3570. str = '';
  3571. }
  3572. return {
  3573. data: str,
  3574. size: 4 + length
  3575. };
  3576. }
  3577. }, {
  3578. key: 'parseDate',
  3579. value: function parseDate(arrayBuffer, dataOffset, dataSize) {
  3580. if (dataSize < 10) {
  3581. throw new _exception.IllegalStateException('Data size invalid when parse Date');
  3582. }
  3583. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  3584. var timestamp = v.getFloat64(0, !le);
  3585. var localTimeOffset = v.getInt16(8, !le);
  3586. timestamp += localTimeOffset * 60 * 1000; // get UTC time
  3587. return {
  3588. data: new Date(timestamp),
  3589. size: 8 + 2
  3590. };
  3591. }
  3592. }, {
  3593. key: 'parseValue',
  3594. value: function parseValue(arrayBuffer, dataOffset, dataSize) {
  3595. if (dataSize < 1) {
  3596. throw new _exception.IllegalStateException('Data not enough when parse Value');
  3597. }
  3598. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  3599. var offset = 1;
  3600. var type = v.getUint8(0);
  3601. var value = void 0;
  3602. var objectEnd = false;
  3603. try {
  3604. switch (type) {
  3605. case 0:
  3606. // Number(Double) type
  3607. value = v.getFloat64(1, !le);
  3608. offset += 8;
  3609. break;
  3610. case 1:
  3611. {
  3612. // Boolean type
  3613. var b = v.getUint8(1);
  3614. value = b ? true : false;
  3615. offset += 1;
  3616. break;
  3617. }
  3618. case 2:
  3619. {
  3620. // String type
  3621. var amfstr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
  3622. value = amfstr.data;
  3623. offset += amfstr.size;
  3624. break;
  3625. }
  3626. case 3:
  3627. {
  3628. // Object(s) type
  3629. value = {};
  3630. var terminal = 0; // workaround for malformed Objects which has missing ScriptDataObjectEnd
  3631. if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
  3632. terminal = 3;
  3633. }
  3634. while (offset < dataSize - 4) {
  3635. // 4 === type(UI8) + ScriptDataObjectEnd(UI24)
  3636. var amfobj = AMF.parseObject(arrayBuffer, dataOffset + offset, dataSize - offset - terminal);
  3637. if (amfobj.objectEnd) break;
  3638. value[amfobj.data.name] = amfobj.data.value;
  3639. offset += amfobj.size;
  3640. }
  3641. if (offset <= dataSize - 3) {
  3642. var marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
  3643. if (marker === 9) {
  3644. offset += 3;
  3645. }
  3646. }
  3647. break;
  3648. }
  3649. case 8:
  3650. {
  3651. // ECMA array type (Mixed array)
  3652. value = {};
  3653. offset += 4; // ECMAArrayLength(UI32)
  3654. var _terminal = 0; // workaround for malformed MixedArrays which has missing ScriptDataObjectEnd
  3655. if ((v.getUint32(dataSize - 4, !le) & 0x00FFFFFF) === 9) {
  3656. _terminal = 3;
  3657. }
  3658. while (offset < dataSize - 8) {
  3659. // 8 === type(UI8) + ECMAArrayLength(UI32) + ScriptDataVariableEnd(UI24)
  3660. var amfvar = AMF.parseVariable(arrayBuffer, dataOffset + offset, dataSize - offset - _terminal);
  3661. if (amfvar.objectEnd) break;
  3662. value[amfvar.data.name] = amfvar.data.value;
  3663. offset += amfvar.size;
  3664. }
  3665. if (offset <= dataSize - 3) {
  3666. var _marker = v.getUint32(offset - 1, !le) & 0x00FFFFFF;
  3667. if (_marker === 9) {
  3668. offset += 3;
  3669. }
  3670. }
  3671. break;
  3672. }
  3673. case 9:
  3674. // ScriptDataObjectEnd
  3675. value = undefined;
  3676. offset = 1;
  3677. objectEnd = true;
  3678. break;
  3679. case 10:
  3680. {
  3681. // Strict array type
  3682. // ScriptDataValue[n]. NOTE: according to video_file_format_spec_v10_1.pdf
  3683. value = [];
  3684. var strictArrayLength = v.getUint32(1, !le);
  3685. offset += 4;
  3686. for (var i = 0; i < strictArrayLength; i++) {
  3687. var val = AMF.parseValue(arrayBuffer, dataOffset + offset, dataSize - offset);
  3688. value.push(val.data);
  3689. offset += val.size;
  3690. }
  3691. break;
  3692. }
  3693. case 11:
  3694. {
  3695. // Date type
  3696. var date = AMF.parseDate(arrayBuffer, dataOffset + 1, dataSize - 1);
  3697. value = date.data;
  3698. offset += date.size;
  3699. break;
  3700. }
  3701. case 12:
  3702. {
  3703. // Long string type
  3704. var amfLongStr = AMF.parseString(arrayBuffer, dataOffset + 1, dataSize - 1);
  3705. value = amfLongStr.data;
  3706. offset += amfLongStr.size;
  3707. break;
  3708. }
  3709. default:
  3710. // ignore and skip
  3711. offset = dataSize;
  3712. _logger2.default.w('AMF', 'Unsupported AMF value type ' + type);
  3713. }
  3714. } catch (e) {
  3715. _logger2.default.e('AMF', e.toString());
  3716. }
  3717. return {
  3718. data: value,
  3719. size: offset,
  3720. objectEnd: objectEnd
  3721. };
  3722. }
  3723. }]);
  3724. return AMF;
  3725. }();
  3726. exports.default = AMF;
  3727. },{"../utils/exception.js":40,"../utils/logger.js":41,"../utils/utf8-conv.js":44}],16:[function(_dereq_,module,exports){
  3728. 'use strict';
  3729. Object.defineProperty(exports, "__esModule", {
  3730. value: true
  3731. });
  3732. /*
  3733. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3734. *
  3735. * @author zheng qian <xqq@xqq.im>
  3736. *
  3737. * Licensed under the Apache License, Version 2.0 (the "License");
  3738. * you may not use this file except in compliance with the License.
  3739. * You may obtain a copy of the License at
  3740. *
  3741. * http://www.apache.org/licenses/LICENSE-2.0
  3742. *
  3743. * Unless required by applicable law or agreed to in writing, software
  3744. * distributed under the License is distributed on an "AS IS" BASIS,
  3745. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3746. * See the License for the specific language governing permissions and
  3747. * limitations under the License.
  3748. */
  3749. var DemuxErrors = {
  3750. OK: 'OK',
  3751. FORMAT_ERROR: 'FormatError',
  3752. FORMAT_UNSUPPORTED: 'FormatUnsupported',
  3753. CODEC_UNSUPPORTED: 'CodecUnsupported'
  3754. };
  3755. exports.default = DemuxErrors;
  3756. },{}],17:[function(_dereq_,module,exports){
  3757. 'use strict';
  3758. Object.defineProperty(exports, "__esModule", {
  3759. value: true
  3760. });
  3761. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  3762. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3763. *
  3764. * @author zheng qian <xqq@xqq.im>
  3765. *
  3766. * Licensed under the Apache License, Version 2.0 (the "License");
  3767. * you may not use this file except in compliance with the License.
  3768. * You may obtain a copy of the License at
  3769. *
  3770. * http://www.apache.org/licenses/LICENSE-2.0
  3771. *
  3772. * Unless required by applicable law or agreed to in writing, software
  3773. * distributed under the License is distributed on an "AS IS" BASIS,
  3774. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3775. * See the License for the specific language governing permissions and
  3776. * limitations under the License.
  3777. */
  3778. var _exception = _dereq_('../utils/exception.js');
  3779. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3780. // Exponential-Golomb buffer decoder
  3781. var ExpGolomb = function () {
  3782. function ExpGolomb(uint8array) {
  3783. _classCallCheck(this, ExpGolomb);
  3784. this.TAG = 'ExpGolomb';
  3785. this._buffer = uint8array;
  3786. this._buffer_index = 0;
  3787. this._total_bytes = uint8array.byteLength;
  3788. this._total_bits = uint8array.byteLength * 8;
  3789. this._current_word = 0;
  3790. this._current_word_bits_left = 0;
  3791. }
  3792. _createClass(ExpGolomb, [{
  3793. key: 'destroy',
  3794. value: function destroy() {
  3795. this._buffer = null;
  3796. }
  3797. }, {
  3798. key: '_fillCurrentWord',
  3799. value: function _fillCurrentWord() {
  3800. var buffer_bytes_left = this._total_bytes - this._buffer_index;
  3801. if (buffer_bytes_left <= 0) throw new _exception.IllegalStateException('ExpGolomb: _fillCurrentWord() but no bytes available');
  3802. var bytes_read = Math.min(4, buffer_bytes_left);
  3803. var word = new Uint8Array(4);
  3804. word.set(this._buffer.subarray(this._buffer_index, this._buffer_index + bytes_read));
  3805. this._current_word = new DataView(word.buffer).getUint32(0, false);
  3806. this._buffer_index += bytes_read;
  3807. this._current_word_bits_left = bytes_read * 8;
  3808. }
  3809. }, {
  3810. key: 'readBits',
  3811. value: function readBits(bits) {
  3812. if (bits > 32) throw new _exception.InvalidArgumentException('ExpGolomb: readBits() bits exceeded max 32bits!');
  3813. if (bits <= this._current_word_bits_left) {
  3814. var _result = this._current_word >>> 32 - bits;
  3815. this._current_word <<= bits;
  3816. this._current_word_bits_left -= bits;
  3817. return _result;
  3818. }
  3819. var result = this._current_word_bits_left ? this._current_word : 0;
  3820. result = result >>> 32 - this._current_word_bits_left;
  3821. var bits_need_left = bits - this._current_word_bits_left;
  3822. this._fillCurrentWord();
  3823. var bits_read_next = Math.min(bits_need_left, this._current_word_bits_left);
  3824. var result2 = this._current_word >>> 32 - bits_read_next;
  3825. this._current_word <<= bits_read_next;
  3826. this._current_word_bits_left -= bits_read_next;
  3827. result = result << bits_read_next | result2;
  3828. return result;
  3829. }
  3830. }, {
  3831. key: 'readBool',
  3832. value: function readBool() {
  3833. return this.readBits(1) === 1;
  3834. }
  3835. }, {
  3836. key: 'readByte',
  3837. value: function readByte() {
  3838. return this.readBits(8);
  3839. }
  3840. }, {
  3841. key: '_skipLeadingZero',
  3842. value: function _skipLeadingZero() {
  3843. var zero_count = void 0;
  3844. for (zero_count = 0; zero_count < this._current_word_bits_left; zero_count++) {
  3845. if (0 !== (this._current_word & 0x80000000 >>> zero_count)) {
  3846. this._current_word <<= zero_count;
  3847. this._current_word_bits_left -= zero_count;
  3848. return zero_count;
  3849. }
  3850. }
  3851. this._fillCurrentWord();
  3852. return zero_count + this._skipLeadingZero();
  3853. }
  3854. }, {
  3855. key: 'readUEG',
  3856. value: function readUEG() {
  3857. // unsigned exponential golomb
  3858. var leading_zeros = this._skipLeadingZero();
  3859. return this.readBits(leading_zeros + 1) - 1;
  3860. }
  3861. }, {
  3862. key: 'readSEG',
  3863. value: function readSEG() {
  3864. // signed exponential golomb
  3865. var value = this.readUEG();
  3866. if (value & 0x01) {
  3867. return value + 1 >>> 1;
  3868. } else {
  3869. return -1 * (value >>> 1);
  3870. }
  3871. }
  3872. }]);
  3873. return ExpGolomb;
  3874. }();
  3875. exports.default = ExpGolomb;
  3876. },{"../utils/exception.js":40}],18:[function(_dereq_,module,exports){
  3877. 'use strict';
  3878. Object.defineProperty(exports, "__esModule", {
  3879. value: true
  3880. });
  3881. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  3882. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  3883. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  3884. *
  3885. * @author zheng qian <xqq@xqq.im>
  3886. *
  3887. * Licensed under the Apache License, Version 2.0 (the "License");
  3888. * you may not use this file except in compliance with the License.
  3889. * You may obtain a copy of the License at
  3890. *
  3891. * http://www.apache.org/licenses/LICENSE-2.0
  3892. *
  3893. * Unless required by applicable law or agreed to in writing, software
  3894. * distributed under the License is distributed on an "AS IS" BASIS,
  3895. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  3896. * See the License for the specific language governing permissions and
  3897. * limitations under the License.
  3898. */
  3899. var _logger = _dereq_('../utils/logger.js');
  3900. var _logger2 = _interopRequireDefault(_logger);
  3901. var _amfParser = _dereq_('./amf-parser.js');
  3902. var _amfParser2 = _interopRequireDefault(_amfParser);
  3903. var _spsParser = _dereq_('./sps-parser.js');
  3904. var _spsParser2 = _interopRequireDefault(_spsParser);
  3905. var _demuxErrors = _dereq_('./demux-errors.js');
  3906. var _demuxErrors2 = _interopRequireDefault(_demuxErrors);
  3907. var _mediaInfo = _dereq_('../core/media-info.js');
  3908. var _mediaInfo2 = _interopRequireDefault(_mediaInfo);
  3909. var _exception = _dereq_('../utils/exception.js');
  3910. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  3911. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3912. function Swap16(src) {
  3913. return src >>> 8 & 0xFF | (src & 0xFF) << 8;
  3914. }
  3915. function Swap32(src) {
  3916. return (src & 0xFF000000) >>> 24 | (src & 0x00FF0000) >>> 8 | (src & 0x0000FF00) << 8 | (src & 0x000000FF) << 24;
  3917. }
  3918. function ReadBig32(array, index) {
  3919. return array[index] << 24 | array[index + 1] << 16 | array[index + 2] << 8 | array[index + 3];
  3920. }
  3921. var FLVDemuxer = function () {
  3922. function FLVDemuxer(probeData, config) {
  3923. _classCallCheck(this, FLVDemuxer);
  3924. this.TAG = 'FLVDemuxer';
  3925. this._config = config;
  3926. this._onError = null;
  3927. this._onMediaInfo = null;
  3928. this._onMetaDataArrived = null;
  3929. this._onTrackMetadata = null;
  3930. this._onDataAvailable = null;
  3931. this._dataOffset = probeData.dataOffset;
  3932. this._firstParse = true;
  3933. this._dispatch = false;
  3934. this._hasAudio = probeData.hasAudioTrack;
  3935. this._hasVideo = probeData.hasVideoTrack;
  3936. this._hasAudioFlagOverrided = false;
  3937. this._hasVideoFlagOverrided = false;
  3938. this._audioInitialMetadataDispatched = false;
  3939. this._videoInitialMetadataDispatched = false;
  3940. this._mediaInfo = new _mediaInfo2.default();
  3941. this._mediaInfo.hasAudio = this._hasAudio;
  3942. this._mediaInfo.hasVideo = this._hasVideo;
  3943. this._metadata = null;
  3944. this._audioMetadata = null;
  3945. this._videoMetadata = null;
  3946. this._naluLengthSize = 4;
  3947. this._timestampBase = 0; // int32, in milliseconds
  3948. this._timescale = 1000;
  3949. this._duration = 0; // int32, in milliseconds
  3950. this._durationOverrided = false;
  3951. this._referenceFrameRate = {
  3952. fixed: true,
  3953. fps: 23.976,
  3954. fps_num: 23976,
  3955. fps_den: 1000
  3956. };
  3957. this._flvSoundRateTable = [5500, 11025, 22050, 44100, 48000];
  3958. this._mpegSamplingRates = [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
  3959. this._mpegAudioV10SampleRateTable = [44100, 48000, 32000, 0];
  3960. this._mpegAudioV20SampleRateTable = [22050, 24000, 16000, 0];
  3961. this._mpegAudioV25SampleRateTable = [11025, 12000, 8000, 0];
  3962. this._mpegAudioL1BitRateTable = [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, -1];
  3963. this._mpegAudioL2BitRateTable = [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, -1];
  3964. this._mpegAudioL3BitRateTable = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, -1];
  3965. this._videoTrack = { type: 'video', id: 1, sequenceNumber: 0, samples: [], length: 0 };
  3966. this._audioTrack = { type: 'audio', id: 2, sequenceNumber: 0, samples: [], length: 0 };
  3967. this._littleEndian = function () {
  3968. var buf = new ArrayBuffer(2);
  3969. new DataView(buf).setInt16(0, 256, true); // little-endian write
  3970. return new Int16Array(buf)[0] === 256; // platform-spec read, if equal then LE
  3971. }();
  3972. }
  3973. _createClass(FLVDemuxer, [{
  3974. key: 'destroy',
  3975. value: function destroy() {
  3976. this._mediaInfo = null;
  3977. this._metadata = null;
  3978. this._audioMetadata = null;
  3979. this._videoMetadata = null;
  3980. this._videoTrack = null;
  3981. this._audioTrack = null;
  3982. this._onError = null;
  3983. this._onMediaInfo = null;
  3984. this._onMetaDataArrived = null;
  3985. this._onTrackMetadata = null;
  3986. this._onDataAvailable = null;
  3987. }
  3988. }, {
  3989. key: 'bindDataSource',
  3990. value: function bindDataSource(loader) {
  3991. loader.onDataArrival = this.parseChunks.bind(this);
  3992. return this;
  3993. }
  3994. // prototype: function(type: string, metadata: any): void
  3995. }, {
  3996. key: 'resetMediaInfo',
  3997. value: function resetMediaInfo() {
  3998. this._mediaInfo = new _mediaInfo2.default();
  3999. }
  4000. }, {
  4001. key: '_isInitialMetadataDispatched',
  4002. value: function _isInitialMetadataDispatched() {
  4003. if (this._hasAudio && this._hasVideo) {
  4004. // both audio & video
  4005. return this._audioInitialMetadataDispatched && this._videoInitialMetadataDispatched;
  4006. }
  4007. if (this._hasAudio && !this._hasVideo) {
  4008. // audio only
  4009. return this._audioInitialMetadataDispatched;
  4010. }
  4011. if (!this._hasAudio && this._hasVideo) {
  4012. // video only
  4013. return this._videoInitialMetadataDispatched;
  4014. }
  4015. return false;
  4016. }
  4017. // function parseChunks(chunk: ArrayBuffer, byteStart: number): number;
  4018. }, {
  4019. key: 'parseChunks',
  4020. value: function parseChunks(chunk, byteStart) {
  4021. if (!this._onError || !this._onMediaInfo || !this._onTrackMetadata || !this._onDataAvailable) {
  4022. throw new _exception.IllegalStateException('Flv: onError & onMediaInfo & onTrackMetadata & onDataAvailable callback must be specified');
  4023. }
  4024. var offset = 0;
  4025. var le = this._littleEndian;
  4026. if (byteStart === 0) {
  4027. // buffer with FLV header
  4028. if (chunk.byteLength > 13) {
  4029. var probeData = FLVDemuxer.probe(chunk);
  4030. offset = probeData.dataOffset;
  4031. } else {
  4032. return 0;
  4033. }
  4034. }
  4035. if (this._firstParse) {
  4036. // handle PreviousTagSize0 before Tag1
  4037. this._firstParse = false;
  4038. if (byteStart + offset !== this._dataOffset) {
  4039. _logger2.default.w(this.TAG, 'First time parsing but chunk byteStart invalid!');
  4040. }
  4041. var v = new DataView(chunk, offset);
  4042. var prevTagSize0 = v.getUint32(0, !le);
  4043. if (prevTagSize0 !== 0) {
  4044. _logger2.default.w(this.TAG, 'PrevTagSize0 !== 0 !!!');
  4045. }
  4046. offset += 4;
  4047. }
  4048. while (offset < chunk.byteLength) {
  4049. this._dispatch = true;
  4050. var _v = new DataView(chunk, offset);
  4051. if (offset + 11 + 4 > chunk.byteLength) {
  4052. // data not enough for parsing an flv tag
  4053. break;
  4054. }
  4055. var tagType = _v.getUint8(0);
  4056. var dataSize = _v.getUint32(0, !le) & 0x00FFFFFF;
  4057. if (offset + 11 + dataSize + 4 > chunk.byteLength) {
  4058. // data not enough for parsing actual data body
  4059. break;
  4060. }
  4061. if (tagType !== 8 && tagType !== 9 && tagType !== 18) {
  4062. _logger2.default.w(this.TAG, 'Unsupported tag type ' + tagType + ', skipped');
  4063. // consume the whole tag (skip it)
  4064. offset += 11 + dataSize + 4;
  4065. continue;
  4066. }
  4067. var ts2 = _v.getUint8(4);
  4068. var ts1 = _v.getUint8(5);
  4069. var ts0 = _v.getUint8(6);
  4070. var ts3 = _v.getUint8(7);
  4071. var timestamp = ts0 | ts1 << 8 | ts2 << 16 | ts3 << 24;
  4072. var streamId = _v.getUint32(7, !le) & 0x00FFFFFF;
  4073. if (streamId !== 0) {
  4074. _logger2.default.w(this.TAG, 'Meet tag which has StreamID != 0!');
  4075. }
  4076. var dataOffset = offset + 11;
  4077. switch (tagType) {
  4078. case 8:
  4079. // Audio
  4080. this._parseAudioData(chunk, dataOffset, dataSize, timestamp);
  4081. break;
  4082. case 9:
  4083. // Video
  4084. this._parseVideoData(chunk, dataOffset, dataSize, timestamp, byteStart + offset);
  4085. break;
  4086. case 18:
  4087. // ScriptDataObject
  4088. this._parseScriptData(chunk, dataOffset, dataSize);
  4089. break;
  4090. }
  4091. var prevTagSize = _v.getUint32(11 + dataSize, !le);
  4092. if (prevTagSize !== 11 + dataSize) {
  4093. _logger2.default.w(this.TAG, 'Invalid PrevTagSize ' + prevTagSize);
  4094. }
  4095. offset += 11 + dataSize + 4; // tagBody + dataSize + prevTagSize
  4096. }
  4097. // dispatch parsed frames to consumer (typically, the remuxer)
  4098. if (this._isInitialMetadataDispatched()) {
  4099. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4100. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4101. }
  4102. }
  4103. return offset; // consumed bytes, just equals latest offset index
  4104. }
  4105. }, {
  4106. key: '_parseScriptData',
  4107. value: function _parseScriptData(arrayBuffer, dataOffset, dataSize) {
  4108. var scriptData = _amfParser2.default.parseScriptData(arrayBuffer, dataOffset, dataSize);
  4109. if (scriptData.hasOwnProperty('onMetaData')) {
  4110. if (scriptData.onMetaData == null || _typeof(scriptData.onMetaData) !== 'object') {
  4111. _logger2.default.w(this.TAG, 'Invalid onMetaData structure!');
  4112. return;
  4113. }
  4114. if (this._metadata) {
  4115. _logger2.default.w(this.TAG, 'Found another onMetaData tag!');
  4116. }
  4117. this._metadata = scriptData;
  4118. var onMetaData = this._metadata.onMetaData;
  4119. if (this._onMetaDataArrived) {
  4120. this._onMetaDataArrived(Object.assign({}, onMetaData));
  4121. }
  4122. if (typeof onMetaData.hasAudio === 'boolean') {
  4123. // hasAudio
  4124. if (this._hasAudioFlagOverrided === false) {
  4125. this._hasAudio = onMetaData.hasAudio;
  4126. this._mediaInfo.hasAudio = this._hasAudio;
  4127. }
  4128. }
  4129. if (typeof onMetaData.hasVideo === 'boolean') {
  4130. // hasVideo
  4131. if (this._hasVideoFlagOverrided === false) {
  4132. this._hasVideo = onMetaData.hasVideo;
  4133. this._mediaInfo.hasVideo = this._hasVideo;
  4134. }
  4135. }
  4136. if (typeof onMetaData.audiodatarate === 'number') {
  4137. // audiodatarate
  4138. this._mediaInfo.audioDataRate = onMetaData.audiodatarate;
  4139. }
  4140. if (typeof onMetaData.videodatarate === 'number') {
  4141. // videodatarate
  4142. this._mediaInfo.videoDataRate = onMetaData.videodatarate;
  4143. }
  4144. if (typeof onMetaData.width === 'number') {
  4145. // width
  4146. this._mediaInfo.width = onMetaData.width;
  4147. }
  4148. if (typeof onMetaData.height === 'number') {
  4149. // height
  4150. this._mediaInfo.height = onMetaData.height;
  4151. }
  4152. if (typeof onMetaData.duration === 'number') {
  4153. // duration
  4154. if (!this._durationOverrided) {
  4155. var duration = Math.floor(onMetaData.duration * this._timescale);
  4156. this._duration = duration;
  4157. this._mediaInfo.duration = duration;
  4158. }
  4159. } else {
  4160. this._mediaInfo.duration = 0;
  4161. }
  4162. if (typeof onMetaData.framerate === 'number') {
  4163. // framerate
  4164. var fps_num = Math.floor(onMetaData.framerate * 1000);
  4165. if (fps_num > 0) {
  4166. var fps = fps_num / 1000;
  4167. this._referenceFrameRate.fixed = true;
  4168. this._referenceFrameRate.fps = fps;
  4169. this._referenceFrameRate.fps_num = fps_num;
  4170. this._referenceFrameRate.fps_den = 1000;
  4171. this._mediaInfo.fps = fps;
  4172. }
  4173. }
  4174. if (_typeof(onMetaData.keyframes) === 'object') {
  4175. // keyframes
  4176. this._mediaInfo.hasKeyframesIndex = true;
  4177. var keyframes = onMetaData.keyframes;
  4178. this._mediaInfo.keyframesIndex = this._parseKeyframesIndex(keyframes);
  4179. onMetaData.keyframes = null; // keyframes has been extracted, remove it
  4180. } else {
  4181. this._mediaInfo.hasKeyframesIndex = false;
  4182. }
  4183. this._dispatch = false;
  4184. this._mediaInfo.metadata = onMetaData;
  4185. _logger2.default.v(this.TAG, 'Parsed onMetaData');
  4186. if (this._mediaInfo.isComplete()) {
  4187. this._onMediaInfo(this._mediaInfo);
  4188. }
  4189. }
  4190. }
  4191. }, {
  4192. key: '_parseKeyframesIndex',
  4193. value: function _parseKeyframesIndex(keyframes) {
  4194. var times = [];
  4195. var filepositions = [];
  4196. // ignore first keyframe which is actually AVC Sequence Header (AVCDecoderConfigurationRecord)
  4197. for (var i = 1; i < keyframes.times.length; i++) {
  4198. var time = this._timestampBase + Math.floor(keyframes.times[i] * 1000);
  4199. times.push(time);
  4200. filepositions.push(keyframes.filepositions[i]);
  4201. }
  4202. return {
  4203. times: times,
  4204. filepositions: filepositions
  4205. };
  4206. }
  4207. }, {
  4208. key: '_parseAudioData',
  4209. value: function _parseAudioData(arrayBuffer, dataOffset, dataSize, tagTimestamp) {
  4210. if (dataSize <= 1) {
  4211. _logger2.default.w(this.TAG, 'Flv: Invalid audio packet, missing SoundData payload!');
  4212. return;
  4213. }
  4214. if (this._hasAudioFlagOverrided === true && this._hasAudio === false) {
  4215. // If hasAudio: false indicated explicitly in MediaDataSource,
  4216. // Ignore all the audio packets
  4217. return;
  4218. }
  4219. var le = this._littleEndian;
  4220. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  4221. var soundSpec = v.getUint8(0);
  4222. var soundFormat = soundSpec >>> 4;
  4223. if (soundFormat !== 2 && soundFormat !== 10) {
  4224. // MP3 or AAC
  4225. this._onError(_demuxErrors2.default.CODEC_UNSUPPORTED, 'Flv: Unsupported audio codec idx: ' + soundFormat);
  4226. return;
  4227. }
  4228. var soundRate = 0;
  4229. var soundRateIndex = (soundSpec & 12) >>> 2;
  4230. if (soundRateIndex >= 0 && soundRateIndex <= 4) {
  4231. soundRate = this._flvSoundRateTable[soundRateIndex];
  4232. } else {
  4233. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Invalid audio sample rate idx: ' + soundRateIndex);
  4234. return;
  4235. }
  4236. var soundSize = (soundSpec & 2) >>> 1; // unused
  4237. var soundType = soundSpec & 1;
  4238. var meta = this._audioMetadata;
  4239. var track = this._audioTrack;
  4240. if (!meta) {
  4241. if (this._hasAudio === false && this._hasAudioFlagOverrided === false) {
  4242. this._hasAudio = true;
  4243. this._mediaInfo.hasAudio = true;
  4244. }
  4245. // initial metadata
  4246. meta = this._audioMetadata = {};
  4247. meta.type = 'audio';
  4248. meta.id = track.id;
  4249. meta.timescale = this._timescale;
  4250. meta.duration = this._duration;
  4251. meta.audioSampleRate = soundRate;
  4252. meta.channelCount = soundType === 0 ? 1 : 2;
  4253. }
  4254. if (soundFormat === 10) {
  4255. // AAC
  4256. var aacData = this._parseAACAudioData(arrayBuffer, dataOffset + 1, dataSize - 1);
  4257. if (aacData == undefined) {
  4258. return;
  4259. }
  4260. if (aacData.packetType === 0) {
  4261. // AAC sequence header (AudioSpecificConfig)
  4262. if (meta.config) {
  4263. _logger2.default.w(this.TAG, 'Found another AudioSpecificConfig!');
  4264. }
  4265. var misc = aacData.data;
  4266. meta.audioSampleRate = misc.samplingRate;
  4267. meta.channelCount = misc.channelCount;
  4268. meta.codec = misc.codec;
  4269. meta.originalCodec = misc.originalCodec;
  4270. meta.config = misc.config;
  4271. // The decode result of an aac sample is 1024 PCM samples
  4272. meta.refSampleDuration = 1024 / meta.audioSampleRate * meta.timescale;
  4273. _logger2.default.v(this.TAG, 'Parsed AudioSpecificConfig');
  4274. if (this._isInitialMetadataDispatched()) {
  4275. // Non-initial metadata, force dispatch (or flush) parsed frames to remuxer
  4276. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4277. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4278. }
  4279. } else {
  4280. this._audioInitialMetadataDispatched = true;
  4281. }
  4282. // then notify new metadata
  4283. this._dispatch = false;
  4284. this._onTrackMetadata('audio', meta);
  4285. var mi = this._mediaInfo;
  4286. mi.audioCodec = meta.originalCodec;
  4287. mi.audioSampleRate = meta.audioSampleRate;
  4288. mi.audioChannelCount = meta.channelCount;
  4289. if (mi.hasVideo) {
  4290. if (mi.videoCodec != null) {
  4291. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + ',' + mi.audioCodec + '"';
  4292. }
  4293. } else {
  4294. mi.mimeType = 'video/x-flv; codecs="' + mi.audioCodec + '"';
  4295. }
  4296. if (mi.isComplete()) {
  4297. this._onMediaInfo(mi);
  4298. }
  4299. } else if (aacData.packetType === 1) {
  4300. // AAC raw frame data
  4301. var dts = this._timestampBase + tagTimestamp;
  4302. var aacSample = { unit: aacData.data, length: aacData.data.byteLength, dts: dts, pts: dts };
  4303. track.samples.push(aacSample);
  4304. track.length += aacData.data.length;
  4305. } else {
  4306. _logger2.default.e(this.TAG, 'Flv: Unsupported AAC data type ' + aacData.packetType);
  4307. }
  4308. } else if (soundFormat === 2) {
  4309. // MP3
  4310. if (!meta.codec) {
  4311. // We need metadata for mp3 audio track, extract info from frame header
  4312. var _misc = this._parseMP3AudioData(arrayBuffer, dataOffset + 1, dataSize - 1, true);
  4313. if (_misc == undefined) {
  4314. return;
  4315. }
  4316. meta.audioSampleRate = _misc.samplingRate;
  4317. meta.channelCount = _misc.channelCount;
  4318. meta.codec = _misc.codec;
  4319. meta.originalCodec = _misc.originalCodec;
  4320. // The decode result of an mp3 sample is 1152 PCM samples
  4321. meta.refSampleDuration = 1152 / meta.audioSampleRate * meta.timescale;
  4322. _logger2.default.v(this.TAG, 'Parsed MPEG Audio Frame Header');
  4323. this._audioInitialMetadataDispatched = true;
  4324. this._onTrackMetadata('audio', meta);
  4325. var _mi = this._mediaInfo;
  4326. _mi.audioCodec = meta.codec;
  4327. _mi.audioSampleRate = meta.audioSampleRate;
  4328. _mi.audioChannelCount = meta.channelCount;
  4329. _mi.audioDataRate = _misc.bitRate;
  4330. if (_mi.hasVideo) {
  4331. if (_mi.videoCodec != null) {
  4332. _mi.mimeType = 'video/x-flv; codecs="' + _mi.videoCodec + ',' + _mi.audioCodec + '"';
  4333. }
  4334. } else {
  4335. _mi.mimeType = 'video/x-flv; codecs="' + _mi.audioCodec + '"';
  4336. }
  4337. if (_mi.isComplete()) {
  4338. this._onMediaInfo(_mi);
  4339. }
  4340. }
  4341. // This packet is always a valid audio packet, extract it
  4342. var data = this._parseMP3AudioData(arrayBuffer, dataOffset + 1, dataSize - 1, false);
  4343. if (data == undefined) {
  4344. return;
  4345. }
  4346. var _dts = this._timestampBase + tagTimestamp;
  4347. var mp3Sample = { unit: data, length: data.byteLength, dts: _dts, pts: _dts };
  4348. track.samples.push(mp3Sample);
  4349. track.length += data.length;
  4350. }
  4351. }
  4352. }, {
  4353. key: '_parseAACAudioData',
  4354. value: function _parseAACAudioData(arrayBuffer, dataOffset, dataSize) {
  4355. if (dataSize <= 1) {
  4356. _logger2.default.w(this.TAG, 'Flv: Invalid AAC packet, missing AACPacketType or/and Data!');
  4357. return;
  4358. }
  4359. var result = {};
  4360. var array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4361. result.packetType = array[0];
  4362. if (array[0] === 0) {
  4363. result.data = this._parseAACAudioSpecificConfig(arrayBuffer, dataOffset + 1, dataSize - 1);
  4364. } else {
  4365. result.data = array.subarray(1);
  4366. }
  4367. return result;
  4368. }
  4369. }, {
  4370. key: '_parseAACAudioSpecificConfig',
  4371. value: function _parseAACAudioSpecificConfig(arrayBuffer, dataOffset, dataSize) {
  4372. var array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4373. var config = null;
  4374. /* Audio Object Type:
  4375. 0: Null
  4376. 1: AAC Main
  4377. 2: AAC LC
  4378. 3: AAC SSR (Scalable Sample Rate)
  4379. 4: AAC LTP (Long Term Prediction)
  4380. 5: HE-AAC / SBR (Spectral Band Replication)
  4381. 6: AAC Scalable
  4382. */
  4383. var audioObjectType = 0;
  4384. var originalAudioObjectType = 0;
  4385. var audioExtensionObjectType = null;
  4386. var samplingIndex = 0;
  4387. var extensionSamplingIndex = null;
  4388. // 5 bits
  4389. audioObjectType = originalAudioObjectType = array[0] >>> 3;
  4390. // 4 bits
  4391. samplingIndex = (array[0] & 0x07) << 1 | array[1] >>> 7;
  4392. if (samplingIndex < 0 || samplingIndex >= this._mpegSamplingRates.length) {
  4393. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: AAC invalid sampling frequency index!');
  4394. return;
  4395. }
  4396. var samplingFrequence = this._mpegSamplingRates[samplingIndex];
  4397. // 4 bits
  4398. var channelConfig = (array[1] & 0x78) >>> 3;
  4399. if (channelConfig < 0 || channelConfig >= 8) {
  4400. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: AAC invalid channel configuration');
  4401. return;
  4402. }
  4403. if (audioObjectType === 5) {
  4404. // HE-AAC?
  4405. // 4 bits
  4406. extensionSamplingIndex = (array[1] & 0x07) << 1 | array[2] >>> 7;
  4407. // 5 bits
  4408. audioExtensionObjectType = (array[2] & 0x7C) >>> 2;
  4409. }
  4410. // workarounds for various browsers
  4411. var userAgent = self.navigator.userAgent.toLowerCase();
  4412. if (userAgent.indexOf('firefox') !== -1) {
  4413. // firefox: use SBR (HE-AAC) if freq less than 24kHz
  4414. if (samplingIndex >= 6) {
  4415. audioObjectType = 5;
  4416. config = new Array(4);
  4417. extensionSamplingIndex = samplingIndex - 3;
  4418. } else {
  4419. // use LC-AAC
  4420. audioObjectType = 2;
  4421. config = new Array(2);
  4422. extensionSamplingIndex = samplingIndex;
  4423. }
  4424. } else if (userAgent.indexOf('android') !== -1) {
  4425. // android: always use LC-AAC
  4426. audioObjectType = 2;
  4427. config = new Array(2);
  4428. extensionSamplingIndex = samplingIndex;
  4429. } else {
  4430. // for other browsers, e.g. chrome...
  4431. // Always use HE-AAC to make it easier to switch aac codec profile
  4432. audioObjectType = 5;
  4433. extensionSamplingIndex = samplingIndex;
  4434. config = new Array(4);
  4435. if (samplingIndex >= 6) {
  4436. extensionSamplingIndex = samplingIndex - 3;
  4437. } else if (channelConfig === 1) {
  4438. // Mono channel
  4439. audioObjectType = 2;
  4440. config = new Array(2);
  4441. extensionSamplingIndex = samplingIndex;
  4442. }
  4443. }
  4444. config[0] = audioObjectType << 3;
  4445. config[0] |= (samplingIndex & 0x0F) >>> 1;
  4446. config[1] = (samplingIndex & 0x0F) << 7;
  4447. config[1] |= (channelConfig & 0x0F) << 3;
  4448. if (audioObjectType === 5) {
  4449. config[1] |= (extensionSamplingIndex & 0x0F) >>> 1;
  4450. config[2] = (extensionSamplingIndex & 0x01) << 7;
  4451. // extended audio object type: force to 2 (LC-AAC)
  4452. config[2] |= 2 << 2;
  4453. config[3] = 0;
  4454. }
  4455. return {
  4456. config: config,
  4457. samplingRate: samplingFrequence,
  4458. channelCount: channelConfig,
  4459. codec: 'mp4a.40.' + audioObjectType,
  4460. originalCodec: 'mp4a.40.' + originalAudioObjectType
  4461. };
  4462. }
  4463. }, {
  4464. key: '_parseMP3AudioData',
  4465. value: function _parseMP3AudioData(arrayBuffer, dataOffset, dataSize, requestHeader) {
  4466. if (dataSize < 4) {
  4467. _logger2.default.w(this.TAG, 'Flv: Invalid MP3 packet, header missing!');
  4468. return;
  4469. }
  4470. var le = this._littleEndian;
  4471. var array = new Uint8Array(arrayBuffer, dataOffset, dataSize);
  4472. var result = null;
  4473. if (requestHeader) {
  4474. if (array[0] !== 0xFF) {
  4475. return;
  4476. }
  4477. var ver = array[1] >>> 3 & 0x03;
  4478. var layer = (array[1] & 0x06) >> 1;
  4479. var bitrate_index = (array[2] & 0xF0) >>> 4;
  4480. var sampling_freq_index = (array[2] & 0x0C) >>> 2;
  4481. var channel_mode = array[3] >>> 6 & 0x03;
  4482. var channel_count = channel_mode !== 3 ? 2 : 1;
  4483. var sample_rate = 0;
  4484. var bit_rate = 0;
  4485. var object_type = 34; // Layer-3, listed in MPEG-4 Audio Object Types
  4486. var codec = 'mp3';
  4487. switch (ver) {
  4488. case 0:
  4489. // MPEG 2.5
  4490. sample_rate = this._mpegAudioV25SampleRateTable[sampling_freq_index];
  4491. break;
  4492. case 2:
  4493. // MPEG 2
  4494. sample_rate = this._mpegAudioV20SampleRateTable[sampling_freq_index];
  4495. break;
  4496. case 3:
  4497. // MPEG 1
  4498. sample_rate = this._mpegAudioV10SampleRateTable[sampling_freq_index];
  4499. break;
  4500. }
  4501. switch (layer) {
  4502. case 1:
  4503. // Layer 3
  4504. object_type = 34;
  4505. if (bitrate_index < this._mpegAudioL3BitRateTable.length) {
  4506. bit_rate = this._mpegAudioL3BitRateTable[bitrate_index];
  4507. }
  4508. break;
  4509. case 2:
  4510. // Layer 2
  4511. object_type = 33;
  4512. if (bitrate_index < this._mpegAudioL2BitRateTable.length) {
  4513. bit_rate = this._mpegAudioL2BitRateTable[bitrate_index];
  4514. }
  4515. break;
  4516. case 3:
  4517. // Layer 1
  4518. object_type = 32;
  4519. if (bitrate_index < this._mpegAudioL1BitRateTable.length) {
  4520. bit_rate = this._mpegAudioL1BitRateTable[bitrate_index];
  4521. }
  4522. break;
  4523. }
  4524. result = {
  4525. bitRate: bit_rate,
  4526. samplingRate: sample_rate,
  4527. channelCount: channel_count,
  4528. codec: codec,
  4529. originalCodec: codec
  4530. };
  4531. } else {
  4532. result = array;
  4533. }
  4534. return result;
  4535. }
  4536. }, {
  4537. key: '_parseVideoData',
  4538. value: function _parseVideoData(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition) {
  4539. if (dataSize <= 1) {
  4540. _logger2.default.w(this.TAG, 'Flv: Invalid video packet, missing VideoData payload!');
  4541. return;
  4542. }
  4543. if (this._hasVideoFlagOverrided === true && this._hasVideo === false) {
  4544. // If hasVideo: false indicated explicitly in MediaDataSource,
  4545. // Ignore all the video packets
  4546. return;
  4547. }
  4548. var spec = new Uint8Array(arrayBuffer, dataOffset, dataSize)[0];
  4549. var frameType = (spec & 240) >>> 4;
  4550. var codecId = spec & 15;
  4551. if (codecId !== 7) {
  4552. this._onError(_demuxErrors2.default.CODEC_UNSUPPORTED, 'Flv: Unsupported codec in video frame: ' + codecId);
  4553. return;
  4554. }
  4555. this._parseAVCVideoPacket(arrayBuffer, dataOffset + 1, dataSize - 1, tagTimestamp, tagPosition, frameType);
  4556. }
  4557. }, {
  4558. key: '_parseAVCVideoPacket',
  4559. value: function _parseAVCVideoPacket(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition, frameType) {
  4560. if (dataSize < 4) {
  4561. _logger2.default.w(this.TAG, 'Flv: Invalid AVC packet, missing AVCPacketType or/and CompositionTime');
  4562. return;
  4563. }
  4564. var le = this._littleEndian;
  4565. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  4566. var packetType = v.getUint8(0);
  4567. var cts_unsigned = v.getUint32(0, !le) & 0x00FFFFFF;
  4568. var cts = cts_unsigned << 8 >> 8; // convert to 24-bit signed int
  4569. if (packetType === 0) {
  4570. // AVCDecoderConfigurationRecord
  4571. this._parseAVCDecoderConfigurationRecord(arrayBuffer, dataOffset + 4, dataSize - 4);
  4572. } else if (packetType === 1) {
  4573. // One or more Nalus
  4574. this._parseAVCVideoData(arrayBuffer, dataOffset + 4, dataSize - 4, tagTimestamp, tagPosition, frameType, cts);
  4575. } else if (packetType === 2) {
  4576. // empty, AVC end of sequence
  4577. } else {
  4578. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Invalid video packet type ' + packetType);
  4579. return;
  4580. }
  4581. }
  4582. }, {
  4583. key: '_parseAVCDecoderConfigurationRecord',
  4584. value: function _parseAVCDecoderConfigurationRecord(arrayBuffer, dataOffset, dataSize) {
  4585. if (dataSize < 7) {
  4586. _logger2.default.w(this.TAG, 'Flv: Invalid AVCDecoderConfigurationRecord, lack of data!');
  4587. return;
  4588. }
  4589. var meta = this._videoMetadata;
  4590. var track = this._videoTrack;
  4591. var le = this._littleEndian;
  4592. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  4593. if (!meta) {
  4594. if (this._hasVideo === false && this._hasVideoFlagOverrided === false) {
  4595. this._hasVideo = true;
  4596. this._mediaInfo.hasVideo = true;
  4597. }
  4598. meta = this._videoMetadata = {};
  4599. meta.type = 'video';
  4600. meta.id = track.id;
  4601. meta.timescale = this._timescale;
  4602. meta.duration = this._duration;
  4603. } else {
  4604. if (typeof meta.avcc !== 'undefined') {
  4605. _logger2.default.w(this.TAG, 'Found another AVCDecoderConfigurationRecord!');
  4606. }
  4607. }
  4608. var version = v.getUint8(0); // configurationVersion
  4609. var avcProfile = v.getUint8(1); // avcProfileIndication
  4610. var profileCompatibility = v.getUint8(2); // profile_compatibility
  4611. var avcLevel = v.getUint8(3); // AVCLevelIndication
  4612. if (version !== 1 || avcProfile === 0) {
  4613. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord');
  4614. return;
  4615. }
  4616. this._naluLengthSize = (v.getUint8(4) & 3) + 1; // lengthSizeMinusOne
  4617. if (this._naluLengthSize !== 3 && this._naluLengthSize !== 4) {
  4618. // holy shit!!!
  4619. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Strange NaluLengthSizeMinusOne: ' + (this._naluLengthSize - 1));
  4620. return;
  4621. }
  4622. var spsCount = v.getUint8(5) & 31; // numOfSequenceParameterSets
  4623. if (spsCount === 0) {
  4624. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No SPS');
  4625. return;
  4626. } else if (spsCount > 1) {
  4627. _logger2.default.w(this.TAG, 'Flv: Strange AVCDecoderConfigurationRecord: SPS Count = ' + spsCount);
  4628. }
  4629. var offset = 6;
  4630. for (var i = 0; i < spsCount; i++) {
  4631. var len = v.getUint16(offset, !le); // sequenceParameterSetLength
  4632. offset += 2;
  4633. if (len === 0) {
  4634. continue;
  4635. }
  4636. // Notice: Nalu without startcode header (00 00 00 01)
  4637. var sps = new Uint8Array(arrayBuffer, dataOffset + offset, len);
  4638. offset += len;
  4639. var config = _spsParser2.default.parseSPS(sps);
  4640. if (i !== 0) {
  4641. // ignore other sps's config
  4642. continue;
  4643. }
  4644. meta.codecWidth = config.codec_size.width;
  4645. meta.codecHeight = config.codec_size.height;
  4646. meta.presentWidth = config.present_size.width;
  4647. meta.presentHeight = config.present_size.height;
  4648. meta.profile = config.profile_string;
  4649. meta.level = config.level_string;
  4650. meta.bitDepth = config.bit_depth;
  4651. meta.chromaFormat = config.chroma_format;
  4652. meta.sarRatio = config.sar_ratio;
  4653. meta.frameRate = config.frame_rate;
  4654. if (config.frame_rate.fixed === false || config.frame_rate.fps_num === 0 || config.frame_rate.fps_den === 0) {
  4655. meta.frameRate = this._referenceFrameRate;
  4656. }
  4657. var fps_den = meta.frameRate.fps_den;
  4658. var fps_num = meta.frameRate.fps_num;
  4659. meta.refSampleDuration = meta.timescale * (fps_den / fps_num);
  4660. var codecArray = sps.subarray(1, 4);
  4661. var codecString = 'avc1.';
  4662. for (var j = 0; j < 3; j++) {
  4663. var h = codecArray[j].toString(16);
  4664. if (h.length < 2) {
  4665. h = '0' + h;
  4666. }
  4667. codecString += h;
  4668. }
  4669. meta.codec = codecString;
  4670. var mi = this._mediaInfo;
  4671. mi.width = meta.codecWidth;
  4672. mi.height = meta.codecHeight;
  4673. mi.fps = meta.frameRate.fps;
  4674. mi.profile = meta.profile;
  4675. mi.level = meta.level;
  4676. mi.refFrames = config.ref_frames;
  4677. mi.chromaFormat = config.chroma_format_string;
  4678. mi.sarNum = meta.sarRatio.width;
  4679. mi.sarDen = meta.sarRatio.height;
  4680. mi.videoCodec = codecString;
  4681. if (mi.hasAudio) {
  4682. if (mi.audioCodec != null) {
  4683. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + ',' + mi.audioCodec + '"';
  4684. }
  4685. } else {
  4686. mi.mimeType = 'video/x-flv; codecs="' + mi.videoCodec + '"';
  4687. }
  4688. if (mi.isComplete()) {
  4689. this._onMediaInfo(mi);
  4690. }
  4691. }
  4692. var ppsCount = v.getUint8(offset); // numOfPictureParameterSets
  4693. if (ppsCount === 0) {
  4694. this._onError(_demuxErrors2.default.FORMAT_ERROR, 'Flv: Invalid AVCDecoderConfigurationRecord: No PPS');
  4695. return;
  4696. } else if (ppsCount > 1) {
  4697. _logger2.default.w(this.TAG, 'Flv: Strange AVCDecoderConfigurationRecord: PPS Count = ' + ppsCount);
  4698. }
  4699. offset++;
  4700. for (var _i = 0; _i < ppsCount; _i++) {
  4701. var _len = v.getUint16(offset, !le); // pictureParameterSetLength
  4702. offset += 2;
  4703. if (_len === 0) {
  4704. continue;
  4705. }
  4706. // pps is useless for extracting video information
  4707. offset += _len;
  4708. }
  4709. meta.avcc = new Uint8Array(dataSize);
  4710. meta.avcc.set(new Uint8Array(arrayBuffer, dataOffset, dataSize), 0);
  4711. _logger2.default.v(this.TAG, 'Parsed AVCDecoderConfigurationRecord');
  4712. if (this._isInitialMetadataDispatched()) {
  4713. // flush parsed frames
  4714. if (this._dispatch && (this._audioTrack.length || this._videoTrack.length)) {
  4715. this._onDataAvailable(this._audioTrack, this._videoTrack);
  4716. }
  4717. } else {
  4718. this._videoInitialMetadataDispatched = true;
  4719. }
  4720. // notify new metadata
  4721. this._dispatch = false;
  4722. this._onTrackMetadata('video', meta);
  4723. }
  4724. }, {
  4725. key: '_parseAVCVideoData',
  4726. value: function _parseAVCVideoData(arrayBuffer, dataOffset, dataSize, tagTimestamp, tagPosition, frameType, cts) {
  4727. var le = this._littleEndian;
  4728. var v = new DataView(arrayBuffer, dataOffset, dataSize);
  4729. var units = [],
  4730. length = 0;
  4731. var offset = 0;
  4732. var lengthSize = this._naluLengthSize;
  4733. var dts = this._timestampBase + tagTimestamp;
  4734. var keyframe = frameType === 1; // from FLV Frame Type constants
  4735. while (offset < dataSize) {
  4736. if (offset + 4 >= dataSize) {
  4737. _logger2.default.w(this.TAG, 'Malformed Nalu near timestamp ' + dts + ', offset = ' + offset + ', dataSize = ' + dataSize);
  4738. break; // data not enough for next Nalu
  4739. }
  4740. // Nalu with length-header (AVC1)
  4741. var naluSize = v.getUint32(offset, !le); // Big-Endian read
  4742. if (lengthSize === 3) {
  4743. naluSize >>>= 8;
  4744. }
  4745. if (naluSize > dataSize - lengthSize) {
  4746. _logger2.default.w(this.TAG, 'Malformed Nalus near timestamp ' + dts + ', NaluSize > DataSize!');
  4747. return;
  4748. }
  4749. var unitType = v.getUint8(offset + lengthSize) & 0x1F;
  4750. if (unitType === 5) {
  4751. // IDR
  4752. keyframe = true;
  4753. }
  4754. var data = new Uint8Array(arrayBuffer, dataOffset + offset, lengthSize + naluSize);
  4755. var unit = { type: unitType, data: data };
  4756. units.push(unit);
  4757. length += data.byteLength;
  4758. offset += lengthSize + naluSize;
  4759. }
  4760. if (units.length) {
  4761. var track = this._videoTrack;
  4762. var avcSample = {
  4763. units: units,
  4764. length: length,
  4765. isKeyframe: keyframe,
  4766. dts: dts,
  4767. cts: cts,
  4768. pts: dts + cts
  4769. };
  4770. if (keyframe) {
  4771. avcSample.fileposition = tagPosition;
  4772. }
  4773. track.samples.push(avcSample);
  4774. track.length += length;
  4775. }
  4776. }
  4777. }, {
  4778. key: 'onTrackMetadata',
  4779. get: function get() {
  4780. return this._onTrackMetadata;
  4781. },
  4782. set: function set(callback) {
  4783. this._onTrackMetadata = callback;
  4784. }
  4785. // prototype: function(mediaInfo: MediaInfo): void
  4786. }, {
  4787. key: 'onMediaInfo',
  4788. get: function get() {
  4789. return this._onMediaInfo;
  4790. },
  4791. set: function set(callback) {
  4792. this._onMediaInfo = callback;
  4793. }
  4794. }, {
  4795. key: 'onMetaDataArrived',
  4796. get: function get() {
  4797. return this._onMetaDataArrived;
  4798. },
  4799. set: function set(callback) {
  4800. this._onMetaDataArrived = callback;
  4801. }
  4802. // prototype: function(type: number, info: string): void
  4803. }, {
  4804. key: 'onError',
  4805. get: function get() {
  4806. return this._onError;
  4807. },
  4808. set: function set(callback) {
  4809. this._onError = callback;
  4810. }
  4811. // prototype: function(videoTrack: any, audioTrack: any): void
  4812. }, {
  4813. key: 'onDataAvailable',
  4814. get: function get() {
  4815. return this._onDataAvailable;
  4816. },
  4817. set: function set(callback) {
  4818. this._onDataAvailable = callback;
  4819. }
  4820. // timestamp base for output samples, must be in milliseconds
  4821. }, {
  4822. key: 'timestampBase',
  4823. get: function get() {
  4824. return this._timestampBase;
  4825. },
  4826. set: function set(base) {
  4827. this._timestampBase = base;
  4828. }
  4829. }, {
  4830. key: 'overridedDuration',
  4831. get: function get() {
  4832. return this._duration;
  4833. }
  4834. // Force-override media duration. Must be in milliseconds, int32
  4835. ,
  4836. set: function set(duration) {
  4837. this._durationOverrided = true;
  4838. this._duration = duration;
  4839. this._mediaInfo.duration = duration;
  4840. }
  4841. // Force-override audio track present flag, boolean
  4842. }, {
  4843. key: 'overridedHasAudio',
  4844. set: function set(hasAudio) {
  4845. this._hasAudioFlagOverrided = true;
  4846. this._hasAudio = hasAudio;
  4847. this._mediaInfo.hasAudio = hasAudio;
  4848. }
  4849. // Force-override video track present flag, boolean
  4850. }, {
  4851. key: 'overridedHasVideo',
  4852. set: function set(hasVideo) {
  4853. this._hasVideoFlagOverrided = true;
  4854. this._hasVideo = hasVideo;
  4855. this._mediaInfo.hasVideo = hasVideo;
  4856. }
  4857. }], [{
  4858. key: 'probe',
  4859. value: function probe(buffer) {
  4860. var data = new Uint8Array(buffer);
  4861. var mismatch = { match: false };
  4862. if (data[0] !== 0x46 || data[1] !== 0x4C || data[2] !== 0x56 || data[3] !== 0x01) {
  4863. return mismatch;
  4864. }
  4865. var hasAudio = (data[4] & 4) >>> 2 !== 0;
  4866. var hasVideo = (data[4] & 1) !== 0;
  4867. var offset = ReadBig32(data, 5);
  4868. if (offset < 9) {
  4869. return mismatch;
  4870. }
  4871. return {
  4872. match: true,
  4873. consumed: offset,
  4874. dataOffset: offset,
  4875. hasAudioTrack: hasAudio,
  4876. hasVideoTrack: hasVideo
  4877. };
  4878. }
  4879. }]);
  4880. return FLVDemuxer;
  4881. }();
  4882. exports.default = FLVDemuxer;
  4883. },{"../core/media-info.js":7,"../utils/exception.js":40,"../utils/logger.js":41,"./amf-parser.js":15,"./demux-errors.js":16,"./sps-parser.js":19}],19:[function(_dereq_,module,exports){
  4884. 'use strict';
  4885. Object.defineProperty(exports, "__esModule", {
  4886. value: true
  4887. });
  4888. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  4889. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  4890. *
  4891. * @author zheng qian <xqq@xqq.im>
  4892. *
  4893. * Licensed under the Apache License, Version 2.0 (the "License");
  4894. * you may not use this file except in compliance with the License.
  4895. * You may obtain a copy of the License at
  4896. *
  4897. * http://www.apache.org/licenses/LICENSE-2.0
  4898. *
  4899. * Unless required by applicable law or agreed to in writing, software
  4900. * distributed under the License is distributed on an "AS IS" BASIS,
  4901. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  4902. * See the License for the specific language governing permissions and
  4903. * limitations under the License.
  4904. */
  4905. var _expGolomb = _dereq_('./exp-golomb.js');
  4906. var _expGolomb2 = _interopRequireDefault(_expGolomb);
  4907. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  4908. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4909. var SPSParser = function () {
  4910. function SPSParser() {
  4911. _classCallCheck(this, SPSParser);
  4912. }
  4913. _createClass(SPSParser, null, [{
  4914. key: '_ebsp2rbsp',
  4915. value: function _ebsp2rbsp(uint8array) {
  4916. var src = uint8array;
  4917. var src_length = src.byteLength;
  4918. var dst = new Uint8Array(src_length);
  4919. var dst_idx = 0;
  4920. for (var i = 0; i < src_length; i++) {
  4921. if (i >= 2) {
  4922. // Unescape: Skip 0x03 after 00 00
  4923. if (src[i] === 0x03 && src[i - 1] === 0x00 && src[i - 2] === 0x00) {
  4924. continue;
  4925. }
  4926. }
  4927. dst[dst_idx] = src[i];
  4928. dst_idx++;
  4929. }
  4930. return new Uint8Array(dst.buffer, 0, dst_idx);
  4931. }
  4932. }, {
  4933. key: 'parseSPS',
  4934. value: function parseSPS(uint8array) {
  4935. var rbsp = SPSParser._ebsp2rbsp(uint8array);
  4936. var gb = new _expGolomb2.default(rbsp);
  4937. gb.readByte();
  4938. var profile_idc = gb.readByte(); // profile_idc
  4939. gb.readByte(); // constraint_set_flags[5] + reserved_zero[3]
  4940. var level_idc = gb.readByte(); // level_idc
  4941. gb.readUEG(); // seq_parameter_set_id
  4942. var profile_string = SPSParser.getProfileString(profile_idc);
  4943. var level_string = SPSParser.getLevelString(level_idc);
  4944. var chroma_format_idc = 1;
  4945. var chroma_format = 420;
  4946. var chroma_format_table = [0, 420, 422, 444];
  4947. var bit_depth = 8;
  4948. if (profile_idc === 100 || profile_idc === 110 || profile_idc === 122 || profile_idc === 244 || profile_idc === 44 || profile_idc === 83 || profile_idc === 86 || profile_idc === 118 || profile_idc === 128 || profile_idc === 138 || profile_idc === 144) {
  4949. chroma_format_idc = gb.readUEG();
  4950. if (chroma_format_idc === 3) {
  4951. gb.readBits(1); // separate_colour_plane_flag
  4952. }
  4953. if (chroma_format_idc <= 3) {
  4954. chroma_format = chroma_format_table[chroma_format_idc];
  4955. }
  4956. bit_depth = gb.readUEG() + 8; // bit_depth_luma_minus8
  4957. gb.readUEG(); // bit_depth_chroma_minus8
  4958. gb.readBits(1); // qpprime_y_zero_transform_bypass_flag
  4959. if (gb.readBool()) {
  4960. // seq_scaling_matrix_present_flag
  4961. var scaling_list_count = chroma_format_idc !== 3 ? 8 : 12;
  4962. for (var i = 0; i < scaling_list_count; i++) {
  4963. if (gb.readBool()) {
  4964. // seq_scaling_list_present_flag
  4965. if (i < 6) {
  4966. SPSParser._skipScalingList(gb, 16);
  4967. } else {
  4968. SPSParser._skipScalingList(gb, 64);
  4969. }
  4970. }
  4971. }
  4972. }
  4973. }
  4974. gb.readUEG(); // log2_max_frame_num_minus4
  4975. var pic_order_cnt_type = gb.readUEG();
  4976. if (pic_order_cnt_type === 0) {
  4977. gb.readUEG(); // log2_max_pic_order_cnt_lsb_minus_4
  4978. } else if (pic_order_cnt_type === 1) {
  4979. gb.readBits(1); // delta_pic_order_always_zero_flag
  4980. gb.readSEG(); // offset_for_non_ref_pic
  4981. gb.readSEG(); // offset_for_top_to_bottom_field
  4982. var num_ref_frames_in_pic_order_cnt_cycle = gb.readUEG();
  4983. for (var _i = 0; _i < num_ref_frames_in_pic_order_cnt_cycle; _i++) {
  4984. gb.readSEG(); // offset_for_ref_frame
  4985. }
  4986. }
  4987. var ref_frames = gb.readUEG(); // max_num_ref_frames
  4988. gb.readBits(1); // gaps_in_frame_num_value_allowed_flag
  4989. var pic_width_in_mbs_minus1 = gb.readUEG();
  4990. var pic_height_in_map_units_minus1 = gb.readUEG();
  4991. var frame_mbs_only_flag = gb.readBits(1);
  4992. if (frame_mbs_only_flag === 0) {
  4993. gb.readBits(1); // mb_adaptive_frame_field_flag
  4994. }
  4995. gb.readBits(1); // direct_8x8_inference_flag
  4996. var frame_crop_left_offset = 0;
  4997. var frame_crop_right_offset = 0;
  4998. var frame_crop_top_offset = 0;
  4999. var frame_crop_bottom_offset = 0;
  5000. var frame_cropping_flag = gb.readBool();
  5001. if (frame_cropping_flag) {
  5002. frame_crop_left_offset = gb.readUEG();
  5003. frame_crop_right_offset = gb.readUEG();
  5004. frame_crop_top_offset = gb.readUEG();
  5005. frame_crop_bottom_offset = gb.readUEG();
  5006. }
  5007. var sar_width = 1,
  5008. sar_height = 1;
  5009. var fps = 0,
  5010. fps_fixed = true,
  5011. fps_num = 0,
  5012. fps_den = 0;
  5013. var vui_parameters_present_flag = gb.readBool();
  5014. if (vui_parameters_present_flag) {
  5015. if (gb.readBool()) {
  5016. // aspect_ratio_info_present_flag
  5017. var aspect_ratio_idc = gb.readByte();
  5018. var sar_w_table = [1, 12, 10, 16, 40, 24, 20, 32, 80, 18, 15, 64, 160, 4, 3, 2];
  5019. var sar_h_table = [1, 11, 11, 11, 33, 11, 11, 11, 33, 11, 11, 33, 99, 3, 2, 1];
  5020. if (aspect_ratio_idc > 0 && aspect_ratio_idc < 16) {
  5021. sar_width = sar_w_table[aspect_ratio_idc - 1];
  5022. sar_height = sar_h_table[aspect_ratio_idc - 1];
  5023. } else if (aspect_ratio_idc === 255) {
  5024. sar_width = gb.readByte() << 8 | gb.readByte();
  5025. sar_height = gb.readByte() << 8 | gb.readByte();
  5026. }
  5027. }
  5028. if (gb.readBool()) {
  5029. // overscan_info_present_flag
  5030. gb.readBool(); // overscan_appropriate_flag
  5031. }
  5032. if (gb.readBool()) {
  5033. // video_signal_type_present_flag
  5034. gb.readBits(4); // video_format & video_full_range_flag
  5035. if (gb.readBool()) {
  5036. // colour_description_present_flag
  5037. gb.readBits(24); // colour_primaries & transfer_characteristics & matrix_coefficients
  5038. }
  5039. }
  5040. if (gb.readBool()) {
  5041. // chroma_loc_info_present_flag
  5042. gb.readUEG(); // chroma_sample_loc_type_top_field
  5043. gb.readUEG(); // chroma_sample_loc_type_bottom_field
  5044. }
  5045. if (gb.readBool()) {
  5046. // timing_info_present_flag
  5047. var num_units_in_tick = gb.readBits(32);
  5048. var time_scale = gb.readBits(32);
  5049. fps_fixed = gb.readBool(); // fixed_frame_rate_flag
  5050. fps_num = time_scale;
  5051. fps_den = num_units_in_tick * 2;
  5052. fps = fps_num / fps_den;
  5053. }
  5054. }
  5055. var sarScale = 1;
  5056. if (sar_width !== 1 || sar_height !== 1) {
  5057. sarScale = sar_width / sar_height;
  5058. }
  5059. var crop_unit_x = 0,
  5060. crop_unit_y = 0;
  5061. if (chroma_format_idc === 0) {
  5062. crop_unit_x = 1;
  5063. crop_unit_y = 2 - frame_mbs_only_flag;
  5064. } else {
  5065. var sub_wc = chroma_format_idc === 3 ? 1 : 2;
  5066. var sub_hc = chroma_format_idc === 1 ? 2 : 1;
  5067. crop_unit_x = sub_wc;
  5068. crop_unit_y = sub_hc * (2 - frame_mbs_only_flag);
  5069. }
  5070. var codec_width = (pic_width_in_mbs_minus1 + 1) * 16;
  5071. var codec_height = (2 - frame_mbs_only_flag) * ((pic_height_in_map_units_minus1 + 1) * 16);
  5072. codec_width -= (frame_crop_left_offset + frame_crop_right_offset) * crop_unit_x;
  5073. codec_height -= (frame_crop_top_offset + frame_crop_bottom_offset) * crop_unit_y;
  5074. var present_width = Math.ceil(codec_width * sarScale);
  5075. gb.destroy();
  5076. gb = null;
  5077. return {
  5078. profile_string: profile_string, // baseline, high, high10, ...
  5079. level_string: level_string, // 3, 3.1, 4, 4.1, 5, 5.1, ...
  5080. bit_depth: bit_depth, // 8bit, 10bit, ...
  5081. ref_frames: ref_frames,
  5082. chroma_format: chroma_format, // 4:2:0, 4:2:2, ...
  5083. chroma_format_string: SPSParser.getChromaFormatString(chroma_format),
  5084. frame_rate: {
  5085. fixed: fps_fixed,
  5086. fps: fps,
  5087. fps_den: fps_den,
  5088. fps_num: fps_num
  5089. },
  5090. sar_ratio: {
  5091. width: sar_width,
  5092. height: sar_height
  5093. },
  5094. codec_size: {
  5095. width: codec_width,
  5096. height: codec_height
  5097. },
  5098. present_size: {
  5099. width: present_width,
  5100. height: codec_height
  5101. }
  5102. };
  5103. }
  5104. }, {
  5105. key: '_skipScalingList',
  5106. value: function _skipScalingList(gb, count) {
  5107. var last_scale = 8,
  5108. next_scale = 8;
  5109. var delta_scale = 0;
  5110. for (var i = 0; i < count; i++) {
  5111. if (next_scale !== 0) {
  5112. delta_scale = gb.readSEG();
  5113. next_scale = (last_scale + delta_scale + 256) % 256;
  5114. }
  5115. last_scale = next_scale === 0 ? last_scale : next_scale;
  5116. }
  5117. }
  5118. }, {
  5119. key: 'getProfileString',
  5120. value: function getProfileString(profile_idc) {
  5121. switch (profile_idc) {
  5122. case 66:
  5123. return 'Baseline';
  5124. case 77:
  5125. return 'Main';
  5126. case 88:
  5127. return 'Extended';
  5128. case 100:
  5129. return 'High';
  5130. case 110:
  5131. return 'High10';
  5132. case 122:
  5133. return 'High422';
  5134. case 244:
  5135. return 'High444';
  5136. default:
  5137. return 'Unknown';
  5138. }
  5139. }
  5140. }, {
  5141. key: 'getLevelString',
  5142. value: function getLevelString(level_idc) {
  5143. return (level_idc / 10).toFixed(1);
  5144. }
  5145. }, {
  5146. key: 'getChromaFormatString',
  5147. value: function getChromaFormatString(chroma) {
  5148. switch (chroma) {
  5149. case 420:
  5150. return '4:2:0';
  5151. case 422:
  5152. return '4:2:2';
  5153. case 444:
  5154. return '4:4:4';
  5155. default:
  5156. return 'Unknown';
  5157. }
  5158. }
  5159. }]);
  5160. return SPSParser;
  5161. }();
  5162. exports.default = SPSParser;
  5163. },{"./exp-golomb.js":17}],20:[function(_dereq_,module,exports){
  5164. 'use strict';
  5165. Object.defineProperty(exports, "__esModule", {
  5166. value: true
  5167. });
  5168. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /*
  5169. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  5170. *
  5171. * @author zheng qian <xqq@xqq.im>
  5172. *
  5173. * Licensed under the Apache License, Version 2.0 (the "License");
  5174. * you may not use this file except in compliance with the License.
  5175. * You may obtain a copy of the License at
  5176. *
  5177. * http://www.apache.org/licenses/LICENSE-2.0
  5178. *
  5179. * Unless required by applicable law or agreed to in writing, software
  5180. * distributed under the License is distributed on an "AS IS" BASIS,
  5181. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5182. * See the License for the specific language governing permissions and
  5183. * limitations under the License.
  5184. */
  5185. var _polyfill = _dereq_('./utils/polyfill.js');
  5186. var _polyfill2 = _interopRequireDefault(_polyfill);
  5187. var _features = _dereq_('./core/features.js');
  5188. var _features2 = _interopRequireDefault(_features);
  5189. var _loader = _dereq_('./io/loader.js');
  5190. var _flvPlayer = _dereq_('./player/flv-player.js');
  5191. var _flvPlayer2 = _interopRequireDefault(_flvPlayer);
  5192. var _nativePlayer = _dereq_('./player/native-player.js');
  5193. var _nativePlayer2 = _interopRequireDefault(_nativePlayer);
  5194. var _playerEvents = _dereq_('./player/player-events.js');
  5195. var _playerEvents2 = _interopRequireDefault(_playerEvents);
  5196. var _playerErrors = _dereq_('./player/player-errors.js');
  5197. var _loggingControl = _dereq_('./utils/logging-control.js');
  5198. var _loggingControl2 = _interopRequireDefault(_loggingControl);
  5199. var _exception = _dereq_('./utils/exception.js');
  5200. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5201. // here are all the interfaces
  5202. // install polyfills
  5203. _polyfill2.default.install();
  5204. // factory method
  5205. function createPlayer(mediaDataSource, optionalConfig) {
  5206. var mds = mediaDataSource;
  5207. if (mds == null || (typeof mds === 'undefined' ? 'undefined' : _typeof(mds)) !== 'object') {
  5208. throw new _exception.InvalidArgumentException('MediaDataSource must be an javascript object!');
  5209. }
  5210. if (!mds.hasOwnProperty('type')) {
  5211. throw new _exception.InvalidArgumentException('MediaDataSource must has type field to indicate video file type!');
  5212. }
  5213. switch (mds.type) {
  5214. case 'flv':
  5215. return new _flvPlayer2.default(mds, optionalConfig);
  5216. default:
  5217. return new _nativePlayer2.default(mds, optionalConfig);
  5218. }
  5219. }
  5220. // feature detection
  5221. function isSupported() {
  5222. return _features2.default.supportMSEH264Playback();
  5223. }
  5224. function getFeatureList() {
  5225. return _features2.default.getFeatureList();
  5226. }
  5227. // interfaces
  5228. var flvjs = {};
  5229. flvjs.createPlayer = createPlayer;
  5230. flvjs.isSupported = isSupported;
  5231. flvjs.getFeatureList = getFeatureList;
  5232. flvjs.BaseLoader = _loader.BaseLoader;
  5233. flvjs.LoaderStatus = _loader.LoaderStatus;
  5234. flvjs.LoaderErrors = _loader.LoaderErrors;
  5235. flvjs.Events = _playerEvents2.default;
  5236. flvjs.ErrorTypes = _playerErrors.ErrorTypes;
  5237. flvjs.ErrorDetails = _playerErrors.ErrorDetails;
  5238. flvjs.FlvPlayer = _flvPlayer2.default;
  5239. flvjs.NativePlayer = _nativePlayer2.default;
  5240. flvjs.LoggingControl = _loggingControl2.default;
  5241. Object.defineProperty(flvjs, 'version', {
  5242. enumerable: true,
  5243. get: function get() {
  5244. // replaced by browserify-versionify transform
  5245. return '1.4.2';
  5246. }
  5247. });
  5248. exports.default = flvjs;
  5249. },{"./core/features.js":6,"./io/loader.js":24,"./player/flv-player.js":32,"./player/native-player.js":33,"./player/player-errors.js":34,"./player/player-events.js":35,"./utils/exception.js":40,"./utils/logging-control.js":42,"./utils/polyfill.js":43}],21:[function(_dereq_,module,exports){
  5250. 'use strict';
  5251. // entry/index file
  5252. // make it compatible with browserify's umd wrapper
  5253. module.exports = _dereq_('./flv.js').default;
  5254. },{"./flv.js":20}],22:[function(_dereq_,module,exports){
  5255. 'use strict';
  5256. Object.defineProperty(exports, "__esModule", {
  5257. value: true
  5258. });
  5259. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  5260. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  5261. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  5262. var _logger = _dereq_('../utils/logger.js');
  5263. var _logger2 = _interopRequireDefault(_logger);
  5264. var _browser = _dereq_('../utils/browser.js');
  5265. var _browser2 = _interopRequireDefault(_browser);
  5266. var _loader = _dereq_('./loader.js');
  5267. var _exception = _dereq_('../utils/exception.js');
  5268. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5269. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5270. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5271. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
  5272. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  5273. *
  5274. * @author zheng qian <xqq@xqq.im>
  5275. *
  5276. * Licensed under the Apache License, Version 2.0 (the "License");
  5277. * you may not use this file except in compliance with the License.
  5278. * You may obtain a copy of the License at
  5279. *
  5280. * http://www.apache.org/licenses/LICENSE-2.0
  5281. *
  5282. * Unless required by applicable law or agreed to in writing, software
  5283. * distributed under the License is distributed on an "AS IS" BASIS,
  5284. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5285. * See the License for the specific language governing permissions and
  5286. * limitations under the License.
  5287. */
  5288. /* fetch + stream IO loader. Currently working on chrome 43+.
  5289. * fetch provides a better alternative http API to XMLHttpRequest
  5290. *
  5291. * fetch spec https://fetch.spec.whatwg.org/
  5292. * stream spec https://streams.spec.whatwg.org/
  5293. */
  5294. var FetchStreamLoader = function (_BaseLoader) {
  5295. _inherits(FetchStreamLoader, _BaseLoader);
  5296. _createClass(FetchStreamLoader, null, [{
  5297. key: 'isSupported',
  5298. value: function isSupported() {
  5299. try {
  5300. // fetch + stream is broken on Microsoft Edge. Disable before build 15048.
  5301. // see https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8196907/
  5302. // Fixed in Jan 10, 2017. Build 15048+ removed from blacklist.
  5303. var isWorkWellEdge = _browser2.default.msedge && _browser2.default.version.minor >= 15048;
  5304. var browserNotBlacklisted = _browser2.default.msedge ? isWorkWellEdge : true;
  5305. return self.fetch && self.ReadableStream && browserNotBlacklisted;
  5306. } catch (e) {
  5307. return false;
  5308. }
  5309. }
  5310. }]);
  5311. function FetchStreamLoader(seekHandler, config) {
  5312. _classCallCheck(this, FetchStreamLoader);
  5313. var _this = _possibleConstructorReturn(this, (FetchStreamLoader.__proto__ || Object.getPrototypeOf(FetchStreamLoader)).call(this, 'fetch-stream-loader'));
  5314. _this.TAG = 'FetchStreamLoader';
  5315. _this._seekHandler = seekHandler;
  5316. _this._config = config;
  5317. _this._needStash = true;
  5318. _this._requestAbort = false;
  5319. _this._contentLength = null;
  5320. _this._receivedLength = 0;
  5321. return _this;
  5322. }
  5323. _createClass(FetchStreamLoader, [{
  5324. key: 'destroy',
  5325. value: function destroy() {
  5326. if (this.isWorking()) {
  5327. this.abort();
  5328. }
  5329. _get(FetchStreamLoader.prototype.__proto__ || Object.getPrototypeOf(FetchStreamLoader.prototype), 'destroy', this).call(this);
  5330. }
  5331. }, {
  5332. key: 'open',
  5333. value: function open(dataSource, range) {
  5334. var _this2 = this;
  5335. this._dataSource = dataSource;
  5336. this._range = range;
  5337. var sourceURL = dataSource.url;
  5338. if (this._config.reuseRedirectedURL && dataSource.redirectedURL != undefined) {
  5339. sourceURL = dataSource.redirectedURL;
  5340. }
  5341. var seekConfig = this._seekHandler.getConfig(sourceURL, range);
  5342. var headers = new self.Headers();
  5343. if (_typeof(seekConfig.headers) === 'object') {
  5344. var configHeaders = seekConfig.headers;
  5345. for (var key in configHeaders) {
  5346. if (configHeaders.hasOwnProperty(key)) {
  5347. headers.append(key, configHeaders[key]);
  5348. }
  5349. }
  5350. }
  5351. var params = {
  5352. method: 'GET',
  5353. headers: headers,
  5354. mode: 'cors',
  5355. cache: 'default',
  5356. // The default policy of Fetch API in the whatwg standard
  5357. // Safari incorrectly indicates 'no-referrer' as default policy, fuck it
  5358. referrerPolicy: 'no-referrer-when-downgrade'
  5359. };
  5360. // cors is enabled by default
  5361. if (dataSource.cors === false) {
  5362. // no-cors means 'disregard cors policy', which can only be used in ServiceWorker
  5363. params.mode = 'same-origin';
  5364. }
  5365. // withCredentials is disabled by default
  5366. if (dataSource.withCredentials) {
  5367. params.credentials = 'include';
  5368. }
  5369. // referrerPolicy from config
  5370. if (dataSource.referrerPolicy) {
  5371. params.referrerPolicy = dataSource.referrerPolicy;
  5372. }
  5373. this._status = _loader.LoaderStatus.kConnecting;
  5374. self.fetch(seekConfig.url, params).then(function (res) {
  5375. if (_this2._requestAbort) {
  5376. _this2._requestAbort = false;
  5377. _this2._status = _loader.LoaderStatus.kIdle;
  5378. return;
  5379. }
  5380. if (res.ok && res.status >= 200 && res.status <= 299) {
  5381. if (res.url !== seekConfig.url) {
  5382. if (_this2._onURLRedirect) {
  5383. var redirectedURL = _this2._seekHandler.removeURLParameters(res.url);
  5384. _this2._onURLRedirect(redirectedURL);
  5385. }
  5386. }
  5387. var lengthHeader = res.headers.get('Content-Length');
  5388. if (lengthHeader != null) {
  5389. _this2._contentLength = parseInt(lengthHeader);
  5390. if (_this2._contentLength !== 0) {
  5391. if (_this2._onContentLengthKnown) {
  5392. _this2._onContentLengthKnown(_this2._contentLength);
  5393. }
  5394. }
  5395. }
  5396. return _this2._pump.call(_this2, res.body.getReader());
  5397. } else {
  5398. _this2._status = _loader.LoaderStatus.kError;
  5399. if (_this2._onError) {
  5400. _this2._onError(_loader.LoaderErrors.HTTP_STATUS_CODE_INVALID, { code: res.status, msg: res.statusText });
  5401. } else {
  5402. throw new _exception.RuntimeException('FetchStreamLoader: Http code invalid, ' + res.status + ' ' + res.statusText);
  5403. }
  5404. }
  5405. }).catch(function (e) {
  5406. _this2._status = _loader.LoaderStatus.kError;
  5407. if (_this2._onError) {
  5408. _this2._onError(_loader.LoaderErrors.EXCEPTION, { code: -1, msg: e.message });
  5409. } else {
  5410. throw e;
  5411. }
  5412. });
  5413. }
  5414. }, {
  5415. key: 'abort',
  5416. value: function abort() {
  5417. this._requestAbort = true;
  5418. }
  5419. }, {
  5420. key: '_pump',
  5421. value: function _pump(reader) {
  5422. var _this3 = this;
  5423. // ReadableStreamReader
  5424. return reader.read().then(function (result) {
  5425. if (result.done) {
  5426. // First check received length
  5427. if (_this3._contentLength !== null && _this3._receivedLength < _this3._contentLength) {
  5428. // Report Early-EOF
  5429. _this3._status = _loader.LoaderStatus.kError;
  5430. var type = _loader.LoaderErrors.EARLY_EOF;
  5431. var info = { code: -1, msg: 'Fetch stream meet Early-EOF' };
  5432. if (_this3._onError) {
  5433. _this3._onError(type, info);
  5434. } else {
  5435. throw new _exception.RuntimeException(info.msg);
  5436. }
  5437. } else {
  5438. // OK. Download complete
  5439. _this3._status = _loader.LoaderStatus.kComplete;
  5440. if (_this3._onComplete) {
  5441. _this3._onComplete(_this3._range.from, _this3._range.from + _this3._receivedLength - 1);
  5442. }
  5443. }
  5444. } else {
  5445. if (_this3._requestAbort === true) {
  5446. _this3._requestAbort = false;
  5447. _this3._status = _loader.LoaderStatus.kComplete;
  5448. return reader.cancel();
  5449. }
  5450. _this3._status = _loader.LoaderStatus.kBuffering;
  5451. var chunk = result.value.buffer;
  5452. var byteStart = _this3._range.from + _this3._receivedLength;
  5453. _this3._receivedLength += chunk.byteLength;
  5454. if (_this3._onDataArrival) {
  5455. _this3._onDataArrival(chunk, byteStart, _this3._receivedLength);
  5456. }
  5457. _this3._pump(reader);
  5458. }
  5459. }).catch(function (e) {
  5460. if (e.code === 11 && _browser2.default.msedge) {
  5461. // InvalidStateError on Microsoft Edge
  5462. // Workaround: Edge may throw InvalidStateError after ReadableStreamReader.cancel() call
  5463. // Ignore the unknown exception.
  5464. // Related issue: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/11265202/
  5465. return;
  5466. }
  5467. _this3._status = _loader.LoaderStatus.kError;
  5468. var type = 0;
  5469. var info = null;
  5470. if ((e.code === 19 || e.message === 'network error') && ( // NETWORK_ERR
  5471. _this3._contentLength === null || _this3._contentLength !== null && _this3._receivedLength < _this3._contentLength)) {
  5472. type = _loader.LoaderErrors.EARLY_EOF;
  5473. info = { code: e.code, msg: 'Fetch stream meet Early-EOF' };
  5474. } else {
  5475. type = _loader.LoaderErrors.EXCEPTION;
  5476. info = { code: e.code, msg: e.message };
  5477. }
  5478. if (_this3._onError) {
  5479. _this3._onError(type, info);
  5480. } else {
  5481. throw new _exception.RuntimeException(info.msg);
  5482. }
  5483. });
  5484. }
  5485. }]);
  5486. return FetchStreamLoader;
  5487. }(_loader.BaseLoader);
  5488. exports.default = FetchStreamLoader;
  5489. },{"../utils/browser.js":39,"../utils/exception.js":40,"../utils/logger.js":41,"./loader.js":24}],23:[function(_dereq_,module,exports){
  5490. 'use strict';
  5491. Object.defineProperty(exports, "__esModule", {
  5492. value: true
  5493. });
  5494. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  5495. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  5496. *
  5497. * @author zheng qian <xqq@xqq.im>
  5498. *
  5499. * Licensed under the Apache License, Version 2.0 (the "License");
  5500. * you may not use this file except in compliance with the License.
  5501. * You may obtain a copy of the License at
  5502. *
  5503. * http://www.apache.org/licenses/LICENSE-2.0
  5504. *
  5505. * Unless required by applicable law or agreed to in writing, software
  5506. * distributed under the License is distributed on an "AS IS" BASIS,
  5507. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  5508. * See the License for the specific language governing permissions and
  5509. * limitations under the License.
  5510. */
  5511. var _logger = _dereq_('../utils/logger.js');
  5512. var _logger2 = _interopRequireDefault(_logger);
  5513. var _speedSampler = _dereq_('./speed-sampler.js');
  5514. var _speedSampler2 = _interopRequireDefault(_speedSampler);
  5515. var _loader = _dereq_('./loader.js');
  5516. var _fetchStreamLoader = _dereq_('./fetch-stream-loader.js');
  5517. var _fetchStreamLoader2 = _interopRequireDefault(_fetchStreamLoader);
  5518. var _xhrMozChunkedLoader = _dereq_('./xhr-moz-chunked-loader.js');
  5519. var _xhrMozChunkedLoader2 = _interopRequireDefault(_xhrMozChunkedLoader);
  5520. var _xhrMsstreamLoader = _dereq_('./xhr-msstream-loader.js');
  5521. var _xhrMsstreamLoader2 = _interopRequireDefault(_xhrMsstreamLoader);
  5522. var _xhrRangeLoader = _dereq_('./xhr-range-loader.js');
  5523. var _xhrRangeLoader2 = _interopRequireDefault(_xhrRangeLoader);
  5524. var _websocketLoader = _dereq_('./websocket-loader.js');
  5525. var _websocketLoader2 = _interopRequireDefault(_websocketLoader);
  5526. var _rangeSeekHandler = _dereq_('./range-seek-handler.js');
  5527. var _rangeSeekHandler2 = _interopRequireDefault(_rangeSeekHandler);
  5528. var _paramSeekHandler = _dereq_('./param-seek-handler.js');
  5529. var _paramSeekHandler2 = _interopRequireDefault(_paramSeekHandler);
  5530. var _exception = _dereq_('../utils/exception.js');
  5531. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5532. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5533. /**
  5534. * DataSource: {
  5535. * url: string,
  5536. * filesize: number,
  5537. * cors: boolean,
  5538. * withCredentials: boolean
  5539. * }
  5540. *
  5541. */
  5542. // Manage IO Loaders
  5543. var IOController = function () {
  5544. function IOController(dataSource, config, extraData) {
  5545. _classCallCheck(this, IOController);
  5546. this.TAG = 'IOController';
  5547. this._config = config;
  5548. this._extraData = extraData;
  5549. this._stashInitialSize = 1024 * 384; // default initial size: 384KB
  5550. if (config.stashInitialSize != undefined && config.stashInitialSize > 0) {
  5551. // apply from config
  5552. this._stashInitialSize = config.stashInitialSize;
  5553. }
  5554. this._stashUsed = 0;
  5555. this._stashSize = this._stashInitialSize;
  5556. this._bufferSize = 1024 * 1024 * 3; // initial size: 3MB
  5557. this._stashBuffer = new ArrayBuffer(this._bufferSize);
  5558. this._stashByteStart = 0;
  5559. this._enableStash = true;
  5560. if (config.enableStashBuffer === false) {
  5561. this._enableStash = false;
  5562. }
  5563. this._loader = null;
  5564. this._loaderClass = null;
  5565. this._seekHandler = null;
  5566. this._dataSource = dataSource;
  5567. this._isWebSocketURL = /wss?:\/\/(.+?)/.test(dataSource.url);
  5568. this._refTotalLength = dataSource.filesize ? dataSource.filesize : null;
  5569. this._totalLength = this._refTotalLength;
  5570. this._fullRequestFlag = false;
  5571. this._currentRange = null;
  5572. this._redirectedURL = null;
  5573. this._speedNormalized = 0;
  5574. this._speedSampler = new _speedSampler2.default();
  5575. this._speedNormalizeList = [64, 128, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096];
  5576. this._isEarlyEofReconnecting = false;
  5577. this._paused = false;
  5578. this._resumeFrom = 0;
  5579. this._onDataArrival = null;
  5580. this._onSeeked = null;
  5581. this._onError = null;
  5582. this._onComplete = null;
  5583. this._onRedirect = null;
  5584. this._onRecoveredEarlyEof = null;
  5585. this._selectSeekHandler();
  5586. this._selectLoader();
  5587. this._createLoader();
  5588. }
  5589. _createClass(IOController, [{
  5590. key: 'destroy',
  5591. value: function destroy() {
  5592. if (this._loader.isWorking()) {
  5593. this._loader.abort();
  5594. }
  5595. this._loader.destroy();
  5596. this._loader = null;
  5597. this._loaderClass = null;
  5598. this._dataSource = null;
  5599. this._stashBuffer = null;
  5600. this._stashUsed = this._stashSize = this._bufferSize = this._stashByteStart = 0;
  5601. this._currentRange = null;
  5602. this._speedSampler = null;
  5603. this._isEarlyEofReconnecting = false;
  5604. this._onDataArrival = null;
  5605. this._onSeeked = null;
  5606. this._onError = null;
  5607. this._onComplete = null;
  5608. this._onRedirect = null;
  5609. this._onRecoveredEarlyEof = null;
  5610. this._extraData = null;
  5611. }
  5612. }, {
  5613. key: 'isWorking',
  5614. value: function isWorking() {
  5615. return this._loader && this._loader.isWorking() && !this._paused;
  5616. }
  5617. }, {
  5618. key: 'isPaused',
  5619. value: function isPaused() {
  5620. return this._paused;
  5621. }
  5622. }, {
  5623. key: '_selectSeekHandler',
  5624. value: function _selectSeekHandler() {
  5625. var config = this._config;
  5626. if (config.seekType === 'range') {
  5627. this._seekHandler = new _rangeSeekHandler2.default(this._config.rangeLoadZeroStart);
  5628. } else if (config.seekType === 'param') {
  5629. var paramStart = config.seekParamStart || 'bstart';
  5630. var paramEnd = config.seekParamEnd || 'bend';
  5631. this._seekHandler = new _paramSeekHandler2.default(paramStart, paramEnd);
  5632. } else if (config.seekType === 'custom') {
  5633. if (typeof config.customSeekHandler !== 'function') {
  5634. throw new _exception.InvalidArgumentException('Custom seekType specified in config but invalid customSeekHandler!');
  5635. }
  5636. this._seekHandler = new config.customSeekHandler();
  5637. } else {
  5638. throw new _exception.InvalidArgumentException('Invalid seekType in config: ' + config.seekType);
  5639. }
  5640. }
  5641. }, {
  5642. key: '_selectLoader',
  5643. value: function _selectLoader() {
  5644. if (this._config.customLoader != null) {
  5645. this._loaderClass = this._config.customLoader;
  5646. } else if (this._isWebSocketURL) {
  5647. this._loaderClass = _websocketLoader2.default;
  5648. } else if (_fetchStreamLoader2.default.isSupported()) {
  5649. this._loaderClass = _fetchStreamLoader2.default;
  5650. } else if (_xhrMozChunkedLoader2.default.isSupported()) {
  5651. this._loaderClass = _xhrMozChunkedLoader2.default;
  5652. } else if (_xhrRangeLoader2.default.isSupported()) {
  5653. this._loaderClass = _xhrRangeLoader2.default;
  5654. } else {
  5655. throw new _exception.RuntimeException('Your browser doesn\'t support xhr with arraybuffer responseType!');
  5656. }
  5657. }
  5658. }, {
  5659. key: '_createLoader',
  5660. value: function _createLoader() {
  5661. this._loader = new this._loaderClass(this._seekHandler, this._config);
  5662. if (this._loader.needStashBuffer === false) {
  5663. this._enableStash = false;
  5664. }
  5665. this._loader.onContentLengthKnown = this._onContentLengthKnown.bind(this);
  5666. this._loader.onURLRedirect = this._onURLRedirect.bind(this);
  5667. this._loader.onDataArrival = this._onLoaderChunkArrival.bind(this);
  5668. this._loader.onComplete = this._onLoaderComplete.bind(this);
  5669. this._loader.onError = this._onLoaderError.bind(this);
  5670. }
  5671. }, {
  5672. key: 'open',
  5673. value: function open(optionalFrom) {
  5674. this._currentRange = { from: 0, to: -1 };
  5675. if (optionalFrom) {
  5676. this._currentRange.from = optionalFrom;
  5677. }
  5678. this._speedSampler.reset();
  5679. if (!optionalFrom) {
  5680. this._fullRequestFlag = true;
  5681. }
  5682. this._loader.open(this._dataSource, Object.assign({}, this._currentRange));
  5683. }
  5684. }, {
  5685. key: 'abort',
  5686. value: function abort() {
  5687. this._loader.abort();
  5688. if (this._paused) {
  5689. this._paused = false;
  5690. this._resumeFrom = 0;
  5691. }
  5692. }
  5693. }, {
  5694. key: 'pause',
  5695. value: function pause() {
  5696. if (this.isWorking()) {
  5697. this._loader.abort();
  5698. if (this._stashUsed !== 0) {
  5699. this._resumeFrom = this._stashByteStart;
  5700. this._currentRange.to = this._stashByteStart - 1;
  5701. } else {
  5702. this._resumeFrom = this._currentRange.to + 1;
  5703. }
  5704. this._stashUsed = 0;
  5705. this._stashByteStart = 0;
  5706. this._paused = true;
  5707. }
  5708. }
  5709. }, {
  5710. key: 'resume',
  5711. value: function resume() {
  5712. if (this._paused) {
  5713. this._paused = false;
  5714. var bytes = this._resumeFrom;
  5715. this._resumeFrom = 0;
  5716. this._internalSeek(bytes, true);
  5717. }
  5718. }
  5719. }, {
  5720. key: 'seek',
  5721. value: function seek(bytes) {
  5722. this._paused = false;
  5723. this._stashUsed = 0;
  5724. this._stashByteStart = 0;
  5725. this._internalSeek(bytes, true);
  5726. }
  5727. /**
  5728. * When seeking request is from media seeking, unconsumed stash data should be dropped
  5729. * However, stash data shouldn't be dropped if seeking requested from http reconnection
  5730. *
  5731. * @dropUnconsumed: Ignore and discard all unconsumed data in stash buffer
  5732. */
  5733. }, {
  5734. key: '_internalSeek',
  5735. value: function _internalSeek(bytes, dropUnconsumed) {
  5736. if (this._loader.isWorking()) {
  5737. this._loader.abort();
  5738. }
  5739. // dispatch & flush stash buffer before seek
  5740. this._flushStashBuffer(dropUnconsumed);
  5741. this._loader.destroy();
  5742. this._loader = null;
  5743. var requestRange = { from: bytes, to: -1 };
  5744. this._currentRange = { from: requestRange.from, to: -1 };
  5745. this._speedSampler.reset();
  5746. this._stashSize = this._stashInitialSize;
  5747. this._createLoader();
  5748. this._loader.open(this._dataSource, requestRange);
  5749. if (this._onSeeked) {
  5750. this._onSeeked();
  5751. }
  5752. }
  5753. }, {
  5754. key: 'updateUrl',
  5755. value: function updateUrl(url) {
  5756. if (!url || typeof url !== 'string' || url.length === 0) {
  5757. throw new _exception.InvalidArgumentException('Url must be a non-empty string!');
  5758. }
  5759. this._dataSource.url = url;
  5760. // TODO: replace with new url
  5761. }
  5762. }, {
  5763. key: '_expandBuffer',
  5764. value: function _expandBuffer(expectedBytes) {
  5765. var bufferNewSize = this._stashSize;
  5766. while (bufferNewSize + 1024 * 1024 * 1 < expectedBytes) {
  5767. bufferNewSize *= 2;
  5768. }
  5769. bufferNewSize += 1024 * 1024 * 1; // bufferSize = stashSize + 1MB
  5770. if (bufferNewSize === this._bufferSize) {
  5771. return;
  5772. }
  5773. var newBuffer = new ArrayBuffer(bufferNewSize);
  5774. if (this._stashUsed > 0) {
  5775. // copy existing data into new buffer
  5776. var stashOldArray = new Uint8Array(this._stashBuffer, 0, this._stashUsed);
  5777. var stashNewArray = new Uint8Array(newBuffer, 0, bufferNewSize);
  5778. stashNewArray.set(stashOldArray, 0);
  5779. }
  5780. this._stashBuffer = newBuffer;
  5781. this._bufferSize = bufferNewSize;
  5782. }
  5783. }, {
  5784. key: '_normalizeSpeed',
  5785. value: function _normalizeSpeed(input) {
  5786. var list = this._speedNormalizeList;
  5787. var last = list.length - 1;
  5788. var mid = 0;
  5789. var lbound = 0;
  5790. var ubound = last;
  5791. if (input < list[0]) {
  5792. return list[0];
  5793. }
  5794. // binary search
  5795. while (lbound <= ubound) {
  5796. mid = lbound + Math.floor((ubound - lbound) / 2);
  5797. if (mid === last || input >= list[mid] && input < list[mid + 1]) {
  5798. return list[mid];
  5799. } else if (list[mid] < input) {
  5800. lbound = mid + 1;
  5801. } else {
  5802. ubound = mid - 1;
  5803. }
  5804. }
  5805. }
  5806. }, {
  5807. key: '_adjustStashSize',
  5808. value: function _adjustStashSize(normalized) {
  5809. var stashSizeKB = 0;
  5810. if (this._config.isLive) {
  5811. // live stream: always use single normalized speed for size of stashSizeKB
  5812. stashSizeKB = normalized;
  5813. } else {
  5814. if (normalized < 512) {
  5815. stashSizeKB = normalized;
  5816. } else if (normalized >= 512 && normalized <= 1024) {
  5817. stashSizeKB = Math.floor(normalized * 1.5);
  5818. } else {
  5819. stashSizeKB = normalized * 2;
  5820. }
  5821. }
  5822. if (stashSizeKB > 8192) {
  5823. stashSizeKB = 8192;
  5824. }
  5825. var bufferSize = stashSizeKB * 1024 + 1024 * 1024 * 1; // stashSize + 1MB
  5826. if (this._bufferSize < bufferSize) {
  5827. this._expandBuffer(bufferSize);
  5828. }
  5829. this._stashSize = stashSizeKB * 1024;
  5830. }
  5831. }, {
  5832. key: '_dispatchChunks',
  5833. value: function _dispatchChunks(chunks, byteStart) {
  5834. this._currentRange.to = byteStart + chunks.byteLength - 1;
  5835. return this._onDataArrival(chunks, byteStart);
  5836. }
  5837. }, {
  5838. key: '_onURLRedirect',
  5839. value: function _onURLRedirect(redirectedURL) {
  5840. this._redirectedURL = redirectedURL;
  5841. if (this._onRedirect) {
  5842. this._onRedirect(redirectedURL);
  5843. }
  5844. }
  5845. }, {
  5846. key: '_onContentLengthKnown',
  5847. value: function _onContentLengthKnown(contentLength) {
  5848. if (contentLength && this._fullRequestFlag) {
  5849. this._totalLength = contentLength;
  5850. this._fullRequestFlag = false;
  5851. }
  5852. }
  5853. }, {
  5854. key: '_onLoaderChunkArrival',
  5855. value: function _onLoaderChunkArrival(chunk, byteStart, receivedLength) {
  5856. if (!this._onDataArrival) {
  5857. throw new _exception.IllegalStateException('IOController: No existing consumer (onDataArrival) callback!');
  5858. }
  5859. if (this._paused) {
  5860. return;
  5861. }
  5862. if (this._isEarlyEofReconnecting) {
  5863. // Auto-reconnect for EarlyEof succeed, notify to upper-layer by callback
  5864. this._isEarlyEofReconnecting = false;
  5865. if (this._onRecoveredEarlyEof) {
  5866. this._onRecoveredEarlyEof();
  5867. }
  5868. }
  5869. this._speedSampler.addBytes(chunk.byteLength);
  5870. // adjust stash buffer size according to network speed dynamically
  5871. var KBps = this._speedSampler.lastSecondKBps;
  5872. if (KBps !== 0) {
  5873. var normalized = this._normalizeSpeed(KBps);
  5874. if (this._speedNormalized !== normalized) {
  5875. this._speedNormalized = normalized;
  5876. this._adjustStashSize(normalized);
  5877. }
  5878. }
  5879. if (!this._enableStash) {
  5880. // disable stash
  5881. if (this._stashUsed === 0) {
  5882. // dispatch chunk directly to consumer;
  5883. // check ret value (consumed bytes) and stash unconsumed to stashBuffer
  5884. var consumed = this._dispatchChunks(chunk, byteStart);
  5885. if (consumed < chunk.byteLength) {
  5886. // unconsumed data remain.
  5887. var remain = chunk.byteLength - consumed;
  5888. if (remain > this._bufferSize) {
  5889. this._expandBuffer(remain);
  5890. }
  5891. var stashArray = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5892. stashArray.set(new Uint8Array(chunk, consumed), 0);
  5893. this._stashUsed += remain;
  5894. this._stashByteStart = byteStart + consumed;
  5895. }
  5896. } else {
  5897. // else: Merge chunk into stashBuffer, and dispatch stashBuffer to consumer.
  5898. if (this._stashUsed + chunk.byteLength > this._bufferSize) {
  5899. this._expandBuffer(this._stashUsed + chunk.byteLength);
  5900. }
  5901. var _stashArray = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5902. _stashArray.set(new Uint8Array(chunk), this._stashUsed);
  5903. this._stashUsed += chunk.byteLength;
  5904. var _consumed = this._dispatchChunks(this._stashBuffer.slice(0, this._stashUsed), this._stashByteStart);
  5905. if (_consumed < this._stashUsed && _consumed > 0) {
  5906. // unconsumed data remain
  5907. var remainArray = new Uint8Array(this._stashBuffer, _consumed);
  5908. _stashArray.set(remainArray, 0);
  5909. }
  5910. this._stashUsed -= _consumed;
  5911. this._stashByteStart += _consumed;
  5912. }
  5913. } else {
  5914. // enable stash
  5915. if (this._stashUsed === 0 && this._stashByteStart === 0) {
  5916. // seeked? or init chunk?
  5917. // This is the first chunk after seek action
  5918. this._stashByteStart = byteStart;
  5919. }
  5920. if (this._stashUsed + chunk.byteLength <= this._stashSize) {
  5921. // just stash
  5922. var _stashArray2 = new Uint8Array(this._stashBuffer, 0, this._stashSize);
  5923. _stashArray2.set(new Uint8Array(chunk), this._stashUsed);
  5924. this._stashUsed += chunk.byteLength;
  5925. } else {
  5926. // stashUsed + chunkSize > stashSize, size limit exceeded
  5927. var _stashArray3 = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5928. if (this._stashUsed > 0) {
  5929. // There're stash datas in buffer
  5930. // dispatch the whole stashBuffer, and stash remain data
  5931. // then append chunk to stashBuffer (stash)
  5932. var buffer = this._stashBuffer.slice(0, this._stashUsed);
  5933. var _consumed2 = this._dispatchChunks(buffer, this._stashByteStart);
  5934. if (_consumed2 < buffer.byteLength) {
  5935. if (_consumed2 > 0) {
  5936. var _remainArray = new Uint8Array(buffer, _consumed2);
  5937. _stashArray3.set(_remainArray, 0);
  5938. this._stashUsed = _remainArray.byteLength;
  5939. this._stashByteStart += _consumed2;
  5940. }
  5941. } else {
  5942. this._stashUsed = 0;
  5943. this._stashByteStart += _consumed2;
  5944. }
  5945. if (this._stashUsed + chunk.byteLength > this._bufferSize) {
  5946. this._expandBuffer(this._stashUsed + chunk.byteLength);
  5947. _stashArray3 = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5948. }
  5949. _stashArray3.set(new Uint8Array(chunk), this._stashUsed);
  5950. this._stashUsed += chunk.byteLength;
  5951. } else {
  5952. // stash buffer empty, but chunkSize > stashSize (oh, holy shit)
  5953. // dispatch chunk directly and stash remain data
  5954. var _consumed3 = this._dispatchChunks(chunk, byteStart);
  5955. if (_consumed3 < chunk.byteLength) {
  5956. var _remain = chunk.byteLength - _consumed3;
  5957. if (_remain > this._bufferSize) {
  5958. this._expandBuffer(_remain);
  5959. _stashArray3 = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5960. }
  5961. _stashArray3.set(new Uint8Array(chunk, _consumed3), 0);
  5962. this._stashUsed += _remain;
  5963. this._stashByteStart = byteStart + _consumed3;
  5964. }
  5965. }
  5966. }
  5967. }
  5968. }
  5969. }, {
  5970. key: '_flushStashBuffer',
  5971. value: function _flushStashBuffer(dropUnconsumed) {
  5972. if (this._stashUsed > 0) {
  5973. var buffer = this._stashBuffer.slice(0, this._stashUsed);
  5974. var consumed = this._dispatchChunks(buffer, this._stashByteStart);
  5975. var remain = buffer.byteLength - consumed;
  5976. if (consumed < buffer.byteLength) {
  5977. if (dropUnconsumed) {
  5978. _logger2.default.w(this.TAG, remain + ' bytes unconsumed data remain when flush buffer, dropped');
  5979. } else {
  5980. if (consumed > 0) {
  5981. var stashArray = new Uint8Array(this._stashBuffer, 0, this._bufferSize);
  5982. var remainArray = new Uint8Array(buffer, consumed);
  5983. stashArray.set(remainArray, 0);
  5984. this._stashUsed = remainArray.byteLength;
  5985. this._stashByteStart += consumed;
  5986. }
  5987. return 0;
  5988. }
  5989. }
  5990. this._stashUsed = 0;
  5991. this._stashByteStart = 0;
  5992. return remain;
  5993. }
  5994. return 0;
  5995. }
  5996. }, {
  5997. key: '_onLoaderComplete',
  5998. value: function _onLoaderComplete(from, to) {
  5999. // Force-flush stash buffer, and drop unconsumed data
  6000. this._flushStashBuffer(true);
  6001. if (this._onComplete) {
  6002. this._onComplete(this._extraData);
  6003. }
  6004. }
  6005. }, {
  6006. key: '_onLoaderError',
  6007. value: function _onLoaderError(type, data) {
  6008. _logger2.default.e(this.TAG, 'Loader error, code = ' + data.code + ', msg = ' + data.msg);
  6009. this._flushStashBuffer(false);
  6010. if (this._isEarlyEofReconnecting) {
  6011. // Auto-reconnect for EarlyEof failed, throw UnrecoverableEarlyEof error to upper-layer
  6012. this._isEarlyEofReconnecting = false;
  6013. type = _loader.LoaderErrors.UNRECOVERABLE_EARLY_EOF;
  6014. }
  6015. switch (type) {
  6016. case _loader.LoaderErrors.EARLY_EOF:
  6017. {
  6018. if (!this._config.isLive) {
  6019. // Do internal http reconnect if not live stream
  6020. if (this._totalLength) {
  6021. var nextFrom = this._currentRange.to + 1;
  6022. if (nextFrom < this._totalLength) {
  6023. _logger2.default.w(this.TAG, 'Connection lost, trying reconnect...');
  6024. this._isEarlyEofReconnecting = true;
  6025. this._internalSeek(nextFrom, false);
  6026. }
  6027. return;
  6028. }
  6029. // else: We don't know totalLength, throw UnrecoverableEarlyEof
  6030. }
  6031. // live stream: throw UnrecoverableEarlyEof error to upper-layer
  6032. type = _loader.LoaderErrors.UNRECOVERABLE_EARLY_EOF;
  6033. break;
  6034. }
  6035. case _loader.LoaderErrors.UNRECOVERABLE_EARLY_EOF:
  6036. case _loader.LoaderErrors.CONNECTING_TIMEOUT:
  6037. case _loader.LoaderErrors.HTTP_STATUS_CODE_INVALID:
  6038. case _loader.LoaderErrors.EXCEPTION:
  6039. break;
  6040. }
  6041. if (this._onError) {
  6042. this._onError(type, data);
  6043. } else {
  6044. throw new _exception.RuntimeException('IOException: ' + data.msg);
  6045. }
  6046. }
  6047. }, {
  6048. key: 'status',
  6049. get: function get() {
  6050. return this._loader.status;
  6051. }
  6052. }, {
  6053. key: 'extraData',
  6054. get: function get() {
  6055. return this._extraData;
  6056. },
  6057. set: function set(data) {
  6058. this._extraData = data;
  6059. }
  6060. // prototype: function onDataArrival(chunks: ArrayBuffer, byteStart: number): number
  6061. }, {
  6062. key: 'onDataArrival',
  6063. get: function get() {
  6064. return this._onDataArrival;
  6065. },
  6066. set: function set(callback) {
  6067. this._onDataArrival = callback;
  6068. }
  6069. }, {
  6070. key: 'onSeeked',
  6071. get: function get() {
  6072. return this._onSeeked;
  6073. },
  6074. set: function set(callback) {
  6075. this._onSeeked = callback;
  6076. }
  6077. // prototype: function onError(type: number, info: {code: number, msg: string}): void
  6078. }, {
  6079. key: 'onError',
  6080. get: function get() {
  6081. return this._onError;
  6082. },
  6083. set: function set(callback) {
  6084. this._onError = callback;
  6085. }
  6086. }, {
  6087. key: 'onComplete',
  6088. get: function get() {
  6089. return this._onComplete;
  6090. },
  6091. set: function set(callback) {
  6092. this._onComplete = callback;
  6093. }
  6094. }, {
  6095. key: 'onRedirect',
  6096. get: function get() {
  6097. return this._onRedirect;
  6098. },
  6099. set: function set(callback) {
  6100. this._onRedirect = callback;
  6101. }
  6102. }, {
  6103. key: 'onRecoveredEarlyEof',
  6104. get: function get() {
  6105. return this._onRecoveredEarlyEof;
  6106. },
  6107. set: function set(callback) {
  6108. this._onRecoveredEarlyEof = callback;
  6109. }
  6110. }, {
  6111. key: 'currentURL',
  6112. get: function get() {
  6113. return this._dataSource.url;
  6114. }
  6115. }, {
  6116. key: 'hasRedirect',
  6117. get: function get() {
  6118. return this._redirectedURL != null || this._dataSource.redirectedURL != undefined;
  6119. }
  6120. }, {
  6121. key: 'currentRedirectedURL',
  6122. get: function get() {
  6123. return this._redirectedURL || this._dataSource.redirectedURL;
  6124. }
  6125. // in KB/s
  6126. }, {
  6127. key: 'currentSpeed',
  6128. get: function get() {
  6129. if (this._loaderClass === _xhrRangeLoader2.default) {
  6130. // SpeedSampler is inaccuracy if loader is RangeLoader
  6131. return this._loader.currentSpeed;
  6132. }
  6133. return this._speedSampler.lastSecondKBps;
  6134. }
  6135. }, {
  6136. key: 'loaderType',
  6137. get: function get() {
  6138. return this._loader.type;
  6139. }
  6140. }]);
  6141. return IOController;
  6142. }();
  6143. exports.default = IOController;
  6144. },{"../utils/exception.js":40,"../utils/logger.js":41,"./fetch-stream-loader.js":22,"./loader.js":24,"./param-seek-handler.js":25,"./range-seek-handler.js":26,"./speed-sampler.js":27,"./websocket-loader.js":28,"./xhr-moz-chunked-loader.js":29,"./xhr-msstream-loader.js":30,"./xhr-range-loader.js":31}],24:[function(_dereq_,module,exports){
  6145. 'use strict';
  6146. Object.defineProperty(exports, "__esModule", {
  6147. value: true
  6148. });
  6149. exports.BaseLoader = exports.LoaderErrors = exports.LoaderStatus = undefined;
  6150. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  6151. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6152. *
  6153. * @author zheng qian <xqq@xqq.im>
  6154. *
  6155. * Licensed under the Apache License, Version 2.0 (the "License");
  6156. * you may not use this file except in compliance with the License.
  6157. * You may obtain a copy of the License at
  6158. *
  6159. * http://www.apache.org/licenses/LICENSE-2.0
  6160. *
  6161. * Unless required by applicable law or agreed to in writing, software
  6162. * distributed under the License is distributed on an "AS IS" BASIS,
  6163. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6164. * See the License for the specific language governing permissions and
  6165. * limitations under the License.
  6166. */
  6167. var _exception = _dereq_('../utils/exception.js');
  6168. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6169. var LoaderStatus = exports.LoaderStatus = {
  6170. kIdle: 0,
  6171. kConnecting: 1,
  6172. kBuffering: 2,
  6173. kError: 3,
  6174. kComplete: 4
  6175. };
  6176. var LoaderErrors = exports.LoaderErrors = {
  6177. OK: 'OK',
  6178. EXCEPTION: 'Exception',
  6179. HTTP_STATUS_CODE_INVALID: 'HttpStatusCodeInvalid',
  6180. CONNECTING_TIMEOUT: 'ConnectingTimeout',
  6181. EARLY_EOF: 'EarlyEof',
  6182. UNRECOVERABLE_EARLY_EOF: 'UnrecoverableEarlyEof'
  6183. };
  6184. /* Loader has callbacks which have following prototypes:
  6185. * function onContentLengthKnown(contentLength: number): void
  6186. * function onURLRedirect(url: string): void
  6187. * function onDataArrival(chunk: ArrayBuffer, byteStart: number, receivedLength: number): void
  6188. * function onError(errorType: number, errorInfo: {code: number, msg: string}): void
  6189. * function onComplete(rangeFrom: number, rangeTo: number): void
  6190. */
  6191. var BaseLoader = exports.BaseLoader = function () {
  6192. function BaseLoader(typeName) {
  6193. _classCallCheck(this, BaseLoader);
  6194. this._type = typeName || 'undefined';
  6195. this._status = LoaderStatus.kIdle;
  6196. this._needStash = false;
  6197. // callbacks
  6198. this._onContentLengthKnown = null;
  6199. this._onURLRedirect = null;
  6200. this._onDataArrival = null;
  6201. this._onError = null;
  6202. this._onComplete = null;
  6203. }
  6204. _createClass(BaseLoader, [{
  6205. key: 'destroy',
  6206. value: function destroy() {
  6207. this._status = LoaderStatus.kIdle;
  6208. this._onContentLengthKnown = null;
  6209. this._onURLRedirect = null;
  6210. this._onDataArrival = null;
  6211. this._onError = null;
  6212. this._onComplete = null;
  6213. }
  6214. }, {
  6215. key: 'isWorking',
  6216. value: function isWorking() {
  6217. return this._status === LoaderStatus.kConnecting || this._status === LoaderStatus.kBuffering;
  6218. }
  6219. }, {
  6220. key: 'open',
  6221. // pure virtual
  6222. value: function open(dataSource, range) {
  6223. throw new _exception.NotImplementedException('Unimplemented abstract function!');
  6224. }
  6225. }, {
  6226. key: 'abort',
  6227. value: function abort() {
  6228. throw new _exception.NotImplementedException('Unimplemented abstract function!');
  6229. }
  6230. }, {
  6231. key: 'type',
  6232. get: function get() {
  6233. return this._type;
  6234. }
  6235. }, {
  6236. key: 'status',
  6237. get: function get() {
  6238. return this._status;
  6239. }
  6240. }, {
  6241. key: 'needStashBuffer',
  6242. get: function get() {
  6243. return this._needStash;
  6244. }
  6245. }, {
  6246. key: 'onContentLengthKnown',
  6247. get: function get() {
  6248. return this._onContentLengthKnown;
  6249. },
  6250. set: function set(callback) {
  6251. this._onContentLengthKnown = callback;
  6252. }
  6253. }, {
  6254. key: 'onURLRedirect',
  6255. get: function get() {
  6256. return this._onURLRedirect;
  6257. },
  6258. set: function set(callback) {
  6259. this._onURLRedirect = callback;
  6260. }
  6261. }, {
  6262. key: 'onDataArrival',
  6263. get: function get() {
  6264. return this._onDataArrival;
  6265. },
  6266. set: function set(callback) {
  6267. this._onDataArrival = callback;
  6268. }
  6269. }, {
  6270. key: 'onError',
  6271. get: function get() {
  6272. return this._onError;
  6273. },
  6274. set: function set(callback) {
  6275. this._onError = callback;
  6276. }
  6277. }, {
  6278. key: 'onComplete',
  6279. get: function get() {
  6280. return this._onComplete;
  6281. },
  6282. set: function set(callback) {
  6283. this._onComplete = callback;
  6284. }
  6285. }]);
  6286. return BaseLoader;
  6287. }();
  6288. },{"../utils/exception.js":40}],25:[function(_dereq_,module,exports){
  6289. 'use strict';
  6290. Object.defineProperty(exports, "__esModule", {
  6291. value: true
  6292. });
  6293. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6294. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6295. /*
  6296. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6297. *
  6298. * @author zheng qian <xqq@xqq.im>
  6299. *
  6300. * Licensed under the Apache License, Version 2.0 (the "License");
  6301. * you may not use this file except in compliance with the License.
  6302. * You may obtain a copy of the License at
  6303. *
  6304. * http://www.apache.org/licenses/LICENSE-2.0
  6305. *
  6306. * Unless required by applicable law or agreed to in writing, software
  6307. * distributed under the License is distributed on an "AS IS" BASIS,
  6308. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6309. * See the License for the specific language governing permissions and
  6310. * limitations under the License.
  6311. */
  6312. var ParamSeekHandler = function () {
  6313. function ParamSeekHandler(paramStart, paramEnd) {
  6314. _classCallCheck(this, ParamSeekHandler);
  6315. this._startName = paramStart;
  6316. this._endName = paramEnd;
  6317. }
  6318. _createClass(ParamSeekHandler, [{
  6319. key: 'getConfig',
  6320. value: function getConfig(baseUrl, range) {
  6321. var url = baseUrl;
  6322. if (range.from !== 0 || range.to !== -1) {
  6323. var needAnd = true;
  6324. if (url.indexOf('?') === -1) {
  6325. url += '?';
  6326. needAnd = false;
  6327. }
  6328. if (needAnd) {
  6329. url += '&';
  6330. }
  6331. url += this._startName + '=' + range.from.toString();
  6332. if (range.to !== -1) {
  6333. url += '&' + this._endName + '=' + range.to.toString();
  6334. }
  6335. }
  6336. return {
  6337. url: url,
  6338. headers: {}
  6339. };
  6340. }
  6341. }, {
  6342. key: 'removeURLParameters',
  6343. value: function removeURLParameters(seekedURL) {
  6344. var baseURL = seekedURL.split('?')[0];
  6345. var params = undefined;
  6346. var queryIndex = seekedURL.indexOf('?');
  6347. if (queryIndex !== -1) {
  6348. params = seekedURL.substring(queryIndex + 1);
  6349. }
  6350. var resultParams = '';
  6351. if (params != undefined && params.length > 0) {
  6352. var pairs = params.split('&');
  6353. for (var i = 0; i < pairs.length; i++) {
  6354. var pair = pairs[i].split('=');
  6355. var requireAnd = i > 0;
  6356. if (pair[0] !== this._startName && pair[0] !== this._endName) {
  6357. if (requireAnd) {
  6358. resultParams += '&';
  6359. }
  6360. resultParams += pairs[i];
  6361. }
  6362. }
  6363. }
  6364. return resultParams.length === 0 ? baseURL : baseURL + '?' + resultParams;
  6365. }
  6366. }]);
  6367. return ParamSeekHandler;
  6368. }();
  6369. exports.default = ParamSeekHandler;
  6370. },{}],26:[function(_dereq_,module,exports){
  6371. 'use strict';
  6372. Object.defineProperty(exports, "__esModule", {
  6373. value: true
  6374. });
  6375. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6376. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6377. /*
  6378. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6379. *
  6380. * @author zheng qian <xqq@xqq.im>
  6381. *
  6382. * Licensed under the Apache License, Version 2.0 (the "License");
  6383. * you may not use this file except in compliance with the License.
  6384. * You may obtain a copy of the License at
  6385. *
  6386. * http://www.apache.org/licenses/LICENSE-2.0
  6387. *
  6388. * Unless required by applicable law or agreed to in writing, software
  6389. * distributed under the License is distributed on an "AS IS" BASIS,
  6390. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6391. * See the License for the specific language governing permissions and
  6392. * limitations under the License.
  6393. */
  6394. var RangeSeekHandler = function () {
  6395. function RangeSeekHandler(zeroStart) {
  6396. _classCallCheck(this, RangeSeekHandler);
  6397. this._zeroStart = zeroStart || false;
  6398. }
  6399. _createClass(RangeSeekHandler, [{
  6400. key: 'getConfig',
  6401. value: function getConfig(url, range) {
  6402. var headers = {};
  6403. if (range.from !== 0 || range.to !== -1) {
  6404. var param = void 0;
  6405. if (range.to !== -1) {
  6406. param = 'bytes=' + range.from.toString() + '-' + range.to.toString();
  6407. } else {
  6408. param = 'bytes=' + range.from.toString() + '-';
  6409. }
  6410. headers['Range'] = param;
  6411. } else if (this._zeroStart) {
  6412. headers['Range'] = 'bytes=0-';
  6413. }
  6414. return {
  6415. url: url,
  6416. headers: headers
  6417. };
  6418. }
  6419. }, {
  6420. key: 'removeURLParameters',
  6421. value: function removeURLParameters(seekedURL) {
  6422. return seekedURL;
  6423. }
  6424. }]);
  6425. return RangeSeekHandler;
  6426. }();
  6427. exports.default = RangeSeekHandler;
  6428. },{}],27:[function(_dereq_,module,exports){
  6429. "use strict";
  6430. Object.defineProperty(exports, "__esModule", {
  6431. value: true
  6432. });
  6433. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6434. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6435. /*
  6436. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6437. *
  6438. * @author zheng qian <xqq@xqq.im>
  6439. *
  6440. * Licensed under the Apache License, Version 2.0 (the "License");
  6441. * you may not use this file except in compliance with the License.
  6442. * You may obtain a copy of the License at
  6443. *
  6444. * http://www.apache.org/licenses/LICENSE-2.0
  6445. *
  6446. * Unless required by applicable law or agreed to in writing, software
  6447. * distributed under the License is distributed on an "AS IS" BASIS,
  6448. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6449. * See the License for the specific language governing permissions and
  6450. * limitations under the License.
  6451. */
  6452. // Utility class to calculate realtime network I/O speed
  6453. var SpeedSampler = function () {
  6454. function SpeedSampler() {
  6455. _classCallCheck(this, SpeedSampler);
  6456. // milliseconds
  6457. this._firstCheckpoint = 0;
  6458. this._lastCheckpoint = 0;
  6459. this._intervalBytes = 0;
  6460. this._totalBytes = 0;
  6461. this._lastSecondBytes = 0;
  6462. // compatibility detection
  6463. if (self.performance && self.performance.now) {
  6464. this._now = self.performance.now.bind(self.performance);
  6465. } else {
  6466. this._now = Date.now;
  6467. }
  6468. }
  6469. _createClass(SpeedSampler, [{
  6470. key: "reset",
  6471. value: function reset() {
  6472. this._firstCheckpoint = this._lastCheckpoint = 0;
  6473. this._totalBytes = this._intervalBytes = 0;
  6474. this._lastSecondBytes = 0;
  6475. }
  6476. }, {
  6477. key: "addBytes",
  6478. value: function addBytes(bytes) {
  6479. if (this._firstCheckpoint === 0) {
  6480. this._firstCheckpoint = this._now();
  6481. this._lastCheckpoint = this._firstCheckpoint;
  6482. this._intervalBytes += bytes;
  6483. this._totalBytes += bytes;
  6484. } else if (this._now() - this._lastCheckpoint < 1000) {
  6485. this._intervalBytes += bytes;
  6486. this._totalBytes += bytes;
  6487. } else {
  6488. // duration >= 1000
  6489. this._lastSecondBytes = this._intervalBytes;
  6490. this._intervalBytes = bytes;
  6491. this._totalBytes += bytes;
  6492. this._lastCheckpoint = this._now();
  6493. }
  6494. }
  6495. }, {
  6496. key: "currentKBps",
  6497. get: function get() {
  6498. this.addBytes(0);
  6499. var durationSeconds = (this._now() - this._lastCheckpoint) / 1000;
  6500. if (durationSeconds == 0) durationSeconds = 1;
  6501. return this._intervalBytes / durationSeconds / 1024;
  6502. }
  6503. }, {
  6504. key: "lastSecondKBps",
  6505. get: function get() {
  6506. this.addBytes(0);
  6507. if (this._lastSecondBytes !== 0) {
  6508. return this._lastSecondBytes / 1024;
  6509. } else {
  6510. // lastSecondBytes === 0
  6511. if (this._now() - this._lastCheckpoint >= 500) {
  6512. // if time interval since last checkpoint has exceeded 500ms
  6513. // the speed is nearly accurate
  6514. return this.currentKBps;
  6515. } else {
  6516. // We don't know
  6517. return 0;
  6518. }
  6519. }
  6520. }
  6521. }, {
  6522. key: "averageKBps",
  6523. get: function get() {
  6524. var durationSeconds = (this._now() - this._firstCheckpoint) / 1000;
  6525. return this._totalBytes / durationSeconds / 1024;
  6526. }
  6527. }]);
  6528. return SpeedSampler;
  6529. }();
  6530. exports.default = SpeedSampler;
  6531. },{}],28:[function(_dereq_,module,exports){
  6532. 'use strict';
  6533. Object.defineProperty(exports, "__esModule", {
  6534. value: true
  6535. });
  6536. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  6537. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6538. var _logger = _dereq_('../utils/logger.js');
  6539. var _logger2 = _interopRequireDefault(_logger);
  6540. var _loader = _dereq_('./loader.js');
  6541. var _exception = _dereq_('../utils/exception.js');
  6542. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6543. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6544. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6545. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
  6546. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6547. *
  6548. * @author zheng qian <xqq@xqq.im>
  6549. *
  6550. * Licensed under the Apache License, Version 2.0 (the "License");
  6551. * you may not use this file except in compliance with the License.
  6552. * You may obtain a copy of the License at
  6553. *
  6554. * http://www.apache.org/licenses/LICENSE-2.0
  6555. *
  6556. * Unless required by applicable law or agreed to in writing, software
  6557. * distributed under the License is distributed on an "AS IS" BASIS,
  6558. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6559. * See the License for the specific language governing permissions and
  6560. * limitations under the License.
  6561. */
  6562. // For FLV over WebSocket live stream
  6563. var WebSocketLoader = function (_BaseLoader) {
  6564. _inherits(WebSocketLoader, _BaseLoader);
  6565. _createClass(WebSocketLoader, null, [{
  6566. key: 'isSupported',
  6567. value: function isSupported() {
  6568. try {
  6569. return typeof self.WebSocket !== 'undefined';
  6570. } catch (e) {
  6571. return false;
  6572. }
  6573. }
  6574. }]);
  6575. function WebSocketLoader() {
  6576. _classCallCheck(this, WebSocketLoader);
  6577. var _this = _possibleConstructorReturn(this, (WebSocketLoader.__proto__ || Object.getPrototypeOf(WebSocketLoader)).call(this, 'websocket-loader'));
  6578. _this.TAG = 'WebSocketLoader';
  6579. _this._needStash = true;
  6580. _this._ws = null;
  6581. _this._requestAbort = false;
  6582. _this._receivedLength = 0;
  6583. return _this;
  6584. }
  6585. _createClass(WebSocketLoader, [{
  6586. key: 'destroy',
  6587. value: function destroy() {
  6588. if (this._ws) {
  6589. this.abort();
  6590. }
  6591. _get(WebSocketLoader.prototype.__proto__ || Object.getPrototypeOf(WebSocketLoader.prototype), 'destroy', this).call(this);
  6592. }
  6593. }, {
  6594. key: 'open',
  6595. value: function open(dataSource) {
  6596. try {
  6597. var ws = this._ws = new self.WebSocket(dataSource.url);
  6598. ws.binaryType = 'arraybuffer';
  6599. ws.onopen = this._onWebSocketOpen.bind(this);
  6600. ws.onclose = this._onWebSocketClose.bind(this);
  6601. ws.onmessage = this._onWebSocketMessage.bind(this);
  6602. ws.onerror = this._onWebSocketError.bind(this);
  6603. this._status = _loader.LoaderStatus.kConnecting;
  6604. } catch (e) {
  6605. this._status = _loader.LoaderStatus.kError;
  6606. var info = { code: e.code, msg: e.message };
  6607. if (this._onError) {
  6608. this._onError(_loader.LoaderErrors.EXCEPTION, info);
  6609. } else {
  6610. throw new _exception.RuntimeException(info.msg);
  6611. }
  6612. }
  6613. }
  6614. }, {
  6615. key: 'abort',
  6616. value: function abort() {
  6617. var ws = this._ws;
  6618. if (ws && (ws.readyState === 0 || ws.readyState === 1)) {
  6619. // CONNECTING || OPEN
  6620. this._requestAbort = true;
  6621. ws.close();
  6622. }
  6623. this._ws = null;
  6624. this._status = _loader.LoaderStatus.kComplete;
  6625. }
  6626. }, {
  6627. key: '_onWebSocketOpen',
  6628. value: function _onWebSocketOpen(e) {
  6629. this._status = _loader.LoaderStatus.kBuffering;
  6630. }
  6631. }, {
  6632. key: '_onWebSocketClose',
  6633. value: function _onWebSocketClose(e) {
  6634. if (this._requestAbort === true) {
  6635. this._requestAbort = false;
  6636. return;
  6637. }
  6638. this._status = _loader.LoaderStatus.kComplete;
  6639. if (this._onComplete) {
  6640. this._onComplete(0, this._receivedLength - 1);
  6641. }
  6642. }
  6643. }, {
  6644. key: '_onWebSocketMessage',
  6645. value: function _onWebSocketMessage(e) {
  6646. var _this2 = this;
  6647. if (e.data instanceof ArrayBuffer) {
  6648. this._dispatchArrayBuffer(e.data);
  6649. } else if (e.data instanceof Blob) {
  6650. var reader = new FileReader();
  6651. reader.onload = function () {
  6652. _this2._dispatchArrayBuffer(reader.result);
  6653. };
  6654. reader.readAsArrayBuffer(e.data);
  6655. } else {
  6656. this._status = _loader.LoaderStatus.kError;
  6657. var info = { code: -1, msg: 'Unsupported WebSocket message type: ' + e.data.constructor.name };
  6658. if (this._onError) {
  6659. this._onError(_loader.LoaderErrors.EXCEPTION, info);
  6660. } else {
  6661. throw new _exception.RuntimeException(info.msg);
  6662. }
  6663. }
  6664. }
  6665. }, {
  6666. key: '_dispatchArrayBuffer',
  6667. value: function _dispatchArrayBuffer(arraybuffer) {
  6668. var chunk = arraybuffer;
  6669. var byteStart = this._receivedLength;
  6670. this._receivedLength += chunk.byteLength;
  6671. if (this._onDataArrival) {
  6672. this._onDataArrival(chunk, byteStart, this._receivedLength);
  6673. }
  6674. }
  6675. }, {
  6676. key: '_onWebSocketError',
  6677. value: function _onWebSocketError(e) {
  6678. this._status = _loader.LoaderStatus.kError;
  6679. var info = {
  6680. code: e.code,
  6681. msg: e.message
  6682. };
  6683. if (this._onError) {
  6684. this._onError(_loader.LoaderErrors.EXCEPTION, info);
  6685. } else {
  6686. throw new _exception.RuntimeException(info.msg);
  6687. }
  6688. }
  6689. }]);
  6690. return WebSocketLoader;
  6691. }(_loader.BaseLoader);
  6692. exports.default = WebSocketLoader;
  6693. },{"../utils/exception.js":40,"../utils/logger.js":41,"./loader.js":24}],29:[function(_dereq_,module,exports){
  6694. 'use strict';
  6695. Object.defineProperty(exports, "__esModule", {
  6696. value: true
  6697. });
  6698. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6699. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  6700. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6701. var _logger = _dereq_('../utils/logger.js');
  6702. var _logger2 = _interopRequireDefault(_logger);
  6703. var _loader = _dereq_('./loader.js');
  6704. var _exception = _dereq_('../utils/exception.js');
  6705. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6706. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6707. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6708. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
  6709. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6710. *
  6711. * @author zheng qian <xqq@xqq.im>
  6712. *
  6713. * Licensed under the Apache License, Version 2.0 (the "License");
  6714. * you may not use this file except in compliance with the License.
  6715. * You may obtain a copy of the License at
  6716. *
  6717. * http://www.apache.org/licenses/LICENSE-2.0
  6718. *
  6719. * Unless required by applicable law or agreed to in writing, software
  6720. * distributed under the License is distributed on an "AS IS" BASIS,
  6721. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6722. * See the License for the specific language governing permissions and
  6723. * limitations under the License.
  6724. */
  6725. // For FireFox browser which supports `xhr.responseType = 'moz-chunked-arraybuffer'`
  6726. var MozChunkedLoader = function (_BaseLoader) {
  6727. _inherits(MozChunkedLoader, _BaseLoader);
  6728. _createClass(MozChunkedLoader, null, [{
  6729. key: 'isSupported',
  6730. value: function isSupported() {
  6731. try {
  6732. var xhr = new XMLHttpRequest();
  6733. // Firefox 37- requires .open() to be called before setting responseType
  6734. xhr.open('GET', 'https://example.com', true);
  6735. xhr.responseType = 'moz-chunked-arraybuffer';
  6736. return xhr.responseType === 'moz-chunked-arraybuffer';
  6737. } catch (e) {
  6738. _logger2.default.w('MozChunkedLoader', e.message);
  6739. return false;
  6740. }
  6741. }
  6742. }]);
  6743. function MozChunkedLoader(seekHandler, config) {
  6744. _classCallCheck(this, MozChunkedLoader);
  6745. var _this = _possibleConstructorReturn(this, (MozChunkedLoader.__proto__ || Object.getPrototypeOf(MozChunkedLoader)).call(this, 'xhr-moz-chunked-loader'));
  6746. _this.TAG = 'MozChunkedLoader';
  6747. _this._seekHandler = seekHandler;
  6748. _this._config = config;
  6749. _this._needStash = true;
  6750. _this._xhr = null;
  6751. _this._requestAbort = false;
  6752. _this._contentLength = null;
  6753. _this._receivedLength = 0;
  6754. return _this;
  6755. }
  6756. _createClass(MozChunkedLoader, [{
  6757. key: 'destroy',
  6758. value: function destroy() {
  6759. if (this.isWorking()) {
  6760. this.abort();
  6761. }
  6762. if (this._xhr) {
  6763. this._xhr.onreadystatechange = null;
  6764. this._xhr.onprogress = null;
  6765. this._xhr.onloadend = null;
  6766. this._xhr.onerror = null;
  6767. this._xhr = null;
  6768. }
  6769. _get(MozChunkedLoader.prototype.__proto__ || Object.getPrototypeOf(MozChunkedLoader.prototype), 'destroy', this).call(this);
  6770. }
  6771. }, {
  6772. key: 'open',
  6773. value: function open(dataSource, range) {
  6774. this._dataSource = dataSource;
  6775. this._range = range;
  6776. var sourceURL = dataSource.url;
  6777. if (this._config.reuseRedirectedURL && dataSource.redirectedURL != undefined) {
  6778. sourceURL = dataSource.redirectedURL;
  6779. }
  6780. var seekConfig = this._seekHandler.getConfig(sourceURL, range);
  6781. this._requestURL = seekConfig.url;
  6782. var xhr = this._xhr = new XMLHttpRequest();
  6783. xhr.open('GET', seekConfig.url, true);
  6784. xhr.responseType = 'moz-chunked-arraybuffer';
  6785. xhr.onreadystatechange = this._onReadyStateChange.bind(this);
  6786. xhr.onprogress = this._onProgress.bind(this);
  6787. xhr.onloadend = this._onLoadEnd.bind(this);
  6788. xhr.onerror = this._onXhrError.bind(this);
  6789. // cors is auto detected and enabled by xhr
  6790. // withCredentials is disabled by default
  6791. if (dataSource.withCredentials) {
  6792. xhr.withCredentials = true;
  6793. }
  6794. if (_typeof(seekConfig.headers) === 'object') {
  6795. var headers = seekConfig.headers;
  6796. for (var key in headers) {
  6797. if (headers.hasOwnProperty(key)) {
  6798. xhr.setRequestHeader(key, headers[key]);
  6799. }
  6800. }
  6801. }
  6802. this._status = _loader.LoaderStatus.kConnecting;
  6803. xhr.send();
  6804. }
  6805. }, {
  6806. key: 'abort',
  6807. value: function abort() {
  6808. this._requestAbort = true;
  6809. if (this._xhr) {
  6810. this._xhr.abort();
  6811. }
  6812. this._status = _loader.LoaderStatus.kComplete;
  6813. }
  6814. }, {
  6815. key: '_onReadyStateChange',
  6816. value: function _onReadyStateChange(e) {
  6817. var xhr = e.target;
  6818. if (xhr.readyState === 2) {
  6819. // HEADERS_RECEIVED
  6820. if (xhr.responseURL != undefined && xhr.responseURL !== this._requestURL) {
  6821. if (this._onURLRedirect) {
  6822. var redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
  6823. this._onURLRedirect(redirectedURL);
  6824. }
  6825. }
  6826. if (xhr.status !== 0 && (xhr.status < 200 || xhr.status > 299)) {
  6827. this._status = _loader.LoaderStatus.kError;
  6828. if (this._onError) {
  6829. this._onError(_loader.LoaderErrors.HTTP_STATUS_CODE_INVALID, { code: xhr.status, msg: xhr.statusText });
  6830. } else {
  6831. throw new _exception.RuntimeException('MozChunkedLoader: Http code invalid, ' + xhr.status + ' ' + xhr.statusText);
  6832. }
  6833. } else {
  6834. this._status = _loader.LoaderStatus.kBuffering;
  6835. }
  6836. }
  6837. }
  6838. }, {
  6839. key: '_onProgress',
  6840. value: function _onProgress(e) {
  6841. if (this._status === _loader.LoaderStatus.kError) {
  6842. // Ignore error response
  6843. return;
  6844. }
  6845. if (this._contentLength === null) {
  6846. if (e.total !== null && e.total !== 0) {
  6847. this._contentLength = e.total;
  6848. if (this._onContentLengthKnown) {
  6849. this._onContentLengthKnown(this._contentLength);
  6850. }
  6851. }
  6852. }
  6853. var chunk = e.target.response;
  6854. var byteStart = this._range.from + this._receivedLength;
  6855. this._receivedLength += chunk.byteLength;
  6856. if (this._onDataArrival) {
  6857. this._onDataArrival(chunk, byteStart, this._receivedLength);
  6858. }
  6859. }
  6860. }, {
  6861. key: '_onLoadEnd',
  6862. value: function _onLoadEnd(e) {
  6863. if (this._requestAbort === true) {
  6864. this._requestAbort = false;
  6865. return;
  6866. } else if (this._status === _loader.LoaderStatus.kError) {
  6867. return;
  6868. }
  6869. this._status = _loader.LoaderStatus.kComplete;
  6870. if (this._onComplete) {
  6871. this._onComplete(this._range.from, this._range.from + this._receivedLength - 1);
  6872. }
  6873. }
  6874. }, {
  6875. key: '_onXhrError',
  6876. value: function _onXhrError(e) {
  6877. this._status = _loader.LoaderStatus.kError;
  6878. var type = 0;
  6879. var info = null;
  6880. if (this._contentLength && e.loaded < this._contentLength) {
  6881. type = _loader.LoaderErrors.EARLY_EOF;
  6882. info = { code: -1, msg: 'Moz-Chunked stream meet Early-Eof' };
  6883. } else {
  6884. type = _loader.LoaderErrors.EXCEPTION;
  6885. info = { code: -1, msg: e.constructor.name + ' ' + e.type };
  6886. }
  6887. if (this._onError) {
  6888. this._onError(type, info);
  6889. } else {
  6890. throw new _exception.RuntimeException(info.msg);
  6891. }
  6892. }
  6893. }]);
  6894. return MozChunkedLoader;
  6895. }(_loader.BaseLoader);
  6896. exports.default = MozChunkedLoader;
  6897. },{"../utils/exception.js":40,"../utils/logger.js":41,"./loader.js":24}],30:[function(_dereq_,module,exports){
  6898. 'use strict';
  6899. Object.defineProperty(exports, "__esModule", {
  6900. value: true
  6901. });
  6902. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  6903. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  6904. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6905. var _logger = _dereq_('../utils/logger.js');
  6906. var _logger2 = _interopRequireDefault(_logger);
  6907. var _loader = _dereq_('./loader.js');
  6908. var _exception = _dereq_('../utils/exception.js');
  6909. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6910. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  6911. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  6912. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
  6913. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  6914. *
  6915. * @author zheng qian <xqq@xqq.im>
  6916. *
  6917. * Licensed under the Apache License, Version 2.0 (the "License");
  6918. * you may not use this file except in compliance with the License.
  6919. * You may obtain a copy of the License at
  6920. *
  6921. * http://www.apache.org/licenses/LICENSE-2.0
  6922. *
  6923. * Unless required by applicable law or agreed to in writing, software
  6924. * distributed under the License is distributed on an "AS IS" BASIS,
  6925. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  6926. * See the License for the specific language governing permissions and
  6927. * limitations under the License.
  6928. */
  6929. /* Notice: ms-stream may cause IE/Edge browser crash if seek too frequently!!!
  6930. * The browser may crash in wininet.dll. Disable for now.
  6931. *
  6932. * For IE11/Edge browser by microsoft which supports `xhr.responseType = 'ms-stream'`
  6933. * Notice that ms-stream API sucks. The buffer is always expanding along with downloading.
  6934. *
  6935. * We need to abort the xhr if buffer size exceeded limit size (e.g. 16 MiB), then do reconnect.
  6936. * in order to release previous ArrayBuffer to avoid memory leak
  6937. *
  6938. * Otherwise, the ArrayBuffer will increase to a terrible size that equals final file size.
  6939. */
  6940. var MSStreamLoader = function (_BaseLoader) {
  6941. _inherits(MSStreamLoader, _BaseLoader);
  6942. _createClass(MSStreamLoader, null, [{
  6943. key: 'isSupported',
  6944. value: function isSupported() {
  6945. try {
  6946. if (typeof self.MSStream === 'undefined' || typeof self.MSStreamReader === 'undefined') {
  6947. return false;
  6948. }
  6949. var xhr = new XMLHttpRequest();
  6950. xhr.open('GET', 'https://example.com', true);
  6951. xhr.responseType = 'ms-stream';
  6952. return xhr.responseType === 'ms-stream';
  6953. } catch (e) {
  6954. _logger2.default.w('MSStreamLoader', e.message);
  6955. return false;
  6956. }
  6957. }
  6958. }]);
  6959. function MSStreamLoader(seekHandler, config) {
  6960. _classCallCheck(this, MSStreamLoader);
  6961. var _this = _possibleConstructorReturn(this, (MSStreamLoader.__proto__ || Object.getPrototypeOf(MSStreamLoader)).call(this, 'xhr-msstream-loader'));
  6962. _this.TAG = 'MSStreamLoader';
  6963. _this._seekHandler = seekHandler;
  6964. _this._config = config;
  6965. _this._needStash = true;
  6966. _this._xhr = null;
  6967. _this._reader = null; // MSStreamReader
  6968. _this._totalRange = null;
  6969. _this._currentRange = null;
  6970. _this._currentRequestURL = null;
  6971. _this._currentRedirectedURL = null;
  6972. _this._contentLength = null;
  6973. _this._receivedLength = 0;
  6974. _this._bufferLimit = 16 * 1024 * 1024; // 16MB
  6975. _this._lastTimeBufferSize = 0;
  6976. _this._isReconnecting = false;
  6977. return _this;
  6978. }
  6979. _createClass(MSStreamLoader, [{
  6980. key: 'destroy',
  6981. value: function destroy() {
  6982. if (this.isWorking()) {
  6983. this.abort();
  6984. }
  6985. if (this._reader) {
  6986. this._reader.onprogress = null;
  6987. this._reader.onload = null;
  6988. this._reader.onerror = null;
  6989. this._reader = null;
  6990. }
  6991. if (this._xhr) {
  6992. this._xhr.onreadystatechange = null;
  6993. this._xhr = null;
  6994. }
  6995. _get(MSStreamLoader.prototype.__proto__ || Object.getPrototypeOf(MSStreamLoader.prototype), 'destroy', this).call(this);
  6996. }
  6997. }, {
  6998. key: 'open',
  6999. value: function open(dataSource, range) {
  7000. this._internalOpen(dataSource, range, false);
  7001. }
  7002. }, {
  7003. key: '_internalOpen',
  7004. value: function _internalOpen(dataSource, range, isSubrange) {
  7005. this._dataSource = dataSource;
  7006. if (!isSubrange) {
  7007. this._totalRange = range;
  7008. } else {
  7009. this._currentRange = range;
  7010. }
  7011. var sourceURL = dataSource.url;
  7012. if (this._config.reuseRedirectedURL) {
  7013. if (this._currentRedirectedURL != undefined) {
  7014. sourceURL = this._currentRedirectedURL;
  7015. } else if (dataSource.redirectedURL != undefined) {
  7016. sourceURL = dataSource.redirectedURL;
  7017. }
  7018. }
  7019. var seekConfig = this._seekHandler.getConfig(sourceURL, range);
  7020. this._currentRequestURL = seekConfig.url;
  7021. var reader = this._reader = new self.MSStreamReader();
  7022. reader.onprogress = this._msrOnProgress.bind(this);
  7023. reader.onload = this._msrOnLoad.bind(this);
  7024. reader.onerror = this._msrOnError.bind(this);
  7025. var xhr = this._xhr = new XMLHttpRequest();
  7026. xhr.open('GET', seekConfig.url, true);
  7027. xhr.responseType = 'ms-stream';
  7028. xhr.onreadystatechange = this._xhrOnReadyStateChange.bind(this);
  7029. xhr.onerror = this._xhrOnError.bind(this);
  7030. if (dataSource.withCredentials) {
  7031. xhr.withCredentials = true;
  7032. }
  7033. if (_typeof(seekConfig.headers) === 'object') {
  7034. var headers = seekConfig.headers;
  7035. for (var key in headers) {
  7036. if (headers.hasOwnProperty(key)) {
  7037. xhr.setRequestHeader(key, headers[key]);
  7038. }
  7039. }
  7040. }
  7041. if (this._isReconnecting) {
  7042. this._isReconnecting = false;
  7043. } else {
  7044. this._status = _loader.LoaderStatus.kConnecting;
  7045. }
  7046. xhr.send();
  7047. }
  7048. }, {
  7049. key: 'abort',
  7050. value: function abort() {
  7051. this._internalAbort();
  7052. this._status = _loader.LoaderStatus.kComplete;
  7053. }
  7054. }, {
  7055. key: '_internalAbort',
  7056. value: function _internalAbort() {
  7057. if (this._reader) {
  7058. if (this._reader.readyState === 1) {
  7059. // LOADING
  7060. this._reader.abort();
  7061. }
  7062. this._reader.onprogress = null;
  7063. this._reader.onload = null;
  7064. this._reader.onerror = null;
  7065. this._reader = null;
  7066. }
  7067. if (this._xhr) {
  7068. this._xhr.abort();
  7069. this._xhr.onreadystatechange = null;
  7070. this._xhr = null;
  7071. }
  7072. }
  7073. }, {
  7074. key: '_xhrOnReadyStateChange',
  7075. value: function _xhrOnReadyStateChange(e) {
  7076. var xhr = e.target;
  7077. if (xhr.readyState === 2) {
  7078. // HEADERS_RECEIVED
  7079. if (xhr.status >= 200 && xhr.status <= 299) {
  7080. this._status = _loader.LoaderStatus.kBuffering;
  7081. if (xhr.responseURL != undefined) {
  7082. var redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
  7083. if (xhr.responseURL !== this._currentRequestURL && redirectedURL !== this._currentRedirectedURL) {
  7084. this._currentRedirectedURL = redirectedURL;
  7085. if (this._onURLRedirect) {
  7086. this._onURLRedirect(redirectedURL);
  7087. }
  7088. }
  7089. }
  7090. var lengthHeader = xhr.getResponseHeader('Content-Length');
  7091. if (lengthHeader != null && this._contentLength == null) {
  7092. var length = parseInt(lengthHeader);
  7093. if (length > 0) {
  7094. this._contentLength = length;
  7095. if (this._onContentLengthKnown) {
  7096. this._onContentLengthKnown(this._contentLength);
  7097. }
  7098. }
  7099. }
  7100. } else {
  7101. this._status = _loader.LoaderStatus.kError;
  7102. if (this._onError) {
  7103. this._onError(_loader.LoaderErrors.HTTP_STATUS_CODE_INVALID, { code: xhr.status, msg: xhr.statusText });
  7104. } else {
  7105. throw new _exception.RuntimeException('MSStreamLoader: Http code invalid, ' + xhr.status + ' ' + xhr.statusText);
  7106. }
  7107. }
  7108. } else if (xhr.readyState === 3) {
  7109. // LOADING
  7110. if (xhr.status >= 200 && xhr.status <= 299) {
  7111. this._status = _loader.LoaderStatus.kBuffering;
  7112. var msstream = xhr.response;
  7113. this._reader.readAsArrayBuffer(msstream);
  7114. }
  7115. }
  7116. }
  7117. }, {
  7118. key: '_xhrOnError',
  7119. value: function _xhrOnError(e) {
  7120. this._status = _loader.LoaderStatus.kError;
  7121. var type = _loader.LoaderErrors.EXCEPTION;
  7122. var info = { code: -1, msg: e.constructor.name + ' ' + e.type };
  7123. if (this._onError) {
  7124. this._onError(type, info);
  7125. } else {
  7126. throw new _exception.RuntimeException(info.msg);
  7127. }
  7128. }
  7129. }, {
  7130. key: '_msrOnProgress',
  7131. value: function _msrOnProgress(e) {
  7132. var reader = e.target;
  7133. var bigbuffer = reader.result;
  7134. if (bigbuffer == null) {
  7135. // result may be null, workaround for buggy M$
  7136. this._doReconnectIfNeeded();
  7137. return;
  7138. }
  7139. var slice = bigbuffer.slice(this._lastTimeBufferSize);
  7140. this._lastTimeBufferSize = bigbuffer.byteLength;
  7141. var byteStart = this._totalRange.from + this._receivedLength;
  7142. this._receivedLength += slice.byteLength;
  7143. if (this._onDataArrival) {
  7144. this._onDataArrival(slice, byteStart, this._receivedLength);
  7145. }
  7146. if (bigbuffer.byteLength >= this._bufferLimit) {
  7147. _logger2.default.v(this.TAG, 'MSStream buffer exceeded max size near ' + (byteStart + slice.byteLength) + ', reconnecting...');
  7148. this._doReconnectIfNeeded();
  7149. }
  7150. }
  7151. }, {
  7152. key: '_doReconnectIfNeeded',
  7153. value: function _doReconnectIfNeeded() {
  7154. if (this._contentLength == null || this._receivedLength < this._contentLength) {
  7155. this._isReconnecting = true;
  7156. this._lastTimeBufferSize = 0;
  7157. this._internalAbort();
  7158. var range = {
  7159. from: this._totalRange.from + this._receivedLength,
  7160. to: -1
  7161. };
  7162. this._internalOpen(this._dataSource, range, true);
  7163. }
  7164. }
  7165. }, {
  7166. key: '_msrOnLoad',
  7167. value: function _msrOnLoad(e) {
  7168. // actually it is onComplete event
  7169. this._status = _loader.LoaderStatus.kComplete;
  7170. if (this._onComplete) {
  7171. this._onComplete(this._totalRange.from, this._totalRange.from + this._receivedLength - 1);
  7172. }
  7173. }
  7174. }, {
  7175. key: '_msrOnError',
  7176. value: function _msrOnError(e) {
  7177. this._status = _loader.LoaderStatus.kError;
  7178. var type = 0;
  7179. var info = null;
  7180. if (this._contentLength && this._receivedLength < this._contentLength) {
  7181. type = _loader.LoaderErrors.EARLY_EOF;
  7182. info = { code: -1, msg: 'MSStream meet Early-Eof' };
  7183. } else {
  7184. type = _loader.LoaderErrors.EARLY_EOF;
  7185. info = { code: -1, msg: e.constructor.name + ' ' + e.type };
  7186. }
  7187. if (this._onError) {
  7188. this._onError(type, info);
  7189. } else {
  7190. throw new _exception.RuntimeException(info.msg);
  7191. }
  7192. }
  7193. }]);
  7194. return MSStreamLoader;
  7195. }(_loader.BaseLoader);
  7196. exports.default = MSStreamLoader;
  7197. },{"../utils/exception.js":40,"../utils/logger.js":41,"./loader.js":24}],31:[function(_dereq_,module,exports){
  7198. 'use strict';
  7199. Object.defineProperty(exports, "__esModule", {
  7200. value: true
  7201. });
  7202. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  7203. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  7204. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  7205. var _logger = _dereq_('../utils/logger.js');
  7206. var _logger2 = _interopRequireDefault(_logger);
  7207. var _speedSampler = _dereq_('./speed-sampler.js');
  7208. var _speedSampler2 = _interopRequireDefault(_speedSampler);
  7209. var _loader = _dereq_('./loader.js');
  7210. var _exception = _dereq_('../utils/exception.js');
  7211. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7212. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7213. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  7214. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
  7215. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  7216. *
  7217. * @author zheng qian <xqq@xqq.im>
  7218. *
  7219. * Licensed under the Apache License, Version 2.0 (the "License");
  7220. * you may not use this file except in compliance with the License.
  7221. * You may obtain a copy of the License at
  7222. *
  7223. * http://www.apache.org/licenses/LICENSE-2.0
  7224. *
  7225. * Unless required by applicable law or agreed to in writing, software
  7226. * distributed under the License is distributed on an "AS IS" BASIS,
  7227. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7228. * See the License for the specific language governing permissions and
  7229. * limitations under the License.
  7230. */
  7231. // Universal IO Loader, implemented by adding Range header in xhr's request header
  7232. var RangeLoader = function (_BaseLoader) {
  7233. _inherits(RangeLoader, _BaseLoader);
  7234. _createClass(RangeLoader, null, [{
  7235. key: 'isSupported',
  7236. value: function isSupported() {
  7237. try {
  7238. var xhr = new XMLHttpRequest();
  7239. xhr.open('GET', 'https://example.com', true);
  7240. xhr.responseType = 'arraybuffer';
  7241. return xhr.responseType === 'arraybuffer';
  7242. } catch (e) {
  7243. _logger2.default.w('RangeLoader', e.message);
  7244. return false;
  7245. }
  7246. }
  7247. }]);
  7248. function RangeLoader(seekHandler, config) {
  7249. _classCallCheck(this, RangeLoader);
  7250. var _this = _possibleConstructorReturn(this, (RangeLoader.__proto__ || Object.getPrototypeOf(RangeLoader)).call(this, 'xhr-range-loader'));
  7251. _this.TAG = 'RangeLoader';
  7252. _this._seekHandler = seekHandler;
  7253. _this._config = config;
  7254. _this._needStash = false;
  7255. _this._chunkSizeKBList = [128, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 5120, 6144, 7168, 8192];
  7256. _this._currentChunkSizeKB = 384;
  7257. _this._currentSpeedNormalized = 0;
  7258. _this._zeroSpeedChunkCount = 0;
  7259. _this._xhr = null;
  7260. _this._speedSampler = new _speedSampler2.default();
  7261. _this._requestAbort = false;
  7262. _this._waitForTotalLength = false;
  7263. _this._totalLengthReceived = false;
  7264. _this._currentRequestURL = null;
  7265. _this._currentRedirectedURL = null;
  7266. _this._currentRequestRange = null;
  7267. _this._totalLength = null; // size of the entire file
  7268. _this._contentLength = null; // Content-Length of entire request range
  7269. _this._receivedLength = 0; // total received bytes
  7270. _this._lastTimeLoaded = 0; // received bytes of current request sub-range
  7271. return _this;
  7272. }
  7273. _createClass(RangeLoader, [{
  7274. key: 'destroy',
  7275. value: function destroy() {
  7276. if (this.isWorking()) {
  7277. this.abort();
  7278. }
  7279. if (this._xhr) {
  7280. this._xhr.onreadystatechange = null;
  7281. this._xhr.onprogress = null;
  7282. this._xhr.onload = null;
  7283. this._xhr.onerror = null;
  7284. this._xhr = null;
  7285. }
  7286. _get(RangeLoader.prototype.__proto__ || Object.getPrototypeOf(RangeLoader.prototype), 'destroy', this).call(this);
  7287. }
  7288. }, {
  7289. key: 'open',
  7290. value: function open(dataSource, range) {
  7291. this._dataSource = dataSource;
  7292. this._range = range;
  7293. this._status = _loader.LoaderStatus.kConnecting;
  7294. var useRefTotalLength = false;
  7295. if (this._dataSource.filesize != undefined && this._dataSource.filesize !== 0) {
  7296. useRefTotalLength = true;
  7297. this._totalLength = this._dataSource.filesize;
  7298. }
  7299. if (!this._totalLengthReceived && !useRefTotalLength) {
  7300. // We need total filesize
  7301. this._waitForTotalLength = true;
  7302. this._internalOpen(this._dataSource, { from: 0, to: -1 });
  7303. } else {
  7304. // We have filesize, start loading
  7305. this._openSubRange();
  7306. }
  7307. }
  7308. }, {
  7309. key: '_openSubRange',
  7310. value: function _openSubRange() {
  7311. var chunkSize = this._currentChunkSizeKB * 1024;
  7312. var from = this._range.from + this._receivedLength;
  7313. var to = from + chunkSize;
  7314. if (this._contentLength != null) {
  7315. if (to - this._range.from >= this._contentLength) {
  7316. to = this._range.from + this._contentLength - 1;
  7317. }
  7318. }
  7319. this._currentRequestRange = { from: from, to: to };
  7320. this._internalOpen(this._dataSource, this._currentRequestRange);
  7321. }
  7322. }, {
  7323. key: '_internalOpen',
  7324. value: function _internalOpen(dataSource, range) {
  7325. this._lastTimeLoaded = 0;
  7326. var sourceURL = dataSource.url;
  7327. if (this._config.reuseRedirectedURL) {
  7328. if (this._currentRedirectedURL != undefined) {
  7329. sourceURL = this._currentRedirectedURL;
  7330. } else if (dataSource.redirectedURL != undefined) {
  7331. sourceURL = dataSource.redirectedURL;
  7332. }
  7333. }
  7334. var seekConfig = this._seekHandler.getConfig(sourceURL, range);
  7335. this._currentRequestURL = seekConfig.url;
  7336. var xhr = this._xhr = new XMLHttpRequest();
  7337. xhr.open('GET', seekConfig.url, true);
  7338. xhr.responseType = 'arraybuffer';
  7339. xhr.onreadystatechange = this._onReadyStateChange.bind(this);
  7340. xhr.onprogress = this._onProgress.bind(this);
  7341. xhr.onload = this._onLoad.bind(this);
  7342. xhr.onerror = this._onXhrError.bind(this);
  7343. if (dataSource.withCredentials) {
  7344. xhr.withCredentials = true;
  7345. }
  7346. if (_typeof(seekConfig.headers) === 'object') {
  7347. var headers = seekConfig.headers;
  7348. for (var key in headers) {
  7349. if (headers.hasOwnProperty(key)) {
  7350. xhr.setRequestHeader(key, headers[key]);
  7351. }
  7352. }
  7353. }
  7354. xhr.send();
  7355. }
  7356. }, {
  7357. key: 'abort',
  7358. value: function abort() {
  7359. this._requestAbort = true;
  7360. this._internalAbort();
  7361. this._status = _loader.LoaderStatus.kComplete;
  7362. }
  7363. }, {
  7364. key: '_internalAbort',
  7365. value: function _internalAbort() {
  7366. if (this._xhr) {
  7367. this._xhr.onreadystatechange = null;
  7368. this._xhr.onprogress = null;
  7369. this._xhr.onload = null;
  7370. this._xhr.onerror = null;
  7371. this._xhr.abort();
  7372. this._xhr = null;
  7373. }
  7374. }
  7375. }, {
  7376. key: '_onReadyStateChange',
  7377. value: function _onReadyStateChange(e) {
  7378. var xhr = e.target;
  7379. if (xhr.readyState === 2) {
  7380. // HEADERS_RECEIVED
  7381. if (xhr.responseURL != undefined) {
  7382. // if the browser support this property
  7383. var redirectedURL = this._seekHandler.removeURLParameters(xhr.responseURL);
  7384. if (xhr.responseURL !== this._currentRequestURL && redirectedURL !== this._currentRedirectedURL) {
  7385. this._currentRedirectedURL = redirectedURL;
  7386. if (this._onURLRedirect) {
  7387. this._onURLRedirect(redirectedURL);
  7388. }
  7389. }
  7390. }
  7391. if (xhr.status >= 200 && xhr.status <= 299) {
  7392. if (this._waitForTotalLength) {
  7393. return;
  7394. }
  7395. this._status = _loader.LoaderStatus.kBuffering;
  7396. } else {
  7397. this._status = _loader.LoaderStatus.kError;
  7398. if (this._onError) {
  7399. this._onError(_loader.LoaderErrors.HTTP_STATUS_CODE_INVALID, { code: xhr.status, msg: xhr.statusText });
  7400. } else {
  7401. throw new _exception.RuntimeException('RangeLoader: Http code invalid, ' + xhr.status + ' ' + xhr.statusText);
  7402. }
  7403. }
  7404. }
  7405. }
  7406. }, {
  7407. key: '_onProgress',
  7408. value: function _onProgress(e) {
  7409. if (this._status === _loader.LoaderStatus.kError) {
  7410. // Ignore error response
  7411. return;
  7412. }
  7413. if (this._contentLength === null) {
  7414. var openNextRange = false;
  7415. if (this._waitForTotalLength) {
  7416. this._waitForTotalLength = false;
  7417. this._totalLengthReceived = true;
  7418. openNextRange = true;
  7419. var total = e.total;
  7420. this._internalAbort();
  7421. if (total != null & total !== 0) {
  7422. this._totalLength = total;
  7423. }
  7424. }
  7425. // calculate currrent request range's contentLength
  7426. if (this._range.to === -1) {
  7427. this._contentLength = this._totalLength - this._range.from;
  7428. } else {
  7429. // to !== -1
  7430. this._contentLength = this._range.to - this._range.from + 1;
  7431. }
  7432. if (openNextRange) {
  7433. this._openSubRange();
  7434. return;
  7435. }
  7436. if (this._onContentLengthKnown) {
  7437. this._onContentLengthKnown(this._contentLength);
  7438. }
  7439. }
  7440. var delta = e.loaded - this._lastTimeLoaded;
  7441. this._lastTimeLoaded = e.loaded;
  7442. this._speedSampler.addBytes(delta);
  7443. }
  7444. }, {
  7445. key: '_normalizeSpeed',
  7446. value: function _normalizeSpeed(input) {
  7447. var list = this._chunkSizeKBList;
  7448. var last = list.length - 1;
  7449. var mid = 0;
  7450. var lbound = 0;
  7451. var ubound = last;
  7452. if (input < list[0]) {
  7453. return list[0];
  7454. }
  7455. while (lbound <= ubound) {
  7456. mid = lbound + Math.floor((ubound - lbound) / 2);
  7457. if (mid === last || input >= list[mid] && input < list[mid + 1]) {
  7458. return list[mid];
  7459. } else if (list[mid] < input) {
  7460. lbound = mid + 1;
  7461. } else {
  7462. ubound = mid - 1;
  7463. }
  7464. }
  7465. }
  7466. }, {
  7467. key: '_onLoad',
  7468. value: function _onLoad(e) {
  7469. if (this._status === _loader.LoaderStatus.kError) {
  7470. // Ignore error response
  7471. return;
  7472. }
  7473. if (this._waitForTotalLength) {
  7474. this._waitForTotalLength = false;
  7475. return;
  7476. }
  7477. this._lastTimeLoaded = 0;
  7478. var KBps = this._speedSampler.lastSecondKBps;
  7479. if (KBps === 0) {
  7480. this._zeroSpeedChunkCount++;
  7481. if (this._zeroSpeedChunkCount >= 3) {
  7482. // Try get currentKBps after 3 chunks
  7483. KBps = this._speedSampler.currentKBps;
  7484. }
  7485. }
  7486. if (KBps !== 0) {
  7487. var normalized = this._normalizeSpeed(KBps);
  7488. if (this._currentSpeedNormalized !== normalized) {
  7489. this._currentSpeedNormalized = normalized;
  7490. this._currentChunkSizeKB = normalized;
  7491. }
  7492. }
  7493. var chunk = e.target.response;
  7494. var byteStart = this._range.from + this._receivedLength;
  7495. this._receivedLength += chunk.byteLength;
  7496. var reportComplete = false;
  7497. if (this._contentLength != null && this._receivedLength < this._contentLength) {
  7498. // continue load next chunk
  7499. this._openSubRange();
  7500. } else {
  7501. reportComplete = true;
  7502. }
  7503. // dispatch received chunk
  7504. if (this._onDataArrival) {
  7505. this._onDataArrival(chunk, byteStart, this._receivedLength);
  7506. }
  7507. if (reportComplete) {
  7508. this._status = _loader.LoaderStatus.kComplete;
  7509. if (this._onComplete) {
  7510. this._onComplete(this._range.from, this._range.from + this._receivedLength - 1);
  7511. }
  7512. }
  7513. }
  7514. }, {
  7515. key: '_onXhrError',
  7516. value: function _onXhrError(e) {
  7517. this._status = _loader.LoaderStatus.kError;
  7518. var type = 0;
  7519. var info = null;
  7520. if (this._contentLength && this._receivedLength > 0 && this._receivedLength < this._contentLength) {
  7521. type = _loader.LoaderErrors.EARLY_EOF;
  7522. info = { code: -1, msg: 'RangeLoader meet Early-Eof' };
  7523. } else {
  7524. type = _loader.LoaderErrors.EXCEPTION;
  7525. info = { code: -1, msg: e.constructor.name + ' ' + e.type };
  7526. }
  7527. if (this._onError) {
  7528. this._onError(type, info);
  7529. } else {
  7530. throw new _exception.RuntimeException(info.msg);
  7531. }
  7532. }
  7533. }, {
  7534. key: 'currentSpeed',
  7535. get: function get() {
  7536. return this._speedSampler.lastSecondKBps;
  7537. }
  7538. }]);
  7539. return RangeLoader;
  7540. }(_loader.BaseLoader);
  7541. exports.default = RangeLoader;
  7542. },{"../utils/exception.js":40,"../utils/logger.js":41,"./loader.js":24,"./speed-sampler.js":27}],32:[function(_dereq_,module,exports){
  7543. 'use strict';
  7544. Object.defineProperty(exports, "__esModule", {
  7545. value: true
  7546. });
  7547. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  7548. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  7549. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  7550. *
  7551. * @author zheng qian <xqq@xqq.im>
  7552. *
  7553. * Licensed under the Apache License, Version 2.0 (the "License");
  7554. * you may not use this file except in compliance with the License.
  7555. * You may obtain a copy of the License at
  7556. *
  7557. * http://www.apache.org/licenses/LICENSE-2.0
  7558. *
  7559. * Unless required by applicable law or agreed to in writing, software
  7560. * distributed under the License is distributed on an "AS IS" BASIS,
  7561. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  7562. * See the License for the specific language governing permissions and
  7563. * limitations under the License.
  7564. */
  7565. var _events = _dereq_('events');
  7566. var _events2 = _interopRequireDefault(_events);
  7567. var _logger = _dereq_('../utils/logger.js');
  7568. var _logger2 = _interopRequireDefault(_logger);
  7569. var _browser = _dereq_('../utils/browser.js');
  7570. var _browser2 = _interopRequireDefault(_browser);
  7571. var _playerEvents = _dereq_('./player-events.js');
  7572. var _playerEvents2 = _interopRequireDefault(_playerEvents);
  7573. var _transmuxer = _dereq_('../core/transmuxer.js');
  7574. var _transmuxer2 = _interopRequireDefault(_transmuxer);
  7575. var _transmuxingEvents = _dereq_('../core/transmuxing-events.js');
  7576. var _transmuxingEvents2 = _interopRequireDefault(_transmuxingEvents);
  7577. var _mseController = _dereq_('../core/mse-controller.js');
  7578. var _mseController2 = _interopRequireDefault(_mseController);
  7579. var _mseEvents = _dereq_('../core/mse-events.js');
  7580. var _mseEvents2 = _interopRequireDefault(_mseEvents);
  7581. var _playerErrors = _dereq_('./player-errors.js');
  7582. var _config = _dereq_('../config.js');
  7583. var _exception = _dereq_('../utils/exception.js');
  7584. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7585. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  7586. var FlvPlayer = function () {
  7587. function FlvPlayer(mediaDataSource, config) {
  7588. _classCallCheck(this, FlvPlayer);
  7589. this.TAG = 'FlvPlayer';
  7590. this._type = 'FlvPlayer';
  7591. this._emitter = new _events2.default();
  7592. this._config = (0, _config.createDefaultConfig)();
  7593. if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object') {
  7594. Object.assign(this._config, config);
  7595. }
  7596. if (mediaDataSource.type.toLowerCase() !== 'flv') {
  7597. throw new _exception.InvalidArgumentException('FlvPlayer requires an flv MediaDataSource input!');
  7598. }
  7599. if (mediaDataSource.isLive === true) {
  7600. this._config.isLive = true;
  7601. }
  7602. this.e = {
  7603. onvLoadedMetadata: this._onvLoadedMetadata.bind(this),
  7604. onvSeeking: this._onvSeeking.bind(this),
  7605. onvCanPlay: this._onvCanPlay.bind(this),
  7606. onvStalled: this._onvStalled.bind(this),
  7607. onvProgress: this._onvProgress.bind(this)
  7608. };
  7609. if (self.performance && self.performance.now) {
  7610. this._now = self.performance.now.bind(self.performance);
  7611. } else {
  7612. this._now = Date.now;
  7613. }
  7614. this._pendingSeekTime = null; // in seconds
  7615. this._requestSetTime = false;
  7616. this._seekpointRecord = null;
  7617. this._progressChecker = null;
  7618. this._mediaDataSource = mediaDataSource;
  7619. this._mediaElement = null;
  7620. this._msectl = null;
  7621. this._transmuxer = null;
  7622. this._mseSourceOpened = false;
  7623. this._hasPendingLoad = false;
  7624. this._receivedCanPlay = false;
  7625. this._mediaInfo = null;
  7626. this._statisticsInfo = null;
  7627. var chromeNeedIDRFix = _browser2.default.chrome && (_browser2.default.version.major < 50 || _browser2.default.version.major === 50 && _browser2.default.version.build < 2661);
  7628. this._alwaysSeekKeyframe = chromeNeedIDRFix || _browser2.default.msedge || _browser2.default.msie ? true : false;
  7629. if (this._alwaysSeekKeyframe) {
  7630. this._config.accurateSeek = false;
  7631. }
  7632. }
  7633. _createClass(FlvPlayer, [{
  7634. key: 'destroy',
  7635. value: function destroy() {
  7636. if (this._progressChecker != null) {
  7637. window.clearInterval(this._progressChecker);
  7638. this._progressChecker = null;
  7639. }
  7640. if (this._transmuxer) {
  7641. this.unload();
  7642. }
  7643. if (this._mediaElement) {
  7644. this.detachMediaElement();
  7645. }
  7646. this.e = null;
  7647. this._mediaDataSource = null;
  7648. this._emitter.removeAllListeners();
  7649. this._emitter = null;
  7650. }
  7651. }, {
  7652. key: 'on',
  7653. value: function on(event, listener) {
  7654. var _this = this;
  7655. if (event === _playerEvents2.default.MEDIA_INFO) {
  7656. if (this._mediaInfo != null) {
  7657. Promise.resolve().then(function () {
  7658. _this._emitter.emit(_playerEvents2.default.MEDIA_INFO, _this.mediaInfo);
  7659. });
  7660. }
  7661. } else if (event === _playerEvents2.default.STATISTICS_INFO) {
  7662. if (this._statisticsInfo != null) {
  7663. Promise.resolve().then(function () {
  7664. _this._emitter.emit(_playerEvents2.default.STATISTICS_INFO, _this.statisticsInfo);
  7665. });
  7666. }
  7667. }
  7668. this._emitter.addListener(event, listener);
  7669. }
  7670. }, {
  7671. key: 'off',
  7672. value: function off(event, listener) {
  7673. this._emitter.removeListener(event, listener);
  7674. }
  7675. }, {
  7676. key: 'attachMediaElement',
  7677. value: function attachMediaElement(mediaElement) {
  7678. var _this2 = this;
  7679. this._mediaElement = mediaElement;
  7680. mediaElement.addEventListener('loadedmetadata', this.e.onvLoadedMetadata);
  7681. mediaElement.addEventListener('seeking', this.e.onvSeeking);
  7682. mediaElement.addEventListener('canplay', this.e.onvCanPlay);
  7683. mediaElement.addEventListener('stalled', this.e.onvStalled);
  7684. mediaElement.addEventListener('progress', this.e.onvProgress);
  7685. this._msectl = new _mseController2.default(this._config);
  7686. this._msectl.on(_mseEvents2.default.UPDATE_END, this._onmseUpdateEnd.bind(this));
  7687. this._msectl.on(_mseEvents2.default.BUFFER_FULL, this._onmseBufferFull.bind(this));
  7688. this._msectl.on(_mseEvents2.default.SOURCE_OPEN, function () {
  7689. _this2._mseSourceOpened = true;
  7690. if (_this2._hasPendingLoad) {
  7691. _this2._hasPendingLoad = false;
  7692. _this2.load();
  7693. }
  7694. });
  7695. this._msectl.on(_mseEvents2.default.ERROR, function (info) {
  7696. _this2._emitter.emit(_playerEvents2.default.ERROR, _playerErrors.ErrorTypes.MEDIA_ERROR, _playerErrors.ErrorDetails.MEDIA_MSE_ERROR, info);
  7697. });
  7698. this._msectl.attachMediaElement(mediaElement);
  7699. if (this._pendingSeekTime != null) {
  7700. try {
  7701. mediaElement.currentTime = this._pendingSeekTime;
  7702. this._pendingSeekTime = null;
  7703. } catch (e) {
  7704. // IE11 may throw InvalidStateError if readyState === 0
  7705. // We can defer set currentTime operation after loadedmetadata
  7706. }
  7707. }
  7708. }
  7709. }, {
  7710. key: 'detachMediaElement',
  7711. value: function detachMediaElement() {
  7712. if (this._mediaElement) {
  7713. this._msectl.detachMediaElement();
  7714. this._mediaElement.removeEventListener('loadedmetadata', this.e.onvLoadedMetadata);
  7715. this._mediaElement.removeEventListener('seeking', this.e.onvSeeking);
  7716. this._mediaElement.removeEventListener('canplay', this.e.onvCanPlay);
  7717. this._mediaElement.removeEventListener('stalled', this.e.onvStalled);
  7718. this._mediaElement.removeEventListener('progress', this.e.onvProgress);
  7719. this._mediaElement = null;
  7720. }
  7721. if (this._msectl) {
  7722. this._msectl.destroy();
  7723. this._msectl = null;
  7724. }
  7725. }
  7726. }, {
  7727. key: 'load',
  7728. value: function load() {
  7729. var _this3 = this;
  7730. if (!this._mediaElement) {
  7731. throw new _exception.IllegalStateException('HTMLMediaElement must be attached before load()!');
  7732. }
  7733. if (this._transmuxer) {
  7734. throw new _exception.IllegalStateException('FlvPlayer.load() has been called, please call unload() first!');
  7735. }
  7736. if (this._hasPendingLoad) {
  7737. return;
  7738. }
  7739. if (this._config.deferLoadAfterSourceOpen && this._mseSourceOpened === false) {
  7740. this._hasPendingLoad = true;
  7741. return;
  7742. }
  7743. if (this._mediaElement.readyState > 0) {
  7744. this._requestSetTime = true;
  7745. // IE11 may throw InvalidStateError if readyState === 0
  7746. this._mediaElement.currentTime = 0;
  7747. }
  7748. this._transmuxer = new _transmuxer2.default(this._mediaDataSource, this._config);
  7749. this._transmuxer.on(_transmuxingEvents2.default.INIT_SEGMENT, function (type, is) {
  7750. _this3._msectl.appendInitSegment(is);
  7751. });
  7752. this._transmuxer.on(_transmuxingEvents2.default.MEDIA_SEGMENT, function (type, ms) {
  7753. _this3._msectl.appendMediaSegment(ms);
  7754. // lazyLoad check
  7755. if (_this3._config.lazyLoad && !_this3._config.isLive) {
  7756. var currentTime = _this3._mediaElement.currentTime;
  7757. if (ms.info.endDts >= (currentTime + _this3._config.lazyLoadMaxDuration) * 1000) {
  7758. if (_this3._progressChecker == null) {
  7759. _logger2.default.v(_this3.TAG, 'Maximum buffering duration exceeded, suspend transmuxing task');
  7760. _this3._suspendTransmuxer();
  7761. }
  7762. }
  7763. }
  7764. });
  7765. this._transmuxer.on(_transmuxingEvents2.default.LOADING_COMPLETE, function () {
  7766. _this3._msectl.endOfStream();
  7767. _this3._emitter.emit(_playerEvents2.default.LOADING_COMPLETE);
  7768. });
  7769. this._transmuxer.on(_transmuxingEvents2.default.RECOVERED_EARLY_EOF, function () {
  7770. _this3._emitter.emit(_playerEvents2.default.RECOVERED_EARLY_EOF);
  7771. });
  7772. this._transmuxer.on(_transmuxingEvents2.default.IO_ERROR, function (detail, info) {
  7773. _this3._emitter.emit(_playerEvents2.default.ERROR, _playerErrors.ErrorTypes.NETWORK_ERROR, detail, info);
  7774. });
  7775. this._transmuxer.on(_transmuxingEvents2.default.DEMUX_ERROR, function (detail, info) {
  7776. _this3._emitter.emit(_playerEvents2.default.ERROR, _playerErrors.ErrorTypes.MEDIA_ERROR, detail, { code: -1, msg: info });
  7777. });
  7778. this._transmuxer.on(_transmuxingEvents2.default.MEDIA_INFO, function (mediaInfo) {
  7779. _this3._mediaInfo = mediaInfo;
  7780. _this3._emitter.emit(_playerEvents2.default.MEDIA_INFO, Object.assign({}, mediaInfo));
  7781. });
  7782. this._transmuxer.on(_transmuxingEvents2.default.METADATA_ARRIVED, function (metadata) {
  7783. _this3._emitter.emit(_playerEvents2.default.METADATA_ARRIVED, metadata);
  7784. });
  7785. this._transmuxer.on(_transmuxingEvents2.default.STATISTICS_INFO, function (statInfo) {
  7786. _this3._statisticsInfo = _this3._fillStatisticsInfo(statInfo);
  7787. _this3._emitter.emit(_playerEvents2.default.STATISTICS_INFO, Object.assign({}, _this3._statisticsInfo));
  7788. });
  7789. this._transmuxer.on(_transmuxingEvents2.default.RECOMMEND_SEEKPOINT, function (milliseconds) {
  7790. if (_this3._mediaElement && !_this3._config.accurateSeek) {
  7791. _this3._requestSetTime = true;
  7792. _this3._mediaElement.currentTime = milliseconds / 1000;
  7793. }
  7794. });
  7795. this._transmuxer.open();
  7796. }
  7797. }, {
  7798. key: 'unload',
  7799. value: function unload() {
  7800. if (this._mediaElement) {
  7801. this._mediaElement.pause();
  7802. }
  7803. if (this._msectl) {
  7804. this._msectl.seek(0);
  7805. }
  7806. if (this._transmuxer) {
  7807. this._transmuxer.close();
  7808. this._transmuxer.destroy();
  7809. this._transmuxer = null;
  7810. }
  7811. }
  7812. }, {
  7813. key: 'play',
  7814. value: function play() {
  7815. return this._mediaElement.play();
  7816. }
  7817. }, {
  7818. key: 'pause',
  7819. value: function pause() {
  7820. this._mediaElement.pause();
  7821. }
  7822. }, {
  7823. key: '_fillStatisticsInfo',
  7824. value: function _fillStatisticsInfo(statInfo) {
  7825. statInfo.playerType = this._type;
  7826. if (!(this._mediaElement instanceof HTMLVideoElement)) {
  7827. return statInfo;
  7828. }
  7829. var hasQualityInfo = true;
  7830. var decoded = 0;
  7831. var dropped = 0;
  7832. if (this._mediaElement.getVideoPlaybackQuality) {
  7833. var quality = this._mediaElement.getVideoPlaybackQuality();
  7834. decoded = quality.totalVideoFrames;
  7835. dropped = quality.droppedVideoFrames;
  7836. } else if (this._mediaElement.webkitDecodedFrameCount != undefined) {
  7837. decoded = this._mediaElement.webkitDecodedFrameCount;
  7838. dropped = this._mediaElement.webkitDroppedFrameCount;
  7839. } else {
  7840. hasQualityInfo = false;
  7841. }
  7842. if (hasQualityInfo) {
  7843. statInfo.decodedFrames = decoded;
  7844. statInfo.droppedFrames = dropped;
  7845. }
  7846. return statInfo;
  7847. }
  7848. }, {
  7849. key: '_onmseUpdateEnd',
  7850. value: function _onmseUpdateEnd() {
  7851. if (!this._config.lazyLoad || this._config.isLive) {
  7852. return;
  7853. }
  7854. var buffered = this._mediaElement.buffered;
  7855. var currentTime = this._mediaElement.currentTime;
  7856. var currentRangeStart = 0;
  7857. var currentRangeEnd = 0;
  7858. for (var i = 0; i < buffered.length; i++) {
  7859. var start = buffered.start(i);
  7860. var end = buffered.end(i);
  7861. if (start <= currentTime && currentTime < end) {
  7862. currentRangeStart = start;
  7863. currentRangeEnd = end;
  7864. break;
  7865. }
  7866. }
  7867. if (currentRangeEnd >= currentTime + this._config.lazyLoadMaxDuration && this._progressChecker == null) {
  7868. _logger2.default.v(this.TAG, 'Maximum buffering duration exceeded, suspend transmuxing task');
  7869. this._suspendTransmuxer();
  7870. }
  7871. }
  7872. }, {
  7873. key: '_onmseBufferFull',
  7874. value: function _onmseBufferFull() {
  7875. _logger2.default.v(this.TAG, 'MSE SourceBuffer is full, suspend transmuxing task');
  7876. if (this._progressChecker == null) {
  7877. this._suspendTransmuxer();
  7878. }
  7879. }
  7880. }, {
  7881. key: '_suspendTransmuxer',
  7882. value: function _suspendTransmuxer() {
  7883. if (this._transmuxer) {
  7884. this._transmuxer.pause();
  7885. if (this._progressChecker == null) {
  7886. this._progressChecker = window.setInterval(this._checkProgressAndResume.bind(this), 1000);
  7887. }
  7888. }
  7889. }
  7890. }, {
  7891. key: '_checkProgressAndResume',
  7892. value: function _checkProgressAndResume() {
  7893. var currentTime = this._mediaElement.currentTime;
  7894. var buffered = this._mediaElement.buffered;
  7895. var needResume = false;
  7896. for (var i = 0; i < buffered.length; i++) {
  7897. var from = buffered.start(i);
  7898. var to = buffered.end(i);
  7899. if (currentTime >= from && currentTime < to) {
  7900. if (currentTime >= to - this._config.lazyLoadRecoverDuration) {
  7901. needResume = true;
  7902. }
  7903. break;
  7904. }
  7905. }
  7906. if (needResume) {
  7907. window.clearInterval(this._progressChecker);
  7908. this._progressChecker = null;
  7909. if (needResume) {
  7910. _logger2.default.v(this.TAG, 'Continue loading from paused position');
  7911. this._transmuxer.resume();
  7912. }
  7913. }
  7914. }
  7915. }, {
  7916. key: '_isTimepointBuffered',
  7917. value: function _isTimepointBuffered(seconds) {
  7918. var buffered = this._mediaElement.buffered;
  7919. for (var i = 0; i < buffered.length; i++) {
  7920. var from = buffered.start(i);
  7921. var to = buffered.end(i);
  7922. if (seconds >= from && seconds < to) {
  7923. return true;
  7924. }
  7925. }
  7926. return false;
  7927. }
  7928. }, {
  7929. key: '_internalSeek',
  7930. value: function _internalSeek(seconds) {
  7931. var directSeek = this._isTimepointBuffered(seconds);
  7932. var directSeekBegin = false;
  7933. var directSeekBeginTime = 0;
  7934. if (seconds < 1.0 && this._mediaElement.buffered.length > 0) {
  7935. var videoBeginTime = this._mediaElement.buffered.start(0);
  7936. if (videoBeginTime < 1.0 && seconds < videoBeginTime || _browser2.default.safari) {
  7937. directSeekBegin = true;
  7938. // also workaround for Safari: Seek to 0 may cause video stuck, use 0.1 to avoid
  7939. directSeekBeginTime = _browser2.default.safari ? 0.1 : videoBeginTime;
  7940. }
  7941. }
  7942. if (directSeekBegin) {
  7943. // seek to video begin, set currentTime directly if beginPTS buffered
  7944. this._requestSetTime = true;
  7945. this._mediaElement.currentTime = directSeekBeginTime;
  7946. } else if (directSeek) {
  7947. // buffered position
  7948. if (!this._alwaysSeekKeyframe) {
  7949. this._requestSetTime = true;
  7950. this._mediaElement.currentTime = seconds;
  7951. } else {
  7952. var idr = this._msectl.getNearestKeyframe(Math.floor(seconds * 1000));
  7953. this._requestSetTime = true;
  7954. if (idr != null) {
  7955. this._mediaElement.currentTime = idr.dts / 1000;
  7956. } else {
  7957. this._mediaElement.currentTime = seconds;
  7958. }
  7959. }
  7960. if (this._progressChecker != null) {
  7961. this._checkProgressAndResume();
  7962. }
  7963. } else {
  7964. if (this._progressChecker != null) {
  7965. window.clearInterval(this._progressChecker);
  7966. this._progressChecker = null;
  7967. }
  7968. this._msectl.seek(seconds);
  7969. this._transmuxer.seek(Math.floor(seconds * 1000)); // in milliseconds
  7970. // no need to set mediaElement.currentTime if non-accurateSeek,
  7971. // just wait for the recommend_seekpoint callback
  7972. if (this._config.accurateSeek) {
  7973. this._requestSetTime = true;
  7974. this._mediaElement.currentTime = seconds;
  7975. }
  7976. }
  7977. }
  7978. }, {
  7979. key: '_checkAndApplyUnbufferedSeekpoint',
  7980. value: function _checkAndApplyUnbufferedSeekpoint() {
  7981. if (this._seekpointRecord) {
  7982. if (this._seekpointRecord.recordTime <= this._now() - 100) {
  7983. var target = this._mediaElement.currentTime;
  7984. this._seekpointRecord = null;
  7985. if (!this._isTimepointBuffered(target)) {
  7986. if (this._progressChecker != null) {
  7987. window.clearTimeout(this._progressChecker);
  7988. this._progressChecker = null;
  7989. }
  7990. // .currentTime is consists with .buffered timestamp
  7991. // Chrome/Edge use DTS, while FireFox/Safari use PTS
  7992. this._msectl.seek(target);
  7993. this._transmuxer.seek(Math.floor(target * 1000));
  7994. // set currentTime if accurateSeek, or wait for recommend_seekpoint callback
  7995. if (this._config.accurateSeek) {
  7996. this._requestSetTime = true;
  7997. this._mediaElement.currentTime = target;
  7998. }
  7999. }
  8000. } else {
  8001. window.setTimeout(this._checkAndApplyUnbufferedSeekpoint.bind(this), 50);
  8002. }
  8003. }
  8004. }
  8005. }, {
  8006. key: '_checkAndResumeStuckPlayback',
  8007. value: function _checkAndResumeStuckPlayback(stalled) {
  8008. var media = this._mediaElement;
  8009. if (stalled || !this._receivedCanPlay || media.readyState < 2) {
  8010. // HAVE_CURRENT_DATA
  8011. var buffered = media.buffered;
  8012. if (buffered.length > 0 && media.currentTime < buffered.start(0)) {
  8013. _logger2.default.w(this.TAG, 'Playback seems stuck at ' + media.currentTime + ', seek to ' + buffered.start(0));
  8014. this._requestSetTime = true;
  8015. this._mediaElement.currentTime = buffered.start(0);
  8016. this._mediaElement.removeEventListener('progress', this.e.onvProgress);
  8017. }
  8018. } else {
  8019. // Playback didn't stuck, remove progress event listener
  8020. this._mediaElement.removeEventListener('progress', this.e.onvProgress);
  8021. }
  8022. }
  8023. }, {
  8024. key: '_onvLoadedMetadata',
  8025. value: function _onvLoadedMetadata(e) {
  8026. if (this._pendingSeekTime != null) {
  8027. this._mediaElement.currentTime = this._pendingSeekTime;
  8028. this._pendingSeekTime = null;
  8029. }
  8030. }
  8031. }, {
  8032. key: '_onvSeeking',
  8033. value: function _onvSeeking(e) {
  8034. // handle seeking request from browser's progress bar
  8035. var target = this._mediaElement.currentTime;
  8036. var buffered = this._mediaElement.buffered;
  8037. if (this._requestSetTime) {
  8038. this._requestSetTime = false;
  8039. return;
  8040. }
  8041. if (target < 1.0 && buffered.length > 0) {
  8042. // seek to video begin, set currentTime directly if beginPTS buffered
  8043. var videoBeginTime = buffered.start(0);
  8044. if (videoBeginTime < 1.0 && target < videoBeginTime || _browser2.default.safari) {
  8045. this._requestSetTime = true;
  8046. // also workaround for Safari: Seek to 0 may cause video stuck, use 0.1 to avoid
  8047. this._mediaElement.currentTime = _browser2.default.safari ? 0.1 : videoBeginTime;
  8048. return;
  8049. }
  8050. }
  8051. if (this._isTimepointBuffered(target)) {
  8052. if (this._alwaysSeekKeyframe) {
  8053. var idr = this._msectl.getNearestKeyframe(Math.floor(target * 1000));
  8054. if (idr != null) {
  8055. this._requestSetTime = true;
  8056. this._mediaElement.currentTime = idr.dts / 1000;
  8057. }
  8058. }
  8059. if (this._progressChecker != null) {
  8060. this._checkProgressAndResume();
  8061. }
  8062. return;
  8063. }
  8064. this._seekpointRecord = {
  8065. seekPoint: target,
  8066. recordTime: this._now()
  8067. };
  8068. window.setTimeout(this._checkAndApplyUnbufferedSeekpoint.bind(this), 50);
  8069. }
  8070. }, {
  8071. key: '_onvCanPlay',
  8072. value: function _onvCanPlay(e) {
  8073. this._receivedCanPlay = true;
  8074. this._mediaElement.removeEventListener('canplay', this.e.onvCanPlay);
  8075. }
  8076. }, {
  8077. key: '_onvStalled',
  8078. value: function _onvStalled(e) {
  8079. this._checkAndResumeStuckPlayback(true);
  8080. }
  8081. }, {
  8082. key: '_onvProgress',
  8083. value: function _onvProgress(e) {
  8084. this._checkAndResumeStuckPlayback();
  8085. }
  8086. }, {
  8087. key: 'type',
  8088. get: function get() {
  8089. return this._type;
  8090. }
  8091. }, {
  8092. key: 'buffered',
  8093. get: function get() {
  8094. return this._mediaElement.buffered;
  8095. }
  8096. }, {
  8097. key: 'duration',
  8098. get: function get() {
  8099. return this._mediaElement.duration;
  8100. }
  8101. }, {
  8102. key: 'volume',
  8103. get: function get() {
  8104. return this._mediaElement.volume;
  8105. },
  8106. set: function set(value) {
  8107. this._mediaElement.volume = value;
  8108. }
  8109. }, {
  8110. key: 'muted',
  8111. get: function get() {
  8112. return this._mediaElement.muted;
  8113. },
  8114. set: function set(muted) {
  8115. this._mediaElement.muted = muted;
  8116. }
  8117. }, {
  8118. key: 'currentTime',
  8119. get: function get() {
  8120. if (this._mediaElement) {
  8121. return this._mediaElement.currentTime;
  8122. }
  8123. return 0;
  8124. },
  8125. set: function set(seconds) {
  8126. if (this._mediaElement) {
  8127. this._internalSeek(seconds);
  8128. } else {
  8129. this._pendingSeekTime = seconds;
  8130. }
  8131. }
  8132. }, {
  8133. key: 'mediaInfo',
  8134. get: function get() {
  8135. return Object.assign({}, this._mediaInfo);
  8136. }
  8137. }, {
  8138. key: 'statisticsInfo',
  8139. get: function get() {
  8140. if (this._statisticsInfo == null) {
  8141. this._statisticsInfo = {};
  8142. }
  8143. this._statisticsInfo = this._fillStatisticsInfo(this._statisticsInfo);
  8144. return Object.assign({}, this._statisticsInfo);
  8145. }
  8146. }]);
  8147. return FlvPlayer;
  8148. }();
  8149. exports.default = FlvPlayer;
  8150. },{"../config.js":5,"../core/mse-controller.js":9,"../core/mse-events.js":10,"../core/transmuxer.js":11,"../core/transmuxing-events.js":13,"../utils/browser.js":39,"../utils/exception.js":40,"../utils/logger.js":41,"./player-errors.js":34,"./player-events.js":35,"events":2}],33:[function(_dereq_,module,exports){
  8151. 'use strict';
  8152. Object.defineProperty(exports, "__esModule", {
  8153. value: true
  8154. });
  8155. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  8156. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  8157. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  8158. *
  8159. * @author zheng qian <xqq@xqq.im>
  8160. *
  8161. * Licensed under the Apache License, Version 2.0 (the "License");
  8162. * you may not use this file except in compliance with the License.
  8163. * You may obtain a copy of the License at
  8164. *
  8165. * http://www.apache.org/licenses/LICENSE-2.0
  8166. *
  8167. * Unless required by applicable law or agreed to in writing, software
  8168. * distributed under the License is distributed on an "AS IS" BASIS,
  8169. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8170. * See the License for the specific language governing permissions and
  8171. * limitations under the License.
  8172. */
  8173. var _events = _dereq_('events');
  8174. var _events2 = _interopRequireDefault(_events);
  8175. var _playerEvents = _dereq_('./player-events.js');
  8176. var _playerEvents2 = _interopRequireDefault(_playerEvents);
  8177. var _config = _dereq_('../config.js');
  8178. var _exception = _dereq_('../utils/exception.js');
  8179. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8180. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8181. // Player wrapper for browser's native player (HTMLVideoElement) without MediaSource src.
  8182. var NativePlayer = function () {
  8183. function NativePlayer(mediaDataSource, config) {
  8184. _classCallCheck(this, NativePlayer);
  8185. this.TAG = 'NativePlayer';
  8186. this._type = 'NativePlayer';
  8187. this._emitter = new _events2.default();
  8188. this._config = (0, _config.createDefaultConfig)();
  8189. if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) === 'object') {
  8190. Object.assign(this._config, config);
  8191. }
  8192. if (mediaDataSource.type.toLowerCase() === 'flv') {
  8193. throw new _exception.InvalidArgumentException('NativePlayer does\'t support flv MediaDataSource input!');
  8194. }
  8195. if (mediaDataSource.hasOwnProperty('segments')) {
  8196. throw new _exception.InvalidArgumentException('NativePlayer(' + mediaDataSource.type + ') doesn\'t support multipart playback!');
  8197. }
  8198. this.e = {
  8199. onvLoadedMetadata: this._onvLoadedMetadata.bind(this)
  8200. };
  8201. this._pendingSeekTime = null;
  8202. this._statisticsReporter = null;
  8203. this._mediaDataSource = mediaDataSource;
  8204. this._mediaElement = null;
  8205. }
  8206. _createClass(NativePlayer, [{
  8207. key: 'destroy',
  8208. value: function destroy() {
  8209. if (this._mediaElement) {
  8210. this.unload();
  8211. this.detachMediaElement();
  8212. }
  8213. this.e = null;
  8214. this._mediaDataSource = null;
  8215. this._emitter.removeAllListeners();
  8216. this._emitter = null;
  8217. }
  8218. }, {
  8219. key: 'on',
  8220. value: function on(event, listener) {
  8221. var _this = this;
  8222. if (event === _playerEvents2.default.MEDIA_INFO) {
  8223. if (this._mediaElement != null && this._mediaElement.readyState !== 0) {
  8224. // HAVE_NOTHING
  8225. Promise.resolve().then(function () {
  8226. _this._emitter.emit(_playerEvents2.default.MEDIA_INFO, _this.mediaInfo);
  8227. });
  8228. }
  8229. } else if (event === _playerEvents2.default.STATISTICS_INFO) {
  8230. if (this._mediaElement != null && this._mediaElement.readyState !== 0) {
  8231. Promise.resolve().then(function () {
  8232. _this._emitter.emit(_playerEvents2.default.STATISTICS_INFO, _this.statisticsInfo);
  8233. });
  8234. }
  8235. }
  8236. this._emitter.addListener(event, listener);
  8237. }
  8238. }, {
  8239. key: 'off',
  8240. value: function off(event, listener) {
  8241. this._emitter.removeListener(event, listener);
  8242. }
  8243. }, {
  8244. key: 'attachMediaElement',
  8245. value: function attachMediaElement(mediaElement) {
  8246. this._mediaElement = mediaElement;
  8247. mediaElement.addEventListener('loadedmetadata', this.e.onvLoadedMetadata);
  8248. if (this._pendingSeekTime != null) {
  8249. try {
  8250. mediaElement.currentTime = this._pendingSeekTime;
  8251. this._pendingSeekTime = null;
  8252. } catch (e) {
  8253. // IE11 may throw InvalidStateError if readyState === 0
  8254. // Defer set currentTime operation after loadedmetadata
  8255. }
  8256. }
  8257. }
  8258. }, {
  8259. key: 'detachMediaElement',
  8260. value: function detachMediaElement() {
  8261. if (this._mediaElement) {
  8262. this._mediaElement.src = '';
  8263. this._mediaElement.removeAttribute('src');
  8264. this._mediaElement.removeEventListener('loadedmetadata', this.e.onvLoadedMetadata);
  8265. this._mediaElement = null;
  8266. }
  8267. if (this._statisticsReporter != null) {
  8268. window.clearInterval(this._statisticsReporter);
  8269. this._statisticsReporter = null;
  8270. }
  8271. }
  8272. }, {
  8273. key: 'load',
  8274. value: function load() {
  8275. if (!this._mediaElement) {
  8276. throw new _exception.IllegalStateException('HTMLMediaElement must be attached before load()!');
  8277. }
  8278. this._mediaElement.src = this._mediaDataSource.url;
  8279. if (this._mediaElement.readyState > 0) {
  8280. this._mediaElement.currentTime = 0;
  8281. }
  8282. this._mediaElement.preload = 'auto';
  8283. this._mediaElement.load();
  8284. this._statisticsReporter = window.setInterval(this._reportStatisticsInfo.bind(this), this._config.statisticsInfoReportInterval);
  8285. }
  8286. }, {
  8287. key: 'unload',
  8288. value: function unload() {
  8289. if (this._mediaElement) {
  8290. this._mediaElement.src = '';
  8291. this._mediaElement.removeAttribute('src');
  8292. }
  8293. if (this._statisticsReporter != null) {
  8294. window.clearInterval(this._statisticsReporter);
  8295. this._statisticsReporter = null;
  8296. }
  8297. }
  8298. }, {
  8299. key: 'play',
  8300. value: function play() {
  8301. return this._mediaElement.play();
  8302. }
  8303. }, {
  8304. key: 'pause',
  8305. value: function pause() {
  8306. this._mediaElement.pause();
  8307. }
  8308. }, {
  8309. key: '_onvLoadedMetadata',
  8310. value: function _onvLoadedMetadata(e) {
  8311. if (this._pendingSeekTime != null) {
  8312. this._mediaElement.currentTime = this._pendingSeekTime;
  8313. this._pendingSeekTime = null;
  8314. }
  8315. this._emitter.emit(_playerEvents2.default.MEDIA_INFO, this.mediaInfo);
  8316. }
  8317. }, {
  8318. key: '_reportStatisticsInfo',
  8319. value: function _reportStatisticsInfo() {
  8320. this._emitter.emit(_playerEvents2.default.STATISTICS_INFO, this.statisticsInfo);
  8321. }
  8322. }, {
  8323. key: 'type',
  8324. get: function get() {
  8325. return this._type;
  8326. }
  8327. }, {
  8328. key: 'buffered',
  8329. get: function get() {
  8330. return this._mediaElement.buffered;
  8331. }
  8332. }, {
  8333. key: 'duration',
  8334. get: function get() {
  8335. return this._mediaElement.duration;
  8336. }
  8337. }, {
  8338. key: 'volume',
  8339. get: function get() {
  8340. return this._mediaElement.volume;
  8341. },
  8342. set: function set(value) {
  8343. this._mediaElement.volume = value;
  8344. }
  8345. }, {
  8346. key: 'muted',
  8347. get: function get() {
  8348. return this._mediaElement.muted;
  8349. },
  8350. set: function set(muted) {
  8351. this._mediaElement.muted = muted;
  8352. }
  8353. }, {
  8354. key: 'currentTime',
  8355. get: function get() {
  8356. if (this._mediaElement) {
  8357. return this._mediaElement.currentTime;
  8358. }
  8359. return 0;
  8360. },
  8361. set: function set(seconds) {
  8362. if (this._mediaElement) {
  8363. this._mediaElement.currentTime = seconds;
  8364. } else {
  8365. this._pendingSeekTime = seconds;
  8366. }
  8367. }
  8368. }, {
  8369. key: 'mediaInfo',
  8370. get: function get() {
  8371. var mediaPrefix = this._mediaElement instanceof HTMLAudioElement ? 'audio/' : 'video/';
  8372. var info = {
  8373. mimeType: mediaPrefix + this._mediaDataSource.type
  8374. };
  8375. if (this._mediaElement) {
  8376. info.duration = Math.floor(this._mediaElement.duration * 1000);
  8377. if (this._mediaElement instanceof HTMLVideoElement) {
  8378. info.width = this._mediaElement.videoWidth;
  8379. info.height = this._mediaElement.videoHeight;
  8380. }
  8381. }
  8382. return info;
  8383. }
  8384. }, {
  8385. key: 'statisticsInfo',
  8386. get: function get() {
  8387. var info = {
  8388. playerType: this._type,
  8389. url: this._mediaDataSource.url
  8390. };
  8391. if (!(this._mediaElement instanceof HTMLVideoElement)) {
  8392. return info;
  8393. }
  8394. var hasQualityInfo = true;
  8395. var decoded = 0;
  8396. var dropped = 0;
  8397. if (this._mediaElement.getVideoPlaybackQuality) {
  8398. var quality = this._mediaElement.getVideoPlaybackQuality();
  8399. decoded = quality.totalVideoFrames;
  8400. dropped = quality.droppedVideoFrames;
  8401. } else if (this._mediaElement.webkitDecodedFrameCount != undefined) {
  8402. decoded = this._mediaElement.webkitDecodedFrameCount;
  8403. dropped = this._mediaElement.webkitDroppedFrameCount;
  8404. } else {
  8405. hasQualityInfo = false;
  8406. }
  8407. if (hasQualityInfo) {
  8408. info.decodedFrames = decoded;
  8409. info.droppedFrames = dropped;
  8410. }
  8411. return info;
  8412. }
  8413. }]);
  8414. return NativePlayer;
  8415. }();
  8416. exports.default = NativePlayer;
  8417. },{"../config.js":5,"../utils/exception.js":40,"./player-events.js":35,"events":2}],34:[function(_dereq_,module,exports){
  8418. 'use strict';
  8419. Object.defineProperty(exports, "__esModule", {
  8420. value: true
  8421. });
  8422. exports.ErrorDetails = exports.ErrorTypes = undefined;
  8423. var _loader = _dereq_('../io/loader.js');
  8424. var _demuxErrors = _dereq_('../demux/demux-errors.js');
  8425. var _demuxErrors2 = _interopRequireDefault(_demuxErrors);
  8426. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8427. /*
  8428. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  8429. *
  8430. * @author zheng qian <xqq@xqq.im>
  8431. *
  8432. * Licensed under the Apache License, Version 2.0 (the "License");
  8433. * you may not use this file except in compliance with the License.
  8434. * You may obtain a copy of the License at
  8435. *
  8436. * http://www.apache.org/licenses/LICENSE-2.0
  8437. *
  8438. * Unless required by applicable law or agreed to in writing, software
  8439. * distributed under the License is distributed on an "AS IS" BASIS,
  8440. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8441. * See the License for the specific language governing permissions and
  8442. * limitations under the License.
  8443. */
  8444. var ErrorTypes = exports.ErrorTypes = {
  8445. NETWORK_ERROR: 'NetworkError',
  8446. MEDIA_ERROR: 'MediaError',
  8447. OTHER_ERROR: 'OtherError'
  8448. };
  8449. var ErrorDetails = exports.ErrorDetails = {
  8450. NETWORK_EXCEPTION: _loader.LoaderErrors.EXCEPTION,
  8451. NETWORK_STATUS_CODE_INVALID: _loader.LoaderErrors.HTTP_STATUS_CODE_INVALID,
  8452. NETWORK_TIMEOUT: _loader.LoaderErrors.CONNECTING_TIMEOUT,
  8453. NETWORK_UNRECOVERABLE_EARLY_EOF: _loader.LoaderErrors.UNRECOVERABLE_EARLY_EOF,
  8454. MEDIA_MSE_ERROR: 'MediaMSEError',
  8455. MEDIA_FORMAT_ERROR: _demuxErrors2.default.FORMAT_ERROR,
  8456. MEDIA_FORMAT_UNSUPPORTED: _demuxErrors2.default.FORMAT_UNSUPPORTED,
  8457. MEDIA_CODEC_UNSUPPORTED: _demuxErrors2.default.CODEC_UNSUPPORTED
  8458. };
  8459. },{"../demux/demux-errors.js":16,"../io/loader.js":24}],35:[function(_dereq_,module,exports){
  8460. 'use strict';
  8461. Object.defineProperty(exports, "__esModule", {
  8462. value: true
  8463. });
  8464. /*
  8465. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  8466. *
  8467. * @author zheng qian <xqq@xqq.im>
  8468. *
  8469. * Licensed under the Apache License, Version 2.0 (the "License");
  8470. * you may not use this file except in compliance with the License.
  8471. * You may obtain a copy of the License at
  8472. *
  8473. * http://www.apache.org/licenses/LICENSE-2.0
  8474. *
  8475. * Unless required by applicable law or agreed to in writing, software
  8476. * distributed under the License is distributed on an "AS IS" BASIS,
  8477. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8478. * See the License for the specific language governing permissions and
  8479. * limitations under the License.
  8480. */
  8481. var PlayerEvents = {
  8482. ERROR: 'error',
  8483. LOADING_COMPLETE: 'loading_complete',
  8484. RECOVERED_EARLY_EOF: 'recovered_early_eof',
  8485. MEDIA_INFO: 'media_info',
  8486. METADATA_ARRIVED: 'metadata_arrived',
  8487. STATISTICS_INFO: 'statistics_info'
  8488. };
  8489. exports.default = PlayerEvents;
  8490. },{}],36:[function(_dereq_,module,exports){
  8491. 'use strict';
  8492. Object.defineProperty(exports, "__esModule", {
  8493. value: true
  8494. });
  8495. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  8496. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8497. /*
  8498. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  8499. *
  8500. * This file is modified from dailymotion's hls.js library (hls.js/src/helper/aac.js)
  8501. * @author zheng qian <xqq@xqq.im>
  8502. *
  8503. * Licensed under the Apache License, Version 2.0 (the "License");
  8504. * you may not use this file except in compliance with the License.
  8505. * You may obtain a copy of the License at
  8506. *
  8507. * http://www.apache.org/licenses/LICENSE-2.0
  8508. *
  8509. * Unless required by applicable law or agreed to in writing, software
  8510. * distributed under the License is distributed on an "AS IS" BASIS,
  8511. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8512. * See the License for the specific language governing permissions and
  8513. * limitations under the License.
  8514. */
  8515. var AAC = function () {
  8516. function AAC() {
  8517. _classCallCheck(this, AAC);
  8518. }
  8519. _createClass(AAC, null, [{
  8520. key: 'getSilentFrame',
  8521. value: function getSilentFrame(codec, channelCount) {
  8522. if (codec === 'mp4a.40.2') {
  8523. // handle LC-AAC
  8524. if (channelCount === 1) {
  8525. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]);
  8526. } else if (channelCount === 2) {
  8527. return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]);
  8528. } else if (channelCount === 3) {
  8529. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]);
  8530. } else if (channelCount === 4) {
  8531. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]);
  8532. } else if (channelCount === 5) {
  8533. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]);
  8534. } else if (channelCount === 6) {
  8535. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]);
  8536. }
  8537. } else {
  8538. // handle HE-AAC (mp4a.40.5 / mp4a.40.29)
  8539. if (channelCount === 1) {
  8540. // ffmpeg -y -f lavfi -i "aevalsrc=0:d=0.05" -c:a libfdk_aac -profile:a aac_he -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  8541. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x4e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x1c, 0x6, 0xf1, 0xc1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  8542. } else if (channelCount === 2) {
  8543. // ffmpeg -y -f lavfi -i "aevalsrc=0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  8544. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  8545. } else if (channelCount === 3) {
  8546. // ffmpeg -y -f lavfi -i "aevalsrc=0|0|0:d=0.05" -c:a libfdk_aac -profile:a aac_he_v2 -b:a 4k output.aac && hexdump -v -e '16/1 "0x%x," "\n"' -v output.aac
  8547. return new Uint8Array([0x1, 0x40, 0x22, 0x80, 0xa3, 0x5e, 0xe6, 0x80, 0xba, 0x8, 0x0, 0x0, 0x0, 0x0, 0x95, 0x0, 0x6, 0xf1, 0xa1, 0xa, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5a, 0x5e]);
  8548. }
  8549. }
  8550. return null;
  8551. }
  8552. }]);
  8553. return AAC;
  8554. }();
  8555. exports.default = AAC;
  8556. },{}],37:[function(_dereq_,module,exports){
  8557. 'use strict';
  8558. Object.defineProperty(exports, "__esModule", {
  8559. value: true
  8560. });
  8561. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  8562. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  8563. /*
  8564. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  8565. *
  8566. * This file is derived from dailymotion's hls.js library (hls.js/src/remux/mp4-generator.js)
  8567. * @author zheng qian <xqq@xqq.im>
  8568. *
  8569. * Licensed under the Apache License, Version 2.0 (the "License");
  8570. * you may not use this file except in compliance with the License.
  8571. * You may obtain a copy of the License at
  8572. *
  8573. * http://www.apache.org/licenses/LICENSE-2.0
  8574. *
  8575. * Unless required by applicable law or agreed to in writing, software
  8576. * distributed under the License is distributed on an "AS IS" BASIS,
  8577. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  8578. * See the License for the specific language governing permissions and
  8579. * limitations under the License.
  8580. */
  8581. // MP4 boxes generator for ISO BMFF (ISO Base Media File Format, defined in ISO/IEC 14496-12)
  8582. var MP4 = function () {
  8583. function MP4() {
  8584. _classCallCheck(this, MP4);
  8585. }
  8586. _createClass(MP4, null, [{
  8587. key: 'init',
  8588. value: function init() {
  8589. MP4.types = {
  8590. avc1: [], avcC: [], btrt: [], dinf: [],
  8591. dref: [], esds: [], ftyp: [], hdlr: [],
  8592. mdat: [], mdhd: [], mdia: [], mfhd: [],
  8593. minf: [], moof: [], moov: [], mp4a: [],
  8594. mvex: [], mvhd: [], sdtp: [], stbl: [],
  8595. stco: [], stsc: [], stsd: [], stsz: [],
  8596. stts: [], tfdt: [], tfhd: [], traf: [],
  8597. trak: [], trun: [], trex: [], tkhd: [],
  8598. vmhd: [], smhd: [], '.mp3': []
  8599. };
  8600. for (var name in MP4.types) {
  8601. if (MP4.types.hasOwnProperty(name)) {
  8602. MP4.types[name] = [name.charCodeAt(0), name.charCodeAt(1), name.charCodeAt(2), name.charCodeAt(3)];
  8603. }
  8604. }
  8605. var constants = MP4.constants = {};
  8606. constants.FTYP = new Uint8Array([0x69, 0x73, 0x6F, 0x6D, // major_brand: isom
  8607. 0x0, 0x0, 0x0, 0x1, // minor_version: 0x01
  8608. 0x69, 0x73, 0x6F, 0x6D, // isom
  8609. 0x61, 0x76, 0x63, 0x31 // avc1
  8610. ]);
  8611. constants.STSD_PREFIX = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8612. 0x00, 0x00, 0x00, 0x01 // entry_count
  8613. ]);
  8614. constants.STTS = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8615. 0x00, 0x00, 0x00, 0x00 // entry_count
  8616. ]);
  8617. constants.STSC = constants.STCO = constants.STTS;
  8618. constants.STSZ = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8619. 0x00, 0x00, 0x00, 0x00, // sample_size
  8620. 0x00, 0x00, 0x00, 0x00 // sample_count
  8621. ]);
  8622. constants.HDLR_VIDEO = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8623. 0x00, 0x00, 0x00, 0x00, // pre_defined
  8624. 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
  8625. 0x00, 0x00, 0x00, 0x00, // reserved: 3 * 4 bytes
  8626. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x56, 0x69, 0x64, 0x65, 0x6F, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: VideoHandler
  8627. ]);
  8628. constants.HDLR_AUDIO = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8629. 0x00, 0x00, 0x00, 0x00, // pre_defined
  8630. 0x73, 0x6F, 0x75, 0x6E, // handler_type: 'soun'
  8631. 0x00, 0x00, 0x00, 0x00, // reserved: 3 * 4 bytes
  8632. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x53, 0x6F, 0x75, 0x6E, 0x64, 0x48, 0x61, 0x6E, 0x64, 0x6C, 0x65, 0x72, 0x00 // name: SoundHandler
  8633. ]);
  8634. constants.DREF = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8635. 0x00, 0x00, 0x00, 0x01, // entry_count
  8636. 0x00, 0x00, 0x00, 0x0C, // entry_size
  8637. 0x75, 0x72, 0x6C, 0x20, // type 'url '
  8638. 0x00, 0x00, 0x00, 0x01 // version(0) + flags
  8639. ]);
  8640. // Sound media header
  8641. constants.SMHD = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8642. 0x00, 0x00, 0x00, 0x00 // balance(2) + reserved(2)
  8643. ]);
  8644. // video media header
  8645. constants.VMHD = new Uint8Array([0x00, 0x00, 0x00, 0x01, // version(0) + flags
  8646. 0x00, 0x00, // graphicsmode: 2 bytes
  8647. 0x00, 0x00, 0x00, 0x00, // opcolor: 3 * 2 bytes
  8648. 0x00, 0x00]);
  8649. }
  8650. // Generate a box
  8651. }, {
  8652. key: 'box',
  8653. value: function box(type) {
  8654. var size = 8;
  8655. var result = null;
  8656. var datas = Array.prototype.slice.call(arguments, 1);
  8657. var arrayCount = datas.length;
  8658. for (var i = 0; i < arrayCount; i++) {
  8659. size += datas[i].byteLength;
  8660. }
  8661. result = new Uint8Array(size);
  8662. result[0] = size >>> 24 & 0xFF; // size
  8663. result[1] = size >>> 16 & 0xFF;
  8664. result[2] = size >>> 8 & 0xFF;
  8665. result[3] = size & 0xFF;
  8666. result.set(type, 4); // type
  8667. var offset = 8;
  8668. for (var _i = 0; _i < arrayCount; _i++) {
  8669. // data body
  8670. result.set(datas[_i], offset);
  8671. offset += datas[_i].byteLength;
  8672. }
  8673. return result;
  8674. }
  8675. // emit ftyp & moov
  8676. }, {
  8677. key: 'generateInitSegment',
  8678. value: function generateInitSegment(meta) {
  8679. var ftyp = MP4.box(MP4.types.ftyp, MP4.constants.FTYP);
  8680. var moov = MP4.moov(meta);
  8681. var result = new Uint8Array(ftyp.byteLength + moov.byteLength);
  8682. result.set(ftyp, 0);
  8683. result.set(moov, ftyp.byteLength);
  8684. return result;
  8685. }
  8686. // Movie metadata box
  8687. }, {
  8688. key: 'moov',
  8689. value: function moov(meta) {
  8690. var mvhd = MP4.mvhd(meta.timescale, meta.duration);
  8691. var trak = MP4.trak(meta);
  8692. var mvex = MP4.mvex(meta);
  8693. return MP4.box(MP4.types.moov, mvhd, trak, mvex);
  8694. }
  8695. // Movie header box
  8696. }, {
  8697. key: 'mvhd',
  8698. value: function mvhd(timescale, duration) {
  8699. return MP4.box(MP4.types.mvhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8700. 0x00, 0x00, 0x00, 0x00, // creation_time
  8701. 0x00, 0x00, 0x00, 0x00, // modification_time
  8702. timescale >>> 24 & 0xFF, // timescale: 4 bytes
  8703. timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF, duration >>> 24 & 0xFF, // duration: 4 bytes
  8704. duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x01, 0x00, 0x00, // Preferred rate: 1.0
  8705. 0x01, 0x00, 0x00, 0x00, // PreferredVolume(1.0, 2bytes) + reserved(2bytes)
  8706. 0x00, 0x00, 0x00, 0x00, // reserved: 4 + 4 bytes
  8707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, // ----begin composition matrix----
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // ----end composition matrix----
  8709. 0x00, 0x00, 0x00, 0x00, // ----begin pre_defined 6 * 4 bytes----
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ----end pre_defined 6 * 4 bytes----
  8711. 0xFF, 0xFF, 0xFF, 0xFF // next_track_ID
  8712. ]));
  8713. }
  8714. // Track box
  8715. }, {
  8716. key: 'trak',
  8717. value: function trak(meta) {
  8718. return MP4.box(MP4.types.trak, MP4.tkhd(meta), MP4.mdia(meta));
  8719. }
  8720. // Track header box
  8721. }, {
  8722. key: 'tkhd',
  8723. value: function tkhd(meta) {
  8724. var trackId = meta.id,
  8725. duration = meta.duration;
  8726. var width = meta.presentWidth,
  8727. height = meta.presentHeight;
  8728. return MP4.box(MP4.types.tkhd, new Uint8Array([0x00, 0x00, 0x00, 0x07, // version(0) + flags
  8729. 0x00, 0x00, 0x00, 0x00, // creation_time
  8730. 0x00, 0x00, 0x00, 0x00, // modification_time
  8731. trackId >>> 24 & 0xFF, // track_ID: 4 bytes
  8732. trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x00, // reserved: 4 bytes
  8733. duration >>> 24 & 0xFF, // duration: 4 bytes
  8734. duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x00, 0x00, 0x00, 0x00, // reserved: 2 * 4 bytes
  8735. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // layer(2bytes) + alternate_group(2bytes)
  8736. 0x00, 0x00, 0x00, 0x00, // volume(2bytes) + reserved(2bytes)
  8737. 0x00, 0x01, 0x00, 0x00, // ----begin composition matrix----
  8738. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // ----end composition matrix----
  8739. width >>> 8 & 0xFF, // width and height
  8740. width & 0xFF, 0x00, 0x00, height >>> 8 & 0xFF, height & 0xFF, 0x00, 0x00]));
  8741. }
  8742. // Media Box
  8743. }, {
  8744. key: 'mdia',
  8745. value: function mdia(meta) {
  8746. return MP4.box(MP4.types.mdia, MP4.mdhd(meta), MP4.hdlr(meta), MP4.minf(meta));
  8747. }
  8748. // Media header box
  8749. }, {
  8750. key: 'mdhd',
  8751. value: function mdhd(meta) {
  8752. var timescale = meta.timescale;
  8753. var duration = meta.duration;
  8754. return MP4.box(MP4.types.mdhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8755. 0x00, 0x00, 0x00, 0x00, // creation_time
  8756. 0x00, 0x00, 0x00, 0x00, // modification_time
  8757. timescale >>> 24 & 0xFF, // timescale: 4 bytes
  8758. timescale >>> 16 & 0xFF, timescale >>> 8 & 0xFF, timescale & 0xFF, duration >>> 24 & 0xFF, // duration: 4 bytes
  8759. duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, 0x55, 0xC4, // language: und (undetermined)
  8760. 0x00, 0x00 // pre_defined = 0
  8761. ]));
  8762. }
  8763. // Media handler reference box
  8764. }, {
  8765. key: 'hdlr',
  8766. value: function hdlr(meta) {
  8767. var data = null;
  8768. if (meta.type === 'audio') {
  8769. data = MP4.constants.HDLR_AUDIO;
  8770. } else {
  8771. data = MP4.constants.HDLR_VIDEO;
  8772. }
  8773. return MP4.box(MP4.types.hdlr, data);
  8774. }
  8775. // Media infomation box
  8776. }, {
  8777. key: 'minf',
  8778. value: function minf(meta) {
  8779. var xmhd = null;
  8780. if (meta.type === 'audio') {
  8781. xmhd = MP4.box(MP4.types.smhd, MP4.constants.SMHD);
  8782. } else {
  8783. xmhd = MP4.box(MP4.types.vmhd, MP4.constants.VMHD);
  8784. }
  8785. return MP4.box(MP4.types.minf, xmhd, MP4.dinf(), MP4.stbl(meta));
  8786. }
  8787. // Data infomation box
  8788. }, {
  8789. key: 'dinf',
  8790. value: function dinf() {
  8791. var result = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, MP4.constants.DREF));
  8792. return result;
  8793. }
  8794. // Sample table box
  8795. }, {
  8796. key: 'stbl',
  8797. value: function stbl(meta) {
  8798. var result = MP4.box(MP4.types.stbl, // type: stbl
  8799. MP4.stsd(meta), // Sample Description Table
  8800. MP4.box(MP4.types.stts, MP4.constants.STTS), // Time-To-Sample
  8801. MP4.box(MP4.types.stsc, MP4.constants.STSC), // Sample-To-Chunk
  8802. MP4.box(MP4.types.stsz, MP4.constants.STSZ), // Sample size
  8803. MP4.box(MP4.types.stco, MP4.constants.STCO) // Chunk offset
  8804. );
  8805. return result;
  8806. }
  8807. // Sample description box
  8808. }, {
  8809. key: 'stsd',
  8810. value: function stsd(meta) {
  8811. if (meta.type === 'audio') {
  8812. if (meta.codec === 'mp3') {
  8813. return MP4.box(MP4.types.stsd, MP4.constants.STSD_PREFIX, MP4.mp3(meta));
  8814. }
  8815. // else: aac -> mp4a
  8816. return MP4.box(MP4.types.stsd, MP4.constants.STSD_PREFIX, MP4.mp4a(meta));
  8817. } else {
  8818. return MP4.box(MP4.types.stsd, MP4.constants.STSD_PREFIX, MP4.avc1(meta));
  8819. }
  8820. }
  8821. }, {
  8822. key: 'mp3',
  8823. value: function mp3(meta) {
  8824. var channelCount = meta.channelCount;
  8825. var sampleRate = meta.audioSampleRate;
  8826. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, // reserved(4)
  8827. 0x00, 0x00, 0x00, 0x01, // reserved(2) + data_reference_index(2)
  8828. 0x00, 0x00, 0x00, 0x00, // reserved: 2 * 4 bytes
  8829. 0x00, 0x00, 0x00, 0x00, 0x00, channelCount, // channelCount(2)
  8830. 0x00, 0x10, // sampleSize(2)
  8831. 0x00, 0x00, 0x00, 0x00, // reserved(4)
  8832. sampleRate >>> 8 & 0xFF, // Audio sample rate
  8833. sampleRate & 0xFF, 0x00, 0x00]);
  8834. return MP4.box(MP4.types['.mp3'], data);
  8835. }
  8836. }, {
  8837. key: 'mp4a',
  8838. value: function mp4a(meta) {
  8839. var channelCount = meta.channelCount;
  8840. var sampleRate = meta.audioSampleRate;
  8841. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, // reserved(4)
  8842. 0x00, 0x00, 0x00, 0x01, // reserved(2) + data_reference_index(2)
  8843. 0x00, 0x00, 0x00, 0x00, // reserved: 2 * 4 bytes
  8844. 0x00, 0x00, 0x00, 0x00, 0x00, channelCount, // channelCount(2)
  8845. 0x00, 0x10, // sampleSize(2)
  8846. 0x00, 0x00, 0x00, 0x00, // reserved(4)
  8847. sampleRate >>> 8 & 0xFF, // Audio sample rate
  8848. sampleRate & 0xFF, 0x00, 0x00]);
  8849. return MP4.box(MP4.types.mp4a, data, MP4.esds(meta));
  8850. }
  8851. }, {
  8852. key: 'esds',
  8853. value: function esds(meta) {
  8854. var config = meta.config || [];
  8855. var configSize = config.length;
  8856. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version 0 + flags
  8857. 0x03, // descriptor_type
  8858. 0x17 + configSize, // length3
  8859. 0x00, 0x01, // es_id
  8860. 0x00, // stream_priority
  8861. 0x04, // descriptor_type
  8862. 0x0F + configSize, // length
  8863. 0x40, // codec: mpeg4_audio
  8864. 0x15, // stream_type: Audio
  8865. 0x00, 0x00, 0x00, // buffer_size
  8866. 0x00, 0x00, 0x00, 0x00, // maxBitrate
  8867. 0x00, 0x00, 0x00, 0x00, // avgBitrate
  8868. 0x05 // descriptor_type
  8869. ].concat([configSize]).concat(config).concat([0x06, 0x01, 0x02 // GASpecificConfig
  8870. ]));
  8871. return MP4.box(MP4.types.esds, data);
  8872. }
  8873. }, {
  8874. key: 'avc1',
  8875. value: function avc1(meta) {
  8876. var avcc = meta.avcc;
  8877. var width = meta.codecWidth,
  8878. height = meta.codecHeight;
  8879. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, // reserved(4)
  8880. 0x00, 0x00, 0x00, 0x01, // reserved(2) + data_reference_index(2)
  8881. 0x00, 0x00, 0x00, 0x00, // pre_defined(2) + reserved(2)
  8882. 0x00, 0x00, 0x00, 0x00, // pre_defined: 3 * 4 bytes
  8883. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, width >>> 8 & 0xFF, // width: 2 bytes
  8884. width & 0xFF, height >>> 8 & 0xFF, // height: 2 bytes
  8885. height & 0xFF, 0x00, 0x48, 0x00, 0x00, // horizresolution: 4 bytes
  8886. 0x00, 0x48, 0x00, 0x00, // vertresolution: 4 bytes
  8887. 0x00, 0x00, 0x00, 0x00, // reserved: 4 bytes
  8888. 0x00, 0x01, // frame_count
  8889. 0x0A, // strlen
  8890. 0x78, 0x71, 0x71, 0x2F, // compressorname: 32 bytes
  8891. 0x66, 0x6C, 0x76, 0x2E, 0x6A, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, // depth
  8892. 0xFF, 0xFF // pre_defined = -1
  8893. ]);
  8894. return MP4.box(MP4.types.avc1, data, MP4.box(MP4.types.avcC, avcc));
  8895. }
  8896. // Movie Extends box
  8897. }, {
  8898. key: 'mvex',
  8899. value: function mvex(meta) {
  8900. return MP4.box(MP4.types.mvex, MP4.trex(meta));
  8901. }
  8902. // Track Extends box
  8903. }, {
  8904. key: 'trex',
  8905. value: function trex(meta) {
  8906. var trackId = meta.id;
  8907. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) + flags
  8908. trackId >>> 24 & 0xFF, // track_ID
  8909. trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF, 0x00, 0x00, 0x00, 0x01, // default_sample_description_index
  8910. 0x00, 0x00, 0x00, 0x00, // default_sample_duration
  8911. 0x00, 0x00, 0x00, 0x00, // default_sample_size
  8912. 0x00, 0x01, 0x00, 0x01 // default_sample_flags
  8913. ]);
  8914. return MP4.box(MP4.types.trex, data);
  8915. }
  8916. // Movie fragment box
  8917. }, {
  8918. key: 'moof',
  8919. value: function moof(track, baseMediaDecodeTime) {
  8920. return MP4.box(MP4.types.moof, MP4.mfhd(track.sequenceNumber), MP4.traf(track, baseMediaDecodeTime));
  8921. }
  8922. }, {
  8923. key: 'mfhd',
  8924. value: function mfhd(sequenceNumber) {
  8925. var data = new Uint8Array([0x00, 0x00, 0x00, 0x00, sequenceNumber >>> 24 & 0xFF, // sequence_number: int32
  8926. sequenceNumber >>> 16 & 0xFF, sequenceNumber >>> 8 & 0xFF, sequenceNumber & 0xFF]);
  8927. return MP4.box(MP4.types.mfhd, data);
  8928. }
  8929. // Track fragment box
  8930. }, {
  8931. key: 'traf',
  8932. value: function traf(track, baseMediaDecodeTime) {
  8933. var trackId = track.id;
  8934. // Track fragment header box
  8935. var tfhd = MP4.box(MP4.types.tfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) & flags
  8936. trackId >>> 24 & 0xFF, // track_ID
  8937. trackId >>> 16 & 0xFF, trackId >>> 8 & 0xFF, trackId & 0xFF]));
  8938. // Track Fragment Decode Time
  8939. var tfdt = MP4.box(MP4.types.tfdt, new Uint8Array([0x00, 0x00, 0x00, 0x00, // version(0) & flags
  8940. baseMediaDecodeTime >>> 24 & 0xFF, // baseMediaDecodeTime: int32
  8941. baseMediaDecodeTime >>> 16 & 0xFF, baseMediaDecodeTime >>> 8 & 0xFF, baseMediaDecodeTime & 0xFF]));
  8942. var sdtp = MP4.sdtp(track);
  8943. var trun = MP4.trun(track, sdtp.byteLength + 16 + 16 + 8 + 16 + 8 + 8);
  8944. return MP4.box(MP4.types.traf, tfhd, tfdt, trun, sdtp);
  8945. }
  8946. // Sample Dependency Type box
  8947. }, {
  8948. key: 'sdtp',
  8949. value: function sdtp(track) {
  8950. var samples = track.samples || [];
  8951. var sampleCount = samples.length;
  8952. var data = new Uint8Array(4 + sampleCount);
  8953. // 0~4 bytes: version(0) & flags
  8954. for (var i = 0; i < sampleCount; i++) {
  8955. var flags = samples[i].flags;
  8956. data[i + 4] = flags.isLeading << 6 | // is_leading: 2 (bit)
  8957. flags.dependsOn << 4 // sample_depends_on
  8958. | flags.isDependedOn << 2 // sample_is_depended_on
  8959. | flags.hasRedundancy; // sample_has_redundancy
  8960. }
  8961. return MP4.box(MP4.types.sdtp, data);
  8962. }
  8963. // Track fragment run box
  8964. }, {
  8965. key: 'trun',
  8966. value: function trun(track, offset) {
  8967. var samples = track.samples || [];
  8968. var sampleCount = samples.length;
  8969. var dataSize = 12 + 16 * sampleCount;
  8970. var data = new Uint8Array(dataSize);
  8971. offset += 8 + dataSize;
  8972. data.set([0x00, 0x00, 0x0F, 0x01, // version(0) & flags
  8973. sampleCount >>> 24 & 0xFF, // sample_count
  8974. sampleCount >>> 16 & 0xFF, sampleCount >>> 8 & 0xFF, sampleCount & 0xFF, offset >>> 24 & 0xFF, // data_offset
  8975. offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF], 0);
  8976. for (var i = 0; i < sampleCount; i++) {
  8977. var duration = samples[i].duration;
  8978. var size = samples[i].size;
  8979. var flags = samples[i].flags;
  8980. var cts = samples[i].cts;
  8981. data.set([duration >>> 24 & 0xFF, // sample_duration
  8982. duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, size >>> 24 & 0xFF, // sample_size
  8983. size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF, flags.isLeading << 2 | flags.dependsOn, // sample_flags
  8984. flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.isNonSync, 0x00, 0x00, // sample_degradation_priority
  8985. cts >>> 24 & 0xFF, // sample_composition_time_offset
  8986. cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF], 12 + 16 * i);
  8987. }
  8988. return MP4.box(MP4.types.trun, data);
  8989. }
  8990. }, {
  8991. key: 'mdat',
  8992. value: function mdat(data) {
  8993. return MP4.box(MP4.types.mdat, data);
  8994. }
  8995. }]);
  8996. return MP4;
  8997. }();
  8998. MP4.init();
  8999. exports.default = MP4;
  9000. },{}],38:[function(_dereq_,module,exports){
  9001. 'use strict';
  9002. Object.defineProperty(exports, "__esModule", {
  9003. value: true
  9004. });
  9005. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  9006. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  9007. *
  9008. * @author zheng qian <xqq@xqq.im>
  9009. *
  9010. * Licensed under the Apache License, Version 2.0 (the "License");
  9011. * you may not use this file except in compliance with the License.
  9012. * You may obtain a copy of the License at
  9013. *
  9014. * http://www.apache.org/licenses/LICENSE-2.0
  9015. *
  9016. * Unless required by applicable law or agreed to in writing, software
  9017. * distributed under the License is distributed on an "AS IS" BASIS,
  9018. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9019. * See the License for the specific language governing permissions and
  9020. * limitations under the License.
  9021. */
  9022. var _logger = _dereq_('../utils/logger.js');
  9023. var _logger2 = _interopRequireDefault(_logger);
  9024. var _mp4Generator = _dereq_('./mp4-generator.js');
  9025. var _mp4Generator2 = _interopRequireDefault(_mp4Generator);
  9026. var _aacSilent = _dereq_('./aac-silent.js');
  9027. var _aacSilent2 = _interopRequireDefault(_aacSilent);
  9028. var _browser = _dereq_('../utils/browser.js');
  9029. var _browser2 = _interopRequireDefault(_browser);
  9030. var _mediaSegmentInfo = _dereq_('../core/media-segment-info.js');
  9031. var _exception = _dereq_('../utils/exception.js');
  9032. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9033. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9034. // Fragmented mp4 remuxer
  9035. var MP4Remuxer = function () {
  9036. function MP4Remuxer(config) {
  9037. _classCallCheck(this, MP4Remuxer);
  9038. this.TAG = 'MP4Remuxer';
  9039. this._config = config;
  9040. this._isLive = config.isLive === true ? true : false;
  9041. this._dtsBase = -1;
  9042. this._dtsBaseInited = false;
  9043. this._audioDtsBase = Infinity;
  9044. this._videoDtsBase = Infinity;
  9045. this._audioNextDts = undefined;
  9046. this._videoNextDts = undefined;
  9047. this._audioStashedLastSample = null;
  9048. this._videoStashedLastSample = null;
  9049. this._audioMeta = null;
  9050. this._videoMeta = null;
  9051. this._audioSegmentInfoList = new _mediaSegmentInfo.MediaSegmentInfoList('audio');
  9052. this._videoSegmentInfoList = new _mediaSegmentInfo.MediaSegmentInfoList('video');
  9053. this._onInitSegment = null;
  9054. this._onMediaSegment = null;
  9055. // Workaround for chrome < 50: Always force first sample as a Random Access Point in media segment
  9056. // see https://bugs.chromium.org/p/chromium/issues/detail?id=229412
  9057. this._forceFirstIDR = _browser2.default.chrome && (_browser2.default.version.major < 50 || _browser2.default.version.major === 50 && _browser2.default.version.build < 2661) ? true : false;
  9058. // Workaround for IE11/Edge: Fill silent aac frame after keyframe-seeking
  9059. // Make audio beginDts equals with video beginDts, in order to fix seek freeze
  9060. this._fillSilentAfterSeek = _browser2.default.msedge || _browser2.default.msie;
  9061. // While only FireFox supports 'audio/mp4, codecs="mp3"', use 'audio/mpeg' for chrome, safari, ...
  9062. this._mp3UseMpegAudio = !_browser2.default.firefox;
  9063. this._fillAudioTimestampGap = this._config.fixAudioTimestampGap;
  9064. }
  9065. _createClass(MP4Remuxer, [{
  9066. key: 'destroy',
  9067. value: function destroy() {
  9068. this._dtsBase = -1;
  9069. this._dtsBaseInited = false;
  9070. this._audioMeta = null;
  9071. this._videoMeta = null;
  9072. this._audioSegmentInfoList.clear();
  9073. this._audioSegmentInfoList = null;
  9074. this._videoSegmentInfoList.clear();
  9075. this._videoSegmentInfoList = null;
  9076. this._onInitSegment = null;
  9077. this._onMediaSegment = null;
  9078. }
  9079. }, {
  9080. key: 'bindDataSource',
  9081. value: function bindDataSource(producer) {
  9082. producer.onDataAvailable = this.remux.bind(this);
  9083. producer.onTrackMetadata = this._onTrackMetadataReceived.bind(this);
  9084. return this;
  9085. }
  9086. /* prototype: function onInitSegment(type: string, initSegment: ArrayBuffer): void
  9087. InitSegment: {
  9088. type: string,
  9089. data: ArrayBuffer,
  9090. codec: string,
  9091. container: string
  9092. }
  9093. */
  9094. }, {
  9095. key: 'insertDiscontinuity',
  9096. value: function insertDiscontinuity() {
  9097. this._audioNextDts = this._videoNextDts = undefined;
  9098. }
  9099. }, {
  9100. key: 'seek',
  9101. value: function seek(originalDts) {
  9102. this._audioStashedLastSample = null;
  9103. this._videoStashedLastSample = null;
  9104. this._videoSegmentInfoList.clear();
  9105. this._audioSegmentInfoList.clear();
  9106. }
  9107. }, {
  9108. key: 'remux',
  9109. value: function remux(audioTrack, videoTrack) {
  9110. if (!this._onMediaSegment) {
  9111. throw new _exception.IllegalStateException('MP4Remuxer: onMediaSegment callback must be specificed!');
  9112. }
  9113. if (!this._dtsBaseInited) {
  9114. this._calculateDtsBase(audioTrack, videoTrack);
  9115. }
  9116. this._remuxVideo(videoTrack);
  9117. this._remuxAudio(audioTrack);
  9118. }
  9119. }, {
  9120. key: '_onTrackMetadataReceived',
  9121. value: function _onTrackMetadataReceived(type, metadata) {
  9122. var metabox = null;
  9123. var container = 'mp4';
  9124. var codec = metadata.codec;
  9125. if (type === 'audio') {
  9126. this._audioMeta = metadata;
  9127. if (metadata.codec === 'mp3' && this._mp3UseMpegAudio) {
  9128. // 'audio/mpeg' for MP3 audio track
  9129. container = 'mpeg';
  9130. codec = '';
  9131. metabox = new Uint8Array();
  9132. } else {
  9133. // 'audio/mp4, codecs="codec"'
  9134. metabox = _mp4Generator2.default.generateInitSegment(metadata);
  9135. }
  9136. } else if (type === 'video') {
  9137. this._videoMeta = metadata;
  9138. metabox = _mp4Generator2.default.generateInitSegment(metadata);
  9139. } else {
  9140. return;
  9141. }
  9142. // dispatch metabox (Initialization Segment)
  9143. if (!this._onInitSegment) {
  9144. throw new _exception.IllegalStateException('MP4Remuxer: onInitSegment callback must be specified!');
  9145. }
  9146. this._onInitSegment(type, {
  9147. type: type,
  9148. data: metabox.buffer,
  9149. codec: codec,
  9150. container: type + '/' + container,
  9151. mediaDuration: metadata.duration // in timescale 1000 (milliseconds)
  9152. });
  9153. }
  9154. }, {
  9155. key: '_calculateDtsBase',
  9156. value: function _calculateDtsBase(audioTrack, videoTrack) {
  9157. if (this._dtsBaseInited) {
  9158. return;
  9159. }
  9160. if (audioTrack.samples && audioTrack.samples.length) {
  9161. this._audioDtsBase = audioTrack.samples[0].dts;
  9162. }
  9163. if (videoTrack.samples && videoTrack.samples.length) {
  9164. this._videoDtsBase = videoTrack.samples[0].dts;
  9165. }
  9166. this._dtsBase = Math.min(this._audioDtsBase, this._videoDtsBase);
  9167. this._dtsBaseInited = true;
  9168. }
  9169. }, {
  9170. key: 'flushStashedSamples',
  9171. value: function flushStashedSamples() {
  9172. var videoSample = this._videoStashedLastSample;
  9173. var audioSample = this._audioStashedLastSample;
  9174. var videoTrack = {
  9175. type: 'video',
  9176. id: 1,
  9177. sequenceNumber: 0,
  9178. samples: [],
  9179. length: 0
  9180. };
  9181. if (videoSample != null) {
  9182. videoTrack.samples.push(videoSample);
  9183. videoTrack.length = videoSample.length;
  9184. }
  9185. var audioTrack = {
  9186. type: 'audio',
  9187. id: 2,
  9188. sequenceNumber: 0,
  9189. samples: [],
  9190. length: 0
  9191. };
  9192. if (audioSample != null) {
  9193. audioTrack.samples.push(audioSample);
  9194. audioTrack.length = audioSample.length;
  9195. }
  9196. this._videoStashedLastSample = null;
  9197. this._audioStashedLastSample = null;
  9198. this._remuxVideo(videoTrack, true);
  9199. this._remuxAudio(audioTrack, true);
  9200. }
  9201. }, {
  9202. key: '_remuxAudio',
  9203. value: function _remuxAudio(audioTrack, force) {
  9204. if (this._audioMeta == null) {
  9205. return;
  9206. }
  9207. var track = audioTrack;
  9208. var samples = track.samples;
  9209. var dtsCorrection = undefined;
  9210. var firstDts = -1,
  9211. lastDts = -1,
  9212. lastPts = -1;
  9213. var refSampleDuration = this._audioMeta.refSampleDuration;
  9214. var mpegRawTrack = this._audioMeta.codec === 'mp3' && this._mp3UseMpegAudio;
  9215. var firstSegmentAfterSeek = this._dtsBaseInited && this._audioNextDts === undefined;
  9216. var insertPrefixSilentFrame = false;
  9217. if (!samples || samples.length === 0) {
  9218. return;
  9219. }
  9220. if (samples.length === 1 && !force) {
  9221. // If [sample count in current batch] === 1 && (force != true)
  9222. // Ignore and keep in demuxer's queue
  9223. return;
  9224. } // else if (force === true) do remux
  9225. var offset = 0;
  9226. var mdatbox = null;
  9227. var mdatBytes = 0;
  9228. // calculate initial mdat size
  9229. if (mpegRawTrack) {
  9230. // for raw mpeg buffer
  9231. offset = 0;
  9232. mdatBytes = track.length;
  9233. } else {
  9234. // for fmp4 mdat box
  9235. offset = 8; // size + type
  9236. mdatBytes = 8 + track.length;
  9237. }
  9238. var lastSample = null;
  9239. // Pop the lastSample and waiting for stash
  9240. if (samples.length > 1) {
  9241. lastSample = samples.pop();
  9242. mdatBytes -= lastSample.length;
  9243. }
  9244. // Insert [stashed lastSample in the previous batch] to the front
  9245. if (this._audioStashedLastSample != null) {
  9246. var sample = this._audioStashedLastSample;
  9247. this._audioStashedLastSample = null;
  9248. samples.unshift(sample);
  9249. mdatBytes += sample.length;
  9250. }
  9251. // Stash the lastSample of current batch, waiting for next batch
  9252. if (lastSample != null) {
  9253. this._audioStashedLastSample = lastSample;
  9254. }
  9255. var firstSampleOriginalDts = samples[0].dts - this._dtsBase;
  9256. // calculate dtsCorrection
  9257. if (this._audioNextDts) {
  9258. dtsCorrection = firstSampleOriginalDts - this._audioNextDts;
  9259. } else {
  9260. // this._audioNextDts == undefined
  9261. if (this._audioSegmentInfoList.isEmpty()) {
  9262. dtsCorrection = 0;
  9263. if (this._fillSilentAfterSeek && !this._videoSegmentInfoList.isEmpty()) {
  9264. if (this._audioMeta.originalCodec !== 'mp3') {
  9265. insertPrefixSilentFrame = true;
  9266. }
  9267. }
  9268. } else {
  9269. var _lastSample = this._audioSegmentInfoList.getLastSampleBefore(firstSampleOriginalDts);
  9270. if (_lastSample != null) {
  9271. var distance = firstSampleOriginalDts - (_lastSample.originalDts + _lastSample.duration);
  9272. if (distance <= 3) {
  9273. distance = 0;
  9274. }
  9275. var expectedDts = _lastSample.dts + _lastSample.duration + distance;
  9276. dtsCorrection = firstSampleOriginalDts - expectedDts;
  9277. } else {
  9278. // lastSample == null, cannot found
  9279. dtsCorrection = 0;
  9280. }
  9281. }
  9282. }
  9283. if (insertPrefixSilentFrame) {
  9284. // align audio segment beginDts to match with current video segment's beginDts
  9285. var firstSampleDts = firstSampleOriginalDts - dtsCorrection;
  9286. var videoSegment = this._videoSegmentInfoList.getLastSegmentBefore(firstSampleOriginalDts);
  9287. if (videoSegment != null && videoSegment.beginDts < firstSampleDts) {
  9288. var silentUnit = _aacSilent2.default.getSilentFrame(this._audioMeta.originalCodec, this._audioMeta.channelCount);
  9289. if (silentUnit) {
  9290. var dts = videoSegment.beginDts;
  9291. var silentFrameDuration = firstSampleDts - videoSegment.beginDts;
  9292. _logger2.default.v(this.TAG, 'InsertPrefixSilentAudio: dts: ' + dts + ', duration: ' + silentFrameDuration);
  9293. samples.unshift({ unit: silentUnit, dts: dts, pts: dts });
  9294. mdatBytes += silentUnit.byteLength;
  9295. } // silentUnit == null: Cannot generate, skip
  9296. } else {
  9297. insertPrefixSilentFrame = false;
  9298. }
  9299. }
  9300. var mp4Samples = [];
  9301. // Correct dts for each sample, and calculate sample duration. Then output to mp4Samples
  9302. for (var i = 0; i < samples.length; i++) {
  9303. var _sample = samples[i];
  9304. var unit = _sample.unit;
  9305. var originalDts = _sample.dts - this._dtsBase;
  9306. var _dts = originalDts - dtsCorrection;
  9307. if (firstDts === -1) {
  9308. firstDts = _dts;
  9309. }
  9310. var sampleDuration = 0;
  9311. if (i !== samples.length - 1) {
  9312. var nextDts = samples[i + 1].dts - this._dtsBase - dtsCorrection;
  9313. sampleDuration = nextDts - _dts;
  9314. } else {
  9315. // the last sample
  9316. if (lastSample != null) {
  9317. // use stashed sample's dts to calculate sample duration
  9318. var _nextDts = lastSample.dts - this._dtsBase - dtsCorrection;
  9319. sampleDuration = _nextDts - _dts;
  9320. } else if (mp4Samples.length >= 1) {
  9321. // use second last sample duration
  9322. sampleDuration = mp4Samples[mp4Samples.length - 1].duration;
  9323. } else {
  9324. // the only one sample, use reference sample duration
  9325. sampleDuration = Math.floor(refSampleDuration);
  9326. }
  9327. }
  9328. var needFillSilentFrames = false;
  9329. var silentFrames = null;
  9330. // Silent frame generation, if large timestamp gap detected && config.fixAudioTimestampGap
  9331. if (sampleDuration > refSampleDuration * 1.5 && this._audioMeta.codec !== 'mp3' && this._fillAudioTimestampGap && !_browser2.default.safari) {
  9332. // We need to insert silent frames to fill timestamp gap
  9333. needFillSilentFrames = true;
  9334. var delta = Math.abs(sampleDuration - refSampleDuration);
  9335. var frameCount = Math.ceil(delta / refSampleDuration);
  9336. var currentDts = _dts + refSampleDuration; // Notice: in float
  9337. _logger2.default.w(this.TAG, 'Large audio timestamp gap detected, may cause AV sync to drift. ' + 'Silent frames will be generated to avoid unsync.\n' + ('dts: ' + (_dts + sampleDuration) + ' ms, expected: ' + (_dts + Math.round(refSampleDuration)) + ' ms, ') + ('delta: ' + Math.round(delta) + ' ms, generate: ' + frameCount + ' frames'));
  9338. var _silentUnit = _aacSilent2.default.getSilentFrame(this._audioMeta.originalCodec, this._audioMeta.channelCount);
  9339. if (_silentUnit == null) {
  9340. _logger2.default.w(this.TAG, 'Unable to generate silent frame for ' + (this._audioMeta.originalCodec + ' with ' + this._audioMeta.channelCount + ' channels, repeat last frame'));
  9341. // Repeat last frame
  9342. _silentUnit = unit;
  9343. }
  9344. silentFrames = [];
  9345. for (var j = 0; j < frameCount; j++) {
  9346. var intDts = Math.round(currentDts); // round to integer
  9347. if (silentFrames.length > 0) {
  9348. // Set previous frame sample duration
  9349. var previousFrame = silentFrames[silentFrames.length - 1];
  9350. previousFrame.duration = intDts - previousFrame.dts;
  9351. }
  9352. var frame = {
  9353. dts: intDts,
  9354. pts: intDts,
  9355. cts: 0,
  9356. unit: _silentUnit,
  9357. size: _silentUnit.byteLength,
  9358. duration: 0, // wait for next sample
  9359. originalDts: originalDts,
  9360. flags: {
  9361. isLeading: 0,
  9362. dependsOn: 1,
  9363. isDependedOn: 0,
  9364. hasRedundancy: 0
  9365. }
  9366. };
  9367. silentFrames.push(frame);
  9368. mdatBytes += unit.byteLength;
  9369. currentDts += refSampleDuration;
  9370. }
  9371. // last frame: align end time to next frame dts
  9372. var lastFrame = silentFrames[silentFrames.length - 1];
  9373. lastFrame.duration = _dts + sampleDuration - lastFrame.dts;
  9374. // silentFrames.forEach((frame) => {
  9375. // Log.w(this.TAG, `SilentAudio: dts: ${frame.dts}, duration: ${frame.duration}`);
  9376. // });
  9377. // Set correct sample duration for current frame
  9378. sampleDuration = Math.round(refSampleDuration);
  9379. }
  9380. mp4Samples.push({
  9381. dts: _dts,
  9382. pts: _dts,
  9383. cts: 0,
  9384. unit: _sample.unit,
  9385. size: _sample.unit.byteLength,
  9386. duration: sampleDuration,
  9387. originalDts: originalDts,
  9388. flags: {
  9389. isLeading: 0,
  9390. dependsOn: 1,
  9391. isDependedOn: 0,
  9392. hasRedundancy: 0
  9393. }
  9394. });
  9395. if (needFillSilentFrames) {
  9396. // Silent frames should be inserted after wrong-duration frame
  9397. mp4Samples.push.apply(mp4Samples, silentFrames);
  9398. }
  9399. }
  9400. // allocate mdatbox
  9401. if (mpegRawTrack) {
  9402. // allocate for raw mpeg buffer
  9403. mdatbox = new Uint8Array(mdatBytes);
  9404. } else {
  9405. // allocate for fmp4 mdat box
  9406. mdatbox = new Uint8Array(mdatBytes);
  9407. // size field
  9408. mdatbox[0] = mdatBytes >>> 24 & 0xFF;
  9409. mdatbox[1] = mdatBytes >>> 16 & 0xFF;
  9410. mdatbox[2] = mdatBytes >>> 8 & 0xFF;
  9411. mdatbox[3] = mdatBytes & 0xFF;
  9412. // type field (fourCC)
  9413. mdatbox.set(_mp4Generator2.default.types.mdat, 4);
  9414. }
  9415. // Write samples into mdatbox
  9416. for (var _i = 0; _i < mp4Samples.length; _i++) {
  9417. var _unit = mp4Samples[_i].unit;
  9418. mdatbox.set(_unit, offset);
  9419. offset += _unit.byteLength;
  9420. }
  9421. var latest = mp4Samples[mp4Samples.length - 1];
  9422. lastDts = latest.dts + latest.duration;
  9423. this._audioNextDts = lastDts;
  9424. // fill media segment info & add to info list
  9425. var info = new _mediaSegmentInfo.MediaSegmentInfo();
  9426. info.beginDts = firstDts;
  9427. info.endDts = lastDts;
  9428. info.beginPts = firstDts;
  9429. info.endPts = lastDts;
  9430. info.originalBeginDts = mp4Samples[0].originalDts;
  9431. info.originalEndDts = latest.originalDts + latest.duration;
  9432. info.firstSample = new _mediaSegmentInfo.SampleInfo(mp4Samples[0].dts, mp4Samples[0].pts, mp4Samples[0].duration, mp4Samples[0].originalDts, false);
  9433. info.lastSample = new _mediaSegmentInfo.SampleInfo(latest.dts, latest.pts, latest.duration, latest.originalDts, false);
  9434. if (!this._isLive) {
  9435. this._audioSegmentInfoList.append(info);
  9436. }
  9437. track.samples = mp4Samples;
  9438. track.sequenceNumber++;
  9439. var moofbox = null;
  9440. if (mpegRawTrack) {
  9441. // Generate empty buffer, because useless for raw mpeg
  9442. moofbox = new Uint8Array();
  9443. } else {
  9444. // Generate moof for fmp4 segment
  9445. moofbox = _mp4Generator2.default.moof(track, firstDts);
  9446. }
  9447. track.samples = [];
  9448. track.length = 0;
  9449. var segment = {
  9450. type: 'audio',
  9451. data: this._mergeBoxes(moofbox, mdatbox).buffer,
  9452. sampleCount: mp4Samples.length,
  9453. info: info
  9454. };
  9455. if (mpegRawTrack && firstSegmentAfterSeek) {
  9456. // For MPEG audio stream in MSE, if seeking occurred, before appending new buffer
  9457. // We need explicitly set timestampOffset to the desired point in timeline for mpeg SourceBuffer.
  9458. segment.timestampOffset = firstDts;
  9459. }
  9460. this._onMediaSegment('audio', segment);
  9461. }
  9462. }, {
  9463. key: '_remuxVideo',
  9464. value: function _remuxVideo(videoTrack, force) {
  9465. if (this._videoMeta == null) {
  9466. return;
  9467. }
  9468. var track = videoTrack;
  9469. var samples = track.samples;
  9470. var dtsCorrection = undefined;
  9471. var firstDts = -1,
  9472. lastDts = -1;
  9473. var firstPts = -1,
  9474. lastPts = -1;
  9475. if (!samples || samples.length === 0) {
  9476. return;
  9477. }
  9478. if (samples.length === 1 && !force) {
  9479. // If [sample count in current batch] === 1 && (force != true)
  9480. // Ignore and keep in demuxer's queue
  9481. return;
  9482. } // else if (force === true) do remux
  9483. var offset = 8;
  9484. var mdatbox = null;
  9485. var mdatBytes = 8 + videoTrack.length;
  9486. var lastSample = null;
  9487. // Pop the lastSample and waiting for stash
  9488. if (samples.length > 1) {
  9489. lastSample = samples.pop();
  9490. mdatBytes -= lastSample.length;
  9491. }
  9492. // Insert [stashed lastSample in the previous batch] to the front
  9493. if (this._videoStashedLastSample != null) {
  9494. var sample = this._videoStashedLastSample;
  9495. this._videoStashedLastSample = null;
  9496. samples.unshift(sample);
  9497. mdatBytes += sample.length;
  9498. }
  9499. // Stash the lastSample of current batch, waiting for next batch
  9500. if (lastSample != null) {
  9501. this._videoStashedLastSample = lastSample;
  9502. }
  9503. var firstSampleOriginalDts = samples[0].dts - this._dtsBase;
  9504. // calculate dtsCorrection
  9505. if (this._videoNextDts) {
  9506. dtsCorrection = firstSampleOriginalDts - this._videoNextDts;
  9507. } else {
  9508. // this._videoNextDts == undefined
  9509. if (this._videoSegmentInfoList.isEmpty()) {
  9510. dtsCorrection = 0;
  9511. } else {
  9512. var _lastSample2 = this._videoSegmentInfoList.getLastSampleBefore(firstSampleOriginalDts);
  9513. if (_lastSample2 != null) {
  9514. var distance = firstSampleOriginalDts - (_lastSample2.originalDts + _lastSample2.duration);
  9515. if (distance <= 3) {
  9516. distance = 0;
  9517. }
  9518. var expectedDts = _lastSample2.dts + _lastSample2.duration + distance;
  9519. dtsCorrection = firstSampleOriginalDts - expectedDts;
  9520. } else {
  9521. // lastSample == null, cannot found
  9522. dtsCorrection = 0;
  9523. }
  9524. }
  9525. }
  9526. var info = new _mediaSegmentInfo.MediaSegmentInfo();
  9527. var mp4Samples = [];
  9528. // Correct dts for each sample, and calculate sample duration. Then output to mp4Samples
  9529. for (var i = 0; i < samples.length; i++) {
  9530. var _sample2 = samples[i];
  9531. var originalDts = _sample2.dts - this._dtsBase;
  9532. var isKeyframe = _sample2.isKeyframe;
  9533. var dts = originalDts - dtsCorrection;
  9534. var cts = _sample2.cts;
  9535. var pts = dts + cts;
  9536. if (firstDts === -1) {
  9537. firstDts = dts;
  9538. firstPts = pts;
  9539. }
  9540. var sampleDuration = 0;
  9541. if (i !== samples.length - 1) {
  9542. var nextDts = samples[i + 1].dts - this._dtsBase - dtsCorrection;
  9543. sampleDuration = nextDts - dts;
  9544. } else {
  9545. // the last sample
  9546. if (lastSample != null) {
  9547. // use stashed sample's dts to calculate sample duration
  9548. var _nextDts2 = lastSample.dts - this._dtsBase - dtsCorrection;
  9549. sampleDuration = _nextDts2 - dts;
  9550. } else if (mp4Samples.length >= 1) {
  9551. // use second last sample duration
  9552. sampleDuration = mp4Samples[mp4Samples.length - 1].duration;
  9553. } else {
  9554. // the only one sample, use reference sample duration
  9555. sampleDuration = Math.floor(this._videoMeta.refSampleDuration);
  9556. }
  9557. }
  9558. if (isKeyframe) {
  9559. var syncPoint = new _mediaSegmentInfo.SampleInfo(dts, pts, sampleDuration, _sample2.dts, true);
  9560. syncPoint.fileposition = _sample2.fileposition;
  9561. info.appendSyncPoint(syncPoint);
  9562. }
  9563. mp4Samples.push({
  9564. dts: dts,
  9565. pts: pts,
  9566. cts: cts,
  9567. units: _sample2.units,
  9568. size: _sample2.length,
  9569. isKeyframe: isKeyframe,
  9570. duration: sampleDuration,
  9571. originalDts: originalDts,
  9572. flags: {
  9573. isLeading: 0,
  9574. dependsOn: isKeyframe ? 2 : 1,
  9575. isDependedOn: isKeyframe ? 1 : 0,
  9576. hasRedundancy: 0,
  9577. isNonSync: isKeyframe ? 0 : 1
  9578. }
  9579. });
  9580. }
  9581. // allocate mdatbox
  9582. mdatbox = new Uint8Array(mdatBytes);
  9583. mdatbox[0] = mdatBytes >>> 24 & 0xFF;
  9584. mdatbox[1] = mdatBytes >>> 16 & 0xFF;
  9585. mdatbox[2] = mdatBytes >>> 8 & 0xFF;
  9586. mdatbox[3] = mdatBytes & 0xFF;
  9587. mdatbox.set(_mp4Generator2.default.types.mdat, 4);
  9588. // Write samples into mdatbox
  9589. for (var _i2 = 0; _i2 < mp4Samples.length; _i2++) {
  9590. var units = mp4Samples[_i2].units;
  9591. while (units.length) {
  9592. var unit = units.shift();
  9593. var data = unit.data;
  9594. mdatbox.set(data, offset);
  9595. offset += data.byteLength;
  9596. }
  9597. }
  9598. var latest = mp4Samples[mp4Samples.length - 1];
  9599. lastDts = latest.dts + latest.duration;
  9600. lastPts = latest.pts + latest.duration;
  9601. this._videoNextDts = lastDts;
  9602. // fill media segment info & add to info list
  9603. info.beginDts = firstDts;
  9604. info.endDts = lastDts;
  9605. info.beginPts = firstPts;
  9606. info.endPts = lastPts;
  9607. info.originalBeginDts = mp4Samples[0].originalDts;
  9608. info.originalEndDts = latest.originalDts + latest.duration;
  9609. info.firstSample = new _mediaSegmentInfo.SampleInfo(mp4Samples[0].dts, mp4Samples[0].pts, mp4Samples[0].duration, mp4Samples[0].originalDts, mp4Samples[0].isKeyframe);
  9610. info.lastSample = new _mediaSegmentInfo.SampleInfo(latest.dts, latest.pts, latest.duration, latest.originalDts, latest.isKeyframe);
  9611. if (!this._isLive) {
  9612. this._videoSegmentInfoList.append(info);
  9613. }
  9614. track.samples = mp4Samples;
  9615. track.sequenceNumber++;
  9616. // workaround for chrome < 50: force first sample as a random access point
  9617. // see https://bugs.chromium.org/p/chromium/issues/detail?id=229412
  9618. if (this._forceFirstIDR) {
  9619. var flags = mp4Samples[0].flags;
  9620. flags.dependsOn = 2;
  9621. flags.isNonSync = 0;
  9622. }
  9623. var moofbox = _mp4Generator2.default.moof(track, firstDts);
  9624. track.samples = [];
  9625. track.length = 0;
  9626. this._onMediaSegment('video', {
  9627. type: 'video',
  9628. data: this._mergeBoxes(moofbox, mdatbox).buffer,
  9629. sampleCount: mp4Samples.length,
  9630. info: info
  9631. });
  9632. }
  9633. }, {
  9634. key: '_mergeBoxes',
  9635. value: function _mergeBoxes(moof, mdat) {
  9636. var result = new Uint8Array(moof.byteLength + mdat.byteLength);
  9637. result.set(moof, 0);
  9638. result.set(mdat, moof.byteLength);
  9639. return result;
  9640. }
  9641. }, {
  9642. key: 'onInitSegment',
  9643. get: function get() {
  9644. return this._onInitSegment;
  9645. },
  9646. set: function set(callback) {
  9647. this._onInitSegment = callback;
  9648. }
  9649. /* prototype: function onMediaSegment(type: string, mediaSegment: MediaSegment): void
  9650. MediaSegment: {
  9651. type: string,
  9652. data: ArrayBuffer,
  9653. sampleCount: int32
  9654. info: MediaSegmentInfo
  9655. }
  9656. */
  9657. }, {
  9658. key: 'onMediaSegment',
  9659. get: function get() {
  9660. return this._onMediaSegment;
  9661. },
  9662. set: function set(callback) {
  9663. this._onMediaSegment = callback;
  9664. }
  9665. }]);
  9666. return MP4Remuxer;
  9667. }();
  9668. exports.default = MP4Remuxer;
  9669. },{"../core/media-segment-info.js":8,"../utils/browser.js":39,"../utils/exception.js":40,"../utils/logger.js":41,"./aac-silent.js":36,"./mp4-generator.js":37}],39:[function(_dereq_,module,exports){
  9670. 'use strict';
  9671. Object.defineProperty(exports, "__esModule", {
  9672. value: true
  9673. });
  9674. /*
  9675. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  9676. *
  9677. * @author zheng qian <xqq@xqq.im>
  9678. *
  9679. * Licensed under the Apache License, Version 2.0 (the "License");
  9680. * you may not use this file except in compliance with the License.
  9681. * You may obtain a copy of the License at
  9682. *
  9683. * http://www.apache.org/licenses/LICENSE-2.0
  9684. *
  9685. * Unless required by applicable law or agreed to in writing, software
  9686. * distributed under the License is distributed on an "AS IS" BASIS,
  9687. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9688. * See the License for the specific language governing permissions and
  9689. * limitations under the License.
  9690. */
  9691. var Browser = {};
  9692. function detect() {
  9693. // modified from jquery-browser-plugin
  9694. var ua = self.navigator.userAgent.toLowerCase();
  9695. var match = /(edge)\/([\w.]+)/.exec(ua) || /(opr)[\/]([\w.]+)/.exec(ua) || /(chrome)[ \/]([\w.]+)/.exec(ua) || /(iemobile)[\/]([\w.]+)/.exec(ua) || /(version)(applewebkit)[ \/]([\w.]+).*(safari)[ \/]([\w.]+)/.exec(ua) || /(webkit)[ \/]([\w.]+).*(version)[ \/]([\w.]+).*(safari)[ \/]([\w.]+)/.exec(ua) || /(webkit)[ \/]([\w.]+)/.exec(ua) || /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || /(msie) ([\w.]+)/.exec(ua) || ua.indexOf('trident') >= 0 && /(rv)(?::| )([\w.]+)/.exec(ua) || ua.indexOf('compatible') < 0 && /(firefox)[ \/]([\w.]+)/.exec(ua) || [];
  9696. var platform_match = /(ipad)/.exec(ua) || /(ipod)/.exec(ua) || /(windows phone)/.exec(ua) || /(iphone)/.exec(ua) || /(kindle)/.exec(ua) || /(android)/.exec(ua) || /(windows)/.exec(ua) || /(mac)/.exec(ua) || /(linux)/.exec(ua) || /(cros)/.exec(ua) || [];
  9697. var matched = {
  9698. browser: match[5] || match[3] || match[1] || '',
  9699. version: match[2] || match[4] || '0',
  9700. majorVersion: match[4] || match[2] || '0',
  9701. platform: platform_match[0] || ''
  9702. };
  9703. var browser = {};
  9704. if (matched.browser) {
  9705. browser[matched.browser] = true;
  9706. var versionArray = matched.majorVersion.split('.');
  9707. browser.version = {
  9708. major: parseInt(matched.majorVersion, 10),
  9709. string: matched.version
  9710. };
  9711. if (versionArray.length > 1) {
  9712. browser.version.minor = parseInt(versionArray[1], 10);
  9713. }
  9714. if (versionArray.length > 2) {
  9715. browser.version.build = parseInt(versionArray[2], 10);
  9716. }
  9717. }
  9718. if (matched.platform) {
  9719. browser[matched.platform] = true;
  9720. }
  9721. if (browser.chrome || browser.opr || browser.safari) {
  9722. browser.webkit = true;
  9723. }
  9724. // MSIE. IE11 has 'rv' identifer
  9725. if (browser.rv || browser.iemobile) {
  9726. if (browser.rv) {
  9727. delete browser.rv;
  9728. }
  9729. var msie = 'msie';
  9730. matched.browser = msie;
  9731. browser[msie] = true;
  9732. }
  9733. // Microsoft Edge
  9734. if (browser.edge) {
  9735. delete browser.edge;
  9736. var msedge = 'msedge';
  9737. matched.browser = msedge;
  9738. browser[msedge] = true;
  9739. }
  9740. // Opera 15+
  9741. if (browser.opr) {
  9742. var opera = 'opera';
  9743. matched.browser = opera;
  9744. browser[opera] = true;
  9745. }
  9746. // Stock android browsers are marked as Safari
  9747. if (browser.safari && browser.android) {
  9748. var android = 'android';
  9749. matched.browser = android;
  9750. browser[android] = true;
  9751. }
  9752. browser.name = matched.browser;
  9753. browser.platform = matched.platform;
  9754. for (var key in Browser) {
  9755. if (Browser.hasOwnProperty(key)) {
  9756. delete Browser[key];
  9757. }
  9758. }
  9759. Object.assign(Browser, browser);
  9760. }
  9761. detect();
  9762. exports.default = Browser;
  9763. },{}],40:[function(_dereq_,module,exports){
  9764. 'use strict';
  9765. Object.defineProperty(exports, "__esModule", {
  9766. value: true
  9767. });
  9768. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  9769. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  9770. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  9771. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9772. /*
  9773. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  9774. *
  9775. * @author zheng qian <xqq@xqq.im>
  9776. *
  9777. * Licensed under the Apache License, Version 2.0 (the "License");
  9778. * you may not use this file except in compliance with the License.
  9779. * You may obtain a copy of the License at
  9780. *
  9781. * http://www.apache.org/licenses/LICENSE-2.0
  9782. *
  9783. * Unless required by applicable law or agreed to in writing, software
  9784. * distributed under the License is distributed on an "AS IS" BASIS,
  9785. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9786. * See the License for the specific language governing permissions and
  9787. * limitations under the License.
  9788. */
  9789. var RuntimeException = exports.RuntimeException = function () {
  9790. function RuntimeException(message) {
  9791. _classCallCheck(this, RuntimeException);
  9792. this._message = message;
  9793. }
  9794. _createClass(RuntimeException, [{
  9795. key: 'toString',
  9796. value: function toString() {
  9797. return this.name + ': ' + this.message;
  9798. }
  9799. }, {
  9800. key: 'name',
  9801. get: function get() {
  9802. return 'RuntimeException';
  9803. }
  9804. }, {
  9805. key: 'message',
  9806. get: function get() {
  9807. return this._message;
  9808. }
  9809. }]);
  9810. return RuntimeException;
  9811. }();
  9812. var IllegalStateException = exports.IllegalStateException = function (_RuntimeException) {
  9813. _inherits(IllegalStateException, _RuntimeException);
  9814. function IllegalStateException(message) {
  9815. _classCallCheck(this, IllegalStateException);
  9816. return _possibleConstructorReturn(this, (IllegalStateException.__proto__ || Object.getPrototypeOf(IllegalStateException)).call(this, message));
  9817. }
  9818. _createClass(IllegalStateException, [{
  9819. key: 'name',
  9820. get: function get() {
  9821. return 'IllegalStateException';
  9822. }
  9823. }]);
  9824. return IllegalStateException;
  9825. }(RuntimeException);
  9826. var InvalidArgumentException = exports.InvalidArgumentException = function (_RuntimeException2) {
  9827. _inherits(InvalidArgumentException, _RuntimeException2);
  9828. function InvalidArgumentException(message) {
  9829. _classCallCheck(this, InvalidArgumentException);
  9830. return _possibleConstructorReturn(this, (InvalidArgumentException.__proto__ || Object.getPrototypeOf(InvalidArgumentException)).call(this, message));
  9831. }
  9832. _createClass(InvalidArgumentException, [{
  9833. key: 'name',
  9834. get: function get() {
  9835. return 'InvalidArgumentException';
  9836. }
  9837. }]);
  9838. return InvalidArgumentException;
  9839. }(RuntimeException);
  9840. var NotImplementedException = exports.NotImplementedException = function (_RuntimeException3) {
  9841. _inherits(NotImplementedException, _RuntimeException3);
  9842. function NotImplementedException(message) {
  9843. _classCallCheck(this, NotImplementedException);
  9844. return _possibleConstructorReturn(this, (NotImplementedException.__proto__ || Object.getPrototypeOf(NotImplementedException)).call(this, message));
  9845. }
  9846. _createClass(NotImplementedException, [{
  9847. key: 'name',
  9848. get: function get() {
  9849. return 'NotImplementedException';
  9850. }
  9851. }]);
  9852. return NotImplementedException;
  9853. }(RuntimeException);
  9854. },{}],41:[function(_dereq_,module,exports){
  9855. 'use strict';
  9856. Object.defineProperty(exports, "__esModule", {
  9857. value: true
  9858. });
  9859. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  9860. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  9861. *
  9862. * @author zheng qian <xqq@xqq.im>
  9863. *
  9864. * Licensed under the Apache License, Version 2.0 (the "License");
  9865. * you may not use this file except in compliance with the License.
  9866. * You may obtain a copy of the License at
  9867. *
  9868. * http://www.apache.org/licenses/LICENSE-2.0
  9869. *
  9870. * Unless required by applicable law or agreed to in writing, software
  9871. * distributed under the License is distributed on an "AS IS" BASIS,
  9872. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9873. * See the License for the specific language governing permissions and
  9874. * limitations under the License.
  9875. */
  9876. var _events = _dereq_('events');
  9877. var _events2 = _interopRequireDefault(_events);
  9878. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9879. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  9880. var Log = function () {
  9881. function Log() {
  9882. _classCallCheck(this, Log);
  9883. }
  9884. _createClass(Log, null, [{
  9885. key: 'e',
  9886. value: function e(tag, msg) {
  9887. if (!tag || Log.FORCE_GLOBAL_TAG) tag = Log.GLOBAL_TAG;
  9888. var str = '[' + tag + '] > ' + msg;
  9889. if (Log.ENABLE_CALLBACK) {
  9890. Log.emitter.emit('log', 'error', str);
  9891. }
  9892. if (!Log.ENABLE_ERROR) {
  9893. return;
  9894. }
  9895. if (console.error) {
  9896. console.error(str);
  9897. } else if (console.warn) {
  9898. console.warn(str);
  9899. } else {
  9900. console.log(str);
  9901. }
  9902. }
  9903. }, {
  9904. key: 'i',
  9905. value: function i(tag, msg) {
  9906. if (!tag || Log.FORCE_GLOBAL_TAG) tag = Log.GLOBAL_TAG;
  9907. var str = '[' + tag + '] > ' + msg;
  9908. if (Log.ENABLE_CALLBACK) {
  9909. Log.emitter.emit('log', 'info', str);
  9910. }
  9911. if (!Log.ENABLE_INFO) {
  9912. return;
  9913. }
  9914. if (console.info) {
  9915. console.info(str);
  9916. } else {
  9917. console.log(str);
  9918. }
  9919. }
  9920. }, {
  9921. key: 'w',
  9922. value: function w(tag, msg) {
  9923. if (!tag || Log.FORCE_GLOBAL_TAG) tag = Log.GLOBAL_TAG;
  9924. var str = '[' + tag + '] > ' + msg;
  9925. if (Log.ENABLE_CALLBACK) {
  9926. Log.emitter.emit('log', 'warn', str);
  9927. }
  9928. if (!Log.ENABLE_WARN) {
  9929. return;
  9930. }
  9931. if (console.warn) {
  9932. console.warn(str);
  9933. } else {
  9934. console.log(str);
  9935. }
  9936. }
  9937. }, {
  9938. key: 'd',
  9939. value: function d(tag, msg) {
  9940. if (!tag || Log.FORCE_GLOBAL_TAG) tag = Log.GLOBAL_TAG;
  9941. var str = '[' + tag + '] > ' + msg;
  9942. if (Log.ENABLE_CALLBACK) {
  9943. Log.emitter.emit('log', 'debug', str);
  9944. }
  9945. if (!Log.ENABLE_DEBUG) {
  9946. return;
  9947. }
  9948. if (console.debug) {
  9949. console.debug(str);
  9950. } else {
  9951. console.log(str);
  9952. }
  9953. }
  9954. }, {
  9955. key: 'v',
  9956. value: function v(tag, msg) {
  9957. if (!tag || Log.FORCE_GLOBAL_TAG) tag = Log.GLOBAL_TAG;
  9958. var str = '[' + tag + '] > ' + msg;
  9959. if (Log.ENABLE_CALLBACK) {
  9960. Log.emitter.emit('log', 'verbose', str);
  9961. }
  9962. if (!Log.ENABLE_VERBOSE) {
  9963. return;
  9964. }
  9965. console.log(str);
  9966. }
  9967. }]);
  9968. return Log;
  9969. }();
  9970. Log.GLOBAL_TAG = 'flv.js';
  9971. Log.FORCE_GLOBAL_TAG = false;
  9972. Log.ENABLE_ERROR = true;
  9973. Log.ENABLE_INFO = true;
  9974. Log.ENABLE_WARN = true;
  9975. Log.ENABLE_DEBUG = true;
  9976. Log.ENABLE_VERBOSE = true;
  9977. Log.ENABLE_CALLBACK = false;
  9978. Log.emitter = new _events2.default();
  9979. exports.default = Log;
  9980. },{"events":2}],42:[function(_dereq_,module,exports){
  9981. 'use strict';
  9982. Object.defineProperty(exports, "__esModule", {
  9983. value: true
  9984. });
  9985. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /*
  9986. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  9987. *
  9988. * @author zheng qian <xqq@xqq.im>
  9989. *
  9990. * Licensed under the Apache License, Version 2.0 (the "License");
  9991. * you may not use this file except in compliance with the License.
  9992. * You may obtain a copy of the License at
  9993. *
  9994. * http://www.apache.org/licenses/LICENSE-2.0
  9995. *
  9996. * Unless required by applicable law or agreed to in writing, software
  9997. * distributed under the License is distributed on an "AS IS" BASIS,
  9998. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9999. * See the License for the specific language governing permissions and
  10000. * limitations under the License.
  10001. */
  10002. var _events = _dereq_('events');
  10003. var _events2 = _interopRequireDefault(_events);
  10004. var _logger = _dereq_('./logger.js');
  10005. var _logger2 = _interopRequireDefault(_logger);
  10006. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10007. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10008. var LoggingControl = function () {
  10009. function LoggingControl() {
  10010. _classCallCheck(this, LoggingControl);
  10011. }
  10012. _createClass(LoggingControl, null, [{
  10013. key: 'getConfig',
  10014. value: function getConfig() {
  10015. return {
  10016. globalTag: _logger2.default.GLOBAL_TAG,
  10017. forceGlobalTag: _logger2.default.FORCE_GLOBAL_TAG,
  10018. enableVerbose: _logger2.default.ENABLE_VERBOSE,
  10019. enableDebug: _logger2.default.ENABLE_DEBUG,
  10020. enableInfo: _logger2.default.ENABLE_INFO,
  10021. enableWarn: _logger2.default.ENABLE_WARN,
  10022. enableError: _logger2.default.ENABLE_ERROR,
  10023. enableCallback: _logger2.default.ENABLE_CALLBACK
  10024. };
  10025. }
  10026. }, {
  10027. key: 'applyConfig',
  10028. value: function applyConfig(config) {
  10029. _logger2.default.GLOBAL_TAG = config.globalTag;
  10030. _logger2.default.FORCE_GLOBAL_TAG = config.forceGlobalTag;
  10031. _logger2.default.ENABLE_VERBOSE = config.enableVerbose;
  10032. _logger2.default.ENABLE_DEBUG = config.enableDebug;
  10033. _logger2.default.ENABLE_INFO = config.enableInfo;
  10034. _logger2.default.ENABLE_WARN = config.enableWarn;
  10035. _logger2.default.ENABLE_ERROR = config.enableError;
  10036. _logger2.default.ENABLE_CALLBACK = config.enableCallback;
  10037. }
  10038. }, {
  10039. key: '_notifyChange',
  10040. value: function _notifyChange() {
  10041. var emitter = LoggingControl.emitter;
  10042. if (emitter.listenerCount('change') > 0) {
  10043. var config = LoggingControl.getConfig();
  10044. emitter.emit('change', config);
  10045. }
  10046. }
  10047. }, {
  10048. key: 'registerListener',
  10049. value: function registerListener(listener) {
  10050. LoggingControl.emitter.addListener('change', listener);
  10051. }
  10052. }, {
  10053. key: 'removeListener',
  10054. value: function removeListener(listener) {
  10055. LoggingControl.emitter.removeListener('change', listener);
  10056. }
  10057. }, {
  10058. key: 'addLogListener',
  10059. value: function addLogListener(listener) {
  10060. _logger2.default.emitter.addListener('log', listener);
  10061. if (_logger2.default.emitter.listenerCount('log') > 0) {
  10062. _logger2.default.ENABLE_CALLBACK = true;
  10063. LoggingControl._notifyChange();
  10064. }
  10065. }
  10066. }, {
  10067. key: 'removeLogListener',
  10068. value: function removeLogListener(listener) {
  10069. _logger2.default.emitter.removeListener('log', listener);
  10070. if (_logger2.default.emitter.listenerCount('log') === 0) {
  10071. _logger2.default.ENABLE_CALLBACK = false;
  10072. LoggingControl._notifyChange();
  10073. }
  10074. }
  10075. }, {
  10076. key: 'forceGlobalTag',
  10077. get: function get() {
  10078. return _logger2.default.FORCE_GLOBAL_TAG;
  10079. },
  10080. set: function set(enable) {
  10081. _logger2.default.FORCE_GLOBAL_TAG = enable;
  10082. LoggingControl._notifyChange();
  10083. }
  10084. }, {
  10085. key: 'globalTag',
  10086. get: function get() {
  10087. return _logger2.default.GLOBAL_TAG;
  10088. },
  10089. set: function set(tag) {
  10090. _logger2.default.GLOBAL_TAG = tag;
  10091. LoggingControl._notifyChange();
  10092. }
  10093. }, {
  10094. key: 'enableAll',
  10095. get: function get() {
  10096. return _logger2.default.ENABLE_VERBOSE && _logger2.default.ENABLE_DEBUG && _logger2.default.ENABLE_INFO && _logger2.default.ENABLE_WARN && _logger2.default.ENABLE_ERROR;
  10097. },
  10098. set: function set(enable) {
  10099. _logger2.default.ENABLE_VERBOSE = enable;
  10100. _logger2.default.ENABLE_DEBUG = enable;
  10101. _logger2.default.ENABLE_INFO = enable;
  10102. _logger2.default.ENABLE_WARN = enable;
  10103. _logger2.default.ENABLE_ERROR = enable;
  10104. LoggingControl._notifyChange();
  10105. }
  10106. }, {
  10107. key: 'enableDebug',
  10108. get: function get() {
  10109. return _logger2.default.ENABLE_DEBUG;
  10110. },
  10111. set: function set(enable) {
  10112. _logger2.default.ENABLE_DEBUG = enable;
  10113. LoggingControl._notifyChange();
  10114. }
  10115. }, {
  10116. key: 'enableVerbose',
  10117. get: function get() {
  10118. return _logger2.default.ENABLE_VERBOSE;
  10119. },
  10120. set: function set(enable) {
  10121. _logger2.default.ENABLE_VERBOSE = enable;
  10122. LoggingControl._notifyChange();
  10123. }
  10124. }, {
  10125. key: 'enableInfo',
  10126. get: function get() {
  10127. return _logger2.default.ENABLE_INFO;
  10128. },
  10129. set: function set(enable) {
  10130. _logger2.default.ENABLE_INFO = enable;
  10131. LoggingControl._notifyChange();
  10132. }
  10133. }, {
  10134. key: 'enableWarn',
  10135. get: function get() {
  10136. return _logger2.default.ENABLE_WARN;
  10137. },
  10138. set: function set(enable) {
  10139. _logger2.default.ENABLE_WARN = enable;
  10140. LoggingControl._notifyChange();
  10141. }
  10142. }, {
  10143. key: 'enableError',
  10144. get: function get() {
  10145. return _logger2.default.ENABLE_ERROR;
  10146. },
  10147. set: function set(enable) {
  10148. _logger2.default.ENABLE_ERROR = enable;
  10149. LoggingControl._notifyChange();
  10150. }
  10151. }]);
  10152. return LoggingControl;
  10153. }();
  10154. LoggingControl.emitter = new _events2.default();
  10155. exports.default = LoggingControl;
  10156. },{"./logger.js":41,"events":2}],43:[function(_dereq_,module,exports){
  10157. 'use strict';
  10158. Object.defineProperty(exports, "__esModule", {
  10159. value: true
  10160. });
  10161. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  10162. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10163. /*
  10164. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  10165. *
  10166. * @author zheng qian <xqq@xqq.im>
  10167. *
  10168. * Licensed under the Apache License, Version 2.0 (the "License");
  10169. * you may not use this file except in compliance with the License.
  10170. * You may obtain a copy of the License at
  10171. *
  10172. * http://www.apache.org/licenses/LICENSE-2.0
  10173. *
  10174. * Unless required by applicable law or agreed to in writing, software
  10175. * distributed under the License is distributed on an "AS IS" BASIS,
  10176. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10177. * See the License for the specific language governing permissions and
  10178. * limitations under the License.
  10179. */
  10180. var Polyfill = function () {
  10181. function Polyfill() {
  10182. _classCallCheck(this, Polyfill);
  10183. }
  10184. _createClass(Polyfill, null, [{
  10185. key: 'install',
  10186. value: function install() {
  10187. // ES6 Object.setPrototypeOf
  10188. Object.setPrototypeOf = Object.setPrototypeOf || function (obj, proto) {
  10189. obj.__proto__ = proto;
  10190. return obj;
  10191. };
  10192. // ES6 Object.assign
  10193. Object.assign = Object.assign || function (target) {
  10194. if (target === undefined || target === null) {
  10195. throw new TypeError('Cannot convert undefined or null to object');
  10196. }
  10197. var output = Object(target);
  10198. for (var i = 1; i < arguments.length; i++) {
  10199. var source = arguments[i];
  10200. if (source !== undefined && source !== null) {
  10201. for (var key in source) {
  10202. if (source.hasOwnProperty(key)) {
  10203. output[key] = source[key];
  10204. }
  10205. }
  10206. }
  10207. }
  10208. return output;
  10209. };
  10210. // ES6 Promise (missing support in IE11)
  10211. if (typeof self.Promise !== 'function') {
  10212. _dereq_('es6-promise').polyfill();
  10213. }
  10214. }
  10215. }]);
  10216. return Polyfill;
  10217. }();
  10218. Polyfill.install();
  10219. exports.default = Polyfill;
  10220. },{"es6-promise":1}],44:[function(_dereq_,module,exports){
  10221. 'use strict';
  10222. Object.defineProperty(exports, "__esModule", {
  10223. value: true
  10224. });
  10225. /*
  10226. * Copyright (C) 2016 Bilibili. All Rights Reserved.
  10227. *
  10228. * This file is derived from C++ project libWinTF8 (https://github.com/m13253/libWinTF8)
  10229. * @author zheng qian <xqq@xqq.im>
  10230. *
  10231. * Licensed under the Apache License, Version 2.0 (the "License");
  10232. * you may not use this file except in compliance with the License.
  10233. * You may obtain a copy of the License at
  10234. *
  10235. * http://www.apache.org/licenses/LICENSE-2.0
  10236. *
  10237. * Unless required by applicable law or agreed to in writing, software
  10238. * distributed under the License is distributed on an "AS IS" BASIS,
  10239. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10240. * See the License for the specific language governing permissions and
  10241. * limitations under the License.
  10242. */
  10243. function checkContinuation(uint8array, start, checkLength) {
  10244. var array = uint8array;
  10245. if (start + checkLength < array.length) {
  10246. while (checkLength--) {
  10247. if ((array[++start] & 0xC0) !== 0x80) return false;
  10248. }
  10249. return true;
  10250. } else {
  10251. return false;
  10252. }
  10253. }
  10254. function decodeUTF8(uint8array) {
  10255. var out = [];
  10256. var input = uint8array;
  10257. var i = 0;
  10258. var length = uint8array.length;
  10259. while (i < length) {
  10260. if (input[i] < 0x80) {
  10261. out.push(String.fromCharCode(input[i]));
  10262. ++i;
  10263. continue;
  10264. } else if (input[i] < 0xC0) {
  10265. // fallthrough
  10266. } else if (input[i] < 0xE0) {
  10267. if (checkContinuation(input, i, 1)) {
  10268. var ucs4 = (input[i] & 0x1F) << 6 | input[i + 1] & 0x3F;
  10269. if (ucs4 >= 0x80) {
  10270. out.push(String.fromCharCode(ucs4 & 0xFFFF));
  10271. i += 2;
  10272. continue;
  10273. }
  10274. }
  10275. } else if (input[i] < 0xF0) {
  10276. if (checkContinuation(input, i, 2)) {
  10277. var _ucs = (input[i] & 0xF) << 12 | (input[i + 1] & 0x3F) << 6 | input[i + 2] & 0x3F;
  10278. if (_ucs >= 0x800 && (_ucs & 0xF800) !== 0xD800) {
  10279. out.push(String.fromCharCode(_ucs & 0xFFFF));
  10280. i += 3;
  10281. continue;
  10282. }
  10283. }
  10284. } else if (input[i] < 0xF8) {
  10285. if (checkContinuation(input, i, 3)) {
  10286. var _ucs2 = (input[i] & 0x7) << 18 | (input[i + 1] & 0x3F) << 12 | (input[i + 2] & 0x3F) << 6 | input[i + 3] & 0x3F;
  10287. if (_ucs2 > 0x10000 && _ucs2 < 0x110000) {
  10288. _ucs2 -= 0x10000;
  10289. out.push(String.fromCharCode(_ucs2 >>> 10 | 0xD800));
  10290. out.push(String.fromCharCode(_ucs2 & 0x3FF | 0xDC00));
  10291. i += 4;
  10292. continue;
  10293. }
  10294. }
  10295. }
  10296. out.push(String.fromCharCode(0xFFFD));
  10297. ++i;
  10298. }
  10299. return out.join('');
  10300. }
  10301. exports.default = decodeUTF8;
  10302. },{}]},{},[21])(21)
  10303. });
  10304. //# sourceMappingURL=flv.js.map