wfs.js 204 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649
  1. (function (f) {
  2. if (typeof exports === "object" && typeof module !== "undefined") {
  3. module.exports = f();
  4. } else if (typeof define === "function" && define.amd) {
  5. define([], f);
  6. } else {
  7. var g;
  8. if (typeof window !== "undefined") {
  9. g = window;
  10. } else if (typeof global !== "undefined") {
  11. g = global;
  12. } else if (typeof self !== "undefined") {
  13. g = self;
  14. } else {
  15. g = this;
  16. }
  17. g.Wfs = f();
  18. }
  19. })(function () {
  20. var define, module, exports;
  21. return (function () {
  22. function r(e, n, t) {
  23. function o(i, f) {
  24. if (!n[i]) {
  25. if (!e[i]) {
  26. var c = "function" == typeof require && require;
  27. if (!f && c) return c(i, !0);
  28. if (u) return u(i, !0);
  29. var a = new Error("Cannot find module '" + i + "'");
  30. throw ((a.code = "MODULE_NOT_FOUND"), a);
  31. }
  32. var p = (n[i] = { exports: {} });
  33. e[i][0].call(
  34. p.exports,
  35. function (r) {
  36. var n = e[i][1][r];
  37. return o(n || r);
  38. },
  39. p,
  40. p.exports,
  41. r,
  42. e,
  43. n,
  44. t
  45. );
  46. }
  47. return n[i].exports;
  48. }
  49. for (
  50. var u = "function" == typeof require && require, i = 0;
  51. i < t.length;
  52. i++
  53. )
  54. o(t[i]);
  55. return o;
  56. }
  57. return r;
  58. })()(
  59. {
  60. 1: [
  61. function (require, module, exports) {
  62. // Copyright Joyent, Inc. and other Node contributors.
  63. //
  64. // Permission is hereby granted, free of charge, to any person obtaining a
  65. // copy of this software and associated documentation files (the
  66. // "Software"), to deal in the Software without restriction, including
  67. // without limitation the rights to use, copy, modify, merge, publish,
  68. // distribute, sublicense, and/or sell copies of the Software, and to permit
  69. // persons to whom the Software is furnished to do so, subject to the
  70. // following conditions:
  71. //
  72. // The above copyright notice and this permission notice shall be included
  73. // in all copies or substantial portions of the Software.
  74. //
  75. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  76. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  77. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  78. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  79. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  80. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  81. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  82. function EventEmitter() {
  83. this._events = this._events || {};
  84. this._maxListeners = this._maxListeners || undefined;
  85. }
  86. module.exports = EventEmitter;
  87. // Backwards-compat with node 0.10.x
  88. EventEmitter.EventEmitter = EventEmitter;
  89. EventEmitter.prototype._events = undefined;
  90. EventEmitter.prototype._maxListeners = undefined;
  91. // By default EventEmitters will print a warning if more than 10 listeners are
  92. // added to it. This is a useful default which helps finding memory leaks.
  93. EventEmitter.defaultMaxListeners = 10;
  94. // Obviously not all Emitters should be limited to 10. This function allows
  95. // that to be increased. Set to zero for unlimited.
  96. EventEmitter.prototype.setMaxListeners = function (n) {
  97. if (!isNumber(n) || n < 0 || isNaN(n))
  98. throw TypeError("n must be a positive number");
  99. this._maxListeners = n;
  100. return this;
  101. };
  102. EventEmitter.prototype.emit = function (type) {
  103. var er, handler, len, args, i, listeners;
  104. if (!this._events) this._events = {};
  105. // If there is no 'error' event listener then throw.
  106. if (type === "error") {
  107. if (
  108. !this._events.error ||
  109. (isObject(this._events.error) && !this._events.error.length)
  110. ) {
  111. er = arguments[1];
  112. if (er instanceof Error) {
  113. throw er; // Unhandled 'error' event
  114. } else {
  115. // At least give some kind of context to the user
  116. var err = new Error(
  117. 'Uncaught, unspecified "error" event. (' + er + ")"
  118. );
  119. err.context = er;
  120. throw err;
  121. }
  122. }
  123. }
  124. handler = this._events[type];
  125. if (isUndefined(handler)) return false;
  126. if (isFunction(handler)) {
  127. switch (arguments.length) {
  128. // fast cases
  129. case 1:
  130. handler.call(this);
  131. break;
  132. case 2:
  133. handler.call(this, arguments[1]);
  134. break;
  135. case 3:
  136. handler.call(this, arguments[1], arguments[2]);
  137. break;
  138. // slower
  139. default:
  140. args = Array.prototype.slice.call(arguments, 1);
  141. handler.apply(this, args);
  142. }
  143. } else if (isObject(handler)) {
  144. args = Array.prototype.slice.call(arguments, 1);
  145. listeners = handler.slice();
  146. len = listeners.length;
  147. for (i = 0; i < len; i++) listeners[i].apply(this, args);
  148. }
  149. return true;
  150. };
  151. EventEmitter.prototype.addListener = function (type, listener) {
  152. var m;
  153. if (!isFunction(listener))
  154. throw TypeError("listener must be a function");
  155. if (!this._events) this._events = {};
  156. // To avoid recursion in the case that type === "newListener"! Before
  157. // adding it to the listeners, first emit "newListener".
  158. if (this._events.newListener)
  159. this.emit(
  160. "newListener",
  161. type,
  162. isFunction(listener.listener) ? listener.listener : listener
  163. );
  164. if (!this._events[type])
  165. // Optimize the case of one listener. Don't need the extra array object.
  166. this._events[type] = listener;
  167. else if (isObject(this._events[type]))
  168. // If we've already got an array, just append.
  169. this._events[type].push(listener);
  170. // Adding the second element, need to change to array.
  171. else this._events[type] = [this._events[type], listener];
  172. // Check for listener leak
  173. if (isObject(this._events[type]) && !this._events[type].warned) {
  174. if (!isUndefined(this._maxListeners)) {
  175. m = this._maxListeners;
  176. } else {
  177. m = EventEmitter.defaultMaxListeners;
  178. }
  179. if (m && m > 0 && this._events[type].length > m) {
  180. this._events[type].warned = true;
  181. console.error(
  182. "(node) warning: possible EventEmitter memory " +
  183. "leak detected. %d listeners added. " +
  184. "Use emitter.setMaxListeners() to increase limit.",
  185. this._events[type].length
  186. );
  187. if (typeof console.trace === "function") {
  188. // not supported in IE 10
  189. console.trace();
  190. }
  191. }
  192. }
  193. return this;
  194. };
  195. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  196. EventEmitter.prototype.once = function (type, listener) {
  197. if (!isFunction(listener))
  198. throw TypeError("listener must be a function");
  199. var fired = false;
  200. function g() {
  201. this.removeListener(type, g);
  202. if (!fired) {
  203. fired = true;
  204. listener.apply(this, arguments);
  205. }
  206. }
  207. g.listener = listener;
  208. this.on(type, g);
  209. return this;
  210. };
  211. // emits a 'removeListener' event iff the listener was removed
  212. EventEmitter.prototype.removeListener = function (type, listener) {
  213. var list, position, length, i;
  214. if (!isFunction(listener))
  215. throw TypeError("listener must be a function");
  216. if (!this._events || !this._events[type]) return this;
  217. list = this._events[type];
  218. length = list.length;
  219. position = -1;
  220. if (
  221. list === listener ||
  222. (isFunction(list.listener) && list.listener === listener)
  223. ) {
  224. delete this._events[type];
  225. if (this._events.removeListener)
  226. this.emit("removeListener", type, listener);
  227. } else if (isObject(list)) {
  228. for (i = length; i-- > 0;) {
  229. if (
  230. list[i] === listener ||
  231. (list[i].listener && list[i].listener === listener)
  232. ) {
  233. position = i;
  234. break;
  235. }
  236. }
  237. if (position < 0) return this;
  238. if (list.length === 1) {
  239. list.length = 0;
  240. delete this._events[type];
  241. } else {
  242. list.splice(position, 1);
  243. }
  244. if (this._events.removeListener)
  245. this.emit("removeListener", type, listener);
  246. }
  247. return this;
  248. };
  249. EventEmitter.prototype.removeAllListeners = function (type) {
  250. var key, listeners;
  251. if (!this._events) return this;
  252. // not listening for removeListener, no need to emit
  253. if (!this._events.removeListener) {
  254. if (arguments.length === 0) this._events = {};
  255. else if (this._events[type]) delete this._events[type];
  256. return this;
  257. }
  258. // emit removeListener for all listeners on all events
  259. if (arguments.length === 0) {
  260. for (key in this._events) {
  261. if (key === "removeListener") continue;
  262. this.removeAllListeners(key);
  263. }
  264. this.removeAllListeners("removeListener");
  265. this._events = {};
  266. return this;
  267. }
  268. listeners = this._events[type];
  269. if (isFunction(listeners)) {
  270. this.removeListener(type, listeners);
  271. } else if (listeners) {
  272. // LIFO order
  273. while (listeners.length)
  274. this.removeListener(type, listeners[listeners.length - 1]);
  275. }
  276. delete this._events[type];
  277. return this;
  278. };
  279. EventEmitter.prototype.listeners = function (type) {
  280. var ret;
  281. if (!this._events || !this._events[type]) ret = [];
  282. else if (isFunction(this._events[type])) ret = [this._events[type]];
  283. else ret = this._events[type].slice();
  284. return ret;
  285. };
  286. EventEmitter.prototype.listenerCount = function (type) {
  287. if (this._events) {
  288. var evlistener = this._events[type];
  289. if (isFunction(evlistener)) return 1;
  290. else if (evlistener) return evlistener.length;
  291. }
  292. return 0;
  293. };
  294. EventEmitter.listenerCount = function (emitter, type) {
  295. return emitter.listenerCount(type);
  296. };
  297. function isFunction(arg) {
  298. return typeof arg === "function";
  299. }
  300. function isNumber(arg) {
  301. return typeof arg === "number";
  302. }
  303. function isObject(arg) {
  304. return typeof arg === "object" && arg !== null;
  305. }
  306. function isUndefined(arg) {
  307. return arg === void 0;
  308. }
  309. },
  310. {}
  311. ],
  312. 2: [
  313. function (require, module, exports) {
  314. "use strict";
  315. Object.defineProperty(exports, "__esModule", {
  316. value: true
  317. });
  318. var _createClass = (function () {
  319. function defineProperties(target, props) {
  320. for (var i = 0; i < props.length; i++) {
  321. var descriptor = props[i];
  322. descriptor.enumerable = descriptor.enumerable || false;
  323. descriptor.configurable = true;
  324. if ("value" in descriptor) descriptor.writable = true;
  325. Object.defineProperty(target, descriptor.key, descriptor);
  326. }
  327. }
  328. return function (Constructor, protoProps, staticProps) {
  329. if (protoProps)
  330. defineProperties(Constructor.prototype, protoProps);
  331. if (staticProps) defineProperties(Constructor, staticProps);
  332. return Constructor;
  333. };
  334. })();
  335. var _events = require("../events");
  336. var _events2 = _interopRequireDefault(_events);
  337. var _eventHandler = require("../event-handler");
  338. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  339. function _interopRequireDefault(obj) {
  340. return obj && obj.__esModule ? obj : { default: obj };
  341. }
  342. function _classCallCheck(instance, Constructor) {
  343. if (!(instance instanceof Constructor)) {
  344. throw new TypeError("Cannot call a class as a function");
  345. }
  346. }
  347. function _possibleConstructorReturn(self, call) {
  348. if (!self) {
  349. throw new ReferenceError(
  350. "this hasn't been initialised - super() hasn't been called"
  351. );
  352. }
  353. return call &&
  354. (typeof call === "object" || typeof call === "function")
  355. ? call
  356. : self;
  357. }
  358. function _inherits(subClass, superClass) {
  359. if (typeof superClass !== "function" && superClass !== null) {
  360. throw new TypeError(
  361. "Super expression must either be null or a function, not " +
  362. typeof superClass
  363. );
  364. }
  365. subClass.prototype = Object.create(
  366. superClass && superClass.prototype,
  367. {
  368. constructor: {
  369. value: subClass,
  370. enumerable: false,
  371. writable: true,
  372. configurable: true
  373. }
  374. }
  375. );
  376. if (superClass)
  377. Object.setPrototypeOf
  378. ? Object.setPrototypeOf(subClass, superClass)
  379. : (subClass.__proto__ = superClass);
  380. }
  381. /*
  382. * Buffer Controller
  383. */
  384. var BufferController = (function (_EventHandler) {
  385. _inherits(BufferController, _EventHandler);
  386. function BufferController(wfs) {
  387. _classCallCheck(this, BufferController);
  388. var _this = _possibleConstructorReturn(
  389. this,
  390. (
  391. BufferController.__proto__ ||
  392. Object.getPrototypeOf(BufferController)
  393. ).call(
  394. this,
  395. wfs,
  396. _events2.default.MEDIA_ATTACHING,
  397. _events2.default.BUFFER_APPENDING,
  398. _events2.default.BUFFER_RESET
  399. )
  400. );
  401. _this.mediaSource = null;
  402. _this.media = null;
  403. _this.pendingTracks = {};
  404. _this.sourceBuffer = {};
  405. _this.segments = [];
  406. _this.appended = 0;
  407. _this._msDuration = null;
  408. // Source Buffer listeners
  409. _this.onsbue = _this.onSBUpdateEnd.bind(_this);
  410. _this.browserType = 0;
  411. if (navigator.userAgent.toLowerCase().indexOf("firefox") !== -1) {
  412. _this.browserType = 1;
  413. }
  414. _this.mediaType = "H264Raw";
  415. _this.url = undefined;
  416. return _this;
  417. }
  418. _createClass(BufferController, [
  419. {
  420. key: "destroy",
  421. value: function destroy() {
  422. _eventHandler2.default.prototype.destroy.call(this);
  423. }
  424. },
  425. {
  426. key: "onMediaAttaching",
  427. value: function onMediaAttaching(data) {
  428. var media = (this.media = data.media);
  429. this.mediaType = data.mediaType;
  430. this.url = data.url;
  431. if (media) {
  432. // setup the media source
  433. var ms = (this.mediaSource = new MediaSource());
  434. //Media Source listeners
  435. var me = this;
  436. this.onmso = function() {
  437. me.onMediaSourceOpen();
  438. };
  439. this.onmse = function() {
  440. me.onMediaSourceEnded();
  441. };
  442. this.onmsc = function() {
  443. me.onMediaSourceClose();
  444. };
  445. // this.onmso = this.onMediaSourceOpen.bind(this);
  446. // this.onmse = this.onMediaSourceEnded.bind(this);
  447. // this.onmsc = this.onMediaSourceClose.bind(this);
  448. ms.addEventListener("sourceopen", this.onmso);
  449. ms.addEventListener("sourceended", this.onmse);
  450. ms.addEventListener("sourceclose", this.onmsc);
  451. // link video and media Source
  452. media.src = URL.createObjectURL(ms);
  453. media.play();
  454. }
  455. }
  456. },
  457. {
  458. key: "onMediaDetaching",
  459. value: function onMediaDetaching() { }
  460. },
  461. {
  462. key: "onBufferAppending",
  463. value: function onBufferAppending(data) {
  464. if (!this.segments) {
  465. this.segments = [data];
  466. } else {
  467. this.segments.push(data);
  468. }
  469. this.doAppending();
  470. }
  471. },
  472. {
  473. key: "onMediaSourceClose",
  474. value: function onMediaSourceClose() {
  475. console.log("media source closed");
  476. }
  477. },
  478. {
  479. key: "onMediaSourceEnded",
  480. value: function onMediaSourceEnded() {
  481. console.log("media source ended");
  482. }
  483. },
  484. {
  485. key: "onSBUpdateEnd",
  486. value: function onSBUpdateEnd(event) {
  487. // Firefox
  488. // if (this.browserType === 1) {
  489. // this.mediaSource.endOfStream();
  490. // this.media.play();
  491. // }
  492. this.appending = false;
  493. this.doAppending();
  494. this.updateMediaElementDuration();
  495. }
  496. },
  497. {
  498. key: "updateMediaElementDuration",
  499. value: function updateMediaElementDuration() { }
  500. },
  501. {
  502. key: "onMediaSourceOpen",
  503. value: function onMediaSourceOpen() {
  504. var mediaSource = this.mediaSource;
  505. if (mediaSource) {
  506. // once received, don't listen anymore to sourceopen event
  507. mediaSource.removeEventListener("sourceopen", this.onmso);
  508. }
  509. if (this.mediaType === "FMp4") {
  510. this.checkPendingTracks();
  511. }
  512. this.wfs.trigger(_events2.default.MEDIA_ATTACHED, {
  513. media: this.media,
  514. mediaType: this.mediaType,
  515. url: this.url
  516. });
  517. }
  518. },
  519. {
  520. key: "checkPendingTracks",
  521. value: function checkPendingTracks() {
  522. this.createSourceBuffers({ tracks: "video", mimeType: "" });
  523. this.pendingTracks = {};
  524. }
  525. },
  526. {
  527. key: "onBufferReset",
  528. value: function onBufferReset(data) {
  529. if (this.mediaType === "H264Raw") {
  530. this.createSourceBuffers({
  531. tracks: "video",
  532. mimeType: data.mimeType
  533. });
  534. }
  535. }
  536. },
  537. {
  538. key: "createSourceBuffers",
  539. value: function createSourceBuffers(tracks) {
  540. var sourceBuffer = this.sourceBuffer,
  541. mediaSource = this.mediaSource;
  542. var mimeType = void 0;
  543. if (tracks.mimeType === "") {
  544. mimeType = "video/mp4;codecs=avc1.420028"; // avc1.42c01f avc1.42801e avc1.640028 avc1.420028
  545. } else {
  546. mimeType = "video/mp4;codecs=" + tracks.mimeType;
  547. }
  548. try {
  549. var sb = (sourceBuffer[
  550. "video"
  551. ] = mediaSource.addSourceBuffer(mimeType));
  552. sb.addEventListener("updateend", this.onsbue);
  553. track.buffer = sb;
  554. } catch (err) { }
  555. this.wfs.trigger(_events2.default.BUFFER_CREATED, {
  556. tracks: tracks
  557. });
  558. this.media.play();
  559. }
  560. },
  561. {
  562. key: "doAppending",
  563. value: function doAppending() {
  564. var wfs = this.wfs,
  565. sourceBuffer = this.sourceBuffer,
  566. segments = this.segments;
  567. if (Object.keys(sourceBuffer).length) {
  568. if (this.media.error) {
  569. this.segments = [];
  570. console.log(
  571. "trying to append although a media error occured, flush segment and abort"
  572. );
  573. return;
  574. }
  575. if (this.appending) {
  576. return;
  577. }
  578. if (segments && segments.length) {
  579. var segment = segments.shift();
  580. try {
  581. if (sourceBuffer[segment.type]) {
  582. this.parent = segment.parent;
  583. sourceBuffer[segment.type].appendBuffer(segment.data);
  584. this.appendError = 0;
  585. this.appended++;
  586. this.appending = true;
  587. } else {
  588. }
  589. } catch (err) {
  590. // in case any error occured while appending, put back segment in segments table
  591. segments.unshift(segment);
  592. var event = { type: ErrorTypes.MEDIA_ERROR };
  593. if (err.code !== 22) {
  594. if (this.appendError) {
  595. this.appendError++;
  596. } else {
  597. this.appendError = 1;
  598. }
  599. event.details = ErrorDetails.BUFFER_APPEND_ERROR;
  600. event.frag = this.fragCurrent;
  601. if (
  602. this.appendError > wfs.config.appendErrorMaxRetry
  603. ) {
  604. segments = [];
  605. event.fatal = true;
  606. return;
  607. } else {
  608. event.fatal = false;
  609. }
  610. } else {
  611. this.segments = [];
  612. event.details = ErrorDetails.BUFFER_FULL_ERROR;
  613. return;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. ]);
  621. return BufferController;
  622. })(_eventHandler2.default);
  623. exports.default = BufferController;
  624. },
  625. { "../event-handler": 7, "../events": 8 }
  626. ],
  627. 3: [
  628. function (require, module, exports) {
  629. "use strict";
  630. Object.defineProperty(exports, "__esModule", {
  631. value: true
  632. });
  633. var _createClass = (function () {
  634. function defineProperties(target, props) {
  635. for (var i = 0; i < props.length; i++) {
  636. var descriptor = props[i];
  637. descriptor.enumerable = descriptor.enumerable || false;
  638. descriptor.configurable = true;
  639. if ("value" in descriptor) descriptor.writable = true;
  640. Object.defineProperty(target, descriptor.key, descriptor);
  641. }
  642. }
  643. return function (Constructor, protoProps, staticProps) {
  644. if (protoProps)
  645. defineProperties(Constructor.prototype, protoProps);
  646. if (staticProps) defineProperties(Constructor, staticProps);
  647. return Constructor;
  648. };
  649. })();
  650. var _events = require("../events");
  651. var _events2 = _interopRequireDefault(_events);
  652. var _eventHandler = require("../event-handler");
  653. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  654. function _interopRequireDefault(obj) {
  655. return obj && obj.__esModule ? obj : { default: obj };
  656. }
  657. function _classCallCheck(instance, Constructor) {
  658. if (!(instance instanceof Constructor)) {
  659. throw new TypeError("Cannot call a class as a function");
  660. }
  661. }
  662. function _possibleConstructorReturn(self, call) {
  663. if (!self) {
  664. throw new ReferenceError(
  665. "this hasn't been initialised - super() hasn't been called"
  666. );
  667. }
  668. return call &&
  669. (typeof call === "object" || typeof call === "function")
  670. ? call
  671. : self;
  672. }
  673. function _inherits(subClass, superClass) {
  674. if (typeof superClass !== "function" && superClass !== null) {
  675. throw new TypeError(
  676. "Super expression must either be null or a function, not " +
  677. typeof superClass
  678. );
  679. }
  680. subClass.prototype = Object.create(
  681. superClass && superClass.prototype,
  682. {
  683. constructor: {
  684. value: subClass,
  685. enumerable: false,
  686. writable: true,
  687. configurable: true
  688. }
  689. }
  690. );
  691. if (superClass)
  692. Object.setPrototypeOf
  693. ? Object.setPrototypeOf(subClass, superClass)
  694. : (subClass.__proto__ = superClass);
  695. }
  696. /*
  697. * Flow Controller
  698. */
  699. var FlowController = (function (_EventHandler) {
  700. _inherits(FlowController, _EventHandler);
  701. function FlowController(wfs) {
  702. _classCallCheck(this, FlowController);
  703. var _this = _possibleConstructorReturn(
  704. this,
  705. (
  706. FlowController.__proto__ ||
  707. Object.getPrototypeOf(FlowController)
  708. ).call(
  709. this,
  710. wfs,
  711. _events2.default.MEDIA_ATTACHED,
  712. _events2.default.BUFFER_CREATED,
  713. _events2.default.FILE_PARSING_DATA,
  714. _events2.default.FILE_HEAD_LOADED,
  715. _events2.default.FILE_DATA_LOADED,
  716. _events2.default.WEBSOCKET_ATTACHED,
  717. _events2.default.FRAG_PARSING_DATA,
  718. _events2.default.FRAG_PARSING_INIT_SEGMENT
  719. )
  720. );
  721. _this.fileStart = 0;
  722. _this.fileEnd = 0;
  723. _this.pendingAppending = 0;
  724. _this.mediaType = undefined;
  725. return _this;
  726. }
  727. _createClass(FlowController, [
  728. {
  729. key: "destroy",
  730. value: function destroy() {
  731. _eventHandler2.default.prototype.destroy.call(this);
  732. }
  733. },
  734. {
  735. key: "onMediaAttached",
  736. value: function onMediaAttached(data) {
  737. if (data.url !== undefined) {
  738. var client = new WebSocket(
  739. data.url
  740. );
  741. this.wfs.attachWebsocket(client);
  742. } else {
  743. console.log("URL ERROR!!!");
  744. }
  745. }
  746. },
  747. {
  748. key: "onBufferCreated",
  749. value: function onBufferCreated(data) {
  750. this.mediaType = data.mediaType;
  751. }
  752. },
  753. {
  754. key: "onFileHeadLoaded",
  755. value: function onFileHeadLoaded(data) { }
  756. },
  757. {
  758. key: "onFileDataLoaded",
  759. value: function onFileDataLoaded(data) { }
  760. },
  761. {
  762. key: "onFileParsingData",
  763. value: function onFileParsingData(data) { }
  764. },
  765. {
  766. key: "onWebsocketAttached",
  767. value: function onWebsocketAttached(data) {
  768. this.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  769. type: "video",
  770. data: data.payload,
  771. parent: "main"
  772. });
  773. }
  774. },
  775. {
  776. key: "onFragParsingInitSegment",
  777. value: function onFragParsingInitSegment(data) {
  778. var tracks = data.tracks,
  779. trackName,
  780. track;
  781. track = tracks.video;
  782. if (track) {
  783. track.id = data.id;
  784. }
  785. for (trackName in tracks) {
  786. track = tracks[trackName];
  787. var initSegment = track.initSegment;
  788. if (initSegment) {
  789. this.pendingAppending++;
  790. this.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  791. type: trackName,
  792. data: initSegment,
  793. parent: "main"
  794. });
  795. }
  796. }
  797. }
  798. },
  799. {
  800. key: "onFragParsingData",
  801. value: function onFragParsingData(data) {
  802. var _this2 = this;
  803. if (data.type === "video") {
  804. }
  805. [data.data1, data.data2].forEach(function (buffer) {
  806. if (buffer) {
  807. _this2.pendingAppending++;
  808. _this2.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  809. type: data.type,
  810. data: buffer,
  811. parent: "main"
  812. });
  813. }
  814. });
  815. }
  816. }
  817. ]);
  818. return FlowController;
  819. })(_eventHandler2.default);
  820. exports.default = FlowController;
  821. },
  822. { "../event-handler": 7, "../events": 8 }
  823. ],
  824. 4: [
  825. function (require, module, exports) {
  826. "use strict";
  827. Object.defineProperty(exports, "__esModule", {
  828. value: true
  829. });
  830. var _createClass = (function () {
  831. function defineProperties(target, props) {
  832. for (var i = 0; i < props.length; i++) {
  833. var descriptor = props[i];
  834. descriptor.enumerable = descriptor.enumerable || false;
  835. descriptor.configurable = true;
  836. if ("value" in descriptor) descriptor.writable = true;
  837. Object.defineProperty(target, descriptor.key, descriptor);
  838. }
  839. }
  840. return function (Constructor, protoProps, staticProps) {
  841. if (protoProps)
  842. defineProperties(Constructor.prototype, protoProps);
  843. if (staticProps) defineProperties(Constructor, staticProps);
  844. return Constructor;
  845. };
  846. })();
  847. /**
  848. * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
  849. */
  850. var _logger = require("../utils/logger");
  851. function _classCallCheck(instance, Constructor) {
  852. if (!(instance instanceof Constructor)) {
  853. throw new TypeError("Cannot call a class as a function");
  854. }
  855. }
  856. var ExpGolomb = (function () {
  857. function ExpGolomb(data) {
  858. _classCallCheck(this, ExpGolomb);
  859. this.data = data;
  860. // the number of bytes left to examine in this.data
  861. this.bytesAvailable = this.data.byteLength;
  862. // the current word being examined
  863. this.word = 0; // :uint
  864. // the number of bits left to examine in the current word
  865. this.bitsAvailable = 0; // :uint
  866. }
  867. // ():void
  868. _createClass(ExpGolomb, [
  869. {
  870. key: "loadWord",
  871. value: function loadWord() {
  872. var position = this.data.byteLength - this.bytesAvailable,
  873. workingBytes = new Uint8Array(4),
  874. availableBytes = Math.min(4, this.bytesAvailable);
  875. if (availableBytes === 0) {
  876. throw new Error("no bytes available");
  877. }
  878. workingBytes.set(
  879. this.data.subarray(position, position + availableBytes)
  880. );
  881. this.word = new DataView(workingBytes.buffer).getUint32(0);
  882. // track the amount of this.data that has been processed
  883. this.bitsAvailable = availableBytes * 8;
  884. this.bytesAvailable -= availableBytes;
  885. }
  886. // (count:int):void
  887. },
  888. {
  889. key: "skipBits",
  890. value: function skipBits(count) {
  891. var skipBytes; // :int
  892. if (this.bitsAvailable > count) {
  893. this.word <<= count;
  894. this.bitsAvailable -= count;
  895. } else {
  896. count -= this.bitsAvailable;
  897. skipBytes = count >> 3;
  898. count -= skipBytes >> 3;
  899. this.bytesAvailable -= skipBytes;
  900. this.loadWord();
  901. this.word <<= count;
  902. this.bitsAvailable -= count;
  903. }
  904. }
  905. // (size:int):uint
  906. },
  907. {
  908. key: "readBits",
  909. value: function readBits(size) {
  910. var bits = Math.min(this.bitsAvailable, size),
  911. // :uint
  912. valu = this.word >>> (32 - bits); // :uint
  913. if (size > 32) {
  914. _logger.logger.error(
  915. "Cannot read more than 32 bits at a time"
  916. );
  917. }
  918. this.bitsAvailable -= bits;
  919. if (this.bitsAvailable > 0) {
  920. this.word <<= bits;
  921. } else if (this.bytesAvailable > 0) {
  922. this.loadWord();
  923. }
  924. bits = size - bits;
  925. if (bits > 0) {
  926. return (valu << bits) | this.readBits(bits);
  927. } else {
  928. return valu;
  929. }
  930. }
  931. // ():uint
  932. },
  933. {
  934. key: "skipLZ",
  935. value: function skipLZ() {
  936. var leadingZeroCount; // :uint
  937. for (
  938. leadingZeroCount = 0;
  939. leadingZeroCount < this.bitsAvailable;
  940. ++leadingZeroCount
  941. ) {
  942. if (0 !== (this.word & (0x80000000 >>> leadingZeroCount))) {
  943. // the first bit of working word is 1
  944. this.word <<= leadingZeroCount;
  945. this.bitsAvailable -= leadingZeroCount;
  946. return leadingZeroCount;
  947. }
  948. }
  949. // we exhausted word and still have not found a 1
  950. this.loadWord();
  951. return leadingZeroCount + this.skipLZ();
  952. }
  953. // ():void
  954. },
  955. {
  956. key: "skipUEG",
  957. value: function skipUEG() {
  958. this.skipBits(1 + this.skipLZ());
  959. }
  960. // ():void
  961. },
  962. {
  963. key: "skipEG",
  964. value: function skipEG() {
  965. this.skipBits(1 + this.skipLZ());
  966. }
  967. // ():uint
  968. },
  969. {
  970. key: "readUEG",
  971. value: function readUEG() {
  972. var clz = this.skipLZ(); // :uint
  973. return this.readBits(clz + 1) - 1;
  974. }
  975. // ():int
  976. },
  977. {
  978. key: "readEG",
  979. value: function readEG() {
  980. var valu = this.readUEG(); // :int
  981. if (0x01 & valu) {
  982. // the number is odd if the low order bit is set
  983. return (1 + valu) >>> 1; // add 1 to make it even, and divide by 2
  984. } else {
  985. return -1 * (valu >>> 1); // divide by two then make it negative
  986. }
  987. }
  988. // Some convenience functions
  989. // :Boolean
  990. },
  991. {
  992. key: "readBoolean",
  993. value: function readBoolean() {
  994. return 1 === this.readBits(1);
  995. }
  996. // ():int
  997. },
  998. {
  999. key: "readUByte",
  1000. value: function readUByte() {
  1001. return this.readBits(8);
  1002. }
  1003. // ():int
  1004. },
  1005. {
  1006. key: "readUShort",
  1007. value: function readUShort() {
  1008. return this.readBits(16);
  1009. }
  1010. // ():int
  1011. },
  1012. {
  1013. key: "readUInt",
  1014. value: function readUInt() {
  1015. return this.readBits(32);
  1016. }
  1017. /**
  1018. * Advance the ExpGolomb decoder past a scaling list. The scaling
  1019. * list is optionally transmitted as part of a sequence parameter
  1020. * set and is not relevant to transmuxing.
  1021. * @param count {number} the number of entries in this scaling list
  1022. * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
  1023. */
  1024. },
  1025. {
  1026. key: "skipScalingList",
  1027. value: function skipScalingList(count) {
  1028. var lastScale = 8,
  1029. nextScale = 8,
  1030. j,
  1031. deltaScale;
  1032. for (j = 0; j < count; j++) {
  1033. if (nextScale !== 0) {
  1034. deltaScale = this.readEG();
  1035. nextScale = (lastScale + deltaScale + 256) % 256;
  1036. }
  1037. lastScale = nextScale === 0 ? lastScale : nextScale;
  1038. }
  1039. }
  1040. /**
  1041. * Read a sequence parameter set and return some interesting video
  1042. * properties. A sequence parameter set is the H264 metadata that
  1043. * describes the properties of upcoming video frames.
  1044. * @param data {Uint8Array} the bytes of a sequence parameter set
  1045. * @return {object} an object with configuration parsed from the
  1046. * sequence parameter set, including the dimensions of the
  1047. * associated video frames.
  1048. */
  1049. },
  1050. {
  1051. key: "readSPS",
  1052. value: function readSPS() {
  1053. var frameCropLeftOffset = 0,
  1054. frameCropRightOffset = 0,
  1055. frameCropTopOffset = 0,
  1056. frameCropBottomOffset = 0,
  1057. sarScale = 1,
  1058. profileIdc,
  1059. profileCompat,
  1060. levelIdc,
  1061. numRefFramesInPicOrderCntCycle,
  1062. picWidthInMbsMinus1,
  1063. picHeightInMapUnitsMinus1,
  1064. frameMbsOnlyFlag,
  1065. scalingListCount,
  1066. i;
  1067. this.readUByte();
  1068. profileIdc = this.readUByte(); // profile_idc
  1069. profileCompat = this.readBits(5); // constraint_set[0-4]_flag, u(5)
  1070. this.skipBits(3); // reserved_zero_3bits u(3),
  1071. levelIdc = this.readUByte(); //level_idc u(8)
  1072. this.skipUEG(); // seq_parameter_set_id
  1073. // some profiles have more optional data we don't need
  1074. if (
  1075. profileIdc === 100 ||
  1076. profileIdc === 110 ||
  1077. profileIdc === 122 ||
  1078. profileIdc === 244 ||
  1079. profileIdc === 44 ||
  1080. profileIdc === 83 ||
  1081. profileIdc === 86 ||
  1082. profileIdc === 118 ||
  1083. profileIdc === 128
  1084. ) {
  1085. var chromaFormatIdc = this.readUEG();
  1086. if (chromaFormatIdc === 3) {
  1087. this.skipBits(1); // separate_colour_plane_flag
  1088. }
  1089. this.skipUEG(); // bit_depth_luma_minus8
  1090. this.skipUEG(); // bit_depth_chroma_minus8
  1091. this.skipBits(1); // qpprime_y_zero_transform_bypass_flag
  1092. if (this.readBoolean()) {
  1093. // seq_scaling_matrix_present_flag
  1094. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  1095. for (i = 0; i < scalingListCount; i++) {
  1096. if (this.readBoolean()) {
  1097. // seq_scaling_list_present_flag[ i ]
  1098. if (i < 6) {
  1099. this.skipScalingList(16);
  1100. } else {
  1101. this.skipScalingList(64);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. this.skipUEG(); // log2_max_frame_num_minus4
  1108. var picOrderCntType = this.readUEG();
  1109. if (picOrderCntType === 0) {
  1110. this.readUEG(); //log2_max_pic_order_cnt_lsb_minus4
  1111. } else if (picOrderCntType === 1) {
  1112. this.skipBits(1); // delta_pic_order_always_zero_flag
  1113. this.skipEG(); // offset_for_non_ref_pic
  1114. this.skipEG(); // offset_for_top_to_bottom_field
  1115. numRefFramesInPicOrderCntCycle = this.readUEG();
  1116. for (i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  1117. this.skipEG(); // offset_for_ref_frame[ i ]
  1118. }
  1119. }
  1120. this.skipUEG(); // max_num_ref_frames
  1121. this.skipBits(1); // gaps_in_frame_num_value_allowed_flag
  1122. picWidthInMbsMinus1 = this.readUEG();
  1123. picHeightInMapUnitsMinus1 = this.readUEG();
  1124. frameMbsOnlyFlag = this.readBits(1);
  1125. if (frameMbsOnlyFlag === 0) {
  1126. this.skipBits(1); // mb_adaptive_frame_field_flag
  1127. }
  1128. this.skipBits(1); // direct_8x8_inference_flag
  1129. if (this.readBoolean()) {
  1130. // frame_cropping_flag
  1131. frameCropLeftOffset = this.readUEG();
  1132. frameCropRightOffset = this.readUEG();
  1133. frameCropTopOffset = this.readUEG();
  1134. frameCropBottomOffset = this.readUEG();
  1135. }
  1136. if (this.readBoolean()) {
  1137. // vui_parameters_present_flag
  1138. if (this.readBoolean()) {
  1139. // aspect_ratio_info_present_flag
  1140. var sarRatio = void 0;
  1141. var aspectRatioIdc = this.readUByte();
  1142. switch (aspectRatioIdc) {
  1143. case 1:
  1144. sarRatio = [1, 1];
  1145. break;
  1146. case 2:
  1147. sarRatio = [12, 11];
  1148. break;
  1149. case 3:
  1150. sarRatio = [10, 11];
  1151. break;
  1152. case 4:
  1153. sarRatio = [16, 11];
  1154. break;
  1155. case 5:
  1156. sarRatio = [40, 33];
  1157. break;
  1158. case 6:
  1159. sarRatio = [24, 11];
  1160. break;
  1161. case 7:
  1162. sarRatio = [20, 11];
  1163. break;
  1164. case 8:
  1165. sarRatio = [32, 11];
  1166. break;
  1167. case 9:
  1168. sarRatio = [80, 33];
  1169. break;
  1170. case 10:
  1171. sarRatio = [18, 11];
  1172. break;
  1173. case 11:
  1174. sarRatio = [15, 11];
  1175. break;
  1176. case 12:
  1177. sarRatio = [64, 33];
  1178. break;
  1179. case 13:
  1180. sarRatio = [160, 99];
  1181. break;
  1182. case 14:
  1183. sarRatio = [4, 3];
  1184. break;
  1185. case 15:
  1186. sarRatio = [3, 2];
  1187. break;
  1188. case 16:
  1189. sarRatio = [2, 1];
  1190. break;
  1191. case 255: {
  1192. sarRatio = [
  1193. (this.readUByte() << 8) | this.readUByte(),
  1194. (this.readUByte() << 8) | this.readUByte()
  1195. ];
  1196. break;
  1197. }
  1198. }
  1199. if (sarRatio) {
  1200. sarScale = sarRatio[0] / sarRatio[1];
  1201. }
  1202. }
  1203. }
  1204. return {
  1205. width: Math.ceil(
  1206. ((picWidthInMbsMinus1 + 1) * 16 -
  1207. frameCropLeftOffset * 2 -
  1208. frameCropRightOffset * 2) *
  1209. sarScale
  1210. ),
  1211. height:
  1212. (2 - frameMbsOnlyFlag) *
  1213. (picHeightInMapUnitsMinus1 + 1) *
  1214. 16 -
  1215. (frameMbsOnlyFlag ? 2 : 4) *
  1216. (frameCropTopOffset + frameCropBottomOffset)
  1217. };
  1218. }
  1219. },
  1220. {
  1221. key: "readSliceType",
  1222. value: function readSliceType() {
  1223. // skip NALu type
  1224. this.readUByte();
  1225. // discard first_mb_in_slice
  1226. this.readUEG();
  1227. // return slice_type
  1228. return this.readUEG();
  1229. }
  1230. }
  1231. ]);
  1232. return ExpGolomb;
  1233. })();
  1234. exports.default = ExpGolomb;
  1235. },
  1236. { "../utils/logger": 15 }
  1237. ],
  1238. 5: [
  1239. function (require, module, exports) {
  1240. "use strict";
  1241. Object.defineProperty(exports, "__esModule", {
  1242. value: true
  1243. });
  1244. var _createClass = (function () {
  1245. function defineProperties(target, props) {
  1246. for (var i = 0; i < props.length; i++) {
  1247. var descriptor = props[i];
  1248. descriptor.enumerable = descriptor.enumerable || false;
  1249. descriptor.configurable = true;
  1250. if ("value" in descriptor) descriptor.writable = true;
  1251. Object.defineProperty(target, descriptor.key, descriptor);
  1252. }
  1253. }
  1254. return function (Constructor, protoProps, staticProps) {
  1255. if (protoProps)
  1256. defineProperties(Constructor.prototype, protoProps);
  1257. if (staticProps) defineProperties(Constructor, staticProps);
  1258. return Constructor;
  1259. };
  1260. })();
  1261. var _events = require("../events");
  1262. var _events2 = _interopRequireDefault(_events);
  1263. var _expGolomb = require("./exp-golomb");
  1264. var _expGolomb2 = _interopRequireDefault(_expGolomb);
  1265. var _eventHandler = require("../event-handler");
  1266. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  1267. var _mp4Remuxer = require("../remux/mp4-remuxer");
  1268. var _mp4Remuxer2 = _interopRequireDefault(_mp4Remuxer);
  1269. function _interopRequireDefault(obj) {
  1270. return obj && obj.__esModule ? obj : { default: obj };
  1271. }
  1272. function _classCallCheck(instance, Constructor) {
  1273. if (!(instance instanceof Constructor)) {
  1274. throw new TypeError("Cannot call a class as a function");
  1275. }
  1276. }
  1277. function _possibleConstructorReturn(self, call) {
  1278. if (!self) {
  1279. throw new ReferenceError(
  1280. "this hasn't been initialised - super() hasn't been called"
  1281. );
  1282. }
  1283. return call &&
  1284. (typeof call === "object" || typeof call === "function")
  1285. ? call
  1286. : self;
  1287. }
  1288. function _inherits(subClass, superClass) {
  1289. if (typeof superClass !== "function" && superClass !== null) {
  1290. throw new TypeError(
  1291. "Super expression must either be null or a function, not " +
  1292. typeof superClass
  1293. );
  1294. }
  1295. subClass.prototype = Object.create(
  1296. superClass && superClass.prototype,
  1297. {
  1298. constructor: {
  1299. value: subClass,
  1300. enumerable: false,
  1301. writable: true,
  1302. configurable: true
  1303. }
  1304. }
  1305. );
  1306. if (superClass)
  1307. Object.setPrototypeOf
  1308. ? Object.setPrototypeOf(subClass, superClass)
  1309. : (subClass.__proto__ = superClass);
  1310. } /**
  1311. */
  1312. var h264Demuxer = (function (_EventHandler) {
  1313. _inherits(h264Demuxer, _EventHandler);
  1314. function h264Demuxer(wfs) {
  1315. var config =
  1316. arguments.length > 1 && arguments[1] !== undefined
  1317. ? arguments[1]
  1318. : null;
  1319. _classCallCheck(this, h264Demuxer);
  1320. var _this = _possibleConstructorReturn(
  1321. this,
  1322. (
  1323. h264Demuxer.__proto__ || Object.getPrototypeOf(h264Demuxer)
  1324. ).call(this, wfs, _events2.default.H264_DATA_PARSED)
  1325. );
  1326. _this.config = _this.wfs.config || config;
  1327. _this.wfs = wfs;
  1328. _this.id = "main";
  1329. _this.remuxer = new _mp4Remuxer2.default(
  1330. _this.wfs,
  1331. _this.id,
  1332. _this.config
  1333. );
  1334. _this.contiguous = true;
  1335. _this.timeOffset = 1;
  1336. _this.sn = 0;
  1337. _this.TIMESCALE = 90000;
  1338. _this.timestamp = 0;
  1339. _this.scaleFactor = _this.TIMESCALE / 1000;
  1340. _this.H264_TIMEBASE = 3000;
  1341. _this._avcTrack = {
  1342. container: "video/mp2t",
  1343. type: "video",
  1344. id: 1,
  1345. sequenceNumber: 0,
  1346. samples: [],
  1347. len: 0,
  1348. nbNalu: 0,
  1349. dropped: 0,
  1350. count: 0
  1351. };
  1352. _this.browserType = 0;
  1353. if (navigator.userAgent.toLowerCase().indexOf("firefox") !== -1) {
  1354. _this.browserType = 1;
  1355. }
  1356. return _this;
  1357. }
  1358. _createClass(h264Demuxer, [
  1359. {
  1360. key: "destroy",
  1361. value: function destroy() {
  1362. _eventHandler2.default.prototype.destroy.call(this);
  1363. }
  1364. },
  1365. {
  1366. key: "getTimestampM",
  1367. value: function getTimestampM() {
  1368. this.timestamp += this.H264_TIMEBASE;
  1369. return this.timestamp;
  1370. }
  1371. },
  1372. {
  1373. key: "onH264DataParsed",
  1374. value: function onH264DataParsed(event) {
  1375. this._parseAVCTrack(event.data);
  1376. if (
  1377. this.browserType === 1 ||
  1378. this._avcTrack.samples.length >= 20
  1379. ) {
  1380. // Firefox
  1381. this.remuxer.pushVideo(
  1382. 0,
  1383. this.sn,
  1384. this._avcTrack,
  1385. this.timeOffset,
  1386. this.contiguous
  1387. );
  1388. this.sn += 1;
  1389. }
  1390. }
  1391. },
  1392. {
  1393. key: "_parseAVCTrack",
  1394. value: function _parseAVCTrack(array) {
  1395. var _this2 = this;
  1396. var track = this._avcTrack,
  1397. samples = track.samples,
  1398. units = this._parseAVCNALu(array),
  1399. units2 = [],
  1400. debug = false,
  1401. key = false,
  1402. length = 0,
  1403. expGolombDecoder,
  1404. avcSample,
  1405. push,
  1406. i;
  1407. var debugString = "";
  1408. var pushAccesUnit = function () {
  1409. if (units2.length) {
  1410. if (
  1411. !this.config.forceKeyFrameOnDiscontinuity ||
  1412. key === true ||
  1413. (track.sps && (samples.length || this.contiguous))
  1414. ) {
  1415. var tss = this.getTimestampM();
  1416. avcSample = {
  1417. units: { units: units2, length: length },
  1418. pts: tss,
  1419. dts: tss,
  1420. key: key
  1421. };
  1422. samples.push(avcSample);
  1423. track.len += length;
  1424. track.nbNalu += units2.length;
  1425. } else {
  1426. track.dropped++;
  1427. }
  1428. units2 = [];
  1429. length = 0;
  1430. }
  1431. }.bind(this);
  1432. units.forEach(function (unit) {
  1433. switch (unit.type) {
  1434. //NDR
  1435. case 1:
  1436. push = true;
  1437. if (debug) {
  1438. debugString += "NDR ";
  1439. }
  1440. break;
  1441. //IDR
  1442. case 5:
  1443. push = true;
  1444. if (debug) {
  1445. debugString += "IDR ";
  1446. }
  1447. key = true;
  1448. break;
  1449. //SEI
  1450. case 6:
  1451. unit.data = _this2.discardEPB(unit.data);
  1452. expGolombDecoder = new _expGolomb2.default(unit.data);
  1453. // skip frameType
  1454. expGolombDecoder.readUByte();
  1455. break;
  1456. //SPS
  1457. case 7:
  1458. push = false;
  1459. if (debug) {
  1460. debugString += "SPS ";
  1461. }
  1462. if (!track.sps) {
  1463. expGolombDecoder = new _expGolomb2.default(unit.data);
  1464. var config = expGolombDecoder.readSPS();
  1465. track.width = config.width;
  1466. track.height = config.height;
  1467. track.sps = [unit.data];
  1468. track.duration = 0;
  1469. var codecarray = unit.data.subarray(1, 4);
  1470. var codecstring = "avc1.";
  1471. for (i = 0; i < 3; i++) {
  1472. var h = codecarray[i].toString(16);
  1473. if (h.length < 2) {
  1474. h = "0" + h;
  1475. }
  1476. codecstring += h;
  1477. }
  1478. track.codec = codecstring;
  1479. _this2.wfs.trigger(_events2.default.BUFFER_RESET, {
  1480. mimeType: track.codec
  1481. });
  1482. push = true;
  1483. }
  1484. break;
  1485. //PPS
  1486. case 8:
  1487. push = false;
  1488. if (debug) {
  1489. debugString += "PPS ";
  1490. }
  1491. if (!track.pps) {
  1492. track.pps = [unit.data];
  1493. push = true;
  1494. }
  1495. break;
  1496. case 9:
  1497. push = false;
  1498. if (debug) {
  1499. debugString += "AUD ";
  1500. }
  1501. pushAccesUnit();
  1502. break;
  1503. default:
  1504. push = false;
  1505. debugString += "unknown NAL " + unit.type + " ";
  1506. break;
  1507. }
  1508. if (push) {
  1509. units2.push(unit);
  1510. length += unit.data.byteLength;
  1511. }
  1512. });
  1513. if (debug || debugString.length) {
  1514. logger.log(debugString);
  1515. }
  1516. pushAccesUnit();
  1517. }
  1518. },
  1519. {
  1520. key: "_parseAVCNALu",
  1521. value: function _parseAVCNALu(array) {
  1522. var i = 0,
  1523. len = array.byteLength,
  1524. value,
  1525. overflow,
  1526. state = 0; //state = this.avcNaluState;
  1527. var units = [],
  1528. unit,
  1529. unitType,
  1530. lastUnitStart,
  1531. lastUnitType;
  1532. while (i < len) {
  1533. value = array[i++];
  1534. // finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)
  1535. switch (state) {
  1536. case 0:
  1537. if (value === 0) {
  1538. state = 1;
  1539. }
  1540. break;
  1541. case 1:
  1542. if (value === 0) {
  1543. state = 2;
  1544. } else {
  1545. state = 0;
  1546. }
  1547. break;
  1548. case 2:
  1549. case 3:
  1550. if (value === 0) {
  1551. state = 3;
  1552. } else if (value === 1 && i < len) {
  1553. unitType = array[i] & 0x1f;
  1554. if (lastUnitStart) {
  1555. unit = {
  1556. data: array.subarray(
  1557. lastUnitStart,
  1558. i - state - 1
  1559. ),
  1560. type: lastUnitType
  1561. };
  1562. units.push(unit);
  1563. } else {
  1564. }
  1565. lastUnitStart = i;
  1566. lastUnitType = unitType;
  1567. state = 0;
  1568. } else {
  1569. state = 0;
  1570. }
  1571. break;
  1572. default:
  1573. break;
  1574. }
  1575. }
  1576. if (lastUnitStart) {
  1577. unit = {
  1578. data: array.subarray(lastUnitStart, len),
  1579. type: lastUnitType,
  1580. state: state
  1581. };
  1582. units.push(unit);
  1583. }
  1584. return units;
  1585. }
  1586. /**
  1587. * remove Emulation Prevention bytes from a RBSP
  1588. */
  1589. },
  1590. {
  1591. key: "discardEPB",
  1592. value: function discardEPB(data) {
  1593. var length = data.byteLength,
  1594. EPBPositions = [],
  1595. i = 1,
  1596. newLength,
  1597. newData;
  1598. // Find all `Emulation Prevention Bytes`
  1599. while (i < length - 2) {
  1600. if (
  1601. data[i] === 0 &&
  1602. data[i + 1] === 0 &&
  1603. data[i + 2] === 0x03
  1604. ) {
  1605. EPBPositions.push(i + 2);
  1606. i += 2;
  1607. } else {
  1608. i++;
  1609. }
  1610. }
  1611. // If no Emulation Prevention Bytes were found just return the original
  1612. // array
  1613. if (EPBPositions.length === 0) {
  1614. return data;
  1615. }
  1616. // Create a new array to hold the NAL unit data
  1617. newLength = length - EPBPositions.length;
  1618. newData = new Uint8Array(newLength);
  1619. var sourceIndex = 0;
  1620. for (i = 0; i < newLength; sourceIndex++ , i++) {
  1621. if (sourceIndex === EPBPositions[0]) {
  1622. // Skip this byte
  1623. sourceIndex++;
  1624. // Remove this position index
  1625. EPBPositions.shift();
  1626. }
  1627. newData[i] = data[sourceIndex];
  1628. }
  1629. return newData;
  1630. }
  1631. }
  1632. ]);
  1633. return h264Demuxer;
  1634. })(_eventHandler2.default);
  1635. exports.default = h264Demuxer;
  1636. },
  1637. {
  1638. "../event-handler": 7,
  1639. "../events": 8,
  1640. "../remux/mp4-remuxer": 13,
  1641. "./exp-golomb": 4
  1642. }
  1643. ],
  1644. 6: [
  1645. function (require, module, exports) {
  1646. "use strict";
  1647. Object.defineProperty(exports, "__esModule", {
  1648. value: true
  1649. });
  1650. var ErrorTypes = (exports.ErrorTypes = {
  1651. // Identifier for a network error (loading error / timeout ...)
  1652. NETWORK_ERROR: "networkError",
  1653. // Identifier for a media Error (video/parsing/mediasource error)
  1654. MEDIA_ERROR: "mediaError",
  1655. // Identifier for all other errors
  1656. OTHER_ERROR: "otherError"
  1657. });
  1658. var ErrorDetails = (exports.ErrorDetails = {
  1659. // Identifier for a manifest load error - data: { url : faulty URL, response : { code: error code, text: error text }}
  1660. MANIFEST_LOAD_ERROR: "manifestLoadError",
  1661. // Identifier for a manifest load timeout - data: { url : faulty URL, response : { code: error code, text: error text }}
  1662. MANIFEST_LOAD_TIMEOUT: "manifestLoadTimeOut",
  1663. // Identifier for a manifest parsing error - data: { url : faulty URL, reason : error reason}
  1664. MANIFEST_PARSING_ERROR: "manifestParsingError",
  1665. // Identifier for a manifest with only incompatible codecs error - data: { url : faulty URL, reason : error reason}
  1666. MANIFEST_INCOMPATIBLE_CODECS_ERROR:
  1667. "manifestIncompatibleCodecsError",
  1668. // Identifier for a level load error - data: { url : faulty URL, response : { code: error code, text: error text }}
  1669. LEVEL_LOAD_ERROR: "levelLoadError",
  1670. // Identifier for a level load timeout - data: { url : faulty URL, response : { code: error code, text: error text }}
  1671. LEVEL_LOAD_TIMEOUT: "levelLoadTimeOut",
  1672. // Identifier for a level switch error - data: { level : faulty level Id, event : error description}
  1673. LEVEL_SWITCH_ERROR: "levelSwitchError",
  1674. // Identifier for an audio track load error - data: { url : faulty URL, response : { code: error code, text: error text }}
  1675. AUDIO_TRACK_LOAD_ERROR: "audioTrackLoadError",
  1676. // Identifier for an audio track load timeout - data: { url : faulty URL, response : { code: error code, text: error text }}
  1677. AUDIO_TRACK_LOAD_TIMEOUT: "audioTrackLoadTimeOut",
  1678. // Identifier for fragment load error - data: { frag : fragment object, response : { code: error code, text: error text }}
  1679. FRAG_LOAD_ERROR: "fragLoadError",
  1680. // Identifier for fragment loop loading error - data: { frag : fragment object}
  1681. FRAG_LOOP_LOADING_ERROR: "fragLoopLoadingError",
  1682. // Identifier for fragment load timeout error - data: { frag : fragment object}
  1683. FRAG_LOAD_TIMEOUT: "fragLoadTimeOut",
  1684. // Identifier for a fragment decryption error event - data: parsing error description
  1685. FRAG_DECRYPT_ERROR: "fragDecryptError",
  1686. // Identifier for a fragment parsing error event - data: parsing error description
  1687. FRAG_PARSING_ERROR: "fragParsingError",
  1688. // Identifier for decrypt key load error - data: { frag : fragment object, response : { code: error code, text: error text }}
  1689. KEY_LOAD_ERROR: "keyLoadError",
  1690. // Identifier for decrypt key load timeout error - data: { frag : fragment object}
  1691. KEY_LOAD_TIMEOUT: "keyLoadTimeOut",
  1692. // Triggered when an exception occurs while adding a sourceBuffer to MediaSource - data : { err : exception , mimeType : mimeType }
  1693. BUFFER_ADD_CODEC_ERROR: "bufferAddCodecError",
  1694. // Identifier for a buffer append error - data: append error description
  1695. BUFFER_APPEND_ERROR: "bufferAppendError",
  1696. // Identifier for a buffer appending error event - data: appending error description
  1697. BUFFER_APPENDING_ERROR: "bufferAppendingError",
  1698. // Identifier for a buffer stalled error event
  1699. BUFFER_STALLED_ERROR: "bufferStalledError",
  1700. // Identifier for a buffer full event
  1701. BUFFER_FULL_ERROR: "bufferFullError",
  1702. // Identifier for a buffer seek over hole event
  1703. BUFFER_SEEK_OVER_HOLE: "bufferSeekOverHole",
  1704. // Identifier for an internal exception happening inside hls.js while handling an event
  1705. INTERNAL_EXCEPTION: "internalException"
  1706. });
  1707. },
  1708. {}
  1709. ],
  1710. 7: [
  1711. function (require, module, exports) {
  1712. "use strict";
  1713. Object.defineProperty(exports, "__esModule", {
  1714. value: true
  1715. });
  1716. var _typeof =
  1717. typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
  1718. ? function (obj) {
  1719. return typeof obj;
  1720. }
  1721. : function (obj) {
  1722. return obj &&
  1723. typeof Symbol === "function" &&
  1724. obj.constructor === Symbol &&
  1725. obj !== Symbol.prototype
  1726. ? "symbol"
  1727. : typeof obj;
  1728. };
  1729. var _createClass = (function () {
  1730. function defineProperties(target, props) {
  1731. for (var i = 0; i < props.length; i++) {
  1732. var descriptor = props[i];
  1733. descriptor.enumerable = descriptor.enumerable || false;
  1734. descriptor.configurable = true;
  1735. if ("value" in descriptor) descriptor.writable = true;
  1736. Object.defineProperty(target, descriptor.key, descriptor);
  1737. }
  1738. }
  1739. return function (Constructor, protoProps, staticProps) {
  1740. if (protoProps)
  1741. defineProperties(Constructor.prototype, protoProps);
  1742. if (staticProps) defineProperties(Constructor, staticProps);
  1743. return Constructor;
  1744. };
  1745. })();
  1746. /*
  1747. *
  1748. * All objects in the event handling chain should inherit from this class
  1749. *
  1750. */
  1751. var _events = require("./events");
  1752. var _events2 = _interopRequireDefault(_events);
  1753. function _interopRequireDefault(obj) {
  1754. return obj && obj.__esModule ? obj : { default: obj };
  1755. }
  1756. function _classCallCheck(instance, Constructor) {
  1757. if (!(instance instanceof Constructor)) {
  1758. throw new TypeError("Cannot call a class as a function");
  1759. }
  1760. }
  1761. var EventHandler = (function () {
  1762. function EventHandler(wfs) {
  1763. _classCallCheck(this, EventHandler);
  1764. this.wfs = wfs;
  1765. this.onEvent = this.onEvent.bind(this);
  1766. for (
  1767. var _len = arguments.length,
  1768. events = Array(_len > 1 ? _len - 1 : 0),
  1769. _key = 1;
  1770. _key < _len;
  1771. _key++
  1772. ) {
  1773. events[_key - 1] = arguments[_key];
  1774. }
  1775. this.handledEvents = events;
  1776. this.useGenericHandler = true;
  1777. this.registerListeners();
  1778. }
  1779. _createClass(EventHandler, [
  1780. {
  1781. key: "destroy",
  1782. value: function destroy() {
  1783. this.unregisterListeners();
  1784. }
  1785. },
  1786. {
  1787. key: "isEventHandler",
  1788. value: function isEventHandler() {
  1789. return (
  1790. _typeof(this.handledEvents) === "object" &&
  1791. this.handledEvents.length &&
  1792. typeof this.onEvent === "function"
  1793. );
  1794. }
  1795. },
  1796. {
  1797. key: "registerListeners",
  1798. value: function registerListeners() {
  1799. if (this.isEventHandler()) {
  1800. this.handledEvents.forEach(
  1801. function (event) {
  1802. if (event === "wfsEventGeneric") {
  1803. //throw new Error('Forbidden event name: ' + event);
  1804. }
  1805. this.wfs.on(event, this.onEvent);
  1806. }.bind(this)
  1807. );
  1808. }
  1809. }
  1810. },
  1811. {
  1812. key: "unregisterListeners",
  1813. value: function unregisterListeners() {
  1814. if (this.isEventHandler()) {
  1815. this.handledEvents.forEach(
  1816. function (event) {
  1817. this.wfs.off(event, this.onEvent);
  1818. }.bind(this)
  1819. );
  1820. }
  1821. }
  1822. /**
  1823. * arguments: event (string), data (any)
  1824. */
  1825. },
  1826. {
  1827. key: "onEvent",
  1828. value: function onEvent(event, data) {
  1829. this.onEventGeneric(event, data);
  1830. }
  1831. },
  1832. {
  1833. key: "onEventGeneric",
  1834. value: function onEventGeneric(event, data) {
  1835. var eventToFunction = function eventToFunction(event, data) {
  1836. var funcName = "on" + event.replace("wfs", "");
  1837. if (typeof this[funcName] !== "function") {
  1838. //throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`);
  1839. }
  1840. return this[funcName].bind(this, data);
  1841. };
  1842. try {
  1843. eventToFunction.call(this, event, data).call();
  1844. } catch (err) {
  1845. console.log(
  1846. "internal error happened while processing " +
  1847. event +
  1848. ":" +
  1849. err.message
  1850. );
  1851. // this.hls.trigger(Event.ERROR, {type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event : event, err : err});
  1852. }
  1853. }
  1854. }
  1855. ]);
  1856. return EventHandler;
  1857. })();
  1858. exports.default = EventHandler;
  1859. },
  1860. { "./events": 8 }
  1861. ],
  1862. 8: [
  1863. function (require, module, exports) {
  1864. "use strict";
  1865. module.exports = {
  1866. MEDIA_ATTACHING: "wfsMediaAttaching",
  1867. MEDIA_ATTACHED: "wfsMediaAttached",
  1868. FRAG_LOADING: "wfsFragLoading",
  1869. BUFFER_CREATED: "wfsBufferCreated",
  1870. BUFFER_APPENDING: "wfsBufferAppending",
  1871. BUFFER_RESET: "wfsBufferReset",
  1872. FRAG_PARSING_DATA: "wfsFragParsingData",
  1873. FRAG_PARSING_INIT_SEGMENT: "wfsFragParsingInitSegment",
  1874. //------------------------------------------
  1875. H264_DATA_PARSING: "wfsH264DataParsing",
  1876. H264_DATA_PARSED: "wfsH264DataParsed",
  1877. //------------------------------------------
  1878. WEBSOCKET_ATTACHED: "wfsWebsocketAttached",
  1879. WEBSOCKET_ATTACHING: "wfsWebsocketAttaching",
  1880. WEBSOCKET_DATA_UPLOADING: "wfsWebsocketDataUploading",
  1881. WEBSOCKET_MESSAGE_SENDING: "wfsWebsocketMessageSending",
  1882. //------------------------------------------
  1883. FILE_HEAD_LOADING: "wfsFileHeadLoading",
  1884. FILE_HEAD_LOADED: "wfsFileHeadLoaded",
  1885. FILE_DATA_LOADING: "wfsFileDataLoading",
  1886. FILE_DATA_LOADED: "wfsFileDataLoaded",
  1887. FILE_PARSING_DATA: "wfsFileParsingData"
  1888. //------------------------------------------
  1889. };
  1890. },
  1891. {}
  1892. ],
  1893. 9: [
  1894. function (require, module, exports) {
  1895. "use strict";
  1896. Object.defineProperty(exports, "__esModule", {
  1897. value: true
  1898. });
  1899. var _createClass = (function () {
  1900. function defineProperties(target, props) {
  1901. for (var i = 0; i < props.length; i++) {
  1902. var descriptor = props[i];
  1903. descriptor.enumerable = descriptor.enumerable || false;
  1904. descriptor.configurable = true;
  1905. if ("value" in descriptor) descriptor.writable = true;
  1906. Object.defineProperty(target, descriptor.key, descriptor);
  1907. }
  1908. }
  1909. return function (Constructor, protoProps, staticProps) {
  1910. if (protoProps)
  1911. defineProperties(Constructor.prototype, protoProps);
  1912. if (staticProps) defineProperties(Constructor, staticProps);
  1913. return Constructor;
  1914. };
  1915. })();
  1916. function _classCallCheck(instance, Constructor) {
  1917. if (!(instance instanceof Constructor)) {
  1918. throw new TypeError("Cannot call a class as a function");
  1919. }
  1920. }
  1921. /**
  1922. * AAC helper
  1923. */
  1924. var AAC = (function () {
  1925. function AAC() {
  1926. _classCallCheck(this, AAC);
  1927. }
  1928. _createClass(AAC, null, [
  1929. {
  1930. key: "getSilentFrame",
  1931. value: function getSilentFrame(channelCount) {
  1932. if (channelCount === 1) {
  1933. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]);
  1934. } else if (channelCount === 2) {
  1935. return new Uint8Array([
  1936. 0x21,
  1937. 0x00,
  1938. 0x49,
  1939. 0x90,
  1940. 0x02,
  1941. 0x19,
  1942. 0x00,
  1943. 0x23,
  1944. 0x80
  1945. ]);
  1946. } else if (channelCount === 3) {
  1947. return new Uint8Array([
  1948. 0x00,
  1949. 0xc8,
  1950. 0x00,
  1951. 0x80,
  1952. 0x20,
  1953. 0x84,
  1954. 0x01,
  1955. 0x26,
  1956. 0x40,
  1957. 0x08,
  1958. 0x64,
  1959. 0x00,
  1960. 0x8e
  1961. ]);
  1962. } else if (channelCount === 4) {
  1963. return new Uint8Array([
  1964. 0x00,
  1965. 0xc8,
  1966. 0x00,
  1967. 0x80,
  1968. 0x20,
  1969. 0x84,
  1970. 0x01,
  1971. 0x26,
  1972. 0x40,
  1973. 0x08,
  1974. 0x64,
  1975. 0x00,
  1976. 0x80,
  1977. 0x2c,
  1978. 0x80,
  1979. 0x08,
  1980. 0x02,
  1981. 0x38
  1982. ]);
  1983. } else if (channelCount === 5) {
  1984. return new Uint8Array([
  1985. 0x00,
  1986. 0xc8,
  1987. 0x00,
  1988. 0x80,
  1989. 0x20,
  1990. 0x84,
  1991. 0x01,
  1992. 0x26,
  1993. 0x40,
  1994. 0x08,
  1995. 0x64,
  1996. 0x00,
  1997. 0x82,
  1998. 0x30,
  1999. 0x04,
  2000. 0x99,
  2001. 0x00,
  2002. 0x21,
  2003. 0x90,
  2004. 0x02,
  2005. 0x38
  2006. ]);
  2007. } else if (channelCount === 6) {
  2008. return new Uint8Array([
  2009. 0x00,
  2010. 0xc8,
  2011. 0x00,
  2012. 0x80,
  2013. 0x20,
  2014. 0x84,
  2015. 0x01,
  2016. 0x26,
  2017. 0x40,
  2018. 0x08,
  2019. 0x64,
  2020. 0x00,
  2021. 0x82,
  2022. 0x30,
  2023. 0x04,
  2024. 0x99,
  2025. 0x00,
  2026. 0x21,
  2027. 0x90,
  2028. 0x02,
  2029. 0x00,
  2030. 0xb2,
  2031. 0x00,
  2032. 0x20,
  2033. 0x08,
  2034. 0xe0
  2035. ]);
  2036. }
  2037. return null;
  2038. }
  2039. }
  2040. ]);
  2041. return AAC;
  2042. })();
  2043. exports.default = AAC;
  2044. },
  2045. {}
  2046. ],
  2047. 10: [
  2048. function (require, module, exports) {
  2049. "use strict";
  2050. // This is mostly for support of the es6 module export
  2051. // syntax with the babel compiler, it looks like it doesnt support
  2052. // function exports like we are used to in node/commonjs
  2053. module.exports = require("./wfs.js").default;
  2054. },
  2055. { "./wfs.js": 18 }
  2056. ],
  2057. 11: [
  2058. function (require, module, exports) {
  2059. "use strict";
  2060. Object.defineProperty(exports, "__esModule", {
  2061. value: true
  2062. });
  2063. var _createClass = (function () {
  2064. function defineProperties(target, props) {
  2065. for (var i = 0; i < props.length; i++) {
  2066. var descriptor = props[i];
  2067. descriptor.enumerable = descriptor.enumerable || false;
  2068. descriptor.configurable = true;
  2069. if ("value" in descriptor) descriptor.writable = true;
  2070. Object.defineProperty(target, descriptor.key, descriptor);
  2071. }
  2072. }
  2073. return function (Constructor, protoProps, staticProps) {
  2074. if (protoProps)
  2075. defineProperties(Constructor.prototype, protoProps);
  2076. if (staticProps) defineProperties(Constructor, staticProps);
  2077. return Constructor;
  2078. };
  2079. })();
  2080. var _events = require("../events");
  2081. var _events2 = _interopRequireDefault(_events);
  2082. var _eventHandler = require("../event-handler");
  2083. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  2084. var _h264NalSlicesreader = require("../utils/h264-nal-slicesreader.js");
  2085. var _h264NalSlicesreader2 = _interopRequireDefault(
  2086. _h264NalSlicesreader
  2087. );
  2088. function _interopRequireDefault(obj) {
  2089. return obj && obj.__esModule ? obj : { default: obj };
  2090. }
  2091. function _classCallCheck(instance, Constructor) {
  2092. if (!(instance instanceof Constructor)) {
  2093. throw new TypeError("Cannot call a class as a function");
  2094. }
  2095. }
  2096. function _possibleConstructorReturn(self, call) {
  2097. if (!self) {
  2098. throw new ReferenceError(
  2099. "this hasn't been initialised - super() hasn't been called"
  2100. );
  2101. }
  2102. return call &&
  2103. (typeof call === "object" || typeof call === "function")
  2104. ? call
  2105. : self;
  2106. }
  2107. function _inherits(subClass, superClass) {
  2108. if (typeof superClass !== "function" && superClass !== null) {
  2109. throw new TypeError(
  2110. "Super expression must either be null or a function, not " +
  2111. typeof superClass
  2112. );
  2113. }
  2114. subClass.prototype = Object.create(
  2115. superClass && superClass.prototype,
  2116. {
  2117. constructor: {
  2118. value: subClass,
  2119. enumerable: false,
  2120. writable: true,
  2121. configurable: true
  2122. }
  2123. }
  2124. );
  2125. if (superClass)
  2126. Object.setPrototypeOf
  2127. ? Object.setPrototypeOf(subClass, superClass)
  2128. : (subClass.__proto__ = superClass);
  2129. }
  2130. /*
  2131. * Websocket Loader
  2132. */
  2133. var WebsocketLoader = (function (_EventHandler) {
  2134. _inherits(WebsocketLoader, _EventHandler);
  2135. function WebsocketLoader(wfs) {
  2136. _classCallCheck(this, WebsocketLoader);
  2137. var _this = _possibleConstructorReturn(
  2138. this,
  2139. (
  2140. WebsocketLoader.__proto__ ||
  2141. Object.getPrototypeOf(WebsocketLoader)
  2142. ).call(
  2143. this,
  2144. wfs,
  2145. _events2.default.WEBSOCKET_ATTACHING,
  2146. _events2.default.WEBSOCKET_DATA_UPLOADING,
  2147. _events2.default.WEBSOCKET_MESSAGE_SENDING
  2148. )
  2149. );
  2150. _this.buf = null;
  2151. _this.slicesReader = new _h264NalSlicesreader2.default(wfs);
  2152. _this.mediaType = undefined;
  2153. return _this;
  2154. }
  2155. _createClass(WebsocketLoader, [
  2156. {
  2157. key: "destroy",
  2158. value: function destroy() {
  2159. !!this.client && this.client.close();
  2160. this.slicesReader.destroy();
  2161. _eventHandler2.default.prototype.destroy.call(this);
  2162. }
  2163. },
  2164. {
  2165. key: "onWebsocketAttaching",
  2166. value: function onWebsocketAttaching(data) {
  2167. this.mediaType = data.mediaType;
  2168. if (data.websocket instanceof WebSocket) {
  2169. this.client = data.websocket;
  2170. this.client.onopen = this.initSocketClient.bind(this);
  2171. this.client.onclose = function (e) {
  2172. console.log("Websocket Disconnected!");
  2173. };
  2174. }
  2175. }
  2176. },
  2177. {
  2178. key: "initSocketClient",
  2179. value: function initSocketClient(client) {
  2180. this.client.binaryType = "arraybuffer";
  2181. this.client.onmessage = this.receiveSocketMessage.bind(this);
  2182. this.wfs.trigger(_events2.default.WEBSOCKET_MESSAGE_SENDING, {
  2183. commandType: "open",
  2184. commandValue: "NA"
  2185. });
  2186. console.log("Websocket Open!");
  2187. }
  2188. },
  2189. {
  2190. key: "receiveSocketMessage",
  2191. value: function receiveSocketMessage(event) {
  2192. this.buf = new Uint8Array(event.data);
  2193. var copy = new Uint8Array(this.buf);
  2194. if (this.mediaType === "FMp4") {
  2195. this.wfs.trigger(_events2.default.WEBSOCKET_ATTACHED, {
  2196. payload: copy
  2197. });
  2198. }
  2199. if (this.mediaType === "H264Raw") {
  2200. this.wfs.trigger(_events2.default.H264_DATA_PARSING, {
  2201. data: copy
  2202. });
  2203. }
  2204. }
  2205. },
  2206. {
  2207. key: "onWebsocketDataUploading",
  2208. value: function onWebsocketDataUploading(event) {
  2209. this.client.send(event.data);
  2210. }
  2211. },
  2212. {
  2213. key: "onWebsocketMessageSending",
  2214. value: function onWebsocketMessageSending(event) {
  2215. this.client.send(
  2216. JSON.stringify({
  2217. t: event.commandType,
  2218. v: event.commandValue
  2219. })
  2220. );
  2221. }
  2222. }
  2223. ]);
  2224. return WebsocketLoader;
  2225. })(_eventHandler2.default);
  2226. exports.default = WebsocketLoader;
  2227. },
  2228. {
  2229. "../event-handler": 7,
  2230. "../events": 8,
  2231. "../utils/h264-nal-slicesreader.js": 14
  2232. }
  2233. ],
  2234. 12: [
  2235. function (require, module, exports) {
  2236. "use strict";
  2237. Object.defineProperty(exports, "__esModule", {
  2238. value: true
  2239. });
  2240. var _createClass = (function () {
  2241. function defineProperties(target, props) {
  2242. for (var i = 0; i < props.length; i++) {
  2243. var descriptor = props[i];
  2244. descriptor.enumerable = descriptor.enumerable || false;
  2245. descriptor.configurable = true;
  2246. if ("value" in descriptor) descriptor.writable = true;
  2247. Object.defineProperty(target, descriptor.key, descriptor);
  2248. }
  2249. }
  2250. return function (Constructor, protoProps, staticProps) {
  2251. if (protoProps)
  2252. defineProperties(Constructor.prototype, protoProps);
  2253. if (staticProps) defineProperties(Constructor, staticProps);
  2254. return Constructor;
  2255. };
  2256. })();
  2257. function _classCallCheck(instance, Constructor) {
  2258. if (!(instance instanceof Constructor)) {
  2259. throw new TypeError("Cannot call a class as a function");
  2260. }
  2261. }
  2262. /**
  2263. * Generate MP4 Box
  2264. */
  2265. //import Hex from '../utils/hex';
  2266. var MP4 = (function () {
  2267. function MP4() {
  2268. _classCallCheck(this, MP4);
  2269. }
  2270. _createClass(MP4, null, [
  2271. {
  2272. key: "init",
  2273. value: function init() {
  2274. MP4.types = {
  2275. avc1: [], // codingname
  2276. avcC: [],
  2277. btrt: [],
  2278. dinf: [],
  2279. dref: [],
  2280. esds: [],
  2281. ftyp: [],
  2282. hdlr: [],
  2283. mdat: [],
  2284. mdhd: [],
  2285. mdia: [],
  2286. mfhd: [],
  2287. minf: [],
  2288. moof: [],
  2289. moov: [],
  2290. mp4a: [],
  2291. mvex: [],
  2292. mvhd: [],
  2293. sdtp: [],
  2294. stbl: [],
  2295. stco: [],
  2296. stsc: [],
  2297. stsd: [],
  2298. stsz: [],
  2299. stts: [],
  2300. tfdt: [],
  2301. tfhd: [],
  2302. traf: [],
  2303. trak: [],
  2304. trun: [],
  2305. trex: [],
  2306. tkhd: [],
  2307. vmhd: [],
  2308. smhd: []
  2309. };
  2310. var i;
  2311. for (i in MP4.types) {
  2312. if (MP4.types.hasOwnProperty(i)) {
  2313. MP4.types[i] = [
  2314. i.charCodeAt(0),
  2315. i.charCodeAt(1),
  2316. i.charCodeAt(2),
  2317. i.charCodeAt(3)
  2318. ];
  2319. }
  2320. }
  2321. var videoHdlr = new Uint8Array([
  2322. 0x00, // version 0
  2323. 0x00,
  2324. 0x00,
  2325. 0x00, // flags
  2326. 0x00,
  2327. 0x00,
  2328. 0x00,
  2329. 0x00, // pre_defined
  2330. 0x76,
  2331. 0x69,
  2332. 0x64,
  2333. 0x65, // handler_type: 'vide'
  2334. 0x00,
  2335. 0x00,
  2336. 0x00,
  2337. 0x00, // reserved
  2338. 0x00,
  2339. 0x00,
  2340. 0x00,
  2341. 0x00, // reserved
  2342. 0x00,
  2343. 0x00,
  2344. 0x00,
  2345. 0x00, // reserved
  2346. 0x56,
  2347. 0x69,
  2348. 0x64,
  2349. 0x65,
  2350. 0x6f,
  2351. 0x48,
  2352. 0x61,
  2353. 0x6e,
  2354. 0x64,
  2355. 0x6c,
  2356. 0x65,
  2357. 0x72,
  2358. 0x00 // name: 'VideoHandler'
  2359. ]);
  2360. var audioHdlr = new Uint8Array([
  2361. 0x00, // version 0
  2362. 0x00,
  2363. 0x00,
  2364. 0x00, // flags
  2365. 0x00,
  2366. 0x00,
  2367. 0x00,
  2368. 0x00, // pre_defined
  2369. 0x73,
  2370. 0x6f,
  2371. 0x75,
  2372. 0x6e, // handler_type: 'soun'
  2373. 0x00,
  2374. 0x00,
  2375. 0x00,
  2376. 0x00, // reserved
  2377. 0x00,
  2378. 0x00,
  2379. 0x00,
  2380. 0x00, // reserved
  2381. 0x00,
  2382. 0x00,
  2383. 0x00,
  2384. 0x00, // reserved
  2385. 0x53,
  2386. 0x6f,
  2387. 0x75,
  2388. 0x6e,
  2389. 0x64,
  2390. 0x48,
  2391. 0x61,
  2392. 0x6e,
  2393. 0x64,
  2394. 0x6c,
  2395. 0x65,
  2396. 0x72,
  2397. 0x00 // name: 'SoundHandler'
  2398. ]);
  2399. MP4.HDLR_TYPES = {
  2400. video: videoHdlr,
  2401. audio: audioHdlr
  2402. };
  2403. var dref = new Uint8Array([
  2404. 0x00, // version 0
  2405. 0x00,
  2406. 0x00,
  2407. 0x00, // flags
  2408. 0x00,
  2409. 0x00,
  2410. 0x00,
  2411. 0x01, // entry_count
  2412. 0x00,
  2413. 0x00,
  2414. 0x00,
  2415. 0x0c, // entry_size
  2416. 0x75,
  2417. 0x72,
  2418. 0x6c,
  2419. 0x20, // 'url' type
  2420. 0x00, // version 0
  2421. 0x00,
  2422. 0x00,
  2423. 0x01 // entry_flags
  2424. ]);
  2425. var stco = new Uint8Array([
  2426. 0x00, // version
  2427. 0x00,
  2428. 0x00,
  2429. 0x00, // flags
  2430. 0x00,
  2431. 0x00,
  2432. 0x00,
  2433. 0x00 // entry_count
  2434. ]);
  2435. MP4.STTS = MP4.STSC = MP4.STCO = stco;
  2436. MP4.STSZ = new Uint8Array([
  2437. 0x00, // version
  2438. 0x00,
  2439. 0x00,
  2440. 0x00, // flags
  2441. 0x00,
  2442. 0x00,
  2443. 0x00,
  2444. 0x00, // sample_size
  2445. 0x00,
  2446. 0x00,
  2447. 0x00,
  2448. 0x00
  2449. ]);
  2450. MP4.VMHD = new Uint8Array([
  2451. 0x00, // version
  2452. 0x00,
  2453. 0x00,
  2454. 0x01, // flags
  2455. 0x00,
  2456. 0x00, // graphicsmode
  2457. 0x00,
  2458. 0x00,
  2459. 0x00,
  2460. 0x00,
  2461. 0x00,
  2462. 0x00 // opcolor
  2463. ]);
  2464. MP4.SMHD = new Uint8Array([
  2465. 0x00, // version
  2466. 0x00,
  2467. 0x00,
  2468. 0x00, // flags
  2469. 0x00,
  2470. 0x00, // balance
  2471. 0x00,
  2472. 0x00 // reserved
  2473. ]);
  2474. MP4.STSD = new Uint8Array([
  2475. 0x00, // version 0
  2476. 0x00,
  2477. 0x00,
  2478. 0x00, // flags
  2479. 0x00,
  2480. 0x00,
  2481. 0x00,
  2482. 0x01
  2483. ]); // entry_count
  2484. var majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
  2485. var avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1
  2486. var minorVersion = new Uint8Array([0, 0, 0, 1]);
  2487. MP4.FTYP = MP4.box(
  2488. MP4.types.ftyp,
  2489. majorBrand,
  2490. minorVersion,
  2491. majorBrand,
  2492. avc1Brand
  2493. );
  2494. MP4.DINF = MP4.box(
  2495. MP4.types.dinf,
  2496. MP4.box(MP4.types.dref, dref)
  2497. );
  2498. }
  2499. },
  2500. {
  2501. key: "box",
  2502. value: function box(type) {
  2503. var payload = Array.prototype.slice.call(arguments, 1),
  2504. size = 8,
  2505. i = payload.length,
  2506. len = i,
  2507. result;
  2508. // calculate the total size we need to allocate
  2509. while (i--) {
  2510. size += payload[i].byteLength;
  2511. }
  2512. result = new Uint8Array(size);
  2513. result[0] = (size >> 24) & 0xff;
  2514. result[1] = (size >> 16) & 0xff;
  2515. result[2] = (size >> 8) & 0xff;
  2516. result[3] = size & 0xff;
  2517. result.set(type, 4);
  2518. // copy the payload into the result
  2519. for (i = 0, size = 8; i < len; i++) {
  2520. // copy payload[i] array @ offset size
  2521. result.set(payload[i], size);
  2522. size += payload[i].byteLength;
  2523. }
  2524. return result;
  2525. }
  2526. },
  2527. {
  2528. key: "hdlr",
  2529. value: function hdlr(type) {
  2530. return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
  2531. }
  2532. },
  2533. {
  2534. key: "mdat",
  2535. value: function mdat(data) {
  2536. // console.log( "mdat==> ",data.length );
  2537. return MP4.box(MP4.types.mdat, data);
  2538. }
  2539. },
  2540. {
  2541. key: "mdhd",
  2542. value: function mdhd(timescale, duration) {
  2543. duration *= timescale;
  2544. return MP4.box(
  2545. MP4.types.mdhd,
  2546. new Uint8Array([
  2547. 0x00, // version 0
  2548. 0x00,
  2549. 0x00,
  2550. 0x00, // flags
  2551. 0x00,
  2552. 0x00,
  2553. 0x00,
  2554. 0x02, // creation_time
  2555. 0x00,
  2556. 0x00,
  2557. 0x00,
  2558. 0x03, // modification_time
  2559. (timescale >> 24) & 0xff,
  2560. (timescale >> 16) & 0xff,
  2561. (timescale >> 8) & 0xff,
  2562. timescale & 0xff, // timescale
  2563. duration >> 24,
  2564. (duration >> 16) & 0xff,
  2565. (duration >> 8) & 0xff,
  2566. duration & 0xff, // duration
  2567. 0x55,
  2568. 0xc4, // 'und' language (undetermined)
  2569. 0x00,
  2570. 0x00
  2571. ])
  2572. );
  2573. }
  2574. },
  2575. {
  2576. key: "mdia",
  2577. value: function mdia(track) {
  2578. return MP4.box(
  2579. MP4.types.mdia,
  2580. MP4.mdhd(track.timescale, track.duration),
  2581. MP4.hdlr(track.type),
  2582. MP4.minf(track)
  2583. );
  2584. }
  2585. },
  2586. {
  2587. key: "mfhd",
  2588. value: function mfhd(sequenceNumber) {
  2589. return MP4.box(
  2590. MP4.types.mfhd,
  2591. new Uint8Array([
  2592. 0x00,
  2593. 0x00,
  2594. 0x00,
  2595. 0x00, // flags
  2596. sequenceNumber >> 24,
  2597. (sequenceNumber >> 16) & 0xff,
  2598. (sequenceNumber >> 8) & 0xff,
  2599. sequenceNumber & 0xff
  2600. ]) // sequence_number
  2601. );
  2602. }
  2603. },
  2604. {
  2605. key: "minf",
  2606. value: function minf(track) {
  2607. if (track.type === "audio") {
  2608. return MP4.box(
  2609. MP4.types.minf,
  2610. MP4.box(MP4.types.smhd, MP4.SMHD),
  2611. MP4.DINF,
  2612. MP4.stbl(track)
  2613. );
  2614. } else {
  2615. return MP4.box(
  2616. MP4.types.minf,
  2617. MP4.box(MP4.types.vmhd, MP4.VMHD),
  2618. MP4.DINF,
  2619. MP4.stbl(track)
  2620. );
  2621. }
  2622. }
  2623. },
  2624. {
  2625. key: "moof",
  2626. value: function moof(sn, baseMediaDecodeTime, track) {
  2627. return MP4.box(
  2628. MP4.types.moof,
  2629. MP4.mfhd(sn),
  2630. MP4.traf(track, baseMediaDecodeTime)
  2631. );
  2632. }
  2633. /**
  2634. * @param tracks... (optional) {array} the tracks associated with this movie
  2635. */
  2636. },
  2637. {
  2638. key: "moov",
  2639. value: function moov(tracks) {
  2640. var i = tracks.length,
  2641. boxes = [];
  2642. while (i--) {
  2643. boxes[i] = MP4.trak(tracks[i]);
  2644. }
  2645. return MP4.box.apply(
  2646. null,
  2647. [
  2648. MP4.types.moov,
  2649. MP4.mvhd(tracks[0].timescale, tracks[0].duration)
  2650. ]
  2651. .concat(boxes)
  2652. .concat(MP4.mvex(tracks))
  2653. );
  2654. }
  2655. },
  2656. {
  2657. key: "mvex",
  2658. value: function mvex(tracks) {
  2659. var i = tracks.length,
  2660. boxes = [];
  2661. while (i--) {
  2662. boxes[i] = MP4.trex(tracks[i]);
  2663. }
  2664. return MP4.box.apply(null, [MP4.types.mvex].concat(boxes));
  2665. }
  2666. },
  2667. {
  2668. key: "mvhd",
  2669. value: function mvhd(timescale, duration) {
  2670. duration = 0;//*= timescale;
  2671. var bytes = new Uint8Array([
  2672. 0x00, // version 0
  2673. 0x00,
  2674. 0x00,
  2675. 0x00, // flags
  2676. 0x00,
  2677. 0x00,
  2678. 0x00,
  2679. 0x01, // creation_time
  2680. 0x00,
  2681. 0x00,
  2682. 0x00,
  2683. 0x02, // modification_time
  2684. (timescale >> 24) & 0xff,
  2685. (timescale >> 16) & 0xff,
  2686. (timescale >> 8) & 0xff,
  2687. timescale & 0xff, // timescale
  2688. (duration >> 24) & 0xff,
  2689. (duration >> 16) & 0xff,
  2690. (duration >> 8) & 0xff,
  2691. duration & 0xff, // duration
  2692. 0x00,
  2693. 0x01,
  2694. 0x00,
  2695. 0x00, // 1.0 rate
  2696. 0x01,
  2697. 0x00, // 1.0 volume
  2698. 0x00,
  2699. 0x00, // reserved
  2700. 0x00,
  2701. 0x00,
  2702. 0x00,
  2703. 0x00, // reserved
  2704. 0x00,
  2705. 0x00,
  2706. 0x00,
  2707. 0x00, // reserved
  2708. 0x00,
  2709. 0x01,
  2710. 0x00,
  2711. 0x00,
  2712. 0x00,
  2713. 0x00,
  2714. 0x00,
  2715. 0x00,
  2716. 0x00,
  2717. 0x00,
  2718. 0x00,
  2719. 0x00,
  2720. 0x00,
  2721. 0x00,
  2722. 0x00,
  2723. 0x00,
  2724. 0x00,
  2725. 0x01,
  2726. 0x00,
  2727. 0x00,
  2728. 0x00,
  2729. 0x00,
  2730. 0x00,
  2731. 0x00,
  2732. 0x00,
  2733. 0x00,
  2734. 0x00,
  2735. 0x00,
  2736. 0x00,
  2737. 0x00,
  2738. 0x00,
  2739. 0x00,
  2740. 0x40,
  2741. 0x00,
  2742. 0x00,
  2743. 0x00, // transformation: unity matrix
  2744. 0x00,
  2745. 0x00,
  2746. 0x00,
  2747. 0x00,
  2748. 0x00,
  2749. 0x00,
  2750. 0x00,
  2751. 0x00,
  2752. 0x00,
  2753. 0x00,
  2754. 0x00,
  2755. 0x00,
  2756. 0x00,
  2757. 0x00,
  2758. 0x00,
  2759. 0x00,
  2760. 0x00,
  2761. 0x00,
  2762. 0x00,
  2763. 0x00,
  2764. 0x00,
  2765. 0x00,
  2766. 0x00,
  2767. 0x00, // pre_defined
  2768. 0xff,
  2769. 0xff,
  2770. 0xff,
  2771. 0xff // next_track_ID
  2772. ]);
  2773. return MP4.box(MP4.types.mvhd, bytes);
  2774. }
  2775. },
  2776. {
  2777. key: "sdtp",
  2778. value: function sdtp(track) {
  2779. var samples = track.samples || [],
  2780. bytes = new Uint8Array(4 + samples.length),
  2781. flags,
  2782. i;
  2783. // leave the full box header (4 bytes) all zero
  2784. // write the sample table
  2785. for (i = 0; i < samples.length; i++) {
  2786. flags = samples[i].flags;
  2787. bytes[i + 4] =
  2788. (flags.dependsOn << 4) |
  2789. (flags.isDependedOn << 2) |
  2790. flags.hasRedundancy;
  2791. }
  2792. return MP4.box(MP4.types.sdtp, bytes);
  2793. }
  2794. },
  2795. {
  2796. key: "stbl",
  2797. value: function stbl(track) {
  2798. return MP4.box(
  2799. MP4.types.stbl,
  2800. MP4.stsd(track),
  2801. MP4.box(MP4.types.stts, MP4.STTS),
  2802. MP4.box(MP4.types.stsc, MP4.STSC),
  2803. MP4.box(MP4.types.stsz, MP4.STSZ),
  2804. MP4.box(MP4.types.stco, MP4.STCO)
  2805. );
  2806. }
  2807. },
  2808. {
  2809. key: "avc1",
  2810. value: function avc1(track) {
  2811. var sps = [],
  2812. pps = [],
  2813. i,
  2814. data,
  2815. len;
  2816. // assemble the SPSs
  2817. for (i = 0; i < track.sps.length; i++) {
  2818. data = track.sps[i];
  2819. len = data.byteLength;
  2820. sps.push((len >>> 8) & 0xff);
  2821. sps.push(len & 0xff);
  2822. sps = sps.concat(Array.prototype.slice.call(data)); // SPS
  2823. }
  2824. // assemble the PPSs
  2825. for (i = 0; i < track.pps.length; i++) {
  2826. data = track.pps[i];
  2827. len = data.byteLength;
  2828. pps.push((len >>> 8) & 0xff);
  2829. pps.push(len & 0xff);
  2830. pps = pps.concat(Array.prototype.slice.call(data));
  2831. }
  2832. var avcc = MP4.box(
  2833. MP4.types.avcC,
  2834. new Uint8Array(
  2835. [
  2836. 0x01, // version
  2837. sps[3], // profile
  2838. sps[4], // profile compat
  2839. sps[5], // level
  2840. 0xfc | 3, // lengthSizeMinusOne, hard-coded to 4 bytes
  2841. 0xe0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets
  2842. ]
  2843. .concat(sps)
  2844. .concat([
  2845. track.pps.length // numOfPictureParameterSets
  2846. ])
  2847. .concat(pps)
  2848. )
  2849. ),
  2850. // "PPS"
  2851. width = track.width,
  2852. height = track.height;
  2853. //console.log('avcc:' + Hex.hexDump(avcc));
  2854. return MP4.box(
  2855. MP4.types.avc1,
  2856. new Uint8Array([
  2857. 0x00,
  2858. 0x00,
  2859. 0x00, // reserved
  2860. 0x00,
  2861. 0x00,
  2862. 0x00, // reserved
  2863. 0x00,
  2864. 0x01, // data_reference_index
  2865. 0x00,
  2866. 0x00, // pre_defined
  2867. 0x00,
  2868. 0x00, // reserved
  2869. 0x00,
  2870. 0x00,
  2871. 0x00,
  2872. 0x00,
  2873. 0x00,
  2874. 0x00,
  2875. 0x00,
  2876. 0x00,
  2877. 0x00,
  2878. 0x00,
  2879. 0x00,
  2880. 0x00, // pre_defined
  2881. (width >> 8) & 0xff,
  2882. width & 0xff, // width
  2883. (height >> 8) & 0xff,
  2884. height & 0xff, // height
  2885. 0x00,
  2886. 0x48,
  2887. 0x00,
  2888. 0x00, // horizresolution
  2889. 0x00,
  2890. 0x48,
  2891. 0x00,
  2892. 0x00, // vertresolution
  2893. 0x00,
  2894. 0x00,
  2895. 0x00,
  2896. 0x00, // reserved
  2897. 0x00,
  2898. 0x01, // frame_count
  2899. 0x12,
  2900. 0x6a,
  2901. 0x65,
  2902. 0x66,
  2903. 0x66, // wfs.js
  2904. 0x2d,
  2905. 0x79,
  2906. 0x61,
  2907. 0x6e,
  2908. 0x2f,
  2909. 0x2f,
  2910. 0x2f,
  2911. 0x67,
  2912. 0x77,
  2913. 0x66,
  2914. 0x73,
  2915. 0x2e,
  2916. 0x6a,
  2917. 0x73,
  2918. 0x00,
  2919. 0x00,
  2920. 0x00,
  2921. 0x00,
  2922. 0x00,
  2923. 0x00,
  2924. 0x00,
  2925. 0x00,
  2926. 0x00,
  2927. 0x00,
  2928. 0x00,
  2929. 0x00,
  2930. 0x00, // compressorname
  2931. 0x00,
  2932. 0x18, // depth = 24
  2933. 0x11,
  2934. 0x11
  2935. ]), // pre_defined = -1
  2936. avcc,
  2937. MP4.box(
  2938. MP4.types.btrt,
  2939. new Uint8Array([
  2940. 0x00,
  2941. 0x1c,
  2942. 0x9c,
  2943. 0x80, // bufferSizeDB
  2944. 0x00,
  2945. 0x2d,
  2946. 0xc6,
  2947. 0xc0, // maxBitrate
  2948. 0x00,
  2949. 0x2d,
  2950. 0xc6,
  2951. 0xc0
  2952. ])
  2953. ) // avgBitrate
  2954. );
  2955. }
  2956. },
  2957. {
  2958. key: "esds",
  2959. value: function esds(track) {
  2960. var configlen = track.config.length;
  2961. return new Uint8Array(
  2962. [
  2963. 0x00, // version 0
  2964. 0x00,
  2965. 0x00,
  2966. 0x00, // flags
  2967. 0x03, // descriptor_type
  2968. 0x17 + configlen, // length
  2969. 0x00,
  2970. 0x01, //es_id
  2971. 0x00, // stream_priority
  2972. 0x04, // descriptor_type
  2973. 0x0f + configlen, // length
  2974. 0x40, //codec : mpeg4_audio
  2975. 0x15, // stream_type
  2976. 0x00,
  2977. 0x00,
  2978. 0x00, // buffer_size
  2979. 0x00,
  2980. 0x00,
  2981. 0x00,
  2982. 0x00, // maxBitrate
  2983. 0x00,
  2984. 0x00,
  2985. 0x00,
  2986. 0x00, // avgBitrate
  2987. 0x05 // descriptor_type
  2988. ]
  2989. .concat([configlen])
  2990. .concat(track.config)
  2991. .concat([0x06, 0x01, 0x02])
  2992. ); // GASpecificConfig)); // length + audio config descriptor
  2993. }
  2994. },
  2995. {
  2996. key: "mp4a",
  2997. value: function mp4a(track) {
  2998. var audiosamplerate = track.audiosamplerate;
  2999. return MP4.box(
  3000. MP4.types.mp4a,
  3001. new Uint8Array([
  3002. 0x00,
  3003. 0x00,
  3004. 0x00, // reserved
  3005. 0x00,
  3006. 0x00,
  3007. 0x00, // reserved
  3008. 0x00,
  3009. 0x01, // data_reference_index
  3010. 0x00,
  3011. 0x00,
  3012. 0x00,
  3013. 0x00,
  3014. 0x00,
  3015. 0x00,
  3016. 0x00,
  3017. 0x00, // reserved
  3018. 0x00,
  3019. track.channelCount, // channelcount
  3020. 0x00,
  3021. 0x10, // sampleSize:16bits
  3022. 0x00,
  3023. 0x00,
  3024. 0x00,
  3025. 0x00, // reserved2
  3026. (audiosamplerate >> 8) & 0xff,
  3027. audiosamplerate & 0xff, //
  3028. 0x00,
  3029. 0x00
  3030. ]),
  3031. MP4.box(MP4.types.esds, MP4.esds(track))
  3032. );
  3033. }
  3034. },
  3035. {
  3036. key: "stsd",
  3037. value: function stsd(track) {
  3038. if (track.type === "audio") {
  3039. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
  3040. } else {
  3041. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
  3042. }
  3043. }
  3044. },
  3045. {
  3046. key: "tkhd",
  3047. value: function tkhd(track) {
  3048. var id = track.id,
  3049. duration = track.duration * track.timescale,
  3050. width = track.width,
  3051. height = track.height;
  3052. // console.log( "tkhd==> ",track.id, track.duration, track.timescale, width,height );
  3053. return MP4.box(
  3054. MP4.types.tkhd,
  3055. new Uint8Array([
  3056. 0x00, // version 0
  3057. 0x00,
  3058. 0x00,
  3059. 0x07, // flags
  3060. 0x00,
  3061. 0x00,
  3062. 0x00,
  3063. 0x00, // creation_time
  3064. 0x00,
  3065. 0x00,
  3066. 0x00,
  3067. 0x00, // modification_time
  3068. (id >> 24) & 0xff,
  3069. (id >> 16) & 0xff,
  3070. (id >> 8) & 0xff,
  3071. id & 0xff, // track_ID
  3072. 0x00,
  3073. 0x00,
  3074. 0x00,
  3075. 0x00, // reserved
  3076. duration >> 24,
  3077. (duration >> 16) & 0xff,
  3078. (duration >> 8) & 0xff,
  3079. duration & 0xff, // duration
  3080. 0x00,
  3081. 0x00,
  3082. 0x00,
  3083. 0x00,
  3084. 0x00,
  3085. 0x00,
  3086. 0x00,
  3087. 0x00, // reserved
  3088. 0x00,
  3089. 0x00, // layer
  3090. 0x00,
  3091. 0x00, // alternate_group
  3092. 0x00,
  3093. 0x00, // non-audio track volume
  3094. 0x00,
  3095. 0x00, // reserved
  3096. 0x00,
  3097. 0x01,
  3098. 0x00,
  3099. 0x00,
  3100. 0x00,
  3101. 0x00,
  3102. 0x00,
  3103. 0x00,
  3104. 0x00,
  3105. 0x00,
  3106. 0x00,
  3107. 0x00,
  3108. 0x00,
  3109. 0x00,
  3110. 0x00,
  3111. 0x00,
  3112. 0x00,
  3113. 0x01,
  3114. 0x00,
  3115. 0x00,
  3116. 0x00,
  3117. 0x00,
  3118. 0x00,
  3119. 0x00,
  3120. 0x00,
  3121. 0x00,
  3122. 0x00,
  3123. 0x00,
  3124. 0x00,
  3125. 0x00,
  3126. 0x00,
  3127. 0x00,
  3128. 0x40,
  3129. 0x00,
  3130. 0x00,
  3131. 0x00, // transformation: unity matrix
  3132. (width >> 8) & 0xff,
  3133. width & 0xff,
  3134. 0x00,
  3135. 0x00, // width
  3136. (height >> 8) & 0xff,
  3137. height & 0xff,
  3138. 0x00,
  3139. 0x00 // height
  3140. ])
  3141. );
  3142. }
  3143. },
  3144. {
  3145. key: "traf",
  3146. value: function traf(track, baseMediaDecodeTime) {
  3147. var sampleDependencyTable = MP4.sdtp(track),
  3148. id = track.id;
  3149. // console.log( "traf==> ",id ,baseMediaDecodeTime);
  3150. return MP4.box(
  3151. MP4.types.traf,
  3152. MP4.box(
  3153. MP4.types.tfhd,
  3154. new Uint8Array([
  3155. 0x00, // version 0
  3156. 0x00,
  3157. 0x00,
  3158. 0x00, // flags
  3159. id >> 24,
  3160. (id >> 16) & 0xff,
  3161. (id >> 8) & 0xff,
  3162. id & 0xff
  3163. ]) // track_ID
  3164. ),
  3165. MP4.box(
  3166. MP4.types.tfdt,
  3167. new Uint8Array([
  3168. 0x00, // version 0
  3169. 0x00,
  3170. 0x00,
  3171. 0x00, // flags
  3172. baseMediaDecodeTime >> 24,
  3173. (baseMediaDecodeTime >> 16) & 0xff,
  3174. (baseMediaDecodeTime >> 8) & 0xff,
  3175. baseMediaDecodeTime & 0xff
  3176. ]) // baseMediaDecodeTime
  3177. ),
  3178. MP4.trun(
  3179. track,
  3180. sampleDependencyTable.length +
  3181. 16 + // tfhd
  3182. 16 + // tfdt
  3183. 8 + // traf header
  3184. 16 + // mfhd
  3185. 8 + // moof header
  3186. 8
  3187. ), // mdat header
  3188. sampleDependencyTable
  3189. );
  3190. }
  3191. /**
  3192. * Generate a track box.
  3193. * @param track {object} a track definition
  3194. * @return {Uint8Array} the track box
  3195. */
  3196. },
  3197. {
  3198. key: "trak",
  3199. value: function trak(track) {
  3200. track.duration = track.duration || 0xffffffff;
  3201. return MP4.box(
  3202. MP4.types.trak,
  3203. MP4.tkhd(track),
  3204. MP4.mdia(track)
  3205. );
  3206. }
  3207. },
  3208. {
  3209. key: "trex",
  3210. value: function trex(track) {
  3211. var id = track.id;
  3212. return MP4.box(
  3213. MP4.types.trex,
  3214. new Uint8Array([
  3215. 0x00, // version 0
  3216. 0x00,
  3217. 0x00,
  3218. 0x00, // flags
  3219. id >> 24,
  3220. (id >> 16) & 0xff,
  3221. (id >> 8) & 0xff,
  3222. id & 0xff, // track_ID
  3223. 0x00,
  3224. 0x00,
  3225. 0x00,
  3226. 0x01, // default_sample_description_index
  3227. 0x00,
  3228. 0x00,
  3229. 0x00,
  3230. 0x00, // default_sample_duration
  3231. 0x00,
  3232. 0x00,
  3233. 0x00,
  3234. 0x00, // default_sample_size
  3235. 0x00,
  3236. 0x01,
  3237. 0x00,
  3238. 0x01 // default_sample_flags
  3239. ])
  3240. );
  3241. }
  3242. },
  3243. {
  3244. key: "trun",
  3245. value: function trun(track, offset) {
  3246. var samples = track.samples || [],
  3247. len = samples.length,
  3248. arraylen = 12 + 16 * len,
  3249. array = new Uint8Array(arraylen),
  3250. i,
  3251. sample,
  3252. duration,
  3253. size,
  3254. flags,
  3255. cts;
  3256. //sample = samples[0];
  3257. // console.log( "trun==> ",sample.duration, sample.cts ,sample.size,len );
  3258. offset += 8 + arraylen;
  3259. array.set(
  3260. [
  3261. 0x00, // version 0
  3262. 0x00,
  3263. 0x0f,
  3264. 0x01, // flags
  3265. (len >>> 24) & 0xff,
  3266. (len >>> 16) & 0xff,
  3267. (len >>> 8) & 0xff,
  3268. len & 0xff, // sample_count
  3269. (offset >>> 24) & 0xff,
  3270. (offset >>> 16) & 0xff,
  3271. (offset >>> 8) & 0xff,
  3272. offset & 0xff // data_offset
  3273. ],
  3274. 0
  3275. );
  3276. for (i = 0; i < len; i++) {
  3277. sample = samples[i];
  3278. duration = sample.duration;
  3279. size = sample.size;
  3280. flags = sample.flags;
  3281. cts = sample.cts;
  3282. array.set(
  3283. [
  3284. (duration >>> 24) & 0xff,
  3285. (duration >>> 16) & 0xff,
  3286. (duration >>> 8) & 0xff,
  3287. duration & 0xff, // sample_duration
  3288. (size >>> 24) & 0xff,
  3289. (size >>> 16) & 0xff,
  3290. (size >>> 8) & 0xff,
  3291. size & 0xff, // sample_size
  3292. (flags.isLeading << 2) | flags.dependsOn,
  3293. (flags.isDependedOn << 6) |
  3294. (flags.hasRedundancy << 4) |
  3295. (flags.paddingValue << 1) |
  3296. flags.isNonSync,
  3297. flags.degradPrio & (0xf0 << 8),
  3298. flags.degradPrio & 0x0f, // sample_flags
  3299. (cts >>> 24) & 0xff,
  3300. (cts >>> 16) & 0xff,
  3301. (cts >>> 8) & 0xff,
  3302. cts & 0xff // sample_composition_time_offset
  3303. ],
  3304. 12 + 16 * i
  3305. );
  3306. }
  3307. return MP4.box(MP4.types.trun, array);
  3308. }
  3309. },
  3310. {
  3311. key: "initSegment",
  3312. value: function initSegment(tracks) {
  3313. if (!MP4.types) {
  3314. MP4.init();
  3315. }
  3316. var movie = MP4.moov(tracks),
  3317. result;
  3318. result = new Uint8Array(
  3319. MP4.FTYP.byteLength + movie.byteLength
  3320. );
  3321. result.set(MP4.FTYP);
  3322. result.set(movie, MP4.FTYP.byteLength);
  3323. return result;
  3324. }
  3325. }
  3326. ]);
  3327. return MP4;
  3328. })();
  3329. exports.default = MP4;
  3330. },
  3331. {}
  3332. ],
  3333. 13: [
  3334. function (require, module, exports) {
  3335. "use strict";
  3336. Object.defineProperty(exports, "__esModule", {
  3337. value: true
  3338. });
  3339. var _createClass = (function () {
  3340. function defineProperties(target, props) {
  3341. for (var i = 0; i < props.length; i++) {
  3342. var descriptor = props[i];
  3343. descriptor.enumerable = descriptor.enumerable || false;
  3344. descriptor.configurable = true;
  3345. if ("value" in descriptor) descriptor.writable = true;
  3346. Object.defineProperty(target, descriptor.key, descriptor);
  3347. }
  3348. }
  3349. return function (Constructor, protoProps, staticProps) {
  3350. if (protoProps)
  3351. defineProperties(Constructor.prototype, protoProps);
  3352. if (staticProps) defineProperties(Constructor, staticProps);
  3353. return Constructor;
  3354. };
  3355. })();
  3356. /**
  3357. * fMP4 remuxer
  3358. */
  3359. var _aac = require("../helper/aac");
  3360. var _aac2 = _interopRequireDefault(_aac);
  3361. var _events = require("../events");
  3362. var _events2 = _interopRequireDefault(_events);
  3363. var _logger = require("../utils/logger");
  3364. var _mp4Generator = require("../remux/mp4-generator");
  3365. var _mp4Generator2 = _interopRequireDefault(_mp4Generator);
  3366. var _errors = require("../errors");
  3367. require("../utils/polyfill");
  3368. function _interopRequireDefault(obj) {
  3369. return obj && obj.__esModule ? obj : { default: obj };
  3370. }
  3371. function _classCallCheck(instance, Constructor) {
  3372. if (!(instance instanceof Constructor)) {
  3373. throw new TypeError("Cannot call a class as a function");
  3374. }
  3375. }
  3376. var MP4Remuxer = (function () {
  3377. function MP4Remuxer(observer, id, config) {
  3378. _classCallCheck(this, MP4Remuxer);
  3379. this.observer = observer;
  3380. this.id = id;
  3381. this.config = config;
  3382. this.ISGenerated = false;
  3383. this.PES2MP4SCALEFACTOR = 4;
  3384. this.PES_TIMESCALE = 90000;
  3385. this.MP4_TIMESCALE = this.PES_TIMESCALE / this.PES2MP4SCALEFACTOR;
  3386. this.nextAvcDts = 90300;
  3387. this.H264_TIMEBASE = 3000;
  3388. }
  3389. _createClass(MP4Remuxer, [
  3390. {
  3391. key: "destroy",
  3392. value: function destroy() { }
  3393. },
  3394. {
  3395. key: "insertDiscontinuity",
  3396. value: function insertDiscontinuity() {
  3397. this._initPTS = this._initDTS = undefined;
  3398. }
  3399. },
  3400. {
  3401. key: "switchLevel",
  3402. value: function switchLevel() {
  3403. this.ISGenerated = false;
  3404. }
  3405. },
  3406. {
  3407. key: "pushVideo",
  3408. value: function pushVideo(
  3409. level,
  3410. sn,
  3411. videoTrack,
  3412. timeOffset,
  3413. contiguous
  3414. ) {
  3415. this.level = level;
  3416. this.sn = sn;
  3417. var videoData = void 0;
  3418. // generate Init Segment if needed
  3419. if (!this.ISGenerated) {
  3420. this.generateVideoIS(videoTrack, timeOffset);
  3421. }
  3422. if (this.ISGenerated) {
  3423. if (videoTrack.samples.length) {
  3424. this.remuxVideo_2(videoTrack, timeOffset, contiguous);
  3425. }
  3426. }
  3427. }
  3428. },
  3429. {
  3430. key: "remuxVideo_2",
  3431. value: function remuxVideo_2(
  3432. track,
  3433. timeOffset,
  3434. contiguous,
  3435. audioTrackLength
  3436. ) {
  3437. var offset = 8,
  3438. pesTimeScale = this.PES_TIMESCALE,
  3439. pes2mp4ScaleFactor = this.PES2MP4SCALEFACTOR,
  3440. mp4SampleDuration,
  3441. mdat,
  3442. moof,
  3443. firstPTS,
  3444. firstDTS,
  3445. nextDTS,
  3446. inputSamples = track.samples,
  3447. outputSamples = [];
  3448. /* concatenate the video data and construct the mdat in place
  3449. (need 8 more bytes to fill length and mpdat type) */
  3450. mdat = new Uint8Array(track.len + 4 * track.nbNalu + 8);
  3451. var view = new DataView(mdat.buffer);
  3452. view.setUint32(0, mdat.byteLength);
  3453. mdat.set(_mp4Generator2.default.types.mdat, 4);
  3454. var sampleDuration = 0;
  3455. var ptsnorm = void 0,
  3456. dtsnorm = void 0,
  3457. mp4Sample = void 0,
  3458. lastDTS = void 0;
  3459. for (var i = 0; i < inputSamples.length; i++) {
  3460. var avcSample = inputSamples[i],
  3461. mp4SampleLength = 0,
  3462. compositionTimeOffset = void 0;
  3463. // convert NALU bitstream to MP4 format (prepend NALU with size field)
  3464. while (avcSample.units.units.length) {
  3465. var unit = avcSample.units.units.shift();
  3466. view.setUint32(offset, unit.data.byteLength);
  3467. offset += 4;
  3468. mdat.set(unit.data, offset);
  3469. offset += unit.data.byteLength;
  3470. mp4SampleLength += 4 + unit.data.byteLength;
  3471. }
  3472. var pts = avcSample.pts - this._initPTS;
  3473. var dts = avcSample.dts - this._initDTS;
  3474. dts = Math.min(pts, dts);
  3475. if (lastDTS !== undefined) {
  3476. ptsnorm = this._PTSNormalize(pts, lastDTS);
  3477. dtsnorm = this._PTSNormalize(dts, lastDTS);
  3478. sampleDuration = dtsnorm - lastDTS;
  3479. if (sampleDuration <= 0) {
  3480. _logger.logger.log(
  3481. "invalid sample duration at PTS/DTS: " +
  3482. avcSample.pts +
  3483. "/" +
  3484. avcSample.dts +
  3485. "|dts norm: " +
  3486. dtsnorm +
  3487. "|lastDTS: " +
  3488. lastDTS +
  3489. ":" +
  3490. sampleDuration
  3491. );
  3492. sampleDuration = 1;
  3493. }
  3494. } else {
  3495. var nextAvcDts = this.nextAvcDts,
  3496. delta;
  3497. ptsnorm = this._PTSNormalize(pts, nextAvcDts);
  3498. dtsnorm = this._PTSNormalize(dts, nextAvcDts);
  3499. if (nextAvcDts) {
  3500. delta = Math.round(dtsnorm - nextAvcDts);
  3501. if (/*contiguous ||*/ Math.abs(delta) < 600) {
  3502. if (delta) {
  3503. if (delta > 1) {
  3504. _logger.logger.log(
  3505. "AVC:" +
  3506. delta +
  3507. " ms hole between fragments detected,filling it"
  3508. );
  3509. } else if (delta < -1) {
  3510. _logger.logger.log(
  3511. "AVC:" +
  3512. -delta +
  3513. " ms overlapping between fragments detected"
  3514. );
  3515. }
  3516. dtsnorm = nextAvcDts;
  3517. ptsnorm = Math.max(ptsnorm - delta, dtsnorm);
  3518. _logger.logger.log(
  3519. "Video/PTS/DTS adjusted: " +
  3520. ptsnorm +
  3521. "/" +
  3522. dtsnorm +
  3523. ",delta:" +
  3524. delta
  3525. );
  3526. }
  3527. }
  3528. }
  3529. this.firstPTS = Math.max(0, ptsnorm);
  3530. this.firstDTS = Math.max(0, dtsnorm);
  3531. sampleDuration = 0.03;
  3532. }
  3533. outputSamples.push({
  3534. size: mp4SampleLength,
  3535. duration: this.H264_TIMEBASE,
  3536. cts: 0,
  3537. flags: {
  3538. isLeading: 0,
  3539. isDependedOn: 0,
  3540. hasRedundancy: 0,
  3541. degradPrio: 0,
  3542. dependsOn: avcSample.key ? 2 : 1,
  3543. isNonSync: avcSample.key ? 0 : 1
  3544. }
  3545. });
  3546. lastDTS = dtsnorm;
  3547. }
  3548. var lastSampleDuration = 0;
  3549. if (outputSamples.length >= 2) {
  3550. lastSampleDuration =
  3551. outputSamples[outputSamples.length - 2].duration;
  3552. outputSamples[0].duration = lastSampleDuration;
  3553. }
  3554. this.nextAvcDts = dtsnorm + lastSampleDuration;
  3555. var dropped = track.dropped;
  3556. track.len = 0;
  3557. track.nbNalu = 0;
  3558. track.dropped = 0;
  3559. if (
  3560. outputSamples.length &&
  3561. navigator.userAgent.toLowerCase().indexOf("chrome") > -1
  3562. ) {
  3563. var flags = outputSamples[0].flags;
  3564. flags.dependsOn = 2;
  3565. flags.isNonSync = 0;
  3566. }
  3567. track.samples = outputSamples;
  3568. moof = _mp4Generator2.default.moof(
  3569. track.sequenceNumber++,
  3570. dtsnorm,
  3571. track
  3572. );
  3573. track.samples = [];
  3574. var data = {
  3575. id: this.id,
  3576. level: this.level,
  3577. sn: this.sn,
  3578. data1: moof,
  3579. data2: mdat,
  3580. startPTS: ptsnorm,
  3581. endPTS: ptsnorm,
  3582. startDTS: dtsnorm,
  3583. endDTS: dtsnorm,
  3584. type: "video",
  3585. nb: outputSamples.length,
  3586. dropped: dropped
  3587. };
  3588. this.observer.trigger(
  3589. _events2.default.FRAG_PARSING_DATA,
  3590. data
  3591. );
  3592. return data;
  3593. }
  3594. },
  3595. {
  3596. key: "generateVideoIS",
  3597. value: function generateVideoIS(videoTrack, timeOffset) {
  3598. var observer = this.observer,
  3599. videoSamples = videoTrack.samples,
  3600. pesTimeScale = this.PES_TIMESCALE,
  3601. tracks = {},
  3602. data = {
  3603. id: this.id,
  3604. level: this.level,
  3605. sn: this.sn,
  3606. tracks: tracks,
  3607. unique: false
  3608. },
  3609. computePTSDTS = this._initPTS === undefined,
  3610. initPTS,
  3611. initDTS;
  3612. if (computePTSDTS) {
  3613. initPTS = initDTS = Infinity;
  3614. }
  3615. if (videoTrack.sps && videoTrack.pps && videoSamples.length) {
  3616. videoTrack.timescale = 90000; //this.MP4_TIMESCALE;
  3617. tracks.video = {
  3618. container: "video/mp4",
  3619. codec: videoTrack.codec,
  3620. initSegment: _mp4Generator2.default.initSegment([
  3621. videoTrack
  3622. ]),
  3623. metadata: {
  3624. width: videoTrack.width,
  3625. height: videoTrack.height
  3626. }
  3627. };
  3628. if (computePTSDTS) {
  3629. initPTS = Math.min(
  3630. initPTS,
  3631. videoSamples[0].pts - this.H264_TIMEBASE
  3632. );
  3633. initDTS = Math.min(
  3634. initDTS,
  3635. videoSamples[0].dts - this.H264_TIMEBASE
  3636. );
  3637. }
  3638. }
  3639. if (Object.keys(tracks).length) {
  3640. observer.trigger(
  3641. _events2.default.FRAG_PARSING_INIT_SEGMENT,
  3642. data
  3643. );
  3644. this.ISGenerated = true;
  3645. if (computePTSDTS) {
  3646. this._initPTS = initPTS;
  3647. this._initDTS = initDTS;
  3648. }
  3649. } else {
  3650. console.log(
  3651. "generateVideoIS ERROR==> ",
  3652. _errors.ErrorTypes.MEDIA_ERROR
  3653. );
  3654. }
  3655. }
  3656. },
  3657. {
  3658. key: "remux",
  3659. value: function remux(
  3660. level,
  3661. sn,
  3662. audioTrack,
  3663. videoTrack,
  3664. id3Track,
  3665. textTrack,
  3666. timeOffset,
  3667. contiguous
  3668. ) {
  3669. this.level = level;
  3670. this.sn = sn;
  3671. // generate Init Segment if needed
  3672. if (!this.ISGenerated) {
  3673. this.generateIS(audioTrack, videoTrack, timeOffset);
  3674. }
  3675. if (this.ISGenerated) {
  3676. // Purposefully remuxing audio before video, so that remuxVideo can use nextAacPts, which is
  3677. // calculated in remuxAudio.
  3678. //logger.log('nb AAC samples:' + audioTrack.samples.length);
  3679. if (audioTrack.samples.length) {
  3680. var audioData = this.remuxAudio(
  3681. audioTrack,
  3682. timeOffset,
  3683. contiguous
  3684. );
  3685. //logger.log('nb AVC samples:' + videoTrack.samples.length);
  3686. if (videoTrack.samples.length) {
  3687. var audioTrackLength = void 0;
  3688. if (audioData) {
  3689. audioTrackLength =
  3690. audioData.endPTS - audioData.startPTS;
  3691. }
  3692. this.remuxVideo(
  3693. videoTrack,
  3694. timeOffset,
  3695. contiguous,
  3696. audioTrackLength
  3697. );
  3698. }
  3699. } else {
  3700. var videoData = void 0;
  3701. //logger.log('nb AVC samples:' + videoTrack.samples.length);
  3702. if (videoTrack.samples.length) {
  3703. videoData = this.remuxVideo(
  3704. videoTrack,
  3705. timeOffset,
  3706. contiguous
  3707. );
  3708. }
  3709. if (videoData && audioTrack.codec) {
  3710. this.remuxEmptyAudio(
  3711. audioTrack,
  3712. timeOffset,
  3713. contiguous,
  3714. videoData
  3715. );
  3716. }
  3717. }
  3718. }
  3719. //logger.log('nb ID3 samples:' + audioTrack.samples.length);
  3720. if (id3Track.samples.length) {
  3721. this.remuxID3(id3Track, timeOffset);
  3722. }
  3723. //logger.log('nb ID3 samples:' + audioTrack.samples.length);
  3724. if (textTrack.samples.length) {
  3725. this.remuxText(textTrack, timeOffset);
  3726. }
  3727. //notify end of parsing
  3728. this.observer.trigger(_events2.default.FRAG_PARSED, {
  3729. id: this.id,
  3730. level: this.level,
  3731. sn: this.sn
  3732. });
  3733. }
  3734. },
  3735. {
  3736. key: "generateIS",
  3737. value: function generateIS(audioTrack, videoTrack, timeOffset) {
  3738. var observer = this.observer,
  3739. audioSamples = audioTrack.samples,
  3740. videoSamples = videoTrack.samples,
  3741. pesTimeScale = this.PES_TIMESCALE,
  3742. tracks = {},
  3743. data = {
  3744. id: this.id,
  3745. level: this.level,
  3746. sn: this.sn,
  3747. tracks: tracks,
  3748. unique: false
  3749. },
  3750. computePTSDTS = this._initPTS === undefined,
  3751. initPTS,
  3752. initDTS;
  3753. if (computePTSDTS) {
  3754. initPTS = initDTS = Infinity;
  3755. }
  3756. if (audioTrack.config && audioSamples.length) {
  3757. audioTrack.timescale = audioTrack.audiosamplerate;
  3758. // MP4 duration (track duration in seconds multiplied by timescale) is coded on 32 bits
  3759. // we know that each AAC sample contains 1024 frames....
  3760. // in order to avoid overflowing the 32 bit counter for large duration, we use smaller timescale (timescale/gcd)
  3761. // we just need to ensure that AAC sample duration will still be an integer (will be 1024/gcd)
  3762. if (
  3763. audioTrack.timescale * audioTrack.duration >
  3764. Math.pow(2, 32)
  3765. ) {
  3766. var greatestCommonDivisor = function greatestCommonDivisor(
  3767. a,
  3768. b
  3769. ) {
  3770. if (!b) {
  3771. return a;
  3772. }
  3773. return greatestCommonDivisor(b, a % b);
  3774. };
  3775. audioTrack.timescale =
  3776. audioTrack.audiosamplerate /
  3777. greatestCommonDivisor(audioTrack.audiosamplerate, 1024);
  3778. }
  3779. _logger.logger.log(
  3780. "audio mp4 timescale :" + audioTrack.timescale
  3781. );
  3782. tracks.audio = {
  3783. container: "audio/mp4",
  3784. codec: audioTrack.codec,
  3785. initSegment: _mp4Generator2.default.initSegment([
  3786. audioTrack
  3787. ]),
  3788. metadata: {
  3789. channelCount: audioTrack.channelCount
  3790. }
  3791. };
  3792. if (computePTSDTS) {
  3793. // remember first PTS of this demuxing context. for audio, PTS + DTS ...
  3794. initPTS = initDTS =
  3795. audioSamples[0].pts - pesTimeScale * timeOffset;
  3796. }
  3797. }
  3798. if (videoTrack.sps && videoTrack.pps && videoSamples.length) {
  3799. videoTrack.timescale = this.MP4_TIMESCALE;
  3800. tracks.video = {
  3801. container: "video/mp4",
  3802. codec: videoTrack.codec,
  3803. initSegment: _mp4Generator2.default.initSegment([
  3804. videoTrack
  3805. ]),
  3806. metadata: {
  3807. width: videoTrack.width,
  3808. height: videoTrack.height
  3809. }
  3810. };
  3811. if (computePTSDTS) {
  3812. initPTS = Math.min(
  3813. initPTS,
  3814. videoSamples[0].pts - pesTimeScale * timeOffset
  3815. );
  3816. initDTS = Math.min(
  3817. initDTS,
  3818. videoSamples[0].dts - pesTimeScale * timeOffset
  3819. );
  3820. }
  3821. }
  3822. if (Object.keys(tracks).length) {
  3823. observer.trigger(
  3824. _events2.default.FRAG_PARSING_INIT_SEGMENT,
  3825. data
  3826. );
  3827. this.ISGenerated = true;
  3828. if (computePTSDTS) {
  3829. this._initPTS = initPTS;
  3830. this._initDTS = initDTS;
  3831. }
  3832. } else {
  3833. observer.trigger(_events2.default.ERROR, {
  3834. type: _errors.ErrorTypes.MEDIA_ERROR,
  3835. id: this.id,
  3836. details: _errors.ErrorDetails.FRAG_PARSING_ERROR,
  3837. fatal: false,
  3838. reason: "no audio/video samples found"
  3839. });
  3840. }
  3841. }
  3842. },
  3843. {
  3844. key: "remuxVideo",
  3845. value: function remuxVideo(
  3846. track,
  3847. timeOffset,
  3848. contiguous,
  3849. audioTrackLength
  3850. ) {
  3851. var offset = 8,
  3852. pesTimeScale = this.PES_TIMESCALE,
  3853. pes2mp4ScaleFactor = this.PES2MP4SCALEFACTOR,
  3854. mp4SampleDuration,
  3855. mdat,
  3856. moof,
  3857. firstPTS,
  3858. firstDTS,
  3859. nextDTS,
  3860. lastPTS,
  3861. lastDTS,
  3862. inputSamples = track.samples,
  3863. outputSamples = [];
  3864. // PTS is coded on 33bits, and can loop from -2^32 to 2^32
  3865. // PTSNormalize will make PTS/DTS value monotonic, we use last known DTS value as reference value
  3866. var nextAvcDts = void 0;
  3867. if (contiguous) {
  3868. // if parsed fragment is contiguous with last one, let's use last DTS value as reference
  3869. nextAvcDts = this.nextAvcDts;
  3870. } else {
  3871. // if not contiguous, let's use target timeOffset
  3872. nextAvcDts = timeOffset * pesTimeScale;
  3873. }
  3874. // compute first DTS and last DTS, normalize them against reference value
  3875. var sample = inputSamples[0];
  3876. firstDTS = Math.max(
  3877. this._PTSNormalize(sample.dts, nextAvcDts) - this._initDTS,
  3878. 0
  3879. );
  3880. firstPTS = Math.max(
  3881. this._PTSNormalize(sample.pts, nextAvcDts) - this._initDTS,
  3882. 0
  3883. );
  3884. // check timestamp continuity accross consecutive fragments (this is to remove inter-fragment gap/hole)
  3885. var delta = Math.round((firstDTS - nextAvcDts) / 90);
  3886. // if fragment are contiguous, detect hole/overlapping between fragments
  3887. if (contiguous) {
  3888. if (delta) {
  3889. if (delta > 1) {
  3890. _logger.logger.log(
  3891. "AVC:" +
  3892. delta +
  3893. " ms hole between fragments detected,filling it"
  3894. );
  3895. } else if (delta < -1) {
  3896. _logger.logger.log(
  3897. "AVC:" +
  3898. -delta +
  3899. " ms overlapping between fragments detected"
  3900. );
  3901. }
  3902. // remove hole/gap : set DTS to next expected DTS
  3903. firstDTS = nextAvcDts;
  3904. inputSamples[0].dts = firstDTS + this._initDTS;
  3905. // offset PTS as well, ensure that PTS is smaller or equal than new DTS
  3906. firstPTS = Math.max(firstPTS - delta, nextAvcDts);
  3907. inputSamples[0].pts = firstPTS + this._initDTS;
  3908. _logger.logger.log(
  3909. "Video/PTS/DTS adjusted: " +
  3910. firstPTS +
  3911. "/" +
  3912. firstDTS +
  3913. ",delta:" +
  3914. delta
  3915. );
  3916. }
  3917. }
  3918. nextDTS = firstDTS;
  3919. // compute lastPTS/lastDTS
  3920. sample = inputSamples[inputSamples.length - 1];
  3921. lastDTS = Math.max(
  3922. this._PTSNormalize(sample.dts, nextAvcDts) - this._initDTS,
  3923. 0
  3924. );
  3925. lastPTS = Math.max(
  3926. this._PTSNormalize(sample.pts, nextAvcDts) - this._initDTS,
  3927. 0
  3928. );
  3929. lastPTS = Math.max(lastPTS, lastDTS);
  3930. var vendor = navigator.vendor,
  3931. userAgent = navigator.userAgent,
  3932. isSafari =
  3933. vendor &&
  3934. vendor.indexOf("Apple") > -1 &&
  3935. userAgent &&
  3936. !userAgent.match("CriOS");
  3937. // on Safari let's signal the same sample duration for all samples
  3938. // sample duration (as expected by trun MP4 boxes), should be the delta between sample DTS
  3939. // set this constant duration as being the avg delta between consecutive DTS.
  3940. if (isSafari) {
  3941. mp4SampleDuration = Math.round(
  3942. (lastDTS - firstDTS) /
  3943. (pes2mp4ScaleFactor * (inputSamples.length - 1))
  3944. );
  3945. }
  3946. // normalize all PTS/DTS now ...
  3947. for (var i = 0; i < inputSamples.length; i++) {
  3948. var _sample = inputSamples[i];
  3949. if (isSafari) {
  3950. // sample DTS is computed using a constant decoding offset (mp4SampleDuration) between samples
  3951. _sample.dts =
  3952. firstDTS + i * pes2mp4ScaleFactor * mp4SampleDuration;
  3953. } else {
  3954. // ensure sample monotonic DTS
  3955. _sample.dts = Math.max(
  3956. this._PTSNormalize(_sample.dts, nextAvcDts) -
  3957. this._initDTS,
  3958. firstDTS
  3959. );
  3960. // ensure dts is a multiple of scale factor to avoid rounding issues
  3961. _sample.dts =
  3962. Math.round(_sample.dts / pes2mp4ScaleFactor) *
  3963. pes2mp4ScaleFactor;
  3964. }
  3965. // we normalize PTS against nextAvcDts, we also substract initDTS (some streams don't start @ PTS O)
  3966. // and we ensure that computed value is greater or equal than sample DTS
  3967. _sample.pts = Math.max(
  3968. this._PTSNormalize(_sample.pts, nextAvcDts) -
  3969. this._initDTS,
  3970. _sample.dts
  3971. );
  3972. // ensure pts is a multiple of scale factor to avoid rounding issues
  3973. _sample.pts =
  3974. Math.round(_sample.pts / pes2mp4ScaleFactor) *
  3975. pes2mp4ScaleFactor;
  3976. }
  3977. /* concatenate the video data and construct the mdat in place
  3978. (need 8 more bytes to fill length and mpdat type) */
  3979. mdat = new Uint8Array(track.len + 4 * track.nbNalu + 8);
  3980. var view = new DataView(mdat.buffer);
  3981. view.setUint32(0, mdat.byteLength);
  3982. mdat.set(_mp4Generator2.default.types.mdat, 4);
  3983. for (var _i = 0; _i < inputSamples.length; _i++) {
  3984. var avcSample = inputSamples[_i],
  3985. mp4SampleLength = 0,
  3986. compositionTimeOffset = void 0;
  3987. // convert NALU bitstream to MP4 format (prepend NALU with size field)
  3988. while (avcSample.units.units.length) {
  3989. var unit = avcSample.units.units.shift();
  3990. view.setUint32(offset, unit.data.byteLength);
  3991. offset += 4;
  3992. mdat.set(unit.data, offset);
  3993. offset += unit.data.byteLength;
  3994. mp4SampleLength += 4 + unit.data.byteLength;
  3995. }
  3996. if (!isSafari) {
  3997. // expected sample duration is the Decoding Timestamp diff of consecutive samples
  3998. if (_i < inputSamples.length - 1) {
  3999. mp4SampleDuration =
  4000. inputSamples[_i + 1].dts - avcSample.dts;
  4001. } else {
  4002. var config = this.config,
  4003. lastFrameDuration =
  4004. avcSample.dts -
  4005. inputSamples[_i > 0 ? _i - 1 : _i].dts;
  4006. if (config.stretchShortVideoTrack) {
  4007. // In some cases, a segment's audio track duration may exceed the video track duration.
  4008. // Since we've already remuxed audio, and we know how long the audio track is, we look to
  4009. // see if the delta to the next segment is longer than the minimum of maxBufferHole and
  4010. // maxSeekHole. If so, playback would potentially get stuck, so we artificially inflate
  4011. // the duration of the last frame to minimize any potential gap between segments.
  4012. var maxBufferHole = config.maxBufferHole,
  4013. maxSeekHole = config.maxSeekHole,
  4014. gapTolerance = Math.floor(
  4015. Math.min(maxBufferHole, maxSeekHole) *
  4016. pesTimeScale
  4017. ),
  4018. deltaToFrameEnd =
  4019. (audioTrackLength
  4020. ? firstPTS + audioTrackLength * pesTimeScale
  4021. : this.nextAacPts) - avcSample.pts;
  4022. if (deltaToFrameEnd > gapTolerance) {
  4023. // We subtract lastFrameDuration from deltaToFrameEnd to try to prevent any video
  4024. // frame overlap. maxBufferHole/maxSeekHole should be >> lastFrameDuration anyway.
  4025. mp4SampleDuration =
  4026. deltaToFrameEnd - lastFrameDuration;
  4027. if (mp4SampleDuration < 0) {
  4028. mp4SampleDuration = lastFrameDuration;
  4029. }
  4030. _logger.logger.log(
  4031. "It is approximately " +
  4032. deltaToFrameEnd / 90 +
  4033. " ms to the next segment; using duration " +
  4034. mp4SampleDuration / 90 +
  4035. " ms for the last video frame."
  4036. );
  4037. } else {
  4038. mp4SampleDuration = lastFrameDuration;
  4039. }
  4040. } else {
  4041. mp4SampleDuration = lastFrameDuration;
  4042. }
  4043. }
  4044. mp4SampleDuration /= pes2mp4ScaleFactor;
  4045. compositionTimeOffset = Math.round(
  4046. (avcSample.pts - avcSample.dts) / pes2mp4ScaleFactor
  4047. );
  4048. } else {
  4049. compositionTimeOffset = Math.max(
  4050. 0,
  4051. mp4SampleDuration *
  4052. Math.round(
  4053. (avcSample.pts - avcSample.dts) /
  4054. (pes2mp4ScaleFactor * mp4SampleDuration)
  4055. )
  4056. );
  4057. }
  4058. outputSamples.push({
  4059. size: mp4SampleLength,
  4060. // constant duration
  4061. duration: mp4SampleDuration,
  4062. cts: compositionTimeOffset,
  4063. flags: {
  4064. isLeading: 0,
  4065. isDependedOn: 0,
  4066. hasRedundancy: 0,
  4067. degradPrio: 0,
  4068. dependsOn: avcSample.key ? 2 : 1,
  4069. isNonSync: avcSample.key ? 0 : 1
  4070. }
  4071. });
  4072. }
  4073. // next AVC sample DTS should be equal to last sample DTS + last sample duration (in PES timescale)
  4074. this.nextAvcDts =
  4075. lastDTS + mp4SampleDuration * pes2mp4ScaleFactor;
  4076. var dropped = track.dropped;
  4077. track.len = 0;
  4078. track.nbNalu = 0;
  4079. track.dropped = 0;
  4080. if (
  4081. outputSamples.length &&
  4082. navigator.userAgent.toLowerCase().indexOf("chrome") > -1
  4083. ) {
  4084. var flags = outputSamples[0].flags;
  4085. // chrome workaround, mark first sample as being a Random Access Point to avoid sourcebuffer append issue
  4086. // https://code.google.com/p/chromium/issues/detail?id=229412
  4087. flags.dependsOn = 2;
  4088. flags.isNonSync = 0;
  4089. }
  4090. track.samples = outputSamples;
  4091. moof = _mp4Generator2.default.moof(
  4092. track.sequenceNumber++,
  4093. firstDTS / pes2mp4ScaleFactor,
  4094. track
  4095. );
  4096. track.samples = [];
  4097. var data = {
  4098. id: this.id,
  4099. level: this.level,
  4100. sn: this.sn,
  4101. data1: moof,
  4102. data2: mdat,
  4103. startPTS: firstPTS / pesTimeScale,
  4104. endPTS:
  4105. (lastPTS + pes2mp4ScaleFactor * mp4SampleDuration) /
  4106. pesTimeScale,
  4107. startDTS: firstPTS / pesTimeScale,
  4108. endDTS:
  4109. (lastPTS + pes2mp4ScaleFactor * mp4SampleDuration) /
  4110. pesTimeScale,
  4111. // startDTS: firstDTS / pesTimeScale,
  4112. // endDTS: this.nextAvcDts / pesTimeScale,
  4113. type: "video",
  4114. nb: outputSamples.length,
  4115. dropped: dropped
  4116. };
  4117. this.observer.trigger(
  4118. _events2.default.FRAG_PARSING_DATA,
  4119. data
  4120. );
  4121. return data;
  4122. }
  4123. },
  4124. {
  4125. key: "remuxAudio",
  4126. value: function remuxAudio(track, timeOffset, contiguous) {
  4127. var pesTimeScale = this.PES_TIMESCALE,
  4128. mp4timeScale = track.timescale,
  4129. pes2mp4ScaleFactor = pesTimeScale / mp4timeScale,
  4130. expectedSampleDuration =
  4131. (track.timescale * 1024) / track.audiosamplerate;
  4132. var view,
  4133. offset = 8,
  4134. aacSample,
  4135. mp4Sample,
  4136. unit,
  4137. mdat,
  4138. moof,
  4139. firstPTS,
  4140. firstDTS,
  4141. lastDTS,
  4142. pts,
  4143. dts,
  4144. ptsnorm,
  4145. dtsnorm,
  4146. samples = [],
  4147. samples0 = [];
  4148. track.samples.sort(function (a, b) {
  4149. return a.pts - b.pts;
  4150. });
  4151. samples0 = track.samples;
  4152. var nextAacPts = contiguous
  4153. ? this.nextAacPts
  4154. : timeOffset * pesTimeScale;
  4155. // If the audio track is missing samples, the frames seem to get "left-shifted" within the
  4156. // resulting mp4 segment, causing sync issues and leaving gaps at the end of the audio segment.
  4157. // In an effort to prevent this from happening, we inject frames here where there are gaps.
  4158. // When possible, we inject a silent frame; when that's not possible, we duplicate the last
  4159. // frame.
  4160. var firstPtsNorm = this._PTSNormalize(
  4161. samples0[0].pts - this._initPTS,
  4162. nextAacPts
  4163. ),
  4164. pesFrameDuration =
  4165. expectedSampleDuration * pes2mp4ScaleFactor;
  4166. var nextPtsNorm = firstPtsNorm + pesFrameDuration;
  4167. for (var i = 1; i < samples0.length;) {
  4168. // First, let's see how far off this frame is from where we expect it to be
  4169. var sample = samples0[i],
  4170. ptsNorm = this._PTSNormalize(
  4171. sample.pts - this._initPTS,
  4172. nextAacPts
  4173. ),
  4174. delta = ptsNorm - nextPtsNorm;
  4175. // If we're overlapping by more than half a duration, drop this sample
  4176. if (delta < -0.5 * pesFrameDuration) {
  4177. _logger.logger.log(
  4178. "Dropping frame due to " +
  4179. Math.abs(delta / 90) +
  4180. " ms overlap."
  4181. );
  4182. samples0.splice(i, 1);
  4183. track.len -= sample.unit.length;
  4184. // Don't touch nextPtsNorm or i
  4185. }
  4186. // Otherwise, if we're more than half a frame away from where we should be, insert missing frames
  4187. else if (delta > 0.5 * pesFrameDuration) {
  4188. var missing = Math.round(delta / pesFrameDuration);
  4189. _logger.logger.log(
  4190. "Injecting " +
  4191. missing +
  4192. " frame" +
  4193. (missing > 1 ? "s" : "") +
  4194. " of missing audio due to " +
  4195. Math.round(delta / 90) +
  4196. " ms gap."
  4197. );
  4198. for (var j = 0; j < missing; j++) {
  4199. var newStamp = samples0[i - 1].pts + pesFrameDuration,
  4200. fillFrame = _aac2.default.getSilentFrame(
  4201. track.channelCount
  4202. );
  4203. if (!fillFrame) {
  4204. _logger.logger.log(
  4205. "Unable to get silent frame for given audio codec; duplicating last frame instead."
  4206. );
  4207. fillFrame = sample.unit.slice(0);
  4208. }
  4209. samples0.splice(i, 0, {
  4210. unit: fillFrame,
  4211. pts: newStamp,
  4212. dts: newStamp
  4213. });
  4214. track.len += fillFrame.length;
  4215. i += 1;
  4216. }
  4217. // Adjust sample to next expected pts
  4218. nextPtsNorm += (missing + 1) * pesFrameDuration;
  4219. sample.pts = samples0[i - 1].pts + pesFrameDuration;
  4220. i += 1;
  4221. }
  4222. // Otherwise, we're within half a frame duration, so just adjust pts
  4223. else {
  4224. if (Math.abs(delta) > 0.1 * pesFrameDuration) {
  4225. _logger.logger.log(
  4226. "Invalid frame delta " +
  4227. (ptsNorm - nextPtsNorm + pesFrameDuration) +
  4228. " at PTS " +
  4229. Math.round(ptsNorm / 90) +
  4230. " (should be " +
  4231. pesFrameDuration +
  4232. ")."
  4233. );
  4234. }
  4235. nextPtsNorm += pesFrameDuration;
  4236. sample.pts = samples0[i - 1].pts + pesFrameDuration;
  4237. i += 1;
  4238. }
  4239. }
  4240. while (samples0.length) {
  4241. aacSample = samples0.shift();
  4242. unit = aacSample.unit;
  4243. pts = aacSample.pts - this._initDTS;
  4244. dts = aacSample.dts - this._initDTS;
  4245. //logger.log(`Audio/PTS:${Math.round(pts/90)}`);
  4246. // if not first sample
  4247. if (lastDTS !== undefined) {
  4248. ptsnorm = this._PTSNormalize(pts, lastDTS);
  4249. dtsnorm = this._PTSNormalize(dts, lastDTS);
  4250. mp4Sample.duration =
  4251. (dtsnorm - lastDTS) / pes2mp4ScaleFactor;
  4252. } else {
  4253. ptsnorm = this._PTSNormalize(pts, nextAacPts);
  4254. dtsnorm = this._PTSNormalize(dts, nextAacPts);
  4255. var _delta = Math.round(
  4256. (1000 * (ptsnorm - nextAacPts)) / pesTimeScale
  4257. );
  4258. // if fragment are contiguous, detect hole/overlapping between fragments
  4259. if (contiguous) {
  4260. // log delta
  4261. if (_delta) {
  4262. if (_delta > 0) {
  4263. _logger.logger.log(
  4264. _delta +
  4265. " ms hole between AAC samples detected,filling it"
  4266. );
  4267. // if we have frame overlap, overlapping for more than half a frame duraion
  4268. } else if (_delta < -12) {
  4269. // drop overlapping audio frames... browser will deal with it
  4270. _logger.logger.log(
  4271. -_delta +
  4272. " ms overlapping between AAC samples detected, drop frame"
  4273. );
  4274. track.len -= unit.byteLength;
  4275. continue;
  4276. }
  4277. // set PTS/DTS to expected PTS/DTS
  4278. ptsnorm = dtsnorm = nextAacPts;
  4279. }
  4280. }
  4281. // remember first PTS of our aacSamples, ensure value is positive
  4282. firstPTS = Math.max(0, ptsnorm);
  4283. firstDTS = Math.max(0, dtsnorm);
  4284. if (track.len > 0) {
  4285. /* concatenate the audio data and construct the mdat in place
  4286. (need 8 more bytes to fill length and mdat type) */
  4287. mdat = new Uint8Array(track.len + 8);
  4288. view = new DataView(mdat.buffer);
  4289. view.setUint32(0, mdat.byteLength);
  4290. mdat.set(_mp4Generator2.default.types.mdat, 4);
  4291. } else {
  4292. // no audio samples
  4293. return;
  4294. }
  4295. }
  4296. mdat.set(unit, offset);
  4297. offset += unit.byteLength;
  4298. //console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${aacSample.pts}/${aacSample.dts}/${this._initDTS}/${ptsnorm}/${dtsnorm}/${(aacSample.pts/4294967296).toFixed(3)}');
  4299. mp4Sample = {
  4300. size: unit.byteLength,
  4301. cts: 0,
  4302. duration: 0,
  4303. flags: {
  4304. isLeading: 0,
  4305. isDependedOn: 0,
  4306. hasRedundancy: 0,
  4307. degradPrio: 0,
  4308. dependsOn: 1
  4309. }
  4310. };
  4311. samples.push(mp4Sample);
  4312. lastDTS = dtsnorm;
  4313. }
  4314. var lastSampleDuration = 0;
  4315. var nbSamples = samples.length;
  4316. //set last sample duration as being identical to previous sample
  4317. if (nbSamples >= 2) {
  4318. lastSampleDuration = samples[nbSamples - 2].duration;
  4319. mp4Sample.duration = lastSampleDuration;
  4320. }
  4321. if (nbSamples) {
  4322. // next aac sample PTS should be equal to last sample PTS + duration
  4323. this.nextAacPts =
  4324. ptsnorm + pes2mp4ScaleFactor * lastSampleDuration;
  4325. //logger.log('Audio/PTS/PTSend:' + aacSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0));
  4326. track.len = 0;
  4327. track.samples = samples;
  4328. moof = _mp4Generator2.default.moof(
  4329. track.sequenceNumber++,
  4330. firstDTS / pes2mp4ScaleFactor,
  4331. track
  4332. );
  4333. track.samples = [];
  4334. var audioData = {
  4335. id: this.id,
  4336. level: this.level,
  4337. sn: this.sn,
  4338. data1: moof,
  4339. data2: mdat,
  4340. startPTS: firstPTS / pesTimeScale,
  4341. endPTS: this.nextAacPts / pesTimeScale,
  4342. startDTS: firstDTS / pesTimeScale,
  4343. endDTS:
  4344. (dtsnorm + pes2mp4ScaleFactor * lastSampleDuration) /
  4345. pesTimeScale,
  4346. type: "audio",
  4347. nb: nbSamples
  4348. };
  4349. this.observer.trigger(
  4350. _events2.default.FRAG_PARSING_DATA,
  4351. audioData
  4352. );
  4353. return audioData;
  4354. }
  4355. return null;
  4356. }
  4357. },
  4358. {
  4359. key: "remuxEmptyAudio",
  4360. value: function remuxEmptyAudio(
  4361. track,
  4362. timeOffset,
  4363. contiguous,
  4364. videoData
  4365. ) {
  4366. var pesTimeScale = this.PES_TIMESCALE,
  4367. mp4timeScale = track.timescale
  4368. ? track.timescale
  4369. : track.audiosamplerate,
  4370. pes2mp4ScaleFactor = pesTimeScale / mp4timeScale,
  4371. // sync with video's timestamp
  4372. startDTS =
  4373. videoData.startDTS * pesTimeScale + this._initDTS,
  4374. endDTS = videoData.endDTS * pesTimeScale + this._initDTS,
  4375. // one sample's duration value
  4376. sampleDuration = 1024,
  4377. frameDuration = pes2mp4ScaleFactor * sampleDuration,
  4378. // samples count of this segment's duration
  4379. nbSamples = Math.ceil((endDTS - startDTS) / frameDuration),
  4380. // silent frame
  4381. silentFrame = _aac2.default.getSilentFrame(
  4382. track.channelCount
  4383. );
  4384. // Can't remux if we can't generate a silent frame...
  4385. if (!silentFrame) {
  4386. _logger.logger.trace(
  4387. "Unable to remuxEmptyAudio since we were unable to get a silent frame for given audio codec!"
  4388. );
  4389. return;
  4390. }
  4391. var samples = [];
  4392. for (var i = 0; i < nbSamples; i++) {
  4393. var stamp = startDTS + i * frameDuration;
  4394. samples.push({
  4395. unit: silentFrame.slice(0),
  4396. pts: stamp,
  4397. dts: stamp
  4398. });
  4399. track.len += silentFrame.length;
  4400. }
  4401. track.samples = samples;
  4402. this.remuxAudio(track, timeOffset, contiguous);
  4403. }
  4404. },
  4405. {
  4406. key: "remuxID3",
  4407. value: function remuxID3(track, timeOffset) {
  4408. var length = track.samples.length,
  4409. sample;
  4410. // consume samples
  4411. if (length) {
  4412. for (var index = 0; index < length; index++) {
  4413. sample = track.samples[index];
  4414. // setting id3 pts, dts to relative time
  4415. // using this._initPTS and this._initDTS to calculate relative time
  4416. sample.pts =
  4417. (sample.pts - this._initPTS) / this.PES_TIMESCALE;
  4418. sample.dts =
  4419. (sample.dts - this._initDTS) / this.PES_TIMESCALE;
  4420. }
  4421. this.observer.trigger(
  4422. _events2.default.FRAG_PARSING_METADATA,
  4423. {
  4424. id: this.id,
  4425. level: this.level,
  4426. sn: this.sn,
  4427. samples: track.samples
  4428. }
  4429. );
  4430. }
  4431. track.samples = [];
  4432. timeOffset = timeOffset;
  4433. }
  4434. },
  4435. {
  4436. key: "remuxText",
  4437. value: function remuxText(track, timeOffset) {
  4438. track.samples.sort(function (a, b) {
  4439. return a.pts - b.pts;
  4440. });
  4441. var length = track.samples.length,
  4442. sample;
  4443. // consume samples
  4444. if (length) {
  4445. for (var index = 0; index < length; index++) {
  4446. sample = track.samples[index];
  4447. // setting text pts, dts to relative time
  4448. // using this._initPTS and this._initDTS to calculate relative time
  4449. sample.pts =
  4450. (sample.pts - this._initPTS) / this.PES_TIMESCALE;
  4451. }
  4452. this.observer.trigger(
  4453. _events2.default.FRAG_PARSING_USERDATA,
  4454. {
  4455. id: this.id,
  4456. level: this.level,
  4457. sn: this.sn,
  4458. samples: track.samples
  4459. }
  4460. );
  4461. }
  4462. track.samples = [];
  4463. timeOffset = timeOffset;
  4464. }
  4465. },
  4466. {
  4467. key: "_PTSNormalize",
  4468. value: function _PTSNormalize(value, reference) {
  4469. var offset;
  4470. if (reference === undefined) {
  4471. return value;
  4472. }
  4473. if (reference < value) {
  4474. // - 2^33
  4475. offset = -8589934592;
  4476. } else {
  4477. // + 2^33
  4478. offset = 8589934592;
  4479. }
  4480. /* PTS is 33bit (from 0 to 2^33 -1)
  4481. if diff between value and reference is bigger than half of the amplitude (2^32) then it means that
  4482. PTS looping occured. fill the gap */
  4483. while (Math.abs(value - reference) > 4294967296) {
  4484. value += offset;
  4485. }
  4486. return value;
  4487. }
  4488. },
  4489. {
  4490. key: "passthrough",
  4491. get: function get() {
  4492. return false;
  4493. }
  4494. }
  4495. ]);
  4496. return MP4Remuxer;
  4497. })();
  4498. exports.default = MP4Remuxer;
  4499. },
  4500. {
  4501. "../errors": 6,
  4502. "../events": 8,
  4503. "../helper/aac": 9,
  4504. "../remux/mp4-generator": 12,
  4505. "../utils/logger": 15,
  4506. "../utils/polyfill": 16
  4507. }
  4508. ],
  4509. 14: [
  4510. function (require, module, exports) {
  4511. "use strict";
  4512. Object.defineProperty(exports, "__esModule", {
  4513. value: true
  4514. });
  4515. var _createClass = (function () {
  4516. function defineProperties(target, props) {
  4517. for (var i = 0; i < props.length; i++) {
  4518. var descriptor = props[i];
  4519. descriptor.enumerable = descriptor.enumerable || false;
  4520. descriptor.configurable = true;
  4521. if ("value" in descriptor) descriptor.writable = true;
  4522. Object.defineProperty(target, descriptor.key, descriptor);
  4523. }
  4524. }
  4525. return function (Constructor, protoProps, staticProps) {
  4526. if (protoProps)
  4527. defineProperties(Constructor.prototype, protoProps);
  4528. if (staticProps) defineProperties(Constructor, staticProps);
  4529. return Constructor;
  4530. };
  4531. })();
  4532. var _events = require("../events");
  4533. var _events2 = _interopRequireDefault(_events);
  4534. var _eventHandler = require("../event-handler");
  4535. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  4536. var _h264Demuxer = require("../demux/h264-demuxer");
  4537. var _h264Demuxer2 = _interopRequireDefault(_h264Demuxer);
  4538. function _interopRequireDefault(obj) {
  4539. return obj && obj.__esModule ? obj : { default: obj };
  4540. }
  4541. function _classCallCheck(instance, Constructor) {
  4542. if (!(instance instanceof Constructor)) {
  4543. throw new TypeError("Cannot call a class as a function");
  4544. }
  4545. }
  4546. function _possibleConstructorReturn(self, call) {
  4547. if (!self) {
  4548. throw new ReferenceError(
  4549. "this hasn't been initialised - super() hasn't been called"
  4550. );
  4551. }
  4552. return call &&
  4553. (typeof call === "object" || typeof call === "function")
  4554. ? call
  4555. : self;
  4556. }
  4557. function _inherits(subClass, superClass) {
  4558. if (typeof superClass !== "function" && superClass !== null) {
  4559. throw new TypeError(
  4560. "Super expression must either be null or a function, not " +
  4561. typeof superClass
  4562. );
  4563. }
  4564. subClass.prototype = Object.create(
  4565. superClass && superClass.prototype,
  4566. {
  4567. constructor: {
  4568. value: subClass,
  4569. enumerable: false,
  4570. writable: true,
  4571. configurable: true
  4572. }
  4573. }
  4574. );
  4575. if (superClass)
  4576. Object.setPrototypeOf
  4577. ? Object.setPrototypeOf(subClass, superClass)
  4578. : (subClass.__proto__ = superClass);
  4579. }
  4580. /*
  4581. * H264 NAL Slicer
  4582. */
  4583. var SlicesReader = (function (_EventHandler) {
  4584. _inherits(SlicesReader, _EventHandler);
  4585. function SlicesReader(wfs) {
  4586. var config =
  4587. arguments.length > 1 && arguments[1] !== undefined
  4588. ? arguments[1]
  4589. : null;
  4590. _classCallCheck(this, SlicesReader);
  4591. var _this = _possibleConstructorReturn(
  4592. this,
  4593. (
  4594. SlicesReader.__proto__ || Object.getPrototypeOf(SlicesReader)
  4595. ).call(this, wfs, _events2.default.H264_DATA_PARSING)
  4596. );
  4597. _this.config = _this.wfs.config || config;
  4598. _this.h264Demuxer = new _h264Demuxer2.default(wfs);
  4599. _this.wfs = wfs;
  4600. _this.lastBuf = null;
  4601. _this.nals = [];
  4602. return _this;
  4603. }
  4604. _createClass(SlicesReader, [
  4605. {
  4606. key: "destroy",
  4607. value: function destroy() {
  4608. this.lastBuf = null;
  4609. this.nals = [];
  4610. _eventHandler2.default.prototype.destroy.call(this);
  4611. }
  4612. },
  4613. {
  4614. key: "_read",
  4615. value: function _read(buffer) {
  4616. var typedAr = null;
  4617. this.nals = [];
  4618. if (!buffer || buffer.byteLength < 1) return;
  4619. if (this.lastBuf) {
  4620. typedAr = new Uint8Array(
  4621. buffer.byteLength + this.lastBuf.length
  4622. );
  4623. typedAr.set(this.lastBuf);
  4624. typedAr.set(new Uint8Array(buffer), this.lastBuf.length);
  4625. } else {
  4626. typedAr = new Uint8Array(buffer);
  4627. }
  4628. var lastNalEndPos = 0;
  4629. var b1 = -1; // byte before one
  4630. var b2 = -2; // byte before two
  4631. var nalStartPos = new Array();
  4632. for (var i = 0; i < typedAr.length; i += 2) {
  4633. var b_0 = typedAr[i];
  4634. var b_1 = typedAr[i + 1];
  4635. if (b1 == 0 && b_0 == 0 && b_1 == 0) {
  4636. nalStartPos.push(i - 1);
  4637. } else if (b_1 == 1 && b_0 == 0 && b1 == 0 && b2 == 0) {
  4638. nalStartPos.push(i - 2);
  4639. }
  4640. b2 = b_0;
  4641. b1 = b_1;
  4642. }
  4643. if (nalStartPos.length > 1) {
  4644. for (var i = 0; i < nalStartPos.length - 1; ++i) {
  4645. this.nals.push(
  4646. typedAr.subarray(nalStartPos[i], nalStartPos[i + 1] + 1)
  4647. );
  4648. lastNalEndPos = nalStartPos[i + 1];
  4649. }
  4650. } else {
  4651. lastNalEndPos = nalStartPos[0];
  4652. }
  4653. if (lastNalEndPos != 0 && lastNalEndPos < typedAr.length) {
  4654. this.lastBuf = typedAr.subarray(lastNalEndPos);
  4655. } else {
  4656. if (!!!this.lastBuf) {
  4657. this.lastBuf = typedAr;
  4658. }
  4659. var _newBuf = new Uint8Array(
  4660. this.lastBuf.length + buffer.byteLength
  4661. );
  4662. _newBuf.set(this.lastBuf);
  4663. _newBuf.set(new Uint8Array(buffer), this.lastBuf.length);
  4664. this.lastBuf = _newBuf;
  4665. }
  4666. }
  4667. },
  4668. {
  4669. key: "onH264DataParsing",
  4670. value: function onH264DataParsing(event) {
  4671. this._read(event.data);
  4672. var $this = this;
  4673. this.nals.forEach(function (nal) {
  4674. $this.wfs.trigger(_events2.default.H264_DATA_PARSED, {
  4675. data: nal
  4676. });
  4677. });
  4678. }
  4679. }
  4680. ]);
  4681. return SlicesReader;
  4682. })(_eventHandler2.default);
  4683. exports.default = SlicesReader;
  4684. },
  4685. { "../demux/h264-demuxer": 5, "../event-handler": 7, "../events": 8 }
  4686. ],
  4687. 15: [
  4688. function (require, module, exports) {
  4689. "use strict";
  4690. Object.defineProperty(exports, "__esModule", {
  4691. value: true
  4692. });
  4693. var _typeof =
  4694. typeof Symbol === "function" && typeof Symbol.iterator === "symbol"
  4695. ? function (obj) {
  4696. return typeof obj;
  4697. }
  4698. : function (obj) {
  4699. return obj &&
  4700. typeof Symbol === "function" &&
  4701. obj.constructor === Symbol &&
  4702. obj !== Symbol.prototype
  4703. ? "symbol"
  4704. : typeof obj;
  4705. };
  4706. function noop() { }
  4707. var fakeLogger = {
  4708. trace: noop,
  4709. debug: noop,
  4710. log: noop,
  4711. warn: noop,
  4712. info: noop,
  4713. error: noop
  4714. };
  4715. var exportedLogger = fakeLogger;
  4716. //let lastCallTime;
  4717. // function formatMsgWithTimeInfo(type, msg) {
  4718. // const now = Date.now();
  4719. // const diff = lastCallTime ? '+' + (now - lastCallTime) : '0';
  4720. // lastCallTime = now;
  4721. // msg = (new Date(now)).toISOString() + ' | [' + type + '] > ' + msg + ' ( ' + diff + ' ms )';
  4722. // return msg;
  4723. // }
  4724. function formatMsg(type, msg) {
  4725. msg = "[" + type + "] > " + msg;
  4726. return msg;
  4727. }
  4728. function consolePrintFn(type) {
  4729. var func = window.console[type];
  4730. if (func) {
  4731. return function () {
  4732. for (
  4733. var _len = arguments.length, args = Array(_len), _key = 0;
  4734. _key < _len;
  4735. _key++
  4736. ) {
  4737. args[_key] = arguments[_key];
  4738. }
  4739. if (args[0]) {
  4740. args[0] = formatMsg(type, args[0]);
  4741. }
  4742. func.apply(window.console, args);
  4743. };
  4744. }
  4745. return noop;
  4746. }
  4747. function exportLoggerFunctions(debugConfig) {
  4748. for (
  4749. var _len2 = arguments.length,
  4750. functions = Array(_len2 > 1 ? _len2 - 1 : 0),
  4751. _key2 = 1;
  4752. _key2 < _len2;
  4753. _key2++
  4754. ) {
  4755. functions[_key2 - 1] = arguments[_key2];
  4756. }
  4757. functions.forEach(function (type) {
  4758. exportedLogger[type] = debugConfig[type]
  4759. ? debugConfig[type].bind(debugConfig)
  4760. : consolePrintFn(type);
  4761. });
  4762. }
  4763. var enableLogs = (exports.enableLogs = function enableLogs(
  4764. debugConfig
  4765. ) {
  4766. if (
  4767. debugConfig === true ||
  4768. (typeof debugConfig === "undefined"
  4769. ? "undefined"
  4770. : _typeof(debugConfig)) === "object"
  4771. ) {
  4772. exportLoggerFunctions(
  4773. debugConfig,
  4774. // Remove out from list here to hard-disable a log-level
  4775. //'trace',
  4776. "debug",
  4777. "log",
  4778. "info",
  4779. "warn",
  4780. "error"
  4781. );
  4782. // Some browsers don't allow to use bind on console object anyway
  4783. // fallback to default if needed
  4784. try {
  4785. exportedLogger.log();
  4786. } catch (e) {
  4787. exportedLogger = fakeLogger;
  4788. }
  4789. } else {
  4790. exportedLogger = fakeLogger;
  4791. }
  4792. });
  4793. var logger = (exports.logger = exportedLogger);
  4794. },
  4795. {}
  4796. ],
  4797. 16: [
  4798. function (require, module, exports) {
  4799. "use strict";
  4800. if (
  4801. typeof ArrayBuffer !== "undefined" &&
  4802. !ArrayBuffer.prototype.slice
  4803. ) {
  4804. ArrayBuffer.prototype.slice = function (start, end) {
  4805. var that = new Uint8Array(this);
  4806. if (end === undefined) {
  4807. end = that.length;
  4808. }
  4809. var result = new ArrayBuffer(end - start);
  4810. var resultArray = new Uint8Array(result);
  4811. for (var i = 0; i < resultArray.length; i++) {
  4812. resultArray[i] = that[i + start];
  4813. }
  4814. return result;
  4815. };
  4816. }
  4817. },
  4818. {}
  4819. ],
  4820. 17: [
  4821. function (require, module, exports) {
  4822. "use strict";
  4823. Object.defineProperty(exports, "__esModule", {
  4824. value: true
  4825. });
  4826. var _createClass = (function () {
  4827. function defineProperties(target, props) {
  4828. for (var i = 0; i < props.length; i++) {
  4829. var descriptor = props[i];
  4830. descriptor.enumerable = descriptor.enumerable || false;
  4831. descriptor.configurable = true;
  4832. if ("value" in descriptor) descriptor.writable = true;
  4833. Object.defineProperty(target, descriptor.key, descriptor);
  4834. }
  4835. }
  4836. return function (Constructor, protoProps, staticProps) {
  4837. if (protoProps)
  4838. defineProperties(Constructor.prototype, protoProps);
  4839. if (staticProps) defineProperties(Constructor, staticProps);
  4840. return Constructor;
  4841. };
  4842. })();
  4843. function _classCallCheck(instance, Constructor) {
  4844. if (!(instance instanceof Constructor)) {
  4845. throw new TypeError("Cannot call a class as a function");
  4846. }
  4847. }
  4848. /**
  4849. * XHR based logger
  4850. */
  4851. var XhrLoader = (function () {
  4852. function XhrLoader(config) {
  4853. _classCallCheck(this, XhrLoader);
  4854. if (config && config.xhrSetup) {
  4855. this.xhrSetup = config.xhrSetup;
  4856. }
  4857. }
  4858. _createClass(XhrLoader, [
  4859. {
  4860. key: "destroy",
  4861. value: function destroy() {
  4862. this.abort();
  4863. this.loader = null;
  4864. }
  4865. },
  4866. {
  4867. key: "abort",
  4868. value: function abort() {
  4869. var loader = this.loader;
  4870. if (loader && loader.readyState !== 4) {
  4871. this.stats.aborted = true;
  4872. loader.abort();
  4873. }
  4874. window.clearTimeout(this.requestTimeout);
  4875. this.requestTimeout = null;
  4876. window.clearTimeout(this.retryTimeout);
  4877. this.retryTimeout = null;
  4878. }
  4879. },
  4880. {
  4881. key: "loadHead",
  4882. value: function loadHead(context, config, callbacks) {
  4883. this.context = context;
  4884. this.config = config;
  4885. this.callbacks = callbacks;
  4886. this.stats = { trequest: performance.now(), retry: 0 };
  4887. this.retryDelay = config.retryDelay;
  4888. var xhr = new XMLHttpRequest();
  4889. xhr.open("head", context.url);
  4890. xhr.onload = function () {
  4891. callbacks.onSuccess(
  4892. xhr.getResponseHeader("content-length")
  4893. );
  4894. };
  4895. xhr.send();
  4896. }
  4897. },
  4898. {
  4899. key: "load",
  4900. value: function load(context, config, callbacks) {
  4901. this.context = context;
  4902. this.config = config;
  4903. this.callbacks = callbacks;
  4904. this.stats = { trequest: performance.now(), retry: 0 };
  4905. this.retryDelay = config.retryDelay;
  4906. this.loadInternal();
  4907. }
  4908. },
  4909. {
  4910. key: "loadInternal",
  4911. value: function loadInternal() {
  4912. var xhr,
  4913. context = this.context;
  4914. if (typeof XDomainRequest !== "undefined") {
  4915. xhr = this.loader = new XDomainRequest();
  4916. } else {
  4917. xhr = this.loader = new XMLHttpRequest();
  4918. }
  4919. xhr.onloadend = this.loadend.bind(this);
  4920. xhr.onprogress = this.loadprogress.bind(this);
  4921. xhr.open("GET", context.url, true);
  4922. if (context.rangeEnd) {
  4923. xhr.setRequestHeader(
  4924. "Range",
  4925. "bytes=" +
  4926. context.rangeStart +
  4927. "-" +
  4928. (context.rangeEnd - 1)
  4929. );
  4930. }
  4931. xhr.responseType = context.responseType;
  4932. var stats = this.stats;
  4933. stats.tfirst = 0;
  4934. stats.loaded = 0;
  4935. if (this.xhrSetup) {
  4936. this.xhrSetup(xhr, context.url);
  4937. }
  4938. // setup timeout before we perform request
  4939. this.requestTimeout = window.setTimeout(
  4940. this.loadtimeout.bind(this),
  4941. this.config.timeout
  4942. );
  4943. xhr.send();
  4944. }
  4945. },
  4946. {
  4947. key: "loadend",
  4948. value: function loadend(event) {
  4949. var xhr = event.currentTarget,
  4950. status = xhr.status,
  4951. stats = this.stats,
  4952. context = this.context,
  4953. config = this.config;
  4954. // don't proceed if xhr has been aborted
  4955. if (stats.aborted) {
  4956. return;
  4957. }
  4958. // in any case clear the current xhrs timeout
  4959. window.clearTimeout(this.requestTimeout);
  4960. // http status between 200 to 299 are all successful
  4961. if (status >= 200 && status < 300) {
  4962. stats.tload = Math.max(stats.tfirst, performance.now());
  4963. var data = void 0,
  4964. len = void 0;
  4965. if (context.responseType === "arraybuffer") {
  4966. data = xhr.response;
  4967. len = data.byteLength;
  4968. } else {
  4969. data = xhr.responseText;
  4970. len = data.length;
  4971. }
  4972. stats.loaded = stats.total = len;
  4973. var response = { url: xhr.responseURL, data: data };
  4974. this.callbacks.onSuccess(response, stats, context);
  4975. } else {
  4976. // if max nb of retries reached or if http status between 400 and 499 (such error cannot be recovered, retrying is useless), return error
  4977. if (
  4978. stats.retry >= config.maxRetry ||
  4979. (status >= 400 && status < 499)
  4980. ) {
  4981. // logger.error(`${status} while loading ${context.url}` );
  4982. this.callbacks.onError(
  4983. { code: status, text: xhr.statusText },
  4984. context
  4985. );
  4986. } else {
  4987. // retry
  4988. // logger.warn(`${status} while loading ${context.url}, retrying in ${this.retryDelay}...`);
  4989. // aborts and resets internal state
  4990. this.destroy();
  4991. // schedule retry
  4992. this.retryTimeout = window.setTimeout(
  4993. this.loadInternal.bind(this),
  4994. this.retryDelay
  4995. );
  4996. // set exponential backoff
  4997. this.retryDelay = Math.min(
  4998. 2 * this.retryDelay,
  4999. config.maxRetryDelay
  5000. );
  5001. stats.retry++;
  5002. }
  5003. }
  5004. }
  5005. },
  5006. {
  5007. key: "loadtimeout",
  5008. value: function loadtimeout() {
  5009. // logger.warn(`timeout while loading ${this.context.url}` );
  5010. this.callbacks.onTimeout(this.stats, this.context);
  5011. }
  5012. },
  5013. {
  5014. key: "loadprogress",
  5015. value: function loadprogress(event) {
  5016. var stats = this.stats;
  5017. if (stats.tfirst === 0) {
  5018. stats.tfirst = Math.max(performance.now(), stats.trequest);
  5019. }
  5020. stats.loaded = event.loaded;
  5021. if (event.lengthComputable) {
  5022. stats.total = event.total;
  5023. }
  5024. var onProgress = this.callbacks.onProgress;
  5025. if (onProgress) {
  5026. // last args is to provide on progress data
  5027. onProgress(stats, this.context, null);
  5028. }
  5029. }
  5030. }
  5031. ]);
  5032. return XhrLoader;
  5033. })();
  5034. exports.default = XhrLoader;
  5035. },
  5036. {}
  5037. ],
  5038. 18: [
  5039. function (require, module, exports) {
  5040. /**
  5041. * WFS interface, Jeff Yang 2016.10
  5042. */
  5043. "use strict";
  5044. Object.defineProperty(exports, "__esModule", {
  5045. value: true
  5046. });
  5047. var _createClass = (function () {
  5048. function defineProperties(target, props) {
  5049. for (var i = 0; i < props.length; i++) {
  5050. var descriptor = props[i];
  5051. descriptor.enumerable = descriptor.enumerable || false;
  5052. descriptor.configurable = true;
  5053. if ("value" in descriptor) descriptor.writable = true;
  5054. Object.defineProperty(target, descriptor.key, descriptor);
  5055. }
  5056. }
  5057. return function (Constructor, protoProps, staticProps) {
  5058. if (protoProps)
  5059. defineProperties(Constructor.prototype, protoProps);
  5060. if (staticProps) defineProperties(Constructor, staticProps);
  5061. return Constructor;
  5062. };
  5063. })();
  5064. var _events = require("./events");
  5065. var _events2 = _interopRequireDefault(_events);
  5066. var _flowController = require("./controller/flow-controller");
  5067. var _flowController2 = _interopRequireDefault(_flowController);
  5068. var _bufferController = require("./controller/buffer-controller");
  5069. var _bufferController2 = _interopRequireDefault(_bufferController);
  5070. var _events3 = require("events");
  5071. var _events4 = _interopRequireDefault(_events3);
  5072. var _xhrLoader = require("./utils/xhr-loader");
  5073. var _xhrLoader2 = _interopRequireDefault(_xhrLoader);
  5074. var _websocketLoader = require("./loader/websocket-loader");
  5075. var _websocketLoader2 = _interopRequireDefault(_websocketLoader);
  5076. function _interopRequireDefault(obj) {
  5077. return obj && obj.__esModule ? obj : { default: obj };
  5078. }
  5079. function _classCallCheck(instance, Constructor) {
  5080. if (!(instance instanceof Constructor)) {
  5081. throw new TypeError("Cannot call a class as a function");
  5082. }
  5083. }
  5084. var Wfs = (function () {
  5085. _createClass(Wfs, null, [
  5086. {
  5087. key: "isSupported",
  5088. value: function isSupported() {
  5089. return (
  5090. window.MediaSource &&
  5091. typeof window.MediaSource.isTypeSupported === "function" &&
  5092. window.MediaSource.isTypeSupported(
  5093. 'video/mp4; codecs="avc1.42c01f,mp4a.40.2"'
  5094. )
  5095. );
  5096. }
  5097. },
  5098. {
  5099. key: "version",
  5100. get: function get() {
  5101. // replaced with browserify-versionify transform
  5102. return "" + "v.0.0.0.1";
  5103. }
  5104. },
  5105. {
  5106. key: "Events",
  5107. get: function get() {
  5108. return _events2.default;
  5109. }
  5110. },
  5111. {
  5112. key: "DefaultConfig",
  5113. get: function get() {
  5114. if (!Wfs.defaultConfig) {
  5115. Wfs.defaultConfig = {
  5116. autoStartLoad: true,
  5117. startPosition: -1,
  5118. debug: false,
  5119. fLoader: undefined,
  5120. loader: _xhrLoader2.default,
  5121. //loader: FetchLoader,
  5122. fmp4FileUrl: "xxxx.mp4",
  5123. fragLoadingTimeOut: 20000,
  5124. fragLoadingMaxRetry: 6,
  5125. fragLoadingRetryDelay: 1000,
  5126. fragLoadingMaxRetryTimeout: 64000,
  5127. fragLoadingLoopThreshold: 3,
  5128. forceKeyFrameOnDiscontinuity: true,
  5129. appendErrorMaxRetry: 3
  5130. };
  5131. }
  5132. return Wfs.defaultConfig;
  5133. },
  5134. set: function set(defaultConfig) {
  5135. Wfs.defaultConfig = defaultConfig;
  5136. }
  5137. }
  5138. ]);
  5139. function Wfs() {
  5140. var config =
  5141. arguments.length > 0 && arguments[0] !== undefined
  5142. ? arguments[0]
  5143. : {};
  5144. _classCallCheck(this, Wfs);
  5145. var defaultConfig = Wfs.DefaultConfig;
  5146. for (var prop in defaultConfig) {
  5147. if (prop in config) {
  5148. continue;
  5149. }
  5150. config[prop] = defaultConfig[prop];
  5151. }
  5152. this.config = config;
  5153. // observer setup
  5154. var observer = (this.observer = new _events4.default());
  5155. observer.trigger = function trigger(event) {
  5156. for (
  5157. var _len = arguments.length,
  5158. data = Array(_len > 1 ? _len - 1 : 0),
  5159. _key = 1;
  5160. _key < _len;
  5161. _key++
  5162. ) {
  5163. data[_key - 1] = arguments[_key];
  5164. }
  5165. observer.emit.apply(observer, [event, event].concat(data));
  5166. };
  5167. observer.off = function off(event) {
  5168. for (
  5169. var _len2 = arguments.length,
  5170. data = Array(_len2 > 1 ? _len2 - 1 : 0),
  5171. _key2 = 1;
  5172. _key2 < _len2;
  5173. _key2++
  5174. ) {
  5175. data[_key2 - 1] = arguments[_key2];
  5176. }
  5177. observer.removeListener.apply(observer, [event].concat(data));
  5178. };
  5179. this.on = observer.on.bind(observer);
  5180. this.off = observer.off.bind(observer);
  5181. this.trigger = observer.trigger.bind(observer);
  5182. this.flowController = new _flowController2.default(this);
  5183. this.bufferController = new _bufferController2.default(this);
  5184. // this.fileLoader = new FileLoader(this);
  5185. this.websocketLoader = new _websocketLoader2.default(this);
  5186. this.mediaType = undefined;
  5187. }
  5188. _createClass(Wfs, [
  5189. {
  5190. key: "destroy",
  5191. value: function destroy() {
  5192. this.flowController.destroy();
  5193. this.bufferController.destroy();
  5194. // this.fileLoader.destroy();
  5195. this.websocketLoader.destroy();
  5196. }
  5197. },
  5198. {
  5199. key: "attachMedia",
  5200. value: function attachMedia(media) {
  5201. var config =
  5202. arguments.length > 1 && arguments[1] !== undefined
  5203. ? arguments[1]
  5204. : undefined;
  5205. var mediaType = "H264Raw";
  5206. var url = undefined;
  5207. if (config !== undefined) {
  5208. // 'H264Raw' 'FMp4'
  5209. if (config.type !== undefined) {
  5210. mediaType = config.type;
  5211. }
  5212. if (config.url !== undefined) {
  5213. url = config.url;
  5214. }
  5215. }
  5216. if (url === undefined) {
  5217. throw new Error("URL is NULL!!!");
  5218. }
  5219. this.url = url;
  5220. this.mediaType = mediaType;
  5221. this.media = media;
  5222. this.trigger(_events2.default.MEDIA_ATTACHING, {
  5223. media: media,
  5224. mediaType: mediaType,
  5225. url: url
  5226. });
  5227. }
  5228. },
  5229. {
  5230. key: "attachWebsocket",
  5231. value: function attachWebsocket(websocket) {
  5232. this.trigger(_events2.default.WEBSOCKET_ATTACHING, {
  5233. websocket: websocket,
  5234. mediaType: this.mediaType,
  5235. url: this.url
  5236. });
  5237. }
  5238. }
  5239. ]);
  5240. return Wfs;
  5241. })();
  5242. exports.default = Wfs;
  5243. },
  5244. {
  5245. "./controller/buffer-controller": 2,
  5246. "./controller/flow-controller": 3,
  5247. "./events": 8,
  5248. "./loader/websocket-loader": 11,
  5249. "./utils/xhr-loader": 17,
  5250. events: 1
  5251. }
  5252. ]
  5253. },
  5254. {},
  5255. [10]
  5256. )(10);
  5257. });
  5258. //# sourceMappingURL=wfs.js.map