Comparing version 6.2.0 to 6.3.0
@@ -0,1 +1,12 @@ | ||
<a name="6.3.0"></a> | ||
# [6.3.0](https://github.com/videojs/mux.js/compare/v6.2.0...v6.3.0) (2023-02-22) | ||
### Features | ||
* support emsg box parsing ([2e77285](https://github.com/videojs/mux.js/commit/2e77285)) | ||
### Bug Fixes | ||
* emsg ie11 test failures ([528e9ed](https://github.com/videojs/mux.js/commit/528e9ed)) | ||
<a name="6.2.0"></a> | ||
@@ -2,0 +13,0 @@ # [6.2.0](https://github.com/videojs/mux.js/compare/v6.1.0...v6.2.0) (2022-07-08) |
@@ -15,160 +15,3 @@ /** | ||
StreamTypes = require('./stream-types'), | ||
typedArrayIndexOf = require('../utils/typed-array').typedArrayIndexOf, | ||
// Frames that allow different types of text encoding contain a text | ||
// encoding description byte [ID3v2.4.0 section 4.] | ||
textEncodingDescriptionByte = { | ||
Iso88591: 0x00, | ||
// ISO-8859-1, terminated with \0. | ||
Utf16: 0x01, | ||
// UTF-16 encoded Unicode BOM, terminated with \0\0 | ||
Utf16be: 0x02, | ||
// UTF-16BE encoded Unicode, without BOM, terminated with \0\0 | ||
Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0 | ||
}, | ||
// return a percent-encoded representation of the specified byte range | ||
// @see http://en.wikipedia.org/wiki/Percent-encoding | ||
percentEncode = function percentEncode(bytes, start, end) { | ||
var i, | ||
result = ''; | ||
for (i = start; i < end; i++) { | ||
result += '%' + ('00' + bytes[i].toString(16)).slice(-2); | ||
} | ||
return result; | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as UTf-8. | ||
parseUtf8 = function parseUtf8(bytes, start, end) { | ||
return decodeURIComponent(percentEncode(bytes, start, end)); | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as ISO-8859-1. | ||
parseIso88591 = function parseIso88591(bytes, start, end) { | ||
return unescape(percentEncode(bytes, start, end)); // jshint ignore:line | ||
}, | ||
parseSyncSafeInteger = function parseSyncSafeInteger(data) { | ||
return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3]; | ||
}, | ||
frameParsers = { | ||
'APIC': function APIC(frame) { | ||
var i = 1, | ||
mimeTypeEndIndex, | ||
descriptionEndIndex, | ||
LINK_MIME_TYPE = '-->'; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} // parsing fields [ID3v2.4.0 section 4.14.] | ||
mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (mimeTypeEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} // parsing Mime type field (terminated with \0) | ||
frame.mimeType = parseIso88591(frame.data, i, mimeTypeEndIndex); | ||
i = mimeTypeEndIndex + 1; // parsing 1-byte Picture Type field | ||
frame.pictureType = frame.data[i]; | ||
i++; | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (descriptionEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} // parsing Description field (terminated with \0) | ||
frame.description = parseUtf8(frame.data, i, descriptionEndIndex); | ||
i = descriptionEndIndex + 1; | ||
if (frame.mimeType === LINK_MIME_TYPE) { | ||
// parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.]) | ||
frame.url = parseIso88591(frame.data, i, frame.data.length); | ||
} else { | ||
// parsing Picture Data field as binary data | ||
frame.pictureData = frame.data.subarray(i, frame.data.length); | ||
} | ||
}, | ||
'T*': function T(frame) { | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} // parse text field, do not include null terminator in the frame value | ||
// frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8(frame.data, 1, frame.data.length).replace(/\0*$/, ''); // text information frames supports multiple strings, stored as a terminator separated list [ID3v2.4.0 section 4.2.] | ||
frame.values = frame.value.split('\0'); | ||
}, | ||
'TXXX': function TXXX(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} // parse the text fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // do not include the null terminator in the tag value | ||
// frames that allow different types of encoding contain terminated text | ||
// [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, ''); | ||
frame.data = frame.value; | ||
}, | ||
'W*': function W(frame) { | ||
// parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591(frame.data, 0, frame.data.length).replace(/\0.*$/, ''); | ||
}, | ||
'WXXX': function WXXX(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} // parse the description and URL fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information | ||
// should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, ''); | ||
}, | ||
'PRIV': function PRIV(frame) { | ||
var i; | ||
for (i = 0; i < frame.data.length; i++) { | ||
if (frame.data[i] === 0) { | ||
// parse the description and URL fields | ||
frame.owner = parseIso88591(frame.data, 0, i); | ||
break; | ||
} | ||
} | ||
frame.privateData = frame.data.subarray(i + 1); | ||
frame.data = frame.privateData; | ||
} | ||
}, | ||
id3 = require('../tools/parse-id3'), | ||
_MetadataStream; | ||
@@ -236,3 +79,3 @@ | ||
// results concatenated to recover the actual value. | ||
tagSize = parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more | ||
tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more | ||
// convenient for our comparisons to include it | ||
@@ -270,5 +113,5 @@ | ||
frameStart += parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end | ||
frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end | ||
tagSize -= parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
} // parse one or more ID3 frames | ||
@@ -280,3 +123,3 @@ // http://id3.org/id3v2.3.0#ID3v2_frame_overview | ||
// determine the number of bytes in this frame | ||
frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
@@ -300,11 +143,11 @@ if (frameSize < 1) { | ||
if (frameParsers[frame.id]) { | ||
if (id3.frameParsers[frame.id]) { | ||
// use frame specific parser | ||
frameParsers[frame.id](frame); | ||
id3.frameParsers[frame.id](frame); | ||
} else if (frame.id[0] === 'T') { | ||
// use text frame generic parser | ||
frameParsers['T*'](frame); | ||
id3.frameParsers['T*'](frame); | ||
} else if (frame.id[0] === 'W') { | ||
// use URL link frame generic parser | ||
frameParsers['W*'](frame); | ||
id3.frameParsers['W*'](frame); | ||
} // handle the special PRIV frame used to indicate the start | ||
@@ -311,0 +154,0 @@ // time for raw AAC data |
@@ -19,2 +19,4 @@ /** | ||
var emsg = require('../mp4/emsg.js'); | ||
var parseTfhd = require('../tools/parse-tfhd.js'); | ||
@@ -28,5 +30,7 @@ | ||
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader; | ||
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3; | ||
var window = require('global/window'); | ||
var parseId3Frames = require('../tools/parse-id3.js').parseId3Frames; | ||
/** | ||
@@ -351,3 +355,31 @@ * Parses an MP4 initialization segment and extracts the timescale | ||
}; | ||
/** | ||
* Returns an array of emsg ID3 data from the provided segmentData. | ||
* An offset can also be provided as the Latest Arrival Time to calculate | ||
* the Event Start Time of v0 EMSG boxes. | ||
* See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing | ||
* | ||
* @param {Uint8Array} segmentData the segment byte array. | ||
* @param {number} offset the segment start time or Latest Arrival Time, | ||
* @return {Object[]} an array of ID3 parsed from EMSG boxes | ||
*/ | ||
getEmsgID3 = function getEmsgID3(segmentData, offset) { | ||
if (offset === void 0) { | ||
offset = 0; | ||
} | ||
var emsgBoxes = findBox(segmentData, ['emsg']); | ||
return emsgBoxes.map(function (data) { | ||
var parsedBox = emsg.parseEmsgBox(new Uint8Array(data)); | ||
var parsedId3Frames = parseId3Frames(parsedBox.message_data); | ||
return { | ||
cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset), | ||
duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale), | ||
frames: parsedId3Frames | ||
}; | ||
}); | ||
}; | ||
module.exports = { | ||
@@ -362,3 +394,4 @@ // export mp4 inspector's findBox and parseType for backwards compatibility | ||
tracks: getTracks, | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader, | ||
getEmsgID3: getEmsgID3 | ||
}; |
@@ -1,2 +0,2 @@ | ||
/*! @name mux.js @version 6.2.0 @license Apache-2.0 */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).muxjs=e()}(this,(function(){"use strict";var t;(t=function(e,i){var s,a=0,n=16384,r=function(t,e){var i,s=t.position+e;s<t.bytes.byteLength||((i=new Uint8Array(2*s)).set(t.bytes.subarray(0,t.position),0),t.bytes=i,t.view=new DataView(t.bytes.buffer))},o=t.widthBytes||new Uint8Array("width".length),h=t.heightBytes||new Uint8Array("height".length),p=t.videocodecidBytes||new Uint8Array("videocodecid".length);if(!t.widthBytes){for(s=0;s<"width".length;s++)o[s]="width".charCodeAt(s);for(s=0;s<"height".length;s++)h[s]="height".charCodeAt(s);for(s=0;s<"videocodecid".length;s++)p[s]="videocodecid".charCodeAt(s);t.widthBytes=o,t.heightBytes=h,t.videocodecidBytes=p}switch(this.keyFrame=!1,e){case t.VIDEO_TAG:this.length=16,n*=6;break;case t.AUDIO_TAG:this.length=13,this.keyFrame=!0;break;case t.METADATA_TAG:this.length=29,this.keyFrame=!0;break;default:throw new Error("Unknown FLV tag type")}this.bytes=new Uint8Array(n),this.view=new DataView(this.bytes.buffer),this.bytes[0]=e,this.position=this.length,this.keyFrame=i,this.pts=0,this.dts=0,this.writeBytes=function(t,e,i){var s,a=e||0;s=a+(i=i||t.byteLength),r(this,i),this.bytes.set(t.subarray(a,s),this.position),this.position+=i,this.length=Math.max(this.length,this.position)},this.writeByte=function(t){r(this,1),this.bytes[this.position]=t,this.position++,this.length=Math.max(this.length,this.position)},this.writeShort=function(t){r(this,2),this.view.setUint16(this.position,t),this.position+=2,this.length=Math.max(this.length,this.position)},this.negIndex=function(t){return this.bytes[this.length-t]},this.nalUnitSize=function(){return 0===a?0:this.length-(a+4)},this.startNalUnit=function(){if(a>0)throw new Error("Attempted to create new NAL wihout closing the old one");a=this.length,this.length+=4,this.position=this.length},this.endNalUnit=function(t){var e,i;this.length===a+4?this.length-=4:a>0&&(e=a+4,i=this.length-e,this.position=a,this.view.setUint32(this.position,i),this.position=this.length,t&&t.push(this.bytes.subarray(e,e+i))),a=0},this.writeMetaDataDouble=function(t,e){var i;if(r(this,2+t.length+9),this.view.setUint16(this.position,t.length),this.position+=2,"width"===t)this.bytes.set(o,this.position),this.position+=5;else if("height"===t)this.bytes.set(h,this.position),this.position+=6;else if("videocodecid"===t)this.bytes.set(p,this.position),this.position+=12;else for(i=0;i<t.length;i++)this.bytes[this.position]=t.charCodeAt(i),this.position++;this.position++,this.view.setFloat64(this.position,e),this.position+=8,this.length=Math.max(this.length,this.position),++a},this.writeMetaDataBoolean=function(t,e){var i;for(r(this,2),this.view.setUint16(this.position,t.length),this.position+=2,i=0;i<t.length;i++)r(this,1),this.bytes[this.position]=t.charCodeAt(i),this.position++;r(this,2),this.view.setUint8(this.position,1),this.position++,this.view.setUint8(this.position,e?1:0),this.position++,this.length=Math.max(this.length,this.position),++a},this.finalize=function(){var e,s;switch(this.bytes[0]){case t.VIDEO_TAG:this.bytes[11]=7|(this.keyFrame||i?16:32),this.bytes[12]=i?0:1,e=this.pts-this.dts,this.bytes[13]=(16711680&e)>>>16,this.bytes[14]=(65280&e)>>>8,this.bytes[15]=(255&e)>>>0;break;case t.AUDIO_TAG:this.bytes[11]=175,this.bytes[12]=i?0:1;break;case t.METADATA_TAG:this.position=11,this.view.setUint8(this.position,2),this.position++,this.view.setUint16(this.position,10),this.position+=2,this.bytes.set([111,110,77,101,116,97,68,97,116,97],this.position),this.position+=10,this.bytes[this.position]=8,this.position++,this.view.setUint32(this.position,a),this.position=this.length,this.bytes.set([0,0,9],this.position),this.position+=3,this.length=this.position}return s=this.length-11,this.bytes[1]=(16711680&s)>>>16,this.bytes[2]=(65280&s)>>>8,this.bytes[3]=(255&s)>>>0,this.bytes[4]=(16711680&this.dts)>>>16,this.bytes[5]=(65280&this.dts)>>>8,this.bytes[6]=(255&this.dts)>>>0,this.bytes[7]=(4278190080&this.dts)>>>24,this.bytes[8]=0,this.bytes[9]=0,this.bytes[10]=0,r(this,4),this.view.setUint32(this.length,this.length),this.length+=4,this.position+=4,this.bytes=this.bytes.subarray(0,this.length),this.frameTime=t.frameTime(this.bytes),this}}).AUDIO_TAG=8,t.VIDEO_TAG=9,t.METADATA_TAG=18,t.isAudioFrame=function(e){return t.AUDIO_TAG===e[0]},t.isVideoFrame=function(e){return t.VIDEO_TAG===e[0]},t.isMetaData=function(e){return t.METADATA_TAG===e[0]},t.isKeyFrame=function(e){return t.isVideoFrame(e)?23===e[11]:!!t.isAudioFrame(e)||!!t.isMetaData(e)},t.frameTime=function(t){var e=t[4]<<16;return e|=t[5]<<8,e|=t[6]<<0,e|=t[7]<<24};var e=t,i=function(){this.init=function(){var t={};this.on=function(e,i){t[e]||(t[e]=[]),t[e]=t[e].concat(i)},this.off=function(e,i){var s;return!!t[e]&&(s=t[e].indexOf(i),t[e]=t[e].slice(),t[e].splice(s,1),s>-1)},this.trigger=function(e){var i,s,a,n;if(i=t[e])if(2===arguments.length)for(a=i.length,s=0;s<a;++s)i[s].call(this,arguments[1]);else{for(n=[],s=arguments.length,s=1;s<arguments.length;++s)n.push(arguments[s]);for(a=i.length,s=0;s<a;++s)i[s].apply(this,n)}},this.dispose=function(){t={}}}};i.prototype.pipe=function(t){return this.on("data",(function(e){t.push(e)})),this.on("done",(function(e){t.flush(e)})),this.on("partialdone",(function(e){t.partialFlush(e)})),this.on("endedtimeline",(function(e){t.endTimeline(e)})),this.on("reset",(function(e){t.reset(e)})),t},i.prototype.push=function(t){this.trigger("data",t)},i.prototype.flush=function(t){this.trigger("done",t)},i.prototype.partialFlush=function(t){this.trigger("partialdone",t)},i.prototype.endTimeline=function(t){this.trigger("endedtimeline",t)},i.prototype.reset=function(t){this.trigger("reset",t)};var s=i,a=function(t){for(var e=0,i={payloadType:-1,payloadSize:0},s=0,a=0;e<t.byteLength&&128!==t[e];){for(;255===t[e];)s+=255,e++;for(s+=t[e++];255===t[e];)a+=255,e++;if(a+=t[e++],!i.payload&&4===s){if("GA94"===String.fromCharCode(t[e+3],t[e+4],t[e+5],t[e+6])){i.payloadType=s,i.payloadSize=a,i.payload=t.subarray(e,e+a);break}i.payload=void 0}e+=a,s=0,a=0}return i},n=function(t){return 181!==t.payload[0]||49!=(t.payload[1]<<8|t.payload[2])||"GA94"!==String.fromCharCode(t.payload[3],t.payload[4],t.payload[5],t.payload[6])||3!==t.payload[7]?null:t.payload.subarray(8,t.payload.length-1)},r=function(t,e){var i,s,a,n,r=[];if(!(64&e[0]))return r;for(s=31&e[0],i=0;i<s;i++)n={type:3&e[(a=3*i)+2],pts:t},4&e[a+2]&&(n.ccData=e[a+3]<<8|e[a+4],r.push(n));return r},o=4,h=function t(e){e=e||{},t.prototype.init.call(this),this.parse708captions_="boolean"!=typeof e.parse708captions||e.parse708captions,this.captionPackets_=[],this.ccStreams_=[new _(0,0),new _(0,1),new _(1,0),new _(1,1)],this.parse708captions_&&(this.cc708Stream_=new u({captionServices:e.captionServices})),this.reset(),this.ccStreams_.forEach((function(t){t.on("data",this.trigger.bind(this,"data")),t.on("partialdone",this.trigger.bind(this,"partialdone")),t.on("done",this.trigger.bind(this,"done"))}),this),this.parse708captions_&&(this.cc708Stream_.on("data",this.trigger.bind(this,"data")),this.cc708Stream_.on("partialdone",this.trigger.bind(this,"partialdone")),this.cc708Stream_.on("done",this.trigger.bind(this,"done")))};(h.prototype=new s).push=function(t){var e,i,s;if("sei_rbsp"===t.nalUnitType&&(e=a(t.escapedRBSP)).payload&&e.payloadType===o&&(i=n(e)))if(t.dts<this.latestDts_)this.ignoreNextEqualDts_=!0;else{if(t.dts===this.latestDts_&&this.ignoreNextEqualDts_)return this.numSameDts_--,void(this.numSameDts_||(this.ignoreNextEqualDts_=!1));s=r(t.pts,i),this.captionPackets_=this.captionPackets_.concat(s),this.latestDts_!==t.dts&&(this.numSameDts_=0),this.numSameDts_++,this.latestDts_=t.dts}},h.prototype.flushCCStreams=function(t){this.ccStreams_.forEach((function(e){return"flush"===t?e.flush():e.partialFlush()}),this)},h.prototype.flushStream=function(t){this.captionPackets_.length?(this.captionPackets_.forEach((function(t,e){t.presortIndex=e})),this.captionPackets_.sort((function(t,e){return t.pts===e.pts?t.presortIndex-e.presortIndex:t.pts-e.pts})),this.captionPackets_.forEach((function(t){t.type<2?this.dispatchCea608Packet(t):this.dispatchCea708Packet(t)}),this),this.captionPackets_.length=0,this.flushCCStreams(t)):this.flushCCStreams(t)},h.prototype.flush=function(){return this.flushStream("flush")},h.prototype.partialFlush=function(){return this.flushStream("partialFlush")},h.prototype.reset=function(){this.latestDts_=null,this.ignoreNextEqualDts_=!1,this.numSameDts_=0,this.activeCea608Channel_=[null,null],this.ccStreams_.forEach((function(t){t.reset()}))},h.prototype.dispatchCea608Packet=function(t){this.setsTextOrXDSActive(t)?this.activeCea608Channel_[t.type]=null:this.setsChannel1Active(t)?this.activeCea608Channel_[t.type]=0:this.setsChannel2Active(t)&&(this.activeCea608Channel_[t.type]=1),null!==this.activeCea608Channel_[t.type]&&this.ccStreams_[(t.type<<1)+this.activeCea608Channel_[t.type]].push(t)},h.prototype.setsChannel1Active=function(t){return 4096==(30720&t.ccData)},h.prototype.setsChannel2Active=function(t){return 6144==(30720&t.ccData)},h.prototype.setsTextOrXDSActive=function(t){return 256==(28928&t.ccData)||4138==(30974&t.ccData)||6186==(30974&t.ccData)},h.prototype.dispatchCea708Packet=function(t){this.parse708captions_&&this.cc708Stream_.push(t)};var p={127:9834,4128:32,4129:160,4133:8230,4138:352,4140:338,4144:9608,4145:8216,4146:8217,4147:8220,4148:8221,4149:8226,4153:8482,4154:353,4156:339,4157:8480,4159:376,4214:8539,4215:8540,4216:8541,4217:8542,4218:9168,4219:9124,4220:9123,4221:9135,4222:9126,4223:9121,4256:12600},d=function(t){return 32<=t&&t<=127||160<=t&&t<=255},l=function(t){this.windowNum=t,this.reset()};l.prototype.reset=function(){this.clearText(),this.pendingNewLine=!1,this.winAttr={},this.penAttr={},this.penLoc={},this.penColor={},this.visible=0,this.rowLock=0,this.columnLock=0,this.priority=0,this.relativePositioning=0,this.anchorVertical=0,this.anchorHorizontal=0,this.anchorPoint=0,this.rowCount=1,this.virtualRowCount=this.rowCount+1,this.columnCount=41,this.windowStyle=0,this.penStyle=0},l.prototype.getText=function(){return this.rows.join("\n")},l.prototype.clearText=function(){this.rows=[""],this.rowIdx=0},l.prototype.newLine=function(t){for(this.rows.length>=this.virtualRowCount&&"function"==typeof this.beforeRowOverflow&&this.beforeRowOverflow(t),this.rows.length>0&&(this.rows.push(""),this.rowIdx++);this.rows.length>this.virtualRowCount;)this.rows.shift(),this.rowIdx--},l.prototype.isEmpty=function(){return 0===this.rows.length||1===this.rows.length&&""===this.rows[0]},l.prototype.addText=function(t){this.rows[this.rowIdx]+=t},l.prototype.backspace=function(){if(!this.isEmpty()){var t=this.rows[this.rowIdx];this.rows[this.rowIdx]=t.substr(0,t.length-1)}};var c=function(t,e,i){this.serviceNum=t,this.text="",this.currentWindow=new l(-1),this.windows=[],this.stream=i,"string"==typeof e&&this.createTextDecoder(e)};c.prototype.init=function(t,e){this.startPts=t;for(var i=0;i<8;i++)this.windows[i]=new l(i),"function"==typeof e&&(this.windows[i].beforeRowOverflow=e)},c.prototype.setCurrentWindow=function(t){this.currentWindow=this.windows[t]},c.prototype.createTextDecoder=function(t){if("undefined"==typeof TextDecoder)this.stream.trigger("log",{level:"warn",message:"The `encoding` option is unsupported without TextDecoder support"});else try{this.textDecoder_=new TextDecoder(t)}catch(e){this.stream.trigger("log",{level:"warn",message:"TextDecoder could not be created with "+t+" encoding. "+e})}};var u=function t(e){e=e||{},t.prototype.init.call(this);var i,s=this,a=e.captionServices||{},n={};Object.keys(a).forEach((function(t){i=a[t],/^SERVICE/.test(t)&&(n[t]=i.encoding)})),this.serviceEncodings=n,this.current708Packet=null,this.services={},this.push=function(t){3===t.type?(s.new708Packet(),s.add708Bytes(t)):(null===s.current708Packet&&s.new708Packet(),s.add708Bytes(t))}};u.prototype=new s,u.prototype.new708Packet=function(){null!==this.current708Packet&&this.push708Packet(),this.current708Packet={data:[],ptsVals:[]}},u.prototype.add708Bytes=function(t){var e=t.ccData,i=e>>>8,s=255&e;this.current708Packet.ptsVals.push(t.pts),this.current708Packet.data.push(i),this.current708Packet.data.push(s)},u.prototype.push708Packet=function(){var t=this.current708Packet,e=t.data,i=null,s=null,a=0,n=e[a++];for(t.seq=n>>6,t.sizeCode=63&n;a<e.length;a++)s=31&(n=e[a++]),7===(i=n>>5)&&s>0&&(i=n=e[a++]),this.pushServiceBlock(i,a,s),s>0&&(a+=s-1)},u.prototype.pushServiceBlock=function(t,e,i){var s,a=e,n=this.current708Packet.data,r=this.services[t];for(r||(r=this.initService(t,a));a<e+i&&a<n.length;a++)s=n[a],d(s)?a=this.handleText(a,r):24===s?a=this.multiByteCharacter(a,r):16===s?a=this.extendedCommands(a,r):128<=s&&s<=135?a=this.setCurrentWindow(a,r):152<=s&&s<=159?a=this.defineWindow(a,r):136===s?a=this.clearWindows(a,r):140===s?a=this.deleteWindows(a,r):137===s?a=this.displayWindows(a,r):138===s?a=this.hideWindows(a,r):139===s?a=this.toggleWindows(a,r):151===s?a=this.setWindowAttributes(a,r):144===s?a=this.setPenAttributes(a,r):145===s?a=this.setPenColor(a,r):146===s?a=this.setPenLocation(a,r):143===s?r=this.reset(a,r):8===s?r.currentWindow.backspace():12===s?r.currentWindow.clearText():13===s?r.currentWindow.pendingNewLine=!0:14===s?r.currentWindow.clearText():141===s&&a++},u.prototype.extendedCommands=function(t,e){var i=this.current708Packet.data[++t];return d(i)&&(t=this.handleText(t,e,{isExtended:!0})),t},u.prototype.getPts=function(t){return this.current708Packet.ptsVals[Math.floor(t/2)]},u.prototype.initService=function(t,e){var i,s,a=this;return(i="SERVICE"+t)in this.serviceEncodings&&(s=this.serviceEncodings[i]),this.services[t]=new c(t,s,a),this.services[t].init(this.getPts(e),(function(e){a.flushDisplayed(e,a.services[t])})),this.services[t]},u.prototype.handleText=function(t,e,i){var s,a,n,r,o=i&&i.isExtended,h=i&&i.isMultiByte,d=this.current708Packet.data,l=o?4096:0,c=d[t],u=d[t+1],f=e.currentWindow;return e.textDecoder_&&!o?(h?(a=[c,u],t++):a=[c],s=e.textDecoder_.decode(new Uint8Array(a))):(r=p[n=l|c]||n,s=4096&n&&n===r?"":String.fromCharCode(r)),f.pendingNewLine&&!f.isEmpty()&&f.newLine(this.getPts(t)),f.pendingNewLine=!1,f.addText(s),t},u.prototype.multiByteCharacter=function(t,e){var i=this.current708Packet.data,s=i[t+1],a=i[t+2];return d(s)&&d(a)&&(t=this.handleText(++t,e,{isMultiByte:!0})),t},u.prototype.setCurrentWindow=function(t,e){var i=7&this.current708Packet.data[t];return e.setCurrentWindow(i),t},u.prototype.defineWindow=function(t,e){var i=this.current708Packet.data,s=i[t],a=7&s;e.setCurrentWindow(a);var n=e.currentWindow;return s=i[++t],n.visible=(32&s)>>5,n.rowLock=(16&s)>>4,n.columnLock=(8&s)>>3,n.priority=7&s,s=i[++t],n.relativePositioning=(128&s)>>7,n.anchorVertical=127&s,s=i[++t],n.anchorHorizontal=s,s=i[++t],n.anchorPoint=(240&s)>>4,n.rowCount=15&s,s=i[++t],n.columnCount=63&s,s=i[++t],n.windowStyle=(56&s)>>3,n.penStyle=7&s,n.virtualRowCount=n.rowCount+1,t},u.prototype.setWindowAttributes=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.winAttr;return s=i[++t],a.fillOpacity=(192&s)>>6,a.fillRed=(48&s)>>4,a.fillGreen=(12&s)>>2,a.fillBlue=3&s,s=i[++t],a.borderType=(192&s)>>6,a.borderRed=(48&s)>>4,a.borderGreen=(12&s)>>2,a.borderBlue=3&s,s=i[++t],a.borderType+=(128&s)>>5,a.wordWrap=(64&s)>>6,a.printDirection=(48&s)>>4,a.scrollDirection=(12&s)>>2,a.justify=3&s,s=i[++t],a.effectSpeed=(240&s)>>4,a.effectDirection=(12&s)>>2,a.displayEffect=3&s,t},u.prototype.flushDisplayed=function(t,e){for(var i=[],s=0;s<8;s++)e.windows[s].visible&&!e.windows[s].isEmpty()&&i.push(e.windows[s].getText());e.endPts=t,e.text=i.join("\n\n"),this.pushCaption(e),e.startPts=t},u.prototype.pushCaption=function(t){""!==t.text&&(this.trigger("data",{startPts:t.startPts,endPts:t.endPts,text:t.text,stream:"cc708_"+t.serviceNum}),t.text="",t.startPts=t.endPts)},u.prototype.displayWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible=1);return t},u.prototype.hideWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible=0);return t},u.prototype.toggleWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible^=1);return t},u.prototype.clearWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&e.windows[a].clearText();return t},u.prototype.deleteWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&e.windows[a].reset();return t},u.prototype.setPenAttributes=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penAttr;return s=i[++t],a.textTag=(240&s)>>4,a.offset=(12&s)>>2,a.penSize=3&s,s=i[++t],a.italics=(128&s)>>7,a.underline=(64&s)>>6,a.edgeType=(56&s)>>3,a.fontStyle=7&s,t},u.prototype.setPenColor=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penColor;return s=i[++t],a.fgOpacity=(192&s)>>6,a.fgRed=(48&s)>>4,a.fgGreen=(12&s)>>2,a.fgBlue=3&s,s=i[++t],a.bgOpacity=(192&s)>>6,a.bgRed=(48&s)>>4,a.bgGreen=(12&s)>>2,a.bgBlue=3&s,s=i[++t],a.edgeRed=(48&s)>>4,a.edgeGreen=(12&s)>>2,a.edgeBlue=3&s,t},u.prototype.setPenLocation=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penLoc;return e.currentWindow.pendingNewLine=!0,s=i[++t],a.row=15&s,s=i[++t],a.column=63&s,t},u.prototype.reset=function(t,e){var i=this.getPts(t);return this.flushDisplayed(i,e),this.initService(e.serviceNum,t)};var f={42:225,92:233,94:237,95:243,96:250,123:231,124:247,125:209,126:241,127:9608,304:174,305:176,306:189,307:191,308:8482,309:162,310:163,311:9834,312:224,313:160,314:232,315:226,316:234,317:238,318:244,319:251,544:193,545:201,546:211,547:218,548:220,549:252,550:8216,551:161,552:42,553:39,554:8212,555:169,556:8480,557:8226,558:8220,559:8221,560:192,561:194,562:199,563:200,564:202,565:203,566:235,567:206,568:207,569:239,570:212,571:217,572:249,573:219,574:171,575:187,800:195,801:227,802:205,803:204,804:236,805:210,806:242,807:213,808:245,809:123,810:125,811:92,812:94,813:95,814:124,815:126,816:196,817:228,818:214,819:246,820:223,821:165,822:164,823:9474,824:197,825:229,826:216,827:248,828:9484,829:9488,830:9492,831:9496},g=function(t){return null===t?"":(t=f[t]||t,String.fromCharCode(t))},y=[4352,4384,4608,4640,5376,5408,5632,5664,5888,5920,4096,4864,4896,5120,5152],m=function(){for(var t=[],e=15;e--;)t.push("");return t},_=function t(e,i){t.prototype.init.call(this),this.field_=e||0,this.dataChannel_=i||0,this.name_="CC"+(1+(this.field_<<1|this.dataChannel_)),this.setConstants(),this.reset(),this.push=function(t){var e,i,s,a,n;if((e=32639&t.ccData)!==this.lastControlCode_){if(4096==(61440&e)?this.lastControlCode_=e:e!==this.PADDING_&&(this.lastControlCode_=null),s=e>>>8,a=255&e,e!==this.PADDING_)if(e===this.RESUME_CAPTION_LOADING_)this.mode_="popOn";else if(e===this.END_OF_CAPTION_)this.mode_="popOn",this.clearFormatting(t.pts),this.flushDisplayed(t.pts),i=this.displayed_,this.displayed_=this.nonDisplayed_,this.nonDisplayed_=i,this.startPts_=t.pts;else if(e===this.ROLL_UP_2_ROWS_)this.rollUpRows_=2,this.setRollUp(t.pts);else if(e===this.ROLL_UP_3_ROWS_)this.rollUpRows_=3,this.setRollUp(t.pts);else if(e===this.ROLL_UP_4_ROWS_)this.rollUpRows_=4,this.setRollUp(t.pts);else if(e===this.CARRIAGE_RETURN_)this.clearFormatting(t.pts),this.flushDisplayed(t.pts),this.shiftRowsUp_(),this.startPts_=t.pts;else if(e===this.BACKSPACE_)"popOn"===this.mode_?this.nonDisplayed_[this.row_]=this.nonDisplayed_[this.row_].slice(0,-1):this.displayed_[this.row_]=this.displayed_[this.row_].slice(0,-1);else if(e===this.ERASE_DISPLAYED_MEMORY_)this.flushDisplayed(t.pts),this.displayed_=m();else if(e===this.ERASE_NON_DISPLAYED_MEMORY_)this.nonDisplayed_=m();else if(e===this.RESUME_DIRECT_CAPTIONING_)"paintOn"!==this.mode_&&(this.flushDisplayed(t.pts),this.displayed_=m()),this.mode_="paintOn",this.startPts_=t.pts;else if(this.isSpecialCharacter(s,a))n=g((s=(3&s)<<8)|a),this[this.mode_](t.pts,n),this.column_++;else if(this.isExtCharacter(s,a))"popOn"===this.mode_?this.nonDisplayed_[this.row_]=this.nonDisplayed_[this.row_].slice(0,-1):this.displayed_[this.row_]=this.displayed_[this.row_].slice(0,-1),n=g((s=(3&s)<<8)|a),this[this.mode_](t.pts,n),this.column_++;else if(this.isMidRowCode(s,a))this.clearFormatting(t.pts),this[this.mode_](t.pts," "),this.column_++,14==(14&a)&&this.addFormatting(t.pts,["i"]),1==(1&a)&&this.addFormatting(t.pts,["u"]);else if(this.isOffsetControlCode(s,a))this.column_+=3&a;else if(this.isPAC(s,a)){var r=y.indexOf(7968&e);"rollUp"===this.mode_&&(r-this.rollUpRows_+1<0&&(r=this.rollUpRows_-1),this.setRollUp(t.pts,r)),r!==this.row_&&(this.clearFormatting(t.pts),this.row_=r),1&a&&-1===this.formatting_.indexOf("u")&&this.addFormatting(t.pts,["u"]),16==(16&e)&&(this.column_=4*((14&e)>>1)),this.isColorPAC(a)&&14==(14&a)&&this.addFormatting(t.pts,["i"])}else this.isNormalChar(s)&&(0===a&&(a=null),n=g(s),n+=g(a),this[this.mode_](t.pts,n),this.column_+=n.length)}else this.lastControlCode_=null}};_.prototype=new s,_.prototype.flushDisplayed=function(t){var e=this.displayed_.map((function(t,e){try{return t.trim()}catch(t){return this.trigger("log",{level:"warn",message:"Skipping a malformed 608 caption at index "+e+"."}),""}}),this).join("\n").replace(/^\n+|\n+$/g,"");e.length&&this.trigger("data",{startPts:this.startPts_,endPts:t,text:e,stream:this.name_})},_.prototype.reset=function(){this.mode_="popOn",this.topRow_=0,this.startPts_=0,this.displayed_=m(),this.nonDisplayed_=m(),this.lastControlCode_=null,this.column_=0,this.row_=14,this.rollUpRows_=2,this.formatting_=[]},_.prototype.setConstants=function(){0===this.dataChannel_?(this.BASE_=16,this.EXT_=17,this.CONTROL_=(20|this.field_)<<8,this.OFFSET_=23):1===this.dataChannel_&&(this.BASE_=24,this.EXT_=25,this.CONTROL_=(28|this.field_)<<8,this.OFFSET_=31),this.PADDING_=0,this.RESUME_CAPTION_LOADING_=32|this.CONTROL_,this.END_OF_CAPTION_=47|this.CONTROL_,this.ROLL_UP_2_ROWS_=37|this.CONTROL_,this.ROLL_UP_3_ROWS_=38|this.CONTROL_,this.ROLL_UP_4_ROWS_=39|this.CONTROL_,this.CARRIAGE_RETURN_=45|this.CONTROL_,this.RESUME_DIRECT_CAPTIONING_=41|this.CONTROL_,this.BACKSPACE_=33|this.CONTROL_,this.ERASE_DISPLAYED_MEMORY_=44|this.CONTROL_,this.ERASE_NON_DISPLAYED_MEMORY_=46|this.CONTROL_},_.prototype.isSpecialCharacter=function(t,e){return t===this.EXT_&&e>=48&&e<=63},_.prototype.isExtCharacter=function(t,e){return(t===this.EXT_+1||t===this.EXT_+2)&&e>=32&&e<=63},_.prototype.isMidRowCode=function(t,e){return t===this.EXT_&&e>=32&&e<=47},_.prototype.isOffsetControlCode=function(t,e){return t===this.OFFSET_&&e>=33&&e<=35},_.prototype.isPAC=function(t,e){return t>=this.BASE_&&t<this.BASE_+8&&e>=64&&e<=127},_.prototype.isColorPAC=function(t){return t>=64&&t<=79||t>=96&&t<=127},_.prototype.isNormalChar=function(t){return t>=32&&t<=127},_.prototype.setRollUp=function(t,e){if("rollUp"!==this.mode_&&(this.row_=14,this.mode_="rollUp",this.flushDisplayed(t),this.nonDisplayed_=m(),this.displayed_=m()),void 0!==e&&e!==this.row_)for(var i=0;i<this.rollUpRows_;i++)this.displayed_[e-i]=this.displayed_[this.row_-i],this.displayed_[this.row_-i]="";void 0===e&&(e=this.row_),this.topRow_=e-this.rollUpRows_+1},_.prototype.addFormatting=function(t,e){this.formatting_=this.formatting_.concat(e);var i=e.reduce((function(t,e){return t+"<"+e+">"}),"");this[this.mode_](t,i)},_.prototype.clearFormatting=function(t){if(this.formatting_.length){var e=this.formatting_.reverse().reduce((function(t,e){return t+"</"+e+">"}),"");this.formatting_=[],this[this.mode_](t,e)}},_.prototype.popOn=function(t,e){var i=this.nonDisplayed_[this.row_];i+=e,this.nonDisplayed_[this.row_]=i},_.prototype.rollUp=function(t,e){var i=this.displayed_[this.row_];i+=e,this.displayed_[this.row_]=i},_.prototype.shiftRowsUp_=function(){var t;for(t=0;t<this.topRow_;t++)this.displayed_[t]="";for(t=this.row_+1;t<15;t++)this.displayed_[t]="";for(t=this.topRow_;t<this.row_;t++)this.displayed_[t]=this.displayed_[t+1];this.displayed_[this.row_]=""},_.prototype.paintOn=function(t,e){var i=this.displayed_[this.row_];i+=e,this.displayed_[this.row_]=i};var w={CaptionStream:h,Cea608Stream:_,Cea708Stream:u},b={H264_STREAM_TYPE:27,ADTS_STREAM_TYPE:15,METADATA_STREAM_TYPE:21},v="shared",T=function(t,e){var i=1;for(t>e&&(i=-1);Math.abs(e-t)>4294967296;)t+=8589934592*i;return t},k=function t(e){var i,s;t.prototype.init.call(this),this.type_=e||v,this.push=function(t){this.type_!==v&&t.type!==this.type_||(void 0===s&&(s=t.dts),t.dts=T(t.dts,s),t.pts=T(t.pts,s),i=t.dts,this.trigger("data",t))},this.flush=function(){s=i,this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")},this.discontinuity=function(){s=void 0,i=void 0},this.reset=function(){this.discontinuity(),this.trigger("reset")}};k.prototype=new s;var S,A=k,C=function(t,e,i){if(!t)return-1;for(var s=i;s<t.length;s++)if(t[s]===e)return s;return-1},D=3,E=function(t,e,i){var s,a="";for(s=e;s<i;s++)a+="%"+("00"+t[s].toString(16)).slice(-2);return a},P=function(t,e,i){return decodeURIComponent(E(t,e,i))},U=function(t,e,i){return unescape(E(t,e,i))},R=function(t){return t[0]<<21|t[1]<<14|t[2]<<7|t[3]},M={APIC:function(t){var e,i,s=1;t.data[0]===D&&((e=C(t.data,0,s))<0||(t.mimeType=U(t.data,s,e),s=e+1,t.pictureType=t.data[s],s++,(i=C(t.data,0,s))<0||(t.description=P(t.data,s,i),s=i+1,"--\x3e"===t.mimeType?t.url=U(t.data,s,t.data.length):t.pictureData=t.data.subarray(s,t.data.length))))},"T*":function(t){t.data[0]===D&&(t.value=P(t.data,1,t.data.length).replace(/\0*$/,""),t.values=t.value.split("\0"))},TXXX:function(t){var e;t.data[0]===D&&-1!==(e=C(t.data,0,1))&&(t.description=P(t.data,1,e),t.value=P(t.data,e+1,t.data.length).replace(/\0*$/,""),t.data=t.value)},"W*":function(t){t.url=U(t.data,0,t.data.length).replace(/\0.*$/,"")},WXXX:function(t){var e;t.data[0]===D&&-1!==(e=C(t.data,0,1))&&(t.description=P(t.data,1,e),t.url=U(t.data,e+1,t.data.length).replace(/\0.*$/,""))},PRIV:function(t){var e;for(e=0;e<t.data.length;e++)if(0===t.data[e]){t.owner=U(t.data,0,e);break}t.privateData=t.data.subarray(e+1),t.data=t.privateData}};(S=function(t){var e,i={descriptor:t&&t.descriptor},s=0,a=[],n=0;if(S.prototype.init.call(this),this.dispatchType=b.METADATA_STREAM_TYPE.toString(16),i.descriptor)for(e=0;e<i.descriptor.length;e++)this.dispatchType+=("00"+i.descriptor[e].toString(16)).slice(-2);this.push=function(t){var e,i,r,o,h;if("timed-metadata"===t.type)if(t.dataAlignmentIndicator&&(n=0,a.length=0),0===a.length&&(t.data.length<10||t.data[0]!=="I".charCodeAt(0)||t.data[1]!=="D".charCodeAt(0)||t.data[2]!=="3".charCodeAt(0)))this.trigger("log",{level:"warn",message:"Skipping unrecognized metadata packet"});else if(a.push(t),n+=t.data.byteLength,1===a.length&&(s=R(t.data.subarray(6,10)),s+=10),!(n<s)){for(e={data:new Uint8Array(s),frames:[],pts:a[0].pts,dts:a[0].dts},h=0;h<s;)e.data.set(a[0].data.subarray(0,s-h),h),h+=a[0].data.byteLength,n-=a[0].data.byteLength,a.shift();i=10,64&e.data[5]&&(i+=4,i+=R(e.data.subarray(10,14)),s-=R(e.data.subarray(16,20)));do{if((r=R(e.data.subarray(i+4,i+8)))<1){this.trigger("log",{level:"warn",message:"Malformed ID3 frame encountered. Skipping remaining metadata parsing."});break}if((o={id:String.fromCharCode(e.data[i],e.data[i+1],e.data[i+2],e.data[i+3]),data:e.data.subarray(i+10,i+r+10)}).key=o.id,M[o.id]?M[o.id](o):"T"===o.id[0]?M["T*"](o):"W"===o.id[0]&&M["W*"](o),"com.apple.streaming.transportStreamTimestamp"===o.owner){var p=o.data,d=(1&p[3])<<30|p[4]<<22|p[5]<<14|p[6]<<6|p[7]>>>2;d*=4,d+=3&p[7],o.timeStamp=d,void 0===e.pts&&void 0===e.dts&&(e.pts=o.timeStamp,e.dts=o.timeStamp),this.trigger("timestamp",o)}e.frames.push(o),i+=10,i+=r}while(i<s);this.trigger("data",e)}}}).prototype=new s;var x,O,L,I=S,B=A,G=188;(x=function(){var t=new Uint8Array(G),e=0;x.prototype.init.call(this),this.push=function(i){var s,a=0,n=G;for(e?((s=new Uint8Array(i.byteLength+e)).set(t.subarray(0,e)),s.set(i,e),e=0):s=i;n<s.byteLength;)71!==s[a]||71!==s[n]?(a++,n++):(this.trigger("data",s.subarray(a,n)),a+=G,n+=G);a<s.byteLength&&(t.set(s.subarray(a),0),e=s.byteLength-a)},this.flush=function(){e===G&&71===t[0]&&(this.trigger("data",t),e=0),this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")},this.reset=function(){e=0,this.trigger("reset")}}).prototype=new s,(O=function(){var t,e,i,s;O.prototype.init.call(this),s=this,this.packetsWaitingForPmt=[],this.programMapTable=void 0,t=function(t,s){var a=0;s.payloadUnitStartIndicator&&(a+=t[a]+1),"pat"===s.type?e(t.subarray(a),s):i(t.subarray(a),s)},e=function(t,e){e.section_number=t[7],e.last_section_number=t[8],s.pmtPid=(31&t[10])<<8|t[11],e.pmtPid=s.pmtPid},i=function(t,e){var i,a;if(1&t[5]){for(s.programMapTable={video:null,audio:null,"timed-metadata":{}},i=3+((15&t[1])<<8|t[2])-4,a=12+((15&t[10])<<8|t[11]);a<i;){var n=t[a],r=(31&t[a+1])<<8|t[a+2];n===b.H264_STREAM_TYPE&&null===s.programMapTable.video?s.programMapTable.video=r:n===b.ADTS_STREAM_TYPE&&null===s.programMapTable.audio?s.programMapTable.audio=r:n===b.METADATA_STREAM_TYPE&&(s.programMapTable["timed-metadata"][r]=n),a+=5+((15&t[a+3])<<8|t[a+4])}e.programMapTable=s.programMapTable}},this.push=function(e){var i={},s=4;if(i.payloadUnitStartIndicator=!!(64&e[1]),i.pid=31&e[1],i.pid<<=8,i.pid|=e[2],(48&e[3])>>>4>1&&(s+=e[s]+1),0===i.pid)i.type="pat",t(e.subarray(s),i),this.trigger("data",i);else if(i.pid===this.pmtPid)for(i.type="pmt",t(e.subarray(s),i),this.trigger("data",i);this.packetsWaitingForPmt.length;)this.processPes_.apply(this,this.packetsWaitingForPmt.shift());else void 0===this.programMapTable?this.packetsWaitingForPmt.push([e,s,i]):this.processPes_(e,s,i)},this.processPes_=function(t,e,i){i.pid===this.programMapTable.video?i.streamType=b.H264_STREAM_TYPE:i.pid===this.programMapTable.audio?i.streamType=b.ADTS_STREAM_TYPE:i.streamType=this.programMapTable["timed-metadata"][i.pid],i.type="pes",i.data=t.subarray(e),this.trigger("data",i)}}).prototype=new s,O.STREAM_TYPES={h264:27,adts:15},(L=function(){var t,e=this,i=!1,s={data:[],size:0},a={data:[],size:0},n={data:[],size:0},r=function(t,i,s){var a,n,r=new Uint8Array(t.size),o={type:i},h=0,p=0;if(t.data.length&&!(t.size<9)){for(o.trackId=t.data[0].pid,h=0;h<t.data.length;h++)n=t.data[h],r.set(n.data,p),p+=n.data.byteLength;var d,l,c,u;l=o,u=(d=r)[0]<<16|d[1]<<8|d[2],l.data=new Uint8Array,1===u&&(l.packetLength=6+(d[4]<<8|d[5]),l.dataAlignmentIndicator=0!=(4&d[6]),192&(c=d[7])&&(l.pts=(14&d[9])<<27|(255&d[10])<<20|(254&d[11])<<12|(255&d[12])<<5|(254&d[13])>>>3,l.pts*=4,l.pts+=(6&d[13])>>>1,l.dts=l.pts,64&c&&(l.dts=(14&d[14])<<27|(255&d[15])<<20|(254&d[16])<<12|(255&d[17])<<5|(254&d[18])>>>3,l.dts*=4,l.dts+=(6&d[18])>>>1)),l.data=d.subarray(9+d[8])),a="video"===i||o.packetLength<=t.size,(s||a)&&(t.size=0,t.data.length=0),a&&e.trigger("data",o)}};L.prototype.init.call(this),this.push=function(o){({pat:function(){},pes:function(){var t,e;switch(o.streamType){case b.H264_STREAM_TYPE:t=s,e="video";break;case b.ADTS_STREAM_TYPE:t=a,e="audio";break;case b.METADATA_STREAM_TYPE:t=n,e="timed-metadata";break;default:return}o.payloadUnitStartIndicator&&r(t,e,!0),t.data.push(o),t.size+=o.data.byteLength},pmt:function(){var s={type:"metadata",tracks:[]};null!==(t=o.programMapTable).video&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.video,codec:"avc",type:"video"}),null!==t.audio&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.audio,codec:"adts",type:"audio"}),i=!0,e.trigger("data",s)}})[o.type]()},this.reset=function(){s.size=0,s.data.length=0,a.size=0,a.data.length=0,this.trigger("reset")},this.flushStreams_=function(){r(s,"video"),r(a,"audio"),r(n,"timed-metadata")},this.flush=function(){if(!i&&t){var s={type:"metadata",tracks:[]};null!==t.video&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.video,codec:"avc",type:"video"}),null!==t.audio&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.audio,codec:"adts",type:"audio"}),e.trigger("data",s)}i=!1,this.flushStreams_(),this.trigger("done")}}).prototype=new s;var N={PAT_PID:0,MP2T_PACKET_LENGTH:G,TransportPacketStream:x,TransportParseStream:O,ElementaryStream:L,TimestampRolloverStream:B,CaptionStream:w.CaptionStream,Cea608Stream:w.Cea608Stream,Cea708Stream:w.Cea708Stream,MetadataStream:I};for(var W in b)b.hasOwnProperty(W)&&(N[W]=b[W]);var F,z,V,Y,X=N,j=9e4;F=function(t){return t*j},z=function(t,e){return t*e},V=function(t){return t/j},Y=function(t,e){return t/e};var q,H=j,K=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];(q=function(t){var e,i=0;q.prototype.init.call(this),this.skipWarn_=function(t,e){this.trigger("log",{level:"warn",message:"adts skiping bytes "+t+" to "+e+" in frame "+i+" outside syncword"})},this.push=function(s){var a,n,r,o,h,p=0;if(t||(i=0),"audio"===s.type){var d;for(e&&e.length?(r=e,(e=new Uint8Array(r.byteLength+s.data.byteLength)).set(r),e.set(s.data,r.byteLength)):e=s.data;p+7<e.length;)if(255===e[p]&&240==(246&e[p+1])){if("number"==typeof d&&(this.skipWarn_(d,p),d=null),n=2*(1&~e[p+1]),a=(3&e[p+3])<<11|e[p+4]<<3|(224&e[p+5])>>5,h=(o=1024*(1+(3&e[p+6])))*H/K[(60&e[p+2])>>>2],e.byteLength-p<a)break;this.trigger("data",{pts:s.pts+i*h,dts:s.dts+i*h,sampleCount:o,audioobjecttype:1+(e[p+2]>>>6&3),channelcount:(1&e[p+2])<<2|(192&e[p+3])>>>6,samplerate:K[(60&e[p+2])>>>2],samplingfrequencyindex:(60&e[p+2])>>>2,samplesize:16,data:e.subarray(p+7+n,p+a)}),i++,p+=a}else"number"!=typeof d&&(d=p),p++;"number"==typeof d&&(this.skipWarn_(d,p),d=null),e=e.subarray(p)}},this.flush=function(){i=0,this.trigger("done")},this.reset=function(){e=void 0,this.trigger("reset")},this.endTimeline=function(){e=void 0,this.trigger("endedtimeline")}}).prototype=new s;var Z,$,J,Q=q,tt=function(t){var e=t.byteLength,i=0,s=0;this.length=function(){return 8*e},this.bitsAvailable=function(){return 8*e+s},this.loadWord=function(){var a=t.byteLength-e,n=new Uint8Array(4),r=Math.min(4,e);if(0===r)throw new Error("no bytes available");n.set(t.subarray(a,a+r)),i=new DataView(n.buffer).getUint32(0),s=8*r,e-=r},this.skipBits=function(t){var a;s>t?(i<<=t,s-=t):(t-=s,t-=8*(a=Math.floor(t/8)),e-=a,this.loadWord(),i<<=t,s-=t)},this.readBits=function(t){var a=Math.min(s,t),n=i>>>32-a;return(s-=a)>0?i<<=a:e>0&&this.loadWord(),(a=t-a)>0?n<<a|this.readBits(a):n},this.skipLeadingZeros=function(){var t;for(t=0;t<s;++t)if(0!=(i&2147483648>>>t))return i<<=t,s-=t,t;return this.loadWord(),t+this.skipLeadingZeros()},this.skipUnsignedExpGolomb=function(){this.skipBits(1+this.skipLeadingZeros())},this.skipExpGolomb=function(){this.skipBits(1+this.skipLeadingZeros())},this.readUnsignedExpGolomb=function(){var t=this.skipLeadingZeros();return this.readBits(t+1)-1},this.readExpGolomb=function(){var t=this.readUnsignedExpGolomb();return 1&t?1+t>>>1:-1*(t>>>1)},this.readBoolean=function(){return 1===this.readBits(1)},this.readUnsignedByte=function(){return this.readBits(8)},this.loadWord()};($=function(){var t,e,i=0;$.prototype.init.call(this),this.push=function(s){var a;e?((a=new Uint8Array(e.byteLength+s.data.byteLength)).set(e),a.set(s.data,e.byteLength),e=a):e=s.data;for(var n=e.byteLength;i<n-3;i++)if(1===e[i+2]){t=i+5;break}for(;t<n;)switch(e[t]){case 0:if(0!==e[t-1]){t+=2;break}if(0!==e[t-2]){t++;break}i+3!==t-2&&this.trigger("data",e.subarray(i+3,t-2));do{t++}while(1!==e[t]&&t<n);i=t-2,t+=3;break;case 1:if(0!==e[t-1]||0!==e[t-2]){t+=3;break}this.trigger("data",e.subarray(i+3,t-2)),i=t-2,t+=3;break;default:t+=3}e=e.subarray(i),t-=i,i=0},this.reset=function(){e=null,i=0,this.trigger("reset")},this.flush=function(){e&&e.byteLength>3&&this.trigger("data",e.subarray(i+3)),e=null,i=0,this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")}}).prototype=new s,J={100:!0,110:!0,122:!0,244:!0,44:!0,83:!0,86:!0,118:!0,128:!0,138:!0,139:!0,134:!0},(Z=function(){var t,e,i,s,a,n,r,o=new $;Z.prototype.init.call(this),t=this,this.push=function(t){"video"===t.type&&(e=t.trackId,i=t.pts,s=t.dts,o.push(t))},o.on("data",(function(r){var o={trackId:e,pts:i,dts:s,data:r,nalUnitTypeCode:31&r[0]};switch(o.nalUnitTypeCode){case 5:o.nalUnitType="slice_layer_without_partitioning_rbsp_idr";break;case 6:o.nalUnitType="sei_rbsp",o.escapedRBSP=a(r.subarray(1));break;case 7:o.nalUnitType="seq_parameter_set_rbsp",o.escapedRBSP=a(r.subarray(1)),o.config=n(o.escapedRBSP);break;case 8:o.nalUnitType="pic_parameter_set_rbsp";break;case 9:o.nalUnitType="access_unit_delimiter_rbsp"}t.trigger("data",o)})),o.on("done",(function(){t.trigger("done")})),o.on("partialdone",(function(){t.trigger("partialdone")})),o.on("reset",(function(){t.trigger("reset")})),o.on("endedtimeline",(function(){t.trigger("endedtimeline")})),this.flush=function(){o.flush()},this.partialFlush=function(){o.partialFlush()},this.reset=function(){o.reset()},this.endTimeline=function(){o.endTimeline()},r=function(t,e){var i,s=8,a=8;for(i=0;i<t;i++)0!==a&&(a=(s+e.readExpGolomb()+256)%256),s=0===a?s:a},a=function(t){for(var e,i,s=t.byteLength,a=[],n=1;n<s-2;)0===t[n]&&0===t[n+1]&&3===t[n+2]?(a.push(n+2),n+=2):n++;if(0===a.length)return t;e=s-a.length,i=new Uint8Array(e);var r=0;for(n=0;n<e;r++,n++)r===a[0]&&(r++,a.shift()),i[n]=t[r];return i},n=function(t){var e,i,s,a,n,o,h,p,d,l,c,u,f=0,g=0,y=0,m=0,_=[1,1];if(i=(e=new tt(t)).readUnsignedByte(),a=e.readUnsignedByte(),s=e.readUnsignedByte(),e.skipUnsignedExpGolomb(),J[i]&&(3===(n=e.readUnsignedExpGolomb())&&e.skipBits(1),e.skipUnsignedExpGolomb(),e.skipUnsignedExpGolomb(),e.skipBits(1),e.readBoolean()))for(c=3!==n?8:12,u=0;u<c;u++)e.readBoolean()&&r(u<6?16:64,e);if(e.skipUnsignedExpGolomb(),0===(o=e.readUnsignedExpGolomb()))e.readUnsignedExpGolomb();else if(1===o)for(e.skipBits(1),e.skipExpGolomb(),e.skipExpGolomb(),h=e.readUnsignedExpGolomb(),u=0;u<h;u++)e.skipExpGolomb();if(e.skipUnsignedExpGolomb(),e.skipBits(1),p=e.readUnsignedExpGolomb(),d=e.readUnsignedExpGolomb(),0===(l=e.readBits(1))&&e.skipBits(1),e.skipBits(1),e.readBoolean()&&(f=e.readUnsignedExpGolomb(),g=e.readUnsignedExpGolomb(),y=e.readUnsignedExpGolomb(),m=e.readUnsignedExpGolomb()),e.readBoolean()&&e.readBoolean()){switch(e.readUnsignedByte()){case 1:_=[1,1];break;case 2:_=[12,11];break;case 3:_=[10,11];break;case 4:_=[16,11];break;case 5:_=[40,33];break;case 6:_=[24,11];break;case 7:_=[20,11];break;case 8:_=[32,11];break;case 9:_=[80,33];break;case 10:_=[18,11];break;case 11:_=[15,11];break;case 12:_=[64,33];break;case 13:_=[160,99];break;case 14:_=[4,3];break;case 15:_=[3,2];break;case 16:_=[2,1];break;case 255:_=[e.readUnsignedByte()<<8|e.readUnsignedByte(),e.readUnsignedByte()<<8|e.readUnsignedByte()]}_&&(_[0],_[1])}return{profileIdc:i,levelIdc:s,profileCompatibility:a,width:16*(p+1)-2*f-2*g,height:(2-l)*(d+1)*16-2*y-2*m,sarRatio:_}}}).prototype=new s;var et={H264Stream:Z,NalByteStream:$},it=function t(e){this.numberOfTracks=0,this.metadataStream=e.metadataStream,this.videoTags=[],this.audioTags=[],this.videoTrack=null,this.audioTrack=null,this.pendingCaptions=[],this.pendingMetadata=[],this.pendingTracks=0,this.processedTracks=0,t.prototype.init.call(this),this.push=function(t){return t.text?this.pendingCaptions.push(t):t.frames?this.pendingMetadata.push(t):("video"===t.track.type&&(this.videoTrack=t.track,this.videoTags=t.tags,this.pendingTracks++),void("audio"===t.track.type&&(this.audioTrack=t.track,this.audioTags=t.tags,this.pendingTracks++)))}};(it.prototype=new s).flush=function(t){var e,i,s,a,n={tags:{},captions:[],captionStreams:{},metadata:[]};if(this.pendingTracks<this.numberOfTracks){if("VideoSegmentStream"!==t&&"AudioSegmentStream"!==t)return;if(0===this.pendingTracks&&(this.processedTracks++,this.processedTracks<this.numberOfTracks))return}if(this.processedTracks+=this.pendingTracks,this.pendingTracks=0,!(this.processedTracks<this.numberOfTracks)){for(this.videoTrack?a=this.videoTrack.timelineStartInfo.pts:this.audioTrack&&(a=this.audioTrack.timelineStartInfo.pts),n.tags.videoTags=this.videoTags,n.tags.audioTags=this.audioTags,s=0;s<this.pendingCaptions.length;s++)(i=this.pendingCaptions[s]).startTime=i.startPts-a,i.startTime/=9e4,i.endTime=i.endPts-a,i.endTime/=9e4,n.captionStreams[i.stream]=!0,n.captions.push(i);for(s=0;s<this.pendingMetadata.length;s++)(e=this.pendingMetadata[s]).cueTime=e.pts-a,e.cueTime/=9e4,n.metadata.push(e);n.metadata.dispatchType=this.metadataStream.dispatchType,this.videoTrack=null,this.audioTrack=null,this.videoTags=[],this.audioTags=[],this.pendingCaptions.length=0,this.pendingMetadata.length=0,this.pendingTracks=0,this.processedTracks=0,this.trigger("data",n),this.trigger("done")}};var st,at,nt,rt,ot,ht,pt=it,dt=function(){var t=this;this.list=[],this.push=function(t){this.list.push({bytes:t.bytes,dts:t.dts,pts:t.pts,keyFrame:t.keyFrame,metaDataTag:t.metaDataTag})},Object.defineProperty(this,"length",{get:function(){return t.list.length}})},lt=et.H264Stream;rt=function(t,e){"number"==typeof e.pts&&(void 0===t.timelineStartInfo.pts?t.timelineStartInfo.pts=e.pts:t.timelineStartInfo.pts=Math.min(t.timelineStartInfo.pts,e.pts)),"number"==typeof e.dts&&(void 0===t.timelineStartInfo.dts?t.timelineStartInfo.dts=e.dts:t.timelineStartInfo.dts=Math.min(t.timelineStartInfo.dts,e.dts))},ot=function(t,i){var s=new e(e.METADATA_TAG);return s.dts=i,s.pts=i,s.writeMetaDataDouble("videocodecid",7),s.writeMetaDataDouble("width",t.width),s.writeMetaDataDouble("height",t.height),s},ht=function(t,i){var s,a=new e(e.VIDEO_TAG,!0);for(a.dts=i,a.pts=i,a.writeByte(1),a.writeByte(t.profileIdc),a.writeByte(t.profileCompatibility),a.writeByte(t.levelIdc),a.writeByte(255),a.writeByte(225),a.writeShort(t.sps[0].length),a.writeBytes(t.sps[0]),a.writeByte(t.pps.length),s=0;s<t.pps.length;++s)a.writeShort(t.pps[s].length),a.writeBytes(t.pps[s]);return a},(nt=function(t){var i,s=[],a=[];nt.prototype.init.call(this),this.push=function(e){rt(t,e),t&&(t.audioobjecttype=e.audioobjecttype,t.channelcount=e.channelcount,t.samplerate=e.samplerate,t.samplingfrequencyindex=e.samplingfrequencyindex,t.samplesize=e.samplesize,t.extraData=t.audioobjecttype<<11|t.samplingfrequencyindex<<7|t.channelcount<<3),e.pts=Math.round(e.pts/90),e.dts=Math.round(e.dts/90),s.push(e)},this.flush=function(){var n,r,o,h=new dt;if(0!==s.length){for(o=-1/0;s.length;)n=s.shift(),a.length&&n.pts>=a[0]&&(o=a.shift(),this.writeMetaDataTags(h,o)),(t.extraData!==i||n.pts-o>=1e3)&&(this.writeMetaDataTags(h,n.pts),i=t.extraData,o=n.pts),(r=new e(e.AUDIO_TAG)).pts=n.pts,r.dts=n.dts,r.writeBytes(n.data),h.push(r.finalize());a.length=0,i=null,this.trigger("data",{track:t,tags:h.list}),this.trigger("done","AudioSegmentStream")}else this.trigger("done","AudioSegmentStream")},this.writeMetaDataTags=function(i,s){var a;(a=new e(e.METADATA_TAG)).pts=s,a.dts=s,a.writeMetaDataDouble("audiocodecid",10),a.writeMetaDataBoolean("stereo",2===t.channelcount),a.writeMetaDataDouble("audiosamplerate",t.samplerate),a.writeMetaDataDouble("audiosamplesize",16),i.push(a.finalize()),(a=new e(e.AUDIO_TAG,!0)).pts=s,a.dts=s,a.view.setUint16(a.position,t.extraData),a.position+=2,a.length=Math.max(a.length,a.position),i.push(a.finalize())},this.onVideoKeyFrame=function(t){a.push(t)}}).prototype=new s,(at=function(t){var i,s,a=[];at.prototype.init.call(this),this.finishFrame=function(e,a){if(a){if(i&&t&&t.newMetadata&&(a.keyFrame||0===e.length)){var n=ot(i,a.dts).finalize(),r=ht(t,a.dts).finalize();n.metaDataTag=r.metaDataTag=!0,e.push(n),e.push(r),t.newMetadata=!1,this.trigger("keyframe",a.dts)}a.endNalUnit(),e.push(a.finalize()),s=null}},this.push=function(e){rt(t,e),e.pts=Math.round(e.pts/90),e.dts=Math.round(e.dts/90),a.push(e)},this.flush=function(){for(var n,r=new dt;a.length&&"access_unit_delimiter_rbsp"!==a[0].nalUnitType;)a.shift();if(0!==a.length){for(;a.length;)"seq_parameter_set_rbsp"===(n=a.shift()).nalUnitType?(t.newMetadata=!0,i=n.config,t.width=i.width,t.height=i.height,t.sps=[n.data],t.profileIdc=i.profileIdc,t.levelIdc=i.levelIdc,t.profileCompatibility=i.profileCompatibility,s.endNalUnit()):"pic_parameter_set_rbsp"===n.nalUnitType?(t.newMetadata=!0,t.pps=[n.data],s.endNalUnit()):"access_unit_delimiter_rbsp"===n.nalUnitType?(s&&this.finishFrame(r,s),(s=new e(e.VIDEO_TAG)).pts=n.pts,s.dts=n.dts):("slice_layer_without_partitioning_rbsp_idr"===n.nalUnitType&&(s.keyFrame=!0),s.endNalUnit()),s.startNalUnit(),s.writeBytes(n.data);s&&this.finishFrame(r,s),this.trigger("data",{track:t,tags:r.list}),this.trigger("done","VideoSegmentStream")}else this.trigger("done","VideoSegmentStream")}}).prototype=new s,(st=function(t){var e,i,s,a,n,r,o,h,p,d,l,c,u=this;st.prototype.init.call(this),t=t||{},this.metadataStream=new X.MetadataStream,t.metadataStream=this.metadataStream,e=new X.TransportPacketStream,i=new X.TransportParseStream,s=new X.ElementaryStream,a=new X.TimestampRolloverStream("video"),n=new X.TimestampRolloverStream("audio"),r=new X.TimestampRolloverStream("timed-metadata"),o=new Q,h=new lt,c=new pt(t),e.pipe(i).pipe(s),s.pipe(a).pipe(h),s.pipe(n).pipe(o),s.pipe(r).pipe(this.metadataStream).pipe(c),l=new X.CaptionStream(t),h.pipe(l).pipe(c),s.on("data",(function(t){var e,i,s;if("metadata"===t.type){for(e=t.tracks.length;e--;)"video"===t.tracks[e].type?i=t.tracks[e]:"audio"===t.tracks[e].type&&(s=t.tracks[e]);i&&!p&&(c.numberOfTracks++,p=new at(i),h.pipe(p).pipe(c)),s&&!d&&(c.numberOfTracks++,d=new nt(s),o.pipe(d).pipe(c),p&&p.on("keyframe",d.onVideoKeyFrame))}})),this.push=function(t){e.push(t)},this.flush=function(){e.flush()},this.resetCaptions=function(){l.reset()},c.on("data",(function(t){u.trigger("data",t)})),c.on("done",(function(){u.trigger("done")}))}).prototype=new s;var ct=function(t,i,s){var a,n,r,o=new Uint8Array(9),h=new DataView(o.buffer);return t=t||0,i=void 0===i||i,s=void 0===s||s,h.setUint8(0,70),h.setUint8(1,76),h.setUint8(2,86),h.setUint8(3,1),h.setUint8(4,(i?4:0)|(s?1:0)),h.setUint32(5,o.byteLength),t<=0?((n=new Uint8Array(o.byteLength+4)).set(o),n.set([0,0,0,0],o.byteLength),n):((a=new e(e.METADATA_TAG)).pts=a.dts=0,a.writeMetaDataDouble("duration",t),r=a.finalize().length,(n=new Uint8Array(o.byteLength+r)).set(o),n.set(h.byteLength,r),n)};return{tag:e,Transmuxer:st,getFlvHeader:ct}})); | ||
/*! @name mux.js @version 6.3.0 @license Apache-2.0 */ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).muxjs=e()}(this,(function(){"use strict";var t;(t=function(e,i){var s,a=0,n=16384,r=function(t,e){var i,s=t.position+e;s<t.bytes.byteLength||((i=new Uint8Array(2*s)).set(t.bytes.subarray(0,t.position),0),t.bytes=i,t.view=new DataView(t.bytes.buffer))},o=t.widthBytes||new Uint8Array("width".length),h=t.heightBytes||new Uint8Array("height".length),p=t.videocodecidBytes||new Uint8Array("videocodecid".length);if(!t.widthBytes){for(s=0;s<"width".length;s++)o[s]="width".charCodeAt(s);for(s=0;s<"height".length;s++)h[s]="height".charCodeAt(s);for(s=0;s<"videocodecid".length;s++)p[s]="videocodecid".charCodeAt(s);t.widthBytes=o,t.heightBytes=h,t.videocodecidBytes=p}switch(this.keyFrame=!1,e){case t.VIDEO_TAG:this.length=16,n*=6;break;case t.AUDIO_TAG:this.length=13,this.keyFrame=!0;break;case t.METADATA_TAG:this.length=29,this.keyFrame=!0;break;default:throw new Error("Unknown FLV tag type")}this.bytes=new Uint8Array(n),this.view=new DataView(this.bytes.buffer),this.bytes[0]=e,this.position=this.length,this.keyFrame=i,this.pts=0,this.dts=0,this.writeBytes=function(t,e,i){var s,a=e||0;s=a+(i=i||t.byteLength),r(this,i),this.bytes.set(t.subarray(a,s),this.position),this.position+=i,this.length=Math.max(this.length,this.position)},this.writeByte=function(t){r(this,1),this.bytes[this.position]=t,this.position++,this.length=Math.max(this.length,this.position)},this.writeShort=function(t){r(this,2),this.view.setUint16(this.position,t),this.position+=2,this.length=Math.max(this.length,this.position)},this.negIndex=function(t){return this.bytes[this.length-t]},this.nalUnitSize=function(){return 0===a?0:this.length-(a+4)},this.startNalUnit=function(){if(a>0)throw new Error("Attempted to create new NAL wihout closing the old one");a=this.length,this.length+=4,this.position=this.length},this.endNalUnit=function(t){var e,i;this.length===a+4?this.length-=4:a>0&&(e=a+4,i=this.length-e,this.position=a,this.view.setUint32(this.position,i),this.position=this.length,t&&t.push(this.bytes.subarray(e,e+i))),a=0},this.writeMetaDataDouble=function(t,e){var i;if(r(this,2+t.length+9),this.view.setUint16(this.position,t.length),this.position+=2,"width"===t)this.bytes.set(o,this.position),this.position+=5;else if("height"===t)this.bytes.set(h,this.position),this.position+=6;else if("videocodecid"===t)this.bytes.set(p,this.position),this.position+=12;else for(i=0;i<t.length;i++)this.bytes[this.position]=t.charCodeAt(i),this.position++;this.position++,this.view.setFloat64(this.position,e),this.position+=8,this.length=Math.max(this.length,this.position),++a},this.writeMetaDataBoolean=function(t,e){var i;for(r(this,2),this.view.setUint16(this.position,t.length),this.position+=2,i=0;i<t.length;i++)r(this,1),this.bytes[this.position]=t.charCodeAt(i),this.position++;r(this,2),this.view.setUint8(this.position,1),this.position++,this.view.setUint8(this.position,e?1:0),this.position++,this.length=Math.max(this.length,this.position),++a},this.finalize=function(){var e,s;switch(this.bytes[0]){case t.VIDEO_TAG:this.bytes[11]=7|(this.keyFrame||i?16:32),this.bytes[12]=i?0:1,e=this.pts-this.dts,this.bytes[13]=(16711680&e)>>>16,this.bytes[14]=(65280&e)>>>8,this.bytes[15]=(255&e)>>>0;break;case t.AUDIO_TAG:this.bytes[11]=175,this.bytes[12]=i?0:1;break;case t.METADATA_TAG:this.position=11,this.view.setUint8(this.position,2),this.position++,this.view.setUint16(this.position,10),this.position+=2,this.bytes.set([111,110,77,101,116,97,68,97,116,97],this.position),this.position+=10,this.bytes[this.position]=8,this.position++,this.view.setUint32(this.position,a),this.position=this.length,this.bytes.set([0,0,9],this.position),this.position+=3,this.length=this.position}return s=this.length-11,this.bytes[1]=(16711680&s)>>>16,this.bytes[2]=(65280&s)>>>8,this.bytes[3]=(255&s)>>>0,this.bytes[4]=(16711680&this.dts)>>>16,this.bytes[5]=(65280&this.dts)>>>8,this.bytes[6]=(255&this.dts)>>>0,this.bytes[7]=(4278190080&this.dts)>>>24,this.bytes[8]=0,this.bytes[9]=0,this.bytes[10]=0,r(this,4),this.view.setUint32(this.length,this.length),this.length+=4,this.position+=4,this.bytes=this.bytes.subarray(0,this.length),this.frameTime=t.frameTime(this.bytes),this}}).AUDIO_TAG=8,t.VIDEO_TAG=9,t.METADATA_TAG=18,t.isAudioFrame=function(e){return t.AUDIO_TAG===e[0]},t.isVideoFrame=function(e){return t.VIDEO_TAG===e[0]},t.isMetaData=function(e){return t.METADATA_TAG===e[0]},t.isKeyFrame=function(e){return t.isVideoFrame(e)?23===e[11]:!!t.isAudioFrame(e)||!!t.isMetaData(e)},t.frameTime=function(t){var e=t[4]<<16;return e|=t[5]<<8,e|=t[6]<<0,e|=t[7]<<24};var e=t,i=function(){this.init=function(){var t={};this.on=function(e,i){t[e]||(t[e]=[]),t[e]=t[e].concat(i)},this.off=function(e,i){var s;return!!t[e]&&(s=t[e].indexOf(i),t[e]=t[e].slice(),t[e].splice(s,1),s>-1)},this.trigger=function(e){var i,s,a,n;if(i=t[e])if(2===arguments.length)for(a=i.length,s=0;s<a;++s)i[s].call(this,arguments[1]);else{for(n=[],s=arguments.length,s=1;s<arguments.length;++s)n.push(arguments[s]);for(a=i.length,s=0;s<a;++s)i[s].apply(this,n)}},this.dispose=function(){t={}}}};i.prototype.pipe=function(t){return this.on("data",(function(e){t.push(e)})),this.on("done",(function(e){t.flush(e)})),this.on("partialdone",(function(e){t.partialFlush(e)})),this.on("endedtimeline",(function(e){t.endTimeline(e)})),this.on("reset",(function(e){t.reset(e)})),t},i.prototype.push=function(t){this.trigger("data",t)},i.prototype.flush=function(t){this.trigger("done",t)},i.prototype.partialFlush=function(t){this.trigger("partialdone",t)},i.prototype.endTimeline=function(t){this.trigger("endedtimeline",t)},i.prototype.reset=function(t){this.trigger("reset",t)};var s=i,a=function(t){for(var e=0,i={payloadType:-1,payloadSize:0},s=0,a=0;e<t.byteLength&&128!==t[e];){for(;255===t[e];)s+=255,e++;for(s+=t[e++];255===t[e];)a+=255,e++;if(a+=t[e++],!i.payload&&4===s){if("GA94"===String.fromCharCode(t[e+3],t[e+4],t[e+5],t[e+6])){i.payloadType=s,i.payloadSize=a,i.payload=t.subarray(e,e+a);break}i.payload=void 0}e+=a,s=0,a=0}return i},n=function(t){return 181!==t.payload[0]||49!=(t.payload[1]<<8|t.payload[2])||"GA94"!==String.fromCharCode(t.payload[3],t.payload[4],t.payload[5],t.payload[6])||3!==t.payload[7]?null:t.payload.subarray(8,t.payload.length-1)},r=function(t,e){var i,s,a,n,r=[];if(!(64&e[0]))return r;for(s=31&e[0],i=0;i<s;i++)n={type:3&e[(a=3*i)+2],pts:t},4&e[a+2]&&(n.ccData=e[a+3]<<8|e[a+4],r.push(n));return r},o=4,h=function t(e){e=e||{},t.prototype.init.call(this),this.parse708captions_="boolean"!=typeof e.parse708captions||e.parse708captions,this.captionPackets_=[],this.ccStreams_=[new _(0,0),new _(0,1),new _(1,0),new _(1,1)],this.parse708captions_&&(this.cc708Stream_=new u({captionServices:e.captionServices})),this.reset(),this.ccStreams_.forEach((function(t){t.on("data",this.trigger.bind(this,"data")),t.on("partialdone",this.trigger.bind(this,"partialdone")),t.on("done",this.trigger.bind(this,"done"))}),this),this.parse708captions_&&(this.cc708Stream_.on("data",this.trigger.bind(this,"data")),this.cc708Stream_.on("partialdone",this.trigger.bind(this,"partialdone")),this.cc708Stream_.on("done",this.trigger.bind(this,"done")))};(h.prototype=new s).push=function(t){var e,i,s;if("sei_rbsp"===t.nalUnitType&&(e=a(t.escapedRBSP)).payload&&e.payloadType===o&&(i=n(e)))if(t.dts<this.latestDts_)this.ignoreNextEqualDts_=!0;else{if(t.dts===this.latestDts_&&this.ignoreNextEqualDts_)return this.numSameDts_--,void(this.numSameDts_||(this.ignoreNextEqualDts_=!1));s=r(t.pts,i),this.captionPackets_=this.captionPackets_.concat(s),this.latestDts_!==t.dts&&(this.numSameDts_=0),this.numSameDts_++,this.latestDts_=t.dts}},h.prototype.flushCCStreams=function(t){this.ccStreams_.forEach((function(e){return"flush"===t?e.flush():e.partialFlush()}),this)},h.prototype.flushStream=function(t){this.captionPackets_.length?(this.captionPackets_.forEach((function(t,e){t.presortIndex=e})),this.captionPackets_.sort((function(t,e){return t.pts===e.pts?t.presortIndex-e.presortIndex:t.pts-e.pts})),this.captionPackets_.forEach((function(t){t.type<2?this.dispatchCea608Packet(t):this.dispatchCea708Packet(t)}),this),this.captionPackets_.length=0,this.flushCCStreams(t)):this.flushCCStreams(t)},h.prototype.flush=function(){return this.flushStream("flush")},h.prototype.partialFlush=function(){return this.flushStream("partialFlush")},h.prototype.reset=function(){this.latestDts_=null,this.ignoreNextEqualDts_=!1,this.numSameDts_=0,this.activeCea608Channel_=[null,null],this.ccStreams_.forEach((function(t){t.reset()}))},h.prototype.dispatchCea608Packet=function(t){this.setsTextOrXDSActive(t)?this.activeCea608Channel_[t.type]=null:this.setsChannel1Active(t)?this.activeCea608Channel_[t.type]=0:this.setsChannel2Active(t)&&(this.activeCea608Channel_[t.type]=1),null!==this.activeCea608Channel_[t.type]&&this.ccStreams_[(t.type<<1)+this.activeCea608Channel_[t.type]].push(t)},h.prototype.setsChannel1Active=function(t){return 4096==(30720&t.ccData)},h.prototype.setsChannel2Active=function(t){return 6144==(30720&t.ccData)},h.prototype.setsTextOrXDSActive=function(t){return 256==(28928&t.ccData)||4138==(30974&t.ccData)||6186==(30974&t.ccData)},h.prototype.dispatchCea708Packet=function(t){this.parse708captions_&&this.cc708Stream_.push(t)};var p={127:9834,4128:32,4129:160,4133:8230,4138:352,4140:338,4144:9608,4145:8216,4146:8217,4147:8220,4148:8221,4149:8226,4153:8482,4154:353,4156:339,4157:8480,4159:376,4214:8539,4215:8540,4216:8541,4217:8542,4218:9168,4219:9124,4220:9123,4221:9135,4222:9126,4223:9121,4256:12600},d=function(t){return 32<=t&&t<=127||160<=t&&t<=255},l=function(t){this.windowNum=t,this.reset()};l.prototype.reset=function(){this.clearText(),this.pendingNewLine=!1,this.winAttr={},this.penAttr={},this.penLoc={},this.penColor={},this.visible=0,this.rowLock=0,this.columnLock=0,this.priority=0,this.relativePositioning=0,this.anchorVertical=0,this.anchorHorizontal=0,this.anchorPoint=0,this.rowCount=1,this.virtualRowCount=this.rowCount+1,this.columnCount=41,this.windowStyle=0,this.penStyle=0},l.prototype.getText=function(){return this.rows.join("\n")},l.prototype.clearText=function(){this.rows=[""],this.rowIdx=0},l.prototype.newLine=function(t){for(this.rows.length>=this.virtualRowCount&&"function"==typeof this.beforeRowOverflow&&this.beforeRowOverflow(t),this.rows.length>0&&(this.rows.push(""),this.rowIdx++);this.rows.length>this.virtualRowCount;)this.rows.shift(),this.rowIdx--},l.prototype.isEmpty=function(){return 0===this.rows.length||1===this.rows.length&&""===this.rows[0]},l.prototype.addText=function(t){this.rows[this.rowIdx]+=t},l.prototype.backspace=function(){if(!this.isEmpty()){var t=this.rows[this.rowIdx];this.rows[this.rowIdx]=t.substr(0,t.length-1)}};var c=function(t,e,i){this.serviceNum=t,this.text="",this.currentWindow=new l(-1),this.windows=[],this.stream=i,"string"==typeof e&&this.createTextDecoder(e)};c.prototype.init=function(t,e){this.startPts=t;for(var i=0;i<8;i++)this.windows[i]=new l(i),"function"==typeof e&&(this.windows[i].beforeRowOverflow=e)},c.prototype.setCurrentWindow=function(t){this.currentWindow=this.windows[t]},c.prototype.createTextDecoder=function(t){if("undefined"==typeof TextDecoder)this.stream.trigger("log",{level:"warn",message:"The `encoding` option is unsupported without TextDecoder support"});else try{this.textDecoder_=new TextDecoder(t)}catch(e){this.stream.trigger("log",{level:"warn",message:"TextDecoder could not be created with "+t+" encoding. "+e})}};var u=function t(e){e=e||{},t.prototype.init.call(this);var i,s=this,a=e.captionServices||{},n={};Object.keys(a).forEach((function(t){i=a[t],/^SERVICE/.test(t)&&(n[t]=i.encoding)})),this.serviceEncodings=n,this.current708Packet=null,this.services={},this.push=function(t){3===t.type?(s.new708Packet(),s.add708Bytes(t)):(null===s.current708Packet&&s.new708Packet(),s.add708Bytes(t))}};u.prototype=new s,u.prototype.new708Packet=function(){null!==this.current708Packet&&this.push708Packet(),this.current708Packet={data:[],ptsVals:[]}},u.prototype.add708Bytes=function(t){var e=t.ccData,i=e>>>8,s=255&e;this.current708Packet.ptsVals.push(t.pts),this.current708Packet.data.push(i),this.current708Packet.data.push(s)},u.prototype.push708Packet=function(){var t=this.current708Packet,e=t.data,i=null,s=null,a=0,n=e[a++];for(t.seq=n>>6,t.sizeCode=63&n;a<e.length;a++)s=31&(n=e[a++]),7===(i=n>>5)&&s>0&&(i=n=e[a++]),this.pushServiceBlock(i,a,s),s>0&&(a+=s-1)},u.prototype.pushServiceBlock=function(t,e,i){var s,a=e,n=this.current708Packet.data,r=this.services[t];for(r||(r=this.initService(t,a));a<e+i&&a<n.length;a++)s=n[a],d(s)?a=this.handleText(a,r):24===s?a=this.multiByteCharacter(a,r):16===s?a=this.extendedCommands(a,r):128<=s&&s<=135?a=this.setCurrentWindow(a,r):152<=s&&s<=159?a=this.defineWindow(a,r):136===s?a=this.clearWindows(a,r):140===s?a=this.deleteWindows(a,r):137===s?a=this.displayWindows(a,r):138===s?a=this.hideWindows(a,r):139===s?a=this.toggleWindows(a,r):151===s?a=this.setWindowAttributes(a,r):144===s?a=this.setPenAttributes(a,r):145===s?a=this.setPenColor(a,r):146===s?a=this.setPenLocation(a,r):143===s?r=this.reset(a,r):8===s?r.currentWindow.backspace():12===s?r.currentWindow.clearText():13===s?r.currentWindow.pendingNewLine=!0:14===s?r.currentWindow.clearText():141===s&&a++},u.prototype.extendedCommands=function(t,e){var i=this.current708Packet.data[++t];return d(i)&&(t=this.handleText(t,e,{isExtended:!0})),t},u.prototype.getPts=function(t){return this.current708Packet.ptsVals[Math.floor(t/2)]},u.prototype.initService=function(t,e){var i,s,a=this;return(i="SERVICE"+t)in this.serviceEncodings&&(s=this.serviceEncodings[i]),this.services[t]=new c(t,s,a),this.services[t].init(this.getPts(e),(function(e){a.flushDisplayed(e,a.services[t])})),this.services[t]},u.prototype.handleText=function(t,e,i){var s,a,n,r,o=i&&i.isExtended,h=i&&i.isMultiByte,d=this.current708Packet.data,l=o?4096:0,c=d[t],u=d[t+1],f=e.currentWindow;return e.textDecoder_&&!o?(h?(a=[c,u],t++):a=[c],s=e.textDecoder_.decode(new Uint8Array(a))):(r=p[n=l|c]||n,s=4096&n&&n===r?"":String.fromCharCode(r)),f.pendingNewLine&&!f.isEmpty()&&f.newLine(this.getPts(t)),f.pendingNewLine=!1,f.addText(s),t},u.prototype.multiByteCharacter=function(t,e){var i=this.current708Packet.data,s=i[t+1],a=i[t+2];return d(s)&&d(a)&&(t=this.handleText(++t,e,{isMultiByte:!0})),t},u.prototype.setCurrentWindow=function(t,e){var i=7&this.current708Packet.data[t];return e.setCurrentWindow(i),t},u.prototype.defineWindow=function(t,e){var i=this.current708Packet.data,s=i[t],a=7&s;e.setCurrentWindow(a);var n=e.currentWindow;return s=i[++t],n.visible=(32&s)>>5,n.rowLock=(16&s)>>4,n.columnLock=(8&s)>>3,n.priority=7&s,s=i[++t],n.relativePositioning=(128&s)>>7,n.anchorVertical=127&s,s=i[++t],n.anchorHorizontal=s,s=i[++t],n.anchorPoint=(240&s)>>4,n.rowCount=15&s,s=i[++t],n.columnCount=63&s,s=i[++t],n.windowStyle=(56&s)>>3,n.penStyle=7&s,n.virtualRowCount=n.rowCount+1,t},u.prototype.setWindowAttributes=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.winAttr;return s=i[++t],a.fillOpacity=(192&s)>>6,a.fillRed=(48&s)>>4,a.fillGreen=(12&s)>>2,a.fillBlue=3&s,s=i[++t],a.borderType=(192&s)>>6,a.borderRed=(48&s)>>4,a.borderGreen=(12&s)>>2,a.borderBlue=3&s,s=i[++t],a.borderType+=(128&s)>>5,a.wordWrap=(64&s)>>6,a.printDirection=(48&s)>>4,a.scrollDirection=(12&s)>>2,a.justify=3&s,s=i[++t],a.effectSpeed=(240&s)>>4,a.effectDirection=(12&s)>>2,a.displayEffect=3&s,t},u.prototype.flushDisplayed=function(t,e){for(var i=[],s=0;s<8;s++)e.windows[s].visible&&!e.windows[s].isEmpty()&&i.push(e.windows[s].getText());e.endPts=t,e.text=i.join("\n\n"),this.pushCaption(e),e.startPts=t},u.prototype.pushCaption=function(t){""!==t.text&&(this.trigger("data",{startPts:t.startPts,endPts:t.endPts,text:t.text,stream:"cc708_"+t.serviceNum}),t.text="",t.startPts=t.endPts)},u.prototype.displayWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible=1);return t},u.prototype.hideWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible=0);return t},u.prototype.toggleWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&(e.windows[a].visible^=1);return t},u.prototype.clearWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&e.windows[a].clearText();return t},u.prototype.deleteWindows=function(t,e){var i=this.current708Packet.data[++t],s=this.getPts(t);this.flushDisplayed(s,e);for(var a=0;a<8;a++)i&1<<a&&e.windows[a].reset();return t},u.prototype.setPenAttributes=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penAttr;return s=i[++t],a.textTag=(240&s)>>4,a.offset=(12&s)>>2,a.penSize=3&s,s=i[++t],a.italics=(128&s)>>7,a.underline=(64&s)>>6,a.edgeType=(56&s)>>3,a.fontStyle=7&s,t},u.prototype.setPenColor=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penColor;return s=i[++t],a.fgOpacity=(192&s)>>6,a.fgRed=(48&s)>>4,a.fgGreen=(12&s)>>2,a.fgBlue=3&s,s=i[++t],a.bgOpacity=(192&s)>>6,a.bgRed=(48&s)>>4,a.bgGreen=(12&s)>>2,a.bgBlue=3&s,s=i[++t],a.edgeRed=(48&s)>>4,a.edgeGreen=(12&s)>>2,a.edgeBlue=3&s,t},u.prototype.setPenLocation=function(t,e){var i=this.current708Packet.data,s=i[t],a=e.currentWindow.penLoc;return e.currentWindow.pendingNewLine=!0,s=i[++t],a.row=15&s,s=i[++t],a.column=63&s,t},u.prototype.reset=function(t,e){var i=this.getPts(t);return this.flushDisplayed(i,e),this.initService(e.serviceNum,t)};var f={42:225,92:233,94:237,95:243,96:250,123:231,124:247,125:209,126:241,127:9608,304:174,305:176,306:189,307:191,308:8482,309:162,310:163,311:9834,312:224,313:160,314:232,315:226,316:234,317:238,318:244,319:251,544:193,545:201,546:211,547:218,548:220,549:252,550:8216,551:161,552:42,553:39,554:8212,555:169,556:8480,557:8226,558:8220,559:8221,560:192,561:194,562:199,563:200,564:202,565:203,566:235,567:206,568:207,569:239,570:212,571:217,572:249,573:219,574:171,575:187,800:195,801:227,802:205,803:204,804:236,805:210,806:242,807:213,808:245,809:123,810:125,811:92,812:94,813:95,814:124,815:126,816:196,817:228,818:214,819:246,820:223,821:165,822:164,823:9474,824:197,825:229,826:216,827:248,828:9484,829:9488,830:9492,831:9496},g=function(t){return null===t?"":(t=f[t]||t,String.fromCharCode(t))},y=[4352,4384,4608,4640,5376,5408,5632,5664,5888,5920,4096,4864,4896,5120,5152],m=function(){for(var t=[],e=15;e--;)t.push("");return t},_=function t(e,i){t.prototype.init.call(this),this.field_=e||0,this.dataChannel_=i||0,this.name_="CC"+(1+(this.field_<<1|this.dataChannel_)),this.setConstants(),this.reset(),this.push=function(t){var e,i,s,a,n;if((e=32639&t.ccData)!==this.lastControlCode_){if(4096==(61440&e)?this.lastControlCode_=e:e!==this.PADDING_&&(this.lastControlCode_=null),s=e>>>8,a=255&e,e!==this.PADDING_)if(e===this.RESUME_CAPTION_LOADING_)this.mode_="popOn";else if(e===this.END_OF_CAPTION_)this.mode_="popOn",this.clearFormatting(t.pts),this.flushDisplayed(t.pts),i=this.displayed_,this.displayed_=this.nonDisplayed_,this.nonDisplayed_=i,this.startPts_=t.pts;else if(e===this.ROLL_UP_2_ROWS_)this.rollUpRows_=2,this.setRollUp(t.pts);else if(e===this.ROLL_UP_3_ROWS_)this.rollUpRows_=3,this.setRollUp(t.pts);else if(e===this.ROLL_UP_4_ROWS_)this.rollUpRows_=4,this.setRollUp(t.pts);else if(e===this.CARRIAGE_RETURN_)this.clearFormatting(t.pts),this.flushDisplayed(t.pts),this.shiftRowsUp_(),this.startPts_=t.pts;else if(e===this.BACKSPACE_)"popOn"===this.mode_?this.nonDisplayed_[this.row_]=this.nonDisplayed_[this.row_].slice(0,-1):this.displayed_[this.row_]=this.displayed_[this.row_].slice(0,-1);else if(e===this.ERASE_DISPLAYED_MEMORY_)this.flushDisplayed(t.pts),this.displayed_=m();else if(e===this.ERASE_NON_DISPLAYED_MEMORY_)this.nonDisplayed_=m();else if(e===this.RESUME_DIRECT_CAPTIONING_)"paintOn"!==this.mode_&&(this.flushDisplayed(t.pts),this.displayed_=m()),this.mode_="paintOn",this.startPts_=t.pts;else if(this.isSpecialCharacter(s,a))n=g((s=(3&s)<<8)|a),this[this.mode_](t.pts,n),this.column_++;else if(this.isExtCharacter(s,a))"popOn"===this.mode_?this.nonDisplayed_[this.row_]=this.nonDisplayed_[this.row_].slice(0,-1):this.displayed_[this.row_]=this.displayed_[this.row_].slice(0,-1),n=g((s=(3&s)<<8)|a),this[this.mode_](t.pts,n),this.column_++;else if(this.isMidRowCode(s,a))this.clearFormatting(t.pts),this[this.mode_](t.pts," "),this.column_++,14==(14&a)&&this.addFormatting(t.pts,["i"]),1==(1&a)&&this.addFormatting(t.pts,["u"]);else if(this.isOffsetControlCode(s,a))this.column_+=3&a;else if(this.isPAC(s,a)){var r=y.indexOf(7968&e);"rollUp"===this.mode_&&(r-this.rollUpRows_+1<0&&(r=this.rollUpRows_-1),this.setRollUp(t.pts,r)),r!==this.row_&&(this.clearFormatting(t.pts),this.row_=r),1&a&&-1===this.formatting_.indexOf("u")&&this.addFormatting(t.pts,["u"]),16==(16&e)&&(this.column_=4*((14&e)>>1)),this.isColorPAC(a)&&14==(14&a)&&this.addFormatting(t.pts,["i"])}else this.isNormalChar(s)&&(0===a&&(a=null),n=g(s),n+=g(a),this[this.mode_](t.pts,n),this.column_+=n.length)}else this.lastControlCode_=null}};_.prototype=new s,_.prototype.flushDisplayed=function(t){var e=this.displayed_.map((function(t,e){try{return t.trim()}catch(t){return this.trigger("log",{level:"warn",message:"Skipping a malformed 608 caption at index "+e+"."}),""}}),this).join("\n").replace(/^\n+|\n+$/g,"");e.length&&this.trigger("data",{startPts:this.startPts_,endPts:t,text:e,stream:this.name_})},_.prototype.reset=function(){this.mode_="popOn",this.topRow_=0,this.startPts_=0,this.displayed_=m(),this.nonDisplayed_=m(),this.lastControlCode_=null,this.column_=0,this.row_=14,this.rollUpRows_=2,this.formatting_=[]},_.prototype.setConstants=function(){0===this.dataChannel_?(this.BASE_=16,this.EXT_=17,this.CONTROL_=(20|this.field_)<<8,this.OFFSET_=23):1===this.dataChannel_&&(this.BASE_=24,this.EXT_=25,this.CONTROL_=(28|this.field_)<<8,this.OFFSET_=31),this.PADDING_=0,this.RESUME_CAPTION_LOADING_=32|this.CONTROL_,this.END_OF_CAPTION_=47|this.CONTROL_,this.ROLL_UP_2_ROWS_=37|this.CONTROL_,this.ROLL_UP_3_ROWS_=38|this.CONTROL_,this.ROLL_UP_4_ROWS_=39|this.CONTROL_,this.CARRIAGE_RETURN_=45|this.CONTROL_,this.RESUME_DIRECT_CAPTIONING_=41|this.CONTROL_,this.BACKSPACE_=33|this.CONTROL_,this.ERASE_DISPLAYED_MEMORY_=44|this.CONTROL_,this.ERASE_NON_DISPLAYED_MEMORY_=46|this.CONTROL_},_.prototype.isSpecialCharacter=function(t,e){return t===this.EXT_&&e>=48&&e<=63},_.prototype.isExtCharacter=function(t,e){return(t===this.EXT_+1||t===this.EXT_+2)&&e>=32&&e<=63},_.prototype.isMidRowCode=function(t,e){return t===this.EXT_&&e>=32&&e<=47},_.prototype.isOffsetControlCode=function(t,e){return t===this.OFFSET_&&e>=33&&e<=35},_.prototype.isPAC=function(t,e){return t>=this.BASE_&&t<this.BASE_+8&&e>=64&&e<=127},_.prototype.isColorPAC=function(t){return t>=64&&t<=79||t>=96&&t<=127},_.prototype.isNormalChar=function(t){return t>=32&&t<=127},_.prototype.setRollUp=function(t,e){if("rollUp"!==this.mode_&&(this.row_=14,this.mode_="rollUp",this.flushDisplayed(t),this.nonDisplayed_=m(),this.displayed_=m()),void 0!==e&&e!==this.row_)for(var i=0;i<this.rollUpRows_;i++)this.displayed_[e-i]=this.displayed_[this.row_-i],this.displayed_[this.row_-i]="";void 0===e&&(e=this.row_),this.topRow_=e-this.rollUpRows_+1},_.prototype.addFormatting=function(t,e){this.formatting_=this.formatting_.concat(e);var i=e.reduce((function(t,e){return t+"<"+e+">"}),"");this[this.mode_](t,i)},_.prototype.clearFormatting=function(t){if(this.formatting_.length){var e=this.formatting_.reverse().reduce((function(t,e){return t+"</"+e+">"}),"");this.formatting_=[],this[this.mode_](t,e)}},_.prototype.popOn=function(t,e){var i=this.nonDisplayed_[this.row_];i+=e,this.nonDisplayed_[this.row_]=i},_.prototype.rollUp=function(t,e){var i=this.displayed_[this.row_];i+=e,this.displayed_[this.row_]=i},_.prototype.shiftRowsUp_=function(){var t;for(t=0;t<this.topRow_;t++)this.displayed_[t]="";for(t=this.row_+1;t<15;t++)this.displayed_[t]="";for(t=this.topRow_;t<this.row_;t++)this.displayed_[t]=this.displayed_[t+1];this.displayed_[this.row_]=""},_.prototype.paintOn=function(t,e){var i=this.displayed_[this.row_];i+=e,this.displayed_[this.row_]=i};var w={CaptionStream:h,Cea608Stream:_,Cea708Stream:u},b={H264_STREAM_TYPE:27,ADTS_STREAM_TYPE:15,METADATA_STREAM_TYPE:21},v="shared",T=function(t,e){var i=1;for(t>e&&(i=-1);Math.abs(e-t)>4294967296;)t+=8589934592*i;return t},k=function t(e){var i,s;t.prototype.init.call(this),this.type_=e||v,this.push=function(t){this.type_!==v&&t.type!==this.type_||(void 0===s&&(s=t.dts),t.dts=T(t.dts,s),t.pts=T(t.pts,s),i=t.dts,this.trigger("data",t))},this.flush=function(){s=i,this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")},this.discontinuity=function(){s=void 0,i=void 0},this.reset=function(){this.discontinuity(),this.trigger("reset")}};k.prototype=new s;var S,A=k,C=function(t,e,i){if(!t)return-1;for(var s=i;s<t.length;s++)if(t[s]===e)return s;return-1},D=3,P=function(t,e,i){var s,a="";for(s=e;s<i;s++)a+="%"+("00"+t[s].toString(16)).slice(-2);return a},E=function(t,e,i){return decodeURIComponent(P(t,e,i))},U=function(t,e,i){return unescape(P(t,e,i))},R=function(t){return t[0]<<21|t[1]<<14|t[2]<<7|t[3]},M={APIC:function(t){var e,i,s=1;t.data[0]===D&&((e=C(t.data,0,s))<0||(t.mimeType=U(t.data,s,e),s=e+1,t.pictureType=t.data[s],s++,(i=C(t.data,0,s))<0||(t.description=E(t.data,s,i),s=i+1,"--\x3e"===t.mimeType?t.url=U(t.data,s,t.data.length):t.pictureData=t.data.subarray(s,t.data.length))))},"T*":function(t){t.data[0]===D&&(t.value=E(t.data,1,t.data.length).replace(/\0*$/,""),t.values=t.value.split("\0"))},TXXX:function(t){var e;t.data[0]===D&&-1!==(e=C(t.data,0,1))&&(t.description=E(t.data,1,e),t.value=E(t.data,e+1,t.data.length).replace(/\0*$/,""),t.data=t.value)},"W*":function(t){t.url=U(t.data,0,t.data.length).replace(/\0.*$/,"")},WXXX:function(t){var e;t.data[0]===D&&-1!==(e=C(t.data,0,1))&&(t.description=E(t.data,1,e),t.url=U(t.data,e+1,t.data.length).replace(/\0.*$/,""))},PRIV:function(t){var e;for(e=0;e<t.data.length;e++)if(0===t.data[e]){t.owner=U(t.data,0,e);break}t.privateData=t.data.subarray(e+1),t.data=t.privateData}},x={parseId3Frames:function(t){var e,i=10,s=0,a=[];if(!(t.length<10||t[0]!=="I".charCodeAt(0)||t[1]!=="D".charCodeAt(0)||t[2]!=="3".charCodeAt(0))){s=R(t.subarray(6,10)),s+=10,64&t[5]&&(i+=4,i+=R(t.subarray(10,14)),s-=R(t.subarray(16,20)));do{if((e=R(t.subarray(i+4,i+8)))<1)break;var n={id:String.fromCharCode(t[i],t[i+1],t[i+2],t[i+3]),data:t.subarray(i+10,i+e+10)};n.key=n.id,M[n.id]?M[n.id](n):"T"===n.id[0]?M["T*"](n):"W"===n.id[0]&&M["W*"](n),a.push(n),i+=10,i+=e}while(i<s);return a}},parseSyncSafeInteger:R,frameParsers:M};(S=function(t){var e,i={descriptor:t&&t.descriptor},s=0,a=[],n=0;if(S.prototype.init.call(this),this.dispatchType=b.METADATA_STREAM_TYPE.toString(16),i.descriptor)for(e=0;e<i.descriptor.length;e++)this.dispatchType+=("00"+i.descriptor[e].toString(16)).slice(-2);this.push=function(t){var e,i,r,o,h;if("timed-metadata"===t.type)if(t.dataAlignmentIndicator&&(n=0,a.length=0),0===a.length&&(t.data.length<10||t.data[0]!=="I".charCodeAt(0)||t.data[1]!=="D".charCodeAt(0)||t.data[2]!=="3".charCodeAt(0)))this.trigger("log",{level:"warn",message:"Skipping unrecognized metadata packet"});else if(a.push(t),n+=t.data.byteLength,1===a.length&&(s=x.parseSyncSafeInteger(t.data.subarray(6,10)),s+=10),!(n<s)){for(e={data:new Uint8Array(s),frames:[],pts:a[0].pts,dts:a[0].dts},h=0;h<s;)e.data.set(a[0].data.subarray(0,s-h),h),h+=a[0].data.byteLength,n-=a[0].data.byteLength,a.shift();i=10,64&e.data[5]&&(i+=4,i+=x.parseSyncSafeInteger(e.data.subarray(10,14)),s-=x.parseSyncSafeInteger(e.data.subarray(16,20)));do{if((r=x.parseSyncSafeInteger(e.data.subarray(i+4,i+8)))<1){this.trigger("log",{level:"warn",message:"Malformed ID3 frame encountered. Skipping remaining metadata parsing."});break}if((o={id:String.fromCharCode(e.data[i],e.data[i+1],e.data[i+2],e.data[i+3]),data:e.data.subarray(i+10,i+r+10)}).key=o.id,x.frameParsers[o.id]?x.frameParsers[o.id](o):"T"===o.id[0]?x.frameParsers["T*"](o):"W"===o.id[0]&&x.frameParsers["W*"](o),"com.apple.streaming.transportStreamTimestamp"===o.owner){var p=o.data,d=(1&p[3])<<30|p[4]<<22|p[5]<<14|p[6]<<6|p[7]>>>2;d*=4,d+=3&p[7],o.timeStamp=d,void 0===e.pts&&void 0===e.dts&&(e.pts=o.timeStamp,e.dts=o.timeStamp),this.trigger("timestamp",o)}e.frames.push(o),i+=10,i+=r}while(i<s);this.trigger("data",e)}}}).prototype=new s;var O,I,L,B=S,G=A,N=188;(O=function(){var t=new Uint8Array(N),e=0;O.prototype.init.call(this),this.push=function(i){var s,a=0,n=N;for(e?((s=new Uint8Array(i.byteLength+e)).set(t.subarray(0,e)),s.set(i,e),e=0):s=i;n<s.byteLength;)71!==s[a]||71!==s[n]?(a++,n++):(this.trigger("data",s.subarray(a,n)),a+=N,n+=N);a<s.byteLength&&(t.set(s.subarray(a),0),e=s.byteLength-a)},this.flush=function(){e===N&&71===t[0]&&(this.trigger("data",t),e=0),this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")},this.reset=function(){e=0,this.trigger("reset")}}).prototype=new s,(I=function(){var t,e,i,s;I.prototype.init.call(this),s=this,this.packetsWaitingForPmt=[],this.programMapTable=void 0,t=function(t,s){var a=0;s.payloadUnitStartIndicator&&(a+=t[a]+1),"pat"===s.type?e(t.subarray(a),s):i(t.subarray(a),s)},e=function(t,e){e.section_number=t[7],e.last_section_number=t[8],s.pmtPid=(31&t[10])<<8|t[11],e.pmtPid=s.pmtPid},i=function(t,e){var i,a;if(1&t[5]){for(s.programMapTable={video:null,audio:null,"timed-metadata":{}},i=3+((15&t[1])<<8|t[2])-4,a=12+((15&t[10])<<8|t[11]);a<i;){var n=t[a],r=(31&t[a+1])<<8|t[a+2];n===b.H264_STREAM_TYPE&&null===s.programMapTable.video?s.programMapTable.video=r:n===b.ADTS_STREAM_TYPE&&null===s.programMapTable.audio?s.programMapTable.audio=r:n===b.METADATA_STREAM_TYPE&&(s.programMapTable["timed-metadata"][r]=n),a+=5+((15&t[a+3])<<8|t[a+4])}e.programMapTable=s.programMapTable}},this.push=function(e){var i={},s=4;if(i.payloadUnitStartIndicator=!!(64&e[1]),i.pid=31&e[1],i.pid<<=8,i.pid|=e[2],(48&e[3])>>>4>1&&(s+=e[s]+1),0===i.pid)i.type="pat",t(e.subarray(s),i),this.trigger("data",i);else if(i.pid===this.pmtPid)for(i.type="pmt",t(e.subarray(s),i),this.trigger("data",i);this.packetsWaitingForPmt.length;)this.processPes_.apply(this,this.packetsWaitingForPmt.shift());else void 0===this.programMapTable?this.packetsWaitingForPmt.push([e,s,i]):this.processPes_(e,s,i)},this.processPes_=function(t,e,i){i.pid===this.programMapTable.video?i.streamType=b.H264_STREAM_TYPE:i.pid===this.programMapTable.audio?i.streamType=b.ADTS_STREAM_TYPE:i.streamType=this.programMapTable["timed-metadata"][i.pid],i.type="pes",i.data=t.subarray(e),this.trigger("data",i)}}).prototype=new s,I.STREAM_TYPES={h264:27,adts:15},(L=function(){var t,e=this,i=!1,s={data:[],size:0},a={data:[],size:0},n={data:[],size:0},r=function(t,i,s){var a,n,r=new Uint8Array(t.size),o={type:i},h=0,p=0;if(t.data.length&&!(t.size<9)){for(o.trackId=t.data[0].pid,h=0;h<t.data.length;h++)n=t.data[h],r.set(n.data,p),p+=n.data.byteLength;var d,l,c,u;l=o,u=(d=r)[0]<<16|d[1]<<8|d[2],l.data=new Uint8Array,1===u&&(l.packetLength=6+(d[4]<<8|d[5]),l.dataAlignmentIndicator=0!=(4&d[6]),192&(c=d[7])&&(l.pts=(14&d[9])<<27|(255&d[10])<<20|(254&d[11])<<12|(255&d[12])<<5|(254&d[13])>>>3,l.pts*=4,l.pts+=(6&d[13])>>>1,l.dts=l.pts,64&c&&(l.dts=(14&d[14])<<27|(255&d[15])<<20|(254&d[16])<<12|(255&d[17])<<5|(254&d[18])>>>3,l.dts*=4,l.dts+=(6&d[18])>>>1)),l.data=d.subarray(9+d[8])),a="video"===i||o.packetLength<=t.size,(s||a)&&(t.size=0,t.data.length=0),a&&e.trigger("data",o)}};L.prototype.init.call(this),this.push=function(o){({pat:function(){},pes:function(){var t,e;switch(o.streamType){case b.H264_STREAM_TYPE:t=s,e="video";break;case b.ADTS_STREAM_TYPE:t=a,e="audio";break;case b.METADATA_STREAM_TYPE:t=n,e="timed-metadata";break;default:return}o.payloadUnitStartIndicator&&r(t,e,!0),t.data.push(o),t.size+=o.data.byteLength},pmt:function(){var s={type:"metadata",tracks:[]};null!==(t=o.programMapTable).video&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.video,codec:"avc",type:"video"}),null!==t.audio&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.audio,codec:"adts",type:"audio"}),i=!0,e.trigger("data",s)}})[o.type]()},this.reset=function(){s.size=0,s.data.length=0,a.size=0,a.data.length=0,this.trigger("reset")},this.flushStreams_=function(){r(s,"video"),r(a,"audio"),r(n,"timed-metadata")},this.flush=function(){if(!i&&t){var s={type:"metadata",tracks:[]};null!==t.video&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.video,codec:"avc",type:"video"}),null!==t.audio&&s.tracks.push({timelineStartInfo:{baseMediaDecodeTime:0},id:+t.audio,codec:"adts",type:"audio"}),e.trigger("data",s)}i=!1,this.flushStreams_(),this.trigger("done")}}).prototype=new s;var W={PAT_PID:0,MP2T_PACKET_LENGTH:N,TransportPacketStream:O,TransportParseStream:I,ElementaryStream:L,TimestampRolloverStream:G,CaptionStream:w.CaptionStream,Cea608Stream:w.Cea608Stream,Cea708Stream:w.Cea708Stream,MetadataStream:B};for(var F in b)b.hasOwnProperty(F)&&(W[F]=b[F]);var z,V,Y,X,j=W,q=9e4;z=function(t){return t*q},V=function(t,e){return t*e},Y=function(t){return t/q},X=function(t,e){return t/e};var H,K=q,Z=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];(H=function(t){var e,i=0;H.prototype.init.call(this),this.skipWarn_=function(t,e){this.trigger("log",{level:"warn",message:"adts skiping bytes "+t+" to "+e+" in frame "+i+" outside syncword"})},this.push=function(s){var a,n,r,o,h,p=0;if(t||(i=0),"audio"===s.type){var d;for(e&&e.length?(r=e,(e=new Uint8Array(r.byteLength+s.data.byteLength)).set(r),e.set(s.data,r.byteLength)):e=s.data;p+7<e.length;)if(255===e[p]&&240==(246&e[p+1])){if("number"==typeof d&&(this.skipWarn_(d,p),d=null),n=2*(1&~e[p+1]),a=(3&e[p+3])<<11|e[p+4]<<3|(224&e[p+5])>>5,h=(o=1024*(1+(3&e[p+6])))*K/Z[(60&e[p+2])>>>2],e.byteLength-p<a)break;this.trigger("data",{pts:s.pts+i*h,dts:s.dts+i*h,sampleCount:o,audioobjecttype:1+(e[p+2]>>>6&3),channelcount:(1&e[p+2])<<2|(192&e[p+3])>>>6,samplerate:Z[(60&e[p+2])>>>2],samplingfrequencyindex:(60&e[p+2])>>>2,samplesize:16,data:e.subarray(p+7+n,p+a)}),i++,p+=a}else"number"!=typeof d&&(d=p),p++;"number"==typeof d&&(this.skipWarn_(d,p),d=null),e=e.subarray(p)}},this.flush=function(){i=0,this.trigger("done")},this.reset=function(){e=void 0,this.trigger("reset")},this.endTimeline=function(){e=void 0,this.trigger("endedtimeline")}}).prototype=new s;var $,J,Q,tt=H,et=function(t){var e=t.byteLength,i=0,s=0;this.length=function(){return 8*e},this.bitsAvailable=function(){return 8*e+s},this.loadWord=function(){var a=t.byteLength-e,n=new Uint8Array(4),r=Math.min(4,e);if(0===r)throw new Error("no bytes available");n.set(t.subarray(a,a+r)),i=new DataView(n.buffer).getUint32(0),s=8*r,e-=r},this.skipBits=function(t){var a;s>t?(i<<=t,s-=t):(t-=s,t-=8*(a=Math.floor(t/8)),e-=a,this.loadWord(),i<<=t,s-=t)},this.readBits=function(t){var a=Math.min(s,t),n=i>>>32-a;return(s-=a)>0?i<<=a:e>0&&this.loadWord(),(a=t-a)>0?n<<a|this.readBits(a):n},this.skipLeadingZeros=function(){var t;for(t=0;t<s;++t)if(0!=(i&2147483648>>>t))return i<<=t,s-=t,t;return this.loadWord(),t+this.skipLeadingZeros()},this.skipUnsignedExpGolomb=function(){this.skipBits(1+this.skipLeadingZeros())},this.skipExpGolomb=function(){this.skipBits(1+this.skipLeadingZeros())},this.readUnsignedExpGolomb=function(){var t=this.skipLeadingZeros();return this.readBits(t+1)-1},this.readExpGolomb=function(){var t=this.readUnsignedExpGolomb();return 1&t?1+t>>>1:-1*(t>>>1)},this.readBoolean=function(){return 1===this.readBits(1)},this.readUnsignedByte=function(){return this.readBits(8)},this.loadWord()};(J=function(){var t,e,i=0;J.prototype.init.call(this),this.push=function(s){var a;e?((a=new Uint8Array(e.byteLength+s.data.byteLength)).set(e),a.set(s.data,e.byteLength),e=a):e=s.data;for(var n=e.byteLength;i<n-3;i++)if(1===e[i+2]){t=i+5;break}for(;t<n;)switch(e[t]){case 0:if(0!==e[t-1]){t+=2;break}if(0!==e[t-2]){t++;break}i+3!==t-2&&this.trigger("data",e.subarray(i+3,t-2));do{t++}while(1!==e[t]&&t<n);i=t-2,t+=3;break;case 1:if(0!==e[t-1]||0!==e[t-2]){t+=3;break}this.trigger("data",e.subarray(i+3,t-2)),i=t-2,t+=3;break;default:t+=3}e=e.subarray(i),t-=i,i=0},this.reset=function(){e=null,i=0,this.trigger("reset")},this.flush=function(){e&&e.byteLength>3&&this.trigger("data",e.subarray(i+3)),e=null,i=0,this.trigger("done")},this.endTimeline=function(){this.flush(),this.trigger("endedtimeline")}}).prototype=new s,Q={100:!0,110:!0,122:!0,244:!0,44:!0,83:!0,86:!0,118:!0,128:!0,138:!0,139:!0,134:!0},($=function(){var t,e,i,s,a,n,r,o=new J;$.prototype.init.call(this),t=this,this.push=function(t){"video"===t.type&&(e=t.trackId,i=t.pts,s=t.dts,o.push(t))},o.on("data",(function(r){var o={trackId:e,pts:i,dts:s,data:r,nalUnitTypeCode:31&r[0]};switch(o.nalUnitTypeCode){case 5:o.nalUnitType="slice_layer_without_partitioning_rbsp_idr";break;case 6:o.nalUnitType="sei_rbsp",o.escapedRBSP=a(r.subarray(1));break;case 7:o.nalUnitType="seq_parameter_set_rbsp",o.escapedRBSP=a(r.subarray(1)),o.config=n(o.escapedRBSP);break;case 8:o.nalUnitType="pic_parameter_set_rbsp";break;case 9:o.nalUnitType="access_unit_delimiter_rbsp"}t.trigger("data",o)})),o.on("done",(function(){t.trigger("done")})),o.on("partialdone",(function(){t.trigger("partialdone")})),o.on("reset",(function(){t.trigger("reset")})),o.on("endedtimeline",(function(){t.trigger("endedtimeline")})),this.flush=function(){o.flush()},this.partialFlush=function(){o.partialFlush()},this.reset=function(){o.reset()},this.endTimeline=function(){o.endTimeline()},r=function(t,e){var i,s=8,a=8;for(i=0;i<t;i++)0!==a&&(a=(s+e.readExpGolomb()+256)%256),s=0===a?s:a},a=function(t){for(var e,i,s=t.byteLength,a=[],n=1;n<s-2;)0===t[n]&&0===t[n+1]&&3===t[n+2]?(a.push(n+2),n+=2):n++;if(0===a.length)return t;e=s-a.length,i=new Uint8Array(e);var r=0;for(n=0;n<e;r++,n++)r===a[0]&&(r++,a.shift()),i[n]=t[r];return i},n=function(t){var e,i,s,a,n,o,h,p,d,l,c,u,f=0,g=0,y=0,m=0,_=[1,1];if(i=(e=new et(t)).readUnsignedByte(),a=e.readUnsignedByte(),s=e.readUnsignedByte(),e.skipUnsignedExpGolomb(),Q[i]&&(3===(n=e.readUnsignedExpGolomb())&&e.skipBits(1),e.skipUnsignedExpGolomb(),e.skipUnsignedExpGolomb(),e.skipBits(1),e.readBoolean()))for(c=3!==n?8:12,u=0;u<c;u++)e.readBoolean()&&r(u<6?16:64,e);if(e.skipUnsignedExpGolomb(),0===(o=e.readUnsignedExpGolomb()))e.readUnsignedExpGolomb();else if(1===o)for(e.skipBits(1),e.skipExpGolomb(),e.skipExpGolomb(),h=e.readUnsignedExpGolomb(),u=0;u<h;u++)e.skipExpGolomb();if(e.skipUnsignedExpGolomb(),e.skipBits(1),p=e.readUnsignedExpGolomb(),d=e.readUnsignedExpGolomb(),0===(l=e.readBits(1))&&e.skipBits(1),e.skipBits(1),e.readBoolean()&&(f=e.readUnsignedExpGolomb(),g=e.readUnsignedExpGolomb(),y=e.readUnsignedExpGolomb(),m=e.readUnsignedExpGolomb()),e.readBoolean()&&e.readBoolean()){switch(e.readUnsignedByte()){case 1:_=[1,1];break;case 2:_=[12,11];break;case 3:_=[10,11];break;case 4:_=[16,11];break;case 5:_=[40,33];break;case 6:_=[24,11];break;case 7:_=[20,11];break;case 8:_=[32,11];break;case 9:_=[80,33];break;case 10:_=[18,11];break;case 11:_=[15,11];break;case 12:_=[64,33];break;case 13:_=[160,99];break;case 14:_=[4,3];break;case 15:_=[3,2];break;case 16:_=[2,1];break;case 255:_=[e.readUnsignedByte()<<8|e.readUnsignedByte(),e.readUnsignedByte()<<8|e.readUnsignedByte()]}_&&(_[0],_[1])}return{profileIdc:i,levelIdc:s,profileCompatibility:a,width:16*(p+1)-2*f-2*g,height:(2-l)*(d+1)*16-2*y-2*m,sarRatio:_}}}).prototype=new s;var it={H264Stream:$,NalByteStream:J},st=function t(e){this.numberOfTracks=0,this.metadataStream=e.metadataStream,this.videoTags=[],this.audioTags=[],this.videoTrack=null,this.audioTrack=null,this.pendingCaptions=[],this.pendingMetadata=[],this.pendingTracks=0,this.processedTracks=0,t.prototype.init.call(this),this.push=function(t){return t.text?this.pendingCaptions.push(t):t.frames?this.pendingMetadata.push(t):("video"===t.track.type&&(this.videoTrack=t.track,this.videoTags=t.tags,this.pendingTracks++),void("audio"===t.track.type&&(this.audioTrack=t.track,this.audioTags=t.tags,this.pendingTracks++)))}};(st.prototype=new s).flush=function(t){var e,i,s,a,n={tags:{},captions:[],captionStreams:{},metadata:[]};if(this.pendingTracks<this.numberOfTracks){if("VideoSegmentStream"!==t&&"AudioSegmentStream"!==t)return;if(0===this.pendingTracks&&(this.processedTracks++,this.processedTracks<this.numberOfTracks))return}if(this.processedTracks+=this.pendingTracks,this.pendingTracks=0,!(this.processedTracks<this.numberOfTracks)){for(this.videoTrack?a=this.videoTrack.timelineStartInfo.pts:this.audioTrack&&(a=this.audioTrack.timelineStartInfo.pts),n.tags.videoTags=this.videoTags,n.tags.audioTags=this.audioTags,s=0;s<this.pendingCaptions.length;s++)(i=this.pendingCaptions[s]).startTime=i.startPts-a,i.startTime/=9e4,i.endTime=i.endPts-a,i.endTime/=9e4,n.captionStreams[i.stream]=!0,n.captions.push(i);for(s=0;s<this.pendingMetadata.length;s++)(e=this.pendingMetadata[s]).cueTime=e.pts-a,e.cueTime/=9e4,n.metadata.push(e);n.metadata.dispatchType=this.metadataStream.dispatchType,this.videoTrack=null,this.audioTrack=null,this.videoTags=[],this.audioTags=[],this.pendingCaptions.length=0,this.pendingMetadata.length=0,this.pendingTracks=0,this.processedTracks=0,this.trigger("data",n),this.trigger("done")}};var at,nt,rt,ot,ht,pt,dt=st,lt=function(){var t=this;this.list=[],this.push=function(t){this.list.push({bytes:t.bytes,dts:t.dts,pts:t.pts,keyFrame:t.keyFrame,metaDataTag:t.metaDataTag})},Object.defineProperty(this,"length",{get:function(){return t.list.length}})},ct=it.H264Stream;ot=function(t,e){"number"==typeof e.pts&&(void 0===t.timelineStartInfo.pts?t.timelineStartInfo.pts=e.pts:t.timelineStartInfo.pts=Math.min(t.timelineStartInfo.pts,e.pts)),"number"==typeof e.dts&&(void 0===t.timelineStartInfo.dts?t.timelineStartInfo.dts=e.dts:t.timelineStartInfo.dts=Math.min(t.timelineStartInfo.dts,e.dts))},ht=function(t,i){var s=new e(e.METADATA_TAG);return s.dts=i,s.pts=i,s.writeMetaDataDouble("videocodecid",7),s.writeMetaDataDouble("width",t.width),s.writeMetaDataDouble("height",t.height),s},pt=function(t,i){var s,a=new e(e.VIDEO_TAG,!0);for(a.dts=i,a.pts=i,a.writeByte(1),a.writeByte(t.profileIdc),a.writeByte(t.profileCompatibility),a.writeByte(t.levelIdc),a.writeByte(255),a.writeByte(225),a.writeShort(t.sps[0].length),a.writeBytes(t.sps[0]),a.writeByte(t.pps.length),s=0;s<t.pps.length;++s)a.writeShort(t.pps[s].length),a.writeBytes(t.pps[s]);return a},(rt=function(t){var i,s=[],a=[];rt.prototype.init.call(this),this.push=function(e){ot(t,e),t&&(t.audioobjecttype=e.audioobjecttype,t.channelcount=e.channelcount,t.samplerate=e.samplerate,t.samplingfrequencyindex=e.samplingfrequencyindex,t.samplesize=e.samplesize,t.extraData=t.audioobjecttype<<11|t.samplingfrequencyindex<<7|t.channelcount<<3),e.pts=Math.round(e.pts/90),e.dts=Math.round(e.dts/90),s.push(e)},this.flush=function(){var n,r,o,h=new lt;if(0!==s.length){for(o=-1/0;s.length;)n=s.shift(),a.length&&n.pts>=a[0]&&(o=a.shift(),this.writeMetaDataTags(h,o)),(t.extraData!==i||n.pts-o>=1e3)&&(this.writeMetaDataTags(h,n.pts),i=t.extraData,o=n.pts),(r=new e(e.AUDIO_TAG)).pts=n.pts,r.dts=n.dts,r.writeBytes(n.data),h.push(r.finalize());a.length=0,i=null,this.trigger("data",{track:t,tags:h.list}),this.trigger("done","AudioSegmentStream")}else this.trigger("done","AudioSegmentStream")},this.writeMetaDataTags=function(i,s){var a;(a=new e(e.METADATA_TAG)).pts=s,a.dts=s,a.writeMetaDataDouble("audiocodecid",10),a.writeMetaDataBoolean("stereo",2===t.channelcount),a.writeMetaDataDouble("audiosamplerate",t.samplerate),a.writeMetaDataDouble("audiosamplesize",16),i.push(a.finalize()),(a=new e(e.AUDIO_TAG,!0)).pts=s,a.dts=s,a.view.setUint16(a.position,t.extraData),a.position+=2,a.length=Math.max(a.length,a.position),i.push(a.finalize())},this.onVideoKeyFrame=function(t){a.push(t)}}).prototype=new s,(nt=function(t){var i,s,a=[];nt.prototype.init.call(this),this.finishFrame=function(e,a){if(a){if(i&&t&&t.newMetadata&&(a.keyFrame||0===e.length)){var n=ht(i,a.dts).finalize(),r=pt(t,a.dts).finalize();n.metaDataTag=r.metaDataTag=!0,e.push(n),e.push(r),t.newMetadata=!1,this.trigger("keyframe",a.dts)}a.endNalUnit(),e.push(a.finalize()),s=null}},this.push=function(e){ot(t,e),e.pts=Math.round(e.pts/90),e.dts=Math.round(e.dts/90),a.push(e)},this.flush=function(){for(var n,r=new lt;a.length&&"access_unit_delimiter_rbsp"!==a[0].nalUnitType;)a.shift();if(0!==a.length){for(;a.length;)"seq_parameter_set_rbsp"===(n=a.shift()).nalUnitType?(t.newMetadata=!0,i=n.config,t.width=i.width,t.height=i.height,t.sps=[n.data],t.profileIdc=i.profileIdc,t.levelIdc=i.levelIdc,t.profileCompatibility=i.profileCompatibility,s.endNalUnit()):"pic_parameter_set_rbsp"===n.nalUnitType?(t.newMetadata=!0,t.pps=[n.data],s.endNalUnit()):"access_unit_delimiter_rbsp"===n.nalUnitType?(s&&this.finishFrame(r,s),(s=new e(e.VIDEO_TAG)).pts=n.pts,s.dts=n.dts):("slice_layer_without_partitioning_rbsp_idr"===n.nalUnitType&&(s.keyFrame=!0),s.endNalUnit()),s.startNalUnit(),s.writeBytes(n.data);s&&this.finishFrame(r,s),this.trigger("data",{track:t,tags:r.list}),this.trigger("done","VideoSegmentStream")}else this.trigger("done","VideoSegmentStream")}}).prototype=new s,(at=function(t){var e,i,s,a,n,r,o,h,p,d,l,c,u=this;at.prototype.init.call(this),t=t||{},this.metadataStream=new j.MetadataStream,t.metadataStream=this.metadataStream,e=new j.TransportPacketStream,i=new j.TransportParseStream,s=new j.ElementaryStream,a=new j.TimestampRolloverStream("video"),n=new j.TimestampRolloverStream("audio"),r=new j.TimestampRolloverStream("timed-metadata"),o=new tt,h=new ct,c=new dt(t),e.pipe(i).pipe(s),s.pipe(a).pipe(h),s.pipe(n).pipe(o),s.pipe(r).pipe(this.metadataStream).pipe(c),l=new j.CaptionStream(t),h.pipe(l).pipe(c),s.on("data",(function(t){var e,i,s;if("metadata"===t.type){for(e=t.tracks.length;e--;)"video"===t.tracks[e].type?i=t.tracks[e]:"audio"===t.tracks[e].type&&(s=t.tracks[e]);i&&!p&&(c.numberOfTracks++,p=new nt(i),h.pipe(p).pipe(c)),s&&!d&&(c.numberOfTracks++,d=new rt(s),o.pipe(d).pipe(c),p&&p.on("keyframe",d.onVideoKeyFrame))}})),this.push=function(t){e.push(t)},this.flush=function(){e.flush()},this.resetCaptions=function(){l.reset()},c.on("data",(function(t){u.trigger("data",t)})),c.on("done",(function(){u.trigger("done")}))}).prototype=new s;var ut=function(t,i,s){var a,n,r,o=new Uint8Array(9),h=new DataView(o.buffer);return t=t||0,i=void 0===i||i,s=void 0===s||s,h.setUint8(0,70),h.setUint8(1,76),h.setUint8(2,86),h.setUint8(3,1),h.setUint8(4,(i?4:0)|(s?1:0)),h.setUint32(5,o.byteLength),t<=0?((n=new Uint8Array(o.byteLength+4)).set(o),n.set([0,0,0,0],o.byteLength),n):((a=new e(e.METADATA_TAG)).pts=a.dts=0,a.writeMetaDataDouble("duration",t),r=a.finalize().length,(n=new Uint8Array(o.byteLength+r)).set(o),n.set(h.byteLength,r),n)};return{tag:e,Transmuxer:at,getFlvHeader:ut}})); |
@@ -15,160 +15,3 @@ /** | ||
StreamTypes = require('./stream-types'), | ||
typedArrayIndexOf = require('../utils/typed-array').typedArrayIndexOf, | ||
// Frames that allow different types of text encoding contain a text | ||
// encoding description byte [ID3v2.4.0 section 4.] | ||
textEncodingDescriptionByte = { | ||
Iso88591: 0x00, | ||
// ISO-8859-1, terminated with \0. | ||
Utf16: 0x01, | ||
// UTF-16 encoded Unicode BOM, terminated with \0\0 | ||
Utf16be: 0x02, | ||
// UTF-16BE encoded Unicode, without BOM, terminated with \0\0 | ||
Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0 | ||
}, | ||
// return a percent-encoded representation of the specified byte range | ||
// @see http://en.wikipedia.org/wiki/Percent-encoding | ||
percentEncode = function percentEncode(bytes, start, end) { | ||
var i, | ||
result = ''; | ||
for (i = start; i < end; i++) { | ||
result += '%' + ('00' + bytes[i].toString(16)).slice(-2); | ||
} | ||
return result; | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as UTf-8. | ||
parseUtf8 = function parseUtf8(bytes, start, end) { | ||
return decodeURIComponent(percentEncode(bytes, start, end)); | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as ISO-8859-1. | ||
parseIso88591 = function parseIso88591(bytes, start, end) { | ||
return unescape(percentEncode(bytes, start, end)); // jshint ignore:line | ||
}, | ||
parseSyncSafeInteger = function parseSyncSafeInteger(data) { | ||
return data[0] << 21 | data[1] << 14 | data[2] << 7 | data[3]; | ||
}, | ||
frameParsers = { | ||
'APIC': function APIC(frame) { | ||
var i = 1, | ||
mimeTypeEndIndex, | ||
descriptionEndIndex, | ||
LINK_MIME_TYPE = '-->'; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} // parsing fields [ID3v2.4.0 section 4.14.] | ||
mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (mimeTypeEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} // parsing Mime type field (terminated with \0) | ||
frame.mimeType = parseIso88591(frame.data, i, mimeTypeEndIndex); | ||
i = mimeTypeEndIndex + 1; // parsing 1-byte Picture Type field | ||
frame.pictureType = frame.data[i]; | ||
i++; | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (descriptionEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} // parsing Description field (terminated with \0) | ||
frame.description = parseUtf8(frame.data, i, descriptionEndIndex); | ||
i = descriptionEndIndex + 1; | ||
if (frame.mimeType === LINK_MIME_TYPE) { | ||
// parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.]) | ||
frame.url = parseIso88591(frame.data, i, frame.data.length); | ||
} else { | ||
// parsing Picture Data field as binary data | ||
frame.pictureData = frame.data.subarray(i, frame.data.length); | ||
} | ||
}, | ||
'T*': function T(frame) { | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} // parse text field, do not include null terminator in the frame value | ||
// frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8(frame.data, 1, frame.data.length).replace(/\0*$/, ''); // text information frames supports multiple strings, stored as a terminator separated list [ID3v2.4.0 section 4.2.] | ||
frame.values = frame.value.split('\0'); | ||
}, | ||
'TXXX': function TXXX(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} // parse the text fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // do not include the null terminator in the tag value | ||
// frames that allow different types of encoding contain terminated text | ||
// [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0*$/, ''); | ||
frame.data = frame.value; | ||
}, | ||
'W*': function W(frame) { | ||
// parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591(frame.data, 0, frame.data.length).replace(/\0.*$/, ''); | ||
}, | ||
'WXXX': function WXXX(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} // parse the description and URL fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); // URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information | ||
// should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591(frame.data, descriptionEndIndex + 1, frame.data.length).replace(/\0.*$/, ''); | ||
}, | ||
'PRIV': function PRIV(frame) { | ||
var i; | ||
for (i = 0; i < frame.data.length; i++) { | ||
if (frame.data[i] === 0) { | ||
// parse the description and URL fields | ||
frame.owner = parseIso88591(frame.data, 0, i); | ||
break; | ||
} | ||
} | ||
frame.privateData = frame.data.subarray(i + 1); | ||
frame.data = frame.privateData; | ||
} | ||
}, | ||
id3 = require('../tools/parse-id3'), | ||
_MetadataStream; | ||
@@ -236,3 +79,3 @@ | ||
// results concatenated to recover the actual value. | ||
tagSize = parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more | ||
tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); // ID3 reports the tag size excluding the header but it's more | ||
// convenient for our comparisons to include it | ||
@@ -270,5 +113,5 @@ | ||
frameStart += parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end | ||
frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14)); // clip any padding off the end | ||
tagSize -= parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
} // parse one or more ID3 frames | ||
@@ -280,3 +123,3 @@ // http://id3.org/id3v2.3.0#ID3v2_frame_overview | ||
// determine the number of bytes in this frame | ||
frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
@@ -300,11 +143,11 @@ if (frameSize < 1) { | ||
if (frameParsers[frame.id]) { | ||
if (id3.frameParsers[frame.id]) { | ||
// use frame specific parser | ||
frameParsers[frame.id](frame); | ||
id3.frameParsers[frame.id](frame); | ||
} else if (frame.id[0] === 'T') { | ||
// use text frame generic parser | ||
frameParsers['T*'](frame); | ||
id3.frameParsers['T*'](frame); | ||
} else if (frame.id[0] === 'W') { | ||
// use URL link frame generic parser | ||
frameParsers['W*'](frame); | ||
id3.frameParsers['W*'](frame); | ||
} // handle the special PRIV frame used to indicate the start | ||
@@ -311,0 +154,0 @@ // time for raw AAC data |
@@ -19,2 +19,4 @@ /** | ||
var emsg = require('../mp4/emsg.js'); | ||
var parseTfhd = require('../tools/parse-tfhd.js'); | ||
@@ -28,5 +30,7 @@ | ||
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader; | ||
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, getTimescaleFromMediaHeader, getEmsgID3; | ||
var window = require('global/window'); | ||
var parseId3Frames = require('../tools/parse-id3.js').parseId3Frames; | ||
/** | ||
@@ -351,3 +355,31 @@ * Parses an MP4 initialization segment and extracts the timescale | ||
}; | ||
/** | ||
* Returns an array of emsg ID3 data from the provided segmentData. | ||
* An offset can also be provided as the Latest Arrival Time to calculate | ||
* the Event Start Time of v0 EMSG boxes. | ||
* See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing | ||
* | ||
* @param {Uint8Array} segmentData the segment byte array. | ||
* @param {number} offset the segment start time or Latest Arrival Time, | ||
* @return {Object[]} an array of ID3 parsed from EMSG boxes | ||
*/ | ||
getEmsgID3 = function getEmsgID3(segmentData, offset) { | ||
if (offset === void 0) { | ||
offset = 0; | ||
} | ||
var emsgBoxes = findBox(segmentData, ['emsg']); | ||
return emsgBoxes.map(function (data) { | ||
var parsedBox = emsg.parseEmsgBox(new Uint8Array(data)); | ||
var parsedId3Frames = parseId3Frames(parsedBox.message_data); | ||
return { | ||
cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset), | ||
duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale), | ||
frames: parsedId3Frames | ||
}; | ||
}); | ||
}; | ||
module.exports = { | ||
@@ -362,3 +394,4 @@ // export mp4 inspector's findBox and parseType for backwards compatibility | ||
tracks: getTracks, | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader, | ||
getEmsgID3: getEmsgID3 | ||
}; |
@@ -15,164 +15,3 @@ /** | ||
StreamTypes = require('./stream-types'), | ||
typedArrayIndexOf = require('../utils/typed-array').typedArrayIndexOf, | ||
// Frames that allow different types of text encoding contain a text | ||
// encoding description byte [ID3v2.4.0 section 4.] | ||
textEncodingDescriptionByte = { | ||
Iso88591: 0x00, // ISO-8859-1, terminated with \0. | ||
Utf16: 0x01, // UTF-16 encoded Unicode BOM, terminated with \0\0 | ||
Utf16be: 0x02, // UTF-16BE encoded Unicode, without BOM, terminated with \0\0 | ||
Utf8: 0x03 // UTF-8 encoded Unicode, terminated with \0 | ||
}, | ||
// return a percent-encoded representation of the specified byte range | ||
// @see http://en.wikipedia.org/wiki/Percent-encoding | ||
percentEncode = function(bytes, start, end) { | ||
var i, result = ''; | ||
for (i = start; i < end; i++) { | ||
result += '%' + ('00' + bytes[i].toString(16)).slice(-2); | ||
} | ||
return result; | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as UTf-8. | ||
parseUtf8 = function(bytes, start, end) { | ||
return decodeURIComponent(percentEncode(bytes, start, end)); | ||
}, | ||
// return the string representation of the specified byte range, | ||
// interpreted as ISO-8859-1. | ||
parseIso88591 = function(bytes, start, end) { | ||
return unescape(percentEncode(bytes, start, end)); // jshint ignore:line | ||
}, | ||
parseSyncSafeInteger = function(data) { | ||
return (data[0] << 21) | | ||
(data[1] << 14) | | ||
(data[2] << 7) | | ||
(data[3]); | ||
}, | ||
frameParsers = { | ||
'APIC': function(frame) { | ||
var | ||
i = 1, | ||
mimeTypeEndIndex, | ||
descriptionEndIndex, | ||
LINK_MIME_TYPE = '-->'; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
// parsing fields [ID3v2.4.0 section 4.14.] | ||
mimeTypeEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (mimeTypeEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} | ||
// parsing Mime type field (terminated with \0) | ||
frame.mimeType = parseIso88591(frame.data, i, mimeTypeEndIndex); | ||
i = mimeTypeEndIndex + 1; | ||
// parsing 1-byte Picture Type field | ||
frame.pictureType = frame.data[i]; | ||
i++ | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, i); | ||
if (descriptionEndIndex < 0) { | ||
// malformed frame | ||
return; | ||
} | ||
// parsing Description field (terminated with \0) | ||
frame.description = parseUtf8(frame.data, i, descriptionEndIndex); | ||
i = descriptionEndIndex + 1; | ||
if (frame.mimeType === LINK_MIME_TYPE) { | ||
// parsing Picture Data field as URL (always represented as ISO-8859-1 [ID3v2.4.0 section 4.]) | ||
frame.url = parseIso88591(frame.data, i, frame.data.length) | ||
} else { | ||
// parsing Picture Data field as binary data | ||
frame.pictureData = frame.data.subarray(i, frame.data.length); | ||
} | ||
}, | ||
'T*': function(frame) { | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
// parse text field, do not include null terminator in the frame value | ||
// frames that allow different types of encoding contain terminated text [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8(frame.data, 1, frame.data.length).replace(/\0*$/, ''); | ||
// text information frames supports multiple strings, stored as a terminator separated list [ID3v2.4.0 section 4.2.] | ||
frame.values = frame.value.split('\0'); | ||
}, | ||
'TXXX': function(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} | ||
// parse the text fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); | ||
// do not include the null terminator in the tag value | ||
// frames that allow different types of encoding contain terminated text | ||
// [ID3v2.4.0 section 4.] | ||
frame.value = parseUtf8( | ||
frame.data, | ||
descriptionEndIndex + 1, | ||
frame.data.length | ||
).replace(/\0*$/, ''); | ||
frame.data = frame.value; | ||
}, | ||
'W*': function(frame) { | ||
// parse URL field; URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591(frame.data, 0, frame.data.length).replace(/\0.*$/, ''); | ||
}, | ||
'WXXX': function(frame) { | ||
var descriptionEndIndex; | ||
if (frame.data[0] !== textEncodingDescriptionByte.Utf8) { | ||
// ignore frames with unrecognized character encodings | ||
return; | ||
} | ||
descriptionEndIndex = typedArrayIndexOf(frame.data, 0, 1); | ||
if (descriptionEndIndex === -1) { | ||
return; | ||
} | ||
// parse the description and URL fields | ||
frame.description = parseUtf8(frame.data, 1, descriptionEndIndex); | ||
// URL fields are always represented as ISO-8859-1 [ID3v2.4.0 section 4.] | ||
// if the value is followed by a string termination all the following information | ||
// should be ignored [ID3v2.4.0 section 4.3] | ||
frame.url = parseIso88591( | ||
frame.data, | ||
descriptionEndIndex + 1, | ||
frame.data.length | ||
).replace(/\0.*$/, ''); | ||
}, | ||
'PRIV': function(frame) { | ||
var i; | ||
for (i = 0; i < frame.data.length; i++) { | ||
if (frame.data[i] === 0) { | ||
// parse the description and URL fields | ||
frame.owner = parseIso88591(frame.data, 0, i); | ||
break; | ||
} | ||
} | ||
frame.privateData = frame.data.subarray(i + 1); | ||
frame.data = frame.privateData; | ||
} | ||
}, | ||
id3 = require('../tools/parse-id3'), | ||
MetadataStream; | ||
@@ -245,3 +84,3 @@ | ||
// results concatenated to recover the actual value. | ||
tagSize = parseSyncSafeInteger(chunk.data.subarray(6, 10)); | ||
tagSize = id3.parseSyncSafeInteger(chunk.data.subarray(6, 10)); | ||
@@ -277,6 +116,6 @@ // ID3 reports the tag size excluding the header but it's more | ||
frameStart += 4; // header size field | ||
frameStart += parseSyncSafeInteger(tag.data.subarray(10, 14)); | ||
frameStart += id3.parseSyncSafeInteger(tag.data.subarray(10, 14)); | ||
// clip any padding off the end | ||
tagSize -= parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
tagSize -= id3.parseSyncSafeInteger(tag.data.subarray(16, 20)); | ||
} | ||
@@ -288,3 +127,3 @@ | ||
// determine the number of bytes in this frame | ||
frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
frameSize = id3.parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8)); | ||
if (frameSize < 1) { | ||
@@ -312,11 +151,11 @@ this.trigger('log', { | ||
// parse frame values | ||
if (frameParsers[frame.id]) { | ||
if (id3.frameParsers[frame.id]) { | ||
// use frame specific parser | ||
frameParsers[frame.id](frame); | ||
id3.frameParsers[frame.id](frame); | ||
} else if (frame.id[0] === 'T') { | ||
// use text frame generic parser | ||
frameParsers['T*'](frame); | ||
id3.frameParsers['T*'](frame); | ||
} else if (frame.id[0] === 'W') { | ||
// use URL link frame generic parser | ||
frameParsers['W*'](frame); | ||
id3.frameParsers['W*'](frame); | ||
} | ||
@@ -323,0 +162,0 @@ |
@@ -15,2 +15,3 @@ /** | ||
var parseType = require('../mp4/parse-type.js'); | ||
var emsg = require('../mp4/emsg.js'); | ||
var parseTfhd = require('../tools/parse-tfhd.js'); | ||
@@ -21,4 +22,5 @@ var parseTrun = require('../tools/parse-trun.js'); | ||
var timescale, startTime, compositionStartTime, getVideoTrackIds, getTracks, | ||
getTimescaleFromMediaHeader; | ||
getTimescaleFromMediaHeader, getEmsgID3; | ||
var window = require('global/window'); | ||
var parseId3Frames = require('../tools/parse-id3.js').parseId3Frames; | ||
@@ -375,2 +377,25 @@ | ||
/** | ||
* Returns an array of emsg ID3 data from the provided segmentData. | ||
* An offset can also be provided as the Latest Arrival Time to calculate | ||
* the Event Start Time of v0 EMSG boxes. | ||
* See: https://dashif-documents.azurewebsites.net/Events/master/event.html#Inband-event-timing | ||
* | ||
* @param {Uint8Array} segmentData the segment byte array. | ||
* @param {number} offset the segment start time or Latest Arrival Time, | ||
* @return {Object[]} an array of ID3 parsed from EMSG boxes | ||
*/ | ||
getEmsgID3 = function(segmentData, offset = 0) { | ||
var emsgBoxes = findBox(segmentData, ['emsg']); | ||
return emsgBoxes.map((data) => { | ||
var parsedBox = emsg.parseEmsgBox(new Uint8Array(data)); | ||
var parsedId3Frames = parseId3Frames(parsedBox.message_data); | ||
return { | ||
cueTime: emsg.scaleTime(parsedBox.presentation_time, parsedBox.timescale, parsedBox.presentation_time_delta, offset), | ||
duration: emsg.scaleTime(parsedBox.event_duration, parsedBox.timescale), | ||
frames: parsedId3Frames | ||
}; | ||
}); | ||
}; | ||
module.exports = { | ||
@@ -385,3 +410,4 @@ // export mp4 inspector's findBox and parseType for backwards compatibility | ||
tracks: getTracks, | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader | ||
getTimescaleFromMediaHeader: getTimescaleFromMediaHeader, | ||
getEmsgID3: getEmsgID3, | ||
}; |
{ | ||
"name": "mux.js", | ||
"version": "6.2.0", | ||
"version": "6.3.0", | ||
"description": "A collection of lightweight utilities for inspecting and manipulating video container formats.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -8,2 +8,3 @@ 'use strict'; | ||
box = mp4Helpers.box, | ||
id3 = require('./utils/id3-generator'), | ||
@@ -147,2 +148,75 @@ // defined below | ||
QUnit.test('can get ID3 data from a v0 EMSG box', function(assert) { | ||
var id3Data = new Uint8Array(id3.id3Tag(id3.id3Frame('PRIV', | ||
id3.stringToCString('priv-owner@example.com'), | ||
id3.stringToInts('foo.bar.id3.com'))) | ||
); | ||
var v0EmsgId3Data = mp4Helpers.generateEmsgBoxData(0, id3Data); | ||
var emsgId3Box = new Uint8Array(box('emsg', [].slice.call(v0EmsgId3Data))); | ||
var emsgBoxes = probe.getEmsgID3(emsgId3Box, 10); | ||
assert.equal(emsgBoxes[0].cueTime, 20, 'got correct emsg cueTime value from v0 emsg'); | ||
assert.equal(emsgBoxes[0].duration, 0, 'got correct emsg duration value from v0 emsg'); | ||
assert.equal(emsgBoxes[0].frames[0].id, 'PRIV' , 'got correct ID3 id'); | ||
assert.equal(emsgBoxes[0].frames[0].owner, 'priv-owner@example.com', 'got correct ID3 owner'); | ||
assert.deepEqual(emsgBoxes[0].frames[0].data, new Uint8Array(id3.stringToInts('foo.bar.id3.com')), 'got correct ID3 data'); | ||
}); | ||
QUnit.test('can get ID3 data from a v1 EMSG box', function(assert) { | ||
var id3Data = new Uint8Array(id3.id3Tag(id3.id3Frame('TXXX', | ||
0x03, // utf-8 | ||
id3.stringToCString('foo bar'), | ||
id3.stringToCString('{ "key": "value" }')), | ||
[0x00, 0x00]) | ||
); | ||
var v1EmsgId3Data = mp4Helpers.generateEmsgBoxData(1, id3Data); | ||
var emsgId3Box = new Uint8Array(box('emsg', [].slice.call(v1EmsgId3Data))); | ||
var emsgBoxes = probe.getEmsgID3(emsgId3Box); | ||
assert.equal(emsgBoxes[0].cueTime, 100, 'got correct emsg cueTime value from v1 emsg'); | ||
assert.equal(emsgBoxes[0].duration, 0.01, 'got correct emsg duration value from v1 emsg'); | ||
assert.equal(emsgBoxes[0].frames[0].id, 'TXXX' , 'got correct ID3 id'); | ||
assert.equal(emsgBoxes[0].frames[0].description, 'foo bar', 'got correct ID3 description'); | ||
assert.deepEqual(JSON.parse(emsgBoxes[0].frames[0].data), { key: 'value' }, 'got correct ID3 data'); | ||
}); | ||
QUnit.test('can get ID3 data from multiple EMSG boxes', function(assert) { | ||
var v1id3Data = new Uint8Array(id3.id3Tag(id3.id3Frame('PRIV', | ||
id3.stringToCString('priv-owner@example.com'), | ||
id3.stringToInts('foo.bar.id3.com'))) | ||
); | ||
var v0id3Data = new Uint8Array(id3.id3Tag(id3.id3Frame('TXXX', | ||
0x03, // utf-8 | ||
id3.stringToCString('foo bar'), | ||
id3.stringToCString('{ "key": "value" }')), | ||
[0x00, 0x00]) | ||
); | ||
var v1EmsgId3Data = mp4Helpers.generateEmsgBoxData(1, v1id3Data); | ||
var v1emsgId3Box = new Uint8Array(box('emsg', [].slice.call(v1EmsgId3Data))); | ||
var v0EmsgId3Data = mp4Helpers.generateEmsgBoxData(0, v0id3Data); | ||
var v0emsgId3Box = new Uint8Array(box('emsg', [].slice.call(v0EmsgId3Data))); | ||
var multiBoxData = new Uint8Array(v1emsgId3Box.length + v0emsgId3Box.length); | ||
multiBoxData.set(v1emsgId3Box); | ||
multiBoxData.set(v0emsgId3Box, v1emsgId3Box.length); | ||
var emsgBoxes = probe.getEmsgID3(multiBoxData); | ||
assert.equal(emsgBoxes[0].cueTime, 100, 'got correct emsg cueTime value from v1 emsg'); | ||
assert.equal(emsgBoxes[0].duration, 0.01, 'got correct emsg duration value from v1 emsg'); | ||
assert.equal(emsgBoxes[0].frames[0].id, 'PRIV' , 'got correct ID3 id'); | ||
assert.equal(emsgBoxes[0].frames[0].owner, 'priv-owner@example.com', 'got correct ID3 owner'); | ||
assert.deepEqual(emsgBoxes[0].frames[0].data, new Uint8Array(id3.stringToInts('foo.bar.id3.com')), 'got correct ID3 data'); | ||
assert.equal(emsgBoxes[1].cueTime, 10, 'got correct emsg cueTime value from v0 emsg'); | ||
assert.equal(emsgBoxes[1].duration, 0, 'got correct emsg duration value from v0 emsg'); | ||
assert.equal(emsgBoxes[1].frames[0].id, 'TXXX' , 'got correct ID3 id'); | ||
assert.equal(emsgBoxes[1].frames[0].description, 'foo bar', 'got correct ID3 description'); | ||
assert.deepEqual(JSON.parse(emsgBoxes[1].frames[0].data),{ key: 'value' }, 'got correct ID3 data'); | ||
}); | ||
// --------- | ||
@@ -149,0 +223,0 @@ // Test Data |
@@ -314,2 +314,62 @@ /** | ||
/** | ||
* Generates generic emsg box data for both v0 and v1 boxes concats the messageData | ||
* and returns the result as a Uint8Array. Passing any version other than 0 or 1 will | ||
* return an invalid EMSG box. | ||
*/ | ||
var generateEmsgBoxData = function(version, messageData) { | ||
var emsgProps; | ||
if (version === 0) { | ||
emsgProps = new Uint8Array([ | ||
0x00, // version | ||
0x00, 0x00, 0x00, //flags | ||
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0 | ||
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00, // foo.bar.value\0 | ||
0x00, 0x00, 0x00, 0x64, // timescale = 100 | ||
0x00, 0x00, 0x03, 0xE8, // presentation_time_delta = 1000 | ||
0x00, 0x00, 0x00, 0x00, // event_duration = 0 | ||
0x00, 0x00, 0x00, 0x01 // id = 1 | ||
]); | ||
} else if (version === 1) { | ||
emsgProps = new Uint8Array([ | ||
0x01, // version | ||
0x00, 0x00, 0x00, //flags | ||
0x00, 0x00, 0x00, 0x64, // timescale = 100 | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x10, // presentation_time = 10000 | ||
0x00, 0x00, 0x00, 0x01, // event_duration = 1 | ||
0x00, 0x00, 0x00, 0x02, // id = 2 | ||
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0 | ||
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00 // foo.bar.value\0 | ||
]); | ||
} else if (version === 2) { | ||
// Invalid version only | ||
emsgProps = new Uint8Array([ | ||
0x02, // version | ||
0x00, 0x00, 0x00, //flags | ||
0x00, 0x00, 0x00, 0x64, // timescale = 100 | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x10, // presentation_time = 10000 | ||
0x00, 0x00, 0x00, 0x01, // event_duration = 1 | ||
0x00, 0x00, 0x00, 0x02, // id = 2 | ||
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0 | ||
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00 // foo.bar.value\0 | ||
]); | ||
} else { | ||
emsgProps = new Uint8Array([ | ||
// malformed emsg data | ||
0x00, 0x00, 0x00, 0x64, // timescale = 100 | ||
// no presentation_time | ||
0x00, 0x00, 0x00, 0x01, // event_duration = 1 | ||
// no id | ||
0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 0x3A, 0x62, 0x61, 0x72, 0x3A, 0x32, 0x30, 0x32, 0x33, 0x00, // urn:foo:bar:2023\0 | ||
0x66, 0x6F, 0x6F, 0x2E, 0x62, 0x61, 0x72, 0x2E, 0x76, 0x61, 0x6C, 0x75, 0x65, 0x00 // foo.bar.value\0 | ||
]); | ||
} | ||
// concat the props and messageData | ||
var retArr = new Uint8Array(emsgProps.length + messageData.length); | ||
retArr.set(emsgProps); | ||
retArr.set(messageData, emsgProps.length); | ||
return retArr; | ||
}; | ||
module.exports = { | ||
@@ -319,3 +379,4 @@ typeBytes, | ||
unityMatrix, | ||
box | ||
box, | ||
generateEmsgBoxData | ||
}; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4698477
224
79952
2