pr-player 0.4.2 → 0.5.2

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.
Files changed (32) hide show
  1. package/README.md +150 -5
  2. package/dist/{src/PrPlayer.d.ts → PrPlayer.d.ts} +4 -3
  3. package/dist/{src/decoder → decoder}/Decoder.d.ts +1 -1
  4. package/dist/{src/decoder → decoder}/type.d.ts +1 -1
  5. package/dist/{src/demuxer → demuxer}/Cacher.d.ts +1 -0
  6. package/dist/{src/demuxer → demuxer}/Demuxer.d.ts +2 -15
  7. package/dist/{src/demuxer → demuxer/parsers}/264Parser.d.ts +3 -8
  8. package/dist/demuxer/parsers/boxParser.d.ts +19 -0
  9. package/dist/{src/demuxer → demuxer/parsers}/flv264Parser.d.ts +4 -4
  10. package/dist/demuxer/parsers/fmp4Parser.d.ts +38 -0
  11. package/dist/demuxer/parsers/index.d.ts +7 -0
  12. package/dist/demuxer/parsers/mpdParser.d.ts +41 -0
  13. package/dist/{src/demuxer → demuxer/parsers}/ts264Parser.d.ts +8 -4
  14. package/dist/demuxer/parsers/types.d.ts +14 -0
  15. package/dist/{src/index.d.ts → index.d.ts} +1 -0
  16. package/dist/index.js +1259 -573
  17. package/dist/index.umd.cjs +2 -2
  18. package/dist/{src/render → render}/Render.d.ts +3 -3
  19. package/dist/{src/render → render}/RenderWorker.d.ts +2 -2
  20. package/dist/{src/tools.d.ts → tools.d.ts} +1 -1
  21. package/dist/type.d.ts +1 -0
  22. package/package.json +5 -5
  23. package/dist/src/type.d.ts +0 -1
  24. /package/dist/{src/PrResolves.d.ts → PrResolves.d.ts} +0 -0
  25. /package/dist/{src/audioPlayer → audioPlayer}/audioPlayer.d.ts +0 -0
  26. /package/dist/{src/decoder → decoder}/DecoderWorker.d.ts +0 -0
  27. /package/dist/{src/decoder → decoder}/decoder.worker.d.ts +0 -0
  28. /package/dist/{src/demuxer → demuxer}/DemuxerWorker.d.ts +0 -0
  29. /package/dist/{src/demuxer → demuxer}/demuxer.worker.d.ts +0 -0
  30. /package/dist/{src/demuxer → demuxer}/type.d.ts +0 -0
  31. /package/dist/{src/render → render}/render.worker.d.ts +0 -0
  32. /package/dist/{src/render → render}/type.d.ts +0 -0
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region src/demuxer/demuxer.worker.ts?worker&inline
2
- var e = "(function(){var e=class{pendingPayloads=[];payload=new Uint8Array;chunks=[];push=e=>{this.pendingPayloads.push(e)};next=(e=0)=>{this.payload=this.payload.slice(e);let t=this.pendingPayloads.shift();if(!t)return!1;let n=new Uint8Array(this.payload.byteLength+t.byteLength);return n.set(this.payload,0),n.set(t,this.payload.byteLength),this.payload=n,new DataView(this.payload.buffer)};pushChunk=e=>{this.chunks.push(e),this.chunks.length>100&&this.chunks.shift()};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array,this.chunks=[]}};let t=(e,t)=>{let n=new Uint8Array(e),r=new Uint8Array(t),i=new Uint8Array(11+n.length+r.length),a=0;return i[a++]=1,i[a++]=n[1],i[a++]=n[2],i[a++]=n[3],i[a++]=255,i[a++]=225,i[a++]=n.length>>8&255,i[a++]=n.length&255,i.set(n,a),a+=n.length,i[a++]=1,i[a++]=r.length>>8&255,i[a++]=r.length&255,i.set(r,a),i},n=e=>{let t=0,n=new DataView(e.buffer),r=n.getUint8(t);if(t+=1,r!==1)throw Error(`Invalid AVC version`);let i=n.getUint8(t)&255;t+=1;let a=n.getUint8(t)&255;t+=1;let o=n.getUint8(t)&255;t+=1;let s=`avc1.${Array.from([i,a,o],e=>e.toString(16).padStart(2,`0`)).join(``)}`,c=(n.getUint8(t)&3)-1;t+=1;let l=n.getUint8(t)&31;t+=1;let u=n.getUint16(t,!1);t+=2;let d=new Uint8Array(n.buffer.slice(t,t+u));t+=u;let f=n.getUint8(t)&31;t+=1;let p=n.getUint16(t,!1);t+=2;let m=new Uint8Array(n.buffer.slice(t,t+p));return t+=p,{version:r,codec:s,profile:i,compatibility:a,level:o,lengthSizeMinusOne:c,numOfSequenceParameterSets:l,sequenceParameterSetLength:u,sps:d,numOfPictureParameterSets:f,pictureParameterSetLength:p,pps:m}},r=e=>{let t=new Uint8Array(4+e.length);return new DataView(t.buffer).setUint32(0,e.length,!1),t.set(e,4),t},i=e=>{let t=0;for(let n of e)t+=n.length;let n=new Uint8Array(t),r=0;for(let t of e){let e=t;n.set(e,r),r+=e.length}return n},a=e=>{let t=`unknown`;switch(e){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};var o=class{pat;pmt;audioConfig;videoConfig;payloadMap=new Map;on={};constructor(){}parse=async e=>{let t=0;for(;!(t+188>e.byteLength);){if(e.getInt8(t)!=71){t++;continue}await this.parsePacket(e,t),t+=188}return t};parsePacket=async(e,t)=>{if(t+188>e.byteLength||e.getUint8(t)!==71)throw Error(`Invalid TS packet`);let n=t,r=this.parseHeader(e,n);n+=4;let{transport_error_indicator:i,pid:a,payload_unit_start_indicator:o,adaptation_field_control:s}=r;if(i===1||a===void 0)return;let c=184;if(s===2||s===3){let t=e.getUint8(n);n+=1,this.parseAdaptationField(e,n),n+=t,c-=t}if(s===3&&--c,s===1||s===3){let t=new Uint8Array(e.buffer.slice(n,n+c));if(a===0)return this.parsePAT(e,n);{let{programs:t=[]}=this.pat||{};if(t.find(e=>e.pmt_pid===a))return this.parsePMT(e,n)}let{streams:r=[]}=this.pmt||{},i=r.find(e=>e.elementary_pid===a);if(i){if(o===1){let e=this.payloadMap.get(a);if(e){switch(i.kind){case`audio`:{let t=await this.parseAudio(e);this.on.chunk&&this.on.chunk(t)}break;case`video`:{let t=await this.parseVideo(e);this.on.chunk&&this.on.chunk(t),await new Promise(e=>setTimeout(()=>e(!0),0))}break}this.payloadMap.delete(a)}}{this.payloadMap.has(a)||this.payloadMap.set(a,new Uint8Array);let e=this.payloadMap.get(a),n=new Uint8Array(e.byteLength+t.byteLength);n.set(e,0),n.set(t,e.byteLength),this.payloadMap.set(a,n)}}}};parseHeader=(e,t)=>{let n=t,r=e.getUint8(n),i=e.getUint8(n+1),a=e.getUint8(n+2),o=e.getUint8(n+3),s=(i&128)>>7;return s===1?{sync_byte:r,transport_error_indicator:s}:{sync_byte:r,transport_error_indicator:s,payload_unit_start_indicator:(i&64)>>6,transport_priority:(i&32)>>5,pid:(i&31)<<8|a,transport_scrambling_control:(o&192)>>6,adaptation_field_control:o>>4&3,continuity_counter:o&15}};parsePAT=(e,t)=>{let n=t,r;{let t=e.getUint8(n);n+=1;let i=e.getUint8(n);if(n+=1,i!==0)throw Error(`Invalid PAT table_id`);let a=e.getUint16(n)&4095;n+=2;let o=e.getUint16(n);n+=2;let s=(e.getUint8(n)&62)>>1,c=e.getUint8(n)&1;n+=1;let l=e.getUint8(n);n+=1;let u=e.getUint8(n);n+=1,r={pointer_field:t,table_id:i,section_length:a,transport_stream_id:o,version_number:s,current_next_indicator:c,section_number:l,last_section_number:u}}let i=[];{let t=r.section_length-5-4,a=n+t;for(;n<a;){let t=e.getUint16(n),r=e.getUint16(n+2)&8191;n+=4,t!==0&&r>=32&&r<=8190&&i.push({program_number:t,pmt_pid:r})}}let a=e.getUint32(n);this.pat={header:r,programs:i,crc32:a},this.on.debug&&this.on.debug({pat:this.pat})};parsePMT=(e,t)=>{let n=t,r;{let t=e.getUint8(n);n+=1;let i=e.getUint8(n);if(n+=1,i!==2)throw Error(`Invalid PMT table_id`);let a=e.getUint16(n)&4095;n+=2;let o=e.getUint16(n);n+=2;let s=(e.getUint8(n)&62)>>1,c=e.getUint8(n)&1;n+=1;let l=e.getUint8(n);n+=1;let u=e.getUint8(n);n+=1;let d=e.getUint16(n)&8191;n+=2;let f=e.getUint16(n)&4095;n+=2,r={pointer_field:t,table_id:i,section_length:a,transport_stream_id:o,version_number:s,current_next_indicator:c,section_number:l,last_section_number:u,pcr_pid:d,program_info_length:f}}let i=[];{let t=r.section_length-9-4,o=n+t;for(;n<o;){let t=e.getUint8(n),r=a(t),o=e.getUint16(n+1)&8191,s=e.getUint16(n+3)&4095;if(n+=5,o<32||o>8190){console.warn(`Invalid elementary_pid: 0x${o.toString(16)}`);continue}i.push({kind:r,stream_type:t,elementary_pid:o,es_info_length:s})}}let o=e.getUint32(n);this.pmt={header:r,streams:i,crc32:o},this.on.debug&&this.on.debug({pmt:this.pmt})};parseAdaptationField=(e,t)=>{let n=t,r,i,a,o,s=e.getUint8(n),c=!!(s&128),l=!!(s&64),u=!!(s&32),d=!!(s&16),f=!!(s&8),p=!!(s&4),m=!!(s&2),h=!!(s&1);n+=1;let g=(e,t)=>{let n=BigInt(0);n|=BigInt(e.getUint16(t))<<25n,n|=BigInt(e.getUint16(t+1))<<17n,n|=BigInt(e.getUint16(t+2))<<9n,n|=BigInt(e.getUint16(t+3))<<1n,n|=BigInt(e.getUint16(t+4)>>7);let r=(e.getUint16(t+4)&1)<<8|e.getUint16(t+5);return n=n*300n+BigInt(r),n};if(d&&(r=g(e,n),n+=6),f&&(i=g(e,n),n+=6),p&&(a=e.getInt8(n),n+=1),m){let t=e.getUint8(n);n+=1,o=new Uint8Array(e.buffer,n,t),n+=t}return{discontinuity_indicator:c,random_access_indicator:l,elementary_stream_priority_indicator:u,pcr_flag:d,opcr_flag:f,splicing_point_flag:p,transport_private_data_flag:m,adaptation_field_extension_flag:h,pcr:r,opcr:i,splice_countdown:a,transport_private_data:o}};parseAudio=async e=>{let t=new DataView(e.buffer),n=0,r,i;{let e=t.getUint8(n)===0&&t.getUint8(n+1)===0&&t.getUint8(n+2)===1;if(n+=3,!e)throw Error(`invalid ts audio payload.`);let i=t.getUint8(n);n+=1;let a=t.getUint8(n)<<8|t.getUint8(n+1);n+=2;let o,s,c,l,u;{let e=t.getUint8(n);n+=1,o=e>>4&3,s=(e>>3&1)==1,c=(e>>2&1)==1,l=(e>>1&1)==1,u=(e&1)==1}let d,f;{let e=t.getUint8(n);n+=1;let r=e>>6,i=t.getUint8(n);n+=1,(r&2)==2&&(d=this.parsePtsDts(t,n)),f=(r&1)==1?this.parsePtsDts(t,n+5):d,n+=i}r={stream_id:i,pes_packet_length:a,scrambling_control:o,priority:s,data_alignment:c,copyright:l,original_copy:u,pts:d,dts:f}}i=e.slice(n);{if(!this.audioConfig&&t.getUint8(n)===255){let e=t.getUint8(n+2),r=t.getUint8(n+3),i,a;a=e>>2&15,i=(e&1)<<2|r>>6,this.audioConfig={kind:`audio`,codec:`mp4a.40.${i}`,sampleRate:[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350][a],numberOfChannels:i},this.on.config&&this.on.config(this.audioConfig)}let{dts:e=0,pts:a=0}=r;return{kind:`audio`,type:`key`,dts:e,pts:a,cts:a-e,data:i.slice(7)}}};parseVideo=async e=>{let r=new DataView(e.buffer),a=0,o,s;{let e=r.getUint8(a)===0&&r.getUint8(a+1)===0&&r.getUint8(a+2)===1;if(a+=3,!e)throw Error(`invalid ts video payload.`);let t=r.getUint8(a);a+=1;let n=r.getUint8(a)<<8|r.getUint8(a+1);a+=2;let i,s,c,l,u;{let e=r.getUint8(a);a+=1,i=e>>4&3,s=(e>>3&1)==1,c=(e>>2&1)==1,l=(e>>1&1)==1,u=(e&1)==1}let d,f;{let e=r.getUint8(a);a+=1;let t=e>>6,n=r.getUint8(a);a+=1,(t&2)==2&&(d=this.parsePtsDts(r,a)),f=(t&1)==1?this.parsePtsDts(r,a+5):d,a+=n}o={stream_id:t,pes_packet_length:n,scrambling_control:i,priority:s,data_alignment:c,copyright:l,original_copy:u,pts:d,dts:f}}s=e.slice(a);{let e=this.getNalus(s);if(!this.videoConfig){let r,i;if(r=e.find(e=>e.type===7)?.nalu.slice(4),i=e.find(e=>e.type===8)?.nalu.slice(4),r&&i){let e=t(r,i),{codec:a}=n(e);this.videoConfig={kind:`video`,codec:a,description:e,sps:r,pps:i},this.on.config&&this.on.config(this.videoConfig)}}let r=[],a=`delta`;for(let t of e){let{type:e,nalu:n}=t;switch(e){case 6:case 9:r.push(n);break;case 1:a=`delta`,r.push(n);break;case 5:a=`key`,r.push(n);break}}let c=i(r),{dts:l=0,pts:u=0}=o,d=u-l;return{kind:`video`,type:a,dts:l,pts:u,cts:d,data:c,nalus:r}}};parsePtsDts(e,t){let n=e.getUint8(t),r=e.getUint8(t+1),i=e.getUint8(t+2),a=e.getUint8(t+3),o=e.getUint8(t+4),s=(BigInt(n)&14n)<<29n|(BigInt(r)&255n)<<22n|(BigInt(i)&254n)<<14n|(BigInt(a)&255n)<<7n|(BigInt(o)&254n)>>1n;return Number(s)/90}getNalus=e=>{let t=[],n=0;for(;!(n+4>e.byteLength);){if(e[n]!==0||e[n+1]!==0||e[n+2]!==1){n+=1;continue}n+=3;let i=n,a=e[n]&31;for(n+=1;!(n+1>e.byteLength);){if(e[n]!==0||e[n+1]!==0||e[n+2]!==1){n+=1;continue}break}let o=n-i;if(e[n-1]===0&&--o,o!==0){let n=r(e.slice(i,i+o));t.push({type:a,nalu:n})}}return t}};let s=(e,t)=>e.getUint8(t)<<16|e.getUint8(t+1)<<8|e.getUint8(t+2);var c=class{audioConfig;videoConfig;header;textDecoder=new TextDecoder(`utf-8`);on={};constructor(){}parse=async e=>{let t=0;for(this.header||(this.parseHeader(e,t),t+=9);this.isSurplusTag(e,t)!==!1;){let{tagType:n,dataSize:r,timestamp:i}=this.parseTagHeader(e,t+4);if(n){let a=this.parseTagBody(n,e,t+4+11,r);switch(n){case`script`:this.on.info&&this.on.info(a);break;case`audio`:{let{accPacketType:e}=a;if(e===0){let{codec:e,sampleRate:t,channelConfiguration:n}=a;this.audioConfig={kind:`audio`,codec:e,sampleRate:t,numberOfChannels:n},this.on.config&&this.on.config(this.audioConfig)}else{let{cts:e,data:t}=a,n=e===void 0?void 0:e+i;this.on.chunk&&this.on.chunk({kind:`audio`,type:`key`,dts:i,pts:n,cts:e,data:t})}}break;case`video`:{let{avcPacketType:e}=a;if(e===0){let{codec:e,sps:t,pps:n,data:r}=a;this.videoConfig={kind:`video`,codec:e,description:r,sps:t,pps:n},this.on.config&&this.on.config(this.videoConfig)}else{let{frameType:e,cts:t,data:n,nalus:r}=a,o=e===1?`key`:`delta`,s=t===void 0?void 0:t+i;this.on.chunk&&this.on.chunk({kind:`video`,type:o,dts:i,pts:s,cts:t,data:n,nalus:r})}}break}t=t+4+11+r}await new Promise(e=>setTimeout(()=>e(!0),8))}return t};parseHeader=(e,t)=>{let n,r,i,a;n=e.getUint8(t)<<16|e.getUint8(t+1)<<8|e.getUint8(t+2),r=e.getUint8(3);{let[,,t,,n]=e.getUint8(0).toString(2).padStart(5,`0`).split(``);i={audio:n===`1`,video:t===`1`}}a=e.getUint32(5),this.header={signature:n,version:r,flags:i,dataOffset:a}};isSurplusTag=(e,t)=>{let n=!0,r=e.byteLength;if(t+4>r)n=!1;else if(t+4+11>r)n=!1;else{let i=s(e,t+4+1);t+4+11+i>r&&(n=!1)}return n};parseTagHeader=(e,t)=>{let n,r,i,a,o;{let r=e.getUint8(t),i;switch(r){case 18:i=`script`;break;case 8:i=`audio`;break;case 9:i=`video`;break}n=i}return r=s(e,t+1),i=s(e,t+4),a=e.getUint8(t+7),o=s(e,t+8),{tagType:n,dataSize:r,timestamp:i,timestampExtended:a,streamID:o}};parseTagBody=(e,t,n,r)=>{let i;switch(e){case`script`:i=this.parseMetaData(t,n);break;case`audio`:i=this.parseAudio(t,n,r);break;case`video`:i=this.parseVideo(t,n,r);break}return i};parseMetaData=(e,t)=>{let n=t;if(e.getUint8(n)!==2)throw Error(`Invalid AMF type for onMetaData (expected 0x02)`);n+=1;let r=e.getUint16(n,!1);n+=2;{let t=new Int8Array(e.buffer.slice(n,n+r));if((this.textDecoder?.decode(t)||``)!==`onMetaData`)throw Error(`Expected 'onMetaData' string`);n+=r}let i=this.getAmfType(e,n);return n+=1,this.getAMFValue(e,n,i).value};parseAudio=(e,t,n)=>{let r=t,i=e.getUint8(r),a=i>>4&15,o=i>>2&3,s=i>>1&1,c=i&1;r+=1;let l=e.getUint8(r);r+=1;let u=n,d=new Uint8Array(e.buffer.slice(r,r+u));if(a===10&&l===0){let t=e.getUint8(r),n=e.getUint8(r+1),i=t>>3&31,u=(t&7)<<1|n>>7;return{soundFormat:a,soundRate:o,soundSize:s,soundType:c,accPacketType:l,data:d,samplingFrequencyIndex:u,channelConfiguration:n>>3&15,codec:`mp4a.40.${i}`,sampleRate:[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350][u]}}return{soundFormat:a,soundRate:o,soundSize:s,soundType:c,accPacketType:l,data:d}};parseVideo=(e,t,r)=>{let i=t,a=e.getUint8(i),o=a>>4&15,c=a&15;i+=1;let l=e.getUint8(i);i+=1;let u=s(e,i);i+=3;let d=r-5,f=new Uint8Array(e.buffer.slice(i,i+d));switch(c){case 7:if(l===0)return{frameType:o,codecID:c,avcPacketType:l,cts:u,data:f,...n(f)};if(l===1){let t=[],n=i+r-5;for(;!(i+4>n);){let n=e.getUint32(i,!1),r=new Uint8Array(e.buffer.slice(i,i+4+n));i+=4+n,t.push(r)}return{frameType:o,codecID:c,avcPacketType:l,cts:u,data:f,nalus:t}}break;default:throw Error(`Unsupported codecID`)}return{frameType:o,codecID:c,avcPacketType:l,cts:u,data:f}};getAmfType=(e,t)=>e.getUint8(t);getAMFName=(e,t,n)=>{let r=new Uint8Array(e.buffer.slice(t,t+n));return this.textDecoder?.decode(r)||``};getAMFValue=(e,t,n)=>{let r=t,i,a=0;switch(n){case 0:i=e.getFloat64(r,!1),a=8;break;case 1:i=!!e.getUint8(r),a=1;break;case 2:{i=``;let t=e.getUint16(r,!1);r+=2;let n=new Int8Array(e.buffer,r,t).filter(e=>e!==0);i=(this.textDecoder?.decode(n)||``).trim(),a=2+t}break;case 3:for(i={};r<e.byteLength;){let t=e.getUint16(r,!1);if(t===0)break;r+=2;let n=this.getAMFName(e,r,t);r+=t;let o=this.getAmfType(e,r);if(o===6)break;r+=1;let s=this.getAMFValue(e,r,o);r+=s.length,i[n]=s.value,a=2+t+1+s.length}break;case 8:{i={};let t=e.getUint32(r,!1);r+=4;for(let n=0;n<t;n++){let t=e.getUint16(r,!1);r+=2;let n=this.getAMFName(e,r,t);r+=t;let o=this.getAmfType(e,r);r+=1;let s=this.getAMFValue(e,r,o);r+=s.length,i[n]=s.value,a=2+t+1+s.length}}break;case 10:{i=[];let t=e.getUint32(r,!1);r+=4;for(let n=0;n<t;n++){let t=this.getAmfType(e,r);r+=1;let n=this.getAMFValue(e,r,t);r+=n.length,i.push(n.value),a=1+n.length}}break}return{amfType:n,length:a,value:i}}};let l=new class{pattern;cacher=new e;isParseing=!1;offset=0;parser;on={};constructor(){}init=e=>{switch(this.destroy(),this.pattern=e,this.pattern){case`flv`:this.parser=new c;break;case`hls`:this.parser=new o;break;default:throw Error(`is error pattern.`)}this.parser.on.debug=e=>this.on.debug&&this.on.debug(e),this.parser.on.info=e=>this.on.info&&this.on.info(e),this.parser.on.config=e=>this.on.config&&this.on.config(e),this.parser.on.chunk=e=>{this.cacher.pushChunk(e),this.on.chunk&&this.on.chunk(e)}};push=e=>{this.cacher.push(e),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 Error(`You need to set the pattern.`);if(!this.parser)throw Error(`You need to init parser.`);for(;;){let e=this.cacher.next(this.offset);if(this.offset=0,!e)break;this.offset=await this.parser.parse(e)}this.isParseing=!1}catch{this.destroy()}}};l.on.info=e=>postMessage({action:`on.info`,data:e}),l.on.config=e=>postMessage({action:`on.config`,data:e}),l.on.chunk=e=>postMessage({action:`on.chunk`,data:e}),l.on.debug=e=>postMessage({action:`on.debug`,data:e}),onmessage=e=>{let{action:t,data:n}=e.data,r=l[t];r&&r(n)}})();", t = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", e], { type: "text/javascript;charset=utf-8" });
2
+ var e = "(function(){var e=class{pendingPayloads=[];payload=new Uint8Array;chunks=[];push=e=>{this.pendingPayloads.push(e)};next=(e=0)=>{this.payload=this.payload.slice(e);let t=this.pendingPayloads.shift();if(!t)return!1;let n=new Uint8Array(this.payload.byteLength+t.byteLength);return n.set(this.payload,0),n.set(t,this.payload.byteLength),this.payload=n,new DataView(this.payload.buffer)};pushChunk=e=>{this.chunks.push(e),this.chunks.length>100&&this.chunks.shift()};discardPayload=()=>{this.payload=new Uint8Array};destroy=()=>{this.pendingPayloads=[],this.payload=new Uint8Array,this.chunks=[]}};let t=(e,t)=>{let n=new Uint8Array(e),r=new Uint8Array(t),i=new Uint8Array(11+n.length+r.length),a=0;return i[a++]=1,i[a++]=n[1],i[a++]=n[2],i[a++]=n[3],i[a++]=255,i[a++]=225,i[a++]=n.length>>8&255,i[a++]=n.length&255,i.set(n,a),a+=n.length,i[a++]=1,i[a++]=r.length>>8&255,i[a++]=r.length&255,i.set(r,a),i},n=e=>{let t=0,n=new DataView(e.buffer,e.byteOffset,e.byteLength),r=n.getUint8(t);if(t+=1,r!==1)throw Error(`Invalid AVC version`);let i=n.getUint8(t)&255;t+=1;let a=n.getUint8(t)&255;t+=1;let o=n.getUint8(t)&255;t+=1;let s=`avc1.${Array.from([i,a,o],e=>e.toString(16).padStart(2,`0`)).join(``)}`;t+=1,t+=1;let c=n.getUint16(t,!1);t+=2;let l=new Uint8Array(e.buffer,e.byteOffset+t,c);t+=c,t+=1;let u=n.getUint16(t,!1);return t+=2,{version:r,codec:s,profile:i,compatibility:a,level:o,sps:l,pps:new Uint8Array(e.buffer,e.byteOffset+t,u)}},r=e=>{let t=new Uint8Array(4+e.length);return new DataView(t.buffer).setUint32(0,e.length,!1),t.set(e,4),t},i=e=>{let t=0;for(let n of e)t+=n.length;let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.length;return n},a=(e,t)=>e.getUint8(t)<<16|e.getUint8(t+1)<<8|e.getUint8(t+2),o=(e,t)=>{let n=a(e,t);return n&8388608&&(n|=-16777216),n},s=(e,t)=>t<<24|e,c=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350];var l=class{audioConfig;videoConfig;header;textDecoder=new TextDecoder(`utf-8`);on={};constructor(){}parse=async e=>{let t=0;for(this.header||(this.parseHeader(e,t),t+=9);this.isSurplusTag(e,t)!==!1;){let{tagType:n,dataSize:r,dts:i}=this.parseTagHeader(e,t+4);if(n){let a=this.parseTagBody(n,e,t+4+11,r);if(!a){t=t+4+11+r,await new Promise(e=>setTimeout(()=>e(!0),8));continue}switch(n){case`script`:this.on.info&&this.on.info(a);break;case`audio`:{let{accPacketType:e}=a;if(e===0){let{codec:e,sampleRate:t,channelConfiguration:n,data:r}=a;this.audioConfig={kind:`audio`,codec:e,sampleRate:t,numberOfChannels:n,...r?{description:r}:{}},this.on.config&&this.on.config(this.audioConfig)}else{let{cts:e,data:t}=a,n=e===void 0?void 0:e+i;this.on.chunk&&this.on.chunk({kind:`audio`,type:`key`,dts:i,pts:n,cts:e,data:t})}}break;case`video`:{let{avcPacketType:e}=a;if(e===0){let{codec:e,sps:t,pps:n,data:r}=a;this.videoConfig={kind:`video`,codec:e,description:r,sps:t,pps:n},this.on.config&&this.on.config(this.videoConfig)}else{let{frameType:e,cts:t,data:n,nalus:r}=a,o=e===1?`key`:`delta`,s=t===void 0?void 0:t+i;this.on.chunk&&this.on.chunk({kind:`video`,type:o,dts:i,pts:s,cts:t,data:n,nalus:r})}}break}t=t+4+11+r}await new Promise(e=>setTimeout(()=>e(!0),8))}return t};parseHeader=(e,t)=>{let n,r,i,a;n=e.getUint8(t)<<16|e.getUint8(t+1)<<8|e.getUint8(t+2),r=e.getUint8(t+3);{let n=e.getUint8(t+4);i={audio:!!(n&4),video:!!(n&1)}}a=e.getUint32(t+5),this.header={signature:n,version:r,flags:i,dataOffset:a}};isSurplusTag=(e,t)=>{let n=!0,r=e.byteLength;if(t+4>r)n=!1;else if(t+4+11>r)n=!1;else{let i=a(e,t+4+1);t+4+11+i>r&&(n=!1)}return n};parseTagHeader=(e,t)=>{let n,r,i,o,c;{let r=e.getUint8(t),i;switch(r){case 18:i=`script`;break;case 8:i=`audio`;break;case 9:i=`video`;break}n=i}r=a(e,t+1),i=a(e,t+4),o=e.getUint8(t+7),c=a(e,t+8);let l=s(i,o);return{tagType:n,dataSize:r,timestamp:i,timestampExtended:o,dts:l,streamID:c}};parseTagBody=(e,t,n,r)=>{let i;switch(e){case`script`:i=this.parseMetaData(t,n);break;case`audio`:i=this.parseAudio(t,n,r);break;case`video`:i=this.parseVideo(t,n,r);break}return i};parseMetaData=(e,t)=>{let n=t;if(e.getUint8(n)!==2)return null;n+=1;let r=e.getUint16(n,!1);n+=2;{let t=new Int8Array(e.buffer.slice(n,n+r)),i=this.textDecoder?.decode(t)||``;if(n+=r,i!==`onMetaData`)return{name:i}}let i=this.getAmfType(e,n);return n+=1,this.getAMFValue(e,n,i).value};parseAudio=(e,t,n)=>{let r=t,i=e.getUint8(r),a=i>>4&15,o=i>>2&3,s=i>>1&1,l=i&1;r+=1;let u=e.getUint8(r);r+=1;let d=n-(r-t);if(d<=0)return{soundFormat:a,soundRate:o,soundSize:s,soundType:l,accPacketType:u,data:new Uint8Array};let f=new Uint8Array(e.buffer.slice(r,r+d));if(a===10&&u===0){let t=e.getUint8(r),n=e.getUint8(r+1),i=t>>3&31,d=(t&7)<<1|n>>7;return{soundFormat:a,soundRate:o,soundSize:s,soundType:l,accPacketType:u,data:f,samplingFrequencyIndex:d,channelConfiguration:n>>3&15,codec:`mp4a.40.${i}`,sampleRate:c[d]}}return{soundFormat:a,soundRate:o,soundSize:s,soundType:l,accPacketType:u,data:f}};parseVideo=(e,t,r)=>{let i=t,a=e.getUint8(i),s=a>>4&15,c=a&15;i+=1;let l=e.getUint8(i);i+=1;let u=o(e,i);i+=3;let d=r-5;if(d<0)return null;let f=new Uint8Array(e.buffer.slice(i,i+d));switch(c){case 7:if(l===0)return{frameType:s,codecID:c,avcPacketType:l,cts:u,data:f,...n(f)};if(l===1){let t=[],n=i+d;for(;!(i+4>n);){let r=e.getUint32(i,!1);if(r<=0||i+4+r>n)break;let a=new Uint8Array(e.buffer.slice(i,i+4+r));i+=4+r,t.push(a)}return{frameType:s,codecID:c,avcPacketType:l,cts:u,data:f,nalus:t}}break;default:return null}return{frameType:s,codecID:c,avcPacketType:l,cts:u,data:f}};getAmfType=(e,t)=>e.getUint8(t);getAMFName=(e,t,n)=>{let r=new Uint8Array(e.buffer.slice(t,t+n));return this.textDecoder?.decode(r)||``};getAMFValue=(e,t,n)=>{let r=t,i,a=0;switch(n){case 0:i=e.getFloat64(r,!1),a=8;break;case 1:i=!!e.getUint8(r),a=1;break;case 2:{i=``;let t=e.getUint16(r,!1);r+=2;let n=new Int8Array(e.buffer,r,t).filter(e=>e!==0);i=(this.textDecoder?.decode(n)||``).trim(),a=2+t}break;case 3:for(i={};r<e.byteLength;){let t=e.getUint16(r,!1);if(t===0)break;r+=2;let n=this.getAMFName(e,r,t);r+=t;let o=this.getAmfType(e,r);if(o===6)break;r+=1;let s=this.getAMFValue(e,r,o);r+=s.length,i[n]=s.value,a=2+t+1+s.length}break;case 8:{i={};let t=e.getUint32(r,!1);r+=4;for(let n=0;n<t;n++){let t=e.getUint16(r,!1);r+=2;let n=this.getAMFName(e,r,t);r+=t;let o=this.getAmfType(e,r);r+=1;let s=this.getAMFValue(e,r,o);r+=s.length,i[n]=s.value,a=2+t+1+s.length}}break;case 10:{i=[];let t=e.getUint32(r,!1);r+=4;for(let n=0;n<t;n++){let t=this.getAmfType(e,r);r+=1;let n=this.getAMFValue(e,r,t);r+=n.length,i.push(n.value),a=1+n.length}}break}return{amfType:n,length:a,value:i}}};let u=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],d=e=>{let t=`unknown`;switch(e){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};var f=class{pat;pmt;audioConfig;videoConfig;payloadMap=new Map;on={};constructor(){}parse=async e=>{let t=0;for(;!(t+188>e.byteLength);){if(e.getInt8(t)!=71){t++;continue}await this.parsePacket(e,t),t+=188}return t};parsePacket=async(e,t)=>{if(t+188>e.byteLength||e.getUint8(t)!==71)throw Error(`Invalid TS packet`);let n=t,r=this.parseHeader(e,n);n+=4;let{transport_error_indicator:i,pid:a,payload_unit_start_indicator:o,adaptation_field_control:s}=r;if(i===1||a===void 0)return;let c=184;if(s===2||s===3){let t=e.getUint8(n);n+=1,t>0&&(this.parseAdaptationField(e,n),n+=t),c-=1+t}if(s===1||s===3){if(c<=0)return;let t=new Uint8Array(e.buffer.slice(n,n+c));if(a===0)return this.parsePAT(e,n);{let{programs:t=[]}=this.pat||{};if(t.find(e=>e.pmt_pid===a))return this.parsePMT(e,n)}let{streams:r=[]}=this.pmt||{},i=r.find(e=>e.elementary_pid===a);if(i){if(o===1){let e=this.payloadMap.get(a);if(e){switch(i.kind){case`audio`:{let t=await this.parseAudio(e);this.on.chunk&&this.on.chunk(t)}break;case`video`:{let t=await this.parseVideo(e);this.on.chunk&&this.on.chunk(t),await new Promise(e=>setTimeout(()=>e(!0),0))}break}this.payloadMap.delete(a)}}{this.payloadMap.has(a)||this.payloadMap.set(a,new Uint8Array);let e=this.payloadMap.get(a),n=new Uint8Array(e.byteLength+t.byteLength);n.set(e,0),n.set(t,e.byteLength),this.payloadMap.set(a,n)}}}};parseHeader=(e,t)=>{let n=t,r=e.getUint8(n),i=e.getUint8(n+1),a=e.getUint8(n+2),o=e.getUint8(n+3),s=(i&128)>>7;return s===1?{sync_byte:r,transport_error_indicator:s}:{sync_byte:r,transport_error_indicator:s,payload_unit_start_indicator:(i&64)>>6,transport_priority:(i&32)>>5,pid:(i&31)<<8|a,transport_scrambling_control:(o&192)>>6,adaptation_field_control:o>>4&3,continuity_counter:o&15}};parsePAT=(e,t)=>{let n=t,r;{let t=e.getUint8(n);n+=1;let i=e.getUint8(n);if(n+=1,i!==0)throw Error(`Invalid PAT table_id`);let a=e.getUint16(n)&4095;n+=2;let o=e.getUint16(n);n+=2;let s=(e.getUint8(n)&62)>>1,c=e.getUint8(n)&1;n+=1;let l=e.getUint8(n);n+=1;let u=e.getUint8(n);n+=1,r={pointer_field:t,table_id:i,section_length:a,transport_stream_id:o,version_number:s,current_next_indicator:c,section_number:l,last_section_number:u}}let i=[];{let t=r.section_length-5-4,a=n+t;for(;n<a;){let t=e.getUint16(n),r=e.getUint16(n+2)&8191;n+=4,t!==0&&r>=32&&r<=8190&&i.push({program_number:t,pmt_pid:r})}}let a=e.getUint32(n);this.pat={header:r,programs:i,crc32:a}};parsePMT=(e,t)=>{let n=t,r;{let t=e.getUint8(n);n+=1;let i=e.getUint8(n);if(n+=1,i!==2)throw Error(`Invalid PMT table_id`);let a=e.getUint16(n)&4095;n+=2;let o=e.getUint16(n);n+=2;let s=(e.getUint8(n)&62)>>1,c=e.getUint8(n)&1;n+=1;let l=e.getUint8(n);n+=1;let u=e.getUint8(n);n+=1;let d=e.getUint16(n)&8191;n+=2;let f=e.getUint16(n)&4095;n+=2,r={pointer_field:t,table_id:i,section_length:a,transport_stream_id:o,version_number:s,current_next_indicator:c,section_number:l,last_section_number:u,pcr_pid:d,program_info_length:f}}n+=r.program_info_length;let i=[];{let t=r.section_length-9-4,a=n+t;for(;n<a;){let t=e.getUint8(n),r=d(t),a=e.getUint16(n+1)&8191,o=e.getUint16(n+3)&4095;if(n+=5+o,a<32||a>8190){console.warn(`Invalid elementary_pid: 0x${a.toString(16)}`);continue}i.push({kind:r,stream_type:t,elementary_pid:a,es_info_length:o})}}let a=e.getUint32(n);this.pmt={header:r,streams:i,crc32:a}};parseAdaptationField=(e,t)=>{let n=t,r,i,a,o,s=e.getUint8(n),c=!!(s&128),l=!!(s&64),u=!!(s&32),d=!!(s&16),f=!!(s&8),p=!!(s&4),m=!!(s&2),h=!!(s&1);n+=1;let g=(e,t)=>{let n=BigInt(0);n|=BigInt(e.getUint16(t))<<25n,n|=BigInt(e.getUint16(t+1))<<17n,n|=BigInt(e.getUint16(t+2))<<9n,n|=BigInt(e.getUint16(t+3))<<1n,n|=BigInt(e.getUint16(t+4)>>7);let r=(e.getUint16(t+4)&1)<<8|e.getUint16(t+5);return n=n*300n+BigInt(r),n};if(d&&(r=g(e,n),n+=6),f&&(i=g(e,n),n+=6),p&&(a=e.getInt8(n),n+=1),m){let t=e.getUint8(n);n+=1,o=new Uint8Array(e.buffer,n,t),n+=t}return{discontinuity_indicator:c,random_access_indicator:l,elementary_stream_priority_indicator:u,pcr_flag:d,opcr_flag:f,splicing_point_flag:p,transport_private_data_flag:m,adaptation_field_extension_flag:h,pcr:r,opcr:i,splice_countdown:a,transport_private_data:o}};parseAudio=async e=>{let t=new DataView(e.buffer),n=0,r,i;{let e=t.getUint8(n)===0&&t.getUint8(n+1)===0&&t.getUint8(n+2)===1;if(n+=3,!e)throw Error(`invalid ts audio payload.`);let i=t.getUint8(n);n+=1;let a=t.getUint8(n)<<8|t.getUint8(n+1);n+=2;let o,s,c,l,u;{let e=t.getUint8(n);n+=1,o=e>>4&3,s=(e>>3&1)==1,c=(e>>2&1)==1,l=(e>>1&1)==1,u=(e&1)==1}let d,f;{let e=t.getUint8(n);n+=1;let r=e>>6,i=t.getUint8(n);n+=1,(r&2)==2&&(d=this.parsePtsDts(t,n)),f=(r&1)==1?this.parsePtsDts(t,n+5):d,n+=i}r={stream_id:i,pes_packet_length:a,scrambling_control:o,priority:s,data_alignment:c,copyright:l,original_copy:u,pts:d,dts:f}}i=e.slice(n);{let e=this.parseAdts(i);!this.audioConfig&&e&&(this.audioConfig={kind:`audio`,codec:e.codec,sampleRate:e.sampleRate,numberOfChannels:e.channelConfiguration},this.on.config&&this.on.config(this.audioConfig));let{dts:t=0,pts:n=0}=r;return{kind:`audio`,type:`key`,dts:t,pts:n,cts:n-t,data:e?i.slice(e.headerLength):i}}};parseAdts=e=>{if(e.length<7||e[0]!==255||(e[1]&240)!=240)return null;let t=e[1]&1?7:9;if(e.length<t)return null;let n=(e[2]>>6&3)+1,r=e[2]>>2&15,i=(e[2]&1)<<2|e[3]>>6,a=u[r]??44100;return{headerLength:t,codec:`mp4a.40.${n}`,sampleRate:a,channelConfiguration:i}};parseVideo=async e=>{let r=new DataView(e.buffer),a=0,o,s;{let e=r.getUint8(a)===0&&r.getUint8(a+1)===0&&r.getUint8(a+2)===1;if(a+=3,!e)throw Error(`invalid ts video payload.`);let t=r.getUint8(a);a+=1;let n=r.getUint8(a)<<8|r.getUint8(a+1);a+=2;let i,s,c,l,u;{let e=r.getUint8(a);a+=1,i=e>>4&3,s=(e>>3&1)==1,c=(e>>2&1)==1,l=(e>>1&1)==1,u=(e&1)==1}let d,f;{let e=r.getUint8(a);a+=1;let t=e>>6,n=r.getUint8(a);a+=1,(t&2)==2&&(d=this.parsePtsDts(r,a)),f=(t&1)==1?this.parsePtsDts(r,a+5):d,a+=n}o={stream_id:t,pes_packet_length:n,scrambling_control:i,priority:s,data_alignment:c,copyright:l,original_copy:u,pts:d,dts:f}}s=e.slice(a);{let e=this.getNalus(s);if(!this.videoConfig){let r,i;if(r=e.find(e=>e.type===7)?.nalu.slice(4),i=e.find(e=>e.type===8)?.nalu.slice(4),r&&i){let e=t(r,i),{codec:a}=n(e);this.videoConfig={kind:`video`,codec:a,description:e,sps:r,pps:i},this.on.config&&this.on.config(this.videoConfig)}}let r=[],a=`delta`;for(let t of e){let{type:e,nalu:n}=t;switch(e){case 6:case 9:r.push(n);break;case 1:a=`delta`,r.push(n);break;case 5:a=`key`,r.push(n);break}}let c=i(r),{dts:l=0,pts:u=0}=o,d=u-l;return{kind:`video`,type:a,dts:l,pts:u,cts:d,data:c,nalus:r}}};parsePtsDts(e,t){let n=e.getUint8(t),r=e.getUint8(t+1),i=e.getUint8(t+2),a=e.getUint8(t+3),o=e.getUint8(t+4),s=(BigInt(n)&14n)<<29n|(BigInt(r)&255n)<<22n|(BigInt(i)&254n)<<14n|(BigInt(a)&255n)<<7n|(BigInt(o)&254n)>>1n;return Number(s)/90}getNalus=e=>{let t=[],n=0;for(;n<e.byteLength-2;){let i=this.findAnnexBStart(e,n);if(!i)break;let a=e[i.naluOffset]&31,o=this.findAnnexBStart(e,i.nextSearchOffset),s=(o?o.startOffset:e.byteLength)-i.naluOffset;if(s>0){let n=e.slice(i.naluOffset,i.naluOffset+s);t.push({type:a,nalu:r(n)})}n=o?o.startOffset:e.byteLength}return t};findAnnexBStart=(e,t)=>{for(let n=t;n<e.length-2;n++)if(!(e[n]!==0||e[n+1]!==0)){if(e[n+2]===1)return{startOffset:n,naluOffset:n+3,nextSearchOffset:n+3};if(n+3<e.length&&e[n+2]===0&&e[n+3]===1)return{startOffset:n,naluOffset:n+4,nextSearchOffset:n+4}}return null}};let p=(e,t)=>String.fromCharCode(e.getUint8(t),e.getUint8(t+1),e.getUint8(t+2),e.getUint8(t+3)),m=(e,t,n)=>{if(t+8>n)return null;let r=e.getUint32(t,!1),i=p(e,t+4),a=8;if(r===1){if(t+16>n)return null;r=Number(e.getBigUint64(t+8,!1)),a=16}else r===0&&(r=n-t);return r<a||t+r>n?null:{offset:t,size:r,type:i,headerSize:a,contentStart:t+a}},h=(e,t,n,r)=>{let i=t;for(;i<n;){let t=m(e,i,n);if(!t)break;r(t),i+=t.size}},g=(e,t,n,r)=>{let i=null;return h(e,t,n,t=>{if(t.type===r)i=t;else if(t.type!==`mdat`){let n=g(e,t.contentStart,t.offset+t.size,r);n&&(i=n)}}),i},_=(e,t,n,r)=>{for(let i=t;i+8<=n;i++){if(p(e,i+4)!==r)continue;let t=m(e,i,n);if(t?.type===r)return t}return null},v=(e,t,n,r,i=86)=>_(e,t+i,n,r)||_(e,t+8,n,r),y=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],b=(e,t)=>{let n=0,r=t;for(;r<e.length;){if(n=n<<7|e[r]&127,!(e[r]&128))return{len:n,next:r+1};r++}return{len:0,next:t}};var x=class{audioConfig;videoConfig;tracks=new Map;mdatBox=null;progressiveParsed=!1;on={};getDiscardOffset=(e,t)=>this.progressiveParsed||g(t,0,e,`moof`)||this.tracks.size>0&&!g(t,0,e,`mdat`)?e:0;constructor(){}parse=async e=>{let t=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),n=t.byteLength,r=0;for(;r<n;){let i=m(e,r,n);if(!i)return 0;switch(i.type){case`moov`:this.parseMoov(e,i),this.tryRunProgressive(e,t);break;case`moof`:this.parseMoof(e,i,t);break;case`mdat`:this.mdatBox=i,this.tryRunProgressive(e,t);break;case`sidx`:case`styp`:case`free`:case`skip`:case`ftyp`:break;default:break}r+=i.size,(i.type===`moov`||i.type===`moof`||i.type===`mdat`)&&await new Promise(e=>setTimeout(()=>e(!0),8))}return this.progressiveParsed?n:0};tryRunProgressive=(e,t)=>{if(this.progressiveParsed||this.tracks.size===0)return;let n=this.mdatBox??g(e,0,t.byteLength,`mdat`),r=g(e,0,t.byteLength,`moov`);!n||!r||this.isProgressiveReady(n,r,t.byteLength)&&(this.parseProgressive(e,n,t),this.progressiveParsed=!0)};isProgressiveReady=(e,t,n)=>e.offset+e.size<=n&&t.offset+t.size<=n;parseMoov=(e,t)=>{h(e,t.contentStart,t.offset+t.size,t=>{t.type===`trak`&&this.parseTrak(e,t)});let n=g(e,t.contentStart,t.offset+t.size,`mvex`);n&&h(e,n.contentStart,n.offset+n.size,t=>{if(t.type!==`trex`)return;let n=e.getUint32(t.contentStart+4,!1),r=this.tracks.get(n);r&&(r.defaultSampleDuration=e.getUint32(t.contentStart+12,!1),r.defaultSampleSize=e.getUint32(t.contentStart+16,!1))});let r=g(e,t.contentStart,t.offset+t.size,`mvhd`);if(r){let t=e.getUint8(r.contentStart),n=e.getUint32(r.contentStart+(t===1?20:12),!1),i=t===1?Number(e.getBigUint64(r.contentStart+24,!1)):e.getUint32(r.contentStart+16,!1);this.on.info?.({width:[...this.tracks.values()].find(e=>e.kind===`video`)?.width,height:[...this.tracks.values()].find(e=>e.kind===`video`)?.height,duration:n?i/n:void 0})}};readTkhdSize=(e,t)=>{let n=e.getUint8(t.contentStart),r=n===1?72:64,i=n===1?76:68;return{width:e.getUint32(t.contentStart+r,!1)>>16,height:e.getUint32(t.contentStart+i,!1)>>16}};readSampleEntrySize=(e,t)=>({width:e.getUint16(t+32,!1),height:e.getUint16(t+34,!1)});parseTrak=(e,t)=>{let r=g(e,t.contentStart,t.offset+t.size,`tkhd`),i=g(e,t.contentStart,t.offset+t.size,`mdia`);if(!r||!i)return;let a=e.getUint8(r.contentStart),o=e.getUint32(r.contentStart+(a===1?20:12),!1),{width:s,height:c}=this.readTkhdSize(e,r),l=g(e,i.contentStart,i.offset+i.size,`hdlr`),u=g(e,i.contentStart,i.offset+i.size,`mdhd`);if(!l||!u)return;let d=p(e,l.contentStart+8),f=d===`vide`?`video`:d===`soun`?`audio`:void 0;if(!f)return;let m=e.getUint8(u.contentStart),h=e.getUint32(u.contentStart+(m===1?20:12),!1);this.tracks.set(o,{kind:f,trackId:o,timescale:h,width:s,height:c});let _=g(e,i.contentStart,i.offset+i.size,`minf`),y=_&&g(e,_.contentStart,_.offset+_.size,`stbl`),b=y&&g(e,y.contentStart,y.offset+y.size,`stsd`);if(!b)return;let x=b.contentStart+8,S=x+e.getUint32(x,!1),C=p(e,x+4);if(f===`video`&&(C===`avc1`||C===`avc3`)){let t=this.readSampleEntrySize(e,x);t.width&&t.height&&(s=t.width,c=t.height);let r=v(e,x,S,`avcC`,86);if(!r)return;let i=new Uint8Array(e.buffer,r.contentStart,r.size-r.headerSize),a=n(i),o=a.codec.replace(/^avc1\\./,`${C}.`);this.videoConfig={kind:`video`,codec:o,description:i,sps:a.sps,pps:a.pps},this.on.config?.(this.videoConfig)}if(f===`video`){let e=this.tracks.get(o);e&&(e.width=s,e.height=c)}if(f===`audio`&&C===`mp4a`){let t=v(e,x,S,`esds`,36),n=t&&this.parseEsds(e,t);n&&(this.audioConfig=n,this.on.config?.(n))}};parseEsds=(e,t)=>{let n=new Uint8Array(e.buffer,t.contentStart,t.size-t.headerSize);for(let e=0;e<n.length-2;e++){if(n[e]!==5)continue;let{len:t,next:r}=b(n,e+1);if(t<2||r+t>n.length)continue;let i=n.slice(r,r+t),a=i[0],o=i[1],s=a>>3&31,c=(a&7)<<1|o>>7,l=o>>3&15;return{kind:`audio`,codec:`mp4a.40.${s}`,sampleRate:y[c]??44100,numberOfChannels:l,description:i}}return null};findMdatForMoof=(e,t,n)=>{let r=t.offset+t.size;for(;r+8<=n;){let t=m(e,r,n);if(!t||t.type===`moof`)break;if(t.type===`mdat`)return t;r+=t.size}return null};parseMoof=(e,t,n)=>{let r=this.findMdatForMoof(e,t,n.byteLength);r&&h(e,t.contentStart,t.offset+t.size,i=>{i.type===`traf`&&this.parseTraf(e,i,t.offset,r,n)})};ticksToMs=(e,t)=>e/t*1e3;readCttsOffsets=(e,t,n)=>{let r=new Int32Array(n);if(!t||n===0)return r;let i=e.getUint32(t.contentStart+4,!1),a=0,o=0,s=0,c=t.contentStart+8;for(let t=0;t<n;t++)o===0&&a<i&&(o=e.getUint32(c,!1),s=e.getInt32(c+4,!1),c+=8,a++),r[t]=s,--o;return r};parseTraf=(e,t,n,r,i)=>{let a=g(e,t.contentStart,t.offset+t.size,`tfhd`),o=g(e,t.contentStart,t.offset+t.size,`tfdt`),s=g(e,t.contentStart,t.offset+t.size,`trun`);if(!a||!o||!s)return;let c=e.getUint32(a.contentStart+4,!1),l=this.tracks.get(c);if(!l)return;let u=e.getUint32(a.contentStart,!1)&16777215,d=(u&131072)!=0,f=a.contentStart+8,p=null,m=0,h=0;u&1&&(p=Number(e.getBigUint64(f,!1)),f+=8),u&2&&(f+=4),u&8&&(m=e.getUint32(f,!1),f+=4),u&16&&(h=e.getUint32(f,!1),f+=4),u&32&&(f+=4),u&8||(m=l.defaultSampleDuration??0),u&16||(h=l.defaultSampleSize??0);let _=e.getUint8(o.contentStart)===1?Number(e.getBigUint64(o.contentStart+4,!1)):e.getUint32(o.contentStart+4,!1),v=e.getUint32(s.contentStart,!1)&16777215,y=s.offset+s.size,b=s.contentStart+4;if(b+4>y)return;let x=e.getUint32(b,!1);if(b+=4,x===0||x>1e4)return;let S=0;if(v&1){if(b+4>y)return;S=e.getInt32(b,!1),b+=4}v&4&&(b+=4);let C;C=d?n:p===null?r.offset:p;let w=C+(v&1?S:0),T=_;for(let t=0;t<x;t++){let t=m,n=h,r=0;if(v&256){if(b+4>y)break;t=e.getUint32(b,!1),b+=4}if(v&512){if(b+4>y)break;n=e.getUint32(b,!1),b+=4}if(v&1024){if(b+4>y)break;r=e.getUint32(b,!1),b+=4}let a=0;if(v&2048){if(b+4>y)break;a=e.getInt32(b,!1),b+=4}if(t<=0&&l.kind===`audio`){let e=this.audioConfig?.sampleRate??l.timescale;t=Math.round(1024*l.timescale/e)}if(n<=0)continue;if(w+n>i.byteLength)break;let o=i.slice(w,w+n);w+=n;let s=this.ticksToMs(T,l.timescale),c=this.ticksToMs(T+a,l.timescale),u=c-s,d=l.kind!==`video`||!(r>>16&1)||this.isAvccKeyFrame(o)?`key`:`delta`;if(l.kind===`video`){let e=this.splitAvccNalus(o);this.on.chunk?.({kind:`video`,type:d,dts:s,pts:c,cts:u,data:o,nalus:e})}else this.on.chunk?.({kind:`audio`,type:`key`,dts:s,pts:c,cts:u,data:o});T+=t}};parseProgressive=(e,t,n)=>{let r=g(e,0,n.byteLength,`moov`);if(!r)return;let i=[];h(e,r.contentStart,r.offset+r.size,t=>{if(t.type!==`trak`)return;let r=this.collectProgressiveTrackSamples(e,t,n);i.push(...r)}),i.sort((e,t)=>e.dts-t.dts||(e.kind===`video`?-1:1));for(let e of i)this.on.chunk?.(e)};collectProgressiveTrackSamples=(e,t,n)=>{let r=[],i=g(e,t.contentStart,t.offset+t.size,`tkhd`),a=g(e,t.contentStart,t.offset+t.size,`mdia`);if(!i||!a)return r;let o=e.getUint32(i.contentStart+(e.getUint8(i.contentStart)===1?20:12),!1),s=this.tracks.get(o);if(!s)return r;let c=g(e,a.contentStart,a.offset+a.size,`minf`),l=c&&g(e,c.contentStart,c.offset+c.size,`stbl`);if(!l)return r;let u=g(e,l.contentStart,l.offset+l.size,`stsz`),d=g(e,l.contentStart,l.offset+l.size,`stco`),f=g(e,l.contentStart,l.offset+l.size,`co64`),p=g(e,l.contentStart,l.offset+l.size,`stsc`),m=g(e,l.contentStart,l.offset+l.size,`stts`),h=g(e,l.contentStart,l.offset+l.size,`stss`),_=g(e,l.contentStart,l.offset+l.size,`ctts`);if(!u||!d&&!f||!m)return r;let v=e.getUint32(u.contentStart+4,!1),y=e.getUint32(u.contentStart+8,!1),b=this.readCttsOffsets(e,_,y),x=d?e.getUint32(d.contentStart+4,!1):e.getUint32(f.contentStart+4,!1),S=t=>d?e.getUint32(d.contentStart+8+(t-1)*4,!1):Number(e.getBigUint64(f.contentStart+8+(t-1)*8,!1)),C=new Set;if(h){let t=e.getUint32(h.contentStart+4,!1);for(let n=0;n<t;n++)C.add(e.getUint32(h.contentStart+8+n*4,!1))}let w=[];if(p){let t=e.getUint32(p.contentStart+4,!1);for(let n=0;n<t;n++){let t=p.contentStart+8+n*12;w.push({firstChunk:e.getUint32(t,!1),samplesPerChunk:e.getUint32(t+4,!1)})}}let T=e=>{if(w.length===0)return y===x?1:Math.max(1,Math.ceil(y/x));let t=w[0].samplesPerChunk;for(let n of w)if(n.firstChunk<=e)t=n.samplesPerChunk;else break;return t},E=0,D=m.contentStart+8,O=e.getUint32(m.contentStart+4,!1),k=0,A=O>0?e.getUint32(D,!1):0,j=O>0?e.getUint32(D+4,!1):0;D+=8;let M=1;for(let t=1;t<=x&&M<=y;t++){let i=T(t),a=S(t);for(let t=0;t<i&&M<=y;t++,M++){let t=v===0?e.getUint32(u.contentStart+12+(M-1)*4,!1):v;if(a+t>n.byteLength)return r;let i=n.slice(a,a+t);a+=t;let o=this.ticksToMs(E,s.timescale),c=b[M-1]??0,l=this.ticksToMs(E+c,s.timescale),d=l-o,f=s.kind===`video`?this.isAvccKeyFrame(i)||C.has(M)||C.size===0:!0;s.kind===`video`?r.push({kind:`video`,type:f?`key`:`delta`,dts:o,pts:l,cts:d,data:i,nalus:this.splitAvccNalus(i)}):r.push({kind:`audio`,type:`key`,dts:o,pts:l,cts:d,data:i}),E+=j,--A,A===0&&k+1<O&&(k+=1,D=m.contentStart+8+k*8,A=e.getUint32(D,!1),j=e.getUint32(D+4,!1))}}return r};isAvccKeyFrame=e=>{let t=0,n=new DataView(e.buffer,e.byteOffset,e.byteLength);for(;t+4<=e.byteLength;){let r=n.getUint32(t,!1);if(r<=0||t+4+r>e.byteLength)break;if((e[t+4]&31)==5)return!0;t+=4+r}return!1};splitAvccNalus=e=>{let t=[],n=0,r=new DataView(e.buffer,e.byteOffset,e.byteLength);for(;n+4<=e.byteLength;){let i=r.getUint32(n,!1);if(i<=0||n+4+i>e.byteLength)break;t.push(e.slice(n,n+4+i)),n+=4+i}return t}};let S=new class{pattern;cacher=new e;isParseing=!1;offset=0;parser;on={};constructor(){}init=e=>{switch(this.destroy(),this.pattern=e,this.pattern){case`flv`:this.parser=new l;break;case`hls`:this.parser=new f;break;case`dash`:case`mp4`:this.parser=new x;break;default:throw Error(`is error pattern.`)}this.parser.on.info=e=>this.on.info&&this.on.info(e),this.parser.on.config=e=>this.on.config&&this.on.config(e),this.parser.on.chunk=e=>{this.cacher.pushChunk(e),this.on.chunk&&this.on.chunk(e)}};push=e=>{this.cacher.push(e),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 Error(`You need to set the pattern.`);if(!this.parser)throw Error(`You need to init parser.`);for(;;){let e=this.cacher.next(this.offset);if(this.offset=0,!e)break;try{let t=await this.parser.parse(e);if(this.pattern===`mp4`||this.pattern===`dash`){let t=this.parser;this.offset=t.getDiscardOffset(e.byteLength,e)}else this.offset=t}catch{this.cacher.discardPayload();continue}}this.isParseing=!1}catch{this.isParseing=!1,this.cacher.discardPayload()}}};S.on.info=e=>postMessage({action:`on.info`,data:e}),S.on.config=e=>postMessage({action:`on.config`,data:e}),S.on.chunk=e=>postMessage({action:`on.chunk`,data:e}),onmessage=e=>{let{action:t,data:n}=e.data,r=S[t];r&&r(n)}})();", t = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", e], { type: "text/javascript;charset=utf-8" });
3
3
  function n(n) {
4
4
  let r;
5
5
  try {
@@ -30,9 +30,6 @@ var r = "pr-player-demuxer", i = class {
30
30
  case "on.chunk":
31
31
  this.on.chunk && this.on.chunk(n);
32
32
  break;
33
- case "on.debug":
34
- this.on.debug && this.on.debug(n);
35
- break;
36
33
  }
37
34
  };
38
35
  }
@@ -50,7 +47,7 @@ var r = "pr-player-demuxer", i = class {
50
47
  data: {}
51
48
  }), this.worker.terminate();
52
49
  };
53
- }, a = "(function(){let e=new class{pattern=`flv`;audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;baseTime=0;pendingChunks=[];currentChunk;isProcessing=!1;decodeTimer=0;frameTrack=!1;isFrameTrack=!1;fameTrackOption={flv:[30,50],hls:[200,300],dash:[50,100],rtmp:[50,100]};decodingSpeedRatio=1;maxDecodingSpeedRatio=2;decodingSpeed=16;fps=0;firstVideoChunkTimestamp;secondVideoChunkTimestamp;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:e=>{let t=this.decodingSpeedRatio;this.on.audio.decode&&this.on.audio.decode({audioData:e,playbackRate:t})},error:e=>{this.on.audio.error&&this.on.audio.error(e)}}),this.audioDecoder.configure(this.audioDecoderConfig)};initVideo=e=>{this.video.destroy(),this.videoDecoderConfig={...e},this.videoDecoder=new VideoDecoder({output:async e=>{this.frameStartTime||=e.timestamp;try{let t=e.timestamp-this.frameStartTime+this.baseTime*1e3,n=await createImageBitmap(e);if(e.close(),!n.width||!n.height)return n.close();this.on.video.decode&&this.on.video.decode({timestamp:t,bitmap:n}),this.currentChunk&&this.currentChunk.kind===`video`&&this.currentChunk.nalus&&this.on.nalus&&this.on.nalus(this.currentChunk.nalus)}catch{e.close()}},error:e=>{this.on.video.error&&this.on.video.error(e)}}),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(),this.pendingChunks=[],this.firstVideoChunkTimestamp=void 0,this.secondVideoChunkTimestamp=void 0,this.frameStartTime=void 0,this.lastRenderTime=void 0,this.nextRenderTime=void 0,clearInterval(this.decodeTimer)};initDecodeInterval=()=>{let e=this.decodingSpeed/this.decodingSpeedRatio,t=this.baseTime+performance.now();if(this.lastRenderTime||=t,this.fps=Math.round(1e3/(t-this.lastRenderTime)),this.lastRenderTime=t,this.nextRenderTime){let 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(),this.currentChunk;){let e=this.pendingChunks.length;if(this.frameTrack){let[t,n]=this.fameTrackOption[this.pattern];if(e<=t&&(this.isFrameTrack=!1),e>=n&&(this.isFrameTrack=!0),this.isFrameTrack){let n=Math.min(1+(e-t)/100,this.maxDecodingSpeedRatio);this.decodingSpeedRatio=Number(n.toFixed(1))}else this.decodingSpeedRatio=1}if(this.on.analysis){let{decodingSpeed:t,decodingSpeedRatio:n,fps:r}=this;this.on.analysis({decodingSpeed:t,decodingSpeedRatio:n,fps:r,cacheLength:e})}let{kind:t,init:n}=this.currentChunk;switch(t){case`audio`:this.decodeAudio(n);break;case`video`:this.decodeVideo(n);break}if(t===`video`)break}this.isProcessing=!1}};decodeAudio=e=>{if(!this.audioDecoder)return;let t=new EncodedAudioChunk(e);this.audioDecoder.decode(t)};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)){let t=new EncodedVideoChunk(e);this.videoDecoder.decode(t)}};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}}};e.on.audio.decode=e=>postMessage({action:`on.audio.decode`,data:e},[e.audioData]),e.on.audio.error=e=>postMessage({action:`on.audio.error`,data:e}),e.on.video.decode=e=>postMessage({action:`on.video.decode`,data:e},[e.bitmap]),e.on.video.error=e=>postMessage({action:`on.video.error`,data:e}),e.on.nalus=e=>postMessage({action:`on.nalus`,data:e}),e.on.analysis=e=>postMessage({action:`on.analysis`,data:e}),onmessage=t=>{let{action:n,data:r}=t.data,i=e[n];i&&i(r)}})();", o = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", a], { type: "text/javascript;charset=utf-8" });
50
+ }, a = "(function(){let e=new class{pattern=`flv`;audioDecoderConfig;audioDecoder;videoDecoderConfig;videoDecoder;hasKeyFrame=!1;baseTime=0;pendingChunks=[];currentChunk;isProcessing=!1;decodeTimer=0;frameTrack=!1;isFrameTrack=!1;fameTrackOption={flv:[30,50],hls:[200,300],dash:[50,100],mp4:[30,50],rtmp:[50,100]};decodingSpeedRatio=1;maxDecodingSpeedRatio=2;decodingSpeed=16;fps=0;firstVideoChunkTimestamp;secondVideoChunkTimestamp;frameStartTime;lastRenderTime;nextRenderTime;on={audio:{},video:{}};constructor(){}init=e=>{this.destroy(),this.pattern=e,this.baseTime=new Date().getTime()-performance.now(),this.initDecodeInterval()};initAudio=async e=>{if(this.audio.destroy(),!(await AudioDecoder.isConfigSupported(e).catch(()=>({supported:!1}))).supported){console.warn(`[Decoder] audio config not supported:`,e.codec,`audio will be skipped`),this.audioDecoderConfig=void 0,this.audioDecoder=void 0;return}this.audioDecoderConfig={...e},this.audioDecoder=new AudioDecoder({output:e=>{let t=this.decodingSpeedRatio;this.on.audio.decode&&this.on.audio.decode({audioData:e,playbackRate:t})},error:e=>{this.on.audio.error&&this.on.audio.error(e)}}),this.audioDecoder.configure(this.audioDecoderConfig)};initVideo=e=>{this.video.destroy(),this.videoDecoderConfig={...e},this.videoDecoder=new VideoDecoder({output:e=>{this.frameStartTime||=e.timestamp;try{let t=e.timestamp-this.frameStartTime+this.baseTime*1e3,n=new VideoFrame(e,{timestamp:t});if(e.close(),!n.displayWidth||!n.displayHeight){n.close();return}this.on.video.decode?.({timestamp:t,frame:n}),this.currentChunk?.kind===`video`&&this.currentChunk.nalus&&this.on.nalus?.(this.currentChunk.nalus)}catch{e.close()}},error:e=>{this.on.video.error&&this.on.video.error(e)}}),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(),this.pendingChunks=[],this.firstVideoChunkTimestamp=void 0,this.secondVideoChunkTimestamp=void 0,this.frameStartTime=void 0,this.lastRenderTime=void 0,this.nextRenderTime=void 0,clearInterval(this.decodeTimer)};initDecodeInterval=()=>{let e=this.decodingSpeed/this.decodingSpeedRatio,t=this.baseTime+performance.now();if(this.lastRenderTime||=t,this.fps=Math.round(1e3/(t-this.lastRenderTime)),this.lastRenderTime=t,this.nextRenderTime){let t=this.lastRenderTime-this.nextRenderTime;e-=t}this.nextRenderTime=this.lastRenderTime+e,this.decodeTimer=setTimeout(()=>{try{this.decode()}catch(e){console.error(`[Decoder] decode error:`,e)}this.initDecodeInterval()},e)};decode=()=>{if(this.isProcessing!==!0){for(this.isProcessing=!0;this.currentChunk=this.pendingChunks.shift(),this.currentChunk;){let e=this.pendingChunks.length;if(this.frameTrack){let[t,n]=this.fameTrackOption[this.pattern];if(e<=t&&(this.isFrameTrack=!1),e>=n&&(this.isFrameTrack=!0),this.isFrameTrack){let n=Math.min(1+(e-t)/100,this.maxDecodingSpeedRatio);this.decodingSpeedRatio=Number(n.toFixed(1))}else this.decodingSpeedRatio=1}if(this.on.analysis){let{decodingSpeed:t,decodingSpeedRatio:n,fps:r}=this;this.on.analysis({decodingSpeed:t,decodingSpeedRatio:n,fps:r,cacheLength:e})}let{kind:t,init:n}=this.currentChunk;switch(t){case`audio`:this.decodeAudio(n);break;case`video`:this.decodeVideo(n);break}if(t===`video`)break}this.isProcessing=!1}};decodeAudio=e=>{if(this.audioDecoder)try{let t=new EncodedAudioChunk(e);this.audioDecoder.decode(t)}catch(e){console.warn(`[Decoder] audio decode error (AudioDecoder may be unusable):`,e)}};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)){let t=new EncodedVideoChunk(e);this.videoDecoder.decode(t)}};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}}};e.on.audio.decode=e=>postMessage({action:`on.audio.decode`,data:e},[e.audioData]),e.on.audio.error=e=>postMessage({action:`on.audio.error`,data:e}),e.on.video.decode=e=>postMessage({action:`on.video.decode`,data:e},[e.frame]),e.on.video.error=e=>postMessage({action:`on.video.error`,data:e}),e.on.nalus=e=>postMessage({action:`on.nalus`,data:e}),e.on.analysis=e=>postMessage({action:`on.analysis`,data:e}),onmessage=t=>{let{action:n,data:r}=t.data,i=e[n];i&&i(r)}})();", o = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", a], { type: "text/javascript;charset=utf-8" });
54
51
  function s(e) {
55
52
  let t;
56
53
  try {
@@ -119,7 +116,7 @@ var c = "pr-player-decoder", l = class {
119
116
  destroy = () => {
120
117
  this.worker.postMessage({ action: "destroy" }), this.worker.terminate();
121
118
  };
122
- }, u = "(function(){let e=new class{renderMap=new Map;constructor(){}push=async e=>{let{timestamp:t}=e,{bitmap:n}=e;try{let e=[...this.renderMap.keys()];for(let r of e){let e=this.renderMap.get(r);if(!e)continue;let{pause:i=!1,writer:a,offscreen:o,option:s}=e;if(i===!0)continue;let c=async e=>{if(a){let n=new VideoFrame(e,{timestamp:t});try{await a.write(n)}catch{}n.close()}else o&&(o.width=e.width,o.height=e.height,o?.getContext(`2d`)?.drawImage(e,0,0))};if(r===`default`)await c(n);else if(s){let{sx:e=0,sy:t=0,sw:r=n.width,sh:i=n.height}=s;await c(await createImageBitmap(n,e,t,r,i))}}}catch{}n.close()};addCut=e=>{let{key:t,writable:n,offscreen:r,option:i}={key:`default`,...e};if(n){let e=n.getWriter();this.renderMap.set(t,{writer:e,option:i,pause:!1})}else this.renderMap.set(t,{offscreen:r,option:i,pause:!1})};delCut=e=>{this.renderMap.delete(e)};setCut=e=>{let{key:t,cutOption:n}={key:`default`,...e},r=this.renderMap.get(t);r&&this.renderMap.set(t,{...r,option:n})};setPause=e=>{let{key:t,pause:n}={key:`default`,...e},r=this.renderMap.get(t);r&&this.renderMap.set(t,{...r,pause:n})};destroy=()=>{this.renderMap=new Map}};onmessage=t=>{let{action:n,data:r}=t.data,i=e[n];i&&i(r)}})();", d = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", u], { type: "text/javascript;charset=utf-8" });
119
+ }, u = "(function(){let e=new class{renderMap=new Map;constructor(){}push=async e=>{let t=e.frame,n=t.timestamp;try{let e=[...this.renderMap.entries()].filter(([,e])=>e&&!e.pause);if(e.length===1&&e[0][0]===\"default\"&&e[0][1].writer){e[0][1].writer.write(t).catch(()=>{}),t.close();return}for(let[r,i]of e){let{writer:e,offscreen:a,option:o}=i;if(e){let i;if(r===\"default\"||!o)i=new VideoFrame(t,{timestamp:n});else{let{sx:e=0,sy:r=0,sw:a=t.displayWidth,sh:s=t.displayHeight}=o;i=new VideoFrame(t,{visibleRect:{x:e,y:r,width:a,height:s},displayWidth:a,displayHeight:s,timestamp:n})}e.write(i).catch(()=>{}),i.close()}else if(a)if(r===\"default\"||!o)a.width=t.displayWidth,a.height=t.displayHeight,a.getContext(`2d`)?.drawImage(t,0,0);else{let{sx:e=0,sy:n=0,sw:r=t.displayWidth,sh:i=t.displayHeight}=o;a.width=r,a.height=i,a.getContext(`2d`)?.drawImage(t,e,n,r,i,0,0,r,i)}}}catch{}t.close()};addCut=e=>{let{key:t,writable:n,offscreen:r,option:i}={key:`default`,...e};if(n){let e=n.getWriter();this.renderMap.set(t,{writer:e,option:i,pause:!1})}else this.renderMap.set(t,{offscreen:r,option:i,pause:!1})};delCut=e=>{this.renderMap.delete(e)};setCut=e=>{let{key:t,option:n}={key:`default`,...e},r=this.renderMap.get(t);r&&this.renderMap.set(t,{...r,option:n})};setPause=e=>{let{key:t,pause:n}={key:`default`,...e},r=this.renderMap.get(t);r&&this.renderMap.set(t,{...r,pause:n})};destroy=()=>{this.renderMap=new Map}};onmessage=t=>{let{action:n,data:r}=t.data,i=e[n];i&&i(r)}})();", d = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", u], { type: "text/javascript;charset=utf-8" });
123
120
  function f(e) {
124
121
  let t;
125
122
  try {
@@ -140,7 +137,7 @@ var p = "pr-player-render", m = class {
140
137
  push = (e) => this.worker.postMessage({
141
138
  action: "push",
142
139
  data: e
143
- }, [e.bitmap]);
140
+ }, [e.frame]);
144
141
  addCut = (e) => {
145
142
  let t = [];
146
143
  e.writable && t.push(e.writable), e.offscreen && t.push(e.offscreen), this.worker.postMessage({
@@ -343,8 +340,8 @@ var p = "pr-player-render", m = class {
343
340
  i.buffer = r, i.playbackRate.value = n;
344
341
  let a = -1200 * Math.log2(n);
345
342
  i.detune.value = a, i.connect(this.destination);
346
- let o = Math.max(this.nextStartTime, this.audioContext.currentTime);
347
- this.nextStartTime = o + r.duration / n, i.start(o), this.pendingSources.push(i), i.onended = () => {
343
+ let o = Math.max(this.nextStartTime, this.audioContext.currentTime), s = r.duration / n;
344
+ this.nextStartTime = o + s, i.start(o), this.pendingSources.push(i), i.onended = () => {
348
345
  this.pendingSources = this.pendingSources.filter((e) => e !== i);
349
346
  }, this.audioContext.state === "suspended" && await this.audioContext.resume();
350
347
  }
@@ -354,7 +351,7 @@ var p = "pr-player-render", m = class {
354
351
  }
355
352
  }, b = (e) => {
356
353
  let t = e.toLowerCase();
357
- return t.includes(".m3u8") || t.includes("hls") || t.includes("master.m3u8") || t.match(/index\d*\.m3u8/) ? "hls" : t.includes(".mpd") || t.includes("dash") ? "dash" : t.startsWith("rtmp://") || t.startsWith("rtmps://") ? "rtmp" : t.includes(".flv") || t.includes("flv") && !t.includes("flash") ? "flv" : "unknown";
354
+ return t.includes(".m3u8") || t.includes("hls") || t.includes("master.m3u8") || t.match(/index\d*\.m3u8/) ? "hls" : t.includes(".mpd") || t.includes("dash") ? "dash" : t.includes(".mp4") || t.includes(".m4s") ? "mp4" : t.startsWith("rtmp://") || t.startsWith("rtmps://") ? "rtmp" : t.includes(".flv") || t.includes("flv") && !t.includes("flash") ? "flv" : "unknown";
358
355
  }, x = (e) => {
359
356
  let t = e?.getTracks() || [];
360
357
  for (let e of t) e.stop();
@@ -394,7 +391,7 @@ var p = "pr-player-render", m = class {
394
391
  let n = new Uint8Array(e), r = new Uint8Array(t), i = new Uint8Array(11 + n.length + r.length), a = 0;
395
392
  return i[a++] = 1, i[a++] = n[1], i[a++] = n[2], i[a++] = n[3], i[a++] = 255, i[a++] = 225, i[a++] = n.length >> 8 & 255, i[a++] = n.length & 255, i.set(n, a), a += n.length, i[a++] = 1, i[a++] = r.length >> 8 & 255, i[a++] = r.length & 255, i.set(r, a), i;
396
393
  }, w = (e) => {
397
- let t = 0, n = new DataView(e.buffer), r = n.getUint8(t);
394
+ let t = 0, n = new DataView(e.buffer, e.byteOffset, e.byteLength), r = n.getUint8(t);
398
395
  if (t += 1, r !== 1) throw Error("Invalid AVC version");
399
396
  let i = n.getUint8(t) & 255;
400
397
  t += 1;
@@ -406,32 +403,21 @@ var p = "pr-player-render", m = class {
406
403
  i,
407
404
  a,
408
405
  o
409
- ], (e) => e.toString(16).padStart(2, "0")).join("")}`, c = (n.getUint8(t) & 3) - 1;
410
- t += 1;
411
- let l = n.getUint8(t) & 31;
412
- t += 1;
413
- let u = n.getUint16(t, !1);
406
+ ], (e) => e.toString(16).padStart(2, "0")).join("")}`;
407
+ t += 1, t += 1;
408
+ let c = n.getUint16(t, !1);
414
409
  t += 2;
415
- let d = new Uint8Array(n.buffer.slice(t, t + u));
416
- t += u;
417
- let f = n.getUint8(t) & 31;
418
- t += 1;
419
- let p = n.getUint16(t, !1);
420
- t += 2;
421
- let m = new Uint8Array(n.buffer.slice(t, t + p));
422
- return t += p, {
410
+ let l = new Uint8Array(e.buffer, e.byteOffset + t, c);
411
+ t += c, t += 1;
412
+ let u = n.getUint16(t, !1);
413
+ return t += 2, {
423
414
  version: r,
424
415
  codec: s,
425
416
  profile: i,
426
417
  compatibility: a,
427
418
  level: o,
428
- lengthSizeMinusOne: c,
429
- numOfSequenceParameterSets: l,
430
- sequenceParameterSetLength: u,
431
- sps: d,
432
- numOfPictureParameterSets: f,
433
- pictureParameterSetLength: p,
434
- pps: m
419
+ sps: l,
420
+ pps: new Uint8Array(e.buffer, e.byteOffset + t, u)
435
421
  };
436
422
  }, T = (e) => {
437
423
  let t = new Uint8Array(4 + e.length);
@@ -440,32 +426,104 @@ var p = "pr-player-render", m = class {
440
426
  let t = 0;
441
427
  for (let n of e) t += n.length;
442
428
  let n = new Uint8Array(t), r = 0;
443
- for (let t of e) {
444
- let e = t;
445
- n.set(e, r), r += e.length;
446
- }
429
+ for (let t of e) n.set(t, r), r += t.length;
447
430
  return n;
448
431
  }, D = (e) => {
449
- let t = new DataView(e.buffer), n = 0, r, i, a;
450
- r = t.getUint32(n, !1), n += 4;
451
- {
452
- let e = t.getUint8(n);
453
- i = {
454
- forbidden_zero_bit: e >> 7 & 1,
455
- nal_ref_idc: e >> 5 & 3,
456
- nal_unit_type: e & 31
457
- }, n += 1;
432
+ let t = new DataView(e.buffer, e.byteOffset, e.byteLength), n = 0, r = t.getUint32(n, !1);
433
+ n += 4;
434
+ let i = t.getUint8(n), a = {
435
+ forbidden_zero_bit: i >> 7 & 1,
436
+ nal_ref_idc: i >> 5 & 3,
437
+ nal_unit_type: i & 31
438
+ };
439
+ return n += 1, {
440
+ size: r,
441
+ header: a,
442
+ data: new Uint8Array(e.buffer, e.byteOffset + n, r - 1)
443
+ };
444
+ }, O = (e, t) => e.match(RegExp(`${t}="([^"]*)"`, "i"))?.[1], k = (e) => {
445
+ let [t, n] = e.split("-").map((e) => parseInt(e, 10));
446
+ return {
447
+ start: t,
448
+ end: n
449
+ };
450
+ }, A = (e) => {
451
+ if (!e) return;
452
+ let t = e.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:([\d.]+)S)?/i);
453
+ if (!t) return;
454
+ let n = parseInt(t[1] || "0", 10), r = parseInt(t[2] || "0", 10), i = parseFloat(t[3] || "0");
455
+ return n * 3600 + r * 60 + i;
456
+ }, j = (e, t) => {
457
+ if (!t) return e;
458
+ if (/^https?:\/\//i.test(t)) return t;
459
+ if (t.startsWith("/")) {
460
+ let n = e.match(/^(https?:\/\/[^/]+)/i)?.[1];
461
+ return n ? `${n}${t}` : t;
458
462
  }
459
- {
460
- let e = r - 1;
461
- a = new Uint8Array(t.buffer.slice(n, n + e));
463
+ return e + t;
464
+ }, M = (e, t) => e.replace(/\$(RepresentationID|\w+)(?:%0(\d+)d)?\$/g, (e, n, r) => {
465
+ let i = t[n] ?? t[n.toLowerCase()] ?? "";
466
+ return r ? String(i).padStart(Number(r), "0") : String(i);
467
+ }), N = (e, t, n) => j(e, M(t, n)), P = (e, t) => {
468
+ let n = e.match(/<SegmentList([^>]*)>([\s\S]*?)<\/SegmentList>/i) || t.match(/<SegmentList([^>]*)>([\s\S]*?)<\/SegmentList>/i);
469
+ if (!n) return;
470
+ let r = n[1], i = n[2], a = i.match(/<Initialization[^>]*range="([^"]+)"/i), o = [], s = /<SegmentURL[^>]*mediaRange="([^"]+)"/gi, c;
471
+ for (; c = s.exec(i);) o.push(k(c[1]));
472
+ if (!(o.length === 0 && !a)) return {
473
+ timescale: parseInt(O(r, "timescale") || "1000", 10),
474
+ duration: parseInt(O(r, "duration") || "0", 10),
475
+ initRange: a ? k(a[1]) : void 0,
476
+ segments: o
477
+ };
478
+ }, F = (e, t) => {
479
+ let n = e.match(/<SegmentTemplate([^>]*)\/>/i)?.[1] || e.match(/<SegmentTemplate([^>]*)>/i)?.[1] || t.match(/<SegmentTemplate([^>]*)\/>/i)?.[1] || t.match(/<SegmentTemplate([^>]*)>/i)?.[1] || "";
480
+ if (!n) {
481
+ let t = e.match(/<SegmentURL[^>]*initialization="([^"]*)"[^>]*media="([^"]*)"/i);
482
+ return t ? {
483
+ initialization: t[1],
484
+ media: t[2],
485
+ timescale: 1e3,
486
+ duration: 0,
487
+ startNumber: 1
488
+ } : void 0;
462
489
  }
463
490
  return {
464
- size: r,
465
- header: i,
466
- data: a
491
+ initialization: O(n, "initialization"),
492
+ media: O(n, "media"),
493
+ timescale: parseInt(O(n, "timescale") || "1000", 10),
494
+ duration: parseInt(O(n, "duration") || "0", 10),
495
+ startNumber: parseInt(O(n, "startNumber") || "1", 10)
496
+ };
497
+ }, I = (e, t) => {
498
+ let n = t.substring(0, t.lastIndexOf("/") + 1), r = e.match(/<MPD[^>]*>/i)?.[0] || "", i = (O(r, "type") || "static") === "dynamic", a = A(O(r, "mediaPresentationDuration")), o = [], s = /<AdaptationSet([^>]*)>([\s\S]*?)<\/AdaptationSet>/gi, c;
499
+ for (; c = s.exec(e);) {
500
+ let e = c[1], t = c[2], n = O(e, "mimeType") || "", r = O(e, "contentType") || "", i = n.includes("video") || r === "video" || /<ContentComponent[^>]*contentType="video"/i.test(t), a = n.includes("audio") || r === "audio" || /<ContentComponent[^>]*contentType="audio"/i.test(t), s;
501
+ if (i && a ? s = "mux" : i ? s = "video" : a && (s = "audio"), !s) continue;
502
+ let l = t.match(/<Representation([^>]*)(?:\/>|>([\s\S]*?)<\/Representation>)/i);
503
+ if (!l) continue;
504
+ let u = l[1], d = l[2] || "", f = O(u, "id") || "0", p = O(u, "codecs") || "", m = parseInt(O(u, "bandwidth") || "0", 10), h = parseInt(O(u, "width") || "0", 10) || void 0, g = parseInt(O(u, "height") || "0", 10) || void 0, _ = d.match(/<BaseURL>([^<]+)<\/BaseURL>/i)?.[1]?.trim(), v = P(d, t), y = v ? void 0 : F(d, t);
505
+ !v && (!y || !y.media && !y.initialization) || o.push({
506
+ kind: s,
507
+ mimeType: n || (i ? "video/mp4" : "audio/mp4"),
508
+ representation: {
509
+ id: f,
510
+ codecs: p,
511
+ bandwidth: m,
512
+ width: h,
513
+ height: g,
514
+ baseUrl: _,
515
+ template: y,
516
+ segmentList: v
517
+ }
518
+ });
519
+ }
520
+ return {
521
+ isLive: i,
522
+ duration: a,
523
+ baseUrl: n,
524
+ adaptations: o
467
525
  };
468
- }, O = class {
526
+ }, L = class {
469
527
  #e = {
470
528
  timeout: 5 * 1e3,
471
529
  check: !1
@@ -524,14 +582,11 @@ var p = "pr-player-render", m = class {
524
582
  e.name = "AbortError", this.#t.abort(e);
525
583
  }
526
584
  };
527
- }, k = class {
528
- option = {
529
- debug: !1,
530
- frameTrack: !1
531
- };
585
+ }, R = class {
586
+ option = { frameTrack: !1 };
532
587
  converter = "MediaStreamTrackGenerator" in window ? "generator" : "canvas";
533
- prFetch = new O();
534
- getSegmentsFetch = new O();
588
+ prFetch = new L();
589
+ getSegmentsFetch = new L();
535
590
  prResolves = new S();
536
591
  url = "";
537
592
  start_resolve;
@@ -547,8 +602,7 @@ var p = "pr-player-render", m = class {
547
602
  cutRenders = /* @__PURE__ */ new Map();
548
603
  trackGenerator;
549
604
  constructor(e = {}) {
550
- let { debug: t = !1 } = e;
551
- this.option.debug = t;
605
+ this.option.frameTrack = e.frameTrack ?? !1;
552
606
  }
553
607
  start = async (e) => {
554
608
  await this.stop(), this.url = e;
@@ -561,11 +615,18 @@ var p = "pr-player-render", m = class {
561
615
  case "hls":
562
616
  this.hls.start();
563
617
  break;
618
+ case "dash":
619
+ this.dash.start();
620
+ break;
621
+ case "mp4":
622
+ this.mp4.start();
623
+ break;
624
+ case "rtmp": throw Error("RTMP is not supported in browser. Please use HTTP-FLV or HLS.");
564
625
  }
565
626
  };
566
627
  stop = async () => {
567
628
  try {
568
- this.url = "", clearInterval(this.hls.getSegmentsTimer), this.prFetch.stop(), this.getSegmentsFetch.stop(), this.demuxerWorker?.destroy(), this.decoderWorker?.destroy(), this.renderWorker?.destroy(), this.cutRenders = /* @__PURE__ */ new Map(), x(this.stream), this.audioPlayer?.destroy();
629
+ this.url = "", clearInterval(this.hls.getSegmentsTimer), clearInterval(this.dash.getSegmentsTimer), this.prFetch.stop(), this.getSegmentsFetch.stop(), this.demuxerWorker?.destroy(), this.decoderWorker?.destroy(), this.renderWorker?.destroy(), this.cutRenders = /* @__PURE__ */ new Map(), x(this.stream), this.audioPlayer?.destroy();
569
630
  } catch (e) {
570
631
  console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: error", e);
571
632
  }
@@ -584,25 +645,27 @@ var p = "pr-player-render", m = class {
584
645
  isReady = () => this.prResolves.add("isReady", () => this.stream?.active === !0);
585
646
  cut = {
586
647
  create: (e, t) => {
648
+ let n = this._getSafeSize(t);
587
649
  if (this.converter === "generator") {
588
- let n = new MediaStreamTrackGenerator({ kind: "video" }), r = new MediaStream([n]);
650
+ let t = new MediaStreamTrackGenerator({ kind: "video" }), r = new MediaStream([t]);
589
651
  return this.renderWorker?.addCut({
590
652
  key: e,
591
- writable: n.writable,
592
- option: t
653
+ writable: t.writable,
654
+ option: n
593
655
  }), this.cutRenders.set(e, { stream: r }), r;
594
656
  }
595
- let n = document.createElement("canvas"), r = n.captureStream(), i = n.transferControlToOffscreen();
657
+ let r = document.createElement("canvas"), i = r.captureStream(), a = r.transferControlToOffscreen();
596
658
  return this.renderWorker?.addCut({
597
659
  key: e,
598
- offscreen: i,
599
- option: t
600
- }), this.cutRenders.set(e, { stream: r }), r;
660
+ offscreen: a,
661
+ option: n
662
+ }), this.cutRenders.set(e, { stream: i }), i;
601
663
  },
602
664
  setCut: (e, t) => {
665
+ let n = this._getSafeSize(t);
603
666
  this.renderWorker?.setCut({
604
667
  key: e,
605
- cutOption: t
668
+ option: n
606
669
  });
607
670
  },
608
671
  getStream: (e) => this.cutRenders.get(e)?.stream,
@@ -620,21 +683,20 @@ var p = "pr-player-render", m = class {
620
683
  this.initRender(), this.initDecoder(e), this.initDemuxer(e), this.audioPlayer = new y(), this.audioPlayer.init();
621
684
  };
622
685
  initDemuxer = (e) => {
623
- this.demuxerWorker = new i(), this.demuxerWorker.init(e), this.demuxerWorker.on.debug = (e) => {
624
- this.option.debug && this.on.debug && this.on.debug(e);
625
- }, this.demuxerWorker.on.info = (e) => {
626
- this.option.debug && console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: info", e), this.on.demuxer.info && this.on.demuxer.info(e);
686
+ this.demuxerWorker = new i(), this.demuxerWorker.init(e), this.demuxerWorker.on.info = (e) => {
687
+ this.on.demuxer.info && this.on.demuxer.info(e);
627
688
  }, this.demuxerWorker.on.config = (e) => {
628
- this.option.debug && console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: config", e), this.on.demuxer.config && this.on.demuxer.config(e);
689
+ this.on.demuxer.config && this.on.demuxer.config(e);
629
690
  let { kind: t } = e;
630
691
  switch (t) {
631
692
  case "audio":
632
693
  {
633
- let { codec: t, sampleRate: n, numberOfChannels: r } = e;
694
+ let { codec: t, sampleRate: n, numberOfChannels: r, description: i } = e;
634
695
  this.decoderWorker?.initAudio({
635
696
  codec: t,
636
697
  sampleRate: n,
637
- numberOfChannels: r
698
+ numberOfChannels: r,
699
+ ...i ? { description: i } : {}
638
700
  });
639
701
  }
640
702
  break;
@@ -685,11 +747,11 @@ var p = "pr-player-render", m = class {
685
747
  initDecoder = (e) => {
686
748
  this.decoderWorker = new l(), this.decoderWorker.init(e);
687
749
  let { frameTrack: t = !1 } = this.option;
688
- this.decoderWorker.setFrameTrack(t), this.decoderWorker.on.audio.decode = (e) => {
750
+ this.decoderWorker.setFrameTrack(t), this.decoderWorker.on.audio.error = (e) => {
751
+ this.on.error && this.on.error(e);
752
+ }, this.decoderWorker.on.audio.decode = (e) => {
689
753
  this.audioPlayer?.push(e), this.on.decoder.audio && this.on.decoder.audio(e);
690
- }, this.decoderWorker.on.audio.error = (e) => {
691
- this.option.debug && console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: audio.error ", e), this.on.error && this.on.error(e);
692
- }, this.decoderWorker.on.video.decode = async (e) => {
754
+ }, this.decoderWorker.on.video.decode = (e) => {
693
755
  this.start_resolve &&= (this.start_resolve(!0), void 0), this.renderWorker?.push(e), this.on.decoder.video && this.on.decoder.video(e);
694
756
  }, this.decoderWorker.on.video.error = (e) => {
695
757
  this.on.error && this.on.error(e);
@@ -806,9 +868,171 @@ var p = "pr-player-render", m = class {
806
868
  }
807
869
  })
808
870
  };
809
- }, A = class {
871
+ mp4 = { start: () => new Promise(async (e, t) => {
872
+ try {
873
+ this.start_resolve = e;
874
+ let n, r = 0;
875
+ for (;;) {
876
+ r += 1;
877
+ try {
878
+ n = await this.prFetch.request(this.url);
879
+ } catch (e) {
880
+ console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
881
+ }
882
+ if (n?.status === 200 || r === 3) break;
883
+ await new Promise((e) => setTimeout(() => e(!0), 500));
884
+ }
885
+ if (!n || n.status !== 200) return t("request is error.");
886
+ let i = n.body?.getReader();
887
+ if (!i) return t("reader is error.");
888
+ for (this.decoderWorker?.setFrameTrack(!1);;) {
889
+ let { done: e, value: t } = await i.read();
890
+ if (t && this.demuxerWorker?.push(t), e || this.url === "") break;
891
+ }
892
+ } catch (e) {
893
+ e.name !== "AbortError" && t(e);
894
+ }
895
+ }) };
896
+ dash = {
897
+ isLive: !1,
898
+ segmentNumber: 1,
899
+ mpdInfo: null,
900
+ getSegmentsTimer: 0,
901
+ fetchSegment: async (e) => {
902
+ let t = await this.prFetch.request(e);
903
+ if (!t || t.status !== 200) return !1;
904
+ let n = t.body?.getReader();
905
+ if (!n) return !1;
906
+ for (;;) {
907
+ let { done: e, value: t } = await n.read();
908
+ if (t && this.demuxerWorker?.push(t), e || this.url === "") break;
909
+ }
910
+ return !0;
911
+ },
912
+ fetchByteRange: async (e, t, n) => {
913
+ let r, i = 0;
914
+ for (;;) {
915
+ i += 1;
916
+ try {
917
+ r = await fetch(e, { headers: { Range: `bytes=${t}-${n}` } });
918
+ } catch (e) {
919
+ console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
920
+ }
921
+ if (r?.ok && (r.status === 200 || r.status === 206) || i === 3) break;
922
+ await new Promise((e) => setTimeout(() => e(!0), 500));
923
+ }
924
+ if (!r?.ok || r.status !== 200 && r.status !== 206) return !1;
925
+ let a = new Uint8Array(await r.arrayBuffer());
926
+ return a.byteLength > 0 && this.demuxerWorker?.push(a), a.byteLength > 0;
927
+ },
928
+ getMpd: async () => {
929
+ try {
930
+ let e, t = 0;
931
+ for (;;) {
932
+ t += 1;
933
+ try {
934
+ e = await this.getSegmentsFetch.request(this.url);
935
+ } catch (e) {
936
+ console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
937
+ }
938
+ if (e?.status === 200 || t === 3) break;
939
+ await new Promise((e) => setTimeout(() => e(!0), 500));
940
+ }
941
+ if (!e || e.status !== 200) throw Error("request is error.");
942
+ let n = e.body?.getReader();
943
+ if (!n) throw Error("reader is error.");
944
+ let r = [];
945
+ for (;;) {
946
+ let { done: e, value: t } = await n.read();
947
+ if (t && r.push(t), e || this.url === "") break;
948
+ }
949
+ let i = r.reduce((e, t) => e + t.byteLength, 0), a = new Uint8Array(i), o = 0;
950
+ for (let e of r) a.set(e, o), o += e.byteLength;
951
+ let s = I(new TextDecoder("utf-8").decode(a), this.url);
952
+ return this.dash.mpdInfo = s, this.dash.isLive = s.isLive, this.dash.isLive || (this.option.frameTrack = !1), s;
953
+ } catch (e) {
954
+ return e.name !== "AbortError" && this.on.error && this.on.error(e), null;
955
+ }
956
+ },
957
+ start: () => new Promise(async (e, t) => {
958
+ try {
959
+ this.start_resolve = e, this.dash.segmentNumber = 1, this.dash.mpdInfo = null;
960
+ let n = await this.dash.getMpd();
961
+ if (!n) return t("mpd parse is error.");
962
+ let r = n.adaptations.find((e) => e.kind === "video" || e.kind === "mux")?.representation;
963
+ if (r?.width && r?.height) {
964
+ let e = {
965
+ width: r.width,
966
+ height: r.height,
967
+ duration: n.duration
968
+ };
969
+ this.on.demuxer.info && this.on.demuxer.info(e);
970
+ }
971
+ let i = n.adaptations.find((e) => e.representation.segmentList);
972
+ if (i?.representation.segmentList) {
973
+ let { segmentList: e, baseUrl: r } = i.representation, a = j(n.baseUrl, r || "");
974
+ if (e.initRange) {
975
+ let { start: n, end: r } = e.initRange;
976
+ if (!await this.dash.fetchByteRange(a, n, r)) return t("dash init segment fetch failed.");
977
+ }
978
+ this.decoderWorker?.setFrameTrack(!1);
979
+ for (let t of e.segments) {
980
+ if (this.url === "") break;
981
+ await this.dash.fetchByteRange(a, t.start, t.end);
982
+ }
983
+ return;
984
+ }
985
+ let a = [
986
+ n.adaptations.find((e) => e.kind === "video"),
987
+ n.adaptations.find((e) => e.kind === "audio"),
988
+ n.adaptations.find((e) => e.kind === "mux")
989
+ ].filter((e) => !!e);
990
+ if (a.length === 0) return t("no adaptation in mpd.");
991
+ for (let e of a) {
992
+ let { id: t, bandwidth: r, template: i } = e.representation;
993
+ if (!i?.initialization) continue;
994
+ let a = N(n.baseUrl, i.initialization, {
995
+ RepresentationID: t,
996
+ Bandwidth: r,
997
+ Number: this.dash.segmentNumber
998
+ });
999
+ await this.dash.fetchSegment(a);
1000
+ }
1001
+ for (this.dash.isLive ? this.dash.getSegmentsTimer = window.setInterval(this.dash.getMpd, 3e3) : this.decoderWorker?.setFrameTrack(!1);;) {
1002
+ let e = this.dash.mpdInfo;
1003
+ if (!e || this.url === "") break;
1004
+ let { baseUrl: t } = e, n = !1;
1005
+ for (let e of a) {
1006
+ let { id: r, bandwidth: i, template: a } = e.representation;
1007
+ if (!a?.media) continue;
1008
+ let o = N(t, a.media, {
1009
+ RepresentationID: r,
1010
+ Bandwidth: i,
1011
+ Number: this.dash.segmentNumber
1012
+ });
1013
+ await this.dash.fetchSegment(o) && (n = !0);
1014
+ }
1015
+ if (!n) {
1016
+ if (this.dash.isLive) {
1017
+ await new Promise((e) => setTimeout(() => e(!0), 300));
1018
+ continue;
1019
+ }
1020
+ break;
1021
+ }
1022
+ this.dash.segmentNumber += 1;
1023
+ }
1024
+ } catch (e) {
1025
+ e.name !== "AbortError" && t(e);
1026
+ }
1027
+ })
1028
+ };
1029
+ _getSafeSize = (e) => {
1030
+ let t = { ...e };
1031
+ return t.sx % 2 != 0 && (t.sx += 1), t.sy % 2 != 0 && (t.sy += 1), t.sw % 2 != 0 && --t.sw, t.sh % 2 != 0 && --t.sh, t;
1032
+ };
1033
+ }, z = class {
810
1034
  pendingPayloads = [];
811
- payload = new Uint8Array();
1035
+ payload = /* @__PURE__ */ new Uint8Array();
812
1036
  chunks = [];
813
1037
  push = (e) => {
814
1038
  this.pendingPayloads.push(e);
@@ -823,119 +1047,477 @@ var p = "pr-player-render", m = class {
823
1047
  pushChunk = (e) => {
824
1048
  this.chunks.push(e), this.chunks.length > 100 && this.chunks.shift();
825
1049
  };
826
- destroy = () => {
827
- this.pendingPayloads = [], this.payload = new Uint8Array(), this.chunks = [];
1050
+ discardPayload = () => {
1051
+ this.payload = /* @__PURE__ */ new Uint8Array();
828
1052
  };
829
- }, j = (e) => {
830
- let t = "unknown";
831
- switch (e) {
832
- case 1:
833
- case 2:
834
- case 27:
835
- case 36:
836
- t = "video";
837
- break;
838
- case 3:
839
- case 4:
840
- case 15:
841
- t = "audio";
842
- break;
843
- case 6:
844
- t = "subtitle";
845
- break;
846
- case 134:
847
- t = "ad";
848
- break;
849
- }
850
- return t;
851
- }, M = class {
852
- pat;
853
- pmt;
1053
+ destroy = () => {
1054
+ this.pendingPayloads = [], this.payload = /* @__PURE__ */ new Uint8Array(), this.chunks = [];
1055
+ };
1056
+ }, B = (e, t) => e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), V = (e, t) => {
1057
+ let n = B(e, t);
1058
+ return n & 8388608 && (n |= -16777216), n;
1059
+ }, H = (e, t) => t << 24 | e, U = [
1060
+ 96e3,
1061
+ 88200,
1062
+ 64e3,
1063
+ 48e3,
1064
+ 44100,
1065
+ 32e3,
1066
+ 24e3,
1067
+ 22050,
1068
+ 16e3,
1069
+ 12e3,
1070
+ 11025,
1071
+ 8e3,
1072
+ 7350
1073
+ ], W = class {
854
1074
  audioConfig;
855
1075
  videoConfig;
856
- payloadMap = /* @__PURE__ */ new Map();
1076
+ header;
1077
+ textDecoder = new TextDecoder("utf-8");
857
1078
  on = {};
858
1079
  constructor() {}
859
1080
  parse = async (e) => {
860
1081
  let t = 0;
861
- for (; !(t + 188 > e.byteLength);) {
862
- if (e.getInt8(t) != 71) {
863
- t++;
864
- continue;
865
- }
866
- await this.parsePacket(e, t), t += 188;
867
- }
868
- return t;
869
- };
870
- parsePacket = async (e, t) => {
871
- if (t + 188 > e.byteLength || e.getUint8(t) !== 71) throw Error("Invalid TS packet");
872
- let n = t, r = this.parseHeader(e, n);
873
- n += 4;
874
- let { transport_error_indicator: i, pid: a, payload_unit_start_indicator: o, adaptation_field_control: s } = r;
875
- if (i === 1 || a === void 0) return;
876
- let c = 184;
877
- if (s === 2 || s === 3) {
878
- let t = e.getUint8(n);
879
- n += 1, this.parseAdaptationField(e, n), n += t, c -= t;
880
- }
881
- if (s === 3 && --c, s === 1 || s === 3) {
882
- let t = new Uint8Array(e.buffer.slice(n, n + c));
883
- if (a === 0) return this.parsePAT(e, n);
884
- {
885
- let { programs: t = [] } = this.pat || {};
886
- if (t.find((e) => e.pmt_pid === a)) return this.parsePMT(e, n);
887
- }
888
- let { streams: r = [] } = this.pmt || {}, i = r.find((e) => e.elementary_pid === a);
889
- if (i) {
890
- if (o === 1) {
891
- let e = this.payloadMap.get(a);
892
- if (e) {
893
- switch (i.kind) {
894
- case "audio":
895
- {
896
- let t = await this.parseAudio(e);
897
- this.on.chunk && this.on.chunk(t);
898
- }
899
- break;
900
- case "video":
901
- {
902
- let t = await this.parseVideo(e);
903
- this.on.chunk && this.on.chunk(t), await new Promise((e) => setTimeout(() => e(!0), 0));
904
- }
905
- break;
906
- }
907
- this.payloadMap.delete(a);
908
- }
1082
+ for (this.header || (this.parseHeader(e, t), t += 9); this.isSurplusTag(e, t) !== !1;) {
1083
+ let { tagType: n, dataSize: r, dts: i } = this.parseTagHeader(e, t + 4);
1084
+ if (n) {
1085
+ let a = this.parseTagBody(n, e, t + 4 + 11, r);
1086
+ if (!a) {
1087
+ t = t + 4 + 11 + r, await new Promise((e) => setTimeout(() => e(!0), 8));
1088
+ continue;
909
1089
  }
910
- {
911
- this.payloadMap.has(a) || this.payloadMap.set(a, new Uint8Array());
912
- let e = this.payloadMap.get(a), n = new Uint8Array(e.byteLength + t.byteLength);
913
- n.set(e, 0), n.set(t, e.byteLength), this.payloadMap.set(a, n);
1090
+ switch (n) {
1091
+ case "script":
1092
+ this.on.info && this.on.info(a);
1093
+ break;
1094
+ case "audio":
1095
+ {
1096
+ let { accPacketType: e } = a;
1097
+ if (e === 0) {
1098
+ let { codec: e, sampleRate: t, channelConfiguration: n, data: r } = a;
1099
+ this.audioConfig = {
1100
+ kind: "audio",
1101
+ codec: e,
1102
+ sampleRate: t,
1103
+ numberOfChannels: n,
1104
+ ...r ? { description: r } : {}
1105
+ }, this.on.config && this.on.config(this.audioConfig);
1106
+ } else {
1107
+ let { cts: e, data: t } = a, n = e === void 0 ? void 0 : e + i;
1108
+ this.on.chunk && this.on.chunk({
1109
+ kind: "audio",
1110
+ type: "key",
1111
+ dts: i,
1112
+ pts: n,
1113
+ cts: e,
1114
+ data: t
1115
+ });
1116
+ }
1117
+ }
1118
+ break;
1119
+ case "video":
1120
+ {
1121
+ let { avcPacketType: e } = a;
1122
+ if (e === 0) {
1123
+ let { codec: e, sps: t, pps: n, data: r } = a;
1124
+ this.videoConfig = {
1125
+ kind: "video",
1126
+ codec: e,
1127
+ description: r,
1128
+ sps: t,
1129
+ pps: n
1130
+ }, this.on.config && this.on.config(this.videoConfig);
1131
+ } else {
1132
+ let { frameType: e, cts: t, data: n, nalus: r } = a, o = e === 1 ? "key" : "delta", s = t === void 0 ? void 0 : t + i;
1133
+ this.on.chunk && this.on.chunk({
1134
+ kind: "video",
1135
+ type: o,
1136
+ dts: i,
1137
+ pts: s,
1138
+ cts: t,
1139
+ data: n,
1140
+ nalus: r
1141
+ });
1142
+ }
1143
+ }
1144
+ break;
914
1145
  }
1146
+ t = t + 4 + 11 + r;
915
1147
  }
1148
+ await new Promise((e) => setTimeout(() => e(!0), 8));
916
1149
  }
1150
+ return t;
917
1151
  };
918
1152
  parseHeader = (e, t) => {
919
- let n = t, r = e.getUint8(n), i = e.getUint8(n + 1), a = e.getUint8(n + 2), o = e.getUint8(n + 3), s = (i & 128) >> 7;
920
- return s === 1 ? {
921
- sync_byte: r,
922
- transport_error_indicator: s
923
- } : {
924
- sync_byte: r,
925
- transport_error_indicator: s,
926
- payload_unit_start_indicator: (i & 64) >> 6,
927
- transport_priority: (i & 32) >> 5,
928
- pid: (i & 31) << 8 | a,
929
- transport_scrambling_control: (o & 192) >> 6,
930
- adaptation_field_control: o >> 4 & 3,
931
- continuity_counter: o & 15
932
- };
933
- };
934
- parsePAT = (e, t) => {
935
- let n = t, r;
1153
+ let n, r, i, a;
1154
+ n = e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), r = e.getUint8(t + 3);
936
1155
  {
937
- let t = e.getUint8(n);
938
- n += 1;
1156
+ let n = e.getUint8(t + 4);
1157
+ i = {
1158
+ audio: !!(n & 4),
1159
+ video: !!(n & 1)
1160
+ };
1161
+ }
1162
+ a = e.getUint32(t + 5), this.header = {
1163
+ signature: n,
1164
+ version: r,
1165
+ flags: i,
1166
+ dataOffset: a
1167
+ };
1168
+ };
1169
+ isSurplusTag = (e, t) => {
1170
+ let n = !0, r = e.byteLength;
1171
+ if (t + 4 > r) n = !1;
1172
+ else if (t + 4 + 11 > r) n = !1;
1173
+ else {
1174
+ let i = B(e, t + 4 + 1);
1175
+ t + 4 + 11 + i > r && (n = !1);
1176
+ }
1177
+ return n;
1178
+ };
1179
+ parseTagHeader = (e, t) => {
1180
+ let n, r, i, a, o;
1181
+ {
1182
+ let r = e.getUint8(t), i;
1183
+ switch (r) {
1184
+ case 18:
1185
+ i = "script";
1186
+ break;
1187
+ case 8:
1188
+ i = "audio";
1189
+ break;
1190
+ case 9:
1191
+ i = "video";
1192
+ break;
1193
+ }
1194
+ n = i;
1195
+ }
1196
+ r = B(e, t + 1), i = B(e, t + 4), a = e.getUint8(t + 7), o = B(e, t + 8);
1197
+ let s = H(i, a);
1198
+ return {
1199
+ tagType: n,
1200
+ dataSize: r,
1201
+ timestamp: i,
1202
+ timestampExtended: a,
1203
+ dts: s,
1204
+ streamID: o
1205
+ };
1206
+ };
1207
+ parseTagBody = (e, t, n, r) => {
1208
+ let i;
1209
+ switch (e) {
1210
+ case "script":
1211
+ i = this.parseMetaData(t, n);
1212
+ break;
1213
+ case "audio":
1214
+ i = this.parseAudio(t, n, r);
1215
+ break;
1216
+ case "video":
1217
+ i = this.parseVideo(t, n, r);
1218
+ break;
1219
+ }
1220
+ return i;
1221
+ };
1222
+ parseMetaData = (e, t) => {
1223
+ let n = t;
1224
+ if (e.getUint8(n) !== 2) return null;
1225
+ n += 1;
1226
+ let r = e.getUint16(n, !1);
1227
+ n += 2;
1228
+ {
1229
+ let t = new Int8Array(e.buffer.slice(n, n + r)), i = this.textDecoder?.decode(t) || "";
1230
+ if (n += r, i !== "onMetaData") return { name: i };
1231
+ }
1232
+ let i = this.getAmfType(e, n);
1233
+ return n += 1, this.getAMFValue(e, n, i).value;
1234
+ };
1235
+ parseAudio = (e, t, n) => {
1236
+ let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i >> 2 & 3, s = i >> 1 & 1, c = i & 1;
1237
+ r += 1;
1238
+ let l = e.getUint8(r);
1239
+ r += 1;
1240
+ let u = n - (r - t);
1241
+ if (u <= 0) return {
1242
+ soundFormat: a,
1243
+ soundRate: o,
1244
+ soundSize: s,
1245
+ soundType: c,
1246
+ accPacketType: l,
1247
+ data: /* @__PURE__ */ new Uint8Array()
1248
+ };
1249
+ let d = new Uint8Array(e.buffer.slice(r, r + u));
1250
+ if (a === 10 && l === 0) {
1251
+ let t = e.getUint8(r), n = e.getUint8(r + 1), i = t >> 3 & 31, u = (t & 7) << 1 | n >> 7;
1252
+ return {
1253
+ soundFormat: a,
1254
+ soundRate: o,
1255
+ soundSize: s,
1256
+ soundType: c,
1257
+ accPacketType: l,
1258
+ data: d,
1259
+ samplingFrequencyIndex: u,
1260
+ channelConfiguration: n >> 3 & 15,
1261
+ codec: `mp4a.40.${i}`,
1262
+ sampleRate: U[u]
1263
+ };
1264
+ }
1265
+ return {
1266
+ soundFormat: a,
1267
+ soundRate: o,
1268
+ soundSize: s,
1269
+ soundType: c,
1270
+ accPacketType: l,
1271
+ data: d
1272
+ };
1273
+ };
1274
+ parseVideo = (e, t, n) => {
1275
+ let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i & 15;
1276
+ r += 1;
1277
+ let s = e.getUint8(r);
1278
+ r += 1;
1279
+ let c = V(e, r);
1280
+ r += 3;
1281
+ let l = n - 5;
1282
+ if (l < 0) return null;
1283
+ let u = new Uint8Array(e.buffer.slice(r, r + l));
1284
+ switch (o) {
1285
+ case 7:
1286
+ if (s === 0) return {
1287
+ frameType: a,
1288
+ codecID: o,
1289
+ avcPacketType: s,
1290
+ cts: c,
1291
+ data: u,
1292
+ ...w(u)
1293
+ };
1294
+ if (s === 1) {
1295
+ let t = [], n = r + l;
1296
+ for (; !(r + 4 > n);) {
1297
+ let i = e.getUint32(r, !1);
1298
+ if (i <= 0 || r + 4 + i > n) break;
1299
+ let a = new Uint8Array(e.buffer.slice(r, r + 4 + i));
1300
+ r += 4 + i, t.push(a);
1301
+ }
1302
+ return {
1303
+ frameType: a,
1304
+ codecID: o,
1305
+ avcPacketType: s,
1306
+ cts: c,
1307
+ data: u,
1308
+ nalus: t
1309
+ };
1310
+ }
1311
+ break;
1312
+ default: return null;
1313
+ }
1314
+ return {
1315
+ frameType: a,
1316
+ codecID: o,
1317
+ avcPacketType: s,
1318
+ cts: c,
1319
+ data: u
1320
+ };
1321
+ };
1322
+ getAmfType = (e, t) => e.getUint8(t);
1323
+ getAMFName = (e, t, n) => {
1324
+ let r = new Uint8Array(e.buffer.slice(t, t + n));
1325
+ return this.textDecoder?.decode(r) || "";
1326
+ };
1327
+ getAMFValue = (e, t, n) => {
1328
+ let r = t, i, a = 0;
1329
+ switch (n) {
1330
+ case 0:
1331
+ i = e.getFloat64(r, !1), a = 8;
1332
+ break;
1333
+ case 1:
1334
+ i = !!e.getUint8(r), a = 1;
1335
+ break;
1336
+ case 2:
1337
+ {
1338
+ i = "";
1339
+ let t = e.getUint16(r, !1);
1340
+ r += 2;
1341
+ let n = new Int8Array(e.buffer, r, t).filter((e) => e !== 0);
1342
+ i = (this.textDecoder?.decode(n) || "").trim(), a = 2 + t;
1343
+ }
1344
+ break;
1345
+ case 3:
1346
+ for (i = {}; r < e.byteLength;) {
1347
+ let t = e.getUint16(r, !1);
1348
+ if (t === 0) break;
1349
+ r += 2;
1350
+ let n = this.getAMFName(e, r, t);
1351
+ r += t;
1352
+ let o = this.getAmfType(e, r);
1353
+ if (o === 6) break;
1354
+ r += 1;
1355
+ let s = this.getAMFValue(e, r, o);
1356
+ r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
1357
+ }
1358
+ break;
1359
+ case 8:
1360
+ {
1361
+ i = {};
1362
+ let t = e.getUint32(r, !1);
1363
+ r += 4;
1364
+ for (let n = 0; n < t; n++) {
1365
+ let t = e.getUint16(r, !1);
1366
+ r += 2;
1367
+ let n = this.getAMFName(e, r, t);
1368
+ r += t;
1369
+ let o = this.getAmfType(e, r);
1370
+ r += 1;
1371
+ let s = this.getAMFValue(e, r, o);
1372
+ r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
1373
+ }
1374
+ }
1375
+ break;
1376
+ case 10:
1377
+ {
1378
+ i = [];
1379
+ let t = e.getUint32(r, !1);
1380
+ r += 4;
1381
+ for (let n = 0; n < t; n++) {
1382
+ let t = this.getAmfType(e, r);
1383
+ r += 1;
1384
+ let n = this.getAMFValue(e, r, t);
1385
+ r += n.length, i.push(n.value), a = 1 + n.length;
1386
+ }
1387
+ }
1388
+ break;
1389
+ }
1390
+ return {
1391
+ amfType: n,
1392
+ length: a,
1393
+ value: i
1394
+ };
1395
+ };
1396
+ }, G = [
1397
+ 96e3,
1398
+ 88200,
1399
+ 64e3,
1400
+ 48e3,
1401
+ 44100,
1402
+ 32e3,
1403
+ 24e3,
1404
+ 22050,
1405
+ 16e3,
1406
+ 12e3,
1407
+ 11025,
1408
+ 8e3,
1409
+ 7350
1410
+ ], K = (e) => {
1411
+ let t = "unknown";
1412
+ switch (e) {
1413
+ case 1:
1414
+ case 2:
1415
+ case 27:
1416
+ case 36:
1417
+ t = "video";
1418
+ break;
1419
+ case 3:
1420
+ case 4:
1421
+ case 15:
1422
+ t = "audio";
1423
+ break;
1424
+ case 6:
1425
+ t = "subtitle";
1426
+ break;
1427
+ case 134:
1428
+ t = "ad";
1429
+ break;
1430
+ }
1431
+ return t;
1432
+ }, q = class {
1433
+ pat;
1434
+ pmt;
1435
+ audioConfig;
1436
+ videoConfig;
1437
+ payloadMap = /* @__PURE__ */ new Map();
1438
+ on = {};
1439
+ constructor() {}
1440
+ parse = async (e) => {
1441
+ let t = 0;
1442
+ for (; !(t + 188 > e.byteLength);) {
1443
+ if (e.getInt8(t) != 71) {
1444
+ t++;
1445
+ continue;
1446
+ }
1447
+ await this.parsePacket(e, t), t += 188;
1448
+ }
1449
+ return t;
1450
+ };
1451
+ parsePacket = async (e, t) => {
1452
+ if (t + 188 > e.byteLength || e.getUint8(t) !== 71) throw Error("Invalid TS packet");
1453
+ let n = t, r = this.parseHeader(e, n);
1454
+ n += 4;
1455
+ let { transport_error_indicator: i, pid: a, payload_unit_start_indicator: o, adaptation_field_control: s } = r;
1456
+ if (i === 1 || a === void 0) return;
1457
+ let c = 184;
1458
+ if (s === 2 || s === 3) {
1459
+ let t = e.getUint8(n);
1460
+ n += 1, t > 0 && (this.parseAdaptationField(e, n), n += t), c -= 1 + t;
1461
+ }
1462
+ if (s === 1 || s === 3) {
1463
+ if (c <= 0) return;
1464
+ let t = new Uint8Array(e.buffer.slice(n, n + c));
1465
+ if (a === 0) return this.parsePAT(e, n);
1466
+ {
1467
+ let { programs: t = [] } = this.pat || {};
1468
+ if (t.find((e) => e.pmt_pid === a)) return this.parsePMT(e, n);
1469
+ }
1470
+ let { streams: r = [] } = this.pmt || {}, i = r.find((e) => e.elementary_pid === a);
1471
+ if (i) {
1472
+ if (o === 1) {
1473
+ let e = this.payloadMap.get(a);
1474
+ if (e) {
1475
+ switch (i.kind) {
1476
+ case "audio":
1477
+ {
1478
+ let t = await this.parseAudio(e);
1479
+ this.on.chunk && this.on.chunk(t);
1480
+ }
1481
+ break;
1482
+ case "video":
1483
+ {
1484
+ let t = await this.parseVideo(e);
1485
+ this.on.chunk && this.on.chunk(t), await new Promise((e) => setTimeout(() => e(!0), 0));
1486
+ }
1487
+ break;
1488
+ }
1489
+ this.payloadMap.delete(a);
1490
+ }
1491
+ }
1492
+ {
1493
+ this.payloadMap.has(a) || this.payloadMap.set(a, /* @__PURE__ */ new Uint8Array());
1494
+ let e = this.payloadMap.get(a), n = new Uint8Array(e.byteLength + t.byteLength);
1495
+ n.set(e, 0), n.set(t, e.byteLength), this.payloadMap.set(a, n);
1496
+ }
1497
+ }
1498
+ }
1499
+ };
1500
+ parseHeader = (e, t) => {
1501
+ let n = t, r = e.getUint8(n), i = e.getUint8(n + 1), a = e.getUint8(n + 2), o = e.getUint8(n + 3), s = (i & 128) >> 7;
1502
+ return s === 1 ? {
1503
+ sync_byte: r,
1504
+ transport_error_indicator: s
1505
+ } : {
1506
+ sync_byte: r,
1507
+ transport_error_indicator: s,
1508
+ payload_unit_start_indicator: (i & 64) >> 6,
1509
+ transport_priority: (i & 32) >> 5,
1510
+ pid: (i & 31) << 8 | a,
1511
+ transport_scrambling_control: (o & 192) >> 6,
1512
+ adaptation_field_control: o >> 4 & 3,
1513
+ continuity_counter: o & 15
1514
+ };
1515
+ };
1516
+ parsePAT = (e, t) => {
1517
+ let n = t, r;
1518
+ {
1519
+ let t = e.getUint8(n);
1520
+ n += 1;
939
1521
  let i = e.getUint8(n);
940
1522
  if (n += 1, i !== 0) throw Error("Invalid PAT table_id");
941
1523
  let a = e.getUint16(n) & 4095;
@@ -974,7 +1556,7 @@ var p = "pr-player-render", m = class {
974
1556
  header: r,
975
1557
  programs: i,
976
1558
  crc32: a
977
- }, this.on.debug && this.on.debug({ pat: this.pat });
1559
+ };
978
1560
  };
979
1561
  parsePMT = (e, t) => {
980
1562
  let n = t, r;
@@ -1009,12 +1591,13 @@ var p = "pr-player-render", m = class {
1009
1591
  program_info_length: f
1010
1592
  };
1011
1593
  }
1594
+ n += r.program_info_length;
1012
1595
  let i = [];
1013
1596
  {
1014
1597
  let t = r.section_length - 9 - 4, a = n + t;
1015
1598
  for (; n < a;) {
1016
- let t = e.getUint8(n), r = j(t), a = e.getUint16(n + 1) & 8191, o = e.getUint16(n + 3) & 4095;
1017
- if (n += 5, a < 32 || a > 8190) {
1599
+ let t = e.getUint8(n), r = K(t), a = e.getUint16(n + 1) & 8191, o = e.getUint16(n + 3) & 4095;
1600
+ if (n += 5 + o, a < 32 || a > 8190) {
1018
1601
  console.warn(`Invalid elementary_pid: 0x${a.toString(16)}`);
1019
1602
  continue;
1020
1603
  }
@@ -1031,7 +1614,7 @@ var p = "pr-player-render", m = class {
1031
1614
  header: r,
1032
1615
  streams: i,
1033
1616
  crc32: a
1034
- }, this.on.debug && this.on.debug({ pmt: this.pmt });
1617
+ };
1035
1618
  };
1036
1619
  parseAdaptationField = (e, t) => {
1037
1620
  let n = t, r, i, a, o, s = e.getUint8(n), c = !!(s & 128), l = !!(s & 64), u = !!(s & 32), d = !!(s & 16), f = !!(s & 8), p = !!(s & 4), m = !!(s & 2), h = !!(s & 1);
@@ -1096,40 +1679,36 @@ var p = "pr-player-render", m = class {
1096
1679
  }
1097
1680
  i = e.slice(n);
1098
1681
  {
1099
- if (!this.audioConfig && t.getUint8(n) === 255) {
1100
- let e = t.getUint8(n + 2), r = t.getUint8(n + 3), i, a;
1101
- a = e >> 2 & 15, i = (e & 1) << 2 | r >> 6, this.audioConfig = {
1102
- kind: "audio",
1103
- codec: `mp4a.40.${i}`,
1104
- sampleRate: [
1105
- 96e3,
1106
- 88200,
1107
- 64e3,
1108
- 48e3,
1109
- 44100,
1110
- 32e3,
1111
- 24e3,
1112
- 22050,
1113
- 16e3,
1114
- 12e3,
1115
- 11025,
1116
- 8e3,
1117
- 7350
1118
- ][a],
1119
- numberOfChannels: i
1120
- }, this.on.config && this.on.config(this.audioConfig);
1121
- }
1122
- let { dts: e = 0, pts: a = 0 } = r;
1682
+ let e = this.parseAdts(i);
1683
+ !this.audioConfig && e && (this.audioConfig = {
1684
+ kind: "audio",
1685
+ codec: e.codec,
1686
+ sampleRate: e.sampleRate,
1687
+ numberOfChannels: e.channelConfiguration
1688
+ }, this.on.config && this.on.config(this.audioConfig));
1689
+ let { dts: t = 0, pts: n = 0 } = r;
1123
1690
  return {
1124
1691
  kind: "audio",
1125
1692
  type: "key",
1126
- dts: e,
1127
- pts: a,
1128
- cts: a - e,
1129
- data: i.slice(7)
1693
+ dts: t,
1694
+ pts: n,
1695
+ cts: n - t,
1696
+ data: e ? i.slice(e.headerLength) : i
1130
1697
  };
1131
1698
  }
1132
1699
  };
1700
+ parseAdts = (e) => {
1701
+ if (e.length < 7 || e[0] !== 255 || (e[1] & 240) != 240) return null;
1702
+ let t = e[1] & 1 ? 7 : 9;
1703
+ if (e.length < t) return null;
1704
+ let n = (e[2] >> 6 & 3) + 1, r = e[2] >> 2 & 15, i = (e[2] & 1) << 2 | e[3] >> 6, a = G[r] ?? 44100;
1705
+ return {
1706
+ headerLength: t,
1707
+ codec: `mp4a.40.${n}`,
1708
+ sampleRate: a,
1709
+ channelConfiguration: i
1710
+ };
1711
+ };
1133
1712
  parseVideo = async (e) => {
1134
1713
  let t = new DataView(e.buffer), n = 0, r, i;
1135
1714
  {
@@ -1213,351 +1792,419 @@ var p = "pr-player-render", m = class {
1213
1792
  }
1214
1793
  getNalus = (e) => {
1215
1794
  let t = [], n = 0;
1216
- for (; !(n + 4 > e.byteLength);) {
1217
- if (e[n] !== 0 || e[n + 1] !== 0 || e[n + 2] !== 1) {
1218
- n += 1;
1219
- continue;
1220
- }
1221
- n += 3;
1222
- let r = n, i = e[n] & 31;
1223
- for (n += 1; !(n + 1 > e.byteLength);) {
1224
- if (e[n] !== 0 || e[n + 1] !== 0 || e[n + 2] !== 1) {
1225
- n += 1;
1226
- continue;
1227
- }
1228
- break;
1229
- }
1230
- let a = n - r;
1231
- if (e[n - 1] === 0 && --a, a !== 0) {
1232
- let n = T(e.slice(r, r + a));
1795
+ for (; n < e.byteLength - 2;) {
1796
+ let r = this.findAnnexBStart(e, n);
1797
+ if (!r) break;
1798
+ let i = e[r.naluOffset] & 31, a = this.findAnnexBStart(e, r.nextSearchOffset), o = (a ? a.startOffset : e.byteLength) - r.naluOffset;
1799
+ if (o > 0) {
1800
+ let n = e.slice(r.naluOffset, r.naluOffset + o);
1233
1801
  t.push({
1234
1802
  type: i,
1235
- nalu: n
1803
+ nalu: T(n)
1236
1804
  });
1237
1805
  }
1806
+ n = a ? a.startOffset : e.byteLength;
1238
1807
  }
1239
- return t;
1240
- };
1241
- }, N = (e, t) => e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), P = class {
1242
- audioConfig;
1243
- videoConfig;
1244
- header;
1245
- textDecoder = new TextDecoder("utf-8");
1246
- on = {};
1247
- constructor() {}
1248
- parse = async (e) => {
1249
- let t = 0;
1250
- for (this.header || (this.parseHeader(e, t), t += 9); this.isSurplusTag(e, t) !== !1;) {
1251
- let { tagType: n, dataSize: r, timestamp: i } = this.parseTagHeader(e, t + 4);
1252
- if (n) {
1253
- let a = this.parseTagBody(n, e, t + 4 + 11, r);
1254
- switch (n) {
1255
- case "script":
1256
- this.on.info && this.on.info(a);
1257
- break;
1258
- case "audio":
1259
- {
1260
- let { accPacketType: e } = a;
1261
- if (e === 0) {
1262
- let { codec: e, sampleRate: t, channelConfiguration: n } = a;
1263
- this.audioConfig = {
1264
- kind: "audio",
1265
- codec: e,
1266
- sampleRate: t,
1267
- numberOfChannels: n
1268
- }, this.on.config && this.on.config(this.audioConfig);
1269
- } else {
1270
- let { cts: e, data: t } = a, n = e === void 0 ? void 0 : e + i;
1271
- this.on.chunk && this.on.chunk({
1272
- kind: "audio",
1273
- type: "key",
1274
- dts: i,
1275
- pts: n,
1276
- cts: e,
1277
- data: t
1278
- });
1279
- }
1280
- }
1281
- break;
1282
- case "video":
1283
- {
1284
- let { avcPacketType: e } = a;
1285
- if (e === 0) {
1286
- let { codec: e, sps: t, pps: n, data: r } = a;
1287
- this.videoConfig = {
1288
- kind: "video",
1289
- codec: e,
1290
- description: r,
1291
- sps: t,
1292
- pps: n
1293
- }, this.on.config && this.on.config(this.videoConfig);
1294
- } else {
1295
- let { frameType: e, cts: t, data: n, nalus: r } = a, o = e === 1 ? "key" : "delta", s = t === void 0 ? void 0 : t + i;
1296
- this.on.chunk && this.on.chunk({
1297
- kind: "video",
1298
- type: o,
1299
- dts: i,
1300
- pts: s,
1301
- cts: t,
1302
- data: n,
1303
- nalus: r
1304
- });
1305
- }
1306
- }
1307
- break;
1308
- }
1309
- t = t + 4 + 11 + r;
1310
- }
1311
- await new Promise((e) => setTimeout(() => e(!0), 8));
1312
- }
1313
- return t;
1314
- };
1315
- parseHeader = (e, t) => {
1316
- let n, r, i, a;
1317
- n = e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), r = e.getUint8(3);
1318
- {
1319
- let [, , t, , n] = e.getUint8(0).toString(2).padStart(5, "0").split("");
1320
- i = {
1321
- audio: n === "1",
1322
- video: t === "1"
1323
- };
1324
- }
1325
- a = e.getUint32(5), this.header = {
1326
- signature: n,
1327
- version: r,
1328
- flags: i,
1329
- dataOffset: a
1330
- };
1808
+ return t;
1331
1809
  };
1332
- isSurplusTag = (e, t) => {
1333
- let n = !0, r = e.byteLength;
1334
- if (t + 4 > r) n = !1;
1335
- else if (t + 4 + 11 > r) n = !1;
1336
- else {
1337
- let i = N(e, t + 4 + 1);
1338
- t + 4 + 11 + i > r && (n = !1);
1810
+ findAnnexBStart = (e, t) => {
1811
+ for (let n = t; n < e.length - 2; n++) if (!(e[n] !== 0 || e[n + 1] !== 0)) {
1812
+ if (e[n + 2] === 1) return {
1813
+ startOffset: n,
1814
+ naluOffset: n + 3,
1815
+ nextSearchOffset: n + 3
1816
+ };
1817
+ if (n + 3 < e.length && e[n + 2] === 0 && e[n + 3] === 1) return {
1818
+ startOffset: n,
1819
+ naluOffset: n + 4,
1820
+ nextSearchOffset: n + 4
1821
+ };
1339
1822
  }
1340
- return n;
1823
+ return null;
1824
+ };
1825
+ }, J = (e, t) => String.fromCharCode(e.getUint8(t), e.getUint8(t + 1), e.getUint8(t + 2), e.getUint8(t + 3)), Y = (e, t, n) => {
1826
+ if (t + 8 > n) return null;
1827
+ let r = e.getUint32(t, !1), i = J(e, t + 4), a = 8;
1828
+ if (r === 1) {
1829
+ if (t + 16 > n) return null;
1830
+ r = Number(e.getBigUint64(t + 8, !1)), a = 16;
1831
+ } else r === 0 && (r = n - t);
1832
+ return r < a || t + r > n ? null : {
1833
+ offset: t,
1834
+ size: r,
1835
+ type: i,
1836
+ headerSize: a,
1837
+ contentStart: t + a
1838
+ };
1839
+ }, X = (e, t, n, r) => {
1840
+ let i = t;
1841
+ for (; i < n;) {
1842
+ let t = Y(e, i, n);
1843
+ if (!t) break;
1844
+ r(t), i += t.size;
1845
+ }
1846
+ }, Z = (e, t, n, r) => {
1847
+ let i = null;
1848
+ return X(e, t, n, (t) => {
1849
+ if (t.type === r) i = t;
1850
+ else if (t.type !== "mdat") {
1851
+ let n = Z(e, t.contentStart, t.offset + t.size, r);
1852
+ n && (i = n);
1853
+ }
1854
+ }), i;
1855
+ }, Q = (e, t, n, r) => {
1856
+ for (let i = t; i + 8 <= n; i++) {
1857
+ if (J(e, i + 4) !== r) continue;
1858
+ let t = Y(e, i, n);
1859
+ if (t?.type === r) return t;
1860
+ }
1861
+ return null;
1862
+ }, $ = (e, t, n, r, i = 86) => Q(e, t + i, n, r) || Q(e, t + 8, n, r), ee = [
1863
+ 96e3,
1864
+ 88200,
1865
+ 64e3,
1866
+ 48e3,
1867
+ 44100,
1868
+ 32e3,
1869
+ 24e3,
1870
+ 22050,
1871
+ 16e3,
1872
+ 12e3,
1873
+ 11025,
1874
+ 8e3,
1875
+ 7350
1876
+ ], te = (e, t) => {
1877
+ let n = 0, r = t;
1878
+ for (; r < e.length;) {
1879
+ if (n = n << 7 | e[r] & 127, !(e[r] & 128)) return {
1880
+ len: n,
1881
+ next: r + 1
1882
+ };
1883
+ r++;
1884
+ }
1885
+ return {
1886
+ len: 0,
1887
+ next: t
1341
1888
  };
1342
- parseTagHeader = (e, t) => {
1343
- let n, r, i, a, o;
1344
- {
1345
- let r = e.getUint8(t), i;
1346
- switch (r) {
1347
- case 18:
1348
- i = "script";
1889
+ }, ne = class {
1890
+ audioConfig;
1891
+ videoConfig;
1892
+ tracks = /* @__PURE__ */ new Map();
1893
+ mdatBox = null;
1894
+ progressiveParsed = !1;
1895
+ on = {};
1896
+ getDiscardOffset = (e, t) => this.progressiveParsed || Z(t, 0, e, "moof") || this.tracks.size > 0 && !Z(t, 0, e, "mdat") ? e : 0;
1897
+ constructor() {}
1898
+ parse = async (e) => {
1899
+ let t = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), n = t.byteLength, r = 0;
1900
+ for (; r < n;) {
1901
+ let i = Y(e, r, n);
1902
+ if (!i) return 0;
1903
+ switch (i.type) {
1904
+ case "moov":
1905
+ this.parseMoov(e, i), this.tryRunProgressive(e, t);
1349
1906
  break;
1350
- case 8:
1351
- i = "audio";
1907
+ case "moof":
1908
+ this.parseMoof(e, i, t);
1352
1909
  break;
1353
- case 9:
1354
- i = "video";
1910
+ case "mdat":
1911
+ this.mdatBox = i, this.tryRunProgressive(e, t);
1355
1912
  break;
1913
+ case "sidx":
1914
+ case "styp":
1915
+ case "free":
1916
+ case "skip":
1917
+ case "ftyp": break;
1918
+ default: break;
1356
1919
  }
1357
- n = i;
1920
+ r += i.size, (i.type === "moov" || i.type === "moof" || i.type === "mdat") && await new Promise((e) => setTimeout(() => e(!0), 8));
1358
1921
  }
1359
- return r = N(e, t + 1), i = N(e, t + 4), a = e.getUint8(t + 7), o = N(e, t + 8), {
1360
- tagType: n,
1361
- dataSize: r,
1362
- timestamp: i,
1363
- timestampExtended: a,
1364
- streamID: o
1365
- };
1922
+ return this.progressiveParsed ? n : 0;
1366
1923
  };
1367
- parseTagBody = (e, t, n, r) => {
1368
- let i;
1369
- switch (e) {
1370
- case "script":
1371
- i = this.parseMetaData(t, n);
1372
- break;
1373
- case "audio":
1374
- i = this.parseAudio(t, n, r);
1375
- break;
1376
- case "video":
1377
- i = this.parseVideo(t, n, r);
1378
- break;
1924
+ tryRunProgressive = (e, t) => {
1925
+ if (this.progressiveParsed || this.tracks.size === 0) return;
1926
+ let n = this.mdatBox ?? Z(e, 0, t.byteLength, "mdat"), r = Z(e, 0, t.byteLength, "moov");
1927
+ !n || !r || this.isProgressiveReady(n, r, t.byteLength) && (this.parseProgressive(e, n, t), this.progressiveParsed = !0);
1928
+ };
1929
+ isProgressiveReady = (e, t, n) => e.offset + e.size <= n && t.offset + t.size <= n;
1930
+ parseMoov = (e, t) => {
1931
+ X(e, t.contentStart, t.offset + t.size, (t) => {
1932
+ t.type === "trak" && this.parseTrak(e, t);
1933
+ });
1934
+ let n = Z(e, t.contentStart, t.offset + t.size, "mvex");
1935
+ n && X(e, n.contentStart, n.offset + n.size, (t) => {
1936
+ if (t.type !== "trex") return;
1937
+ let n = e.getUint32(t.contentStart + 4, !1), r = this.tracks.get(n);
1938
+ r && (r.defaultSampleDuration = e.getUint32(t.contentStart + 12, !1), r.defaultSampleSize = e.getUint32(t.contentStart + 16, !1));
1939
+ });
1940
+ let r = Z(e, t.contentStart, t.offset + t.size, "mvhd");
1941
+ if (r) {
1942
+ let t = e.getUint8(r.contentStart), n = e.getUint32(r.contentStart + (t === 1 ? 20 : 12), !1), i = t === 1 ? Number(e.getBigUint64(r.contentStart + 24, !1)) : e.getUint32(r.contentStart + 16, !1);
1943
+ this.on.info?.({
1944
+ width: [...this.tracks.values()].find((e) => e.kind === "video")?.width,
1945
+ height: [...this.tracks.values()].find((e) => e.kind === "video")?.height,
1946
+ duration: n ? i / n : void 0
1947
+ });
1379
1948
  }
1380
- return i;
1381
1949
  };
1382
- parseMetaData = (e, t) => {
1383
- let n = t;
1384
- if (e.getUint8(n) !== 2) throw Error("Invalid AMF type for onMetaData (expected 0x02)");
1385
- n += 1;
1386
- let r = e.getUint16(n, !1);
1387
- n += 2;
1388
- {
1389
- let t = new Int8Array(e.buffer.slice(n, n + r));
1390
- if ((this.textDecoder?.decode(t) || "") !== "onMetaData") throw Error("Expected 'onMetaData' string");
1391
- n += r;
1950
+ readTkhdSize = (e, t) => {
1951
+ let n = e.getUint8(t.contentStart), r = n === 1 ? 72 : 64, i = n === 1 ? 76 : 68;
1952
+ return {
1953
+ width: e.getUint32(t.contentStart + r, !1) >> 16,
1954
+ height: e.getUint32(t.contentStart + i, !1) >> 16
1955
+ };
1956
+ };
1957
+ readSampleEntrySize = (e, t) => ({
1958
+ width: e.getUint16(t + 32, !1),
1959
+ height: e.getUint16(t + 34, !1)
1960
+ });
1961
+ parseTrak = (e, t) => {
1962
+ let n = Z(e, t.contentStart, t.offset + t.size, "tkhd"), r = Z(e, t.contentStart, t.offset + t.size, "mdia");
1963
+ if (!n || !r) return;
1964
+ let i = e.getUint8(n.contentStart), a = e.getUint32(n.contentStart + (i === 1 ? 20 : 12), !1), { width: o, height: s } = this.readTkhdSize(e, n), c = Z(e, r.contentStart, r.offset + r.size, "hdlr"), l = Z(e, r.contentStart, r.offset + r.size, "mdhd");
1965
+ if (!c || !l) return;
1966
+ let u = J(e, c.contentStart + 8), d = u === "vide" ? "video" : u === "soun" ? "audio" : void 0;
1967
+ if (!d) return;
1968
+ let f = e.getUint8(l.contentStart), p = e.getUint32(l.contentStart + (f === 1 ? 20 : 12), !1);
1969
+ this.tracks.set(a, {
1970
+ kind: d,
1971
+ trackId: a,
1972
+ timescale: p,
1973
+ width: o,
1974
+ height: s
1975
+ });
1976
+ let m = Z(e, r.contentStart, r.offset + r.size, "minf"), h = m && Z(e, m.contentStart, m.offset + m.size, "stbl"), g = h && Z(e, h.contentStart, h.offset + h.size, "stsd");
1977
+ if (!g) return;
1978
+ let _ = g.contentStart + 8, v = _ + e.getUint32(_, !1), y = J(e, _ + 4);
1979
+ if (d === "video" && (y === "avc1" || y === "avc3")) {
1980
+ let t = this.readSampleEntrySize(e, _);
1981
+ t.width && t.height && (o = t.width, s = t.height);
1982
+ let n = $(e, _, v, "avcC", 86);
1983
+ if (!n) return;
1984
+ let r = new Uint8Array(e.buffer, n.contentStart, n.size - n.headerSize), i = w(r), a = i.codec.replace(/^avc1\./, `${y}.`);
1985
+ this.videoConfig = {
1986
+ kind: "video",
1987
+ codec: a,
1988
+ description: r,
1989
+ sps: i.sps,
1990
+ pps: i.pps
1991
+ }, this.on.config?.(this.videoConfig);
1992
+ }
1993
+ if (d === "video") {
1994
+ let e = this.tracks.get(a);
1995
+ e && (e.width = o, e.height = s);
1996
+ }
1997
+ if (d === "audio" && y === "mp4a") {
1998
+ let t = $(e, _, v, "esds", 36), n = t && this.parseEsds(e, t);
1999
+ n && (this.audioConfig = n, this.on.config?.(n));
1392
2000
  }
1393
- let i = this.getAmfType(e, n);
1394
- return n += 1, this.getAMFValue(e, n, i).value;
1395
2001
  };
1396
- parseAudio = (e, t, n) => {
1397
- let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i >> 2 & 3, s = i >> 1 & 1, c = i & 1;
1398
- r += 1;
1399
- let l = e.getUint8(r);
1400
- r += 1;
1401
- let u = n, d = new Uint8Array(e.buffer.slice(r, r + u));
1402
- if (a === 10 && l === 0) {
1403
- let t = e.getUint8(r), n = e.getUint8(r + 1), i = t >> 3 & 31, u = (t & 7) << 1 | n >> 7;
2002
+ parseEsds = (e, t) => {
2003
+ let n = new Uint8Array(e.buffer, t.contentStart, t.size - t.headerSize);
2004
+ for (let e = 0; e < n.length - 2; e++) {
2005
+ if (n[e] !== 5) continue;
2006
+ let { len: t, next: r } = te(n, e + 1);
2007
+ if (t < 2 || r + t > n.length) continue;
2008
+ let i = n.slice(r, r + t), a = i[0], o = i[1], s = a >> 3 & 31, c = (a & 7) << 1 | o >> 7, l = o >> 3 & 15;
1404
2009
  return {
1405
- soundFormat: a,
1406
- soundRate: o,
1407
- soundSize: s,
1408
- soundType: c,
1409
- accPacketType: l,
1410
- data: d,
1411
- samplingFrequencyIndex: u,
1412
- channelConfiguration: n >> 3 & 15,
1413
- codec: `mp4a.40.${i}`,
1414
- sampleRate: [
1415
- 96e3,
1416
- 88200,
1417
- 64e3,
1418
- 48e3,
1419
- 44100,
1420
- 32e3,
1421
- 24e3,
1422
- 22050,
1423
- 16e3,
1424
- 12e3,
1425
- 11025,
1426
- 8e3,
1427
- 7350
1428
- ][u]
2010
+ kind: "audio",
2011
+ codec: `mp4a.40.${s}`,
2012
+ sampleRate: ee[c] ?? 44100,
2013
+ numberOfChannels: l,
2014
+ description: i
1429
2015
  };
1430
2016
  }
1431
- return {
1432
- soundFormat: a,
1433
- soundRate: o,
1434
- soundSize: s,
1435
- soundType: c,
1436
- accPacketType: l,
1437
- data: d
1438
- };
1439
- };
1440
- parseVideo = (e, t, n) => {
1441
- let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i & 15;
1442
- r += 1;
1443
- let s = e.getUint8(r);
1444
- r += 1;
1445
- let c = N(e, r);
1446
- r += 3;
1447
- let l = n - 5, u = new Uint8Array(e.buffer.slice(r, r + l));
1448
- switch (o) {
1449
- case 7:
1450
- if (s === 0) return {
1451
- frameType: a,
1452
- codecID: o,
1453
- avcPacketType: s,
1454
- cts: c,
1455
- data: u,
1456
- ...w(u)
1457
- };
1458
- if (s === 1) {
1459
- let t = [], i = r + n - 5;
1460
- for (; !(r + 4 > i);) {
1461
- let n = e.getUint32(r, !1), i = new Uint8Array(e.buffer.slice(r, r + 4 + n));
1462
- r += 4 + n, t.push(i);
1463
- }
1464
- return {
1465
- frameType: a,
1466
- codecID: o,
1467
- avcPacketType: s,
1468
- cts: c,
1469
- data: u,
1470
- nalus: t
1471
- };
1472
- }
1473
- break;
1474
- default: throw Error("Unsupported codecID");
2017
+ return null;
2018
+ };
2019
+ findMdatForMoof = (e, t, n) => {
2020
+ let r = t.offset + t.size;
2021
+ for (; r + 8 <= n;) {
2022
+ let t = Y(e, r, n);
2023
+ if (!t || t.type === "moof") break;
2024
+ if (t.type === "mdat") return t;
2025
+ r += t.size;
1475
2026
  }
1476
- return {
1477
- frameType: a,
1478
- codecID: o,
1479
- avcPacketType: s,
1480
- cts: c,
1481
- data: u
1482
- };
2027
+ return null;
1483
2028
  };
1484
- getAmfType = (e, t) => e.getUint8(t);
1485
- getAMFName = (e, t, n) => {
1486
- let r = new Uint8Array(e.buffer.slice(t, t + n));
1487
- return this.textDecoder?.decode(r) || "";
2029
+ parseMoof = (e, t, n) => {
2030
+ let r = this.findMdatForMoof(e, t, n.byteLength);
2031
+ r && X(e, t.contentStart, t.offset + t.size, (i) => {
2032
+ i.type === "traf" && this.parseTraf(e, i, t.offset, r, n);
2033
+ });
1488
2034
  };
1489
- getAMFValue = (e, t, n) => {
1490
- let r = t, i, a = 0;
1491
- switch (n) {
1492
- case 0:
1493
- i = e.getFloat64(r, !1), a = 8;
1494
- break;
1495
- case 1:
1496
- i = !!e.getUint8(r), a = 1;
1497
- break;
1498
- case 2:
1499
- {
1500
- i = "";
1501
- let t = e.getUint16(r, !1);
1502
- r += 2;
1503
- let n = new Int8Array(e.buffer, r, t).filter((e) => e !== 0);
1504
- i = (this.textDecoder?.decode(n) || "").trim(), a = 2 + t;
1505
- }
1506
- break;
1507
- case 3:
1508
- for (i = {}; r < e.byteLength;) {
1509
- let t = e.getUint16(r, !1);
1510
- if (t === 0) break;
1511
- r += 2;
1512
- let n = this.getAMFName(e, r, t);
1513
- r += t;
1514
- let o = this.getAmfType(e, r);
1515
- if (o === 6) break;
1516
- r += 1;
1517
- let s = this.getAMFValue(e, r, o);
1518
- r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
1519
- }
1520
- break;
1521
- case 8:
1522
- {
1523
- i = {};
1524
- let t = e.getUint32(r, !1);
1525
- r += 4;
1526
- for (let n = 0; n < t; n++) {
1527
- let t = e.getUint16(r, !1);
1528
- r += 2;
1529
- let n = this.getAMFName(e, r, t);
1530
- r += t;
1531
- let o = this.getAmfType(e, r);
1532
- r += 1;
1533
- let s = this.getAMFValue(e, r, o);
1534
- r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
1535
- }
1536
- }
1537
- break;
1538
- case 10:
1539
- {
1540
- i = [];
1541
- let t = e.getUint32(r, !1);
1542
- r += 4;
1543
- for (let n = 0; n < t; n++) {
1544
- let t = this.getAmfType(e, r);
1545
- r += 1;
1546
- let n = this.getAMFValue(e, r, t);
1547
- r += n.length, i.push(n.value), a = 1 + n.length;
1548
- }
1549
- }
1550
- break;
2035
+ ticksToMs = (e, t) => e / t * 1e3;
2036
+ readCttsOffsets = (e, t, n) => {
2037
+ let r = new Int32Array(n);
2038
+ if (!t || n === 0) return r;
2039
+ let i = e.getUint32(t.contentStart + 4, !1), a = 0, o = 0, s = 0, c = t.contentStart + 8;
2040
+ for (let t = 0; t < n; t++) o === 0 && a < i && (o = e.getUint32(c, !1), s = e.getInt32(c + 4, !1), c += 8, a++), r[t] = s, --o;
2041
+ return r;
2042
+ };
2043
+ parseTraf = (e, t, n, r, i) => {
2044
+ let a = Z(e, t.contentStart, t.offset + t.size, "tfhd"), o = Z(e, t.contentStart, t.offset + t.size, "tfdt"), s = Z(e, t.contentStart, t.offset + t.size, "trun");
2045
+ if (!a || !o || !s) return;
2046
+ let c = e.getUint32(a.contentStart + 4, !1), l = this.tracks.get(c);
2047
+ if (!l) return;
2048
+ let u = e.getUint32(a.contentStart, !1) & 16777215, d = (u & 131072) != 0, f = a.contentStart + 8, p = null, m = 0, h = 0;
2049
+ u & 1 && (p = Number(e.getBigUint64(f, !1)), f += 8), u & 2 && (f += 4), u & 8 && (m = e.getUint32(f, !1), f += 4), u & 16 && (h = e.getUint32(f, !1), f += 4), u & 32 && (f += 4), u & 8 || (m = l.defaultSampleDuration ?? 0), u & 16 || (h = l.defaultSampleSize ?? 0);
2050
+ let g = e.getUint8(o.contentStart) === 1 ? Number(e.getBigUint64(o.contentStart + 4, !1)) : e.getUint32(o.contentStart + 4, !1), _ = e.getUint32(s.contentStart, !1) & 16777215, v = s.offset + s.size, y = s.contentStart + 4;
2051
+ if (y + 4 > v) return;
2052
+ let b = e.getUint32(y, !1);
2053
+ if (y += 4, b === 0 || b > 1e4) return;
2054
+ let x = 0;
2055
+ if (_ & 1) {
2056
+ if (y + 4 > v) return;
2057
+ x = e.getInt32(y, !1), y += 4;
1551
2058
  }
1552
- return {
1553
- amfType: n,
1554
- length: a,
1555
- value: i
1556
- };
2059
+ _ & 4 && (y += 4);
2060
+ let S;
2061
+ S = d ? n : p === null ? r.offset : p;
2062
+ let C = S + (_ & 1 ? x : 0), w = g;
2063
+ for (let t = 0; t < b; t++) {
2064
+ let t = m, n = h, r = 0;
2065
+ if (_ & 256) {
2066
+ if (y + 4 > v) break;
2067
+ t = e.getUint32(y, !1), y += 4;
2068
+ }
2069
+ if (_ & 512) {
2070
+ if (y + 4 > v) break;
2071
+ n = e.getUint32(y, !1), y += 4;
2072
+ }
2073
+ if (_ & 1024) {
2074
+ if (y + 4 > v) break;
2075
+ r = e.getUint32(y, !1), y += 4;
2076
+ }
2077
+ let a = 0;
2078
+ if (_ & 2048) {
2079
+ if (y + 4 > v) break;
2080
+ a = e.getInt32(y, !1), y += 4;
2081
+ }
2082
+ if (t <= 0 && l.kind === "audio") {
2083
+ let e = this.audioConfig?.sampleRate ?? l.timescale;
2084
+ t = Math.round(1024 * l.timescale / e);
2085
+ }
2086
+ if (n <= 0) continue;
2087
+ if (C + n > i.byteLength) break;
2088
+ let o = i.slice(C, C + n);
2089
+ C += n;
2090
+ let s = this.ticksToMs(w, l.timescale), c = this.ticksToMs(w + a, l.timescale), u = c - s, d = l.kind !== "video" || !(r >> 16 & 1) || this.isAvccKeyFrame(o) ? "key" : "delta";
2091
+ if (l.kind === "video") {
2092
+ let e = this.splitAvccNalus(o);
2093
+ this.on.chunk?.({
2094
+ kind: "video",
2095
+ type: d,
2096
+ dts: s,
2097
+ pts: c,
2098
+ cts: u,
2099
+ data: o,
2100
+ nalus: e
2101
+ });
2102
+ } else this.on.chunk?.({
2103
+ kind: "audio",
2104
+ type: "key",
2105
+ dts: s,
2106
+ pts: c,
2107
+ cts: u,
2108
+ data: o
2109
+ });
2110
+ w += t;
2111
+ }
2112
+ };
2113
+ parseProgressive = (e, t, n) => {
2114
+ let r = Z(e, 0, n.byteLength, "moov");
2115
+ if (!r) return;
2116
+ let i = [];
2117
+ X(e, r.contentStart, r.offset + r.size, (t) => {
2118
+ if (t.type !== "trak") return;
2119
+ let r = this.collectProgressiveTrackSamples(e, t, n);
2120
+ i.push(...r);
2121
+ }), i.sort((e, t) => e.dts - t.dts || (e.kind === "video" ? -1 : 1));
2122
+ for (let e of i) this.on.chunk?.(e);
2123
+ };
2124
+ collectProgressiveTrackSamples = (e, t, n) => {
2125
+ let r = [], i = Z(e, t.contentStart, t.offset + t.size, "tkhd"), a = Z(e, t.contentStart, t.offset + t.size, "mdia");
2126
+ if (!i || !a) return r;
2127
+ let o = e.getUint32(i.contentStart + (e.getUint8(i.contentStart) === 1 ? 20 : 12), !1), s = this.tracks.get(o);
2128
+ if (!s) return r;
2129
+ let c = Z(e, a.contentStart, a.offset + a.size, "minf"), l = c && Z(e, c.contentStart, c.offset + c.size, "stbl");
2130
+ if (!l) return r;
2131
+ let u = Z(e, l.contentStart, l.offset + l.size, "stsz"), d = Z(e, l.contentStart, l.offset + l.size, "stco"), f = Z(e, l.contentStart, l.offset + l.size, "co64"), p = Z(e, l.contentStart, l.offset + l.size, "stsc"), m = Z(e, l.contentStart, l.offset + l.size, "stts"), h = Z(e, l.contentStart, l.offset + l.size, "stss"), g = Z(e, l.contentStart, l.offset + l.size, "ctts");
2132
+ if (!u || !d && !f || !m) return r;
2133
+ let _ = e.getUint32(u.contentStart + 4, !1), v = e.getUint32(u.contentStart + 8, !1), y = this.readCttsOffsets(e, g, v), b = d ? e.getUint32(d.contentStart + 4, !1) : e.getUint32(f.contentStart + 4, !1), x = (t) => d ? e.getUint32(d.contentStart + 8 + (t - 1) * 4, !1) : Number(e.getBigUint64(f.contentStart + 8 + (t - 1) * 8, !1)), S = /* @__PURE__ */ new Set();
2134
+ if (h) {
2135
+ let t = e.getUint32(h.contentStart + 4, !1);
2136
+ for (let n = 0; n < t; n++) S.add(e.getUint32(h.contentStart + 8 + n * 4, !1));
2137
+ }
2138
+ let C = [];
2139
+ if (p) {
2140
+ let t = e.getUint32(p.contentStart + 4, !1);
2141
+ for (let n = 0; n < t; n++) {
2142
+ let t = p.contentStart + 8 + n * 12;
2143
+ C.push({
2144
+ firstChunk: e.getUint32(t, !1),
2145
+ samplesPerChunk: e.getUint32(t + 4, !1)
2146
+ });
2147
+ }
2148
+ }
2149
+ let w = (e) => {
2150
+ if (C.length === 0) return v === b ? 1 : Math.max(1, Math.ceil(v / b));
2151
+ let t = C[0].samplesPerChunk;
2152
+ for (let n of C) if (n.firstChunk <= e) t = n.samplesPerChunk;
2153
+ else break;
2154
+ return t;
2155
+ }, T = 0, E = m.contentStart + 8, D = e.getUint32(m.contentStart + 4, !1), O = 0, k = D > 0 ? e.getUint32(E, !1) : 0, A = D > 0 ? e.getUint32(E + 4, !1) : 0;
2156
+ E += 8;
2157
+ let j = 1;
2158
+ for (let t = 1; t <= b && j <= v; t++) {
2159
+ let i = w(t), a = x(t);
2160
+ for (let t = 0; t < i && j <= v; t++, j++) {
2161
+ let t = _ === 0 ? e.getUint32(u.contentStart + 12 + (j - 1) * 4, !1) : _;
2162
+ if (a + t > n.byteLength) return r;
2163
+ let i = n.slice(a, a + t);
2164
+ a += t;
2165
+ let o = this.ticksToMs(T, s.timescale), c = y[j - 1] ?? 0, l = this.ticksToMs(T + c, s.timescale), d = l - o, f = s.kind === "video" ? this.isAvccKeyFrame(i) || S.has(j) || S.size === 0 : !0;
2166
+ s.kind === "video" ? r.push({
2167
+ kind: "video",
2168
+ type: f ? "key" : "delta",
2169
+ dts: o,
2170
+ pts: l,
2171
+ cts: d,
2172
+ data: i,
2173
+ nalus: this.splitAvccNalus(i)
2174
+ }) : r.push({
2175
+ kind: "audio",
2176
+ type: "key",
2177
+ dts: o,
2178
+ pts: l,
2179
+ cts: d,
2180
+ data: i
2181
+ }), T += A, --k, k === 0 && O + 1 < D && (O += 1, E = m.contentStart + 8 + O * 8, k = e.getUint32(E, !1), A = e.getUint32(E + 4, !1));
2182
+ }
2183
+ }
2184
+ return r;
2185
+ };
2186
+ isAvccKeyFrame = (e) => {
2187
+ let t = 0, n = new DataView(e.buffer, e.byteOffset, e.byteLength);
2188
+ for (; t + 4 <= e.byteLength;) {
2189
+ let r = n.getUint32(t, !1);
2190
+ if (r <= 0 || t + 4 + r > e.byteLength) break;
2191
+ if ((e[t + 4] & 31) == 5) return !0;
2192
+ t += 4 + r;
2193
+ }
2194
+ return !1;
2195
+ };
2196
+ splitAvccNalus = (e) => {
2197
+ let t = [], n = 0, r = new DataView(e.buffer, e.byteOffset, e.byteLength);
2198
+ for (; n + 4 <= e.byteLength;) {
2199
+ let i = r.getUint32(n, !1);
2200
+ if (i <= 0 || n + 4 + i > e.byteLength) break;
2201
+ t.push(e.slice(n, n + 4 + i)), n += 4 + i;
2202
+ }
2203
+ return t;
1557
2204
  };
1558
- }, F = class {
2205
+ }, re = class {
1559
2206
  pattern;
1560
- cacher = new A();
2207
+ cacher = new z();
1561
2208
  isParseing = !1;
1562
2209
  offset = 0;
1563
2210
  parser;
@@ -1566,14 +2213,18 @@ var p = "pr-player-render", m = class {
1566
2213
  init = (e) => {
1567
2214
  switch (this.destroy(), this.pattern = e, this.pattern) {
1568
2215
  case "flv":
1569
- this.parser = new P();
2216
+ this.parser = new W();
1570
2217
  break;
1571
2218
  case "hls":
1572
- this.parser = new M();
2219
+ this.parser = new q();
2220
+ break;
2221
+ case "dash":
2222
+ case "mp4":
2223
+ this.parser = new ne();
1573
2224
  break;
1574
2225
  default: throw Error("is error pattern.");
1575
2226
  }
1576
- this.parser.on.debug = (e) => this.on.debug && this.on.debug(e), this.parser.on.info = (e) => this.on.info && this.on.info(e), this.parser.on.config = (e) => this.on.config && this.on.config(e), this.parser.on.chunk = (e) => {
2227
+ this.parser.on.info = (e) => this.on.info && this.on.info(e), this.parser.on.config = (e) => this.on.config && this.on.config(e), this.parser.on.chunk = (e) => {
1577
2228
  this.cacher.pushChunk(e), this.on.chunk && this.on.chunk(e);
1578
2229
  };
1579
2230
  };
@@ -1590,14 +2241,23 @@ var p = "pr-player-render", m = class {
1590
2241
  for (;;) {
1591
2242
  let e = this.cacher.next(this.offset);
1592
2243
  if (this.offset = 0, !e) break;
1593
- this.offset = await this.parser.parse(e);
2244
+ try {
2245
+ let t = await this.parser.parse(e);
2246
+ if (this.pattern === "mp4" || this.pattern === "dash") {
2247
+ let t = this.parser;
2248
+ this.offset = t.getDiscardOffset(e.byteLength, e);
2249
+ } else this.offset = t;
2250
+ } catch {
2251
+ this.cacher.discardPayload();
2252
+ continue;
2253
+ }
1594
2254
  }
1595
2255
  this.isParseing = !1;
1596
2256
  } catch {
1597
- this.destroy();
2257
+ this.isParseing = !1, this.cacher.discardPayload();
1598
2258
  }
1599
2259
  };
1600
- }, I = class {
2260
+ }, ie = class {
1601
2261
  pattern = "flv";
1602
2262
  audioDecoderConfig;
1603
2263
  audioDecoder;
@@ -1615,6 +2275,7 @@ var p = "pr-player-render", m = class {
1615
2275
  flv: [30, 50],
1616
2276
  hls: [200, 300],
1617
2277
  dash: [50, 100],
2278
+ mp4: [30, 50],
1618
2279
  rtmp: [50, 100]
1619
2280
  };
1620
2281
  decodingSpeedRatio = 1;
@@ -1634,8 +2295,12 @@ var p = "pr-player-render", m = class {
1634
2295
  init = (e) => {
1635
2296
  this.destroy(), this.pattern = e, this.baseTime = (/* @__PURE__ */ new Date()).getTime() - performance.now(), this.initDecodeInterval();
1636
2297
  };
1637
- initAudio = (e) => {
1638
- this.audio.destroy(), this.audioDecoderConfig = { ...e }, this.audioDecoder = new AudioDecoder({
2298
+ initAudio = async (e) => {
2299
+ if (this.audio.destroy(), !(await AudioDecoder.isConfigSupported(e).catch(() => ({ supported: !1 }))).supported) {
2300
+ console.warn("[Decoder] audio config not supported:", e.codec, "audio will be skipped"), this.audioDecoderConfig = void 0, this.audioDecoder = void 0;
2301
+ return;
2302
+ }
2303
+ this.audioDecoderConfig = { ...e }, this.audioDecoder = new AudioDecoder({
1639
2304
  output: (e) => {
1640
2305
  let t = this.decodingSpeedRatio;
1641
2306
  this.on.audio.decode && this.on.audio.decode({
@@ -1650,15 +2315,18 @@ var p = "pr-player-render", m = class {
1650
2315
  };
1651
2316
  initVideo = (e) => {
1652
2317
  this.video.destroy(), this.videoDecoderConfig = { ...e }, this.videoDecoder = new VideoDecoder({
1653
- output: async (e) => {
2318
+ output: (e) => {
1654
2319
  this.frameStartTime ||= e.timestamp;
1655
2320
  try {
1656
- let t = e.timestamp - this.frameStartTime + this.baseTime * 1e3, n = await createImageBitmap(e);
1657
- if (e.close(), !n.width || !n.height) return n.close();
1658
- this.on.video.decode && this.on.video.decode({
2321
+ let t = e.timestamp - this.frameStartTime + this.baseTime * 1e3, n = new VideoFrame(e, { timestamp: t });
2322
+ if (e.close(), !n.displayWidth || !n.displayHeight) {
2323
+ n.close();
2324
+ return;
2325
+ }
2326
+ this.on.video.decode?.({
1659
2327
  timestamp: t,
1660
- bitmap: n
1661
- }), this.currentChunk && this.currentChunk.kind === "video" && this.currentChunk.nalus && this.on.nalus && this.on.nalus(this.currentChunk.nalus);
2328
+ frame: n
2329
+ }), this.currentChunk?.kind === "video" && this.currentChunk.nalus && this.on.nalus?.(this.currentChunk.nalus);
1662
2330
  } catch {
1663
2331
  e.close();
1664
2332
  }
@@ -1684,7 +2352,12 @@ var p = "pr-player-render", m = class {
1684
2352
  e -= t;
1685
2353
  }
1686
2354
  this.nextRenderTime = this.lastRenderTime + e, this.decodeTimer = setTimeout(() => {
1687
- this.decode(), this.initDecodeInterval();
2355
+ try {
2356
+ this.decode();
2357
+ } catch (e) {
2358
+ console.error("[Decoder] decode error:", e);
2359
+ }
2360
+ this.initDecodeInterval();
1688
2361
  }, e);
1689
2362
  };
1690
2363
  decode = () => {
@@ -1722,9 +2395,12 @@ var p = "pr-player-render", m = class {
1722
2395
  }
1723
2396
  };
1724
2397
  decodeAudio = (e) => {
1725
- if (!this.audioDecoder) return;
1726
- let t = new EncodedAudioChunk(e);
1727
- this.audioDecoder.decode(t);
2398
+ if (this.audioDecoder) try {
2399
+ let t = new EncodedAudioChunk(e);
2400
+ this.audioDecoder.decode(t);
2401
+ } catch (e) {
2402
+ console.warn("[Decoder] audio decode error (AudioDecoder may be unusable):", e);
2403
+ }
1728
2404
  };
1729
2405
  decodeVideo = (e) => {
1730
2406
  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)) {
@@ -1748,35 +2424,45 @@ var p = "pr-player-render", m = class {
1748
2424
  this.videoDecoderConfig = void 0, this.videoDecoder?.close(), this.videoDecoder = void 0, this.hasKeyFrame = !1;
1749
2425
  }
1750
2426
  };
1751
- }, L = class {
2427
+ }, ae = class {
1752
2428
  renderMap = /* @__PURE__ */ new Map();
1753
2429
  constructor() {}
1754
2430
  push = async (e) => {
1755
- let { timestamp: t } = e, { bitmap: n } = e;
2431
+ let t = e.frame, n = t.timestamp;
1756
2432
  try {
1757
- let e = [...this.renderMap.keys()];
1758
- for (let r of e) {
1759
- let e = this.renderMap.get(r);
1760
- if (!e) continue;
1761
- let { pause: i = !1, writer: a, offscreen: o, option: s } = e;
1762
- if (i === !0) continue;
1763
- let c = async (e) => {
1764
- if (a) {
1765
- let n = new VideoFrame(e, { timestamp: t });
1766
- try {
1767
- await a.write(n);
1768
- } catch {}
1769
- n.close();
1770
- } else o && (o.width = e.width, o.height = e.height, o?.getContext("2d")?.drawImage(e, 0, 0));
1771
- };
1772
- if (r === "default") await c(n);
1773
- else if (s) {
1774
- let { sx: e = 0, sy: t = 0, sw: r = n.width, sh: i = n.height } = s;
1775
- await c(await createImageBitmap(n, e, t, r, i));
2433
+ let e = [...this.renderMap.entries()].filter(([, e]) => e && !e.pause);
2434
+ if (e.length === 1 && e[0][0] === "default" && e[0][1].writer) {
2435
+ e[0][1].writer.write(t).catch(() => {}), t.close();
2436
+ return;
2437
+ }
2438
+ for (let [r, i] of e) {
2439
+ let { writer: e, offscreen: a, option: o } = i;
2440
+ if (e) {
2441
+ let i;
2442
+ if (r === "default" || !o) i = new VideoFrame(t, { timestamp: n });
2443
+ else {
2444
+ let { sx: e = 0, sy: r = 0, sw: a = t.displayWidth, sh: s = t.displayHeight } = o;
2445
+ i = new VideoFrame(t, {
2446
+ visibleRect: {
2447
+ x: e,
2448
+ y: r,
2449
+ width: a,
2450
+ height: s
2451
+ },
2452
+ displayWidth: a,
2453
+ displayHeight: s,
2454
+ timestamp: n
2455
+ });
2456
+ }
2457
+ e.write(i).catch(() => {}), i.close();
2458
+ } else if (a) if (r === "default" || !o) a.width = t.displayWidth, a.height = t.displayHeight, a.getContext("2d")?.drawImage(t, 0, 0);
2459
+ else {
2460
+ let { sx: e = 0, sy: n = 0, sw: r = t.displayWidth, sh: i = t.displayHeight } = o;
2461
+ a.width = r, a.height = i, a.getContext("2d")?.drawImage(t, e, n, r, i, 0, 0, r, i);
1776
2462
  }
1777
2463
  }
1778
2464
  } catch {}
1779
- n.close();
2465
+ t.close();
1780
2466
  };
1781
2467
  addCut = (e) => {
1782
2468
  let { key: t, writable: n, offscreen: r, option: i } = {
@@ -1800,7 +2486,7 @@ var p = "pr-player-render", m = class {
1800
2486
  this.renderMap.delete(e);
1801
2487
  };
1802
2488
  setCut = (e) => {
1803
- let { key: t, cutOption: n } = {
2489
+ let { key: t, option: n } = {
1804
2490
  key: "default",
1805
2491
  ...e
1806
2492
  }, r = this.renderMap.get(t);
@@ -1824,4 +2510,4 @@ var p = "pr-player-render", m = class {
1824
2510
  };
1825
2511
  };
1826
2512
  //#endregion
1827
- export { I as Decoder, l as DecoderWorker, F as Demuxer, i as DemuxerWorker, k as PrPlayer, L as Render, m as RenderWorker };
2513
+ export { ie as Decoder, l as DecoderWorker, re as Demuxer, i as DemuxerWorker, R as PrPlayer, ae as Render, m as RenderWorker, N as buildSegmentUrl, I as parseMpd, j as resolveUrl };