(self.webpackChunkscreenity=self.webpackChunkscreenity||[]).push([[75],{9335(t,e,i){"use strict";i.d(e,{MkvMuxerWrapper:()=>n});var s=i(9917),r=i(7621),o=i(4002),u=i(917),h=i(3936);class n{constructor(t){this.options=t,this.audioSource=null,this.started=!1,this.videoTimestampOffsetUs=null,this.audioTimestampOffsetUs=null,this.lastVideoTimestampUs=0,this.lastAudioTimestampUs=0,this._lastDecoderConfig=null,this._writeBuffer=[],this._writeBufferBytes=0,this._writeBufferTimer=null,this._pendingFlush=Promise.resolve(),this._closed=!1,this.debug=t.debug??!1,this.log=(...t)=>this.debug&&void 0,this.warn=(...t)=>this.debug&&console.warn(...t),this.err=(...t)=>this.debug&&console.error(...t),this.videoCodec=t.videoCodec||"vp9",this.audioCodec=t.audioCodec||"opus";const e=new WritableStream({write:t=>{this.bufferWrite(t.data)}});this.target=new u.Kw(e),this.output=new s.k7({format:new r.wQ({appendOnly:!0,minimumClusterDuration:1}),target:this.target}),this.videoSource=new o.pO(this.videoCodec),this.output.addVideoTrack(this.videoSource,{frameRate:this.options.fps})}bufferWrite(t){if(this._closed)return;const e=new Uint8Array(t.byteLength);e.set(t),this._writeBuffer.push(e),this._writeBufferBytes+=e.byteLength,this._writeBufferBytes>=1048576?this.flushWriteBuffer():this._writeBufferTimer||(this._writeBufferTimer=setTimeout(()=>this.flushWriteBuffer(),1e3))}flushWriteBuffer(){if(this._writeBufferTimer&&(clearTimeout(this._writeBufferTimer),this._writeBufferTimer=null),0===this._writeBuffer.length)return;const t=this._writeBuffer,e=this._writeBufferBytes;this._writeBuffer=[],this._writeBufferBytes=0;const i=new Uint8Array(e);let s=0;for(const e of t)i.set(e,s),s+=e.byteLength;this._pendingFlush=this._pendingFlush.catch(()=>{}).then(()=>this.emitChunk(i,null))}enableAudio(){this.audioSource||(this.log("[MKV-MUXER] enableAudio"),this.audioSource=new o.m(this.audioCodec),this.output.addAudioTrack(this.audioSource))}addVideoChunk(t,e){e?.decoderConfig&&(this._lastDecoderConfig=e.decoderConfig);const i=!e?.decoderConfig&&this._lastDecoderConfig?{...e,decoderConfig:this._lastDecoderConfig}:e,s=this.buildPacket(t,i,"video");return this.videoSource.add(s,i)}addAudioChunk(t,e){if(!this.audioSource)return;const i=this.buildPacket(t,e,"audio");return this.audioSource.add(i,e)}async start(){this.started||(this.log("[MKV-MUXER] start"),await this.output.start(),this.started=!0)}async finalize(){this.log("[MKV-MUXER] finalize");try{await this.output.finalize()}finally{this.flushWriteBuffer(),this._closed=!0,await this._pendingFlush}}async flushPending(){this.flushWriteBuffer(),this._closed=!0,await this._pendingFlush}setPausedOffset(t){}setAudioOffset(t){this.audioTimestampOffsetUs=t}emitChunk(t,e){try{const i=this.options.onChunk?.(t,e??null);i instanceof Promise&&i.catch(t=>this.err("[MKV-MUXER] onChunk rejected",t))}catch(t){this.err("[MKV-MUXER] onChunk error",t)}}buildPacket(t,e,i){const s=new Uint8Array(t.byteLength);t.copyTo(s);const r=this.normalizeTimestamp(i,t.timestamp,t.duration||0),o=(t.duration||0)/1e6;return new h.Z(s,t.type,r/1e6,o)}normalizeTimestamp(t,e,i=0){const s="video"===t?"lastVideoTimestampUs":"lastAudioTimestampUs",r=this[s]??0;if("audio"===t){const t=r+i;return this[s]=t,t}if("number"!=typeof e){const t=r+i;return this[s]=t,this.warn("[MKV-MUX] video missing ts, fallback:",t),t}null==this.videoTimestampOffsetUs&&(this.videoTimestampOffsetUs=e);const o=Math.max(0,e-this.videoTimestampOffsetUs);return this[s]=o,o}}},1845(){}}]);