pr-player 0.4.1 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +148 -5
- package/dist/{src/PrPlayer.d.ts → PrPlayer.d.ts} +3 -3
- package/dist/{src/decoder → decoder}/Decoder.d.ts +1 -1
- package/dist/{src/decoder → decoder}/type.d.ts +1 -1
- package/dist/{src/demuxer → demuxer}/Cacher.d.ts +1 -0
- package/dist/{src/demuxer → demuxer}/Demuxer.d.ts +2 -15
- package/dist/{src/demuxer → demuxer/parsers}/264Parser.d.ts +3 -8
- package/dist/demuxer/parsers/boxParser.d.ts +19 -0
- package/dist/{src/demuxer → demuxer/parsers}/flv264Parser.d.ts +4 -4
- package/dist/demuxer/parsers/fmp4Parser.d.ts +38 -0
- package/dist/demuxer/parsers/index.d.ts +7 -0
- package/dist/demuxer/parsers/mpdParser.d.ts +41 -0
- package/dist/{src/demuxer → demuxer/parsers}/ts264Parser.d.ts +8 -4
- package/dist/demuxer/parsers/types.d.ts +14 -0
- package/dist/{src/index.d.ts → index.d.ts} +1 -0
- package/dist/index.js +1255 -567
- package/dist/index.umd.cjs +2 -2
- package/dist/{src/render → render}/Render.d.ts +2 -2
- package/dist/{src/render → render}/RenderWorker.d.ts +1 -1
- package/dist/{src/tools.d.ts → tools.d.ts} +1 -1
- package/dist/type.d.ts +1 -0
- package/package.json +5 -5
- package/dist/src/type.d.ts +0 -1
- /package/dist/{src/PrResolves.d.ts → PrResolves.d.ts} +0 -0
- /package/dist/{src/audioPlayer → audioPlayer}/audioPlayer.d.ts +0 -0
- /package/dist/{src/decoder → decoder}/DecoderWorker.d.ts +0 -0
- /package/dist/{src/decoder → decoder}/decoder.worker.d.ts +0 -0
- /package/dist/{src/demuxer → demuxer}/DemuxerWorker.d.ts +0 -0
- /package/dist/{src/demuxer → demuxer}/demuxer.worker.d.ts +0 -0
- /package/dist/{src/demuxer → demuxer}/type.d.ts +0 -0
- /package/dist/{src/render → render}/render.worker.d.ts +0 -0
- /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:
|
|
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
|
|
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,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" });
|
|
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.
|
|
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({
|
|
@@ -166,7 +163,7 @@ var p = "pr-player-render", m = class {
|
|
|
166
163
|
data: {}
|
|
167
164
|
}), this.worker.terminate();
|
|
168
165
|
};
|
|
169
|
-
}, h = "data:text/javascript;base64,LyoqDQogKiDpnZnpn7Pml7bvvJrlnKjpopHln5/ku4Xkv53nlZkgYmluIDDvvZ4077yI6ZqP5py677yJ77yM5YW25L2Z5Li6IDDvvIzlho3lkIjmiJDml7bln5/lhpnlhaXovpPlh7og4oCUIOWNs+S/ruaUueecn+WuniBQQ03vvIznu48gQW5hbHlzZXIoZmZ0U2l6ZT01MTIpIOaXtuWJjSA1IOS4qiBiaW4g5pyJ6IO96YeP44CB5YW25L2Z5o6l6L+
|
|
166
|
+
}, h = "data:text/javascript;base64,LyoqDQogKiDpnZnpn7Pml7bvvJrlnKjpopHln5/ku4Xkv53nlZkgYmluIDDvvZ4077yI6ZqP5py677yJ77yM5YW25L2Z5Li6IDDvvIzlho3lkIjmiJDml7bln5/lhpnlhaXovpPlh7og4oCUIOWNs+S/ruaUueecn+WuniBQQ03vvIznu48gQW5hbHlzZXIoZmZ0U2l6ZT01MTIpIOaXtuWJjSA1IOS4qiBiaW4g5pyJ6IO96YeP44CB5YW25L2Z5o6l6L+RIDDjgIINCiAqIFBFUklPRCDpobvkuI4gUHJBdWRpb1N0cmVhbSDkuK0gYW5hbHlzZXJOb2RlLmZmdFNpemUg5LiA6Ie044CCDQogKi8NCmNsYXNzIE5vdEVtcHR5RmlsdGVyV29ya2xldFByb2Nlc3NvciBleHRlbmRzIEF1ZGlvV29ya2xldFByb2Nlc3NvciB7DQogIGlzRGVzdHJveSA9IGZhbHNlDQogIGRlYnVnID0gZmFsc2UNCg0KICBzdGF0aWMgU0lMRU5DRV9FUFMgPSAxZS0xNQ0KDQogIC8qKiDkuI4gc3JjL1ByQXVkaW9TdHJlYW0udHMg6YeMIGFuYWx5c2VyTm9kZS5mZnRTaXplIOS/neaMgeS4gOiHtCAqLw0KICBzdGF0aWMgUEVSSU9EID0gNTEyDQoNCiAgLyoqIGJpbiAw772eNCDpmo/mnLrlpI3mlbDvvIjlrp7pg6gv6Jma6YOo77yJ55qE5aSn6Ie05bmF5bqm5bC65bqm77yM5Y+v5oyJ5ZCs5oSfL+e8lueggeWGjeiwgyAqLw0KICBzdGF0aWMgQklOX1NDQUxFID0gMC4wMg0KDQogIGNvbnN0cnVjdG9yKCkgew0KICAgIHN1cGVyKCkNCiAgICB0aGlzLl9zaWxlbnRQY20gPSBuZXcgRmxvYXQzMkFycmF5KE5vdEVtcHR5RmlsdGVyV29ya2xldFByb2Nlc3Nvci5QRVJJT0QpDQogICAgdGhpcy5fc2lsZW50T2ZmID0gTm90RW1wdHlGaWx0ZXJXb3JrbGV0UHJvY2Vzc29yLlBFUklPRA0KICB9DQoNCiAgc3RhdGljIGFsbElucHV0c1NpbGVudChpbnB1dHMpIHsNCiAgICBjb25zdCBlcHMgPSBOb3RFbXB0eUZpbHRlcldvcmtsZXRQcm9jZXNzb3IuU0lMRU5DRV9FUFMNCiAgICBmb3IgKGxldCBuaSA9IDA7IG5pIDwgaW5wdXRzLmxlbmd0aDsgbmkrKykgew0KICAgICAgY29uc3QgaW5wdXQgPSBpbnB1dHNbbmldDQogICAgICBpZiAoIWlucHV0KSBjb250aW51ZQ0KICAgICAgZm9yIChsZXQgY2ggPSAwOyBjaCA8IGlucHV0Lmxlbmd0aDsgY2grKykgew0KICAgICAgICBjb25zdCBjaGFubmVsID0gaW5wdXRbY2hdDQogICAgICAgIGlmICghY2hhbm5lbCkgY29udGludWUNCiAgICAgICAgY29uc3QgbiA9IGNoYW5uZWwubGVuZ3RoDQogICAgICAgIGZvciAobGV0IGkgPSAwOyBpIDwgbjsgaSsrKSB7DQogICAgICAgICAgaWYgKE1hdGguYWJzKGNoYW5uZWxbaV0pID49IGVwcykgcmV0dXJuIGZhbHNlDQogICAgICAgIH0NCiAgICAgIH0NCiAgICB9DQogICAgcmV0dXJuIHRydWUNCiAgfQ0KDQogIC8qKg0KICAgKiDku4UgYmluIDAuLjQg6Z2e6Zu277yI6ZqP5py677yJ77yM5ruh6Laz5a6e5L+h5Y+3IElERlTvvIzlhpnlhaUgdGhpcy5fc2lsZW50UGNtDQogICAqLw0KICByZWZpbGxTaWxlbnRQY21Gcm9tRml2ZUJpbnMoKSB7DQogICAgY29uc3QgTiA9IE5vdEVtcHR5RmlsdGVyV29ya2xldFByb2Nlc3Nvci5QRVJJT0QNCiAgICBjb25zdCBzY2FsZSA9IE5vdEVtcHR5RmlsdGVyV29ya2xldFByb2Nlc3Nvci5CSU5fU0NBTEUNCiAgICBjb25zdCByZSA9IG5ldyBGbG9hdDY0QXJyYXkoNSkNCiAgICBjb25zdCBpbSA9IG5ldyBGbG9hdDY0QXJyYXkoNSkNCiAgICByZVswXSA9IChNYXRoLnJhbmRvbSgpICogMiAtIDEpICogc2NhbGUNCiAgICBpbVswXSA9IDANCiAgICBmb3IgKGxldCBrID0gMTsgayA8PSA0OyBrKyspIHsNCiAgICAgIHJlW2tdID0gKE1hdGgucmFuZG9tKCkgKiAyIC0gMSkgKiBzY2FsZQ0KICAgICAgaW1ba10gPSAoTWF0aC5yYW5kb20oKSAqIDIgLSAxKSAqIHNjYWxlDQogICAgfQ0KICAgIGNvbnN0IGludk4gPSAxIC8gTg0KICAgIGNvbnN0IHR3b1BpT3Zlck4gPSAoMiAqIE1hdGguUEkpIC8gTg0KICAgIGZvciAobGV0IG4gPSAwOyBuIDwgTjsgbisrKSB7DQogICAgICBsZXQgcyA9IHJlWzBdDQogICAgICBmb3IgKGxldCBrID0gMTsgayA8PSA0OyBrKyspIHsNCiAgICAgICAgY29uc3QgYW5nID0gdHdvUGlPdmVyTiAqIGsgKiBuDQogICAgICAgIHMgKz0gMiAqIChyZVtrXSAqIE1hdGguY29zKGFuZykgLSBpbVtrXSAqIE1hdGguc2luKGFuZykpDQogICAgICB9DQogICAgICB0aGlzLl9zaWxlbnRQY21bbl0gPSBzICogaW52Tg0KICAgIH0NCiAgICB0aGlzLl9zaWxlbnRPZmYgPSAwDQogIH0NCg0KICBmaWxsT3V0cHV0c1NwYXJzZUZpdmVCaW5zKG91dHB1dHMpIHsNCiAgICBjb25zdCBmaXJzdCA9IG91dHB1dHNbMF0NCiAgICBpZiAoIWZpcnN0IHx8ICFmaXJzdFswXSkgcmV0dXJuDQogICAgY29uc3QgZnJhbWVMZW4gPSBmaXJzdFswXS5sZW5ndGgNCg0KICAgIGZvciAobGV0IGkgPSAwOyBpIDwgZnJhbWVMZW47IGkrKykgew0KICAgICAgaWYgKHRoaXMuX3NpbGVudE9mZiA+PSBOb3RFbXB0eUZpbHRlcldvcmtsZXRQcm9jZXNzb3IuUEVSSU9EKSB7DQogICAgICAgIHRoaXMucmVmaWxsU2lsZW50UGNtRnJvbUZpdmVCaW5zKCkNCiAgICAgIH0NCiAgICAgIGNvbnN0IHYgPSB0aGlzLl9zaWxlbnRQY21bdGhpcy5fc2lsZW50T2ZmKytdDQogICAgICBmb3IgKGxldCBubyA9IDA7IG5vIDwgb3V0cHV0cy5sZW5ndGg7IG5vKyspIHsNCiAgICAgICAgY29uc3Qgb3V0UG9ydHMgPSBvdXRwdXRzW25vXQ0KICAgICAgICBpZiAoIW91dFBvcnRzKSBjb250aW51ZQ0KICAgICAgICBmb3IgKGxldCBjaCA9IDA7IGNoIDwgb3V0UG9ydHMubGVuZ3RoOyBjaCsrKSB7DQogICAgICAgICAgY29uc3QgY2hhbm5lbCA9IG91dFBvcnRzW2NoXQ0KICAgICAgICAgIGlmIChjaGFubmVsKSBjaGFubmVsW2ldID0gdg0KICAgICAgICB9DQogICAgICB9DQogICAgfQ0KICB9DQoNCiAgc3RhdGljIGNvcHlJbnB1dHNUb091dHB1dHMoaW5wdXRzLCBvdXRwdXRzKSB7DQogICAgZm9yIChsZXQgbm8gPSAwOyBubyA8IG91dHB1dHMubGVuZ3RoOyBubysrKSB7DQogICAgICBjb25zdCBvdXRQb3J0cyA9IG91dHB1dHNbbm9dDQogICAgICBjb25zdCBpblBvcnRzID0gaW5wdXRzW25vXQ0KICAgICAgaWYgKCFvdXRQb3J0cykgY29udGludWUNCiAgICAgIGZvciAobGV0IGNoID0gMDsgY2ggPCBvdXRQb3J0cy5sZW5ndGg7IGNoKyspIHsNCiAgICAgICAgY29uc3Qgb3V0Q2ggPSBvdXRQb3J0c1tjaF0NCiAgICAgICAgaWYgKCFvdXRDaCkgY29udGludWUNCiAgICAgICAgY29uc3QgaW5DaCA9IGluUG9ydHMgJiYgaW5Qb3J0c1tjaF0NCiAgICAgICAgaWYgKGluQ2ggJiYgaW5DaC5sZW5ndGggPT09IG91dENoLmxlbmd0aCkgew0KICAgICAgICAgIG91dENoLnNldChpbkNoKQ0KICAgICAgICB9IGVsc2UgaWYgKGluQ2gpIHsNCiAgICAgICAgICBjb25zdCBuID0gTWF0aC5taW4oaW5DaC5sZW5ndGgsIG91dENoLmxlbmd0aCkNCiAgICAgICAgICBmb3IgKGxldCBpID0gMDsgaSA8IG47IGkrKykgb3V0Q2hbaV0gPSBpbkNoW2ldDQogICAgICAgICAgZm9yIChsZXQgaSA9IG47IGkgPCBvdXRDaC5sZW5ndGg7IGkrKykgb3V0Q2hbaV0gPSAwDQogICAgICAgIH0gZWxzZSB7DQogICAgICAgICAgb3V0Q2guZmlsbCgwKQ0KICAgICAgICB9DQogICAgICB9DQogICAgfQ0KICB9DQoNCiAgcHJvY2VzcyhpbnB1dHMsIG91dHB1dHMpIHsNCiAgICBpZiAodGhpcy5pc0Rlc3Ryb3kpIHJldHVybiBmYWxzZQ0KDQogICAgaWYgKHRoaXMuZGVidWcpIHsNCiAgICAgIGNvbnNvbGUubG9nKCdceDFiWzM4OzI7MDsxNTE7MjU1bSVjJXNceDFiWzBtJywgJ2NvbG9yOiMwMDk3ZmY7JywgJy0tLS0tLS0+QnJlYXRoZTogaW5wdXRzJywgaW5wdXRzKQ0KICAgIH0NCg0KICAgIGlmIChOb3RFbXB0eUZpbHRlcldvcmtsZXRQcm9jZXNzb3IuYWxsSW5wdXRzU2lsZW50KGlucHV0cykpIHsNCiAgICAgIHRoaXMuZmlsbE91dHB1dHNTcGFyc2VGaXZlQmlucyhvdXRwdXRzKQ0KICAgIH0gZWxzZSB7DQogICAgICB0aGlzLl9zaWxlbnRPZmYgPSBOb3RFbXB0eUZpbHRlcldvcmtsZXRQcm9jZXNzb3IuUEVSSU9EDQogICAgICBOb3RFbXB0eUZpbHRlcldvcmtsZXRQcm9jZXNzb3IuY29weUlucHV0c1RvT3V0cHV0cyhpbnB1dHMsIG91dHB1dHMpDQogICAgfQ0KDQogICAgcmV0dXJuIHRydWUNCiAgfQ0KDQogIGRlc3Ryb3koKSB7DQogICAgdGhpcy5pc0Rlc3Ryb3kgPSB0cnVlDQogIH0NCn0NCg0KcmVnaXN0ZXJQcm9jZXNzb3IoJ25vdC1lbXB0eS1maWx0ZXItcHJvY2Vzc29yJywgTm90RW1wdHlGaWx0ZXJXb3JrbGV0UHJvY2Vzc29yKQ0K", g = class {
|
|
170
167
|
audioContext = new AudioContext();
|
|
171
168
|
notEmptyFilterWorkletNode;
|
|
172
169
|
constructor() {}
|
|
@@ -184,6 +181,7 @@ var p = "pr-player-render", m = class {
|
|
|
184
181
|
outputGain = 1;
|
|
185
182
|
mixAudioMap = /* @__PURE__ */ new Map();
|
|
186
183
|
audioContext = new AudioContext();
|
|
184
|
+
isExternalAudioContext = !1;
|
|
187
185
|
sourceNode;
|
|
188
186
|
inputGainNode;
|
|
189
187
|
enhanceGainNode;
|
|
@@ -203,7 +201,7 @@ var p = "pr-player-render", m = class {
|
|
|
203
201
|
notEmptyFilterWorkletNode;
|
|
204
202
|
filterStream = (e) => e;
|
|
205
203
|
constructor(e, t) {
|
|
206
|
-
this.inputStream = e, t && (this.audioContext = t), this.sourceNode = this.audioContext.createMediaStreamSource(this.inputStream), this.inputGainNode = this.audioContext.createGain(), this.inputGainNode.gain.setValueAtTime(this.inputGain, this.audioContext.currentTime), this.enhanceGainNode = this.audioContext.createGain(), this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain, this.audioContext.currentTime), this.bgsGainNode = this.audioContext.createGain(), this.bgsGainNode.gain.setValueAtTime(this.bgsGain, this.audioContext.currentTime), this.bgmGainNode = this.audioContext.createGain(), this.bgmGainNode.gain.setValueAtTime(this.bgmGain, this.audioContext.currentTime), this.analyserNode = this.audioContext.createAnalyser(), this.analyserNode.fftSize = 512, this.analyserArrayData = new Uint8Array(this.analyserNode.frequencyBinCount), this.outputGainNode = this.audioContext.createGain(), this.outputGainNode.gain.setValueAtTime(this.outputGain, this.audioContext.currentTime), this.destinationNode = this.audioContext.createMediaStreamDestination(), this.outputStream = this.destinationNode.stream;
|
|
204
|
+
this.inputStream = e, t && (this.audioContext = t, this.isExternalAudioContext = !0), this.sourceNode = this.audioContext.createMediaStreamSource(this.inputStream), this.inputGainNode = this.audioContext.createGain(), this.inputGainNode.gain.setValueAtTime(this.inputGain, this.audioContext.currentTime), this.enhanceGainNode = this.audioContext.createGain(), this.enhanceGainNode.gain.setValueAtTime(this.enhanceGain, this.audioContext.currentTime), this.bgsGainNode = this.audioContext.createGain(), this.bgsGainNode.gain.setValueAtTime(this.bgsGain, this.audioContext.currentTime), this.bgmGainNode = this.audioContext.createGain(), this.bgmGainNode.gain.setValueAtTime(this.bgmGain, this.audioContext.currentTime), this.analyserNode = this.audioContext.createAnalyser(), this.analyserNode.fftSize = 512, this.analyserArrayData = new Uint8Array(this.analyserNode.frequencyBinCount), this.outputGainNode = this.audioContext.createGain(), this.outputGainNode.gain.setValueAtTime(this.outputGain, this.audioContext.currentTime), this.destinationNode = this.audioContext.createMediaStreamDestination(), this.outputStream = this.destinationNode.stream;
|
|
207
205
|
{
|
|
208
206
|
let { sourceNode: e, inputGainNode: t, enhanceGainNode: n, bgsGainNode: r, bgmGainNode: i, analyserNode: a, outputGainNode: o, destinationNode: s } = this;
|
|
209
207
|
e.connect(t), t.connect(n), n.connect(a), r.connect(a), i.connect(a), n.connect(s), r.connect(s), i.connect(s), o.connect(this.audioContext.destination);
|
|
@@ -284,6 +282,13 @@ var p = "pr-player-render", m = class {
|
|
|
284
282
|
let n = e === "bgs" ? this.bgsGainNode : this.bgmGainNode;
|
|
285
283
|
t ? n.connect(this.destinationNode) : n.disconnect(this.destinationNode);
|
|
286
284
|
};
|
|
285
|
+
destroy = async () => {
|
|
286
|
+
for (let e of this.mixAudioMap.values()) e.stop(), e.disconnect();
|
|
287
|
+
this.mixAudioMap.clear(), this.notEmptyFilterWorklet.destroy(), this.rnnoiseWorklet?.destroy(), this.notEmptyFilterWorkletNode = void 0, this.rnnoiseWorkletNode = void 0, this.sourceNode.disconnect(), this.inputGainNode.disconnect(), this.enhanceGainNode.disconnect(), this.bgsGainNode.disconnect(), this.bgmGainNode.disconnect(), this.analyserNode.disconnect(), this.outputGainNode.disconnect();
|
|
288
|
+
for (let e of this.inputStream.getTracks()) e.stop(), this.inputStream.removeTrack(e);
|
|
289
|
+
for (let e of this.outputStream.getTracks()) e.stop(), this.outputStream.removeTrack(e);
|
|
290
|
+
this.notEmpty = !1, this.denoise = !1, this.mute = !0, !this.isExternalAudioContext && this.audioContext.state !== "closed" && await this.audioContext.close();
|
|
291
|
+
};
|
|
287
292
|
}, v = async (e, t) => {
|
|
288
293
|
try {
|
|
289
294
|
let { format: n, numberOfChannels: r, numberOfFrames: i, sampleRate: a } = t, o = e.createBuffer(r, i, a);
|
|
@@ -335,18 +340,18 @@ var p = "pr-player-render", m = class {
|
|
|
335
340
|
i.buffer = r, i.playbackRate.value = n;
|
|
336
341
|
let a = -1200 * Math.log2(n);
|
|
337
342
|
i.detune.value = a, i.connect(this.destination);
|
|
338
|
-
let o = Math.max(this.nextStartTime, this.audioContext.currentTime);
|
|
339
|
-
this.nextStartTime = o +
|
|
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 = () => {
|
|
340
345
|
this.pendingSources = this.pendingSources.filter((e) => e !== i);
|
|
341
346
|
}, this.audioContext.state === "suspended" && await this.audioContext.resume();
|
|
342
347
|
}
|
|
343
348
|
getStream = () => this.prAudioStream?.getStream();
|
|
344
349
|
destroy() {
|
|
345
|
-
this.audioContext?.close(), this.audioContext = void 0, this.destination = void 0, this.nextStartTime = 0, this.prAudioStream?.stop(), this.pendingSources.forEach((e) => e.stop()), this.pendingSources = [];
|
|
350
|
+
this.audioContext?.close(), this.audioContext = void 0, this.destination = void 0, this.nextStartTime = 0, this.prAudioStream?.stop(), this.prAudioStream?.destroy(), this.pendingSources.forEach((e) => e.stop()), this.pendingSources = [];
|
|
346
351
|
}
|
|
347
352
|
}, b = (e) => {
|
|
348
353
|
let t = e.toLowerCase();
|
|
349
|
-
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";
|
|
350
355
|
}, x = (e) => {
|
|
351
356
|
let t = e?.getTracks() || [];
|
|
352
357
|
for (let e of t) e.stop();
|
|
@@ -386,7 +391,7 @@ var p = "pr-player-render", m = class {
|
|
|
386
391
|
let n = new Uint8Array(e), r = new Uint8Array(t), i = new Uint8Array(11 + n.length + r.length), a = 0;
|
|
387
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;
|
|
388
393
|
}, w = (e) => {
|
|
389
|
-
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);
|
|
390
395
|
if (t += 1, r !== 1) throw Error("Invalid AVC version");
|
|
391
396
|
let i = n.getUint8(t) & 255;
|
|
392
397
|
t += 1;
|
|
@@ -398,32 +403,21 @@ var p = "pr-player-render", m = class {
|
|
|
398
403
|
i,
|
|
399
404
|
a,
|
|
400
405
|
o
|
|
401
|
-
], (e) => e.toString(16).padStart(2, "0")).join("")}
|
|
402
|
-
t += 1;
|
|
403
|
-
let
|
|
404
|
-
t += 1;
|
|
405
|
-
let u = n.getUint16(t, !1);
|
|
406
|
-
t += 2;
|
|
407
|
-
let d = new Uint8Array(n.buffer.slice(t, t + u));
|
|
408
|
-
t += u;
|
|
409
|
-
let f = n.getUint8(t) & 31;
|
|
410
|
-
t += 1;
|
|
411
|
-
let p = 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);
|
|
412
409
|
t += 2;
|
|
413
|
-
let
|
|
414
|
-
|
|
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, {
|
|
415
414
|
version: r,
|
|
416
415
|
codec: s,
|
|
417
416
|
profile: i,
|
|
418
417
|
compatibility: a,
|
|
419
418
|
level: o,
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
sequenceParameterSetLength: u,
|
|
423
|
-
sps: d,
|
|
424
|
-
numOfPictureParameterSets: f,
|
|
425
|
-
pictureParameterSetLength: p,
|
|
426
|
-
pps: m
|
|
419
|
+
sps: l,
|
|
420
|
+
pps: new Uint8Array(e.buffer, e.byteOffset + t, u)
|
|
427
421
|
};
|
|
428
422
|
}, T = (e) => {
|
|
429
423
|
let t = new Uint8Array(4 + e.length);
|
|
@@ -432,32 +426,104 @@ var p = "pr-player-render", m = class {
|
|
|
432
426
|
let t = 0;
|
|
433
427
|
for (let n of e) t += n.length;
|
|
434
428
|
let n = new Uint8Array(t), r = 0;
|
|
435
|
-
for (let t of e)
|
|
436
|
-
let e = t;
|
|
437
|
-
n.set(e, r), r += e.length;
|
|
438
|
-
}
|
|
429
|
+
for (let t of e) n.set(t, r), r += t.length;
|
|
439
430
|
return n;
|
|
440
431
|
}, D = (e) => {
|
|
441
|
-
let t = new DataView(e.buffer), n = 0, r
|
|
442
|
-
|
|
443
|
-
{
|
|
444
|
-
|
|
445
|
-
i
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
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;
|
|
450
462
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
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;
|
|
454
489
|
}
|
|
455
490
|
return {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
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
|
|
459
525
|
};
|
|
460
|
-
},
|
|
526
|
+
}, L = class {
|
|
461
527
|
#e = {
|
|
462
528
|
timeout: 5 * 1e3,
|
|
463
529
|
check: !1
|
|
@@ -516,14 +582,11 @@ var p = "pr-player-render", m = class {
|
|
|
516
582
|
e.name = "AbortError", this.#t.abort(e);
|
|
517
583
|
}
|
|
518
584
|
};
|
|
519
|
-
},
|
|
520
|
-
option = {
|
|
521
|
-
debug: !1,
|
|
522
|
-
frameTrack: !1
|
|
523
|
-
};
|
|
585
|
+
}, R = class {
|
|
586
|
+
option = { frameTrack: !1 };
|
|
524
587
|
converter = "MediaStreamTrackGenerator" in window ? "generator" : "canvas";
|
|
525
|
-
prFetch = new
|
|
526
|
-
getSegmentsFetch = new
|
|
588
|
+
prFetch = new L();
|
|
589
|
+
getSegmentsFetch = new L();
|
|
527
590
|
prResolves = new S();
|
|
528
591
|
url = "";
|
|
529
592
|
start_resolve;
|
|
@@ -539,8 +602,7 @@ var p = "pr-player-render", m = class {
|
|
|
539
602
|
cutRenders = /* @__PURE__ */ new Map();
|
|
540
603
|
trackGenerator;
|
|
541
604
|
constructor(e = {}) {
|
|
542
|
-
|
|
543
|
-
this.option.debug = t;
|
|
605
|
+
this.option.frameTrack = e.frameTrack ?? !1;
|
|
544
606
|
}
|
|
545
607
|
start = async (e) => {
|
|
546
608
|
await this.stop(), this.url = e;
|
|
@@ -553,11 +615,18 @@ var p = "pr-player-render", m = class {
|
|
|
553
615
|
case "hls":
|
|
554
616
|
this.hls.start();
|
|
555
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.");
|
|
556
625
|
}
|
|
557
626
|
};
|
|
558
627
|
stop = async () => {
|
|
559
628
|
try {
|
|
560
|
-
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();
|
|
561
630
|
} catch (e) {
|
|
562
631
|
console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->pr-player: error", e);
|
|
563
632
|
}
|
|
@@ -612,21 +681,20 @@ var p = "pr-player-render", m = class {
|
|
|
612
681
|
this.initRender(), this.initDecoder(e), this.initDemuxer(e), this.audioPlayer = new y(), this.audioPlayer.init();
|
|
613
682
|
};
|
|
614
683
|
initDemuxer = (e) => {
|
|
615
|
-
this.demuxerWorker = new i(), this.demuxerWorker.init(e), this.demuxerWorker.on.
|
|
616
|
-
this.
|
|
617
|
-
}, this.demuxerWorker.on.info = (e) => {
|
|
618
|
-
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);
|
|
684
|
+
this.demuxerWorker = new i(), this.demuxerWorker.init(e), this.demuxerWorker.on.info = (e) => {
|
|
685
|
+
this.on.demuxer.info && this.on.demuxer.info(e);
|
|
619
686
|
}, this.demuxerWorker.on.config = (e) => {
|
|
620
|
-
this.
|
|
687
|
+
this.on.demuxer.config && this.on.demuxer.config(e);
|
|
621
688
|
let { kind: t } = e;
|
|
622
689
|
switch (t) {
|
|
623
690
|
case "audio":
|
|
624
691
|
{
|
|
625
|
-
let { codec: t, sampleRate: n, numberOfChannels: r } = e;
|
|
692
|
+
let { codec: t, sampleRate: n, numberOfChannels: r, description: i } = e;
|
|
626
693
|
this.decoderWorker?.initAudio({
|
|
627
694
|
codec: t,
|
|
628
695
|
sampleRate: n,
|
|
629
|
-
numberOfChannels: r
|
|
696
|
+
numberOfChannels: r,
|
|
697
|
+
...i ? { description: i } : {}
|
|
630
698
|
});
|
|
631
699
|
}
|
|
632
700
|
break;
|
|
@@ -677,11 +745,11 @@ var p = "pr-player-render", m = class {
|
|
|
677
745
|
initDecoder = (e) => {
|
|
678
746
|
this.decoderWorker = new l(), this.decoderWorker.init(e);
|
|
679
747
|
let { frameTrack: t = !1 } = this.option;
|
|
680
|
-
this.decoderWorker.setFrameTrack(t), this.decoderWorker.on.audio.
|
|
748
|
+
this.decoderWorker.setFrameTrack(t), this.decoderWorker.on.audio.error = (e) => {
|
|
749
|
+
this.on.error && this.on.error(e);
|
|
750
|
+
}, this.decoderWorker.on.audio.decode = (e) => {
|
|
681
751
|
this.audioPlayer?.push(e), this.on.decoder.audio && this.on.decoder.audio(e);
|
|
682
|
-
}, this.decoderWorker.on.
|
|
683
|
-
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);
|
|
684
|
-
}, this.decoderWorker.on.video.decode = async (e) => {
|
|
752
|
+
}, this.decoderWorker.on.video.decode = (e) => {
|
|
685
753
|
this.start_resolve &&= (this.start_resolve(!0), void 0), this.renderWorker?.push(e), this.on.decoder.video && this.on.decoder.video(e);
|
|
686
754
|
}, this.decoderWorker.on.video.error = (e) => {
|
|
687
755
|
this.on.error && this.on.error(e);
|
|
@@ -798,9 +866,167 @@ var p = "pr-player-render", m = class {
|
|
|
798
866
|
}
|
|
799
867
|
})
|
|
800
868
|
};
|
|
801
|
-
|
|
869
|
+
mp4 = { start: () => new Promise(async (e, t) => {
|
|
870
|
+
try {
|
|
871
|
+
this.start_resolve = e;
|
|
872
|
+
let n, r = 0;
|
|
873
|
+
for (;;) {
|
|
874
|
+
r += 1;
|
|
875
|
+
try {
|
|
876
|
+
n = await this.prFetch.request(this.url);
|
|
877
|
+
} catch (e) {
|
|
878
|
+
console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
|
|
879
|
+
}
|
|
880
|
+
if (n?.status === 200 || r === 3) break;
|
|
881
|
+
await new Promise((e) => setTimeout(() => e(!0), 500));
|
|
882
|
+
}
|
|
883
|
+
if (!n || n.status !== 200) return t("request is error.");
|
|
884
|
+
let i = n.body?.getReader();
|
|
885
|
+
if (!i) return t("reader is error.");
|
|
886
|
+
for (this.decoderWorker?.setFrameTrack(!1);;) {
|
|
887
|
+
let { done: e, value: t } = await i.read();
|
|
888
|
+
if (t && this.demuxerWorker?.push(t), e || this.url === "") break;
|
|
889
|
+
}
|
|
890
|
+
} catch (e) {
|
|
891
|
+
e.name !== "AbortError" && t(e);
|
|
892
|
+
}
|
|
893
|
+
}) };
|
|
894
|
+
dash = {
|
|
895
|
+
isLive: !1,
|
|
896
|
+
segmentNumber: 1,
|
|
897
|
+
mpdInfo: null,
|
|
898
|
+
getSegmentsTimer: 0,
|
|
899
|
+
fetchSegment: async (e) => {
|
|
900
|
+
let t = await this.prFetch.request(e);
|
|
901
|
+
if (!t || t.status !== 200) return !1;
|
|
902
|
+
let n = t.body?.getReader();
|
|
903
|
+
if (!n) return !1;
|
|
904
|
+
for (;;) {
|
|
905
|
+
let { done: e, value: t } = await n.read();
|
|
906
|
+
if (t && this.demuxerWorker?.push(t), e || this.url === "") break;
|
|
907
|
+
}
|
|
908
|
+
return !0;
|
|
909
|
+
},
|
|
910
|
+
fetchByteRange: async (e, t, n) => {
|
|
911
|
+
let r, i = 0;
|
|
912
|
+
for (;;) {
|
|
913
|
+
i += 1;
|
|
914
|
+
try {
|
|
915
|
+
r = await fetch(e, { headers: { Range: `bytes=${t}-${n}` } });
|
|
916
|
+
} catch (e) {
|
|
917
|
+
console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
|
|
918
|
+
}
|
|
919
|
+
if (r?.ok && (r.status === 200 || r.status === 206) || i === 3) break;
|
|
920
|
+
await new Promise((e) => setTimeout(() => e(!0), 500));
|
|
921
|
+
}
|
|
922
|
+
if (!r?.ok || r.status !== 200 && r.status !== 206) return !1;
|
|
923
|
+
let a = new Uint8Array(await r.arrayBuffer());
|
|
924
|
+
return a.byteLength > 0 && this.demuxerWorker?.push(a), a.byteLength > 0;
|
|
925
|
+
},
|
|
926
|
+
getMpd: async () => {
|
|
927
|
+
try {
|
|
928
|
+
let e, t = 0;
|
|
929
|
+
for (;;) {
|
|
930
|
+
t += 1;
|
|
931
|
+
try {
|
|
932
|
+
e = await this.getSegmentsFetch.request(this.url);
|
|
933
|
+
} catch (e) {
|
|
934
|
+
console.log("\x1B[38;2;0;151;255m%c%s\x1B[0m", "color:#0097ff;", "------->Breathe: error", e);
|
|
935
|
+
}
|
|
936
|
+
if (e?.status === 200 || t === 3) break;
|
|
937
|
+
await new Promise((e) => setTimeout(() => e(!0), 500));
|
|
938
|
+
}
|
|
939
|
+
if (!e || e.status !== 200) throw Error("request is error.");
|
|
940
|
+
let n = e.body?.getReader();
|
|
941
|
+
if (!n) throw Error("reader is error.");
|
|
942
|
+
let r = [];
|
|
943
|
+
for (;;) {
|
|
944
|
+
let { done: e, value: t } = await n.read();
|
|
945
|
+
if (t && r.push(t), e || this.url === "") break;
|
|
946
|
+
}
|
|
947
|
+
let i = r.reduce((e, t) => e + t.byteLength, 0), a = new Uint8Array(i), o = 0;
|
|
948
|
+
for (let e of r) a.set(e, o), o += e.byteLength;
|
|
949
|
+
let s = I(new TextDecoder("utf-8").decode(a), this.url);
|
|
950
|
+
return this.dash.mpdInfo = s, this.dash.isLive = s.isLive, this.dash.isLive || (this.option.frameTrack = !1), s;
|
|
951
|
+
} catch (e) {
|
|
952
|
+
return e.name !== "AbortError" && this.on.error && this.on.error(e), null;
|
|
953
|
+
}
|
|
954
|
+
},
|
|
955
|
+
start: () => new Promise(async (e, t) => {
|
|
956
|
+
try {
|
|
957
|
+
this.start_resolve = e, this.dash.segmentNumber = 1, this.dash.mpdInfo = null;
|
|
958
|
+
let n = await this.dash.getMpd();
|
|
959
|
+
if (!n) return t("mpd parse is error.");
|
|
960
|
+
let r = n.adaptations.find((e) => e.kind === "video" || e.kind === "mux")?.representation;
|
|
961
|
+
if (r?.width && r?.height) {
|
|
962
|
+
let e = {
|
|
963
|
+
width: r.width,
|
|
964
|
+
height: r.height,
|
|
965
|
+
duration: n.duration
|
|
966
|
+
};
|
|
967
|
+
this.on.demuxer.info && this.on.demuxer.info(e);
|
|
968
|
+
}
|
|
969
|
+
let i = n.adaptations.find((e) => e.representation.segmentList);
|
|
970
|
+
if (i?.representation.segmentList) {
|
|
971
|
+
let { segmentList: e, baseUrl: r } = i.representation, a = j(n.baseUrl, r || "");
|
|
972
|
+
if (e.initRange) {
|
|
973
|
+
let { start: n, end: r } = e.initRange;
|
|
974
|
+
if (!await this.dash.fetchByteRange(a, n, r)) return t("dash init segment fetch failed.");
|
|
975
|
+
}
|
|
976
|
+
this.decoderWorker?.setFrameTrack(!1);
|
|
977
|
+
for (let t of e.segments) {
|
|
978
|
+
if (this.url === "") break;
|
|
979
|
+
await this.dash.fetchByteRange(a, t.start, t.end);
|
|
980
|
+
}
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
let a = [
|
|
984
|
+
n.adaptations.find((e) => e.kind === "video"),
|
|
985
|
+
n.adaptations.find((e) => e.kind === "audio"),
|
|
986
|
+
n.adaptations.find((e) => e.kind === "mux")
|
|
987
|
+
].filter((e) => !!e);
|
|
988
|
+
if (a.length === 0) return t("no adaptation in mpd.");
|
|
989
|
+
for (let e of a) {
|
|
990
|
+
let { id: t, bandwidth: r, template: i } = e.representation;
|
|
991
|
+
if (!i?.initialization) continue;
|
|
992
|
+
let a = N(n.baseUrl, i.initialization, {
|
|
993
|
+
RepresentationID: t,
|
|
994
|
+
Bandwidth: r,
|
|
995
|
+
Number: this.dash.segmentNumber
|
|
996
|
+
});
|
|
997
|
+
await this.dash.fetchSegment(a);
|
|
998
|
+
}
|
|
999
|
+
for (this.dash.isLive ? this.dash.getSegmentsTimer = window.setInterval(this.dash.getMpd, 3e3) : this.decoderWorker?.setFrameTrack(!1);;) {
|
|
1000
|
+
let e = this.dash.mpdInfo;
|
|
1001
|
+
if (!e || this.url === "") break;
|
|
1002
|
+
let { baseUrl: t } = e, n = !1;
|
|
1003
|
+
for (let e of a) {
|
|
1004
|
+
let { id: r, bandwidth: i, template: a } = e.representation;
|
|
1005
|
+
if (!a?.media) continue;
|
|
1006
|
+
let o = N(t, a.media, {
|
|
1007
|
+
RepresentationID: r,
|
|
1008
|
+
Bandwidth: i,
|
|
1009
|
+
Number: this.dash.segmentNumber
|
|
1010
|
+
});
|
|
1011
|
+
await this.dash.fetchSegment(o) && (n = !0);
|
|
1012
|
+
}
|
|
1013
|
+
if (!n) {
|
|
1014
|
+
if (this.dash.isLive) {
|
|
1015
|
+
await new Promise((e) => setTimeout(() => e(!0), 300));
|
|
1016
|
+
continue;
|
|
1017
|
+
}
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
1020
|
+
this.dash.segmentNumber += 1;
|
|
1021
|
+
}
|
|
1022
|
+
} catch (e) {
|
|
1023
|
+
e.name !== "AbortError" && t(e);
|
|
1024
|
+
}
|
|
1025
|
+
})
|
|
1026
|
+
};
|
|
1027
|
+
}, z = class {
|
|
802
1028
|
pendingPayloads = [];
|
|
803
|
-
payload = new Uint8Array();
|
|
1029
|
+
payload = /* @__PURE__ */ new Uint8Array();
|
|
804
1030
|
chunks = [];
|
|
805
1031
|
push = (e) => {
|
|
806
1032
|
this.pendingPayloads.push(e);
|
|
@@ -815,119 +1041,477 @@ var p = "pr-player-render", m = class {
|
|
|
815
1041
|
pushChunk = (e) => {
|
|
816
1042
|
this.chunks.push(e), this.chunks.length > 100 && this.chunks.shift();
|
|
817
1043
|
};
|
|
818
|
-
|
|
819
|
-
this.
|
|
1044
|
+
discardPayload = () => {
|
|
1045
|
+
this.payload = /* @__PURE__ */ new Uint8Array();
|
|
820
1046
|
};
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
return t;
|
|
843
|
-
}, M = class {
|
|
844
|
-
pat;
|
|
845
|
-
pmt;
|
|
1047
|
+
destroy = () => {
|
|
1048
|
+
this.pendingPayloads = [], this.payload = /* @__PURE__ */ new Uint8Array(), this.chunks = [];
|
|
1049
|
+
};
|
|
1050
|
+
}, B = (e, t) => e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), V = (e, t) => {
|
|
1051
|
+
let n = B(e, t);
|
|
1052
|
+
return n & 8388608 && (n |= -16777216), n;
|
|
1053
|
+
}, H = (e, t) => t << 24 | e, U = [
|
|
1054
|
+
96e3,
|
|
1055
|
+
88200,
|
|
1056
|
+
64e3,
|
|
1057
|
+
48e3,
|
|
1058
|
+
44100,
|
|
1059
|
+
32e3,
|
|
1060
|
+
24e3,
|
|
1061
|
+
22050,
|
|
1062
|
+
16e3,
|
|
1063
|
+
12e3,
|
|
1064
|
+
11025,
|
|
1065
|
+
8e3,
|
|
1066
|
+
7350
|
|
1067
|
+
], W = class {
|
|
846
1068
|
audioConfig;
|
|
847
1069
|
videoConfig;
|
|
848
|
-
|
|
1070
|
+
header;
|
|
1071
|
+
textDecoder = new TextDecoder("utf-8");
|
|
849
1072
|
on = {};
|
|
850
1073
|
constructor() {}
|
|
851
1074
|
parse = async (e) => {
|
|
852
1075
|
let t = 0;
|
|
853
|
-
for (
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
return t;
|
|
861
|
-
};
|
|
862
|
-
parsePacket = async (e, t) => {
|
|
863
|
-
if (t + 188 > e.byteLength || e.getUint8(t) !== 71) throw Error("Invalid TS packet");
|
|
864
|
-
let n = t, r = this.parseHeader(e, n);
|
|
865
|
-
n += 4;
|
|
866
|
-
let { transport_error_indicator: i, pid: a, payload_unit_start_indicator: o, adaptation_field_control: s } = r;
|
|
867
|
-
if (i === 1 || a === void 0) return;
|
|
868
|
-
let c = 184;
|
|
869
|
-
if (s === 2 || s === 3) {
|
|
870
|
-
let t = e.getUint8(n);
|
|
871
|
-
n += 1, this.parseAdaptationField(e, n), n += t, c -= t;
|
|
872
|
-
}
|
|
873
|
-
if (s === 3 && --c, s === 1 || s === 3) {
|
|
874
|
-
let t = new Uint8Array(e.buffer.slice(n, n + c));
|
|
875
|
-
if (a === 0) return this.parsePAT(e, n);
|
|
876
|
-
{
|
|
877
|
-
let { programs: t = [] } = this.pat || {};
|
|
878
|
-
if (t.find((e) => e.pmt_pid === a)) return this.parsePMT(e, n);
|
|
879
|
-
}
|
|
880
|
-
let { streams: r = [] } = this.pmt || {}, i = r.find((e) => e.elementary_pid === a);
|
|
881
|
-
if (i) {
|
|
882
|
-
if (o === 1) {
|
|
883
|
-
let e = this.payloadMap.get(a);
|
|
884
|
-
if (e) {
|
|
885
|
-
switch (i.kind) {
|
|
886
|
-
case "audio":
|
|
887
|
-
{
|
|
888
|
-
let t = await this.parseAudio(e);
|
|
889
|
-
this.on.chunk && this.on.chunk(t);
|
|
890
|
-
}
|
|
891
|
-
break;
|
|
892
|
-
case "video":
|
|
893
|
-
{
|
|
894
|
-
let t = await this.parseVideo(e);
|
|
895
|
-
this.on.chunk && this.on.chunk(t), await new Promise((e) => setTimeout(() => e(!0), 0));
|
|
896
|
-
}
|
|
897
|
-
break;
|
|
898
|
-
}
|
|
899
|
-
this.payloadMap.delete(a);
|
|
900
|
-
}
|
|
1076
|
+
for (this.header || (this.parseHeader(e, t), t += 9); this.isSurplusTag(e, t) !== !1;) {
|
|
1077
|
+
let { tagType: n, dataSize: r, dts: i } = this.parseTagHeader(e, t + 4);
|
|
1078
|
+
if (n) {
|
|
1079
|
+
let a = this.parseTagBody(n, e, t + 4 + 11, r);
|
|
1080
|
+
if (!a) {
|
|
1081
|
+
t = t + 4 + 11 + r, await new Promise((e) => setTimeout(() => e(!0), 8));
|
|
1082
|
+
continue;
|
|
901
1083
|
}
|
|
902
|
-
{
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
1084
|
+
switch (n) {
|
|
1085
|
+
case "script":
|
|
1086
|
+
this.on.info && this.on.info(a);
|
|
1087
|
+
break;
|
|
1088
|
+
case "audio":
|
|
1089
|
+
{
|
|
1090
|
+
let { accPacketType: e } = a;
|
|
1091
|
+
if (e === 0) {
|
|
1092
|
+
let { codec: e, sampleRate: t, channelConfiguration: n, data: r } = a;
|
|
1093
|
+
this.audioConfig = {
|
|
1094
|
+
kind: "audio",
|
|
1095
|
+
codec: e,
|
|
1096
|
+
sampleRate: t,
|
|
1097
|
+
numberOfChannels: n,
|
|
1098
|
+
...r ? { description: r } : {}
|
|
1099
|
+
}, this.on.config && this.on.config(this.audioConfig);
|
|
1100
|
+
} else {
|
|
1101
|
+
let { cts: e, data: t } = a, n = e === void 0 ? void 0 : e + i;
|
|
1102
|
+
this.on.chunk && this.on.chunk({
|
|
1103
|
+
kind: "audio",
|
|
1104
|
+
type: "key",
|
|
1105
|
+
dts: i,
|
|
1106
|
+
pts: n,
|
|
1107
|
+
cts: e,
|
|
1108
|
+
data: t
|
|
1109
|
+
});
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
break;
|
|
1113
|
+
case "video":
|
|
1114
|
+
{
|
|
1115
|
+
let { avcPacketType: e } = a;
|
|
1116
|
+
if (e === 0) {
|
|
1117
|
+
let { codec: e, sps: t, pps: n, data: r } = a;
|
|
1118
|
+
this.videoConfig = {
|
|
1119
|
+
kind: "video",
|
|
1120
|
+
codec: e,
|
|
1121
|
+
description: r,
|
|
1122
|
+
sps: t,
|
|
1123
|
+
pps: n
|
|
1124
|
+
}, this.on.config && this.on.config(this.videoConfig);
|
|
1125
|
+
} else {
|
|
1126
|
+
let { frameType: e, cts: t, data: n, nalus: r } = a, o = e === 1 ? "key" : "delta", s = t === void 0 ? void 0 : t + i;
|
|
1127
|
+
this.on.chunk && this.on.chunk({
|
|
1128
|
+
kind: "video",
|
|
1129
|
+
type: o,
|
|
1130
|
+
dts: i,
|
|
1131
|
+
pts: s,
|
|
1132
|
+
cts: t,
|
|
1133
|
+
data: n,
|
|
1134
|
+
nalus: r
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
break;
|
|
906
1139
|
}
|
|
1140
|
+
t = t + 4 + 11 + r;
|
|
907
1141
|
}
|
|
1142
|
+
await new Promise((e) => setTimeout(() => e(!0), 8));
|
|
908
1143
|
}
|
|
1144
|
+
return t;
|
|
909
1145
|
};
|
|
910
1146
|
parseHeader = (e, t) => {
|
|
911
|
-
let n
|
|
912
|
-
|
|
913
|
-
sync_byte: r,
|
|
914
|
-
transport_error_indicator: s
|
|
915
|
-
} : {
|
|
916
|
-
sync_byte: r,
|
|
917
|
-
transport_error_indicator: s,
|
|
918
|
-
payload_unit_start_indicator: (i & 64) >> 6,
|
|
919
|
-
transport_priority: (i & 32) >> 5,
|
|
920
|
-
pid: (i & 31) << 8 | a,
|
|
921
|
-
transport_scrambling_control: (o & 192) >> 6,
|
|
922
|
-
adaptation_field_control: o >> 4 & 3,
|
|
923
|
-
continuity_counter: o & 15
|
|
924
|
-
};
|
|
925
|
-
};
|
|
926
|
-
parsePAT = (e, t) => {
|
|
927
|
-
let n = t, r;
|
|
1147
|
+
let n, r, i, a;
|
|
1148
|
+
n = e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), r = e.getUint8(t + 3);
|
|
928
1149
|
{
|
|
929
|
-
let
|
|
930
|
-
|
|
1150
|
+
let n = e.getUint8(t + 4);
|
|
1151
|
+
i = {
|
|
1152
|
+
audio: !!(n & 4),
|
|
1153
|
+
video: !!(n & 1)
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
a = e.getUint32(t + 5), this.header = {
|
|
1157
|
+
signature: n,
|
|
1158
|
+
version: r,
|
|
1159
|
+
flags: i,
|
|
1160
|
+
dataOffset: a
|
|
1161
|
+
};
|
|
1162
|
+
};
|
|
1163
|
+
isSurplusTag = (e, t) => {
|
|
1164
|
+
let n = !0, r = e.byteLength;
|
|
1165
|
+
if (t + 4 > r) n = !1;
|
|
1166
|
+
else if (t + 4 + 11 > r) n = !1;
|
|
1167
|
+
else {
|
|
1168
|
+
let i = B(e, t + 4 + 1);
|
|
1169
|
+
t + 4 + 11 + i > r && (n = !1);
|
|
1170
|
+
}
|
|
1171
|
+
return n;
|
|
1172
|
+
};
|
|
1173
|
+
parseTagHeader = (e, t) => {
|
|
1174
|
+
let n, r, i, a, o;
|
|
1175
|
+
{
|
|
1176
|
+
let r = e.getUint8(t), i;
|
|
1177
|
+
switch (r) {
|
|
1178
|
+
case 18:
|
|
1179
|
+
i = "script";
|
|
1180
|
+
break;
|
|
1181
|
+
case 8:
|
|
1182
|
+
i = "audio";
|
|
1183
|
+
break;
|
|
1184
|
+
case 9:
|
|
1185
|
+
i = "video";
|
|
1186
|
+
break;
|
|
1187
|
+
}
|
|
1188
|
+
n = i;
|
|
1189
|
+
}
|
|
1190
|
+
r = B(e, t + 1), i = B(e, t + 4), a = e.getUint8(t + 7), o = B(e, t + 8);
|
|
1191
|
+
let s = H(i, a);
|
|
1192
|
+
return {
|
|
1193
|
+
tagType: n,
|
|
1194
|
+
dataSize: r,
|
|
1195
|
+
timestamp: i,
|
|
1196
|
+
timestampExtended: a,
|
|
1197
|
+
dts: s,
|
|
1198
|
+
streamID: o
|
|
1199
|
+
};
|
|
1200
|
+
};
|
|
1201
|
+
parseTagBody = (e, t, n, r) => {
|
|
1202
|
+
let i;
|
|
1203
|
+
switch (e) {
|
|
1204
|
+
case "script":
|
|
1205
|
+
i = this.parseMetaData(t, n);
|
|
1206
|
+
break;
|
|
1207
|
+
case "audio":
|
|
1208
|
+
i = this.parseAudio(t, n, r);
|
|
1209
|
+
break;
|
|
1210
|
+
case "video":
|
|
1211
|
+
i = this.parseVideo(t, n, r);
|
|
1212
|
+
break;
|
|
1213
|
+
}
|
|
1214
|
+
return i;
|
|
1215
|
+
};
|
|
1216
|
+
parseMetaData = (e, t) => {
|
|
1217
|
+
let n = t;
|
|
1218
|
+
if (e.getUint8(n) !== 2) return null;
|
|
1219
|
+
n += 1;
|
|
1220
|
+
let r = e.getUint16(n, !1);
|
|
1221
|
+
n += 2;
|
|
1222
|
+
{
|
|
1223
|
+
let t = new Int8Array(e.buffer.slice(n, n + r)), i = this.textDecoder?.decode(t) || "";
|
|
1224
|
+
if (n += r, i !== "onMetaData") return { name: i };
|
|
1225
|
+
}
|
|
1226
|
+
let i = this.getAmfType(e, n);
|
|
1227
|
+
return n += 1, this.getAMFValue(e, n, i).value;
|
|
1228
|
+
};
|
|
1229
|
+
parseAudio = (e, t, n) => {
|
|
1230
|
+
let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i >> 2 & 3, s = i >> 1 & 1, c = i & 1;
|
|
1231
|
+
r += 1;
|
|
1232
|
+
let l = e.getUint8(r);
|
|
1233
|
+
r += 1;
|
|
1234
|
+
let u = n - (r - t);
|
|
1235
|
+
if (u <= 0) return {
|
|
1236
|
+
soundFormat: a,
|
|
1237
|
+
soundRate: o,
|
|
1238
|
+
soundSize: s,
|
|
1239
|
+
soundType: c,
|
|
1240
|
+
accPacketType: l,
|
|
1241
|
+
data: /* @__PURE__ */ new Uint8Array()
|
|
1242
|
+
};
|
|
1243
|
+
let d = new Uint8Array(e.buffer.slice(r, r + u));
|
|
1244
|
+
if (a === 10 && l === 0) {
|
|
1245
|
+
let t = e.getUint8(r), n = e.getUint8(r + 1), i = t >> 3 & 31, u = (t & 7) << 1 | n >> 7;
|
|
1246
|
+
return {
|
|
1247
|
+
soundFormat: a,
|
|
1248
|
+
soundRate: o,
|
|
1249
|
+
soundSize: s,
|
|
1250
|
+
soundType: c,
|
|
1251
|
+
accPacketType: l,
|
|
1252
|
+
data: d,
|
|
1253
|
+
samplingFrequencyIndex: u,
|
|
1254
|
+
channelConfiguration: n >> 3 & 15,
|
|
1255
|
+
codec: `mp4a.40.${i}`,
|
|
1256
|
+
sampleRate: U[u]
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
return {
|
|
1260
|
+
soundFormat: a,
|
|
1261
|
+
soundRate: o,
|
|
1262
|
+
soundSize: s,
|
|
1263
|
+
soundType: c,
|
|
1264
|
+
accPacketType: l,
|
|
1265
|
+
data: d
|
|
1266
|
+
};
|
|
1267
|
+
};
|
|
1268
|
+
parseVideo = (e, t, n) => {
|
|
1269
|
+
let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i & 15;
|
|
1270
|
+
r += 1;
|
|
1271
|
+
let s = e.getUint8(r);
|
|
1272
|
+
r += 1;
|
|
1273
|
+
let c = V(e, r);
|
|
1274
|
+
r += 3;
|
|
1275
|
+
let l = n - 5;
|
|
1276
|
+
if (l < 0) return null;
|
|
1277
|
+
let u = new Uint8Array(e.buffer.slice(r, r + l));
|
|
1278
|
+
switch (o) {
|
|
1279
|
+
case 7:
|
|
1280
|
+
if (s === 0) return {
|
|
1281
|
+
frameType: a,
|
|
1282
|
+
codecID: o,
|
|
1283
|
+
avcPacketType: s,
|
|
1284
|
+
cts: c,
|
|
1285
|
+
data: u,
|
|
1286
|
+
...w(u)
|
|
1287
|
+
};
|
|
1288
|
+
if (s === 1) {
|
|
1289
|
+
let t = [], n = r + l;
|
|
1290
|
+
for (; !(r + 4 > n);) {
|
|
1291
|
+
let i = e.getUint32(r, !1);
|
|
1292
|
+
if (i <= 0 || r + 4 + i > n) break;
|
|
1293
|
+
let a = new Uint8Array(e.buffer.slice(r, r + 4 + i));
|
|
1294
|
+
r += 4 + i, t.push(a);
|
|
1295
|
+
}
|
|
1296
|
+
return {
|
|
1297
|
+
frameType: a,
|
|
1298
|
+
codecID: o,
|
|
1299
|
+
avcPacketType: s,
|
|
1300
|
+
cts: c,
|
|
1301
|
+
data: u,
|
|
1302
|
+
nalus: t
|
|
1303
|
+
};
|
|
1304
|
+
}
|
|
1305
|
+
break;
|
|
1306
|
+
default: return null;
|
|
1307
|
+
}
|
|
1308
|
+
return {
|
|
1309
|
+
frameType: a,
|
|
1310
|
+
codecID: o,
|
|
1311
|
+
avcPacketType: s,
|
|
1312
|
+
cts: c,
|
|
1313
|
+
data: u
|
|
1314
|
+
};
|
|
1315
|
+
};
|
|
1316
|
+
getAmfType = (e, t) => e.getUint8(t);
|
|
1317
|
+
getAMFName = (e, t, n) => {
|
|
1318
|
+
let r = new Uint8Array(e.buffer.slice(t, t + n));
|
|
1319
|
+
return this.textDecoder?.decode(r) || "";
|
|
1320
|
+
};
|
|
1321
|
+
getAMFValue = (e, t, n) => {
|
|
1322
|
+
let r = t, i, a = 0;
|
|
1323
|
+
switch (n) {
|
|
1324
|
+
case 0:
|
|
1325
|
+
i = e.getFloat64(r, !1), a = 8;
|
|
1326
|
+
break;
|
|
1327
|
+
case 1:
|
|
1328
|
+
i = !!e.getUint8(r), a = 1;
|
|
1329
|
+
break;
|
|
1330
|
+
case 2:
|
|
1331
|
+
{
|
|
1332
|
+
i = "";
|
|
1333
|
+
let t = e.getUint16(r, !1);
|
|
1334
|
+
r += 2;
|
|
1335
|
+
let n = new Int8Array(e.buffer, r, t).filter((e) => e !== 0);
|
|
1336
|
+
i = (this.textDecoder?.decode(n) || "").trim(), a = 2 + t;
|
|
1337
|
+
}
|
|
1338
|
+
break;
|
|
1339
|
+
case 3:
|
|
1340
|
+
for (i = {}; r < e.byteLength;) {
|
|
1341
|
+
let t = e.getUint16(r, !1);
|
|
1342
|
+
if (t === 0) break;
|
|
1343
|
+
r += 2;
|
|
1344
|
+
let n = this.getAMFName(e, r, t);
|
|
1345
|
+
r += t;
|
|
1346
|
+
let o = this.getAmfType(e, r);
|
|
1347
|
+
if (o === 6) break;
|
|
1348
|
+
r += 1;
|
|
1349
|
+
let s = this.getAMFValue(e, r, o);
|
|
1350
|
+
r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
|
|
1351
|
+
}
|
|
1352
|
+
break;
|
|
1353
|
+
case 8:
|
|
1354
|
+
{
|
|
1355
|
+
i = {};
|
|
1356
|
+
let t = e.getUint32(r, !1);
|
|
1357
|
+
r += 4;
|
|
1358
|
+
for (let n = 0; n < t; n++) {
|
|
1359
|
+
let t = e.getUint16(r, !1);
|
|
1360
|
+
r += 2;
|
|
1361
|
+
let n = this.getAMFName(e, r, t);
|
|
1362
|
+
r += t;
|
|
1363
|
+
let o = this.getAmfType(e, r);
|
|
1364
|
+
r += 1;
|
|
1365
|
+
let s = this.getAMFValue(e, r, o);
|
|
1366
|
+
r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
break;
|
|
1370
|
+
case 10:
|
|
1371
|
+
{
|
|
1372
|
+
i = [];
|
|
1373
|
+
let t = e.getUint32(r, !1);
|
|
1374
|
+
r += 4;
|
|
1375
|
+
for (let n = 0; n < t; n++) {
|
|
1376
|
+
let t = this.getAmfType(e, r);
|
|
1377
|
+
r += 1;
|
|
1378
|
+
let n = this.getAMFValue(e, r, t);
|
|
1379
|
+
r += n.length, i.push(n.value), a = 1 + n.length;
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
break;
|
|
1383
|
+
}
|
|
1384
|
+
return {
|
|
1385
|
+
amfType: n,
|
|
1386
|
+
length: a,
|
|
1387
|
+
value: i
|
|
1388
|
+
};
|
|
1389
|
+
};
|
|
1390
|
+
}, G = [
|
|
1391
|
+
96e3,
|
|
1392
|
+
88200,
|
|
1393
|
+
64e3,
|
|
1394
|
+
48e3,
|
|
1395
|
+
44100,
|
|
1396
|
+
32e3,
|
|
1397
|
+
24e3,
|
|
1398
|
+
22050,
|
|
1399
|
+
16e3,
|
|
1400
|
+
12e3,
|
|
1401
|
+
11025,
|
|
1402
|
+
8e3,
|
|
1403
|
+
7350
|
|
1404
|
+
], K = (e) => {
|
|
1405
|
+
let t = "unknown";
|
|
1406
|
+
switch (e) {
|
|
1407
|
+
case 1:
|
|
1408
|
+
case 2:
|
|
1409
|
+
case 27:
|
|
1410
|
+
case 36:
|
|
1411
|
+
t = "video";
|
|
1412
|
+
break;
|
|
1413
|
+
case 3:
|
|
1414
|
+
case 4:
|
|
1415
|
+
case 15:
|
|
1416
|
+
t = "audio";
|
|
1417
|
+
break;
|
|
1418
|
+
case 6:
|
|
1419
|
+
t = "subtitle";
|
|
1420
|
+
break;
|
|
1421
|
+
case 134:
|
|
1422
|
+
t = "ad";
|
|
1423
|
+
break;
|
|
1424
|
+
}
|
|
1425
|
+
return t;
|
|
1426
|
+
}, q = class {
|
|
1427
|
+
pat;
|
|
1428
|
+
pmt;
|
|
1429
|
+
audioConfig;
|
|
1430
|
+
videoConfig;
|
|
1431
|
+
payloadMap = /* @__PURE__ */ new Map();
|
|
1432
|
+
on = {};
|
|
1433
|
+
constructor() {}
|
|
1434
|
+
parse = async (e) => {
|
|
1435
|
+
let t = 0;
|
|
1436
|
+
for (; !(t + 188 > e.byteLength);) {
|
|
1437
|
+
if (e.getInt8(t) != 71) {
|
|
1438
|
+
t++;
|
|
1439
|
+
continue;
|
|
1440
|
+
}
|
|
1441
|
+
await this.parsePacket(e, t), t += 188;
|
|
1442
|
+
}
|
|
1443
|
+
return t;
|
|
1444
|
+
};
|
|
1445
|
+
parsePacket = async (e, t) => {
|
|
1446
|
+
if (t + 188 > e.byteLength || e.getUint8(t) !== 71) throw Error("Invalid TS packet");
|
|
1447
|
+
let n = t, r = this.parseHeader(e, n);
|
|
1448
|
+
n += 4;
|
|
1449
|
+
let { transport_error_indicator: i, pid: a, payload_unit_start_indicator: o, adaptation_field_control: s } = r;
|
|
1450
|
+
if (i === 1 || a === void 0) return;
|
|
1451
|
+
let c = 184;
|
|
1452
|
+
if (s === 2 || s === 3) {
|
|
1453
|
+
let t = e.getUint8(n);
|
|
1454
|
+
n += 1, t > 0 && (this.parseAdaptationField(e, n), n += t), c -= 1 + t;
|
|
1455
|
+
}
|
|
1456
|
+
if (s === 1 || s === 3) {
|
|
1457
|
+
if (c <= 0) return;
|
|
1458
|
+
let t = new Uint8Array(e.buffer.slice(n, n + c));
|
|
1459
|
+
if (a === 0) return this.parsePAT(e, n);
|
|
1460
|
+
{
|
|
1461
|
+
let { programs: t = [] } = this.pat || {};
|
|
1462
|
+
if (t.find((e) => e.pmt_pid === a)) return this.parsePMT(e, n);
|
|
1463
|
+
}
|
|
1464
|
+
let { streams: r = [] } = this.pmt || {}, i = r.find((e) => e.elementary_pid === a);
|
|
1465
|
+
if (i) {
|
|
1466
|
+
if (o === 1) {
|
|
1467
|
+
let e = this.payloadMap.get(a);
|
|
1468
|
+
if (e) {
|
|
1469
|
+
switch (i.kind) {
|
|
1470
|
+
case "audio":
|
|
1471
|
+
{
|
|
1472
|
+
let t = await this.parseAudio(e);
|
|
1473
|
+
this.on.chunk && this.on.chunk(t);
|
|
1474
|
+
}
|
|
1475
|
+
break;
|
|
1476
|
+
case "video":
|
|
1477
|
+
{
|
|
1478
|
+
let t = await this.parseVideo(e);
|
|
1479
|
+
this.on.chunk && this.on.chunk(t), await new Promise((e) => setTimeout(() => e(!0), 0));
|
|
1480
|
+
}
|
|
1481
|
+
break;
|
|
1482
|
+
}
|
|
1483
|
+
this.payloadMap.delete(a);
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
{
|
|
1487
|
+
this.payloadMap.has(a) || this.payloadMap.set(a, /* @__PURE__ */ new Uint8Array());
|
|
1488
|
+
let e = this.payloadMap.get(a), n = new Uint8Array(e.byteLength + t.byteLength);
|
|
1489
|
+
n.set(e, 0), n.set(t, e.byteLength), this.payloadMap.set(a, n);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
};
|
|
1494
|
+
parseHeader = (e, t) => {
|
|
1495
|
+
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;
|
|
1496
|
+
return s === 1 ? {
|
|
1497
|
+
sync_byte: r,
|
|
1498
|
+
transport_error_indicator: s
|
|
1499
|
+
} : {
|
|
1500
|
+
sync_byte: r,
|
|
1501
|
+
transport_error_indicator: s,
|
|
1502
|
+
payload_unit_start_indicator: (i & 64) >> 6,
|
|
1503
|
+
transport_priority: (i & 32) >> 5,
|
|
1504
|
+
pid: (i & 31) << 8 | a,
|
|
1505
|
+
transport_scrambling_control: (o & 192) >> 6,
|
|
1506
|
+
adaptation_field_control: o >> 4 & 3,
|
|
1507
|
+
continuity_counter: o & 15
|
|
1508
|
+
};
|
|
1509
|
+
};
|
|
1510
|
+
parsePAT = (e, t) => {
|
|
1511
|
+
let n = t, r;
|
|
1512
|
+
{
|
|
1513
|
+
let t = e.getUint8(n);
|
|
1514
|
+
n += 1;
|
|
931
1515
|
let i = e.getUint8(n);
|
|
932
1516
|
if (n += 1, i !== 0) throw Error("Invalid PAT table_id");
|
|
933
1517
|
let a = e.getUint16(n) & 4095;
|
|
@@ -966,7 +1550,7 @@ var p = "pr-player-render", m = class {
|
|
|
966
1550
|
header: r,
|
|
967
1551
|
programs: i,
|
|
968
1552
|
crc32: a
|
|
969
|
-
}
|
|
1553
|
+
};
|
|
970
1554
|
};
|
|
971
1555
|
parsePMT = (e, t) => {
|
|
972
1556
|
let n = t, r;
|
|
@@ -1001,12 +1585,13 @@ var p = "pr-player-render", m = class {
|
|
|
1001
1585
|
program_info_length: f
|
|
1002
1586
|
};
|
|
1003
1587
|
}
|
|
1588
|
+
n += r.program_info_length;
|
|
1004
1589
|
let i = [];
|
|
1005
1590
|
{
|
|
1006
1591
|
let t = r.section_length - 9 - 4, a = n + t;
|
|
1007
1592
|
for (; n < a;) {
|
|
1008
|
-
let t = e.getUint8(n), r =
|
|
1009
|
-
if (n += 5, a < 32 || a > 8190) {
|
|
1593
|
+
let t = e.getUint8(n), r = K(t), a = e.getUint16(n + 1) & 8191, o = e.getUint16(n + 3) & 4095;
|
|
1594
|
+
if (n += 5 + o, a < 32 || a > 8190) {
|
|
1010
1595
|
console.warn(`Invalid elementary_pid: 0x${a.toString(16)}`);
|
|
1011
1596
|
continue;
|
|
1012
1597
|
}
|
|
@@ -1023,7 +1608,7 @@ var p = "pr-player-render", m = class {
|
|
|
1023
1608
|
header: r,
|
|
1024
1609
|
streams: i,
|
|
1025
1610
|
crc32: a
|
|
1026
|
-
}
|
|
1611
|
+
};
|
|
1027
1612
|
};
|
|
1028
1613
|
parseAdaptationField = (e, t) => {
|
|
1029
1614
|
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);
|
|
@@ -1088,40 +1673,36 @@ var p = "pr-player-render", m = class {
|
|
|
1088
1673
|
}
|
|
1089
1674
|
i = e.slice(n);
|
|
1090
1675
|
{
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
64e3,
|
|
1100
|
-
48e3,
|
|
1101
|
-
44100,
|
|
1102
|
-
32e3,
|
|
1103
|
-
24e3,
|
|
1104
|
-
22050,
|
|
1105
|
-
16e3,
|
|
1106
|
-
12e3,
|
|
1107
|
-
11025,
|
|
1108
|
-
8e3,
|
|
1109
|
-
7350
|
|
1110
|
-
][a],
|
|
1111
|
-
numberOfChannels: i
|
|
1112
|
-
}, this.on.config && this.on.config(this.audioConfig);
|
|
1113
|
-
}
|
|
1114
|
-
let { dts: e = 0, pts: a = 0 } = r;
|
|
1676
|
+
let e = this.parseAdts(i);
|
|
1677
|
+
!this.audioConfig && e && (this.audioConfig = {
|
|
1678
|
+
kind: "audio",
|
|
1679
|
+
codec: e.codec,
|
|
1680
|
+
sampleRate: e.sampleRate,
|
|
1681
|
+
numberOfChannels: e.channelConfiguration
|
|
1682
|
+
}, this.on.config && this.on.config(this.audioConfig));
|
|
1683
|
+
let { dts: t = 0, pts: n = 0 } = r;
|
|
1115
1684
|
return {
|
|
1116
1685
|
kind: "audio",
|
|
1117
1686
|
type: "key",
|
|
1118
|
-
dts:
|
|
1119
|
-
pts:
|
|
1120
|
-
cts:
|
|
1121
|
-
data: i.slice(
|
|
1687
|
+
dts: t,
|
|
1688
|
+
pts: n,
|
|
1689
|
+
cts: n - t,
|
|
1690
|
+
data: e ? i.slice(e.headerLength) : i
|
|
1122
1691
|
};
|
|
1123
1692
|
}
|
|
1124
1693
|
};
|
|
1694
|
+
parseAdts = (e) => {
|
|
1695
|
+
if (e.length < 7 || e[0] !== 255 || (e[1] & 240) != 240) return null;
|
|
1696
|
+
let t = e[1] & 1 ? 7 : 9;
|
|
1697
|
+
if (e.length < t) return null;
|
|
1698
|
+
let n = (e[2] >> 6 & 3) + 1, r = e[2] >> 2 & 15, i = (e[2] & 1) << 2 | e[3] >> 6, a = G[r] ?? 44100;
|
|
1699
|
+
return {
|
|
1700
|
+
headerLength: t,
|
|
1701
|
+
codec: `mp4a.40.${n}`,
|
|
1702
|
+
sampleRate: a,
|
|
1703
|
+
channelConfiguration: i
|
|
1704
|
+
};
|
|
1705
|
+
};
|
|
1125
1706
|
parseVideo = async (e) => {
|
|
1126
1707
|
let t = new DataView(e.buffer), n = 0, r, i;
|
|
1127
1708
|
{
|
|
@@ -1205,351 +1786,419 @@ var p = "pr-player-render", m = class {
|
|
|
1205
1786
|
}
|
|
1206
1787
|
getNalus = (e) => {
|
|
1207
1788
|
let t = [], n = 0;
|
|
1208
|
-
for (;
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
let r = n, i = e[n] & 31;
|
|
1215
|
-
for (n += 1; !(n + 1 > e.byteLength);) {
|
|
1216
|
-
if (e[n] !== 0 || e[n + 1] !== 0 || e[n + 2] !== 1) {
|
|
1217
|
-
n += 1;
|
|
1218
|
-
continue;
|
|
1219
|
-
}
|
|
1220
|
-
break;
|
|
1221
|
-
}
|
|
1222
|
-
let a = n - r;
|
|
1223
|
-
if (e[n - 1] === 0 && --a, a !== 0) {
|
|
1224
|
-
let n = T(e.slice(r, r + a));
|
|
1789
|
+
for (; n < e.byteLength - 2;) {
|
|
1790
|
+
let r = this.findAnnexBStart(e, n);
|
|
1791
|
+
if (!r) break;
|
|
1792
|
+
let i = e[r.naluOffset] & 31, a = this.findAnnexBStart(e, r.nextSearchOffset), o = (a ? a.startOffset : e.byteLength) - r.naluOffset;
|
|
1793
|
+
if (o > 0) {
|
|
1794
|
+
let n = e.slice(r.naluOffset, r.naluOffset + o);
|
|
1225
1795
|
t.push({
|
|
1226
1796
|
type: i,
|
|
1227
|
-
nalu: n
|
|
1797
|
+
nalu: T(n)
|
|
1228
1798
|
});
|
|
1229
1799
|
}
|
|
1800
|
+
n = a ? a.startOffset : e.byteLength;
|
|
1230
1801
|
}
|
|
1231
|
-
return t;
|
|
1232
|
-
};
|
|
1233
|
-
}, N = (e, t) => e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), P = class {
|
|
1234
|
-
audioConfig;
|
|
1235
|
-
videoConfig;
|
|
1236
|
-
header;
|
|
1237
|
-
textDecoder = new TextDecoder("utf-8");
|
|
1238
|
-
on = {};
|
|
1239
|
-
constructor() {}
|
|
1240
|
-
parse = async (e) => {
|
|
1241
|
-
let t = 0;
|
|
1242
|
-
for (this.header || (this.parseHeader(e, t), t += 9); this.isSurplusTag(e, t) !== !1;) {
|
|
1243
|
-
let { tagType: n, dataSize: r, timestamp: i } = this.parseTagHeader(e, t + 4);
|
|
1244
|
-
if (n) {
|
|
1245
|
-
let a = this.parseTagBody(n, e, t + 4 + 11, r);
|
|
1246
|
-
switch (n) {
|
|
1247
|
-
case "script":
|
|
1248
|
-
this.on.info && this.on.info(a);
|
|
1249
|
-
break;
|
|
1250
|
-
case "audio":
|
|
1251
|
-
{
|
|
1252
|
-
let { accPacketType: e } = a;
|
|
1253
|
-
if (e === 0) {
|
|
1254
|
-
let { codec: e, sampleRate: t, channelConfiguration: n } = a;
|
|
1255
|
-
this.audioConfig = {
|
|
1256
|
-
kind: "audio",
|
|
1257
|
-
codec: e,
|
|
1258
|
-
sampleRate: t,
|
|
1259
|
-
numberOfChannels: n
|
|
1260
|
-
}, this.on.config && this.on.config(this.audioConfig);
|
|
1261
|
-
} else {
|
|
1262
|
-
let { cts: e, data: t } = a, n = e === void 0 ? void 0 : e + i;
|
|
1263
|
-
this.on.chunk && this.on.chunk({
|
|
1264
|
-
kind: "audio",
|
|
1265
|
-
type: "key",
|
|
1266
|
-
dts: i,
|
|
1267
|
-
pts: n,
|
|
1268
|
-
cts: e,
|
|
1269
|
-
data: t
|
|
1270
|
-
});
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
break;
|
|
1274
|
-
case "video":
|
|
1275
|
-
{
|
|
1276
|
-
let { avcPacketType: e } = a;
|
|
1277
|
-
if (e === 0) {
|
|
1278
|
-
let { codec: e, sps: t, pps: n, data: r } = a;
|
|
1279
|
-
this.videoConfig = {
|
|
1280
|
-
kind: "video",
|
|
1281
|
-
codec: e,
|
|
1282
|
-
description: r,
|
|
1283
|
-
sps: t,
|
|
1284
|
-
pps: n
|
|
1285
|
-
}, this.on.config && this.on.config(this.videoConfig);
|
|
1286
|
-
} else {
|
|
1287
|
-
let { frameType: e, cts: t, data: n, nalus: r } = a, o = e === 1 ? "key" : "delta", s = t === void 0 ? void 0 : t + i;
|
|
1288
|
-
this.on.chunk && this.on.chunk({
|
|
1289
|
-
kind: "video",
|
|
1290
|
-
type: o,
|
|
1291
|
-
dts: i,
|
|
1292
|
-
pts: s,
|
|
1293
|
-
cts: t,
|
|
1294
|
-
data: n,
|
|
1295
|
-
nalus: r
|
|
1296
|
-
});
|
|
1297
|
-
}
|
|
1298
|
-
}
|
|
1299
|
-
break;
|
|
1300
|
-
}
|
|
1301
|
-
t = t + 4 + 11 + r;
|
|
1302
|
-
}
|
|
1303
|
-
await new Promise((e) => setTimeout(() => e(!0), 8));
|
|
1304
|
-
}
|
|
1305
|
-
return t;
|
|
1306
|
-
};
|
|
1307
|
-
parseHeader = (e, t) => {
|
|
1308
|
-
let n, r, i, a;
|
|
1309
|
-
n = e.getUint8(t) << 16 | e.getUint8(t + 1) << 8 | e.getUint8(t + 2), r = e.getUint8(3);
|
|
1310
|
-
{
|
|
1311
|
-
let [, , t, , n] = e.getUint8(0).toString(2).padStart(5, "0").split("");
|
|
1312
|
-
i = {
|
|
1313
|
-
audio: n === "1",
|
|
1314
|
-
video: t === "1"
|
|
1315
|
-
};
|
|
1316
|
-
}
|
|
1317
|
-
a = e.getUint32(5), this.header = {
|
|
1318
|
-
signature: n,
|
|
1319
|
-
version: r,
|
|
1320
|
-
flags: i,
|
|
1321
|
-
dataOffset: a
|
|
1322
|
-
};
|
|
1802
|
+
return t;
|
|
1323
1803
|
};
|
|
1324
|
-
|
|
1325
|
-
let n =
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1804
|
+
findAnnexBStart = (e, t) => {
|
|
1805
|
+
for (let n = t; n < e.length - 2; n++) if (!(e[n] !== 0 || e[n + 1] !== 0)) {
|
|
1806
|
+
if (e[n + 2] === 1) return {
|
|
1807
|
+
startOffset: n,
|
|
1808
|
+
naluOffset: n + 3,
|
|
1809
|
+
nextSearchOffset: n + 3
|
|
1810
|
+
};
|
|
1811
|
+
if (n + 3 < e.length && e[n + 2] === 0 && e[n + 3] === 1) return {
|
|
1812
|
+
startOffset: n,
|
|
1813
|
+
naluOffset: n + 4,
|
|
1814
|
+
nextSearchOffset: n + 4
|
|
1815
|
+
};
|
|
1331
1816
|
}
|
|
1332
|
-
return
|
|
1817
|
+
return null;
|
|
1818
|
+
};
|
|
1819
|
+
}, J = (e, t) => String.fromCharCode(e.getUint8(t), e.getUint8(t + 1), e.getUint8(t + 2), e.getUint8(t + 3)), Y = (e, t, n) => {
|
|
1820
|
+
if (t + 8 > n) return null;
|
|
1821
|
+
let r = e.getUint32(t, !1), i = J(e, t + 4), a = 8;
|
|
1822
|
+
if (r === 1) {
|
|
1823
|
+
if (t + 16 > n) return null;
|
|
1824
|
+
r = Number(e.getBigUint64(t + 8, !1)), a = 16;
|
|
1825
|
+
} else r === 0 && (r = n - t);
|
|
1826
|
+
return r < a || t + r > n ? null : {
|
|
1827
|
+
offset: t,
|
|
1828
|
+
size: r,
|
|
1829
|
+
type: i,
|
|
1830
|
+
headerSize: a,
|
|
1831
|
+
contentStart: t + a
|
|
1832
|
+
};
|
|
1833
|
+
}, X = (e, t, n, r) => {
|
|
1834
|
+
let i = t;
|
|
1835
|
+
for (; i < n;) {
|
|
1836
|
+
let t = Y(e, i, n);
|
|
1837
|
+
if (!t) break;
|
|
1838
|
+
r(t), i += t.size;
|
|
1839
|
+
}
|
|
1840
|
+
}, Z = (e, t, n, r) => {
|
|
1841
|
+
let i = null;
|
|
1842
|
+
return X(e, t, n, (t) => {
|
|
1843
|
+
if (t.type === r) i = t;
|
|
1844
|
+
else if (t.type !== "mdat") {
|
|
1845
|
+
let n = Z(e, t.contentStart, t.offset + t.size, r);
|
|
1846
|
+
n && (i = n);
|
|
1847
|
+
}
|
|
1848
|
+
}), i;
|
|
1849
|
+
}, Q = (e, t, n, r) => {
|
|
1850
|
+
for (let i = t; i + 8 <= n; i++) {
|
|
1851
|
+
if (J(e, i + 4) !== r) continue;
|
|
1852
|
+
let t = Y(e, i, n);
|
|
1853
|
+
if (t?.type === r) return t;
|
|
1854
|
+
}
|
|
1855
|
+
return null;
|
|
1856
|
+
}, $ = (e, t, n, r, i = 86) => Q(e, t + i, n, r) || Q(e, t + 8, n, r), ee = [
|
|
1857
|
+
96e3,
|
|
1858
|
+
88200,
|
|
1859
|
+
64e3,
|
|
1860
|
+
48e3,
|
|
1861
|
+
44100,
|
|
1862
|
+
32e3,
|
|
1863
|
+
24e3,
|
|
1864
|
+
22050,
|
|
1865
|
+
16e3,
|
|
1866
|
+
12e3,
|
|
1867
|
+
11025,
|
|
1868
|
+
8e3,
|
|
1869
|
+
7350
|
|
1870
|
+
], te = (e, t) => {
|
|
1871
|
+
let n = 0, r = t;
|
|
1872
|
+
for (; r < e.length;) {
|
|
1873
|
+
if (n = n << 7 | e[r] & 127, !(e[r] & 128)) return {
|
|
1874
|
+
len: n,
|
|
1875
|
+
next: r + 1
|
|
1876
|
+
};
|
|
1877
|
+
r++;
|
|
1878
|
+
}
|
|
1879
|
+
return {
|
|
1880
|
+
len: 0,
|
|
1881
|
+
next: t
|
|
1333
1882
|
};
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1883
|
+
}, ne = class {
|
|
1884
|
+
audioConfig;
|
|
1885
|
+
videoConfig;
|
|
1886
|
+
tracks = /* @__PURE__ */ new Map();
|
|
1887
|
+
mdatBox = null;
|
|
1888
|
+
progressiveParsed = !1;
|
|
1889
|
+
on = {};
|
|
1890
|
+
getDiscardOffset = (e, t) => this.progressiveParsed || Z(t, 0, e, "moof") || this.tracks.size > 0 && !Z(t, 0, e, "mdat") ? e : 0;
|
|
1891
|
+
constructor() {}
|
|
1892
|
+
parse = async (e) => {
|
|
1893
|
+
let t = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), n = t.byteLength, r = 0;
|
|
1894
|
+
for (; r < n;) {
|
|
1895
|
+
let i = Y(e, r, n);
|
|
1896
|
+
if (!i) return 0;
|
|
1897
|
+
switch (i.type) {
|
|
1898
|
+
case "moov":
|
|
1899
|
+
this.parseMoov(e, i), this.tryRunProgressive(e, t);
|
|
1341
1900
|
break;
|
|
1342
|
-
case
|
|
1343
|
-
i
|
|
1901
|
+
case "moof":
|
|
1902
|
+
this.parseMoof(e, i, t);
|
|
1344
1903
|
break;
|
|
1345
|
-
case
|
|
1346
|
-
|
|
1904
|
+
case "mdat":
|
|
1905
|
+
this.mdatBox = i, this.tryRunProgressive(e, t);
|
|
1347
1906
|
break;
|
|
1907
|
+
case "sidx":
|
|
1908
|
+
case "styp":
|
|
1909
|
+
case "free":
|
|
1910
|
+
case "skip":
|
|
1911
|
+
case "ftyp": break;
|
|
1912
|
+
default: break;
|
|
1348
1913
|
}
|
|
1349
|
-
|
|
1914
|
+
r += i.size, (i.type === "moov" || i.type === "moof" || i.type === "mdat") && await new Promise((e) => setTimeout(() => e(!0), 8));
|
|
1350
1915
|
}
|
|
1351
|
-
return
|
|
1352
|
-
tagType: n,
|
|
1353
|
-
dataSize: r,
|
|
1354
|
-
timestamp: i,
|
|
1355
|
-
timestampExtended: a,
|
|
1356
|
-
streamID: o
|
|
1357
|
-
};
|
|
1916
|
+
return this.progressiveParsed ? n : 0;
|
|
1358
1917
|
};
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1918
|
+
tryRunProgressive = (e, t) => {
|
|
1919
|
+
if (this.progressiveParsed || this.tracks.size === 0) return;
|
|
1920
|
+
let n = this.mdatBox ?? Z(e, 0, t.byteLength, "mdat"), r = Z(e, 0, t.byteLength, "moov");
|
|
1921
|
+
!n || !r || this.isProgressiveReady(n, r, t.byteLength) && (this.parseProgressive(e, n, t), this.progressiveParsed = !0);
|
|
1922
|
+
};
|
|
1923
|
+
isProgressiveReady = (e, t, n) => e.offset + e.size <= n && t.offset + t.size <= n;
|
|
1924
|
+
parseMoov = (e, t) => {
|
|
1925
|
+
X(e, t.contentStart, t.offset + t.size, (t) => {
|
|
1926
|
+
t.type === "trak" && this.parseTrak(e, t);
|
|
1927
|
+
});
|
|
1928
|
+
let n = Z(e, t.contentStart, t.offset + t.size, "mvex");
|
|
1929
|
+
n && X(e, n.contentStart, n.offset + n.size, (t) => {
|
|
1930
|
+
if (t.type !== "trex") return;
|
|
1931
|
+
let n = e.getUint32(t.contentStart + 4, !1), r = this.tracks.get(n);
|
|
1932
|
+
r && (r.defaultSampleDuration = e.getUint32(t.contentStart + 12, !1), r.defaultSampleSize = e.getUint32(t.contentStart + 16, !1));
|
|
1933
|
+
});
|
|
1934
|
+
let r = Z(e, t.contentStart, t.offset + t.size, "mvhd");
|
|
1935
|
+
if (r) {
|
|
1936
|
+
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);
|
|
1937
|
+
this.on.info?.({
|
|
1938
|
+
width: [...this.tracks.values()].find((e) => e.kind === "video")?.width,
|
|
1939
|
+
height: [...this.tracks.values()].find((e) => e.kind === "video")?.height,
|
|
1940
|
+
duration: n ? i / n : void 0
|
|
1941
|
+
});
|
|
1371
1942
|
}
|
|
1372
|
-
return i;
|
|
1373
1943
|
};
|
|
1374
|
-
|
|
1375
|
-
let n = t;
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1944
|
+
readTkhdSize = (e, t) => {
|
|
1945
|
+
let n = e.getUint8(t.contentStart), r = n === 1 ? 72 : 64, i = n === 1 ? 76 : 68;
|
|
1946
|
+
return {
|
|
1947
|
+
width: e.getUint32(t.contentStart + r, !1) >> 16,
|
|
1948
|
+
height: e.getUint32(t.contentStart + i, !1) >> 16
|
|
1949
|
+
};
|
|
1950
|
+
};
|
|
1951
|
+
readSampleEntrySize = (e, t) => ({
|
|
1952
|
+
width: e.getUint16(t + 32, !1),
|
|
1953
|
+
height: e.getUint16(t + 34, !1)
|
|
1954
|
+
});
|
|
1955
|
+
parseTrak = (e, t) => {
|
|
1956
|
+
let n = Z(e, t.contentStart, t.offset + t.size, "tkhd"), r = Z(e, t.contentStart, t.offset + t.size, "mdia");
|
|
1957
|
+
if (!n || !r) return;
|
|
1958
|
+
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");
|
|
1959
|
+
if (!c || !l) return;
|
|
1960
|
+
let u = J(e, c.contentStart + 8), d = u === "vide" ? "video" : u === "soun" ? "audio" : void 0;
|
|
1961
|
+
if (!d) return;
|
|
1962
|
+
let f = e.getUint8(l.contentStart), p = e.getUint32(l.contentStart + (f === 1 ? 20 : 12), !1);
|
|
1963
|
+
this.tracks.set(a, {
|
|
1964
|
+
kind: d,
|
|
1965
|
+
trackId: a,
|
|
1966
|
+
timescale: p,
|
|
1967
|
+
width: o,
|
|
1968
|
+
height: s
|
|
1969
|
+
});
|
|
1970
|
+
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");
|
|
1971
|
+
if (!g) return;
|
|
1972
|
+
let _ = g.contentStart + 8, v = _ + e.getUint32(_, !1), y = J(e, _ + 4);
|
|
1973
|
+
if (d === "video" && (y === "avc1" || y === "avc3")) {
|
|
1974
|
+
let t = this.readSampleEntrySize(e, _);
|
|
1975
|
+
t.width && t.height && (o = t.width, s = t.height);
|
|
1976
|
+
let n = $(e, _, v, "avcC", 86);
|
|
1977
|
+
if (!n) return;
|
|
1978
|
+
let r = new Uint8Array(e.buffer, n.contentStart, n.size - n.headerSize), i = w(r), a = i.codec.replace(/^avc1\./, `${y}.`);
|
|
1979
|
+
this.videoConfig = {
|
|
1980
|
+
kind: "video",
|
|
1981
|
+
codec: a,
|
|
1982
|
+
description: r,
|
|
1983
|
+
sps: i.sps,
|
|
1984
|
+
pps: i.pps
|
|
1985
|
+
}, this.on.config?.(this.videoConfig);
|
|
1986
|
+
}
|
|
1987
|
+
if (d === "video") {
|
|
1988
|
+
let e = this.tracks.get(a);
|
|
1989
|
+
e && (e.width = o, e.height = s);
|
|
1990
|
+
}
|
|
1991
|
+
if (d === "audio" && y === "mp4a") {
|
|
1992
|
+
let t = $(e, _, v, "esds", 36), n = t && this.parseEsds(e, t);
|
|
1993
|
+
n && (this.audioConfig = n, this.on.config?.(n));
|
|
1384
1994
|
}
|
|
1385
|
-
let i = this.getAmfType(e, n);
|
|
1386
|
-
return n += 1, this.getAMFValue(e, n, i).value;
|
|
1387
1995
|
};
|
|
1388
|
-
|
|
1389
|
-
let
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
let t = e.getUint8(r), n = e.getUint8(r + 1), i = t >> 3 & 31, u = (t & 7) << 1 | n >> 7;
|
|
1996
|
+
parseEsds = (e, t) => {
|
|
1997
|
+
let n = new Uint8Array(e.buffer, t.contentStart, t.size - t.headerSize);
|
|
1998
|
+
for (let e = 0; e < n.length - 2; e++) {
|
|
1999
|
+
if (n[e] !== 5) continue;
|
|
2000
|
+
let { len: t, next: r } = te(n, e + 1);
|
|
2001
|
+
if (t < 2 || r + t > n.length) continue;
|
|
2002
|
+
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;
|
|
1396
2003
|
return {
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
data: d,
|
|
1403
|
-
samplingFrequencyIndex: u,
|
|
1404
|
-
channelConfiguration: n >> 3 & 15,
|
|
1405
|
-
codec: `mp4a.40.${i}`,
|
|
1406
|
-
sampleRate: [
|
|
1407
|
-
96e3,
|
|
1408
|
-
88200,
|
|
1409
|
-
64e3,
|
|
1410
|
-
48e3,
|
|
1411
|
-
44100,
|
|
1412
|
-
32e3,
|
|
1413
|
-
24e3,
|
|
1414
|
-
22050,
|
|
1415
|
-
16e3,
|
|
1416
|
-
12e3,
|
|
1417
|
-
11025,
|
|
1418
|
-
8e3,
|
|
1419
|
-
7350
|
|
1420
|
-
][u]
|
|
2004
|
+
kind: "audio",
|
|
2005
|
+
codec: `mp4a.40.${s}`,
|
|
2006
|
+
sampleRate: ee[c] ?? 44100,
|
|
2007
|
+
numberOfChannels: l,
|
|
2008
|
+
description: i
|
|
1421
2009
|
};
|
|
1422
2010
|
}
|
|
1423
|
-
return
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
parseVideo = (e, t, n) => {
|
|
1433
|
-
let r = t, i = e.getUint8(r), a = i >> 4 & 15, o = i & 15;
|
|
1434
|
-
r += 1;
|
|
1435
|
-
let s = e.getUint8(r);
|
|
1436
|
-
r += 1;
|
|
1437
|
-
let c = N(e, r);
|
|
1438
|
-
r += 3;
|
|
1439
|
-
let l = n - 5, u = new Uint8Array(e.buffer.slice(r, r + l));
|
|
1440
|
-
switch (o) {
|
|
1441
|
-
case 7:
|
|
1442
|
-
if (s === 0) return {
|
|
1443
|
-
frameType: a,
|
|
1444
|
-
codecID: o,
|
|
1445
|
-
avcPacketType: s,
|
|
1446
|
-
cts: c,
|
|
1447
|
-
data: u,
|
|
1448
|
-
...w(u)
|
|
1449
|
-
};
|
|
1450
|
-
if (s === 1) {
|
|
1451
|
-
let t = [], i = r + n - 5;
|
|
1452
|
-
for (; !(r + 4 > i);) {
|
|
1453
|
-
let n = e.getUint32(r, !1), i = new Uint8Array(e.buffer.slice(r, r + 4 + n));
|
|
1454
|
-
r += 4 + n, t.push(i);
|
|
1455
|
-
}
|
|
1456
|
-
return {
|
|
1457
|
-
frameType: a,
|
|
1458
|
-
codecID: o,
|
|
1459
|
-
avcPacketType: s,
|
|
1460
|
-
cts: c,
|
|
1461
|
-
data: u,
|
|
1462
|
-
nalus: t
|
|
1463
|
-
};
|
|
1464
|
-
}
|
|
1465
|
-
break;
|
|
1466
|
-
default: throw Error("Unsupported codecID");
|
|
2011
|
+
return null;
|
|
2012
|
+
};
|
|
2013
|
+
findMdatForMoof = (e, t, n) => {
|
|
2014
|
+
let r = t.offset + t.size;
|
|
2015
|
+
for (; r + 8 <= n;) {
|
|
2016
|
+
let t = Y(e, r, n);
|
|
2017
|
+
if (!t || t.type === "moof") break;
|
|
2018
|
+
if (t.type === "mdat") return t;
|
|
2019
|
+
r += t.size;
|
|
1467
2020
|
}
|
|
1468
|
-
return
|
|
1469
|
-
frameType: a,
|
|
1470
|
-
codecID: o,
|
|
1471
|
-
avcPacketType: s,
|
|
1472
|
-
cts: c,
|
|
1473
|
-
data: u
|
|
1474
|
-
};
|
|
2021
|
+
return null;
|
|
1475
2022
|
};
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
2023
|
+
parseMoof = (e, t, n) => {
|
|
2024
|
+
let r = this.findMdatForMoof(e, t, n.byteLength);
|
|
2025
|
+
r && X(e, t.contentStart, t.offset + t.size, (i) => {
|
|
2026
|
+
i.type === "traf" && this.parseTraf(e, i, t.offset, r, n);
|
|
2027
|
+
});
|
|
1480
2028
|
};
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
let n = this.getAMFName(e, r, t);
|
|
1505
|
-
r += t;
|
|
1506
|
-
let o = this.getAmfType(e, r);
|
|
1507
|
-
if (o === 6) break;
|
|
1508
|
-
r += 1;
|
|
1509
|
-
let s = this.getAMFValue(e, r, o);
|
|
1510
|
-
r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
|
|
1511
|
-
}
|
|
1512
|
-
break;
|
|
1513
|
-
case 8:
|
|
1514
|
-
{
|
|
1515
|
-
i = {};
|
|
1516
|
-
let t = e.getUint32(r, !1);
|
|
1517
|
-
r += 4;
|
|
1518
|
-
for (let n = 0; n < t; n++) {
|
|
1519
|
-
let t = e.getUint16(r, !1);
|
|
1520
|
-
r += 2;
|
|
1521
|
-
let n = this.getAMFName(e, r, t);
|
|
1522
|
-
r += t;
|
|
1523
|
-
let o = this.getAmfType(e, r);
|
|
1524
|
-
r += 1;
|
|
1525
|
-
let s = this.getAMFValue(e, r, o);
|
|
1526
|
-
r += s.length, i[n] = s.value, a = 2 + t + 1 + s.length;
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
break;
|
|
1530
|
-
case 10:
|
|
1531
|
-
{
|
|
1532
|
-
i = [];
|
|
1533
|
-
let t = e.getUint32(r, !1);
|
|
1534
|
-
r += 4;
|
|
1535
|
-
for (let n = 0; n < t; n++) {
|
|
1536
|
-
let t = this.getAmfType(e, r);
|
|
1537
|
-
r += 1;
|
|
1538
|
-
let n = this.getAMFValue(e, r, t);
|
|
1539
|
-
r += n.length, i.push(n.value), a = 1 + n.length;
|
|
1540
|
-
}
|
|
1541
|
-
}
|
|
1542
|
-
break;
|
|
2029
|
+
ticksToMs = (e, t) => e / t * 1e3;
|
|
2030
|
+
readCttsOffsets = (e, t, n) => {
|
|
2031
|
+
let r = new Int32Array(n);
|
|
2032
|
+
if (!t || n === 0) return r;
|
|
2033
|
+
let i = e.getUint32(t.contentStart + 4, !1), a = 0, o = 0, s = 0, c = t.contentStart + 8;
|
|
2034
|
+
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;
|
|
2035
|
+
return r;
|
|
2036
|
+
};
|
|
2037
|
+
parseTraf = (e, t, n, r, i) => {
|
|
2038
|
+
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");
|
|
2039
|
+
if (!a || !o || !s) return;
|
|
2040
|
+
let c = e.getUint32(a.contentStart + 4, !1), l = this.tracks.get(c);
|
|
2041
|
+
if (!l) return;
|
|
2042
|
+
let u = e.getUint32(a.contentStart, !1) & 16777215, d = (u & 131072) != 0, f = a.contentStart + 8, p = null, m = 0, h = 0;
|
|
2043
|
+
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);
|
|
2044
|
+
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;
|
|
2045
|
+
if (y + 4 > v) return;
|
|
2046
|
+
let b = e.getUint32(y, !1);
|
|
2047
|
+
if (y += 4, b === 0 || b > 1e4) return;
|
|
2048
|
+
let x = 0;
|
|
2049
|
+
if (_ & 1) {
|
|
2050
|
+
if (y + 4 > v) return;
|
|
2051
|
+
x = e.getInt32(y, !1), y += 4;
|
|
1543
2052
|
}
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
2053
|
+
_ & 4 && (y += 4);
|
|
2054
|
+
let S;
|
|
2055
|
+
S = d ? n : p === null ? r.offset : p;
|
|
2056
|
+
let C = S + (_ & 1 ? x : 0), w = g;
|
|
2057
|
+
for (let t = 0; t < b; t++) {
|
|
2058
|
+
let t = m, n = h, r = 0;
|
|
2059
|
+
if (_ & 256) {
|
|
2060
|
+
if (y + 4 > v) break;
|
|
2061
|
+
t = e.getUint32(y, !1), y += 4;
|
|
2062
|
+
}
|
|
2063
|
+
if (_ & 512) {
|
|
2064
|
+
if (y + 4 > v) break;
|
|
2065
|
+
n = e.getUint32(y, !1), y += 4;
|
|
2066
|
+
}
|
|
2067
|
+
if (_ & 1024) {
|
|
2068
|
+
if (y + 4 > v) break;
|
|
2069
|
+
r = e.getUint32(y, !1), y += 4;
|
|
2070
|
+
}
|
|
2071
|
+
let a = 0;
|
|
2072
|
+
if (_ & 2048) {
|
|
2073
|
+
if (y + 4 > v) break;
|
|
2074
|
+
a = e.getInt32(y, !1), y += 4;
|
|
2075
|
+
}
|
|
2076
|
+
if (t <= 0 && l.kind === "audio") {
|
|
2077
|
+
let e = this.audioConfig?.sampleRate ?? l.timescale;
|
|
2078
|
+
t = Math.round(1024 * l.timescale / e);
|
|
2079
|
+
}
|
|
2080
|
+
if (n <= 0) continue;
|
|
2081
|
+
if (C + n > i.byteLength) break;
|
|
2082
|
+
let o = i.slice(C, C + n);
|
|
2083
|
+
C += n;
|
|
2084
|
+
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";
|
|
2085
|
+
if (l.kind === "video") {
|
|
2086
|
+
let e = this.splitAvccNalus(o);
|
|
2087
|
+
this.on.chunk?.({
|
|
2088
|
+
kind: "video",
|
|
2089
|
+
type: d,
|
|
2090
|
+
dts: s,
|
|
2091
|
+
pts: c,
|
|
2092
|
+
cts: u,
|
|
2093
|
+
data: o,
|
|
2094
|
+
nalus: e
|
|
2095
|
+
});
|
|
2096
|
+
} else this.on.chunk?.({
|
|
2097
|
+
kind: "audio",
|
|
2098
|
+
type: "key",
|
|
2099
|
+
dts: s,
|
|
2100
|
+
pts: c,
|
|
2101
|
+
cts: u,
|
|
2102
|
+
data: o
|
|
2103
|
+
});
|
|
2104
|
+
w += t;
|
|
2105
|
+
}
|
|
2106
|
+
};
|
|
2107
|
+
parseProgressive = (e, t, n) => {
|
|
2108
|
+
let r = Z(e, 0, n.byteLength, "moov");
|
|
2109
|
+
if (!r) return;
|
|
2110
|
+
let i = [];
|
|
2111
|
+
X(e, r.contentStart, r.offset + r.size, (t) => {
|
|
2112
|
+
if (t.type !== "trak") return;
|
|
2113
|
+
let r = this.collectProgressiveTrackSamples(e, t, n);
|
|
2114
|
+
i.push(...r);
|
|
2115
|
+
}), i.sort((e, t) => e.dts - t.dts || (e.kind === "video" ? -1 : 1));
|
|
2116
|
+
for (let e of i) this.on.chunk?.(e);
|
|
2117
|
+
};
|
|
2118
|
+
collectProgressiveTrackSamples = (e, t, n) => {
|
|
2119
|
+
let r = [], i = Z(e, t.contentStart, t.offset + t.size, "tkhd"), a = Z(e, t.contentStart, t.offset + t.size, "mdia");
|
|
2120
|
+
if (!i || !a) return r;
|
|
2121
|
+
let o = e.getUint32(i.contentStart + (e.getUint8(i.contentStart) === 1 ? 20 : 12), !1), s = this.tracks.get(o);
|
|
2122
|
+
if (!s) return r;
|
|
2123
|
+
let c = Z(e, a.contentStart, a.offset + a.size, "minf"), l = c && Z(e, c.contentStart, c.offset + c.size, "stbl");
|
|
2124
|
+
if (!l) return r;
|
|
2125
|
+
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");
|
|
2126
|
+
if (!u || !d && !f || !m) return r;
|
|
2127
|
+
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();
|
|
2128
|
+
if (h) {
|
|
2129
|
+
let t = e.getUint32(h.contentStart + 4, !1);
|
|
2130
|
+
for (let n = 0; n < t; n++) S.add(e.getUint32(h.contentStart + 8 + n * 4, !1));
|
|
2131
|
+
}
|
|
2132
|
+
let C = [];
|
|
2133
|
+
if (p) {
|
|
2134
|
+
let t = e.getUint32(p.contentStart + 4, !1);
|
|
2135
|
+
for (let n = 0; n < t; n++) {
|
|
2136
|
+
let t = p.contentStart + 8 + n * 12;
|
|
2137
|
+
C.push({
|
|
2138
|
+
firstChunk: e.getUint32(t, !1),
|
|
2139
|
+
samplesPerChunk: e.getUint32(t + 4, !1)
|
|
2140
|
+
});
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
let w = (e) => {
|
|
2144
|
+
if (C.length === 0) return v === b ? 1 : Math.max(1, Math.ceil(v / b));
|
|
2145
|
+
let t = C[0].samplesPerChunk;
|
|
2146
|
+
for (let n of C) if (n.firstChunk <= e) t = n.samplesPerChunk;
|
|
2147
|
+
else break;
|
|
2148
|
+
return t;
|
|
2149
|
+
}, 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;
|
|
2150
|
+
E += 8;
|
|
2151
|
+
let j = 1;
|
|
2152
|
+
for (let t = 1; t <= b && j <= v; t++) {
|
|
2153
|
+
let i = w(t), a = x(t);
|
|
2154
|
+
for (let t = 0; t < i && j <= v; t++, j++) {
|
|
2155
|
+
let t = _ === 0 ? e.getUint32(u.contentStart + 12 + (j - 1) * 4, !1) : _;
|
|
2156
|
+
if (a + t > n.byteLength) return r;
|
|
2157
|
+
let i = n.slice(a, a + t);
|
|
2158
|
+
a += t;
|
|
2159
|
+
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;
|
|
2160
|
+
s.kind === "video" ? r.push({
|
|
2161
|
+
kind: "video",
|
|
2162
|
+
type: f ? "key" : "delta",
|
|
2163
|
+
dts: o,
|
|
2164
|
+
pts: l,
|
|
2165
|
+
cts: d,
|
|
2166
|
+
data: i,
|
|
2167
|
+
nalus: this.splitAvccNalus(i)
|
|
2168
|
+
}) : r.push({
|
|
2169
|
+
kind: "audio",
|
|
2170
|
+
type: "key",
|
|
2171
|
+
dts: o,
|
|
2172
|
+
pts: l,
|
|
2173
|
+
cts: d,
|
|
2174
|
+
data: i
|
|
2175
|
+
}), 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));
|
|
2176
|
+
}
|
|
2177
|
+
}
|
|
2178
|
+
return r;
|
|
2179
|
+
};
|
|
2180
|
+
isAvccKeyFrame = (e) => {
|
|
2181
|
+
let t = 0, n = new DataView(e.buffer, e.byteOffset, e.byteLength);
|
|
2182
|
+
for (; t + 4 <= e.byteLength;) {
|
|
2183
|
+
let r = n.getUint32(t, !1);
|
|
2184
|
+
if (r <= 0 || t + 4 + r > e.byteLength) break;
|
|
2185
|
+
if ((e[t + 4] & 31) == 5) return !0;
|
|
2186
|
+
t += 4 + r;
|
|
2187
|
+
}
|
|
2188
|
+
return !1;
|
|
2189
|
+
};
|
|
2190
|
+
splitAvccNalus = (e) => {
|
|
2191
|
+
let t = [], n = 0, r = new DataView(e.buffer, e.byteOffset, e.byteLength);
|
|
2192
|
+
for (; n + 4 <= e.byteLength;) {
|
|
2193
|
+
let i = r.getUint32(n, !1);
|
|
2194
|
+
if (i <= 0 || n + 4 + i > e.byteLength) break;
|
|
2195
|
+
t.push(e.slice(n, n + 4 + i)), n += 4 + i;
|
|
2196
|
+
}
|
|
2197
|
+
return t;
|
|
1549
2198
|
};
|
|
1550
|
-
},
|
|
2199
|
+
}, re = class {
|
|
1551
2200
|
pattern;
|
|
1552
|
-
cacher = new
|
|
2201
|
+
cacher = new z();
|
|
1553
2202
|
isParseing = !1;
|
|
1554
2203
|
offset = 0;
|
|
1555
2204
|
parser;
|
|
@@ -1558,14 +2207,18 @@ var p = "pr-player-render", m = class {
|
|
|
1558
2207
|
init = (e) => {
|
|
1559
2208
|
switch (this.destroy(), this.pattern = e, this.pattern) {
|
|
1560
2209
|
case "flv":
|
|
1561
|
-
this.parser = new
|
|
2210
|
+
this.parser = new W();
|
|
1562
2211
|
break;
|
|
1563
2212
|
case "hls":
|
|
1564
|
-
this.parser = new
|
|
2213
|
+
this.parser = new q();
|
|
2214
|
+
break;
|
|
2215
|
+
case "dash":
|
|
2216
|
+
case "mp4":
|
|
2217
|
+
this.parser = new ne();
|
|
1565
2218
|
break;
|
|
1566
2219
|
default: throw Error("is error pattern.");
|
|
1567
2220
|
}
|
|
1568
|
-
this.parser.on.
|
|
2221
|
+
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) => {
|
|
1569
2222
|
this.cacher.pushChunk(e), this.on.chunk && this.on.chunk(e);
|
|
1570
2223
|
};
|
|
1571
2224
|
};
|
|
@@ -1582,14 +2235,23 @@ var p = "pr-player-render", m = class {
|
|
|
1582
2235
|
for (;;) {
|
|
1583
2236
|
let e = this.cacher.next(this.offset);
|
|
1584
2237
|
if (this.offset = 0, !e) break;
|
|
1585
|
-
|
|
2238
|
+
try {
|
|
2239
|
+
let t = await this.parser.parse(e);
|
|
2240
|
+
if (this.pattern === "mp4" || this.pattern === "dash") {
|
|
2241
|
+
let t = this.parser;
|
|
2242
|
+
this.offset = t.getDiscardOffset(e.byteLength, e);
|
|
2243
|
+
} else this.offset = t;
|
|
2244
|
+
} catch {
|
|
2245
|
+
this.cacher.discardPayload();
|
|
2246
|
+
continue;
|
|
2247
|
+
}
|
|
1586
2248
|
}
|
|
1587
2249
|
this.isParseing = !1;
|
|
1588
2250
|
} catch {
|
|
1589
|
-
this.
|
|
2251
|
+
this.isParseing = !1, this.cacher.discardPayload();
|
|
1590
2252
|
}
|
|
1591
2253
|
};
|
|
1592
|
-
},
|
|
2254
|
+
}, ie = class {
|
|
1593
2255
|
pattern = "flv";
|
|
1594
2256
|
audioDecoderConfig;
|
|
1595
2257
|
audioDecoder;
|
|
@@ -1607,6 +2269,7 @@ var p = "pr-player-render", m = class {
|
|
|
1607
2269
|
flv: [30, 50],
|
|
1608
2270
|
hls: [200, 300],
|
|
1609
2271
|
dash: [50, 100],
|
|
2272
|
+
mp4: [30, 50],
|
|
1610
2273
|
rtmp: [50, 100]
|
|
1611
2274
|
};
|
|
1612
2275
|
decodingSpeedRatio = 1;
|
|
@@ -1626,8 +2289,12 @@ var p = "pr-player-render", m = class {
|
|
|
1626
2289
|
init = (e) => {
|
|
1627
2290
|
this.destroy(), this.pattern = e, this.baseTime = (/* @__PURE__ */ new Date()).getTime() - performance.now(), this.initDecodeInterval();
|
|
1628
2291
|
};
|
|
1629
|
-
initAudio = (e) => {
|
|
1630
|
-
this.audio.destroy(),
|
|
2292
|
+
initAudio = async (e) => {
|
|
2293
|
+
if (this.audio.destroy(), !(await AudioDecoder.isConfigSupported(e).catch(() => ({ supported: !1 }))).supported) {
|
|
2294
|
+
console.warn("[Decoder] audio config not supported:", e.codec, "audio will be skipped"), this.audioDecoderConfig = void 0, this.audioDecoder = void 0;
|
|
2295
|
+
return;
|
|
2296
|
+
}
|
|
2297
|
+
this.audioDecoderConfig = { ...e }, this.audioDecoder = new AudioDecoder({
|
|
1631
2298
|
output: (e) => {
|
|
1632
2299
|
let t = this.decodingSpeedRatio;
|
|
1633
2300
|
this.on.audio.decode && this.on.audio.decode({
|
|
@@ -1642,15 +2309,18 @@ var p = "pr-player-render", m = class {
|
|
|
1642
2309
|
};
|
|
1643
2310
|
initVideo = (e) => {
|
|
1644
2311
|
this.video.destroy(), this.videoDecoderConfig = { ...e }, this.videoDecoder = new VideoDecoder({
|
|
1645
|
-
output:
|
|
2312
|
+
output: (e) => {
|
|
1646
2313
|
this.frameStartTime ||= e.timestamp;
|
|
1647
2314
|
try {
|
|
1648
|
-
let t = e.timestamp - this.frameStartTime + this.baseTime * 1e3, n =
|
|
1649
|
-
if (e.close(), !n.
|
|
1650
|
-
|
|
2315
|
+
let t = e.timestamp - this.frameStartTime + this.baseTime * 1e3, n = new VideoFrame(e, { timestamp: t });
|
|
2316
|
+
if (e.close(), !n.displayWidth || !n.displayHeight) {
|
|
2317
|
+
n.close();
|
|
2318
|
+
return;
|
|
2319
|
+
}
|
|
2320
|
+
this.on.video.decode?.({
|
|
1651
2321
|
timestamp: t,
|
|
1652
|
-
|
|
1653
|
-
}), this.currentChunk
|
|
2322
|
+
frame: n
|
|
2323
|
+
}), this.currentChunk?.kind === "video" && this.currentChunk.nalus && this.on.nalus?.(this.currentChunk.nalus);
|
|
1654
2324
|
} catch {
|
|
1655
2325
|
e.close();
|
|
1656
2326
|
}
|
|
@@ -1676,7 +2346,12 @@ var p = "pr-player-render", m = class {
|
|
|
1676
2346
|
e -= t;
|
|
1677
2347
|
}
|
|
1678
2348
|
this.nextRenderTime = this.lastRenderTime + e, this.decodeTimer = setTimeout(() => {
|
|
1679
|
-
|
|
2349
|
+
try {
|
|
2350
|
+
this.decode();
|
|
2351
|
+
} catch (e) {
|
|
2352
|
+
console.error("[Decoder] decode error:", e);
|
|
2353
|
+
}
|
|
2354
|
+
this.initDecodeInterval();
|
|
1680
2355
|
}, e);
|
|
1681
2356
|
};
|
|
1682
2357
|
decode = () => {
|
|
@@ -1714,9 +2389,12 @@ var p = "pr-player-render", m = class {
|
|
|
1714
2389
|
}
|
|
1715
2390
|
};
|
|
1716
2391
|
decodeAudio = (e) => {
|
|
1717
|
-
if (
|
|
1718
|
-
|
|
1719
|
-
|
|
2392
|
+
if (this.audioDecoder) try {
|
|
2393
|
+
let t = new EncodedAudioChunk(e);
|
|
2394
|
+
this.audioDecoder.decode(t);
|
|
2395
|
+
} catch (e) {
|
|
2396
|
+
console.warn("[Decoder] audio decode error (AudioDecoder may be unusable):", e);
|
|
2397
|
+
}
|
|
1720
2398
|
};
|
|
1721
2399
|
decodeVideo = (e) => {
|
|
1722
2400
|
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)) {
|
|
@@ -1740,35 +2418,45 @@ var p = "pr-player-render", m = class {
|
|
|
1740
2418
|
this.videoDecoderConfig = void 0, this.videoDecoder?.close(), this.videoDecoder = void 0, this.hasKeyFrame = !1;
|
|
1741
2419
|
}
|
|
1742
2420
|
};
|
|
1743
|
-
},
|
|
2421
|
+
}, ae = class {
|
|
1744
2422
|
renderMap = /* @__PURE__ */ new Map();
|
|
1745
2423
|
constructor() {}
|
|
1746
2424
|
push = async (e) => {
|
|
1747
|
-
let
|
|
2425
|
+
let t = e.frame, n = t.timestamp;
|
|
1748
2426
|
try {
|
|
1749
|
-
let e = [...this.renderMap.
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
let
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
}
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
2427
|
+
let e = [...this.renderMap.entries()].filter(([, e]) => e && !e.pause);
|
|
2428
|
+
if (e.length === 1 && e[0][0] === "default" && e[0][1].writer) {
|
|
2429
|
+
e[0][1].writer.write(t).catch(() => {}), t.close();
|
|
2430
|
+
return;
|
|
2431
|
+
}
|
|
2432
|
+
for (let [r, i] of e) {
|
|
2433
|
+
let { writer: e, offscreen: a, option: o } = i;
|
|
2434
|
+
if (e) {
|
|
2435
|
+
let i;
|
|
2436
|
+
if (r === "default" || !o) i = new VideoFrame(t, { timestamp: n });
|
|
2437
|
+
else {
|
|
2438
|
+
let { sx: e = 0, sy: r = 0, sw: a = t.displayWidth, sh: s = t.displayHeight } = o;
|
|
2439
|
+
i = new VideoFrame(t, {
|
|
2440
|
+
visibleRect: {
|
|
2441
|
+
x: e,
|
|
2442
|
+
y: r,
|
|
2443
|
+
width: a,
|
|
2444
|
+
height: s
|
|
2445
|
+
},
|
|
2446
|
+
displayWidth: a,
|
|
2447
|
+
displayHeight: s,
|
|
2448
|
+
timestamp: n
|
|
2449
|
+
});
|
|
2450
|
+
}
|
|
2451
|
+
e.write(i).catch(() => {}), i.close();
|
|
2452
|
+
} else if (a) if (r === "default" || !o) a.width = t.displayWidth, a.height = t.displayHeight, a.getContext("2d")?.drawImage(t, 0, 0);
|
|
2453
|
+
else {
|
|
2454
|
+
let { sx: e = 0, sy: n = 0, sw: r = t.displayWidth, sh: i = t.displayHeight } = o;
|
|
2455
|
+
a.width = r, a.height = i, a.getContext("2d")?.drawImage(t, e, n, r, i, 0, 0, r, i);
|
|
1768
2456
|
}
|
|
1769
2457
|
}
|
|
1770
2458
|
} catch {}
|
|
1771
|
-
|
|
2459
|
+
t.close();
|
|
1772
2460
|
};
|
|
1773
2461
|
addCut = (e) => {
|
|
1774
2462
|
let { key: t, writable: n, offscreen: r, option: i } = {
|
|
@@ -1816,4 +2504,4 @@ var p = "pr-player-render", m = class {
|
|
|
1816
2504
|
};
|
|
1817
2505
|
};
|
|
1818
2506
|
//#endregion
|
|
1819
|
-
export {
|
|
2507
|
+
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 };
|