@meframe/core 0.1.7 → 0.1.9

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.
Files changed (82) hide show
  1. package/dist/cache/CacheManager.d.ts +1 -1
  2. package/dist/cache/CacheManager.d.ts.map +1 -1
  3. package/dist/cache/CacheManager.js +4 -3
  4. package/dist/cache/CacheManager.js.map +1 -1
  5. package/dist/cache/l1/AudioL1Cache.d.ts +3 -8
  6. package/dist/cache/l1/AudioL1Cache.d.ts.map +1 -1
  7. package/dist/cache/l1/AudioL1Cache.js +41 -56
  8. package/dist/cache/l1/AudioL1Cache.js.map +1 -1
  9. package/dist/controllers/PlaybackController.d.ts +2 -0
  10. package/dist/controllers/PlaybackController.d.ts.map +1 -1
  11. package/dist/controllers/PlaybackController.js +75 -14
  12. package/dist/controllers/PlaybackController.js.map +1 -1
  13. package/dist/controllers/PlaybackStateMachine.d.ts.map +1 -1
  14. package/dist/controllers/PlaybackStateMachine.js +33 -10
  15. package/dist/controllers/PlaybackStateMachine.js.map +1 -1
  16. package/dist/controllers/types.d.ts +13 -3
  17. package/dist/controllers/types.d.ts.map +1 -1
  18. package/dist/controllers/types.js +2 -0
  19. package/dist/controllers/types.js.map +1 -1
  20. package/dist/model/types.d.ts +0 -1
  21. package/dist/model/types.d.ts.map +1 -1
  22. package/dist/model/types.js.map +1 -1
  23. package/dist/orchestrator/ExportScheduler.d.ts.map +1 -1
  24. package/dist/orchestrator/ExportScheduler.js +22 -19
  25. package/dist/orchestrator/ExportScheduler.js.map +1 -1
  26. package/dist/orchestrator/GlobalAudioSession.d.ts +15 -2
  27. package/dist/orchestrator/GlobalAudioSession.d.ts.map +1 -1
  28. package/dist/orchestrator/GlobalAudioSession.js +48 -20
  29. package/dist/orchestrator/GlobalAudioSession.js.map +1 -1
  30. package/dist/orchestrator/OnDemandVideoSession.d.ts.map +1 -1
  31. package/dist/orchestrator/OnDemandVideoSession.js +13 -0
  32. package/dist/orchestrator/OnDemandVideoSession.js.map +1 -1
  33. package/dist/orchestrator/Orchestrator.d.ts +1 -1
  34. package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
  35. package/dist/orchestrator/Orchestrator.js +52 -20
  36. package/dist/orchestrator/Orchestrator.js.map +1 -1
  37. package/dist/orchestrator/VideoClipSession.d.ts.map +1 -1
  38. package/dist/orchestrator/VideoClipSession.js +4 -2
  39. package/dist/orchestrator/VideoClipSession.js.map +1 -1
  40. package/dist/orchestrator/types.d.ts +7 -1
  41. package/dist/orchestrator/types.d.ts.map +1 -1
  42. package/dist/stages/load/ResourceLoader.d.ts +6 -5
  43. package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
  44. package/dist/stages/load/ResourceLoader.js +62 -41
  45. package/dist/stages/load/ResourceLoader.js.map +1 -1
  46. package/dist/stages/load/TaskManager.d.ts +5 -12
  47. package/dist/stages/load/TaskManager.d.ts.map +1 -1
  48. package/dist/stages/load/TaskManager.js +60 -46
  49. package/dist/stages/load/TaskManager.js.map +1 -1
  50. package/dist/stages/mux/MP4Muxer.d.ts.map +1 -1
  51. package/dist/stages/mux/MP4Muxer.js +3 -0
  52. package/dist/stages/mux/MP4Muxer.js.map +1 -1
  53. package/dist/utils/platform-utils.d.ts +1 -0
  54. package/dist/utils/platform-utils.d.ts.map +1 -1
  55. package/dist/utils/platform-utils.js +19 -6
  56. package/dist/utils/platform-utils.js.map +1 -1
  57. package/dist/worker/BaseWorker.d.ts +2 -0
  58. package/dist/worker/BaseWorker.d.ts.map +1 -1
  59. package/dist/worker/BaseWorker.js.map +1 -1
  60. package/dist/worker/WorkerChannel.d.ts +2 -0
  61. package/dist/worker/WorkerChannel.d.ts.map +1 -1
  62. package/dist/worker/WorkerChannel.js +17 -1
  63. package/dist/worker/WorkerChannel.js.map +1 -1
  64. package/dist/workers/{WorkerChannel.DjBEVvEA.js → WorkerChannel.DQK8rAab.js} +18 -2
  65. package/dist/workers/{WorkerChannel.DjBEVvEA.js.map → WorkerChannel.DQK8rAab.js.map} +1 -1
  66. package/dist/workers/stages/compose/{audio-compose.worker.CiM_KP27.js → audio-compose.worker.B4Io5w9i.js} +2 -2
  67. package/dist/workers/stages/compose/{audio-compose.worker.CiM_KP27.js.map → audio-compose.worker.B4Io5w9i.js.map} +1 -1
  68. package/dist/workers/stages/compose/{video-compose.worker.CQwmNfXT.js → video-compose.worker.CA2_Kpg-.js} +2 -2
  69. package/dist/workers/stages/compose/{video-compose.worker.CQwmNfXT.js.map → video-compose.worker.CA2_Kpg-.js.map} +1 -1
  70. package/dist/workers/stages/decode/{audio-decode.worker.CpjkrZtT.js → audio-decode.worker.-DGlQrJD.js} +2 -2
  71. package/dist/workers/stages/decode/{audio-decode.worker.CpjkrZtT.js.map → audio-decode.worker.-DGlQrJD.js.map} +1 -1
  72. package/dist/workers/stages/decode/{video-decode.worker.BQtw6eWn.js → video-decode.worker.BnWVUkng.js} +2 -2
  73. package/dist/workers/stages/decode/video-decode.worker.BnWVUkng.js.map +1 -0
  74. package/dist/workers/stages/demux/{audio-demux.worker.C4V11GQi.js → audio-demux.worker.D-_LoVqW.js} +2 -2
  75. package/dist/workers/stages/demux/{audio-demux.worker.C4V11GQi.js.map → audio-demux.worker.D-_LoVqW.js.map} +1 -1
  76. package/dist/workers/stages/demux/{video-demux.worker.5pJr0Ij-.js → video-demux.worker.BWDrLGni.js} +2 -2
  77. package/dist/workers/stages/demux/{video-demux.worker.5pJr0Ij-.js.map → video-demux.worker.BWDrLGni.js.map} +1 -1
  78. package/dist/workers/stages/encode/{video-encode.worker.CX2_3YhQ.js → video-encode.worker.D6aB_rF9.js} +2 -2
  79. package/dist/workers/stages/encode/{video-encode.worker.CX2_3YhQ.js.map → video-encode.worker.D6aB_rF9.js.map} +1 -1
  80. package/dist/workers/worker-manifest.json +7 -7
  81. package/package.json +1 -1
  82. package/dist/workers/stages/decode/video-decode.worker.BQtw6eWn.js.map +0 -1
@@ -120,7 +120,7 @@ export declare class CacheManager {
120
120
  startTimeUs?: TimeUs;
121
121
  }): Promise<boolean>;
122
122
  clearVideoCache(): void;
123
- clear(): Promise<void>;
123
+ clear(): void;
124
124
  getMetadata(): {
125
125
  l1: import('./l1/VideoL1Cache').L1CacheMetadata;
126
126
  };
@@ -1 +1 @@
1
- {"version":3,"file":"CacheManager.d.ts","sourceRoot":"","sources":["../../src/cache/CacheManager.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAIvD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEtD,UAAU,kBAAkB;IAC1B,EAAE,EAAE;QACF,WAAW,EAAE,MAAM,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,aAAa,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,QAAQ,EAAE;QACR,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAYD;;;;;;;;GAQG;AACH,qBAAa,YAAY;IACvB,QAAQ,CAAC,YAAY,EAAE,YAAY,CAAC;IACpC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAC;IAC5C,OAAO,CAAC,gBAAgB,CAAsC;IA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AAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI;IAIlD,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInD;;OAEG;IACH,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIxC;;OAEG;IACH,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAIpC;;;;OAIG;IACH,gBAAgB,CACd,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,WAAW,CAAC,EAAE,MAAM,CAAA;KAAO,GACjF,OAAO,CAAC,OAAO,CAAC;IA0CnB,eAAe,IAAI,IAAI;IAIvB,KAAK,IAAI,IAAI;IAKb,WAAW;;;IAMX;;;OAGG;IACH,uBAAuB,CAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,GAAG,IAAI;CAsBxE"}
@@ -1,6 +1,6 @@
1
1
  import { VideoL1Cache } from "./l1/VideoL1Cache.js";
2
2
  import { MeframeEvent } from "../event/events.js";
3
- import { AudioL1Cache } from "./l1/AudioL1Cache.js";
3
+ import { AudioL1Cache, EXPORT_PCM_COVERAGE_THRESHOLD, PREVIEW_PCM_COVERAGE_THRESHOLD } from "./l1/AudioL1Cache.js";
4
4
  import { WaiterReplacedError } from "../utils/errors.js";
5
5
  import { OPFSManager } from "./storage/opfs/OPFSManager.js";
6
6
  import { ResourceCache } from "./resource/ResourceCache.js";
@@ -72,7 +72,8 @@ class CacheManager {
72
72
  * Returns true only if at least threshold (default 80%) of requested duration is available
73
73
  */
74
74
  hasWindowPCM(clipId, startUs, endUs, strictMode) {
75
- return this.audioL1Cache.hasWindowData(clipId, startUs, endUs, strictMode);
75
+ const threshold = strictMode ? EXPORT_PCM_COVERAGE_THRESHOLD : PREVIEW_PCM_COVERAGE_THRESHOLD;
76
+ return this.audioL1Cache.getAudioRangeCoverage(clipId, startUs, endUs, threshold).covered;
76
77
  }
77
78
  /**
78
79
  * Get audio range coverage information for incremental decoding
@@ -186,7 +187,7 @@ class CacheManager {
186
187
  clearVideoCache() {
187
188
  this.videoL1Cache.clear();
188
189
  }
189
- async clear() {
190
+ clear() {
190
191
  this.clearVideoCache();
191
192
  this.clearAudioCache();
192
193
  }
@@ -1 +1 @@
1
- {"version":3,"file":"CacheManager.js","sources":["../../src/cache/CacheManager.ts"],"sourcesContent":["import type { TimeUs } from '../model/types';\nimport { RcFrame } from '../model';\nimport { VideoL1Cache } from './l1/VideoL1Cache';\nimport { MeframeEvent } from '../event/events';\nimport type { EventBus } from '../event/EventBus';\nimport type { EventPayloadMap } from '../event/events';\nimport { AudioL1Cache } from './l1/AudioL1Cache';\nimport { WaiterReplacedError } from '../utils/errors';\nimport { OPFSManager } from './storage/opfs/OPFSManager';\nimport { ResourceCache } from './resource/ResourceCache';\nimport { MP4IndexCache } from './resource/MP4IndexCache';\nimport { AudioSampleCache } from './resource/AudioSampleCache';\nimport type { MP4Index } from '../stages/demux/types';\n\ninterface CacheManagerConfig {\n l1: {\n maxMemoryMB: number;\n maxGOPs?: number;\n gopIntervalUs?: number;\n };\n resource: {\n projectId: string;\n };\n}\n\ninterface ClipReadyWaiter {\n clipId: string;\n minFrameCount: number;\n startTimeUs: TimeUs;\n currentCount: number;\n resolve: (ready: boolean) => void;\n reject: (reason?: unknown) => void;\n timeoutId?: ReturnType<typeof setTimeout>;\n}\n\n/**\n * CacheManager for Window Cache Architecture\n *\n * Core features:\n * - L1 (VRAM) for composed VideoFrames (window cache ±3s)\n * - ResourceCache (OPFS) for original MP4 files\n * - MP4IndexCache (RAM) for GOP/Sample indexes\n * - AudioSampleCache (RAM) for extracted audio chunks\n */\nexport class CacheManager {\n readonly videoL1Cache: VideoL1Cache;\n private readonly audioL1Cache: AudioL1Cache;\n readonly resourceCache: ResourceCache;\n readonly mp4IndexCache: MP4IndexCache;\n readonly audioSampleCache: AudioSampleCache;\n private clipReadyWaiters = new Map<string, ClipReadyWaiter>();\n private eventBus?: EventBus<EventPayloadMap>;\n\n constructor(config: CacheManagerConfig, eventBus?: EventBus<EventPayloadMap>) {\n this.videoL1Cache = new VideoL1Cache(config.l1);\n this.audioL1Cache = new AudioL1Cache();\n\n const opfsManager = new OPFSManager();\n this.resourceCache = new ResourceCache(opfsManager, config.resource.projectId);\n this.mp4IndexCache = new MP4IndexCache();\n this.audioSampleCache = new AudioSampleCache();\n this.eventBus = eventBus;\n }\n\n async init(): Promise<void> {\n await this.resourceCache.init();\n }\n\n /**\n * Set window center for L1 cache management (unified for video and audio)\n * L1 cache uses center ±3.5s window for video, ±5s for audio\n */\n setWindow(centerTimeUs: TimeUs): void {\n this.videoL1Cache.setWindow(centerTimeUs);\n this.audioL1Cache.setWindow(centerTimeUs);\n }\n\n /**\n * Check if resource exists in OPFS cache\n */\n async hasResourceInCache(resourceId: string): Promise<boolean> {\n return await this.resourceCache.hasResource(resourceId);\n }\n\n /**\n * Read byte range from resource (for GOP-level access)\n */\n async readResourceRange(resourceId: string, start: number, end: number): Promise<ArrayBuffer> {\n return await this.resourceCache.readRange(resourceId, start, end);\n }\n\n /**\n * Get MP4 index for a resource\n */\n getMP4Index(resourceId: string): MP4Index | null {\n return this.mp4IndexCache.get(resourceId);\n }\n\n putClipAudioData(\n clipId: string,\n audioData: AudioData,\n clipDurationUs: TimeUs,\n globalTimeUs?: TimeUs\n ): void {\n this.audioL1Cache.putClipAudioData(clipId, audioData, clipDurationUs, globalTimeUs);\n }\n\n getClipPCMWithMetadata(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs\n ): { planes: Float32Array[]; sampleRate: number; numberOfChannels: number } | null {\n return this.audioL1Cache.getPCMWithMetadata(clipId, startUs, endUs);\n }\n\n hasClipPCM(clipId: string): boolean {\n return this.audioL1Cache.hasClipPCM(clipId);\n }\n\n /**\n * Check if a specific audio chunk is already cached (by timestamp)\n */\n hasAudioSlotAt(clipId: string, timestampUs: TimeUs): boolean {\n return this.audioL1Cache.hasSlotAt(clipId, timestampUs);\n }\n\n /**\n * Check if sufficient PCM data exists for the requested time window\n * Returns true only if at least threshold (default 80%) of requested duration is available\n */\n hasWindowPCM(clipId: string, startUs: TimeUs, endUs: TimeUs, strictMode?: boolean): boolean {\n return this.audioL1Cache.hasWindowData(clipId, startUs, endUs, strictMode);\n }\n\n /**\n * Get audio range coverage information for incremental decoding\n * @param clipId - Clip identifier\n * @param startUs - Range start time in microseconds\n * @param endUs - Range end time in microseconds\n * @param threshold - Coverage threshold (0-1)\n * @returns Coverage info { covered: boolean, coverageRatio: number }\n */\n getAudioRangeCoverage(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs,\n threshold?: number\n ): { covered: boolean; coverageRatio: number } {\n return this.audioL1Cache.getAudioRangeCoverage(clipId, startUs, endUs, threshold);\n }\n\n clearAudioCache(): void {\n this.audioL1Cache.clear();\n }\n\n clearClipAudioData(clipId: string): void {\n this.audioL1Cache.clearClipPCM(clipId);\n }\n\n /**\n * Add a frame to L1 cache\n * Handles event notifications (CacheCover, ComposeFrameReady)\n * @param frame - VideoFrame to add\n * @param clipId - Clip identifier\n * @param frameDuration - Frame duration in microseconds\n * @param trackId - Track identifier\n * @param globalTimeUs - Global timestamp for event emission and window management\n */\n addFrame(\n frame: VideoFrame,\n clipId: string,\n frameDuration: TimeUs,\n trackId: string,\n globalTimeUs: TimeUs\n ): RcFrame {\n const rcFrame = this.videoL1Cache.addFrame(frame, clipId, frameDuration, trackId, globalTimeUs);\n\n const relativeTimeUs = frame.timestamp ?? 0;\n\n // Check and notify clip ready\n this.checkAndNotifyClipReady(clipId, relativeTimeUs);\n\n // Emit cover event for first frame (globalTimeUs = 0 in composition)\n if (globalTimeUs === 0) {\n this.eventBus?.emit(MeframeEvent.CacheCover, {\n timeUs: globalTimeUs,\n clipId,\n level: 'L1',\n size: rcFrame.sizeEstimate ?? 0,\n });\n }\n\n // Emit frame ready event\n this.eventBus?.emit(MeframeEvent.ComposeFrameReady, {\n timeUs: globalTimeUs,\n frameNumber: Math.floor(globalTimeUs / frameDuration),\n renderTimeMs: 0,\n trackId,\n clipId,\n });\n\n return rcFrame;\n }\n\n /**\n * Get frame from L1 cache\n * @param timeUs - Clip-relative timestamp (0-based)\n * @param clipId - Clip identifier\n */\n getFrame(timeUs: TimeUs, clipId: string): RcFrame | null {\n return this.videoL1Cache.get(timeUs, clipId);\n }\n\n async invalidateClip(clipId: string): Promise<void> {\n this.videoL1Cache.invalidateClip(clipId);\n }\n\n /**\n * Evict a clip from L1 cache\n */\n invalidateClipInL1(clipId: string): void {\n this.videoL1Cache.invalidateClip(clipId);\n }\n\n /**\n * Check if a clip is cached in L1\n */\n hasClipInL1(clipId: string): boolean {\n return this.videoL1Cache.hasClip(clipId);\n }\n\n /**\n * Wait for a clip to have minimum frames cached\n * Used by PlaybackController for buffering state\n * Only one waiter per clip - new waiter replaces old one\n */\n waitForClipReady(\n clipId: string,\n options: { minFrameCount?: number; timeoutMs?: number; startTimeUs?: TimeUs } = {}\n ): Promise<boolean> {\n const minFrameCount = options.minFrameCount ?? 30;\n const startTimeUs = options.startTimeUs ?? 0;\n\n // Check if already have enough frames\n const currentFrameCount = this.videoL1Cache.getClipFrameCount(clipId, startTimeUs);\n if (currentFrameCount >= minFrameCount) {\n return Promise.resolve(true);\n }\n\n // Cancel previous waiter if exists\n const oldWaiter = this.clipReadyWaiters.get(clipId);\n if (oldWaiter) {\n if (oldWaiter.timeoutId) {\n clearTimeout(oldWaiter.timeoutId);\n }\n oldWaiter.reject(new WaiterReplacedError(clipId));\n }\n\n return new Promise<boolean>((resolve, reject) => {\n const waiter: ClipReadyWaiter = {\n clipId,\n minFrameCount,\n startTimeUs,\n currentCount: currentFrameCount,\n resolve,\n reject,\n };\n\n this.clipReadyWaiters.set(clipId, waiter);\n\n if (options.timeoutMs && options.timeoutMs > 0) {\n waiter.timeoutId = setTimeout(() => {\n if (this.clipReadyWaiters.get(clipId) === waiter) {\n this.clipReadyWaiters.delete(clipId);\n }\n resolve(false);\n }, options.timeoutMs);\n }\n });\n }\n\n clearVideoCache(): void {\n this.videoL1Cache.clear();\n }\n\n async clear(): Promise<void> {\n this.clearVideoCache();\n this.clearAudioCache();\n }\n\n getMetadata() {\n return {\n l1: this.videoL1Cache.getMetadata(),\n };\n }\n\n /**\n * Check if incoming frame satisfies clip ready condition\n * O(1) complexity - only checks single waiter and increments counter\n */\n checkAndNotifyClipReady(clipId: string, frameTimestampUs: TimeUs): void {\n const waiter = this.clipReadyWaiters.get(clipId);\n if (!waiter) {\n return;\n }\n\n // Count all frames if startTimeUs is 0 (buffering scenario)\n // Otherwise only count frames at or after the target start time\n const shouldCount = waiter.startTimeUs === 0 || frameTimestampUs >= waiter.startTimeUs;\n\n if (shouldCount) {\n waiter.currentCount++;\n\n if (waiter.currentCount >= waiter.minFrameCount) {\n if (waiter.timeoutId) {\n clearTimeout(waiter.timeoutId);\n }\n waiter.resolve(true);\n this.clipReadyWaiters.delete(clipId);\n }\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;AA4CO,MAAM,aAAa;AAAA,EACf;AAAA,EACQ;AAAA,EACR;AAAA,EACA;AAAA,EACA;AAAA,EACD,uCAAuB,IAAA;AAAA,EACvB;AAAA,EAER,YAAY,QAA4B,UAAsC;AAC5E,SAAK,eAAe,IAAI,aAAa,OAAO,EAAE;AAC9C,SAAK,eAAe,IAAI,aAAA;AAExB,UAAM,cAAc,IAAI,YAAA;AACxB,SAAK,gBAAgB,IAAI,cAAc,aAAa,OAAO,SAAS,SAAS;AAC7E,SAAK,gBAAgB,IAAI,cAAA;AACzB,SAAK,mBAAmB,IAAI,iBAAA;AAC5B,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,MAAM,OAAsB;AAC1B,UAAM,KAAK,cAAc,KAAA;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,cAA4B;AACpC,SAAK,aAAa,UAAU,YA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":3,"file":"CacheManager.js","sources":["../../src/cache/CacheManager.ts"],"sourcesContent":["import type { TimeUs } from '../model/types';\nimport { RcFrame } from '../model';\nimport { VideoL1Cache } from './l1/VideoL1Cache';\nimport { MeframeEvent } from '../event/events';\nimport type { EventBus } from '../event/EventBus';\nimport type { EventPayloadMap } from '../event/events';\nimport {\n AudioL1Cache,\n EXPORT_PCM_COVERAGE_THRESHOLD,\n PREVIEW_PCM_COVERAGE_THRESHOLD,\n} from './l1/AudioL1Cache';\nimport { WaiterReplacedError } from '../utils/errors';\nimport { OPFSManager } from './storage/opfs/OPFSManager';\nimport { ResourceCache } from './resource/ResourceCache';\nimport { MP4IndexCache } from './resource/MP4IndexCache';\nimport { AudioSampleCache } from './resource/AudioSampleCache';\nimport type { MP4Index } from '../stages/demux/types';\n\ninterface CacheManagerConfig {\n l1: {\n maxMemoryMB: number;\n maxGOPs?: number;\n gopIntervalUs?: number;\n };\n resource: {\n projectId: string;\n };\n}\n\ninterface ClipReadyWaiter {\n clipId: string;\n minFrameCount: number;\n startTimeUs: TimeUs;\n currentCount: number;\n resolve: (ready: boolean) => void;\n reject: (reason?: unknown) => void;\n timeoutId?: ReturnType<typeof setTimeout>;\n}\n\n/**\n * CacheManager for Window Cache Architecture\n *\n * Core features:\n * - L1 (VRAM) for composed VideoFrames (window cache ±3s)\n * - ResourceCache (OPFS) for original MP4 files\n * - MP4IndexCache (RAM) for GOP/Sample indexes\n * - AudioSampleCache (RAM) for extracted audio chunks\n */\nexport class CacheManager {\n readonly videoL1Cache: VideoL1Cache;\n private readonly audioL1Cache: AudioL1Cache;\n readonly resourceCache: ResourceCache;\n readonly mp4IndexCache: MP4IndexCache;\n readonly audioSampleCache: AudioSampleCache;\n private clipReadyWaiters = new Map<string, ClipReadyWaiter>();\n private eventBus?: EventBus<EventPayloadMap>;\n\n constructor(config: CacheManagerConfig, eventBus?: EventBus<EventPayloadMap>) {\n this.videoL1Cache = new VideoL1Cache(config.l1);\n this.audioL1Cache = new AudioL1Cache();\n\n const opfsManager = new OPFSManager();\n this.resourceCache = new ResourceCache(opfsManager, config.resource.projectId);\n this.mp4IndexCache = new MP4IndexCache();\n this.audioSampleCache = new AudioSampleCache();\n this.eventBus = eventBus;\n }\n\n async init(): Promise<void> {\n await this.resourceCache.init();\n }\n\n /**\n * Set window center for L1 cache management (unified for video and audio)\n * L1 cache uses center ±3.5s window for video, ±5s for audio\n */\n setWindow(centerTimeUs: TimeUs): void {\n this.videoL1Cache.setWindow(centerTimeUs);\n this.audioL1Cache.setWindow(centerTimeUs);\n }\n\n /**\n * Check if resource exists in OPFS cache\n */\n async hasResourceInCache(resourceId: string): Promise<boolean> {\n return await this.resourceCache.hasResource(resourceId);\n }\n\n /**\n * Read byte range from resource (for GOP-level access)\n */\n async readResourceRange(resourceId: string, start: number, end: number): Promise<ArrayBuffer> {\n return await this.resourceCache.readRange(resourceId, start, end);\n }\n\n /**\n * Get MP4 index for a resource\n */\n getMP4Index(resourceId: string): MP4Index | null {\n return this.mp4IndexCache.get(resourceId);\n }\n\n putClipAudioData(\n clipId: string,\n audioData: AudioData,\n clipDurationUs: TimeUs,\n globalTimeUs?: TimeUs\n ): void {\n this.audioL1Cache.putClipAudioData(clipId, audioData, clipDurationUs, globalTimeUs);\n }\n\n getClipPCMWithMetadata(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs\n ): { planes: Float32Array[]; sampleRate: number; numberOfChannels: number } | null {\n return this.audioL1Cache.getPCMWithMetadata(clipId, startUs, endUs);\n }\n\n hasClipPCM(clipId: string): boolean {\n return this.audioL1Cache.hasClipPCM(clipId);\n }\n\n /**\n * Check if a specific audio chunk is already cached (by timestamp)\n */\n hasAudioSlotAt(clipId: string, timestampUs: TimeUs): boolean {\n return this.audioL1Cache.hasSlotAt(clipId, timestampUs);\n }\n\n /**\n * Check if sufficient PCM data exists for the requested time window\n * Returns true only if at least threshold (default 80%) of requested duration is available\n */\n hasWindowPCM(clipId: string, startUs: TimeUs, endUs: TimeUs, strictMode?: boolean): boolean {\n const threshold = strictMode ? EXPORT_PCM_COVERAGE_THRESHOLD : PREVIEW_PCM_COVERAGE_THRESHOLD;\n return this.audioL1Cache.getAudioRangeCoverage(clipId, startUs, endUs, threshold).covered;\n }\n\n /**\n * Get audio range coverage information for incremental decoding\n * @param clipId - Clip identifier\n * @param startUs - Range start time in microseconds\n * @param endUs - Range end time in microseconds\n * @param threshold - Coverage threshold (0-1)\n * @returns Coverage info { covered: boolean, coverageRatio: number }\n */\n getAudioRangeCoverage(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs,\n threshold?: number\n ): { covered: boolean; coverageRatio: number } {\n return this.audioL1Cache.getAudioRangeCoverage(clipId, startUs, endUs, threshold);\n }\n\n clearAudioCache(): void {\n this.audioL1Cache.clear();\n }\n\n clearClipAudioData(clipId: string): void {\n this.audioL1Cache.clearClipPCM(clipId);\n }\n\n /**\n * Add a frame to L1 cache\n * Handles event notifications (CacheCover, ComposeFrameReady)\n * @param frame - VideoFrame to add\n * @param clipId - Clip identifier\n * @param frameDuration - Frame duration in microseconds\n * @param trackId - Track identifier\n * @param globalTimeUs - Global timestamp for event emission and window management\n */\n addFrame(\n frame: VideoFrame,\n clipId: string,\n frameDuration: TimeUs,\n trackId: string,\n globalTimeUs: TimeUs\n ): RcFrame {\n const rcFrame = this.videoL1Cache.addFrame(frame, clipId, frameDuration, trackId, globalTimeUs);\n\n const relativeTimeUs = frame.timestamp ?? 0;\n\n // Check and notify clip ready\n this.checkAndNotifyClipReady(clipId, relativeTimeUs);\n\n // Emit cover event for first frame (globalTimeUs = 0 in composition)\n if (globalTimeUs === 0) {\n this.eventBus?.emit(MeframeEvent.CacheCover, {\n timeUs: globalTimeUs,\n clipId,\n level: 'L1',\n size: rcFrame.sizeEstimate ?? 0,\n });\n }\n\n // Emit frame ready event\n this.eventBus?.emit(MeframeEvent.ComposeFrameReady, {\n timeUs: globalTimeUs,\n frameNumber: Math.floor(globalTimeUs / frameDuration),\n renderTimeMs: 0,\n trackId,\n clipId,\n });\n\n return rcFrame;\n }\n\n /**\n * Get frame from L1 cache\n * @param timeUs - Clip-relative timestamp (0-based)\n * @param clipId - Clip identifier\n */\n getFrame(timeUs: TimeUs, clipId: string): RcFrame | null {\n return this.videoL1Cache.get(timeUs, clipId);\n }\n\n async invalidateClip(clipId: string): Promise<void> {\n this.videoL1Cache.invalidateClip(clipId);\n }\n\n /**\n * Evict a clip from L1 cache\n */\n invalidateClipInL1(clipId: string): void {\n this.videoL1Cache.invalidateClip(clipId);\n }\n\n /**\n * Check if a clip is cached in L1\n */\n hasClipInL1(clipId: string): boolean {\n return this.videoL1Cache.hasClip(clipId);\n }\n\n /**\n * Wait for a clip to have minimum frames cached\n * Used by PlaybackController for buffering state\n * Only one waiter per clip - new waiter replaces old one\n */\n waitForClipReady(\n clipId: string,\n options: { minFrameCount?: number; timeoutMs?: number; startTimeUs?: TimeUs } = {}\n ): Promise<boolean> {\n const minFrameCount = options.minFrameCount ?? 30;\n const startTimeUs = options.startTimeUs ?? 0;\n\n // Check if already have enough frames\n const currentFrameCount = this.videoL1Cache.getClipFrameCount(clipId, startTimeUs);\n if (currentFrameCount >= minFrameCount) {\n return Promise.resolve(true);\n }\n\n // Cancel previous waiter if exists\n const oldWaiter = this.clipReadyWaiters.get(clipId);\n if (oldWaiter) {\n if (oldWaiter.timeoutId) {\n clearTimeout(oldWaiter.timeoutId);\n }\n oldWaiter.reject(new WaiterReplacedError(clipId));\n }\n\n return new Promise<boolean>((resolve, reject) => {\n const waiter: ClipReadyWaiter = {\n clipId,\n minFrameCount,\n startTimeUs,\n currentCount: currentFrameCount,\n resolve,\n reject,\n };\n\n this.clipReadyWaiters.set(clipId, waiter);\n\n if (options.timeoutMs && options.timeoutMs > 0) {\n waiter.timeoutId = setTimeout(() => {\n if (this.clipReadyWaiters.get(clipId) === waiter) {\n this.clipReadyWaiters.delete(clipId);\n }\n resolve(false);\n }, options.timeoutMs);\n }\n });\n }\n\n clearVideoCache(): void {\n this.videoL1Cache.clear();\n }\n\n clear(): void {\n this.clearVideoCache();\n this.clearAudioCache();\n }\n\n getMetadata() {\n return {\n l1: this.videoL1Cache.getMetadata(),\n };\n }\n\n /**\n * Check if incoming frame satisfies clip ready condition\n * O(1) complexity - only checks single waiter and increments counter\n */\n checkAndNotifyClipReady(clipId: string, frameTimestampUs: TimeUs): void {\n const waiter = this.clipReadyWaiters.get(clipId);\n if (!waiter) {\n return;\n }\n\n // Count all frames if startTimeUs is 0 (buffering scenario)\n // Otherwise only count frames at or after the target start time\n const shouldCount = waiter.startTimeUs === 0 || frameTimestampUs >= waiter.startTimeUs;\n\n if (shouldCount) {\n waiter.currentCount++;\n\n if (waiter.currentCount >= waiter.minFrameCount) {\n if (waiter.timeoutId) {\n clearTimeout(waiter.timeoutId);\n }\n waiter.resolve(true);\n this.clipReadyWaiters.delete(clipId);\n }\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;;AAgDO,MAAM,aAAa;AAAA,EACf;AAAA,EACQ;AAAA,EACR;AAAA,EACA;AAAA,EACA;AAAA,EACD,uCAAuB,IAAA;AAAA,EACvB;AAAA,EAER,YAAY,QAA4B,UAAsC;AAC5E,SAAK,eAAe,IAAI,aAAa,OAAO,EAAE;AAC9C,SAAK,eAAe,IAAI,aAAA;AAExB,UAAM,cAAc,IAAI,YAAA;AACxB,SAAK,gBAAgB,IAAI,cAAc,aAAa,OAAO,SAAS,SAAS;AAC7E,SAAK,gBAAgB,IAAI,cAAA;AACzB,SAAK,mBAAmB,IAAI,iBAAA;AAC5B,SAAK,WAAW;AAAA,EAClB;AAAA,EAEA,MAAM,OAAsB;AAC1B,UAAM,KAAK,cAAc,KAAA;AAAA,EAC3B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,UAAU,cAA4B;AACpC,SAAK,aAAa,UAAU,YAAY;AACxC,SAAK,aAAa,UAAU,YAAY;AAAA,EAC1C;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,mBAAmB,YAAsC;AAC7D,WAAO,MAA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D = 0.95;
4
+ export declare const EXPORT_PCM_COVERAGE_THRESHOLD = 0.99;
3
5
  interface AudioDataSlot {
4
6
  timestampUs: TimeUs;
5
7
  durationUs: TimeUs;
@@ -28,14 +30,6 @@ export declare class AudioL1Cache {
28
30
  * Check if a slot with specific timestamp exists (used for incremental decoding)
29
31
  */
30
32
  hasSlotAt(clipId: string, timestampUs: TimeUs, toleranceUs?: number): boolean;
31
- /**
32
- * Check if sufficient PCM data exists for the requested time window
33
- * @param clipId - Clip identifier
34
- * @param startUs - Window start time
35
- * @param endUs - Window end time
36
- * @param strictMode - If true, require 99% coverage (export). If false, accept 95% (preview)
37
- */
38
- hasWindowData(clipId: string, startUs: TimeUs, endUs: TimeUs, strictMode?: boolean): boolean;
39
33
  /**
40
34
  * Get audio range coverage information for incremental decoding
41
35
  * @param clipId - Clip identifier
@@ -48,6 +42,7 @@ export declare class AudioL1Cache {
48
42
  covered: boolean;
49
43
  coverageRatio: number;
50
44
  };
45
+ private computeRangeCoverageDuration;
51
46
  clearClipPCM(clipId: string): void;
52
47
  /**
53
48
  * Update window center (unified global window)
@@ -1 +1 @@
1
- {"version":3,"file":"AudioL1Cache.d.ts","sourceRoot":"","sources":["../../../src/cache/l1/AudioL1Cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAIhD,UAAU,aAAa;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,YAAY,EAAE,aAAa,EAAE,CAAC;AAE9B,qBAAa,YAAY;IAEvB,OAAO,CAAC,eAAe,CAAsC;IAI7D,OAAO,CAAC,YAAY,CAAa;IAGjC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAa;IAC3C,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GAAG,IAAI;IAiG7E,kBAAkB,CAChB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACZ;QAAE,MAAM,EAAE,YAAY,EAAE,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,gBAAgB,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI;IAoBlF,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAInC;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,GAAE,MAAa,GAAG,OAAO;IAmBnF;;;;;;;OAOG;IACH,qBAAqB,CACnB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,SAAS,SAAiC,GACzC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,aAAa,EAAE,MAAM,CAAA;KAAE;IAa9C,OAAO,CAAC,4BAA4B;IAsCpC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAIlC;;;OAGG;IACH,SAAS,CAAC,cAAc,EAAE,MAAM,GAAG,IAAI;IAKvC,OAAO,CAAC,aAAa;IAQrB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IA+BxB;;OAEG;IACH,OAAO,CAAC,eAAe;IAIvB,KAAK,IAAI,IAAI;IAIb,KAAK,IAAI,IAAI;IAMb,OAAO,IAAI,IAAI;CAGhB"}
@@ -1,5 +1,7 @@
1
1
  import { binarySearchOverlapping, binarySearchFirst } from "../../utils/binary-search.js";
2
2
  import { extractPlanesFromAudioData } from "../../utils/audio-data.js";
3
+ const PREVIEW_PCM_COVERAGE_THRESHOLD = 0.95;
4
+ const EXPORT_PCM_COVERAGE_THRESHOLD = 0.99;
3
5
  class AudioL1Cache {
4
6
  // Aligned with VideoL1Cache: array of discrete audio data slots per clip
5
7
  audioDataByClip = /* @__PURE__ */ new Map();
@@ -104,16 +106,17 @@ class AudioL1Cache {
104
106
  () => new Float32Array(totalFrames)
105
107
  );
106
108
  for (const slot of overlappingSlots) {
107
- const slotStartUs = slot.timestampUs;
108
- const slotEndUs = slotStartUs + slot.durationUs;
109
- const copyStartUs = Math.max(slotStartUs, startUs);
110
- const copyEndUs = Math.min(slotEndUs, endUs);
111
- if (copyStartUs >= copyEndUs) continue;
112
- const srcOffsetFrames = Math.floor(
113
- (copyStartUs - slotStartUs) / 1e6 * uniformSampleRate
109
+ const slotFrames = slot.planes[0]?.length ?? 0;
110
+ if (slotFrames <= 0) continue;
111
+ const slotStartFrame = Math.round(
112
+ (slot.timestampUs - startUs) / 1e6 * uniformSampleRate
114
113
  );
115
- const dstOffsetFrames = Math.floor((copyStartUs - startUs) / 1e6 * uniformSampleRate);
116
- const copyFrameCount = Math.ceil((copyEndUs - copyStartUs) / 1e6 * uniformSampleRate);
114
+ const slotEndFrame = slotStartFrame + slotFrames;
115
+ const dstStartFrame = Math.max(0, slotStartFrame);
116
+ const dstEndFrame = Math.min(totalFrames, slotEndFrame);
117
+ const copyFrameCount = dstEndFrame - dstStartFrame;
118
+ if (copyFrameCount <= 0) continue;
119
+ const srcOffsetFrames = dstStartFrame - slotStartFrame;
117
120
  for (let ch = 0; ch < uniformChannels; ch++) {
118
121
  const srcPlane = slot.planes[ch];
119
122
  const dstPlane = result[ch];
@@ -121,12 +124,13 @@ class AudioL1Cache {
121
124
  const actualCopyFrames = Math.min(
122
125
  copyFrameCount,
123
126
  srcPlane.length - srcOffsetFrames,
124
- dstPlane.length - dstOffsetFrames
127
+ dstPlane.length - dstStartFrame
128
+ );
129
+ if (actualCopyFrames <= 0) continue;
130
+ dstPlane.set(
131
+ srcPlane.subarray(srcOffsetFrames, srcOffsetFrames + actualCopyFrames),
132
+ dstStartFrame
125
133
  );
126
- if (actualCopyFrames > 0) {
127
- const srcSlice = srcPlane.subarray(srcOffsetFrames, srcOffsetFrames + actualCopyFrames);
128
- dstPlane.set(srcSlice, dstOffsetFrames);
129
- }
130
134
  }
131
135
  }
132
136
  return result;
@@ -167,38 +171,6 @@ class AudioL1Cache {
167
171
  }
168
172
  return false;
169
173
  }
170
- /**
171
- * Check if sufficient PCM data exists for the requested time window
172
- * @param clipId - Clip identifier
173
- * @param startUs - Window start time
174
- * @param endUs - Window end time
175
- * @param strictMode - If true, require 99% coverage (export). If false, accept 95% (preview)
176
- */
177
- hasWindowData(clipId, startUs, endUs, strictMode = false) {
178
- const slots = this.audioDataByClip.get(clipId);
179
- if (!slots || slots.length === 0) {
180
- return false;
181
- }
182
- const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({
183
- start: slot.timestampUs,
184
- end: slot.timestampUs + slot.durationUs
185
- }));
186
- if (overlappingSlots.length === 0) {
187
- return false;
188
- }
189
- let coveredDurationUs = 0;
190
- const requestedDurationUs = endUs - startUs;
191
- for (const slot of overlappingSlots) {
192
- const slotEndUs = slot.timestampUs + slot.durationUs;
193
- const overlapStart = Math.max(slot.timestampUs, startUs);
194
- const overlapEnd = Math.min(slotEndUs, endUs);
195
- if (overlapStart < overlapEnd) {
196
- coveredDurationUs += overlapEnd - overlapStart;
197
- }
198
- }
199
- const threshold = strictMode ? 0.99 : 0.95;
200
- return coveredDurationUs >= requestedDurationUs * threshold;
201
- }
202
174
  /**
203
175
  * Get audio range coverage information for incremental decoding
204
176
  * @param clipId - Clip identifier
@@ -207,20 +179,35 @@ class AudioL1Cache {
207
179
  * @param threshold - Coverage threshold (0-1), default 0.95
208
180
  * @returns Coverage info { covered: boolean, coverageRatio: number }
209
181
  */
210
- getAudioRangeCoverage(clipId, startUs, endUs, threshold = 0.95) {
182
+ getAudioRangeCoverage(clipId, startUs, endUs, threshold = PREVIEW_PCM_COVERAGE_THRESHOLD) {
183
+ const { coveredDurationUs, requestedDurationUs } = this.computeRangeCoverageDuration(
184
+ clipId,
185
+ startUs,
186
+ endUs
187
+ );
188
+ const coverageRatio = requestedDurationUs > 0 ? coveredDurationUs / requestedDurationUs : 0;
189
+ return {
190
+ covered: coverageRatio >= threshold,
191
+ coverageRatio
192
+ };
193
+ }
194
+ computeRangeCoverageDuration(clipId, startUs, endUs) {
195
+ const requestedDurationUs = Math.max(0, endUs - startUs);
196
+ if (requestedDurationUs === 0) {
197
+ return { coveredDurationUs: 0, requestedDurationUs: 0 };
198
+ }
211
199
  const slots = this.audioDataByClip.get(clipId);
212
200
  if (!slots || slots.length === 0) {
213
- return { covered: false, coverageRatio: 0 };
201
+ return { coveredDurationUs: 0, requestedDurationUs };
214
202
  }
215
203
  const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({
216
204
  start: slot.timestampUs,
217
205
  end: slot.timestampUs + slot.durationUs
218
206
  }));
219
207
  if (overlappingSlots.length === 0) {
220
- return { covered: false, coverageRatio: 0 };
208
+ return { coveredDurationUs: 0, requestedDurationUs };
221
209
  }
222
210
  let coveredDurationUs = 0;
223
- const requestedDurationUs = endUs - startUs;
224
211
  for (const slot of overlappingSlots) {
225
212
  const slotEndUs = slot.timestampUs + slot.durationUs;
226
213
  const overlapStart = Math.max(startUs, slot.timestampUs);
@@ -229,11 +216,7 @@ class AudioL1Cache {
229
216
  coveredDurationUs += overlapEnd - overlapStart;
230
217
  }
231
218
  }
232
- const coverageRatio = requestedDurationUs > 0 ? coveredDurationUs / requestedDurationUs : 0;
233
- return {
234
- covered: coverageRatio >= threshold,
235
- coverageRatio
236
- };
219
+ return { coveredDurationUs, requestedDurationUs };
237
220
  }
238
221
  clearClipPCM(clipId) {
239
222
  this.audioDataByClip.delete(clipId);
@@ -298,6 +281,8 @@ class AudioL1Cache {
298
281
  }
299
282
  }
300
283
  export {
301
- AudioL1Cache
284
+ AudioL1Cache,
285
+ EXPORT_PCM_COVERAGE_THRESHOLD,
286
+ PREVIEW_PCM_COVERAGE_THRESHOLD
302
287
  };
303
288
  //# sourceMappingURL=AudioL1Cache.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"AudioL1Cache.js","sources":["../../../src/cache/l1/AudioL1Cache.ts"],"sourcesContent":["import type { TimeUs } from '../../model/types';\nimport { binarySearchFirst, binarySearchOverlapping } from '../../utils/binary-search';\nimport { extractPlanesFromAudioData } from '../../utils/audio-data';\n\ninterface AudioDataSlot {\n timestampUs: TimeUs; // Resource timestamp (aligned with VideoL1Cache)\n durationUs: TimeUs;\n planes: Float32Array[]; // PCM data for this slot\n sampleRate: number;\n numberOfChannels: number;\n globalTimeUs?: TimeUs; // Global timeline time (for window management)\n}\n\nexport type { AudioDataSlot };\n\nexport class AudioL1Cache {\n // Aligned with VideoL1Cache: array of discrete audio data slots per clip\n private audioDataByClip = new Map<string, AudioDataSlot[]>();\n\n // Unified window management (aligned with VideoL1Cache)\n // All clips share the same global window center\n private windowCenter: TimeUs = 0;\n\n // Window radius aligned with video (±3.5s, but we use 5s for audio safety margin)\n private readonly WINDOW_RADIUS = 5_000_000; // ±5s\n private readonly EVICT_THROTTLE_MS = 500;\n private lastEvictTime = 0;\n\n putClipAudioData(\n clipId: string,\n audioData: AudioData,\n _clipDurationUs: TimeUs,\n globalTimeUs?: TimeUs\n ): void {\n const numberOfChannels = audioData.numberOfChannels ?? 2;\n const numberOfFrames = audioData.numberOfFrames ?? 0;\n const sampleRate = audioData.sampleRate ?? 48_000;\n const audioTimestampUs = audioData.timestamp ?? 0;\n const audioDurationUs =\n audioData.duration ?? Math.round((numberOfFrames / sampleRate) * 1_000_000);\n\n if (!numberOfChannels || !numberOfFrames) {\n audioData.close();\n return;\n }\n\n // Extract PCM data\n const planes = extractPlanesFromAudioData(audioData, numberOfChannels, numberOfFrames);\n audioData.close();\n\n // Create audio data slot (aligned with video architecture)\n const slot: AudioDataSlot = {\n timestampUs: audioTimestampUs,\n durationUs: audioDurationUs,\n planes,\n sampleRate,\n numberOfChannels,\n globalTimeUs,\n };\n // Get or create slots array for this clip\n let slots = this.audioDataByClip.get(clipId);\n if (!slots) {\n slots = [];\n this.audioDataByClip.set(clipId, slots);\n }\n\n // Insert slot in sorted order (aligned with VideoL1Cache.addFrame)\n const insertIndex = this.findInsertIndex(slots, audioTimestampUs);\n\n // Deduplication: Check for duplicate or near-duplicate timestamp\n // Tolerance of 1ms to handle floating-point precision and concurrent decode attempts\n const DUPLICATE_THRESHOLD_US = 0.5 * audioDurationUs;\n\n if (insertIndex < slots.length) {\n const existingSlot = slots[insertIndex]!;\n const timeDiff = Math.abs(existingSlot.timestampUs - audioTimestampUs);\n\n if (timeDiff < DUPLICATE_THRESHOLD_US) {\n // Near-duplicate detected, skip insertion\n return;\n }\n }\n\n // Also check previous slot to catch overlapping duplicates\n if (insertIndex > 0) {\n const prevSlot = slots[insertIndex - 1]!;\n const timeDiff = Math.abs(prevSlot.timestampUs - audioTimestampUs);\n\n if (timeDiff < DUPLICATE_THRESHOLD_US) {\n // Near-duplicate detected, skip insertion\n return;\n }\n }\n\n // Insert new slot\n slots.splice(insertIndex, 0, slot);\n }\n\n getSlotsInWindow(clipId: string, startUs: TimeUs, endUs: TimeUs): AudioDataSlot[] | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n // Use binary search to find overlapping slots\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return null;\n }\n\n return overlappingSlots;\n }\n\n getPCM(clipId: string, startUs: TimeUs, endUs: TimeUs): Float32Array[] | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n // Use binary search to find overlapping slots (O(log n + k) vs O(n))\n // Aligned with video GOP/frame search\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return null;\n }\n\n const requestedDurationUs = endUs - startUs;\n\n // Validate sample rate consistency across all slots\n const firstSlot = overlappingSlots[0]!;\n const uniformSampleRate = firstSlot.sampleRate;\n const uniformChannels = firstSlot.numberOfChannels;\n\n // Check if all slots have the same sample rate and channel count\n const hasUniformRate = overlappingSlots.every(\n (s) => s.sampleRate === uniformSampleRate && s.numberOfChannels === uniformChannels\n );\n\n if (!hasUniformRate) {\n console.error(\n `[AudioL1Cache] Inconsistent sample rates detected for clip ${clipId}:`,\n overlappingSlots.map((s) => ({\n timestamp: s.timestampUs,\n sampleRate: s.sampleRate,\n channels: s.numberOfChannels,\n }))\n );\n // Return null to avoid corrupted audio data\n // This will trigger re-decode with correct sample rate\n return null;\n }\n\n // Calculate total frame count needed\n const totalFrames = Math.ceil((requestedDurationUs / 1_000_000) * uniformSampleRate);\n\n // Initialize result arrays\n const result: Float32Array[] = Array.from(\n { length: uniformChannels },\n () => new Float32Array(totalFrames)\n );\n\n // Copy data from each overlapping slot\n // Note: Slots are guaranteed non-overlapping by putClipAudioData deduplication\n for (const slot of overlappingSlots) {\n const slotStartUs = slot.timestampUs;\n const slotEndUs = slotStartUs + slot.durationUs;\n\n // Calculate intersection with requested range\n const copyStartUs = Math.max(slotStartUs, startUs);\n const copyEndUs = Math.min(slotEndUs, endUs);\n\n if (copyStartUs >= copyEndUs) continue;\n\n // Convert time to frame indices (all slots have same sample rate after validation)\n const srcOffsetFrames = Math.floor(\n ((copyStartUs - slotStartUs) / 1_000_000) * uniformSampleRate\n );\n const dstOffsetFrames = Math.floor(((copyStartUs - startUs) / 1_000_000) * uniformSampleRate);\n const copyFrameCount = Math.ceil(((copyEndUs - copyStartUs) / 1_000_000) * uniformSampleRate);\n\n // Copy each channel\n for (let ch = 0; ch < uniformChannels; ch++) {\n const srcPlane = slot.planes[ch];\n const dstPlane = result[ch];\n if (!srcPlane || !dstPlane) continue;\n\n // Boundary check\n const actualCopyFrames = Math.min(\n copyFrameCount,\n srcPlane.length - srcOffsetFrames,\n dstPlane.length - dstOffsetFrames\n );\n\n if (actualCopyFrames > 0) {\n const srcSlice = srcPlane.subarray(srcOffsetFrames, srcOffsetFrames + actualCopyFrames);\n dstPlane.set(srcSlice, dstOffsetFrames);\n }\n }\n }\n\n return result;\n }\n\n getPCMWithMetadata(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs\n ): { planes: Float32Array[]; sampleRate: number; numberOfChannels: number } | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n const planes = this.getPCM(clipId, startUs, endUs);\n if (!planes) {\n return null;\n }\n\n // Use first slot's metadata\n const firstSlot = slots[0]!;\n return {\n planes,\n sampleRate: firstSlot.sampleRate,\n numberOfChannels: firstSlot.numberOfChannels,\n };\n }\n\n hasClipPCM(clipId: string): boolean {\n return this.audioDataByClip.has(clipId);\n }\n\n /**\n * Check if a slot with specific timestamp exists (used for incremental decoding)\n */\n hasSlotAt(clipId: string, timestampUs: TimeUs, toleranceUs: number = 1000): boolean {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots) return false;\n\n // Since slots are sorted, we can use binary search\n const index = this.findInsertIndex(slots, timestampUs);\n\n // Check index and index-1 (in case findInsertIndex lands after)\n if (index < slots.length) {\n const slot = slots[index];\n if (slot && Math.abs(slot.timestampUs - timestampUs) < toleranceUs) return true;\n }\n if (index > 0) {\n const slot = slots[index - 1];\n if (slot && Math.abs(slot.timestampUs - timestampUs) < toleranceUs) return true;\n }\n return false;\n }\n\n /**\n * Check if sufficient PCM data exists for the requested time window\n * @param clipId - Clip identifier\n * @param startUs - Window start time\n * @param endUs - Window end time\n * @param strictMode - If true, require 99% coverage (export). If false, accept 95% (preview)\n */\n hasWindowData(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs,\n strictMode: boolean = false\n ): boolean {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return false;\n }\n\n // Use binary search to find overlapping slots (performance optimization)\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return false;\n }\n\n // Calculate total duration covered\n let coveredDurationUs = 0;\n const requestedDurationUs = endUs - startUs;\n\n for (const slot of overlappingSlots) {\n const slotEndUs = slot.timestampUs + slot.durationUs;\n\n // Calculate overlap with requested range\n const overlapStart = Math.max(slot.timestampUs, startUs);\n const overlapEnd = Math.min(slotEndUs, endUs);\n\n if (overlapStart < overlapEnd) {\n coveredDurationUs += overlapEnd - overlapStart;\n }\n }\n\n // Adaptive threshold based on mode:\n // - Export (strictMode=true): 99% - ensures complete audio, critical for final output\n // - Preview (strictMode=false): 95% - balances performance and quality, tolerates chunk misalignment\n const threshold = strictMode ? 0.99 : 0.95;\n return coveredDurationUs >= requestedDurationUs * threshold;\n }\n\n /**\n * Get audio range coverage information for incremental decoding\n * @param clipId - Clip identifier\n * @param startUs - Range start time in microseconds\n * @param endUs - Range end time in microseconds\n * @param threshold - Coverage threshold (0-1), default 0.95\n * @returns Coverage info { covered: boolean, coverageRatio: number }\n */\n getAudioRangeCoverage(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs,\n threshold = 0.95\n ): { covered: boolean; coverageRatio: number } {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return { covered: false, coverageRatio: 0 };\n }\n\n // Use binary search to find overlapping slots\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return { covered: false, coverageRatio: 0 };\n }\n\n // Calculate covered duration within [startUs, endUs]\n let coveredDurationUs = 0;\n const requestedDurationUs = endUs - startUs;\n\n for (const slot of overlappingSlots) {\n const slotEndUs = slot.timestampUs + slot.durationUs;\n\n // Calculate overlap with requested range\n const overlapStart = Math.max(startUs, slot.timestampUs);\n const overlapEnd = Math.min(endUs, slotEndUs);\n\n if (overlapStart < overlapEnd) {\n coveredDurationUs += overlapEnd - overlapStart;\n }\n }\n\n const coverageRatio = requestedDurationUs > 0 ? coveredDurationUs / requestedDurationUs : 0;\n return {\n covered: coverageRatio >= threshold,\n coverageRatio,\n };\n }\n\n clearClipPCM(clipId: string): void {\n this.audioDataByClip.delete(clipId);\n }\n\n /**\n * Update window center (unified global window)\n * Aligned with VideoL1Cache strategy: maintains a window of ±RADIUS around center\n */\n setWindow(centerGlobalUs: TimeUs): void {\n this.windowCenter = centerGlobalUs;\n this.checkEviction();\n }\n\n private checkEviction(): void {\n const now = Date.now();\n if (now - this.lastEvictTime > this.EVICT_THROTTLE_MS) {\n this.evictOutOfWindow();\n this.lastEvictTime = now;\n }\n }\n\n /**\n * Evict audio slots outside the global window (aligned with VideoL1Cache)\n * Skip if eviction is disabled (e.g., during export)\n */\n private evictOutOfWindow(): void {\n const windowStart = Math.max(0, this.windowCenter - this.WINDOW_RADIUS);\n const windowEnd = this.windowCenter + this.WINDOW_RADIUS;\n\n for (const [clipId, slots] of this.audioDataByClip) {\n const toKeep: AudioDataSlot[] = [];\n\n for (const slot of slots) {\n const globalTime = slot.globalTimeUs;\n\n // Slots without globalTimeUs are kept (legacy)\n if (globalTime === undefined) {\n toKeep.push(slot);\n continue;\n }\n\n // Keep slots within window\n if (globalTime >= windowStart && globalTime <= windowEnd) {\n toKeep.push(slot);\n }\n // Slots outside window are discarded (no close needed for Float32Array)\n }\n\n if (toKeep.length > 0) {\n this.audioDataByClip.set(clipId, toKeep);\n } else {\n this.audioDataByClip.delete(clipId);\n }\n }\n }\n\n /**\n * Find insertion index for a new slot (aligned with VideoL1Cache)\n */\n private findInsertIndex(slots: AudioDataSlot[], timestamp: TimeUs): number {\n return binarySearchFirst(slots, (slot) => slot.timestampUs >= timestamp);\n }\n\n flush(): void {\n this.audioDataByClip.clear();\n }\n\n clear(): void {\n this.flush();\n this.audioDataByClip.clear();\n this.windowCenter = 0;\n }\n\n dispose(): void {\n this.clear();\n }\n}\n"],"names":[],"mappings":";;AAeO,MAAM,aAAa;AAAA;AAAA,EAEhB,sCAAsB,IAAA;AAAA;AAAA;AAAA,EAItB,eAA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F;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,mBAAyB;AAC/B,UAAM,cAAc,KAAK,IAAI,GAAG,KAAK,eAAe,KAAK,aAAa;AACtE,UAAM,YAAY,KAAK,eAAe,KAAK;AAE3C,eAAW,CAAC,QAAQ,KAAK,KAAK,KAAK,iBAAiB;AAClD,YAAM,SAA0B,CAAA;AAEhC,iBAAW,QAAQ,OAAO;AACxB,cAAM,aAAa,KAAK;AAGxB,YAAI,eAAe,QAAW;AAC5B,iBAAO,KAAK,IAAI;AAChB;AAAA,QACF;AAGA,YAAI,cAAc,eAAe,cAAc,WAAW;AACxD,iBAAO,KAAK,IAAI;AAAA,QAClB;AAAA,MAEF;AAEA,UAAI,OAAO,SAAS,GAAG;AACrB,aAAK,gBAAgB,IAAI,QAAQ,MAAM;AAAA,MACzC,OAAO;AACL,aAAK,gBAAgB,OAAO,MAAM;AAAA,MACpC;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,gBAAgB,OAAwB,WAA2B;AACzE,WAAO,kBAAkB,OAAO,CAAC,SAAS,KAAK,eAAe,SAAS;AAAA,EACzE;AAAA,EAEA,QAAc;AACZ,SAAK,gBAAgB,MAAA;AAAA,EACvB;AAAA,EAEA,QAAc;AACZ,SAAK,MAAA;AACL,SAAK,gBAAgB,MAAA;AACrB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,UAAgB;AACd,SAAK,MAAA;AAAA,EACP;AACF;"}
1
+ {"version":3,"file":"AudioL1Cache.js","sources":["../../../src/cache/l1/AudioL1Cache.ts"],"sourcesContent":["import type { TimeUs } from '../../model/types';\nimport { binarySearchFirst, binarySearchOverlapping } from '../../utils/binary-search';\nimport { extractPlanesFromAudioData } from '../../utils/audio-data';\n\nexport const PREVIEW_PCM_COVERAGE_THRESHOLD = 0.95;\nexport const EXPORT_PCM_COVERAGE_THRESHOLD = 0.99;\n\ninterface AudioDataSlot {\n timestampUs: TimeUs; // Resource timestamp (aligned with VideoL1Cache)\n durationUs: TimeUs;\n planes: Float32Array[]; // PCM data for this slot\n sampleRate: number;\n numberOfChannels: number;\n globalTimeUs?: TimeUs; // Global timeline time (for window management)\n}\n\nexport type { AudioDataSlot };\n\nexport class AudioL1Cache {\n // Aligned with VideoL1Cache: array of discrete audio data slots per clip\n private audioDataByClip = new Map<string, AudioDataSlot[]>();\n\n // Unified window management (aligned with VideoL1Cache)\n // All clips share the same global window center\n private windowCenter: TimeUs = 0;\n\n // Window radius aligned with video (±3.5s, but we use 5s for audio safety margin)\n private readonly WINDOW_RADIUS = 5_000_000; // ±5s\n private readonly EVICT_THROTTLE_MS = 500;\n private lastEvictTime = 0;\n\n putClipAudioData(\n clipId: string,\n audioData: AudioData,\n _clipDurationUs: TimeUs,\n globalTimeUs?: TimeUs\n ): void {\n const numberOfChannels = audioData.numberOfChannels ?? 2;\n const numberOfFrames = audioData.numberOfFrames ?? 0;\n const sampleRate = audioData.sampleRate ?? 48_000;\n const audioTimestampUs = audioData.timestamp ?? 0;\n const audioDurationUs =\n audioData.duration ?? Math.round((numberOfFrames / sampleRate) * 1_000_000);\n\n if (!numberOfChannels || !numberOfFrames) {\n audioData.close();\n return;\n }\n\n // Extract PCM data\n const planes = extractPlanesFromAudioData(audioData, numberOfChannels, numberOfFrames);\n audioData.close();\n\n // Create audio data slot (aligned with video architecture)\n const slot: AudioDataSlot = {\n timestampUs: audioTimestampUs,\n durationUs: audioDurationUs,\n planes,\n sampleRate,\n numberOfChannels,\n globalTimeUs,\n };\n // Get or create slots array for this clip\n let slots = this.audioDataByClip.get(clipId);\n if (!slots) {\n slots = [];\n this.audioDataByClip.set(clipId, slots);\n }\n\n // Insert slot in sorted order (aligned with VideoL1Cache.addFrame)\n const insertIndex = this.findInsertIndex(slots, audioTimestampUs);\n\n // Deduplication: Check for duplicate or near-duplicate timestamp\n // Tolerance of 1ms to handle floating-point precision and concurrent decode attempts\n const DUPLICATE_THRESHOLD_US = 0.5 * audioDurationUs;\n\n if (insertIndex < slots.length) {\n const existingSlot = slots[insertIndex]!;\n const timeDiff = Math.abs(existingSlot.timestampUs - audioTimestampUs);\n\n if (timeDiff < DUPLICATE_THRESHOLD_US) {\n // Near-duplicate detected, skip insertion\n return;\n }\n }\n\n // Also check previous slot to catch overlapping duplicates\n if (insertIndex > 0) {\n const prevSlot = slots[insertIndex - 1]!;\n const timeDiff = Math.abs(prevSlot.timestampUs - audioTimestampUs);\n\n if (timeDiff < DUPLICATE_THRESHOLD_US) {\n // Near-duplicate detected, skip insertion\n return;\n }\n }\n\n // Insert new slot\n slots.splice(insertIndex, 0, slot);\n }\n\n getSlotsInWindow(clipId: string, startUs: TimeUs, endUs: TimeUs): AudioDataSlot[] | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n // Use binary search to find overlapping slots\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return null;\n }\n\n return overlappingSlots;\n }\n\n getPCM(clipId: string, startUs: TimeUs, endUs: TimeUs): Float32Array[] | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n // Use binary search to find overlapping slots (O(log n + k) vs O(n))\n // Aligned with video GOP/frame search\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return null;\n }\n\n const requestedDurationUs = endUs - startUs;\n\n // Validate sample rate consistency across all slots\n const firstSlot = overlappingSlots[0]!;\n const uniformSampleRate = firstSlot.sampleRate;\n const uniformChannels = firstSlot.numberOfChannels;\n\n // Check if all slots have the same sample rate and channel count\n const hasUniformRate = overlappingSlots.every(\n (s) => s.sampleRate === uniformSampleRate && s.numberOfChannels === uniformChannels\n );\n\n if (!hasUniformRate) {\n console.error(\n `[AudioL1Cache] Inconsistent sample rates detected for clip ${clipId}:`,\n overlappingSlots.map((s) => ({\n timestamp: s.timestampUs,\n sampleRate: s.sampleRate,\n channels: s.numberOfChannels,\n }))\n );\n // Return null to avoid corrupted audio data\n // This will trigger re-decode with correct sample rate\n return null;\n }\n\n // Calculate total frame count needed.\n // Note: We intentionally keep ceil here to avoid truncating the requested range.\n const totalFrames = Math.ceil((requestedDurationUs / 1_000_000) * uniformSampleRate);\n\n // Initialize result arrays\n const result: Float32Array[] = Array.from(\n { length: uniformChannels },\n () => new Float32Array(totalFrames)\n );\n\n // Copy data from each overlapping slot.\n // IMPORTANT: Use frame-aligned mapping based on slot plane length instead of time-based floor/ceil.\n // This avoids off-by-one discontinuities at MP3/AAC frame boundaries caused by microsecond rounding.\n for (const slot of overlappingSlots) {\n const slotFrames = slot.planes[0]?.length ?? 0;\n if (slotFrames <= 0) continue;\n\n // Map slot start time to destination frame index (frame-aligned).\n const slotStartFrame = Math.round(\n ((slot.timestampUs - startUs) / 1_000_000) * uniformSampleRate\n );\n const slotEndFrame = slotStartFrame + slotFrames;\n\n // Intersect with requested destination buffer [0, totalFrames)\n const dstStartFrame = Math.max(0, slotStartFrame);\n const dstEndFrame = Math.min(totalFrames, slotEndFrame);\n const copyFrameCount = dstEndFrame - dstStartFrame;\n if (copyFrameCount <= 0) continue;\n\n // Source offset is derived from the chosen destination start.\n const srcOffsetFrames = dstStartFrame - slotStartFrame;\n\n for (let ch = 0; ch < uniformChannels; ch++) {\n const srcPlane = slot.planes[ch];\n const dstPlane = result[ch];\n if (!srcPlane || !dstPlane) continue;\n\n const actualCopyFrames = Math.min(\n copyFrameCount,\n srcPlane.length - srcOffsetFrames,\n dstPlane.length - dstStartFrame\n );\n if (actualCopyFrames <= 0) continue;\n\n dstPlane.set(\n srcPlane.subarray(srcOffsetFrames, srcOffsetFrames + actualCopyFrames),\n dstStartFrame\n );\n }\n }\n\n return result;\n }\n\n getPCMWithMetadata(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs\n ): { planes: Float32Array[]; sampleRate: number; numberOfChannels: number } | null {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return null;\n }\n\n const planes = this.getPCM(clipId, startUs, endUs);\n if (!planes) {\n return null;\n }\n\n // Use first slot's metadata\n const firstSlot = slots[0]!;\n return {\n planes,\n sampleRate: firstSlot.sampleRate,\n numberOfChannels: firstSlot.numberOfChannels,\n };\n }\n\n hasClipPCM(clipId: string): boolean {\n return this.audioDataByClip.has(clipId);\n }\n\n /**\n * Check if a slot with specific timestamp exists (used for incremental decoding)\n */\n hasSlotAt(clipId: string, timestampUs: TimeUs, toleranceUs: number = 1000): boolean {\n const slots = this.audioDataByClip.get(clipId);\n if (!slots) return false;\n\n // Since slots are sorted, we can use binary search\n const index = this.findInsertIndex(slots, timestampUs);\n\n // Check index and index-1 (in case findInsertIndex lands after)\n if (index < slots.length) {\n const slot = slots[index];\n if (slot && Math.abs(slot.timestampUs - timestampUs) < toleranceUs) return true;\n }\n if (index > 0) {\n const slot = slots[index - 1];\n if (slot && Math.abs(slot.timestampUs - timestampUs) < toleranceUs) return true;\n }\n return false;\n }\n\n /**\n * Get audio range coverage information for incremental decoding\n * @param clipId - Clip identifier\n * @param startUs - Range start time in microseconds\n * @param endUs - Range end time in microseconds\n * @param threshold - Coverage threshold (0-1), default 0.95\n * @returns Coverage info { covered: boolean, coverageRatio: number }\n */\n getAudioRangeCoverage(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs,\n threshold = PREVIEW_PCM_COVERAGE_THRESHOLD\n ): { covered: boolean; coverageRatio: number } {\n const { coveredDurationUs, requestedDurationUs } = this.computeRangeCoverageDuration(\n clipId,\n startUs,\n endUs\n );\n const coverageRatio = requestedDurationUs > 0 ? coveredDurationUs / requestedDurationUs : 0;\n return {\n covered: coverageRatio >= threshold,\n coverageRatio,\n };\n }\n\n private computeRangeCoverageDuration(\n clipId: string,\n startUs: TimeUs,\n endUs: TimeUs\n ): { coveredDurationUs: TimeUs; requestedDurationUs: TimeUs } {\n const requestedDurationUs = Math.max(0, endUs - startUs);\n if (requestedDurationUs === 0) {\n return { coveredDurationUs: 0, requestedDurationUs: 0 };\n }\n\n const slots = this.audioDataByClip.get(clipId);\n if (!slots || slots.length === 0) {\n return { coveredDurationUs: 0, requestedDurationUs };\n }\n\n // Use binary search to find overlapping slots (O(log n + k))\n const overlappingSlots = binarySearchOverlapping(slots, startUs, endUs, (slot) => ({\n start: slot.timestampUs,\n end: slot.timestampUs + slot.durationUs,\n }));\n\n if (overlappingSlots.length === 0) {\n return { coveredDurationUs: 0, requestedDurationUs };\n }\n\n let coveredDurationUs = 0;\n for (const slot of overlappingSlots) {\n const slotEndUs = slot.timestampUs + slot.durationUs;\n const overlapStart = Math.max(startUs, slot.timestampUs);\n const overlapEnd = Math.min(endUs, slotEndUs);\n if (overlapStart < overlapEnd) {\n coveredDurationUs += overlapEnd - overlapStart;\n }\n }\n\n return { coveredDurationUs, requestedDurationUs };\n }\n\n clearClipPCM(clipId: string): void {\n this.audioDataByClip.delete(clipId);\n }\n\n /**\n * Update window center (unified global window)\n * Aligned with VideoL1Cache strategy: maintains a window of ±RADIUS around center\n */\n setWindow(centerGlobalUs: TimeUs): void {\n this.windowCenter = centerGlobalUs;\n this.checkEviction();\n }\n\n private checkEviction(): void {\n const now = Date.now();\n if (now - this.lastEvictTime > this.EVICT_THROTTLE_MS) {\n this.evictOutOfWindow();\n this.lastEvictTime = now;\n }\n }\n\n /**\n * Evict audio slots outside the global window (aligned with VideoL1Cache)\n * Skip if eviction is disabled (e.g., during export)\n */\n private evictOutOfWindow(): void {\n const windowStart = Math.max(0, this.windowCenter - this.WINDOW_RADIUS);\n const windowEnd = this.windowCenter + this.WINDOW_RADIUS;\n\n for (const [clipId, slots] of this.audioDataByClip) {\n const toKeep: AudioDataSlot[] = [];\n\n for (const slot of slots) {\n const globalTime = slot.globalTimeUs;\n\n // Slots without globalTimeUs are kept (legacy)\n if (globalTime === undefined) {\n toKeep.push(slot);\n continue;\n }\n\n // Keep slots within window\n if (globalTime >= windowStart && globalTime <= windowEnd) {\n toKeep.push(slot);\n }\n // Slots outside window are discarded (no close needed for Float32Array)\n }\n\n if (toKeep.length > 0) {\n this.audioDataByClip.set(clipId, toKeep);\n } else {\n this.audioDataByClip.delete(clipId);\n }\n }\n }\n\n /**\n * Find insertion index for a new slot (aligned with VideoL1Cache)\n */\n private findInsertIndex(slots: AudioDataSlot[], timestamp: TimeUs): number {\n return binarySearchFirst(slots, (slot) => slot.timestampUs >= timestamp);\n }\n\n flush(): void {\n this.audioDataByClip.clear();\n }\n\n clear(): void {\n this.flush();\n this.audioDataByClip.clear();\n this.windowCenter = 0;\n }\n\n dispose(): void {\n this.clear();\n }\n}\n"],"names":[],"mappings":";;AAIO,MAAM,iCAAiC;AACvC,MAAM,gCAAgC;AAatC,MAAM,aAAa;AAAA;AAAA,EAEhB,sCAAsB,IAAA;AAAA;AAAA;AAAA,EAItB,eAAuB;AAAA;AAAA,EAGd,gBAAgB;AAAA;AAAA,EAChB,oBAAoB;AAAA,EAC7B,gBAAgB;AAAA,EAExB,iBACE,QACA,WACA,iBACA,cACM;AACN,UAAM,mBAAmB,UAAU,oBAAoB;AACvD,UAAM,iBAAiB,UAAU,kBAAkB;AACnD,UAAM,aAAa,UAAU,cAAc;AAC3C,UAAM,mBAAmB,UAAU,aAAa;AAChD,UAAM,kBACJ,UAAU,YAAY,KAAK,MAAO,iBAAiB,aAAc,GAAS;AAE5E,QAAI,CAAC,oBAAoB,CAAC,gBAAgB;AACxC,gBAAU,MAAA;AACV;AAAA,IACF;AAGA,UAAM,SAAS,2BAA2B,WAAW,kBAAkB,cAAc;AACrF,cAAU,MAAA;AAGV,UAAM,OAAsB;AAAA,MAC1B,aAAa;AAAA,MACb,YAAY;AAAA,MACZ;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAGF,QAAI,QAAQ,KAAK,gBAAgB,IAAI,MAAM;AAC3C,QAAI,CAAC,OA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MAAM,KAAK,IAAA;AACjB,QAAI,MAAM,KAAK,gBAAgB,KAAK,mBAAmB;AACrD,WAAK,iBAAA;AACL,WAAK,gBAAgB;AAAA,IACvB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,mBAAyB;AAC/B,UAAM,cAAc,KAAK,IAAI,GAAG,KAAK,eAAe,KAAK,aAAa;AACtE,UAAM,YAAY,KAAK,eAAe,KAAK;AAE3C,eAAW,CAAC,QAAQ,KAAK,KAAK,KAAK,iBAAiB;AAClD,YAAM,SAA0B,CAAA;AAEhC,iBAAW,QAAQ,OAAO;AACxB,cAAM,aAAa,KAAK;AAGxB,YAAI,eAAe,QAAW;AAC5B,iBAAO,KAAK,IAAI;AAChB;AAAA,QACF;AAGA,YAAI,cAAc,eAAe,cAAc,WAAW;AACxD,iBAAO,KAAK,IAAI;AAAA,QAClB;AAAA,MAEF;AAEA,UAAI,OAAO,SAAS,GAAG;AACrB,aAAK,gBAAgB,IAAI,QAAQ,MAAM;AAAA,MACzC,OAAO;AACL,aAAK,gBAAgB,OAAO,MAAM;AAAA,MACpC;AAAA,IACF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,gBAAgB,OAAwB,WAA2B;AACzE,WAAO,kBAAkB,OAAO,CAAC,SAAS,KAAK,eAAe,SAAS;AAAA,EACzE;AAAA,EAEA,QAAc;AACZ,SAAK,gBAAgB,MAAA;AAAA,EACvB;AAAA,EAEA,QAAc;AACZ,SAAK,MAAA;AACL,SAAK,gBAAgB,MAAA;AACrB,SAAK,eAAe;AAAA,EACtB;AAAA,EAEA,UAAgB;AACd,SAAK,MAAA;AAAA,EACP;AACF;"}
@@ -26,6 +26,7 @@ export declare class PlaybackController implements IPlaybackController, PreviewH
26
26
  private fsm;
27
27
  private windowEnd;
28
28
  private readonly WINDOW_DURATION;
29
+ private readonly AUDIO_READY_PROBE_WINDOW;
29
30
  private readonly PREHEAT_DISTANCE;
30
31
  private preheatInProgress;
31
32
  constructor(orchestrator: Orchestrator, eventBus: IEventBus, options: PlaybackOptions);
@@ -63,5 +64,6 @@ export declare class PlaybackController implements IPlaybackController, PreviewH
63
64
  private onCacheCover;
64
65
  private onModelSet;
65
66
  private setupEventListeners;
67
+ private isVideoResourceReadyAtTime;
66
68
  }
67
69
  //# sourceMappingURL=PlaybackController.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"PlaybackController.d.ts","sourceRoot":"","sources":["../../src/controllers/PlaybackController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,mBAAmB,EACnB,eAAe,EACf,SAAS,EACT,aAAa,EACb,MAAM,EACP,MAAM,SAAS,CAAC;AAWjB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAOpD;;;GAGG;AACH,qBAAa,kBAAmB,YAAW,mBAAmB,EAAE,aAAa;IAC3E,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,MAAM,CAAsC;IACpD,OAAO,CAAC,aAAa,CAA8B;IAGnD,aAAa,EAAE,MAAM,CAAK;IAC1B,OAAO,CAAC,YAAY,CAAO;IAC3B,OAAO,CAAC,MAAM,CAAO;IACrB,OAAO,CAAC,IAAI,CAAS;IAGrB,OAAO,CAAC,KAAK,CAAuB;IACpC,OAAO,CAAC,WAAW,CAAa;IAGhC,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,aAAa,CAAK;IAC1B,OAAO,CAAC,GAAG,CAAK;IAGhB,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,qBAAqB,CAAK;IAClC,OAAO,CAAC,QAAQ,CAAC,uBAAuB,CAAW;IAGnD,OAAO,CAAC,GAAG,CAA8B;IAGzC,OAAO,CAAC,SAAS,CAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAa;IAC7C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAa;IAC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A0B;CAUnC"}
@@ -34,6 +34,8 @@ class PlaybackController {
34
34
  windowEnd = 0;
35
35
  WINDOW_DURATION = 3e6;
36
36
  // 3s decode window
37
+ AUDIO_READY_PROBE_WINDOW = 5e5;
38
+ // 500ms: gate buffering only for near-future audio
37
39
  PREHEAT_DISTANCE = 1e6;
38
40
  // 1s preheat trigger distance
39
41
  preheatInProgress = false;
@@ -68,7 +70,7 @@ class PlaybackController {
68
70
  }
69
71
  }
70
72
  async renderCover() {
71
- await this.renderCurrentFrame(0, { immediate: false });
73
+ await this.renderCurrentFrame(0, { mode: "blocking" });
72
74
  }
73
75
  // ========= Public API =========
74
76
  play() {
@@ -158,7 +160,7 @@ class PlaybackController {
158
160
  if (maybe) promises.push(maybe);
159
161
  }
160
162
  if (promises.length === 0) return;
161
- return Promise.all(promises).then(() => void 0);
163
+ return Promise.all(promises);
162
164
  }
163
165
  executeCommand(command, token) {
164
166
  if (!this.isCurrentToken(token)) return;
@@ -173,9 +175,23 @@ class PlaybackController {
173
175
  if (command.ignoreWaiterReplacedError && error instanceof WaiterReplacedError) return;
174
176
  if (command.logPrefix) console.error(command.logPrefix, error);
175
177
  const onErrorDone = command.onError ? this.dispatch(command.onError).done : void 0;
178
+ const normalizeError = (e) => {
179
+ if (e instanceof Error) return e;
180
+ return new Error(typeof e === "string" ? e : JSON.stringify(e));
181
+ };
176
182
  const emit = () => {
177
183
  if (command.emitPlaybackError) {
178
- this.eventBus.emit(MeframeEvent.PlaybackError, error);
184
+ const err = normalizeError(error);
185
+ this.eventBus.emit(MeframeEvent.PlaybackError, err);
186
+ this.eventBus.emit(MeframeEvent.Error, {
187
+ source: "playback",
188
+ error: err,
189
+ context: {
190
+ command: command.logPrefix,
191
+ onError: command.onError?.type
192
+ },
193
+ recoverable: false
194
+ });
179
195
  }
180
196
  };
181
197
  if (onErrorDone) {
@@ -256,7 +272,7 @@ class PlaybackController {
256
272
  }
257
273
  case PlaybackCommandType.RenderFrame: {
258
274
  return this.renderCurrentFrame(command.timeUs, {
259
- immediate: command.immediate,
275
+ mode: command.mode,
260
276
  relativeTimeUs: command.relativeTimeUs
261
277
  });
262
278
  }
@@ -265,7 +281,7 @@ class PlaybackController {
265
281
  if (!this.isCurrentToken(token)) return;
266
282
  if (keyframeTimeUs === null) return;
267
283
  return this.orchestrator.getRenderState(command.timeUs, {
268
- immediate: true,
284
+ mode: "probe",
269
285
  relativeTimeUs: keyframeTimeUs
270
286
  }).then((keyframeRenderState) => {
271
287
  if (!this.isCurrentToken(token)) return;
@@ -276,18 +292,43 @@ class PlaybackController {
276
292
  }
277
293
  case PlaybackCommandType.EnsureAudio: {
278
294
  return this.audioSession.ensureAudioForTime(command.timeUs, {
279
- immediate: command.immediate
295
+ mode: command.mode
280
296
  });
281
297
  }
282
298
  case PlaybackCommandType.GetFrame: {
283
299
  return this.orchestrator.getFrame(command.timeUs, {
284
- immediate: command.immediate,
300
+ mode: command.mode,
285
301
  preheat: command.preheat
286
- }).then(() => void 0);
302
+ });
287
303
  }
288
304
  case PlaybackCommandType.StartAudioPlayback: {
289
305
  return this.audioSession.startPlayback(command.timeUs, this.audioContext);
290
306
  }
307
+ case PlaybackCommandType.ProbeStartReady: {
308
+ const audioWindowEnd = Math.min(
309
+ this.duration,
310
+ command.timeUs + this.AUDIO_READY_PROBE_WINDOW
311
+ );
312
+ const audioReady = this.audioSession.isAudioResourceWindowReady(
313
+ command.timeUs,
314
+ audioWindowEnd
315
+ );
316
+ const videoReady = this.isVideoResourceReadyAtTime(command.timeUs);
317
+ if (audioReady && videoReady) return;
318
+ if (!audioReady) {
319
+ void this.audioSession.ensureAudioForTime(command.timeUs, { mode: "probe" });
320
+ }
321
+ if (!videoReady) {
322
+ void this.orchestrator.getFrame(command.timeUs, { mode: "probe" });
323
+ }
324
+ this.dispatch({
325
+ type: PlaybackActionType.EnterBuffering,
326
+ timeUs: command.timeUs,
327
+ bumpToken: true,
328
+ reason: "startup"
329
+ });
330
+ return;
331
+ }
291
332
  case PlaybackCommandType.StartRafLoop: {
292
333
  this.startPlaybackLoop(token);
293
334
  return;
@@ -323,13 +364,22 @@ class PlaybackController {
323
364
  if (!this.isCurrentToken(token) || this.fsm.snapshot.state !== PlaybackState.Playing) {
324
365
  return;
325
366
  }
367
+ const audioWindowEnd = Math.min(
368
+ this.duration,
369
+ this.currentTimeUs + this.AUDIO_READY_PROBE_WINDOW
370
+ );
371
+ if (!this.audioSession.isAudioResourceWindowReady(this.currentTimeUs, audioWindowEnd)) {
372
+ void this.audioSession.ensureAudioForTime(this.currentTimeUs, { mode: "probe" });
373
+ this.dispatch({ type: PlaybackActionType.EnterBuffering, timeUs: this.currentTimeUs });
374
+ return;
375
+ }
326
376
  if (this.currentTimeUs - this.lastAudioScheduleTime >= this.AUDIO_SCHEDULE_INTERVAL) {
327
377
  await this.audioSession.scheduleAudio(this.currentTimeUs, this.audioContext);
328
378
  if (!this.isCurrentToken(token) || this.fsm.snapshot.state !== PlaybackState.Playing) return;
329
379
  this.lastAudioScheduleTime = this.currentTimeUs;
330
380
  }
331
381
  const renderState = await this.orchestrator.getRenderState(this.currentTimeUs, {
332
- immediate: true
382
+ mode: "probe"
333
383
  });
334
384
  if (!this.isCurrentToken(token) || this.fsm.snapshot.state !== PlaybackState.Playing) {
335
385
  return;
@@ -391,7 +441,7 @@ class PlaybackController {
391
441
  this.orchestrator.preheatClipWindow(clip.id, clipWindowStart, clipWindowEnd, windowStart)
392
442
  );
393
443
  }
394
- preheatPromises.push(this.audioSession.ensureAudioForTime(windowStart, { immediate: false }));
444
+ preheatPromises.push(this.audioSession.ensureAudioForTime(windowStart, { mode: "blocking" }));
395
445
  await Promise.all(preheatPromises);
396
446
  this.windowEnd = windowEnd;
397
447
  } catch (error) {
@@ -406,7 +456,7 @@ class PlaybackController {
406
456
  return;
407
457
  }
408
458
  const renderState = await this.orchestrator.getRenderState(timeUs, {
409
- immediate: options.immediate,
459
+ mode: options.mode,
410
460
  relativeTimeUs: options.relativeTimeUs
411
461
  });
412
462
  if (!renderState) {
@@ -440,11 +490,12 @@ class PlaybackController {
440
490
  }
441
491
  onCacheCover = () => {
442
492
  if (this.fsm.snapshot.state === PlaybackState.Idle && this.currentTimeUs === 0) {
443
- void this.renderCurrentFrame(0, { immediate: false });
493
+ void this.renderCurrentFrame(0, { mode: "blocking" });
444
494
  }
445
495
  };
446
496
  onModelSet = () => {
447
497
  if (!this.videoComposer || !this.orchestrator.compositionModel) return;
498
+ this.dispatch({ type: PlaybackActionType.ModelSet });
448
499
  const model = this.orchestrator.compositionModel;
449
500
  this.videoComposer.updateConfig({
450
501
  width: model.renderConfig?.width || 720,
@@ -452,13 +503,23 @@ class PlaybackController {
452
503
  fps: model.fps || 30,
453
504
  backgroundColor: model.renderConfig?.backgroundColor || "#000"
454
505
  });
455
- void this.audioSession.ensureAudioForTime(this.currentTimeUs, { immediate: false });
456
- void this.renderCurrentFrame(this.currentTimeUs, { immediate: false });
506
+ void this.audioSession.ensureAudioForTime(this.currentTimeUs, { mode: "blocking" });
507
+ void this.renderCurrentFrame(this.currentTimeUs, { mode: "blocking" });
457
508
  };
458
509
  setupEventListeners() {
459
510
  this.eventBus.on(MeframeEvent.CacheCover, this.onCacheCover);
460
511
  this.eventBus.on(MeframeEvent.ModelSet, this.onModelSet);
461
512
  }
513
+ isVideoResourceReadyAtTime(timeUs) {
514
+ const model = this.orchestrator.compositionModel;
515
+ if (!model) return true;
516
+ const clip = model.getClipsAtTime(timeUs, model.mainTrackId)[0];
517
+ if (!clip || !("resourceId" in clip) || typeof clip.resourceId !== "string")
518
+ return true;
519
+ const resourceId = clip.resourceId;
520
+ const resource = model.getResource(resourceId);
521
+ return resource?.state === "ready";
522
+ }
462
523
  }
463
524
  export {
464
525
  PlaybackController