12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564 |
- var JMuxer = (function () {
- 'use strict';
- function _classCallCheck(instance, Constructor) {
- if (!(instance instanceof Constructor)) {
- throw new TypeError("Cannot call a class as a function");
- }
- }
- function _defineProperties(target, props) {
- for (var i = 0; i < props.length; i++) {
- var descriptor = props[i];
- descriptor.enumerable = descriptor.enumerable || false;
- descriptor.configurable = true;
- if ("value" in descriptor) descriptor.writable = true;
- Object.defineProperty(target, descriptor.key, descriptor);
- }
- }
- function _createClass(Constructor, protoProps, staticProps) {
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
- if (staticProps) _defineProperties(Constructor, staticProps);
- return Constructor;
- }
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
- function _inherits(subClass, superClass) {
- if (typeof superClass !== "function" && superClass !== null) {
- throw new TypeError("Super expression must either be null or a function");
- }
- subClass.prototype = Object.create(superClass && superClass.prototype, {
- constructor: {
- value: subClass,
- writable: true,
- configurable: true
- }
- });
- if (superClass) _setPrototypeOf(subClass, superClass);
- }
- function _getPrototypeOf(o) {
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
- return o.__proto__ || Object.getPrototypeOf(o);
- };
- return _getPrototypeOf(o);
- }
- function _setPrototypeOf(o, p) {
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
- o.__proto__ = p;
- return o;
- };
- return _setPrototypeOf(o, p);
- }
- function _isNativeReflectConstruct() {
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
- if (Reflect.construct.sham) return false;
- if (typeof Proxy === "function") return true;
- try {
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
- return true;
- } catch (e) {
- return false;
- }
- }
- function _assertThisInitialized(self) {
- if (self === void 0) {
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
- }
- return self;
- }
- function _possibleConstructorReturn(self, call) {
- if (call && (typeof call === "object" || typeof call === "function")) {
- return call;
- }
- return _assertThisInitialized(self);
- }
- function _createSuper(Derived) {
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
- return function _createSuperInternal() {
- var Super = _getPrototypeOf(Derived),
- result;
- if (hasNativeReflectConstruct) {
- var NewTarget = _getPrototypeOf(this).constructor;
- result = Reflect.construct(Super, arguments, NewTarget);
- } else {
- result = Super.apply(this, arguments);
- }
- return _possibleConstructorReturn(this, result);
- };
- }
- function _unsupportedIterableToArray(o, minLen) {
- if (!o) return;
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
- var n = Object.prototype.toString.call(o).slice(8, -1);
- if (n === "Object" && o.constructor) n = o.constructor.name;
- if (n === "Map" || n === "Set") return Array.from(o);
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
- }
- function _arrayLikeToArray(arr, len) {
- if (len == null || len > arr.length) len = arr.length;
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
- return arr2;
- }
- function _createForOfIteratorHelper(o, allowArrayLike) {
- var it;
- if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
- if (it) o = it;
- var i = 0;
- var F = function () {};
- return {
- s: F,
- n: function () {
- if (i >= o.length) return {
- done: true
- };
- return {
- done: false,
- value: o[i++]
- };
- },
- e: function (e) {
- throw e;
- },
- f: F
- };
- }
- 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 normalCompletion = true,
- didErr = false,
- err;
- return {
- s: function () {
- it = o[Symbol.iterator]();
- },
- n: function () {
- var step = it.next();
- normalCompletion = step.done;
- return step;
- },
- e: function (e) {
- didErr = true;
- err = e;
- },
- f: function () {
- try {
- if (!normalCompletion && it.return != null) it.return();
- } finally {
- if (didErr) throw err;
- }
- }
- };
- }
- var logger;
- var errorLogger;
- function setLogger() {
- /*eslint-disable */
- logger = console.log;
- errorLogger = console.error;
- /*eslint-enable */
- }
- function log(message) {
- if (logger) {
- for (var _len = arguments.length, optionalParams = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- optionalParams[_key - 1] = arguments[_key];
- }
- logger.apply(void 0, [message].concat(optionalParams));
- }
- }
- function error(message) {
- if (errorLogger) {
- for (var _len2 = arguments.length, optionalParams = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
- optionalParams[_key2 - 1] = arguments[_key2];
- }
- errorLogger.apply(void 0, [message].concat(optionalParams));
- }
- }
- var NALU = /*#__PURE__*/function () {
- _createClass(NALU, null, [{
- key: "type",
- value: function type(nalu) {
- if (nalu.ntype in NALU.TYPES) {
- return NALU.TYPES[nalu.ntype];
- } else {
- return 'UNKNOWN';
- }
- }
- }, {
- key: "NDR",
- get: function get() {
- return 1;
- }
- }, {
- key: "IDR",
- get: function get() {
- return 5;
- }
- }, {
- key: "SEI",
- get: function get() {
- return 6;
- }
- }, {
- key: "SPS",
- get: function get() {
- return 7;
- }
- }, {
- key: "PPS",
- get: function get() {
- return 8;
- }
- }, {
- key: "AUD",
- get: function get() {
- return 9;
- }
- }, {
- key: "TYPES",
- get: function get() {
- var _ref;
- return _ref = {}, _defineProperty(_ref, NALU.IDR, 'IDR'), _defineProperty(_ref, NALU.SEI, 'SEI'), _defineProperty(_ref, NALU.SPS, 'SPS'), _defineProperty(_ref, NALU.PPS, 'PPS'), _defineProperty(_ref, NALU.NDR, 'NDR'), _defineProperty(_ref, NALU.AUD, 'AUD'), _ref;
- }
- }]);
- function NALU(data) {
- _classCallCheck(this, NALU);
- this.payload = data;
- this.nri = (this.payload[0] & 0x60) >> 5; // nal_ref_idc
- this.ntype = this.payload[0] & 0x1f;
- this.isvcl = this.ntype == 1 || this.ntype == 5;
- this.stype = ''; // slice_type
- this.isfmb = false; // first_mb_in_slice
- }
- _createClass(NALU, [{
- key: "toString",
- value: function toString() {
- return "".concat(NALU.type(this), ": NRI: ").concat(this.getNri());
- }
- }, {
- key: "getNri",
- value: function getNri() {
- return this.nri;
- }
- }, {
- key: "type",
- value: function type() {
- return this.ntype;
- }
- }, {
- key: "isKeyframe",
- value: function isKeyframe() {
- return this.ntype === NALU.IDR || this.stype === 7;
- }
- }, {
- key: "getPayload",
- value: function getPayload() {
- return this.payload;
- }
- }, {
- key: "getPayloadSize",
- value: function getPayloadSize() {
- return this.payload.byteLength;
- }
- }, {
- key: "getSize",
- value: function getSize() {
- return 4 + this.getPayloadSize();
- }
- }, {
- key: "getData",
- value: function getData() {
- var result = new Uint8Array(this.getSize());
- var view = new DataView(result.buffer);
- view.setUint32(0, this.getSize() - 4);
- result.set(this.getPayload(), 4);
- return result;
- }
- }]);
- return NALU;
- }();
- /**
- * Parser for exponential Golomb codes, a variable-bitwidth number encoding scheme used by h264.
- */
- var ExpGolomb = /*#__PURE__*/function () {
- function ExpGolomb(data) {
- _classCallCheck(this, ExpGolomb);
- this.data = data;
- this.index = 0;
- this.bitLength = data.byteLength * 8;
- }
- _createClass(ExpGolomb, [{
- key: "skipBits",
- value: function skipBits(size) {
- // console.log(` skip bits: size=${size}, ${this.index}.`);
- if (this.bitsAvailable < size) {
- //throw new Error('no bytes available');
- return false;
- }
- this.index += size;
- }
- }, {
- key: "readBits",
- value: function readBits(size) {
- var moveIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
- // console.log(` read bits: size=${size}, ${this.index}.`);
- var result = this.getBits(size, this.index, moveIndex); // console.log(` read bits: result=${result}`);
- return result;
- }
- }, {
- key: "getBits",
- value: function getBits(size, offsetBits) {
- var moveIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
- if (this.bitsAvailable < size) {
- //throw new Error('no bytes available');
- return 0;
- }
- var offset = offsetBits % 8;
- var _byte = this.data[offsetBits / 8 | 0] & 0xff >>> offset;
- var bits = 8 - offset;
- if (bits >= size) {
- if (moveIndex) {
- this.index += size;
- }
- return _byte >> bits - size;
- } else {
- if (moveIndex) {
- this.index += bits;
- }
- var nextSize = size - bits;
- return _byte << nextSize | this.getBits(nextSize, offsetBits + bits, moveIndex);
- }
- }
- }, {
- key: "skipLZ",
- value: function skipLZ() {
- var leadingZeroCount;
- for (leadingZeroCount = 0; leadingZeroCount < this.bitLength - this.index; ++leadingZeroCount) {
- if (this.getBits(1, this.index + leadingZeroCount, false) !== 0) {
- // console.log(` skip LZ : size=${leadingZeroCount}, ${this.index}.`);
- this.index += leadingZeroCount;
- return leadingZeroCount;
- }
- }
- return leadingZeroCount;
- }
- }, {
- key: "skipUEG",
- value: function skipUEG() {
- this.skipBits(1 + this.skipLZ());
- }
- }, {
- key: "skipEG",
- value: function skipEG() {
- this.skipBits(1 + this.skipLZ());
- }
- }, {
- key: "readUEG",
- value: function readUEG() {
- var prefix = this.skipLZ();
- return this.readBits(prefix + 1) - 1;
- }
- }, {
- key: "readEG",
- value: function readEG() {
- var value = this.readUEG();
- if (0x01 & value) {
- // the number is odd if the low order bit is set
- return 1 + value >>> 1; // add 1 to make it even, and divide by 2
- } else {
- return -1 * (value >>> 1); // divide by two then make it negative
- }
- }
- }, {
- key: "readBoolean",
- value: function readBoolean() {
- return this.readBits(1) === 1;
- }
- }, {
- key: "readUByte",
- value: function readUByte() {
- var numberOfBytes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
- return this.readBits(numberOfBytes * 8);
- }
- }, {
- key: "readUShort",
- value: function readUShort() {
- return this.readBits(16);
- }
- }, {
- key: "readUInt",
- value: function readUInt() {
- return this.readBits(32);
- }
- }, {
- key: "bitsAvailable",
- get: function get() {
- return this.bitLength - this.index;
- }
- }]);
- return ExpGolomb;
- }();
- var H264Parser = /*#__PURE__*/function () {
- _createClass(H264Parser, null, [{
- key: "extractNALu",
- value: function extractNALu(buffer) {
- var i = 0,
- length = buffer.byteLength,
- value,
- state = 0,
- result = [],
- lastIndex;
- while (i < length) {
- value = buffer[i++]; // finding 3 or 4-byte start codes (00 00 01 OR 00 00 00 01)
- switch (state) {
- case 0:
- if (value === 0) {
- state = 1;
- }
- break;
- case 1:
- if (value === 0) {
- state = 2;
- } else {
- state = 0;
- }
- break;
- case 2:
- case 3:
- if (value === 0) {
- state = 3;
- } else if (value === 1 && i < length) {
- if (lastIndex) {
- result.push(buffer.subarray(lastIndex, i - state - 1));
- }
- lastIndex = i;
- state = 0;
- } else {
- state = 0;
- }
- break;
- }
- }
- if (lastIndex) {
- result.push(buffer.subarray(lastIndex, length));
- }
- return result;
- }
- /**
- * Advance the ExpGolomb decoder past a scaling list. The scaling
- * list is optionally transmitted as part of a sequence parameter
- * set and is not relevant to transmuxing.
- * @param decoder {ExpGolomb} exp golomb decoder
- * @param count {number} the number of entries in this scaling list
- * @see Recommendation ITU-T H.264, Section 7.3.2.1.1.1
- */
- }, {
- key: "skipScalingList",
- value: function skipScalingList(decoder, count) {
- var lastScale = 8,
- nextScale = 8,
- deltaScale;
- for (var j = 0; j < count; j++) {
- if (nextScale !== 0) {
- deltaScale = decoder.readEG();
- nextScale = (lastScale + deltaScale + 256) % 256;
- }
- lastScale = nextScale === 0 ? lastScale : nextScale;
- }
- }
- /**
- * Read a sequence parameter set and return some interesting video
- * properties. A sequence parameter set is the H264 metadata that
- * describes the properties of upcoming video frames.
- * @param data {Uint8Array} the bytes of a sequence parameter set
- * @return {object} an object with configuration parsed from the
- * sequence parameter set, including the dimensions of the
- * associated video frames.
- */
- }, {
- key: "readSPS",
- value: function readSPS(data) {
- var decoder = new ExpGolomb(data);
- var frameCropLeftOffset = 0,
- frameCropRightOffset = 0,
- frameCropTopOffset = 0,
- frameCropBottomOffset = 0,
- sarScale = 1,
- profileIdc,
- profileCompat,
- levelIdc,
- numRefFramesInPicOrderCntCycle,
- picWidthInMbsMinus1,
- picHeightInMapUnitsMinus1,
- frameMbsOnlyFlag,
- scalingListCount;
- decoder.readUByte();
- profileIdc = decoder.readUByte(); // profile_idc
- profileCompat = decoder.readBits(5); // constraint_set[0-4]_flag, u(5)
- decoder.skipBits(3); // reserved_zero_3bits u(3),
- levelIdc = decoder.readUByte(); // level_idc u(8)
- decoder.skipUEG(); // seq_parameter_set_id
- // some profiles have more optional data we don't need
- if (profileIdc === 100 || profileIdc === 110 || profileIdc === 122 || profileIdc === 244 || profileIdc === 44 || profileIdc === 83 || profileIdc === 86 || profileIdc === 118 || profileIdc === 128) {
- var chromaFormatIdc = decoder.readUEG();
- if (chromaFormatIdc === 3) {
- decoder.skipBits(1); // separate_colour_plane_flag
- }
- decoder.skipUEG(); // bit_depth_luma_minus8
- decoder.skipUEG(); // bit_depth_chroma_minus8
- decoder.skipBits(1); // qpprime_y_zero_transform_bypass_flag
- if (decoder.readBoolean()) {
- // seq_scaling_matrix_present_flag
- scalingListCount = chromaFormatIdc !== 3 ? 8 : 12;
- for (var i = 0; i < scalingListCount; ++i) {
- if (decoder.readBoolean()) {
- // seq_scaling_list_present_flag[ i ]
- if (i < 6) {
- H264Parser.skipScalingList(decoder, 16);
- } else {
- H264Parser.skipScalingList(decoder, 64);
- }
- }
- }
- }
- }
- decoder.skipUEG(); // log2_max_frame_num_minus4
- var picOrderCntType = decoder.readUEG();
- if (picOrderCntType === 0) {
- decoder.readUEG(); // log2_max_pic_order_cnt_lsb_minus4
- } else if (picOrderCntType === 1) {
- decoder.skipBits(1); // delta_pic_order_always_zero_flag
- decoder.skipEG(); // offset_for_non_ref_pic
- decoder.skipEG(); // offset_for_top_to_bottom_field
- numRefFramesInPicOrderCntCycle = decoder.readUEG();
- for (var _i = 0; _i < numRefFramesInPicOrderCntCycle; ++_i) {
- decoder.skipEG(); // offset_for_ref_frame[ i ]
- }
- }
- decoder.skipUEG(); // max_num_ref_frames
- decoder.skipBits(1); // gaps_in_frame_num_value_allowed_flag
- picWidthInMbsMinus1 = decoder.readUEG();
- picHeightInMapUnitsMinus1 = decoder.readUEG();
- frameMbsOnlyFlag = decoder.readBits(1);
- if (frameMbsOnlyFlag === 0) {
- decoder.skipBits(1); // mb_adaptive_frame_field_flag
- }
- decoder.skipBits(1); // direct_8x8_inference_flag
- if (decoder.readBoolean()) {
- // frame_cropping_flag
- frameCropLeftOffset = decoder.readUEG();
- frameCropRightOffset = decoder.readUEG();
- frameCropTopOffset = decoder.readUEG();
- frameCropBottomOffset = decoder.readUEG();
- }
- if (decoder.readBoolean()) {
- // vui_parameters_present_flag
- if (decoder.readBoolean()) {
- // aspect_ratio_info_present_flag
- var sarRatio;
- var aspectRatioIdc = decoder.readUByte();
- switch (aspectRatioIdc) {
- case 1:
- sarRatio = [1, 1];
- break;
- case 2:
- sarRatio = [12, 11];
- break;
- case 3:
- sarRatio = [10, 11];
- break;
- case 4:
- sarRatio = [16, 11];
- break;
- case 5:
- sarRatio = [40, 33];
- break;
- case 6:
- sarRatio = [24, 11];
- break;
- case 7:
- sarRatio = [20, 11];
- break;
- case 8:
- sarRatio = [32, 11];
- break;
- case 9:
- sarRatio = [80, 33];
- break;
- case 10:
- sarRatio = [18, 11];
- break;
- case 11:
- sarRatio = [15, 11];
- break;
- case 12:
- sarRatio = [64, 33];
- break;
- case 13:
- sarRatio = [160, 99];
- break;
- case 14:
- sarRatio = [4, 3];
- break;
- case 15:
- sarRatio = [3, 2];
- break;
- case 16:
- sarRatio = [2, 1];
- break;
- case 255:
- {
- sarRatio = [decoder.readUByte() << 8 | decoder.readUByte(), decoder.readUByte() << 8 | decoder.readUByte()];
- break;
- }
- }
- if (sarRatio) {
- sarScale = sarRatio[0] / sarRatio[1];
- }
- }
- if (decoder.readBoolean()) {
- decoder.skipBits(1);
- }
- if (decoder.readBoolean()) {
- decoder.skipBits(4);
- if (decoder.readBoolean()) {
- decoder.skipBits(24);
- }
- }
- if (decoder.readBoolean()) {
- decoder.skipUEG();
- decoder.skipUEG();
- }
- if (decoder.readBoolean()) {
- var unitsInTick = decoder.readUInt();
- var timeScale = decoder.readUInt();
- var fixedFrameRate = decoder.readBoolean();
- }
- }
- return {
- width: Math.ceil(((picWidthInMbsMinus1 + 1) * 16 - frameCropLeftOffset * 2 - frameCropRightOffset * 2) * sarScale),
- height: (2 - frameMbsOnlyFlag) * (picHeightInMapUnitsMinus1 + 1) * 16 - (frameMbsOnlyFlag ? 2 : 4) * (frameCropTopOffset + frameCropBottomOffset)
- };
- }
- }, {
- key: "parseHeader",
- value: function parseHeader(unit) {
- var decoder = new ExpGolomb(unit.getPayload()); // skip NALu type
- decoder.readUByte();
- unit.isfmb = decoder.readUEG() === 0;
- unit.stype = decoder.readUEG();
- }
- }]);
- function H264Parser(remuxer) {
- _classCallCheck(this, H264Parser);
- this.remuxer = remuxer;
- this.track = remuxer.mp4track;
- }
- _createClass(H264Parser, [{
- key: "parseSPS",
- value: function parseSPS(sps) {
- var config = H264Parser.readSPS(new Uint8Array(sps));
- this.track.width = config.width;
- this.track.height = config.height;
- this.track.sps = [new Uint8Array(sps)];
- this.track.codec = 'avc1.';
- var codecarray = new DataView(sps.buffer, sps.byteOffset + 1, 4);
- for (var i = 0; i < 3; ++i) {
- var h = codecarray.getUint8(i).toString(16);
- if (h.length < 2) {
- h = '0' + h;
- }
- this.track.codec += h;
- }
- }
- }, {
- key: "parsePPS",
- value: function parsePPS(pps) {
- this.track.pps = [new Uint8Array(pps)];
- }
- }, {
- key: "parseNAL",
- value: function parseNAL(unit) {
- if (!unit) return false;
- var push = false;
- switch (unit.type()) {
- case NALU.IDR:
- case NALU.NDR:
- push = true;
- break;
- case NALU.PPS:
- if (!this.track.pps) {
- this.parsePPS(unit.getPayload());
- if (!this.remuxer.readyToDecode && this.track.pps && this.track.sps) {
- this.remuxer.readyToDecode = true;
- }
- }
- push = true;
- break;
- case NALU.SPS:
- if (!this.track.sps) {
- this.parseSPS(unit.getPayload());
- if (!this.remuxer.readyToDecode && this.track.pps && this.track.sps) {
- this.remuxer.readyToDecode = true;
- }
- }
- push = true;
- break;
- case NALU.AUD:
- log('AUD - ignoing');
- break;
- case NALU.SEI:
- log('SEI - ignoing');
- break;
- }
- return push;
- }
- }]);
- return H264Parser;
- }();
- var aacHeader;
- var AACParser = /*#__PURE__*/function () {
- _createClass(AACParser, null, [{
- key: "getHeaderLength",
- value: function getHeaderLength(data) {
- return data[1] & 0x01 ? 7 : 9; // without CRC 7 and with CRC 9 Refs: https://wiki.multimedia.cx/index.php?title=ADTS
- }
- }, {
- key: "getFrameLength",
- value: function getFrameLength(data) {
- return (data[3] & 0x03) << 11 | data[4] << 3 | (data[5] & 0xE0) >>> 5; // 13 bits length ref: https://wiki.multimedia.cx/index.php?title=ADTS
- }
- }, {
- key: "isAACPattern",
- value: function isAACPattern(data) {
- return data[0] === 0xff && (data[1] & 0xf0) === 0xf0 && (data[1] & 0x06) === 0x00;
- }
- }, {
- key: "extractAAC",
- value: function extractAAC(buffer) {
- var i = 0,
- length = buffer.byteLength,
- result = [],
- headerLength,
- frameLength;
- if (!AACParser.isAACPattern(buffer)) {
- error('Invalid ADTS audio format');
- return result;
- }
- headerLength = AACParser.getHeaderLength(buffer);
- if (!aacHeader) {
- aacHeader = buffer.subarray(0, headerLength);
- }
- while (i < length) {
- frameLength = AACParser.getFrameLength(buffer);
- result.push(buffer.subarray(headerLength, frameLength));
- buffer = buffer.slice(frameLength);
- i += frameLength;
- }
- return result;
- }
- }, {
- key: "samplingRateMap",
- get: function get() {
- return [96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350];
- }
- }, {
- key: "getAACHeaderData",
- get: function get() {
- return aacHeader;
- }
- }]);
- function AACParser(remuxer) {
- _classCallCheck(this, AACParser);
- this.remuxer = remuxer;
- this.track = remuxer.mp4track;
- }
- _createClass(AACParser, [{
- key: "setAACConfig",
- value: function setAACConfig() {
- var objectType,
- sampleIndex,
- channelCount,
- config = new Uint8Array(2),
- headerData = AACParser.getAACHeaderData;
- if (!headerData) return;
- objectType = ((headerData[2] & 0xC0) >>> 6) + 1;
- sampleIndex = (headerData[2] & 0x3C) >>> 2;
- channelCount = (headerData[2] & 0x01) << 2;
- channelCount |= (headerData[3] & 0xC0) >>> 6;
- /* refer to http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio#Audio_Specific_Config */
- config[0] = objectType << 3;
- config[0] |= (sampleIndex & 0x0E) >> 1;
- config[1] |= (sampleIndex & 0x01) << 7;
- config[1] |= channelCount << 3;
- this.track.codec = 'mp4a.40.' + objectType;
- this.track.channelCount = channelCount;
- this.track.config = config;
- this.remuxer.readyToDecode = true;
- }
- }]);
- return AACParser;
- }();
- var Event = /*#__PURE__*/function () {
- function Event(type) {
- _classCallCheck(this, Event);
- this.listener = {};
- this.type = type | '';
- }
- _createClass(Event, [{
- key: "on",
- value: function on(event, fn) {
- if (!this.listener[event]) {
- this.listener[event] = [];
- }
- this.listener[event].push(fn);
- return true;
- }
- }, {
- key: "off",
- value: function off(event, fn) {
- if (this.listener[event]) {
- var index = this.listener[event].indexOf(fn);
- if (index > -1) {
- this.listener[event].splice(index, 1);
- }
- return true;
- }
- return false;
- }
- }, {
- key: "offAll",
- value: function offAll() {
- this.listener = {};
- }
- }, {
- key: "dispatch",
- value: function dispatch(event, data) {
- if (this.listener[event]) {
- this.listener[event].map(function (each) {
- each.apply(null, [data]);
- });
- return true;
- }
- return false;
- }
- }]);
- return Event;
- }();
- /**
- * Generate MP4 Box
- * taken from: https://github.com/dailymotion/hls.js
- */
- var MP4 = /*#__PURE__*/function () {
- function MP4() {
- _classCallCheck(this, MP4);
- }
- _createClass(MP4, null, [{
- key: "init",
- value: function init() {
- MP4.types = {
- avc1: [],
- // codingname
- avcC: [],
- btrt: [],
- dinf: [],
- dref: [],
- esds: [],
- ftyp: [],
- hdlr: [],
- mdat: [],
- mdhd: [],
- mdia: [],
- mfhd: [],
- minf: [],
- moof: [],
- moov: [],
- mp4a: [],
- mvex: [],
- mvhd: [],
- sdtp: [],
- stbl: [],
- stco: [],
- stsc: [],
- stsd: [],
- stsz: [],
- stts: [],
- tfdt: [],
- tfhd: [],
- traf: [],
- trak: [],
- trun: [],
- trex: [],
- tkhd: [],
- vmhd: [],
- smhd: []
- };
- var i;
- for (i in MP4.types) {
- if (MP4.types.hasOwnProperty(i)) {
- MP4.types[i] = [i.charCodeAt(0), i.charCodeAt(1), i.charCodeAt(2), i.charCodeAt(3)];
- }
- }
- var videoHdlr = new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x00, // pre_defined
- 0x76, 0x69, 0x64, 0x65, // handler_type: 'vide'
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x56, 0x69, 0x64, 0x65, 0x6f, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'VideoHandler'
- ]);
- var audioHdlr = new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x00, // pre_defined
- 0x73, 0x6f, 0x75, 0x6e, // handler_type: 'soun'
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x53, 0x6f, 0x75, 0x6e, 0x64, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x00 // name: 'SoundHandler'
- ]);
- MP4.HDLR_TYPES = {
- video: videoHdlr,
- audio: audioHdlr
- };
- var dref = new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x01, // entry_count
- 0x00, 0x00, 0x00, 0x0c, // entry_size
- 0x75, 0x72, 0x6c, 0x20, // 'url' type
- 0x00, // version 0
- 0x00, 0x00, 0x01 // entry_flags
- ]);
- var stco = new Uint8Array([0x00, // version
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x00 // entry_count
- ]);
- MP4.STTS = MP4.STSC = MP4.STCO = stco;
- MP4.STSZ = new Uint8Array([0x00, // version
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x00, // sample_size
- 0x00, 0x00, 0x00, 0x00 // sample_count
- ]);
- MP4.VMHD = new Uint8Array([0x00, // version
- 0x00, 0x00, 0x01, // flags
- 0x00, 0x00, // graphicsmode
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // opcolor
- ]);
- MP4.SMHD = new Uint8Array([0x00, // version
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, // balance
- 0x00, 0x00 // reserved
- ]);
- MP4.STSD = new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x01]); // entry_count
- var majorBrand = new Uint8Array([105, 115, 111, 109]); // isom
- var avc1Brand = new Uint8Array([97, 118, 99, 49]); // avc1
- var minorVersion = new Uint8Array([0, 0, 0, 1]);
- MP4.FTYP = MP4.box(MP4.types.ftyp, majorBrand, minorVersion, majorBrand, avc1Brand);
- MP4.DINF = MP4.box(MP4.types.dinf, MP4.box(MP4.types.dref, dref));
- }
- }, {
- key: "box",
- value: function box(type) {
- for (var _len = arguments.length, payload = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
- payload[_key - 1] = arguments[_key];
- }
- var size = 8,
- i = payload.length,
- len = i,
- result; // calculate the total size we need to allocate
- while (i--) {
- size += payload[i].byteLength;
- }
- result = new Uint8Array(size);
- result[0] = size >> 24 & 0xff;
- result[1] = size >> 16 & 0xff;
- result[2] = size >> 8 & 0xff;
- result[3] = size & 0xff;
- result.set(type, 4); // copy the payload into the result
- for (i = 0, size = 8; i < len; ++i) {
- // copy payload[i] array @ offset size
- result.set(payload[i], size);
- size += payload[i].byteLength;
- }
- return result;
- }
- }, {
- key: "hdlr",
- value: function hdlr(type) {
- return MP4.box(MP4.types.hdlr, MP4.HDLR_TYPES[type]);
- }
- }, {
- key: "mdat",
- value: function mdat(data) {
- return MP4.box(MP4.types.mdat, data);
- }
- }, {
- key: "mdhd",
- value: function mdhd(timescale, duration) {
- return MP4.box(MP4.types.mdhd, new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x02, // creation_time
- 0x00, 0x00, 0x00, 0x03, // modification_time
- timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF, // timescale
- duration >> 24, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
- 0x55, 0xc4, // 'und' language (undetermined)
- 0x00, 0x00]));
- }
- }, {
- key: "mdia",
- value: function mdia(track) {
- return MP4.box(MP4.types.mdia, MP4.mdhd(track.timescale, track.duration), MP4.hdlr(track.type), MP4.minf(track));
- }
- }, {
- key: "mfhd",
- value: function mfhd(sequenceNumber) {
- return MP4.box(MP4.types.mfhd, new Uint8Array([0x00, 0x00, 0x00, 0x00, // flags
- sequenceNumber >> 24, sequenceNumber >> 16 & 0xFF, sequenceNumber >> 8 & 0xFF, sequenceNumber & 0xFF // sequence_number
- ]));
- }
- }, {
- key: "minf",
- value: function minf(track) {
- if (track.type === 'audio') {
- return MP4.box(MP4.types.minf, MP4.box(MP4.types.smhd, MP4.SMHD), MP4.DINF, MP4.stbl(track));
- } else {
- return MP4.box(MP4.types.minf, MP4.box(MP4.types.vmhd, MP4.VMHD), MP4.DINF, MP4.stbl(track));
- }
- }
- }, {
- key: "moof",
- value: function moof(sn, baseMediaDecodeTime, track) {
- return MP4.box(MP4.types.moof, MP4.mfhd(sn), MP4.traf(track, baseMediaDecodeTime));
- }
- /**
- * @param tracks... (optional) {array} the tracks associated with this movie
- */
- }, {
- key: "moov",
- value: function moov(tracks, duration, timescale) {
- var i = tracks.length,
- boxes = [];
- while (i--) {
- boxes[i] = MP4.trak(tracks[i]);
- }
- return MP4.box.apply(null, [MP4.types.moov, MP4.mvhd(timescale, duration)].concat(boxes).concat(MP4.mvex(tracks)));
- }
- }, {
- key: "mvex",
- value: function mvex(tracks) {
- var i = tracks.length,
- boxes = [];
- while (i--) {
- boxes[i] = MP4.trex(tracks[i]);
- }
- return MP4.box.apply(null, [MP4.types.mvex].concat(boxes));
- }
- }, {
- key: "mvhd",
- value: function mvhd(timescale, duration) {
- var bytes = new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x00, 0x00, 0x00, 0x01, // creation_time
- 0x00, 0x00, 0x00, 0x02, // modification_time
- timescale >> 24 & 0xFF, timescale >> 16 & 0xFF, timescale >> 8 & 0xFF, timescale & 0xFF, // timescale
- duration >> 24 & 0xFF, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
- 0x00, 0x01, 0x00, 0x00, // 1.0 rate
- 0x01, 0x00, // 1.0 volume
- 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
- 0xff, 0xff, 0xff, 0xff // next_track_ID
- ]);
- return MP4.box(MP4.types.mvhd, bytes);
- }
- }, {
- key: "sdtp",
- value: function sdtp(track) {
- var samples = track.samples || [],
- bytes = new Uint8Array(4 + samples.length),
- flags,
- i; // leave the full box header (4 bytes) all zero
- // write the sample table
- for (i = 0; i < samples.length; i++) {
- flags = samples[i].flags;
- bytes[i + 4] = flags.dependsOn << 4 | flags.isDependedOn << 2 | flags.hasRedundancy;
- }
- return MP4.box(MP4.types.sdtp, bytes);
- }
- }, {
- key: "stbl",
- value: function stbl(track) {
- 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));
- }
- }, {
- key: "avc1",
- value: function avc1(track) {
- var sps = [],
- pps = [],
- i,
- data,
- len; // assemble the SPSs
- for (i = 0; i < track.sps.length; i++) {
- data = track.sps[i];
- len = data.byteLength;
- sps.push(len >>> 8 & 0xFF);
- sps.push(len & 0xFF);
- sps = sps.concat(Array.prototype.slice.call(data)); // SPS
- } // assemble the PPSs
- for (i = 0; i < track.pps.length; i++) {
- data = track.pps[i];
- len = data.byteLength;
- pps.push(len >>> 8 & 0xFF);
- pps.push(len & 0xFF);
- pps = pps.concat(Array.prototype.slice.call(data));
- }
- var avcc = MP4.box(MP4.types.avcC, new Uint8Array([0x01, // version
- sps[3], // profile
- sps[4], // profile compat
- sps[5], // level
- 0xfc | 3, // lengthSizeMinusOne, hard-coded to 4 bytes
- 0xE0 | track.sps.length // 3bit reserved (111) + numOfSequenceParameterSets
- ].concat(sps).concat([track.pps.length // numOfPictureParameterSets
- ]).concat(pps))),
- // "PPS"
- width = track.width,
- height = track.height; // console.log('avcc:' + Hex.hexDump(avcc));
- return MP4.box(MP4.types.avc1, new Uint8Array([0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, // reserved
- 0x00, 0x01, // data_reference_index
- 0x00, 0x00, // pre_defined
- 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // pre_defined
- width >> 8 & 0xFF, width & 0xff, // width
- height >> 8 & 0xFF, height & 0xff, // height
- 0x00, 0x48, 0x00, 0x00, // horizresolution
- 0x00, 0x48, 0x00, 0x00, // vertresolution
- 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x01, // frame_count
- 0x12, 0x62, 0x69, 0x6E, 0x65, // binelpro.ru
- 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, // compressorname
- 0x00, 0x18, // depth = 24
- 0x11, 0x11]), // pre_defined = -1
- avcc, MP4.box(MP4.types.btrt, new Uint8Array([0x00, 0x1c, 0x9c, 0x80, // bufferSizeDB
- 0x00, 0x2d, 0xc6, 0xc0, // maxBitrate
- 0x00, 0x2d, 0xc6, 0xc0])) // avgBitrate
- );
- }
- }, {
- key: "esds",
- value: function esds(track) {
- var configlen = track.config.byteLength;
- var data = new Uint8Array(26 + configlen + 3);
- data.set([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- 0x03, // descriptor_type
- 0x17 + configlen, // length
- 0x00, 0x01, // es_id
- 0x00, // stream_priority
- 0x04, // descriptor_type
- 0x0f + configlen, // length
- 0x40, // codec : mpeg4_audio
- 0x15, // stream_type
- 0x00, 0x00, 0x00, // buffer_size
- 0x00, 0x00, 0x00, 0x00, // maxBitrate
- 0x00, 0x00, 0x00, 0x00, // avgBitrate
- 0x05, // descriptor_type
- configlen]);
- data.set(track.config, 26);
- data.set([0x06, 0x01, 0x02], 26 + configlen); // return new Uint8Array([
- // 0x00, // version 0
- // 0x00, 0x00, 0x00, // flags
- //
- // 0x03, // descriptor_type
- // 0x17+configlen, // length
- // 0x00, 0x01, //es_id
- // 0x00, // stream_priority
- //
- // 0x04, // descriptor_type
- // 0x0f+configlen, // length
- // 0x40, //codec : mpeg4_audio
- // 0x15, // stream_type
- // 0x00, 0x00, 0x00, // buffer_size
- // 0x00, 0x00, 0x00, 0x00, // maxBitrate
- // 0x00, 0x00, 0x00, 0x00, // avgBitrate
- //
- // 0x05 // descriptor_type
- // ].concat([configlen]).concat(track.config).concat([0x06, 0x01, 0x02])); // GASpecificConfig)); // length + audio config descriptor
- return data;
- }
- }, {
- key: "mp4a",
- value: function mp4a(track) {
- var audiosamplerate = track.audiosamplerate;
- return MP4.box(MP4.types.mp4a, new Uint8Array([0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, 0x00, // reserved
- 0x00, 0x01, // data_reference_index
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, track.channelCount, // channelcount
- 0x00, 0x10, // sampleSize:16bits
- 0x00, 0x00, // pre_defined
- 0x00, 0x00, // reserved2
- audiosamplerate >> 8 & 0xFF, audiosamplerate & 0xff, //
- 0x00, 0x00]), MP4.box(MP4.types.esds, MP4.esds(track)));
- }
- }, {
- key: "stsd",
- value: function stsd(track) {
- if (track.type === 'audio') {
- return MP4.box(MP4.types.stsd, MP4.STSD, MP4.mp4a(track));
- } else {
- return MP4.box(MP4.types.stsd, MP4.STSD, MP4.avc1(track));
- }
- }
- }, {
- key: "tkhd",
- value: function tkhd(track) {
- var id = track.id,
- duration = track.duration,
- width = track.width,
- height = track.height,
- volume = track.volume;
- return MP4.box(MP4.types.tkhd, new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x07, // flags
- 0x00, 0x00, 0x00, 0x00, // creation_time
- 0x00, 0x00, 0x00, 0x00, // modification_time
- id >> 24 & 0xFF, id >> 16 & 0xFF, id >> 8 & 0xFF, id & 0xFF, // track_ID
- 0x00, 0x00, 0x00, 0x00, // reserved
- duration >> 24, duration >> 16 & 0xFF, duration >> 8 & 0xFF, duration & 0xFF, // duration
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
- 0x00, 0x00, // layer
- 0x00, 0x00, // alternate_group
- volume >> 0 & 0xff, volume % 1 * 10 >> 0 & 0xff, // track volume // FIXME
- 0x00, 0x00, // reserved
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, // transformation: unity matrix
- width >> 8 & 0xFF, width & 0xFF, 0x00, 0x00, // width
- height >> 8 & 0xFF, height & 0xFF, 0x00, 0x00 // height
- ]));
- }
- }, {
- key: "traf",
- value: function traf(track, baseMediaDecodeTime) {
- var sampleDependencyTable = MP4.sdtp(track),
- id = track.id;
- return MP4.box(MP4.types.traf, MP4.box(MP4.types.tfhd, new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF // track_ID
- ])), MP4.box(MP4.types.tfdt, new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- baseMediaDecodeTime >> 24, baseMediaDecodeTime >> 16 & 0XFF, baseMediaDecodeTime >> 8 & 0XFF, baseMediaDecodeTime & 0xFF // baseMediaDecodeTime
- ])), MP4.trun(track, sampleDependencyTable.length + 16 + // tfhd
- 16 + // tfdt
- 8 + // traf header
- 16 + // mfhd
- 8 + // moof header
- 8), // mdat header
- sampleDependencyTable);
- }
- /**
- * Generate a track box.
- * @param track {object} a track definition
- * @return {Uint8Array} the track box
- */
- }, {
- key: "trak",
- value: function trak(track) {
- track.duration = track.duration || 0xffffffff;
- return MP4.box(MP4.types.trak, MP4.tkhd(track), MP4.mdia(track));
- }
- }, {
- key: "trex",
- value: function trex(track) {
- var id = track.id;
- return MP4.box(MP4.types.trex, new Uint8Array([0x00, // version 0
- 0x00, 0x00, 0x00, // flags
- id >> 24, id >> 16 & 0XFF, id >> 8 & 0XFF, id & 0xFF, // track_ID
- 0x00, 0x00, 0x00, 0x01, // default_sample_description_index
- 0x00, 0x00, 0x00, 0x00, // default_sample_duration
- 0x00, 0x00, 0x00, 0x00, // default_sample_size
- 0x00, 0x01, 0x00, 0x01 // default_sample_flags
- ]));
- }
- }, {
- key: "trun",
- value: function trun(track, offset) {
- var samples = track.samples || [],
- len = samples.length,
- arraylen = 12 + 16 * len,
- array = new Uint8Array(arraylen),
- i,
- sample,
- duration,
- size,
- flags,
- cts;
- offset += 8 + arraylen;
- array.set([0x00, // version 0
- 0x00, 0x0f, 0x01, // flags
- len >>> 24 & 0xFF, len >>> 16 & 0xFF, len >>> 8 & 0xFF, len & 0xFF, // sample_count
- offset >>> 24 & 0xFF, offset >>> 16 & 0xFF, offset >>> 8 & 0xFF, offset & 0xFF // data_offset
- ], 0);
- for (i = 0; i < len; i++) {
- sample = samples[i];
- duration = sample.duration;
- size = sample.size;
- flags = sample.flags;
- cts = sample.cts;
- array.set([duration >>> 24 & 0xFF, duration >>> 16 & 0xFF, duration >>> 8 & 0xFF, duration & 0xFF, // sample_duration
- size >>> 24 & 0xFF, size >>> 16 & 0xFF, size >>> 8 & 0xFF, size & 0xFF, // sample_size
- flags.isLeading << 2 | flags.dependsOn, flags.isDependedOn << 6 | flags.hasRedundancy << 4 | flags.paddingValue << 1 | flags.isNonSync, flags.degradPrio & 0xF0 << 8, flags.degradPrio & 0x0F, // sample_flags
- cts >>> 24 & 0xFF, cts >>> 16 & 0xFF, cts >>> 8 & 0xFF, cts & 0xFF // sample_composition_time_offset
- ], 12 + 16 * i);
- }
- return MP4.box(MP4.types.trun, array);
- }
- }, {
- key: "initSegment",
- value: function initSegment(tracks, duration, timescale) {
- if (!MP4.types) {
- MP4.init();
- }
- var movie = MP4.moov(tracks, duration, timescale),
- result;
- result = new Uint8Array(MP4.FTYP.byteLength + movie.byteLength);
- result.set(MP4.FTYP);
- result.set(movie, MP4.FTYP.byteLength);
- return result;
- }
- }]);
- return MP4;
- }();
- var track_id = 1;
- var BaseRemuxer = /*#__PURE__*/function () {
- _createClass(BaseRemuxer, null, [{
- key: "getTrackID",
- value: function getTrackID() {
- return track_id++;
- }
- }]);
- function BaseRemuxer() {
- _classCallCheck(this, BaseRemuxer);
- this.seq = 1;
- }
- _createClass(BaseRemuxer, [{
- key: "flush",
- value: function flush() {
- this.seq++;
- this.mp4track.len = 0;
- this.mp4track.samples = [];
- }
- }, {
- key: "isReady",
- value: function isReady() {
- if (!this.readyToDecode || !this.samples.length) return null;
- return true;
- }
- }]);
- return BaseRemuxer;
- }();
- var AACRemuxer = /*#__PURE__*/function (_BaseRemuxer) {
- _inherits(AACRemuxer, _BaseRemuxer);
- var _super = _createSuper(AACRemuxer);
- function AACRemuxer() {
- var _this;
- _classCallCheck(this, AACRemuxer);
- _this = _super.call(this);
- _this.readyToDecode = false;
- _this.nextDts = 0;
- _this.dts = 0;
- _this.timescale = 1000;
- _this.mp4track = {
- id: BaseRemuxer.getTrackID(),
- type: 'audio',
- channelCount: 0,
- len: 0,
- fragmented: true,
- timescale: _this.timescale,
- duration: _this.timescale,
- samples: [],
- config: '',
- codec: ''
- };
- _this.samples = [];
- _this.aac = new AACParser(_assertThisInitialized(_this));
- return _this;
- }
- _createClass(AACRemuxer, [{
- key: "resetTrack",
- value: function resetTrack() {
- this.readyToDecode = false;
- this.mp4track.codec = '';
- this.mp4track.channelCount = '';
- this.mp4track.config = '';
- this.mp4track.timescale = this.timescale;
- }
- }, {
- key: "remux",
- value: function remux(frames) {
- if (frames.length > 0) {
- for (var i = 0; i < frames.length; i++) {
- var frame = frames[i];
- var payload = frame.units;
- var size = payload.byteLength;
- this.samples.push({
- units: payload,
- size: size,
- duration: frame.duration
- });
- this.mp4track.len += size;
- if (!this.readyToDecode) {
- this.aac.setAACConfig();
- }
- }
- }
- }
- }, {
- key: "getPayload",
- value: function getPayload() {
- if (!this.isReady()) {
- return null;
- }
- var payload = new Uint8Array(this.mp4track.len);
- var offset = 0;
- var samples = this.mp4track.samples;
- var mp4Sample, duration;
- this.dts = this.nextDts;
- while (this.samples.length) {
- var sample = this.samples.shift(),
- units = sample.units;
- duration = sample.duration;
- if (duration <= 0) {
- log("remuxer: invalid sample duration at DTS: ".concat(this.nextDts, " :").concat(duration));
- this.mp4track.len -= sample.size;
- continue;
- }
- this.nextDts += duration;
- mp4Sample = {
- size: sample.size,
- duration: duration,
- cts: 0,
- flags: {
- isLeading: 0,
- isDependedOn: 0,
- hasRedundancy: 0,
- degradPrio: 0,
- dependsOn: 1
- }
- };
- payload.set(sample.units, offset);
- offset += sample.size;
- samples.push(mp4Sample);
- }
- if (!samples.length) return null;
- return new Uint8Array(payload.buffer, 0, this.mp4track.len);
- }
- }]);
- return AACRemuxer;
- }(BaseRemuxer);
- var H264Remuxer = /*#__PURE__*/function (_BaseRemuxer) {
- _inherits(H264Remuxer, _BaseRemuxer);
- var _super = _createSuper(H264Remuxer);
- function H264Remuxer() {
- var _this;
- _classCallCheck(this, H264Remuxer);
- _this = _super.call(this);
- _this.readyToDecode = false;
- _this.nextDts = 0;
- _this.dts = 0;
- _this.timescale = 1000;
- _this.mp4track = {
- id: BaseRemuxer.getTrackID(),
- type: 'video',
- len: 0,
- fragmented: true,
- sps: '',
- pps: '',
- width: 0,
- height: 0,
- timescale: _this.timescale,
- duration: _this.timescale,
- samples: []
- };
- _this.samples = [];
- _this.h264 = new H264Parser(_assertThisInitialized(_this));
- return _this;
- }
- _createClass(H264Remuxer, [{
- key: "resetTrack",
- value: function resetTrack() {
- this.readyToDecode = false;
- this.mp4track.sps = '';
- this.mp4track.pps = '';
- }
- }, {
- key: "remux",
- value: function remux(frames) {
- var _iterator = _createForOfIteratorHelper(frames),
- _step;
- try {
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
- var frame = _step.value;
- var units = [];
- var size = 0;
- var _iterator2 = _createForOfIteratorHelper(frame.units),
- _step2;
- try {
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
- var unit = _step2.value;
- if (this.h264.parseNAL(unit)) {
- units.push(unit);
- size += unit.getSize();
- }
- }
- } catch (err) {
- _iterator2.e(err);
- } finally {
- _iterator2.f();
- }
- if (units.length > 0 && this.readyToDecode) {
- this.mp4track.len += size;
- this.samples.push({
- units: units,
- size: size,
- keyFrame: frame.keyFrame,
- duration: frame.duration
- });
- }
- }
- } catch (err) {
- _iterator.e(err);
- } finally {
- _iterator.f();
- }
- }
- }, {
- key: "getPayload",
- value: function getPayload() {
- if (!this.isReady()) {
- return null;
- }
- var payload = new Uint8Array(this.mp4track.len);
- var offset = 0;
- var samples = this.mp4track.samples;
- var mp4Sample, duration;
- this.dts = this.nextDts;
- while (this.samples.length) {
- var sample = this.samples.shift(),
- units = sample.units;
- duration = sample.duration;
- if (duration <= 0) {
- log("remuxer: invalid sample duration at DTS: ".concat(this.nextDts, " :").concat(duration));
- this.mp4track.len -= sample.size;
- continue;
- }
- this.nextDts += duration;
- mp4Sample = {
- size: sample.size,
- duration: duration,
- cts: 0,
- flags: {
- isLeading: 0,
- isDependedOn: 0,
- hasRedundancy: 0,
- degradPrio: 0,
- isNonSync: sample.keyFrame ? 0 : 1,
- dependsOn: sample.keyFrame ? 2 : 1
- }
- };
- var _iterator3 = _createForOfIteratorHelper(units),
- _step3;
- try {
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
- var unit = _step3.value;
- payload.set(unit.getData(), offset);
- offset += unit.getSize();
- }
- } catch (err) {
- _iterator3.e(err);
- } finally {
- _iterator3.f();
- }
- samples.push(mp4Sample);
- }
- if (!samples.length) return null;
- return new Uint8Array(payload.buffer, 0, this.mp4track.len);
- }
- }]);
- return H264Remuxer;
- }(BaseRemuxer);
- function appendByteArray(buffer1, buffer2) {
- var tmp = new Uint8Array((buffer1.byteLength | 0) + (buffer2.byteLength | 0));
- tmp.set(buffer1, 0);
- tmp.set(buffer2, buffer1.byteLength | 0);
- return tmp;
- }
- function secToTime(sec) {
- var seconds,
- hours,
- minutes,
- result = '';
- seconds = Math.floor(sec);
- hours = parseInt(seconds / 3600, 10) % 24;
- minutes = parseInt(seconds / 60, 10) % 60;
- seconds = seconds < 0 ? 0 : seconds % 60;
- if (hours > 0) {
- result += (hours < 10 ? '0' + hours : hours) + ':';
- }
- result += (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds);
- return result;
- }
- var RemuxController = /*#__PURE__*/function (_Event) {
- _inherits(RemuxController, _Event);
- var _super = _createSuper(RemuxController);
- function RemuxController(streaming) {
- var _this;
- _classCallCheck(this, RemuxController);
- _this = _super.call(this, 'remuxer');
- _this.initialized = false;
- _this.trackTypes = [];
- _this.tracks = {};
- _this.mediaDuration = streaming ? Infinity : 1000;
- return _this;
- }
- _createClass(RemuxController, [{
- key: "addTrack",
- value: function addTrack(type) {
- if (type === 'video' || type === 'both') {
- this.tracks.video = new H264Remuxer();
- this.trackTypes.push('video');
- }
- if (type === 'audio' || type === 'both') {
- this.tracks.audio = new AACRemuxer();
- this.trackTypes.push('audio');
- }
- }
- }, {
- key: "reset",
- value: function reset() {
- var _iterator = _createForOfIteratorHelper(this.trackTypes),
- _step;
- try {
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
- var type = _step.value;
- this.tracks[type].resetTrack();
- }
- } catch (err) {
- _iterator.e(err);
- } finally {
- _iterator.f();
- }
- this.initialized = false;
- }
- }, {
- key: "destroy",
- value: function destroy() {
- this.tracks = {};
- this.offAll();
- }
- }, {
- key: "flush",
- value: function flush() {
- if (!this.initialized) {
- if (this.isReady()) {
- this.dispatch('ready');
- var _iterator2 = _createForOfIteratorHelper(this.trackTypes),
- _step2;
- try {
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
- var type = _step2.value;
- var track = this.tracks[type];
- var data = {
- type: type,
- payload: MP4.initSegment([track.mp4track], this.mediaDuration, track.mp4track.timescale)
- };
- this.dispatch('buffer', data);
- }
- } catch (err) {
- _iterator2.e(err);
- } finally {
- _iterator2.f();
- }
- log('Initial segment generated.');
- this.initialized = true;
- this.flush();
- }
- } else {
- var _iterator3 = _createForOfIteratorHelper(this.trackTypes),
- _step3;
- try {
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
- var _type = _step3.value;
- var _track = this.tracks[_type];
- var pay = _track.getPayload();
- if (pay && pay.byteLength) {
- var moof = MP4.moof(_track.seq, _track.dts, _track.mp4track);
- var mdat = MP4.mdat(pay);
- var payload = appendByteArray(moof, mdat);
- var _data = {
- type: _type,
- payload: payload,
- dts: _track.dts
- };
- this.dispatch('buffer', _data);
- var duration = secToTime(_track.dts / 1000);
- log("put segment (".concat(_type, "): ").concat(_track.seq, " dts: ").concat(_track.dts, " frames: ").concat(_track.mp4track.samples.length, " second: ").concat(duration));
- _track.flush();
- }
- }
- } catch (err) {
- _iterator3.e(err);
- } finally {
- _iterator3.f();
- }
- }
- }
- }, {
- key: "isReady",
- value: function isReady() {
- var _iterator4 = _createForOfIteratorHelper(this.trackTypes),
- _step4;
- try {
- for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
- var type = _step4.value;
- if (!this.tracks[type].readyToDecode || !this.tracks[type].samples.length) return false;
- }
- } catch (err) {
- _iterator4.e(err);
- } finally {
- _iterator4.f();
- }
- return true;
- }
- }, {
- key: "remux",
- value: function remux(data) {
- var _iterator5 = _createForOfIteratorHelper(this.trackTypes),
- _step5;
- try {
- for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
- var type = _step5.value;
- var frames = data[type];
- if (type === 'audio' && this.tracks.video && !this.tracks.video.readyToDecode) continue;
- /* if video is present, don't add audio until video get ready */
- if (frames.length > 0) {
- this.tracks[type].remux(frames);
- }
- }
- } catch (err) {
- _iterator5.e(err);
- } finally {
- _iterator5.f();
- }
- this.flush();
- }
- }]);
- return RemuxController;
- }(Event);
- var BufferController = /*#__PURE__*/function (_Event) {
- _inherits(BufferController, _Event);
- var _super = _createSuper(BufferController);
- function BufferController(sourceBuffer, type) {
- var _this;
- _classCallCheck(this, BufferController);
- _this = _super.call(this, 'buffer');
- _this.type = type;
- _this.queue = new Uint8Array();
- _this.cleaning = false;
- _this.pendingCleaning = 0;
- _this.cleanOffset = 30;
- _this.cleanRanges = [];
- _this.sourceBuffer = sourceBuffer;
- _this.sourceBuffer.addEventListener('updateend', function () {
- if (_this.pendingCleaning > 0) {
- _this.initCleanup(_this.pendingCleaning);
- _this.pendingCleaning = 0;
- }
- _this.cleaning = false;
- if (_this.cleanRanges.length) {
- _this.doCleanup();
- return;
- }
- });
- _this.sourceBuffer.addEventListener('error', function () {
- _this.dispatch('error', {
- type: _this.type,
- name: 'buffer',
- error: 'buffer error'
- });
- });
- return _this;
- }
- _createClass(BufferController, [{
- key: "destroy",
- value: function destroy() {
- this.queue = null;
- this.sourceBuffer = null;
- this.offAll();
- }
- }, {
- key: "doCleanup",
- value: function doCleanup() {
- if (!this.cleanRanges.length) {
- this.cleaning = false;
- return;
- }
- var range = this.cleanRanges.shift();
- log("".concat(this.type, " remove range [").concat(range[0], " - ").concat(range[1], ")"));
- this.cleaning = true;
- this.sourceBuffer.remove(range[0], range[1]);
- }
- }, {
- key: "initCleanup",
- value: function initCleanup(cleanMaxLimit) {
- if (this.sourceBuffer.updating) {
- this.pendingCleaning = cleanMaxLimit;
- return;
- }
- if (this.sourceBuffer.buffered && this.sourceBuffer.buffered.length && !this.cleaning) {
- for (var i = 0; i < this.sourceBuffer.buffered.length; ++i) {
- var start = this.sourceBuffer.buffered.start(i);
- var end = this.sourceBuffer.buffered.end(i);
- if (cleanMaxLimit - start > this.cleanOffset) {
- end = cleanMaxLimit - this.cleanOffset;
- if (start < end) {
- this.cleanRanges.push([start, end]);
- }
- }
- }
- this.doCleanup();
- }
- }
- }, {
- key: "doAppend",
- value: function doAppend() {
- if (!this.queue.length) return;
- if (this.sourceBuffer.updating) {
- return;
- }
- try {
- this.sourceBuffer.appendBuffer(this.queue);
- this.queue = new Uint8Array();
- } catch (e) {
- if (e.name === 'QuotaExceededError') {
- log("".concat(this.type, " buffer quota full"));
- this.dispatch('error', {
- type: this.type,
- name: 'QuotaExceeded',
- error: 'buffer error'
- });
- return;
- }
- error("Error occured while appending ".concat(this.type, " buffer - ").concat(e.name, ": ").concat(e.message));
- this.dispatch('error', {
- type: this.type,
- name: 'unexpectedError',
- error: 'buffer error'
- });
- }
- }
- }, {
- key: "feed",
- value: function feed(data) {
- this.queue = appendByteArray(this.queue, data);
- }
- }]);
- return BufferController;
- }(Event);
- window.MediaSource = window.MediaSource || window.WebKitMediaSource;
- var JMuxmer = /*#__PURE__*/function (_Event) {
- _inherits(JMuxmer, _Event);
- var _super = _createSuper(JMuxmer);
- _createClass(JMuxmer, null, [{
- key: "isSupported",
- value: function isSupported(codec) {
- return window.MediaSource && window.MediaSource.isTypeSupported(codec);
- }
- }]);
- function JMuxmer(options) {
- var _this;
- _classCallCheck(this, JMuxmer);
- _this = _super.call(this, 'jmuxer');
- window.MediaSource = window.MediaSource || window.WebKitMediaSource;
- var defaults = {
- node: '',
- mode: 'both',
- // both, audio, video
- flushingTime: 1500,
- clearBuffer: true,
- onReady: null,
- // function called when MSE is ready to accept frames
- fps: 30,
- debug: false
- };
- _this.options = Object.assign({}, defaults, options);
- if (_this.options.debug) {
- setLogger();
- }
- if (typeof _this.options.node === 'string' && _this.options.node == '') {
- error('no video element were found to render, provide a valid video element');
- }
- if (!_this.options.fps) {
- _this.options.fps = 30;
- }
- _this.frameDuration = 1000 / _this.options.fps | 0; // todo remove
- _this.node = typeof _this.options.node === 'string' ? document.getElementById(_this.options.node) : _this.options.node;
- _this.sourceBuffers = {};
- _this.isMSESupported = !!window.MediaSource;
- if (!_this.isMSESupported) {
- throw 'Oops! Browser does not support media source extension.';
- }
- _this.setupMSE();
- _this.remuxController = new RemuxController(_this.options.clearBuffer);
- _this.remuxController.addTrack(_this.options.mode);
- _this.mseReady = false;
- _this.lastCleaningTime = Date.now();
- _this.kfPosition = [];
- _this.kfCounter = 0;
- /* events callback */
- _this.remuxController.on('buffer', _this.onBuffer.bind(_assertThisInitialized(_this)));
- _this.remuxController.on('ready', _this.createBuffer.bind(_assertThisInitialized(_this)));
- _this.startInterval();
- return _this;
- }
- _createClass(JMuxmer, [{
- key: "setupMSE",
- value: function setupMSE() {
- this.mediaSource = new MediaSource();
- this.node.src = URL.createObjectURL(this.mediaSource);
- this.mediaSource.addEventListener('sourceopen', this.onMSEOpen.bind(this));
- this.mediaSource.addEventListener('sourceclose', this.onMSEClose.bind(this));
- this.mediaSource.addEventListener('webkitsourceopen', this.onMSEOpen.bind(this));
- this.mediaSource.addEventListener('webkitsourceclose', this.onMSEClose.bind(this));
- }
- }, {
- key: "feed",
- value: function feed(data) {
- var remux = false,
- slices,
- duration,
- chunks = {
- video: [],
- audio: []
- };
- if (!data || !this.remuxController) return;
- duration = data.duration ? parseInt(data.duration) : 0;
- if (data.video) {
- slices = H264Parser.extractNALu(data.video);
- if (slices.length > 0) {
- chunks.video = this.getVideoFrames(slices, duration);
- remux = true;
- }
- }
- if (data.audio) {
- slices = AACParser.extractAAC(data.audio);
- if (slices.length > 0) {
- chunks.audio = this.getAudioFrames(slices, duration);
- remux = true;
- }
- }
- if (!remux) {
- error('Input object must have video and/or audio property. Make sure it is a valid typed array');
- return;
- }
- this.remuxController.remux(chunks);
- }
- }, {
- key: "getVideoFrames",
- value: function getVideoFrames(nalus, duration) {
- var _this2 = this;
- var units = [],
- frames = [],
- fd = 0,
- tt = 0,
- keyFrame = false,
- vcl = false;
- var _iterator = _createForOfIteratorHelper(nalus),
- _step;
- try {
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
- var nalu = _step.value;
- var unit = new NALU(nalu);
- if (unit.type() === NALU.IDR || unit.type() === NALU.NDR) {
- H264Parser.parseHeader(unit);
- }
- if (units.length && vcl && (unit.isfmb || !unit.isvcl)) {
- frames.push({
- units: units,
- keyFrame: keyFrame
- });
- units = [];
- keyFrame = false;
- vcl = false;
- }
- units.push(unit);
- keyFrame = keyFrame || unit.isKeyframe();
- vcl = vcl || unit.isvcl;
- }
- } catch (err) {
- _iterator.e(err);
- } finally {
- _iterator.f();
- }
- if (units.length) {
- if (vcl || !frames.length) {
- frames.push({
- units: units,
- keyFrame: keyFrame
- });
- } else {
- var last = frames.length - 1;
- frames[last].units = frames[last].units.concat(units);
- }
- }
- fd = duration ? duration / frames.length | 0 : this.frameDuration;
- tt = duration ? duration - fd * frames.length : 0;
- frames.map(function (frame) {
- frame.duration = fd;
- if (tt > 0) {
- frame.duration++;
- tt--;
- }
- _this2.kfCounter++;
- if (frame.keyFrame && _this2.options.clearBuffer) {
- _this2.kfPosition.push(_this2.kfCounter * fd / 1000);
- }
- });
- log("jmuxer: No. of frames of the last chunk: ".concat(frames.length));
- return frames;
- }
- }, {
- key: "getAudioFrames",
- value: function getAudioFrames(aacFrames, duration) {
- var frames = [],
- fd = 0,
- tt = 0;
- var _iterator2 = _createForOfIteratorHelper(aacFrames),
- _step2;
- try {
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
- var units = _step2.value;
- frames.push({
- units: units
- });
- }
- } catch (err) {
- _iterator2.e(err);
- } finally {
- _iterator2.f();
- }
- fd = duration ? duration / frames.length | 0 : this.frameDuration;
- tt = duration ? duration - fd * frames.length : 0;
- frames.map(function (frame) {
- frame.duration = fd;
- if (tt > 0) {
- frame.duration++;
- tt--;
- }
- });
- return frames;
- }
- }, {
- key: "destroy",
- value: function destroy() {
- this.stopInterval();
- if (this.mediaSource) {
- try {
- if (this.bufferControllers) {
- this.mediaSource.endOfStream();
- }
- } catch (e) {
- error("mediasource is not available to end ".concat(e.message));
- }
- this.mediaSource = null;
- }
- if (this.remuxController) {
- this.remuxController.destroy();
- this.remuxController = null;
- }
- if (this.bufferControllers) {
- for (var type in this.bufferControllers) {
- this.bufferControllers[type].destroy();
- }
- this.bufferControllers = null;
- }
- this.node = false;
- this.mseReady = false;
- this.videoStarted = false;
- }
- }, {
- key: "createBuffer",
- value: function createBuffer() {
- if (!this.mseReady || !this.remuxController || !this.remuxController.isReady() || this.bufferControllers) return;
- this.bufferControllers = {};
- for (var type in this.remuxController.tracks) {
- var track = this.remuxController.tracks[type];
- if (!JMuxmer.isSupported("".concat(type, "/mp4; codecs=\"").concat(track.mp4track.codec, "\""))) {
- error('Browser does not support codec');
- return false;
- }
- var sb = this.mediaSource.addSourceBuffer("".concat(type, "/mp4; codecs=\"").concat(track.mp4track.codec, "\""));
- this.bufferControllers[type] = new BufferController(sb, type);
- this.sourceBuffers[type] = sb;
- this.bufferControllers[type].on('error', this.onBufferError.bind(this));
- }
- }
- }, {
- key: "startInterval",
- value: function startInterval() {
- var _this3 = this;
- this.interval = setInterval(function () {
- if (_this3.bufferControllers) {
- _this3.releaseBuffer();
- _this3.clearBuffer();
- }
- }, this.options.flushingTime);
- }
- }, {
- key: "stopInterval",
- value: function stopInterval() {
- if (this.interval) {
- clearInterval(this.interval);
- }
- }
- }, {
- key: "releaseBuffer",
- value: function releaseBuffer() {
- for (var type in this.bufferControllers) {
- this.bufferControllers[type].doAppend();
- }
- }
- }, {
- key: "getSafeClearOffsetOfBuffer",
- value: function getSafeClearOffsetOfBuffer(offset) {
- var maxLimit = this.options.mode === 'audio' && offset || 0,
- adjacentOffset;
- for (var i = 0; i < this.kfPosition.length; i++) {
- if (this.kfPosition[i] >= offset) {
- break;
- }
- adjacentOffset = this.kfPosition[i];
- }
- if (adjacentOffset) {
- this.kfPosition = this.kfPosition.filter(function (kfDelimiter) {
- if (kfDelimiter < adjacentOffset) {
- maxLimit = kfDelimiter;
- }
- return kfDelimiter >= adjacentOffset;
- });
- }
- return maxLimit;
- }
- }, {
- key: "clearBuffer",
- value: function clearBuffer() {
- if (this.options.clearBuffer && Date.now() - this.lastCleaningTime > 10000) {
- for (var type in this.bufferControllers) {
- var cleanMaxLimit = this.getSafeClearOffsetOfBuffer(this.node.currentTime);
- this.bufferControllers[type].initCleanup(cleanMaxLimit);
- }
- this.lastCleaningTime = Date.now();
- }
- }
- }, {
- key: "onBuffer",
- value: function onBuffer(data) {
- if (this.bufferControllers && this.bufferControllers[data.type]) {
- this.bufferControllers[data.type].feed(data.payload);
- }
- }
- /* Events on MSE */
- }, {
- key: "onMSEOpen",
- value: function onMSEOpen() {
- this.mseReady = true;
- if (typeof this.options.onReady === 'function') {
- this.options.onReady();
- this.options.onReady = null;
- }
- this.createBuffer();
- }
- }, {
- key: "onMSEClose",
- value: function onMSEClose() {
- this.mseReady = false;
- this.videoStarted = false;
- }
- }, {
- key: "onBufferError",
- value: function onBufferError(data) {
- if (data.name == 'QuotaExceeded') {
- this.bufferControllers[data.type].initCleanup(this.node.currentTime);
- return;
- }
- if (this.mediaSource.sourceBuffers.length > 0 && this.sourceBuffers[data.type]) {
- this.mediaSource.removeSourceBuffer(this.sourceBuffers[data.type]);
- }
- if (this.mediaSource.sourceBuffers.length == 0) {
- try {
- this.mediaSource.endOfStream();
- } catch (e) {
- error('mediasource is not available to end');
- }
- }
- }
- }]);
- return JMuxmer;
- }(Event);
- return JMuxmer;
- }());
|