@meframe/core 0.0.1 → 0.0.3

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 (159) hide show
  1. package/README.md +17 -4
  2. package/dist/Meframe.d.ts.map +1 -1
  3. package/dist/Meframe.js +2 -4
  4. package/dist/Meframe.js.map +1 -1
  5. package/dist/cache/CacheManager.d.ts.map +1 -1
  6. package/dist/cache/CacheManager.js +8 -1
  7. package/dist/cache/CacheManager.js.map +1 -1
  8. package/dist/config/defaults.d.ts.map +1 -1
  9. package/dist/config/defaults.js +2 -9
  10. package/dist/config/defaults.js.map +1 -1
  11. package/dist/config/types.d.ts +3 -4
  12. package/dist/config/types.d.ts.map +1 -1
  13. package/dist/controllers/PlaybackController.d.ts +4 -2
  14. package/dist/controllers/PlaybackController.d.ts.map +1 -1
  15. package/dist/controllers/PlaybackController.js +7 -13
  16. package/dist/controllers/PlaybackController.js.map +1 -1
  17. package/dist/controllers/PreRenderService.d.ts +3 -2
  18. package/dist/controllers/PreRenderService.d.ts.map +1 -1
  19. package/dist/controllers/PreRenderService.js.map +1 -1
  20. package/dist/controllers/PreviewHandle.d.ts +2 -0
  21. package/dist/controllers/PreviewHandle.d.ts.map +1 -1
  22. package/dist/controllers/PreviewHandle.js +6 -0
  23. package/dist/controllers/PreviewHandle.js.map +1 -1
  24. package/dist/controllers/index.d.ts +1 -1
  25. package/dist/controllers/index.d.ts.map +1 -1
  26. package/dist/controllers/types.d.ts +2 -12
  27. package/dist/controllers/types.d.ts.map +1 -1
  28. package/dist/event/events.d.ts +5 -59
  29. package/dist/event/events.d.ts.map +1 -1
  30. package/dist/event/events.js +1 -6
  31. package/dist/event/events.js.map +1 -1
  32. package/dist/model/CompositionModel.js +1 -2
  33. package/dist/model/CompositionModel.js.map +1 -1
  34. package/dist/orchestrator/CompositionPlanner.d.ts.map +1 -1
  35. package/dist/orchestrator/CompositionPlanner.js +1 -0
  36. package/dist/orchestrator/CompositionPlanner.js.map +1 -1
  37. package/dist/orchestrator/Orchestrator.d.ts.map +1 -1
  38. package/dist/orchestrator/Orchestrator.js +3 -13
  39. package/dist/orchestrator/Orchestrator.js.map +1 -1
  40. package/dist/orchestrator/VideoClipSession.d.ts.map +1 -1
  41. package/dist/orchestrator/VideoClipSession.js +4 -5
  42. package/dist/orchestrator/VideoClipSession.js.map +1 -1
  43. package/dist/orchestrator/types.d.ts +1 -1
  44. package/dist/orchestrator/types.d.ts.map +1 -1
  45. package/dist/stages/compose/GlobalAudioSession.d.ts.map +1 -1
  46. package/dist/stages/compose/GlobalAudioSession.js +3 -2
  47. package/dist/stages/compose/GlobalAudioSession.js.map +1 -1
  48. package/dist/stages/compose/VideoComposer.d.ts.map +1 -1
  49. package/dist/stages/compose/types.d.ts +3 -1
  50. package/dist/stages/compose/types.d.ts.map +1 -1
  51. package/dist/stages/decode/AudioChunkDecoder.d.ts.map +1 -1
  52. package/dist/stages/decode/VideoChunkDecoder.d.ts +0 -1
  53. package/dist/stages/decode/VideoChunkDecoder.d.ts.map +1 -1
  54. package/dist/stages/demux/MP4Demuxer.d.ts +2 -1
  55. package/dist/stages/demux/MP4Demuxer.d.ts.map +1 -1
  56. package/dist/stages/load/EventHandlers.d.ts +2 -11
  57. package/dist/stages/load/EventHandlers.d.ts.map +1 -1
  58. package/dist/stages/load/EventHandlers.js +1 -24
  59. package/dist/stages/load/EventHandlers.js.map +1 -1
  60. package/dist/stages/load/ResourceLoader.d.ts.map +1 -1
  61. package/dist/stages/load/ResourceLoader.js +11 -13
  62. package/dist/stages/load/ResourceLoader.js.map +1 -1
  63. package/dist/stages/load/TaskManager.d.ts +1 -1
  64. package/dist/stages/load/TaskManager.d.ts.map +1 -1
  65. package/dist/stages/load/TaskManager.js +3 -2
  66. package/dist/stages/load/TaskManager.js.map +1 -1
  67. package/dist/stages/load/types.d.ts +2 -0
  68. package/dist/stages/load/types.d.ts.map +1 -1
  69. package/dist/utils/time-utils.d.ts +3 -2
  70. package/dist/utils/time-utils.d.ts.map +1 -1
  71. package/dist/utils/time-utils.js +2 -1
  72. package/dist/utils/time-utils.js.map +1 -1
  73. package/dist/vite-plugin.d.ts +19 -0
  74. package/dist/vite-plugin.d.ts.map +1 -0
  75. package/dist/vite-plugin.js +145 -0
  76. package/dist/vite-plugin.js.map +1 -0
  77. package/dist/worker/WorkerPool.d.ts +7 -4
  78. package/dist/worker/WorkerPool.d.ts.map +1 -1
  79. package/dist/worker/WorkerPool.js +29 -18
  80. package/dist/worker/WorkerPool.js.map +1 -1
  81. package/dist/{stages/demux → workers}/MP4Demuxer.js +17 -15
  82. package/dist/workers/MP4Demuxer.js.map +1 -0
  83. package/dist/workers/WorkerChannel.js +486 -0
  84. package/dist/workers/WorkerChannel.js.map +1 -0
  85. package/dist/workers/mp4box.all.js +7049 -0
  86. package/dist/workers/mp4box.all.js.map +1 -0
  87. package/dist/workers/stages/compose/audio-compose.worker.js +1063 -0
  88. package/dist/workers/stages/compose/audio-compose.worker.js.map +1 -0
  89. package/dist/workers/stages/compose/video-compose.worker.js +1209 -0
  90. package/dist/workers/stages/compose/video-compose.worker.js.map +1 -0
  91. package/dist/{stages → workers/stages}/decode/decode.worker.js +401 -20
  92. package/dist/workers/stages/decode/decode.worker.js.map +1 -0
  93. package/dist/{stages → workers/stages}/demux/audio-demux.worker.js +184 -4
  94. package/dist/workers/stages/demux/audio-demux.worker.js.map +1 -0
  95. package/dist/{stages → workers/stages}/demux/video-demux.worker.js +7 -30
  96. package/dist/workers/stages/demux/video-demux.worker.js.map +1 -0
  97. package/dist/{stages → workers/stages}/encode/encode.worker.js +238 -5
  98. package/dist/workers/stages/encode/encode.worker.js.map +1 -0
  99. package/dist/{stages/mux/MP4Muxer.js → workers/stages/mux/mux.worker.js} +244 -5
  100. package/dist/workers/stages/mux/mux.worker.js.map +1 -0
  101. package/package.json +27 -21
  102. package/dist/model/types.js +0 -5
  103. package/dist/model/types.js.map +0 -1
  104. package/dist/plugins/BackpressureMonitor.js +0 -62
  105. package/dist/plugins/BackpressureMonitor.js.map +0 -1
  106. package/dist/stages/compose/AudioDucker.js +0 -161
  107. package/dist/stages/compose/AudioDucker.js.map +0 -1
  108. package/dist/stages/compose/AudioMixer.js +0 -373
  109. package/dist/stages/compose/AudioMixer.js.map +0 -1
  110. package/dist/stages/compose/FilterProcessor.js +0 -226
  111. package/dist/stages/compose/FilterProcessor.js.map +0 -1
  112. package/dist/stages/compose/LayerRenderer.js +0 -215
  113. package/dist/stages/compose/LayerRenderer.js.map +0 -1
  114. package/dist/stages/compose/TransitionProcessor.js +0 -189
  115. package/dist/stages/compose/TransitionProcessor.js.map +0 -1
  116. package/dist/stages/compose/VideoComposer.js +0 -186
  117. package/dist/stages/compose/VideoComposer.js.map +0 -1
  118. package/dist/stages/compose/audio-compose.worker.d.ts +0 -79
  119. package/dist/stages/compose/audio-compose.worker.d.ts.map +0 -1
  120. package/dist/stages/compose/audio-compose.worker.js +0 -541
  121. package/dist/stages/compose/audio-compose.worker.js.map +0 -1
  122. package/dist/stages/compose/video-compose.worker.d.ts +0 -60
  123. package/dist/stages/compose/video-compose.worker.d.ts.map +0 -1
  124. package/dist/stages/compose/video-compose.worker.js +0 -369
  125. package/dist/stages/compose/video-compose.worker.js.map +0 -1
  126. package/dist/stages/decode/AudioChunkDecoder.js +0 -83
  127. package/dist/stages/decode/AudioChunkDecoder.js.map +0 -1
  128. package/dist/stages/decode/BaseDecoder.js +0 -130
  129. package/dist/stages/decode/BaseDecoder.js.map +0 -1
  130. package/dist/stages/decode/VideoChunkDecoder.js +0 -209
  131. package/dist/stages/decode/VideoChunkDecoder.js.map +0 -1
  132. package/dist/stages/decode/decode.worker.d.ts +0 -70
  133. package/dist/stages/decode/decode.worker.d.ts.map +0 -1
  134. package/dist/stages/decode/decode.worker.js.map +0 -1
  135. package/dist/stages/demux/MP3FrameParser.js +0 -186
  136. package/dist/stages/demux/MP3FrameParser.js.map +0 -1
  137. package/dist/stages/demux/MP4Demuxer.js.map +0 -1
  138. package/dist/stages/demux/audio-demux.worker.d.ts +0 -51
  139. package/dist/stages/demux/audio-demux.worker.d.ts.map +0 -1
  140. package/dist/stages/demux/audio-demux.worker.js.map +0 -1
  141. package/dist/stages/demux/video-demux.worker.d.ts +0 -48
  142. package/dist/stages/demux/video-demux.worker.d.ts.map +0 -1
  143. package/dist/stages/demux/video-demux.worker.js.map +0 -1
  144. package/dist/stages/encode/AudioChunkEncoder.js +0 -37
  145. package/dist/stages/encode/AudioChunkEncoder.js.map +0 -1
  146. package/dist/stages/encode/BaseEncoder.js +0 -164
  147. package/dist/stages/encode/BaseEncoder.js.map +0 -1
  148. package/dist/stages/encode/VideoChunkEncoder.js +0 -50
  149. package/dist/stages/encode/VideoChunkEncoder.js.map +0 -1
  150. package/dist/stages/encode/encode.worker.d.ts +0 -3
  151. package/dist/stages/encode/encode.worker.d.ts.map +0 -1
  152. package/dist/stages/encode/encode.worker.js.map +0 -1
  153. package/dist/stages/mux/MP4Muxer.js.map +0 -1
  154. package/dist/stages/mux/mux.worker.d.ts +0 -65
  155. package/dist/stages/mux/mux.worker.d.ts.map +0 -1
  156. package/dist/stages/mux/mux.worker.js +0 -219
  157. package/dist/stages/mux/mux.worker.js.map +0 -1
  158. package/dist/stages/mux/utils.js +0 -34
  159. package/dist/stages/mux/utils.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"video-compose.worker.js","sources":["../../../../src/utils/time-utils.ts","../../../../src/stages/compose/LayerRenderer.ts","../../../../src/stages/compose/TransitionProcessor.ts","../../../../src/stages/compose/FilterProcessor.ts","../../../../src/stages/compose/VideoComposer.ts","../../../../src/stages/compose/video-compose.worker.ts"],"sourcesContent":["export type QuantizeStrategy = 'nearest' | 'floor' | 'ceil';\ntype TimeUs = number;\nexport const MICROSECONDS_PER_SECOND = 1_000_000;\n\nconst DEFAULT_FPS = 30;\n\nexport const DEFAULT_FRAME_TOLERANCE_US: TimeUs = 33_333;\n\nexport function normalizeFps(value?: number): number {\n if (!Number.isFinite(value) || (value as number) <= 0) {\n return DEFAULT_FPS;\n }\n return value as number;\n}\n\nexport function frameDurationFromFps(fps?: number): TimeUs {\n const normalized = normalizeFps(fps);\n const duration = MICROSECONDS_PER_SECOND / normalized;\n return Math.max(Math.round(duration), 1);\n}\n\nexport function frameIndexFromTimestamp(\n baseTimestampUs: TimeUs,\n timestampUs: TimeUs,\n fps?: number,\n strategy: QuantizeStrategy = 'nearest'\n): number {\n const frameDurationUs = frameDurationFromFps(fps);\n if (frameDurationUs <= 0) {\n return 0;\n }\n\n const delta = timestampUs - baseTimestampUs;\n const rawIndex = delta / frameDurationUs;\n\n if (!Number.isFinite(rawIndex)) {\n return 0;\n }\n\n switch (strategy) {\n case 'floor':\n return Math.floor(rawIndex);\n case 'ceil':\n return Math.ceil(rawIndex);\n default:\n return Math.round(rawIndex);\n }\n}\n\nexport function quantizeTimestampToFrame(\n timestampUs: TimeUs,\n baseTimestampUs: TimeUs,\n fps?: number,\n strategy: QuantizeStrategy = 'nearest'\n): TimeUs {\n const frameDurationUs = frameDurationFromFps(fps);\n const index = frameIndexFromTimestamp(baseTimestampUs, timestampUs, fps, strategy);\n return baseTimestampUs + index * frameDurationUs;\n}\n\nexport function isTimestampWithinFrame(\n targetTimeUs: TimeUs,\n frameTimestampUs: TimeUs,\n frameDurationUs: TimeUs,\n toleranceUs: TimeUs = DEFAULT_FRAME_TOLERANCE_US\n): boolean {\n if (!Number.isFinite(frameTimestampUs)) {\n return false;\n }\n\n const delta = Math.abs(targetTimeUs - frameTimestampUs);\n if (delta <= toleranceUs) {\n return true;\n }\n\n if (frameDurationUs > 0 && frameTimestampUs <= targetTimeUs) {\n return targetTimeUs < frameTimestampUs + frameDurationUs;\n }\n\n return false;\n}\n","import type { Layer, VideoLayer, ImageLayer, TextLayer, Transform2D, MaskConfig } from './types';\n\n/**\n * LayerRenderer - Handles rendering of individual layers\n * Single responsibility: Draw a single layer to the canvas context\n */\nexport class LayerRenderer {\n private ctx: OffscreenCanvasRenderingContext2D;\n private width: number;\n private height: number;\n\n constructor(ctx: OffscreenCanvasRenderingContext2D, width: number, height: number) {\n this.ctx = ctx;\n this.width = width;\n this.height = height;\n this.ensureHighQualityRendering();\n }\n\n private ensureHighQualityRendering(): void {\n this.ctx.imageSmoothingEnabled = true;\n this.ctx.imageSmoothingQuality = 'high';\n }\n\n /**\n * Render a single layer with all its properties\n */\n async renderLayer(layer: Layer): Promise<void> {\n if (!layer.visible || layer.opacity <= 0) return;\n\n this.ctx.save();\n\n try {\n this.ensureHighQualityRendering();\n\n // Apply layer properties\n this.ctx.globalAlpha = layer.opacity;\n\n if (layer.blendMode) {\n this.ctx.globalCompositeOperation = layer.blendMode;\n }\n\n if (layer.transform) {\n this.applyTransform(layer.transform);\n }\n\n // Render based on layer type\n switch (layer.type) {\n case 'video':\n await this.renderVideoLayer(layer as VideoLayer);\n break;\n case 'image':\n await this.renderImageLayer(layer as ImageLayer);\n break;\n case 'text':\n await this.renderTextLayer(layer as TextLayer);\n break;\n }\n\n // Apply mask if present\n if (layer.mask) {\n this.applyMask(layer.mask);\n }\n } finally {\n this.ctx.restore();\n }\n }\n\n private applyTransform(transform: Transform2D): void {\n const centerX = this.width * (transform.anchorX ?? 0.5);\n const centerY = this.height * (transform.anchorY ?? 0.5);\n\n this.ctx.translate(transform.x + centerX, transform.y + centerY);\n\n if (transform.rotation) {\n this.ctx.rotate(transform.rotation);\n }\n\n this.ctx.scale(transform.scaleX, transform.scaleY);\n\n if (transform.skewX || transform.skewY) {\n this.ctx.transform(1, transform.skewY ?? 0, transform.skewX ?? 0, 1, 0, 0);\n }\n\n this.ctx.translate(-centerX, -centerY);\n }\n\n private async renderVideoLayer(layer: VideoLayer): Promise<void> {\n const { videoFrame, crop } = layer;\n\n // Get video dimensions\n const videoWidth = videoFrame.displayWidth || videoFrame.codedWidth;\n const videoHeight = videoFrame.displayHeight || videoFrame.codedHeight;\n\n // Calculate scaling to fit (contain mode - preserve aspect ratio)\n const scaleX = this.width / videoWidth;\n const scaleY = this.height / videoHeight;\n\n // Use the smaller scale to ensure entire video fits\n const scale = Math.min(scaleX, scaleY);\n\n // Calculate final render dimensions\n const renderWidth = Math.round(videoWidth * scale);\n const renderHeight = Math.round(videoHeight * scale);\n\n // Center the video\n const renderX = Math.round((this.width - renderWidth) / 2);\n const renderY = Math.round((this.height - renderHeight) / 2);\n\n if (crop) {\n this.ctx.drawImage(\n videoFrame,\n crop.x,\n crop.y,\n crop.width,\n crop.height,\n renderX,\n renderY,\n renderWidth,\n renderHeight\n );\n } else {\n this.ctx.drawImage(videoFrame, renderX, renderY, renderWidth, renderHeight);\n }\n }\n\n private async renderImageLayer(layer: ImageLayer): Promise<void> {\n const { source, crop } = layer;\n\n // Handle ImageData by putting it on canvas first\n if (source instanceof ImageData) {\n if (crop) {\n // For ImageData with crop, we need to extract the cropped region\n const tempCanvas = new OffscreenCanvas(crop.width, crop.height);\n const tempCtx = tempCanvas.getContext('2d')!;\n tempCtx.putImageData(source, -crop.x, -crop.y);\n this.ctx.drawImage(tempCanvas, 0, 0, this.width, this.height);\n } else {\n // Put ImageData directly\n this.ctx.putImageData(source, 0, 0);\n }\n } else {\n // ImageBitmap can be drawn directly\n if (!source) {\n return;\n }\n if (crop) {\n this.ctx.drawImage(\n source,\n crop.x,\n crop.y,\n crop.width,\n crop.height,\n 0,\n 0,\n this.width,\n this.height\n );\n } else {\n this.ctx.drawImage(source, 0, 0, this.width, this.height);\n }\n }\n }\n\n private async renderTextLayer(layer: TextLayer): Promise<void> {\n const fontSize = layer.fontSize ?? 16;\n const fontFamily = layer.fontFamily ?? 'sans-serif';\n const fontWeight = layer.fontWeight ?? 'normal';\n const fontStyle = layer.fontStyle ?? 'normal';\n\n this.ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;\n this.ctx.fillStyle = layer.color ?? '#000000';\n this.ctx.textAlign = layer.textAlign ?? 'left';\n this.ctx.textBaseline = layer.verticalAlign ?? 'top';\n\n // Apply letter spacing if supported\n if (layer.letterSpacing && typeof (this.ctx as any).letterSpacing !== 'undefined') {\n (this.ctx as any).letterSpacing = `${layer.letterSpacing}px`;\n }\n\n // Ensure high-quality text rendering\n this.ensureHighQualityRendering();\n\n // Calculate position with sub-pixel adjustment for sharper rendering\n const baseX = this.calculateTextX(layer.textAlign);\n const baseY = this.calculateTextY(layer.verticalAlign, fontSize);\n const x = Math.round(baseX) + 0.5;\n const y = Math.round(baseY) + 0.5;\n\n // Apply shadow before rendering text (if needed)\n if (layer.shadow) {\n this.ctx.shadowColor = layer.shadow.color;\n this.ctx.shadowOffsetX = layer.shadow.offsetX;\n this.ctx.shadowOffsetY = layer.shadow.offsetY;\n this.ctx.shadowBlur = layer.shadow.blur;\n }\n\n // Render stroke with enhanced quality (multi-layer approach)\n if (layer.strokeColor && layer.strokeWidth && layer.strokeWidth > 0) {\n this.drawEnhancedStroke(layer.text, x, y, layer.strokeColor, layer.strokeWidth);\n }\n\n // Render fill text\n this.ctx.fillText(layer.text, x, y);\n\n // Reset shadow\n if (layer.shadow) {\n this.ctx.shadowColor = 'transparent';\n this.ctx.shadowOffsetX = 0;\n this.ctx.shadowOffsetY = 0;\n this.ctx.shadowBlur = 0;\n }\n }\n\n /**\n * Draw enhanced multi-layer stroke for better text visibility\n */\n private drawEnhancedStroke(\n text: string,\n x: number,\n y: number,\n strokeColor: string,\n strokeWidth: number\n ): void {\n this.ctx.save();\n\n this.ctx.strokeStyle = strokeColor;\n this.ctx.lineJoin = 'round';\n this.ctx.lineCap = 'round';\n this.ctx.miterLimit = 2;\n\n // Multi-layer stroke for enhanced visibility without excessive thickness\n const layers = [1.1, 1.0];\n layers.forEach((multiplier) => {\n this.ctx.lineWidth = strokeWidth * multiplier;\n this.ctx.strokeText(text, x, y);\n });\n\n this.ctx.restore();\n }\n\n private calculateTextX(align?: 'left' | 'center' | 'right'): number {\n switch (align) {\n case 'center':\n return this.width / 2;\n case 'right':\n return this.width;\n default:\n return 0;\n }\n }\n\n private calculateTextY(align?: 'top' | 'middle' | 'bottom', fontSize: number = 16): number {\n switch (align) {\n case 'middle':\n return this.height / 2;\n case 'bottom':\n // Place text at 85% height to avoid bottom edge, similar to SubtitleComposer's 70% position\n return this.height * 0.85;\n default:\n return fontSize;\n }\n }\n\n private applyMask(mask: MaskConfig): void {\n this.ctx.globalCompositeOperation = mask.invert ? 'source-out' : 'destination-in';\n\n if (mask.source) {\n this.ctx.drawImage(mask.source, 0, 0, this.width, this.height);\n } else if (mask.shape === 'circle') {\n this.ctx.beginPath();\n this.ctx.arc(\n this.width / 2,\n this.height / 2,\n Math.min(this.width, this.height) / 2,\n 0,\n Math.PI * 2\n );\n this.ctx.fill();\n }\n }\n\n updateDimensions(width: number, height: number): void {\n this.width = width;\n this.height = height;\n this.ensureHighQualityRendering();\n }\n}\n","import type { TransitionEffect } from './types';\n\n/**\n * TransitionProcessor - Handles transition effects between frames/scenes\n * Single responsibility: Apply transition transformations to canvas context\n */\nexport class TransitionProcessor {\n private width: number;\n private height: number;\n\n constructor(width: number, height: number) {\n this.width = width;\n this.height = height;\n }\n\n /**\n * Apply transition effect to the canvas context\n * Returns true if transition was applied, false if not needed\n */\n applyTransition(ctx: OffscreenCanvasRenderingContext2D, transition: TransitionEffect): boolean {\n if (!transition || transition.progress <= 0) return false;\n\n const progress = this.calculateEasedProgress(transition.progress, transition.easing);\n\n switch (transition.type) {\n case 'fade':\n return this.applyFade(ctx, progress);\n case 'slide':\n return this.applySlide(ctx, progress, transition.direction);\n case 'wipe':\n return this.applyWipe(ctx, progress, transition.direction);\n case 'zoom':\n return this.applyZoom(ctx, progress, transition.direction);\n case 'rotate':\n return this.applyRotate(ctx, progress);\n case 'dissolve':\n return this.applyDissolve(ctx, progress);\n default:\n return false;\n }\n }\n\n private calculateEasedProgress(\n progress: number,\n easing?: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out'\n ): number {\n switch (easing) {\n case 'ease-in':\n return progress * progress;\n case 'ease-out':\n return 1 - (1 - progress) * (1 - progress);\n case 'ease-in-out':\n return progress < 0.5 ? 2 * progress * progress : 1 - Math.pow(-2 * progress + 2, 2) / 2;\n default:\n return progress;\n }\n }\n\n private applyFade(ctx: OffscreenCanvasRenderingContext2D, progress: number): boolean {\n ctx.globalAlpha = progress;\n return true;\n }\n\n private applySlide(\n ctx: OffscreenCanvasRenderingContext2D,\n progress: number,\n direction?: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'\n ): boolean {\n const distance = 1 - progress;\n\n switch (direction) {\n case 'left':\n ctx.translate(-this.width * distance, 0);\n break;\n case 'right':\n ctx.translate(this.width * distance, 0);\n break;\n case 'up':\n ctx.translate(0, -this.height * distance);\n break;\n case 'down':\n ctx.translate(0, this.height * distance);\n break;\n default:\n ctx.translate(-this.width * distance, 0);\n }\n\n return true;\n }\n\n private applyWipe(\n ctx: OffscreenCanvasRenderingContext2D,\n progress: number,\n direction?: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'\n ): boolean {\n ctx.save();\n ctx.beginPath();\n\n switch (direction) {\n case 'left':\n ctx.rect(0, 0, this.width * progress, this.height);\n break;\n case 'right':\n ctx.rect(this.width * (1 - progress), 0, this.width * progress, this.height);\n break;\n case 'up':\n ctx.rect(0, 0, this.width, this.height * progress);\n break;\n case 'down':\n ctx.rect(0, this.height * (1 - progress), this.width, this.height * progress);\n break;\n default:\n ctx.rect(0, 0, this.width * progress, this.height);\n }\n\n ctx.clip();\n return true;\n }\n\n private applyZoom(\n ctx: OffscreenCanvasRenderingContext2D,\n progress: number,\n direction?: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'\n ): boolean {\n const scale = direction === 'out' ? 1 + (1 - progress) : progress;\n const centerX = this.width / 2;\n const centerY = this.height / 2;\n\n ctx.translate(centerX, centerY);\n ctx.scale(scale, scale);\n ctx.translate(-centerX, -centerY);\n\n if (direction === 'out') {\n ctx.globalAlpha = progress;\n }\n\n return true;\n }\n\n private applyRotate(ctx: OffscreenCanvasRenderingContext2D, progress: number): boolean {\n const rotation = (1 - progress) * Math.PI * 2;\n const centerX = this.width / 2;\n const centerY = this.height / 2;\n\n ctx.translate(centerX, centerY);\n ctx.rotate(rotation);\n ctx.translate(-centerX, -centerY);\n\n return true;\n }\n\n private applyDissolve(ctx: OffscreenCanvasRenderingContext2D, progress: number): boolean {\n // Simple dissolve using alpha\n ctx.globalAlpha = progress;\n ctx.globalCompositeOperation = 'multiply';\n return true;\n }\n\n /**\n * Create a transition mask for advanced effects\n */\n createTransitionMask(transition: TransitionEffect, canvas: OffscreenCanvas): ImageData | null {\n const ctx = canvas.getContext('2d');\n if (!ctx) return null;\n\n const imageData = ctx.createImageData(this.width, this.height);\n const data = imageData.data;\n const progress = this.calculateEasedProgress(transition.progress, transition.easing);\n\n // Generate gradient mask based on transition type\n for (let y = 0; y < this.height; y++) {\n for (let x = 0; x < this.width; x++) {\n const index = (y * this.width + x) * 4;\n let alpha = 255;\n\n switch (transition.type) {\n case 'wipe':\n alpha = this.calculateWipeAlpha(x, y, progress, transition.direction);\n break;\n case 'dissolve':\n // Random noise dissolve\n alpha = Math.random() < progress ? 255 : 0;\n break;\n default:\n alpha = Math.floor(255 * progress);\n }\n\n data[index] = 255; // R\n data[index + 1] = 255; // G\n data[index + 2] = 255; // B\n data[index + 3] = alpha; // A\n }\n }\n\n return imageData;\n }\n\n private calculateWipeAlpha(\n x: number,\n y: number,\n progress: number,\n direction?: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'\n ): number {\n let position = 0;\n\n switch (direction) {\n case 'left':\n position = x / this.width;\n break;\n case 'right':\n position = 1 - x / this.width;\n break;\n case 'up':\n position = y / this.height;\n break;\n case 'down':\n position = 1 - y / this.height;\n break;\n case 'in': {\n // Radial wipe from center\n const cx = x - this.width / 2;\n const cy = y - this.height / 2;\n const maxDist = Math.sqrt(this.width * this.width + this.height * this.height) / 2;\n position = Math.sqrt(cx * cx + cy * cy) / maxDist;\n break;\n }\n case 'out': {\n // Radial wipe to center\n const cx2 = x - this.width / 2;\n const cy2 = y - this.height / 2;\n const maxDist2 = Math.sqrt(this.width * this.width + this.height * this.height) / 2;\n position = 1 - Math.sqrt(cx2 * cx2 + cy2 * cy2) / maxDist2;\n break;\n }\n default:\n position = x / this.width;\n }\n\n return position < progress ? 255 : 0;\n }\n\n updateDimensions(width: number, height: number): void {\n this.width = width;\n this.height = height;\n }\n}\n","import type { VisualFilter } from './types';\n\n/**\n * FilterProcessor - Handles visual filters and effects\n * Single responsibility: Apply CSS filters and custom shader effects\n */\nexport class FilterProcessor {\n private filterCache = new Map<string, string>();\n\n /**\n * Apply filters to canvas context\n * Combines multiple filters into a single CSS filter string for performance\n */\n applyFilters(ctx: OffscreenCanvasRenderingContext2D, filters: VisualFilter[]): void {\n if (!filters || filters.length === 0) {\n ctx.filter = 'none';\n return;\n }\n\n // Generate cache key\n const cacheKey = this.generateCacheKey(filters);\n\n // Check cache\n let filterString = this.filterCache.get(cacheKey);\n\n if (!filterString) {\n filterString = this.buildFilterString(filters);\n this.filterCache.set(cacheKey, filterString);\n }\n\n ctx.filter = filterString;\n }\n\n /**\n * Build CSS filter string from filter array\n */\n private buildFilterString(filters: VisualFilter[]): string {\n const filterStrings: string[] = [];\n\n for (const filter of filters) {\n const filterStr = this.buildSingleFilter(filter);\n if (filterStr) {\n filterStrings.push(filterStr);\n }\n }\n\n return filterStrings.length > 0 ? filterStrings.join(' ') : 'none';\n }\n\n private buildSingleFilter(filter: VisualFilter): string | null {\n switch (filter.type) {\n case 'blur':\n return `blur(${filter.value ?? 0}px)`;\n\n case 'brightness':\n return `brightness(${filter.value ?? 1})`;\n\n case 'contrast':\n return `contrast(${filter.value ?? 1})`;\n\n case 'grayscale':\n return `grayscale(${filter.value ?? 0})`;\n\n case 'hue-rotate':\n return `hue-rotate(${filter.value ?? 0}deg)`;\n\n case 'saturate':\n return `saturate(${filter.value ?? 1})`;\n\n case 'sepia':\n return `sepia(${filter.value ?? 0})`;\n\n case 'custom':\n return this.buildCustomFilter(filter);\n\n default:\n console.warn(`Unknown filter type: ${filter.type}`);\n return null;\n }\n }\n\n /**\n * Build custom filter from params\n */\n private buildCustomFilter(filter: VisualFilter): string | null {\n if (!filter.params) return null;\n\n const { type, ...params } = filter.params;\n\n switch (type) {\n case 'drop-shadow':\n return `drop-shadow(${params.offsetX}px ${params.offsetY}px ${params.blur}px ${params.color})`;\n\n case 'opacity':\n return `opacity(${params.value})`;\n\n case 'invert':\n return `invert(${params.value})`;\n\n default:\n return null;\n }\n }\n\n /**\n * Apply color matrix transformation for advanced effects\n * This allows for more complex color manipulations than CSS filters\n */\n applyColorMatrix(imageData: ImageData, matrix: number[]): ImageData {\n if (matrix.length !== 20) {\n throw new Error('Color matrix must have 20 values (4x5 matrix)');\n }\n\n const data = imageData.data;\n const length = data.length;\n\n for (let i = 0; i < length; i += 4) {\n const r = data[i]!;\n const g = data[i + 1]!;\n const b = data[i + 2]!;\n const a = data[i + 3]!;\n const m = matrix;\n\n // Apply matrix transformation\n data[i] = this.clamp(r * m[0]! + g * m[1]! + b * m[2]! + a * m[3]! + m[4]! * 255);\n data[i + 1] = this.clamp(r * m[5]! + g * m[6]! + b * m[7]! + a * m[8]! + m[9]! * 255);\n data[i + 2] = this.clamp(r * m[10]! + g * m[11]! + b * m[12]! + a * m[13]! + m[14]! * 255);\n data[i + 3] = this.clamp(r * m[15]! + g * m[16]! + b * m[17]! + a * m[18]! + m[19]! * 255);\n }\n\n return imageData;\n }\n\n /**\n * Predefined color matrices for common effects\n */\n getPresetMatrix(preset: string): number[] | null {\n switch (preset) {\n case 'vintage':\n return [\n 0.393, 0.769, 0.189, 0, 0, 0.349, 0.686, 0.168, 0, 0, 0.272, 0.534, 0.131, 0, 0, 0, 0, 0,\n 1, 0,\n ];\n\n case 'noir':\n return [\n 0.25, 0.25, 0.25, 0, 0, 0.25, 0.25, 0.25, 0, 0, 0.25, 0.25, 0.25, 0, 0, 0, 0, 0, 1, 0,\n ];\n\n case 'cool':\n return [0.8, 0, 0, 0, 0, 0, 0.9, 0, 0, 0, 0, 0, 1.2, 0, 0, 0, 0, 0, 1, 0];\n\n case 'warm':\n return [1.2, 0, 0, 0, 0, 0, 1.1, 0, 0, 0, 0, 0, 0.8, 0, 0, 0, 0, 0, 1, 0];\n\n default:\n return null;\n }\n }\n\n /**\n * Apply Gaussian blur manually (for cases where CSS filter is not enough)\n */\n applyGaussianBlur(imageData: ImageData, radius: number): ImageData {\n // Simplified box blur approximation of Gaussian blur\n const output = new ImageData(\n new Uint8ClampedArray(imageData.data),\n imageData.width,\n imageData.height\n );\n\n const width = imageData.width;\n const height = imageData.height;\n const data = imageData.data;\n const outData = output.data;\n\n // Horizontal pass\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n let r = 0,\n g = 0,\n b = 0,\n a = 0;\n let count = 0;\n\n for (let dx = -radius; dx <= radius; dx++) {\n const nx = Math.min(Math.max(x + dx, 0), width - 1);\n const idx = (y * width + nx) * 4;\n r += data[idx]!;\n g += data[idx + 1]!;\n b += data[idx + 2]!;\n a += data[idx + 3]!;\n count++;\n }\n\n const idx = (y * width + x) * 4;\n outData[idx] = r / count;\n outData[idx + 1] = g / count;\n outData[idx + 2] = b / count;\n outData[idx + 3] = a / count;\n }\n }\n\n // Vertical pass\n for (let x = 0; x < width; x++) {\n for (let y = 0; y < height; y++) {\n let r = 0,\n g = 0,\n b = 0,\n a = 0;\n let count = 0;\n\n for (let dy = -radius; dy <= radius; dy++) {\n const ny = Math.min(Math.max(y + dy, 0), height - 1);\n const idx = (ny * width + x) * 4;\n r += outData[idx]!;\n g += outData[idx + 1]!;\n b += outData[idx + 2]!;\n a += outData[idx + 3]!;\n count++;\n }\n\n const idx = (y * width + x) * 4;\n data[idx] = r / count;\n data[idx + 1] = g / count;\n data[idx + 2] = b / count;\n data[idx + 3] = a / count;\n }\n }\n\n return imageData;\n }\n\n private clamp(value: number): number {\n return Math.min(255, Math.max(0, Math.round(value)));\n }\n\n private generateCacheKey(filters: VisualFilter[]): string {\n return filters.map((f) => `${f.type}:${f.value ?? 'default'}`).join('|');\n }\n\n clearCache(): void {\n this.filterCache.clear();\n }\n\n getCacheSize(): number {\n return this.filterCache.size;\n }\n}\n","import type {\n VideoComposeConfig,\n ComposeRequest,\n ComposeResult,\n TransitionEffect,\n VideoLayer,\n ComposeTimelineContext,\n} from './types';\nimport { frameDurationFromFps } from '../../utils/time-utils';\nimport { LayerRenderer } from './LayerRenderer';\nimport { TransitionProcessor } from './TransitionProcessor';\nimport { FilterProcessor } from './FilterProcessor';\nimport { ClipInstructionSet } from './instructions';\n\ninterface ComposeStreams {\n composeStream: WritableStream<ComposeRequest>;\n cacheStream: ReadableStream<VideoFrame>;\n}\n\n/**\n * VideoComposer - Main visual composition orchestrator\n */\nexport class VideoComposer {\n readonly config: Required<VideoComposeConfig>;\n readonly canvas: OffscreenCanvas;\n\n private ctx: OffscreenCanvasRenderingContext2D;\n private layerRenderer: LayerRenderer;\n private transitionProcessor: TransitionProcessor;\n private filterProcessor: FilterProcessor;\n private timelineContext: ComposeTimelineContext;\n\n constructor(config: VideoComposeConfig) {\n this.config = this.applyDefaults(config);\n this.canvas = new OffscreenCanvas(this.config.width, this.config.height);\n\n const ctx = this.canvas.getContext('2d', {\n alpha: true,\n desynchronized: true,\n willReadFrequently: false,\n colorSpace: 'srgb',\n });\n\n if (!ctx) {\n throw new Error('Failed to create 2D rendering context');\n }\n\n this.ctx = ctx;\n this.ctx.imageSmoothingEnabled = this.config.enableSmoothing;\n this.ctx.imageSmoothingQuality = 'high';\n\n this.layerRenderer = new LayerRenderer(ctx, this.config.width, this.config.height);\n this.transitionProcessor = new TransitionProcessor(this.config.width, this.config.height);\n this.filterProcessor = new FilterProcessor();\n this.timelineContext = this.config.timeline;\n }\n\n private applyDefaults(config: VideoComposeConfig): Required<VideoComposeConfig> {\n return {\n width: config.width || 720,\n height: config.height || 1280,\n fps: config.fps || 30,\n backgroundColor: config.backgroundColor ?? '#000',\n renderer: config.renderer ?? 'canvas2d',\n enableSmoothing: config.enableSmoothing ?? true,\n enableHardwareAcceleration: config.enableHardwareAcceleration ?? true,\n revision: config.revision ?? 0,\n inputHighWaterMark: config.inputHighWaterMark ?? 3,\n outputHighWaterMark: config.outputHighWaterMark ?? 1,\n maxLayers: config.maxLayers ?? 100,\n timeline: config.timeline ?? {\n clipId: 'default',\n trackId: 'main',\n clipStartUs: 0,\n clipDurationUs: Infinity,\n compositionFps: 30,\n },\n };\n }\n\n createStreams(_instruction?: ClipInstructionSet): ComposeStreams {\n if (_instruction?.baseConfig.timeline) {\n this.timelineContext = _instruction.baseConfig.timeline;\n }\n\n // Always create new streams for each clip\n // ReadableStreams can only be consumed once\n const stream = new TransformStream<ComposeRequest, any>(\n {\n transform: async (request, controller) => {\n // console.log('[VideoComposer] transform', request, controller.desiredSize);\n const result = await this.composeFrame(request);\n controller.enqueue({\n frame: result.frame,\n metadata: result.metadata,\n });\n },\n\n flush: async () => {\n this.filterProcessor.clearCache();\n },\n },\n {\n highWaterMark: this.config.inputHighWaterMark,\n },\n {\n highWaterMark: this.config.outputHighWaterMark,\n }\n );\n // const [encodeStream, cacheStream] = stream.readable.tee();\n return {\n composeStream: stream.writable,\n cacheStream: stream.readable,\n };\n }\n\n async composeFrame(request: ComposeRequest): Promise<ComposeResult> {\n if (request.layers.length > this.config.maxLayers) {\n throw new Error(`Too many layers: ${request.layers.length} > ${this.config.maxLayers}`);\n }\n\n this.clearCanvas();\n // const sortedLayers = this.sortLayers(request.layers);\n\n if (request.transition) {\n this.ctx.save();\n this.transitionProcessor.applyTransition(this.ctx, request.transition);\n }\n\n for (const layer of request.layers) {\n if (!layer.visible || layer.opacity <= 0) {\n // Close video frame for invisible layers\n if ((layer as any).type === 'video') {\n const vf = (layer as VideoLayer).videoFrame;\n vf?.close?.();\n }\n continue;\n }\n\n try {\n if (layer.filters && layer.filters.length > 0) {\n this.ctx.save();\n this.filterProcessor.applyFilters(this.ctx, layer.filters);\n }\n\n await this.layerRenderer.renderLayer(layer);\n\n if (layer.filters && layer.filters.length > 0) {\n this.ctx.restore();\n }\n } finally {\n // Always close video frame after rendering (or on error)\n if ((layer as any).type === 'video') {\n const vf = (layer as VideoLayer).videoFrame;\n vf?.close?.();\n }\n }\n }\n\n if (request.transition) {\n this.ctx.restore();\n }\n\n const frame = await this.createOutputFrame(request.timeUs);\n\n const gopSerial = (request as any).gopSerial;\n const isKeyframe = (request as any).isKeyframe;\n\n return {\n frame,\n timeUs: request.timeUs,\n metadata: {\n layerCount: request.layers.length,\n renderTime: 0,\n gpuAccelerated:\n this.config.enableHardwareAcceleration && this.config.renderer !== 'canvas2d',\n ...(typeof gopSerial === 'number' && { gopSerial }),\n ...(typeof isKeyframe === 'boolean' && { isKeyframe }),\n },\n };\n }\n\n async composeTransition(\n fromRequest: ComposeRequest,\n toRequest: ComposeRequest,\n transition: TransitionEffect\n ): Promise<ComposeResult> {\n await this.composeFrame(fromRequest);\n\n const toFrameRequest = {\n ...toRequest,\n transition,\n };\n\n return this.composeFrame(toFrameRequest);\n }\n\n private clearCanvas(): void {\n if (this.config.backgroundColor) {\n this.ctx.fillStyle = this.config.backgroundColor;\n this.ctx.fillRect(0, 0, this.config.width, this.config.height);\n } else {\n this.ctx.clearRect(0, 0, this.config.width, this.config.height);\n }\n }\n\n // private sortLayers(layers: Layer[]): Layer[] {\n // return [...layers].sort((a, b) => a.zIndex - b.zIndex);\n // }\n\n private async createOutputFrame(timeUs: number): Promise<VideoFrame> {\n const duration = frameDurationFromFps(this.timelineContext.compositionFps);\n const frame = new VideoFrame(this.canvas, {\n timestamp: timeUs,\n duration,\n alpha: 'discard',\n visibleRect: { x: 0, y: 0, width: this.canvas.width, height: this.canvas.height },\n });\n return frame;\n }\n\n updateConfig(config: Partial<VideoComposeConfig>): void {\n Object.assign(this.config, this.applyDefaults({ ...this.config, ...config }));\n\n if (config.width || config.height) {\n this.canvas.width = this.config.width;\n this.canvas.height = this.config.height;\n this.layerRenderer.updateDimensions(this.config.width, this.config.height);\n this.transitionProcessor.updateDimensions(this.config.width, this.config.height);\n }\n\n if (config.enableSmoothing !== undefined) {\n this.ctx.imageSmoothingEnabled = this.config.enableSmoothing;\n }\n\n if (config.timeline) {\n this.timelineContext = config.timeline;\n }\n }\n\n dispose(): void {\n this.filterProcessor.clearCache();\n }\n}\n","import { WorkerChannel } from '../../worker/WorkerChannel';\nimport { WorkerMessageType, WorkerState } from '../../worker/types';\nimport { VideoComposer } from './VideoComposer';\nimport type { VideoComposeConfig, ComposeRequest, Layer } from './types';\nimport type {\n ClipInstructionSet,\n SerializedLayerPlan,\n SerializedTransitionPlan,\n SerializedImageLayerPayload,\n SerializedTextLayerPayload,\n} from './instructions';\nimport {\n frameDurationFromFps,\n frameIndexFromTimestamp,\n quantizeTimestampToFrame,\n} from '../../utils/time-utils';\n\ntype TimeUs = number;\n\nfunction resolveActiveLayers(\n layers: SerializedLayerPlan[],\n timestamp: number,\n _frame: VideoFrame\n): SerializedLayerPlan[] {\n return layers.filter((layer) => {\n if (layer.status !== 'ready') {\n return false;\n }\n return layer.activeRanges.some(\n (range) => timestamp >= range.startUs && timestamp < range.endUs\n );\n });\n}\n\nfunction materializeLayer(layer: SerializedLayerPlan, frame: VideoFrame): Layer {\n const baseLayer: Layer = {\n id: layer.layerId,\n type: layer.type as any,\n zIndex: layer.zIndex ?? 0,\n visible: true,\n opacity: layer.opacity ?? 1,\n };\n\n if (layer.type === 'video') {\n return {\n ...baseLayer,\n type: 'video',\n videoFrame: frame,\n } as Layer;\n }\n\n if (layer.type === 'text') {\n const payload = layer.payload as SerializedTextLayerPayload;\n return {\n ...baseLayer,\n type: 'text',\n text: payload.text,\n fontFamily: payload.fontFamily,\n fontSize: payload.fontSize,\n fontWeight: payload.fontWeight,\n color: payload.color,\n strokeColor: payload.strokeColor,\n strokeWidth: payload.strokeWidth,\n lineHeight: payload.lineHeight,\n textAlign: payload.align,\n verticalAlign: 'bottom', // Subtitles positioned at bottom\n } as Layer;\n }\n\n if (layer.type === 'image') {\n const payload = layer.payload as SerializedImageLayerPayload;\n const source = payload.bitmapHandle ?? null;\n if (source) {\n return {\n ...baseLayer,\n type: 'image',\n source,\n } as Layer;\n }\n }\n\n return baseLayer;\n}\n\n/**\n * VideoComposeWorker - Visual composition in the pipeline\n * Receives decoded video frames and outputs composed frames\n *\n * Pipeline: DecodeWorker → VideoComposeWorker → EncodeWorker\n *\n * Features:\n * - Multi-layer composition with Canvas2D/WebGL\n * - Transition effects and filters\n * - Text and image overlay support\n * - Stream-based processing with configurable backpressure\n */\nexport class VideoComposeWorker {\n private channel: WorkerChannel;\n private composer: VideoComposer | null = null;\n private composeStream: TransformStream<ComposeRequest, VideoFrame> | null = null;\n\n private downstreamPorts = new Map<string, MessagePort>();\n private upstreamPorts = new Map<string, MessagePort>();\n private instructionRegistry = new Map<string, ClipInstructionSet>();\n private pendingReplay = new Map<string, { startUs: number; endUs: number; revision: number }>();\n private streamState = new Map<string, StreamState>();\n\n constructor() {\n // Initialize WorkerChannel\n this.channel = new WorkerChannel(self as any, {\n name: 'VideoComposeWorker',\n timeout: 30000,\n });\n\n this.setupHandlers();\n }\n\n private setupHandlers(): void {\n // Register message handlers\n this.channel.registerHandler('configure', this.handleConfigure.bind(this));\n this.channel.registerHandler('connect' as any, this.handleConnect.bind(this));\n this.channel.registerHandler('flush', this.handleFlush.bind(this));\n // this.channel.registerHandler('get_stream', this.handleGetStream.bind(this));\n this.channel.registerHandler('get_stats', this.handleGetStats.bind(this));\n this.channel.registerHandler('install_instructions', this.handleInstallInstructions.bind(this));\n this.channel.registerHandler('sync_clip', this.handleSyncClip.bind(this));\n this.channel.registerHandler('dispose_clip', this.handleDisposeClip.bind(this));\n this.channel.registerHandler(WorkerMessageType.Dispose, this.handleDispose.bind(this));\n }\n\n /**\n * Unified connect handler used by stream pipeline\n */\n private async handleConnect(payload: {\n direction: 'upstream' | 'downstream';\n port: MessagePort;\n streamType: 'video' | 'audio' | 'frame' | 'chunk';\n clipId?: string;\n }): Promise<{ success: boolean }> {\n const { port, direction, clipId = 'default' } = payload;\n if (direction === 'upstream') {\n this.upstreamPorts.set(clipId, port);\n const channel = new WorkerChannel(port, {\n name: 'VideoCompose-Decode',\n timeout: 30000,\n });\n channel.receiveStream(this.handleReceiveStream.bind(this));\n }\n if (direction === 'downstream') {\n this.downstreamPorts.set(clipId, port);\n }\n return { success: true };\n }\n\n /**\n * Configure composer\n * According to docs/impl/14-config, only reinitialize when initial=true\n */\n private async handleConfigure(payload: {\n config: VideoComposeConfig;\n initial?: boolean;\n }): Promise<{\n success: boolean;\n config: VideoComposeConfig;\n }> {\n const { config, initial } = payload;\n\n const hasValidDimensions = config.width > 0 && config.height > 0;\n const hasValidFps = config.fps > 0;\n\n if (!hasValidDimensions || !hasValidFps) {\n throw new Error(\n `VideoComposeWorker: invalid canvas config width=${config.width}, height=${config.height}, fps=${config.fps}`\n );\n }\n\n // Set worker state to ready on initial configuration\n if (initial) {\n this.channel.state = WorkerState.Ready;\n }\n\n if (initial || !this.composer) {\n // Initial configuration or composer doesn't exist\n if (this.composer) {\n this.composer.dispose();\n this.composeStream = null;\n }\n\n // Create new composer\n this.composer = new VideoComposer(config);\n } else {\n // Just update configuration\n this.composer.updateConfig(config);\n }\n\n // Notify configuration complete\n this.channel.notify('configured', {\n config: this.composer.config,\n initialized: initial || false,\n });\n\n return {\n success: true,\n config: this.composer.config,\n };\n }\n\n private async handleReceiveStream(\n stream: ReadableStream,\n metadata?: Record<string, any>\n ): Promise<void> {\n const { clipId = 'default' } = metadata || {};\n\n if (!this.composer) {\n console.error('[VideoComposeWorker] Composer not configured');\n return;\n }\n\n const instruction = this.instructionRegistry.get(clipId);\n if (!instruction) {\n console.warn('[VideoComposeWorker] No instructions for clip', clipId);\n return;\n }\n\n // TODO: ENCODE\n // const downstreamPort = this.downstreamPorts.get(clipId);\n // if (downstreamPort) {\n // const channel = new WorkerChannel(downstreamPort, {\n // name: 'VideoCompose-Encoder',\n // timeout: 30000,\n // });\n // channel.sendStream(encodeStream, {\n // streamType: 'video',\n // ...metadata,\n // });\n // }\n\n const filteredStream = stream.pipeThrough(\n new TransformStream<any, ComposeRequest>({\n transform: (wrappedFrame, controller) => {\n try {\n // Extract frame and metadata from wrapped object\n const frame = wrappedFrame.frame || wrappedFrame;\n const gopSerial = wrappedFrame.gopSerial;\n const isKeyframe = wrappedFrame.isKeyframe;\n\n const timestamp = frame.timestamp ?? 0;\n if (this.shouldSkipFrame(clipId, timestamp)) {\n frame.close();\n return;\n }\n\n const request = this.buildComposeRequest(clipId, instruction, frame, timestamp);\n if (!request) {\n frame.close();\n return;\n }\n (request as any).gopSerial = gopSerial;\n (request as any).isKeyframe = isKeyframe;\n controller.enqueue(request);\n } catch (error) {\n const frame = wrappedFrame.frame || wrappedFrame;\n frame?.close?.();\n throw error;\n }\n },\n })\n );\n\n const { composeStream, cacheStream } = this.composer.createStreams();\n this.channel.sendStream(cacheStream, metadata);\n\n filteredStream.pipeTo(composeStream).catch((error) => {\n console.error('[VideoComposeWorker] compose stream error', clipId, error);\n });\n }\n\n // private handleGetStream(): ReadableStream<VideoFrame> | undefined {\n // return this.composer?.createStreams()?.cacheStream;\n // }\n\n /**\n * Flush the composition pipeline\n */\n private async handleFlush(): Promise<{ success: boolean }> {\n try {\n // Flush any pending frames in the stream\n // The stream will handle this automatically\n\n this.channel.notify('flush_complete', {});\n return { success: true };\n } catch (error: any) {\n throw {\n code: 'FLUSH_ERROR',\n message: error.message,\n };\n }\n }\n\n /**\n * Get composer statistics\n */\n private async handleGetStats(): Promise<{\n configured: boolean;\n config?: VideoComposeConfig;\n streaming: boolean;\n }> {\n return {\n configured: this.composer !== null,\n config: this.composer?.config,\n streaming: this.composeStream !== null,\n };\n }\n\n /**\n * Dispose worker and cleanup resources\n */\n private async handleDispose(): Promise<{ success: boolean }> {\n // Dispose composer\n if (this.composer) {\n this.composer.dispose();\n this.composer = null;\n }\n\n this.composeStream = null;\n\n // Close connections\n this.downstreamPorts.get('default')?.close();\n this.upstreamPorts.get('default')?.close();\n this.downstreamPorts.clear();\n this.upstreamPorts.clear();\n\n this.channel.state = WorkerState.Disposed;\n\n return { success: true };\n }\n\n private async handleInstallInstructions(data: ClipInstructionSet): Promise<{ success: boolean }> {\n const { clipId, revision } = data;\n const current = this.instructionRegistry.get(clipId);\n if (current && current.revision > revision) {\n return { success: false };\n }\n\n this.instructionRegistry.set(clipId, data);\n return { success: true };\n }\n\n private async handleSyncClip(payload: {\n clipId: string;\n revision: number;\n range: { startUs: number; endUs: number };\n }): Promise<{ success: boolean }> {\n const { clipId, revision, range } = payload;\n const current = this.instructionRegistry.get(clipId);\n if (!current || current.revision > revision) {\n return { success: false };\n }\n\n this.pendingReplay.set(clipId, { ...range, revision });\n this.channel.notify('sync_ack', { clipId, revision });\n return { success: true };\n }\n\n private async handleDisposeClip(payload: { clipId: string }): Promise<{ success: boolean }> {\n const { clipId } = payload;\n this.instructionRegistry.delete(clipId);\n this.pendingReplay.delete(clipId);\n this.downstreamPorts.get(clipId)?.close();\n this.upstreamPorts.get(clipId)?.close();\n this.downstreamPorts.delete(clipId);\n this.upstreamPorts.delete(clipId);\n return { success: true };\n }\n\n /**\n * Check if frame should be skipped (outside dirty range)\n * Returns true if frame is NOT in the dirty range and should use cached version\n */\n private shouldSkipFrame(clipId: string, timestamp: number): boolean {\n const dirtyRange = this.pendingReplay.get(clipId);\n if (!dirtyRange) {\n return false; // No dirty range, don't skip (first render or no updates)\n }\n\n // Frame is within dirty range → don't skip, re-compose\n if (timestamp >= dirtyRange.startUs && timestamp <= dirtyRange.endUs) {\n return false;\n }\n\n // Passed dirty range end → clean up\n if (timestamp > dirtyRange.endUs) {\n this.pendingReplay.delete(clipId);\n }\n\n // Frame outside dirty range → skip, use cache\n return true;\n }\n\n private buildComposeRequest(\n clipId: string,\n instruction: ClipInstructionSet,\n frame: VideoFrame,\n _timestamp: number\n ): ComposeRequest | null {\n const normalizedTime = this.computeTimelineTimestamp(clipId, frame, instruction.baseConfig);\n const clipStartUs = instruction.baseConfig.timeline?.clipStartUs ?? 0;\n const clipDurationUs = instruction.baseConfig.timeline?.clipDurationUs ?? Infinity;\n const clipEndUs = clipStartUs + clipDurationUs;\n\n // Check if frame is within clip boundary\n if (normalizedTime < clipStartUs || normalizedTime >= clipEndUs) {\n return null;\n }\n\n const clipRelativeTime = normalizedTime - clipStartUs;\n const activeLayers = resolveActiveLayers(instruction.layers, clipRelativeTime, frame);\n\n if (!activeLayers.length) {\n return null;\n }\n const layers = activeLayers.map((layer) => materializeLayer(layer, frame));\n return {\n timeUs: normalizedTime,\n layers,\n transition: VideoComposeWorker.buildTransition(\n instruction.transitions,\n clipRelativeTime,\n instruction.baseConfig.timeline\n ),\n };\n }\n\n private static buildTransition(\n transitions: SerializedTransitionPlan[],\n timeUs: TimeUs,\n _timeline?: VideoComposeConfig['timeline']\n ) {\n const entry = transitions.find((transition) => {\n const { startUs, endUs } = transition.range;\n return timeUs >= startUs && timeUs < endUs;\n });\n if (!entry) {\n return undefined;\n }\n const durationUs = entry.range.endUs - entry.range.startUs;\n const progress = durationUs > 0 ? (timeUs - entry.range.startUs) / durationUs : 0;\n return {\n type: entry.params.type,\n progress: Math.min(Math.max(progress, 0), 1),\n easing: entry.params.easing,\n params: entry.params.payload,\n direction: entry.params.payload?.direction,\n } as any;\n }\n\n private computeTimelineTimestamp(\n clipId: string,\n frame: VideoFrame,\n config: VideoComposeConfig\n ): TimeUs {\n const key = clipId;\n let state = this.streamState.get(key);\n if (!state) {\n state = {\n baseTimestamp: null,\n lastSourceTimestamp: null,\n nextFrameIndex: 0,\n };\n this.streamState.set(key, state);\n }\n\n const timeline = config.timeline;\n if (!timeline) {\n const ts = frame.timestamp ?? 0;\n state.lastSourceTimestamp = frame.timestamp ?? null;\n return ts;\n }\n\n const { clipStartUs, compositionFps } = timeline;\n const sourceTimestamp = frame.timestamp ?? null;\n\n // Detect stream reset (e.g., after seek)\n if (\n sourceTimestamp !== null &&\n state.lastSourceTimestamp !== null &&\n sourceTimestamp < state.lastSourceTimestamp\n ) {\n state.baseTimestamp = null;\n state.nextFrameIndex = 0;\n }\n\n // Initialize base timestamp (should be 0 or close to 0 after demuxer normalization)\n if (state.baseTimestamp === null) {\n state.baseTimestamp = sourceTimestamp ?? 0;\n state.nextFrameIndex = 0;\n\n // Warn if base is not near zero (indicates demuxer normalization issue)\n if (state.baseTimestamp > 1000) {\n console.warn(\n `[VideoComposeWorker] First frame timestamp is ${state.baseTimestamp}us, expected ~0. ` +\n `Check MP4Demuxer normalization.`\n );\n }\n }\n\n // Since demuxer normalizes to 0, we can directly calculate frame index\n const frameDuration = frameDurationFromFps(compositionFps);\n let frameIndex = state.nextFrameIndex;\n\n if (sourceTimestamp !== null) {\n // Calculate frame index from normalized timestamp\n const approxIndex = frameIndexFromTimestamp(\n state.baseTimestamp,\n sourceTimestamp,\n compositionFps,\n 'nearest'\n );\n frameIndex = Math.max(frameIndex, approxIndex);\n }\n\n // Map to timeline position\n const rawTimeline = clipStartUs + frameIndex * frameDuration;\n const timelineTime = quantizeTimestampToFrame(\n rawTimeline,\n clipStartUs,\n compositionFps,\n 'nearest'\n );\n\n state.nextFrameIndex = frameIndex + 1;\n state.lastSourceTimestamp = sourceTimestamp;\n\n return timelineTime;\n }\n}\n\ninterface StreamState {\n baseTimestamp: number | null;\n lastSourceTimestamp: number | null;\n nextFrameIndex: number;\n}\n\n// Initialize worker\nconst worker = new VideoComposeWorker();\n\n// Handle worker termination\nself.addEventListener('beforeunload', () => {\n worker['handleDispose']();\n});\n\nexport default null; // Required for TypeScript worker compilation\n"],"names":["idx"],"mappings":";AAEO,MAAM,0BAA0B;AAEvC,MAAM,cAAc;AAIb,SAAS,aAAa,OAAwB;AACnD,MAAI,CAAC,OAAO,SAAS,KAAK,KAAM,SAAoB,GAAG;AACrD,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAEO,SAAS,qBAAqB,KAAsB;AACzD,QAAM,aAAa,aAAa,GAAG;AACnC,QAAM,WAAW,0BAA0B;AAC3C,SAAO,KAAK,IAAI,KAAK,MAAM,QAAQ,GAAG,CAAC;AACzC;AAEO,SA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MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAGF,UAAM,iBAAiB,aAAa;AACpC,UAAM,sBAAsB;AAE5B,WAAO;AAAA,EACT;AACF;AASA,MAAM,SAAS,IAAI,mBAAA;AAGnB,KAAK,iBAAiB,gBAAgB,MAAM;AAC1C,SAAO,eAAe,EAAA;AACxB,CAAC;AAED,MAAA,sBAAe;"}
@@ -1,7 +1,401 @@
1
- import { WorkerChannel } from "../../worker/WorkerChannel.js";
2
- import { WorkerMessageType, WorkerState } from "../../worker/types.js";
3
- import { VideoChunkDecoder } from "./VideoChunkDecoder.js";
4
- import { AudioChunkDecoder } from "./AudioChunkDecoder.js";
1
+ import { W as WorkerChannel, a as WorkerMessageType, b as WorkerState } from "../../WorkerChannel.js";
2
+ class BaseDecoder {
3
+ decoder;
4
+ config;
5
+ controller = null;
6
+ constructor(config) {
7
+ this.config = config;
8
+ }
9
+ async initialize() {
10
+ if (this.decoder?.state === "configured") {
11
+ return;
12
+ }
13
+ const isSupported = await this.isConfigSupported(this.config);
14
+ if (!isSupported.supported) {
15
+ throw new Error(
16
+ `Codec not supported: ${this.config.codecString || this.config.codec}`
17
+ );
18
+ }
19
+ this.decoder = this.createDecoder({
20
+ output: this.handleOutput.bind(this),
21
+ error: this.handleError.bind(this)
22
+ });
23
+ await this.configureDecoder(this.config);
24
+ }
25
+ async reconfigure(config) {
26
+ this.config = { ...this.config, ...config };
27
+ if (!this.decoder) {
28
+ await this.initialize();
29
+ return;
30
+ }
31
+ if (this.decoder.state === "configured") {
32
+ await this.decoder.flush();
33
+ }
34
+ const isSupported = await this.isConfigSupported(this.config);
35
+ if (!isSupported.supported) {
36
+ throw new Error(
37
+ `New configuration not supported: ${this.config.codecString || this.config.codec}`
38
+ );
39
+ }
40
+ await this.configureDecoder(this.config);
41
+ }
42
+ async flush() {
43
+ if (!this.decoder) return;
44
+ await this.decoder.flush();
45
+ }
46
+ async reset() {
47
+ if (!this.decoder) return;
48
+ this.decoder.reset();
49
+ this.onReset();
50
+ }
51
+ async close() {
52
+ if (!this.decoder) return;
53
+ if (this.decoder.state === "configured") {
54
+ await this.decoder.flush();
55
+ }
56
+ this.decoder.close();
57
+ this.decoder = void 0;
58
+ }
59
+ get isReady() {
60
+ return this.decoder?.state === "configured";
61
+ }
62
+ get queueSize() {
63
+ return this.decoder?.decodeQueueSize ?? 0;
64
+ }
65
+ handleOutput(data) {
66
+ if (!this.controller) {
67
+ this.closeIfPossible(data);
68
+ return;
69
+ }
70
+ try {
71
+ this.controller.enqueue(data);
72
+ } catch (error) {
73
+ if (error instanceof TypeError && /Cannot enqueue a chunk into a readable stream that is closed/.test(error.message)) {
74
+ this.closeIfPossible(data);
75
+ return;
76
+ }
77
+ throw error;
78
+ }
79
+ }
80
+ handleError(error) {
81
+ console.error(`${this.getDecoderType()} decoder error:`, error);
82
+ this.controller?.error(error);
83
+ }
84
+ closeIfPossible(data) {
85
+ data?.close?.();
86
+ data?.frame?.close?.();
87
+ }
88
+ onReset() {
89
+ }
90
+ createStream() {
91
+ return new TransformStream(
92
+ {
93
+ start: async (controller) => {
94
+ this.controller = controller;
95
+ if (!this.isReady) {
96
+ await this.initialize();
97
+ }
98
+ },
99
+ transform: async (input) => {
100
+ if (!this.decoder || this.decoder.state !== "configured") {
101
+ throw new Error("Decoder not configured");
102
+ }
103
+ if (this.decoder.decodeQueueSize >= this.decodeQueueThreshold) {
104
+ await new Promise((resolve) => {
105
+ const check = () => {
106
+ if (!this.decoder || this.decoder.decodeQueueSize < this.decodeQueueThreshold - 1) {
107
+ resolve();
108
+ } else {
109
+ setTimeout(check, 10);
110
+ }
111
+ };
112
+ check();
113
+ });
114
+ }
115
+ this.decode(input);
116
+ },
117
+ flush: async () => {
118
+ await this.flush();
119
+ }
120
+ },
121
+ {
122
+ highWaterMark: this.highWaterMark,
123
+ size: () => 1
124
+ }
125
+ );
126
+ }
127
+ }
128
+ class VideoChunkDecoder extends BaseDecoder {
129
+ static DEFAULT_HIGH_WATER_MARK = 4;
130
+ static DEFAULT_DECODE_QUEUE_THRESHOLD = 16;
131
+ trackId;
132
+ highWaterMark;
133
+ decodeQueueThreshold;
134
+ // GOP tracking (serial is derived from keyframe timestamp for idempotency)
135
+ currentGopSerial = -1;
136
+ isCurrentFrameKeyframe = false;
137
+ // Buffering support for delayed configuration
138
+ bufferedChunks = [];
139
+ isProcessingBuffer = false;
140
+ constructor(trackId, config) {
141
+ super(config || {});
142
+ this.trackId = trackId;
143
+ this.highWaterMark = config?.backpressure?.highWaterMark ?? VideoChunkDecoder.DEFAULT_HIGH_WATER_MARK;
144
+ this.decodeQueueThreshold = config?.backpressure?.decodeQueueThreshold ?? VideoChunkDecoder.DEFAULT_DECODE_QUEUE_THRESHOLD;
145
+ }
146
+ // Computed properties
147
+ get isConfigured() {
148
+ return this.isReady;
149
+ }
150
+ get state() {
151
+ return this.decoder?.state || "unconfigured";
152
+ }
153
+ /**
154
+ * Update configuration - can be called before or after initialization
155
+ */
156
+ async updateConfig(config) {
157
+ if (!this.isReady && config.codec) {
158
+ await this.configure(config);
159
+ await this.processBufferedChunks();
160
+ }
161
+ }
162
+ // Override createStream to handle GOP tracking and buffering
163
+ // Always create new stream for each clip (ReadableStreams can only be consumed once)
164
+ createStream() {
165
+ return new TransformStream(
166
+ {
167
+ start: async (controller) => {
168
+ this.controller = controller;
169
+ if (this.config?.codec && this.config?.description && !this.isReady) {
170
+ await this.initialize();
171
+ }
172
+ },
173
+ transform: async (chunk) => {
174
+ if (!this.isReady) {
175
+ this.bufferedChunks.push(chunk);
176
+ return;
177
+ }
178
+ if (this.isProcessingBuffer) {
179
+ this.bufferedChunks.push(chunk);
180
+ return;
181
+ }
182
+ await this.processChunk(chunk);
183
+ },
184
+ flush: async () => {
185
+ if (this.isReady) {
186
+ await this.flush();
187
+ }
188
+ }
189
+ },
190
+ {
191
+ highWaterMark: this.highWaterMark,
192
+ size: () => 1
193
+ }
194
+ );
195
+ }
196
+ /**
197
+ * Process a single chunk (extracted from transform for reuse)
198
+ */
199
+ async processChunk(chunk) {
200
+ if (!this.decoder) {
201
+ throw new Error("Decoder not initialized");
202
+ }
203
+ if (this.decoder.state !== "configured") {
204
+ console.error("[VideoChunkDecoder] Decoder in unexpected state:", this.decoder.state);
205
+ throw new Error(`Decoder not configured, state: ${this.decoder.state}`);
206
+ }
207
+ if (chunk.type === "key") {
208
+ this.handleKeyFrame(chunk.timestamp);
209
+ }
210
+ if (this.decoder.decodeQueueSize >= this.decodeQueueThreshold) {
211
+ await new Promise((resolve) => {
212
+ const check = () => {
213
+ if (!this.decoder || this.decoder.decodeQueueSize < this.decodeQueueThreshold - 1) {
214
+ resolve();
215
+ } else {
216
+ setTimeout(check, 20);
217
+ }
218
+ };
219
+ check();
220
+ });
221
+ }
222
+ try {
223
+ this.decode(chunk);
224
+ } catch (error) {
225
+ console.error(`[VideoChunkDecoder] decode error:`, error);
226
+ throw error;
227
+ }
228
+ }
229
+ // Override handleOutput to attach GOP metadata
230
+ handleOutput(frame) {
231
+ const wrappedFrame = {
232
+ frame,
233
+ gopSerial: this.currentGopSerial,
234
+ isKeyframe: this.isCurrentFrameKeyframe,
235
+ timestamp: frame.timestamp
236
+ };
237
+ this.isCurrentFrameKeyframe = false;
238
+ super.handleOutput(wrappedFrame);
239
+ }
240
+ // Implement abstract methods
241
+ async isConfigSupported(config) {
242
+ const result = await VideoDecoder.isConfigSupported({
243
+ codec: config.codec,
244
+ codedWidth: config.width,
245
+ codedHeight: config.height
246
+ });
247
+ return { supported: result.supported ?? false };
248
+ }
249
+ createDecoder(init) {
250
+ return new VideoDecoder(init);
251
+ }
252
+ getDecoderType() {
253
+ return "Video";
254
+ }
255
+ async configureDecoder(config) {
256
+ if (!this.decoder) return;
257
+ const decoderConfig = {
258
+ codec: config.codec,
259
+ codedWidth: config.width,
260
+ codedHeight: config.height,
261
+ hardwareAcceleration: config.hardwareAcceleration || "prefer-hardware",
262
+ optimizeForLatency: false,
263
+ ...config.description && { description: config.description },
264
+ ...config.displayAspectWidth && { displayAspectWidth: config.displayAspectWidth },
265
+ ...config.displayAspectHeight && { displayAspectHeight: config.displayAspectHeight }
266
+ };
267
+ this.decoder.configure(decoderConfig);
268
+ }
269
+ decode(chunk) {
270
+ this.decoder?.decode(chunk);
271
+ }
272
+ // Override reset to clear GOP data
273
+ onReset() {
274
+ this.currentGopSerial = -1;
275
+ this.isCurrentFrameKeyframe = false;
276
+ }
277
+ // GOP management methods
278
+ handleKeyFrame(timestamp) {
279
+ this.currentGopSerial = timestamp;
280
+ this.isCurrentFrameKeyframe = true;
281
+ }
282
+ /**
283
+ * Configure the decoder with codec info (can be called after creation)
284
+ */
285
+ async configure(config) {
286
+ if (this.isReady) {
287
+ await this.reconfigure(config);
288
+ return;
289
+ }
290
+ this.config = config;
291
+ await this.initialize();
292
+ }
293
+ /**
294
+ * Process any buffered chunks after configuration
295
+ */
296
+ async processBufferedChunks() {
297
+ if (!this.isReady || this.bufferedChunks.length === 0) {
298
+ return;
299
+ }
300
+ this.isProcessingBuffer = true;
301
+ const chunks = [...this.bufferedChunks];
302
+ this.bufferedChunks = [];
303
+ for (const chunk of chunks) {
304
+ try {
305
+ await this.processChunk(chunk);
306
+ } catch (error) {
307
+ console.error("[VideoChunkDecoder] Error processing buffered chunk:", error);
308
+ }
309
+ }
310
+ this.isProcessingBuffer = false;
311
+ if (this.bufferedChunks.length > 0) {
312
+ await this.processBufferedChunks();
313
+ }
314
+ }
315
+ // Override close to clean up GOP data
316
+ async close() {
317
+ this.bufferedChunks = [];
318
+ this.onReset();
319
+ await super.close();
320
+ }
321
+ }
322
+ class AudioChunkDecoder extends BaseDecoder {
323
+ // Default values
324
+ static DEFAULT_HIGH_WATER_MARK = 20;
325
+ static DEFAULT_DECODE_QUEUE_THRESHOLD = 16;
326
+ // Exposed properties
327
+ trackId;
328
+ // Backpressure configuration
329
+ highWaterMark;
330
+ decodeQueueThreshold;
331
+ constructor(trackId, config) {
332
+ super(config);
333
+ this.trackId = trackId;
334
+ this.highWaterMark = config?.backpressure?.highWaterMark ?? AudioChunkDecoder.DEFAULT_HIGH_WATER_MARK;
335
+ this.decodeQueueThreshold = AudioChunkDecoder.DEFAULT_DECODE_QUEUE_THRESHOLD;
336
+ }
337
+ // Computed properties
338
+ get isConfigured() {
339
+ return this.isReady;
340
+ }
341
+ get state() {
342
+ return this.decoder?.state || "unconfigured";
343
+ }
344
+ /**
345
+ * Update configuration - can be called before or after initialization
346
+ */
347
+ async updateConfig(config) {
348
+ if (!this.isReady && config.codec) {
349
+ await this.configure(config);
350
+ await this.processBufferedChunks();
351
+ return;
352
+ }
353
+ }
354
+ // Implement abstract methods
355
+ async isConfigSupported(config) {
356
+ const result = await AudioDecoder.isConfigSupported({
357
+ codec: config.codec,
358
+ sampleRate: config.sampleRate,
359
+ numberOfChannels: config.numberOfChannels
360
+ });
361
+ return { supported: result.supported ?? false };
362
+ }
363
+ createDecoder(init) {
364
+ return new AudioDecoder(init);
365
+ }
366
+ getDecoderType() {
367
+ return "Audio";
368
+ }
369
+ async configureDecoder(config) {
370
+ if (!this.decoder) return;
371
+ await this.decoder.configure({
372
+ codec: config.codec,
373
+ sampleRate: config.sampleRate,
374
+ numberOfChannels: config.numberOfChannels,
375
+ ...config.description && { description: config.description }
376
+ });
377
+ }
378
+ decode(chunk) {
379
+ this.decoder?.decode(chunk);
380
+ }
381
+ /**
382
+ * Configure the decoder with codec info (can be called after creation)
383
+ */
384
+ async configure(config) {
385
+ if (this.isReady) {
386
+ await this.reconfigure(config);
387
+ return;
388
+ }
389
+ this.config = config;
390
+ await this.initialize();
391
+ }
392
+ /**
393
+ * Process any buffered chunks after configuration
394
+ * Note: Audio doesn't buffer in current implementation, but keeping for interface consistency
395
+ */
396
+ async processBufferedChunks() {
397
+ }
398
+ }
5
399
  const normalizeDescription = (desc) => {
6
400
  if (!desc) return void 0;
7
401
  if (desc instanceof ArrayBuffer) return desc;
@@ -76,17 +470,7 @@ class DecodeWorker {
76
470
  * @param payload.initial - If true, initialize worker and recreate decoder instances; otherwise just update config
77
471
  */
78
472
  async handleConfigure(payload) {
79
- const {
80
- clipId,
81
- streamType,
82
- codec,
83
- width,
84
- height,
85
- sampleRate,
86
- numberOfChannels,
87
- description,
88
- range
89
- } = payload;
473
+ const { clipId, streamType, codec, width, height, sampleRate, numberOfChannels, description } = payload;
90
474
  if (clipId && streamType) {
91
475
  try {
92
476
  if (streamType === "video") {
@@ -96,8 +480,7 @@ class DecodeWorker {
96
480
  codec,
97
481
  width,
98
482
  height,
99
- description: normalizeDescription(description),
100
- ...range && { range }
483
+ description: normalizeDescription(description)
101
484
  });
102
485
  }
103
486
  } else if (streamType === "audio") {
@@ -146,7 +529,6 @@ class DecodeWorker {
146
529
  async handleReceiveStream(stream, metadata) {
147
530
  const clipId = metadata?.clipId || "default";
148
531
  const streamType = metadata?.streamType;
149
- console.log("[DecodeWorker] handleReceiveStream", streamType, clipId, metadata);
150
532
  if (streamType === "video") {
151
533
  const decoder = await this.getOrCreateDecoder("video", clipId, metadata);
152
534
  const transform = decoder.createStream();
@@ -298,8 +680,7 @@ class DecodeWorker {
298
680
  codec: metadata.codec,
299
681
  width: metadata.width,
300
682
  height: metadata.height,
301
- description: normalizeDescription(metadata.description),
302
- ...metadata.range && { range: metadata.range }
683
+ description: normalizeDescription(metadata.description)
303
684
  } : void 0
304
685
  );
305
686
  this.evictIfNeeded("video");