jmuxer 2.0.7 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -33
- package/dist/jmuxer.min.js +3416 -1
- package/package.json +11 -6
- package/src/controller/remux.js +63 -46
- package/src/jmuxer.js +39 -144
- package/src/parsers/aac.js +11 -38
- package/src/parsers/h264.js +101 -96
- package/src/parsers/h265.js +303 -0
- package/src/remuxer/aac.js +63 -7
- package/src/remuxer/base.js +2 -1
- package/src/remuxer/h264.js +163 -7
- package/src/remuxer/h265.js +288 -0
- package/src/util/debug.js +3 -3
- package/src/util/event.js +2 -2
- package/src/util/mp4-generator.js +124 -9
- package/loader-thumb.jpg +0 -0
- package/packet-format.png +0 -0
- package/src/util/nalu.js +0 -72
package/dist/jmuxer.min.js
CHANGED
|
@@ -1 +1,3416 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("stream")):"function"==typeof define&&define.amd?define(["stream"],t):(e="undefined"!=typeof globalThis?globalThis:e||self).JMuxer=t(e.stream)}(this,(function(e){"use strict";function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t(e)}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function r(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function i(e,t,n){return t&&r(e.prototype,t),n&&r(e,n),e}function a(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&u(e,t)}function o(e){return o=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)},o(e)}function u(e,t){return u=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e},u(e,t)}function c(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function f(e,t){if(t&&("object"==typeof t||"function"==typeof t))return t;if(void 0!==t)throw new TypeError("Derived constructors may only return object or undefined");return c(e)}function l(e){var t=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}();return function(){var n,r=o(e);if(t){var i=o(this).constructor;n=Reflect.construct(r,arguments,i)}else n=r.apply(this,arguments);return f(this,n)}}function d(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==n)return;var r,i,a=[],s=!0,o=!1;try{for(n=n.call(e);!(s=(r=n.next()).done)&&(a.push(r.value),!t||a.length!==t);s=!0);}catch(e){o=!0,i=e}finally{try{s||null==n.return||n.return()}finally{if(o)throw i}}return a}(e,t)||h(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function h(e,t){if(e){if("string"==typeof e)return p(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?p(e,t):void 0}}function p(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function y(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!n){if(Array.isArray(e)||(n=h(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0,i=function(){};return{s:i,n:function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}},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(){n=n.call(e)},n:function(){var e=n.next();return s=e.done,e},e:function(e){o=!0,a=e},f:function(){try{s||null==n.return||n.return()}finally{if(o)throw a}}}}var v,m;function k(e){if(v){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];v.apply(void 0,[e].concat(n))}}function g(e){if(m){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];m.apply(void 0,[e].concat(n))}}var b=function(){function e(t){n(this,e),this.payload=t,this.nri=(96&this.payload[0])>>5,this.ntype=31&this.payload[0],this.isvcl=1==this.ntype||5==this.ntype,this.stype="",this.isfmb=!1}return i(e,[{key:"toString",value:function(){return"".concat(e.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===e.IDR}},{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}}],[{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 a(t={},e.IDR,"IDR"),a(t,e.SEI,"SEI"),a(t,e.SPS,"SPS"),a(t,e.PPS,"PPS"),a(t,e.NDR,"NDR"),a(t,e.AUD,"AUD"),t}},{key:"type",value:function(t){return t.ntype in e.TYPES?e.TYPES[t.ntype]:"UNKNOWN"}}]),e}();function S(e,t){var n=new Uint8Array((0|e.byteLength)+(0|t.byteLength));return n.set(e,0),n.set(t,0|e.byteLength),n}var w=function(){function e(t){n(this,e),this.data=t,this.index=0,this.bitLength=8*t.byteLength}return i(e,[{key:"setData",value:function(e){this.data=e,this.index=0,this.bitLength=8*e.byteLength}},{key:"bitsAvailable",get:function(){return this.bitLength-this.index}},{key:"skipBits",value:function(e){if(this.bitsAvailable<e)return!1;this.index+=e}},{key:"readBits",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=this.getBits(e,this.index,t);return n}},{key:"getBits",value:function(e,t){var n=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];if(this.bitsAvailable<e)return 0;var r=t%8,i=this.data[t/8|0]&255>>>r,a=8-r;if(a>=e)return n&&(this.index+=e),i>>a-e;n&&(this.index+=a);var s=e-a;return i<<s|this.getBits(s,t+a,n)}},{key:"skipLZ",value:function(){var e;for(e=0;e<this.bitLength-this.index;++e)if(0!==this.getBits(1,this.index+e,!1))return this.index+=e,e;return e}},{key:"skipUEG",value:function(){this.skipBits(1+this.skipLZ())}},{key:"skipEG",value:function(){this.skipBits(1+this.skipLZ())}},{key:"readUEG",value:function(){var e=this.skipLZ();return this.readBits(e+1)-1}},{key:"readEG",value:function(){var e=this.readUEG();return 1&e?1+e>>>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)}}]),e}(),x=function(){function e(t){n(this,e),this.remuxer=t,this.track=t.mp4track}return i(e,[{key:"parseSPS",value:function(t){var n=e.readSPS(new Uint8Array(t));this.track.fps=n.fps,this.track.width=n.width,this.track.height=n.height,this.track.sps=[new Uint8Array(t)],this.track.codec="avc1.";for(var r=new DataView(t.buffer,t.byteOffset+1,4),i=0;i<3;++i){var a=r.getUint8(i).toString(16);a.length<2&&(a="0"+a),this.track.codec+=a}}},{key:"parsePPS",value:function(e){this.track.pps=[new Uint8Array(e)]}},{key:"parseNAL",value:function(e){if(!e)return!1;var t=!1;switch(e.type()){case b.IDR:case b.NDR:t=!0;break;case b.PPS:this.track.pps||(this.parsePPS(e.getPayload()),!this.remuxer.readyToDecode&&this.track.pps&&this.track.sps&&(this.remuxer.readyToDecode=!0)),t=!0;break;case b.SPS:this.track.sps||(this.parseSPS(e.getPayload()),!this.remuxer.readyToDecode&&this.track.pps&&this.track.sps&&(this.remuxer.readyToDecode=!0)),t=!0;break;case b.AUD:k("AUD - ignoing");break;case b.SEI:k("SEI - ignoing")}return t}}],[{key:"extractNALu",value:function(e){for(var t,n,r=0,i=e.byteLength,a=0,s=[],o=0;r<i;)switch(t=e[r++],a){case 0:0===t&&(a=1);break;case 1:a=0===t?2:0;break;case 2:case 3:0===t?a=3:1===t&&r<i?(o!=r-a-1&&s.push(e.subarray(o,r-a-1)),o=r,a=0):a=0}return o<i&&(n=e.subarray(o,i)),[s,n]}},{key:"skipScalingList",value:function(e,t){for(var n=8,r=8,i=0;i<t;i++)0!==r&&(r=(n+e.readEG()+256)%256),n=0===r?n:r}},{key:"readSPS",value:function(t){var n,r,i,a,s,o,u=new w(t),c=0,f=0,l=0,d=0,h=1,p=0;u.readUByte();for(var y=[],v=t.byteLength,m=1;m<v;m++)m+2<v&&3===u.readBits(24,!1)?(y.push(u.readBits(8)),y.push(u.readBits(8)),m+=2,u.readBits(8)):y.push(u.readBits(8));if(u.setData(new Uint8Array(y)),n=u.readUByte(),u.readBits(5),u.skipBits(3),u.readUByte(),u.skipUEG(),100===n||110===n||122===n||244===n||44===n||83===n||86===n||118===n||128===n){var k=u.readUEG();if(3===k&&u.skipBits(1),u.skipUEG(),u.skipUEG(),u.skipBits(1),u.readBoolean()){o=3!==k?8:12;for(var g=0;g<o;++g)u.readBoolean()&&(g<6?e.skipScalingList(u,16):e.skipScalingList(u,64))}}u.skipUEG();var b=u.readUEG();if(0===b)u.readUEG();else if(1===b){u.skipBits(1),u.skipEG(),u.skipEG(),r=u.readUEG();for(var S=0;S<r;++S)u.skipEG()}if(u.skipUEG(),u.skipBits(1),i=u.readUEG(),a=u.readUEG(),0===(s=u.readBits(1))&&u.skipBits(1),u.skipBits(1),u.readBoolean()&&(c=u.readUEG(),f=u.readUEG(),l=u.readUEG(),d=u.readUEG()),u.readBoolean()){if(u.readBoolean()){var x;switch(u.readUByte()){case 1:x=[1,1];break;case 2:x=[12,11];break;case 3:x=[10,11];break;case 4:x=[16,11];break;case 5:x=[40,33];break;case 6:x=[24,11];break;case 7:x=[20,11];break;case 8:x=[32,11];break;case 9:x=[80,33];break;case 10:x=[18,11];break;case 11:x=[15,11];break;case 12:x=[64,33];break;case 13:x=[160,99];break;case 14:x=[4,3];break;case 15:x=[3,2];break;case 16:x=[2,1];break;case 255:x=[u.readUByte()<<8|u.readUByte(),u.readUByte()<<8|u.readUByte()]}x&&x[0]>0&&x[1]>0&&(h=x[0]/x[1])}if(u.readBoolean()&&u.skipBits(1),u.readBoolean()&&(u.skipBits(4),u.readBoolean()&&u.skipBits(24)),u.readBoolean()&&(u.skipUEG(),u.skipUEG()),u.readBoolean()){var A=u.readUInt(),U=u.readUInt();u.readBoolean()&&(p=U/(2*A))}}return{fps:p>0?p:void 0,width:Math.ceil((16*(i+1)-2*c-2*f)*h),height:(2-s)*(a+1)*16-(s?2:4)*(l+d)}}},{key:"parseHeader",value:function(e){var t=new w(e.getPayload());t.readUByte(),e.isfmb=0===t.readUEG(),e.stype=t.readUEG()}}]),e}(),A=function(){function e(t){n(this,e),this.remuxer=t,this.track=t.mp4track}return i(e,[{key:"setAACConfig",value:function(){var t,n,r,i=new Uint8Array(2),a=e.aacHeader;a&&(t=1+((192&a[2])>>>6),n=(60&a[2])>>>2,r=(1&a[2])<<2,r|=(192&a[3])>>>6,i[0]=t<<3,i[0]|=(14&n)>>1,i[1]|=(1&n)<<7,i[1]|=r<<3,this.track.codec="mp4a.40."+t,this.track.channelCount=r,this.track.config=i,this.remuxer.readyToDecode=!0)}}],[{key:"samplingRateMap",get:function(){return[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350]}},{key:"getHeaderLength",value:function(e){return 1&e[1]?7:9}},{key:"getFrameLength",value:function(e){return(3&e[3])<<11|e[4]<<3|(224&e[5])>>>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,r,i=0,a=t.byteLength,s=[];if(!e.isAACPattern(t))return g("Invalid ADTS audio format"),s;for(n=e.getHeaderLength(t),e.aacHeader||(e.aacHeader=t.subarray(0,n));i<a;)r=e.getFrameLength(t),s.push(t.subarray(n,r)),t=t.slice(r),i+=r;return s}}]),e}();a(A,"aacHeader",void 0);var U=function(){function e(t){n(this,e),this.listener={},this.type=""|t}return i(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 n=this.listener[e].indexOf(t);return n>-1&&this.listener[e].splice(n,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}(),B=function(){function e(){n(this,e)}return i(e,null,[{key:"init",value:function(){var t;for(t in 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:[]},e.types)e.types.hasOwnProperty(t)&&(e.types[t]=[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]);var n=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]),r=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:n,audio:r};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,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];for(var i,a=8,s=n.length,o=s;s--;)a+=n[s].byteLength;for((i=new Uint8Array(a))[0]=a>>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<o;++s)i.set(n[s],a),a+=n[s].byteLength;return i}},{key:"hdlr",value:function(t){return e.box(e.types.hdlr,e.HDLR_TYPES[t])}},{key:"mdat",value:function(t){return e.box(e.types.mdat,t)}},{key:"mdhd",value:function(t,n){return e.box(e.types.mdhd,new Uint8Array([0,0,0,0,0,0,0,2,0,0,0,3,t>>24&255,t>>16&255,t>>8&255,255&t,n>>24,n>>16&255,n>>8&255,255&n,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,n,r){return e.box(e.types.moof,e.mfhd(t),e.traf(r,n))}},{key:"moov",value:function(t,n,r){for(var i=t.length,a=[];i--;)a[i]=e.trak(t[i]);return e.box.apply(null,[e.types.moov,e.mvhd(r,n)].concat(a).concat(e.mvex(t)))}},{key:"mvex",value:function(t){for(var n=t.length,r=[];n--;)r[n]=e.trex(t[n]);return e.box.apply(null,[e.types.mvex].concat(r))}},{key:"mvhd",value:function(t,n){var r=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,n>>24&255,n>>16&255,n>>8&255,255&n,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,r)}},{key:"sdtp",value:function(t){var n,r,i=t.samples||[],a=new Uint8Array(4+i.length);for(r=0;r<i.length;r++)n=i[r].flags,a[r+4]=n.dependsOn<<4|n.isDependedOn<<2|n.hasRedundancy;return e.box(e.types.sdtp,a)}},{key:"stbl",value:function(t){return e.box(e.types.stbl,e.stsd(t),e.box(e.types.stts,e.STTS),e.box(e.types.stsc,e.STSC),e.box(e.types.stsz,e.STSZ),e.box(e.types.stco,e.STCO))}},{key:"avc1",value:function(t){var n,r,i,a=[],s=[];for(n=0;n<t.sps.length;n++)i=(r=t.sps[n]).byteLength,a.push(i>>>8&255),a.push(255&i),a=a.concat(Array.prototype.slice.call(r));for(n=0;n<t.pps.length;n++)i=(r=t.pps[n]).byteLength,s.push(i>>>8&255),s.push(255&i),s=s.concat(Array.prototype.slice.call(r));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,c=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,c>>8&255,255&c,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,n=new Uint8Array(26+t+3);return n.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]),n.set(e.config,26),n.set([6,1,2],26+t),n}},{key:"mp4a",value:function(t){var n=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,n>>8&255,255&n,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 n=t.id,r=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,n>>24&255,n>>16&255,n>>8&255,255&n,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,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,n){var r=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,n>>24,n>>16&255,n>>8&255,255&n])),e.trun(t,r.length+16+16+8+16+8+8),r)}},{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 n=t.id;return e.box(e.types.trex,new Uint8Array([0,0,0,0,n>>24,n>>16&255,n>>8&255,255&n,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,1]))}},{key:"trun",value:function(t,n){var r,i,a,s,o,u,c=t.samples||[],f=c.length,l=12+16*f,d=new Uint8Array(l);for(n+=8+l,d.set([0,0,15,1,f>>>24&255,f>>>16&255,f>>>8&255,255&f,n>>>24&255,n>>>16&255,n>>>8&255,255&n],0),r=0;r<f;r++)a=(i=c[r]).duration,s=i.size,o=i.flags,u=i.cts,d.set([a>>>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*r);return e.box(e.types.trun,d)}},{key:"initSegment",value:function(t,n,r){e.types||e.init();var i,a=e.moov(t,n,r);return(i=new Uint8Array(e.FTYP.byteLength+a.byteLength)).set(e.FTYP),i.set(a,e.FTYP.byteLength),i}}]),e}(),D=1,C=function(){function e(){n(this,e)}return i(e,[{key:"flush",value:function(){this.mp4track.len=0,this.mp4track.samples=[]}},{key:"isReady",value:function(){return!(!this.readyToDecode||!this.samples.length)||null}}],[{key:"getTrackID",value:function(){return D++}}]),e}(),E=function(e){s(r,e);var t=l(r);function r(e){var i;return n(this,r),(i=t.call(this)).readyToDecode=!1,i.nextDts=0,i.dts=0,i.mp4track={id:C.getTrackID(),type:"audio",channelCount:0,len:0,fragmented:!0,timescale:e,duration:e,samples:[],config:"",codec:""},i.samples=[],i.aac=new A(c(i)),i}return i(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.codec="",this.mp4track.channelCount="",this.mp4track.config="",this.mp4track.timescale=this.timescale,this.nextDts=0,this.dts=0}},{key:"remux",value:function(e){if(e.length>0)for(var t=0;t<e.length;t++){var n=e[t],r=n.units,i=r.byteLength;this.samples.push({units:r,size:i,duration:n.duration}),this.mp4track.len+=i,this.readyToDecode||this.aac.setAACConfig()}}},{key:"getPayload",value:function(){if(!this.isReady())return null;var e,t,n=new Uint8Array(this.mp4track.len),r=0,i=this.mp4track.samples;for(this.dts=this.nextDts;this.samples.length;){var a=this.samples.shift();a.units,(t=a.duration)<=0?(k("remuxer: invalid sample duration at DTS: ".concat(this.nextDts," :").concat(t)),this.mp4track.len-=a.size):(this.nextDts+=t,e={size:a.size,duration:t,cts:0,flags:{isLeading:0,isDependedOn:0,hasRedundancy:0,degradPrio:0,dependsOn:1}},n.set(a.units,r),r+=a.size,i.push(e))}return i.length?new Uint8Array(n.buffer,0,this.mp4track.len):null}},{key:"getAacParser",value:function(){return this.aac}}]),r}(C),T=function(e){s(r,e);var t=l(r);function r(e){var i;return n(this,r),(i=t.call(this)).readyToDecode=!1,i.nextDts=0,i.dts=0,i.mp4track={id:C.getTrackID(),type:"video",len:0,fragmented:!0,sps:"",pps:"",fps:30,width:0,height:0,timescale:e,duration:e,samples:[]},i.samples=[],i.h264=new x(c(i)),i}return i(r,[{key:"resetTrack",value:function(){this.readyToDecode=!1,this.mp4track.sps="",this.mp4track.pps="",this.nextDts=0,this.dts=0}},{key:"remux",value:function(e){var t,n=y(e);try{for(n.s();!(t=n.n()).done;){var r,i=t.value,a=[],s=0,o=y(i.units);try{for(o.s();!(r=o.n()).done;){var u=r.value;this.h264.parseNAL(u)&&(a.push(u),s+=u.getSize())}}catch(e){o.e(e)}finally{o.f()}a.length>0&&this.readyToDecode&&(this.mp4track.len+=s,this.samples.push({units:a,size:s,keyFrame:i.keyFrame,duration:i.duration,compositionTimeOffset:i.compositionTimeOffset}))}}catch(e){n.e(e)}finally{n.f()}}},{key:"getPayload",value:function(){if(!this.isReady())return null;var e,t,n=new Uint8Array(this.mp4track.len),r=0,i=this.mp4track.samples;for(this.dts=this.nextDts;this.samples.length;){var a=this.samples.shift(),s=a.units;if((t=a.duration)<=0)k("remuxer: invalid sample duration at DTS: ".concat(this.nextDts," :").concat(t)),this.mp4track.len-=a.size;else{this.nextDts+=t,e={size:a.size,duration:t,cts:a.compositionTimeOffset||0,flags:{isLeading:0,isDependedOn:0,hasRedundancy:0,degradPrio:0,isNonSync:a.keyFrame?0:1,dependsOn:a.keyFrame?2:1}};var o,u=y(s);try{for(u.s();!(o=u.n()).done;){var c=o.value;n.set(c.getData(),r),r+=c.getSize()}}catch(e){u.e(e)}finally{u.f()}i.push(e)}}return i.length?new Uint8Array(n.buffer,0,this.mp4track.len):null}}]),r}(C),P=function(e){s(r,e);var t=l(r);function r(e){var i;return n(this,r),(i=t.call(this,"remuxer")).initialized=!1,i.trackTypes=[],i.tracks={},i.seq=1,i.env=e,i.timescale=1e3,i.mediaDuration=0,i.aacParser=null,i}return i(r,[{key:"addTrack",value:function(e){if("video"!==e&&"both"!==e||(this.tracks.video=new T(this.timescale),this.trackTypes.push("video")),"audio"===e||"both"===e){var t=new E(this.timescale);this.aacParser=t.getAacParser(),this.tracks.audio=t,this.trackTypes.push("audio")}}},{key:"reset",value:function(){var e,t=y(this.trackTypes);try{for(t.s();!(e=t.n()).done;){var n=e.value;this.tracks[n].resetTrack()}}catch(e){t.e(e)}finally{t.f()}this.initialized=!1}},{key:"destroy",value:function(){this.tracks={},this.offAll()}},{key:"flush",value:function(){if(this.initialized){var e,t=y(this.trackTypes);try{for(t.s();!(e=t.n()).done;){var n=e.value,r=this.tracks[n],i=r.getPayload();if(i&&i.byteLength){var a={type:n,payload:S(B.moof(this.seq,r.dts,r.mp4track),B.mdat(i)),dts:r.dts};"video"===n&&(a.fps=r.mp4track.fps),this.dispatch("buffer",a);var s=(o=r.dts/this.timescale,u=void 0,c=void 0,f=void 0,l=void 0,l="",u=Math.floor(o),(c=parseInt(u/3600,10)%24)>0&&(l+=(c<10?"0"+c:c)+":"),l+=((f=parseInt(u/60,10)%60)<10?"0"+f:f)+":"+((u=u<0?0:u%60)<10?"0"+u:u));k("put segment (".concat(n,"): dts: ").concat(r.dts," frames: ").concat(r.mp4track.samples.length," second: ").concat(s)),r.flush(),this.seq++}}}catch(e){t.e(e)}finally{t.f()}}else this.isReady()&&(this.dispatch("ready"),this.initSegment(),this.initialized=!0,this.flush());var o,u,c,f,l}},{key:"initSegment",value:function(){var e,t=[],n=y(this.trackTypes);try{for(n.s();!(e=n.n()).done;){var r=e.value,i=this.tracks[r];if("browser"==this.env){var a={type:r,payload:B.initSegment([i.mp4track],this.mediaDuration,this.timescale)};this.dispatch("buffer",a)}else t.push(i.mp4track)}}catch(e){n.e(e)}finally{n.f()}if("node"==this.env){var s={type:"all",payload:B.initSegment(t,this.mediaDuration,this.timescale)};this.dispatch("buffer",s)}k("Initial segment generated.")}},{key:"isReady",value:function(){var e,t=y(this.trackTypes);try{for(t.s();!(e=t.n()).done;){var n=e.value;if(!this.tracks[n].readyToDecode||!this.tracks[n].samples.length)return!1}}catch(e){t.e(e)}finally{t.f()}return!0}},{key:"remux",value:function(e){var t,n=y(this.trackTypes);try{for(n.s();!(t=n.n()).done;){var r=t.value,i=e[r];"audio"===r&&this.tracks.video&&!this.tracks.video.readyToDecode||i.length>0&&this.tracks[r].remux(i)}}catch(e){n.e(e)}finally{n.f()}this.flush()}}]),r}(U),L=function(e){s(r,e);var t=l(r);function r(e,i){var a;return n(this,r),(a=t.call(this,"buffer")).type=i,a.queue=new Uint8Array,a.cleaning=!1,a.pendingCleaning=0,a.cleanOffset=30,a.cleanRanges=[],a.sourceBuffer=e,a.sourceBuffer.addEventListener("updateend",(function(){a.pendingCleaning>0&&(a.initCleanup(a.pendingCleaning),a.pendingCleaning=0),a.cleaning=!1,a.cleanRanges.length&&a.doCleanup()})),a.sourceBuffer.addEventListener("error",(function(){a.dispatch("error",{type:a.type,name:"buffer",error:"buffer error"})})),a}return i(r,[{key:"destroy",value:function(){this.queue=null,this.sourceBuffer=null,this.offAll()}},{key:"doCleanup",value:function(){if(this.cleanRanges.length){var e=this.cleanRanges.shift();k("".concat(this.type," remove range [").concat(e[0]," - ").concat(e[1],")")),this.cleaning=!0,this.sourceBuffer.remove(e[0],e[1])}else this.cleaning=!1}},{key:"initCleanup",value:function(e){try{if(this.sourceBuffer.updating)return void(this.pendingCleaning=e);if(this.sourceBuffer.buffered&&this.sourceBuffer.buffered.length&&!this.cleaning){for(var t=0;t<this.sourceBuffer.buffered.length;++t){var n=this.sourceBuffer.buffered.start(t),r=this.sourceBuffer.buffered.end(t);e-n>this.cleanOffset&&n<(r=e-this.cleanOffset)&&this.cleanRanges.push([n,r])}this.doCleanup()}}catch(e){g("Error occured while cleaning ".concat(this.type," buffer - ").concat(e.name,": ").concat(e.message))}}},{key:"doAppend",value:function(){if(this.queue.length&&this.sourceBuffer&&!this.sourceBuffer.updating)try{this.sourceBuffer.appendBuffer(this.queue),this.queue=new Uint8Array}catch(t){var e="unexpectedError";"QuotaExceededError"===t.name?(k("".concat(this.type," buffer quota full")),e="QuotaExceeded"):(g("Error occured while appending ".concat(this.type," buffer - ").concat(t.name,": ").concat(t.message)),e="InvalidStateError"),this.dispatch("error",{type:this.type,name:e,error:"buffer error"})}}},{key:"feed",value:function(e){this.queue=S(this.queue,e)}}]),r}(U);return function(r){s(o,r);var a=l(o);function o(e){var r;n(this,o),(r=a.call(this,"jmuxer")).isReset=!1;return r.options=Object.assign({},{node:"",mode:"both",flushingTime:500,maxDelay:500,clearBuffer:!0,fps:30,readFpsFromTrack:!1,debug:!1,onReady:function(){},onData:function(){},onError:function(){},onMissingVideoFrames:function(){},onMissingAudioFrames:function(){}},e),r.env="object"===("undefined"==typeof process?"undefined":t(process))&&"undefined"==typeof window?"node":"browser",r.options.debug&&(v=console.log,m=console.error),r.options.fps||(r.options.fps=30),r.frameDuration=1e3/r.options.fps|0,r.remuxController=new P(r.env),r.remuxController.addTrack(r.options.mode),r.initData(),r.remuxController.on("buffer",r.onBuffer.bind(c(r))),"browser"==r.env&&(r.remuxController.on("ready",r.createBuffer.bind(c(r))),r.initBrowser()),r}return i(o,[{key:"initData",value:function(){this.lastCleaningTime=Date.now(),this.kfPosition=[],this.kfCounter=0,this.pendingUnits={},this.remainingData=new Uint8Array,this.startInterval()}},{key:"initBrowser",value:function(){"string"==typeof this.options.node&&""==this.options.node&&g("no video element were found to render, provide a valid video element"),this.node="string"==typeof this.options.node?document.getElementById(this.options.node):this.options.node,this.mseReady=!1,this.setupMSE()}},{key:"createStream",value:function(){var t=this.feed.bind(this),n=this.destroy.bind(this);return this.stream=new e.Duplex({writableObjectMode:!0,read:function(e){},write:function(e,n,r){t(e),r()},final:function(e){n(),e()}}),this.stream}},{key:"setupMSE",value:function(){if(window.MediaSource=window.MediaSource||window.WebKitMediaSource||window.ManagedMediaSource,!window.MediaSource)throw"Oops! Browser does not support Media Source Extension or Managed Media Source (IOS 17+).";if(this.isMSESupported=!!window.MediaSource,this.mediaSource=new window.MediaSource,this.url=URL.createObjectURL(this.mediaSource),window.MediaSource===window.ManagedMediaSource)try{this.node.removeAttribute("src"),this.node.disableRemotePlayback=!0;var e=document.createElement("source");e.type="video/mp4",e.src=this.url,this.node.appendChild(e),this.node.load()}catch(e){this.node.src=this.url}else this.node.src=this.url;this.mseEnded=!1,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:"endMSE",value:function(){if(!this.mseEnded)try{this.mseEnded=!0,this.mediaSource.endOfStream()}catch(e){g("mediasource is not available to end")}}},{key:"feed",value:function(e){var t,n,r,i=!1,a={video:[],audio:[]};if(e&&this.remuxController){if(r=e.duration?parseInt(e.duration):0,e.video){e.video=S(this.remainingData,e.video);var s=d(x.extractNALu(e.video),2);if(t=s[0],n=s[1],this.remainingData=n||new Uint8Array,!(t.length>0))return g("Failed to extract any NAL units from video data:",n),void("function"==typeof this.options.onMissingVideoFrames&&this.options.onMissingVideoFrames.call(null,e));a.video=this.getVideoFrames(t,r,e.compositionTimeOffset),i=!0}if(e.audio){if(!((t=A.extractAAC(e.audio)).length>0))return g("Failed to extract audio data from:",e.audio),void("function"==typeof this.options.onMissingAudioFrames&&this.options.onMissingAudioFrames.call(null,e));a.audio=this.getAudioFrames(t,r),i=!0}i?this.remuxController.remux(a):g("Input object must have video and/or audio property. Make sure it is a valid typed array")}}},{key:"getVideoFrames",value:function(e,t,n){var r,i=this,a=[],s=[],o=0,u=!1,c=!1;this.pendingUnits.units&&(a=this.pendingUnits.units,c=this.pendingUnits.vcl,u=this.pendingUnits.keyFrame,this.pendingUnits={});var f,l=y(e);try{for(l.s();!(f=l.n()).done;){var d=f.value,h=new b(d);h.type()!==b.IDR&&h.type()!==b.NDR||x.parseHeader(h),a.length&&c&&(h.isfmb||!h.isvcl)&&(s.push({units:a,keyFrame:u}),a=[],u=!1,c=!1),a.push(h),u=u||h.isKeyframe(),c=c||h.isvcl}}catch(e){l.e(e)}finally{l.f()}if(a.length)if(t)if(c)s.push({units:a,keyFrame:u});else{var p=s.length-1;p>=0&&(s[p].units=s[p].units.concat(a))}else this.pendingUnits={units:a,keyFrame:u,vcl:c};return r=t?t/s.length|0:this.frameDuration,o=t?t-r*s.length:0,s.map((function(e){e.duration=r,e.compositionTimeOffset=n,o>0&&(e.duration++,o--),i.kfCounter++,e.keyFrame&&i.options.clearBuffer&&i.kfPosition.push(i.kfCounter*r/1e3)})),k("jmuxer: No. of frames of the last chunk: ".concat(s.length)),s}},{key:"getAudioFrames",value:function(e,t){var n,r,i=[],a=0,s=y(e);try{for(s.s();!(r=s.n()).done;){var o=r.value;i.push({units:o})}}catch(e){s.e(e)}finally{s.f()}return n=t?t/i.length|0:this.frameDuration,a=t?t-n*i.length:0,i.map((function(e){e.duration=n,a>0&&(e.duration++,a--)})),i}},{key:"destroy",value:function(){if(this.stopInterval(),this.stream&&(this.remuxController.flush(),this.stream.push(null),this.stream=null),this.remuxController&&(this.remuxController.destroy(),this.remuxController=null),this.bufferControllers){for(var e in this.bufferControllers)this.bufferControllers[e].destroy();this.bufferControllers=null,this.endMSE()}this.node=!1,this.mseReady=!1,this.videoStarted=!1,this.mediaSource=null}},{key:"reset",value:function(){if(this.stopInterval(),this.isReset=!0,this.node.pause(),this.remuxController&&this.remuxController.reset(),this.bufferControllers){for(var e in this.bufferControllers)this.bufferControllers[e].destroy();this.bufferControllers=null,this.endMSE()}this.initData(),"browser"==this.env&&this.initBrowser(),k("JMuxer was reset")}},{key:"createBuffer",value:function(){if(this.mseReady&&this.remuxController&&this.remuxController.isReady()&&!this.bufferControllers)for(var e in this.bufferControllers={},this.remuxController.tracks){var t=this.remuxController.tracks[e];if(!o.isSupported("".concat(e,'/mp4; codecs="').concat(t.mp4track.codec,'"')))return g("Browser does not support codec"),!1;var n=this.mediaSource.addSourceBuffer("".concat(e,'/mp4; codecs="').concat(t.mp4track.codec,'"'));this.bufferControllers[e]=new L(n,e),this.bufferControllers[e].on("error",this.onBufferError.bind(this))}}},{key:"startInterval",value:function(){var e=this;this.interval=setInterval((function(){e.options.flushingTime?e.applyAndClearBuffer():e.bufferControllers&&e.cancelDelay()}),this.options.flushingTime||1e3)}},{key:"stopInterval",value:function(){this.interval&&clearInterval(this.interval)}},{key:"cancelDelay",value:function(){if(this.node.buffered&&this.node.buffered.length>0&&!this.node.seeking){var e=this.node.buffered.end(0);e-this.node.currentTime>this.options.maxDelay/1e3&&(console.log("delay"),this.node.currentTime=e-.001)}}},{key:"releaseBuffer",value:function(){for(var e in this.bufferControllers)this.bufferControllers[e].doAppend()}},{key:"applyAndClearBuffer",value:function(){this.bufferControllers&&(this.releaseBuffer(),this.clearBuffer())}},{key:"getSafeClearOffsetOfBuffer",value:function(e){for(var t,n="audio"===this.options.mode&&e||0,r=0;r<this.kfPosition.length&&!(this.kfPosition[r]>=e);r++)t=this.kfPosition[r];return t&&(this.kfPosition=this.kfPosition.filter((function(e){return e<t&&(n=e),e>=t}))),n}},{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.options.readFpsFromTrack&&void 0!==e.fps&&this.options.fps!=e.fps&&(this.options.fps=e.fps,this.frameDuration=Math.ceil(1e3/e.fps),k("JMuxer changed FPS to ".concat(e.fps," from track data"))),"browser"==this.env?this.bufferControllers&&this.bufferControllers[e.type]&&this.bufferControllers[e.type].feed(e.payload):this.stream&&this.stream.push(e.payload),this.options.onData&&this.options.onData(e.payload),0===this.options.flushingTime&&this.applyAndClearBuffer()}},{key:"onMSEOpen",value:function(){this.mseReady=!0,URL.revokeObjectURL(this.url),"function"==typeof this.options.onReady&&this.options.onReady.call(null,this.isReset)}},{key:"onMSEClose",value:function(){this.mseReady=!1,this.videoStarted=!1}},{key:"onBufferError",value:function(e){if("QuotaExceeded"==e.name)return k("JMuxer cleaning ".concat(e.type," buffer due to QuotaExceeded error")),void this.bufferControllers[e.type].initCleanup(this.node.currentTime);"InvalidStateError"==e.name?(k("JMuxer is reseting due to InvalidStateError"),this.reset()):this.endMSE(),"function"==typeof this.options.onError&&this.options.onError.call(null,e)}}],[{key:"isSupported",value:function(e){return window.MediaSource&&window.MediaSource.isTypeSupported(e)}}]),o}(U)}));
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('stream')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['stream'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.JMuxer = factory(global.stream));
|
|
5
|
+
})(this, (function (stream) { 'use strict';
|
|
6
|
+
|
|
7
|
+
function _arrayLikeToArray(r, a) {
|
|
8
|
+
(null == a || a > r.length) && (a = r.length);
|
|
9
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
10
|
+
return n;
|
|
11
|
+
}
|
|
12
|
+
function _arrayWithHoles(r) {
|
|
13
|
+
if (Array.isArray(r)) return r;
|
|
14
|
+
}
|
|
15
|
+
function _arrayWithoutHoles(r) {
|
|
16
|
+
if (Array.isArray(r)) return _arrayLikeToArray(r);
|
|
17
|
+
}
|
|
18
|
+
function _assertThisInitialized(e) {
|
|
19
|
+
if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
20
|
+
return e;
|
|
21
|
+
}
|
|
22
|
+
function _classCallCheck(a, n) {
|
|
23
|
+
if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
|
|
24
|
+
}
|
|
25
|
+
function _defineProperties(e, r) {
|
|
26
|
+
for (var t = 0; t < r.length; t++) {
|
|
27
|
+
var o = r[t];
|
|
28
|
+
o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function _createClass(e, r, t) {
|
|
32
|
+
return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
|
|
33
|
+
writable: !1
|
|
34
|
+
}), e;
|
|
35
|
+
}
|
|
36
|
+
function _createForOfIteratorHelper(r, e) {
|
|
37
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
38
|
+
if (!t) {
|
|
39
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
|
|
40
|
+
t && (r = t);
|
|
41
|
+
var n = 0,
|
|
42
|
+
F = function () {};
|
|
43
|
+
return {
|
|
44
|
+
s: F,
|
|
45
|
+
n: function () {
|
|
46
|
+
return n >= r.length ? {
|
|
47
|
+
done: !0
|
|
48
|
+
} : {
|
|
49
|
+
done: !1,
|
|
50
|
+
value: r[n++]
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
e: function (r) {
|
|
54
|
+
throw r;
|
|
55
|
+
},
|
|
56
|
+
f: F
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
60
|
+
}
|
|
61
|
+
var o,
|
|
62
|
+
a = !0,
|
|
63
|
+
u = !1;
|
|
64
|
+
return {
|
|
65
|
+
s: function () {
|
|
66
|
+
t = t.call(r);
|
|
67
|
+
},
|
|
68
|
+
n: function () {
|
|
69
|
+
var r = t.next();
|
|
70
|
+
return a = r.done, r;
|
|
71
|
+
},
|
|
72
|
+
e: function (r) {
|
|
73
|
+
u = !0, o = r;
|
|
74
|
+
},
|
|
75
|
+
f: function () {
|
|
76
|
+
try {
|
|
77
|
+
a || null == t.return || t.return();
|
|
78
|
+
} finally {
|
|
79
|
+
if (u) throw o;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function _createSuper(t) {
|
|
85
|
+
var r = _isNativeReflectConstruct();
|
|
86
|
+
return function () {
|
|
87
|
+
var e,
|
|
88
|
+
o = _getPrototypeOf(t);
|
|
89
|
+
if (r) {
|
|
90
|
+
var s = _getPrototypeOf(this).constructor;
|
|
91
|
+
e = Reflect.construct(o, arguments, s);
|
|
92
|
+
} else e = o.apply(this, arguments);
|
|
93
|
+
return _possibleConstructorReturn(this, e);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function _defineProperty(e, r, t) {
|
|
97
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
98
|
+
value: t,
|
|
99
|
+
enumerable: !0,
|
|
100
|
+
configurable: !0,
|
|
101
|
+
writable: !0
|
|
102
|
+
}) : e[r] = t, e;
|
|
103
|
+
}
|
|
104
|
+
function _getPrototypeOf(t) {
|
|
105
|
+
return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) {
|
|
106
|
+
return t.__proto__ || Object.getPrototypeOf(t);
|
|
107
|
+
}, _getPrototypeOf(t);
|
|
108
|
+
}
|
|
109
|
+
function _inherits(t, e) {
|
|
110
|
+
if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function");
|
|
111
|
+
t.prototype = Object.create(e && e.prototype, {
|
|
112
|
+
constructor: {
|
|
113
|
+
value: t,
|
|
114
|
+
writable: !0,
|
|
115
|
+
configurable: !0
|
|
116
|
+
}
|
|
117
|
+
}), Object.defineProperty(t, "prototype", {
|
|
118
|
+
writable: !1
|
|
119
|
+
}), e && _setPrototypeOf(t, e);
|
|
120
|
+
}
|
|
121
|
+
function _isNativeReflectConstruct() {
|
|
122
|
+
try {
|
|
123
|
+
var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
|
124
|
+
} catch (t) {}
|
|
125
|
+
return (_isNativeReflectConstruct = function () {
|
|
126
|
+
return !!t;
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
function _iterableToArray(r) {
|
|
130
|
+
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
|
|
131
|
+
}
|
|
132
|
+
function _iterableToArrayLimit(r, l) {
|
|
133
|
+
var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
134
|
+
if (null != t) {
|
|
135
|
+
var e,
|
|
136
|
+
n,
|
|
137
|
+
i,
|
|
138
|
+
u,
|
|
139
|
+
a = [],
|
|
140
|
+
f = !0,
|
|
141
|
+
o = !1;
|
|
142
|
+
try {
|
|
143
|
+
if (i = (t = t.call(r)).next, 0 === l) {
|
|
144
|
+
if (Object(t) !== t) return;
|
|
145
|
+
f = !1;
|
|
146
|
+
} else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
|
|
147
|
+
} catch (r) {
|
|
148
|
+
o = !0, n = r;
|
|
149
|
+
} finally {
|
|
150
|
+
try {
|
|
151
|
+
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
152
|
+
} finally {
|
|
153
|
+
if (o) throw n;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return a;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function _nonIterableRest() {
|
|
160
|
+
throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
161
|
+
}
|
|
162
|
+
function _nonIterableSpread() {
|
|
163
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
164
|
+
}
|
|
165
|
+
function _possibleConstructorReturn(t, e) {
|
|
166
|
+
if (e && ("object" == typeof e || "function" == typeof e)) return e;
|
|
167
|
+
if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined");
|
|
168
|
+
return _assertThisInitialized(t);
|
|
169
|
+
}
|
|
170
|
+
function _setPrototypeOf(t, e) {
|
|
171
|
+
return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
|
|
172
|
+
return t.__proto__ = e, t;
|
|
173
|
+
}, _setPrototypeOf(t, e);
|
|
174
|
+
}
|
|
175
|
+
function _slicedToArray(r, e) {
|
|
176
|
+
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
177
|
+
}
|
|
178
|
+
function _toConsumableArray(r) {
|
|
179
|
+
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
|
|
180
|
+
}
|
|
181
|
+
function _toPrimitive(t, r) {
|
|
182
|
+
if ("object" != typeof t || !t) return t;
|
|
183
|
+
var e = t[Symbol.toPrimitive];
|
|
184
|
+
if (void 0 !== e) {
|
|
185
|
+
var i = e.call(t, r || "default");
|
|
186
|
+
if ("object" != typeof i) return i;
|
|
187
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
188
|
+
}
|
|
189
|
+
return ("string" === r ? String : Number)(t);
|
|
190
|
+
}
|
|
191
|
+
function _toPropertyKey(t) {
|
|
192
|
+
var i = _toPrimitive(t, "string");
|
|
193
|
+
return "symbol" == typeof i ? i : i + "";
|
|
194
|
+
}
|
|
195
|
+
function _typeof(o) {
|
|
196
|
+
"@babel/helpers - typeof";
|
|
197
|
+
|
|
198
|
+
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
199
|
+
return typeof o;
|
|
200
|
+
} : function (o) {
|
|
201
|
+
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
202
|
+
}, _typeof(o);
|
|
203
|
+
}
|
|
204
|
+
function _unsupportedIterableToArray(r, a) {
|
|
205
|
+
if (r) {
|
|
206
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
207
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
208
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
var logger;
|
|
213
|
+
var errorLogger;
|
|
214
|
+
function setLogger(log, err) {
|
|
215
|
+
/*eslint-disable */
|
|
216
|
+
logger = log;
|
|
217
|
+
errorLogger = err;
|
|
218
|
+
/*eslint-enable */
|
|
219
|
+
}
|
|
220
|
+
function log(message) {
|
|
221
|
+
if (logger) {
|
|
222
|
+
for (var _len = arguments.length, optionalParams = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
223
|
+
optionalParams[_key - 1] = arguments[_key];
|
|
224
|
+
}
|
|
225
|
+
logger.apply(void 0, [message].concat(optionalParams));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function error(message) {
|
|
229
|
+
if (errorLogger) {
|
|
230
|
+
for (var _len2 = arguments.length, optionalParams = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
231
|
+
optionalParams[_key2 - 1] = arguments[_key2];
|
|
232
|
+
}
|
|
233
|
+
errorLogger.apply(void 0, [message].concat(optionalParams));
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
var Event = /*#__PURE__*/function () {
|
|
238
|
+
function Event(type) {
|
|
239
|
+
_classCallCheck(this, Event);
|
|
240
|
+
this.listener = {};
|
|
241
|
+
this.type = type | '';
|
|
242
|
+
}
|
|
243
|
+
_createClass(Event, [{
|
|
244
|
+
key: "on",
|
|
245
|
+
value: function on(event, fn) {
|
|
246
|
+
if (!this.listener[event]) {
|
|
247
|
+
this.listener[event] = [];
|
|
248
|
+
}
|
|
249
|
+
this.listener[event].push(fn);
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
}, {
|
|
253
|
+
key: "off",
|
|
254
|
+
value: function off(event, fn) {
|
|
255
|
+
if (this.listener[event]) {
|
|
256
|
+
var index = this.listener[event].indexOf(fn);
|
|
257
|
+
if (index > -1) {
|
|
258
|
+
this.listener[event].splice(index, 1);
|
|
259
|
+
}
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}, {
|
|
265
|
+
key: "offAll",
|
|
266
|
+
value: function offAll() {
|
|
267
|
+
this.listener = {};
|
|
268
|
+
}
|
|
269
|
+
}, {
|
|
270
|
+
key: "dispatch",
|
|
271
|
+
value: function dispatch(event) {
|
|
272
|
+
for (var _len = arguments.length, data = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
273
|
+
data[_key - 1] = arguments[_key];
|
|
274
|
+
}
|
|
275
|
+
if (this.listener[event]) {
|
|
276
|
+
this.listener[event].map(function (each) {
|
|
277
|
+
each.apply(null, data);
|
|
278
|
+
});
|
|
279
|
+
return true;
|
|
280
|
+
}
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
}]);
|
|
284
|
+
return Event;
|
|
285
|
+
}();
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Generate MP4 Box
|
|
289
|
+
* taken from: https://github.com/dailymotion/hls.js
|
|
290
|
+
*/
|
|
291
|
+
|
|
292
|
+
var MP4 = /*#__PURE__*/function () {
|
|
293
|
+
function MP4() {
|
|
294
|
+
_classCallCheck(this, MP4);
|
|
295
|
+
}
|
|
296
|
+
_createClass(MP4, null, [{
|
|
297
|
+
key: "init",
|
|
298
|
+
value: function init() {
|
|
299
|
+
MP4.types = {
|
|
300
|
+
avc1: [],
|
|
301
|
+
// codingname
|
|
302
|
+
avcC: [],
|
|
303
|
+
btrt: [],
|
|
304
|
+
dinf: [],
|
|
305
|
+
dref: [],
|
|
306
|
+
esds: [],
|
|
307
|
+
ftyp: [],
|
|
308
|
+
hdlr: [],
|
|
309
|
+
hev1: [],
|
|
310
|
+
hvcC: [],
|
|
311
|
+
mdat: [],
|
|
312
|
+
mdhd: [],
|
|
313
|
+
mdia: [],
|
|
314
|
+
mfhd: [],
|
|
315
|
+
minf: [],
|
|
316
|
+
moof: [],
|
|
317
|
+
moov: [],
|
|
318
|
+
mp4a: [],
|
|
319
|
+
mvex: [],
|
|
320
|
+
mvhd: [],
|
|
321
|
+
sdtp: [],
|
|
322
|
+
stbl: [],
|
|
323
|
+
stco: [],
|
|
324
|
+
stsc: [],
|
|
325
|
+
stsd: [],
|
|
326
|
+
stsz: [],
|
|
327
|
+
stts: [],
|
|
328
|
+
tfdt: [],
|
|
329
|
+
tfhd: [],
|
|
330
|
+
traf: [],
|
|
331
|
+
trak: [],
|
|
332
|
+
trun: [],
|
|
333
|
+
trex: [],
|
|
334
|
+
tkhd: [],
|
|
335
|
+
vmhd: [],
|
|
336
|
+
smhd: []
|
|
337
|
+
};
|
|
338
|
+
var i;
|
|
339
|
+
for (i in MP4.types) {
|
|
340
|
+
if (MP4.types.hasOwnProperty(i)) {
|
|
341
|
+
MP4.types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
var videoHdlr = new Uint8Array([0x00,
|
|
345
|
+
// version 0
|
|
346
|
+
0x00, 0x00, 0x00,
|
|
347
|
+
// flags
|
|
348
|
+
0x00, 0x00, 0x00, 0x00,
|
|
349
|
+
// pre_defined
|
|
350
|
+
0x76, 0x69, 0x64, 0x65,
|
|
351
|
+
// handler_type: 'vide'
|
|
352
|
+
0x00, 0x00, 0x00, 0x00,
|
|
353
|
+
// reserved
|
|
354
|
+
0x00, 0x00, 0x00, 0x00,
|
|
355
|
+
// reserved
|
|
356
|
+
0x00, 0x00, 0x00, 0x00,
|
|
357
|
+
// reserved
|
|
358
|
+
0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
|
|
359
|
+
]);
|
|
360
|
+
var audioHdlr = new Uint8Array([0x00,
|
|
361
|
+
// version 0
|
|
362
|
+
0x00, 0x00, 0x00,
|
|
363
|
+
// flags
|
|
364
|
+
0x00, 0x00, 0x00, 0x00,
|
|
365
|
+
// pre_defined
|
|
366
|
+
0x73, 0x6f, 0x75, 0x6e,
|
|
367
|
+
// handler_type: 'soun'
|
|
368
|
+
0x00, 0x00, 0x00, 0x00,
|
|
369
|
+
// reserved
|
|
370
|
+
0x00, 0x00, 0x00, 0x00,
|
|
371
|
+
// reserved
|
|
372
|
+
0x00, 0x00, 0x00, 0x00,
|
|
373
|
+
// reserved
|
|
374
|
+
0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
|
|
375
|
+
]);
|
|
376
|
+
MP4.HDLR_TYPES = {
|
|
377
|
+
video: videoHdlr,
|
|
378
|
+
audio: audioHdlr
|
|
379
|
+
};
|
|
380
|
+
var dref = new Uint8Array([0x00,
|
|
381
|
+
// version 0
|
|
382
|
+
0x00, 0x00, 0x00,
|
|
383
|
+
// flags
|
|
384
|
+
0x00, 0x00, 0x00, 0x01,
|
|
385
|
+
// entry_count
|
|
386
|
+
0x00, 0x00, 0x00, 0x0c,
|
|
387
|
+
// entry_size
|
|
388
|
+
0x75, 0x72, 0x6c, 0x20,
|
|
389
|
+
// 'url' type
|
|
390
|
+
0x00,
|
|
391
|
+
// version 0
|
|
392
|
+
0x00, 0x00, 0x01 // entry_flags
|
|
393
|
+
]);
|
|
394
|
+
var stco = new Uint8Array([0x00,
|
|
395
|
+
// version
|
|
396
|
+
0x00, 0x00, 0x00,
|
|
397
|
+
// flags
|
|
398
|
+
0x00, 0x00, 0x00, 0x00 // entry_count
|
|
399
|
+
]);
|
|
400
|
+
MP4.STTS = MP4.STSC = MP4.STCO = stco;
|
|
401
|
+
MP4.STSZ = new Uint8Array([0x00,
|
|
402
|
+
// version
|
|
403
|
+
0x00, 0x00, 0x00,
|
|
404
|
+
// flags
|
|
405
|
+
0x00, 0x00, 0x00, 0x00,
|
|
406
|
+
// sample_size
|
|
407
|
+
0x00, 0x00, 0x00, 0x00 // sample_count
|
|
408
|
+
]);
|
|
409
|
+
MP4.VMHD = new Uint8Array([0x00,
|
|
410
|
+
// version
|
|
411
|
+
0x00, 0x00, 0x01,
|
|
412
|
+
// flags
|
|
413
|
+
0x00, 0x00,
|
|
414
|
+
// graphicsmode
|
|
415
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
|
|
416
|
+
]);
|
|
417
|
+
MP4.SMHD = new Uint8Array([0x00,
|
|
418
|
+
// version
|
|
419
|
+
0x00, 0x00, 0x00,
|
|
420
|
+
// flags
|
|
421
|
+
0x00, 0x00,
|
|
422
|
+
// balance
|
|
423
|
+
0x00, 0x00 // reserved
|
|
424
|
+
]);
|
|
425
|
+
MP4.STSD = new Uint8Array([0x00,
|
|
426
|
+
// version 0
|
|
427
|
+
0x00, 0x00, 0x00,
|
|
428
|
+
// flags
|
|
429
|
+
0x00, 0x00, 0x00, 0x01]); // entry_count
|
|
430
|
+
|
|
431
|
+
var majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
|
|
432
|
+
var avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1
|
|
433
|
+
var minorVersion = new Uint8Array([0, 0, 0, 1]);
|
|
434
|
+
MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand);
|
|
435
|
+
MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
|
|
436
|
+
}
|
|
437
|
+
}, {
|
|
438
|
+
key: "box",
|
|
439
|
+
value: function box(type) {
|
|
440
|
+
for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
441
|
+
payload[_key - 1] = arguments[_key];
|
|
442
|
+
}
|
|
443
|
+
var size = 8,
|
|
444
|
+
i = payload.length,
|
|
445
|
+
len = i,
|
|
446
|
+
result;
|
|
447
|
+
// calculate the total size we need to allocate
|
|
448
|
+
while (i--) {
|
|
449
|
+
size += payload[i].byteLength;
|
|
450
|
+
}
|
|
451
|
+
result = new Uint8Array(size);
|
|
452
|
+
result[0] = size >> 24 & 0xff;
|
|
453
|
+
result[1] = size >> 16 & 0xff;
|
|
454
|
+
result[2] = size >> 8 & 0xff;
|
|
455
|
+
result[3] = size & 0xff;
|
|
456
|
+
result.set(type, 4);
|
|
457
|
+
// copy the payload into the result
|
|
458
|
+
for (i = 0, size = 8; i < len; ++i) {
|
|
459
|
+
// copy payload[i] array @ offset size
|
|
460
|
+
result.set(payload[i], size);
|
|
461
|
+
size += payload[i].byteLength;
|
|
462
|
+
}
|
|
463
|
+
return result;
|
|
464
|
+
}
|
|
465
|
+
}, {
|
|
466
|
+
key: "hdlr",
|
|
467
|
+
value: function hdlr(type) {
|
|
468
|
+
return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
|
|
469
|
+
}
|
|
470
|
+
}, {
|
|
471
|
+
key: "mdat",
|
|
472
|
+
value: function mdat(data) {
|
|
473
|
+
return MP4.box(MP4.types.mdat, data);
|
|
474
|
+
}
|
|
475
|
+
}, {
|
|
476
|
+
key: "mdhd",
|
|
477
|
+
value: function mdhd(timescale, duration) {
|
|
478
|
+
return MP4.box(MP4.types.mdhd, new Uint8Array([0x00,
|
|
479
|
+
// version 0
|
|
480
|
+
0x00, 0x00, 0x00,
|
|
481
|
+
// flags
|
|
482
|
+
0x00, 0x00, 0x00, 0x02,
|
|
483
|
+
// creation_time
|
|
484
|
+
0x00, 0x00, 0x00, 0x03,
|
|
485
|
+
// modification_time
|
|
486
|
+
timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF,
|
|
487
|
+
// timescale
|
|
488
|
+
duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF,
|
|
489
|
+
// duration
|
|
490
|
+
0x55, 0xc4,
|
|
491
|
+
// 'und' language (undetermined)
|
|
492
|
+
0x00, 0x00]));
|
|
493
|
+
}
|
|
494
|
+
}, {
|
|
495
|
+
key: "mdia",
|
|
496
|
+
value: function mdia(track) {
|
|
497
|
+
return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track));
|
|
498
|
+
}
|
|
499
|
+
}, {
|
|
500
|
+
key: "mfhd",
|
|
501
|
+
value: function mfhd(sequenceNumber) {
|
|
502
|
+
return MP4.box(MP4.types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00,
|
|
503
|
+
// flags
|
|
504
|
+
sequenceNumber >> 24, sequenceNumber >> 16 & 0xFF, sequenceNumber >> 8 & 0xFF, sequenceNumber & 0xFF // sequence_number
|
|
505
|
+
]));
|
|
506
|
+
}
|
|
507
|
+
}, {
|
|
508
|
+
key: "minf",
|
|
509
|
+
value: function minf(track) {
|
|
510
|
+
if (track.type === 'audio') {
|
|
511
|
+
return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track));
|
|
512
|
+
} else {
|
|
513
|
+
return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}, {
|
|
517
|
+
key: "moof",
|
|
518
|
+
value: function moof(sn, baseMediaDecodeTime, track) {
|
|
519
|
+
return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* @param tracks... (optional) {array} the tracks associated with this movie
|
|
523
|
+
*/
|
|
524
|
+
}, {
|
|
525
|
+
key: "moov",
|
|
526
|
+
value: function moov(tracks, duration, timescale) {
|
|
527
|
+
var i = tracks.length,
|
|
528
|
+
boxes = [];
|
|
529
|
+
while (i--) {
|
|
530
|
+
boxes[i] = MP4.trak(tracks[i]);
|
|
531
|
+
}
|
|
532
|
+
return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(timescale, duration)].concat(boxes).concat(MP4.mvex(tracks)));
|
|
533
|
+
}
|
|
534
|
+
}, {
|
|
535
|
+
key: "mvex",
|
|
536
|
+
value: function mvex(tracks) {
|
|
537
|
+
var i = tracks.length,
|
|
538
|
+
boxes = [];
|
|
539
|
+
while (i--) {
|
|
540
|
+
boxes[i] = MP4.trex(tracks[i]);
|
|
541
|
+
}
|
|
542
|
+
return MP4.box.apply(null, [MP4.types.mvex].concat(boxes));
|
|
543
|
+
}
|
|
544
|
+
}, {
|
|
545
|
+
key: "mvhd",
|
|
546
|
+
value: function mvhd(timescale, duration) {
|
|
547
|
+
var bytes = new Uint8Array([0x00,
|
|
548
|
+
// version 0
|
|
549
|
+
0x00, 0x00, 0x00,
|
|
550
|
+
// flags
|
|
551
|
+
0x00, 0x00, 0x00, 0x01,
|
|
552
|
+
// creation_time
|
|
553
|
+
0x00, 0x00, 0x00, 0x02,
|
|
554
|
+
// modification_time
|
|
555
|
+
timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF,
|
|
556
|
+
// timescale
|
|
557
|
+
duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF,
|
|
558
|
+
// duration
|
|
559
|
+
0x00, 0x01, 0x00, 0x00,
|
|
560
|
+
// 1.0 rate
|
|
561
|
+
0x01, 0x00,
|
|
562
|
+
// 1.0 volume
|
|
563
|
+
0x00, 0x00,
|
|
564
|
+
// reserved
|
|
565
|
+
0x00, 0x00, 0x00, 0x00,
|
|
566
|
+
// reserved
|
|
567
|
+
0x00, 0x00, 0x00, 0x00,
|
|
568
|
+
// reserved
|
|
569
|
+
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
|
570
|
+
// transformation: unity matrix
|
|
571
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
572
|
+
// pre_defined
|
|
573
|
+
0xff, 0xff, 0xff, 0xff // next_track_ID
|
|
574
|
+
]);
|
|
575
|
+
return MP4.box(MP4.types.mvhd, bytes);
|
|
576
|
+
}
|
|
577
|
+
}, {
|
|
578
|
+
key: "sdtp",
|
|
579
|
+
value: function sdtp(track) {
|
|
580
|
+
var samples = track.samples || [],
|
|
581
|
+
bytes = new Uint8Array(4 + samples.length),
|
|
582
|
+
flags,
|
|
583
|
+
i;
|
|
584
|
+
// leave the full box header (4 bytes) all zero
|
|
585
|
+
// write the sample table
|
|
586
|
+
for (i = 0; i < samples.length; i++) {
|
|
587
|
+
flags = samples[i].flags;
|
|
588
|
+
bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
|
|
589
|
+
}
|
|
590
|
+
return MP4.box(MP4.types.sdtp, bytes);
|
|
591
|
+
}
|
|
592
|
+
}, {
|
|
593
|
+
key: "stbl",
|
|
594
|
+
value: function stbl(track) {
|
|
595
|
+
return MP4.box(MP4.types.stbl, MP4.stsd(track), MP4.box(MP4.types.stts, MP4.STTS), MP4.box(MP4.types.stsc, MP4.STSC), MP4.box(MP4.types.stsz, MP4.STSZ), MP4.box(MP4.types.stco, MP4.STCO));
|
|
596
|
+
}
|
|
597
|
+
}, {
|
|
598
|
+
key: "avc1",
|
|
599
|
+
value: function avc1(track) {
|
|
600
|
+
var sps = [],
|
|
601
|
+
pps = [],
|
|
602
|
+
i,
|
|
603
|
+
data,
|
|
604
|
+
len;
|
|
605
|
+
// assemble the SPSs
|
|
606
|
+
|
|
607
|
+
for (i = 0; i < track.sps.length; i++) {
|
|
608
|
+
data = track.sps[i];
|
|
609
|
+
len = data.byteLength;
|
|
610
|
+
sps.push(len >>> 8 & 0xFF);
|
|
611
|
+
sps.push(len & 0xFF);
|
|
612
|
+
sps = sps.concat(Array.prototype.slice.call(data)); // SPS
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
// assemble the PPSs
|
|
616
|
+
for (i = 0; i < track.pps.length; i++) {
|
|
617
|
+
data = track.pps[i];
|
|
618
|
+
len = data.byteLength;
|
|
619
|
+
pps.push(len >>> 8 & 0xFF);
|
|
620
|
+
pps.push(len & 0xFF);
|
|
621
|
+
pps = pps.concat(Array.prototype.slice.call(data));
|
|
622
|
+
}
|
|
623
|
+
var avcc = MP4.box(MP4.types.avcC, new Uint8Array([0x01,
|
|
624
|
+
// version
|
|
625
|
+
sps[3],
|
|
626
|
+
// profile
|
|
627
|
+
sps[4],
|
|
628
|
+
// profile compat
|
|
629
|
+
sps[5],
|
|
630
|
+
// level
|
|
631
|
+
0xfc | 3,
|
|
632
|
+
// lengthSizeMinusOne, hard-coded to 4 bytes
|
|
633
|
+
0xE0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets
|
|
634
|
+
].concat(sps).concat([track.pps.length // numOfPictureParameterSets
|
|
635
|
+
]).concat(pps))),
|
|
636
|
+
// "PPS"
|
|
637
|
+
width = track.width,
|
|
638
|
+
height = track.height;
|
|
639
|
+
// console.log('avcc:' + Hex.hexDump(avcc));
|
|
640
|
+
return MP4.box(MP4.types.avc1, new Uint8Array([0x00, 0x00, 0x00,
|
|
641
|
+
// reserved
|
|
642
|
+
0x00, 0x00, 0x00,
|
|
643
|
+
// reserved
|
|
644
|
+
0x00, 0x01,
|
|
645
|
+
// data_reference_index
|
|
646
|
+
0x00, 0x00,
|
|
647
|
+
// pre_defined
|
|
648
|
+
0x00, 0x00,
|
|
649
|
+
// reserved
|
|
650
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
651
|
+
// pre_defined
|
|
652
|
+
width >> 8 & 0xFF, width & 0xff,
|
|
653
|
+
// width
|
|
654
|
+
height >> 8 & 0xFF, height & 0xff,
|
|
655
|
+
// height
|
|
656
|
+
0x00, 0x48, 0x00, 0x00,
|
|
657
|
+
// horizresolution
|
|
658
|
+
0x00, 0x48, 0x00, 0x00,
|
|
659
|
+
// vertresolution
|
|
660
|
+
0x00, 0x00, 0x00, 0x00,
|
|
661
|
+
// reserved
|
|
662
|
+
0x00, 0x01,
|
|
663
|
+
// frame_count
|
|
664
|
+
0x12, 0x62, 0x69, 0x6E, 0x65,
|
|
665
|
+
// binelpro.ru
|
|
666
|
+
0x6C, 0x70, 0x72, 0x6F, 0x2E, 0x72, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
667
|
+
// compressorname
|
|
668
|
+
0x00, 0x18,
|
|
669
|
+
// depth = 24
|
|
670
|
+
0x11, 0x11]),
|
|
671
|
+
// pre_defined = -1
|
|
672
|
+
avcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
|
|
673
|
+
// bufferSizeDB
|
|
674
|
+
0x00, 0x2d, 0xc6, 0xc0,
|
|
675
|
+
// maxBitrate
|
|
676
|
+
0x00, 0x2d, 0xc6, 0xc0])) // avgBitrate
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
}, {
|
|
680
|
+
key: "hev1",
|
|
681
|
+
value: function hev1(track) {
|
|
682
|
+
var vps = [],
|
|
683
|
+
sps = [],
|
|
684
|
+
pps = [],
|
|
685
|
+
data,
|
|
686
|
+
len;
|
|
687
|
+
|
|
688
|
+
// assemble the VPSs
|
|
689
|
+
for (var i = 0; i < (((_track$vps = track.vps) === null || _track$vps === void 0 ? void 0 : _track$vps.length) || 0); i++) {
|
|
690
|
+
var _track$vps;
|
|
691
|
+
data = track.vps[i];
|
|
692
|
+
len = data.byteLength;
|
|
693
|
+
vps.push(len >>> 8 & 0xFF, len & 0xFF);
|
|
694
|
+
vps = vps.concat(Array.prototype.slice.call(data));
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// assemble the SPSs
|
|
698
|
+
for (var _i = 0; _i < (((_track$sps = track.sps) === null || _track$sps === void 0 ? void 0 : _track$sps.length) || 0); _i++) {
|
|
699
|
+
var _track$sps;
|
|
700
|
+
data = track.sps[_i];
|
|
701
|
+
len = data.byteLength;
|
|
702
|
+
sps.push(len >>> 8 & 0xFF, len & 0xFF);
|
|
703
|
+
sps = sps.concat(Array.prototype.slice.call(data));
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// assemble the PPSs
|
|
707
|
+
for (var _i2 = 0; _i2 < (((_track$pps = track.pps) === null || _track$pps === void 0 ? void 0 : _track$pps.length) || 0); _i2++) {
|
|
708
|
+
var _track$pps;
|
|
709
|
+
data = track.pps[_i2];
|
|
710
|
+
len = data.byteLength;
|
|
711
|
+
pps.push(len >>> 8 & 0xFF, len & 0xFF);
|
|
712
|
+
pps = pps.concat(Array.prototype.slice.call(data));
|
|
713
|
+
}
|
|
714
|
+
var _track$hvcC = track.hvcC,
|
|
715
|
+
profile_space = _track$hvcC.profile_space,
|
|
716
|
+
tier_flag = _track$hvcC.tier_flag,
|
|
717
|
+
profile_idc = _track$hvcC.profile_idc,
|
|
718
|
+
profile_compatibility_flags = _track$hvcC.profile_compatibility_flags,
|
|
719
|
+
constraint_indicator_flags = _track$hvcC.constraint_indicator_flags,
|
|
720
|
+
level_idc = _track$hvcC.level_idc,
|
|
721
|
+
chroma_format_idc = _track$hvcC.chroma_format_idc;
|
|
722
|
+
var hvcc = MP4.box(MP4.types.hvcC, new Uint8Array([0x01,
|
|
723
|
+
// configurationVersion
|
|
724
|
+
profile_space << 6 | tier_flag << 5 | profile_idc, profile_compatibility_flags >> 24 & 0xFF, profile_compatibility_flags >> 16 & 0xFF, profile_compatibility_flags >> 8 & 0xFF, profile_compatibility_flags & 0xFF].concat(_toConsumableArray(constraint_indicator_flags), [level_idc, 0xF0, 0x00,
|
|
725
|
+
// min_spatial_segmentation_idc = 0
|
|
726
|
+
0xFC | 0,
|
|
727
|
+
// parallelismType = 0
|
|
728
|
+
0xFC | chroma_format_idc,
|
|
729
|
+
// chromaFormat (from SPS)
|
|
730
|
+
0xF8 | 0,
|
|
731
|
+
// bitDepthLumaMinus8 = 0 (8-bit)
|
|
732
|
+
0xF8 | 0,
|
|
733
|
+
// bitDepthChromaMinus8 = 0
|
|
734
|
+
0x00, 0x00,
|
|
735
|
+
// avgFrameRate = 0
|
|
736
|
+
0x03,
|
|
737
|
+
// constantFrameRate = 0, numTemporalLayers = 0, lengthSizeMinusOne = 3 (AKA 4)
|
|
738
|
+
0x03,
|
|
739
|
+
// numOfArrays
|
|
740
|
+
|
|
741
|
+
0x20,
|
|
742
|
+
// array_completeness + NAL_unit_type (32 = VPS)
|
|
743
|
+
0x00, 0x01], _toConsumableArray(vps), [0x21,
|
|
744
|
+
// NAL_unit_type (33 = SPS)
|
|
745
|
+
0x00, 0x01], _toConsumableArray(sps), [0x22,
|
|
746
|
+
// NAL_unit_type (34 = PPS)
|
|
747
|
+
0x00, 0x01], _toConsumableArray(pps))));
|
|
748
|
+
var width = track.width;
|
|
749
|
+
var height = track.height;
|
|
750
|
+
return MP4.box(MP4.types.hev1, new Uint8Array([0x00, 0x00, 0x00,
|
|
751
|
+
// reserved
|
|
752
|
+
0x00, 0x00, 0x00,
|
|
753
|
+
// reserved
|
|
754
|
+
0x00, 0x01,
|
|
755
|
+
// data_reference_index
|
|
756
|
+
0x00, 0x00,
|
|
757
|
+
// pre_defined
|
|
758
|
+
0x00, 0x00,
|
|
759
|
+
// reserved
|
|
760
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
761
|
+
// pre_defined
|
|
762
|
+
width >> 8 & 0xFF, width & 0xff, height >> 8 & 0xFF, height & 0xff, 0x00, 0x48, 0x00, 0x00,
|
|
763
|
+
// horizresolution
|
|
764
|
+
0x00, 0x48, 0x00, 0x00,
|
|
765
|
+
// vertresolution
|
|
766
|
+
0x00, 0x00, 0x00, 0x00,
|
|
767
|
+
// reserved
|
|
768
|
+
0x00, 0x01,
|
|
769
|
+
// frame_count
|
|
770
|
+
0x12, 0x62, 0x69, 0x6E, 0x65,
|
|
771
|
+
// 'binelpro.ru'
|
|
772
|
+
0x6C, 0x70, 0x72, 0x6F, 0x2E, 0x72, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
773
|
+
// compressorname padding
|
|
774
|
+
0x00, 0x18,
|
|
775
|
+
// depth = 24
|
|
776
|
+
0x11, 0x11 // pre_defined = -1
|
|
777
|
+
]), hvcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80,
|
|
778
|
+
// bufferSizeDB
|
|
779
|
+
0x00, 0x2d, 0xc6, 0xc0,
|
|
780
|
+
// maxBitrate
|
|
781
|
+
0x00, 0x2d, 0xc6, 0xc0 // avgBitrate
|
|
782
|
+
])));
|
|
783
|
+
}
|
|
784
|
+
}, {
|
|
785
|
+
key: "esds",
|
|
786
|
+
value: function esds(track) {
|
|
787
|
+
var configlen = track.config.byteLength;
|
|
788
|
+
var data = new Uint8Array(26 + configlen + 3);
|
|
789
|
+
data.set([0x00,
|
|
790
|
+
// version 0
|
|
791
|
+
0x00, 0x00, 0x00,
|
|
792
|
+
// flags
|
|
793
|
+
|
|
794
|
+
0x03,
|
|
795
|
+
// descriptor_type
|
|
796
|
+
0x17 + configlen,
|
|
797
|
+
// length
|
|
798
|
+
0x00, 0x01,
|
|
799
|
+
// es_id
|
|
800
|
+
0x00,
|
|
801
|
+
// stream_priority
|
|
802
|
+
|
|
803
|
+
0x04,
|
|
804
|
+
// descriptor_type
|
|
805
|
+
0x0f + configlen,
|
|
806
|
+
// length
|
|
807
|
+
0x40,
|
|
808
|
+
// codec : mpeg4_audio
|
|
809
|
+
0x15,
|
|
810
|
+
// stream_type
|
|
811
|
+
0x00, 0x00, 0x00,
|
|
812
|
+
// buffer_size
|
|
813
|
+
0x00, 0x00, 0x00, 0x00,
|
|
814
|
+
// maxBitrate
|
|
815
|
+
0x00, 0x00, 0x00, 0x00,
|
|
816
|
+
// avgBitrate
|
|
817
|
+
|
|
818
|
+
0x05,
|
|
819
|
+
// descriptor_type
|
|
820
|
+
configlen]);
|
|
821
|
+
data.set(track.config, 26);
|
|
822
|
+
data.set([0x06, 0x01, 0x02], 26 + configlen);
|
|
823
|
+
// return new Uint8Array([
|
|
824
|
+
// 0x00, // version 0
|
|
825
|
+
// 0x00, 0x00, 0x00, // flags
|
|
826
|
+
//
|
|
827
|
+
// 0x03, // descriptor_type
|
|
828
|
+
// 0x17+configlen, // length
|
|
829
|
+
// 0x00, 0x01, //es_id
|
|
830
|
+
// 0x00, // stream_priority
|
|
831
|
+
//
|
|
832
|
+
// 0x04, // descriptor_type
|
|
833
|
+
// 0x0f+configlen, // length
|
|
834
|
+
// 0x40, //codec : mpeg4_audio
|
|
835
|
+
// 0x15, // stream_type
|
|
836
|
+
// 0x00, 0x00, 0x00, // buffer_size
|
|
837
|
+
// 0x00, 0x00, 0x00, 0x00, // maxBitrate
|
|
838
|
+
// 0x00, 0x00, 0x00, 0x00, // avgBitrate
|
|
839
|
+
//
|
|
840
|
+
// 0x05 // descriptor_type
|
|
841
|
+
// ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor
|
|
842
|
+
return data;
|
|
843
|
+
}
|
|
844
|
+
}, {
|
|
845
|
+
key: "mp4a",
|
|
846
|
+
value: function mp4a(track) {
|
|
847
|
+
var audiosamplerate = track.audiosamplerate;
|
|
848
|
+
return MP4.box(MP4.types.mp4a, new Uint8Array([0x00, 0x00, 0x00,
|
|
849
|
+
// reserved
|
|
850
|
+
0x00, 0x00, 0x00,
|
|
851
|
+
// reserved
|
|
852
|
+
0x00, 0x01,
|
|
853
|
+
// data_reference_index
|
|
854
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
855
|
+
// reserved
|
|
856
|
+
0x00, track.channelCount,
|
|
857
|
+
// channelcount
|
|
858
|
+
0x00, 0x10,
|
|
859
|
+
// sampleSize:16bits
|
|
860
|
+
0x00, 0x00,
|
|
861
|
+
// pre_defined
|
|
862
|
+
0x00, 0x00,
|
|
863
|
+
// reserved2
|
|
864
|
+
audiosamplerate >> 8 & 0xFF, audiosamplerate & 0xff,
|
|
865
|
+
//
|
|
866
|
+
0x00, 0x00]), MP4.box(MP4.types.esds, MP4.esds(track)));
|
|
867
|
+
}
|
|
868
|
+
}, {
|
|
869
|
+
key: "stsd",
|
|
870
|
+
value: function stsd(track) {
|
|
871
|
+
if (track.type === 'audio') {
|
|
872
|
+
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
|
|
873
|
+
} else {
|
|
874
|
+
if (track.codec.startsWith('hvc1')) {
|
|
875
|
+
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.hev1(track));
|
|
876
|
+
}
|
|
877
|
+
return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}, {
|
|
881
|
+
key: "tkhd",
|
|
882
|
+
value: function tkhd(track) {
|
|
883
|
+
var id = track.id,
|
|
884
|
+
duration = track.duration,
|
|
885
|
+
width = track.width,
|
|
886
|
+
height = track.height,
|
|
887
|
+
volume = track.volume;
|
|
888
|
+
return MP4.box(MP4.types.tkhd, new Uint8Array([0x00,
|
|
889
|
+
// version 0
|
|
890
|
+
0x00, 0x00, 0x07,
|
|
891
|
+
// flags
|
|
892
|
+
0x00, 0x00, 0x00, 0x00,
|
|
893
|
+
// creation_time
|
|
894
|
+
0x00, 0x00, 0x00, 0x00,
|
|
895
|
+
// modification_time
|
|
896
|
+
id >> 24 & 0xFF, id >> 16 & 0xFF, id >> 8 & 0xFF, id & 0xFF,
|
|
897
|
+
// track_ID
|
|
898
|
+
0x00, 0x00, 0x00, 0x00,
|
|
899
|
+
// reserved
|
|
900
|
+
duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF,
|
|
901
|
+
// duration
|
|
902
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
903
|
+
// reserved
|
|
904
|
+
0x00, 0x00,
|
|
905
|
+
// layer
|
|
906
|
+
0x00, 0x00,
|
|
907
|
+
// alternate_group
|
|
908
|
+
volume >> 0 & 0xff, volume % 1 * 10 >> 0 & 0xff,
|
|
909
|
+
// track volume // FIXME
|
|
910
|
+
0x00, 0x00,
|
|
911
|
+
// reserved
|
|
912
|
+
0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
|
|
913
|
+
// transformation: unity matrix
|
|
914
|
+
width >> 8 & 0xFF, width & 0xFF, 0x00, 0x00,
|
|
915
|
+
// width
|
|
916
|
+
height >> 8 & 0xFF, height & 0xFF, 0x00, 0x00 // height
|
|
917
|
+
]));
|
|
918
|
+
}
|
|
919
|
+
}, {
|
|
920
|
+
key: "traf",
|
|
921
|
+
value: function traf(track, baseMediaDecodeTime) {
|
|
922
|
+
var sampleDependencyTable = MP4.sdtp(track),
|
|
923
|
+
id = track.id;
|
|
924
|
+
return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([0x00,
|
|
925
|
+
// version 0
|
|
926
|
+
0x00, 0x00, 0x00,
|
|
927
|
+
// flags
|
|
928
|
+
id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF // track_ID
|
|
929
|
+
])), MP4.box(MP4.types.tfdt, new Uint8Array([0x00,
|
|
930
|
+
// version 0
|
|
931
|
+
0x00, 0x00, 0x00,
|
|
932
|
+
// flags
|
|
933
|
+
baseMediaDecodeTime >> 24, baseMediaDecodeTime >> 16 & 0XFF, baseMediaDecodeTime >> 8 & 0XFF, baseMediaDecodeTime & 0xFF // baseMediaDecodeTime
|
|
934
|
+
])), MP4.trun(track, sampleDependencyTable.length + 16 +
|
|
935
|
+
// tfhd
|
|
936
|
+
16 +
|
|
937
|
+
// tfdt
|
|
938
|
+
8 +
|
|
939
|
+
// traf header
|
|
940
|
+
16 +
|
|
941
|
+
// mfhd
|
|
942
|
+
8 +
|
|
943
|
+
// moof header
|
|
944
|
+
8),
|
|
945
|
+
// mdat header
|
|
946
|
+
sampleDependencyTable);
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Generate a track box.
|
|
951
|
+
* @param track {object} a track definition
|
|
952
|
+
* @return {Uint8Array} the track box
|
|
953
|
+
*/
|
|
954
|
+
}, {
|
|
955
|
+
key: "trak",
|
|
956
|
+
value: function trak(track) {
|
|
957
|
+
track.duration = track.duration || 0xffffffff;
|
|
958
|
+
return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
|
|
959
|
+
}
|
|
960
|
+
}, {
|
|
961
|
+
key: "trex",
|
|
962
|
+
value: function trex(track) {
|
|
963
|
+
var id = track.id;
|
|
964
|
+
return MP4.box(MP4.types.trex, new Uint8Array([0x00,
|
|
965
|
+
// version 0
|
|
966
|
+
0x00, 0x00, 0x00,
|
|
967
|
+
// flags
|
|
968
|
+
id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF,
|
|
969
|
+
// track_ID
|
|
970
|
+
0x00, 0x00, 0x00, 0x01,
|
|
971
|
+
// default_sample_description_index
|
|
972
|
+
0x00, 0x00, 0x00, 0x00,
|
|
973
|
+
// default_sample_duration
|
|
974
|
+
0x00, 0x00, 0x00, 0x00,
|
|
975
|
+
// default_sample_size
|
|
976
|
+
0x00, 0x01, 0x00, 0x01 // default_sample_flags
|
|
977
|
+
]));
|
|
978
|
+
}
|
|
979
|
+
}, {
|
|
980
|
+
key: "trun",
|
|
981
|
+
value: function trun(track, offset) {
|
|
982
|
+
var samples = track.samples || [],
|
|
983
|
+
len = samples.length,
|
|
984
|
+
arraylen = 12 + 16 * len,
|
|
985
|
+
array = new Uint8Array(arraylen),
|
|
986
|
+
i,
|
|
987
|
+
sample,
|
|
988
|
+
duration,
|
|
989
|
+
size,
|
|
990
|
+
flags,
|
|
991
|
+
cts;
|
|
992
|
+
offset += 8 + arraylen;
|
|
993
|
+
array.set([0x00,
|
|
994
|
+
// version 0
|
|
995
|
+
0x00, 0x0f, 0x01,
|
|
996
|
+
// flags
|
|
997
|
+
len >>> 24 & 0xFF, len >>> 16 & 0xFF, len >>> 8 & 0xFF, len & 0xFF,
|
|
998
|
+
// sample_count
|
|
999
|
+
offset >>> 24 & 0xFF, offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF // data_offset
|
|
1000
|
+
], 0);
|
|
1001
|
+
for (i = 0; i < len; i++) {
|
|
1002
|
+
sample = samples[i];
|
|
1003
|
+
duration = sample.duration;
|
|
1004
|
+
size = sample.size;
|
|
1005
|
+
flags = sample.flags;
|
|
1006
|
+
cts = sample.cts;
|
|
1007
|
+
array.set([duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF,
|
|
1008
|
+
// sample_duration
|
|
1009
|
+
size >>> 24 & 0xFF, size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF,
|
|
1010
|
+
// sample_size
|
|
1011
|
+
flags.isLeading << 2 | flags.dependsOn, flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.paddingValue << 1 | flags.isNonSync, flags.degradPrio & 0xF0 << 8, flags.degradPrio & 0x0F,
|
|
1012
|
+
// sample_flags
|
|
1013
|
+
cts >>> 24 & 0xFF, cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF // sample_composition_time_offset
|
|
1014
|
+
], 12 + 16 * i);
|
|
1015
|
+
}
|
|
1016
|
+
return MP4.box(MP4.types.trun, array);
|
|
1017
|
+
}
|
|
1018
|
+
}, {
|
|
1019
|
+
key: "initSegment",
|
|
1020
|
+
value: function initSegment(tracks, duration, timescale) {
|
|
1021
|
+
if (!MP4.types) {
|
|
1022
|
+
MP4.init();
|
|
1023
|
+
}
|
|
1024
|
+
var movie = MP4.moov(tracks, duration, timescale),
|
|
1025
|
+
result;
|
|
1026
|
+
result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
|
|
1027
|
+
result.set(MP4.FTYP);
|
|
1028
|
+
result.set(movie, MP4.FTYP.byteLength);
|
|
1029
|
+
return result;
|
|
1030
|
+
}
|
|
1031
|
+
}]);
|
|
1032
|
+
return MP4;
|
|
1033
|
+
}();
|
|
1034
|
+
|
|
1035
|
+
var AACParser = /*#__PURE__*/function () {
|
|
1036
|
+
function AACParser() {
|
|
1037
|
+
_classCallCheck(this, AACParser);
|
|
1038
|
+
}
|
|
1039
|
+
_createClass(AACParser, null, [{
|
|
1040
|
+
key: "samplingRateMap",
|
|
1041
|
+
get: function get() {
|
|
1042
|
+
return [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
|
|
1043
|
+
}
|
|
1044
|
+
}, {
|
|
1045
|
+
key: "getHeaderLength",
|
|
1046
|
+
value: function getHeaderLength(data) {
|
|
1047
|
+
return data[1] & 0x01 ? 7 : 9; // without CRC 7 and with CRC 9 Refs: https://wiki.multimedia.cx/index.php?title=ADTS
|
|
1048
|
+
}
|
|
1049
|
+
}, {
|
|
1050
|
+
key: "getFrameLength",
|
|
1051
|
+
value: function getFrameLength(data) {
|
|
1052
|
+
return (data[3] & 0x03) << 11 | data[4] << 3 | (data[5] & 0xE0) >>> 5; // 13 bits length ref: https://wiki.multimedia.cx/index.php?title=ADTS
|
|
1053
|
+
}
|
|
1054
|
+
}, {
|
|
1055
|
+
key: "isAACPattern",
|
|
1056
|
+
value: function isAACPattern(data) {
|
|
1057
|
+
return data[0] === 0xff && (data[1] & 0xf0) === 0xf0 && (data[1] & 0x06) === 0x00;
|
|
1058
|
+
}
|
|
1059
|
+
}, {
|
|
1060
|
+
key: "extractAAC",
|
|
1061
|
+
value: function extractAAC(buffer) {
|
|
1062
|
+
var i = 0,
|
|
1063
|
+
length = buffer.byteLength,
|
|
1064
|
+
slices = [],
|
|
1065
|
+
headerLength,
|
|
1066
|
+
frameLength;
|
|
1067
|
+
if (!AACParser.isAACPattern(buffer)) {
|
|
1068
|
+
error('Invalid ADTS audio format');
|
|
1069
|
+
return {
|
|
1070
|
+
valid: false
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
headerLength = AACParser.getHeaderLength(buffer);
|
|
1074
|
+
var header = buffer.subarray(0, headerLength);
|
|
1075
|
+
while (i < length) {
|
|
1076
|
+
frameLength = AACParser.getFrameLength(buffer);
|
|
1077
|
+
slices.push(buffer.subarray(headerLength, frameLength));
|
|
1078
|
+
buffer = buffer.slice(frameLength);
|
|
1079
|
+
i += frameLength;
|
|
1080
|
+
}
|
|
1081
|
+
return {
|
|
1082
|
+
valid: true,
|
|
1083
|
+
header: header,
|
|
1084
|
+
slices: slices
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
}]);
|
|
1088
|
+
return AACParser;
|
|
1089
|
+
}();
|
|
1090
|
+
|
|
1091
|
+
var track_id = 1;
|
|
1092
|
+
var BaseRemuxer = /*#__PURE__*/function (_Event) {
|
|
1093
|
+
_inherits(BaseRemuxer, _Event);
|
|
1094
|
+
var _super = _createSuper(BaseRemuxer);
|
|
1095
|
+
function BaseRemuxer() {
|
|
1096
|
+
_classCallCheck(this, BaseRemuxer);
|
|
1097
|
+
return _super.apply(this, arguments);
|
|
1098
|
+
}
|
|
1099
|
+
_createClass(BaseRemuxer, [{
|
|
1100
|
+
key: "flush",
|
|
1101
|
+
value: function flush() {
|
|
1102
|
+
this.mp4track.len = 0;
|
|
1103
|
+
this.mp4track.samples = [];
|
|
1104
|
+
}
|
|
1105
|
+
}, {
|
|
1106
|
+
key: "isReady",
|
|
1107
|
+
value: function isReady() {
|
|
1108
|
+
if (!this.readyToDecode || !this.samples.length) return null;
|
|
1109
|
+
return true;
|
|
1110
|
+
}
|
|
1111
|
+
}], [{
|
|
1112
|
+
key: "getTrackID",
|
|
1113
|
+
value: function getTrackID() {
|
|
1114
|
+
return track_id++;
|
|
1115
|
+
}
|
|
1116
|
+
}]);
|
|
1117
|
+
return BaseRemuxer;
|
|
1118
|
+
}(Event);
|
|
1119
|
+
|
|
1120
|
+
var AACRemuxer = /*#__PURE__*/function (_BaseRemuxer) {
|
|
1121
|
+
_inherits(AACRemuxer, _BaseRemuxer);
|
|
1122
|
+
var _super = _createSuper(AACRemuxer);
|
|
1123
|
+
function AACRemuxer(timescale, duration, frameDuration) {
|
|
1124
|
+
var _this;
|
|
1125
|
+
_classCallCheck(this, AACRemuxer);
|
|
1126
|
+
_this = _super.call(this, 'AACRemuxer');
|
|
1127
|
+
_this.frameDuration = frameDuration;
|
|
1128
|
+
_this.readyToDecode = false;
|
|
1129
|
+
_this.header = null;
|
|
1130
|
+
_this.nextDts = 0;
|
|
1131
|
+
_this.dts = 0;
|
|
1132
|
+
_this.mp4track = {
|
|
1133
|
+
id: BaseRemuxer.getTrackID(),
|
|
1134
|
+
type: 'audio',
|
|
1135
|
+
channelCount: 0,
|
|
1136
|
+
len: 0,
|
|
1137
|
+
fragmented: true,
|
|
1138
|
+
timescale: timescale,
|
|
1139
|
+
duration: duration,
|
|
1140
|
+
samples: [],
|
|
1141
|
+
config: '',
|
|
1142
|
+
codec: ''
|
|
1143
|
+
};
|
|
1144
|
+
_this.samples = [];
|
|
1145
|
+
return _this;
|
|
1146
|
+
}
|
|
1147
|
+
_createClass(AACRemuxer, [{
|
|
1148
|
+
key: "resetTrack",
|
|
1149
|
+
value: function resetTrack() {
|
|
1150
|
+
this.readyToDecode = false;
|
|
1151
|
+
this.header = null;
|
|
1152
|
+
this.mp4track.codec = '';
|
|
1153
|
+
this.mp4track.channelCount = '';
|
|
1154
|
+
this.mp4track.config = '';
|
|
1155
|
+
this.mp4track.timescale = this.timescale;
|
|
1156
|
+
this.nextDts = 0;
|
|
1157
|
+
this.dts = 0;
|
|
1158
|
+
}
|
|
1159
|
+
}, {
|
|
1160
|
+
key: "feed",
|
|
1161
|
+
value: function feed(data, duration) {
|
|
1162
|
+
var _AACParser$extractAAC = AACParser.extractAAC(data),
|
|
1163
|
+
valid = _AACParser$extractAAC.valid,
|
|
1164
|
+
header = _AACParser$extractAAC.header,
|
|
1165
|
+
slices = _AACParser$extractAAC.slices;
|
|
1166
|
+
if (!this.header) this.header = header;
|
|
1167
|
+
if (valid && slices.length > 0) {
|
|
1168
|
+
this.remux(this.getAudioFrames(slices, duration));
|
|
1169
|
+
return true;
|
|
1170
|
+
} else {
|
|
1171
|
+
error('Failed to extract audio data from:', data);
|
|
1172
|
+
this.dispatch('outOfData');
|
|
1173
|
+
return false;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
}, {
|
|
1177
|
+
key: "getAudioFrames",
|
|
1178
|
+
value: function getAudioFrames(aacFrames, duration) {
|
|
1179
|
+
var frames = [],
|
|
1180
|
+
fd = 0,
|
|
1181
|
+
tt = 0;
|
|
1182
|
+
var _iterator = _createForOfIteratorHelper(aacFrames),
|
|
1183
|
+
_step;
|
|
1184
|
+
try {
|
|
1185
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1186
|
+
var units = _step.value;
|
|
1187
|
+
frames.push({
|
|
1188
|
+
units: units
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
} catch (err) {
|
|
1192
|
+
_iterator.e(err);
|
|
1193
|
+
} finally {
|
|
1194
|
+
_iterator.f();
|
|
1195
|
+
}
|
|
1196
|
+
fd = duration ? duration / frames.length | 0 : this.frameDuration;
|
|
1197
|
+
tt = duration ? duration - fd * frames.length : 0;
|
|
1198
|
+
frames.map(function (frame) {
|
|
1199
|
+
frame.duration = fd;
|
|
1200
|
+
if (tt > 0) {
|
|
1201
|
+
frame.duration++;
|
|
1202
|
+
tt--;
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
return frames;
|
|
1206
|
+
}
|
|
1207
|
+
}, {
|
|
1208
|
+
key: "remux",
|
|
1209
|
+
value: function remux(frames) {
|
|
1210
|
+
if (frames.length > 0) {
|
|
1211
|
+
for (var i = 0; i < frames.length; i++) {
|
|
1212
|
+
var frame = frames[i];
|
|
1213
|
+
var payload = frame.units;
|
|
1214
|
+
var size = payload.byteLength;
|
|
1215
|
+
this.samples.push({
|
|
1216
|
+
units: payload,
|
|
1217
|
+
size: size,
|
|
1218
|
+
duration: frame.duration
|
|
1219
|
+
});
|
|
1220
|
+
this.mp4track.len += size;
|
|
1221
|
+
if (!this.readyToDecode) {
|
|
1222
|
+
this.setAACConfig();
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
}, {
|
|
1228
|
+
key: "getPayload",
|
|
1229
|
+
value: function getPayload() {
|
|
1230
|
+
if (!this.isReady()) {
|
|
1231
|
+
return null;
|
|
1232
|
+
}
|
|
1233
|
+
var payload = new Uint8Array(this.mp4track.len);
|
|
1234
|
+
var offset = 0;
|
|
1235
|
+
var samples = this.mp4track.samples;
|
|
1236
|
+
var mp4Sample, duration;
|
|
1237
|
+
this.dts = this.nextDts;
|
|
1238
|
+
while (this.samples.length) {
|
|
1239
|
+
var sample = this.samples.shift();
|
|
1240
|
+
sample.units;
|
|
1241
|
+
duration = sample.duration;
|
|
1242
|
+
if (duration <= 0) {
|
|
1243
|
+
log("remuxer: invalid sample duration at DTS: ".concat(this.nextDts, " :").concat(duration));
|
|
1244
|
+
this.mp4track.len -= sample.size;
|
|
1245
|
+
continue;
|
|
1246
|
+
}
|
|
1247
|
+
this.nextDts += duration;
|
|
1248
|
+
mp4Sample = {
|
|
1249
|
+
size: sample.size,
|
|
1250
|
+
duration: duration,
|
|
1251
|
+
cts: 0,
|
|
1252
|
+
flags: {
|
|
1253
|
+
isLeading: 0,
|
|
1254
|
+
isDependedOn: 0,
|
|
1255
|
+
hasRedundancy: 0,
|
|
1256
|
+
degradPrio: 0,
|
|
1257
|
+
dependsOn: 1
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
payload.set(sample.units, offset);
|
|
1261
|
+
offset += sample.size;
|
|
1262
|
+
samples.push(mp4Sample);
|
|
1263
|
+
}
|
|
1264
|
+
if (!samples.length) return null;
|
|
1265
|
+
return new Uint8Array(payload.buffer, 0, this.mp4track.len);
|
|
1266
|
+
}
|
|
1267
|
+
}, {
|
|
1268
|
+
key: "setAACConfig",
|
|
1269
|
+
value: function setAACConfig() {
|
|
1270
|
+
var objectType,
|
|
1271
|
+
sampleIndex,
|
|
1272
|
+
channelCount,
|
|
1273
|
+
config = new Uint8Array(2);
|
|
1274
|
+
if (!this.header) return;
|
|
1275
|
+
objectType = ((this.header[2] & 0xC0) >>> 6) + 1;
|
|
1276
|
+
sampleIndex = (this.header[2] & 0x3C) >>> 2;
|
|
1277
|
+
channelCount = (this.header[2] & 0x01) << 2;
|
|
1278
|
+
channelCount |= (this.header[3] & 0xC0) >>> 6;
|
|
1279
|
+
|
|
1280
|
+
/* refer to http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Audio_Specific_Config */
|
|
1281
|
+
config[0] = objectType << 3;
|
|
1282
|
+
config[0] |= (sampleIndex & 0x0E) >> 1;
|
|
1283
|
+
config[1] |= (sampleIndex & 0x01) << 7;
|
|
1284
|
+
config[1] |= channelCount << 3;
|
|
1285
|
+
this.mp4track.codec = 'mp4a.40.' + objectType;
|
|
1286
|
+
this.mp4track.channelCount = channelCount;
|
|
1287
|
+
this.mp4track.config = config;
|
|
1288
|
+
this.readyToDecode = true;
|
|
1289
|
+
}
|
|
1290
|
+
}]);
|
|
1291
|
+
return AACRemuxer;
|
|
1292
|
+
}(BaseRemuxer);
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
|
|
1296
|
+
*/
|
|
1297
|
+
|
|
1298
|
+
var ExpGolomb = /*#__PURE__*/function () {
|
|
1299
|
+
function ExpGolomb(data) {
|
|
1300
|
+
_classCallCheck(this, ExpGolomb);
|
|
1301
|
+
this.data = data;
|
|
1302
|
+
this.index = 0;
|
|
1303
|
+
this.bitLength = data.byteLength * 8;
|
|
1304
|
+
}
|
|
1305
|
+
_createClass(ExpGolomb, [{
|
|
1306
|
+
key: "setData",
|
|
1307
|
+
value: function setData(data) {
|
|
1308
|
+
this.data = data;
|
|
1309
|
+
this.index = 0;
|
|
1310
|
+
this.bitLength = data.byteLength * 8;
|
|
1311
|
+
}
|
|
1312
|
+
}, {
|
|
1313
|
+
key: "bitsAvailable",
|
|
1314
|
+
get: function get() {
|
|
1315
|
+
return this.bitLength - this.index;
|
|
1316
|
+
}
|
|
1317
|
+
}, {
|
|
1318
|
+
key: "skipBits",
|
|
1319
|
+
value: function skipBits(size) {
|
|
1320
|
+
// console.log(` skip bits: size=${size}, ${this.index}.`);
|
|
1321
|
+
if (this.bitsAvailable < size) {
|
|
1322
|
+
//throw new Error('no bytes available');
|
|
1323
|
+
return false;
|
|
1324
|
+
}
|
|
1325
|
+
this.index += size;
|
|
1326
|
+
}
|
|
1327
|
+
}, {
|
|
1328
|
+
key: "readBits",
|
|
1329
|
+
value: function readBits(size) {
|
|
1330
|
+
var moveIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
1331
|
+
// console.log(` read bits: size=${size}, ${this.index}.`);
|
|
1332
|
+
var result = this.getBits(size, this.index, moveIndex);
|
|
1333
|
+
// console.log(` read bits: result=${result}`);
|
|
1334
|
+
return result;
|
|
1335
|
+
}
|
|
1336
|
+
}, {
|
|
1337
|
+
key: "getBits",
|
|
1338
|
+
value: function getBits(size, offsetBits) {
|
|
1339
|
+
var moveIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
|
1340
|
+
if (this.bitsAvailable < size) {
|
|
1341
|
+
//throw new Error('no bytes available');
|
|
1342
|
+
return 0;
|
|
1343
|
+
}
|
|
1344
|
+
var offset = offsetBits % 8;
|
|
1345
|
+
var _byte = this.data[offsetBits / 8 | 0] & 0xff >>> offset;
|
|
1346
|
+
var bits = 8 - offset;
|
|
1347
|
+
if (bits >= size) {
|
|
1348
|
+
if (moveIndex) {
|
|
1349
|
+
this.index += size;
|
|
1350
|
+
}
|
|
1351
|
+
return _byte >> bits - size;
|
|
1352
|
+
} else {
|
|
1353
|
+
if (moveIndex) {
|
|
1354
|
+
this.index += bits;
|
|
1355
|
+
}
|
|
1356
|
+
var nextSize = size - bits;
|
|
1357
|
+
return _byte << nextSize | this.getBits(nextSize, offsetBits + bits, moveIndex);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}, {
|
|
1361
|
+
key: "skipLZ",
|
|
1362
|
+
value: function skipLZ() {
|
|
1363
|
+
var leadingZeroCount;
|
|
1364
|
+
for (leadingZeroCount = 0; leadingZeroCount < this.bitLength - this.index; ++leadingZeroCount) {
|
|
1365
|
+
if (this.getBits(1, this.index + leadingZeroCount, false) !== 0) {
|
|
1366
|
+
// console.log(` skip LZ : size=${leadingZeroCount}, ${this.index}.`);
|
|
1367
|
+
this.index += leadingZeroCount;
|
|
1368
|
+
return leadingZeroCount;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
return leadingZeroCount;
|
|
1372
|
+
}
|
|
1373
|
+
}, {
|
|
1374
|
+
key: "skipUEG",
|
|
1375
|
+
value: function skipUEG() {
|
|
1376
|
+
this.skipBits(1 + this.skipLZ());
|
|
1377
|
+
}
|
|
1378
|
+
}, {
|
|
1379
|
+
key: "skipEG",
|
|
1380
|
+
value: function skipEG() {
|
|
1381
|
+
this.skipBits(1 + this.skipLZ());
|
|
1382
|
+
}
|
|
1383
|
+
}, {
|
|
1384
|
+
key: "readUEG",
|
|
1385
|
+
value: function readUEG() {
|
|
1386
|
+
var prefix = this.skipLZ();
|
|
1387
|
+
return this.readBits(prefix + 1) - 1;
|
|
1388
|
+
}
|
|
1389
|
+
}, {
|
|
1390
|
+
key: "readEG",
|
|
1391
|
+
value: function readEG() {
|
|
1392
|
+
var value = this.readUEG();
|
|
1393
|
+
if (0x01 & value) {
|
|
1394
|
+
// the number is odd if the low order bit is set
|
|
1395
|
+
return 1 + value >>> 1; // add 1 to make it even, and divide by 2
|
|
1396
|
+
} else {
|
|
1397
|
+
return -1 * (value >>> 1); // divide by two then make it negative
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
}, {
|
|
1401
|
+
key: "readBoolean",
|
|
1402
|
+
value: function readBoolean() {
|
|
1403
|
+
return this.readBits(1) === 1;
|
|
1404
|
+
}
|
|
1405
|
+
}, {
|
|
1406
|
+
key: "readUByte",
|
|
1407
|
+
value: function readUByte() {
|
|
1408
|
+
var numberOfBytes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
|
|
1409
|
+
return this.readBits(numberOfBytes * 8);
|
|
1410
|
+
}
|
|
1411
|
+
}, {
|
|
1412
|
+
key: "readUShort",
|
|
1413
|
+
value: function readUShort() {
|
|
1414
|
+
return this.readBits(16);
|
|
1415
|
+
}
|
|
1416
|
+
}, {
|
|
1417
|
+
key: "readUInt",
|
|
1418
|
+
value: function readUInt() {
|
|
1419
|
+
return this.readBits(32);
|
|
1420
|
+
}
|
|
1421
|
+
}]);
|
|
1422
|
+
return ExpGolomb;
|
|
1423
|
+
}();
|
|
1424
|
+
|
|
1425
|
+
// spec https://www.itu.int/rec/T-REC-H.264/
|
|
1426
|
+
|
|
1427
|
+
var H264Parser = /*#__PURE__*/function () {
|
|
1428
|
+
function H264Parser() {
|
|
1429
|
+
_classCallCheck(this, H264Parser);
|
|
1430
|
+
}
|
|
1431
|
+
_createClass(H264Parser, null, [{
|
|
1432
|
+
key: "extractNALu",
|
|
1433
|
+
value: function extractNALu(buffer) {
|
|
1434
|
+
var i = 0,
|
|
1435
|
+
length = buffer.byteLength,
|
|
1436
|
+
result = [],
|
|
1437
|
+
lastIndex = 0,
|
|
1438
|
+
zeroCount = 0;
|
|
1439
|
+
while (i < length) {
|
|
1440
|
+
var value = buffer[i++];
|
|
1441
|
+
if (value === 0) {
|
|
1442
|
+
zeroCount++;
|
|
1443
|
+
} else if (value === 1 && zeroCount >= 2) {
|
|
1444
|
+
var startCodeLength = zeroCount + 1;
|
|
1445
|
+
if (lastIndex !== i - startCodeLength) {
|
|
1446
|
+
result.push(buffer.subarray(lastIndex, i - startCodeLength));
|
|
1447
|
+
}
|
|
1448
|
+
lastIndex = i;
|
|
1449
|
+
zeroCount = 0;
|
|
1450
|
+
} else {
|
|
1451
|
+
zeroCount = 0;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
// Remaining data after last start code
|
|
1456
|
+
var left = null;
|
|
1457
|
+
if (lastIndex < length) {
|
|
1458
|
+
left = buffer.subarray(lastIndex, length);
|
|
1459
|
+
}
|
|
1460
|
+
return [result, left];
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
/**
|
|
1464
|
+
* Advance the ExpGolomb decoder past a scaling list. The scaling
|
|
1465
|
+
* list is optionally transmitted as part of a sequence parameter
|
|
1466
|
+
* set and is not relevant to transmuxing.
|
|
1467
|
+
* @param decoder {ExpGolomb} exp golomb decoder
|
|
1468
|
+
* @param count {number} the number of entries in this scaling list
|
|
1469
|
+
* @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
|
|
1470
|
+
*/
|
|
1471
|
+
}, {
|
|
1472
|
+
key: "skipScalingList",
|
|
1473
|
+
value: function skipScalingList(decoder, count) {
|
|
1474
|
+
var lastScale = 8,
|
|
1475
|
+
nextScale = 8,
|
|
1476
|
+
deltaScale;
|
|
1477
|
+
for (var j = 0; j < count; j++) {
|
|
1478
|
+
if (nextScale !== 0) {
|
|
1479
|
+
deltaScale = decoder.readEG();
|
|
1480
|
+
nextScale = (lastScale + deltaScale + 256) % 256;
|
|
1481
|
+
}
|
|
1482
|
+
lastScale = nextScale === 0 ? lastScale : nextScale;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
|
|
1486
|
+
/**
|
|
1487
|
+
* Read a sequence parameter set and return some interesting video
|
|
1488
|
+
* properties. A sequence parameter set is the H264 metadata that
|
|
1489
|
+
* describes the properties of upcoming video frames.
|
|
1490
|
+
* @param data {Uint8Array} the bytes of a sequence parameter set
|
|
1491
|
+
* @return {object} an object with configuration parsed from the
|
|
1492
|
+
* sequence parameter set, including the dimensions of the
|
|
1493
|
+
* associated video frames.
|
|
1494
|
+
*/
|
|
1495
|
+
}, {
|
|
1496
|
+
key: "readSPS",
|
|
1497
|
+
value: function readSPS(data) {
|
|
1498
|
+
var decoder = new ExpGolomb(data);
|
|
1499
|
+
var frameCropLeftOffset = 0,
|
|
1500
|
+
frameCropRightOffset = 0,
|
|
1501
|
+
frameCropTopOffset = 0,
|
|
1502
|
+
frameCropBottomOffset = 0,
|
|
1503
|
+
sarScale = 1,
|
|
1504
|
+
profileIdc,
|
|
1505
|
+
numRefFramesInPicOrderCntCycle,
|
|
1506
|
+
picWidthInMbsMinus1,
|
|
1507
|
+
picHeightInMapUnitsMinus1,
|
|
1508
|
+
frameMbsOnlyFlag,
|
|
1509
|
+
scalingListCount,
|
|
1510
|
+
fps = 0;
|
|
1511
|
+
decoder.readUByte(); // skip NAL header
|
|
1512
|
+
|
|
1513
|
+
// rewrite NAL
|
|
1514
|
+
var rbsp = [],
|
|
1515
|
+
hdr_bytes = 1,
|
|
1516
|
+
nal_bytes = data.byteLength;
|
|
1517
|
+
for (var i = hdr_bytes; i < nal_bytes; i++) {
|
|
1518
|
+
if (i + 2 < nal_bytes && decoder.readBits(24, false) === 0x000003) {
|
|
1519
|
+
rbsp.push(decoder.readBits(8));
|
|
1520
|
+
rbsp.push(decoder.readBits(8));
|
|
1521
|
+
i += 2;
|
|
1522
|
+
|
|
1523
|
+
// emulation_prevention_three_byte
|
|
1524
|
+
decoder.readBits(8);
|
|
1525
|
+
} else {
|
|
1526
|
+
rbsp.push(decoder.readBits(8));
|
|
1527
|
+
}
|
|
1528
|
+
}
|
|
1529
|
+
decoder.setData(new Uint8Array(rbsp));
|
|
1530
|
+
// end of rewrite data
|
|
1531
|
+
|
|
1532
|
+
profileIdc = decoder.readUByte(); // profile_idc
|
|
1533
|
+
decoder.readBits(5); // constraint_set[0-4]_flag, u(5)
|
|
1534
|
+
decoder.skipBits(3); // reserved_zero_3bits u(3),
|
|
1535
|
+
decoder.readUByte(); // level_idc u(8)
|
|
1536
|
+
decoder.skipUEG(); // seq_parameter_set_id
|
|
1537
|
+
// some profiles have more optional data we don't need
|
|
1538
|
+
if (profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || profileIdc === 44 || profileIdc === 83 || profileIdc === 86 || profileIdc === 118 || profileIdc === 128) {
|
|
1539
|
+
var chromaFormatIdc = decoder.readUEG();
|
|
1540
|
+
if (chromaFormatIdc === 3) {
|
|
1541
|
+
decoder.skipBits(1); // separate_colour_plane_flag
|
|
1542
|
+
}
|
|
1543
|
+
decoder.skipUEG(); // bit_depth_luma_minus8
|
|
1544
|
+
decoder.skipUEG(); // bit_depth_chroma_minus8
|
|
1545
|
+
decoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
|
|
1546
|
+
if (decoder.readBoolean()) {
|
|
1547
|
+
// seq_scaling_matrix_present_flag
|
|
1548
|
+
scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
|
|
1549
|
+
for (var _i = 0; _i < scalingListCount; ++_i) {
|
|
1550
|
+
if (decoder.readBoolean()) {
|
|
1551
|
+
// seq_scaling_list_present_flag[ i ]
|
|
1552
|
+
if (_i < 6) {
|
|
1553
|
+
H264Parser.skipScalingList(decoder, 16);
|
|
1554
|
+
} else {
|
|
1555
|
+
H264Parser.skipScalingList(decoder, 64);
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
decoder.skipUEG(); // log2_max_frame_num_minus4
|
|
1562
|
+
var picOrderCntType = decoder.readUEG();
|
|
1563
|
+
if (picOrderCntType === 0) {
|
|
1564
|
+
decoder.readUEG(); // log2_max_pic_order_cnt_lsb_minus4
|
|
1565
|
+
} else if (picOrderCntType === 1) {
|
|
1566
|
+
decoder.skipBits(1); // delta_pic_order_always_zero_flag
|
|
1567
|
+
decoder.skipEG(); // offset_for_non_ref_pic
|
|
1568
|
+
decoder.skipEG(); // offset_for_top_to_bottom_field
|
|
1569
|
+
numRefFramesInPicOrderCntCycle = decoder.readUEG();
|
|
1570
|
+
for (var _i2 = 0; _i2 < numRefFramesInPicOrderCntCycle; ++_i2) {
|
|
1571
|
+
decoder.skipEG(); // offset_for_ref_frame[ i ]
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
decoder.skipUEG(); // max_num_ref_frames
|
|
1575
|
+
decoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
|
|
1576
|
+
picWidthInMbsMinus1 = decoder.readUEG();
|
|
1577
|
+
picHeightInMapUnitsMinus1 = decoder.readUEG();
|
|
1578
|
+
frameMbsOnlyFlag = decoder.readBits(1);
|
|
1579
|
+
if (frameMbsOnlyFlag === 0) {
|
|
1580
|
+
decoder.skipBits(1); // mb_adaptive_frame_field_flag
|
|
1581
|
+
}
|
|
1582
|
+
decoder.skipBits(1); // direct_8x8_inference_flag
|
|
1583
|
+
if (decoder.readBoolean()) {
|
|
1584
|
+
// frame_cropping_flag
|
|
1585
|
+
frameCropLeftOffset = decoder.readUEG();
|
|
1586
|
+
frameCropRightOffset = decoder.readUEG();
|
|
1587
|
+
frameCropTopOffset = decoder.readUEG();
|
|
1588
|
+
frameCropBottomOffset = decoder.readUEG();
|
|
1589
|
+
}
|
|
1590
|
+
if (decoder.readBoolean()) {
|
|
1591
|
+
// vui_parameters_present_flag
|
|
1592
|
+
if (decoder.readBoolean()) {
|
|
1593
|
+
// aspect_ratio_info_present_flag
|
|
1594
|
+
var sarRatio;
|
|
1595
|
+
var aspectRatioIdc = decoder.readUByte();
|
|
1596
|
+
switch (aspectRatioIdc) {
|
|
1597
|
+
case 1:
|
|
1598
|
+
sarRatio = [1, 1];
|
|
1599
|
+
break;
|
|
1600
|
+
case 2:
|
|
1601
|
+
sarRatio = [12, 11];
|
|
1602
|
+
break;
|
|
1603
|
+
case 3:
|
|
1604
|
+
sarRatio = [10, 11];
|
|
1605
|
+
break;
|
|
1606
|
+
case 4:
|
|
1607
|
+
sarRatio = [16, 11];
|
|
1608
|
+
break;
|
|
1609
|
+
case 5:
|
|
1610
|
+
sarRatio = [40, 33];
|
|
1611
|
+
break;
|
|
1612
|
+
case 6:
|
|
1613
|
+
sarRatio = [24, 11];
|
|
1614
|
+
break;
|
|
1615
|
+
case 7:
|
|
1616
|
+
sarRatio = [20, 11];
|
|
1617
|
+
break;
|
|
1618
|
+
case 8:
|
|
1619
|
+
sarRatio = [32, 11];
|
|
1620
|
+
break;
|
|
1621
|
+
case 9:
|
|
1622
|
+
sarRatio = [80, 33];
|
|
1623
|
+
break;
|
|
1624
|
+
case 10:
|
|
1625
|
+
sarRatio = [18, 11];
|
|
1626
|
+
break;
|
|
1627
|
+
case 11:
|
|
1628
|
+
sarRatio = [15, 11];
|
|
1629
|
+
break;
|
|
1630
|
+
case 12:
|
|
1631
|
+
sarRatio = [64, 33];
|
|
1632
|
+
break;
|
|
1633
|
+
case 13:
|
|
1634
|
+
sarRatio = [160, 99];
|
|
1635
|
+
break;
|
|
1636
|
+
case 14:
|
|
1637
|
+
sarRatio = [4, 3];
|
|
1638
|
+
break;
|
|
1639
|
+
case 15:
|
|
1640
|
+
sarRatio = [3, 2];
|
|
1641
|
+
break;
|
|
1642
|
+
case 16:
|
|
1643
|
+
sarRatio = [2, 1];
|
|
1644
|
+
break;
|
|
1645
|
+
case 255:
|
|
1646
|
+
{
|
|
1647
|
+
sarRatio = [decoder.readUByte() << 8 | decoder.readUByte(), decoder.readUByte() << 8 | decoder.readUByte()];
|
|
1648
|
+
break;
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
if (sarRatio && sarRatio[0] > 0 && sarRatio[1] > 0) {
|
|
1652
|
+
sarScale = sarRatio[0] / sarRatio[1];
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
if (decoder.readBoolean()) {
|
|
1656
|
+
decoder.skipBits(1);
|
|
1657
|
+
}
|
|
1658
|
+
if (decoder.readBoolean()) {
|
|
1659
|
+
decoder.skipBits(4);
|
|
1660
|
+
if (decoder.readBoolean()) {
|
|
1661
|
+
decoder.skipBits(24);
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
if (decoder.readBoolean()) {
|
|
1665
|
+
decoder.skipUEG();
|
|
1666
|
+
decoder.skipUEG();
|
|
1667
|
+
}
|
|
1668
|
+
if (decoder.readBoolean()) {
|
|
1669
|
+
var unitsInTick = decoder.readUInt();
|
|
1670
|
+
var timeScale = decoder.readUInt();
|
|
1671
|
+
var fixedFrameRate = decoder.readBoolean();
|
|
1672
|
+
var frameDuration = timeScale / (2 * unitsInTick);
|
|
1673
|
+
if (fixedFrameRate) {
|
|
1674
|
+
fps = frameDuration;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
return {
|
|
1679
|
+
fps: fps > 0 ? fps : undefined,
|
|
1680
|
+
width: Math.ceil(((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
|
|
1681
|
+
height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - (frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)
|
|
1682
|
+
};
|
|
1683
|
+
}
|
|
1684
|
+
}]);
|
|
1685
|
+
return H264Parser;
|
|
1686
|
+
}();
|
|
1687
|
+
var NALU264 = /*#__PURE__*/function () {
|
|
1688
|
+
function NALU264(data) {
|
|
1689
|
+
_classCallCheck(this, NALU264);
|
|
1690
|
+
this.payload = data;
|
|
1691
|
+
this.nri = (this.payload[0] & 0x60) >> 5; // nal_ref_idc
|
|
1692
|
+
this.nalUnitType = this.payload[0] & 0x1f;
|
|
1693
|
+
this._sliceType = null;
|
|
1694
|
+
this._isFirstSlice = false;
|
|
1695
|
+
}
|
|
1696
|
+
_createClass(NALU264, [{
|
|
1697
|
+
key: "toString",
|
|
1698
|
+
value: function toString() {
|
|
1699
|
+
return "".concat(NALU264.TYPES[this.type()] || 'UNKNOWN', ": NRI: ").concat(this.getNri());
|
|
1700
|
+
}
|
|
1701
|
+
}, {
|
|
1702
|
+
key: "getNri",
|
|
1703
|
+
value: function getNri() {
|
|
1704
|
+
return this.nri;
|
|
1705
|
+
}
|
|
1706
|
+
}, {
|
|
1707
|
+
key: "type",
|
|
1708
|
+
value: function type() {
|
|
1709
|
+
return this.nalUnitType;
|
|
1710
|
+
}
|
|
1711
|
+
}, {
|
|
1712
|
+
key: "isKeyframe",
|
|
1713
|
+
get: function get() {
|
|
1714
|
+
return this.nalUnitType === NALU264.IDR;
|
|
1715
|
+
}
|
|
1716
|
+
}, {
|
|
1717
|
+
key: "isVCL",
|
|
1718
|
+
get: function get() {
|
|
1719
|
+
return this.nalUnitType == NALU264.IDR || this.nalUnitType == NALU264.NDR;
|
|
1720
|
+
}
|
|
1721
|
+
}, {
|
|
1722
|
+
key: "parseHeader",
|
|
1723
|
+
value: function parseHeader() {
|
|
1724
|
+
var decoder = new ExpGolomb(this.getPayload());
|
|
1725
|
+
// skip NALu type
|
|
1726
|
+
decoder.readUByte();
|
|
1727
|
+
this._isFirstSlice = decoder.readUEG() === 0;
|
|
1728
|
+
this._sliceType = decoder.readUEG();
|
|
1729
|
+
}
|
|
1730
|
+
}, {
|
|
1731
|
+
key: "isFirstSlice",
|
|
1732
|
+
get: function get() {
|
|
1733
|
+
if (!this._isFirstSlice) {
|
|
1734
|
+
this.parseHeader();
|
|
1735
|
+
}
|
|
1736
|
+
return this._isFirstSlice;
|
|
1737
|
+
}
|
|
1738
|
+
}, {
|
|
1739
|
+
key: "sliceType",
|
|
1740
|
+
get: function get() {
|
|
1741
|
+
if (!this._sliceType) {
|
|
1742
|
+
this.parseHeader();
|
|
1743
|
+
}
|
|
1744
|
+
return this._sliceType;
|
|
1745
|
+
}
|
|
1746
|
+
}, {
|
|
1747
|
+
key: "getPayload",
|
|
1748
|
+
value: function getPayload() {
|
|
1749
|
+
return this.payload;
|
|
1750
|
+
}
|
|
1751
|
+
}, {
|
|
1752
|
+
key: "getPayloadSize",
|
|
1753
|
+
value: function getPayloadSize() {
|
|
1754
|
+
return this.payload.byteLength;
|
|
1755
|
+
}
|
|
1756
|
+
}, {
|
|
1757
|
+
key: "getSize",
|
|
1758
|
+
value: function getSize() {
|
|
1759
|
+
return 4 + this.getPayloadSize();
|
|
1760
|
+
}
|
|
1761
|
+
}, {
|
|
1762
|
+
key: "getData",
|
|
1763
|
+
value: function getData() {
|
|
1764
|
+
var result = new Uint8Array(this.getSize());
|
|
1765
|
+
var view = new DataView(result.buffer);
|
|
1766
|
+
view.setUint32(0, this.getSize() - 4);
|
|
1767
|
+
result.set(this.getPayload(), 4);
|
|
1768
|
+
return result;
|
|
1769
|
+
}
|
|
1770
|
+
}], [{
|
|
1771
|
+
key: "NDR",
|
|
1772
|
+
get: function get() {
|
|
1773
|
+
return 1;
|
|
1774
|
+
}
|
|
1775
|
+
}, {
|
|
1776
|
+
key: "IDR",
|
|
1777
|
+
get: function get() {
|
|
1778
|
+
return 5;
|
|
1779
|
+
}
|
|
1780
|
+
}, {
|
|
1781
|
+
key: "SEI",
|
|
1782
|
+
get: function get() {
|
|
1783
|
+
return 6;
|
|
1784
|
+
}
|
|
1785
|
+
}, {
|
|
1786
|
+
key: "SPS",
|
|
1787
|
+
get: function get() {
|
|
1788
|
+
return 7;
|
|
1789
|
+
}
|
|
1790
|
+
}, {
|
|
1791
|
+
key: "PPS",
|
|
1792
|
+
get: function get() {
|
|
1793
|
+
return 8;
|
|
1794
|
+
}
|
|
1795
|
+
}, {
|
|
1796
|
+
key: "AUD",
|
|
1797
|
+
get: function get() {
|
|
1798
|
+
return 9;
|
|
1799
|
+
}
|
|
1800
|
+
}, {
|
|
1801
|
+
key: "TYPES",
|
|
1802
|
+
get: function get() {
|
|
1803
|
+
var _ref;
|
|
1804
|
+
return _ref = {}, _defineProperty(_ref, NALU264.IDR, 'IDR'), _defineProperty(_ref, NALU264.SEI, 'SEI'), _defineProperty(_ref, NALU264.SPS, 'SPS'), _defineProperty(_ref, NALU264.PPS, 'PPS'), _defineProperty(_ref, NALU264.NDR, 'NDR'), _defineProperty(_ref, NALU264.AUD, 'AUD'), _ref;
|
|
1805
|
+
}
|
|
1806
|
+
}]);
|
|
1807
|
+
return NALU264;
|
|
1808
|
+
}();
|
|
1809
|
+
|
|
1810
|
+
function appendByteArray(buffer1, buffer2) {
|
|
1811
|
+
var tmp = new Uint8Array((buffer1.byteLength | 0) + (buffer2.byteLength | 0));
|
|
1812
|
+
tmp.set(buffer1, 0);
|
|
1813
|
+
tmp.set(buffer2, buffer1.byteLength | 0);
|
|
1814
|
+
return tmp;
|
|
1815
|
+
}
|
|
1816
|
+
function secToTime(sec) {
|
|
1817
|
+
var seconds,
|
|
1818
|
+
hours,
|
|
1819
|
+
minutes,
|
|
1820
|
+
result = '';
|
|
1821
|
+
seconds = Math.floor(sec);
|
|
1822
|
+
hours = parseInt(seconds / 3600, 10) % 24;
|
|
1823
|
+
minutes = parseInt(seconds / 60, 10) % 60;
|
|
1824
|
+
seconds = seconds < 0 ? 0 : seconds % 60;
|
|
1825
|
+
if (hours > 0) {
|
|
1826
|
+
result += (hours < 10 ? '0' + hours : hours) + ':';
|
|
1827
|
+
}
|
|
1828
|
+
result += (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds);
|
|
1829
|
+
return result;
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
var H264Remuxer = /*#__PURE__*/function (_BaseRemuxer) {
|
|
1833
|
+
_inherits(H264Remuxer, _BaseRemuxer);
|
|
1834
|
+
var _super = _createSuper(H264Remuxer);
|
|
1835
|
+
function H264Remuxer(timescale, duration, frameDuration) {
|
|
1836
|
+
var _this;
|
|
1837
|
+
_classCallCheck(this, H264Remuxer);
|
|
1838
|
+
_this = _super.call(this, 'H264Remuxer');
|
|
1839
|
+
_this.frameDuration = frameDuration;
|
|
1840
|
+
_this.readyToDecode = false;
|
|
1841
|
+
_this.nextDts = 0;
|
|
1842
|
+
_this.dts = 0;
|
|
1843
|
+
_this.mp4track = {
|
|
1844
|
+
id: BaseRemuxer.getTrackID(),
|
|
1845
|
+
type: 'video',
|
|
1846
|
+
len: 0,
|
|
1847
|
+
fragmented: true,
|
|
1848
|
+
sps: '',
|
|
1849
|
+
pps: '',
|
|
1850
|
+
fps: 30,
|
|
1851
|
+
width: 0,
|
|
1852
|
+
height: 0,
|
|
1853
|
+
timescale: timescale,
|
|
1854
|
+
duration: duration,
|
|
1855
|
+
samples: []
|
|
1856
|
+
};
|
|
1857
|
+
_this.samples = [];
|
|
1858
|
+
_this.remainingData = new Uint8Array();
|
|
1859
|
+
_this.kfCounter = 0;
|
|
1860
|
+
_this.pendingUnits = {};
|
|
1861
|
+
return _this;
|
|
1862
|
+
}
|
|
1863
|
+
_createClass(H264Remuxer, [{
|
|
1864
|
+
key: "resetTrack",
|
|
1865
|
+
value: function resetTrack() {
|
|
1866
|
+
this.readyToDecode = false;
|
|
1867
|
+
this.mp4track.sps = '';
|
|
1868
|
+
this.mp4track.pps = '';
|
|
1869
|
+
this.nextDts = 0;
|
|
1870
|
+
this.dts = 0;
|
|
1871
|
+
this.remainingData = new Uint8Array();
|
|
1872
|
+
this.kfCounter = 0;
|
|
1873
|
+
this.pendingUnits = {};
|
|
1874
|
+
}
|
|
1875
|
+
}, {
|
|
1876
|
+
key: "feed",
|
|
1877
|
+
value: function feed(data, duration, compositionTimeOffset) {
|
|
1878
|
+
var slices = [];
|
|
1879
|
+
var left;
|
|
1880
|
+
data = appendByteArray(this.remainingData, data);
|
|
1881
|
+
var _H264Parser$extractNA = H264Parser.extractNALu(data);
|
|
1882
|
+
var _H264Parser$extractNA2 = _slicedToArray(_H264Parser$extractNA, 2);
|
|
1883
|
+
slices = _H264Parser$extractNA2[0];
|
|
1884
|
+
left = _H264Parser$extractNA2[1];
|
|
1885
|
+
this.remainingData = left || new Uint8Array();
|
|
1886
|
+
if (slices.length > 0) {
|
|
1887
|
+
this.remux(this.getVideoFrames(slices, duration, compositionTimeOffset));
|
|
1888
|
+
return true;
|
|
1889
|
+
} else {
|
|
1890
|
+
error('Failed to extract any NAL units from video data:', left);
|
|
1891
|
+
this.dispatch('outOfData');
|
|
1892
|
+
return false;
|
|
1893
|
+
}
|
|
1894
|
+
}
|
|
1895
|
+
}, {
|
|
1896
|
+
key: "getVideoFrames",
|
|
1897
|
+
value: function getVideoFrames(nalus, duration, compositionTimeOffset) {
|
|
1898
|
+
var _this2 = this;
|
|
1899
|
+
var units = [],
|
|
1900
|
+
frames = [],
|
|
1901
|
+
fd = 0,
|
|
1902
|
+
// frame duration
|
|
1903
|
+
tt = 0,
|
|
1904
|
+
// time ticks (remainder adjustment counter)
|
|
1905
|
+
keyFrame = false,
|
|
1906
|
+
vcl = false; // Video Coding Layer data (i.e., a "real" frame)
|
|
1907
|
+
if (this.pendingUnits.units) {
|
|
1908
|
+
units = this.pendingUnits.units;
|
|
1909
|
+
vcl = this.pendingUnits.vcl;
|
|
1910
|
+
keyFrame = this.pendingUnits.keyFrame;
|
|
1911
|
+
this.pendingUnits = {};
|
|
1912
|
+
}
|
|
1913
|
+
var _iterator = _createForOfIteratorHelper(nalus),
|
|
1914
|
+
_step;
|
|
1915
|
+
try {
|
|
1916
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1917
|
+
var nalu = _step.value;
|
|
1918
|
+
var unit = new NALU264(nalu);
|
|
1919
|
+
|
|
1920
|
+
// frame boundary detection
|
|
1921
|
+
if (units.length && vcl && (unit.isFirstSlice || !unit.isVCL)) {
|
|
1922
|
+
frames.push({
|
|
1923
|
+
units: units,
|
|
1924
|
+
keyFrame: keyFrame
|
|
1925
|
+
});
|
|
1926
|
+
units = [];
|
|
1927
|
+
keyFrame = false;
|
|
1928
|
+
vcl = false;
|
|
1929
|
+
}
|
|
1930
|
+
units.push(unit);
|
|
1931
|
+
keyFrame = keyFrame || unit.isKeyframe;
|
|
1932
|
+
vcl = vcl || unit.isVCL;
|
|
1933
|
+
}
|
|
1934
|
+
} catch (err) {
|
|
1935
|
+
_iterator.e(err);
|
|
1936
|
+
} finally {
|
|
1937
|
+
_iterator.f();
|
|
1938
|
+
}
|
|
1939
|
+
if (units.length) {
|
|
1940
|
+
// lets keep indecisive nalus as pending in case of fixed fps
|
|
1941
|
+
if (!duration) {
|
|
1942
|
+
this.pendingUnits = {
|
|
1943
|
+
units: units,
|
|
1944
|
+
keyFrame: keyFrame,
|
|
1945
|
+
vcl: vcl
|
|
1946
|
+
};
|
|
1947
|
+
} else if (vcl) {
|
|
1948
|
+
frames.push({
|
|
1949
|
+
units: units,
|
|
1950
|
+
keyFrame: keyFrame
|
|
1951
|
+
});
|
|
1952
|
+
} else {
|
|
1953
|
+
var last = frames.length - 1;
|
|
1954
|
+
if (last >= 0) {
|
|
1955
|
+
frames[last].units = frames[last].units.concat(units);
|
|
1956
|
+
}
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1959
|
+
fd = duration ? duration / frames.length | 0 : this.frameDuration;
|
|
1960
|
+
tt = duration ? duration - fd * frames.length : 0;
|
|
1961
|
+
frames.map(function (frame) {
|
|
1962
|
+
frame.duration = fd;
|
|
1963
|
+
frame.compositionTimeOffset = compositionTimeOffset;
|
|
1964
|
+
if (tt > 0) {
|
|
1965
|
+
frame.duration++;
|
|
1966
|
+
tt--;
|
|
1967
|
+
}
|
|
1968
|
+
_this2.kfCounter++;
|
|
1969
|
+
if (frame.keyFrame) {
|
|
1970
|
+
_this2.dispatch('keyframePosition', _this2.kfCounter * fd / 1000);
|
|
1971
|
+
}
|
|
1972
|
+
});
|
|
1973
|
+
log("jmuxer: No. of H264 frames of the last chunk: ".concat(frames.length));
|
|
1974
|
+
return frames;
|
|
1975
|
+
}
|
|
1976
|
+
}, {
|
|
1977
|
+
key: "remux",
|
|
1978
|
+
value: function remux(frames) {
|
|
1979
|
+
var _iterator2 = _createForOfIteratorHelper(frames),
|
|
1980
|
+
_step2;
|
|
1981
|
+
try {
|
|
1982
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
1983
|
+
var frame = _step2.value;
|
|
1984
|
+
var units = [];
|
|
1985
|
+
var size = 0;
|
|
1986
|
+
var _iterator3 = _createForOfIteratorHelper(frame.units),
|
|
1987
|
+
_step3;
|
|
1988
|
+
try {
|
|
1989
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
1990
|
+
var unit = _step3.value;
|
|
1991
|
+
if (this.parseNAL(unit)) {
|
|
1992
|
+
units.push(unit);
|
|
1993
|
+
size += unit.getSize();
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
} catch (err) {
|
|
1997
|
+
_iterator3.e(err);
|
|
1998
|
+
} finally {
|
|
1999
|
+
_iterator3.f();
|
|
2000
|
+
}
|
|
2001
|
+
if (units.length > 0 && this.readyToDecode) {
|
|
2002
|
+
this.mp4track.len += size;
|
|
2003
|
+
this.samples.push({
|
|
2004
|
+
units: units,
|
|
2005
|
+
size: size,
|
|
2006
|
+
keyFrame: frame.keyFrame,
|
|
2007
|
+
duration: frame.duration,
|
|
2008
|
+
compositionTimeOffset: frame.compositionTimeOffset
|
|
2009
|
+
});
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
} catch (err) {
|
|
2013
|
+
_iterator2.e(err);
|
|
2014
|
+
} finally {
|
|
2015
|
+
_iterator2.f();
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
}, {
|
|
2019
|
+
key: "getPayload",
|
|
2020
|
+
value: function getPayload() {
|
|
2021
|
+
if (!this.isReady()) {
|
|
2022
|
+
return null;
|
|
2023
|
+
}
|
|
2024
|
+
var payload = new Uint8Array(this.mp4track.len);
|
|
2025
|
+
var offset = 0;
|
|
2026
|
+
var samples = this.mp4track.samples;
|
|
2027
|
+
var mp4Sample, duration;
|
|
2028
|
+
this.dts = this.nextDts;
|
|
2029
|
+
while (this.samples.length) {
|
|
2030
|
+
var sample = this.samples.shift(),
|
|
2031
|
+
units = sample.units;
|
|
2032
|
+
duration = sample.duration;
|
|
2033
|
+
if (duration <= 0) {
|
|
2034
|
+
log("remuxer: invalid sample duration at DTS: ".concat(this.nextDts, " :").concat(duration));
|
|
2035
|
+
this.mp4track.len -= sample.size;
|
|
2036
|
+
continue;
|
|
2037
|
+
}
|
|
2038
|
+
this.nextDts += duration;
|
|
2039
|
+
mp4Sample = {
|
|
2040
|
+
size: sample.size,
|
|
2041
|
+
duration: duration,
|
|
2042
|
+
cts: sample.compositionTimeOffset || 0,
|
|
2043
|
+
flags: {
|
|
2044
|
+
isLeading: 0,
|
|
2045
|
+
isDependedOn: 0,
|
|
2046
|
+
hasRedundancy: 0,
|
|
2047
|
+
degradPrio: 0,
|
|
2048
|
+
isNonSync: sample.keyFrame ? 0 : 1,
|
|
2049
|
+
dependsOn: sample.keyFrame ? 2 : 1
|
|
2050
|
+
}
|
|
2051
|
+
};
|
|
2052
|
+
var _iterator4 = _createForOfIteratorHelper(units),
|
|
2053
|
+
_step4;
|
|
2054
|
+
try {
|
|
2055
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
2056
|
+
var unit = _step4.value;
|
|
2057
|
+
payload.set(unit.getData(), offset);
|
|
2058
|
+
offset += unit.getSize();
|
|
2059
|
+
}
|
|
2060
|
+
} catch (err) {
|
|
2061
|
+
_iterator4.e(err);
|
|
2062
|
+
} finally {
|
|
2063
|
+
_iterator4.f();
|
|
2064
|
+
}
|
|
2065
|
+
samples.push(mp4Sample);
|
|
2066
|
+
}
|
|
2067
|
+
if (!samples.length) return null;
|
|
2068
|
+
return new Uint8Array(payload.buffer, 0, this.mp4track.len);
|
|
2069
|
+
}
|
|
2070
|
+
}, {
|
|
2071
|
+
key: "parseSPS",
|
|
2072
|
+
value: function parseSPS(sps) {
|
|
2073
|
+
var config = H264Parser.readSPS(new Uint8Array(sps));
|
|
2074
|
+
this.mp4track.fps = config.fps || this.mp4track.fps;
|
|
2075
|
+
this.mp4track.width = config.width;
|
|
2076
|
+
this.mp4track.height = config.height;
|
|
2077
|
+
this.mp4track.sps = [new Uint8Array(sps)];
|
|
2078
|
+
this.mp4track.codec = 'avc1.';
|
|
2079
|
+
var codecarray = new DataView(sps.buffer, sps.byteOffset + 1, 4);
|
|
2080
|
+
for (var i = 0; i < 3; ++i) {
|
|
2081
|
+
var h = codecarray.getUint8(i).toString(16);
|
|
2082
|
+
if (h.length < 2) {
|
|
2083
|
+
h = '0' + h;
|
|
2084
|
+
}
|
|
2085
|
+
this.mp4track.codec += h;
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
}, {
|
|
2089
|
+
key: "parsePPS",
|
|
2090
|
+
value: function parsePPS(pps) {
|
|
2091
|
+
this.mp4track.pps = [new Uint8Array(pps)];
|
|
2092
|
+
}
|
|
2093
|
+
}, {
|
|
2094
|
+
key: "parseNAL",
|
|
2095
|
+
value: function parseNAL(unit) {
|
|
2096
|
+
if (!unit) return false;
|
|
2097
|
+
if (unit.isVCL) {
|
|
2098
|
+
return true;
|
|
2099
|
+
}
|
|
2100
|
+
var push = false;
|
|
2101
|
+
switch (unit.type()) {
|
|
2102
|
+
case NALU264.PPS:
|
|
2103
|
+
if (!this.mp4track.pps) {
|
|
2104
|
+
this.parsePPS(unit.getPayload());
|
|
2105
|
+
}
|
|
2106
|
+
push = true;
|
|
2107
|
+
break;
|
|
2108
|
+
case NALU264.SPS:
|
|
2109
|
+
if (!this.mp4track.sps) {
|
|
2110
|
+
this.parseSPS(unit.getPayload());
|
|
2111
|
+
}
|
|
2112
|
+
push = true;
|
|
2113
|
+
break;
|
|
2114
|
+
case NALU264.AUD:
|
|
2115
|
+
log('AUD - ignoing');
|
|
2116
|
+
break;
|
|
2117
|
+
case NALU264.SEI:
|
|
2118
|
+
log('SEI - ignoing');
|
|
2119
|
+
break;
|
|
2120
|
+
}
|
|
2121
|
+
if (!this.readyToDecode && this.mp4track.pps && this.mp4track.sps) {
|
|
2122
|
+
this.readyToDecode = true;
|
|
2123
|
+
}
|
|
2124
|
+
return push;
|
|
2125
|
+
}
|
|
2126
|
+
}]);
|
|
2127
|
+
return H264Remuxer;
|
|
2128
|
+
}(BaseRemuxer);
|
|
2129
|
+
|
|
2130
|
+
// spec https://www.itu.int/rec/T-REC-H.265/
|
|
2131
|
+
|
|
2132
|
+
var H265Parser = /*#__PURE__*/function () {
|
|
2133
|
+
function H265Parser() {
|
|
2134
|
+
_classCallCheck(this, H265Parser);
|
|
2135
|
+
}
|
|
2136
|
+
_createClass(H265Parser, null, [{
|
|
2137
|
+
key: "extractNALu",
|
|
2138
|
+
value: function extractNALu(buffer) {
|
|
2139
|
+
var i = 0,
|
|
2140
|
+
length = buffer.byteLength,
|
|
2141
|
+
result = [],
|
|
2142
|
+
lastIndex = 0,
|
|
2143
|
+
zeroCount = 0;
|
|
2144
|
+
while (i < length) {
|
|
2145
|
+
var value = buffer[i++];
|
|
2146
|
+
if (value === 0) {
|
|
2147
|
+
zeroCount++;
|
|
2148
|
+
} else if (value === 1 && zeroCount >= 2) {
|
|
2149
|
+
var startCodeLength = zeroCount + 1;
|
|
2150
|
+
if (lastIndex !== i - startCodeLength) {
|
|
2151
|
+
result.push(buffer.subarray(lastIndex, i - startCodeLength));
|
|
2152
|
+
}
|
|
2153
|
+
lastIndex = i;
|
|
2154
|
+
zeroCount = 0;
|
|
2155
|
+
} else {
|
|
2156
|
+
zeroCount = 0;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
|
|
2160
|
+
// Remaining data after last start code
|
|
2161
|
+
var left = null;
|
|
2162
|
+
if (lastIndex < length) {
|
|
2163
|
+
left = buffer.subarray(lastIndex, length);
|
|
2164
|
+
}
|
|
2165
|
+
return [result, left];
|
|
2166
|
+
}
|
|
2167
|
+
}, {
|
|
2168
|
+
key: "removeEmulationPreventionBytes",
|
|
2169
|
+
value: function removeEmulationPreventionBytes(nal) {
|
|
2170
|
+
var rbsp = [];
|
|
2171
|
+
var zeroCount = 0;
|
|
2172
|
+
for (var i = 0; i < nal.length; i++) {
|
|
2173
|
+
var value = nal[i];
|
|
2174
|
+
if (zeroCount === 2 && value === 0x03) {
|
|
2175
|
+
// Skip this emulation prevention byte
|
|
2176
|
+
zeroCount = 0;
|
|
2177
|
+
continue;
|
|
2178
|
+
}
|
|
2179
|
+
rbsp.push(value);
|
|
2180
|
+
if (value === 0x00) {
|
|
2181
|
+
zeroCount++;
|
|
2182
|
+
} else {
|
|
2183
|
+
zeroCount = 0;
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
return new Uint8Array(rbsp);
|
|
2187
|
+
}
|
|
2188
|
+
|
|
2189
|
+
/**
|
|
2190
|
+
* Read a sequence parameter set and return some interesting video
|
|
2191
|
+
* properties. A sequence parameter set is the H265 metadata that
|
|
2192
|
+
* describes the properties of upcoming video frames.
|
|
2193
|
+
* @param data {Uint8Array} the bytes of a sequence parameter set
|
|
2194
|
+
* @return {object} an object with configuration parsed from the
|
|
2195
|
+
* sequence parameter set, including the dimensions of the
|
|
2196
|
+
* associated video frames.
|
|
2197
|
+
*/
|
|
2198
|
+
}, {
|
|
2199
|
+
key: "readSPS",
|
|
2200
|
+
value: function readSPS(data) {
|
|
2201
|
+
var decoder = new ExpGolomb(data);
|
|
2202
|
+
|
|
2203
|
+
// Skip NALU header (2 bytes for HEVC)
|
|
2204
|
+
decoder.readUByte();
|
|
2205
|
+
decoder.readUByte();
|
|
2206
|
+
decoder.readBits(4); // sps_video_parameter_set_id
|
|
2207
|
+
decoder.readBits(3); // sps_max_sub_layers_minus1
|
|
2208
|
+
decoder.readBits(1); // sps_temporal_id_nesting_flag
|
|
2209
|
+
|
|
2210
|
+
// --- profile_tier_level() ---
|
|
2211
|
+
var profile_space = decoder.readBits(2);
|
|
2212
|
+
var tier_flag = decoder.readBits(1);
|
|
2213
|
+
var profile_idc = decoder.readBits(5);
|
|
2214
|
+
var profile_compatibility_flags = decoder.readUInt(); // 32 bits
|
|
2215
|
+
var constraint_indicator_flags = new Uint8Array(6);
|
|
2216
|
+
for (var i = 0; i < 6; i++) {
|
|
2217
|
+
constraint_indicator_flags[i] = decoder.readUByte();
|
|
2218
|
+
}
|
|
2219
|
+
var level_idc = decoder.readUByte();
|
|
2220
|
+
decoder.readUEG(); // seq_parameter_set_id
|
|
2221
|
+
|
|
2222
|
+
var chroma_format_idc = decoder.readUEG();
|
|
2223
|
+
if (chroma_format_idc === 3) {
|
|
2224
|
+
decoder.readBits(1); // separate_colour_plane_flag
|
|
2225
|
+
}
|
|
2226
|
+
var pic_width_in_luma_samples = decoder.readUEG();
|
|
2227
|
+
var pic_height_in_luma_samples = decoder.readUEG();
|
|
2228
|
+
|
|
2229
|
+
// Cropping
|
|
2230
|
+
var conformance_window_flag = decoder.readBoolean();
|
|
2231
|
+
var conf_win_left_offset = 0,
|
|
2232
|
+
conf_win_right_offset = 0,
|
|
2233
|
+
conf_win_top_offset = 0,
|
|
2234
|
+
conf_win_bottom_offset = 0;
|
|
2235
|
+
if (conformance_window_flag) {
|
|
2236
|
+
conf_win_left_offset = decoder.readUEG();
|
|
2237
|
+
conf_win_right_offset = decoder.readUEG();
|
|
2238
|
+
conf_win_top_offset = decoder.readUEG();
|
|
2239
|
+
conf_win_bottom_offset = decoder.readUEG();
|
|
2240
|
+
}
|
|
2241
|
+
var fps = null;
|
|
2242
|
+
var vui_parameters_present_flag = decoder.readBoolean();
|
|
2243
|
+
if (vui_parameters_present_flag) {
|
|
2244
|
+
var aspect_ratio_info_present_flag = decoder.readBoolean();
|
|
2245
|
+
if (aspect_ratio_info_present_flag) {
|
|
2246
|
+
var aspect_ratio_idc = decoder.readUByte();
|
|
2247
|
+
if (aspect_ratio_idc === 255) {
|
|
2248
|
+
// Extended_SAR
|
|
2249
|
+
decoder.readUShort(); // sar_width
|
|
2250
|
+
decoder.readUShort(); // sar_height
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
var overscan_info_present_flag = decoder.readBoolean();
|
|
2254
|
+
if (overscan_info_present_flag) {
|
|
2255
|
+
decoder.readBoolean(); // overscan_appropriate_flag
|
|
2256
|
+
}
|
|
2257
|
+
var video_signal_type_present_flag = decoder.readBoolean();
|
|
2258
|
+
if (video_signal_type_present_flag) {
|
|
2259
|
+
decoder.readBits(3); // video_format
|
|
2260
|
+
decoder.readBoolean(); // video_full_range_flag
|
|
2261
|
+
var colour_description_present_flag = decoder.readBoolean();
|
|
2262
|
+
if (colour_description_present_flag) {
|
|
2263
|
+
decoder.readUByte(); // colour_primaries
|
|
2264
|
+
decoder.readUByte(); // transfer_characteristics
|
|
2265
|
+
decoder.readUByte(); // matrix_coeffs
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
var chroma_loc_info_present_flag = decoder.readBoolean();
|
|
2269
|
+
if (chroma_loc_info_present_flag) {
|
|
2270
|
+
decoder.readUEG(); // chroma_sample_loc_type_top_field
|
|
2271
|
+
decoder.readUEG(); // chroma_sample_loc_type_bottom_field
|
|
2272
|
+
}
|
|
2273
|
+
decoder.readBoolean(); // neutral_chroma_indication_flag
|
|
2274
|
+
decoder.readBoolean(); // field_seq_flag
|
|
2275
|
+
decoder.readBoolean(); // frame_field_info_present_flag
|
|
2276
|
+
|
|
2277
|
+
// Timing info
|
|
2278
|
+
var timing_info_present_flag = decoder.readBoolean();
|
|
2279
|
+
if (timing_info_present_flag) {
|
|
2280
|
+
var num_units_in_tick = decoder.readUInt();
|
|
2281
|
+
var time_scale = decoder.readUInt();
|
|
2282
|
+
decoder.readBoolean(); // poc_proportional_to_timing_flag
|
|
2283
|
+
if (num_units_in_tick) {
|
|
2284
|
+
fps = time_scale / (2 * num_units_in_tick);
|
|
2285
|
+
}
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
// Final width/height
|
|
2290
|
+
var sub_width_c = chroma_format_idc === 1 || chroma_format_idc === 2 ? 2 : 1;
|
|
2291
|
+
var sub_height_c = chroma_format_idc === 1 ? 2 : 1;
|
|
2292
|
+
var width = pic_width_in_luma_samples - sub_width_c * (conf_win_right_offset + conf_win_left_offset);
|
|
2293
|
+
var height = pic_height_in_luma_samples - sub_height_c * (conf_win_top_offset + conf_win_bottom_offset);
|
|
2294
|
+
return {
|
|
2295
|
+
width: width,
|
|
2296
|
+
height: height,
|
|
2297
|
+
profile_space: profile_space,
|
|
2298
|
+
tier_flag: tier_flag,
|
|
2299
|
+
profile_idc: profile_idc,
|
|
2300
|
+
profile_compatibility_flags: profile_compatibility_flags,
|
|
2301
|
+
constraint_indicator_flags: constraint_indicator_flags,
|
|
2302
|
+
level_idc: level_idc,
|
|
2303
|
+
chroma_format_idc: chroma_format_idc,
|
|
2304
|
+
fps: fps
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
}]);
|
|
2308
|
+
return H265Parser;
|
|
2309
|
+
}();
|
|
2310
|
+
var NALU265 = /*#__PURE__*/function () {
|
|
2311
|
+
function NALU265(data) {
|
|
2312
|
+
_classCallCheck(this, NALU265);
|
|
2313
|
+
this.payload = data;
|
|
2314
|
+
this.nalUnitType = (data[0] & 126) >> 1;
|
|
2315
|
+
this.nuhLayerId = (data[0] & 1) << 5 | (data[1] & 248) >> 3;
|
|
2316
|
+
this.nuhTemporalIdPlus1 = data[1] & 7;
|
|
2317
|
+
this._isFirstSlice = null;
|
|
2318
|
+
this._sliceType = null;
|
|
2319
|
+
}
|
|
2320
|
+
_createClass(NALU265, [{
|
|
2321
|
+
key: "toString",
|
|
2322
|
+
value: function toString() {
|
|
2323
|
+
return "".concat(NALU265.TYPES[this.type()] || 'UNKNOWN (' + this.type() + ')', ": Layer: ").concat(this.nuhLayerId, ", Temporal Id: ").concat(this.nuhTemporalIdPlus1);
|
|
2324
|
+
}
|
|
2325
|
+
}, {
|
|
2326
|
+
key: "type",
|
|
2327
|
+
value: function type() {
|
|
2328
|
+
return this.nalUnitType;
|
|
2329
|
+
}
|
|
2330
|
+
}, {
|
|
2331
|
+
key: "isKeyframe",
|
|
2332
|
+
get: function get() {
|
|
2333
|
+
return [NALU265.IDR_W_RADL, NALU265.IDR_N_LP, NALU265.CRA].includes(this.nalUnitType);
|
|
2334
|
+
}
|
|
2335
|
+
}, {
|
|
2336
|
+
key: "isVCL",
|
|
2337
|
+
get: function get() {
|
|
2338
|
+
return this.nalUnitType <= 31;
|
|
2339
|
+
}
|
|
2340
|
+
}, {
|
|
2341
|
+
key: "parseHeader",
|
|
2342
|
+
value: function parseHeader() {
|
|
2343
|
+
var decoder = new ExpGolomb(this.getPayload());
|
|
2344
|
+
// skip NALu type
|
|
2345
|
+
decoder.readUByte();
|
|
2346
|
+
decoder.readUByte();
|
|
2347
|
+
|
|
2348
|
+
// first_slice_segment_in_pic_flag
|
|
2349
|
+
this._isFirstSlice = decoder.readBoolean();
|
|
2350
|
+
|
|
2351
|
+
// if NALU is not IDR/CRA/other IRAP, next comes no_output_of_prior_pics_flag
|
|
2352
|
+
if (this.isKeyframe) {
|
|
2353
|
+
decoder.readBits(1); // no_output_of_prior_pics_flag
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
// slice_pic_parameter_set_id
|
|
2357
|
+
decoder.readUEG();
|
|
2358
|
+
|
|
2359
|
+
// slice_type (only for some NALU types, but useful for debugging)
|
|
2360
|
+
this._sliceType = decoder.readUEG();
|
|
2361
|
+
}
|
|
2362
|
+
}, {
|
|
2363
|
+
key: "isFirstSlice",
|
|
2364
|
+
get: function get() {
|
|
2365
|
+
if (!this._isFirstSlice) {
|
|
2366
|
+
this.parseHeader();
|
|
2367
|
+
}
|
|
2368
|
+
return this._isFirstSlice;
|
|
2369
|
+
}
|
|
2370
|
+
}, {
|
|
2371
|
+
key: "sliceType",
|
|
2372
|
+
get: function get() {
|
|
2373
|
+
if (!this._sliceType) {
|
|
2374
|
+
this.parseHeader();
|
|
2375
|
+
}
|
|
2376
|
+
return this._sliceType;
|
|
2377
|
+
}
|
|
2378
|
+
}, {
|
|
2379
|
+
key: "getPayload",
|
|
2380
|
+
value: function getPayload() {
|
|
2381
|
+
return this.payload;
|
|
2382
|
+
}
|
|
2383
|
+
}, {
|
|
2384
|
+
key: "getPayloadSize",
|
|
2385
|
+
value: function getPayloadSize() {
|
|
2386
|
+
return this.payload.byteLength;
|
|
2387
|
+
}
|
|
2388
|
+
}, {
|
|
2389
|
+
key: "getSize",
|
|
2390
|
+
value: function getSize() {
|
|
2391
|
+
return 4 + this.getPayloadSize();
|
|
2392
|
+
}
|
|
2393
|
+
}, {
|
|
2394
|
+
key: "getData",
|
|
2395
|
+
value: function getData() {
|
|
2396
|
+
var result = new Uint8Array(this.getSize());
|
|
2397
|
+
var view = new DataView(result.buffer);
|
|
2398
|
+
view.setUint32(0, this.getSize() - 4);
|
|
2399
|
+
result.set(this.getPayload(), 4);
|
|
2400
|
+
return result;
|
|
2401
|
+
}
|
|
2402
|
+
}], [{
|
|
2403
|
+
key: "TRAIL_N",
|
|
2404
|
+
get: function get() {
|
|
2405
|
+
return 0;
|
|
2406
|
+
}
|
|
2407
|
+
}, {
|
|
2408
|
+
key: "TRAIL_R",
|
|
2409
|
+
get: function get() {
|
|
2410
|
+
return 1;
|
|
2411
|
+
}
|
|
2412
|
+
}, {
|
|
2413
|
+
key: "IDR_W_RADL",
|
|
2414
|
+
get: function get() {
|
|
2415
|
+
return 19;
|
|
2416
|
+
}
|
|
2417
|
+
}, {
|
|
2418
|
+
key: "IDR_N_LP",
|
|
2419
|
+
get: function get() {
|
|
2420
|
+
return 20;
|
|
2421
|
+
}
|
|
2422
|
+
}, {
|
|
2423
|
+
key: "CRA",
|
|
2424
|
+
get: function get() {
|
|
2425
|
+
return 21;
|
|
2426
|
+
}
|
|
2427
|
+
}, {
|
|
2428
|
+
key: "VPS",
|
|
2429
|
+
get: function get() {
|
|
2430
|
+
return 32;
|
|
2431
|
+
}
|
|
2432
|
+
}, {
|
|
2433
|
+
key: "SPS",
|
|
2434
|
+
get: function get() {
|
|
2435
|
+
return 33;
|
|
2436
|
+
}
|
|
2437
|
+
}, {
|
|
2438
|
+
key: "PPS",
|
|
2439
|
+
get: function get() {
|
|
2440
|
+
return 34;
|
|
2441
|
+
}
|
|
2442
|
+
}, {
|
|
2443
|
+
key: "AUD",
|
|
2444
|
+
get: function get() {
|
|
2445
|
+
return 35;
|
|
2446
|
+
}
|
|
2447
|
+
}, {
|
|
2448
|
+
key: "SEI",
|
|
2449
|
+
get: function get() {
|
|
2450
|
+
return 39;
|
|
2451
|
+
}
|
|
2452
|
+
}, {
|
|
2453
|
+
key: "SEI2",
|
|
2454
|
+
get: function get() {
|
|
2455
|
+
return 40;
|
|
2456
|
+
}
|
|
2457
|
+
}, {
|
|
2458
|
+
key: "TYPES",
|
|
2459
|
+
get: function get() {
|
|
2460
|
+
var _ref;
|
|
2461
|
+
return _ref = {}, _defineProperty(_ref, NALU265.TRAIL_N, 'TRAIL_N'), _defineProperty(_ref, NALU265.TRAIL_R, 'TRAIL_R'), _defineProperty(_ref, NALU265.IDR_W_RADL, 'IDR'), _defineProperty(_ref, NALU265.IDR_N_LP, 'IDR2'), _defineProperty(_ref, NALU265.CRA, 'CRA'), _defineProperty(_ref, NALU265.VPS, 'VPS'), _defineProperty(_ref, NALU265.SPS, 'SPS'), _defineProperty(_ref, NALU265.PPS, 'PPS'), _defineProperty(_ref, NALU265.AUD, 'AUD'), _defineProperty(_ref, NALU265.SEI, 'SEI'), _defineProperty(_ref, NALU265.SEI2, 'SEI2'), _ref;
|
|
2462
|
+
}
|
|
2463
|
+
}]);
|
|
2464
|
+
return NALU265;
|
|
2465
|
+
}();
|
|
2466
|
+
|
|
2467
|
+
var H265Remuxer = /*#__PURE__*/function (_BaseRemuxer) {
|
|
2468
|
+
_inherits(H265Remuxer, _BaseRemuxer);
|
|
2469
|
+
var _super = _createSuper(H265Remuxer);
|
|
2470
|
+
function H265Remuxer(timescale, duration, frameDuration) {
|
|
2471
|
+
var _this;
|
|
2472
|
+
_classCallCheck(this, H265Remuxer);
|
|
2473
|
+
_this = _super.call(this, 'H264Remuxer');
|
|
2474
|
+
_this.frameDuration = frameDuration;
|
|
2475
|
+
_this.readyToDecode = false;
|
|
2476
|
+
_this.nextDts = 0;
|
|
2477
|
+
_this.dts = 0;
|
|
2478
|
+
_this.mp4track = {
|
|
2479
|
+
id: BaseRemuxer.getTrackID(),
|
|
2480
|
+
type: 'video',
|
|
2481
|
+
len: 0,
|
|
2482
|
+
fragmented: true,
|
|
2483
|
+
vps: '',
|
|
2484
|
+
sps: '',
|
|
2485
|
+
pps: '',
|
|
2486
|
+
hvcC: {},
|
|
2487
|
+
fps: 30,
|
|
2488
|
+
width: 0,
|
|
2489
|
+
height: 0,
|
|
2490
|
+
timescale: timescale,
|
|
2491
|
+
duration: duration,
|
|
2492
|
+
samples: []
|
|
2493
|
+
};
|
|
2494
|
+
_this.samples = [];
|
|
2495
|
+
_this.remainingData = new Uint8Array();
|
|
2496
|
+
_this.kfCounter = 0;
|
|
2497
|
+
_this.pendingUnits = {};
|
|
2498
|
+
return _this;
|
|
2499
|
+
}
|
|
2500
|
+
_createClass(H265Remuxer, [{
|
|
2501
|
+
key: "resetTrack",
|
|
2502
|
+
value: function resetTrack() {
|
|
2503
|
+
this.readyToDecode = false;
|
|
2504
|
+
this.mp4track.vps = '';
|
|
2505
|
+
this.mp4track.sps = '';
|
|
2506
|
+
this.mp4track.pps = '';
|
|
2507
|
+
this.mp4track.hvcC = {};
|
|
2508
|
+
this.nextDts = 0;
|
|
2509
|
+
this.dts = 0;
|
|
2510
|
+
this.remainingData = new Uint8Array();
|
|
2511
|
+
this.kfCounter = 0;
|
|
2512
|
+
this.pendingUnits = {};
|
|
2513
|
+
}
|
|
2514
|
+
}, {
|
|
2515
|
+
key: "feed",
|
|
2516
|
+
value: function feed(data, duration, compositionTimeOffset) {
|
|
2517
|
+
var slices = [];
|
|
2518
|
+
var left;
|
|
2519
|
+
data = appendByteArray(this.remainingData, data);
|
|
2520
|
+
var _H265Parser$extractNA = H265Parser.extractNALu(data);
|
|
2521
|
+
var _H265Parser$extractNA2 = _slicedToArray(_H265Parser$extractNA, 2);
|
|
2522
|
+
slices = _H265Parser$extractNA2[0];
|
|
2523
|
+
left = _H265Parser$extractNA2[1];
|
|
2524
|
+
this.remainingData = left || new Uint8Array();
|
|
2525
|
+
if (slices.length > 0) {
|
|
2526
|
+
this.remux(this.getVideoFrames(slices, duration, compositionTimeOffset));
|
|
2527
|
+
return true;
|
|
2528
|
+
} else {
|
|
2529
|
+
error('Failed to extract any NAL units from video data:', left);
|
|
2530
|
+
this.dispatch('outOfData');
|
|
2531
|
+
return false;
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
}, {
|
|
2535
|
+
key: "getVideoFrames",
|
|
2536
|
+
value: function getVideoFrames(nalus, duration, compositionTimeOffset) {
|
|
2537
|
+
var _this2 = this;
|
|
2538
|
+
var units = [],
|
|
2539
|
+
frames = [],
|
|
2540
|
+
fd = 0,
|
|
2541
|
+
tt = 0,
|
|
2542
|
+
keyFrame = false,
|
|
2543
|
+
vcl = false;
|
|
2544
|
+
if (this.pendingUnits.units) {
|
|
2545
|
+
units = this.pendingUnits.units;
|
|
2546
|
+
vcl = this.pendingUnits.vcl;
|
|
2547
|
+
keyFrame = this.pendingUnits.keyFrame;
|
|
2548
|
+
this.pendingUnits = {};
|
|
2549
|
+
}
|
|
2550
|
+
var _iterator = _createForOfIteratorHelper(nalus),
|
|
2551
|
+
_step;
|
|
2552
|
+
try {
|
|
2553
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
2554
|
+
var nalu = _step.value;
|
|
2555
|
+
var unit = new NALU265(nalu);
|
|
2556
|
+
|
|
2557
|
+
// frame boundary detection
|
|
2558
|
+
if (units.length && vcl && (unit.isFirstSlice || !unit.isVCL)) {
|
|
2559
|
+
frames.push({
|
|
2560
|
+
units: units,
|
|
2561
|
+
keyFrame: keyFrame
|
|
2562
|
+
});
|
|
2563
|
+
units = [];
|
|
2564
|
+
keyFrame = false;
|
|
2565
|
+
vcl = false;
|
|
2566
|
+
}
|
|
2567
|
+
units.push(unit);
|
|
2568
|
+
keyFrame = keyFrame || unit.isKeyframe;
|
|
2569
|
+
vcl = vcl || unit.isVCL;
|
|
2570
|
+
}
|
|
2571
|
+
} catch (err) {
|
|
2572
|
+
_iterator.e(err);
|
|
2573
|
+
} finally {
|
|
2574
|
+
_iterator.f();
|
|
2575
|
+
}
|
|
2576
|
+
if (units.length) {
|
|
2577
|
+
if (!duration) {
|
|
2578
|
+
this.pendingUnits = {
|
|
2579
|
+
units: units,
|
|
2580
|
+
keyFrame: keyFrame,
|
|
2581
|
+
vcl: vcl
|
|
2582
|
+
};
|
|
2583
|
+
} else if (vcl) {
|
|
2584
|
+
frames.push({
|
|
2585
|
+
units: units,
|
|
2586
|
+
keyFrame: keyFrame
|
|
2587
|
+
});
|
|
2588
|
+
} else {
|
|
2589
|
+
var last = frames.length - 1;
|
|
2590
|
+
if (last >= 0) {
|
|
2591
|
+
frames[last].units = frames[last].units.concat(units);
|
|
2592
|
+
}
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
fd = duration ? duration / frames.length | 0 : this.frameDuration;
|
|
2596
|
+
tt = duration ? duration - fd * frames.length : 0;
|
|
2597
|
+
frames.map(function (frame) {
|
|
2598
|
+
frame.duration = fd;
|
|
2599
|
+
frame.compositionTimeOffset = compositionTimeOffset;
|
|
2600
|
+
if (tt > 0) {
|
|
2601
|
+
frame.duration++;
|
|
2602
|
+
tt--;
|
|
2603
|
+
}
|
|
2604
|
+
_this2.kfCounter++;
|
|
2605
|
+
if (frame.keyFrame) {
|
|
2606
|
+
_this2.dispatch('keyframePosition', _this2.kfCounter * fd / 1000);
|
|
2607
|
+
}
|
|
2608
|
+
});
|
|
2609
|
+
log("jmuxer: No. of H265 frames of the last chunk: ".concat(frames.length));
|
|
2610
|
+
return frames;
|
|
2611
|
+
}
|
|
2612
|
+
}, {
|
|
2613
|
+
key: "remux",
|
|
2614
|
+
value: function remux(frames) {
|
|
2615
|
+
var _iterator2 = _createForOfIteratorHelper(frames),
|
|
2616
|
+
_step2;
|
|
2617
|
+
try {
|
|
2618
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
2619
|
+
var frame = _step2.value;
|
|
2620
|
+
var units = [];
|
|
2621
|
+
var size = 0;
|
|
2622
|
+
var _iterator3 = _createForOfIteratorHelper(frame.units),
|
|
2623
|
+
_step3;
|
|
2624
|
+
try {
|
|
2625
|
+
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
2626
|
+
var unit = _step3.value;
|
|
2627
|
+
if (this.parseNAL(unit)) {
|
|
2628
|
+
units.push(unit);
|
|
2629
|
+
size += unit.getSize();
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
} catch (err) {
|
|
2633
|
+
_iterator3.e(err);
|
|
2634
|
+
} finally {
|
|
2635
|
+
_iterator3.f();
|
|
2636
|
+
}
|
|
2637
|
+
if (units.length > 0 && this.readyToDecode) {
|
|
2638
|
+
this.mp4track.len += size;
|
|
2639
|
+
this.samples.push({
|
|
2640
|
+
units: units,
|
|
2641
|
+
size: size,
|
|
2642
|
+
keyFrame: frame.keyFrame,
|
|
2643
|
+
duration: frame.duration,
|
|
2644
|
+
compositionTimeOffset: frame.compositionTimeOffset
|
|
2645
|
+
});
|
|
2646
|
+
}
|
|
2647
|
+
}
|
|
2648
|
+
} catch (err) {
|
|
2649
|
+
_iterator2.e(err);
|
|
2650
|
+
} finally {
|
|
2651
|
+
_iterator2.f();
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
}, {
|
|
2655
|
+
key: "getPayload",
|
|
2656
|
+
value: function getPayload() {
|
|
2657
|
+
if (!this.isReady()) {
|
|
2658
|
+
return null;
|
|
2659
|
+
}
|
|
2660
|
+
var payload = new Uint8Array(this.mp4track.len);
|
|
2661
|
+
var offset = 0;
|
|
2662
|
+
var samples = this.mp4track.samples;
|
|
2663
|
+
var mp4Sample, duration;
|
|
2664
|
+
this.dts = this.nextDts;
|
|
2665
|
+
while (this.samples.length) {
|
|
2666
|
+
var sample = this.samples.shift(),
|
|
2667
|
+
units = sample.units;
|
|
2668
|
+
duration = sample.duration;
|
|
2669
|
+
if (duration <= 0) {
|
|
2670
|
+
log("remuxer: invalid sample duration at DTS: ".concat(this.nextDts, " :").concat(duration));
|
|
2671
|
+
this.mp4track.len -= sample.size;
|
|
2672
|
+
continue;
|
|
2673
|
+
}
|
|
2674
|
+
this.nextDts += duration;
|
|
2675
|
+
mp4Sample = {
|
|
2676
|
+
size: sample.size,
|
|
2677
|
+
duration: duration,
|
|
2678
|
+
cts: sample.compositionTimeOffset || 0,
|
|
2679
|
+
flags: {
|
|
2680
|
+
isLeading: 0,
|
|
2681
|
+
isDependedOn: 0,
|
|
2682
|
+
hasRedundancy: 0,
|
|
2683
|
+
degradPrio: 0,
|
|
2684
|
+
isNonSync: sample.keyFrame ? 0 : 1,
|
|
2685
|
+
dependsOn: sample.keyFrame ? 2 : 1
|
|
2686
|
+
}
|
|
2687
|
+
};
|
|
2688
|
+
var _iterator4 = _createForOfIteratorHelper(units),
|
|
2689
|
+
_step4;
|
|
2690
|
+
try {
|
|
2691
|
+
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
2692
|
+
var unit = _step4.value;
|
|
2693
|
+
payload.set(unit.getData(), offset);
|
|
2694
|
+
offset += unit.getSize();
|
|
2695
|
+
}
|
|
2696
|
+
} catch (err) {
|
|
2697
|
+
_iterator4.e(err);
|
|
2698
|
+
} finally {
|
|
2699
|
+
_iterator4.f();
|
|
2700
|
+
}
|
|
2701
|
+
samples.push(mp4Sample);
|
|
2702
|
+
}
|
|
2703
|
+
if (!samples.length) return null;
|
|
2704
|
+
return new Uint8Array(payload.buffer, 0, this.mp4track.len);
|
|
2705
|
+
}
|
|
2706
|
+
}, {
|
|
2707
|
+
key: "parseSPS",
|
|
2708
|
+
value: function parseSPS(sps) {
|
|
2709
|
+
this.mp4track.sps = [new Uint8Array(sps)];
|
|
2710
|
+
sps = H265Parser.removeEmulationPreventionBytes(sps);
|
|
2711
|
+
var config = H265Parser.readSPS(new Uint8Array(sps));
|
|
2712
|
+
this.mp4track.fps = config.fps || this.mp4track.fps;
|
|
2713
|
+
this.mp4track.width = config.width;
|
|
2714
|
+
this.mp4track.height = config.height;
|
|
2715
|
+
this.mp4track.codec = "hvc1.".concat(config.profile_idc, ".").concat(config.profile_compatibility_flags.toString(16)) + ".L".concat(config.level_idc).concat(config.tier_flag ? 'H' : 'L') + ".".concat(config.constraint_indicator_flags.map(function (b) {
|
|
2716
|
+
return b.toString(16);
|
|
2717
|
+
}).join('.').toUpperCase());
|
|
2718
|
+
this.mp4track.hvcC = {
|
|
2719
|
+
profile_space: config.profile_space,
|
|
2720
|
+
tier_flag: config.tier_flag,
|
|
2721
|
+
profile_idc: config.profile_idc,
|
|
2722
|
+
profile_compatibility_flags: config.profile_compatibility_flags,
|
|
2723
|
+
constraint_indicator_flags: config.constraint_indicator_flags,
|
|
2724
|
+
level_idc: config.level_idc,
|
|
2725
|
+
chroma_format_idc: config.chroma_format_idc
|
|
2726
|
+
};
|
|
2727
|
+
}
|
|
2728
|
+
}, {
|
|
2729
|
+
key: "parsePPS",
|
|
2730
|
+
value: function parsePPS(pps) {
|
|
2731
|
+
this.mp4track.pps = [pps];
|
|
2732
|
+
}
|
|
2733
|
+
}, {
|
|
2734
|
+
key: "parseVPS",
|
|
2735
|
+
value: function parseVPS(vps) {
|
|
2736
|
+
this.mp4track.vps = [vps];
|
|
2737
|
+
}
|
|
2738
|
+
}, {
|
|
2739
|
+
key: "parseNAL",
|
|
2740
|
+
value: function parseNAL(unit) {
|
|
2741
|
+
if (!unit) return false;
|
|
2742
|
+
if (unit.isVCL) {
|
|
2743
|
+
return true;
|
|
2744
|
+
}
|
|
2745
|
+
var push = false;
|
|
2746
|
+
switch (unit.type()) {
|
|
2747
|
+
case NALU265.VPS:
|
|
2748
|
+
if (!this.mp4track.vps) {
|
|
2749
|
+
this.parseVPS(unit.getPayload());
|
|
2750
|
+
}
|
|
2751
|
+
push = true;
|
|
2752
|
+
break;
|
|
2753
|
+
case NALU265.SPS:
|
|
2754
|
+
if (!this.mp4track.sps) {
|
|
2755
|
+
this.parseSPS(unit.getPayload());
|
|
2756
|
+
}
|
|
2757
|
+
push = true;
|
|
2758
|
+
break;
|
|
2759
|
+
case NALU265.PPS:
|
|
2760
|
+
if (!this.mp4track.pps) {
|
|
2761
|
+
this.parsePPS(unit.getPayload());
|
|
2762
|
+
}
|
|
2763
|
+
push = true;
|
|
2764
|
+
break;
|
|
2765
|
+
case NALU265.AUD:
|
|
2766
|
+
log('AUD - ignoing');
|
|
2767
|
+
break;
|
|
2768
|
+
case NALU265.SEI:
|
|
2769
|
+
case NALU265.SEI2:
|
|
2770
|
+
log('SEI - ignoing');
|
|
2771
|
+
break;
|
|
2772
|
+
}
|
|
2773
|
+
if (!this.readyToDecode && this.mp4track.vps && this.mp4track.sps && this.mp4track.pps) {
|
|
2774
|
+
this.readyToDecode = true;
|
|
2775
|
+
}
|
|
2776
|
+
return push;
|
|
2777
|
+
}
|
|
2778
|
+
}]);
|
|
2779
|
+
return H265Remuxer;
|
|
2780
|
+
}(BaseRemuxer);
|
|
2781
|
+
|
|
2782
|
+
var RemuxController = /*#__PURE__*/function (_Event) {
|
|
2783
|
+
_inherits(RemuxController, _Event);
|
|
2784
|
+
var _super = _createSuper(RemuxController);
|
|
2785
|
+
function RemuxController(env, live, videoCodec, frameDuration) {
|
|
2786
|
+
var _this;
|
|
2787
|
+
_classCallCheck(this, RemuxController);
|
|
2788
|
+
_this = _super.call(this, 'remuxer');
|
|
2789
|
+
_this.videoCodec = videoCodec;
|
|
2790
|
+
_this.frameDuration = frameDuration;
|
|
2791
|
+
_this.initialized = false;
|
|
2792
|
+
_this.tracks = {};
|
|
2793
|
+
_this.seq = 1;
|
|
2794
|
+
_this.env = env;
|
|
2795
|
+
_this.timescale = 1000;
|
|
2796
|
+
_this.mediaDuration = live ? 0xffffffff : 0;
|
|
2797
|
+
return _this;
|
|
2798
|
+
}
|
|
2799
|
+
_createClass(RemuxController, [{
|
|
2800
|
+
key: "addTrack",
|
|
2801
|
+
value: function addTrack(type) {
|
|
2802
|
+
var _this2 = this;
|
|
2803
|
+
if (type === 'video' || type === 'both') {
|
|
2804
|
+
if (this.videoCodec == 'H265') {
|
|
2805
|
+
this.tracks.video = new H265Remuxer(this.timescale, this.mediaDuration, this.frameDuration);
|
|
2806
|
+
} else {
|
|
2807
|
+
this.tracks.video = new H264Remuxer(this.timescale, this.mediaDuration, this.frameDuration);
|
|
2808
|
+
}
|
|
2809
|
+
this.tracks.video.on('outOfData', function () {
|
|
2810
|
+
_this2.dispatch('missingVideoFrames');
|
|
2811
|
+
});
|
|
2812
|
+
this.tracks.video.on('keyframePosition', function (time) {
|
|
2813
|
+
_this2.dispatch('keyframePosition', time);
|
|
2814
|
+
});
|
|
2815
|
+
}
|
|
2816
|
+
if (type === 'audio' || type === 'both') {
|
|
2817
|
+
var aacRemuxer = new AACRemuxer(this.timescale, this.mediaDuration, this.frameDuration);
|
|
2818
|
+
this.tracks.audio = aacRemuxer;
|
|
2819
|
+
this.tracks.video.on('outOfData', function () {
|
|
2820
|
+
_this2.dispatch('missingAudioFrames');
|
|
2821
|
+
});
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
}, {
|
|
2825
|
+
key: "reset",
|
|
2826
|
+
value: function reset() {
|
|
2827
|
+
for (var type in this.tracks) {
|
|
2828
|
+
this.tracks[type].resetTrack();
|
|
2829
|
+
}
|
|
2830
|
+
this.initialized = false;
|
|
2831
|
+
}
|
|
2832
|
+
}, {
|
|
2833
|
+
key: "destroy",
|
|
2834
|
+
value: function destroy() {
|
|
2835
|
+
this.tracks = {};
|
|
2836
|
+
this.offAll();
|
|
2837
|
+
}
|
|
2838
|
+
}, {
|
|
2839
|
+
key: "flush",
|
|
2840
|
+
value: function flush() {
|
|
2841
|
+
if (!this.initialized) {
|
|
2842
|
+
if (!this.isReady()) return;
|
|
2843
|
+
this.dispatch('ready');
|
|
2844
|
+
this.initSegment();
|
|
2845
|
+
this.initialized = true;
|
|
2846
|
+
}
|
|
2847
|
+
for (var type in this.tracks) {
|
|
2848
|
+
var track = this.tracks[type];
|
|
2849
|
+
var pay = track.getPayload();
|
|
2850
|
+
if (pay && pay.byteLength) {
|
|
2851
|
+
var moof = MP4.moof(this.seq, track.dts, track.mp4track);
|
|
2852
|
+
var mdat = MP4.mdat(pay);
|
|
2853
|
+
var payload = appendByteArray(moof, mdat);
|
|
2854
|
+
var data = {
|
|
2855
|
+
type: type,
|
|
2856
|
+
payload: payload,
|
|
2857
|
+
dts: track.dts
|
|
2858
|
+
};
|
|
2859
|
+
if (type === 'video') {
|
|
2860
|
+
data.fps = track.mp4track.fps;
|
|
2861
|
+
}
|
|
2862
|
+
this.dispatch('buffer', data);
|
|
2863
|
+
var duration = secToTime(track.dts / this.timescale);
|
|
2864
|
+
log("put segment (".concat(type, "): dts: ").concat(track.dts, " frames: ").concat(track.mp4track.samples.length, " second: ").concat(duration));
|
|
2865
|
+
track.flush();
|
|
2866
|
+
this.seq++;
|
|
2867
|
+
}
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
}, {
|
|
2871
|
+
key: "initSegment",
|
|
2872
|
+
value: function initSegment() {
|
|
2873
|
+
var tracks = [];
|
|
2874
|
+
for (var type in this.tracks) {
|
|
2875
|
+
var track = this.tracks[type];
|
|
2876
|
+
if (this.env == 'browser') {
|
|
2877
|
+
var data = {
|
|
2878
|
+
type: type,
|
|
2879
|
+
payload: MP4.initSegment([track.mp4track], this.mediaDuration, this.timescale)
|
|
2880
|
+
};
|
|
2881
|
+
this.dispatch('buffer', data);
|
|
2882
|
+
} else {
|
|
2883
|
+
tracks.push(track.mp4track);
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
if (this.env == 'node') {
|
|
2887
|
+
var _data = {
|
|
2888
|
+
type: 'all',
|
|
2889
|
+
payload: MP4.initSegment(tracks, this.mediaDuration, this.timescale)
|
|
2890
|
+
};
|
|
2891
|
+
this.dispatch('buffer', _data);
|
|
2892
|
+
}
|
|
2893
|
+
log('Initial segment generated.');
|
|
2894
|
+
}
|
|
2895
|
+
}, {
|
|
2896
|
+
key: "isReady",
|
|
2897
|
+
value: function isReady() {
|
|
2898
|
+
for (var type in this.tracks) {
|
|
2899
|
+
if (!this.tracks[type].readyToDecode || !this.tracks[type].samples.length) return false;
|
|
2900
|
+
}
|
|
2901
|
+
return true;
|
|
2902
|
+
}
|
|
2903
|
+
}, {
|
|
2904
|
+
key: "feed",
|
|
2905
|
+
value: function feed(data) {
|
|
2906
|
+
var remux = false;
|
|
2907
|
+
if (data.video && this.tracks.video) {
|
|
2908
|
+
remux |= this.tracks.video.feed(data.video, data.duration, data.compositionTimeOffset);
|
|
2909
|
+
}
|
|
2910
|
+
if (data.audio && this.tracks.audio) {
|
|
2911
|
+
remux |= this.tracks.audio.feed(data.audio, data.duration);
|
|
2912
|
+
}
|
|
2913
|
+
if (!remux) {
|
|
2914
|
+
error('Input object must have video and/or audio property. Make sure it is a valid typed array');
|
|
2915
|
+
return;
|
|
2916
|
+
}
|
|
2917
|
+
this.flush();
|
|
2918
|
+
}
|
|
2919
|
+
}]);
|
|
2920
|
+
return RemuxController;
|
|
2921
|
+
}(Event);
|
|
2922
|
+
|
|
2923
|
+
var BufferController = /*#__PURE__*/function (_Event) {
|
|
2924
|
+
_inherits(BufferController, _Event);
|
|
2925
|
+
var _super = _createSuper(BufferController);
|
|
2926
|
+
function BufferController(sourceBuffer, type) {
|
|
2927
|
+
var _this;
|
|
2928
|
+
_classCallCheck(this, BufferController);
|
|
2929
|
+
_this = _super.call(this, 'buffer');
|
|
2930
|
+
_this.type = type;
|
|
2931
|
+
_this.queue = new Uint8Array();
|
|
2932
|
+
_this.cleaning = false;
|
|
2933
|
+
_this.pendingCleaning = 0;
|
|
2934
|
+
_this.cleanOffset = 30;
|
|
2935
|
+
_this.cleanRanges = [];
|
|
2936
|
+
_this.sourceBuffer = sourceBuffer;
|
|
2937
|
+
_this.sourceBuffer.addEventListener('updateend', function () {
|
|
2938
|
+
if (_this.pendingCleaning > 0) {
|
|
2939
|
+
_this.initCleanup(_this.pendingCleaning);
|
|
2940
|
+
_this.pendingCleaning = 0;
|
|
2941
|
+
}
|
|
2942
|
+
_this.cleaning = false;
|
|
2943
|
+
if (_this.cleanRanges.length) {
|
|
2944
|
+
_this.doCleanup();
|
|
2945
|
+
return;
|
|
2946
|
+
}
|
|
2947
|
+
});
|
|
2948
|
+
_this.sourceBuffer.addEventListener('error', function () {
|
|
2949
|
+
_this.dispatch('error', {
|
|
2950
|
+
type: _this.type,
|
|
2951
|
+
name: 'buffer',
|
|
2952
|
+
error: 'buffer error'
|
|
2953
|
+
});
|
|
2954
|
+
});
|
|
2955
|
+
return _this;
|
|
2956
|
+
}
|
|
2957
|
+
_createClass(BufferController, [{
|
|
2958
|
+
key: "destroy",
|
|
2959
|
+
value: function destroy() {
|
|
2960
|
+
this.queue = null;
|
|
2961
|
+
this.sourceBuffer = null;
|
|
2962
|
+
this.offAll();
|
|
2963
|
+
}
|
|
2964
|
+
}, {
|
|
2965
|
+
key: "doCleanup",
|
|
2966
|
+
value: function doCleanup() {
|
|
2967
|
+
if (!this.cleanRanges.length) {
|
|
2968
|
+
this.cleaning = false;
|
|
2969
|
+
return;
|
|
2970
|
+
}
|
|
2971
|
+
var range = this.cleanRanges.shift();
|
|
2972
|
+
log("".concat(this.type, " remove range [").concat(range[0], " - ").concat(range[1], ")"));
|
|
2973
|
+
this.cleaning = true;
|
|
2974
|
+
this.sourceBuffer.remove(range[0], range[1]);
|
|
2975
|
+
}
|
|
2976
|
+
}, {
|
|
2977
|
+
key: "initCleanup",
|
|
2978
|
+
value: function initCleanup(cleanMaxLimit) {
|
|
2979
|
+
try {
|
|
2980
|
+
if (this.sourceBuffer.updating) {
|
|
2981
|
+
this.pendingCleaning = cleanMaxLimit;
|
|
2982
|
+
return;
|
|
2983
|
+
}
|
|
2984
|
+
if (this.sourceBuffer.buffered && this.sourceBuffer.buffered.length && !this.cleaning) {
|
|
2985
|
+
for (var i = 0; i < this.sourceBuffer.buffered.length; ++i) {
|
|
2986
|
+
var start = this.sourceBuffer.buffered.start(i);
|
|
2987
|
+
var end = this.sourceBuffer.buffered.end(i);
|
|
2988
|
+
if (cleanMaxLimit - start > this.cleanOffset) {
|
|
2989
|
+
end = cleanMaxLimit - this.cleanOffset;
|
|
2990
|
+
if (start < end) {
|
|
2991
|
+
this.cleanRanges.push([start, end]);
|
|
2992
|
+
}
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
this.doCleanup();
|
|
2996
|
+
}
|
|
2997
|
+
} catch (e) {
|
|
2998
|
+
error("Error occured while cleaning ".concat(this.type, " buffer - ").concat(e.name, ": ").concat(e.message));
|
|
2999
|
+
}
|
|
3000
|
+
}
|
|
3001
|
+
}, {
|
|
3002
|
+
key: "doAppend",
|
|
3003
|
+
value: function doAppend() {
|
|
3004
|
+
if (!this.queue.length) return;
|
|
3005
|
+
if (!this.sourceBuffer || this.sourceBuffer.updating) return;
|
|
3006
|
+
try {
|
|
3007
|
+
this.sourceBuffer.appendBuffer(this.queue);
|
|
3008
|
+
this.queue = new Uint8Array();
|
|
3009
|
+
} catch (e) {
|
|
3010
|
+
var name = 'unexpectedError';
|
|
3011
|
+
if (e.name === 'QuotaExceededError') {
|
|
3012
|
+
log("".concat(this.type, " buffer quota full"));
|
|
3013
|
+
name = 'QuotaExceeded';
|
|
3014
|
+
} else {
|
|
3015
|
+
error("Error occured while appending ".concat(this.type, " buffer - ").concat(e.name, ": ").concat(e.message));
|
|
3016
|
+
name = 'InvalidStateError';
|
|
3017
|
+
}
|
|
3018
|
+
this.dispatch('error', {
|
|
3019
|
+
type: this.type,
|
|
3020
|
+
name: name,
|
|
3021
|
+
error: 'buffer error'
|
|
3022
|
+
});
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
}, {
|
|
3026
|
+
key: "feed",
|
|
3027
|
+
value: function feed(data) {
|
|
3028
|
+
this.queue = appendByteArray(this.queue, data);
|
|
3029
|
+
}
|
|
3030
|
+
}]);
|
|
3031
|
+
return BufferController;
|
|
3032
|
+
}(Event);
|
|
3033
|
+
|
|
3034
|
+
var JMuxer = /*#__PURE__*/function (_Event) {
|
|
3035
|
+
_inherits(JMuxer, _Event);
|
|
3036
|
+
var _super = _createSuper(JMuxer);
|
|
3037
|
+
function JMuxer(options) {
|
|
3038
|
+
var _this;
|
|
3039
|
+
_classCallCheck(this, JMuxer);
|
|
3040
|
+
_this = _super.call(this, 'jmuxer');
|
|
3041
|
+
_this.isReset = false;
|
|
3042
|
+
var defaults = {
|
|
3043
|
+
node: '',
|
|
3044
|
+
mode: 'both',
|
|
3045
|
+
// both, audio, video
|
|
3046
|
+
videoCodec: 'H264',
|
|
3047
|
+
// H264, H265
|
|
3048
|
+
flushingTime: 500,
|
|
3049
|
+
maxDelay: 500,
|
|
3050
|
+
clearBuffer: true,
|
|
3051
|
+
fps: 30,
|
|
3052
|
+
readFpsFromTrack: false,
|
|
3053
|
+
// set true to fetch fps value from NALu
|
|
3054
|
+
debug: false,
|
|
3055
|
+
onReady: function onReady() {},
|
|
3056
|
+
// function called when MSE is ready to accept frames
|
|
3057
|
+
onData: function onData() {},
|
|
3058
|
+
// function called when data is ready to be sent
|
|
3059
|
+
onError: function onError() {},
|
|
3060
|
+
// function called when jmuxer encounters any buffer related errors
|
|
3061
|
+
onUnsupportedCodec: function onUnsupportedCodec() {},
|
|
3062
|
+
// function called when a codec is not supported by the browser
|
|
3063
|
+
onMissingVideoFrames: function onMissingVideoFrames() {},
|
|
3064
|
+
// function called when jmuxer encounters any missing video frames
|
|
3065
|
+
onMissingAudioFrames: function onMissingAudioFrames() {},
|
|
3066
|
+
// function called when jmuxer encounters any missing audio frames
|
|
3067
|
+
onKeyframePosition: function onKeyframePosition() {},
|
|
3068
|
+
// function called when a keyframe is detected thus the provided time is seekable
|
|
3069
|
+
onLoggerLog: console.log,
|
|
3070
|
+
onLoggerErr: console.error
|
|
3071
|
+
};
|
|
3072
|
+
_this.options = Object.assign({}, defaults, options);
|
|
3073
|
+
_this.env = (typeof process === "undefined" ? "undefined" : _typeof(process)) === 'object' && typeof window === 'undefined' ? 'node' : 'browser';
|
|
3074
|
+
if (_this.options.debug) {
|
|
3075
|
+
setLogger(_this.options.onLoggerLog, _this.options.onLoggerErr);
|
|
3076
|
+
}
|
|
3077
|
+
if (!_this.options.fps) {
|
|
3078
|
+
_this.options.fps = 30;
|
|
3079
|
+
}
|
|
3080
|
+
_this.frameDuration = 1000 / _this.options.fps | 0;
|
|
3081
|
+
_this.remuxController = new RemuxController(_this.env, options.live, _this.options.videoCodec, _this.frameDuration);
|
|
3082
|
+
_this.remuxController.addTrack(_this.options.mode);
|
|
3083
|
+
_this.initData();
|
|
3084
|
+
|
|
3085
|
+
/* events callback */
|
|
3086
|
+
_this.remuxController.on('buffer', _this.onBuffer.bind(_assertThisInitialized(_this)));
|
|
3087
|
+
if (_this.env == 'browser') {
|
|
3088
|
+
_this.remuxController.on('ready', _this.createBuffer.bind(_assertThisInitialized(_this)));
|
|
3089
|
+
_this.initBrowser();
|
|
3090
|
+
}
|
|
3091
|
+
_this.remuxController.on('missingVideoFrames', function () {
|
|
3092
|
+
if (typeof _this.options.onMissingVideoFrames === 'function') {
|
|
3093
|
+
_this.options.onMissingVideoFrames.call(null);
|
|
3094
|
+
}
|
|
3095
|
+
});
|
|
3096
|
+
_this.remuxController.on('missingAudioFrames', function () {
|
|
3097
|
+
if (typeof _this.options.onMissingAudioFrames === 'function') {
|
|
3098
|
+
_this.options.onMissingAudioFrames.call(null);
|
|
3099
|
+
}
|
|
3100
|
+
});
|
|
3101
|
+
if (_this.clearBuffer) {
|
|
3102
|
+
// this is used to know when keyframes are,
|
|
3103
|
+
// to essentially know which specific times are seekable
|
|
3104
|
+
_this.remuxController.on('keyframePosition', function (time) {
|
|
3105
|
+
_this.kfPosition.push(time);
|
|
3106
|
+
});
|
|
3107
|
+
}
|
|
3108
|
+
if (typeof _this.options.onKeyframePosition === 'function') {
|
|
3109
|
+
_this.remuxController.on('keyframePosition', function (time) {
|
|
3110
|
+
_this.options.onKeyframePosition.call(null, time);
|
|
3111
|
+
});
|
|
3112
|
+
}
|
|
3113
|
+
return _this;
|
|
3114
|
+
}
|
|
3115
|
+
_createClass(JMuxer, [{
|
|
3116
|
+
key: "initData",
|
|
3117
|
+
value: function initData() {
|
|
3118
|
+
this.lastCleaningTime = Date.now();
|
|
3119
|
+
this.kfPosition = [];
|
|
3120
|
+
this.pendingUnits = {};
|
|
3121
|
+
this.remainingData = new Uint8Array();
|
|
3122
|
+
this.startInterval();
|
|
3123
|
+
}
|
|
3124
|
+
}, {
|
|
3125
|
+
key: "initBrowser",
|
|
3126
|
+
value: function initBrowser() {
|
|
3127
|
+
if (typeof this.options.node === 'string' && this.options.node == '') {
|
|
3128
|
+
error('no video element were found to render, provide a valid video element');
|
|
3129
|
+
}
|
|
3130
|
+
this.node = typeof this.options.node === 'string' ? document.getElementById(this.options.node) : this.options.node;
|
|
3131
|
+
this.mseReady = false;
|
|
3132
|
+
this.setupMSE();
|
|
3133
|
+
}
|
|
3134
|
+
}, {
|
|
3135
|
+
key: "createStream",
|
|
3136
|
+
value: function createStream() {
|
|
3137
|
+
var feed = this.feed.bind(this);
|
|
3138
|
+
var destroy = this.destroy.bind(this);
|
|
3139
|
+
this.stream = new stream.Duplex({
|
|
3140
|
+
writableObjectMode: true,
|
|
3141
|
+
read: function read(size) {},
|
|
3142
|
+
write: function write(data, encoding, callback) {
|
|
3143
|
+
feed(data);
|
|
3144
|
+
callback();
|
|
3145
|
+
},
|
|
3146
|
+
"final": function final(callback) {
|
|
3147
|
+
destroy();
|
|
3148
|
+
callback();
|
|
3149
|
+
}
|
|
3150
|
+
});
|
|
3151
|
+
return this.stream;
|
|
3152
|
+
}
|
|
3153
|
+
}, {
|
|
3154
|
+
key: "setupMSE",
|
|
3155
|
+
value: function setupMSE() {
|
|
3156
|
+
window.MediaSource = window.MediaSource || window.WebKitMediaSource || window.ManagedMediaSource;
|
|
3157
|
+
if (!window.MediaSource) {
|
|
3158
|
+
throw 'Oops! Browser does not support Media Source Extension or Managed Media Source (IOS 17+).';
|
|
3159
|
+
}
|
|
3160
|
+
this.isMSESupported = !!window.MediaSource;
|
|
3161
|
+
this.mediaSource = new window.MediaSource();
|
|
3162
|
+
this.url = URL.createObjectURL(this.mediaSource);
|
|
3163
|
+
if (window.MediaSource === window.ManagedMediaSource) {
|
|
3164
|
+
try {
|
|
3165
|
+
this.node.removeAttribute('src');
|
|
3166
|
+
// ManagedMediaSource will not open without disableRemotePlayback set to false or source alternatives
|
|
3167
|
+
this.node.disableRemotePlayback = true;
|
|
3168
|
+
var source = document.createElement('source');
|
|
3169
|
+
source.type = 'video/mp4';
|
|
3170
|
+
source.src = this.url;
|
|
3171
|
+
this.node.appendChild(source);
|
|
3172
|
+
this.node.load();
|
|
3173
|
+
} catch (error) {
|
|
3174
|
+
this.node.src = this.url;
|
|
3175
|
+
}
|
|
3176
|
+
} else {
|
|
3177
|
+
this.node.src = this.url;
|
|
3178
|
+
}
|
|
3179
|
+
this.mseEnded = false;
|
|
3180
|
+
this.mediaSource.addEventListener('sourceopen', this.onMSEOpen.bind(this));
|
|
3181
|
+
this.mediaSource.addEventListener('sourceclose', this.onMSEClose.bind(this));
|
|
3182
|
+
this.mediaSource.addEventListener('webkitsourceopen', this.onMSEOpen.bind(this));
|
|
3183
|
+
this.mediaSource.addEventListener('webkitsourceclose', this.onMSEClose.bind(this));
|
|
3184
|
+
}
|
|
3185
|
+
}, {
|
|
3186
|
+
key: "endMSE",
|
|
3187
|
+
value: function endMSE() {
|
|
3188
|
+
if (!this.mseEnded) {
|
|
3189
|
+
try {
|
|
3190
|
+
this.mseEnded = true;
|
|
3191
|
+
this.mediaSource.endOfStream();
|
|
3192
|
+
} catch (e) {
|
|
3193
|
+
error('mediasource is not available to end');
|
|
3194
|
+
}
|
|
3195
|
+
}
|
|
3196
|
+
}
|
|
3197
|
+
}, {
|
|
3198
|
+
key: "feed",
|
|
3199
|
+
value: function feed(data) {
|
|
3200
|
+
if (!data || !this.remuxController) return;
|
|
3201
|
+
data.duration = data.duration ? parseInt(data.duration) : 0;
|
|
3202
|
+
this.remuxController.feed(data);
|
|
3203
|
+
}
|
|
3204
|
+
}, {
|
|
3205
|
+
key: "destroy",
|
|
3206
|
+
value: function destroy() {
|
|
3207
|
+
this.stopInterval();
|
|
3208
|
+
if (this.stream) {
|
|
3209
|
+
this.remuxController.flush();
|
|
3210
|
+
this.stream.push(null);
|
|
3211
|
+
this.stream = null;
|
|
3212
|
+
}
|
|
3213
|
+
if (this.remuxController) {
|
|
3214
|
+
this.remuxController.destroy();
|
|
3215
|
+
this.remuxController = null;
|
|
3216
|
+
}
|
|
3217
|
+
if (this.bufferControllers) {
|
|
3218
|
+
for (var type in this.bufferControllers) {
|
|
3219
|
+
this.bufferControllers[type].destroy();
|
|
3220
|
+
}
|
|
3221
|
+
this.bufferControllers = null;
|
|
3222
|
+
this.endMSE();
|
|
3223
|
+
}
|
|
3224
|
+
this.node = false;
|
|
3225
|
+
this.mseReady = false;
|
|
3226
|
+
this.videoStarted = false;
|
|
3227
|
+
this.mediaSource = null;
|
|
3228
|
+
}
|
|
3229
|
+
}, {
|
|
3230
|
+
key: "reset",
|
|
3231
|
+
value: function reset() {
|
|
3232
|
+
this.stopInterval();
|
|
3233
|
+
this.isReset = true;
|
|
3234
|
+
this.node.pause();
|
|
3235
|
+
if (this.remuxController) {
|
|
3236
|
+
this.remuxController.reset();
|
|
3237
|
+
}
|
|
3238
|
+
if (this.bufferControllers) {
|
|
3239
|
+
for (var type in this.bufferControllers) {
|
|
3240
|
+
this.bufferControllers[type].destroy();
|
|
3241
|
+
}
|
|
3242
|
+
this.bufferControllers = null;
|
|
3243
|
+
this.endMSE();
|
|
3244
|
+
}
|
|
3245
|
+
this.initData();
|
|
3246
|
+
if (this.env == 'browser') {
|
|
3247
|
+
this.initBrowser();
|
|
3248
|
+
}
|
|
3249
|
+
log('JMuxer was reset');
|
|
3250
|
+
}
|
|
3251
|
+
}, {
|
|
3252
|
+
key: "createBuffer",
|
|
3253
|
+
value: function createBuffer() {
|
|
3254
|
+
if (!this.mseReady || !this.remuxController || !this.remuxController.isReady() || this.bufferControllers) return;
|
|
3255
|
+
this.bufferControllers = {};
|
|
3256
|
+
for (var type in this.remuxController.tracks) {
|
|
3257
|
+
var track = this.remuxController.tracks[type];
|
|
3258
|
+
if (!JMuxer.isSupported("".concat(type, "/mp4; codecs=\"").concat(track.mp4track.codec, "\""))) {
|
|
3259
|
+
error("Browser does not support codec: ".concat(type, "/mp4; codecs=\"").concat(track.mp4track.codec, "\""));
|
|
3260
|
+
if (typeof this.options.onUnsupportedCodec === 'function') {
|
|
3261
|
+
this.options.onUnsupportedCodec.call(null, track.mp4track.codec);
|
|
3262
|
+
}
|
|
3263
|
+
return false;
|
|
3264
|
+
}
|
|
3265
|
+
var sb = this.mediaSource.addSourceBuffer("".concat(type, "/mp4; codecs=\"").concat(track.mp4track.codec, "\""));
|
|
3266
|
+
this.bufferControllers[type] = new BufferController(sb, type);
|
|
3267
|
+
this.bufferControllers[type].on('error', this.onBufferError.bind(this));
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
}, {
|
|
3271
|
+
key: "startInterval",
|
|
3272
|
+
value: function startInterval() {
|
|
3273
|
+
var _this2 = this;
|
|
3274
|
+
this.interval = setInterval(function () {
|
|
3275
|
+
if (_this2.options.flushingTime) {
|
|
3276
|
+
_this2.applyAndClearBuffer();
|
|
3277
|
+
} else if (_this2.bufferControllers) {
|
|
3278
|
+
_this2.cancelDelay();
|
|
3279
|
+
}
|
|
3280
|
+
}, this.options.flushingTime || 1000);
|
|
3281
|
+
}
|
|
3282
|
+
}, {
|
|
3283
|
+
key: "stopInterval",
|
|
3284
|
+
value: function stopInterval() {
|
|
3285
|
+
if (this.interval) {
|
|
3286
|
+
clearInterval(this.interval);
|
|
3287
|
+
}
|
|
3288
|
+
}
|
|
3289
|
+
}, {
|
|
3290
|
+
key: "cancelDelay",
|
|
3291
|
+
value: function cancelDelay() {
|
|
3292
|
+
if (this.node.buffered && this.node.buffered.length > 0 && !this.node.seeking) {
|
|
3293
|
+
var end = this.node.buffered.end(0);
|
|
3294
|
+
if (end - this.node.currentTime > this.options.maxDelay / 1000) {
|
|
3295
|
+
log('delay');
|
|
3296
|
+
if (this.node.paused) this.node.play()["catch"](error);
|
|
3297
|
+
this.node.currentTime = end - 0.001;
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
}
|
|
3301
|
+
}, {
|
|
3302
|
+
key: "releaseBuffer",
|
|
3303
|
+
value: function releaseBuffer() {
|
|
3304
|
+
for (var type in this.bufferControllers) {
|
|
3305
|
+
this.bufferControllers[type].doAppend();
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
}, {
|
|
3309
|
+
key: "applyAndClearBuffer",
|
|
3310
|
+
value: function applyAndClearBuffer() {
|
|
3311
|
+
if (this.bufferControllers) {
|
|
3312
|
+
this.releaseBuffer();
|
|
3313
|
+
this.clearBuffer();
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
}, {
|
|
3317
|
+
key: "getSafeClearOffsetOfBuffer",
|
|
3318
|
+
value: function getSafeClearOffsetOfBuffer(offset) {
|
|
3319
|
+
var maxLimit = this.options.mode === 'audio' && offset || 0,
|
|
3320
|
+
adjacentOffset;
|
|
3321
|
+
for (var i = 0; i < this.kfPosition.length; i++) {
|
|
3322
|
+
if (this.kfPosition[i] >= offset) {
|
|
3323
|
+
break;
|
|
3324
|
+
}
|
|
3325
|
+
adjacentOffset = this.kfPosition[i];
|
|
3326
|
+
}
|
|
3327
|
+
if (adjacentOffset) {
|
|
3328
|
+
this.kfPosition = this.kfPosition.filter(function (kfDelimiter) {
|
|
3329
|
+
if (kfDelimiter < adjacentOffset) {
|
|
3330
|
+
maxLimit = kfDelimiter;
|
|
3331
|
+
}
|
|
3332
|
+
return kfDelimiter >= adjacentOffset;
|
|
3333
|
+
});
|
|
3334
|
+
}
|
|
3335
|
+
return maxLimit;
|
|
3336
|
+
}
|
|
3337
|
+
}, {
|
|
3338
|
+
key: "clearBuffer",
|
|
3339
|
+
value: function clearBuffer() {
|
|
3340
|
+
if (this.options.clearBuffer && Date.now() - this.lastCleaningTime > 10000) {
|
|
3341
|
+
for (var type in this.bufferControllers) {
|
|
3342
|
+
var cleanMaxLimit = this.getSafeClearOffsetOfBuffer(this.node.currentTime);
|
|
3343
|
+
this.bufferControllers[type].initCleanup(cleanMaxLimit);
|
|
3344
|
+
}
|
|
3345
|
+
this.lastCleaningTime = Date.now();
|
|
3346
|
+
}
|
|
3347
|
+
}
|
|
3348
|
+
}, {
|
|
3349
|
+
key: "onBuffer",
|
|
3350
|
+
value: function onBuffer(data) {
|
|
3351
|
+
if (this.options.readFpsFromTrack && typeof data.fps !== 'undefined' && this.options.fps != data.fps) {
|
|
3352
|
+
this.options.fps = data.fps;
|
|
3353
|
+
this.frameDuration = Math.ceil(1000 / data.fps);
|
|
3354
|
+
log("JMuxer changed FPS to ".concat(data.fps, " from track data"));
|
|
3355
|
+
}
|
|
3356
|
+
if (this.env == 'browser') {
|
|
3357
|
+
if (this.bufferControllers && this.bufferControllers[data.type]) {
|
|
3358
|
+
this.bufferControllers[data.type].feed(data.payload);
|
|
3359
|
+
}
|
|
3360
|
+
} else if (this.stream) {
|
|
3361
|
+
this.stream.push(data.payload);
|
|
3362
|
+
}
|
|
3363
|
+
if (this.options.onData) {
|
|
3364
|
+
this.options.onData(data.payload);
|
|
3365
|
+
}
|
|
3366
|
+
if (this.options.flushingTime === 0) {
|
|
3367
|
+
this.applyAndClearBuffer();
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
|
|
3371
|
+
/* Events on MSE */
|
|
3372
|
+
}, {
|
|
3373
|
+
key: "onMSEOpen",
|
|
3374
|
+
value: function onMSEOpen() {
|
|
3375
|
+
this.mseReady = true;
|
|
3376
|
+
URL.revokeObjectURL(this.url);
|
|
3377
|
+
// this.createBuffer();
|
|
3378
|
+
if (typeof this.options.onReady === 'function') {
|
|
3379
|
+
this.options.onReady.call(null, this.isReset);
|
|
3380
|
+
}
|
|
3381
|
+
}
|
|
3382
|
+
}, {
|
|
3383
|
+
key: "onMSEClose",
|
|
3384
|
+
value: function onMSEClose() {
|
|
3385
|
+
this.mseReady = false;
|
|
3386
|
+
this.videoStarted = false;
|
|
3387
|
+
}
|
|
3388
|
+
}, {
|
|
3389
|
+
key: "onBufferError",
|
|
3390
|
+
value: function onBufferError(data) {
|
|
3391
|
+
if (data.name == 'QuotaExceeded') {
|
|
3392
|
+
log("JMuxer cleaning ".concat(data.type, " buffer due to QuotaExceeded error"));
|
|
3393
|
+
this.bufferControllers[data.type].initCleanup(this.node.currentTime);
|
|
3394
|
+
return;
|
|
3395
|
+
} else if (data.name == 'InvalidStateError') {
|
|
3396
|
+
log('JMuxer is reseting due to InvalidStateError');
|
|
3397
|
+
this.reset();
|
|
3398
|
+
} else {
|
|
3399
|
+
this.endMSE();
|
|
3400
|
+
}
|
|
3401
|
+
if (typeof this.options.onError === 'function') {
|
|
3402
|
+
this.options.onError.call(null, data);
|
|
3403
|
+
}
|
|
3404
|
+
}
|
|
3405
|
+
}], [{
|
|
3406
|
+
key: "isSupported",
|
|
3407
|
+
value: function isSupported(codec) {
|
|
3408
|
+
return window.MediaSource && window.MediaSource.isTypeSupported(codec);
|
|
3409
|
+
}
|
|
3410
|
+
}]);
|
|
3411
|
+
return JMuxer;
|
|
3412
|
+
}(Event);
|
|
3413
|
+
|
|
3414
|
+
return JMuxer;
|
|
3415
|
+
|
|
3416
|
+
}));
|