UNPKG

@sqhead/mux.js

Version:

A collection of lightweight utilities for inspecting and manipulating video container formats.

17 lines 51.8 kB
/** * Copyright 2015 Brightcove * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ !function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.muxjs=t()}}(function(){return function t(e,i,a){function n(r,o){if(!i[r]){if(!e[r]){var d="function"==typeof require&&require;if(!o&&d)return d(r,!0);if(s)return s(r,!0);var p=new Error("Cannot find module '"+r+"'");throw p.code="MODULE_NOT_FOUND",p}var h=i[r]={exports:{}};e[r][0].call(h.exports,function(t){var i=e[r][1][t];return n(i||t)},h,h.exports,t,e,i,a)}return i[r].exports}for(var s="function"==typeof require&&require,r=0;r<a.length;r++)n(a[r]);return n}({1:[function(t,e,i){"use strict";var a,n=t("../utils/stream.js");a=function(){var t=new Uint8Array,e=0;a.prototype.init.call(this),this.setTimestamp=function(t){e=t},this.parseId3TagSize=function(t,e){var i=t[e+6]<<21|t[e+7]<<14|t[e+8]<<7|t[e+9];return(16&t[e+5])>>4?i+20:i+10},this.parseAdtsSize=function(t,e){var i=(224&t[e+5])>>5,a=t[e+4]<<3;return 6144&t[e+3]|a|i},this.push=function(i){var a,n,s,r,o=0,d=0;for(t.length?(r=t.length,t=new Uint8Array(i.byteLength+r),t.set(t.subarray(0,r)),t.set(i,r)):t=i;t.length-d>=3;)if(t[d]!=="I".charCodeAt(0)||t[d+1]!=="D".charCodeAt(0)||t[d+2]!=="3".charCodeAt(0))if(!0&t[d]&&240==(240&t[d+1])){if(t.length-d<7)break;if((o=this.parseAdtsSize(t,d))>t.length)break;s={type:"audio",data:t.subarray(d,d+o),pts:e,dts:e},this.trigger("data",s),d+=o}else d++;else{if(t.length-d<10)break;if((o=this.parseId3TagSize(t,d))>t.length)break;n={type:"timed-metadata",data:t.subarray(d,d+o)},this.trigger("data",n),d+=o}a=t.length-d,t=a>0?t.subarray(d):new Uint8Array}},a.prototype=new n,e.exports=a},{"../utils/stream.js":17}],2:[function(t,e,i){"use strict";var a,n=t("../utils/stream.js"),s=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];a=function(){var t;a.prototype.init.call(this),this.push=function(e){var i,a,n,r,o,d,p=0,h=0;if("audio"===e.type)for(t?(r=t,t=new Uint8Array(r.byteLength+e.data.byteLength),t.set(r),t.set(e.data,r.byteLength)):t=e.data;p+5<t.length;)if(255===t[p]&&240==(246&t[p+1])){if(a=2*(1&~t[p+1]),i=(3&t[p+3])<<11|t[p+4]<<3|(224&t[p+5])>>5,o=1024*(1+(3&t[p+6])),d=9e4*o/s[(60&t[p+2])>>>2],n=p+i,t.byteLength<n)return;if(this.trigger("data",{pts:e.pts+h*d,dts:e.dts+h*d,sampleCount:o,audioobjecttype:1+(t[p+2]>>>6&3),channelcount:(1&t[p+2])<<2|(192&t[p+3])>>>6,samplerate:s[(60&t[p+2])>>>2],samplingfrequencyindex:(60&t[p+2])>>>2,samplesize:16,data:t.subarray(p+7+a,n)}),t.byteLength===n)return void(t=void 0);h++,t=t.subarray(n)}else p++},this.flush=function(){this.trigger("done")}},a.prototype=new n,e.exports=a},{"../utils/stream.js":17}],3:[function(t,e,i){"use strict";var a,n,s,r=t("../utils/stream.js"),o=t("../utils/exp-golomb.js");n=function(){var t,e,i=0;n.prototype.init.call(this),this.push=function(a){var n;for(e?(n=new Uint8Array(e.byteLength+a.data.byteLength),n.set(e),n.set(a.data,e.byteLength),e=n):e=a.data;i<e.byteLength-3;i++)if(1===e[i+2]){t=i+5;break}for(;t<e.byteLength;)switch(e[t]){case 0:if(0!==e[t-1]){t+=2;break}if(0!==e[t-2]){t++;break}i+3!==t-2&&this.trigger("data",e.subarray(i+3,t-2));do{t++}while(1!==e[t]&&t<e.length);i=t-2,t+=3;break;case 1:if(0!==e[t-1]||0!==e[t-2]){t+=3;break}this.trigger("data",e.subarray(i+3,t-2)),i=t-2,t+=3;break;default:t+=3}e=e.subarray(i),t-=i,i=0},this.flush=function(){e&&e.byteLength>3&&this.trigger("data",e.subarray(i+3)),e=null,i=0,this.trigger("done")}},n.prototype=new r,s={100:!0,110:!0,122:!0,244:!0,44:!0,83:!0,86:!0,118:!0,128:!0,138:!0,139:!0,134:!0},a=function(){var t,e,i,r,d,p,h,c=new n;a.prototype.init.call(this),t=this,this.push=function(t){"video"===t.type&&(e=t.trackId,i=t.pts,r=t.dts,c.push(t))},c.on("data",function(a){var n={trackId:e,pts:i,dts:r,data:a};switch(31&a[0]){case 5:n.nalUnitType="slice_layer_without_partitioning_rbsp_idr";break;case 6:n.nalUnitType="sei_rbsp",n.escapedRBSP=d(a.subarray(1));break;case 7:n.nalUnitType="seq_parameter_set_rbsp",n.escapedRBSP=d(a.subarray(1)),n.config=p(n.escapedRBSP);break;case 8:n.nalUnitType="pic_parameter_set_rbsp";break;case 9:n.nalUnitType="access_unit_delimiter_rbsp"}t.trigger("data",n)}),c.on("done",function(){t.trigger("done")}),this.flush=function(){c.flush()},h=function(t,e){var i,a,n=8,s=8;for(i=0;i<t;i++)0!==s&&(a=e.readExpGolomb(),s=(n+a+256)%256),n=0===s?n:s},d=function(t){for(var e,i,a=t.byteLength,n=[],s=1;s<a-2;)0===t[s]&&0===t[s+1]&&3===t[s+2]?(n.push(s+2),s+=2):s++;if(0===n.length)return t;e=a-n.length,i=new Uint8Array(e);var r=0;for(s=0;s<e;r++,s++)r===n[0]&&(r++,n.shift()),i[s]=t[r];return i},p=function(t){var e,i,a,n,r,d,p,c,u,l,m,f,g,y=0,S=0,_=0,b=0,T=1;if(e=new o(t),i=e.readUnsignedByte(),n=e.readUnsignedByte(),a=e.readUnsignedByte(),e.skipUnsignedExpGolomb(),s[i]&&(r=e.readUnsignedExpGolomb(),3===r&&e.skipBits(1),e.skipUnsignedExpGolomb(),e.skipUnsignedExpGolomb(),e.skipBits(1),e.readBoolean()))for(m=3!==r?8:12,g=0;g<m;g++)e.readBoolean()&&(g<6?h(16,e):h(64,e));if(e.skipUnsignedExpGolomb(),0===(d=e.readUnsignedExpGolomb()))e.readUnsignedExpGolomb();else if(1===d)for(e.skipBits(1),e.skipExpGolomb(),e.skipExpGolomb(),p=e.readUnsignedExpGolomb(),g=0;g<p;g++)e.skipExpGolomb();if(e.skipUnsignedExpGolomb(),e.skipBits(1),c=e.readUnsignedExpGolomb(),u=e.readUnsignedExpGolomb(),l=e.readBits(1),0===l&&e.skipBits(1),e.skipBits(1),e.readBoolean()&&(y=e.readUnsignedExpGolomb(),S=e.readUnsignedExpGolomb(),_=e.readUnsignedExpGolomb(),b=e.readUnsignedExpGolomb()),e.readBoolean()&&e.readBoolean()){switch(e.readUnsignedByte()){case 1:f=[1,1];break;case 2:f=[12,11];break;case 3:f=[10,11];break;case 4:f=[16,11];break;case 5:f=[40,33];break;case 6:f=[24,11];break;case 7:f=[20,11];break;case 8:f=[32,11];break;case 9:f=[80,33];break;case 10:f=[18,11];break;case 11:f=[15,11];break;case 12:f=[64,33];break;case 13:f=[160,99];break;case 14:f=[4,3];break;case 15:f=[3,2];break;case 16:f=[2,1];break;case 255:f=[e.readUnsignedByte()<<8|e.readUnsignedByte(),e.readUnsignedByte()<<8|e.readUnsignedByte()]}f&&(T=f[0]/f[1])}return{profileIdc:i,levelIdc:a,profileCompatibility:n,width:Math.ceil((16*(c+1)-2*y-2*S)*T),height:(2-l)*(u+1)*16-2*_-2*b}}},a.prototype=new r,e.exports={H264Stream:a,NalByteStream:n}},{"../utils/exp-golomb.js":16,"../utils/stream.js":17}],4:[function(t,e,i){var a=[33,16,5,32,164,27],n=[33,65,108,84,1,2,4,8,168,2,4,8,17,191,252],s=function(t){for(var e=[];t--;)e.push(0);return e},r={96e3:[a,[227,64],s(154),[56]],88200:[a,[231],s(170),[56]],64e3:[a,[248,192],s(240),[56]],48e3:[a,[255,192],s(268),[55,148,128],s(54),[112]],44100:[a,[255,192],s(268),[55,163,128],s(84),[112]],32e3:[a,[255,192],s(268),[55,234],s(226),[112]],24e3:[a,[255,192],s(268),[55,255,128],s(268),[111,112],s(126),[224]],16e3:[a,[255,192],s(268),[55,255,128],s(268),[111,255],s(269),[223,108],s(195),[1,192]],12e3:[n,s(268),[3,127,248],s(268),[6,255,240],s(268),[13,255,224],s(268),[27,253,128],s(259),[56]],11025:[n,s(268),[3,127,248],s(268),[6,255,240],s(268),[13,255,224],s(268),[27,255,192],s(268),[55,175,128],s(108),[112]],8e3:[n,s(268),[3,121,16],s(47),[7]]};e.exports=function(t){return Object.keys(t).reduce(function(e,i){return e[i]=new Uint8Array(t[i].reduce(function(t,e){return t.concat(e)},[])),e},{})}(r)},{}],5:[function(t,e,i){"use strict";var a=t("../utils/stream"),n=function(t){for(var e=0,i={payloadType:-1,payloadSize:0},a=0,n=0;e<t.byteLength&&128!==t[e];){for(;255===t[e];)a+=255,e++;for(a+=t[e++];255===t[e];)n+=255,e++;if(n+=t[e++],!i.payload&&4===a){i.payloadType=a,i.payloadSize=n,i.payload=t.subarray(e,e+n);break}e+=n,a=0,n=0}return i},s=function(t){return 181!==t.payload[0]?null:49!=(t.payload[1]<<8|t.payload[2])?null:"GA94"!==String.fromCharCode(t.payload[3],t.payload[4],t.payload[5],t.payload[6])?null:3!==t.payload[7]?null:t.payload.subarray(8,t.payload.length-1)},r=function(t,e){var i,a,n,s,r=[];if(!(64&e[0]))return r;for(a=31&e[0],i=0;i<a;i++)n=3*i,s={type:3&e[n+2],pts:t},4&e[n+2]&&(s.ccData=e[n+3]<<8|e[n+4],r.push(s));return r},o=function(){o.prototype.init.call(this),this.captionPackets_=[],this.ccStreams_=[new u(0,0),new u(0,1),new u(1,0),new u(1,1)],this.reset(),this.ccStreams_.forEach(function(t){t.on("data",this.trigger.bind(this,"data")),t.on("done",this.trigger.bind(this,"done"))},this)};o.prototype=new a,o.prototype.push=function(t){var e,i;if("sei_rbsp"===t.nalUnitType&&(e=n(t.escapedRBSP),4===e.payloadType&&(i=s(e)))){if(t.dts<this.latestDts_)return void(this.ignoreNextEqualDts_=!0);if(t.dts===this.latestDts_&&this.ignoreNextEqualDts_)return void(--this.numSameDts_||(this.ignoreNextEqualDts_=!1));this.captionPackets_=this.captionPackets_.concat(r(t.pts,i)),this.latestDts_!==t.dts&&(this.numSameDts_=0),this.numSameDts_++,this.latestDts_=t.dts}},o.prototype.flush=function(){if(!this.captionPackets_.length)return void this.ccStreams_.forEach(function(t){t.flush()},this);this.captionPackets_.forEach(function(t,e){t.presortIndex=e}),this.captionPackets_.sort(function(t,e){return t.pts===e.pts?t.presortIndex-e.presortIndex:t.pts-e.pts}),this.captionPackets_.forEach(function(t){t.type<2&&this.dispatchCea608Packet(t)},this),this.captionPackets_.length=0,this.ccStreams_.forEach(function(t){t.flush()},this)},o.prototype.reset=function(){this.latestDts_=null,this.ignoreNextEqualDts_=!1,this.numSameDts_=0,this.activeCea608Channel_=[null,null],this.ccStreams_.forEach(function(t){t.reset()})},o.prototype.dispatchCea608Packet=function(t){this.setsChannel1Active(t)?this.activeCea608Channel_[t.type]=0:this.setsChannel2Active(t)&&(this.activeCea608Channel_[t.type]=1),null!==this.activeCea608Channel_[t.type]&&this.ccStreams_[(t.type<<1)+this.activeCea608Channel_[t.type]].push(t)},o.prototype.setsChannel1Active=function(t){return 4096==(30720&t.ccData)},o.prototype.setsChannel2Active=function(t){return 6144==(30720&t.ccData)};var d={42:225,92:233,94:237,95:243,96:250,123:231,124:247,125:209,126:241,127:9608,304:174,305:176,306:189,307:191,308:8482,309:162,310:163,311:9834,312:224,313:160,314:232,315:226,316:234,317:238,318:244,319:251,544:193,545:201,546:211,547:218,548:220,549:252,550:8216,551:161,552:42,553:39,554:8212,555:169,556:8480,557:8226,558:8220,559:8221,560:192,561:194,562:199,563:200,564:202,565:203,566:235,567:206,568:207,569:239,570:212,571:217,572:249,573:219,574:171,575:187,800:195,801:227,802:205,803:204,804:236,805:210,806:242,807:213,808:245,809:123,810:125,811:92,812:94,813:95,814:124,815:126,816:196,817:228,818:214,819:246,820:223,821:165,822:164,823:9474,824:197,825:229,826:216,827:248,828:9484,829:9488,830:9492,831:9496},p=function(t){return null===t?"":(t=d[t]||t,String.fromCharCode(t))},h=[4352,4384,4608,4640,5376,5408,5632,5664,5888,5920,4096,4864,4896,5120,5152],c=function(){for(var t=[],e=15;e--;)t.push("");return t},u=function(t,e){u.prototype.init.call(this),this.field_=t||0,this.dataChannel_=e||0,this.name_="CC"+(1+(this.field_<<1|this.dataChannel_)),this.setConstants(),this.reset(),this.push=function(t){var e,i,a,n,s;if((e=32639&t.ccData)===this.lastControlCode_)return void(this.lastControlCode_=null);if(4096==(61440&e)?this.lastControlCode_=e:e!==this.PADDING_&&(this.lastControlCode_=null),a=e>>>8,n=255&e,e!==this.PADDING_)if(e===this.RESUME_CAPTION_LOADING_)this.mode_="popOn";else if(e===this.END_OF_CAPTION_)this.clearFormatting(t.pts),this.flushDisplayed(t.pts),i=this.displayed_,this.displayed_=this.nonDisplayed_,this.nonDisplayed_=i,this.startPts_=t.pts;else if(e===this.ROLL_UP_2_ROWS_)this.topRow_=13,this.mode_="rollUp";else if(e===this.ROLL_UP_3_ROWS_)this.topRow_=12,this.mode_="rollUp";else if(e===this.ROLL_UP_4_ROWS_)this.topRow_=11,this.mode_="rollUp";else if(e===this.CARRIAGE_RETURN_)this.clearFormatting(t.pts),this.flushDisplayed(t.pts),this.shiftRowsUp_(),this.startPts_=t.pts;else if(e===this.BACKSPACE_)"popOn"===this.mode_?this.nonDisplayed_[14]=this.nonDisplayed_[14].slice(0,-1):this.displayed_[14]=this.displayed_[14].slice(0,-1);else if(e===this.ERASE_DISPLAYED_MEMORY_)this.flushDisplayed(t.pts),this.displayed_=c();else if(e===this.ERASE_NON_DISPLAYED_MEMORY_)this.nonDisplayed_=c();else if(e===this.RESUME_DIRECT_CAPTIONING_)this.mode_="paintOn";else if(this.isSpecialCharacter(a,n))a=(3&a)<<8,s=p(a|n),this[this.mode_](t.pts,s),this.column_++;else if(this.isExtCharacter(a,n))"popOn"===this.mode_?this.nonDisplayed_[this.row_]=this.nonDisplayed_[this.row_].slice(0,-1):this.displayed_[14]=this.displayed_[14].slice(0,-1),a=(3&a)<<8,s=p(a|n),this[this.mode_](t.pts,s),this.column_++;else if(this.isMidRowCode(a,n))this.clearFormatting(t.pts),this[this.mode_](t.pts," "),this.column_++,14==(14&n)&&this.addFormatting(t.pts,["i"]),1==(1&n)&&this.addFormatting(t.pts,["u"]);else if(this.isOffsetControlCode(a,n))this.column_+=3&n;else if(this.isPAC(a,n)){var r=h.indexOf(7968&e);r!==this.row_&&(this.clearFormatting(t.pts),this.row_=r),1&n&&-1===this.formatting_.indexOf("u")&&this.addFormatting(t.pts,["u"]),16==(16&e)&&(this.column_=4*((14&e)>>1)),this.isColorPAC(n)&&14==(14&n)&&this.addFormatting(t.pts,["i"])}else this.isNormalChar(a)&&(0===n&&(n=null),s=p(a),s+=p(n),this[this.mode_](t.pts,s),this.column_+=s.length)}};u.prototype=new a,u.prototype.flushDisplayed=function(t){var e=this.displayed_.map(function(t){return t.trim()}).join("\n").replace(/^\n+|\n+$/g,"");e.length&&this.trigger("data",{startPts:this.startPts_,endPts:t,text:e,stream:this.name_})},u.prototype.reset=function(){this.mode_="popOn",this.topRow_=0,this.startPts_=0,this.displayed_=c(),this.nonDisplayed_=c(),this.lastControlCode_=null,this.column_=0,this.row_=14,this.formatting_=[]},u.prototype.setConstants=function(){0===this.dataChannel_?(this.BASE_=16,this.EXT_=17,this.CONTROL_=(20|this.field_)<<8,this.OFFSET_=23):1===this.dataChannel_&&(this.BASE_=24,this.EXT_=25,this.CONTROL_=(28|this.field_)<<8,this.OFFSET_=31),this.PADDING_=0,this.RESUME_CAPTION_LOADING_=32|this.CONTROL_,this.END_OF_CAPTION_=47|this.CONTROL_,this.ROLL_UP_2_ROWS_=37|this.CONTROL_,this.ROLL_UP_3_ROWS_=38|this.CONTROL_,this.ROLL_UP_4_ROWS_=39|this.CONTROL_,this.CARRIAGE_RETURN_=45|this.CONTROL_,this.RESUME_DIRECT_CAPTIONING_=41|this.CONTROL_,this.BACKSPACE_=33|this.CONTROL_,this.ERASE_DISPLAYED_MEMORY_=44|this.CONTROL_,this.ERASE_NON_DISPLAYED_MEMORY_=46|this.CONTROL_},u.prototype.isSpecialCharacter=function(t,e){return t===this.EXT_&&e>=48&&e<=63},u.prototype.isExtCharacter=function(t,e){return(t===this.EXT_+1||t===this.EXT_+2)&&e>=32&&e<=63},u.prototype.isMidRowCode=function(t,e){return t===this.EXT_&&e>=32&&e<=47},u.prototype.isOffsetControlCode=function(t,e){return t===this.OFFSET_&&e>=33&&e<=35},u.prototype.isPAC=function(t,e){return t>=this.BASE_&&t<this.BASE_+8&&e>=64&&e<=127},u.prototype.isColorPAC=function(t){return t>=64&&t<=79||t>=96&&t<=127},u.prototype.isNormalChar=function(t){return t>=32&&t<=127},u.prototype.addFormatting=function(t,e){this.formatting_=this.formatting_.concat(e);var i=e.reduce(function(t,e){return t+"<"+e+">"},"");this[this.mode_](t,i)},u.prototype.clearFormatting=function(t){if(this.formatting_.length){var e=this.formatting_.reverse().reduce(function(t,e){return t+"</"+e+">"},"");this.formatting_=[],this[this.mode_](t,e)}},u.prototype.popOn=function(t,e){var i=this.nonDisplayed_[this.row_];i+=e,this.nonDisplayed_[this.row_]=i},u.prototype.rollUp=function(t,e){var i=this.displayed_[14];i+=e,this.displayed_[14]=i},u.prototype.shiftRowsUp_=function(){var t;for(t=0;t<this.topRow_;t++)this.displayed_[t]="";for(t=this.topRow_;t<14;t++)this.displayed_[t]=this.displayed_[t+1];this.displayed_[14]=""},u.prototype.paintOn=function(){},e.exports={CaptionStream:o,Cea608Stream:u}},{"../utils/stream":17}],6:[function(t,e,i){"use strict";var a,n,s,r=t("../utils/stream.js"),o=t("./caption-stream"),d=t("./stream-types"),p=t("./timestamp-rollover-stream").TimestampRolloverStream,h=t("./stream-types.js");a=function(){var t=new Uint8Array(188),e=0;a.prototype.init.call(this),this.push=function(i){var a,n=0,s=188;for(e?(a=new Uint8Array(i.byteLength+e),a.set(t.subarray(0,e)),a.set(i,e),e=0):a=i;s<a.byteLength;)71!==a[n]||71!==a[s]?(n++,s++):(this.trigger("data",a.subarray(n,s)),n+=188,s+=188);n<a.byteLength&&(t.set(a.subarray(n),0),e=a.byteLength-n)},this.flush=function(){188===e&&71===t[0]&&(this.trigger("data",t),e=0),this.trigger("done")}},a.prototype=new r,n=function(){var t,e,i,a;n.prototype.init.call(this),a=this,this.packetsWaitingForPmt=[],this.programMapTable=void 0,t=function(t,a){var n=0;a.payloadUnitStartIndicator&&(n+=t[n]+1),"pat"===a.type?e(t.subarray(n),a):i(t.subarray(n),a)},e=function(t,e){e.section_number=t[7],e.last_section_number=t[8],a.pmtPid=(31&t[10])<<8|t[11],e.pmtPid=a.pmtPid},i=function(t,e){var i,n,s,r;if(1&t[5]){for(a.programMapTable={video:null,audio:null,"timed-metadata":{}},i=(15&t[1])<<8|t[2],n=3+i-4,s=(15&t[10])<<8|t[11],r=12+s;r<n;){var o=t[r],p=(31&t[r+1])<<8|t[r+2];o===d.H264_STREAM_TYPE&&null===a.programMapTable.video?a.programMapTable.video=p:o===d.ADTS_STREAM_TYPE&&null===a.programMapTable.audio?a.programMapTable.audio=p:o===d.METADATA_STREAM_TYPE&&(a.programMapTable["timed-metadata"][p]=o),r+=5+((15&t[r+3])<<8|t[r+4])}e.programMapTable=a.programMapTable}},this.push=function(e){var i={},a=4;if(i.payloadUnitStartIndicator=!!(64&e[1]),i.pid=31&e[1],i.pid<<=8,i.pid|=e[2],(48&e[3])>>>4>1&&(a+=e[a]+1),0===i.pid)i.type="pat",t(e.subarray(a),i),this.trigger("data",i);else if(i.pid===this.pmtPid)for(i.type="pmt",t(e.subarray(a),i),this.trigger("data",i);this.packetsWaitingForPmt.length;)this.processPes_.apply(this,this.packetsWaitingForPmt.shift());else void 0===this.programMapTable?this.packetsWaitingForPmt.push([e,a,i]):this.processPes_(e,a,i)},this.processPes_=function(t,e,i){i.pid===this.programMapTable.video?i.streamType=d.H264_STREAM_TYPE:i.pid===this.programMapTable.audio?i.streamType=d.ADTS_STREAM_TYPE:i.streamType=this.programMapTable["timed-metadata"][i.pid],i.type="pes",i.data=t.subarray(e),this.trigger("data",i)}},n.prototype=new r,n.STREAM_TYPES={h264:27,adts:15},s=function(){var t=this,e={data:[],size:0},i={data:[],size:0},a={data:[],size:0},n=function(t,e){var i;e.packetLength=6+(t[4]<<8|t[5]),e.dataAlignmentIndicator=0!=(4&t[6]),i=t[7],192&i&&(e.pts=(14&t[9])<<27|(255&t[10])<<20|(254&t[11])<<12|(255&t[12])<<5|(254&t[13])>>>3,e.pts*=4,e.pts+=(6&t[13])>>>1,e.dts=e.pts,64&i&&(e.dts=(14&t[14])<<27|(255&t[15])<<20|(254&t[16])<<12|(255&t[17])<<5|(254&t[18])>>>3,e.dts*=4,e.dts+=(6&t[18])>>>1)),e.data=t.subarray(9+t[8])},r=function(e,i,a){var s,r=new Uint8Array(e.size),o={type:i},d=0,p=0,h=!1;if(e.data.length&&!(e.size<9)){for(o.trackId=e.data[0].pid,d=0;d<e.data.length;d++)s=e.data[d],r.set(s.data,p),p+=s.data.byteLength;n(r,o),h="video"===i||o.packetLength<=e.size,(a||h)&&(e.size=0,e.data.length=0),h&&t.trigger("data",o)}};s.prototype.init.call(this),this.push=function(n){({pat:function(){},pes:function(){var t,s;switch(n.streamType){case d.H264_STREAM_TYPE:case h.H264_STREAM_TYPE:t=e,s="video";break;case d.ADTS_STREAM_TYPE:t=i,s="audio";break;case d.METADATA_STREAM_TYPE:t=a,s="timed-metadata";break;default:return}n.payloadUnitStartIndicator&&r(t,s,!0),t.data.push(n),t.size+=n.data.byteLength},pmt:function(){var e={type:"metadata",tracks:[]},i=n.programMapTable;null!==i.video&&e.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+i.video,codec:"avc",type:"video"}),null!==i.audio&&e.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+i.audio,codec:"adts",type:"audio"}),t.trigger("data",e)}})[n.type]()},this.flush=function(){r(e,"video"),r(i,"audio"),r(a,"timed-metadata"),this.trigger("done")}},s.prototype=new r;var c={PAT_PID:0,MP2T_PACKET_LENGTH:188,TransportPacketStream:a,TransportParseStream:n,ElementaryStream:s,TimestampRolloverStream:p,CaptionStream:o.CaptionStream,Cea608Stream:o.Cea608Stream,MetadataStream:t("./metadata-stream")};for(var u in d)d.hasOwnProperty(u)&&(c[u]=d[u]);e.exports=c},{"../utils/stream.js":17,"./caption-stream":5,"./metadata-stream":7,"./stream-types":8,"./stream-types.js":8,"./timestamp-rollover-stream":9}],7:[function(t,e,i){"use strict";var a,n=t("../utils/stream"),s=t("./stream-types"),r=function(t,e,i){var a,n="";for(a=e;a<i;a++)n+="%"+("00"+t[a].toString(16)).slice(-2);return n},o=function(t,e,i){return decodeURIComponent(r(t,e,i))},d=function(t,e,i){return unescape(r(t,e,i))},p=function(t){return t[0]<<21|t[1]<<14|t[2]<<7|t[3]},h={TXXX:function(t){var e;if(3===t.data[0]){for(e=1;e<t.data.length;e++)if(0===t.data[e]){t.description=o(t.data,1,e),t.value=o(t.data,e+1,t.data.length).replace(/\0*$/,"");break}t.data=t.value}},WXXX:function(t){var e;if(3===t.data[0])for(e=1;e<t.data.length;e++)if(0===t.data[e]){t.description=o(t.data,1,e),t.url=o(t.data,e+1,t.data.length);break}},PRIV:function(t){var e;for(e=0;e<t.data.length;e++)if(0===t.data[e]){t.owner=d(t.data,0,e);break}t.privateData=t.data.subarray(e+1),t.data=t.privateData}};a=function(t){var e,i={debug:!(!t||!t.debug),descriptor:t&&t.descriptor},n=0,r=[],o=0;if(a.prototype.init.call(this),this.dispatchType=s.METADATA_STREAM_TYPE.toString(16),i.descriptor)for(e=0;e<i.descriptor.length;e++)this.dispatchType+=("00"+i.descriptor[e].toString(16)).slice(-2);this.push=function(t){var e,a,s,d,c,u;if("timed-metadata"===t.type){if(t.dataAlignmentIndicator&&(o=0,r.length=0),0===r.length&&(t.data.length<10||t.data[0]!=="I".charCodeAt(0)||t.data[1]!=="D".charCodeAt(0)||t.data[2]!=="3".charCodeAt(0)))return void(i.debug&&console.log("Skipping unrecognized metadata packet"));if(r.push(t),o+=t.data.byteLength,1===r.length&&(n=p(t.data.subarray(6,10)),n+=10),!(o<n)){for(e={data:new Uint8Array(n),frames:[],pts:r[0].pts,dts:r[0].dts},c=0;c<n;)e.data.set(r[0].data.subarray(0,n-c),c),c+=r[0].data.byteLength,o-=r[0].data.byteLength,r.shift();a=10,64&e.data[5]&&(a+=4,a+=p(e.data.subarray(10,14)),n-=p(e.data.subarray(16,20)));do{if((s=p(e.data.subarray(a+4,a+8)))<1)return console.log("Malformed ID3 frame encountered. Skipping metadata parsing.");if(u=String.fromCharCode(e.data[a],e.data[a+1],e.data[a+2],e.data[a+3]),d={id:u,data:e.data.subarray(a+10,a+s+10)},d.key=d.id,h[d.id]&&(h[d.id](d),"com.apple.streaming.transportStreamTimestamp"===d.owner)){var l=d.data,m=(1&l[3])<<30|l[4]<<22|l[5]<<14|l[6]<<6|l[7]>>>2;m*=4,m+=3&l[7],d.timeStamp=m,void 0===e.pts&&void 0===e.dts&&(e.pts=d.timeStamp,e.dts=d.timeStamp),this.trigger("timestamp",d)}e.frames.push(d),a+=10,a+=s}while(a<n);this.trigger("data",e)}}}},a.prototype=new n,e.exports=a},{"../utils/stream":17,"./stream-types":8}],8:[function(t,e,i){"use strict";e.exports={H264_STREAM_TYPE:27,ADTS_STREAM_TYPE:15,METADATA_STREAM_TYPE:21}},{}],9:[function(t,e,i){"use strict";var a=t("../utils/stream"),n=function(t,e){var i=1;for(t>e&&(i=-1);Math.abs(e-t)>4294967296;)t+=8589934592*i;return t},s=function(t){var e,i;s.prototype.init.call(this),this.type_=t,this.push=function(t){t.type===this.type_&&(void 0===i&&(i=t.dts),t.dts=n(t.dts,i),t.pts=n(t.pts,i),e=t.dts,this.trigger("data",t))},this.flush=function(){i=e,this.trigger("done")},this.discontinuity=function(){i=void 0,e=void 0}};s.prototype=new a,e.exports={TimestampRolloverStream:s,handleRollover:n}},{"../utils/stream":17}],10:[function(t,e,i){"use strict";var a,n,s,r,o,d,p,h,c,u,l,m,f,g,y,S,_,b,T,v,k,A,C,w,E,x,D,P,L,U,M,O,R,I,B,N,G=Math.pow(2,32)-1;!function(){var t;if(C={avc1:[],avcC:[],btrt:[],dinf:[],dref:[],esds:[],ftyp:[],hdlr:[],mdat:[],mdhd:[],mdia:[],mfhd:[],minf:[],moof:[],moov:[],mp4a:[],mvex:[],mvhd:[],sdtp:[],smhd:[],stbl:[],stco:[],stsc:[],stsd:[],stsz:[],stts:[],styp:[],tfdt:[],tfhd:[],traf:[],trak:[],trun:[],trex:[],tkhd:[],vmhd:[]},"undefined"!=typeof Uint8Array){for(t in C)C.hasOwnProperty(t)&&(C[t]=[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]);w=new Uint8Array(["i".charCodeAt(0),"s".charCodeAt(0),"o".charCodeAt(0),"m".charCodeAt(0)]),x=new Uint8Array(["a".charCodeAt(0),"v".charCodeAt(0),"c".charCodeAt(0),"1".charCodeAt(0)]),E=new Uint8Array([0,0,0,1]),D=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]),P=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]),L={video:D,audio:P},O=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,12,117,114,108,32,0,0,0,1]),M=new Uint8Array([0,0,0,0,0,0,0,0]),R=new Uint8Array([0,0,0,0,0,0,0,0]),I=R,B=new Uint8Array([0,0,0,0,0,0,0,0,0,0,0,0]),N=R,U=new Uint8Array([0,0,0,1,0,0,0,0,0,0,0,0])}}(),a=function(t){var e,i,a,n=[],s=0;for(e=1;e<arguments.length;e++)n.push(arguments[e]);for(e=n.length;e--;)s+=n[e].byteLength;for(i=new Uint8Array(s+8),a=new DataView(i.buffer,i.byteOffset,i.byteLength),a.setUint32(0,i.byteLength),i.set(t,4),e=0,s=8;e<n.length;e++)i.set(n[e],s),s+=n[e].byteLength;return i},n=function(){return a(C.dinf,a(C.dref,O))},s=function(t){return a(C.esds,new Uint8Array([0,0,0,0,3,25,0,0,0,4,17,64,21,0,6,0,0,0,218,192,0,0,218,192,5,2,t.audioobjecttype<<3|t.samplingfrequencyindex>>>1,t.samplingfrequencyindex<<7|t.channelcount<<3,6,1,2]))},r=function(){return a(C.ftyp,w,E,w,x)},S=function(t){return a(C.hdlr,L[t])},o=function(t){return a(C.mdat,t)},y=function(t){var e=new Uint8Array([0,0,0,0,0,0,0,2,0,0,0,3,0,1,95,144,t.duration>>>24&255,t.duration>>>16&255,t.duration>>>8&255,255&t.duration,85,196,0,0]);return t.samplerate&&(e[12]=t.samplerate>>>24&255,e[13]=t.samplerate>>>16&255,e[14]=t.samplerate>>>8&255,e[15]=255&t.samplerate),a(C.mdhd,e)},g=function(t){return a(C.mdia,y(t),S(t.type),p(t))},d=function(t){return a(C.mfhd,new Uint8Array([0,0,0,0,(4278190080&t)>>24,(16711680&t)>>16,(65280&t)>>8,255&t]))},p=function(t){return a(C.minf,"video"===t.type?a(C.vmhd,U):a(C.smhd,M),n(),b(t))},h=function(t,e){for(var i=[],n=e.length;n--;)i[n]=v(e[n]);return a.apply(null,[C.moof,d(t)].concat(i))},c=function(t){for(var e=t.length,i=[];e--;)i[e]=m(t[e]);return a.apply(null,[C.moov,l(4294967295)].concat(i).concat(u(t)))},u=function(t){for(var e=t.length,i=[];e--;)i[e]=k(t[e]);return a.apply(null,[C.mvex].concat(i))},l=function(t){var e=new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,2,0,1,95,144,(4278190080&t)>>24,(16711680&t)>>16,(65280&t)>>8,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 a(C.mvhd,e)},_=function(t){var e,i,n=t.samples||[],s=new Uint8Array(4+n.length);for(i=0;i<n.length;i++)e=n[i].flags,s[i+4]=e.dependsOn<<4|e.isDependedOn<<2|e.hasRedundancy;return a(C.sdtp,s)},b=function(t){return a(C.stbl,T(t),a(C.stts,N),a(C.stsc,I),a(C.stsz,B),a(C.stco,R))},function(){var t,e;T=function(i){return a(C.stsd,new Uint8Array([0,0,0,0,0,0,0,1]),"video"===i.type?t(i):e(i))},t=function(t){var e,i=t.sps||[],n=t.pps||[],s=[],r=[];for(e=0;e<i.length;e++)s.push((65280&i[e].byteLength)>>>8),s.push(255&i[e].byteLength),s=s.concat(Array.prototype.slice.call(i[e]));for(e=0;e<n.length;e++)r.push((65280&n[e].byteLength)>>>8),r.push(255&n[e].byteLength),r=r.concat(Array.prototype.slice.call(n[e]));return a(C.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,(65280&t.width)>>8,255&t.width,(65280&t.height)>>8,255&t.height,0,72,0,0,0,72,0,0,0,0,0,0,0,1,19,118,105,100,101,111,106,115,45,99,111,110,116,114,105,98,45,104,108,115,0,0,0,0,0,0,0,0,0,0,0,0,0,24,17,17]),a(C.avcC,new Uint8Array([1,t.profileIdc,t.profileCompatibility,t.levelIdc,255].concat([i.length]).concat(s).concat([n.length]).concat(r))),a(C.btrt,new Uint8Array([0,28,156,128,0,45,198,192,0,45,198,192])))},e=function(t){return a(C.mp4a,new Uint8Array([0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,(65280&t.channelcount)>>8,255&t.channelcount,(65280&t.samplesize)>>8,255&t.samplesize,0,0,0,0,(65280&t.samplerate)>>8,255&t.samplerate,0,0]),s(t))}}(),f=function(t){var e=new Uint8Array([0,0,0,7,0,0,0,0,0,0,0,0,(4278190080&t.id)>>24,(16711680&t.id)>>16,(65280&t.id)>>8,255&t.id,0,0,0,0,(4278190080&t.duration)>>24,(16711680&t.duration)>>16,(65280&t.duration)>>8,255&t.duration,0,0,0,0,0,0,0,0,0,0,0,0,1,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,(65280&t.width)>>8,255&t.width,0,0,(65280&t.height)>>8,255&t.height,0,0]);return a(C.tkhd,e)},v=function(t){var e,i,n,s,r,o,d;return e=a(C.tfhd,new Uint8Array([0,0,0,58,(4278190080&t.id)>>24,(16711680&t.id)>>16,(65280&t.id)>>8,255&t.id,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0])),o=Math.floor(t.baseMediaDecodeTime/(G+1)),d=Math.floor(t.baseMediaDecodeTime%(G+1)),i=a(C.tfdt,new Uint8Array([1,0,0,0,o>>>24&255,o>>>16&255,o>>>8&255,255&o,d>>>24&255,d>>>16&255,d>>>8&255,255&d])),r=92,"audio"===t.type?(n=A(t,r),a(C.traf,e,i,n)):(s=_(t),n=A(t,s.length+r),a(C.traf,e,i,n,s))},m=function(t){return t.duration=t.duration||4294967295,a(C.trak,f(t),g(t))},k=function(t){var e=new Uint8Array([0,0,0,0,(4278190080&t.id)>>24,(16711680&t.id)>>16,(65280&t.id)>>8,255&t.id,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]);return"video"!==t.type&&(e[e.length-1]=0),a(C.trex,e)},function(){var t,e,i;i=function(t,e){var i=0,a=0,n=0,s=0;return t.length&&(void 0!==t[0].duration&&(i=1),void 0!==t[0].size&&(a=2),void 0!==t[0].flags&&(n=4),void 0!==t[0].compositionTimeOffset&&(s=8)),[0,0,i|a|n|s,1,(4278190080&t.length)>>>24,(16711680&t.length)>>>16,(65280&t.length)>>>8,255&t.length,(4278190080&e)>>>24,(16711680&e)>>>16,(65280&e)>>>8,255&e]},e=function(t,e){var n,s,r,o;for(s=t.samples||[],e+=20+16*s.length,n=i(s,e),o=0;o<s.length;o++)r=s[o],n=n.concat([(4278190080&r.duration)>>>24,(16711680&r.duration)>>>16,(65280&r.duration)>>>8,255&r.duration,(4278190080&r.size)>>>24,(16711680&r.size)>>>16,(65280&r.size)>>>8,255&r.size,r.flags.isLeading<<2|r.flags.dependsOn,r.flags.isDependedOn<<6|r.flags.hasRedundancy<<4|r.flags.paddingValue<<1|r.flags.isNonSyncSample,61440&r.flags.degradationPriority,15&r.flags.degradationPriority,(4278190080&r.compositionTimeOffset)>>>24,(16711680&r.compositionTimeOffset)>>>16,(65280&r.compositionTimeOffset)>>>8,255&r.compositionTimeOffset]);return a(C.trun,new Uint8Array(n))},t=function(t,e){var n,s,r,o;for(s=t.samples||[],e+=20+8*s.length,n=i(s,e),o=0;o<s.length;o++)r=s[o],n=n.concat([(4278190080&r.duration)>>>24,(16711680&r.duration)>>>16,(65280&r.duration)>>>8,255&r.duration,(4278190080&r.size)>>>24,(16711680&r.size)>>>16,(65280&r.size)>>>8,255&r.size]);return a(C.trun,new Uint8Array(n))},A=function(i,a){return"audio"===i.type?t(i,a):e(i,a)}}(),e.exports={ftyp:r,mdat:o,moof:h,moov:c,initSegment:function(t){var e,i=r(),a=c(t);return e=new Uint8Array(i.byteLength+a.byteLength),e.set(i),e.set(a,i.byteLength),e}}},{}],11:[function(t,e,i){"use strict";var a,n,s,r,o,d,p,h,c,u,l,m=t("../utils/stream.js"),f=t("./mp4-generator.js"),g=t("../m2ts/m2ts.js"),y=t("../codecs/adts.js"),S=t("../codecs/h264").H264Stream,_=t("../aac"),b=t("../data/silence"),T=t("../utils/clock"),v=["audioobjecttype","channelcount","samplerate","samplingfrequencyindex","samplesize"],k=["width","height","profileIdc","levelIdc","profileCompatibility"];o=function(){return{size:0,flags:{isLeading:0,dependsOn:1,isDependedOn:0,hasRedundancy:0,degradationPriority:0,isNonSyncSample:1}}},d=function(t){return t[0]==="I".charCodeAt(0)&&t[1]==="D".charCodeAt(0)&&t[2]==="3".charCodeAt(0)},u=function(t,e){var i;if(t.length!==e.length)return!1;for(i=0;i<t.length;i++)if(t[i]!==e[i])return!1;return!0},l=function(t){var e,i,a=0;for(e=0;e<t.length;e++)i=t[e],a+=i.data.byteLength;return a},n=function(t,e){var i=[],a=0,s=0,r=0,o=1/0;e=e||{},n.prototype.init.call(this),this.push=function(e){p(t,e),t&&v.forEach(function(i){t[i]=e[i]}),i.push(e)},this.setEarliestDts=function(e){s=e-t.timelineStartInfo.baseMediaDecodeTime},this.setVideoBaseMediaDecodeTime=function(t){o=t},this.setAudioAppendStart=function(t){r=t},this.flush=function(){var n,s,r,o;if(0===i.length)return void this.trigger("done","AudioSegmentStream");n=this.trimAdtsFramesByEarliestDts_(i),t.baseMediaDecodeTime=c(t,e.keepOriginalTimestamps),this.prefixWithSilence_(t,n),t.samples=this.generateSampleTable_(n),r=f.mdat(this.concatenateFrameData_(n)),i=[],s=f.moof(a,[t]),o=new Uint8Array(s.byteLength+r.byteLength),a++,o.set(s),o.set(r,s.byteLength), h(t),this.trigger("data",{track:t,boxes:o}),this.trigger("done","AudioSegmentStream")},this.prefixWithSilence_=function(t,e){var i,a,n,s=0,d=0,p=0,h=0;if(e.length&&(i=T.audioTsToVideoTs(t.baseMediaDecodeTime,t.samplerate),s=Math.ceil(9e4/(t.samplerate/1024)),r&&o&&(d=i-Math.max(r,o),p=Math.floor(d/s),h=p*s),!(p<1||h>45e3))){for(a=b[t.samplerate],a||(a=e[0].data),n=0;n<p;n++)e.splice(n,0,{data:a});t.baseMediaDecodeTime-=Math.floor(T.videoTsToAudioTs(h,t.samplerate))}},this.trimAdtsFramesByEarliestDts_=function(e){return t.minSegmentDts>=s?e:(t.minSegmentDts=1/0,e.filter(function(e){return e.dts>=s&&(t.minSegmentDts=Math.min(t.minSegmentDts,e.dts),t.minSegmentPts=t.minSegmentDts,!0)}))},this.generateSampleTable_=function(t){var e,i,a=[];for(e=0;e<t.length;e++)i=t[e],a.push({size:i.data.byteLength,duration:1024});return a},this.concatenateFrameData_=function(t){var e,i,a=0,n=new Uint8Array(l(t));for(e=0;e<t.length;e++)i=t[e],n.set(i.data,a),a+=i.data.byteLength;return n}},n.prototype=new m,a=function(t,e){var i,n,s=0,r=[],d=[];e=e||{},a.prototype.init.call(this),delete t.minPTS,this.gopCache_=[],this.push=function(e){p(t,e),"seq_parameter_set_rbsp"!==e.nalUnitType||i||(i=e.config,t.sps=[e.data],k.forEach(function(e){t[e]=i[e]},this)),"pic_parameter_set_rbsp"!==e.nalUnitType||n||(n=e.data,t.pps=[e.data]),r.push(e)},this.flush=function(){for(var i,a,n,o,u;r.length&&"access_unit_delimiter_rbsp"!==r[0].nalUnitType;)r.shift();if(0===r.length)return this.resetStream_(),void this.trigger("done","VideoSegmentStream");if(i=this.groupNalsIntoFrames_(r),a=this.groupFramesIntoGops_(i),d.length){var l;if(!(l=e.alignGopsAtEnd?this.alignGopsAtEnd_(a):this.alignGopsAtStart_(a)))return this.gopCache_.unshift({gop:a.pop(),pps:t.pps,sps:t.sps}),this.gopCache_.length=Math.min(6,this.gopCache_.length),r=[],this.resetStream_(),void this.trigger("done","VideoSegmentStream");h(t),a=l}p(t,a),t.samples=this.generateSampleTable_(a),o=f.mdat(this.concatenateNalData_(a)),t.baseMediaDecodeTime=c(t,e.keepOriginalTimestamps),this.trigger("processedGopsInfo",a.map(function(t){return{pts:t.pts,dts:t.dts,byteLength:t.byteLength}})),this.gopCache_.unshift({gop:a.pop(),pps:t.pps,sps:t.sps}),this.gopCache_.length=Math.min(6,this.gopCache_.length),r=[],this.trigger("baseMediaDecodeTime",t.baseMediaDecodeTime),this.trigger("timelineStartInfo",t.timelineStartInfo),n=f.moof(s,[t]),u=new Uint8Array(n.byteLength+o.byteLength),s++,u.set(n),u.set(o,n.byteLength),this.trigger("data",{track:t,boxes:u}),this.resetStream_(),this.trigger("done","VideoSegmentStream")},this.resetStream_=function(){h(t),i=void 0,n=void 0},this.getGopForFusion_=function(e){var i,a,n,s,r,o=1/0;for(r=0;r<this.gopCache_.length;r++)s=this.gopCache_[r],n=s.gop,t.pps&&u(t.pps[0],s.pps[0])&&t.sps&&u(t.sps[0],s.sps[0])&&(n.dts<t.timelineStartInfo.dts||(i=e.dts-n.dts-n.duration)>=-1e4&&i<=45e3&&(!a||o>i)&&(a=s,o=i));return a?a.gop:null},this.extendFirstKeyFrame_=function(t){var e;return!t[0][0].keyFrame&&t.length>1&&(e=t.shift(),t.byteLength-=e.byteLength,t.nalCount-=e.nalCount,t[0][0].dts=e.dts,t[0][0].pts=e.pts,t[0][0].duration+=e.duration),t},this.groupNalsIntoFrames_=function(t){var e,i,a=[],n=[];for(a.byteLength=0,e=0;e<t.length;e++)i=t[e],"access_unit_delimiter_rbsp"===i.nalUnitType?(a.length&&(a.duration=i.dts-a.dts,n.push(a)),a=[i],a.byteLength=i.data.byteLength,a.pts=i.pts,a.dts=i.dts):("slice_layer_without_partitioning_rbsp_idr"===i.nalUnitType&&(a.keyFrame=!0),a.duration=i.dts-a.dts,a.byteLength+=i.data.byteLength,a.push(i));return n.length&&(!a.duration||a.duration<=0)&&(a.duration=n[n.length-1].duration),n.push(a),n},this.groupFramesIntoGops_=function(t){var e,i,a=[],n=[];for(a.byteLength=0,a.nalCount=0,a.duration=0,a.pts=t[0].pts,a.dts=t[0].dts,n.byteLength=0,n.nalCount=0,n.duration=0,n.pts=t[0].pts,n.dts=t[0].dts,e=0;e<t.length;e++)i=t[e],i.keyFrame?(a.length&&(n.push(a),n.byteLength+=a.byteLength,n.nalCount+=a.nalCount,n.duration+=a.duration),a=[i],a.nalCount=i.length,a.byteLength=i.byteLength,a.pts=i.pts,a.dts=i.dts,a.duration=i.duration):(a.duration+=i.duration,a.nalCount+=i.length,a.byteLength+=i.byteLength,a.push(i));return n.length&&a.duration<=0&&(a.duration=n[n.length-1].duration),n.byteLength+=a.byteLength,n.nalCount+=a.nalCount,n.duration+=a.duration,n.push(a),n},this.generateSampleTable_=function(t,e){var i,a,n,s,r,d=e||0,p=[];for(i=0;i<t.length;i++)for(s=t[i],a=0;a<s.length;a++)r=s[a],n=o(),n.dataOffset=d,n.compositionTimeOffset=r.pts-r.dts,n.duration=r.duration,n.size=4*r.length,n.size+=r.byteLength,r.keyFrame&&(n.flags.dependsOn=2,n.flags.isNonSyncSample=0),d+=n.size,p.push(n);return p},this.concatenateNalData_=function(t){var e,i,a,n,s,r,o=0,d=t.byteLength,p=t.nalCount,h=d+4*p,c=new Uint8Array(h),u=new DataView(c.buffer);for(e=0;e<t.length;e++)for(n=t[e],i=0;i<n.length;i++)for(s=n[i],a=0;a<s.length;a++)r=s[a],u.setUint32(o,r.data.byteLength),o+=4,c.set(r.data,o),o+=r.data.byteLength;return c},this.alignGopsAtStart_=function(t){var e,i,a,n,s,r,o,p;for(s=t.byteLength,r=t.nalCount,o=t.duration,e=i=0;e<d.length&&i<t.length&&(a=d[e],n=t[i],a.pts!==n.pts);)n.pts>a.pts?e++:(i++,s-=n.byteLength,r-=n.nalCount,o-=n.duration);return 0===i?t:i===t.length?null:(p=t.slice(i),p.byteLength=s,p.duration=o,p.nalCount=r,p.pts=p[0].pts,p.dts=p[0].dts,p)},this.alignGopsAtEnd_=function(t){var e,i,a,n,s,r;for(e=d.length-1,i=t.length-1,s=null,r=!1;e>=0&&i>=0;){if(a=d[e],n=t[i],a.pts===n.pts){r=!0;break}a.pts>n.pts?e--:(e===d.length-1&&(s=i),i--)}if(!r&&null===s)return null;var o;if(0===(o=r?i:s))return t;var p=t.slice(o),h=p.reduce(function(t,e){return t.byteLength+=e.byteLength,t.duration+=e.duration,t.nalCount+=e.nalCount,t},{byteLength:0,duration:0,nalCount:0});return p.byteLength=h.byteLength,p.duration=h.duration,p.nalCount=h.nalCount,p.pts=p[0].pts,p.dts=p[0].dts,p},this.alignGopsWith=function(t){d=t}},a.prototype=new m,p=function(t,e){"number"==typeof e.pts&&(void 0===t.timelineStartInfo.pts&&(t.timelineStartInfo.pts=e.pts),void 0===t.minSegmentPts?t.minSegmentPts=e.pts:t.minSegmentPts=Math.min(t.minSegmentPts,e.pts),void 0===t.maxSegmentPts?t.maxSegmentPts=e.pts:t.maxSegmentPts=Math.max(t.maxSegmentPts,e.pts)),"number"==typeof e.dts&&(void 0===t.timelineStartInfo.dts&&(t.timelineStartInfo.dts=e.dts),void 0===t.minSegmentDts?t.minSegmentDts=e.dts:t.minSegmentDts=Math.min(t.minSegmentDts,e.dts),void 0===t.maxSegmentDts?t.maxSegmentDts=e.dts:t.maxSegmentDts=Math.max(t.maxSegmentDts,e.dts))},h=function(t){delete t.minSegmentDts,delete t.maxSegmentDts,delete t.minSegmentPts,delete t.maxSegmentPts},c=function(t,e){var i,a,n=t.minSegmentDts;return e||(n-=t.timelineStartInfo.dts),i=t.timelineStartInfo.baseMediaDecodeTime,i+=n,i=Math.max(0,i),"audio"===t.type&&(a=t.samplerate/9e4,i*=a,i=Math.floor(i)),i},r=function(t,e){this.numberOfTracks=0,this.metadataStream=e,void 0!==t.remux?this.remuxTracks=!!t.remux:this.remuxTracks=!0,this.pendingTracks=[],this.videoTrack=null,this.pendingBoxes=[],this.pendingCaptions=[],this.pendingMetadata=[],this.pendingBytes=0,this.emittedTracks=0,r.prototype.init.call(this),this.push=function(t){return t.text?this.pendingCaptions.push(t):t.frames?this.pendingMetadata.push(t):(this.pendingTracks.push(t.track),this.pendingBoxes.push(t.boxes),this.pendingBytes+=t.boxes.byteLength,"video"===t.track.type&&(this.videoTrack=t.track),void("audio"===t.track.type&&(this.audioTrack=t.track)))}},r.prototype=new m,r.prototype.flush=function(t){var e,i,a,n,s=0,r={captions:[],captionStreams:{},metadata:[],info:{}},o=0;if(this.pendingTracks.length<this.numberOfTracks){if("VideoSegmentStream"!==t&&"AudioSegmentStream"!==t)return;if(this.remuxTracks)return;if(0===this.pendingTracks.length)return void(++this.emittedTracks>=this.numberOfTracks&&(this.trigger("done"),this.emittedTracks=0))}for(this.videoTrack?(o=this.videoTrack.timelineStartInfo.pts,k.forEach(function(t){r.info[t]=this.videoTrack[t]},this)):this.audioTrack&&(o=this.audioTrack.timelineStartInfo.pts,v.forEach(function(t){r.info[t]=this.audioTrack[t]},this)),1===this.pendingTracks.length?r.type=this.pendingTracks[0].type:r.type="combined",this.emittedTracks+=this.pendingTracks.length,a=f.initSegment(this.pendingTracks),r.initSegment=new Uint8Array(a.byteLength),r.initSegment.set(a),r.data=new Uint8Array(this.pendingBytes),n=0;n<this.pendingBoxes.length;n++)r.data.set(this.pendingBoxes[n],s),s+=this.pendingBoxes[n].byteLength;for(n=0;n<this.pendingCaptions.length;n++)e=this.pendingCaptions[n],e.startTime=e.startPts-o,e.startTime/=9e4,e.endTime=e.endPts-o,e.endTime/=9e4,r.captionStreams[e.stream]=!0,r.captions.push(e);for(n=0;n<this.pendingMetadata.length;n++)i=this.pendingMetadata[n],i.cueTime=i.pts-o,i.cueTime/=9e4,r.metadata.push(i);r.metadata.dispatchType=this.metadataStream.dispatchType,this.pendingTracks.length=0,this.videoTrack=null,this.pendingBoxes.length=0,this.pendingCaptions.length=0,this.pendingBytes=0,this.pendingMetadata.length=0,this.trigger("data",r),this.emittedTracks>=this.numberOfTracks&&(this.trigger("done"),this.emittedTracks=0)},s=function(t){var e,i,o=this,p=!0;s.prototype.init.call(this),t=t||{},this.baseMediaDecodeTime=t.baseMediaDecodeTime||0,this.transmuxPipeline_={},this.setupAacPipeline=function(){var e={};this.transmuxPipeline_=e,e.type="aac",e.metadataStream=new g.MetadataStream,e.aacStream=new _,e.audioTimestampRolloverStream=new g.TimestampRolloverStream("audio"),e.timedMetadataTimestampRolloverStream=new g.TimestampRolloverStream("timed-metadata"),e.adtsStream=new y,e.coalesceStream=new r(t,e.metadataStream),e.headOfPipeline=e.aacStream,e.aacStream.pipe(e.audioTimestampRolloverStream).pipe(e.adtsStream),e.aacStream.pipe(e.timedMetadataTimestampRolloverStream).pipe(e.metadataStream).pipe(e.coalesceStream),e.metadataStream.on("timestamp",function(t){e.aacStream.setTimestamp(t.timeStamp)}),e.aacStream.on("data",function(a){"timed-metadata"!==a.type||e.audioSegmentStream||(i=i||{timelineStartInfo:{baseMediaDecodeTime:o.baseMediaDecodeTime},codec:"adts",type:"audio"},e.coalesceStream.numberOfTracks++,e.audioSegmentStream=new n(i,t),e.adtsStream.pipe(e.audioSegmentStream).pipe(e.coalesceStream))}),e.coalesceStream.on("data",this.trigger.bind(this,"data")),e.coalesceStream.on("done",this.trigger.bind(this,"done"))},this.setupTsPipeline=function(){var s={};this.transmuxPipeline_=s,s.type="ts",s.metadataStream=new g.MetadataStream,s.packetStream=new g.TransportPacketStream,s.parseStream=new g.TransportParseStream,s.elementaryStream=new g.ElementaryStream,s.videoTimestampRolloverStream=new g.TimestampRolloverStream("video"),s.audioTimestampRolloverStream=new g.TimestampRolloverStream("audio"),s.timedMetadataTimestampRolloverStream=new g.TimestampRolloverStream("timed-metadata"),s.adtsStream=new y,s.h264Stream=new S,s.captionStream=new g.CaptionStream,s.coalesceStream=new r(t,s.metadataStream),s.headOfPipeline=s.packetStream,s.packetStream.pipe(s.parseStream).pipe(s.elementaryStream),s.elementaryStream.pipe(s.videoTimestampRolloverStream).pipe(s.h264Stream),s.elementaryStream.pipe(s.audioTimestampRolloverStream).pipe(s.adtsStream),s.elementaryStream.pipe(s.timedMetadataTimestampRolloverStream).pipe(s.metadataStream).pipe(s.coalesceStream),s.h264Stream.pipe(s.captionStream).pipe(s.coalesceStream),s.elementaryStream.on("data",function(r){var d;if("metadata"===r.type){for(d=r.tracks.length;d--;)e||"video"!==r.tracks[d].type?i||"audio"!==r.tracks[d].type||(i=r.tracks[d],i.timelineStartInfo.baseMediaDecodeTime=o.baseMediaDecodeTime):(e=r.tracks[d],e.timelineStartInfo.baseMediaDecodeTime=o.baseMediaDecodeTime);e&&!s.videoSegmentStream&&(s.coalesceStream.numberOfTracks++,s.videoSegmentStream=new a(e,t),s.videoSegmentStream.on("timelineStartInfo",function(t){i&&(i.timelineStartInfo=t,s.audioSegmentStream.setEarliestDts(t.dts))}),s.videoSegmentStream.on("processedGopsInfo",o.trigger.bind(o,"gopInfo")),s.videoSegmentStream.on("baseMediaDecodeTime",function(t){i&&s.audioSegmentStream.setVideoBaseMediaDecodeTime(t)}),s.h264Stream.pipe(s.videoSegmentStream).pipe(s.coalesceStream)),i&&!s.audioSegmentStream&&(s.coalesceStream.numberOfTracks++,s.audioSegmentStream=new n(i,t),s.adtsStream.pipe(s.audioSegmentStream).pipe(s.coalesceStream))}}),s.coalesceStream.on("data",this.trigger.bind(this,"data")),s.coalesceStream.on("done",this.trigger.bind(this,"done"))},this.setBaseMediaDecodeTime=function(t){var a=this.transmuxPipeline_;this.baseMediaDecodeTime=t,i&&(i.timelineStartInfo.dts=void 0,i.timelineStartInfo.pts=void 0,h(i),i.timelineStartInfo.baseMediaDecodeTime=t,a.audioTimestampRolloverStream&&a.audioTimestampRolloverStream.discontinuity()),e&&(a.videoSegmentStream&&(a.videoSegmentStream.gopCache_=[],a.videoTimestampRolloverStream.discontinuity()),e.timelineStartInfo.dts=void 0,e.timelineStartInfo.pts=void 0,h(e),a.captionStream.reset(),e.timelineStartInfo.baseMediaDecodeTime=t),a.timedMetadataTimestampRolloverStream&&a.timedMetadataTimestampRolloverStream.discontinuity()},this.setAudioAppendStart=function(t){i&&this.transmuxPipeline_.audioSegmentStream.setAudioAppendStart(t)},this.alignGopsWith=function(t){e&&this.transmuxPipeline_.videoSegmentStream&&this.transmuxPipeline_.videoSegmentStream.alignGopsWith(t)},this.push=function(t){if(p){var e=d(t);e&&"aac"!==this.transmuxPipeline_.type?this.setupAacPipeline():e||"ts"===this.transmuxPipeline_.type||this.setupTsPipeline(),p=!1}this.transmuxPipeline_.headOfPipeline.push(t)},this.flush=function(){p=!0,this.transmuxPipeline_.headOfPipeline.flush()},this.resetCaptions=function(){this.transmuxPipeline_.captionStream&&this.transmuxPipeline_.captionStream.reset()}},s.prototype=new m,e.exports={Transmuxer:s,VideoSegmentStream:a,AudioSegmentStream:n,AUDIO_PROPERTIES:v,VIDEO_PROPERTIES:k}},{"../aac":1,"../codecs/adts.js":2,"../codecs/h264":3,"../data/silence":4,"../m2ts/m2ts.js":6,"../utils/clock":15,"../utils/stream.js":17,"./mp4-generator.js":10}],12:[function(t,e,i){"use strict";var a=t("../utils/stream.js"),n=t("../mp4/mp4-generator.js"),s=["width","height","profileIdc","levelIdc","profileCompatibility"],r=function(t){this.numberOfTracks=0,this.pendingTracks=[],this.pendingBoxes=[],this.videoTrack=null,this.pendingBytes=0,this.emittedTracks=0,r.prototype.init.call(this),this.push=function(t){this.pendingTracks.push(t.track),this.pendingBoxes.push(t.boxes),this.pendingBytes+=t.boxes.byteLength,"video"===t.track.type?this.videoTrack=t.track:console.log("Error: unknown output type: "+t.track.type)}};r.prototype=new a,r.prototype.flush=function(t){var e,i,a=0,r={captions:[],captionStreams:{},metadata:[],info:{}};if(this.pendingTracks.length<this.numberOfTracks){if("VideoSegmentStream"!==t)return;if(0===this.pendingTracks.length)return void(++this.emittedTracks>=this.numberOfTracks&&(this.trigger("done"),this.emittedTracks=0))}for(this.videoTrack&&s.forEach(function(t){r.info[t]=this.videoTrack[t]},this),1===this.pendingTracks.length?r.type=this.pendingTracks[0].type:r.type="combined",this.emittedTracks+=this.pendingTracks.length,e=n.initSegment(this.pendingTracks),r.initSegment=new Uint8Array(e.byteLength),r.initSegment.set(e),r.data=new Uint8Array(this.pendingBytes),i=0;i<this.pendingBoxes.length;i++)r.data.set(this.pendingBoxes[i],a),a+=this.pendingBoxes[i].byteLength;this.pendingTracks.length=0,this.videoTrack=null,this.pendingBoxes.length=0,this.pendingBytes=0,this.trigger("data",r),this.emittedTracks>=this.numberOfTracks&&(this.trigger("done"),this.emittedTracks=0)},e.exports=r},{"../mp4/mp4-generator.js":10,"../utils/stream.js":17}],13:[function(t,e,i){e.exports={Transmuxer:t("./transmuxer").Transmuxer}},{"./transmuxer":14}],14:[function(t,e,i){var a,n=t("../utils/stream.js"),s=t("../codecs/h264").H264Stream,r=t("../mp4/transmuxer").VideoSegmentStream,o=t("./coalesce-stream.js"),d=new Uint8Array([0,0,0,1,9]);a=function(t){a.prototype.init.call(this),t=t||{},this.baseMediaDecodeTime=t.baseMediaDecodeTime||0,this.transmuxPipeline_,this.setupPipeline=function(){var e={};this.transmuxPipeline_=e,e.type="rawh264";var i={timelineStartInfo:{baseMediaDecodeTime:0},id:0,codec:"avc",type:"video"};e.h264Stream=new s,e.videoSegmentStream=new r(i,t),e.coalesceStream=new o(t),e.coalesceStream.numberOfTracks++,e.h264Stream.pipe(e.videoSegmentStream).pipe(e.coalesceStream),e.coalesceStream.on("data",this.trigger.bind(this,"data")),e.coalesceStream.on("done",this.trigger.bind(this,"done")),e.headOfPipeline=e.h264Stream},this.setupPipeline(),this.push=function(t){pts=dts=9e4*t.timestamp,packetData=new Uint8Array(t.data.byteLength+d.byteLength),packetData.set(d),packetData.set(t.data,d.byteLength);var e={type:"video",trackId:0,pts:pts,dts:dts,data:packetData};this.transmuxPipeline_.headOfPipeline.push(e)},this.flush=function(){this.transmuxPipeline_.headOfPipeline.flush()}},a.prototype=new n,e.exports={Transmuxer:a}},{"../codecs/h264":3,"../mp4/transmuxer":11,"../utils/stream.js":17,"./coalesce-stream.js":12}],15:[function(t,e,i){var a,n,s,r,o,d;a=function(t){return 9e4*t},n=function(t,e){return t*e},s=function(t){return t/9e4},r=function(t,e){return t/e},o=function(t,e){return a(r(t,e))},d=function(t,e){return n(s(t),e)},e.exports={secondsToVideoTs:a,secondsToAudioTs:n,videoTsToSeconds:s,audioTsToSeconds:r,audioTsToVideoTs:o,videoTsToAudioTs:d}},{}],16:[function(t,e,i){"use strict";var a;a=function(t){var e=t.byteLength,i=0,a=0;this.length=function(){return 8*e},this.bitsAvailable=function(){return 8*e+a},this.loadWord=function(){var n=t.byteLength-e,s=