pr-player 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- const D = '(function(){"use strict";class P{pendingPayloads=[];payload=new Uint8Array(0);chunks=[];push=t=>{this.pendingPayloads.push(t)};next=(t=0)=>{this.payload=this.payload.slice(t);const s=this.pendingPayloads.shift();if(!s)return!1;const e=new Uint8Array(this.payload.byteLength+s.byteLength);return e.set(this.payload,0),e.set(s,this.payload.byteLength),this.payload=e,new DataView(this.payload.buffer)};pushChunk=t=>{this.chunks.push(t),this.chunks.length>1e3&&this.chunks.shift()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array(0),this.chunks=[]}}const I=(y,t)=>{const s=new Uint8Array(y),e=new Uint8Array(t),n=new Uint8Array(11+s.length+e.length);let a=0;return n[a++]=1,n[a++]=s[1],n[a++]=s[2],n[a++]=s[3],n[a++]=255,n[a++]=225,n[a++]=s.length>>8&255,n[a++]=s.length&255,n.set(s,a),a+=s.length,n[a++]=1,n[a++]=e.length>>8&255,n[a++]=e.length&255,n.set(e,a),n},T=y=>{let t=0;const s=new DataView(y.buffer),e=s.getUint8(t);if(t=t+1,e!==1)throw new Error("Invalid AVC version");const n=s.getUint8(t)&255;t=t+1;const a=s.getUint8(t)&255;t=t+1;const o=s.getUint8(t)&255;t=t+1;const c=`avc1.${Array.from([n,a,o],U=>U.toString(16).padStart(2,"0")).join("")}`,r=(s.getUint8(t)&3)-1;t=t+1;const g=s.getUint8(t)&31;t=t+1;const p=s.getUint16(t,!1);t=t+2;const h=new Uint8Array(s.buffer.slice(t,t+p));t=t+p;const d=s.getUint8(t)&31;t=t+1;const u=s.getUint16(t,!1);t=t+2;const f=new Uint8Array(s.buffer.slice(t,t+u));return t=t+u,{version:e,codec:c,profile:n,compatibility:a,level:o,lengthSizeMinusOne:r,numOfSequenceParameterSets:g,sequenceParameterSetLength:p,sps:h,numOfPictureParameterSets:d,pictureParameterSetLength:u,pps:f}},M=y=>{const t=new Uint8Array(4+y.length);return new DataView(t.buffer).setUint32(0,y.length,!1),t.set(y,4),t},C=y=>{let t=0;for(const n of y)t+=n.length;const s=new Uint8Array(t);let e=0;for(const n of y){const a=n;s.set(a,e),e+=a.length}return s},D=y=>{let t="unknown";switch(y){case 1:case 2:case 27:case 36:t="video";break;case 3:case 4:case 15:t="audio";break;case 6:t="subtitle";break;case 134:t="ad";break}return t};class x{pat;pmt;audioConfig;videoConfig;payloadMap=new Map;on={};constructor(){}parse=async t=>{let s=0;for(;!(s+188>t.byteLength);){if(t.getInt8(s)!=71){s++;continue}await this.parsePacket(t,s),s+=188}return s};parsePacket=async(t,s)=>{if(s+188>t.byteLength)throw new Error("Invalid TS packet");if(t.getUint8(s)!==71)throw new Error("Invalid TS packet");let e=s;const n=this.parseHeader(t,e);e+=4;const{transport_error_indicator:a,pid:o,payload_unit_start_indicator:l,adaptation_field_control:i}=n;if(a===1||o===void 0)return;let c=184;if(i===2||i===3){const r=t.getUint8(e);e+=1,this.parseAdaptationField(t,e),e+=r,c-=r}if(i===3&&(c-=1),i===1||i===3){const r=new Uint8Array(t.buffer.slice(e,e+c));if(o===0)return this.parsePAT(t,e);{const{programs:h=[]}=this.pat||{};if(h.find(u=>u.pmt_pid===o))return this.parsePMT(t,e)}const{streams:g=[]}=this.pmt||{},p=g.find(h=>h.elementary_pid===o);if(p){if(l===1){const h=this.payloadMap.get(o);if(h){switch(p.kind){case"audio":{const d=await this.parseAudio(h);this.on.chunk&&this.on.chunk(d)}break;case"video":{const d=await this.parseVideo(h);this.on.chunk&&this.on.chunk(d),await new Promise(u=>setTimeout(()=>u(!0),0))}break}this.payloadMap.delete(o)}}{this.payloadMap.has(o)||this.payloadMap.set(o,new Uint8Array);const h=this.payloadMap.get(o),d=new Uint8Array(h.byteLength+r.byteLength);d.set(h,0),d.set(r,h.byteLength),this.payloadMap.set(o,d)}}}};parseHeader=(t,s)=>{let e=s;const n=t.getUint8(e),a=t.getUint8(e+1),o=t.getUint8(e+2),l=t.getUint8(e+3),i=(a&128)>>7;if(i===1)return{sync_byte:n,transport_error_indicator:i};const c=(a&64)>>6,r=(a&32)>>5,g=(a&31)<<8|o,p=(l&192)>>6,h=l>>4&3,d=l&15;return{sync_byte:n,transport_error_indicator:i,payload_unit_start_indicator:c,transport_priority:r,pid:g,transport_scrambling_control:p,adaptation_field_control:h,continuity_counter:d}};parsePAT=(t,s)=>{let e=s,n;{const l=t.getUint8(e);e+=1;const i=t.getUint8(e);if(e+=1,i!==0)throw new Error("Invalid PAT table_id");const c=t.getUint16(e)&4095;e+=2;const r=t.getUint16(e);e+=2;const g=(t.getUint8(e)&62)>>1,p=t.getUint8(e)&1;e+=1;const h=t.getUint8(e);e+=1;const d=t.getUint8(e);e+=1,n={pointer_field:l,table_id:i,section_length:c,transport_stream_id:r,version_number:g,current_next_indicator:p,section_number:h,last_section_number:d}}const a=[];{const l=n.section_length-5-4,i=e+l;for(;e<i;){const c=t.getUint16(e),r=t.getUint16(e+2)&8191;e+=4,c!==0&&r>=32&&r<=8190&&a.push({program_number:c,pmt_pid:r})}}const o=t.getUint32(e);this.pat={header:n,programs:a,crc32:o},this.on.debug&&this.on.debug({pat:this.pat})};parsePMT=(t,s)=>{let e=s,n;{const l=t.getUint8(e);e+=1;const i=t.getUint8(e);if(e+=1,i!==2)throw new Error("Invalid PMT table_id");const c=t.getUint16(e)&4095;e+=2;const r=t.getUint16(e);e+=2;const g=(t.getUint8(e)&62)>>1,p=t.getUint8(e)&1;e+=1;const h=t.getUint8(e);e+=1;const d=t.getUint8(e);e+=1;const u=t.getUint16(e)&8191;e+=2;const f=t.getUint16(e)&4095;e+=2,n={pointer_field:l,table_id:i,section_length:c,transport_stream_id:r,version_number:g,current_next_indicator:p,section_number:h,last_section_number:d,pcr_pid:u,program_info_length:f}}const a=[];{const l=n.section_length-9-4,i=e+l;for(;e<i;){const c=t.getUint8(e),r=D(c),g=t.getUint16(e+1)&8191,p=t.getUint16(e+3)&4095;if(e+=5,g<32||g>8190){console.warn(`Invalid elementary_pid: 0x${g.toString(16)}`);continue}a.push({kind:r,stream_type:c,elementary_pid:g,es_info_length:p})}}const o=t.getUint32(e);this.pmt={header:n,streams:a,crc32:o},this.on.debug&&this.on.debug({pmt:this.pmt})};parseAdaptationField=(t,s)=>{let e=s,n,a,o,l;const i=t.getUint8(e),c=!!(i&128),r=!!(i&64),g=!!(i&32),p=!!(i&16),h=!!(i&8),d=!!(i&4),u=!!(i&2),f=!!(i&1);e+=1;const U=(_,b)=>{let m=BigInt(0);m|=BigInt(_.getUint16(b))<<25n,m|=BigInt(_.getUint16(b+1))<<17n,m|=BigInt(_.getUint16(b+2))<<9n,m|=BigInt(_.getUint16(b+3))<<1n,m|=BigInt(_.getUint16(b+4)>>7);const S=(_.getUint16(b+4)&1)<<8|_.getUint16(b+5);return m=m*300n+BigInt(S),m};if(p&&(n=U(t,e),e+=6),h&&(a=U(t,e),e+=6),d&&(o=t.getInt8(e),e+=1),u){const _=t.getUint8(e);e+=1,l=new Uint8Array(t.buffer,e,_),e+=_}return{discontinuity_indicator:c,random_access_indicator:r,elementary_stream_priority_indicator:g,pcr_flag:p,opcr_flag:h,splicing_point_flag:d,transport_private_data_flag:u,adaptation_field_extension_flag:f,pcr:n,opcr:a,splice_countdown:o,transport_private_data:l}};parseAudio=async t=>{const s=new DataView(t.buffer);let e=0,n,a;{const o=s.getUint8(e)===0&&s.getUint8(e+1)===0&&s.getUint8(e+2)===1;if(e+=3,!o)throw new Error("invalid ts audio payload.");const l=s.getUint8(e);e+=1;const i=s.getUint8(e)<<8|s.getUint8(e+1);e+=2;let c,r,g,p,h;{const f=s.getUint8(e);e+=1,c=f>>4&3,r=(f>>3&1)===1,g=(f>>2&1)===1,p=(f>>1&1)===1,h=(f&1)===1}let d,u;{const f=s.getUint8(e);e+=1;const U=f>>6,_=s.getUint8(e);e+=1,(U&2)===2&&(d=this.parsePtsDts(s,e)),(U&1)===1?u=this.parsePtsDts(s,e+5):u=d,e+=_}n={stream_id:l,pes_packet_length:i,scrambling_control:c,priority:r,data_alignment:g,copyright:p,original_copy:h,pts:d,dts:u}}a=t.slice(e);{if(!this.audioConfig&&s.getUint8(e)===255){const g=s.getUint8(e+2),p=s.getUint8(e+3);let h,d;d=g>>2&15,h=(g&1)<<2|p>>6;const u=`mp4a.40.${h}`,U=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350][d];this.audioConfig={kind:"audio",codec:u,sampleRate:U,numberOfChannels:h},this.on.config&&this.on.config(this.audioConfig)}const{dts:o=0,pts:l=0}=n,i=l-o,c=a.slice(7);return{kind:"audio",type:"key",dts:o,pts:l,cts:i,data:c}}};parseVideo=async t=>{const s=new DataView(t.buffer);let e=0,n,a;{const o=s.getUint8(e)===0&&s.getUint8(e+1)===0&&s.getUint8(e+2)===1;if(e+=3,!o)throw new Error("invalid ts video payload.");const l=s.getUint8(e);e+=1;const i=s.getUint8(e)<<8|s.getUint8(e+1);e+=2;let c,r,g,p,h;{const f=s.getUint8(e);e+=1,c=f>>4&3,r=(f>>3&1)===1,g=(f>>2&1)===1,p=(f>>1&1)===1,h=(f&1)===1}let d,u;{const f=s.getUint8(e);e+=1;const U=f>>6,_=s.getUint8(e);e+=1,(U&2)===2&&(d=this.parsePtsDts(s,e)),(U&1)===1?u=this.parsePtsDts(s,e+5):u=d,e+=_}n={stream_id:l,pes_packet_length:i,scrambling_control:c,priority:r,data_alignment:g,copyright:p,original_copy:h,pts:d,dts:u}}a=t.slice(e);{const o=this.getNalus(a);if(!this.videoConfig){let h,d;if(h=o.find(f=>f.type===7)?.nalu.slice(4),d=o.find(f=>f.type===8)?.nalu.slice(4),h&&d){const u=I(h,d),{codec:f}=T(u);this.videoConfig={kind:"video",codec:f,description:u,sps:h,pps:d},this.on.config&&this.on.config(this.videoConfig)}}const l=[];let i="delta";for(const h of o){const{type:d,nalu:u}=h;switch(d){case 6:case 9:l.push(u);break;case 1:i="delta",l.push(u);break;case 5:i="key",l.push(u);break}}const c=C(l),{dts:r=0,pts:g=0}=n,p=g-r;return{kind:"video",type:i,dts:r,pts:g,cts:p,data:c,nalus:l}}};parsePtsDts(t,s){const e=t.getUint8(s),n=t.getUint8(s+1),a=t.getUint8(s+2),o=t.getUint8(s+3),l=t.getUint8(s+4),i=(BigInt(e)&0b00001110n)<<29n|(BigInt(n)&0b11111111n)<<22n|(BigInt(a)&0b11111110n)<<14n|(BigInt(o)&0b11111111n)<<7n|(BigInt(l)&0b11111110n)>>1n;return Number(i)/90}getNalus=t=>{const s=[];let e=0;for(;!(e+4>t.byteLength);){if(t[e]!==0||t[e+1]!==0||t[e+2]!==1){e+=1;continue}e+=3;let n=e;const a=t[e]&31;for(e+=1;!(e+1>t.byteLength);){if(t[e]!==0||t[e+1]!==0||t[e+2]!==1){e+=1;continue}break}let o=e-n;if(t[e-1]===0&&(o-=1),o!==0){const l=t.slice(n,n+o),i=M(l);s.push({type:a,nalu:i})}}return s}}const k=(y,t)=>y.getUint8(t)<<16|y.getUint8(t+1)<<8|y.getUint8(t+2);class L{audioConfig;videoConfig;header;textDecoder=new TextDecoder("utf-8");on={};constructor(){}parse=async t=>{let s=0;for(this.header||(this.parseHeader(t,s),s+=9);this.isSurplusTag(t,s)!==!1;){const n=this.parseTagHeader(t,s+4),{tagType:a,dataSize:o,timestamp:l}=n;if(a){const i=this.parseTagBody(a,t,s+4+11,o);switch(a){case"script":this.on.info&&this.on.info(i);break;case"audio":{const{accPacketType:c}=i;if(c===0){const{codec:r,sampleRate:g,channelConfiguration:p}=i;this.audioConfig={kind:"audio",codec:r,sampleRate:g,numberOfChannels:p},this.on.config&&this.on.config(this.audioConfig)}else{const{cts:r,data:g}=i,p="key",h=r===void 0?void 0:r+l;this.on.chunk&&this.on.chunk({kind:"audio",type:p,dts:l,pts:h,cts:r,data:g})}}break;case"video":{const{avcPacketType:c}=i;if(c===0){const{codec:r,sps:g,pps:p,data:h}=i;this.videoConfig={kind:"video",codec:r,description:h,sps:g,pps:p},this.on.config&&this.on.config(this.videoConfig)}else{const{frameType:r,cts:g,data:p,nalus:h}=i,d=r===1?"key":"delta",u=g===void 0?void 0:g+l;this.on.chunk&&this.on.chunk({kind:"video",type:d,dts:l,pts:u,cts:g,data:p,nalus:h})}}break}s=s+4+11+o}await new Promise(i=>setTimeout(()=>i(!0),8))}return s};parseHeader=(t,s)=>{let e,n,a,o;e=t.getUint8(s)<<16|t.getUint8(s+1)<<8|t.getUint8(s+2),n=t.getUint8(3);{const i=t.getUint8(0).toString(2).padStart(5,"0").split(""),[,,c,,r]=i;a={audio:r==="1",video:c==="1"}}o=t.getUint32(5),this.header={signature:e,version:n,flags:a,dataOffset:o}};isSurplusTag=(t,s)=>{let e=!0;const n=t.byteLength;if(s+4>n)e=!1;else if(s+4+11>n)e=!1;else{const a=k(t,s+4+1);s+4+11+a>n&&(e=!1)}return e};parseTagHeader=(t,s)=>{let e,n,a,o,l;{const i=t.getUint8(s);let c;switch(i){case 18:c="script";break;case 8:c="audio";break;case 9:c="video";break}e=c}return n=k(t,s+1),a=k(t,s+4),o=t.getUint8(s+7),l=k(t,s+8),{tagType:e,dataSize:n,timestamp:a,timestampExtended:o,streamID:l}};parseTagBody=(t,s,e,n)=>{let a;switch(t){case"script":a=this.parseMetaData(s,e);break;case"audio":a=this.parseAudio(s,e,n);break;case"video":a=this.parseVideo(s,e,n);break}return a};parseMetaData=(t,s)=>{let e=s;{if(t.getUint8(e)!==2)throw new Error("Invalid AMF type for onMetaData (expected 0x02)");e=e+1}const n=t.getUint16(e,!1);e=e+2;{const l=new Int8Array(t.buffer.slice(e,e+n));if((this.textDecoder?.decode(l)||"")!=="onMetaData")throw new Error("Expected \'onMetaData\' string");e=e+n}const a=this.getAmfType(t,e);return e=e+1,this.getAMFValue(t,e,a).value};parseAudio=(t,s,e)=>{let n=s;const a=t.getUint8(n),o=a>>4&15,l=a>>2&3,i=a>>1&1,c=a&1;n=n+1;const r=t.getUint8(n);n=n+1;const g=e,p=new Uint8Array(t.buffer.slice(n,n+g));if(o===10&&r===0){const h=t.getUint8(n),d=t.getUint8(n+1),u=h>>3&31,f=(h&7)<<1|d>>7,U=d>>3&15,_=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],b=`mp4a.40.${u}`,m=_[f];return{soundFormat:o,soundRate:l,soundSize:i,soundType:c,accPacketType:r,data:p,samplingFrequencyIndex:f,channelConfiguration:U,codec:b,sampleRate:m}}return{soundFormat:o,soundRate:l,soundSize:i,soundType:c,accPacketType:r,data:p}};parseVideo=(t,s,e)=>{let n=s;const a=t.getUint8(n),o=a>>4&15,l=a&15;n=n+1;const i=t.getUint8(n);n=n+1;const c=k(t,n);n=n+3;const r=e-5,g=new Uint8Array(t.buffer.slice(n,n+r));switch(l){case 7:if(i===0){const p=T(g);return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g,...p}}else if(i===1){const p=[],h=n+e-5;for(;!(n+4>h);){const d=t.getUint32(n,!1),u=new Uint8Array(t.buffer.slice(n,n+4+d));n+=4+d,p.push(u)}return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g,nalus:p}}break;default:throw new Error("Unsupported codecID")}return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g}};getAmfType=(t,s)=>t.getUint8(s);getAMFName=(t,s,e)=>{const n=new Uint8Array(t.buffer.slice(s,s+e));return this.textDecoder?.decode(n)||""};getAMFValue=(t,s,e)=>{let n=s,a,o=0;switch(e){case 0:a=t.getFloat64(n,!1),o=8;break;case 1:a=!!t.getUint8(n),o=1;break;case 2:{a="";const i=t.getUint16(n,!1);n=n+2;const c=new Int8Array(t.buffer,n,i).filter(g=>g!==0);a=(this.textDecoder?.decode(c)||"").trim(),o=2+i}break;case 3:for(a={};n<t.byteLength;){const i=t.getUint16(n,!1);if(i===0)break;n=n+2;const c=this.getAMFName(t,n,i);n=n+i;const r=this.getAmfType(t,n);if(r===6)break;n=n+1;const g=this.getAMFValue(t,n,r);n=n+g.length,a[c]=g.value,o=2+i+1+g.length}break;case 8:{a={};const i=t.getUint32(n,!1);n=n+4;for(let c=0;c<i;c++){const r=t.getUint16(n,!1);n=n+2;const g=this.getAMFName(t,n,r);n=n+r;const p=this.getAmfType(t,n);n=n+1;const h=this.getAMFValue(t,n,p);n=n+h.length,a[g]=h.value,o=2+r+1+h.length}}break;case 10:{a=[];const i=t.getUint32(n,!1);n=n+4;for(let c=0;c<i;c++){const r=this.getAmfType(t,n);n=n+1;const g=this.getAMFValue(t,n,r);n=n+g.length,a.push(g.value),o=1+g.length}}break}return{amfType:e,length:o,value:a}}}class O{pattern;cacher=new P;isParseing=!1;offset=0;on={};parser;constructor(){}init=t=>{switch(this.destroy(),this.pattern=t,this.pattern){case"flv":this.parser=new L;break;case"hls":this.parser=new x;break;default:throw new Error("is error pattern.")}this.parser.on.debug=s=>this.on.debug&&this.on.debug(s),this.parser.on.info=s=>this.on.info&&this.on.info(s),this.parser.on.config=s=>this.on.config&&this.on.config(s),this.parser.on.chunk=s=>{this.cacher.pushChunk(s),this.on.chunk&&this.on.chunk(s)}};push=t=>{this.cacher.push(t),this.isParseing===!1&&this.parse()};destroy=()=>{this.cacher.destroy(),this.isParseing=!1,this.offset=0};parse=async()=>{try{if(this.isParseing=!0,!this.pattern)throw new Error("You need to set the pattern.");if(!this.parser)throw new Error("You need to init parser.");for(;;){const t=this.cacher.next(this.offset);if(this.offset=0,!t)break;this.offset=await this.parser.parse(t)}this.isParseing=!1}catch{this.destroy()}}}const A=new O;A.on.debug=y=>postMessage({action:"onDebug",data:y}),A.on.info=y=>postMessage({action:"onInfo",data:y}),A.on.config=y=>postMessage({action:"onConfig",data:y}),A.on.chunk=y=>postMessage({action:"onChunk",data:y}),onmessage=y=>{const{action:t,data:s}=y.data,e=A[t];e&&e(s)}})();\n', T = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", D], { type: "text/javascript;charset=utf-8" });
1
+ const D = '(function(){"use strict";class P{pendingPayloads=[];payload=new Uint8Array(0);chunks=[];push=t=>{this.pendingPayloads.push(t)};next=(t=0)=>{this.payload=this.payload.slice(t);const s=this.pendingPayloads.shift();if(!s)return!1;const e=new Uint8Array(this.payload.byteLength+s.byteLength);return e.set(this.payload,0),e.set(s,this.payload.byteLength),this.payload=e,new DataView(this.payload.buffer)};pushChunk=t=>{this.chunks.push(t),this.chunks.length>1e3&&this.chunks.shift()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array(0),this.chunks=[]}}const I=(y,t)=>{const s=new Uint8Array(y),e=new Uint8Array(t),n=new Uint8Array(11+s.length+e.length);let a=0;return n[a++]=1,n[a++]=s[1],n[a++]=s[2],n[a++]=s[3],n[a++]=255,n[a++]=225,n[a++]=s.length>>8&255,n[a++]=s.length&255,n.set(s,a),a+=s.length,n[a++]=1,n[a++]=e.length>>8&255,n[a++]=e.length&255,n.set(e,a),n},T=y=>{let t=0;const s=new DataView(y.buffer),e=s.getUint8(t);if(t=t+1,e!==1)throw new Error("Invalid AVC version");const n=s.getUint8(t)&255;t=t+1;const a=s.getUint8(t)&255;t=t+1;const o=s.getUint8(t)&255;t=t+1;const c=`avc1.${Array.from([n,a,o],U=>U.toString(16).padStart(2,"0")).join("")}`,r=(s.getUint8(t)&3)-1;t=t+1;const g=s.getUint8(t)&31;t=t+1;const p=s.getUint16(t,!1);t=t+2;const h=new Uint8Array(s.buffer.slice(t,t+p));t=t+p;const d=s.getUint8(t)&31;t=t+1;const u=s.getUint16(t,!1);t=t+2;const f=new Uint8Array(s.buffer.slice(t,t+u));return t=t+u,{version:e,codec:c,profile:n,compatibility:a,level:o,lengthSizeMinusOne:r,numOfSequenceParameterSets:g,sequenceParameterSetLength:p,sps:h,numOfPictureParameterSets:d,pictureParameterSetLength:u,pps:f}},M=y=>{const t=new Uint8Array(4+y.length);return new DataView(t.buffer).setUint32(0,y.length,!1),t.set(y,4),t},C=y=>{let t=0;for(const n of y)t+=n.length;const s=new Uint8Array(t);let e=0;for(const n of y){const a=n;s.set(a,e),e+=a.length}return s},D=y=>{let t="unknown";switch(y){case 1:case 2:case 27:case 36:t="video";break;case 3:case 4:case 15:t="audio";break;case 6:t="subtitle";break;case 134:t="ad";break}return t};class x{pat;pmt;audioConfig;videoConfig;payloadMap=new Map;on={};constructor(){}parse=async t=>{let s=0;for(;!(s+188>t.byteLength);){if(t.getInt8(s)!=71){s++;continue}await this.parsePacket(t,s),s+=188}return s};parsePacket=async(t,s)=>{if(s+188>t.byteLength)throw new Error("Invalid TS packet");if(t.getUint8(s)!==71)throw new Error("Invalid TS packet");let e=s;const n=this.parseHeader(t,e);e+=4;const{transport_error_indicator:a,pid:o,payload_unit_start_indicator:l,adaptation_field_control:i}=n;if(a===1||o===void 0)return;let c=184;if(i===2||i===3){const r=t.getUint8(e);e+=1,this.parseAdaptationField(t,e),e+=r,c-=r}if(i===3&&(c-=1),i===1||i===3){const r=new Uint8Array(t.buffer.slice(e,e+c));if(o===0)return this.parsePAT(t,e);{const{programs:h=[]}=this.pat||{};if(h.find(u=>u.pmt_pid===o))return this.parsePMT(t,e)}const{streams:g=[]}=this.pmt||{},p=g.find(h=>h.elementary_pid===o);if(p){if(l===1){const h=this.payloadMap.get(o);if(h){switch(p.kind){case"audio":{const d=await this.parseAudio(h);this.on.chunk&&this.on.chunk(d)}break;case"video":{const d=await this.parseVideo(h);this.on.chunk&&this.on.chunk(d),await new Promise(u=>setTimeout(()=>u(!0),0))}break}this.payloadMap.delete(o)}}{this.payloadMap.has(o)||this.payloadMap.set(o,new Uint8Array);const h=this.payloadMap.get(o),d=new Uint8Array(h.byteLength+r.byteLength);d.set(h,0),d.set(r,h.byteLength),this.payloadMap.set(o,d)}}}};parseHeader=(t,s)=>{let e=s;const n=t.getUint8(e),a=t.getUint8(e+1),o=t.getUint8(e+2),l=t.getUint8(e+3),i=(a&128)>>7;if(i===1)return{sync_byte:n,transport_error_indicator:i};const c=(a&64)>>6,r=(a&32)>>5,g=(a&31)<<8|o,p=(l&192)>>6,h=l>>4&3,d=l&15;return{sync_byte:n,transport_error_indicator:i,payload_unit_start_indicator:c,transport_priority:r,pid:g,transport_scrambling_control:p,adaptation_field_control:h,continuity_counter:d}};parsePAT=(t,s)=>{let e=s,n;{const l=t.getUint8(e);e+=1;const i=t.getUint8(e);if(e+=1,i!==0)throw new Error("Invalid PAT table_id");const c=t.getUint16(e)&4095;e+=2;const r=t.getUint16(e);e+=2;const g=(t.getUint8(e)&62)>>1,p=t.getUint8(e)&1;e+=1;const h=t.getUint8(e);e+=1;const d=t.getUint8(e);e+=1,n={pointer_field:l,table_id:i,section_length:c,transport_stream_id:r,version_number:g,current_next_indicator:p,section_number:h,last_section_number:d}}const a=[];{const l=n.section_length-5-4,i=e+l;for(;e<i;){const c=t.getUint16(e),r=t.getUint16(e+2)&8191;e+=4,c!==0&&r>=32&&r<=8190&&a.push({program_number:c,pmt_pid:r})}}const o=t.getUint32(e);this.pat={header:n,programs:a,crc32:o},this.on.debug&&this.on.debug({pat:this.pat})};parsePMT=(t,s)=>{let e=s,n;{const l=t.getUint8(e);e+=1;const i=t.getUint8(e);if(e+=1,i!==2)throw new Error("Invalid PMT table_id");const c=t.getUint16(e)&4095;e+=2;const r=t.getUint16(e);e+=2;const g=(t.getUint8(e)&62)>>1,p=t.getUint8(e)&1;e+=1;const h=t.getUint8(e);e+=1;const d=t.getUint8(e);e+=1;const u=t.getUint16(e)&8191;e+=2;const f=t.getUint16(e)&4095;e+=2,n={pointer_field:l,table_id:i,section_length:c,transport_stream_id:r,version_number:g,current_next_indicator:p,section_number:h,last_section_number:d,pcr_pid:u,program_info_length:f}}const a=[];{const l=n.section_length-9-4,i=e+l;for(;e<i;){const c=t.getUint8(e),r=D(c),g=t.getUint16(e+1)&8191,p=t.getUint16(e+3)&4095;if(e+=5,g<32||g>8190){console.warn(`Invalid elementary_pid: 0x${g.toString(16)}`);continue}a.push({kind:r,stream_type:c,elementary_pid:g,es_info_length:p})}}const o=t.getUint32(e);this.pmt={header:n,streams:a,crc32:o},this.on.debug&&this.on.debug({pmt:this.pmt})};parseAdaptationField=(t,s)=>{let e=s,n,a,o,l;const i=t.getUint8(e),c=!!(i&128),r=!!(i&64),g=!!(i&32),p=!!(i&16),h=!!(i&8),d=!!(i&4),u=!!(i&2),f=!!(i&1);e+=1;const U=(_,b)=>{let m=BigInt(0);m|=BigInt(_.getUint16(b))<<25n,m|=BigInt(_.getUint16(b+1))<<17n,m|=BigInt(_.getUint16(b+2))<<9n,m|=BigInt(_.getUint16(b+3))<<1n,m|=BigInt(_.getUint16(b+4)>>7);const S=(_.getUint16(b+4)&1)<<8|_.getUint16(b+5);return m=m*300n+BigInt(S),m};if(p&&(n=U(t,e),e+=6),h&&(a=U(t,e),e+=6),d&&(o=t.getInt8(e),e+=1),u){const _=t.getUint8(e);e+=1,l=new Uint8Array(t.buffer,e,_),e+=_}return{discontinuity_indicator:c,random_access_indicator:r,elementary_stream_priority_indicator:g,pcr_flag:p,opcr_flag:h,splicing_point_flag:d,transport_private_data_flag:u,adaptation_field_extension_flag:f,pcr:n,opcr:a,splice_countdown:o,transport_private_data:l}};parseAudio=async t=>{const s=new DataView(t.buffer);let e=0,n,a;{const o=s.getUint8(e)===0&&s.getUint8(e+1)===0&&s.getUint8(e+2)===1;if(e+=3,!o)throw new Error("invalid ts audio payload.");const l=s.getUint8(e);e+=1;const i=s.getUint8(e)<<8|s.getUint8(e+1);e+=2;let c,r,g,p,h;{const f=s.getUint8(e);e+=1,c=f>>4&3,r=(f>>3&1)===1,g=(f>>2&1)===1,p=(f>>1&1)===1,h=(f&1)===1}let d,u;{const f=s.getUint8(e);e+=1;const U=f>>6,_=s.getUint8(e);e+=1,(U&2)===2&&(d=this.parsePtsDts(s,e)),(U&1)===1?u=this.parsePtsDts(s,e+5):u=d,e+=_}n={stream_id:l,pes_packet_length:i,scrambling_control:c,priority:r,data_alignment:g,copyright:p,original_copy:h,pts:d,dts:u}}a=t.slice(e);{if(!this.audioConfig&&s.getUint8(e)===255){const g=s.getUint8(e+2),p=s.getUint8(e+3);let h,d;d=g>>2&15,h=(g&1)<<2|p>>6;const u=`mp4a.40.${h}`,U=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350][d];this.audioConfig={kind:"audio",codec:u,sampleRate:U,numberOfChannels:h},this.on.config&&this.on.config(this.audioConfig)}const{dts:o=0,pts:l=0}=n,i=l-o,c=a.slice(7);return{kind:"audio",type:"key",dts:o,pts:l,cts:i,data:c}}};parseVideo=async t=>{const s=new DataView(t.buffer);let e=0,n,a;{const o=s.getUint8(e)===0&&s.getUint8(e+1)===0&&s.getUint8(e+2)===1;if(e+=3,!o)throw new Error("invalid ts video payload.");const l=s.getUint8(e);e+=1;const i=s.getUint8(e)<<8|s.getUint8(e+1);e+=2;let c,r,g,p,h;{const f=s.getUint8(e);e+=1,c=f>>4&3,r=(f>>3&1)===1,g=(f>>2&1)===1,p=(f>>1&1)===1,h=(f&1)===1}let d,u;{const f=s.getUint8(e);e+=1;const U=f>>6,_=s.getUint8(e);e+=1,(U&2)===2&&(d=this.parsePtsDts(s,e)),(U&1)===1?u=this.parsePtsDts(s,e+5):u=d,e+=_}n={stream_id:l,pes_packet_length:i,scrambling_control:c,priority:r,data_alignment:g,copyright:p,original_copy:h,pts:d,dts:u}}a=t.slice(e);{const o=this.getNalus(a);if(!this.videoConfig){let h,d;if(h=o.find(f=>f.type===7)?.nalu.slice(4),d=o.find(f=>f.type===8)?.nalu.slice(4),h&&d){const u=I(h,d),{codec:f}=T(u);this.videoConfig={kind:"video",codec:f,description:u,sps:h,pps:d},this.on.config&&this.on.config(this.videoConfig)}}const l=[];let i="delta";for(const h of o){const{type:d,nalu:u}=h;switch(d){case 6:case 9:l.push(u);break;case 1:i="delta",l.push(u);break;case 5:i="key",l.push(u);break}}const c=C(l),{dts:r=0,pts:g=0}=n,p=g-r;return{kind:"video",type:i,dts:r,pts:g,cts:p,data:c,nalus:l}}};parsePtsDts(t,s){const e=t.getUint8(s),n=t.getUint8(s+1),a=t.getUint8(s+2),o=t.getUint8(s+3),l=t.getUint8(s+4),i=(BigInt(e)&0b00001110n)<<29n|(BigInt(n)&0b11111111n)<<22n|(BigInt(a)&0b11111110n)<<14n|(BigInt(o)&0b11111111n)<<7n|(BigInt(l)&0b11111110n)>>1n;return Number(i)/90}getNalus=t=>{const s=[];let e=0;for(;!(e+4>t.byteLength);){if(t[e]!==0||t[e+1]!==0||t[e+2]!==1){e+=1;continue}e+=3;let n=e;const a=t[e]&31;for(e+=1;!(e+1>t.byteLength);){if(t[e]!==0||t[e+1]!==0||t[e+2]!==1){e+=1;continue}break}let o=e-n;if(t[e-1]===0&&(o-=1),o!==0){const l=t.slice(n,n+o),i=M(l);s.push({type:a,nalu:i})}}return s}}const k=(y,t)=>y.getUint8(t)<<16|y.getUint8(t+1)<<8|y.getUint8(t+2);class L{audioConfig;videoConfig;header;textDecoder=new TextDecoder("utf-8");on={};constructor(){}parse=async t=>{let s=0;for(this.header||(this.parseHeader(t,s),s+=9);this.isSurplusTag(t,s)!==!1;){const n=this.parseTagHeader(t,s+4),{tagType:a,dataSize:o,timestamp:l}=n;if(a){const i=this.parseTagBody(a,t,s+4+11,o);switch(a){case"script":this.on.info&&this.on.info(i);break;case"audio":{const{accPacketType:c}=i;if(c===0){const{codec:r,sampleRate:g,channelConfiguration:p}=i;this.audioConfig={kind:"audio",codec:r,sampleRate:g,numberOfChannels:p},this.on.config&&this.on.config(this.audioConfig)}else{const{cts:r,data:g}=i,p="key",h=r===void 0?void 0:r+l;this.on.chunk&&this.on.chunk({kind:"audio",type:p,dts:l,pts:h,cts:r,data:g})}}break;case"video":{const{avcPacketType:c}=i;if(c===0){const{codec:r,sps:g,pps:p,data:h}=i;this.videoConfig={kind:"video",codec:r,description:h,sps:g,pps:p},this.on.config&&this.on.config(this.videoConfig)}else{const{frameType:r,cts:g,data:p,nalus:h}=i,d=r===1?"key":"delta",u=g===void 0?void 0:g+l;this.on.chunk&&this.on.chunk({kind:"video",type:d,dts:l,pts:u,cts:g,data:p,nalus:h})}}break}s=s+4+11+o}await new Promise(i=>setTimeout(()=>i(!0),8))}return s};parseHeader=(t,s)=>{let e,n,a,o;e=t.getUint8(s)<<16|t.getUint8(s+1)<<8|t.getUint8(s+2),n=t.getUint8(3);{const i=t.getUint8(0).toString(2).padStart(5,"0").split(""),[,,c,,r]=i;a={audio:r==="1",video:c==="1"}}o=t.getUint32(5),this.header={signature:e,version:n,flags:a,dataOffset:o}};isSurplusTag=(t,s)=>{let e=!0;const n=t.byteLength;if(s+4>n)e=!1;else if(s+4+11>n)e=!1;else{const a=k(t,s+4+1);s+4+11+a>n&&(e=!1)}return e};parseTagHeader=(t,s)=>{let e,n,a,o,l;{const i=t.getUint8(s);let c;switch(i){case 18:c="script";break;case 8:c="audio";break;case 9:c="video";break}e=c}return n=k(t,s+1),a=k(t,s+4),o=t.getUint8(s+7),l=k(t,s+8),{tagType:e,dataSize:n,timestamp:a,timestampExtended:o,streamID:l}};parseTagBody=(t,s,e,n)=>{let a;switch(t){case"script":a=this.parseMetaData(s,e);break;case"audio":a=this.parseAudio(s,e,n);break;case"video":a=this.parseVideo(s,e,n);break}return a};parseMetaData=(t,s)=>{let e=s;{if(t.getUint8(e)!==2)throw new Error("Invalid AMF type for onMetaData (expected 0x02)");e=e+1}const n=t.getUint16(e,!1);e=e+2;{const l=new Int8Array(t.buffer.slice(e,e+n));if((this.textDecoder?.decode(l)||"")!=="onMetaData")throw new Error("Expected \'onMetaData\' string");e=e+n}const a=this.getAmfType(t,e);return e=e+1,this.getAMFValue(t,e,a).value};parseAudio=(t,s,e)=>{let n=s;const a=t.getUint8(n),o=a>>4&15,l=a>>2&3,i=a>>1&1,c=a&1;n=n+1;const r=t.getUint8(n);n=n+1;const g=e,p=new Uint8Array(t.buffer.slice(n,n+g));if(o===10&&r===0){const h=t.getUint8(n),d=t.getUint8(n+1),u=h>>3&31,f=(h&7)<<1|d>>7,U=d>>3&15,_=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],b=`mp4a.40.${u}`,m=_[f];return{soundFormat:o,soundRate:l,soundSize:i,soundType:c,accPacketType:r,data:p,samplingFrequencyIndex:f,channelConfiguration:U,codec:b,sampleRate:m}}return{soundFormat:o,soundRate:l,soundSize:i,soundType:c,accPacketType:r,data:p}};parseVideo=(t,s,e)=>{let n=s;const a=t.getUint8(n),o=a>>4&15,l=a&15;n=n+1;const i=t.getUint8(n);n=n+1;const c=k(t,n);n=n+3;const r=e-5,g=new Uint8Array(t.buffer.slice(n,n+r));switch(l){case 7:if(i===0){const p=T(g);return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g,...p}}else if(i===1){const p=[],h=n+e-5;for(;!(n+4>h);){const d=t.getUint32(n,!1),u=new Uint8Array(t.buffer.slice(n,n+4+d));n+=4+d,p.push(u)}return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g,nalus:p}}break;default:throw new Error("Unsupported codecID")}return{frameType:o,codecID:l,avcPacketType:i,cts:c,data:g}};getAmfType=(t,s)=>t.getUint8(s);getAMFName=(t,s,e)=>{const n=new Uint8Array(t.buffer.slice(s,s+e));return this.textDecoder?.decode(n)||""};getAMFValue=(t,s,e)=>{let n=s,a,o=0;switch(e){case 0:a=t.getFloat64(n,!1),o=8;break;case 1:a=!!t.getUint8(n),o=1;break;case 2:{a="";const i=t.getUint16(n,!1);n=n+2;const c=new Int8Array(t.buffer,n,i).filter(g=>g!==0);a=(this.textDecoder?.decode(c)||"").trim(),o=2+i}break;case 3:for(a={};n<t.byteLength;){const i=t.getUint16(n,!1);if(i===0)break;n=n+2;const c=this.getAMFName(t,n,i);n=n+i;const r=this.getAmfType(t,n);if(r===6)break;n=n+1;const g=this.getAMFValue(t,n,r);n=n+g.length,a[c]=g.value,o=2+i+1+g.length}break;case 8:{a={};const i=t.getUint32(n,!1);n=n+4;for(let c=0;c<i;c++){const r=t.getUint16(n,!1);n=n+2;const g=this.getAMFName(t,n,r);n=n+r;const p=this.getAmfType(t,n);n=n+1;const h=this.getAMFValue(t,n,p);n=n+h.length,a[g]=h.value,o=2+r+1+h.length}}break;case 10:{a=[];const i=t.getUint32(n,!1);n=n+4;for(let c=0;c<i;c++){const r=this.getAmfType(t,n);n=n+1;const g=this.getAMFValue(t,n,r);n=n+g.length,a.push(g.value),o=1+g.length}}break}return{amfType:e,length:o,value:a}}}class O{pattern;cacher=new P;isParseing=!1;offset=0;on={};parser;constructor(){}init=t=>{switch(this.destroy(),this.pattern=t,this.pattern){case"flv":this.parser=new L;break;case"hls":this.parser=new x;break;default:throw new Error("is error pattern.")}this.parser.on.debug=s=>this.on.debug&&this.on.debug(s),this.parser.on.info=s=>this.on.info&&this.on.info(s),this.parser.on.config=s=>this.on.config&&this.on.config(s),this.parser.on.chunk=s=>{this.cacher.pushChunk(s),this.on.chunk&&this.on.chunk(s)}};push=t=>{this.cacher.push(t),this.isParseing===!1&&this.parse()};destroy=()=>{this.cacher.destroy(),this.isParseing=!1,this.offset=0};parse=async()=>{try{if(this.isParseing=!0,!this.pattern)throw new Error("You need to set the pattern.");if(!this.parser)throw new Error("You need to init parser.");for(;;){const t=this.cacher.next(this.offset);if(this.offset=0,!t)break;this.offset=await this.parser.parse(t)}this.isParseing=!1}catch{this.destroy()}}}const A=new O;A.on.info=y=>postMessage({action:"on.info",data:y}),A.on.config=y=>postMessage({action:"on.config",data:y}),A.on.chunk=y=>postMessage({action:"on.chunk",data:y}),A.on.debug=y=>postMessage({action:"on.debug",data:y}),onmessage=y=>{const{action:t,data:s}=y.data,e=A[t];e&&e(s)}})();\n', _ = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", D], { type: "text/javascript;charset=utf-8" });
2
2
  function L(l) {
3
3
  let t;
4
4
  try {
5
- if (t = T && (self.URL || self.webkitURL).createObjectURL(T), !t) throw "";
5
+ if (t = _ && (self.URL || self.webkitURL).createObjectURL(_), !t) throw "";
6
6
  const s = new Worker(t, {
7
7
  name: l?.name
8
8
  });
@@ -18,25 +18,25 @@ function L(l) {
18
18
  );
19
19
  }
20
20
  }
21
- class F {
21
+ class V {
22
22
  worker = new L();
23
23
  on = {};
24
24
  constructor() {
25
25
  this.worker.onmessage = (t) => {
26
26
  const { action: s, data: e } = t.data;
27
27
  switch (s) {
28
- case "onInfo":
28
+ case "on.info":
29
29
  this.on.info && this.on.info(e);
30
30
  break;
31
- case "onConfig":
31
+ case "on.config":
32
32
  this.on.config && this.on.config(e);
33
33
  break;
34
- case "onDebug":
35
- this.on.debug && this.on.debug(e);
36
- break;
37
- case "onChunk":
34
+ case "on.chunk":
38
35
  this.on.chunk && this.on.chunk(e);
39
36
  break;
37
+ case "on.debug":
38
+ this.on.debug && this.on.debug(e);
39
+ break;
40
40
  }
41
41
  };
42
42
  }
@@ -46,12 +46,12 @@ class F {
46
46
  this.worker.postMessage({ action: "destroy", data: {} }), this.worker.terminate();
47
47
  };
48
48
  }
49
- const S = `(function(){"use strict";class c{pattern="flv";audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;baseTime=0;pendingChunks=[];isProcessing=!1;decodeTimer=0;frameTrack=!1;isFrameTrack=!1;fameTrackOption={flv:[20,50],hls:[200,300],dash:[50,100],rtmp:[50,100]};decodingSpeed=16;fps=0;firstVideoChunkTimestamp;secondVideoChunkTimestamp;decodingSpeedRatio=1;maxDecodingSpeedRatio=2;lastRenderTime;nextRenderTime;on={audio:{},video:{}};constructor(){}init=e=>{this.destroy(),this.pattern=e,this.baseTime=new Date().getTime()-performance.now(),this.initDecodeInterval()};setFrameTrack=e=>{this.frameTrack=e,this.frameTrack===!1&&(this.decodingSpeedRatio=1)};initDecodeInterval=()=>{let e=this.decodingSpeed/this.decodingSpeedRatio;const i=this.baseTime+performance.now();if(this.lastRenderTime||(this.lastRenderTime=i),this.fps=Math.round(1e3/(i-this.lastRenderTime)),this.lastRenderTime=i,this.nextRenderTime){const t=this.lastRenderTime-this.nextRenderTime;e-=t}this.nextRenderTime=this.lastRenderTime+e,this.decodeTimer=setTimeout(()=>{this.decode(),this.initDecodeInterval()},e)};decode=()=>{if(this.isProcessing!==!0){for(this.isProcessing=!0;;){const e=this.pendingChunks.shift(),i=this.pendingChunks.length;if(this.frameTrack){const[n,r]=this.fameTrackOption[this.pattern];if(i<=n&&(this.isFrameTrack=!1),i>=r&&(this.isFrameTrack=!0),this.isFrameTrack){const a=Math.min(1+(i-n)/100,this.maxDecodingSpeedRatio);this.decodingSpeedRatio=Number(a.toFixed(1))}else this.decodingSpeedRatio=1}if(this.on.debug){const{decodingSpeed:n,decodingSpeedRatio:r,fps:a}=this;this.on.debug({decodingSpeed:n,decodingSpeedRatio:r,fps:a})}if(!e)break;const{type:t,init:o}=e;switch(t){case"audio":this.decodeAudio(o);break;case"video":this.decodeVideo(o);break}if(t==="video")break}this.isProcessing=!1}};decodeAudio=e=>{if(!this.audioDecoder)return;const i=new EncodedAudioChunk(e);this.audioDecoder.decode(i)};decodeVideo=e=>{if(this.videoDecoder&&(e.type==="key"&&(this.hasKeyFrame=!0),this.firstVideoChunkTimestamp?this.secondVideoChunkTimestamp||(this.secondVideoChunkTimestamp=e.timestamp,this.decodingSpeed=(this.secondVideoChunkTimestamp-this.firstVideoChunkTimestamp)/1e3):this.firstVideoChunkTimestamp=e.timestamp,this.hasKeyFrame)){const i=new EncodedVideoChunk(e);this.videoDecoder.decode(i)}};destroy=()=>{this.audio.destroy(),this.video.destroy(),clearInterval(this.decodeTimer)};audio={init:e=>{this.audio.destroy(),this.audioDecoderConfig={...e},this.audioDecoder=new AudioDecoder({output:i=>{const t=this.decodingSpeedRatio;this.on.audio.decode&&this.on.audio.decode({audioData:i,playbackRate:t})},error:i=>{this.on.audio.error&&this.on.audio.error(i)}}),this.audioDecoder.configure(this.audioDecoderConfig)},push:e=>{this.pendingChunks.push({type:"audio",init:e})},flush:()=>{this.audioDecoder?.flush()},destroy:()=>{this.audioDecoderConfig=void 0,this.audioDecoder?.close(),this.audioDecoder=void 0}};video={init:e=>{this.video.destroy(),this.videoDecoderConfig={...e},this.videoDecoder=new VideoDecoder({output:async i=>{const t=i.timestamp+this.baseTime*1e3,o=await createImageBitmap(i);i.close(),o.width>0&&o.height>0?this.on.video.decode&&this.on.video.decode({timestamp:t,bitmap:o}):o.close()},error:i=>{this.on.video.error&&this.on.video.error(i)}}),this.videoDecoder.configure(this.videoDecoderConfig)},push:e=>{this.pendingChunks.push({type:"video",init:e})},flush:()=>{this.videoDecoder?.flush()},destroy:()=>{this.videoDecoderConfig=void 0,this.videoDecoder?.close(),this.videoDecoder=void 0,this.hasKeyFrame=!1}}}const d=new c;d.on.audio.decode=s=>postMessage({type:"audio",action:"onDecode",data:s}),d.on.audio.error=s=>postMessage({type:"audio",action:"onError",data:s}),d.on.video.decode=s=>postMessage({type:"video",action:"onDecode",data:s}),d.on.video.error=s=>postMessage({type:"video",action:"onError",data:s}),onmessage=s=>{const{type:e,action:i,data:t}=s.data;if(e){const o=d[e][i];o&&o(t)}else{const o=d[i];o&&o(t)}}})();
50
- `, v = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", S], { type: "text/javascript;charset=utf-8" });
51
- function V(l) {
49
+ const S = `(function(){"use strict";class a{pattern="flv";audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;baseTime=0;pendingChunks=[];currentChunk;isProcessing=!1;decodeTimer=0;frameTrack=!1;isFrameTrack=!1;fameTrackOption={flv:[20,50],hls:[200,300],dash:[50,100],rtmp:[50,100]};decodingSpeed=16;fps=0;firstVideoChunkTimestamp;secondVideoChunkTimestamp;decodingSpeedRatio=1;maxDecodingSpeedRatio=2;frameStartTime;lastRenderTime;nextRenderTime;on={audio:{},video:{}};constructor(){}init=e=>{this.destroy(),this.pattern=e,this.baseTime=new Date().getTime()-performance.now(),this.initDecodeInterval()};initAudio=e=>{this.audio.destroy(),this.audioDecoderConfig={...e},this.audioDecoder=new AudioDecoder({output:i=>{const t=this.decodingSpeedRatio;this.on.audio.decode&&this.on.audio.decode({audioData:i,playbackRate:t})},error:i=>{this.on.audio.error&&this.on.audio.error(i)}}),this.audioDecoder.configure(this.audioDecoderConfig)};initVideo=e=>{this.video.destroy(),this.videoDecoderConfig={...e},this.videoDecoder=new VideoDecoder({output:async i=>{this.frameStartTime||(this.frameStartTime=i.timestamp);const t=i.timestamp-this.frameStartTime+this.baseTime*1e3,s=await createImageBitmap(i);i.close(),s.width>0&&s.height>0?(this.on.video.decode&&this.on.video.decode({timestamp:t,bitmap:s}),this.currentChunk&&this.currentChunk.kind==="video"&&this.currentChunk.nalus&&this.on.nalus&&this.on.nalus(this.currentChunk.nalus)):s.close()},error:i=>{this.on.video.error&&this.on.video.error(i)}}),this.videoDecoder.configure(this.videoDecoderConfig)};setFrameTrack=e=>{this.frameTrack=e,this.frameTrack===!1&&(this.decodingSpeedRatio=1)};push=e=>{this.pendingChunks.push(e)};destroy=()=>{this.audio.destroy(),this.video.destroy(),clearInterval(this.decodeTimer)};initDecodeInterval=()=>{let e=this.decodingSpeed/this.decodingSpeedRatio;const i=this.baseTime+performance.now();if(this.lastRenderTime||(this.lastRenderTime=i),this.fps=Math.round(1e3/(i-this.lastRenderTime)),this.lastRenderTime=i,this.nextRenderTime){const t=this.lastRenderTime-this.nextRenderTime;e-=t}this.nextRenderTime=this.lastRenderTime+e,this.decodeTimer=setTimeout(()=>{this.decode(),this.initDecodeInterval()},e)};decode=()=>{if(this.isProcessing!==!0){for(this.isProcessing=!0;;){this.currentChunk=this.pendingChunks.shift();const e=this.pendingChunks.length;if(this.frameTrack){const[s,n]=this.fameTrackOption[this.pattern];if(e<=s&&(this.isFrameTrack=!1),e>=n&&(this.isFrameTrack=!0),this.isFrameTrack){const r=Math.min(1+(e-s)/100,this.maxDecodingSpeedRatio);this.decodingSpeedRatio=Number(r.toFixed(1))}else this.decodingSpeedRatio=1}if(this.on.debug){const{decodingSpeed:s,decodingSpeedRatio:n,fps:r}=this;this.on.debug({decodingSpeed:s,decodingSpeedRatio:n,fps:r,cacheLength:e})}if(!this.currentChunk)break;const{kind:i,init:t}=this.currentChunk;switch(i){case"audio":this.decodeAudio(t);break;case"video":this.decodeVideo(t);break}if(i==="video")break}this.isProcessing=!1}};decodeAudio=e=>{if(!this.audioDecoder)return;const i=new EncodedAudioChunk(e);this.audioDecoder.decode(i)};decodeVideo=e=>{if(this.videoDecoder&&(e.type==="key"&&(this.hasKeyFrame=!0),this.firstVideoChunkTimestamp?this.secondVideoChunkTimestamp||(this.secondVideoChunkTimestamp=e.timestamp,this.decodingSpeed=(this.secondVideoChunkTimestamp-this.firstVideoChunkTimestamp)/1e3):this.firstVideoChunkTimestamp=e.timestamp,this.hasKeyFrame)){const i=new EncodedVideoChunk(e);this.videoDecoder.decode(i)}};audio={flush:()=>{this.audioDecoder?.flush()},destroy:()=>{this.audioDecoderConfig=void 0,this.audioDecoder?.close(),this.audioDecoder=void 0}};video={flush:()=>{this.videoDecoder?.flush()},destroy:()=>{this.videoDecoderConfig=void 0,this.videoDecoder?.close(),this.videoDecoder=void 0,this.hasKeyFrame=!1}}}const d=new a;d.on.audio.decode=o=>postMessage({action:"on.audio.decode",data:o}),d.on.audio.error=o=>postMessage({action:"on.audio.error",data:o}),d.on.video.decode=o=>postMessage({action:"on.video.decode",data:o}),d.on.video.error=o=>postMessage({action:"on.video.error",data:o}),d.on.nalus=o=>postMessage({action:"on.nalus",data:o}),d.on.debug=o=>postMessage({action:"on.debug",data:o}),onmessage=o=>{const{action:e,data:i}=o.data,t=d[e];t&&t(i)}})();
50
+ `, C = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", S], { type: "text/javascript;charset=utf-8" });
51
+ function F(l) {
52
52
  let t;
53
53
  try {
54
- if (t = v && (self.URL || self.webkitURL).createObjectURL(v), !t) throw "";
54
+ if (t = C && (self.URL || self.webkitURL).createObjectURL(C), !t) throw "";
55
55
  const s = new Worker(t, {
56
56
  name: l?.name
57
57
  });
@@ -68,45 +68,44 @@ function V(l) {
68
68
  }
69
69
  }
70
70
  class N {
71
- worker = new V();
71
+ worker = new F();
72
72
  on = { audio: {}, video: {} };
73
73
  constructor() {
74
74
  this.worker.onmessage = (t) => {
75
- const { type: s, action: e, data: i } = t.data;
75
+ const { action: s, data: e } = t.data;
76
76
  switch (s) {
77
- case "audio":
78
- e === "onDecode" && this.on.audio.decode && this.on.audio.decode(i), e === "onError" && this.on.audio.error && this.on.audio.error(i);
77
+ case "on.audio.decode":
78
+ this.on.audio.decode && this.on.audio.decode(e);
79
79
  break;
80
- case "video":
81
- e === "onDecode" && this.on.video.decode && this.on.video.decode(i), e === "onError" && this.on.video.error && this.on.video.error(i);
80
+ case "on.audio.error":
81
+ this.on.audio.error && this.on.audio.error(e);
82
+ break;
83
+ case "on.video.decode":
84
+ this.on.video.decode && this.on.video.decode(e);
85
+ break;
86
+ case "on.video.error":
87
+ this.on.video.error && this.on.video.error(e);
88
+ break;
89
+ case "on.nalus":
90
+ this.on.nalus && this.on.nalus(e);
91
+ break;
92
+ case "on.debug":
93
+ this.on.debug && this.on.debug(e);
82
94
  break;
83
95
  }
84
96
  };
85
97
  }
86
98
  init = (t) => this.worker.postMessage({ action: "init", data: t });
99
+ initAudio = (t) => this.worker.postMessage({ action: "initAudio", data: t });
100
+ initVideo = (t) => this.worker.postMessage({ action: "initVideo", data: t });
101
+ push = (t) => this.worker.postMessage({ action: "push", data: t });
87
102
  setFrameTrack = (t) => this.worker.postMessage({ action: "setFrameTrack", data: t });
88
- audio = {
89
- init: (t) => this.worker.postMessage({ type: "audio", action: "init", data: t }),
90
- push: (t) => this.worker.postMessage({ type: "audio", action: "push", data: t }),
91
- flush: () => this.worker.postMessage({ type: "audio", action: "flush" }),
92
- destroy: () => {
93
- this.worker.postMessage({ type: "audio", action: "destroy" });
94
- }
95
- };
96
- video = {
97
- init: (t) => this.worker.postMessage({ type: "video", action: "init", data: t }),
98
- push: (t) => this.worker.postMessage({ type: "video", action: "push", data: t }),
99
- flush: () => this.worker.postMessage({ type: "video", action: "flush" }),
100
- destroy: () => {
101
- this.worker.postMessage({ type: "video", action: "destroy", data: {} });
102
- }
103
- };
104
103
  destroy = () => {
105
104
  this.worker.postMessage({ action: "destroy" }), this.worker.terminate();
106
105
  };
107
106
  }
108
- var B = Object.defineProperty, E = (l, t, s) => t in l ? B(l, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : l[t] = s, y = (l, t, s) => E(l, typeof t != "symbol" ? t + "" : t, s);
109
- class G {
107
+ var B = Object.defineProperty, G = (l, t, s) => t in l ? B(l, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : l[t] = s, y = (l, t, s) => G(l, typeof t != "symbol" ? t + "" : t, s);
108
+ class O {
110
109
  constructor(t, s) {
111
110
  y(this, "inputStream", new MediaStream()), y(this, "outputStream", new MediaStream()), y(this, "inputGain", 1), y(this, "enhanceGain", 1), y(this, "bgsGain", 1), y(this, "bgmGain", 1), y(this, "outputGain", 1), y(this, "mixAudioMap", /* @__PURE__ */ new Map()), y(this, "audioContext", new AudioContext()), y(this, "sourceNode"), y(this, "inputGainNode"), y(this, "enhanceGainNode"), y(this, "bgsGainNode"), y(this, "bgmGainNode"), y(this, "analyserNode"), y(this, "analyserArrayData"), y(this, "outputGainNode"), y(this, "destinationNode"), y(this, "filterStream", (e) => e), y(this, "stop", () => {
112
111
  {
@@ -130,21 +129,21 @@ class G {
130
129
  const { analyserNode: e, analyserArrayData: i } = this;
131
130
  e.getByteFrequencyData(i);
132
131
  let n = 0;
133
- for (let r = 0; r < i.length; r++)
134
- n += i[r];
132
+ for (let o = 0; o < i.length; o++)
133
+ n += i[o];
135
134
  return Math.ceil(n / i.length);
136
- }), y(this, "mixAudio", (e, i = "bgm") => new Promise(async (n, r) => {
135
+ }), y(this, "mixAudio", (e, i = "bgm") => new Promise(async (n, o) => {
137
136
  try {
138
137
  {
139
138
  const d = this.mixAudioMap.get(i);
140
139
  d && d.stop();
141
140
  }
142
- const a = i === "bgs" ? this.bgsGainNode : this.bgmGainNode, o = this.audioContext.createBufferSource();
143
- this.mixAudioMap.set(i, o), o.buffer = e, o.connect(a), o.onended = () => {
144
- o.disconnect(a), this.mixAudioMap.delete(i), n(!0);
145
- }, o.start(0);
141
+ const a = i === "bgs" ? this.bgsGainNode : this.bgmGainNode, r = this.audioContext.createBufferSource();
142
+ this.mixAudioMap.set(i, r), r.buffer = e, r.connect(a), r.onended = () => {
143
+ r.disconnect(a), this.mixAudioMap.delete(i), n(!0);
144
+ }, r.start(0);
146
145
  } catch (a) {
147
- r(a);
146
+ o(a);
148
147
  }
149
148
  })), y(this, "mixAudioStop", (e) => {
150
149
  const i = this.mixAudioMap.get(e);
@@ -154,19 +153,19 @@ class G {
154
153
  i ? n.connect(this.destinationNode) : n.disconnect(this.destinationNode);
155
154
  }), s && (this.audioContext = s), this.inputStream = t, this.sourceNode = this.audioContext.createMediaStreamSource(this.inputStream), this.inputGainNode = this.audioContext.createGain(), this.inputGainNode.gain.setValueAtTime(this.inputGain, this.audioContext.currentTime), this.enhanceGainNode = this.audioContext.createGain(), this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain, this.audioContext.currentTime), this.bgsGainNode = this.audioContext.createGain(), this.bgsGainNode.gain.setValueAtTime(this.bgsGain, this.audioContext.currentTime), this.bgmGainNode = this.audioContext.createGain(), this.bgmGainNode.gain.setValueAtTime(this.bgmGain, this.audioContext.currentTime), this.analyserNode = this.audioContext.createAnalyser(), this.analyserNode.fftSize = 512, this.analyserArrayData = new Uint8Array(this.analyserNode.frequencyBinCount), this.outputGainNode = this.audioContext.createGain(), this.outputGainNode.gain.setValueAtTime(this.outputGain, this.audioContext.currentTime), this.destinationNode = this.audioContext.createMediaStreamDestination(), this.outputStream = this.destinationNode.stream;
156
155
  {
157
- const { sourceNode: e, inputGainNode: i, enhanceGainNode: n, bgsGainNode: r, bgmGainNode: a, analyserNode: o, outputGainNode: d, destinationNode: c } = this;
158
- e.connect(i), i.connect(n), n.connect(o), r.connect(o), a.connect(o), n.connect(c), r.connect(c), a.connect(c), o.connect(d), d.connect(this.audioContext.destination);
156
+ const { sourceNode: e, inputGainNode: i, enhanceGainNode: n, bgsGainNode: o, bgmGainNode: a, analyserNode: r, outputGainNode: d, destinationNode: c } = this;
157
+ e.connect(i), i.connect(n), n.connect(r), o.connect(r), a.connect(r), n.connect(c), o.connect(c), a.connect(c), r.connect(d), d.connect(this.audioContext.destination);
159
158
  }
160
159
  this.setMute(!0), this.audioContext.resume();
161
160
  }
162
161
  }
163
- const O = async (l, t) => {
162
+ const E = async (l, t) => {
164
163
  try {
165
- const { format: s, numberOfChannels: e, numberOfFrames: i, sampleRate: n } = t, r = l.createBuffer(e, i, n);
164
+ const { format: s, numberOfChannels: e, numberOfFrames: i, sampleRate: n } = t, o = l.createBuffer(e, i, n);
166
165
  for (let a = 0; a < e; a++) {
167
- const o = t.allocationSize({ planeIndex: a }), d = new Uint8Array(o);
166
+ const r = t.allocationSize({ planeIndex: a }), d = new Uint8Array(r);
168
167
  t.copyTo(d, { planeIndex: a });
169
- const c = new DataView(d.buffer), h = r.getChannelData(a);
168
+ const c = new DataView(d.buffer), h = o.getChannelData(a);
170
169
  for (let p = 0; p < i; p++) {
171
170
  let u;
172
171
  switch (s) {
@@ -191,7 +190,7 @@ const O = async (l, t) => {
191
190
  h[p] = Math.max(-1, Math.min(1, u));
192
191
  }
193
192
  }
194
- return r;
193
+ return o;
195
194
  } catch (s) {
196
195
  throw console.error("Failed to convert AudioData to AudioBuffer:", s), s;
197
196
  }
@@ -206,20 +205,20 @@ class W {
206
205
  constructor() {
207
206
  }
208
207
  init = (t) => {
209
- t || (t = new (window.AudioContext || window.webkitAudioContext)()), this.audioContext = t, this.destination = this.audioContext.createMediaStreamDestination(), this.stream = new MediaStream(), this.stream.addTrack(this.destination.stream.getAudioTracks()[0]), this.prAudioStream = new G(this.stream, this.audioContext), this.nextStartTime = 0, this.pendingSources = [];
208
+ t || (t = new (window.AudioContext || window.webkitAudioContext)()), this.audioContext = t, this.destination = this.audioContext.createMediaStreamDestination(), this.stream = new MediaStream(), this.stream.addTrack(this.destination.stream.getAudioTracks()[0]), this.prAudioStream = new O(this.stream, this.audioContext), this.nextStartTime = 0, this.pendingSources = [];
210
209
  };
211
210
  async push(t) {
212
211
  const { audioData: s, playbackRate: e = 1 } = t;
213
212
  try {
214
213
  if (!this.audioContext || !this.destination) return;
215
- let i = await O(this.audioContext, s);
214
+ let i = await E(this.audioContext, s);
216
215
  if (!i) return;
217
216
  const n = this.audioContext.createBufferSource();
218
217
  n.buffer = i, n.playbackRate.value = e;
219
- const r = -1200 * Math.log2(e);
220
- n.detune.value = r, n.connect(this.destination);
221
- const a = Math.max(this.nextStartTime, this.audioContext.currentTime), o = i.duration / e;
222
- this.nextStartTime = a + o, n.start(a), this.pendingSources.push(n), n.onended = () => {
218
+ const o = -1200 * Math.log2(e);
219
+ n.detune.value = o, n.connect(this.destination);
220
+ const a = Math.max(this.nextStartTime, this.audioContext.currentTime), r = i.duration / e;
221
+ this.nextStartTime = a + r, n.start(a), this.pendingSources.push(n), n.onended = () => {
223
222
  this.pendingSources = this.pendingSources.filter((d) => d !== n);
224
223
  }, this.audioContext.state === "suspended" && await this.audioContext.resume();
225
224
  } finally {
@@ -232,11 +231,11 @@ class W {
232
231
  }
233
232
  }
234
233
  const M = `(function(){"use strict";class o{writable;writer;cutOption;pause=!1;constructor(){}init=({writable:t})=>{this.destroy(),this.writable=t,this.writer=this.writable.getWriter()};push=async t=>{if(this.pause)return;const{timestamp:s}=t;let{bitmap:i}=t;if(this.cutOption){const{sx:n=0,sy:c=0,sw:h=i.width,sh:u=i.height}=this.cutOption;i=await createImageBitmap(i,n,c,h,u)}const r=new VideoFrame(i,{timestamp:s});this.cutOption&&i.close(),this.writer.write(r),r.close()};setCut=t=>{this.cutOption=t};setPause=t=>{this.pause=t};destroy=()=>{this.writable=void 0,this.writer=void 0,this.cutOption=void 0}}const a=new o;onmessage=e=>{const{action:t,data:s}=e.data,i=a[t];i&&i(s)}})();
235
- `, x = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", M], { type: "text/javascript;charset=utf-8" });
234
+ `, A = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", M], { type: "text/javascript;charset=utf-8" });
236
235
  function j(l) {
237
236
  let t;
238
237
  try {
239
- if (t = x && (self.URL || self.webkitURL).createObjectURL(x), !t) throw "";
238
+ if (t = A && (self.URL || self.webkitURL).createObjectURL(A), !t) throw "";
240
239
  const s = new Worker(t, {
241
240
  name: l?.name
242
241
  });
@@ -271,7 +270,7 @@ const q = (l) => {
271
270
  const t = l?.getTracks() || [];
272
271
  for (const s of t)
273
272
  s.stop();
274
- }, A = () => {
273
+ }, x = () => {
275
274
  const l = new MediaStreamTrackGenerator({ kind: "video" }), t = new MediaStream([l]), s = new z();
276
275
  return s.init({ writable: l.writable }), { worker: s, stream: t, destroy: () => {
277
276
  s.destroy(), R(t);
@@ -297,10 +296,10 @@ class $ {
297
296
  this.resolvesMap.get(t)?.set(a, { resolve: i, timer: 0 });
298
297
  return;
299
298
  }
300
- const o = window.setTimeout(() => {
299
+ const r = window.setTimeout(() => {
301
300
  this.emit(t);
302
301
  }, e);
303
- this.resolvesMap.get(t)?.set(a, { resolve: i, timer: o });
302
+ this.resolvesMap.get(t)?.set(a, { resolve: i, timer: r });
304
303
  });
305
304
  emit = async (t) => {
306
305
  const s = this.resolvesMap.get(t);
@@ -324,9 +323,9 @@ const H = (l, t) => {
324
323
  t = t + 1;
325
324
  const n = s.getUint8(t) & 255;
326
325
  t = t + 1;
327
- const r = s.getUint8(t) & 255;
326
+ const o = s.getUint8(t) & 255;
328
327
  t = t + 1;
329
- const d = `avc1.${Array.from([i, n, r], (b) => b.toString(16).padStart(2, "0")).join("")}`, c = (s.getUint8(t) & 3) - 1;
328
+ const d = `avc1.${Array.from([i, n, o], (b) => b.toString(16).padStart(2, "0")).join("")}`, c = (s.getUint8(t) & 3) - 1;
330
329
  t = t + 1;
331
330
  const h = s.getUint8(t) & 31;
332
331
  t = t + 1;
@@ -339,7 +338,7 @@ const H = (l, t) => {
339
338
  const f = s.getUint16(t, !1);
340
339
  t = t + 2;
341
340
  const m = new Uint8Array(s.buffer.slice(t, t + f));
342
- return t = t + f, { version: e, codec: d, profile: i, compatibility: n, level: r, lengthSizeMinusOne: c, numOfSequenceParameterSets: h, sequenceParameterSetLength: p, sps: u, numOfPictureParameterSets: g, pictureParameterSetLength: f, pps: m };
341
+ return t = t + f, { version: e, codec: d, profile: i, compatibility: n, level: o, lengthSizeMinusOne: c, numOfSequenceParameterSets: h, sequenceParameterSetLength: p, sps: u, numOfPictureParameterSets: g, pictureParameterSetLength: f, pps: m };
343
342
  }, K = (l) => {
344
343
  const t = new Uint8Array(4 + l.length);
345
344
  return new DataView(t.buffer).setUint32(0, l.length, !1), t.set(l, 4), t;
@@ -359,16 +358,16 @@ const H = (l, t) => {
359
358
  let s = 0, e, i, n;
360
359
  e = t.getUint32(s, !1), s += 4;
361
360
  {
362
- const r = t.getUint8(s), a = r >> 7 & 1, o = r >> 5 & 3, d = r & 31;
363
- i = { forbidden_zero_bit: a, nal_ref_idc: o, nal_unit_type: d }, s += 1;
361
+ const o = t.getUint8(s), a = o >> 7 & 1, r = o >> 5 & 3, d = o & 31;
362
+ i = { forbidden_zero_bit: a, nal_ref_idc: r, nal_unit_type: d }, s += 1;
364
363
  }
365
364
  {
366
- const r = e - 1;
367
- n = new Uint8Array(t.buffer.slice(s, s + r));
365
+ const o = e - 1;
366
+ n = new Uint8Array(t.buffer.slice(s, s + o));
368
367
  }
369
368
  return { size: e, header: i, data: n };
370
369
  };
371
- class C {
370
+ class v {
372
371
  #e = {
373
372
  timeout: 5 * 1e3,
374
373
  check: !1
@@ -388,10 +387,10 @@ class C {
388
387
  this.#t?.abort("Timeout."), i({ status: "timeout", reason: "" });
389
388
  }, this.#e.timeout);
390
389
  try {
391
- const r = await fetch(t, { ...s, method: "HEAD", signal: this.#t?.signal });
392
- r.status === 200 ? e({ status: "successed", reason: "" }) : i({ status: "failed", reason: `${r.status}` });
393
- } catch (r) {
394
- i({ status: "error", reason: r.message });
390
+ const o = await fetch(t, { ...s, method: "HEAD", signal: this.#t?.signal });
391
+ o.status === 200 ? e({ status: "successed", reason: "" }) : i({ status: "failed", reason: `${o.status}` });
392
+ } catch (o) {
393
+ i({ status: "error", reason: o.message });
395
394
  }
396
395
  clearTimeout(n);
397
396
  });
@@ -421,7 +420,7 @@ class et {
421
420
  debug: !1,
422
421
  frameTrack: !1
423
422
  };
424
- prFetch = new C();
423
+ prFetch = new v();
425
424
  prResolves = new $();
426
425
  url = "";
427
426
  demuxerWorker;
@@ -505,7 +504,7 @@ class et {
505
504
  */
506
505
  create: (t, s) => {
507
506
  let e = this.cutRenders.get(t);
508
- return e ? (e.worker.setCut(s), e.worker.setPause(!1), e) : (e = A(), e.worker.setCut(s), this.cutRenders.set(t, e), e);
507
+ return e ? (e.worker.setCut(s), e.worker.setPause(!1), e) : (e = x(), e.worker.setCut(s), this.cutRenders.set(t, e), e);
509
508
  },
510
509
  /**
511
510
  * 获取媒体流
@@ -535,7 +534,7 @@ class et {
535
534
  * 初始化解复器
536
535
  */
537
536
  initDemuxer = (t) => {
538
- this.demuxerWorker = new F(), this.demuxerWorker.init(t), this.demuxerWorker.on.debug = (s) => {
537
+ this.demuxerWorker = new V(), this.demuxerWorker.init(t), this.demuxerWorker.on.debug = (s) => {
539
538
  this.option.debug && console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: debug", s);
540
539
  }, this.demuxerWorker.on.info = (s) => {
541
540
  this.option.debug && console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: info", s), this.on.demuxer.info && this.on.demuxer.info(s);
@@ -545,14 +544,14 @@ class et {
545
544
  switch (e) {
546
545
  case "audio":
547
546
  {
548
- const { codec: i, sampleRate: n, numberOfChannels: r } = s;
549
- this.decoderWorker?.audio.init({ codec: i, sampleRate: n, numberOfChannels: r });
547
+ const { codec: i, sampleRate: n, numberOfChannels: o } = s;
548
+ this.decoderWorker?.initAudio({ codec: i, sampleRate: n, numberOfChannels: o });
550
549
  }
551
550
  break;
552
551
  case "video":
553
552
  {
554
553
  const { codec: i, description: n } = s;
555
- this.decoderWorker?.video.init({ codec: i, description: n });
554
+ this.decoderWorker?.initVideo({ codec: i, description: n });
556
555
  }
557
556
  break;
558
557
  }
@@ -562,19 +561,14 @@ class et {
562
561
  switch (e) {
563
562
  case "audio":
564
563
  {
565
- const { type: i, dts: n, data: r } = s, a = n * 1;
566
- this.decoderWorker.audio.push({ type: i, timestamp: a, data: r });
564
+ const { type: i, dts: n, data: o } = s, a = n * 1e3;
565
+ this.decoderWorker.push({ kind: e, init: { type: i, timestamp: a, data: o } });
567
566
  }
568
567
  break;
569
568
  case "video":
570
569
  {
571
- const { type: i, dts: n, data: r, nalus: a = [] } = s, o = n * 1e3;
572
- this.decoderWorker.video.push({ type: i, timestamp: o, data: r });
573
- for (const d of a) {
574
- if (d.byteLength <= 4) continue;
575
- const { header: c, data: h } = Y(d), { nal_unit_type: p } = c;
576
- p === 6 && this.on.demuxer.sei && this.on.demuxer.sei(h);
577
- }
570
+ const { type: i, dts: n, data: o, nalus: a = [] } = s, r = n * 1e3;
571
+ this.decoderWorker.push({ kind: e, init: { type: i, timestamp: r, data: o }, nalus: a });
578
572
  }
579
573
  break;
580
574
  }
@@ -598,13 +592,19 @@ class et {
598
592
  this.on.decoder.video && this.on.decoder.video(s), s.bitmap.close();
599
593
  }, this.decoderWorker.on.video.error = (s) => {
600
594
  this.stop(), this.on.error && this.on.error(s);
595
+ }, this.decoderWorker.on.nalus = async (s) => {
596
+ for (const e of s) {
597
+ if (e.byteLength <= 4) continue;
598
+ const { header: i, data: n } = Y(e), { nal_unit_type: o } = i;
599
+ o === 6 && this.on.decoder.sei && this.on.decoder.sei(n);
600
+ }
601
601
  };
602
602
  };
603
603
  /**
604
604
  * 初始化渲染器
605
605
  */
606
606
  initRender = () => {
607
- const { worker: t, stream: s } = A();
607
+ const { worker: t, stream: s } = x();
608
608
  this.renderWorker = t, this.stream = s, this.renderWorker.setPause(!1);
609
609
  };
610
610
  flv = {
@@ -632,19 +632,19 @@ class et {
632
632
  parse: async (t) => {
633
633
  const i = new TextDecoder("utf-8").decode(t).split(`
634
634
  `).map((c) => c.replace("\r", "")), n = this.url.substring(0, this.url.lastIndexOf("/") + 1);
635
- let r = 4, a = 0, o = !1;
635
+ let o = 4, a = 0, r = !1;
636
636
  const d = [];
637
637
  for (const c of i)
638
- c.startsWith("#EXTINF:") ? r = parseFloat(c.split(":")[1].split(",")[0]) : c.startsWith("#EXT-X-TARGETDURATION:") ? a = parseInt(c.split(":")[1]) : c.startsWith("#EXT-X-ENDLIST") ? o = !1 : c.startsWith("#EXT-X-MEDIA-SEQUENCE:") ? o = !0 : c.includes(".ts") && !c.startsWith("#") && d.push({
638
+ c.startsWith("#EXTINF:") ? o = parseFloat(c.split(":")[1].split(",")[0]) : c.startsWith("#EXT-X-TARGETDURATION:") ? a = parseInt(c.split(":")[1]) : c.startsWith("#EXT-X-ENDLIST") ? r = !1 : c.startsWith("#EXT-X-MEDIA-SEQUENCE:") ? r = !0 : c.includes(".ts") && !c.startsWith("#") && d.push({
639
639
  url: c.startsWith("http") ? c : n + c,
640
- duration: r,
641
- isLive: o
640
+ duration: o,
641
+ isLive: r
642
642
  });
643
- return { baseUrl: n, targetDuration: a, isLive: o, segments: d };
643
+ return { baseUrl: n, targetDuration: a, isLive: r, segments: d };
644
644
  },
645
645
  getSegments: async () => {
646
646
  try {
647
- const t = new C();
647
+ const t = new v();
648
648
  let s = await t.request(this.url);
649
649
  if (s.status !== 200 && (await new Promise((i) => setTimeout(() => i(!0), 500)), s = await t.request(this.url)), s.status !== 200 && (await new Promise((i) => setTimeout(() => i(!0), 500)), s = await t.request(this.url)), s.status !== 200) throw new Error("request is error.");
650
650
  const e = s.body?.getReader();
@@ -652,8 +652,8 @@ class et {
652
652
  for (; ; ) {
653
653
  const { done: i, value: n } = await e.read();
654
654
  if (n) {
655
- const r = await this.hls.parse(n), { segments: a = [], isLive: o = !1 } = r;
656
- this.hls.isLive = o, o === !1 && (this.option.frameTrack = !1);
655
+ const o = await this.hls.parse(n), { segments: a = [], isLive: r = !1 } = o;
656
+ this.hls.isLive = r, r === !1 && (this.option.frameTrack = !1);
657
657
  let d = Array.from(a, (h) => h.url);
658
658
  const c = d.findIndex((h) => h === this.hls.url);
659
659
  c !== -1 && (d = d.slice(c + 1)), this.hls.urls = d;
@@ -760,24 +760,24 @@ class Z {
760
760
  let e = s;
761
761
  const i = this.parseHeader(t, e);
762
762
  e += 4;
763
- const { transport_error_indicator: n, pid: r, payload_unit_start_indicator: a, adaptation_field_control: o } = i;
764
- if (n === 1 || r === void 0) return;
763
+ const { transport_error_indicator: n, pid: o, payload_unit_start_indicator: a, adaptation_field_control: r } = i;
764
+ if (n === 1 || o === void 0) return;
765
765
  let d = 184;
766
- if (o === 2 || o === 3) {
766
+ if (r === 2 || r === 3) {
767
767
  const c = t.getUint8(e);
768
768
  e += 1, this.parseAdaptationField(t, e), e += c, d -= c;
769
769
  }
770
- if (o === 3 && (d -= 1), o === 1 || o === 3) {
770
+ if (r === 3 && (d -= 1), r === 1 || r === 3) {
771
771
  const c = new Uint8Array(t.buffer.slice(e, e + d));
772
- if (r === 0) return this.parsePAT(t, e);
772
+ if (o === 0) return this.parsePAT(t, e);
773
773
  {
774
774
  const { programs: u = [] } = this.pat || {};
775
- if (u.find((f) => f.pmt_pid === r)) return this.parsePMT(t, e);
775
+ if (u.find((f) => f.pmt_pid === o)) return this.parsePMT(t, e);
776
776
  }
777
- const { streams: h = [] } = this.pmt || {}, p = h.find((u) => u.elementary_pid === r);
777
+ const { streams: h = [] } = this.pmt || {}, p = h.find((u) => u.elementary_pid === o);
778
778
  if (p) {
779
779
  if (a === 1) {
780
- const u = this.payloadMap.get(r);
780
+ const u = this.payloadMap.get(o);
781
781
  if (u) {
782
782
  switch (p.kind) {
783
783
  case "audio":
@@ -793,13 +793,13 @@ class Z {
793
793
  }
794
794
  break;
795
795
  }
796
- this.payloadMap.delete(r);
796
+ this.payloadMap.delete(o);
797
797
  }
798
798
  }
799
799
  {
800
- this.payloadMap.has(r) || this.payloadMap.set(r, new Uint8Array());
801
- const u = this.payloadMap.get(r), g = new Uint8Array(u.byteLength + c.byteLength);
802
- g.set(u, 0), g.set(c, u.byteLength), this.payloadMap.set(r, g);
800
+ this.payloadMap.has(o) || this.payloadMap.set(o, new Uint8Array());
801
+ const u = this.payloadMap.get(o), g = new Uint8Array(u.byteLength + c.byteLength);
802
+ g.set(u, 0), g.set(c, u.byteLength), this.payloadMap.set(o, g);
803
803
  }
804
804
  }
805
805
  }
@@ -807,10 +807,10 @@ class Z {
807
807
  // Header
808
808
  parseHeader = (t, s) => {
809
809
  let e = s;
810
- const i = t.getUint8(e), n = t.getUint8(e + 1), r = t.getUint8(e + 2), a = t.getUint8(e + 3), o = (n & 128) >> 7;
811
- if (o === 1) return { sync_byte: i, transport_error_indicator: o };
812
- const d = (n & 64) >> 6, c = (n & 32) >> 5, h = (n & 31) << 8 | r, p = (a & 192) >> 6, u = a >> 4 & 3, g = a & 15;
813
- return { sync_byte: i, transport_error_indicator: o, payload_unit_start_indicator: d, transport_priority: c, pid: h, transport_scrambling_control: p, adaptation_field_control: u, continuity_counter: g };
810
+ const i = t.getUint8(e), n = t.getUint8(e + 1), o = t.getUint8(e + 2), a = t.getUint8(e + 3), r = (n & 128) >> 7;
811
+ if (r === 1) return { sync_byte: i, transport_error_indicator: r };
812
+ const d = (n & 64) >> 6, c = (n & 32) >> 5, h = (n & 31) << 8 | o, p = (a & 192) >> 6, u = a >> 4 & 3, g = a & 15;
813
+ return { sync_byte: i, transport_error_indicator: r, payload_unit_start_indicator: d, transport_priority: c, pid: h, transport_scrambling_control: p, adaptation_field_control: u, continuity_counter: g };
814
814
  };
815
815
  // PAT表
816
816
  parsePAT = (t, s) => {
@@ -818,8 +818,8 @@ class Z {
818
818
  {
819
819
  const a = t.getUint8(e);
820
820
  e += 1;
821
- const o = t.getUint8(e);
822
- if (e += 1, o !== 0) throw new Error("Invalid PAT table_id");
821
+ const r = t.getUint8(e);
822
+ if (e += 1, r !== 0) throw new Error("Invalid PAT table_id");
823
823
  const d = t.getUint16(e) & 4095;
824
824
  e += 2;
825
825
  const c = t.getUint16(e);
@@ -829,18 +829,18 @@ class Z {
829
829
  const u = t.getUint8(e);
830
830
  e += 1;
831
831
  const g = t.getUint8(e);
832
- e += 1, i = { pointer_field: a, table_id: o, section_length: d, transport_stream_id: c, version_number: h, current_next_indicator: p, section_number: u, last_section_number: g };
832
+ e += 1, i = { pointer_field: a, table_id: r, section_length: d, transport_stream_id: c, version_number: h, current_next_indicator: p, section_number: u, last_section_number: g };
833
833
  }
834
834
  const n = [];
835
835
  {
836
- const a = i.section_length - 5 - 4, o = e + a;
837
- for (; e < o; ) {
836
+ const a = i.section_length - 5 - 4, r = e + a;
837
+ for (; e < r; ) {
838
838
  const d = t.getUint16(e), c = t.getUint16(e + 2) & 8191;
839
839
  e += 4, d !== 0 && c >= 32 && c <= 8190 && n.push({ program_number: d, pmt_pid: c });
840
840
  }
841
841
  }
842
- const r = t.getUint32(e);
843
- this.pat = { header: i, programs: n, crc32: r }, this.on.debug && this.on.debug({ pat: this.pat });
842
+ const o = t.getUint32(e);
843
+ this.pat = { header: i, programs: n, crc32: o }, this.on.debug && this.on.debug({ pat: this.pat });
844
844
  };
845
845
  // PMT表
846
846
  parsePMT = (t, s) => {
@@ -848,8 +848,8 @@ class Z {
848
848
  {
849
849
  const a = t.getUint8(e);
850
850
  e += 1;
851
- const o = t.getUint8(e);
852
- if (e += 1, o !== 2) throw new Error("Invalid PMT table_id");
851
+ const r = t.getUint8(e);
852
+ if (e += 1, r !== 2) throw new Error("Invalid PMT table_id");
853
853
  const d = t.getUint16(e) & 4095;
854
854
  e += 2;
855
855
  const c = t.getUint16(e);
@@ -863,12 +863,12 @@ class Z {
863
863
  const f = t.getUint16(e) & 8191;
864
864
  e += 2;
865
865
  const m = t.getUint16(e) & 4095;
866
- e += 2, i = { pointer_field: a, table_id: o, section_length: d, transport_stream_id: c, version_number: h, current_next_indicator: p, section_number: u, last_section_number: g, pcr_pid: f, program_info_length: m };
866
+ e += 2, i = { pointer_field: a, table_id: r, section_length: d, transport_stream_id: c, version_number: h, current_next_indicator: p, section_number: u, last_section_number: g, pcr_pid: f, program_info_length: m };
867
867
  }
868
868
  const n = [];
869
869
  {
870
- const a = i.section_length - 9 - 4, o = e + a;
871
- for (; e < o; ) {
870
+ const a = i.section_length - 9 - 4, r = e + a;
871
+ for (; e < r; ) {
872
872
  const d = t.getUint8(e), c = J(d), h = t.getUint16(e + 1) & 8191, p = t.getUint16(e + 3) & 4095;
873
873
  if (e += 5, h < 32 || h > 8190) {
874
874
  console.warn(`Invalid elementary_pid: 0x${h.toString(16)}`);
@@ -877,13 +877,13 @@ class Z {
877
877
  n.push({ kind: c, stream_type: d, elementary_pid: h, es_info_length: p });
878
878
  }
879
879
  }
880
- const r = t.getUint32(e);
881
- this.pmt = { header: i, streams: n, crc32: r }, this.on.debug && this.on.debug({ pmt: this.pmt });
880
+ const o = t.getUint32(e);
881
+ this.pmt = { header: i, streams: n, crc32: o }, this.on.debug && this.on.debug({ pmt: this.pmt });
882
882
  };
883
883
  // AdaptationField
884
884
  parseAdaptationField = (t, s) => {
885
- let e = s, i, n, r, a;
886
- const o = t.getUint8(e), d = !!(o & 128), c = !!(o & 64), h = !!(o & 32), p = !!(o & 16), u = !!(o & 8), g = !!(o & 4), f = !!(o & 2), m = !!(o & 1);
885
+ let e = s, i, n, o, a;
886
+ const r = t.getUint8(e), d = !!(r & 128), c = !!(r & 64), h = !!(r & 32), p = !!(r & 16), u = !!(r & 8), g = !!(r & 4), f = !!(r & 2), m = !!(r & 1);
887
887
  e += 1;
888
888
  const b = (k, w) => {
889
889
  let U = BigInt(0);
@@ -891,22 +891,22 @@ class Z {
891
891
  const I = (k.getUint16(w + 4) & 1) << 8 | k.getUint16(w + 5);
892
892
  return U = U * 300n + BigInt(I), U;
893
893
  };
894
- if (p && (i = b(t, e), e += 6), u && (n = b(t, e), e += 6), g && (r = t.getInt8(e), e += 1), f) {
894
+ if (p && (i = b(t, e), e += 6), u && (n = b(t, e), e += 6), g && (o = t.getInt8(e), e += 1), f) {
895
895
  const k = t.getUint8(e);
896
896
  e += 1, a = new Uint8Array(t.buffer, e, k), e += k;
897
897
  }
898
- return { discontinuity_indicator: d, random_access_indicator: c, elementary_stream_priority_indicator: h, pcr_flag: p, opcr_flag: u, splicing_point_flag: g, transport_private_data_flag: f, adaptation_field_extension_flag: m, pcr: i, opcr: n, splice_countdown: r, transport_private_data: a };
898
+ return { discontinuity_indicator: d, random_access_indicator: c, elementary_stream_priority_indicator: h, pcr_flag: p, opcr_flag: u, splicing_point_flag: g, transport_private_data_flag: f, adaptation_field_extension_flag: m, pcr: i, opcr: n, splice_countdown: o, transport_private_data: a };
899
899
  };
900
900
  parseAudio = async (t) => {
901
901
  const s = new DataView(t.buffer);
902
902
  let e = 0, i, n;
903
903
  {
904
- const r = s.getUint8(e) === 0 && s.getUint8(e + 1) === 0 && s.getUint8(e + 2) === 1;
905
- if (e += 3, !r)
904
+ const o = s.getUint8(e) === 0 && s.getUint8(e + 1) === 0 && s.getUint8(e + 2) === 1;
905
+ if (e += 3, !o)
906
906
  throw new Error("invalid ts audio payload.");
907
907
  const a = s.getUint8(e);
908
908
  e += 1;
909
- const o = s.getUint8(e) << 8 | s.getUint8(e + 1);
909
+ const r = s.getUint8(e) << 8 | s.getUint8(e + 1);
910
910
  e += 2;
911
911
  let d, c, h, p, u;
912
912
  {
@@ -920,7 +920,7 @@ class Z {
920
920
  const b = m >> 6, k = s.getUint8(e);
921
921
  e += 1, (b & 2) === 2 && (g = this.parsePtsDts(s, e)), (b & 1) === 1 ? f = this.parsePtsDts(s, e + 5) : f = g, e += k;
922
922
  }
923
- i = { stream_id: a, pes_packet_length: o, scrambling_control: d, priority: c, data_alignment: h, copyright: p, original_copy: u, pts: g, dts: f };
923
+ i = { stream_id: a, pes_packet_length: r, scrambling_control: d, priority: c, data_alignment: h, copyright: p, original_copy: u, pts: g, dts: f };
924
924
  }
925
925
  n = t.slice(e);
926
926
  {
@@ -931,20 +931,20 @@ class Z {
931
931
  const f = `mp4a.40.${u}`, b = [96e3, 88200, 64e3, 48e3, 44100, 32e3, 24e3, 22050, 16e3, 12e3, 11025, 8e3, 7350][g];
932
932
  this.audioConfig = { kind: "audio", codec: f, sampleRate: b, numberOfChannels: u }, this.on.config && this.on.config(this.audioConfig);
933
933
  }
934
- const { dts: r = 0, pts: a = 0 } = i, o = a - r, d = n.slice(7);
935
- return { kind: "audio", type: "key", dts: r, pts: a, cts: o, data: d };
934
+ const { dts: o = 0, pts: a = 0 } = i, r = a - o, d = n.slice(7);
935
+ return { kind: "audio", type: "key", dts: o, pts: a, cts: r, data: d };
936
936
  }
937
937
  };
938
938
  parseVideo = async (t) => {
939
939
  const s = new DataView(t.buffer);
940
940
  let e = 0, i, n;
941
941
  {
942
- const r = s.getUint8(e) === 0 && s.getUint8(e + 1) === 0 && s.getUint8(e + 2) === 1;
943
- if (e += 3, !r)
942
+ const o = s.getUint8(e) === 0 && s.getUint8(e + 1) === 0 && s.getUint8(e + 2) === 1;
943
+ if (e += 3, !o)
944
944
  throw new Error("invalid ts video payload.");
945
945
  const a = s.getUint8(e);
946
946
  e += 1;
947
- const o = s.getUint8(e) << 8 | s.getUint8(e + 1);
947
+ const r = s.getUint8(e) << 8 | s.getUint8(e + 1);
948
948
  e += 2;
949
949
  let d, c, h, p, u;
950
950
  {
@@ -958,21 +958,21 @@ class Z {
958
958
  const b = m >> 6, k = s.getUint8(e);
959
959
  e += 1, (b & 2) === 2 && (g = this.parsePtsDts(s, e)), (b & 1) === 1 ? f = this.parsePtsDts(s, e + 5) : f = g, e += k;
960
960
  }
961
- i = { stream_id: a, pes_packet_length: o, scrambling_control: d, priority: c, data_alignment: h, copyright: p, original_copy: u, pts: g, dts: f };
961
+ i = { stream_id: a, pes_packet_length: r, scrambling_control: d, priority: c, data_alignment: h, copyright: p, original_copy: u, pts: g, dts: f };
962
962
  }
963
963
  n = t.slice(e);
964
964
  {
965
- const r = this.getNalus(n);
965
+ const o = this.getNalus(n);
966
966
  if (!this.videoConfig) {
967
967
  let u, g;
968
- if (u = r.find((m) => m.type === 7)?.nalu.slice(4), g = r.find((m) => m.type === 8)?.nalu.slice(4), u && g) {
968
+ if (u = o.find((m) => m.type === 7)?.nalu.slice(4), g = o.find((m) => m.type === 8)?.nalu.slice(4), u && g) {
969
969
  const f = H(u, g), { codec: m } = P(f);
970
970
  this.videoConfig = { kind: "video", codec: m, description: f, sps: u, pps: g }, this.on.config && this.on.config(this.videoConfig);
971
971
  }
972
972
  }
973
973
  const a = [];
974
- let o = "delta";
975
- for (const u of r) {
974
+ let r = "delta";
975
+ for (const u of o) {
976
976
  const { type: g, nalu: f } = u;
977
977
  switch (g) {
978
978
  case 6:
@@ -981,23 +981,23 @@ class Z {
981
981
  a.push(f);
982
982
  break;
983
983
  case 1:
984
- o = "delta", a.push(f);
984
+ r = "delta", a.push(f);
985
985
  break;
986
986
  case 5:
987
- o = "key", a.push(f);
987
+ r = "key", a.push(f);
988
988
  break;
989
989
  }
990
990
  }
991
991
  const d = X(a), { dts: c = 0, pts: h = 0 } = i, p = h - c;
992
- return { kind: "video", type: o, dts: c, pts: h, cts: p, data: d, nalus: a };
992
+ return { kind: "video", type: r, dts: c, pts: h, cts: p, data: d, nalus: a };
993
993
  }
994
994
  };
995
995
  /**
996
996
  * 解析 PTS/DTS 时间戳(33-bit,单位:90kHz)
997
997
  */
998
998
  parsePtsDts(t, s) {
999
- const e = t.getUint8(s), i = t.getUint8(s + 1), n = t.getUint8(s + 2), r = t.getUint8(s + 3), a = t.getUint8(s + 4), o = (BigInt(e) & 0b00001110n) << 29n | (BigInt(i) & 0b11111111n) << 22n | (BigInt(n) & 0b11111110n) << 14n | (BigInt(r) & 0b11111111n) << 7n | (BigInt(a) & 0b11111110n) >> 1n;
1000
- return Number(o) / 90;
999
+ const e = t.getUint8(s), i = t.getUint8(s + 1), n = t.getUint8(s + 2), o = t.getUint8(s + 3), a = t.getUint8(s + 4), r = (BigInt(e) & 0b00001110n) << 29n | (BigInt(i) & 0b11111111n) << 22n | (BigInt(n) & 0b11111110n) << 14n | (BigInt(o) & 0b11111111n) << 7n | (BigInt(a) & 0b11111110n) >> 1n;
1000
+ return Number(r) / 90;
1001
1001
  }
1002
1002
  getNalus = (t) => {
1003
1003
  const s = [];
@@ -1017,16 +1017,16 @@ class Z {
1017
1017
  }
1018
1018
  break;
1019
1019
  }
1020
- let r = e - i;
1021
- if (t[e - 1] === 0 && (r -= 1), r !== 0) {
1022
- const a = t.slice(i, i + r), o = K(a);
1023
- s.push({ type: n, nalu: o });
1020
+ let o = e - i;
1021
+ if (t[e - 1] === 0 && (o -= 1), o !== 0) {
1022
+ const a = t.slice(i, i + o), r = K(a);
1023
+ s.push({ type: n, nalu: r });
1024
1024
  }
1025
1025
  }
1026
1026
  return s;
1027
1027
  };
1028
1028
  }
1029
- const _ = (l, t) => l.getUint8(t) << 16 | l.getUint8(t + 1) << 8 | l.getUint8(t + 2);
1029
+ const T = (l, t) => l.getUint8(t) << 16 | l.getUint8(t + 1) << 8 | l.getUint8(t + 2);
1030
1030
  class tt {
1031
1031
  audioConfig;
1032
1032
  videoConfig;
@@ -1039,53 +1039,53 @@ class tt {
1039
1039
  parse = async (t) => {
1040
1040
  let s = 0;
1041
1041
  for (this.header || (this.parseHeader(t, s), s += 9); this.isSurplusTag(t, s) !== !1; ) {
1042
- const i = this.parseTagHeader(t, s + 4), { tagType: n, dataSize: r, timestamp: a } = i;
1042
+ const i = this.parseTagHeader(t, s + 4), { tagType: n, dataSize: o, timestamp: a } = i;
1043
1043
  if (n) {
1044
- const o = this.parseTagBody(n, t, s + 4 + 11, r);
1044
+ const r = this.parseTagBody(n, t, s + 4 + 11, o);
1045
1045
  switch (n) {
1046
1046
  case "script":
1047
- this.on.info && this.on.info(o);
1047
+ this.on.info && this.on.info(r);
1048
1048
  break;
1049
1049
  case "audio":
1050
1050
  {
1051
- const { accPacketType: d } = o;
1051
+ const { accPacketType: d } = r;
1052
1052
  if (d === 0) {
1053
- const { codec: c, sampleRate: h, channelConfiguration: p } = o;
1053
+ const { codec: c, sampleRate: h, channelConfiguration: p } = r;
1054
1054
  this.audioConfig = { kind: "audio", codec: c, sampleRate: h, numberOfChannels: p }, this.on.config && this.on.config(this.audioConfig);
1055
1055
  } else {
1056
- const { cts: c, data: h } = o, p = "key", u = c === void 0 ? void 0 : c + a;
1056
+ const { cts: c, data: h } = r, p = "key", u = c === void 0 ? void 0 : c + a;
1057
1057
  this.on.chunk && this.on.chunk({ kind: "audio", type: p, dts: a, pts: u, cts: c, data: h });
1058
1058
  }
1059
1059
  }
1060
1060
  break;
1061
1061
  case "video":
1062
1062
  {
1063
- const { avcPacketType: d } = o;
1063
+ const { avcPacketType: d } = r;
1064
1064
  if (d === 0) {
1065
- const { codec: c, sps: h, pps: p, data: u } = o;
1065
+ const { codec: c, sps: h, pps: p, data: u } = r;
1066
1066
  this.videoConfig = { kind: "video", codec: c, description: u, sps: h, pps: p }, this.on.config && this.on.config(this.videoConfig);
1067
1067
  } else {
1068
- const { frameType: c, cts: h, data: p, nalus: u } = o, g = c === 1 ? "key" : "delta", f = h === void 0 ? void 0 : h + a;
1068
+ const { frameType: c, cts: h, data: p, nalus: u } = r, g = c === 1 ? "key" : "delta", f = h === void 0 ? void 0 : h + a;
1069
1069
  this.on.chunk && this.on.chunk({ kind: "video", type: g, dts: a, pts: f, cts: h, data: p, nalus: u });
1070
1070
  }
1071
1071
  }
1072
1072
  break;
1073
1073
  }
1074
- s = s + 4 + 11 + r;
1074
+ s = s + 4 + 11 + o;
1075
1075
  }
1076
- await new Promise((o) => setTimeout(() => o(!0), 8));
1076
+ await new Promise((r) => setTimeout(() => r(!0), 8));
1077
1077
  }
1078
1078
  return s;
1079
1079
  };
1080
1080
  // Header
1081
1081
  parseHeader = (t, s) => {
1082
- let e, i, n, r;
1082
+ let e, i, n, o;
1083
1083
  e = t.getUint8(s) << 16 | t.getUint8(s + 1) << 8 | t.getUint8(s + 2), i = t.getUint8(3);
1084
1084
  {
1085
- const o = t.getUint8(0).toString(2).padStart(5, "0").split(""), [, , d, , c] = o;
1085
+ const r = t.getUint8(0).toString(2).padStart(5, "0").split(""), [, , d, , c] = r;
1086
1086
  n = { audio: c === "1", video: d === "1" };
1087
1087
  }
1088
- r = t.getUint32(5), this.header = { signature: e, version: i, flags: n, dataOffset: r };
1088
+ o = t.getUint32(5), this.header = { signature: e, version: i, flags: n, dataOffset: o };
1089
1089
  };
1090
1090
  // 是否是完整tag
1091
1091
  isSurplusTag = (t, s) => {
@@ -1096,17 +1096,17 @@ class tt {
1096
1096
  else if (s + 4 + 11 > i)
1097
1097
  e = !1;
1098
1098
  else {
1099
- const n = _(t, s + 4 + 1);
1099
+ const n = T(t, s + 4 + 1);
1100
1100
  s + 4 + 11 + n > i && (e = !1);
1101
1101
  }
1102
1102
  return e;
1103
1103
  };
1104
1104
  parseTagHeader = (t, s) => {
1105
- let e, i, n, r, a;
1105
+ let e, i, n, o, a;
1106
1106
  {
1107
- const o = t.getUint8(s);
1107
+ const r = t.getUint8(s);
1108
1108
  let d;
1109
- switch (o) {
1109
+ switch (r) {
1110
1110
  case 18:
1111
1111
  d = "script";
1112
1112
  break;
@@ -1119,7 +1119,7 @@ class tt {
1119
1119
  }
1120
1120
  e = d;
1121
1121
  }
1122
- return i = _(t, s + 1), n = _(t, s + 4), r = t.getUint8(s + 7), a = _(t, s + 8), { tagType: e, dataSize: i, timestamp: n, timestampExtended: r, streamID: a };
1122
+ return i = T(t, s + 1), n = T(t, s + 4), o = t.getUint8(s + 7), a = T(t, s + 8), { tagType: e, dataSize: i, timestamp: n, timestampExtended: o, streamID: a };
1123
1123
  };
1124
1124
  parseTagBody = (t, s, e, i) => {
1125
1125
  let n;
@@ -1154,44 +1154,44 @@ class tt {
1154
1154
  };
1155
1155
  parseAudio = (t, s, e) => {
1156
1156
  let i = s;
1157
- const n = t.getUint8(i), r = n >> 4 & 15, a = n >> 2 & 3, o = n >> 1 & 1, d = n & 1;
1157
+ const n = t.getUint8(i), o = n >> 4 & 15, a = n >> 2 & 3, r = n >> 1 & 1, d = n & 1;
1158
1158
  i = i + 1;
1159
1159
  const c = t.getUint8(i);
1160
1160
  i = i + 1;
1161
1161
  const h = e, p = new Uint8Array(t.buffer.slice(i, i + h));
1162
- if (r === 10 && c === 0) {
1162
+ if (o === 10 && c === 0) {
1163
1163
  const u = t.getUint8(i), g = t.getUint8(i + 1), f = u >> 3 & 31, m = (u & 7) << 1 | g >> 7, b = g >> 3 & 15, k = [96e3, 88200, 64e3, 48e3, 44100, 32e3, 24e3, 22050, 16e3, 12e3, 11025, 8e3, 7350], w = `mp4a.40.${f}`, U = k[m];
1164
- return { soundFormat: r, soundRate: a, soundSize: o, soundType: d, accPacketType: c, data: p, samplingFrequencyIndex: m, channelConfiguration: b, codec: w, sampleRate: U };
1164
+ return { soundFormat: o, soundRate: a, soundSize: r, soundType: d, accPacketType: c, data: p, samplingFrequencyIndex: m, channelConfiguration: b, codec: w, sampleRate: U };
1165
1165
  }
1166
- return { soundFormat: r, soundRate: a, soundSize: o, soundType: d, accPacketType: c, data: p };
1166
+ return { soundFormat: o, soundRate: a, soundSize: r, soundType: d, accPacketType: c, data: p };
1167
1167
  };
1168
1168
  parseVideo = (t, s, e) => {
1169
1169
  let i = s;
1170
- const n = t.getUint8(i), r = n >> 4 & 15, a = n & 15;
1170
+ const n = t.getUint8(i), o = n >> 4 & 15, a = n & 15;
1171
1171
  i = i + 1;
1172
- const o = t.getUint8(i);
1172
+ const r = t.getUint8(i);
1173
1173
  i = i + 1;
1174
- const d = _(t, i);
1174
+ const d = T(t, i);
1175
1175
  i = i + 3;
1176
1176
  const c = e - 5, h = new Uint8Array(t.buffer.slice(i, i + c));
1177
1177
  switch (a) {
1178
1178
  case 7:
1179
- if (o === 0) {
1179
+ if (r === 0) {
1180
1180
  const p = P(h);
1181
- return { frameType: r, codecID: a, avcPacketType: o, cts: d, data: h, ...p };
1182
- } else if (o === 1) {
1181
+ return { frameType: o, codecID: a, avcPacketType: r, cts: d, data: h, ...p };
1182
+ } else if (r === 1) {
1183
1183
  const p = [], u = i + e - 5;
1184
1184
  for (; !(i + 4 > u); ) {
1185
1185
  const g = t.getUint32(i, !1), f = new Uint8Array(t.buffer.slice(i, i + 4 + g));
1186
1186
  i += 4 + g, p.push(f);
1187
1187
  }
1188
- return { frameType: r, codecID: a, avcPacketType: o, cts: d, data: h, nalus: p };
1188
+ return { frameType: o, codecID: a, avcPacketType: r, cts: d, data: h, nalus: p };
1189
1189
  }
1190
1190
  break;
1191
1191
  default:
1192
1192
  throw new Error("Unsupported codecID");
1193
1193
  }
1194
- return { frameType: r, codecID: a, avcPacketType: o, cts: d, data: h };
1194
+ return { frameType: o, codecID: a, avcPacketType: r, cts: d, data: h };
1195
1195
  };
1196
1196
  getAmfType = (t, s) => t.getUint8(s);
1197
1197
  getAMFName = (t, s, e) => {
@@ -1199,43 +1199,43 @@ class tt {
1199
1199
  return this.textDecoder?.decode(i) || "";
1200
1200
  };
1201
1201
  getAMFValue = (t, s, e) => {
1202
- let i = s, n, r = 0;
1202
+ let i = s, n, o = 0;
1203
1203
  switch (e) {
1204
1204
  case 0:
1205
- n = t.getFloat64(i, !1), r = 8;
1205
+ n = t.getFloat64(i, !1), o = 8;
1206
1206
  break;
1207
1207
  case 1:
1208
- n = !!t.getUint8(i), r = 1;
1208
+ n = !!t.getUint8(i), o = 1;
1209
1209
  break;
1210
1210
  case 2:
1211
1211
  {
1212
1212
  n = "";
1213
- const o = t.getUint16(i, !1);
1213
+ const r = t.getUint16(i, !1);
1214
1214
  i = i + 2;
1215
- const d = new Int8Array(t.buffer, i, o).filter((h) => h !== 0);
1216
- n = (this.textDecoder?.decode(d) || "").trim(), r = 2 + o;
1215
+ const d = new Int8Array(t.buffer, i, r).filter((h) => h !== 0);
1216
+ n = (this.textDecoder?.decode(d) || "").trim(), o = 2 + r;
1217
1217
  }
1218
1218
  break;
1219
1219
  case 3:
1220
1220
  for (n = {}; i < t.byteLength; ) {
1221
- const o = t.getUint16(i, !1);
1222
- if (o === 0) break;
1221
+ const r = t.getUint16(i, !1);
1222
+ if (r === 0) break;
1223
1223
  i = i + 2;
1224
- const d = this.getAMFName(t, i, o);
1225
- i = i + o;
1224
+ const d = this.getAMFName(t, i, r);
1225
+ i = i + r;
1226
1226
  const c = this.getAmfType(t, i);
1227
1227
  if (c === 6) break;
1228
1228
  i = i + 1;
1229
1229
  const h = this.getAMFValue(t, i, c);
1230
- i = i + h.length, n[d] = h.value, r = 2 + o + 1 + h.length;
1230
+ i = i + h.length, n[d] = h.value, o = 2 + r + 1 + h.length;
1231
1231
  }
1232
1232
  break;
1233
1233
  case 8:
1234
1234
  {
1235
1235
  n = {};
1236
- const o = t.getUint32(i, !1);
1236
+ const r = t.getUint32(i, !1);
1237
1237
  i = i + 4;
1238
- for (let d = 0; d < o; d++) {
1238
+ for (let d = 0; d < r; d++) {
1239
1239
  const c = t.getUint16(i, !1);
1240
1240
  i = i + 2;
1241
1241
  const h = this.getAMFName(t, i, c);
@@ -1243,25 +1243,25 @@ class tt {
1243
1243
  const p = this.getAmfType(t, i);
1244
1244
  i = i + 1;
1245
1245
  const u = this.getAMFValue(t, i, p);
1246
- i = i + u.length, n[h] = u.value, r = 2 + c + 1 + u.length;
1246
+ i = i + u.length, n[h] = u.value, o = 2 + c + 1 + u.length;
1247
1247
  }
1248
1248
  }
1249
1249
  break;
1250
1250
  case 10:
1251
1251
  {
1252
1252
  n = [];
1253
- const o = t.getUint32(i, !1);
1253
+ const r = t.getUint32(i, !1);
1254
1254
  i = i + 4;
1255
- for (let d = 0; d < o; d++) {
1255
+ for (let d = 0; d < r; d++) {
1256
1256
  const c = this.getAmfType(t, i);
1257
1257
  i = i + 1;
1258
1258
  const h = this.getAMFValue(t, i, c);
1259
- i = i + h.length, n.push(h.value), r = 1 + h.length;
1259
+ i = i + h.length, n.push(h.value), o = 1 + h.length;
1260
1260
  }
1261
1261
  }
1262
1262
  break;
1263
1263
  }
1264
- return { amfType: e, length: r, value: n };
1264
+ return { amfType: e, length: o, value: n };
1265
1265
  };
1266
1266
  }
1267
1267
  class st {
@@ -1321,6 +1321,7 @@ class it {
1321
1321
  baseTime = 0;
1322
1322
  // ms
1323
1323
  pendingChunks = [];
1324
+ currentChunk;
1324
1325
  isProcessing = !1;
1325
1326
  decodeTimer = 0;
1326
1327
  // 解码定时器
@@ -1343,6 +1344,8 @@ class it {
1343
1344
  secondVideoChunkTimestamp;
1344
1345
  decodingSpeedRatio = 1;
1345
1346
  maxDecodingSpeedRatio = 2;
1347
+ frameStartTime;
1348
+ // 帧开始时间 用于校准最终渲染时间
1346
1349
  lastRenderTime;
1347
1350
  // 上一次渲染的时间
1348
1351
  nextRenderTime;
@@ -1353,9 +1356,38 @@ class it {
1353
1356
  init = (t) => {
1354
1357
  this.destroy(), this.pattern = t, this.baseTime = (/* @__PURE__ */ new Date()).getTime() - performance.now(), this.initDecodeInterval();
1355
1358
  };
1359
+ initAudio = (t) => {
1360
+ this.audio.destroy(), this.audioDecoderConfig = { ...t }, this.audioDecoder = new AudioDecoder({
1361
+ output: (s) => {
1362
+ const e = this.decodingSpeedRatio;
1363
+ this.on.audio.decode && this.on.audio.decode({ audioData: s, playbackRate: e });
1364
+ },
1365
+ error: (s) => {
1366
+ this.on.audio.error && this.on.audio.error(s);
1367
+ }
1368
+ }), this.audioDecoder.configure(this.audioDecoderConfig);
1369
+ };
1370
+ initVideo = (t) => {
1371
+ this.video.destroy(), this.videoDecoderConfig = { ...t }, this.videoDecoder = new VideoDecoder({
1372
+ output: async (s) => {
1373
+ this.frameStartTime || (this.frameStartTime = s.timestamp);
1374
+ const e = s.timestamp - this.frameStartTime + this.baseTime * 1e3, i = await createImageBitmap(s);
1375
+ s.close(), i.width > 0 && i.height > 0 ? (this.on.video.decode && this.on.video.decode({ timestamp: e, bitmap: i }), this.currentChunk && this.currentChunk.kind === "video" && this.currentChunk.nalus && this.on.nalus && this.on.nalus(this.currentChunk.nalus)) : i.close();
1376
+ },
1377
+ error: (s) => {
1378
+ this.on.video.error && this.on.video.error(s);
1379
+ }
1380
+ }), this.videoDecoder.configure(this.videoDecoderConfig);
1381
+ };
1356
1382
  setFrameTrack = (t) => {
1357
1383
  this.frameTrack = t, this.frameTrack === !1 && (this.decodingSpeedRatio = 1);
1358
1384
  };
1385
+ push = (t) => {
1386
+ this.pendingChunks.push(t);
1387
+ };
1388
+ destroy = () => {
1389
+ this.audio.destroy(), this.video.destroy(), clearInterval(this.decodeTimer);
1390
+ };
1359
1391
  initDecodeInterval = () => {
1360
1392
  let t = this.decodingSpeed / this.decodingSpeedRatio;
1361
1393
  const s = this.baseTime + performance.now();
@@ -1370,30 +1402,31 @@ class it {
1370
1402
  decode = () => {
1371
1403
  if (this.isProcessing !== !0) {
1372
1404
  for (this.isProcessing = !0; ; ) {
1373
- const t = this.pendingChunks.shift(), s = this.pendingChunks.length;
1405
+ this.currentChunk = this.pendingChunks.shift();
1406
+ const t = this.pendingChunks.length;
1374
1407
  if (this.frameTrack) {
1375
- const [n, r] = this.fameTrackOption[this.pattern];
1376
- if (s <= n && (this.isFrameTrack = !1), s >= r && (this.isFrameTrack = !0), this.isFrameTrack) {
1377
- const a = Math.min(1 + (s - n) / 100, this.maxDecodingSpeedRatio);
1378
- this.decodingSpeedRatio = Number(a.toFixed(1));
1408
+ const [i, n] = this.fameTrackOption[this.pattern];
1409
+ if (t <= i && (this.isFrameTrack = !1), t >= n && (this.isFrameTrack = !0), this.isFrameTrack) {
1410
+ const o = Math.min(1 + (t - i) / 100, this.maxDecodingSpeedRatio);
1411
+ this.decodingSpeedRatio = Number(o.toFixed(1));
1379
1412
  } else
1380
1413
  this.decodingSpeedRatio = 1;
1381
1414
  }
1382
1415
  if (this.on.debug) {
1383
- const { decodingSpeed: n, decodingSpeedRatio: r, fps: a } = this;
1384
- this.on.debug({ decodingSpeed: n, decodingSpeedRatio: r, fps: a });
1416
+ const { decodingSpeed: i, decodingSpeedRatio: n, fps: o } = this;
1417
+ this.on.debug({ decodingSpeed: i, decodingSpeedRatio: n, fps: o, cacheLength: t });
1385
1418
  }
1386
- if (!t) break;
1387
- const { type: e, init: i } = t;
1388
- switch (e) {
1419
+ if (!this.currentChunk) break;
1420
+ const { kind: s, init: e } = this.currentChunk;
1421
+ switch (s) {
1389
1422
  case "audio":
1390
- this.decodeAudio(i);
1423
+ this.decodeAudio(e);
1391
1424
  break;
1392
1425
  case "video":
1393
- this.decodeVideo(i);
1426
+ this.decodeVideo(e);
1394
1427
  break;
1395
1428
  }
1396
- if (e === "video") break;
1429
+ if (s === "video") break;
1397
1430
  }
1398
1431
  this.isProcessing = !1;
1399
1432
  }
@@ -1409,24 +1442,7 @@ class it {
1409
1442
  this.videoDecoder.decode(s);
1410
1443
  }
1411
1444
  };
1412
- destroy = () => {
1413
- this.audio.destroy(), this.video.destroy(), clearInterval(this.decodeTimer);
1414
- };
1415
1445
  audio = {
1416
- init: (t) => {
1417
- this.audio.destroy(), this.audioDecoderConfig = { ...t }, this.audioDecoder = new AudioDecoder({
1418
- output: (s) => {
1419
- const e = this.decodingSpeedRatio;
1420
- this.on.audio.decode && this.on.audio.decode({ audioData: s, playbackRate: e });
1421
- },
1422
- error: (s) => {
1423
- this.on.audio.error && this.on.audio.error(s);
1424
- }
1425
- }), this.audioDecoder.configure(this.audioDecoderConfig);
1426
- },
1427
- push: (t) => {
1428
- this.pendingChunks.push({ type: "audio", init: t });
1429
- },
1430
1446
  flush: () => {
1431
1447
  this.audioDecoder?.flush();
1432
1448
  },
@@ -1435,20 +1451,6 @@ class it {
1435
1451
  }
1436
1452
  };
1437
1453
  video = {
1438
- init: (t) => {
1439
- this.video.destroy(), this.videoDecoderConfig = { ...t }, this.videoDecoder = new VideoDecoder({
1440
- output: async (s) => {
1441
- const e = s.timestamp + this.baseTime * 1e3, i = await createImageBitmap(s);
1442
- s.close(), i.width > 0 && i.height > 0 ? this.on.video.decode && this.on.video.decode({ timestamp: e, bitmap: i }) : i.close();
1443
- },
1444
- error: (s) => {
1445
- this.on.video.error && this.on.video.error(s);
1446
- }
1447
- }), this.videoDecoder.configure(this.videoDecoderConfig);
1448
- },
1449
- push: (t) => {
1450
- this.pendingChunks.push({ type: "video", init: t });
1451
- },
1452
1454
  flush: () => {
1453
1455
  this.videoDecoder?.flush();
1454
1456
  },
@@ -1472,8 +1474,8 @@ class nt {
1472
1474
  const { timestamp: s } = t;
1473
1475
  let { bitmap: e } = t;
1474
1476
  if (this.cutOption) {
1475
- const { sx: n = 0, sy: r = 0, sw: a = e.width, sh: o = e.height } = this.cutOption;
1476
- e = await createImageBitmap(e, n, r, a, o);
1477
+ const { sx: n = 0, sy: o = 0, sw: a = e.width, sh: r = e.height } = this.cutOption;
1478
+ e = await createImageBitmap(e, n, o, a, r);
1477
1479
  }
1478
1480
  const i = new VideoFrame(e, { timestamp: s });
1479
1481
  this.cutOption && e.close(), this.writer.write(i), i.close();
@@ -1498,7 +1500,7 @@ export {
1498
1500
  it as Decoder,
1499
1501
  N as DecoderWorker,
1500
1502
  st as Demuxer,
1501
- F as DemuxerWorker,
1503
+ V as DemuxerWorker,
1502
1504
  et as PrPlayer,
1503
1505
  nt as Render,
1504
1506
  z as RenderWorker