(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 W=Object.defineProperty;var D=s=>{throw TypeError(s)};var _=(s,t,i)=>t in s?W(s,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):s[t]=i;var E=(s,t,i)=>_(s,typeof t!="symbol"?t+"":t,i),M=(s,t,i)=>t.has(s)||D("Cannot "+i);var e=(s,t,i)=>(M(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),a=(s,t,i,k)=>(M(s,t,"write to private field"),k?k.call(s,i):t.set(s,i),i);var d,g,p,w,S,m,v,T,l,b;class i{constructor(o,r={}){h(this,d,"inactive");h(this,g,new t.EventTool);E(this,"on",e(this,g).on);h(this,p);h(this,w);h(this,S,()=>{});a(this,p,k(o,r)),a(this,w,new L(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:f}=q({timeSlice:o,...e(this,p)},e(this,w),()=>{this.stop()});return e(this,S).call(this),a(this,S,f),c}pause(){a(this,d,"paused"),e(this,w).pause(),e(this,g).emit("stateChange",e(this,d))}resume(){if(e(this,d)==="stopped")throw Error("AVRecorder is stopped");a(this,d,"recording"),e(this,w).play(),e(this,g).emit("stateChange",e(this,d))}async stop(){e(this,d)!=="stopped"&&(a(this,d,"stopped"),e(this,S).call(this))}}d=new WeakMap,g=new WeakMap,p=new WeakMap,w=new WeakMap,S=new WeakMap;function k(n,o){const r={bitrate:3e6,expectFPS:30,videoCodec:"avc1.42E032",...o},{streams:c,width:f,height:R,sampleRate:C,channelCount:A}=O(n);return{video:{width:f??1280,height:R??720,expectFPS:r.expectFPS,codec:r.videoCodec},audio:{codec:"aac",sampleRate:C??44100,channelCount:A??2},bitrate:r.bitrate,streams:c}}function O(n){const o=n.getVideoTracks()[0],r={streams:{}};o!=null&&(Object.assign(r,o.getSettings()),r.streams.video=new MediaStreamTrackProcessor({track:o}).readable);const c=n.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 L{constructor(o){h(this,m,performance.now());h(this,v,e(this,m));h(this,T,0);h(this,l,!1);h(this,b,0);this.expectFPS=o}start(){a(this,m,performance.now()),a(this,v,e(this,m))}play(){e(this,l)&&(a(this,l,!1),a(this,m,e(this,m)+(performance.now()-e(this,b))),a(this,v,e(this,v)+(performance.now()-e(this,b))))}pause(){e(this,l)||(a(this,l,!0),a(this,b,performance.now()))}transfromVideo(o){const r=performance.now(),c=r-e(this,m);if(e(this,l)||e(this,T)/c*1e3>this.expectFPS){o.close();return}const f=new VideoFrame(o,{timestamp:c*1e3,duration:(r-e(this,v))*1e3});return a(this,v,r),a(this,T,e(this,T)+1),o.close(),{vf:f,opts:{keyFrame:e(this,T)%30===0}}}transformAudio(o){if(e(this,l)){o.close();return}return o}}m=new WeakMap,v=new WeakMap,T=new WeakMap,l=new WeakMap,b=new WeakMap;function q(n,o,r){let c=null,f=null;const R=t.recodemux({video:{...n.video,bitrate:n.bitrate??3e6},audio:n.audio});let C=!1;if(n.streams.video!=null){let u=null,y=0;const j=x=>{clearTimeout(y),u==null||u.close(),u=x;const P=o.transfromVideo(x.clone());P!=null&&(R.encodeVideo(P.vf,P.opts),y=self.setTimeout(()=>{if(u==null)return;const N=new VideoFrame(u,{timestamp:u.timestamp+1e6,duration:1e6});j(N)},1e3))};o.start();const I=t.autoReadStream(n.streams.video,{onChunk:async x=>{if(C){x.close();return}j(x)},onDone:()=>{}});c=()=>{I(),clearTimeout(y),u==null||u.close()}}n.audio!=null&&n.streams.audio!=null&&(f=t.autoReadStream(n.streams.audio,{onChunk:async u=>{if(C){u.close();return}o.transformAudio(u)!=null&&R.encodeAudio(u)},onDone:()=>{}}));const{stream:A,stop:V}=t.file2stream(R.mp4file,n.timeSlice,()=>{F(),r()});function F(){C=!0,c==null||c(),f==null||f(),R.close(),V()}return{exit:F,stream:A}}s.AVRecorder=i,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}); //# sourceMappingURL=av-recorder.umd.cjs.map