From cdeb7fcbe4c36e8e1f6043eb96c33f125117a100 Mon Sep 17 00:00:00 2001 From: Samir Date: Sun, 26 Jul 2020 16:12:23 +0600 Subject: [PATCH] update all outdated node modules --- .babelrc | 8 +- .eslintignore | 2 +- dist/jmuxer.min.js | 2 +- example/jmuxer.min.js | 2 +- karma.conf.js | 8 +- package-lock.json | 4982 +++++++++++++++++++++++++++++------------ package.json | 23 +- rollup.config.js | 4 +- 8 files changed, 3607 insertions(+), 1424 deletions(-) diff --git a/.babelrc b/.babelrc index fb890d2..9df1f64 100644 --- a/.babelrc +++ b/.babelrc @@ -1,13 +1,7 @@ { "presets": [ [ - "es2015", - { - "modules": false - } + "@babel/preset-env" ] - ], - "plugins": [ - "external-helpers" ] } \ No newline at end of file diff --git a/.eslintignore b/.eslintignore index 8782b4e..e7c2f1d 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1 +1 @@ -decoder.js \ No newline at end of file +mp4-generator.js \ No newline at end of file diff --git a/dist/jmuxer.min.js b/dist/jmuxer.min.js index 8af4a28..d1a4fc7 100644 --- a/dist/jmuxer.min.js +++ b/dist/jmuxer.min.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.JMuxer=t()}(this,function(){"use strict";function e(){a=console.log,s=console.error}function t(e){if(a){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n1?t-1:0),n=1;n0&&(i+=(r<10?"0"+r:r)+":"),i+=(n<10?"0"+n:n)+":"+(t<10?"0"+t:t)}var a=void 0,s=void 0,o=(function(){function e(e){this.value=e}function t(t){function r(e,t){return new Promise(function(r,i){var o={key:e,arg:t,resolve:r,reject:i,next:null};s?s=s.next=o:(a=s=o,n(e,t))})}function n(r,a){try{var s=t[r](a),o=s.value;o instanceof e?Promise.resolve(o.value).then(function(e){n("next",e)},function(e){n("throw",e)}):i(s.done?"return":"normal",s.value)}catch(e){i("throw",e)}}function i(e,t){switch(e){case"return":a.resolve({value:t,done:!0});break;case"throw":a.reject(t);break;default:a.resolve({value:t,done:!1})}a=a.next,a?n(a.key,a.arg):s=null}var a,s;this._invoke=r,"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),u=function(){function e(e,t){for(var r=0;r>5,this.ntype=31&this.payload[0],this.isvcl=1==this.ntype||5==this.ntype,this.stype="",this.isfmb=!1}return u(e,null,[{key:"type",value:function(t){return t.ntype in e.TYPES?e.TYPES[t.ntype]:"UNKNOWN"}},{key:"NDR",get:function(){return 1}},{key:"IDR",get:function(){return 5}},{key:"SEI",get:function(){return 6}},{key:"SPS",get:function(){return 7}},{key:"PPS",get:function(){return 8}},{key:"AUD",get:function(){return 9}},{key:"TYPES",get:function(){var t;return t={},babelHelpers.defineProperty(t,e.IDR,"IDR"),babelHelpers.defineProperty(t,e.SEI,"SEI"),babelHelpers.defineProperty(t,e.SPS,"SPS"),babelHelpers.defineProperty(t,e.PPS,"PPS"),babelHelpers.defineProperty(t,e.NDR,"NDR"),babelHelpers.defineProperty(t,e.AUD,"AUD"),t}}]),u(e,[{key:"toString",value:function(){return e.type(this)+": NRI: "+this.getNri()}},{key:"getNri",value:function(){return this.nri}},{key:"type",value:function(){return this.ntype}},{key:"isKeyframe",value:function(){return this.ntype===e.IDR||7===this.stype}},{key:"getPayload",value:function(){return this.payload}},{key:"getPayloadSize",value:function(){return this.payload.byteLength}},{key:"getSize",value:function(){return 4+this.getPayloadSize()}},{key:"getData",value:function(){var e=new Uint8Array(this.getSize());return new DataView(e.buffer).setUint32(0,this.getSize()-4),e.set(this.getPayload(),4),e}}]),e}(),d=function(){function e(t){o(this,e),this.data=t,this.index=0,this.bitLength=8*t.byteLength}return u(e,[{key:"skipBits",value:function(e){if(this.bitsAvailable1&&void 0!==arguments[1])||arguments[1];return this.getBits(e,this.index,t)}},{key:"getBits",value:function(e,t){var r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];if(this.bitsAvailable>>n,a=8-n;if(a>=e)return r&&(this.index+=e),i>>a-e;r&&(this.index+=a);var s=e-a;return i<>>1:-1*(e>>>1)}},{key:"readBoolean",value:function(){return 1===this.readBits(1)}},{key:"readUByte",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return this.readBits(8*e)}},{key:"readUShort",value:function(){return this.readBits(16)}},{key:"readUInt",value:function(){return this.readBits(32)}},{key:"bitsAvailable",get:function(){return this.bitLength-this.index}}]),e}(),h=function(){function e(t){o(this,e),this.remuxer=t,this.track=t.mp4track}return u(e,null,[{key:"extractNALu",value:function(e){for(var t=0,r=e.byteLength,n=void 0,i=0,a=[],s=void 0;t>>5}},{key:"isAACPattern",value:function(e){return 255===e[0]&&240==(240&e[1])&&0==(6&e[1])}},{key:"extractAAC",value:function(t){var n=0,i=t.byteLength,a=[],s=void 0,o=void 0;if(!e.isAACPattern(t))return r("Invalid ADTS audio format"),a;for(s=e.getHeaderLength(t),y||(y=t.subarray(0,s));n>>6),r=(60&a[2])>>>2,n=(1&a[2])<<2,n|=(192&a[3])>>>6,i[0]=t<<3,i[0]|=(14&r)>>1,i[1]|=(1&r)<<7,i[1]|=n<<3,this.track.codec="mp4a.40."+t,this.track.channelCount=n,this.track.config=i,this.remuxer.readyToDecode=!0)}}]),e}(),v=function(){function e(t){o(this,e),this.listener={},this.type=""|t}return u(e,[{key:"on",value:function(e,t){return this.listener[e]||(this.listener[e]=[]),this.listener[e].push(t),!0}},{key:"off",value:function(e,t){if(this.listener[e]){var r=this.listener[e].indexOf(t);return r>-1&&this.listener[e].splice(r,1),!0}return!1}},{key:"offAll",value:function(){this.listener={}}},{key:"dispatch",value:function(e,t){return!!this.listener[e]&&(this.listener[e].map(function(e){e.apply(null,[t])}),!0)}}]),e}(),k=function(){function e(){o(this,e)}return u(e,null,[{key:"init",value:function(){e.types={avc1:[],avcC:[],btrt:[],dinf:[],dref:[],esds:[],ftyp:[],hdlr:[],mdat:[],mdhd:[],mdia:[],mfhd:[],minf:[],moof:[],moov:[],mp4a:[],mvex:[],mvhd:[],sdtp:[],stbl:[],stco:[],stsc:[],stsd:[],stsz:[],stts:[],tfdt:[],tfhd:[],traf:[],trak:[],trun:[],trex:[],tkhd:[],vmhd:[],smhd:[]};var t;for(t in e.types)e.types.hasOwnProperty(t)&&(e.types[t]=[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]);var r=new Uint8Array([0,0,0,0,0,0,0,0,118,105,100,101,0,0,0,0,0,0,0,0,0,0,0,0,86,105,100,101,111,72,97,110,100,108,101,114,0]),n=new Uint8Array([0,0,0,0,0,0,0,0,115,111,117,110,0,0,0,0,0,0,0,0,0,0,0,0,83,111,117,110,100,72,97,110,100,108,101,114,0]);e.HDLR_TYPES={video:r,audio:n};var i=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,12,117,114,108,32,0,0,0,1]),a=new Uint8Array([0,0,0,0,0,0,0,0]);e.STTS=e.STSC=e.STCO=a,e.STSZ=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0]),e.VMHD=new Uint8Array([0,0,0,1,0,0,0,0,0,0,0,0]),e.SMHD=new Uint8Array([0,0,0,0,0,0,0,0]),e.STSD=new Uint8Array([0,0,0,0,0,0,0,1]);var s=new Uint8Array([105,115,111,109]),o=new Uint8Array([97,118,99,49]),u=new Uint8Array([0,0,0,1]);e.FTYP=e.box(e.types.ftyp,s,u,s,o),e.DINF=e.box(e.types.dinf,e.box(e.types.dref,i))}},{key:"box",value:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n>24&255,i[1]=a>>16&255,i[2]=a>>8&255,i[3]=255&a,i.set(e,4),s=0,a=8;s>24&255,t>>16&255,t>>8&255,255&t,r>>24,r>>16&255,r>>8&255,255&r,85,196,0,0]))}},{key:"mdia",value:function(t){return e.box(e.types.mdia,e.mdhd(t.timescale,t.duration),e.hdlr(t.type),e.minf(t))}},{key:"mfhd",value:function(t){return e.box(e.types.mfhd,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t]))}},{key:"minf",value:function(t){return"audio"===t.type?e.box(e.types.minf,e.box(e.types.smhd,e.SMHD),e.DINF,e.stbl(t)):e.box(e.types.minf,e.box(e.types.vmhd,e.VMHD),e.DINF,e.stbl(t))}},{key:"moof",value:function(t,r,n){return e.box(e.types.moof,e.mfhd(t),e.traf(n,r))}},{key:"moov",value:function(t,r,n){for(var i=t.length,a=[];i--;)a[i]=e.trak(t[i]);return e.box.apply(null,[e.types.moov,e.mvhd(n,r)].concat(a).concat(e.mvex(t)))}},{key:"mvex",value:function(t){for(var r=t.length,n=[];r--;)n[r]=e.trex(t[r]);return e.box.apply(null,[e.types.mvex].concat(n))}},{key:"mvhd",value:function(t,r){var n=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,2,t>>24&255,t>>16&255,t>>8&255,255&t,r>>24&255,r>>16&255,r>>8&255,255&r,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255]);return e.box(e.types.mvhd,n)}},{key:"sdtp",value:function(t){var r,n,i=t.samples||[],a=new Uint8Array(4+i.length);for(n=0;n>>8&255),a.push(255&i),a=a.concat(Array.prototype.slice.call(n));for(r=0;r>>8&255),s.push(255&i),s=s.concat(Array.prototype.slice.call(n));var o=e.box(e.types.avcC,new Uint8Array([1,a[3],a[4],a[5],255,224|t.sps.length].concat(a).concat([t.pps.length]).concat(s))),u=t.width,l=t.height;return e.box(e.types.avc1,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,u>>8&255,255&u,l>>8&255,255&l,0,72,0,0,0,72,0,0,0,0,0,0,0,1,18,98,105,110,101,108,112,114,111,46,114,117,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,17,17]),o,e.box(e.types.btrt,new Uint8Array([0,28,156,128,0,45,198,192,0,45,198,192])))}},{key:"esds",value:function(e){var t=e.config.byteLength,r=new Uint8Array(26+t+3);return r.set([0,0,0,0,3,23+t,0,1,0,4,15+t,64,21,0,0,0,0,0,0,0,0,0,0,0,5,t]),r.set(e.config,26),r.set([6,1,2],26+t),r}},{key:"mp4a",value:function(t){var r=t.audiosamplerate;return e.box(e.types.mp4a,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,t.channelCount,0,16,0,0,0,0,r>>8&255,255&r,0,0]),e.box(e.types.esds,e.esds(t)))}},{key:"stsd",value:function(t){return"audio"===t.type?e.box(e.types.stsd,e.STSD,e.mp4a(t)):e.box(e.types.stsd,e.STSD,e.avc1(t))}},{key:"tkhd",value:function(t){var r=t.id,n=t.duration,i=t.width,a=t.height,s=t.volume;return e.box(e.types.tkhd,new Uint8Array([0,0,0,7,0,0,0,0,0,0,0,0,r>>24&255,r>>16&255,r>>8&255,255&r,0,0,0,0,n>>24,n>>16&255,n>>8&255,255&n,0,0,0,0,0,0,0,0,0,0,0,0,s>>0&255,s%1*10>>0&255,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,i>>8&255,255&i,0,0,a>>8&255,255&a,0,0]))}},{key:"traf",value:function(t,r){var n=e.sdtp(t),i=t.id;return e.box(e.types.traf,e.box(e.types.tfhd,new Uint8Array([0,0,0,0,i>>24,i>>16&255,i>>8&255,255&i])),e.box(e.types.tfdt,new Uint8Array([0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r])),e.trun(t,n.length+16+16+8+16+8+8),n)}},{key:"trak",value:function(t){return t.duration=t.duration||4294967295,e.box(e.types.trak,e.tkhd(t),e.mdia(t))}},{key:"trex",value:function(t){var r=t.id;return e.box(e.types.trex,new Uint8Array([0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]))}},{key:"trun",value:function(t,r){var n,i,a,s,o,u,l=t.samples||[],f=l.length,c=12+16*f,d=new Uint8Array(c);for(r+=8+c,d.set([0,0,15,1,f>>>24&255,f>>>16&255,f>>>8&255,255&f,r>>>24&255,r>>>16&255,r>>>8&255,255&r],0),n=0;n>>24&255,a>>>16&255,a>>>8&255,255&a,s>>>24&255,s>>>16&255,s>>>8&255,255&s,o.isLeading<<2|o.dependsOn,o.isDependedOn<<6|o.hasRedundancy<<4|o.paddingValue<<1|o.isNonSync,61440&o.degradPrio,15&o.degradPrio,u>>>24&255,u>>>16&255,u>>>8&255,255&u],12+16*n);return e.box(e.types.trun,d)}},{key:"initSegment",value:function(t,r,n){e.types||e.init();var i,a=e.moov(t,r,n);return i=new Uint8Array(e.FTYP.byteLength+a.byteLength),i.set(e.FTYP),i.set(a,e.FTYP.byteLength),i}}]),e}(),m=1,g=function(){function e(){o(this,e),this.seq=1}return u(e,null,[{key:"getTrackID",value:function(){return m++}}]),u(e,[{key:"flush",value:function(){this.seq++,this.mp4track.len=0,this.mp4track.samples=[]}},{key:"isReady",value:function(){return!(!this.readyToDecode||!this.samples.length)||null}}]),e}(),b=function(e){function r(){o(this,r);var e=f(this,(r.__proto__||Object.getPrototypeOf(r)).call(this));return e.readyToDecode=!1,e.nextDts=0,e.dts=0,e.timescale=1e3,e.mp4track={id:g.getTrackID(),type:"audio",channelCount:0,len:0,fragmented:!0,timescale:e.timescale,duration:e.timescale,samples:[],config:"",codec:""},e.samples=[],e.aac=new p(e),e}return l(r,e),u(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.codec="",this.mp4track.channelCount="",this.mp4track.config="",this.mp4track.timescale=this.timescale}},{key:"remux",value:function(e){if(e.length>0)for(var t=0;t0&&this.readyToDecode&&(this.mp4track.len+=u,this.samples.push({units:o,size:u,keyFrame:s.keyFrame,duration:s.duration}))}}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}}},{key:"getPayload",value:function(){if(!this.isReady())return null;var e=new Uint8Array(this.mp4track.len),r=0,n=this.mp4track.samples,i=void 0,a=void 0;for(this.dts=this.nextDts;this.samples.length;){var s=this.samples.shift(),o=s.units;if((a=s.duration)<=0)t("remuxer: invalid sample duration at DTS: "+this.nextDts+" :"+a),this.mp4track.len-=s.size;else{this.nextDts+=a,i={size:s.size,duration:a,cts:0,flags:{isLeading:0,isDependedOn:0,hasRedundancy:0,degradPrio:0,isNonSync:s.keyFrame?0:1,dependsOn:s.keyFrame?2:1}};var u=!0,l=!1,f=void 0;try{for(var c,d=o[Symbol.iterator]();!(u=(c=d.next()).done);u=!0){var h=c.value;e.set(h.getData(),r),r+=h.getSize()}}catch(e){l=!0,f=e}finally{try{!u&&d.return&&d.return()}finally{if(l)throw f}}n.push(i)}}return n.length?new Uint8Array(e.buffer,0,this.mp4track.len):null}}]),r}(g),x=function(e){function r(e){o(this,r);var t=f(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,"remuxer"));return t.initialized=!1,t.trackTypes=[],t.tracks={},t.mediaDuration=e?1/0:1e3,t}return l(r,e),u(r,[{key:"addTrack",value:function(e){"video"!==e&&"both"!==e||(this.tracks.video=new S,this.trackTypes.push("video")),"audio"!==e&&"both"!==e||(this.tracks.audio=new b,this.trackTypes.push("audio"))}},{key:"reset",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,i=this.trackTypes[Symbol.iterator]();!(e=(n=i.next()).done);e=!0){var a=n.value;this.tracks[a].resetTrack()}}catch(e){t=!0,r=e}finally{try{!e&&i.return&&i.return()}finally{if(t)throw r}}this.initialized=!1}},{key:"destroy",value:function(){this.tracks={},this.offAll()}},{key:"flush",value:function(){if(this.initialized){var e=!0,r=!1,a=void 0;try{for(var s,o=this.trackTypes[Symbol.iterator]();!(e=(s=o.next()).done);e=!0){var u=s.value,l=this.tracks[u],f=l.getPayload();if(f&&f.byteLength){var c=k.moof(l.seq,l.dts,l.mp4track),d=k.mdat(f),h=n(c,d),y={type:u,payload:h,dts:l.dts};this.dispatch("buffer",y);var p=i(l.dts/1e3);t("put segment ("+u+"): "+l.seq+" dts: "+l.dts+" gop: "+l.mp4track.samples.length+" second: "+p),l.flush()}}}catch(e){r=!0,a=e}finally{try{!e&&o.return&&o.return()}finally{if(r)throw a}}}else if(this.isReady()){this.dispatch("ready");var v=!0,m=!1,g=void 0;try{for(var b,S=this.trackTypes[Symbol.iterator]();!(v=(b=S.next()).done);v=!0){var x=b.value,w=this.tracks[x],B={type:x,payload:k.initSegment([w.mp4track],this.mediaDuration,w.mp4track.timescale)};this.dispatch("buffer",B)}}catch(e){m=!0,g=e}finally{try{!v&&S.return&&S.return()}finally{if(m)throw g}}t("Initial segment generated."),this.initialized=!0}}},{key:"isReady",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,i=this.trackTypes[Symbol.iterator]();!(e=(n=i.next()).done);e=!0){var a=n.value;if(!this.tracks[a].readyToDecode||!this.tracks[a].samples.length)return!1}}catch(e){t=!0,r=e}finally{try{!e&&i.return&&i.return()}finally{if(t)throw r}}return!0}},{key:"remux",value:function(e){var t=!0,r=!1,n=void 0;try{for(var i,a=this.trackTypes[Symbol.iterator]();!(t=(i=a.next()).done);t=!0){var s=i.value,o=e[s];"audio"===s&&this.tracks.video&&!this.tracks.video.readyToDecode||o.length>0&&this.tracks[s].remux(o)}}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}this.flush()}}]),r}(v),w=function(e){function i(e,t){o(this,i);var r=f(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,"buffer"));return r.type=t,r.queue=new Uint8Array,r.cleaning=!1,r.pendingCleaning=0,r.cleanOffset=30,r.cleanRanges=[],r.sourceBuffer=e,r.sourceBuffer.addEventListener("updateend",function(){if(r.pendingCleaning>0&&(r.initCleanup(r.pendingCleaning),r.pendingCleaning=0),r.cleaning=!1,r.cleanRanges.length)return void r.doCleanup()}),r.sourceBuffer.addEventListener("error",function(){r.dispatch("error",{type:r.type,name:"buffer",error:"buffer error"})}),r}return l(i,e),u(i,[{key:"destroy",value:function(){this.queue=null,this.sourceBuffer=null,this.offAll()}},{key:"doCleanup",value:function(){if(!this.cleanRanges.length)return void(this.cleaning=!1);var e=this.cleanRanges.shift();t(this.type+" remove range ["+e[0]+" - "+e[1]+")"),this.cleaning=!0,this.sourceBuffer.remove(e[0],e[1])}},{key:"initCleanup",value:function(e){if(this.sourceBuffer.updating)return void(this.pendingCleaning=e);if(this.sourceBuffer.buffered&&this.sourceBuffer.buffered.length&&!this.cleaning){for(var t=0;tthis.cleanOffset&&(n=e-this.cleanOffset,r0&&(a.video=this.getVideoFrames(n,i),t=!0)),e.audio&&(n=p.extractAAC(e.audio),n.length>0&&(a.audio=this.getAudioFrames(n,i),t=!0)),!t)return void r("Input object must have video and/or audio property. Make sure it is a valid typed array");this.remuxController.remux(a)}}},{key:"getVideoFrames",value:function(e,r){var n=this,i=[],a=[],s=0,o=0,u=!1,l=!1,f=!0,d=!1,y=void 0;try{for(var p,v=e[Symbol.iterator]();!(f=(p=v.next()).done);f=!0){var k=p.value,m=new c(k);m.type()!==c.IDR&&m.type()!==c.NDR||h.parseHeader(m),i.length&&l&&(m.isfmb||!m.isvcl)&&(a.push({units:i,keyFrame:u}),i=[],u=!1,l=!1),i.push(m),u=u||m.isKeyframe(),l=l||m.isvcl}}catch(e){d=!0,y=e}finally{try{!f&&v.return&&v.return()}finally{if(d)throw y}}if(i.length)if(l||!a.length)a.push({units:i,keyFrame:u});else{var g=a.length-1;a[g].units=a[g].units.concat(i)}return s=r?r/a.length|0:this.frameDuration,o=r?r-s*a.length:0,a.map(function(e){e.duration=s,o>0&&(e.duration++,o--),n.kfCounter++,e.keyFrame&&n.options.clearBuffer&&n.kfPosition.push(n.kfCounter*s/1e3)}),t("jmuxer: No. of frames of the last chunk: "+a.length),a}},{key:"getAudioFrames",value:function(e,t){var r=[],n=0,i=0,a=!0,s=!1,o=void 0;try{for(var u,l=e[Symbol.iterator]();!(a=(u=l.next()).done);a=!0){var f=u.value;r.push({units:f})}}catch(e){s=!0,o=e}finally{try{!a&&l.return&&l.return()}finally{if(s)throw o}}return n=t?t/r.length|0:this.frameDuration,i=t?t-n*r.length:0,r.map(function(e){e.duration=n,i>0&&(e.duration++,i--)}),r}},{key:"destroy",value:function(){if(this.stopInterval(),this.mediaSource){try{this.bufferControllers&&this.mediaSource.endOfStream()}catch(e){r("mediasource is not available to end "+e.message)}this.mediaSource=null}if(this.remuxController&&(this.remuxController.destroy(),this.remuxController=null),this.bufferControllers){for(var e in this.bufferControllers)this.bufferControllers[e].destroy();this.bufferControllers=null}this.node=!1,this.mseReady=!1,this.videoStarted=!1}},{key:"createBuffer",value:function(){if(this.mseReady&&this.remuxController&&this.remuxController.isReady()&&!this.bufferControllers){this.bufferControllers={};for(var e in this.remuxController.tracks){var t=this.remuxController.tracks[e];if(!i.isSupported(e+'/mp4; codecs="'+t.mp4track.codec+'"'))return r("Browser does not support codec"),!1;var n=this.mediaSource.addSourceBuffer(e+'/mp4; codecs="'+t.mp4track.codec+'"');this.bufferControllers[e]=new w(n,e),this.sourceBuffers[e]=n,this.bufferControllers[e].on("error",this.onBufferError.bind(this))}}}},{key:"startInterval",value:function(){var e=this;this.interval=setInterval(function(){e.bufferControllers&&(e.releaseBuffer(),e.clearBuffer())},this.options.flushingTime)}},{key:"stopInterval",value:function(){this.interval&&clearInterval(this.interval)}},{key:"releaseBuffer",value:function(){for(var e in this.bufferControllers)this.bufferControllers[e].doAppend()}},{key:"getSafeClearOffsetOfBuffer",value:function(e){for(var t="audio"===this.options.mode&&e||0,r=void 0,n=0;n=e);n++)r=this.kfPosition[n];return r&&(this.kfPosition=this.kfPosition.filter(function(e){return e=r})),t}},{key:"clearBuffer",value:function(){if(this.options.clearBuffer&&Date.now()-this.lastCleaningTime>1e4){for(var e in this.bufferControllers){var t=this.getSafeClearOffsetOfBuffer(this.node.currentTime);this.bufferControllers[e].initCleanup(t)}this.lastCleaningTime=Date.now()}}},{key:"onBuffer",value:function(e){this.bufferControllers&&this.bufferControllers[e.type]&&this.bufferControllers[e.type].feed(e.payload)}},{key:"onMSEOpen",value:function(){this.mseReady=!0,"function"==typeof this.options.onReady&&(this.options.onReady(),this.options.onReady=null),this.createBuffer()}},{key:"onMSEClose",value:function(){this.mseReady=!1,this.videoStarted=!1}},{key:"onBufferError",value:function(e){if("QuotaExceeded"==e.name)return void this.bufferControllers[e.type].initCleanup(this.node.currentTime);if(this.mediaSource.sourceBuffers.length>0&&this.sourceBuffers[e.type]&&this.mediaSource.removeSourceBuffer(this.sourceBuffers[e.type]),0==this.mediaSource.sourceBuffers.length)try{this.mediaSource.endOfStream()}catch(e){r("mediasource is not available to end")}}}]),i}(v)}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).JMuxer=t()}(this,function(){"use strict";function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var r=0;re.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,s=!0,o=!1;return{s:function(){r=e[Symbol.iterator]()},n:function(){var e=r.next();return s=e.done,e},e:function(e){o=!0,a=e},f:function(){try{s||null==r.return||r.return()}finally{if(o)throw a}}}}var l,d;function g(e){if(l){for(var t=arguments.length,r=new Array(1>5,this.ntype=31&this.payload[0],this.isvcl=1==this.ntype||5==this.ntype,this.stype="",this.isfmb=!1}return o(t,null,[{key:"type",value:function(e){return e.ntype in t.TYPES?t.TYPES[e.ntype]:"UNKNOWN"}},{key:"NDR",get:function(){return 1}},{key:"IDR",get:function(){return 5}},{key:"SEI",get:function(){return 6}},{key:"SPS",get:function(){return 7}},{key:"PPS",get:function(){return 8}},{key:"AUD",get:function(){return 9}},{key:"TYPES",get:function(){var e={};return r(e,t.IDR,"IDR"),r(e,t.SEI,"SEI"),r(e,t.SPS,"SPS"),r(e,t.PPS,"PPS"),r(e,t.NDR,"NDR"),r(e,t.AUD,"AUD"),e}}]),o(t,[{key:"toString",value:function(){return"".concat(t.type(this),": NRI: ").concat(this.getNri())}},{key:"getNri",value:function(){return this.nri}},{key:"type",value:function(){return this.ntype}},{key:"isKeyframe",value:function(){return this.ntype===t.IDR||7===this.stype}},{key:"getPayload",value:function(){return this.payload}},{key:"getPayloadSize",value:function(){return this.payload.byteLength}},{key:"getSize",value:function(){return 4+this.getPayloadSize()}},{key:"getData",value:function(){var e=new Uint8Array(this.getSize());return new DataView(e.buffer).setUint32(0,this.getSize()-4),e.set(this.getPayload(),4),e}}]),t}(),b=function(){function t(e){a(this,t),this.data=e,this.index=0,this.bitLength=8*e.byteLength}return o(t,[{key:"skipBits",value:function(e){if(this.bitsAvailable>>i,s=8-i;if(e<=s)return n&&(this.index+=e),a>>s-e;n&&(this.index+=s);var o=e-s;return a<>>1:-1*(e>>>1)}},{key:"readBoolean",value:function(){return 1===this.readBits(1)}},{key:"readUByte",value:function(e){var t=0>>5}},{key:"isAACPattern",value:function(e){return 255===e[0]&&240==(240&e[1])&&0==(6&e[1])}},{key:"extractAAC",value:function(e){var t,r,n=0,i=e.byteLength,a=[];if(!s.isAACPattern(e))return h("Invalid ADTS audio format"),a;for(t=s.getHeaderLength(e),y=y||e.subarray(0,t);n>>6),t=(60&i[2])>>>2,r=(1&i[2])<<2,r|=(192&i[3])>>>6,n[0]=e<<3,n[0]|=(14&t)>>1,n[1]|=(1&t)<<7,n[1]|=r<<3,this.track.codec="mp4a.40."+e,this.track.channelCount=r,this.track.config=n,this.remuxer.readyToDecode=!0)}}]),s}(),t=function(){function t(e){a(this,t),this.listener={},this.type=""|e}return o(t,[{key:"on",value:function(e,t){return this.listener[e]||(this.listener[e]=[]),this.listener[e].push(t),!0}},{key:"off",value:function(e,t){if(this.listener[e]){var r=this.listener[e].indexOf(t);return-1>24&255,i[1]=a>>16&255,i[2]=a>>8&255,i[3]=255&a,i.set(e,4),s=0,a=8;s>24&255,e>>16&255,e>>8&255,255&e,t>>24,t>>16&255,t>>8&255,255&t,85,196,0,0]))}},{key:"mdia",value:function(e){return d.box(d.types.mdia,d.mdhd(e.timescale,e.duration),d.hdlr(e.type),d.minf(e))}},{key:"mfhd",value:function(e){return d.box(d.types.mfhd,new Uint8Array([0,0,0,0,e>>24,e>>16&255,e>>8&255,255&e]))}},{key:"minf",value:function(e){return"audio"===e.type?d.box(d.types.minf,d.box(d.types.smhd,d.SMHD),d.DINF,d.stbl(e)):d.box(d.types.minf,d.box(d.types.vmhd,d.VMHD),d.DINF,d.stbl(e))}},{key:"moof",value:function(e,t,r){return d.box(d.types.moof,d.mfhd(e),d.traf(r,t))}},{key:"moov",value:function(e,t,r){for(var n=e.length,i=[];n--;)i[n]=d.trak(e[n]);return d.box.apply(null,[d.types.moov,d.mvhd(r,t)].concat(i).concat(d.mvex(e)))}},{key:"mvex",value:function(e){for(var t=e.length,r=[];t--;)r[t]=d.trex(e[t]);return d.box.apply(null,[d.types.mvex].concat(r))}},{key:"mvhd",value:function(e,t){var r=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,2,e>>24&255,e>>16&255,e>>8&255,255&e,t>>24&255,t>>16&255,t>>8&255,255&t,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255]);return d.box(d.types.mvhd,r)}},{key:"sdtp",value:function(e){for(var t,r=e.samples||[],n=new Uint8Array(4+r.length),i=0;i>>8&255),n.push(255&r),n=n.concat(Array.prototype.slice.call(t));for(a=0;a>>8&255),i.push(255&r),i=i.concat(Array.prototype.slice.call(t));var s=d.box(d.types.avcC,new Uint8Array([1,n[3],n[4],n[5],255,224|e.sps.length].concat(n).concat([e.pps.length]).concat(i))),o=e.width,u=e.height;return d.box(d.types.avc1,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,o>>8&255,255&o,u>>8&255,255&u,0,72,0,0,0,72,0,0,0,0,0,0,0,1,18,98,105,110,101,108,112,114,111,46,114,117,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,17,17]),s,d.box(d.types.btrt,new Uint8Array([0,28,156,128,0,45,198,192,0,45,198,192])))}},{key:"esds",value:function(e){var t=e.config.byteLength,r=new Uint8Array(26+t+3);return r.set([0,0,0,0,3,23+t,0,1,0,4,15+t,64,21,0,0,0,0,0,0,0,0,0,0,0,5,t]),r.set(e.config,26),r.set([6,1,2],26+t),r}},{key:"mp4a",value:function(e){var t=e.audiosamplerate;return d.box(d.types.mp4a,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,e.channelCount,0,16,0,0,0,0,t>>8&255,255&t,0,0]),d.box(d.types.esds,d.esds(e)))}},{key:"stsd",value:function(e){return"audio"===e.type?d.box(d.types.stsd,d.STSD,d.mp4a(e)):d.box(d.types.stsd,d.STSD,d.avc1(e))}},{key:"tkhd",value:function(e){var t=e.id,r=e.duration,n=e.width,i=e.height,a=e.volume;return d.box(d.types.tkhd,new Uint8Array([0,0,0,7,0,0,0,0,0,0,0,0,t>>24&255,t>>16&255,t>>8&255,255&t,0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r,0,0,0,0,0,0,0,0,0,0,0,0,a>>0&255,a%1*10>>0&255,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,n>>8&255,255&n,0,0,i>>8&255,255&i,0,0]))}},{key:"traf",value:function(e,t){var r=d.sdtp(e),n=e.id;return d.box(d.types.traf,d.box(d.types.tfhd,new Uint8Array([0,0,0,0,n>>24,n>>16&255,n>>8&255,255&n])),d.box(d.types.tfdt,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t])),d.trun(e,r.length+16+16+8+16+8+8),r)}},{key:"trak",value:function(e){return e.duration=e.duration||4294967295,d.box(d.types.trak,d.tkhd(e),d.mdia(e))}},{key:"trex",value:function(e){var t=e.id;return d.box(d.types.trex,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]))}},{key:"trun",value:function(e,t){var r,n,i,a,s,o,u=e.samples||[],c=u.length,f=12+16*c,l=new Uint8Array(f);for(t+=8+f,l.set([0,0,15,1,c>>>24&255,c>>>16&255,c>>>8&255,255&c,t>>>24&255,t>>>16&255,t>>>8&255,255&t],0),r=0;r>>24&255,i>>>16&255,i>>>8&255,255&i,a>>>24&255,a>>>16&255,a>>>8&255,255&a,s.isLeading<<2|s.dependsOn,s.isDependedOn<<6|s.hasRedundancy<<4|s.paddingValue<<1|s.isNonSync,61440&s.degradPrio,15&s.degradPrio,o>>>24&255,o>>>16&255,o>>>8&255,255&o],12+16*r);return d.box(d.types.trun,l)}},{key:"initSegment",value:function(e,t,r){d.types||d.init();var n=d.moov(e,t,r),i=new Uint8Array(d.FTYP.byteLength+n.byteLength);return i.set(d.FTYP),i.set(n,d.FTYP.byteLength),i}}]),d}(),w=1,x=function(){function e(){a(this,e),this.seq=1}return o(e,null,[{key:"getTrackID",value:function(){return w++}}]),o(e,[{key:"flush",value:function(){this.seq++,this.mp4track.len=0,this.mp4track.samples=[]}},{key:"isReady",value:function(){return!(!this.readyToDecode||!this.samples.length)||null}}]),e}(),A=function(){e(r,x);var t=s(r);function r(){var e;return a(this,r),(e=t.call(this)).readyToDecode=!1,e.nextDts=0,e.dts=0,e.timescale=1e3,e.mp4track={id:x.getTrackID(),type:"audio",channelCount:0,len:0,fragmented:!0,timescale:e.timescale,duration:e.timescale,samples:[],config:"",codec:""},e.samples=[],e.aac=new m(c(e)),e}return o(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.codec="",this.mp4track.channelCount="",this.mp4track.config="",this.mp4track.timescale=this.timescale}},{key:"remux",value:function(e){if(0this.cleanOffset&&r<(n=e-this.cleanOffset)&&this.cleanRanges.push([r,n])}this.doCleanup()}}},{key:"doAppend",value:function(){if(this.queue.length&&!this.sourceBuffer.updating)try{this.sourceBuffer.appendBuffer(this.queue),this.queue=new Uint8Array}catch(e){if("QuotaExceededError"===e.name)return g("".concat(this.type," buffer quota full")),void this.dispatch("error",{type:this.type,name:"QuotaExceeded",error:"buffer error"});h("Error occured while appending ".concat(this.type," buffer - ").concat(e.name,": ").concat(e.message)),this.dispatch("error",{type:this.type,name:"unexpectedError",error:"buffer error"})}}},{key:"feed",value:function(e){this.queue=U(this.queue,e)}}]),i}();return window.MediaSource=window.MediaSource||window.WebKitMediaSource,function(){e(n,t);var r=s(n);function n(e){var t;a(this,n),t=r.call(this,"jmuxer"),window.MediaSource=window.MediaSource||window.WebKitMediaSource;if(t.options=Object.assign({},{node:"",mode:"both",flushingTime:1500,clearBuffer:!0,onReady:null,fps:30,debug:!1},e),t.options.debug&&(l=console.log,d=console.error),"string"==typeof t.options.node&&""==t.options.node&&h("no video element were found to render, provide a valid video element"),t.options.fps||(t.options.fps=30),t.frameDuration=1e3/t.options.fps|0,t.node="string"==typeof t.options.node?document.getElementById(t.options.node):t.options.node,t.sourceBuffers={},t.isMSESupported=!!window.MediaSource,!t.isMSESupported)throw"Oops! Browser does not support media source extension.";return t.setupMSE(),t.remuxController=new C(t.options.clearBuffer),t.remuxController.addTrack(t.options.mode),t.mseReady=!1,t.lastCleaningTime=Date.now(),t.kfPosition=[],t.kfCounter=0,t.remuxController.on("buffer",t.onBuffer.bind(c(t))),t.remuxController.on("ready",t.createBuffer.bind(c(t))),t.startInterval(),t}return o(n,null,[{key:"isSupported",value:function(e){return window.MediaSource&&window.MediaSource.isTypeSupported(e)}}]),o(n,[{key:"setupMSE",value:function(){this.mediaSource=new MediaSource,this.node.src=URL.createObjectURL(this.mediaSource),this.mediaSource.addEventListener("sourceopen",this.onMSEOpen.bind(this)),this.mediaSource.addEventListener("sourceclose",this.onMSEClose.bind(this)),this.mediaSource.addEventListener("webkitsourceopen",this.onMSEOpen.bind(this)),this.mediaSource.addEventListener("webkitsourceclose",this.onMSEClose.bind(this))}},{key:"feed",value:function(e){var t,r,n=!1,i={video:[],audio:[]};e&&this.remuxController&&(r=e.duration?parseInt(e.duration):0,e.video&&0<(t=v.extractNALu(e.video)).length&&(i.video=this.getVideoFrames(t,r),n=!0),e.audio&&0<(t=m.extractAAC(e.audio)).length&&(i.audio=this.getAudioFrames(t,r),n=!0),n?this.remuxController.remux(i):h("Input object must have video and/or audio property. Make sure it is a valid typed array"))}},{key:"getVideoFrames",value:function(e,t){var r,n,i,a=this,s=[],o=[],u=0,c=!1,f=!1,l=k(e);try{for(l.s();!(n=l.n()).done;){var d=n.value,h=new p(d);h.type()!==p.IDR&&h.type()!==p.NDR||v.parseHeader(h),s.length&&f&&(h.isfmb||!h.isvcl)&&(o.push({units:s,keyFrame:c}),f=c=!(s=[])),s.push(h),c=c||h.isKeyframe(),f=f||h.isvcl}}catch(e){l.e(e)}finally{l.f()}return s.length&&(f||!o.length?o.push({units:s,keyFrame:c}):o[i=o.length-1].units=o[i].units.concat(s)),r=t?t/o.length|0:this.frameDuration,u=t?t-r*o.length:0,o.map(function(e){e.duration=r,0=e);n++)t=this.kfPosition[n];return t&&(this.kfPosition=this.kfPosition.filter(function(e){return e1?t-1:0),n=1;n1?t-1:0),n=1;n0&&(i+=(r<10?"0"+r:r)+":"),i+=(n<10?"0"+n:n)+":"+(t<10?"0"+t:t)}var a=void 0,s=void 0,o=(function(){function e(e){this.value=e}function t(t){function r(e,t){return new Promise(function(r,i){var o={key:e,arg:t,resolve:r,reject:i,next:null};s?s=s.next=o:(a=s=o,n(e,t))})}function n(r,a){try{var s=t[r](a),o=s.value;o instanceof e?Promise.resolve(o.value).then(function(e){n("next",e)},function(e){n("throw",e)}):i(s.done?"return":"normal",s.value)}catch(e){i("throw",e)}}function i(e,t){switch(e){case"return":a.resolve({value:t,done:!0});break;case"throw":a.reject(t);break;default:a.resolve({value:t,done:!1})}a=a.next,a?n(a.key,a.arg):s=null}var a,s;this._invoke=r,"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),u=function(){function e(e,t){for(var r=0;r>5,this.ntype=31&this.payload[0],this.isvcl=1==this.ntype||5==this.ntype,this.stype="",this.isfmb=!1}return u(e,null,[{key:"type",value:function(t){return t.ntype in e.TYPES?e.TYPES[t.ntype]:"UNKNOWN"}},{key:"NDR",get:function(){return 1}},{key:"IDR",get:function(){return 5}},{key:"SEI",get:function(){return 6}},{key:"SPS",get:function(){return 7}},{key:"PPS",get:function(){return 8}},{key:"AUD",get:function(){return 9}},{key:"TYPES",get:function(){var t;return t={},babelHelpers.defineProperty(t,e.IDR,"IDR"),babelHelpers.defineProperty(t,e.SEI,"SEI"),babelHelpers.defineProperty(t,e.SPS,"SPS"),babelHelpers.defineProperty(t,e.PPS,"PPS"),babelHelpers.defineProperty(t,e.NDR,"NDR"),babelHelpers.defineProperty(t,e.AUD,"AUD"),t}}]),u(e,[{key:"toString",value:function(){return e.type(this)+": NRI: "+this.getNri()}},{key:"getNri",value:function(){return this.nri}},{key:"type",value:function(){return this.ntype}},{key:"isKeyframe",value:function(){return this.ntype===e.IDR||7===this.stype}},{key:"getPayload",value:function(){return this.payload}},{key:"getPayloadSize",value:function(){return this.payload.byteLength}},{key:"getSize",value:function(){return 4+this.getPayloadSize()}},{key:"getData",value:function(){var e=new Uint8Array(this.getSize());return new DataView(e.buffer).setUint32(0,this.getSize()-4),e.set(this.getPayload(),4),e}}]),e}(),d=function(){function e(t){o(this,e),this.data=t,this.index=0,this.bitLength=8*t.byteLength}return u(e,[{key:"skipBits",value:function(e){if(this.bitsAvailable1&&void 0!==arguments[1])||arguments[1];return this.getBits(e,this.index,t)}},{key:"getBits",value:function(e,t){var r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];if(this.bitsAvailable>>n,a=8-n;if(a>=e)return r&&(this.index+=e),i>>a-e;r&&(this.index+=a);var s=e-a;return i<>>1:-1*(e>>>1)}},{key:"readBoolean",value:function(){return 1===this.readBits(1)}},{key:"readUByte",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return this.readBits(8*e)}},{key:"readUShort",value:function(){return this.readBits(16)}},{key:"readUInt",value:function(){return this.readBits(32)}},{key:"bitsAvailable",get:function(){return this.bitLength-this.index}}]),e}(),h=function(){function e(t){o(this,e),this.remuxer=t,this.track=t.mp4track}return u(e,null,[{key:"extractNALu",value:function(e){for(var t=0,r=e.byteLength,n=void 0,i=0,a=[],s=void 0;t>>5}},{key:"isAACPattern",value:function(e){return 255===e[0]&&240==(240&e[1])&&0==(6&e[1])}},{key:"extractAAC",value:function(t){var n=0,i=t.byteLength,a=[],s=void 0,o=void 0;if(!e.isAACPattern(t))return r("Invalid ADTS audio format"),a;for(s=e.getHeaderLength(t),y||(y=t.subarray(0,s));n>>6),r=(60&a[2])>>>2,n=(1&a[2])<<2,n|=(192&a[3])>>>6,i[0]=t<<3,i[0]|=(14&r)>>1,i[1]|=(1&r)<<7,i[1]|=n<<3,this.track.codec="mp4a.40."+t,this.track.channelCount=n,this.track.config=i,this.remuxer.readyToDecode=!0)}}]),e}(),v=function(){function e(t){o(this,e),this.listener={},this.type=""|t}return u(e,[{key:"on",value:function(e,t){return this.listener[e]||(this.listener[e]=[]),this.listener[e].push(t),!0}},{key:"off",value:function(e,t){if(this.listener[e]){var r=this.listener[e].indexOf(t);return r>-1&&this.listener[e].splice(r,1),!0}return!1}},{key:"offAll",value:function(){this.listener={}}},{key:"dispatch",value:function(e,t){return!!this.listener[e]&&(this.listener[e].map(function(e){e.apply(null,[t])}),!0)}}]),e}(),k=function(){function e(){o(this,e)}return u(e,null,[{key:"init",value:function(){e.types={avc1:[],avcC:[],btrt:[],dinf:[],dref:[],esds:[],ftyp:[],hdlr:[],mdat:[],mdhd:[],mdia:[],mfhd:[],minf:[],moof:[],moov:[],mp4a:[],mvex:[],mvhd:[],sdtp:[],stbl:[],stco:[],stsc:[],stsd:[],stsz:[],stts:[],tfdt:[],tfhd:[],traf:[],trak:[],trun:[],trex:[],tkhd:[],vmhd:[],smhd:[]};var t;for(t in e.types)e.types.hasOwnProperty(t)&&(e.types[t]=[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]);var r=new Uint8Array([0,0,0,0,0,0,0,0,118,105,100,101,0,0,0,0,0,0,0,0,0,0,0,0,86,105,100,101,111,72,97,110,100,108,101,114,0]),n=new Uint8Array([0,0,0,0,0,0,0,0,115,111,117,110,0,0,0,0,0,0,0,0,0,0,0,0,83,111,117,110,100,72,97,110,100,108,101,114,0]);e.HDLR_TYPES={video:r,audio:n};var i=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,12,117,114,108,32,0,0,0,1]),a=new Uint8Array([0,0,0,0,0,0,0,0]);e.STTS=e.STSC=e.STCO=a,e.STSZ=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0]),e.VMHD=new Uint8Array([0,0,0,1,0,0,0,0,0,0,0,0]),e.SMHD=new Uint8Array([0,0,0,0,0,0,0,0]),e.STSD=new Uint8Array([0,0,0,0,0,0,0,1]);var s=new Uint8Array([105,115,111,109]),o=new Uint8Array([97,118,99,49]),u=new Uint8Array([0,0,0,1]);e.FTYP=e.box(e.types.ftyp,s,u,s,o),e.DINF=e.box(e.types.dinf,e.box(e.types.dref,i))}},{key:"box",value:function(e){for(var t=arguments.length,r=Array(t>1?t-1:0),n=1;n>24&255,i[1]=a>>16&255,i[2]=a>>8&255,i[3]=255&a,i.set(e,4),s=0,a=8;s>24&255,t>>16&255,t>>8&255,255&t,r>>24,r>>16&255,r>>8&255,255&r,85,196,0,0]))}},{key:"mdia",value:function(t){return e.box(e.types.mdia,e.mdhd(t.timescale,t.duration),e.hdlr(t.type),e.minf(t))}},{key:"mfhd",value:function(t){return e.box(e.types.mfhd,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t]))}},{key:"minf",value:function(t){return"audio"===t.type?e.box(e.types.minf,e.box(e.types.smhd,e.SMHD),e.DINF,e.stbl(t)):e.box(e.types.minf,e.box(e.types.vmhd,e.VMHD),e.DINF,e.stbl(t))}},{key:"moof",value:function(t,r,n){return e.box(e.types.moof,e.mfhd(t),e.traf(n,r))}},{key:"moov",value:function(t,r,n){for(var i=t.length,a=[];i--;)a[i]=e.trak(t[i]);return e.box.apply(null,[e.types.moov,e.mvhd(n,r)].concat(a).concat(e.mvex(t)))}},{key:"mvex",value:function(t){for(var r=t.length,n=[];r--;)n[r]=e.trex(t[r]);return e.box.apply(null,[e.types.mvex].concat(n))}},{key:"mvhd",value:function(t,r){var n=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,2,t>>24&255,t>>16&255,t>>8&255,255&t,r>>24&255,r>>16&255,r>>8&255,255&r,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255]);return e.box(e.types.mvhd,n)}},{key:"sdtp",value:function(t){var r,n,i=t.samples||[],a=new Uint8Array(4+i.length);for(n=0;n>>8&255),a.push(255&i),a=a.concat(Array.prototype.slice.call(n));for(r=0;r>>8&255),s.push(255&i),s=s.concat(Array.prototype.slice.call(n));var o=e.box(e.types.avcC,new Uint8Array([1,a[3],a[4],a[5],255,224|t.sps.length].concat(a).concat([t.pps.length]).concat(s))),u=t.width,l=t.height;return e.box(e.types.avc1,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,u>>8&255,255&u,l>>8&255,255&l,0,72,0,0,0,72,0,0,0,0,0,0,0,1,18,98,105,110,101,108,112,114,111,46,114,117,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,17,17]),o,e.box(e.types.btrt,new Uint8Array([0,28,156,128,0,45,198,192,0,45,198,192])))}},{key:"esds",value:function(e){var t=e.config.byteLength,r=new Uint8Array(26+t+3);return r.set([0,0,0,0,3,23+t,0,1,0,4,15+t,64,21,0,0,0,0,0,0,0,0,0,0,0,5,t]),r.set(e.config,26),r.set([6,1,2],26+t),r}},{key:"mp4a",value:function(t){var r=t.audiosamplerate;return e.box(e.types.mp4a,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,t.channelCount,0,16,0,0,0,0,r>>8&255,255&r,0,0]),e.box(e.types.esds,e.esds(t)))}},{key:"stsd",value:function(t){return"audio"===t.type?e.box(e.types.stsd,e.STSD,e.mp4a(t)):e.box(e.types.stsd,e.STSD,e.avc1(t))}},{key:"tkhd",value:function(t){var r=t.id,n=t.duration,i=t.width,a=t.height,s=t.volume;return e.box(e.types.tkhd,new Uint8Array([0,0,0,7,0,0,0,0,0,0,0,0,r>>24&255,r>>16&255,r>>8&255,255&r,0,0,0,0,n>>24,n>>16&255,n>>8&255,255&n,0,0,0,0,0,0,0,0,0,0,0,0,s>>0&255,s%1*10>>0&255,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,i>>8&255,255&i,0,0,a>>8&255,255&a,0,0]))}},{key:"traf",value:function(t,r){var n=e.sdtp(t),i=t.id;return e.box(e.types.traf,e.box(e.types.tfhd,new Uint8Array([0,0,0,0,i>>24,i>>16&255,i>>8&255,255&i])),e.box(e.types.tfdt,new Uint8Array([0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r])),e.trun(t,n.length+16+16+8+16+8+8),n)}},{key:"trak",value:function(t){return t.duration=t.duration||4294967295,e.box(e.types.trak,e.tkhd(t),e.mdia(t))}},{key:"trex",value:function(t){var r=t.id;return e.box(e.types.trex,new Uint8Array([0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]))}},{key:"trun",value:function(t,r){var n,i,a,s,o,u,l=t.samples||[],f=l.length,c=12+16*f,d=new Uint8Array(c);for(r+=8+c,d.set([0,0,15,1,f>>>24&255,f>>>16&255,f>>>8&255,255&f,r>>>24&255,r>>>16&255,r>>>8&255,255&r],0),n=0;n>>24&255,a>>>16&255,a>>>8&255,255&a,s>>>24&255,s>>>16&255,s>>>8&255,255&s,o.isLeading<<2|o.dependsOn,o.isDependedOn<<6|o.hasRedundancy<<4|o.paddingValue<<1|o.isNonSync,61440&o.degradPrio,15&o.degradPrio,u>>>24&255,u>>>16&255,u>>>8&255,255&u],12+16*n);return e.box(e.types.trun,d)}},{key:"initSegment",value:function(t,r,n){e.types||e.init();var i,a=e.moov(t,r,n);return i=new Uint8Array(e.FTYP.byteLength+a.byteLength),i.set(e.FTYP),i.set(a,e.FTYP.byteLength),i}}]),e}(),m=1,g=function(){function e(){o(this,e),this.seq=1}return u(e,null,[{key:"getTrackID",value:function(){return m++}}]),u(e,[{key:"flush",value:function(){this.seq++,this.mp4track.len=0,this.mp4track.samples=[]}},{key:"isReady",value:function(){return!(!this.readyToDecode||!this.samples.length)||null}}]),e}(),b=function(e){function r(){o(this,r);var e=f(this,(r.__proto__||Object.getPrototypeOf(r)).call(this));return e.readyToDecode=!1,e.nextDts=0,e.dts=0,e.timescale=1e3,e.mp4track={id:g.getTrackID(),type:"audio",channelCount:0,len:0,fragmented:!0,timescale:e.timescale,duration:e.timescale,samples:[],config:"",codec:""},e.samples=[],e.aac=new p(e),e}return l(r,e),u(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.codec="",this.mp4track.channelCount="",this.mp4track.config="",this.mp4track.timescale=this.timescale}},{key:"remux",value:function(e){if(e.length>0)for(var t=0;t0&&this.readyToDecode&&(this.mp4track.len+=u,this.samples.push({units:o,size:u,keyFrame:s.keyFrame,duration:s.duration}))}}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}}},{key:"getPayload",value:function(){if(!this.isReady())return null;var e=new Uint8Array(this.mp4track.len),r=0,n=this.mp4track.samples,i=void 0,a=void 0;for(this.dts=this.nextDts;this.samples.length;){var s=this.samples.shift(),o=s.units;if((a=s.duration)<=0)t("remuxer: invalid sample duration at DTS: "+this.nextDts+" :"+a),this.mp4track.len-=s.size;else{this.nextDts+=a,i={size:s.size,duration:a,cts:0,flags:{isLeading:0,isDependedOn:0,hasRedundancy:0,degradPrio:0,isNonSync:s.keyFrame?0:1,dependsOn:s.keyFrame?2:1}};var u=!0,l=!1,f=void 0;try{for(var c,d=o[Symbol.iterator]();!(u=(c=d.next()).done);u=!0){var h=c.value;e.set(h.getData(),r),r+=h.getSize()}}catch(e){l=!0,f=e}finally{try{!u&&d.return&&d.return()}finally{if(l)throw f}}n.push(i)}}return n.length?new Uint8Array(e.buffer,0,this.mp4track.len):null}}]),r}(g),x=function(e){function r(e){o(this,r);var t=f(this,(r.__proto__||Object.getPrototypeOf(r)).call(this,"remuxer"));return t.initialized=!1,t.trackTypes=[],t.tracks={},t.mediaDuration=e?1/0:1e3,t}return l(r,e),u(r,[{key:"addTrack",value:function(e){"video"!==e&&"both"!==e||(this.tracks.video=new S,this.trackTypes.push("video")),"audio"!==e&&"both"!==e||(this.tracks.audio=new b,this.trackTypes.push("audio"))}},{key:"reset",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,i=this.trackTypes[Symbol.iterator]();!(e=(n=i.next()).done);e=!0){var a=n.value;this.tracks[a].resetTrack()}}catch(e){t=!0,r=e}finally{try{!e&&i.return&&i.return()}finally{if(t)throw r}}this.initialized=!1}},{key:"destroy",value:function(){this.tracks={},this.offAll()}},{key:"flush",value:function(){if(this.initialized){var e=!0,r=!1,a=void 0;try{for(var s,o=this.trackTypes[Symbol.iterator]();!(e=(s=o.next()).done);e=!0){var u=s.value,l=this.tracks[u],f=l.getPayload();if(f&&f.byteLength){var c=k.moof(l.seq,l.dts,l.mp4track),d=k.mdat(f),h=n(c,d),y={type:u,payload:h,dts:l.dts};this.dispatch("buffer",y);var p=i(l.dts/1e3);t("put segment ("+u+"): "+l.seq+" dts: "+l.dts+" gop: "+l.mp4track.samples.length+" second: "+p),l.flush()}}}catch(e){r=!0,a=e}finally{try{!e&&o.return&&o.return()}finally{if(r)throw a}}}else if(this.isReady()){this.dispatch("ready");var v=!0,m=!1,g=void 0;try{for(var b,S=this.trackTypes[Symbol.iterator]();!(v=(b=S.next()).done);v=!0){var x=b.value,w=this.tracks[x],B={type:x,payload:k.initSegment([w.mp4track],this.mediaDuration,w.mp4track.timescale)};this.dispatch("buffer",B)}}catch(e){m=!0,g=e}finally{try{!v&&S.return&&S.return()}finally{if(m)throw g}}t("Initial segment generated."),this.initialized=!0}}},{key:"isReady",value:function(){var e=!0,t=!1,r=void 0;try{for(var n,i=this.trackTypes[Symbol.iterator]();!(e=(n=i.next()).done);e=!0){var a=n.value;if(!this.tracks[a].readyToDecode||!this.tracks[a].samples.length)return!1}}catch(e){t=!0,r=e}finally{try{!e&&i.return&&i.return()}finally{if(t)throw r}}return!0}},{key:"remux",value:function(e){var t=!0,r=!1,n=void 0;try{for(var i,a=this.trackTypes[Symbol.iterator]();!(t=(i=a.next()).done);t=!0){var s=i.value,o=e[s];"audio"===s&&this.tracks.video&&!this.tracks.video.readyToDecode||o.length>0&&this.tracks[s].remux(o)}}catch(e){r=!0,n=e}finally{try{!t&&a.return&&a.return()}finally{if(r)throw n}}this.flush()}}]),r}(v),w=function(e){function i(e,t){o(this,i);var r=f(this,(i.__proto__||Object.getPrototypeOf(i)).call(this,"buffer"));return r.type=t,r.queue=new Uint8Array,r.cleaning=!1,r.pendingCleaning=0,r.cleanOffset=30,r.cleanRanges=[],r.sourceBuffer=e,r.sourceBuffer.addEventListener("updateend",function(){if(r.pendingCleaning>0&&(r.initCleanup(r.pendingCleaning),r.pendingCleaning=0),r.cleaning=!1,r.cleanRanges.length)return void r.doCleanup()}),r.sourceBuffer.addEventListener("error",function(){r.dispatch("error",{type:r.type,name:"buffer",error:"buffer error"})}),r}return l(i,e),u(i,[{key:"destroy",value:function(){this.queue=null,this.sourceBuffer=null,this.offAll()}},{key:"doCleanup",value:function(){if(!this.cleanRanges.length)return void(this.cleaning=!1);var e=this.cleanRanges.shift();t(this.type+" remove range ["+e[0]+" - "+e[1]+")"),this.cleaning=!0,this.sourceBuffer.remove(e[0],e[1])}},{key:"initCleanup",value:function(e){if(this.sourceBuffer.updating)return void(this.pendingCleaning=e);if(this.sourceBuffer.buffered&&this.sourceBuffer.buffered.length&&!this.cleaning){for(var t=0;tthis.cleanOffset&&(n=e-this.cleanOffset,r0&&(a.video=this.getVideoFrames(n,i),t=!0)),e.audio&&(n=p.extractAAC(e.audio),n.length>0&&(a.audio=this.getAudioFrames(n,i),t=!0)),!t)return void r("Input object must have video and/or audio property. Make sure it is a valid typed array");this.remuxController.remux(a)}}},{key:"getVideoFrames",value:function(e,r){var n=this,i=[],a=[],s=0,o=0,u=!1,l=!1,f=!0,d=!1,y=void 0;try{for(var p,v=e[Symbol.iterator]();!(f=(p=v.next()).done);f=!0){var k=p.value,m=new c(k);m.type()!==c.IDR&&m.type()!==c.NDR||h.parseHeader(m),i.length&&l&&(m.isfmb||!m.isvcl)&&(a.push({units:i,keyFrame:u}),i=[],u=!1,l=!1),i.push(m),u=u||m.isKeyframe(),l=l||m.isvcl}}catch(e){d=!0,y=e}finally{try{!f&&v.return&&v.return()}finally{if(d)throw y}}if(i.length)if(l||!a.length)a.push({units:i,keyFrame:u});else{var g=a.length-1;a[g].units=a[g].units.concat(i)}return s=r?r/a.length|0:this.frameDuration,o=r?r-s*a.length:0,a.map(function(e){e.duration=s,o>0&&(e.duration++,o--),n.kfCounter++,e.keyFrame&&n.options.clearBuffer&&n.kfPosition.push(n.kfCounter*s/1e3)}),t("jmuxer: No. of frames of the last chunk: "+a.length),a}},{key:"getAudioFrames",value:function(e,t){var r=[],n=0,i=0,a=!0,s=!1,o=void 0;try{for(var u,l=e[Symbol.iterator]();!(a=(u=l.next()).done);a=!0){var f=u.value;r.push({units:f})}}catch(e){s=!0,o=e}finally{try{!a&&l.return&&l.return()}finally{if(s)throw o}}return n=t?t/r.length|0:this.frameDuration,i=t?t-n*r.length:0,r.map(function(e){e.duration=n,i>0&&(e.duration++,i--)}),r}},{key:"destroy",value:function(){if(this.stopInterval(),this.mediaSource){try{this.bufferControllers&&this.mediaSource.endOfStream()}catch(e){r("mediasource is not available to end "+e.message)}this.mediaSource=null}if(this.remuxController&&(this.remuxController.destroy(),this.remuxController=null),this.bufferControllers){for(var e in this.bufferControllers)this.bufferControllers[e].destroy();this.bufferControllers=null}this.node=!1,this.mseReady=!1,this.videoStarted=!1}},{key:"createBuffer",value:function(){if(this.mseReady&&this.remuxController&&this.remuxController.isReady()&&!this.bufferControllers){this.bufferControllers={};for(var e in this.remuxController.tracks){var t=this.remuxController.tracks[e];if(!i.isSupported(e+'/mp4; codecs="'+t.mp4track.codec+'"'))return r("Browser does not support codec"),!1;var n=this.mediaSource.addSourceBuffer(e+'/mp4; codecs="'+t.mp4track.codec+'"');this.bufferControllers[e]=new w(n,e),this.sourceBuffers[e]=n,this.bufferControllers[e].on("error",this.onBufferError.bind(this))}}}},{key:"startInterval",value:function(){var e=this;this.interval=setInterval(function(){e.bufferControllers&&(e.releaseBuffer(),e.clearBuffer())},this.options.flushingTime)}},{key:"stopInterval",value:function(){this.interval&&clearInterval(this.interval)}},{key:"releaseBuffer",value:function(){for(var e in this.bufferControllers)this.bufferControllers[e].doAppend()}},{key:"getSafeClearOffsetOfBuffer",value:function(e){for(var t="audio"===this.options.mode&&e||0,r=void 0,n=0;n=e);n++)r=this.kfPosition[n];return r&&(this.kfPosition=this.kfPosition.filter(function(e){return e=r})),t}},{key:"clearBuffer",value:function(){if(this.options.clearBuffer&&Date.now()-this.lastCleaningTime>1e4){for(var e in this.bufferControllers){var t=this.getSafeClearOffsetOfBuffer(this.node.currentTime);this.bufferControllers[e].initCleanup(t)}this.lastCleaningTime=Date.now()}}},{key:"onBuffer",value:function(e){this.bufferControllers&&this.bufferControllers[e.type]&&this.bufferControllers[e.type].feed(e.payload)}},{key:"onMSEOpen",value:function(){this.mseReady=!0,"function"==typeof this.options.onReady&&(this.options.onReady(),this.options.onReady=null),this.createBuffer()}},{key:"onMSEClose",value:function(){this.mseReady=!1,this.videoStarted=!1}},{key:"onBufferError",value:function(e){if("QuotaExceeded"==e.name)return void this.bufferControllers[e.type].initCleanup(this.node.currentTime);if(this.mediaSource.sourceBuffers.length>0&&this.sourceBuffers[e.type]&&this.mediaSource.removeSourceBuffer(this.sourceBuffers[e.type]),0==this.mediaSource.sourceBuffers.length)try{this.mediaSource.endOfStream()}catch(e){r("mediasource is not available to end")}}}]),i}(v)}(); +var JMuxer=function(){"use strict";function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function n(e,t){for(var r=0;re.length)&&(t=e.length);for(var r=0,n=new Array(t);r=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,s=!0,o=!1;return{s:function(){r=e[Symbol.iterator]()},n:function(){var e=r.next();return s=e.done,e},e:function(e){o=!0,a=e},f:function(){try{s||null==r.return||r.return()}finally{if(o)throw a}}}}var f,h;function g(e){if(f){for(var t=arguments.length,r=new Array(1>5,this.ntype=31&this.payload[0],this.isvcl=1==this.ntype||5==this.ntype,this.stype="",this.isfmb=!1}return o(t,null,[{key:"type",value:function(e){return e.ntype in t.TYPES?t.TYPES[e.ntype]:"UNKNOWN"}},{key:"NDR",get:function(){return 1}},{key:"IDR",get:function(){return 5}},{key:"SEI",get:function(){return 6}},{key:"SPS",get:function(){return 7}},{key:"PPS",get:function(){return 8}},{key:"AUD",get:function(){return 9}},{key:"TYPES",get:function(){var e={};return r(e,t.IDR,"IDR"),r(e,t.SEI,"SEI"),r(e,t.SPS,"SPS"),r(e,t.PPS,"PPS"),r(e,t.NDR,"NDR"),r(e,t.AUD,"AUD"),e}}]),o(t,[{key:"toString",value:function(){return"".concat(t.type(this),": NRI: ").concat(this.getNri())}},{key:"getNri",value:function(){return this.nri}},{key:"type",value:function(){return this.ntype}},{key:"isKeyframe",value:function(){return this.ntype===t.IDR||7===this.stype}},{key:"getPayload",value:function(){return this.payload}},{key:"getPayloadSize",value:function(){return this.payload.byteLength}},{key:"getSize",value:function(){return 4+this.getPayloadSize()}},{key:"getData",value:function(){var e=new Uint8Array(this.getSize());return new DataView(e.buffer).setUint32(0,this.getSize()-4),e.set(this.getPayload(),4),e}}]),t}(),b=function(){function t(e){a(this,t),this.data=e,this.index=0,this.bitLength=8*e.byteLength}return o(t,[{key:"skipBits",value:function(e){if(this.bitsAvailable>>i,s=8-i;if(e<=s)return n&&(this.index+=e),a>>s-e;n&&(this.index+=s);var o=e-s;return a<>>1:-1*(e>>>1)}},{key:"readBoolean",value:function(){return 1===this.readBits(1)}},{key:"readUByte",value:function(e){var t=0>>5}},{key:"isAACPattern",value:function(e){return 255===e[0]&&240==(240&e[1])&&0==(6&e[1])}},{key:"extractAAC",value:function(e){var t,r,n=0,i=e.byteLength,a=[];if(!s.isAACPattern(e))return d("Invalid ADTS audio format"),a;for(t=s.getHeaderLength(e),y=y||e.subarray(0,t);n>>6),t=(60&i[2])>>>2,r=(1&i[2])<<2,r|=(192&i[3])>>>6,n[0]=e<<3,n[0]|=(14&t)>>1,n[1]|=(1&t)<<7,n[1]|=r<<3,this.track.codec="mp4a.40."+e,this.track.channelCount=r,this.track.config=n,this.remuxer.readyToDecode=!0)}}]),s}(),t=function(){function t(e){a(this,t),this.listener={},this.type=""|e}return o(t,[{key:"on",value:function(e,t){return this.listener[e]||(this.listener[e]=[]),this.listener[e].push(t),!0}},{key:"off",value:function(e,t){if(this.listener[e]){var r=this.listener[e].indexOf(t);return-1>24&255,i[1]=a>>16&255,i[2]=a>>8&255,i[3]=255&a,i.set(e,4),s=0,a=8;s>24&255,e>>16&255,e>>8&255,255&e,t>>24,t>>16&255,t>>8&255,255&t,85,196,0,0]))}},{key:"mdia",value:function(e){return h.box(h.types.mdia,h.mdhd(e.timescale,e.duration),h.hdlr(e.type),h.minf(e))}},{key:"mfhd",value:function(e){return h.box(h.types.mfhd,new Uint8Array([0,0,0,0,e>>24,e>>16&255,e>>8&255,255&e]))}},{key:"minf",value:function(e){return"audio"===e.type?h.box(h.types.minf,h.box(h.types.smhd,h.SMHD),h.DINF,h.stbl(e)):h.box(h.types.minf,h.box(h.types.vmhd,h.VMHD),h.DINF,h.stbl(e))}},{key:"moof",value:function(e,t,r){return h.box(h.types.moof,h.mfhd(e),h.traf(r,t))}},{key:"moov",value:function(e,t,r){for(var n=e.length,i=[];n--;)i[n]=h.trak(e[n]);return h.box.apply(null,[h.types.moov,h.mvhd(r,t)].concat(i).concat(h.mvex(e)))}},{key:"mvex",value:function(e){for(var t=e.length,r=[];t--;)r[t]=h.trex(e[t]);return h.box.apply(null,[h.types.mvex].concat(r))}},{key:"mvhd",value:function(e,t){var r=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,2,e>>24&255,e>>16&255,e>>8&255,255&e,t>>24&255,t>>16&255,t>>8&255,255&t,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,255,255]);return h.box(h.types.mvhd,r)}},{key:"sdtp",value:function(e){for(var t,r=e.samples||[],n=new Uint8Array(4+r.length),i=0;i>>8&255),n.push(255&r),n=n.concat(Array.prototype.slice.call(t));for(a=0;a>>8&255),i.push(255&r),i=i.concat(Array.prototype.slice.call(t));var s=h.box(h.types.avcC,new Uint8Array([1,n[3],n[4],n[5],255,224|e.sps.length].concat(n).concat([e.pps.length]).concat(i))),o=e.width,u=e.height;return h.box(h.types.avc1,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,o>>8&255,255&o,u>>8&255,255&u,0,72,0,0,0,72,0,0,0,0,0,0,0,1,18,98,105,110,101,108,112,114,111,46,114,117,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,17,17]),s,h.box(h.types.btrt,new Uint8Array([0,28,156,128,0,45,198,192,0,45,198,192])))}},{key:"esds",value:function(e){var t=e.config.byteLength,r=new Uint8Array(26+t+3);return r.set([0,0,0,0,3,23+t,0,1,0,4,15+t,64,21,0,0,0,0,0,0,0,0,0,0,0,5,t]),r.set(e.config,26),r.set([6,1,2],26+t),r}},{key:"mp4a",value:function(e){var t=e.audiosamplerate;return h.box(h.types.mp4a,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,e.channelCount,0,16,0,0,0,0,t>>8&255,255&t,0,0]),h.box(h.types.esds,h.esds(e)))}},{key:"stsd",value:function(e){return"audio"===e.type?h.box(h.types.stsd,h.STSD,h.mp4a(e)):h.box(h.types.stsd,h.STSD,h.avc1(e))}},{key:"tkhd",value:function(e){var t=e.id,r=e.duration,n=e.width,i=e.height,a=e.volume;return h.box(h.types.tkhd,new Uint8Array([0,0,0,7,0,0,0,0,0,0,0,0,t>>24&255,t>>16&255,t>>8&255,255&t,0,0,0,0,r>>24,r>>16&255,r>>8&255,255&r,0,0,0,0,0,0,0,0,0,0,0,0,a>>0&255,a%1*10>>0&255,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,0,0,0,n>>8&255,255&n,0,0,i>>8&255,255&i,0,0]))}},{key:"traf",value:function(e,t){var r=h.sdtp(e),n=e.id;return h.box(h.types.traf,h.box(h.types.tfhd,new Uint8Array([0,0,0,0,n>>24,n>>16&255,n>>8&255,255&n])),h.box(h.types.tfdt,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t])),h.trun(e,r.length+16+16+8+16+8+8),r)}},{key:"trak",value:function(e){return e.duration=e.duration||4294967295,h.box(h.types.trak,h.tkhd(e),h.mdia(e))}},{key:"trex",value:function(e){var t=e.id;return h.box(h.types.trex,new Uint8Array([0,0,0,0,t>>24,t>>16&255,t>>8&255,255&t,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]))}},{key:"trun",value:function(e,t){var r,n,i,a,s,o,u=e.samples||[],c=u.length,l=12+16*c,f=new Uint8Array(l);for(t+=8+l,f.set([0,0,15,1,c>>>24&255,c>>>16&255,c>>>8&255,255&c,t>>>24&255,t>>>16&255,t>>>8&255,255&t],0),r=0;r>>24&255,i>>>16&255,i>>>8&255,255&i,a>>>24&255,a>>>16&255,a>>>8&255,255&a,s.isLeading<<2|s.dependsOn,s.isDependedOn<<6|s.hasRedundancy<<4|s.paddingValue<<1|s.isNonSync,61440&s.degradPrio,15&s.degradPrio,o>>>24&255,o>>>16&255,o>>>8&255,255&o],12+16*r);return h.box(h.types.trun,f)}},{key:"initSegment",value:function(e,t,r){h.types||h.init();var n=h.moov(e,t,r),i=new Uint8Array(h.FTYP.byteLength+n.byteLength);return i.set(h.FTYP),i.set(n,h.FTYP.byteLength),i}}]),h}(),w=1,x=function(){function e(){a(this,e),this.seq=1}return o(e,null,[{key:"getTrackID",value:function(){return w++}}]),o(e,[{key:"flush",value:function(){this.seq++,this.mp4track.len=0,this.mp4track.samples=[]}},{key:"isReady",value:function(){return!(!this.readyToDecode||!this.samples.length)||null}}]),e}(),A=function(){e(r,x);var t=s(r);function r(){var e;return a(this,r),(e=t.call(this)).readyToDecode=!1,e.nextDts=0,e.dts=0,e.timescale=1e3,e.mp4track={id:x.getTrackID(),type:"audio",channelCount:0,len:0,fragmented:!0,timescale:e.timescale,duration:e.timescale,samples:[],config:"",codec:""},e.samples=[],e.aac=new m(c(e)),e}return o(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.codec="",this.mp4track.channelCount="",this.mp4track.config="",this.mp4track.timescale=this.timescale}},{key:"remux",value:function(e){if(0this.cleanOffset&&r<(n=e-this.cleanOffset)&&this.cleanRanges.push([r,n])}this.doCleanup()}}},{key:"doAppend",value:function(){if(this.queue.length&&!this.sourceBuffer.updating)try{this.sourceBuffer.appendBuffer(this.queue),this.queue=new Uint8Array}catch(e){if("QuotaExceededError"===e.name)return g("".concat(this.type," buffer quota full")),void this.dispatch("error",{type:this.type,name:"QuotaExceeded",error:"buffer error"});d("Error occured while appending ".concat(this.type," buffer - ").concat(e.name,": ").concat(e.message)),this.dispatch("error",{type:this.type,name:"unexpectedError",error:"buffer error"})}}},{key:"feed",value:function(e){this.queue=U(this.queue,e)}}]),i}();return window.MediaSource=window.MediaSource||window.WebKitMediaSource,function(){e(n,t);var r=s(n);function n(e){var t;a(this,n),t=r.call(this,"jmuxer"),window.MediaSource=window.MediaSource||window.WebKitMediaSource;if(t.options=Object.assign({},{node:"",mode:"both",flushingTime:1500,clearBuffer:!0,onReady:null,fps:30,debug:!1},e),t.options.debug&&(f=console.log,h=console.error),"string"==typeof t.options.node&&""==t.options.node&&d("no video element were found to render, provide a valid video element"),t.options.fps||(t.options.fps=30),t.frameDuration=1e3/t.options.fps|0,t.node="string"==typeof t.options.node?document.getElementById(t.options.node):t.options.node,t.sourceBuffers={},t.isMSESupported=!!window.MediaSource,!t.isMSESupported)throw"Oops! Browser does not support media source extension.";return t.setupMSE(),t.remuxController=new C(t.options.clearBuffer),t.remuxController.addTrack(t.options.mode),t.mseReady=!1,t.lastCleaningTime=Date.now(),t.kfPosition=[],t.kfCounter=0,t.remuxController.on("buffer",t.onBuffer.bind(c(t))),t.remuxController.on("ready",t.createBuffer.bind(c(t))),t.startInterval(),t}return o(n,null,[{key:"isSupported",value:function(e){return window.MediaSource&&window.MediaSource.isTypeSupported(e)}}]),o(n,[{key:"setupMSE",value:function(){this.mediaSource=new MediaSource,this.node.src=URL.createObjectURL(this.mediaSource),this.mediaSource.addEventListener("sourceopen",this.onMSEOpen.bind(this)),this.mediaSource.addEventListener("sourceclose",this.onMSEClose.bind(this)),this.mediaSource.addEventListener("webkitsourceopen",this.onMSEOpen.bind(this)),this.mediaSource.addEventListener("webkitsourceclose",this.onMSEClose.bind(this))}},{key:"feed",value:function(e){var t,r,n=!1,i={video:[],audio:[]};e&&this.remuxController&&(r=e.duration?parseInt(e.duration):0,e.video&&0<(t=v.extractNALu(e.video)).length&&(i.video=this.getVideoFrames(t,r),n=!0),e.audio&&0<(t=m.extractAAC(e.audio)).length&&(i.audio=this.getAudioFrames(t,r),n=!0),n?this.remuxController.remux(i):d("Input object must have video and/or audio property. Make sure it is a valid typed array"))}},{key:"getVideoFrames",value:function(e,t){var r,n,i,a=this,s=[],o=[],u=0,c=!1,l=!1,f=k(e);try{for(f.s();!(n=f.n()).done;){var h=n.value,d=new p(h);d.type()!==p.IDR&&d.type()!==p.NDR||v.parseHeader(d),s.length&&l&&(d.isfmb||!d.isvcl)&&(o.push({units:s,keyFrame:c}),l=c=!(s=[])),s.push(d),c=c||d.isKeyframe(),l=l||d.isvcl}}catch(e){f.e(e)}finally{f.f()}return s.length&&(l||!o.length?o.push({units:s,keyFrame:c}):o[i=o.length-1].units=o[i].units.concat(s)),r=t?t/o.length|0:this.frameDuration,u=t?t-r*o.length:0,o.map(function(e){e.duration=r,0=e);n++)t=this.kfPosition[n];return t&&(this.kfPosition=this.kfPosition.filter(function(e){return e