@vidtreo/recorder-wc 0.8.5 → 0.8.6
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/dist/vidtreo-recorder.js +520 -122
- package/package.json +2 -2
package/dist/vidtreo-recorder.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var VidtreoRecorder=(function(e){function t(e){return e instanceof Error?e.message:String(e)}var n=class{constructor(){this.audioContext=null,this.analyser=null,this.audioLevelIntervalId=null,this.audioLevel=0,this.getMutedState=null,this.currentStream=null}startTracking(e,n,r){if(!e)throw Error(`Stream is required`);if(!r)throw Error(`getMutedState callback is required`);if(this.stopTracking(),this.currentStream=e,this.getMutedState=r,e.getAudioTracks().length===0)throw Error(`Stream has no audio tracks`);let i=this.getAudioContextClass();if(!i)throw Error(`AudioContext is not supported in this browser`);let a;try{a=new i}catch(e){throw Error(`Failed to create AudioContext: ${t(e)}`)}this.audioContext=a;let o=this.audioContext.createMediaStreamSource(e);this.analyser=this.audioContext.createAnalyser(),this.analyser.fftSize=2048,this.analyser.smoothingTimeConstant=.8,o.connect(this.analyser);let s=new Uint8Array(this.analyser.fftSize);this.audioLevel=0;let c=this.checkMutedState();n.onLevelUpdate(0,c),this.audioLevelIntervalId=window.setInterval(()=>{if(!this.analyser)return;this.analyser.getByteTimeDomainData(s);let e=this.calculateAudioLevel(s);this.audioLevel=e;let t=this.checkMutedState(),r=t?0:e;n.onLevelUpdate(r,t)},100)}stopTracking(){this.audioLevelIntervalId!==null&&(clearInterval(this.audioLevelIntervalId),this.audioLevelIntervalId=null),this.analyser&&=(this.analyser.disconnect(),null),this.audioContext&&=(this.audioContext.close(),null),this.audioLevel=0,this.getMutedState=null,this.currentStream=null}getAudioLevel(){return this.audioLevel}getAudioContextClass(){return window.AudioContext?window.AudioContext:window.webkitAudioContext||null}calculateAudioLevel(e){let t=0;for(let n of e){let e=(n-128)/128;t+=e*e}let n=Math.sqrt(t/e.length),r=n>0?20*Math.log10(n):-60,i=Math.max(0,Math.min(1,(r+50)/50))**.6;return Math.min(100,i*110)}checkMutedState(){if(!this.getMutedState)throw Error(`getMutedState callback is not set`);let e=this.getMutedState();if(!this.currentStream)throw Error(`Current stream is not set`);let t=this.currentStream.getAudioTracks(),n=t.length>0&&t.some(e=>!e.enabled);return e||n}};let r={mp4:`aac`,mov:`aac`,mkv:`opus`,webm:`opus`};function i(e){return r[e]}function a(e,t){return t||i(e)}let o=Object.freeze({format:`mp4`,fps:30,width:1280,height:720,bitrate:5e5,audioCodec:void 0,audioBitrate:128e3,preset:`medium`,packetCount:1200}),s={sd:5e5,hd:1e6,fhd:2e6,"4k":8e6},c={sd:800,hd:1200,fhd:2e3,"4k":4e3};function l(e,t,n,r=`mp4`){if(!(e in s))throw Error(`Invalid preset: ${e}`);if(typeof t!=`number`||t<=0)throw Error(`maxWidth must be a positive number`);if(typeof n!=`number`||n<=0)throw Error(`maxHeight must be a positive number`);let a=i(r);return{format:r,fps:30,width:t,height:n,bitrate:s[e],audioCodec:a,preset:`medium`,packetCount:c[e],audioBitrate:128e3}}let u=new Map;function d(e,t){return`${e}:${t}`}var f=class e{constructor(e){if(this.cachedConfig=null,this.cacheTimestamp=0,this.fetchPromise=null,this.options=e,e.cacheTimeout!==void 0){if(typeof e.cacheTimeout!=`number`||e.cacheTimeout<=0)throw Error(`cacheTimeout must be a positive number`);this.cacheTimeout=e.cacheTimeout}else this.cacheTimeout=3e5}static getInstance(t){let n=d(t.backendUrl,t.apiKey),r=u.get(n);return r||(r=new e(t),u.set(n,r)),r}async fetchConfig(){let e=Date.now();if(this.cachedConfig&&e-this.cacheTimestamp<this.cacheTimeout)return this.cachedConfig;if(this.fetchPromise)return this.fetchPromise;this.fetchPromise=this.fetchConfigFromBackend();try{let t=await this.fetchPromise;return this.cachedConfig=t,this.cacheTimestamp=e,this.fetchPromise=null,t}catch{return this.fetchPromise=null,o}}clearCache(){let e=d(this.options.backendUrl,this.options.apiKey);this.cachedConfig=null,this.cacheTimestamp=0,this.fetchPromise=null,u.delete(e)}static clearAllInstances(){u.clear()}getCurrentConfig(){if(!this.cachedConfig)throw Error(`No cached config available. Call fetchConfig() first.`);return this.cachedConfig}async fetchConfigFromBackend(){let e=`${this.options.backendUrl}/api/v1/videos/config`,t=await fetch(e,{method:`GET`,headers:{Authorization:`Bearer ${this.options.apiKey}`,"Content-Type":`application/json`}});if(!t.ok)throw Error(`Failed to fetch config: ${t.status} ${t.statusText}`);let n=await t.json();if(!n.presetEncoding||typeof n.max_width!=`number`||typeof n.max_height!=`number`)throw Error(`Invalid config response from backend`);return l(n.presetEncoding,n.max_width,n.max_height)}},p=class{constructor(){this.configService=null,this.currentConfig=o,this.configFetched=!1}async initialize(e,t){if(!this.configService){if(!e)throw Error(`apiKey is required`);if(!t)throw Error(`backendUrl is required`);this.configService=f.getInstance({apiKey:e,backendUrl:t}),this.currentConfig=await this.configService.fetchConfig(),this.configFetched=!0}}async fetchConfig(){this.configService&&(this.currentConfig=await this.configService.fetchConfig(),this.configFetched=!0)}async getConfig(){return this.configService&&!this.configFetched&&await this.fetchConfig(),this.currentConfig}clearCache(){if(!this.configService)throw Error(`ConfigService is not initialized`);this.configService.clearCache()}},m=class{constructor(e,t){this.availableDevices={audioinput:[],videoinput:[]},this.selectedCameraDeviceId=null,this.selectedMicDeviceId=null,this.streamManager=e,this.callbacks=t}async getAvailableDevices(){return this.availableDevices=await this.streamManager.getAvailableDevices(),this.callbacks?.onDevicesChanged&&this.callbacks.onDevicesChanged(this.availableDevices),this.availableDevices}setCameraDevice(e){this.selectedCameraDeviceId=e,this.streamManager.setVideoDevice(e),this.callbacks?.onDeviceSelected&&this.callbacks.onDeviceSelected(`camera`,e)}setMicDevice(e){this.selectedMicDeviceId=e,this.streamManager.setAudioDevice(e),this.callbacks?.onDeviceSelected&&this.callbacks.onDeviceSelected(`mic`,e)}getSelectedCameraDeviceId(){return this.selectedCameraDeviceId}getSelectedMicDeviceId(){return this.selectedMicDeviceId}getAvailableDevicesList(){return this.availableDevices}};let h=`pending-uploads`,g=`status`,_=`createdAt`;var v=class{constructor(){this.db=null}init(){return this.db?Promise.resolve():new Promise((e,t)=>{let n=indexedDB.open(`vidtreo-recorder`,1);n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to open database`))},n.onsuccess=()=>{if(!n.result){t(Error(`Database result is null`));return}this.db=n.result,e()},n.onupgradeneeded=e=>{let n=e.target.result;if(!n){t(Error(`Database upgrade result is null`));return}if(!n.objectStoreNames.contains(h)){let e=n.createObjectStore(h,{keyPath:`id`});e.createIndex(g,g,{unique:!1}),e.createIndex(_,_,{unique:!1})}}})}isInitialized(){return this.db!==null}savePendingUpload(e){let t=this.generateUploadId(),n={...e,id:t,status:`pending`,retryCount:0,createdAt:Date.now(),updatedAt:Date.now()};return this.executeTransaction(`readwrite`,e=>{let r=e.add(n);return new Promise((e,n)=>{r.onsuccess=()=>e(t),r.onerror=()=>{r.error?r.error.name===`QuotaExceededError`?n(Error(`Storage quota exceeded. Please free up space or delete old uploads.`)):n(r.error):n(Error(`Failed to save upload`))}})})}getPendingUploads(e){return this.executeTransaction(`readonly`,t=>{let n=e?t.index(g).getAll(e):t.getAll();return new Promise((e,t)=>{n.onsuccess=()=>{if(n.result===void 0){t(Error(`Failed to get uploads: result is undefined`));return}e(n.result)},n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to get uploads`))}})})}updateUploadStatus(e,t){return this.executeTransaction(`readwrite`,n=>{let r=n.get(e);return new Promise((e,i)=>{r.onsuccess=()=>{let a=r.result;if(!a){i(Error(`Upload not found`));return}let o={...a,...t,updatedAt:Date.now()},s=n.put(o);s.onsuccess=()=>e(),s.onerror=()=>{s.error?i(s.error):i(Error(`Failed to update upload`))}},r.onerror=()=>{r.error?i(r.error):i(Error(`Failed to get upload`))}})})}deleteUpload(e){return this.executeTransaction(`readwrite`,t=>{let n=t.delete(e);return new Promise((e,t)=>{n.onsuccess=()=>e(),n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to delete upload`))}})})}async cleanupPermanentlyFailedUploads(e){let t=e===void 0?24:e;if(typeof t!=`number`||t<0)throw Error(`retentionHours must be a non-negative number`);let n=Date.now()-t*36e5,r=(await this.getPendingUploads()).filter(e=>e.status===`failed`&&e.retryCount>=10&&e.updatedAt<n);for(let e of r)await this.deleteUpload(e.id);return r.length}async getTotalStorageSize(){return(await this.getPendingUploads()).reduce((e,t)=>e+t.blob.size,0)}generateUploadId(){return`upload-${Date.now()}-${Math.random().toString(36).substring(2,11)}`}executeTransaction(e,t){if(!this.db)throw Error(`Database not initialized`);return t(this.db.transaction([h],e).objectStore(h))}},y=class{constructor(){this.storageService=null,this.cleanupIntervalId=null}async initialize(e){this.storageService||=new v,this.storageService.isInitialized()||await this.storageService.init(),this.setupCleanupInterval(e)}setupCleanupInterval(e){this.cleanupIntervalId===null&&(this.cleanupIntervalId=window.setInterval(()=>{this.performCleanup().catch(n=>{e(t(n))})},36e5))}async performCleanup(){if(!this.storageService)throw Error(`StorageService not initialized`);await this.storageService.cleanupPermanentlyFailedUploads(24)}getStorageService(){return this.storageService}destroy(){this.cleanupIntervalId!==null&&(clearInterval(this.cleanupIntervalId),this.cleanupIntervalId=null)}};function ee(){let e=globalThis;if(e.__VIDTREO_DEBUG__===!0||e.__VIDTREO_DEV__===!0)return!0;let t=typeof process<`u`&&process?.env?`production`:void 0;return t===`development`||t===`test`||typeof localStorage<`u`&&localStorage.getItem(`VIDTREO_DEBUG`)===`true`}let te=ee(),ne={reset:`\x1B[0m`,bright:`\x1B[1m`,dim:`\x1B[2m`,red:`\x1B[31m`,green:`\x1B[32m`,yellow:`\x1B[33m`,blue:`\x1B[34m`,magenta:`\x1B[35m`,cyan:`\x1B[36m`,white:`\x1B[37m`,gray:`\x1B[90m`};function re(e,t,n){if(!te)return``;let r=n?.prefix||`[${e.toUpperCase()}]`;return`${ne[n?.color||ie(e)]}${r}${ne.reset} ${t}`}function ie(e){switch(e){case`error`:return`red`;case`warn`:return`yellow`;case`info`:return`cyan`;case`debug`:return`gray`;default:return`white`}}function ae(e,t,...n){if(!te)return;let r=re(e,t);console[e](r,...n)}let b={log:(e,...t)=>{ae(`log`,e,...t)},info:(e,...t)=>{ae(`info`,e,...t)},warn:(e,...t)=>{ae(`warn`,e,...t)},error:(e,...t)=>{ae(`error`,e,...t)},debug:(e,...t)=>{ae(`debug`,e,...t)},group:(e,t=`cyan`)=>{if(!te)return;let n=ne[t],r=ne.reset;console.group(`${n}${e}${r}`)},groupEnd:()=>{te&&console.groupEnd()}},oe=`live`;var se=class{constructor(e,t={}){this.currentSourceType=`camera`,this.originalCameraStream=null,this.originalCameraConstraints=null,this.screenShareStream=null,this.screenShareTrackEndHandler=null,this.streamManager=e,this.callbacks=t}getCurrentSourceType(){return this.currentSourceType}getOriginalCameraStream(){return this.originalCameraStream}stopLiveTracks(e){for(let t of e)t.readyState===oe&&t.stop()}stopStreamTracks(e){this.stopLiveTracks(e.getTracks())}stopStreamVideoTracks(e){this.stopLiveTracks(e.getVideoTracks())}isTrackLive(e){return e!==void 0&&e.readyState===oe}areTracksLive(e,t){return this.isTrackLive(e)&&this.isTrackLive(t)}storeOriginalCameraConstraints(e){let t=e.getVideoTracks()[0];if(!t)return;let n=t.getSettings();this.originalCameraConstraints={width:n.width,height:n.height,aspectRatio:n.aspectRatio,frameRate:n.frameRate,deviceId:n.deviceId,facingMode:n.facingMode}}storeOriginalCameraStream(e){let t=e.getVideoTracks()[0],n=e.getAudioTracks()[0];this.areTracksLive(t,n)?this.originalCameraStream=new MediaStream([t,n]):this.originalCameraStream=e}createError(e){return e instanceof Error?e:Error(t(e))}waitForTracksToEnd(e){return new Promise(t=>{setTimeout(()=>{this.screenShareStream=null,t()},e)})}combineScreenShareWithOriginalAudio(e){let t=this.originalCameraStream?this.originalCameraStream.getAudioTracks()[0]:void 0,n=[e];return this.isTrackLive(t)&&n.push(t),new MediaStream(n)}async switchToScreenCapture(){let e=this.streamManager.getStream();e&&(this.storeOriginalCameraConstraints(e),this.storeOriginalCameraStream(e)),this.callbacks.onTransitionStart&&this.callbacks.onTransitionStart(`Select screen to share...`);try{let t=await navigator.mediaDevices.getDisplayMedia({video:!0,audio:!0});this.screenShareStream=t;let n=t.getVideoTracks()[0];if(!n)throw this.stopStreamTracks(t),Error(`No video track found in screen share stream`);let r=this.combineScreenShareWithOriginalAudio(n);e&&e!==this.originalCameraStream&&this.stopStreamVideoTracks(e);let i=t.getAudioTracks();for(let e of i)e.stop();return this.currentSourceType=`screen`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),this.setupScreenShareTrackHandler(r),r}catch(e){this.callbacks.onTransitionEnd&&this.callbacks.onTransitionEnd();let n=t(e);if(n.includes(`NotAllowedError`)||n.includes(`AbortError`)||n.toLowerCase().includes(`permission denied`)||n.toLowerCase().includes(`user denied`))return null;throw e}}setupScreenShareTrackHandler(e){let t=e.getVideoTracks()[0];if(!t)throw Error(`No video track found in screen share stream`);let n=this.screenShareTrackEndHandler;if(n){let e=this.streamManager.getStream();if(e){let t=e.getVideoTracks()[0];t&&t.removeEventListener(`ended`,n)}}this.screenShareTrackEndHandler=async()=>{if(this.currentSourceType===`screen`)try{await this.switchToCamera()}catch(e){this.callbacks.onError&&this.callbacks.onError(this.createError(e))}},t.addEventListener(`ended`,this.screenShareTrackEndHandler)}removeScreenShareTrackHandler(e){if(!(this.screenShareTrackEndHandler&&e))return;let t=e.getVideoTracks()[0];t&&t.removeEventListener(`ended`,this.screenShareTrackEndHandler),this.screenShareTrackEndHandler=null}canReuseStream(e,t){if(!e||t&&e!==this.originalCameraStream)return!1;let n=e.getVideoTracks()[0],r=e.getAudioTracks()[0];return!(!this.areTracksLive(n,r)||this.callbacks.getSelectedCameraDeviceId&&this.callbacks.getSelectedCameraDeviceId()!==n.getSettings().deviceId)}canReuseOriginalStream(){return this.canReuseStream(this.originalCameraStream,!1)}canReuseManagerStream(){let e=this.streamManager.getStream();return e&&this.originalCameraStream?this.canReuseStream(e,!0):!1}getSelectedCameraDeviceId(){return this.callbacks.getSelectedCameraDeviceId?this.callbacks.getSelectedCameraDeviceId():this.streamManager.getVideoDevice()}getSelectedMicDeviceId(){return this.callbacks.getSelectedMicDeviceId?this.callbacks.getSelectedMicDeviceId():this.streamManager.getAudioDevice()}buildVideoConstraints(e){let t={};if(this.originalCameraConstraints){let{deviceId:e,...n}=this.originalCameraConstraints;Object.assign(t,n)}if(e)t.deviceId={exact:e};else if(!t.deviceId){let e=this.getSelectedCameraDeviceId();e&&(t.deviceId={exact:e})}return t}buildAudioConstraints(e){return e?{deviceId:{exact:e}}:!0}validateTrack(e,t,n){if(!this.isTrackLive(e)){this.stopStreamTracks(n);let r=e?e.readyState:`undefined`;throw Error(`Failed to get live camera ${t} track. ReadyState: ${r}`)}}async createCameraStreamWithOriginalAudio(e){let t=this.originalCameraStream?this.originalCameraStream.getAudioTracks()[0]:void 0;if(!this.isTrackLive(t))return null;let n=this.buildVideoConstraints(e),r={video:Object.keys(n).length>0?n:!0,audio:!1},i=await navigator.mediaDevices.getUserMedia(r),a=i.getVideoTracks()[0];this.validateTrack(a,`video`,i);let o=[a];o.push(t);let s=new MediaStream(o);return this.stopLiveTracks(i.getAudioTracks()),this.originalCameraStream=s,s}async createCameraStreamWithNewAudio(e){let t=this.getSelectedMicDeviceId(),n=this.buildVideoConstraints(e),r=this.buildAudioConstraints(t),i={video:Object.keys(n).length>0?n:!0,audio:r},a=await navigator.mediaDevices.getUserMedia(i),o=a.getVideoTracks()[0],s=a.getAudioTracks()[0];return this.validateTrack(o,`video`,a),this.validateTrack(s,`audio`,a),this.originalCameraStream=a,a}async createNewCameraStreamForRecording(){let e=this.getSelectedCameraDeviceId();return await this.createCameraStreamWithOriginalAudio(e)||this.createCameraStreamWithNewAudio(e)}async getCameraStream(){let e=this.streamManager.isRecording(),t=this.getSelectedCameraDeviceId(),n=this.getSelectedMicDeviceId();if(this.streamManager.setVideoDevice(t),this.streamManager.setAudioDevice(n),this.canReuseOriginalStream()){if(!this.originalCameraStream)throw Error(`Original camera stream is null`);return this.originalCameraStream}if(this.canReuseManagerStream()){let e=this.streamManager.getStream();if(!e)throw Error(`Manager stream is null`);return e}this.originalCameraStream&&=null;let r=this.streamManager.getStream();if(!e&&r&&r!==this.originalCameraStream&&(this.stopStreamTracks(r),this.streamManager.setMediaStream(null)),e)return this.streamManager.setVideoDevice(this.getSelectedCameraDeviceId()),this.streamManager.setAudioDevice(this.getSelectedMicDeviceId()),this.createNewCameraStreamForRecording();let i=await this.streamManager.startStream();return this.originalCameraStream=i,i}async switchToCamera(){let e=this.streamManager.isRecording();if(!(!e&&this.currentSourceType===`camera`))try{this.notifyTransitionStart(`Switching to camera...`),await this.handleScreenShareStop();let t=await this.getCameraStream();if(!t)throw Error(`Failed to get camera stream`);await this.applyCameraStream(t,e),this.notifyTransitionEnd()}catch(e){throw this.notifyTransitionEnd(),e}}notifyTransitionStart(e){this.callbacks.onTransitionStart&&this.callbacks.onTransitionStart(e)}notifyTransitionEnd(){this.callbacks.onTransitionEnd&&this.callbacks.onTransitionEnd()}stopScreenShareStreamTracks(e){let t=e.getVideoTracks(),n=e.getAudioTracks();b.debug(`[SourceSwitchManager] stopping screen share tracks`,{videoTracks:t.map(e=>({id:e.id,readyState:e.readyState,displaySurface:e.getSettings().displaySurface,constraints:e.getConstraints()})),audioTracks:n.map(e=>({id:e.id,readyState:e.readyState,constraints:e.getConstraints()}))});for(let e of t)e.stop();for(let e of n)e.stop()}stopDisplayTracks(e){if(e)for(let t of e.getVideoTracks())typeof t.getSettings().displaySurface==`string`&&t.readyState===oe&&(b.debug(`[SourceSwitchManager] stopping display track`,{id:t.id,readyState:t.readyState,constraints:t.getConstraints(),settings:t.getSettings()}),t.stop())}async handleScreenShareStop(){if(this.currentSourceType!==`screen`)return;b.debug(`[SourceSwitchManager] handleScreenShareStop invoked`,{currentSourceType:this.currentSourceType,hasScreenShareStream:!!this.screenShareStream});let e=this.screenShareStream,t=this.streamManager.getStream();e&&(this.removeScreenShareTrackHandler(e),this.stopScreenShareStreamTracks(e),this.stopDisplayTracks(e),this.screenShareStream=null,await this.waitForTracksToEnd(100)),t&&(this.stopStreamVideoTracks(t),this.stopDisplayTracks(t)),this.currentSourceType=`camera`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),b.debug(`[SourceSwitchManager] handleScreenShareStop completed`,{hasScreenShareStream:!!this.screenShareStream,currentSourceType:this.currentSourceType})}async applyCameraStream(e,t){this.streamManager.setMediaStream(e);let n=this.currentSourceType!==`camera`;this.currentSourceType=`camera`,n&&this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),t&&await this.streamManager.switchVideoSource(e),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(e)}async toggleSource(){if(this.streamManager.isRecording())try{this.currentSourceType===`camera`?await this.switchToScreen():await this.switchToCamera()}catch(e){this.handleToggleError(e)}}async switchToScreen(){let e=await this.switchToScreenCapture();if(!e){this.notifyTransitionEnd();return}this.notifyTransitionStart(`Switching to screen...`),await this.streamManager.switchVideoSource(e),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(e),this.notifyTransitionEnd()}handleToggleError(e){this.notifyTransitionEnd();let n=t(e);n.includes(`NotAllowedError`)||n.includes(`AbortError`)?this.currentSourceType===`screen`&&this.switchToCamera().catch(e=>{this.callbacks.onError&&this.callbacks.onError(this.createError(e))}):this.callbacks.onError&&this.callbacks.onError(this.createError(e))}async handleRecordingStop(){if(this.currentSourceType!==`screen`){this.cleanup();return}try{let e=this.streamManager.getStream();e&&(this.removeScreenShareTrackHandler(e),this.stopStreamVideoTracks(e));let t=await this.getCameraStream();if(!t)throw Error(`Failed to get camera stream`);this.streamManager.setMediaStream(t),this.currentSourceType=`camera`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(t)}catch(e){throw this.callbacks.onError&&this.callbacks.onError(this.createError(e)),e}this.cleanup()}cleanup(){this.screenShareStream&&=(this.removeScreenShareTrackHandler(this.screenShareStream),this.stopScreenShareStreamTracks(this.screenShareStream),null);let e=this.streamManager.getStream();e&&this.removeScreenShareTrackHandler(e),this.screenShareTrackEndHandler=null,this.originalCameraStream=null,this.originalCameraConstraints=null}setCallbacks(e){this.callbacks={...this.callbacks,...e}}};let ce=Object.freeze({width:{ideal:o.width},height:{ideal:o.height},frameRate:{ideal:o.fps}}),le=Object.freeze({video:ce,audio:!0});Object.freeze({mimeType:`video/webm;codecs=vp9,opus`});var ue=class{constructor(e={}){this.mediaStream=null,this.state=`idle`,this.eventListeners=new Map,this.selectedAudioDeviceId=null,this.selectedVideoDeviceId=null,this.streamConfig={...le,...e}}getState(){return this.state}getStream(){return this.mediaStream}getAudioStreamForAnalysis(){return this.mediaStream&&this.mediaStream.getAudioTracks().length>0?this.mediaStream:null}isActive(){return this.state===`active`||this.state===`recording`}on(e,t){this.eventListeners.has(e)||this.eventListeners.set(e,new Set);let n=this.eventListeners.get(e);return n&&n.add(t),()=>{this.off(e,t)}}off(e,t){let n=this.eventListeners.get(e);n&&n.delete(t)}once(e,t){let n=(r=>{t(r),this.off(e,n)});return this.on(e,n)}emit(e,t){let n=this.eventListeners.get(e);if(n)for(let e of n)try{e(t)}catch{}}setState(e){if(this.state===e)return;let t=this.state;this.state=e,this.emit(`statechange`,{state:e,previousState:t})}setAudioDevice(e){this.selectedAudioDeviceId=e}setVideoDevice(e){this.selectedVideoDeviceId=e}getAudioDevice(){return this.selectedAudioDeviceId}getVideoDevice(){return this.selectedVideoDeviceId}async getAvailableDevices(){try{let e=await navigator.mediaDevices.enumerateDevices();return{audioinput:e.filter(e=>e.kind===`audioinput`),videoinput:e.filter(e=>e.kind===`videoinput`)}}catch(e){throw Error(`Failed to enumerate devices: ${t(e)}`)}}buildDeviceConstraints(e,t){return e?typeof t==`object`?{...t,deviceId:{exact:e}}:{deviceId:{exact:e}}:t}buildVideoConstraints(e){return this.buildDeviceConstraints(e,this.streamConfig.video)}buildAudioConstraints(e){return this.buildDeviceConstraints(e,this.streamConfig.audio)}async startStream(){if(b.debug(`[StreamManager] startStream called`,{hasExistingStream:!!this.mediaStream,selectedVideoDeviceId:this.selectedVideoDeviceId,selectedAudioDeviceId:this.selectedAudioDeviceId}),this.mediaStream){let e=this.mediaStream.getVideoTracks()[0];if(this.selectedVideoDeviceId===null)if(e?.getSettings?.()?.deviceId)b.debug(`[StreamManager] Stopping existing stream to recreate`),this.stopStream();else return b.debug(`[StreamManager] Reusing existing stream`),this.mediaStream;if(e?.getSettings&&e.getSettings().deviceId===this.selectedVideoDeviceId)return b.debug(`[StreamManager] Existing stream matches device, reusing`),this.mediaStream;b.debug(`[StreamManager] Device changed, stopping existing stream`),this.stopStream()}this.setState(`starting`),b.debug(`[StreamManager] State set to 'starting'`);try{let e={video:this.buildVideoConstraints(this.selectedVideoDeviceId),audio:this.buildAudioConstraints(this.selectedAudioDeviceId)};return b.debug(`[StreamManager] Requesting media stream with constraints`,{hasVideo:!!e.video,hasAudio:!!e.audio}),this.mediaStream=await navigator.mediaDevices.getUserMedia(e),b.info(`[StreamManager] Media stream obtained`,{streamId:this.mediaStream.id,videoTracks:this.mediaStream.getVideoTracks().length,audioTracks:this.mediaStream.getAudioTracks().length}),this.setState(`active`),b.debug(`[StreamManager] State set to 'active'`),b.debug(`[StreamManager] Emitting streamstart event`),this.emit(`streamstart`,{stream:this.mediaStream}),this.mediaStream}catch(e){let n=e instanceof Error?e:Error(t(e));throw b.error(`[StreamManager] Failed to start stream`,n),this.setState(`error`),this.emit(`error`,{error:n}),n}}stopStream(){if(this.mediaStream){for(let e of this.mediaStream.getTracks())e.stop();this.mediaStream=null}this.state!==`idle`&&(this.setState(`idle`),this.emit(`streamstop`,void 0))}stopStreamTracks(e){for(let t of e.getTracks())t.stop()}isTrackLive(e){return e!==void 0&&e.readyState===`live`}async tryReplaceTrack(e,t,n){let r=e.replaceTrack;if(typeof r!=`function`)return!1;try{await r.call(e,t),e.stop();for(let e of n.getTracks())e!==t&&e.stop();return t.stop(),!0}catch{return!1}}recreateStreamWithNewTrack(e,t,n){for(let t of n.getTracks())t!==e&&t.stop();let r=[e];this.isTrackLive(t)&&t&&r.push(t);let i=new MediaStream(r);if(this.mediaStream)for(let e of this.mediaStream.getTracks())e!==t&&e.stop();return i}async switchDeviceTrack(e,t,n){if(!this.mediaStream)throw Error(`No active stream to switch device`);let r=t===`video`?this.mediaStream.getVideoTracks()[0]:this.mediaStream.getAudioTracks()[0],i=t===`video`?this.mediaStream.getAudioTracks()[0]:this.mediaStream.getVideoTracks()[0];if(!r){let e=t===`video`?`video`:`audio`;throw Error(`No ${e} track in current stream`)}let a={[t]:this.buildDeviceConstraints(e,n)},o=await navigator.mediaDevices.getUserMedia(a),s=t===`video`?o.getVideoTracks()[0]:o.getAudioTracks()[0];if(!s){this.stopStreamTracks(o);let e=t===`video`?`video`:`audio`;throw Error(`Failed to get new ${e} track`)}return await this.tryReplaceTrack(r,s,o)||(r.stop(),this.mediaStream=this.recreateStreamWithNewTrack(s,i,o)),t===`video`?(this.selectedVideoDeviceId=e,this.emit(`videosourcechange`,{stream:this.mediaStream})):this.selectedAudioDeviceId=e,this.mediaStream}switchVideoDevice(e){return this.switchDeviceTrack(e,`video`,this.streamConfig.video)}switchAudioDevice(e){return this.switchDeviceTrack(e,`audio`,this.streamConfig.audio)}setMediaStream(e){this.mediaStream=e}setAudioTracksEnabled(e){if(!this.mediaStream)return;let t=this.mediaStream.getAudioTracks();for(let n of t)n.enabled=e}destroy(){this.stopStream(),this.eventListeners.clear(),this.setState(`idle`)}};let de=[`Bytes`,`KB`,`MB`,`GB`],fe=1024;function pe(e){if(e===0)return`0 Bytes`;let t=Math.floor(Math.log(e)/Math.log(fe));return`${Math.round(e/fe**t*100)/100} ${de[t]}`}function me(e){let t=Math.floor(e/3600),n=Math.floor(e%3600/60),r=e%60;return t>0?`${t.toString().padStart(2,`0`)}:${n.toString().padStart(2,`0`)}:${r.toString().padStart(2,`0`)}`:`${n.toString().padStart(2,`0`)}:${r.toString().padStart(2,`0`)}`}function he(e,t){if(e==null)throw Error(t);return e}function ge(e,t=`StreamProcessor`){if(!e)throw Error(`${t} is required`);return e}let _e=1e3;var ve=class{constructor(e){this.recordingStartTime=0,this.recordingTimer=null,this.pauseStartTime=null,this.totalPausedTime=0,this.streamProcessor=null,this.bufferSizeUpdateInterval=null,this.streamManager=e}isRecording(){return this.streamManager.getState()===`recording`}getStreamProcessor(){return this.streamProcessor}getAudioStreamForAnalysis(){if(this.streamProcessor){let e=this.streamProcessor.getAudioStreamForAnalysis();if(e)return e}return this.streamManager.getAudioStreamForAnalysis()}async startRecording(e,t){let n=this.streamManager.getStream();if(b.debug(`[StreamRecordingState] startRecording called`,{hasMediaStream:!!n,isRecording:this.isRecording(),hasProcessor:!!e,audioTracks:n?.getAudioTracks().length||0}),!n)throw Error(`Stream must be started before recording`);if(this.isRecording())return b.debug(`[StreamRecordingState] Already recording, returning`),Promise.resolve();this.streamProcessor=e,b.debug(`[StreamRecordingState] StreamProcessor assigned, setting callbacks`),e.setOnMuteStateChange(e=>{this.streamManager.emit(`audiomutetoggle`,{muted:e})}),e.setOnSourceChange(e=>{this.streamManager.emit(`videosourcechange`,{stream:e})}),this.bufferSizeUpdateInterval=window.setInterval(()=>{if(!this.streamProcessor)return;let e=this.streamProcessor.getBufferSize(),t=pe(e);this.streamManager.emit(`recordingbufferupdate`,{size:e,formatted:t})},_e),b.debug(`[StreamRecordingState] Starting processing`),await e.startProcessing(n,t),b.info(`[StreamRecordingState] Processing started and worker ready`),this.resetRecordingState(),this.streamManager.setState(`recording`),this.streamManager.emit(`recordingstart`,{recorder:null}),this.startRecordingTimer()}async stopRecording(){if(b.debug(`[StreamRecordingState] stopRecording called`,{hasStreamProcessor:!!this.streamProcessor,isRecording:this.isRecording()}),!(this.streamProcessor&&this.isRecording()))throw Error(`Not currently recording`);this.streamManager.setState(`stopping`),this.clearRecordingTimer(),this.clearBufferSizeInterval(),this.resetPauseState(),b.debug(`[StreamRecordingState] Finalizing stream processor`);let e=await this.streamProcessor.finalize();return b.info(`[StreamRecordingState] Stream processor finalized`,{blobSize:e.blob.size,hasBlob:!!e.blob}),this.streamManager.setState(`active`),this.streamManager.emit(`recordingstop`,{blob:e.blob,mimeType:`video/mp4`}),this.streamProcessor=null,b.debug(`[StreamRecordingState] StreamProcessor cleared`),e.blob}pauseRecording(){this.clearRecordingTimer(),this.pauseStartTime===null&&(this.pauseStartTime=Date.now()),this.streamProcessor&&this.isRecording()&&this.streamProcessor.pause()}resumeRecording(){if(this.pauseStartTime!==null){let e=Date.now()-this.pauseStartTime;this.totalPausedTime+=e,this.pauseStartTime=null}this.startRecordingTimer(),this.streamProcessor&&this.isRecording()&&this.streamProcessor.resume()}toggleMute(){ge(this.streamProcessor,`StreamProcessor`).toggleMute()}muteAudio(){this.streamProcessor?(this.streamProcessor.isMutedState()||this.streamProcessor.toggleMute(),this.streamManager.setAudioTracksEnabled(!1)):this.streamManager.getStream()&&(this.streamManager.setAudioTracksEnabled(!1),this.streamManager.emit(`audiomutetoggle`,{muted:!0}))}unmuteAudio(){this.streamProcessor?(this.streamProcessor.isMutedState()&&this.streamProcessor.toggleMute(),this.streamManager.setAudioTracksEnabled(!0)):this.streamManager.getStream()&&(this.streamManager.setAudioTracksEnabled(!0),this.streamManager.emit(`audiomutetoggle`,{muted:!1}))}isMuted(){if(this.streamProcessor)return this.streamProcessor.isMutedState();let e=this.streamManager.getStream();if(e){let t=e.getAudioTracks();return t.length>0&&t.every(e=>!e.enabled)}return!1}async switchVideoSource(e){await ge(this.streamProcessor,`StreamProcessor`).switchVideoSource(e)}getCurrentVideoSource(){return he(ge(this.streamProcessor,`StreamProcessor`).getCurrentVideoSource(),`Current video source is not available`)}formatTimeElapsed(e){let t=Math.floor(e/60),n=Math.floor(e%60);return`${t.toString().padStart(2,`0`)}:${n.toString().padStart(2,`0`)}`}startRecordingTimer(){this.recordingTimer=window.setInterval(()=>{let e=(Date.now()-this.recordingStartTime-this.totalPausedTime)/1e3,t=this.formatTimeElapsed(e);this.streamManager.emit(`recordingtimeupdate`,{elapsed:e,formatted:t})},_e)}clearRecordingTimer(){this.recordingTimer!==null&&(clearInterval(this.recordingTimer),this.recordingTimer=null)}clearBufferSizeInterval(){this.bufferSizeUpdateInterval!==null&&(clearInterval(this.bufferSizeUpdateInterval),this.bufferSizeUpdateInterval=null)}resetRecordingState(){this.recordingStartTime=Date.now(),this.totalPausedTime=0,this.pauseStartTime=null}resetPauseState(){this.totalPausedTime=0,this.pauseStartTime=null}destroy(){this.streamProcessor&&=(this.streamProcessor.cancel().catch(()=>{}),null),this.clearRecordingTimer(),this.clearBufferSizeInterval()}},ye=class{constructor(e={}){this.streamManager=new ue(e),this.recordingState=new ve(this.streamManager)}getState(){return this.streamManager.getState()}getStream(){return this.streamManager.getStream()}getAudioStreamForAnalysis(){return this.recordingState.getAudioStreamForAnalysis()}isRecording(){return this.recordingState.isRecording()}isActive(){return this.streamManager.isActive()}on(e,t){return this.streamManager.on(e,t)}off(e,t){this.streamManager.off(e,t)}once(e,t){return this.streamManager.once(e,t)}setAudioDevice(e){this.streamManager.setAudioDevice(e)}setVideoDevice(e){this.streamManager.setVideoDevice(e)}getAudioDevice(){return this.streamManager.getAudioDevice()}getVideoDevice(){return this.streamManager.getVideoDevice()}async getAvailableDevices(){return await this.streamManager.getAvailableDevices()}async startStream(){return await this.streamManager.startStream()}stopStream(){this.streamManager.stopStream()}switchVideoDevice(e){return this.streamManager.switchVideoDevice(e)}switchAudioDevice(e){return this.streamManager.switchAudioDevice(e)}async startRecording(e,t){return await this.recordingState.startRecording(e,t)}async stopRecording(){return await this.recordingState.stopRecording()}pauseRecording(){this.recordingState.pauseRecording()}resumeRecording(){this.recordingState.resumeRecording()}toggleMute(){this.recordingState.toggleMute()}muteAudio(){this.recordingState.muteAudio()}unmuteAudio(){this.recordingState.unmuteAudio()}isMuted(){return this.recordingState.isMuted()}async switchVideoSource(e){return await this.recordingState.switchVideoSource(e)}setMediaStream(e){this.streamManager.setMediaStream(e)}getCurrentVideoSource(){return this.recordingState.getCurrentVideoSource()}destroy(){this.recordingState.destroy(),this.streamManager.destroy()}},be=class{constructor(e,n){this.isProcessing=!1,this.retryTimeoutId=null,this.callbacks={},this.storageService=e,this.uploadService=n,this.networkOnlineHandler=()=>{this.processQueue().catch(e=>{let n=t(e);this.callbacks.onUploadError?.(`network-recovery`,Error(n))})},window.addEventListener(`online`,this.networkOnlineHandler),this.processingIntervalId=window.setInterval(()=>{this.processQueue().catch(e=>{let n=t(e);this.callbacks.onUploadError?.(`processing-loop`,Error(n))})},5e3)}destroy(){this.clearTimer(this.processingIntervalId,clearInterval),this.clearTimer(this.retryTimeoutId,clearTimeout),window.removeEventListener(`online`,this.networkOnlineHandler)}setCallbacks(e){this.callbacks=e}async queueUpload(e){let t=await this.storageService.savePendingUpload(e);return this.processQueue(),t}async processQueue(){if(!this.isProcessing){this.isProcessing=!0;try{if(!this.storageService.isInitialized())throw Error(`Database not initialized`);let e=await this.storageService.getPendingUploads(`pending`);if(e.length>0){let t=this.getOldestUpload(e);await this.processUpload(t),this.isProcessing=!1;return}let t=(await this.storageService.getPendingUploads(`failed`)).filter(e=>e.retryCount<10);if(t.length>0){let e=this.getOldestFailedUpload(t),n=this.calculateRetryDelay(e.retryCount),r=Date.now()-e.updatedAt;if(r>=n)await this.storageService.updateUploadStatus(e.id,{status:`pending`,retryCount:e.retryCount}),await this.processUpload(e);else{let e=n-r;this.scheduleRetry(e)}}this.isProcessing=!1}catch(e){throw this.isProcessing=!1,Error(`Error processing upload queue: ${t(e)}`)}}}getPendingUploads(){return this.storageService.getPendingUploads()}async getStats(){let e=await this.storageService.getPendingUploads(),t={pending:0,uploading:0,failed:0,total:e.length};for(let n of e)n.status===`pending`?t.pending+=1:n.status===`uploading`?t.uploading+=1:n.status===`failed`&&(t.failed+=1);return t}getOldestUpload(e){if(e.length===0)throw Error(`Cannot get oldest upload from empty array`);return e.sort((e,t)=>e.createdAt-t.createdAt)[0]}getOldestFailedUpload(e){if(e.length===0)throw Error(`Cannot get oldest failed upload from empty array`);return e.sort((e,t)=>e.updatedAt-t.updatedAt)[0]}async processUpload(e){try{await this.storageService.updateUploadStatus(e.id,{status:`uploading`});let t=await this.uploadService.uploadVideo(e.blob,{apiKey:e.apiKey,backendUrl:e.backendUrl,filename:e.filename,duration:e.duration,metadata:e.metadata,userMetadata:e.userMetadata,onProgress:t=>{this.callbacks.onUploadProgress?.(e.id,t)}});await this.storageService.deleteUpload(e.id),this.callbacks.onUploadComplete?.(e.id,t)}catch(n){let r=t(n),i=e.retryCount+1;if(await this.storageService.updateUploadStatus(e.id,{status:`failed`,retryCount:i,lastError:r}),i>=10)this.callbacks.onUploadError?.(e.id,Error(`Upload failed after 10 attempts: ${r}`));else{let e=this.calculateRetryDelay(i);this.scheduleRetry(e)}}}calculateRetryDelay(e){let t=2e3*1.5**(e-1);return Math.min(t,3e5)}scheduleRetry(e){this.clearTimer(this.retryTimeoutId,clearTimeout),this.retryTimeoutId=window.setTimeout(()=>{this.retryTimeoutId=null,this.processQueue()},e)}clearTimer(e,t){e!==null&&t(e)}},xe=class{async uploadVideo(e,t){if(!t.filename)throw Error(`Filename is required`);if(!e.type||e.type.trim()===``)throw Error(`Blob type is required`);let n=await this.initVideoUpload({apiKey:t.apiKey,backendUrl:t.backendUrl,filename:t.filename,fileSize:e.size,mimeType:e.type,metadata:t.metadata,userMetadata:t.userMetadata});return this.uploadVideoFile(e,n.uploadUrl,{apiKey:t.apiKey,duration:t.duration,onProgress:t.onProgress})}async initVideoUpload(e){let t=`${e.backendUrl}/api/v1/videos/init`,n={filename:e.filename,fileSize:e.fileSize,mimeType:e.mimeType,preProcessed:!0};e.metadata&&(n.metadata=e.metadata),e.userMetadata&&(n.userMetadata=e.userMetadata);let r=await fetch(t,{method:`POST`,headers:{Authorization:`Bearer ${e.apiKey}`,"Content-Type":`application/json`},body:JSON.stringify(n)});if(!r.ok){let e=await this.extractErrorFromResponse(r,`Failed to initialize video upload`);throw Error(e)}return await r.json()}async extractErrorFromResponse(e,t){let n=await this.parseJsonResponse(e);return n&&typeof n==`object`&&`error`in n&&typeof n.error==`string`?n.error:`${t}: ${e.status} ${e.statusText}`}async parseJsonResponse(e){let t=await e.json();return typeof t==`object`&&t?t:null}uploadVideoFile(e,t,n){return new Promise((r,i)=>{let a=new XMLHttpRequest;if(n.onProgress){let e=n.onProgress;a.upload.addEventListener(`progress`,t=>{t.lengthComputable&&e(t.loaded/t.total)})}a.addEventListener(`load`,()=>{if(a.status>=200&&a.status<=299){this.parseSuccessResponse(a,r,i);return}this.parseErrorResponse(a,i)}),a.addEventListener(`error`,()=>{i(Error(`Network error during upload`))}),a.addEventListener(`abort`,()=>{i(Error(`Upload was aborted`))}),a.open(`PUT`,t),a.setRequestHeader(`Authorization`,`Bearer ${n.apiKey}`),a.setRequestHeader(`Content-Type`,e.type),n.duration!==void 0&&a.setRequestHeader(`X-Video-Duration`,n.duration.toString()),a.send(e)})}parseSuccessResponse(e,t,n){let r=this.safeParseJsonFromXhr(e);if(!r){n(Error(`Failed to parse upload response: invalid JSON`));return}t(r)}parseErrorResponse(e,t){let n=this.safeParseJsonFromXhr(e);if(n&&typeof n==`object`&&`error`in n&&typeof n.error==`string`){t(Error(n.error));return}t(Error(`Upload failed: ${e.status} ${e.statusText}`))}safeParseJsonFromXhr(e){if(!e.responseText||e.responseText.trim()===``)return null;let t=e.responseText.trim();if(!(t.startsWith(`{`)&&t.endsWith(`}`))||t.length<2)return null;let n=JSON.parse(e.responseText);return typeof n==`object`&&n?n:null}};
|
|
1
|
+
var VidtreoRecorder=(function(e){function t(e){return e instanceof Error?e.message:String(e)}var n=class{constructor(){this.audioContext=null,this.analyser=null,this.audioLevelIntervalId=null,this.audioLevel=0,this.getMutedState=null,this.currentStream=null}startTracking(e,n,r){if(!e)throw Error(`Stream is required`);if(!r)throw Error(`getMutedState callback is required`);if(this.stopTracking(),this.currentStream=e,this.getMutedState=r,e.getAudioTracks().length===0)throw Error(`Stream has no audio tracks`);let i=this.getAudioContextClass();if(!i)throw Error(`AudioContext is not supported in this browser`);let a;try{a=new i}catch(e){throw Error(`Failed to create AudioContext: ${t(e)}`)}this.audioContext=a;let o=this.audioContext.createMediaStreamSource(e);this.analyser=this.audioContext.createAnalyser(),this.analyser.fftSize=2048,this.analyser.smoothingTimeConstant=.8,o.connect(this.analyser);let s=new Uint8Array(this.analyser.fftSize);this.audioLevel=0;let c=this.checkMutedState();n.onLevelUpdate(0,c),this.audioLevelIntervalId=window.setInterval(()=>{if(!this.analyser)return;this.analyser.getByteTimeDomainData(s);let e=this.calculateAudioLevel(s);this.audioLevel=e;let t=this.checkMutedState(),r=t?0:e;n.onLevelUpdate(r,t)},100)}stopTracking(){this.audioLevelIntervalId!==null&&(clearInterval(this.audioLevelIntervalId),this.audioLevelIntervalId=null),this.analyser&&=(this.analyser.disconnect(),null),this.audioContext&&=(this.audioContext.close(),null),this.audioLevel=0,this.getMutedState=null,this.currentStream=null}getAudioLevel(){return this.audioLevel}getAudioContextClass(){return window.AudioContext?window.AudioContext:window.webkitAudioContext||null}calculateAudioLevel(e){let t=0;for(let n of e){let e=(n-128)/128;t+=e*e}let n=Math.sqrt(t/e.length),r=n>0?20*Math.log10(n):-60,i=Math.max(0,Math.min(1,(r+50)/50))**.6;return Math.min(100,i*110)}checkMutedState(){if(!this.getMutedState)throw Error(`getMutedState callback is not set`);let e=this.getMutedState();if(!this.currentStream)throw Error(`Current stream is not set`);let t=this.currentStream.getAudioTracks(),n=t.length>0&&t.some(e=>!e.enabled);return e||n}};let r={mp4:`aac`,mov:`aac`,mkv:`opus`,webm:`opus`};function i(e){return r[e]}function a(e,t){return t||i(e)}let o=Object.freeze({format:`mp4`,fps:30,width:1280,height:720,bitrate:5e5,audioCodec:void 0,audioBitrate:128e3,preset:`medium`,packetCount:1200}),s={sd:5e5,hd:1e6,fhd:2e6,"4k":8e6},c={sd:800,hd:1200,fhd:2e3,"4k":4e3};function l(e,t,n,r=`mp4`){if(!(e in s))throw Error(`Invalid preset: ${e}`);if(typeof t!=`number`||t<=0)throw Error(`maxWidth must be a positive number`);if(typeof n!=`number`||n<=0)throw Error(`maxHeight must be a positive number`);let a=i(r);return{format:r,fps:30,width:t,height:n,bitrate:s[e],audioCodec:a,preset:`medium`,packetCount:c[e],audioBitrate:128e3}}let u=new Map;function d(e,t){return`${e}:${t}`}var f=class e{constructor(e){if(this.cachedConfig=null,this.cacheTimestamp=0,this.fetchPromise=null,this.options=e,e.cacheTimeout!==void 0){if(typeof e.cacheTimeout!=`number`||e.cacheTimeout<=0)throw Error(`cacheTimeout must be a positive number`);this.cacheTimeout=e.cacheTimeout}else this.cacheTimeout=3e5}static getInstance(t){let n=d(t.backendUrl,t.apiKey),r=u.get(n);return r||(r=new e(t),u.set(n,r)),r}async fetchConfig(){let e=Date.now();if(this.cachedConfig&&e-this.cacheTimestamp<this.cacheTimeout)return this.cachedConfig;if(this.fetchPromise)return this.fetchPromise;this.fetchPromise=this.fetchConfigFromBackend();try{let t=await this.fetchPromise;return this.cachedConfig=t,this.cacheTimestamp=e,this.fetchPromise=null,t}catch{return this.fetchPromise=null,o}}clearCache(){let e=d(this.options.backendUrl,this.options.apiKey);this.cachedConfig=null,this.cacheTimestamp=0,this.fetchPromise=null,u.delete(e)}static clearAllInstances(){u.clear()}getCurrentConfig(){if(!this.cachedConfig)throw Error(`No cached config available. Call fetchConfig() first.`);return this.cachedConfig}async fetchConfigFromBackend(){let e=`${this.options.backendUrl}/api/v1/videos/config`,t=await fetch(e,{method:`GET`,headers:{Authorization:`Bearer ${this.options.apiKey}`,"Content-Type":`application/json`}});if(!t.ok)throw Error(`Failed to fetch config: ${t.status} ${t.statusText}`);let n=await t.json();if(!n.presetEncoding||typeof n.max_width!=`number`||typeof n.max_height!=`number`)throw Error(`Invalid config response from backend`);return l(n.presetEncoding,n.max_width,n.max_height)}},p=class{constructor(){this.configService=null,this.currentConfig=o,this.configFetched=!1}async initialize(e,t){if(!this.configService){if(!e)throw Error(`apiKey is required`);if(!t)throw Error(`backendUrl is required`);this.configService=f.getInstance({apiKey:e,backendUrl:t}),this.currentConfig=await this.configService.fetchConfig(),this.configFetched=!0}}async fetchConfig(){this.configService&&(this.currentConfig=await this.configService.fetchConfig(),this.configFetched=!0)}async getConfig(){return this.configService&&!this.configFetched&&await this.fetchConfig(),this.currentConfig}clearCache(){if(!this.configService)throw Error(`ConfigService is not initialized`);this.configService.clearCache()}},m=class{constructor(e,t){this.availableDevices={audioinput:[],videoinput:[]},this.selectedCameraDeviceId=null,this.selectedMicDeviceId=null,this.streamManager=e,this.callbacks=t}async getAvailableDevices(){return this.availableDevices=await this.streamManager.getAvailableDevices(),this.callbacks?.onDevicesChanged&&this.callbacks.onDevicesChanged(this.availableDevices),this.availableDevices}setCameraDevice(e){this.selectedCameraDeviceId=e,this.streamManager.setVideoDevice(e),this.callbacks?.onDeviceSelected&&this.callbacks.onDeviceSelected(`camera`,e)}setMicDevice(e){this.selectedMicDeviceId=e,this.streamManager.setAudioDevice(e),this.callbacks?.onDeviceSelected&&this.callbacks.onDeviceSelected(`mic`,e)}getSelectedCameraDeviceId(){return this.selectedCameraDeviceId}getSelectedMicDeviceId(){return this.selectedMicDeviceId}getAvailableDevicesList(){return this.availableDevices}};let h=`pending-uploads`,g=`status`,_=`createdAt`;var v=class{constructor(){this.db=null}init(){return this.db?Promise.resolve():new Promise((e,t)=>{let n=indexedDB.open(`vidtreo-recorder`,1);n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to open database`))},n.onsuccess=()=>{if(!n.result){t(Error(`Database result is null`));return}this.db=n.result,e()},n.onupgradeneeded=e=>{let n=e.target.result;if(!n){t(Error(`Database upgrade result is null`));return}if(!n.objectStoreNames.contains(h)){let e=n.createObjectStore(h,{keyPath:`id`});e.createIndex(g,g,{unique:!1}),e.createIndex(_,_,{unique:!1})}}})}isInitialized(){return this.db!==null}savePendingUpload(e){let t=this.generateUploadId(),n={...e,id:t,status:`pending`,retryCount:0,createdAt:Date.now(),updatedAt:Date.now()};return this.executeTransaction(`readwrite`,e=>{let r=e.add(n);return new Promise((e,n)=>{r.onsuccess=()=>e(t),r.onerror=()=>{r.error?r.error.name===`QuotaExceededError`?n(Error(`Storage quota exceeded. Please free up space or delete old uploads.`)):n(r.error):n(Error(`Failed to save upload`))}})})}getPendingUploads(e){return this.executeTransaction(`readonly`,t=>{let n=e?t.index(g).getAll(e):t.getAll();return new Promise((e,t)=>{n.onsuccess=()=>{if(n.result===void 0){t(Error(`Failed to get uploads: result is undefined`));return}e(n.result)},n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to get uploads`))}})})}updateUploadStatus(e,t){return this.executeTransaction(`readwrite`,n=>{let r=n.get(e);return new Promise((e,i)=>{r.onsuccess=()=>{let a=r.result;if(!a){i(Error(`Upload not found`));return}let o={...a,...t,updatedAt:Date.now()},s=n.put(o);s.onsuccess=()=>e(),s.onerror=()=>{s.error?i(s.error):i(Error(`Failed to update upload`))}},r.onerror=()=>{r.error?i(r.error):i(Error(`Failed to get upload`))}})})}deleteUpload(e){return this.executeTransaction(`readwrite`,t=>{let n=t.delete(e);return new Promise((e,t)=>{n.onsuccess=()=>e(),n.onerror=()=>{n.error?t(n.error):t(Error(`Failed to delete upload`))}})})}async cleanupPermanentlyFailedUploads(e){let t=e===void 0?24:e;if(typeof t!=`number`||t<0)throw Error(`retentionHours must be a non-negative number`);let n=Date.now()-t*36e5,r=(await this.getPendingUploads()).filter(e=>e.status===`failed`&&e.retryCount>=10&&e.updatedAt<n);for(let e of r)await this.deleteUpload(e.id);return r.length}async getTotalStorageSize(){return(await this.getPendingUploads()).reduce((e,t)=>e+t.blob.size,0)}generateUploadId(){return`upload-${Date.now()}-${Math.random().toString(36).substring(2,11)}`}executeTransaction(e,t){if(!this.db)throw Error(`Database not initialized`);return t(this.db.transaction([h],e).objectStore(h))}},ee=class{constructor(){this.storageService=null,this.cleanupIntervalId=null}async initialize(e){this.storageService||=new v,this.storageService.isInitialized()||await this.storageService.init(),this.setupCleanupInterval(e)}setupCleanupInterval(e){this.cleanupIntervalId===null&&(this.cleanupIntervalId=window.setInterval(()=>{this.performCleanup().catch(n=>{e(t(n))})},36e5))}async performCleanup(){if(!this.storageService)throw Error(`StorageService not initialized`);await this.storageService.cleanupPermanentlyFailedUploads(24)}getStorageService(){return this.storageService}destroy(){this.cleanupIntervalId!==null&&(clearInterval(this.cleanupIntervalId),this.cleanupIntervalId=null)}};function te(){let e=globalThis;if(e.__VIDTREO_DEBUG__===!0||e.__VIDTREO_DEV__===!0)return!0;let t=typeof process<`u`&&process?.env?`production`:void 0;return t===`development`||t===`test`||typeof localStorage<`u`&&localStorage.getItem(`VIDTREO_DEBUG`)===`true`}let ne=te(),re={reset:`\x1B[0m`,bright:`\x1B[1m`,dim:`\x1B[2m`,red:`\x1B[31m`,green:`\x1B[32m`,yellow:`\x1B[33m`,blue:`\x1B[34m`,magenta:`\x1B[35m`,cyan:`\x1B[36m`,white:`\x1B[37m`,gray:`\x1B[90m`};function ie(e,t,n){if(!ne)return``;let r=n?.prefix||`[${e.toUpperCase()}]`;return`${re[n?.color||ae(e)]}${r}${re.reset} ${t}`}function ae(e){switch(e){case`error`:return`red`;case`warn`:return`yellow`;case`info`:return`cyan`;case`debug`:return`gray`;default:return`white`}}function oe(e,t,...n){if(!ne)return;let r=ie(e,t);console[e](r,...n)}let y={log:(e,...t)=>{oe(`log`,e,...t)},info:(e,...t)=>{oe(`info`,e,...t)},warn:(e,...t)=>{oe(`warn`,e,...t)},error:(e,...t)=>{oe(`error`,e,...t)},debug:(e,...t)=>{oe(`debug`,e,...t)},group:(e,t=`cyan`)=>{if(!ne)return;let n=re[t],r=re.reset;console.group(`${n}${e}${r}`)},groupEnd:()=>{ne&&console.groupEnd()}},se=`live`;var ce=class{constructor(e,t={}){this.currentSourceType=`camera`,this.originalCameraStream=null,this.originalCameraConstraints=null,this.screenShareStream=null,this.screenShareTrackEndHandler=null,this.streamManager=e,this.callbacks=t}getCurrentSourceType(){return this.currentSourceType}getOriginalCameraStream(){return this.originalCameraStream}stopLiveTracks(e){for(let t of e)t.readyState===se&&t.stop()}stopStreamTracks(e){this.stopLiveTracks(e.getTracks())}stopStreamVideoTracks(e){this.stopLiveTracks(e.getVideoTracks())}isTrackLive(e){return e!==void 0&&e.readyState===se}areTracksLive(e,t){return this.isTrackLive(e)&&this.isTrackLive(t)}storeOriginalCameraConstraints(e){let t=e.getVideoTracks()[0];if(!t)return;let n=t.getSettings();this.originalCameraConstraints={width:n.width,height:n.height,aspectRatio:n.aspectRatio,frameRate:n.frameRate,deviceId:n.deviceId,facingMode:n.facingMode}}storeOriginalCameraStream(e){let t=e.getVideoTracks()[0],n=e.getAudioTracks()[0];this.areTracksLive(t,n)?this.originalCameraStream=new MediaStream([t,n]):this.originalCameraStream=e}createError(e){return e instanceof Error?e:Error(t(e))}waitForTracksToEnd(e){return new Promise(t=>{setTimeout(()=>{this.screenShareStream=null,t()},e)})}combineScreenShareWithOriginalAudio(e){let t=this.originalCameraStream?this.originalCameraStream.getAudioTracks()[0]:void 0;y.debug(`[SourceSwitchManager] combineScreenShareWithOriginalAudio`,{hasOriginalCameraStream:!!this.originalCameraStream,originalCameraStreamId:this.originalCameraStream?.id,hasOriginalAudioTrack:!!t,originalAudioTrackId:t?.id,originalAudioTrackReadyState:t?.readyState,originalAudioTrackEnabled:t?.enabled,originalAudioTrackMuted:t?.muted,originalAudioTrackLabel:t?.label,isTrackLive:this.isTrackLive(t),screenVideoTrackId:e.id,screenVideoTrackReadyState:e.readyState});let n=[e];this.isTrackLive(t)&&t?(n.push(t),y.debug(`[SourceSwitchManager] Added original audio track to combined stream`,{audioTrackId:t.id,combinedTracksCount:n.length})):y.warn(`[SourceSwitchManager] Original audio track is not live, not adding to combined stream`,{audioTrackId:t?.id,audioTrackReadyState:t?.readyState,combinedTracksCount:n.length});let r=new MediaStream(n);return y.debug(`[SourceSwitchManager] Combined stream created`,{combinedStreamId:r.id,combinedStreamVideoTracksCount:r.getVideoTracks().length,combinedStreamAudioTracksCount:r.getAudioTracks().length,combinedStreamAudioTrackId:r.getAudioTracks()[0]?.id,combinedStreamAudioTrackReadyState:r.getAudioTracks()[0]?.readyState}),r}handleScreenSelectionError(){this.callbacks.onScreenSelectionEnd&&this.callbacks.onScreenSelectionEnd(),this.callbacks.onTransitionEnd&&this.callbacks.onTransitionEnd()}isPermissionDeniedError(e){let n=t(e);return n.includes(`NotAllowedError`)||n.includes(`AbortError`)||n.toLowerCase().includes(`permission denied`)||n.toLowerCase().includes(`user denied`)}async processScreenShareStream(e,t){this.screenShareStream=e;let n=e.getVideoTracks()[0];if(!n)throw this.stopStreamTracks(e),Error(`No video track found in screen share stream`);let r=this.combineScreenShareWithOriginalAudio(n);t&&t!==this.originalCameraStream&&this.stopStreamVideoTracks(t);let i=e.getAudioTracks();for(let e of i)e.stop();return this.currentSourceType=`screen`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),this.setupScreenShareTrackHandler(r),r}async switchToScreenCapture(){let e=this.streamManager.getStream();e&&(this.storeOriginalCameraConstraints(e),this.storeOriginalCameraStream(e)),this.callbacks.onTransitionStart&&this.callbacks.onTransitionStart(`Select screen to share...`),this.callbacks.onScreenSelectionStart&&this.callbacks.onScreenSelectionStart();try{let t=await navigator.mediaDevices.getDisplayMedia({video:!0,audio:!0});return await this.processScreenShareStream(t,e)}catch(e){if(this.handleScreenSelectionError(),this.isPermissionDeniedError(e))return null;throw e}}setupScreenShareTrackHandler(e){let t=e.getVideoTracks()[0];if(!t)throw Error(`No video track found in screen share stream`);let n=this.screenShareTrackEndHandler;if(n){let e=this.streamManager.getStream();if(e){let t=e.getVideoTracks()[0];t&&t.removeEventListener(`ended`,n)}}this.screenShareTrackEndHandler=async()=>{if(this.currentSourceType===`screen`)try{await this.switchToCamera()}catch(e){this.callbacks.onError&&this.callbacks.onError(this.createError(e))}},t.addEventListener(`ended`,this.screenShareTrackEndHandler)}removeScreenShareTrackHandler(e){if(!(this.screenShareTrackEndHandler&&e))return;let t=e.getVideoTracks()[0];t&&t.removeEventListener(`ended`,this.screenShareTrackEndHandler),this.screenShareTrackEndHandler=null}canReuseStream(e,t){if(!e||t&&e!==this.originalCameraStream)return!1;let n=e.getVideoTracks()[0],r=e.getAudioTracks()[0];return!(!this.areTracksLive(n,r)||this.callbacks.getSelectedCameraDeviceId&&this.callbacks.getSelectedCameraDeviceId()!==n.getSettings().deviceId)}canReuseOriginalStream(){return this.canReuseStream(this.originalCameraStream,!1)}canReuseManagerStream(){let e=this.streamManager.getStream();return e&&this.originalCameraStream?this.canReuseStream(e,!0):!1}getSelectedCameraDeviceId(){return this.callbacks.getSelectedCameraDeviceId?this.callbacks.getSelectedCameraDeviceId():this.streamManager.getVideoDevice()}getSelectedMicDeviceId(){return this.callbacks.getSelectedMicDeviceId?this.callbacks.getSelectedMicDeviceId():this.streamManager.getAudioDevice()}buildVideoConstraints(e){let t={};if(this.originalCameraConstraints){let{deviceId:e,...n}=this.originalCameraConstraints;Object.assign(t,n)}if(e)t.deviceId={exact:e};else if(!t.deviceId){let e=this.getSelectedCameraDeviceId();e&&(t.deviceId={exact:e})}return t}buildAudioConstraints(e){return e?{deviceId:{exact:e}}:!0}validateTrack(e,t,n){if(!this.isTrackLive(e)){this.stopStreamTracks(n);let r=e?e.readyState:`undefined`;throw Error(`Failed to get live camera ${t} track. ReadyState: ${r}`)}}async createCameraStreamWithOriginalAudio(e){let t=this.originalCameraStream?this.originalCameraStream.getAudioTracks()[0]:void 0;if(y.debug(`[SourceSwitchManager] createCameraStreamWithOriginalAudio`,{hasOriginalCameraStream:!!this.originalCameraStream,originalCameraStreamId:this.originalCameraStream?.id,hasOriginalAudioTrack:!!t,originalAudioTrackId:t?.id,originalAudioTrackReadyState:t?.readyState,originalAudioTrackEnabled:t?.enabled,originalAudioTrackMuted:t?.muted,originalAudioTrackLabel:t?.label,isTrackLive:this.isTrackLive(t),cameraDeviceId:e}),!this.isTrackLive(t))return y.warn(`[SourceSwitchManager] Original audio track is not live, cannot reuse`,{originalAudioTrackId:t?.id,originalAudioTrackReadyState:t?.readyState}),null;let n=this.buildVideoConstraints(e),r={video:Object.keys(n).length>0?n:!0,audio:!1};y.debug(`[SourceSwitchManager] Requesting new video stream`,{constraints:r,cameraDeviceId:e});let i=await navigator.mediaDevices.getUserMedia(r),a=i.getVideoTracks()[0];this.validateTrack(a,`video`,i),y.debug(`[SourceSwitchManager] New video stream obtained`,{newStreamId:i.id,videoTrackId:a.id,videoTrackReadyState:a.readyState,newStreamAudioTracksCount:i.getAudioTracks().length});let o=[a];t&&o.push(t),y.debug(`[SourceSwitchManager] Creating combined stream with original audio`,{videoTrackId:a.id,audioTrackId:t?.id,audioTrackReadyState:t?.readyState,audioTrackEnabled:t?.enabled,audioTrackMuted:t?.muted,combinedTracksCount:o.length});let s=new MediaStream(o);this.stopLiveTracks(i.getAudioTracks());let c=this.originalCameraStream?.id;return this.originalCameraStream=s,y.debug(`[SourceSwitchManager] Combined stream created and assigned`,{combinedStreamId:s.id,previousOriginalCameraStreamId:c,newOriginalCameraStreamId:this.originalCameraStream.id,combinedStreamVideoTracksCount:s.getVideoTracks().length,combinedStreamAudioTracksCount:s.getAudioTracks().length,combinedStreamAudioTrackId:s.getAudioTracks()[0]?.id,combinedStreamAudioTrackReadyState:s.getAudioTracks()[0]?.readyState,audioTrackStillSame:s.getAudioTracks()[0]===t}),s}async createCameraStreamWithNewAudio(e){let t=this.getSelectedMicDeviceId(),n=this.buildVideoConstraints(e),r=this.buildAudioConstraints(t),i={video:Object.keys(n).length>0?n:!0,audio:r},a=await navigator.mediaDevices.getUserMedia(i),o=a.getVideoTracks()[0],s=a.getAudioTracks()[0];return this.validateTrack(o,`video`,a),this.validateTrack(s,`audio`,a),this.originalCameraStream=a,a}async createNewCameraStreamForRecording(){let e=this.getSelectedCameraDeviceId();return await this.createCameraStreamWithOriginalAudio(e)||this.createCameraStreamWithNewAudio(e)}async getCameraStream(){let e=this.streamManager.isRecording(),t=this.getSelectedCameraDeviceId(),n=this.getSelectedMicDeviceId();if(this.streamManager.setVideoDevice(t),this.streamManager.setAudioDevice(n),this.canReuseOriginalStream()){if(!this.originalCameraStream)throw Error(`Original camera stream is null`);return this.originalCameraStream}if(this.canReuseManagerStream()){let e=this.streamManager.getStream();if(!e)throw Error(`Manager stream is null`);return e}!e&&this.originalCameraStream&&(this.originalCameraStream=null);let r=this.streamManager.getStream();if(!e&&r&&r!==this.originalCameraStream&&(this.stopStreamTracks(r),this.streamManager.setMediaStream(null)),e)return this.streamManager.setVideoDevice(this.getSelectedCameraDeviceId()),this.streamManager.setAudioDevice(this.getSelectedMicDeviceId()),this.createNewCameraStreamForRecording();let i=await this.streamManager.startStream();return this.originalCameraStream=i,i}async switchToCamera(){let e=this.streamManager.isRecording();if(!(!e&&this.currentSourceType===`camera`))try{this.notifyTransitionStart(`Switching to camera...`),await this.handleScreenShareStop();let t=await this.getCameraStream();if(!t)throw Error(`Failed to get camera stream`);await this.applyCameraStream(t,e),this.notifyTransitionEnd()}catch(e){throw this.notifyTransitionEnd(),e}}notifyTransitionStart(e){this.callbacks.onTransitionStart&&this.callbacks.onTransitionStart(e)}notifyTransitionEnd(){this.callbacks.onTransitionEnd&&this.callbacks.onTransitionEnd()}stopScreenShareStreamTracks(e){let t=e.getVideoTracks(),n=e.getAudioTracks();y.debug(`[SourceSwitchManager] stopping screen share tracks`,{videoTracks:t.map(e=>({id:e.id,readyState:e.readyState,displaySurface:e.getSettings().displaySurface,constraints:e.getConstraints()})),audioTracks:n.map(e=>({id:e.id,readyState:e.readyState,constraints:e.getConstraints()}))});for(let e of t)e.stop();for(let e of n)e.stop()}stopDisplayTracks(e){if(e)for(let t of e.getVideoTracks())typeof t.getSettings().displaySurface==`string`&&t.readyState===se&&(y.debug(`[SourceSwitchManager] stopping display track`,{id:t.id,readyState:t.readyState,constraints:t.getConstraints(),settings:t.getSettings()}),t.stop())}async handleScreenShareStop(){if(this.currentSourceType!==`screen`)return;y.debug(`[SourceSwitchManager] handleScreenShareStop invoked`,{currentSourceType:this.currentSourceType,hasScreenShareStream:!!this.screenShareStream,screenShareStreamId:this.screenShareStream?.id,hasOriginalCameraStream:!!this.originalCameraStream,originalCameraStreamId:this.originalCameraStream?.id});let e=this.screenShareStream,t=this.streamManager.getStream();if(y.debug(`[SourceSwitchManager] Current stream state before stop`,{currentStreamId:t?.id,currentStreamVideoTracksCount:t?.getVideoTracks().length,currentStreamAudioTracksCount:t?.getAudioTracks().length,currentStreamAudioTrackId:t?.getAudioTracks()[0]?.id,currentStreamAudioTrackReadyState:t?.getAudioTracks()[0]?.readyState,originalCameraStreamAudioTrackId:this.originalCameraStream?.getAudioTracks()[0]?.id,originalCameraStreamAudioTrackReadyState:this.originalCameraStream?.getAudioTracks()[0]?.readyState}),e){let t=e.getAudioTracks();y.debug(`[SourceSwitchManager] Screen share stream audio tracks before stop`,{screenShareStreamId:e.id,screenShareAudioTracksCount:t.length,screenShareAudioTrackIds:t.map(e=>({id:e.id,readyState:e.readyState,enabled:e.enabled}))}),this.removeScreenShareTrackHandler(e),this.stopScreenShareStreamTracks(e),this.stopDisplayTracks(e),this.screenShareStream=null,await this.waitForTracksToEnd(0),y.debug(`[SourceSwitchManager] Screen share stream stopped`,{screenShareAudioTracksAfterStop:t.map(e=>({id:e.id,readyState:e.readyState}))})}if(t){let e=t.getAudioTracks();y.debug(`[SourceSwitchManager] Current stream audio tracks before video stop`,{currentStreamId:t.id,currentStreamAudioTracksCount:e.length,currentStreamAudioTrackIds:e.map(e=>({id:e.id,readyState:e.readyState,enabled:e.enabled}))}),this.stopStreamVideoTracks(t),this.stopDisplayTracks(t),y.debug(`[SourceSwitchManager] Current stream audio tracks after video stop`,{currentStreamId:t.id,currentStreamAudioTracksCount:t.getAudioTracks().length,currentStreamAudioTrackIds:t.getAudioTracks().map(e=>({id:e.id,readyState:e.readyState,enabled:e.enabled}))})}y.debug(`[SourceSwitchManager] Original camera stream state before source change`,{hasOriginalCameraStream:!!this.originalCameraStream,originalCameraStreamId:this.originalCameraStream?.id,originalCameraStreamAudioTracksCount:this.originalCameraStream?.getAudioTracks().length,originalCameraStreamAudioTrackId:this.originalCameraStream?.getAudioTracks()[0]?.id,originalCameraStreamAudioTrackReadyState:this.originalCameraStream?.getAudioTracks()[0]?.readyState,originalCameraStreamAudioTrackEnabled:this.originalCameraStream?.getAudioTracks()[0]?.enabled}),this.currentSourceType=`camera`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),y.debug(`[SourceSwitchManager] handleScreenShareStop completed`,{hasScreenShareStream:!!this.screenShareStream,currentSourceType:this.currentSourceType,hasOriginalCameraStream:!!this.originalCameraStream,originalCameraStreamId:this.originalCameraStream?.id,originalCameraStreamAudioTracksCount:this.originalCameraStream?.getAudioTracks().length,originalCameraStreamAudioTrackId:this.originalCameraStream?.getAudioTracks()[0]?.id,originalCameraStreamAudioTrackReadyState:this.originalCameraStream?.getAudioTracks()[0]?.readyState})}async applyCameraStream(e,t){this.streamManager.setMediaStream(e);let n=this.currentSourceType!==`camera`;this.currentSourceType=`camera`,n&&this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),t&&await this.streamManager.switchVideoSource(e),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(e)}async toggleSource(){if(this.streamManager.isRecording())try{this.currentSourceType===`camera`?await this.switchToScreen():await this.switchToCamera()}catch(e){this.handleToggleError(e)}}async switchToScreen(){let e=await this.switchToScreenCapture();if(!e){this.notifyTransitionEnd();return}this.notifyTransitionStart(`Switching to screen...`),await this.streamManager.switchVideoSource(e),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(e),this.notifyTransitionEnd()}handleToggleError(e){this.notifyTransitionEnd();let n=t(e);n.includes(`NotAllowedError`)||n.includes(`AbortError`)?this.currentSourceType===`screen`&&this.switchToCamera().catch(e=>{this.callbacks.onError&&this.callbacks.onError(this.createError(e))}):this.callbacks.onError&&this.callbacks.onError(this.createError(e))}async handleRecordingStop(){if(this.currentSourceType!==`screen`){this.cleanup();return}try{let e=this.streamManager.getStream();e&&(this.removeScreenShareTrackHandler(e),this.stopStreamVideoTracks(e));let t=await this.getCameraStream();if(!t)throw Error(`Failed to get camera stream`);this.streamManager.setMediaStream(t),this.currentSourceType=`camera`,this.callbacks.onSourceChange&&await this.callbacks.onSourceChange(this.currentSourceType),this.callbacks.onPreviewUpdate&&await this.callbacks.onPreviewUpdate(t)}catch(e){throw this.callbacks.onError&&this.callbacks.onError(this.createError(e)),e}this.cleanup()}cleanup(){this.screenShareStream&&=(this.removeScreenShareTrackHandler(this.screenShareStream),this.stopScreenShareStreamTracks(this.screenShareStream),null);let e=this.streamManager.getStream();e&&this.removeScreenShareTrackHandler(e),this.screenShareTrackEndHandler=null,this.originalCameraStream=null,this.originalCameraConstraints=null}setCallbacks(e){this.callbacks={...this.callbacks,...e}}};let le=Object.freeze({width:{ideal:o.width},height:{ideal:o.height},frameRate:{ideal:o.fps}}),ue=Object.freeze({video:le,audio:!0});Object.freeze({mimeType:`video/webm;codecs=vp9,opus`});var de=class{constructor(e={}){this.mediaStream=null,this.state=`idle`,this.eventListeners=new Map,this.selectedAudioDeviceId=null,this.selectedVideoDeviceId=null,this.streamConfig={...ue,...e}}getState(){return this.state}getStream(){return this.mediaStream}getAudioStreamForAnalysis(){return this.mediaStream&&this.mediaStream.getAudioTracks().length>0?this.mediaStream:null}isActive(){return this.state===`active`||this.state===`recording`}on(e,t){this.eventListeners.has(e)||this.eventListeners.set(e,new Set);let n=this.eventListeners.get(e);return n&&n.add(t),()=>{this.off(e,t)}}off(e,t){let n=this.eventListeners.get(e);n&&n.delete(t)}once(e,t){let n=(r=>{t(r),this.off(e,n)});return this.on(e,n)}emit(e,t){let n=this.eventListeners.get(e);if(n)for(let e of n)try{e(t)}catch{}}setState(e){if(this.state===e)return;let t=this.state;this.state=e,this.emit(`statechange`,{state:e,previousState:t})}setAudioDevice(e){this.selectedAudioDeviceId=e}setVideoDevice(e){this.selectedVideoDeviceId=e}getAudioDevice(){return this.selectedAudioDeviceId}getVideoDevice(){return this.selectedVideoDeviceId}async getAvailableDevices(){try{let e=await navigator.mediaDevices.enumerateDevices();return{audioinput:e.filter(e=>e.kind===`audioinput`),videoinput:e.filter(e=>e.kind===`videoinput`)}}catch(e){throw Error(`Failed to enumerate devices: ${t(e)}`)}}buildDeviceConstraints(e,t){return e?typeof t==`object`?{...t,deviceId:{exact:e}}:{deviceId:{exact:e}}:t}buildVideoConstraints(e){return this.buildDeviceConstraints(e,this.streamConfig.video)}buildAudioConstraints(e){return this.buildDeviceConstraints(e,this.streamConfig.audio)}async startStream(){if(y.debug(`[StreamManager] startStream called`,{hasExistingStream:!!this.mediaStream,selectedVideoDeviceId:this.selectedVideoDeviceId,selectedAudioDeviceId:this.selectedAudioDeviceId}),this.mediaStream){let e=this.mediaStream.getVideoTracks()[0];if(this.selectedVideoDeviceId===null)if(e?.getSettings?.()?.deviceId)y.debug(`[StreamManager] Stopping existing stream to recreate`),this.stopStream();else return y.debug(`[StreamManager] Reusing existing stream`),this.mediaStream;if(e?.getSettings&&e.getSettings().deviceId===this.selectedVideoDeviceId)return y.debug(`[StreamManager] Existing stream matches device, reusing`),this.mediaStream;y.debug(`[StreamManager] Device changed, stopping existing stream`),this.stopStream()}this.setState(`starting`),y.debug(`[StreamManager] State set to 'starting'`);try{let e={video:this.buildVideoConstraints(this.selectedVideoDeviceId),audio:this.buildAudioConstraints(this.selectedAudioDeviceId)};return y.debug(`[StreamManager] Requesting media stream with constraints`,{hasVideo:!!e.video,hasAudio:!!e.audio}),this.mediaStream=await navigator.mediaDevices.getUserMedia(e),y.info(`[StreamManager] Media stream obtained`,{streamId:this.mediaStream.id,videoTracks:this.mediaStream.getVideoTracks().length,audioTracks:this.mediaStream.getAudioTracks().length}),this.setState(`active`),y.debug(`[StreamManager] State set to 'active'`),y.debug(`[StreamManager] Emitting streamstart event`),this.emit(`streamstart`,{stream:this.mediaStream}),this.mediaStream}catch(e){let n=e instanceof Error?e:Error(t(e));throw y.error(`[StreamManager] Failed to start stream`,n),this.setState(`error`),this.emit(`error`,{error:n}),n}}stopStream(){if(this.mediaStream){for(let e of this.mediaStream.getTracks())e.stop();this.mediaStream=null}this.state!==`idle`&&(this.setState(`idle`),this.emit(`streamstop`,void 0))}stopStreamTracks(e){for(let t of e.getTracks())t.stop()}isTrackLive(e){return e!==void 0&&e.readyState===`live`}async tryReplaceTrack(e,t,n){let r=e.replaceTrack;if(typeof r!=`function`)return!1;try{await r.call(e,t),e.stop();for(let e of n.getTracks())e!==t&&e.stop();return t.stop(),!0}catch{return!1}}recreateStreamWithNewTrack(e,t,n){for(let t of n.getTracks())t!==e&&t.stop();let r=[e];this.isTrackLive(t)&&t&&r.push(t);let i=new MediaStream(r);if(this.mediaStream)for(let e of this.mediaStream.getTracks())e!==t&&e.stop();return i}async switchDeviceTrack(e,t,n){if(!this.mediaStream)throw Error(`No active stream to switch device`);let r=t===`video`?this.mediaStream.getVideoTracks()[0]:this.mediaStream.getAudioTracks()[0],i=t===`video`?this.mediaStream.getAudioTracks()[0]:this.mediaStream.getVideoTracks()[0];if(!r){let e=t===`video`?`video`:`audio`;throw Error(`No ${e} track in current stream`)}let a={[t]:this.buildDeviceConstraints(e,n)},o=await navigator.mediaDevices.getUserMedia(a),s=t===`video`?o.getVideoTracks()[0]:o.getAudioTracks()[0];if(!s){this.stopStreamTracks(o);let e=t===`video`?`video`:`audio`;throw Error(`Failed to get new ${e} track`)}return await this.tryReplaceTrack(r,s,o)||(r.stop(),this.mediaStream=this.recreateStreamWithNewTrack(s,i,o)),t===`video`?(this.selectedVideoDeviceId=e,this.emit(`videosourcechange`,{stream:this.mediaStream})):this.selectedAudioDeviceId=e,this.mediaStream}switchVideoDevice(e){return this.switchDeviceTrack(e,`video`,this.streamConfig.video)}switchAudioDevice(e){return this.switchDeviceTrack(e,`audio`,this.streamConfig.audio)}setMediaStream(e){this.mediaStream=e}setAudioTracksEnabled(e){if(!this.mediaStream)return;let t=this.mediaStream.getAudioTracks();for(let n of t)n.enabled=e}destroy(){this.stopStream(),this.eventListeners.clear(),this.setState(`idle`)}};let fe=[`Bytes`,`KB`,`MB`,`GB`],pe=1024;function me(e){if(e===0)return`0 Bytes`;let t=Math.floor(Math.log(e)/Math.log(pe));return`${Math.round(e/pe**t*100)/100} ${fe[t]}`}function he(e){let t=Math.floor(e/3600),n=Math.floor(e%3600/60),r=e%60;return t>0?`${t.toString().padStart(2,`0`)}:${n.toString().padStart(2,`0`)}:${r.toString().padStart(2,`0`)}`:`${n.toString().padStart(2,`0`)}:${r.toString().padStart(2,`0`)}`}var ge=class{constructor(){this.recordingStartTime=0,this.totalPausedTime=0,this.pauseStartTime=null,this.intervals=[],this.currentIntervalStart=null,this.isTracking=!1,this.visibilityChangeHandler=this.handleVisibilityChange.bind(this),this.blurHandler=this.handleBlur.bind(this),this.focusHandler=this.handleFocus.bind(this)}start(e){this.isTracking||(this.recordingStartTime=e,this.totalPausedTime=0,this.pauseStartTime=null,this.intervals=[],this.currentIntervalStart=null,this.isTracking=!0,typeof document<`u`&&document.addEventListener(`visibilitychange`,this.visibilityChangeHandler),typeof window<`u`&&(window.addEventListener(`blur`,this.blurHandler),window.addEventListener(`focus`,this.focusHandler)),this.checkInitialState())}pause(){!this.isTracking||this.pauseStartTime!==null||(this.pauseStartTime=Date.now(),this.endCurrentIntervalIfActive())}resume(){if(!this.isTracking||this.pauseStartTime===null)return;let e=Date.now()-this.pauseStartTime;this.totalPausedTime+=e,this.pauseStartTime=null}getIntervals(){return this.endCurrentIntervalIfActive(),this.intervals.map(e=>({start:this.normalizeTimestamp(e.start),end:this.normalizeTimestamp(e.end)}))}reset(){this.intervals=[],this.currentIntervalStart=null,this.totalPausedTime=0,this.pauseStartTime=null}cleanup(){this.isTracking=!1,this.endCurrentIntervalIfActive(),typeof document<`u`&&document.removeEventListener(`visibilitychange`,this.visibilityChangeHandler),typeof window<`u`&&(window.removeEventListener(`blur`,this.blurHandler),window.removeEventListener(`focus`,this.focusHandler)),this.reset()}checkInitialState(){typeof document>`u`||document.visibilityState===`hidden`&&this.startInterval()}handleVisibilityChange(){typeof document>`u`||(document.visibilityState===`hidden`?this.startInterval():this.endCurrentIntervalIfActive())}handleBlur(){this.startInterval()}handleFocus(){this.endCurrentIntervalIfActive()}startInterval(){this.currentIntervalStart!==null||!this.isTracking||this.pauseStartTime===null&&(this.currentIntervalStart=Date.now())}endCurrentIntervalIfActive(){if(this.currentIntervalStart===null)return;let e=Date.now(),t=this.currentIntervalStart;e>t&&this.intervals.push({start:t,end:e}),this.currentIntervalStart=null}normalizeTimestamp(e){let t=(e-this.recordingStartTime-this.totalPausedTime)/1e3;return Math.max(0,t)}};function _e(e,t){if(e==null)throw Error(t);return e}function ve(e,t=`StreamProcessor`){if(!e)throw Error(`${t} is required`);return e}let ye=1e3;var be=class{constructor(e){this.recordingStartTime=0,this.recordingTimer=null,this.pauseStartTime=null,this.totalPausedTime=0,this.streamProcessor=null,this.bufferSizeUpdateInterval=null,this.tabVisibilityTracker=null,this.visibilityChangeHandler=null,this.blurHandler=null,this.focusHandler=null,this.streamManager=e}isRecording(){return this.streamManager.getState()===`recording`}getStreamProcessor(){return this.streamProcessor}getAudioStreamForAnalysis(){if(this.streamProcessor){let e=this.streamProcessor.getAudioStreamForAnalysis();if(e)return e}return this.streamManager.getAudioStreamForAnalysis()}async startRecording(e,t,n,r){let i=this.streamManager.getStream();if(y.debug(`[StreamRecordingState] startRecording called`,{hasMediaStream:!!i,isRecording:this.isRecording(),hasProcessor:!!e,audioTracks:i?.getAudioTracks().length||0}),!i)throw Error(`Stream must be started before recording`);if(this.isRecording())return y.debug(`[StreamRecordingState] Already recording, returning`),Promise.resolve();this.streamProcessor=e,y.debug(`[StreamRecordingState] StreamProcessor assigned, setting callbacks`),e.setOnMuteStateChange(e=>{this.streamManager.emit(`audiomutetoggle`,{muted:e})}),e.setOnSourceChange(e=>{this.streamManager.emit(`videosourcechange`,{stream:e})}),this.bufferSizeUpdateInterval=window.setInterval(()=>{if(!this.streamProcessor)return;let e=this.streamProcessor.getBufferSize(),t=me(e);this.streamManager.emit(`recordingbufferupdate`,{size:e,formatted:t})},ye),this.resetRecordingState();let a=n&&r?{enabled:!0,text:r,recordingStartTime:this.recordingStartTime}:void 0;y.debug(`[StreamRecordingState] Overlay config`,{enableTabVisibilityOverlay:n,hasOverlayText:!!r,overlayText:r,overlayConfig:a}),y.debug(`[StreamRecordingState] Starting processing`),await e.startProcessing(i,t,a),y.info(`[StreamRecordingState] Processing started and worker ready`),n&&(y.debug(`[StreamRecordingState] Setting up tab visibility tracking`,{recordingStartTime:this.recordingStartTime}),this.tabVisibilityTracker=new ge,this.tabVisibilityTracker.start(this.recordingStartTime),this.setupVisibilityUpdates(e)),this.streamManager.setState(`recording`),this.streamManager.emit(`recordingstart`,{recorder:null}),this.startRecordingTimer()}async stopRecording(){if(y.debug(`[StreamRecordingState] stopRecording called`,{hasStreamProcessor:!!this.streamProcessor,isRecording:this.isRecording()}),!(this.streamProcessor&&this.isRecording()))throw Error(`Not currently recording`);this.streamManager.setState(`stopping`),this.clearRecordingTimer(),this.clearBufferSizeInterval(),this.resetPauseState(),this.cleanupVisibilityUpdates();let e=[];this.tabVisibilityTracker?(e=this.tabVisibilityTracker.getIntervals(),y.debug(`[StreamRecordingState] Tab visibility intervals collected`,{intervalsCount:e.length,intervals:e}),this.tabVisibilityTracker.cleanup(),this.tabVisibilityTracker=null):y.debug(`[StreamRecordingState] No tab visibility tracker was active`),y.debug(`[StreamRecordingState] Finalizing stream processor`);let t=await this.streamProcessor.finalize();return y.info(`[StreamRecordingState] Stream processor finalized`,{blobSize:t.blob.size,hasBlob:!!t.blob}),this.streamManager.setState(`active`),this.streamManager.emit(`recordingstop`,{blob:t.blob,mimeType:`video/mp4`}),this.streamProcessor=null,y.debug(`[StreamRecordingState] StreamProcessor cleared`),{blob:t.blob,tabVisibilityIntervals:e}}pauseRecording(){this.clearRecordingTimer(),this.pauseStartTime===null&&(this.pauseStartTime=Date.now()),this.tabVisibilityTracker&&this.tabVisibilityTracker.pause(),this.streamProcessor&&this.isRecording()&&this.streamProcessor.pause()}resumeRecording(){if(this.pauseStartTime!==null){let e=Date.now()-this.pauseStartTime;this.totalPausedTime+=e,this.pauseStartTime=null}this.tabVisibilityTracker&&this.tabVisibilityTracker.resume(),this.startRecordingTimer(),this.streamProcessor&&this.isRecording()&&this.streamProcessor.resume()}toggleMute(){ve(this.streamProcessor,`StreamProcessor`).toggleMute()}muteAudio(){this.streamProcessor?(this.streamProcessor.isMutedState()||this.streamProcessor.toggleMute(),this.streamManager.setAudioTracksEnabled(!1)):this.streamManager.getStream()&&(this.streamManager.setAudioTracksEnabled(!1),this.streamManager.emit(`audiomutetoggle`,{muted:!0}))}unmuteAudio(){this.streamProcessor?(this.streamProcessor.isMutedState()&&this.streamProcessor.toggleMute(),this.streamManager.setAudioTracksEnabled(!0)):this.streamManager.getStream()&&(this.streamManager.setAudioTracksEnabled(!0),this.streamManager.emit(`audiomutetoggle`,{muted:!1}))}isMuted(){if(this.streamProcessor)return this.streamProcessor.isMutedState();let e=this.streamManager.getStream();if(e){let t=e.getAudioTracks();return t.length>0&&t.every(e=>!e.enabled)}return!1}async switchVideoSource(e){await ve(this.streamProcessor,`StreamProcessor`).switchVideoSource(e)}getCurrentVideoSource(){return _e(ve(this.streamProcessor,`StreamProcessor`).getCurrentVideoSource(),`Current video source is not available`)}formatTimeElapsed(e){let t=Math.floor(e/60),n=Math.floor(e%60);return`${t.toString().padStart(2,`0`)}:${n.toString().padStart(2,`0`)}`}startRecordingTimer(){this.recordingTimer=window.setInterval(()=>{let e=(Date.now()-this.recordingStartTime-this.totalPausedTime)/1e3,t=this.formatTimeElapsed(e);this.streamManager.emit(`recordingtimeupdate`,{elapsed:e,formatted:t})},ye)}clearRecordingTimer(){this.recordingTimer!==null&&(clearInterval(this.recordingTimer),this.recordingTimer=null)}clearBufferSizeInterval(){this.bufferSizeUpdateInterval!==null&&(clearInterval(this.bufferSizeUpdateInterval),this.bufferSizeUpdateInterval=null)}resetRecordingState(){this.recordingStartTime=performance.now(),this.totalPausedTime=0,this.pauseStartTime=null}resetPauseState(){this.totalPausedTime=0,this.pauseStartTime=null}setupVisibilityUpdates(e){if(typeof document>`u`||typeof window>`u`){y.warn(`[StreamRecordingState] Cannot setup visibility updates - document/window not available`);return}if(this.visibilityChangeHandler=()=>{if(typeof document>`u`)return;let t=document.visibilityState===`hidden`,n=performance.now();y.debug(`[StreamRecordingState] Visibility change`,{isHidden:t,timestamp:n,visibilityState:document.visibilityState}),e.updateTabVisibility(t,n)},this.blurHandler=()=>{let t=performance.now();y.debug(`[StreamRecordingState] Window blur`,{timestamp:t}),e.updateTabVisibility(!0,t)},this.focusHandler=()=>{let t=performance.now();y.debug(`[StreamRecordingState] Window focus`,{timestamp:t}),e.updateTabVisibility(!1,t)},document.addEventListener(`visibilitychange`,this.visibilityChangeHandler),window.addEventListener(`blur`,this.blurHandler),window.addEventListener(`focus`,this.focusHandler),document.visibilityState===`hidden`){let t=performance.now();y.debug(`[StreamRecordingState] Initial state is hidden`,{timestamp:t}),e.updateTabVisibility(!0,t)}else y.debug(`[StreamRecordingState] Initial state is visible`)}cleanupVisibilityUpdates(){this.visibilityChangeHandler&&typeof document<`u`&&(document.removeEventListener(`visibilitychange`,this.visibilityChangeHandler),this.visibilityChangeHandler=null),this.blurHandler&&typeof window<`u`&&(window.removeEventListener(`blur`,this.blurHandler),this.blurHandler=null),this.focusHandler&&typeof window<`u`&&(window.removeEventListener(`focus`,this.focusHandler),this.focusHandler=null)}destroy(){this.streamProcessor&&=(this.streamProcessor.cancel().catch(()=>{}),null),this.cleanupVisibilityUpdates(),this.tabVisibilityTracker&&=(this.tabVisibilityTracker.cleanup(),null),this.clearRecordingTimer(),this.clearBufferSizeInterval()}},xe=class{constructor(e={}){this.streamManager=new de(e),this.recordingState=new be(this.streamManager)}getState(){return this.streamManager.getState()}getStream(){return this.streamManager.getStream()}getAudioStreamForAnalysis(){return this.recordingState.getAudioStreamForAnalysis()}isRecording(){return this.recordingState.isRecording()}isActive(){return this.streamManager.isActive()}on(e,t){return this.streamManager.on(e,t)}off(e,t){this.streamManager.off(e,t)}once(e,t){return this.streamManager.once(e,t)}setAudioDevice(e){this.streamManager.setAudioDevice(e)}setVideoDevice(e){this.streamManager.setVideoDevice(e)}getAudioDevice(){return this.streamManager.getAudioDevice()}getVideoDevice(){return this.streamManager.getVideoDevice()}async getAvailableDevices(){return await this.streamManager.getAvailableDevices()}async startStream(){return await this.streamManager.startStream()}stopStream(){this.streamManager.stopStream()}switchVideoDevice(e){return this.streamManager.switchVideoDevice(e)}switchAudioDevice(e){return this.streamManager.switchAudioDevice(e)}async startRecording(e,t,n,r){return await this.recordingState.startRecording(e,t,n,r)}async stopRecording(){return await this.recordingState.stopRecording()}pauseRecording(){this.recordingState.pauseRecording()}resumeRecording(){this.recordingState.resumeRecording()}toggleMute(){this.recordingState.toggleMute()}muteAudio(){this.recordingState.muteAudio()}unmuteAudio(){this.recordingState.unmuteAudio()}isMuted(){return this.recordingState.isMuted()}async switchVideoSource(e){return await this.recordingState.switchVideoSource(e)}setMediaStream(e){this.streamManager.setMediaStream(e)}getCurrentVideoSource(){return this.recordingState.getCurrentVideoSource()}destroy(){this.recordingState.destroy(),this.streamManager.destroy()}},Se=class{constructor(e,n){this.isProcessing=!1,this.retryTimeoutId=null,this.callbacks={},this.storageService=e,this.uploadService=n,this.networkOnlineHandler=()=>{this.processQueue().catch(e=>{let n=t(e);this.callbacks.onUploadError?.(`network-recovery`,Error(n))})},window.addEventListener(`online`,this.networkOnlineHandler),this.processingIntervalId=window.setInterval(()=>{this.processQueue().catch(e=>{let n=t(e);this.callbacks.onUploadError?.(`processing-loop`,Error(n))})},5e3)}destroy(){this.clearTimer(this.processingIntervalId,clearInterval),this.clearTimer(this.retryTimeoutId,clearTimeout),window.removeEventListener(`online`,this.networkOnlineHandler)}setCallbacks(e){this.callbacks=e}async queueUpload(e){let t=await this.storageService.savePendingUpload(e);return this.processQueue(),t}async processQueue(){if(!this.isProcessing){this.isProcessing=!0;try{if(!this.storageService.isInitialized())throw Error(`Database not initialized`);let e=await this.storageService.getPendingUploads(`pending`);if(e.length>0){let t=this.getOldestUpload(e);await this.processUpload(t),this.isProcessing=!1;return}let t=(await this.storageService.getPendingUploads(`failed`)).filter(e=>e.retryCount<10);if(t.length>0){let e=this.getOldestFailedUpload(t),n=this.calculateRetryDelay(e.retryCount),r=Date.now()-e.updatedAt;if(r>=n)await this.storageService.updateUploadStatus(e.id,{status:`pending`,retryCount:e.retryCount}),await this.processUpload(e);else{let e=n-r;this.scheduleRetry(e)}}this.isProcessing=!1}catch(e){throw this.isProcessing=!1,Error(`Error processing upload queue: ${t(e)}`)}}}getPendingUploads(){return this.storageService.getPendingUploads()}async getStats(){let e=await this.storageService.getPendingUploads(),t={pending:0,uploading:0,failed:0,total:e.length};for(let n of e)n.status===`pending`?t.pending+=1:n.status===`uploading`?t.uploading+=1:n.status===`failed`&&(t.failed+=1);return t}getOldestUpload(e){if(e.length===0)throw Error(`Cannot get oldest upload from empty array`);return e.sort((e,t)=>e.createdAt-t.createdAt)[0]}getOldestFailedUpload(e){if(e.length===0)throw Error(`Cannot get oldest failed upload from empty array`);return e.sort((e,t)=>e.updatedAt-t.updatedAt)[0]}async processUpload(e){try{await this.storageService.updateUploadStatus(e.id,{status:`uploading`});let t=await this.uploadService.uploadVideo(e.blob,{apiKey:e.apiKey,backendUrl:e.backendUrl,filename:e.filename,duration:e.duration,metadata:e.metadata,userMetadata:e.userMetadata,onProgress:t=>{this.callbacks.onUploadProgress?.(e.id,t)}});await this.storageService.deleteUpload(e.id),this.callbacks.onUploadComplete?.(e.id,t)}catch(n){let r=t(n),i=e.retryCount+1;if(await this.storageService.updateUploadStatus(e.id,{status:`failed`,retryCount:i,lastError:r}),i>=10)this.callbacks.onUploadError?.(e.id,Error(`Upload failed after 10 attempts: ${r}`));else{let e=this.calculateRetryDelay(i);this.scheduleRetry(e)}}}calculateRetryDelay(e){let t=2e3*1.5**(e-1);return Math.min(t,3e5)}scheduleRetry(e){this.clearTimer(this.retryTimeoutId,clearTimeout),this.retryTimeoutId=window.setTimeout(()=>{this.retryTimeoutId=null,this.processQueue()},e)}clearTimer(e,t){e!==null&&t(e)}},Ce=class{async uploadVideo(e,t){if(!t.filename)throw Error(`Filename is required`);if(!e.type||e.type.trim()===``)throw Error(`Blob type is required`);let n=await this.initVideoUpload({apiKey:t.apiKey,backendUrl:t.backendUrl,filename:t.filename,fileSize:e.size,mimeType:e.type,metadata:t.metadata,userMetadata:t.userMetadata});return this.uploadVideoFile(e,n.uploadUrl,{apiKey:t.apiKey,duration:t.duration,onProgress:t.onProgress})}async initVideoUpload(e){let t=`${e.backendUrl}/api/v1/videos/init`,n={filename:e.filename,fileSize:e.fileSize,mimeType:e.mimeType,preProcessed:!0};e.metadata&&(n.metadata=e.metadata),e.userMetadata&&(n.userMetadata=e.userMetadata);let r=await fetch(t,{method:`POST`,headers:{Authorization:`Bearer ${e.apiKey}`,"Content-Type":`application/json`},body:JSON.stringify(n)});if(!r.ok){let e=await this.extractErrorFromResponse(r,`Failed to initialize video upload`);throw Error(e)}return await r.json()}async extractErrorFromResponse(e,t){let n=await this.parseJsonResponse(e);return n&&typeof n==`object`&&`error`in n&&typeof n.error==`string`?n.error:`${t}: ${e.status} ${e.statusText}`}async parseJsonResponse(e){let t=await e.json();return typeof t==`object`&&t?t:null}uploadVideoFile(e,t,n){return new Promise((r,i)=>{let a=new XMLHttpRequest;if(n.onProgress){let e=n.onProgress;a.upload.addEventListener(`progress`,t=>{t.lengthComputable&&e(t.loaded/t.total)})}a.addEventListener(`load`,()=>{if(a.status>=200&&a.status<=299){this.parseSuccessResponse(a,r,i);return}this.parseErrorResponse(a,i)}),a.addEventListener(`error`,()=>{i(Error(`Network error during upload`))}),a.addEventListener(`abort`,()=>{i(Error(`Upload was aborted`))}),a.open(`PUT`,t),a.setRequestHeader(`Authorization`,`Bearer ${n.apiKey}`),a.setRequestHeader(`Content-Type`,e.type),n.duration!==void 0&&a.setRequestHeader(`X-Video-Duration`,n.duration.toString()),a.send(e)})}parseSuccessResponse(e,t,n){let r=this.safeParseJsonFromXhr(e);if(!r){n(Error(`Failed to parse upload response: invalid JSON`));return}t(r)}parseErrorResponse(e,t){let n=this.safeParseJsonFromXhr(e);if(n&&typeof n==`object`&&`error`in n&&typeof n.error==`string`){t(Error(n.error));return}t(Error(`Upload failed: ${e.status} ${e.statusText}`))}safeParseJsonFromXhr(e){if(!e.responseText||e.responseText.trim()===``)return null;let t=e.responseText.trim();if(!(t.startsWith(`{`)&&t.endsWith(`}`))||t.length<2)return null;let n=JSON.parse(e.responseText);return typeof n==`object`&&n?n:null}};
|
|
2
2
|
/*!
|
|
3
3
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
4
4
|
*
|
|
@@ -6,7 +6,7 @@ var VidtreoRecorder=(function(e){function t(e){return e instanceof Error?e.messa
|
|
|
6
6
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
7
7
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
8
8
|
*/
|
|
9
|
-
function
|
|
9
|
+
function b(e){if(!e)throw Error(`Assertion failed.`)}let we=e=>{let t=(e%360+360)%360;if(t===0||t===90||t===180||t===270)return t;throw Error(`Invalid rotation ${e}.`)},x=e=>e&&e[e.length-1],Te=e=>e>=0&&e<2**32;var S=class e{constructor(e){this.bytes=e,this.pos=0}seekToByte(e){this.pos=8*e}readBit(){let e=Math.floor(this.pos/8),t=this.bytes[e]??0,n=7-(this.pos&7),r=(t&1<<n)>>n;return this.pos++,r}readBits(e){if(e===1)return this.readBit();let t=0;for(let n=0;n<e;n++)t<<=1,t|=this.readBit();return t}writeBits(e,t){let n=this.pos+e;for(let e=this.pos;e<n;e++){let r=Math.floor(e/8),i=this.bytes[r],a=7-(e&7);i&=~(1<<a),i|=(t&1<<n-e-1)>>n-e-1<<a,this.bytes[r]=i}this.pos=n}readAlignedByte(){if(this.pos%8!=0)throw Error(`Bitstream is not byte-aligned.`);let e=this.pos/8,t=this.bytes[e]??0;return this.pos+=8,t}skipBits(e){this.pos+=e}getBitsLeft(){return this.bytes.length*8-this.pos}clone(){let t=new e(this.bytes);return t.pos=this.pos,t}};let C=e=>{let t=0;for(;e.readBits(1)===0&&t<32;)t++;if(t>=32)throw Error(`Invalid exponential-Golomb code.`);return(1<<t)-1+e.readBits(t)},Ee=e=>{let t=C(e);return t&1?t+1>>1:-(t>>1)},w=e=>e.constructor===Uint8Array?e:ArrayBuffer.isView(e)?new Uint8Array(e.buffer,e.byteOffset,e.byteLength):new Uint8Array(e),T=e=>e.constructor===DataView?e:ArrayBuffer.isView(e)?new DataView(e.buffer,e.byteOffset,e.byteLength):new DataView(e),De=new TextDecoder,E=new TextEncoder,Oe=e=>Object.fromEntries(Object.entries(e).map(([e,t])=>[t,e])),ke={bt709:1,bt470bg:5,smpte170m:6,bt2020:9,smpte432:12},Ae=Oe(ke),je={bt709:1,smpte170m:6,linear:8,"iec61966-2-1":13,pq:16,hlg:18},Me=Oe(je),Ne={rgb:0,bt709:1,bt470bg:5,smpte170m:6,"bt2020-ncl":9},Pe=Oe(Ne),Fe=e=>!!e&&!!e.primaries&&!!e.transfer&&!!e.matrix&&e.fullRange!==void 0,Ie=e=>e instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&e instanceof SharedArrayBuffer||ArrayBuffer.isView(e);var Le=class{constructor(){this.currentPromise=Promise.resolve()}async acquire(){let e,t=new Promise(t=>{e=t}),n=this.currentPromise;return this.currentPromise=t,await n,e}};let Re=e=>[...e].map(e=>e.toString(16).padStart(2,`0`)).join(``),ze=e=>(e=e>>1&1431655765|(e&1431655765)<<1,e=e>>2&858993459|(e&858993459)<<2,e=e>>4&252645135|(e&252645135)<<4,e=e>>8&16711935|(e&16711935)<<8,e=e>>16&65535|(e&65535)<<16,e>>>0),Be=(e,t,n)=>{let r=0,i=e.length-1,a=-1;for(;r<=i;){let o=r+i>>1,s=n(e[o]);s===t?(a=o,i=o-1):s<t?r=o+1:i=o-1}return a},D=(e,t,n)=>{let r=0,i=e.length-1,a=-1;for(;r<=i;){let o=r+(i-r+1)/2|0;n(e[o])<=t?(a=o,r=o+1):i=o-1}return a},Ve=(e,t,n)=>{let r=D(e,n(t),n);e.splice(r+1,0,t)},O=()=>{let e,t;return{promise:new Promise((n,r)=>{e=n,t=r}),resolve:e,reject:t}},He=(e,t)=>{for(let n=e.length-1;n>=0;n--)if(t(e[n]))return n;return-1},Ue=async function*(e){Symbol.iterator in e?yield*e[Symbol.iterator]():yield*e[Symbol.asyncIterator]()},We=e=>{if(!(Symbol.iterator in e)&&!(Symbol.asyncIterator in e))throw TypeError(`Argument must be an iterable or async iterable.`)},Ge=e=>{throw Error(`Unexpected value: ${e}`)},Ke=(e,t,n)=>{let r=e.getUint8(t),i=e.getUint8(t+1),a=e.getUint8(t+2);return n?r|i<<8|a<<16:r<<16|i<<8|a},qe=(e,t,n)=>Ke(e,t,n)<<8>>8,Je=(e,t,n,r)=>{n>>>=0,n&=16777215,r?(e.setUint8(t,n&255),e.setUint8(t+1,n>>>8&255),e.setUint8(t+2,n>>>16&255)):(e.setUint8(t,n>>>16&255),e.setUint8(t+1,n>>>8&255),e.setUint8(t+2,n&255))},Ye=(e,t,n,r)=>{n=k(n,-8388608,8388607),n<0&&(n=n+16777216&16777215),Je(e,t,n,r)},Xe=(e,t)=>({async next(){let n=await e.next();return n.done?{value:void 0,done:!0}:{value:t(n.value),done:!1}},return(){return e.return()},throw(t){return e.throw(t)},[Symbol.asyncIterator](){return this}}),k=(e,t,n)=>Math.max(t,Math.min(n,e)),Ze=e=>{let t=Math.round(e);return Math.abs(e/t-1)<10*2**-52?t:e},Qe=(e,t)=>Math.round(e/t)*t,$e=/^[a-z]{3}$/,et=e=>$e.test(e),tt=1e6*(1+2**-52),nt=(e,t)=>{let n=e<0?-1:1;e=Math.abs(e);let r=0,i=1,a=1,o=0,s=e;for(;;){let e=Math.floor(s),c=e*a+r,l=e*o+i;if(l>t)return{numerator:n*a,denominator:o};if(r=a,i=o,a=c,o=l,s=1/(s-e),!isFinite(s))break}return{numerator:n*a,denominator:o}};var rt=class{constructor(){this.currentPromise=Promise.resolve()}call(e){return this.currentPromise=this.currentPromise.then(e)}};let it=null,at=()=>it===null?it=!!(typeof navigator<`u`&&(navigator.vendor?.match(/apple/i)||/AppleWebKit/.test(navigator.userAgent)&&!/Chrome/.test(navigator.userAgent)||/\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent))):it,ot=null,st=()=>ot===null?ot=typeof navigator<`u`&&navigator.userAgent?.includes(`Firefox`):ot,ct=null,lt=()=>ct===null?ct=!!(typeof navigator<`u`&&(navigator.vendor?.includes(`Google Inc`)||/Chrome/.test(navigator.userAgent))):ct,ut=null,dt=()=>{if(ut!==null)return ut;if(typeof navigator>`u`)return null;let e=/\bChrome\/(\d+)/.exec(navigator.userAgent);return e?ut=Number(e[1]):null},ft=(e,t,n,r)=>e<=r&&n<=t,pt=function*(e){for(let t in e){let n=e[t];n!==void 0&&(yield{key:t,value:n})}},mt=()=>{Symbol.dispose??=Symbol(`Symbol.dispose`)},ht=e=>typeof e==`number`&&!Number.isNaN(e);
|
|
10
10
|
/*!
|
|
11
11
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
12
12
|
*
|
|
@@ -14,7 +14,7 @@ function x(e){if(!e)throw Error(`Assertion failed.`)}let Se=e=>{let t=(e%360+360
|
|
|
14
14
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
15
15
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
16
16
|
*/
|
|
17
|
-
var mt=class{constructor(e,t){if(this.data=e,this.mimeType=t,!(e instanceof Uint8Array))throw TypeError(`data must be a Uint8Array.`);if(typeof t!=`string`)throw TypeError(`mimeType must be a string.`)}},ht=class{constructor(e,t,n,r){if(this.data=e,this.mimeType=t,this.name=n,this.description=r,!(e instanceof Uint8Array))throw TypeError(`data must be a Uint8Array.`);if(t!==void 0&&typeof t!=`string`)throw TypeError(`mimeType, when provided, must be a string.`);if(n!==void 0&&typeof n!=`string`)throw TypeError(`name, when provided, must be a string.`);if(r!==void 0&&typeof r!=`string`)throw TypeError(`description, when provided, must be a string.`)}};let gt=e=>{if(!e||typeof e!=`object`)throw TypeError(`tags must be an object.`);if(e.title!==void 0&&typeof e.title!=`string`)throw TypeError(`tags.title, when provided, must be a string.`);if(e.description!==void 0&&typeof e.description!=`string`)throw TypeError(`tags.description, when provided, must be a string.`);if(e.artist!==void 0&&typeof e.artist!=`string`)throw TypeError(`tags.artist, when provided, must be a string.`);if(e.album!==void 0&&typeof e.album!=`string`)throw TypeError(`tags.album, when provided, must be a string.`);if(e.albumArtist!==void 0&&typeof e.albumArtist!=`string`)throw TypeError(`tags.albumArtist, when provided, must be a string.`);if(e.trackNumber!==void 0&&(!Number.isInteger(e.trackNumber)||e.trackNumber<=0))throw TypeError(`tags.trackNumber, when provided, must be a positive integer.`);if(e.tracksTotal!==void 0&&(!Number.isInteger(e.tracksTotal)||e.tracksTotal<=0))throw TypeError(`tags.tracksTotal, when provided, must be a positive integer.`);if(e.discNumber!==void 0&&(!Number.isInteger(e.discNumber)||e.discNumber<=0))throw TypeError(`tags.discNumber, when provided, must be a positive integer.`);if(e.discsTotal!==void 0&&(!Number.isInteger(e.discsTotal)||e.discsTotal<=0))throw TypeError(`tags.discsTotal, when provided, must be a positive integer.`);if(e.genre!==void 0&&typeof e.genre!=`string`)throw TypeError(`tags.genre, when provided, must be a string.`);if(e.date!==void 0&&(!(e.date instanceof Date)||Number.isNaN(e.date.getTime())))throw TypeError(`tags.date, when provided, must be a valid Date.`);if(e.lyrics!==void 0&&typeof e.lyrics!=`string`)throw TypeError(`tags.lyrics, when provided, must be a string.`);if(e.images!==void 0){if(!Array.isArray(e.images))throw TypeError(`tags.images, when provided, must be an array.`);for(let t of e.images){if(!t||typeof t!=`object`)throw TypeError(`Each image in tags.images must be an object.`);if(!(t.data instanceof Uint8Array))throw TypeError(`Each image.data must be a Uint8Array.`);if(typeof t.mimeType!=`string`)throw TypeError(`Each image.mimeType must be a string.`);if(![`coverFront`,`coverBack`,`unknown`].includes(t.kind))throw TypeError(`Each image.kind must be 'coverFront', 'coverBack', or 'unknown'.`)}}if(e.comment!==void 0&&typeof e.comment!=`string`)throw TypeError(`tags.comment, when provided, must be a string.`);if(e.raw!==void 0){if(!e.raw||typeof e.raw!=`object`)throw TypeError(`tags.raw, when provided, must be an object.`);for(let t of Object.values(e.raw))if(t!==null&&typeof t!=`string`&&!(t instanceof Uint8Array)&&!(t instanceof mt)&&!(t instanceof ht))throw TypeError(`Each value in tags.raw must be a string, Uint8Array, RichImageData, AttachedFile, or null.`)}},_t={default:!0,forced:!1,original:!1,commentary:!1,hearingImpaired:!1,visuallyImpaired:!1},vt=e=>{if(!e||typeof e!=`object`)throw TypeError(`disposition must be an object.`);if(e.default!==void 0&&typeof e.default!=`boolean`)throw TypeError(`disposition.default must be a boolean.`);if(e.forced!==void 0&&typeof e.forced!=`boolean`)throw TypeError(`disposition.forced must be a boolean.`);if(e.original!==void 0&&typeof e.original!=`boolean`)throw TypeError(`disposition.original must be a boolean.`);if(e.commentary!==void 0&&typeof e.commentary!=`boolean`)throw TypeError(`disposition.commentary must be a boolean.`);if(e.hearingImpaired!==void 0&&typeof e.hearingImpaired!=`boolean`)throw TypeError(`disposition.hearingImpaired must be a boolean.`);if(e.visuallyImpaired!==void 0&&typeof e.visuallyImpaired!=`boolean`)throw TypeError(`disposition.visuallyImpaired must be a boolean.`)},j=[`avc`,`hevc`,`vp9`,`av1`,`vp8`],M=[`pcm-s16`,`pcm-s16be`,`pcm-s24`,`pcm-s24be`,`pcm-s32`,`pcm-s32be`,`pcm-f32`,`pcm-f32be`,`pcm-f64`,`pcm-f64be`,`pcm-u8`,`pcm-s8`,`ulaw`,`alaw`],yt=[`aac`,`opus`,`mp3`,`vorbis`,`flac`],N=[...yt,...M],bt=[`webvtt`],xt=[{maxMacroblocks:99,maxBitrate:64e3,level:10},{maxMacroblocks:396,maxBitrate:192e3,level:11},{maxMacroblocks:396,maxBitrate:384e3,level:12},{maxMacroblocks:396,maxBitrate:768e3,level:13},{maxMacroblocks:396,maxBitrate:2e6,level:20},{maxMacroblocks:792,maxBitrate:4e6,level:21},{maxMacroblocks:1620,maxBitrate:4e6,level:22},{maxMacroblocks:1620,maxBitrate:1e7,level:30},{maxMacroblocks:3600,maxBitrate:14e6,level:31},{maxMacroblocks:5120,maxBitrate:2e7,level:32},{maxMacroblocks:8192,maxBitrate:2e7,level:40},{maxMacroblocks:8192,maxBitrate:5e7,level:41},{maxMacroblocks:8704,maxBitrate:5e7,level:42},{maxMacroblocks:22080,maxBitrate:135e6,level:50},{maxMacroblocks:36864,maxBitrate:24e7,level:51},{maxMacroblocks:36864,maxBitrate:24e7,level:52},{maxMacroblocks:139264,maxBitrate:24e7,level:60},{maxMacroblocks:139264,maxBitrate:48e7,level:61},{maxMacroblocks:139264,maxBitrate:8e8,level:62}],St=[{maxPictureSize:36864,maxBitrate:128e3,tier:`L`,level:30},{maxPictureSize:122880,maxBitrate:15e5,tier:`L`,level:60},{maxPictureSize:245760,maxBitrate:3e6,tier:`L`,level:63},{maxPictureSize:552960,maxBitrate:6e6,tier:`L`,level:90},{maxPictureSize:983040,maxBitrate:1e7,tier:`L`,level:93},{maxPictureSize:2228224,maxBitrate:12e6,tier:`L`,level:120},{maxPictureSize:2228224,maxBitrate:3e7,tier:`H`,level:120},{maxPictureSize:2228224,maxBitrate:2e7,tier:`L`,level:123},{maxPictureSize:2228224,maxBitrate:5e7,tier:`H`,level:123},{maxPictureSize:8912896,maxBitrate:25e6,tier:`L`,level:150},{maxPictureSize:8912896,maxBitrate:1e8,tier:`H`,level:150},{maxPictureSize:8912896,maxBitrate:4e7,tier:`L`,level:153},{maxPictureSize:8912896,maxBitrate:16e7,tier:`H`,level:153},{maxPictureSize:8912896,maxBitrate:6e7,tier:`L`,level:156},{maxPictureSize:8912896,maxBitrate:24e7,tier:`H`,level:156},{maxPictureSize:35651584,maxBitrate:6e7,tier:`L`,level:180},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:180},{maxPictureSize:35651584,maxBitrate:12e7,tier:`L`,level:183},{maxPictureSize:35651584,maxBitrate:48e7,tier:`H`,level:183},{maxPictureSize:35651584,maxBitrate:24e7,tier:`L`,level:186},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:186}],Ct=[{maxPictureSize:36864,maxBitrate:2e5,level:10},{maxPictureSize:73728,maxBitrate:8e5,level:11},{maxPictureSize:122880,maxBitrate:18e5,level:20},{maxPictureSize:245760,maxBitrate:36e5,level:21},{maxPictureSize:552960,maxBitrate:72e5,level:30},{maxPictureSize:983040,maxBitrate:12e6,level:31},{maxPictureSize:2228224,maxBitrate:18e6,level:40},{maxPictureSize:2228224,maxBitrate:3e7,level:41},{maxPictureSize:8912896,maxBitrate:6e7,level:50},{maxPictureSize:8912896,maxBitrate:12e7,level:51},{maxPictureSize:8912896,maxBitrate:18e7,level:52},{maxPictureSize:35651584,maxBitrate:18e7,level:60},{maxPictureSize:35651584,maxBitrate:24e7,level:61},{maxPictureSize:35651584,maxBitrate:48e7,level:62}],wt=[{maxPictureSize:147456,maxBitrate:15e5,tier:`M`,level:0},{maxPictureSize:278784,maxBitrate:3e6,tier:`M`,level:1},{maxPictureSize:665856,maxBitrate:6e6,tier:`M`,level:4},{maxPictureSize:1065024,maxBitrate:1e7,tier:`M`,level:5},{maxPictureSize:2359296,maxBitrate:12e6,tier:`M`,level:8},{maxPictureSize:2359296,maxBitrate:3e7,tier:`H`,level:8},{maxPictureSize:2359296,maxBitrate:2e7,tier:`M`,level:9},{maxPictureSize:2359296,maxBitrate:5e7,tier:`H`,level:9},{maxPictureSize:8912896,maxBitrate:3e7,tier:`M`,level:12},{maxPictureSize:8912896,maxBitrate:1e8,tier:`H`,level:12},{maxPictureSize:8912896,maxBitrate:4e7,tier:`M`,level:13},{maxPictureSize:8912896,maxBitrate:16e7,tier:`H`,level:13},{maxPictureSize:8912896,maxBitrate:6e7,tier:`M`,level:14},{maxPictureSize:8912896,maxBitrate:24e7,tier:`H`,level:14},{maxPictureSize:35651584,maxBitrate:6e7,tier:`M`,level:15},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:15},{maxPictureSize:35651584,maxBitrate:6e7,tier:`M`,level:16},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:16},{maxPictureSize:35651584,maxBitrate:1e8,tier:`M`,level:17},{maxPictureSize:35651584,maxBitrate:48e7,tier:`H`,level:17},{maxPictureSize:35651584,maxBitrate:16e7,tier:`M`,level:18},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:18},{maxPictureSize:35651584,maxBitrate:16e7,tier:`M`,level:19},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:19}],Tt=(e,t,n,r)=>{if(e===`avc`){let e=Math.ceil(t/16)*Math.ceil(n/16),i=xt.find(t=>e<=t.maxMacroblocks&&r<=t.maxBitrate)??S(xt),a=i?i.level:0;return`avc1.${`64`.padStart(2,`0`)}00${a.toString(16).padStart(2,`0`)}`}else if(e===`hevc`){let e=t*n,i=St.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??S(St);return`hev1.1.6.${i.tier}${i.level}.B0`}else if(e===`vp8`)return`vp8`;else if(e===`vp9`){let e=t*n;return`vp09.00.${(Ct.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??S(Ct)).level.toString().padStart(2,`0`)}.08`}else if(e===`av1`){let e=t*n,i=wt.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??S(wt);return`av01.0.${i.level.toString().padStart(2,`0`)}${i.tier}.08`}throw TypeError(`Unhandled codec '${e}'.`)},Et=e=>{let t=e.split(`.`),n=Number(t[1]),r=t[2],i=Number(r.slice(0,-1)),a=(n<<5)+i,o=r.slice(-1)===`H`?1:0,s=Number(t[3])===8?0:1,c=t[4]?Number(t[4]):0,l=t[5]?Number(t[5][0]):1,u=t[5]?Number(t[5][1]):1,d=t[5]?Number(t[5][2]):0;return[129,a,(o<<7)+(s<<6)+0+(c<<4)+(l<<3)+(u<<2)+d,0]},Dt=e=>{let{codec:t,codecDescription:n,colorSpace:r,avcCodecInfo:i,hevcCodecInfo:a,vp9CodecInfo:o,av1CodecInfo:s}=e;if(t===`avc`){if(x(e.avcType!==null),i){let t=new Uint8Array([i.avcProfileIndication,i.profileCompatibility,i.avcLevelIndication]);return`avc${e.avcType}.${Ie(t)}`}if(!n||n.byteLength<4)throw TypeError(`AVC decoder description is not provided or is not at least 4 bytes long.`);return`avc${e.avcType}.${Ie(n.subarray(1,4))}`}else if(t===`hevc`){let e,t,r,i,o,s;if(a)e=a.generalProfileSpace,t=a.generalProfileIdc,r=Le(a.generalProfileCompatibilityFlags),i=a.generalTierFlag,o=a.generalLevelIdc,s=[...a.generalConstraintIndicatorFlags];else{if(!n||n.byteLength<23)throw TypeError(`HEVC decoder description is not provided or is not at least 23 bytes long.`);let a=E(n),c=a.getUint8(1);e=c>>6&3,t=c&31,r=Le(a.getUint32(2)),i=c>>5&1,o=a.getUint8(12),s=[];for(let e=0;e<6;e++)s.push(a.getUint8(6+e))}let c=`hev1.`;for(c+=[``,`A`,`B`,`C`][e]+t,c+=`.`,c+=r.toString(16).toUpperCase(),c+=`.`,c+=i===0?`L`:`H`,c+=o;s.length>0&&s[s.length-1]===0;)s.pop();return s.length>0&&(c+=`.`,c+=s.map(e=>e.toString(16).toUpperCase()).join(`.`)),c}else if(t===`vp8`)return`vp8`;else if(t===`vp9`){if(!o){let t=e.width*e.height,n=S(Ct).level;for(let e of Ct)if(t<=e.maxPictureSize){n=e.level;break}return`vp09.00.${n.toString().padStart(2,`0`)}.08`}let t=o.profile.toString().padStart(2,`0`),n=o.level.toString().padStart(2,`0`),r=o.bitDepth.toString().padStart(2,`0`),i=o.chromaSubsampling.toString().padStart(2,`0`),a=o.colourPrimaries.toString().padStart(2,`0`),s=o.transferCharacteristics.toString().padStart(2,`0`),c=o.matrixCoefficients.toString().padStart(2,`0`),l=o.videoFullRangeFlag.toString().padStart(2,`0`),u=`vp09.${t}.${n}.${r}.${i}`;return u+=`.${a}.${s}.${c}.${l}`,u.endsWith(`.01.01.01.01.00`)&&(u=u.slice(0,-15)),u}else if(t===`av1`){if(!s){let t=e.width*e.height,n=S(Ct).level;for(let e of Ct)if(t<=e.maxPictureSize){n=e.level;break}return`av01.0.${n.toString().padStart(2,`0`)}M.08`}let t=s.profile,n=s.level.toString().padStart(2,`0`),i=s.tier?`H`:`M`,a=s.bitDepth.toString().padStart(2,`0`),o=s.monochrome?`1`:`0`,c=100*s.chromaSubsamplingX+10*s.chromaSubsamplingY+1*(s.chromaSubsamplingX&&s.chromaSubsamplingY?s.chromaSamplePosition:0),l=r?.primaries?De[r.primaries]:1,u=r?.transfer?ke[r.transfer]:1,d=r?.matrix?je[r.matrix]:1,f=r?.fullRange?1:0,p=`av01.${t}.${n}${i}.${a}`;return p+=`.${o}.${c.toString().padStart(3,`0`)}`,p+=`.${l.toString().padStart(2,`0`)}`,p+=`.${u.toString().padStart(2,`0`)}`,p+=`.${d.toString().padStart(2,`0`)}`,p+=`.${f}`,p.endsWith(`.0.110.01.01.01.0`)&&(p=p.slice(0,-17)),p}throw TypeError(`Unhandled codec '${t}'.`)},Ot=(e,t,n)=>{if(e===`aac`)return t>=2&&n<=24e3?`mp4a.40.29`:n<=24e3?`mp4a.40.5`:`mp4a.40.2`;if(e===`mp3`)return`mp3`;if(e===`opus`)return`opus`;if(e===`vorbis`)return`vorbis`;if(e===`flac`)return`flac`;if(M.includes(e))return e;throw TypeError(`Unhandled codec '${e}'.`)},kt=e=>{let{codec:t,codecDescription:n,aacCodecInfo:r}=e;if(t===`aac`){if(!r)throw TypeError(`AAC codec info must be provided.`);return r.isMpeg2?`mp4a.67`:`mp4a.40.${Mt(n).objectType}`}else if(t===`mp3`)return`mp3`;else if(t===`opus`)return`opus`;else if(t===`vorbis`)return`vorbis`;else if(t===`flac`)return`flac`;else if(t&&M.includes(t))return t;throw TypeError(`Unhandled codec '${t}'.`)},At=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],jt=[-1,1,2,3,4,5,6,8],Mt=e=>{if(!e||e.byteLength<2)throw TypeError(`AAC description must be at least 2 bytes long.`);let t=new C(e),n=t.readBits(5);n===31&&(n=32+t.readBits(6));let r=t.readBits(4),i=null;r===15?i=t.readBits(24):r<At.length&&(i=At[r]);let a=t.readBits(4),o=null;return a>=1&&a<=7&&(o=jt[a]),{objectType:n,frequencyIndex:r,sampleRate:i,channelConfiguration:a,numberOfChannels:o}},Nt=e=>{let t=At.indexOf(e.sampleRate),n=null;t===-1&&(t=15,n=e.sampleRate);let r=jt.indexOf(e.numberOfChannels);if(r===-1)throw TypeError(`Unsupported number of channels: ${e.numberOfChannels}`);let i=13;e.objectType>=32&&(i+=6),t===15&&(i+=24);let a=Math.ceil(i/8),o=new Uint8Array(a),s=new C(o);return e.objectType<32?s.writeBits(5,e.objectType):(s.writeBits(5,31),s.writeBits(6,e.objectType-32)),s.writeBits(4,t),t===15&&s.writeBits(24,n),s.writeBits(4,r),o},Pt=/^pcm-([usf])(\d+)+(be)?$/,Ft=e=>{if(x(M.includes(e)),e===`ulaw`)return{dataType:`ulaw`,sampleSize:1,littleEndian:!0,silentValue:255};if(e===`alaw`)return{dataType:`alaw`,sampleSize:1,littleEndian:!0,silentValue:213};let t=Pt.exec(e);x(t);let n;n=t[1]===`u`?`unsigned`:t[1]===`s`?`signed`:`float`;let r=Number(t[2])/8,i=t[3]!==`be`;return{dataType:n,sampleSize:r,littleEndian:i,silentValue:e===`pcm-u8`?2**7:0}},It=e=>e.startsWith(`avc1`)||e.startsWith(`avc3`)?`avc`:e.startsWith(`hev1`)||e.startsWith(`hvc1`)?`hevc`:e===`vp8`?`vp8`:e.startsWith(`vp09`)?`vp9`:e.startsWith(`av01`)?`av1`:e.startsWith(`mp4a.40`)||e===`mp4a.67`?`aac`:e===`mp3`||e===`mp4a.69`||e===`mp4a.6B`||e===`mp4a.6b`?`mp3`:e===`opus`?`opus`:e===`vorbis`?`vorbis`:e===`flac`?`flac`:e===`ulaw`?`ulaw`:e===`alaw`?`alaw`:Pt.test(e)?e:e===`webvtt`?`webvtt`:null,Lt=e=>e===`avc`?{avc:{format:`avc`}}:e===`hevc`?{hevc:{format:`hevc`}}:{},Rt=e=>e===`aac`?{aac:{format:`aac`}}:e===`opus`?{opus:{format:`opus`}}:{},zt=[`avc1`,`avc3`,`hev1`,`hvc1`,`vp8`,`vp09`,`av01`],Bt=/^(avc1|avc3)\.[0-9a-fA-F]{6}$/,Vt=/^(hev1|hvc1)\.(?:[ABC]?\d+)\.[0-9a-fA-F]{1,8}\.[LH]\d+(?:\.[0-9a-fA-F]{1,2}){0,6}$/,Ht=/^vp09(?:\.\d{2}){3}(?:(?:\.\d{2}){5})?$/,Ut=/^av01\.\d\.\d{2}[MH]\.\d{2}(?:\.\d\.\d{3}\.\d{2}\.\d{2}\.\d{2}\.\d)?$/,Wt=e=>{if(!e)throw TypeError(`Video chunk metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Video chunk metadata must be an object.`);if(!e.decoderConfig)throw TypeError(`Video chunk metadata must include a decoder configuration.`);if(typeof e.decoderConfig!=`object`)throw TypeError(`Video chunk metadata decoder configuration must be an object.`);if(typeof e.decoderConfig.codec!=`string`)throw TypeError(`Video chunk metadata decoder configuration must specify a codec string.`);if(!zt.some(t=>e.decoderConfig.codec.startsWith(t)))throw TypeError(`Video chunk metadata decoder configuration codec string must be a valid video codec string as specified in the WebCodecs Codec Registry.`);if(!Number.isInteger(e.decoderConfig.codedWidth)||e.decoderConfig.codedWidth<=0)throw TypeError(`Video chunk metadata decoder configuration must specify a valid codedWidth (positive integer).`);if(!Number.isInteger(e.decoderConfig.codedHeight)||e.decoderConfig.codedHeight<=0)throw TypeError(`Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).`);if(e.decoderConfig.description!==void 0&&!Pe(e.decoderConfig.description))throw TypeError(`Video chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.`);if(e.decoderConfig.colorSpace!==void 0){let{colorSpace:t}=e.decoderConfig;if(typeof t!=`object`)throw TypeError(`Video chunk metadata decoder configuration colorSpace, when provided, must be an object.`);let n=Object.keys(De);if(t.primaries!=null&&!n.includes(t.primaries))throw TypeError(`Video chunk metadata decoder configuration colorSpace primaries, when defined, must be one of ${n.join(`, `)}.`);let r=Object.keys(ke);if(t.transfer!=null&&!r.includes(t.transfer))throw TypeError(`Video chunk metadata decoder configuration colorSpace transfer, when defined, must be one of ${r.join(`, `)}.`);let i=Object.keys(je);if(t.matrix!=null&&!i.includes(t.matrix))throw TypeError(`Video chunk metadata decoder configuration colorSpace matrix, when defined, must be one of ${i.join(`, `)}.`);if(t.fullRange!=null&&typeof t.fullRange!=`boolean`)throw TypeError(`Video chunk metadata decoder configuration colorSpace fullRange, when defined, must be a boolean.`)}if(e.decoderConfig.codec.startsWith(`avc1`)||e.decoderConfig.codec.startsWith(`avc3`)){if(!Bt.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for AVC must be a valid AVC codec string as specified in Section 3.4 of RFC 6381.`)}else if(e.decoderConfig.codec.startsWith(`hev1`)||e.decoderConfig.codec.startsWith(`hvc1`)){if(!Vt.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for HEVC must be a valid HEVC codec string as specified in Section E.3 of ISO 14496-15.`)}else if(e.decoderConfig.codec.startsWith(`vp8`)){if(e.decoderConfig.codec!==`vp8`)throw TypeError(`Video chunk metadata decoder configuration codec string for VP8 must be "vp8".`)}else if(e.decoderConfig.codec.startsWith(`vp09`)){if(!Ht.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for VP9 must be a valid VP9 codec string as specified in Section "Codecs Parameter String" of https://www.webmproject.org/vp9/mp4/.`)}else if(e.decoderConfig.codec.startsWith(`av01`)&&!Ut.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for AV1 must be a valid AV1 codec string as specified in Section "Codecs Parameter String" of https://aomediacodec.github.io/av1-isobmff/.`)},Gt=[`mp4a`,`mp3`,`opus`,`vorbis`,`flac`,`ulaw`,`alaw`,`pcm`],Kt=e=>{if(!e)throw TypeError(`Audio chunk metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Audio chunk metadata must be an object.`);if(!e.decoderConfig)throw TypeError(`Audio chunk metadata must include a decoder configuration.`);if(typeof e.decoderConfig!=`object`)throw TypeError(`Audio chunk metadata decoder configuration must be an object.`);if(typeof e.decoderConfig.codec!=`string`)throw TypeError(`Audio chunk metadata decoder configuration must specify a codec string.`);if(!Gt.some(t=>e.decoderConfig.codec.startsWith(t)))throw TypeError(`Audio chunk metadata decoder configuration codec string must be a valid audio codec string as specified in the WebCodecs Codec Registry.`);if(!Number.isInteger(e.decoderConfig.sampleRate)||e.decoderConfig.sampleRate<=0)throw TypeError(`Audio chunk metadata decoder configuration must specify a valid sampleRate (positive integer).`);if(!Number.isInteger(e.decoderConfig.numberOfChannels)||e.decoderConfig.numberOfChannels<=0)throw TypeError(`Audio chunk metadata decoder configuration must specify a valid numberOfChannels (positive integer).`);if(e.decoderConfig.description!==void 0&&!Pe(e.decoderConfig.description))throw TypeError(`Audio chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.`);if(e.decoderConfig.codec.startsWith(`mp4a`)&&e.decoderConfig.codec!==`mp4a.69`&&e.decoderConfig.codec!==`mp4a.6B`&&e.decoderConfig.codec!==`mp4a.6b`){if(![`mp4a.40.2`,`mp4a.40.02`,`mp4a.40.5`,`mp4a.40.05`,`mp4a.40.29`,`mp4a.67`].includes(e.decoderConfig.codec))throw TypeError(`Audio chunk metadata decoder configuration codec string for AAC must be a valid AAC codec string as specified in https://www.w3.org/TR/webcodecs-aac-codec-registration/.`);if(!e.decoderConfig.description)throw TypeError(`Audio chunk metadata decoder configuration for AAC must include a description, which is expected to be an AudioSpecificConfig as specified in ISO 14496-3.`)}else if(e.decoderConfig.codec.startsWith(`mp3`)||e.decoderConfig.codec.startsWith(`mp4a`)){if(e.decoderConfig.codec!==`mp3`&&e.decoderConfig.codec!==`mp4a.69`&&e.decoderConfig.codec!==`mp4a.6B`&&e.decoderConfig.codec!==`mp4a.6b`)throw TypeError(`Audio chunk metadata decoder configuration codec string for MP3 must be "mp3", "mp4a.69" or "mp4a.6B".`)}else if(e.decoderConfig.codec.startsWith(`opus`)){if(e.decoderConfig.codec!==`opus`)throw TypeError(`Audio chunk metadata decoder configuration codec string for Opus must be "opus".`);if(e.decoderConfig.description&&e.decoderConfig.description.byteLength<18)throw TypeError(`Audio chunk metadata decoder configuration description, when specified, is expected to be an Identification Header as specified in Section 5.1 of RFC 7845.`)}else if(e.decoderConfig.codec.startsWith(`vorbis`)){if(e.decoderConfig.codec!==`vorbis`)throw TypeError(`Audio chunk metadata decoder configuration codec string for Vorbis must be "vorbis".`);if(!e.decoderConfig.description)throw TypeError(`Audio chunk metadata decoder configuration for Vorbis must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-vorbis-codec-registration/.`)}else if(e.decoderConfig.codec.startsWith(`flac`)){if(e.decoderConfig.codec!==`flac`)throw TypeError(`Audio chunk metadata decoder configuration codec string for FLAC must be "flac".`);if(!e.decoderConfig.description||e.decoderConfig.description.byteLength<42)throw TypeError(`Audio chunk metadata decoder configuration for FLAC must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-flac-codec-registration/.`)}else if((e.decoderConfig.codec.startsWith(`pcm`)||e.decoderConfig.codec.startsWith(`ulaw`)||e.decoderConfig.codec.startsWith(`alaw`))&&!M.includes(e.decoderConfig.codec))throw TypeError(`Audio chunk metadata decoder configuration codec string for PCM must be one of the supported PCM codecs (${M.join(`, `)}).`)},qt=e=>{if(!e)throw TypeError(`Subtitle metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Subtitle metadata must be an object.`);if(!e.config)throw TypeError(`Subtitle metadata must include a config object.`);if(typeof e.config!=`object`)throw TypeError(`Subtitle metadata config must be an object.`);if(typeof e.config.description!=`string`)throw TypeError(`Subtitle metadata config description must be a string.`)};
|
|
17
|
+
var gt=class{constructor(e,t){if(this.data=e,this.mimeType=t,!(e instanceof Uint8Array))throw TypeError(`data must be a Uint8Array.`);if(typeof t!=`string`)throw TypeError(`mimeType must be a string.`)}},_t=class{constructor(e,t,n,r){if(this.data=e,this.mimeType=t,this.name=n,this.description=r,!(e instanceof Uint8Array))throw TypeError(`data must be a Uint8Array.`);if(t!==void 0&&typeof t!=`string`)throw TypeError(`mimeType, when provided, must be a string.`);if(n!==void 0&&typeof n!=`string`)throw TypeError(`name, when provided, must be a string.`);if(r!==void 0&&typeof r!=`string`)throw TypeError(`description, when provided, must be a string.`)}};let vt=e=>{if(!e||typeof e!=`object`)throw TypeError(`tags must be an object.`);if(e.title!==void 0&&typeof e.title!=`string`)throw TypeError(`tags.title, when provided, must be a string.`);if(e.description!==void 0&&typeof e.description!=`string`)throw TypeError(`tags.description, when provided, must be a string.`);if(e.artist!==void 0&&typeof e.artist!=`string`)throw TypeError(`tags.artist, when provided, must be a string.`);if(e.album!==void 0&&typeof e.album!=`string`)throw TypeError(`tags.album, when provided, must be a string.`);if(e.albumArtist!==void 0&&typeof e.albumArtist!=`string`)throw TypeError(`tags.albumArtist, when provided, must be a string.`);if(e.trackNumber!==void 0&&(!Number.isInteger(e.trackNumber)||e.trackNumber<=0))throw TypeError(`tags.trackNumber, when provided, must be a positive integer.`);if(e.tracksTotal!==void 0&&(!Number.isInteger(e.tracksTotal)||e.tracksTotal<=0))throw TypeError(`tags.tracksTotal, when provided, must be a positive integer.`);if(e.discNumber!==void 0&&(!Number.isInteger(e.discNumber)||e.discNumber<=0))throw TypeError(`tags.discNumber, when provided, must be a positive integer.`);if(e.discsTotal!==void 0&&(!Number.isInteger(e.discsTotal)||e.discsTotal<=0))throw TypeError(`tags.discsTotal, when provided, must be a positive integer.`);if(e.genre!==void 0&&typeof e.genre!=`string`)throw TypeError(`tags.genre, when provided, must be a string.`);if(e.date!==void 0&&(!(e.date instanceof Date)||Number.isNaN(e.date.getTime())))throw TypeError(`tags.date, when provided, must be a valid Date.`);if(e.lyrics!==void 0&&typeof e.lyrics!=`string`)throw TypeError(`tags.lyrics, when provided, must be a string.`);if(e.images!==void 0){if(!Array.isArray(e.images))throw TypeError(`tags.images, when provided, must be an array.`);for(let t of e.images){if(!t||typeof t!=`object`)throw TypeError(`Each image in tags.images must be an object.`);if(!(t.data instanceof Uint8Array))throw TypeError(`Each image.data must be a Uint8Array.`);if(typeof t.mimeType!=`string`)throw TypeError(`Each image.mimeType must be a string.`);if(![`coverFront`,`coverBack`,`unknown`].includes(t.kind))throw TypeError(`Each image.kind must be 'coverFront', 'coverBack', or 'unknown'.`)}}if(e.comment!==void 0&&typeof e.comment!=`string`)throw TypeError(`tags.comment, when provided, must be a string.`);if(e.raw!==void 0){if(!e.raw||typeof e.raw!=`object`)throw TypeError(`tags.raw, when provided, must be an object.`);for(let t of Object.values(e.raw))if(t!==null&&typeof t!=`string`&&!(t instanceof Uint8Array)&&!(t instanceof gt)&&!(t instanceof _t))throw TypeError(`Each value in tags.raw must be a string, Uint8Array, RichImageData, AttachedFile, or null.`)}},yt={default:!0,forced:!1,original:!1,commentary:!1,hearingImpaired:!1,visuallyImpaired:!1},bt=e=>{if(!e||typeof e!=`object`)throw TypeError(`disposition must be an object.`);if(e.default!==void 0&&typeof e.default!=`boolean`)throw TypeError(`disposition.default must be a boolean.`);if(e.forced!==void 0&&typeof e.forced!=`boolean`)throw TypeError(`disposition.forced must be a boolean.`);if(e.original!==void 0&&typeof e.original!=`boolean`)throw TypeError(`disposition.original must be a boolean.`);if(e.commentary!==void 0&&typeof e.commentary!=`boolean`)throw TypeError(`disposition.commentary must be a boolean.`);if(e.hearingImpaired!==void 0&&typeof e.hearingImpaired!=`boolean`)throw TypeError(`disposition.hearingImpaired must be a boolean.`);if(e.visuallyImpaired!==void 0&&typeof e.visuallyImpaired!=`boolean`)throw TypeError(`disposition.visuallyImpaired must be a boolean.`)},A=[`avc`,`hevc`,`vp9`,`av1`,`vp8`],j=[`pcm-s16`,`pcm-s16be`,`pcm-s24`,`pcm-s24be`,`pcm-s32`,`pcm-s32be`,`pcm-f32`,`pcm-f32be`,`pcm-f64`,`pcm-f64be`,`pcm-u8`,`pcm-s8`,`ulaw`,`alaw`],xt=[`aac`,`opus`,`mp3`,`vorbis`,`flac`],M=[...xt,...j],St=[`webvtt`],Ct=[{maxMacroblocks:99,maxBitrate:64e3,level:10},{maxMacroblocks:396,maxBitrate:192e3,level:11},{maxMacroblocks:396,maxBitrate:384e3,level:12},{maxMacroblocks:396,maxBitrate:768e3,level:13},{maxMacroblocks:396,maxBitrate:2e6,level:20},{maxMacroblocks:792,maxBitrate:4e6,level:21},{maxMacroblocks:1620,maxBitrate:4e6,level:22},{maxMacroblocks:1620,maxBitrate:1e7,level:30},{maxMacroblocks:3600,maxBitrate:14e6,level:31},{maxMacroblocks:5120,maxBitrate:2e7,level:32},{maxMacroblocks:8192,maxBitrate:2e7,level:40},{maxMacroblocks:8192,maxBitrate:5e7,level:41},{maxMacroblocks:8704,maxBitrate:5e7,level:42},{maxMacroblocks:22080,maxBitrate:135e6,level:50},{maxMacroblocks:36864,maxBitrate:24e7,level:51},{maxMacroblocks:36864,maxBitrate:24e7,level:52},{maxMacroblocks:139264,maxBitrate:24e7,level:60},{maxMacroblocks:139264,maxBitrate:48e7,level:61},{maxMacroblocks:139264,maxBitrate:8e8,level:62}],wt=[{maxPictureSize:36864,maxBitrate:128e3,tier:`L`,level:30},{maxPictureSize:122880,maxBitrate:15e5,tier:`L`,level:60},{maxPictureSize:245760,maxBitrate:3e6,tier:`L`,level:63},{maxPictureSize:552960,maxBitrate:6e6,tier:`L`,level:90},{maxPictureSize:983040,maxBitrate:1e7,tier:`L`,level:93},{maxPictureSize:2228224,maxBitrate:12e6,tier:`L`,level:120},{maxPictureSize:2228224,maxBitrate:3e7,tier:`H`,level:120},{maxPictureSize:2228224,maxBitrate:2e7,tier:`L`,level:123},{maxPictureSize:2228224,maxBitrate:5e7,tier:`H`,level:123},{maxPictureSize:8912896,maxBitrate:25e6,tier:`L`,level:150},{maxPictureSize:8912896,maxBitrate:1e8,tier:`H`,level:150},{maxPictureSize:8912896,maxBitrate:4e7,tier:`L`,level:153},{maxPictureSize:8912896,maxBitrate:16e7,tier:`H`,level:153},{maxPictureSize:8912896,maxBitrate:6e7,tier:`L`,level:156},{maxPictureSize:8912896,maxBitrate:24e7,tier:`H`,level:156},{maxPictureSize:35651584,maxBitrate:6e7,tier:`L`,level:180},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:180},{maxPictureSize:35651584,maxBitrate:12e7,tier:`L`,level:183},{maxPictureSize:35651584,maxBitrate:48e7,tier:`H`,level:183},{maxPictureSize:35651584,maxBitrate:24e7,tier:`L`,level:186},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:186}],Tt=[{maxPictureSize:36864,maxBitrate:2e5,level:10},{maxPictureSize:73728,maxBitrate:8e5,level:11},{maxPictureSize:122880,maxBitrate:18e5,level:20},{maxPictureSize:245760,maxBitrate:36e5,level:21},{maxPictureSize:552960,maxBitrate:72e5,level:30},{maxPictureSize:983040,maxBitrate:12e6,level:31},{maxPictureSize:2228224,maxBitrate:18e6,level:40},{maxPictureSize:2228224,maxBitrate:3e7,level:41},{maxPictureSize:8912896,maxBitrate:6e7,level:50},{maxPictureSize:8912896,maxBitrate:12e7,level:51},{maxPictureSize:8912896,maxBitrate:18e7,level:52},{maxPictureSize:35651584,maxBitrate:18e7,level:60},{maxPictureSize:35651584,maxBitrate:24e7,level:61},{maxPictureSize:35651584,maxBitrate:48e7,level:62}],Et=[{maxPictureSize:147456,maxBitrate:15e5,tier:`M`,level:0},{maxPictureSize:278784,maxBitrate:3e6,tier:`M`,level:1},{maxPictureSize:665856,maxBitrate:6e6,tier:`M`,level:4},{maxPictureSize:1065024,maxBitrate:1e7,tier:`M`,level:5},{maxPictureSize:2359296,maxBitrate:12e6,tier:`M`,level:8},{maxPictureSize:2359296,maxBitrate:3e7,tier:`H`,level:8},{maxPictureSize:2359296,maxBitrate:2e7,tier:`M`,level:9},{maxPictureSize:2359296,maxBitrate:5e7,tier:`H`,level:9},{maxPictureSize:8912896,maxBitrate:3e7,tier:`M`,level:12},{maxPictureSize:8912896,maxBitrate:1e8,tier:`H`,level:12},{maxPictureSize:8912896,maxBitrate:4e7,tier:`M`,level:13},{maxPictureSize:8912896,maxBitrate:16e7,tier:`H`,level:13},{maxPictureSize:8912896,maxBitrate:6e7,tier:`M`,level:14},{maxPictureSize:8912896,maxBitrate:24e7,tier:`H`,level:14},{maxPictureSize:35651584,maxBitrate:6e7,tier:`M`,level:15},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:15},{maxPictureSize:35651584,maxBitrate:6e7,tier:`M`,level:16},{maxPictureSize:35651584,maxBitrate:24e7,tier:`H`,level:16},{maxPictureSize:35651584,maxBitrate:1e8,tier:`M`,level:17},{maxPictureSize:35651584,maxBitrate:48e7,tier:`H`,level:17},{maxPictureSize:35651584,maxBitrate:16e7,tier:`M`,level:18},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:18},{maxPictureSize:35651584,maxBitrate:16e7,tier:`M`,level:19},{maxPictureSize:35651584,maxBitrate:8e8,tier:`H`,level:19}],Dt=(e,t,n,r)=>{if(e===`avc`){let e=Math.ceil(t/16)*Math.ceil(n/16),i=Ct.find(t=>e<=t.maxMacroblocks&&r<=t.maxBitrate)??x(Ct),a=i?i.level:0;return`avc1.${`64`.padStart(2,`0`)}00${a.toString(16).padStart(2,`0`)}`}else if(e===`hevc`){let e=t*n,i=wt.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??x(wt);return`hev1.1.6.${i.tier}${i.level}.B0`}else if(e===`vp8`)return`vp8`;else if(e===`vp9`){let e=t*n;return`vp09.00.${(Tt.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??x(Tt)).level.toString().padStart(2,`0`)}.08`}else if(e===`av1`){let e=t*n,i=Et.find(t=>e<=t.maxPictureSize&&r<=t.maxBitrate)??x(Et);return`av01.0.${i.level.toString().padStart(2,`0`)}${i.tier}.08`}throw TypeError(`Unhandled codec '${e}'.`)},Ot=e=>{let t=e.split(`.`),n=Number(t[1]),r=t[2],i=Number(r.slice(0,-1)),a=(n<<5)+i,o=r.slice(-1)===`H`?1:0,s=Number(t[3])===8?0:1,c=t[4]?Number(t[4]):0,l=t[5]?Number(t[5][0]):1,u=t[5]?Number(t[5][1]):1,d=t[5]?Number(t[5][2]):0;return[129,a,(o<<7)+(s<<6)+0+(c<<4)+(l<<3)+(u<<2)+d,0]},kt=e=>{let{codec:t,codecDescription:n,colorSpace:r,avcCodecInfo:i,hevcCodecInfo:a,vp9CodecInfo:o,av1CodecInfo:s}=e;if(t===`avc`){if(b(e.avcType!==null),i){let t=new Uint8Array([i.avcProfileIndication,i.profileCompatibility,i.avcLevelIndication]);return`avc${e.avcType}.${Re(t)}`}if(!n||n.byteLength<4)throw TypeError(`AVC decoder description is not provided or is not at least 4 bytes long.`);return`avc${e.avcType}.${Re(n.subarray(1,4))}`}else if(t===`hevc`){let e,t,r,i,o,s;if(a)e=a.generalProfileSpace,t=a.generalProfileIdc,r=ze(a.generalProfileCompatibilityFlags),i=a.generalTierFlag,o=a.generalLevelIdc,s=[...a.generalConstraintIndicatorFlags];else{if(!n||n.byteLength<23)throw TypeError(`HEVC decoder description is not provided or is not at least 23 bytes long.`);let a=T(n),c=a.getUint8(1);e=c>>6&3,t=c&31,r=ze(a.getUint32(2)),i=c>>5&1,o=a.getUint8(12),s=[];for(let e=0;e<6;e++)s.push(a.getUint8(6+e))}let c=`hev1.`;for(c+=[``,`A`,`B`,`C`][e]+t,c+=`.`,c+=r.toString(16).toUpperCase(),c+=`.`,c+=i===0?`L`:`H`,c+=o;s.length>0&&s[s.length-1]===0;)s.pop();return s.length>0&&(c+=`.`,c+=s.map(e=>e.toString(16).toUpperCase()).join(`.`)),c}else if(t===`vp8`)return`vp8`;else if(t===`vp9`){if(!o){let t=e.width*e.height,n=x(Tt).level;for(let e of Tt)if(t<=e.maxPictureSize){n=e.level;break}return`vp09.00.${n.toString().padStart(2,`0`)}.08`}let t=o.profile.toString().padStart(2,`0`),n=o.level.toString().padStart(2,`0`),r=o.bitDepth.toString().padStart(2,`0`),i=o.chromaSubsampling.toString().padStart(2,`0`),a=o.colourPrimaries.toString().padStart(2,`0`),s=o.transferCharacteristics.toString().padStart(2,`0`),c=o.matrixCoefficients.toString().padStart(2,`0`),l=o.videoFullRangeFlag.toString().padStart(2,`0`),u=`vp09.${t}.${n}.${r}.${i}`;return u+=`.${a}.${s}.${c}.${l}`,u.endsWith(`.01.01.01.01.00`)&&(u=u.slice(0,-15)),u}else if(t===`av1`){if(!s){let t=e.width*e.height,n=x(Tt).level;for(let e of Tt)if(t<=e.maxPictureSize){n=e.level;break}return`av01.0.${n.toString().padStart(2,`0`)}M.08`}let t=s.profile,n=s.level.toString().padStart(2,`0`),i=s.tier?`H`:`M`,a=s.bitDepth.toString().padStart(2,`0`),o=s.monochrome?`1`:`0`,c=100*s.chromaSubsamplingX+10*s.chromaSubsamplingY+1*(s.chromaSubsamplingX&&s.chromaSubsamplingY?s.chromaSamplePosition:0),l=r?.primaries?ke[r.primaries]:1,u=r?.transfer?je[r.transfer]:1,d=r?.matrix?Ne[r.matrix]:1,f=r?.fullRange?1:0,p=`av01.${t}.${n}${i}.${a}`;return p+=`.${o}.${c.toString().padStart(3,`0`)}`,p+=`.${l.toString().padStart(2,`0`)}`,p+=`.${u.toString().padStart(2,`0`)}`,p+=`.${d.toString().padStart(2,`0`)}`,p+=`.${f}`,p.endsWith(`.0.110.01.01.01.0`)&&(p=p.slice(0,-17)),p}throw TypeError(`Unhandled codec '${t}'.`)},At=(e,t,n)=>{if(e===`aac`)return t>=2&&n<=24e3?`mp4a.40.29`:n<=24e3?`mp4a.40.5`:`mp4a.40.2`;if(e===`mp3`)return`mp3`;if(e===`opus`)return`opus`;if(e===`vorbis`)return`vorbis`;if(e===`flac`)return`flac`;if(j.includes(e))return e;throw TypeError(`Unhandled codec '${e}'.`)},jt=e=>{let{codec:t,codecDescription:n,aacCodecInfo:r}=e;if(t===`aac`){if(!r)throw TypeError(`AAC codec info must be provided.`);return r.isMpeg2?`mp4a.67`:`mp4a.40.${Pt(n).objectType}`}else if(t===`mp3`)return`mp3`;else if(t===`opus`)return`opus`;else if(t===`vorbis`)return`vorbis`;else if(t===`flac`)return`flac`;else if(t&&j.includes(t))return t;throw TypeError(`Unhandled codec '${t}'.`)},Mt=[96e3,88200,64e3,48e3,44100,32e3,24e3,22050,16e3,12e3,11025,8e3,7350],Nt=[-1,1,2,3,4,5,6,8],Pt=e=>{if(!e||e.byteLength<2)throw TypeError(`AAC description must be at least 2 bytes long.`);let t=new S(e),n=t.readBits(5);n===31&&(n=32+t.readBits(6));let r=t.readBits(4),i=null;r===15?i=t.readBits(24):r<Mt.length&&(i=Mt[r]);let a=t.readBits(4),o=null;return a>=1&&a<=7&&(o=Nt[a]),{objectType:n,frequencyIndex:r,sampleRate:i,channelConfiguration:a,numberOfChannels:o}},Ft=e=>{let t=Mt.indexOf(e.sampleRate),n=null;t===-1&&(t=15,n=e.sampleRate);let r=Nt.indexOf(e.numberOfChannels);if(r===-1)throw TypeError(`Unsupported number of channels: ${e.numberOfChannels}`);let i=13;e.objectType>=32&&(i+=6),t===15&&(i+=24);let a=Math.ceil(i/8),o=new Uint8Array(a),s=new S(o);return e.objectType<32?s.writeBits(5,e.objectType):(s.writeBits(5,31),s.writeBits(6,e.objectType-32)),s.writeBits(4,t),t===15&&s.writeBits(24,n),s.writeBits(4,r),o},It=/^pcm-([usf])(\d+)+(be)?$/,Lt=e=>{if(b(j.includes(e)),e===`ulaw`)return{dataType:`ulaw`,sampleSize:1,littleEndian:!0,silentValue:255};if(e===`alaw`)return{dataType:`alaw`,sampleSize:1,littleEndian:!0,silentValue:213};let t=It.exec(e);b(t);let n;n=t[1]===`u`?`unsigned`:t[1]===`s`?`signed`:`float`;let r=Number(t[2])/8,i=t[3]!==`be`;return{dataType:n,sampleSize:r,littleEndian:i,silentValue:e===`pcm-u8`?2**7:0}},Rt=e=>e.startsWith(`avc1`)||e.startsWith(`avc3`)?`avc`:e.startsWith(`hev1`)||e.startsWith(`hvc1`)?`hevc`:e===`vp8`?`vp8`:e.startsWith(`vp09`)?`vp9`:e.startsWith(`av01`)?`av1`:e.startsWith(`mp4a.40`)||e===`mp4a.67`?`aac`:e===`mp3`||e===`mp4a.69`||e===`mp4a.6B`||e===`mp4a.6b`?`mp3`:e===`opus`?`opus`:e===`vorbis`?`vorbis`:e===`flac`?`flac`:e===`ulaw`?`ulaw`:e===`alaw`?`alaw`:It.test(e)?e:e===`webvtt`?`webvtt`:null,zt=e=>e===`avc`?{avc:{format:`avc`}}:e===`hevc`?{hevc:{format:`hevc`}}:{},Bt=e=>e===`aac`?{aac:{format:`aac`}}:e===`opus`?{opus:{format:`opus`}}:{},Vt=[`avc1`,`avc3`,`hev1`,`hvc1`,`vp8`,`vp09`,`av01`],Ht=/^(avc1|avc3)\.[0-9a-fA-F]{6}$/,Ut=/^(hev1|hvc1)\.(?:[ABC]?\d+)\.[0-9a-fA-F]{1,8}\.[LH]\d+(?:\.[0-9a-fA-F]{1,2}){0,6}$/,Wt=/^vp09(?:\.\d{2}){3}(?:(?:\.\d{2}){5})?$/,Gt=/^av01\.\d\.\d{2}[MH]\.\d{2}(?:\.\d\.\d{3}\.\d{2}\.\d{2}\.\d{2}\.\d)?$/,Kt=e=>{if(!e)throw TypeError(`Video chunk metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Video chunk metadata must be an object.`);if(!e.decoderConfig)throw TypeError(`Video chunk metadata must include a decoder configuration.`);if(typeof e.decoderConfig!=`object`)throw TypeError(`Video chunk metadata decoder configuration must be an object.`);if(typeof e.decoderConfig.codec!=`string`)throw TypeError(`Video chunk metadata decoder configuration must specify a codec string.`);if(!Vt.some(t=>e.decoderConfig.codec.startsWith(t)))throw TypeError(`Video chunk metadata decoder configuration codec string must be a valid video codec string as specified in the WebCodecs Codec Registry.`);if(!Number.isInteger(e.decoderConfig.codedWidth)||e.decoderConfig.codedWidth<=0)throw TypeError(`Video chunk metadata decoder configuration must specify a valid codedWidth (positive integer).`);if(!Number.isInteger(e.decoderConfig.codedHeight)||e.decoderConfig.codedHeight<=0)throw TypeError(`Video chunk metadata decoder configuration must specify a valid codedHeight (positive integer).`);if(e.decoderConfig.description!==void 0&&!Ie(e.decoderConfig.description))throw TypeError(`Video chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.`);if(e.decoderConfig.colorSpace!==void 0){let{colorSpace:t}=e.decoderConfig;if(typeof t!=`object`)throw TypeError(`Video chunk metadata decoder configuration colorSpace, when provided, must be an object.`);let n=Object.keys(ke);if(t.primaries!=null&&!n.includes(t.primaries))throw TypeError(`Video chunk metadata decoder configuration colorSpace primaries, when defined, must be one of ${n.join(`, `)}.`);let r=Object.keys(je);if(t.transfer!=null&&!r.includes(t.transfer))throw TypeError(`Video chunk metadata decoder configuration colorSpace transfer, when defined, must be one of ${r.join(`, `)}.`);let i=Object.keys(Ne);if(t.matrix!=null&&!i.includes(t.matrix))throw TypeError(`Video chunk metadata decoder configuration colorSpace matrix, when defined, must be one of ${i.join(`, `)}.`);if(t.fullRange!=null&&typeof t.fullRange!=`boolean`)throw TypeError(`Video chunk metadata decoder configuration colorSpace fullRange, when defined, must be a boolean.`)}if(e.decoderConfig.codec.startsWith(`avc1`)||e.decoderConfig.codec.startsWith(`avc3`)){if(!Ht.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for AVC must be a valid AVC codec string as specified in Section 3.4 of RFC 6381.`)}else if(e.decoderConfig.codec.startsWith(`hev1`)||e.decoderConfig.codec.startsWith(`hvc1`)){if(!Ut.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for HEVC must be a valid HEVC codec string as specified in Section E.3 of ISO 14496-15.`)}else if(e.decoderConfig.codec.startsWith(`vp8`)){if(e.decoderConfig.codec!==`vp8`)throw TypeError(`Video chunk metadata decoder configuration codec string for VP8 must be "vp8".`)}else if(e.decoderConfig.codec.startsWith(`vp09`)){if(!Wt.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for VP9 must be a valid VP9 codec string as specified in Section "Codecs Parameter String" of https://www.webmproject.org/vp9/mp4/.`)}else if(e.decoderConfig.codec.startsWith(`av01`)&&!Gt.test(e.decoderConfig.codec))throw TypeError(`Video chunk metadata decoder configuration codec string for AV1 must be a valid AV1 codec string as specified in Section "Codecs Parameter String" of https://aomediacodec.github.io/av1-isobmff/.`)},qt=[`mp4a`,`mp3`,`opus`,`vorbis`,`flac`,`ulaw`,`alaw`,`pcm`],Jt=e=>{if(!e)throw TypeError(`Audio chunk metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Audio chunk metadata must be an object.`);if(!e.decoderConfig)throw TypeError(`Audio chunk metadata must include a decoder configuration.`);if(typeof e.decoderConfig!=`object`)throw TypeError(`Audio chunk metadata decoder configuration must be an object.`);if(typeof e.decoderConfig.codec!=`string`)throw TypeError(`Audio chunk metadata decoder configuration must specify a codec string.`);if(!qt.some(t=>e.decoderConfig.codec.startsWith(t)))throw TypeError(`Audio chunk metadata decoder configuration codec string must be a valid audio codec string as specified in the WebCodecs Codec Registry.`);if(!Number.isInteger(e.decoderConfig.sampleRate)||e.decoderConfig.sampleRate<=0)throw TypeError(`Audio chunk metadata decoder configuration must specify a valid sampleRate (positive integer).`);if(!Number.isInteger(e.decoderConfig.numberOfChannels)||e.decoderConfig.numberOfChannels<=0)throw TypeError(`Audio chunk metadata decoder configuration must specify a valid numberOfChannels (positive integer).`);if(e.decoderConfig.description!==void 0&&!Ie(e.decoderConfig.description))throw TypeError(`Audio chunk metadata decoder configuration description, when defined, must be an ArrayBuffer or an ArrayBuffer view.`);if(e.decoderConfig.codec.startsWith(`mp4a`)&&e.decoderConfig.codec!==`mp4a.69`&&e.decoderConfig.codec!==`mp4a.6B`&&e.decoderConfig.codec!==`mp4a.6b`){if(![`mp4a.40.2`,`mp4a.40.02`,`mp4a.40.5`,`mp4a.40.05`,`mp4a.40.29`,`mp4a.67`].includes(e.decoderConfig.codec))throw TypeError(`Audio chunk metadata decoder configuration codec string for AAC must be a valid AAC codec string as specified in https://www.w3.org/TR/webcodecs-aac-codec-registration/.`);if(!e.decoderConfig.description)throw TypeError(`Audio chunk metadata decoder configuration for AAC must include a description, which is expected to be an AudioSpecificConfig as specified in ISO 14496-3.`)}else if(e.decoderConfig.codec.startsWith(`mp3`)||e.decoderConfig.codec.startsWith(`mp4a`)){if(e.decoderConfig.codec!==`mp3`&&e.decoderConfig.codec!==`mp4a.69`&&e.decoderConfig.codec!==`mp4a.6B`&&e.decoderConfig.codec!==`mp4a.6b`)throw TypeError(`Audio chunk metadata decoder configuration codec string for MP3 must be "mp3", "mp4a.69" or "mp4a.6B".`)}else if(e.decoderConfig.codec.startsWith(`opus`)){if(e.decoderConfig.codec!==`opus`)throw TypeError(`Audio chunk metadata decoder configuration codec string for Opus must be "opus".`);if(e.decoderConfig.description&&e.decoderConfig.description.byteLength<18)throw TypeError(`Audio chunk metadata decoder configuration description, when specified, is expected to be an Identification Header as specified in Section 5.1 of RFC 7845.`)}else if(e.decoderConfig.codec.startsWith(`vorbis`)){if(e.decoderConfig.codec!==`vorbis`)throw TypeError(`Audio chunk metadata decoder configuration codec string for Vorbis must be "vorbis".`);if(!e.decoderConfig.description)throw TypeError(`Audio chunk metadata decoder configuration for Vorbis must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-vorbis-codec-registration/.`)}else if(e.decoderConfig.codec.startsWith(`flac`)){if(e.decoderConfig.codec!==`flac`)throw TypeError(`Audio chunk metadata decoder configuration codec string for FLAC must be "flac".`);if(!e.decoderConfig.description||e.decoderConfig.description.byteLength<42)throw TypeError(`Audio chunk metadata decoder configuration for FLAC must include a description, which is expected to adhere to the format described in https://www.w3.org/TR/webcodecs-flac-codec-registration/.`)}else if((e.decoderConfig.codec.startsWith(`pcm`)||e.decoderConfig.codec.startsWith(`ulaw`)||e.decoderConfig.codec.startsWith(`alaw`))&&!j.includes(e.decoderConfig.codec))throw TypeError(`Audio chunk metadata decoder configuration codec string for PCM must be one of the supported PCM codecs (${j.join(`, `)}).`)},Yt=e=>{if(!e)throw TypeError(`Subtitle metadata must be provided.`);if(typeof e!=`object`)throw TypeError(`Subtitle metadata must be an object.`);if(!e.config)throw TypeError(`Subtitle metadata must include a config object.`);if(typeof e.config!=`object`)throw TypeError(`Subtitle metadata config must be an object.`);if(typeof e.config.description!=`string`)throw TypeError(`Subtitle metadata config description must be a string.`)};
|
|
18
18
|
/*!
|
|
19
19
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
20
20
|
*
|
|
@@ -22,7 +22,7 @@ var mt=class{constructor(e,t){if(this.data=e,this.mimeType=t,!(e instanceof Uint
|
|
|
22
22
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
23
23
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
24
24
|
*/
|
|
25
|
-
var Jt=class{constructor(e){this.mutex=new Fe,this.firstMediaStreamTimestamp=null,this.trackTimestampInfo=new WeakMap,this.output=e}onTrackClose(e){}validateAndNormalizeTimestamp(e,t,n){t+=e.source._timestampOffset;let r=this.trackTimestampInfo.get(e);if(!r){if(!n)throw Error(`First packet must be a key packet.`);r={maxTimestamp:t,maxTimestampBeforeLastKeyPacket:t},this.trackTimestampInfo.set(e,r)}if(t<0)throw Error(`Timestamps must be non-negative (got ${t}s).`);if(n&&(r.maxTimestampBeforeLastKeyPacket=r.maxTimestamp),t<r.maxTimestampBeforeLastKeyPacket)throw Error(`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key packet and ends right before the next key packet). Got ${t}s, but largest timestamp is ${r.maxTimestampBeforeLastKeyPacket}s.`);return r.maxTimestamp=Math.max(r.maxTimestamp,t),t}},Yt;(function(e){e[e.IDR=5]=`IDR`,e[e.SEI=6]=`SEI`,e[e.SPS=7]=`SPS`,e[e.PPS=8]=`PPS`,e[e.SPS_EXT=13]=`SPS_EXT`})(Yt||={});var P;(function(e){e[e.RASL_N=8]=`RASL_N`,e[e.RASL_R=9]=`RASL_R`,e[e.BLA_W_LP=16]=`BLA_W_LP`,e[e.RSV_IRAP_VCL23=23]=`RSV_IRAP_VCL23`,e[e.VPS_NUT=32]=`VPS_NUT`,e[e.SPS_NUT=33]=`SPS_NUT`,e[e.PPS_NUT=34]=`PPS_NUT`,e[e.PREFIX_SEI_NUT=39]=`PREFIX_SEI_NUT`,e[e.SUFFIX_SEI_NUT=40]=`SUFFIX_SEI_NUT`})(P||={});let Xt=e=>{let t=[],n=0;for(;n<e.length;){let r=-1,i=0;for(let t=n;t<e.length-3;t++){if(e[t]===0&&e[t+1]===0&&e[t+2]===1){r=t,i=3;break}if(t<e.length-4&&e[t]===0&&e[t+1]===0&&e[t+2]===0&&e[t+3]===1){r=t,i=4;break}}if(r===-1)break;if(n>0&&r>n){let i=e.subarray(n,r);i.length>0&&t.push(i)}n=r+i}if(n<e.length){let r=e.subarray(n);r.length>0&&t.push(r)}return t},Zt=(e,t)=>{let n=[],r=0,i=new DataView(e.buffer,e.byteOffset,e.byteLength);for(;r+t<=e.length;){let a;t===1?a=i.getUint8(r):t===2?a=i.getUint16(r,!1):t===3?a=We(i,r,!1):t===4?a=i.getUint32(r,!1):(Ue(t),x(!1)),r+=t;let o=e.subarray(r,r+a);n.push(o),r+=a}return n},Qt=e=>{let t=[],n=e.length;for(let r=0;r<n;r++)r+2<n&&e[r]===0&&e[r+1]===0&&e[r+2]===3?(t.push(0,0),r+=2):t.push(e[r]);return new Uint8Array(t)},$t=e=>{let t=Xt(e);if(t.length===0)return null;let n=0;for(let e of t)n+=4+e.byteLength;let r=new Uint8Array(n),i=new DataView(r.buffer),a=0;for(let e of t){let t=e.byteLength;i.setUint32(a,t,!1),a+=4,r.set(e,a),a+=e.byteLength}return r},en=(e,t)=>t.description?Zt(e,(T(t.description)[4]&3)+1):Xt(e),tn=e=>e[0]&31,nn=e=>{try{let t=Xt(e),n=t.filter(e=>tn(e)===Yt.SPS),r=t.filter(e=>tn(e)===Yt.PPS),i=t.filter(e=>tn(e)===Yt.SPS_EXT);if(n.length===0||r.length===0)return null;let a=n[0],o=on(a);x(o!==null);let s=o.profileIdc===100||o.profileIdc===110||o.profileIdc===122||o.profileIdc===144;return{configurationVersion:1,avcProfileIndication:o.profileIdc,profileCompatibility:o.constraintFlags,avcLevelIndication:o.levelIdc,lengthSizeMinusOne:3,sequenceParameterSets:n,pictureParameterSets:r,chromaFormat:s?o.chromaFormatIdc:null,bitDepthLumaMinus8:s?o.bitDepthLumaMinus8:null,bitDepthChromaMinus8:s?o.bitDepthChromaMinus8:null,sequenceParameterSetExt:s?i:null}}catch(e){return console.error(`Error building AVC Decoder Configuration Record:`,e),null}},rn=e=>{let t=[];t.push(e.configurationVersion),t.push(e.avcProfileIndication),t.push(e.profileCompatibility),t.push(e.avcLevelIndication),t.push(252|e.lengthSizeMinusOne&3),t.push(224|e.sequenceParameterSets.length&31);for(let n of e.sequenceParameterSets){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}t.push(e.pictureParameterSets.length);for(let n of e.pictureParameterSets){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}if(e.avcProfileIndication===100||e.avcProfileIndication===110||e.avcProfileIndication===122||e.avcProfileIndication===144){x(e.chromaFormat!==null),x(e.bitDepthLumaMinus8!==null),x(e.bitDepthChromaMinus8!==null),x(e.sequenceParameterSetExt!==null),t.push(252|e.chromaFormat&3),t.push(248|e.bitDepthLumaMinus8&7),t.push(248|e.bitDepthChromaMinus8&7),t.push(e.sequenceParameterSetExt.length);for(let n of e.sequenceParameterSetExt){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}}return new Uint8Array(t)},an=e=>{try{let t=E(e),n=0,r=t.getUint8(n++),i=t.getUint8(n++),a=t.getUint8(n++),o=t.getUint8(n++),s=t.getUint8(n++)&3,c=t.getUint8(n++)&31,l=[];for(let r=0;r<c;r++){let r=t.getUint16(n,!1);n+=2,l.push(e.subarray(n,n+r)),n+=r}let u=t.getUint8(n++),d=[];for(let r=0;r<u;r++){let r=t.getUint16(n,!1);n+=2,d.push(e.subarray(n,n+r)),n+=r}let f={configurationVersion:r,avcProfileIndication:i,profileCompatibility:a,avcLevelIndication:o,lengthSizeMinusOne:s,sequenceParameterSets:l,pictureParameterSets:d,chromaFormat:null,bitDepthLumaMinus8:null,bitDepthChromaMinus8:null,sequenceParameterSetExt:null};if((i===100||i===110||i===122||i===144)&&n+4<=e.length){let r=t.getUint8(n++)&3,i=t.getUint8(n++)&7,a=t.getUint8(n++)&7,o=t.getUint8(n++);f.chromaFormat=r,f.bitDepthLumaMinus8=i,f.bitDepthChromaMinus8=a;let s=[];for(let r=0;r<o;r++){let r=t.getUint16(n,!1);n+=2,s.push(e.subarray(n,n+r)),n+=r}f.sequenceParameterSetExt=s}return f}catch(e){return console.error(`Error deserializing AVC Decoder Configuration Record:`,e),null}},on=e=>{try{let t=new C(Qt(e));if(t.skipBits(1),t.skipBits(2),t.readBits(5)!==7)return null;let n=t.readAlignedByte(),r=t.readAlignedByte(),i=t.readAlignedByte();w(t);let a=null,o=null,s=null;if((n===100||n===110||n===122||n===244||n===44||n===83||n===86||n===118||n===128)&&(a=w(t),a===3&&t.skipBits(1),o=w(t),s=w(t),t.skipBits(1),t.readBits(1))){for(let e=0;e<(a===3?12:8);e++)if(t.readBits(1)){let n=e<6?16:64,r=8,i=8;for(let e=0;e<n;e++){if(i!==0){let e=we(t);i=(r+e+256)%256}r=i===0?r:i}}}w(t);let c=w(t);if(c===0)w(t);else if(c===1){t.skipBits(1),we(t),we(t);let e=w(t);for(let n=0;n<e;n++)we(t)}return w(t),t.skipBits(1),w(t),w(t),{profileIdc:n,constraintFlags:r,levelIdc:i,frameMbsOnlyFlag:t.readBits(1),chromaFormatIdc:a,bitDepthLumaMinus8:o,bitDepthChromaMinus8:s}}catch(e){return console.error(`Error parsing AVC SPS:`,e),null}},sn=(e,t)=>t.description?Zt(e,(T(t.description)[21]&3)+1):Xt(e),cn=e=>e[0]>>1&63,ln=e=>{try{let t=Xt(e),n=t.filter(e=>cn(e)===P.VPS_NUT),r=t.filter(e=>cn(e)===P.SPS_NUT),i=t.filter(e=>cn(e)===P.PPS_NUT),a=t.filter(e=>cn(e)===P.PREFIX_SEI_NUT||cn(e)===P.SUFFIX_SEI_NUT);if(r.length===0||i.length===0)return null;let o=r[0],s=new C(Qt(o));s.skipBits(16),s.readBits(4);let c=s.readBits(3),l=s.readBits(1),{general_profile_space:u,general_tier_flag:d,general_profile_idc:f,general_profile_compatibility_flags:p,general_constraint_indicator_flags:m,general_level_idc:h}=un(s,c);w(s);let g=w(s);g===3&&s.skipBits(1),w(s),w(s),s.readBits(1)&&(w(s),w(s),w(s),w(s));let _=w(s),v=w(s);w(s);let y=s.readBits(1)?0:c;for(let e=y;e<=c;e++)w(s),w(s),w(s);if(w(s),w(s),w(s),w(s),w(s),w(s),s.readBits(1)&&s.readBits(1)&&dn(s),s.skipBits(1),s.skipBits(1),s.readBits(1)&&(s.skipBits(4),s.skipBits(4),w(s),w(s),s.skipBits(1)),fn(s,w(s)),s.readBits(1)){let e=w(s);for(let t=0;t<e;t++)w(s),s.skipBits(1)}s.skipBits(1),s.skipBits(1);let ee=0;s.readBits(1)&&(ee=mn(s,c));let te=0;if(i.length>0){let e=i[0],t=new C(Qt(e));t.skipBits(16),w(t),w(t),t.skipBits(1),t.skipBits(1),t.skipBits(3),t.skipBits(1),t.skipBits(1),w(t),w(t),we(t),t.skipBits(1),t.skipBits(1),t.readBits(1)&&w(t),we(t),we(t),t.skipBits(1),t.skipBits(1),t.skipBits(1),t.skipBits(1);let n=t.readBits(1),r=t.readBits(1);te=!n&&!r?0:n&&!r?2:!n&&r?3:0}let ne=[...n.length?[{arrayCompleteness:1,nalUnitType:P.VPS_NUT,nalUnits:n}]:[],...r.length?[{arrayCompleteness:1,nalUnitType:P.SPS_NUT,nalUnits:r}]:[],...i.length?[{arrayCompleteness:1,nalUnitType:P.PPS_NUT,nalUnits:i}]:[],...a.length?[{arrayCompleteness:1,nalUnitType:cn(a[0]),nalUnits:a}]:[]];return{configurationVersion:1,generalProfileSpace:u,generalTierFlag:d,generalProfileIdc:f,generalProfileCompatibilityFlags:p,generalConstraintIndicatorFlags:m,generalLevelIdc:h,minSpatialSegmentationIdc:ee,parallelismType:te,chromaFormatIdc:g,bitDepthLumaMinus8:_,bitDepthChromaMinus8:v,avgFrameRate:0,constantFrameRate:0,numTemporalLayers:c+1,temporalIdNested:l,lengthSizeMinusOne:3,arrays:ne}}catch(e){return console.error(`Error building HEVC Decoder Configuration Record:`,e),null}},un=(e,t)=>{let n=e.readBits(2),r=e.readBits(1),i=e.readBits(5),a=0;for(let t=0;t<32;t++)a=a<<1|e.readBits(1);let o=new Uint8Array(6);for(let t=0;t<6;t++)o[t]=e.readBits(8);let s=e.readBits(8),c=[],l=[];for(let n=0;n<t;n++)c.push(e.readBits(1)),l.push(e.readBits(1));if(t>0)for(let n=t;n<8;n++)e.skipBits(2);for(let n=0;n<t;n++)c[n]&&e.skipBits(88),l[n]&&e.skipBits(8);return{general_profile_space:n,general_tier_flag:r,general_profile_idc:i,general_profile_compatibility_flags:a,general_constraint_indicator_flags:o,general_level_idc:s}},dn=e=>{for(let t=0;t<4;t++)for(let n=0;n<(t===3?2:6);n++)if(!e.readBits(1))w(e);else{let n=Math.min(64,1<<4+(t<<1));t>1&&we(e);for(let t=0;t<n;t++)we(e)}},fn=(e,t)=>{let n=[];for(let r=0;r<t;r++)n[r]=pn(e,r,t,n)},pn=(e,t,n,r)=>{let i=0,a=0,o=0;if(t!==0&&(a=e.readBits(1)),a){o=t===n?t-(w(e)+1):t-1,e.readBits(1),w(e);let a=r[o]??0;for(let t=0;t<=a;t++)e.readBits(1)||e.readBits(1);i=r[o]}else{let t=w(e),n=w(e);for(let n=0;n<t;n++)w(e),e.readBits(1);for(let t=0;t<n;t++)w(e),e.readBits(1);i=t+n}return i},mn=(e,t)=>{if(e.readBits(1)&&e.readBits(8)===255&&(e.readBits(16),e.readBits(16)),e.readBits(1)&&e.readBits(1),e.readBits(1)&&(e.readBits(3),e.readBits(1),e.readBits(1)&&(e.readBits(8),e.readBits(8),e.readBits(8))),e.readBits(1)&&(w(e),w(e)),e.readBits(1),e.readBits(1),e.readBits(1),e.readBits(1)&&(w(e),w(e),w(e),w(e)),e.readBits(1)&&(e.readBits(32),e.readBits(32),e.readBits(1)&&w(e),e.readBits(1)&&hn(e,!0,t)),e.readBits(1)){e.readBits(1),e.readBits(1),e.readBits(1);let t=w(e);return w(e),w(e),w(e),w(e),t}return 0},hn=(e,t,n)=>{let r=!1,i=!1,a=!1;t&&(r=e.readBits(1)===1,i=e.readBits(1)===1,(r||i)&&(a=e.readBits(1)===1,a&&(e.readBits(8),e.readBits(5),e.readBits(1),e.readBits(5)),e.readBits(4),e.readBits(4),a&&e.readBits(4),e.readBits(5),e.readBits(5),e.readBits(5)));for(let t=0;t<=n;t++){let t=e.readBits(1)===1,n=!0;t||(n=e.readBits(1)===1);let o=!1;n?w(e):o=e.readBits(1)===1;let s=1;o||(s=w(e)+1),r&&gn(e,s,a),i&&gn(e,s,a)}},gn=(e,t,n)=>{for(let r=0;r<t;r++)w(e),w(e),n&&(w(e),w(e)),e.readBits(1)},_n=e=>{let t=[];t.push(e.configurationVersion),t.push((e.generalProfileSpace&3)<<6|(e.generalTierFlag&1)<<5|e.generalProfileIdc&31),t.push(e.generalProfileCompatibilityFlags>>>24&255),t.push(e.generalProfileCompatibilityFlags>>>16&255),t.push(e.generalProfileCompatibilityFlags>>>8&255),t.push(e.generalProfileCompatibilityFlags&255),t.push(...e.generalConstraintIndicatorFlags),t.push(e.generalLevelIdc&255),t.push(240|e.minSpatialSegmentationIdc>>8&15),t.push(e.minSpatialSegmentationIdc&255),t.push(252|e.parallelismType&3),t.push(252|e.chromaFormatIdc&3),t.push(248|e.bitDepthLumaMinus8&7),t.push(248|e.bitDepthChromaMinus8&7),t.push(e.avgFrameRate>>8&255),t.push(e.avgFrameRate&255),t.push((e.constantFrameRate&3)<<6|(e.numTemporalLayers&7)<<3|(e.temporalIdNested&1)<<2|e.lengthSizeMinusOne&3),t.push(e.arrays.length&255);for(let n of e.arrays){t.push((n.arrayCompleteness&1)<<7|0|n.nalUnitType&63),t.push(n.nalUnits.length>>8&255),t.push(n.nalUnits.length&255);for(let e of n.nalUnits){t.push(e.length>>8&255),t.push(e.length&255);for(let n=0;n<e.length;n++)t.push(e[n])}}return new Uint8Array(t)},vn=e=>{let t=new C(e);if(t.readBits(2)!==2)return null;let n=t.readBits(1),r=(t.readBits(1)<<1)+n;if(r===3&&t.skipBits(1),t.readBits(1)===1||t.readBits(1)!==0||(t.skipBits(2),t.readBits(24)!==4817730))return null;let i=8;r>=2&&(i=t.readBits(1)?12:10);let a=t.readBits(3),o=0,s=0;if(a!==7)if(s=t.readBits(1),r===1||r===3){let e=t.readBits(1),n=t.readBits(1);o=!e&&!n?3:e&&!n?2:1,t.skipBits(1)}else o=1;else o=3,s=1;let c=t.readBits(16),l=t.readBits(16),u=(c+1)*(l+1),d=S(Ct).level;for(let e of Ct)if(u<=e.maxPictureSize){d=e.level;break}return{profile:r,level:d,bitDepth:i,chromaSubsampling:o,videoFullRangeFlag:s,colourPrimaries:a===2?1:a===1?6:2,transferCharacteristics:a===2?1:a===1?6:2,matrixCoefficients:a===7?0:a===2?1:a===1?6:2}},yn=function*(e){let t=new C(e),n=()=>{let e=0;for(let n=0;n<8;n++){let r=t.readAlignedByte();if(e|=(r&127)<<n*7,!(r&128))break;if(n===7&&r&128)return null}return e>=2**32-1?null:e};for(;t.getBitsLeft()>=8;){t.skipBits(1);let r=t.readBits(4),i=t.readBits(1),a=t.readBits(1);t.skipBits(1),i&&t.skipBits(8);let o;if(a){let e=n();if(e===null)return;o=e}else o=Math.floor(t.getBitsLeft()/8);x(t.pos%8==0),yield{type:r,data:e.subarray(t.pos/8,t.pos/8+o)},t.skipBits(o*8)}},bn=e=>{for(let{type:t,data:n}of yn(e)){if(t!==1)continue;let e=new C(n),r=e.readBits(3);e.readBits(1);let i=e.readBits(1),a=0,o=0,s=0;if(i)a=e.readBits(5);else{if(e.readBits(1)&&(e.skipBits(32),e.skipBits(32),e.readBits(1)))return null;let t=e.readBits(1);t&&(s=e.readBits(5),e.skipBits(32),e.skipBits(5),e.skipBits(5));let n=e.readBits(5);for(let r=0;r<=n;r++){e.skipBits(12);let n=e.readBits(5);if(r===0&&(a=n),n>7){let t=e.readBits(1);r===0&&(o=t)}if(t&&e.readBits(1)){let t=s+1;e.skipBits(t),e.skipBits(t),e.skipBits(1)}e.readBits(1)&&e.skipBits(4)}}let c=e.readBits(4),l=e.readBits(4),u=c+1;e.skipBits(u);let d=l+1;e.skipBits(d);let f=0;if(f=i?0:e.readBits(1),f&&(e.skipBits(4),e.skipBits(3)),e.skipBits(1),e.skipBits(1),e.skipBits(1),!i){e.skipBits(1),e.skipBits(1),e.skipBits(1),e.skipBits(1);let t=e.readBits(1);t&&(e.skipBits(1),e.skipBits(1));let n=e.readBits(1),r=0;r=n?2:e.readBits(1),r>0&&(e.readBits(1)||e.skipBits(1)),t&&e.skipBits(3)}e.skipBits(1),e.skipBits(1),e.skipBits(1);let p=e.readBits(1),m=8;r===2&&p?m=e.readBits(1)?12:10:r<=2&&(m=p?10:8);let h=0;r!==1&&(h=e.readBits(1));let g=1,_=1,v=0;return h||(r===0?(g=1,_=1):r===1?(g=0,_=0):m===12&&(g=e.readBits(1),g&&(_=e.readBits(1))),g&&_&&(v=e.readBits(2))),{profile:r,level:a,tier:o,bitDepth:m,monochrome:h,chromaSubsamplingX:g,chromaSubsamplingY:_,chromaSamplePosition:v}}return null},xn=e=>{let t=E(e),n=t.getUint8(9),r=t.getUint16(10,!0),i=t.getUint32(12,!0),a=t.getInt16(16,!0),o=t.getUint8(18),s=null;return o&&(s=e.subarray(19,21+n)),{outputChannelCount:n,preSkip:r,inputSampleRate:i,outputGain:a,channelMappingFamily:o,channelMappingTable:s}},Sn=(e,t,n)=>{switch(e){case`avc`:{let e=en(n,t),r=e.some(e=>tn(e)===Yt.IDR);if(!r&&(!st()||lt()>=144))for(let t of e){if(tn(t)!==Yt.SEI)continue;let e=Qt(t),n=1;do{let t=0;for(;;){let r=e[n++];if(r===void 0||(t+=r,r<255))break}let i=0;for(;;){let t=e[n++];if(t===void 0||(i+=t,t<255))break}if(t===6){let t=new C(e);t.pos=8*n;let i=w(t),a=t.readBits(1);if(i===0&&a===1){r=!0;break}}n+=i}while(n<e.length-1)}return r?`key`:`delta`}case`hevc`:return sn(n,t).some(e=>{let t=cn(e);return P.BLA_W_LP<=t&&t<=P.RSV_IRAP_VCL23})?`key`:`delta`;case`vp8`:return n[0]&1?`delta`:`key`;case`vp9`:{let e=new C(n);if(e.readBits(2)!==2)return null;let t=e.readBits(1);return(e.readBits(1)<<1)+t===3&&e.skipBits(1),e.readBits(1)?null:e.readBits(1)===0?`key`:`delta`}case`av1`:{let e=!1;for(let{type:t,data:r}of yn(n))if(t===1){let t=new C(r);t.skipBits(4),e=!!t.readBits(1)}else if(t===3||t===6||t===7){if(e)return`key`;let t=new C(r);return t.readBits(1)?null:t.readBits(2)===0?`key`:`delta`}return null}default:Ue(e),x(!1)}};var Cn;(function(e){e[e.STREAMINFO=0]=`STREAMINFO`,e[e.VORBIS_COMMENT=4]=`VORBIS_COMMENT`,e[e.PICTURE=6]=`PICTURE`})(Cn||={});
|
|
25
|
+
var Xt=class{constructor(e){this.mutex=new Le,this.firstMediaStreamTimestamp=null,this.trackTimestampInfo=new WeakMap,this.output=e}onTrackClose(e){}validateAndNormalizeTimestamp(e,t,n){t+=e.source._timestampOffset;let r=this.trackTimestampInfo.get(e);if(!r){if(!n)throw Error(`First packet must be a key packet.`);r={maxTimestamp:t,maxTimestampBeforeLastKeyPacket:t},this.trackTimestampInfo.set(e,r)}if(t<0)throw Error(`Timestamps must be non-negative (got ${t}s).`);if(n&&(r.maxTimestampBeforeLastKeyPacket=r.maxTimestamp),t<r.maxTimestampBeforeLastKeyPacket)throw Error(`Timestamps cannot be smaller than the largest timestamp of the previous GOP (a GOP begins with a key packet and ends right before the next key packet). Got ${t}s, but largest timestamp is ${r.maxTimestampBeforeLastKeyPacket}s.`);return r.maxTimestamp=Math.max(r.maxTimestamp,t),t}},Zt;(function(e){e[e.IDR=5]=`IDR`,e[e.SEI=6]=`SEI`,e[e.SPS=7]=`SPS`,e[e.PPS=8]=`PPS`,e[e.SPS_EXT=13]=`SPS_EXT`})(Zt||={});var N;(function(e){e[e.RASL_N=8]=`RASL_N`,e[e.RASL_R=9]=`RASL_R`,e[e.BLA_W_LP=16]=`BLA_W_LP`,e[e.RSV_IRAP_VCL23=23]=`RSV_IRAP_VCL23`,e[e.VPS_NUT=32]=`VPS_NUT`,e[e.SPS_NUT=33]=`SPS_NUT`,e[e.PPS_NUT=34]=`PPS_NUT`,e[e.PREFIX_SEI_NUT=39]=`PREFIX_SEI_NUT`,e[e.SUFFIX_SEI_NUT=40]=`SUFFIX_SEI_NUT`})(N||={});let Qt=e=>{let t=[],n=0;for(;n<e.length;){let r=-1,i=0;for(let t=n;t<e.length-3;t++){if(e[t]===0&&e[t+1]===0&&e[t+2]===1){r=t,i=3;break}if(t<e.length-4&&e[t]===0&&e[t+1]===0&&e[t+2]===0&&e[t+3]===1){r=t,i=4;break}}if(r===-1)break;if(n>0&&r>n){let i=e.subarray(n,r);i.length>0&&t.push(i)}n=r+i}if(n<e.length){let r=e.subarray(n);r.length>0&&t.push(r)}return t},$t=(e,t)=>{let n=[],r=0,i=new DataView(e.buffer,e.byteOffset,e.byteLength);for(;r+t<=e.length;){let a;t===1?a=i.getUint8(r):t===2?a=i.getUint16(r,!1):t===3?a=Ke(i,r,!1):t===4?a=i.getUint32(r,!1):(Ge(t),b(!1)),r+=t;let o=e.subarray(r,r+a);n.push(o),r+=a}return n},en=e=>{let t=[],n=e.length;for(let r=0;r<n;r++)r+2<n&&e[r]===0&&e[r+1]===0&&e[r+2]===3?(t.push(0,0),r+=2):t.push(e[r]);return new Uint8Array(t)},tn=e=>{let t=Qt(e);if(t.length===0)return null;let n=0;for(let e of t)n+=4+e.byteLength;let r=new Uint8Array(n),i=new DataView(r.buffer),a=0;for(let e of t){let t=e.byteLength;i.setUint32(a,t,!1),a+=4,r.set(e,a),a+=e.byteLength}return r},nn=(e,t)=>t.description?$t(e,(w(t.description)[4]&3)+1):Qt(e),rn=e=>e[0]&31,an=e=>{try{let t=Qt(e),n=t.filter(e=>rn(e)===Zt.SPS),r=t.filter(e=>rn(e)===Zt.PPS),i=t.filter(e=>rn(e)===Zt.SPS_EXT);if(n.length===0||r.length===0)return null;let a=n[0],o=cn(a);b(o!==null);let s=o.profileIdc===100||o.profileIdc===110||o.profileIdc===122||o.profileIdc===144;return{configurationVersion:1,avcProfileIndication:o.profileIdc,profileCompatibility:o.constraintFlags,avcLevelIndication:o.levelIdc,lengthSizeMinusOne:3,sequenceParameterSets:n,pictureParameterSets:r,chromaFormat:s?o.chromaFormatIdc:null,bitDepthLumaMinus8:s?o.bitDepthLumaMinus8:null,bitDepthChromaMinus8:s?o.bitDepthChromaMinus8:null,sequenceParameterSetExt:s?i:null}}catch(e){return console.error(`Error building AVC Decoder Configuration Record:`,e),null}},on=e=>{let t=[];t.push(e.configurationVersion),t.push(e.avcProfileIndication),t.push(e.profileCompatibility),t.push(e.avcLevelIndication),t.push(252|e.lengthSizeMinusOne&3),t.push(224|e.sequenceParameterSets.length&31);for(let n of e.sequenceParameterSets){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}t.push(e.pictureParameterSets.length);for(let n of e.pictureParameterSets){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}if(e.avcProfileIndication===100||e.avcProfileIndication===110||e.avcProfileIndication===122||e.avcProfileIndication===144){b(e.chromaFormat!==null),b(e.bitDepthLumaMinus8!==null),b(e.bitDepthChromaMinus8!==null),b(e.sequenceParameterSetExt!==null),t.push(252|e.chromaFormat&3),t.push(248|e.bitDepthLumaMinus8&7),t.push(248|e.bitDepthChromaMinus8&7),t.push(e.sequenceParameterSetExt.length);for(let n of e.sequenceParameterSetExt){let e=n.byteLength;t.push(e>>8),t.push(e&255);for(let r=0;r<e;r++)t.push(n[r])}}return new Uint8Array(t)},sn=e=>{try{let t=T(e),n=0,r=t.getUint8(n++),i=t.getUint8(n++),a=t.getUint8(n++),o=t.getUint8(n++),s=t.getUint8(n++)&3,c=t.getUint8(n++)&31,l=[];for(let r=0;r<c;r++){let r=t.getUint16(n,!1);n+=2,l.push(e.subarray(n,n+r)),n+=r}let u=t.getUint8(n++),d=[];for(let r=0;r<u;r++){let r=t.getUint16(n,!1);n+=2,d.push(e.subarray(n,n+r)),n+=r}let f={configurationVersion:r,avcProfileIndication:i,profileCompatibility:a,avcLevelIndication:o,lengthSizeMinusOne:s,sequenceParameterSets:l,pictureParameterSets:d,chromaFormat:null,bitDepthLumaMinus8:null,bitDepthChromaMinus8:null,sequenceParameterSetExt:null};if((i===100||i===110||i===122||i===144)&&n+4<=e.length){let r=t.getUint8(n++)&3,i=t.getUint8(n++)&7,a=t.getUint8(n++)&7,o=t.getUint8(n++);f.chromaFormat=r,f.bitDepthLumaMinus8=i,f.bitDepthChromaMinus8=a;let s=[];for(let r=0;r<o;r++){let r=t.getUint16(n,!1);n+=2,s.push(e.subarray(n,n+r)),n+=r}f.sequenceParameterSetExt=s}return f}catch(e){return console.error(`Error deserializing AVC Decoder Configuration Record:`,e),null}},cn=e=>{try{let t=new S(en(e));if(t.skipBits(1),t.skipBits(2),t.readBits(5)!==7)return null;let n=t.readAlignedByte(),r=t.readAlignedByte(),i=t.readAlignedByte();C(t);let a=null,o=null,s=null;if((n===100||n===110||n===122||n===244||n===44||n===83||n===86||n===118||n===128)&&(a=C(t),a===3&&t.skipBits(1),o=C(t),s=C(t),t.skipBits(1),t.readBits(1))){for(let e=0;e<(a===3?12:8);e++)if(t.readBits(1)){let n=e<6?16:64,r=8,i=8;for(let e=0;e<n;e++){if(i!==0){let e=Ee(t);i=(r+e+256)%256}r=i===0?r:i}}}C(t);let c=C(t);if(c===0)C(t);else if(c===1){t.skipBits(1),Ee(t),Ee(t);let e=C(t);for(let n=0;n<e;n++)Ee(t)}return C(t),t.skipBits(1),C(t),C(t),{profileIdc:n,constraintFlags:r,levelIdc:i,frameMbsOnlyFlag:t.readBits(1),chromaFormatIdc:a,bitDepthLumaMinus8:o,bitDepthChromaMinus8:s}}catch(e){return console.error(`Error parsing AVC SPS:`,e),null}},ln=(e,t)=>t.description?$t(e,(w(t.description)[21]&3)+1):Qt(e),un=e=>e[0]>>1&63,dn=e=>{try{let t=Qt(e),n=t.filter(e=>un(e)===N.VPS_NUT),r=t.filter(e=>un(e)===N.SPS_NUT),i=t.filter(e=>un(e)===N.PPS_NUT),a=t.filter(e=>un(e)===N.PREFIX_SEI_NUT||un(e)===N.SUFFIX_SEI_NUT);if(r.length===0||i.length===0)return null;let o=r[0],s=new S(en(o));s.skipBits(16),s.readBits(4);let c=s.readBits(3),l=s.readBits(1),{general_profile_space:u,general_tier_flag:d,general_profile_idc:f,general_profile_compatibility_flags:p,general_constraint_indicator_flags:m,general_level_idc:h}=fn(s,c);C(s);let g=C(s);g===3&&s.skipBits(1),C(s),C(s),s.readBits(1)&&(C(s),C(s),C(s),C(s));let _=C(s),v=C(s);C(s);let ee=s.readBits(1)?0:c;for(let e=ee;e<=c;e++)C(s),C(s),C(s);if(C(s),C(s),C(s),C(s),C(s),C(s),s.readBits(1)&&s.readBits(1)&&pn(s),s.skipBits(1),s.skipBits(1),s.readBits(1)&&(s.skipBits(4),s.skipBits(4),C(s),C(s),s.skipBits(1)),mn(s,C(s)),s.readBits(1)){let e=C(s);for(let t=0;t<e;t++)C(s),s.skipBits(1)}s.skipBits(1),s.skipBits(1);let te=0;s.readBits(1)&&(te=gn(s,c));let ne=0;if(i.length>0){let e=i[0],t=new S(en(e));t.skipBits(16),C(t),C(t),t.skipBits(1),t.skipBits(1),t.skipBits(3),t.skipBits(1),t.skipBits(1),C(t),C(t),Ee(t),t.skipBits(1),t.skipBits(1),t.readBits(1)&&C(t),Ee(t),Ee(t),t.skipBits(1),t.skipBits(1),t.skipBits(1),t.skipBits(1);let n=t.readBits(1),r=t.readBits(1);ne=!n&&!r?0:n&&!r?2:!n&&r?3:0}let re=[...n.length?[{arrayCompleteness:1,nalUnitType:N.VPS_NUT,nalUnits:n}]:[],...r.length?[{arrayCompleteness:1,nalUnitType:N.SPS_NUT,nalUnits:r}]:[],...i.length?[{arrayCompleteness:1,nalUnitType:N.PPS_NUT,nalUnits:i}]:[],...a.length?[{arrayCompleteness:1,nalUnitType:un(a[0]),nalUnits:a}]:[]];return{configurationVersion:1,generalProfileSpace:u,generalTierFlag:d,generalProfileIdc:f,generalProfileCompatibilityFlags:p,generalConstraintIndicatorFlags:m,generalLevelIdc:h,minSpatialSegmentationIdc:te,parallelismType:ne,chromaFormatIdc:g,bitDepthLumaMinus8:_,bitDepthChromaMinus8:v,avgFrameRate:0,constantFrameRate:0,numTemporalLayers:c+1,temporalIdNested:l,lengthSizeMinusOne:3,arrays:re}}catch(e){return console.error(`Error building HEVC Decoder Configuration Record:`,e),null}},fn=(e,t)=>{let n=e.readBits(2),r=e.readBits(1),i=e.readBits(5),a=0;for(let t=0;t<32;t++)a=a<<1|e.readBits(1);let o=new Uint8Array(6);for(let t=0;t<6;t++)o[t]=e.readBits(8);let s=e.readBits(8),c=[],l=[];for(let n=0;n<t;n++)c.push(e.readBits(1)),l.push(e.readBits(1));if(t>0)for(let n=t;n<8;n++)e.skipBits(2);for(let n=0;n<t;n++)c[n]&&e.skipBits(88),l[n]&&e.skipBits(8);return{general_profile_space:n,general_tier_flag:r,general_profile_idc:i,general_profile_compatibility_flags:a,general_constraint_indicator_flags:o,general_level_idc:s}},pn=e=>{for(let t=0;t<4;t++)for(let n=0;n<(t===3?2:6);n++)if(!e.readBits(1))C(e);else{let n=Math.min(64,1<<4+(t<<1));t>1&&Ee(e);for(let t=0;t<n;t++)Ee(e)}},mn=(e,t)=>{let n=[];for(let r=0;r<t;r++)n[r]=hn(e,r,t,n)},hn=(e,t,n,r)=>{let i=0,a=0,o=0;if(t!==0&&(a=e.readBits(1)),a){o=t===n?t-(C(e)+1):t-1,e.readBits(1),C(e);let a=r[o]??0;for(let t=0;t<=a;t++)e.readBits(1)||e.readBits(1);i=r[o]}else{let t=C(e),n=C(e);for(let n=0;n<t;n++)C(e),e.readBits(1);for(let t=0;t<n;t++)C(e),e.readBits(1);i=t+n}return i},gn=(e,t)=>{if(e.readBits(1)&&e.readBits(8)===255&&(e.readBits(16),e.readBits(16)),e.readBits(1)&&e.readBits(1),e.readBits(1)&&(e.readBits(3),e.readBits(1),e.readBits(1)&&(e.readBits(8),e.readBits(8),e.readBits(8))),e.readBits(1)&&(C(e),C(e)),e.readBits(1),e.readBits(1),e.readBits(1),e.readBits(1)&&(C(e),C(e),C(e),C(e)),e.readBits(1)&&(e.readBits(32),e.readBits(32),e.readBits(1)&&C(e),e.readBits(1)&&_n(e,!0,t)),e.readBits(1)){e.readBits(1),e.readBits(1),e.readBits(1);let t=C(e);return C(e),C(e),C(e),C(e),t}return 0},_n=(e,t,n)=>{let r=!1,i=!1,a=!1;t&&(r=e.readBits(1)===1,i=e.readBits(1)===1,(r||i)&&(a=e.readBits(1)===1,a&&(e.readBits(8),e.readBits(5),e.readBits(1),e.readBits(5)),e.readBits(4),e.readBits(4),a&&e.readBits(4),e.readBits(5),e.readBits(5),e.readBits(5)));for(let t=0;t<=n;t++){let t=e.readBits(1)===1,n=!0;t||(n=e.readBits(1)===1);let o=!1;n?C(e):o=e.readBits(1)===1;let s=1;o||(s=C(e)+1),r&&vn(e,s,a),i&&vn(e,s,a)}},vn=(e,t,n)=>{for(let r=0;r<t;r++)C(e),C(e),n&&(C(e),C(e)),e.readBits(1)},yn=e=>{let t=[];t.push(e.configurationVersion),t.push((e.generalProfileSpace&3)<<6|(e.generalTierFlag&1)<<5|e.generalProfileIdc&31),t.push(e.generalProfileCompatibilityFlags>>>24&255),t.push(e.generalProfileCompatibilityFlags>>>16&255),t.push(e.generalProfileCompatibilityFlags>>>8&255),t.push(e.generalProfileCompatibilityFlags&255),t.push(...e.generalConstraintIndicatorFlags),t.push(e.generalLevelIdc&255),t.push(240|e.minSpatialSegmentationIdc>>8&15),t.push(e.minSpatialSegmentationIdc&255),t.push(252|e.parallelismType&3),t.push(252|e.chromaFormatIdc&3),t.push(248|e.bitDepthLumaMinus8&7),t.push(248|e.bitDepthChromaMinus8&7),t.push(e.avgFrameRate>>8&255),t.push(e.avgFrameRate&255),t.push((e.constantFrameRate&3)<<6|(e.numTemporalLayers&7)<<3|(e.temporalIdNested&1)<<2|e.lengthSizeMinusOne&3),t.push(e.arrays.length&255);for(let n of e.arrays){t.push((n.arrayCompleteness&1)<<7|0|n.nalUnitType&63),t.push(n.nalUnits.length>>8&255),t.push(n.nalUnits.length&255);for(let e of n.nalUnits){t.push(e.length>>8&255),t.push(e.length&255);for(let n=0;n<e.length;n++)t.push(e[n])}}return new Uint8Array(t)},bn=e=>{let t=new S(e);if(t.readBits(2)!==2)return null;let n=t.readBits(1),r=(t.readBits(1)<<1)+n;if(r===3&&t.skipBits(1),t.readBits(1)===1||t.readBits(1)!==0||(t.skipBits(2),t.readBits(24)!==4817730))return null;let i=8;r>=2&&(i=t.readBits(1)?12:10);let a=t.readBits(3),o=0,s=0;if(a!==7)if(s=t.readBits(1),r===1||r===3){let e=t.readBits(1),n=t.readBits(1);o=!e&&!n?3:e&&!n?2:1,t.skipBits(1)}else o=1;else o=3,s=1;let c=t.readBits(16),l=t.readBits(16),u=(c+1)*(l+1),d=x(Tt).level;for(let e of Tt)if(u<=e.maxPictureSize){d=e.level;break}return{profile:r,level:d,bitDepth:i,chromaSubsampling:o,videoFullRangeFlag:s,colourPrimaries:a===2?1:a===1?6:2,transferCharacteristics:a===2?1:a===1?6:2,matrixCoefficients:a===7?0:a===2?1:a===1?6:2}},xn=function*(e){let t=new S(e),n=()=>{let e=0;for(let n=0;n<8;n++){let r=t.readAlignedByte();if(e|=(r&127)<<n*7,!(r&128))break;if(n===7&&r&128)return null}return e>=2**32-1?null:e};for(;t.getBitsLeft()>=8;){t.skipBits(1);let r=t.readBits(4),i=t.readBits(1),a=t.readBits(1);t.skipBits(1),i&&t.skipBits(8);let o;if(a){let e=n();if(e===null)return;o=e}else o=Math.floor(t.getBitsLeft()/8);b(t.pos%8==0),yield{type:r,data:e.subarray(t.pos/8,t.pos/8+o)},t.skipBits(o*8)}},Sn=e=>{for(let{type:t,data:n}of xn(e)){if(t!==1)continue;let e=new S(n),r=e.readBits(3);e.readBits(1);let i=e.readBits(1),a=0,o=0,s=0;if(i)a=e.readBits(5);else{if(e.readBits(1)&&(e.skipBits(32),e.skipBits(32),e.readBits(1)))return null;let t=e.readBits(1);t&&(s=e.readBits(5),e.skipBits(32),e.skipBits(5),e.skipBits(5));let n=e.readBits(5);for(let r=0;r<=n;r++){e.skipBits(12);let n=e.readBits(5);if(r===0&&(a=n),n>7){let t=e.readBits(1);r===0&&(o=t)}if(t&&e.readBits(1)){let t=s+1;e.skipBits(t),e.skipBits(t),e.skipBits(1)}e.readBits(1)&&e.skipBits(4)}}let c=e.readBits(4),l=e.readBits(4),u=c+1;e.skipBits(u);let d=l+1;e.skipBits(d);let f=0;if(f=i?0:e.readBits(1),f&&(e.skipBits(4),e.skipBits(3)),e.skipBits(1),e.skipBits(1),e.skipBits(1),!i){e.skipBits(1),e.skipBits(1),e.skipBits(1),e.skipBits(1);let t=e.readBits(1);t&&(e.skipBits(1),e.skipBits(1));let n=e.readBits(1),r=0;r=n?2:e.readBits(1),r>0&&(e.readBits(1)||e.skipBits(1)),t&&e.skipBits(3)}e.skipBits(1),e.skipBits(1),e.skipBits(1);let p=e.readBits(1),m=8;r===2&&p?m=e.readBits(1)?12:10:r<=2&&(m=p?10:8);let h=0;r!==1&&(h=e.readBits(1));let g=1,_=1,v=0;return h||(r===0?(g=1,_=1):r===1?(g=0,_=0):m===12&&(g=e.readBits(1),g&&(_=e.readBits(1))),g&&_&&(v=e.readBits(2))),{profile:r,level:a,tier:o,bitDepth:m,monochrome:h,chromaSubsamplingX:g,chromaSubsamplingY:_,chromaSamplePosition:v}}return null},Cn=e=>{let t=T(e),n=t.getUint8(9),r=t.getUint16(10,!0),i=t.getUint32(12,!0),a=t.getInt16(16,!0),o=t.getUint8(18),s=null;return o&&(s=e.subarray(19,21+n)),{outputChannelCount:n,preSkip:r,inputSampleRate:i,outputGain:a,channelMappingFamily:o,channelMappingTable:s}},wn=(e,t,n)=>{switch(e){case`avc`:{let e=nn(n,t),r=e.some(e=>rn(e)===Zt.IDR);if(!r&&(!lt()||dt()>=144))for(let t of e){if(rn(t)!==Zt.SEI)continue;let e=en(t),n=1;do{let t=0;for(;;){let r=e[n++];if(r===void 0||(t+=r,r<255))break}let i=0;for(;;){let t=e[n++];if(t===void 0||(i+=t,t<255))break}if(t===6){let t=new S(e);t.pos=8*n;let i=C(t),a=t.readBits(1);if(i===0&&a===1){r=!0;break}}n+=i}while(n<e.length-1)}return r?`key`:`delta`}case`hevc`:return ln(n,t).some(e=>{let t=un(e);return N.BLA_W_LP<=t&&t<=N.RSV_IRAP_VCL23})?`key`:`delta`;case`vp8`:return n[0]&1?`delta`:`key`;case`vp9`:{let e=new S(n);if(e.readBits(2)!==2)return null;let t=e.readBits(1);return(e.readBits(1)<<1)+t===3&&e.skipBits(1),e.readBits(1)?null:e.readBits(1)===0?`key`:`delta`}case`av1`:{let e=!1;for(let{type:t,data:r}of xn(n))if(t===1){let t=new S(r);t.skipBits(4),e=!!t.readBits(1)}else if(t===3||t===6||t===7){if(e)return`key`;let t=new S(r);return t.readBits(1)?null:t.readBits(2)===0?`key`:`delta`}return null}default:Ge(e),b(!1)}};var Tn;(function(e){e[e.STREAMINFO=0]=`STREAMINFO`,e[e.VORBIS_COMMENT=4]=`VORBIS_COMMENT`,e[e.PICTURE=6]=`PICTURE`})(Tn||={});
|
|
26
26
|
/*!
|
|
27
27
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
28
28
|
*
|
|
@@ -30,7 +30,7 @@ var Jt=class{constructor(e){this.mutex=new Fe,this.firstMediaStreamTimestamp=nul
|
|
|
30
30
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
31
31
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
32
32
|
*/
|
|
33
|
-
var
|
|
33
|
+
var En=class{constructor(e){this.input=e}};let Dn=[],On=[],kn=[],An=[],jn=new Uint8Array;var P=class e{constructor(e,t,n,r,i=-1,a,o){if(this.data=e,this.type=t,this.timestamp=n,this.duration=r,this.sequenceNumber=i,e===jn&&a===void 0)throw Error(`Internal error: byteLength must be explicitly provided when constructing metadata-only packets.`);if(a===void 0&&(a=e.byteLength),!(e instanceof Uint8Array))throw TypeError(`data must be a Uint8Array.`);if(t!==`key`&&t!==`delta`)throw TypeError(`type must be either "key" or "delta".`);if(!Number.isFinite(n))throw TypeError(`timestamp must be a number.`);if(!Number.isFinite(r)||r<0)throw TypeError(`duration must be a non-negative number.`);if(!Number.isFinite(i))throw TypeError(`sequenceNumber must be a number.`);if(!Number.isInteger(a)||a<0)throw TypeError(`byteLength must be a non-negative integer.`);if(o!==void 0&&(typeof o!=`object`||!o))throw TypeError(`sideData, when provided, must be an object.`);if(o?.alpha!==void 0&&!(o.alpha instanceof Uint8Array))throw TypeError(`sideData.alpha, when provided, must be a Uint8Array.`);if(o?.alphaByteLength!==void 0&&(!Number.isInteger(o.alphaByteLength)||o.alphaByteLength<0))throw TypeError(`sideData.alphaByteLength, when provided, must be a non-negative integer.`);this.byteLength=a,this.sideData=o??{},this.sideData.alpha&&this.sideData.alphaByteLength===void 0&&(this.sideData.alphaByteLength=this.sideData.alpha.byteLength)}get isMetadataOnly(){return this.data===jn}get microsecondTimestamp(){return Math.trunc(tt*this.timestamp)}get microsecondDuration(){return Math.trunc(tt*this.duration)}toEncodedVideoChunk(){if(this.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be converted to a video chunk.`);if(typeof EncodedVideoChunk>`u`)throw Error(`Your browser does not support EncodedVideoChunk.`);return new EncodedVideoChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}alphaToEncodedVideoChunk(e=this.type){if(!this.sideData.alpha)throw TypeError(`This packet does not contain alpha side data.`);if(this.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be converted to a video chunk.`);if(typeof EncodedVideoChunk>`u`)throw Error(`Your browser does not support EncodedVideoChunk.`);return new EncodedVideoChunk({data:this.sideData.alpha,type:e,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}toEncodedAudioChunk(){if(this.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be converted to an audio chunk.`);if(typeof EncodedAudioChunk>`u`)throw Error(`Your browser does not support EncodedAudioChunk.`);return new EncodedAudioChunk({data:this.data,type:this.type,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration})}static fromEncodedChunk(t,n){if(!(t instanceof EncodedVideoChunk||t instanceof EncodedAudioChunk))throw TypeError(`chunk must be an EncodedVideoChunk or EncodedAudioChunk.`);let r=new Uint8Array(t.byteLength);return t.copyTo(r),new e(r,t.type,t.timestamp/1e6,(t.duration??0)/1e6,void 0,void 0,n)}clone(t){if(t!==void 0&&(typeof t!=`object`||!t))throw TypeError(`options, when provided, must be an object.`);if(t?.timestamp!==void 0&&!Number.isFinite(t.timestamp))throw TypeError(`options.timestamp, when provided, must be a number.`);if(t?.duration!==void 0&&!Number.isFinite(t.duration))throw TypeError(`options.duration, when provided, must be a number.`);return new e(this.data,this.type,t?.timestamp??this.timestamp,t?.duration??this.duration,this.sequenceNumber,this.byteLength)}};
|
|
34
34
|
/*!
|
|
35
35
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
36
36
|
*
|
|
@@ -38,7 +38,7 @@ var wn=class{constructor(e){this.input=e}};let Tn=[],En=[],Dn=[],On=[],kn=new Ui
|
|
|
38
38
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
39
39
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
40
40
|
*/
|
|
41
|
-
let
|
|
41
|
+
let Mn=e=>{let t=8191,n=e,r=4096,i=0,a=12,o=0;for(n<0&&(n=-n,i=128),n+=33,n>t&&(n=t);(n&r)!==r&&a>=5;)r>>=1,a--;return o=n>>a-4&15,~(i|a-5<<4|o)&255},Nn=e=>{let t=0,n=0,r=~e;r&128&&(r&=-129,t=-1),n=((r&240)>>4)+5;let i=(1<<n|(r&15)<<n-4|1<<n-5)-33;return t===0?i:-i},Pn=e=>{let t=4095,n=2048,r=0,i=11,a=0,o=e;for(o<0&&(o=-o,r=128),o>t&&(o=t);(o&n)!==n&&i>=5;)n>>=1,i--;return a=o>>(i===4?1:i-4)&15,(r|i-4<<4|a)^85},Fn=e=>{let t=0,n=0,r=e^85;r&128&&(r&=-129,t=-1),n=((r&240)>>4)+4;let i=0;return i=n===4?r<<1|1:1<<n|(r&15)<<n-4|1<<n-5,t===0?i:-i};
|
|
42
42
|
/*!
|
|
43
43
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
44
44
|
*
|
|
@@ -46,7 +46,7 @@ let An=e=>{let t=8191,n=e,r=4096,i=0,a=12,o=0;for(n<0&&(n=-n,i=128),n+=33,n>t&&(
|
|
|
46
46
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
47
47
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
48
48
|
*/
|
|
49
|
-
ft();let Pn=-1/0,Fn=-1/0,In=null;typeof FinalizationRegistry<`u`&&(In=new FinalizationRegistry(e=>{let t=Date.now();e.type===`video`?(t-Pn>=1e3&&(console.error(`A VideoSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your VideoSamples as soon as you're done using them.`),Pn=t),typeof VideoFrame<`u`&&e.data instanceof VideoFrame&&e.data.close()):(t-Fn>=1e3&&(console.error(`An AudioSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your AudioSamples as soon as you're done using them.`),Fn=t),typeof AudioData<`u`&&e.data instanceof AudioData&&e.data.close())}));var Ln=class e{get displayWidth(){return this.rotation%180==0?this.codedWidth:this.codedHeight}get displayHeight(){return this.rotation%180==0?this.codedHeight:this.codedWidth}get microsecondTimestamp(){return Math.trunc($e*this.timestamp)}get microsecondDuration(){return Math.trunc($e*this.duration)}get hasAlpha(){return this.format&&this.format.includes(`A`)}constructor(t,n){if(this._closed=!1,t instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&t instanceof SharedArrayBuffer||ArrayBuffer.isView(t)){if(!n||typeof n!=`object`)throw TypeError(`init must be an object.`);if(!(`format`in n)||typeof n.format!=`string`)throw TypeError(`init.format must be a string.`);if(!Number.isInteger(n.codedWidth)||n.codedWidth<=0)throw TypeError(`init.codedWidth must be a positive integer.`);if(!Number.isInteger(n.codedHeight)||n.codedHeight<=0)throw TypeError(`init.codedHeight must be a positive integer.`);if(n.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(!Number.isFinite(n.timestamp))throw TypeError(`init.timestamp must be a number.`);if(n.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);this._data=T(t).slice(),this.format=n.format,this.codedWidth=n.codedWidth,this.codedHeight=n.codedHeight,this.rotation=n.rotation??0,this.timestamp=n.timestamp,this.duration=n.duration??0,this.colorSpace=new VideoColorSpace(n.colorSpace)}else if(typeof VideoFrame<`u`&&t instanceof VideoFrame){if(n?.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(n?.timestamp!==void 0&&!Number.isFinite(n?.timestamp))throw TypeError(`init.timestamp, when provided, must be a number.`);if(n?.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);this._data=t,this.format=t.format,this.codedWidth=t.displayWidth,this.codedHeight=t.displayHeight,this.rotation=n?.rotation??0,this.timestamp=n?.timestamp??t.timestamp/1e6,this.duration=n?.duration??(t.duration??0)/1e6,this.colorSpace=t.colorSpace}else if(typeof HTMLImageElement<`u`&&t instanceof HTMLImageElement||typeof SVGImageElement<`u`&&t instanceof SVGImageElement||typeof ImageBitmap<`u`&&t instanceof ImageBitmap||typeof HTMLVideoElement<`u`&&t instanceof HTMLVideoElement||typeof HTMLCanvasElement<`u`&&t instanceof HTMLCanvasElement||typeof OffscreenCanvas<`u`&&t instanceof OffscreenCanvas){if(!n||typeof n!=`object`)throw TypeError(`init must be an object.`);if(n.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(!Number.isFinite(n.timestamp))throw TypeError(`init.timestamp must be a number.`);if(n.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);if(typeof VideoFrame<`u`)return new e(new VideoFrame(t,{timestamp:Math.trunc(n.timestamp*$e),duration:Math.trunc((n.duration??0)*$e)||void 0}),n);let r=0,i=0;if(`naturalWidth`in t?(r=t.naturalWidth,i=t.naturalHeight):`videoWidth`in t?(r=t.videoWidth,i=t.videoHeight):`width`in t&&(r=Number(t.width),i=Number(t.height)),!r||!i)throw TypeError(`Could not determine dimensions.`);let a=new OffscreenCanvas(r,i),o=a.getContext(`2d`,{alpha:at(),willReadFrequently:!0});x(o),o.drawImage(t,0,0),this._data=a,this.format=`RGBX`,this.codedWidth=r,this.codedHeight=i,this.rotation=n.rotation??0,this.timestamp=n.timestamp,this.duration=n.duration??0,this.colorSpace=new VideoColorSpace({matrix:`rgb`,primaries:`bt709`,transfer:`iec61966-2-1`,fullRange:!0})}else throw TypeError(`Invalid data type: Must be a BufferSource or CanvasImageSource.`);In?.register(this,{type:`video`,data:this._data},this)}clone(){if(this._closed)throw Error(`VideoSample is closed.`);return x(this._data!==null),Rn(this._data)?new e(this._data.clone(),{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):this._data instanceof Uint8Array?new e(this._data.slice(),{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation}):new e(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation})}close(){this._closed||=(In?.unregister(this),Rn(this._data)?this._data.close():this._data=null,!0)}allocationSize(){if(this._closed)throw Error(`VideoSample is closed.`);return x(this._data!==null),Rn(this._data)?this._data.allocationSize():this._data instanceof Uint8Array?this._data.byteLength:this.codedWidth*this.codedHeight*4}async copyTo(e){if(!Pe(e))throw TypeError(`destination must be an ArrayBuffer or an ArrayBuffer view.`);if(this._closed)throw Error(`VideoSample is closed.`);if(x(this._data!==null),Rn(this._data))await this._data.copyTo(e);else if(this._data instanceof Uint8Array)T(e).set(this._data);else{let t=this._data.getContext(`2d`);x(t);let n=t.getImageData(0,0,this.codedWidth,this.codedHeight);T(e).set(n.data)}}toVideoFrame(){if(this._closed)throw Error(`VideoSample is closed.`);return x(this._data!==null),Rn(this._data)?new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0}):this._data instanceof Uint8Array?new VideoFrame(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0,colorSpace:this.colorSpace}):new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0})}draw(e,t,n,r,i,a,o,s,c){let l=0,u=0,d=this.displayWidth,f=this.displayHeight,p=0,m=0,h=this.displayWidth,g=this.displayHeight;if(a===void 0?(p=t,m=n,r!==void 0&&(h=r,g=i)):(l=t,u=n,d=r,f=i,p=a,m=o,s===void 0?(h=d,g=f):(h=s,g=c)),!(typeof CanvasRenderingContext2D<`u`&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<`u`&&e instanceof OffscreenCanvasRenderingContext2D))throw TypeError(`context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.`);if(!Number.isFinite(l))throw TypeError(`sx must be a number.`);if(!Number.isFinite(u))throw TypeError(`sy must be a number.`);if(!Number.isFinite(d)||d<0)throw TypeError(`sWidth must be a non-negative number.`);if(!Number.isFinite(f)||f<0)throw TypeError(`sHeight must be a non-negative number.`);if(!Number.isFinite(p))throw TypeError(`dx must be a number.`);if(!Number.isFinite(m))throw TypeError(`dy must be a number.`);if(!Number.isFinite(h)||h<0)throw TypeError(`dWidth must be a non-negative number.`);if(!Number.isFinite(g)||g<0)throw TypeError(`dHeight must be a non-negative number.`);if(this._closed)throw Error(`VideoSample is closed.`);({sx:l,sy:u,sWidth:d,sHeight:f}=this._rotateSourceRegion(l,u,d,f,this.rotation));let _=this.toCanvasImageSource();e.save();let v=p+h/2,y=m+g/2;e.translate(v,y),e.rotate(this.rotation*Math.PI/180);let ee=this.rotation%180==0?1:h/g;e.scale(1/ee,ee),e.drawImage(_,l,u,d,f,-h/2,-g/2,h,g),e.restore()}drawWithFit(e,t){if(!(typeof CanvasRenderingContext2D<`u`&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<`u`&&e instanceof OffscreenCanvasRenderingContext2D))throw TypeError(`context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(![`fill`,`contain`,`cover`].includes(t.fit))throw TypeError(`options.fit must be 'fill', 'contain', or 'cover'.`);if(t.rotation!==void 0&&![0,90,180,270].includes(t.rotation))throw TypeError(`options.rotation, when provided, must be 0, 90, 180, or 270.`);t.crop!==void 0&&Bn(t.crop,`options.`);let n=e.canvas.width,r=e.canvas.height,i=t.rotation??this.rotation,[a,o]=i%180==0?[this.codedWidth,this.codedHeight]:[this.codedHeight,this.codedWidth];t.crop&&zn(t.crop,a,o);let s,c,l,u,{sx:d,sy:f,sWidth:p,sHeight:m}=this._rotateSourceRegion(t.crop?.left??0,t.crop?.top??0,t.crop?.width??a,t.crop?.height??o,i);if(t.fit===`fill`)s=0,c=0,l=n,u=r;else{let[e,i]=t.crop?[t.crop.width,t.crop.height]:[a,o],d=t.fit===`contain`?Math.min(n/e,r/i):Math.max(n/e,r/i);l=e*d,u=i*d,s=(n-l)/2,c=(r-u)/2}e.save();let h=i%180==0?1:l/u;e.translate(n/2,r/2),e.rotate(i*Math.PI/180),e.scale(1/h,h),e.translate(-n/2,-r/2),e.drawImage(this.toCanvasImageSource(),d,f,p,m,s,c,l,u),e.restore()}_rotateSourceRegion(e,t,n,r,i){return i===90?[e,t,n,r]=[t,this.codedHeight-e-n,r,n]:i===180?[e,t]=[this.codedWidth-e-n,this.codedHeight-t-r]:i===270&&([e,t,n,r]=[this.codedWidth-t-r,e,r,n]),{sx:e,sy:t,sWidth:n,sHeight:r}}toCanvasImageSource(){if(this._closed)throw Error(`VideoSample is closed.`);if(x(this._data!==null),this._data instanceof Uint8Array){let e=this.toVideoFrame();return queueMicrotask(()=>e.close()),e}else return this._data}setRotation(e){if(![0,90,180,270].includes(e))throw TypeError(`newRotation must be 0, 90, 180, or 270.`);this.rotation=e}setTimestamp(e){if(!Number.isFinite(e))throw TypeError(`newTimestamp must be a number.`);this.timestamp=e}setDuration(e){if(!Number.isFinite(e)||e<0)throw TypeError(`newDuration must be a non-negative number.`);this.duration=e}[Symbol.dispose](){this.close()}};let Rn=e=>typeof VideoFrame<`u`&&e instanceof VideoFrame,zn=(e,t,n)=>{e.left=Math.min(e.left,t),e.top=Math.min(e.top,n),e.width=Math.min(e.width,t-e.left),e.height=Math.min(e.height,n-e.top),x(e.width>=0),x(e.height>=0)},Bn=(e,t)=>{if(!e||typeof e!=`object`)throw TypeError(t+`crop, when provided, must be an object.`);if(!Number.isInteger(e.left)||e.left<0)throw TypeError(t+`crop.left must be a non-negative integer.`);if(!Number.isInteger(e.top)||e.top<0)throw TypeError(t+`crop.top must be a non-negative integer.`);if(!Number.isInteger(e.width)||e.width<0)throw TypeError(t+`crop.width must be a non-negative integer.`);if(!Number.isInteger(e.height)||e.height<0)throw TypeError(t+`crop.height must be a non-negative integer.`)},Vn=new Set([`f32`,`f32-planar`,`s16`,`s16-planar`,`s32`,`s32-planar`,`u8`,`u8-planar`]);var Hn=class e{get microsecondTimestamp(){return Math.trunc($e*this.timestamp)}get microsecondDuration(){return Math.trunc($e*this.duration)}constructor(e){if(this._closed=!1,qn(e)){if(e.format===null)throw TypeError(`AudioData with null format is not supported.`);this._data=e,this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=e.numberOfFrames,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp/1e6,this.duration=e.numberOfFrames/e.sampleRate}else{if(!e||typeof e!=`object`)throw TypeError(`Invalid AudioDataInit: must be an object.`);if(!Vn.has(e.format))throw TypeError(`Invalid AudioDataInit: invalid format.`);if(!Number.isFinite(e.sampleRate)||e.sampleRate<=0)throw TypeError(`Invalid AudioDataInit: sampleRate must be > 0.`);if(!Number.isInteger(e.numberOfChannels)||e.numberOfChannels===0)throw TypeError(`Invalid AudioDataInit: numberOfChannels must be an integer > 0.`);if(!Number.isFinite(e?.timestamp))throw TypeError(`init.timestamp must be a number.`);let t=e.data.byteLength/(Un(e.format)*e.numberOfChannels);if(!Number.isInteger(t))throw TypeError(`Invalid AudioDataInit: data size is not a multiple of frame size.`);this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=t,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp,this.duration=t/e.sampleRate;let n;if(e.data instanceof ArrayBuffer)n=new Uint8Array(e.data);else if(ArrayBuffer.isView(e.data))n=new Uint8Array(e.data.buffer,e.data.byteOffset,e.data.byteLength);else throw TypeError(`Invalid AudioDataInit: data is not a BufferSource.`);let r=this.numberOfFrames*this.numberOfChannels*Un(this.format);if(n.byteLength<r)throw TypeError(`Invalid AudioDataInit: insufficient data size.`);this._data=n}In?.register(this,{type:`audio`,data:this._data},this)}allocationSize(e){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(!Number.isInteger(e.planeIndex)||e.planeIndex<0)throw TypeError(`planeIndex must be a non-negative integer.`);if(e.format!==void 0&&!Vn.has(e.format))throw TypeError(`Invalid format.`);if(e.frameOffset!==void 0&&(!Number.isInteger(e.frameOffset)||e.frameOffset<0))throw TypeError(`frameOffset must be a non-negative integer.`);if(e.frameCount!==void 0&&(!Number.isInteger(e.frameCount)||e.frameCount<0))throw TypeError(`frameCount must be a non-negative integer.`);if(this._closed)throw Error(`AudioSample is closed.`);let t=e.format??this.format,n=e.frameOffset??0;if(n>=this.numberOfFrames)throw RangeError(`frameOffset out of range`);let r=e.frameCount===void 0?this.numberOfFrames-n:e.frameCount;if(r>this.numberOfFrames-n)throw RangeError(`frameCount out of range`);let i=Un(t),a=Wn(t);if(a&&e.planeIndex>=this.numberOfChannels||!a&&e.planeIndex!==0)throw RangeError(`planeIndex out of range`);return(a?r:r*this.numberOfChannels)*i}copyTo(e,t){if(!Pe(e))throw TypeError(`destination must be an ArrayBuffer or an ArrayBuffer view.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(!Number.isInteger(t.planeIndex)||t.planeIndex<0)throw TypeError(`planeIndex must be a non-negative integer.`);if(t.format!==void 0&&!Vn.has(t.format))throw TypeError(`Invalid format.`);if(t.frameOffset!==void 0&&(!Number.isInteger(t.frameOffset)||t.frameOffset<0))throw TypeError(`frameOffset must be a non-negative integer.`);if(t.frameCount!==void 0&&(!Number.isInteger(t.frameCount)||t.frameCount<0))throw TypeError(`frameCount must be a non-negative integer.`);if(this._closed)throw Error(`AudioSample is closed.`);let{planeIndex:n,format:r,frameCount:i,frameOffset:a}=t,o=r??this.format;if(!o)throw Error(`Destination format not determined`);let s=this.numberOfFrames,c=this.numberOfChannels,l=a??0;if(l>=s)throw RangeError(`frameOffset out of range`);let u=i===void 0?s-l:i;if(u>s-l)throw RangeError(`frameCount out of range`);let d=Un(o),f=Wn(o);if(f&&n>=c||!f&&n!==0)throw RangeError(`planeIndex out of range`);let p=(f?u:u*c)*d;if(e.byteLength<p)throw RangeError(`Destination buffer is too small`);let m=E(e),h=Kn(o);if(qn(this._data))if(f)if(o===`f32-planar`)this._data.copyTo(e,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});else{let e=new ArrayBuffer(u*4),t=new Float32Array(e);this._data.copyTo(t,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});let r=new DataView(e);for(let e=0;e<u;e++)h(m,e*d,r.getFloat32(e*4,!0))}else{let e=c,t=new Float32Array(u);for(let n=0;n<e;n++){this._data.copyTo(t,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});for(let r=0;r<u;r++)h(m,(r*e+n)*d,t[r])}}else{let e=this._data,t=E(e),r=this.format,i=Gn(r),a=Un(r),o=Wn(r);for(let e=0;e<u;e++)if(f){let r=e*d,u;u=o?(n*s+(e+l))*a:((e+l)*c+n)*a,h(m,r,i(t,u))}else for(let n=0;n<c;n++){let r=(e*c+n)*d,u;u=o?(n*s+(e+l))*a:((e+l)*c+n)*a,h(m,r,i(t,u))}}}clone(){if(this._closed)throw Error(`AudioSample is closed.`);if(qn(this._data)){let t=new e(this._data.clone());return t.setTimestamp(this.timestamp),t}else return new e({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.timestamp,data:this._data})}close(){this._closed||=(In?.unregister(this),qn(this._data)?this._data.close():this._data=new Uint8Array,!0)}toAudioData(){if(this._closed)throw Error(`AudioSample is closed.`);if(qn(this._data)){if(this._data.timestamp===this.microsecondTimestamp)return this._data.clone();if(Wn(this.format)){let e=this.allocationSize({planeIndex:0,format:this.format}),t=new ArrayBuffer(e*this.numberOfChannels);for(let n=0;n<this.numberOfChannels;n++)this.copyTo(new Uint8Array(t,n*e,e),{planeIndex:n,format:this.format});return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:t})}else{let e=new ArrayBuffer(this.allocationSize({planeIndex:0,format:this.format}));return this.copyTo(e,{planeIndex:0,format:this.format}),new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:e})}}else return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:this._data.buffer instanceof ArrayBuffer?this._data.buffer:this._data.slice()})}toAudioBuffer(){if(this._closed)throw Error(`AudioSample is closed.`);let e=new AudioBuffer({numberOfChannels:this.numberOfChannels,length:this.numberOfFrames,sampleRate:this.sampleRate}),t=new Float32Array(this.allocationSize({planeIndex:0,format:`f32-planar`})/4);for(let n=0;n<this.numberOfChannels;n++)this.copyTo(t,{planeIndex:n,format:`f32-planar`}),e.copyToChannel(t,n);return e}setTimestamp(e){if(!Number.isFinite(e))throw TypeError(`newTimestamp must be a number.`);this.timestamp=e}[Symbol.dispose](){this.close()}static*_fromAudioBuffer(t,n){if(!(t instanceof AudioBuffer))throw TypeError(`audioBuffer must be an AudioBuffer.`);let r=t.numberOfChannels,i=t.sampleRate,a=t.length,o=Math.floor(24e4/r),s=0,c=a;for(;c>0;){let a=Math.min(o,c),l=new Float32Array(r*a);for(let e=0;e<r;e++)t.copyFromChannel(l.subarray(e*a,(e+1)*a),e,s);yield new e({format:`f32-planar`,sampleRate:i,numberOfFrames:a,numberOfChannels:r,timestamp:n+s/i,data:l}),s+=a,c-=a}}static fromAudioBuffer(t,n){if(!(t instanceof AudioBuffer))throw TypeError(`audioBuffer must be an AudioBuffer.`);let r=t.numberOfChannels,i=t.sampleRate,a=t.length,o=Math.floor(24e4/r),s=0,c=a,l=[];for(;c>0;){let a=Math.min(o,c),u=new Float32Array(r*a);for(let e=0;e<r;e++)t.copyFromChannel(u.subarray(e*a,(e+1)*a),e,s);let d=new e({format:`f32-planar`,sampleRate:i,numberOfFrames:a,numberOfChannels:r,timestamp:n+s/i,data:u});l.push(d),s+=a,c-=a}return l}};let Un=e=>{switch(e){case`u8`:case`u8-planar`:return 1;case`s16`:case`s16-planar`:return 2;case`s32`:case`s32-planar`:return 4;case`f32`:case`f32-planar`:return 4;default:throw Error(`Unknown AudioSampleFormat`)}},Wn=e=>{switch(e){case`u8-planar`:case`s16-planar`:case`s32-planar`:case`f32-planar`:return!0;default:return!1}},Gn=e=>{switch(e){case`u8`:case`u8-planar`:return(e,t)=>(e.getUint8(t)-128)/128;case`s16`:case`s16-planar`:return(e,t)=>e.getInt16(t,!0)/32768;case`s32`:case`s32-planar`:return(e,t)=>e.getInt32(t,!0)/2147483648;case`f32`:case`f32-planar`:return(e,t)=>e.getFloat32(t,!0)}},Kn=e=>{switch(e){case`u8`:case`u8-planar`:return(e,t,n)=>e.setUint8(t,A((n+1)*127.5,0,255));case`s16`:case`s16-planar`:return(e,t,n)=>e.setInt16(t,A(Math.round(n*32767),-32768,32767),!0);case`s32`:case`s32-planar`:return(e,t,n)=>e.setInt32(t,A(Math.round(n*2147483647),-2147483648,2147483647),!0);case`f32`:case`f32-planar`:return(e,t,n)=>e.setFloat32(t,n,!0)}},qn=e=>typeof AudioData<`u`&&e instanceof AudioData,Jn=e=>{if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(e.metadataOnly!==void 0&&typeof e.metadataOnly!=`boolean`)throw TypeError(`options.metadataOnly, when defined, must be a boolean.`);if(e.verifyKeyPackets!==void 0&&typeof e.verifyKeyPackets!=`boolean`)throw TypeError(`options.verifyKeyPackets, when defined, must be a boolean.`);if(e.verifyKeyPackets&&e.metadataOnly)throw TypeError(`options.verifyKeyPackets and options.metadataOnly cannot be enabled together.`)},Yn=e=>{if(!pt(e))throw TypeError(`timestamp must be a number.`)},Xn=(e,t,n)=>n.verifyKeyPackets?t.then(async t=>{if(!t||t.type===`delta`)return t;let n=await e.determinePacketType(t);return n&&(t.type=n),t}):t;var Zn=class{constructor(e){if(!(e instanceof cr))throw TypeError(`track must be an InputTrack.`);this._track=e}getFirstPacket(e={}){if(Jn(e),this._track.input._disposed)throw new L;return Xn(this._track,this._track._backing.getFirstPacket(e),e)}getPacket(e,t={}){if(Yn(e),Jn(t),this._track.input._disposed)throw new L;return Xn(this._track,this._track._backing.getPacket(e,t),t)}getNextPacket(e,t={}){if(!(e instanceof F))throw TypeError(`packet must be an EncodedPacket.`);if(Jn(t),this._track.input._disposed)throw new L;return Xn(this._track,this._track._backing.getNextPacket(e,t),t)}async getKeyPacket(e,t={}){if(Yn(e),Jn(t),this._track.input._disposed)throw new L;if(!t.verifyKeyPackets)return this._track._backing.getKeyPacket(e,t);let n=await this._track._backing.getKeyPacket(e,t);return n&&(x(n.type===`key`),await this._track.determinePacketType(n)===`delta`?this.getKeyPacket(n.timestamp-1/this._track.timeResolution,t):n)}async getNextKeyPacket(e,t={}){if(!(e instanceof F))throw TypeError(`packet must be an EncodedPacket.`);if(Jn(t),this._track.input._disposed)throw new L;if(!t.verifyKeyPackets)return this._track._backing.getNextKeyPacket(e,t);let n=await this._track._backing.getNextKeyPacket(e,t);return n&&(x(n.type===`key`),await this._track.determinePacketType(n)===`delta`?this.getNextKeyPacket(n,t):n)}packets(e,t,n={}){if(e!==void 0&&!(e instanceof F))throw TypeError(`startPacket must be an EncodedPacket.`);if(e!==void 0&&e.isMetadataOnly&&!n?.metadataOnly)throw TypeError(`startPacket can only be metadata-only if options.metadataOnly is enabled.`);if(t!==void 0&&!(t instanceof F))throw TypeError(`endPacket must be an EncodedPacket.`);if(Jn(n),this._track.input._disposed)throw new L;let r=[],{promise:i,resolve:a}=k(),{promise:o,resolve:s}=k(),c=!1,l=!1,u=null,d=[],f=()=>Math.max(2,d.length);(async()=>{let u=e??await this.getFirstPacket(n);for(;u&&!l&&!this._track.input._disposed&&!(t&&u.sequenceNumber>=t?.sequenceNumber);){if(r.length>f()){({promise:o,resolve:s}=k()),await o;continue}r.push(u),a(),{promise:i,resolve:a}=k(),u=await this.getNextPacket(u,n)}c=!0,a()})().catch(e=>{u||(u=e,a())});let p=this._track;return{async next(){for(;;)if(p.input._disposed)throw new L;else if(l)return{value:void 0,done:!0};else if(u)throw u;else if(r.length>0){let e=r.shift(),t=performance.now();for(d.push(t);d.length>0&&t-d[0]>=1e3;)d.shift();return s(),{value:e,done:!1}}else if(c)return{value:void 0,done:!0};else await i},async return(){return l=!0,s(),a(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}},Qn=class{constructor(e,t){this.onSample=e,this.onError=t}},$n=class{mediaSamplesInRange(e=0,t=1/0){Yn(e),Yn(t);let n=[],r=!1,i=null,{promise:a,resolve:o}=k(),{promise:s,resolve:c}=k(),l=!1,u=!1,d=!1,f=null;(async()=>{let p=Error(),m=await this._createDecoder(s=>{if(c(),s.timestamp>=t&&(u=!0),u){s.close();return}i&&(s.timestamp>e?(n.push(i),r=!0):i.close()),s.timestamp>=e&&(n.push(s),r=!0),i=r?null:s,n.length>0&&(o(),{promise:a,resolve:o}=k())},e=>{f||(e.stack=p.stack,f=e,o())}),h=this._createPacketSink(),g=await h.getKeyPacket(e,{verifyKeyPackets:!0})??await h.getFirstPacket(),_=g,v;if(t<1/0){let e=await h.getPacket(t),n=e?e.type===`key`&&e.timestamp===t?e:await h.getNextKeyPacket(e,{verifyKeyPackets:!0}):null;n&&(v=n)}let y=h.packets(g??void 0,v);for(await y.next();_&&!u&&!this._track.input._disposed;){let e=er(n.length);if(n.length+m.getDecodeQueueSize()>e){({promise:s,resolve:c}=k()),await s;continue}m.decode(_);let t=await y.next();if(t.done)break;_=t.value}await y.return(),!d&&!this._track.input._disposed&&await m.flush(),m.close(),!r&&i&&n.push(i),l=!0,o()})().catch(e=>{f||(f=e,o())});let p=this._track,m=()=>{i?.close();for(let e of n)e.close()};return{async next(){for(;;)if(p.input._disposed)throw m(),new L;else if(d)return{value:void 0,done:!0};else if(f)throw m(),f;else if(n.length>0){let e=n.shift();return c(),{value:e,done:!1}}else if(!l)await a;else return{value:void 0,done:!0}},async return(){return d=!0,u=!0,c(),o(),m(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}mediaSamplesAtTimestamps(e){He(e);let t=Ve(e),n=[],r=[],{promise:i,resolve:a}=k(),{promise:o,resolve:s}=k(),c=!1,l=!1,u=null,d=e=>{r.push(e),a(),{promise:i,resolve:a}=k()};(async()=>{let e=Error(),i=await this._createDecoder(e=>{if(s(),l){e.close();return}let t=0;for(;n.length>0&&e.timestamp-n[0]>-1e-10;)t++,n.shift();if(t>0)for(let n=0;n<t;n++)d(n<t-1?e.clone():e);else e.close()},t=>{u||(t.stack=e.stack,u=t,a())}),f=this._createPacketSink(),p=null,m=null,h=-1,g=async()=>{x(m);let e=m;for(i.decode(e);e.sequenceNumber<h;){let t=er(r.length);for(;r.length+i.getDecodeQueueSize()>t&&!l;)({promise:o,resolve:s}=k()),await o;if(l)break;let n=await f.getNextPacket(e);x(n),i.decode(n),e=n}h=-1},_=async()=>{await i.flush();for(let e=0;e<n.length;e++)d(null);n.length=0};for await(let e of t){if(Yn(e),l||this._track.input._disposed)break;let t=await f.getPacket(e),r=t&&await f.getKeyPacket(e,{verifyKeyPackets:!0});if(!r){h!==-1&&(await g(),await _()),d(null),p=null;continue}p&&(r.sequenceNumber!==m.sequenceNumber||t.timestamp<p.timestamp)&&(await g(),await _()),n.push(t.timestamp),h=Math.max(t.sequenceNumber,h),p=t,m=r}!l&&!this._track.input._disposed&&(h!==-1&&await g(),await _()),i.close(),c=!0,a()})().catch(e=>{u||(u=e,a())});let f=this._track,p=()=>{for(let e of r)e?.close()};return{async next(){for(;;)if(f.input._disposed)throw p(),new L;else if(l)return{value:void 0,done:!0};else if(u)throw p(),u;else if(r.length>0){let e=r.shift();return x(e!==void 0),s(),{value:e,done:!1}}else if(!c)await i;else return{value:void 0,done:!0}},async return(){return l=!0,s(),a(),p(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}};let er=e=>e===0?40:8;var tr=class extends Qn{constructor(e,t,n,r,i,a){super(e,t),this.codec=n,this.decoderConfig=r,this.rotation=i,this.timeResolution=a,this.decoder=null,this.customDecoder=null,this.customDecoderCallSerializer=new tt,this.customDecoderQueueSize=0,this.inputTimestamps=[],this.sampleQueue=[],this.currentPacketIndex=0,this.raslSkipped=!1,this.alphaDecoder=null,this.alphaHadKeyframe=!1,this.colorQueue=[],this.alphaQueue=[],this.merger=null,this.mergerCreationFailed=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue=[],this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1;let o=Tn.find(e=>e.supports(n,r));if(o)this.customDecoder=new o,this.customDecoder.codec=n,this.customDecoder.config=r,this.customDecoder.onSample=e=>{if(!(e instanceof Ln))throw TypeError(`The argument passed to onSample must be a VideoSample.`);this.finalizeAndEmitSample(e)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let e=e=>{if(this.alphaQueue.length>0){let t=this.alphaQueue.shift();x(t!==void 0),this.mergeAlpha(e,t)}else this.colorQueue.push(e)};if(n===`avc`&&this.decoderConfig.description&&st()){let e=an(T(this.decoderConfig.description));if(e&&e.sequenceParameterSets.length>0){let t=on(e.sequenceParameterSets[0]);t&&t.frameMbsOnlyFlag===0&&(this.decoderConfig={...this.decoderConfig,hardwareAcceleration:`prefer-software`})}}this.decoder=new VideoDecoder({output:t=>{try{e(t)}catch(e){this.onError(e)}},error:t}),this.decoder.configure(this.decoderConfig)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(x(this.decoder),Math.max(this.decoder.decodeQueueSize,this.alphaDecoder?.decodeQueueSize??0))}decode(e){if(this.codec===`hevc`&&this.currentPacketIndex>0&&!this.raslSkipped){if(this.hasHevcRaslPicture(e.data))return;this.raslSkipped=!0}this.currentPacketIndex++,this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(x(this.decoder),rt()||ze(this.inputTimestamps,e.timestamp,e=>e),this.decoder.decode(e.toEncodedVideoChunk()),this.decodeAlphaData(e))}decodeAlphaData(e){if(!e.sideData.alpha||this.mergerCreationFailed){this.pushNullAlphaFrame();return}if(!this.merger)try{this.merger=new nr}catch(t){console.error(`Due to an error, only color data will be decoded.`,t),this.mergerCreationFailed=!0,this.decodeAlphaData(e);return}if(!this.alphaDecoder){let e=e=>{if(this.alphaDecoderQueueSize--,this.colorQueue.length>0){let t=this.colorQueue.shift();x(t!==void 0),this.mergeAlpha(t,e)}else this.alphaQueue.push(e);for(this.decodedAlphaChunkCount++;this.nullAlphaFrameQueue.length>0&&this.nullAlphaFrameQueue[0]===this.decodedAlphaChunkCount;)if(this.nullAlphaFrameQueue.shift(),this.colorQueue.length>0){let e=this.colorQueue.shift();x(e!==void 0),this.mergeAlpha(e,null)}else this.alphaQueue.push(null)};this.alphaDecoder=new VideoDecoder({output:t=>{try{e(t)}catch(e){this.onError(e)}},error:this.onError}),this.alphaDecoder.configure(this.decoderConfig)}let t=Sn(this.codec,this.decoderConfig,e.sideData.alpha);if(this.alphaHadKeyframe||=t===`key`,this.alphaHadKeyframe){if(this.codec===`hevc`&&this.currentAlphaPacketIndex>0&&!this.alphaRaslSkipped){if(this.hasHevcRaslPicture(e.sideData.alpha)){this.pushNullAlphaFrame();return}this.alphaRaslSkipped=!0}this.currentAlphaPacketIndex++,this.alphaDecoder.decode(e.alphaToEncodedVideoChunk(t??e.type)),this.alphaDecoderQueueSize++}else this.pushNullAlphaFrame()}pushNullAlphaFrame(){this.alphaDecoderQueueSize===0?this.alphaQueue.push(null):this.nullAlphaFrameQueue.push(this.decodedAlphaChunkCount+this.alphaDecoderQueueSize)}hasHevcRaslPicture(e){return sn(e,this.decoderConfig).some(e=>{let t=cn(e);return t===P.RASL_N||t===P.RASL_R})}sampleHandler(e){if(rt()){if(this.sampleQueue.length>0&&e.timestamp>=S(this.sampleQueue).timestamp){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}ze(this.sampleQueue,e,e=>e.timestamp)}else{let t=this.inputTimestamps.shift();x(t!==void 0),e.setTimestamp(t),this.finalizeAndEmitSample(e)}}finalizeAndEmitSample(e){e.setTimestamp(Math.round(e.timestamp*this.timeResolution)/this.timeResolution),e.setDuration(Math.round(e.duration*this.timeResolution)/this.timeResolution),e.setRotation(this.rotation),this.onSample(e)}mergeAlpha(e,t){if(!t){let t=new Ln(e);this.sampleHandler(t);return}x(this.merger),this.merger.update(e,t),e.close(),t.close();let n=new Ln(new VideoFrame(this.merger.canvas,{timestamp:e.timestamp,duration:e.duration??void 0}));this.sampleHandler(n)}async flush(){if(this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(x(this.decoder),await Promise.all([this.decoder.flush(),this.alphaDecoder?.flush()]),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.alphaHadKeyframe=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue.length=0,this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1),rt()){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}this.currentPacketIndex=0,this.raslSkipped=!1}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(x(this.decoder),this.decoder.close(),this.alphaDecoder?.close(),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.merger?.close());for(let e of this.sampleQueue)e.close();this.sampleQueue.length=0}},nr=class{constructor(){typeof OffscreenCanvas<`u`?this.canvas=new OffscreenCanvas(300,150):this.canvas=document.createElement(`canvas`);let e=this.canvas.getContext(`webgl2`,{premultipliedAlpha:!1});if(!e)throw Error(`Couldn't acquire WebGL 2 context.`);this.gl=e,this.program=this.createProgram(),this.vao=this.createVAO(),this.colorTexture=this.createTexture(),this.alphaTexture=this.createTexture(),this.gl.useProgram(this.program),this.gl.uniform1i(this.gl.getUniformLocation(this.program,`u_colorTexture`),0),this.gl.uniform1i(this.gl.getUniformLocation(this.program,`u_alphaTexture`),1)}createProgram(){let e=this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
|
|
49
|
+
mt();let In=-1/0,Ln=-1/0,Rn=null;typeof FinalizationRegistry<`u`&&(Rn=new FinalizationRegistry(e=>{let t=Date.now();e.type===`video`?(t-In>=1e3&&(console.error(`A VideoSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your VideoSamples as soon as you're done using them.`),In=t),typeof VideoFrame<`u`&&e.data instanceof VideoFrame&&e.data.close()):(t-Ln>=1e3&&(console.error(`An AudioSample was garbage collected without first being closed. For proper resource management, make sure to call close() on all your AudioSamples as soon as you're done using them.`),Ln=t),typeof AudioData<`u`&&e.data instanceof AudioData&&e.data.close())}));var F=class e{get displayWidth(){return this.rotation%180==0?this.codedWidth:this.codedHeight}get displayHeight(){return this.rotation%180==0?this.codedHeight:this.codedWidth}get microsecondTimestamp(){return Math.trunc(tt*this.timestamp)}get microsecondDuration(){return Math.trunc(tt*this.duration)}get hasAlpha(){return this.format&&this.format.includes(`A`)}constructor(t,n){if(this._closed=!1,t instanceof ArrayBuffer||typeof SharedArrayBuffer<`u`&&t instanceof SharedArrayBuffer||ArrayBuffer.isView(t)){if(!n||typeof n!=`object`)throw TypeError(`init must be an object.`);if(!(`format`in n)||typeof n.format!=`string`)throw TypeError(`init.format must be a string.`);if(!Number.isInteger(n.codedWidth)||n.codedWidth<=0)throw TypeError(`init.codedWidth must be a positive integer.`);if(!Number.isInteger(n.codedHeight)||n.codedHeight<=0)throw TypeError(`init.codedHeight must be a positive integer.`);if(n.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(!Number.isFinite(n.timestamp))throw TypeError(`init.timestamp must be a number.`);if(n.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);this._data=w(t).slice(),this.format=n.format,this.codedWidth=n.codedWidth,this.codedHeight=n.codedHeight,this.rotation=n.rotation??0,this.timestamp=n.timestamp,this.duration=n.duration??0,this.colorSpace=new VideoColorSpace(n.colorSpace)}else if(typeof VideoFrame<`u`&&t instanceof VideoFrame){if(n?.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(n?.timestamp!==void 0&&!Number.isFinite(n?.timestamp))throw TypeError(`init.timestamp, when provided, must be a number.`);if(n?.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);this._data=t,this.format=t.format,this.codedWidth=t.displayWidth,this.codedHeight=t.displayHeight,this.rotation=n?.rotation??0,this.timestamp=n?.timestamp??t.timestamp/1e6,this.duration=n?.duration??(t.duration??0)/1e6,this.colorSpace=t.colorSpace}else if(typeof HTMLImageElement<`u`&&t instanceof HTMLImageElement||typeof SVGImageElement<`u`&&t instanceof SVGImageElement||typeof ImageBitmap<`u`&&t instanceof ImageBitmap||typeof HTMLVideoElement<`u`&&t instanceof HTMLVideoElement||typeof HTMLCanvasElement<`u`&&t instanceof HTMLCanvasElement||typeof OffscreenCanvas<`u`&&t instanceof OffscreenCanvas){if(!n||typeof n!=`object`)throw TypeError(`init must be an object.`);if(n.rotation!==void 0&&![0,90,180,270].includes(n.rotation))throw TypeError(`init.rotation, when provided, must be 0, 90, 180, or 270.`);if(!Number.isFinite(n.timestamp))throw TypeError(`init.timestamp must be a number.`);if(n.duration!==void 0&&(!Number.isFinite(n.duration)||n.duration<0))throw TypeError(`init.duration, when provided, must be a non-negative number.`);if(typeof VideoFrame<`u`)return new e(new VideoFrame(t,{timestamp:Math.trunc(n.timestamp*tt),duration:Math.trunc((n.duration??0)*tt)||void 0}),n);let r=0,i=0;if(`naturalWidth`in t?(r=t.naturalWidth,i=t.naturalHeight):`videoWidth`in t?(r=t.videoWidth,i=t.videoHeight):`width`in t&&(r=Number(t.width),i=Number(t.height)),!r||!i)throw TypeError(`Could not determine dimensions.`);let a=new OffscreenCanvas(r,i),o=a.getContext(`2d`,{alpha:st(),willReadFrequently:!0});b(o),o.drawImage(t,0,0),this._data=a,this.format=`RGBX`,this.codedWidth=r,this.codedHeight=i,this.rotation=n.rotation??0,this.timestamp=n.timestamp,this.duration=n.duration??0,this.colorSpace=new VideoColorSpace({matrix:`rgb`,primaries:`bt709`,transfer:`iec61966-2-1`,fullRange:!0})}else throw TypeError(`Invalid data type: Must be a BufferSource or CanvasImageSource.`);Rn?.register(this,{type:`video`,data:this._data},this)}clone(){if(this._closed)throw Error(`VideoSample is closed.`);return b(this._data!==null),zn(this._data)?new e(this._data.clone(),{timestamp:this.timestamp,duration:this.duration,rotation:this.rotation}):this._data instanceof Uint8Array?new e(this._data.slice(),{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation}):new e(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.timestamp,duration:this.duration,colorSpace:this.colorSpace,rotation:this.rotation})}close(){this._closed||=(Rn?.unregister(this),zn(this._data)?this._data.close():this._data=null,!0)}allocationSize(){if(this._closed)throw Error(`VideoSample is closed.`);return b(this._data!==null),zn(this._data)?this._data.allocationSize():this._data instanceof Uint8Array?this._data.byteLength:this.codedWidth*this.codedHeight*4}async copyTo(e){if(!Ie(e))throw TypeError(`destination must be an ArrayBuffer or an ArrayBuffer view.`);if(this._closed)throw Error(`VideoSample is closed.`);if(b(this._data!==null),zn(this._data))await this._data.copyTo(e);else if(this._data instanceof Uint8Array)w(e).set(this._data);else{let t=this._data.getContext(`2d`);b(t);let n=t.getImageData(0,0,this.codedWidth,this.codedHeight);w(e).set(n.data)}}toVideoFrame(){if(this._closed)throw Error(`VideoSample is closed.`);return b(this._data!==null),zn(this._data)?new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0}):this._data instanceof Uint8Array?new VideoFrame(this._data,{format:this.format,codedWidth:this.codedWidth,codedHeight:this.codedHeight,timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0,colorSpace:this.colorSpace}):new VideoFrame(this._data,{timestamp:this.microsecondTimestamp,duration:this.microsecondDuration||void 0})}draw(e,t,n,r,i,a,o,s,c){let l=0,u=0,d=this.displayWidth,f=this.displayHeight,p=0,m=0,h=this.displayWidth,g=this.displayHeight;if(a===void 0?(p=t,m=n,r!==void 0&&(h=r,g=i)):(l=t,u=n,d=r,f=i,p=a,m=o,s===void 0?(h=d,g=f):(h=s,g=c)),!(typeof CanvasRenderingContext2D<`u`&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<`u`&&e instanceof OffscreenCanvasRenderingContext2D))throw TypeError(`context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.`);if(!Number.isFinite(l))throw TypeError(`sx must be a number.`);if(!Number.isFinite(u))throw TypeError(`sy must be a number.`);if(!Number.isFinite(d)||d<0)throw TypeError(`sWidth must be a non-negative number.`);if(!Number.isFinite(f)||f<0)throw TypeError(`sHeight must be a non-negative number.`);if(!Number.isFinite(p))throw TypeError(`dx must be a number.`);if(!Number.isFinite(m))throw TypeError(`dy must be a number.`);if(!Number.isFinite(h)||h<0)throw TypeError(`dWidth must be a non-negative number.`);if(!Number.isFinite(g)||g<0)throw TypeError(`dHeight must be a non-negative number.`);if(this._closed)throw Error(`VideoSample is closed.`);({sx:l,sy:u,sWidth:d,sHeight:f}=this._rotateSourceRegion(l,u,d,f,this.rotation));let _=this.toCanvasImageSource();e.save();let v=p+h/2,ee=m+g/2;e.translate(v,ee),e.rotate(this.rotation*Math.PI/180);let te=this.rotation%180==0?1:h/g;e.scale(1/te,te),e.drawImage(_,l,u,d,f,-h/2,-g/2,h,g),e.restore()}drawWithFit(e,t){if(!(typeof CanvasRenderingContext2D<`u`&&e instanceof CanvasRenderingContext2D||typeof OffscreenCanvasRenderingContext2D<`u`&&e instanceof OffscreenCanvasRenderingContext2D))throw TypeError(`context must be a CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(![`fill`,`contain`,`cover`].includes(t.fit))throw TypeError(`options.fit must be 'fill', 'contain', or 'cover'.`);if(t.rotation!==void 0&&![0,90,180,270].includes(t.rotation))throw TypeError(`options.rotation, when provided, must be 0, 90, 180, or 270.`);t.crop!==void 0&&Vn(t.crop,`options.`);let n=e.canvas.width,r=e.canvas.height,i=t.rotation??this.rotation,[a,o]=i%180==0?[this.codedWidth,this.codedHeight]:[this.codedHeight,this.codedWidth];t.crop&&Bn(t.crop,a,o);let s,c,l,u,{sx:d,sy:f,sWidth:p,sHeight:m}=this._rotateSourceRegion(t.crop?.left??0,t.crop?.top??0,t.crop?.width??a,t.crop?.height??o,i);if(t.fit===`fill`)s=0,c=0,l=n,u=r;else{let[e,i]=t.crop?[t.crop.width,t.crop.height]:[a,o],d=t.fit===`contain`?Math.min(n/e,r/i):Math.max(n/e,r/i);l=e*d,u=i*d,s=(n-l)/2,c=(r-u)/2}e.save();let h=i%180==0?1:l/u;e.translate(n/2,r/2),e.rotate(i*Math.PI/180),e.scale(1/h,h),e.translate(-n/2,-r/2),e.drawImage(this.toCanvasImageSource(),d,f,p,m,s,c,l,u),e.restore()}_rotateSourceRegion(e,t,n,r,i){return i===90?[e,t,n,r]=[t,this.codedHeight-e-n,r,n]:i===180?[e,t]=[this.codedWidth-e-n,this.codedHeight-t-r]:i===270&&([e,t,n,r]=[this.codedWidth-t-r,e,r,n]),{sx:e,sy:t,sWidth:n,sHeight:r}}toCanvasImageSource(){if(this._closed)throw Error(`VideoSample is closed.`);if(b(this._data!==null),this._data instanceof Uint8Array){let e=this.toVideoFrame();return queueMicrotask(()=>e.close()),e}else return this._data}setRotation(e){if(![0,90,180,270].includes(e))throw TypeError(`newRotation must be 0, 90, 180, or 270.`);this.rotation=e}setTimestamp(e){if(!Number.isFinite(e))throw TypeError(`newTimestamp must be a number.`);this.timestamp=e}setDuration(e){if(!Number.isFinite(e)||e<0)throw TypeError(`newDuration must be a non-negative number.`);this.duration=e}[Symbol.dispose](){this.close()}};let zn=e=>typeof VideoFrame<`u`&&e instanceof VideoFrame,Bn=(e,t,n)=>{e.left=Math.min(e.left,t),e.top=Math.min(e.top,n),e.width=Math.min(e.width,t-e.left),e.height=Math.min(e.height,n-e.top),b(e.width>=0),b(e.height>=0)},Vn=(e,t)=>{if(!e||typeof e!=`object`)throw TypeError(t+`crop, when provided, must be an object.`);if(!Number.isInteger(e.left)||e.left<0)throw TypeError(t+`crop.left must be a non-negative integer.`);if(!Number.isInteger(e.top)||e.top<0)throw TypeError(t+`crop.top must be a non-negative integer.`);if(!Number.isInteger(e.width)||e.width<0)throw TypeError(t+`crop.width must be a non-negative integer.`);if(!Number.isInteger(e.height)||e.height<0)throw TypeError(t+`crop.height must be a non-negative integer.`)},Hn=new Set([`f32`,`f32-planar`,`s16`,`s16-planar`,`s32`,`s32-planar`,`u8`,`u8-planar`]);var Un=class e{get microsecondTimestamp(){return Math.trunc(tt*this.timestamp)}get microsecondDuration(){return Math.trunc(tt*this.duration)}constructor(e){if(this._closed=!1,Jn(e)){if(e.format===null)throw TypeError(`AudioData with null format is not supported.`);this._data=e,this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=e.numberOfFrames,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp/1e6,this.duration=e.numberOfFrames/e.sampleRate}else{if(!e||typeof e!=`object`)throw TypeError(`Invalid AudioDataInit: must be an object.`);if(!Hn.has(e.format))throw TypeError(`Invalid AudioDataInit: invalid format.`);if(!Number.isFinite(e.sampleRate)||e.sampleRate<=0)throw TypeError(`Invalid AudioDataInit: sampleRate must be > 0.`);if(!Number.isInteger(e.numberOfChannels)||e.numberOfChannels===0)throw TypeError(`Invalid AudioDataInit: numberOfChannels must be an integer > 0.`);if(!Number.isFinite(e?.timestamp))throw TypeError(`init.timestamp must be a number.`);let t=e.data.byteLength/(Wn(e.format)*e.numberOfChannels);if(!Number.isInteger(t))throw TypeError(`Invalid AudioDataInit: data size is not a multiple of frame size.`);this.format=e.format,this.sampleRate=e.sampleRate,this.numberOfFrames=t,this.numberOfChannels=e.numberOfChannels,this.timestamp=e.timestamp,this.duration=t/e.sampleRate;let n;if(e.data instanceof ArrayBuffer)n=new Uint8Array(e.data);else if(ArrayBuffer.isView(e.data))n=new Uint8Array(e.data.buffer,e.data.byteOffset,e.data.byteLength);else throw TypeError(`Invalid AudioDataInit: data is not a BufferSource.`);let r=this.numberOfFrames*this.numberOfChannels*Wn(this.format);if(n.byteLength<r)throw TypeError(`Invalid AudioDataInit: insufficient data size.`);this._data=n}Rn?.register(this,{type:`audio`,data:this._data},this)}allocationSize(e){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(!Number.isInteger(e.planeIndex)||e.planeIndex<0)throw TypeError(`planeIndex must be a non-negative integer.`);if(e.format!==void 0&&!Hn.has(e.format))throw TypeError(`Invalid format.`);if(e.frameOffset!==void 0&&(!Number.isInteger(e.frameOffset)||e.frameOffset<0))throw TypeError(`frameOffset must be a non-negative integer.`);if(e.frameCount!==void 0&&(!Number.isInteger(e.frameCount)||e.frameCount<0))throw TypeError(`frameCount must be a non-negative integer.`);if(this._closed)throw Error(`AudioSample is closed.`);let t=e.format??this.format,n=e.frameOffset??0;if(n>=this.numberOfFrames)throw RangeError(`frameOffset out of range`);let r=e.frameCount===void 0?this.numberOfFrames-n:e.frameCount;if(r>this.numberOfFrames-n)throw RangeError(`frameCount out of range`);let i=Wn(t),a=Gn(t);if(a&&e.planeIndex>=this.numberOfChannels||!a&&e.planeIndex!==0)throw RangeError(`planeIndex out of range`);return(a?r:r*this.numberOfChannels)*i}copyTo(e,t){if(!Ie(e))throw TypeError(`destination must be an ArrayBuffer or an ArrayBuffer view.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(!Number.isInteger(t.planeIndex)||t.planeIndex<0)throw TypeError(`planeIndex must be a non-negative integer.`);if(t.format!==void 0&&!Hn.has(t.format))throw TypeError(`Invalid format.`);if(t.frameOffset!==void 0&&(!Number.isInteger(t.frameOffset)||t.frameOffset<0))throw TypeError(`frameOffset must be a non-negative integer.`);if(t.frameCount!==void 0&&(!Number.isInteger(t.frameCount)||t.frameCount<0))throw TypeError(`frameCount must be a non-negative integer.`);if(this._closed)throw Error(`AudioSample is closed.`);let{planeIndex:n,format:r,frameCount:i,frameOffset:a}=t,o=r??this.format;if(!o)throw Error(`Destination format not determined`);let s=this.numberOfFrames,c=this.numberOfChannels,l=a??0;if(l>=s)throw RangeError(`frameOffset out of range`);let u=i===void 0?s-l:i;if(u>s-l)throw RangeError(`frameCount out of range`);let d=Wn(o),f=Gn(o);if(f&&n>=c||!f&&n!==0)throw RangeError(`planeIndex out of range`);let p=(f?u:u*c)*d;if(e.byteLength<p)throw RangeError(`Destination buffer is too small`);let m=T(e),h=qn(o);if(Jn(this._data))if(f)if(o===`f32-planar`)this._data.copyTo(e,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});else{let e=new ArrayBuffer(u*4),t=new Float32Array(e);this._data.copyTo(t,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});let r=new DataView(e);for(let e=0;e<u;e++)h(m,e*d,r.getFloat32(e*4,!0))}else{let e=c,t=new Float32Array(u);for(let n=0;n<e;n++){this._data.copyTo(t,{planeIndex:n,frameOffset:l,frameCount:u,format:`f32-planar`});for(let r=0;r<u;r++)h(m,(r*e+n)*d,t[r])}}else{let e=this._data,t=T(e),r=this.format,i=Kn(r),a=Wn(r),o=Gn(r);for(let e=0;e<u;e++)if(f){let r=e*d,u;u=o?(n*s+(e+l))*a:((e+l)*c+n)*a,h(m,r,i(t,u))}else for(let n=0;n<c;n++){let r=(e*c+n)*d,u;u=o?(n*s+(e+l))*a:((e+l)*c+n)*a,h(m,r,i(t,u))}}}clone(){if(this._closed)throw Error(`AudioSample is closed.`);if(Jn(this._data)){let t=new e(this._data.clone());return t.setTimestamp(this.timestamp),t}else return new e({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.timestamp,data:this._data})}close(){this._closed||=(Rn?.unregister(this),Jn(this._data)?this._data.close():this._data=new Uint8Array,!0)}toAudioData(){if(this._closed)throw Error(`AudioSample is closed.`);if(Jn(this._data)){if(this._data.timestamp===this.microsecondTimestamp)return this._data.clone();if(Gn(this.format)){let e=this.allocationSize({planeIndex:0,format:this.format}),t=new ArrayBuffer(e*this.numberOfChannels);for(let n=0;n<this.numberOfChannels;n++)this.copyTo(new Uint8Array(t,n*e,e),{planeIndex:n,format:this.format});return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:t})}else{let e=new ArrayBuffer(this.allocationSize({planeIndex:0,format:this.format}));return this.copyTo(e,{planeIndex:0,format:this.format}),new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:e})}}else return new AudioData({format:this.format,sampleRate:this.sampleRate,numberOfFrames:this.numberOfFrames,numberOfChannels:this.numberOfChannels,timestamp:this.microsecondTimestamp,data:this._data.buffer instanceof ArrayBuffer?this._data.buffer:this._data.slice()})}toAudioBuffer(){if(this._closed)throw Error(`AudioSample is closed.`);let e=new AudioBuffer({numberOfChannels:this.numberOfChannels,length:this.numberOfFrames,sampleRate:this.sampleRate}),t=new Float32Array(this.allocationSize({planeIndex:0,format:`f32-planar`})/4);for(let n=0;n<this.numberOfChannels;n++)this.copyTo(t,{planeIndex:n,format:`f32-planar`}),e.copyToChannel(t,n);return e}setTimestamp(e){if(!Number.isFinite(e))throw TypeError(`newTimestamp must be a number.`);this.timestamp=e}[Symbol.dispose](){this.close()}static*_fromAudioBuffer(t,n){if(!(t instanceof AudioBuffer))throw TypeError(`audioBuffer must be an AudioBuffer.`);let r=t.numberOfChannels,i=t.sampleRate,a=t.length,o=Math.floor(24e4/r),s=0,c=a;for(;c>0;){let a=Math.min(o,c),l=new Float32Array(r*a);for(let e=0;e<r;e++)t.copyFromChannel(l.subarray(e*a,(e+1)*a),e,s);yield new e({format:`f32-planar`,sampleRate:i,numberOfFrames:a,numberOfChannels:r,timestamp:n+s/i,data:l}),s+=a,c-=a}}static fromAudioBuffer(t,n){if(!(t instanceof AudioBuffer))throw TypeError(`audioBuffer must be an AudioBuffer.`);let r=t.numberOfChannels,i=t.sampleRate,a=t.length,o=Math.floor(24e4/r),s=0,c=a,l=[];for(;c>0;){let a=Math.min(o,c),u=new Float32Array(r*a);for(let e=0;e<r;e++)t.copyFromChannel(u.subarray(e*a,(e+1)*a),e,s);let d=new e({format:`f32-planar`,sampleRate:i,numberOfFrames:a,numberOfChannels:r,timestamp:n+s/i,data:u});l.push(d),s+=a,c-=a}return l}};let Wn=e=>{switch(e){case`u8`:case`u8-planar`:return 1;case`s16`:case`s16-planar`:return 2;case`s32`:case`s32-planar`:return 4;case`f32`:case`f32-planar`:return 4;default:throw Error(`Unknown AudioSampleFormat`)}},Gn=e=>{switch(e){case`u8-planar`:case`s16-planar`:case`s32-planar`:case`f32-planar`:return!0;default:return!1}},Kn=e=>{switch(e){case`u8`:case`u8-planar`:return(e,t)=>(e.getUint8(t)-128)/128;case`s16`:case`s16-planar`:return(e,t)=>e.getInt16(t,!0)/32768;case`s32`:case`s32-planar`:return(e,t)=>e.getInt32(t,!0)/2147483648;case`f32`:case`f32-planar`:return(e,t)=>e.getFloat32(t,!0)}},qn=e=>{switch(e){case`u8`:case`u8-planar`:return(e,t,n)=>e.setUint8(t,k((n+1)*127.5,0,255));case`s16`:case`s16-planar`:return(e,t,n)=>e.setInt16(t,k(Math.round(n*32767),-32768,32767),!0);case`s32`:case`s32-planar`:return(e,t,n)=>e.setInt32(t,k(Math.round(n*2147483647),-2147483648,2147483647),!0);case`f32`:case`f32-planar`:return(e,t,n)=>e.setFloat32(t,n,!0)}},Jn=e=>typeof AudioData<`u`&&e instanceof AudioData,Yn=e=>{if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(e.metadataOnly!==void 0&&typeof e.metadataOnly!=`boolean`)throw TypeError(`options.metadataOnly, when defined, must be a boolean.`);if(e.verifyKeyPackets!==void 0&&typeof e.verifyKeyPackets!=`boolean`)throw TypeError(`options.verifyKeyPackets, when defined, must be a boolean.`);if(e.verifyKeyPackets&&e.metadataOnly)throw TypeError(`options.verifyKeyPackets and options.metadataOnly cannot be enabled together.`)},Xn=e=>{if(!ht(e))throw TypeError(`timestamp must be a number.`)},Zn=(e,t,n)=>n.verifyKeyPackets?t.then(async t=>{if(!t||t.type===`delta`)return t;let n=await e.determinePacketType(t);return n&&(t.type=n),t}):t;var Qn=class{constructor(e){if(!(e instanceof lr))throw TypeError(`track must be an InputTrack.`);this._track=e}getFirstPacket(e={}){if(Yn(e),this._track.input._disposed)throw new L;return Zn(this._track,this._track._backing.getFirstPacket(e),e)}getPacket(e,t={}){if(Xn(e),Yn(t),this._track.input._disposed)throw new L;return Zn(this._track,this._track._backing.getPacket(e,t),t)}getNextPacket(e,t={}){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);if(Yn(t),this._track.input._disposed)throw new L;return Zn(this._track,this._track._backing.getNextPacket(e,t),t)}async getKeyPacket(e,t={}){if(Xn(e),Yn(t),this._track.input._disposed)throw new L;if(!t.verifyKeyPackets)return this._track._backing.getKeyPacket(e,t);let n=await this._track._backing.getKeyPacket(e,t);return n&&(b(n.type===`key`),await this._track.determinePacketType(n)===`delta`?this.getKeyPacket(n.timestamp-1/this._track.timeResolution,t):n)}async getNextKeyPacket(e,t={}){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);if(Yn(t),this._track.input._disposed)throw new L;if(!t.verifyKeyPackets)return this._track._backing.getNextKeyPacket(e,t);let n=await this._track._backing.getNextKeyPacket(e,t);return n&&(b(n.type===`key`),await this._track.determinePacketType(n)===`delta`?this.getNextKeyPacket(n,t):n)}packets(e,t,n={}){if(e!==void 0&&!(e instanceof P))throw TypeError(`startPacket must be an EncodedPacket.`);if(e!==void 0&&e.isMetadataOnly&&!n?.metadataOnly)throw TypeError(`startPacket can only be metadata-only if options.metadataOnly is enabled.`);if(t!==void 0&&!(t instanceof P))throw TypeError(`endPacket must be an EncodedPacket.`);if(Yn(n),this._track.input._disposed)throw new L;let r=[],{promise:i,resolve:a}=O(),{promise:o,resolve:s}=O(),c=!1,l=!1,u=null,d=[],f=()=>Math.max(2,d.length);(async()=>{let u=e??await this.getFirstPacket(n);for(;u&&!l&&!this._track.input._disposed&&!(t&&u.sequenceNumber>=t?.sequenceNumber);){if(r.length>f()){({promise:o,resolve:s}=O()),await o;continue}r.push(u),a(),{promise:i,resolve:a}=O(),u=await this.getNextPacket(u,n)}c=!0,a()})().catch(e=>{u||(u=e,a())});let p=this._track;return{async next(){for(;;)if(p.input._disposed)throw new L;else if(l)return{value:void 0,done:!0};else if(u)throw u;else if(r.length>0){let e=r.shift(),t=performance.now();for(d.push(t);d.length>0&&t-d[0]>=1e3;)d.shift();return s(),{value:e,done:!1}}else if(c)return{value:void 0,done:!0};else await i},async return(){return l=!0,s(),a(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}},$n=class{constructor(e,t){this.onSample=e,this.onError=t}},er=class{mediaSamplesInRange(e=0,t=1/0){Xn(e),Xn(t);let n=[],r=!1,i=null,{promise:a,resolve:o}=O(),{promise:s,resolve:c}=O(),l=!1,u=!1,d=!1,f=null;(async()=>{let p=Error(),m=await this._createDecoder(s=>{if(c(),s.timestamp>=t&&(u=!0),u){s.close();return}i&&(s.timestamp>e?(n.push(i),r=!0):i.close()),s.timestamp>=e&&(n.push(s),r=!0),i=r?null:s,n.length>0&&(o(),{promise:a,resolve:o}=O())},e=>{f||(e.stack=p.stack,f=e,o())}),h=this._createPacketSink(),g=await h.getKeyPacket(e,{verifyKeyPackets:!0})??await h.getFirstPacket(),_=g,v;if(t<1/0){let e=await h.getPacket(t),n=e?e.type===`key`&&e.timestamp===t?e:await h.getNextKeyPacket(e,{verifyKeyPackets:!0}):null;n&&(v=n)}let ee=h.packets(g??void 0,v);for(await ee.next();_&&!u&&!this._track.input._disposed;){let e=tr(n.length);if(n.length+m.getDecodeQueueSize()>e){({promise:s,resolve:c}=O()),await s;continue}m.decode(_);let t=await ee.next();if(t.done)break;_=t.value}await ee.return(),!d&&!this._track.input._disposed&&await m.flush(),m.close(),!r&&i&&n.push(i),l=!0,o()})().catch(e=>{f||(f=e,o())});let p=this._track,m=()=>{i?.close();for(let e of n)e.close()};return{async next(){for(;;)if(p.input._disposed)throw m(),new L;else if(d)return{value:void 0,done:!0};else if(f)throw m(),f;else if(n.length>0){let e=n.shift();return c(),{value:e,done:!1}}else if(!l)await a;else return{value:void 0,done:!0}},async return(){return d=!0,u=!0,c(),o(),m(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}mediaSamplesAtTimestamps(e){We(e);let t=Ue(e),n=[],r=[],{promise:i,resolve:a}=O(),{promise:o,resolve:s}=O(),c=!1,l=!1,u=null,d=e=>{r.push(e),a(),{promise:i,resolve:a}=O()};(async()=>{let e=Error(),i=await this._createDecoder(e=>{if(s(),l){e.close();return}let t=0;for(;n.length>0&&e.timestamp-n[0]>-1e-10;)t++,n.shift();if(t>0)for(let n=0;n<t;n++)d(n<t-1?e.clone():e);else e.close()},t=>{u||(t.stack=e.stack,u=t,a())}),f=this._createPacketSink(),p=null,m=null,h=-1,g=async()=>{b(m);let e=m;for(i.decode(e);e.sequenceNumber<h;){let t=tr(r.length);for(;r.length+i.getDecodeQueueSize()>t&&!l;)({promise:o,resolve:s}=O()),await o;if(l)break;let n=await f.getNextPacket(e);b(n),i.decode(n),e=n}h=-1},_=async()=>{await i.flush();for(let e=0;e<n.length;e++)d(null);n.length=0};for await(let e of t){if(Xn(e),l||this._track.input._disposed)break;let t=await f.getPacket(e),r=t&&await f.getKeyPacket(e,{verifyKeyPackets:!0});if(!r){h!==-1&&(await g(),await _()),d(null),p=null;continue}p&&(r.sequenceNumber!==m.sequenceNumber||t.timestamp<p.timestamp)&&(await g(),await _()),n.push(t.timestamp),h=Math.max(t.sequenceNumber,h),p=t,m=r}!l&&!this._track.input._disposed&&(h!==-1&&await g(),await _()),i.close(),c=!0,a()})().catch(e=>{u||(u=e,a())});let f=this._track,p=()=>{for(let e of r)e?.close()};return{async next(){for(;;)if(f.input._disposed)throw p(),new L;else if(l)return{value:void 0,done:!0};else if(u)throw p(),u;else if(r.length>0){let e=r.shift();return b(e!==void 0),s(),{value:e,done:!1}}else if(!c)await i;else return{value:void 0,done:!0}},async return(){return l=!0,s(),a(),p(),{value:void 0,done:!0}},async throw(e){throw e},[Symbol.asyncIterator](){return this}}}};let tr=e=>e===0?40:8;var nr=class extends $n{constructor(e,t,n,r,i,a){super(e,t),this.codec=n,this.decoderConfig=r,this.rotation=i,this.timeResolution=a,this.decoder=null,this.customDecoder=null,this.customDecoderCallSerializer=new rt,this.customDecoderQueueSize=0,this.inputTimestamps=[],this.sampleQueue=[],this.currentPacketIndex=0,this.raslSkipped=!1,this.alphaDecoder=null,this.alphaHadKeyframe=!1,this.colorQueue=[],this.alphaQueue=[],this.merger=null,this.mergerCreationFailed=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue=[],this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1;let o=Dn.find(e=>e.supports(n,r));if(o)this.customDecoder=new o,this.customDecoder.codec=n,this.customDecoder.config=r,this.customDecoder.onSample=e=>{if(!(e instanceof F))throw TypeError(`The argument passed to onSample must be a VideoSample.`);this.finalizeAndEmitSample(e)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init());else{let e=e=>{if(this.alphaQueue.length>0){let t=this.alphaQueue.shift();b(t!==void 0),this.mergeAlpha(e,t)}else this.colorQueue.push(e)};if(n===`avc`&&this.decoderConfig.description&<()){let e=sn(w(this.decoderConfig.description));if(e&&e.sequenceParameterSets.length>0){let t=cn(e.sequenceParameterSets[0]);t&&t.frameMbsOnlyFlag===0&&(this.decoderConfig={...this.decoderConfig,hardwareAcceleration:`prefer-software`})}}this.decoder=new VideoDecoder({output:t=>{try{e(t)}catch(e){this.onError(e)}},error:t}),this.decoder.configure(this.decoderConfig)}}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(b(this.decoder),Math.max(this.decoder.decodeQueueSize,this.alphaDecoder?.decodeQueueSize??0))}decode(e){if(this.codec===`hevc`&&this.currentPacketIndex>0&&!this.raslSkipped){if(this.hasHevcRaslPicture(e.data))return;this.raslSkipped=!0}this.currentPacketIndex++,this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(b(this.decoder),at()||Ve(this.inputTimestamps,e.timestamp,e=>e),this.decoder.decode(e.toEncodedVideoChunk()),this.decodeAlphaData(e))}decodeAlphaData(e){if(!e.sideData.alpha||this.mergerCreationFailed){this.pushNullAlphaFrame();return}if(!this.merger)try{this.merger=new rr}catch(t){console.error(`Due to an error, only color data will be decoded.`,t),this.mergerCreationFailed=!0,this.decodeAlphaData(e);return}if(!this.alphaDecoder){let e=e=>{if(this.alphaDecoderQueueSize--,this.colorQueue.length>0){let t=this.colorQueue.shift();b(t!==void 0),this.mergeAlpha(t,e)}else this.alphaQueue.push(e);for(this.decodedAlphaChunkCount++;this.nullAlphaFrameQueue.length>0&&this.nullAlphaFrameQueue[0]===this.decodedAlphaChunkCount;)if(this.nullAlphaFrameQueue.shift(),this.colorQueue.length>0){let e=this.colorQueue.shift();b(e!==void 0),this.mergeAlpha(e,null)}else this.alphaQueue.push(null)};this.alphaDecoder=new VideoDecoder({output:t=>{try{e(t)}catch(e){this.onError(e)}},error:this.onError}),this.alphaDecoder.configure(this.decoderConfig)}let t=wn(this.codec,this.decoderConfig,e.sideData.alpha);if(this.alphaHadKeyframe||=t===`key`,this.alphaHadKeyframe){if(this.codec===`hevc`&&this.currentAlphaPacketIndex>0&&!this.alphaRaslSkipped){if(this.hasHevcRaslPicture(e.sideData.alpha)){this.pushNullAlphaFrame();return}this.alphaRaslSkipped=!0}this.currentAlphaPacketIndex++,this.alphaDecoder.decode(e.alphaToEncodedVideoChunk(t??e.type)),this.alphaDecoderQueueSize++}else this.pushNullAlphaFrame()}pushNullAlphaFrame(){this.alphaDecoderQueueSize===0?this.alphaQueue.push(null):this.nullAlphaFrameQueue.push(this.decodedAlphaChunkCount+this.alphaDecoderQueueSize)}hasHevcRaslPicture(e){return ln(e,this.decoderConfig).some(e=>{let t=un(e);return t===N.RASL_N||t===N.RASL_R})}sampleHandler(e){if(at()){if(this.sampleQueue.length>0&&e.timestamp>=x(this.sampleQueue).timestamp){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}Ve(this.sampleQueue,e,e=>e.timestamp)}else{let t=this.inputTimestamps.shift();b(t!==void 0),e.setTimestamp(t),this.finalizeAndEmitSample(e)}}finalizeAndEmitSample(e){e.setTimestamp(Math.round(e.timestamp*this.timeResolution)/this.timeResolution),e.setDuration(Math.round(e.duration*this.timeResolution)/this.timeResolution),e.setRotation(this.rotation),this.onSample(e)}mergeAlpha(e,t){if(!t){let t=new F(e);this.sampleHandler(t);return}b(this.merger),this.merger.update(e,t),e.close(),t.close();let n=new F(new VideoFrame(this.merger.canvas,{timestamp:e.timestamp,duration:e.duration??void 0}));this.sampleHandler(n)}async flush(){if(this.customDecoder?await this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(b(this.decoder),await Promise.all([this.decoder.flush(),this.alphaDecoder?.flush()]),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.alphaHadKeyframe=!1,this.decodedAlphaChunkCount=0,this.alphaDecoderQueueSize=0,this.nullAlphaFrameQueue.length=0,this.currentAlphaPacketIndex=0,this.alphaRaslSkipped=!1),at()){for(let e of this.sampleQueue)this.finalizeAndEmitSample(e);this.sampleQueue.length=0}this.currentPacketIndex=0,this.raslSkipped=!1}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(b(this.decoder),this.decoder.close(),this.alphaDecoder?.close(),this.colorQueue.forEach(e=>e.close()),this.colorQueue.length=0,this.alphaQueue.forEach(e=>e?.close()),this.alphaQueue.length=0,this.merger?.close());for(let e of this.sampleQueue)e.close();this.sampleQueue.length=0}},rr=class{constructor(){typeof OffscreenCanvas<`u`?this.canvas=new OffscreenCanvas(300,150):this.canvas=document.createElement(`canvas`);let e=this.canvas.getContext(`webgl2`,{premultipliedAlpha:!1});if(!e)throw Error(`Couldn't acquire WebGL 2 context.`);this.gl=e,this.program=this.createProgram(),this.vao=this.createVAO(),this.colorTexture=this.createTexture(),this.alphaTexture=this.createTexture(),this.gl.useProgram(this.program),this.gl.uniform1i(this.gl.getUniformLocation(this.program,`u_colorTexture`),0),this.gl.uniform1i(this.gl.getUniformLocation(this.program,`u_alphaTexture`),1)}createProgram(){let e=this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
|
|
50
50
|
in vec2 a_position;
|
|
51
51
|
in vec2 a_texCoord;
|
|
52
52
|
out vec2 v_texCoord;
|
|
@@ -68,7 +68,7 @@ ft();let Pn=-1/0,Fn=-1/0,In=null;typeof FinalizationRegistry<`u`&&(In=new Finali
|
|
|
68
68
|
float alpha = texture(u_alphaTexture, v_texCoord).r;
|
|
69
69
|
fragColor = vec4(color, alpha);
|
|
70
70
|
}
|
|
71
|
-
`),n=this.gl.createProgram();return this.gl.attachShader(n,e),this.gl.attachShader(n,t),this.gl.linkProgram(n),n}createShader(e,t){let n=this.gl.createShader(e);return this.gl.shaderSource(n,t),this.gl.compileShader(n),n}createVAO(){let e=this.gl.createVertexArray();this.gl.bindVertexArray(e);let t=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),n=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,n),this.gl.bufferData(this.gl.ARRAY_BUFFER,t,this.gl.STATIC_DRAW);let r=this.gl.getAttribLocation(this.program,`a_position`),i=this.gl.getAttribLocation(this.program,`a_texCoord`);return this.gl.enableVertexAttribArray(r),this.gl.vertexAttribPointer(r,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,8),e}createTexture(){let e=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),e}update(e,t){(e.displayWidth!==this.canvas.width||e.displayHeight!==this.canvas.height)&&(this.canvas.width=e.displayWidth,this.canvas.height=e.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.colorTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.gl.activeTexture(this.gl.TEXTURE1),this.gl.bindTexture(this.gl.TEXTURE_2D,this.alphaTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4)}close(){this.gl.getExtension(`WEBGL_lose_context`)?.loseContext(),this.gl=null}},rr=class extends $n{constructor(e){if(!(e instanceof lr))throw TypeError(`videoTrack must be an InputVideoTrack.`);super(),this._track=e}async _createDecoder(e,t){if(!await this._track.canDecode())throw Error(`This video track cannot be decoded by this browser. Make sure to check decodability before using a track.`);let n=this._track.codec,r=this._track.rotation,i=await this._track.getDecoderConfig(),a=this._track.timeResolution;return x(n&&i),new tr(e,t,n,i,r,a)}_createPacketSink(){return new Zn(this._track)}async getSample(e){Yn(e);for await(let t of this.mediaSamplesAtTimestamps([e]))return t;throw Error(`Internal error: Iterator returned nothing.`)}samples(e=0,t=1/0){return this.mediaSamplesInRange(e,t)}samplesAtTimestamps(e){return this.mediaSamplesAtTimestamps(e)}},ir=class{constructor(e,t={}){if(this._nextCanvasIndex=0,!(e instanceof lr))throw TypeError(`videoTrack must be an InputVideoTrack.`);if(t&&typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.alpha!==void 0&&typeof t.alpha!=`boolean`)throw TypeError(`options.alpha, when provided, must be a boolean.`);if(t.width!==void 0&&(!Number.isInteger(t.width)||t.width<=0))throw TypeError(`options.width, when defined, must be a positive integer.`);if(t.height!==void 0&&(!Number.isInteger(t.height)||t.height<=0))throw TypeError(`options.height, when defined, must be a positive integer.`);if(t.fit!==void 0&&![`fill`,`contain`,`cover`].includes(t.fit))throw TypeError(`options.fit, when provided, must be one of "fill", "contain", or "cover".`);if(t.width!==void 0&&t.height!==void 0&&t.fit===void 0)throw TypeError(`When both options.width and options.height are provided, options.fit must also be provided.`);if(t.rotation!==void 0&&![0,90,180,270].includes(t.rotation))throw TypeError(`options.rotation, when provided, must be 0, 90, 180 or 270.`);if(t.crop!==void 0&&Bn(t.crop,`options.`),t.poolSize!==void 0&&(typeof t.poolSize!=`number`||!Number.isInteger(t.poolSize)||t.poolSize<0))throw TypeError(`poolSize must be a non-negative integer.`);let n=t.rotation??e.rotation,[r,i]=n%180==0?[e.codedWidth,e.codedHeight]:[e.codedHeight,e.codedWidth],a=t.crop;a&&zn(a,r,i);let[o,s]=a?[a.width,a.height]:[r,i],c=o/s;t.width!==void 0&&t.height===void 0?(o=t.width,s=Math.round(o/c)):t.width===void 0&&t.height!==void 0?(s=t.height,o=Math.round(s*c)):t.width!==void 0&&t.height!==void 0&&(o=t.width,s=t.height),this._videoTrack=e,this._alpha=t.alpha??!1,this._width=o,this._height=s,this._rotation=n,this._crop=a,this._fit=t.fit??`fill`,this._videoSampleSink=new rr(e),this._canvasPool=Array.from({length:t.poolSize??0},()=>null)}_videoSampleToWrappedCanvas(e){let t=this._canvasPool[this._nextCanvasIndex],n=!1;t||(typeof document<`u`?(t=document.createElement(`canvas`),t.width=this._width,t.height=this._height):t=new OffscreenCanvas(this._width,this._height),this._canvasPool.length>0&&(this._canvasPool[this._nextCanvasIndex]=t),n=!0),this._canvasPool.length>0&&(this._nextCanvasIndex=(this._nextCanvasIndex+1)%this._canvasPool.length);let r=t.getContext(`2d`,{alpha:this._alpha||at()});x(r),r.resetTransform(),n||(!this._alpha&&at()?(r.fillStyle=`black`,r.fillRect(0,0,this._width,this._height)):r.clearRect(0,0,this._width,this._height)),e.drawWithFit(r,{fit:this._fit,rotation:this._rotation,crop:this._crop});let i={canvas:t,timestamp:e.timestamp,duration:e.duration};return e.close(),i}async getCanvas(e){Yn(e);let t=await this._videoSampleSink.getSample(e);return t&&this._videoSampleToWrappedCanvas(t)}canvases(e=0,t=1/0){return Je(this._videoSampleSink.samples(e,t),e=>this._videoSampleToWrappedCanvas(e))}canvasesAtTimestamps(e){return Je(this._videoSampleSink.samplesAtTimestamps(e),e=>e&&this._videoSampleToWrappedCanvas(e))}},ar=class extends Qn{constructor(e,t,n,r){super(e,t),this.decoder=null,this.customDecoder=null,this.customDecoderCallSerializer=new tt,this.customDecoderQueueSize=0,this.currentTimestamp=null;let i=t=>{(this.currentTimestamp===null||Math.abs(t.timestamp-this.currentTimestamp)>=t.duration)&&(this.currentTimestamp=t.timestamp);let n=this.currentTimestamp;if(this.currentTimestamp+=t.duration,t.numberOfFrames===0){t.close();return}let i=r.sampleRate;t.setTimestamp(Math.round(n*i)/i),e(t)},a=En.find(e=>e.supports(n,r));a?(this.customDecoder=new a,this.customDecoder.codec=n,this.customDecoder.config=r,this.customDecoder.onSample=e=>{if(!(e instanceof Hn))throw TypeError(`The argument passed to onSample must be an AudioSample.`);i(e)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init())):(this.decoder=new AudioDecoder({output:e=>{try{i(new Hn(e))}catch(e){this.onError(e)}},error:t}),this.decoder.configure(r))}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(x(this.decoder),this.decoder.decodeQueueSize)}decode(e){this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(x(this.decoder),this.decoder.decode(e.toEncodedAudioChunk()))}flush(){return this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(x(this.decoder),this.decoder.flush())}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(x(this.decoder),this.decoder.close())}},or=class extends Qn{constructor(e,t,n){super(e,t),this.decoderConfig=n,this.currentTimestamp=null,x(M.includes(n.codec)),this.codec=n.codec;let{dataType:r,sampleSize:i,littleEndian:a}=Ft(this.codec);switch(this.inputSampleSize=i,i){case 1:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint8(t)-2**7:r===`signed`?this.readInputValue=(e,t)=>e.getInt8(t):r===`ulaw`?this.readInputValue=(e,t)=>jn(e.getUint8(t)):r===`alaw`?this.readInputValue=(e,t)=>Nn(e.getUint8(t)):x(!1);break;case 2:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint16(t,a)-2**15:r===`signed`?this.readInputValue=(e,t)=>e.getInt16(t,a):x(!1);break;case 3:r===`unsigned`?this.readInputValue=(e,t)=>We(e,t,a)-2**23:r===`signed`?this.readInputValue=(e,t)=>Ge(e,t,a):x(!1);break;case 4:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint32(t,a)-2**31:r===`signed`?this.readInputValue=(e,t)=>e.getInt32(t,a):r===`float`?this.readInputValue=(e,t)=>e.getFloat32(t,a):x(!1);break;case 8:r===`float`?this.readInputValue=(e,t)=>e.getFloat64(t,a):x(!1);break;default:Ue(i),x(!1)}switch(i){case 1:r===`ulaw`||r===`alaw`?(this.outputSampleSize=2,this.outputFormat=`s16`,this.writeOutputValue=(e,t,n)=>e.setInt16(t,n,!0)):(this.outputSampleSize=1,this.outputFormat=`u8`,this.writeOutputValue=(e,t,n)=>e.setUint8(t,n+2**7));break;case 2:this.outputSampleSize=2,this.outputFormat=`s16`,this.writeOutputValue=(e,t,n)=>e.setInt16(t,n,!0);break;case 3:this.outputSampleSize=4,this.outputFormat=`s32`,this.writeOutputValue=(e,t,n)=>e.setInt32(t,n<<8,!0);break;case 4:this.outputSampleSize=4,r===`float`?(this.outputFormat=`f32`,this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,!0)):(this.outputFormat=`s32`,this.writeOutputValue=(e,t,n)=>e.setInt32(t,n,!0));break;case 8:this.outputSampleSize=4,this.outputFormat=`f32`,this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,!0);break;default:Ue(i),x(!1)}}getDecodeQueueSize(){return 0}decode(e){let t=E(e.data),n=e.byteLength/this.decoderConfig.numberOfChannels/this.inputSampleSize,r=n*this.decoderConfig.numberOfChannels*this.outputSampleSize,i=new ArrayBuffer(r),a=new DataView(i);for(let e=0;e<n*this.decoderConfig.numberOfChannels;e++){let n=e*this.inputSampleSize,r=e*this.outputSampleSize,i=this.readInputValue(t,n);this.writeOutputValue(a,r,i)}let o=n/this.decoderConfig.sampleRate;(this.currentTimestamp===null||Math.abs(e.timestamp-this.currentTimestamp)>=o)&&(this.currentTimestamp=e.timestamp);let s=this.currentTimestamp;this.currentTimestamp+=o;let c=new Hn({format:this.outputFormat,data:i,numberOfChannels:this.decoderConfig.numberOfChannels,sampleRate:this.decoderConfig.sampleRate,numberOfFrames:n,timestamp:s});this.onSample(c)}async flush(){}close(){}},sr=class extends $n{constructor(e){if(!(e instanceof ur))throw TypeError(`audioTrack must be an InputAudioTrack.`);super(),this._track=e}async _createDecoder(e,t){if(!await this._track.canDecode())throw Error(`This audio track cannot be decoded by this browser. Make sure to check decodability before using a track.`);let n=this._track.codec,r=await this._track.getDecoderConfig();return x(n&&r),M.includes(r.codec)?new or(e,t,r):new ar(e,t,n,r)}_createPacketSink(){return new Zn(this._track)}async getSample(e){Yn(e);for await(let t of this.mediaSamplesAtTimestamps([e]))return t;throw Error(`Internal error: Iterator returned nothing.`)}samples(e=0,t=1/0){return this.mediaSamplesInRange(e,t)}samplesAtTimestamps(e){return this.mediaSamplesAtTimestamps(e)}},cr=class{constructor(e,t){this.input=e,this._backing=t}isVideoTrack(){return this instanceof lr}isAudioTrack(){return this instanceof ur}get id(){return this._backing.getId()}get internalCodecId(){return this._backing.getInternalCodecId()}get languageCode(){return this._backing.getLanguageCode()}get name(){return this._backing.getName()}get timeResolution(){return this._backing.getTimeResolution()}get disposition(){return this._backing.getDisposition()}getFirstTimestamp(){return this._backing.getFirstTimestamp()}computeDuration(){return this._backing.computeDuration()}async computePacketStats(e=1/0){let t=new Zn(this),n=1/0,r=-1/0,i=0,a=0;for await(let o of t.packets(void 0,void 0,{metadataOnly:!0})){if(i>=e&&o.timestamp>=r)break;n=Math.min(n,o.timestamp),r=Math.max(r,o.timestamp+o.duration),i++,a+=o.byteLength}return{packetCount:i,averagePacketRate:i?Number((i/(r-n)).toPrecision(16)):0,averageBitrate:i?Number((8*a/(r-n)).toPrecision(16)):0}}},lr=class extends cr{constructor(e,t){super(e,t),this._backing=t}get type(){return`video`}get codec(){return this._backing.getCodec()}get codedWidth(){return this._backing.getCodedWidth()}get codedHeight(){return this._backing.getCodedHeight()}get rotation(){return this._backing.getRotation()}get displayWidth(){return this._backing.getRotation()%180==0?this._backing.getCodedWidth():this._backing.getCodedHeight()}get displayHeight(){return this._backing.getRotation()%180==0?this._backing.getCodedHeight():this._backing.getCodedWidth()}getColorSpace(){return this._backing.getColorSpace()}async hasHighDynamicRange(){let e=await this._backing.getColorSpace();return e.primaries===`bt2020`||e.primaries===`smpte432`||e.transfer===`pg`||e.transfer===`hlg`||e.matrix===`bt2020-ncl`}canBeTransparent(){return this._backing.canBeTransparent()}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let t=this._backing.getCodec();return x(t!==null),Tn.some(n=>n.supports(t,e))?!0:typeof VideoDecoder>`u`?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error(`Error during decodability check:`,e),!1}}async determinePacketType(e){if(!(e instanceof F))throw TypeError(`packet must be an EncodedPacket.`);if(e.isMetadataOnly)throw TypeError(`packet must not be metadata-only to determine its type.`);if(this.codec===null)return null;let t=await this.getDecoderConfig();return x(t),Sn(this.codec,t,e.data)}},ur=class extends cr{constructor(e,t){super(e,t),this._backing=t}get type(){return`audio`}get codec(){return this._backing.getCodec()}get numberOfChannels(){return this._backing.getNumberOfChannels()}get sampleRate(){return this._backing.getSampleRate()}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let t=this._backing.getCodec();return x(t!==null),En.some(n=>n.supports(t,e))||e.codec.startsWith(`pcm-`)?!0:typeof AudioDecoder>`u`?!1:(await AudioDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error(`Error during decodability check:`,e),!1}}async determinePacketType(e){if(!(e instanceof F))throw TypeError(`packet must be an EncodedPacket.`);return this.codec===null?null:`key`}};
|
|
71
|
+
`),n=this.gl.createProgram();return this.gl.attachShader(n,e),this.gl.attachShader(n,t),this.gl.linkProgram(n),n}createShader(e,t){let n=this.gl.createShader(e);return this.gl.shaderSource(n,t),this.gl.compileShader(n),n}createVAO(){let e=this.gl.createVertexArray();this.gl.bindVertexArray(e);let t=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),n=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,n),this.gl.bufferData(this.gl.ARRAY_BUFFER,t,this.gl.STATIC_DRAW);let r=this.gl.getAttribLocation(this.program,`a_position`),i=this.gl.getAttribLocation(this.program,`a_texCoord`);return this.gl.enableVertexAttribArray(r),this.gl.vertexAttribPointer(r,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,8),e}createTexture(){let e=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),e}update(e,t){(e.displayWidth!==this.canvas.width||e.displayHeight!==this.canvas.height)&&(this.canvas.width=e.displayWidth,this.canvas.height=e.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.colorTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.gl.activeTexture(this.gl.TEXTURE1),this.gl.bindTexture(this.gl.TEXTURE_2D,this.alphaTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,t),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4)}close(){this.gl.getExtension(`WEBGL_lose_context`)?.loseContext(),this.gl=null}},ir=class extends er{constructor(e){if(!(e instanceof ur))throw TypeError(`videoTrack must be an InputVideoTrack.`);super(),this._track=e}async _createDecoder(e,t){if(!await this._track.canDecode())throw Error(`This video track cannot be decoded by this browser. Make sure to check decodability before using a track.`);let n=this._track.codec,r=this._track.rotation,i=await this._track.getDecoderConfig(),a=this._track.timeResolution;return b(n&&i),new nr(e,t,n,i,r,a)}_createPacketSink(){return new Qn(this._track)}async getSample(e){Xn(e);for await(let t of this.mediaSamplesAtTimestamps([e]))return t;throw Error(`Internal error: Iterator returned nothing.`)}samples(e=0,t=1/0){return this.mediaSamplesInRange(e,t)}samplesAtTimestamps(e){return this.mediaSamplesAtTimestamps(e)}},ar=class{constructor(e,t={}){if(this._nextCanvasIndex=0,!(e instanceof ur))throw TypeError(`videoTrack must be an InputVideoTrack.`);if(t&&typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.alpha!==void 0&&typeof t.alpha!=`boolean`)throw TypeError(`options.alpha, when provided, must be a boolean.`);if(t.width!==void 0&&(!Number.isInteger(t.width)||t.width<=0))throw TypeError(`options.width, when defined, must be a positive integer.`);if(t.height!==void 0&&(!Number.isInteger(t.height)||t.height<=0))throw TypeError(`options.height, when defined, must be a positive integer.`);if(t.fit!==void 0&&![`fill`,`contain`,`cover`].includes(t.fit))throw TypeError(`options.fit, when provided, must be one of "fill", "contain", or "cover".`);if(t.width!==void 0&&t.height!==void 0&&t.fit===void 0)throw TypeError(`When both options.width and options.height are provided, options.fit must also be provided.`);if(t.rotation!==void 0&&![0,90,180,270].includes(t.rotation))throw TypeError(`options.rotation, when provided, must be 0, 90, 180 or 270.`);if(t.crop!==void 0&&Vn(t.crop,`options.`),t.poolSize!==void 0&&(typeof t.poolSize!=`number`||!Number.isInteger(t.poolSize)||t.poolSize<0))throw TypeError(`poolSize must be a non-negative integer.`);let n=t.rotation??e.rotation,[r,i]=n%180==0?[e.codedWidth,e.codedHeight]:[e.codedHeight,e.codedWidth],a=t.crop;a&&Bn(a,r,i);let[o,s]=a?[a.width,a.height]:[r,i],c=o/s;t.width!==void 0&&t.height===void 0?(o=t.width,s=Math.round(o/c)):t.width===void 0&&t.height!==void 0?(s=t.height,o=Math.round(s*c)):t.width!==void 0&&t.height!==void 0&&(o=t.width,s=t.height),this._videoTrack=e,this._alpha=t.alpha??!1,this._width=o,this._height=s,this._rotation=n,this._crop=a,this._fit=t.fit??`fill`,this._videoSampleSink=new ir(e),this._canvasPool=Array.from({length:t.poolSize??0},()=>null)}_videoSampleToWrappedCanvas(e){let t=this._canvasPool[this._nextCanvasIndex],n=!1;t||(typeof document<`u`?(t=document.createElement(`canvas`),t.width=this._width,t.height=this._height):t=new OffscreenCanvas(this._width,this._height),this._canvasPool.length>0&&(this._canvasPool[this._nextCanvasIndex]=t),n=!0),this._canvasPool.length>0&&(this._nextCanvasIndex=(this._nextCanvasIndex+1)%this._canvasPool.length);let r=t.getContext(`2d`,{alpha:this._alpha||st()});b(r),r.resetTransform(),n||(!this._alpha&&st()?(r.fillStyle=`black`,r.fillRect(0,0,this._width,this._height)):r.clearRect(0,0,this._width,this._height)),e.drawWithFit(r,{fit:this._fit,rotation:this._rotation,crop:this._crop});let i={canvas:t,timestamp:e.timestamp,duration:e.duration};return e.close(),i}async getCanvas(e){Xn(e);let t=await this._videoSampleSink.getSample(e);return t&&this._videoSampleToWrappedCanvas(t)}canvases(e=0,t=1/0){return Xe(this._videoSampleSink.samples(e,t),e=>this._videoSampleToWrappedCanvas(e))}canvasesAtTimestamps(e){return Xe(this._videoSampleSink.samplesAtTimestamps(e),e=>e&&this._videoSampleToWrappedCanvas(e))}},or=class extends $n{constructor(e,t,n,r){super(e,t),this.decoder=null,this.customDecoder=null,this.customDecoderCallSerializer=new rt,this.customDecoderQueueSize=0,this.currentTimestamp=null;let i=t=>{(this.currentTimestamp===null||Math.abs(t.timestamp-this.currentTimestamp)>=t.duration)&&(this.currentTimestamp=t.timestamp);let n=this.currentTimestamp;if(this.currentTimestamp+=t.duration,t.numberOfFrames===0){t.close();return}let i=r.sampleRate;t.setTimestamp(Math.round(n*i)/i),e(t)},a=On.find(e=>e.supports(n,r));a?(this.customDecoder=new a,this.customDecoder.codec=n,this.customDecoder.config=r,this.customDecoder.onSample=e=>{if(!(e instanceof Un))throw TypeError(`The argument passed to onSample must be an AudioSample.`);i(e)},this.customDecoderCallSerializer.call(()=>this.customDecoder.init())):(this.decoder=new AudioDecoder({output:e=>{try{i(new Un(e))}catch(e){this.onError(e)}},error:t}),this.decoder.configure(r))}getDecodeQueueSize(){return this.customDecoder?this.customDecoderQueueSize:(b(this.decoder),this.decoder.decodeQueueSize)}decode(e){this.customDecoder?(this.customDecoderQueueSize++,this.customDecoderCallSerializer.call(()=>this.customDecoder.decode(e)).then(()=>this.customDecoderQueueSize--)):(b(this.decoder),this.decoder.decode(e.toEncodedAudioChunk()))}flush(){return this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.flush()):(b(this.decoder),this.decoder.flush())}close(){this.customDecoder?this.customDecoderCallSerializer.call(()=>this.customDecoder.close()):(b(this.decoder),this.decoder.close())}},sr=class extends $n{constructor(e,t,n){super(e,t),this.decoderConfig=n,this.currentTimestamp=null,b(j.includes(n.codec)),this.codec=n.codec;let{dataType:r,sampleSize:i,littleEndian:a}=Lt(this.codec);switch(this.inputSampleSize=i,i){case 1:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint8(t)-2**7:r===`signed`?this.readInputValue=(e,t)=>e.getInt8(t):r===`ulaw`?this.readInputValue=(e,t)=>Nn(e.getUint8(t)):r===`alaw`?this.readInputValue=(e,t)=>Fn(e.getUint8(t)):b(!1);break;case 2:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint16(t,a)-2**15:r===`signed`?this.readInputValue=(e,t)=>e.getInt16(t,a):b(!1);break;case 3:r===`unsigned`?this.readInputValue=(e,t)=>Ke(e,t,a)-2**23:r===`signed`?this.readInputValue=(e,t)=>qe(e,t,a):b(!1);break;case 4:r===`unsigned`?this.readInputValue=(e,t)=>e.getUint32(t,a)-2**31:r===`signed`?this.readInputValue=(e,t)=>e.getInt32(t,a):r===`float`?this.readInputValue=(e,t)=>e.getFloat32(t,a):b(!1);break;case 8:r===`float`?this.readInputValue=(e,t)=>e.getFloat64(t,a):b(!1);break;default:Ge(i),b(!1)}switch(i){case 1:r===`ulaw`||r===`alaw`?(this.outputSampleSize=2,this.outputFormat=`s16`,this.writeOutputValue=(e,t,n)=>e.setInt16(t,n,!0)):(this.outputSampleSize=1,this.outputFormat=`u8`,this.writeOutputValue=(e,t,n)=>e.setUint8(t,n+2**7));break;case 2:this.outputSampleSize=2,this.outputFormat=`s16`,this.writeOutputValue=(e,t,n)=>e.setInt16(t,n,!0);break;case 3:this.outputSampleSize=4,this.outputFormat=`s32`,this.writeOutputValue=(e,t,n)=>e.setInt32(t,n<<8,!0);break;case 4:this.outputSampleSize=4,r===`float`?(this.outputFormat=`f32`,this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,!0)):(this.outputFormat=`s32`,this.writeOutputValue=(e,t,n)=>e.setInt32(t,n,!0));break;case 8:this.outputSampleSize=4,this.outputFormat=`f32`,this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,!0);break;default:Ge(i),b(!1)}}getDecodeQueueSize(){return 0}decode(e){let t=T(e.data),n=e.byteLength/this.decoderConfig.numberOfChannels/this.inputSampleSize,r=n*this.decoderConfig.numberOfChannels*this.outputSampleSize,i=new ArrayBuffer(r),a=new DataView(i);for(let e=0;e<n*this.decoderConfig.numberOfChannels;e++){let n=e*this.inputSampleSize,r=e*this.outputSampleSize,i=this.readInputValue(t,n);this.writeOutputValue(a,r,i)}let o=n/this.decoderConfig.sampleRate;(this.currentTimestamp===null||Math.abs(e.timestamp-this.currentTimestamp)>=o)&&(this.currentTimestamp=e.timestamp);let s=this.currentTimestamp;this.currentTimestamp+=o;let c=new Un({format:this.outputFormat,data:i,numberOfChannels:this.decoderConfig.numberOfChannels,sampleRate:this.decoderConfig.sampleRate,numberOfFrames:n,timestamp:s});this.onSample(c)}async flush(){}close(){}},cr=class extends er{constructor(e){if(!(e instanceof dr))throw TypeError(`audioTrack must be an InputAudioTrack.`);super(),this._track=e}async _createDecoder(e,t){if(!await this._track.canDecode())throw Error(`This audio track cannot be decoded by this browser. Make sure to check decodability before using a track.`);let n=this._track.codec,r=await this._track.getDecoderConfig();return b(n&&r),j.includes(r.codec)?new sr(e,t,r):new or(e,t,n,r)}_createPacketSink(){return new Qn(this._track)}async getSample(e){Xn(e);for await(let t of this.mediaSamplesAtTimestamps([e]))return t;throw Error(`Internal error: Iterator returned nothing.`)}samples(e=0,t=1/0){return this.mediaSamplesInRange(e,t)}samplesAtTimestamps(e){return this.mediaSamplesAtTimestamps(e)}},lr=class{constructor(e,t){this.input=e,this._backing=t}isVideoTrack(){return this instanceof ur}isAudioTrack(){return this instanceof dr}get id(){return this._backing.getId()}get internalCodecId(){return this._backing.getInternalCodecId()}get languageCode(){return this._backing.getLanguageCode()}get name(){return this._backing.getName()}get timeResolution(){return this._backing.getTimeResolution()}get disposition(){return this._backing.getDisposition()}getFirstTimestamp(){return this._backing.getFirstTimestamp()}computeDuration(){return this._backing.computeDuration()}async computePacketStats(e=1/0){let t=new Qn(this),n=1/0,r=-1/0,i=0,a=0;for await(let o of t.packets(void 0,void 0,{metadataOnly:!0})){if(i>=e&&o.timestamp>=r)break;n=Math.min(n,o.timestamp),r=Math.max(r,o.timestamp+o.duration),i++,a+=o.byteLength}return{packetCount:i,averagePacketRate:i?Number((i/(r-n)).toPrecision(16)):0,averageBitrate:i?Number((8*a/(r-n)).toPrecision(16)):0}}},ur=class extends lr{constructor(e,t){super(e,t),this._backing=t}get type(){return`video`}get codec(){return this._backing.getCodec()}get codedWidth(){return this._backing.getCodedWidth()}get codedHeight(){return this._backing.getCodedHeight()}get rotation(){return this._backing.getRotation()}get displayWidth(){return this._backing.getRotation()%180==0?this._backing.getCodedWidth():this._backing.getCodedHeight()}get displayHeight(){return this._backing.getRotation()%180==0?this._backing.getCodedHeight():this._backing.getCodedWidth()}getColorSpace(){return this._backing.getColorSpace()}async hasHighDynamicRange(){let e=await this._backing.getColorSpace();return e.primaries===`bt2020`||e.primaries===`smpte432`||e.transfer===`pg`||e.transfer===`hlg`||e.matrix===`bt2020-ncl`}canBeTransparent(){return this._backing.canBeTransparent()}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let t=this._backing.getCodec();return b(t!==null),Dn.some(n=>n.supports(t,e))?!0:typeof VideoDecoder>`u`?!1:(await VideoDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error(`Error during decodability check:`,e),!1}}async determinePacketType(e){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);if(e.isMetadataOnly)throw TypeError(`packet must not be metadata-only to determine its type.`);if(this.codec===null)return null;let t=await this.getDecoderConfig();return b(t),wn(this.codec,t,e.data)}},dr=class extends lr{constructor(e,t){super(e,t),this._backing=t}get type(){return`audio`}get codec(){return this._backing.getCodec()}get numberOfChannels(){return this._backing.getNumberOfChannels()}get sampleRate(){return this._backing.getSampleRate()}getDecoderConfig(){return this._backing.getDecoderConfig()}async getCodecParameterString(){return(await this._backing.getDecoderConfig())?.codec??null}async canDecode(){try{let e=await this._backing.getDecoderConfig();if(!e)return!1;let t=this._backing.getCodec();return b(t!==null),On.some(n=>n.supports(t,e))||e.codec.startsWith(`pcm-`)?!0:typeof AudioDecoder>`u`?!1:(await AudioDecoder.isConfigSupported(e)).supported===!0}catch(e){return console.error(`Error during decodability check:`,e),!1}}async determinePacketType(e){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);return this.codec===null?null:`key`}};
|
|
72
72
|
/*!
|
|
73
73
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
74
74
|
*
|
|
@@ -76,7 +76,7 @@ ft();let Pn=-1/0,Fn=-1/0,In=null;typeof FinalizationRegistry<`u`&&(In=new Finali
|
|
|
76
76
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
77
77
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
78
78
|
*/
|
|
79
|
-
let
|
|
79
|
+
let fr=e=>{let t=(e.hasVideo?`video/`:e.hasAudio?`audio/`:`application/`)+(e.isQuickTime?`quicktime`:`mp4`);if(e.codecStrings.length>0){let n=[...new Set(e.codecStrings)];t+=`; codecs="${n.join(`, `)}"`}return t},pr=e=>{let t=V(e),n=Xr(e,4),r=8;t===1&&(t=H(e),r=16);let i=t-r;return i<0?null:{name:n,totalSize:t,headerSize:r,contentSize:i}},mr=e=>qr(e)/65536,hr=e=>qr(e)/1073741824,gr=e=>{let t=0;for(let n=0;n<4;n++){t<<=7;let n=z(e);if(t|=n&127,!(n&128))break}return t},I=e=>{let t=B(e);return e.skip(2),t=Math.min(t,e.remainingLength),De.decode(R(e,t))},_r=e=>{let t=pr(e);if(!t||t.name!==`data`||e.remainingLength<8)return null;let n=V(e);e.skip(4);let r=R(e,t.contentSize-8);switch(n){case 1:return De.decode(r);case 2:return new TextDecoder(`utf-16be`).decode(r);case 13:return new gt(r,`image/jpeg`);case 14:return new gt(r,`image/png`);case 27:return new gt(r,`image/bmp`);default:return r}};
|
|
80
80
|
/*!
|
|
81
81
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
82
82
|
*
|
|
@@ -84,7 +84,7 @@ let dr=e=>{let t=(e.hasVideo?`video/`:e.hasAudio?`audio/`:`application/`)+(e.isQ
|
|
|
84
84
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
85
85
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
86
86
|
*/
|
|
87
|
-
var _r=class extends wn{constructor(e){super(e),this.moovSlice=null,this.currentTrack=null,this.tracks=[],this.metadataPromise=null,this.movieTimescale=-1,this.movieDurationInTimescale=-1,this.isQuickTime=!1,this.metadataTags={},this.currentMetadataKeys=null,this.isFragmented=!1,this.fragmentTrackDefaults=[],this.currentFragment=null,this.lastReadFragment=null,this.reader=e._reader}async computeDuration(){let e=await this.getTracks(),t=await Promise.all(e.map(e=>e.computeDuration()));return Math.max(0,...t)}async getTracks(){return await this.readMetadata(),this.tracks.map(e=>e.inputTrack)}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.tracks.map(e=>e.inputTrack.getCodecParameterString()));return dr({isQuickTime:this.isQuickTime,hasVideo:this.tracks.some(e=>e.info?.type===`video`),hasAudio:this.tracks.some(e=>e.info?.type===`audio`),codecStrings:e.filter(Boolean)})}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}readMetadata(){return this.metadataPromise??=(async()=>{let e=0;for(;;){let t=this.reader.requestSliceRange(e,8,16);if(t instanceof Promise&&(t=await t),!t)break;let n=e,r=fr(t);if(!r)break;if(r.name===`ftyp`)this.isQuickTime=Yr(t,4)===`qt `;else if(r.name===`moov`){let e=this.reader.requestSlice(t.filePos,r.contentSize);if(e instanceof Promise&&(e=await e),!e)break;this.moovSlice=e,this.readContiguousBoxes(this.moovSlice),this.tracks.sort((e,t)=>Number(t.disposition.default)-Number(e.disposition.default));for(let e of this.tracks){let t=e.editListPreviousSegmentDurations/this.movieTimescale;e.editListOffset-=Math.round(t*e.timescale)}break}e=n+r.totalSize}if(this.isFragmented&&this.reader.fileSize!==null){let e=this.reader.requestSlice(this.reader.fileSize-4,4);e instanceof Promise&&(e=await e),x(e);let t=V(e),n=this.reader.fileSize-t;if(n>=0&&n<=this.reader.fileSize-16){let e=this.reader.requestSliceRange(n,8,16);if(e instanceof Promise&&(e=await e),e){let t=fr(e);if(t&&t.name===`mfra`){let n=this.reader.requestSlice(e.filePos,t.contentSize);n instanceof Promise&&(n=await n),n&&this.readContiguousBoxes(n)}}}}})()}getSampleTableForTrack(e){if(e.sampleTable)return e.sampleTable;let t={sampleTimingEntries:[],sampleCompositionTimeOffsets:[],sampleSizes:[],keySampleIndices:null,chunkOffsets:[],sampleToChunk:[],presentationTimestamps:null,presentationTimestampIndexMap:null};e.sampleTable=t,x(this.moovSlice);let n=this.moovSlice.slice(e.sampleTableByteOffset);if(this.currentTrack=e,this.traverseBox(n),this.currentTrack=null,e.info?.type===`audio`&&e.info.codec&&M.includes(e.info.codec)&&t.sampleCompositionTimeOffsets.length===0){x(e.info?.type===`audio`);let n=Ft(e.info.codec),r=[],i=[];for(let a=0;a<t.sampleToChunk.length;a++){let o=t.sampleToChunk[a],s=t.sampleToChunk[a+1],c=(s?s.startChunkIndex:t.chunkOffsets.length)-o.startChunkIndex;for(let a=0;a<c;a++){let s=o.startSampleIndex+a*o.samplesPerChunk,c=s+o.samplesPerChunk,l=O(t.sampleTimingEntries,s,e=>e.startIndex),u=t.sampleTimingEntries[l],d=O(t.sampleTimingEntries,c,e=>e.startIndex),f=t.sampleTimingEntries[d],p=u.startDecodeTimestamp+(s-u.startIndex)*u.delta,m=f.startDecodeTimestamp+(c-f.startIndex)*f.delta-p,h=S(r);h&&h.delta===m?h.count++:r.push({startIndex:o.startChunkIndex+a,startDecodeTimestamp:p,count:1,delta:m});let g=o.samplesPerChunk*n.sampleSize*e.info.numberOfChannels;i.push(g)}o.startSampleIndex=o.startChunkIndex,o.samplesPerChunk=1}t.sampleTimingEntries=r,t.sampleSizes=i}if(t.sampleCompositionTimeOffsets.length>0){t.presentationTimestamps=[];for(let e of t.sampleTimingEntries)for(let n=0;n<e.count;n++)t.presentationTimestamps.push({presentationTimestamp:e.startDecodeTimestamp+n*e.delta,sampleIndex:e.startIndex+n});for(let e of t.sampleCompositionTimeOffsets)for(let n=0;n<e.count;n++){let r=e.startIndex+n,i=t.presentationTimestamps[r];i&&(i.presentationTimestamp+=e.offset)}t.presentationTimestamps.sort((e,t)=>e.presentationTimestamp-t.presentationTimestamp),t.presentationTimestampIndexMap=Array(t.presentationTimestamps.length).fill(-1);for(let e=0;e<t.presentationTimestamps.length;e++)t.presentationTimestampIndexMap[t.presentationTimestamps[e].sampleIndex]=e}return t}async readFragment(e){if(this.lastReadFragment?.moofOffset===e)return this.lastReadFragment;let t=this.reader.requestSliceRange(e,8,16);t instanceof Promise&&(t=await t),x(t);let n=fr(t);x(n?.name===`moof`);let r=this.reader.requestSlice(e,n.totalSize);r instanceof Promise&&(r=await r),x(r),this.traverseBox(r);let i=this.lastReadFragment;x(i&&i.moofOffset===e);for(let[,e]of i.trackData){let t=e.track,{fragmentPositionCache:n}=t;if(!e.startTimestampIsFinal){let r=t.fragmentLookupTable.find(e=>e.moofOffset===i.moofOffset);if(r)Tr(e,r.timestamp);else{let t=O(n,i.moofOffset-1,e=>e.moofOffset);if(t!==-1){let r=n[t];Tr(e,r.endTimestamp)}}e.startTimestampIsFinal=!0}let r=O(n,e.startTimestamp,e=>e.startTimestamp);(r===-1||n[r].moofOffset!==i.moofOffset)&&n.splice(r+1,0,{moofOffset:i.moofOffset,startTimestamp:e.startTimestamp,endTimestamp:e.endTimestamp})}return i}readContiguousBoxes(e){let t=e.filePos;for(;e.filePos-t<=e.length-8&&this.traverseBox(e););}*iterateContiguousBoxes(e){let t=e.filePos;for(;e.filePos-t<=e.length-8;){let t=e.filePos,n=fr(e);if(!n)break;yield{boxInfo:n,slice:e},e.filePos=t+n.totalSize}}traverseBox(e){let t=e.filePos,n=fr(e);if(!n)return!1;let r=e.filePos,i=t+n.totalSize;switch(n.name){case`mdia`:case`minf`:case`dinf`:case`mfra`:case`edts`:this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`mvhd`:{let t=z(e);e.skip(3),t===1?(e.skip(16),this.movieTimescale=V(e),this.movieDurationInTimescale=H(e)):(e.skip(8),this.movieTimescale=V(e),this.movieDurationInTimescale=V(e))}break;case`trak`:{let t={id:-1,demuxer:this,inputTrack:null,disposition:{..._t},info:null,timescale:-1,durationInMovieTimescale:-1,durationInMediaTimescale:-1,rotation:0,internalCodecId:null,name:null,languageCode:`und`,sampleTableByteOffset:-1,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:0,editListOffset:0};if(this.currentTrack=t,this.readContiguousBoxes(e.slice(r,n.contentSize)),t.id!==-1&&t.timescale!==-1&&t.info!==null){if(t.info.type===`video`&&t.info.width!==-1){let e=t;t.inputTrack=new lr(this.input,new yr(e)),this.tracks.push(t)}else if(t.info.type===`audio`&&t.info.numberOfChannels!==-1){let e=t;t.inputTrack=new ur(this.input,new br(e)),this.tracks.push(t)}}this.currentTrack=null}break;case`tkhd`:{let t=this.currentTrack;if(!t)break;let n=z(e),r=!!(Wr(e)&1);if(t.disposition.default=r,n===0)e.skip(8),t.id=V(e),e.skip(4),t.durationInMovieTimescale=V(e);else if(n===1)e.skip(16),t.id=V(e),e.skip(4),t.durationInMovieTimescale=H(e);else throw Error(`Incorrect track header version ${n}.`);e.skip(16);let i=Se(Xe(Er([pr(e),pr(e),mr(e),pr(e),pr(e),mr(e),pr(e),pr(e),mr(e)]),90));x(i===0||i===90||i===180||i===270),t.rotation=i}break;case`elst`:{let t=this.currentTrack;if(!t)break;let n=z(e);e.skip(3);let r=!1,i=0,a=V(e);for(let o=0;o<a;o++){let a=n===1?H(e):V(e),o=n===1?qr(e):Kr(e),s=pr(e);if(a!==0){if(r){console.warn(`Unsupported edit list: multiple edits are not currently supported. Only using first edit.`);break}if(o===-1){i+=a;continue}if(s!==1){console.warn(`Unsupported edit list entry: media rate must be 1.`);break}t.editListPreviousSegmentDurations=i,t.editListOffset=o,r=!0}}}break;case`mdhd`:{let t=this.currentTrack;if(!t)break;let n=z(e);e.skip(3),n===0?(e.skip(8),t.timescale=V(e),t.durationInMediaTimescale=V(e)):n===1&&(e.skip(16),t.timescale=V(e),t.durationInMediaTimescale=H(e));let r=B(e);if(r>0){t.languageCode=``;for(let e=0;e<3;e++)t.languageCode=String.fromCharCode(96+(r&31))+t.languageCode,r>>=5;Qe(t.languageCode)||(t.languageCode=`und`)}}break;case`hdlr`:{let t=this.currentTrack;if(!t)break;e.skip(8);let n=Yr(e,4);n===`vide`?t.info={type:`video`,width:-1,height:-1,codec:null,codecDescription:null,colorSpace:null,avcType:null,avcCodecInfo:null,hevcCodecInfo:null,vp9CodecInfo:null,av1CodecInfo:null}:n===`soun`&&(t.info={type:`audio`,numberOfChannels:-1,sampleRate:-1,codec:null,codecDescription:null,aacCodecInfo:null})}break;case`stbl`:{let i=this.currentTrack;if(!i)break;i.sampleTableByteOffset=t,this.readContiguousBoxes(e.slice(r,n.contentSize))}break;case`stsd`:{let t=this.currentTrack;if(!t||t.info===null||t.sampleTable)break;let n=z(e);e.skip(3);let r=V(e);for(let i=0;i<r;i++){let r=e.filePos,i=fr(e);if(!i)break;t.internalCodecId=i.name;let a=i.name.toLowerCase();if(t.info.type===`video`)a===`avc1`||a===`avc3`?(t.info.codec=`avc`,t.info.avcType=a===`avc1`?1:3):a===`hvc1`||a===`hev1`?t.info.codec=`hevc`:a===`vp08`?t.info.codec=`vp8`:a===`vp09`?t.info.codec=`vp9`:a===`av01`?t.info.codec=`av1`:console.warn(`Unsupported video codec (sample entry type '${i.name}').`),e.skip(24),t.info.width=B(e),t.info.height=B(e),e.skip(50),this.readContiguousBoxes(e.slice(e.filePos,r+i.totalSize-e.filePos));else{a===`mp4a`||(a===`opus`?t.info.codec=`opus`:a===`flac`?t.info.codec=`flac`:a===`twos`||a===`sowt`||a===`raw `||a===`in24`||a===`in32`||a===`fl32`||a===`fl64`||a===`lpcm`||a===`ipcm`||a===`fpcm`||(a===`ulaw`?t.info.codec=`ulaw`:a===`alaw`?t.info.codec=`alaw`:console.warn(`Unsupported audio codec (sample entry type '${i.name}').`))),e.skip(8);let o=B(e);e.skip(6);let s=B(e),c=B(e);e.skip(4);let l=V(e)/65536;if(n===0&&o>0){if(o===1)e.skip(4),c=8*V(e),e.skip(8);else if(o===2){e.skip(4),l=Jr(e),s=V(e),e.skip(4),c=V(e);let n=V(e);if(e.skip(8),a===`lpcm`){let e=c+7>>3,r=!!(n&1),i=!!(n&2),a=n&4?-1:0;c>0&&c<=64&&(r?c===32&&(t.info.codec=i?`pcm-f32be`:`pcm-f32`):a&1<<e-1?e===1?t.info.codec=`pcm-s8`:e===2?t.info.codec=i?`pcm-s16be`:`pcm-s16`:e===3?t.info.codec=i?`pcm-s24be`:`pcm-s24`:e===4&&(t.info.codec=i?`pcm-s32be`:`pcm-s32`):e===1&&(t.info.codec=`pcm-u8`)),t.info.codec===null&&console.warn(`Unsupported PCM format.`)}}}t.info.codec===`opus`&&(l=48e3),t.info.numberOfChannels=s,t.info.sampleRate=l,a===`twos`?c===8?t.info.codec=`pcm-s8`:c===16?t.info.codec=`pcm-s16be`:(console.warn(`Unsupported sample size ${c} for codec 'twos'.`),t.info.codec=null):a===`sowt`?c===8?t.info.codec=`pcm-s8`:c===16?t.info.codec=`pcm-s16`:(console.warn(`Unsupported sample size ${c} for codec 'sowt'.`),t.info.codec=null):a===`raw `?t.info.codec=`pcm-u8`:a===`in24`?t.info.codec=`pcm-s24be`:a===`in32`?t.info.codec=`pcm-s32be`:a===`fl32`?t.info.codec=`pcm-f32be`:a===`fl64`?t.info.codec=`pcm-f64be`:a===`ipcm`?t.info.codec=`pcm-s16be`:a===`fpcm`&&(t.info.codec=`pcm-f32be`),this.readContiguousBoxes(e.slice(e.filePos,r+i.totalSize-e.filePos))}}}break;case`avcC`:{let t=this.currentTrack;if(!t)break;x(t.info),t.info.codecDescription=R(e,n.contentSize)}break;case`hvcC`:{let t=this.currentTrack;if(!t)break;x(t.info),t.info.codecDescription=R(e,n.contentSize)}break;case`vpcC`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`video`),e.skip(4);let n=z(e),r=z(e),i=z(e),a=i>>4,o=i>>1&7,s=i&1,c=z(e),l=z(e),u=z(e);t.info.vp9CodecInfo={profile:n,level:r,bitDepth:a,chromaSubsampling:o,videoFullRangeFlag:s,colourPrimaries:c,transferCharacteristics:l,matrixCoefficients:u}}break;case`av1C`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`video`),e.skip(1);let n=z(e),r=n>>5,i=n&31,a=z(e),o=a>>7,s=a>>6&1,c=a>>5&1,l=a>>4&1,u=a>>3&1,d=a>>2&1,f=a&3,p=r===2&&s?c?12:10:s?10:8;t.info.av1CodecInfo={profile:r,level:i,tier:o,bitDepth:p,monochrome:l,chromaSubsamplingX:u,chromaSubsamplingY:d,chromaSamplePosition:f}}break;case`colr`:{let t=this.currentTrack;if(!t||(x(t.info?.type===`video`),Yr(e,4)!==`nclx`))break;let n=B(e),r=B(e),i=B(e),a=!!(z(e)&128);t.info.colorSpace={primaries:Oe[n],transfer:Ae[r],matrix:Me[i],fullRange:a}}break;case`wave`:this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`esds`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`audio`),e.skip(4),x(z(e)===3),hr(e),e.skip(2);let n=z(e),r=(n&128)!=0,i=(n&64)!=0,a=(n&32)!=0;if(r&&e.skip(2),i){let t=z(e);e.skip(t)}a&&e.skip(2),x(z(e)===4);let o=hr(e),s=e.filePos,c=z(e);if(c===64||c===103?(t.info.codec=`aac`,t.info.aacCodecInfo={isMpeg2:c===103}):c===105||c===107?t.info.codec=`mp3`:c===221?t.info.codec=`vorbis`:console.warn(`Unsupported audio codec (objectTypeIndication ${c}) - discarding track.`),e.skip(12),o>e.filePos-s){x(z(e)===5);let n=hr(e);if(t.info.codecDescription=R(e,n),t.info.codec===`aac`){let e=Mt(t.info.codecDescription);e.numberOfChannels!==null&&(t.info.numberOfChannels=e.numberOfChannels),e.sampleRate!==null&&(t.info.sampleRate=e.sampleRate)}}}break;case`enda`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`audio`),B(e)&255&&(t.info.codec===`pcm-s16be`?t.info.codec=`pcm-s16`:t.info.codec===`pcm-s24be`?t.info.codec=`pcm-s24`:t.info.codec===`pcm-s32be`?t.info.codec=`pcm-s32`:t.info.codec===`pcm-f32be`?t.info.codec=`pcm-f32`:t.info.codec===`pcm-f64be`&&(t.info.codec=`pcm-f64`))}break;case`pcmC`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`audio`),e.skip(4);let n=!!(z(e)&1),r=z(e);t.info.codec===`pcm-s16be`?n?r===16?t.info.codec=`pcm-s16`:r===24?t.info.codec=`pcm-s24`:r===32?t.info.codec=`pcm-s32`:(console.warn(`Invalid ipcm sample size ${r}.`),t.info.codec=null):r===16?t.info.codec=`pcm-s16be`:r===24?t.info.codec=`pcm-s24be`:r===32?t.info.codec=`pcm-s32be`:(console.warn(`Invalid ipcm sample size ${r}.`),t.info.codec=null):t.info.codec===`pcm-f32be`&&(n?r===32?t.info.codec=`pcm-f32`:r===64?t.info.codec=`pcm-f64`:(console.warn(`Invalid fpcm sample size ${r}.`),t.info.codec=null):r===32?t.info.codec=`pcm-f32be`:r===64?t.info.codec=`pcm-f64be`:(console.warn(`Invalid fpcm sample size ${r}.`),t.info.codec=null));break}case`dOps`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`audio`),e.skip(1);let n=z(e),r=B(e),i=V(e),a=Gr(e),o=z(e),s;s=o===0?new Uint8Array:R(e,2+n);let c=new Uint8Array(19+s.byteLength),l=new DataView(c.buffer);l.setUint32(0,1332770163,!1),l.setUint32(4,1214603620,!1),l.setUint8(8,1),l.setUint8(9,n),l.setUint16(10,r,!0),l.setUint32(12,i,!0),l.setInt16(16,a,!0),l.setUint8(18,o),c.set(s,19),t.info.codecDescription=c,t.info.numberOfChannels=n}break;case`dfLa`:{let t=this.currentTrack;if(!t)break;x(t.info?.type===`audio`),e.skip(4);let n=e.filePos;for(;e.filePos<i;){let n=z(e),r=Wr(e);if((n&127)===Cn.STREAMINFO){e.skip(10);let n=V(e),r=n>>>12,i=(n>>9&7)+1;t.info.sampleRate=r,t.info.numberOfChannels=i,e.skip(20)}else e.skip(r);if(n&128)break}let r=e.filePos;e.filePos=n;let a=R(e,r-n),o=new Uint8Array(4+a.byteLength);new DataView(o.buffer).setUint32(0,1716281667,!1),o.set(a,4),t.info.codecDescription=o}break;case`stts`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=0,i=0;for(let a=0;a<n;a++){let n=V(e),a=V(e);t.sampleTable.sampleTimingEntries.push({startIndex:r,startDecodeTimestamp:i,count:n,delta:a}),r+=n,i+=n*a}}break;case`ctts`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=0;for(let i=0;i<n;i++){let n=V(e),i=Kr(e);t.sampleTable.sampleCompositionTimeOffsets.push({startIndex:r,count:n,offset:i}),r+=n}}break;case`stsz`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=V(e);if(n===0)for(let n=0;n<r;n++){let n=V(e);t.sampleTable.sampleSizes.push(n)}else t.sampleTable.sampleSizes.push(n)}break;case`stz2`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4),e.skip(3);let n=z(e),r=V(e),i=new C(R(e,Math.ceil(r*n/8)));for(let e=0;e<r;e++){let e=i.readBits(n);t.sampleTable.sampleSizes.push(e)}}break;case`stss`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4),t.sampleTable.keySampleIndices=[];let n=V(e);for(let r=0;r<n;r++){let n=V(e)-1;t.sampleTable.keySampleIndices.push(n)}t.sampleTable.keySampleIndices[0]!==0&&t.sampleTable.keySampleIndices.unshift(0)}break;case`stsc`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=V(e)-1,r=V(e),i=V(e);t.sampleTable.sampleToChunk.push({startSampleIndex:-1,startChunkIndex:n,samplesPerChunk:r,sampleDescriptionIndex:i})}let r=0;for(let e=0;e<t.sampleTable.sampleToChunk.length;e++)if(t.sampleTable.sampleToChunk[e].startSampleIndex=r,e<t.sampleTable.sampleToChunk.length-1){let n=t.sampleTable.sampleToChunk[e+1].startChunkIndex-t.sampleTable.sampleToChunk[e].startChunkIndex;r+=n*t.sampleTable.sampleToChunk[e].samplesPerChunk}}break;case`stco`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=V(e);t.sampleTable.chunkOffsets.push(n)}}break;case`co64`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=H(e);t.sampleTable.chunkOffsets.push(n)}}break;case`mvex`:this.isFragmented=!0,this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`mehd`:{let t=z(e);e.skip(3),this.movieDurationInTimescale=t===1?H(e):V(e)}break;case`trex`:{e.skip(4);let t=V(e),n=V(e),r=V(e),i=V(e),a=V(e);this.fragmentTrackDefaults.push({trackId:t,defaultSampleDescriptionIndex:n,defaultSampleDuration:r,defaultSampleSize:i,defaultSampleFlags:a})}break;case`tfra`:{let t=z(e);e.skip(3);let n=V(e),r=this.tracks.find(e=>e.id===n);if(!r)break;let i=V(e),a=(i&48)>>4,o=(i&12)>>2,s=i&3,c=[z,B,Wr,V],l=c[a],u=c[o],d=c[s],f=V(e);for(let n=0;n<f;n++){let n=t===1?H(e):V(e),i=t===1?H(e):V(e);l(e),u(e),d(e),r.fragmentLookupTable.push({timestamp:n,moofOffset:i})}r.fragmentLookupTable.sort((e,t)=>e.timestamp-t.timestamp);for(let e=0;e<r.fragmentLookupTable.length-1;e++){let t=r.fragmentLookupTable[e],n=r.fragmentLookupTable[e+1];t.timestamp===n.timestamp&&(r.fragmentLookupTable.splice(e+1,1),e--)}}break;case`moof`:this.currentFragment={moofOffset:t,moofSize:n.totalSize,implicitBaseDataOffset:t,trackData:new Map},this.readContiguousBoxes(e.slice(r,n.contentSize)),this.lastReadFragment=this.currentFragment,this.currentFragment=null;break;case`traf`:if(x(this.currentFragment),this.readContiguousBoxes(e.slice(r,n.contentSize)),this.currentTrack){let e=this.currentFragment.trackData.get(this.currentTrack.id);if(e){let{currentFragmentState:t}=this.currentTrack;x(t),t.startTimestamp!==null&&(Tr(e,t.startTimestamp),e.startTimestampIsFinal=!0)}this.currentTrack.currentFragmentState=null,this.currentTrack=null}break;case`tfhd`:{x(this.currentFragment),e.skip(1);let t=Wr(e),n=!!(t&1),r=!!(t&2),i=!!(t&8),a=!!(t&16),o=!!(t&32),s=!!(t&65536),c=!!(t&131072),l=V(e),u=this.tracks.find(e=>e.id===l);if(!u)break;let d=this.fragmentTrackDefaults.find(e=>e.trackId===l);this.currentTrack=u,u.currentFragmentState={baseDataOffset:this.currentFragment.implicitBaseDataOffset,sampleDescriptionIndex:d?.defaultSampleDescriptionIndex??null,defaultSampleDuration:d?.defaultSampleDuration??null,defaultSampleSize:d?.defaultSampleSize??null,defaultSampleFlags:d?.defaultSampleFlags??null,startTimestamp:null},n?u.currentFragmentState.baseDataOffset=H(e):c&&(u.currentFragmentState.baseDataOffset=this.currentFragment.moofOffset),r&&(u.currentFragmentState.sampleDescriptionIndex=V(e)),i&&(u.currentFragmentState.defaultSampleDuration=V(e)),a&&(u.currentFragmentState.defaultSampleSize=V(e)),o&&(u.currentFragmentState.defaultSampleFlags=V(e)),s&&(u.currentFragmentState.defaultSampleDuration=0)}break;case`tfdt`:{let t=this.currentTrack;if(!t)break;x(t.currentFragmentState);let n=z(e);e.skip(3);let r=n===0?V(e):H(e);t.currentFragmentState.startTimestamp=r}break;case`trun`:{let t=this.currentTrack;if(!t)break;if(x(this.currentFragment),x(t.currentFragmentState),this.currentFragment.trackData.has(t.id)){console.warn(`Can't have two trun boxes for the same track in one fragment. Ignoring...`);break}let n=z(e),r=Wr(e),i=!!(r&1),a=!!(r&4),o=!!(r&256),s=!!(r&512),c=!!(r&1024),l=!!(r&2048),u=V(e),d=t.currentFragmentState.baseDataOffset;i&&(d+=Kr(e));let f=null;a&&(f=V(e));let p=d;if(u===0){this.currentFragment.implicitBaseDataOffset=p;break}let m=0,h={track:t,startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,samples:[],presentationTimestamps:[],startTimestampIsFinal:!1};this.currentFragment.trackData.set(t.id,h);for(let r=0;r<u;r++){let i;o?i=V(e):(x(t.currentFragmentState.defaultSampleDuration!==null),i=t.currentFragmentState.defaultSampleDuration);let a;s?a=V(e):(x(t.currentFragmentState.defaultSampleSize!==null),a=t.currentFragmentState.defaultSampleSize);let u;c?u=V(e):(x(t.currentFragmentState.defaultSampleFlags!==null),u=t.currentFragmentState.defaultSampleFlags),r===0&&f!==null&&(u=f);let d=0;l&&(d=n===0?V(e):Kr(e));let g=!(u&65536);h.samples.push({presentationTimestamp:m+d,duration:i,byteOffset:p,byteSize:a,isKeyFrame:g}),p+=a,m+=i}h.presentationTimestamps=h.samples.map((e,t)=>({presentationTimestamp:e.presentationTimestamp,sampleIndex:t})).sort((e,t)=>e.presentationTimestamp-t.presentationTimestamp);for(let e=0;e<h.presentationTimestamps.length;e++){let t=h.presentationTimestamps[e],n=h.samples[t.sampleIndex];h.firstKeyFrameTimestamp===null&&n.isKeyFrame&&(h.firstKeyFrameTimestamp=n.presentationTimestamp),e<h.presentationTimestamps.length-1&&(n.duration=h.presentationTimestamps[e+1].presentationTimestamp-t.presentationTimestamp)}let g=h.samples[h.presentationTimestamps[0].sampleIndex],_=h.samples[S(h.presentationTimestamps).sampleIndex];h.startTimestamp=g.presentationTimestamp,h.endTimestamp=_.presentationTimestamp+_.duration,this.currentFragment.implicitBaseDataOffset=p}break;case`udta`:{let t=this.iterateContiguousBoxes(e.slice(r,n.contentSize));for(let{boxInfo:e,slice:n}of t){if(e.name!==`meta`&&!this.currentTrack){let t=n.filePos;this.metadataTags.raw??={},e.name[0]===`©`?this.metadataTags.raw[e.name]??=I(n):this.metadataTags.raw[e.name]??=R(n,e.contentSize),n.filePos=t}switch(e.name){case`meta`:n.skip(-e.headerSize),this.traverseBox(n);break;case`©nam`:case`name`:this.currentTrack?this.currentTrack.name=Te.decode(R(n,e.contentSize)):this.metadataTags.title??=I(n);break;case`©des`:this.currentTrack||(this.metadataTags.description??=I(n));break;case`©ART`:this.currentTrack||(this.metadataTags.artist??=I(n));break;case`©alb`:this.currentTrack||(this.metadataTags.album??=I(n));break;case`albr`:this.currentTrack||(this.metadataTags.albumArtist??=I(n));break;case`©gen`:this.currentTrack||(this.metadataTags.genre??=I(n));break;case`©day`:if(!this.currentTrack){let e=new Date(I(n));Number.isNaN(e.getTime())||(this.metadataTags.date??=e)}break;case`©cmt`:this.currentTrack||(this.metadataTags.comment??=I(n));break;case`©lyr`:this.currentTrack||(this.metadataTags.lyrics??=I(n));break}}}break;case`meta`:{if(this.currentTrack)break;let t=V(e)!==0;this.currentMetadataKeys=new Map,t?this.readContiguousBoxes(e.slice(r,n.contentSize)):this.readContiguousBoxes(e.slice(r+4,n.contentSize-4)),this.currentMetadataKeys=null}break;case`keys`:{if(!this.currentMetadataKeys)break;e.skip(4);let t=V(e);for(let n=0;n<t;n++){let t=V(e);e.skip(4);let r=Te.decode(R(e,t-8));this.currentMetadataKeys.set(n+1,r)}}break;case`ilst`:{if(!this.currentMetadataKeys)break;let t=this.iterateContiguousBoxes(e.slice(r,n.contentSize));for(let{boxInfo:e,slice:n}of t){let t=e.name,r=(t.charCodeAt(0)<<24)+(t.charCodeAt(1)<<16)+(t.charCodeAt(2)<<8)+t.charCodeAt(3);this.currentMetadataKeys.has(r)&&(t=this.currentMetadataKeys.get(r));let i=gr(n);switch(this.metadataTags.raw??={},this.metadataTags.raw[t]??=i,t){case`©nam`:case`titl`:case`com.apple.quicktime.title`:case`title`:typeof i==`string`&&(this.metadataTags.title??=i);break;case`©des`:case`desc`:case`dscp`:case`com.apple.quicktime.description`:case`description`:typeof i==`string`&&(this.metadataTags.description??=i);break;case`©ART`:case`com.apple.quicktime.artist`:case`artist`:typeof i==`string`&&(this.metadataTags.artist??=i);break;case`©alb`:case`albm`:case`com.apple.quicktime.album`:case`album`:typeof i==`string`&&(this.metadataTags.album??=i);break;case`aART`:case`album_artist`:typeof i==`string`&&(this.metadataTags.albumArtist??=i);break;case`©cmt`:case`com.apple.quicktime.comment`:case`comment`:typeof i==`string`&&(this.metadataTags.comment??=i);break;case`©gen`:case`gnre`:case`com.apple.quicktime.genre`:case`genre`:typeof i==`string`&&(this.metadataTags.genre??=i);break;case`©lyr`:case`lyrics`:typeof i==`string`&&(this.metadataTags.lyrics??=i);break;case`©day`:case`rldt`:case`com.apple.quicktime.creationdate`:case`date`:if(typeof i==`string`){let e=new Date(i);Number.isNaN(e.getTime())||(this.metadataTags.date??=e)}break;case`covr`:case`com.apple.quicktime.artwork`:i instanceof mt?(this.metadataTags.images??=[],this.metadataTags.images.push({data:i.data,kind:`coverFront`,mimeType:i.mimeType})):i instanceof Uint8Array&&(this.metadataTags.images??=[],this.metadataTags.images.push({data:i,kind:`coverFront`,mimeType:`image/*`}));break;case`track`:if(typeof i==`string`){let e=i.split(`/`),t=Number.parseInt(e[0],10),n=e[1]&&Number.parseInt(e[1],10);Number.isInteger(t)&&t>0&&(this.metadataTags.trackNumber??=t),n&&Number.isInteger(n)&&n>0&&(this.metadataTags.tracksTotal??=n)}break;case`trkn`:if(i instanceof Uint8Array&&i.length>=6){let e=E(i),t=e.getUint16(2,!1),n=e.getUint16(4,!1);t>0&&(this.metadataTags.trackNumber??=t),n>0&&(this.metadataTags.tracksTotal??=n)}break;case`disc`:case`disk`:if(i instanceof Uint8Array&&i.length>=6){let e=E(i),t=e.getUint16(2,!1),n=e.getUint16(4,!1);t>0&&(this.metadataTags.discNumber??=t),n>0&&(this.metadataTags.discsTotal??=n)}break}}}break}return e.filePos=i,!0}},vr=class{constructor(e){this.internalTrack=e,this.packetToSampleIndex=new WeakMap,this.packetToFragmentLocation=new WeakMap}getId(){return this.internalTrack.id}getCodec(){throw Error(`Not implemented on base class.`)}getInternalCodecId(){return this.internalTrack.internalCodecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.timescale}getDisposition(){return this.internalTrack.disposition}async computeDuration(){let e=await this.getPacket(1/0,{metadataOnly:!0});return(e?.timestamp??0)+(e?.duration??0)}async getFirstTimestamp(){return(await this.getFirstPacket({metadataOnly:!0}))?.timestamp??0}async getFirstPacket(e){let t=await this.fetchPacketForSampleIndex(0,e);return t||!this.internalTrack.demuxer.isFragmented?t:this.performFragmentedLookup(null,e=>e.trackData.get(this.internalTrack.id)?{sampleIndex:0,correctSampleFound:!0}:{sampleIndex:-1,correctSampleFound:!1},-1/0,1/0,e)}mapTimestampIntoTimescale(e){return Ye(e*this.internalTrack.timescale)+this.internalTrack.editListOffset}async getPacket(e,t){let n=this.mapTimestampIntoTimescale(e),r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=xr(r,n),a=await this.fetchPacketForSampleIndex(i,t);return!Dr(r)||!this.internalTrack.demuxer.isFragmented?a:this.performFragmentedLookup(null,e=>{let t=e.trackData.get(this.internalTrack.id);if(!t)return{sampleIndex:-1,correctSampleFound:!1};let r=O(t.presentationTimestamps,n,e=>e.presentationTimestamp);return{sampleIndex:r===-1?-1:t.presentationTimestamps[r].sampleIndex,correctSampleFound:r!==-1&&n<t.endTimestamp}},n,n,t)}async getNextPacket(e,t){let n=this.packetToSampleIndex.get(e);if(n!==void 0)return this.fetchPacketForSampleIndex(n+1,t);let r=this.packetToFragmentLocation.get(e);if(r===void 0)throw Error(`Packet was not created from this track.`);return this.performFragmentedLookup(r.fragment,e=>{if(e===r.fragment){let t=e.trackData.get(this.internalTrack.id);if(r.sampleIndex+1<t.samples.length)return{sampleIndex:r.sampleIndex+1,correctSampleFound:!0}}else if(e.trackData.get(this.internalTrack.id))return{sampleIndex:0,correctSampleFound:!0};return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,t)}async getKeyPacket(e,t){let n=this.mapTimestampIntoTimescale(e),r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=Sr(r,n),a=await this.fetchPacketForSampleIndex(i,t);return!Dr(r)||!this.internalTrack.demuxer.isFragmented?a:this.performFragmentedLookup(null,e=>{let t=e.trackData.get(this.internalTrack.id);if(!t)return{sampleIndex:-1,correctSampleFound:!1};let r=Be(t.presentationTimestamps,e=>t.samples[e.sampleIndex].isKeyFrame&&e.presentationTimestamp<=n);return{sampleIndex:r===-1?-1:t.presentationTimestamps[r].sampleIndex,correctSampleFound:r!==-1&&n<t.endTimestamp}},n,n,t)}async getNextKeyPacket(e,t){let n=this.packetToSampleIndex.get(e);if(n!==void 0){let e=wr(this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),n);return this.fetchPacketForSampleIndex(e,t)}let r=this.packetToFragmentLocation.get(e);if(r===void 0)throw Error(`Packet was not created from this track.`);return this.performFragmentedLookup(r.fragment,e=>{if(e===r.fragment){let t=e.trackData.get(this.internalTrack.id).samples.findIndex((e,t)=>e.isKeyFrame&&t>r.sampleIndex);if(t!==-1)return{sampleIndex:t,correctSampleFound:!0}}else{let t=e.trackData.get(this.internalTrack.id);if(t&&t.firstKeyFrameTimestamp!==null){let e=t.samples.findIndex(e=>e.isKeyFrame);return x(e!==-1),{sampleIndex:e,correctSampleFound:!0}}}return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,t)}async fetchPacketForSampleIndex(e,t){if(e===-1)return null;let n=Cr(this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),e);if(!n)return null;let r;if(t.metadataOnly)r=kn;else{let e=this.internalTrack.demuxer.reader.requestSlice(n.sampleOffset,n.sampleSize);e instanceof Promise&&(e=await e),x(e),r=R(e,n.sampleSize)}let i=(n.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,a=n.duration/this.internalTrack.timescale,o=new F(r,n.isKeyFrame?`key`:`delta`,i,a,e,n.sampleSize);return this.packetToSampleIndex.set(o,e),o}async fetchPacketInFragment(e,t,n){if(t===-1)return null;let r=e.trackData.get(this.internalTrack.id).samples[t];x(r);let i;if(n.metadataOnly)i=kn;else{let e=this.internalTrack.demuxer.reader.requestSlice(r.byteOffset,r.byteSize);e instanceof Promise&&(e=await e),x(e),i=R(e,r.byteSize)}let a=(r.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,o=r.duration/this.internalTrack.timescale,s=new F(i,r.isKeyFrame?`key`:`delta`,a,o,e.moofOffset+t,r.byteSize);return this.packetToFragmentLocation.set(s,{fragment:e,sampleIndex:t}),s}async performFragmentedLookup(e,t,n,r,i){let a=this.internalTrack.demuxer,o=null,s=null,c=-1;if(e){let{sampleIndex:n,correctSampleFound:r}=t(e);if(r)return this.fetchPacketInFragment(e,n,i);n!==-1&&(s=e,c=n)}let l=O(this.internalTrack.fragmentLookupTable,n,e=>e.timestamp),u=l===-1?null:this.internalTrack.fragmentLookupTable[l],d=O(this.internalTrack.fragmentPositionCache,n,e=>e.startTimestamp),f=d===-1?null:this.internalTrack.fragmentPositionCache[d],p=Math.max(u?.moofOffset??0,f?.moofOffset??0)||null,m;for(e?p===null||e.moofOffset>=p?(m=e.moofOffset+e.moofSize,o=e):m=p:m=p??0;;){if(o){let e=o.trackData.get(this.internalTrack.id);if(e&&e.startTimestamp>r)break}let e=a.reader.requestSliceRange(m,8,16);if(e instanceof Promise&&(e=await e),!e)break;let n=m,l=fr(e);if(!l)break;if(l.name===`moof`){o=await a.readFragment(n);let{sampleIndex:e,correctSampleFound:r}=t(o);if(r)return this.fetchPacketInFragment(o,e,i);e!==-1&&(s=o,c=e)}m=n+l.totalSize}if(u&&(!s||s.moofOffset<u.moofOffset)){let e=this.internalTrack.fragmentLookupTable[l-1];x(!e||e.timestamp<u.timestamp);let n=e?.timestamp??-1/0;return this.performFragmentedLookup(null,t,n,r,i)}return s?this.fetchPacketInFragment(s,c,i):null}},yr=class extends vr{constructor(e){super(e),this.decoderConfigPromise=null,this.internalTrack=e}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getRotation(){return this.internalTrack.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return!1}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{if(this.internalTrack.info.codec===`vp9`&&!this.internalTrack.info.vp9CodecInfo){let e=await this.getFirstPacket({});this.internalTrack.info.vp9CodecInfo=e&&vn(e.data)}else if(this.internalTrack.info.codec===`av1`&&!this.internalTrack.info.av1CodecInfo){let e=await this.getFirstPacket({});this.internalTrack.info.av1CodecInfo=e&&bn(e.data)}return{codec:Dt(this.internalTrack.info),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0}})():null}},br=class extends vr{constructor(e){super(e),this.decoderConfig=null,this.internalTrack=e}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:kt(this.internalTrack.info),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}};let xr=(e,t)=>{if(e.presentationTimestamps){let n=O(e.presentationTimestamps,t,e=>e.presentationTimestamp);return n===-1?-1:e.presentationTimestamps[n].sampleIndex}else{let n=O(e.sampleTimingEntries,t,e=>e.startDecodeTimestamp);if(n===-1)return-1;let r=e.sampleTimingEntries[n];return r.startIndex+Math.min(Math.floor((t-r.startDecodeTimestamp)/r.delta),r.count-1)}},Sr=(e,t)=>{if(!e.keySampleIndices)return xr(e,t);if(e.presentationTimestamps){let n=O(e.presentationTimestamps,t,e=>e.presentationTimestamp);if(n===-1)return-1;for(let t=n;t>=0;t--){let n=e.presentationTimestamps[t].sampleIndex;if(Re(e.keySampleIndices,n,e=>e)!==-1)return n}return-1}else{let n=xr(e,t),r=O(e.keySampleIndices,n,e=>e);return e.keySampleIndices[r]??-1}},Cr=(e,t)=>{let n=O(e.sampleTimingEntries,t,e=>e.startIndex),r=e.sampleTimingEntries[n];if(!r||r.startIndex+r.count<=t)return null;let i=r.startDecodeTimestamp+(t-r.startIndex)*r.delta,a=O(e.sampleCompositionTimeOffsets,t,e=>e.startIndex),o=e.sampleCompositionTimeOffsets[a];o&&t-o.startIndex<o.count&&(i+=o.offset);let s=e.sampleSizes[Math.min(t,e.sampleSizes.length-1)],c=O(e.sampleToChunk,t,e=>e.startSampleIndex),l=e.sampleToChunk[c];x(l);let u=l.startChunkIndex+Math.floor((t-l.startSampleIndex)/l.samplesPerChunk),d=e.chunkOffsets[u],f=l.startSampleIndex+(u-l.startChunkIndex)*l.samplesPerChunk,p=0,m=d;if(e.sampleSizes.length===1)m+=s*(t-f),p+=s*l.samplesPerChunk;else for(let n=f;n<f+l.samplesPerChunk;n++){let r=e.sampleSizes[n];n<t&&(m+=r),p+=r}let h=r.delta;if(e.presentationTimestamps){let n=e.presentationTimestampIndexMap[t];x(n!==void 0),n<e.presentationTimestamps.length-1&&(h=e.presentationTimestamps[n+1].presentationTimestamp-i)}return{presentationTimestamp:i,duration:h,sampleOffset:m,sampleSize:s,chunkOffset:d,chunkSize:p,isKeyFrame:e.keySampleIndices?Re(e.keySampleIndices,t,e=>e)!==-1:!0}},wr=(e,t)=>{if(!e.keySampleIndices)return t+1;let n=O(e.keySampleIndices,t,e=>e);return e.keySampleIndices[n+1]??-1},Tr=(e,t)=>{e.startTimestamp+=t,e.endTimestamp+=t;for(let n of e.samples)n.presentationTimestamp+=t;for(let n of e.presentationTimestamps)n.presentationTimestamp+=t},Er=e=>{let[t,,,n]=e,r=Math.hypot(t,n),i=t/r,a=n/r,o=-Math.atan2(a,i)*(180/Math.PI);return Number.isFinite(o)?o:0},Dr=e=>e.sampleSizes.length===0;
|
|
87
|
+
var vr=class extends En{constructor(e){super(e),this.moovSlice=null,this.currentTrack=null,this.tracks=[],this.metadataPromise=null,this.movieTimescale=-1,this.movieDurationInTimescale=-1,this.isQuickTime=!1,this.metadataTags={},this.currentMetadataKeys=null,this.isFragmented=!1,this.fragmentTrackDefaults=[],this.currentFragment=null,this.lastReadFragment=null,this.reader=e._reader}async computeDuration(){let e=await this.getTracks(),t=await Promise.all(e.map(e=>e.computeDuration()));return Math.max(0,...t)}async getTracks(){return await this.readMetadata(),this.tracks.map(e=>e.inputTrack)}async getMimeType(){await this.readMetadata();let e=await Promise.all(this.tracks.map(e=>e.inputTrack.getCodecParameterString()));return fr({isQuickTime:this.isQuickTime,hasVideo:this.tracks.some(e=>e.info?.type===`video`),hasAudio:this.tracks.some(e=>e.info?.type===`audio`),codecStrings:e.filter(Boolean)})}async getMetadataTags(){return await this.readMetadata(),this.metadataTags}readMetadata(){return this.metadataPromise??=(async()=>{let e=0;for(;;){let t=this.reader.requestSliceRange(e,8,16);if(t instanceof Promise&&(t=await t),!t)break;let n=e,r=pr(t);if(!r)break;if(r.name===`ftyp`)this.isQuickTime=Xr(t,4)===`qt `;else if(r.name===`moov`){let e=this.reader.requestSlice(t.filePos,r.contentSize);if(e instanceof Promise&&(e=await e),!e)break;this.moovSlice=e,this.readContiguousBoxes(this.moovSlice),this.tracks.sort((e,t)=>Number(t.disposition.default)-Number(e.disposition.default));for(let e of this.tracks){let t=e.editListPreviousSegmentDurations/this.movieTimescale;e.editListOffset-=Math.round(t*e.timescale)}break}e=n+r.totalSize}if(this.isFragmented&&this.reader.fileSize!==null){let e=this.reader.requestSlice(this.reader.fileSize-4,4);e instanceof Promise&&(e=await e),b(e);let t=V(e),n=this.reader.fileSize-t;if(n>=0&&n<=this.reader.fileSize-16){let e=this.reader.requestSliceRange(n,8,16);if(e instanceof Promise&&(e=await e),e){let t=pr(e);if(t&&t.name===`mfra`){let n=this.reader.requestSlice(e.filePos,t.contentSize);n instanceof Promise&&(n=await n),n&&this.readContiguousBoxes(n)}}}}})()}getSampleTableForTrack(e){if(e.sampleTable)return e.sampleTable;let t={sampleTimingEntries:[],sampleCompositionTimeOffsets:[],sampleSizes:[],keySampleIndices:null,chunkOffsets:[],sampleToChunk:[],presentationTimestamps:null,presentationTimestampIndexMap:null};e.sampleTable=t,b(this.moovSlice);let n=this.moovSlice.slice(e.sampleTableByteOffset);if(this.currentTrack=e,this.traverseBox(n),this.currentTrack=null,e.info?.type===`audio`&&e.info.codec&&j.includes(e.info.codec)&&t.sampleCompositionTimeOffsets.length===0){b(e.info?.type===`audio`);let n=Lt(e.info.codec),r=[],i=[];for(let a=0;a<t.sampleToChunk.length;a++){let o=t.sampleToChunk[a],s=t.sampleToChunk[a+1],c=(s?s.startChunkIndex:t.chunkOffsets.length)-o.startChunkIndex;for(let a=0;a<c;a++){let s=o.startSampleIndex+a*o.samplesPerChunk,c=s+o.samplesPerChunk,l=D(t.sampleTimingEntries,s,e=>e.startIndex),u=t.sampleTimingEntries[l],d=D(t.sampleTimingEntries,c,e=>e.startIndex),f=t.sampleTimingEntries[d],p=u.startDecodeTimestamp+(s-u.startIndex)*u.delta,m=f.startDecodeTimestamp+(c-f.startIndex)*f.delta-p,h=x(r);h&&h.delta===m?h.count++:r.push({startIndex:o.startChunkIndex+a,startDecodeTimestamp:p,count:1,delta:m});let g=o.samplesPerChunk*n.sampleSize*e.info.numberOfChannels;i.push(g)}o.startSampleIndex=o.startChunkIndex,o.samplesPerChunk=1}t.sampleTimingEntries=r,t.sampleSizes=i}if(t.sampleCompositionTimeOffsets.length>0){t.presentationTimestamps=[];for(let e of t.sampleTimingEntries)for(let n=0;n<e.count;n++)t.presentationTimestamps.push({presentationTimestamp:e.startDecodeTimestamp+n*e.delta,sampleIndex:e.startIndex+n});for(let e of t.sampleCompositionTimeOffsets)for(let n=0;n<e.count;n++){let r=e.startIndex+n,i=t.presentationTimestamps[r];i&&(i.presentationTimestamp+=e.offset)}t.presentationTimestamps.sort((e,t)=>e.presentationTimestamp-t.presentationTimestamp),t.presentationTimestampIndexMap=Array(t.presentationTimestamps.length).fill(-1);for(let e=0;e<t.presentationTimestamps.length;e++)t.presentationTimestampIndexMap[t.presentationTimestamps[e].sampleIndex]=e}return t}async readFragment(e){if(this.lastReadFragment?.moofOffset===e)return this.lastReadFragment;let t=this.reader.requestSliceRange(e,8,16);t instanceof Promise&&(t=await t),b(t);let n=pr(t);b(n?.name===`moof`);let r=this.reader.requestSlice(e,n.totalSize);r instanceof Promise&&(r=await r),b(r),this.traverseBox(r);let i=this.lastReadFragment;b(i&&i.moofOffset===e);for(let[,e]of i.trackData){let t=e.track,{fragmentPositionCache:n}=t;if(!e.startTimestampIsFinal){let r=t.fragmentLookupTable.find(e=>e.moofOffset===i.moofOffset);if(r)Er(e,r.timestamp);else{let t=D(n,i.moofOffset-1,e=>e.moofOffset);if(t!==-1){let r=n[t];Er(e,r.endTimestamp)}}e.startTimestampIsFinal=!0}let r=D(n,e.startTimestamp,e=>e.startTimestamp);(r===-1||n[r].moofOffset!==i.moofOffset)&&n.splice(r+1,0,{moofOffset:i.moofOffset,startTimestamp:e.startTimestamp,endTimestamp:e.endTimestamp})}return i}readContiguousBoxes(e){let t=e.filePos;for(;e.filePos-t<=e.length-8&&this.traverseBox(e););}*iterateContiguousBoxes(e){let t=e.filePos;for(;e.filePos-t<=e.length-8;){let t=e.filePos,n=pr(e);if(!n)break;yield{boxInfo:n,slice:e},e.filePos=t+n.totalSize}}traverseBox(e){let t=e.filePos,n=pr(e);if(!n)return!1;let r=e.filePos,i=t+n.totalSize;switch(n.name){case`mdia`:case`minf`:case`dinf`:case`mfra`:case`edts`:this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`mvhd`:{let t=z(e);e.skip(3),t===1?(e.skip(16),this.movieTimescale=V(e),this.movieDurationInTimescale=H(e)):(e.skip(8),this.movieTimescale=V(e),this.movieDurationInTimescale=V(e))}break;case`trak`:{let t={id:-1,demuxer:this,inputTrack:null,disposition:{...yt},info:null,timescale:-1,durationInMovieTimescale:-1,durationInMediaTimescale:-1,rotation:0,internalCodecId:null,name:null,languageCode:`und`,sampleTableByteOffset:-1,sampleTable:null,fragmentLookupTable:[],currentFragmentState:null,fragmentPositionCache:[],editListPreviousSegmentDurations:0,editListOffset:0};if(this.currentTrack=t,this.readContiguousBoxes(e.slice(r,n.contentSize)),t.id!==-1&&t.timescale!==-1&&t.info!==null){if(t.info.type===`video`&&t.info.width!==-1){let e=t;t.inputTrack=new ur(this.input,new br(e)),this.tracks.push(t)}else if(t.info.type===`audio`&&t.info.numberOfChannels!==-1){let e=t;t.inputTrack=new dr(this.input,new xr(e)),this.tracks.push(t)}}this.currentTrack=null}break;case`tkhd`:{let t=this.currentTrack;if(!t)break;let n=z(e),r=!!(Gr(e)&1);if(t.disposition.default=r,n===0)e.skip(8),t.id=V(e),e.skip(4),t.durationInMovieTimescale=V(e);else if(n===1)e.skip(16),t.id=V(e),e.skip(4),t.durationInMovieTimescale=H(e);else throw Error(`Incorrect track header version ${n}.`);e.skip(16);let i=we(Qe(Dr([mr(e),mr(e),hr(e),mr(e),mr(e),hr(e),mr(e),mr(e),hr(e)]),90));b(i===0||i===90||i===180||i===270),t.rotation=i}break;case`elst`:{let t=this.currentTrack;if(!t)break;let n=z(e);e.skip(3);let r=!1,i=0,a=V(e);for(let o=0;o<a;o++){let a=n===1?H(e):V(e),o=n===1?Jr(e):qr(e),s=mr(e);if(a!==0){if(r){console.warn(`Unsupported edit list: multiple edits are not currently supported. Only using first edit.`);break}if(o===-1){i+=a;continue}if(s!==1){console.warn(`Unsupported edit list entry: media rate must be 1.`);break}t.editListPreviousSegmentDurations=i,t.editListOffset=o,r=!0}}}break;case`mdhd`:{let t=this.currentTrack;if(!t)break;let n=z(e);e.skip(3),n===0?(e.skip(8),t.timescale=V(e),t.durationInMediaTimescale=V(e)):n===1&&(e.skip(16),t.timescale=V(e),t.durationInMediaTimescale=H(e));let r=B(e);if(r>0){t.languageCode=``;for(let e=0;e<3;e++)t.languageCode=String.fromCharCode(96+(r&31))+t.languageCode,r>>=5;et(t.languageCode)||(t.languageCode=`und`)}}break;case`hdlr`:{let t=this.currentTrack;if(!t)break;e.skip(8);let n=Xr(e,4);n===`vide`?t.info={type:`video`,width:-1,height:-1,codec:null,codecDescription:null,colorSpace:null,avcType:null,avcCodecInfo:null,hevcCodecInfo:null,vp9CodecInfo:null,av1CodecInfo:null}:n===`soun`&&(t.info={type:`audio`,numberOfChannels:-1,sampleRate:-1,codec:null,codecDescription:null,aacCodecInfo:null})}break;case`stbl`:{let i=this.currentTrack;if(!i)break;i.sampleTableByteOffset=t,this.readContiguousBoxes(e.slice(r,n.contentSize))}break;case`stsd`:{let t=this.currentTrack;if(!t||t.info===null||t.sampleTable)break;let n=z(e);e.skip(3);let r=V(e);for(let i=0;i<r;i++){let r=e.filePos,i=pr(e);if(!i)break;t.internalCodecId=i.name;let a=i.name.toLowerCase();if(t.info.type===`video`)a===`avc1`||a===`avc3`?(t.info.codec=`avc`,t.info.avcType=a===`avc1`?1:3):a===`hvc1`||a===`hev1`?t.info.codec=`hevc`:a===`vp08`?t.info.codec=`vp8`:a===`vp09`?t.info.codec=`vp9`:a===`av01`?t.info.codec=`av1`:console.warn(`Unsupported video codec (sample entry type '${i.name}').`),e.skip(24),t.info.width=B(e),t.info.height=B(e),e.skip(50),this.readContiguousBoxes(e.slice(e.filePos,r+i.totalSize-e.filePos));else{a===`mp4a`||(a===`opus`?t.info.codec=`opus`:a===`flac`?t.info.codec=`flac`:a===`twos`||a===`sowt`||a===`raw `||a===`in24`||a===`in32`||a===`fl32`||a===`fl64`||a===`lpcm`||a===`ipcm`||a===`fpcm`||(a===`ulaw`?t.info.codec=`ulaw`:a===`alaw`?t.info.codec=`alaw`:console.warn(`Unsupported audio codec (sample entry type '${i.name}').`))),e.skip(8);let o=B(e);e.skip(6);let s=B(e),c=B(e);e.skip(4);let l=V(e)/65536;if(n===0&&o>0){if(o===1)e.skip(4),c=8*V(e),e.skip(8);else if(o===2){e.skip(4),l=Yr(e),s=V(e),e.skip(4),c=V(e);let n=V(e);if(e.skip(8),a===`lpcm`){let e=c+7>>3,r=!!(n&1),i=!!(n&2),a=n&4?-1:0;c>0&&c<=64&&(r?c===32&&(t.info.codec=i?`pcm-f32be`:`pcm-f32`):a&1<<e-1?e===1?t.info.codec=`pcm-s8`:e===2?t.info.codec=i?`pcm-s16be`:`pcm-s16`:e===3?t.info.codec=i?`pcm-s24be`:`pcm-s24`:e===4&&(t.info.codec=i?`pcm-s32be`:`pcm-s32`):e===1&&(t.info.codec=`pcm-u8`)),t.info.codec===null&&console.warn(`Unsupported PCM format.`)}}}t.info.codec===`opus`&&(l=48e3),t.info.numberOfChannels=s,t.info.sampleRate=l,a===`twos`?c===8?t.info.codec=`pcm-s8`:c===16?t.info.codec=`pcm-s16be`:(console.warn(`Unsupported sample size ${c} for codec 'twos'.`),t.info.codec=null):a===`sowt`?c===8?t.info.codec=`pcm-s8`:c===16?t.info.codec=`pcm-s16`:(console.warn(`Unsupported sample size ${c} for codec 'sowt'.`),t.info.codec=null):a===`raw `?t.info.codec=`pcm-u8`:a===`in24`?t.info.codec=`pcm-s24be`:a===`in32`?t.info.codec=`pcm-s32be`:a===`fl32`?t.info.codec=`pcm-f32be`:a===`fl64`?t.info.codec=`pcm-f64be`:a===`ipcm`?t.info.codec=`pcm-s16be`:a===`fpcm`&&(t.info.codec=`pcm-f32be`),this.readContiguousBoxes(e.slice(e.filePos,r+i.totalSize-e.filePos))}}}break;case`avcC`:{let t=this.currentTrack;if(!t)break;b(t.info),t.info.codecDescription=R(e,n.contentSize)}break;case`hvcC`:{let t=this.currentTrack;if(!t)break;b(t.info),t.info.codecDescription=R(e,n.contentSize)}break;case`vpcC`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`video`),e.skip(4);let n=z(e),r=z(e),i=z(e),a=i>>4,o=i>>1&7,s=i&1,c=z(e),l=z(e),u=z(e);t.info.vp9CodecInfo={profile:n,level:r,bitDepth:a,chromaSubsampling:o,videoFullRangeFlag:s,colourPrimaries:c,transferCharacteristics:l,matrixCoefficients:u}}break;case`av1C`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`video`),e.skip(1);let n=z(e),r=n>>5,i=n&31,a=z(e),o=a>>7,s=a>>6&1,c=a>>5&1,l=a>>4&1,u=a>>3&1,d=a>>2&1,f=a&3,p=r===2&&s?c?12:10:s?10:8;t.info.av1CodecInfo={profile:r,level:i,tier:o,bitDepth:p,monochrome:l,chromaSubsamplingX:u,chromaSubsamplingY:d,chromaSamplePosition:f}}break;case`colr`:{let t=this.currentTrack;if(!t||(b(t.info?.type===`video`),Xr(e,4)!==`nclx`))break;let n=B(e),r=B(e),i=B(e),a=!!(z(e)&128);t.info.colorSpace={primaries:Ae[n],transfer:Me[r],matrix:Pe[i],fullRange:a}}break;case`wave`:this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`esds`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`audio`),e.skip(4),b(z(e)===3),gr(e),e.skip(2);let n=z(e),r=(n&128)!=0,i=(n&64)!=0,a=(n&32)!=0;if(r&&e.skip(2),i){let t=z(e);e.skip(t)}a&&e.skip(2),b(z(e)===4);let o=gr(e),s=e.filePos,c=z(e);if(c===64||c===103?(t.info.codec=`aac`,t.info.aacCodecInfo={isMpeg2:c===103}):c===105||c===107?t.info.codec=`mp3`:c===221?t.info.codec=`vorbis`:console.warn(`Unsupported audio codec (objectTypeIndication ${c}) - discarding track.`),e.skip(12),o>e.filePos-s){b(z(e)===5);let n=gr(e);if(t.info.codecDescription=R(e,n),t.info.codec===`aac`){let e=Pt(t.info.codecDescription);e.numberOfChannels!==null&&(t.info.numberOfChannels=e.numberOfChannels),e.sampleRate!==null&&(t.info.sampleRate=e.sampleRate)}}}break;case`enda`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`audio`),B(e)&255&&(t.info.codec===`pcm-s16be`?t.info.codec=`pcm-s16`:t.info.codec===`pcm-s24be`?t.info.codec=`pcm-s24`:t.info.codec===`pcm-s32be`?t.info.codec=`pcm-s32`:t.info.codec===`pcm-f32be`?t.info.codec=`pcm-f32`:t.info.codec===`pcm-f64be`&&(t.info.codec=`pcm-f64`))}break;case`pcmC`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`audio`),e.skip(4);let n=!!(z(e)&1),r=z(e);t.info.codec===`pcm-s16be`?n?r===16?t.info.codec=`pcm-s16`:r===24?t.info.codec=`pcm-s24`:r===32?t.info.codec=`pcm-s32`:(console.warn(`Invalid ipcm sample size ${r}.`),t.info.codec=null):r===16?t.info.codec=`pcm-s16be`:r===24?t.info.codec=`pcm-s24be`:r===32?t.info.codec=`pcm-s32be`:(console.warn(`Invalid ipcm sample size ${r}.`),t.info.codec=null):t.info.codec===`pcm-f32be`&&(n?r===32?t.info.codec=`pcm-f32`:r===64?t.info.codec=`pcm-f64`:(console.warn(`Invalid fpcm sample size ${r}.`),t.info.codec=null):r===32?t.info.codec=`pcm-f32be`:r===64?t.info.codec=`pcm-f64be`:(console.warn(`Invalid fpcm sample size ${r}.`),t.info.codec=null));break}case`dOps`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`audio`),e.skip(1);let n=z(e),r=B(e),i=V(e),a=Kr(e),o=z(e),s;s=o===0?new Uint8Array:R(e,2+n);let c=new Uint8Array(19+s.byteLength),l=new DataView(c.buffer);l.setUint32(0,1332770163,!1),l.setUint32(4,1214603620,!1),l.setUint8(8,1),l.setUint8(9,n),l.setUint16(10,r,!0),l.setUint32(12,i,!0),l.setInt16(16,a,!0),l.setUint8(18,o),c.set(s,19),t.info.codecDescription=c,t.info.numberOfChannels=n}break;case`dfLa`:{let t=this.currentTrack;if(!t)break;b(t.info?.type===`audio`),e.skip(4);let n=e.filePos;for(;e.filePos<i;){let n=z(e),r=Gr(e);if((n&127)===Tn.STREAMINFO){e.skip(10);let n=V(e),r=n>>>12,i=(n>>9&7)+1;t.info.sampleRate=r,t.info.numberOfChannels=i,e.skip(20)}else e.skip(r);if(n&128)break}let r=e.filePos;e.filePos=n;let a=R(e,r-n),o=new Uint8Array(4+a.byteLength);new DataView(o.buffer).setUint32(0,1716281667,!1),o.set(a,4),t.info.codecDescription=o}break;case`stts`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=0,i=0;for(let a=0;a<n;a++){let n=V(e),a=V(e);t.sampleTable.sampleTimingEntries.push({startIndex:r,startDecodeTimestamp:i,count:n,delta:a}),r+=n,i+=n*a}}break;case`ctts`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=0;for(let i=0;i<n;i++){let n=V(e),i=qr(e);t.sampleTable.sampleCompositionTimeOffsets.push({startIndex:r,count:n,offset:i}),r+=n}}break;case`stsz`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e),r=V(e);if(n===0)for(let n=0;n<r;n++){let n=V(e);t.sampleTable.sampleSizes.push(n)}else t.sampleTable.sampleSizes.push(n)}break;case`stz2`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4),e.skip(3);let n=z(e),r=V(e),i=new S(R(e,Math.ceil(r*n/8)));for(let e=0;e<r;e++){let e=i.readBits(n);t.sampleTable.sampleSizes.push(e)}}break;case`stss`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4),t.sampleTable.keySampleIndices=[];let n=V(e);for(let r=0;r<n;r++){let n=V(e)-1;t.sampleTable.keySampleIndices.push(n)}t.sampleTable.keySampleIndices[0]!==0&&t.sampleTable.keySampleIndices.unshift(0)}break;case`stsc`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=V(e)-1,r=V(e),i=V(e);t.sampleTable.sampleToChunk.push({startSampleIndex:-1,startChunkIndex:n,samplesPerChunk:r,sampleDescriptionIndex:i})}let r=0;for(let e=0;e<t.sampleTable.sampleToChunk.length;e++)if(t.sampleTable.sampleToChunk[e].startSampleIndex=r,e<t.sampleTable.sampleToChunk.length-1){let n=t.sampleTable.sampleToChunk[e+1].startChunkIndex-t.sampleTable.sampleToChunk[e].startChunkIndex;r+=n*t.sampleTable.sampleToChunk[e].samplesPerChunk}}break;case`stco`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=V(e);t.sampleTable.chunkOffsets.push(n)}}break;case`co64`:{let t=this.currentTrack;if(!t||!t.sampleTable)break;e.skip(4);let n=V(e);for(let r=0;r<n;r++){let n=H(e);t.sampleTable.chunkOffsets.push(n)}}break;case`mvex`:this.isFragmented=!0,this.readContiguousBoxes(e.slice(r,n.contentSize));break;case`mehd`:{let t=z(e);e.skip(3),this.movieDurationInTimescale=t===1?H(e):V(e)}break;case`trex`:{e.skip(4);let t=V(e),n=V(e),r=V(e),i=V(e),a=V(e);this.fragmentTrackDefaults.push({trackId:t,defaultSampleDescriptionIndex:n,defaultSampleDuration:r,defaultSampleSize:i,defaultSampleFlags:a})}break;case`tfra`:{let t=z(e);e.skip(3);let n=V(e),r=this.tracks.find(e=>e.id===n);if(!r)break;let i=V(e),a=(i&48)>>4,o=(i&12)>>2,s=i&3,c=[z,B,Gr,V],l=c[a],u=c[o],d=c[s],f=V(e);for(let n=0;n<f;n++){let n=t===1?H(e):V(e),i=t===1?H(e):V(e);l(e),u(e),d(e),r.fragmentLookupTable.push({timestamp:n,moofOffset:i})}r.fragmentLookupTable.sort((e,t)=>e.timestamp-t.timestamp);for(let e=0;e<r.fragmentLookupTable.length-1;e++){let t=r.fragmentLookupTable[e],n=r.fragmentLookupTable[e+1];t.timestamp===n.timestamp&&(r.fragmentLookupTable.splice(e+1,1),e--)}}break;case`moof`:this.currentFragment={moofOffset:t,moofSize:n.totalSize,implicitBaseDataOffset:t,trackData:new Map},this.readContiguousBoxes(e.slice(r,n.contentSize)),this.lastReadFragment=this.currentFragment,this.currentFragment=null;break;case`traf`:if(b(this.currentFragment),this.readContiguousBoxes(e.slice(r,n.contentSize)),this.currentTrack){let e=this.currentFragment.trackData.get(this.currentTrack.id);if(e){let{currentFragmentState:t}=this.currentTrack;b(t),t.startTimestamp!==null&&(Er(e,t.startTimestamp),e.startTimestampIsFinal=!0)}this.currentTrack.currentFragmentState=null,this.currentTrack=null}break;case`tfhd`:{b(this.currentFragment),e.skip(1);let t=Gr(e),n=!!(t&1),r=!!(t&2),i=!!(t&8),a=!!(t&16),o=!!(t&32),s=!!(t&65536),c=!!(t&131072),l=V(e),u=this.tracks.find(e=>e.id===l);if(!u)break;let d=this.fragmentTrackDefaults.find(e=>e.trackId===l);this.currentTrack=u,u.currentFragmentState={baseDataOffset:this.currentFragment.implicitBaseDataOffset,sampleDescriptionIndex:d?.defaultSampleDescriptionIndex??null,defaultSampleDuration:d?.defaultSampleDuration??null,defaultSampleSize:d?.defaultSampleSize??null,defaultSampleFlags:d?.defaultSampleFlags??null,startTimestamp:null},n?u.currentFragmentState.baseDataOffset=H(e):c&&(u.currentFragmentState.baseDataOffset=this.currentFragment.moofOffset),r&&(u.currentFragmentState.sampleDescriptionIndex=V(e)),i&&(u.currentFragmentState.defaultSampleDuration=V(e)),a&&(u.currentFragmentState.defaultSampleSize=V(e)),o&&(u.currentFragmentState.defaultSampleFlags=V(e)),s&&(u.currentFragmentState.defaultSampleDuration=0)}break;case`tfdt`:{let t=this.currentTrack;if(!t)break;b(t.currentFragmentState);let n=z(e);e.skip(3);let r=n===0?V(e):H(e);t.currentFragmentState.startTimestamp=r}break;case`trun`:{let t=this.currentTrack;if(!t)break;if(b(this.currentFragment),b(t.currentFragmentState),this.currentFragment.trackData.has(t.id)){console.warn(`Can't have two trun boxes for the same track in one fragment. Ignoring...`);break}let n=z(e),r=Gr(e),i=!!(r&1),a=!!(r&4),o=!!(r&256),s=!!(r&512),c=!!(r&1024),l=!!(r&2048),u=V(e),d=t.currentFragmentState.baseDataOffset;i&&(d+=qr(e));let f=null;a&&(f=V(e));let p=d;if(u===0){this.currentFragment.implicitBaseDataOffset=p;break}let m=0,h={track:t,startTimestamp:0,endTimestamp:0,firstKeyFrameTimestamp:null,samples:[],presentationTimestamps:[],startTimestampIsFinal:!1};this.currentFragment.trackData.set(t.id,h);for(let r=0;r<u;r++){let i;o?i=V(e):(b(t.currentFragmentState.defaultSampleDuration!==null),i=t.currentFragmentState.defaultSampleDuration);let a;s?a=V(e):(b(t.currentFragmentState.defaultSampleSize!==null),a=t.currentFragmentState.defaultSampleSize);let u;c?u=V(e):(b(t.currentFragmentState.defaultSampleFlags!==null),u=t.currentFragmentState.defaultSampleFlags),r===0&&f!==null&&(u=f);let d=0;l&&(d=n===0?V(e):qr(e));let g=!(u&65536);h.samples.push({presentationTimestamp:m+d,duration:i,byteOffset:p,byteSize:a,isKeyFrame:g}),p+=a,m+=i}h.presentationTimestamps=h.samples.map((e,t)=>({presentationTimestamp:e.presentationTimestamp,sampleIndex:t})).sort((e,t)=>e.presentationTimestamp-t.presentationTimestamp);for(let e=0;e<h.presentationTimestamps.length;e++){let t=h.presentationTimestamps[e],n=h.samples[t.sampleIndex];h.firstKeyFrameTimestamp===null&&n.isKeyFrame&&(h.firstKeyFrameTimestamp=n.presentationTimestamp),e<h.presentationTimestamps.length-1&&(n.duration=h.presentationTimestamps[e+1].presentationTimestamp-t.presentationTimestamp)}let g=h.samples[h.presentationTimestamps[0].sampleIndex],_=h.samples[x(h.presentationTimestamps).sampleIndex];h.startTimestamp=g.presentationTimestamp,h.endTimestamp=_.presentationTimestamp+_.duration,this.currentFragment.implicitBaseDataOffset=p}break;case`udta`:{let t=this.iterateContiguousBoxes(e.slice(r,n.contentSize));for(let{boxInfo:e,slice:n}of t){if(e.name!==`meta`&&!this.currentTrack){let t=n.filePos;this.metadataTags.raw??={},e.name[0]===`©`?this.metadataTags.raw[e.name]??=I(n):this.metadataTags.raw[e.name]??=R(n,e.contentSize),n.filePos=t}switch(e.name){case`meta`:n.skip(-e.headerSize),this.traverseBox(n);break;case`©nam`:case`name`:this.currentTrack?this.currentTrack.name=De.decode(R(n,e.contentSize)):this.metadataTags.title??=I(n);break;case`©des`:this.currentTrack||(this.metadataTags.description??=I(n));break;case`©ART`:this.currentTrack||(this.metadataTags.artist??=I(n));break;case`©alb`:this.currentTrack||(this.metadataTags.album??=I(n));break;case`albr`:this.currentTrack||(this.metadataTags.albumArtist??=I(n));break;case`©gen`:this.currentTrack||(this.metadataTags.genre??=I(n));break;case`©day`:if(!this.currentTrack){let e=new Date(I(n));Number.isNaN(e.getTime())||(this.metadataTags.date??=e)}break;case`©cmt`:this.currentTrack||(this.metadataTags.comment??=I(n));break;case`©lyr`:this.currentTrack||(this.metadataTags.lyrics??=I(n));break}}}break;case`meta`:{if(this.currentTrack)break;let t=V(e)!==0;this.currentMetadataKeys=new Map,t?this.readContiguousBoxes(e.slice(r,n.contentSize)):this.readContiguousBoxes(e.slice(r+4,n.contentSize-4)),this.currentMetadataKeys=null}break;case`keys`:{if(!this.currentMetadataKeys)break;e.skip(4);let t=V(e);for(let n=0;n<t;n++){let t=V(e);e.skip(4);let r=De.decode(R(e,t-8));this.currentMetadataKeys.set(n+1,r)}}break;case`ilst`:{if(!this.currentMetadataKeys)break;let t=this.iterateContiguousBoxes(e.slice(r,n.contentSize));for(let{boxInfo:e,slice:n}of t){let t=e.name,r=(t.charCodeAt(0)<<24)+(t.charCodeAt(1)<<16)+(t.charCodeAt(2)<<8)+t.charCodeAt(3);this.currentMetadataKeys.has(r)&&(t=this.currentMetadataKeys.get(r));let i=_r(n);switch(this.metadataTags.raw??={},this.metadataTags.raw[t]??=i,t){case`©nam`:case`titl`:case`com.apple.quicktime.title`:case`title`:typeof i==`string`&&(this.metadataTags.title??=i);break;case`©des`:case`desc`:case`dscp`:case`com.apple.quicktime.description`:case`description`:typeof i==`string`&&(this.metadataTags.description??=i);break;case`©ART`:case`com.apple.quicktime.artist`:case`artist`:typeof i==`string`&&(this.metadataTags.artist??=i);break;case`©alb`:case`albm`:case`com.apple.quicktime.album`:case`album`:typeof i==`string`&&(this.metadataTags.album??=i);break;case`aART`:case`album_artist`:typeof i==`string`&&(this.metadataTags.albumArtist??=i);break;case`©cmt`:case`com.apple.quicktime.comment`:case`comment`:typeof i==`string`&&(this.metadataTags.comment??=i);break;case`©gen`:case`gnre`:case`com.apple.quicktime.genre`:case`genre`:typeof i==`string`&&(this.metadataTags.genre??=i);break;case`©lyr`:case`lyrics`:typeof i==`string`&&(this.metadataTags.lyrics??=i);break;case`©day`:case`rldt`:case`com.apple.quicktime.creationdate`:case`date`:if(typeof i==`string`){let e=new Date(i);Number.isNaN(e.getTime())||(this.metadataTags.date??=e)}break;case`covr`:case`com.apple.quicktime.artwork`:i instanceof gt?(this.metadataTags.images??=[],this.metadataTags.images.push({data:i.data,kind:`coverFront`,mimeType:i.mimeType})):i instanceof Uint8Array&&(this.metadataTags.images??=[],this.metadataTags.images.push({data:i,kind:`coverFront`,mimeType:`image/*`}));break;case`track`:if(typeof i==`string`){let e=i.split(`/`),t=Number.parseInt(e[0],10),n=e[1]&&Number.parseInt(e[1],10);Number.isInteger(t)&&t>0&&(this.metadataTags.trackNumber??=t),n&&Number.isInteger(n)&&n>0&&(this.metadataTags.tracksTotal??=n)}break;case`trkn`:if(i instanceof Uint8Array&&i.length>=6){let e=T(i),t=e.getUint16(2,!1),n=e.getUint16(4,!1);t>0&&(this.metadataTags.trackNumber??=t),n>0&&(this.metadataTags.tracksTotal??=n)}break;case`disc`:case`disk`:if(i instanceof Uint8Array&&i.length>=6){let e=T(i),t=e.getUint16(2,!1),n=e.getUint16(4,!1);t>0&&(this.metadataTags.discNumber??=t),n>0&&(this.metadataTags.discsTotal??=n)}break}}}break}return e.filePos=i,!0}},yr=class{constructor(e){this.internalTrack=e,this.packetToSampleIndex=new WeakMap,this.packetToFragmentLocation=new WeakMap}getId(){return this.internalTrack.id}getCodec(){throw Error(`Not implemented on base class.`)}getInternalCodecId(){return this.internalTrack.internalCodecId}getName(){return this.internalTrack.name}getLanguageCode(){return this.internalTrack.languageCode}getTimeResolution(){return this.internalTrack.timescale}getDisposition(){return this.internalTrack.disposition}async computeDuration(){let e=await this.getPacket(1/0,{metadataOnly:!0});return(e?.timestamp??0)+(e?.duration??0)}async getFirstTimestamp(){return(await this.getFirstPacket({metadataOnly:!0}))?.timestamp??0}async getFirstPacket(e){let t=await this.fetchPacketForSampleIndex(0,e);return t||!this.internalTrack.demuxer.isFragmented?t:this.performFragmentedLookup(null,e=>e.trackData.get(this.internalTrack.id)?{sampleIndex:0,correctSampleFound:!0}:{sampleIndex:-1,correctSampleFound:!1},-1/0,1/0,e)}mapTimestampIntoTimescale(e){return Ze(e*this.internalTrack.timescale)+this.internalTrack.editListOffset}async getPacket(e,t){let n=this.mapTimestampIntoTimescale(e),r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=Sr(r,n),a=await this.fetchPacketForSampleIndex(i,t);return!Or(r)||!this.internalTrack.demuxer.isFragmented?a:this.performFragmentedLookup(null,e=>{let t=e.trackData.get(this.internalTrack.id);if(!t)return{sampleIndex:-1,correctSampleFound:!1};let r=D(t.presentationTimestamps,n,e=>e.presentationTimestamp);return{sampleIndex:r===-1?-1:t.presentationTimestamps[r].sampleIndex,correctSampleFound:r!==-1&&n<t.endTimestamp}},n,n,t)}async getNextPacket(e,t){let n=this.packetToSampleIndex.get(e);if(n!==void 0)return this.fetchPacketForSampleIndex(n+1,t);let r=this.packetToFragmentLocation.get(e);if(r===void 0)throw Error(`Packet was not created from this track.`);return this.performFragmentedLookup(r.fragment,e=>{if(e===r.fragment){let t=e.trackData.get(this.internalTrack.id);if(r.sampleIndex+1<t.samples.length)return{sampleIndex:r.sampleIndex+1,correctSampleFound:!0}}else if(e.trackData.get(this.internalTrack.id))return{sampleIndex:0,correctSampleFound:!0};return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,t)}async getKeyPacket(e,t){let n=this.mapTimestampIntoTimescale(e),r=this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),i=Cr(r,n),a=await this.fetchPacketForSampleIndex(i,t);return!Or(r)||!this.internalTrack.demuxer.isFragmented?a:this.performFragmentedLookup(null,e=>{let t=e.trackData.get(this.internalTrack.id);if(!t)return{sampleIndex:-1,correctSampleFound:!1};let r=He(t.presentationTimestamps,e=>t.samples[e.sampleIndex].isKeyFrame&&e.presentationTimestamp<=n);return{sampleIndex:r===-1?-1:t.presentationTimestamps[r].sampleIndex,correctSampleFound:r!==-1&&n<t.endTimestamp}},n,n,t)}async getNextKeyPacket(e,t){let n=this.packetToSampleIndex.get(e);if(n!==void 0){let e=Tr(this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),n);return this.fetchPacketForSampleIndex(e,t)}let r=this.packetToFragmentLocation.get(e);if(r===void 0)throw Error(`Packet was not created from this track.`);return this.performFragmentedLookup(r.fragment,e=>{if(e===r.fragment){let t=e.trackData.get(this.internalTrack.id).samples.findIndex((e,t)=>e.isKeyFrame&&t>r.sampleIndex);if(t!==-1)return{sampleIndex:t,correctSampleFound:!0}}else{let t=e.trackData.get(this.internalTrack.id);if(t&&t.firstKeyFrameTimestamp!==null){let e=t.samples.findIndex(e=>e.isKeyFrame);return b(e!==-1),{sampleIndex:e,correctSampleFound:!0}}}return{sampleIndex:-1,correctSampleFound:!1}},-1/0,1/0,t)}async fetchPacketForSampleIndex(e,t){if(e===-1)return null;let n=wr(this.internalTrack.demuxer.getSampleTableForTrack(this.internalTrack),e);if(!n)return null;let r;if(t.metadataOnly)r=jn;else{let e=this.internalTrack.demuxer.reader.requestSlice(n.sampleOffset,n.sampleSize);e instanceof Promise&&(e=await e),b(e),r=R(e,n.sampleSize)}let i=(n.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,a=n.duration/this.internalTrack.timescale,o=new P(r,n.isKeyFrame?`key`:`delta`,i,a,e,n.sampleSize);return this.packetToSampleIndex.set(o,e),o}async fetchPacketInFragment(e,t,n){if(t===-1)return null;let r=e.trackData.get(this.internalTrack.id).samples[t];b(r);let i;if(n.metadataOnly)i=jn;else{let e=this.internalTrack.demuxer.reader.requestSlice(r.byteOffset,r.byteSize);e instanceof Promise&&(e=await e),b(e),i=R(e,r.byteSize)}let a=(r.presentationTimestamp-this.internalTrack.editListOffset)/this.internalTrack.timescale,o=r.duration/this.internalTrack.timescale,s=new P(i,r.isKeyFrame?`key`:`delta`,a,o,e.moofOffset+t,r.byteSize);return this.packetToFragmentLocation.set(s,{fragment:e,sampleIndex:t}),s}async performFragmentedLookup(e,t,n,r,i){let a=this.internalTrack.demuxer,o=null,s=null,c=-1;if(e){let{sampleIndex:n,correctSampleFound:r}=t(e);if(r)return this.fetchPacketInFragment(e,n,i);n!==-1&&(s=e,c=n)}let l=D(this.internalTrack.fragmentLookupTable,n,e=>e.timestamp),u=l===-1?null:this.internalTrack.fragmentLookupTable[l],d=D(this.internalTrack.fragmentPositionCache,n,e=>e.startTimestamp),f=d===-1?null:this.internalTrack.fragmentPositionCache[d],p=Math.max(u?.moofOffset??0,f?.moofOffset??0)||null,m;for(e?p===null||e.moofOffset>=p?(m=e.moofOffset+e.moofSize,o=e):m=p:m=p??0;;){if(o){let e=o.trackData.get(this.internalTrack.id);if(e&&e.startTimestamp>r)break}let e=a.reader.requestSliceRange(m,8,16);if(e instanceof Promise&&(e=await e),!e)break;let n=m,l=pr(e);if(!l)break;if(l.name===`moof`){o=await a.readFragment(n);let{sampleIndex:e,correctSampleFound:r}=t(o);if(r)return this.fetchPacketInFragment(o,e,i);e!==-1&&(s=o,c=e)}m=n+l.totalSize}if(u&&(!s||s.moofOffset<u.moofOffset)){let e=this.internalTrack.fragmentLookupTable[l-1];b(!e||e.timestamp<u.timestamp);let n=e?.timestamp??-1/0;return this.performFragmentedLookup(null,t,n,r,i)}return s?this.fetchPacketInFragment(s,c,i):null}},br=class extends yr{constructor(e){super(e),this.decoderConfigPromise=null,this.internalTrack=e}getCodec(){return this.internalTrack.info.codec}getCodedWidth(){return this.internalTrack.info.width}getCodedHeight(){return this.internalTrack.info.height}getRotation(){return this.internalTrack.rotation}async getColorSpace(){return{primaries:this.internalTrack.info.colorSpace?.primaries,transfer:this.internalTrack.info.colorSpace?.transfer,matrix:this.internalTrack.info.colorSpace?.matrix,fullRange:this.internalTrack.info.colorSpace?.fullRange}}async canBeTransparent(){return!1}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfigPromise??=(async()=>{if(this.internalTrack.info.codec===`vp9`&&!this.internalTrack.info.vp9CodecInfo){let e=await this.getFirstPacket({});this.internalTrack.info.vp9CodecInfo=e&&bn(e.data)}else if(this.internalTrack.info.codec===`av1`&&!this.internalTrack.info.av1CodecInfo){let e=await this.getFirstPacket({});this.internalTrack.info.av1CodecInfo=e&&Sn(e.data)}return{codec:kt(this.internalTrack.info),codedWidth:this.internalTrack.info.width,codedHeight:this.internalTrack.info.height,description:this.internalTrack.info.codecDescription??void 0,colorSpace:this.internalTrack.info.colorSpace??void 0}})():null}},xr=class extends yr{constructor(e){super(e),this.decoderConfig=null,this.internalTrack=e}getCodec(){return this.internalTrack.info.codec}getNumberOfChannels(){return this.internalTrack.info.numberOfChannels}getSampleRate(){return this.internalTrack.info.sampleRate}async getDecoderConfig(){return this.internalTrack.info.codec?this.decoderConfig??={codec:jt(this.internalTrack.info),numberOfChannels:this.internalTrack.info.numberOfChannels,sampleRate:this.internalTrack.info.sampleRate,description:this.internalTrack.info.codecDescription??void 0}:null}};let Sr=(e,t)=>{if(e.presentationTimestamps){let n=D(e.presentationTimestamps,t,e=>e.presentationTimestamp);return n===-1?-1:e.presentationTimestamps[n].sampleIndex}else{let n=D(e.sampleTimingEntries,t,e=>e.startDecodeTimestamp);if(n===-1)return-1;let r=e.sampleTimingEntries[n];return r.startIndex+Math.min(Math.floor((t-r.startDecodeTimestamp)/r.delta),r.count-1)}},Cr=(e,t)=>{if(!e.keySampleIndices)return Sr(e,t);if(e.presentationTimestamps){let n=D(e.presentationTimestamps,t,e=>e.presentationTimestamp);if(n===-1)return-1;for(let t=n;t>=0;t--){let n=e.presentationTimestamps[t].sampleIndex;if(Be(e.keySampleIndices,n,e=>e)!==-1)return n}return-1}else{let n=Sr(e,t),r=D(e.keySampleIndices,n,e=>e);return e.keySampleIndices[r]??-1}},wr=(e,t)=>{let n=D(e.sampleTimingEntries,t,e=>e.startIndex),r=e.sampleTimingEntries[n];if(!r||r.startIndex+r.count<=t)return null;let i=r.startDecodeTimestamp+(t-r.startIndex)*r.delta,a=D(e.sampleCompositionTimeOffsets,t,e=>e.startIndex),o=e.sampleCompositionTimeOffsets[a];o&&t-o.startIndex<o.count&&(i+=o.offset);let s=e.sampleSizes[Math.min(t,e.sampleSizes.length-1)],c=D(e.sampleToChunk,t,e=>e.startSampleIndex),l=e.sampleToChunk[c];b(l);let u=l.startChunkIndex+Math.floor((t-l.startSampleIndex)/l.samplesPerChunk),d=e.chunkOffsets[u],f=l.startSampleIndex+(u-l.startChunkIndex)*l.samplesPerChunk,p=0,m=d;if(e.sampleSizes.length===1)m+=s*(t-f),p+=s*l.samplesPerChunk;else for(let n=f;n<f+l.samplesPerChunk;n++){let r=e.sampleSizes[n];n<t&&(m+=r),p+=r}let h=r.delta;if(e.presentationTimestamps){let n=e.presentationTimestampIndexMap[t];b(n!==void 0),n<e.presentationTimestamps.length-1&&(h=e.presentationTimestamps[n+1].presentationTimestamp-i)}return{presentationTimestamp:i,duration:h,sampleOffset:m,sampleSize:s,chunkOffset:d,chunkSize:p,isKeyFrame:e.keySampleIndices?Be(e.keySampleIndices,t,e=>e)!==-1:!0}},Tr=(e,t)=>{if(!e.keySampleIndices)return t+1;let n=D(e.keySampleIndices,t,e=>e);return e.keySampleIndices[n+1]??-1},Er=(e,t)=>{e.startTimestamp+=t,e.endTimestamp+=t;for(let n of e.samples)n.presentationTimestamp+=t;for(let n of e.presentationTimestamps)n.presentationTimestamp+=t},Dr=e=>{let[t,,,n]=e,r=Math.hypot(t,n),i=t/r,a=n/r,o=-Math.atan2(a,i)*(180/Math.PI);return Number.isFinite(o)?o:0},Or=e=>e.sampleSizes.length===0;
|
|
88
88
|
/*!
|
|
89
89
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
90
90
|
*
|
|
@@ -92,7 +92,7 @@ var _r=class extends wn{constructor(e){super(e),this.moovSlice=null,this.current
|
|
|
92
92
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
93
93
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
94
94
|
*/
|
|
95
|
-
var
|
|
95
|
+
var kr=class{},Ar=class extends kr{async _getMajorBrand(e){let t=e._reader.requestSlice(0,12);return t instanceof Promise&&(t=await t),!t||(t.skip(4),Xr(t,4)!==`ftyp`)?null:Xr(t,4)}_createDemuxer(e){return new vr(e)}};let jr=new class extends Ar{async _canReadInput(e){let t=await this._getMajorBrand(e);return!!t&&t!==`qt `}get name(){return`MP4`}get mimeType(){return`video/mp4`}};var Mr={};
|
|
96
96
|
/*!
|
|
97
97
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
98
98
|
*
|
|
@@ -100,7 +100,7 @@ var Or=class{},kr=class extends Or{async _getMajorBrand(e){let t=e._reader.reque
|
|
|
100
100
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
101
101
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
102
102
|
*/
|
|
103
|
-
let Mr=jr===void 0?void 0:jr;var Nr=class{constructor(){this._disposed=!1,this._sizePromise=null,this.onread=null}async getSizeOrNull(){if(this._disposed)throw new L;return this._sizePromise??=Promise.resolve(this._retrieveSize())}async getSize(){if(this._disposed)throw new L;let e=await this.getSizeOrNull();if(e===null)throw Error(`Cannot determine the size of an unsized source.`);return e}},Pr=class extends Nr{constructor(e,t={}){if(!(e instanceof Blob))throw TypeError(`blob must be a Blob.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.maxCacheSize!==void 0&&(!pt(t.maxCacheSize)||t.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);super(),this._readers=new WeakMap,this._blob=e,this._orchestrator=new zr({maxCacheSize:t.maxCacheSize??8*2**20,maxWorkerCount:4,runWorker:this._runWorker.bind(this),prefetchProfile:Rr.fileSystem})}_retrieveSize(){let e=this._blob.size;return this._orchestrator.fileSize=e,e}_read(e,t){return this._orchestrator.read(e,t)}async _runWorker(e){let t=this._readers.get(e);for(t===void 0&&(t=(`stream`in this._blob)&&!rt()?this._blob.slice(e.currentPos).stream().getReader():null,this._readers.set(e,t));e.currentPos<e.targetPos&&!e.aborted;)if(t){let{done:n,value:r}=await t.read();if(n)throw this._orchestrator.forgetWorker(e),Error(`Blob reader stopped unexpectedly before all requested data was read.`);if(e.aborted)break;this.onread?.(e.currentPos,e.currentPos+r.length),this._orchestrator.supplyWorkerData(e,r)}else{let t=await this._blob.slice(e.currentPos,e.targetPos).arrayBuffer();if(e.aborted)break;this.onread?.(e.currentPos,e.currentPos+t.byteLength),this._orchestrator.supplyWorkerData(e,new Uint8Array(t))}e.running=!1}_dispose(){this._orchestrator.dispose()}};let Fr=.5*2**20;var Ir=class extends Nr{constructor(e,t={}){if(typeof e!=`string`)throw TypeError(`filePath must be a string.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.maxCacheSize!==void 0&&(!pt(t.maxCacheSize)||t.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);super(),this._fileHandle=null,this._streamSource=new Lr({getSize:async()=>(this._fileHandle=await Mr.fs.open(e,`r`),(await this._fileHandle.stat()).size),read:async(e,t)=>{x(this._fileHandle);let n=new Uint8Array(t-e);return await this._fileHandle.read(n,0,t-e,e),n},maxCacheSize:t.maxCacheSize,prefetchProfile:`fileSystem`})}_read(e,t){return this._streamSource._read(e,t)}_retrieveSize(){return this._streamSource._retrieveSize()}_dispose(){this._streamSource._dispose(),this._fileHandle?.close(),this._fileHandle=null}},Lr=class extends Nr{constructor(e){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(typeof e.getSize!=`function`)throw TypeError(`options.getSize must be a function.`);if(typeof e.read!=`function`)throw TypeError(`options.read must be a function.`);if(e.dispose!==void 0&&typeof e.dispose!=`function`)throw TypeError(`options.dispose, when provided, must be a function.`);if(e.maxCacheSize!==void 0&&(!pt(e.maxCacheSize)||e.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);if(e.prefetchProfile&&![`none`,`fileSystem`,`network`].includes(e.prefetchProfile))throw TypeError(`options.prefetchProfile, when provided, must be one of 'none', 'fileSystem' or 'network'.`);super(),this._options=e,this._orchestrator=new zr({maxCacheSize:e.maxCacheSize??8*2**20,maxWorkerCount:2,prefetchProfile:Rr[e.prefetchProfile??`none`],runWorker:this._runWorker.bind(this)})}_retrieveSize(){let e=this._options.getSize();if(e instanceof Promise)return e.then(e=>{if(!Number.isInteger(e)||e<0)throw TypeError(`options.getSize must return or resolve to a non-negative integer.`);return this._orchestrator.fileSize=e,e});if(!Number.isInteger(e)||e<0)throw TypeError(`options.getSize must return or resolve to a non-negative integer.`);return this._orchestrator.fileSize=e,e}_read(e,t){return this._orchestrator.read(e,t)}async _runWorker(e){for(;e.currentPos<e.targetPos&&!e.aborted;){let t=e.currentPos,n=e.targetPos,r=this._options.read(e.currentPos,n);if(r instanceof Promise&&(r=await r),e.aborted)break;if(r instanceof Uint8Array){if(r=T(r),r.length!==n-e.currentPos)throw Error(`options.read returned a Uint8Array with unexpected length: Requested ${n-e.currentPos} bytes, but got ${r.length}.`);this.onread?.(e.currentPos,e.currentPos+r.length),this._orchestrator.supplyWorkerData(e,r)}else if(r instanceof ReadableStream){let i=r.getReader();for(;e.currentPos<n&&!e.aborted;){let{done:r,value:a}=await i.read();if(r){if(e.currentPos<n)throw Error(`ReadableStream returned by options.read ended before supplying enough data. Requested ${n-t} bytes, but got ${e.currentPos-t}`);break}if(!(a instanceof Uint8Array))throw TypeError(`ReadableStream returned by options.read must yield Uint8Array chunks.`);if(e.aborted)break;let o=T(a);this.onread?.(e.currentPos,e.currentPos+o.length),this._orchestrator.supplyWorkerData(e,o)}}else throw TypeError(`options.read must return or resolve to a Uint8Array or a ReadableStream.`)}e.running=!1}_dispose(){this._orchestrator.dispose(),this._options.dispose?.()}};let Rr={none:(e,t)=>({start:e,end:t}),fileSystem:(e,t)=>{let n=2**16;return e=Math.floor((e-n)/n)*n,t=Math.ceil((t+n)/n)*n,{start:e,end:t}},network:(e,t,n)=>{let r=2**16;e=Math.max(0,Math.floor((e-r)/r)*r);for(let r of n){let n=8*2**20,i=Math.max((r.startPos+r.targetPos)/2,r.targetPos-n);if(ut(e,t,i,r.targetPos)){let e=r.targetPos-r.startPos,i=Math.ceil((e+1)/n)*n,a=2**Math.ceil(Math.log2(e+1)),o=Math.min(a,i);t=Math.max(t,r.startPos+o)}}return t=Math.max(t,e+Fr),{start:e,end:t}}};var zr=class{constructor(e){this.options=e,this.fileSize=null,this.nextAge=0,this.workers=[],this.cache=[],this.currentCacheSize=0,this.disposed=!1}read(e,t){x(this.fileSize!==null);let n=this.options.prefetchProfile(e,t,this.workers),r=Math.max(n.start,0),i=Math.min(n.end,this.fileSize);x(r<=e&&t<=i);let a=null,o=O(this.cache,e,e=>e.start),s=o===-1?null:this.cache[o];s&&s.start<=e&&t<=s.end&&(s.age=this.nextAge++,a={bytes:s.bytes,view:s.view,offset:s.start});let c=O(this.cache,r,e=>e.start),l=a?null:new Uint8Array(t-e),u=0,d=r,f=[];if(c!==-1){for(let n=c;n<this.cache.length;n++){let a=this.cache[n];if(a.start>=i)break;if(a.end<=r)continue;let o=Math.max(r,a.start),s=Math.min(i,a.end);if(x(o<=s),d<o&&f.push({start:d,end:o}),d=s,l){let n=Math.max(e,a.start),r=Math.min(t,a.end);if(n<r){let t=n-e;l.set(a.bytes.subarray(n-a.start,r-a.start),t),t===u&&(u=r-e)}}a.age=this.nextAge++}d<i&&f.push({start:d,end:i})}else f.push({start:r,end:i});if(l&&u>=l.length&&(a={bytes:l,view:E(l),offset:e}),f.length===0)return x(a),a;let{promise:p,resolve:m,reject:h}=k(),g=[];for(let n of f){let r=Math.max(e,n.start),i=Math.min(t,n.end);r===n.start&&i===n.end?g.push(n):r<i&&g.push({start:r,end:i})}for(let t of f){let n=l&&{start:e,bytes:l,holes:g,resolve:m,reject:h},r=!1;for(let e of this.workers)if(ut(t.start-2**17,t.start,e.currentPos,e.targetPos)){e.targetPos=Math.max(e.targetPos,t.end),r=!0,n&&!e.pendingSlices.includes(n)&&e.pendingSlices.push(n),e.running||this.runWorker(e);break}if(!r){let e=this.createWorker(t.start,t.end);n&&(e.pendingSlices=[n]),this.runWorker(e)}}return a||=(x(l),p.then(t=>({bytes:t,view:E(t),offset:e}))),a}createWorker(e,t){let n={startPos:e,currentPos:e,targetPos:t,running:!1,aborted:this.disposed,pendingSlices:[],age:this.nextAge++};for(this.workers.push(n);this.workers.length>this.options.maxWorkerCount;){let e=0,t=this.workers[0];for(let n=1;n<this.workers.length;n++){let r=this.workers[n];r.age<t.age&&(e=n,t=r)}if(t.running&&t.pendingSlices.length>0)break;t.aborted=!0,this.workers.splice(e,1)}return n}runWorker(e){x(!e.running),x(e.currentPos<e.targetPos),e.running=!0,e.age=this.nextAge++,this.options.runWorker(e).catch(t=>{if(e.running=!1,e.pendingSlices.length>0)e.pendingSlices.forEach(e=>e.reject(t)),e.pendingSlices.length=0;else throw t})}supplyWorkerData(e,t){x(!e.aborted);let n=e.currentPos,r=n+t.length;this.insertIntoCache({start:n,end:r,bytes:t,view:E(t),age:this.nextAge++}),e.currentPos+=t.length,e.targetPos=Math.max(e.targetPos,e.currentPos);for(let i=0;i<e.pendingSlices.length;i++){let a=e.pendingSlices[i],o=Math.max(n,a.start),s=Math.min(r,a.start+a.bytes.length);o<s&&a.bytes.set(t.subarray(o-n,s-n),o-a.start);for(let e=0;e<a.holes.length;e++){let t=a.holes[e];n<=t.start&&r>t.start&&(t.start=r),t.end<=t.start&&(a.holes.splice(e,1),e--)}a.holes.length===0&&(a.resolve(a.bytes),e.pendingSlices.splice(i,1),i--)}for(let t=0;t<this.workers.length;t++){let i=this.workers[t];e===i||i.running||ut(n,r,i.currentPos,i.targetPos)&&(this.workers.splice(t,1),t--)}}forgetWorker(e){let t=this.workers.indexOf(e);x(t!==-1),this.workers.splice(t,1)}insertIntoCache(e){if(this.options.maxCacheSize===0)return;let t=O(this.cache,e.start,e=>e.start)+1;if(t>0){let n=this.cache[t-1];if(n.end>=e.end)return;if(n.end>e.start){let r=new Uint8Array(e.end-n.start);r.set(n.bytes,0),r.set(e.bytes,e.start-n.start),this.currentCacheSize+=e.end-n.end,n.bytes=r,n.view=E(r),n.end=e.end,t--,e=n}else this.cache.splice(t,0,e),this.currentCacheSize+=e.bytes.length}else this.cache.splice(t,0,e),this.currentCacheSize+=e.bytes.length;for(let n=t+1;n<this.cache.length;n++){let t=this.cache[n];if(e.end<=t.start)break;if(e.end>=t.end){this.cache.splice(n,1),this.currentCacheSize-=t.bytes.length,n--;continue}let r=new Uint8Array(t.end-e.start);r.set(e.bytes,0),r.set(t.bytes,t.start-e.start),this.currentCacheSize-=e.end-t.start,e.bytes=r,e.view=E(r),e.end=t.end,this.cache.splice(n,1);break}for(;this.currentCacheSize>this.options.maxCacheSize;){let e=0,t=this.cache[0];for(let n=1;n<this.cache.length;n++){let r=this.cache[n];r.age<t.age&&(e=n,t=r)}if(this.currentCacheSize-t.bytes.length<=this.options.maxCacheSize)break;this.cache.splice(e,1),this.currentCacheSize-=t.bytes.length}}dispose(){for(let e of this.workers)e.aborted=!0;this.workers.length=0,this.cache.length=0,this.disposed=!0}};
|
|
103
|
+
let Nr=Mr===void 0?void 0:Mr;var Pr=class{constructor(){this._disposed=!1,this._sizePromise=null,this.onread=null}async getSizeOrNull(){if(this._disposed)throw new L;return this._sizePromise??=Promise.resolve(this._retrieveSize())}async getSize(){if(this._disposed)throw new L;let e=await this.getSizeOrNull();if(e===null)throw Error(`Cannot determine the size of an unsized source.`);return e}},Fr=class extends Pr{constructor(e,t={}){if(!(e instanceof Blob))throw TypeError(`blob must be a Blob.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.maxCacheSize!==void 0&&(!ht(t.maxCacheSize)||t.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);super(),this._readers=new WeakMap,this._blob=e,this._orchestrator=new Br({maxCacheSize:t.maxCacheSize??8*2**20,maxWorkerCount:4,runWorker:this._runWorker.bind(this),prefetchProfile:zr.fileSystem})}_retrieveSize(){let e=this._blob.size;return this._orchestrator.fileSize=e,e}_read(e,t){return this._orchestrator.read(e,t)}async _runWorker(e){let t=this._readers.get(e);for(t===void 0&&(t=(`stream`in this._blob)&&!at()?this._blob.slice(e.currentPos).stream().getReader():null,this._readers.set(e,t));e.currentPos<e.targetPos&&!e.aborted;)if(t){let{done:n,value:r}=await t.read();if(n)throw this._orchestrator.forgetWorker(e),Error(`Blob reader stopped unexpectedly before all requested data was read.`);if(e.aborted)break;this.onread?.(e.currentPos,e.currentPos+r.length),this._orchestrator.supplyWorkerData(e,r)}else{let t=await this._blob.slice(e.currentPos,e.targetPos).arrayBuffer();if(e.aborted)break;this.onread?.(e.currentPos,e.currentPos+t.byteLength),this._orchestrator.supplyWorkerData(e,new Uint8Array(t))}e.running=!1}_dispose(){this._orchestrator.dispose()}};let Ir=.5*2**20;var Lr=class extends Pr{constructor(e,t={}){if(typeof e!=`string`)throw TypeError(`filePath must be a string.`);if(!t||typeof t!=`object`)throw TypeError(`options must be an object.`);if(t.maxCacheSize!==void 0&&(!ht(t.maxCacheSize)||t.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);super(),this._fileHandle=null,this._streamSource=new Rr({getSize:async()=>(this._fileHandle=await Nr.fs.open(e,`r`),(await this._fileHandle.stat()).size),read:async(e,t)=>{b(this._fileHandle);let n=new Uint8Array(t-e);return await this._fileHandle.read(n,0,t-e,e),n},maxCacheSize:t.maxCacheSize,prefetchProfile:`fileSystem`})}_read(e,t){return this._streamSource._read(e,t)}_retrieveSize(){return this._streamSource._retrieveSize()}_dispose(){this._streamSource._dispose(),this._fileHandle?.close(),this._fileHandle=null}},Rr=class extends Pr{constructor(e){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(typeof e.getSize!=`function`)throw TypeError(`options.getSize must be a function.`);if(typeof e.read!=`function`)throw TypeError(`options.read must be a function.`);if(e.dispose!==void 0&&typeof e.dispose!=`function`)throw TypeError(`options.dispose, when provided, must be a function.`);if(e.maxCacheSize!==void 0&&(!ht(e.maxCacheSize)||e.maxCacheSize<0))throw TypeError(`options.maxCacheSize, when provided, must be a non-negative number.`);if(e.prefetchProfile&&![`none`,`fileSystem`,`network`].includes(e.prefetchProfile))throw TypeError(`options.prefetchProfile, when provided, must be one of 'none', 'fileSystem' or 'network'.`);super(),this._options=e,this._orchestrator=new Br({maxCacheSize:e.maxCacheSize??8*2**20,maxWorkerCount:2,prefetchProfile:zr[e.prefetchProfile??`none`],runWorker:this._runWorker.bind(this)})}_retrieveSize(){let e=this._options.getSize();if(e instanceof Promise)return e.then(e=>{if(!Number.isInteger(e)||e<0)throw TypeError(`options.getSize must return or resolve to a non-negative integer.`);return this._orchestrator.fileSize=e,e});if(!Number.isInteger(e)||e<0)throw TypeError(`options.getSize must return or resolve to a non-negative integer.`);return this._orchestrator.fileSize=e,e}_read(e,t){return this._orchestrator.read(e,t)}async _runWorker(e){for(;e.currentPos<e.targetPos&&!e.aborted;){let t=e.currentPos,n=e.targetPos,r=this._options.read(e.currentPos,n);if(r instanceof Promise&&(r=await r),e.aborted)break;if(r instanceof Uint8Array){if(r=w(r),r.length!==n-e.currentPos)throw Error(`options.read returned a Uint8Array with unexpected length: Requested ${n-e.currentPos} bytes, but got ${r.length}.`);this.onread?.(e.currentPos,e.currentPos+r.length),this._orchestrator.supplyWorkerData(e,r)}else if(r instanceof ReadableStream){let i=r.getReader();for(;e.currentPos<n&&!e.aborted;){let{done:r,value:a}=await i.read();if(r){if(e.currentPos<n)throw Error(`ReadableStream returned by options.read ended before supplying enough data. Requested ${n-t} bytes, but got ${e.currentPos-t}`);break}if(!(a instanceof Uint8Array))throw TypeError(`ReadableStream returned by options.read must yield Uint8Array chunks.`);if(e.aborted)break;let o=w(a);this.onread?.(e.currentPos,e.currentPos+o.length),this._orchestrator.supplyWorkerData(e,o)}}else throw TypeError(`options.read must return or resolve to a Uint8Array or a ReadableStream.`)}e.running=!1}_dispose(){this._orchestrator.dispose(),this._options.dispose?.()}};let zr={none:(e,t)=>({start:e,end:t}),fileSystem:(e,t)=>{let n=2**16;return e=Math.floor((e-n)/n)*n,t=Math.ceil((t+n)/n)*n,{start:e,end:t}},network:(e,t,n)=>{let r=2**16;e=Math.max(0,Math.floor((e-r)/r)*r);for(let r of n){let n=8*2**20,i=Math.max((r.startPos+r.targetPos)/2,r.targetPos-n);if(ft(e,t,i,r.targetPos)){let e=r.targetPos-r.startPos,i=Math.ceil((e+1)/n)*n,a=2**Math.ceil(Math.log2(e+1)),o=Math.min(a,i);t=Math.max(t,r.startPos+o)}}return t=Math.max(t,e+Ir),{start:e,end:t}}};var Br=class{constructor(e){this.options=e,this.fileSize=null,this.nextAge=0,this.workers=[],this.cache=[],this.currentCacheSize=0,this.disposed=!1}read(e,t){b(this.fileSize!==null);let n=this.options.prefetchProfile(e,t,this.workers),r=Math.max(n.start,0),i=Math.min(n.end,this.fileSize);b(r<=e&&t<=i);let a=null,o=D(this.cache,e,e=>e.start),s=o===-1?null:this.cache[o];s&&s.start<=e&&t<=s.end&&(s.age=this.nextAge++,a={bytes:s.bytes,view:s.view,offset:s.start});let c=D(this.cache,r,e=>e.start),l=a?null:new Uint8Array(t-e),u=0,d=r,f=[];if(c!==-1){for(let n=c;n<this.cache.length;n++){let a=this.cache[n];if(a.start>=i)break;if(a.end<=r)continue;let o=Math.max(r,a.start),s=Math.min(i,a.end);if(b(o<=s),d<o&&f.push({start:d,end:o}),d=s,l){let n=Math.max(e,a.start),r=Math.min(t,a.end);if(n<r){let t=n-e;l.set(a.bytes.subarray(n-a.start,r-a.start),t),t===u&&(u=r-e)}}a.age=this.nextAge++}d<i&&f.push({start:d,end:i})}else f.push({start:r,end:i});if(l&&u>=l.length&&(a={bytes:l,view:T(l),offset:e}),f.length===0)return b(a),a;let{promise:p,resolve:m,reject:h}=O(),g=[];for(let n of f){let r=Math.max(e,n.start),i=Math.min(t,n.end);r===n.start&&i===n.end?g.push(n):r<i&&g.push({start:r,end:i})}for(let t of f){let n=l&&{start:e,bytes:l,holes:g,resolve:m,reject:h},r=!1;for(let e of this.workers)if(ft(t.start-2**17,t.start,e.currentPos,e.targetPos)){e.targetPos=Math.max(e.targetPos,t.end),r=!0,n&&!e.pendingSlices.includes(n)&&e.pendingSlices.push(n),e.running||this.runWorker(e);break}if(!r){let e=this.createWorker(t.start,t.end);n&&(e.pendingSlices=[n]),this.runWorker(e)}}return a||=(b(l),p.then(t=>({bytes:t,view:T(t),offset:e}))),a}createWorker(e,t){let n={startPos:e,currentPos:e,targetPos:t,running:!1,aborted:this.disposed,pendingSlices:[],age:this.nextAge++};for(this.workers.push(n);this.workers.length>this.options.maxWorkerCount;){let e=0,t=this.workers[0];for(let n=1;n<this.workers.length;n++){let r=this.workers[n];r.age<t.age&&(e=n,t=r)}if(t.running&&t.pendingSlices.length>0)break;t.aborted=!0,this.workers.splice(e,1)}return n}runWorker(e){b(!e.running),b(e.currentPos<e.targetPos),e.running=!0,e.age=this.nextAge++,this.options.runWorker(e).catch(t=>{if(e.running=!1,e.pendingSlices.length>0)e.pendingSlices.forEach(e=>e.reject(t)),e.pendingSlices.length=0;else throw t})}supplyWorkerData(e,t){b(!e.aborted);let n=e.currentPos,r=n+t.length;this.insertIntoCache({start:n,end:r,bytes:t,view:T(t),age:this.nextAge++}),e.currentPos+=t.length,e.targetPos=Math.max(e.targetPos,e.currentPos);for(let i=0;i<e.pendingSlices.length;i++){let a=e.pendingSlices[i],o=Math.max(n,a.start),s=Math.min(r,a.start+a.bytes.length);o<s&&a.bytes.set(t.subarray(o-n,s-n),o-a.start);for(let e=0;e<a.holes.length;e++){let t=a.holes[e];n<=t.start&&r>t.start&&(t.start=r),t.end<=t.start&&(a.holes.splice(e,1),e--)}a.holes.length===0&&(a.resolve(a.bytes),e.pendingSlices.splice(i,1),i--)}for(let t=0;t<this.workers.length;t++){let i=this.workers[t];e===i||i.running||ft(n,r,i.currentPos,i.targetPos)&&(this.workers.splice(t,1),t--)}}forgetWorker(e){let t=this.workers.indexOf(e);b(t!==-1),this.workers.splice(t,1)}insertIntoCache(e){if(this.options.maxCacheSize===0)return;let t=D(this.cache,e.start,e=>e.start)+1;if(t>0){let n=this.cache[t-1];if(n.end>=e.end)return;if(n.end>e.start){let r=new Uint8Array(e.end-n.start);r.set(n.bytes,0),r.set(e.bytes,e.start-n.start),this.currentCacheSize+=e.end-n.end,n.bytes=r,n.view=T(r),n.end=e.end,t--,e=n}else this.cache.splice(t,0,e),this.currentCacheSize+=e.bytes.length}else this.cache.splice(t,0,e),this.currentCacheSize+=e.bytes.length;for(let n=t+1;n<this.cache.length;n++){let t=this.cache[n];if(e.end<=t.start)break;if(e.end>=t.end){this.cache.splice(n,1),this.currentCacheSize-=t.bytes.length,n--;continue}let r=new Uint8Array(t.end-e.start);r.set(e.bytes,0),r.set(t.bytes,t.start-e.start),this.currentCacheSize-=e.end-t.start,e.bytes=r,e.view=T(r),e.end=t.end,this.cache.splice(n,1);break}for(;this.currentCacheSize>this.options.maxCacheSize;){let e=0,t=this.cache[0];for(let n=1;n<this.cache.length;n++){let r=this.cache[n];r.age<t.age&&(e=n,t=r)}if(this.currentCacheSize-t.bytes.length<=this.options.maxCacheSize)break;this.cache.splice(e,1),this.currentCacheSize-=t.bytes.length}}dispose(){for(let e of this.workers)e.aborted=!0;this.workers.length=0,this.cache.length=0,this.disposed=!0}};
|
|
104
104
|
/*!
|
|
105
105
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
106
106
|
*
|
|
@@ -108,7 +108,7 @@ let Mr=jr===void 0?void 0:jr;var Nr=class{constructor(){this._disposed=!1,this._
|
|
|
108
108
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
109
109
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
110
110
|
*/
|
|
111
|
-
|
|
111
|
+
mt();var Vr=class{get disposed(){return this._disposed}constructor(e){if(this._demuxerPromise=null,this._format=null,this._disposed=!1,!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(!Array.isArray(e.formats)||e.formats.some(e=>!(e instanceof kr)))throw TypeError(`options.formats must be an array of InputFormat.`);if(!(e.source instanceof Pr))throw TypeError(`options.source must be a Source.`);if(e.source._disposed)throw Error(`options.source must not be disposed.`);this._formats=e.formats,this._source=e.source,this._reader=new Hr(e.source)}_getDemuxer(){return this._demuxerPromise??=(async()=>{this._reader.fileSize=await this._source.getSizeOrNull();for(let e of this._formats)if(await e._canReadInput(this))return this._format=e,e._createDemuxer(this);throw Error(`Input has an unsupported or unrecognizable format.`)})()}get source(){return this._source}async getFormat(){return await this._getDemuxer(),b(this._format),this._format}async computeDuration(){return(await this._getDemuxer()).computeDuration()}async getTracks(){return(await this._getDemuxer()).getTracks()}async getVideoTracks(){return(await this.getTracks()).filter(e=>e.isVideoTrack())}async getAudioTracks(){return(await this.getTracks()).filter(e=>e.isAudioTrack())}async getPrimaryVideoTrack(){return(await this.getTracks()).find(e=>e.isVideoTrack())??null}async getPrimaryAudioTrack(){return(await this.getTracks()).find(e=>e.isAudioTrack())??null}async getMimeType(){return(await this._getDemuxer()).getMimeType()}async getMetadataTags(){return(await this._getDemuxer()).getMetadataTags()}dispose(){this._disposed||(this._disposed=!0,this._source._disposed=!0,this._source._dispose())}[Symbol.dispose](){this.dispose()}},L=class extends Error{constructor(e=`Input has been disposed.`){super(e),this.name=`InputDisposedError`}},Hr=class{constructor(e){this.source=e}requestSlice(e,t){if(this.source._disposed)throw new L;if(this.fileSize!==null&&e+t>this.fileSize)return null;let n=e+t,r=this.source._read(e,n);return r instanceof Promise?r.then(t=>t?new Ur(t.bytes,t.view,t.offset,e,n):null):r?new Ur(r.bytes,r.view,r.offset,e,n):null}requestSliceRange(e,t,n){if(this.source._disposed)throw new L;if(this.fileSize!==null)return this.requestSlice(e,k(this.fileSize-e,t,n));{let r=this.requestSlice(e,n),i=r=>{if(r)return r;let i=r=>(b(r!==null),this.requestSlice(e,k(r-e,t,n))),a=this.source._retrieveSize();return a instanceof Promise?a.then(i):i(a)};return r instanceof Promise?r.then(i):i(r)}}},Ur=class e{constructor(e,t,n,r,i){this.bytes=e,this.view=t,this.offset=n,this.start=r,this.end=i,this.bufferPos=r-n}static tempFromBytes(t){return new e(t,T(t),0,0,t.length)}get length(){return this.end-this.start}get filePos(){return this.offset+this.bufferPos}set filePos(e){this.bufferPos=e-this.offset}get remainingLength(){return Math.max(this.end-this.filePos,0)}skip(e){this.bufferPos+=e}slice(t,n=this.end-t){if(t<this.start||t+n>this.end)throw RangeError(`Slicing outside of original slice.`);return new e(this.bytes,this.view,this.offset,t,t+n)}};let Wr=(e,t)=>{if(e.filePos<e.start||e.filePos+t>e.end)throw RangeError(`Tried reading [${e.filePos}, ${e.filePos+t}), but slice is [${e.start}, ${e.end}). This is likely an internal error, please report it alongside the file that caused it.`)},R=(e,t)=>{Wr(e,t);let n=e.bytes.subarray(e.bufferPos,e.bufferPos+t);return e.bufferPos+=t,n},z=e=>(Wr(e,1),e.view.getUint8(e.bufferPos++)),B=e=>{Wr(e,2);let t=e.view.getUint16(e.bufferPos,!1);return e.bufferPos+=2,t},Gr=e=>{Wr(e,3);let t=Ke(e.view,e.bufferPos,!1);return e.bufferPos+=3,t},Kr=e=>{Wr(e,2);let t=e.view.getInt16(e.bufferPos,!1);return e.bufferPos+=2,t},V=e=>{Wr(e,4);let t=e.view.getUint32(e.bufferPos,!1);return e.bufferPos+=4,t},qr=e=>{Wr(e,4);let t=e.view.getInt32(e.bufferPos,!1);return e.bufferPos+=4,t},H=e=>{let t=V(e),n=V(e);return t*4294967296+n},Jr=e=>{let t=qr(e),n=V(e);return t*4294967296+n},Yr=e=>{Wr(e,8);let t=e.view.getFloat64(e.bufferPos,!1);return e.bufferPos+=8,t},Xr=(e,t)=>{Wr(e,t);let n=``;for(let r=0;r<t;r++)n+=String.fromCharCode(e.bytes[e.bufferPos++]);return n},Zr=/<(?:(\d{2}):)?(\d{2}):(\d{2}).(\d{3})>/g,Qr=e=>{let t=Math.floor(e/(3600*1e3)),n=Math.floor(e%(3600*1e3)/(60*1e3)),r=Math.floor(e%(60*1e3)/1e3),i=e%1e3;return t.toString().padStart(2,`0`)+`:`+n.toString().padStart(2,`0`)+`:`+r.toString().padStart(2,`0`)+`.`+i.toString().padStart(3,`0`)};
|
|
112
112
|
/*!
|
|
113
113
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
114
114
|
*
|
|
@@ -116,7 +116,7 @@ ft();var Br=class{get disposed(){return this._disposed}constructor(e){if(this._d
|
|
|
116
116
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
117
117
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
118
118
|
*/
|
|
119
|
-
var Qr=class{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.helperView=new DataView(this.helper.buffer),this.offsets=new WeakMap}writeU32(e){this.helperView.setUint32(0,e,!1),this.writer.write(this.helper.subarray(0,4))}writeU64(e){this.helperView.setUint32(0,Math.floor(e/2**32),!1),this.helperView.setUint32(4,e,!1),this.writer.write(this.helper.subarray(0,8))}writeAscii(e){for(let t=0;t<e.length;t++)this.helperView.setUint8(t%8,e.charCodeAt(t)),t%8==7&&this.writer.write(this.helper);e.length%8!=0&&this.writer.write(this.helper.subarray(0,e.length%8))}writeBox(e){if(this.offsets.set(e,this.writer.getPos()),e.contents&&!e.children)this.writeBoxHeader(e,e.size??e.contents.byteLength+8),this.writer.write(e.contents);else{let t=this.writer.getPos();if(this.writeBoxHeader(e,0),e.contents&&this.writer.write(e.contents),e.children)for(let t of e.children)t&&this.writeBox(t);let n=this.writer.getPos(),r=e.size??n-t;this.writer.seek(t),this.writeBoxHeader(e,r),this.writer.seek(n)}}writeBoxHeader(e,t){this.writeU32(e.largeSize?1:t),this.writeAscii(e.type),e.largeSize&&this.writeU64(t)}measureBoxHeader(e){return 8+(e.largeSize?8:0)}patchBox(e){let t=this.offsets.get(e);x(t!==void 0);let n=this.writer.getPos();this.writer.seek(t),this.writeBox(e),this.writer.seek(n)}measureBox(e){if(e.contents&&!e.children)return this.measureBoxHeader(e)+e.contents.byteLength;{let t=this.measureBoxHeader(e);if(e.contents&&(t+=e.contents.byteLength),e.children)for(let n of e.children)n&&(t+=this.measureBox(n));return t}}};let U=new Uint8Array(8),W=new DataView(U.buffer),G=e=>[(e%256+256)%256],K=e=>(W.setUint16(0,e,!1),[U[0],U[1]]),$r=e=>(W.setInt16(0,e,!1),[U[0],U[1]]),ei=e=>(W.setUint32(0,e,!1),[U[1],U[2],U[3]]),q=e=>(W.setUint32(0,e,!1),[U[0],U[1],U[2],U[3]]),ti=e=>(W.setInt32(0,e,!1),[U[0],U[1],U[2],U[3]]),ni=e=>(W.setUint32(0,Math.floor(e/2**32),!1),W.setUint32(4,e,!1),[U[0],U[1],U[2],U[3],U[4],U[5],U[6],U[7]]),ri=e=>(W.setInt16(0,2**8*e,!1),[U[0],U[1]]),ii=e=>(W.setInt32(0,2**16*e,!1),[U[0],U[1],U[2],U[3]]),ai=e=>(W.setInt32(0,2**30*e,!1),[U[0],U[1],U[2],U[3]]),oi=(e,t)=>{let n=[],r=e;do{let e=r&127;r>>=7,n.length>0&&(e|=128),n.push(e),t!==void 0&&t--}while(r>0||t);return n.reverse()},J=(e,t=!1)=>{let n=Array(e.length).fill(null).map((t,n)=>e.charCodeAt(n));return t&&n.push(0),n},si=e=>{let t=null;for(let n of e)(!t||n.timestamp>t.timestamp)&&(t=n);return t},ci=e=>{let t=e*(Math.PI/180),n=Math.round(Math.cos(t)),r=Math.round(Math.sin(t));return[n,r,0,-r,n,0,0,0,1]},li=ci(0),ui=e=>[ii(e[0]),ii(e[1]),ai(e[2]),ii(e[3]),ii(e[4]),ai(e[5]),ii(e[6]),ii(e[7]),ai(e[8])],Y=(e,t,n)=>({type:e,contents:t&&new Uint8Array(t.flat(10)),children:n}),X=(e,t,n,r,i)=>Y(e,[G(t),ei(n),r??[]],i),di=e=>e.isQuickTime?Y(`ftyp`,[J(`qt `),q(512),J(`qt `)]):e.fragmented?Y(`ftyp`,[J(`iso5`),q(512),J(`iso5`),J(`iso6`),J(`mp41`)]):Y(`ftyp`,[J(`isom`),q(512),J(`isom`),e.holdsAvc?J(`avc1`):[],J(`mp41`)]),fi=e=>({type:`mdat`,largeSize:e}),pi=e=>({type:`free`,size:e}),mi=e=>Y(`moov`,void 0,[hi(e.creationTime,e.trackDatas),...e.trackDatas.map(t=>gi(t,e.creationTime)),e.isFragmented?Qi(e.trackDatas):null,pa(e)]),hi=(e,t)=>{let n=$(Math.max(0,...t.filter(e=>e.samples.length>0).map(e=>{let t=si(e.samples);return t.timestamp+t.duration})),Fa),r=Math.max(0,...t.map(e=>e.track.id))+1,i=!Ce(e)||!Ce(n),a=i?ni:q;return X(`mvhd`,+i,0,[a(e),a(e),q(Fa),a(n),ii(1),ri(1),Array(10).fill(0),ui(li),Array(24).fill(0),q(r)])},gi=(e,t)=>{let n=Ia(e);return Y(`trak`,void 0,[_i(e,t),vi(e,t),n.name===void 0?null:Y(`udta`,void 0,[Y(`name`,[...D.encode(n.name)])])])},_i=(e,t)=>{let n=si(e.samples),r=$(n?n.timestamp+n.duration:0,Fa),i=!Ce(t)||!Ce(r),a=i?ni:q,o;if(e.type===`video`){let t=e.track.metadata.rotation;o=ci(t??0)}else o=li;let s=2;return e.track.metadata.disposition?.default!==!1&&(s|=1),X(`tkhd`,+i,s,[a(t),a(t),q(e.track.id),q(0),a(r),Array(8).fill(0),K(0),K(e.track.id),ri(e.type===`audio`?1:0),K(0),ui(o),ii(e.type===`video`?e.info.width:0),ii(e.type===`video`?e.info.height:0)])},vi=(e,t)=>Y(`mdia`,void 0,[yi(e,t),Si(!0,bi[e.type],xi[e.type]),Ci(e)]),yi=(e,t)=>{let n=si(e.samples),r=$(n?n.timestamp+n.duration:0,e.timescale),i=!Ce(t)||!Ce(r),a=i?ni:q;return X(`mdhd`,+i,0,[a(t),a(t),q(e.timescale),a(r),K(Ea(e.track.metadata.languageCode??`und`)),K(0)])},bi={video:`vide`,audio:`soun`,subtitle:`text`},xi={video:`MediabunnyVideoHandler`,audio:`MediabunnySoundHandler`,subtitle:`MediabunnyTextHandler`},Si=(e,t,n,r=`\0\0\0\0`)=>X(`hdlr`,0,0,[e?J(`mhlr`):q(0),J(t),J(r),q(0),q(0),J(n,!0)]),Ci=e=>Y(`minf`,void 0,[wi[e.type](),Ti(),Oi(e)]),wi={video:()=>X(`vmhd`,0,1,[K(0),K(0),K(0),K(0)]),audio:()=>X(`smhd`,0,0,[K(0),K(0)]),subtitle:()=>X(`nmhd`,0,0)},Ti=()=>Y(`dinf`,void 0,[Ei()]),Ei=()=>X(`dref`,0,0,[q(1)],[Di()]),Di=()=>X(`url `,0,1),Oi=e=>{let t=e.compositionTimeOffsetTable.length>1||e.compositionTimeOffsetTable.some(e=>e.sampleCompositionTimeOffset!==0);return Y(`stbl`,void 0,[ki(e),Gi(e),t?Xi(e):null,t?Zi(e):null,qi(e),Ji(e),Yi(e),Ki(e)])},ki=e=>{let t;if(e.type===`video`)t=Ai(ba(e.track.source._codec,e.info.decoderConfig.codec),e);else if(e.type===`audio`){let n=Sa(e.track.source._codec,e.muxer.isQuickTime);x(n),t=Ii(n,e)}else e.type===`subtitle`&&(t=Ui(wa[e.track.source._codec],e));return x(t),X(`stsd`,0,0,[q(1)],[t])},Ai=(e,t)=>Y(e,[[,,,,,,].fill(0),K(1),K(0),K(0),Array(12).fill(0),K(t.info.width),K(t.info.height),q(4718592),q(4718592),q(0),K(1),Array(32).fill(0),K(24),$r(65535)],[xa[t.track.source._codec](t),Ne(t.info.decoderConfig.colorSpace)?ji(t):null]),ji=e=>Y(`colr`,[J(`nclx`),K(De[e.info.decoderConfig.colorSpace.primaries]),K(ke[e.info.decoderConfig.colorSpace.transfer]),K(je[e.info.decoderConfig.colorSpace.matrix]),G((e.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),Mi=e=>e.info.decoderConfig&&Y(`avcC`,[...T(e.info.decoderConfig.description)]),Ni=e=>e.info.decoderConfig&&Y(`hvcC`,[...T(e.info.decoderConfig.description)]),Pi=e=>{if(!e.info.decoderConfig)return null;let t=e.info.decoderConfig,n=t.codec.split(`.`),r=Number(n[1]),i=Number(n[2]),a=Number(n[3]),o=n[4]?Number(n[4]):1,s=n[8]?Number(n[8]):Number(t.colorSpace?.fullRange??0),c=(a<<4)+(o<<1)+s,l=n[5]?Number(n[5]):t.colorSpace?.primaries?De[t.colorSpace.primaries]:2,u=n[6]?Number(n[6]):t.colorSpace?.transfer?ke[t.colorSpace.transfer]:2,d=n[7]?Number(n[7]):t.colorSpace?.matrix?je[t.colorSpace.matrix]:2;return X(`vpcC`,1,0,[G(r),G(i),G(c),G(l),G(u),G(d),K(0)])},Fi=e=>Y(`av1C`,Et(e.info.decoderConfig.codec)),Ii=(e,t)=>{let n=0,r,i=16;if(M.includes(t.track.source._codec)){let e=t.track.source._codec,{sampleSize:r}=Ft(e);i=8*r,i>16&&(n=1)}return r=n===0?[[,,,,,,].fill(0),K(1),K(n),K(0),q(0),K(t.info.numberOfChannels),K(i),K(0),K(0),K(t.info.sampleRate<2**16?t.info.sampleRate:0),K(0)]:[[,,,,,,].fill(0),K(1),K(n),K(0),q(0),K(t.info.numberOfChannels),K(Math.min(i,16)),K(0),K(0),K(t.info.sampleRate<2**16?t.info.sampleRate:0),K(0),q(1),q(i/8),q(t.info.numberOfChannels*i/8),q(2)],Y(e,r,[Ca(t.track.source._codec,t.muxer.isQuickTime)?.(t)??null])},Li=e=>{let t;switch(e.track.source._codec){case`aac`:t=64;break;case`mp3`:t=107;break;case`vorbis`:t=221;break;default:throw Error(`Unhandled audio codec: ${e.track.source._codec}`)}let n=[...G(t),...G(21),...ei(0),...q(0),...q(0)];if(e.info.decoderConfig.description){let t=T(e.info.decoderConfig.description);n=[...n,...G(5),...oi(t.byteLength),...t]}return n=[...K(1),...G(0),...G(4),...oi(n.length),...n,...G(6),...G(1),...G(2)],n=[...G(3),...oi(n.length),...n],X(`esds`,0,0,n)},Ri=e=>Y(`wave`,void 0,[zi(e),Bi(e),Y(`\0\0\0\0`)]),zi=e=>Y(`frma`,[J(Sa(e.track.source._codec,e.muxer.isQuickTime))]),Bi=e=>{let{littleEndian:t}=Ft(e.track.source._codec);return Y(`enda`,[K(+t)])},Vi=e=>{let t=e.info.numberOfChannels,n=3840,r=e.info.sampleRate,i=0,a=0,o=new Uint8Array,s=e.info.decoderConfig?.description;if(s){x(s.byteLength>=18);let e=xn(T(s));t=e.outputChannelCount,n=e.preSkip,r=e.inputSampleRate,i=e.outputGain,a=e.channelMappingFamily,e.channelMappingTable&&(o=e.channelMappingTable)}return Y(`dOps`,[G(0),G(t),K(n),q(r),$r(i),G(a),...o])},Hi=e=>{let t=e.info.decoderConfig?.description;return x(t),X(`dfLa`,0,0,[...T(t).subarray(4)])},Z=e=>{let{littleEndian:t,sampleSize:n}=Ft(e.track.source._codec);return X(`pcmC`,0,0,[G(+t),G(8*n)])},Ui=(e,t)=>Y(e,[[,,,,,,].fill(0),K(1)],[Ta[t.track.source._codec](t)]),Wi=e=>Y(`vttC`,[...D.encode(e.info.config.description)]),Gi=e=>X(`stts`,0,0,[q(e.timeToSampleTable.length),e.timeToSampleTable.map(e=>[q(e.sampleCount),q(e.sampleDelta)])]),Ki=e=>{if(e.samples.every(e=>e.type===`key`))return null;let t=[...e.samples.entries()].filter(([,e])=>e.type===`key`);return X(`stss`,0,0,[q(t.length),t.map(([e])=>q(e+1))])},qi=e=>X(`stsc`,0,0,[q(e.compactlyCodedChunkTable.length),e.compactlyCodedChunkTable.map(e=>[q(e.firstChunk),q(e.samplesPerChunk),q(1)])]),Ji=e=>{if(e.type===`audio`&&e.info.requiresPcmTransformation){let{sampleSize:t}=Ft(e.track.source._codec);return X(`stsz`,0,0,[q(t*e.info.numberOfChannels),q(e.samples.reduce((t,n)=>t+$(n.duration,e.timescale),0))])}return X(`stsz`,0,0,[q(0),q(e.samples.length),e.samples.map(e=>q(e.size))])},Yi=e=>e.finalizedChunks.length>0&&S(e.finalizedChunks).offset>=2**32?X(`co64`,0,0,[q(e.finalizedChunks.length),e.finalizedChunks.map(e=>ni(e.offset))]):X(`stco`,0,0,[q(e.finalizedChunks.length),e.finalizedChunks.map(e=>q(e.offset))]),Xi=e=>X(`ctts`,1,0,[q(e.compositionTimeOffsetTable.length),e.compositionTimeOffsetTable.map(e=>[q(e.sampleCount),ti(e.sampleCompositionTimeOffset)])]),Zi=e=>{let t=1/0,n=-1/0,r=1/0,i=-1/0;x(e.compositionTimeOffsetTable.length>0),x(e.samples.length>0);for(let r=0;r<e.compositionTimeOffsetTable.length;r++){let i=e.compositionTimeOffsetTable[r];t=Math.min(t,i.sampleCompositionTimeOffset),n=Math.max(n,i.sampleCompositionTimeOffset)}for(let t=0;t<e.samples.length;t++){let n=e.samples[t];r=Math.min(r,$(n.timestamp,e.timescale)),i=Math.max(i,$(n.timestamp+n.duration,e.timescale))}let a=Math.max(-t,0);return i>=2**31?null:X(`cslg`,0,0,[ti(a),ti(t),ti(n),ti(r),ti(i)])},Qi=e=>Y(`mvex`,void 0,e.map($i)),$i=e=>X(`trex`,0,0,[q(e.track.id),q(1),q(0),q(0),q(0)]),ea=(e,t)=>Y(`moof`,void 0,[ta(e),...t.map(ra)]),ta=e=>X(`mfhd`,0,0,[q(e)]),na=e=>{let t=0,n=0,r=e.type===`delta`;return n|=+r,r?t|=1:t|=2,t<<24|n<<16|0},ra=e=>Y(`traf`,void 0,[ia(e),aa(e),oa(e)]),ia=e=>{x(e.currentChunk);let t=0;t|=8,t|=16,t|=32,t|=131072;let n=e.currentChunk.samples[1]??e.currentChunk.samples[0],r={duration:n.timescaleUnitsToNextSample,size:n.size,flags:na(n)};return X(`tfhd`,0,t,[q(e.track.id),q(r.duration),q(r.size),q(r.flags)])},aa=e=>(x(e.currentChunk),X(`tfdt`,1,0,[ni($(e.currentChunk.startTimestamp,e.timescale))])),oa=e=>{x(e.currentChunk);let t=e.currentChunk.samples.map(e=>e.timescaleUnitsToNextSample),n=e.currentChunk.samples.map(e=>e.size),r=e.currentChunk.samples.map(na),i=e.currentChunk.samples.map(t=>$(t.timestamp-t.decodeTimestamp,e.timescale)),a=new Set(t),o=new Set(n),s=new Set(r),c=new Set(i),l=s.size===2&&r[0]!==r[1],u=a.size>1,d=o.size>1,f=!l&&s.size>1,p=c.size>1||[...c].some(e=>e!==0),m=0;return m|=1,m|=4*l,m|=256*u,m|=512*d,m|=1024*f,m|=2048*p,X(`trun`,1,m,[q(e.currentChunk.samples.length),q(e.currentChunk.offset-e.currentChunk.moofOffset||0),l?q(r[0]):[],e.currentChunk.samples.map((e,a)=>[u?q(t[a]):[],d?q(n[a]):[],f?q(r[a]):[],p?ti(i[a]):[]])])},sa=e=>Y(`mfra`,void 0,[...e.map(ca),la()]),ca=(e,t)=>X(`tfra`,1,0,[q(e.track.id),q(63),q(e.finalizedChunks.length),e.finalizedChunks.map(n=>[ni($(n.samples[0].timestamp,e.timescale)),ni(n.moofOffset),q(t+1),q(1),q(1)])]),la=()=>X(`mfro`,0,0,[q(0)]),ua=()=>Y(`vtte`),da=(e,t,n,r,i)=>Y(`vttc`,void 0,[i===null?null:Y(`vsid`,[ti(i)]),n===null?null:Y(`iden`,[...D.encode(n)]),t===null?null:Y(`ctim`,[...D.encode(Zr(t))]),r===null?null:Y(`sttg`,[...D.encode(r)]),Y(`payl`,[...D.encode(e)])]),fa=e=>Y(`vtta`,[...D.encode(e)]),pa=e=>{let t=[],n=e.format._options.metadataFormat??`auto`,r=e.output._metadataTags;if(n===`mdir`||n===`auto`&&!e.isQuickTime){let e=va(r);e&&t.push(e)}else if(n===`mdta`){let e=ya(r);e&&t.push(e)}else (n===`udta`||n===`auto`&&e.isQuickTime)&&ma(t,e.output._metadataTags);return t.length===0?null:Y(`udta`,void 0,t)},ma=(e,t)=>{for(let{key:n,value:r}of dt(t))switch(n){case`title`:e.push(ha(`©nam`,r));break;case`description`:e.push(ha(`©des`,r));break;case`artist`:e.push(ha(`©ART`,r));break;case`album`:e.push(ha(`©alb`,r));break;case`albumArtist`:e.push(ha(`albr`,r));break;case`genre`:e.push(ha(`©gen`,r));break;case`date`:e.push(ha(`©day`,r.toISOString().slice(0,10)));break;case`comment`:e.push(ha(`©cmt`,r));break;case`lyrics`:e.push(ha(`©lyr`,r));break;case`raw`:break;case`discNumber`:case`discsTotal`:case`trackNumber`:case`tracksTotal`:case`images`:break;default:Ue(n)}if(t.raw)for(let n in t.raw){let r=t.raw[n];r==null||n.length!==4||e.some(e=>e.type===n)||(typeof r==`string`?e.push(ha(n,r)):r instanceof Uint8Array&&e.push(Y(n,Array.from(r))))}},ha=(e,t)=>{let n=D.encode(t);return Y(e,[K(n.length),K(Ea(`und`)),Array.from(n)])},ga={"image/jpeg":13,"image/png":14,"image/bmp":27},_a=(e,t)=>{let n=[];for(let{key:r,value:i}of dt(e))switch(r){case`title`:n.push({key:t?`title`:`©nam`,value:Q(i)});break;case`description`:n.push({key:t?`description`:`©des`,value:Q(i)});break;case`artist`:n.push({key:t?`artist`:`©ART`,value:Q(i)});break;case`album`:n.push({key:t?`album`:`©alb`,value:Q(i)});break;case`albumArtist`:n.push({key:t?`album_artist`:`aART`,value:Q(i)});break;case`comment`:n.push({key:t?`comment`:`©cmt`,value:Q(i)});break;case`genre`:n.push({key:t?`genre`:`©gen`,value:Q(i)});break;case`lyrics`:n.push({key:t?`lyrics`:`©lyr`,value:Q(i)});break;case`date`:n.push({key:t?`date`:`©day`,value:Q(i.toISOString().slice(0,10))});break;case`images`:for(let e of i)e.kind===`coverFront`&&n.push({key:`covr`,value:Y(`data`,[q(ga[e.mimeType]??0),q(0),Array.from(e.data)])});break;case`trackNumber`:if(t){let t=e.tracksTotal===void 0?i.toString():`${i}/${e.tracksTotal}`;n.push({key:`track`,value:Q(t)})}else n.push({key:`trkn`,value:Y(`data`,[q(0),q(0),K(0),K(i),K(e.tracksTotal??0),K(0)])});break;case`discNumber`:t||n.push({key:`disc`,value:Y(`data`,[q(0),q(0),K(0),K(i),K(e.discsTotal??0),K(0)])});break;case`tracksTotal`:case`discsTotal`:break;case`raw`:break;default:Ue(r)}if(e.raw)for(let r in e.raw){let i=e.raw[r];i==null||!t&&r.length!==4||n.some(e=>e.key===r)||(typeof i==`string`?n.push({key:r,value:Q(i)}):i instanceof Uint8Array?n.push({key:r,value:Y(`data`,[q(0),q(0),Array.from(i)])}):i instanceof mt&&n.push({key:r,value:Y(`data`,[q(ga[i.mimeType]??0),q(0),Array.from(i.data)])}))}return n},va=e=>{let t=_a(e,!1);return t.length===0?null:X(`meta`,0,0,void 0,[Si(!1,`mdir`,``,`appl`),Y(`ilst`,void 0,t.map(e=>Y(e.key,void 0,[e.value])))])},ya=e=>{let t=_a(e,!0);return t.length===0?null:Y(`meta`,void 0,[Si(!1,`mdta`,``),X(`keys`,0,0,[q(t.length)],t.map(e=>Y(`mdta`,[...D.encode(e.key)]))),Y(`ilst`,void 0,t.map((e,t)=>Y(String.fromCharCode(...q(t+1)),void 0,[e.value])))])},Q=e=>Y(`data`,[q(1),q(0),...D.encode(e)]),ba=(e,t)=>{switch(e){case`avc`:return t.startsWith(`avc3`)?`avc3`:`avc1`;case`hevc`:return`hvc1`;case`vp8`:return`vp08`;case`vp9`:return`vp09`;case`av1`:return`av01`}},xa={avc:Mi,hevc:Ni,vp8:Pi,vp9:Pi,av1:Fi},Sa=(e,t)=>{switch(e){case`aac`:return`mp4a`;case`mp3`:return`mp4a`;case`opus`:return`Opus`;case`vorbis`:return`mp4a`;case`flac`:return`fLaC`;case`ulaw`:return`ulaw`;case`alaw`:return`alaw`;case`pcm-u8`:return`raw `;case`pcm-s8`:return`sowt`}if(t)switch(e){case`pcm-s16`:return`sowt`;case`pcm-s16be`:return`twos`;case`pcm-s24`:return`in24`;case`pcm-s24be`:return`in24`;case`pcm-s32`:return`in32`;case`pcm-s32be`:return`in32`;case`pcm-f32`:return`fl32`;case`pcm-f32be`:return`fl32`;case`pcm-f64`:return`fl64`;case`pcm-f64be`:return`fl64`}else switch(e){case`pcm-s16`:return`ipcm`;case`pcm-s16be`:return`ipcm`;case`pcm-s24`:return`ipcm`;case`pcm-s24be`:return`ipcm`;case`pcm-s32`:return`ipcm`;case`pcm-s32be`:return`ipcm`;case`pcm-f32`:return`fpcm`;case`pcm-f32be`:return`fpcm`;case`pcm-f64`:return`fpcm`;case`pcm-f64be`:return`fpcm`}},Ca=(e,t)=>{switch(e){case`aac`:return Li;case`mp3`:return Li;case`opus`:return Vi;case`vorbis`:return Li;case`flac`:return Hi}if(t)switch(e){case`pcm-s24`:return Ri;case`pcm-s24be`:return Ri;case`pcm-s32`:return Ri;case`pcm-s32be`:return Ri;case`pcm-f32`:return Ri;case`pcm-f32be`:return Ri;case`pcm-f64`:return Ri;case`pcm-f64be`:return Ri}else switch(e){case`pcm-s16`:return Z;case`pcm-s16be`:return Z;case`pcm-s24`:return Z;case`pcm-s24be`:return Z;case`pcm-s32`:return Z;case`pcm-s32be`:return Z;case`pcm-f32`:return Z;case`pcm-f32be`:return Z;case`pcm-f64`:return Z;case`pcm-f64be`:return Z}return null},wa={webvtt:`wvtt`},Ta={webvtt:Wi},Ea=e=>{x(e.length===3);let t=0;for(let n=0;n<3;n++)t<<=5,t+=e.charCodeAt(n)-96;return t};var Da=class{constructor(){this.ensureMonotonicity=!1,this.trackedWrites=null,this.trackedStart=-1,this.trackedEnd=-1}start(){}maybeTrackWrites(e){if(!this.trackedWrites)return;let t=this.getPos();if(t<this.trackedStart){if(t+e.byteLength<=this.trackedStart)return;e=e.subarray(this.trackedStart-t),t=0}let n=t+e.byteLength-this.trackedStart,r=this.trackedWrites.byteLength;for(;r<n;)r*=2;if(r!==this.trackedWrites.byteLength){let e=new Uint8Array(r);e.set(this.trackedWrites,0),this.trackedWrites=e}this.trackedWrites.set(e,t-this.trackedStart),this.trackedEnd=Math.max(this.trackedEnd,t+e.byteLength)}startTrackingWrites(){this.trackedWrites=new Uint8Array(2**10),this.trackedStart=this.getPos(),this.trackedEnd=this.trackedStart}stopTrackingWrites(){if(!this.trackedWrites)throw Error(`Internal error: Can't get tracked writes since nothing was tracked.`);let e={data:this.trackedWrites.subarray(0,this.trackedEnd-this.trackedStart),start:this.trackedStart,end:this.trackedEnd};return this.trackedWrites=null,e}};let Oa=2**16,ka=2**32;var Aa=class extends Da{constructor(e){if(super(),this.pos=0,this.maxPos=0,this.target=e,this.supportsResize=`resize`in new ArrayBuffer(0),this.supportsResize)try{this.buffer=new ArrayBuffer(Oa,{maxByteLength:ka})}catch{this.buffer=new ArrayBuffer(Oa),this.supportsResize=!1}else this.buffer=new ArrayBuffer(Oa);this.bytes=new Uint8Array(this.buffer)}ensureSize(e){let t=this.buffer.byteLength;for(;t<e;)t*=2;if(t!==this.buffer.byteLength){if(t>ka)throw Error(`ArrayBuffer exceeded maximum size of ${ka} bytes. Please consider using another target.`);if(this.supportsResize)this.buffer.resize(t);else{let e=new ArrayBuffer(t),n=new Uint8Array(e);n.set(this.bytes,0),this.buffer=e,this.bytes=n}}}write(e){this.maybeTrackWrites(e),this.ensureSize(this.pos+e.byteLength),this.bytes.set(e,this.pos),this.target.onwrite?.(this.pos,this.pos+e.byteLength),this.pos+=e.byteLength,this.maxPos=Math.max(this.maxPos,this.pos)}seek(e){this.pos=e}getPos(){return this.pos}async flush(){}async finalize(){this.ensureSize(this.pos),this.target.buffer=this.buffer.slice(0,Math.max(this.maxPos,this.pos))}async close(){}getSlice(e,t){return this.bytes.slice(e,t)}},ja=class extends Da{constructor(e){super(),this.target=e,this.pos=0}write(e){this.maybeTrackWrites(e),this.target.onwrite?.(this.pos,this.pos+e.byteLength),this.pos+=e.byteLength}getPos(){return this.pos}seek(e){this.pos=e}async flush(){}async finalize(){}async close(){}},Ma=class{constructor(){this._output=null,this.onwrite=null}},Na=class extends Ma{constructor(){super(...arguments),this.buffer=null}_createWriter(){return new Aa(this)}},Pa=class extends Ma{_createWriter(){return new ja(this)}};
|
|
119
|
+
var $r=class{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.helperView=new DataView(this.helper.buffer),this.offsets=new WeakMap}writeU32(e){this.helperView.setUint32(0,e,!1),this.writer.write(this.helper.subarray(0,4))}writeU64(e){this.helperView.setUint32(0,Math.floor(e/2**32),!1),this.helperView.setUint32(4,e,!1),this.writer.write(this.helper.subarray(0,8))}writeAscii(e){for(let t=0;t<e.length;t++)this.helperView.setUint8(t%8,e.charCodeAt(t)),t%8==7&&this.writer.write(this.helper);e.length%8!=0&&this.writer.write(this.helper.subarray(0,e.length%8))}writeBox(e){if(this.offsets.set(e,this.writer.getPos()),e.contents&&!e.children)this.writeBoxHeader(e,e.size??e.contents.byteLength+8),this.writer.write(e.contents);else{let t=this.writer.getPos();if(this.writeBoxHeader(e,0),e.contents&&this.writer.write(e.contents),e.children)for(let t of e.children)t&&this.writeBox(t);let n=this.writer.getPos(),r=e.size??n-t;this.writer.seek(t),this.writeBoxHeader(e,r),this.writer.seek(n)}}writeBoxHeader(e,t){this.writeU32(e.largeSize?1:t),this.writeAscii(e.type),e.largeSize&&this.writeU64(t)}measureBoxHeader(e){return 8+(e.largeSize?8:0)}patchBox(e){let t=this.offsets.get(e);b(t!==void 0);let n=this.writer.getPos();this.writer.seek(t),this.writeBox(e),this.writer.seek(n)}measureBox(e){if(e.contents&&!e.children)return this.measureBoxHeader(e)+e.contents.byteLength;{let t=this.measureBoxHeader(e);if(e.contents&&(t+=e.contents.byteLength),e.children)for(let n of e.children)n&&(t+=this.measureBox(n));return t}}};let U=new Uint8Array(8),W=new DataView(U.buffer),G=e=>[(e%256+256)%256],K=e=>(W.setUint16(0,e,!1),[U[0],U[1]]),ei=e=>(W.setInt16(0,e,!1),[U[0],U[1]]),ti=e=>(W.setUint32(0,e,!1),[U[1],U[2],U[3]]),q=e=>(W.setUint32(0,e,!1),[U[0],U[1],U[2],U[3]]),ni=e=>(W.setInt32(0,e,!1),[U[0],U[1],U[2],U[3]]),ri=e=>(W.setUint32(0,Math.floor(e/2**32),!1),W.setUint32(4,e,!1),[U[0],U[1],U[2],U[3],U[4],U[5],U[6],U[7]]),ii=e=>(W.setInt16(0,2**8*e,!1),[U[0],U[1]]),ai=e=>(W.setInt32(0,2**16*e,!1),[U[0],U[1],U[2],U[3]]),oi=e=>(W.setInt32(0,2**30*e,!1),[U[0],U[1],U[2],U[3]]),si=(e,t)=>{let n=[],r=e;do{let e=r&127;r>>=7,n.length>0&&(e|=128),n.push(e),t!==void 0&&t--}while(r>0||t);return n.reverse()},J=(e,t=!1)=>{let n=Array(e.length).fill(null).map((t,n)=>e.charCodeAt(n));return t&&n.push(0),n},ci=e=>{let t=null;for(let n of e)(!t||n.timestamp>t.timestamp)&&(t=n);return t},li=e=>{let t=e*(Math.PI/180),n=Math.round(Math.cos(t)),r=Math.round(Math.sin(t));return[n,r,0,-r,n,0,0,0,1]},ui=li(0),di=e=>[ai(e[0]),ai(e[1]),oi(e[2]),ai(e[3]),ai(e[4]),oi(e[5]),ai(e[6]),ai(e[7]),oi(e[8])],Y=(e,t,n)=>({type:e,contents:t&&new Uint8Array(t.flat(10)),children:n}),X=(e,t,n,r,i)=>Y(e,[G(t),ti(n),r??[]],i),fi=e=>e.isQuickTime?Y(`ftyp`,[J(`qt `),q(512),J(`qt `)]):e.fragmented?Y(`ftyp`,[J(`iso5`),q(512),J(`iso5`),J(`iso6`),J(`mp41`)]):Y(`ftyp`,[J(`isom`),q(512),J(`isom`),e.holdsAvc?J(`avc1`):[],J(`mp41`)]),pi=e=>({type:`mdat`,largeSize:e}),mi=e=>({type:`free`,size:e}),hi=e=>Y(`moov`,void 0,[gi(e.creationTime,e.trackDatas),...e.trackDatas.map(t=>_i(t,e.creationTime)),e.isFragmented?ea(e.trackDatas):null,ha(e)]),gi=(e,t)=>{let n=$(Math.max(0,...t.filter(e=>e.samples.length>0).map(e=>{let t=ci(e.samples);return t.timestamp+t.duration})),Ia),r=Math.max(0,...t.map(e=>e.track.id))+1,i=!Te(e)||!Te(n),a=i?ri:q;return X(`mvhd`,+i,0,[a(e),a(e),q(Ia),a(n),ai(1),ii(1),Array(10).fill(0),di(ui),Array(24).fill(0),q(r)])},_i=(e,t)=>{let n=La(e);return Y(`trak`,void 0,[vi(e,t),yi(e,t),n.name===void 0?null:Y(`udta`,void 0,[Y(`name`,[...E.encode(n.name)])])])},vi=(e,t)=>{let n=ci(e.samples),r=$(n?n.timestamp+n.duration:0,Ia),i=!Te(t)||!Te(r),a=i?ri:q,o;if(e.type===`video`){let t=e.track.metadata.rotation;o=li(t??0)}else o=ui;let s=2;return e.track.metadata.disposition?.default!==!1&&(s|=1),X(`tkhd`,+i,s,[a(t),a(t),q(e.track.id),q(0),a(r),Array(8).fill(0),K(0),K(e.track.id),ii(e.type===`audio`?1:0),K(0),di(o),ai(e.type===`video`?e.info.width:0),ai(e.type===`video`?e.info.height:0)])},yi=(e,t)=>Y(`mdia`,void 0,[bi(e,t),Ci(!0,xi[e.type],Si[e.type]),wi(e)]),bi=(e,t)=>{let n=ci(e.samples),r=$(n?n.timestamp+n.duration:0,e.timescale),i=!Te(t)||!Te(r),a=i?ri:q;return X(`mdhd`,+i,0,[a(t),a(t),q(e.timescale),a(r),K(Da(e.track.metadata.languageCode??`und`)),K(0)])},xi={video:`vide`,audio:`soun`,subtitle:`text`},Si={video:`MediabunnyVideoHandler`,audio:`MediabunnySoundHandler`,subtitle:`MediabunnyTextHandler`},Ci=(e,t,n,r=`\0\0\0\0`)=>X(`hdlr`,0,0,[e?J(`mhlr`):q(0),J(t),J(r),q(0),q(0),J(n,!0)]),wi=e=>Y(`minf`,void 0,[Ti[e.type](),Ei(),ki(e)]),Ti={video:()=>X(`vmhd`,0,1,[K(0),K(0),K(0),K(0)]),audio:()=>X(`smhd`,0,0,[K(0),K(0)]),subtitle:()=>X(`nmhd`,0,0)},Ei=()=>Y(`dinf`,void 0,[Di()]),Di=()=>X(`dref`,0,0,[q(1)],[Oi()]),Oi=()=>X(`url `,0,1),ki=e=>{let t=e.compositionTimeOffsetTable.length>1||e.compositionTimeOffsetTable.some(e=>e.sampleCompositionTimeOffset!==0);return Y(`stbl`,void 0,[Ai(e),qi(e),t?Qi(e):null,t?$i(e):null,Yi(e),Xi(e),Zi(e),Ji(e)])},Ai=e=>{let t;if(e.type===`video`)t=ji(xa(e.track.source._codec,e.info.decoderConfig.codec),e);else if(e.type===`audio`){let n=Ca(e.track.source._codec,e.muxer.isQuickTime);b(n),t=Li(n,e)}else e.type===`subtitle`&&(t=Gi(Ta[e.track.source._codec],e));return b(t),X(`stsd`,0,0,[q(1)],[t])},ji=(e,t)=>Y(e,[[,,,,,,].fill(0),K(1),K(0),K(0),Array(12).fill(0),K(t.info.width),K(t.info.height),q(4718592),q(4718592),q(0),K(1),Array(32).fill(0),K(24),ei(65535)],[Sa[t.track.source._codec](t),Fe(t.info.decoderConfig.colorSpace)?Mi(t):null]),Mi=e=>Y(`colr`,[J(`nclx`),K(ke[e.info.decoderConfig.colorSpace.primaries]),K(je[e.info.decoderConfig.colorSpace.transfer]),K(Ne[e.info.decoderConfig.colorSpace.matrix]),G((e.info.decoderConfig.colorSpace.fullRange?1:0)<<7)]),Ni=e=>e.info.decoderConfig&&Y(`avcC`,[...w(e.info.decoderConfig.description)]),Pi=e=>e.info.decoderConfig&&Y(`hvcC`,[...w(e.info.decoderConfig.description)]),Fi=e=>{if(!e.info.decoderConfig)return null;let t=e.info.decoderConfig,n=t.codec.split(`.`),r=Number(n[1]),i=Number(n[2]),a=Number(n[3]),o=n[4]?Number(n[4]):1,s=n[8]?Number(n[8]):Number(t.colorSpace?.fullRange??0),c=(a<<4)+(o<<1)+s,l=n[5]?Number(n[5]):t.colorSpace?.primaries?ke[t.colorSpace.primaries]:2,u=n[6]?Number(n[6]):t.colorSpace?.transfer?je[t.colorSpace.transfer]:2,d=n[7]?Number(n[7]):t.colorSpace?.matrix?Ne[t.colorSpace.matrix]:2;return X(`vpcC`,1,0,[G(r),G(i),G(c),G(l),G(u),G(d),K(0)])},Ii=e=>Y(`av1C`,Ot(e.info.decoderConfig.codec)),Li=(e,t)=>{let n=0,r,i=16;if(j.includes(t.track.source._codec)){let e=t.track.source._codec,{sampleSize:r}=Lt(e);i=8*r,i>16&&(n=1)}return r=n===0?[[,,,,,,].fill(0),K(1),K(n),K(0),q(0),K(t.info.numberOfChannels),K(i),K(0),K(0),K(t.info.sampleRate<2**16?t.info.sampleRate:0),K(0)]:[[,,,,,,].fill(0),K(1),K(n),K(0),q(0),K(t.info.numberOfChannels),K(Math.min(i,16)),K(0),K(0),K(t.info.sampleRate<2**16?t.info.sampleRate:0),K(0),q(1),q(i/8),q(t.info.numberOfChannels*i/8),q(2)],Y(e,r,[wa(t.track.source._codec,t.muxer.isQuickTime)?.(t)??null])},Ri=e=>{let t;switch(e.track.source._codec){case`aac`:t=64;break;case`mp3`:t=107;break;case`vorbis`:t=221;break;default:throw Error(`Unhandled audio codec: ${e.track.source._codec}`)}let n=[...G(t),...G(21),...ti(0),...q(0),...q(0)];if(e.info.decoderConfig.description){let t=w(e.info.decoderConfig.description);n=[...n,...G(5),...si(t.byteLength),...t]}return n=[...K(1),...G(0),...G(4),...si(n.length),...n,...G(6),...G(1),...G(2)],n=[...G(3),...si(n.length),...n],X(`esds`,0,0,n)},zi=e=>Y(`wave`,void 0,[Bi(e),Vi(e),Y(`\0\0\0\0`)]),Bi=e=>Y(`frma`,[J(Ca(e.track.source._codec,e.muxer.isQuickTime))]),Vi=e=>{let{littleEndian:t}=Lt(e.track.source._codec);return Y(`enda`,[K(+t)])},Hi=e=>{let t=e.info.numberOfChannels,n=3840,r=e.info.sampleRate,i=0,a=0,o=new Uint8Array,s=e.info.decoderConfig?.description;if(s){b(s.byteLength>=18);let e=Cn(w(s));t=e.outputChannelCount,n=e.preSkip,r=e.inputSampleRate,i=e.outputGain,a=e.channelMappingFamily,e.channelMappingTable&&(o=e.channelMappingTable)}return Y(`dOps`,[G(0),G(t),K(n),q(r),ei(i),G(a),...o])},Ui=e=>{let t=e.info.decoderConfig?.description;return b(t),X(`dfLa`,0,0,[...w(t).subarray(4)])},Wi=e=>{let{littleEndian:t,sampleSize:n}=Lt(e.track.source._codec);return X(`pcmC`,0,0,[G(+t),G(8*n)])},Gi=(e,t)=>Y(e,[[,,,,,,].fill(0),K(1)],[Ea[t.track.source._codec](t)]),Ki=e=>Y(`vttC`,[...E.encode(e.info.config.description)]),qi=e=>X(`stts`,0,0,[q(e.timeToSampleTable.length),e.timeToSampleTable.map(e=>[q(e.sampleCount),q(e.sampleDelta)])]),Ji=e=>{if(e.samples.every(e=>e.type===`key`))return null;let t=[...e.samples.entries()].filter(([,e])=>e.type===`key`);return X(`stss`,0,0,[q(t.length),t.map(([e])=>q(e+1))])},Yi=e=>X(`stsc`,0,0,[q(e.compactlyCodedChunkTable.length),e.compactlyCodedChunkTable.map(e=>[q(e.firstChunk),q(e.samplesPerChunk),q(1)])]),Xi=e=>{if(e.type===`audio`&&e.info.requiresPcmTransformation){let{sampleSize:t}=Lt(e.track.source._codec);return X(`stsz`,0,0,[q(t*e.info.numberOfChannels),q(e.samples.reduce((t,n)=>t+$(n.duration,e.timescale),0))])}return X(`stsz`,0,0,[q(0),q(e.samples.length),e.samples.map(e=>q(e.size))])},Zi=e=>e.finalizedChunks.length>0&&x(e.finalizedChunks).offset>=2**32?X(`co64`,0,0,[q(e.finalizedChunks.length),e.finalizedChunks.map(e=>ri(e.offset))]):X(`stco`,0,0,[q(e.finalizedChunks.length),e.finalizedChunks.map(e=>q(e.offset))]),Qi=e=>X(`ctts`,1,0,[q(e.compositionTimeOffsetTable.length),e.compositionTimeOffsetTable.map(e=>[q(e.sampleCount),ni(e.sampleCompositionTimeOffset)])]),$i=e=>{let t=1/0,n=-1/0,r=1/0,i=-1/0;b(e.compositionTimeOffsetTable.length>0),b(e.samples.length>0);for(let r=0;r<e.compositionTimeOffsetTable.length;r++){let i=e.compositionTimeOffsetTable[r];t=Math.min(t,i.sampleCompositionTimeOffset),n=Math.max(n,i.sampleCompositionTimeOffset)}for(let t=0;t<e.samples.length;t++){let n=e.samples[t];r=Math.min(r,$(n.timestamp,e.timescale)),i=Math.max(i,$(n.timestamp+n.duration,e.timescale))}let a=Math.max(-t,0);return i>=2**31?null:X(`cslg`,0,0,[ni(a),ni(t),ni(n),ni(r),ni(i)])},ea=e=>Y(`mvex`,void 0,e.map(ta)),ta=e=>X(`trex`,0,0,[q(e.track.id),q(1),q(0),q(0),q(0)]),na=(e,t)=>Y(`moof`,void 0,[ra(e),...t.map(aa)]),ra=e=>X(`mfhd`,0,0,[q(e)]),ia=e=>{let t=0,n=0,r=e.type===`delta`;return n|=+r,r?t|=1:t|=2,t<<24|n<<16|0},aa=e=>Y(`traf`,void 0,[oa(e),sa(e),ca(e)]),oa=e=>{b(e.currentChunk);let t=0;t|=8,t|=16,t|=32,t|=131072;let n=e.currentChunk.samples[1]??e.currentChunk.samples[0],r={duration:n.timescaleUnitsToNextSample,size:n.size,flags:ia(n)};return X(`tfhd`,0,t,[q(e.track.id),q(r.duration),q(r.size),q(r.flags)])},sa=e=>(b(e.currentChunk),X(`tfdt`,1,0,[ri($(e.currentChunk.startTimestamp,e.timescale))])),ca=e=>{b(e.currentChunk);let t=e.currentChunk.samples.map(e=>e.timescaleUnitsToNextSample),n=e.currentChunk.samples.map(e=>e.size),r=e.currentChunk.samples.map(ia),i=e.currentChunk.samples.map(t=>$(t.timestamp-t.decodeTimestamp,e.timescale)),a=new Set(t),o=new Set(n),s=new Set(r),c=new Set(i),l=s.size===2&&r[0]!==r[1],u=a.size>1,d=o.size>1,f=!l&&s.size>1,p=c.size>1||[...c].some(e=>e!==0),m=0;return m|=1,m|=4*l,m|=256*u,m|=512*d,m|=1024*f,m|=2048*p,X(`trun`,1,m,[q(e.currentChunk.samples.length),q(e.currentChunk.offset-e.currentChunk.moofOffset||0),l?q(r[0]):[],e.currentChunk.samples.map((e,a)=>[u?q(t[a]):[],d?q(n[a]):[],f?q(r[a]):[],p?ni(i[a]):[]])])},la=e=>Y(`mfra`,void 0,[...e.map(ua),da()]),ua=(e,t)=>X(`tfra`,1,0,[q(e.track.id),q(63),q(e.finalizedChunks.length),e.finalizedChunks.map(n=>[ri($(n.samples[0].timestamp,e.timescale)),ri(n.moofOffset),q(t+1),q(1),q(1)])]),da=()=>X(`mfro`,0,0,[q(0)]),fa=()=>Y(`vtte`),pa=(e,t,n,r,i)=>Y(`vttc`,void 0,[i===null?null:Y(`vsid`,[ni(i)]),n===null?null:Y(`iden`,[...E.encode(n)]),t===null?null:Y(`ctim`,[...E.encode(Qr(t))]),r===null?null:Y(`sttg`,[...E.encode(r)]),Y(`payl`,[...E.encode(e)])]),ma=e=>Y(`vtta`,[...E.encode(e)]),ha=e=>{let t=[],n=e.format._options.metadataFormat??`auto`,r=e.output._metadataTags;if(n===`mdir`||n===`auto`&&!e.isQuickTime){let e=ya(r);e&&t.push(e)}else if(n===`mdta`){let e=ba(r);e&&t.push(e)}else (n===`udta`||n===`auto`&&e.isQuickTime)&&ga(t,e.output._metadataTags);return t.length===0?null:Y(`udta`,void 0,t)},ga=(e,t)=>{for(let{key:n,value:r}of pt(t))switch(n){case`title`:e.push(Z(`©nam`,r));break;case`description`:e.push(Z(`©des`,r));break;case`artist`:e.push(Z(`©ART`,r));break;case`album`:e.push(Z(`©alb`,r));break;case`albumArtist`:e.push(Z(`albr`,r));break;case`genre`:e.push(Z(`©gen`,r));break;case`date`:e.push(Z(`©day`,r.toISOString().slice(0,10)));break;case`comment`:e.push(Z(`©cmt`,r));break;case`lyrics`:e.push(Z(`©lyr`,r));break;case`raw`:break;case`discNumber`:case`discsTotal`:case`trackNumber`:case`tracksTotal`:case`images`:break;default:Ge(n)}if(t.raw)for(let n in t.raw){let r=t.raw[n];r==null||n.length!==4||e.some(e=>e.type===n)||(typeof r==`string`?e.push(Z(n,r)):r instanceof Uint8Array&&e.push(Y(n,Array.from(r))))}},Z=(e,t)=>{let n=E.encode(t);return Y(e,[K(n.length),K(Da(`und`)),Array.from(n)])},_a={"image/jpeg":13,"image/png":14,"image/bmp":27},va=(e,t)=>{let n=[];for(let{key:r,value:i}of pt(e))switch(r){case`title`:n.push({key:t?`title`:`©nam`,value:Q(i)});break;case`description`:n.push({key:t?`description`:`©des`,value:Q(i)});break;case`artist`:n.push({key:t?`artist`:`©ART`,value:Q(i)});break;case`album`:n.push({key:t?`album`:`©alb`,value:Q(i)});break;case`albumArtist`:n.push({key:t?`album_artist`:`aART`,value:Q(i)});break;case`comment`:n.push({key:t?`comment`:`©cmt`,value:Q(i)});break;case`genre`:n.push({key:t?`genre`:`©gen`,value:Q(i)});break;case`lyrics`:n.push({key:t?`lyrics`:`©lyr`,value:Q(i)});break;case`date`:n.push({key:t?`date`:`©day`,value:Q(i.toISOString().slice(0,10))});break;case`images`:for(let e of i)e.kind===`coverFront`&&n.push({key:`covr`,value:Y(`data`,[q(_a[e.mimeType]??0),q(0),Array.from(e.data)])});break;case`trackNumber`:if(t){let t=e.tracksTotal===void 0?i.toString():`${i}/${e.tracksTotal}`;n.push({key:`track`,value:Q(t)})}else n.push({key:`trkn`,value:Y(`data`,[q(0),q(0),K(0),K(i),K(e.tracksTotal??0),K(0)])});break;case`discNumber`:t||n.push({key:`disc`,value:Y(`data`,[q(0),q(0),K(0),K(i),K(e.discsTotal??0),K(0)])});break;case`tracksTotal`:case`discsTotal`:break;case`raw`:break;default:Ge(r)}if(e.raw)for(let r in e.raw){let i=e.raw[r];i==null||!t&&r.length!==4||n.some(e=>e.key===r)||(typeof i==`string`?n.push({key:r,value:Q(i)}):i instanceof Uint8Array?n.push({key:r,value:Y(`data`,[q(0),q(0),Array.from(i)])}):i instanceof gt&&n.push({key:r,value:Y(`data`,[q(_a[i.mimeType]??0),q(0),Array.from(i.data)])}))}return n},ya=e=>{let t=va(e,!1);return t.length===0?null:X(`meta`,0,0,void 0,[Ci(!1,`mdir`,``,`appl`),Y(`ilst`,void 0,t.map(e=>Y(e.key,void 0,[e.value])))])},ba=e=>{let t=va(e,!0);return t.length===0?null:Y(`meta`,void 0,[Ci(!1,`mdta`,``),X(`keys`,0,0,[q(t.length)],t.map(e=>Y(`mdta`,[...E.encode(e.key)]))),Y(`ilst`,void 0,t.map((e,t)=>Y(String.fromCharCode(...q(t+1)),void 0,[e.value])))])},Q=e=>Y(`data`,[q(1),q(0),...E.encode(e)]),xa=(e,t)=>{switch(e){case`avc`:return t.startsWith(`avc3`)?`avc3`:`avc1`;case`hevc`:return`hvc1`;case`vp8`:return`vp08`;case`vp9`:return`vp09`;case`av1`:return`av01`}},Sa={avc:Ni,hevc:Pi,vp8:Fi,vp9:Fi,av1:Ii},Ca=(e,t)=>{switch(e){case`aac`:return`mp4a`;case`mp3`:return`mp4a`;case`opus`:return`Opus`;case`vorbis`:return`mp4a`;case`flac`:return`fLaC`;case`ulaw`:return`ulaw`;case`alaw`:return`alaw`;case`pcm-u8`:return`raw `;case`pcm-s8`:return`sowt`}if(t)switch(e){case`pcm-s16`:return`sowt`;case`pcm-s16be`:return`twos`;case`pcm-s24`:return`in24`;case`pcm-s24be`:return`in24`;case`pcm-s32`:return`in32`;case`pcm-s32be`:return`in32`;case`pcm-f32`:return`fl32`;case`pcm-f32be`:return`fl32`;case`pcm-f64`:return`fl64`;case`pcm-f64be`:return`fl64`}else switch(e){case`pcm-s16`:return`ipcm`;case`pcm-s16be`:return`ipcm`;case`pcm-s24`:return`ipcm`;case`pcm-s24be`:return`ipcm`;case`pcm-s32`:return`ipcm`;case`pcm-s32be`:return`ipcm`;case`pcm-f32`:return`fpcm`;case`pcm-f32be`:return`fpcm`;case`pcm-f64`:return`fpcm`;case`pcm-f64be`:return`fpcm`}},wa=(e,t)=>{switch(e){case`aac`:return Ri;case`mp3`:return Ri;case`opus`:return Hi;case`vorbis`:return Ri;case`flac`:return Ui}if(t)switch(e){case`pcm-s24`:return zi;case`pcm-s24be`:return zi;case`pcm-s32`:return zi;case`pcm-s32be`:return zi;case`pcm-f32`:return zi;case`pcm-f32be`:return zi;case`pcm-f64`:return zi;case`pcm-f64be`:return zi}else switch(e){case`pcm-s16`:return Wi;case`pcm-s16be`:return Wi;case`pcm-s24`:return Wi;case`pcm-s24be`:return Wi;case`pcm-s32`:return Wi;case`pcm-s32be`:return Wi;case`pcm-f32`:return Wi;case`pcm-f32be`:return Wi;case`pcm-f64`:return Wi;case`pcm-f64be`:return Wi}return null},Ta={webvtt:`wvtt`},Ea={webvtt:Ki},Da=e=>{b(e.length===3);let t=0;for(let n=0;n<3;n++)t<<=5,t+=e.charCodeAt(n)-96;return t};var Oa=class{constructor(){this.ensureMonotonicity=!1,this.trackedWrites=null,this.trackedStart=-1,this.trackedEnd=-1}start(){}maybeTrackWrites(e){if(!this.trackedWrites)return;let t=this.getPos();if(t<this.trackedStart){if(t+e.byteLength<=this.trackedStart)return;e=e.subarray(this.trackedStart-t),t=0}let n=t+e.byteLength-this.trackedStart,r=this.trackedWrites.byteLength;for(;r<n;)r*=2;if(r!==this.trackedWrites.byteLength){let e=new Uint8Array(r);e.set(this.trackedWrites,0),this.trackedWrites=e}this.trackedWrites.set(e,t-this.trackedStart),this.trackedEnd=Math.max(this.trackedEnd,t+e.byteLength)}startTrackingWrites(){this.trackedWrites=new Uint8Array(2**10),this.trackedStart=this.getPos(),this.trackedEnd=this.trackedStart}stopTrackingWrites(){if(!this.trackedWrites)throw Error(`Internal error: Can't get tracked writes since nothing was tracked.`);let e={data:this.trackedWrites.subarray(0,this.trackedEnd-this.trackedStart),start:this.trackedStart,end:this.trackedEnd};return this.trackedWrites=null,e}};let ka=2**16,Aa=2**32;var ja=class extends Oa{constructor(e){if(super(),this.pos=0,this.maxPos=0,this.target=e,this.supportsResize=`resize`in new ArrayBuffer(0),this.supportsResize)try{this.buffer=new ArrayBuffer(ka,{maxByteLength:Aa})}catch{this.buffer=new ArrayBuffer(ka),this.supportsResize=!1}else this.buffer=new ArrayBuffer(ka);this.bytes=new Uint8Array(this.buffer)}ensureSize(e){let t=this.buffer.byteLength;for(;t<e;)t*=2;if(t!==this.buffer.byteLength){if(t>Aa)throw Error(`ArrayBuffer exceeded maximum size of ${Aa} bytes. Please consider using another target.`);if(this.supportsResize)this.buffer.resize(t);else{let e=new ArrayBuffer(t),n=new Uint8Array(e);n.set(this.bytes,0),this.buffer=e,this.bytes=n}}}write(e){this.maybeTrackWrites(e),this.ensureSize(this.pos+e.byteLength),this.bytes.set(e,this.pos),this.target.onwrite?.(this.pos,this.pos+e.byteLength),this.pos+=e.byteLength,this.maxPos=Math.max(this.maxPos,this.pos)}seek(e){this.pos=e}getPos(){return this.pos}async flush(){}async finalize(){this.ensureSize(this.pos),this.target.buffer=this.buffer.slice(0,Math.max(this.maxPos,this.pos))}async close(){}getSlice(e,t){return this.bytes.slice(e,t)}},Ma=class extends Oa{constructor(e){super(),this.target=e,this.pos=0}write(e){this.maybeTrackWrites(e),this.target.onwrite?.(this.pos,this.pos+e.byteLength),this.pos+=e.byteLength}getPos(){return this.pos}seek(e){this.pos=e}async flush(){}async finalize(){}async close(){}},Na=class{constructor(){this._output=null,this.onwrite=null}},Pa=class extends Na{constructor(){super(...arguments),this.buffer=null}_createWriter(){return new ja(this)}},Fa=class extends Na{_createWriter(){return new Ma(this)}};
|
|
120
120
|
/*!
|
|
121
121
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
122
122
|
*
|
|
@@ -124,7 +124,7 @@ var Qr=class{constructor(e){this.writer=e,this.helper=new Uint8Array(8),this.hel
|
|
|
124
124
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
125
125
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
126
126
|
*/
|
|
127
|
-
let Fa=1e3,Ia=e=>{let t={},n=e.track;return n.metadata.name!==void 0&&(t.name=n.metadata.name),t},$=(e,t,n=!0)=>{let r=e*t;return n?Math.round(r):r};var La=class extends Jt{constructor(e,t){super(e),this.auxTarget=new Na,this.auxWriter=this.auxTarget._createWriter(),this.auxBoxWriter=new Qr(this.auxWriter),this.mdat=null,this.ftypSize=null,this.trackDatas=[],this.allTracksKnown=k(),this.creationTime=Math.floor(Date.now()/1e3)+2082844800,this.finalizedChunks=[],this.nextFragmentNumber=1,this.maxWrittenTimestamp=-1/0,this.format=t,this.writer=e._writer,this.boxWriter=new Qr(this.writer),this.isQuickTime=t instanceof Va;let n=this.writer instanceof Aa?`in-memory`:!1;this.fastStart=t._options.fastStart??n,this.isFragmented=this.fastStart===`fragmented`,(this.fastStart===`in-memory`||this.isFragmented)&&(this.writer.ensureMonotonicity=!0),this.minimumFragmentDuration=t._options.minimumFragmentDuration??1}async start(){let e=await this.mutex.acquire(),t=this.output._tracks.some(e=>e.type===`video`&&e.source._codec===`avc`);if(this.format._options.onFtyp&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(di({isQuickTime:this.isQuickTime,holdsAvc:t,fragmented:this.isFragmented})),this.format._options.onFtyp){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onFtyp(e,t)}if(this.ftypSize=this.writer.getPos(),this.fastStart!==`in-memory`)if(this.fastStart===`reserve`){for(let e of this.output._tracks)if(e.metadata.maximumPacketCount===void 0)throw Error(`All tracks must specify maximumPacketCount in their metadata when using fastStart: 'reserve'.`)}else this.isFragmented||(this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=fi(!0),this.boxWriter.writeBox(this.mdat));await this.writer.flush(),e()}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(t=>t.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(e=>e.type===`video`||e.type===`audio`?e.info.decoderConfig.codec:{webvtt:`wvtt`}[e.track.source._codec]);return dr({isQuickTime:this.isQuickTime,hasVideo:this.trackDatas.some(e=>e.type===`video`),hasAudio:this.trackDatas.some(e=>e.type===`audio`),codecStrings:e})}getVideoTrackData(e,t,n){let r=this.trackDatas.find(t=>t.track===e);if(r)return r;Wt(n),x(n),x(n.decoderConfig);let i={...n.decoderConfig};x(i.codedWidth!==void 0),x(i.codedHeight!==void 0);let a=!1;if(e.source._codec===`avc`&&!i.description){let e=nn(t.data);if(!e)throw Error(`Couldn't extract an AVCDecoderConfigurationRecord from the AVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.264) when not providing a description, or provide a description (must be an AVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in AVCC format.`);i.description=rn(e),a=!0}else if(e.source._codec===`hevc`&&!i.description){let e=ln(t.data);if(!e)throw Error(`Couldn't extract an HEVCDecoderConfigurationRecord from the HEVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.265) when not providing a description, or provide a description (must be an HEVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in HEVC format.`);i.description=_n(e),a=!0}let o=et(1/(e.metadata.frameRate??57600),1e6).denominator,s={muxer:this,track:e,type:`video`,info:{width:i.codedWidth,height:i.codedHeight,decoderConfig:i,requiresAnnexBTransformation:a},timescale:o,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(s),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}getAudioTrackData(e,t){let n=this.trackDatas.find(t=>t.track===e);if(n)return n;Kt(t),x(t),x(t.decoderConfig);let r={muxer:this,track:e,type:`audio`,info:{numberOfChannels:t.decoderConfig.numberOfChannels,sampleRate:t.decoderConfig.sampleRate,decoderConfig:t.decoderConfig,requiresPcmTransformation:!this.isFragmented&&M.includes(e.source._codec)},timescale:t.decoderConfig.sampleRate,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(r),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),r}getSubtitleTrackData(e,t){let n=this.trackDatas.find(t=>t.track===e);if(n)return n;qt(t),x(t),x(t.config);let r={muxer:this,track:e,type:`subtitle`,info:{config:t.config},timescale:1e3,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],lastCueEndTimestamp:0,cueQueue:[],nextSourceId:0,cueToSourceId:new WeakMap};return this.trackDatas.push(r),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),r}async addEncodedVideoPacket(e,t,n){let r=await this.mutex.acquire();try{let r=this.getVideoTrackData(e,t,n),i=t.data;if(r.info.requiresAnnexBTransformation){let e=$t(i);if(!e)throw Error(`Failed to transform packet data. Make sure all packets are provided in Annex B format, as specified in ITU-T-REC-H.264 and ITU-T-REC-H.265.`);i=e}let a=this.validateAndNormalizeTimestamp(r.track,t.timestamp,t.type===`key`),o=this.createSampleForTrack(r,i,a,t.duration,t.type);await this.registerSample(r,o)}finally{r()}}async addEncodedAudioPacket(e,t,n){let r=await this.mutex.acquire();try{let r=this.getAudioTrackData(e,n),i=this.validateAndNormalizeTimestamp(r.track,t.timestamp,t.type===`key`),a=this.createSampleForTrack(r,t.data,i,t.duration,t.type);r.info.requiresPcmTransformation&&await this.maybePadWithSilence(r,i),await this.registerSample(r,a)}finally{r()}}async maybePadWithSilence(e,t){let n=S(e.samples),r=n?n.timestamp+n.duration:0,i=t-r,a=$(i,e.timescale);if(a>0){let{sampleSize:t,silentValue:n}=Ft(e.info.decoderConfig.codec),o=a*e.info.numberOfChannels,s=new Uint8Array(t*o).fill(n),c=this.createSampleForTrack(e,new Uint8Array(s.buffer),r,i,`key`);await this.registerSample(e,c)}}async addSubtitleCue(e,t,n){let r=await this.mutex.acquire();try{let r=this.getSubtitleTrackData(e,n);this.validateAndNormalizeTimestamp(r.track,t.timestamp,!0),e.source._codec===`webvtt`&&(r.cueQueue.push(t),await this.processWebVTTCues(r,t.timestamp))}finally{r()}}async processWebVTTCues(e,t){for(;e.cueQueue.length>0;){let n=new Set([]);for(let r of e.cueQueue)x(r.timestamp<=t),x(e.lastCueEndTimestamp<=r.timestamp+r.duration),n.add(Math.max(r.timestamp,e.lastCueEndTimestamp)),n.add(r.timestamp+r.duration);let r=[...n].sort((e,t)=>e-t),i=r[0],a=r[1]??i;if(t<a)break;if(e.lastCueEndTimestamp<i){this.auxWriter.seek(0);let t=ua();this.auxBoxWriter.writeBox(t);let n=this.auxWriter.getSlice(0,this.auxWriter.getPos()),r=this.createSampleForTrack(e,n,e.lastCueEndTimestamp,i-e.lastCueEndTimestamp,`key`);await this.registerSample(e,r),e.lastCueEndTimestamp=i}this.auxWriter.seek(0);for(let t=0;t<e.cueQueue.length;t++){let n=e.cueQueue[t];if(n.timestamp>=a)break;Xr.lastIndex=0;let r=Xr.test(n.text),o=n.timestamp+n.duration,s=e.cueToSourceId.get(n);if(s===void 0&&a<o&&(s=e.nextSourceId++,e.cueToSourceId.set(n,s)),n.notes){let e=fa(n.notes);this.auxBoxWriter.writeBox(e)}let c=da(n.text,r?i:null,n.identifier??null,n.settings??null,s??null);this.auxBoxWriter.writeBox(c),o===a&&e.cueQueue.splice(t--,1)}let o=this.auxWriter.getSlice(0,this.auxWriter.getPos()),s=this.createSampleForTrack(e,o,i,a-i,`key`);await this.registerSample(e,s),e.lastCueEndTimestamp=a}}createSampleForTrack(e,t,n,r,i){return{timestamp:n,decodeTimestamp:n,duration:r,data:t,size:t.byteLength,type:i,timescaleUnitsToNextSample:$(r,e.timescale)}}processTimestamps(e,t){if(e.timestampProcessingQueue.length===0)return;if(e.type===`audio`&&e.info.requiresPcmTransformation){let t=0;for(let n=0;n<e.timestampProcessingQueue.length;n++){let r=e.timestampProcessingQueue[n],i=$(r.duration,e.timescale);t+=i}if(e.timeToSampleTable.length===0)e.timeToSampleTable.push({sampleCount:t,sampleDelta:1});else{let n=S(e.timeToSampleTable);n.sampleCount+=t}e.timestampProcessingQueue.length=0;return}let n=e.timestampProcessingQueue.map(e=>e.timestamp).sort((e,t)=>e-t);for(let t=0;t<e.timestampProcessingQueue.length;t++){let r=e.timestampProcessingQueue[t];r.decodeTimestamp=n[t],!this.isFragmented&&e.lastTimescaleUnits===null&&(r.decodeTimestamp=0);let i=$(r.timestamp-r.decodeTimestamp,e.timescale),a=$(r.duration,e.timescale);if(e.lastTimescaleUnits!==null){x(e.lastSample);let t=$(r.decodeTimestamp,e.timescale,!1),n=Math.round(t-e.lastTimescaleUnits);if(x(n>=0),e.lastTimescaleUnits+=n,e.lastSample.timescaleUnitsToNextSample=n,!this.isFragmented){let t=S(e.timeToSampleTable);if(x(t),t.sampleCount===1){t.sampleDelta=n;let r=e.timeToSampleTable[e.timeToSampleTable.length-2];r&&r.sampleDelta===n&&(r.sampleCount++,e.timeToSampleTable.pop(),t=r)}else t.sampleDelta!==n&&(t.sampleCount--,e.timeToSampleTable.push(t={sampleCount:1,sampleDelta:n}));t.sampleDelta===a?t.sampleCount++:e.timeToSampleTable.push({sampleCount:1,sampleDelta:a});let r=S(e.compositionTimeOffsetTable);x(r),r.sampleCompositionTimeOffset===i?r.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i})}}else e.lastTimescaleUnits=$(r.decodeTimestamp,e.timescale,!1),this.isFragmented||(e.timeToSampleTable.push({sampleCount:1,sampleDelta:a}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i}));e.lastSample=r}if(e.timestampProcessingQueue.length=0,x(e.lastSample),x(e.lastTimescaleUnits!==null),t!==void 0&&e.lastSample.timescaleUnitsToNextSample===0){x(t.type===`key`);let n=$(t.timestamp,e.timescale,!1),r=Math.round(n-e.lastTimescaleUnits);e.lastSample.timescaleUnitsToNextSample=r}}async registerSample(e,t){t.type===`key`&&this.processTimestamps(e,t),e.timestampProcessingQueue.push(t),this.isFragmented?(e.sampleQueue.push(t),await this.interleaveSamples()):this.fastStart===`reserve`?await this.registerSampleFastStartReserve(e,t):await this.addSampleToTrack(e,t)}async addSampleToTrack(e,t){if(!this.isFragmented&&(e.samples.push(t),this.fastStart===`reserve`)){let t=e.track.metadata.maximumPacketCount;if(x(t!==void 0),e.samples.length>t)throw Error(`Track #${e.track.id} has already reached the maximum packet count (${t}). Either add less packets or increase the maximum packet count.`)}let n=!1;if(!e.currentChunk)n=!0;else{e.currentChunk.startTimestamp=Math.min(e.currentChunk.startTimestamp,t.timestamp);let r=t.timestamp-e.currentChunk.startTimestamp;if(this.isFragmented){let i=this.trackDatas.every(n=>{if(e===n)return t.type===`key`;let r=n.sampleQueue[0];return r?r.type===`key`:n.track.source._closed});r>=this.minimumFragmentDuration&&i&&t.timestamp>this.maxWrittenTimestamp&&(n=!0,await this.finalizeFragment())}else n=r>=.5}n&&(e.currentChunk&&await this.finalizeCurrentChunk(e),e.currentChunk={startTimestamp:t.timestamp,samples:[],offset:null,moofOffset:null}),x(e.currentChunk),e.currentChunk.samples.push(t),this.isFragmented&&(this.maxWrittenTimestamp=Math.max(this.maxWrittenTimestamp,t.timestamp))}async finalizeCurrentChunk(e){if(x(!this.isFragmented),!e.currentChunk)return;e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk);let t=e.currentChunk.samples.length;if(e.type===`audio`&&e.info.requiresPcmTransformation&&(t=e.currentChunk.samples.reduce((t,n)=>t+$(n.duration,e.timescale),0)),(e.compactlyCodedChunkTable.length===0||S(e.compactlyCodedChunkTable).samplesPerChunk!==t)&&e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:t}),this.fastStart===`in-memory`){e.currentChunk.offset=0;return}e.currentChunk.offset=this.writer.getPos();for(let t of e.currentChunk.samples)x(t.data),this.writer.write(t.data),t.data=null;await this.writer.flush()}async interleaveSamples(e=!1){if(x(this.isFragmented),!(!e&&!this.allTracksAreKnown()))outer:for(;;){let t=null,n=1/0;for(let r of this.trackDatas){if(!e&&r.sampleQueue.length===0&&!r.track.source._closed)break outer;r.sampleQueue.length>0&&r.sampleQueue[0].timestamp<n&&(t=r,n=r.sampleQueue[0].timestamp)}if(!t)break;let r=t.sampleQueue.shift();await this.addSampleToTrack(t,r)}}async finalizeFragment(e=!0){x(this.isFragmented);let t=this.nextFragmentNumber++;if(t===1){this.format._options.onMoov&&this.writer.startTrackingWrites();let e=mi(this);if(this.boxWriter.writeBox(e),this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}}let n=this.trackDatas.filter(e=>e.currentChunk),r=ea(t,n),i=this.writer.getPos(),a=i+this.boxWriter.measureBox(r),o=a+8,s=1/0;for(let e of n){e.currentChunk.offset=o,e.currentChunk.moofOffset=i;for(let t of e.currentChunk.samples)o+=t.size;s=Math.min(s,e.currentChunk.startTimestamp)}let c=o-a,l=c>=2**32;if(l)for(let e of n)e.currentChunk.offset+=8;this.format._options.onMoof&&this.writer.startTrackingWrites();let u=ea(t,n);if(this.boxWriter.writeBox(u),this.format._options.onMoof){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoof(e,t,s)}x(this.writer.getPos()===a),this.format._options.onMdat&&this.writer.startTrackingWrites();let d=fi(l);d.size=c,this.boxWriter.writeBox(d),this.writer.seek(a+(l?16:8));for(let e of n)for(let t of e.currentChunk.samples)this.writer.write(t.data),t.data=null;if(this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}for(let e of n)e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk),e.currentChunk=null;e&&await this.writer.flush()}async registerSampleFastStartReserve(e,t){if(this.allTracksAreKnown()){if(!this.mdat){let e=mi(this),t=this.boxWriter.measureBox(e)+this.computeSampleTableSizeUpperBound()+4096;x(this.ftypSize!==null),this.writer.seek(this.ftypSize+t),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=fi(!0),this.boxWriter.writeBox(this.mdat);for(let e of this.trackDatas){for(let t of e.sampleQueue)await this.addSampleToTrack(e,t);e.sampleQueue.length=0}}await this.addSampleToTrack(e,t)}else e.sampleQueue.push(t)}computeSampleTableSizeUpperBound(){x(this.fastStart===`reserve`);let e=0;for(let t of this.trackDatas){let n=t.track.metadata.maximumPacketCount;x(n!==void 0),e+=8*Math.ceil(2/3*n),e+=4*n,e+=8*Math.ceil(2/3*n),e+=12*Math.ceil(2/3*n),e+=4*n,e+=8*n}return e}async onTrackClose(e){let t=await this.mutex.acquire();if(e.type===`subtitle`&&e.source._codec===`webvtt`){let t=this.trackDatas.find(t=>t.track===e);t&&await this.processWebVTTCues(t,1/0)}this.allTracksAreKnown()&&this.allTracksKnown.resolve(),this.isFragmented&&await this.interleaveSamples(),t()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let e of this.trackDatas)e.type===`subtitle`&&e.track.source._codec===`webvtt`&&await this.processWebVTTCues(e,1/0);if(this.isFragmented){await this.interleaveSamples(!0);for(let e of this.trackDatas)this.processTimestamps(e);await this.finalizeFragment(!1)}else for(let e of this.trackDatas)this.processTimestamps(e),await this.finalizeCurrentChunk(e);if(this.fastStart===`in-memory`){this.mdat=fi(!1);let e;for(let t=0;t<2;t++){let t=mi(this),n=this.boxWriter.measureBox(t);e=this.boxWriter.measureBox(this.mdat);let r=this.writer.getPos()+n+e;for(let t of this.finalizedChunks){t.offset=r;for(let{data:n}of t.samples)x(n),r+=n.byteLength,e+=n.byteLength}if(r<2**32)break;e>=2**32&&(this.mdat.largeSize=!0)}this.format._options.onMoov&&this.writer.startTrackingWrites();let t=mi(this);if(this.boxWriter.writeBox(t),this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat.size=e,this.boxWriter.writeBox(this.mdat);for(let e of this.finalizedChunks)for(let t of e.samples)x(t.data),this.writer.write(t.data),t.data=null;if(this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}}else if(this.isFragmented){let e=this.writer.getPos(),t=sa(this.trackDatas);this.boxWriter.writeBox(t);let n=this.writer.getPos()-e;this.writer.seek(this.writer.getPos()-4),this.boxWriter.writeU32(n)}else{x(this.mdat);let e=this.boxWriter.offsets.get(this.mdat);x(e!==void 0);let t=this.writer.getPos()-e;if(this.mdat.size=t,this.mdat.largeSize=t>=2**32,this.boxWriter.patchBox(this.mdat),this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}let n=mi(this);if(this.fastStart===`reserve`){x(this.ftypSize!==null),this.writer.seek(this.ftypSize),this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(n);let e=this.boxWriter.offsets.get(this.mdat)-this.writer.getPos();this.boxWriter.writeBox(pi(e))}else this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(n);if(this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}}e()}},Ra=class{getSupportedVideoCodecs(){return this.getSupportedCodecs().filter(e=>j.includes(e))}getSupportedAudioCodecs(){return this.getSupportedCodecs().filter(e=>N.includes(e))}getSupportedSubtitleCodecs(){return this.getSupportedCodecs().filter(e=>bt.includes(e))}_codecUnsupportedHint(e){return``}},za=class extends Ra{constructor(e={}){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(e.fastStart!==void 0&&![!1,`in-memory`,`reserve`,`fragmented`].includes(e.fastStart))throw TypeError(`options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'.`);if(e.minimumFragmentDuration!==void 0&&(!Number.isFinite(e.minimumFragmentDuration)||e.minimumFragmentDuration<0))throw TypeError(`options.minimumFragmentDuration, when provided, must be a non-negative number.`);if(e.onFtyp!==void 0&&typeof e.onFtyp!=`function`)throw TypeError(`options.onFtyp, when provided, must be a function.`);if(e.onMoov!==void 0&&typeof e.onMoov!=`function`)throw TypeError(`options.onMoov, when provided, must be a function.`);if(e.onMdat!==void 0&&typeof e.onMdat!=`function`)throw TypeError(`options.onMdat, when provided, must be a function.`);if(e.onMoof!==void 0&&typeof e.onMoof!=`function`)throw TypeError(`options.onMoof, when provided, must be a function.`);if(e.metadataFormat!==void 0&&![`mdir`,`mdta`,`udta`,`auto`].includes(e.metadataFormat))throw TypeError(`options.metadataFormat, when provided, must be either 'auto', 'mdir', 'mdta', or 'udta'.`);super(),this._options=e}getSupportedTrackCounts(){return{video:{min:0,max:1/0},audio:{min:0,max:1/0},subtitle:{min:0,max:1/0},total:{min:1,max:2**32-1}}}get supportsVideoRotationMetadata(){return!0}_createMuxer(e){return new La(e,this)}},Ba=class extends za{constructor(e){super(e)}get _name(){return`MP4`}get fileExtension(){return`.mp4`}get mimeType(){return`video/mp4`}getSupportedCodecs(){return[...j,...yt,`pcm-s16`,`pcm-s16be`,`pcm-s24`,`pcm-s24be`,`pcm-s32`,`pcm-s32be`,`pcm-f32`,`pcm-f32be`,`pcm-f64`,`pcm-f64be`,...bt]}_codecUnsupportedHint(e){return new Va().getSupportedCodecs().includes(e)?` Switching to MOV will grant support for this codec.`:``}},Va=class extends za{constructor(e){super(e)}get _name(){return`MOV`}get fileExtension(){return`.mov`}get mimeType(){return`video/quicktime`}getSupportedCodecs(){return[...j,...N]}_codecUnsupportedHint(e){return new Ba().getSupportedCodecs().includes(e)?` Switching to MP4 will grant support for this codec.`:``}};
|
|
127
|
+
let Ia=1e3,La=e=>{let t={},n=e.track;return n.metadata.name!==void 0&&(t.name=n.metadata.name),t},$=(e,t,n=!0)=>{let r=e*t;return n?Math.round(r):r};var Ra=class extends Xt{constructor(e,t){super(e),this.auxTarget=new Pa,this.auxWriter=this.auxTarget._createWriter(),this.auxBoxWriter=new $r(this.auxWriter),this.mdat=null,this.ftypSize=null,this.trackDatas=[],this.allTracksKnown=O(),this.creationTime=Math.floor(Date.now()/1e3)+2082844800,this.finalizedChunks=[],this.nextFragmentNumber=1,this.maxWrittenTimestamp=-1/0,this.format=t,this.writer=e._writer,this.boxWriter=new $r(this.writer),this.isQuickTime=t instanceof Ha;let n=this.writer instanceof ja?`in-memory`:!1;this.fastStart=t._options.fastStart??n,this.isFragmented=this.fastStart===`fragmented`,(this.fastStart===`in-memory`||this.isFragmented)&&(this.writer.ensureMonotonicity=!0),this.minimumFragmentDuration=t._options.minimumFragmentDuration??1}async start(){let e=await this.mutex.acquire(),t=this.output._tracks.some(e=>e.type===`video`&&e.source._codec===`avc`);if(this.format._options.onFtyp&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(fi({isQuickTime:this.isQuickTime,holdsAvc:t,fragmented:this.isFragmented})),this.format._options.onFtyp){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onFtyp(e,t)}if(this.ftypSize=this.writer.getPos(),this.fastStart!==`in-memory`)if(this.fastStart===`reserve`){for(let e of this.output._tracks)if(e.metadata.maximumPacketCount===void 0)throw Error(`All tracks must specify maximumPacketCount in their metadata when using fastStart: 'reserve'.`)}else this.isFragmented||(this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=pi(!0),this.boxWriter.writeBox(this.mdat));await this.writer.flush(),e()}allTracksAreKnown(){for(let e of this.output._tracks)if(!e.source._closed&&!this.trackDatas.some(t=>t.track===e))return!1;return!0}async getMimeType(){await this.allTracksKnown.promise;let e=this.trackDatas.map(e=>e.type===`video`||e.type===`audio`?e.info.decoderConfig.codec:{webvtt:`wvtt`}[e.track.source._codec]);return fr({isQuickTime:this.isQuickTime,hasVideo:this.trackDatas.some(e=>e.type===`video`),hasAudio:this.trackDatas.some(e=>e.type===`audio`),codecStrings:e})}getVideoTrackData(e,t,n){let r=this.trackDatas.find(t=>t.track===e);if(r)return r;Kt(n),b(n),b(n.decoderConfig);let i={...n.decoderConfig};b(i.codedWidth!==void 0),b(i.codedHeight!==void 0);let a=!1;if(e.source._codec===`avc`&&!i.description){let e=an(t.data);if(!e)throw Error(`Couldn't extract an AVCDecoderConfigurationRecord from the AVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.264) when not providing a description, or provide a description (must be an AVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in AVCC format.`);i.description=on(e),a=!0}else if(e.source._codec===`hevc`&&!i.description){let e=dn(t.data);if(!e)throw Error(`Couldn't extract an HEVCDecoderConfigurationRecord from the HEVC packet. Make sure the packets are in Annex B format (as specified in ITU-T-REC-H.265) when not providing a description, or provide a description (must be an HEVCDecoderConfigurationRecord as specified in ISO 14496-15) and ensure the packets are in HEVC format.`);i.description=yn(e),a=!0}let o=nt(1/(e.metadata.frameRate??57600),1e6).denominator,s={muxer:this,track:e,type:`video`,info:{width:i.codedWidth,height:i.codedHeight,decoderConfig:i,requiresAnnexBTransformation:a},timescale:o,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(s),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),s}getAudioTrackData(e,t){let n=this.trackDatas.find(t=>t.track===e);if(n)return n;Jt(t),b(t),b(t.decoderConfig);let r={muxer:this,track:e,type:`audio`,info:{numberOfChannels:t.decoderConfig.numberOfChannels,sampleRate:t.decoderConfig.sampleRate,decoderConfig:t.decoderConfig,requiresPcmTransformation:!this.isFragmented&&j.includes(e.source._codec)},timescale:t.decoderConfig.sampleRate,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[]};return this.trackDatas.push(r),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),r}getSubtitleTrackData(e,t){let n=this.trackDatas.find(t=>t.track===e);if(n)return n;Yt(t),b(t),b(t.config);let r={muxer:this,track:e,type:`subtitle`,info:{config:t.config},timescale:1e3,samples:[],sampleQueue:[],timestampProcessingQueue:[],timeToSampleTable:[],compositionTimeOffsetTable:[],lastTimescaleUnits:null,lastSample:null,finalizedChunks:[],currentChunk:null,compactlyCodedChunkTable:[],lastCueEndTimestamp:0,cueQueue:[],nextSourceId:0,cueToSourceId:new WeakMap};return this.trackDatas.push(r),this.trackDatas.sort((e,t)=>e.track.id-t.track.id),this.allTracksAreKnown()&&this.allTracksKnown.resolve(),r}async addEncodedVideoPacket(e,t,n){let r=await this.mutex.acquire();try{let r=this.getVideoTrackData(e,t,n),i=t.data;if(r.info.requiresAnnexBTransformation){let e=tn(i);if(!e)throw Error(`Failed to transform packet data. Make sure all packets are provided in Annex B format, as specified in ITU-T-REC-H.264 and ITU-T-REC-H.265.`);i=e}let a=this.validateAndNormalizeTimestamp(r.track,t.timestamp,t.type===`key`),o=this.createSampleForTrack(r,i,a,t.duration,t.type);await this.registerSample(r,o)}finally{r()}}async addEncodedAudioPacket(e,t,n){let r=await this.mutex.acquire();try{let r=this.getAudioTrackData(e,n),i=this.validateAndNormalizeTimestamp(r.track,t.timestamp,t.type===`key`),a=this.createSampleForTrack(r,t.data,i,t.duration,t.type);r.info.requiresPcmTransformation&&await this.maybePadWithSilence(r,i),await this.registerSample(r,a)}finally{r()}}async maybePadWithSilence(e,t){let n=x(e.samples),r=n?n.timestamp+n.duration:0,i=t-r,a=$(i,e.timescale);if(a>0){let{sampleSize:t,silentValue:n}=Lt(e.info.decoderConfig.codec),o=a*e.info.numberOfChannels,s=new Uint8Array(t*o).fill(n),c=this.createSampleForTrack(e,new Uint8Array(s.buffer),r,i,`key`);await this.registerSample(e,c)}}async addSubtitleCue(e,t,n){let r=await this.mutex.acquire();try{let r=this.getSubtitleTrackData(e,n);this.validateAndNormalizeTimestamp(r.track,t.timestamp,!0),e.source._codec===`webvtt`&&(r.cueQueue.push(t),await this.processWebVTTCues(r,t.timestamp))}finally{r()}}async processWebVTTCues(e,t){for(;e.cueQueue.length>0;){let n=new Set([]);for(let r of e.cueQueue)b(r.timestamp<=t),b(e.lastCueEndTimestamp<=r.timestamp+r.duration),n.add(Math.max(r.timestamp,e.lastCueEndTimestamp)),n.add(r.timestamp+r.duration);let r=[...n].sort((e,t)=>e-t),i=r[0],a=r[1]??i;if(t<a)break;if(e.lastCueEndTimestamp<i){this.auxWriter.seek(0);let t=fa();this.auxBoxWriter.writeBox(t);let n=this.auxWriter.getSlice(0,this.auxWriter.getPos()),r=this.createSampleForTrack(e,n,e.lastCueEndTimestamp,i-e.lastCueEndTimestamp,`key`);await this.registerSample(e,r),e.lastCueEndTimestamp=i}this.auxWriter.seek(0);for(let t=0;t<e.cueQueue.length;t++){let n=e.cueQueue[t];if(n.timestamp>=a)break;Zr.lastIndex=0;let r=Zr.test(n.text),o=n.timestamp+n.duration,s=e.cueToSourceId.get(n);if(s===void 0&&a<o&&(s=e.nextSourceId++,e.cueToSourceId.set(n,s)),n.notes){let e=ma(n.notes);this.auxBoxWriter.writeBox(e)}let c=pa(n.text,r?i:null,n.identifier??null,n.settings??null,s??null);this.auxBoxWriter.writeBox(c),o===a&&e.cueQueue.splice(t--,1)}let o=this.auxWriter.getSlice(0,this.auxWriter.getPos()),s=this.createSampleForTrack(e,o,i,a-i,`key`);await this.registerSample(e,s),e.lastCueEndTimestamp=a}}createSampleForTrack(e,t,n,r,i){return{timestamp:n,decodeTimestamp:n,duration:r,data:t,size:t.byteLength,type:i,timescaleUnitsToNextSample:$(r,e.timescale)}}processTimestamps(e,t){if(e.timestampProcessingQueue.length===0)return;if(e.type===`audio`&&e.info.requiresPcmTransformation){let t=0;for(let n=0;n<e.timestampProcessingQueue.length;n++){let r=e.timestampProcessingQueue[n],i=$(r.duration,e.timescale);t+=i}if(e.timeToSampleTable.length===0)e.timeToSampleTable.push({sampleCount:t,sampleDelta:1});else{let n=x(e.timeToSampleTable);n.sampleCount+=t}e.timestampProcessingQueue.length=0;return}let n=e.timestampProcessingQueue.map(e=>e.timestamp).sort((e,t)=>e-t);for(let t=0;t<e.timestampProcessingQueue.length;t++){let r=e.timestampProcessingQueue[t];r.decodeTimestamp=n[t],!this.isFragmented&&e.lastTimescaleUnits===null&&(r.decodeTimestamp=0);let i=$(r.timestamp-r.decodeTimestamp,e.timescale),a=$(r.duration,e.timescale);if(e.lastTimescaleUnits!==null){b(e.lastSample);let t=$(r.decodeTimestamp,e.timescale,!1),n=Math.round(t-e.lastTimescaleUnits);if(b(n>=0),e.lastTimescaleUnits+=n,e.lastSample.timescaleUnitsToNextSample=n,!this.isFragmented){let t=x(e.timeToSampleTable);if(b(t),t.sampleCount===1){t.sampleDelta=n;let r=e.timeToSampleTable[e.timeToSampleTable.length-2];r&&r.sampleDelta===n&&(r.sampleCount++,e.timeToSampleTable.pop(),t=r)}else t.sampleDelta!==n&&(t.sampleCount--,e.timeToSampleTable.push(t={sampleCount:1,sampleDelta:n}));t.sampleDelta===a?t.sampleCount++:e.timeToSampleTable.push({sampleCount:1,sampleDelta:a});let r=x(e.compositionTimeOffsetTable);b(r),r.sampleCompositionTimeOffset===i?r.sampleCount++:e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i})}}else e.lastTimescaleUnits=$(r.decodeTimestamp,e.timescale,!1),this.isFragmented||(e.timeToSampleTable.push({sampleCount:1,sampleDelta:a}),e.compositionTimeOffsetTable.push({sampleCount:1,sampleCompositionTimeOffset:i}));e.lastSample=r}if(e.timestampProcessingQueue.length=0,b(e.lastSample),b(e.lastTimescaleUnits!==null),t!==void 0&&e.lastSample.timescaleUnitsToNextSample===0){b(t.type===`key`);let n=$(t.timestamp,e.timescale,!1),r=Math.round(n-e.lastTimescaleUnits);e.lastSample.timescaleUnitsToNextSample=r}}async registerSample(e,t){t.type===`key`&&this.processTimestamps(e,t),e.timestampProcessingQueue.push(t),this.isFragmented?(e.sampleQueue.push(t),await this.interleaveSamples()):this.fastStart===`reserve`?await this.registerSampleFastStartReserve(e,t):await this.addSampleToTrack(e,t)}async addSampleToTrack(e,t){if(!this.isFragmented&&(e.samples.push(t),this.fastStart===`reserve`)){let t=e.track.metadata.maximumPacketCount;if(b(t!==void 0),e.samples.length>t)throw Error(`Track #${e.track.id} has already reached the maximum packet count (${t}). Either add less packets or increase the maximum packet count.`)}let n=!1;if(!e.currentChunk)n=!0;else{e.currentChunk.startTimestamp=Math.min(e.currentChunk.startTimestamp,t.timestamp);let r=t.timestamp-e.currentChunk.startTimestamp;if(this.isFragmented){let i=this.trackDatas.every(n=>{if(e===n)return t.type===`key`;let r=n.sampleQueue[0];return r?r.type===`key`:n.track.source._closed});r>=this.minimumFragmentDuration&&i&&t.timestamp>this.maxWrittenTimestamp&&(n=!0,await this.finalizeFragment())}else n=r>=.5}n&&(e.currentChunk&&await this.finalizeCurrentChunk(e),e.currentChunk={startTimestamp:t.timestamp,samples:[],offset:null,moofOffset:null}),b(e.currentChunk),e.currentChunk.samples.push(t),this.isFragmented&&(this.maxWrittenTimestamp=Math.max(this.maxWrittenTimestamp,t.timestamp))}async finalizeCurrentChunk(e){if(b(!this.isFragmented),!e.currentChunk)return;e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk);let t=e.currentChunk.samples.length;if(e.type===`audio`&&e.info.requiresPcmTransformation&&(t=e.currentChunk.samples.reduce((t,n)=>t+$(n.duration,e.timescale),0)),(e.compactlyCodedChunkTable.length===0||x(e.compactlyCodedChunkTable).samplesPerChunk!==t)&&e.compactlyCodedChunkTable.push({firstChunk:e.finalizedChunks.length,samplesPerChunk:t}),this.fastStart===`in-memory`){e.currentChunk.offset=0;return}e.currentChunk.offset=this.writer.getPos();for(let t of e.currentChunk.samples)b(t.data),this.writer.write(t.data),t.data=null;await this.writer.flush()}async interleaveSamples(e=!1){if(b(this.isFragmented),!(!e&&!this.allTracksAreKnown()))outer:for(;;){let t=null,n=1/0;for(let r of this.trackDatas){if(!e&&r.sampleQueue.length===0&&!r.track.source._closed)break outer;r.sampleQueue.length>0&&r.sampleQueue[0].timestamp<n&&(t=r,n=r.sampleQueue[0].timestamp)}if(!t)break;let r=t.sampleQueue.shift();await this.addSampleToTrack(t,r)}}async finalizeFragment(e=!0){b(this.isFragmented);let t=this.nextFragmentNumber++;if(t===1){this.format._options.onMoov&&this.writer.startTrackingWrites();let e=hi(this);if(this.boxWriter.writeBox(e),this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}}let n=this.trackDatas.filter(e=>e.currentChunk),r=na(t,n),i=this.writer.getPos(),a=i+this.boxWriter.measureBox(r),o=a+8,s=1/0;for(let e of n){e.currentChunk.offset=o,e.currentChunk.moofOffset=i;for(let t of e.currentChunk.samples)o+=t.size;s=Math.min(s,e.currentChunk.startTimestamp)}let c=o-a,l=c>=2**32;if(l)for(let e of n)e.currentChunk.offset+=8;this.format._options.onMoof&&this.writer.startTrackingWrites();let u=na(t,n);if(this.boxWriter.writeBox(u),this.format._options.onMoof){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoof(e,t,s)}b(this.writer.getPos()===a),this.format._options.onMdat&&this.writer.startTrackingWrites();let d=pi(l);d.size=c,this.boxWriter.writeBox(d),this.writer.seek(a+(l?16:8));for(let e of n)for(let t of e.currentChunk.samples)this.writer.write(t.data),t.data=null;if(this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}for(let e of n)e.finalizedChunks.push(e.currentChunk),this.finalizedChunks.push(e.currentChunk),e.currentChunk=null;e&&await this.writer.flush()}async registerSampleFastStartReserve(e,t){if(this.allTracksAreKnown()){if(!this.mdat){let e=hi(this),t=this.boxWriter.measureBox(e)+this.computeSampleTableSizeUpperBound()+4096;b(this.ftypSize!==null),this.writer.seek(this.ftypSize+t),this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat=pi(!0),this.boxWriter.writeBox(this.mdat);for(let e of this.trackDatas){for(let t of e.sampleQueue)await this.addSampleToTrack(e,t);e.sampleQueue.length=0}}await this.addSampleToTrack(e,t)}else e.sampleQueue.push(t)}computeSampleTableSizeUpperBound(){b(this.fastStart===`reserve`);let e=0;for(let t of this.trackDatas){let n=t.track.metadata.maximumPacketCount;b(n!==void 0),e+=8*Math.ceil(2/3*n),e+=4*n,e+=8*Math.ceil(2/3*n),e+=12*Math.ceil(2/3*n),e+=4*n,e+=8*n}return e}async onTrackClose(e){let t=await this.mutex.acquire();if(e.type===`subtitle`&&e.source._codec===`webvtt`){let t=this.trackDatas.find(t=>t.track===e);t&&await this.processWebVTTCues(t,1/0)}this.allTracksAreKnown()&&this.allTracksKnown.resolve(),this.isFragmented&&await this.interleaveSamples(),t()}async finalize(){let e=await this.mutex.acquire();this.allTracksKnown.resolve();for(let e of this.trackDatas)e.type===`subtitle`&&e.track.source._codec===`webvtt`&&await this.processWebVTTCues(e,1/0);if(this.isFragmented){await this.interleaveSamples(!0);for(let e of this.trackDatas)this.processTimestamps(e);await this.finalizeFragment(!1)}else for(let e of this.trackDatas)this.processTimestamps(e),await this.finalizeCurrentChunk(e);if(this.fastStart===`in-memory`){this.mdat=pi(!1);let e;for(let t=0;t<2;t++){let t=hi(this),n=this.boxWriter.measureBox(t);e=this.boxWriter.measureBox(this.mdat);let r=this.writer.getPos()+n+e;for(let t of this.finalizedChunks){t.offset=r;for(let{data:n}of t.samples)b(n),r+=n.byteLength,e+=n.byteLength}if(r<2**32)break;e>=2**32&&(this.mdat.largeSize=!0)}this.format._options.onMoov&&this.writer.startTrackingWrites();let t=hi(this);if(this.boxWriter.writeBox(t),this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}this.format._options.onMdat&&this.writer.startTrackingWrites(),this.mdat.size=e,this.boxWriter.writeBox(this.mdat);for(let e of this.finalizedChunks)for(let t of e.samples)b(t.data),this.writer.write(t.data),t.data=null;if(this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}}else if(this.isFragmented){let e=this.writer.getPos(),t=la(this.trackDatas);this.boxWriter.writeBox(t);let n=this.writer.getPos()-e;this.writer.seek(this.writer.getPos()-4),this.boxWriter.writeU32(n)}else{b(this.mdat);let e=this.boxWriter.offsets.get(this.mdat);b(e!==void 0);let t=this.writer.getPos()-e;if(this.mdat.size=t,this.mdat.largeSize=t>=2**32,this.boxWriter.patchBox(this.mdat),this.format._options.onMdat){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMdat(e,t)}let n=hi(this);if(this.fastStart===`reserve`){b(this.ftypSize!==null),this.writer.seek(this.ftypSize),this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(n);let e=this.boxWriter.offsets.get(this.mdat)-this.writer.getPos();this.boxWriter.writeBox(mi(e))}else this.format._options.onMoov&&this.writer.startTrackingWrites(),this.boxWriter.writeBox(n);if(this.format._options.onMoov){let{data:e,start:t}=this.writer.stopTrackingWrites();this.format._options.onMoov(e,t)}}e()}},za=class{getSupportedVideoCodecs(){return this.getSupportedCodecs().filter(e=>A.includes(e))}getSupportedAudioCodecs(){return this.getSupportedCodecs().filter(e=>M.includes(e))}getSupportedSubtitleCodecs(){return this.getSupportedCodecs().filter(e=>St.includes(e))}_codecUnsupportedHint(e){return``}},Ba=class extends za{constructor(e={}){if(!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(e.fastStart!==void 0&&![!1,`in-memory`,`reserve`,`fragmented`].includes(e.fastStart))throw TypeError(`options.fastStart, when provided, must be false, 'in-memory', 'reserve', or 'fragmented'.`);if(e.minimumFragmentDuration!==void 0&&(!Number.isFinite(e.minimumFragmentDuration)||e.minimumFragmentDuration<0))throw TypeError(`options.minimumFragmentDuration, when provided, must be a non-negative number.`);if(e.onFtyp!==void 0&&typeof e.onFtyp!=`function`)throw TypeError(`options.onFtyp, when provided, must be a function.`);if(e.onMoov!==void 0&&typeof e.onMoov!=`function`)throw TypeError(`options.onMoov, when provided, must be a function.`);if(e.onMdat!==void 0&&typeof e.onMdat!=`function`)throw TypeError(`options.onMdat, when provided, must be a function.`);if(e.onMoof!==void 0&&typeof e.onMoof!=`function`)throw TypeError(`options.onMoof, when provided, must be a function.`);if(e.metadataFormat!==void 0&&![`mdir`,`mdta`,`udta`,`auto`].includes(e.metadataFormat))throw TypeError(`options.metadataFormat, when provided, must be either 'auto', 'mdir', 'mdta', or 'udta'.`);super(),this._options=e}getSupportedTrackCounts(){return{video:{min:0,max:1/0},audio:{min:0,max:1/0},subtitle:{min:0,max:1/0},total:{min:1,max:2**32-1}}}get supportsVideoRotationMetadata(){return!0}_createMuxer(e){return new Ra(e,this)}},Va=class extends Ba{constructor(e){super(e)}get _name(){return`MP4`}get fileExtension(){return`.mp4`}get mimeType(){return`video/mp4`}getSupportedCodecs(){return[...A,...xt,`pcm-s16`,`pcm-s16be`,`pcm-s24`,`pcm-s24be`,`pcm-s32`,`pcm-s32be`,`pcm-f32`,`pcm-f32be`,`pcm-f64`,`pcm-f64be`,...St]}_codecUnsupportedHint(e){return new Ha().getSupportedCodecs().includes(e)?` Switching to MOV will grant support for this codec.`:``}},Ha=class extends Ba{constructor(e){super(e)}get _name(){return`MOV`}get fileExtension(){return`.mov`}get mimeType(){return`video/quicktime`}getSupportedCodecs(){return[...A,...M]}_codecUnsupportedHint(e){return new Va().getSupportedCodecs().includes(e)?` Switching to MP4 will grant support for this codec.`:``}};
|
|
128
128
|
/*!
|
|
129
129
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
130
130
|
*
|
|
@@ -132,7 +132,7 @@ let Fa=1e3,Ia=e=>{let t={},n=e.track;return n.metadata.name!==void 0&&(t.name=n.
|
|
|
132
132
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
133
133
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
134
134
|
*/
|
|
135
|
-
let
|
|
135
|
+
let Ua=e=>{if(!e||typeof e!=`object`)throw TypeError(`Encoding config must be an object.`);if(!A.includes(e.codec))throw TypeError(`Invalid video codec '${e.codec}'. Must be one of: ${A.join(`, `)}.`);if(!(e.bitrate instanceof Ya)&&(!Number.isInteger(e.bitrate)||e.bitrate<=0))throw TypeError(`config.bitrate must be a positive integer or a quality.`);if(e.keyFrameInterval!==void 0&&(!Number.isFinite(e.keyFrameInterval)||e.keyFrameInterval<0))throw TypeError(`config.keyFrameInterval, when provided, must be a non-negative number.`);if(e.sizeChangeBehavior!==void 0&&![`deny`,`passThrough`,`fill`,`contain`,`cover`].includes(e.sizeChangeBehavior))throw TypeError(`config.sizeChangeBehavior, when provided, must be 'deny', 'passThrough', 'fill', 'contain' or 'cover'.`);if(e.onEncodedPacket!==void 0&&typeof e.onEncodedPacket!=`function`)throw TypeError(`config.onEncodedChunk, when provided, must be a function.`);if(e.onEncoderConfig!==void 0&&typeof e.onEncoderConfig!=`function`)throw TypeError(`config.onEncoderConfig, when provided, must be a function.`);Wa(e.codec,e)},Wa=(e,t)=>{if(!t||typeof t!=`object`)throw TypeError(`Encoding options must be an object.`);if(t.alpha!==void 0&&![`discard`,`keep`].includes(t.alpha))throw TypeError(`options.alpha, when provided, must be 'discard' or 'keep'.`);if(t.bitrateMode!==void 0&&![`constant`,`variable`].includes(t.bitrateMode))throw TypeError(`bitrateMode, when provided, must be 'constant' or 'variable'.`);if(t.latencyMode!==void 0&&![`quality`,`realtime`].includes(t.latencyMode))throw TypeError(`latencyMode, when provided, must be 'quality' or 'realtime'.`);if(t.fullCodecString!==void 0&&typeof t.fullCodecString!=`string`)throw TypeError(`fullCodecString, when provided, must be a string.`);if(t.fullCodecString!==void 0&&Rt(t.fullCodecString)!==e)throw TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${e}).`);if(t.hardwareAcceleration!==void 0&&![`no-preference`,`prefer-hardware`,`prefer-software`].includes(t.hardwareAcceleration))throw TypeError(`hardwareAcceleration, when provided, must be 'no-preference', 'prefer-hardware' or 'prefer-software'.`);if(t.scalabilityMode!==void 0&&typeof t.scalabilityMode!=`string`)throw TypeError(`scalabilityMode, when provided, must be a string.`);if(t.contentHint!==void 0&&typeof t.contentHint!=`string`)throw TypeError(`contentHint, when provided, must be a string.`)},Ga=e=>{let t=e.bitrate instanceof Ya?e.bitrate._toVideoBitrate(e.codec,e.width,e.height):e.bitrate;return{codec:e.fullCodecString??Dt(e.codec,e.width,e.height,t),width:e.width,height:e.height,bitrate:t,bitrateMode:e.bitrateMode,alpha:e.alpha??`discard`,framerate:e.framerate,latencyMode:e.latencyMode,hardwareAcceleration:e.hardwareAcceleration,scalabilityMode:e.scalabilityMode,contentHint:e.contentHint,...zt(e.codec)}},Ka=e=>{if(!e||typeof e!=`object`)throw TypeError(`Encoding config must be an object.`);if(!M.includes(e.codec))throw TypeError(`Invalid audio codec '${e.codec}'. Must be one of: ${M.join(`, `)}.`);if(e.bitrate===void 0&&(!j.includes(e.codec)||e.codec===`flac`))throw TypeError(`config.bitrate must be provided for compressed audio codecs.`);if(e.bitrate!==void 0&&!(e.bitrate instanceof Ya)&&(!Number.isInteger(e.bitrate)||e.bitrate<=0))throw TypeError(`config.bitrate, when provided, must be a positive integer or a quality.`);if(e.onEncodedPacket!==void 0&&typeof e.onEncodedPacket!=`function`)throw TypeError(`config.onEncodedChunk, when provided, must be a function.`);if(e.onEncoderConfig!==void 0&&typeof e.onEncoderConfig!=`function`)throw TypeError(`config.onEncoderConfig, when provided, must be a function.`);qa(e.codec,e)},qa=(e,t)=>{if(!t||typeof t!=`object`)throw TypeError(`Encoding options must be an object.`);if(t.bitrateMode!==void 0&&![`constant`,`variable`].includes(t.bitrateMode))throw TypeError(`bitrateMode, when provided, must be 'constant' or 'variable'.`);if(t.fullCodecString!==void 0&&typeof t.fullCodecString!=`string`)throw TypeError(`fullCodecString, when provided, must be a string.`);if(t.fullCodecString!==void 0&&Rt(t.fullCodecString)!==e)throw TypeError(`fullCodecString, when provided, must be a string that matches the specified codec (${e}).`)},Ja=e=>{let t=e.bitrate instanceof Ya?e.bitrate._toAudioBitrate(e.codec):e.bitrate;return{codec:e.fullCodecString??At(e.codec,e.numberOfChannels,e.sampleRate),numberOfChannels:e.numberOfChannels,sampleRate:e.sampleRate,bitrate:t,bitrateMode:e.bitrateMode,...Bt(e.codec)}};var Ya=class{constructor(e){this._factor=e}_toVideoBitrate(e,t,n){let r=3e6*(t*n/(1920*1080))**.95*{avc:1,hevc:.6,vp9:.6,av1:.4,vp8:1.2}[e]*this._factor;return Math.ceil(r/1e3)*1e3}_toAudioBitrate(e){if(j.includes(e)||e===`flac`)return;let t={aac:128e3,opus:64e3,mp3:16e4,vorbis:64e3}[e];if(!t)throw Error(`Unhandled codec: ${e}`);let n=t*this._factor;return e===`aac`?n=[96e3,128e3,16e4,192e3].reduce((e,t)=>Math.abs(t-n)<Math.abs(e-n)?t:e):e===`opus`||e===`vorbis`?n=Math.max(6e3,n):e===`mp3`&&(n=[8e3,16e3,24e3,32e3,4e4,48e3,64e3,8e4,96e3,112e3,128e3,16e4,192e3,224e3,256e3,32e4].reduce((e,t)=>Math.abs(t-n)<Math.abs(e-n)?t:e)),Math.round(n/1e3)*1e3}};let Xa=new Ya(2),Za=async(e,t={})=>{let{width:n=1280,height:r=720,bitrate:i=1e6,...a}=t;if(!A.includes(e))return!1;if(!Number.isInteger(n)||n<=0)throw TypeError(`width must be a positive integer.`);if(!Number.isInteger(r)||r<=0)throw TypeError(`height must be a positive integer.`);if(!(i instanceof Ya)&&(!Number.isInteger(i)||i<=0))throw TypeError(`bitrate must be a positive integer or a quality.`);Wa(e,a);let o=null;return kn.length>0&&(o??=Ga({codec:e,width:n,height:r,bitrate:i,framerate:void 0,...a}),kn.some(t=>t.supports(e,o)))?!0:typeof VideoEncoder>`u`||(n%2==1||r%2==1)&&(e===`avc`||e===`hevc`)||(o??=Ga({codec:e,width:n,height:r,bitrate:i,framerate:void 0,...a,alpha:`discard`}),!(await VideoEncoder.isConfigSupported(o)).supported)?!1:st()?new Promise(async e=>{try{let t=new VideoEncoder({output:()=>{},error:()=>e(!1)});t.configure(o);let i=new Uint8Array(n*r*4),a=new VideoFrame(i,{format:`RGBA`,codedWidth:n,codedHeight:r,timestamp:0});t.encode(a),a.close(),await t.flush(),e(!0)}catch{e(!1)}}):!0},Qa=async(e,t={})=>{let{numberOfChannels:n=2,sampleRate:r=48e3,bitrate:i=128e3,...a}=t;if(!M.includes(e))return!1;if(!Number.isInteger(n)||n<=0)throw TypeError(`numberOfChannels must be a positive integer.`);if(!Number.isInteger(r)||r<=0)throw TypeError(`sampleRate must be a positive integer.`);if(!(i instanceof Ya)&&(!Number.isInteger(i)||i<=0))throw TypeError(`bitrate must be a positive integer.`);qa(e,a);let o=null;return An.length>0&&(o??=Ja({codec:e,numberOfChannels:n,sampleRate:r,bitrate:i,...a}),An.some(t=>t.supports(e,o)))||j.includes(e)?!0:typeof AudioEncoder>`u`?!1:(o??=Ja({codec:e,numberOfChannels:n,sampleRate:r,bitrate:i,...a}),(await AudioEncoder.isConfigSupported(o)).supported===!0)},$a=async(e=M,t)=>{let n=await Promise.all(e.map(e=>Qa(e,t)));return e.filter((e,t)=>n[t])},eo=async(e,t)=>{for(let n of e)if(await Za(n,t))return n;return null};
|
|
136
136
|
/*!
|
|
137
137
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
138
138
|
*
|
|
@@ -140,7 +140,7 @@ let Ha=e=>{if(!e||typeof e!=`object`)throw TypeError(`Encoding config must be an
|
|
|
140
140
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
141
141
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
142
142
|
*/
|
|
143
|
-
var
|
|
143
|
+
var to=class{constructor(){this._connectedTrack=null,this._closingPromise=null,this._closed=!1,this._timestampOffset=0}_ensureValidAdd(){if(!this._connectedTrack)throw Error(`Source is not connected to an output track.`);if(this._connectedTrack.output.state===`canceled`)throw Error(`Output has been canceled.`);if(this._connectedTrack.output.state===`finalizing`||this._connectedTrack.output.state===`finalized`)throw Error(`Output has been finalized.`);if(this._connectedTrack.output.state===`pending`)throw Error(`Output has not started.`);if(this._closed)throw Error(`Source is closed.`)}async _start(){}async _flushAndClose(e){}close(){if(this._closingPromise)return;let e=this._connectedTrack;if(!e)throw Error(`Cannot call close without connecting the source to an output track.`);if(e.output.state===`pending`)throw Error(`Cannot call close before output has been started.`);this._closingPromise=(async()=>{await this._flushAndClose(!1),this._closed=!0,!(e.output.state===`finalizing`||e.output.state===`finalized`)&&e.output._muxer.onTrackClose(e)})()}async _flushOrWaitForOngoingClose(e){return this._closingPromise?this._closingPromise:this._flushAndClose(e)}},no=class extends to{constructor(e){if(super(),this._connectedTrack=null,!A.includes(e))throw TypeError(`Invalid video codec '${e}'. Must be one of: ${A.join(`, `)}.`);this._codec=e}},ro=class extends no{constructor(e){super(e)}add(e,t){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);if(e.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be added.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`meta, when provided, must be an object.`);return this._ensureValidAdd(),this._connectedTrack.output._muxer.addEncodedVideoPacket(this._connectedTrack,e,t)}},io=class{constructor(e,t){this.source=e,this.encodingConfig=t,this.ensureEncoderPromise=null,this.encoderInitialized=!1,this.encoder=null,this.muxer=null,this.lastMultipleOfKeyFrameInterval=-1,this.codedWidth=null,this.codedHeight=null,this.resizeCanvas=null,this.customEncoder=null,this.customEncoderCallSerializer=new rt,this.customEncoderQueueSize=0,this.alphaEncoder=null,this.splitter=null,this.splitterCreationFailed=!1,this.alphaFrameQueue=[],this.error=null,this.errorNeedsNewStack=!0}async add(e,t,n){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.codedWidth!==null&&this.codedHeight!==null){if(e.codedWidth!==this.codedWidth||e.codedHeight!==this.codedHeight){let n=this.encodingConfig.sizeChangeBehavior??`deny`;if(n!==`passThrough`){if(n===`deny`)throw Error(`Video sample size must remain constant. Expected ${this.codedWidth}x${this.codedHeight}, got ${e.codedWidth}x${e.codedHeight}. To allow the sample size to change over time, set \`sizeChangeBehavior\` to a value other than 'strict' in the encoding options.`);{let r=!1;this.resizeCanvas||(typeof document<`u`?(this.resizeCanvas=document.createElement(`canvas`),this.resizeCanvas.width=this.codedWidth,this.resizeCanvas.height=this.codedHeight):this.resizeCanvas=new OffscreenCanvas(this.codedWidth,this.codedHeight),r=!0);let i=this.resizeCanvas.getContext(`2d`,{alpha:st()});b(i),r||(st()?(i.fillStyle=`black`,i.fillRect(0,0,this.codedWidth,this.codedHeight)):i.clearRect(0,0,this.codedWidth,this.codedHeight)),e.drawWithFit(i,{fit:n}),t&&e.close(),e=new F(this.resizeCanvas,{timestamp:e.timestamp,duration:e.duration,rotation:e.rotation}),t=!0}}}}else this.codedWidth=e.codedWidth,this.codedHeight=e.codedHeight;this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(e),this.encoderInitialized||await this.ensureEncoderPromise),b(this.encoderInitialized);let r=this.encodingConfig.keyFrameInterval??5,i=Math.floor(e.timestamp/r),a={...n,keyFrame:n?.keyFrame||r===0||i!==this.lastMultipleOfKeyFrameInterval};if(this.lastMultipleOfKeyFrameInterval=i,this.customEncoder){this.customEncoderQueueSize++;let t=e.clone(),n=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(t,a)).then(()=>this.customEncoderQueueSize--).catch(e=>this.error??=e).finally(()=>{t.close()});this.customEncoderQueueSize>=4&&await n}else{b(this.encoder);let n=e.toVideoFrame();if(!this.alphaEncoder)this.encoder.encode(n,a),n.close();else if(n.format&&!n.format.includes(`A`)||this.splitterCreationFailed)this.alphaFrameQueue.push(null),this.encoder.encode(n,a),n.close();else{let e=n.displayWidth,t=n.displayHeight;if(!this.splitter)try{this.splitter=new ao(e,t)}catch(e){console.error(`Due to an error, only color data will be encoded.`,e),this.splitterCreationFailed=!0,this.alphaFrameQueue.push(null),this.encoder.encode(n,a),n.close()}if(this.splitter){let e=this.splitter.extractColor(n),t=this.splitter.extractAlpha(n);this.alphaFrameQueue.push(t),this.encoder.encode(e,a),e.close(),n.close()}}t&&e.close(),this.encoder.encodeQueueSize>=4&&await new Promise(e=>this.encoder.addEventListener(`dequeue`,e,{once:!0}))}await this.muxer.mutex.currentPromise}finally{t&&e.close()}}ensureEncoder(e){let t=Error();this.ensureEncoderPromise=(async()=>{let n=Ga({width:e.codedWidth,height:e.codedHeight,...this.encodingConfig,framerate:this.source._connectedTrack?.metadata.frameRate});this.encodingConfig.onEncoderConfig?.(n);let r=kn.find(e=>e.supports(this.encodingConfig.codec,n));if(r)this.customEncoder=new r,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=n,this.customEncoder.onPacket=(e,t)=>{if(!(e instanceof P))throw TypeError(`The first argument passed to onPacket must be an EncodedPacket.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`The second argument passed to onPacket must be an object or undefined.`);this.encodingConfig.onEncodedPacket?.(e,t),this.muxer.addEncodedVideoPacket(this.source._connectedTrack,e,t).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})},await this.customEncoder.init();else{if(typeof VideoEncoder>`u`)throw Error(`VideoEncoder is not supported by this browser.`);if(n.alpha=`discard`,this.encodingConfig.alpha===`keep`&&(n.latencyMode=`quality`),(n.width%2==1||n.height%2==1)&&(this.encodingConfig.codec===`avc`||this.encodingConfig.codec===`hevc`))throw Error(`The dimensions ${n.width}x${n.height} are not supported for codec '${this.encodingConfig.codec}'; both width and height must be even numbers. Make sure to round your dimensions to the nearest even number.`);if(!(await VideoEncoder.isConfigSupported(n)).supported)throw Error(`This specific encoder configuration (${n.codec}, ${n.bitrate} bps, ${n.width}x${n.height}, hardware acceleration: ${n.hardwareAcceleration??`no-preference`}) is not supported by this browser. Consider using another codec or changing your video parameters.`);let e=[],r=[],i=0,a=0,o=(e,t,n)=>{let r={};if(t){let e=new Uint8Array(t.byteLength);t.copyTo(e),r.alpha=e}let i=P.fromEncodedChunk(e,r);this.encodingConfig.onEncodedPacket?.(i,n),this.muxer.addEncodedVideoPacket(this.source._connectedTrack,i,n).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})};this.encoder=new VideoEncoder({output:(t,n)=>{if(!this.alphaEncoder){o(t,null,n);return}let s=this.alphaFrameQueue.shift();b(s!==void 0),s?(this.alphaEncoder.encode(s,{keyFrame:t.type===`key`}),a++,s.close(),e.push({chunk:t,meta:n})):a===0?o(t,null,n):(r.push(i+a),e.push({chunk:t,meta:n}))},error:e=>{e.stack=t.stack,this.error??=e}}),this.encoder.configure(n),this.encodingConfig.alpha===`keep`&&(this.alphaEncoder=new VideoEncoder({output:(t,n)=>{a--;let s=e.shift();for(b(s!==void 0),o(s.chunk,t,s.meta),i++;r.length>0&&r[0]===i;){r.shift();let t=e.shift();b(t!==void 0),o(t.chunk,null,t.meta)}},error:e=>{e.stack=t.stack,this.error??=e}}),this.alphaEncoder.configure(n))}b(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}async flushAndClose(e){e||this.checkForEncoderError(),this.customEncoder?(e||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(e||(await this.encoder.flush(),await this.alphaEncoder?.flush()),this.encoder.state!==`closed`&&this.encoder.close(),this.alphaEncoder&&this.alphaEncoder.state!==`closed`&&this.alphaEncoder.close(),this.alphaFrameQueue.forEach(e=>e?.close()),this.splitter?.close()),e||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.errorNeedsNewStack&&(this.error.stack=Error().stack),this.error}},ao=class{constructor(e,t){this.lastFrame=null,typeof OffscreenCanvas<`u`?this.canvas=new OffscreenCanvas(e,t):(this.canvas=document.createElement(`canvas`),this.canvas.width=e,this.canvas.height=t);let n=this.canvas.getContext(`webgl2`,{alpha:!0});if(!n)throw Error(`Couldn't acquire WebGL 2 context.`);this.gl=n,this.colorProgram=this.createColorProgram(),this.alphaProgram=this.createAlphaProgram(),this.vao=this.createVAO(),this.sourceTexture=this.createTexture(),this.alphaResolutionLocation=this.gl.getUniformLocation(this.alphaProgram,`u_resolution`),this.gl.useProgram(this.colorProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.colorProgram,`u_sourceTexture`),0),this.gl.useProgram(this.alphaProgram),this.gl.uniform1i(this.gl.getUniformLocation(this.alphaProgram,`u_sourceTexture`),0)}createVertexShader(){return this.createShader(this.gl.VERTEX_SHADER,`#version 300 es
|
|
144
144
|
in vec2 a_position;
|
|
145
145
|
in vec2 a_texCoord;
|
|
146
146
|
out vec2 v_texCoord;
|
|
@@ -204,7 +204,7 @@ var eo=class{constructor(){this._connectedTrack=null,this._closingPromise=null,t
|
|
|
204
204
|
|
|
205
205
|
fragColor = result;
|
|
206
206
|
}
|
|
207
|
-
`),n=this.gl.createProgram();return this.gl.attachShader(n,e),this.gl.attachShader(n,t),this.gl.linkProgram(n),n}createShader(e,t){let n=this.gl.createShader(e);return this.gl.shaderSource(n,t),this.gl.compileShader(n),this.gl.getShaderParameter(n,this.gl.COMPILE_STATUS)||console.error(`Shader compile error:`,this.gl.getShaderInfoLog(n)),n}createVAO(){let e=this.gl.createVertexArray();this.gl.bindVertexArray(e);let t=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),n=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,n),this.gl.bufferData(this.gl.ARRAY_BUFFER,t,this.gl.STATIC_DRAW);let r=this.gl.getAttribLocation(this.colorProgram,`a_position`),i=this.gl.getAttribLocation(this.colorProgram,`a_texCoord`);return this.gl.enableVertexAttribArray(r),this.gl.vertexAttribPointer(r,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,8),e}createTexture(){let e=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),e}updateTexture(e){this.lastFrame!==e&&((e.displayWidth!==this.canvas.width||e.displayHeight!==this.canvas.height)&&(this.canvas.width=e.displayWidth,this.canvas.height=e.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.sourceTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.lastFrame=e)}extractColor(e){return this.updateTexture(e),this.gl.useProgram(this.colorProgram),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),new VideoFrame(this.canvas,{timestamp:e.timestamp,duration:e.duration??void 0,alpha:`discard`})}extractAlpha(e){this.updateTexture(e),this.gl.useProgram(this.alphaProgram),this.gl.uniform2f(this.alphaResolutionLocation,this.canvas.width,this.canvas.height),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let{width:t,height:n}=this.canvas,r=Math.ceil(t/2)*Math.ceil(n/2),i=t*n+r*2,a=Math.ceil(i/(t*4)),o=new Uint8Array(4*t*a);this.gl.readPixels(0,0,t,a,this.gl.RGBA,this.gl.UNSIGNED_BYTE,o),o=o.subarray(0,i),x(o[t*n]===128),x(o[o.length-1]===128);let s={format:`I420`,codedWidth:t,codedHeight:n,timestamp:e.timestamp,duration:e.duration??void 0,transfer:[o.buffer]};return new VideoFrame(o,s)}close(){this.gl.getExtension(`WEBGL_lose_context`)?.loseContext(),this.gl=null}},ao=class extends to{constructor(e){Ha(e),super(e.codec),this._encoder=new ro(this,e)}add(e,t){if(!(e instanceof Ln))throw TypeError(`videoSample must be a VideoSample.`);return this._encoder.add(e,!1,t)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},oo=class extends eo{constructor(e){if(super(),this._connectedTrack=null,!N.includes(e))throw TypeError(`Invalid audio codec '${e}'. Must be one of: ${N.join(`, `)}.`);this._codec=e}},so=class extends oo{constructor(e){super(e)}add(e,t){if(!(e instanceof F))throw TypeError(`packet must be an EncodedPacket.`);if(e.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be added.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`meta, when provided, must be an object.`);return this._ensureValidAdd(),this._connectedTrack.output._muxer.addEncodedAudioPacket(this._connectedTrack,e,t)}},co=class{constructor(e,t){this.source=e,this.encodingConfig=t,this.ensureEncoderPromise=null,this.encoderInitialized=!1,this.encoder=null,this.muxer=null,this.lastNumberOfChannels=null,this.lastSampleRate=null,this.isPcmEncoder=!1,this.outputSampleSize=null,this.writeOutputValue=null,this.customEncoder=null,this.customEncoderCallSerializer=new tt,this.customEncoderQueueSize=0,this.lastEndSampleIndex=null,this.error=null,this.errorNeedsNewStack=!0}async add(e,t){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.lastNumberOfChannels!==null&&this.lastSampleRate!==null){if(e.numberOfChannels!==this.lastNumberOfChannels||e.sampleRate!==this.lastSampleRate)throw Error(`Audio parameters must remain constant. Expected ${this.lastNumberOfChannels} channels at ${this.lastSampleRate} Hz, got ${e.numberOfChannels} channels at ${e.sampleRate} Hz.`)}else this.lastNumberOfChannels=e.numberOfChannels,this.lastSampleRate=e.sampleRate;this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(e),this.encoderInitialized||await this.ensureEncoderPromise),x(this.encoderInitialized);{let t=Math.round(e.timestamp*e.sampleRate),n=Math.round((e.timestamp+e.duration)*e.sampleRate);if(this.lastEndSampleIndex===null)this.lastEndSampleIndex=n;else{let n=t-this.lastEndSampleIndex;if(n>=64){let t=new Hn({data:new Float32Array(n*e.numberOfChannels),format:`f32-planar`,sampleRate:e.sampleRate,numberOfChannels:e.numberOfChannels,numberOfFrames:n,timestamp:this.lastEndSampleIndex/e.sampleRate});await this.add(t,!0)}this.lastEndSampleIndex+=e.numberOfFrames}}if(this.customEncoder){this.customEncoderQueueSize++;let t=e.clone(),n=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(t)).then(()=>this.customEncoderQueueSize--).catch(e=>this.error??=e).finally(()=>{t.close()});this.customEncoderQueueSize>=4&&await n,await this.muxer.mutex.currentPromise}else if(this.isPcmEncoder)await this.doPcmEncoding(e,t);else{x(this.encoder);let n=e.toAudioData();this.encoder.encode(n),n.close(),t&&e.close(),this.encoder.encodeQueueSize>=4&&await new Promise(e=>this.encoder.addEventListener(`dequeue`,e,{once:!0})),await this.muxer.mutex.currentPromise}}finally{t&&e.close()}}async doPcmEncoding(e,t){x(this.outputSampleSize),x(this.writeOutputValue);let{numberOfChannels:n,numberOfFrames:r,sampleRate:i,timestamp:a}=e,o=2048,s=[];for(let t=0;t<r;t+=o){let r=Math.min(o,e.numberOfFrames-t),i=r*n*this.outputSampleSize,a=new ArrayBuffer(i),c=new DataView(a);s.push({frameCount:r,view:c})}let c=e.allocationSize({planeIndex:0,format:`f32-planar`}),l=new Float32Array(c/Float32Array.BYTES_PER_ELEMENT);for(let t=0;t<n;t++){e.copyTo(l,{planeIndex:t,format:`f32-planar`});for(let e=0;e<s.length;e++){let{frameCount:r,view:i}=s[e];for(let a=0;a<r;a++)this.writeOutputValue(i,(a*n+t)*this.outputSampleSize,l[e*o+a])}}t&&e.close();let u={decoderConfig:{codec:this.encodingConfig.codec,numberOfChannels:n,sampleRate:i}};for(let e=0;e<s.length;e++){let{frameCount:t,view:n}=s[e],r=n.buffer,c=e*o,l=new F(new Uint8Array(r),`key`,a+c/i,t/i);this.encodingConfig.onEncodedPacket?.(l,u),await this.muxer.addEncodedAudioPacket(this.source._connectedTrack,l,u)}}ensureEncoder(e){let t=Error();this.ensureEncoderPromise=(async()=>{let{numberOfChannels:n,sampleRate:r}=e,i=qa({numberOfChannels:n,sampleRate:r,...this.encodingConfig});this.encodingConfig.onEncoderConfig?.(i);let a=On.find(e=>e.supports(this.encodingConfig.codec,i));if(a)this.customEncoder=new a,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=i,this.customEncoder.onPacket=(e,t)=>{if(!(e instanceof F))throw TypeError(`The first argument passed to onPacket must be an EncodedPacket.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`The second argument passed to onPacket must be an object or undefined.`);this.encodingConfig.onEncodedPacket?.(e,t),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,e,t).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})},await this.customEncoder.init();else if(M.includes(this.encodingConfig.codec))this.initPcmEncoder();else{if(typeof AudioEncoder>`u`)throw Error(`AudioEncoder is not supported by this browser.`);if(!(await AudioEncoder.isConfigSupported(i)).supported)throw Error(`This specific encoder configuration (${i.codec}, ${i.bitrate} bps, ${i.numberOfChannels} channels, ${i.sampleRate} Hz) is not supported by this browser. Consider using another codec or changing your audio parameters.`);this.encoder=new AudioEncoder({output:(e,t)=>{if(this.encodingConfig.codec===`aac`&&t?.decoderConfig){let e=!1;if(e=!t.decoderConfig.description||t.decoderConfig.description.byteLength<2?!0:Mt(T(t.decoderConfig.description)).objectType===0,e){let e=Number(S(i.codec.split(`.`)));t.decoderConfig.description=Nt({objectType:e,numberOfChannels:t.decoderConfig.numberOfChannels,sampleRate:t.decoderConfig.sampleRate})}}let n=F.fromEncodedChunk(e);this.encodingConfig.onEncodedPacket?.(n,t),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,n,t).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})},error:e=>{e.stack=t.stack,this.error??=e}}),this.encoder.configure(i)}x(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}initPcmEncoder(){this.isPcmEncoder=!0;let e=this.encodingConfig.codec,{dataType:t,sampleSize:n,littleEndian:r}=Ft(e);switch(this.outputSampleSize=n,n){case 1:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint8(t,A((n+1)*127.5,0,255)):t===`signed`?this.writeOutputValue=(e,t,n)=>{e.setInt8(t,A(Math.round(n*128),-128,127))}:t===`ulaw`?this.writeOutputValue=(e,t,n)=>{let r=A(Math.floor(n*32767),-32768,32767);e.setUint8(t,An(r))}:t===`alaw`?this.writeOutputValue=(e,t,n)=>{let r=A(Math.floor(n*32767),-32768,32767);e.setUint8(t,Mn(r))}:x(!1);break;case 2:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint16(t,A((n+1)*32767.5,0,65535),r):t===`signed`?this.writeOutputValue=(e,t,n)=>e.setInt16(t,A(Math.round(n*32767),-32768,32767),r):x(!1);break;case 3:t===`unsigned`?this.writeOutputValue=(e,t,n)=>Ke(e,t,A((n+1)*8388607.5,0,16777215),r):t===`signed`?this.writeOutputValue=(e,t,n)=>qe(e,t,A(Math.round(n*8388607),-8388608,8388607),r):x(!1);break;case 4:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint32(t,A((n+1)*2147483647.5,0,4294967295),r):t===`signed`?this.writeOutputValue=(e,t,n)=>e.setInt32(t,A(Math.round(n*2147483647),-2147483648,2147483647),r):t===`float`?this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,r):x(!1);break;case 8:t===`float`?this.writeOutputValue=(e,t,n)=>e.setFloat64(t,n,r):x(!1);break;default:Ue(n),x(!1)}}async flushAndClose(e){e||this.checkForEncoderError(),this.customEncoder?(e||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(e||await this.encoder.flush(),this.encoder.state!==`closed`&&this.encoder.close()),e||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.isPcmEncoder?0:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.errorNeedsNewStack&&(this.error.stack=Error().stack),this.error}},lo=class extends oo{constructor(e){Ga(e),super(e.codec),this._encoder=new co(this,e)}add(e){if(!(e instanceof Hn))throw TypeError(`audioSample must be an AudioSample.`);return this._encoder.add(e,!1)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},uo=class extends eo{constructor(e){if(super(),this._connectedTrack=null,!bt.includes(e))throw TypeError(`Invalid subtitle codec '${e}'. Must be one of: ${bt.join(`, `)}.`);this._codec=e}};
|
|
207
|
+
`),n=this.gl.createProgram();return this.gl.attachShader(n,e),this.gl.attachShader(n,t),this.gl.linkProgram(n),n}createShader(e,t){let n=this.gl.createShader(e);return this.gl.shaderSource(n,t),this.gl.compileShader(n),this.gl.getShaderParameter(n,this.gl.COMPILE_STATUS)||console.error(`Shader compile error:`,this.gl.getShaderInfoLog(n)),n}createVAO(){let e=this.gl.createVertexArray();this.gl.bindVertexArray(e);let t=new Float32Array([-1,-1,0,1,1,-1,1,1,-1,1,0,0,1,1,1,0]),n=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,n),this.gl.bufferData(this.gl.ARRAY_BUFFER,t,this.gl.STATIC_DRAW);let r=this.gl.getAttribLocation(this.colorProgram,`a_position`),i=this.gl.getAttribLocation(this.colorProgram,`a_texCoord`);return this.gl.enableVertexAttribArray(r),this.gl.vertexAttribPointer(r,2,this.gl.FLOAT,!1,16,0),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,16,8),e}createTexture(){let e=this.gl.createTexture();return this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),e}updateTexture(e){this.lastFrame!==e&&((e.displayWidth!==this.canvas.width||e.displayHeight!==this.canvas.height)&&(this.canvas.width=e.displayWidth,this.canvas.height=e.displayHeight),this.gl.activeTexture(this.gl.TEXTURE0),this.gl.bindTexture(this.gl.TEXTURE_2D,this.sourceTexture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e),this.lastFrame=e)}extractColor(e){return this.updateTexture(e),this.gl.useProgram(this.colorProgram),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),new VideoFrame(this.canvas,{timestamp:e.timestamp,duration:e.duration??void 0,alpha:`discard`})}extractAlpha(e){this.updateTexture(e),this.gl.useProgram(this.alphaProgram),this.gl.uniform2f(this.alphaResolutionLocation,this.canvas.width,this.canvas.height),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.bindVertexArray(this.vao),this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4);let{width:t,height:n}=this.canvas,r=Math.ceil(t/2)*Math.ceil(n/2),i=t*n+r*2,a=Math.ceil(i/(t*4)),o=new Uint8Array(4*t*a);this.gl.readPixels(0,0,t,a,this.gl.RGBA,this.gl.UNSIGNED_BYTE,o),o=o.subarray(0,i),b(o[t*n]===128),b(o[o.length-1]===128);let s={format:`I420`,codedWidth:t,codedHeight:n,timestamp:e.timestamp,duration:e.duration??void 0,transfer:[o.buffer]};return new VideoFrame(o,s)}close(){this.gl.getExtension(`WEBGL_lose_context`)?.loseContext(),this.gl=null}},oo=class extends no{constructor(e){Ua(e),super(e.codec),this._encoder=new io(this,e)}add(e,t){if(!(e instanceof F))throw TypeError(`videoSample must be a VideoSample.`);return this._encoder.add(e,!1,t)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},so=class extends to{constructor(e){if(super(),this._connectedTrack=null,!M.includes(e))throw TypeError(`Invalid audio codec '${e}'. Must be one of: ${M.join(`, `)}.`);this._codec=e}},co=class extends so{constructor(e){super(e)}add(e,t){if(!(e instanceof P))throw TypeError(`packet must be an EncodedPacket.`);if(e.isMetadataOnly)throw TypeError(`Metadata-only packets cannot be added.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`meta, when provided, must be an object.`);return this._ensureValidAdd(),this._connectedTrack.output._muxer.addEncodedAudioPacket(this._connectedTrack,e,t)}},lo=class{constructor(e,t){this.source=e,this.encodingConfig=t,this.ensureEncoderPromise=null,this.encoderInitialized=!1,this.encoder=null,this.muxer=null,this.lastNumberOfChannels=null,this.lastSampleRate=null,this.isPcmEncoder=!1,this.outputSampleSize=null,this.writeOutputValue=null,this.customEncoder=null,this.customEncoderCallSerializer=new rt,this.customEncoderQueueSize=0,this.lastEndSampleIndex=null,this.error=null,this.errorNeedsNewStack=!0}async add(e,t){try{if(this.checkForEncoderError(),this.source._ensureValidAdd(),this.lastNumberOfChannels!==null&&this.lastSampleRate!==null){if(e.numberOfChannels!==this.lastNumberOfChannels||e.sampleRate!==this.lastSampleRate)throw Error(`Audio parameters must remain constant. Expected ${this.lastNumberOfChannels} channels at ${this.lastSampleRate} Hz, got ${e.numberOfChannels} channels at ${e.sampleRate} Hz.`)}else this.lastNumberOfChannels=e.numberOfChannels,this.lastSampleRate=e.sampleRate;this.encoderInitialized||(this.ensureEncoderPromise||this.ensureEncoder(e),this.encoderInitialized||await this.ensureEncoderPromise),b(this.encoderInitialized);{let t=Math.round(e.timestamp*e.sampleRate),n=Math.round((e.timestamp+e.duration)*e.sampleRate);if(this.lastEndSampleIndex===null)this.lastEndSampleIndex=n;else{let n=t-this.lastEndSampleIndex;if(n>=64){let t=new Un({data:new Float32Array(n*e.numberOfChannels),format:`f32-planar`,sampleRate:e.sampleRate,numberOfChannels:e.numberOfChannels,numberOfFrames:n,timestamp:this.lastEndSampleIndex/e.sampleRate});await this.add(t,!0)}this.lastEndSampleIndex+=e.numberOfFrames}}if(this.customEncoder){this.customEncoderQueueSize++;let t=e.clone(),n=this.customEncoderCallSerializer.call(()=>this.customEncoder.encode(t)).then(()=>this.customEncoderQueueSize--).catch(e=>this.error??=e).finally(()=>{t.close()});this.customEncoderQueueSize>=4&&await n,await this.muxer.mutex.currentPromise}else if(this.isPcmEncoder)await this.doPcmEncoding(e,t);else{b(this.encoder);let n=e.toAudioData();this.encoder.encode(n),n.close(),t&&e.close(),this.encoder.encodeQueueSize>=4&&await new Promise(e=>this.encoder.addEventListener(`dequeue`,e,{once:!0})),await this.muxer.mutex.currentPromise}}finally{t&&e.close()}}async doPcmEncoding(e,t){b(this.outputSampleSize),b(this.writeOutputValue);let{numberOfChannels:n,numberOfFrames:r,sampleRate:i,timestamp:a}=e,o=2048,s=[];for(let t=0;t<r;t+=o){let r=Math.min(o,e.numberOfFrames-t),i=r*n*this.outputSampleSize,a=new ArrayBuffer(i),c=new DataView(a);s.push({frameCount:r,view:c})}let c=e.allocationSize({planeIndex:0,format:`f32-planar`}),l=new Float32Array(c/Float32Array.BYTES_PER_ELEMENT);for(let t=0;t<n;t++){e.copyTo(l,{planeIndex:t,format:`f32-planar`});for(let e=0;e<s.length;e++){let{frameCount:r,view:i}=s[e];for(let a=0;a<r;a++)this.writeOutputValue(i,(a*n+t)*this.outputSampleSize,l[e*o+a])}}t&&e.close();let u={decoderConfig:{codec:this.encodingConfig.codec,numberOfChannels:n,sampleRate:i}};for(let e=0;e<s.length;e++){let{frameCount:t,view:n}=s[e],r=n.buffer,c=e*o,l=new P(new Uint8Array(r),`key`,a+c/i,t/i);this.encodingConfig.onEncodedPacket?.(l,u),await this.muxer.addEncodedAudioPacket(this.source._connectedTrack,l,u)}}ensureEncoder(e){let t=Error();this.ensureEncoderPromise=(async()=>{let{numberOfChannels:n,sampleRate:r}=e,i=Ja({numberOfChannels:n,sampleRate:r,...this.encodingConfig});this.encodingConfig.onEncoderConfig?.(i);let a=An.find(e=>e.supports(this.encodingConfig.codec,i));if(a)this.customEncoder=new a,this.customEncoder.codec=this.encodingConfig.codec,this.customEncoder.config=i,this.customEncoder.onPacket=(e,t)=>{if(!(e instanceof P))throw TypeError(`The first argument passed to onPacket must be an EncodedPacket.`);if(t!==void 0&&(!t||typeof t!=`object`))throw TypeError(`The second argument passed to onPacket must be an object or undefined.`);this.encodingConfig.onEncodedPacket?.(e,t),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,e,t).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})},await this.customEncoder.init();else if(j.includes(this.encodingConfig.codec))this.initPcmEncoder();else{if(typeof AudioEncoder>`u`)throw Error(`AudioEncoder is not supported by this browser.`);if(!(await AudioEncoder.isConfigSupported(i)).supported)throw Error(`This specific encoder configuration (${i.codec}, ${i.bitrate} bps, ${i.numberOfChannels} channels, ${i.sampleRate} Hz) is not supported by this browser. Consider using another codec or changing your audio parameters.`);this.encoder=new AudioEncoder({output:(e,t)=>{if(this.encodingConfig.codec===`aac`&&t?.decoderConfig){let e=!1;if(e=!t.decoderConfig.description||t.decoderConfig.description.byteLength<2?!0:Pt(w(t.decoderConfig.description)).objectType===0,e){let e=Number(x(i.codec.split(`.`)));t.decoderConfig.description=Ft({objectType:e,numberOfChannels:t.decoderConfig.numberOfChannels,sampleRate:t.decoderConfig.sampleRate})}}let n=P.fromEncodedChunk(e);this.encodingConfig.onEncodedPacket?.(n,t),this.muxer.addEncodedAudioPacket(this.source._connectedTrack,n,t).catch(e=>{this.error??=e,this.errorNeedsNewStack=!1})},error:e=>{e.stack=t.stack,this.error??=e}}),this.encoder.configure(i)}b(this.source._connectedTrack),this.muxer=this.source._connectedTrack.output._muxer,this.encoderInitialized=!0})()}initPcmEncoder(){this.isPcmEncoder=!0;let e=this.encodingConfig.codec,{dataType:t,sampleSize:n,littleEndian:r}=Lt(e);switch(this.outputSampleSize=n,n){case 1:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint8(t,k((n+1)*127.5,0,255)):t===`signed`?this.writeOutputValue=(e,t,n)=>{e.setInt8(t,k(Math.round(n*128),-128,127))}:t===`ulaw`?this.writeOutputValue=(e,t,n)=>{let r=k(Math.floor(n*32767),-32768,32767);e.setUint8(t,Mn(r))}:t===`alaw`?this.writeOutputValue=(e,t,n)=>{let r=k(Math.floor(n*32767),-32768,32767);e.setUint8(t,Pn(r))}:b(!1);break;case 2:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint16(t,k((n+1)*32767.5,0,65535),r):t===`signed`?this.writeOutputValue=(e,t,n)=>e.setInt16(t,k(Math.round(n*32767),-32768,32767),r):b(!1);break;case 3:t===`unsigned`?this.writeOutputValue=(e,t,n)=>Je(e,t,k((n+1)*8388607.5,0,16777215),r):t===`signed`?this.writeOutputValue=(e,t,n)=>Ye(e,t,k(Math.round(n*8388607),-8388608,8388607),r):b(!1);break;case 4:t===`unsigned`?this.writeOutputValue=(e,t,n)=>e.setUint32(t,k((n+1)*2147483647.5,0,4294967295),r):t===`signed`?this.writeOutputValue=(e,t,n)=>e.setInt32(t,k(Math.round(n*2147483647),-2147483648,2147483647),r):t===`float`?this.writeOutputValue=(e,t,n)=>e.setFloat32(t,n,r):b(!1);break;case 8:t===`float`?this.writeOutputValue=(e,t,n)=>e.setFloat64(t,n,r):b(!1);break;default:Ge(n),b(!1)}}async flushAndClose(e){e||this.checkForEncoderError(),this.customEncoder?(e||this.customEncoderCallSerializer.call(()=>this.customEncoder.flush()),await this.customEncoderCallSerializer.call(()=>this.customEncoder.close())):this.encoder&&(e||await this.encoder.flush(),this.encoder.state!==`closed`&&this.encoder.close()),e||this.checkForEncoderError()}getQueueSize(){return this.customEncoder?this.customEncoderQueueSize:this.isPcmEncoder?0:this.encoder?.encodeQueueSize??0}checkForEncoderError(){if(this.error)throw this.errorNeedsNewStack&&(this.error.stack=Error().stack),this.error}},uo=class extends so{constructor(e){Ka(e),super(e.codec),this._encoder=new lo(this,e)}add(e){if(!(e instanceof Un))throw TypeError(`audioSample must be an AudioSample.`);return this._encoder.add(e,!1)}_flushAndClose(e){return this._encoder.flushAndClose(e)}},fo=class extends to{constructor(e){if(super(),this._connectedTrack=null,!St.includes(e))throw TypeError(`Invalid subtitle codec '${e}'. Must be one of: ${St.join(`, `)}.`);this._codec=e}};
|
|
208
208
|
/*!
|
|
209
209
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
210
210
|
*
|
|
@@ -212,7 +212,7 @@ var eo=class{constructor(){this._connectedTrack=null,this._closingPromise=null,t
|
|
|
212
212
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
213
213
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
214
214
|
*/
|
|
215
|
-
let
|
|
215
|
+
let po=[`video`,`audio`,`subtitle`],mo=e=>{if(!e||typeof e!=`object`)throw TypeError(`metadata must be an object.`);if(e.languageCode!==void 0&&!et(e.languageCode))throw TypeError(`metadata.languageCode, when provided, must be a three-letter, ISO 639-2/T language code.`);if(e.name!==void 0&&typeof e.name!=`string`)throw TypeError(`metadata.name, when provided, must be a string.`);if(e.disposition!==void 0&&bt(e.disposition),e.maximumPacketCount!==void 0&&(!Number.isInteger(e.maximumPacketCount)||e.maximumPacketCount<0))throw TypeError(`metadata.maximumPacketCount, when provided, must be a non-negative integer.`)};var ho=class{constructor(e){if(this.state=`pending`,this._tracks=[],this._startPromise=null,this._cancelPromise=null,this._finalizePromise=null,this._mutex=new Le,this._metadataTags={},!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(!(e.format instanceof za))throw TypeError(`options.format must be an OutputFormat.`);if(!(e.target instanceof Na))throw TypeError(`options.target must be a Target.`);if(e.target._output)throw Error(`Target is already used for another output.`);e.target._output=this,this.format=e.format,this.target=e.target,this._writer=e.target._createWriter(),this._muxer=e.format._createMuxer(this)}addVideoTrack(e,t={}){if(!(e instanceof no))throw TypeError(`source must be a VideoSource.`);if(mo(t),t.rotation!==void 0&&![0,90,180,270].includes(t.rotation))throw TypeError(`Invalid video rotation: ${t.rotation}. Has to be 0, 90, 180 or 270.`);if(!this.format.supportsVideoRotationMetadata&&t.rotation)throw Error(`${this.format._name} does not support video rotation metadata.`);if(t.frameRate!==void 0&&(!Number.isFinite(t.frameRate)||t.frameRate<=0))throw TypeError(`Invalid video frame rate: ${t.frameRate}. Must be a positive number.`);this._addTrack(`video`,e,t)}addAudioTrack(e,t={}){if(!(e instanceof so))throw TypeError(`source must be an AudioSource.`);mo(t),this._addTrack(`audio`,e,t)}addSubtitleTrack(e,t={}){if(!(e instanceof fo))throw TypeError(`source must be a SubtitleSource.`);mo(t),this._addTrack(`subtitle`,e,t)}setMetadataTags(e){if(vt(e),this.state!==`pending`)throw Error(`Cannot set metadata tags after output has been started or canceled.`);this._metadataTags=e}_addTrack(e,t,n){if(this.state!==`pending`)throw Error(`Cannot add track after output has been started or canceled.`);if(t._connectedTrack)throw Error(`Source is already used for a track.`);let r=this.format.getSupportedTrackCounts(),i=this._tracks.reduce((t,n)=>t+(n.type===e?1:0),0),a=r[e].max;if(i===a)throw Error(a===0?`${this.format._name} does not support ${e} tracks.`:`${this.format._name} does not support more than ${a} ${e} track${a===1?``:`s`}.`);let o=r.total.max;if(this._tracks.length===o)throw Error(`${this.format._name} does not support more than ${o} tracks${o===1?``:`s`} in total.`);let s={id:this._tracks.length+1,output:this,type:e,source:t,metadata:n};if(s.type===`video`){let e=this.format.getSupportedVideoCodecs();if(e.length===0)throw Error(`${this.format._name} does not support video tracks.`+this.format._codecUnsupportedHint(s.source._codec));if(!e.includes(s.source._codec))throw Error(`Codec '${s.source._codec}' cannot be contained within ${this.format._name}. Supported video codecs are: ${e.map(e=>`'${e}'`).join(`, `)}.`+this.format._codecUnsupportedHint(s.source._codec))}else if(s.type===`audio`){let e=this.format.getSupportedAudioCodecs();if(e.length===0)throw Error(`${this.format._name} does not support audio tracks.`+this.format._codecUnsupportedHint(s.source._codec));if(!e.includes(s.source._codec))throw Error(`Codec '${s.source._codec}' cannot be contained within ${this.format._name}. Supported audio codecs are: ${e.map(e=>`'${e}'`).join(`, `)}.`+this.format._codecUnsupportedHint(s.source._codec))}else if(s.type===`subtitle`){let e=this.format.getSupportedSubtitleCodecs();if(e.length===0)throw Error(`${this.format._name} does not support subtitle tracks.`+this.format._codecUnsupportedHint(s.source._codec));if(!e.includes(s.source._codec))throw Error(`Codec '${s.source._codec}' cannot be contained within ${this.format._name}. Supported subtitle codecs are: ${e.map(e=>`'${e}'`).join(`, `)}.`+this.format._codecUnsupportedHint(s.source._codec))}this._tracks.push(s),t._connectedTrack=s}async start(){let e=this.format.getSupportedTrackCounts();for(let t of po){let n=this._tracks.reduce((e,n)=>e+(n.type===t?1:0),0),r=e[t].min;if(n<r)throw Error(r===e[t].max?`${this.format._name} requires exactly ${r} ${t} track${r===1?``:`s`}.`:`${this.format._name} requires at least ${r} ${t} track${r===1?``:`s`}.`)}let t=e.total.min;if(this._tracks.length<t)throw Error(t===e.total.max?`${this.format._name} requires exactly ${t} track${t===1?``:`s`}.`:`${this.format._name} requires at least ${t} track${t===1?``:`s`}.`);if(this.state===`canceled`)throw Error(`Output has been canceled.`);return this._startPromise?(console.warn(`Output has already been started.`),this._startPromise):this._startPromise=(async()=>{this.state=`started`,this._writer.start();let e=await this._mutex.acquire();await this._muxer.start();let t=this._tracks.map(e=>e.source._start());await Promise.all(t),e()})()}getMimeType(){return this._muxer.getMimeType()}async cancel(){if(this._cancelPromise)return console.warn(`Output has already been canceled.`),this._cancelPromise;if(this.state===`finalizing`||this.state===`finalized`){console.warn(`Output has already been finalized.`);return}return this._cancelPromise=(async()=>{this.state=`canceled`;let e=await this._mutex.acquire(),t=this._tracks.map(e=>e.source._flushOrWaitForOngoingClose(!0));await Promise.all(t),await this._writer.close(),e()})()}async finalize(){if(this.state===`pending`)throw Error(`Cannot finalize before starting.`);if(this.state===`canceled`)throw Error(`Cannot finalize after canceling.`);return this._finalizePromise?(console.warn(`Output has already been finalized.`),this._finalizePromise):this._finalizePromise=(async()=>{this.state=`finalizing`;let e=await this._mutex.acquire(),t=this._tracks.map(e=>e.source._flushOrWaitForOngoingClose(!1));await Promise.all(t),await this._muxer.finalize(),await this._writer.flush(),await this._writer.finalize(),this.state=`finalized`,e()})()}};
|
|
216
216
|
/*!
|
|
217
217
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
218
218
|
*
|
|
@@ -220,13 +220,13 @@ let fo=[`video`,`audio`,`subtitle`],po=e=>{if(!e||typeof e!=`object`)throw TypeE
|
|
|
220
220
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
221
221
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
222
222
|
*/
|
|
223
|
-
let
|
|
223
|
+
let go=e=>{if(e!==void 0&&(!e||typeof e!=`object`))throw TypeError(`options.video, when provided, must be an object.`);if(e?.discard!==void 0&&typeof e.discard!=`boolean`)throw TypeError(`options.video.discard, when provided, must be a boolean.`);if(e?.forceTranscode!==void 0&&typeof e.forceTranscode!=`boolean`)throw TypeError(`options.video.forceTranscode, when provided, must be a boolean.`);if(e?.codec!==void 0&&!A.includes(e.codec))throw TypeError(`options.video.codec, when provided, must be one of: ${A.join(`, `)}.`);if(e?.bitrate!==void 0&&!(e.bitrate instanceof Ya)&&(!Number.isInteger(e.bitrate)||e.bitrate<=0))throw TypeError(`options.video.bitrate, when provided, must be a positive integer or a quality.`);if(e?.width!==void 0&&(!Number.isInteger(e.width)||e.width<=0))throw TypeError(`options.video.width, when provided, must be a positive integer.`);if(e?.height!==void 0&&(!Number.isInteger(e.height)||e.height<=0))throw TypeError(`options.video.height, when provided, must be a positive integer.`);if(e?.fit!==void 0&&![`fill`,`contain`,`cover`].includes(e.fit))throw TypeError(`options.video.fit, when provided, must be one of 'fill', 'contain', or 'cover'.`);if(e?.width!==void 0&&e.height!==void 0&&e.fit===void 0)throw TypeError(`When both options.video.width and options.video.height are provided, options.video.fit must also be provided.`);if(e?.rotate!==void 0&&![0,90,180,270].includes(e.rotate))throw TypeError(`options.video.rotate, when provided, must be 0, 90, 180 or 270.`);if(e?.crop!==void 0&&Vn(e.crop,`options.video.`),e?.frameRate!==void 0&&(!Number.isFinite(e.frameRate)||e.frameRate<=0))throw TypeError(`options.video.frameRate, when provided, must be a finite positive number.`);if(e?.alpha!==void 0&&![`discard`,`keep`].includes(e.alpha))throw TypeError(`options.video.alpha, when provided, must be either 'discard' or 'keep'.`);if(e?.keyFrameInterval!==void 0&&(!Number.isFinite(e.keyFrameInterval)||e.keyFrameInterval<0))throw TypeError(`options.video.keyFrameInterval, when provided, must be a non-negative number.`);if(e?.process!==void 0&&typeof e.process!=`function`)throw TypeError(`options.video.process, when provided, must be a function.`);if(e?.processedWidth!==void 0&&(!Number.isInteger(e.processedWidth)||e.processedWidth<=0))throw TypeError(`options.video.processedWidth, when provided, must be a positive integer.`);if(e?.processedHeight!==void 0&&(!Number.isInteger(e.processedHeight)||e.processedHeight<=0))throw TypeError(`options.video.processedHeight, when provided, must be a positive integer.`)},_o=e=>{if(e!==void 0&&(!e||typeof e!=`object`))throw TypeError(`options.audio, when provided, must be an object.`);if(e?.discard!==void 0&&typeof e.discard!=`boolean`)throw TypeError(`options.audio.discard, when provided, must be a boolean.`);if(e?.forceTranscode!==void 0&&typeof e.forceTranscode!=`boolean`)throw TypeError(`options.audio.forceTranscode, when provided, must be a boolean.`);if(e?.codec!==void 0&&!M.includes(e.codec))throw TypeError(`options.audio.codec, when provided, must be one of: ${M.join(`, `)}.`);if(e?.bitrate!==void 0&&!(e.bitrate instanceof Ya)&&(!Number.isInteger(e.bitrate)||e.bitrate<=0))throw TypeError(`options.audio.bitrate, when provided, must be a positive integer or a quality.`);if(e?.numberOfChannels!==void 0&&(!Number.isInteger(e.numberOfChannels)||e.numberOfChannels<=0))throw TypeError(`options.audio.numberOfChannels, when provided, must be a positive integer.`);if(e?.sampleRate!==void 0&&(!Number.isInteger(e.sampleRate)||e.sampleRate<=0))throw TypeError(`options.audio.sampleRate, when provided, must be a positive integer.`);if(e?.process!==void 0&&typeof e.process!=`function`)throw TypeError(`options.audio.process, when provided, must be a function.`);if(e?.processedNumberOfChannels!==void 0&&(!Number.isInteger(e.processedNumberOfChannels)||e.processedNumberOfChannels<=0))throw TypeError(`options.audio.processedNumberOfChannels, when provided, must be a positive integer.`);if(e?.processedSampleRate!==void 0&&(!Number.isInteger(e.processedSampleRate)||e.processedSampleRate<=0))throw TypeError(`options.audio.processedSampleRate, when provided, must be a positive integer.`)},vo=48e3;var yo=class e{static async init(t){let n=new e(t);return await n._init(),n}constructor(e){if(this._addedCounts={video:0,audio:0,subtitle:0},this._totalTrackCount=0,this._trackPromises=[],this._executed=!1,this._synchronizer=new bo,this._totalDuration=null,this._maxTimestamps=new Map,this._canceled=!1,this.onProgress=void 0,this._computeProgress=!1,this._lastProgress=0,this.isValid=!1,this.utilizedTracks=[],this.discardedTracks=[],!e||typeof e!=`object`)throw TypeError(`options must be an object.`);if(!(e.input instanceof Vr))throw TypeError(`options.input must be an Input.`);if(!(e.output instanceof ho))throw TypeError(`options.output must be an Output.`);if(e.output._tracks.length>0||Object.keys(e.output._metadataTags).length>0||e.output.state!==`pending`)throw TypeError(`options.output must be fresh: no tracks or metadata tags added and not started.`);if(typeof e.video!=`function`&&go(e.video),typeof e.audio!=`function`&&_o(e.audio),e.trim!==void 0&&(!e.trim||typeof e.trim!=`object`))throw TypeError(`options.trim, when provided, must be an object.`);if(e.trim?.start!==void 0&&(!Number.isFinite(e.trim.start)||e.trim.start<0))throw TypeError(`options.trim.start, when provided, must be a non-negative number.`);if(e.trim?.end!==void 0&&(!Number.isFinite(e.trim.end)||e.trim.end<0))throw TypeError(`options.trim.end, when provided, must be a non-negative number.`);if(e.trim?.start!==void 0&&e.trim.end!==void 0&&e.trim.start>=e.trim.end)throw TypeError(`options.trim.start must be less than options.trim.end.`);if(e.tags!==void 0&&(typeof e.tags!=`object`||!e.tags)&&typeof e.tags!=`function`)throw TypeError(`options.tags, when provided, must be an object or a function.`);if(typeof e.tags==`object`&&vt(e.tags),e.showWarnings!==void 0&&typeof e.showWarnings!=`boolean`)throw TypeError(`options.showWarnings, when provided, must be a boolean.`);this._options=e,this.input=e.input,this.output=e.output,this._startTimestamp=e.trim?.start??0,this._endTimestamp=e.trim?.end??1/0;let{promise:t,resolve:n}=O();this._started=t,this._start=n}async _init(){let e=await this.input.getTracks(),t=this.output.format.getSupportedTrackCounts(),n=1,r=1;for(let i of e){let e;if(i.isVideoTrack()?this._options.video&&(typeof this._options.video==`function`?(e=await this._options.video(i,n),go(e),n++):e=this._options.video):i.isAudioTrack()?this._options.audio&&(typeof this._options.audio==`function`?(e=await this._options.audio(i,r),_o(e),r++):e=this._options.audio):b(!1),e?.discard){this.discardedTracks.push({track:i,reason:`discarded_by_user`});continue}if(this._totalTrackCount===t.total.max){this.discardedTracks.push({track:i,reason:`max_track_count_reached`});continue}if(this._addedCounts[i.type]===t[i.type].max){this.discardedTracks.push({track:i,reason:`max_track_count_of_type_reached`});continue}i.isVideoTrack()?await this._processVideoTrack(i,e??{}):i.isAudioTrack()&&await this._processAudioTrack(i,e??{})}let i=await this.input.getMetadataTags(),a;if(this._options.tags){let e=typeof this._options.tags==`function`?await this._options.tags(i):this._options.tags;vt(e),a=e}else a=i;let o=(await this.input.getFormat()).mimeType===this.output.format.mimeType,s=i.raw===a.raw;if(i.raw&&s&&!o&&delete a.raw,this.output.setMetadataTags(a),this.isValid=this._totalTrackCount>=t.total.min&&this._addedCounts.video>=t.video.min&&this._addedCounts.audio>=t.audio.min&&this._addedCounts.subtitle>=t.subtitle.min,this._options.showWarnings??!0){let e=[],t=this.discardedTracks.filter(e=>e.reason!==`discarded_by_user`);t.length>0&&e.push(`Some tracks had to be discarded from the conversion:`,t),this.isValid||e.push(`
|
|
224
224
|
|
|
225
225
|
`+this._getInvalidityExplanation().join(``)),e.length>0&&console.warn(...e)}}_getInvalidityExplanation(){let e=[];if(this.discardedTracks.length===0)e.push(`Due to missing tracks, this conversion cannot be executed.`);else{let t=this.discardedTracks.every(e=>e.reason===`discarded_by_user`||e.reason===`no_encodable_target_codec`);if(e.push(`Due to discarded tracks, this conversion cannot be executed.`),t){let t=this.discardedTracks.flatMap(e=>e.reason===`discarded_by_user`?[]:e.track.type===`video`?this.output.format.getSupportedVideoCodecs():e.track.type===`audio`?this.output.format.getSupportedAudioCodecs():this.output.format.getSupportedSubtitleCodecs());t.length===1?e.push(`\nTracks were discarded because your environment is not able to encode '${t[0]}'.`):e.push(`
|
|
226
226
|
Tracks were discarded because your environment is not able to encode any of the following codecs: ${t.map(e=>`'${e}'`).join(`, `)}.`),t.includes(`mp3`)&&e.push(`
|
|
227
227
|
The @mediabunny/mp3-encoder extension package provides support for encoding MP3.`)}else e.push(`
|
|
228
228
|
Check the discardedTracks field for more info.`)}return e}async execute(){if(!this.isValid)throw Error(`Cannot execute this conversion because its output configuration is invalid. Make sure to always check the isValid field before executing a conversion.
|
|
229
|
-
`+this._getInvalidityExplanation().join(``));if(this._executed)throw Error(`Conversion cannot be executed twice.`);if(this._executed=!0,this.onProgress){this._computeProgress=!0,this._totalDuration=Math.min(await this.input.computeDuration()-this._startTimestamp,this._endTimestamp-this._startTimestamp);for(let e of this.utilizedTracks)this._maxTimestamps.set(e.id,0);this.onProgress?.(0)}await this.output.start(),this._start();try{await Promise.all(this._trackPromises)}catch(e){throw this._canceled||this.cancel(),e}this._canceled&&await new Promise(()=>{}),await this.output.finalize(),this._computeProgress&&this.onProgress?.(1)}async cancel(){if(!(this.output.state===`finalizing`||this.output.state===`finalized`)){if(this._canceled){console.warn(`Conversion already canceled.`);return}this._canceled=!0,await this.output.cancel()}}async _processVideoTrack(e,t){let n=e.codec;if(!n){this.discardedTracks.push({track:e,reason:`unknown_source_codec`});return}let r,i=Se(e.rotation+(t.rotate??0)),a=this.output.format.supportsVideoRotationMetadata,[o,s]=i%180==0?[e.codedWidth,e.codedHeight]:[e.codedHeight,e.codedWidth],c=t.crop;c&&zn(c,o,s);let[l,u]=c?[c.width,c.height]:[o,s],d=l,f=u,p=d/f,m=e=>Math.ceil(e/2)*2;t.width!==void 0&&t.height===void 0?(d=m(t.width),f=m(Math.round(d/p))):t.width===void 0&&t.height!==void 0?(f=m(t.height),d=m(Math.round(f*p))):t.width!==void 0&&t.height!==void 0&&(d=m(t.width),f=m(t.height));let h=await e.getFirstTimestamp(),g=!!t.forceTranscode||this._startTimestamp>0||h<0||!!t.frameRate||t.keyFrameInterval!==void 0||t.process!==void 0,_=d!==l||f!==u||i!==0&&(!a||t.process!==void 0)||!!c,v=t.alpha??`discard`,y=this.output.format.getSupportedVideoCodecs();if(!g&&!t.bitrate&&!_&&y.includes(n)&&(!t.codec||t.codec===n)){let t=new no(n);r=t,this._trackPromises.push((async()=>{await this._started;let n=new Zn(e),r={decoderConfig:await e.getDecoderConfig()??void 0},i=Number.isFinite(this._endTimestamp)?await n.getPacket(this._endTimestamp,{metadataOnly:!0})??void 0:void 0;for await(let a of n.packets(void 0,i,{verifyKeyPackets:!0})){if(this._canceled)return;v===`discard`&&(delete a.sideData.alpha,delete a.sideData.alphaByteLength),this._reportProgress(e.id,a.timestamp),await t.add(a,r),this._synchronizer.shouldWait(e.id,a.timestamp)&&await this._synchronizer.wait(a.timestamp)}t.close(),this._synchronizer.closeTrack(e.id)})())}else{if(!await e.canDecode()){this.discardedTracks.push({track:e,reason:`undecodable_source_codec`});return}t.codec&&(y=y.filter(e=>e===t.codec));let n=t.bitrate??Ya,a=await $a(y,{width:t.process&&t.processedWidth?t.processedWidth:d,height:t.process&&t.processedHeight?t.processedHeight:f,bitrate:n});if(!a){this.discardedTracks.push({track:e,reason:`no_encodable_target_codec`});return}let o={codec:a,bitrate:n,keyFrameInterval:t.keyFrameInterval,sizeChangeBehavior:t.fit??`passThrough`,alpha:v},s=new ao(o);if(r=s,!_){let t=new mo({format:new Ba,target:new Pa}),n=new ao(o);t.addVideoTrack(n),await t.start();let r=await new rr(e).getSample(h);if(r)try{await n.add(r),r.close(),await t.finalize()}catch(e){console.info(`Error when probing encoder support. Falling back to rerender path.`,e),_=!0,t.cancel()}else await t.cancel()}_?this._trackPromises.push((async()=>{await this._started;let n=new ir(e,{width:d,height:f,fit:t.fit??`fill`,rotation:i,crop:t.crop,poolSize:1,alpha:v===`keep`}).canvases(this._startTimestamp,this._endTimestamp),r=t.frameRate,a=null,o=null,c=null,l=async n=>{x(a),x(r!==void 0);let i=Math.round((n-o)*r);for(let n=1;n<i;n++){let i=new Ln(a,{timestamp:o+n/r,duration:1/r});await this._registerVideoSample(e,t,s,i),i.close()}};for await(let{canvas:i,timestamp:u,duration:d}of n){if(this._canceled)return;let n=Math.max(u-this._startTimestamp,0);if(c=n+d,r!==void 0){let e=Math.floor(n*r)/r;if(a!==null)if(e<=o){a=i,o=e;continue}else await l(e);n=e}let f=new Ln(i,{timestamp:n,duration:r===void 0?d:1/r});await this._registerVideoSample(e,t,s,f),f.close(),r!==void 0&&(a=i,o=n)}a&&(x(c!==null),x(r!==void 0),await l(Math.floor(c*r)/r)),s.close(),this._synchronizer.closeTrack(e.id)})()):this._trackPromises.push((async()=>{await this._started;let n=new rr(e),r=t.frameRate,i=null,a=null,o=null,c=async n=>{x(i),x(r!==void 0);let o=Math.round((n-a)*r);for(let n=1;n<o;n++)i.setTimestamp(a+n/r),i.setDuration(1/r),await this._registerVideoSample(e,t,s,i);i.close()};for await(let l of n.samples(this._startTimestamp,this._endTimestamp)){if(this._canceled){i?.close();return}let n=Math.max(l.timestamp-this._startTimestamp,0);if(o=n+l.duration,r!==void 0){let e=Math.floor(n*r)/r;if(i!==null)if(e<=a){i.close(),i=l,a=e;continue}else await c(e);n=e,l.setDuration(1/r)}l.setTimestamp(n),await this._registerVideoSample(e,t,s,l),r===void 0?l.close():(i=l,a=n)}i&&(x(o!==null),x(r!==void 0),await c(Math.floor(o*r)/r)),s.close(),this._synchronizer.closeTrack(e.id)})())}this.output.addVideoTrack(r,{frameRate:t.frameRate,languageCode:Qe(e.languageCode)?e.languageCode:void 0,name:e.name??void 0,disposition:e.disposition,rotation:_?0:i}),this._addedCounts.video++,this._totalTrackCount++,this.utilizedTracks.push(e)}async _registerVideoSample(e,t,n,r){if(this._canceled)return;this._reportProgress(e.id,r.timestamp);let i;if(!t.process)i=[r];else{let e=t.process(r);e instanceof Promise&&(e=await e),Array.isArray(e)||(e=e===null?[]:[e]),i=e.map(e=>e instanceof Ln?e:typeof VideoFrame<`u`&&e instanceof VideoFrame?new Ln(e):new Ln(e,{timestamp:r.timestamp,duration:r.duration}))}for(let t of i){if(this._canceled)break;await n.add(t),this._synchronizer.shouldWait(e.id,t.timestamp)&&await this._synchronizer.wait(t.timestamp)}for(let e of i)e!==r&&e.close()}async _processAudioTrack(e,t){let n=e.codec;if(!n){this.discardedTracks.push({track:e,reason:`unknown_source_codec`});return}let r,i=e.numberOfChannels,a=e.sampleRate,o=await e.getFirstTimestamp(),s=t.numberOfChannels??i,c=t.sampleRate??a,l=s!==i||c!==a||this._startTimestamp>0||o<0,u=this.output.format.getSupportedAudioCodecs();if(!t.forceTranscode&&!t.bitrate&&!l&&u.includes(n)&&(!t.codec||t.codec===n)&&!t.process){let t=new so(n);r=t,this._trackPromises.push((async()=>{await this._started;let n=new Zn(e),r={decoderConfig:await e.getDecoderConfig()??void 0},i=Number.isFinite(this._endTimestamp)?await n.getPacket(this._endTimestamp,{metadataOnly:!0})??void 0:void 0;for await(let a of n.packets(void 0,i)){if(this._canceled)return;this._reportProgress(e.id,a.timestamp),await t.add(a,r),this._synchronizer.shouldWait(e.id,a.timestamp)&&await this._synchronizer.wait(a.timestamp)}t.close(),this._synchronizer.closeTrack(e.id)})())}else{if(!await e.canDecode()){this.discardedTracks.push({track:e,reason:`undecodable_source_codec`});return}let n=null;t.codec&&(u=u.filter(e=>e===t.codec));let i=t.bitrate??Ya,a=await Qa(u,{numberOfChannels:t.process&&t.processedNumberOfChannels?t.processedNumberOfChannels:s,sampleRate:t.process&&t.processedSampleRate?t.processedSampleRate:c,bitrate:i});if(!a.some(e=>yt.includes(e))&&u.some(e=>yt.includes(e))&&(s!==2||c!==_o)){let e=(await Qa(u,{numberOfChannels:2,sampleRate:_o,bitrate:i})).find(e=>yt.includes(e));e&&(l=!0,n=e,s=2,c=_o)}else n=a[0]??null;if(n===null){this.discardedTracks.push({track:e,reason:`no_encodable_target_codec`});return}if(l)r=this._resampleAudio(e,t,n,s,c,i);else{let a=new lo({codec:n,bitrate:i});r=a,this._trackPromises.push((async()=>{await this._started;let n=new sr(e);for await(let r of n.samples(void 0,this._endTimestamp)){if(this._canceled)return;await this._registerAudioSample(e,t,a,r),r.close()}a.close(),this._synchronizer.closeTrack(e.id)})())}}this.output.addAudioTrack(r,{languageCode:Qe(e.languageCode)?e.languageCode:void 0,name:e.name??void 0,disposition:e.disposition}),this._addedCounts.audio++,this._totalTrackCount++,this.utilizedTracks.push(e)}async _registerAudioSample(e,t,n,r){if(this._canceled)return;this._reportProgress(e.id,r.timestamp);let i;if(!t.process)i=[r];else{let e=t.process(r);if(e instanceof Promise&&(e=await e),Array.isArray(e)||(e=e===null?[]:[e]),!e.every(e=>e instanceof Hn))throw TypeError(`The audio process function must return an AudioSample, null, or an array of AudioSamples.`);i=e}for(let t of i){if(this._canceled)break;await n.add(t),this._synchronizer.shouldWait(e.id,t.timestamp)&&await this._synchronizer.wait(t.timestamp)}for(let e of i)e!==r&&e.close()}_resampleAudio(e,t,n,r,i,a){let o=new lo({codec:n,bitrate:a});return this._trackPromises.push((async()=>{await this._started;let n=new bo({targetNumberOfChannels:r,targetSampleRate:i,startTime:this._startTimestamp,endTime:this._endTimestamp,onSample:async n=>{await this._registerAudioSample(e,t,o,n),n.close()}}),a=new sr(e).samples(this._startTimestamp,this._endTimestamp);for await(let e of a){if(this._canceled)return;await n.add(e),e.close()}await n.finalize(),o.close(),this._synchronizer.closeTrack(e.id)})()),o}_reportProgress(e,t){if(!this._computeProgress)return;x(this._totalDuration!==null),this._maxTimestamps.set(e,Math.max(t,this._maxTimestamps.get(e)));let n=A(Math.min(...this._maxTimestamps.values())/this._totalDuration,0,1);n!==this._lastProgress&&(this._lastProgress=n,this.onProgress?.(n))}},yo=class{constructor(){this.maxTimestamps=new Map,this.resolvers=[]}computeMinAndMaybeResolve(){let e=1/0;for(let[,t]of this.maxTimestamps)e=Math.min(e,t);for(let t=0;t<this.resolvers.length;t++){let n=this.resolvers[t];n.timestamp-e<5&&(n.resolve(),this.resolvers.splice(t,1),t--)}return e}shouldWait(e,t){return this.maxTimestamps.set(e,Math.max(t,this.maxTimestamps.get(e)??-1/0)),t-this.computeMinAndMaybeResolve()>=5}wait(e){let{promise:t,resolve:n}=k();return this.resolvers.push({timestamp:e,resolve:n}),t}closeTrack(e){this.maxTimestamps.delete(e),this.computeMinAndMaybeResolve()}},bo=class{constructor(e){this.sourceSampleRate=null,this.sourceNumberOfChannels=null,this.targetSampleRate=e.targetSampleRate,this.targetNumberOfChannels=e.targetNumberOfChannels,this.startTime=e.startTime,this.endTime=e.endTime,this.onSample=e.onSample,this.bufferSizeInFrames=Math.floor(this.targetSampleRate*5),this.bufferSizeInSamples=this.bufferSizeInFrames*this.targetNumberOfChannels,this.outputBuffer=new Float32Array(this.bufferSizeInSamples),this.bufferStartFrame=0,this.maxWrittenFrame=-1}doChannelMixerSetup(){x(this.sourceNumberOfChannels!==null);let e=this.sourceNumberOfChannels,t=this.targetNumberOfChannels;e===1&&t===2?this.channelMixer=(t,n)=>t[n*e]:e===1&&t===4?this.channelMixer=(t,n,r)=>t[n*e]*+(r<2):e===1&&t===6?this.channelMixer=(t,n,r)=>t[n*e]*+(r===2):e===2&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return .5*(t[r]+t[r+1])}:e===2&&t===4||e===2&&t===6?this.channelMixer=(t,n,r)=>t[n*e+r]*+(r<2):e===4&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return .25*(t[r]+t[r+1]+t[r+2]+t[r+3])}:e===4&&t===2?this.channelMixer=(t,n,r)=>{let i=n*e;return .5*(t[i+r]+t[i+r+2])}:e===4&&t===6?this.channelMixer=(t,n,r)=>{let i=n*e;return r<2?t[i+r]:r===2||r===3?0:t[i+r-2]}:e===6&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return Math.SQRT1_2*(t[r]+t[r+1])+t[r+2]+.5*(t[r+4]+t[r+5])}:e===6&&t===2?this.channelMixer=(t,n,r)=>{let i=n*e;return t[i+r]+Math.SQRT1_2*(t[i+2]+t[i+r+4])}:e===6&&t===4?this.channelMixer=(t,n,r)=>{let i=n*e;return r<2?t[i+r]+Math.SQRT1_2*t[i+2]:t[i+r+2]}:this.channelMixer=(t,n,r)=>r<e?t[n*e+r]:0}ensureTempBufferSize(e){let t=this.tempSourceBuffer.length;for(;t<e;)t*=2;if(t!==this.tempSourceBuffer.length){let e=new Float32Array(t);e.set(this.tempSourceBuffer),this.tempSourceBuffer=e}}async add(e){this.sourceSampleRate===null&&(this.sourceSampleRate=e.sampleRate,this.sourceNumberOfChannels=e.numberOfChannels,this.tempSourceBuffer=new Float32Array(this.sourceSampleRate*this.sourceNumberOfChannels),this.doChannelMixerSetup());let t=e.numberOfFrames*e.numberOfChannels;this.ensureTempBufferSize(t);let n=e.allocationSize({planeIndex:0,format:`f32`}),r=new Float32Array(this.tempSourceBuffer.buffer,0,n/4);e.copyTo(r,{planeIndex:0,format:`f32`});let i=e.timestamp-this.startTime,a=e.numberOfFrames/this.sourceSampleRate,o=Math.min(i+a,this.endTime-this.startTime),s=Math.floor(i*this.targetSampleRate),c=Math.ceil(o*this.targetSampleRate);for(let t=s;t<c;t++){if(t<this.bufferStartFrame)continue;for(;t>=this.bufferStartFrame+this.bufferSizeInFrames;)await this.finalizeCurrentBuffer(),this.bufferStartFrame+=this.bufferSizeInFrames;let n=t-this.bufferStartFrame;x(n<this.bufferSizeInFrames);let a=(t/this.targetSampleRate-i)*this.sourceSampleRate,o=Math.floor(a),s=Math.ceil(a),c=a-o;for(let t=0;t<this.targetNumberOfChannels;t++){let i=0,a=0;o>=0&&o<e.numberOfFrames&&(i=this.channelMixer(r,o,t)),s>=0&&s<e.numberOfFrames&&(a=this.channelMixer(r,s,t));let l=i+c*(a-i),u=n*this.targetNumberOfChannels+t;this.outputBuffer[u]+=l}this.maxWrittenFrame=Math.max(this.maxWrittenFrame,n)}}async finalizeCurrentBuffer(){if(this.maxWrittenFrame<0)return;let e=(this.maxWrittenFrame+1)*this.targetNumberOfChannels,t=new Float32Array(e);t.set(this.outputBuffer.subarray(0,e));let n=this.bufferStartFrame/this.targetSampleRate,r=new Hn({format:`f32`,sampleRate:this.targetSampleRate,numberOfChannels:this.targetNumberOfChannels,timestamp:n,data:t});await this.onSample(r),this.outputBuffer.fill(0),this.maxWrittenFrame=-1}finalize(){return this.finalizeCurrentBuffer()}};async function xo(e){try{let t=new Pr(e),n=new Br({formats:[Ar],source:t});if(typeof n.computeDuration!=`function`)throw Error(`computeDuration method is not available`);let r=await n.computeDuration();if(!r)throw Error(`Duration is missing from computeDuration`);if(r<=0)throw Error(`Invalid duration: must be greater than 0`);return r}catch{return So(e)}}function So(e){return new Promise((t,n)=>{let r=document.createElement(`video`),i=URL.createObjectURL(e),a=()=>{URL.revokeObjectURL(i)};r.addEventListener(`loadedmetadata`,()=>{a();let e=r.duration;if(!Number.isFinite(e)||e<=0){n(Error(`Invalid video duration`));return}t(e)}),r.addEventListener(`error`,()=>{a(),n(Error(`Failed to load video metadata`))}),r.src=i,r.load()})}function Co(e){let t=e.getVideoTracks();if(t.length===0)return!1;let n=t[0];return`displaySurface`in n.getSettings()||n.label.toLowerCase().includes(`screen`)||n.label.toLowerCase().includes(`display`)}let wo=null;function To(){if(wo)return wo;if(typeof Blob>`u`||typeof URL>`u`)throw Error(`Blob and URL APIs are required for worker loading`);let e=new Blob([`// ../../node_modules/mediabunny/dist/modules/src/misc.js
|
|
229
|
+
`+this._getInvalidityExplanation().join(``));if(this._executed)throw Error(`Conversion cannot be executed twice.`);if(this._executed=!0,this.onProgress){this._computeProgress=!0,this._totalDuration=Math.min(await this.input.computeDuration()-this._startTimestamp,this._endTimestamp-this._startTimestamp);for(let e of this.utilizedTracks)this._maxTimestamps.set(e.id,0);this.onProgress?.(0)}await this.output.start(),this._start();try{await Promise.all(this._trackPromises)}catch(e){throw this._canceled||this.cancel(),e}this._canceled&&await new Promise(()=>{}),await this.output.finalize(),this._computeProgress&&this.onProgress?.(1)}async cancel(){if(!(this.output.state===`finalizing`||this.output.state===`finalized`)){if(this._canceled){console.warn(`Conversion already canceled.`);return}this._canceled=!0,await this.output.cancel()}}async _processVideoTrack(e,t){let n=e.codec;if(!n){this.discardedTracks.push({track:e,reason:`unknown_source_codec`});return}let r,i=we(e.rotation+(t.rotate??0)),a=this.output.format.supportsVideoRotationMetadata,[o,s]=i%180==0?[e.codedWidth,e.codedHeight]:[e.codedHeight,e.codedWidth],c=t.crop;c&&Bn(c,o,s);let[l,u]=c?[c.width,c.height]:[o,s],d=l,f=u,p=d/f,m=e=>Math.ceil(e/2)*2;t.width!==void 0&&t.height===void 0?(d=m(t.width),f=m(Math.round(d/p))):t.width===void 0&&t.height!==void 0?(f=m(t.height),d=m(Math.round(f*p))):t.width!==void 0&&t.height!==void 0&&(d=m(t.width),f=m(t.height));let h=await e.getFirstTimestamp(),g=!!t.forceTranscode||this._startTimestamp>0||h<0||!!t.frameRate||t.keyFrameInterval!==void 0||t.process!==void 0,_=d!==l||f!==u||i!==0&&(!a||t.process!==void 0)||!!c,v=t.alpha??`discard`,ee=this.output.format.getSupportedVideoCodecs();if(!g&&!t.bitrate&&!_&&ee.includes(n)&&(!t.codec||t.codec===n)){let t=new ro(n);r=t,this._trackPromises.push((async()=>{await this._started;let n=new Qn(e),r={decoderConfig:await e.getDecoderConfig()??void 0},i=Number.isFinite(this._endTimestamp)?await n.getPacket(this._endTimestamp,{metadataOnly:!0})??void 0:void 0;for await(let a of n.packets(void 0,i,{verifyKeyPackets:!0})){if(this._canceled)return;v===`discard`&&(delete a.sideData.alpha,delete a.sideData.alphaByteLength),this._reportProgress(e.id,a.timestamp),await t.add(a,r),this._synchronizer.shouldWait(e.id,a.timestamp)&&await this._synchronizer.wait(a.timestamp)}t.close(),this._synchronizer.closeTrack(e.id)})())}else{if(!await e.canDecode()){this.discardedTracks.push({track:e,reason:`undecodable_source_codec`});return}t.codec&&(ee=ee.filter(e=>e===t.codec));let n=t.bitrate??Xa,a=await eo(ee,{width:t.process&&t.processedWidth?t.processedWidth:d,height:t.process&&t.processedHeight?t.processedHeight:f,bitrate:n});if(!a){this.discardedTracks.push({track:e,reason:`no_encodable_target_codec`});return}let o={codec:a,bitrate:n,keyFrameInterval:t.keyFrameInterval,sizeChangeBehavior:t.fit??`passThrough`,alpha:v},s=new oo(o);if(r=s,!_){let t=new ho({format:new Va,target:new Fa}),n=new oo(o);t.addVideoTrack(n),await t.start();let r=await new ir(e).getSample(h);if(r)try{await n.add(r),r.close(),await t.finalize()}catch(e){console.info(`Error when probing encoder support. Falling back to rerender path.`,e),_=!0,t.cancel()}else await t.cancel()}_?this._trackPromises.push((async()=>{await this._started;let n=new ar(e,{width:d,height:f,fit:t.fit??`fill`,rotation:i,crop:t.crop,poolSize:1,alpha:v===`keep`}).canvases(this._startTimestamp,this._endTimestamp),r=t.frameRate,a=null,o=null,c=null,l=async n=>{b(a),b(r!==void 0);let i=Math.round((n-o)*r);for(let n=1;n<i;n++){let i=new F(a,{timestamp:o+n/r,duration:1/r});await this._registerVideoSample(e,t,s,i),i.close()}};for await(let{canvas:i,timestamp:u,duration:d}of n){if(this._canceled)return;let n=Math.max(u-this._startTimestamp,0);if(c=n+d,r!==void 0){let e=Math.floor(n*r)/r;if(a!==null)if(e<=o){a=i,o=e;continue}else await l(e);n=e}let f=new F(i,{timestamp:n,duration:r===void 0?d:1/r});await this._registerVideoSample(e,t,s,f),f.close(),r!==void 0&&(a=i,o=n)}a&&(b(c!==null),b(r!==void 0),await l(Math.floor(c*r)/r)),s.close(),this._synchronizer.closeTrack(e.id)})()):this._trackPromises.push((async()=>{await this._started;let n=new ir(e),r=t.frameRate,i=null,a=null,o=null,c=async n=>{b(i),b(r!==void 0);let o=Math.round((n-a)*r);for(let n=1;n<o;n++)i.setTimestamp(a+n/r),i.setDuration(1/r),await this._registerVideoSample(e,t,s,i);i.close()};for await(let l of n.samples(this._startTimestamp,this._endTimestamp)){if(this._canceled){i?.close();return}let n=Math.max(l.timestamp-this._startTimestamp,0);if(o=n+l.duration,r!==void 0){let e=Math.floor(n*r)/r;if(i!==null)if(e<=a){i.close(),i=l,a=e;continue}else await c(e);n=e,l.setDuration(1/r)}l.setTimestamp(n),await this._registerVideoSample(e,t,s,l),r===void 0?l.close():(i=l,a=n)}i&&(b(o!==null),b(r!==void 0),await c(Math.floor(o*r)/r)),s.close(),this._synchronizer.closeTrack(e.id)})())}this.output.addVideoTrack(r,{frameRate:t.frameRate,languageCode:et(e.languageCode)?e.languageCode:void 0,name:e.name??void 0,disposition:e.disposition,rotation:_?0:i}),this._addedCounts.video++,this._totalTrackCount++,this.utilizedTracks.push(e)}async _registerVideoSample(e,t,n,r){if(this._canceled)return;this._reportProgress(e.id,r.timestamp);let i;if(!t.process)i=[r];else{let e=t.process(r);e instanceof Promise&&(e=await e),Array.isArray(e)||(e=e===null?[]:[e]),i=e.map(e=>e instanceof F?e:typeof VideoFrame<`u`&&e instanceof VideoFrame?new F(e):new F(e,{timestamp:r.timestamp,duration:r.duration}))}for(let t of i){if(this._canceled)break;await n.add(t),this._synchronizer.shouldWait(e.id,t.timestamp)&&await this._synchronizer.wait(t.timestamp)}for(let e of i)e!==r&&e.close()}async _processAudioTrack(e,t){let n=e.codec;if(!n){this.discardedTracks.push({track:e,reason:`unknown_source_codec`});return}let r,i=e.numberOfChannels,a=e.sampleRate,o=await e.getFirstTimestamp(),s=t.numberOfChannels??i,c=t.sampleRate??a,l=s!==i||c!==a||this._startTimestamp>0||o<0,u=this.output.format.getSupportedAudioCodecs();if(!t.forceTranscode&&!t.bitrate&&!l&&u.includes(n)&&(!t.codec||t.codec===n)&&!t.process){let t=new co(n);r=t,this._trackPromises.push((async()=>{await this._started;let n=new Qn(e),r={decoderConfig:await e.getDecoderConfig()??void 0},i=Number.isFinite(this._endTimestamp)?await n.getPacket(this._endTimestamp,{metadataOnly:!0})??void 0:void 0;for await(let a of n.packets(void 0,i)){if(this._canceled)return;this._reportProgress(e.id,a.timestamp),await t.add(a,r),this._synchronizer.shouldWait(e.id,a.timestamp)&&await this._synchronizer.wait(a.timestamp)}t.close(),this._synchronizer.closeTrack(e.id)})())}else{if(!await e.canDecode()){this.discardedTracks.push({track:e,reason:`undecodable_source_codec`});return}let n=null;t.codec&&(u=u.filter(e=>e===t.codec));let i=t.bitrate??Xa,a=await $a(u,{numberOfChannels:t.process&&t.processedNumberOfChannels?t.processedNumberOfChannels:s,sampleRate:t.process&&t.processedSampleRate?t.processedSampleRate:c,bitrate:i});if(!a.some(e=>xt.includes(e))&&u.some(e=>xt.includes(e))&&(s!==2||c!==vo)){let e=(await $a(u,{numberOfChannels:2,sampleRate:vo,bitrate:i})).find(e=>xt.includes(e));e&&(l=!0,n=e,s=2,c=vo)}else n=a[0]??null;if(n===null){this.discardedTracks.push({track:e,reason:`no_encodable_target_codec`});return}if(l)r=this._resampleAudio(e,t,n,s,c,i);else{let a=new uo({codec:n,bitrate:i});r=a,this._trackPromises.push((async()=>{await this._started;let n=new cr(e);for await(let r of n.samples(void 0,this._endTimestamp)){if(this._canceled)return;await this._registerAudioSample(e,t,a,r),r.close()}a.close(),this._synchronizer.closeTrack(e.id)})())}}this.output.addAudioTrack(r,{languageCode:et(e.languageCode)?e.languageCode:void 0,name:e.name??void 0,disposition:e.disposition}),this._addedCounts.audio++,this._totalTrackCount++,this.utilizedTracks.push(e)}async _registerAudioSample(e,t,n,r){if(this._canceled)return;this._reportProgress(e.id,r.timestamp);let i;if(!t.process)i=[r];else{let e=t.process(r);if(e instanceof Promise&&(e=await e),Array.isArray(e)||(e=e===null?[]:[e]),!e.every(e=>e instanceof Un))throw TypeError(`The audio process function must return an AudioSample, null, or an array of AudioSamples.`);i=e}for(let t of i){if(this._canceled)break;await n.add(t),this._synchronizer.shouldWait(e.id,t.timestamp)&&await this._synchronizer.wait(t.timestamp)}for(let e of i)e!==r&&e.close()}_resampleAudio(e,t,n,r,i,a){let o=new uo({codec:n,bitrate:a});return this._trackPromises.push((async()=>{await this._started;let n=new xo({targetNumberOfChannels:r,targetSampleRate:i,startTime:this._startTimestamp,endTime:this._endTimestamp,onSample:async n=>{await this._registerAudioSample(e,t,o,n),n.close()}}),a=new cr(e).samples(this._startTimestamp,this._endTimestamp);for await(let e of a){if(this._canceled)return;await n.add(e),e.close()}await n.finalize(),o.close(),this._synchronizer.closeTrack(e.id)})()),o}_reportProgress(e,t){if(!this._computeProgress)return;b(this._totalDuration!==null),this._maxTimestamps.set(e,Math.max(t,this._maxTimestamps.get(e)));let n=k(Math.min(...this._maxTimestamps.values())/this._totalDuration,0,1);n!==this._lastProgress&&(this._lastProgress=n,this.onProgress?.(n))}},bo=class{constructor(){this.maxTimestamps=new Map,this.resolvers=[]}computeMinAndMaybeResolve(){let e=1/0;for(let[,t]of this.maxTimestamps)e=Math.min(e,t);for(let t=0;t<this.resolvers.length;t++){let n=this.resolvers[t];n.timestamp-e<5&&(n.resolve(),this.resolvers.splice(t,1),t--)}return e}shouldWait(e,t){return this.maxTimestamps.set(e,Math.max(t,this.maxTimestamps.get(e)??-1/0)),t-this.computeMinAndMaybeResolve()>=5}wait(e){let{promise:t,resolve:n}=O();return this.resolvers.push({timestamp:e,resolve:n}),t}closeTrack(e){this.maxTimestamps.delete(e),this.computeMinAndMaybeResolve()}},xo=class{constructor(e){this.sourceSampleRate=null,this.sourceNumberOfChannels=null,this.targetSampleRate=e.targetSampleRate,this.targetNumberOfChannels=e.targetNumberOfChannels,this.startTime=e.startTime,this.endTime=e.endTime,this.onSample=e.onSample,this.bufferSizeInFrames=Math.floor(this.targetSampleRate*5),this.bufferSizeInSamples=this.bufferSizeInFrames*this.targetNumberOfChannels,this.outputBuffer=new Float32Array(this.bufferSizeInSamples),this.bufferStartFrame=0,this.maxWrittenFrame=-1}doChannelMixerSetup(){b(this.sourceNumberOfChannels!==null);let e=this.sourceNumberOfChannels,t=this.targetNumberOfChannels;e===1&&t===2?this.channelMixer=(t,n)=>t[n*e]:e===1&&t===4?this.channelMixer=(t,n,r)=>t[n*e]*+(r<2):e===1&&t===6?this.channelMixer=(t,n,r)=>t[n*e]*+(r===2):e===2&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return .5*(t[r]+t[r+1])}:e===2&&t===4||e===2&&t===6?this.channelMixer=(t,n,r)=>t[n*e+r]*+(r<2):e===4&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return .25*(t[r]+t[r+1]+t[r+2]+t[r+3])}:e===4&&t===2?this.channelMixer=(t,n,r)=>{let i=n*e;return .5*(t[i+r]+t[i+r+2])}:e===4&&t===6?this.channelMixer=(t,n,r)=>{let i=n*e;return r<2?t[i+r]:r===2||r===3?0:t[i+r-2]}:e===6&&t===1?this.channelMixer=(t,n)=>{let r=n*e;return Math.SQRT1_2*(t[r]+t[r+1])+t[r+2]+.5*(t[r+4]+t[r+5])}:e===6&&t===2?this.channelMixer=(t,n,r)=>{let i=n*e;return t[i+r]+Math.SQRT1_2*(t[i+2]+t[i+r+4])}:e===6&&t===4?this.channelMixer=(t,n,r)=>{let i=n*e;return r<2?t[i+r]+Math.SQRT1_2*t[i+2]:t[i+r+2]}:this.channelMixer=(t,n,r)=>r<e?t[n*e+r]:0}ensureTempBufferSize(e){let t=this.tempSourceBuffer.length;for(;t<e;)t*=2;if(t!==this.tempSourceBuffer.length){let e=new Float32Array(t);e.set(this.tempSourceBuffer),this.tempSourceBuffer=e}}async add(e){this.sourceSampleRate===null&&(this.sourceSampleRate=e.sampleRate,this.sourceNumberOfChannels=e.numberOfChannels,this.tempSourceBuffer=new Float32Array(this.sourceSampleRate*this.sourceNumberOfChannels),this.doChannelMixerSetup());let t=e.numberOfFrames*e.numberOfChannels;this.ensureTempBufferSize(t);let n=e.allocationSize({planeIndex:0,format:`f32`}),r=new Float32Array(this.tempSourceBuffer.buffer,0,n/4);e.copyTo(r,{planeIndex:0,format:`f32`});let i=e.timestamp-this.startTime,a=e.numberOfFrames/this.sourceSampleRate,o=Math.min(i+a,this.endTime-this.startTime),s=Math.floor(i*this.targetSampleRate),c=Math.ceil(o*this.targetSampleRate);for(let t=s;t<c;t++){if(t<this.bufferStartFrame)continue;for(;t>=this.bufferStartFrame+this.bufferSizeInFrames;)await this.finalizeCurrentBuffer(),this.bufferStartFrame+=this.bufferSizeInFrames;let n=t-this.bufferStartFrame;b(n<this.bufferSizeInFrames);let a=(t/this.targetSampleRate-i)*this.sourceSampleRate,o=Math.floor(a),s=Math.ceil(a),c=a-o;for(let t=0;t<this.targetNumberOfChannels;t++){let i=0,a=0;o>=0&&o<e.numberOfFrames&&(i=this.channelMixer(r,o,t)),s>=0&&s<e.numberOfFrames&&(a=this.channelMixer(r,s,t));let l=i+c*(a-i),u=n*this.targetNumberOfChannels+t;this.outputBuffer[u]+=l}this.maxWrittenFrame=Math.max(this.maxWrittenFrame,n)}}async finalizeCurrentBuffer(){if(this.maxWrittenFrame<0)return;let e=(this.maxWrittenFrame+1)*this.targetNumberOfChannels,t=new Float32Array(e);t.set(this.outputBuffer.subarray(0,e));let n=this.bufferStartFrame/this.targetSampleRate,r=new Un({format:`f32`,sampleRate:this.targetSampleRate,numberOfChannels:this.targetNumberOfChannels,timestamp:n,data:t});await this.onSample(r),this.outputBuffer.fill(0),this.maxWrittenFrame=-1}finalize(){return this.finalizeCurrentBuffer()}};async function So(e){try{let t=new Fr(e),n=new Vr({formats:[jr],source:t});if(typeof n.computeDuration!=`function`)throw Error(`computeDuration method is not available`);let r=await n.computeDuration();if(!r)throw Error(`Duration is missing from computeDuration`);if(r<=0)throw Error(`Invalid duration: must be greater than 0`);return r}catch{return Co(e)}}function Co(e){return new Promise((t,n)=>{let r=document.createElement(`video`),i=URL.createObjectURL(e),a=()=>{URL.revokeObjectURL(i)};r.addEventListener(`loadedmetadata`,()=>{a();let e=r.duration;if(!Number.isFinite(e)||e<=0){n(Error(`Invalid video duration`));return}t(e)}),r.addEventListener(`error`,()=>{a(),n(Error(`Failed to load video metadata`))}),r.src=i,r.load()})}function wo(e){let t=e.getVideoTracks();if(t.length===0)return!1;let n=t[0];return`displaySurface`in n.getSettings()||n.label.toLowerCase().includes(`screen`)||n.label.toLowerCase().includes(`display`)}let To=null;function Eo(){if(To)return To;if(typeof Blob>`u`||typeof URL>`u`)throw Error(`Blob and URL APIs are required for worker loading`);let e=new Blob([`// ../../node_modules/mediabunny/dist/modules/src/misc.js
|
|
230
230
|
/*!
|
|
231
231
|
* Copyright (c) 2025-present, Vanilagy and contributors
|
|
232
232
|
*
|
|
@@ -7162,6 +7162,14 @@ class Output {
|
|
|
7162
7162
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
7163
7163
|
*/
|
|
7164
7164
|
|
|
7165
|
+
// src/core/utils/error-handler.ts
|
|
7166
|
+
function extractErrorMessage(error) {
|
|
7167
|
+
if (error instanceof Error) {
|
|
7168
|
+
return error.message;
|
|
7169
|
+
}
|
|
7170
|
+
return String(error);
|
|
7171
|
+
}
|
|
7172
|
+
|
|
7165
7173
|
// src/core/utils/logger.ts
|
|
7166
7174
|
function isDebugEnabled() {
|
|
7167
7175
|
const globalAny = globalThis;
|
|
@@ -7257,8 +7265,35 @@ var logger = {
|
|
|
7257
7265
|
}
|
|
7258
7266
|
};
|
|
7259
7267
|
|
|
7268
|
+
// src/core/utils/validation.ts
|
|
7269
|
+
function requireNonNull(value, message) {
|
|
7270
|
+
if (value === null || value === undefined) {
|
|
7271
|
+
throw new Error(message);
|
|
7272
|
+
}
|
|
7273
|
+
return value;
|
|
7274
|
+
}
|
|
7275
|
+
function requireDefined(value, message) {
|
|
7276
|
+
if (value === undefined) {
|
|
7277
|
+
throw new Error(message);
|
|
7278
|
+
}
|
|
7279
|
+
return value;
|
|
7280
|
+
}
|
|
7281
|
+
function requireInitialized(value, componentName) {
|
|
7282
|
+
if (value === null || value === undefined) {
|
|
7283
|
+
throw new Error(\`\${componentName} is not initialized\`);
|
|
7284
|
+
}
|
|
7285
|
+
return value;
|
|
7286
|
+
}
|
|
7287
|
+
|
|
7260
7288
|
// src/core/processor/worker/recorder-worker.ts
|
|
7261
7289
|
var CHUNK_SIZE = 16 * 1024 * 1024;
|
|
7290
|
+
var OVERLAY_BACKGROUND_OPACITY = 0.6;
|
|
7291
|
+
var OVERLAY_PADDING = 16;
|
|
7292
|
+
var OVERLAY_TEXT_COLOR = "#ffffff";
|
|
7293
|
+
var OVERLAY_FONT_SIZE = 16;
|
|
7294
|
+
var OVERLAY_FONT_FAMILY = "Arial, sans-serif";
|
|
7295
|
+
var OVERLAY_MIN_WIDTH = 200;
|
|
7296
|
+
var OVERLAY_MIN_HEIGHT = 50;
|
|
7262
7297
|
|
|
7263
7298
|
class RecorderWorker {
|
|
7264
7299
|
output = null;
|
|
@@ -7281,8 +7316,18 @@ class RecorderWorker {
|
|
|
7281
7316
|
bufferUpdateInterval = null;
|
|
7282
7317
|
pausedDuration = 0;
|
|
7283
7318
|
pauseStartedAt = null;
|
|
7319
|
+
overlayConfig = null;
|
|
7320
|
+
overlayCanvas = null;
|
|
7321
|
+
compositionCanvas = null;
|
|
7322
|
+
compositionCtx = null;
|
|
7323
|
+
hiddenIntervals = [];
|
|
7324
|
+
currentHiddenIntervalStart = null;
|
|
7325
|
+
recordingStartTime = 0;
|
|
7326
|
+
pendingVisibilityUpdates = [];
|
|
7327
|
+
isScreenCapture = false;
|
|
7328
|
+
driftOffset = 0;
|
|
7284
7329
|
constructor() {
|
|
7285
|
-
self.addEventListener("message", this.handleMessage
|
|
7330
|
+
self.addEventListener("message", this.handleMessage);
|
|
7286
7331
|
}
|
|
7287
7332
|
formatFileSize(bytes2) {
|
|
7288
7333
|
if (bytes2 === 0) {
|
|
@@ -7294,59 +7339,78 @@ class RecorderWorker {
|
|
|
7294
7339
|
const size = Math.round(bytes2 / base ** index * 100) / 100;
|
|
7295
7340
|
return \`\${size} \${units[index]}\`;
|
|
7296
7341
|
}
|
|
7297
|
-
|
|
7298
|
-
|
|
7299
|
-
|
|
7300
|
-
|
|
7301
|
-
|
|
7302
|
-
|
|
7342
|
+
shouldIgnoreMessage() {
|
|
7343
|
+
return this.isStopping || this.isFinalized;
|
|
7344
|
+
}
|
|
7345
|
+
handleAsyncOperation(operation, context) {
|
|
7346
|
+
operation.catch((error) => {
|
|
7347
|
+
logger.error(\`[RecorderWorker] Error in \${context}:\`, error);
|
|
7348
|
+
this.sendError(error);
|
|
7303
7349
|
});
|
|
7304
|
-
switch (message.type) {
|
|
7305
|
-
case "start":
|
|
7306
|
-
if (this.isStopping || this.isFinalized) {
|
|
7307
|
-
logger.debug("[RecorderWorker] start ignored (stopping/finalized)");
|
|
7308
|
-
return;
|
|
7309
|
-
}
|
|
7310
|
-
logger.debug("[RecorderWorker] Starting recording", {
|
|
7311
|
-
hasVideoStream: !!message.videoStream,
|
|
7312
|
-
hasAudioStream: !!message.audioStream
|
|
7313
|
-
});
|
|
7314
|
-
this.handleStart(message.videoStream, message.audioStream, message.config).catch((error) => {
|
|
7315
|
-
logger.error("[RecorderWorker] Error in handleStart:", error);
|
|
7316
|
-
this.sendError(error);
|
|
7317
|
-
});
|
|
7318
|
-
break;
|
|
7319
|
-
case "pause":
|
|
7320
|
-
this.handlePause();
|
|
7321
|
-
break;
|
|
7322
|
-
case "resume":
|
|
7323
|
-
this.handleResume();
|
|
7324
|
-
break;
|
|
7325
|
-
case "stop":
|
|
7326
|
-
if (this.isStopping || this.isFinalized) {
|
|
7327
|
-
logger.debug("[RecorderWorker] stop ignored (stopping/finalized)");
|
|
7328
|
-
return;
|
|
7329
|
-
}
|
|
7330
|
-
this.handleStop().catch((error) => {
|
|
7331
|
-
this.sendError(error);
|
|
7332
|
-
});
|
|
7333
|
-
break;
|
|
7334
|
-
case "toggleMute":
|
|
7335
|
-
this.handleToggleMute();
|
|
7336
|
-
break;
|
|
7337
|
-
case "switchSource":
|
|
7338
|
-
this.handleSwitchSource(message.videoStream).catch((error) => {
|
|
7339
|
-
this.sendError(error);
|
|
7340
|
-
});
|
|
7341
|
-
break;
|
|
7342
|
-
case "updateFps":
|
|
7343
|
-
this.handleUpdateFps(message.fps);
|
|
7344
|
-
break;
|
|
7345
|
-
default:
|
|
7346
|
-
this.sendError(new Error(\`Unknown message type: \${message.type}\`));
|
|
7347
|
-
}
|
|
7348
7350
|
}
|
|
7349
|
-
|
|
7351
|
+
handleMessage = (event) => {
|
|
7352
|
+
const message = event.data;
|
|
7353
|
+
logger.debug("[RecorderWorker] Received message:", { type: message.type });
|
|
7354
|
+
if (message.type === "start") {
|
|
7355
|
+
if (this.shouldIgnoreMessage()) {
|
|
7356
|
+
logger.debug("[RecorderWorker] start ignored (stopping/finalized)");
|
|
7357
|
+
return;
|
|
7358
|
+
}
|
|
7359
|
+
this.handleAsyncOperation(this.handleStart(message.videoStream, message.audioStream, message.config, message.overlayConfig), "handleStart");
|
|
7360
|
+
return;
|
|
7361
|
+
}
|
|
7362
|
+
if (message.type === "pause") {
|
|
7363
|
+
this.handlePause();
|
|
7364
|
+
return;
|
|
7365
|
+
}
|
|
7366
|
+
if (message.type === "resume") {
|
|
7367
|
+
this.handleResume();
|
|
7368
|
+
return;
|
|
7369
|
+
}
|
|
7370
|
+
if (message.type === "stop") {
|
|
7371
|
+
if (this.shouldIgnoreMessage()) {
|
|
7372
|
+
logger.debug("[RecorderWorker] stop ignored (stopping/finalized)");
|
|
7373
|
+
return;
|
|
7374
|
+
}
|
|
7375
|
+
this.handleAsyncOperation(this.handleStop(), "handleStop");
|
|
7376
|
+
return;
|
|
7377
|
+
}
|
|
7378
|
+
if (message.type === "toggleMute") {
|
|
7379
|
+
this.handleToggleMute();
|
|
7380
|
+
return;
|
|
7381
|
+
}
|
|
7382
|
+
if (message.type === "switchSource") {
|
|
7383
|
+
this.handleAsyncOperation(this.handleSwitchSource(message.videoStream), "handleSwitchSource");
|
|
7384
|
+
return;
|
|
7385
|
+
}
|
|
7386
|
+
if (message.type === "updateFps") {
|
|
7387
|
+
this.handleUpdateFps(message.fps);
|
|
7388
|
+
return;
|
|
7389
|
+
}
|
|
7390
|
+
if (message.type === "updateVisibility") {
|
|
7391
|
+
this.handleUpdateVisibility(message.isHidden, message.timestamp);
|
|
7392
|
+
return;
|
|
7393
|
+
}
|
|
7394
|
+
if (message.type === "updateSourceType") {
|
|
7395
|
+
this.handleUpdateSourceType(message.isScreenCapture);
|
|
7396
|
+
return;
|
|
7397
|
+
}
|
|
7398
|
+
this.sendError(new Error(\`Unknown message type: \${message.type}\`));
|
|
7399
|
+
};
|
|
7400
|
+
async handleStart(videoStream, audioStream, config, overlayConfig) {
|
|
7401
|
+
requireDefined(config, "Transcode config is required");
|
|
7402
|
+
if (config.width <= 0 || config.height <= 0) {
|
|
7403
|
+
throw new Error("Video dimensions must be greater than zero");
|
|
7404
|
+
}
|
|
7405
|
+
if (config.fps <= 0) {
|
|
7406
|
+
throw new Error("Frame rate must be greater than zero");
|
|
7407
|
+
}
|
|
7408
|
+
if (config.bitrate <= 0) {
|
|
7409
|
+
throw new Error("Bitrate must be greater than zero");
|
|
7410
|
+
}
|
|
7411
|
+
if (config.keyFrameInterval <= 0) {
|
|
7412
|
+
throw new Error("Key frame interval must be greater than zero");
|
|
7413
|
+
}
|
|
7350
7414
|
logger.debug("[RecorderWorker] handleStart called", {
|
|
7351
7415
|
hasVideoStream: !!videoStream,
|
|
7352
7416
|
hasAudioStream: !!audioStream,
|
|
@@ -7355,7 +7419,9 @@ class RecorderWorker {
|
|
|
7355
7419
|
height: config.height,
|
|
7356
7420
|
fps: config.fps,
|
|
7357
7421
|
bitrate: config.bitrate
|
|
7358
|
-
}
|
|
7422
|
+
},
|
|
7423
|
+
hasOverlayConfig: !!overlayConfig,
|
|
7424
|
+
overlayConfig
|
|
7359
7425
|
});
|
|
7360
7426
|
this.isStopping = false;
|
|
7361
7427
|
this.isFinalized = false;
|
|
@@ -7373,6 +7439,19 @@ class RecorderWorker {
|
|
|
7373
7439
|
this.frameCount = 0;
|
|
7374
7440
|
this.pausedDuration = 0;
|
|
7375
7441
|
this.pauseStartedAt = null;
|
|
7442
|
+
this.overlayConfig = overlayConfig ? { enabled: overlayConfig.enabled, text: overlayConfig.text } : null;
|
|
7443
|
+
this.overlayCanvas = null;
|
|
7444
|
+
this.hiddenIntervals = [];
|
|
7445
|
+
this.currentHiddenIntervalStart = null;
|
|
7446
|
+
this.recordingStartTime = overlayConfig?.recordingStartTime !== undefined ? overlayConfig.recordingStartTime / 1000 : performance.now() / 1000;
|
|
7447
|
+
this.pendingVisibilityUpdates = [];
|
|
7448
|
+
const logData = {
|
|
7449
|
+
hasOverlayConfig: !!this.overlayConfig,
|
|
7450
|
+
overlayEnabled: this.overlayConfig?.enabled,
|
|
7451
|
+
overlayText: this.overlayConfig?.text,
|
|
7452
|
+
recordingStartTime: this.recordingStartTime
|
|
7453
|
+
};
|
|
7454
|
+
logger.debug("[RecorderWorker] Overlay config initialized", logData);
|
|
7376
7455
|
const writable = new WritableStream({
|
|
7377
7456
|
write: (chunk) => {
|
|
7378
7457
|
this.sendChunk(chunk.data, chunk.position);
|
|
@@ -7401,6 +7480,9 @@ class RecorderWorker {
|
|
|
7401
7480
|
this.setupVideoProcessing(videoStream);
|
|
7402
7481
|
}
|
|
7403
7482
|
if (audioStream && config.audioBitrate && config.audioCodec) {
|
|
7483
|
+
if (config.audioBitrate <= 0) {
|
|
7484
|
+
throw new Error("Audio bitrate must be greater than zero");
|
|
7485
|
+
}
|
|
7404
7486
|
this.audioSource = new AudioSampleSource({
|
|
7405
7487
|
codec: config.audioCodec,
|
|
7406
7488
|
bitrate: config.audioBitrate
|
|
@@ -7451,50 +7533,260 @@ class RecorderWorker {
|
|
|
7451
7533
|
if (pausedResult.done) {
|
|
7452
7534
|
return false;
|
|
7453
7535
|
}
|
|
7454
|
-
pausedResult.value
|
|
7536
|
+
if (pausedResult.value) {
|
|
7537
|
+
pausedResult.value.close();
|
|
7538
|
+
}
|
|
7455
7539
|
return true;
|
|
7456
7540
|
}
|
|
7457
7541
|
calculateVideoFrameTimestamp(videoFrame) {
|
|
7458
|
-
|
|
7542
|
+
requireDefined(this.frameRate, "Frame rate must be set");
|
|
7543
|
+
if (this.frameRate <= 0) {
|
|
7544
|
+
throw new Error("Frame rate must be greater than zero");
|
|
7545
|
+
}
|
|
7546
|
+
const rawTs = typeof videoFrame.timestamp === "number" && videoFrame.timestamp !== null ? videoFrame.timestamp / 1e6 : performance.now() / 1000;
|
|
7459
7547
|
if (this.baseVideoTimestamp === null) {
|
|
7460
7548
|
this.baseVideoTimestamp = rawTs;
|
|
7549
|
+
const logData = {
|
|
7550
|
+
baseVideoTimestamp: this.baseVideoTimestamp,
|
|
7551
|
+
recordingStartTime: this.recordingStartTime,
|
|
7552
|
+
difference: this.baseVideoTimestamp - this.recordingStartTime,
|
|
7553
|
+
pendingUpdates: this.pendingVisibilityUpdates.length
|
|
7554
|
+
};
|
|
7555
|
+
logger.debug("[RecorderWorker] baseVideoTimestamp set", logData);
|
|
7556
|
+
for (const update of this.pendingVisibilityUpdates) {
|
|
7557
|
+
this.processVisibilityUpdate(update.isHidden, update.timestamp);
|
|
7558
|
+
}
|
|
7559
|
+
this.pendingVisibilityUpdates = [];
|
|
7560
|
+
}
|
|
7561
|
+
requireNonNull(this.baseVideoTimestamp, "Base video timestamp must be set");
|
|
7562
|
+
if (this.frameCount === 0 && this.lastVideoTimestamp > 0) {
|
|
7563
|
+
const originalBase = this.baseVideoTimestamp;
|
|
7564
|
+
const offset = rawTs - originalBase;
|
|
7565
|
+
this.baseVideoTimestamp = rawTs - this.lastVideoTimestamp;
|
|
7566
|
+
const frameTimestamp2 = this.lastVideoTimestamp;
|
|
7567
|
+
logger.debug("[RecorderWorker] First frame after source switch", {
|
|
7568
|
+
rawTs,
|
|
7569
|
+
originalBase,
|
|
7570
|
+
offset,
|
|
7571
|
+
adjustedBaseVideoTimestamp: this.baseVideoTimestamp,
|
|
7572
|
+
continuationTimestamp: this.lastVideoTimestamp,
|
|
7573
|
+
frameTimestamp: frameTimestamp2,
|
|
7574
|
+
isScreenCapture: this.isScreenCapture
|
|
7575
|
+
});
|
|
7576
|
+
return frameTimestamp2;
|
|
7461
7577
|
}
|
|
7462
|
-
const normalizedTs = rawTs -
|
|
7463
|
-
const prevTs = this.lastVideoTimestamp
|
|
7578
|
+
const normalizedTs = rawTs - this.baseVideoTimestamp - this.pausedDuration;
|
|
7579
|
+
const prevTs = this.lastVideoTimestamp > 0 ? this.lastVideoTimestamp : 0;
|
|
7464
7580
|
const frameTimestamp = normalizedTs >= prevTs ? normalizedTs : prevTs + 1 / this.frameRate;
|
|
7581
|
+
if (frameTimestamp < 0) {
|
|
7582
|
+
logger.warn("[RecorderWorker] Negative frame timestamp detected, clamping to zero", { frameTimestamp, normalizedTs, prevTs });
|
|
7583
|
+
return 0;
|
|
7584
|
+
}
|
|
7465
7585
|
if (this.lastVideoTimestamp === 0) {
|
|
7466
7586
|
this.lastVideoTimestamp = frameTimestamp;
|
|
7467
7587
|
}
|
|
7588
|
+
logger.debug("[RecorderWorker] Frame timestamp calculation", {
|
|
7589
|
+
rawTs,
|
|
7590
|
+
baseVideoTimestamp: this.baseVideoTimestamp,
|
|
7591
|
+
normalizedTs,
|
|
7592
|
+
prevTs,
|
|
7593
|
+
frameTimestamp,
|
|
7594
|
+
lastVideoTimestamp: this.lastVideoTimestamp,
|
|
7595
|
+
isScreenCapture: this.isScreenCapture,
|
|
7596
|
+
frameCount: this.frameCount
|
|
7597
|
+
});
|
|
7468
7598
|
return frameTimestamp;
|
|
7469
7599
|
}
|
|
7470
|
-
|
|
7471
|
-
|
|
7600
|
+
createOverlayCanvas(text) {
|
|
7601
|
+
requireDefined(text, "Overlay text is required");
|
|
7602
|
+
const canvas = new OffscreenCanvas(1, 1);
|
|
7603
|
+
const ctx = requireNonNull(canvas.getContext("2d"), "Failed to get OffscreenCanvas context");
|
|
7604
|
+
ctx.font = \`\${OVERLAY_FONT_SIZE}px \${OVERLAY_FONT_FAMILY}\`;
|
|
7605
|
+
const textMetrics = ctx.measureText(text);
|
|
7606
|
+
const textWidth = textMetrics.width;
|
|
7607
|
+
const textHeight = OVERLAY_FONT_SIZE;
|
|
7608
|
+
const overlayWidth = Math.max(OVERLAY_MIN_WIDTH, textWidth + OVERLAY_PADDING * 2);
|
|
7609
|
+
const overlayHeight = Math.max(OVERLAY_MIN_HEIGHT, textHeight + OVERLAY_PADDING * 2);
|
|
7610
|
+
canvas.width = overlayWidth;
|
|
7611
|
+
canvas.height = overlayHeight;
|
|
7612
|
+
const r = 20;
|
|
7613
|
+
const g = 20;
|
|
7614
|
+
const b = 20;
|
|
7615
|
+
const borderRadius = 50;
|
|
7616
|
+
ctx.fillStyle = \`rgba(\${r}, \${g}, \${b}, \${OVERLAY_BACKGROUND_OPACITY})\`;
|
|
7617
|
+
ctx.beginPath();
|
|
7618
|
+
ctx.roundRect(0, 0, overlayWidth, overlayHeight, borderRadius);
|
|
7619
|
+
ctx.fill();
|
|
7620
|
+
ctx.fillStyle = OVERLAY_TEXT_COLOR;
|
|
7621
|
+
ctx.font = \`\${OVERLAY_FONT_SIZE}px \${OVERLAY_FONT_FAMILY}\`;
|
|
7622
|
+
ctx.textBaseline = "middle";
|
|
7623
|
+
ctx.textAlign = "center";
|
|
7624
|
+
const textX = overlayWidth / 2;
|
|
7625
|
+
const textY = overlayHeight / 2;
|
|
7626
|
+
ctx.fillText(text, textX, textY);
|
|
7627
|
+
return canvas;
|
|
7628
|
+
}
|
|
7629
|
+
getOverlayPosition(overlayWidth, videoWidth) {
|
|
7630
|
+
const padding = OVERLAY_PADDING;
|
|
7631
|
+
return {
|
|
7632
|
+
x: videoWidth - overlayWidth - padding,
|
|
7633
|
+
y: padding
|
|
7634
|
+
};
|
|
7635
|
+
}
|
|
7636
|
+
shouldApplyOverlay(timestamp) {
|
|
7637
|
+
if (!this.overlayConfig?.enabled) {
|
|
7638
|
+
return false;
|
|
7639
|
+
}
|
|
7640
|
+
if (this.isScreenCapture) {
|
|
7641
|
+
return false;
|
|
7642
|
+
}
|
|
7643
|
+
const completedIntervalMatch = this.hiddenIntervals.some((interval) => timestamp >= interval.start && timestamp <= interval.end);
|
|
7644
|
+
const currentIntervalMatch = this.currentHiddenIntervalStart !== null && timestamp >= this.currentHiddenIntervalStart;
|
|
7645
|
+
const shouldApply = completedIntervalMatch || currentIntervalMatch;
|
|
7646
|
+
if (this.frameCount % 90 === 0) {
|
|
7647
|
+
logger.debug("[RecorderWorker] Overlay check", {
|
|
7648
|
+
timestamp,
|
|
7649
|
+
shouldApply,
|
|
7650
|
+
frameCount: this.frameCount,
|
|
7651
|
+
intervalsCount: this.hiddenIntervals.length
|
|
7652
|
+
});
|
|
7653
|
+
}
|
|
7654
|
+
return shouldApply;
|
|
7655
|
+
}
|
|
7656
|
+
handleUpdateVisibility(isHidden, timestamp) {
|
|
7657
|
+
if (this.baseVideoTimestamp === null) {
|
|
7658
|
+
this.pendingVisibilityUpdates.push({ isHidden, timestamp });
|
|
7472
7659
|
return;
|
|
7473
7660
|
}
|
|
7661
|
+
this.processVisibilityUpdate(isHidden, timestamp);
|
|
7662
|
+
}
|
|
7663
|
+
processVisibilityUpdate(isHidden, timestamp) {
|
|
7664
|
+
const timestampSeconds = timestamp / 1000;
|
|
7665
|
+
const normalizedTimestamp = timestampSeconds - this.recordingStartTime - this.pausedDuration;
|
|
7666
|
+
if (isHidden) {
|
|
7667
|
+
if (this.currentHiddenIntervalStart === null) {
|
|
7668
|
+
this.currentHiddenIntervalStart = Math.max(0, normalizedTimestamp);
|
|
7669
|
+
logger.debug("[RecorderWorker] Started hidden interval", {
|
|
7670
|
+
start: this.currentHiddenIntervalStart
|
|
7671
|
+
});
|
|
7672
|
+
}
|
|
7673
|
+
} else if (this.currentHiddenIntervalStart !== null) {
|
|
7674
|
+
const endTimestamp = Math.max(0, normalizedTimestamp);
|
|
7675
|
+
if (endTimestamp > this.currentHiddenIntervalStart) {
|
|
7676
|
+
const interval = {
|
|
7677
|
+
start: this.currentHiddenIntervalStart,
|
|
7678
|
+
end: endTimestamp
|
|
7679
|
+
};
|
|
7680
|
+
this.hiddenIntervals.push(interval);
|
|
7681
|
+
logger.debug("[RecorderWorker] Completed hidden interval", {
|
|
7682
|
+
interval,
|
|
7683
|
+
duration: endTimestamp - this.currentHiddenIntervalStart,
|
|
7684
|
+
totalIntervals: this.hiddenIntervals.length
|
|
7685
|
+
});
|
|
7686
|
+
} else {
|
|
7687
|
+
logger.warn("[RecorderWorker] Invalid interval (end <= start), discarding");
|
|
7688
|
+
}
|
|
7689
|
+
this.currentHiddenIntervalStart = null;
|
|
7690
|
+
}
|
|
7691
|
+
}
|
|
7692
|
+
async processVideoFrame(videoFrame) {
|
|
7693
|
+
const videoSource = requireInitialized(this.videoSource, "Video source");
|
|
7694
|
+
const config = requireInitialized(this.config, "Transcode config");
|
|
7474
7695
|
const frameTimestamp = this.calculateVideoFrameTimestamp(videoFrame);
|
|
7696
|
+
requireDefined(this.frameRate, "Frame rate must be set");
|
|
7697
|
+
if (this.frameRate <= 0) {
|
|
7698
|
+
throw new Error("Frame rate must be greater than zero");
|
|
7699
|
+
}
|
|
7475
7700
|
const frameDuration = 1 / this.frameRate;
|
|
7476
|
-
|
|
7477
|
-
|
|
7701
|
+
let frameToProcess = videoFrame;
|
|
7702
|
+
let imageBitmap = null;
|
|
7703
|
+
if (this.shouldApplyOverlay(frameTimestamp)) {
|
|
7704
|
+
const width = videoFrame.displayWidth;
|
|
7705
|
+
const height = videoFrame.displayHeight;
|
|
7706
|
+
if (width <= 0 || height <= 0) {
|
|
7707
|
+
logger.warn("[RecorderWorker] Invalid video frame dimensions, skipping overlay", { width, height });
|
|
7708
|
+
} else if (this.overlayConfig) {
|
|
7709
|
+
if (!this.overlayCanvas) {
|
|
7710
|
+
this.overlayCanvas = this.createOverlayCanvas(this.overlayConfig.text);
|
|
7711
|
+
logger.debug("[RecorderWorker] Overlay canvas created", {
|
|
7712
|
+
overlayWidth: this.overlayCanvas.width,
|
|
7713
|
+
overlayHeight: this.overlayCanvas.height
|
|
7714
|
+
});
|
|
7715
|
+
}
|
|
7716
|
+
if (this.overlayCanvas) {
|
|
7717
|
+
if (!(this.compositionCanvas && this.compositionCtx) || this.compositionCanvas.width !== width || this.compositionCanvas.height !== height) {
|
|
7718
|
+
this.compositionCanvas = new OffscreenCanvas(width, height);
|
|
7719
|
+
this.compositionCtx = requireNonNull(this.compositionCanvas.getContext("2d"), "Failed to get composition canvas context");
|
|
7720
|
+
}
|
|
7721
|
+
requireNonNull(this.compositionCtx, "Composition context must be available");
|
|
7722
|
+
this.compositionCtx.clearRect(0, 0, width, height);
|
|
7723
|
+
this.compositionCtx.drawImage(videoFrame, 0, 0, width, height);
|
|
7724
|
+
const position = this.getOverlayPosition(this.overlayCanvas.width, width);
|
|
7725
|
+
this.compositionCtx.drawImage(this.overlayCanvas, position.x, position.y);
|
|
7726
|
+
imageBitmap = this.compositionCanvas.transferToImageBitmap();
|
|
7727
|
+
const frameInit = {};
|
|
7728
|
+
if (typeof videoFrame.timestamp === "number" && videoFrame.timestamp !== null) {
|
|
7729
|
+
frameInit.timestamp = videoFrame.timestamp;
|
|
7730
|
+
}
|
|
7731
|
+
if (typeof videoFrame.duration === "number" && videoFrame.duration !== null) {
|
|
7732
|
+
frameInit.duration = videoFrame.duration;
|
|
7733
|
+
}
|
|
7734
|
+
frameToProcess = new VideoFrame(imageBitmap, frameInit);
|
|
7735
|
+
}
|
|
7736
|
+
}
|
|
7737
|
+
}
|
|
7738
|
+
const keyFrameInterval = config.keyFrameInterval > 0 ? config.keyFrameInterval : 5;
|
|
7739
|
+
const isKeyFrame = this.frameCount % keyFrameInterval === 0;
|
|
7740
|
+
const maxLead = 0.05;
|
|
7741
|
+
const maxLag = 0.1;
|
|
7742
|
+
const targetAudio = this.lastAudioTimestamp;
|
|
7743
|
+
let adjustedTimestamp = frameTimestamp + this.driftOffset;
|
|
7744
|
+
if (adjustedTimestamp - targetAudio > maxLead) {
|
|
7745
|
+
adjustedTimestamp = targetAudio + maxLead;
|
|
7746
|
+
} else if (targetAudio - adjustedTimestamp > maxLag) {
|
|
7747
|
+
adjustedTimestamp = targetAudio - maxLag;
|
|
7748
|
+
}
|
|
7749
|
+
const monotonicTimestamp = this.lastVideoTimestamp + frameDuration;
|
|
7750
|
+
const finalTimestamp = adjustedTimestamp >= monotonicTimestamp ? adjustedTimestamp : monotonicTimestamp;
|
|
7751
|
+
this.driftOffset *= 0.5;
|
|
7752
|
+
const sample = new VideoSample(frameToProcess, {
|
|
7753
|
+
timestamp: finalTimestamp,
|
|
7478
7754
|
duration: frameDuration
|
|
7479
7755
|
});
|
|
7480
|
-
const
|
|
7481
|
-
|
|
7482
|
-
await this.videoSource.add(sample, isKeyFrame ? { keyFrame: true } : undefined);
|
|
7483
|
-
this.frameCount += 1;
|
|
7484
|
-
this.lastVideoTimestamp = frameTimestamp;
|
|
7485
|
-
} catch (error) {
|
|
7486
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
7756
|
+
const addError = await videoSource.add(sample, isKeyFrame ? { keyFrame: true } : undefined).then(() => null).catch((error) => {
|
|
7757
|
+
const errorMessage = extractErrorMessage(error);
|
|
7487
7758
|
this.sendError(new Error(\`Failed to add video frame: \${errorMessage}\`));
|
|
7488
|
-
|
|
7489
|
-
|
|
7490
|
-
|
|
7759
|
+
return error;
|
|
7760
|
+
});
|
|
7761
|
+
sample.close();
|
|
7762
|
+
if (!addError) {
|
|
7763
|
+
this.frameCount += 1;
|
|
7764
|
+
this.lastVideoTimestamp = finalTimestamp;
|
|
7765
|
+
if (this.frameCount % 90 === 0 && this.audioProcessingActive) {
|
|
7766
|
+
const avDrift = this.lastAudioTimestamp - this.lastVideoTimestamp;
|
|
7767
|
+
logger.debug("[RecorderWorker] AV drift metrics", {
|
|
7768
|
+
frameCount: this.frameCount,
|
|
7769
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
7770
|
+
lastVideoTimestamp: this.lastVideoTimestamp,
|
|
7771
|
+
avDrift,
|
|
7772
|
+
isScreenCapture: this.isScreenCapture
|
|
7773
|
+
});
|
|
7774
|
+
}
|
|
7775
|
+
}
|
|
7776
|
+
if (imageBitmap) {
|
|
7777
|
+
imageBitmap.close();
|
|
7778
|
+
imageBitmap = null;
|
|
7491
7779
|
}
|
|
7780
|
+
if (frameToProcess !== videoFrame) {
|
|
7781
|
+
frameToProcess.close();
|
|
7782
|
+
}
|
|
7783
|
+
videoFrame.close();
|
|
7492
7784
|
}
|
|
7493
7785
|
async processVideoFrames() {
|
|
7494
7786
|
if (!(this.videoProcessor && this.videoSource)) {
|
|
7495
7787
|
return;
|
|
7496
7788
|
}
|
|
7497
|
-
while (this.videoProcessingActive) {
|
|
7789
|
+
while (this.videoProcessingActive && !this.isStopping) {
|
|
7498
7790
|
if (this.isPaused) {
|
|
7499
7791
|
const shouldContinue = await this.handlePausedVideoFrame();
|
|
7500
7792
|
if (!shouldContinue) {
|
|
@@ -7510,15 +7802,30 @@ class RecorderWorker {
|
|
|
7510
7802
|
if (!videoFrame) {
|
|
7511
7803
|
continue;
|
|
7512
7804
|
}
|
|
7513
|
-
await this.processVideoFrame(videoFrame)
|
|
7805
|
+
await this.processVideoFrame(videoFrame).catch((error) => {
|
|
7806
|
+
const errorMessage = extractErrorMessage(error);
|
|
7807
|
+
logger.error("[RecorderWorker] Error processing video frame", errorMessage);
|
|
7808
|
+
videoFrame.close();
|
|
7809
|
+
});
|
|
7514
7810
|
}
|
|
7515
7811
|
}
|
|
7516
7812
|
setupAudioProcessing(audioStream) {
|
|
7517
7813
|
if (!this.audioSource) {
|
|
7814
|
+
logger.warn("[RecorderWorker] setupAudioProcessing called but audioSource is null");
|
|
7518
7815
|
return;
|
|
7519
7816
|
}
|
|
7817
|
+
logger.debug("[RecorderWorker] setupAudioProcessing", {
|
|
7818
|
+
hasAudioSource: !!this.audioSource,
|
|
7819
|
+
hasAudioStream: !!audioStream,
|
|
7820
|
+
audioProcessingActive: this.audioProcessingActive,
|
|
7821
|
+
lastAudioTimestamp: this.lastAudioTimestamp
|
|
7822
|
+
});
|
|
7520
7823
|
this.audioProcessor = audioStream.getReader();
|
|
7521
7824
|
this.audioProcessingActive = true;
|
|
7825
|
+
logger.debug("[RecorderWorker] Audio processing started", {
|
|
7826
|
+
hasAudioProcessor: !!this.audioProcessor,
|
|
7827
|
+
audioProcessingActive: this.audioProcessingActive
|
|
7828
|
+
});
|
|
7522
7829
|
this.processAudioData();
|
|
7523
7830
|
}
|
|
7524
7831
|
handlePausedAudioData(audioData) {
|
|
@@ -7526,16 +7833,25 @@ class RecorderWorker {
|
|
|
7526
7833
|
}
|
|
7527
7834
|
createAudioBuffer(audioData) {
|
|
7528
7835
|
const numberOfFrames = audioData.numberOfFrames;
|
|
7836
|
+
if (numberOfFrames <= 0) {
|
|
7837
|
+
throw new Error("Number of frames must be greater than zero");
|
|
7838
|
+
}
|
|
7529
7839
|
const numberOfChannels = audioData.numberOfChannels;
|
|
7840
|
+
if (numberOfChannels <= 0) {
|
|
7841
|
+
throw new Error("Number of channels must be greater than zero");
|
|
7842
|
+
}
|
|
7530
7843
|
const audioBuffer = new Float32Array(numberOfFrames * numberOfChannels);
|
|
7531
7844
|
audioData.copyTo(audioBuffer, { planeIndex: 0 });
|
|
7532
7845
|
return audioBuffer;
|
|
7533
7846
|
}
|
|
7534
7847
|
createAudioSample(audioData, audioBuffer) {
|
|
7535
7848
|
const sampleRate = audioData.sampleRate;
|
|
7849
|
+
if (sampleRate <= 0) {
|
|
7850
|
+
throw new Error("Sample rate must be greater than zero");
|
|
7851
|
+
}
|
|
7536
7852
|
const numberOfChannels = audioData.numberOfChannels;
|
|
7537
|
-
if (
|
|
7538
|
-
|
|
7853
|
+
if (numberOfChannels <= 0) {
|
|
7854
|
+
throw new Error("Number of channels must be greater than zero");
|
|
7539
7855
|
}
|
|
7540
7856
|
const shouldWriteSilence = this.isMuted;
|
|
7541
7857
|
return new AudioSample({
|
|
@@ -7547,37 +7863,73 @@ class RecorderWorker {
|
|
|
7547
7863
|
});
|
|
7548
7864
|
}
|
|
7549
7865
|
async processAudioSample(audioData, audioSample) {
|
|
7550
|
-
|
|
7551
|
-
return;
|
|
7552
|
-
}
|
|
7866
|
+
const audioSource = requireInitialized(this.audioSource, "Audio source");
|
|
7553
7867
|
const sampleRate = audioData.sampleRate;
|
|
7868
|
+
if (sampleRate <= 0) {
|
|
7869
|
+
throw new Error("Sample rate must be greater than zero");
|
|
7870
|
+
}
|
|
7554
7871
|
const numberOfFrames = audioData.numberOfFrames;
|
|
7555
7872
|
const duration = numberOfFrames / sampleRate;
|
|
7556
|
-
|
|
7557
|
-
|
|
7558
|
-
this.lastAudioTimestamp += duration;
|
|
7559
|
-
} catch (error) {
|
|
7560
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
7873
|
+
await audioSource.add(audioSample).catch((error) => {
|
|
7874
|
+
const errorMessage = extractErrorMessage(error);
|
|
7561
7875
|
this.sendError(new Error(\`Failed to add audio sample: \${errorMessage}\`));
|
|
7562
|
-
}
|
|
7563
|
-
|
|
7564
|
-
|
|
7565
|
-
|
|
7876
|
+
});
|
|
7877
|
+
logger.debug("[RecorderWorker] Audio sample processed", {
|
|
7878
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
7879
|
+
duration,
|
|
7880
|
+
newLastAudioTimestamp: this.lastAudioTimestamp + duration,
|
|
7881
|
+
sampleRate: audioData.sampleRate,
|
|
7882
|
+
numberOfFrames: audioData.numberOfFrames
|
|
7883
|
+
});
|
|
7884
|
+
this.lastAudioTimestamp += duration;
|
|
7885
|
+
audioSample.close();
|
|
7886
|
+
audioData.close();
|
|
7566
7887
|
}
|
|
7567
7888
|
async processAudioData() {
|
|
7568
7889
|
if (!(this.audioProcessor && this.audioSource)) {
|
|
7890
|
+
logger.warn("[RecorderWorker] processAudioData called but processor or source is null", {
|
|
7891
|
+
hasAudioProcessor: !!this.audioProcessor,
|
|
7892
|
+
hasAudioSource: !!this.audioSource
|
|
7893
|
+
});
|
|
7569
7894
|
return;
|
|
7570
7895
|
}
|
|
7896
|
+
logger.debug("[RecorderWorker] processAudioData loop started", {
|
|
7897
|
+
hasAudioProcessor: !!this.audioProcessor,
|
|
7898
|
+
hasAudioSource: !!this.audioSource,
|
|
7899
|
+
audioProcessingActive: this.audioProcessingActive,
|
|
7900
|
+
isPaused: this.isPaused,
|
|
7901
|
+
isMuted: this.isMuted,
|
|
7902
|
+
lastAudioTimestamp: this.lastAudioTimestamp
|
|
7903
|
+
});
|
|
7904
|
+
let audioSampleCount = 0;
|
|
7571
7905
|
while (this.audioProcessingActive) {
|
|
7572
7906
|
const result = await this.audioProcessor.read();
|
|
7573
7907
|
if (result.done) {
|
|
7908
|
+
logger.debug("[RecorderWorker] Audio processor stream ended", {
|
|
7909
|
+
audioSampleCount,
|
|
7910
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
7911
|
+
audioProcessingActive: this.audioProcessingActive
|
|
7912
|
+
});
|
|
7574
7913
|
this.audioProcessingActive = false;
|
|
7575
7914
|
break;
|
|
7576
7915
|
}
|
|
7577
7916
|
const audioData = result.value;
|
|
7578
7917
|
if (!audioData) {
|
|
7918
|
+
logger.warn("[RecorderWorker] Received null audioData from processor");
|
|
7579
7919
|
continue;
|
|
7580
7920
|
}
|
|
7921
|
+
audioSampleCount += 1;
|
|
7922
|
+
if (audioSampleCount % 100 === 0) {
|
|
7923
|
+
logger.debug("[RecorderWorker] Processing audio sample", {
|
|
7924
|
+
sampleCount: audioSampleCount,
|
|
7925
|
+
numberOfFrames: audioData.numberOfFrames,
|
|
7926
|
+
sampleRate: audioData.sampleRate,
|
|
7927
|
+
numberOfChannels: audioData.numberOfChannels,
|
|
7928
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
7929
|
+
isPaused: this.isPaused,
|
|
7930
|
+
isMuted: this.isMuted
|
|
7931
|
+
});
|
|
7932
|
+
}
|
|
7581
7933
|
if (this.isPaused) {
|
|
7582
7934
|
this.handlePausedAudioData(audioData);
|
|
7583
7935
|
continue;
|
|
@@ -7586,6 +7938,11 @@ class RecorderWorker {
|
|
|
7586
7938
|
const audioSample = this.createAudioSample(audioData, audioBuffer);
|
|
7587
7939
|
await this.processAudioSample(audioData, audioSample);
|
|
7588
7940
|
}
|
|
7941
|
+
logger.debug("[RecorderWorker] processAudioData loop ended", {
|
|
7942
|
+
audioSampleCount,
|
|
7943
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
7944
|
+
audioProcessingActive: this.audioProcessingActive
|
|
7945
|
+
});
|
|
7589
7946
|
}
|
|
7590
7947
|
handlePause() {
|
|
7591
7948
|
if (this.isPaused) {
|
|
@@ -7625,11 +7982,9 @@ class RecorderWorker {
|
|
|
7625
7982
|
this.audioProcessor = null;
|
|
7626
7983
|
}
|
|
7627
7984
|
if (this.output) {
|
|
7628
|
-
|
|
7629
|
-
await this.output.finalize();
|
|
7630
|
-
} catch (error) {
|
|
7985
|
+
await this.output.finalize().catch((error) => {
|
|
7631
7986
|
logger.warn("[RecorderWorker] finalize failed (ignored, already finalized?)", error);
|
|
7632
|
-
}
|
|
7987
|
+
});
|
|
7633
7988
|
}
|
|
7634
7989
|
await this.cleanup();
|
|
7635
7990
|
this.sendStateChange("stopped");
|
|
@@ -7639,6 +7994,9 @@ class RecorderWorker {
|
|
|
7639
7994
|
this.isMuted = !this.isMuted;
|
|
7640
7995
|
}
|
|
7641
7996
|
handleUpdateFps(fps) {
|
|
7997
|
+
if (fps <= 0) {
|
|
7998
|
+
throw new Error("Frame rate must be greater than zero");
|
|
7999
|
+
}
|
|
7642
8000
|
logger.debug("[RecorderWorker] Updating FPS", {
|
|
7643
8001
|
fps,
|
|
7644
8002
|
previousFps: this.frameRate
|
|
@@ -7648,16 +8006,51 @@ class RecorderWorker {
|
|
|
7648
8006
|
this.config.fps = fps;
|
|
7649
8007
|
}
|
|
7650
8008
|
}
|
|
8009
|
+
handleUpdateSourceType(isScreenCapture) {
|
|
8010
|
+
logger.debug("[RecorderWorker] Updating source type", {
|
|
8011
|
+
isScreenCapture,
|
|
8012
|
+
previousIsScreenCapture: this.isScreenCapture
|
|
8013
|
+
});
|
|
8014
|
+
this.isScreenCapture = isScreenCapture;
|
|
8015
|
+
}
|
|
7651
8016
|
async handleSwitchSource(videoStream) {
|
|
8017
|
+
requireDefined(videoStream, "Video stream is required");
|
|
8018
|
+
requireDefined(this.frameRate, "Frame rate must be set");
|
|
8019
|
+
if (this.frameRate <= 0) {
|
|
8020
|
+
throw new Error("Frame rate must be greater than zero");
|
|
8021
|
+
}
|
|
7652
8022
|
if (this.videoProcessor) {
|
|
7653
8023
|
this.videoProcessingActive = false;
|
|
7654
8024
|
await this.videoProcessor.cancel();
|
|
8025
|
+
let drainResult = await this.videoProcessor.read().catch(() => ({ done: true }));
|
|
8026
|
+
while (!drainResult.done) {
|
|
8027
|
+
drainResult.value?.close();
|
|
8028
|
+
drainResult = await this.videoProcessor.read().catch(() => ({ done: true }));
|
|
8029
|
+
}
|
|
7655
8030
|
this.videoProcessor = null;
|
|
7656
8031
|
}
|
|
7657
|
-
|
|
8032
|
+
requireNonNull(this.baseVideoTimestamp, "Base video timestamp must be set for source switch");
|
|
8033
|
+
const minFrameDuration = 1 / this.frameRate;
|
|
8034
|
+
const rawDrift = this.lastAudioTimestamp - this.lastVideoTimestamp;
|
|
8035
|
+
const maxDriftCorrection = 0.1;
|
|
8036
|
+
this.driftOffset = Math.max(-maxDriftCorrection, Math.min(maxDriftCorrection, rawDrift));
|
|
8037
|
+
const continuationTimestamp = Math.max(this.lastAudioTimestamp, this.lastVideoTimestamp) + minFrameDuration;
|
|
8038
|
+
const previousVideoTimestamp = this.lastVideoTimestamp;
|
|
7658
8039
|
this.lastVideoTimestamp = continuationTimestamp;
|
|
7659
8040
|
this.frameCount = 0;
|
|
7660
|
-
|
|
8041
|
+
logger.debug("[RecorderWorker] handleSwitchSource - preserving baseVideoTimestamp", {
|
|
8042
|
+
continuationTimestamp,
|
|
8043
|
+
lastVideoTimestamp: this.lastVideoTimestamp,
|
|
8044
|
+
frameRate: this.frameRate,
|
|
8045
|
+
isScreenCapture: this.isScreenCapture,
|
|
8046
|
+
baseVideoTimestamp: this.baseVideoTimestamp,
|
|
8047
|
+
recordingStartTime: this.recordingStartTime,
|
|
8048
|
+
lastAudioTimestamp: this.lastAudioTimestamp,
|
|
8049
|
+
previousVideoTimestamp,
|
|
8050
|
+
minFrameDuration,
|
|
8051
|
+
rawDrift,
|
|
8052
|
+
driftOffset: this.driftOffset
|
|
8053
|
+
});
|
|
7661
8054
|
this.setupVideoProcessing(videoStream);
|
|
7662
8055
|
}
|
|
7663
8056
|
async cleanup() {
|
|
@@ -7686,11 +8079,9 @@ class RecorderWorker {
|
|
|
7686
8079
|
}
|
|
7687
8080
|
if (this.output) {
|
|
7688
8081
|
if (!this.isFinalized) {
|
|
7689
|
-
|
|
7690
|
-
await this.output.cancel();
|
|
7691
|
-
} catch (error) {
|
|
8082
|
+
await this.output.cancel().catch((error) => {
|
|
7692
8083
|
logger.warn("[RecorderWorker] cancel failed (ignored, possibly finalized)", error);
|
|
7693
|
-
}
|
|
8084
|
+
});
|
|
7694
8085
|
this.isFinalized = true;
|
|
7695
8086
|
}
|
|
7696
8087
|
this.output = null;
|
|
@@ -7702,13 +8093,20 @@ class RecorderWorker {
|
|
|
7702
8093
|
this.totalSize = 0;
|
|
7703
8094
|
this.pausedDuration = 0;
|
|
7704
8095
|
this.pauseStartedAt = null;
|
|
8096
|
+
this.overlayCanvas = null;
|
|
8097
|
+
this.overlayConfig = null;
|
|
8098
|
+
this.hiddenIntervals = [];
|
|
8099
|
+
this.currentHiddenIntervalStart = null;
|
|
8100
|
+
this.recordingStartTime = 0;
|
|
8101
|
+
this.pendingVisibilityUpdates = [];
|
|
8102
|
+
this.isScreenCapture = false;
|
|
7705
8103
|
}
|
|
7706
8104
|
sendReady() {
|
|
7707
8105
|
const response = { type: "ready" };
|
|
7708
8106
|
self.postMessage(response);
|
|
7709
8107
|
}
|
|
7710
8108
|
sendError(error) {
|
|
7711
|
-
const errorMessage =
|
|
8109
|
+
const errorMessage = extractErrorMessage(error);
|
|
7712
8110
|
const response = {
|
|
7713
8111
|
type: "error",
|
|
7714
8112
|
error: errorMessage
|
|
@@ -7742,7 +8140,7 @@ class RecorderWorker {
|
|
|
7742
8140
|
}
|
|
7743
8141
|
}
|
|
7744
8142
|
new RecorderWorker;
|
|
7745
|
-
`],{type:`application/javascript`});return wo=URL.createObjectURL(e),wo}var Eo=class{constructor(){this.worker=null,this.chunks=[],this.totalSize=0,this.isActive=!1,this.videoTrackClone=null,this.audioTrackClone=null,this.isMuted=!1,this.currentVideoTrack=null,this.isPaused=!1,this.readyPromiseResolve=null,this.setupWorker()}setupWorker(){if(typeof Worker>`u`)throw b.error(`[WorkerProcessor] Web Workers are not supported`),Error(`Web Workers are not supported`);try{b.debug(`[WorkerProcessor] Setting up worker`);let e=To();b.debug(`[WorkerProcessor] Worker URL created`,{urlType:typeof e,isBlobUrl:e.startsWith(`blob:`)}),this.worker=new Worker(e,{type:`classic`}),b.debug(`[WorkerProcessor] Worker created successfully`),this.worker.onmessage=this.handleWorkerMessage.bind(this),this.worker.onerror=this.handleWorkerError.bind(this),b.debug(`[WorkerProcessor] Worker event handlers attached`)}catch(e){let t=e instanceof Error?e.message:String(e);throw b.error(`[WorkerProcessor] Failed to create worker:`,e),Error(`Failed to create worker: ${t}`)}}handleWorkerMessage(e){let t=e.data;switch(t.type){case`ready`:b.debug(`[WorkerProcessor] Worker ready`),this.readyPromiseResolve&&=(this.readyPromiseResolve(),null);break;case`error`:b.error(`[WorkerProcessor] Worker error:`,t.error),this.onError&&this.onError(Error(t.error));break;case`chunk`:this.chunks.push({data:t.data,position:t.position}),this.totalSize=Math.max(this.totalSize,t.position+t.data.length);break;case`bufferUpdate`:this.onBufferUpdate&&this.onBufferUpdate(t.size,t.formatted);break;case`stateChange`:b.debug(`[WorkerProcessor] State changed:`,t.state),this.isPaused=t.state===`paused`;break;default:b.warn(`[WorkerProcessor] Unknown response type:`,t)}}handleWorkerError(e){b.error(`[WorkerProcessor] Worker error event:`,{message:e.message,filename:e.filename,lineno:e.lineno,colno:e.colno,error:e.error}),this.onError&&this.onError(Error(e.message||`Unknown worker error`))}async startProcessing(e,t){if(!this.worker)throw Error(`Worker not initialized`);if(this.isActive)throw Error(`Processing already active`);this.isActive=!0,this.isMuted=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0;let n=t.format||`mp4`,r=t.audioCodec||i(n),a=Co(e),o=a?15:t.fps;b.debug(`[WorkerProcessor] Starting processing`,{isScreenCapture:a,targetFps:o,originalFps:t.fps});let s={width:t.width,height:t.height,fps:o,bitrate:t.bitrate,audioCodec:r,audioBitrate:t.audioBitrate,codec:`avc`,keyFrameInterval:5,format:n},c=e.getVideoTracks(),l=e.getAudioTracks();b.debug(`[WorkerProcessor] Preparing to start processing`,{videoTracksCount:c.length,audioTracksCount:l.length,hasWorker:!!this.worker});let u=null,d=null;if(c.length>0){this.stopCurrentVideoTrack();let e=c[0];u=this.cloneVideoTrack(e),this.currentVideoTrack=u}if(l.length>0){let e=l[0];d=this.cloneAudioTrack(e)}b.debug(`[WorkerProcessor] Track details`,{hasVideoTrack:!!u,videoTrackId:u?.id,videoTrackKind:u?.kind,videoTrackReadyState:u?.readyState,hasAudioTrack:!!d,audioTrackId:d?.id,audioTrackKind:d?.kind,audioTrackReadyState:d?.readyState});let f=u===null?null:new MediaStreamTrackProcessor({track:u}).readable,p=d===null?null:new MediaStreamTrackProcessor({track:d}).readable,m={type:`start`,videoStream:f,audioStream:p,config:s},h=[];f&&h.push(f),p&&h.push(p),b.debug(`[WorkerProcessor] Posting message to worker`,{transferablesCount:h.length,messageType:m.type});let g=new Promise(e=>{this.readyPromiseResolve=e});try{this.worker.postMessage(m,h),b.debug(`[WorkerProcessor] Message posted successfully`),await g,b.debug(`[WorkerProcessor] Worker confirmed ready`)}catch(e){throw b.error(`[WorkerProcessor] Failed to post message:`,e),this.readyPromiseResolve=null,e}}pause(){this.worker&&this.isActive&&this.worker.postMessage({type:`pause`})}resume(){this.isWorkerActive()&&this.worker&&this.worker.postMessage({type:`resume`})}isWorkerActive(){return!!(this.worker&&this.isActive)}toggleMute(){this.isMuted=!this.isMuted,this.onMuteStateChange&&this.onMuteStateChange(this.isMuted),this.isWorkerActive()&&this.worker&&this.worker.postMessage({type:`toggleMute`})}switchVideoSource(e){if(!(this.isWorkerActive()&&this.worker))return b.debug(`[WorkerProcessor] Cannot switch source - worker not active`,{hasWorker:!!this.worker,isActive:this.isActive}),Promise.resolve();let t=e.getVideoTracks();if(b.debug(`[WorkerProcessor] Switching video source`,{videoTracksCount:t.length}),t.length===0)return b.warn(`[WorkerProcessor] No video tracks in new stream`),Promise.resolve();let n=Co(e),r=n?15:30;b.debug(`[WorkerProcessor] Source type detected`,{isScreenCapture:n,targetFps:r});let i={type:`updateFps`,fps:r};this.worker.postMessage(i);let a=t[0];this.stopCurrentVideoTrack();let o=this.cloneVideoTrack(a);b.debug(`[WorkerProcessor] New video track details`,{trackId:o.id,trackKind:o.kind,trackReadyState:o.readyState}),this.currentVideoTrack=o;let s={type:`switchSource`,videoStream:new MediaStreamTrackProcessor({track:o}).readable};try{b.debug(`[WorkerProcessor] Posting switch source message`);let e=[];return s.videoStream&&e.push(s.videoStream),this.worker.postMessage(s,e),b.debug(`[WorkerProcessor] Switch source message posted`),new Promise(e=>{setTimeout(()=>{e()},0)})}catch(e){throw b.error(`[WorkerProcessor] Failed to switch source:`,e),e}}finalize(){if(!this.isWorkerActive())throw Error(`Processing not active`);return new Promise((e,t)=>{let n=setTimeout(()=>{t(Error(`Finalize timeout`))},3e4),r=i=>{let a=i.data;a.type===`stateChange`&&a.state===`stopped`?(this.worker&&this.worker.removeEventListener(`message`,r),clearTimeout(n),this.isActive=!1,e(this.createBlobFromChunks())):a.type===`error`&&(this.worker&&this.worker.removeEventListener(`message`,r),clearTimeout(n),t(Error(a.error)))};this.worker&&(this.worker.addEventListener(`message`,r),this.worker.postMessage({type:`stop`}))})}createBlobFromChunks(){let e=[...this.chunks].sort((e,t)=>e.position-t.position),t=new ArrayBuffer(this.totalSize),n=new Uint8Array(t);for(let t of e)n.set(t.data,t.position);return{blob:new Blob([t],{type:`video/mp4`}),totalSize:this.totalSize}}cancel(){return this.worker&&this.isActive&&this.worker.postMessage({type:`stop`}),this.isActive=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0,Promise.resolve()}getBufferSize(){return this.totalSize}getMutedState(){return this.isMuted}isPausedState(){return this.isPaused}getClonedAudioTrack(){return this.audioTrackClone}getAudioStreamForAnalysis(){return this.audioTrackClone?new MediaStream([this.audioTrackClone]):null}setOnBufferUpdate(e){this.onBufferUpdate=e}setOnError(e){this.onError=e}setOnMuteStateChange(e){this.onMuteStateChange=e}cloneVideoTrack(e){if(b.debug(`[WorkerProcessor] Original video track:`,{id:e.id,kind:e.kind,readyState:e.readyState,hasClone:typeof e.clone==`function`}),typeof e.clone==`function`)try{let t=e.clone();return this.videoTrackClone=t,b.debug(`[WorkerProcessor] Video track cloned successfully:`,{id:t.id,kind:t.kind,readyState:t.readyState}),t}catch(e){throw b.error(`[WorkerProcessor] Failed to clone video track:`,e),Error(`Failed to clone video track: ${e instanceof Error?e.message:String(e)}`)}return b.warn(`[WorkerProcessor] Video track clone() not available, using original`),this.videoTrackClone=e,e}cloneAudioTrack(e){if(b.debug(`[WorkerProcessor] Original audio track:`,{id:e.id,kind:e.kind,readyState:e.readyState,hasClone:typeof e.clone==`function`}),typeof e.clone==`function`)try{let t=e.clone();return this.audioTrackClone=t,b.debug(`[WorkerProcessor] Audio track cloned successfully:`,{id:t.id,kind:t.kind,readyState:t.readyState}),t}catch(e){throw b.error(`[WorkerProcessor] Failed to clone audio track:`,e),Error(`Failed to clone audio track: ${e instanceof Error?e.message:String(e)}`)}return b.warn(`[WorkerProcessor] Audio track clone() not available, using original`),this.audioTrackClone=e,e}stopCurrentVideoTrack(){this.currentVideoTrack&&this.currentVideoTrack.readyState===`live`&&this.currentVideoTrack.stop(),this.currentVideoTrack=null}cleanup(){this.worker&&=(this.worker.terminate(),null),wo&&=(URL.revokeObjectURL(wo),null),this.isActive=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0}static isSupported(){return typeof Worker<`u`&&typeof MediaStreamTrackProcessor<`u`&&typeof VideoFrame<`u`&&typeof AudioData<`u`}},Do=class{constructor(){if(this.currentVideoStream=null,!Eo.isSupported())throw Error(`Web Workers are required for video processing. Please use a modern browser that supports Web Workers, MediaStreamTrackProcessor, VideoFrame, and AudioData APIs.`);try{this.workerProcessor=new Eo,b.debug(`[StreamProcessor] Using worker-based processing`)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to initialize worker: ${t}. Web Workers are required for video processing.`)}}async startProcessing(e,t){this.workerProcessor.setOnBufferUpdate((e,t)=>{b.debug(`[StreamProcessor] Buffer update:`,{size:e,formatted:t})}),this.workerProcessor.setOnError(e=>{b.error(`[StreamProcessor] Worker error:`,e)}),this.currentVideoStream=e,await this.workerProcessor.startProcessing(e,t)}pause(){this.workerProcessor.pause()}resume(){this.workerProcessor.resume()}isPausedState(){return this.workerProcessor.isPausedState()}async finalize(){return b.debug(`[StreamProcessor] finalize called`),await this.workerProcessor.finalize()}toggleMute(){this.workerProcessor.toggleMute()}isMutedState(){return this.workerProcessor.getMutedState()}getClonedAudioTrack(){return this.workerProcessor.getClonedAudioTrack()}getAudioStreamForAnalysis(){return this.workerProcessor.getAudioStreamForAnalysis()}async switchVideoSource(e){await this.workerProcessor.switchVideoSource(e),this.currentVideoStream=e,this.onSourceChange&&this.onSourceChange(e)}getCurrentVideoSource(){return this.currentVideoStream}getBufferSize(){return this.workerProcessor.getBufferSize()}setOnMuteStateChange(e){this.workerProcessor.setOnMuteStateChange(e)}setOnSourceChange(e){this.onSourceChange=e}async cancel(){await this.workerProcessor.cancel(),this.workerProcessor.cleanup(),this.currentVideoStream=null}};let Oo=1e3,ko=`recording`,Ao=`idle`;var jo=class{constructor(e,t){this.recordingState=Ao,this.countdownDuration=5e3,this.countdownRemaining=0,this.countdownTimeoutId=null,this.countdownIntervalId=null,this.countdownStartTime=null,this.isPaused=!1,this.maxRecordingTime=null,this.maxTimeTimer=null,this.recordingSeconds=0,this.recordingIntervalId=null,this.pauseStartTime=null,this.totalPausedTime=0,this.streamProcessor=null,this.originalCameraStream=null,this.streamManager=e,this.callbacks=t}setCountdownDuration(e){this.countdownDuration=e}setMaxRecordingTime(e){this.maxRecordingTime=e}getRecordingState(){return this.recordingState}isPausedState(){return this.isPaused}getRecordingSeconds(){return this.recordingSeconds}getStreamProcessor(){return this.streamProcessor}setOriginalCameraStream(e){this.originalCameraStream=e}getOriginalCameraStream(){return this.originalCameraStream}async startRecording(){try{this.callbacks.onClearUploadStatus(),this.countdownDuration>0?this.startCountdown():await this.doStartRecording()}catch(e){this.handleError(e),this.recordingState=Ao,this.cancelCountdown()}}startCountdown(){this.recordingState=`countdown`,this.countdownRemaining=Math.ceil(this.countdownDuration/Oo),this.countdownStartTime=Date.now(),this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining),this.countdownIntervalId=window.setInterval(()=>{if(!this.countdownStartTime)return;let e=Date.now()-this.countdownStartTime;this.countdownRemaining=Math.max(0,Math.ceil((this.countdownDuration-e)/Oo)),this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining)},100),this.countdownTimeoutId=window.setTimeout(async()=>{await this.doStartRecording().catch(()=>{})},this.countdownDuration)}async doStartRecording(){b.debug(`[RecordingManager] doStartRecording called`),this.cancelCountdown(),this.recordingState=ko,this.callbacks.onStateChange(this.recordingState),this.resetRecordingState();let e=this.streamManager.getStream();if(b.debug(`[RecordingManager] Current stream:`,{hasStream:!!e,audioTracks:e?.getAudioTracks().length||0,videoTracks:e?.getVideoTracks().length||0}),!e){b.warn(`[RecordingManager] No stream available`),this.handleError(Error(`No stream available for recording`)),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState);return}this.originalCameraStream=e,b.debug(`[RecordingManager] Creating new StreamProcessor`),this.streamProcessor=new Do,b.debug(`[RecordingManager] StreamProcessor created:`,!!this.streamProcessor);let t=await this.callbacks.onGetConfig().then(e=>({config:e,error:null})).catch(e=>({config:null,error:e}));if(t.error){this.handleError(t.error),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState);return}if(!t.config){this.handleError(Error(`Failed to get recording config`)),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState);return}b.debug(`[RecordingManager] Starting recording with stream manager`);let n=await this.streamManager.startRecording(this.streamProcessor,t.config).then(()=>(b.info(`[RecordingManager] Recording started successfully`),null)).catch(e=>(b.error(`[RecordingManager] Error starting recording:`,e),e));if(n){this.handleError(n),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState);return}this.startRecordingTimer(),this.maxRecordingTime&&this.maxRecordingTime>0&&(this.maxTimeTimer=window.setTimeout(async()=>{this.recordingState===ko&&await this.stopRecording()},this.maxRecordingTime))}async stopRecording(){b.debug(`[RecordingManager] stopRecording called`);try{this.cancelCountdown(),this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.clearTimer(this.maxTimeTimer,clearTimeout),this.maxTimeTimer=null,this.resetPauseState(),this.callbacks.onStopAudioTracking(),b.debug(`[RecordingManager] Stopping recording in stream manager`);let e=await this.streamManager.stopRecording();return b.info(`[RecordingManager] Recording stopped, blob size:`,e.size),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState),this.recordingSeconds=0,this.streamProcessor=null,this.callbacks.onRecordingComplete(e),e}catch(e){throw this.handleError(e),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState),e}}pauseRecording(){this.recordingState!==ko||this.isPaused||(this.streamManager.pauseRecording(),this.isPaused=!0,this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.pauseStartTime=Date.now())}resumeRecording(){this.recordingState!==ko||!this.isPaused||(this.streamManager.resumeRecording(),this.isPaused=!1,this.updatePausedDuration(),this.startRecordingTimer())}cancelCountdown(){this.clearTimer(this.countdownTimeoutId,clearTimeout),this.countdownTimeoutId=null,this.clearTimer(this.countdownIntervalId,clearInterval),this.countdownIntervalId=null,this.recordingState=Ao,this.countdownRemaining=0,this.countdownStartTime=null,this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining)}cleanup(){this.cancelCountdown(),this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.clearTimer(this.maxTimeTimer,clearTimeout),this.maxTimeTimer=null}resetRecordingState(){this.isPaused=!1,this.recordingSeconds=0,this.totalPausedTime=0,this.pauseStartTime=null}resetPauseState(){this.isPaused=!1,this.pauseStartTime=null,this.totalPausedTime=0}updatePausedDuration(){if(this.pauseStartTime===null)throw Error(`Pause start time not set`);let e=Date.now()-this.pauseStartTime;this.totalPausedTime+=e,this.pauseStartTime=null}startRecordingTimer(){this.recordingIntervalId===null&&(this.recordingIntervalId=window.setInterval(()=>{this.recordingSeconds+=1,this.callbacks.onTimerUpdate(me(this.recordingSeconds))},1e3))}clearTimer(e,t){e!==null&&t(e)}handleError(e){let n=e instanceof Error?e:Error(t(e));this.callbacks.onError(n)}};let Mo=()=>{},No=()=>{},Po=()=>{},Fo=()=>{},Io=e=>{},Lo=(e,t)=>{},Ro=e=>{},zo=e=>{},Bo=e=>{},Vo=()=>{},Ho=e=>{};function Uo(){return{onProgress:Mo,onSuccess:No,onError:Po,onClearStatus:Fo}}var Wo=class{constructor(e={}){this.uploadService=null,this.uploadQueueManager=null,this.isInitialized=!1,this.callbacks=e,this.streamManager=new ye,this.configManager=new p,this.storageManager=new y,this.deviceManager=new m(this.streamManager,e.device),this.audioLevelAnalyzer=new n,this.uploadService=new xe,this.uploadCallbacks=e.upload?e.upload:Uo();let t=this.createRecordingCallbacks(e);this.recordingManager=new jo(this.streamManager,t);let r=this.createSourceSwitchCallbacks(e);this.sourceSwitchManager=new se(this.streamManager,r),e.stream&&(this.streamManager.on(`streamstart`,({stream:t})=>{b.debug(`[RecorderController] streamstart event received, calling callback`),e.stream?.onStreamStart&&e.stream.onStreamStart(t)}),this.streamManager.on(`streamstop`,()=>{b.debug(`[RecorderController] streamstop event received, calling callback`),e.stream?.onStreamStop&&e.stream.onStreamStop()}),this.streamManager.on(`error`,({error:t})=>{b.error(`[RecorderController] stream error event received, calling callback`,t),e.stream?.onError&&e.stream.onError(t)}))}async initialize(e){if(this.isInitialized)return;e.apiKey&&e.backendUrl&&await this.configManager.initialize(e.apiKey,e.backendUrl),e.countdownDuration!==void 0&&this.recordingManager.setCountdownDuration(e.countdownDuration),e.maxRecordingTime!==void 0&&this.recordingManager.setMaxRecordingTime(e.maxRecordingTime);let t=this.callbacks.onStorageCleanupError??Ho;await this.storageManager.initialize(t);let n=this.storageManager.getStorageService();n&&this.uploadService&&(this.uploadQueueManager=new be(n,this.uploadService),this.uploadQueueManager.setCallbacks({onUploadProgress:(e,t)=>{this.uploadCallbacks.onProgress(t)},onUploadComplete:(e,t)=>{this.uploadCallbacks.onSuccess(t)},onUploadError:(e,t)=>{this.uploadCallbacks.onError(t)}})),this.isInitialized=!0}async startStream(){b.debug(`[RecorderController] startStream called`),await this.streamManager.startStream(),b.debug(`[RecorderController] startStream completed`)}async stopStream(){await this.streamManager.stopStream()}switchVideoDevice(e){return this.streamManager.switchVideoDevice(e)}switchAudioDevice(e){return this.streamManager.switchAudioDevice(e)}async startRecording(){await this.recordingManager.startRecording()}async stopRecording(){let e=await this.recordingManager.stopRecording();return await this.sourceSwitchManager.handleRecordingStop().catch(()=>{throw Error(`Source switch cleanup failed`)}),e}pauseRecording(){this.recordingManager.pauseRecording()}resumeRecording(){this.recordingManager.resumeRecording()}async switchSource(e){await this.sourceSwitchManager.toggleSource()}setCameraDevice(e){this.deviceManager.setCameraDevice(e)}setMicDevice(e){this.deviceManager.setMicDevice(e)}getAvailableDevices(){return this.deviceManager.getAvailableDevices()}muteAudio(){this.streamManager.muteAudio()}unmuteAudio(){this.streamManager.unmuteAudio()}toggleMute(){this.streamManager.toggleMute()}getIsMuted(){return this.streamManager.isMuted()}startAudioLevelTracking(e,t){if(!t)throw Error(`Audio level callbacks are required`);return this.audioLevelAnalyzer.startTracking(e,t,()=>this.streamManager.isMuted()),Promise.resolve()}stopAudioLevelTracking(){this.audioLevelAnalyzer.stopTracking()}getAudioLevel(){return this.audioLevelAnalyzer.getAudioLevel()}async uploadVideo(e,t,n,r){if(!this.uploadQueueManager)throw Error(`Upload queue manager not initialized`);this.uploadCallbacks.onClearStatus();let i=await xo(e),a=Object.keys(r).length>0?r:void 0;await this.uploadQueueManager.queueUpload({blob:e,apiKey:t,backendUrl:n,filename:`recording-${Date.now()}.mp4`,duration:i,metadata:void 0,userMetadata:a})}getStream(){return this.streamManager.getStream()}getRecordingState(){return this.recordingManager.getRecordingState()}isPaused(){return this.recordingManager.isPausedState()}getCurrentSourceType(){return this.sourceSwitchManager.getCurrentSourceType()}getOriginalCameraStream(){return this.sourceSwitchManager.getOriginalCameraStream()}getStreamManager(){return this.streamManager}getAudioStreamForAnalysis(){return this.streamManager.getAudioStreamForAnalysis()}getDeviceManager(){return this.deviceManager}getConfig(){return this.configManager.getConfig()}getUploadService(){return this.uploadService}isRecording(){return this.streamManager.isRecording()}isActive(){return this.streamManager.isActive()}cleanup(){this.storageManager.destroy(),this.recordingManager.cleanup(),this.audioLevelAnalyzer.stopTracking(),this.sourceSwitchManager.cleanup()}createRecordingCallbacks(e){let t=e.recording;return{onStateChange:t?.onStateChange??Io,onCountdownUpdate:t?.onCountdownUpdate??Lo,onTimerUpdate:t?.onTimerUpdate??Ro,onError:t?.onError??zo,onRecordingComplete:t?.onRecordingComplete??Bo,onClearUploadStatus:t?.onClearUploadStatus??Vo,onStopAudioTracking:()=>{this.audioLevelAnalyzer.stopTracking()},onGetConfig:()=>this.configManager.getConfig()}}createSourceSwitchCallbacks(e){let t=e.sourceSwitch;return{onSourceChange:t?.onSourceChange,onPreviewUpdate:t?.onPreviewUpdate,onError:t?.onError,onTransitionStart:t?.onTransitionStart,onTransitionEnd:t?.onTransitionEnd,getSelectedCameraDeviceId:()=>this.deviceManager.getSelectedCameraDeviceId(),getSelectedMicDeviceId:()=>this.deviceManager.getSelectedMicDeviceId()}}};function Go(e){if(typeof e==`string`)return new Ir(e);if(e instanceof Blob)return new Pr(e);throw Error(`Invalid input type. Expected Blob, File, or file path string.`)}function Ko(e){switch(e){case`mp4`:return new Ba;case`webm`:case`mkv`:case`mov`:throw Error(`Format ${e} is not yet supported. Only MP4 is currently supported.`);default:throw Error(`Unsupported output format: ${e}`)}}function qo(e){switch(e){case`mp4`:return`video/mp4`;case`webm`:return`video/webm`;case`mkv`:return`video/x-matroska`;case`mov`:return`video/quicktime`;default:throw Error(`Unsupported output format: ${e}`)}}function Jo(e){let t=a(e.format,e.audioCodec);return{video:{width:e.width,height:e.height,fit:`contain`,frameRate:e.fps,bitrate:e.bitrate,forceTranscode:!0},audio:{codec:t,forceTranscode:!0}}}function Yo(e){if(!e.isValid){let t=e.discardedTracks.map(e=>e.reason).join(`, `);throw Error(`Conversion is invalid. Discarded tracks: ${t}`)}}async function Xo(e,t={},n){let r={...o,...t,format:t.format||o.format};r.audioCodec||=i(r.format);let a=Go(e),s=new Br({formats:[Ar],source:a}),c=new mo({format:Ko(r.format),target:new Na}),l=await vo.init({input:s,output:c,...Jo(r)});Yo(l),n&&(l.onProgress=n),await l.execute();let u=c.target.buffer;if(!u)throw Error(`Transcoding completed but no output buffer was generated`);let d=qo(r.format);return{buffer:u,blob:new Blob([u],{type:d})}}function Zo(e){if(e<.25){let t=e/.25;return`rgb(255, ${Math.round(165+50*t)}, 0)`}if(e<.5){let t=(e-.25)/.25;return`rgb(${Math.round(255- -205*t)}, ${Math.round(215+-10*t)}, ${Math.round(0+50*t)})`}if(e<.75){let t=(e-.5)/.25;return`rgb(${Math.round(50- -50*t)}, ${Math.round(205+-77*t)}, ${Math.round(50+78*t)})`}let t=(e-.75)/.25;return`rgb(0, ${Math.round(128- -28*t)}, ${Math.round(128+72*t)})`}function Qo(e,t){let n=e.querySelector(t);if(!n)throw Error(`Element not found: ${t}`);return n}async function $o(e,n,r){e.pause(),e.srcObject=null,e.load(),r===`screen`?e.classList.add(`vidtreo-screen-share`):e.classList.remove(`vidtreo-screen-share`),await new Promise(e=>{setTimeout(()=>e(),100)}),e.srcObject=n,await new Promise((n,r)=>{let i=setTimeout(()=>{a||(a=!0,o(),r(Error(`Timeout waiting for video to load`)))},1e4),a=!1,o=()=>{a||!e||(e.removeEventListener(`loadedmetadata`,f),e.removeEventListener(`loadeddata`,p),e.removeEventListener(`canplay`,m),e.removeEventListener(`playing`,h),e.removeEventListener(`error`,g))},s=()=>{a||(a=!0,clearTimeout(i),o(),n())},c=e=>{a||(a=!0,clearTimeout(i),o(),r(Error(`Failed to play preview video: ${e}`)))},l=async()=>{if(!(a||!e))try{await e.play(),s()}catch(e){c(`Failed to play preview video: ${t(e)}`)}},u=n=>{if(a||!e)return;let r=t(n);r.includes(`interrupted`)||r.includes(`abort`)?setTimeout(l,200):c(r)},d=async()=>{if(!(a||!e))try{await e.play(),s()}catch(e){u(e)}},f=()=>{a||!e||d()},p=()=>{a||!e||e.readyState>=2&&d()},m=()=>{a||!e||e.readyState>=3&&d()},h=()=>{a||!e||(a=!0,clearTimeout(i),o(),n())},g=e=>{if(a)return;a=!0,clearTimeout(i),o();let n=e instanceof ErrorEvent?e.error:Error(`Video element error`);r(Error(`Failed to load preview: ${t(n)}`))};if(e.readyState>=1){d();return}e.addEventListener(`loadedmetadata`,f,{once:!0}),e.addEventListener(`loadeddata`,p,{once:!0}),e.addEventListener(`canplay`,m,{once:!0}),e.addEventListener(`playing`,h,{once:!0}),e.addEventListener(`error`,g,{once:!0}),setTimeout(()=>{!a&&e&&e.readyState>=1&&d()},500)})}async function es(e,t){e.pause(),e.srcObject=t,await new Promise(t=>{let n=()=>{e.removeEventListener(`loadedmetadata`,n),t()};e.addEventListener(`loadedmetadata`,n),e.load(),setTimeout(()=>t(),500)});try{await e.play()}catch(e){let t=e instanceof Error?e.message:String(e);if(!(t.includes(`abort`)||t.includes(`interrupted`)))throw Error(`Failed to play preview after device switch: ${t}`)}}function ts(e){return{recording:{onStateChange:()=>{},onCountdownUpdate:(t,n)=>{e.getUIStateManager().updateCountdownOverlay(t,n)},onTimerUpdate:t=>{e.getUIStateManager().updateRecordingTimer(t)},onError:t=>{e.getUIStateManager().showError(t.message)},onRecordingComplete:e=>{},onClearUploadStatus:()=>{e.getUIStateManager().clearUploadStatus()},onStopAudioTracking:()=>{},onGetConfig:async()=>({format:`mp4`,fps:30,width:1280,height:720,bitrate:25e5,audioCodec:`aac`,preset:`medium`,packetCount:0,audioBitrate:128e3})},upload:{onProgress:t=>{e.getUIStateManager().showUploadProgress(),e.getUIStateManager().updateUploadProgress(t)},onSuccess:t=>{e.getUIStateManager().hideUploadProgress(),e.getUIStateManager().showUploadSuccess(t)},onError:t=>{e.getUIStateManager().hideUploadProgress(),e.getUIStateManager().showUploadError(t.message)},onClearStatus:()=>{e.getUIStateManager().clearUploadStatus()}},sourceSwitch:{onSourceChange:async t=>{e.getUIStateManager().updateSwitchButtonText(t);let n=e.getController();if(t===`camera`){let t=n.getStream();if(!t||t.getAudioTracks().length===0)return;n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}).catch(()=>{})}},onPreviewUpdate:async t=>{let n=e.getController(),r=n.getCurrentSourceType();if(r===`screen`){let t=n.getAudioStreamForAnalysis();if(!t||t.getAudioTracks().length===0)return;n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}).catch(()=>{})}else t.getAudioTracks().length>0&&(n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}));await $o(Qo(e.getShadowRoot(),`#videoPreview`),t,r)},onError:n=>{e.getUIStateManager().showError(t(n))},onTransitionStart:t=>{e.getUIStateManager().showSourceTransition(t)},onTransitionEnd:()=>{e.getUIStateManager().hideSourceTransition()}},storage:{onUploadProgress:()=>{},onUploadComplete:()=>{e.checkPendingUploads()},onUploadError:()=>{e.checkPendingUploads()}},onStorageCleanupError:t=>{e.getUIStateManager().showError(t)}}}function ns(e,t){let n=e.querySelector(`#previewSkeleton`),r=e.querySelector(`.vidtreo-skeleton-text`);if(!n)throw Error(`Preview skeleton element not found`);if(n.style.display=`flex`,!r)throw Error(`Skeleton text element not found`);r.textContent=t}function rs(e){let t=e.querySelector(`#previewSkeleton`);if(!t)throw Error(`Preview skeleton element not found`);t.style.display=`none`}async function is(e,n){let r=n===`default`?null:n;if(e.getController().setCameraDevice(r),e.getDeviceManager().setCameraDevice(r),!(!e.getController().isActive()||e.getController().isRecording())){ns(e.getShadowRoot(),e.getI18nManager().t(`switchingCamera`));try{let t=await e.getController().switchVideoDevice(r);await es(Qo(e.getShadowRoot(),`#videoPreview`),t),rs(e.getShadowRoot())}catch(n){rs(e.getShadowRoot()),e.showError(t(n));let r=e.getDeviceManager().getSelectedCameraDeviceId(),i=e.getShadowRoot().querySelector(`#cameraSelect`);if(!i)throw Error(`Camera select element not found`);if(!r)throw Error(`Camera device ID is required`);i.value=r}}}async function as(e,n){let r=n===`default`?null:n;if(e.getController().setMicDevice(r),e.getDeviceManager().setMicDevice(r),!(!e.getController().isActive()||e.getController().isRecording())){ns(e.getShadowRoot(),e.getI18nManager().t(`switchingMicrophone`));try{await e.getController().switchAudioDevice(r);let t=e.getController().getStream();if(!t)throw Error(`Stream is required`);e.getController().stopAudioLevelTracking(),await e.getController().startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}),setTimeout(()=>{rs(e.getShadowRoot())},200)}catch(n){rs(e.getShadowRoot()),e.showError(t(n));let r=e.getDeviceManager().getSelectedMicDeviceId(),i=e.getShadowRoot().querySelector(`#micSelect`);if(!i)throw Error(`Microphone select element not found`);if(!r)throw Error(`Microphone device ID is required`);i.value=r}}}function os(e){let t=e.getStreamManager();t.on(`statechange`,({state:t,previousState:n})=>{e.handleStateChange(t,n)}),t.on(`streamstart`,({stream:t})=>{e.handleStreamStart(t)}),t.on(`streamstop`,()=>{e.handleStreamStop()}),t.on(`recordingstart`,()=>{e.handleRecordingStart()}),t.on(`recordingstop`,({blob:t})=>{e.handleRecordingStop(t).catch(t=>{e.showError(e.extractErrorMessage(t))})}),t.on(`recordingtimeupdate`,({formatted:t})=>{e.updateRecordingTimer(t)}),t.on(`recordingbufferupdate`,()=>{}),t.on(`audiomutetoggle`,({muted:t})=>{e.updateMuteState(t)}),t.on(`videosourcechange`,({stream:t})=>{e.updateVideoPreview(t)}),t.on(`error`,({error:t})=>{e.showError(t.message)})}async function ss(e){if(e.getShowSettings()){e.setShowSettings(!1);let t=e.getShadowRoot().querySelector(`#settingsPanel`);if(!t)throw Error(`Settings panel element not found`);t.style.display=`none`}await e.getController().startRecording()}async function cs(e){let t=await e.getController().stopRecording();return e.setProcessedBlob(t),e.setRecordedBlob(t),e.getUIStateManager().updateRecordingControlsAfterStop(),await fs(e,t),t}function ls(e){e.getController().pauseRecording(),e.getUIStateManager().updatePauseState(e.getController().isPaused())}function us(e){e.getController().resumeRecording(),e.getUIStateManager().updatePauseState(e.getController().isPaused())}async function ds(e){let n=e.getRecordedBlob();if(!n)throw Error(`No recording available`);e.setIsProcessing(!0);let r=Qo(e.getShadowRoot(),`#processButton`);r.disabled=!0,e.getUIStateManager().hideError(),e.getUIStateManager().showProgress(),e.getUIStateManager().updateProgress(0,`Starting transcoding...`);try{let t=await Xo(n,await e.getController().getConfig(),t=>{e.getUIStateManager().updateProgress(t,`Transcoding... ${Math.round(t*100)}%`)});e.setProcessedBlob(t.blob),e.getUIStateManager().updateProgress(1,`Complete!`),setTimeout(()=>{e.getUIStateManager().hideProgress(),r.disabled=!1,e.setIsProcessing(!1)},500)}catch(n){e.getUIStateManager().hideProgress(),e.getUIStateManager().showError(t(n)),r.disabled=!1,e.setIsProcessing(!1)}}async function fs(e,t){let n=e.getAttribute(`api-key`),r=e.getNormalizedBackendUrl(e.getAttribute(`backend-url`));n&&await e.getController().uploadVideo(t,n,r,e.getUserMetadata())}function ps(e){let t=e.getShadowRoot(),n=t.querySelector(`#startCameraButton`),r=t.querySelector(`#startButton`),i=t.querySelector(`#stopButton`),a=t.querySelector(`#processButton`),o=t.querySelector(`#muteButton`),s=t.querySelector(`#switchSourceButton`),c=t.querySelector(`#settingsButton`),l=t.querySelector(`#pauseButton`),u=t.querySelector(`#resumeButton`),d=t.querySelector(`#cameraSelect`),f=t.querySelector(`#micSelect`);if(!(n&&r&&i&&a&&o&&s))throw Error(`Required UI elements not found`);n.addEventListener(`click`,()=>{e.startCamera().catch(t=>{e.showError(e.extractErrorMessage(t))})}),r.addEventListener(`click`,()=>{e.startRecording().catch(t=>{e.showError(e.extractErrorMessage(t))})}),i.addEventListener(`click`,()=>{e.stopRecording().catch(t=>{e.showError(e.extractErrorMessage(t))})}),a&&(a.style.display=`none`,a.addEventListener(`click`,()=>{e.processVideo().catch(t=>{e.showError(e.extractErrorMessage(t))})})),o.addEventListener(`click`,()=>{e.toggleMute()}),s.addEventListener(`click`,()=>{e.toggleSource().catch(t=>{e.showError(e.extractErrorMessage(t))})}),c&&c.addEventListener(`click`,()=>{e.toggleSettings()}),l&&l.addEventListener(`click`,()=>{e.pauseRecording()}),u&&u.addEventListener(`click`,()=>{e.resumeRecording()}),d&&d.addEventListener(`change`,t=>{let n=t.target;e.handleCameraChange(n.value).catch(t=>{e.showError(e.extractErrorMessage(t))})}),f&&f.addEventListener(`change`,t=>{let n=t.target;e.handleMicChange(n.value).catch(t=>{e.showError(e.extractErrorMessage(t))})})}let ms=/[:.]/g;function hs(e){let t=URL.createObjectURL(e),n=document.createElement(`a`),r=new Date().toISOString().replace(ms,`-`).slice(0,-5);n.href=t,n.download=`vidtreo-recording-${r}.mp4`,document.body.appendChild(n),n.click(),document.body.removeChild(n),URL.revokeObjectURL(t)}function gs(e){let t=URL.createObjectURL(e),n=window.open();if(!n)throw Error(`Failed to open video player window`);n.document.write(`
|
|
8143
|
+
`],{type:`application/javascript`});return To=URL.createObjectURL(e),To}var Do=class{constructor(){this.worker=null,this.chunks=[],this.totalSize=0,this.isActive=!1,this.videoTrackClone=null,this.audioTrackClone=null,this.isMuted=!1,this.currentVideoTrack=null,this.isPaused=!1,this.overlayConfig=null,this.readyPromiseResolve=null,this.setupWorker()}setupWorker(){if(typeof Worker>`u`)throw y.error(`[WorkerProcessor] Web Workers are not supported`),Error(`Web Workers are not supported`);try{y.debug(`[WorkerProcessor] Setting up worker`);let e=Eo();y.debug(`[WorkerProcessor] Worker URL created`,{urlType:typeof e,isBlobUrl:e.startsWith(`blob:`)}),this.worker=new Worker(e,{type:`classic`}),y.debug(`[WorkerProcessor] Worker created successfully`),this.worker.onmessage=this.handleWorkerMessage.bind(this),this.worker.onerror=this.handleWorkerError.bind(this),y.debug(`[WorkerProcessor] Worker event handlers attached`)}catch(e){let t=e instanceof Error?e.message:String(e);throw y.error(`[WorkerProcessor] Failed to create worker:`,e),Error(`Failed to create worker: ${t}`)}}handleWorkerMessage(e){let t=e.data;switch(t.type){case`ready`:y.debug(`[WorkerProcessor] Worker ready`),this.readyPromiseResolve&&=(this.readyPromiseResolve(),null);break;case`error`:y.error(`[WorkerProcessor] Worker error:`,t.error),this.onError&&this.onError(Error(t.error));break;case`chunk`:this.chunks.push({data:t.data,position:t.position}),this.totalSize=Math.max(this.totalSize,t.position+t.data.length);break;case`bufferUpdate`:this.onBufferUpdate&&this.onBufferUpdate(t.size,t.formatted);break;case`stateChange`:y.debug(`[WorkerProcessor] State changed:`,t.state),this.isPaused=t.state===`paused`;break;default:y.warn(`[WorkerProcessor] Unknown response type:`,t)}}handleWorkerError(e){y.error(`[WorkerProcessor] Worker error event:`,{message:e.message,filename:e.filename,lineno:e.lineno,colno:e.colno,error:e.error}),this.onError&&this.onError(Error(e.message||`Unknown worker error`))}async startProcessing(e,t,n){if(!this.worker)throw Error(`Worker not initialized`);if(this.isActive)throw Error(`Processing already active`);this.isActive=!0,this.isMuted=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0,this.overlayConfig=n||null;let r=t.format||`mp4`,a=t.audioCodec||i(r),o=wo(e),s=t.fps;y.debug(`[WorkerProcessor] Starting processing`,{isScreenCapture:o,targetFps:s,originalFps:t.fps});let c={width:t.width,height:t.height,fps:s,bitrate:t.bitrate,audioCodec:a,audioBitrate:t.audioBitrate,codec:`avc`,keyFrameInterval:5,format:r},l=e.getVideoTracks(),u=e.getAudioTracks();y.debug(`[WorkerProcessor] Preparing to start processing`,{videoTracksCount:l.length,audioTracksCount:u.length,hasWorker:!!this.worker});let d=null,f=null;if(l.length>0){this.stopCurrentVideoTrack();let e=l[0];d=this.cloneVideoTrack(e),this.currentVideoTrack=d}if(u.length>0){let e=u[0];f=this.cloneAudioTrack(e)}y.debug(`[WorkerProcessor] Track details`,{hasVideoTrack:!!d,videoTrackId:d?.id,videoTrackKind:d?.kind,videoTrackReadyState:d?.readyState,hasAudioTrack:!!f,audioTrackId:f?.id,audioTrackKind:f?.kind,audioTrackReadyState:f?.readyState});let p=d===null?null:new MediaStreamTrackProcessor({track:d}).readable,m=f===null?null:new MediaStreamTrackProcessor({track:f}).readable,h={type:`start`,videoStream:p,audioStream:m,config:c,overlayConfig:this.overlayConfig||void 0},g=[];p&&g.push(p),m&&g.push(m),y.debug(`[WorkerProcessor] Posting message to worker`,{transferablesCount:g.length,messageType:h.type});let _=new Promise(e=>{this.readyPromiseResolve=e});try{this.worker.postMessage(h,g),y.debug(`[WorkerProcessor] Message posted successfully`),await _,y.debug(`[WorkerProcessor] Worker confirmed ready`)}catch(e){throw y.error(`[WorkerProcessor] Failed to post message:`,e),this.readyPromiseResolve=null,e}}pause(){this.worker&&this.isActive&&this.worker.postMessage({type:`pause`})}resume(){this.isWorkerActive()&&this.worker&&this.worker.postMessage({type:`resume`})}isWorkerActive(){return!!(this.worker&&this.isActive)}toggleMute(){this.isMuted=!this.isMuted,this.onMuteStateChange&&this.onMuteStateChange(this.isMuted),this.isWorkerActive()&&this.worker&&this.worker.postMessage({type:`toggleMute`})}switchVideoSource(e){if(!(this.isWorkerActive()&&this.worker))return y.debug(`[WorkerProcessor] Cannot switch source - worker not active`,{hasWorker:!!this.worker,isActive:this.isActive}),Promise.resolve();let t=e.getVideoTracks();if(y.debug(`[WorkerProcessor] Switching video source`,{videoTracksCount:t.length}),t.length===0)return y.warn(`[WorkerProcessor] No video tracks in new stream`),Promise.resolve();let n=wo(e);y.debug(`[WorkerProcessor] Source type detected`,{isScreenCapture:n,targetFps:30}),this.worker.postMessage({type:`updateFps`,fps:30});let r={type:`updateSourceType`,isScreenCapture:n};this.worker.postMessage(r);let i=t[0];this.stopCurrentVideoTrack();let a=this.cloneVideoTrack(i);y.debug(`[WorkerProcessor] New video track details`,{trackId:a.id,trackKind:a.kind,trackReadyState:a.readyState}),this.currentVideoTrack=a;let o={type:`switchSource`,videoStream:new MediaStreamTrackProcessor({track:a}).readable};try{y.debug(`[WorkerProcessor] Posting switch source message`);let e=[];return o.videoStream&&e.push(o.videoStream),this.worker.postMessage(o,e),y.debug(`[WorkerProcessor] Switch source message posted`),new Promise(e=>{setTimeout(()=>{e()},0)})}catch(e){throw y.error(`[WorkerProcessor] Failed to switch source:`,e),e}}finalize(){if(!this.isWorkerActive())throw Error(`Processing not active`);return new Promise((e,t)=>{let n=setTimeout(()=>{t(Error(`Finalize timeout`))},3e4),r=i=>{let a=i.data;a.type===`stateChange`&&a.state===`stopped`?(this.worker&&this.worker.removeEventListener(`message`,r),clearTimeout(n),this.isActive=!1,e(this.createBlobFromChunks())):a.type===`error`&&(this.worker&&this.worker.removeEventListener(`message`,r),clearTimeout(n),t(Error(a.error)))};this.worker&&(this.worker.addEventListener(`message`,r),this.worker.postMessage({type:`stop`}))})}createBlobFromChunks(){let e=[...this.chunks].sort((e,t)=>e.position-t.position),t=new ArrayBuffer(this.totalSize),n=new Uint8Array(t);for(let t of e)n.set(t.data,t.position);return{blob:new Blob([t],{type:`video/mp4`}),totalSize:this.totalSize}}cancel(){return this.worker&&this.isActive&&this.worker.postMessage({type:`stop`}),this.isActive=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0,Promise.resolve()}getBufferSize(){return this.totalSize}getMutedState(){return this.isMuted}updateTabVisibility(e,t){if(!(this.isWorkerActive()&&this.worker)){y.warn(`[WorkerProcessor] Cannot update visibility - worker not active`,{isActive:this.isActive,hasWorker:!!this.worker});return}y.debug(`[WorkerProcessor] Sending visibility update to worker`,{isHidden:e,timestamp:t,timestampSeconds:t/1e3});let n={type:`updateVisibility`,isHidden:e,timestamp:t};this.worker.postMessage(n)}updateSourceType(e){if(!(this.isWorkerActive()&&this.worker)){y.warn(`[WorkerProcessor] Cannot update source type - worker not active`,{isActive:this.isActive,hasWorker:!!this.worker});return}y.debug(`[WorkerProcessor] Sending source type update to worker`,{isScreenCapture:e});let t={type:`updateSourceType`,isScreenCapture:e};this.worker.postMessage(t)}isPausedState(){return this.isPaused}getClonedAudioTrack(){return this.audioTrackClone}getAudioStreamForAnalysis(){return this.audioTrackClone?new MediaStream([this.audioTrackClone]):null}setOnBufferUpdate(e){this.onBufferUpdate=e}setOnError(e){this.onError=e}setOnMuteStateChange(e){this.onMuteStateChange=e}cloneVideoTrack(e){if(y.debug(`[WorkerProcessor] Original video track:`,{id:e.id,kind:e.kind,readyState:e.readyState,hasClone:typeof e.clone==`function`}),typeof e.clone==`function`)try{let t=e.clone();return this.videoTrackClone=t,y.debug(`[WorkerProcessor] Video track cloned successfully:`,{id:t.id,kind:t.kind,readyState:t.readyState}),t}catch(e){throw y.error(`[WorkerProcessor] Failed to clone video track:`,e),Error(`Failed to clone video track: ${e instanceof Error?e.message:String(e)}`)}return y.warn(`[WorkerProcessor] Video track clone() not available, using original`),this.videoTrackClone=e,e}cloneAudioTrack(e){if(y.debug(`[WorkerProcessor] Original audio track:`,{id:e.id,kind:e.kind,readyState:e.readyState,hasClone:typeof e.clone==`function`}),typeof e.clone==`function`)try{let t=e.clone();return this.audioTrackClone=t,y.debug(`[WorkerProcessor] Audio track cloned successfully:`,{id:t.id,kind:t.kind,readyState:t.readyState}),t}catch(e){throw y.error(`[WorkerProcessor] Failed to clone audio track:`,e),Error(`Failed to clone audio track: ${e instanceof Error?e.message:String(e)}`)}return y.warn(`[WorkerProcessor] Audio track clone() not available, using original`),this.audioTrackClone=e,e}stopCurrentVideoTrack(){this.currentVideoTrack&&this.currentVideoTrack.readyState===`live`&&this.currentVideoTrack.stop(),this.currentVideoTrack=null}cleanup(){this.worker&&=(this.worker.terminate(),null),To&&=(URL.revokeObjectURL(To),null),this.isActive=!1,this.isPaused=!1,this.chunks=[],this.totalSize=0}static isSupported(){return typeof Worker<`u`&&typeof MediaStreamTrackProcessor<`u`&&typeof VideoFrame<`u`&&typeof AudioData<`u`&&typeof OffscreenCanvas<`u`}},Oo=class{constructor(){if(this.currentVideoStream=null,!Do.isSupported())throw Error(`Web Workers are required for video processing. Please use a modern browser that supports Web Workers, MediaStreamTrackProcessor, VideoFrame, and AudioData APIs.`);try{this.workerProcessor=new Do,y.debug(`[StreamProcessor] Using worker-based processing`)}catch(e){let t=e instanceof Error?e.message:String(e);throw Error(`Failed to initialize worker: ${t}. Web Workers are required for video processing.`)}}async startProcessing(e,t,n){this.workerProcessor.setOnBufferUpdate((e,t)=>{y.debug(`[StreamProcessor] Buffer update:`,{size:e,formatted:t})}),this.workerProcessor.setOnError(e=>{y.error(`[StreamProcessor] Worker error:`,e)}),this.currentVideoStream=e,await this.workerProcessor.startProcessing(e,t,n)}updateTabVisibility(e,t){this.workerProcessor.updateTabVisibility(e,t)}updateSourceType(e){this.workerProcessor.updateSourceType(e)}pause(){this.workerProcessor.pause()}resume(){this.workerProcessor.resume()}isPausedState(){return this.workerProcessor.isPausedState()}async finalize(){return y.debug(`[StreamProcessor] finalize called`),await this.workerProcessor.finalize()}toggleMute(){this.workerProcessor.toggleMute()}isMutedState(){return this.workerProcessor.getMutedState()}getClonedAudioTrack(){return this.workerProcessor.getClonedAudioTrack()}getAudioStreamForAnalysis(){return this.workerProcessor.getAudioStreamForAnalysis()}async switchVideoSource(e){await this.workerProcessor.switchVideoSource(e),this.currentVideoStream=e,this.onSourceChange&&this.onSourceChange(e)}getCurrentVideoSource(){return this.currentVideoStream}getBufferSize(){return this.workerProcessor.getBufferSize()}setOnMuteStateChange(e){this.workerProcessor.setOnMuteStateChange(e)}setOnSourceChange(e){this.onSourceChange=e}async cancel(){await this.workerProcessor.cancel(),this.workerProcessor.cleanup(),this.currentVideoStream=null}};let ko=1e3,Ao=`recording`,jo=`idle`;var Mo=class{constructor(e,t){this.recordingState=jo,this.countdownDuration=5e3,this.countdownRemaining=0,this.countdownTimeoutId=null,this.countdownIntervalId=null,this.countdownStartTime=null,this.isPaused=!1,this.maxRecordingTime=null,this.maxTimeTimer=null,this.recordingSeconds=0,this.recordingIntervalId=null,this.pauseStartTime=null,this.totalPausedTime=0,this.streamProcessor=null,this.originalCameraStream=null,this.enableTabVisibilityOverlay=!1,this.streamManager=e,this.callbacks=t}setCountdownDuration(e){this.countdownDuration=e}setMaxRecordingTime(e){this.maxRecordingTime=e}setTabVisibilityOverlayConfig(e,t){this.enableTabVisibilityOverlay=e,this.tabVisibilityOverlayText=t}getRecordingState(){return this.recordingState}isPausedState(){return this.isPaused}getRecordingSeconds(){return this.recordingSeconds}getStreamProcessor(){return this.streamProcessor}updateSourceType(e){this.recordingState!==Ao||!this.streamProcessor||this.streamProcessor.updateSourceType(e)}setOriginalCameraStream(e){this.originalCameraStream=e}getOriginalCameraStream(){return this.originalCameraStream}async startRecording(){try{this.callbacks.onClearUploadStatus(),this.countdownDuration>0?this.startCountdown():await this.doStartRecording()}catch(e){this.handleError(e),this.recordingState=jo,this.cancelCountdown()}}startCountdown(){this.recordingState=`countdown`,this.countdownRemaining=Math.ceil(this.countdownDuration/ko),this.countdownStartTime=Date.now(),this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining),this.countdownIntervalId=window.setInterval(()=>{if(!this.countdownStartTime)return;let e=Date.now()-this.countdownStartTime;this.countdownRemaining=Math.max(0,Math.ceil((this.countdownDuration-e)/ko)),this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining)},100),this.countdownTimeoutId=window.setTimeout(async()=>{await this.doStartRecording().catch(()=>{})},this.countdownDuration)}async doStartRecording(){y.debug(`[RecordingManager] doStartRecording called`),this.cancelCountdown(),this.recordingState=Ao,this.callbacks.onStateChange(this.recordingState),this.resetRecordingState();let e=this.streamManager.getStream();if(y.debug(`[RecordingManager] Current stream:`,{hasStream:!!e,audioTracks:e?.getAudioTracks().length||0,videoTracks:e?.getVideoTracks().length||0}),!e){y.warn(`[RecordingManager] No stream available`),this.handleError(Error(`No stream available for recording`)),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState);return}this.originalCameraStream=e,y.debug(`[RecordingManager] Creating new StreamProcessor`),this.streamProcessor=new Oo,y.debug(`[RecordingManager] StreamProcessor created:`,!!this.streamProcessor);let t=await this.callbacks.onGetConfig().then(e=>({config:e,error:null})).catch(e=>({config:null,error:e}));if(t.error){this.handleError(t.error),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState);return}if(!t.config){this.handleError(Error(`Failed to get recording config`)),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState);return}y.debug(`[RecordingManager] Starting recording with stream manager`);let n=await this.streamManager.startRecording(this.streamProcessor,t.config,this.enableTabVisibilityOverlay,this.tabVisibilityOverlayText).then(()=>(y.info(`[RecordingManager] Recording started successfully`),null)).catch(e=>(y.error(`[RecordingManager] Error starting recording:`,e),e));if(n){this.handleError(n),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState);return}this.startRecordingTimer(),this.maxRecordingTime&&this.maxRecordingTime>0&&(this.maxTimeTimer=window.setTimeout(async()=>{this.recordingState===Ao&&await this.stopRecording()},this.maxRecordingTime))}async stopRecording(){y.debug(`[RecordingManager] stopRecording called`);try{this.cancelCountdown(),this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.clearTimer(this.maxTimeTimer,clearTimeout),this.maxTimeTimer=null,this.resetPauseState(),this.callbacks.onStopAudioTracking(),y.debug(`[RecordingManager] Stopping recording in stream manager`);let e=(await this.streamManager.stopRecording()).blob;return y.info(`[RecordingManager] Recording stopped, blob size:`,e.size),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState),this.recordingSeconds=0,this.streamProcessor=null,this.callbacks.onRecordingComplete(e),e}catch(e){throw this.handleError(e),this.recordingState=jo,this.callbacks.onStateChange(this.recordingState),e}}pauseRecording(){this.recordingState!==Ao||this.isPaused||(this.streamManager.pauseRecording(),this.isPaused=!0,this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.pauseStartTime=Date.now())}resumeRecording(){this.recordingState!==Ao||!this.isPaused||(this.streamManager.resumeRecording(),this.isPaused=!1,this.updatePausedDuration(),this.startRecordingTimer())}cancelCountdown(){this.clearTimer(this.countdownTimeoutId,clearTimeout),this.countdownTimeoutId=null,this.clearTimer(this.countdownIntervalId,clearInterval),this.countdownIntervalId=null,this.recordingState=jo,this.countdownRemaining=0,this.countdownStartTime=null,this.callbacks.onCountdownUpdate(this.recordingState,this.countdownRemaining)}cleanup(){this.cancelCountdown(),this.clearTimer(this.recordingIntervalId,clearInterval),this.recordingIntervalId=null,this.clearTimer(this.maxTimeTimer,clearTimeout),this.maxTimeTimer=null}resetRecordingState(){this.isPaused=!1,this.recordingSeconds=0,this.totalPausedTime=0,this.pauseStartTime=null}resetPauseState(){this.isPaused=!1,this.pauseStartTime=null,this.totalPausedTime=0}updatePausedDuration(){if(this.pauseStartTime===null)throw Error(`Pause start time not set`);let e=Date.now()-this.pauseStartTime;this.totalPausedTime+=e,this.pauseStartTime=null}startRecordingTimer(){this.recordingIntervalId===null&&(this.recordingIntervalId=window.setInterval(()=>{this.recordingSeconds+=1,this.callbacks.onTimerUpdate(he(this.recordingSeconds))},1e3))}clearTimer(e,t){e!==null&&t(e)}handleError(e){let n=e instanceof Error?e:Error(t(e));this.callbacks.onError(n)}};let No=()=>{},Po=()=>{},Fo=()=>{},Io=()=>{},Lo=e=>{},Ro=(e,t)=>{},zo=e=>{},Bo=e=>{},Vo=e=>{},Ho=()=>{},Uo=e=>{};function Wo(){return{onProgress:No,onSuccess:Po,onError:Fo,onClearStatus:Io}}var Go=class{constructor(e={}){this.uploadService=null,this.uploadQueueManager=null,this.isInitialized=!1,this.enableTabVisibilityOverlay=!1,this.callbacks=e,this.streamManager=new xe,this.configManager=new p,this.storageManager=new ee,this.deviceManager=new m(this.streamManager,e.device),this.audioLevelAnalyzer=new n,this.uploadService=new Ce,this.uploadCallbacks=e.upload?e.upload:Wo();let t=this.createRecordingCallbacks(e);this.recordingManager=new Mo(this.streamManager,t);let r=this.createSourceSwitchCallbacks(e);this.sourceSwitchManager=new ce(this.streamManager,r),e.stream&&(this.streamManager.on(`streamstart`,({stream:t})=>{y.debug(`[RecorderController] streamstart event received, calling callback`),e.stream?.onStreamStart&&e.stream.onStreamStart(t)}),this.streamManager.on(`streamstop`,()=>{y.debug(`[RecorderController] streamstop event received, calling callback`),e.stream?.onStreamStop&&e.stream.onStreamStop()}),this.streamManager.on(`error`,({error:t})=>{y.error(`[RecorderController] stream error event received, calling callback`,t),e.stream?.onError&&e.stream.onError(t)}))}async initialize(e){if(this.isInitialized)return;e.apiKey&&e.backendUrl&&await this.configManager.initialize(e.apiKey,e.backendUrl),e.countdownDuration!==void 0&&this.recordingManager.setCountdownDuration(e.countdownDuration),e.maxRecordingTime!==void 0&&this.recordingManager.setMaxRecordingTime(e.maxRecordingTime),e.enableTabVisibilityOverlay!==void 0&&(this.enableTabVisibilityOverlay=e.enableTabVisibilityOverlay),e.tabVisibilityOverlayText!==void 0&&(this.tabVisibilityOverlayText=e.tabVisibilityOverlayText),this.recordingManager.setTabVisibilityOverlayConfig(this.enableTabVisibilityOverlay,this.tabVisibilityOverlayText);let t=this.callbacks.onStorageCleanupError??Uo;await this.storageManager.initialize(t);let n=this.storageManager.getStorageService();n&&this.uploadService&&(this.uploadQueueManager=new Se(n,this.uploadService),this.uploadQueueManager.setCallbacks({onUploadProgress:(e,t)=>{this.uploadCallbacks.onProgress(t)},onUploadComplete:(e,t)=>{this.uploadCallbacks.onSuccess(t)},onUploadError:(e,t)=>{this.uploadCallbacks.onError(t)}})),this.isInitialized=!0}async startStream(){y.debug(`[RecorderController] startStream called`),await this.streamManager.startStream(),y.debug(`[RecorderController] startStream completed`)}async stopStream(){await this.streamManager.stopStream()}switchVideoDevice(e){return this.streamManager.switchVideoDevice(e)}switchAudioDevice(e){return this.streamManager.switchAudioDevice(e)}async startRecording(){await this.recordingManager.startRecording()}async stopRecording(){let e=await this.recordingManager.stopRecording();return await this.sourceSwitchManager.handleRecordingStop().catch(()=>{throw Error(`Source switch cleanup failed`)}),e}getTabVisibilityOverlayConfig(){return{enabled:this.enableTabVisibilityOverlay,text:this.tabVisibilityOverlayText}}pauseRecording(){this.recordingManager.pauseRecording()}resumeRecording(){this.recordingManager.resumeRecording()}async switchSource(e){await this.sourceSwitchManager.toggleSource()}setCameraDevice(e){this.deviceManager.setCameraDevice(e)}setMicDevice(e){this.deviceManager.setMicDevice(e)}getAvailableDevices(){return this.deviceManager.getAvailableDevices()}muteAudio(){this.streamManager.muteAudio()}unmuteAudio(){this.streamManager.unmuteAudio()}toggleMute(){this.streamManager.toggleMute()}getIsMuted(){return this.streamManager.isMuted()}startAudioLevelTracking(e,t){if(!t)throw Error(`Audio level callbacks are required`);return this.audioLevelAnalyzer.startTracking(e,t,()=>this.streamManager.isMuted()),Promise.resolve()}stopAudioLevelTracking(){this.audioLevelAnalyzer.stopTracking()}getAudioLevel(){return this.audioLevelAnalyzer.getAudioLevel()}async uploadVideo(e,t,n,r){if(!this.uploadQueueManager)throw Error(`Upload queue manager not initialized`);this.uploadCallbacks.onClearStatus();let i=await So(e),a=Object.keys(r).length>0?r:void 0;await this.uploadQueueManager.queueUpload({blob:e,apiKey:t,backendUrl:n,filename:`recording-${Date.now()}.mp4`,duration:i,metadata:void 0,userMetadata:a})}getStream(){return this.streamManager.getStream()}getRecordingState(){return this.recordingManager.getRecordingState()}isPaused(){return this.recordingManager.isPausedState()}getCurrentSourceType(){return this.sourceSwitchManager.getCurrentSourceType()}getOriginalCameraStream(){return this.sourceSwitchManager.getOriginalCameraStream()}getStreamManager(){return this.streamManager}getAudioStreamForAnalysis(){return this.streamManager.getAudioStreamForAnalysis()}getDeviceManager(){return this.deviceManager}getConfig(){return this.configManager.getConfig()}getUploadService(){return this.uploadService}isRecording(){return this.streamManager.isRecording()}isActive(){return this.streamManager.isActive()}cleanup(){this.storageManager.destroy(),this.recordingManager.cleanup(),this.audioLevelAnalyzer.stopTracking(),this.sourceSwitchManager.cleanup()}createRecordingCallbacks(e){let t=e.recording;return{onStateChange:t?.onStateChange??Lo,onCountdownUpdate:t?.onCountdownUpdate??Ro,onTimerUpdate:t?.onTimerUpdate??zo,onError:t?.onError??Bo,onRecordingComplete:t?.onRecordingComplete??Vo,onClearUploadStatus:t?.onClearUploadStatus??Ho,onStopAudioTracking:()=>{this.audioLevelAnalyzer.stopTracking()},onGetConfig:()=>this.configManager.getConfig()}}createSourceSwitchCallbacks(e){let t=e.sourceSwitch;return{onSourceChange:t?.onSourceChange,onPreviewUpdate:t?.onPreviewUpdate,onError:t?.onError,onTransitionStart:t?.onTransitionStart,onTransitionEnd:t?.onTransitionEnd,onScreenSelectionStart:()=>{this.isRecording()&&this.recordingManager.updateSourceType(!0)},onScreenSelectionEnd:()=>{this.isRecording()&&this.recordingManager.updateSourceType(!1)},getSelectedCameraDeviceId:()=>this.deviceManager.getSelectedCameraDeviceId(),getSelectedMicDeviceId:()=>this.deviceManager.getSelectedMicDeviceId()}}};function Ko(e){if(typeof e==`string`)return new Lr(e);if(e instanceof Blob)return new Fr(e);throw Error(`Invalid input type. Expected Blob, File, or file path string.`)}function qo(e){switch(e){case`mp4`:return new Va;case`webm`:case`mkv`:case`mov`:throw Error(`Format ${e} is not yet supported. Only MP4 is currently supported.`);default:throw Error(`Unsupported output format: ${e}`)}}function Jo(e){switch(e){case`mp4`:return`video/mp4`;case`webm`:return`video/webm`;case`mkv`:return`video/x-matroska`;case`mov`:return`video/quicktime`;default:throw Error(`Unsupported output format: ${e}`)}}function Yo(e){let t=a(e.format,e.audioCodec);return{video:{width:e.width,height:e.height,fit:`contain`,frameRate:e.fps,bitrate:e.bitrate,forceTranscode:!0},audio:{codec:t,forceTranscode:!0}}}function Xo(e){if(!e.isValid){let t=e.discardedTracks.map(e=>e.reason).join(`, `);throw Error(`Conversion is invalid. Discarded tracks: ${t}`)}}async function Zo(e,t={},n){let r={...o,...t,format:t.format||o.format};r.audioCodec||=i(r.format);let a=Ko(e),s=new Vr({formats:[jr],source:a}),c=new ho({format:qo(r.format),target:new Pa}),l=await yo.init({input:s,output:c,...Yo(r)});Xo(l),n&&(l.onProgress=n),await l.execute();let u=c.target.buffer;if(!u)throw Error(`Transcoding completed but no output buffer was generated`);let d=Jo(r.format);return{buffer:u,blob:new Blob([u],{type:d})}}function Qo(e){if(e<.25){let t=e/.25;return`rgb(255, ${Math.round(165+50*t)}, 0)`}if(e<.5){let t=(e-.25)/.25;return`rgb(${Math.round(255- -205*t)}, ${Math.round(215+-10*t)}, ${Math.round(0+50*t)})`}if(e<.75){let t=(e-.5)/.25;return`rgb(${Math.round(50- -50*t)}, ${Math.round(205+-77*t)}, ${Math.round(50+78*t)})`}let t=(e-.75)/.25;return`rgb(0, ${Math.round(128- -28*t)}, ${Math.round(128+72*t)})`}function $o(e,t){let n=e.querySelector(t);if(!n)throw Error(`Element not found: ${t}`);return n}async function es(e,n,r){e.pause(),e.srcObject=null,e.load(),r===`screen`?e.classList.add(`vidtreo-screen-share`):e.classList.remove(`vidtreo-screen-share`),await new Promise(e=>{setTimeout(()=>e(),100)}),e.srcObject=n,await new Promise((n,r)=>{let i=setTimeout(()=>{a||(a=!0,o(),r(Error(`Timeout waiting for video to load`)))},1e4),a=!1,o=()=>{a||!e||(e.removeEventListener(`loadedmetadata`,f),e.removeEventListener(`loadeddata`,p),e.removeEventListener(`canplay`,m),e.removeEventListener(`playing`,h),e.removeEventListener(`error`,g))},s=()=>{a||(a=!0,clearTimeout(i),o(),n())},c=e=>{a||(a=!0,clearTimeout(i),o(),r(Error(`Failed to play preview video: ${e}`)))},l=async()=>{if(!(a||!e))try{await e.play(),s()}catch(e){c(`Failed to play preview video: ${t(e)}`)}},u=n=>{if(a||!e)return;let r=t(n);r.includes(`interrupted`)||r.includes(`abort`)?setTimeout(l,200):c(r)},d=async()=>{if(!(a||!e))try{await e.play(),s()}catch(e){u(e)}},f=()=>{a||!e||d()},p=()=>{a||!e||e.readyState>=2&&d()},m=()=>{a||!e||e.readyState>=3&&d()},h=()=>{a||!e||(a=!0,clearTimeout(i),o(),n())},g=e=>{if(a)return;a=!0,clearTimeout(i),o();let n=e instanceof ErrorEvent?e.error:Error(`Video element error`);r(Error(`Failed to load preview: ${t(n)}`))};if(e.readyState>=1){d();return}e.addEventListener(`loadedmetadata`,f,{once:!0}),e.addEventListener(`loadeddata`,p,{once:!0}),e.addEventListener(`canplay`,m,{once:!0}),e.addEventListener(`playing`,h,{once:!0}),e.addEventListener(`error`,g,{once:!0}),setTimeout(()=>{!a&&e&&e.readyState>=1&&d()},500)})}async function ts(e,t){e.pause(),e.srcObject=t,await new Promise(t=>{let n=()=>{e.removeEventListener(`loadedmetadata`,n),t()};e.addEventListener(`loadedmetadata`,n),e.load(),setTimeout(()=>t(),500)});try{await e.play()}catch(e){let t=e instanceof Error?e.message:String(e);if(!(t.includes(`abort`)||t.includes(`interrupted`)))throw Error(`Failed to play preview after device switch: ${t}`)}}function ns(e){return{recording:{onStateChange:()=>{},onCountdownUpdate:(t,n)=>{e.getUIStateManager().updateCountdownOverlay(t,n)},onTimerUpdate:t=>{e.getUIStateManager().updateRecordingTimer(t)},onError:t=>{e.getUIStateManager().showError(t.message)},onRecordingComplete:e=>{},onClearUploadStatus:()=>{e.getUIStateManager().clearUploadStatus()},onStopAudioTracking:()=>{},onGetConfig:async()=>({format:`mp4`,fps:30,width:1280,height:720,bitrate:25e5,audioCodec:`aac`,preset:`medium`,packetCount:0,audioBitrate:128e3})},upload:{onProgress:t=>{e.getUIStateManager().showUploadProgress(),e.getUIStateManager().updateUploadProgress(t)},onSuccess:t=>{e.getUIStateManager().hideUploadProgress(),e.getUIStateManager().showUploadSuccess(t)},onError:t=>{e.getUIStateManager().hideUploadProgress(),e.getUIStateManager().showUploadError(t.message)},onClearStatus:()=>{e.getUIStateManager().clearUploadStatus()}},sourceSwitch:{onSourceChange:async t=>{e.getUIStateManager().updateSwitchButtonText(t);let n=e.getController();if(t===`camera`){let t=n.getStream();if(!t||t.getAudioTracks().length===0)return;n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}).catch(()=>{})}},onPreviewUpdate:async t=>{let n=e.getController(),r=n.getCurrentSourceType();if(r===`screen`){let t=n.getAudioStreamForAnalysis();if(!t||t.getAudioTracks().length===0)return;n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}).catch(()=>{})}else t.getAudioTracks().length>0&&(n.stopAudioLevelTracking(),await n.startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}));await es($o(e.getShadowRoot(),`#videoPreview`),t,r)},onError:n=>{e.getUIStateManager().showError(t(n))},onTransitionStart:t=>{e.getUIStateManager().showSourceTransition(t)},onTransitionEnd:()=>{e.getUIStateManager().hideSourceTransition()}},storage:{onUploadProgress:()=>{},onUploadComplete:()=>{e.checkPendingUploads()},onUploadError:()=>{e.checkPendingUploads()}},onStorageCleanupError:t=>{e.getUIStateManager().showError(t)}}}function rs(e,t){let n=e.querySelector(`#previewSkeleton`),r=e.querySelector(`.vidtreo-skeleton-text`);if(!n)throw Error(`Preview skeleton element not found`);if(n.style.display=`flex`,!r)throw Error(`Skeleton text element not found`);r.textContent=t}function is(e){let t=e.querySelector(`#previewSkeleton`);if(!t)throw Error(`Preview skeleton element not found`);t.style.display=`none`}async function as(e,n){let r=n===`default`?null:n;if(e.getController().setCameraDevice(r),e.getDeviceManager().setCameraDevice(r),!(!e.getController().isActive()||e.getController().isRecording())){rs(e.getShadowRoot(),e.getI18nManager().t(`switchingCamera`));try{let t=await e.getController().switchVideoDevice(r);await ts($o(e.getShadowRoot(),`#videoPreview`),t),is(e.getShadowRoot())}catch(n){is(e.getShadowRoot()),e.showError(t(n));let r=e.getDeviceManager().getSelectedCameraDeviceId(),i=e.getShadowRoot().querySelector(`#cameraSelect`);if(!i)throw Error(`Camera select element not found`);if(!r)throw Error(`Camera device ID is required`);i.value=r}}}async function os(e,n){let r=n===`default`?null:n;if(e.getController().setMicDevice(r),e.getDeviceManager().setMicDevice(r),!(!e.getController().isActive()||e.getController().isRecording())){rs(e.getShadowRoot(),e.getI18nManager().t(`switchingMicrophone`));try{await e.getController().switchAudioDevice(r);let t=e.getController().getStream();if(!t)throw Error(`Stream is required`);e.getController().stopAudioLevelTracking(),await e.getController().startAudioLevelTracking(t,{onLevelUpdate:(t,n)=>{e.getAudioLevelVisualizer().updateBars(t,n)}}),setTimeout(()=>{is(e.getShadowRoot())},200)}catch(n){is(e.getShadowRoot()),e.showError(t(n));let r=e.getDeviceManager().getSelectedMicDeviceId(),i=e.getShadowRoot().querySelector(`#micSelect`);if(!i)throw Error(`Microphone select element not found`);if(!r)throw Error(`Microphone device ID is required`);i.value=r}}}function ss(e){let t=e.getStreamManager();t.on(`statechange`,({state:t,previousState:n})=>{e.handleStateChange(t,n)}),t.on(`streamstart`,({stream:t})=>{e.handleStreamStart(t)}),t.on(`streamstop`,()=>{e.handleStreamStop()}),t.on(`recordingstart`,()=>{e.handleRecordingStart()}),t.on(`recordingstop`,({blob:t})=>{e.handleRecordingStop(t).catch(t=>{e.showError(e.extractErrorMessage(t))})}),t.on(`recordingtimeupdate`,({formatted:t})=>{e.updateRecordingTimer(t)}),t.on(`recordingbufferupdate`,()=>{}),t.on(`audiomutetoggle`,({muted:t})=>{e.updateMuteState(t)}),t.on(`videosourcechange`,({stream:t})=>{e.updateVideoPreview(t)}),t.on(`error`,({error:t})=>{e.showError(t.message)})}async function cs(e){if(e.getShowSettings()){e.setShowSettings(!1);let t=e.getShadowRoot().querySelector(`#settingsPanel`);if(!t)throw Error(`Settings panel element not found`);t.style.display=`none`}await e.getController().startRecording()}async function ls(e){let t=await e.getController().stopRecording();return e.setProcessedBlob(t),e.setRecordedBlob(t),e.getUIStateManager().updateRecordingControlsAfterStop(),await ps(e,t),t}function us(e){e.getController().pauseRecording(),e.getUIStateManager().updatePauseState(e.getController().isPaused())}function ds(e){e.getController().resumeRecording(),e.getUIStateManager().updatePauseState(e.getController().isPaused())}async function fs(e){let n=e.getRecordedBlob();if(!n)throw Error(`No recording available`);e.setIsProcessing(!0);let r=$o(e.getShadowRoot(),`#processButton`);r.disabled=!0,e.getUIStateManager().hideError(),e.getUIStateManager().showProgress(),e.getUIStateManager().updateProgress(0,`Starting transcoding...`);try{let t=await Zo(n,await e.getController().getConfig(),t=>{e.getUIStateManager().updateProgress(t,`Transcoding... ${Math.round(t*100)}%`)});e.setProcessedBlob(t.blob),e.getUIStateManager().updateProgress(1,`Complete!`),setTimeout(()=>{e.getUIStateManager().hideProgress(),r.disabled=!1,e.setIsProcessing(!1)},500)}catch(n){e.getUIStateManager().hideProgress(),e.getUIStateManager().showError(t(n)),r.disabled=!1,e.setIsProcessing(!1)}}async function ps(e,t){let n=e.getAttribute(`api-key`),r=e.getNormalizedBackendUrl(e.getAttribute(`backend-url`));n&&await e.getController().uploadVideo(t,n,r,e.getUserMetadata())}function ms(e){let t=e.getShadowRoot(),n=t.querySelector(`#startCameraButton`),r=t.querySelector(`#startButton`),i=t.querySelector(`#stopButton`),a=t.querySelector(`#processButton`),o=t.querySelector(`#muteButton`),s=t.querySelector(`#switchSourceButton`),c=t.querySelector(`#settingsButton`),l=t.querySelector(`#pauseButton`),u=t.querySelector(`#resumeButton`),d=t.querySelector(`#cameraSelect`),f=t.querySelector(`#micSelect`);if(!(n&&r&&i&&a&&o&&s))throw Error(`Required UI elements not found`);n.addEventListener(`click`,()=>{e.startCamera().catch(t=>{e.showError(e.extractErrorMessage(t))})}),r.addEventListener(`click`,()=>{e.startRecording().catch(t=>{e.showError(e.extractErrorMessage(t))})}),i.addEventListener(`click`,()=>{e.stopRecording().catch(t=>{e.showError(e.extractErrorMessage(t))})}),a&&(a.style.display=`none`,a.addEventListener(`click`,()=>{e.processVideo().catch(t=>{e.showError(e.extractErrorMessage(t))})})),o.addEventListener(`click`,()=>{e.toggleMute()}),s.addEventListener(`click`,()=>{e.toggleSource().catch(t=>{e.showError(e.extractErrorMessage(t))})}),c&&c.addEventListener(`click`,()=>{e.toggleSettings()}),l&&l.addEventListener(`click`,()=>{e.pauseRecording()}),u&&u.addEventListener(`click`,()=>{e.resumeRecording()}),d&&d.addEventListener(`change`,t=>{let n=t.target;e.handleCameraChange(n.value).catch(t=>{e.showError(e.extractErrorMessage(t))})}),f&&f.addEventListener(`change`,t=>{let n=t.target;e.handleMicChange(n.value).catch(t=>{e.showError(e.extractErrorMessage(t))})})}let hs=/[:.]/g;function gs(e){let t=URL.createObjectURL(e),n=document.createElement(`a`),r=new Date().toISOString().replace(hs,`-`).slice(0,-5);n.href=t,n.download=`vidtreo-recording-${r}.mp4`,document.body.appendChild(n),n.click(),document.body.removeChild(n),URL.revokeObjectURL(t)}function _s(e){let t=URL.createObjectURL(e),n=window.open();if(!n)throw Error(`Failed to open video player window`);n.document.write(`
|
|
7746
8144
|
<html>
|
|
7747
8145
|
<head><title>Recorded Video</title></head>
|
|
7748
8146
|
<body style="margin:0;background:#000;display:flex;align-items:center;justify-content:center;min-height:100vh;">
|
|
@@ -7751,7 +8149,7 @@ new RecorderWorker;
|
|
|
7751
8149
|
</video>
|
|
7752
8150
|
</body>
|
|
7753
8151
|
</html>
|
|
7754
|
-
`)}let _s={en:{title:`Video Recorder`,subtitle:`Record video from your camera and transcode it to MP4 format`,initializingCamera:`Initializing camera...`,grantPermissions:`Grant camera and microphone permissions when prompted`,retryCamera:`Retry Camera`,switchingDevice:`Switching device...`,recordingStartsIn:`Recording starts in...`,switchingSource:`Switching source...`,rec:`REC`,settings:`Settings`,record:`Record`,stop:`Stop`,pause:`Pause`,resume:`Resume`,mute:`Mute`,unmute:`Unmute`,switchSource:`Switch Source`,camera:`Camera`,microphone:`Microphone`,processVideo:`Process Video`,processing:`Processing...`,uploading:`Uploading...`,switchingCamera:`Switching camera...`,switchingMicrophone:`Switching microphone...`,failedToStartCamera:`Failed to start camera`},es:{title:`Grabador de Video`,subtitle:`Graba video desde tu cámara y transcodifícalo a formato MP4`,initializingCamera:`Inicializando cámara...`,grantPermissions:`Otorga permisos de cámara y micrófono cuando se solicite`,retryCamera:`Reintentar Cámara`,switchingDevice:`Cambiando dispositivo...`,recordingStartsIn:`La grabación comienza en...`,switchingSource:`Cambiando fuente...`,rec:`GRAB`,settings:`Configuración`,record:`Grabar`,stop:`Detener`,pause:`Pausar`,resume:`Reanudar`,mute:`Silenciar`,unmute:`Activar sonido`,switchSource:`Cambiar Fuente`,camera:`Cámara`,microphone:`Micrófono`,processVideo:`Procesar Video`,processing:`Procesando...`,uploading:`Subiendo...`,switchingCamera:`Cambiando cámara...`,switchingMicrophone:`Cambiando micrófono...`,failedToStartCamera:`Error al iniciar la cámara`}};var vs=class{constructor(e=`en`,t={}){this.lang=e,this.customTexts=t}setLang(e){this.lang=e}setCustomTexts(e){this.customTexts=e}t(e){return this.customTexts[e]?this.customTexts[e]:(_s[this.lang]||_s.en)[e]}getAll(){return{..._s[this.lang]||_s.en,...this.customTexts}}};let ys=.7;var bs=class{constructor(){this.barsContainer=null}initializeBars(e){this.barsContainer=this.queryElement(e,`#audioLevelBars`),this.barsContainer.innerHTML=``;for(let e=0;e<15;e++){let e=this.createBar();this.barsContainer.appendChild(e)}}updateBars(e,t){if(!this.barsContainer)throw Error(`Bars container not initialized`);this.barsContainer.querySelectorAll(`.vidtreo-audio-level-bar`).forEach((n,r)=>{this.updateBar(n,r,e,t)})}queryElement(e,t){let n=e.querySelector(t);if(!n)throw Error(`Element not found: ${t}`);return n}createBar(){let e=document.createElement(`div`);return e.className=`vidtreo-audio-level-bar`,e.style.height=`15%`,e}updateBar(e,t,n,r){let i=n>=this.calculateThreshold(t)&&!r;this.setBarHeight(e,t,i),this.setBarStyle(e,t,i,r)}calculateThreshold(e){return(e+1)/15*100}setBarHeight(e,t,n){if(n){let n=30+t/15*70;e.style.height=`${n}%`}else e.style.height=`15%`}setBarStyle(e,t,n,r){if(r||!n)e.style.backgroundColor=``,e.style.opacity=`1`;else{let n=t/15,r=ys+t/15*(1-ys);e.style.backgroundColor=Zo(n),e.style.opacity=String(r)}}},xs=class extends m{updateDeviceSelects(e){let t=e.querySelector(`#cameraSelect`),n=e.querySelector(`#micSelect`),r=this.getAvailableDevicesList(),i=this.getSelectedCameraDeviceId(),a=this.getSelectedMicDeviceId();if(t){t.innerHTML=`<option value="default">Default Camera</option>`;for(let e of r.videoinput.filter(e=>e.deviceId&&e.deviceId.trim()!==``)){let n=document.createElement(`option`);n.value=e.deviceId,n.textContent=e.label||`Camera ${e.deviceId.slice(0,8)}`,e.deviceId===i&&(n.selected=!0),t.appendChild(n)}}if(n){n.innerHTML=`<option value="default">Default Microphone</option>`;for(let e of r.audioinput.filter(e=>e.deviceId&&e.deviceId.trim()!==``)){let t=document.createElement(`option`);t.value=e.deviceId,t.textContent=e.label||`Microphone ${e.deviceId.slice(0,8)}`,e.deviceId===a&&(t.selected=!0),n.appendChild(t)}}}},Ss=class{constructor(e){this.shadowRoot=e}setShadowRoot(e){this.shadowRoot=e}queryElement(e){let t=this.shadowRoot.querySelector(e);if(!t)throw Error(`Element not found: ${e}`);return t}setText(e,t){e.textContent=t}toggleClass(e,t,n){n?e.classList.add(t):e.classList.remove(t)}setStyle(e,t,n){e.style.setProperty(t,n)}setDisplay(e,t){this.setStyle(e,`display`,t)}setDisabled(e,t){e.disabled=t}showError(e){let t=this.queryElement(`#error`);this.setText(t,e),this.toggleClass(t,`vidtreo-active`,!0)}hideError(){let e=this.queryElement(`#error`);this.toggleClass(e,`vidtreo-active`,!1)}showProgress(){let e=this.queryElement(`#progress`);this.toggleClass(e,`vidtreo-active`,!0)}hideProgress(){let e=this.queryElement(`#progress`);this.toggleClass(e,`vidtreo-active`,!1)}updateProgress(e,t){let n=Math.round(e*100),r=this.queryElement(`#progressFill`),i=this.queryElement(`#progressText`);this.setStyle(r,`width`,`${n}%`),this.setText(i,t)}updateRecordingTimer(e){let t=this.queryElement(`#recordingTimer`);this.setText(t,e)}updateMuteState(e,t,n){let r=this.queryElement(`#muteButton`),i=this.shadowRoot.querySelector(`#muteIcon`);if(!i)throw Error(`Mute icon element not found`);e?(this.toggleClass(r,`vidtreo-muted`,!0),i.innerHTML=`<path d="M211,221.31,51,45.31A4,4,0,0,0,45,50.69L84,93.55V128a44,44,0,0,0,66,38.12l16.38,18A67.21,67.21,0,0,1,128,196a68.07,68.07,0,0,1-68-68,4,4,0,0,0-8,0,76.09,76.09,0,0,0,72,75.89V240a4,4,0,0,0,8,0V203.89a75.1,75.1,0,0,0,39.79-13.77L205,226.69a4,4,0,1,0,5.92-5.38ZM128,164a36,36,0,0,1-36-36V102.35L144.43,160A35.83,35.83,0,0,1,128,164Zm61.12-6.15A67.44,67.44,0,0,0,196,128a4,4,0,0,1,8,0,75.28,75.28,0,0,1-7.7,33.37,4,4,0,0,1-7.18-3.52ZM87.63,46.46A44,44,0,0,1,172,64v64a44.2,44.2,0,0,1-.24,4.61,4,4,0,0,1-4,3.58l-.42,0a4,4,0,0,1-3.57-4.39A36.67,36.67,0,0,0,164,128V64A36,36,0,0,0,95,49.66a4,4,0,0,1-7.34-3.2Z"></path>`):(this.toggleClass(r,`vidtreo-muted`,!1),i.innerHTML=`<path d="M128,172a44.05,44.05,0,0,0,44-44V64a44,44,0,0,0-88,0v64A44.05,44.05,0,0,0,128,172ZM92,64a36,36,0,0,1,72,0v64a36,36,0,0,1-72,0Zm40,139.89V240a4,4,0,0,1-8,0V203.89A76.09,76.09,0,0,1,52,128a4,4,0,0,1,8,0,68,68,0,0,0,136,0,4,4,0,0,1,8,0A76.09,76.09,0,0,1,132,203.89Z"></path>`),t(n,e)}updatePauseState(e){let t=this.queryElement(`#pauseButton`),n=this.queryElement(`#resumeButton`);this.setDisplay(t,e?`none`:`flex`),this.setDisplay(n,e?`flex`:`none`)}updateCountdownOverlay(e,t){let n=this.queryElement(`#countdownOverlay`),r=this.queryElement(`#countdownNumber`);e===`countdown`&&t>0?(this.setDisplay(n,`flex`),this.setText(r,t.toString())):this.setDisplay(n,`none`)}showSourceTransition(e){let t=this.queryElement(`#videoPreview`);this.toggleClass(t,`vidtreo-transitioning`,!0);let n=this.queryElement(`#sourceTransitionOverlay`);this.toggleClass(n,`vidtreo-active`,!0);let r=n.querySelector(`.vidtreo-transition-message`);if(!r)throw Error(`Transition message element not found`);this.setText(r,e)}hideSourceTransition(){let e=this.queryElement(`#videoPreview`);this.toggleClass(e,`vidtreo-transitioning`,!1);let t=this.queryElement(`#sourceTransitionOverlay`);this.toggleClass(t,`vidtreo-active`,!1)}handleStreamStart(e,t){let n=this.queryElement(`#startButton`),r=this.queryElement(`#stopButton`),i=this.queryElement(`#cameraArea`),a=this.queryElement(`#startCameraArea`),o=this.queryElement(`#audioLevelBars`);if(this.setDisabled(n,!1),this.setDisplay(n,`flex`),this.setDisabled(r,!0),this.setDisplay(r,`none`),this.toggleClass(i,`vidtreo-active`,!0),this.setDisplay(a,`none`),e===`idle`){let e=this.queryElement(`#settingsButton`),t=this.queryElement(`#muteButton`),n=this.queryElement(`#pauseButton`),r=this.queryElement(`#resumeButton`),i=this.queryElement(`#switchSourceButton`);this.setDisplay(e,`flex`),this.setDisplay(t,`none`),this.setDisplay(n,`none`),this.setDisplay(r,`none`),this.setDisplay(i,`none`)}t(),this.setDisplay(o,`flex`)}handleStreamStop(e){let t=this.queryElement(`#videoPreview`);t.srcObject=null;let n=this.queryElement(`#cameraArea`),r=this.queryElement(`#audioLevelBars`);this.toggleClass(n,`vidtreo-active`,!1),e(),this.setDisplay(r,`none`)}handleRecordingStart(e){let t=this.queryElement(`#startButton`),n=this.queryElement(`#stopButton`),r=this.queryElement(`#muteButton`),i=this.queryElement(`#switchSourceButton`),a=this.queryElement(`#settingsButton`),o=this.queryElement(`#pauseButton`),s=this.queryElement(`#resumeButton`),c=this.queryElement(`#recIndicatorTop`),l=this.queryElement(`#recordingTimerRow`);this.setDisplay(t,`none`),this.setDisplay(n,`flex`),this.setDisabled(n,!1),this.setDisplay(c,`flex`),this.setDisplay(l,`flex`),this.setDisplay(a,`none`),this.setDisabled(r,!1),this.setDisplay(r,`flex`),this.setDisabled(i,!1),this.setDisplay(i,`flex`),this.setDisplay(o,e?`none`:`flex`),this.setDisplay(s,e?`flex`:`none`),this.hideError()}updateRecordingControlsAfterStop(){let e=this.queryElement(`#startButton`),t=this.queryElement(`#stopButton`),n=this.queryElement(`#processButton`),r=this.queryElement(`#muteButton`),i=this.queryElement(`#switchSourceButton`),a=this.queryElement(`#settingsButton`),o=this.queryElement(`#pauseButton`),s=this.queryElement(`#resumeButton`),c=this.queryElement(`#recIndicatorTop`),l=this.queryElement(`#recordingTimerRow`);this.setDisplay(e,`flex`),this.setDisabled(e,!1),this.setDisplay(t,`none`),this.setDisabled(t,!0),this.setDisplay(c,`none`),this.setDisplay(l,`none`),this.setDisplay(n,`none`),this.setDisplay(r,`none`),this.setDisplay(i,`none`),this.setDisplay(a,`flex`),this.setDisplay(o,`none`),this.setDisplay(s,`none`)}updateSwitchButtonText(e){let t=this.queryElement(`#switchSourceButton`),n=this.shadowRoot.querySelector(`#switchSourceIcon`);if(!n)throw Error(`Switch source icon element not found`);e===`camera`?(n.innerHTML=`<rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/>`,t.title=`Switch to Screen`):(n.innerHTML=`<path d="M17 17H4a2 2 0 0 1-2-2V5c0-1.5 1-2 1-2"/><path d="M22 15V5a2 2 0 0 0-2-2H9"/><path d="M8 21h8"/><path d="M12 17v4"/><path d="m2 2 20 20"/>`,t.title=`Switch to Camera`)}toggleSettings(e){let t=this.queryElement(`#settingsPanel`),n=!e;return this.setDisplay(t,n?`block`:`none`),n}showUploadProgress(){let e=this.queryElement(`#uploadProgress`);this.toggleClass(e,`vidtreo-active`,!0),this.updateUploadProgress(0)}updateUploadProgress(e){let t=this.queryElement(`#uploadProgressFill`),n=this.queryElement(`#uploadProgressText`),r=Math.round(e*100);this.setStyle(t,`width`,`${r}%`),this.setText(n,`Uploading... ${r}%`)}hideUploadProgress(){let e=this.queryElement(`#uploadProgress`);this.toggleClass(e,`vidtreo-active`,!1)}showUploadSuccess(e){let t=this.queryElement(`#uploadStatus`),n=this.queryElement(`#uploadStatusText`);this.toggleClass(t,`vidtreo-active`,!0),this.toggleClass(t,`vidtreo-success`,!0),this.toggleClass(t,`vidtreo-error`,!1),this.setText(n,`✅ Video uploaded successfully! Video ID: ${e.videoId}`)}showUploadError(e){let t=this.queryElement(`#uploadStatus`),n=this.queryElement(`#uploadStatusText`);this.toggleClass(t,`vidtreo-active`,!0),this.toggleClass(t,`vidtreo-error`,!0),this.toggleClass(t,`vidtreo-success`,!1),this.setText(n,`❌ Upload failed: ${e}`)}clearUploadStatus(){let e=this.queryElement(`#uploadStatus`);this.toggleClass(e,`vidtreo-active`,!1),this.toggleClass(e,`vidtreo-success`,!1),this.toggleClass(e,`vidtreo-error`,!1),this.hideUploadProgress()}updateVideoPreview(e){let t=this.queryElement(`#videoPreview`);if(t.pause(),!e){t.srcObject=null;return}t.srcObject=e,t.play().catch(e=>{let t=e instanceof Error?e.message:String(e);t.includes(`abort`)||t.includes(`NotAllowedError`)||t.includes(`interrupted`)||this.showError(`Failed to play preview video: ${t}`)})}getShadowRoot(){return this.shadowRoot}};let Cs=`:host{--vidtreo-background:0 0% 100%;--vidtreo-foreground:0 0% 3.9%;--vidtreo-card:0 0% 100%;--vidtreo-card-foreground:0 0% 3.9%;--vidtreo-primary:0 0% 9%;--vidtreo-primary-foreground:0 0% 98%;--vidtreo-secondary:0 0% 96.1%;--vidtreo-secondary-foreground:0 0% 9%;--vidtreo-muted:0 0% 96.1%;--vidtreo-muted-foreground:0 0% 45.1%;--vidtreo-accent:0 0% 96.1%;--vidtreo-accent-foreground:0 0% 9%;--vidtreo-destructive:0 84.2% 60.2%;--vidtreo-destructive-foreground:0 0% 98%;--vidtreo-border:0 0% 89.8%;--vidtreo-input:0 0% 89.8%;--vidtreo-ring:0 0% 3.9%;--vidtreo-radius:.5rem;--vidtreo-preview-bg:0 0% 98%;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,sans-serif;display:block}.vidtreo-container{background:hsl(var(--vidtreo-background));border-radius:16px;width:100%;max-width:600px;padding:40px;box-shadow:0 20px 60px #0000004d}.vidtreo-preview-container{aspect-ratio:9/16;border:1px solid hsl(var(--vidtreo-border));background:hsl(var(--vidtreo-preview-bg));border-radius:.5rem;justify-content:center;align-items:center;width:100%;display:flex;position:relative;overflow:hidden}@media (width>=768px){.vidtreo-preview-container{aspect-ratio:16/9}}.vidtreo-camera-area{background:#f8f9ff;border:2px solid #667eea;border-radius:12px;margin-bottom:20px;padding:20px;display:none;position:relative}.vidtreo-source-transition-overlay{z-index:10;backdrop-filter:blur(4px);background:#000000b3;border-radius:8px;flex-direction:column;justify-content:center;align-items:center;transition:opacity .3s;display:none;position:absolute;inset:20px}.vidtreo-source-transition-overlay.vidtreo-active{animation:.2s vidtreo-fadeIn;display:flex}@keyframes vidtreo-fadeIn{0%{opacity:0}to{opacity:1}}.vidtreo-transition-spinner{border:4px solid #ffffff4d;border-top-color:#667eea;border-radius:50%;width:40px;height:40px;margin-bottom:12px;animation:.8s linear infinite vidtreo-spin}@keyframes vidtreo-spin{to{transform:rotate(360deg)}}.vidtreo-transition-message{color:#fff;text-align:center;font-size:14px;font-weight:500}.vidtreo-camera-area.vidtreo-active{display:block}.vidtreo-preview-skeleton{background:hsl(var(--vidtreo-background)/.95);z-index:10;border-radius:.5rem;flex-direction:column;justify-content:center;align-items:center;gap:1rem;display:flex;position:absolute;inset:0}.vidtreo-skeleton-spinner{border:4px solid hsl(var(--vidtreo-muted)/.3);border-top-color:hsl(var(--vidtreo-primary));border-radius:50%;width:40px;height:40px;animation:.8s linear infinite vidtreo-spin}.vidtreo-skeleton-text{color:hsl(var(--vidtreo-muted-foreground));font-size:.875rem;font-weight:500}.vidtreo-video-preview{object-fit:contain;background:#000;border-radius:.5rem;width:100%;height:100%;transition:opacity .3s,transform .3s;display:block;position:absolute;inset:0}.vidtreo-video-preview.vidtreo-screen-share{object-fit:cover}.vidtreo-video-preview.vidtreo-transitioning{opacity:.5;transform:scale(.98)}.vidtreo-countdown-overlay{background:hsl(var(--vidtreo-background)/.95);z-index:20;border-radius:.5rem;justify-content:center;align-items:center;display:none;position:absolute;inset:0}.vidtreo-countdown-overlay.vidtreo-active{display:flex}.vidtreo-countdown-number{color:hsl(var(--vidtreo-foreground));font-size:9rem;font-weight:700;animation:.3s vidtreo-zoomIn}@keyframes vidtreo-zoomIn{0%{opacity:0;transform:scale(.5)}to{opacity:1;transform:scale(1)}}.vidtreo-countdown-text{color:hsl(var(--vidtreo-muted-foreground));margin-top:1rem;font-size:.875rem;font-weight:500}.vidtreo-settings-panel{background:hsl(var(--vidtreo-background));border:1px solid hsl(var(--vidtreo-border));border-radius:.5rem;margin-top:1rem;padding:1.25rem;animation:.3s vidtreo-slideIn;display:none}.vidtreo-settings-panel.vidtreo-active{display:block}@keyframes vidtreo-slideIn{0%{opacity:0;transform:translateY(1rem)}to{opacity:1;transform:translateY(0)}}.vidtreo-settings-title{margin-bottom:1.25rem;font-size:.875rem;font-weight:600}.vidtreo-device-select-group{margin-bottom:1.25rem}.vidtreo-device-select-label{color:hsl(var(--vidtreo-muted-foreground));align-items:center;gap:.5rem;margin-bottom:.5rem;font-size:.75rem;font-weight:500;display:flex}.vidtreo-device-select{border:1px solid hsl(var(--vidtreo-input));background:hsl(var(--vidtreo-background));border-radius:.375rem;width:100%;height:2.25rem;padding:0 .75rem;font-size:.875rem}.vidtreo-audio-level-bars{z-index:10;align-items:center;gap:.125rem;height:1rem;display:flex;position:absolute;bottom:.75rem;right:.75rem}@media (width>=768px){.vidtreo-audio-level-bars{height:1.25rem}}.vidtreo-audio-level-bar{background:hsl(var(--vidtreo-border));border-radius:9999px;align-self:flex-end;width:.125rem;transition:all .1s}.vidtreo-recording-controls{padding:.75rem 1rem;position:absolute;bottom:0;left:0;right:0}.vidtreo-recording-controls-row{justify-content:center;align-items:center;gap:.5rem;display:flex}.vidtreo-recording-timer-row{justify-content:space-between;align-items:center;gap:.5rem;display:flex}.vidtreo-recording-timer-badge{background:hsl(var(--vidtreo-background)/.9);border:1px solid hsl(var(--vidtreo-border));z-index:10;border-radius:9999px;align-items:center;gap:.375rem;padding:.25rem .5rem;display:flex;position:absolute;top:.75rem;right:.75rem;box-shadow:0 1px 3px #0000001a,0 1px 2px -1px #0000001a}.vidtreo-recording-dot-small{background:hsl(var(--vidtreo-destructive));border-radius:50%;width:.375rem;height:.375rem;animation:1.5s ease-in-out infinite vidtreo-pulse}.vidtreo-recording-timer-text{color:hsl(var(--vidtreo-foreground));font-family:monospace;font-size:.75rem;font-weight:500}.vidtreo-control-buttons-row{justify-content:center;align-items:center;gap:.375rem;display:flex}.vidtreo-control-button{border:1px solid hsl(var(--vidtreo-border));background:hsl(var(--vidtreo-background)/.9);cursor:pointer;width:2rem;height:2rem;color:hsl(var(--vidtreo-foreground));border-radius:9999px;justify-content:center;align-items:center;transition:all .2s;display:flex}.vidtreo-control-button svg{color:inherit;fill:currentColor;flex-shrink:0;width:24px!important;height:24px!important}@media (width>=768px){.vidtreo-control-button svg{width:26px!important;height:26px!important}.vidtreo-control-button{width:2.25rem;height:2.25rem}}.vidtreo-control-button:hover:not(:disabled){background:hsl(var(--vidtreo-accent))}.vidtreo-control-button:disabled{opacity:.5;cursor:not-allowed}.vidtreo-control-button.vidtreo-muted{background:hsl(var(--vidtreo-muted));color:hsl(var(--vidtreo-foreground))}.vidtreo-record-button{background:hsl(var(--vidtreo-destructive));height:2rem;color:hsl(var(--vidtreo-destructive-foreground));border:none;border-radius:9999px;align-items:center;gap:.375rem;padding:0 .75rem;font-size:.75rem;font-weight:500;transition:all .2s;display:flex}@media (width>=768px){.vidtreo-record-button{height:2.25rem;font-size:.875rem}}.vidtreo-record-button:hover:not(:disabled){background:hsl(var(--vidtreo-destructive)/.9)}.vidtreo-rec-indicator-top{background:hsl(var(--vidtreo-background)/.9);border:1px solid hsl(var(--vidtreo-border));border-radius:9999px;align-items:center;gap:.375rem;padding:.25rem .5rem;display:flex;position:absolute;top:.75rem;left:.75rem;box-shadow:0 1px 3px #0000001a,0 1px 2px -1px #0000001a}.vidtreo-rec-indicator-top span{font-size:.75rem;font-weight:500}.vidtreo-start-camera-area{text-align:center;cursor:pointer;background:#f8f9ff;border:2px dashed #667eea;border-radius:12px;margin-bottom:20px;padding:40px;transition:all .3s}.vidtreo-start-camera-area:hover:not(.vidtreo-loading){background:#f0f2ff;border-color:#764ba2}.vidtreo-start-camera-area.vidtreo-loading{cursor:wait;opacity:.7}.vidtreo-start-camera-area.vidtreo-loading .vidtreo-camera-text{color:#999}.vidtreo-camera-icon{margin-bottom:16px;font-size:48px}.vidtreo-camera-text{color:#667eea;margin-bottom:8px;font-weight:600}.vidtreo-camera-hint{color:#999;font-size:12px}@keyframes vidtreo-pulse{0%,to{opacity:1}50%{opacity:.3}}.vidtreo-progress{margin-top:20px;display:none}.vidtreo-progress.vidtreo-active{display:block}.vidtreo-progress-bar{background:#e0e0e0;border-radius:4px;width:100%;height:8px;margin-bottom:8px;overflow:hidden}.vidtreo-progress-fill{background:linear-gradient(90deg,#667eea 0%,#764ba2 100%);width:0%;height:100%;transition:width .3s}.vidtreo-progress-text{text-align:center;color:#666;font-size:14px}.vidtreo-error{color:#c33;background:#fee;border:2px solid #fcc;border-radius:8px;margin-top:20px;padding:16px;display:none}.vidtreo-error.vidtreo-active{display:block}.vidtreo-upload-progress{margin-top:20px;display:none}.vidtreo-upload-progress.vidtreo-active{display:block}.vidtreo-upload-status{border-radius:8px;margin-top:20px;padding:16px;display:none}.vidtreo-upload-status.vidtreo-active{display:block}.vidtreo-upload-status.vidtreo-success{color:#22543d;background:#f0f9ff;border:2px solid #48bb78}.vidtreo-upload-status.vidtreo-error{color:#c33;background:#fee;border:2px solid #fcc}.vidtreo-upload-status-text{font-size:14px;font-weight:500}`;var ws=`<div class="vidtreo-container">
|
|
8152
|
+
`)}let vs={en:{title:`Video Recorder`,subtitle:`Record video from your camera and transcode it to MP4 format`,initializingCamera:`Initializing camera...`,grantPermissions:`Grant camera and microphone permissions when prompted`,retryCamera:`Retry Camera`,switchingDevice:`Switching device...`,recordingStartsIn:`Recording starts in...`,switchingSource:`Switching source...`,rec:`REC`,settings:`Settings`,record:`Record`,stop:`Stop`,pause:`Pause`,resume:`Resume`,mute:`Mute`,unmute:`Unmute`,switchSource:`Switch Source`,camera:`Camera`,microphone:`Microphone`,processVideo:`Process Video`,processing:`Processing...`,uploading:`Uploading...`,switchingCamera:`Switching camera...`,switchingMicrophone:`Switching microphone...`,failedToStartCamera:`Failed to start camera`,userInAnotherTab:`User in another tab`},es:{title:`Grabador de Video`,subtitle:`Graba video desde tu cámara y transcodifícalo a formato MP4`,initializingCamera:`Inicializando cámara...`,grantPermissions:`Otorga permisos de cámara y micrófono cuando se solicite`,retryCamera:`Reintentar Cámara`,switchingDevice:`Cambiando dispositivo...`,recordingStartsIn:`La grabación comienza en...`,switchingSource:`Cambiando fuente...`,rec:`GRAB`,settings:`Configuración`,record:`Grabar`,stop:`Detener`,pause:`Pausar`,resume:`Reanudar`,mute:`Silenciar`,unmute:`Activar sonido`,switchSource:`Cambiar Fuente`,camera:`Cámara`,microphone:`Micrófono`,processVideo:`Procesar Video`,processing:`Procesando...`,uploading:`Subiendo...`,switchingCamera:`Cambiando cámara...`,switchingMicrophone:`Cambiando micrófono...`,failedToStartCamera:`Error al iniciar la cámara`,userInAnotherTab:`Usuario en otra pestaña`}};var ys=class{constructor(e=`en`,t={}){this.lang=e,this.customTexts=t}setLang(e){this.lang=e}setCustomTexts(e){this.customTexts=e}t(e){return this.customTexts[e]?this.customTexts[e]:(vs[this.lang]||vs.en)[e]}getAll(){return{...vs[this.lang]||vs.en,...this.customTexts}}};let bs=.7;var xs=class{constructor(){this.barsContainer=null}initializeBars(e){this.barsContainer=this.queryElement(e,`#audioLevelBars`),this.barsContainer.innerHTML=``;for(let e=0;e<15;e++){let e=this.createBar();this.barsContainer.appendChild(e)}}updateBars(e,t){if(!this.barsContainer)throw Error(`Bars container not initialized`);this.barsContainer.querySelectorAll(`.vidtreo-audio-level-bar`).forEach((n,r)=>{this.updateBar(n,r,e,t)})}queryElement(e,t){let n=e.querySelector(t);if(!n)throw Error(`Element not found: ${t}`);return n}createBar(){let e=document.createElement(`div`);return e.className=`vidtreo-audio-level-bar`,e.style.height=`15%`,e}updateBar(e,t,n,r){let i=n>=this.calculateThreshold(t)&&!r;this.setBarHeight(e,t,i),this.setBarStyle(e,t,i,r)}calculateThreshold(e){return(e+1)/15*100}setBarHeight(e,t,n){if(n){let n=30+t/15*70;e.style.height=`${n}%`}else e.style.height=`15%`}setBarStyle(e,t,n,r){if(r||!n)e.style.backgroundColor=``,e.style.opacity=`1`;else{let n=t/15,r=bs+t/15*(1-bs);e.style.backgroundColor=Qo(n),e.style.opacity=String(r)}}},Ss=class extends m{updateDeviceSelects(e){let t=e.querySelector(`#cameraSelect`),n=e.querySelector(`#micSelect`),r=this.getAvailableDevicesList(),i=this.getSelectedCameraDeviceId(),a=this.getSelectedMicDeviceId();if(t){t.innerHTML=`<option value="default">Default Camera</option>`;for(let e of r.videoinput.filter(e=>e.deviceId&&e.deviceId.trim()!==``)){let n=document.createElement(`option`);n.value=e.deviceId,n.textContent=e.label||`Camera ${e.deviceId.slice(0,8)}`,e.deviceId===i&&(n.selected=!0),t.appendChild(n)}}if(n){n.innerHTML=`<option value="default">Default Microphone</option>`;for(let e of r.audioinput.filter(e=>e.deviceId&&e.deviceId.trim()!==``)){let t=document.createElement(`option`);t.value=e.deviceId,t.textContent=e.label||`Microphone ${e.deviceId.slice(0,8)}`,e.deviceId===a&&(t.selected=!0),n.appendChild(t)}}}},Cs=class{constructor(e){this.shadowRoot=e}setShadowRoot(e){this.shadowRoot=e}queryElement(e){let t=this.shadowRoot.querySelector(e);if(!t)throw Error(`Element not found: ${e}`);return t}setText(e,t){e.textContent=t}toggleClass(e,t,n){n?e.classList.add(t):e.classList.remove(t)}setStyle(e,t,n){e.style.setProperty(t,n)}setDisplay(e,t){this.setStyle(e,`display`,t)}setDisabled(e,t){e.disabled=t}showError(e){let t=this.queryElement(`#error`);this.setText(t,e),this.toggleClass(t,`vidtreo-active`,!0)}hideError(){let e=this.queryElement(`#error`);this.toggleClass(e,`vidtreo-active`,!1)}showProgress(){let e=this.queryElement(`#progress`);this.toggleClass(e,`vidtreo-active`,!0)}hideProgress(){let e=this.queryElement(`#progress`);this.toggleClass(e,`vidtreo-active`,!1)}updateProgress(e,t){let n=Math.round(e*100),r=this.queryElement(`#progressFill`),i=this.queryElement(`#progressText`);this.setStyle(r,`width`,`${n}%`),this.setText(i,t)}updateRecordingTimer(e){let t=this.queryElement(`#recordingTimer`);this.setText(t,e)}updateMuteState(e,t,n){let r=this.queryElement(`#muteButton`),i=this.shadowRoot.querySelector(`#muteIcon`);if(!i)throw Error(`Mute icon element not found`);e?(this.toggleClass(r,`vidtreo-muted`,!0),i.innerHTML=`<path d="M211,221.31,51,45.31A4,4,0,0,0,45,50.69L84,93.55V128a44,44,0,0,0,66,38.12l16.38,18A67.21,67.21,0,0,1,128,196a68.07,68.07,0,0,1-68-68,4,4,0,0,0-8,0,76.09,76.09,0,0,0,72,75.89V240a4,4,0,0,0,8,0V203.89a75.1,75.1,0,0,0,39.79-13.77L205,226.69a4,4,0,1,0,5.92-5.38ZM128,164a36,36,0,0,1-36-36V102.35L144.43,160A35.83,35.83,0,0,1,128,164Zm61.12-6.15A67.44,67.44,0,0,0,196,128a4,4,0,0,1,8,0,75.28,75.28,0,0,1-7.7,33.37,4,4,0,0,1-7.18-3.52ZM87.63,46.46A44,44,0,0,1,172,64v64a44.2,44.2,0,0,1-.24,4.61,4,4,0,0,1-4,3.58l-.42,0a4,4,0,0,1-3.57-4.39A36.67,36.67,0,0,0,164,128V64A36,36,0,0,0,95,49.66a4,4,0,0,1-7.34-3.2Z"></path>`):(this.toggleClass(r,`vidtreo-muted`,!1),i.innerHTML=`<path d="M128,172a44.05,44.05,0,0,0,44-44V64a44,44,0,0,0-88,0v64A44.05,44.05,0,0,0,128,172ZM92,64a36,36,0,0,1,72,0v64a36,36,0,0,1-72,0Zm40,139.89V240a4,4,0,0,1-8,0V203.89A76.09,76.09,0,0,1,52,128a4,4,0,0,1,8,0,68,68,0,0,0,136,0,4,4,0,0,1,8,0A76.09,76.09,0,0,1,132,203.89Z"></path>`),t(n,e)}updatePauseState(e){let t=this.queryElement(`#pauseButton`),n=this.queryElement(`#resumeButton`);this.setDisplay(t,e?`none`:`flex`),this.setDisplay(n,e?`flex`:`none`)}updateCountdownOverlay(e,t){let n=this.queryElement(`#countdownOverlay`),r=this.queryElement(`#countdownNumber`);e===`countdown`&&t>0?(this.setDisplay(n,`flex`),this.setText(r,t.toString())):this.setDisplay(n,`none`)}showSourceTransition(e){let t=this.queryElement(`#videoPreview`);this.toggleClass(t,`vidtreo-transitioning`,!0);let n=this.queryElement(`#sourceTransitionOverlay`);this.toggleClass(n,`vidtreo-active`,!0);let r=n.querySelector(`.vidtreo-transition-message`);if(!r)throw Error(`Transition message element not found`);this.setText(r,e)}hideSourceTransition(){let e=this.queryElement(`#videoPreview`);this.toggleClass(e,`vidtreo-transitioning`,!1);let t=this.queryElement(`#sourceTransitionOverlay`);this.toggleClass(t,`vidtreo-active`,!1)}handleStreamStart(e,t){let n=this.queryElement(`#startButton`),r=this.queryElement(`#stopButton`),i=this.queryElement(`#cameraArea`),a=this.queryElement(`#startCameraArea`),o=this.queryElement(`#audioLevelBars`);if(this.setDisabled(n,!1),this.setDisplay(n,`flex`),this.setDisabled(r,!0),this.setDisplay(r,`none`),this.toggleClass(i,`vidtreo-active`,!0),this.setDisplay(a,`none`),e===`idle`){let e=this.queryElement(`#settingsButton`),t=this.queryElement(`#muteButton`),n=this.queryElement(`#pauseButton`),r=this.queryElement(`#resumeButton`),i=this.queryElement(`#switchSourceButton`);this.setDisplay(e,`flex`),this.setDisplay(t,`none`),this.setDisplay(n,`none`),this.setDisplay(r,`none`),this.setDisplay(i,`none`)}t(),this.setDisplay(o,`flex`)}handleStreamStop(e){let t=this.queryElement(`#videoPreview`);t.srcObject=null;let n=this.queryElement(`#cameraArea`),r=this.queryElement(`#audioLevelBars`);this.toggleClass(n,`vidtreo-active`,!1),e(),this.setDisplay(r,`none`)}handleRecordingStart(e){let t=this.queryElement(`#startButton`),n=this.queryElement(`#stopButton`),r=this.queryElement(`#muteButton`),i=this.queryElement(`#switchSourceButton`),a=this.queryElement(`#settingsButton`),o=this.queryElement(`#pauseButton`),s=this.queryElement(`#resumeButton`),c=this.queryElement(`#recIndicatorTop`),l=this.queryElement(`#recordingTimerRow`);this.setDisplay(t,`none`),this.setDisplay(n,`flex`),this.setDisabled(n,!1),this.setDisplay(c,`flex`),this.setDisplay(l,`flex`),this.setDisplay(a,`none`),this.setDisabled(r,!1),this.setDisplay(r,`flex`),this.setDisabled(i,!1),this.setDisplay(i,`flex`),this.setDisplay(o,e?`none`:`flex`),this.setDisplay(s,e?`flex`:`none`),this.hideError()}updateRecordingControlsAfterStop(){let e=this.queryElement(`#startButton`),t=this.queryElement(`#stopButton`),n=this.queryElement(`#processButton`),r=this.queryElement(`#muteButton`),i=this.queryElement(`#switchSourceButton`),a=this.queryElement(`#settingsButton`),o=this.queryElement(`#pauseButton`),s=this.queryElement(`#resumeButton`),c=this.queryElement(`#recIndicatorTop`),l=this.queryElement(`#recordingTimerRow`);this.setDisplay(e,`flex`),this.setDisabled(e,!1),this.setDisplay(t,`none`),this.setDisabled(t,!0),this.setDisplay(c,`none`),this.setDisplay(l,`none`),this.setDisplay(n,`none`),this.setDisplay(r,`none`),this.setDisplay(i,`none`),this.setDisplay(a,`flex`),this.setDisplay(o,`none`),this.setDisplay(s,`none`)}updateSwitchButtonText(e){let t=this.queryElement(`#switchSourceButton`),n=this.shadowRoot.querySelector(`#switchSourceIcon`);if(!n)throw Error(`Switch source icon element not found`);e===`camera`?(n.innerHTML=`<rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/>`,t.title=`Switch to Screen`):(n.innerHTML=`<path d="M17 17H4a2 2 0 0 1-2-2V5c0-1.5 1-2 1-2"/><path d="M22 15V5a2 2 0 0 0-2-2H9"/><path d="M8 21h8"/><path d="M12 17v4"/><path d="m2 2 20 20"/>`,t.title=`Switch to Camera`)}toggleSettings(e){let t=this.queryElement(`#settingsPanel`),n=!e;return this.setDisplay(t,n?`block`:`none`),n}showUploadProgress(){let e=this.queryElement(`#uploadProgress`);this.toggleClass(e,`vidtreo-active`,!0),this.updateUploadProgress(0)}updateUploadProgress(e){let t=this.queryElement(`#uploadProgressFill`),n=this.queryElement(`#uploadProgressText`),r=Math.round(e*100);this.setStyle(t,`width`,`${r}%`),this.setText(n,`Uploading... ${r}%`)}hideUploadProgress(){let e=this.queryElement(`#uploadProgress`);this.toggleClass(e,`vidtreo-active`,!1)}showUploadSuccess(e){let t=this.queryElement(`#uploadStatus`),n=this.queryElement(`#uploadStatusText`);this.toggleClass(t,`vidtreo-active`,!0),this.toggleClass(t,`vidtreo-success`,!0),this.toggleClass(t,`vidtreo-error`,!1),this.setText(n,`✅ Video uploaded successfully! Video ID: ${e.videoId}`)}showUploadError(e){let t=this.queryElement(`#uploadStatus`),n=this.queryElement(`#uploadStatusText`);this.toggleClass(t,`vidtreo-active`,!0),this.toggleClass(t,`vidtreo-error`,!0),this.toggleClass(t,`vidtreo-success`,!1),this.setText(n,`❌ Upload failed: ${e}`)}clearUploadStatus(){let e=this.queryElement(`#uploadStatus`);this.toggleClass(e,`vidtreo-active`,!1),this.toggleClass(e,`vidtreo-success`,!1),this.toggleClass(e,`vidtreo-error`,!1),this.hideUploadProgress()}updateVideoPreview(e){let t=this.queryElement(`#videoPreview`);if(t.pause(),!e){t.srcObject=null;return}t.srcObject=e,t.play().catch(e=>{let t=e instanceof Error?e.message:String(e);t.includes(`abort`)||t.includes(`NotAllowedError`)||t.includes(`interrupted`)||this.showError(`Failed to play preview video: ${t}`)})}getShadowRoot(){return this.shadowRoot}};let ws=`:host{--vidtreo-background:0 0% 100%;--vidtreo-foreground:0 0% 3.9%;--vidtreo-card:0 0% 100%;--vidtreo-card-foreground:0 0% 3.9%;--vidtreo-primary:0 0% 9%;--vidtreo-primary-foreground:0 0% 98%;--vidtreo-secondary:0 0% 96.1%;--vidtreo-secondary-foreground:0 0% 9%;--vidtreo-muted:0 0% 96.1%;--vidtreo-muted-foreground:0 0% 45.1%;--vidtreo-accent:0 0% 96.1%;--vidtreo-accent-foreground:0 0% 9%;--vidtreo-destructive:0 84.2% 60.2%;--vidtreo-destructive-foreground:0 0% 98%;--vidtreo-border:0 0% 89.8%;--vidtreo-input:0 0% 89.8%;--vidtreo-ring:0 0% 3.9%;--vidtreo-radius:.5rem;--vidtreo-preview-bg:0 0% 98%;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,sans-serif;display:block}.vidtreo-container{background:hsl(var(--vidtreo-background));border-radius:16px;width:100%;max-width:600px;padding:40px;box-shadow:0 20px 60px #0000004d}.vidtreo-preview-container{aspect-ratio:9/16;border:1px solid hsl(var(--vidtreo-border));background:hsl(var(--vidtreo-preview-bg));border-radius:.5rem;justify-content:center;align-items:center;width:100%;display:flex;position:relative;overflow:hidden}@media (width>=768px){.vidtreo-preview-container{aspect-ratio:16/9}}.vidtreo-camera-area{background:#f8f9ff;border:2px solid #667eea;border-radius:12px;margin-bottom:20px;padding:20px;display:none;position:relative}.vidtreo-source-transition-overlay{z-index:10;backdrop-filter:blur(4px);background:#000000b3;border-radius:8px;flex-direction:column;justify-content:center;align-items:center;transition:opacity .3s;display:none;position:absolute;inset:20px}.vidtreo-source-transition-overlay.vidtreo-active{animation:.2s vidtreo-fadeIn;display:flex}@keyframes vidtreo-fadeIn{0%{opacity:0}to{opacity:1}}.vidtreo-transition-spinner{border:4px solid #ffffff4d;border-top-color:#667eea;border-radius:50%;width:40px;height:40px;margin-bottom:12px;animation:.8s linear infinite vidtreo-spin}@keyframes vidtreo-spin{to{transform:rotate(360deg)}}.vidtreo-transition-message{color:#fff;text-align:center;font-size:14px;font-weight:500}.vidtreo-camera-area.vidtreo-active{display:block}.vidtreo-preview-skeleton{background:hsl(var(--vidtreo-background)/.95);z-index:10;border-radius:.5rem;flex-direction:column;justify-content:center;align-items:center;gap:1rem;display:flex;position:absolute;inset:0}.vidtreo-skeleton-spinner{border:4px solid hsl(var(--vidtreo-muted)/.3);border-top-color:hsl(var(--vidtreo-primary));border-radius:50%;width:40px;height:40px;animation:.8s linear infinite vidtreo-spin}.vidtreo-skeleton-text{color:hsl(var(--vidtreo-muted-foreground));font-size:.875rem;font-weight:500}.vidtreo-video-preview{object-fit:contain;background:#000;border-radius:.5rem;width:100%;height:100%;transition:opacity .3s,transform .3s;display:block;position:absolute;inset:0}.vidtreo-video-preview.vidtreo-screen-share{object-fit:cover}.vidtreo-video-preview.vidtreo-transitioning{opacity:.5;transform:scale(.98)}.vidtreo-countdown-overlay{background:hsl(var(--vidtreo-background)/.95);z-index:20;border-radius:.5rem;justify-content:center;align-items:center;display:none;position:absolute;inset:0}.vidtreo-countdown-overlay.vidtreo-active{display:flex}.vidtreo-countdown-number{color:hsl(var(--vidtreo-foreground));font-size:9rem;font-weight:700;animation:.3s vidtreo-zoomIn}@keyframes vidtreo-zoomIn{0%{opacity:0;transform:scale(.5)}to{opacity:1;transform:scale(1)}}.vidtreo-countdown-text{color:hsl(var(--vidtreo-muted-foreground));margin-top:1rem;font-size:.875rem;font-weight:500}.vidtreo-settings-panel{background:hsl(var(--vidtreo-background));border:1px solid hsl(var(--vidtreo-border));border-radius:.5rem;margin-top:1rem;padding:1.25rem;animation:.3s vidtreo-slideIn;display:none}.vidtreo-settings-panel.vidtreo-active{display:block}@keyframes vidtreo-slideIn{0%{opacity:0;transform:translateY(1rem)}to{opacity:1;transform:translateY(0)}}.vidtreo-settings-title{margin-bottom:1.25rem;font-size:.875rem;font-weight:600}.vidtreo-device-select-group{margin-bottom:1.25rem}.vidtreo-device-select-label{color:hsl(var(--vidtreo-muted-foreground));align-items:center;gap:.5rem;margin-bottom:.5rem;font-size:.75rem;font-weight:500;display:flex}.vidtreo-device-select{border:1px solid hsl(var(--vidtreo-input));background:hsl(var(--vidtreo-background));border-radius:.375rem;width:100%;height:2.25rem;padding:0 .75rem;font-size:.875rem}.vidtreo-audio-level-bars{z-index:10;align-items:center;gap:.125rem;height:1rem;display:flex;position:absolute;bottom:.75rem;right:.75rem}@media (width>=768px){.vidtreo-audio-level-bars{height:1.25rem}}.vidtreo-audio-level-bar{background:hsl(var(--vidtreo-border));border-radius:9999px;align-self:flex-end;width:.125rem;transition:all .1s}.vidtreo-recording-controls{padding:.75rem 1rem;position:absolute;bottom:0;left:0;right:0}.vidtreo-recording-controls-row{justify-content:center;align-items:center;gap:.5rem;display:flex}.vidtreo-recording-timer-row{justify-content:space-between;align-items:center;gap:.5rem;display:flex}.vidtreo-recording-timer-badge{background:hsl(var(--vidtreo-background)/.9);border:1px solid hsl(var(--vidtreo-border));z-index:10;border-radius:9999px;align-items:center;gap:.375rem;padding:.25rem .5rem;display:flex;position:absolute;top:.75rem;right:.75rem;box-shadow:0 1px 3px #0000001a,0 1px 2px -1px #0000001a}.vidtreo-recording-dot-small{background:hsl(var(--vidtreo-destructive));border-radius:50%;width:.375rem;height:.375rem;animation:1.5s ease-in-out infinite vidtreo-pulse}.vidtreo-recording-timer-text{color:hsl(var(--vidtreo-foreground));font-family:monospace;font-size:.75rem;font-weight:500}.vidtreo-control-buttons-row{justify-content:center;align-items:center;gap:.375rem;display:flex}.vidtreo-control-button{border:1px solid hsl(var(--vidtreo-border));background:hsl(var(--vidtreo-background)/.9);cursor:pointer;width:2rem;height:2rem;color:hsl(var(--vidtreo-foreground));border-radius:9999px;justify-content:center;align-items:center;transition:all .2s;display:flex}.vidtreo-control-button svg{color:inherit;fill:currentColor;flex-shrink:0;width:24px!important;height:24px!important}@media (width>=768px){.vidtreo-control-button svg{width:26px!important;height:26px!important}.vidtreo-control-button{width:2.25rem;height:2.25rem}}.vidtreo-control-button:hover:not(:disabled){background:hsl(var(--vidtreo-accent))}.vidtreo-control-button:disabled{opacity:.5;cursor:not-allowed}.vidtreo-control-button.vidtreo-muted{background:hsl(var(--vidtreo-muted));color:hsl(var(--vidtreo-foreground))}.vidtreo-record-button{background:hsl(var(--vidtreo-destructive));height:2rem;color:hsl(var(--vidtreo-destructive-foreground));border:none;border-radius:9999px;align-items:center;gap:.375rem;padding:0 .75rem;font-size:.75rem;font-weight:500;transition:all .2s;display:flex}@media (width>=768px){.vidtreo-record-button{height:2.25rem;font-size:.875rem}}.vidtreo-record-button:hover:not(:disabled){background:hsl(var(--vidtreo-destructive)/.9)}.vidtreo-rec-indicator-top{background:hsl(var(--vidtreo-background)/.9);border:1px solid hsl(var(--vidtreo-border));border-radius:9999px;align-items:center;gap:.375rem;padding:.25rem .5rem;display:flex;position:absolute;top:.75rem;left:.75rem;box-shadow:0 1px 3px #0000001a,0 1px 2px -1px #0000001a}.vidtreo-rec-indicator-top span{font-size:.75rem;font-weight:500}.vidtreo-start-camera-area{text-align:center;cursor:pointer;background:#f8f9ff;border:2px dashed #667eea;border-radius:12px;margin-bottom:20px;padding:40px;transition:all .3s}.vidtreo-start-camera-area:hover:not(.vidtreo-loading){background:#f0f2ff;border-color:#764ba2}.vidtreo-start-camera-area.vidtreo-loading{cursor:wait;opacity:.7}.vidtreo-start-camera-area.vidtreo-loading .vidtreo-camera-text{color:#999}.vidtreo-camera-icon{margin-bottom:16px;font-size:48px}.vidtreo-camera-text{color:#667eea;margin-bottom:8px;font-weight:600}.vidtreo-camera-hint{color:#999;font-size:12px}@keyframes vidtreo-pulse{0%,to{opacity:1}50%{opacity:.3}}.vidtreo-progress{margin-top:20px;display:none}.vidtreo-progress.vidtreo-active{display:block}.vidtreo-progress-bar{background:#e0e0e0;border-radius:4px;width:100%;height:8px;margin-bottom:8px;overflow:hidden}.vidtreo-progress-fill{background:linear-gradient(90deg,#667eea 0%,#764ba2 100%);width:0%;height:100%;transition:width .3s}.vidtreo-progress-text{text-align:center;color:#666;font-size:14px}.vidtreo-error{color:#c33;background:#fee;border:2px solid #fcc;border-radius:8px;margin-top:20px;padding:16px;display:none}.vidtreo-error.vidtreo-active{display:block}.vidtreo-upload-progress{margin-top:20px;display:none}.vidtreo-upload-progress.vidtreo-active{display:block}.vidtreo-upload-status{border-radius:8px;margin-top:20px;padding:16px;display:none}.vidtreo-upload-status.vidtreo-active{display:block}.vidtreo-upload-status.vidtreo-success{color:#22543d;background:#f0f9ff;border:2px solid #48bb78}.vidtreo-upload-status.vidtreo-error{color:#c33;background:#fee;border:2px solid #fcc}.vidtreo-upload-status-text{font-size:14px;font-weight:500}`;var Ts=`<div class="vidtreo-container">
|
|
7755
8153
|
<h1>{{TITLE}}</h1>
|
|
7756
8154
|
<p class="vidtreo-subtitle">{{SUBTITLE}}</p>
|
|
7757
8155
|
|
|
@@ -8062,4 +8460,4 @@ new RecorderWorker;
|
|
|
8062
8460
|
<div class="vidtreo-upload-status-text" id="uploadStatusText"></div>
|
|
8063
8461
|
</div>
|
|
8064
8462
|
</div>
|
|
8065
|
-
`;function Ts(e,t){return t.reduce((e,[t,n])=>e.split(t).join(n),e)}function Es(e,t,n=`camera`){return Ts(ws,[[`{{MUTE_ICON_PLACEHOLDER}}`,e?`<path d="M211,221.31,51,45.31A4,4,0,0,0,45,50.69L84,93.55V128a44,44,0,0,0,66,38.12l16.38,18A67.21,67.21,0,0,1,128,196a68.07,68.07,0,0,1-68-68,4,4,0,0,0-8,0,76.09,76.09,0,0,0,72,75.89V240a4,4,0,0,0,8,0V203.89a75.1,75.1,0,0,0,39.79-13.77L205,226.69a4,4,0,1,0,5.92-5.38ZM128,164a36,36,0,0,1-36-36V102.35L144.43,160A35.83,35.83,0,0,1,128,164Zm61.12-6.15A67.44,67.44,0,0,0,196,128a4,4,0,0,1,8,0,75.28,75.28,0,0,1-7.7,33.37,4,4,0,0,1-7.18-3.52ZM87.63,46.46A44,44,0,0,1,172,64v64a44.2,44.2,0,0,1-.24,4.61,4,4,0,0,1-4,3.58l-.42,0a4,4,0,0,1-3.57-4.39A36.67,36.67,0,0,0,164,128V64A36,36,0,0,0,95,49.66a4,4,0,0,1-7.34-3.2Z"></path>`:`<path d="M128,172a44.05,44.05,0,0,0,44-44V64a44,44,0,0,0-88,0v64A44.05,44.05,0,0,0,128,172ZM92,64a36,36,0,0,1,72,0v64a36,36,0,0,1-72,0Zm40,139.89V240a4,4,0,0,1-8,0V203.89A76.09,76.09,0,0,1,52,128a4,4,0,0,1,8,0,68,68,0,0,0,136,0,4,4,0,0,1,8,0A76.09,76.09,0,0,1,132,203.89Z"></path>`],[`{{SWITCH_SOURCE_ICON_PLACEHOLDER}}`,n===`camera`?`<rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/>`:`<path d="M17 17H4a2 2 0 0 1-2-2V5c0-1.5 1-2 1-2"/><path d="M22 15V5a2 2 0 0 0-2-2H9"/><path d="M8 21h8"/><path d="M12 17v4"/><path d="m2 2 20 20"/>`],[`{{TITLE}}`,t.title],[`{{SUBTITLE}}`,t.subtitle],[`{{INITIALIZING_CAMERA}}`,t.initializingCamera],[`{{GRANT_PERMISSIONS}}`,t.grantPermissions],[`{{RETRY_CAMERA}}`,t.retryCamera],[`{{SWITCHING_DEVICE}}`,t.switchingDevice],[`{{RECORDING_STARTS_IN}}`,t.recordingStartsIn],[`{{SWITCHING_SOURCE}}`,t.switchingSource],[`{{REC}}`,t.rec],[`{{SETTINGS}}`,t.settings],[`{{RECORD}}`,t.record],[`{{STOP}}`,t.stop],[`{{PAUSE}}`,t.pause],[`{{RESUME}}`,t.resume],[`{{CAMERA}}`,t.camera],[`{{MICROPHONE}}`,t.microphone],[`{{PROCESS_VIDEO}}`,t.processVideo],[`{{PROCESSING}}`,t.processing],[`{{UPLOADING}}`,t.uploading]])}let Ds=/^https?:\/\//i;function Os(e){return e?Ds.test(e)?e:`https://${e}`:`https://api.vidtreo.com`}function ks(e,n){let r={apiKey:e.getAttribute(`api-key`),backendUrl:Os(e.getAttribute(`backend-url`))},i=e.getAttribute(`countdown-duration`);if(i){let e=Number.parseInt(i,10);Number.isNaN(e)||(r.countdownDuration=e)}let a=e.getAttribute(`max-recording-time`);if(a){let e=Number.parseInt(a,10);Number.isNaN(e)||(r.maxRecordingTime=e)}let o=e.getAttribute(`user-metadata`);if(o)try{let e=JSON.parse(o);Object.assign(n,e),r.userMetadata=n}catch(e){throw Error(`Invalid user-metadata JSON: ${t(e)}`)}let s=e.getAttribute(`enable-source-switching`);r.enableSourceSwitching=s===null||s!==`false`;let c=e.getAttribute(`enable-mute`);r.enableMute=c===null||c!==`false`;let l=e.getAttribute(`enable-pause`);r.enablePause=l===null||l!==`false`;let u=e.getAttribute(`enable-device-change`);return r.enableDeviceChange=u===null||u!==`false`,r}var As=class extends HTMLElement{static{this.observedAttributes=[`api-key`,`backend-url`,`countdown-duration`,`max-recording-time`,`user-metadata`,`enable-source-switching`,`enable-mute`,`enable-pause`,`enable-device-change`,`lang`,`texts`]}constructor(){if(super(),this.recordedBlob=null,this.processedBlob=null,this.isProcessing=!1,this.isMuted=!1,this.showSettings=!1,this.userMetadata={},this.enableSourceSwitching=!0,this.enableMute=!0,this.enablePause=!0,this.enableDeviceChange=!0,this.attachShadow({mode:`open`}),!this.shadowRoot)throw Error(`Shadow root not initialized`);let e=this.getAttribute(`lang`)||`en`,t=this.getAttribute(`texts`);if(this.i18nManager=new vs(e,t?JSON.parse(t):{}),this.uiStateManager=new Ss(this.shadowRoot),this.audioLevelVisualizer=new bs,this.controller=new Wo(ts(this)),this.deviceManager=new xs(this.controller.getStreamManager(),void 0),os(this),!this.shadowRoot)throw Error(`Shadow root not initialized`);let n=Es(this.isMuted,this.i18nManager.getAll(),`camera`);this.shadowRoot.innerHTML=`<style>${Cs}</style>${n}`,ps(this)}async connectedCallback(){try{this.updateFeatureFlags();let e=ks(this,this.userMetadata);await this.controller.initialize(e),await this.checkPendingUploads(),this.initializeUIState(),this.startCamera().catch(e=>{this.uiStateManager.showError(t(e))})}catch(e){this.uiStateManager.showError(t(e))}}attributeChangedCallback(e,n,r){if(n===r)return;if(e===`lang`){this.i18nManager.setLang(r||`en`),this.updateTemplate();return}if(e===`texts`){let e=r?JSON.parse(r):{};this.i18nManager.setCustomTexts(e),this.updateTemplate();return}if(e===`enable-source-switching`||e===`enable-mute`||e===`enable-pause`||e===`enable-device-change`){this.updateFeatureFlags(),this.updateUIForFeatureFlags();return}let i=ks(this,this.userMetadata);this.controller.initialize(i).catch(e=>{this.uiStateManager.showError(t(e))})}disconnectedCallback(){this.controller.cleanup(),this.controller.getStreamManager().destroy()}get shadow(){if(!this.shadowRoot)throw Error(`Shadow root not initialized`);return this.shadowRoot}checkPendingUploads(){return Promise.resolve()}async startCamera(){let e=Qo(this.shadow,`#startCameraArea`);e.classList.add(`vidtreo-loading`);try{let t=this.controller.getDeviceManager(),n=t.getSelectedCameraDeviceId(),r=t.getSelectedMicDeviceId();if(this.deviceManager.setCameraDevice(n),this.deviceManager.setMicDevice(r),await this.controller.startStream(),!this.controller.getStream())throw Error(`Stream was not created after startStream()`);await this.deviceManager.getAvailableDevices(),this.deviceManager.updateDeviceSelects(this.shadow),e.classList.remove(`vidtreo-loading`)}catch(e){let n=Qo(this.shadow,`#startCameraArea`),r=Qo(this.shadow,`#startCameraButton`),i=n.querySelector(`.vidtreo-camera-text`);if(!i)throw Error(`Camera text element not found`);n.classList.remove(`vidtreo-loading`),n.style.display=`block`,r.style.display=`block`;let a=t(e);i.textContent=`${this.i18nManager.t(`failedToStartCamera`)}: ${a}`}}async startRecording(){await ss(this)}async stopRecording(){await cs(this)}pauseRecording(){this.enablePause&&ls(this)}resumeRecording(){this.enablePause&&us(this)}async processVideo(){await ds(this)}downloadVideo(){if(!this.processedBlob)throw Error(`No processed video available`);hs(this.processedBlob)}playVideo(){if(!this.processedBlob)throw Error(`No processed video available`);gs(this.processedBlob)}toggleMute(){this.enableMute&&(this.isMuted?(this.controller.unmuteAudio(),this.isMuted=!1):(this.controller.muteAudio(),this.isMuted=!0),this.uiStateManager.updateMuteState(this.isMuted,(e,t)=>this.audioLevelVisualizer.updateBars(e,t),this.controller.getAudioLevel()))}async toggleSource(){this.enableSourceSwitching&&await this.controller.switchSource(this.controller.getCurrentSourceType()===`camera`?`screen`:`camera`)}toggleSettings(){if(!this.enableDeviceChange)return;this.showSettings=!this.showSettings;let e=this.shadow.querySelector(`#settingsPanel`);if(!e)throw Error(`Settings panel element not found`);e.style.display=this.showSettings?`block`:`none`}async handleCameraChange(e){this.enableDeviceChange&&await is(this,e)}async handleMicChange(e){this.enableDeviceChange&&await as(this,e)}handleStateChange(e,t){e===`active`&&t===`starting`&&this.uiStateManager.hideError()}startAudioLevelTrackingForStream(e){try{this.controller.stopAudioLevelTracking(),this.audioLevelVisualizer.initializeBars(this.shadow),this.controller.startAudioLevelTracking(e,{onLevelUpdate:(e,t)=>{this.audioLevelVisualizer.updateBars(e,t)}}),this.uiStateManager.updateMuteState(this.isMuted,(e,t)=>{this.audioLevelVisualizer.updateBars(e,t)},this.controller.getAudioLevel())}catch(e){this.uiStateManager.showError(t(e))}}handleStreamStart(e){try{this.uiStateManager.updateVideoPreview(e),this.uiStateManager.handleStreamStart(this.controller.getRecordingState(),()=>{this.audioLevelVisualizer.initializeBars(this.shadow)}),this.updateUIForFeatureFlags();try{this.startAudioLevelTrackingForStream(e)}catch{}}catch(e){this.uiStateManager.showError(t(e))}}handleStreamStop(){this.uiStateManager.handleStreamStop(()=>this.controller.stopAudioLevelTracking())}handleRecordingStart(){if(this.showSettings){this.showSettings=!1;let e=this.shadow.querySelector(`#settingsPanel`);if(!e)throw Error(`Settings panel element not found`);e.style.display=`none`}this.uiStateManager.handleRecordingStart(this.controller.isPaused()),this.updateUIForFeatureFlags();let e=this.controller.getAudioStreamForAnalysis();e&&this.startAudioLevelTrackingForStream(e)}handleRecordingStop(e){return this.recordedBlob=e,this.uiStateManager.updateRecordingControlsAfterStop(),this.updateUIForFeatureFlags(),Promise.resolve()}updateRecordingTimer(e){this.uiStateManager.updateRecordingTimer(e)}updateMuteState(e){this.isMuted=e,this.uiStateManager.updateMuteState(e,(e,t)=>this.audioLevelVisualizer.updateBars(e,t),this.controller.getAudioLevel())}updateVideoPreview(e){this.uiStateManager.updateVideoPreview(e)}showError(e){this.uiStateManager.showError(e)}extractErrorMessage(e){return t(e)}getStreamManager(){return this.controller.getStreamManager()}getShadowRoot(){return this.shadow}getController(){return this.controller}getUIStateManager(){return this.uiStateManager}getDeviceManager(){return this.deviceManager}getAudioLevelVisualizer(){return this.audioLevelVisualizer}getRecordedBlob(){return this.recordedBlob}setRecordedBlob(e){this.recordedBlob=e}getProcessedBlob(){return this.processedBlob}setProcessedBlob(e){this.processedBlob=e}getIsProcessing(){return this.isProcessing}setIsProcessing(e){this.isProcessing=e}getIsMuted(){return this.isMuted}setIsMuted(e){this.isMuted=e}getShowSettings(){return this.showSettings}setShowSettings(e){this.showSettings=e}getUserMetadata(){return this.userMetadata}getNormalizedBackendUrl(e){return Os(e)}updateFeatureFlags(){let e=this.getAttribute(`enable-source-switching`);this.enableSourceSwitching=e===null||e!==`false`;let t=this.getAttribute(`enable-mute`);this.enableMute=t===null||t!==`false`;let n=this.getAttribute(`enable-pause`);this.enablePause=n===null||n!==`false`;let r=this.getAttribute(`enable-device-change`);this.enableDeviceChange=r===null||r!==`false`}initializeUIState(){let e=this.shadow.querySelector(`#muteButton`),t=this.shadow.querySelector(`#pauseButton`),n=this.shadow.querySelector(`#resumeButton`),r=this.shadow.querySelector(`#switchSourceButton`),i=this.shadow.querySelector(`#stopButton`),a=this.shadow.querySelector(`#settingsButton`);e&&(e.style.display=`none`),t&&(t.style.display=`none`),n&&(n.style.display=`none`),r&&(r.style.display=`none`),i&&(i.style.display=`none`),a&&(a.style.display=this.enableDeviceChange?`flex`:`none`)}updateUIForFeatureFlags(){let e=this.controller.getRecordingState()===`recording`,t=this.controller.isPaused();this.updateButtonVisibility(`#switchSourceButton`,e&&this.enableSourceSwitching),this.updateButtonVisibility(`#muteButton`,e&&this.enableMute),this.updateButtonVisibility(`#pauseButton`,e&&this.enablePause&&!t),this.updateButtonVisibility(`#resumeButton`,e&&this.enablePause&&t),this.updateButtonVisibility(`#settingsButton`,!e&&this.enableDeviceChange)}updateButtonVisibility(e,t){let n=this.shadow.querySelector(e);n&&(n.style.display=t?`flex`:`none`)}getEnableSourceSwitching(){return this.enableSourceSwitching}getEnableMute(){return this.enableMute}getEnablePause(){return this.enablePause}getEnableDeviceChange(){return this.enableDeviceChange}getI18nManager(){return this.i18nManager}updateTemplate(){if(!this.shadowRoot)return;let e=Es(this.isMuted,this.i18nManager.getAll(),this.controller.getCurrentSourceType());this.shadowRoot.innerHTML=`<style>${Cs}</style>${e}`,ps(this),this.uiStateManager.setShadowRoot(this.shadowRoot),this.initializeUIState(),this.controller.getStreamManager().getStream()&&this.uiStateManager.updateVideoPreview(this.controller.getStreamManager().getStream())}};return customElements.get(`vidtreo-recorder`)||customElements.define(`vidtreo-recorder`,As),e.VidtreoRecorder=As,e})({});
|
|
8463
|
+
`;function Es(e,t){return t.reduce((e,[t,n])=>e.split(t).join(n),e)}function Ds(e,t,n=`camera`){return Es(Ts,[[`{{MUTE_ICON_PLACEHOLDER}}`,e?`<path d="M211,221.31,51,45.31A4,4,0,0,0,45,50.69L84,93.55V128a44,44,0,0,0,66,38.12l16.38,18A67.21,67.21,0,0,1,128,196a68.07,68.07,0,0,1-68-68,4,4,0,0,0-8,0,76.09,76.09,0,0,0,72,75.89V240a4,4,0,0,0,8,0V203.89a75.1,75.1,0,0,0,39.79-13.77L205,226.69a4,4,0,1,0,5.92-5.38ZM128,164a36,36,0,0,1-36-36V102.35L144.43,160A35.83,35.83,0,0,1,128,164Zm61.12-6.15A67.44,67.44,0,0,0,196,128a4,4,0,0,1,8,0,75.28,75.28,0,0,1-7.7,33.37,4,4,0,0,1-7.18-3.52ZM87.63,46.46A44,44,0,0,1,172,64v64a44.2,44.2,0,0,1-.24,4.61,4,4,0,0,1-4,3.58l-.42,0a4,4,0,0,1-3.57-4.39A36.67,36.67,0,0,0,164,128V64A36,36,0,0,0,95,49.66a4,4,0,0,1-7.34-3.2Z"></path>`:`<path d="M128,172a44.05,44.05,0,0,0,44-44V64a44,44,0,0,0-88,0v64A44.05,44.05,0,0,0,128,172ZM92,64a36,36,0,0,1,72,0v64a36,36,0,0,1-72,0Zm40,139.89V240a4,4,0,0,1-8,0V203.89A76.09,76.09,0,0,1,52,128a4,4,0,0,1,8,0,68,68,0,0,0,136,0,4,4,0,0,1,8,0A76.09,76.09,0,0,1,132,203.89Z"></path>`],[`{{SWITCH_SOURCE_ICON_PLACEHOLDER}}`,n===`camera`?`<rect x="2" y="3" width="20" height="14" rx="2" ry="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/>`:`<path d="M17 17H4a2 2 0 0 1-2-2V5c0-1.5 1-2 1-2"/><path d="M22 15V5a2 2 0 0 0-2-2H9"/><path d="M8 21h8"/><path d="M12 17v4"/><path d="m2 2 20 20"/>`],[`{{TITLE}}`,t.title],[`{{SUBTITLE}}`,t.subtitle],[`{{INITIALIZING_CAMERA}}`,t.initializingCamera],[`{{GRANT_PERMISSIONS}}`,t.grantPermissions],[`{{RETRY_CAMERA}}`,t.retryCamera],[`{{SWITCHING_DEVICE}}`,t.switchingDevice],[`{{RECORDING_STARTS_IN}}`,t.recordingStartsIn],[`{{SWITCHING_SOURCE}}`,t.switchingSource],[`{{REC}}`,t.rec],[`{{SETTINGS}}`,t.settings],[`{{RECORD}}`,t.record],[`{{STOP}}`,t.stop],[`{{PAUSE}}`,t.pause],[`{{RESUME}}`,t.resume],[`{{CAMERA}}`,t.camera],[`{{MICROPHONE}}`,t.microphone],[`{{PROCESS_VIDEO}}`,t.processVideo],[`{{PROCESSING}}`,t.processing],[`{{UPLOADING}}`,t.uploading]])}let Os=/^https?:\/\//i;function ks(e){return e?Os.test(e)?e:`https://${e}`:`https://api.vidtreo.com`}function As(e,n){let r={apiKey:e.getAttribute(`api-key`),backendUrl:ks(e.getAttribute(`backend-url`))},i=e.getAttribute(`countdown-duration`);if(i){let e=Number.parseInt(i,10);Number.isNaN(e)||(r.countdownDuration=e)}let a=e.getAttribute(`max-recording-time`);if(a){let e=Number.parseInt(a,10);Number.isNaN(e)||(r.maxRecordingTime=e)}let o=e.getAttribute(`user-metadata`);if(o)try{let e=JSON.parse(o);Object.assign(n,e),r.userMetadata=n}catch(e){throw Error(`Invalid user-metadata JSON: ${t(e)}`)}let s=e.getAttribute(`enable-source-switching`);r.enableSourceSwitching=s===null||s!==`false`;let c=e.getAttribute(`enable-mute`);r.enableMute=c===null||c!==`false`;let l=e.getAttribute(`enable-pause`);r.enablePause=l===null||l!==`false`;let u=e.getAttribute(`enable-device-change`);r.enableDeviceChange=u===null||u!==`false`;let d=e.getAttribute(`enable-tab-visibility-overlay`);return r.enableTabVisibilityOverlay=d!==null&&d!==`false`,r}var js=class extends HTMLElement{static{this.observedAttributes=[`api-key`,`backend-url`,`countdown-duration`,`max-recording-time`,`user-metadata`,`enable-source-switching`,`enable-mute`,`enable-pause`,`enable-device-change`,`enable-tab-visibility-overlay`,`lang`,`texts`]}constructor(){if(super(),this.recordedBlob=null,this.processedBlob=null,this.isProcessing=!1,this.isMuted=!1,this.showSettings=!1,this.userMetadata={},this.enableSourceSwitching=!0,this.enableMute=!0,this.enablePause=!0,this.enableDeviceChange=!0,this.attachShadow({mode:`open`}),!this.shadowRoot)throw Error(`Shadow root not initialized`);let e=this.getAttribute(`lang`)||`en`,t=this.getAttribute(`texts`);if(this.i18nManager=new ys(e,t?JSON.parse(t):{}),this.uiStateManager=new Cs(this.shadowRoot),this.audioLevelVisualizer=new xs,this.controller=new Go(ns(this)),this.deviceManager=new Ss(this.controller.getStreamManager(),void 0),ss(this),!this.shadowRoot)throw Error(`Shadow root not initialized`);let n=Ds(this.isMuted,this.i18nManager.getAll(),`camera`);this.shadowRoot.innerHTML=`<style>${ws}</style>${n}`,ms(this)}async connectedCallback(){try{this.updateFeatureFlags();let e=As(this,this.userMetadata);e.enableTabVisibilityOverlay&&(e.tabVisibilityOverlayText=this.i18nManager.t(`userInAnotherTab`)),await this.controller.initialize(e),await this.checkPendingUploads(),this.initializeUIState(),this.startCamera().catch(e=>{this.uiStateManager.showError(t(e))})}catch(e){this.uiStateManager.showError(t(e))}}attributeChangedCallback(e,n,r){if(n===r)return;if(e===`lang`){this.i18nManager.setLang(r||`en`),this.updateTemplate();return}if(e===`texts`){let e=r?JSON.parse(r):{};this.i18nManager.setCustomTexts(e),this.updateTemplate();return}if(e===`enable-source-switching`||e===`enable-mute`||e===`enable-pause`||e===`enable-device-change`||e===`enable-tab-visibility-overlay`){this.updateFeatureFlags(),this.updateUIForFeatureFlags();let e=As(this,this.userMetadata);e.enableTabVisibilityOverlay&&(e.tabVisibilityOverlayText=this.i18nManager.t(`userInAnotherTab`)),this.controller.initialize(e).catch(e=>{this.uiStateManager.showError(t(e))});return}let i=As(this,this.userMetadata);i.enableTabVisibilityOverlay&&(i.tabVisibilityOverlayText=this.i18nManager.t(`userInAnotherTab`)),this.controller.initialize(i).catch(e=>{this.uiStateManager.showError(t(e))})}disconnectedCallback(){this.controller.cleanup(),this.controller.getStreamManager().destroy()}get shadow(){if(!this.shadowRoot)throw Error(`Shadow root not initialized`);return this.shadowRoot}checkPendingUploads(){return Promise.resolve()}async startCamera(){let e=$o(this.shadow,`#startCameraArea`);e.classList.add(`vidtreo-loading`);try{let t=this.controller.getDeviceManager(),n=t.getSelectedCameraDeviceId(),r=t.getSelectedMicDeviceId();if(this.deviceManager.setCameraDevice(n),this.deviceManager.setMicDevice(r),await this.controller.startStream(),!this.controller.getStream())throw Error(`Stream was not created after startStream()`);await this.deviceManager.getAvailableDevices(),this.deviceManager.updateDeviceSelects(this.shadow),e.classList.remove(`vidtreo-loading`)}catch(e){let n=$o(this.shadow,`#startCameraArea`),r=$o(this.shadow,`#startCameraButton`),i=n.querySelector(`.vidtreo-camera-text`);if(!i)throw Error(`Camera text element not found`);n.classList.remove(`vidtreo-loading`),n.style.display=`block`,r.style.display=`block`;let a=t(e);i.textContent=`${this.i18nManager.t(`failedToStartCamera`)}: ${a}`}}async startRecording(){await cs(this)}async stopRecording(){await ls(this)}pauseRecording(){this.enablePause&&us(this)}resumeRecording(){this.enablePause&&ds(this)}async processVideo(){await fs(this)}downloadVideo(){if(!this.processedBlob)throw Error(`No processed video available`);gs(this.processedBlob)}playVideo(){if(!this.processedBlob)throw Error(`No processed video available`);_s(this.processedBlob)}toggleMute(){this.enableMute&&(this.isMuted?(this.controller.unmuteAudio(),this.isMuted=!1):(this.controller.muteAudio(),this.isMuted=!0),this.uiStateManager.updateMuteState(this.isMuted,(e,t)=>this.audioLevelVisualizer.updateBars(e,t),this.controller.getAudioLevel()))}async toggleSource(){this.enableSourceSwitching&&await this.controller.switchSource(this.controller.getCurrentSourceType()===`camera`?`screen`:`camera`)}toggleSettings(){if(!this.enableDeviceChange)return;this.showSettings=!this.showSettings;let e=this.shadow.querySelector(`#settingsPanel`);if(!e)throw Error(`Settings panel element not found`);e.style.display=this.showSettings?`block`:`none`}async handleCameraChange(e){this.enableDeviceChange&&await as(this,e)}async handleMicChange(e){this.enableDeviceChange&&await os(this,e)}handleStateChange(e,t){e===`active`&&t===`starting`&&this.uiStateManager.hideError()}startAudioLevelTrackingForStream(e){try{this.controller.stopAudioLevelTracking(),this.audioLevelVisualizer.initializeBars(this.shadow),this.controller.startAudioLevelTracking(e,{onLevelUpdate:(e,t)=>{this.audioLevelVisualizer.updateBars(e,t)}}),this.uiStateManager.updateMuteState(this.isMuted,(e,t)=>{this.audioLevelVisualizer.updateBars(e,t)},this.controller.getAudioLevel())}catch(e){this.uiStateManager.showError(t(e))}}handleStreamStart(e){try{this.uiStateManager.updateVideoPreview(e),this.uiStateManager.handleStreamStart(this.controller.getRecordingState(),()=>{this.audioLevelVisualizer.initializeBars(this.shadow)}),this.updateUIForFeatureFlags();try{this.startAudioLevelTrackingForStream(e)}catch{}}catch(e){this.uiStateManager.showError(t(e))}}handleStreamStop(){this.uiStateManager.handleStreamStop(()=>this.controller.stopAudioLevelTracking())}handleRecordingStart(){if(this.showSettings){this.showSettings=!1;let e=this.shadow.querySelector(`#settingsPanel`);if(!e)throw Error(`Settings panel element not found`);e.style.display=`none`}this.uiStateManager.handleRecordingStart(this.controller.isPaused()),this.updateUIForFeatureFlags();let e=this.controller.getAudioStreamForAnalysis();e&&this.startAudioLevelTrackingForStream(e)}handleRecordingStop(e){return this.recordedBlob=e,this.uiStateManager.updateRecordingControlsAfterStop(),this.updateUIForFeatureFlags(),Promise.resolve()}updateRecordingTimer(e){this.uiStateManager.updateRecordingTimer(e)}updateMuteState(e){this.isMuted=e,this.uiStateManager.updateMuteState(e,(e,t)=>this.audioLevelVisualizer.updateBars(e,t),this.controller.getAudioLevel())}updateVideoPreview(e){this.uiStateManager.updateVideoPreview(e)}showError(e){this.uiStateManager.showError(e)}extractErrorMessage(e){return t(e)}getStreamManager(){return this.controller.getStreamManager()}getShadowRoot(){return this.shadow}getController(){return this.controller}getUIStateManager(){return this.uiStateManager}getDeviceManager(){return this.deviceManager}getAudioLevelVisualizer(){return this.audioLevelVisualizer}getRecordedBlob(){return this.recordedBlob}setRecordedBlob(e){this.recordedBlob=e}getProcessedBlob(){return this.processedBlob}setProcessedBlob(e){this.processedBlob=e}getIsProcessing(){return this.isProcessing}setIsProcessing(e){this.isProcessing=e}getIsMuted(){return this.isMuted}setIsMuted(e){this.isMuted=e}getShowSettings(){return this.showSettings}setShowSettings(e){this.showSettings=e}getUserMetadata(){return this.userMetadata}getNormalizedBackendUrl(e){return ks(e)}updateFeatureFlags(){let e=this.getAttribute(`enable-source-switching`);this.enableSourceSwitching=e===null||e!==`false`;let t=this.getAttribute(`enable-mute`);this.enableMute=t===null||t!==`false`;let n=this.getAttribute(`enable-pause`);this.enablePause=n===null||n!==`false`;let r=this.getAttribute(`enable-device-change`);this.enableDeviceChange=r===null||r!==`false`}initializeUIState(){let e=this.shadow.querySelector(`#muteButton`),t=this.shadow.querySelector(`#pauseButton`),n=this.shadow.querySelector(`#resumeButton`),r=this.shadow.querySelector(`#switchSourceButton`),i=this.shadow.querySelector(`#stopButton`),a=this.shadow.querySelector(`#settingsButton`);e&&(e.style.display=`none`),t&&(t.style.display=`none`),n&&(n.style.display=`none`),r&&(r.style.display=`none`),i&&(i.style.display=`none`),a&&(a.style.display=this.enableDeviceChange?`flex`:`none`)}updateUIForFeatureFlags(){let e=this.controller.getRecordingState()===`recording`,t=this.controller.isPaused();this.updateButtonVisibility(`#switchSourceButton`,e&&this.enableSourceSwitching),this.updateButtonVisibility(`#muteButton`,e&&this.enableMute),this.updateButtonVisibility(`#pauseButton`,e&&this.enablePause&&!t),this.updateButtonVisibility(`#resumeButton`,e&&this.enablePause&&t),this.updateButtonVisibility(`#settingsButton`,!e&&this.enableDeviceChange)}updateButtonVisibility(e,t){let n=this.shadow.querySelector(e);n&&(n.style.display=t?`flex`:`none`)}getEnableSourceSwitching(){return this.enableSourceSwitching}getEnableMute(){return this.enableMute}getEnablePause(){return this.enablePause}getEnableDeviceChange(){return this.enableDeviceChange}getI18nManager(){return this.i18nManager}updateTemplate(){if(!this.shadowRoot)return;let e=Ds(this.isMuted,this.i18nManager.getAll(),this.controller.getCurrentSourceType());this.shadowRoot.innerHTML=`<style>${ws}</style>${e}`,ms(this),this.uiStateManager.setShadowRoot(this.shadowRoot),this.initializeUIState(),this.controller.getStreamManager().getStream()&&this.uiStateManager.updateVideoPreview(this.controller.getStreamManager().getStream())}};return customElements.get(`vidtreo-recorder`)||customElements.define(`vidtreo-recorder`,js),e.VidtreoRecorder=js,e})({});
|