(function(s,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("@webav/internal-utils")):typeof define=="function"&&define.amd?define(["exports","@webav/internal-utils"],t):(s=typeof globalThis<"u"?globalThis:s||self,t(s["av-recorder"]={},s.internalUtils))})(this,function(s,t){"use strict";var B=Object.defineProperty;var D=s=>{throw TypeError(s)};var G=(s,t,i)=>t in s?B(s,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):s[t]=i;var E=(s,t,i)=>G(s,typeof t!="symbol"?t+"":t,i),O=(s,t,i)=>t.has(s)||D("Cannot "+i);var e=(s,t,i)=>(O(s,t,"read from private field"),i?i.call(s):t.get(s)),h=(s,t,i)=>t.has(s)?D("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(s):t.set(s,i),n=(s,t,i,A)=>(O(s,t,"write to private field"),A?A.call(s,i):t.set(s,i),i);var d,v,p,w,T,m,g,S,f,b,k;class i{constructor(o,r={}){h(this,d,"inactive");h(this,v,new t.EventTool);E(this,"on",e(this,v).on);h(this,p);h(this,w);h(this,T,()=>{});n(this,p,A(o,r)),n(this,w,new q(e(this,p).video.expectFPS))}get state(){return e(this,d)}set state(o){throw new Error("state is readonly")}start(o=500){if(e(this,d)==="stopped")throw Error("AVRecorder is stopped");t.Log.info("AVRecorder.start recoding");const{streams:r}=e(this,p);if(r.audio==null&&r.video==null)throw new Error("No available tracks in MediaStream");const{stream:c,exit:l}=z({timeSlice:o,...e(this,p)},e(this,w),()=>{this.stop()});return e(this,T).call(this),n(this,T,l),c}pause(){n(this,d,"paused"),e(this,w).pause(),e(this,v).emit("stateChange",e(this,d))}resume(){if(e(this,d)==="stopped")throw Error("AVRecorder is stopped");n(this,d,"recording"),e(this,w).play(),e(this,v).emit("stateChange",e(this,d))}async stop(){e(this,d)!=="stopped"&&(n(this,d,"stopped"),e(this,T).call(this))}}d=new WeakMap,v=new WeakMap,p=new WeakMap,w=new WeakMap,T=new WeakMap;function A(a,o){const r={bitrate:3e6,expectFPS:30,videoCodec:"avc1.42E032",...o},{streams:c,width:l,height:V,sampleRate:x,channelCount:R}=L(a);return{video:{width:l??1280,height:V??720,expectFPS:r.expectFPS,codec:r.videoCodec},audio:{codec:"aac",sampleRate:x??44100,channelCount:R??2},bitrate:r.bitrate,streams:c}}function L(a){const o=a.getVideoTracks()[0],r={streams:{}};o!=null&&(Object.assign(r,o.getSettings()),r.streams.video=new MediaStreamTrackProcessor({track:o}).readable);const c=a.getAudioTracks()[0];return c!=null&&(Object.assign(r,c.getSettings()),t.Log.info("AVRecorder recording audioConf:",r),r.streams.audio=new MediaStreamTrackProcessor({track:c}).readable),r}class q{constructor(o){h(this,m,performance.now());h(this,g,e(this,m));h(this,S,0);h(this,f,!1);h(this,b,0);h(this,k,30);this.expectFPS=o,n(this,k,Math.floor(o*3))}start(){n(this,m,performance.now()),n(this,g,e(this,m))}play(){e(this,f)&&(n(this,f,!1),n(this,m,e(this,m)+(performance.now()-e(this,b))),n(this,g,e(this,g)+(performance.now()-e(this,b))))}pause(){e(this,f)||(n(this,f,!0),n(this,b,performance.now()))}transfromVideo(o){const r=performance.now(),c=r-e(this,m);if(e(this,f)||e(this,S)/c*1e3>this.expectFPS){o.close();return}const l=new VideoFrame(o,{timestamp:c*1e3,duration:(r-e(this,g))*1e3});return n(this,g,r),n(this,S,e(this,S)+1),o.close(),{vf:l,opts:{keyFrame:e(this,S)%e(this,k)===0}}}transformAudio(o){if(e(this,f)){o.close();return}return o}}m=new WeakMap,g=new WeakMap,S=new WeakMap,f=new WeakMap,b=new WeakMap,k=new WeakMap;function z(a,o,r){let c=null,l=null;const[V,x]=[a.streams.video!=null,a.streams.audio!=null&&a.audio!=null],R=t.recodemux({video:V?{...a.video,bitrate:a.bitrate??3e6}:null,audio:x?a.audio:null});let y=!1;if(V){let u=null,P=0;const j=C=>{clearTimeout(P),u==null||u.close(),u=C;const F=o.transfromVideo(C.clone());F!=null&&(R.encodeVideo(F.vf,F.opts),P=self.setTimeout(()=>{if(u==null)return;const _=new VideoFrame(u,{timestamp:u.timestamp+1e6,duration:1e6});j(_)},1e3))};o.start();const W=t.autoReadStream(a.streams.video,{onChunk:async C=>{if(y){C.close();return}j(C)},onDone:()=>{}});c=()=>{W(),clearTimeout(P),u==null||u.close()}}x&&(l=t.autoReadStream(a.streams.audio,{onChunk:async u=>{if(y){u.close();return}o.transformAudio(u)!=null&&R.encodeAudio(u)},onDone:()=>{}}));const{stream:I,stop:N}=t.file2stream(R.mp4file,a.timeSlice,()=>{M(),r()});function M(){y=!0,c==null||c(),l==null||l(),R.close(),N()}return{exit:M,stream:I}}s.AVRecorder=i,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=av-recorder.umd.cjs.map