szxcode.js 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664
  1. //应模块合适安卓手机,因解码流快
  2. //了解当前模块请搜索关键字 》》》TDDE《《《 了解注意注释
  3. var client
  4. (function(f) {
  5. if(typeof exports === "object" && typeof module !== "undefined") {
  6. module.exports = f()
  7. } else if(typeof define === "function" && define.amd) {
  8. define([], f)
  9. } else {
  10. var g;
  11. if(typeof window !== "undefined") {
  12. g = window
  13. } else if(typeof global !== "undefined") {
  14. g = global
  15. } else if(typeof self !== "undefined") {
  16. g = self
  17. } else {
  18. g = this
  19. }
  20. g.Wfs = f()
  21. }
  22. })(function() {
  23. var define, module, exports;
  24. return(function e(t, n, r) {
  25. function s(o, u) {
  26. if(!n[o]) {
  27. if(!t[o]) {
  28. var a = typeof require == "function" && require;
  29. if(!u && a) return a(o, !0);
  30. if(i) return i(o, !0);
  31. var f = new Error("Cannot find module '" + o + "'");
  32. throw f.code = "MODULE_NOT_FOUND", f
  33. }
  34. var l = n[o] = {
  35. exports: {}
  36. };
  37. t[o][0].call(l.exports, function(e) {
  38. var n = t[o][1][e];
  39. return s(n ? n : e)
  40. }, l, l.exports, e, t, n, r)
  41. }
  42. return n[o].exports
  43. }
  44. var i = typeof require == "function" && require;
  45. for(var o = 0; o < r.length; o++) s(r[o]);
  46. return s
  47. })({
  48. 1: [function(require, module, exports) {
  49. function EventEmitter() {
  50. this._events = this._events || {};
  51. this._maxListeners = this._maxListeners || undefined;
  52. }
  53. module.exports = EventEmitter;
  54. // 向后兼容节点0.10.x
  55. EventEmitter.EventEmitter = EventEmitter;
  56. EventEmitter.prototype._events = undefined;
  57. EventEmitter.prototype._maxListeners = undefined;
  58. // 默认情况下,如果超过10个侦听器 TDDE
  59. // 添加到其中。这是一个有用的默认值,有助于查找内存泄漏。
  60. EventEmitter.defaultMaxListeners = 10;
  61. // 显然不是所有的发射器都应该限制在10个。此功能允许
  62. // 要增加的。设为零表示无限制。
  63. EventEmitter.prototype.setMaxListeners = function(n) {
  64. if(!isNumber(n) || n < 0 || isNaN(n))
  65. throw TypeError('n must be a positive number');
  66. this._maxListeners = n;
  67. return this;
  68. };
  69. EventEmitter.prototype.emit = function(type) {
  70. var er, handler, len, args, i, listeners;
  71. if(!this._events)
  72. this._events = {};
  73. // 如果没有“错误”事件侦听器,则抛出。
  74. if(type === 'error') {
  75. if(!this._events.error ||
  76. (isObject(this._events.error) && !this._events.error.length)) {
  77. er = arguments[1];
  78. if(er instanceof Error) {
  79. throw er; // Unhandled 'error' event
  80. } else {
  81. // At least give some kind of context to the user
  82. var err = new Error('Uncaught, unspecified "error" event. (' + er + ')');
  83. err.context = er;
  84. throw err;
  85. }
  86. }
  87. }
  88. handler = this._events[type];
  89. if(isUndefined(handler))
  90. return false;
  91. if(isFunction(handler)) {
  92. switch(arguments.length) {
  93. // 快速案件
  94. case 1:
  95. handler.call(this);
  96. break;
  97. case 2:
  98. handler.call(this, arguments[1]);
  99. break;
  100. case 3:
  101. handler.call(this, arguments[1], arguments[2]);
  102. break;
  103. // slower
  104. default:
  105. args = Array.prototype.slice.call(arguments, 1);
  106. handler.apply(this, args);
  107. }
  108. } else if(isObject(handler)) {
  109. args = Array.prototype.slice.call(arguments, 1);
  110. listeners = handler.slice();
  111. len = listeners.length;
  112. for(i = 0; i < len; i++)
  113. listeners[i].apply(this, args);
  114. }
  115. return true;
  116. };
  117. EventEmitter.prototype.addListener = function(type, listener) {
  118. var m;
  119. if(!isFunction(listener))
  120. throw TypeError('listener must be a function');
  121. if(!this._events)
  122. this._events = {};
  123. // 在类型为“newListener”的情况下避免递归!以前 TDDE
  124. // 将它添加到侦听器中,首先发出“newListener”。
  125. if(this._events.newListener)
  126. this.emit('newListener', type,
  127. isFunction(listener.listener) ?
  128. listener.listener : listener);
  129. if(!this._events[type])
  130. // 优化一个侦听器的情况。不需要额外的数组对象。
  131. this._events[type] = listener;
  132. else if(isObject(this._events[type]))
  133. // If 如果我们已经有一个数组,只需追加。
  134. this._events[type].push(listener);
  135. else
  136. // 云游戏添加第二个元素时,需要更改为数组。
  137. this._events[type] = [this._events[type], listener];
  138. // 检查侦听器是否泄漏
  139. if(isObject(this._events[type]) && !this._events[type].warned) {
  140. if(!isUndefined(this._maxListeners)) {
  141. m = this._maxListeners;
  142. } else {
  143. m = EventEmitter.defaultMaxListeners;
  144. }
  145. if(m && m > 0 && this._events[type].length > m) {
  146. this._events[type].warned = true;
  147. console.error('(node) warning: possible EventEmitter memory ' +
  148. 'leak detected. %d listeners added. ' +
  149. 'Use emitter.setMaxListeners() to increase limit.',
  150. this._events[type].length);
  151. if(typeof console.trace === 'function') {
  152. console.trace();
  153. }
  154. }
  155. }
  156. return this;
  157. };
  158. EventEmitter.prototype.on = EventEmitter.prototype.addListener;
  159. EventEmitter.prototype.once = function(type, listener) {
  160. if(!isFunction(listener))
  161. throw TypeError('listener must be a function');
  162. var fired = false;
  163. function g() {
  164. this.removeListener(type, g);
  165. if(!fired) {
  166. fired = true;
  167. listener.apply(this, arguments);
  168. }
  169. }
  170. g.listener = listener;
  171. this.on(type, g);
  172. return this;
  173. };
  174. EventEmitter.prototype.removeListener = function(type, listener) {
  175. var list, position, length, i;
  176. if(!isFunction(listener))
  177. throw TypeError('listener must be a function');
  178. if(!this._events || !this._events[type])
  179. return this;
  180. list = this._events[type];
  181. length = list.length;
  182. position = -1;
  183. if(list === listener ||
  184. (isFunction(list.listener) && list.listener === listener)) {
  185. delete this._events[type];
  186. if(this._events.removeListener)
  187. this.emit('removeListener', type, listener);
  188. } else if(isObject(list)) {
  189. for(i = length; i-- > 0;) {
  190. if(list[i] === listener ||
  191. (list[i].listener && list[i].listener === listener)) {
  192. position = i;
  193. break;
  194. }
  195. }
  196. if(position < 0)
  197. return this;
  198. if(list.length === 1) {
  199. list.length = 0;
  200. delete this._events[type];
  201. } else {
  202. list.splice(position, 1);
  203. }
  204. if(this._events.removeListener)
  205. this.emit('removeListener', type, listener);
  206. }
  207. return this;
  208. };
  209. EventEmitter.prototype.removeAllListeners = function(type) {
  210. var key, listeners;
  211. if(!this._events)
  212. return this;
  213. if(!this._events.removeListener) {
  214. if(arguments.length === 0)
  215. this._events = {};
  216. else if(this._events[type])
  217. delete this._events[type];
  218. return this;
  219. }
  220. if(arguments.length === 0) {
  221. for(key in this._events) {
  222. if(key === 'removeListener') continue;
  223. this.removeAllListeners(key);
  224. }
  225. this.removeAllListeners('removeListener');
  226. this._events = {};
  227. return this;
  228. }
  229. listeners = this._events[type];
  230. if(isFunction(listeners)) {
  231. this.removeListener(type, listeners);
  232. } else if(listeners) {
  233. while(listeners.length)
  234. this.removeListener(type, listeners[listeners.length - 1]);
  235. }
  236. delete this._events[type];
  237. return this;
  238. };
  239. EventEmitter.prototype.listeners = function(type) {
  240. var ret;
  241. if(!this._events || !this._events[type])
  242. ret = [];
  243. else if(isFunction(this._events[type]))
  244. ret = [this._events[type]];
  245. else
  246. ret = this._events[type].slice();
  247. return ret;
  248. };
  249. EventEmitter.prototype.listenerCount = function(type) {
  250. if(this._events) {
  251. var evlistener = this._events[type];
  252. if(isFunction(evlistener))
  253. return 1;
  254. else if(evlistener)
  255. return evlistener.length;
  256. }
  257. return 0;
  258. };
  259. EventEmitter.listenerCount = function(emitter, type) {
  260. return emitter.listenerCount(type);
  261. };
  262. function isFunction(arg) {
  263. return typeof arg === 'function';
  264. }
  265. function isNumber(arg) {
  266. return typeof arg === 'number';
  267. }
  268. function isObject(arg) {
  269. return typeof arg === 'object' && arg !== null;
  270. }
  271. function isUndefined(arg) {
  272. return arg === void 0;
  273. }
  274. }, {}],
  275. 2: [function(require, module, exports) {
  276. var process = module.exports = {};
  277. var cachedSetTimeout;
  278. var cachedClearTimeout;
  279. function defaultSetTimout() {
  280. throw new Error('setTimeout has not been defined');
  281. }
  282. function defaultClearTimeout() {
  283. throw new Error('clearTimeout has not been defined');
  284. }
  285. (function() {
  286. try {
  287. if(typeof setTimeout === 'function') {
  288. cachedSetTimeout = setTimeout;
  289. } else {
  290. cachedSetTimeout = defaultSetTimout;
  291. }
  292. } catch(e) {
  293. cachedSetTimeout = defaultSetTimout;
  294. }
  295. try {
  296. if(typeof clearTimeout === 'function') {
  297. cachedClearTimeout = clearTimeout;
  298. } else {
  299. cachedClearTimeout = defaultClearTimeout;
  300. }
  301. } catch(e) {
  302. cachedClearTimeout = defaultClearTimeout;
  303. }
  304. }())
  305. function runTimeout(fun) {
  306. if(cachedSetTimeout === setTimeout) {
  307. return setTimeout(fun, 0);
  308. }
  309. if((cachedSetTimeout === defaultSetTimout || !cachedSetTimeout) && setTimeout) {
  310. cachedSetTimeout = setTimeout;
  311. return setTimeout(fun, 0);
  312. }
  313. try {
  314. return cachedSetTimeout(fun, 0);
  315. } catch(e) {
  316. try {
  317. return cachedSetTimeout.call(null, fun, 0);
  318. } catch(e) {
  319. // 渲染不执行8998
  320. return cachedSetTimeout.call(this, fun, 0);
  321. }
  322. }
  323. }
  324. function runClearTimeout(marker) {
  325. if(cachedClearTimeout === clearTimeout) {
  326. //正常环境
  327. return clearTimeout(marker);
  328. }
  329. if((cachedClearTimeout === defaultClearTimeout || !cachedClearTimeout) && clearTimeout) {
  330. cachedClearTimeout = clearTimeout;
  331. return clearTimeout(marker);
  332. }
  333. try {
  334. return cachedClearTimeout(marker);
  335. } catch(e) {
  336. try {
  337. return cachedClearTimeout.call(null, marker);
  338. } catch(e) {
  339. return cachedClearTimeout.call(this, marker);
  340. }
  341. }
  342. }
  343. var queue = [];
  344. var draining = false;
  345. var currentQueue;
  346. var queueIndex = -1;
  347. function cleanUpNextTick() {
  348. if(!draining || !currentQueue) {
  349. return;
  350. }
  351. draining = false;
  352. if(currentQueue.length) {
  353. queue = currentQueue.concat(queue);
  354. } else {
  355. queueIndex = -1;
  356. }
  357. if(queue.length) {
  358. drainQueue();
  359. }
  360. }
  361. function drainQueue() {
  362. if(draining) {
  363. return;
  364. }
  365. var timeout = runTimeout(cleanUpNextTick);
  366. draining = true;
  367. var len = queue.length;
  368. while(len) {
  369. currentQueue = queue;
  370. queue = [];
  371. while(++queueIndex < len) {
  372. if(currentQueue) {
  373. currentQueue[queueIndex].run();
  374. }
  375. }
  376. queueIndex = -1;
  377. len = queue.length;
  378. }
  379. currentQueue = null;
  380. draining = false;
  381. runClearTimeout(timeout);
  382. }
  383. process.nextTick = function(fun) {
  384. var args = new Array(arguments.length - 1);
  385. if(arguments.length > 1) {
  386. for(var i = 1; i < arguments.length; i++) {
  387. args[i - 1] = arguments[i];
  388. }
  389. }
  390. queue.push(new Item(fun, args));
  391. if(queue.length === 1 && !draining) {
  392. runTimeout(drainQueue);
  393. }
  394. };
  395. // v8 可预测的对象 意思就是 高等画质
  396. function Item(fun, array) {
  397. this.fun = fun;
  398. this.array = array;
  399. }
  400. Item.prototype.run = function() {
  401. this.fun.apply(null, this.array);
  402. };
  403. process.title = 'browser';
  404. process.browser = true;
  405. process.env = {};
  406. process.argv = [];
  407. process.version = ''; // empty string to avoid regexp issues
  408. process.versions = {};
  409. function noop() {}
  410. process.on = noop;
  411. process.addListener = noop;
  412. process.once = noop;
  413. process.off = noop;
  414. process.removeListener = noop;
  415. process.removeAllListeners = noop;
  416. process.emit = noop;
  417. process.prependListener = noop;
  418. process.prependOnceListener = noop;
  419. process.listeners = function(name) {
  420. return []
  421. }
  422. process.binding = function(name) {
  423. throw new Error('process.binding is not supported');
  424. };
  425. process.cwd = function() {
  426. return '/'
  427. };
  428. process.chdir = function(dir) {
  429. throw new Error('process.chdir is not supported');
  430. };
  431. process.umask = function() {
  432. return 0;
  433. };
  434. }, {}],
  435. 3: [function(require, module, exports) {
  436. 'use strict';
  437. Object.defineProperty(exports, "__esModule", {
  438. value: true
  439. });
  440. var _createClass = function() {
  441. function defineProperties(target, props) {
  442. for(var i = 0; i < props.length; i++) {
  443. var descriptor = props[i];
  444. descriptor.enumerable = descriptor.enumerable || false;
  445. descriptor.configurable = true;
  446. if("value" in descriptor) descriptor.writable = true;
  447. Object.defineProperty(target, descriptor.key, descriptor);
  448. }
  449. }
  450. return function(Constructor, protoProps, staticProps) {
  451. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  452. if(staticProps) defineProperties(Constructor, staticProps);
  453. return Constructor;
  454. };
  455. }();
  456. var _events = require('../events');
  457. var _events2 = _interopRequireDefault(_events);
  458. var _eventHandler = require('../event-handler');
  459. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  460. function _interopRequireDefault(obj) {
  461. return obj && obj.__esModule ? obj : {
  462. default: obj
  463. };
  464. }
  465. function _classCallCheck(instance, Constructor) {
  466. if(!(instance instanceof Constructor)) {
  467. throw new TypeError("Cannot call a class as a function");
  468. }
  469. }
  470. function _possibleConstructorReturn(self, call) {
  471. if(!self) {
  472. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  473. }
  474. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  475. }
  476. function _inherits(subClass, superClass) {
  477. if(typeof superClass !== "function" && superClass !== null) {
  478. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  479. }
  480. subClass.prototype = Object.create(superClass && superClass.prototype, {
  481. constructor: {
  482. value: subClass,
  483. enumerable: false,
  484. writable: true,
  485. configurable: true
  486. }
  487. });
  488. if(superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  489. }
  490. /*
  491. * Buffer Controller
  492. */
  493. var BufferController = function(_EventHandler) {
  494. _inherits(BufferController, _EventHandler);
  495. function BufferController(wfs) {
  496. _classCallCheck(this, BufferController);
  497. var _this = _possibleConstructorReturn(this, (BufferController.__proto__ || Object.getPrototypeOf(BufferController)).call(this, wfs, _events2.default.MEDIA_ATTACHING, _events2.default.BUFFER_APPENDING, _events2.default.BUFFER_RESET));
  498. _this.mediaSource = null;
  499. _this.media = null;
  500. _this.pendingTracks = {};
  501. _this.sourceBuffer = {};
  502. _this.segments = [];
  503. _this.appended = 0;
  504. _this._msDuration = null;
  505. _this.onsbue = _this.onSBUpdateEnd.bind(_this);
  506. _this.browserType = 0;
  507. if(navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
  508. _this.browserType = 1;
  509. }
  510. _this.mediaType = 'H264Raw';
  511. _this.websocketName = undefined;
  512. _this.channelName = undefined;
  513. return _this;
  514. }
  515. _createClass(BufferController, [{
  516. key: 'destroy',
  517. value: function destroy() {
  518. _eventHandler2.default.prototype.destroy.call(this);
  519. }
  520. }, {
  521. key: 'onMediaAttaching',
  522. value: function onMediaAttaching(data) {
  523. var media = this.media = data.media;
  524. this.mediaType = data.mediaType;
  525. this.websocketName = data.websocketName;
  526. this.channelName = data.channelName;
  527. if(media) {
  528. var ms = this.mediaSource = new MediaSource();
  529. this.onmso = this.onMediaSourceOpen.bind(this);
  530. this.onmse = this.onMediaSourceEnded.bind(this);
  531. this.onmsc = this.onMediaSourceClose.bind(this);
  532. ms.addEventListener('sourceopen', this.onmso);
  533. ms.addEventListener('sourceended', this.onmse);
  534. ms.addEventListener('sourceclose', this.onmsc);
  535. media.src = URL.createObjectURL(ms);
  536. }
  537. }
  538. }, {
  539. key: 'onMediaDetaching',
  540. value: function onMediaDetaching() {}
  541. }, {
  542. key: 'onBufferAppending',
  543. value: function onBufferAppending(data) {
  544. if(!this.segments) {
  545. this.segments = [data];
  546. } else {
  547. this.segments.push(data);
  548. }
  549. this.doAppending();
  550. }
  551. }, {
  552. key: 'onMediaSourceClose',
  553. value: function onMediaSourceClose() {
  554. console.log('media source closed');
  555. }
  556. }, {
  557. key: 'onMediaSourceEnded',
  558. value: function onMediaSourceEnded() {
  559. console.log('media source ended');
  560. }
  561. }, {
  562. key: 'onSBUpdateEnd',
  563. value: function onSBUpdateEnd(event) {
  564. // Firefox
  565. if(this.browserType === 1) {
  566. this.mediaSource.endOfStream();
  567. this.media.play();
  568. }
  569. // console.log("黄飞>>>currentTime: " + this.media.currentTime);//1111111111
  570. var buffered = this.sourceBuffer['video'].buffered;
  571. var played = this.media.played;
  572. for(var j = 0; j < played.length; j++) {
  573. // console.log("played start: " + played.start(j));//111111111
  574. // console.log("played end: " + played.end(j));
  575. }
  576. // console.log("readystate: " + this.media.readyState);
  577. for(var i = 0; i < buffered.length; i++) {
  578. // console.log("start: " + buffered.start(i));//1111111111
  579. // console.log("end: " + buffered.end(i));
  580. //this.media.currentTime = buffered.end(i);
  581. }
  582. this.appending = false;
  583. this.doAppending();
  584. this.updateMediaElementDuration();
  585. }
  586. }, {
  587. key: 'updateMediaElementDuration',
  588. value: function updateMediaElementDuration() {}
  589. }, {
  590. key: 'onMediaSourceOpen',
  591. value: function onMediaSourceOpen() {
  592. var mediaSource = this.mediaSource;
  593. if(mediaSource) {
  594. mediaSource.removeEventListener('sourceopen', this.onmso);
  595. }
  596. if(this.mediaType === 'FMp4') {
  597. this.checkPendingTracks();
  598. }
  599. this.wfs.trigger(_events2.default.MEDIA_ATTACHED, {
  600. media: this.media,
  601. channelName: this.channelName,
  602. mediaType: this.mediaType,
  603. websocketName: this.websocketName
  604. });
  605. }
  606. }, {
  607. key: 'checkPendingTracks',
  608. value: function checkPendingTracks() {
  609. this.createSourceBuffers({
  610. tracks: 'video',
  611. mimeType: ''
  612. });
  613. this.pendingTracks = {};
  614. }
  615. }, {
  616. key: 'onBufferReset',
  617. value: function onBufferReset(data) {
  618. if(this.mediaType === 'H264Raw') {
  619. this.createSourceBuffers({
  620. tracks: 'video',
  621. mimeType: data.mimeType
  622. });
  623. }
  624. }
  625. }, {
  626. key: 'createSourceBuffers',
  627. value: function createSourceBuffers(tracks) {
  628. var sourceBuffer = this.sourceBuffer,
  629. mediaSource = this.mediaSource;
  630. var mimeType = void 0;
  631. // 因后端要高等画质 然后 if HUANGFEI
  632. if(tracks.mimeType === '') {
  633. mimeType = 'video/mp4;codecs=avc1.420028'; // avc1.42c01f avc1.42801e avc1.640028 avc1.420028
  634. } else {
  635. mimeType = 'video/mp4;codecs=' + tracks.mimeType;
  636. }
  637. try {
  638. var sb = sourceBuffer['video'] = mediaSource.addSourceBuffer(mimeType);
  639. sb.addEventListener('updateend', this.onsbue);
  640. track.buffer = sb;
  641. } catch(err) {}
  642. this.wfs.trigger(_events2.default.BUFFER_CREATED, {
  643. tracks: tracks
  644. });
  645. this.media.play();
  646. }
  647. }, {
  648. key: 'doAppending',
  649. value: function doAppending() {
  650. var wfs = this.wfs,
  651. sourceBuffer = this.sourceBuffer,
  652. segments = this.segments;
  653. if(Object.keys(sourceBuffer).length) {
  654. if(this.media.error) {
  655. this.segments = [];
  656. console.log('trying to append although a media error occured, flush segment and abort');
  657. return;
  658. }
  659. if(this.appending) {
  660. return;
  661. }
  662. if(segments && segments.length) {
  663. var segment = segments.shift();
  664. //console.log("segments len: " + segments.length + " segment len: " + segment.data.length);
  665. try {
  666. if(sourceBuffer[segment.type]) {
  667. this.parent = segment.parent;
  668. sourceBuffer[segment.type].appendBuffer(segment.data);
  669. this.appendError = 0;
  670. this.appended++;
  671. this.appending = true;
  672. } else {}
  673. } catch(err) {
  674. segments.unshift(segment);
  675. var event = {
  676. type: ErrorTypes.MEDIA_ERROR
  677. };
  678. if(err.code !== 22) {
  679. if(this.appendError) {
  680. this.appendError++;
  681. } else {
  682. this.appendError = 1;
  683. }
  684. event.details = ErrorDetails.BUFFER_APPEND_ERROR;
  685. event.frag = this.fragCurrent;
  686. if(this.appendError > wfs.config.appendErrorMaxRetry) {
  687. segments = [];
  688. event.fatal = true;
  689. return;
  690. } else {
  691. event.fatal = false;
  692. }
  693. } else {
  694. this.segments = [];
  695. event.details = ErrorDetails.BUFFER_FULL_ERROR;
  696. return;
  697. }
  698. }
  699. }
  700. }
  701. }
  702. }]);
  703. return BufferController;
  704. }(_eventHandler2.default);
  705. exports.default = BufferController;
  706. }, {
  707. "../event-handler": 8,
  708. "../events": 9
  709. }],
  710. 4: [function(require, module, exports) {
  711. 'use strict';
  712. Object.defineProperty(exports, "__esModule", {
  713. value: true
  714. });
  715. var _createClass = function() {
  716. function defineProperties(target, props) {
  717. for(var i = 0; i < props.length; i++) {
  718. var descriptor = props[i];
  719. descriptor.enumerable = descriptor.enumerable || false;
  720. descriptor.configurable = true;
  721. if("value" in descriptor) descriptor.writable = true;
  722. Object.defineProperty(target, descriptor.key, descriptor);
  723. }
  724. }
  725. return function(Constructor, protoProps, staticProps) {
  726. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  727. if(staticProps) defineProperties(Constructor, staticProps);
  728. return Constructor;
  729. };
  730. }();
  731. var _events = require('../events');
  732. var _events2 = _interopRequireDefault(_events);
  733. var _eventHandler = require('../event-handler');
  734. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  735. function _interopRequireDefault(obj) {
  736. return obj && obj.__esModule ? obj : {
  737. default: obj
  738. };
  739. }
  740. function _classCallCheck(instance, Constructor) {
  741. if(!(instance instanceof Constructor)) {
  742. throw new TypeError("Cannot call a class as a function");
  743. }
  744. }
  745. function _possibleConstructorReturn(self, call) {
  746. if(!self) {
  747. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  748. }
  749. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  750. }
  751. function _inherits(subClass, superClass) {
  752. if(typeof superClass !== "function" && superClass !== null) {
  753. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  754. }
  755. subClass.prototype = Object.create(superClass && superClass.prototype, {
  756. constructor: {
  757. value: subClass,
  758. enumerable: false,
  759. writable: true,
  760. configurable: true
  761. }
  762. });
  763. if(superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  764. }
  765. /*
  766. * Flow Controller
  767. */
  768. var FlowController = function(_EventHandler) {
  769. _inherits(FlowController, _EventHandler);
  770. function FlowController(wfs) {
  771. _classCallCheck(this, FlowController);
  772. var _this = _possibleConstructorReturn(this, (FlowController.__proto__ || Object.getPrototypeOf(FlowController)).call(this, wfs, _events2.default.MEDIA_ATTACHED, _events2.default.BUFFER_CREATED, _events2.default.FILE_PARSING_DATA, _events2.default.FILE_HEAD_LOADED, _events2.default.FILE_DATA_LOADED, _events2.default.WEBSOCKET_ATTACHED, _events2.default.FRAG_PARSING_DATA, _events2.default.FRAG_PARSING_INIT_SEGMENT));
  773. _this.fileStart = 0;
  774. _this.fileEnd = 0;
  775. _this.pendingAppending = 0;
  776. _this.mediaType = undefined;
  777. channelName: _this.channelName;
  778. return _this;
  779. }
  780. _createClass(FlowController, [{
  781. key: 'destroy',
  782. value: function destroy() {
  783. _eventHandler2.default.prototype.destroy.call(this);
  784. }
  785. }, {
  786. key: 'onMediaAttached',
  787. value: function onMediaAttached(data) {
  788. if(data.websocketName != undefined) {
  789. //WS拼接url等回调
  790. var query = window.location.search.substring(1);
  791. var vars = query.split("&");
  792. var data = {}
  793. var url = window.location.href;
  794. url = url.split('/')
  795. var clientType = vars.find(e => {
  796. return e.startsWith('clientType')
  797. })
  798. var cardIp = vars.find(e => {
  799. return e.startsWith('cardIp')
  800. })
  801. var port = vars.find(e => {
  802. return e.startsWith('port')
  803. })
  804. var sn = vars.find(e => {
  805. return e.startsWith('sn')
  806. })
  807. var demoTime = vars.find(e => {
  808. return e.startsWith('demoTime')
  809. })
  810. var data = {}
  811. data.clientType=clientType.substring(11, clientType.length)
  812. data.cardIp=cardIp.substring(7, cardIp.length)
  813. data.port=port.substring(5, port.length)
  814. data.sn=sn.substring(3, sn.length)
  815. data.demoTime=demoTime.substring(9, demoTime.length)
  816. console.log(data)
  817. //var client = new WebSocket("wss://xcx.androidscloud.com/videoWebSocket?clientType=0&cardIp=30.30.30.24&port=9100&sn=RK3930C2301900042");
  818. var urlss = url[2]
  819. client = new WebSocket("ws://" + urlss + '/videoWebSocket?' + "clientType=" + data.clientType +"&cardIp=" + data.cardIp + "&port=" + data.port + "&sn=" + data.sn);
  820. this.wfs.attachWebsocket(client, data.channelName);
  821. } else {
  822. console.log('websocketName ERROE!!!');
  823. }
  824. }
  825. }, {
  826. key: 'onBufferCreated',
  827. value: function onBufferCreated(data) {
  828. this.mediaType = data.mediaType;
  829. }
  830. }, {
  831. key: 'onFileHeadLoaded',
  832. value: function onFileHeadLoaded(data) {}
  833. }, {
  834. key: 'onFileDataLoaded',
  835. value: function onFileDataLoaded(data) {}
  836. }, {
  837. key: 'onFileParsingData',
  838. value: function onFileParsingData(data) {}
  839. }, {
  840. key: 'onWebsocketAttached',
  841. value: function onWebsocketAttached(data) {
  842. this.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  843. type: 'video',
  844. data: data.payload,
  845. parent: 'main'
  846. });
  847. }
  848. }, {
  849. key: 'onFragParsingInitSegment',
  850. value: function onFragParsingInitSegment(data) {
  851. var tracks = data.tracks,
  852. trackName,
  853. track;
  854. track = tracks.video;
  855. if(track) {
  856. track.id = data.id;
  857. }
  858. for(trackName in tracks) {
  859. track = tracks[trackName];
  860. var initSegment = track.initSegment;
  861. if(initSegment) {
  862. this.pendingAppending++;
  863. this.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  864. type: trackName,
  865. data: initSegment,
  866. parent: 'main'
  867. });
  868. }
  869. }
  870. }
  871. }, {
  872. key: 'onFragParsingData',
  873. value: function onFragParsingData(data) {
  874. var _this2 = this;
  875. if(data.type === 'video') {}
  876. [data.data1, data.data2].forEach(function(buffer) {
  877. if(buffer) {
  878. _this2.pendingAppending++;
  879. _this2.wfs.trigger(_events2.default.BUFFER_APPENDING, {
  880. type: data.type,
  881. data: buffer,
  882. parent: 'main'
  883. });
  884. }
  885. });
  886. }
  887. }]);
  888. return FlowController;
  889. }(_eventHandler2.default);
  890. exports.default = FlowController;
  891. }, {
  892. "../event-handler": 8,
  893. "../events": 9
  894. }],
  895. 5: [function(require, module, exports) {
  896. 'use strict';
  897. Object.defineProperty(exports, "__esModule", {
  898. value: true
  899. });
  900. var _createClass = function() {
  901. function defineProperties(target, props) {
  902. for(var i = 0; i < props.length; i++) {
  903. var descriptor = props[i];
  904. descriptor.enumerable = descriptor.enumerable || false;
  905. descriptor.configurable = true;
  906. if("value" in descriptor) descriptor.writable = true;
  907. Object.defineProperty(target, descriptor.key, descriptor);
  908. }
  909. }
  910. return function(Constructor, protoProps, staticProps) {
  911. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  912. if(staticProps) defineProperties(Constructor, staticProps);
  913. return Constructor;
  914. };
  915. }();
  916. var _logger = require('../utils/logger');
  917. function _classCallCheck(instance, Constructor) {
  918. if(!(instance instanceof Constructor)) {
  919. throw new TypeError("Cannot call a class as a function");
  920. }
  921. }
  922. var ExpGolomb = function() {
  923. function ExpGolomb(data) {
  924. _classCallCheck(this, ExpGolomb);
  925. this.data = data;
  926. // 要检查的剩余字节数this.数据
  927. this.bytesAvailable = this.data.byteLength;
  928. // 正在检查的当前uint
  929. this.word = 0; // :uint
  930. // 当前字中还剩要检查的位数 huangfei HDDT
  931. this.bitsAvailable = 0; // :uint
  932. }
  933. // ():void
  934. _createClass(ExpGolomb, [{
  935. key: 'loadWord',
  936. value: function loadWord() {
  937. var position = this.data.byteLength - this.bytesAvailable,
  938. workingBytes = new Uint8Array(4),
  939. availableBytes = Math.min(4, this.bytesAvailable);
  940. if(availableBytes === 0) {
  941. throw new Error('no bytes available');
  942. }
  943. workingBytes.set(this.data.subarray(position, position + availableBytes));
  944. this.word = new DataView(workingBytes.buffer).getUint32(0);
  945. // 跟踪的数量this.数据已经处理过的
  946. this.bitsAvailable = availableBytes * 8;
  947. this.bytesAvailable -= availableBytes;
  948. }
  949. // (count:int):void
  950. }, {
  951. key: 'skipBits', //船体用头 0001
  952. value: function skipBits(count) {
  953. var skipBytes; // :int
  954. if(this.bitsAvailable > count) {
  955. this.word <<= count;
  956. this.bitsAvailable -= count;
  957. } else {
  958. count -= this.bitsAvailable;
  959. skipBytes = count >> 3;
  960. count -= skipBytes >> 3;
  961. this.bytesAvailable -= skipBytes;
  962. this.loadWord();
  963. this.word <<= count;
  964. this.bitsAvailable -= count;
  965. }
  966. }
  967. // (size:int):uint
  968. }, {
  969. key: 'readBits',
  970. value: function readBits(size) {
  971. var bits = Math.min(this.bitsAvailable, size),
  972. // :uint
  973. valu = this.word >>> 32 - bits; // :uint
  974. if(size > 32) {
  975. _logger.logger.error('Cannot read more than 32 bits at a time');
  976. }
  977. this.bitsAvailable -= bits;
  978. if(this.bitsAvailable > 0) {
  979. this.word <<= bits;
  980. } else if(this.bytesAvailable > 0) {
  981. this.loadWord();
  982. }
  983. bits = size - bits;
  984. if(bits > 0) {
  985. return valu << bits | this.readBits(bits);
  986. } else {
  987. return valu;
  988. }
  989. }
  990. // ():uint
  991. }, {
  992. key: 'skipLZ',
  993. value: function skipLZ() {
  994. var leadingZeroCount; // :uint
  995. for(leadingZeroCount = 0; leadingZeroCount < this.bitsAvailable; ++leadingZeroCount) {
  996. if(0 !== (this.word & 0x80000000 >>> leadingZeroCount)) {
  997. // the first bit of working word is 1
  998. this.word <<= leadingZeroCount;
  999. this.bitsAvailable -= leadingZeroCount;
  1000. return leadingZeroCount;
  1001. }
  1002. }
  1003. // we exhausted word and still have not found a 1
  1004. this.loadWord();
  1005. return leadingZeroCount + this.skipLZ();
  1006. }
  1007. // ():void
  1008. }, {
  1009. key: 'skipUEG',
  1010. value: function skipUEG() {
  1011. this.skipBits(1 + this.skipLZ());
  1012. }
  1013. // ():void
  1014. }, {
  1015. key: 'skipEG',
  1016. value: function skipEG() {
  1017. this.skipBits(1 + this.skipLZ());
  1018. }
  1019. // ():uint
  1020. }, {
  1021. key: 'readUEG',
  1022. value: function readUEG() {
  1023. var clz = this.skipLZ(); // :uint
  1024. return this.readBits(clz + 1) - 1;
  1025. }
  1026. // ():int
  1027. }, {
  1028. key: 'readEG',
  1029. value: function readEG() {
  1030. var valu = this.readUEG(); // :int
  1031. if(0x01 & valu) {
  1032. // 如果设置了低阶位,则该数字为奇数 TDDE HUANG 这个是为了画read
  1033. return 1 + valu >>> 1; // 加1使其均匀,然后除以2
  1034. } else {
  1035. return -1 * (valu >>> 1); // 除以2,然后设为负数
  1036. }
  1037. }
  1038. // Some convenience functions
  1039. // :Boolean
  1040. }, {
  1041. key: 'readBoolean',
  1042. value: function readBoolean() {
  1043. return 1 === this.readBits(1);
  1044. }
  1045. // ():int
  1046. }, {
  1047. key: 'readUByte',
  1048. value: function readUByte() {
  1049. return this.readBits(8);
  1050. }
  1051. // ():int
  1052. }, {
  1053. key: 'readUShort',
  1054. value: function readUShort() {
  1055. return this.readBits(16);
  1056. }
  1057. // ():int
  1058. }, {
  1059. key: 'readUInt',
  1060. value: function readUInt() {
  1061. return this.readBits(32);
  1062. }
  1063. /**
  1064. *将ExpGolomb解码器移过缩放列表。缩放
  1065. *列表可以作为序列参数的一部分进行传输
  1066. *与变形无关。
  1067. *@param count{number}此缩放列表中的条目数
  1068. *@见建议ITU-T H.264,第7.3.2.1.1.1节
  1069. */
  1070. }, {
  1071. key: 'skipScalingList',
  1072. value: function skipScalingList(count) {
  1073. var lastScale = 8,
  1074. nextScale = 8,
  1075. j,
  1076. deltaScale;
  1077. for(j = 0; j < count; j++) {
  1078. if(nextScale !== 0) {
  1079. deltaScale = this.readEG();
  1080. nextScale = (lastScale + deltaScale + 256) % 256;
  1081. }
  1082. lastScale = nextScale === 0 ? lastScale : nextScale;
  1083. }
  1084. }
  1085. /**
  1086. *读取序列参数集并返回一些的视频
  1087. *财产。序列参数集是H264元数据
  1088. *描述即将到来的视频帧的属性。
  1089. *@param data{Uint8Array}序列参数集的字节数
  1090. *@return{object}从
  1091. *序列参数集,包括
  1092. *关联的视频帧。
  1093. */
  1094. }, {
  1095. key: 'readSPS',
  1096. value: function readSPS() {
  1097. var frameCropLeftOffset = 0,
  1098. frameCropRightOffset = 0,
  1099. frameCropTopOffset = 0,
  1100. frameCropBottomOffset = 0,
  1101. sarScale = 1,
  1102. profileIdc,
  1103. profileCompat,
  1104. levelIdc,
  1105. numRefFramesInPicOrderCntCycle,
  1106. picWidthInMbsMinus1,
  1107. picHeightInMapUnitsMinus1,
  1108. frameMbsOnlyFlag,
  1109. scalingListCount,
  1110. i;
  1111. this.readUByte();
  1112. profileIdc = this.readUByte(); // profile_idc 》》》idc简介
  1113. profileCompat = this.readBits(5); // constraint_set[0-4]_flag, u(5)
  1114. this.skipBits(3); // reserved_zero_3bits u(3),
  1115. levelIdc = this.readUByte(); //level_idc u(8)
  1116. this.skipUEG(); // seq_parameter_set_id
  1117. // 有些配置文件有更多我们不需要的可选数据
  1118. if(profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || profileIdc === 44 || profileIdc === 83 || profileIdc === 86 || profileIdc === 118 || profileIdc === 128) {
  1119. var chromaFormatIdc = this.readUEG();
  1120. if(chromaFormatIdc === 3) {
  1121. this.skipBits(1); // separate_colour_plane_flag对应的函数
  1122. }
  1123. this.skipUEG(); // bit_depth_luma_minus8 对应的函数
  1124. this.skipUEG(); // bit_depth_chroma_minus8 对应的函数
  1125. this.skipBits(1); // qpprime_y_zero_transform_bypass_flag 对应的函数
  1126. if(this.readBoolean()) {
  1127. // seq_scaling_matrix_present_flag
  1128. scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
  1129. for(i = 0; i < scalingListCount; i++) {
  1130. if(this.readBoolean()) {
  1131. // seq_scaling_list_present_flag[ i ]
  1132. if(i < 6) {
  1133. this.skipScalingList(16);
  1134. } else {
  1135. this.skipScalingList(64);
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. this.skipUEG();
  1142. var picOrderCntType = this.readUEG();
  1143. if(picOrderCntType === 0) {
  1144. this.readUEG(); //log2最大pic顺序cnt u lsb minus4
  1145. } else if(picOrderCntType === 1) {
  1146. this.skipBits(1); // 标志
  1147. this.skipEG(); // 非参考图片的偏移量
  1148. this.skipEG(); // 从上到下”字段的偏移量
  1149. numRefFramesInPicOrderCntCycle = this.readUEG();
  1150. for(i = 0; i < numRefFramesInPicOrderCntCycle; i++) {
  1151. this.skipEG(); // 参考帧的偏移量[ i ]
  1152. }
  1153. }
  1154. this.skipUEG(); // efu u帧中的最大值
  1155. this.skipBits(1); // 间隙数值允许标志
  1156. picWidthInMbsMinus1 = this.readUEG();
  1157. picHeightInMapUnitsMinus1 = this.readUEG();
  1158. frameMbsOnlyFlag = this.readBits(1);
  1159. if(frameMbsOnlyFlag === 0) {
  1160. this.skipBits(1); // mb_adaptive_frame_field_flag 对应的函数
  1161. }
  1162. this.skipBits(1); // direct_8x8_inference_flag 对应的函数
  1163. if(this.readBoolean()) {
  1164. frameCropLeftOffset = this.readUEG();
  1165. frameCropRightOffset = this.readUEG();
  1166. frameCropTopOffset = this.readUEG();
  1167. frameCropBottomOffset = this.readUEG();
  1168. }
  1169. if(this.readBoolean()) {
  1170. if(this.readBoolean()) {
  1171. var sarRatio = void 0;
  1172. var aspectRatioIdc = this.readUByte();
  1173. switch(aspectRatioIdc) {
  1174. case 1:
  1175. sarRatio = [1, 1];
  1176. break;
  1177. case 2:
  1178. sarRatio = [12, 11];
  1179. break;
  1180. case 3:
  1181. sarRatio = [10, 11];
  1182. break;
  1183. case 4:
  1184. sarRatio = [16, 11];
  1185. break;
  1186. case 5:
  1187. sarRatio = [40, 33];
  1188. break;
  1189. case 6:
  1190. sarRatio = [24, 11];
  1191. break;
  1192. case 7:
  1193. sarRatio = [20, 11];
  1194. break;
  1195. case 8:
  1196. sarRatio = [32, 11];
  1197. break;
  1198. case 9:
  1199. sarRatio = [80, 33];
  1200. break;
  1201. case 10:
  1202. sarRatio = [18, 11];
  1203. break;
  1204. case 11:
  1205. sarRatio = [15, 11];
  1206. break;
  1207. case 12:
  1208. sarRatio = [64, 33];
  1209. break;
  1210. case 13:
  1211. sarRatio = [160, 99];
  1212. break;
  1213. case 14:
  1214. sarRatio = [4, 3];
  1215. break;
  1216. case 15:
  1217. sarRatio = [3, 2];
  1218. break;
  1219. case 16:
  1220. sarRatio = [2, 1];
  1221. break;
  1222. case 255:
  1223. {
  1224. sarRatio = [this.readUByte() << 8 | this.readUByte(), this.readUByte() << 8 | this.readUByte()];
  1225. break;
  1226. }
  1227. }
  1228. if(sarRatio) {
  1229. sarScale = sarRatio[0] / sarRatio[1];
  1230. }
  1231. }
  1232. }
  1233. return {
  1234. width: Math.ceil(((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
  1235. height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - (frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)
  1236. };
  1237. }
  1238. }, {
  1239. key: 'readSliceType',
  1240. value: function readSliceType() {
  1241. this.readUByte();
  1242. this.readUEG();
  1243. return this.readUEG();
  1244. }
  1245. }]);
  1246. return ExpGolomb;
  1247. }();
  1248. exports.default = ExpGolomb;
  1249. }, {
  1250. "../utils/logger": 17
  1251. }],
  1252. 6: [function(require, module, exports) {
  1253. 'use strict';
  1254. Object.defineProperty(exports, "__esModule", {
  1255. value: true
  1256. });
  1257. var _createClass = function() {
  1258. function defineProperties(target, props) {
  1259. for(var i = 0; i < props.length; i++) {
  1260. var descriptor = props[i];
  1261. descriptor.enumerable = descriptor.enumerable || false;
  1262. descriptor.configurable = true;
  1263. if("value" in descriptor) descriptor.writable = true;
  1264. Object.defineProperty(target, descriptor.key, descriptor);
  1265. }
  1266. }
  1267. return function(Constructor, protoProps, staticProps) {
  1268. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  1269. if(staticProps) defineProperties(Constructor, staticProps);
  1270. return Constructor;
  1271. };
  1272. }();
  1273. // 云游戏生成模块
  1274. var _errors = require('../errors');
  1275. var _events = require('../events');
  1276. var _events2 = _interopRequireDefault(_events);
  1277. var _expGolomb = require('./exp-golomb');
  1278. var _expGolomb2 = _interopRequireDefault(_expGolomb);
  1279. var _eventHandler = require('../event-handler');
  1280. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  1281. var _mp4Remuxer = require('../remux/mp4-remuxer');
  1282. var _mp4Remuxer2 = _interopRequireDefault(_mp4Remuxer);
  1283. function _interopRequireDefault(obj) {
  1284. return obj && obj.__esModule ? obj : {
  1285. default: obj
  1286. };
  1287. }
  1288. function _classCallCheck(instance, Constructor) {
  1289. if(!(instance instanceof Constructor)) {
  1290. throw new TypeError("Cannot call a class as a function");
  1291. }
  1292. }
  1293. function _possibleConstructorReturn(self, call) {
  1294. if(!self) {
  1295. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  1296. }
  1297. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  1298. }
  1299. function _inherits(subClass, superClass) {
  1300. if(typeof superClass !== "function" && superClass !== null) {
  1301. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  1302. }
  1303. subClass.prototype = Object.create(superClass && superClass.prototype, {
  1304. constructor: {
  1305. value: subClass,
  1306. enumerable: false,
  1307. writable: true,
  1308. configurable: true
  1309. }
  1310. });
  1311. if(superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  1312. }
  1313. /**
  1314. */
  1315. var h264Demuxer = function(_EventHandler) {
  1316. _inherits(h264Demuxer, _EventHandler);
  1317. function h264Demuxer(wfs) {
  1318. var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  1319. _classCallCheck(this, h264Demuxer);
  1320. var _this = _possibleConstructorReturn(this, (h264Demuxer.__proto__ || Object.getPrototypeOf(h264Demuxer)).call(this, wfs, _events2.default.H264_DATA_PARSING));
  1321. _this.config = _this.wfs.config || config;
  1322. _this.wfs = wfs;
  1323. _this.id = 'main';
  1324. var typeSupported = {
  1325. mp4: MediaSource.isTypeSupported('video/mp4') //,
  1326. // mp2t : wfs.config.enableMP2TPassThrough && MediaSource.isTypeSupported('video/mp2t')
  1327. };
  1328. _this.remuxer = new _mp4Remuxer2.default(_this.wfs, _this.id, _this.config);
  1329. _this.contiguous = true;
  1330. _this.timeOffset = 1;
  1331. _this.sn = 0;
  1332. _this.TIMESCALE = 90000;
  1333. _this.timestamp = 0;
  1334. _this.scaleFactor = _this.TIMESCALE / 1000;
  1335. _this.H264_TIMEBASE = 2000;
  1336. _this._avcTrack = {
  1337. container: 'video/mp4',
  1338. type: 'video',
  1339. id: 1,
  1340. sequenceNumber: 0,
  1341. //this._avcTrack = {container : 'video/mp4', type: 'video', id :1, sequenceNumber: 0,
  1342. samples: [],
  1343. len: 0,
  1344. nbNalu: 0,
  1345. dropped: 0,
  1346. count: 0
  1347. };
  1348. _this.browserType = 0;
  1349. if(navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) {
  1350. _this.browserType = 1;
  1351. }
  1352. return _this;
  1353. }
  1354. _createClass(h264Demuxer, [{
  1355. key: 'destroy',
  1356. value: function destroy() {
  1357. _eventHandler2.default.prototype.destroy.call(this);
  1358. }
  1359. }, {
  1360. key: 'getTimestampM',
  1361. value: function getTimestampM() {
  1362. this.timestamp += this.H264_TIMEBASE;
  1363. return this.timestamp;
  1364. }
  1365. }, {
  1366. key: 'onH264DataParsing',
  1367. value: function onH264DataParsing(event) {
  1368. this._parseAVCTrack(event.data);
  1369. if(this.browserType === 1) {
  1370. // Firefox
  1371. this.remuxer.pushVideo(0, this.sn, this._avcTrack, this.timeOffset, this.contiguous);
  1372. this.sn += 1;
  1373. } else {
  1374. this.remuxer.pushVideo(0, this.sn, this._avcTrack, this.timeOffset, this.contiguous);
  1375. this.sn += 1;
  1376. }
  1377. }
  1378. }, {
  1379. key: '_parseAVCTrack',
  1380. value: function _parseAVCTrack(array) {
  1381. var _this2 = this;
  1382. var track = this._avcTrack,
  1383. samples = track.samples,
  1384. units = this._parseAVCNALu(array),
  1385. units2 = [],
  1386. debug = false,
  1387. key = false,
  1388. length = 0,
  1389. expGolombDecoder,
  1390. avcSample,
  1391. push,
  1392. i;
  1393. var debugString = '';
  1394. var pushAccesUnit = function() {
  1395. if(units2.length) {
  1396. if(!this.config.forceKeyFrameOnDiscontinuity || key === true || track.sps && (samples.length || this.contiguous)) {
  1397. var tss = this.getTimestampM();
  1398. avcSample = {
  1399. units: {
  1400. units: units2,
  1401. length: length
  1402. },
  1403. pts: tss,
  1404. dts: tss,
  1405. key: key
  1406. };
  1407. samples.push(avcSample);
  1408. track.len += length;
  1409. track.nbNalu += units2.length;
  1410. } else {
  1411. track.dropped++;
  1412. }
  1413. units2 = [];
  1414. length = 0;
  1415. }
  1416. }.bind(this);
  1417. units.forEach(function(unit) {
  1418. switch(unit.type) {
  1419. //NDR
  1420. case 1:
  1421. push = true;
  1422. if(debug) {
  1423. debugString += 'NDR ';
  1424. }
  1425. break;
  1426. //IDR
  1427. case 5:
  1428. push = true;
  1429. if(debug) {
  1430. debugString += 'IDR ';
  1431. }
  1432. key = true;
  1433. break;
  1434. //SEI
  1435. case 6:
  1436. unit.data = _this2.discardEPB(unit.data);
  1437. expGolombDecoder = new _expGolomb2.default(unit.data);
  1438. // skip frameType
  1439. expGolombDecoder.readUByte();
  1440. break;
  1441. //SPS
  1442. case 7:
  1443. push = false;
  1444. if(debug) {
  1445. debugString += 'SPS ';
  1446. }
  1447. if(!track.sps) {
  1448. expGolombDecoder = new _expGolomb2.default(unit.data);
  1449. var config = expGolombDecoder.readSPS();
  1450. track.width = config.width;
  1451. track.height = config.height;
  1452. track.sps = [unit.data];
  1453. track.duration = 0;
  1454. var codecarray = unit.data.subarray(1, 4);
  1455. var codecstring = 'avc1.';
  1456. for(i = 0; i < 3; i++) {
  1457. var h = codecarray[i].toString(16);
  1458. if(h.length < 2) {
  1459. h = '0' + h;
  1460. }
  1461. codecstring += h;
  1462. }
  1463. track.codec = codecstring;
  1464. _this2.wfs.trigger(_events2.default.BUFFER_RESET, {
  1465. mimeType: track.codec
  1466. });
  1467. push = true;
  1468. }
  1469. break;
  1470. //PPS
  1471. case 8:
  1472. push = false;
  1473. if(debug) {
  1474. debugString += 'PPS ';
  1475. }
  1476. if(!track.pps) {
  1477. track.pps = [unit.data];
  1478. push = true;
  1479. }
  1480. break;
  1481. case 9:
  1482. push = false;
  1483. if(debug) {
  1484. debugString += 'AUD ';
  1485. }
  1486. pushAccesUnit();
  1487. break;
  1488. default:
  1489. push = false;
  1490. debugString += 'unknown NAL ' + unit.type + ' ';
  1491. break;
  1492. }
  1493. if(push) {
  1494. units2.push(unit);
  1495. length += unit.data.byteLength;
  1496. }
  1497. });
  1498. if(debug || debugString.length) {
  1499. logger.log(debugString);
  1500. }
  1501. pushAccesUnit();
  1502. }
  1503. }, {
  1504. key: '_parseAVCNALu',
  1505. value: function _parseAVCNALu(array) {
  1506. var i = 0,
  1507. len = array.byteLength,
  1508. value,
  1509. overflow,
  1510. state = 0; //state = this.avcNaluState;
  1511. var units = [],
  1512. unit,
  1513. unitType,
  1514. lastUnitStart,
  1515. lastUnitType;
  1516. while(i < len) {
  1517. value = array[i++];
  1518. // 绑定3或4字节起始代码 (00 00 01 OR 00 00 00 01)
  1519. switch(state) {
  1520. case 0:
  1521. if(value === 0) {
  1522. state = 1;
  1523. }
  1524. break;
  1525. case 1:
  1526. if(value === 0) {
  1527. state = 2;
  1528. } else {
  1529. state = 0;
  1530. }
  1531. break;
  1532. case 2:
  1533. case 3:
  1534. if(value === 0) {
  1535. state = 3;
  1536. } else if(value === 1 && i < len) {
  1537. unitType = array[i] & 0x1f;
  1538. if(lastUnitStart) {
  1539. unit = {
  1540. data: array.subarray(lastUnitStart, i - state - 1),
  1541. type: lastUnitType
  1542. };
  1543. units.push(unit);
  1544. } else {}
  1545. lastUnitStart = i;
  1546. lastUnitType = unitType;
  1547. state = 0;
  1548. } else {
  1549. state = 0;
  1550. }
  1551. break;
  1552. default:
  1553. break;
  1554. }
  1555. }
  1556. if(lastUnitStart) {
  1557. unit = {
  1558. data: array.subarray(lastUnitStart, len),
  1559. type: lastUnitType,
  1560. state: state
  1561. };
  1562. units.push(unit);
  1563. }
  1564. return units;
  1565. }
  1566. /**
  1567. * 从RBSP中删除仿真保护字节
  1568. */
  1569. }, {
  1570. key: 'discardEPB',
  1571. value: function discardEPB(data) {
  1572. var length = data.byteLength,
  1573. EPBPositions = [],
  1574. i = 1,
  1575. newLength,
  1576. newData;
  1577. // 查找所有“仿真保护字节”`
  1578. while(i < length - 2) {
  1579. if(data[i] === 0 && data[i + 1] === 0 && data[i + 2] === 0x03) {
  1580. EPBPositions.push(i + 2);
  1581. i += 2;
  1582. } else {
  1583. i++;
  1584. }
  1585. }
  1586. if(EPBPositions.length === 0) {
  1587. return data;
  1588. }
  1589. newLength = length - EPBPositions.length;
  1590. newData = new Uint8Array(newLength);
  1591. var sourceIndex = 0;
  1592. for(i = 0; i < newLength; sourceIndex++, i++) {
  1593. if(sourceIndex === EPBPositions[0]) {
  1594. // 跳过此字节
  1595. sourceIndex++;
  1596. // 删除此位置索引
  1597. EPBPositions.shift();
  1598. }
  1599. newData[i] = data[sourceIndex];
  1600. }
  1601. return newData;
  1602. }
  1603. }]);
  1604. return h264Demuxer;
  1605. }(_eventHandler2.default);
  1606. exports.default = h264Demuxer;
  1607. }, {
  1608. "../errors": 7,
  1609. "../event-handler": 8,
  1610. "../events": 9,
  1611. "../remux/mp4-remuxer": 16,
  1612. "./exp-golomb": 5
  1613. }],
  1614. 7: [function(require, module, exports) {
  1615. 'use strict';
  1616. Object.defineProperty(exports, "__esModule", {
  1617. value: true
  1618. });
  1619. var ErrorTypes = exports.ErrorTypes = {
  1620. // 网络错误的标识符(加载错误/超时…)
  1621. NETWORK_ERROR: 'networkError',
  1622. // 媒体错误的标识符(视频/分析/媒体源错误)
  1623. MEDIA_ERROR: 'mediaError',
  1624. // 所有其他错误的标识符
  1625. OTHER_ERROR: 'otherError'
  1626. };
  1627. var ErrorDetails = exports.ErrorDetails = {
  1628. // 清单加载错误的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1629. MANIFEST_LOAD_ERROR: 'manifestLoadError',
  1630. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1631. MANIFEST_LOAD_TIMEOUT: 'manifestLoadTimeOut',
  1632. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1633. MANIFEST_PARSING_ERROR: 'manifestParsingError',
  1634. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1635. MANIFEST_INCOMPATIBLE_CODECS_ERROR: 'manifestIncompatibleCodecsError',
  1636. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1637. LEVEL_LOAD_ERROR: 'levelLoadError',
  1638. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1639. LEVEL_LOAD_TIMEOUT: 'levelLoadTimeOut',
  1640. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1641. LEVEL_SWITCH_ERROR: 'levelSwitchError',
  1642. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1643. AUDIO_TRACK_LOAD_ERROR: 'audioTrackLoadError',
  1644. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1645. AUDIO_TRACK_LOAD_TIMEOUT: 'audioTrackLoadTimeOut',
  1646. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1647. FRAG_LOAD_ERROR: 'fragLoadError',
  1648. // 片段循环加载错误的标识符-数据:{frag:fragment object}
  1649. FRAG_LOOP_LOADING_ERROR: 'fragLoopLoadingError',
  1650. // 片段加载超时错误的标识符-数据:{frag:fragment object}
  1651. FRAG_LOAD_TIMEOUT: 'fragLoadTimeOut',
  1652. // 片段解密错误事件的标识符-数据:分析错误描述
  1653. FRAG_DECRYPT_ERROR: 'fragDecryptError',
  1654. // 片段解密错误事件的标识符-数据:分析错误描述
  1655. FRAG_PARSING_ERROR: 'fragParsingError',
  1656. // 清单加载超时的标识符-数据:{url:faulty url,响应:{code:error code,text:error text}
  1657. KEY_LOAD_ERROR: 'keyLoadError',
  1658. // 解密密钥加载超时错误的标识符-数据:{frag:fragment object}
  1659. KEY_LOAD_TIMEOUT: 'keyLoadTimeOut',
  1660. // 向MediaSource添加sourceBuffer时发生异常时触发-数据:{err:exception,mimeType:mimeType}
  1661. BUFFER_ADD_CODEC_ERROR: 'bufferAddCodecError',
  1662. // 缓冲区追加错误的标识符-数据:追加错误描述
  1663. BUFFER_APPEND_ERROR: 'bufferAppendError',
  1664. // 缓冲区追加错误事件的标识符-数据:追加错误描述
  1665. BUFFER_APPENDING_ERROR: 'bufferAppendingError',
  1666. //缓冲区暂停错误事件的标识符
  1667. BUFFER_STALLED_ERROR: 'bufferStalledError',
  1668. // 缓冲区暂停错误事件的标识符
  1669. BUFFER_FULL_ERROR: 'bufferFullError',
  1670. // 缓冲区查找孔事件的标识符
  1671. BUFFER_SEEK_OVER_HOLE: 'bufferSeekOverHole',
  1672. // 发生在内部的内部异常的标识符hls.js公司处理事件时
  1673. INTERNAL_EXCEPTION: 'internalException'
  1674. };
  1675. }, {}],
  1676. 8: [function(require, module, exports) {
  1677. 'use strict';
  1678. Object.defineProperty(exports, "__esModule", {
  1679. value: true
  1680. });
  1681. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
  1682. return typeof obj;
  1683. } : function(obj) {
  1684. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  1685. };
  1686. var _createClass = function() {
  1687. function defineProperties(target, props) {
  1688. for(var i = 0; i < props.length; i++) {
  1689. var descriptor = props[i];
  1690. descriptor.enumerable = descriptor.enumerable || false;
  1691. descriptor.configurable = true;
  1692. if("value" in descriptor) descriptor.writable = true;
  1693. Object.defineProperty(target, descriptor.key, descriptor);
  1694. }
  1695. }
  1696. return function(Constructor, protoProps, staticProps) {
  1697. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  1698. if(staticProps) defineProperties(Constructor, staticProps);
  1699. return Constructor;
  1700. };
  1701. }();
  1702. /*
  1703. *
  1704. *事件处理链中的所有对象都应继承自该类 class 定义
  1705. *
  1706. */
  1707. var _events = require('./events');
  1708. var _events2 = _interopRequireDefault(_events);
  1709. function _interopRequireDefault(obj) {
  1710. return obj && obj.__esModule ? obj : {
  1711. default: obj
  1712. };
  1713. }
  1714. function _classCallCheck(instance, Constructor) {
  1715. if(!(instance instanceof Constructor)) {
  1716. throw new TypeError("Cannot call a class as a function");
  1717. }
  1718. }
  1719. var EventHandler = function() {
  1720. function EventHandler(wfs) {
  1721. _classCallCheck(this, EventHandler);
  1722. this.wfs = wfs;
  1723. this.onEvent = this.onEvent.bind(this);
  1724. for(var _len = arguments.length, events = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  1725. events[_key - 1] = arguments[_key];
  1726. }
  1727. this.handledEvents = events;
  1728. this.useGenericHandler = true;
  1729. this.registerListeners();
  1730. }
  1731. _createClass(EventHandler, [{
  1732. key: 'destroy',
  1733. value: function destroy() {
  1734. this.unregisterListeners();
  1735. }
  1736. }, {
  1737. key: 'isEventHandler',
  1738. value: function isEventHandler() {
  1739. return _typeof(this.handledEvents) === 'object' && this.handledEvents.length && typeof this.onEvent === 'function';
  1740. }
  1741. }, {
  1742. key: 'registerListeners',
  1743. value: function registerListeners() {
  1744. if(this.isEventHandler()) {
  1745. this.handledEvents.forEach(function(event) {
  1746. if(event === 'wfsEventGeneric') {
  1747. //throw new Error('Forbidden event name: ' + event);
  1748. }
  1749. this.wfs.on(event, this.onEvent);
  1750. }.bind(this));
  1751. }
  1752. }
  1753. }, {
  1754. key: 'unregisterListeners',
  1755. value: function unregisterListeners() {
  1756. if(this.isEventHandler()) {
  1757. this.handledEvents.forEach(function(event) {
  1758. this.wfs.off(event, this.onEvent);
  1759. }.bind(this));
  1760. }
  1761. }
  1762. /**
  1763. *参数:事件(字符串)、数据(任意) arguments: event (string), data (any) huangfei
  1764. */
  1765. }, {
  1766. key: 'onEvent',
  1767. value: function onEvent(event, data) {
  1768. this.onEventGeneric(event, data);
  1769. }
  1770. }, {
  1771. key: 'onEventGeneric',
  1772. value: function onEventGeneric(event, data) {
  1773. var eventToFunction = function eventToFunction(event, data) {
  1774. var funcName = 'on' + event.replace('wfs', '');
  1775. if(typeof this[funcName] !== 'function') {
  1776. //throw new Error(`Event ${event} has no generic handler in this ${this.constructor.name} class (tried ${funcName})`);
  1777. }
  1778. return this[funcName].bind(this, data);
  1779. };
  1780. try {
  1781. eventToFunction.call(this, event, data).call();
  1782. } catch(err) {
  1783. console.log('internal error happened while processing ' + event + ':' + err.message);
  1784. // this.hls.trigger(Event.ERROR, {type: ErrorTypes.OTHER_ERROR, details: ErrorDetails.INTERNAL_EXCEPTION, fatal: false, event : event, err : err});
  1785. }
  1786. }
  1787. }]);
  1788. return EventHandler;
  1789. }();
  1790. exports.default = EventHandler;
  1791. }, {
  1792. "./events": 9
  1793. }],
  1794. 9: [function(require, module, exports) {
  1795. 'use strict';
  1796. module.exports = {
  1797. MEDIA_ATTACHING: 'wfsMediaAttaching',
  1798. MEDIA_ATTACHED: 'wfsMediaAttached',
  1799. FRAG_LOADING: 'wfsFragLoading',
  1800. BUFFER_CREATED: 'wfsBufferCreated',
  1801. BUFFER_APPENDING: 'wfsBufferAppending',
  1802. BUFFER_RESET: 'wfsBufferReset',
  1803. FRAG_PARSING_DATA: 'wfsFragParsingData',
  1804. FRAG_PARSING_INIT_SEGMENT: 'wfsFragParsingInitSegment',
  1805. //------------------------------------------
  1806. H264_DATA_PARSING: 'wfsH264DataParsing',
  1807. //------------------------------------------
  1808. WEBSOCKET_ATTACHED: 'wfsWebsocketAttached',
  1809. WEBSOCKET_ATTACHING: 'wfsWebsocketAttaching',
  1810. WEBSOCKET_DATA_UPLOADING: 'wfsWebsocketDataUploading',
  1811. WEBSOCKET_MESSAGE_SENDING: 'wfsWebsocketMessageSending',
  1812. //------------------------------------------
  1813. FILE_HEAD_LOADING: 'wfsFileHeadLoading',
  1814. FILE_HEAD_LOADED: 'wfsFileHeadLoaded',
  1815. FILE_DATA_LOADING: 'wfsFileDataLoading',
  1816. FILE_DATA_LOADED: 'wfsFileDataLoaded',
  1817. FILE_PARSING_DATA: 'wfsFileParsingData'
  1818. //------------------------------------------
  1819. };
  1820. }, {}],
  1821. 10: [function(require, module, exports) {
  1822. "use strict";
  1823. Object.defineProperty(exports, "__esModule", {
  1824. value: true
  1825. });
  1826. var _createClass = function() {
  1827. function defineProperties(target, props) {
  1828. for(var i = 0; i < props.length; i++) {
  1829. var descriptor = props[i];
  1830. descriptor.enumerable = descriptor.enumerable || false;
  1831. descriptor.configurable = true;
  1832. if("value" in descriptor) descriptor.writable = true;
  1833. Object.defineProperty(target, descriptor.key, descriptor);
  1834. }
  1835. }
  1836. return function(Constructor, protoProps, staticProps) {
  1837. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  1838. if(staticProps) defineProperties(Constructor, staticProps);
  1839. return Constructor;
  1840. };
  1841. }();
  1842. function _classCallCheck(instance, Constructor) {
  1843. if(!(instance instanceof Constructor)) {
  1844. throw new TypeError("Cannot call a class as a function");
  1845. }
  1846. }
  1847. /**
  1848. * AAC helper
  1849. */
  1850. var AAC = function() {
  1851. function AAC() {
  1852. _classCallCheck(this, AAC);
  1853. }
  1854. _createClass(AAC, null, [{
  1855. key: "getSilentFrame",
  1856. value: function getSilentFrame(channelCount) {
  1857. if(channelCount === 1) {
  1858. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x23, 0x80]);
  1859. } else if(channelCount === 2) {
  1860. return new Uint8Array([0x21, 0x00, 0x49, 0x90, 0x02, 0x19, 0x00, 0x23, 0x80]);
  1861. } else if(channelCount === 3) {
  1862. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x8e]);
  1863. } else if(channelCount === 4) {
  1864. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x80, 0x2c, 0x80, 0x08, 0x02, 0x38]);
  1865. } else if(channelCount === 5) {
  1866. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x38]);
  1867. } else if(channelCount === 6) {
  1868. return new Uint8Array([0x00, 0xc8, 0x00, 0x80, 0x20, 0x84, 0x01, 0x26, 0x40, 0x08, 0x64, 0x00, 0x82, 0x30, 0x04, 0x99, 0x00, 0x21, 0x90, 0x02, 0x00, 0xb2, 0x00, 0x20, 0x08, 0xe0]);
  1869. }
  1870. return null;
  1871. }
  1872. }]);
  1873. return AAC;
  1874. }();
  1875. exports.default = AAC;
  1876. }, {}],
  1877. 11: [function(require, module, exports) {
  1878. 'use strict';
  1879. //这主要是为了支持es6模块导出
  1880. //像我们在node/commonjs中习惯的那样导出函数
  1881. module.exports = require('./wfs.js').default;
  1882. }, {
  1883. "./wfs.js": 20
  1884. }],
  1885. 12: [function(require, module, exports) {
  1886. (function(process, global) {
  1887. 'use strict';
  1888. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
  1889. return typeof obj;
  1890. } : function(obj) {
  1891. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  1892. };
  1893. // jslint bitwise: true
  1894. (function() {
  1895. 'use strict';
  1896. var root = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' ? window : {};
  1897. var NODE_JS = !root.JS_CRC_NO_NODE_JS && (typeof process === 'undefined' ? 'undefined' : _typeof(process)) === 'object' && process.versions && process.versions.node;
  1898. if(NODE_JS) {
  1899. root = global;
  1900. }
  1901. var COMMON_JS = !root.JS_CRC_NO_COMMON_JS && (typeof module === 'undefined' ? 'undefined' : _typeof(module)) === 'object' && module.exports;
  1902. var AMD = typeof define === 'function' && define.amd;
  1903. var ARRAY_BUFFER = !root.JS_CRC_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
  1904. var HEX_CHARS = '0123456789abcdef'.split('');
  1905. var Modules = [{
  1906. name: 'crc32',
  1907. polynom: 0xEDB88320,
  1908. initValue: -1,
  1909. bytes: 4
  1910. }, {
  1911. name: 'crc16',
  1912. polynom: 0xA001,
  1913. initValue: 0,
  1914. bytes: 2
  1915. }];
  1916. var i, j, k, b;
  1917. for(i = 0; i < Modules.length; ++i) {
  1918. var m = Modules[i];
  1919. m.method = function(m) {
  1920. return function(message) {
  1921. return crc(message, m);
  1922. };
  1923. }(m);
  1924. m.table = [];
  1925. for(j = 0; j < 256; ++j) {
  1926. b = j;
  1927. for(k = 0; k < 8; ++k) {
  1928. b = b & 1 ? m.polynom ^ b >>> 1 : b >>> 1;
  1929. }
  1930. m.table[j] = b >>> 0;
  1931. }
  1932. }
  1933. var crc = function crc(message, module) {
  1934. var notString = typeof message !== 'string';
  1935. if(notString && ARRAY_BUFFER && message instanceof ArrayBuffer) {
  1936. message = new Uint8Array(message);
  1937. }
  1938. var crc = module.initValue,
  1939. code,
  1940. i,
  1941. length = message.length,
  1942. table = module.table;
  1943. if(notString) {
  1944. for(i = 0; i < length; ++i) {
  1945. crc = table[(crc ^ message[i]) & 0xFF] ^ crc >>> 8;
  1946. }
  1947. } else {
  1948. // 解码计算 小程序和云游戏 0001 二进制 TDDE ??????
  1949. for(i = 0; i < length; ++i) {
  1950. code = message.charCodeAt(i);
  1951. if(code < 0x80) {
  1952. crc = table[(crc ^ code) & 0xFF] ^ crc >>> 8;
  1953. } else if(code < 0x800) {
  1954. crc = table[(crc ^ (0xc0 | code >> 6)) & 0xFF] ^ crc >>> 8;
  1955. crc = table[(crc ^ (0x80 | code & 0x3f)) & 0xFF] ^ crc >>> 8;
  1956. } else if(code < 0xd800 || code >= 0xe000) {
  1957. crc = table[(crc ^ (0xe0 | code >> 12)) & 0xFF] ^ crc >>> 8;
  1958. crc = table[(crc ^ (0x80 | code >> 6 & 0x3f)) & 0xFF] ^ crc >>> 8;
  1959. crc = table[(crc ^ (0x80 | code & 0x3f)) & 0xFF] ^ crc >>> 8;
  1960. } else {
  1961. code = 0x10000 + ((code & 0x3ff) << 10 | message.charCodeAt(++i) & 0x3ff);
  1962. crc = table[(crc ^ (0xf0 | code >> 18)) & 0xFF] ^ crc >>> 8;
  1963. crc = table[(crc ^ (0x80 | code >> 12 & 0x3f)) & 0xFF] ^ crc >>> 8;
  1964. crc = table[(crc ^ (0x80 | code >> 6 & 0x3f)) & 0xFF] ^ crc >>> 8;
  1965. crc = table[(crc ^ (0x80 | code & 0x3f)) & 0xFF] ^ crc >>> 8;
  1966. }
  1967. }
  1968. }
  1969. crc ^= module.initValue;
  1970. var hex = '';
  1971. if(module.bytes > 2) {
  1972. hex += HEX_CHARS[crc >> 28 & 0x0F] + HEX_CHARS[crc >> 24 & 0x0F] + HEX_CHARS[crc >> 20 & 0x0F] + HEX_CHARS[crc >> 16 & 0x0F];
  1973. }
  1974. hex += HEX_CHARS[crc >> 12 & 0x0F] + HEX_CHARS[crc >> 8 & 0x0F] + HEX_CHARS[crc >> 4 & 0x0F] + HEX_CHARS[crc & 0x0F];
  1975. return hex;
  1976. };
  1977. var exports = {};
  1978. for(i = 0; i < Modules.length; ++i) {
  1979. var m = Modules[i];
  1980. exports[m.name] = m.method;
  1981. }
  1982. if(COMMON_JS) {
  1983. module.exports = exports;
  1984. } else {
  1985. for(i = 0; i < Modules.length; ++i) {
  1986. var m = Modules[i];
  1987. root[m.name] = m.method;
  1988. }
  1989. if(AMD) {
  1990. define(function() {
  1991. return exports;
  1992. });
  1993. }
  1994. }
  1995. })();
  1996. }).call(this, require('_process'), typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1997. }, {
  1998. "_process": 2
  1999. }],
  2000. 13: [function(require, module, exports) {
  2001. 'use strict';
  2002. Object.defineProperty(exports, "__esModule", {
  2003. value: true
  2004. });
  2005. var _createClass = function() {
  2006. function defineProperties(target, props) {
  2007. for(var i = 0; i < props.length; i++) {
  2008. var descriptor = props[i];
  2009. descriptor.enumerable = descriptor.enumerable || false;
  2010. descriptor.configurable = true;
  2011. if("value" in descriptor) descriptor.writable = true;
  2012. Object.defineProperty(target, descriptor.key, descriptor);
  2013. }
  2014. }
  2015. return function(Constructor, protoProps, staticProps) {
  2016. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  2017. if(staticProps) defineProperties(Constructor, staticProps);
  2018. return Constructor;
  2019. };
  2020. }();
  2021. var _events = require('../events');
  2022. var _events2 = _interopRequireDefault(_events);
  2023. var _eventHandler = require('../event-handler');
  2024. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  2025. function _interopRequireDefault(obj) {
  2026. return obj && obj.__esModule ? obj : {
  2027. default: obj
  2028. };
  2029. }
  2030. function _classCallCheck(instance, Constructor) {
  2031. if(!(instance instanceof Constructor)) {
  2032. throw new TypeError("Cannot call a class as a function");
  2033. }
  2034. }
  2035. function _possibleConstructorReturn(self, call) {
  2036. if(!self) {
  2037. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  2038. }
  2039. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  2040. }
  2041. function _inherits(subClass, superClass) {
  2042. if(typeof superClass !== "function" && superClass !== null) {
  2043. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  2044. }
  2045. subClass.prototype = Object.create(superClass && superClass.prototype, {
  2046. constructor: {
  2047. value: subClass,
  2048. enumerable: false,
  2049. writable: true,
  2050. configurable: true
  2051. }
  2052. });
  2053. if(superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  2054. }
  2055. /*
  2056. * 文件加载器 Loader
  2057. */
  2058. var FileLoader = function(_EventHandler) {
  2059. _inherits(FileLoader, _EventHandler);
  2060. function FileLoader(wfs) {
  2061. _classCallCheck(this, FileLoader);
  2062. var _this = _possibleConstructorReturn(this, (FileLoader.__proto__ || Object.getPrototypeOf(FileLoader)).call(this, wfs, _events2.default.FRAG_LOADING, _events2.default.FILE_HEAD_LOADING, _events2.default.FILE_DATA_LOADING));
  2063. _this.loaders = {};
  2064. return _this;
  2065. }
  2066. _createClass(FileLoader, [{
  2067. key: 'destroy',
  2068. value: function destroy() {
  2069. for(var loaderName in this.loaders) {
  2070. var loader = this.loaders[loaderName];
  2071. if(loader) {
  2072. loader.destroy();
  2073. }
  2074. }
  2075. this.loaders = {};
  2076. _eventHandler2.default.prototype.destroy.call(this);
  2077. }
  2078. }, {
  2079. key: 'onFileHeadLoading',
  2080. value: function onFileHeadLoading(data) {
  2081. var config = this.wfs.config;
  2082. var loader = new config.loader(config);
  2083. var loaderContext = void 0,
  2084. loaderConfig = void 0,
  2085. loaderCallbacks = void 0;
  2086. loaderContext = {
  2087. url: config.fmp4FileUrl
  2088. };
  2089. loaderConfig = {
  2090. maxRetry: 0,
  2091. retryDelay: 0
  2092. };
  2093. loaderCallbacks = {
  2094. onSuccess: this.fileloadheadsuccess.bind(this)
  2095. };
  2096. loader.loadHead(loaderContext, loaderConfig, loaderCallbacks);
  2097. }
  2098. }, {
  2099. key: 'fileloadheadsuccess',
  2100. value: function fileloadheadsuccess(response) {
  2101. this.wfs.trigger(_events2.default.FILE_HEAD_LOADED, {
  2102. size: response
  2103. });
  2104. }
  2105. }, {
  2106. key: 'onFileDataLoading',
  2107. value: function onFileDataLoading(data) {
  2108. var config = this.wfs.config;
  2109. var loader = new config.loader(config);
  2110. var loaderContext = void 0,
  2111. loaderConfig = void 0,
  2112. loaderCallbacks = void 0;
  2113. loaderContext = {
  2114. url: config.fmp4FileUrl,
  2115. responseType: 'arraybuffer',
  2116. progressData: false
  2117. };
  2118. var start = data.fileStart,
  2119. end = data.fileEnd;
  2120. if(!isNaN(start) && !isNaN(end)) {
  2121. loaderContext.rangeStart = start;
  2122. loaderContext.rangeEnd = end;
  2123. }
  2124. loaderConfig = {
  2125. timeout: config.fragLoadingTimeOut,
  2126. maxRetry: 0,
  2127. retryDelay: 0,
  2128. maxRetryDelay: config.fragLoadingMaxRetryTimeout
  2129. };
  2130. loaderCallbacks = {
  2131. onSuccess: this.fileloaddatasuccess.bind(this)
  2132. };
  2133. loader.load(loaderContext, loaderConfig, loaderCallbacks);
  2134. }
  2135. }, {
  2136. key: 'fileloaddatasuccess',
  2137. value: function fileloaddatasuccess(response, stats, context) {
  2138. this.wfs.trigger(_events2.default.FILE_DATA_LOADED, {
  2139. payload: response.data,
  2140. stats: stats
  2141. });
  2142. }
  2143. }, {
  2144. key: 'loaderror',
  2145. value: function loaderror(response, context) {
  2146. var loader = context.loader;
  2147. if(loader) {
  2148. loader.abort();
  2149. }
  2150. this.loaders[context.type] = undefined;
  2151. }
  2152. }, {
  2153. key: 'loadtimeout',
  2154. value: function loadtimeout(stats, context) {
  2155. var loader = context.loader;
  2156. if(loader) {
  2157. loader.abort();
  2158. }
  2159. this.loaders[context.type] = undefined;
  2160. }
  2161. }, {
  2162. key: 'loadprogress',
  2163. value: function loadprogress(stats, context, data) {
  2164. var frag = context.frag;
  2165. frag.loaded = stats.loaded;
  2166. }
  2167. }]);
  2168. return FileLoader;
  2169. }(_eventHandler2.default);
  2170. exports.default = FileLoader;
  2171. }, {
  2172. "../event-handler": 8,
  2173. "../events": 9
  2174. }],
  2175. 14: [function(require, module, exports) {
  2176. 'use strict';
  2177. Object.defineProperty(exports, "__esModule", {
  2178. value: true
  2179. });
  2180. var _createClass = function() {
  2181. function defineProperties(target, props) {
  2182. for(var i = 0; i < props.length; i++) {
  2183. var descriptor = props[i];
  2184. descriptor.enumerable = descriptor.enumerable || false;
  2185. descriptor.configurable = true;
  2186. if("value" in descriptor) descriptor.writable = true;
  2187. Object.defineProperty(target, descriptor.key, descriptor);
  2188. }
  2189. }
  2190. return function(Constructor, protoProps, staticProps) {
  2191. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  2192. if(staticProps) defineProperties(Constructor, staticProps);
  2193. return Constructor;
  2194. };
  2195. }();
  2196. var _events = require('../events');
  2197. var _events2 = _interopRequireDefault(_events);
  2198. var _eventHandler = require('../event-handler');
  2199. var _eventHandler2 = _interopRequireDefault(_eventHandler);
  2200. var _h264Demuxer = require('../demux/h264-demuxer');
  2201. var _h264Demuxer2 = _interopRequireDefault(_h264Demuxer);
  2202. var _crc = require('./crc.js');
  2203. var _crc2 = _interopRequireDefault(_crc);
  2204. function _interopRequireDefault(obj) {
  2205. return obj && obj.__esModule ? obj : {
  2206. default: obj
  2207. };
  2208. }
  2209. function _classCallCheck(instance, Constructor) {
  2210. if(!(instance instanceof Constructor)) {
  2211. throw new TypeError("Cannot call a class as a function");
  2212. }
  2213. }
  2214. function _possibleConstructorReturn(self, call) {
  2215. if(!self) {
  2216. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  2217. }
  2218. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  2219. }
  2220. function _inherits(subClass, superClass) {
  2221. if(typeof superClass !== "function" && superClass !== null) {
  2222. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  2223. }
  2224. subClass.prototype = Object.create(superClass && superClass.prototype, {
  2225. constructor: {
  2226. value: subClass,
  2227. enumerable: false,
  2228. writable: true,
  2229. configurable: true
  2230. }
  2231. });
  2232. if(superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  2233. }
  2234. /*
  2235. * Websocket Loader ws加载 根据后端需要的对应参数
  2236. */
  2237. var WebsocketLoader = function(_EventHandler) {
  2238. _inherits(WebsocketLoader, _EventHandler);
  2239. function WebsocketLoader(wfs) {
  2240. _classCallCheck(this, WebsocketLoader);
  2241. var _this = _possibleConstructorReturn(this, (WebsocketLoader.__proto__ || Object.getPrototypeOf(WebsocketLoader)).call(this, wfs, _events2.default.WEBSOCKET_ATTACHING, _events2.default.WEBSOCKET_DATA_UPLOADING, _events2.default.WEBSOCKET_MESSAGE_SENDING));
  2242. _this.buf = null;
  2243. _this.h264Demuxer = new _h264Demuxer2.default(wfs);
  2244. _this.mediaType = undefined;
  2245. _this.channelName = undefined;
  2246. return _this;
  2247. }
  2248. _createClass(WebsocketLoader, [{
  2249. key: 'destroy',
  2250. value: function destroy() {
  2251. _eventHandler2.default.prototype.destroy.call(this);
  2252. }
  2253. }, {
  2254. key: 'onWebsocketAttaching',
  2255. value: function onWebsocketAttaching(data) {
  2256. this.mediaType = data.mediaType;
  2257. this.channelName = data.channelName;
  2258. if(data.websocket instanceof WebSocket) {
  2259. this.client = data.websocket;
  2260. this.client.onopen = this.initSocketClient.bind(this);
  2261. this.client.onclose = function(e) {
  2262. console.log('Websocket Disconnected!');
  2263. };
  2264. }
  2265. }
  2266. }, {
  2267. key: 'initSocketClient',
  2268. value: function initSocketClient(client) {
  2269. this.client.binaryType = 'arraybuffer';
  2270. this.client.onmessage = this.receiveSocketMessage.bind(this);
  2271. }
  2272. }, {
  2273. key: 'receiveSocketMessage',
  2274. value: function receiveSocketMessage(event) {
  2275. this.buf = new Uint8Array(event.data);
  2276. var copy = new Uint8Array(this.buf);
  2277. // console.log(">>>>黄飞",this.mediaType)
  2278. if(this.buf[0] == 0) {
  2279. if(this.mediaType === 'FMp4') {
  2280. this.wfs.trigger(_events2.default.WEBSOCKET_ATTACHED, {
  2281. payload: copy
  2282. });
  2283. }
  2284. this.wfs.trigger(_events2.default.H264_DATA_PARSING, {
  2285. data: copy
  2286. });
  2287. }
  2288. }
  2289. }, {
  2290. key: 'onWebsocketDataUploading',
  2291. value: function onWebsocketDataUploading(event) {
  2292. this.client.send(event.data);
  2293. }
  2294. }, {
  2295. key: 'onWebsocketMessageSending',
  2296. value: function onWebsocketMessageSending(event) {
  2297. this.client.send(JSON.stringify({
  2298. t: event.commandType,
  2299. c: event.channelName,
  2300. v: event.commandValue
  2301. }));
  2302. }
  2303. }]);
  2304. return WebsocketLoader;
  2305. }(_eventHandler2.default);
  2306. exports.default = WebsocketLoader;
  2307. }, {
  2308. "../demux/h264-demuxer": 6,
  2309. "../event-handler": 8,
  2310. "../events": 9,
  2311. "./crc.js": 12
  2312. }],
  2313. 15: [function(require, module, exports) {
  2314. 'use strict';
  2315. Object.defineProperty(exports, "__esModule", {
  2316. value: true
  2317. });
  2318. var _createClass = function() {
  2319. function defineProperties(target, props) {
  2320. for(var i = 0; i < props.length; i++) {
  2321. var descriptor = props[i];
  2322. descriptor.enumerable = descriptor.enumerable || false;
  2323. descriptor.configurable = true;
  2324. if("value" in descriptor) descriptor.writable = true;
  2325. Object.defineProperty(target, descriptor.key, descriptor);
  2326. }
  2327. }
  2328. return function(Constructor, protoProps, staticProps) {
  2329. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  2330. if(staticProps) defineProperties(Constructor, staticProps);
  2331. return Constructor;
  2332. };
  2333. }();
  2334. function _classCallCheck(instance, Constructor) {
  2335. if(!(instance instanceof Constructor)) {
  2336. throw new TypeError("Cannot call a class as a function");
  2337. }
  2338. }
  2339. /**
  2340. * 生成MP4框 HDDT 没有用
  2341. */
  2342. //import Hex from '../utils/hex';
  2343. var MP4 = function() {
  2344. function MP4() {
  2345. _classCallCheck(this, MP4);
  2346. }
  2347. _createClass(MP4, null, [{
  2348. key: 'init',
  2349. value: function init() {
  2350. MP4.types = {
  2351. avc1: [], // codingname
  2352. avcC: [],
  2353. btrt: [],
  2354. dinf: [],
  2355. dref: [],
  2356. esds: [],
  2357. ftyp: [],
  2358. hdlr: [],
  2359. mdat: [],
  2360. mdhd: [],
  2361. mdia: [],
  2362. mfhd: [],
  2363. minf: [],
  2364. moof: [],
  2365. moov: [],
  2366. mp4a: [],
  2367. mvex: [],
  2368. mvhd: [],
  2369. sdtp: [],
  2370. stbl: [],
  2371. stco: [],
  2372. stsc: [],
  2373. stsd: [],
  2374. stsz: [],
  2375. stts: [],
  2376. tfdt: [],
  2377. tfhd: [],
  2378. traf: [],
  2379. trak: [],
  2380. trun: [],
  2381. trex: [],
  2382. tkhd: [],
  2383. vmhd: [],
  2384. smhd: []
  2385. };
  2386. var i;
  2387. for(i in MP4.types) {
  2388. if(MP4.types.hasOwnProperty(i)) {
  2389. MP4.types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
  2390. }
  2391. }
  2392. // 为了安卓机不卡和延时的处理 对应的注释 是为了看清类型
  2393. var videoHdlr = new Uint8Array([0x00, // version 0
  2394. 0x00, 0x00, 0x00, // flags
  2395. 0x00, 0x00, 0x00, 0x00, // pre_defined
  2396. 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
  2397. 0x00, 0x00, 0x00, 0x00, // reserved
  2398. 0x00, 0x00, 0x00, 0x00, // reserved
  2399. 0x00, 0x00, 0x00, 0x00, // reserved
  2400. 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
  2401. ]);
  2402. var audioHdlr = new Uint8Array([0x00, // version 0
  2403. 0x00, 0x00, 0x00, // flags
  2404. 0x00, 0x00, 0x00, 0x00, // pre_defined
  2405. 0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
  2406. 0x00, 0x00, 0x00, 0x00, // reserved
  2407. 0x00, 0x00, 0x00, 0x00, // reserved
  2408. 0x00, 0x00, 0x00, 0x00, // reserved
  2409. 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
  2410. ]);
  2411. MP4.HDLR_TYPES = {
  2412. 'video': videoHdlr,
  2413. 'audio': audioHdlr
  2414. };
  2415. var dref = new Uint8Array([0x00, // version 0
  2416. 0x00, 0x00, 0x00, // flags
  2417. 0x00, 0x00, 0x00, 0x01, // entry_count
  2418. 0x00, 0x00, 0x00, 0x0c, // entry_size
  2419. 0x75, 0x72, 0x6c, 0x20, // 'url' type
  2420. 0x00, // version 0
  2421. 0x00, 0x00, 0x01 // entry_flags
  2422. ]);
  2423. var stco = new Uint8Array([0x00, // version
  2424. 0x00, 0x00, 0x00, // flags
  2425. 0x00, 0x00, 0x00, 0x00 // entry_count
  2426. ]);
  2427. MP4.STTS = MP4.STSC = MP4.STCO = stco;
  2428. MP4.STSZ = new Uint8Array([0x00, // version
  2429. 0x00, 0x00, 0x00, // flags
  2430. 0x00, 0x00, 0x00, 0x00, // sample_size
  2431. 0x00, 0x00, 0x00, 0x00
  2432. ]);
  2433. MP4.VMHD = new Uint8Array([0x00, // version
  2434. 0x00, 0x00, 0x01, // flags
  2435. 0x00, 0x00, // graphicsmode
  2436. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
  2437. ]);
  2438. MP4.SMHD = new Uint8Array([0x00, // version
  2439. 0x00, 0x00, 0x00, // flags
  2440. 0x00, 0x00, // balance
  2441. 0x00, 0x00 // reserved
  2442. ]);
  2443. MP4.STSD = new Uint8Array([0x00, // version 0
  2444. 0x00, 0x00, 0x00, // flags
  2445. 0x00, 0x00, 0x00, 0x01
  2446. ]); // entry_count
  2447. var majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
  2448. var avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1 这个解码方案 是为了可以支持 高等画质 high 云游戏
  2449. var minorVersion = new Uint8Array([0, 0, 0, 1]);
  2450. MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand);
  2451. MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
  2452. }
  2453. }, {
  2454. key: 'box',
  2455. value: function box(type) {
  2456. var payload = Array.prototype.slice.call(arguments, 1), //box 是分配帧数
  2457. size = 8,
  2458. i = payload.length,
  2459. len = i,
  2460. result;
  2461. // c计算我们需要分配的总大小
  2462. while(i--) {
  2463. size += payload[i].byteLength;
  2464. }
  2465. result = new Uint8Array(size);
  2466. result[0] = size >> 24 & 0xff;
  2467. result[1] = size >> 16 & 0xff;
  2468. result[2] = size >> 8 & 0xff;
  2469. result[3] = size & 0xff;
  2470. result.set(type, 4);
  2471. // 将有效负载复制到结果中
  2472. for(i = 0, size = 8; i < len; i++) {
  2473. // 复制负载[i]数组@offset size
  2474. result.set(payload[i], size);
  2475. size += payload[i].byteLength;
  2476. }
  2477. return result;
  2478. }
  2479. }, {
  2480. key: 'hdlr',
  2481. value: function hdlr(type) {
  2482. return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
  2483. }
  2484. }, {
  2485. key: 'mdat',
  2486. value: function mdat(data) {
  2487. // console.log( "mdat==> ",data.length );
  2488. return MP4.box(MP4.types.mdat, data);
  2489. }
  2490. }, {
  2491. key: 'mdhd',
  2492. value: function mdhd(timescale, duration) {
  2493. duration *= timescale;
  2494. return MP4.box(MP4.types.mdhd, new Uint8Array([0x00, // version 0
  2495. 0x00, 0x00, 0x00, // flags
  2496. 0x00, 0x00, 0x00, 0x02, // creation_time 创作时间
  2497. 0x00, 0x00, 0x00, 0x03, // modification_time 修改时间
  2498. timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF, // timescale
  2499. duration >> 24, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
  2500. 0x55, 0xc4, // 'und' language (undetermined)
  2501. 0x00, 0x00
  2502. ]));
  2503. }
  2504. }, {
  2505. key: 'mdia',
  2506. value: function mdia(track) {
  2507. return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track));
  2508. }
  2509. }, {
  2510. key: 'mfhd',
  2511. value: function mfhd(sequenceNumber) {
  2512. return MP4.box(MP4.types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // flags
  2513. sequenceNumber >> 24, sequenceNumber >> 16 & 0xFF, sequenceNumber >> 8 & 0xFF, sequenceNumber & 0xFF
  2514. ]) // sequence_number 序号
  2515. );
  2516. }
  2517. }, {
  2518. key: 'minf',
  2519. value: function minf(track) {
  2520. if(track.type === 'audio') {
  2521. return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track));
  2522. } else {
  2523. return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
  2524. }
  2525. }
  2526. }, {
  2527. key: 'moof',
  2528. value: function moof(sn, baseMediaDecodeTime, track) {
  2529. return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
  2530. }
  2531. /**
  2532. * @param tracks... (optional) {array} the tracks associated with this movie
  2533. */
  2534. }, {
  2535. key: 'moov',
  2536. value: function moov(tracks) {
  2537. var i = tracks.length,
  2538. boxes = [];
  2539. while(i--) {
  2540. boxes[i] = MP4.trak(tracks[i]);
  2541. }
  2542. return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(tracks[0].timescale, tracks[0].duration)].concat(boxes).concat(MP4.mvex(tracks)));
  2543. }
  2544. }, {
  2545. key: 'mvex',
  2546. value: function mvex(tracks) {
  2547. var i = tracks.length,
  2548. boxes = [];
  2549. while(i--) {
  2550. boxes[i] = MP4.trex(tracks[i]);
  2551. }
  2552. return MP4.box.apply(null, [MP4.types.mvex].concat(boxes));
  2553. }
  2554. }, {
  2555. key: 'mvhd',
  2556. value: function mvhd(timescale, duration) {
  2557. //duration*=timescale;
  2558. duration = 0;
  2559. var bytes = new Uint8Array([0x00, // version 0
  2560. 0x00, 0x00, 0x00, // flags
  2561. 0x00, 0x00, 0x00, 0x01, // creation_time
  2562. 0x00, 0x00, 0x00, 0x02, // modification_time
  2563. timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF, // timescale
  2564. duration >> 24 & 0xFF, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
  2565. 0x00, 0x01, 0x00, 0x00, // 1.0 rate
  2566. 0x01, 0x00, // 1.0 volume
  2567. 0x00, 0x00, // reserved
  2568. 0x00, 0x00, 0x00, 0x00, // reserved
  2569. 0x00, 0x00, 0x00, 0x00, // reserved
  2570. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
  2571. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  2572. 0xff, 0xff, 0xff, 0xff // next_track_ID
  2573. ]);
  2574. return MP4.box(MP4.types.mvhd, bytes);
  2575. }
  2576. }, {
  2577. key: 'sdtp',
  2578. value: function sdtp(track) {
  2579. var samples = track.samples || [],
  2580. bytes = new Uint8Array(4 + samples.length),
  2581. flags,
  2582. i;
  2583. // 将整框标题(4字节)全部保留为零 》》》 比如是这样的 可以打印(4 bytes)
  2584. // 编写示例表
  2585. for(i = 0; i < samples.length; i++) {
  2586. flags = samples[i].flags;
  2587. bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
  2588. }
  2589. return MP4.box(MP4.types.sdtp, bytes);
  2590. }
  2591. }, {
  2592. key: 'stbl',
  2593. value: function stbl(track) {
  2594. return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
  2595. }
  2596. }, {
  2597. key: 'avc1',
  2598. value: function avc1(track) {
  2599. var sps = [],
  2600. pps = [],
  2601. i,
  2602. data,
  2603. len;
  2604. // 组装SPSs
  2605. for(i = 0; i < track.sps.length; i++) {
  2606. data = track.sps[i];
  2607. len = data.byteLength;
  2608. sps.push(len >>> 8 & 0xFF);
  2609. sps.push(len & 0xFF);
  2610. sps = sps.concat(Array.prototype.slice.call(data)); // SPS
  2611. }
  2612. // 装配PPS
  2613. for(i = 0; i < track.pps.length; i++) {
  2614. data = track.pps[i];
  2615. len = data.byteLength;
  2616. pps.push(len >>> 8 & 0xFF);
  2617. pps.push(len & 0xFF);
  2618. pps = pps.concat(Array.prototype.slice.call(data));
  2619. }
  2620. var avcc = MP4.box(MP4.types.avcC, new Uint8Array([0x01, // version
  2621. sps[3], // profile
  2622. sps[4], // profile compat
  2623. sps[5], // level
  2624. 0xfc | 3, // 长度SizeMinusOne,硬编码为4字节 4 bytes
  2625. 0xE0 | track.sps.length // 3bit保留(111)+numofsequenceparameters集
  2626. ].concat(sps).concat([track.pps.length // 对应的数字 numOfPictureParameterSets
  2627. ]).concat(pps))),
  2628. // "PPS"
  2629. width = track.width,
  2630. height = track.height;
  2631. //console.log('avcc:' + Hex.hexDump(avcc));
  2632. return MP4.box(MP4.types.avc1, new Uint8Array([0x00, 0x00, 0x00, // reserved
  2633. 0x00, 0x00, 0x00, // reserved
  2634. 0x00, 0x01, // data_reference_index
  2635. 0x00, 0x00, // pre_defined
  2636. 0x00, 0x00, // reserved
  2637. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
  2638. width >> 8 & 0xFF, width & 0xff, // width
  2639. height >> 8 & 0xFF, height & 0xff, // height
  2640. 0x00, 0x48, 0x00, 0x00, // horizresolution
  2641. 0x00, 0x48, 0x00, 0x00, // vertresolution
  2642. 0x00, 0x00, 0x00, 0x00, // reserved
  2643. 0x00, 0x01, // frame_count
  2644. 0x12, 0x6a, 0x65, 0x66, 0x66,
  2645. 0x2d, 0x79, 0x61, 0x6e, 0x2f, 0x2f, 0x2f, 0x67, 0x77, 0x66, 0x73, 0x2E, 0x6A, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // compressorname
  2646. 0x00, 0x18, // depth = 24
  2647. 0x11, 0x11
  2648. ]), // pre_defined = -1
  2649. avcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
  2650. 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
  2651. 0x00, 0x2d, 0xc6, 0xc0
  2652. ])) // avgBitrate
  2653. );
  2654. }
  2655. }, {
  2656. key: 'esds',
  2657. value: function esds(track) {
  2658. var configlen = track.config.length;
  2659. return new Uint8Array([0x00, // version 0
  2660. 0x00, 0x00, 0x00, // flags
  2661. 0x03, // descriptor_type
  2662. 0x17 + configlen, // length
  2663. 0x00, 0x01, //es_id
  2664. 0x00, // stream_priority
  2665. 0x04, // descriptor_type
  2666. 0x0f + configlen, // length
  2667. 0x40, //codec : mpeg4_audio
  2668. 0x15, // stream_type
  2669. 0x00, 0x00, 0x00, // buffer_size
  2670. 0x00, 0x00, 0x00, 0x00, // maxBitrate
  2671. 0x00, 0x00, 0x00, 0x00, // avgBitrate
  2672. 0x05 // descriptor_type
  2673. ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor
  2674. }
  2675. }, {
  2676. key: 'mp4a',
  2677. value: function mp4a(track) {
  2678. var audiosamplerate = track.audiosamplerate;
  2679. // 下面的注释是 对应每个函数和对象
  2680. return MP4.box(MP4.types.mp4a, new Uint8Array([0x00, 0x00, 0x00, // reserved
  2681. 0x00, 0x00, 0x00, // reserved
  2682. 0x00, 0x01, // data_reference_index
  2683. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  2684. 0x00, track.channelCount, // channelcount
  2685. 0x00, 0x10, // sampleSize:16bits
  2686. 0x00, 0x00, 0x00, 0x00, // reserved2
  2687. audiosamplerate >> 8 & 0xFF, audiosamplerate & 0xff, //
  2688. 0x00, 0x00
  2689. ]), MP4.box(MP4.types.esds, MP4.esds(track)));
  2690. }
  2691. }, {
  2692. key: 'stsd',
  2693. value: function stsd(track) {
  2694. if(track.type === 'audio') {
  2695. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
  2696. } else {
  2697. return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
  2698. }
  2699. }
  2700. }, {
  2701. key: 'tkhd',
  2702. value: function tkhd(track) {
  2703. var id = track.id,
  2704. duration = track.duration * track.timescale,
  2705. width = track.width,
  2706. height = track.height;
  2707. // console.log( "tkhd==> ",track.id, track.duration, track.timescale, width,height );
  2708. // 计算width和height 可打印
  2709. return MP4.box(MP4.types.tkhd, new Uint8Array([0x00, // version 0
  2710. 0x00, 0x00, 0x07, // flags
  2711. 0x00, 0x00, 0x00, 0x00, // creation_time
  2712. 0x00, 0x00, 0x00, 0x00, // modification_time
  2713. id >> 24 & 0xFF, id >> 16 & 0xFF, id >> 8 & 0xFF, id & 0xFF, // track_ID
  2714. 0x00, 0x00, 0x00, 0x00, // reserved
  2715. duration >> 24, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
  2716. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
  2717. 0x00, 0x00, // layer
  2718. 0x00, 0x00, // alternate_group
  2719. 0x00, 0x00, // non-audio track volume
  2720. 0x00, 0x00, // reserved
  2721. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
  2722. width >> 8 & 0xFF, width & 0xFF, 0x00, 0x00, // width
  2723. height >> 8 & 0xFF, height & 0xFF, 0x00, 0x00 // height
  2724. ]));
  2725. }
  2726. }, {
  2727. key: 'traf',
  2728. value: function traf(track, baseMediaDecodeTime) {
  2729. var sampleDependencyTable = MP4.sdtp(track),
  2730. id = track.id;
  2731. // console.log( "traf==> ",id ,baseMediaDecodeTime);
  2732. // 每一帧的
  2733. return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([0x00, // version 0
  2734. 0x00, 0x00, 0x00, // flags
  2735. id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF
  2736. ]) // track_ID
  2737. ), MP4.box(MP4.types.tfdt, new Uint8Array([0x00, // version 0
  2738. 0x00, 0x00, 0x00, // flags
  2739. baseMediaDecodeTime >> 24, baseMediaDecodeTime >> 16 & 0XFF, baseMediaDecodeTime >> 8 & 0XFF, baseMediaDecodeTime & 0xFF
  2740. ]) // baseMediaDecodeTime
  2741. ), MP4.trun(track, sampleDependencyTable.length + 16 + // tfhd
  2742. 16 + // tfdt
  2743. 8 + // traf header
  2744. 16 + // mfhd
  2745. 8 + // moof header
  2746. 8), // mdat header
  2747. sampleDependencyTable);
  2748. }
  2749. }, {
  2750. key: 'trak',
  2751. value: function trak(track) {
  2752. track.duration = track.duration || 0xffffffff;
  2753. return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
  2754. }
  2755. }, {
  2756. key: 'trex',
  2757. value: function trex(track) {
  2758. var id = track.id;
  2759. return MP4.box(MP4.types.trex, new Uint8Array([0x00, // version 0
  2760. 0x00, 0x00, 0x00, // flags
  2761. id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF, // track_ID
  2762. 0x00, 0x00, 0x00, 0x01, //索引 default_sample_description_index
  2763. 0x00, 0x00, 0x00, 0x00, //持续时间 default_sample_duration
  2764. 0x00, 0x00, 0x00, 0x00, //大小 default_sample_size
  2765. 0x00, 0x01, 0x00, 0x01 // 帧数default_sample_flags
  2766. ]));
  2767. }
  2768. }, {
  2769. key: 'trun',
  2770. value: function trun(track, offset) {
  2771. var samples = track.samples || [],
  2772. len = samples.length,
  2773. arraylen = 12 + 16 * len,
  2774. array = new Uint8Array(arraylen),
  2775. i,
  2776. sample,
  2777. duration,
  2778. size,
  2779. flags,
  2780. cts;
  2781. //sample = samples[0];
  2782. // console.log( "trun==> ",sample.duration, sample.cts ,sample.size,len );
  2783. offset += 8 + arraylen;
  2784. array.set([0x00, // version 0
  2785. 0x00, 0x0f, 0x01, // flags
  2786. len >>> 24 & 0xFF, len >>> 16 & 0xFF, len >>> 8 & 0xFF, len & 0xFF, // sample_count
  2787. offset >>> 24 & 0xFF, offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF // data_offset
  2788. ], 0);
  2789. for(i = 0; i < len; i++) {
  2790. sample = samples[i];
  2791. duration = sample.duration;
  2792. size = sample.size;
  2793. flags = sample.flags;
  2794. cts = sample.cts;
  2795. array.set([duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, // sample_duration
  2796. size >>> 24 & 0xFF, size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF, // sample_size
  2797. flags.isLeading << 2 | flags.dependsOn, flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.paddingValue << 1 | flags.isNonSync, flags.degradPrio & 0xF0 << 8, flags.degradPrio & 0x0F, // sample_flags
  2798. cts >>> 24 & 0xFF, cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF // sample_composition_time_offset
  2799. ], 12 + 16 * i);
  2800. }
  2801. return MP4.box(MP4.types.trun, array);
  2802. }
  2803. }, {
  2804. key: 'initSegment',
  2805. value: function initSegment(tracks) {
  2806. if(!MP4.types) {
  2807. MP4.init();
  2808. }
  2809. var movie = MP4.moov(tracks),
  2810. result;
  2811. result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
  2812. result.set(MP4.FTYP);
  2813. result.set(movie, MP4.FTYP.byteLength);
  2814. return result;
  2815. }
  2816. }]);
  2817. return MP4;
  2818. }();
  2819. exports.default = MP4;
  2820. }, {}],
  2821. 16: [function(require, module, exports) {
  2822. 'use strict';
  2823. Object.defineProperty(exports, "__esModule", {
  2824. value: true
  2825. });
  2826. var _createClass = function() {
  2827. function defineProperties(target, props) {
  2828. for(var i = 0; i < props.length; i++) {
  2829. var descriptor = props[i];
  2830. descriptor.enumerable = descriptor.enumerable || false;
  2831. descriptor.configurable = true;
  2832. if("value" in descriptor) descriptor.writable = true;
  2833. Object.defineProperty(target, descriptor.key, descriptor);
  2834. }
  2835. }
  2836. return function(Constructor, protoProps, staticProps) {
  2837. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  2838. if(staticProps) defineProperties(Constructor, staticProps);
  2839. return Constructor;
  2840. };
  2841. }();
  2842. /**
  2843. * fMP4 remuxer
  2844. */
  2845. var _aac = require('../helper/aac');
  2846. var _aac2 = _interopRequireDefault(_aac);
  2847. var _events = require('../events');
  2848. var _events2 = _interopRequireDefault(_events);
  2849. var _logger = require('../utils/logger');
  2850. var _mp4Generator = require('../remux/mp4-generator');
  2851. var _mp4Generator2 = _interopRequireDefault(_mp4Generator);
  2852. var _errors = require('../errors');
  2853. require('../utils/polyfill');
  2854. function _interopRequireDefault(obj) {
  2855. return obj && obj.__esModule ? obj : {
  2856. default: obj
  2857. };
  2858. }
  2859. function _classCallCheck(instance, Constructor) {
  2860. if(!(instance instanceof Constructor)) {
  2861. throw new TypeError("Cannot call a class as a function");
  2862. }
  2863. }
  2864. var MP4Remuxer = function() {
  2865. function MP4Remuxer(observer, id, config) {
  2866. _classCallCheck(this, MP4Remuxer);
  2867. this.observer = observer;
  2868. this.id = id;
  2869. this.config = config;
  2870. this.ISGenerated = false;
  2871. this.PES2MP4SCALEFACTOR = 4;
  2872. this.PES_TIMESCALE = 90000;
  2873. this.MP4_TIMESCALE = this.PES_TIMESCALE / this.PES2MP4SCALEFACTOR;
  2874. this.nextAvcDts = 90300;
  2875. this.H264_TIMEBASE = 2000;
  2876. }
  2877. _createClass(MP4Remuxer, [{
  2878. key: 'destroy',
  2879. value: function destroy() {}
  2880. }, {
  2881. key: 'insertDiscontinuity',
  2882. value: function insertDiscontinuity() {
  2883. this._initPTS = this._initDTS = undefined;
  2884. }
  2885. }, {
  2886. key: 'switchLevel',
  2887. value: function switchLevel() {
  2888. this.ISGenerated = false;
  2889. }
  2890. }, {
  2891. key: 'pushVideo',
  2892. value: function pushVideo(level, sn, videoTrack, timeOffset, contiguous) {
  2893. this.level = level;
  2894. this.sn = sn;
  2895. var videoData = void 0;
  2896. // generate Init Segment if needed
  2897. if(!this.ISGenerated) {
  2898. this.generateVideoIS(videoTrack, timeOffset);
  2899. }
  2900. if(this.ISGenerated) {
  2901. // if (videoTrack.samples.length) {
  2902. this.remuxVideo_2(videoTrack, timeOffset, contiguous);
  2903. // }
  2904. }
  2905. }
  2906. }, {
  2907. key: 'remuxVideo_2',
  2908. value: function remuxVideo_2(track, timeOffset, contiguous, audioTrackLength) {
  2909. var offset = 8,
  2910. pesTimeScale = this.PES_TIMESCALE,
  2911. pes2mp4ScaleFactor = this.PES2MP4SCALEFACTOR,
  2912. mp4SampleDuration,
  2913. mdat,
  2914. moof,
  2915. firstPTS,
  2916. firstDTS,
  2917. nextDTS,
  2918. inputSamples = track.samples,
  2919. outputSamples = [];
  2920. /*连接视频数据并就地构造mdat需要另外8个字节来填充长度和mpdat类型)*/
  2921. mdat = new Uint8Array(track.len + 4 * track.nbNalu + 8);
  2922. var view = new DataView(mdat.buffer);
  2923. view.setUint32(0, mdat.byteLength);
  2924. mdat.set(_mp4Generator2.default.types.mdat, 4);
  2925. var sampleDuration = 0;
  2926. var ptsnorm = void 0,
  2927. dtsnorm = void 0,
  2928. mp4Sample = void 0,
  2929. lastDTS = void 0;
  2930. for(var i = 0; i < inputSamples.length; i++) {
  2931. var avcSample = inputSamples[i],
  2932. mp4SampleLength = 0,
  2933. compositionTimeOffset = void 0;
  2934. // 将NALU比特流转换为MP4格式(在NALU前面加上大小字段) 交接的人 请了解比特流的流程 》》 huangfei
  2935. while(avcSample.units.units.length) {
  2936. var unit = avcSample.units.units.shift();
  2937. view.setUint32(offset, unit.data.byteLength);
  2938. offset += 4;
  2939. mdat.set(unit.data, offset);
  2940. offset += unit.data.byteLength;
  2941. mp4SampleLength += 4 + unit.data.byteLength;
  2942. }
  2943. var pts = avcSample.pts - this._initPTS;
  2944. var dts = avcSample.dts - this._initDTS;
  2945. dts = Math.min(pts, dts);
  2946. if(lastDTS !== undefined) {
  2947. ptsnorm = this._PTSNormalize(pts, lastDTS);
  2948. dtsnorm = this._PTSNormalize(dts, lastDTS);
  2949. sampleDuration = dtsnorm - lastDTS;
  2950. if(sampleDuration <= 0) {
  2951. _logger.logger.log('invalid sample duration at PTS/DTS: ' + avcSample.pts + '/' + avcSample.dts + '|dts norm: ' + dtsnorm + '|lastDTS: ' + lastDTS + ':' + sampleDuration);
  2952. sampleDuration = 1;
  2953. }
  2954. } else {
  2955. var nextAvcDts = this.nextAvcDts,
  2956. delta;
  2957. ptsnorm = this._PTSNormalize(pts, nextAvcDts);
  2958. dtsnorm = this._PTSNormalize(dts, nextAvcDts);
  2959. if(nextAvcDts) {
  2960. delta = Math.round(dtsnorm - nextAvcDts);
  2961. if( /*contiguous ||*/ Math.abs(delta) < 600) {
  2962. if(delta) {
  2963. if(delta > 1) {
  2964. _logger.logger.log('AVC:' + delta + ' ms hole between fragments detected,filling it');
  2965. } else if(delta < -1) {
  2966. _logger.logger.log('AVC:' + -delta + ' ms overlapping between fragments detected');
  2967. }
  2968. dtsnorm = nextAvcDts;
  2969. ptsnorm = Math.max(ptsnorm - delta, dtsnorm);
  2970. _logger.logger.log('Video/PTS/DTS adjusted: ' + ptsnorm + '/' + dtsnorm + ',delta:' + delta);
  2971. }
  2972. }
  2973. }
  2974. this.firstPTS = Math.max(0, ptsnorm);
  2975. this.firstDTS = Math.max(0, dtsnorm);
  2976. sampleDuration = 0.03;
  2977. }
  2978. outputSamples.push({
  2979. size: mp4SampleLength,
  2980. duration: this.H264_TIMEBASE,
  2981. cts: 0,
  2982. flags: {
  2983. isLeading: 0,
  2984. isDependedOn: 0,
  2985. hasRedundancy: 0,
  2986. degradPrio: 0,
  2987. dependsOn: avcSample.key ? 2 : 1,
  2988. isNonSync: avcSample.key ? 0 : 1
  2989. }
  2990. });
  2991. lastDTS = dtsnorm;
  2992. }
  2993. var lastSampleDuration = 0;
  2994. if(outputSamples.length >= 2) {
  2995. lastSampleDuration = outputSamples[outputSamples.length - 2].duration;
  2996. outputSamples[0].duration = lastSampleDuration;
  2997. }
  2998. this.nextAvcDts = dtsnorm + lastSampleDuration;
  2999. var dropped = track.dropped;
  3000. track.len = 0;
  3001. track.nbNalu = 0;
  3002. track.dropped = 0;
  3003. if(outputSamples.length && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
  3004. var flags = outputSamples[0].flags;
  3005. flags.dependsOn = 2;
  3006. flags.isNonSync = 0;
  3007. }
  3008. track.samples = outputSamples;
  3009. moof = _mp4Generator2.default.moof(track.sequenceNumber++, dtsnorm, track);
  3010. track.samples = [];
  3011. var data = {
  3012. id: this.id,
  3013. level: this.level,
  3014. sn: this.sn,
  3015. data1: moof,
  3016. data2: mdat,
  3017. startPTS: ptsnorm,
  3018. endPTS: ptsnorm,
  3019. startDTS: dtsnorm,
  3020. endDTS: dtsnorm,
  3021. type: 'video',
  3022. nb: outputSamples.length,
  3023. dropped: dropped
  3024. };
  3025. this.observer.trigger(_events2.default.FRAG_PARSING_DATA, data);
  3026. return data;
  3027. }
  3028. }, {
  3029. key: 'generateVideoIS',
  3030. value: function generateVideoIS(videoTrack, timeOffset) {
  3031. var observer = this.observer,
  3032. videoSamples = videoTrack.samples,
  3033. pesTimeScale = this.PES_TIMESCALE,
  3034. tracks = {},
  3035. data = {
  3036. id: this.id,
  3037. level: this.level,
  3038. sn: this.sn,
  3039. tracks: tracks,
  3040. unique: false
  3041. },
  3042. computePTSDTS = this._initPTS === undefined,
  3043. initPTS,
  3044. initDTS;
  3045. if(computePTSDTS) {
  3046. initPTS = initDTS = Infinity;
  3047. }
  3048. if(videoTrack.sps && videoTrack.pps && videoSamples.length) {
  3049. videoTrack.timescale = 90000; //this.MP4_TIMESCALE;
  3050. tracks.video = {
  3051. container: 'video/mp4',
  3052. codec: videoTrack.codec,
  3053. initSegment: _mp4Generator2.default.initSegment([videoTrack]),
  3054. metadata: {
  3055. width: videoTrack.width,
  3056. height: videoTrack.height
  3057. }
  3058. };
  3059. if(computePTSDTS) {
  3060. initPTS = Math.min(initPTS, videoSamples[0].pts - this.H264_TIMEBASE);
  3061. initDTS = Math.min(initDTS, videoSamples[0].dts - this.H264_TIMEBASE);
  3062. }
  3063. }
  3064. if(Object.keys(tracks).length) {
  3065. observer.trigger(_events2.default.FRAG_PARSING_INIT_SEGMENT, data);
  3066. this.ISGenerated = true;
  3067. if(computePTSDTS) {
  3068. this._initPTS = initPTS;
  3069. this._initDTS = initDTS;
  3070. }
  3071. } else {
  3072. console.log("generateVideoIS ERROR==> ", _errors.ErrorTypes.MEDIA_ERROR);
  3073. }
  3074. }
  3075. }, {
  3076. key: 'remux',
  3077. value: function remux(level, sn, audioTrack, videoTrack, id3Track, textTrack, timeOffset, contiguous) {
  3078. this.level = level;
  3079. this.sn = sn;
  3080. // 如果需要,生成Init段
  3081. if(!this.ISGenerated) {
  3082. this.generateIS(audioTrack, videoTrack, timeOffset);
  3083. }
  3084. if(this.ISGenerated) {
  3085. //在视频之前有目的地重复播放音频,以便重复播放视频可以使用nextAacPts,即
  3086. //以remuxAudio计算。
  3087. //logger.log日志(“nb AAC样品:”+音轨.样品.长度); huangfei
  3088. //请了解代码之前,打开注释 打印对应的》》》块 》》》 logger.log()
  3089. if(audioTrack.samples.length) {
  3090. var audioData = this.remuxAudio(audioTrack, timeOffset, contiguous);
  3091. //logger.log('nb AVC samples:' + videoTrack.samples.length);
  3092. if(videoTrack.samples.length) {
  3093. var audioTrackLength = void 0;
  3094. if(audioData) {
  3095. audioTrackLength = audioData.endPTS - audioData.startPTS;
  3096. }
  3097. this.remuxVideo(videoTrack, timeOffset, contiguous, audioTrackLength);
  3098. }
  3099. } else {
  3100. var videoData = void 0;
  3101. //logger.log('nb AVC samples:' + videoTrack.samples.length);
  3102. if(videoTrack.samples.length) {
  3103. videoData = this.remuxVideo(videoTrack, timeOffset, contiguous);
  3104. }
  3105. if(videoData && audioTrack.codec) {
  3106. this.remuxEmptyAudio(audioTrack, timeOffset, contiguous, videoData);
  3107. }
  3108. }
  3109. }
  3110. //logger.log('nb ID3 samples:>>>>>' + audioTrack.samples.length);
  3111. if(id3Track.samples.length) {
  3112. this.remuxID3(id3Track, timeOffset);
  3113. }
  3114. //logger.log('nb ID3 samples:' + audioTrack.samples.length);
  3115. if(textTrack.samples.length) {
  3116. this.remuxText(textTrack, timeOffset);
  3117. }
  3118. //notify end of parsing
  3119. this.observer.trigger(_events2.default.FRAG_PARSED, {
  3120. id: this.id,
  3121. level: this.level,
  3122. sn: this.sn
  3123. });
  3124. }
  3125. }, {
  3126. key: 'generateIS',
  3127. value: function generateIS(audioTrack, videoTrack, timeOffset) {
  3128. var observer = this.observer,
  3129. audioSamples = audioTrack.samples,
  3130. videoSamples = videoTrack.samples,
  3131. pesTimeScale = this.PES_TIMESCALE,
  3132. tracks = {},
  3133. data = {
  3134. id: this.id,
  3135. level: this.level,
  3136. sn: this.sn,
  3137. tracks: tracks,
  3138. unique: false
  3139. },
  3140. computePTSDTS = this._initPTS === undefined,
  3141. initPTS,
  3142. initDTS;
  3143. if(computePTSDTS) {
  3144. initPTS = initDTS = Infinity;
  3145. }
  3146. if(audioTrack.config && audioSamples.length) {
  3147. audioTrack.timescale = audioTrack.audiosamplerate;
  3148. //MP4持续时间(以秒为单位的跟踪持续时间乘以时间刻度)编码为32位
  3149. //我们知道每个AAC样本包含1024帧。。。。
  3150. //为了避免32位计数器长时间溢出,我们使用较小的时间刻度(timescale/gcd)
  3151. //我们只需要确保AAC采样持续时间仍然是一个整数(将是1024/gcd)
  3152. if(audioTrack.timescale * audioTrack.duration > Math.pow(2, 32)) {
  3153. var greatestCommonDivisor = function greatestCommonDivisor(a, b) {
  3154. if(!b) {
  3155. return a;
  3156. }
  3157. return greatestCommonDivisor(b, a % b);
  3158. };
  3159. audioTrack.timescale = audioTrack.audiosamplerate / greatestCommonDivisor(audioTrack.audiosamplerate, 1024);
  3160. }
  3161. _logger.logger.log('audio mp4 timescale :' + audioTrack.timescale);
  3162. tracks.audio = {
  3163. container: 'audio/mp4',
  3164. codec: audioTrack.codec,
  3165. initSegment: _mp4Generator2.default.initSegment([audioTrack]),
  3166. metadata: {
  3167. channelCount: audioTrack.channelCount
  3168. }
  3169. };
  3170. if(computePTSDTS) {
  3171. // 请记住这个反对的背景的第一点。音频,PTS+D
  3172. initPTS = initDTS = audioSamples[0].pts - pesTimeScale * timeOffset;
  3173. }
  3174. }
  3175. if(videoTrack.sps && videoTrack.pps && videoSamples.length) {
  3176. videoTrack.timescale = this.MP4_TIMESCALE;
  3177. tracks.video = {
  3178. container: 'video/mp4',
  3179. codec: videoTrack.codec,
  3180. initSegment: _mp4Generator2.default.initSegment([videoTrack]),
  3181. metadata: {
  3182. width: videoTrack.width,
  3183. height: videoTrack.height
  3184. }
  3185. };
  3186. if(computePTSDTS) {
  3187. initPTS = Math.min(initPTS, videoSamples[0].pts - pesTimeScale * timeOffset);
  3188. initDTS = Math.min(initDTS, videoSamples[0].dts - pesTimeScale * timeOffset);
  3189. }
  3190. }
  3191. if(Object.keys(tracks).length) {
  3192. observer.trigger(_events2.default.FRAG_PARSING_INIT_SEGMENT, data);
  3193. this.ISGenerated = true;
  3194. if(computePTSDTS) {
  3195. this._initPTS = initPTS;
  3196. this._initDTS = initDTS;
  3197. }
  3198. } else {
  3199. observer.trigger(_events2.default.ERROR, {
  3200. type: _errors.ErrorTypes.MEDIA_ERROR,
  3201. id: this.id,
  3202. details: _errors.ErrorDetails.FRAG_PARSING_ERROR,
  3203. fatal: false,
  3204. reason: 'no audio/video samples found'
  3205. });
  3206. }
  3207. }
  3208. }, {
  3209. key: 'remuxVideo',
  3210. value: function remuxVideo(track, timeOffset, contiguous, audioTrackLength) {
  3211. var offset = 8,
  3212. pesTimeScale = this.PES_TIMESCALE,
  3213. pes2mp4ScaleFactor = this.PES2MP4SCALEFACTOR,
  3214. mp4SampleDuration,
  3215. mdat,
  3216. moof,
  3217. firstPTS,
  3218. firstDTS,
  3219. nextDTS,
  3220. lastPTS,
  3221. lastDTS,
  3222. inputSamples = track.samples,
  3223. outputSamples = [];
  3224. //PTS编码为33位,可以从-2^32循环到2^32
  3225. //PTSNormalize会使PTS/DTS值单调,我们使用最后已知的DTS值作为参考值
  3226. var nextAvcDts = void 0;
  3227. if(contiguous) {
  3228. // if parsed fragment is contiguous with last one, let's use last DTS value as reference
  3229. nextAvcDts = this.nextAvcDts;
  3230. } else {
  3231. // if not contiguous, let's use target timeOffset
  3232. nextAvcDts = timeOffset * pesTimeScale;
  3233. }
  3234. //计算第一个DTS和最后一个DTS,根据参考值对它们进行规范化 reference value
  3235. var sample = inputSamples[0];
  3236. firstDTS = Math.max(this._PTSNormalize(sample.dts, nextAvcDts) - this._initDTS, 0);
  3237. firstPTS = Math.max(this._PTSNormalize(sample.pts, nextAvcDts) - this._initDTS, 0);
  3238. // 检查跨连续片段的时间戳连续性(这是为了消除片段间间隙/孔)
  3239. var delta = Math.round((firstDTS - nextAvcDts) / 90);
  3240. // if fragment are contiguous, detect hole/overlapping between fragments
  3241. if(contiguous) {
  3242. if(delta) {
  3243. if(delta > 1) {
  3244. _logger.logger.log('AVC:' + delta + ' ms hole between fragments detected,filling it');
  3245. } else if(delta < -1) {
  3246. _logger.logger.log('AVC:' + -delta + ' ms overlapping between fragments detected');
  3247. }
  3248. // remove hole/gap : set DTS to next expected DTS
  3249. firstDTS = nextAvcDts;
  3250. inputSamples[0].dts = firstDTS + this._initDTS;
  3251. // offset PTS as well, ensure that PTS is smaller or equal than new DTS
  3252. firstPTS = Math.max(firstPTS - delta, nextAvcDts);
  3253. inputSamples[0].pts = firstPTS + this._initDTS;
  3254. _logger.logger.log('Video/PTS/DTS adjusted: ' + firstPTS + '/' + firstDTS + ',delta:' + delta);
  3255. }
  3256. }
  3257. nextDTS = firstDTS;
  3258. // 计算lastps/lastDTS
  3259. sample = inputSamples[inputSamples.length - 1];
  3260. lastDTS = Math.max(this._PTSNormalize(sample.dts, nextAvcDts) - this._initDTS, 0);
  3261. lastPTS = Math.max(this._PTSNormalize(sample.pts, nextAvcDts) - this._initDTS, 0);
  3262. lastPTS = Math.max(lastPTS, lastDTS);
  3263. var vendor = navigator.vendor,
  3264. userAgent = navigator.userAgent,
  3265. isSafari = vendor && vendor.indexOf('Apple') > -1 && userAgent && !userAgent.match('CriOS');
  3266. //在Safari上,让我们对所有样本发出相同的采样持续时间信号
  3267. //样本持续时间(如trun MP4框所预期的那样)应为样本DTS之间的增量
  3268. //将此持续时间设置为连续DTS之间的平均增量。
  3269. if(isSafari) {
  3270. mp4SampleDuration = Math.round((lastDTS - firstDTS) / (pes2mp4ScaleFactor * (inputSamples.length - 1)));
  3271. }
  3272. //现在规范化所有PTS/DTS。。。
  3273. for(var i = 0; i < inputSamples.length; i++) {
  3274. var _sample = inputSamples[i];
  3275. if(isSafari) {
  3276. // 使用样本之间的恒定解码偏移量(mp4SampleDuration)计算样本DTS
  3277. _sample.dts = firstDTS + i * pes2mp4ScaleFactor * mp4SampleDuration;
  3278. } else {
  3279. // 确保样本单调DTS
  3280. _sample.dts = Math.max(this._PTSNormalize(_sample.dts, nextAvcDts) - this._initDTS, firstDTS);
  3281. // 确保dts是比例因子的倍数,以避免舍入问题 对接的人,请注意》》》倍数
  3282. _sample.dts = Math.round(_sample.dts / pes2mp4ScaleFactor) * pes2mp4ScaleFactor;
  3283. }
  3284. //我们针对nextavcdt规范化PTS,还对initDTS进行减法(有些流不启动@PTS O)
  3285. //我们确保计算值大于或等于样本DTS
  3286. _sample.pts = Math.max(this._PTSNormalize(_sample.pts, nextAvcDts) - this._initDTS, _sample.dts);
  3287. // 确保pts是比例因子的倍数,以避免舍入问题
  3288. _sample.pts = Math.round(_sample.pts / pes2mp4ScaleFactor) * pes2mp4ScaleFactor;
  3289. }
  3290. /*连接视频数据并就地构造mdat(需要另外8个字节来填充长度和mpdat类型) need 8 */
  3291. mdat = new Uint8Array(track.len + 4 * track.nbNalu + 8);
  3292. var view = new DataView(mdat.buffer);
  3293. view.setUint32(0, mdat.byteLength);
  3294. mdat.set(_mp4Generator2.default.types.mdat, 4);
  3295. for(var _i = 0; _i < inputSamples.length; _i++) {
  3296. var avcSample = inputSamples[_i],
  3297. mp4SampleLength = 0,
  3298. compositionTimeOffset = void 0;
  3299. // 将NALU比特流转换为MP4格式(在NALU前面加上大小字段) == (prepend NALU with size field)
  3300. while(avcSample.units.units.length) {
  3301. var unit = avcSample.units.units.shift();
  3302. view.setUint32(offset, unit.data.byteLength);
  3303. offset += 4;
  3304. mdat.set(unit.data, offset);
  3305. offset += unit.data.byteLength;
  3306. mp4SampleLength += 4 + unit.data.byteLength;
  3307. }
  3308. if(!isSafari) {
  3309. // 预期样本持续时间是连续样本的解码时间戳差异 TDDE 解码时间
  3310. if(_i < inputSamples.length - 1) {
  3311. mp4SampleDuration = inputSamples[_i + 1].dts - avcSample.dts;
  3312. } else {
  3313. var config = this.config,
  3314. lastFrameDuration = avcSample.dts - inputSamples[_i > 0 ? _i - 1 : _i].dts;
  3315. if(config.stretchShortVideoTrack) {
  3316. //这里是为了拿到帧数的大小 TDDE
  3317. //在某些情况下,段的音频跟踪持续时间可能超过视频跟踪持续时间。
  3318. //既然我们已经恢复了音频,而且我们知道音频轨道有多长,我们希望
  3319. //查看到下一段的增量是否大于maxBufferHole的最小值,以及
  3320. //使段之间的任何潜在间隔最小化的最后一帧的持续时间。
  3321. var maxBufferHole = config.maxBufferHole,
  3322. maxSeekHole = config.maxSeekHole,
  3323. gapTolerance = Math.floor(Math.min(maxBufferHole, maxSeekHole) * pesTimeScale),
  3324. deltaToFrameEnd = (audioTrackLength ? firstPTS + audioTrackLength * pesTimeScale : this.nextAacPts) - avcSample.pts;
  3325. if(deltaToFrameEnd > gapTolerance) {
  3326. //我们从deltaToFrameEnd中减去lastFrameDuration以尝试阻止任何视频
  3327. //重叠。maxBufferHole/maxSeekHole无论如何都应该是>>lastFrameDuration。
  3328. mp4SampleDuration = deltaToFrameEnd - lastFrameDuration;
  3329. if(mp4SampleDuration < 0) {
  3330. mp4SampleDuration = lastFrameDuration;
  3331. }
  3332. _logger.logger.log('It is approximately ' + deltaToFrameEnd / 90 + ' ms to the next segment; using duration ' + mp4SampleDuration / 90 + ' ms for the last video frame.');
  3333. } else {
  3334. mp4SampleDuration = lastFrameDuration;
  3335. }
  3336. } else {
  3337. mp4SampleDuration = lastFrameDuration;
  3338. }
  3339. }
  3340. mp4SampleDuration /= pes2mp4ScaleFactor;
  3341. compositionTimeOffset = Math.round((avcSample.pts - avcSample.dts) / pes2mp4ScaleFactor);
  3342. } else {
  3343. compositionTimeOffset = Math.max(0, mp4SampleDuration * Math.round((avcSample.pts - avcSample.dts) / (pes2mp4ScaleFactor * mp4SampleDuration)));
  3344. }
  3345. outputSamples.push({
  3346. size: mp4SampleLength,
  3347. // 持续时间不变
  3348. duration: mp4SampleDuration,
  3349. cts: compositionTimeOffset,
  3350. flags: {
  3351. isLeading: 0,
  3352. isDependedOn: 0,
  3353. hasRedundancy: 0,
  3354. degradPrio: 0,
  3355. dependsOn: avcSample.key ? 2 : 1,
  3356. isNonSync: avcSample.key ? 0 : 1
  3357. }
  3358. });
  3359. }
  3360. //下一个AVC样本DTS应等于上一个样本DTS+上一个样本持续时间(以PES时间刻度为单位) == next AVC sample DTS should be equal to last sample DTS + last sample duration (in PES timescale)
  3361. this.nextAvcDts = lastDTS + mp4SampleDuration * pes2mp4ScaleFactor;
  3362. var dropped = track.dropped;
  3363. track.len = 0;
  3364. track.nbNalu = 0;
  3365. track.dropped = 0;
  3366. if(outputSamples.length && navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
  3367. var flags = outputSamples[0].flags;
  3368. flags.dependsOn = 2;
  3369. flags.isNonSync = 0;
  3370. }
  3371. track.samples = outputSamples;
  3372. moof = _mp4Generator2.default.moof(track.sequenceNumber++, firstDTS / pes2mp4ScaleFactor, track);
  3373. track.samples = [];
  3374. var data = {
  3375. id: this.id,
  3376. level: this.level,
  3377. sn: this.sn,
  3378. data1: moof,
  3379. data2: mdat,
  3380. startPTS: firstPTS / pesTimeScale,
  3381. endPTS: (lastPTS + pes2mp4ScaleFactor * mp4SampleDuration) / pesTimeScale,
  3382. startDTS: firstPTS / pesTimeScale,
  3383. endDTS: (lastPTS + pes2mp4ScaleFactor * mp4SampleDuration) / pesTimeScale,
  3384. // startDTS: firstDTS / pesTimeScale,
  3385. // endDTS: this.nextAvcDts / pesTimeScale,
  3386. type: 'video',
  3387. nb: outputSamples.length,
  3388. dropped: dropped
  3389. };
  3390. this.observer.trigger(_events2.default.FRAG_PARSING_DATA, data);
  3391. return data;
  3392. }
  3393. }, {
  3394. key: 'remuxAudio',
  3395. value: function remuxAudio(track, timeOffset, contiguous) {
  3396. var pesTimeScale = this.PES_TIMESCALE,
  3397. mp4timeScale = track.timescale,
  3398. pes2mp4ScaleFactor = pesTimeScale / mp4timeScale,
  3399. expectedSampleDuration = track.timescale * 1024 / track.audiosamplerate;
  3400. var view,
  3401. offset = 8,
  3402. aacSample,
  3403. mp4Sample,
  3404. unit,
  3405. mdat,
  3406. moof,
  3407. firstPTS,
  3408. firstDTS,
  3409. lastDTS,
  3410. pts,
  3411. dts,
  3412. ptsnorm,
  3413. dtsnorm,
  3414. samples = [],
  3415. samples0 = [];
  3416. track.samples.sort(function(a, b) {
  3417. return a.pts - b.pts;
  3418. });
  3419. samples0 = track.samples;
  3420. var nextAacPts = contiguous ? this.nextAacPts : timeOffset * pesTimeScale;
  3421. //如果音频曲目缺少示例,则帧似乎在
  3422. //生成的mp4段,导致同步问题,并在音频段的末尾留下间隙。
  3423. //为了防止这种情况发生,我们在有间隙的地方注入帧。
  3424. //如果可能,我们注入一个无声的帧;如果不可能,我们复制最后一个
  3425. var firstPtsNorm = this._PTSNormalize(samples0[0].pts - this._initPTS, nextAacPts),
  3426. pesFrameDuration = expectedSampleDuration * pes2mp4ScaleFactor;
  3427. var nextPtsNorm = firstPtsNorm + pesFrameDuration;
  3428. for(var i = 1; i < samples0.length;) {
  3429. // 首先,让我们看看这个离我们预期的距离有多远
  3430. var sample = samples0[i],
  3431. ptsNorm = this._PTSNormalize(sample.pts - this._initPTS, nextAacPts),
  3432. delta = ptsNorm - nextPtsNorm;
  3433. //如果重叠超过半个持续时间,则丢弃此样本
  3434. if(delta < -0.5 * pesFrameDuration) {
  3435. _logger.logger.log('Dropping frame due to ' + Math.abs(delta / 90) + ' ms overlap.');
  3436. samples0.splice(i, 1);
  3437. track.len -= sample.unit.length;
  3438. // 别碰nextPtsNorm或我
  3439. }
  3440. // 否则,如果我们离我们应该在的地方超过半帧,请插入丢失的帧 TDDE
  3441. else if(delta > 0.5 * pesFrameDuration) {
  3442. var missing = Math.round(delta / pesFrameDuration);
  3443. _logger.logger.log('Injecting ' + missing + ' frame' + (missing > 1 ? 's' : '') + ' of missing audio due to ' + Math.round(delta / 90) + ' ms gap.');
  3444. for(var j = 0; j < missing; j++) {
  3445. var newStamp = samples0[i - 1].pts + pesFrameDuration,
  3446. fillFrame = _aac2.default.getSilentFrame(track.channelCount);
  3447. if(!fillFrame) {
  3448. _logger.logger.log('Unable to get silent frame for given audio codec; duplicating last frame instead.');
  3449. fillFrame = sample.unit.slice(0);
  3450. }
  3451. samples0.splice(i, 0, {
  3452. unit: fillFrame,
  3453. pts: newStamp,
  3454. dts: newStamp
  3455. });
  3456. track.len += fillFrame.length;
  3457. i += 1;
  3458. }
  3459. // 将样本调整到下一个预期点
  3460. nextPtsNorm += (missing + 1) * pesFrameDuration;
  3461. sample.pts = samples0[i - 1].pts + pesFrameDuration;
  3462. i += 1;
  3463. }
  3464. // 否则,我们在半帧内,所以调整点
  3465. else {
  3466. if(Math.abs(delta) > 0.1 * pesFrameDuration) {
  3467. _logger.logger.log('Invalid frame delta ' + (ptsNorm - nextPtsNorm + pesFrameDuration) + ' at PTS ' + Math.round(ptsNorm / 90) + ' (should be ' + pesFrameDuration + ').');
  3468. }
  3469. nextPtsNorm += pesFrameDuration;
  3470. sample.pts = samples0[i - 1].pts + pesFrameDuration;
  3471. i += 1;
  3472. }
  3473. }
  3474. while(samples0.length) {
  3475. aacSample = samples0.shift();
  3476. unit = aacSample.unit;
  3477. pts = aacSample.pts - this._initDTS;
  3478. dts = aacSample.dts - this._initDTS;
  3479. //logger.log(`Audio/PTS:${Math.round(pts/90)}`);
  3480. // 如果不是第一个样本
  3481. if(lastDTS !== undefined) {
  3482. ptsnorm = this._PTSNormalize(pts, lastDTS);
  3483. dtsnorm = this._PTSNormalize(dts, lastDTS);
  3484. mp4Sample.duration = (dtsnorm - lastDTS) / pes2mp4ScaleFactor;
  3485. } else {
  3486. ptsnorm = this._PTSNormalize(pts, nextAacPts);
  3487. dtsnorm = this._PTSNormalize(dts, nextAacPts);
  3488. var _delta = Math.round(1000 * (ptsnorm - nextAacPts) / pesTimeScale);
  3489. // 如果碎片是连续的,则检测碎片之间的 孔/重叠 detect hole/overlapping between fragments
  3490. if(contiguous) {
  3491. // log delta
  3492. if(_delta) {
  3493. if(_delta > 0) {
  3494. _logger.logger.log(_delta + ' ms hole between AAC samples detected,filling it');
  3495. //如果我们有帧重叠,重叠超过半帧持续时间 overlapping for more than half a frame duraion
  3496. } else if(_delta < -12) {
  3497. // 放置重叠的音频帧。。。浏览器会处理的
  3498. _logger.logger.log(-_delta + ' ms overlapping between AAC samples detected, drop frame');
  3499. track.len -= unit.byteLength;
  3500. continue;
  3501. }
  3502. // set PTS/DTS to expected PTS/DTS
  3503. ptsnorm = dtsnorm = nextAacPts;
  3504. }
  3505. }
  3506. // 记住我们的aac样本的第一个点,确保值为正
  3507. firstPTS = Math.max(0, ptsnorm);
  3508. firstDTS = Math.max(0, dtsnorm);
  3509. if(track.len > 0) {
  3510. /*连接音频数据并就地构造mdat
  3511. (需要8个字节来填充长度和mdat类型)*/
  3512. mdat = new Uint8Array(track.len + 8);
  3513. view = new DataView(mdat.buffer);
  3514. view.setUint32(0, mdat.byteLength);
  3515. mdat.set(_mp4Generator2.default.types.mdat, 4);
  3516. } else {
  3517. // no audio samples
  3518. return;
  3519. }
  3520. }
  3521. mdat.set(unit, offset);
  3522. offset += unit.byteLength;
  3523. //console.log('PTS/DTS/initDTS/normPTS/normDTS/relative PTS : ${aacSample.pts}/${aacSample.dts}/${this._initDTS}/${ptsnorm}/${dtsnorm}/${(aacSample.pts/4294967296).toFixed(3)}');
  3524. mp4Sample = {
  3525. size: unit.byteLength,
  3526. cts: 0,
  3527. duration: 0,
  3528. flags: {
  3529. isLeading: 0,
  3530. isDependedOn: 0,
  3531. hasRedundancy: 0,
  3532. degradPrio: 0,
  3533. dependsOn: 1
  3534. }
  3535. };
  3536. samples.push(mp4Sample);
  3537. lastDTS = dtsnorm;
  3538. }
  3539. var lastSampleDuration = 0;
  3540. var nbSamples = samples.length;
  3541. //set last sample duration as being identical to previous sample
  3542. if(nbSamples >= 2) {
  3543. lastSampleDuration = samples[nbSamples - 2].duration;
  3544. mp4Sample.duration = lastSampleDuration;
  3545. }
  3546. if(nbSamples) {
  3547. //下一个aac样本点应等于上一个样本点+持续时间
  3548. this.nextAacPts = ptsnorm + pes2mp4ScaleFactor * lastSampleDuration;
  3549. //logger.log('Audio/PTS/PTSend:' + aacSample.pts.toFixed(0) + '/' + this.nextAacDts.toFixed(0));
  3550. track.len = 0;
  3551. track.samples = samples;
  3552. moof = _mp4Generator2.default.moof(track.sequenceNumber++, firstDTS / pes2mp4ScaleFactor, track);
  3553. track.samples = [];
  3554. var audioData = {
  3555. id: this.id,
  3556. level: this.level,
  3557. sn: this.sn,
  3558. data1: moof,
  3559. data2: mdat,
  3560. startPTS: firstPTS / pesTimeScale,
  3561. endPTS: this.nextAacPts / pesTimeScale,
  3562. startDTS: firstDTS / pesTimeScale,
  3563. endDTS: (dtsnorm + pes2mp4ScaleFactor * lastSampleDuration) / pesTimeScale,
  3564. type: 'audio',
  3565. nb: nbSamples
  3566. };
  3567. this.observer.trigger(_events2.default.FRAG_PARSING_DATA, audioData);
  3568. return audioData;
  3569. }
  3570. return null;
  3571. }
  3572. }, {
  3573. key: 'remuxEmptyAudio',
  3574. value: function remuxEmptyAudio(track, timeOffset, contiguous, videoData) {
  3575. var pesTimeScale = this.PES_TIMESCALE,
  3576. mp4timeScale = track.timescale ? track.timescale : track.audiosamplerate,
  3577. pes2mp4ScaleFactor = pesTimeScale / mp4timeScale,
  3578. // 与视频的时间戳同步
  3579. startDTS = videoData.startDTS * pesTimeScale + this._initDTS,
  3580. endDTS = videoData.endDTS * pesTimeScale + this._initDTS,
  3581. //一个样本的持续时间值
  3582. sampleDuration = 1024,
  3583. frameDuration = pes2mp4ScaleFactor * sampleDuration,
  3584. //此段持续时间的样本计数
  3585. nbSamples = Math.ceil((endDTS - startDTS) / frameDuration),
  3586. //无声
  3587. silentFrame = _aac2.default.getSilentFrame(track.channelCount);
  3588. // 如果我们不能产生一个无声的画面。。。
  3589. if(!silentFrame) {
  3590. _logger.logger.trace('Unable to remuxEmptyAudio since we were unable to get a silent frame for given audio codec!');
  3591. return;
  3592. }
  3593. var samples = [];
  3594. for(var i = 0; i < nbSamples; i++) {
  3595. var stamp = startDTS + i * frameDuration;
  3596. samples.push({
  3597. unit: silentFrame.slice(0),
  3598. pts: stamp,
  3599. dts: stamp
  3600. });
  3601. track.len += silentFrame.length;
  3602. }
  3603. track.samples = samples;
  3604. this.remuxAudio(track, timeOffset, contiguous);
  3605. }
  3606. }, {
  3607. key: 'remuxID3',
  3608. value: function remuxID3(track, timeOffset) {
  3609. var length = track.samples.length,
  3610. sample;
  3611. // 消耗样本
  3612. if(length) {
  3613. for(var index = 0; index < length; index++) {
  3614. sample = track.samples[index];
  3615. //将id3 pts,dts设置为相对时间
  3616. //使用this.\u initPTS和this.\u initDTS计算相对时间
  3617. sample.pts = (sample.pts - this._initPTS) / this.PES_TIMESCALE;
  3618. sample.dts = (sample.dts - this._initDTS) / this.PES_TIMESCALE;
  3619. }
  3620. this.observer.trigger(_events2.default.FRAG_PARSING_METADATA, {
  3621. id: this.id,
  3622. level: this.level,
  3623. sn: this.sn,
  3624. samples: track.samples
  3625. });
  3626. }
  3627. track.samples = [];
  3628. timeOffset = timeOffset;
  3629. }
  3630. }, {
  3631. key: 'remuxText',
  3632. value: function remuxText(track, timeOffset) {
  3633. track.samples.sort(function(a, b) {
  3634. return a.pts - b.pts;
  3635. });
  3636. var length = track.samples.length,
  3637. sample;
  3638. // 消耗样本
  3639. if(length) {
  3640. for(var index = 0; index < length; index++) {
  3641. sample = track.samples[index];
  3642. sample.pts = (sample.pts - this._initPTS) / this.PES_TIMESCALE;
  3643. }
  3644. this.observer.trigger(_events2.default.FRAG_PARSING_USERDATA, {
  3645. id: this.id,
  3646. level: this.level,
  3647. sn: this.sn,
  3648. samples: track.samples
  3649. });
  3650. }
  3651. track.samples = [];
  3652. timeOffset = timeOffset;
  3653. }
  3654. }, {
  3655. key: '_PTSNormalize',
  3656. value: function _PTSNormalize(value, reference) {
  3657. var offset;
  3658. if(reference === undefined) {
  3659. return value;
  3660. }
  3661. if(reference < value) {
  3662. // - 2^33
  3663. offset = -8589934592;
  3664. } else {
  3665. // + 2^33
  3666. offset = 8589934592;
  3667. }
  3668. /*PTS是33位(从0到2^33-1)
  3669. 如果值和参考值之间的差值大于振幅的一半(2^32),则意味着
  3670. 发生PTS循环。填补空白*/
  3671. while(Math.abs(value - reference) > 4294967296) {
  3672. value += offset;
  3673. }
  3674. return value;
  3675. }
  3676. }, {
  3677. key: 'passthrough',
  3678. get: function get() {
  3679. return false;
  3680. }
  3681. }]);
  3682. return MP4Remuxer;
  3683. }();
  3684. exports.default = MP4Remuxer;
  3685. }, {
  3686. "../errors": 7,
  3687. "../events": 9,
  3688. "../helper/aac": 10,
  3689. "../remux/mp4-generator": 15,
  3690. "../utils/logger": 17,
  3691. "../utils/polyfill": 18
  3692. }],
  3693. 17: [function(require, module, exports) {
  3694. 'use strict';
  3695. Object.defineProperty(exports, "__esModule", {
  3696. value: true
  3697. });
  3698. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function(obj) {
  3699. return typeof obj;
  3700. } : function(obj) {
  3701. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  3702. };
  3703. function noop() {}
  3704. var fakeLogger = {
  3705. trace: noop,
  3706. debug: noop,
  3707. log: noop,
  3708. warn: noop,
  3709. info: noop,
  3710. error: noop
  3711. };
  3712. var exportedLogger = fakeLogger;
  3713. //let lastCallTime;
  3714. // function formatMsgWithTimeInfo(type, msg) {
  3715. // const now = Date.now();
  3716. // const diff = lastCallTime ? '+' + (now - lastCallTime) : '0';
  3717. // lastCallTime = now;
  3718. // msg = (new Date(now)).toISOString() + ' | [' + type + '] > ' + msg + ' ( ' + diff + ' ms )';
  3719. // return msg;
  3720. // }
  3721. function formatMsg(type, msg) {
  3722. msg = '[' + type + '] > ' + msg;
  3723. return msg;
  3724. }
  3725. function consolePrintFn(type) {
  3726. var func = window.console[type];
  3727. if(func) {
  3728. return function() {
  3729. for(var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  3730. args[_key] = arguments[_key];
  3731. }
  3732. if(args[0]) {
  3733. args[0] = formatMsg(type, args[0]);
  3734. }
  3735. func.apply(window.console, args);
  3736. };
  3737. }
  3738. return noop;
  3739. }
  3740. function exportLoggerFunctions(debugConfig) {
  3741. for(var _len2 = arguments.length, functions = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  3742. functions[_key2 - 1] = arguments[_key2];
  3743. }
  3744. functions.forEach(function(type) {
  3745. exportedLogger[type] = debugConfig[type] ? debugConfig[type].bind(debugConfig) : consolePrintFn(type);
  3746. });
  3747. }
  3748. var enableLogs = exports.enableLogs = function enableLogs(debugConfig) {
  3749. if(debugConfig === true || (typeof debugConfig === 'undefined' ? 'undefined' : _typeof(debugConfig)) === 'object') {
  3750. exportLoggerFunctions(debugConfig,
  3751. // Remove out from list here to hard-disable a log-level
  3752. //'trace',
  3753. 'debug', 'log', 'info', 'warn', 'error');
  3754. // Some browsers don't allow to use bind on console object anyway
  3755. // fallback to default if needed
  3756. try {
  3757. exportedLogger.log();
  3758. } catch(e) {
  3759. exportedLogger = fakeLogger;
  3760. }
  3761. } else {
  3762. exportedLogger = fakeLogger;
  3763. }
  3764. };
  3765. var logger = exports.logger = exportedLogger;
  3766. }, {}],
  3767. 18: [function(require, module, exports) {
  3768. 'use strict';
  3769. if(typeof ArrayBuffer !== 'undefined' && !ArrayBuffer.prototype.slice) {
  3770. ArrayBuffer.prototype.slice = function(start, end) {
  3771. var that = new Uint8Array(this);
  3772. if(end === undefined) {
  3773. end = that.length;
  3774. }
  3775. var result = new ArrayBuffer(end - start);
  3776. var resultArray = new Uint8Array(result);
  3777. for(var i = 0; i < resultArray.length; i++) {
  3778. resultArray[i] = that[i + start];
  3779. }
  3780. return result;
  3781. };
  3782. }
  3783. }, {}],
  3784. 19: [function(require, module, exports) {
  3785. 'use strict';
  3786. Object.defineProperty(exports, "__esModule", {
  3787. value: true
  3788. });
  3789. var _createClass = function() {
  3790. function defineProperties(target, props) {
  3791. for(var i = 0; i < props.length; i++) {
  3792. var descriptor = props[i];
  3793. descriptor.enumerable = descriptor.enumerable || false;
  3794. descriptor.configurable = true;
  3795. if("value" in descriptor) descriptor.writable = true;
  3796. Object.defineProperty(target, descriptor.key, descriptor);
  3797. }
  3798. }
  3799. return function(Constructor, protoProps, staticProps) {
  3800. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  3801. if(staticProps) defineProperties(Constructor, staticProps);
  3802. return Constructor;
  3803. };
  3804. }();
  3805. function _classCallCheck(instance, Constructor) {
  3806. if(!(instance instanceof Constructor)) {
  3807. throw new TypeError("Cannot call a class as a function");
  3808. }
  3809. }
  3810. /**
  3811. * 基于XHR的记录器 logger
  3812. */
  3813. var XhrLoader = function() {
  3814. function XhrLoader(config) {
  3815. _classCallCheck(this, XhrLoader);
  3816. if(config && config.xhrSetup) {
  3817. this.xhrSetup = config.xhrSetup;
  3818. }
  3819. }
  3820. _createClass(XhrLoader, [{
  3821. key: 'destroy',
  3822. value: function destroy() {
  3823. this.abort();
  3824. this.loader = null;
  3825. }
  3826. }, {
  3827. key: 'abort',
  3828. value: function abort() {
  3829. var loader = this.loader;
  3830. if(loader && loader.readyState !== 4) {
  3831. this.stats.aborted = true;
  3832. loader.abort();
  3833. }
  3834. window.clearTimeout(this.requestTimeout);
  3835. this.requestTimeout = null;
  3836. window.clearTimeout(this.retryTimeout);
  3837. this.retryTimeout = null;
  3838. }
  3839. }, {
  3840. key: 'loadHead',
  3841. value: function loadHead(context, config, callbacks) {
  3842. this.context = context;
  3843. this.config = config;
  3844. this.callbacks = callbacks;
  3845. this.stats = {
  3846. trequest: performance.now(),
  3847. retry: 0
  3848. };
  3849. this.retryDelay = config.retryDelay;
  3850. var xhr = new XMLHttpRequest();
  3851. xhr.open('head', context.url);
  3852. xhr.onload = function() {
  3853. callbacks.onSuccess(xhr.getResponseHeader('content-length'));
  3854. };
  3855. xhr.send();
  3856. }
  3857. }, {
  3858. key: 'load',
  3859. value: function load(context, config, callbacks) {
  3860. this.context = context;
  3861. this.config = config;
  3862. this.callbacks = callbacks;
  3863. this.stats = {
  3864. trequest: performance.now(),
  3865. retry: 0
  3866. };
  3867. this.retryDelay = config.retryDelay;
  3868. this.loadInternal();
  3869. }
  3870. }, {
  3871. key: 'loadInternal',
  3872. value: function loadInternal() {
  3873. var xhr,
  3874. context = this.context;
  3875. if(typeof XDomainRequest !== 'undefined') {
  3876. xhr = this.loader = new XDomainRequest();
  3877. } else {
  3878. xhr = this.loader = new XMLHttpRequest();
  3879. }
  3880. xhr.onloadend = this.loadend.bind(this);
  3881. xhr.onprogress = this.loadprogress.bind(this);
  3882. xhr.open('GET', context.url, true);
  3883. if(context.rangeEnd) {
  3884. xhr.setRequestHeader('Range', 'bytes=' + context.rangeStart + '-' + (context.rangeEnd - 1));
  3885. }
  3886. xhr.responseType = context.responseType;
  3887. var stats = this.stats;
  3888. stats.tfirst = 0;
  3889. stats.loaded = 0;
  3890. if(this.xhrSetup) {
  3891. this.xhrSetup(xhr, context.url);
  3892. }
  3893. //在执行请求之前设置超时
  3894. this.requestTimeout = window.setTimeout(this.loadtimeout.bind(this), this.config.timeout);
  3895. xhr.send();
  3896. }
  3897. }, {
  3898. key: 'loadend',
  3899. value: function loadend(event) {
  3900. var xhr = event.currentTarget,
  3901. status = xhr.status,
  3902. stats = this.stats,
  3903. context = this.context,
  3904. config = this.config;
  3905. // 如果xhr已中止,则不要继续
  3906. if(stats.aborted) {
  3907. return;
  3908. }
  3909. // 无论如何,清除当前XHR超时
  3910. window.clearTimeout(this.requestTimeout);
  3911. // 200到299之间的http状态都是成功的
  3912. if(status >= 200 && status < 300) {
  3913. stats.tload = Math.max(stats.tfirst, performance.now());
  3914. var data = void 0,
  3915. len = void 0;
  3916. if(context.responseType === 'arraybuffer') {
  3917. data = xhr.response;
  3918. len = data.byteLength;
  3919. } else {
  3920. data = xhr.responseText;
  3921. len = data.length;
  3922. }
  3923. stats.loaded = stats.total = len;
  3924. var response = {
  3925. url: xhr.responseURL,
  3926. data: data
  3927. };
  3928. this.callbacks.onSuccess(response, stats, context);
  3929. } else {
  3930. // 如果达到最大重试次数,或者如果http状态介于400到499之间(此错误无法恢复,重试无效),则返回错误 TDDE
  3931. if(stats.retry >= config.maxRetry || status >= 400 && status < 499) {
  3932. // logger.error(`${status} while loading ${context.url}` );
  3933. this.callbacks.onError({
  3934. code: status,
  3935. text: xhr.statusText
  3936. }, context);
  3937. } else {
  3938. this.destroy();
  3939. // 计划重试
  3940. this.retryTimeout = window.setTimeout(this.loadInternal.bind(this), this.retryDelay);
  3941. // 设置指数退避 backoff
  3942. this.retryDelay = Math.min(2 * this.retryDelay, config.maxRetryDelay);
  3943. stats.retry++;
  3944. }
  3945. }
  3946. }
  3947. }, {
  3948. key: 'loadtimeout',
  3949. value: function loadtimeout() {
  3950. // logger.warn(`timeout while loading ${this.context.url}` );
  3951. this.callbacks.onTimeout(this.stats, this.context);
  3952. }
  3953. }, {
  3954. key: 'loadprogress',
  3955. value: function loadprogress(event) {
  3956. var stats = this.stats;
  3957. if(stats.tfirst === 0) {
  3958. stats.tfirst = Math.max(performance.now(), stats.trequest);
  3959. }
  3960. stats.loaded = event.loaded;
  3961. if(event.lengthComputable) {
  3962. stats.total = event.total;
  3963. }
  3964. var onProgress = this.callbacks.onProgress;
  3965. if(onProgress) {
  3966. // last args is to provide on progress data
  3967. onProgress(stats, this.context, null);
  3968. }
  3969. }
  3970. }]);
  3971. return XhrLoader;
  3972. }();
  3973. exports.default = XhrLoader;
  3974. }, {}],
  3975. 20: [function(require, module, exports) {
  3976. 'use strict';
  3977. Object.defineProperty(exports, "__esModule", {
  3978. value: true
  3979. });
  3980. var _createClass = function() {
  3981. function defineProperties(target, props) {
  3982. for(var i = 0; i < props.length; i++) {
  3983. var descriptor = props[i];
  3984. descriptor.enumerable = descriptor.enumerable || false;
  3985. descriptor.configurable = true;
  3986. if("value" in descriptor) descriptor.writable = true;
  3987. Object.defineProperty(target, descriptor.key, descriptor);
  3988. }
  3989. }
  3990. return function(Constructor, protoProps, staticProps) {
  3991. if(protoProps) defineProperties(Constructor.prototype, protoProps);
  3992. if(staticProps) defineProperties(Constructor, staticProps);
  3993. return Constructor;
  3994. };
  3995. }();
  3996. var _events = require('./events');
  3997. var _events2 = _interopRequireDefault(_events);
  3998. var _flowController = require('./controller/flow-controller');
  3999. var _flowController2 = _interopRequireDefault(_flowController);
  4000. var _bufferController = require('./controller/buffer-controller');
  4001. var _bufferController2 = _interopRequireDefault(_bufferController);
  4002. var _events3 = require('events');
  4003. var _events4 = _interopRequireDefault(_events3);
  4004. var _xhrLoader = require('./utils/xhr-loader');
  4005. var _xhrLoader2 = _interopRequireDefault(_xhrLoader);
  4006. var _fileLoader = require('./loader/file-loader');
  4007. var _fileLoader2 = _interopRequireDefault(_fileLoader);
  4008. var _websocketLoader = require('./loader/websocket-loader');
  4009. var _websocketLoader2 = _interopRequireDefault(_websocketLoader);
  4010. function _interopRequireDefault(obj) {
  4011. return obj && obj.__esModule ? obj : {
  4012. default: obj
  4013. };
  4014. }
  4015. function _classCallCheck(instance, Constructor) {
  4016. if(!(instance instanceof Constructor)) {
  4017. throw new TypeError("Cannot call a class as a function");
  4018. }
  4019. }
  4020. var Wfs = function() {
  4021. _createClass(Wfs, null, [{
  4022. key: 'isSupported',
  4023. value: function isSupported() {
  4024. return window.MediaSource && typeof window.MediaSource.isTypeSupported === 'function' && window.MediaSource.isTypeSupported('video/mp4; codecs="avc1.42c01f,mp4a.40.2"');
  4025. }
  4026. }, {
  4027. key: 'version',
  4028. get: function get() {
  4029. return '' + 'v.0.0.0.1';
  4030. }
  4031. }, {
  4032. key: 'Events',
  4033. get: function get() {
  4034. return _events2.default;
  4035. }
  4036. }, {
  4037. key: 'DefaultConfig',
  4038. get: function get() {
  4039. if(!Wfs.defaultConfig) {
  4040. Wfs.defaultConfig = {
  4041. autoStartLoad: true,
  4042. startPosition: -1,
  4043. debug: false,
  4044. fLoader: undefined,
  4045. loader: _xhrLoader2.default,
  4046. //loader: FetchLoader,
  4047. fmp4FileUrl: 'xxxx.mp4',
  4048. fragLoadingTimeOut: 20000,
  4049. fragLoadingMaxRetry: 6,
  4050. fragLoadingRetryDelay: 1000,
  4051. fragLoadingMaxRetryTimeout: 64000,
  4052. fragLoadingLoopThreshold: 3,
  4053. forceKeyFrameOnDiscontinuity: true,
  4054. appendErrorMaxRetry: 3
  4055. };
  4056. }
  4057. return Wfs.defaultConfig;
  4058. },
  4059. set: function set(defaultConfig) {
  4060. Wfs.defaultConfig = defaultConfig;
  4061. }
  4062. }]);
  4063. function Wfs() {
  4064. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  4065. _classCallCheck(this, Wfs);
  4066. var defaultConfig = Wfs.DefaultConfig;
  4067. for(var prop in defaultConfig) {
  4068. if(prop in config) {
  4069. continue;
  4070. }
  4071. config[prop] = defaultConfig[prop];
  4072. }
  4073. this.config = config;
  4074. // observer setup
  4075. var observer = this.observer = new _events4.default();
  4076. observer.trigger = function trigger(event) {
  4077. for(var _len = arguments.length, data = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  4078. data[_key - 1] = arguments[_key];
  4079. }
  4080. observer.emit.apply(observer, [event, event].concat(data));
  4081. };
  4082. observer.off = function off(event) {
  4083. for(var _len2 = arguments.length, data = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  4084. data[_key2 - 1] = arguments[_key2];
  4085. }
  4086. observer.removeListener.apply(observer, [event].concat(data));
  4087. };
  4088. this.on = observer.on.bind(observer);
  4089. this.off = observer.off.bind(observer);
  4090. this.trigger = observer.trigger.bind(observer);
  4091. this.flowController = new _flowController2.default(this);
  4092. this.bufferController = new _bufferController2.default(this);
  4093. // this.fileLoader = new FileLoader(this);
  4094. this.websocketLoader = new _websocketLoader2.default(this);
  4095. this.mediaType = undefined;
  4096. }
  4097. _createClass(Wfs, [{
  4098. key: 'destroy',
  4099. value: function destroy() {
  4100. this.flowController.destroy();
  4101. this.bufferController.destroy();
  4102. // this.fileLoader.destroy();
  4103. this.websocketLoader.destroy();
  4104. }
  4105. }, {
  4106. key: 'attachMedia',
  4107. value: function attachMedia(media) {
  4108. var channelName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'chX';
  4109. var mediaType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'H264Raw';
  4110. var websocketName = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'play2';
  4111. // 'H264Raw' 'FMp4'
  4112. this.mediaType = mediaType;
  4113. this.media = media;
  4114. this.trigger(_events2.default.MEDIA_ATTACHING, {
  4115. media: media,
  4116. channelName: channelName,
  4117. mediaType: mediaType,
  4118. websocketName: websocketName
  4119. });
  4120. }
  4121. }, {
  4122. key: 'attachWebsocket',
  4123. value: function attachWebsocket(websocket, channelName) {
  4124. console.log("111>>>",websocket)
  4125. this.trigger(_events2.default.WEBSOCKET_ATTACHING, {
  4126. websocket: websocket,
  4127. mediaType: this.mediaType,
  4128. channelName: channelName
  4129. });
  4130. }
  4131. }]);
  4132. return Wfs;
  4133. }();
  4134. exports.default = Wfs;
  4135. }, {
  4136. "./controller/buffer-controller": 3,
  4137. "./controller/flow-controller": 4,
  4138. "./events": 9,
  4139. "./loader/file-loader": 13,
  4140. "./loader/websocket-loader": 14,
  4141. "./utils/xhr-loader": 19,
  4142. "events": 1
  4143. }]
  4144. }, {}, [11])(11)
  4145. });