(function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i["web-demuxer"]={}))})(this,function(i){"use strict";var R=Object.defineProperty;var b=(i,r,l)=>r in i?R(i,r,{enumerable:!0,configurable:!0,writable:!0,value:l}):i[r]=l;var A=(i,r,l)=>b(i,typeof r!="symbol"?r+"":r,l);var r=(e=>(e[e.AVMEDIA_TYPE_UNKNOWN=-1]="AVMEDIA_TYPE_UNKNOWN",e[e.AVMEDIA_TYPE_VIDEO=0]="AVMEDIA_TYPE_VIDEO",e[e.AVMEDIA_TYPE_AUDIO=1]="AVMEDIA_TYPE_AUDIO",e[e.AVMEDIA_TYPE_DATA=2]="AVMEDIA_TYPE_DATA",e[e.AVMEDIA_TYPE_SUBTITLE=3]="AVMEDIA_TYPE_SUBTITLE",e[e.AVMEDIA_TYPE_ATTACHMENT=4]="AVMEDIA_TYPE_ATTACHMENT",e[e.AVMEDIA_TYPE_NB=5]="AVMEDIA_TYPE_NB",e))(r||{}),l=(e=>(e[e.AV_LOG_QUIET=-8]="AV_LOG_QUIET",e[e.AV_LOG_PANIC=0]="AV_LOG_PANIC",e[e.AV_LOG_FATAL=8]="AV_LOG_FATAL",e[e.AV_LOG_ERROR=16]="AV_LOG_ERROR",e[e.AV_LOG_WARNING=24]="AV_LOG_WARNING",e[e.AV_LOG_INFO=32]="AV_LOG_INFO",e[e.AV_LOG_VERBOSE=40]="AV_LOG_VERBOSE",e[e.AV_LOG_DEBUG=48]="AV_LOG_DEBUG",e[e.AV_LOG_TRACE=56]="AV_LOG_TRACE",e))(l||{}),Z=(e=>(e[e.AVSEEK_FLAG_BACKWARD=1]="AVSEEK_FLAG_BACKWARD",e[e.AVSEEK_FLAG_BYTE=2]="AVSEEK_FLAG_BYTE",e[e.AVSEEK_FLAG_ANY=4]="AVSEEK_FLAG_ANY",e[e.AVSEEK_FLAG_FRAME=8]="AVSEEK_FLAG_FRAME",e))(Z||{}),s=(e=>(e.FFmpegWorkerLoaded="FFmpegWorkerLoaded",e.WASMRuntimeInitialized="WASMRuntimeInitialized",e.LoadWASM="LoadWASM",e.GetAVPacket="GetAVPacket",e.GetAVPackets="GetAVPackets",e.GetAVStream="GetAVStream",e.GetAVStreams="GetAVStreams",e.GetMediaInfo="GetMediaInfo",e.ReadAVPacket="ReadAVPacket",e.AVPacketStream="AVPacketStream",e.ReadNextAVPacket="ReadNextAVPacket",e.StopReadAVPacket="StopReadAVPacket",e.SetAVLogLevel="SetAVLogLevel",e))(s||{});const W="KGZ1bmN0aW9uKCl7InVzZSBzdHJpY3QiO3ZhciByPShlPT4oZS5GRm1wZWdXb3JrZXJMb2FkZWQ9IkZGbXBlZ1dvcmtlckxvYWRlZCIsZS5XQVNNUnVudGltZUluaXRpYWxpemVkPSJXQVNNUnVudGltZUluaXRpYWxpemVkIixlLkxvYWRXQVNNPSJMb2FkV0FTTSIsZS5HZXRBVlBhY2tldD0iR2V0QVZQYWNrZXQiLGUuR2V0QVZQYWNrZXRzPSJHZXRBVlBhY2tldHMiLGUuR2V0QVZTdHJlYW09IkdldEFWU3RyZWFtIixlLkdldEFWU3RyZWFtcz0iR2V0QVZTdHJlYW1zIixlLkdldE1lZGlhSW5mbz0iR2V0TWVkaWFJbmZvIixlLlJlYWRBVlBhY2tldD0iUmVhZEFWUGFja2V0IixlLkFWUGFja2V0U3RyZWFtPSJBVlBhY2tldFN0cmVhbSIsZS5SZWFkTmV4dEFWUGFja2V0PSJSZWFkTmV4dEFWUGFja2V0IixlLlN0b3BSZWFkQVZQYWNrZXQ9IlN0b3BSZWFkQVZQYWNrZXQiLGUuU2V0QVZMb2dMZXZlbD0iU2V0QVZMb2dMZXZlbCIsZSkpKHJ8fHt9KTtsZXQgbztzZWxmLnBvc3RNZXNzYWdlKHt0eXBlOiJGRm1wZWdXb3JrZXJMb2FkZWQifSksc2VsZi5hZGRFdmVudExpc3RlbmVyKCJtZXNzYWdlIixhc3luYyBmdW5j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Ht0eXBlOiJTZXRBVkxvZ0xldmVsIixtc2dJZDpzfSl9fSkoKTsK",_=e=>Uint8Array.from(atob(e),t=>t.charCodeAt(0)),G=typeof self<"u"&&self.Blob&&new Blob([_(W)],{type:"text/javascript;charset=utf-8"});function k(e){let t;try{if(t=G&&(self.URL||self.webkitURL).createObjectURL(G),!t)throw"";const d=new Worker(t,{name:e==null?void 0:e.name});return d.addEventListener("error",()=>{(self.URL||self.webkitURL).revokeObjectURL(t)}),d}catch{return new Worker("data:text/javascript;base64,"+W,{name:e==null?void 0:e.name})}finally{t&&(self.URL||self.webkitURL).revokeObjectURL(t)}}const h=1e6;class I{constructor(t){A(this,"ffmpegWorker");A(this,"ffmpegWorkerLoadStatus");A(this,"msgId");A(this,"source");this.ffmpegWorker=new k,this.ffmpegWorkerLoadStatus=new Promise((d,n)=>{this.ffmpegWorker.addEventListener("message",a=>{const{type:u,errMsg:V}=a.data;u===s.FFmpegWorkerLoaded&&this.post(s.LoadWASM,{wasmLoaderPath:t.wasmLoaderPath}),u===s.WASMRuntimeInitialized&&d(),u===s.LoadWASM&&V&&n(V)})}),this.msgId=0}post(t,d,n){this.ffmpegWorker.postMessage({type:t,msgId:n??this.msgId++,data:d})}getFromWorker(t,d){return new Promise((n,a)=>{if(!this.source){a("source is not loaded. call load() first");return}const u=this.msgId,V=o=>{let{data:c}=o;c.type===t&&c.msgId===u&&(c.errMsg?a(c.errMsg):n(c.result),this.ffmpegWorker.removeEventListener("message",V))};this.ffmpegWorker.addEventListener("message",V),this.post(t,d,u)})}async load(t){await this.ffmpegWorkerLoadStatus,this.source=t}destroy(){this.source=void 0,this.ffmpegWorker.terminate()}getAVStream(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:r.AVMEDIA_TYPE_VIDEO,d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:-1;return this.getFromWorker(s.GetAVStream,{source:this.source,streamType:t,streamIndex:d})}getAVStreams(){return this.getFromWorker(s.GetAVStreams,{source:this.source})}getMediaInfo(){return this.getFromWorker(s.GetMediaInfo,{source:this.source})}getAVPacket(t){let d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:r.AVMEDIA_TYPE_VIDEO,n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:-1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:Z.AVSEEK_FLAG_BACKWARD;return this.getFromWorker(s.GetAVPacket,{source:this.source,time:t,streamType:d,streamIndex:n,seekFlag:a})}getAVPackets(t){let d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:Z.AVSEEK_FLAG_BACKWARD;return this.getFromWorker(s.GetAVPackets,{source:this.source,time:t,seekFlag:d})}readAVPacket(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:0,d=arguments.length>1&&arguments[1]!==void 0?arguments[1]:0,n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:r.AVMEDIA_TYPE_VIDEO,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:-1,u=arguments.length>4&&arguments[4]!==void 0?arguments[4]:Z.AVSEEK_FLAG_BACKWARD;const V=new CountQueuingStrategy({highWaterMark:1}),o=this.msgId;let c=0;return new ReadableStream({start:E=>{if(!this.source){E.error("source is not loaded. call load() first");return}const Y=f=>{const m=f.data;m.type===s.ReadAVPacket&&m.msgId===o&&m.errMsg&&(E.error(m.errMsg),this.ffmpegWorker.removeEventListener("message",Y)),m.type===s.AVPacketStream&&m.msgId===o&&(m.result?E.enqueue(m.result):E.close())};this.ffmpegWorker.addEventListener("message",Y),this.post(s.ReadAVPacket,{source:this.source,start:t,end:d,streamType:n,streamIndex:a,seekFlag:u})},pull:()=>{c>0&&this.post(s.ReadNextAVPacket,void 0,o),c++},cancel:()=>{this.post(s.StopReadAVPacket,void 0,o)}},V)}setLogLevel(t){return this.getFromWorker(s.SetAVLogLevel,{level:t})}getVideoStream(t){return this.getAVStream(r.AVMEDIA_TYPE_VIDEO,t)}getAudioStream(t){return this.getAVStream(r.AVMEDIA_TYPE_AUDIO,t)}seekVideoPacket(t,d){return this.getAVPacket(t,r.AVMEDIA_TYPE_VIDEO,void 0,d)}seekAudioPacket(t,d){return this.getAVPacket(t,r.AVMEDIA_TYPE_AUDIO,void 0,d)}readVideoPacket(t,d,n){return this.readAVPacket(t,d,r.AVMEDIA_TYPE_VIDEO,void 0,n)}readAudioPacket(t,d,n){return this.readAVPacket(t,d,r.AVMEDIA_TYPE_AUDIO,void 0,n)}genVideoDecoderConfig(t){var d;return{codec:t.codec_string,codedWidth:t.width,codedHeight:t.height,description:((d=t.extradata)==null?void 0:d.length)>0?t.extradata:void 0}}genEncodedVideoChunk(t){return new EncodedVideoChunk({type:t.keyframe===1?"key":"delta",timestamp:t.timestamp*h,duration:t.duration*h,data:t.data})}genAudioDecoderConfig(t){var d;return{codec:t.codec_string||"",sampleRate:t.sample_rate,numberOfChannels:t.channels,description:((d=t.extradata)==null?void 0:d.length)>0?t.extradata:void 0}}genEncodedAudioChunk(t){return new EncodedAudioChunk({type:t.keyframe===1?"key":"delta",timestamp:t.timestamp*h,duration:t.duration*h,data:t.data})}async getVideoDecoderConfig(){const t=await this.getVideoStream();return this.genVideoDecoderConfig(t)}async seekEncodedVideoChunk(t,d){const n=await this.seekVideoPacket(t,d);return this.genEncodedVideoChunk(n)}async getAudioDecoderConfig(){const t=await this.getAudioStream();return this.genAudioDecoderConfig(t)}async seekEncodedAudioChunk(t,d){const n=await this.seekAudioPacket(t,d);return this.genEncodedAudioChunk(n)}}i.AVLogLevel=l,i.AVMediaType=r,i.AVSeekFlag=Z,i.WebDemuxer=I,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});