@editframe/elements 0.19.2-beta.0 → 0.20.0-beta.0

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 (96) hide show
  1. package/dist/elements/ContextProxiesController.d.ts +40 -0
  2. package/dist/elements/ContextProxiesController.js +69 -0
  3. package/dist/elements/EFCaptions.d.ts +45 -6
  4. package/dist/elements/EFCaptions.js +220 -26
  5. package/dist/elements/EFImage.js +4 -1
  6. package/dist/elements/EFMedia/AssetIdMediaEngine.d.ts +2 -1
  7. package/dist/elements/EFMedia/AssetIdMediaEngine.js +9 -0
  8. package/dist/elements/EFMedia/AssetMediaEngine.d.ts +1 -0
  9. package/dist/elements/EFMedia/AssetMediaEngine.js +11 -0
  10. package/dist/elements/EFMedia/BaseMediaEngine.d.ts +13 -1
  11. package/dist/elements/EFMedia/BaseMediaEngine.js +9 -0
  12. package/dist/elements/EFMedia/JitMediaEngine.d.ts +7 -1
  13. package/dist/elements/EFMedia/JitMediaEngine.js +24 -0
  14. package/dist/elements/EFMedia/shared/GlobalInputCache.d.ts +39 -0
  15. package/dist/elements/EFMedia/shared/GlobalInputCache.js +57 -0
  16. package/dist/elements/EFMedia/shared/ThumbnailExtractor.d.ts +27 -0
  17. package/dist/elements/EFMedia/shared/ThumbnailExtractor.js +106 -0
  18. package/dist/elements/EFMedia.js +25 -1
  19. package/dist/elements/EFSurface.browsertest.d.ts +0 -0
  20. package/dist/elements/EFSurface.d.ts +30 -0
  21. package/dist/elements/EFSurface.js +96 -0
  22. package/dist/elements/EFTemporal.js +7 -6
  23. package/dist/elements/EFThumbnailStrip.browsertest.d.ts +0 -0
  24. package/dist/elements/EFThumbnailStrip.d.ts +86 -0
  25. package/dist/elements/EFThumbnailStrip.js +490 -0
  26. package/dist/elements/EFThumbnailStrip.media-engine.browsertest.d.ts +0 -0
  27. package/dist/elements/EFTimegroup.d.ts +7 -7
  28. package/dist/elements/EFTimegroup.js +59 -16
  29. package/dist/elements/updateAnimations.browsertest.d.ts +13 -0
  30. package/dist/elements/updateAnimations.d.ts +5 -0
  31. package/dist/elements/updateAnimations.js +37 -13
  32. package/dist/getRenderInfo.js +1 -1
  33. package/dist/gui/ContextMixin.js +27 -14
  34. package/dist/gui/EFControls.browsertest.d.ts +0 -0
  35. package/dist/gui/EFControls.d.ts +38 -0
  36. package/dist/gui/EFControls.js +51 -0
  37. package/dist/gui/EFFilmstrip.d.ts +40 -1
  38. package/dist/gui/EFFilmstrip.js +240 -3
  39. package/dist/gui/EFPreview.js +2 -1
  40. package/dist/gui/EFScrubber.d.ts +6 -5
  41. package/dist/gui/EFScrubber.js +31 -21
  42. package/dist/gui/EFTimeDisplay.browsertest.d.ts +0 -0
  43. package/dist/gui/EFTimeDisplay.d.ts +2 -6
  44. package/dist/gui/EFTimeDisplay.js +13 -23
  45. package/dist/gui/TWMixin.js +1 -1
  46. package/dist/gui/currentTimeContext.d.ts +3 -0
  47. package/dist/gui/currentTimeContext.js +3 -0
  48. package/dist/gui/durationContext.d.ts +3 -0
  49. package/dist/gui/durationContext.js +3 -0
  50. package/dist/index.d.ts +3 -0
  51. package/dist/index.js +4 -1
  52. package/dist/style.css +1 -1
  53. package/dist/transcoding/types/index.d.ts +11 -0
  54. package/dist/utils/LRUCache.d.ts +46 -0
  55. package/dist/utils/LRUCache.js +382 -1
  56. package/dist/utils/LRUCache.test.d.ts +1 -0
  57. package/package.json +2 -2
  58. package/src/elements/ContextProxiesController.ts +123 -0
  59. package/src/elements/EFCaptions.browsertest.ts +1820 -0
  60. package/src/elements/EFCaptions.ts +373 -36
  61. package/src/elements/EFImage.ts +4 -1
  62. package/src/elements/EFMedia/AssetIdMediaEngine.ts +30 -1
  63. package/src/elements/EFMedia/AssetMediaEngine.ts +33 -0
  64. package/src/elements/EFMedia/BaseMediaEngine.browsertest.ts +3 -8
  65. package/src/elements/EFMedia/BaseMediaEngine.ts +35 -0
  66. package/src/elements/EFMedia/JitMediaEngine.ts +48 -0
  67. package/src/elements/EFMedia/shared/GlobalInputCache.ts +77 -0
  68. package/src/elements/EFMedia/shared/ThumbnailExtractor.ts +227 -0
  69. package/src/elements/EFMedia.ts +38 -1
  70. package/src/elements/EFSurface.browsertest.ts +155 -0
  71. package/src/elements/EFSurface.ts +141 -0
  72. package/src/elements/EFTemporal.ts +14 -8
  73. package/src/elements/EFThumbnailStrip.browsertest.ts +591 -0
  74. package/src/elements/EFThumbnailStrip.media-engine.browsertest.ts +713 -0
  75. package/src/elements/EFThumbnailStrip.ts +905 -0
  76. package/src/elements/EFTimegroup.browsertest.ts +56 -7
  77. package/src/elements/EFTimegroup.ts +88 -18
  78. package/src/elements/updateAnimations.browsertest.ts +361 -12
  79. package/src/elements/updateAnimations.ts +68 -19
  80. package/src/gui/ContextMixin.browsertest.ts +0 -25
  81. package/src/gui/ContextMixin.ts +44 -20
  82. package/src/gui/EFControls.browsertest.ts +175 -0
  83. package/src/gui/EFControls.ts +84 -0
  84. package/src/gui/EFFilmstrip.ts +323 -4
  85. package/src/gui/EFPreview.ts +2 -1
  86. package/src/gui/EFScrubber.ts +29 -25
  87. package/src/gui/EFTimeDisplay.browsertest.ts +237 -0
  88. package/src/gui/EFTimeDisplay.ts +12 -40
  89. package/src/gui/currentTimeContext.ts +5 -0
  90. package/src/gui/durationContext.ts +3 -0
  91. package/src/transcoding/types/index.ts +13 -0
  92. package/src/utils/LRUCache.test.ts +272 -0
  93. package/src/utils/LRUCache.ts +543 -0
  94. package/types.json +1 -1
  95. package/dist/transcoding/cache/CacheManager.d.ts +0 -73
  96. package/src/transcoding/cache/CacheManager.ts +0 -208
@@ -0,0 +1,905 @@
1
+ import { Task } from "@lit/task";
2
+ import { css, html, LitElement } from "lit";
3
+ import { customElement, property, state } from "lit/decorators.js";
4
+ import { createRef, ref } from "lit/directives/ref.js";
5
+ import type { MediaEngine as ImportedMediaEngine } from "../transcoding/types/index.js";
6
+ import { OrderedLRUCache } from "../utils/LRUCache.js";
7
+ import type { EFVideo } from "./EFVideo.js";
8
+ import { TargetController } from "./TargetController.ts";
9
+
10
+ /**
11
+ * Global thumbnail image cache for smooth resize performance
12
+ * Shared across all thumbnail strip instances
13
+ * Uses OrderedLRUCache for efficient timestamp-based searching
14
+ */
15
+ const thumbnailImageCache = new OrderedLRUCache<string, ImageData>(
16
+ 200,
17
+ (a, b) => {
18
+ // Extract timestamp from cache key for ordered searching (take last part after splitting on ':')
19
+ const partsA = a.split(":");
20
+ const partsB = b.split(":");
21
+ const timeA = Number.parseFloat(partsA[partsA.length - 1] || "0");
22
+ const timeB = Number.parseFloat(partsB[partsB.length - 1] || "0");
23
+ return timeA - timeB;
24
+ },
25
+ );
26
+
27
+ // Export for debugging (works in both browser and server)
28
+ (
29
+ globalThis as typeof globalThis & {
30
+ debugThumbnailCache: typeof thumbnailImageCache;
31
+ }
32
+ ).debugThumbnailCache = thumbnailImageCache;
33
+
34
+ /**
35
+ * Quantize timestamp to 30fps frame boundaries for consistent caching
36
+ * This eliminates cache misses from floating point precision differences
37
+ */
38
+ function quantizeTimestamp(timeMs: number): number {
39
+ const frameIntervalMs = 1000 / 30; // 33.33ms at 30fps
40
+ return Math.round(timeMs / frameIntervalMs) * frameIntervalMs;
41
+ }
42
+
43
+ /**
44
+ * Generate cache key for thumbnail image data (dimension-independent, quantized)
45
+ */
46
+ function getThumbnailCacheKey(videoSrc: string, timeMs: number): string {
47
+ const quantizedTimeMs = quantizeTimestamp(timeMs);
48
+ return `${videoSrc}:${quantizedTimeMs}`;
49
+ }
50
+
51
+ // Constants for consistent thumbnail layout
52
+ const THUMBNAIL_GAP = 1; // 1px gap between thumbnails
53
+ const STRIP_BORDER_PADDING = 4; // Account for border/padding in available height
54
+
55
+ interface ThumbnailSegment {
56
+ segmentId: number;
57
+ thumbnails: Array<{
58
+ timeMs: number;
59
+ }>;
60
+ }
61
+
62
+ interface ThumbnailLayout {
63
+ count: number;
64
+ segments: ThumbnailSegment[];
65
+ }
66
+
67
+ // Use the imported MediaEngine type and mediabunny types
68
+
69
+ interface ThumbnailRenderInfo {
70
+ timeMs: number;
71
+ segmentId: number;
72
+ x: number;
73
+ width: number;
74
+ height: number;
75
+ status: "exact-hit" | "near-hit" | "missing" | "loading";
76
+ imageData?: ImageData;
77
+ nearHitKey?: string;
78
+ }
79
+
80
+ /**
81
+ * Calculate optimal thumbnail count and timestamps for the strip
82
+ * Groups thumbnails by scrub segment ID for efficient caching
83
+ */
84
+ function calculateThumbnailLayout(
85
+ stripWidth: number,
86
+ thumbnailWidth: number,
87
+ startTimeMs: number,
88
+ endTimeMs: number,
89
+ scrubSegmentDurationMs?: number,
90
+ ): ThumbnailLayout {
91
+ // Must have positive width and valid time range
92
+ if (stripWidth <= 0 || thumbnailWidth <= 0 || endTimeMs <= startTimeMs) {
93
+ return { count: 0, segments: [] };
94
+ }
95
+
96
+ // Simple calculation: how many full thumbnails fit, plus one more to fill the width
97
+ const thumbnailPitch = thumbnailWidth + THUMBNAIL_GAP;
98
+ const baseFitCount = Math.floor(stripWidth / thumbnailPitch);
99
+ const count = Math.max(1, baseFitCount + 1); // Always one extra to fill width
100
+
101
+ // Generate timestamps evenly distributed across time range
102
+ const timestamps: number[] = [];
103
+ const timeRange = endTimeMs - startTimeMs;
104
+
105
+ for (let i = 0; i < count; i++) {
106
+ const timeMs =
107
+ count === 1
108
+ ? (startTimeMs + endTimeMs) / 2
109
+ : startTimeMs + (i * timeRange) / (count - 1);
110
+ timestamps.push(timeMs);
111
+ }
112
+
113
+ // Group by segment ID
114
+ const segmentMap = new Map<number, Array<{ timeMs: number }>>();
115
+ for (const timeMs of timestamps) {
116
+ const segmentId = scrubSegmentDurationMs
117
+ ? Math.floor(timeMs / scrubSegmentDurationMs)
118
+ : 0;
119
+ if (!segmentMap.has(segmentId)) {
120
+ segmentMap.set(segmentId, []);
121
+ }
122
+ // biome-ignore lint/style/noNonNullAssertion: Set in line above
123
+ segmentMap.get(segmentId)!.push({ timeMs });
124
+ }
125
+
126
+ const segments = Array.from(segmentMap.entries())
127
+ .sort(([a], [b]) => a - b)
128
+ .map(([segmentId, thumbnails]) => ({ segmentId, thumbnails }));
129
+
130
+ return { count, segments };
131
+ }
132
+
133
+ @customElement("ef-thumbnail-strip")
134
+ export class EFThumbnailStrip extends LitElement {
135
+ static styles = [
136
+ css`
137
+ :host {
138
+ display: block;
139
+ position: relative;
140
+ width: 100%;
141
+ height: 48px; /* Default filmstrip height */
142
+ background: #2a2a2a;
143
+ border: 2px solid #333;
144
+ border-radius: 6px;
145
+ overflow: hidden;
146
+ box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3);
147
+ }
148
+ canvas {
149
+ display: block;
150
+ position: absolute;
151
+ top: 0;
152
+ left: 0;
153
+ image-rendering: pixelated; /* Keep thumbnails crisp */
154
+ /* Width and height set programmatically to prevent CSS scaling */
155
+ }
156
+ .loading-overlay {
157
+ position: absolute;
158
+ top: 0;
159
+ left: 0;
160
+ right: 0;
161
+ bottom: 0;
162
+ background: rgba(42, 42, 42, 0.9);
163
+ display: flex;
164
+ align-items: center;
165
+ justify-content: center;
166
+ font-size: 11px;
167
+ color: #ccc;
168
+ font-weight: 500;
169
+ }
170
+ `,
171
+ ];
172
+
173
+ canvasRef = createRef<HTMLCanvasElement>();
174
+
175
+ // Target video element using the same pattern as EFSurface
176
+ // @ts-expect-error controller is intentionally not referenced directly to prevent GC
177
+ private _targetController: TargetController = new TargetController(
178
+ this as any,
179
+ );
180
+
181
+ private _targetElement: EFVideo | null = null;
182
+
183
+ @state()
184
+ get targetElement(): EFVideo | null {
185
+ return this._targetElement;
186
+ }
187
+
188
+ set targetElement(value: EFVideo | null) {
189
+ const oldValue = this._targetElement;
190
+ this._targetElement = value;
191
+
192
+ // Clean up previous video property observer
193
+ this._videoPropertyObserver?.disconnect();
194
+
195
+ // When target element changes, set up property watching and media engine listener
196
+ if (value && value !== oldValue) {
197
+ // Watch for video property changes that affect thumbnails
198
+ this._videoPropertyObserver = new MutationObserver((mutations) => {
199
+ let shouldUpdate = false;
200
+ for (const mutation of mutations) {
201
+ if (mutation.type === "attributes" && mutation.attributeName) {
202
+ const attr = mutation.attributeName;
203
+ if (
204
+ attr === "trimstart" ||
205
+ attr === "trimend" ||
206
+ attr === "sourcein" ||
207
+ attr === "sourceout" ||
208
+ attr === "src"
209
+ ) {
210
+ shouldUpdate = true;
211
+ break;
212
+ }
213
+ }
214
+ }
215
+ if (shouldUpdate) {
216
+ this.runThumbnailUpdate();
217
+ }
218
+ });
219
+
220
+ this._videoPropertyObserver.observe(value, {
221
+ attributes: true,
222
+ attributeFilter: [
223
+ "trimstart",
224
+ "trimend",
225
+ "sourcein",
226
+ "sourceout",
227
+ "src",
228
+ ],
229
+ });
230
+
231
+ // Listen for media engine ready
232
+ if (value.mediaEngineTask) {
233
+ value.mediaEngineTask.taskComplete
234
+ .then(() => {
235
+ // When media engine is ready, retrigger thumbnails if we have width
236
+ if (this._stripWidth > 0) {
237
+ this.thumbnailLayoutTask.run();
238
+ }
239
+ })
240
+ .catch(() => {
241
+ // Ignore media engine errors
242
+ });
243
+ }
244
+ }
245
+
246
+ this.requestUpdate("targetElement", oldValue);
247
+ }
248
+
249
+ @property({ type: String })
250
+ target = "";
251
+
252
+ /**
253
+ * Desired thumbnail width in pixels (height is determined by aspect ratio)
254
+ * Number of thumbnails is derived from this and available strip width
255
+ */
256
+ @property({ type: Number, attribute: "thumbnail-width" })
257
+ thumbnailWidth = 80;
258
+
259
+ /**
260
+ * Custom start time in milliseconds relative to trimmed timeline (0 = start of trimmed portion)
261
+ * In trimmed mode: 0ms = sourceStartMs, 1000ms = sourceStartMs + 1000ms
262
+ * In intrinsic mode: 0ms = 0ms in source media
263
+ */
264
+ @property({ type: Number, attribute: "start-time-ms" })
265
+ startTimeMs?: number;
266
+
267
+ /**
268
+ * Custom end time in milliseconds relative to trimmed timeline
269
+ * In trimmed mode: relative to sourceStartMs
270
+ * In intrinsic mode: relative to source media start (0ms)
271
+ */
272
+ @property({ type: Number, attribute: "end-time-ms" })
273
+ endTimeMs?: number;
274
+
275
+ /**
276
+ * Use intrinsic duration instead of trimmed duration
277
+ * Accepts "true"/"false" string values or boolean
278
+ */
279
+ @property({
280
+ type: Boolean,
281
+ attribute: "use-intrinsic-duration",
282
+ reflect: true,
283
+ converter: {
284
+ fromAttribute: (value: string | null) => {
285
+ if (value === null) return false;
286
+ return value === "true";
287
+ },
288
+ toAttribute: (value: boolean) => (value ? "true" : null),
289
+ },
290
+ })
291
+ useIntrinsicDuration = false;
292
+
293
+ private _stripWidth = 0;
294
+ private _stripHeight = 48; // Default height, updated by ResizeObserver
295
+ private _pendingStripWidth: number | undefined;
296
+ private _thumbnailLayoutTask: Promise<ThumbnailRenderInfo[]> | undefined;
297
+ @state()
298
+ private set stripWidth(value: number) {
299
+ if (this._thumbnailLayoutTask) {
300
+ this._pendingStripWidth = value;
301
+ return;
302
+ }
303
+ this._stripWidth = value;
304
+
305
+ if (value > 0) {
306
+ this._thumbnailLayoutTask = this.thumbnailLayoutTask
307
+ .run()
308
+ .then(async () => {
309
+ // Use taskComplete and .value instead of promise return value
310
+ await this.thumbnailLayoutTask.taskComplete;
311
+ const layout = this.thumbnailLayoutTask.value;
312
+ return layout ? this.runThumbnailRenderTask(layout) : [];
313
+ })
314
+ .finally(() => {
315
+ this._thumbnailLayoutTask = undefined;
316
+ if (this._pendingStripWidth) {
317
+ this.stripWidth = this._pendingStripWidth;
318
+ this._pendingStripWidth = undefined;
319
+ }
320
+ });
321
+ }
322
+ }
323
+ private get stripWidth() {
324
+ return this._stripWidth;
325
+ }
326
+
327
+ /**
328
+ * Run thumbnail render task directly with provided layout (bypasses task args dependency)
329
+ */
330
+ private async runThumbnailRenderTask(
331
+ layout: ThumbnailLayout,
332
+ ): Promise<ThumbnailRenderInfo[]> {
333
+ if (!layout || !this.targetElement || layout.count === 0) {
334
+ return [];
335
+ }
336
+
337
+ // Run the thumbnail render task logic directly
338
+ return this.renderThumbnails(
339
+ layout,
340
+ this.targetElement,
341
+ this.thumbnailWidth,
342
+ );
343
+ }
344
+
345
+ private resizeObserver?: ResizeObserver;
346
+ private _thumbnailUpdateInProgress = false;
347
+ private _pendingThumbnailUpdate = false;
348
+ private _videoPropertyObserver?: MutationObserver;
349
+
350
+ updated(changedProperties: Map<string | number | symbol, unknown>) {
351
+ super.updated(changedProperties);
352
+
353
+ // IMPLEMENTATION GUIDELINES: Fix for initial loading bug - ensure width is detected
354
+ if (this._stripWidth === 0) {
355
+ const width = this.clientWidth;
356
+ if (width > 0) {
357
+ this.stripWidth = width;
358
+ }
359
+ }
360
+
361
+ // IMPLEMENTATION GUIDELINES: Responsive debouncing for thumbnail property changes using EFTimegroup pattern
362
+ if (
363
+ changedProperties.has("thumbnailWidth") ||
364
+ changedProperties.has("startTimeMs") ||
365
+ changedProperties.has("endTimeMs") ||
366
+ changedProperties.has("useIntrinsicDuration")
367
+ ) {
368
+ this.runThumbnailUpdate();
369
+ }
370
+ }
371
+
372
+ /**
373
+ * Run thumbnail update with responsive debouncing (based on EFTimegroup currentTime pattern)
374
+ */
375
+ private runThumbnailUpdate() {
376
+ // If update already in progress, just flag that another update is needed
377
+ if (this._thumbnailUpdateInProgress) {
378
+ this._pendingThumbnailUpdate = true;
379
+ return;
380
+ }
381
+
382
+ this._thumbnailUpdateInProgress = true;
383
+
384
+ // Trigger full layout→render pipeline immediately for responsiveness
385
+ this.thumbnailLayoutTask
386
+ .run()
387
+ .then(async () => {
388
+ await this.thumbnailLayoutTask.taskComplete;
389
+ const layout = this.thumbnailLayoutTask.value;
390
+ if (layout) {
391
+ await this.runThumbnailRenderTask(layout);
392
+ }
393
+ })
394
+ .catch(() => {
395
+ // Ignore errors - thumbnails will show as placeholders
396
+ })
397
+ .finally(() => {
398
+ this._thumbnailUpdateInProgress = false;
399
+
400
+ // If more property changes came in while we were processing, run another update
401
+ if (this._pendingThumbnailUpdate) {
402
+ this._pendingThumbnailUpdate = false;
403
+ this.runThumbnailUpdate();
404
+ }
405
+ });
406
+ }
407
+
408
+ private thumbnailLayoutTask = new Task(this, {
409
+ autoRun: false,
410
+ task: async ([
411
+ stripWidth,
412
+ thumbnailWidth,
413
+ targetElement,
414
+ startTimeMs,
415
+ endTimeMs,
416
+ useIntrinsicDuration,
417
+ mediaEngine,
418
+ ]: readonly [
419
+ number,
420
+ number,
421
+ EFVideo | null,
422
+ number | undefined,
423
+ number | undefined,
424
+ boolean,
425
+ ImportedMediaEngine | null | undefined,
426
+ ]) => {
427
+ // Need valid dimensions and target element
428
+ if (stripWidth <= 0 || !targetElement) {
429
+ return { count: 0, segments: [] };
430
+ }
431
+
432
+ // IMPLEMENTATION GUIDELINES: Wait for media engine to be ready before generating thumbnails
433
+ if (!mediaEngine) {
434
+ // If no media engine yet, wait for it to be ready
435
+ if (targetElement.mediaEngineTask) {
436
+ await targetElement.mediaEngineTask.taskComplete;
437
+ // Get the media engine after it's ready
438
+ const readyMediaEngine = targetElement.mediaEngineTask.value;
439
+ if (!readyMediaEngine) {
440
+ return { count: 0, segments: [] };
441
+ }
442
+ // Continue with the ready media engine
443
+ return this.calculateLayoutWithMediaEngine(
444
+ stripWidth,
445
+ thumbnailWidth,
446
+ targetElement,
447
+ startTimeMs,
448
+ endTimeMs,
449
+ useIntrinsicDuration,
450
+ readyMediaEngine,
451
+ );
452
+ }
453
+ return { count: 0, segments: [] };
454
+ }
455
+
456
+ // Media engine is ready, proceed with layout calculation
457
+ return this.calculateLayoutWithMediaEngine(
458
+ stripWidth,
459
+ thumbnailWidth,
460
+ targetElement,
461
+ startTimeMs,
462
+ endTimeMs,
463
+ useIntrinsicDuration,
464
+ mediaEngine,
465
+ );
466
+ },
467
+ args: () =>
468
+ [
469
+ this.stripWidth,
470
+ this.thumbnailWidth,
471
+ this.targetElement,
472
+ this.startTimeMs,
473
+ this.endTimeMs,
474
+ this.useIntrinsicDuration,
475
+ this.targetElement?.mediaEngineTask?.value,
476
+ ] as const,
477
+ });
478
+
479
+ /**
480
+ * Calculate layout with a ready media engine
481
+ */
482
+ private calculateLayoutWithMediaEngine(
483
+ stripWidth: number,
484
+ thumbnailWidth: number,
485
+ targetElement: EFVideo,
486
+ startTimeMs: number | undefined,
487
+ endTimeMs: number | undefined,
488
+ useIntrinsicDuration: boolean,
489
+ mediaEngine: ImportedMediaEngine,
490
+ ) {
491
+ // Determine time range for thumbnails with correct timeline coordinate handling
492
+ if (useIntrinsicDuration) {
493
+ // INTRINSIC MODE: start-time-ms/end-time-ms are relative to source timeline (0 = source start)
494
+ const effectiveStartMs = startTimeMs ?? 0;
495
+ const effectiveEndMs =
496
+ endTimeMs ?? targetElement.intrinsicDurationMs ?? 0;
497
+
498
+ return this.generateLayoutFromTimeRange(
499
+ stripWidth,
500
+ thumbnailWidth,
501
+ effectiveStartMs,
502
+ effectiveEndMs,
503
+ mediaEngine,
504
+ );
505
+ }
506
+ // TRIMMED MODE: start-time-ms/end-time-ms are relative to trimmed timeline (0 = trim start)
507
+ const sourceStart = targetElement.sourceStartMs ?? 0;
508
+ const trimmedDuration = targetElement.durationMs ?? 0;
509
+
510
+ // Convert trimmed timeline coordinates to source timeline coordinates
511
+ const effectiveStartMs =
512
+ startTimeMs !== undefined
513
+ ? sourceStart + startTimeMs // Convert from trimmed timeline to source timeline
514
+ : sourceStart; // Default: start of trimmed portion
515
+
516
+ const effectiveEndMs =
517
+ endTimeMs !== undefined
518
+ ? sourceStart + endTimeMs // Convert from trimmed timeline to source timeline
519
+ : sourceStart + trimmedDuration; // Default: end of trimmed portion
520
+
521
+ return this.generateLayoutFromTimeRange(
522
+ stripWidth,
523
+ thumbnailWidth,
524
+ effectiveStartMs,
525
+ effectiveEndMs,
526
+ mediaEngine,
527
+ );
528
+ }
529
+
530
+ /**
531
+ * Generate layout from calculated time range
532
+ */
533
+ private generateLayoutFromTimeRange(
534
+ stripWidth: number,
535
+ thumbnailWidth: number,
536
+ effectiveStartMs: number,
537
+ effectiveEndMs: number,
538
+ mediaEngine: ImportedMediaEngine,
539
+ ) {
540
+ // Get scrub segment duration from media engine if available
541
+ const scrubSegmentDurationMs =
542
+ mediaEngine && typeof mediaEngine.getScrubVideoRendition === "function"
543
+ ? mediaEngine.getScrubVideoRendition()?.segmentDurationMs
544
+ : undefined;
545
+
546
+ // Generate layout using our algorithm with segment alignment
547
+ const layout = calculateThumbnailLayout(
548
+ stripWidth,
549
+ thumbnailWidth,
550
+ effectiveStartMs,
551
+ effectiveEndMs,
552
+ scrubSegmentDurationMs,
553
+ );
554
+
555
+ return layout;
556
+ }
557
+
558
+ private thumbnailRenderTask = new Task(this, {
559
+ autoRun: false,
560
+ task: async ([layout, targetElement, thumbnailWidth]: readonly [
561
+ ThumbnailLayout | null,
562
+ EFVideo | null,
563
+ number,
564
+ ]) => {
565
+ // Simplified task that delegates to renderThumbnails method
566
+ if (!layout || !targetElement) {
567
+ return [];
568
+ }
569
+ return this.renderThumbnails(layout, targetElement, thumbnailWidth);
570
+ },
571
+ args: () =>
572
+ [
573
+ this.thumbnailLayoutTask.value || null,
574
+ this.targetElement,
575
+ this.thumbnailWidth,
576
+ ] as const,
577
+ });
578
+
579
+ /**
580
+ * Render thumbnails with provided layout (main rendering logic)
581
+ */
582
+ private async renderThumbnails(
583
+ layout: ThumbnailLayout,
584
+ targetElement: EFVideo,
585
+ thumbnailWidth: number,
586
+ ): Promise<ThumbnailRenderInfo[]> {
587
+ if (!layout || !targetElement || layout.count === 0) {
588
+ return [];
589
+ }
590
+
591
+ const videoSrc = targetElement.src;
592
+ const availableHeight = this._stripHeight - STRIP_BORDER_PADDING; // Account for border/padding
593
+
594
+ const allThumbnails: ThumbnailRenderInfo[] = [];
595
+ let thumbnailIndex = 0; // Track ordinal position
596
+
597
+ // Process each segment
598
+ for (const segment of layout.segments) {
599
+ for (const thumbnail of segment.thumbnails) {
600
+ const cacheKey = getThumbnailCacheKey(videoSrc, thumbnail.timeMs);
601
+
602
+ // Try exact cache hit first
603
+ let imageData = thumbnailImageCache.get(cacheKey);
604
+ let status: ThumbnailRenderInfo["status"] = "exact-hit";
605
+ let nearHitKey: string | undefined;
606
+
607
+ if (!imageData) {
608
+ // Try near cache hit within 5 seconds using proper range search
609
+ const timeMinus = Math.max(0, thumbnail.timeMs - 5000);
610
+ const timePlus = thumbnail.timeMs + 5000;
611
+
612
+ // For range bounds, use raw timestamps (don't quantize the search range)
613
+ const rangeStartKey = `${videoSrc}:${timeMinus}`;
614
+ const rangeEndKey = `${videoSrc}:${timePlus}`;
615
+
616
+ // Use findRange to find any cached items in this time window
617
+ const nearHits = thumbnailImageCache.findRange(
618
+ rangeStartKey,
619
+ rangeEndKey,
620
+ );
621
+
622
+ // Filter to only include the same video source
623
+ const sameVideoHits = nearHits.filter((hit) =>
624
+ hit.key.startsWith(`${videoSrc}:`),
625
+ );
626
+
627
+ if (sameVideoHits.length > 0) {
628
+ // Get the closest match by time from same video
629
+ const nearestHit = sameVideoHits.reduce((closest, current) => {
630
+ const currentParts = current.key.split(":");
631
+ const closestParts = closest.key.split(":");
632
+ const currentTime = Number.parseFloat(
633
+ currentParts[currentParts.length - 1] || "0",
634
+ );
635
+ const closestTime = Number.parseFloat(
636
+ closestParts[closestParts.length - 1] || "0",
637
+ );
638
+ const currentDiff = Math.abs(currentTime - thumbnail.timeMs);
639
+ const closestDiff = Math.abs(closestTime - thumbnail.timeMs);
640
+ return currentDiff < closestDiff ? current : closest;
641
+ });
642
+
643
+ imageData = nearestHit.value;
644
+ status = "near-hit";
645
+ nearHitKey = nearestHit.key;
646
+ } else {
647
+ status = "missing";
648
+ }
649
+ }
650
+
651
+ // Fixed integer positioning - no floating point
652
+ const x = thumbnailIndex * (thumbnailWidth + THUMBNAIL_GAP);
653
+
654
+ allThumbnails.push({
655
+ timeMs: thumbnail.timeMs,
656
+ segmentId: segment.segmentId,
657
+ x,
658
+ width: thumbnailWidth, // Always exactly 80px
659
+ height: availableHeight, // Always exactly 44px
660
+ status,
661
+ imageData,
662
+ nearHitKey,
663
+ });
664
+
665
+ thumbnailIndex++; // Increment ordinal position
666
+ }
667
+ }
668
+
669
+ // Draw current state (cache hits and placeholders)
670
+ await this.drawThumbnails(allThumbnails);
671
+
672
+ // Load missing thumbnails from scrub tracks
673
+ await this.loadMissingThumbnails(allThumbnails, targetElement);
674
+
675
+ return allThumbnails;
676
+ }
677
+
678
+ connectedCallback() {
679
+ super.connectedCallback();
680
+
681
+ // Set up ResizeObserver to track element dimensions
682
+ this.resizeObserver = new ResizeObserver((entries) => {
683
+ for (const entry of entries) {
684
+ // Use borderBoxSize for accurate dimensions including borders/padding
685
+ const width =
686
+ entry.borderBoxSize && entry.borderBoxSize.length > 0
687
+ ? entry.borderBoxSize[0]?.inlineSize
688
+ : entry.contentRect.width;
689
+
690
+ const height =
691
+ entry.borderBoxSize && entry.borderBoxSize.length > 0
692
+ ? entry.borderBoxSize[0]?.blockSize
693
+ : entry.contentRect.height;
694
+
695
+ this._stripHeight = height ?? 0;
696
+ this.stripWidth = width ?? 0; // This triggers thumbnail layout update
697
+ }
698
+ });
699
+
700
+ this.resizeObserver.observe(this);
701
+
702
+ // Force initial width calculation after element is fully connected
703
+ this.updateComplete.then(() => {
704
+ if (this._stripWidth === 0) {
705
+ const width = this.clientWidth;
706
+ if (width > 0) {
707
+ this.stripWidth = width ?? 0;
708
+ }
709
+ }
710
+ });
711
+ }
712
+
713
+ disconnectedCallback() {
714
+ super.disconnectedCallback();
715
+ this.resizeObserver?.disconnect();
716
+ this.resizeObserver = undefined;
717
+
718
+ // Clean up video property observer
719
+ this._videoPropertyObserver?.disconnect();
720
+ this._videoPropertyObserver = undefined;
721
+ }
722
+
723
+ /**
724
+ * Draw thumbnails to the canvas with cache hits and placeholders
725
+ */
726
+ private async drawThumbnails(
727
+ thumbnails: ThumbnailRenderInfo[],
728
+ ): Promise<void> {
729
+ const canvas = this.canvasRef.value;
730
+ if (!canvas) {
731
+ return;
732
+ }
733
+
734
+ const ctx = canvas.getContext("2d");
735
+ if (!ctx) {
736
+ return;
737
+ }
738
+
739
+ // Set canvas to exact size we're drawing - prevents CSS scaling
740
+ const dpr = window.devicePixelRatio || 1;
741
+
742
+ // Set canvas buffer size for high DPI rendering
743
+ canvas.width = this._stripWidth * dpr;
744
+ canvas.height = this._stripHeight * dpr;
745
+
746
+ // Set canvas DOM size to exactly what we're drawing - no CSS scaling
747
+ canvas.style.width = `${this._stripWidth}px`;
748
+ canvas.style.height = `${this._stripHeight}px`;
749
+
750
+ // Scale the drawing context to match device pixel ratio
751
+ ctx.scale(dpr, dpr);
752
+
753
+ // Clear canvas (use logical pixel dimensions since context is scaled)
754
+ ctx.fillStyle = "#2a2a2a";
755
+ ctx.fillRect(0, 0, this._stripWidth, this._stripHeight);
756
+
757
+ // Draw each thumbnail with proper aspect ratio and centering
758
+ for (const thumb of thumbnails) {
759
+ if (thumb.imageData) {
760
+ // Draw cached thumbnail with aspect ratio preservation
761
+ const tempCanvas = document.createElement("canvas");
762
+ tempCanvas.width = thumb.imageData.width;
763
+ tempCanvas.height = thumb.imageData.height;
764
+ const tempCtx = tempCanvas.getContext("2d");
765
+ if (!tempCtx) {
766
+ continue;
767
+ }
768
+ tempCtx.putImageData(thumb.imageData, 0, 0);
769
+
770
+ // Preserve aspect ratio within fixed container bounds
771
+ const sourceAspect = thumb.imageData.width / thumb.imageData.height;
772
+ const containerAspect = thumb.width / thumb.height;
773
+
774
+ // Calculate aspect-ratio-preserving dimensions with integer coordinates
775
+ let drawWidth: number;
776
+ let drawHeight: number;
777
+ let drawX: number;
778
+ let drawY: number;
779
+
780
+ if (sourceAspect > containerAspect) {
781
+ // Source is wider - fit to container width, letterbox top/bottom
782
+ drawWidth = thumb.width;
783
+ drawHeight = Math.round(thumb.width / sourceAspect);
784
+ drawX = thumb.x;
785
+ drawY = Math.round((this._stripHeight - drawHeight) / 2);
786
+ } else {
787
+ // Source is taller - fit to container height, pillarbox left/right
788
+ drawWidth = Math.round(thumb.height * sourceAspect);
789
+ drawHeight = thumb.height;
790
+ drawX = thumb.x + Math.round((thumb.width - drawWidth) / 2);
791
+ drawY = Math.round((this._stripHeight - drawHeight) / 2);
792
+ }
793
+
794
+ // Draw with proper aspect ratio preservation
795
+ ctx.drawImage(tempCanvas, drawX, drawY, drawWidth, drawHeight);
796
+
797
+ // Add subtle indicator for near hits
798
+ if (thumb.status === "near-hit") {
799
+ ctx.fillStyle = "rgba(255, 165, 0, 0.3)";
800
+ ctx.fillRect(thumb.x, 0, thumb.width, 2);
801
+ }
802
+ } else {
803
+ // Draw placeholder - center vertically in strip with integer positioning
804
+ const placeholderY = Math.round((this._stripHeight - thumb.height) / 2);
805
+ ctx.fillStyle = "#404040";
806
+ ctx.fillRect(thumb.x, placeholderY, thumb.width, thumb.height);
807
+
808
+ // Add subtle loading indicator with integer positioning
809
+ ctx.strokeStyle = "#666";
810
+ ctx.lineWidth = 1;
811
+ ctx.setLineDash([2, 2]);
812
+ ctx.strokeRect(thumb.x, placeholderY, thumb.width, thumb.height);
813
+ ctx.setLineDash([]);
814
+ }
815
+ }
816
+ }
817
+
818
+ /**
819
+ * Load missing thumbnails using MediaEngine batch extraction
820
+ */
821
+ private async loadMissingThumbnails(
822
+ thumbnails: ThumbnailRenderInfo[],
823
+ targetElement: EFVideo,
824
+ ): Promise<void> {
825
+ const mediaEngine = targetElement.mediaEngineTask?.value;
826
+ if (!mediaEngine) {
827
+ return;
828
+ }
829
+
830
+ // Get all missing thumbnails
831
+ const missingThumbnails = thumbnails.filter(
832
+ (t) => t.status === "missing" || t.status === "near-hit",
833
+ );
834
+
835
+ if (missingThumbnails.length === 0) {
836
+ return;
837
+ }
838
+
839
+ // Update status to loading
840
+ for (const thumb of missingThumbnails) {
841
+ thumb.status = "loading";
842
+ }
843
+
844
+ // Batch extract all missing thumbnails using MediaEngine
845
+ const timestamps = missingThumbnails.map((t) => t.timeMs);
846
+
847
+ const thumbnailResults = await mediaEngine.extractThumbnails(timestamps);
848
+
849
+ // Convert canvases to ImageData and update thumbnails
850
+ for (let i = 0; i < missingThumbnails.length; i++) {
851
+ const thumb = missingThumbnails[i];
852
+ const thumbnailResult = thumbnailResults[i];
853
+
854
+ if (thumb && thumbnailResult) {
855
+ // Convert canvas to ImageData
856
+ const imageData = this.canvasToImageData(thumbnailResult.thumbnail);
857
+
858
+ if (imageData) {
859
+ const cacheKey = getThumbnailCacheKey(
860
+ targetElement.src,
861
+ thumb.timeMs,
862
+ );
863
+ thumbnailImageCache.set(cacheKey, imageData);
864
+ thumb.imageData = imageData;
865
+ thumb.status = "exact-hit";
866
+ }
867
+ }
868
+ }
869
+
870
+ // Redraw with newly loaded thumbnails
871
+ await this.drawThumbnails(thumbnails);
872
+ }
873
+
874
+ /**
875
+ * Convert Canvas to ImageData for caching
876
+ */
877
+ private canvasToImageData(
878
+ canvas: HTMLCanvasElement | OffscreenCanvas,
879
+ ): ImageData | null {
880
+ // Extract ImageData from canvas
881
+ const ctx = canvas.getContext("2d");
882
+ if (!ctx) {
883
+ return null;
884
+ }
885
+
886
+ return ctx.getImageData(0, 0, canvas.width, canvas.height);
887
+ }
888
+
889
+ render() {
890
+ return html`
891
+ <canvas ${ref(this.canvasRef)}></canvas>
892
+ ${this.thumbnailRenderTask.render({
893
+ pending: () => html``,
894
+ complete: () => html``,
895
+ error: (e) =>
896
+ html`<div class="error">Error loading thumbnails: ${e}</div>`,
897
+ })}
898
+ `;
899
+ }
900
+ }
901
+ declare global {
902
+ interface HTMLElementTagNameMap {
903
+ "ef-thumbnail-strip": EFThumbnailStrip;
904
+ }
905
+ }